Skip to content

Commit ead480d

Browse files
committed
feat: NOPYTHON mode
1 parent d2f3d9e commit ead480d

File tree

6 files changed

+69
-48
lines changed

6 files changed

+69
-48
lines changed

CMakeLists.txt

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,9 @@ include(GNUInstallDirs)
4141
include(CMakePackageConfigHelpers)
4242
include(CMakeDependentOption)
4343

44-
message(STATUS "pybind11 v${pybind11_VERSION} ${pybind11_VERSION_TYPE}")
44+
if(NOT pybind11_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)
@@ -60,7 +62,7 @@ endif()
6062
# Options
6163
option(PYBIND11_INSTALL "Install pybind11 header files?" ${PYBIND11_MASTER_PROJECT})
6264
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)
65+
option(PYBIND11_NOPYTHON "Disable search for Python" OFF)
6466

6567
cmake_dependent_option(
6668
USE_PYTHON_INCLUDE_DIR
@@ -213,5 +215,9 @@ if(PYBIND11_INSTALL)
213215
endif()
214216

215217
if(PYBIND11_TEST OR (BUILD_TESTING AND PYBIND11_MASTER_PROJECT))
216-
add_subdirectory(tests)
218+
if(_pybind11_nopython)
219+
message(FATAL_ERROR "Cannot activate tests in NOPYTHON mode")
220+
else()
221+
add_subdirectory(tests)
222+
endif()
217223
endif()

docs/compiling.rst

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -234,7 +234,7 @@ available in all modes. The targets provided are:
234234
An alternative to `INTERPROCEDURAL_OPTIMIZATION` for adding link-time optimization.
235235

236236
``pybind11::windows_extras``
237-
Bigobj and mp for MSVC.
237+
``/bigobj`` and ``/mp`` for MSVC.
238238

239239
Two helper functions are also provided:
240240

@@ -276,6 +276,17 @@ Instead of setting properties, you can set ``CMAKE_*`` variables to initialize t
276276
It can be expecially important to provide or set these properties; the
277277
:ref:`FAQ <faq:symhidden>` contains an explanation on why these are needed.
278278

279+
Advanced: NOPYTHON mode
280+
-----------------------
281+
282+
If you want complete control, you can set ``PYBIND11_NOPYTHON`` to completely
283+
disable Python integration (this also happens if you run ``FindPython2`` and/or
284+
``FindPython3`` without running ``FindPython``). This gives you complete
285+
freedom to integrate into an existing system (like `Scikit-Build's
286+
<https://scikit-build.readthedocs.io>`_ ``PythonExtensions``).
287+
``pybind11_add_module`` and ``pybind11_extension`` will be unavailable, and the
288+
targets will be missing any Python specific behavior.
289+
279290
Embedding the Python interpreter
280291
--------------------------------
281292

tools/pybind11Common.cmake

Lines changed: 40 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -132,17 +132,50 @@ if(MSVC)
132132
PROPERTY INTERFACE_COMPILE_OPTIONS $<$<NOT:$<CONFIG:Debug>>:$<$<COMPILE_LANGUAGE:CXX>:/MP>>)
133133
endif()
134134
endif()
135+
136+
# ----------------------- Legacy option --------------------------
137+
138+
# Warn or error if old variable name used
139+
if(PYBIND11_CPP_STANDARD)
140+
string(REGEX MATCH [[..$]] VAL "${PYBIND11_CPP_STANDARD}")
141+
if(CMAKE_CXX_STANDARD)
142+
if(NOT CMAKE_CXX_STANDARD STREQUAL VAL)
143+
message(WARNING "CMAKE_CXX_STANDARD=${CMAKE_CXX_STANDARD} does not match "
144+
"PYBIND11_CPP_STANDARD=${PYBIND11_CPP_STANDARD}, "
145+
"please remove PYBIND11_CPP_STANDARD from your cache")
146+
endif()
147+
else()
148+
set(supported_standards 11 14 17 20)
149+
if("${VAL}" IN_LIST supported_standards)
150+
message(WARNING "USE -DCMAKE_CXX_STANDARD=${VAL} instead of PYBIND11_PYTHON_VERSION")
151+
set(CMAKE_CXX_STANDARD
152+
${VAL}
153+
CACHE STRING "From PYBIND11_CPP_STANDARD")
154+
else()
155+
message(FATAL_ERROR "PYBIND11_CPP_STANDARD should be replaced with CMAKE_CXX_STANDARD "
156+
"(last two chars: ${VAL} not understood as a valid CXX std)")
157+
endif()
158+
endif()
159+
endif()
160+
135161
# --------------------- Python specifics -------------------------
136162

