From 2b4e537ccddd1d5be5be9621fabfdf7e88144735 Mon Sep 17 00:00:00 2001 From: Silvio Traversaro Date: Sun, 11 Apr 2021 23:38:55 +0200 Subject: [PATCH] Add IDYNTREE_DETECT_ACTIVE_PYTHON_SITEPACKAGES CMake option (#852) * Add IDYNTREE_DETECT_ACTIVE_PYTHON_SITEPACKAGES CMake option This option (OFF by default) can be used to automatically install the Python bindings in the current active site location. This is convenient for example on system in which the site location is not part of the same prefix in which C++ libraries are installed, for example in Conda in Windows where: CMAKE_INSTALL_PREFIX is %CONDA_PREFIX%/Library and Python3_SITELIB is %CONDA_PREFIX%/Lib/site-packages --- CHANGELOG.md | 1 + bindings/CMakeLists.txt | 21 +++++++++++++++++---- bindings/pybind11/CMakeLists.txt | 6 ------ bindings/python/CMakeLists.txt | 8 -------- cmake/iDynTreeOptions.cmake | 12 +----------- 5 files changed, 19 insertions(+), 29 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 300c90d6aa0..5d243ac73be 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,6 +10,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Added - Add the possibility to use `MatrixView` and `Span` as input/output objects for `InverseKinematics` class (https://github.com/robotology/idyntree/pull/822). - Add the possibility to get frame trasform from the visualizer, and to use frame/link name in place of indices (https://github.com/robotology/idyntree/pull/849). +- Add `IDYNTREE_DETECT_ACTIVE_PYTHON_SITEPACKAGES` CMake option (default value: OFF) to detect and install in the active site-package directory the Python bindings (https://github.com/robotology/idyntree/pull/852). ### Changed - The wheels uploaded to PyPI are now `manylinux_2_24` compliant (https://github.com/robotology/idyntree/pull/844) diff --git a/bindings/CMakeLists.txt b/bindings/CMakeLists.txt index 01b51d16f7b..b729474d994 100644 --- a/bindings/CMakeLists.txt +++ b/bindings/CMakeLists.txt @@ -9,14 +9,27 @@ option(IDYNTREE_USES_LUA "Do you want to create the Lua bindings" FALSE) option(IDYNTREE_USES_MATLAB "Do you want to create the MATLAB bindings" FALSE) option(IDYNTREE_USES_OCTAVE "Do you want to create the OCTAVE bindings" FALSE) option(IDYNTREE_GENERATE_MATLAB "Enable if you have the experimental version of SWIG necessary for generating the Matlab wrapper" FALSE) +option(IDYNTREE_DETECT_ACTIVE_PYTHON_SITEPACKAGES "Do you want iDynTree to detect and install in the active site-package directory? (it could be a system dir)" FALSE) find_package(SWIG) -# Setup installation directory for Python bindings. -if (IDYNTREE_USES_PYTHON_PYBIND11 OR IDYNTREE_USES_PYTHON) - if(IDYNTREE_PACKAGE_FOR_PYPI) - set(PYTHON_INSTDIR ${CMAKE_INSTALL_PREFIX}) +if(IDYNTREE_USES_PYTHON OR IDYNTREE_USES_PYTHON_PYBIND11) + if(${CMAKE_VERSION} VERSION_LESS "3.18.0") + set(IDYNTREE_SEARCHED_Python_Development_Component "Development") + else() + set(IDYNTREE_SEARCHED_Python_Development_Component "Development.Module") + endif() + find_package(Python3 COMPONENTS Interpreter ${IDYNTREE_SEARCHED_Python_Development_Component} NumPy REQUIRED) + + if (IDYNTREE_DETECT_ACTIVE_PYTHON_SITEPACKAGES) + set(PYTHON_INSTDIR ${Python3_SITELIB}/idyntree) + elseif(IDYNTREE_PACKAGE_FOR_PYPI) + set(PYTHON_INSTDIR ${CMAKE_INSTALL_PREFIX}) else() + execute_process(COMMAND ${Python3_EXECUTABLE} + -c "from distutils import sysconfig; print(sysconfig.get_python_lib(1,0,prefix=''))" + OUTPUT_VARIABLE _PYTHON_INSTDIR) + string(STRIP ${_PYTHON_INSTDIR} IDYNTREE_PYTHON_INSTALL_DIR) set(PYTHON_INSTDIR ${IDYNTREE_PYTHON_INSTALL_DIR}/idyntree) endif() endif() diff --git a/bindings/pybind11/CMakeLists.txt b/bindings/pybind11/CMakeLists.txt index d985487cff0..d5f47d94a15 100644 --- a/bindings/pybind11/CMakeLists.txt +++ b/bindings/pybind11/CMakeLists.txt @@ -1,9 +1,3 @@ -if(${CMAKE_VERSION} VERSION_LESS "3.18.0") - find_package(Python3 COMPONENTS Interpreter Development NumPy REQUIRED) -else() - find_package(Python3 COMPONENTS Interpreter Development.Module NumPy REQUIRED) -endif() - pybind11_add_module(pybind11_idyntree SYSTEM idyntree.cpp error_utilities.h error_utilities.cpp idyntree_core.h idyntree_core.cpp diff --git a/bindings/python/CMakeLists.txt b/bindings/python/CMakeLists.txt index ed09b96fc79..2c04e4ea70b 100644 --- a/bindings/python/CMakeLists.txt +++ b/bindings/python/CMakeLists.txt @@ -1,11 +1,3 @@ -if(${CMAKE_VERSION} VERSION_LESS "3.18.0") - find_package(Python3 COMPONENTS Interpreter Development NumPy REQUIRED) -else() - find_package(Python3 COMPONENTS Interpreter Development.Module NumPy REQUIRED) -endif() - -message(STATUS "Using Python: ${Python3_EXECUTABLE}") - cmake_policy(SET CMP0078 NEW) cmake_policy(SET CMP0086 NEW) diff --git a/cmake/iDynTreeOptions.cmake b/cmake/iDynTreeOptions.cmake index b3ee5ad2592..19e8f85e65d 100644 --- a/cmake/iDynTreeOptions.cmake +++ b/cmake/iDynTreeOptions.cmake @@ -73,17 +73,7 @@ if(IDYNTREE_COMPILE_BINDINGS) add_definitions(-DIDYNTREE_COMPILE_BINDINGS) endif(IDYNTREE_COMPILE_BINDINGS) -if(IDYNTREE_USES_PYTHON OR IDYNTREE_USES_PYTHON_PYBIND11) - find_package(Python3 COMPONENTS Interpreter QUIET) - if (Python3_FOUND) - execute_process(COMMAND ${Python3_EXECUTABLE} - -c "from distutils import sysconfig; print(sysconfig.get_python_lib(1,0,prefix=''))" - OUTPUT_VARIABLE _PYTHON_INSTDIR) - string(STRIP ${_PYTHON_INSTDIR} IDYNTREE_PYTHON_INSTALL_DIR) - # Bindings are installed in `idyntree` subdirectory. - list(APPEND IDYNTREE_BINARY_DIRS "${IDYNTREE_PYTHON_INSTALL_DIR}/idyntree") - endif() -endif() + #set default build type to "Release" in single-config generators if(NOT CMAKE_CONFIGURATION_TYPES)