build: Reduce build systems to just top level waf and autoconf
[amitay/samba.git] / buildtools / wafsamba / samba_abi.py
1 # functions for handling ABI checking of libraries
2
3 import Options, Utils, os, Logs, samba_utils, sys, Task, fnmatch, re, Build
4 from TaskGen import feature, before, after
5
6 # these type maps cope with platform specific names for common types
7 # please add new type mappings into the list below
8 abi_type_maps = {
9     '_Bool' : 'bool',
10     'struct __va_list_tag *' : 'va_list'
11     }
12
13 version_key = lambda x: map(int, x.split("."))
14
15 def normalise_signature(sig):
16     '''normalise a signature from gdb'''
17     sig = sig.strip()
18     sig = re.sub('^\$[0-9]+\s=\s\{*', '', sig)
19     sig = re.sub('\}(\s0x[0-9a-f]+\s<\w+>)?$', '', sig)
20     sig = re.sub('0x[0-9a-f]+', '0xXXXX', sig)
21
22     for t in abi_type_maps:
23         # we need to cope with non-word characters in mapped types
24         m = t
25         m = m.replace('*', '\*')
26         if m[-1].isalnum() or m[-1] == '_':
27             m += '\\b'
28         if m[0].isalnum() or m[0] == '_':
29             m = '\\b' + m
30         sig = re.sub(m, abi_type_maps[t], sig)
31     return sig
32
33 def normalise_varargs(sig):
34     '''cope with older versions of gdb'''
35     sig = re.sub(',\s\.\.\.', '', sig)
36     return sig
37
38 def parse_sigs(sigs, abi_match):
39     '''parse ABI signatures file'''
40     abi_match = samba_utils.TO_LIST(abi_match)
41     ret = {}
42     a = sigs.split('\n')
43     for s in a:
44         if s.find(':') == -1:
45             continue
46         sa = s.split(':')
47         if abi_match:
48             matched = False
49             for p in abi_match:
50                 if p[0] == '!' and fnmatch.fnmatch(sa[0], p[1:]):
51                     break
52                 elif fnmatch.fnmatch(sa[0], p):
53                     matched = True
54                     break
55             if not matched:
56                 continue
57         ret[sa[0]] = normalise_signature(sa[1])
58     return ret
59
60 def save_sigs(sig_file, parsed_sigs):
61     '''save ABI signatures to a file'''
62     sigs = ''
63     for s in sorted(parsed_sigs.keys()):
64         sigs += '%s: %s\n' % (s, parsed_sigs[s])
65     return samba_utils.save_file(sig_file, sigs, create_dir=True)
66
67
68 def abi_check_task(self):
69     '''check if the ABI has changed'''
70     abi_gen = self.ABI_GEN
71
72     libpath = self.inputs[0].abspath(self.env)
73     libname = os.path.basename(libpath)
74
75     sigs = Utils.cmd_output([abi_gen, libpath])
76     parsed_sigs = parse_sigs(sigs, self.ABI_MATCH)
77
78     sig_file = self.ABI_FILE
79
80     old_sigs = samba_utils.load_file(sig_file)
81     if old_sigs is None or Options.options.ABI_UPDATE:
82         if not save_sigs(sig_file, parsed_sigs):
83             raise Utils.WafError('Failed to save ABI file "%s"' % sig_file)
84         Logs.warn('Generated ABI signatures %s' % sig_file)
85         return
86
87     parsed_old_sigs = parse_sigs(old_sigs, self.ABI_MATCH)
88
89     # check all old sigs
90     got_error = False
91     for s in parsed_old_sigs:
92         if not s in parsed_sigs:
93             Logs.error('%s: symbol %s has been removed - please update major version\n\tsignature: %s' % (
94                 libname, s, parsed_old_sigs[s]))
95             got_error = True
96         elif normalise_varargs(parsed_old_sigs[s]) != normalise_varargs(parsed_sigs[s]):
97             Logs.error('%s: symbol %s has changed - please update major version\n\told_signature: %s\n\tnew_signature: %s' % (
98                 libname, s, parsed_old_sigs[s], parsed_sigs[s]))
99             got_error = True
100
101     for s in parsed_sigs:
102         if not s in parsed_old_sigs:
103             Logs.error('%s: symbol %s has been added - please mark it _PRIVATE_ or update minor version\n\tsignature: %s' % (
104                 libname, s, parsed_sigs[s]))
105             got_error = True
106
107     if got_error:
108         raise Utils.WafError('ABI for %s has changed - please fix library version then build with --abi-update\nSee http://wiki.samba.org/index.php/Waf#ABI_Checking for more information' % libname)
109
110
111 t = Task.task_type_from_func('abi_check', abi_check_task, color='BLUE', ext_in='.bin')
112 t.quiet = True
113 # allow "waf --abi-check" to force re-checking the ABI
114 if '--abi-check' in sys.argv:
115     Task.always_run(t)
116
117 @after('apply_link')
118 @feature('abi_check')
119 def abi_check(self):
120     '''check that ABI matches saved signatures'''
121     env = self.bld.env
122     if not env.ABI_CHECK or self.abi_directory is None:
123         return
124
125     # if the platform doesn't support -fvisibility=hidden then the ABI
126     # checks become fairly meaningless
127     if not env.HAVE_VISIBILITY_ATTR:
128         return
129
130     topsrc = self.bld.srcnode.abspath()
131     abi_gen = os.path.join(topsrc, 'buildtools/scripts/abi_gen.sh')
132
133     abi_file = "%s/%s-%s.sigs" % (self.abi_directory, self.name, self.vnum)
134
135     tsk = self.create_task('abi_check', self.link_task.outputs[0])
136     tsk.ABI_FILE = abi_file
137     tsk.ABI_MATCH = self.abi_match
138     tsk.ABI_GEN = abi_gen
139
140
141 def abi_process_file(fname, version, symmap):
142     '''process one ABI file, adding new symbols to the symmap'''
143     f = open(fname, mode='r')
144     for line in f:
145         symname = line.split(":")[0]
146         if not symname in symmap:
147             symmap[symname] = version
148     f.close()
149
150 def abi_write_vscript(vscript, libname, current_version, versions, symmap, abi_match):
151     '''write a vscript file for a library in --version-script format
152     
153     :param vscript: Path to the vscript file
154     :param libname: Name of the library, uppercased
155     :param current_version: Current version
156     :param versions: Versions to consider
157     :param symmap: Dictionary mapping symbols -> version
158     :param abi_match: List of symbols considered to be public in the current version
159     '''
160
161     invmap = {}
162     for s in symmap:
163         invmap.setdefault(symmap[s], []).append(s)
164
165     f = open(vscript, mode='w')
166     last_key = ""
167     versions = sorted(versions, key=version_key)
168     for k in versions:
169         symver = "%s_%s" % (libname, k)
170         if symver == current_version:
171             break
172         f.write("%s {\n" % symver)
173         if k in invmap:
174             f.write("\tglobal: \n")
175             for s in invmap.get(k, []):
176                 f.write("\t\t%s;\n" % s);
177         f.write("}%s;\n\n" % last_key)
178         last_key = " %s" % symver
179     f.write("%s {\n" % current_version)
180     f.write("\tglobal:\n")
181     for x in abi_match:
182         f.write("\t\t%s;\n" % x)
183     if abi_match != ["*"]:
184         f.write("\tlocal: *;\n")
185     f.write("};\n")
186     f.close()
187
188
189 def abi_build_vscript(task):
190     '''generate a vscript file for our public libraries'''
191
192     tgt = task.outputs[0].bldpath(task.env)
193
194     symmap = {}
195     versions = []
196     for f in task.inputs:
197         fname = f.abspath(task.env)
198         basename = os.path.basename(fname)
199         version = basename[len(task.env.LIBNAME)+1:-len(".sigs")]
200         versions.append(version)
201         abi_process_file(fname, version, symmap)
202     abi_write_vscript(tgt, task.env.LIBNAME, task.env.VERSION, versions, symmap,
203                       task.env.ABI_MATCH)
204
205
206 def ABI_VSCRIPT(bld, libname, abi_directory, version, vscript, abi_match=None):
207     '''generate a vscript file for our public libraries'''
208     if abi_directory:
209         source = bld.path.ant_glob('%s/%s-[0-9]*.sigs' % (abi_directory, libname))
210         def abi_file_key(path):
211             return version_key(path[:-len(".sigs")].rsplit("-")[-1])
212         source = sorted(source.split(), key=abi_file_key)
213     else:
214         source = ''
215
216     libname = os.path.basename(libname)
217     version = os.path.basename(version)
218     libname = libname.replace("-", "_").replace("+","_").upper()
219     version = version.replace("-", "_").replace("+","_").upper()
220
221     t = bld.SAMBA_GENERATOR(vscript,
222                             rule=abi_build_vscript,
223                             source=source,
224                             group='vscripts',
225                             target=vscript)
226     if abi_match is None:
227         abi_match = ["*"]
228     else:
229         abi_match = samba_utils.TO_LIST(abi_match)
230     t.env.ABI_MATCH = abi_match
231     t.env.VERSION = version
232     t.env.LIBNAME = libname
233     t.vars = ['LIBNAME', 'VERSION', 'ABI_MATCH']
234 Build.BuildContext.ABI_VSCRIPT = ABI_VSCRIPT