Improve the VFS Makefile so that it is easier for use out of tree but still works...
[Samba/gebeck_regimport.git] / buildtools / wafsamba / samba_abi.py
blob76c2d8b0d41e23e4dc8deabc373417176ce967cd
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 version_key = lambda x: map(int, x.split("."))
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 sig = re.sub('", <incomplete sequence (\\\\[a-z0-9]+)>', r'\1"', sig)
23 for t in abi_type_maps:
24 # we need to cope with non-word characters in mapped types
25 m = t
26 m = m.replace('*', '\*')
27 if m[-1].isalnum() or m[-1] == '_':
28 m += '\\b'
29 if m[0].isalnum() or m[0] == '_':
30 m = '\\b' + m
31 sig = re.sub(m, abi_type_maps[t], sig)
32 return sig
35 def normalise_varargs(sig):
36 '''cope with older versions of gdb'''
37 sig = re.sub(',\s\.\.\.', '', sig)
38 return sig
41 def parse_sigs(sigs, abi_match):
42 '''parse ABI signatures file'''
43 abi_match = samba_utils.TO_LIST(abi_match)
44 ret = {}
45 a = sigs.split('\n')
46 for s in a:
47 if s.find(':') == -1:
48 continue
49 sa = s.split(':')
50 if abi_match:
51 matched = False
52 for p in abi_match:
53 if p[0] == '!' and fnmatch.fnmatch(sa[0], p[1:]):
54 break
55 elif fnmatch.fnmatch(sa[0], p):
56 matched = True
57 break
58 if not matched:
59 continue
60 print "%s -> %s" % (sa[1], normalise_signature(sa[1]))
61 ret[sa[0]] = normalise_signature(sa[1])
62 return ret
64 def save_sigs(sig_file, parsed_sigs):
65 '''save ABI signatures to a file'''
66 sigs = ''
67 for s in sorted(parsed_sigs.keys()):
68 sigs += '%s: %s\n' % (s, parsed_sigs[s])
69 return samba_utils.save_file(sig_file, sigs, create_dir=True)
72 def abi_check_task(self):
73 '''check if the ABI has changed'''
74 abi_gen = self.ABI_GEN
76 libpath = self.inputs[0].abspath(self.env)
77 libname = os.path.basename(libpath)
79 sigs = Utils.cmd_output([abi_gen, libpath])
80 parsed_sigs = parse_sigs(sigs, self.ABI_MATCH)
82 sig_file = self.ABI_FILE
84 old_sigs = samba_utils.load_file(sig_file)
85 if old_sigs is None or Options.options.ABI_UPDATE:
86 if not save_sigs(sig_file, parsed_sigs):
87 raise Utils.WafError('Failed to save ABI file "%s"' % sig_file)
88 Logs.warn('Generated ABI signatures %s' % sig_file)
89 return
91 parsed_old_sigs = parse_sigs(old_sigs, self.ABI_MATCH)
93 # check all old sigs
94 got_error = False
95 for s in parsed_old_sigs:
96 if not s in parsed_sigs:
97 Logs.error('%s: symbol %s has been removed - please update major version\n\tsignature: %s' % (
98 libname, s, parsed_old_sigs[s]))
99 got_error = True
100 elif normalise_varargs(parsed_old_sigs[s]) != normalise_varargs(parsed_sigs[s]):
101 Logs.error('%s: symbol %s has changed - please update major version\n\told_signature: %s\n\tnew_signature: %s' % (
102 libname, s, parsed_old_sigs[s], parsed_sigs[s]))
103 got_error = True
105 for s in parsed_sigs:
106 if not s in parsed_old_sigs:
107 Logs.error('%s: symbol %s has been added - please mark it _PRIVATE_ or update minor version\n\tsignature: %s' % (
108 libname, s, parsed_sigs[s]))
109 got_error = True
111 if got_error:
112 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)
115 t = Task.task_type_from_func('abi_check', abi_check_task, color='BLUE', ext_in='.bin')
116 t.quiet = True
117 # allow "waf --abi-check" to force re-checking the ABI
118 if '--abi-check' in sys.argv:
119 Task.always_run(t)
121 @after('apply_link')
122 @feature('abi_check')
123 def abi_check(self):
124 '''check that ABI matches saved signatures'''
125 env = self.bld.env
126 if not env.ABI_CHECK or self.abi_directory is None:
127 return
129 # if the platform doesn't support -fvisibility=hidden then the ABI
130 # checks become fairly meaningless
131 if not env.HAVE_VISIBILITY_ATTR:
132 return
134 topsrc = self.bld.srcnode.abspath()
135 abi_gen = os.path.join(topsrc, 'buildtools/scripts/abi_gen.sh')
137 abi_file = "%s/%s-%s.sigs" % (self.abi_directory, self.name, self.vnum)
139 tsk = self.create_task('abi_check', self.link_task.outputs[0])
140 tsk.ABI_FILE = abi_file
141 tsk.ABI_MATCH = self.abi_match
142 tsk.ABI_GEN = abi_gen
145 def abi_process_file(fname, version, symmap):
146 '''process one ABI file, adding new symbols to the symmap'''
147 f = open(fname, mode='r')
148 for line in f:
149 symname = line.split(":")[0]
150 if not symname in symmap:
151 symmap[symname] = version
152 f.close()
154 def abi_write_vscript(vscript, libname, current_version, versions, symmap, abi_match):
155 '''write a vscript file for a library in --version-script format
157 :param vscript: Path to the vscript file
158 :param libname: Name of the library, uppercased
159 :param current_version: Current version
160 :param versions: Versions to consider
161 :param symmap: Dictionary mapping symbols -> version
162 :param abi_match: List of symbols considered to be public in the current version
165 invmap = {}
166 for s in symmap:
167 invmap.setdefault(symmap[s], []).append(s)
169 f = open(vscript, mode='w')
170 last_key = ""
171 versions = sorted(versions, key=version_key)
172 for k in versions:
173 symver = "%s_%s" % (libname, k)
174 if symver == current_version:
175 break
176 f.write("%s {\n" % symver)
177 if k in invmap:
178 f.write("\tglobal: \n")
179 for s in invmap.get(k, []):
180 f.write("\t\t%s;\n" % s);
181 f.write("}%s;\n\n" % last_key)
182 last_key = " %s" % symver
183 f.write("%s {\n" % current_version)
184 f.write("\tglobal:\n")
185 for x in abi_match:
186 f.write("\t\t%s;\n" % x)
187 if abi_match != ["*"]:
188 f.write("\tlocal: *;\n")
189 f.write("};\n")
190 f.close()
193 def abi_build_vscript(task):
194 '''generate a vscript file for our public libraries'''
196 tgt = task.outputs[0].bldpath(task.env)
198 symmap = {}
199 versions = []
200 for f in task.inputs:
201 fname = f.abspath(task.env)
202 basename = os.path.basename(fname)
203 version = basename[len(task.env.LIBNAME)+1:-len(".sigs")]
204 versions.append(version)
205 abi_process_file(fname, version, symmap)
206 abi_write_vscript(tgt, task.env.LIBNAME, task.env.VERSION, versions, symmap,
207 task.env.ABI_MATCH)
210 def ABI_VSCRIPT(bld, libname, abi_directory, version, vscript, abi_match=None):
211 '''generate a vscript file for our public libraries'''
212 if abi_directory:
213 source = bld.path.ant_glob('%s/%s-[0-9]*.sigs' % (abi_directory, libname))
214 def abi_file_key(path):
215 return version_key(path[:-len(".sigs")].rsplit("-")[-1])
216 source = sorted(source.split(), key=abi_file_key)
217 else:
218 source = ''
220 libname = os.path.basename(libname)
221 version = os.path.basename(version)
222 libname = libname.replace("-", "_").replace("+","_").upper()
223 version = version.replace("-", "_").replace("+","_").upper()
225 t = bld.SAMBA_GENERATOR(vscript,
226 rule=abi_build_vscript,
227 source=source,
228 group='vscripts',
229 target=vscript)
230 if abi_match is None:
231 abi_match = ["*"]
232 else:
233 abi_match = samba_utils.TO_LIST(abi_match)
234 t.env.ABI_MATCH = abi_match
235 t.env.VERSION = version
236 t.env.LIBNAME = libname
237 t.vars = ['LIBNAME', 'VERSION', 'ABI_MATCH']
238 Build.BuildContext.ABI_VSCRIPT = ABI_VSCRIPT