Skip to content

Commit d379812

Browse files
committed
Find python3 via pkg-config when available.
This is needed when using linuxdeploy / appimagecraft to create an AppImage and when cross compiling (f.e. with MXE) to find the python libraries. You may need to set CMAKE_FIND_ROOT_PATH_MODE_LIBRARY to BOTH when cross compiling. This may fix issues pybind#1718, pybind#1159, pybind#1330 and pybind#99.
1 parent cd85699 commit d379812

File tree

2 files changed

+21
-3
lines changed

2 files changed

+21
-3
lines changed

CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,7 @@ target_include_directories(
164164
pybind11 ${pybind11_system} INTERFACE $<BUILD_INTERFACE:${PYBIND11_INCLUDE_DIR}>
165165
$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>)
166166
# Only add Python for build - must be added during the import for config since it has to be re-discovered.
167+
# TODO: isn't working, when PYTHON_INCLUDE_DIRS contains a semikolon seperated list? -> ${PYTHON_INCLUDE_DIRS}
167168
target_include_directories(pybind11 SYSTEM INTERFACE $<BUILD_INTERFACE:${PYTHON_INCLUDE_DIRS}>)
168169

169170
if(CMAKE_VERSION VERSION_LESS 3.13)

tools/pybind11Tools.cmake

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,26 @@ set(Python_ADDITIONAL_VERSIONS
1717
"3.9;3.8;3.7;3.6;3.5;3.4"
1818
CACHE INTERNAL "")
1919

20-
list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_LIST_DIR}")
21-
find_package(PythonLibsNew ${PYBIND11_PYTHON_VERSION} MODULE REQUIRED)
22-
list(REMOVE_AT CMAKE_MODULE_PATH -1)
20+
# Try to get python via pgk-config, else fall back to the PythonLibsNew script.
21+
# When cross compiling, you need to set CMAKE_FIND_ROOT_PATH_MODE_LIBRARY to BOTH.
22+
find_package(PkgConfig)
23+
if (PkgConfig_FOUND)
24+
pkg_check_modules(PYTHON3 python3)
25+
if (PYTHON3_FOUND)
26+
set(PYTHON_INCLUDE_DIRS ${PYTHON3_INCLUDE_DIRS})
27+
set(PYTHON_LIBRARIES ${PYTHON3_LINK_LIBRARIES})
28+
set(PYTHON_MODULE_PREFIX "")
29+
set(PYTHON_MODULE_EXTENSION "")
30+
set(PYTHON_VERSION_MAJOR "") # TODO
31+
set(PYTHON_VERSION_MINOR "") # TODO
32+
message(STATUS "Python3 found via pkg-config!")
33+
endif()
34+
endif()
35+
if (NOT PYTHON3_FOUND)
36+
list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_LIST_DIR}")
37+
find_package(PythonLibsNew ${PYBIND11_PYTHON_VERSION} MODULE REQUIRED)
38+
list(REMOVE_AT CMAKE_MODULE_PATH -1)
39+
endif()
2340

2441
include(CheckCXXCompilerFlag)
2542

0 commit comments

Comments
 (0)