compilers: cpp: don't set stdlib assertion macros if already set

Followup to 90098473d5.

If the compiler already has one of these assertion macros [0] set, then don't
interfere.

e.g. a Linux distribution might be setting a stricter default than usual. This
is a pitfall many fell into with _FORTIFY_SOURCE and it's why AX_ADD_FORTIFY_SOURCE [1]
was contributed to autoconf-archive.

[0] _GLIBCXX_ASSERTIONS, _LIBCPP_HARDENING_MODE, or _LIBCPP_ENABLE_ASSERTIONS.
[1] https://www.gnu.org/software/autoconf-archive/ax_add_fortify_source.html

Bug: https://github.com/mesonbuild/meson/issues/12962
Signed-off-by: Sam James <sam@gentoo.org>
Signed-off-by: Eli Schwartz <eschwartz93@gmail.com>
pull/13014/head
Sam James 8 months ago committed by Eli Schwartz
parent 509a140529
commit 31314419aa
No known key found for this signature in database
GPG Key ID: CEB167EFB5722BD6
  1. 11
      mesonbuild/compilers/cpp.py

@ -312,7 +312,13 @@ class ClangCPPCompiler(_StdCPPLibMixin, ClangCompiler, CPPCompiler):
if disable:
return ['-DNDEBUG']
# Don't inject the macro if the compiler already has it pre-defined.
for macro in ['_GLIBCXX_ASSERTIONS', '_LIBCPP_HARDENING_MODE', '_LIBCPP_ENABLE_ASSERTIONS']:
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')
@ -500,8 +506,13 @@ class GnuCPPCompiler(_StdCPPLibMixin, GnuCompiler, CPPCompiler):
if disable:
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 []
# 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']
def get_pch_use_args(self, pch_dir: str, header: str) -> T.List[str]:

Loading…
Cancel
Save