137-
# Check to see which Python mode we are in, new or old python
138-
if(PYBIND11_FINDPYTHON
139-
OR Python_FOUND
140-
OR Python3_FOUND
141-
OR Python2_FOUND)
163+
# Check to see which Python mode we are in, new, old, or no python
164+
if(PYBIND11_NOPYTHON)
165+
set(_pybind11_nopython ON)
166+
if(NOT pybind11_QUIETLY)
167+
message(STATUS "pybind11 in NOPYHTON mode")
168+
endif()
169+
elseif(PYBIND11_FINDPYTHON OR Python_FOUND)
142170

143171
# New mode
144172
include("${CMAKE_CURRENT_LIST_DIR}/pybind11NewTools.cmake")
145173

174+
elseif(Python3_FOUND OR Python2_FOUND)
175+
set(_pybind11_nopython ON)
176+
if(NOT pybind11_QUIETLY)
177+
message(STATUS "pybind11 in NOPYHTON mode due to Python2/Python3 only being present")
178+
endif()
146179
else()
147180

148181
# Classic mode
@@ -221,11 +254,11 @@ function(_pybind11_generate_lto target prefer_thin_lto)
221254
TARGET ${target}
222255
APPEND
223256
PROPERTY INTERFACE_COMPILE_OPTIONS "$<${genex}:${PYBIND11_LTO_CXX_FLAGS}>")
224-
if(NOT is_config)
257+
if(NOT is_config AND NOT pybind11_QUIETLY)
225258
message(STATUS "${target} enabled")
226259
endif()
227260
else()
228-
if(NOT is_config)
261+
if(NOT is_config AND NOT pybind11_QUIETLY)
229262
message(STATUS "${target} disabled (not supported by the compiler and/or linker)")
230263
endif()
231264
endif()

tools/pybind11Config.cmake.in

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,9 @@ Set PythonLibsNew variables to influence python detection and
7575
CMAKE_CXX_STANDARD to influence standard setting. ::
7676
7777
find_package(pybind11 CONFIG REQUIRED)
78-
message(STATUS "Found pybind11 v${pybind11_VERSION} ${pybind11_VERSION_TYPE}: ${pybind11_INCLUDE_DIRS}")
78+
if(NOT pybind11_QUIETLY)
79+
message(STATUS "Found pybind11 v${pybind11_VERSION} ${pybind11_VERSION_TYPE}: ${pybind11_INCLUDE_DIRS}")
80+
endif()
7981
8082
# Create an extension module
8183
add_library(mylib MODULE main.cpp)

tools/pybind11NewTools.cmake

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,12 @@ if(CMAKE_VERSION VERSION_LESS 3.12)
1414
message(FATAL_ERROR "You cannot use the new FindPython module with CMake < 3.12")
1515
endif()
1616

17-
if(NOT Python_FOUND
18-
OR Python2_FOUND
19-
OR Python3_FOUND)
20-
17+
if(NOT Python_FOUND)
2118
if(NOT DEFINED Python_FIND_IMPLEMENTATIONS)
2219
set(Python_FIND_IMPLEMENTATIONS CPython PyPy)
2320
endif()
2421

22+
# GitHub Actions like activation
2523
if(NOT DEFINED Python_ROOT_DIR AND DEFINED ENV{pythonLocation})
2624
set(Python_ROOT_DIR "$ENV{pythonLocation}")
2725
endif()
@@ -48,14 +46,14 @@ if(DEFINED Python_INCLUDE_DIRS)
4846
PROPERTY INTERFACE_INCLUDE_DIRECTORIES $<BUILD_INTERFACE:${Python_INCLUDE_DIRS}>)
4947
endif()
5048

