From 3e1e37f563916221d2b090df41f684ab37e843bb Mon Sep 17 00:00:00 2001 From: Sam James Date: Wed, 13 Mar 2024 01:14:30 +0000 Subject: [PATCH] compilers: cpp: factor out C++ stdlib detection We're going to use it in some more places in a minute (for controlling assertions). Bug: https://github.com/mesonbuild/meson/issues/12962 Signed-off-by: Sam James Signed-off-by: Eli Schwartz --- mesonbuild/compilers/cpp.py | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/mesonbuild/compilers/cpp.py b/mesonbuild/compilers/cpp.py index c26def9fc..449a6888b 100644 --- a/mesonbuild/compilers/cpp.py +++ b/mesonbuild/compilers/cpp.py @@ -183,6 +183,13 @@ class _StdCPPLibMixin(CompilerMixinBase): """Detect whether to use libc++ or libstdc++.""" + def language_stdlib_provider(self, env: Environment) -> str: + # https://stackoverflow.com/a/31658120 + header = 'version' if self.has_header('', '', env) else 'ciso646' + is_libcxx = self.has_header_symbol(header, '_LIBCPP_VERSION', '', env)[0] + lib = 'c++' if is_libcxx else 'stdc++' + return lib + @functools.lru_cache(None) def language_stdlib_only_link_flags(self, env: Environment) -> T.List[str]: """Detect the C++ stdlib and default search dirs @@ -202,11 +209,7 @@ class _StdCPPLibMixin(CompilerMixinBase): machine = env.machines[self.for_machine] assert machine is not None, 'for mypy' - # https://stackoverflow.com/a/31658120 - header = 'version' if self.has_header('', '', env) else 'ciso646' - is_libcxx = self.has_header_symbol(header, '_LIBCPP_VERSION', '', env)[0] - lib = 'c++' if is_libcxx else 'stdc++' - + lib = self.language_stdlib_provider(env) if self.find_library(lib, env, []) is not None: return search_dirs + [f'-l{lib}']