From bb0e3dcf7e00bae312d99940b9edd9bbf96fd93f Mon Sep 17 00:00:00 2001 From: Dylan Baker Date: Tue, 16 Feb 2021 12:01:36 -0800 Subject: [PATCH] compilers: Only insert -flto-jobs in clang's link arguments Clang has a hand `-Wunused-command-line-argument` switch, which when turned to an error, gets very grump about `-flto-jobs=0` being set in the compiler arguments (although `-flto=` belongs there). We'll refactor a bit to put that only in the link arguments. GCC doesn't have this probably because, a) it doesn't have an equivalent warning, and b) it uses `-flto=<$numthreads. Fixes: #8347 --- mesonbuild/compilers/compilers.py | 6 ++++-- mesonbuild/compilers/mixins/clang.py | 4 ++++ mesonbuild/compilers/mixins/islinker.py | 2 +- run_unittests.py | 14 +++++++++----- 4 files changed, 18 insertions(+), 8 deletions(-) diff --git a/mesonbuild/compilers/compilers.py b/mesonbuild/compilers/compilers.py index 8171758f5e6b..a4823e2da977 100644 --- a/mesonbuild/compilers/compilers.py +++ b/mesonbuild/compilers/compilers.py @@ -375,7 +375,9 @@ def get_base_link_args(options: 'KeyedOptionDictType', linker: 'Compiler', args = [] # type: T.List[str] try: if options[OptionKey('b_lto')].value: - args.extend(linker.get_lto_link_args()) + args.extend(linker.get_lto_link_args( + threads=get_option_value(options, OptionKey('b_lto_threads'), 0), + mode=get_option_value(options, OptionKey('b_lto_mode'), 'default'))) except KeyError: pass try: @@ -950,7 +952,7 @@ def remove_linkerlike_args(self, args: T.List[str]) -> T.List[str]: def get_lto_compile_args(self, *, threads: int = 0, mode: str = 'default') -> T.List[str]: return [] - def get_lto_link_args(self) -> T.List[str]: + def get_lto_link_args(self, *, threads: int = 0, mode: str = 'default') -> T.List[str]: return self.linker.get_lto_args() def sanitizer_compile_args(self, value: str) -> T.List[str]: diff --git a/mesonbuild/compilers/mixins/clang.py b/mesonbuild/compilers/mixins/clang.py index 1778c3131b37..7531e572d186 100644 --- a/mesonbuild/compilers/mixins/clang.py +++ b/mesonbuild/compilers/mixins/clang.py @@ -148,6 +148,10 @@ def get_lto_compile_args(self, *, threads: int = 0, mode: str = 'default') -> T. else: assert mode == 'default', 'someone forgot to wire something up' args.extend(super().get_lto_compile_args(threads=threads)) + return args + + def get_lto_link_args(self, *, threads: int = 0, mode: str = 'default') -> T.List[str]: + args = self.get_lto_compile_args(threads=threads, mode=mode) # In clang -flto=0 means auto if threads >= 0: args.append(f'-flto-jobs={threads}') diff --git a/mesonbuild/compilers/mixins/islinker.py b/mesonbuild/compilers/mixins/islinker.py index 3fe338215cbe..298d47ff00a6 100644 --- a/mesonbuild/compilers/mixins/islinker.py +++ b/mesonbuild/compilers/mixins/islinker.py @@ -48,7 +48,7 @@ class BasicLinkerIsCompilerMixin(Compiler): def sanitizer_link_args(self, value: str) -> T.List[str]: return [] - def get_lto_link_args(self) -> T.List[str]: + def get_lto_link_args(self, *, threads: int = 0, mode: str = 'default') -> T.List[str]: return [] def can_linker_accept_rsp(self) -> bool: diff --git a/run_unittests.py b/run_unittests.py index 73be9b7ae21e..a41559ff81e4 100755 --- a/run_unittests.py +++ b/run_unittests.py @@ -3062,10 +3062,14 @@ def test_lto_threads(self): env = get_fake_env(testdir, self.builddir, self.prefix) cc = env.detect_c_compiler(MachineChoice.HOST) - if cc.get_id() == 'clang' and is_windows(): - raise unittest.SkipTest('LTO not (yet) supported by windows clang') + extra_args: T.List[str] = [] + if cc.get_id() == 'clang': + if is_windows(): + raise unittest.SkipTest('LTO not (yet) supported by windows clang') + else: + extra_args.append('-D_cargs=-Werror=unused-command-line-argument') - self.init(testdir, extra_args=['-Db_lto=true', '-Db_lto_threads=8']) + self.init(testdir, extra_args=['-Db_lto=true', '-Db_lto_threads=8'] + extra_args) self.build() self.run_tests() @@ -3091,7 +3095,7 @@ def test_lto_mode(self): elif is_windows(): raise unittest.SkipTest('LTO not (yet) supported by windows clang') - self.init(testdir, extra_args=['-Db_lto=true', '-Db_lto_mode=thin', '-Db_lto_threads=8']) + self.init(testdir, extra_args=['-Db_lto=true', '-Db_lto_mode=thin', '-Db_lto_threads=8', '-Dc_args=-Werror=unused-command-line-argument']) self.build() self.run_tests() @@ -3100,7 +3104,7 @@ def test_lto_mode(self): # This assumes all of the targets support lto for t in targets: for s in t['target_sources']: - assert expected.issubset(set(s['parameters'])), f'Incorrect values for {t["name"]}' + self.assertTrue(expected.issubset(set(s['parameters'])), f'Incorrect values for {t["name"]}') def test_dist_git(self): if not shutil.which('git'):