From 1437bc11fd7bd5264973d262130a4cc1952a7ce1 Mon Sep 17 00:00:00 2001 From: Francisco Ramirez de Anton Date: Fri, 17 Dec 2021 16:41:54 +0100 Subject: [PATCH 1/2] Fixing bug when msvc compiler is selected (legacy cmake generator) --- conans/client/build/cmake_flags.py | 27 ++++++++++++------- .../client/build/test_cmake_flags.py | 25 +++++++++-------- 2 files changed, 32 insertions(+), 20 deletions(-) diff --git a/conans/client/build/cmake_flags.py b/conans/client/build/cmake_flags.py index fbc937108f2..179772f2d4c 100644 --- a/conans/client/build/cmake_flags.py +++ b/conans/client/build/cmake_flags.py @@ -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 @@ -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 diff --git a/conans/test/unittests/client/build/test_cmake_flags.py b/conans/test/unittests/client/build/test_cmake_flags.py index 47dce0145cd..7251733b48b 100644 --- a/conans/test/unittests/client/build/test_cmake_flags.py +++ b/conans/test/unittests/client/build/test_cmake_flags.py @@ -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 From 7f2390058eb04deda2f77b9ee0bdf7b970c3992c Mon Sep 17 00:00:00 2001 From: Francisco Ramirez de Anton Date: Fri, 17 Dec 2021 18:08:32 +0100 Subject: [PATCH 2/2] Added one functional test --- .../test/functional/generators/cmake_test.py | 36 +++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/conans/test/functional/generators/cmake_test.py b/conans/test/functional/generators/cmake_test.py index b15e7342d6a..cd3cfaa9d7f 100644 --- a/conans/test/functional/generators/cmake_test.py +++ b/conans/test/functional/generators/cmake_test.py @@ -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()