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