Skip to content

Commit

Permalink
Refactor finding pybind11 (#360)
Browse files Browse the repository at this point in the history
Signed-off-by: Louise Poubel <louise@openrobotics.org>
Signed-off-by: Steve Peters <scpeters@openrobotics.org>

Co-authored-by: Steve Peters <scpeters@openrobotics.org>
  • Loading branch information
chapulina and scpeters committed Jan 11, 2022
1 parent 4b25160 commit fa67ea8
Show file tree
Hide file tree
Showing 3 changed files with 137 additions and 130 deletions.
25 changes: 18 additions & 7 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -67,15 +67,26 @@ if (SWIG_FOUND)
else()
message (STATUS "Searching for Ruby - found version ${RUBY_VERSION}.")
endif()
endif()

########################################
# Include python
find_package(PythonInterp 3 REQUIRED) # change to Python3 when Bionic is EOL
find_package(PythonLibs QUIET)
if (NOT PYTHONLIBS_FOUND)
message (STATUS "Searching for Python - not found.")
########################################
# Python bindings
include(IgnPython)
find_package(PythonLibs QUIET)
if (NOT PYTHONLIBS_FOUND)
IGN_BUILD_WARNING("Python is missing: Python interfaces are disabled.")
message (STATUS "Searching for Python - not found.")
else()
message (STATUS "Searching for Python - found version ${PYTHONLIBS_VERSION_STRING}.")

set(PYBIND11_PYTHON_VERSION 3)
find_package(pybind11 2.2 QUIET)

if (${pybind11_FOUND})
message (STATUS "Searching for pybind11 - found version ${pybind11_VERSION}.")
else()
message (STATUS "Searching for Python - found version ${PYTHONLIBS_VERSION_STRING}.")
IGN_BUILD_WARNING("pybind11 is missing: Python interfaces are disabled.")
message (STATUS "Searching for pybind11 - not found.")
endif()
endif()

Expand Down
4 changes: 3 additions & 1 deletion src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,7 @@ ign_build_tests(TYPE UNIT SOURCES ${gtest_sources})
add_subdirectory(graph)

# Bindings subdirectories
add_subdirectory(python_pybind11)
if (${pybind11_FOUND})
add_subdirectory(python_pybind11)
endif()
add_subdirectory(ruby)
238 changes: 116 additions & 122 deletions src/python_pybind11/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,139 +5,133 @@ if(WIN32 AND CMAKE_BUILD_TYPE STREQUAL "Debug")
set(PYTHON_LIBRARIES "${PYTHON_DEBUG_LIBRARIES}")
endif()

set(PYBIND11_PYTHON_VERSION 3)
message(STATUS "Building pybind11 interfaces")
# Split from main extension and converted to pybind11
pybind11_add_module(math SHARED
src/_ignition_math_pybind11.cc
src/Angle.cc
src/AxisAlignedBox.cc
src/Color.cc
src/DiffDriveOdometry.cc
src/Frustum.cc
src/GaussMarkovProcess.cc
src/Helpers.cc
src/Kmeans.cc
src/Material.cc
src/PID.cc
src/Rand.cc
src/RollingMean.cc
src/RotationSpline.cc
src/SemanticVersion.cc
src/SignalStats.cc
src/SphericalCoordinates.cc
src/Spline.cc
src/StopWatch.cc
src/Temperature.cc
src/Vector3Stats.cc
)

find_package(pybind11 2.2 QUIET)
target_link_libraries(math PRIVATE
${PROJECT_LIBRARY_TARGET_NAME}
)

if (${pybind11_FOUND})
message(STATUS "Building pybind11 interfaces")
# Split from main extension and converted to pybind11
pybind11_add_module(math SHARED
src/_ignition_math_pybind11.cc
src/Angle.cc
src/AxisAlignedBox.cc
src/Color.cc
src/DiffDriveOdometry.cc
src/Frustum.cc
src/GaussMarkovProcess.cc
src/Helpers.cc
src/Kmeans.cc
src/Material.cc
src/PID.cc
src/Rand.cc
src/RollingMean.cc
src/RotationSpline.cc
src/SemanticVersion.cc
src/SignalStats.cc
src/SphericalCoordinates.cc
src/Spline.cc
src/StopWatch.cc
src/Temperature.cc
src/Vector3Stats.cc
)

target_link_libraries(math PRIVATE
${PROJECT_LIBRARY_TARGET_NAME}
)

if(USE_SYSTEM_PATHS_FOR_PYTHON_INSTALLATION)
if(${CMAKE_VERSION} VERSION_LESS "3.12.0")
execute_process(
COMMAND "${PYTHON_EXECUTABLE}" -c "if True:
from distutils import sysconfig as sc
print(sc.get_python_lib(plat_specific=True))"
OUTPUT_VARIABLE Python3_SITEARCH
OUTPUT_STRIP_TRAILING_WHITESPACE)
else()
# Get install variable from Python3 module
# Python3_SITEARCH is available from 3.12 on, workaround if needed:
find_package(Python3 COMPONENTS Interpreter)
endif()
if(USE_SYSTEM_PATHS_FOR_PYTHON_INSTALLATION)
if(${CMAKE_VERSION} VERSION_LESS "3.12.0")
execute_process(
COMMAND "${PYTHON_EXECUTABLE}" -c "if True:
from distutils import sysconfig as sc
print(sc.get_python_lib(plat_specific=True))"
OUTPUT_VARIABLE Python3_SITEARCH
OUTPUT_STRIP_TRAILING_WHITESPACE)
else()
# Get install variable from Python3 module
# Python3_SITEARCH is available from 3.12 on, workaround if needed:
find_package(Python3 COMPONENTS Interpreter)
endif()

if(USE_DIST_PACKAGES_FOR_PYTHON)
string(REPLACE "site-packages" "dist-packages" IGN_PYTHON_INSTALL_PATH ${Python3_SITEARCH})
else()
# custom cmake command is returning dist-packages
string(REPLACE "dist-packages" "site-packages" IGN_PYTHON_INSTALL_PATH ${Python3_SITEARCH})
endif()
if(USE_DIST_PACKAGES_FOR_PYTHON)
string(REPLACE "site-packages" "dist-packages" IGN_PYTHON_INSTALL_PATH ${Python3_SITEARCH})
else()
# If not a system installation, respect local paths
set(IGN_PYTHON_INSTALL_PATH ${IGN_LIB_INSTALL_DIR}/python)
# custom cmake command is returning dist-packages
string(REPLACE "dist-packages" "site-packages" IGN_PYTHON_INSTALL_PATH ${Python3_SITEARCH})
endif()
else()
# If not a system installation, respect local paths
set(IGN_PYTHON_INSTALL_PATH ${IGN_LIB_INSTALL_DIR}/python)
endif()

set(IGN_PYTHON_INSTALL_PATH "${IGN_PYTHON_INSTALL_PATH}/ignition")
set(IGN_PYTHON_INSTALL_PATH "${IGN_PYTHON_INSTALL_PATH}/ignition")

# Set the build location and install location for a CPython extension
function(configure_build_install_location _library_name)
# Install into test folder in build space for unit tests to import
set_target_properties(${_library_name} PROPERTIES
# Use generator expression to avoid prepending a build type specific directory on Windows
LIBRARY_OUTPUT_DIRECTORY $<1:${CMAKE_CURRENT_BINARY_DIR}/test>
RUNTIME_OUTPUT_DIRECTORY $<1:${CMAKE_CURRENT_BINARY_DIR}/test>)
# Set the build location and install location for a CPython extension
function(configure_build_install_location _library_name)
# Install into test folder in build space for unit tests to import
set_target_properties(${_library_name} PROPERTIES
# Use generator expression to avoid prepending a build type specific directory on Windows
LIBRARY_OUTPUT_DIRECTORY $<1:${CMAKE_CURRENT_BINARY_DIR}/test>
RUNTIME_OUTPUT_DIRECTORY $<1:${CMAKE_CURRENT_BINARY_DIR}/test>)

# Install library for actual use
install(TARGETS ${_library_name}
DESTINATION "${IGN_PYTHON_INSTALL_PATH}/"
)
endfunction()
# Install library for actual use
install(TARGETS ${_library_name}
DESTINATION "${IGN_PYTHON_INSTALL_PATH}/"
)
endfunction()

configure_build_install_location(math)
configure_build_install_location(math)

if (BUILD_TESTING)
# Add the Python tests
set(python_tests
Angle_TEST
AxisAlignedBox_TEST
Box_TEST
Color_TEST
Cylinder_TEST
DiffDriveOdometry_TEST
Filter_TEST
Frustum_TEST
GaussMarkovProcess_TEST
Helpers_TEST
Inertial_TEST
Kmeans_TEST
Line2_TEST
Line3_TEST
MassMatrix3_TEST
Material_TEST
Matrix3_TEST
Matrix4_TEST
MovingWindowFilter_TEST
OrientedBox_TEST
PID_TEST
Plane_TEST
Pose3_TEST
Quaternion_TEST
Rand_TEST
RollingMean_TEST
RotationSpline_TEST
SemanticVersion_TEST
SignalStats_TEST
Sphere_TEST
SphericalCoordinates_TEST
Spline_TEST
StopWatch_TEST
Temperature_TEST
Triangle3_TEST
Triangle_TEST
Vector2_TEST
Vector3_TEST
Vector3Stats_TEST
Vector4_TEST
)
if (BUILD_TESTING)
# Add the Python tests
set(python_tests
Angle_TEST
AxisAlignedBox_TEST
Box_TEST
Color_TEST
Cylinder_TEST
DiffDriveOdometry_TEST
Filter_TEST
Frustum_TEST
GaussMarkovProcess_TEST
Helpers_TEST
Inertial_TEST
Kmeans_TEST
Line2_TEST
Line3_TEST
MassMatrix3_TEST
Material_TEST
Matrix3_TEST
Matrix4_TEST
MovingWindowFilter_TEST
OrientedBox_TEST
PID_TEST
Plane_TEST
Pose3_TEST
Quaternion_TEST
Rand_TEST
RollingMean_TEST
RotationSpline_TEST
SemanticVersion_TEST
SignalStats_TEST
Sphere_TEST
SphericalCoordinates_TEST
Spline_TEST
StopWatch_TEST
Temperature_TEST
Triangle3_TEST
Triangle_TEST
Vector2_TEST
Vector3_TEST
Vector3Stats_TEST
Vector4_TEST
)

foreach (test ${python_tests})
add_test(NAME ${test}.py COMMAND
"${PYTHON_EXECUTABLE}" "${CMAKE_SOURCE_DIR}/src/python_pybind11/test/${test}.py")
foreach (test ${python_tests})
add_test(NAME ${test}.py COMMAND
"${PYTHON_EXECUTABLE}" "${CMAKE_SOURCE_DIR}/src/python_pybind11/test/${test}.py")

set(_env_vars)
list(APPEND _env_vars "PYTHONPATH=${FAKE_INSTALL_PREFIX}/lib/python/")
list(APPEND _env_vars "LD_LIBRARY_PATH=${FAKE_INSTALL_PREFIX}/lib:$ENV{LD_LIBRARY_PATH}")
set_tests_properties(${test}.py PROPERTIES
ENVIRONMENT "${_env_vars}")
endforeach()
set(_env_vars)
list(APPEND _env_vars "PYTHONPATH=${FAKE_INSTALL_PREFIX}/lib/python/")
list(APPEND _env_vars "LD_LIBRARY_PATH=${FAKE_INSTALL_PREFIX}/lib:$ENV{LD_LIBRARY_PATH}")
set_tests_properties(${test}.py PROPERTIES
ENVIRONMENT "${_env_vars}")
endforeach()

endif()
endif()

0 comments on commit fa67ea8

Please sign in to comment.