@@ -113,13 +113,59 @@ set(PYBIND11_FINDPYTHON
113
113
${_pybind11_findpython_default}
114
114
CACHE STRING "Force new FindPython - NEW, OLD, COMPAT" )
115
115
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
+
123
169
endif ()
124
170
125
171
# NB: when adding a header don't forget to also add it to setup.py
0 commit comments