From c750d36598f33ef3ad52fe85c35564b4da27f8de Mon Sep 17 00:00:00 2001 From: "Klein, Thorsten (GDE-EDS9)" Date: Mon, 18 Sep 2023 23:02:17 +0200 Subject: [PATCH] move test to functional --- conan/tools/cmake/presets.py | 3 +- .../functional/toolchains/cmake/test_cmake.py | 88 +++++++++++++++++++ .../toolchains/cmake/test_cmake.py | 54 ------------ 3 files changed, 90 insertions(+), 55 deletions(-) diff --git a/conan/tools/cmake/presets.py b/conan/tools/cmake/presets.py index 72ef8af58b6..8bc763de2db 100644 --- a/conan/tools/cmake/presets.py +++ b/conan/tools/cmake/presets.py @@ -100,7 +100,8 @@ def _configure_preset(conanfile, generator, cache_variables, toolchain_file, mul if preset_prefix: name = f"{preset_prefix}-{name}" if not multiconfig and build_type: - cache_variables["CMAKE_BUILD_TYPE"] = build_type + if not "CMAKE_BUILD_TYPE" in cache_variables: + cache_variables["CMAKE_BUILD_TYPE"] = build_type ret = { "name": name, "displayName": "'{}' config".format(name), diff --git a/conans/test/functional/toolchains/cmake/test_cmake.py b/conans/test/functional/toolchains/cmake/test_cmake.py index 38f7319fd30..90be19af04c 100644 --- a/conans/test/functional/toolchains/cmake/test_cmake.py +++ b/conans/test/functional/toolchains/cmake/test_cmake.py @@ -733,3 +733,91 @@ def build(self): client.run("build . --profile=profile_false") assert "using FindComandante.cmake" in client.out + +@pytest.mark.tool("cmake") +class TestCMakeBuildType: + cmakelists = textwrap.dedent(""" + cmake_minimum_required(VERSION 3.15) + project(pkg C CXX) + message(STATUS "CMAKE_BUILD_TYPE: '${CMAKE_BUILD_TYPE}'") + """) + + def test_build_type_single_config_warn(self): + client = TestClient() + + conanfile = textwrap.dedent(""" + from conan import ConanFile + from conan.tools.cmake import CMake + class Pkg(ConanFile): + name = "pkg" + version = "0.1" + settings = "os", "compiler", "arch" + exports_sources = '*' + generators = "CMakeToolchain" + def build(self): + cmake = CMake(self) + cmake.configure() + cmake.build(build_type="Release") + """) + client.save({"conanfile.py": conanfile, "CMakeLists.txt":self.cmakelists}) + client.run("create . ") + assert "Specifying 'build_type' does not have " + \ + "any effect for single-config build systems" in client.out + assert "CMAKE_BUILD_TYPE: ''" in client.out + assert "Created package" in client.out + + + @pytest.mark.parametrize( + "setting_build_type", [True, False], + ) + def test_build_type_enforced(self, setting_build_type): + client = TestClient() + + conanfile = textwrap.dedent(""" + from conan import ConanFile + from conan.tools.cmake import CMake, CMakeToolchain + class Pkg(ConanFile): + name = "pkg" + version = "0.1" + if {}: + settings = "os", "compiler", "arch", "build_type" + else: + settings = "os", "compiler", "arch" + exports_sources = '*' + def generate(self): + tc = CMakeToolchain(self) + tc.cache_variables["CMAKE_BUILD_TYPE"] = "MinSizeRel" + tc.generate() + def build(self): + cmake = CMake(self) + cmake.configure() + cmake.build() + cmake.install() + """).format(setting_build_type) + client.save({"conanfile.py": conanfile, "CMakeLists.txt":self.cmakelists}) + client.run("create . -s build_type=Debug") + assert "CMAKE_BUILD_TYPE: 'MinSizeRel'" in client.out + assert 'Created package' in client.out + + def test_build_type_setting(self): + client = TestClient() + + conanfile = textwrap.dedent(""" + from conan import ConanFile + from conan.tools.cmake import CMake + class Pkg(ConanFile): + name = "pkg" + version = "0.1" + settings = "os", "compiler", "arch" + generators = "CMakeToolchain" + exports_sources = '*' + def build(self): + cmake = CMake(self) + cmake.configure() + cmake.build() + cmake.install() + """) + client.save({"conanfile.py": conanfile, "CMakeLists.txt":self.cmakelists}) + client.run("create . -s build_type=Debug") + assert "CMAKE_BUILD_TYPE: ''" in client.out + assert "Created package" in client.out diff --git a/conans/test/integration/toolchains/cmake/test_cmake.py b/conans/test/integration/toolchains/cmake/test_cmake.py index 5fa7c87ba03..86ae90d270d 100644 --- a/conans/test/integration/toolchains/cmake/test_cmake.py +++ b/conans/test/integration/toolchains/cmake/test_cmake.py @@ -31,57 +31,3 @@ def run(self, *args, **kwargs): assert "-something" in client.out assert "--testverbose" in client.out assert "-testok" in client.out - -def test_build_type_single_config_warn(): - client = TestClient() - - conanfile = textwrap.dedent(""" - from conan import ConanFile - from conan.tools.cmake import CMake - class Pkg(ConanFile): - name = "pkg" - version = "0.1" - settings = "os", "compiler", "arch" - generators = "CMakeToolchain" - def build(self): - cmake = CMake(self) - cmake.configure() - cmake.build(build_type="Release") - - def run(self, *args, **kwargs): - self.output.info("MYRUN: {}".format(*args)) - """) - client.save({"conanfile.py": conanfile}) - client.run("create . ") - assert "Specifying 'build_type' does not have " + \ - "any effect for single-config build systems" in client.out - assert "Created package" in client.out - -def test_build_type_empty(): - client = TestClient() - - conanfile = textwrap.dedent(""" - from conan import ConanFile - from conan.tools.cmake import CMake, CMakeToolchain - class Pkg(ConanFile): - name = "pkg" - version = "0.1" - settings = "os", "compiler", "arch" - - def generate(self): - tc = CMakeToolchain(self) - tc.cache_variables["CMAKE_BUILD_TYPE"] = "MinSizeRel" - tc.generate() - def build(self): - cmake = CMake(self) - cmake.configure() - cmake.build() - cmake.install() - - def run(self, *args, **kwargs): - self.output.info("MYRUN: {}".format(*args)) - """) - client.save({"conanfile.py": conanfile}) - client.run("create . -s build_type=Debug") - assert '-DCMAKE_BUILD_TYPE="MinSizeRel"' in client.out - assert 'Created package' in client.out