Skip to content

Commit a12b09a

Browse files
committed
review python bindings install
1 parent f434b75 commit a12b09a

File tree

4 files changed

+29
-33
lines changed

4 files changed

+29
-33
lines changed

CMakeLists.txt

-7
Original file line numberDiff line numberDiff line change
@@ -61,15 +61,8 @@ option(BUILD_PYTHON_BINDINGS "Build Python bindings with pybind11." OFF)
6161
option(BUILD_TESTING_PYTHON "Build Python tests only." OFF)
6262

6363
if (BUILD_PYTHON_BINDINGS)
64-
65-
# Include the vendored FindPython3 resources
66-
list(PREPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake/modules")
67-
68-
find_package(Python3 REQUIRED COMPONENTS Interpreter Development.Module)
6964
find_package(pybind11 REQUIRED)
70-
7165
add_subdirectory(python)
72-
7366
endif()
7467

7568
###########

python/CMakeLists.txt

+28-24
Original file line numberDiff line numberDiff line change
@@ -18,46 +18,50 @@ set_property(TARGET manifpy PROPERTY CXX_STANDARD 11)
1818
set_property(TARGET manifpy PROPERTY CXX_STANDARD_REQUIRED ON)
1919
set_property(TARGET manifpy PROPERTY CXX_EXTENSIONS OFF)
2020

21-
# Detect the active site-packages folder instead of using CMAKE_INSTALL_PREFIX
22-
option(
23-
DETECT_ACTIVE_PYTHON_SITEPACKAGES
24-
"Do you want manif to detect and use the active site-package directory? (it could be a system dir)"
25-
FALSE)
26-
27-
# Installation in the active site-packages folder (could be a system folder)
28-
if(DETECT_ACTIVE_PYTHON_SITEPACKAGES)
29-
set(PYTHON_INSTDIR "${Python3_SITELIB}/manifpy")
30-
# Installation from the build extension of setup.py
31-
elseif(CALL_FROM_SETUP_PY)
32-
set(PYTHON_INSTDIR "${CMAKE_INSTALL_PREFIX}")
33-
# Installation in the CMAKE_INSTALL_PREFIX
21+
if (CALL_FROM_SETUP_PY)
22+
# cmake-build-extension sets the full absolute path as CMAKE_INSTALL_PREFIX.
23+
set(MANIFPY_INSTDIR "${CMAKE_INSTALL_PREFIX}")
3424
else()
25+
# 'distutils.sysconfig.get_python_lib' returns the absolute path of Python
26+
# by default a global location managed by the distro e.g. /usr/lib/python.
27+
#
28+
# pybind11 and FindPython3 set respectively PYTHON_SITE_PACKAGES/Python3_SITELIB
29+
# from 'distutils.sysconfig.get_python_lib'
30+
#
31+
# Those are especially annoying on Ubuntu since it has
32+
# some hardcoded paths in python3.x/site.py
33+
#
34+
# `sysconfig.get_path` may return paths that does not even exists.
35+
#
36+
# So below we retrieve the first site-package path from 'site.getsitepackages()'.
37+
3538
execute_process(
3639
COMMAND
37-
${Python3_EXECUTABLE} -c
38-
"import distutils.sysconfig; print(distutils.sysconfig.get_python_lib(plat_specific=True, standard_lib=False, prefix=''))"
39-
OUTPUT_VARIABLE _PYTHON_INSTDIR)
40-
string(STRIP ${_PYTHON_INSTDIR} _PYTHON_INSTDIR_CLEAN)
41-
set(PYTHON_INSTDIR "${_PYTHON_INSTDIR_CLEAN}/manifpy")
40+
${PYTHON_EXECUTABLE} -c "import site; print(site.getsitepackages()[0])"
41+
OUTPUT_VARIABLE _PYTHON_SITE_PACKAGE OUTPUT_STRIP_TRAILING_WHITESPACE
42+
)
43+
set(MANIFPY_INSTDIR "${_PYTHON_SITE_PACKAGE}/manifpy")
4244
endif()
4345

46+
message(STATUS "Installing manifpy in ${MANIFPY_INSTDIR}")
47+
4448
# Setup installation path
45-
install(TARGETS manifpy DESTINATION "${PYTHON_INSTDIR}")
49+
install(TARGETS manifpy COMPONENT python DESTINATION "${MANIFPY_INSTDIR}")
4650

4751
# Create the Python package in the build tree for testing purposes
48-
set(BUILD_TREE_PYTHON_PACKAGE "${CMAKE_BINARY_DIR}/manifpy")
52+
set(MANIFPY_BUILDDIR "${CMAKE_BINARY_DIR}/manifpy")
4953
set_target_properties(
5054
manifpy PROPERTIES
5155
OUTPUT_NAME _bindings
52-
LIBRARY_OUTPUT_DIRECTORY "${BUILD_TREE_PYTHON_PACKAGE}")
56+
LIBRARY_OUTPUT_DIRECTORY "${MANIFPY_BUILDDIR}")
5357

5458
# Create the __init__.py file
5559
file(
5660
GENERATE
57-
OUTPUT "${BUILD_TREE_PYTHON_PACKAGE}/__init__.py"
61+
OUTPUT "${MANIFPY_BUILDDIR}/__init__.py"
5862
CONTENT "from manifpy._bindings import *\n")
5963

6064
# Install the __init__.py file
6165
install(
62-
FILES "${BUILD_TREE_PYTHON_PACKAGE}/__init__.py"
63-
DESTINATION ${PYTHON_INSTDIR})
66+
FILES "${MANIFPY_BUILDDIR}/__init__.py"
67+
DESTINATION ${MANIFPY_INSTDIR})

setup.py

-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
cmake_configure_options=[
1414
"-DCALL_FROM_SETUP_PY:BOOL=ON",
1515
"-DBUILD_PYTHON_BINDINGS:BOOL=ON",
16-
f"-DPython3_EXECUTABLE:PATH={sys.executable}",
1716
],
1817
)
1918
],

test/python/CMakeLists.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
function(manif_add_pytest target)
33
add_test(
44
NAME ${target}
5-
COMMAND ${Python3_EXECUTABLE} -m pytest ${target}.py
5+
COMMAND pytest-3 ${target}.py
66
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
77
)
88
set_tests_properties(${target}

0 commit comments

Comments
 (0)