From a63739d394dd77314270f5a46f79171a8c544e77 Mon Sep 17 00:00:00 2001 From: Sam James Date: Wed, 13 Mar 2024 01:15:07 +0000 Subject: compilers: cpp: reduce macro pollution for stdlib macros Now that we have access to Environment in get_assert_args, we can check what the actual C++ stdlib provider is and only set relevant macros rather than all possibly-relevant ones based on the compiler. Also, while we're here, now that's sorted, wire up the GCC experimental libc++ support in the macro emission given it doesn't uglify anything for libstdc++ users now. Bug: https://github.com/mesonbuild/meson/issues/12962 Signed-off-by: Sam James Signed-off-by: Eli Schwartz --- mesonbuild/compilers/cpp.py | 35 ++++++++++++++++++++--------------- 1 file changed, 20 insertions(+), 15 deletions(-) (limited to 'mesonbuild/compilers/cpp.py') diff --git a/mesonbuild/compilers/cpp.py b/mesonbuild/compilers/cpp.py index 449a6888b..525c9fcdf 100644 --- a/mesonbuild/compilers/cpp.py +++ b/mesonbuild/compilers/cpp.py @@ -311,7 +311,6 @@ class ClangCPPCompiler(_StdCPPLibMixin, ClangCompiler, CPPCompiler): return [] def get_assert_args(self, disable: bool, env: 'Environment') -> T.List[str]: - args: T.List[str] = [] if disable: return ['-DNDEBUG'] @@ -320,15 +319,15 @@ class ClangCPPCompiler(_StdCPPLibMixin, ClangCompiler, CPPCompiler): if self.defines.get(macro) is not None: return [] - # Clang supports both libstdc++ and libc++ - # TODO: Pipe through the C++ stdlib impl information to here so we can avoid pollution - args.append('-D_GLIBCXX_ASSERTIONS=1') - if version_compare(self.version, '>=18'): - args.append('-D_LIBCPP_HARDENING_MODE=_LIBCPP_HARDENING_MODE_FAST') - elif version_compare(self.version, '>=15'): - args.append('-D_LIBCPP_ENABLE_ASSERTIONS=1') + if self.language_stdlib_provider(env) == 'stdc++': + return ['-D_GLIBCXX_ASSERTIONS=1'] + else: + if version_compare(self.version, '>=18'): + return ['-D_LIBCPP_HARDENING_MODE=_LIBCPP_HARDENING_MODE_FAST'] + elif version_compare(self.version, '>=15'): + return ['-D_LIBCPP_ENABLE_ASSERTIONS=1'] - return args + return [] class ArmLtdClangCPPCompiler(ClangCPPCompiler): @@ -510,13 +509,19 @@ class GnuCPPCompiler(_StdCPPLibMixin, GnuCompiler, CPPCompiler): return ['-DNDEBUG'] # Don't inject the macro if the compiler already has it pre-defined. - if self.defines.get('_GLIBCXX_ASSERTIONS') is not None: - return [] + for macro in ['_GLIBCXX_ASSERTIONS', '_LIBCPP_HARDENING_MODE', '_LIBCPP_ENABLE_ASSERTIONS']: + if self.defines.get(macro) is not None: + return [] + + if self.language_stdlib_provider(env) == 'stdc++': + return ['-D_GLIBCXX_ASSERTIONS=1'] + else: + if version_compare(self.version, '>=18'): + return ['-D_LIBCPP_HARDENING_MODE=_LIBCPP_HARDENING_MODE_FAST'] + elif version_compare(self.version, '>=15'): + return ['-D_LIBCPP_ENABLE_ASSERTIONS=1'] - # XXX: This needs updating if/when GCC starts to support libc++. - # It currently only does so via an experimental configure arg. - # TODO: Pipe through the C++ stdlib impl information to here so we can avoid pollution - return ['-D_GLIBCXX_ASSERTIONS=1'] + return [] def get_pch_use_args(self, pch_dir: str, header: str) -> T.List[str]: return ['-fpch-preprocess', '-include', os.path.basename(header)] -- cgit v1.2.3