Skip to content

Commit 29949c4

Browse files
committed
chore(cmake): add CMake presets
Signed-off-by: Henry Schreiner <[email protected]>
1 parent c125cc7 commit 29949c4

File tree

3 files changed

+132
-22
lines changed

3 files changed

+132
-22
lines changed

.github/workflows/ci.yml

Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -211,25 +211,14 @@ jobs:
211211
with:
212212
fetch-depth: 0
213213

214-
- name: Prepare venv
215-
run: python3.13t -m venv .venv
216-
217-
- name: Install Python deps
218-
run: .venv/bin/pip install -r tests/requirements.txt
219-
220-
- name: Configure C++11
221-
run: >
222-
cmake -S. -Bbuild
223-
-DPYBIND11_WERROR=ON
224-
-DDOWNLOAD_CATCH=ON
225-
-DDOWNLOAD_EIGEN=ON
226-
-DPython_ROOT_DIR=.venv
214+
- name: Configure via preset
215+
run: cmake --preset venv -DPYBIND11_CREATE_WITH_UV=python3.13t
227216

228217
- name: Build C++11
229-
run: cmake --build build -j2
218+
run: cmake --build --preset venv
230219

231220
- name: Python tests C++11
232-
run: cmake --build build --target pytest -j2
221+
run: cmake --build --preset venvtests
233222

234223
deadsnakes:
235224
strategy:

CMakeLists.txt

Lines changed: 53 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -113,13 +113,59 @@ set(PYBIND11_FINDPYTHON
113113
${_pybind11_findpython_default}
114114
CACHE STRING "Force new FindPython - NEW, OLD, COMPAT")
115115

