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

Fix module CMakeDeps vars and call find_dependency for transitive modules #10227

Merged
merged 10 commits into from
Dec 28, 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
23 changes: 17 additions & 6 deletions conan/tools/cmake/cmakedeps/templates/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,25 +47,36 @@ def template(self):

{% if is_module %}
include(FindPackageHandleStandardArgs)
set({{ pkg_name }}_FOUND 1)
set({{ pkg_name }}_VERSION "{{ version }}")
set({{ file_name }}_FOUND 1)
set({{ file_name }}_VERSION "{{ version }}")

find_package_handle_standard_args({{ pkg_name }}
REQUIRED_VARS {{ pkg_name }}_VERSION
VERSION_VAR {{ pkg_name }}_VERSION)
mark_as_advanced({{ pkg_name }}_FOUND {{ pkg_name }}_VERSION)
find_package_handle_standard_args({{ file_name }}
REQUIRED_VARS {{ file_name }}_VERSION
VERSION_VAR {{ file_name }}_VERSION)
mark_as_advanced({{ file_name }}_FOUND {{ file_name }}_VERSION)
{% endif %}

include(${CMAKE_CURRENT_LIST_DIR}/cmakedeps_macros.cmake)
include(${CMAKE_CURRENT_LIST_DIR}/{{ targets_include_file }})
include(CMakeFindDependencyMacro)

foreach(_DEPENDENCY {{ '${' + pkg_name + '_FIND_DEPENDENCY_NAMES' + '}' }} )
# Check that we have not already called a find_package with the transitive dependency
if(NOT {{ '${_DEPENDENCY}' }}_FOUND)
{% if is_module %}
find_dependency({{ '${_DEPENDENCY}' }} REQUIRED MODULE)
{% else %}
find_dependency({{ '${_DEPENDENCY}' }} REQUIRED NO_MODULE)
{% endif %}
endif()
endforeach()

set({{ file_name }}_VERSION_STRING "{{ version }}")
set({{ file_name }}_INCLUDE_DIRS {{ '${' + pkg_name + '_INCLUDE_DIRS' + config_suffix + '}' }} )
set({{ file_name }}_INCLUDE_DIR {{ '${' + pkg_name + '_INCLUDE_DIRS' + config_suffix + '}' }} )
set({{ file_name }}_LIBRARIES {{ '${' + pkg_name + '_LIBRARIES' + config_suffix + '}' }} )
set({{ file_name }}_DEFINITIONS {{ '${' + pkg_name + '_DEFINITIONS' + config_suffix + '}' }} )

# Only the first installed configuration is included to avoid the collision
foreach(_BUILD_MODULE {{ '${' + pkg_name + '_BUILD_MODULES_PATHS' + config_suffix + '}' }} )
conan_message(STATUS "Conan: Including build module from '${_BUILD_MODULE}'")
Expand Down
1 change: 1 addition & 0 deletions conan/tools/cmake/cmakedeps/templates/target_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ def context(self):
.replace('$', '\\$').replace('"', '\\"')
return {"global_cpp": global_cpp,
"pkg_name": self.pkg_name,
"file_name": self.file_name,
"package_folder": package_folder,
"config_suffix": self.config_suffix,
"components_names": components_names,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,3 +111,69 @@ def test_reuse_with_modules_and_config(client):

client.run("install . -if=install")
client.run("build . -if=install")


@pytest.mark.parametrize("find_mode", ["both", "config", "module"])
def test_transitive_modules_found(find_mode):
"""
related to https://github.com/conan-io/conan/issues/10224
modules files variables were set with the pkg_name_FOUND or pkg_name_VERSION
instead of using filename_*, also there was missing doing a find_dependency of the
requires packages to find_package transitive dependencies
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice test with clear explanation 👍

"""
client = TestClient()
conan_pkg = textwrap.dedent("""
from conan import ConanFile
class Pkg(ConanFile):
{requires}
def package_info(self):
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}")
""")

consumer = textwrap.dedent("""
from conan import ConanFile
from conan.tools.cmake import CMake
class Consumer(ConanFile):
settings = "os", "compiler", "arch", "build_type"
requires = "pkgb/1.0"
generators = "CMakeDeps", "CMakeToolchain"
exports_sources = "CMakeLists.txt"
def build(self):
cmake = CMake(self)
cmake.configure()
cmake.build()
""")

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}")
""")

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),
"consumer.py": consumer,
"CMakeLists.txt": cmakelist})
client.run("create pkga.py pkga/1.0@")
client.run("create pkgb.py pkgb/1.0@")
client.run("create consumer.py consumer/1.0@")

assert "MYPKGB_VERSION: 1.0" in client.out
assert "MYPKGB_VERSION_STRING: 1.0" in client.out
assert "MYPKGB_INCLUDE_DIRS:" in client.out
assert "MYPKGB_INCLUDE_DIR:" in client.out
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":
assert 'Found MYPKGA: 1.0 (found version "1.0")' in client.out