Skip to content

Commit e23ca63

Browse files
committed
moving legacy vars in CMakeConfigDeps (conan-io#18860)
* moving legacy vars in CMakeConfigDeps * wip * fix * fix
1 parent 90e33ef commit e23ca63

File tree

4 files changed

+62
-50
lines changed

4 files changed

+62
-50
lines changed

conan/tools/cmake/cmakedeps2/cmakedeps.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,14 +68,15 @@ def _content(self):
6868

6969
if require.direct:
7070
direct_deps.append((require, dep))
71-
config = ConfigTemplate2(self, dep)
71+
full_cpp_info = dep.cpp_info.deduce_full_cpp_info(dep)
72+
config = ConfigTemplate2(self, require, dep, full_cpp_info)
7273
ret[config.filename] = config.content()
7374
config_version = ConfigVersionTemplate2(self, dep)
7475
ret[config_version.filename] = config_version.content()
7576

7677
targets = TargetsTemplate2(self, dep)
7778
ret[targets.filename] = targets.content()
78-
target_configuration = TargetConfigurationTemplate2(self, dep, require)
79+
target_configuration = TargetConfigurationTemplate2(self, dep, require, full_cpp_info)
7980
ret[target_configuration.filename] = target_configuration.content()
8081

8182
self._print_help(direct_deps)

conan/tools/cmake/cmakedeps2/config.py

Lines changed: 53 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,11 @@ class ConfigTemplate2:
1111
FooConfig.cmake
1212
foo-config.cmake
1313
"""
14-
def __init__(self, cmakedeps, conanfile):
14+
def __init__(self, cmakedeps, require, conanfile, full_cpp_info):
1515
self._cmakedeps = cmakedeps
16+
self._require = require
1617
self._conanfile = conanfile
18+
self._full_cpp_info = full_cpp_info
1719

1820
def content(self):
1921
t = Template(self._template, trim_blocks=True, lstrip_blocks=True,
@@ -57,11 +59,41 @@ def _context(self):
5759
cmakename = cmakename.split("::", 1)[1]
5860
components.append(cmakename or name)
5961
components = " ".join(components) if components else ""
60-
return {"filename": f,
61-
"components": components,
62-
"pkg_name": pkg_name,
63-
"targets_include_file": targets_include,
64-
"build_modules_paths": build_modules_paths}
62+
63+
result = {"filename": f,
64+
"components": components,
65+
"pkg_name": pkg_name,
66+
"targets_include_file": targets_include,
67+
"build_modules_paths": build_modules_paths}
68+
69+
result.update(self._get_legacy_vars())
70+
return result
71+
72+
def _get_legacy_vars(self):
73+
# Auxiliary variables for legacy consumption and try_compile cases
74+
pkg_name = self._conanfile.ref.name
75+
prefixes = self._cmakedeps.get_property("cmake_additional_variables_prefixes",
76+
self._conanfile, check_type=list) or []
77+
78+
f = self._cmakedeps.get_cmake_filename(self._conanfile)
79+
prefixes = [f] + prefixes
80+
include_dirs = definitions = libraries = None
81+
if not self._require.build: # To add global variables for try_compile and legacy
82+
aggregated_cppinfo = self._full_cpp_info.aggregated_components()
83+
# FIXME: Proper escaping of paths for CMake
84+
incdirs = [i.replace("\\", "/") for i in aggregated_cppinfo.includedirs]
85+
incdirs = [relativize_path(i, self._cmakedeps._conanfile, "${CMAKE_CURRENT_LIST_DIR}")
86+
for i in incdirs]
87+
include_dirs = ";".join(incdirs)
88+
definitions = ""
89+
root_target_name = self._cmakedeps.get_property("cmake_target_name", self._conanfile)
90+
libraries = root_target_name or f"{pkg_name}::{pkg_name}"
91+
92+
return {"additional_variables_prefixes": prefixes,
93+
"version": self._conanfile.ref.version,
94+
"include_dirs": include_dirs,
95+
"definitions": definitions,
96+
"libraries": libraries}
6597

6698
@property
6799
def _template(self):
@@ -96,4 +128,19 @@ def _template(self):
96128
endif()
97129
endforeach()
98130
{% endif %}
131+
132+
################# Global variables for try compile and legacy ##############
133+
{% for prefix in additional_variables_prefixes %}
134+
set({{ prefix }}_VERSION_STRING "{{ version }}")
135+
{% if include_dirs is not none %}
136+
set({{ prefix }}_INCLUDE_DIRS "{{ include_dirs }}" )
137+
set({{ prefix }}_INCLUDE_DIR "{{ include_dirs }}" )
138+
{% endif %}
139+
{% if libraries is not none %}
140+
set({{ prefix }}_LIBRARIES {{ libraries }} )
141+
{% endif %}
142+
{% if definitions is not none %}
143+
set({{ prefix }}_DEFINITIONS {{ definitions}} )
144+
{% endif %}
145+
{% endfor %}
99146
""")

conan/tools/cmake/cmakedeps2/target_configuration.py

Lines changed: 4 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,11 @@ class TargetConfigurationTemplate2:
1414
"""
1515
FooTarget-release.cmake
1616
"""
17-
def __init__(self, cmakedeps, conanfile, require):
17+
def __init__(self, cmakedeps, conanfile, require, full_cpp_info):
1818
self._cmakedeps = cmakedeps
1919
self._conanfile = conanfile # The dependency conanfile, not the consumer one
2020
self._require = require
21+
self._full_cpp_info = full_cpp_info
2122

2223
def content(self):
2324
auto_link = self._cmakedeps.get_property("cmake_set_interface_link_directories",
@@ -102,7 +103,7 @@ def _requires(self, info, components):
102103

103104
@property
104105
def _context(self):
105-
cpp_info = self._conanfile.cpp_info.deduce_full_cpp_info(self._conanfile)
106+
cpp_info = self._full_cpp_info
106107
assert isinstance(cpp_info.type, PackageType)
107108
pkg_name = self._conanfile.ref.name
108109
# fallback to consumer configuration if it doesn't have build_type
@@ -120,8 +121,6 @@ def _context(self):
120121
self._add_root_lib_target(libs, pkg_name, cpp_info)
121122
exes = self._get_exes(cpp_info, pkg_name, pkg_folder, pkg_folder_var)
122123

123-
prefixes = self._cmakedeps.get_property("cmake_additional_variables_prefixes",
124-
self._conanfile, check_type=list) or []
125124
seen_aliases = set()
126125
root_target_name = self._cmakedeps.get_property("cmake_target_name", self._conanfile)
127126
root_target_name = root_target_name or f"{pkg_name}::{pkg_name}"
@@ -139,20 +138,6 @@ def _context(self):
139138
raise ConanException(f"Alias '{alias}' already defined as a target in "
140139
f"{self._conanfile}. ")
141140

142-
f = self._cmakedeps.get_cmake_filename(self._conanfile)
143-
prefixes = [f] + prefixes
144-
include_dirs = definitions = libraries = None
145-
if not self._require.build: # To add global variables for try_compile and legacy
146-
aggregated_cppinfo = cpp_info.aggregated_components()
147-
# FIXME: Proper escaping of paths for CMake
148-
incdirs = [i.replace("\\", "/") for i in aggregated_cppinfo.includedirs]
149-
incdirs = [relativize_path(i, self._cmakedeps._conanfile, "${CMAKE_CURRENT_LIST_DIR}")
150-
for i in incdirs]
151-
include_dirs = ";".join(incdirs)
152-
definitions = ""
153-
root_target_name = self._cmakedeps.get_property("cmake_target_name", self._conanfile)
154-
libraries = root_target_name or f"{pkg_name}::{pkg_name}"
155-
156141
pkg_folder = relativize_path(pkg_folder, self._cmakedeps._conanfile,
157142
"${CMAKE_CURRENT_LIST_DIR}")
158143
dependencies = self._get_dependencies()
@@ -162,13 +147,7 @@ def _context(self):
162147
"config": config,
163148
"exes": exes,
164149
"libs": libs,
165-
"context": self._conanfile.context,
166-
# Extra global variables
167-
"additional_variables_prefixes": prefixes,
168-
"version": self._conanfile.ref.version,
169-
"include_dirs": include_dirs,
170-
"definitions": definitions,
171-
"libraries": libraries,
150+
"context": self._conanfile.context
172151
}
173152

174153
def _get_libs(self, cpp_info, pkg_name, pkg_folder, pkg_folder_var) -> dict:
@@ -467,21 +446,6 @@ def _template(self):
467446
{% endif %}
468447
{% endfor %}
469448
470-
################# Global variables for try compile and legacy ##############
471-
{% for prefix in additional_variables_prefixes %}
472-
set({{ prefix }}_VERSION_STRING "{{ version }}")
473-
{% if include_dirs is not none %}
474-
set({{ prefix }}_INCLUDE_DIRS "{{ include_dirs }}" )
475-
set({{ prefix }}_INCLUDE_DIR "{{ include_dirs }}" )
476-
{% endif %}
477-
{% if libraries is not none %}
478-
set({{ prefix }}_LIBRARIES {{ libraries }} )
479-
{% endif %}
480-
{% if definitions is not none %}
481-
set({{ prefix }}_DEFINITIONS {{ definitions}} )
482-
{% endif %}
483-
{% endfor %}
484-
485449
################# Exes information ##############
486450
{% for exe, location in exes.items() %}
487451
#################### {{exe}} ####################

test/integration/toolchains/cmake/cmakedeps2/test_cmakedeps.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -145,11 +145,11 @@ class PkgConan(ConanFile):
145145
liba_config = c.load("liba-config.cmake")
146146
assert ('include("${CMAKE_CURRENT_LIST_DIR}/full_deploy/'
147147
'host/liba/1.0/share/cmake/crypto.cmake")') in liba_config
148+
assert ('set(liba_INCLUDE_DIRS "${CMAKE_CURRENT_LIST_DIR}/full_deploy/'
149+
'host/liba/1.0/includea" )') in liba_config
148150
liba_targets = c.load("liba-Targets-release.cmake")
149151
assert ('set(liba_PACKAGE_FOLDER_RELEASE "${CMAKE_CURRENT_LIST_DIR}/full_deploy/'
150152
'host/liba/1.0")') in liba_targets
151-
assert ('set(liba_INCLUDE_DIRS "${CMAKE_CURRENT_LIST_DIR}/full_deploy/'
152-
'host/liba/1.0/includea" )') in liba_targets
153153

154154

155155
def test_cmakeconfigdeps_recipe():

0 commit comments

Comments
 (0)