Skip to content

Commit 70ff22a

Browse files
committed
cmake 4.2 and cps full round trip
1 parent a8fac02 commit 70ff22a

File tree

6 files changed

+141
-16
lines changed

6 files changed

+141
-16
lines changed

.ci/docker/conan-tests

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ ENV PY37=3.7.9 \
1717
CMAKE_3_19=/usr/share/cmake-3.19.7/bin/cmake \
1818
CMAKE_3_23=/usr/share/cmake-3.23.5/bin/cmake \
1919
CMAKE_3_27=/usr/share/cmake-3.27.9/bin/cmake \
20-
CMAKE_4_0=/usr/share/cmake-4.0.0-rc3/bin/cmake \
20+
CMAKE_4_2=/usr/share/cmake-4.2.1/bin/cmake \
2121
GCC_9=/usr/bin/gcc-9 \
2222
GXX_9=/usr/bin/g++-9 \
2323
GCC_11=/usr/bin/gcc-11 \
@@ -123,15 +123,15 @@ RUN wget https://github.com/Kitware/CMake/releases/download/v3.15.7/cmake-3.15.7
123123
tar -xvzf cmake-3.23.5-Linux-x86_64.tar.gz && mv cmake-3.23.5-linux-x86_64/ /usr/share/cmake-3.23.5 && \
124124
wget https://github.com/Kitware/CMake/releases/download/v3.27.9/cmake-3.27.9-Linux-x86_64.tar.gz && \
125125
tar -xvzf cmake-3.27.9-Linux-x86_64.tar.gz && mv cmake-3.27.9-linux-x86_64/ /usr/share/cmake-3.27.9 && \
126-
wget https://cmake.org/files/v4.0/cmake-4.0.0-rc3-linux-x86_64.tar.gz && \
127-
tar -xvzf cmake-4.0.0-rc3-linux-x86_64.tar.gz && mv cmake-4.0.0-rc3-linux-x86_64/ /usr/share/cmake-4.0.0-rc3 && \
126+
wget https://cmake.org/files/v4.0/cmake-4.2.1-linux-x86_64.tar.gz && \
127+
tar -xvzf cmake-4.2.1-linux-x86_64.tar.gz && mv cmake-4.2.1-linux-x86_64/ /usr/share/cmake-4.2.1 && \
128128
update-alternatives --install /usr/bin/cmake cmake $CMAKE_3_15 10 && \
129129
update-alternatives --install /usr/bin/cmake cmake $CMAKE_3_16 20 && \
130130
update-alternatives --install /usr/bin/cmake cmake $CMAKE_3_17 30 && \
131131
update-alternatives --install /usr/bin/cmake cmake $CMAKE_3_19 40 && \
132132
update-alternatives --install /usr/bin/cmake cmake $CMAKE_3_23 50 && \
133133
update-alternatives --install /usr/bin/cmake cmake $CMAKE_3_27 60 && \
134-
update-alternatives --install /usr/bin/cmake cmake $CMAKE_4_0 70 && \
134+
update-alternatives --install /usr/bin/cmake cmake $CMAKE_4_2 70 && \
135135
# set CMake 3.15 as default
136136
update-alternatives --set cmake $CMAKE_3_15
137137

.github/workflows/osx-tests.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ jobs:
5151
~/Applications/CMake/3.19.7
5252
~/Applications/CMake/3.23.5
5353
~/Applications/CMake/3.27.9
54-
~/Applications/CMake/4.0.0-rc3
54+
~/Applications/CMake/4.2.1
5555
~/Applications/bazel/6.5.0
5656
~/Applications/bazel/7.6.2
5757
~/Applications/bazel/8.4.2
@@ -80,7 +80,7 @@ jobs:
8080
if: steps.cache-tools.outputs.cache-hit != 'true'
8181
run: |
8282
set -e
83-
CMAKE_PRECOMP_VERSIONS=("3.19.7" "3.23.5" "3.27.9" "4.0.0-rc3")
83+
CMAKE_PRECOMP_VERSIONS=("3.19.7" "3.23.5" "3.27.9" "4.2.1")
8484
for version in "${CMAKE_PRECOMP_VERSIONS[@]}"; do
8585
echo "Downloading and installing precompiled universal CMake version ${version}..."
8686
wget -q --no-check-certificate https://cmake.org/files/v${version%.*}/cmake-${version}-macos-universal.tar.gz
@@ -134,7 +134,7 @@ jobs:
134134
~/Applications/CMake/3.19.7
135135
~/Applications/CMake/3.23.5
136136
~/Applications/CMake/3.27.9
137-
~/Applications/CMake/4.0.0-rc3
137+
~/Applications/CMake/4.2.1
138138
~/Applications/bazel/6.5.0
139139
~/Applications/bazel/7.6.2
140140
~/Applications/bazel/8.4.2

