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
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'''
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
27 m
= m
.replace('*', '\*')
28 if m
[-1].isalnum() or m
[-1] == '_':
30 if m
[0].isalnum() or m
[0] == '_':
32 sig
= re
.sub(m
, abi_type_maps
[t
], sig
)
36 def normalise_varargs(sig
):
37 '''cope with older versions of gdb'''
38 sig
= re
.sub(',\s\.\.\.', '', sig
)
42 def parse_sigs(sigs
, abi_match
):
43 '''parse ABI signatures file'''
44 abi_match
= samba_utils
.TO_LIST(abi_match
)
55 if p
[0] == '!' and fnmatch
.fnmatch(sa
[0], p
[1:]):
58 elif fnmatch
.fnmatch(sa
[0], p
):
61 if (not matched
) and negative
:
63 Logs
.debug("%s -> %s" % (sa
[1], normalise_signature(sa
[1])))
64 ret
[sa
[0]] = normalise_signature(sa
[1])
67 def save_sigs(sig_file
, parsed_sigs
):
68 '''save ABI signatures to a file'''
70 for s
in sorted(parsed_sigs
.keys()):
71 sigs
+= '%s: %s\n' % (s
, parsed_sigs
[s
])
72 return samba_utils
.save_file(sig_file
, sigs
, create_dir
=True)
75 def abi_check_task(self
):
76 '''check if the ABI has changed'''
77 abi_gen
= self
.ABI_GEN
79 libpath
= self
.inputs
[0].abspath(self
.env
)
80 libname
= os
.path
.basename(libpath
)
82 sigs
= Utils
.cmd_output([abi_gen
, libpath
])
83 parsed_sigs
= parse_sigs(sigs
, self
.ABI_MATCH
)
85 sig_file
= self
.ABI_FILE
87 old_sigs
= samba_utils
.load_file(sig_file
)
88 if old_sigs
is None or Options
.options
.ABI_UPDATE
:
89 if not save_sigs(sig_file
, parsed_sigs
):
90 raise Utils
.WafError('Failed to save ABI file "%s"' % sig_file
)
91 Logs
.warn('Generated ABI signatures %s' % sig_file
)
94 parsed_old_sigs
= parse_sigs(old_sigs
, self
.ABI_MATCH
)
98 for s
in parsed_old_sigs
:
99 if not s
in parsed_sigs
:
100 Logs
.error('%s: symbol %s has been removed - please update major version\n\tsignature: %s' % (
101 libname
, s
, parsed_old_sigs
[s
]))
103 elif normalise_varargs(parsed_old_sigs
[s
]) != normalise_varargs(parsed_sigs
[s
]):
104 Logs
.error('%s: symbol %s has changed - please update major version\n\told_signature: %s\n\tnew_signature: %s' % (
105 libname
, s
, parsed_old_sigs
[s
], parsed_sigs
[s
]))
108 for s
in parsed_sigs
:
109 if not s
in parsed_old_sigs
:
110 Logs
.error('%s: symbol %s has been added - please mark it _PRIVATE_ or update minor version\n\tsignature: %s' % (
111 libname
, s
, parsed_sigs
[s
]))
115 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
)
118 t
= Task
.task_type_from_func('abi_check', abi_check_task
, color
='BLUE', ext_in
='.bin')
120 # allow "waf --abi-check" to force re-checking the ABI
121 if '--abi-check' in sys
.argv
:
125 @feature('abi_check')
127 '''check that ABI matches saved signatures'''
129 if not env
.ABI_CHECK
or self
.abi_directory
is None:
132 # if the platform doesn't support -fvisibility=hidden then the ABI
133 # checks become fairly meaningless
134 if not env
.HAVE_VISIBILITY_ATTR
:
137 topsrc
= self
.bld
.srcnode
.abspath()
138 abi_gen
= os
.path
.join(topsrc
, 'buildtools/scripts/abi_gen.sh')
140 abi_file
= "%s/%s-%s.sigs" % (self
.abi_directory
, self
.name
, self
.vnum
)
142 tsk
= self
.create_task('abi_check', self
.link_task
.outputs
[0])
143 tsk
.ABI_FILE
= abi_file
144 tsk
.ABI_MATCH
= self
.abi_match
145 tsk
.ABI_GEN
= abi_gen
148 def abi_process_file(fname
, version
, symmap
):
149 '''process one ABI file, adding new symbols to the symmap'''
150 for line
in Utils
.readf(fname
).splitlines():
151 symname
= line
.split(":")[0]
152 if not symname
in symmap
:
153 symmap
[symname
] = version
156 def abi_write_vscript(f
, libname
, current_version
, versions
, symmap
, abi_match
):
157 """Write a vscript file for a library in --version-script format.
159 :param f: File-like object to write to
160 :param libname: Name of the library, uppercased
161 :param current_version: Current version
162 :param versions: Versions to consider
163 :param symmap: Dictionary mapping symbols -> version
164 :param abi_match: List of symbols considered to be public in the current
170 invmap
.setdefault(symmap
[s
], []).append(s
)
173 versions
= sorted(versions
, key
=version_key
)
175 symver
= "%s_%s" % (libname
, k
)
176 if symver
== current_version
:
178 f
.write("%s {\n" % symver
)
179 if k
in sorted(invmap
.keys()):
180 f
.write("\tglobal:\n")
181 for s
in invmap
.get(k
, []):
182 f
.write("\t\t%s;\n" % s
);
183 f
.write("}%s;\n\n" % last_key
)
184 last_key
= " %s" % symver
185 f
.write("%s {\n" % current_version
)
186 local_abi
= filter(lambda x
: x
[0] == '!', abi_match
)
187 global_abi
= filter(lambda x
: x
[0] != '!', abi_match
)
188 f
.write("\tglobal:\n")
189 if len(global_abi
) > 0:
191 f
.write("\t\t%s;\n" % x
)
194 if abi_match
!= ["*"]:
195 f
.write("\tlocal:\n")
197 f
.write("\t\t%s;\n" % x
[1:])
198 if len(global_abi
) > 0:
203 def abi_build_vscript(task
):
204 '''generate a vscript file for our public libraries'''
206 tgt
= task
.outputs
[0].bldpath(task
.env
)
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 f
= open(tgt
, mode
='w')
218 abi_write_vscript(f
, task
.env
.LIBNAME
, task
.env
.VERSION
, versions
,
219 symmap
, task
.env
.ABI_MATCH
)
224 def ABI_VSCRIPT(bld
, libname
, abi_directory
, version
, vscript
, abi_match
=None):
225 '''generate a vscript file for our public libraries'''
227 source
= bld
.path
.ant_glob('%s/%s-[0-9]*.sigs' % (abi_directory
, libname
))
228 def abi_file_key(path
):
229 return version_key(path
[:-len(".sigs")].rsplit("-")[-1])
230 source
= sorted(source
.split(), key
=abi_file_key
)
234 libname
= os
.path
.basename(libname
)
235 version
= os
.path
.basename(version
)
236 libname
= libname
.replace("-", "_").replace("+","_").upper()
237 version
= version
.replace("-", "_").replace("+","_").upper()
239 t
= bld
.SAMBA_GENERATOR(vscript
,
240 rule
=abi_build_vscript
,
244 if abi_match
is None:
247 abi_match
= samba_utils
.TO_LIST(abi_match
)
248 t
.env
.ABI_MATCH
= abi_match
249 t
.env
.VERSION
= version
250 t
.env
.LIBNAME
= libname
251 t
.vars = ['LIBNAME', 'VERSION', 'ABI_MATCH']
252 Build
.BuildContext
.ABI_VSCRIPT
= ABI_VSCRIPT