From 819f063d66be55f34f7b192db4c102caa2a503ae Mon Sep 17 00:00:00 2001 From: Andrew McNulty Date: Tue, 17 Sep 2024 21:28:31 +0100 Subject: [PATCH] compilers: Pass all options to links/compiles The `compiler.links()` and `compiler.compiles()` functions did not take into account all arguments which might be added to a real link or real compile job, specifically it was failing to add many of the options which are added as a result of language level options such as `b_coverage` or `b_sanitize`. This commit fixes that by constructing the args list for these functions using the same methods as a real compile/link job. A new unittest has been added to check that this is functioning correctly. Fixes: #13610 --- mesonbuild/interpreter/compiler.py | 6 ++++-- .../unit/123 compiler.links args/meson.build | 18 ++++++++++++++++++ unittests/allplatformstests.py | 10 ++++++++++ 3 files changed, 32 insertions(+), 2 deletions(-) create mode 100644 test cases/unit/123 compiler.links args/meson.build diff --git a/mesonbuild/interpreter/compiler.py b/mesonbuild/interpreter/compiler.py index 90514446bb12..e365dfa5fae4 100644 --- a/mesonbuild/interpreter/compiler.py +++ b/mesonbuild/interpreter/compiler.py @@ -17,7 +17,7 @@ from .. import mesonlib from .. import mlog from ..compilers import SUFFIX_TO_LANG, RunResult -from ..compilers.compilers import CompileCheckMode +from ..compilers.compilers import CompileCheckMode, get_base_compile_args, get_base_link_args from ..interpreterbase import (ObjectHolder, noPosargs, noKwargs, FeatureNew, FeatureNewKwargs, disablerIfNotFound, InterpreterException) @@ -271,9 +271,11 @@ def _determine_args(self, kwargs: BaseCompileKW, args.extend(self.compiler.get_include_args(idir, False)) if not kwargs['no_builtin_args']: opts = coredata.OptionsView(self.environment.coredata.optstore, self.subproject) - args += self.compiler.get_option_compile_args(opts) + args.extend(self.compiler.get_option_compile_args(opts)) + args.extend(get_base_compile_args(opts, self.compiler, self.environment)) if mode is CompileCheckMode.LINK: args.extend(self.compiler.get_option_link_args(opts)) + args.extend(get_base_link_args(opts, self.compiler, False, self.environment.get_build_dir())) if kwargs.get('werror', False): args.extend(self.compiler.get_werror_args()) args.extend(kwargs['args']) diff --git a/test cases/unit/123 compiler.links args/meson.build b/test cases/unit/123 compiler.links args/meson.build new file mode 100644 index 000000000000..de4cfffbcd29 --- /dev/null +++ b/test cases/unit/123 compiler.links args/meson.build @@ -0,0 +1,18 @@ +# https://github.com/mesonbuild/meson/issues/13610 + +project('279 compiler.links args', 'c') +cc = meson.get_compiler('c') + +gcov_str = ''' +#include +int main(void) { + __gcov_dump(); + return 0; +} +''' + +if cc.links(gcov_str) + message('gcov links successfully') +else + error('gcov does not link') +endif diff --git a/unittests/allplatformstests.py b/unittests/allplatformstests.py index 3be97cb3b613..1747b2f8567e 100644 --- a/unittests/allplatformstests.py +++ b/unittests/allplatformstests.py @@ -5040,3 +5040,13 @@ def test_rsp_support(self): 'link', 'lld-link', 'mwldarm', 'mwldeppc', 'optlink', 'xilink', } self.assertEqual(cc.linker.get_accepts_rsp(), has_rsp) + + def test_compiler_link_args(self): + testdir = os.path.join(self.unit_test_dir, '279 compiler.links args') + + with self.assertRaises(subprocess.CalledProcessError) as cm: + self.init(testdir) + self.assertIn('gcov does not link', cm.exception.stdout) + + # This should succeed without raising due to '-Db_coverage=true' + self.init(testdir, extra_args=['-Db_coverage=true'])