Skip to content

Commit

Permalink
fixing definition of CXX11_ABI for gcc-like (#10165)
Browse files Browse the repository at this point in the history
* fixing definition of CXX11_ABI for gcc-like

* fix test

* added conf and tests
  • Loading branch information
memsharded authored Dec 14, 2021
1 parent ab4b12a commit d0e2897
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 10 deletions.
10 changes: 5 additions & 5 deletions conan/tools/cmake/toolchain.py
Original file line number Diff line number Diff line change
Expand Up @@ -187,12 +187,12 @@ def context(self):
}.get(libcxx)
if lib:
lib = "-library={}".format(lib)
elif compiler == "gcc":
# we might want to remove this "1", it is the default in most distros
if libcxx == "libstdc++11":
glib = "1"
elif libcxx == "libstdc++":

if compiler in ['clang', 'apple-clang', 'gcc']:
if libcxx == "libstdc++":
glib = "0"
elif libcxx == "libstdc++11" and self._conanfile.conf["tools.gnu:define_libcxx11_abi"]:
glib = "1"
return {"set_libcxx": lib, "glibcxx": glib}


Expand Down
4 changes: 3 additions & 1 deletion conan/tools/gnu/autotoolstoolchain.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,11 @@ def _cxx11_abi_define(self):
return

compiler = settings.get_safe("compiler.base") or settings.get_safe("compiler")
if compiler == "gcc":
if compiler in ['clang', 'apple-clang', 'gcc']:
if libcxx == 'libstdc++':
return '_GLIBCXX_USE_CXX11_ABI=0'
elif libcxx == "libstdc++11" and self._conanfile.conf["tools.gnu:define_libcxx11_abi"]:
return '_GLIBCXX_USE_CXX11_ABI=1'

def _libcxx(self):
settings = self._conanfile.settings
Expand Down
1 change: 1 addition & 0 deletions conans/model/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
"tools.files.download:retry": "Number of retries in case of failure when downloading",
"tools.files.download:retry_wait": "Seconds to wait between download attempts",
"tools.gnu:make_program": "Indicate path to make program",
"tools.gnu:define_libcxx11_abi": "Force definition of GLIBCXX_USE_CXX11_ABI=1 for libstdc++11",
"tools.google.bazel:config": "Define Bazel config file",
"tools.google.bazel:bazelrc_path": "Defines Bazel rc-path",
"tools.microsoft.msbuild:verbosity": "Verbosity level for MSBuild: "
Expand Down
4 changes: 2 additions & 2 deletions conans/test/functional/toolchains/cmake/test_cmake.py
Original file line number Diff line number Diff line change
Expand Up @@ -352,8 +352,8 @@ def test_toolchain_linux(self, build_type, cppstd, arch, libcxx, shared):

extensions_str = "ON" if "gnu" in cppstd else "OFF"
arch_str = "-m32" if arch == "x86" else "-m64"
cxx11_abi_str = "1" if libcxx == "libstdc++11" else "0"
defines = '_GLIBCXX_USE_CXX11_ABI=%s;MYDEFINE="MYDEF_VALUE";MYDEFINEINT=42;'\
cxx11_abi_str = "_GLIBCXX_USE_CXX11_ABI=0;" if libcxx == "libstdc++" else ""
defines = '%sMYDEFINE="MYDEF_VALUE";MYDEFINEINT=42;'\
'MYDEFINE_CONFIG=$<IF:$<CONFIG:debug>,"MYDEF_DEBUG",$<IF:$<CONFIG:release>,'\
'"MYDEF_RELEASE","">>;MYDEFINEINT_CONFIG=$<IF:$<CONFIG:debug>,421,'\
'$<IF:$<CONFIG:release>,422,"">>' % cxx11_abi_str
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -203,12 +203,23 @@ def test_cxx11_abi_define():
be = AutotoolsToolchain(conanfile)
env = be.vars()
assert be.gcc_cxx11_abi is None
assert "-D_GLIBCXX_USE_CXX11_ABI=0" not in env["CPPFLAGS"]
assert "GLIBCXX_USE_CXX11_ABI" not in env["CPPFLAGS"]

# Force the GLIBCXX_USE_CXX11_ABI=1 for old distros is direct def f ``gcc_cxx11_abi``
be.gcc_cxx11_abi = "_GLIBCXX_USE_CXX11_ABI=1"
env = be.vars()
assert "-D_GLIBCXX_USE_CXX11_ABI=1" in env["CPPFLAGS"]

# Also conf is possible
conanfile.conf["tools.gnu:define_libcxx11_abi"] = True
be = AutotoolsToolchain(conanfile)
env = be.vars()
assert "-D_GLIBCXX_USE_CXX11_ABI=1" in env["CPPFLAGS"]


@pytest.mark.parametrize("config", [
('x86_64', "-m64"),
('x86', "-m32"),])
('x86', "-m32")])
def test_architecture_flag(config):
"""Architecture flag is set in CXXFLAGS, CFLAGS and LDFLAGS"""
arch, expected = config
Expand Down
37 changes: 37 additions & 0 deletions conans/test/unittests/tools/cmake/test_cmaketoolchain.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from conan.tools.cmake import CMakeToolchain
from conan.tools.cmake.toolchain import Block, GenericSystemBlock
from conans import ConanFile, Settings
from conans.client.conf import get_default_settings_yml
from conans.model.conf import Conf
from conans.model.env_info import EnvValues

Expand Down Expand Up @@ -331,3 +332,39 @@ def test_fpic_enabled(conanfile_linux_fpic):
toolchain = CMakeToolchain(conanfile_linux_fpic)
content = toolchain.content
assert 'set(CMAKE_POSITION_INDEPENDENT_CODE ON' in content


def test_libcxx_abi_flag():
c = ConanFile(Mock(), None)
c.settings = "os", "compiler", "build_type", "arch"
c.initialize(Settings.loads(get_default_settings_yml()), EnvValues())
c.settings.build_type = "Release"
c.settings.arch = "x86_64"
c.settings.compiler = "gcc"
c.settings.compiler.version = "11"
c.settings.compiler.cppstd = "20"
c.settings.compiler.libcxx = "libstdc++"
c.settings.os = "Linux"
c.conf = Conf()
c.folders.set_base_generators(".")
c._conan_node = Mock()
c._conan_node.dependencies = []

toolchain = CMakeToolchain(c)
content = toolchain.content
assert '-D_GLIBCXX_USE_CXX11_ABI=0' in content
c.settings.compiler.libcxx = "libstdc++11"
toolchain = CMakeToolchain(c)
content = toolchain.content
# by default, no flag is output anymore, it is assumed the compiler default
assert 'GLIBCXX_USE_CXX11_ABI' not in content
# recipe workaround for older distros
toolchain.blocks["libcxx"].values["glibcxx"] = "1"
content = toolchain.content
assert '-D_GLIBCXX_USE_CXX11_ABI=1' in content

# but maybe the conf is better
c.conf["tools.gnu:define_libcxx11_abi"] = True
toolchain = CMakeToolchain(c)
content = toolchain.content
assert '-D_GLIBCXX_USE_CXX11_ABI=1' in content

0 comments on commit d0e2897

Please sign in to comment.