1 # functions for handling ABI checking of libraries
3 import Options
, Utils
, os
, Logs
, samba_utils
, sys
, Task
, fnmatch
, re
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 def normalise_signature(sig
):
14 '''normalise a signature from gdb'''
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
23 m
= m
.replace('*', '\*')
24 if m
[-1].isalnum() or m
[-1] == '_':
26 if m
[0].isalnum() or m
[0] == '_':
28 sig
= re
.sub(m
, abi_type_maps
[t
], sig
)
31 def normalise_varargs(sig
):
32 '''cope with older versions of gdb'''
33 sig
= re
.sub(',\s\.\.\.', '', sig
)
36 def parse_sigs(sigs
, abi_match
):
37 '''parse ABI signatures file'''
38 abi_match
= samba_utils
.TO_LIST(abi_match
)
48 if p
[0] == '!' and fnmatch
.fnmatch(sa
[0], p
[1:]):
50 elif fnmatch
.fnmatch(sa
[0], p
):
55 ret
[sa
[0]] = normalise_signature(sa
[1])
58 def save_sigs(sig_file
, parsed_sigs
):
59 '''save ABI signatures to a file'''
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
)
85 parsed_old_sigs
= parse_sigs(old_sigs
, self
.ABI_MATCH
)
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
]))
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
]))
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
]))
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')
111 # allow "waf --abi-check" to force re-checking the ABI
112 if '--abi-check' in sys
.argv
:
116 @feature('abi_check')
118 '''check that ABI matches saved signatures'''
120 if not env
.ABI_CHECK
or self
.abi_file
is None:
123 # if the platform doesn't support -fvisibility=hidden then the ABI
124 # checks become fairly meaningless
125 if not env
.HAVE_VISIBILITY_ATTR
:
128 topsrc
= self
.bld
.srcnode
.abspath()
129 abi_gen
= os
.path
.join(topsrc
, 'buildtools/scripts/abi_gen.sh')
131 tsk
= self
.create_task('abi_check', self
.link_task
.outputs
[0])
132 tsk
.ABI_FILE
= self
.abi_file
133 tsk
.ABI_MATCH
= self
.abi_match
134 tsk
.ABI_GEN
= abi_gen