docs: Document new tdbdump -x option
[cs/samba-autobuild/.git] / buildtools / wafsamba / samba_abi.py
1 # functions for handling ABI checking of libraries
2
3 import os
4 import sys
5 import re
6 import fnmatch
7
8 from waflib import Options, Utils, Logs, Task, Build, Errors
9 from waflib.TaskGen import feature, before, after
10 from wafsamba import samba_utils
11
12 # these type maps cope with platform specific names for common types
13 # please add new type mappings into the list below
14 abi_type_maps = {
15     '_Bool' : 'bool',
16     'struct __va_list_tag *' : 'va_list'
17     }
18
19 version_key = lambda x: list(map(int, x.split(".")))
20
21 def normalise_signature(sig):
22     '''normalise a signature from gdb'''
23     sig = sig.strip()
24     sig = re.sub(r'^\$[0-9]+\s=\s\{(.+)\}$', r'\1', sig)
25     sig = re.sub(r'^\$[0-9]+\s=\s\{(.+)\}(\s0x[0-9a-f]+\s<\w+>)+$', r'\1', sig)
26     sig = re.sub(r'^\$[0-9]+\s=\s(0x[0-9a-f]+)\s?(<\w+>)?$', r'\1', sig)
27     sig = re.sub(r'0x[0-9a-f]+', '0xXXXX', sig)
28     sig = re.sub('", <incomplete sequence (\\\\[a-z0-9]+)>', r'\1"', sig)
29
30     for t in abi_type_maps:
31         # we need to cope with non-word characters in mapped types
32         m = t
33         m = m.replace('*', r'\*')
34         if m[-1].isalnum() or m[-1] == '_':
35             m += '\\b'
36         if m[0].isalnum() or m[0] == '_':
37             m = '\\b' + m
38         sig = re.sub(m, abi_type_maps[t], sig)
39     return sig
40
41
42 def normalise_varargs(sig):
43     '''cope with older versions of gdb'''
44     sig = re.sub(r',\s\.\.\.', '', sig)
45     # Make sure we compare bytes and not strings
46     return bytes(sig, encoding='utf-8').decode('unicode_escape')
47
48
49 def parse_sigs(sigs, abi_match):
50     '''parse ABI signatures file'''
51     abi_match = samba_utils.TO_LIST(abi_match)
52     ret = {}
53     a = sigs.split('\n')
54     for s in a:
55         if s.find(':') == -1:
56             continue
57         sa = s.split(':')
58         if abi_match:
59             matched = False
60             negative = False
61             for p in abi_match:
62                 if p[0] == '!' and fnmatch.fnmatch(sa[0], p[1:]):
63                     negative = True
64                     break
65                 elif fnmatch.fnmatch(sa[0], p):
66                     matched = True
67                     break
68             if (not matched) and negative:
69                 continue
70         Logs.debug("%s -> %s" % (sa[1], normalise_signature(sa[1])))
71         ret[sa[0]] = normalise_signature(sa[1])
72     return ret
73
74 def save_sigs(sig_file, parsed_sigs):
75     '''save ABI signatures to a file'''
76     sigs = "".join('%s: %s\n' % (s, parsed_sigs[s]) for s in sorted(parsed_sigs.keys()))
77     return samba_utils.save_file(sig_file, sigs, create_dir=True)
78
79
80 def abi_check_task(self):
81     '''check if the ABI has changed'''
82     abi_gen = self.ABI_GEN
83
84     libpath = self.inputs[0].abspath(self.env)
85     libname = os.path.basename(libpath)
86
87     sigs = samba_utils.get_string(Utils.cmd_output([abi_gen, libpath]))
88     parsed_sigs = parse_sigs(sigs, self.ABI_MATCH)
89
90     sig_file = self.ABI_FILE
91
92     old_sigs = samba_utils.load_file(sig_file)
93     if old_sigs is None or Options.options.ABI_UPDATE:
94         if not save_sigs(sig_file, parsed_sigs):
95             raise Errors.WafError('Failed to save ABI file "%s"' % sig_file)
96         Logs.warn('Generated ABI signatures %s' % sig_file)
97         return
98
99     parsed_old_sigs = parse_sigs(old_sigs, self.ABI_MATCH)
100
101     # check all old sigs
102     got_error = False
103     for s in parsed_old_sigs:
104         if not s in parsed_sigs:
105             Logs.error('%s: symbol %s has been removed - please update major version\n\tsignature: %s' % (
106                 libname, s, parsed_old_sigs[s]))
107             got_error = True
108         elif normalise_varargs(parsed_old_sigs[s]) != normalise_varargs(parsed_sigs[s]):
109             Logs.error('%s: symbol %s has changed - please update major version\n\told_signature: %s\n\tnew_signature: %s' % (
110                 libname, s, parsed_old_sigs[s], parsed_sigs[s]))
111             got_error = True
112
113     for s in parsed_sigs:
114         if not s in parsed_old_sigs:
115             Logs.error('%s: symbol %s has been added - please mark it _PRIVATE_ or update minor version\n\tsignature: %s' % (
116                 libname, s, parsed_sigs[s]))
117             got_error = True
118
119     if got_error:
120         raise Errors.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\nIf you have not changed any ABI, and your platform always gives this error, please configure with --abi-check-disable to skip this check' % libname)
121
122
123 t = Task.task_factory('abi_check', abi_check_task, color='BLUE', ext_in='.bin')
124 t.quiet = True
125 # allow "waf --abi-check" to force re-checking the ABI
126 if '--abi-check' in sys.argv:
127     t.always_run = True
128
129 @after('apply_link')
130 @feature('abi_check')
131 def abi_check(self):
132     '''check that ABI matches saved signatures'''
133     env = self.bld.env
134     if not env.ABI_CHECK or self.abi_directory is None:
135         return
136
137     # if the platform doesn't support -fvisibility=hidden then the ABI
138     # checks become fairly meaningless
139     if not env.HAVE_VISIBILITY_ATTR:
140         return
141
142     topsrc = self.bld.srcnode.abspath()
143     abi_gen = os.path.join(topsrc, 'buildtools/scripts/abi_gen.sh')
144
145     abi_file = "%s/%s-%s.sigs" % (self.abi_directory, self.version_libname,
146                                   self.abi_vnum)
147
148     tsk = self.create_task('abi_check', self.link_task.outputs[0])
149     tsk.ABI_FILE = abi_file
150     tsk.ABI_MATCH = self.abi_match
151     tsk.ABI_GEN = abi_gen
152
153
154 def abi_process_file(fname, version, symmap):
155     '''process one ABI file, adding new symbols to the symmap'''
156     for line in Utils.readf(fname).splitlines():
157         symname = line.split(":")[0]
158         if not symname in symmap:
159             symmap[symname] = version
160
161 def version_script_map_process_file(fname, version, abi_match):
162     '''process one standard version_script file, adding the symbols to the
163     abi_match'''
164     in_section = False
165     in_global = False
166     in_local = False
167     for _line in Utils.readf(fname).splitlines():
168         line = _line.strip()
169         if line == "":
170             continue
171         if line.startswith("#"):
172             continue
173         if line.endswith(" {"):
174             in_section = True
175             continue
176         if line == "};":
177             assert in_section
178             in_section = False
179             in_global = False
180             in_local = False
181             continue
182         if not in_section:
183             continue
184         if line == "global:":
185             in_global = True
186             in_local = False
187             continue
188         if line == "local:":
189             in_global = False
190             in_local = True
191             continue
192
193         symname = line.split(";")[0]
194         assert symname != ""
195         if in_local:
196             if symname == "*":
197                 continue
198             symname = "!%s" % symname
199         if not symname in abi_match:
200             abi_match.append(symname)
201
202 def abi_write_vscript(f, libname, current_version, versions, symmap, abi_match):
203     """Write a vscript file for a library in --version-script format.
204
205     :param f: File-like object to write to
206     :param libname: Name of the library, uppercased
207     :param current_version: Current version
208     :param versions: Versions to consider
209     :param symmap: Dictionary mapping symbols -> version
210     :param abi_match: List of symbols considered to be public in the current
211         version
212     """
213
214     invmap = {}
215     for s in symmap:
216         invmap.setdefault(symmap[s], []).append(s)
217
218     last_key = ""
219     versions = sorted(versions, key=version_key)
220     for k in versions:
221         symver = "%s_%s" % (libname, k)
222         if symver == current_version:
223             break
224         f.write("%s {\n" % symver)
225         if k in sorted(invmap.keys()):
226             f.write("\tglobal:\n")
227             for s in invmap.get(k, []):
228                 f.write("\t\t%s;\n" % s)
229         f.write("}%s;\n\n" % last_key)
230         last_key = " %s" % symver
231     f.write("%s {\n" % current_version)
232     local_abi = list(filter(lambda x: x[0] == '!', abi_match))
233     global_abi = list(filter(lambda x: x[0] != '!', abi_match))
234     f.write("\tglobal:\n")
235     if len(global_abi) > 0:
236         for x in global_abi:
237             f.write("\t\t%s;\n" % x)
238     else:
239         f.write("\t\t*;\n")
240     # Always hide symbols that must be local if exist
241     local_abi.extend(["!_end", "!__bss_start", "!_edata"])
242     f.write("\tlocal:\n")
243     for x in local_abi:
244         f.write("\t\t%s;\n" % x[1:])
245     if global_abi != ["*"]:
246         if len(global_abi) > 0:
247             f.write("\t\t*;\n")
248     f.write("};\n")
249
250
251 def abi_build_vscript(task):
252     '''generate a vscript file for our public libraries'''
253
254     tgt = task.outputs[0].bldpath(task.env)
255
256     symmap = {}
257     versions = []
258     abi_match = list(task.env.ABI_MATCH)
259     for f in task.inputs:
260         fname = f.abspath(task.env)
261         basename = os.path.basename(fname)
262         if basename.endswith(".sigs"):
263             version = basename[len(task.env.LIBNAME)+1:-len(".sigs")]
264             versions.append(version)
265             abi_process_file(fname, version, symmap)
266             continue
267         if basename == "version-script.map":
268             version_script_map_process_file(fname, task.env.VERSION, abi_match)
269             continue
270         raise Errors.WafError('Unsupported input "%s"' % fname)
271     if task.env.PRIVATE_LIBRARY:
272         # For private libraries we need to inject
273         # each public symbol explicitly into the
274         # abi match array and remove all explicit
275         # versioning so that each exported symbol
276         # is tagged with the private library tag.
277         for s in symmap:
278             abi_match.append(s)
279         symmap = {}
280         versions = []
281     f = open(tgt, mode='w')
282     try:
283         abi_write_vscript(f, task.env.LIBNAME, task.env.VERSION, versions,
284             symmap, abi_match)
285     finally:
286         f.close()
287
288 def VSCRIPT_MAP_PRIVATE(bld, libname, orig_vscript, version, private_vscript):
289     version = version.replace("-", "_").replace("+","_").upper()
290     t = bld.SAMBA_GENERATOR(private_vscript,
291                             rule=abi_build_vscript,
292                             source=orig_vscript,
293                             group='vscripts',
294                             target=private_vscript)
295     t.env.ABI_MATCH = []
296     t.env.VERSION = version
297     t.env.LIBNAME = libname
298     t.env.PRIVATE_LIBRARY = True
299     t.vars = ['LIBNAME', 'VERSION', 'ABI_MATCH', 'PRIVATE_LIBRARY']
300 Build.BuildContext.VSCRIPT_MAP_PRIVATE = VSCRIPT_MAP_PRIVATE
301
302 def ABI_VSCRIPT(bld, libname, abi_directory, version, vscript, abi_match=None, private_library=False):
303     '''generate a vscript file for our public libraries'''
304     if abi_directory:
305         source = bld.path.ant_glob('%s/%s-[0-9]*.sigs' % (abi_directory, libname), flat=True)
306         def abi_file_key(path):
307             return version_key(path[:-len(".sigs")].rsplit("-")[-1])
308         source = sorted(source.split(), key=abi_file_key)
309     else:
310         source = ''
311
312     if private_library is None:
313         private_library = False
314
315     libname = os.path.basename(libname)
316     version = os.path.basename(version)
317     libname = libname.replace("-", "_").replace("+","_").upper()
318     version = version.replace("-", "_").replace("+","_").upper()
319
320     t = bld.SAMBA_GENERATOR(vscript,
321                             rule=abi_build_vscript,
322                             source=source,
323                             group='vscripts',
324                             target=vscript)
325     if abi_match is None:
326         abi_match = ["*"]
327     else:
328         abi_match = samba_utils.TO_LIST(abi_match)
329     t.env.ABI_MATCH = abi_match
330     t.env.VERSION = version
331     t.env.LIBNAME = libname
332     t.env.PRIVATE_LIBRARY = private_library
333     t.vars = ['LIBNAME', 'VERSION', 'ABI_MATCH', 'PRIVATE_LIBRARY']
334 Build.BuildContext.ABI_VSCRIPT = ABI_VSCRIPT