116-
# Allow PYTHON_EXECUTABLE if in FINDPYTHON mode and building pybind11's tests
117-
# (makes transition easier while we support both modes).
118-
if(PYBIND11_MASTER_PROJECT
119-
AND PYBIND11_FINDPYTHON
120-
AND DEFINED PYTHON_EXECUTABLE
121-
AND NOT DEFINED Python_EXECUTABLE)
122-
set(Python_EXECUTABLE "${PYTHON_EXECUTABLE}")
116+
if(PYBIND11_MASTER_PROJECT)
117+
118+
# Allow PYTHON_EXECUTABLE if in FINDPYTHON mode and building pybind11's tests
119+
# (makes transition easier while we support both modes).
120+
if(PYBIND11_FINDPYTHON
121+
AND DEFINED PYTHON_EXECUTABLE
122+
AND NOT DEFINED Python_EXECUTABLE)
123+
set(Python_EXECUTABLE "${PYTHON_EXECUTABLE}")
124+
endif()
125+
126+
if(NOT DEFINED Python3_EXECUTABLE
127+
AND NOT DEFINED Python_EXECUTABLE
128+
AND NOT DEFINED Python_ROOT_SIR
129+
AND EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/.venv")
130+
message(STATUS "Autodetecting Python in virtual environment")
131+
set(Python_ROOT_DIR "${CMAKE_CURRENT_SOURCE_DIR}/.venv")
132+
endif()
133+
134+
# This is a shortcut that is primarily for the venv cmake preset,
135+
# but can be used to quickly setup tests manually, too
136+
set(PYBIND11_CREATE_WITH_UV
137+
""
138+
CACHE STRING "Create a virtualenv in Python_ROOT_DIR with uv if it doesn't exist")
139+
140+
if(NOT PYBIND11_CREATE_WITH_UV STREQUAL "")
141+
if(NOT DEFINED Python_ROOT_DIR)
142+
message(FATAL_ERROR "Python_ROOT_DIR must be defined to use PYBIND11_CREATE_WITH_UV")
143+
endif()
144+
if(EXISTS "${Python_ROOT_DIR}")
145+
message(STATUS "Using existing venv at ${Python_ROOT_DIR}, remove to recreate")
146+
else()
147+
find_program(UV uv REQUIRED)
148+
# CMake 3.19+ would be able to use COMMAND_ERROR_IS_FATAL
149+
message(
150+
STATUS "Creating venv with ${UV} venv -p ${PYBIND11_CREATE_WITH_UV} '${Python_ROOT_DIR}'")
151+
execute_process(COMMAND ${UV} venv -p ${PYBIND11_CREATE_WITH_UV} "${Python_ROOT_DIR}"
152+
RESULT_VARIABLE _venv_result)
153+
if(_venv_result AND NOT _venv_result EQUAL 0)
154+
message(FATAL_ERROR "uv venv failed with '${_venv_result}'")
155+
endif()
156+
message(
157+
STATUS
158+
"Installing deps with ${UV} pip install -p '${Python_ROOT_DIR}' -r tests/requirements.txt"
159+
)
160+
execute_process(
161+
COMMAND ${UV} pip install -p "${Python_ROOT_DIR}" -r
162+
"${CMAKE_CURRENT_SOURCE_DIR}/tests/requirements.txt" RESULT_VARIABLE _pip_result)
163+
if(_pip_result AND NOT _pip_result EQUAL 0)
164+
message(FATAL_ERROR "uv pip install failed with '${_pip_result}'")
165+
endif()
166+
endif()
167+
endif()
168+
123169
endif()
124170

125171
# NB: when adding a header don't forget to also add it to setup.py

CMakePresets.json

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
{
2+
"version": 6,
3+
"configurePresets": [
4+
{
5+
"name": "default",
6+
"displayName": "Default",
7+
"binaryDir": "build/default",
8+
"generator": "Ninja",
9+
"cacheVariables": {
10+
"PYBIND11_FINDPYTHON": "NEW",
11+
"PYBIND11_WERROR": true,
12+
"DOWNLOAD_CATCH": true,
13+
"DOWNLOAD_EIGEN": true
14+
}
15+
},
16+
{
17+
"name": "venv",
18+
"displayName": "Venv",
19+
"binaryDir": "build/venv",
20+
"generator": "Ninja",
21+
"cacheVariables": {
22+
"PYBIND11_CREATE_WITH_UV": "python3",
23+
"Python_ROOT_DIR": ".venv",
24+
"PYBIND11_WERROR": true,
25+
"PYBIND11_FINDPYTHON": "NEW",
26+
"DOWNLOAD_CATCH": true,
27+
"DOWNLOAD_EIGEN": true
28+
}
29+
}
30+
],
31+
"buildPresets": [
32+
{
33+
"name": "default",
34+
"displayName": "Default Build",
35+
"configurePreset": "default"
36+
},
37+
{
38+
"name": "venv",
39+
"displayName": "Venv Build",
40+
"configurePreset": "venv"
41+
},
42+
{
43+
"name": "tests",
44+
"displayName": "Default Tests Build",
45+
"configurePreset": "default",
46+
"targets": ["pytest", "cpptest", "test_cmake_build"]
47+
},
48+
{
49+
"name": "testsvenv",
50+
"displayName": "Venv Tests Build",
51+
"configurePreset": "venv",
52+
"targets": ["pytest", "cpptest", "test_cmake_build"]
53+
}
54+
],
55+
"workflowPresets": [
56+
{
57+
"name": "default",
58+
"displayName": "Default Workflow",
59+
"steps": [
60+
{ "type": "configure", "name": "default" },
61+
{ "type": "build", "name": "default" },
62+
{ "type": "build", "name": "tests" }
63+
]
64+
},
65+
{
66+
"name": "venv",
67+
"displayName": "Default Workflow",
68+
"steps": [
69+
{ "type": "configure", "name": "venv" },
70+
{ "type": "build", "name": "venv" },
71+
{ "type": "build", "name": "testsvenv" }
72+
]
73+
}
74+
]
75+
}

0 commit comments

Comments
 (0)