Skip to content

Commit a1e0d30

Browse files
authored
cmake 4.2 and cps full round trip (#19410)
* cmake 4.2 and cps full round trip * fix * fix emsdk build * fix locations
1 parent a8fac02 commit a1e0d30

File tree

6 files changed

+147
-14
lines changed

6 files changed

+147
-14
lines changed

.ci/docker/conan-tests

Lines changed: 6 additions & 5 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 \
@@ -27,7 +27,7 @@ ENV PY37=3.7.9 \
2727
BAZEL_6=6.5.0 \
2828
BAZEL_7=7.6.2 \
2929
BAZEL_8=8.4.2 \
30-
EMSDK=4.0.10
30+
EMSDK=4.0.22
3131

3232
RUN apt-get update && \
3333
apt-get install -y --no-install-recommends \
@@ -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.2/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

@@ -175,6 +175,7 @@ RUN cd /tmp && \
175175
wget https://github.com/emscripten-core/emsdk/archive/refs/tags/${EMSDK}.tar.gz && \
176176
tar xzf ${EMSDK}.tar.gz --directory /usr/share && \
177177
cd /usr/share/emsdk-${EMSDK} && \
178+
pyenv local 3.12 && \
178179
./emsdk update && \
179180
./emsdk install latest && \
180181
./emsdk activate latest --permanent && \

.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: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import glob
12
import os
23
import re
34
import textwrap
@@ -258,6 +259,14 @@ def generate(self):
258259
pkg_name = self._cmakedeps.get_cmake_filename(dep)
259260
# https://cmake.org/cmake/help/v3.22/guide/using-dependencies/index.html
260261
if cmake_find_mode == FIND_MODE_NONE:
262+
cps = glob.glob(f"**/{pkg_name}.cps", root_dir=dep.package_folder, recursive=True)
263+
if cps:
264+
loc = os.path.dirname(os.path.join(dep.package_folder, cps[0]))
265+
loc = loc.replace("\\", "/")
266+
pkg_paths[pkg_name] = relativize_path(loc, self._conanfile,
267+
"${CMAKE_CURRENT_LIST_DIR}")
268+
continue
269+
261270
try:
262271
# This is irrespective of the components, it should be in the root cpp_info
263272
# To define the location of the pkg-config.cmake file

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

0 commit comments

Comments
 (0)