From d38cb57125061ecf15e644120df10c452d36aff6 Mon Sep 17 00:00:00 2001 From: SSE4 Date: Thu, 4 Mar 2021 13:10:41 +0700 Subject: [PATCH 1/6] - set CMAKE_FIND_PACKAGE_PREFER_CONFIG=ON Signed-off-by: SSE4 --- conan/tools/cmake/base.py | 1 + 1 file changed, 1 insertion(+) diff --git a/conan/tools/cmake/base.py b/conan/tools/cmake/base.py index 61af6c79c91..6199bac8eeb 100644 --- a/conan/tools/cmake/base.py +++ b/conan/tools/cmake/base.py @@ -85,6 +85,7 @@ class CMakeToolchainBase(object): # We are going to adjust automagically many things as requested by Conan # these are the things done by 'conan_basic_setup()' set(CMAKE_EXPORT_NO_PACKAGE_REGISTRY ON) + set(CMAKE_FIND_PACKAGE_PREFER_CONFIG ON) # To support the cmake_find_package generators {% if cmake_module_path -%} set(CMAKE_MODULE_PATH {{ cmake_module_path }} ${CMAKE_MODULE_PATH}) From 47965cfceece43c9466380b400b9a1152fedc142 Mon Sep 17 00:00:00 2001 From: SSE4 Date: Wed, 17 Mar 2021 14:45:22 +0700 Subject: [PATCH 2/6] - add conf & test Signed-off-by: SSE4 --- conan/tools/cmake/base.py | 17 +++++- .../functional/toolchains/cmake/test_cmake.py | 57 +++++++++++++++++++ 2 files changed, 73 insertions(+), 1 deletion(-) diff --git a/conan/tools/cmake/base.py b/conan/tools/cmake/base.py index 6199bac8eeb..d513e6bab0a 100644 --- a/conan/tools/cmake/base.py +++ b/conan/tools/cmake/base.py @@ -85,7 +85,10 @@ class CMakeToolchainBase(object): # We are going to adjust automagically many things as requested by Conan # these are the things done by 'conan_basic_setup()' set(CMAKE_EXPORT_NO_PACKAGE_REGISTRY ON) - set(CMAKE_FIND_PACKAGE_PREFER_CONFIG ON) + {%- if find_package_prefer_config %} + set(CMAKE_FIND_PACKAGE_PREFER_CONFIG {{ find_package_prefer_config }}) + {%- endif %} + #set(CMAKE_FIND_PACKAGE_PREFER_CONFIG ON) # To support the cmake_find_package generators {% if cmake_module_path -%} set(CMAKE_MODULE_PATH {{ cmake_module_path }} ${CMAKE_MODULE_PATH}) @@ -134,6 +137,8 @@ class CMakeToolchainBase(object): """) def __init__(self, conanfile, **kwargs): + import six + self._conanfile = conanfile self.variables = Variables() self.preprocessor_definitions = Variables() @@ -144,6 +149,15 @@ def __init__(self, conanfile, **kwargs): self.build_type = None + self.find_package_prefer_config = \ + conanfile.conf["tools.cmake.cmaketoolchain"].find_package_prefer_config + if self.find_package_prefer_config is not None: + if isinstance(self.find_package_prefer_config, six.string_types): + self.find_package_prefer_config = self.find_package_prefer_config == "True" + elif isinstance(self.find_package_prefer_config, six.integer_types): + self.find_package_prefer_config = bool(self.find_package_prefer_config) + self.find_package_prefer_config = "ON" if self.find_package_prefer_config else "OFF" + def _get_templates(self): return { 'toolchain_macros': self._toolchain_macros_tpl, @@ -161,6 +175,7 @@ def _get_template_context_data(self): "cmake_prefix_path": self.cmake_prefix_path, "cmake_module_path": self.cmake_module_path, "build_type": self.build_type, + "find_package_prefer_config": self.find_package_prefer_config, } return ctxt_toolchain diff --git a/conans/test/functional/toolchains/cmake/test_cmake.py b/conans/test/functional/toolchains/cmake/test_cmake.py index 3d52429a500..1be6b0aaa93 100644 --- a/conans/test/functional/toolchains/cmake/test_cmake.py +++ b/conans/test/functional/toolchains/cmake/test_cmake.py @@ -516,3 +516,60 @@ def build(self): client.run("install .") client.run("build .") self.assertIn("VALUE OF CONFIG STRING: my new value", client.out) + + +@pytest.mark.toolchain +@pytest.mark.tool_cmake +class CMakeFindPackagePreferConfigTest(unittest.TestCase): + + @parameterized.expand([(True,), (False,)]) + def test_prefer_config(self, prefer_config): + conanfile = textwrap.dedent(""" + from conans import ConanFile + from conan.tools.cmake import CMake, CMakeToolchain + class App(ConanFile): + settings = "os", "arch", "compiler", "build_type" + exports_sources = "CMakeLists.txt" + def generate(self): + toolchain = CMakeToolchain(self) + toolchain.generate() + def build(self): + cmake = CMake(self) + cmake.configure() + """) + + cmakelist = textwrap.dedent(""" + cmake_minimum_required(VERSION 3.15) + project(my_project) + message(STATUS "CMAKE_FIND_PACKAGE_PREFER_CONFIG ${CMAKE_FIND_PACKAGE_PREFER_CONFIG}") + find_package(Comandante REQUIRED) + """) + + find = textwrap.dedent(""" + message(STATUS "using FindComandante.cmake") + """) + + config = textwrap.dedent(""" + message(STATUS "using Comandante-config.cmake") + """) + + profile = textwrap.dedent(""" + include(default) + [conf] + tools.cmake.cmaketoolchain:find_package_prefer_config={} + """).format(prefer_config) + + client = TestClient() + client.save({"conanfile.py": conanfile, + "CMakeLists.txt": cmakelist, + "FindComandante.cmake": find, + "Comandante-config.cmake": config, + "profile": profile}) + + client.run("install . --profile profile") + client.run("build .") + + if prefer_config: + self.assertIn("using Comandante-config.cmake", client.out) + else: + self.assertIn("using FindComandante.cmake", client.out) From e39d2a21b3ab78a065e99194f3ffc5cf2487c316 Mon Sep 17 00:00:00 2001 From: SSE4 Date: Wed, 17 Mar 2021 14:49:23 +0700 Subject: [PATCH 3/6] - remove some debug code Signed-off-by: SSE4 --- conan/tools/cmake/base.py | 1 - conans/test/functional/toolchains/cmake/test_cmake.py | 1 - 2 files changed, 2 deletions(-) diff --git a/conan/tools/cmake/base.py b/conan/tools/cmake/base.py index d513e6bab0a..fe783c4faa4 100644 --- a/conan/tools/cmake/base.py +++ b/conan/tools/cmake/base.py @@ -88,7 +88,6 @@ class CMakeToolchainBase(object): {%- if find_package_prefer_config %} set(CMAKE_FIND_PACKAGE_PREFER_CONFIG {{ find_package_prefer_config }}) {%- endif %} - #set(CMAKE_FIND_PACKAGE_PREFER_CONFIG ON) # To support the cmake_find_package generators {% if cmake_module_path -%} set(CMAKE_MODULE_PATH {{ cmake_module_path }} ${CMAKE_MODULE_PATH}) diff --git a/conans/test/functional/toolchains/cmake/test_cmake.py b/conans/test/functional/toolchains/cmake/test_cmake.py index 1be6b0aaa93..f4c454762b9 100644 --- a/conans/test/functional/toolchains/cmake/test_cmake.py +++ b/conans/test/functional/toolchains/cmake/test_cmake.py @@ -541,7 +541,6 @@ def build(self): cmakelist = textwrap.dedent(""" cmake_minimum_required(VERSION 3.15) project(my_project) - message(STATUS "CMAKE_FIND_PACKAGE_PREFER_CONFIG ${CMAKE_FIND_PACKAGE_PREFER_CONFIG}") find_package(Comandante REQUIRED) """) From fd15d4f9e666a9f5f2e5bc1333d2c051ea59b262 Mon Sep 17 00:00:00 2001 From: SSE4 Date: Wed, 17 Mar 2021 15:05:01 +0700 Subject: [PATCH 4/6] - fix file name for config Signed-off-by: SSE4 --- conans/test/functional/toolchains/cmake/test_cmake.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/conans/test/functional/toolchains/cmake/test_cmake.py b/conans/test/functional/toolchains/cmake/test_cmake.py index f4c454762b9..8f3fa69d7c6 100644 --- a/conans/test/functional/toolchains/cmake/test_cmake.py +++ b/conans/test/functional/toolchains/cmake/test_cmake.py @@ -549,7 +549,7 @@ def build(self): """) config = textwrap.dedent(""" - message(STATUS "using Comandante-config.cmake") + message(STATUS "using ComandanteConfig.cmake") """) profile = textwrap.dedent(""" @@ -562,13 +562,13 @@ def build(self): client.save({"conanfile.py": conanfile, "CMakeLists.txt": cmakelist, "FindComandante.cmake": find, - "Comandante-config.cmake": config, + "ComandanteConfig.cmake": config, "profile": profile}) client.run("install . --profile profile") client.run("build .") if prefer_config: - self.assertIn("using Comandante-config.cmake", client.out) + self.assertIn("using ComandanteConfig.cmake", client.out) else: self.assertIn("using FindComandante.cmake", client.out) From 53cc88b8a85d13c433a685452d37457cec1b45a8 Mon Sep 17 00:00:00 2001 From: SSE4 Date: Thu, 18 Mar 2021 18:23:53 +0700 Subject: [PATCH 5/6] Update base.py --- conan/tools/cmake/base.py | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/conan/tools/cmake/base.py b/conan/tools/cmake/base.py index fe783c4faa4..37842e77c1a 100644 --- a/conan/tools/cmake/base.py +++ b/conan/tools/cmake/base.py @@ -151,12 +151,7 @@ def __init__(self, conanfile, **kwargs): self.find_package_prefer_config = \ conanfile.conf["tools.cmake.cmaketoolchain"].find_package_prefer_config if self.find_package_prefer_config is not None: - if isinstance(self.find_package_prefer_config, six.string_types): - self.find_package_prefer_config = self.find_package_prefer_config == "True" - elif isinstance(self.find_package_prefer_config, six.integer_types): - self.find_package_prefer_config = bool(self.find_package_prefer_config) - self.find_package_prefer_config = "ON" if self.find_package_prefer_config else "OFF" - + self.find_package_prefer_config = "OFF" if self.find_package_prefer_config.lower() in ("false", "0", "off") else "ON" def _get_templates(self): return { 'toolchain_macros': self._toolchain_macros_tpl, From 6984540adccb2f2ea9d2d8f0707af866d4d0b213 Mon Sep 17 00:00:00 2001 From: SSE4 Date: Thu, 18 Mar 2021 20:49:24 +0700 Subject: [PATCH 6/6] Update base.py --- conan/tools/cmake/base.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/conan/tools/cmake/base.py b/conan/tools/cmake/base.py index 37842e77c1a..5e9473b2f88 100644 --- a/conan/tools/cmake/base.py +++ b/conan/tools/cmake/base.py @@ -152,6 +152,8 @@ def __init__(self, conanfile, **kwargs): conanfile.conf["tools.cmake.cmaketoolchain"].find_package_prefer_config if self.find_package_prefer_config is not None: self.find_package_prefer_config = "OFF" if self.find_package_prefer_config.lower() in ("false", "0", "off") else "ON" + else: + self.find_package_prefer_config = "ON" # assume ON by default if not specified in conf def _get_templates(self): return { 'toolchain_macros': self._toolchain_macros_tpl,