51-
if(DEFINED Python2_VERSION OR (Python_VERSION AND Python_VERSION VERSION_LESS 3))
49+
if(DEFINED Python_VERSION AND Python_VERSION VERSION_LESS 3)
5250
set_property(
5351
TARGET pybind11::pybind11
5452
APPEND
5553
PROPERTY INTERFACE_LINK_LIBRARIES pybind11::python2_no_register)
5654
endif()
5755

58-
# In CMake 3.18+, you can find these separately, so include an if (also Python2 and Python3, for which we do nothing)
56+
# In CMake 3.18+, you can find these separately, so include an if
5957
if(TARGET Python::Python)
6058
set_property(
6159
TARGET pybind11::embed
@@ -89,10 +87,6 @@ function(pybind11_add_module target_name)
8987

9088
if(COMMAND Python_add_library)
9189
python_add_library(${target_name} ${type} WITH_SOABI ${ARG_UNPARSED_ARGUMENTS})
92-
elseif(COMMAND Python3_add_library)
93-
python3_add_library(${target_name} ${type} WITH_SOABI ${ARG_UNPARSED_ARGUMENTS})
94-
elseif(COMMAND Python2_add_library)
95-
python2_add_library(${target_name} ${type} WITH_SOABI ${ARG_UNPARSED_ARGUMENTS})
9690
endif()
9791

9892
target_link_libraries(${target_name} PRIVATE pybind11::headers)
@@ -107,9 +101,7 @@ function(pybind11_add_module target_name)
107101
target_link_libraries(${target_name} PRIVATE pybind11::windows_extras)
108102
endif()
109103

110-
if(DEFINED Python2_VERSION
111-
OR Python_VERSION
112-
AND Python_VERSION VERSION_LESS 3)
104+
if(DEFINED Python_VERSION AND Python_VERSION VERSION_LESS 3)
113105
target_link_libraries(${target_name} PRIVATE pybind11::python2_no_register)
114106
endif()
115107

tools/pybind11Tools.cmake

Lines changed: 0 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -30,29 +30,6 @@ list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_LIST_DIR}")
3030
find_package(PythonLibsNew ${PYBIND11_PYTHON_VERSION} MODULE REQUIRED)
3131
list(REMOVE_AT CMAKE_MODULE_PATH -1)
3232

33-
# Warn or error if old variable name used
34-
if(PYBIND11_CPP_STANDARD)
35-
string(REGEX MATCH [[..$]] VAL "${PYBIND11_CPP_STANDARD}")
36-
if(CMAKE_CXX_STANDARD)
37-
if(NOT CMAKE_CXX_STANDARD STREQUAL VAL)
38-
message(WARNING "CMAKE_CXX_STANDARD=${CMAKE_CXX_STANDARD} does not match "
39-
"PYBIND11_CPP_STANDARD=${PYBIND11_CPP_STANDARD}, "
40-
"please remove PYBIND11_CPP_STANDARD from your cache")
41-
endif()
42-
else()
43-
set(supported_standards 11 14 17 20)
44-
if("${VAL}" IN_LIST supported_standards)
45-
message(WARNING "USE -DCMAKE_CXX_STANDARD=${VAL} instead of PYBIND11_PYTHON_VERSION")
46-
set(CMAKE_CXX_STANDARD
47-
${VAL}
48-
CACHE STRING "From PYBIND11_CPP_STANDARD")
49-
else()
50-
message(FATAL_ERROR "PYBIND11_CPP_STANDARD should be replaced with CMAKE_CXX_STANDARD "
51-
"(last two chars: ${VAL} not understood as a valid CXX std)")
52-
endif()
53-
endif()
54-
endif()
55-
5633
# Cache variables so pybind11_add_module can be used in parent projects
5734
set(PYTHON_INCLUDE_DIRS
5835
${PYTHON_INCLUDE_DIRS}

0 commit comments

Comments
 (0)