Skip to content

Commit

Permalink
Internal refactor for readability of CMakeDeps (#16040)
Browse files Browse the repository at this point in the history
* cmake deps internal refactor for readability

* fix

* fix

* fix

* fix

* fix

* copy only if different (#16031)

* allow conf in exports-sources and export (#16034)

* refactor apple_min_version_flag() (#16017)

* refactor apple_min_version_flag()

* Refactored all the apple module and where it was being used (AutotoolsToolchain and MesonToolchain for now)

* Fixed bad return

* Fixing tests

* Keeping legacy behavior in apple_min_version_flag function

* Preventing possible breaking change

---------

Co-authored-by: Francisco Ramirez de Anton <franchuti688@gmail.com>

* Allow to unhide git url (#16038)

* Add hide_url tests for git scm tool

* Add hide_url flag to clone and fetch_commit.

Resolves #15684

* Update conans/test/functional/tools/scm/test_git.py

* Update conans/test/functional/tools/scm/test_git.py

---------

Co-authored-by: Rubén Rincón Blanco <git@rinconblanco.es>

* remove repeated non-running test (#16053)

* refactor transitive_requires

---------

Co-authored-by: Francisco Ramirez de Anton <franchuti688@gmail.com>
Co-authored-by: Sebastian Höffner <info@sebastian-hoeffner.de>
Co-authored-by: Rubén Rincón Blanco <git@rinconblanco.es>
  • Loading branch information
4 people committed Apr 11, 2024
1 parent f0bddeb commit ad0b39c
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 63 deletions.
5 changes: 5 additions & 0 deletions conan/tools/cmake/cmakedeps/cmakedeps.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
from conan.tools.files import save
from conans.client.generators import relativize_generated_file
from conan.errors import ConanException
from conans.model.dependencies import get_transitive_requires


class CMakeDeps(object):
Expand Down Expand Up @@ -220,3 +221,7 @@ def generate_aggregator(self):
undefined=jinja2.StrictUndefined)
conandeps = template.render({"configs": configs})
save(self._conanfile, "conandeps_legacy.cmake", conandeps)

def get_transitive_requires(self, conanfile):
# Prepared to filter transitive tool-requires with visible=True
return get_transitive_requires(self._conanfile, conanfile)
17 changes: 10 additions & 7 deletions conan/tools/cmake/cmakedeps/templates/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ def context(self):
@property
def template(self):
return textwrap.dedent("""\
{%- macro pkg_var(pkg_name, var, config_suffix) -%}
{{'${'+pkg_name+'_'+var+config_suffix+'}'}}
{%- endmacro -%}
########## MACROS ###########################################################################
#############################################################################################
Expand All @@ -57,21 +60,21 @@ def template(self):
check_build_type_defined()
foreach(_DEPENDENCY {{ '${' + pkg_name + '_FIND_DEPENDENCY_NAMES' + '}' }} )
foreach(_DEPENDENCY {{ pkg_var(pkg_name, 'FIND_DEPENDENCY_NAMES', '') }} )
# Check that we have not already called a find_package with the transitive dependency
if(NOT {{ '${_DEPENDENCY}' }}_FOUND)
find_dependency({{ '${_DEPENDENCY}' }} REQUIRED ${${_DEPENDENCY}_FIND_MODE})
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 + '}' }} )
set({{ file_name }}_INCLUDE_DIRS {{ pkg_var(pkg_name, 'INCLUDE_DIRS', config_suffix) }} )
set({{ file_name }}_INCLUDE_DIR {{ pkg_var(pkg_name, 'INCLUDE_DIRS', config_suffix) }} )
set({{ file_name }}_LIBRARIES {{ pkg_var(pkg_name, 'LIBRARIES', config_suffix) }} )
set({{ file_name }}_DEFINITIONS {{ pkg_var(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 + '}' }} )
foreach(_BUILD_MODULE {{ pkg_var(pkg_name, 'BUILD_MODULES_PATHS', config_suffix) }} )
message({% raw %}${{% endraw %}{{ file_name }}_MESSAGE_MODE} "Conan: Including build module from '${_BUILD_MODULE}'")
include({{ '${_BUILD_MODULE}' }})
endforeach()
Expand All @@ -80,7 +83,7 @@ def template(self):
# 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({{ file_name }}_FIND_COMPONENTS)
foreach(_FIND_COMPONENT {{ '${'+file_name+'_FIND_COMPONENTS}' }})
foreach(_FIND_COMPONENT {{ pkg_var(file_name, 'FIND_COMPONENTS', '') }})
if (TARGET ${_FIND_COMPONENT})
message({% raw %}${{% endraw %}{{ file_name }}_MESSAGE_MODE} "Conan: Component '${_FIND_COMPONENT}' found in package '{{ pkg_name }}'")
else()
Expand Down
86 changes: 44 additions & 42 deletions conan/tools/cmake/cmakedeps/templates/target_configuration.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import textwrap

from conan.tools.cmake.cmakedeps.templates import CMakeDepsFileTemplate
from conans.model.dependencies import get_transitive_requires

"""
Expand Down Expand Up @@ -45,14 +44,17 @@ def template(self):
# Avoid multiple calls to find_package to append duplicated properties to the targets
include_guard()
{%- macro tvalue(pkg_name, comp_name, var, config_suffix) -%}
{%- macro comp_var(pkg_name, comp_name, var, config_suffix) -%}
{{'${'+pkg_name+'_'+comp_name+'_'+var+config_suffix+'}'}}
{%- endmacro -%}
{%- macro pkg_var(pkg_name, var, config_suffix) -%}
{{'${'+pkg_name+'_'+var+config_suffix+'}'}}
{%- endmacro -%}
########### VARIABLES #######################################################################
#############################################################################################
set({{ pkg_name }}_FRAMEWORKS_FOUND{{ config_suffix }} "") # Will be filled later
conan_find_apple_frameworks({{ pkg_name }}_FRAMEWORKS_FOUND{{ config_suffix }} "{{ '${' }}{{ pkg_name }}_FRAMEWORKS{{ config_suffix }}}" "{{ '${' }}{{ pkg_name }}_FRAMEWORK_DIRS{{ config_suffix }}}")
conan_find_apple_frameworks({{ pkg_name }}_FRAMEWORKS_FOUND{{ config_suffix }} "{{ pkg_var(pkg_name, 'FRAMEWORKS', config_suffix) }}" "{{ pkg_var(pkg_name, 'FRAMEWORK_DIRS', config_suffix) }}")
set({{ pkg_name }}_LIBRARIES_TARGETS "") # Will be filled later
Expand All @@ -64,35 +66,35 @@ def template(self):
set_property(TARGET {{ pkg_name + '_DEPS_TARGET'}}
APPEND PROPERTY INTERFACE_LINK_LIBRARIES
$<$<CONFIG:{{configuration}}>:{{ '${'+pkg_name+'_FRAMEWORKS_FOUND'+config_suffix+'}' }}>
$<$<CONFIG:{{configuration}}>:{{ '${'+pkg_name+'_SYSTEM_LIBS'+config_suffix+'}' }}>
$<$<CONFIG:{{configuration}}>:{{ pkg_var(pkg_name, 'FRAMEWORKS_FOUND', config_suffix) }}>
$<$<CONFIG:{{configuration}}>:{{ pkg_var(pkg_name, 'SYSTEM_LIBS', config_suffix) }}>
$<$<CONFIG:{{configuration}}>:{{ deps_targets_names }}>)
####### Find the libraries declared in cpp_info.libs, create an IMPORTED target for each one and link the
####### {{pkg_name}}_DEPS_TARGET to all of them
conan_package_library_targets("{{ '${' }}{{ pkg_name }}_LIBS{{ config_suffix }}}" # libraries
"{{ '${' }}{{ pkg_name }}_LIB_DIRS{{ config_suffix }}}" # package_libdir
"{{ '${' }}{{ pkg_name }}_BIN_DIRS{{ config_suffix }}}" # package_bindir
"{{ '${' }}{{ pkg_name }}_LIBRARY_TYPE{{ config_suffix }}}"
"{{ '${' }}{{ pkg_name }}_IS_HOST_WINDOWS{{ config_suffix }}}"
conan_package_library_targets("{{ pkg_var(pkg_name, 'LIBS', config_suffix) }}" # libraries
"{{ pkg_var(pkg_name, 'LIB_DIRS', config_suffix) }}" # package_libdir
"{{ pkg_var(pkg_name, 'BIN_DIRS', config_suffix) }}" # package_bindir
"{{ pkg_var(pkg_name, 'LIBRARY_TYPE', config_suffix) }}"
"{{ pkg_var(pkg_name, 'IS_HOST_WINDOWS', config_suffix) }}"
{{ pkg_name + '_DEPS_TARGET'}}
{{ pkg_name }}_LIBRARIES_TARGETS # out_libraries_targets
"{{ config_suffix }}"
"{{ pkg_name }}" # package_name
"{{ '${' }}{{ pkg_name }}_NO_SONAME_MODE{{ config_suffix }}}") # soname
"{{ pkg_var(pkg_name, 'NO_SONAME_MODE', config_suffix) }}") # soname
# FIXME: What is the result of this for multi-config? All configs adding themselves to path?
set(CMAKE_MODULE_PATH {{ '${' }}{{ pkg_name }}_BUILD_DIRS{{ config_suffix }}} {{ '${' }}CMAKE_MODULE_PATH})
set(CMAKE_MODULE_PATH {{ pkg_var(pkg_name, 'BUILD_DIRS', config_suffix) }} {{ '${' }}CMAKE_MODULE_PATH})
{% if not components_names %}
########## GLOBAL TARGET PROPERTIES {{ configuration }} ########################################
set_property(TARGET {{root_target_name}}
APPEND PROPERTY INTERFACE_LINK_LIBRARIES
$<$<CONFIG:{{configuration}}>:{{ '${'+pkg_name+'_OBJECTS'+config_suffix+'}' }}>
$<$<CONFIG:{{configuration}}>:${{'{'}}{{pkg_name}}_LIBRARIES_TARGETS}>
$<$<CONFIG:{{configuration}}>:{{ pkg_var(pkg_name, 'OBJECTS', config_suffix) }}>
$<$<CONFIG:{{configuration}}>:{{ pkg_var(pkg_name, 'LIBRARIES_TARGETS', '') }}>
)
if("{{ '${' }}{{ pkg_name }}_LIBS{{ config_suffix }}}" STREQUAL "")
if("{{ pkg_var(pkg_name, 'LIBS', config_suffix) }}" STREQUAL "")
# If the package is not declaring any "cpp_info.libs" the package deps, system libs,
# frameworks etc are not linked to the imported targets and we need to do it to the
# global target
Expand All @@ -103,27 +105,27 @@ def template(self):
set_property(TARGET {{root_target_name}}
APPEND PROPERTY INTERFACE_LINK_OPTIONS
$<$<CONFIG:{{configuration}}>:${{'{'}}{{pkg_name}}_LINKER_FLAGS{{config_suffix}}}>)
$<$<CONFIG:{{configuration}}>:{{ pkg_var(pkg_name, 'LINKER_FLAGS', config_suffix) }}>)
set_property(TARGET {{root_target_name}}
APPEND PROPERTY INTERFACE_INCLUDE_DIRECTORIES
$<$<CONFIG:{{configuration}}>:${{'{'}}{{pkg_name}}_INCLUDE_DIRS{{config_suffix}}}>)
$<$<CONFIG:{{configuration}}>:{{ pkg_var(pkg_name, 'INCLUDE_DIRS', config_suffix) }}>)
# Necessary to find LINK shared libraries in Linux
set_property(TARGET {{root_target_name}}
APPEND PROPERTY INTERFACE_LINK_DIRECTORIES
$<$<CONFIG:{{configuration}}>:${{'{'}}{{pkg_name}}_LIB_DIRS{{config_suffix}}}>)
$<$<CONFIG:{{configuration}}>:{{ pkg_var(pkg_name, 'LIB_DIRS', config_suffix) }}>)
set_property(TARGET {{root_target_name}}
APPEND PROPERTY INTERFACE_COMPILE_DEFINITIONS
$<$<CONFIG:{{configuration}}>:${{'{'}}{{pkg_name}}_COMPILE_DEFINITIONS{{config_suffix}}}>)
$<$<CONFIG:{{configuration}}>:{{ pkg_var(pkg_name, 'COMPILE_DEFINITIONS', config_suffix) }}>)
set_property(TARGET {{root_target_name}}
APPEND PROPERTY INTERFACE_COMPILE_OPTIONS
$<$<CONFIG:{{configuration}}>:${{'{'}}{{pkg_name}}_COMPILE_OPTIONS{{config_suffix}}}>)
$<$<CONFIG:{{configuration}}>:{{ pkg_var(pkg_name, 'COMPILE_OPTIONS', config_suffix) }}>)
{%- if set_interface_link_directories %}
# This is only used for '#pragma comment(lib, "foo")' (automatic link)
set_property(TARGET {{root_target_name}}
APPEND PROPERTY INTERFACE_LINK_DIRECTORIES
$<$<CONFIG:{{configuration}}>:${{'{'}}{{pkg_name}}_LIB_DIRS{{config_suffix}}}>)
$<$<CONFIG:{{configuration}}>:{{ pkg_var(pkg_name, 'LIB_DIRS', config_suffix) }}>)
{%- endif %}
Expand All @@ -137,7 +139,7 @@ def template(self):
########## COMPONENT {{ comp_target_name }} #############
set({{ pkg_name }}_{{ comp_variable_name }}_FRAMEWORKS_FOUND{{ config_suffix }} "")
conan_find_apple_frameworks({{ pkg_name }}_{{ comp_variable_name }}_FRAMEWORKS_FOUND{{ config_suffix }} "{{ '${'+pkg_name+'_'+comp_variable_name+'_FRAMEWORKS'+config_suffix+'}' }}" "{{ '${'+pkg_name+'_'+comp_variable_name+'_FRAMEWORK_DIRS'+config_suffix+'}' }}")
conan_find_apple_frameworks({{ pkg_name }}_{{ comp_variable_name }}_FRAMEWORKS_FOUND{{ config_suffix }} "{{ comp_var(pkg_name, comp_variable_name, 'FRAMEWORKS', config_suffix) }}" "{{ comp_var(pkg_name, comp_variable_name, 'FRAMEWORK_DIRS', config_suffix) }}")
set({{ pkg_name }}_{{ comp_variable_name }}_LIBRARIES_TARGETS "")
Expand All @@ -148,33 +150,33 @@ def template(self):
set_property(TARGET {{ pkg_name + '_' + comp_variable_name + '_DEPS_TARGET'}}
APPEND PROPERTY INTERFACE_LINK_LIBRARIES
$<$<CONFIG:{{configuration}}>:{{ '${'+pkg_name+'_'+comp_variable_name+'_FRAMEWORKS_FOUND'+config_suffix+'}' }}>
$<$<CONFIG:{{configuration}}>:{{ '${'+pkg_name+'_'+comp_variable_name+'_SYSTEM_LIBS'+config_suffix+'}' }}>
$<$<CONFIG:{{configuration}}>:{{ '${'+pkg_name+'_'+comp_variable_name+'_DEPENDENCIES'+config_suffix+'}' }}>
$<$<CONFIG:{{configuration}}>:{{ comp_var(pkg_name, comp_variable_name, 'FRAMEWORKS_FOUND', config_suffix) }}>
$<$<CONFIG:{{configuration}}>:{{ comp_var(pkg_name, comp_variable_name, 'SYSTEM_LIBS', config_suffix) }}>
$<$<CONFIG:{{configuration}}>:{{ comp_var(pkg_name, comp_variable_name, 'DEPENDENCIES', config_suffix) }}>
)
####### Find the libraries declared in cpp_info.component["xxx"].libs,
####### create an IMPORTED target for each one and link the '{{pkg_name}}_{{comp_variable_name}}_DEPS_TARGET' to all of them
conan_package_library_targets("{{ '${'+pkg_name+'_'+comp_variable_name+'_LIBS'+config_suffix+'}' }}"
"{{ '${'+pkg_name+'_'+comp_variable_name+'_LIB_DIRS'+config_suffix+'}' }}"
"{{ '${'+pkg_name+'_'+comp_variable_name+'_BIN_DIRS'+config_suffix+'}' }}" # package_bindir
"{{ '${'+pkg_name+'_'+comp_variable_name+'_LIBRARY_TYPE'+config_suffix+'}' }}"
"{{ '${'+pkg_name+'_'+comp_variable_name+'_IS_HOST_WINDOWS'+config_suffix+'}' }}"
conan_package_library_targets("{{ comp_var(pkg_name, comp_variable_name, 'LIBS', config_suffix) }}"
"{{ comp_var(pkg_name, comp_variable_name, 'LIB_DIRS', config_suffix) }}"
"{{ comp_var(pkg_name, comp_variable_name, 'BIN_DIRS', config_suffix) }}" # package_bindir
"{{ comp_var(pkg_name, comp_variable_name, 'LIBRARY_TYPE', config_suffix) }}"
"{{ comp_var(pkg_name, comp_variable_name, 'IS_HOST_WINDOWS', config_suffix) }}"
{{ pkg_name + '_' + comp_variable_name + '_DEPS_TARGET'}}
{{ pkg_name }}_{{ comp_variable_name }}_LIBRARIES_TARGETS
{{ pkg_name + '_' + comp_variable_name + '_LIBRARIES_TARGETS'}}
"{{ config_suffix }}"
"{{ pkg_name }}_{{ comp_variable_name }}"
"{{ '${'+pkg_name+'_'+comp_variable_name+'_NO_SONAME_MODE'+config_suffix+'}' }}")
"{{ comp_var(pkg_name, comp_variable_name, 'NO_SONAME_MODE', config_suffix) }}")
########## TARGET PROPERTIES #####################################
set_property(TARGET {{comp_target_name}}
APPEND PROPERTY INTERFACE_LINK_LIBRARIES
$<$<CONFIG:{{configuration}}>:{{ '${'+pkg_name+'_'+comp_variable_name+'_OBJECTS'+config_suffix+'}' }}>
$<$<CONFIG:{{configuration}}>:${{'{'}}{{pkg_name}}_{{comp_variable_name}}_LIBRARIES_TARGETS}>
$<$<CONFIG:{{configuration}}>:{{ comp_var(pkg_name, comp_variable_name, 'OBJECTS', config_suffix) }}>
$<$<CONFIG:{{configuration}}>:{{ comp_var(pkg_name, comp_variable_name, 'LIBRARIES_TARGETS', '') }}>
)
if("{{ '${' }}{{ pkg_name }}_{{comp_variable_name}}_LIBS{{ config_suffix }}}" STREQUAL "")
if("{{ comp_var(pkg_name, comp_variable_name, 'LIBS', config_suffix) }}" STREQUAL "")
# If the component is not declaring any "cpp_info.components['foo'].libs" the system, frameworks etc are not
# linked to the imported targets and we need to do it to the global target
set_property(TARGET {{comp_target_name}}
Expand All @@ -183,20 +185,20 @@ def template(self):
endif()
set_property(TARGET {{ comp_target_name }} APPEND PROPERTY INTERFACE_LINK_OPTIONS
$<$<CONFIG:{{ configuration }}>:{{tvalue(pkg_name, comp_variable_name, 'LINKER_FLAGS', config_suffix)}}>)
$<$<CONFIG:{{ configuration }}>:{{ comp_var(pkg_name, comp_variable_name, 'LINKER_FLAGS', config_suffix) }}>)
set_property(TARGET {{ comp_target_name }} APPEND PROPERTY INTERFACE_INCLUDE_DIRECTORIES
$<$<CONFIG:{{ configuration }}>:{{tvalue(pkg_name, comp_variable_name, 'INCLUDE_DIRS', config_suffix)}}>)
$<$<CONFIG:{{ configuration }}>:{{ comp_var(pkg_name, comp_variable_name, 'INCLUDE_DIRS', config_suffix) }}>)
set_property(TARGET {{comp_target_name }} APPEND PROPERTY INTERFACE_LINK_DIRECTORIES
$<$<CONFIG:{{ configuration }}>:{{tvalue(pkg_name, comp_variable_name, 'LIB_DIRS', config_suffix)}}>)
$<$<CONFIG:{{ configuration }}>:{{ comp_var(pkg_name, comp_variable_name, 'LIB_DIRS', config_suffix) }}>)
set_property(TARGET {{ comp_target_name }} APPEND PROPERTY INTERFACE_COMPILE_DEFINITIONS
$<$<CONFIG:{{ configuration }}>:{{tvalue(pkg_name, comp_variable_name, 'COMPILE_DEFINITIONS', config_suffix)}}>)
$<$<CONFIG:{{ configuration }}>:{{ comp_var(pkg_name, comp_variable_name, 'COMPILE_DEFINITIONS', config_suffix) }}>)
set_property(TARGET {{ comp_target_name }} APPEND PROPERTY INTERFACE_COMPILE_OPTIONS
$<$<CONFIG:{{ configuration }}>:{{tvalue(pkg_name, comp_variable_name, 'COMPILE_OPTIONS', config_suffix)}}>)
$<$<CONFIG:{{ configuration }}>:{{ comp_var(pkg_name, comp_variable_name, 'COMPILE_OPTIONS', config_suffix) }}>)
{%- if set_interface_link_directories %}
# This is only used for '#pragma comment(lib, "foo")' (automatic link)
set_property(TARGET {{ comp_target_name }} APPEND PROPERTY INTERFACE_LINK_DIRECTORIES
$<$<CONFIG:{{ configuration }}>:{{tvalue(pkg_name, comp_variable_name, 'LIB_DIRS', config_suffix)}}>)
$<$<CONFIG:{{ configuration }}>:{{ comp_var(pkg_name, comp_variable_name, 'LIB_DIRS', config_suffix) }}>)
{%- endif %}
{%endfor %}
Expand Down Expand Up @@ -235,7 +237,7 @@ def get_deps_targets_names(self):

# Get a list of dependencies target names
# Declared cppinfo.requires or .components[].requires
transitive_reqs = get_transitive_requires(self.cmakedeps._conanfile, self.conanfile)
transitive_reqs = self.cmakedeps.get_transitive_requires(self.conanfile)
if self.conanfile.cpp_info.required_components:
for dep_name, component_name in self.conanfile.cpp_info.required_components:
try:
Expand Down
Loading

0 comments on commit ad0b39c

Please sign in to comment.