summaryrefslogtreecommitdiffstats
path: root/extras/syms.py
diff options
context:
space:
mode:
authorDavid Robillard <d@drobilla.net>2019-03-17 17:31:05 +0100
committerDavid Robillard <d@drobilla.net>2019-03-17 17:31:05 +0100
commit406f89271452fdb573c7e28113b1ed08ff2b4eda (patch)
treed2dcbaf61f3749f73dc7a5e10d3fc6cd5e6e129a /extras/syms.py
parent7983a5aae615290d04fd43cbc2752f8cf4a46d10 (diff)
downloadsuil-406f89271452fdb573c7e28113b1ed08ff2b4eda.tar.gz
suil-406f89271452fdb573c7e28113b1ed08ff2b4eda.tar.bz2
suil-406f89271452fdb573c7e28113b1ed08ff2b4eda.zip
Squashed 'waflib/' changes from 915dcb1..e7a29b6
e7a29b6 Upgrade to waf 2.0.15 8280f9d Add command for running executables from the build directory 8073c1a Make make_simple_dox() safe in case of exception 70d03b8 Avoid use of global counter hacks for configuration display b7d689a Rewrite test framework 94deadf Automatically add options and move add_flags() to options context f4259ee Reduce system include path noise 927b608 Automatically display configuration header c44b8f3 Set line justification from a constant in the wscript a48e26f Automatically detect if wscript has a test hook ef66724 Save runtime variables in the environment 63bcbcd Clean up TestContext b1d9505 Add ExecutionContext for setting runtime environment 387c1df Add show_diff() and test_file_equals() utilities 29d4d29 Fix in-tree library paths 9fde01f Add custom configuration context 6d3612f Add lib_path_name constant git-subtree-dir: waflib git-subtree-split: e7a29b6b9b2f842314244c23c14d8f8f560904e1
Diffstat (limited to 'extras/syms.py')
-rw-r--r--extras/syms.py84
1 files changed, 84 insertions, 0 deletions
diff --git a/extras/syms.py b/extras/syms.py
new file mode 100644
index 0000000..dfa0059
--- /dev/null
+++ b/extras/syms.py
@@ -0,0 +1,84 @@
+#! /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.
+"""
+
+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]
+ kw = {}
+
+ reg = getattr(self.generator, 'export_symbols_regex', '.+?')
+ if 'msvc' in (self.env.CC_NAME, self.env.CXX_NAME):
+ re_nm = re.compile(r'External\s+\|\s+_(?P<symbol>%s)\b' % reg)
+ cmd = (self.env.DUMPBIN or ['dumpbin']) + ['/symbols', obj.abspath()]
+ else:
+ if self.env.DEST_BINFMT == 'pe': #gcc uses nm, and has a preceding _ on windows
+ re_nm = re.compile(r'(T|D)\s+_(?P<symbol>%s)\b' % reg)
+ elif self.env.DEST_BINFMT=='mac-o':
+ re_nm=re.compile(r'(T|D)\s+(?P<symbol>_?%s)\b' % reg)
+ else:
+ re_nm = re.compile(r'(T|D)\s+(?P<symbol>%s)\b' % reg)
+ cmd = (self.env.NM or ['nm']) + ['-g', obj.abspath()]
+ syms = [m.group('symbol') for m in re_nm.finditer(self.generator.bld.cmd_and_log(cmd, quiet=STDOUT, **kw))]
+ 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
+ lsyms = list(syms.keys())
+ 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")
+ elif self.env.DEST_BINFMT=='mac-o':
+ self.outputs[0].write('\n'.join(lsyms) + '\n')
+ else:
+ raise WafError('NotImplemented')
+
+@feature('syms')
+@after_method('process_source', 'process_use', 'apply_link', 'process_uselib_local', 'propagate_uselib_vars')
+def do_the_symbol_stuff(self):
+ def_node = self.path.find_or_declare(getattr(self, 'sym_file', self.target + '.def'))
+ compiled_tasks = getattr(self, 'compiled_tasks', None)
+ if compiled_tasks:
+ ins = [x.outputs[0] for x in compiled_tasks]
+ self.gen_sym_tasks = [self.create_task('gen_sym', x, x.change_ext('.%d.sym' % self.idx)) for x in ins]
+ self.create_task('compile_sym', [x.outputs[0] for x in self.gen_sym_tasks], def_node)
+
+ link_task = getattr(self, 'link_task', None)
+ if link_task:
+ self.link_task.dep_nodes.append(def_node)
+
+ if 'msvc' in (self.env.CC_NAME, self.env.CXX_NAME):
+ self.link_task.env.append_value('LINKFLAGS', ['/def:' + def_node.bldpath()])
+ elif self.env.DEST_BINFMT == 'pe':
+ # gcc on windows takes *.def as an additional input
+ self.link_task.inputs.append(def_node)
+ elif self.env.DEST_BINFMT == 'elf':
+ self.link_task.env.append_value('LINKFLAGS', ['-Wl,-version-script', '-Wl,' + def_node.bldpath()])
+ elif self.env.DEST_BINFMT=='mac-o':
+ self.link_task.env.append_value('LINKFLAGS',['-Wl,-exported_symbols_list,' + def_node.bldpath()])
+ else:
+ raise WafError('NotImplemented')
+