Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

small refactor to prefer config #8694

Merged
merged 1 commit into from Mar 24, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
13 changes: 5 additions & 8 deletions conan/tools/cmake/base.py
Expand Up @@ -146,8 +146,6 @@ class CMakeToolchainBase(object):
""")

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

self._conanfile = conanfile
self.variables = Variables()
self.preprocessor_definitions = Variables()
Expand All @@ -158,12 +156,11 @@ 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
self.find_package_prefer_config = "ON" # assume ON by default if not specified in conf
prefer_config = conanfile.conf["tools.cmake.cmaketoolchain"].find_package_prefer_config
if prefer_config is not None and prefer_config.lower() in ("false", "0", "off"):
self.find_package_prefer_config = "OFF"

def _get_templates(self):
return {
'toolchain_macros': self._toolchain_macros_tpl,
Expand Down
48 changes: 23 additions & 25 deletions conans/test/functional/toolchains/cmake/test_cmake.py
Expand Up @@ -534,55 +534,53 @@ def build(self):

@pytest.mark.toolchain
@pytest.mark.tool_cmake
class CMakeFindPackagePreferConfigTest(unittest.TestCase):
class TestCMakeFindPackagePreferConfig:

@parameterized.expand([(True,), (False,)])
def test_prefer_config(self, prefer_config):
def test_prefer_config(self):
conanfile = textwrap.dedent("""
from conans import ConanFile
from conan.tools.cmake import CMake, CMakeToolchain
from conan.tools.cmake import CMake
class App(ConanFile):
settings = "os", "arch", "compiler", "build_type"
exports_sources = "CMakeLists.txt"
def generate(self):
toolchain = CMakeToolchain(self)
toolchain.generate()
generators = "CMakeToolchain"
def build(self):
cmake = CMake(self)
cmake.configure()
""")
""")

cmakelist = textwrap.dedent("""
set(CMAKE_C_COMPILER_WORKS 1)
set(CMAKE_C_ABI_COMPILED 1)
cmake_minimum_required(VERSION 3.15)
project(my_project)
project(my_project C)
find_package(Comandante REQUIRED)
""")

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

config = textwrap.dedent("""
message(STATUS "using ComandanteConfig.cmake")
""")
find = 'message(STATUS "using FindComandante.cmake")'
config = '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})
"profile_true": profile.format(True),
"profile_false": profile.format(False)})

client.run("install . --profile profile")
client.run("install .")
client.run("build .")
assert "using ComandanteConfig.cmake" in client.out

if prefer_config:
self.assertIn("using ComandanteConfig.cmake", client.out)
else:
self.assertIn("using FindComandante.cmake", client.out)
client.run("install . --profile=profile_true")
client.run("build .")
assert "using ComandanteConfig.cmake" in client.out

client.run("install . --profile=profile_false")
client.run("build .")
assert "using FindComandante.cmake" in client.out