Skip to content

Commit

Permalink
[bugfix] msvc in legacy cmake generators (#10195)
Browse files Browse the repository at this point in the history
* Fixing bug when msvc compiler is selected (legacy cmake generator)

* Added one functional test
  • Loading branch information
franramirez688 authored Dec 20, 2021
1 parent 7dd55c0 commit b8cbf28
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 20 deletions.
27 changes: 18 additions & 9 deletions conans/client/build/cmake_flags.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import platform
from collections import OrderedDict

from conan.tools.microsoft.visual import vs_ide_version
from conans.client import tools
from conans.client.build.compiler_flags import architecture_flag, parallel_compiler_cl_flag
from conans.client.build.cppstd_flags import cppstd_from_settings, cppstd_flag_new as cppstd_flag
Expand Down Expand Up @@ -53,18 +54,26 @@ def get_generator(conanfile):
return None
return "Unix Makefiles"

cmake_years = {'8': '8 2005',
'9': '9 2008',
'10': '10 2010',
'11': '11 2012',
'12': '12 2013',
'14': '14 2015',
'15': '15 2017',
'16': '16 2019',
'17': '17 2022'}

if compiler == "msvc":
if compiler_version is None:
raise ConanException("compiler.version must be defined")
vs_version = vs_ide_version(conanfile)
return "Visual Studio %s" % cmake_years[vs_version]

if compiler == "Visual Studio" or compiler_base == "Visual Studio":
version = compiler_base_version or compiler_version
major_version = version.split('.', 1)[0]
_visuals = {'8': '8 2005',
'9': '9 2008',
'10': '10 2010',
'11': '11 2012',
'12': '12 2013',
'14': '14 2015',
'15': '15 2017',
'16': '16 2019',
'17': '17 2022'}.get(major_version, "UnknownVersion %s" % version)
_visuals = cmake_years.get(major_version, "UnknownVersion %s" % version)
base = "Visual Studio %s" % _visuals
return base

Expand Down
36 changes: 36 additions & 0 deletions conans/test/functional/generators/cmake_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,42 @@ def build(self):
client.run("create . lib/1.0@ -s compiler='Visual Studio' -s compiler.toolset=v140")
self.assertIn("Conan: Skipping compiler check: Declared 'compiler.toolset'", client.out)

@pytest.mark.slow
@pytest.mark.tool_visual_studio(version="17")
@pytest.mark.skipif(platform.system() != "Windows", reason="Requires Visual Studio")
def test_check_msvc_compiler(self):
"""
Checking if MSVC 19.X compiler is being called via CMake
while using compiler=msvc in Conan profile.
Issue related: https://github.com/conan-io/conan/issues/10185
"""
file_content = textwrap.dedent("""
from conans import ConanFile, CMake
class ConanFileMSVCTest(ConanFile):
generators = "cmake"
exports_sources = "CMakeLists.txt"
settings = "os", "arch", "compiler"
def build(self):
cmake = CMake(self)
cmake.configure()
cmake.build()
""")
client = TestClient()
cmakelists = textwrap.dedent("""
PROJECT(Hello)
cmake_minimum_required(VERSION 2.8)
include("${CMAKE_BINARY_DIR}/conanbuildinfo.cmake")
CONAN_BASIC_SETUP()
""")
client.save({"conanfile.py": file_content,
"CMakeLists.txt": cmakelists})
client.run("create . lib/1.0@ -s compiler=msvc -s compiler.version=193")
self.assertIn("-- The C compiler identification is MSVC 19.3", client.out)
self.assertIn("-- The CXX compiler identification is MSVC 19.3", client.out)

@pytest.mark.slow
def test_no_output(self):
client = TestClient()
Expand Down
25 changes: 14 additions & 11 deletions conans/test/unittests/client/build/test_cmake_flags.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
import pytest

from conans.client.build.cmake_flags import get_generator
from conans.test.utils.mocks import ConanFileMock, MockSettings


class TestGetGenerator(object):

def test_vs_generator(self):
settings = MockSettings({"os": "Windows", "arch": "x86_64", "compiler": "Visual Studio"})
conanfile = ConanFileMock()
conanfile.settings = settings

settings.values['compiler.version'] = '15'
assert get_generator(conanfile) == 'Visual Studio 15 2017'
@pytest.mark.parametrize("compiler,version,expected", [
("Visual Studio", "15", "Visual Studio 15 2017"),
("Visual Studio", "15.9", "Visual Studio 15 2017"),
("msvc", "193", "Visual Studio 17 2022"),
("msvc", "192", "Visual Studio 16 2019")
])
def test_vs_generator(compiler, version, expected):
settings = MockSettings({"os": "Windows", "arch": "x86_64", "compiler": compiler})
conanfile = ConanFileMock()
conanfile.settings = settings

settings.values['compiler.version'] = '15.9'
assert get_generator(conanfile) == 'Visual Studio 15 2017'
settings.values['compiler.version'] = version
assert get_generator(conanfile) == expected

0 comments on commit b8cbf28

Please sign in to comment.