From 4ec063cfc4943426d590a785f073460b23a8832a Mon Sep 17 00:00:00 2001 From: Nirbheek Chauhan Date: Fri, 24 Aug 2018 04:16:48 +0530 Subject: CompilerArgs: Allow calling to_native() multiple times Add a keyword argument to to_native() to operate on a copy so that we can call it multiple times instead of modifying the original compiler args while iterating. This is used in the unit test, and might be used in Meson at some point too. --- mesonbuild/compilers/compilers.py | 14 +++++++++----- run_unittests.py | 37 ++++++++++++++++++++++++++++++------- 2 files changed, 39 insertions(+), 12 deletions(-) diff --git a/mesonbuild/compilers/compilers.py b/mesonbuild/compilers/compilers.py index f92b984..a047f7d 100644 --- a/mesonbuild/compilers/compilers.py +++ b/mesonbuild/compilers/compilers.py @@ -554,16 +554,20 @@ class CompilerArgs(list): return True return False - def to_native(self): + def to_native(self, copy=False): # Check if we need to add --start/end-group for circular dependencies # between static libraries, and for recursively searching for symbols # needed by static libraries that are provided by object files or # shared libraries. + if copy: + new = self.copy() + else: + new = self if get_compiler_uses_gnuld(self.compiler): global soregex group_start = -1 group_end = -1 - for i, each in enumerate(self): + for i, each in enumerate(new): if not each.startswith('-l') and not each.endswith('.a') and \ not soregex.match(each): continue @@ -573,9 +577,9 @@ class CompilerArgs(list): group_start = i if group_start >= 0: # Last occurrence of a library - self.insert(group_end + 1, '-Wl,--end-group') - self.insert(group_start, '-Wl,--start-group') - return self.compiler.unix_args_to_native(self) + new.insert(group_end + 1, '-Wl,--end-group') + new.insert(group_start, '-Wl,--start-group') + return self.compiler.unix_args_to_native(new) def append_direct(self, arg): ''' diff --git a/run_unittests.py b/run_unittests.py index 056f8e8..2826460 100755 --- a/run_unittests.py +++ b/run_unittests.py @@ -163,19 +163,19 @@ class InternalTests(unittest.TestCase): def test_compiler_args_class(self): cargsfunc = mesonbuild.compilers.CompilerArgs - c = mesonbuild.compilers.CCompiler([], 'fake', False) + cc = mesonbuild.compilers.CCompiler([], 'fake', False) # Test that bad initialization fails self.assertRaises(TypeError, cargsfunc, []) self.assertRaises(TypeError, cargsfunc, [], []) - self.assertRaises(TypeError, cargsfunc, c, [], []) + self.assertRaises(TypeError, cargsfunc, cc, [], []) # Test that empty initialization works - a = cargsfunc(c) + a = cargsfunc(cc) self.assertEqual(a, []) # Test that list initialization works - a = cargsfunc(['-I.', '-I..'], c) + a = cargsfunc(['-I.', '-I..'], cc) self.assertEqual(a, ['-I.', '-I..']) # Test that there is no de-dup on initialization - self.assertEqual(cargsfunc(['-I.', '-I.'], c), ['-I.', '-I.']) + self.assertEqual(cargsfunc(['-I.', '-I.'], cc), ['-I.', '-I.']) ## Test that appending works a.append('-I..') @@ -221,7 +221,7 @@ class InternalTests(unittest.TestCase): self.assertEqual(a, ['-Ibar', '-Ifoo', '-Ibaz', '-I..', '-I.', '-Ldir', '-Lbah', '-Werror', '-O3', '-O2', '-Wall']) ## Test that adding libraries works - l = cargsfunc(c, ['-Lfoodir', '-lfoo']) + l = cargsfunc(cc, ['-Lfoodir', '-lfoo']) self.assertEqual(l, ['-Lfoodir', '-lfoo']) # Adding a library and a libpath appends both correctly l += ['-Lbardir', '-lbar'] @@ -231,7 +231,7 @@ class InternalTests(unittest.TestCase): self.assertEqual(l, ['-Lbardir', '-Lfoodir', '-lfoo', '-lbar']) ## Test that 'direct' append and extend works - l = cargsfunc(c, ['-Lfoodir', '-lfoo']) + l = cargsfunc(cc, ['-Lfoodir', '-lfoo']) self.assertEqual(l, ['-Lfoodir', '-lfoo']) # Direct-adding a library and a libpath appends both correctly l.extend_direct(['-Lbardir', '-lbar']) @@ -246,6 +246,29 @@ class InternalTests(unittest.TestCase): l.append_direct('/libbaz.a') self.assertEqual(l, ['-Lfoodir', '-lfoo', '-Lbardir', '-lbar', '-lbar', '/libbaz.a']) + def test_compiler_args_class_gnuld(self): + cargsfunc = mesonbuild.compilers.CompilerArgs + ## Test --start/end-group + gcc = mesonbuild.compilers.GnuCCompiler([], 'fake', 0, False) + ## Test that 'direct' append and extend works + l = cargsfunc(gcc, ['-Lfoodir', '-lfoo']) + self.assertEqual(l.to_native(copy=True), ['-Lfoodir', '-Wl,--start-group', '-lfoo', '-Wl,--end-group']) + # Direct-adding a library and a libpath appends both correctly + l.extend_direct(['-Lbardir', '-lbar']) + self.assertEqual(l.to_native(copy=True), ['-Lfoodir', '-Wl,--start-group', '-lfoo', '-Lbardir', '-lbar', '-Wl,--end-group']) + # Direct-adding the same library again still adds it + l.append_direct('-lbar') + self.assertEqual(l.to_native(copy=True), ['-Lfoodir', '-Wl,--start-group', '-lfoo', '-Lbardir', '-lbar', '-lbar', '-Wl,--end-group']) + # Direct-adding with absolute path deduplicates + l.append_direct('/libbaz.a') + self.assertEqual(l.to_native(copy=True), ['-Lfoodir', '-Wl,--start-group', '-lfoo', '-Lbardir', '-lbar', '-lbar', '/libbaz.a', '-Wl,--end-group']) + # Adding libbaz again does nothing + l.append_direct('/libbaz.a') + self.assertEqual(l.to_native(copy=True), ['-Lfoodir', '-Wl,--start-group', '-lfoo', '-Lbardir', '-lbar', '-lbar', '/libbaz.a', '-Wl,--end-group']) + # Adding a non-library argument doesn't include it in the group + l += ['-Lfoo', '-Wl,--export-dynamic'] + self.assertEqual(l.to_native(copy=True), ['-Lfoo', '-Lfoodir', '-Wl,--start-group', '-lfoo', '-Lbardir', '-lbar', '-lbar', '/libbaz.a', '-Wl,--end-group', '-Wl,--export-dynamic']) + def test_string_templates_substitution(self): dictfunc = mesonbuild.mesonlib.get_filenames_templates_dict substfunc = mesonbuild.mesonlib.substitute_values -- cgit v1.1