Skip to content

Commit 1729aae

Browse files
authored
feat: new FindPython support (#2370)
* feat: FindPython support * refactor: rename to PYBIND11_FINDPYTHON * docs: Caps fixes * feat: NOPYTHON mode * test: check simple call * docs: add changelog/upgrade guide * feat: Support Python3 and Python2 * refactor: Use targets in tests * fix: support CMake 3.4+ * feat: classic search also finds virtual environments * docs: some updates from @wjakob's review * fix: wrong name for QUIET mode variable, reported by @skoslowski * refactor: cleaner output messaging * fix: support debug Python's in FindPython mode too * fixup! refactor: cleaner output messaging * fix: missing pybind11_FOUND and pybind11_INCLUDE_DIR restored to subdir mode * fix: nicer reporting of Python / PyPy * fix: out-of-order variable fix * docs: minor last-minute cleanup
1 parent 69821d9 commit 1729aae

24 files changed

+1115
-393
lines changed

.github/workflows/ci.yml

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,13 @@ jobs:
3131
arch: x64
3232
max-cxx-std: 17
3333
dev: false
34+
args: "-DPYBIND11_FINDPYTHON=ON"
3435
- runs-on: macos-latest
3536
python: 3.7
3637
arch: x64
3738
max-cxx-std: 17
3839
dev: false
40+
args: "-DPYBIND11_FINDPYTHON=ON"
3941
- runs-on: windows-2016
4042
python: 3.7
4143
arch: x86
@@ -46,6 +48,7 @@ jobs:
4648
arch: x64
4749
max-cxx-std: 17
4850
dev: false
51+
args: "-DPYBIND11_FINDPYTHON=ON"
4952
- runs-on: windows-latest
5053
python: 3.7
5154
arch: x64
@@ -89,7 +92,7 @@ jobs:
8992
dev: false
9093

9194

92-
name: "🐍 ${{ matrix.python }} • ${{ matrix.runs-on }} • ${{ matrix.arch }}"
95+
name: "🐍 ${{ matrix.python }} • ${{ matrix.runs-on }} • ${{ matrix.arch }} ${{ matrix.args }}"
9396
runs-on: ${{ matrix.runs-on }}
9497
continue-on-error: ${{ matrix.dev }}
9598

@@ -106,6 +109,9 @@ jobs:
106109
if: runner.os != 'macOS'
107110
run: echo "::set-env name=BOOST_ROOT::$BOOST_ROOT_1_72_0"
108111

112+
- name: Update CMake
113+
uses: jwlawson/[email protected]
114+
109115
- name: Cache wheels
110116
if: runner.os == 'macOS'
111117
uses: actions/cache@v2
@@ -120,15 +126,15 @@ jobs:
120126
- name: Prepare env
121127
run: python -m pip install -r tests/requirements.txt
122128

123-
- name: Configure C++11
129+
- name: Configure C++11 ${{ matrix.args }}
124130
shell: bash
125131
run: >
126132
cmake -S . -B build
127133
-DPYBIND11_WERROR=ON
128134
-DDOWNLOAD_CATCH=ON
129135
-DDOWNLOAD_EIGEN=ON
130136
-DCMAKE_CXX_STANDARD=11
131-
-DPYTHON_EXECUTABLE=$(python -c "import sys; print(sys.executable)")
137+
${{ matrix.args }}
132138
133139
- name: Build C++11
134140
run: cmake --build build -j 2
@@ -140,17 +146,17 @@ jobs:
140146
run: cmake --build build --target cpptest -j 2
141147

142148
- name: Interface test C++11
143-
run: cmake --build build --target test_cmake_build
149+
run: cmake --build build --target test_cmake_build -v
144150

145-
- name: Configure C++${{ matrix.max-cxx-std }}
151+
- name: Configure C++${{ matrix.max-cxx-std }} ${{ matrix.args }}
146152
shell: bash
147153
run: >
148154
cmake -S . -B build2
149155
-DPYBIND11_WERROR=ON
150156
-DDOWNLOAD_CATCH=ON
151157
-DDOWNLOAD_EIGEN=ON
152158
-DCMAKE_CXX_STANDARD=${{ matrix.max-cxx-std }}
153-
-DPYTHON_EXECUTABLE=$(python -c "import sys; print(sys.executable)")
159+
${{ matrix.args }}
154160
155161
- name: Build C++${{ matrix.max-cxx-std }}
156162
run: cmake --build build2 -j 2
@@ -350,14 +356,14 @@ jobs:
350356
steps:
351357
- uses: actions/checkout@v2
352358

353-
- name: Install requirements
359+
- name: Install system requirements
354360
run: apk add doxygen python3-dev
355361

356362
- name: Ensure pip
357363
run: python3 -m ensurepip
358364

359-
- name: Install python docs requirements
360-
run: python3 -m pip install "sphinx<3" sphinx_rtd_theme breathe==4.13.1 pytest setuptools
365+
- name: Install docs & setup requirements
366+
run: python3 -m pip install -r docs/requirements.txt pytest setuptools
361367

362368
- name: Build docs
363369
run: python3 -m sphinx -W -b html docs docs/.build

.github/workflows/configure.yml

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,19 +14,22 @@ jobs:
1414
strategy:
1515
fail-fast: false
1616
matrix:
17-
runs-on: [ubuntu-latest, macos-latest]
17+
runs-on: [ubuntu-latest, macos-latest, windows-latest]
1818
arch: [x64]
19-
cmake: [3.7, 3.18]
19+
cmake: [3.18]
2020

2121
include:
22-
- runs-on: windows-latest
22+
- runs-on: ubuntu-latest
2323
arch: x64
24-
cmake: 3.18
24+
cmake: 3.4
25+
26+
- runs-on: macos-latest
27+
arch: x64
28+
cmake: 3.7
2529

26-
# TODO: 3.8
2730
- runs-on: windows-2016
2831
arch: x86
29-
cmake: 3.11
32+
cmake: 3.8
3033

3134
- runs-on: windows-2016
3235
arch: x86
@@ -63,3 +66,13 @@ jobs:
6366
-DPYBIND11_WERROR=ON
6467
-DDOWNLOAD_CATCH=ON
6568
-DPYTHON_EXECUTABLE=$(python -c "import sys; print(sys.executable)")
69+
70+
- name: Build
71+
working-directory: build dir
72+
if: github.event_name == 'workflow_dispatch'
73+
run: cmake --build . --config Release
74+
75+
- name: Test
76+
working-directory: build dir
77+
if: github.event_name == 'workflow_dispatch'
78+
run: cmake --build . --config Release --target check

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ MANIFEST
3232
.*.swp
3333
.DS_Store
3434
/dist
35-
/build
35+
/build*
3636
.cache/
3737
sosize-*.txt
3838
pybind11Config*.cmake

CMakeLists.txt

Lines changed: 63 additions & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@
55
# All rights reserved. Use of this source code is governed by a
66
# BSD-style license that can be found in the LICENSE file.
77

8-
cmake_minimum_required(VERSION 3.7)
8+
cmake_minimum_required(VERSION 3.4)
99

10-
# The `cmake_minimum_required(VERSION 3.7...3.18)` syntax does not work with
10+
# The `cmake_minimum_required(VERSION 3.4...3.18)` syntax does not work with
1111
# some versions of VS that have a patched CMake 3.11. This forces us to emulate
1212
# the behavior using the following workaround:
1313
if(${CMAKE_VERSION} VERSION_LESS 3.18)
@@ -41,11 +41,22 @@ include(GNUInstallDirs)
4141
include(CMakePackageConfigHelpers)
4242
include(CMakeDependentOption)
4343

44-
message(STATUS "pybind11 v${pybind11_VERSION} ${pybind11_VERSION_TYPE}")
44+
if(NOT pybind11_FIND_QUIETLY)
45+
message(STATUS "pybind11 v${pybind11_VERSION} ${pybind11_VERSION_TYPE}")
46+
endif()
4547

4648
# Check if pybind11 is being used directly or via add_subdirectory
4749
if(CMAKE_CURRENT_SOURCE_DIR STREQUAL CMAKE_SOURCE_DIR)
4850
set(PYBIND11_MASTER_PROJECT ON)
51+
52+
if(OSX AND CMAKE_VERSION VERSION_LESS 3.7)
53+
# Bug in macOS CMake < 3.7 is unable to download catch
54+
message(WARNING "CMAKE 3.7+ needed on macOS to download catch, and newer HIGHLY recommended")
55+
elseif(WINDOWS AND CMAKE_VERSION VERSION_LESS 3.8)
56+
# Only tested with 3.8+ in CI.
57+
message(WARNING "CMAKE 3.8+ tested on Windows, previous versions untested")
58+
endif()
59+
4960
message(STATUS "CMake ${CMAKE_VERSION}")
5061

5162
if(CMAKE_CXX_STANDARD)
@@ -60,13 +71,16 @@ endif()
6071
# Options
6172
option(PYBIND11_INSTALL "Install pybind11 header files?" ${PYBIND11_MASTER_PROJECT})
6273
option(PYBIND11_TEST "Build pybind11 test suite?" ${PYBIND11_MASTER_PROJECT})
63-
option(PYBIND11_CLASSIC_LTO "Use the classic LTO flag algorithm, even on CMake 3.9+" OFF)
74+
option(PYBIND11_NOPYTHON "Disable search for Python" OFF)
6475

6576
cmake_dependent_option(
6677
USE_PYTHON_INCLUDE_DIR
6778
"Install pybind11 headers in Python include directory instead of default installation prefix"
6879
OFF "PYBIND11_INSTALL" OFF)
6980

81+
cmake_dependent_option(PYBIND11_FINDPYTHON "Force new FindPython" OFF
82+
"NOT CMAKE_VERSION VERSION_LESS 3.12" OFF)
83+
7084
# NB: when adding a header don't forget to also add it to setup.py
7185
set(PYBIND11_HEADERS
7286
include/pybind11/detail/class.h
@@ -118,101 +132,41 @@ endif()
118132
string(REPLACE "include/" "${CMAKE_CURRENT_SOURCE_DIR}/include/" PYBIND11_HEADERS
119133
"${PYBIND11_HEADERS}")
120134

121-
# Classic mode
122-
123-
include("${CMAKE_CURRENT_LIST_DIR}/tools/pybind11Tools.cmake")
124-
125135
# Cache variables so pybind11_add_module can be used in parent projects
126136
set(PYBIND11_INCLUDE_DIR
127137
"${CMAKE_CURRENT_LIST_DIR}/include"
128138
CACHE INTERNAL "")
129-
set(PYTHON_INCLUDE_DIRS
130-
${PYTHON_INCLUDE_DIRS}
131-
CACHE INTERNAL "")
132-
set(PYTHON_LIBRARIES
133-
${PYTHON_LIBRARIES}
134-
CACHE INTERNAL "")
135-
set(PYTHON_MODULE_PREFIX
136-
${PYTHON_MODULE_PREFIX}
137-
CACHE INTERNAL "")
138-
set(PYTHON_MODULE_EXTENSION
139-
${PYTHON_MODULE_EXTENSION}
140-
CACHE INTERNAL "")
141-
set(PYTHON_VERSION_MAJOR
142-
${PYTHON_VERSION_MAJOR}
143-
CACHE INTERNAL "")
144-
set(PYTHON_VERSION_MINOR
145-
${PYTHON_VERSION_MINOR}
146-
CACHE INTERNAL "")
147-
set(PYTHON_IS_DEBUG
148-
"${PYTHON_IS_DEBUG}"
149-
CACHE INTERNAL "")
150-
151-
if(USE_PYTHON_INCLUDE_DIR)
152-
file(RELATIVE_PATH CMAKE_INSTALL_INCLUDEDIR ${CMAKE_INSTALL_PREFIX} ${PYTHON_INCLUDE_DIRS})
153-
endif()
154139

155140
# Note: when creating targets, you cannot use if statements at configure time -
156141
# you need generator expressions, because those will be placed in the target file.
157142
# You can also place ifs *in* the Config.in, but not here.
158143

159-
# Build an interface library target:
160-
add_library(pybind11 INTERFACE)
161-
add_library(pybind11::pybind11 ALIAS pybind11) # to match exported target
162-
163-
target_include_directories(
164-
pybind11 ${pybind11_system} INTERFACE $<BUILD_INTERFACE:${PYBIND11_INCLUDE_DIR}>
165-
$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>)
166-
# Only add Python for build - must be added during the import for config since it has to be re-discovered.
167-
target_include_directories(pybind11 SYSTEM INTERFACE $<BUILD_INTERFACE:${PYTHON_INCLUDE_DIRS}>)
168-
169-
if(CMAKE_VERSION VERSION_LESS 3.13)
170-
target_compile_features(pybind11 INTERFACE cxx_inheriting_constructors cxx_user_literals
171-
cxx_right_angle_brackets)
172-
else()
173-
# This was added in CMake 3.8, but we are keeping a consistent breaking
174-
# point for the config file at 3.13. A config generated by CMake 3.13+
175-
# can only be read in 3.13+ due to the SHELL usage later, so this is safe to do.
176-
target_compile_features(pybind11 INTERFACE cxx_std_11)
177-
endif()
144+
# This section builds targets, but does *not* touch Python
178145

179-
add_library(module INTERFACE)
180-
add_library(pybind11::module ALIAS module)
146+
# Build the headers-only target (no Python included):
147+
add_library(headers INTERFACE)
148+
add_library(pybind11::headers ALIAS headers) # to match exported target
181149

182-
target_link_libraries(module INTERFACE pybind11::pybind11)
150+
include("${CMAKE_CURRENT_SOURCE_DIR}/tools/pybind11Common.cmake")
183151

184-
# See https://github.com/Kitware/CMake/blob/master/Modules/CMakePlatformId.h.in for platform IDs
185-
# Note: CMake 3.15 allows $<PLATFORM_ID:Windows,Cygwin>
186-
target_link_libraries(
187-
module
188-
INTERFACE
189-
"$<$<OR:$<PLATFORM_ID:Windows>,$<PLATFORM_ID:Cygwin>>:$<BUILD_INTERFACE:${PYTHON_LIBRARIES}>>")
152+
if(NOT PYBIND11_MASTER_PROJECT AND NOT pybind11_FIND_QUIETLY)
153+
message(STATUS "Using pybind11: (version \"${pybind11_VERSION}\" ${pybind11_VERSION_TYPE})")
154+
endif()
190155

191-
if(CMAKE_VERSION VERSION_LESS 3.13)
192-
target_link_libraries(module INTERFACE "$<$<PLATFORM_ID:Darwin>:-undefined dynamic_lookup>")
193-
else()
194-
# SHELL (3.12+) forces this to remain together, and link_options was added in 3.13+
195-
# This is safer, because you are ensured the deduplication pass in CMake will not consider
196-
# these separate and remove one but not the other.
197-
target_link_options(module INTERFACE "$<$<PLATFORM_ID:Darwin>:SHELL:-undefined dynamic_lookup>")
156+
# Relative directory setting
157+
if(USE_PYTHON_INCLUDE_DIR AND DEFINED Python_INCLUDE_DIRS)
158+
file(RELATIVE_PATH CMAKE_INSTALL_INCLUDEDIR ${CMAKE_INSTALL_PREFIX} ${Python_INCLUDE_DIRS})
159+
elseif(USE_PYTHON_INCLUDE_DIR AND DEFINED PYTHON_INCLUDE_DIR)
160+
file(RELATIVE_PATH CMAKE_INSTALL_INCLUDEDIR ${CMAKE_INSTALL_PREFIX} ${PYTHON_INCLUDE_DIRS})
198161
endif()
199162

200-
# Workaround for Python 2.7 and C++17 (C++14 as a warning) incompatibility
201-
# This adds the flags -Wno-register and -Wno-deprecated-register if the compiler
202-
# is Clang 3.9+ or AppleClang and the compile language is CXX, or /wd5033 for MSVC (all languages,
203-
# since MSVC didn't recognize COMPILE_LANGUAGE until CMake 3.11+).
204-
set(clang_4plus
205-
"$<AND:$<CXX_COMPILER_ID:Clang>,$<NOT:$<VERSION_LESS:$<CXX_COMPILER_VERSION>,3.9>>>")
206-
set(no_register "$<OR:${clang_4plus},$<CXX_COMPILER_ID:AppleClang>>")
207-
set(cxx_no_register "$<AND:$<COMPILE_LANGUAGE:CXX>,${no_register}>")
208-
set(msvc "$<CXX_COMPILER_ID:MSVC>")
209-
target_compile_options(
210-
pybind11 INTERFACE "$<${cxx_no_register}:-Wno-register;-Wno-deprecated-register>"
211-
"$<${msvc}:/wd5033>")
212-
213-
add_library(embed INTERFACE)
214-
add_library(pybind11::embed ALIAS embed)
215-
target_link_libraries(embed INTERFACE pybind11::pybind11 $<BUILD_INTERFACE:${PYTHON_LIBRARIES}>)
163+
# Fill in headers target
164+
target_include_directories(
165+
headers ${pybind11_system} INTERFACE $<BUILD_INTERFACE:${PYBIND11_INCLUDE_DIR}>
166+
$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>)
167+
168+
target_compile_features(headers INTERFACE cxx_inheriting_constructors cxx_user_literals
169+
cxx_right_angle_brackets)
216170

217171
if(PYBIND11_INSTALL)
218172
install(DIRECTORY ${PYBIND11_INCLUDE_DIR}/pybind11 DESTINATION ${CMAKE_INSTALL_INCLUDEDIR})
@@ -248,14 +202,17 @@ if(PYBIND11_INSTALL)
248202
install(
249203
FILES ${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}Config.cmake
250204
${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}ConfigVersion.cmake
251-
tools/FindPythonLibsNew.cmake tools/pybind11Tools.cmake
205+
tools/FindPythonLibsNew.cmake
206+
tools/pybind11Common.cmake
207+
tools/pybind11Tools.cmake
208+
tools/pybind11NewTools.cmake
252209
DESTINATION ${PYBIND11_CMAKECONFIG_INSTALL_DIR})
253210

254211
if(NOT PYBIND11_EXPORT_NAME)
255212
set(PYBIND11_EXPORT_NAME "${PROJECT_NAME}Targets")
256213
endif()
257214

258-
install(TARGETS pybind11 module embed EXPORT "${PYBIND11_EXPORT_NAME}")
215+
install(TARGETS headers EXPORT "${PYBIND11_EXPORT_NAME}")
259216

260217
install(
261218
EXPORT "${PYBIND11_EXPORT_NAME}"
@@ -275,10 +232,28 @@ endif()
275232
# BUILD_TESTING takes priority, but only if this is the master project
276233
if(PYBIND11_MASTER_PROJECT AND DEFINED BUILD_TESTING)
277234
if(BUILD_TESTING)
278-
add_subdirectory(tests)
235+
if(_pybind11_nopython)
236+
message(FATAL_ERROR "Cannot activate tests in NOPYTHON mode")
237+
else()
238+
add_subdirectory(tests)
239+
endif()
279240
endif()
280241
else()
281242
if(PYBIND11_TEST)
282-
add_subdirectory(tests)
243+
if(_pybind11_nopython)
244+
message(FATAL_ERROR "Cannot activate tests in NOPYTHON mode")
245+
else()
246+
add_subdirectory(tests)
247+
endif()
283248
endif()
284249
endif()
250+
251+
# Better symmetry with find_package(pybind11 CONFIG) mode.
252+
if(NOT PYBIND11_MASTER_PROJECT)
253+
set(pybind11_FOUND
254+
TRUE
255+
CACHE INTERNAL "true if pybind11 and all required components found on the system")
256+
set(pybind11_INCLUDE_DIR
257+
"${PYBIND11_INCLUDE_DIR}"
258+
CACHE INTERNAL "Directory where pybind11 headers are located")
259+
endif()

docs/advanced/embedding.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ information, see :doc:`/compiling`.
1818

1919
.. code-block:: cmake
2020
21-
cmake_minimum_required(VERSION 3.0)
21+
cmake_minimum_required(VERSION 3.4)
2222
project(example)
2323
2424
find_package(pybind11 REQUIRED) # or `add_subdirectory(pybind11)`

0 commit comments

Comments
 (0)