.github/workflows/win-tests.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ jobs:
162162
C:\tools\cmake\3.19.7
163163
C:\tools\cmake\3.23.5
164164
C:\tools\cmake\3.27.9
165-
C:\tools\cmake\4.0.0-rc3
165+
C:\tools\cmake\4.2.1
166166
C:\tools\bazel\6.5.0
167167
C:\tools\bazel\7.6.2
168168
C:\tools\bazel\8.4.2
@@ -171,7 +171,7 @@ jobs:
171171
- name: Install CMake versions
172172
if: steps.cache-tools.outputs.cache-hit != 'true'
173173
run: |
174-
$CMAKE_BUILD_VERSIONS = "3.15.7", "3.19.7", "3.23.5", "3.27.9", "4.0.0-rc3"
174+
$CMAKE_BUILD_VERSIONS = "3.15.7", "3.19.7", "3.23.5", "3.27.9", "4.2.1"
175175
foreach ($version in $CMAKE_BUILD_VERSIONS) {
176176
Write-Host "Downloading CMake version $version for Windows..."
177177
$destination = "C:\tools\cmake\$version"

conan/tools/cmake/cmakedeps2/cmakedeps.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -266,11 +266,15 @@ def generate(self):
266266
build_dir = dep.package_folder
267267
pkg_folder = build_dir.replace("\\", "/") if build_dir else None
268268
if pkg_folder:
269-
f = self._cmakedeps.get_cmake_filename(dep)
270-
for filename in (f"{f}-config.cmake", f"{f}Config.cmake"):
269+
# prioritize cps if existing as CMake does
270+
for filename in (f"{pkg_name}.cps", f"cps/{pkg_name}.cps",
271+
f"{pkg_name}-config.cmake", f"{pkg_name}Config.cmake"):
271272
if os.path.isfile(os.path.join(pkg_folder, filename)):
272-
pkg_paths[pkg_name] = relativize_path(pkg_folder, self._conanfile,
273+
loc = os.path.dirname(os.path.join(pkg_folder, filename))
274+
loc = loc.replace("\\", "/")
275+
pkg_paths[pkg_name] = relativize_path(loc, self._conanfile,
273276
"${CMAKE_CURRENT_LIST_DIR}")
277+
break
274278

275279
existing_paths = pkg_paths_multi.setdefault(pkg_name, [])
276280
if pkg_folder not in existing_paths:

test/conftest.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -93,10 +93,10 @@
9393
'Darwin': '/Users/runner/Applications/CMake/3.27.9/bin',
9494
'Linux': "/usr/share/cmake-3.27.9/bin"}
9595
},
96-
"4.0": {
97-
"path": {'Windows': 'C:/tools/cmake/4.0.0-rc3/cmake-4.0.0-rc3-windows-x86_64/bin',
98-
'Darwin': '/Users/runner/Applications/CMake/4.0.0-rc3/bin',
99-
'Linux': "/usr/share/cmake-4.0.0-rc3/bin"}
96+
"4.2": {
97+
"path": {'Windows': 'C:/tools/cmake/4.2.1/cmake-4.2.1-windows-x86_64/bin',
98+
'Darwin': '/Users/runner/Applications/CMake/4.2.1/bin',
99+
'Linux': "/usr/share/cmake-4.2.1/bin"}
100100
}
101101
},
102102
'ninja': {
Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
import os
2+
import shutil
3+
import textwrap
4+
5+
import pytest
6+
7+
from conan.test.utils.tools import TestClient
8+
9+
10+
@pytest.mark.tool("cmake", "4.2")
11+
def test_cps():
12+
c = TestClient()
13+
c.run("new cmake_lib")
14+
conanfile = textwrap.dedent("""\
15+
from conan import ConanFile
16+
from conan.tools.cmake import CMakeToolchain, CMake, cmake_layout
17+
18+
class mypkgRecipe(ConanFile):
19+
name = "mypkg"
20+
version = "0.1"
21+
package_type = "library"
22+
23+
settings = "os", "compiler", "build_type", "arch"
24+
options = {"shared": [True, False], "fPIC": [True, False]}
25+
default_options = {"shared": False, "fPIC": True}
26+
27+
exports_sources = "CMakeLists.txt", "src/*", "include/*"
28+
implements = ["auto_shared_fpic"]
29+
generators = "CMakeToolchain"
30+
31+
def layout(self):
32+
cmake_layout(self)
33+
34+
def build(self):
35+
cmake = CMake(self)
36+
cmake.configure()
37+
cmake.build()
38+
39+
def package(self):
40+
cmake = CMake(self)
41+
cmake.install()
42+
43+
def package_info(self):
44+
from conan.cps import CPS
45+
self.cpp_info = CPS.load("cps/mypkg.cps").to_conan()
46+
""")
47+
48+
cmake = textwrap.dedent("""\
49+
cmake_minimum_required(VERSION 4.2)
50+
project(mypkg CXX)
51+
52+
set(CMAKE_EXPERIMENTAL_EXPORT_PACKAGE_INFO "b80be207-778e-46ba-8080-b23bba22639e")
53+
54+
add_library(mypkg src/mypkg.cpp)
55+
target_include_directories(mypkg PUBLIC
56+
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
57+
$<INSTALL_INTERFACE:include>)
58+
59+
set_target_properties(mypkg PROPERTIES PUBLIC_HEADER "include/mypkg.h")
60+
install(TARGETS mypkg EXPORT mypkg)
61+
62+
install(PACKAGE_INFO mypkg EXPORT mypkg)
63+
""")
64+
65+
# First, try with the standard mypkg-config.cmake consumption
66+
c.save({"conanfile.py": conanfile,
67+
"CMakeLists.txt": cmake})
68+
c.run("create")
69+
assert "mypkg/0.1: Hello World Release!" in c.out
70+
71+
# Lets consume directly with CPS
72+
test_cmake = textwrap.dedent("""\
73+
cmake_minimum_required(VERSION 4.2)
74+
project(PackageTest CXX)
75+
76+
set(CMAKE_EXPERIMENTAL_FIND_CPS_PACKAGES e82e467b-f997-4464-8ace-b00808fff261)
77+
78+
find_package(mypkg CONFIG REQUIRED)
79+
80+
add_executable(example src/example.cpp)
81+
target_link_libraries(example mypkg::mypkg)
82+
""")
83+
test_conanfile = textwrap.dedent("""\
84+
import os
85+
86+
from conan import ConanFile
87+
from conan.tools.cmake import CMake, cmake_layout, CMakeToolchain, CMakeConfigDeps
88+
from conan.tools.build import can_run
89+
90+
91+
class TestConan(ConanFile):
92+
settings = "os", "compiler", "build_type", "arch"
93+
94+
def requirements(self):
95+
self.requires(self.tested_reference_str)
96+
97+
def generate(self):
98+
deps = CMakeConfigDeps(self)
99+
deps.set_property("mypkg", "cmake_find_mode", "none")
100+
deps.generate()
101+
tc = CMakeToolchain(self)
102+
tc.generate()
103+
104+
def build(self):
105+
cmake = CMake(self)
106+
cmake.configure()
107+
cmake.build()
108+
109+
def layout(self):
110+
cmake_layout(self)
111+
112+
def test(self):
113+
if can_run(self):
114+
cmd = os.path.join(self.cpp.build.bindir, "example")
115+
self.run(cmd, env="conanrun")
116+
""")
117+
shutil.rmtree(os.path.join(c.current_folder, "test_package", "build"))
118+
c.save({"test_package/conanfile.py": test_conanfile,
119+
"test_package/CMakeLists.txt": test_cmake})
120+
c.run("create --build=missing -c tools.cmake.cmakedeps:new=will_break_next")
121+
assert "mypkg/0.1: Hello World Release!" in c.out

0 commit comments

Comments
 (0)