Skip to content

Commit 7930b28

Browse files
committed
Update Python install path detection
This changes the `PYTHON_MODULE_PATH` handling in two ways: * Makes it a cache variable, so detection is only run once and only if it's not already set. This allows it to be overridden on the command line, e.g. `cmake -DPYTHON_MODULE_PATH:PATH=lib/python3.6/dist-packages` * Uses the presence of the `pybuild` executable to sense for a Debian-derived system (only on UNIX AND NOT APPLE), and if found it falls back to the old `getsitepackages()[0]` path extraction method.
1 parent aa85c1b commit 7930b28

File tree

1 file changed

+28
-9
lines changed

1 file changed

+28
-9
lines changed

src/bindings/python/CMakeLists.txt

Lines changed: 28 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -66,17 +66,36 @@ if (PYTHONLIBS_FOUND AND PYTHONINTERP_FOUND)
6666
${PYTHON_LIBRARIES} openshot)
6767

6868
### FIND THE PYTHON INTERPRETER (AND THE SITE PACKAGES FOLDER)
69-
execute_process ( COMMAND ${PYTHON_EXECUTABLE} -c "\
69+
if (UNIX AND NOT APPLE)
70+
### Special-case for Debian's crazy, by checking to see if pybuild
71+
### is available. We don't use it, except as a canary in a coal mine
72+
find_program(PYBUILD_EXECUTABLE pybuild
73+
DOC "Path to Debian's pybuild utility")
74+
if (PYBUILD_EXECUTABLE)
75+
# We're on a Debian derivative, fall back to old path detection
76+
set(py_detection "import site; print(site.getsitepackages()[0])")
77+
else()
78+
# Use distutils to detect install path
79+
set (py_detection "\
7080
from distutils.sysconfig import get_python_lib; \
71-
print( get_python_lib( plat_specific=True, prefix='${CMAKE_INSTALL_PREFIX}' ) )"
72-
OUTPUT_VARIABLE _ABS_PYTHON_MODULE_PATH
73-
OUTPUT_STRIP_TRAILING_WHITESPACE )
81+
print( get_python_lib( plat_specific=True, prefix='${CMAKE_INSTALL_PREFIX}' ) )")
82+
endif()
83+
endif()
84+
85+
if (NOT PYTHON_MODULE_PATH)
86+
execute_process ( COMMAND ${PYTHON_EXECUTABLE} -c "${py_detection}"
87+
OUTPUT_VARIABLE _ABS_PYTHON_MODULE_PATH
88+
OUTPUT_STRIP_TRAILING_WHITESPACE )
89+
90+
GET_FILENAME_COMPONENT(_ABS_PYTHON_MODULE_PATH
91+
"${_ABS_PYTHON_MODULE_PATH}" ABSOLUTE)
92+
FILE(RELATIVE_PATH _REL_PYTHON_MODULE_PATH
93+
${CMAKE_INSTALL_PREFIX} ${_ABS_PYTHON_MODULE_PATH})
94+
SET(PYTHON_MODULE_PATH ${_REL_PYTHON_MODULE_PATH}
95+
CACHE PATH "Install path for Python modules (relative to prefix)")
96+
endif()
7497

75-
GET_FILENAME_COMPONENT(_ABS_PYTHON_MODULE_PATH
76-
"${_ABS_PYTHON_MODULE_PATH}" ABSOLUTE)
77-
FILE(RELATIVE_PATH _REL_PYTHON_MODULE_PATH
78-
${CMAKE_INSTALL_PREFIX} ${_ABS_PYTHON_MODULE_PATH})
79-
SET(PYTHON_MODULE_PATH ${_REL_PYTHON_MODULE_PATH})
98+
message(STATUS "Will install Python module to: ${PYTHON_MODULE_PATH}")
8099

81100
############### INSTALL HEADERS & LIBRARY ################
82101
### Install Python bindings

0 commit comments

Comments
 (0)