Skip to content

Commit

Permalink
Feature: set CMAKE_FIND_PACKAGE_PREFER_CONFIG=ON (#8599)
Browse files Browse the repository at this point in the history
* - set CMAKE_FIND_PACKAGE_PREFER_CONFIG=ON

Signed-off-by: SSE4 <tomskside@gmail.com>

* - add conf & test

Signed-off-by: SSE4 <tomskside@gmail.com>

* - remove some debug code

Signed-off-by: SSE4 <tomskside@gmail.com>

* - fix file name for config

Signed-off-by: SSE4 <tomskside@gmail.com>

* Update base.py

* Update base.py
  • Loading branch information
SSE4 committed Mar 23, 2021
1 parent 95f4aed commit 8fed505
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 0 deletions.
12 changes: 12 additions & 0 deletions conan/tools/cmake/base.py
Expand Up @@ -95,6 +95,9 @@ 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)
{%- if find_package_prefer_config %}
set(CMAKE_FIND_PACKAGE_PREFER_CONFIG {{ find_package_prefer_config }})
{%- endif %}
# To support the cmake_find_package generators
{% if cmake_module_path -%}
set(CMAKE_MODULE_PATH {{ cmake_module_path }} ${CMAKE_MODULE_PATH})
Expand Down Expand Up @@ -143,6 +146,8 @@ class CMakeToolchainBase(object):
""")

def __init__(self, conanfile, **kwargs):
import six

self._conanfile = conanfile
self.variables = Variables()
self.preprocessor_definitions = Variables()
Expand All @@ -153,6 +158,12 @@ 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:
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,
Expand All @@ -172,6 +183,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

Expand Down
56 changes: 56 additions & 0 deletions conans/test/functional/toolchains/cmake/test_cmake.py
Expand Up @@ -530,3 +530,59 @@ 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)
find_package(Comandante REQUIRED)
""")

find = textwrap.dedent("""
message(STATUS "using FindComandante.cmake")
""")

config = textwrap.dedent("""
message(STATUS "using ComandanteConfig.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,
"ComandanteConfig.cmake": config,
"profile": profile})

client.run("install . --profile profile")
client.run("build .")

if prefer_config:
self.assertIn("using ComandanteConfig.cmake", client.out)
else:
self.assertIn("using FindComandante.cmake", client.out)

0 comments on commit 8fed505

Please sign in to comment.