Skip to content

Commit db8d50d

Browse files
authored
add CMAKE_MODULE_PATH to CMakeConfigDeps for include(module) (#18162)
Changelog: Feature: add CMAKE_MODULE_PATH to CMakeConfigDeps for include(module) Docs: Omit Close #18155
2 parents db16b7b + 5aaf76f commit db8d50d

File tree

2 files changed

+96
-5
lines changed

2 files changed

+96
-5
lines changed

conan/tools/cmake/cmakedeps2/cmakedeps.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,7 @@ def _get_cmake_paths(self, requirements, dirs_name):
174174
"libdirs": "CMAKE_LIBRARY_PATH",
175175
"includedirs": "CMAKE_INCLUDE_PATH",
176176
"frameworkdirs": "CMAKE_FRAMEWORK_PATH",
177+
"builddirs": "CMAKE_MODULE_PATH"
177178
}
178179
for req, dep in requirements:
179180
cppinfo = dep.cpp_info.aggregated_components()
@@ -210,6 +211,10 @@ def generate(self):
210211
{% if cmake_framework_path %}
211212
list(PREPEND CMAKE_FRAMEWORK_PATH {{ cmake_framework_path }})
212213
{% endif %}
214+
# Definition of CMAKE_MODULE_PATH to be able to include(module)
215+
{% if cmake_module_path %}
216+
list(PREPEND CMAKE_MODULE_PATH {{ cmake_module_path }})
217+
{% endif %}
213218
""")
214219
host_req = self._conanfile.dependencies.host
215220
build_req = self._conanfile.dependencies.direct_build
@@ -251,13 +256,15 @@ def generate(self):
251256
cmake_library_path = self._get_cmake_paths(host_test_reqs, "libdirs")
252257
cmake_include_path = self._get_cmake_paths(host_test_reqs, "includedirs")
253258
cmake_framework_path = self._get_cmake_paths(host_test_reqs, "frameworkdirs")
259+
cmake_module_path = self._get_cmake_paths(all_reqs, "builddirs")
254260
context = {"host_runtime_dirs": self._get_host_runtime_dirs(),
255261
"pkg_paths": pkg_paths,
256262
"cmake_program_path": _join_paths(self._conanfile, cmake_program_path),
257263
"cmake_library_path": _join_paths(self._conanfile, cmake_library_path),
258264
"cmake_include_path": _join_paths(self._conanfile, cmake_include_path),
259265
"cmake_framework_path": _join_paths(self._conanfile, cmake_framework_path),
260-
}
266+
"cmake_module_path": _join_paths(self._conanfile, cmake_module_path)
267+
}
261268
content = Template(template, trim_blocks=True, lstrip_blocks=True).render(context)
262269
save(self._conanfile, self._conan_cmakedeps_paths, content)
263270

test/functional/toolchains/cmake/cmakedeps2/test_cmakeconfigdeps_new_paths.py

Lines changed: 88 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ def build(self):
141141
""")
142142
consumer = textwrap.dedent("""
143143
cmake_minimum_required(VERSION 3.15)
144-
project(MyHello)
144+
project(MyHello NONE)
145145
find_program(HELLOPROG hello)
146146
if(HELLOPROG)
147147
message(STATUS "Found hello prog: ${HELLOPROG}")
@@ -172,8 +172,7 @@ def package(self):
172172
""")
173173
c.save({"conanfile.py": conanfile,
174174
"hello.h": "", "hello.lib": "", "libhello.a": "",
175-
"libhello.so": "", "libhello.dll": ""
176-
})
175+
"libhello.so": "", "libhello.dll": ""})
177176
c.run("create .")
178177
conanfile = textwrap.dedent(f"""
179178
from conan import ConanFile
@@ -188,7 +187,7 @@ def build(self):
188187
""")
189188
consumer = textwrap.dedent("""
190189
cmake_minimum_required(VERSION 3.15)
191-
project(MyHello)
190+
project(MyHello NONE)
192191
find_file(HELLOINC hello.h)
193192
find_library(HELLOLIB hello)
194193
if(HELLOINC)
@@ -202,3 +201,88 @@ def build(self):
202201
c.run(f"build . -c tools.cmake.cmakedeps:new={new_value}")
203202
assert "Found hello header" in c.out
204203
assert "Found hello lib" in c.out
204+
205+
@pytest.mark.parametrize("require_type", ["requires", "tool_requires"])
206+
def test_include_modules(self, require_type):
207+
"""Test that cmake module files in builddirs of requires and tool_requires
208+
are accessible with include() in consumer CMakeLists
209+
"""
210+
c = TestClient()
211+
conanfile = textwrap.dedent("""
212+
from conan import ConanFile
213+
from conan.tools.files import copy
214+
class TestConan(ConanFile):
215+
exports_sources = "*"
216+
def package(self):
217+
copy(self, "*", self.source_folder, self.package_folder)
218+
def package_info(self):
219+
self.cpp_info.builddirs.append("cmake")
220+
""")
221+
c.save({"conanfile.py": conanfile,
222+
"cmake/myowncmake.cmake": 'MESSAGE("MYOWNCMAKE FROM hello!")'})
223+
c.run("create . --name=hello --version=0.1")
224+
225+
conanfile = textwrap.dedent(f"""
226+
from conan import ConanFile
227+
from conan.tools.cmake import CMake
228+
class PkgConan(ConanFile):
229+
settings = "os", "compiler", "arch", "build_type"
230+
{require_type} = "hello/0.1"
231+
generators = "CMakeToolchain", "CMakeConfigDeps"
232+
233+
def build(self):
234+
cmake = CMake(self)
235+
cmake.configure()
236+
cmake.build()
237+
""")
238+
consumer = textwrap.dedent("""
239+
cmake_minimum_required(VERSION 3.15)
240+
project(MyHello NONE)
241+
include(myowncmake)
242+
""")
243+
c.save({"conanfile.py": conanfile,
244+
"CMakeLists.txt": consumer}, clean_first=True)
245+
c.run(f"build . -c tools.cmake.cmakedeps:new={new_value}")
246+
assert "MYOWNCMAKE FROM hello!" in c.out
247+
248+
def test_include_modules_both_build_host(self):
249+
c = TestClient()
250+
conanfile = textwrap.dedent("""
251+
from conan import ConanFile
252+
from conan.tools.files import copy
253+
class TestConan(ConanFile):
254+
exports_sources = "*"
255+
def package(self):
256+
copy(self, "*", self.source_folder, self.package_folder)
257+
def package_info(self):
258+
self.cpp_info.builddirs.append("cmake")
259+
""")
260+
c.save({"conanfile.py": conanfile,
261+
"cmake/myowncmake.cmake": 'MESSAGE("MYOWNCMAKE FROM hello!")'})
262+
c.run("create . --name=hello --version=0.1")
263+
264+
conanfile = textwrap.dedent(f"""
265+
from conan import ConanFile
266+
from conan.tools.cmake import CMake
267+
class PkgConan(ConanFile):
268+
settings = "os", "compiler", "arch", "build_type"
269+
requires = "hello/0.1"
270+
tool_requires = "hello/0.1"
271+
generators = "CMakeToolchain", "CMakeConfigDeps"
272+
273+
def build(self):
274+
cmake = CMake(self)
275+
cmake.configure()
276+
cmake.build()
277+
""")
278+
consumer = textwrap.dedent("""
279+
cmake_minimum_required(VERSION 3.15)
280+
project(MyHello NONE)
281+
include(myowncmake)
282+
""")
283+
c.save({"conanfile.py": conanfile,
284+
"CMakeLists.txt": consumer}, clean_first=True)
285+
c.run(f"build . -c tools.cmake.cmakedeps:new={new_value}")
286+
assert "conanfile.py: There is already a 'hello/0.1' package " \
287+
"contributing to CMAKE_MODULE_PATH" in c.out
288+
assert "MYOWNCMAKE FROM hello!" in c.out

0 commit comments

Comments
 (0)