From bfce5c45a4784d86be8472a00f628f2ffac74cf2 Mon Sep 17 00:00:00 2001 From: Dylan Baker Date: Wed, 22 Feb 2023 13:10:24 -0800 Subject: [PATCH] compilers: convert method to get assert control to a boolean C like compilers only off `-DNDEBUG` to disable asserts. This is not a universal paradigm however. Rust, for example has an argument that takes a boolean. To better represent this, we allow passing a `disable` boolean. `disable` was chosen rather than `enable` because it allowed all existing logic to be left in place --- mesonbuild/compilers/compilers.py | 15 ++++++++++----- mesonbuild/compilers/cuda.py | 4 ++-- mesonbuild/compilers/d.py | 18 ++++++++++++------ mesonbuild/compilers/mixins/clike.py | 6 ++++-- 4 files changed, 28 insertions(+), 15 deletions(-) diff --git a/mesonbuild/compilers/compilers.py b/mesonbuild/compilers/compilers.py index 2f3086acc..ee382f043 100644 --- a/mesonbuild/compilers/compilers.py +++ b/mesonbuild/compilers/compilers.py @@ -365,10 +365,10 @@ def get_base_compile_args(options: 'KeyedOptionDictType', compiler: 'Compiler') except KeyError: pass try: - if (options[OptionKey('b_ndebug')].value == 'true' or - (options[OptionKey('b_ndebug')].value == 'if-release' and - options[OptionKey('buildtype')].value in {'release', 'plain'})): - args += compiler.get_disable_assert_args() + disable = (options[OptionKey('b_ndebug')].value == 'true' or + (options[OptionKey('b_ndebug')].value == 'if-release' and + options[OptionKey('buildtype')].value in {'release', 'plain'})) + args += compiler.get_assert_args(disable) except KeyError: pass # This does not need a try...except @@ -1071,7 +1071,12 @@ class Compiler(HoldableObject, metaclass=abc.ABCMeta): def get_coverage_link_args(self) -> T.List[str]: return self.linker.get_coverage_args() - def get_disable_assert_args(self) -> T.List[str]: + def get_assert_args(self, disable: bool) -> T.List[str]: + """Get arguments to enable or disable assertion. + + :param disable: Whether to disable assertions + :return: A list of string arguments for this compiler + """ return [] def get_crt_compile_args(self, crt_val: str, buildtype: str) -> T.List[str]: diff --git a/mesonbuild/compilers/cuda.py b/mesonbuild/compilers/cuda.py index 70a4fe9e1..bbc2fc8fc 100644 --- a/mesonbuild/compilers/cuda.py +++ b/mesonbuild/compilers/cuda.py @@ -787,5 +787,5 @@ class CudaCompiler(Compiler): def get_profile_use_args(self) -> T.List[str]: return ['-Xcompiler=' + x for x in self.host_compiler.get_profile_use_args()] - def get_disable_assert_args(self) -> T.List[str]: - return self.host_compiler.get_disable_assert_args() + def get_assert_args(self, disable: bool) -> T.List[str]: + return self.host_compiler.get_assert_args(disable) diff --git a/mesonbuild/compilers/d.py b/mesonbuild/compilers/d.py index 5d43a6011..4cdda0093 100644 --- a/mesonbuild/compilers/d.py +++ b/mesonbuild/compilers/d.py @@ -855,8 +855,10 @@ class GnuDCompiler(GnuCompiler, DCompiler): return args return args + ['-shared-libphobos'] - def get_disable_assert_args(self) -> T.List[str]: - return ['-frelease'] + def get_assert_args(self, disable: bool) -> T.List[str]: + if disable: + return ['-frelease'] + return [] # LDC uses the DMD frontend code to parse and analyse the code. # It then uses LLVM for the binary code generation and optimizations. @@ -927,8 +929,10 @@ class LLVMDCompiler(DmdLikeCompilerMixin, DCompiler): return args return args + ['-link-defaultlib-shared'] - def get_disable_assert_args(self) -> T.List[str]: - return ['--release'] + def get_assert_args(self, disable: bool) -> T.List[str]: + if disable: + return ['--release'] + return [] def rsp_file_syntax(self) -> RSPFileSyntax: # We use `mesonlib.is_windows` here because we want to know what the @@ -1015,8 +1019,10 @@ class DmdDCompiler(DmdLikeCompilerMixin, DCompiler): return args return args + ['-defaultlib=phobos2', '-debuglib=phobos2'] - def get_disable_assert_args(self) -> T.List[str]: - return ['-release'] + def get_assert_args(self, disable: bool) -> T.List[str]: + if disable: + return ['-release'] + return [] def rsp_file_syntax(self) -> RSPFileSyntax: return RSPFileSyntax.MSVC diff --git a/mesonbuild/compilers/mixins/clike.py b/mesonbuild/compilers/mixins/clike.py index f8c7ebcd2..4ebec3f8e 100644 --- a/mesonbuild/compilers/mixins/clike.py +++ b/mesonbuild/compilers/mixins/clike.py @@ -1332,8 +1332,10 @@ class CLikeCompiler(Compiler): return self.compiles(self.attribute_check_func(name), env, extra_args=self.get_has_func_attribute_extra_args(name)) - def get_disable_assert_args(self) -> T.List[str]: - return ['-DNDEBUG'] + def get_assert_args(self, disable: bool) -> T.List[str]: + if disable: + return ['-DNDEBUG'] + return [] @functools.lru_cache(maxsize=None) def can_compile(self, src: 'mesonlib.FileOrString') -> bool: