Skip to content

Commit 79d7454

Browse files
authored
new CMakeConfigDeps properties to inject extra dependencies and targets (#18316)
1 parent 8f1a5ca commit 79d7454

File tree

2 files changed

+27
-1
lines changed

2 files changed

+27
-1
lines changed

conan/tools/cmake/cmakedeps2/target_configuration.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,8 @@ def _get_cmake_lib(self, info, components, pkg_folder, pkg_folder_var):
190190
# FIXME: Filter by lib traits!!!!!
191191
if not self._require.headers: # If not depending on headers, paths and
192192
includedirs = defines = None
193+
extra_libs = self._cmakedeps.get_property("cmake_extra_interface_libs", self._conanfile,
194+
check_type=list) or []
193195
sources = [self._path(source, pkg_folder, pkg_folder_var) for source in info.sources]
194196
target = {"type": "INTERFACE",
195197
"includedirs": includedirs,
@@ -199,7 +201,7 @@ def _get_cmake_lib(self, info, components, pkg_folder, pkg_folder_var):
199201
"cflags": " ".join(cmake_escape_value(f) for f in info.cflags),
200202
"sharedlinkflags": " ".join(cmake_escape_value(v) for v in info.sharedlinkflags),
201203
"exelinkflags": " ".join(cmake_escape_value(v) for v in info.exelinkflags),
202-
"system_libs": " ".join(info.system_libs),
204+
"system_libs": " ".join(info.system_libs + extra_libs),
203205
"sources": " ".join(sources)
204206
}
205207
# System frameworks (only Apple OS)
@@ -295,6 +297,9 @@ def _get_dependencies(self):
295297
transitive_reqs = self._cmakedeps.get_transitive_requires(self._conanfile)
296298
# FIXME: Hardcoded CONFIG
297299
ret = {self._cmakedeps.get_cmake_filename(r): "CONFIG" for r in transitive_reqs.values()}
300+
extra_mods = self._cmakedeps.get_property("cmake_extra_dependencies", self._conanfile,
301+
check_type=list) or []
302+
ret.update({extra_mod: "" for extra_mod in extra_mods})
298303
return ret
299304

300305
@staticmethod

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

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -388,6 +388,27 @@ def package_info(self):
388388
assert "CMakeConfigDeps does not support module find mode"
389389

390390

391+
def test_cmake_extra_dependencies():
392+
tc = TestClient()
393+
dep = textwrap.dedent("""
394+
from conan import ConanFile
395+
class Pkg(ConanFile):
396+
name = "dep"
397+
version = "0.1"
398+
def package_info(self):
399+
self.cpp_info.set_property("cmake_extra_dependencies", ["MyOpenMPI"])
400+
self.cpp_info.set_property("cmake_extra_interface_libs", ["MyOpenMPILib"])
401+
""")
402+
tc.save({"conanfile.py": dep})
403+
tc.run("create .")
404+
args = f"-g CMakeDeps -c tools.cmake.cmakedeps:new={new_value}"
405+
tc.run(f"install --requires=dep/0.1 {args}")
406+
dep = tc.load("dep-Targets-release.cmake")
407+
assert "find_dependency(MyOpenMPI REQUIRED )" in dep
408+
assert "set_property(TARGET dep::dep APPEND PROPERTY INTERFACE_LINK_LIBRARIES\n" \
409+
" $<$<CONFIG:RELEASE>:MyOpenMPILib>)" in dep
410+
411+
391412
class TestRequiresToApp:
392413
def test_requires_to_application(self):
393414
c = TestClient()

0 commit comments

Comments
 (0)