Skip to content

Commit 263a03d

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 f6e543b commit 263a03d

File tree

2 files changed

+21
-2
lines changed

2 files changed

+21
-2
lines changed

CMakeLists.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,8 @@ if(NOT (CMAKE_VERSION VERSION_LESS 3.0)) # CMake >= 3.0
9898
add_library(pybind11 INTERFACE)
9999
add_library(pybind11::pybind11 ALIAS pybind11) # to match exported target
100100
target_include_directories(pybind11 INTERFACE $<BUILD_INTERFACE:${PYBIND11_INCLUDE_DIR}>
101-
$<BUILD_INTERFACE:${PYTHON_INCLUDE_DIRS}>
101+
# $<BUILD_INTERFACE:${PYTHON_INCLUDE_DIRS}> isn't working, when PYTHON_INCLUDE_DIRS contains a semikolon seperated list
102+
${PYTHON_INCLUDE_DIRS}
102103
$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>)
103104
target_compile_options(pybind11 INTERFACE $<BUILD_INTERFACE:${PYBIND11_CPP_STANDARD}>)
104105

tools/pybind11Tools.cmake

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,25 @@ if(NOT PYBIND11_PYTHON_VERSION)
1313
endif()
1414

1515
set(Python_ADDITIONAL_VERSIONS 3.9 3.8 3.7 3.6 3.5 3.4)
16-
find_package(PythonLibsNew ${PYBIND11_PYTHON_VERSION} REQUIRED)
16+
17+
# Try to get python via pgk-config, else fall back to the PythonLibsNew script.
18+
# When cross compiling, you need to set CMAKE_FIND_ROOT_PATH_MODE_LIBRARY to BOTH.
19+
find_package(PkgConfig)
20+
if (PkgConfig_FOUND)
21+
pkg_check_modules(PYTHON3 python3) # TODO PYBIND11_PYTHON_VERSION
22+
if (PYTHON3_FOUND)
23+
set(PYTHON_INCLUDE_DIRS ${PYTHON3_INCLUDE_DIRS})
24+
set(PYTHON_LIBRARIES ${PYTHON3_LINK_LIBRARIES})
25+
set(PYTHON_MODULE_PREFIX "")
26+
set(PYTHON_MODULE_EXTENSION "")
27+
set(PYTHON_VERSION_MAJOR "") # TODO
28+
set(PYTHON_VERSION_MINOR "") # TODO
29+
message(STATUS "Python3 found via pkg-config!")
30+
endif()
31+
endif()
32+
if (NOT PYTHON3_FOUND)
33+
find_package(PythonLibsNew ${PYBIND11_PYTHON_VERSION} REQUIRED)
34+
endif()
1735

1836
include(CheckCXXCompilerFlag)
1937
include(CMakeParseArguments)

0 commit comments

Comments
 (0)