diff options
author | Nirbheek Chauhan <nirbheek@centricular.com> | 2019-03-12 17:56:44 +0530 |
---|---|---|
committer | Nirbheek Chauhan <nirbheek.chauhan@gmail.com> | 2019-03-14 12:21:05 +0000 |
commit | 07818dac6acc7a805548be4b605320dd27de126c (patch) | |
tree | 768d8eff8970d3c51312153ea0426660f11c9779 | |
parent | 5f00c3020073962edbeb2f3f709c27acdb09fd74 (diff) | |
download | meson-07818dac6acc7a805548be4b605320dd27de126c.tar.gz |
compilers: Try harder to dedup builtin libs
Compiler internal libs should always be de-duplicated, no matter what.
Closes https://github.com/mesonbuild/meson/issues/2150
Test case is by Bruce Richardson in the issue.
-rw-r--r-- | mesonbuild/compilers/c.py | 7 | ||||
-rw-r--r-- | mesonbuild/compilers/compilers.py | 8 | ||||
-rwxr-xr-x | run_unittests.py | 15 | ||||
-rw-r--r-- | test cases/unit/55 dedup compiler libs/app/app.c | 13 | ||||
-rw-r--r-- | test cases/unit/55 dedup compiler libs/app/meson.build | 2 | ||||
-rw-r--r-- | test cases/unit/55 dedup compiler libs/liba/liba.c | 18 | ||||
-rw-r--r-- | test cases/unit/55 dedup compiler libs/liba/liba.h | 8 | ||||
-rw-r--r-- | test cases/unit/55 dedup compiler libs/liba/meson.build | 8 | ||||
-rw-r--r-- | test cases/unit/55 dedup compiler libs/libb/libb.c | 7 | ||||
-rw-r--r-- | test cases/unit/55 dedup compiler libs/libb/libb.h | 6 | ||||
-rw-r--r-- | test cases/unit/55 dedup compiler libs/libb/meson.build | 6 | ||||
-rw-r--r-- | test cases/unit/55 dedup compiler libs/meson.build | 7 |
12 files changed, 99 insertions, 6 deletions
diff --git a/mesonbuild/compilers/c.py b/mesonbuild/compilers/c.py index d4626d6d5..d4d6f6629 100644 --- a/mesonbuild/compilers/c.py +++ b/mesonbuild/compilers/c.py @@ -35,6 +35,7 @@ from .compilers import ( get_largefile_args, gnu_winlibs, msvc_winlibs, + unixy_compiler_internal_libs, vs32_instruction_set_args, vs64_instruction_set_args, ArmCompiler, @@ -52,8 +53,6 @@ from .compilers import ( CcrxCompiler, ) -gnu_compiler_internal_libs = ('m', 'c', 'pthread', 'dl', 'rt') - class CCompiler(Compiler): # TODO: Replace this manual cache with functools.lru_cache @@ -61,7 +60,7 @@ class CCompiler(Compiler): program_dirs_cache = {} find_library_cache = {} find_framework_cache = {} - internal_libs = gnu_compiler_internal_libs + internal_libs = unixy_compiler_internal_libs @staticmethod def attribute_check_func(name): @@ -1375,7 +1374,7 @@ class IntelCCompiler(IntelCompiler, CCompiler): class VisualStudioCCompiler(CCompiler): std_warn_args = ['/W3'] std_opt_args = ['/O2'] - ignore_libs = gnu_compiler_internal_libs + ignore_libs = unixy_compiler_internal_libs internal_libs = () crt_args = {'none': [], diff --git a/mesonbuild/compilers/compilers.py b/mesonbuild/compilers/compilers.py index 94ebf1622..ceefefeff 100644 --- a/mesonbuild/compilers/compilers.py +++ b/mesonbuild/compilers/compilers.py @@ -81,6 +81,9 @@ cflags_mapping = {'c': 'CFLAGS', 'vala': 'VALAFLAGS', 'rust': 'RUSTFLAGS'} +# execinfo is a compiler lib on BSD +unixy_compiler_internal_libs = ('m', 'c', 'pthread', 'dl', 'rt', 'execinfo') + # All these are only for C-linkable languages; see `clink_langs` above. def sort_clink(lang): @@ -659,6 +662,9 @@ class CompilerArgs(list): # Only UNIX shared libraries require this. Others have a fixed extension. dedup1_regex = re.compile(r'([\/\\]|\A)lib.*\.so(\.[0-9]+)?(\.[0-9]+)?(\.[0-9]+)?$') dedup1_args = ('-c', '-S', '-E', '-pipe', '-pthread') + # In generate_link() we add external libs without de-dup, but we must + # *always* de-dup these because they're special arguments to the linker + always_dedup_args = tuple('-l' + lib for lib in unixy_compiler_internal_libs) compiler = None def _check_args(self, args): @@ -793,7 +799,7 @@ class CompilerArgs(list): normal_flags = [] lflags = [] for i in iterable: - if i.startswith('-l') or i.startswith('-L'): + if i not in self.always_dedup_args and (i.startswith('-l') or i.startswith('-L')): lflags.append(i) else: normal_flags.append(i) diff --git a/run_unittests.py b/run_unittests.py index 3f1a29017..0b2164fe6 100755 --- a/run_unittests.py +++ b/run_unittests.py @@ -3893,7 +3893,7 @@ class WindowsTests(BasePlatformTests): if cc.get_argument_syntax() != 'msvc': raise unittest.SkipTest('Not using MSVC') # To force people to update this test, and also test - self.assertEqual(set(cc.ignore_libs), {'c', 'm', 'pthread', 'dl', 'rt'}) + self.assertEqual(set(cc.ignore_libs), {'c', 'm', 'pthread', 'dl', 'rt', 'execinfo'}) for l in cc.ignore_libs: self.assertEqual(cc.find_library(l, env, []), []) @@ -5043,6 +5043,19 @@ endian = 'little' max_count = max(max_count, line.count(search_term)) self.assertEqual(max_count, 1, 'Export dynamic incorrectly deduplicated.') + def test_compiler_libs_static_dedup(self): + testdir = os.path.join(self.unit_test_dir, '55 dedup compiler libs') + self.init(testdir) + build_ninja = os.path.join(self.builddir, 'build.ninja') + with open(build_ninja, 'r', encoding='utf-8') as f: + lines = f.readlines() + for lib in ('-ldl', '-lm', '-lc', '-lrt'): + for line in lines: + if lib not in line: + continue + # Assert that + self.assertEqual(len(line.split(lib)), 2, msg=(lib, line)) + def should_run_cross_arm_tests(): return shutil.which('arm-linux-gnueabihf-gcc') and not platform.machine().lower().startswith('arm') diff --git a/test cases/unit/55 dedup compiler libs/app/app.c b/test cases/unit/55 dedup compiler libs/app/app.c new file mode 100644 index 000000000..4e215b30b --- /dev/null +++ b/test cases/unit/55 dedup compiler libs/app/app.c @@ -0,0 +1,13 @@ +#include <stdio.h> +#include <liba.h> +#include <libb.h> + +int +main(void) +{ + printf("start value = %d\n", liba_get()); + liba_add(2); + libb_mul(5); + printf("end value = %d\n", liba_get()); + return 0; +} diff --git a/test cases/unit/55 dedup compiler libs/app/meson.build b/test cases/unit/55 dedup compiler libs/app/meson.build new file mode 100644 index 000000000..82ac3069d --- /dev/null +++ b/test cases/unit/55 dedup compiler libs/app/meson.build @@ -0,0 +1,2 @@ +executable('app', 'app.c', + dependencies: [liba_dep, libb_dep]) diff --git a/test cases/unit/55 dedup compiler libs/liba/liba.c b/test cases/unit/55 dedup compiler libs/liba/liba.c new file mode 100644 index 000000000..962d47f30 --- /dev/null +++ b/test cases/unit/55 dedup compiler libs/liba/liba.c @@ -0,0 +1,18 @@ +#include "liba.h" + +static int val; + +void liba_add(int x) +{ + val += x; +} + +void liba_sub(int x) +{ + val -= x; +} + +int liba_get(void) +{ + return val; +} diff --git a/test cases/unit/55 dedup compiler libs/liba/liba.h b/test cases/unit/55 dedup compiler libs/liba/liba.h new file mode 100644 index 000000000..a980cdc6b --- /dev/null +++ b/test cases/unit/55 dedup compiler libs/liba/liba.h @@ -0,0 +1,8 @@ +#ifndef LIBA_H_ +#define LIBA_H_ + +void liba_add(int x); +void liba_sub(int x); +int liba_get(void); + +#endif diff --git a/test cases/unit/55 dedup compiler libs/liba/meson.build b/test cases/unit/55 dedup compiler libs/liba/meson.build new file mode 100644 index 000000000..eccfa46c3 --- /dev/null +++ b/test cases/unit/55 dedup compiler libs/liba/meson.build @@ -0,0 +1,8 @@ +deps = [dependency('threads'), cc.find_library('dl'), cc.find_library('m')] + +liba = library('a', 'liba.c', + dependencies: deps) + +liba_dep = declare_dependency(link_with: liba, + include_directories: include_directories('.'), + dependencies: deps) diff --git a/test cases/unit/55 dedup compiler libs/libb/libb.c b/test cases/unit/55 dedup compiler libs/libb/libb.c new file mode 100644 index 000000000..372086878 --- /dev/null +++ b/test cases/unit/55 dedup compiler libs/libb/libb.c @@ -0,0 +1,7 @@ +#include <liba.h> +#include "libb.h" + +void libb_mul(int x) +{ + liba_add(liba_get() * (x - 1)); +} diff --git a/test cases/unit/55 dedup compiler libs/libb/libb.h b/test cases/unit/55 dedup compiler libs/libb/libb.h new file mode 100644 index 000000000..2e4ddd0c7 --- /dev/null +++ b/test cases/unit/55 dedup compiler libs/libb/libb.h @@ -0,0 +1,6 @@ +#ifndef _LIBB_H_ +#define _LIBB_H_ + +void libb_mul(int x); + +#endif diff --git a/test cases/unit/55 dedup compiler libs/libb/meson.build b/test cases/unit/55 dedup compiler libs/libb/meson.build new file mode 100644 index 000000000..d59206f8d --- /dev/null +++ b/test cases/unit/55 dedup compiler libs/libb/meson.build @@ -0,0 +1,6 @@ +libb = library('b', 'libb.c', + dependencies: liba_dep) + +libb_dep = declare_dependency(link_with: libb, + include_directories: include_directories('.'), + dependencies: liba_dep) diff --git a/test cases/unit/55 dedup compiler libs/meson.build b/test cases/unit/55 dedup compiler libs/meson.build new file mode 100644 index 000000000..fad0ed556 --- /dev/null +++ b/test cases/unit/55 dedup compiler libs/meson.build @@ -0,0 +1,7 @@ +project('temp', 'c') + +cc = meson.get_compiler('c') + +subdir('liba') +subdir('libb') +subdir('app') |