Revert "Fix bug #9222 - smbd ignores the "server signing = no" setting for SMB2."
[Samba/gebeck_regimport.git] / buildtools / wafsamba / samba_abi.py
blobed977ba4c2b3e156dfafc5ba6325cf148e401e4e
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\{(.+)\}$', r'\1', sig)
19 sig = re.sub('^\$[0-9]+\s=\s\{(.+)\}(\s0x[0-9a-f]+\s<\w+>)+$', r'\1', sig)
20 sig = re.sub('^\$[0-9]+\s=\s(0x[0-9a-f]+)\s?(<\w+>)?$', r'\1', sig)
21 sig = re.sub('0x[0-9a-f]+', '0xXXXX', sig)
22 sig = re.sub('", <incomplete sequence (\\\\[a-z0-9]+)>', r'\1"', sig)
24 for t in abi_type_maps:
25 # we need to cope with non-word characters in mapped types
26 m = t
27 m = m.replace('*', '\*')
28 if m[-1].isalnum() or m[-1] == '_':
29 m += '\\b'
30 if m[0].isalnum() or m[0] == '_':
31 m = '\\b' + m
32 sig = re.sub(m, abi_type_maps[t], sig)
33 return sig
36 def normalise_varargs(sig):
37 '''cope with older versions of gdb'''
38 sig = re.sub(',\s\.\.\.', '', sig)
39 return sig
42 def parse_sigs(sigs, abi_match):
43 '''parse ABI signatures file'''
44 abi_match = samba_utils.TO_LIST(abi_match)
45 ret = {}
46 a = sigs.split('\n')
47 for s in a:
48 if s.find(':') == -1:
49 continue
50 sa = s.split(':')
51 if abi_match:
52 matched = False
53 for p in abi_match:
54 if p[0] == '!' and fnmatch.fnmatch(sa[0], p[1:]):
55 break
56 elif fnmatch.fnmatch(sa[0], p):
57 matched = True
58 break
59 if not matched:
60 continue
61 Logs.debug("%s -> %s" % (sa[1], normalise_signature(sa[1])))
62 ret[sa[0]] = normalise_signature(sa[1])
63 return ret
65 def save_sigs(sig_file, parsed_sigs):
66 '''save ABI signatures to a file'''
67 sigs = ''
68 for s in sorted(parsed_sigs.keys()):
69 sigs += '%s: %s\n' % (s, parsed_sigs[s])
70 return samba_utils.save_file(sig_file, sigs, create_dir=True)
73 def abi_check_task(self):
74 '''check if the ABI has changed'''
75 abi_gen = self.ABI_GEN
77 libpath = self.inputs[0].abspath(self.env)
78 libname = os.path.basename(libpath)
80 sigs = Utils.cmd_output([abi_gen, libpath])
81 parsed_sigs = parse_sigs(sigs, self.ABI_MATCH)
83 sig_file = self.ABI_FILE
85 old_sigs = samba_utils.load_file(sig_file)
86 if old_sigs is None or Options.options.ABI_UPDATE:
87 if not save_sigs(sig_file, parsed_sigs):
88 raise Utils.WafError('Failed to save ABI file "%s"' % sig_file)
89 Logs.warn('Generated ABI signatures %s' % sig_file)
90 return
92 parsed_old_sigs = parse_sigs(old_sigs, self.ABI_MATCH)
94 # check all old sigs
95 got_error = False
96 for s in parsed_old_sigs:
97 if not s in parsed_sigs:
98 Logs.error('%s: symbol %s has been removed - please update major version\n\tsignature: %s' % (
99 libname, s, parsed_old_sigs[s]))
100 got_error = True
101 elif normalise_varargs(parsed_old_sigs[s]) != normalise_varargs(parsed_sigs[s]):
102 Logs.error('%s: symbol %s has changed - please update major version\n\told_signature: %s\n\tnew_signature: %s' % (
103 libname, s, parsed_old_sigs[s], parsed_sigs[s]))
104 got_error = True
106 for s in parsed_sigs:
107 if not s in parsed_old_sigs:
108 Logs.error('%s: symbol %s has been added - please mark it _PRIVATE_ or update minor version\n\tsignature: %s' % (
109 libname, s, parsed_sigs[s]))
110 got_error = True
112 if got_error:
113 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\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)
116 t = Task.task_type_from_func('abi_check', abi_check_task, color='BLUE', ext_in='.bin')
117 t.quiet = True
118 # allow "waf --abi-check" to force re-checking the ABI
119 if '--abi-check' in sys.argv:
120 Task.always_run(t)
122 @after('apply_link')
123 @feature('abi_check')
124 def abi_check(self):
125 '''check that ABI matches saved signatures'''
126 env = self.bld.env
127 if not env.ABI_CHECK or self.abi_directory is None:
128 return
130 # if the platform doesn't support -fvisibility=hidden then the ABI
131 # checks become fairly meaningless
132 if not env.HAVE_VISIBILITY_ATTR:
133 return
135 topsrc = self.bld.srcnode.abspath()
136 abi_gen = os.path.join(topsrc, 'buildtools/scripts/abi_gen.sh')
138 abi_file = "%s/%s-%s.sigs" % (self.abi_directory, self.name, self.vnum)
140 tsk = self.create_task('abi_check', self.link_task.outputs[0])
141 tsk.ABI_FILE = abi_file
142 tsk.ABI_MATCH = self.abi_match
143 tsk.ABI_GEN = abi_gen
146 def abi_process_file(fname, version, symmap):
147 '''process one ABI file, adding new symbols to the symmap'''
148 f = open(fname, mode='r')
149 for line in f:
150 symname = line.split(":")[0]
151 if not symname in symmap:
152 symmap[symname] = version
153 f.close()
155 def abi_write_vscript(vscript, libname, current_version, versions, symmap, abi_match):
156 '''write a vscript file for a library in --version-script format
158 :param vscript: Path to the vscript file
159 :param libname: Name of the library, uppercased
160 :param current_version: Current version
161 :param versions: Versions to consider
162 :param symmap: Dictionary mapping symbols -> version
163 :param abi_match: List of symbols considered to be public in the current version
166 invmap = {}
167 for s in symmap:
168 invmap.setdefault(symmap[s], []).append(s)
170 f = open(vscript, mode='w')
171 last_key = ""
172 versions = sorted(versions, key=version_key)
173 for k in versions:
174 symver = "%s_%s" % (libname, k)
175 if symver == current_version:
176 break
177 f.write("%s {\n" % symver)
178 if k in invmap:
179 f.write("\tglobal: \n")
180 for s in invmap.get(k, []):
181 f.write("\t\t%s;\n" % s);
182 f.write("}%s;\n\n" % last_key)
183 last_key = " %s" % symver
184 f.write("%s {\n" % current_version)
185 local_abi = filter(lambda x: x[0] == '!', abi_match)
186 global_abi = filter(lambda x: x[0] != '!', abi_match)
187 f.write("\tglobal:\n")
188 if len(global_abi) > 0:
189 for x in global_abi:
190 f.write("\t\t%s;\n" % x)
191 else:
192 f.write("\t\t*;\n")
193 if len(local_abi) > 0:
194 f.write("\tlocal:\n")
195 for x in local_abi:
196 f.write("\t\t%s;\n" % x[1:])
197 elif abi_match != ["*"]:
198 f.write("\tlocal: *;\n")
199 f.write("};\n")
200 f.close()
203 def abi_build_vscript(task):
204 '''generate a vscript file for our public libraries'''
206 tgt = task.outputs[0].bldpath(task.env)
208 symmap = {}
209 versions = []
210 for f in task.inputs:
211 fname = f.abspath(task.env)
212 basename = os.path.basename(fname)
213 version = basename[len(task.env.LIBNAME)+1:-len(".sigs")]
214 versions.append(version)
215 abi_process_file(fname, version, symmap)
216 abi_write_vscript(tgt, task.env.LIBNAME, task.env.VERSION, versions, symmap,
217 task.env.ABI_MATCH)
220 def ABI_VSCRIPT(bld, libname, abi_directory, version, vscript, abi_match=None):
221 '''generate a vscript file for our public libraries'''
222 if abi_directory:
223 source = bld.path.ant_glob('%s/%s-[0-9]*.sigs' % (abi_directory, libname))
224 def abi_file_key(path):
225 return version_key(path[:-len(".sigs")].rsplit("-")[-1])
226 source = sorted(source.split(), key=abi_file_key)
227 else:
228 source = ''
230 libname = os.path.basename(libname)
231 version = os.path.basename(version)
232 libname = libname.replace("-", "_").replace("+","_").upper()
233 version = version.replace("-", "_").replace("+","_").upper()
235 t = bld.SAMBA_GENERATOR(vscript,
236 rule=abi_build_vscript,
237 source=source,
238 group='vscripts',
239 target=vscript)
240 if abi_match is None:
241 abi_match = ["*"]
242 else:
243 abi_match = samba_utils.TO_LIST(abi_match)
244 t.env.ABI_MATCH = abi_match
245 t.env.VERSION = version
246 t.env.LIBNAME = libname
247 t.vars = ['LIBNAME', 'VERSION', 'ABI_MATCH']
248 Build.BuildContext.ABI_VSCRIPT = ABI_VSCRIPT