2011-09-10 20:02:12 +02:00
|
|
|
#! /usr/bin/env python
|
|
|
|
# encoding: utf-8
|
|
|
|
|
|
|
|
"""
|
|
|
|
this tool supports the export_symbols_regex to export the symbols in a shared library.
|
|
|
|
by default, all symbols are exported by gcc, and nothing by msvc.
|
|
|
|
to use the tool, do something like:
|
|
|
|
|
|
|
|
def build(ctx):
|
|
|
|
ctx(features='c cshlib syms', source='a.c b.c', export_symbols_regex='mylib_.*', target='testlib')
|
|
|
|
|
|
|
|
only the symbols starting with 'mylib_' will be exported.
|
|
|
|
"""
|
|
|
|
|
2013-06-26 20:35:31 +02:00
|
|
|
import os
|
2011-09-10 20:02:12 +02:00
|
|
|
import re
|
|
|
|
from waflib.Context import STDOUT
|
|
|
|
from waflib.Task import Task
|
|
|
|
from waflib.Errors import WafError
|
|
|
|
from waflib.TaskGen import feature, after_method
|
|
|
|
|
|
|
|
class gen_sym(Task):
|
|
|
|
def run(self):
|
|
|
|
obj = self.inputs[0]
|
2013-06-26 20:35:31 +02:00
|
|
|
kw = {}
|
2017-09-20 00:06:50 +02:00
|
|
|
|
|
|
|
reg = getattr(self.generator, 'export_symbols_regex', '.+?')
|
2011-09-10 20:02:12 +02:00
|
|
|
if 'msvc' in (self.env.CC_NAME, self.env.CXX_NAME):
|
2017-09-20 00:06:50 +02:00
|
|
|
re_nm = re.compile(r'External\s+\|\s+_(?P<symbol>%s)\b' % reg)
|
2014-08-25 20:01:43 +02:00
|
|
|
cmd = (self.env.DUMPBIN or ['dumpbin']) + ['/symbols', obj.abspath()]
|
2011-09-10 20:02:12 +02:00
|
|
|
else:
|
|
|
|
if self.env.DEST_BINFMT == 'pe': #gcc uses nm, and has a preceding _ on windows
|
2017-09-20 00:06:50 +02:00
|
|
|
re_nm = re.compile(r'(T|D)\s+_(?P<symbol>%s)\b' % reg)
|
2014-02-16 01:28:46 +01:00
|
|
|
elif self.env.DEST_BINFMT=='mac-o':
|
2017-09-20 00:06:50 +02:00
|
|
|
re_nm=re.compile(r'(T|D)\s+(?P<symbol>_?%s)\b' % reg)
|
2011-09-10 20:02:12 +02:00
|
|
|
else:
|
2017-09-20 00:06:50 +02:00
|
|
|
re_nm = re.compile(r'(T|D)\s+(?P<symbol>%s)\b' % reg)
|
2017-01-06 23:00:45 +01:00
|
|
|
cmd = (self.env.NM or ['nm']) + ['-g', obj.abspath()]
|
2017-07-27 03:33:24 +02:00
|
|
|
syms = [m.group('symbol') for m in re_nm.finditer(self.generator.bld.cmd_and_log(cmd, quiet=STDOUT, **kw))]
|
2011-09-10 20:02:12 +02:00
|
|
|
self.outputs[0].write('%r' % syms)
|
|
|
|
|
|
|
|
class compile_sym(Task):
|
|
|
|
def run(self):
|
|
|
|
syms = {}
|
|
|
|
for x in self.inputs:
|
|
|
|
slist = eval(x.read())
|
|
|
|
for s in slist:
|
|
|
|
syms[s] = 1
|
2014-01-23 21:19:57 +01:00
|
|
|
lsyms = list(syms.keys())
|
2011-09-10 20:02:12 +02:00
|
|
|
lsyms.sort()
|
|
|
|
if self.env.DEST_BINFMT == 'pe':
|
|
|
|
self.outputs[0].write('EXPORTS\n' + '\n'.join(lsyms))
|
|
|
|
elif self.env.DEST_BINFMT == 'elf':
|
|
|
|
self.outputs[0].write('{ global:\n' + ';\n'.join(lsyms) + ";\nlocal: *; };\n")
|
2014-02-16 01:28:46 +01:00
|
|
|
elif self.env.DEST_BINFMT=='mac-o':
|
|
|
|
self.outputs[0].write('\n'.join(lsyms) + '\n')
|
2011-09-10 20:02:12 +02:00
|
|
|
else:
|
|
|
|
raise WafError('NotImplemented')
|
|
|
|
|
|
|
|
@feature('syms')
|
2017-09-20 00:06:50 +02:00
|
|
|
@after_method('process_source', 'process_use', 'apply_link', 'process_uselib_local', 'propagate_uselib_vars')
|
2011-09-10 20:02:12 +02:00
|
|
|
def do_the_symbol_stuff(self):
|
|
|
|
ins = [x.outputs[0] for x in self.compiled_tasks]
|
|
|
|
self.gen_sym_tasks = [self.create_task('gen_sym', x, x.change_ext('.%d.sym' % self.idx)) for x in ins]
|
|
|
|
|
|
|
|
tsk = self.create_task('compile_sym',
|
|
|
|
[x.outputs[0] for x in self.gen_sym_tasks],
|
|
|
|
self.path.find_or_declare(getattr(self, 'sym_filename', self.target + '.def')))
|
|
|
|
self.link_task.set_run_after(tsk)
|
2013-10-23 23:50:51 +02:00
|
|
|
self.link_task.dep_nodes.append(tsk.outputs[0])
|
2011-09-10 20:02:12 +02:00
|
|
|
if 'msvc' in (self.env.CC_NAME, self.env.CXX_NAME):
|
|
|
|
self.link_task.env.append_value('LINKFLAGS', ['/def:' + tsk.outputs[0].bldpath()])
|
|
|
|
elif self.env.DEST_BINFMT == 'pe': #gcc on windows takes *.def as an additional input
|
|
|
|
self.link_task.inputs.append(tsk.outputs[0])
|
|
|
|
elif self.env.DEST_BINFMT == 'elf':
|
|
|
|
self.link_task.env.append_value('LINKFLAGS', ['-Wl,-version-script', '-Wl,' + tsk.outputs[0].bldpath()])
|
2014-02-16 01:28:46 +01:00
|
|
|
elif self.env.DEST_BINFMT=='mac-o':
|
|
|
|
self.link_task.env.append_value('LINKFLAGS',['-Wl,-exported_symbols_list,'+tsk.outputs[0].bldpath()])
|
2011-09-10 20:02:12 +02:00
|
|
|
else:
|
|
|
|
raise WafError('NotImplemented')
|
|
|
|
|