From ba8c97fca2f15705aabb47bb15f9639b7fba5deb Mon Sep 17 00:00:00 2001 From: Luis Caro Campos <3535649+jcar87@users.noreply.github.com> Date: Fri, 13 May 2022 10:09:57 +0100 Subject: [PATCH] Assume only config files are generated in CMakeDeps generator when none is specified (#11240) * Assume find mode is config when none is specified in CMakeDeps generator * Add test to cover case when transitive dependency doesnt provide a config mode --- conan/tools/cmake/utils.py | 4 +- .../test_cmakedeps_find_module_and_config.py | 41 +++++++++++-------- 2 files changed, 27 insertions(+), 18 deletions(-) diff --git a/conan/tools/cmake/utils.py b/conan/tools/cmake/utils.py index 27d5d578450..696535672c8 100644 --- a/conan/tools/cmake/utils.py +++ b/conan/tools/cmake/utils.py @@ -22,9 +22,9 @@ def get_file_name(conanfile, forced_module_mode=None): def get_find_mode(conanfile): """ :param conanfile: conanfile of the requirement - :return: "none" or "config" or "module" or "both" or None when not set + :return: "none" or "config" or "module" or "both" or "config" when not set """ tmp = conanfile.cpp_info.get_property("cmake_find_mode") if tmp is None: - return None + return "config" return tmp.lower() diff --git a/conans/test/functional/toolchains/cmake/cmakedeps/test_cmakedeps_find_module_and_config.py b/conans/test/functional/toolchains/cmake/cmakedeps/test_cmakedeps_find_module_and_config.py index a0cd6780fc9..46023343f46 100644 --- a/conans/test/functional/toolchains/cmake/cmakedeps/test_cmakedeps_find_module_and_config.py +++ b/conans/test/functional/toolchains/cmake/cmakedeps/test_cmakedeps_find_module_and_config.py @@ -113,8 +113,17 @@ def test_reuse_with_modules_and_config(client): client.run("build . -if=install") -@pytest.mark.parametrize("find_mode", ["both", "config", "module"]) -def test_transitive_modules_found(find_mode): +find_modes = [ + ("both", "both", ""), + ("config", "config", ""), + ("module", "module", ""), + ("both", None, ""), + ("both", None, "MODULE") +] + + +@pytest.mark.parametrize("find_mode_PKGA, find_mode_PKGB, find_mode_consumer", find_modes) +def test_transitive_modules_found(find_mode_PKGA, find_mode_PKGB, find_mode_consumer): """ related to https://github.com/conan-io/conan/issues/10224 modules files variables were set with the pkg_name_FOUND or pkg_name_VERSION @@ -127,7 +136,8 @@ def test_transitive_modules_found(find_mode): class Pkg(ConanFile): {requires} def package_info(self): - self.cpp_info.set_property("cmake_find_mode", "{mode}") + if "{mode}" != "None": + self.cpp_info.set_property("cmake_find_mode", "{mode}") self.cpp_info.set_property("cmake_file_name", "{filename}") self.cpp_info.defines.append("DEFINE_{filename}") """) @@ -149,19 +159,20 @@ def build(self): cmakelist = textwrap.dedent(""" cmake_minimum_required(VERSION 3.1) project(test_package CXX) - find_package(MYPKGB REQUIRED) - message("MYPKGB_VERSION: ${MYPKGB_VERSION}") - message("MYPKGB_VERSION_STRING: ${MYPKGB_VERSION_STRING}") - message("MYPKGB_INCLUDE_DIRS: ${MYPKGB_INCLUDE_DIRS}") - message("MYPKGB_INCLUDE_DIR: ${MYPKGB_INCLUDE_DIR}") - message("MYPKGB_LIBRARIES: ${MYPKGB_LIBRARIES}") - message("MYPKGB_DEFINITIONS: ${MYPKGB_DEFINITIONS}") + find_package(MYPKGB REQUIRED {find_mode}) + message("MYPKGB_VERSION: ${{MYPKGB_VERSION}}") + message("MYPKGB_VERSION_STRING: ${{MYPKGB_VERSION_STRING}}") + message("MYPKGB_INCLUDE_DIRS: ${{MYPKGB_INCLUDE_DIRS}}") + message("MYPKGB_INCLUDE_DIR: ${{MYPKGB_INCLUDE_DIR}}") + message("MYPKGB_LIBRARIES: ${{MYPKGB_LIBRARIES}}") + message("MYPKGB_DEFINITIONS: ${{MYPKGB_DEFINITIONS}}") """) - client.save({"pkgb.py": conan_pkg.format(requires='requires="pkga/1.0"', filename='MYPKGB', mode=find_mode), - "pkga.py": conan_pkg.format(requires='', filename='MYPKGA', mode=find_mode), + client.save({"pkgb.py": conan_pkg.format(requires='requires="pkga/1.0"', filename='MYPKGB', + mode=find_mode_PKGA), + "pkga.py": conan_pkg.format(requires='', filename='MYPKGA', mode=find_mode_PKGB), "consumer.py": consumer, - "CMakeLists.txt": cmakelist}) + "CMakeLists.txt": cmakelist.format(find_mode=find_mode_consumer)}) client.run("create pkga.py pkga/1.0@") client.run("create pkgb.py pkgb/1.0@") client.run("create consumer.py consumer/1.0@") @@ -173,7 +184,5 @@ def build(self): assert "MYPKGB_LIBRARIES: pkga::pkga" in client.out assert "MYPKGB_DEFINITIONS: -DDEFINE_MYPKGB" in client.out assert "Conan: Target declared 'pkga::pkga'" - if find_mode == "module": + if find_mode_PKGA == "module": assert 'Found MYPKGA: 1.0 (found version "1.0")' in client.out - -