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
10 'struct __va_list_tag *' : 'va_list'
13 version_key = lambda x: map(int, x.split("."))
15 def normalise_signature(sig):
16 '''normalise a signature from gdb'''
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)
22 for t in abi_type_maps:
23 # we need to cope with non-word characters in mapped types
25 m = m.replace('*', '\*')
26 if m[-1].isalnum() or m[-1] == '_':
28 if m[0].isalnum() or m[0] == '_':
30 sig = re.sub(m, abi_type_maps[t], sig)
33 def normalise_varargs(sig):
34 '''cope with older versions of gdb'''
35 sig = re.sub(',\s\.\.\.', '', sig)
38 def parse_sigs(sigs, abi_match):
39 '''parse ABI signatures file'''
40 abi_match = samba_utils.TO_LIST(abi_match)
50 if p[0] == '!' and fnmatch.fnmatch(sa[0], p[1:]):
52 elif fnmatch.fnmatch(sa[0], p):
57 ret[sa[0]] = normalise_signature(sa[1])
60 def save_sigs(sig_file, parsed_sigs):
61 '''save ABI signatures to a file'''
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)
68 def abi_check_task(self):
69 '''check if the ABI has changed'''
70 abi_gen = self.ABI_GEN
72 libpath = self.inputs[0].abspath(self.env)
73 libname = os.path.basename(libpath)
75 sigs = Utils.cmd_output([abi_gen, libpath])
76 parsed_sigs = parse_sigs(sigs, self.ABI_MATCH)
78 sig_file = self.ABI_FILE
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)
87 parsed_old_sigs = parse_sigs(old_sigs, self.ABI_MATCH)
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]))
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]))
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]))
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)
111 t = Task.task_type_from_func('abi_check', abi_check_task, color='BLUE', ext_in='.bin')
113 # allow "waf --abi-check" to force re-checking the ABI
114 if '--abi-check' in sys.argv:
118 @feature('abi_check')
120 '''check that ABI matches saved signatures'''
122 if not env.ABI_CHECK or self.abi_directory is None:
125 # if the platform doesn't support -fvisibility=hidden then the ABI
126 # checks become fairly meaningless
127 if not env.HAVE_VISIBILITY_ATTR:
130 topsrc = self.bld.srcnode.abspath()
131 abi_gen = os.path.join(topsrc, 'buildtools/scripts/abi_gen.sh')
133 abi_file = "%s/%s-%s.sigs" % (self.abi_directory, self.name, self.vnum)
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
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')
145 symname = line.split(":")[0]
146 if not symname in symmap:
147 symmap[symname] = version
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
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
163 invmap.setdefault(symmap[s], []).append(s)
165 f = open(vscript, mode='w')
167 versions = sorted(versions, key=version_key)
169 symver = "%s_%s" % (libname, k)
170 if symver == current_version:
172 f.write("%s {\n" % symver)
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")
182 f.write("\t\t%s;\n" % x)
183 if abi_match != ["*"]:
184 f.write("\tlocal: *;\n")
189 def abi_build_vscript(task):
190 '''generate a vscript file for our public libraries'''
192 tgt = task.outputs[0].bldpath(task.env)
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,
206 def ABI_VSCRIPT(bld, libname, abi_directory, version, vscript, abi_match=None):
207 '''generate a vscript file for our public libraries'''
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)
216 libname = os.path.basename(libname)
217 version = os.path.basename(version)
218 libname = libname.replace("-", "_").replace("+","_").upper()
219 version = version.replace("-", "_").replace("+","_").upper()
221 t = bld.SAMBA_GENERATOR(vscript,
222 rule=abi_build_vscript,
226 if abi_match is None:
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