1 # functions for handling ABI checking of libraries
8 from waflib
import Options
, Utils
, Logs
, Task
, Build
, Errors
9 from waflib
.TaskGen
import feature
, before
, after
10 from wafsamba
import samba_utils
12 # these type maps cope with platform specific names for common types
13 # please add new type mappings into the list below
16 'struct __va_list_tag *' : 'va_list'
19 version_key
= lambda x
: list(map(int, x
.split(".")))
21 def normalise_signature(sig
):
22 '''normalise a signature from gdb'''
24 sig
= re
.sub(r
'^\$[0-9]+\s=\s\{(.+)\}$', r
'\1', sig
)
25 sig
= re
.sub(r
'^\$[0-9]+\s=\s\{(.+)\}(\s0x[0-9a-f]+\s<\w+>)+$', r
'\1', sig
)
26 sig
= re
.sub(r
'^\$[0-9]+\s=\s(0x[0-9a-f]+)\s?(<\w+>)?$', r
'\1', sig
)
27 sig
= re
.sub(r
'0x[0-9a-f]+', '0xXXXX', sig
)
28 sig
= re
.sub('", <incomplete sequence (\\\\[a-z0-9]+)>', r
'\1"', sig
)
30 for t
in abi_type_maps
:
31 # we need to cope with non-word characters in mapped types
33 m
= m
.replace('*', r
'\*')
34 if m
[-1].isalnum() or m
[-1] == '_':
36 if m
[0].isalnum() or m
[0] == '_':
38 sig
= re
.sub(m
, abi_type_maps
[t
], sig
)
42 def normalise_varargs(sig
):
43 '''cope with older versions of gdb'''
44 sig
= re
.sub(r
',\s\.\.\.', '', sig
)
45 # Make sure we compare bytes and not strings
46 return bytes(sig
, encoding
='utf-8').decode('unicode_escape')
49 def parse_sigs(sigs
, abi_match
):
50 '''parse ABI signatures file'''
51 abi_match
= samba_utils
.TO_LIST(abi_match
)
62 if p
[0] == '!' and fnmatch
.fnmatch(sa
[0], p
[1:]):
65 elif fnmatch
.fnmatch(sa
[0], p
):
68 if (not matched
) and negative
:
70 Logs
.debug("%s -> %s" % (sa
[1], normalise_signature(sa
[1])))
71 ret
[sa
[0]] = normalise_signature(sa
[1])
74 def save_sigs(sig_file
, parsed_sigs
):
75 '''save ABI signatures to a file'''
76 sigs
= "".join('%s: %s\n' % (s
, parsed_sigs
[s
]) for s
in sorted(parsed_sigs
.keys()))
77 return samba_utils
.save_file(sig_file
, sigs
, create_dir
=True)
80 def abi_check_task(self
):
81 '''check if the ABI has changed'''
82 abi_gen
= self
.ABI_GEN
84 libpath
= self
.inputs
[0].abspath(self
.env
)
85 libname
= os
.path
.basename(libpath
)
87 sigs
= samba_utils
.get_string(Utils
.cmd_output([abi_gen
, libpath
]))
88 parsed_sigs
= parse_sigs(sigs
, self
.ABI_MATCH
)
90 sig_file
= self
.ABI_FILE
92 old_sigs
= samba_utils
.load_file(sig_file
)
93 if old_sigs
is None or Options
.options
.ABI_UPDATE
:
94 if not save_sigs(sig_file
, parsed_sigs
):
95 raise Errors
.WafError('Failed to save ABI file "%s"' % sig_file
)
96 Logs
.warn('Generated ABI signatures %s' % sig_file
)
99 parsed_old_sigs
= parse_sigs(old_sigs
, self
.ABI_MATCH
)
103 for s
in parsed_old_sigs
:
104 if not s
in parsed_sigs
:
105 Logs
.error('%s: symbol %s has been removed - please update major version\n\tsignature: %s' % (
106 libname
, s
, parsed_old_sigs
[s
]))
108 elif normalise_varargs(parsed_old_sigs
[s
]) != normalise_varargs(parsed_sigs
[s
]):
109 Logs
.error('%s: symbol %s has changed - please update major version\n\told_signature: %s\n\tnew_signature: %s' % (
110 libname
, s
, parsed_old_sigs
[s
], parsed_sigs
[s
]))
113 for s
in parsed_sigs
:
114 if not s
in parsed_old_sigs
:
115 Logs
.error('%s: symbol %s has been added - please mark it _PRIVATE_ or update minor version\n\tsignature: %s' % (
116 libname
, s
, parsed_sigs
[s
]))
120 raise Errors
.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
)
123 t
= Task
.task_factory('abi_check', abi_check_task
, color
='BLUE', ext_in
='.bin')
125 # allow "waf --abi-check" to force re-checking the ABI
126 if '--abi-check' in sys
.argv
:
130 @feature('abi_check')
132 '''check that ABI matches saved signatures'''
134 if not env
.ABI_CHECK
or self
.abi_directory
is None:
137 # if the platform doesn't support -fvisibility=hidden then the ABI
138 # checks become fairly meaningless
139 if not env
.HAVE_VISIBILITY_ATTR
:
142 topsrc
= self
.bld
.srcnode
.abspath()
143 abi_gen
= os
.path
.join(topsrc
, 'buildtools/scripts/abi_gen.sh')
145 abi_file
= "%s/%s-%s.sigs" % (self
.abi_directory
, self
.version_libname
,
148 tsk
= self
.create_task('abi_check', self
.link_task
.outputs
[0])
149 tsk
.ABI_FILE
= abi_file
150 tsk
.ABI_MATCH
= self
.abi_match
151 tsk
.ABI_GEN
= abi_gen
154 def abi_process_file(fname
, version
, symmap
):
155 '''process one ABI file, adding new symbols to the symmap'''
156 for line
in Utils
.readf(fname
).splitlines():
157 symname
= line
.split(":")[0]
158 if not symname
in symmap
:
159 symmap
[symname
] = version
161 def version_script_map_process_file(fname
, version
, abi_match
):
162 '''process one standard version_script file, adding the symbols to the
167 for _line
in Utils
.readf(fname
).splitlines():
171 if line
.startswith("#"):
173 if line
.endswith(" {"):
184 if line
== "global:":
193 symname
= line
.split(";")[0]
198 symname
= "!%s" % symname
199 if not symname
in abi_match
:
200 abi_match
.append(symname
)
202 def abi_write_vscript(f
, libname
, current_version
, versions
, symmap
, abi_match
):
203 """Write a vscript file for a library in --version-script format.
205 :param f: File-like object to write to
206 :param libname: Name of the library, uppercased
207 :param current_version: Current version
208 :param versions: Versions to consider
209 :param symmap: Dictionary mapping symbols -> version
210 :param abi_match: List of symbols considered to be public in the current
216 invmap
.setdefault(symmap
[s
], []).append(s
)
219 versions
= sorted(versions
, key
=version_key
)
221 symver
= "%s_%s" % (libname
, k
)
222 if symver
== current_version
:
224 f
.write("%s {\n" % symver
)
225 if k
in sorted(invmap
.keys()):
226 f
.write("\tglobal:\n")
227 for s
in invmap
.get(k
, []):
228 f
.write("\t\t%s;\n" % s
)
229 f
.write("}%s;\n\n" % last_key
)
230 last_key
= " %s" % symver
231 f
.write("%s {\n" % current_version
)
232 local_abi
= list(filter(lambda x
: x
[0] == '!', abi_match
))
233 global_abi
= list(filter(lambda x
: x
[0] != '!', abi_match
))
234 f
.write("\tglobal:\n")
235 if len(global_abi
) > 0:
237 f
.write("\t\t%s;\n" % x
)
240 # Always hide symbols that must be local if exist
241 local_abi
.extend(["!_end", "!__bss_start", "!_edata"])
242 f
.write("\tlocal:\n")
244 f
.write("\t\t%s;\n" % x
[1:])
245 if global_abi
!= ["*"]:
246 if len(global_abi
) > 0:
251 def abi_build_vscript(task
):
252 '''generate a vscript file for our public libraries'''
254 tgt
= task
.outputs
[0].bldpath(task
.env
)
258 abi_match
= list(task
.env
.ABI_MATCH
)
259 for f
in task
.inputs
:
260 fname
= f
.abspath(task
.env
)
261 basename
= os
.path
.basename(fname
)
262 if basename
.endswith(".sigs"):
263 version
= basename
[len(task
.env
.LIBNAME
)+1:-len(".sigs")]
264 versions
.append(version
)
265 abi_process_file(fname
, version
, symmap
)
267 if basename
== "version-script.map":
268 version_script_map_process_file(fname
, task
.env
.VERSION
, abi_match
)
270 raise Errors
.WafError('Unsupported input "%s"' % fname
)
271 if task
.env
.PRIVATE_LIBRARY
:
272 # For private libraries we need to inject
273 # each public symbol explicitly into the
274 # abi match array and remove all explicit
275 # versioning so that each exported symbol
276 # is tagged with the private library tag.
281 f
= open(tgt
, mode
='w')
283 abi_write_vscript(f
, task
.env
.LIBNAME
, task
.env
.VERSION
, versions
,
288 def VSCRIPT_MAP_PRIVATE(bld
, libname
, orig_vscript
, version
, private_vscript
):
289 version
= re
.sub(r
'[^.\w]', '_', version
).upper()
290 t
= bld
.SAMBA_GENERATOR(private_vscript
,
291 rule
=abi_build_vscript
,
294 target
=private_vscript
)
296 t
.env
.VERSION
= version
297 t
.env
.LIBNAME
= libname
298 t
.env
.PRIVATE_LIBRARY
= True
299 t
.vars = ['LIBNAME', 'VERSION', 'ABI_MATCH', 'PRIVATE_LIBRARY']
300 Build
.BuildContext
.VSCRIPT_MAP_PRIVATE
= VSCRIPT_MAP_PRIVATE
302 def ABI_VSCRIPT(bld
, libname
, abi_directory
, version
, vscript
, abi_match
=None, private_library
=False):
303 '''generate a vscript file for our public libraries'''
305 source
= bld
.path
.ant_glob('%s/%s-[0-9]*.sigs' % (abi_directory
, libname
), flat
=True)
306 def abi_file_key(path
):
307 return version_key(path
[:-len(".sigs")].rsplit("-")[-1])
308 source
= sorted(source
.split(), key
=abi_file_key
)
312 if private_library
is None:
313 private_library
= False
315 libname
= os
.path
.basename(libname
)
316 version
= os
.path
.basename(version
)
317 libname
= re
.sub(r
'[^.\w]', '_', libname
).upper()
318 version
= re
.sub(r
'[^.\w]', '_', version
).upper()
320 t
= bld
.SAMBA_GENERATOR(vscript
,
321 rule
=abi_build_vscript
,
325 if abi_match
is None:
328 abi_match
= samba_utils
.TO_LIST(abi_match
)
329 t
.env
.ABI_MATCH
= abi_match
330 t
.env
.VERSION
= version
331 t
.env
.LIBNAME
= libname
332 t
.env
.PRIVATE_LIBRARY
= private_library
333 t
.vars = ['LIBNAME', 'VERSION', 'ABI_MATCH', 'PRIVATE_LIBRARY']
334 Build
.BuildContext
.ABI_VSCRIPT
= ABI_VSCRIPT