Skip to content

Commit

Permalink
Default check False and introduced switch
Browse files Browse the repository at this point in the history
  • Loading branch information
lasote committed Jul 7, 2021
1 parent bba18e5 commit 468b68e
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 10 deletions.
3 changes: 3 additions & 0 deletions conan/tools/cmake/cmakedeps/cmakedeps.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ def __init__(self, conanfile):

check_using_build_profile(self._conanfile)

# Enable/Disable checking if a component target exists or not
self.check_components_exist = False

def generate(self):
# FIXME: Remove this in 2.0
if not hasattr(self._conanfile, "settings_build") and \
Expand Down
6 changes: 4 additions & 2 deletions conan/tools/cmake/cmakedeps/templates/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ def context(self):
return {"file_name": self.file_name,
"pkg_name": self.pkg_name,
"config_suffix": self.config_suffix,
"target_namespace": self.target_namespace}
"target_namespace": self.target_namespace,
"check_components_exist": self.cmakedeps.check_components_exist}

@property
def template(self):
Expand Down Expand Up @@ -52,6 +53,7 @@ def template(self):
include({{ '${_BUILD_MODULE}' }})
endforeach()
{% if check_components_exist %}
# Check that the specified components in the find_package(Foo COMPONENTS x y z) are there
# This is the variable filled by CMake with the requested components in find_package
if({{ target_namespace }}_FIND_COMPONENTS)
Expand All @@ -63,5 +65,5 @@ def template(self):
endif()
endforeach()
endif()
{% endif %}
""")
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,7 @@
import pytest

from conans.model.ref import ConanFileReference, PackageReference
from conans.test.assets.cmake import gen_cmakelists
from conans.test.assets.genconanfile import GenConanfile
from conans.test.assets.sources import gen_function_cpp
from conans.test.utils.tools import TestClient, NO_SETTINGS_PACKAGE_ID


Expand Down Expand Up @@ -271,7 +269,8 @@ def test_multi_generator_macos(self):
t.run_command('cmake --build . --config Release') # Compiles and links.


def test_targets_declared_in_build_modules():
@pytest.mark.parametrize("check_components_exist", [False, True, None])
def test_targets_declared_in_build_modules(check_components_exist):
"""If a require is declaring the component targets in a build_module, CMakeDeps is
fine with it, not needed to locate it as a conan declared component"""

Expand All @@ -296,8 +295,36 @@ def package_info(self):
"my_modules.cmake": my_modules, "hello.h": hello_h})
client.run("create .")

conanfile = GenConanfile().with_cmake_build().with_requires("hello/1.0")\
.with_exports_sources("*.txt", "*.cpp").with_name("app").with_version("1.0")
conanfile = textwrap.dedent("""
from conans import ConanFile
from conan.tools.cmake import CMake, CMakeDeps
class HelloConan(ConanFile):
name = 'app'
version = '1.0'
exports_sources = "*.txt", "*.cpp"
generators = "CMakeToolchain"
requires = ("hello/1.0", )
settings = "os", "compiler", "arch", "build_type"
def generate(self):
deps = CMakeDeps(self)
{}
deps.generate()
def build(self):
cmake = CMake(self)
cmake.configure()
cmake.build()
""")
if check_components_exist is False:
conanfile = conanfile.format("deps.check_components_exist=False")
elif check_components_exist is True:
conanfile = conanfile.format("deps.check_components_exist=True")
else:
conanfile = conanfile.format("")

main_cpp = textwrap.dedent("""
#include <iostream>
#include "hello.h"
Expand All @@ -316,13 +343,17 @@ def package_info(self):
cmake_minimum_required(VERSION 3.15)
project(project CXX)
find_package(hello COMPONENTS invented)
find_package(hello COMPONENTS invented missing)
add_executable(myapp main.cpp)
target_link_libraries(myapp hello::invented)
""")
client.save({"conanfile.py": conanfile,
"CMakeLists.txt": cmakelist, "main.cpp": main_cpp})
client.run("create .")
client.run("create .", assert_error=check_components_exist)
assert bool(check_components_exist) == ("Conan: Component 'missing' NOT found in package "
"'hello'" in client.out)

assert "Conan: Including build module" in client.out
assert "my_modules.cmake" in client.out
assert "Conan: Component 'invented' found in package 'hello'" in client.out
assert bool(check_components_exist) == ("Conan: Component 'invented' found in package 'hello'"
in client.out)

0 comments on commit 468b68e

Please sign in to comment.