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('^\$[0-9]+\s=\s\{(.+)\}$', r
'\1', sig
)
25 sig
= re
.sub('^\$[0-9]+\s=\s\{(.+)\}(\s0x[0-9a-f]+\s<\w+>)+$', r
'\1', sig
)
26 sig
= re
.sub('^\$[0-9]+\s=\s(0x[0-9a-f]+)\s?(<\w+>)?$', r
'\1', sig
)
27 sig
= re
.sub('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('*', '\*')
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(',\s\.\.\.', '', sig
)
48 def parse_sigs(sigs
, abi_match
):
49 '''parse ABI signatures file'''
50 abi_match
= samba_utils
.TO_LIST(abi_match
)
61 if p
[0] == '!' and fnmatch
.fnmatch(sa
[0], p
[1:]):
64 elif fnmatch
.fnmatch(sa
[0], p
):
67 if (not matched
) and negative
:
69 Logs
.debug("%s -> %s" % (sa
[1], normalise_signature(sa
[1])))
70 ret
[sa
[0]] = normalise_signature(sa
[1])
73 def save_sigs(sig_file
, parsed_sigs
):
74 '''save ABI signatures to a file'''
76 for s
in sorted(parsed_sigs
.keys()):
77 sigs
+= '%s: %s\n' % (s
, parsed_sigs
[s
])
78 return samba_utils
.save_file(sig_file
, sigs
, create_dir
=True)
81 def abi_check_task(self
):
82 '''check if the ABI has changed'''
83 abi_gen
= self
.ABI_GEN
85 libpath
= self
.inputs
[0].abspath(self
.env
)
86 libname
= os
.path
.basename(libpath
)
88 sigs
= Utils
.cmd_output([abi_gen
, libpath
]).decode('utf8')
89 parsed_sigs
= parse_sigs(sigs
, self
.ABI_MATCH
)
91 sig_file
= self
.ABI_FILE
93 old_sigs
= samba_utils
.load_file(sig_file
)
94 if old_sigs
is None or Options
.options
.ABI_UPDATE
:
95 if not save_sigs(sig_file
, parsed_sigs
):
96 raise Errors
.WafError('Failed to save ABI file "%s"' % sig_file
)
97 Logs
.warn('Generated ABI signatures %s' % sig_file
)
100 parsed_old_sigs
= parse_sigs(old_sigs
, self
.ABI_MATCH
)
104 for s
in parsed_old_sigs
:
105 if not s
in parsed_sigs
:
106 Logs
.error('%s: symbol %s has been removed - please update major version\n\tsignature: %s' % (
107 libname
, s
, parsed_old_sigs
[s
]))
109 elif normalise_varargs(parsed_old_sigs
[s
]) != normalise_varargs(parsed_sigs
[s
]):
110 Logs
.error('%s: symbol %s has changed - please update major version\n\told_signature: %s\n\tnew_signature: %s' % (
111 libname
, s
, parsed_old_sigs
[s
], parsed_sigs
[s
]))
114 for s
in parsed_sigs
:
115 if not s
in parsed_old_sigs
:
116 Logs
.error('%s: symbol %s has been added - please mark it _PRIVATE_ or update minor version\n\tsignature: %s' % (
117 libname
, s
, parsed_sigs
[s
]))
121 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
)
124 t
= Task
.task_factory('abi_check', abi_check_task
, color
='BLUE', ext_in
='.bin')
126 # allow "waf --abi-check" to force re-checking the ABI
127 if '--abi-check' in sys
.argv
:
131 @feature('abi_check')
133 '''check that ABI matches saved signatures'''
135 if not env
.ABI_CHECK
or self
.abi_directory
is None:
138 # if the platform doesn't support -fvisibility=hidden then the ABI
139 # checks become fairly meaningless
140 if not env
.HAVE_VISIBILITY_ATTR
:
143 topsrc
= self
.bld
.srcnode
.abspath()
144 abi_gen
= os
.path
.join(topsrc
, 'buildtools/scripts/abi_gen.sh')
146 abi_file
= "%s/%s-%s.sigs" % (self
.abi_directory
, self
.version_libname
,
149 tsk
= self
.create_task('abi_check', self
.link_task
.outputs
[0])
150 tsk
.ABI_FILE
= abi_file
151 tsk
.ABI_MATCH
= self
.abi_match
152 tsk
.ABI_GEN
= abi_gen
155 def abi_process_file(fname
, version
, symmap
):
156 '''process one ABI file, adding new symbols to the symmap'''
157 for line
in Utils
.readf(fname
).splitlines():
158 symname
= line
.split(":")[0]
159 if not symname
in symmap
:
160 symmap
[symname
] = version
163 def abi_write_vscript(f
, libname
, current_version
, versions
, symmap
, abi_match
):
164 """Write a vscript file for a library in --version-script format.
166 :param f: File-like object to write to
167 :param libname: Name of the library, uppercased
168 :param current_version: Current version
169 :param versions: Versions to consider
170 :param symmap: Dictionary mapping symbols -> version
171 :param abi_match: List of symbols considered to be public in the current
177 invmap
.setdefault(symmap
[s
], []).append(s
)
180 versions
= sorted(versions
, key
=version_key
)
182 symver
= "%s_%s" % (libname
, k
)
183 if symver
== current_version
:
185 f
.write("%s {\n" % symver
)
186 if k
in sorted(invmap
.keys()):
187 f
.write("\tglobal:\n")
188 for s
in invmap
.get(k
, []):
189 f
.write("\t\t%s;\n" % s
);
190 f
.write("}%s;\n\n" % last_key
)
191 last_key
= " %s" % symver
192 f
.write("%s {\n" % current_version
)
193 local_abi
= list(filter(lambda x
: x
[0] == '!', abi_match
))
194 global_abi
= list(filter(lambda x
: x
[0] != '!', abi_match
))
195 f
.write("\tglobal:\n")
196 if len(global_abi
) > 0:
198 f
.write("\t\t%s;\n" % x
)
201 # Always hide symbols that must be local if exist
202 local_abi
.extend(["!_end", "!__bss_start", "!_edata"])
203 f
.write("\tlocal:\n")
205 f
.write("\t\t%s;\n" % x
[1:])
206 if global_abi
!= ["*"]:
207 if len(global_abi
) > 0:
212 def abi_build_vscript(task
):
213 '''generate a vscript file for our public libraries'''
215 tgt
= task
.outputs
[0].bldpath(task
.env
)
219 for f
in task
.inputs
:
220 fname
= f
.abspath(task
.env
)
221 basename
= os
.path
.basename(fname
)
222 version
= basename
[len(task
.env
.LIBNAME
)+1:-len(".sigs")]
223 versions
.append(version
)
224 abi_process_file(fname
, version
, symmap
)
225 f
= open(tgt
, mode
='w')
227 abi_write_vscript(f
, task
.env
.LIBNAME
, task
.env
.VERSION
, versions
,
228 symmap
, task
.env
.ABI_MATCH
)
233 def ABI_VSCRIPT(bld
, libname
, abi_directory
, version
, vscript
, abi_match
=None):
234 '''generate a vscript file for our public libraries'''
236 source
= bld
.path
.ant_glob('%s/%s-[0-9]*.sigs' % (abi_directory
, libname
), flat
=True)
237 def abi_file_key(path
):
238 return version_key(path
[:-len(".sigs")].rsplit("-")[-1])
239 source
= sorted(source
.split(), key
=abi_file_key
)
243 libname
= os
.path
.basename(libname
)
244 version
= os
.path
.basename(version
)
245 libname
= libname
.replace("-", "_").replace("+","_").upper()
246 version
= version
.replace("-", "_").replace("+","_").upper()
248 t
= bld
.SAMBA_GENERATOR(vscript
,
249 rule
=abi_build_vscript
,
253 if abi_match
is None:
256 abi_match
= samba_utils
.TO_LIST(abi_match
)
257 t
.env
.ABI_MATCH
= abi_match
258 t
.env
.VERSION
= version
259 t
.env
.LIBNAME
= libname
260 t
.vars = ['LIBNAME', 'VERSION', 'ABI_MATCH']
261 Build
.BuildContext
.ABI_VSCRIPT
= ABI_VSCRIPT