From 09f65b7a3cb2bd3bb5b5fa42ffac45f8fb632132 Mon Sep 17 00:00:00 2001 From: Nirbheek Chauhan Date: Sun, 4 Dec 2016 17:02:59 +0530 Subject: [PATCH] New compiler function 'symbols_have_underscore_prefix' Check if the compiler prefixes an underscore to global symbols. This is useful when linking to compiled assembly code, or other code that does not have its C symbol mangling handled transparently by the compiler. C symbol mangling is platform and architecture dependent, and a helper function is needed to detect it. Eg: Windows 32-bit prefixes underscore, but 64-bit does not. Linux does not prefix an underscore but OS X does. --- mesonbuild/compilers.py | 34 ++++++++++++++++++++++++++++++++++ mesonbuild/interpreter.py | 8 ++++++++ 2 files changed, 42 insertions(+) diff --git a/mesonbuild/compilers.py b/mesonbuild/compilers.py index e474d2ed2..9ddaa560d 100644 --- a/mesonbuild/compilers.py +++ b/mesonbuild/compilers.py @@ -1058,6 +1058,40 @@ void bar() { ''' return self.compiles(templ % (prefix, typename), env, extra_args, dependencies) + def symbols_have_underscore_prefix(self, env): + ''' + Check if the compiler prefixes an underscore to global C symbols + ''' + symbol_name = b'meson_uscore_prefix' + code = '''#ifdef __cplusplus + extern "C" { + #endif + void ''' + symbol_name.decode() + ''' () {} + #ifdef __cplusplus + } + #endif + ''' + args = self.get_cross_extra_flags(env, compile=True, link=False) + args += self.get_compiler_check_args() + n = 'symbols_have_underscore_prefix' + with self.compile(code, args, compile_only=True) as p: + if p.returncode != 0: + m = 'BUG: Unable to compile {!r} check: {}' + raise RuntimeError(m.format(n, p.stdo)) + if not os.path.isfile(p.output_name): + m = 'BUG: Can\'t find compiled test code for {!r} check' + raise RuntimeError(m.format(n)) + with open(p.output_name, 'rb') as o: + for line in o: + # Check if the underscore form of the symbol is somewhere + # in the output file. + if b'_' + symbol_name in line: + return True + # Else, check if the non-underscored form is present + elif symbol_name in line: + return False + raise RuntimeError('BUG: {!r} check failed unexpectedly'.format(n)) + def find_library(self, libname, env, extra_dirs): # First try if we can just add the library as -l. code = '''int main(int argc, char **argv) { diff --git a/mesonbuild/interpreter.py b/mesonbuild/interpreter.py index 945430259..cff98b1cb 100644 --- a/mesonbuild/interpreter.py +++ b/mesonbuild/interpreter.py @@ -623,6 +623,7 @@ class CompilerHolder(InterpreterObject): 'has_argument' : self.has_argument_method, 'first_supported_argument' : self.first_supported_argument_method, 'unittest_args' : self.unittest_args_method, + 'symbols_have_underscore_prefix': self.symbols_have_underscore_prefix_method, }) def version_method(self, args, kwargs): @@ -698,6 +699,13 @@ class CompilerHolder(InterpreterObject): def get_id_method(self, args, kwargs): return self.compiler.get_id() + def symbols_have_underscore_prefix_method(self, args, kwargs): + ''' + Check if the compiler prefixes _ (underscore) to global C symbols + See: https://en.wikipedia.org/wiki/Name_mangling#C + ''' + return self.compiler.symbols_have_underscore_prefix(self.environment) + def unittest_args_method(self, args, kwargs): # At time, only D compilers have this feature. if not hasattr(self.compiler, 'get_unittest_args'):