summaryrefslogtreecommitdiff
path: root/mesonbuild/compilers
diff options
context:
space:
mode:
authorPatrick Steinhardt <ps@pks.im>2025-03-07 11:41:18 +0100
committerEli Schwartz <eschwartz93@gmail.com>2025-03-09 18:06:14 -0400
commit43ea11ea49948635b1d672fef1bd397233b65b19 (patch)
treee1fa9be70e178fb12d6d01ca8c07d3d4fa437ab9 /mesonbuild/compilers
parente629d191b99119c360605b056b954fff0905c83f (diff)
downloadmeson-43ea11ea49948635b1d672fef1bd397233b65b19.tar.gz
compilers: convert `b_sanitize` to a free-form array option
In the preceding commit we have started to perform compiler checks for the value of `b_sanitize`, which allows us to detect sanitizers that aren't supported by the compiler toolchain. But we haven't yet loosened the option itself to accept arbitrary values, so until now it's still only possible to pass sanitizer combinations known by Meson, which is quite restrictive. Lift that restriction by adapting the `b_sanitize` option to become a free-form array. Like this, users can pass whatever combination of comma-separated sanitizers to Meson, which will then figure out whether that combination is supported via the compiler checks. This lifts a couple of restrictions and makes the supporting infrastructure way more future proof. A couple of notes regarding backwards compatibility: - All previous values of `b_sanitize` will remain valid as the syntax for free-form array values and valid combo choices is the same. We also treat 'none' specially so that we know to convert it into an empty array. - Even though the option has been converted into a free-form array, callers of `get_option('b_sanitize')` continue to get a string as value. We may eventually want to introduce a kwarg to alter this behaviour, but for now it is expected to be good enough for most use cases. Fixes #8283 Fixes #7761 Fixes #5154 Fixes #1582 Co-authored-by: Dylan Baker <dylan@pnwbakers.com> Signed-off-by: Patrick Steinhardt <ps@pks.im>
Diffstat (limited to 'mesonbuild/compilers')
-rw-r--r--mesonbuild/compilers/compilers.py16
-rw-r--r--mesonbuild/compilers/cuda.py6
-rw-r--r--mesonbuild/compilers/mixins/gnu.py11
-rw-r--r--mesonbuild/compilers/mixins/islinker.py3
-rw-r--r--mesonbuild/compilers/mixins/visualstudio.py11
5 files changed, 25 insertions, 22 deletions
diff --git a/mesonbuild/compilers/compilers.py b/mesonbuild/compilers/compilers.py
index 27bc44b41..f0744f80b 100644
--- a/mesonbuild/compilers/compilers.py
+++ b/mesonbuild/compilers/compilers.py
@@ -222,9 +222,7 @@ BASE_OPTIONS: T.Mapping[OptionKey, options.AnyOptionType] = {
options.UserComboOption('b_lto_mode', 'Select between different LTO modes.', 'default', choices=['default', 'thin']),
options.UserBooleanOption('b_thinlto_cache', 'Use LLVM ThinLTO caching for faster incremental builds', False),
options.UserStringOption('b_thinlto_cache_dir', 'Directory to store ThinLTO cache objects', ''),
- options.UserComboOption(
- 'b_sanitize', 'Code sanitizer to use', 'none',
- choices=['none', 'address', 'thread', 'undefined', 'memory', 'leak', 'address,undefined']),
+ options.UserStringArrayOption('b_sanitize', 'Code sanitizer to use', []),
options.UserBooleanOption('b_lundef', 'Use -Wl,--no-undefined when linking', True),
options.UserBooleanOption('b_asneeded', 'Use -Wl,--as-needed when linking', True),
options.UserComboOption(
@@ -307,7 +305,9 @@ def get_base_compile_args(target: 'BuildTarget', compiler: 'Compiler', env: 'Env
pass
try:
sanitize = env.coredata.get_option_for_target(target, 'b_sanitize')
- assert isinstance(sanitize, str)
+ assert isinstance(sanitize, list)
+ if sanitize == ['none']:
+ sanitize = []
sanitize_args = compiler.sanitizer_compile_args(sanitize)
# We consider that if there are no sanitizer arguments returned, then
# the language doesn't support them.
@@ -376,7 +376,9 @@ def get_base_link_args(target: 'BuildTarget',
pass
try:
sanitizer = env.coredata.get_option_for_target(target, 'b_sanitize')
- assert isinstance(sanitizer, str)
+ assert isinstance(sanitizer, list)
+ if sanitizer == ['none']:
+ sanitizer = []
sanitizer_args = linker.sanitizer_link_args(sanitizer)
# We consider that if there are no sanitizer arguments returned, then
# the language doesn't support them.
@@ -1044,10 +1046,10 @@ class Compiler(HoldableObject, metaclass=abc.ABCMeta):
thinlto_cache_dir: T.Optional[str] = None) -> T.List[str]:
return self.linker.get_lto_args()
- def sanitizer_compile_args(self, value: str) -> T.List[str]:
+ def sanitizer_compile_args(self, value: T.List[str]) -> T.List[str]:
return []
- def sanitizer_link_args(self, value: str) -> T.List[str]:
+ def sanitizer_link_args(self, value: T.List[str]) -> T.List[str]:
return self.linker.sanitizer_args(value)
def get_asneeded_args(self) -> T.List[str]:
diff --git a/mesonbuild/compilers/cuda.py b/mesonbuild/compilers/cuda.py
index 134cd4ede..612643c8f 100644
--- a/mesonbuild/compilers/cuda.py
+++ b/mesonbuild/compilers/cuda.py
@@ -1,6 +1,6 @@
# SPDX-License-Identifier: Apache-2.0
# Copyright 2012-2017 The Meson development team
-# Copyright © 2024 Intel Corporation
+# Copyright © 2023-2024 Intel Corporation
from __future__ import annotations
@@ -700,10 +700,10 @@ class CudaCompiler(Compiler):
# return self._to_host_flags(self.host_compiler.get_optimization_args(optimization_level))
return cuda_optimization_args[optimization_level]
- def sanitizer_compile_args(self, value: str) -> T.List[str]:
+ def sanitizer_compile_args(self, value: T.List[str]) -> T.List[str]:
return self._to_host_flags(self.host_compiler.sanitizer_compile_args(value))
- def sanitizer_link_args(self, value: str) -> T.List[str]:
+ def sanitizer_link_args(self, value: T.List[str]) -> T.List[str]:
return self._to_host_flags(self.host_compiler.sanitizer_link_args(value))
def get_debug_args(self, is_debug: bool) -> T.List[str]:
diff --git a/mesonbuild/compilers/mixins/gnu.py b/mesonbuild/compilers/mixins/gnu.py
index 4dc344519..70fd9ee7d 100644
--- a/mesonbuild/compilers/mixins/gnu.py
+++ b/mesonbuild/compilers/mixins/gnu.py
@@ -1,5 +1,6 @@
# SPDX-License-Identifier: Apache-2.0
# Copyright 2019-2022 The meson development team
+# Copyright © 2023 Intel Corporation
from __future__ import annotations
@@ -495,11 +496,11 @@ class GnuLikeCompiler(Compiler, metaclass=abc.ABCMeta):
# for their specific arguments
return ['-flto']
- def sanitizer_compile_args(self, value: str) -> T.List[str]:
- if value == 'none':
- return []
- args = ['-fsanitize=' + value]
- if 'address' in value: # for -fsanitize=address,undefined
+ def sanitizer_compile_args(self, value: T.List[str]) -> T.List[str]:
+ if not value:
+ return value
+ args = ['-fsanitize=' + ','.join(value)]
+ if 'address' in value:
args.append('-fno-omit-frame-pointer')
return args
diff --git a/mesonbuild/compilers/mixins/islinker.py b/mesonbuild/compilers/mixins/islinker.py
index 44040a7a2..3f3561972 100644
--- a/mesonbuild/compilers/mixins/islinker.py
+++ b/mesonbuild/compilers/mixins/islinker.py
@@ -1,5 +1,6 @@
# SPDX-License-Identifier: Apache-2.0
# Copyright 2019 The Meson development team
+# Copyright © 2023 Intel Corporation
from __future__ import annotations
@@ -37,7 +38,7 @@ class BasicLinkerIsCompilerMixin(Compiler):
functionality itself.
"""
- def sanitizer_link_args(self, value: str) -> T.List[str]:
+ def sanitizer_link_args(self, value: T.List[str]) -> T.List[str]:
return []
def get_lto_link_args(self, *, threads: int = 0, mode: str = 'default',
diff --git a/mesonbuild/compilers/mixins/visualstudio.py b/mesonbuild/compilers/mixins/visualstudio.py
index 30127eca4..275e7ab0a 100644
--- a/mesonbuild/compilers/mixins/visualstudio.py
+++ b/mesonbuild/compilers/mixins/visualstudio.py
@@ -1,5 +1,6 @@
# SPDX-License-Identifier: Apache-2.0
# Copyright 2019 The meson development team
+# Copyright © 2023 Intel Corporation
from __future__ import annotations
@@ -166,12 +167,10 @@ class VisualStudioLikeCompiler(Compiler, metaclass=abc.ABCMeta):
def get_no_optimization_args(self) -> T.List[str]:
return ['/Od', '/Oi-']
- def sanitizer_compile_args(self, value: str) -> T.List[str]:
- if value == 'none':
- return []
- if value != 'address':
- raise mesonlib.MesonException('VS only supports address sanitizer at the moment.')
- return ['/fsanitize=address']
+ def sanitizer_compile_args(self, value: T.List[str]) -> T.List[str]:
+ if not value:
+ return value
+ return [f'/fsanitize={",".join(value)}']
def get_output_args(self, outputname: str) -> T.List[str]:
if self.mode == 'PREPROCESSOR':