6e4d8d81e0852c729ce37a5201b54a3a44789cfc
[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
4 from TaskGen import feature, before, after
5
6 def normalise_signature(sig):
7     '''normalise a signature from gdb'''
8     sig = sig.strip()
9     sig = re.sub('^\$[0-9]+\s=\s\{*', '', sig)
10     sig = re.sub('\}(\s0x[0-9a-f]+\s<\w+>)?$', '', sig)
11     sig = re.sub('0x[0-9a-f]+', '0xXXXX', sig)
12     return sig
13
14 def normalise_varargs(sig):
15     '''cope with older versions of gdb'''
16     sig = re.sub(',\s\.\.\.', '', sig)
17     return sig
18
19 def parse_sigs(sigs, abi_match):
20     '''parse ABI signatures file'''
21     abi_match = samba_utils.TO_LIST(abi_match)
22     ret = {}
23     a = sigs.split('\n')
24     for s in a:
25         if s.find(':') == -1:
26             continue
27         sa = s.split(':')
28         if abi_match:
29             matched = False
30             for p in abi_match:
31                 if fnmatch.fnmatch(sa[0], p):
32                     matched = True
33                     break
34             if not matched:
35                 continue
36         ret[sa[0]] = normalise_signature(sa[1])
37     return ret
38
39 def save_sigs(sig_file, parsed_sigs):
40     '''save ABI signatures to a file'''
41     sigs = ''
42     for s in sorted(parsed_sigs.keys()):
43         sigs += '%s: %s\n' % (s, parsed_sigs[s])
44     return samba_utils.save_file(sig_file, sigs, create_dir=True)
45
46
47 def abi_check_task(self):
48     '''check if the ABI has changed'''
49     abi_gen = self.ABI_GEN
50
51     libpath = self.inputs[0].abspath(self.env)
52     libname = os.path.basename(libpath)
53
54     sigs = Utils.cmd_output([abi_gen, libpath])
55     parsed_sigs = parse_sigs(sigs, self.ABI_MATCH)
56
57     sig_file = self.ABI_FILE
58
59     old_sigs = samba_utils.load_file(sig_file)
60     if old_sigs is None or Options.options.ABI_UPDATE:
61         if not save_sigs(sig_file, parsed_sigs):
62             raise Utils.WafError('Failed to save ABI file "%s"' % sig_file)
63         Logs.warn('Generated ABI signatures %s' % sig_file)
64         return
65
66     parsed_old_sigs = parse_sigs(old_sigs, self.ABI_MATCH)
67
68     # check all old sigs
69     got_error = False
70     for s in parsed_old_sigs:
71         if not s in parsed_sigs:
72             Logs.error('%s: symbol %s has been removed - please update major version\n\tsignature: %s' % (
73                 libname, s, parsed_old_sigs[s]))
74             got_error = True
75         elif normalise_varargs(parsed_old_sigs[s]) != normalise_varargs(parsed_sigs[s]):
76             Logs.error('%s: symbol %s has changed - please update major version\n\told_signature: %s\n\tnew_signature: %s' % (
77                 libname, s, parsed_old_sigs[s], parsed_sigs[s]))
78             got_error = True
79
80     for s in parsed_sigs:
81         if not s in parsed_old_sigs:
82             Logs.error('%s: symbol %s has been added - please mark it _PRIVATE_ or update minor version\n\tsignature: %s' % (
83                 libname, s, parsed_sigs[s]))
84             got_error = True
85
86     if got_error:
87         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)
88
89
90 t = Task.task_type_from_func('abi_check', abi_check_task, color='BLUE', ext_in='.bin')
91 t.quiet = True
92
93 @after('apply_link')
94 @feature('abi_check')
95 def abi_check(self):
96     '''check that ABI matches saved signatures'''
97     env = self.bld.env
98     if not env.ABI_CHECK or self.abi_file is None:
99         return
100
101     # if the platform doesn't support -fvisibility=hidden then the ABI
102     # checks become fairly meaningless
103     if not env.HAVE_VISIBILITY_ATTR:
104         return
105
106     topsrc = self.bld.srcnode.abspath()
107     abi_gen = os.path.join(topsrc, 'buildtools/scripts/abi_gen.sh')
108
109     tsk = self.create_task('abi_check', self.link_task.outputs[0])
110     tsk.ABI_FILE = self.abi_file
111     tsk.ABI_MATCH = self.abi_match
112     tsk.ABI_GEN = abi_gen