Skip to content

Commit

Permalink
msvc runtime to AutotoolsToolchain (#10755)
Browse files Browse the repository at this point in the history
* msvc runtime to AutotoolsToolchain

* Support Visual

* Fix toolchain
  • Loading branch information
lasote committed Mar 11, 2022
1 parent 5550df9 commit 8311b40
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 1 deletion.
26 changes: 25 additions & 1 deletion conan/tools/gnu/autotoolstoolchain.py
Expand Up @@ -48,8 +48,10 @@ def __init__(self, conanfile, namespace=None):
self._target = None

self.apple_arch_flag = self.apple_isysroot_flag = None

self.apple_min_version_flag = apple_min_version_flag(self._conanfile)

self.msvc_runtime_flag = self._get_msvc_runtime_flag()

if cross_building(self._conanfile):
os_build, arch_build, os_host, arch_host = get_cross_building_settings(self._conanfile)
compiler = self._conanfile.settings.get_safe("compiler")
Expand All @@ -67,6 +69,24 @@ def __init__(self, conanfile, namespace=None):

check_using_build_profile(self._conanfile)

def _get_msvc_runtime_flag(self):
msvc_runtime_flag = None
if self._conanfile.settings.get_safe("compiler") == "msvc":
runtime_type = self._conanfile.settings.get_safe("compiler.runtime_type")
if runtime_type == "Release":
values = {"static": "MT", "dynamic": "MD"}
else:
values = {"static": "MTd", "dynamic": "MDd"}
runtime = values.get(self._conanfile.settings.get_safe("compiler.runtime"))
if runtime:
msvc_runtime_flag = "-{}".format(runtime)
elif self._conanfile.settings.get_safe("compiler") == "Visual Studio":
runtime = self._conanfile.settings.get_safe("compiler.runtime")
if runtime:
msvc_runtime_flag = "-{}".format(runtime)

return msvc_runtime_flag

def _cxx11_abi_define(self):
# https://gcc.gnu.org/onlinedocs/libstdc++/manual/using_dual_abi.html
# The default is libstdc++11, only specify the contrary '_GLIBCXX_USE_CXX11_ABI=0'
Expand Down Expand Up @@ -134,6 +154,10 @@ def environment(self):
self.cxxflags.append("-fPIC")
self.cflags.append("-fPIC")

if self.msvc_runtime_flag:
self.cxxflags.append(self.msvc_runtime_flag)
self.cflags.append(self.msvc_runtime_flag)

if is_msvc(self._conanfile):
env.define("CXX", "cl")
env.define("CC", "cl")
Expand Down
49 changes: 49 additions & 0 deletions conans/test/unittests/tools/gnu/autotoolschain_test.py
Expand Up @@ -24,6 +24,55 @@ def test_get_gnu_triplet_for_cross_building():
assert autotoolschain._build == "i686-solaris"


@pytest.mark.parametrize("runtime, runtime_type, expected",
[("static", "Debug", "MTd"),
("static", "Release", "MT"),
("dynamic", "Debug", "MDd"),
("dynamic", "Release", "MD")])
def test_msvc_runtime(runtime, runtime_type, expected):
"""
Testing AutotoolsToolchain with the msvc compiler adjust the runtime
"""
# Issue: https://github.com/conan-io/conan/issues/10139
settings = MockSettings({"build_type": "Release",
"compiler": "msvc",
"compiler.runtime": runtime,
"compiler.runtime_type": runtime_type,
"os": "Windows",
"arch": "x86_64"})
conanfile = ConanFileMock()
conanfile.settings = settings
conanfile.settings_build = settings
autotoolschain = AutotoolsToolchain(conanfile)
expected_flag = "-{}".format(expected)
assert autotoolschain.msvc_runtime_flag == expected_flag
env = autotoolschain.environment().vars(conanfile)
assert expected_flag in env["CFLAGS"]
assert expected_flag in env["CXXFLAGS"]


@pytest.mark.parametrize("runtime", ["MTd", "MT", "MDd", "MD"])
def test_visual_runtime(runtime):
"""
Testing AutotoolsToolchain with the msvc compiler adjust the runtime
"""
# Issue: https://github.com/conan-io/conan/issues/10139
settings = MockSettings({"build_type": "Release",
"compiler": "Visual Studio",
"compiler.runtime": runtime,
"os": "Windows",
"arch": "x86_64"})
conanfile = ConanFileMock()
conanfile.settings = settings
conanfile.settings_build = settings
autotoolschain = AutotoolsToolchain(conanfile)
expected_flag = "-{}".format(runtime)
assert autotoolschain.msvc_runtime_flag == expected_flag
env = autotoolschain.environment().vars(conanfile)
assert expected_flag in env["CFLAGS"]
assert expected_flag in env["CXXFLAGS"]


def test_get_gnu_triplet_for_cross_building_raise_error():
"""
Testing AutotoolsToolchain and _get_gnu_triplet() function raises an error in case of
Expand Down

0 comments on commit 8311b40

Please sign in to comment.