Skip to content

Commit fa67ea8

Browse files
chapulinascpeters
andauthored
Refactor finding pybind11 (#360)
Signed-off-by: Louise Poubel <louise@openrobotics.org> Signed-off-by: Steve Peters <scpeters@openrobotics.org> Co-authored-by: Steve Peters <scpeters@openrobotics.org>
1 parent 4b25160 commit fa67ea8

File tree

3 files changed

+137
-130
lines changed

3 files changed

+137
-130
lines changed

CMakeLists.txt

+18-7
Original file line numberDiff line numberDiff line change
@@ -67,15 +67,26 @@ if (SWIG_FOUND)
6767
else()
6868
message (STATUS "Searching for Ruby - found version ${RUBY_VERSION}.")
6969
endif()
70+
endif()
7071

71-
########################################
72-
# Include python
73-
find_package(PythonInterp 3 REQUIRED) # change to Python3 when Bionic is EOL
74-
find_package(PythonLibs QUIET)
75-
if (NOT PYTHONLIBS_FOUND)
76-
message (STATUS "Searching for Python - not found.")
72+
########################################
73+
# Python bindings
74+
include(IgnPython)
75+
find_package(PythonLibs QUIET)
76+
if (NOT PYTHONLIBS_FOUND)
77+
IGN_BUILD_WARNING("Python is missing: Python interfaces are disabled.")
78+
message (STATUS "Searching for Python - not found.")
79+
else()
80+
message (STATUS "Searching for Python - found version ${PYTHONLIBS_VERSION_STRING}.")
81+
82+
set(PYBIND11_PYTHON_VERSION 3)
83+
find_package(pybind11 2.2 QUIET)
84+
85+
if (${pybind11_FOUND})
86+
message (STATUS "Searching for pybind11 - found version ${pybind11_VERSION}.")
7787
else()
78-
message (STATUS "Searching for Python - found version ${PYTHONLIBS_VERSION_STRING}.")
88+
IGN_BUILD_WARNING("pybind11 is missing: Python interfaces are disabled.")
89+
message (STATUS "Searching for pybind11 - not found.")
7990
endif()
8091
endif()
8192

src/CMakeLists.txt

+3-1
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,7 @@ ign_build_tests(TYPE UNIT SOURCES ${gtest_sources})
1313
add_subdirectory(graph)
1414

1515
# Bindings subdirectories
16-
add_subdirectory(python_pybind11)
16+
if (${pybind11_FOUND})
17+
add_subdirectory(python_pybind11)
18+
endif()
1719
add_subdirectory(ruby)

src/python_pybind11/CMakeLists.txt

+116-122
Original file line numberDiff line numberDiff line change
@@ -5,139 +5,133 @@ if(WIN32 AND CMAKE_BUILD_TYPE STREQUAL "Debug")
55
set(PYTHON_LIBRARIES "${PYTHON_DEBUG_LIBRARIES}")
66
endif()
77

8-
set(PYBIND11_PYTHON_VERSION 3)
8+
message(STATUS "Building pybind11 interfaces")
9+
# Split from main extension and converted to pybind11
10+
pybind11_add_module(math SHARED
11+
src/_ignition_math_pybind11.cc
12+
src/Angle.cc
13+
src/AxisAlignedBox.cc
14+
src/Color.cc
15+
src/DiffDriveOdometry.cc
16+
src/Frustum.cc
17+
src/GaussMarkovProcess.cc
18+
src/Helpers.cc
19+
src/Kmeans.cc
20+
src/Material.cc
21+
src/PID.cc
22+
src/Rand.cc
23+
src/RollingMean.cc
24+
src/RotationSpline.cc
25+
src/SemanticVersion.cc
26+
src/SignalStats.cc
27+
src/SphericalCoordinates.cc
28+
src/Spline.cc
29+
src/StopWatch.cc
30+
src/Temperature.cc
31+
src/Vector3Stats.cc
32+
)
933

10-
find_package(pybind11 2.2 QUIET)
34+
target_link_libraries(math PRIVATE
35+
${PROJECT_LIBRARY_TARGET_NAME}
36+
)
1137

12-
if (${pybind11_FOUND})
13-
message(STATUS "Building pybind11 interfaces")
14-
# Split from main extension and converted to pybind11
15-
pybind11_add_module(math SHARED
16-
src/_ignition_math_pybind11.cc
17-
src/Angle.cc
18-
src/AxisAlignedBox.cc
19-
src/Color.cc
20-
src/DiffDriveOdometry.cc
21-
src/Frustum.cc
22-
src/GaussMarkovProcess.cc
23-
src/Helpers.cc
24-
src/Kmeans.cc
25-
src/Material.cc
26-
src/PID.cc
27-
src/Rand.cc
28-
src/RollingMean.cc
29-
src/RotationSpline.cc
30-
src/SemanticVersion.cc
31-
src/SignalStats.cc
32-
src/SphericalCoordinates.cc
33-
src/Spline.cc
34-
src/StopWatch.cc
35-
src/Temperature.cc
36-
src/Vector3Stats.cc
37-
)
38-
39-
target_link_libraries(math PRIVATE
40-
${PROJECT_LIBRARY_TARGET_NAME}
41-
)
42-
43-
if(USE_SYSTEM_PATHS_FOR_PYTHON_INSTALLATION)
44-
if(${CMAKE_VERSION} VERSION_LESS "3.12.0")
45-
execute_process(
46-
COMMAND "${PYTHON_EXECUTABLE}" -c "if True:
47-
from distutils import sysconfig as sc
48-
print(sc.get_python_lib(plat_specific=True))"
49-
OUTPUT_VARIABLE Python3_SITEARCH
50-
OUTPUT_STRIP_TRAILING_WHITESPACE)
51-
else()
52-
# Get install variable from Python3 module
53-
# Python3_SITEARCH is available from 3.12 on, workaround if needed:
54-
find_package(Python3 COMPONENTS Interpreter)
55-
endif()
38+
if(USE_SYSTEM_PATHS_FOR_PYTHON_INSTALLATION)
39+
if(${CMAKE_VERSION} VERSION_LESS "3.12.0")
40+
execute_process(
41+
COMMAND "${PYTHON_EXECUTABLE}" -c "if True:
42+
from distutils import sysconfig as sc
43+
print(sc.get_python_lib(plat_specific=True))"
44+
OUTPUT_VARIABLE Python3_SITEARCH
45+
OUTPUT_STRIP_TRAILING_WHITESPACE)
46+
else()
47+
# Get install variable from Python3 module
48+
# Python3_SITEARCH is available from 3.12 on, workaround if needed:
49+
find_package(Python3 COMPONENTS Interpreter)
50+
endif()
5651

57-
if(USE_DIST_PACKAGES_FOR_PYTHON)
58-
string(REPLACE "site-packages" "dist-packages" IGN_PYTHON_INSTALL_PATH ${Python3_SITEARCH})
59-
else()
60-
# custom cmake command is returning dist-packages
61-
string(REPLACE "dist-packages" "site-packages" IGN_PYTHON_INSTALL_PATH ${Python3_SITEARCH})
62-
endif()
52+
if(USE_DIST_PACKAGES_FOR_PYTHON)
53+
string(REPLACE "site-packages" "dist-packages" IGN_PYTHON_INSTALL_PATH ${Python3_SITEARCH})
6354
else()
64-
# If not a system installation, respect local paths
65-
set(IGN_PYTHON_INSTALL_PATH ${IGN_LIB_INSTALL_DIR}/python)
55+
# custom cmake command is returning dist-packages
56+
string(REPLACE "dist-packages" "site-packages" IGN_PYTHON_INSTALL_PATH ${Python3_SITEARCH})
6657
endif()
58+
else()
59+
# If not a system installation, respect local paths
60+
set(IGN_PYTHON_INSTALL_PATH ${IGN_LIB_INSTALL_DIR}/python)
61+
endif()
6762

68-
set(IGN_PYTHON_INSTALL_PATH "${IGN_PYTHON_INSTALL_PATH}/ignition")
63+
set(IGN_PYTHON_INSTALL_PATH "${IGN_PYTHON_INSTALL_PATH}/ignition")
6964

70-
# Set the build location and install location for a CPython extension
71-
function(configure_build_install_location _library_name)
72-
# Install into test folder in build space for unit tests to import
73-
set_target_properties(${_library_name} PROPERTIES
74-
# Use generator expression to avoid prepending a build type specific directory on Windows
75-
LIBRARY_OUTPUT_DIRECTORY $<1:${CMAKE_CURRENT_BINARY_DIR}/test>
76-
RUNTIME_OUTPUT_DIRECTORY $<1:${CMAKE_CURRENT_BINARY_DIR}/test>)
65+
# Set the build location and install location for a CPython extension
66+
function(configure_build_install_location _library_name)
67+
# Install into test folder in build space for unit tests to import
68+
set_target_properties(${_library_name} PROPERTIES
69+
# Use generator expression to avoid prepending a build type specific directory on Windows
70+
LIBRARY_OUTPUT_DIRECTORY $<1:${CMAKE_CURRENT_BINARY_DIR}/test>
71+
RUNTIME_OUTPUT_DIRECTORY $<1:${CMAKE_CURRENT_BINARY_DIR}/test>)
7772

78-
# Install library for actual use
79-
install(TARGETS ${_library_name}
80-
DESTINATION "${IGN_PYTHON_INSTALL_PATH}/"
81-
)
82-
endfunction()
73+
# Install library for actual use
74+
install(TARGETS ${_library_name}
75+
DESTINATION "${IGN_PYTHON_INSTALL_PATH}/"
76+
)
77+
endfunction()
8378

84-
configure_build_install_location(math)
79+
configure_build_install_location(math)
8580

86-
if (BUILD_TESTING)
87-
# Add the Python tests
88-
set(python_tests
89-
Angle_TEST
90-
AxisAlignedBox_TEST
91-
Box_TEST
92-
Color_TEST
93-
Cylinder_TEST
94-
DiffDriveOdometry_TEST
95-
Filter_TEST
96-
Frustum_TEST
97-
GaussMarkovProcess_TEST
98-
Helpers_TEST
99-
Inertial_TEST
100-
Kmeans_TEST
101-
Line2_TEST
102-
Line3_TEST
103-
MassMatrix3_TEST
104-
Material_TEST
105-
Matrix3_TEST
106-
Matrix4_TEST
107-
MovingWindowFilter_TEST
108-
OrientedBox_TEST
109-
PID_TEST
110-
Plane_TEST
111-
Pose3_TEST
112-
Quaternion_TEST
113-
Rand_TEST
114-
RollingMean_TEST
115-
RotationSpline_TEST
116-
SemanticVersion_TEST
117-
SignalStats_TEST
118-
Sphere_TEST
119-
SphericalCoordinates_TEST
120-
Spline_TEST
121-
StopWatch_TEST
122-
Temperature_TEST
123-
Triangle3_TEST
124-
Triangle_TEST
125-
Vector2_TEST
126-
Vector3_TEST
127-
Vector3Stats_TEST
128-
Vector4_TEST
129-
)
81+
if (BUILD_TESTING)
82+
# Add the Python tests
83+
set(python_tests
84+
Angle_TEST
85+
AxisAlignedBox_TEST
86+
Box_TEST
87+
Color_TEST
88+
Cylinder_TEST
89+
DiffDriveOdometry_TEST
90+
Filter_TEST
91+
Frustum_TEST
92+
GaussMarkovProcess_TEST
93+
Helpers_TEST
94+
Inertial_TEST
95+
Kmeans_TEST
96+
Line2_TEST
97+
Line3_TEST
98+
MassMatrix3_TEST
99+
Material_TEST
100+
Matrix3_TEST
101+
Matrix4_TEST
102+
MovingWindowFilter_TEST
103+
OrientedBox_TEST
104+
PID_TEST
105+
Plane_TEST
106+
Pose3_TEST
107+
Quaternion_TEST
108+
Rand_TEST
109+
RollingMean_TEST
110+
RotationSpline_TEST
111+
SemanticVersion_TEST
112+
SignalStats_TEST
113+
Sphere_TEST
114+
SphericalCoordinates_TEST
115+
Spline_TEST
116+
StopWatch_TEST
117+
Temperature_TEST
118+
Triangle3_TEST
119+
Triangle_TEST
120+
Vector2_TEST
121+
Vector3_TEST
122+
Vector3Stats_TEST
123+
Vector4_TEST
124+
)
130125

131-
foreach (test ${python_tests})
132-
add_test(NAME ${test}.py COMMAND
133-
"${PYTHON_EXECUTABLE}" "${CMAKE_SOURCE_DIR}/src/python_pybind11/test/${test}.py")
126+
foreach (test ${python_tests})
127+
add_test(NAME ${test}.py COMMAND
128+
"${PYTHON_EXECUTABLE}" "${CMAKE_SOURCE_DIR}/src/python_pybind11/test/${test}.py")
134129

135-
set(_env_vars)
136-
list(APPEND _env_vars "PYTHONPATH=${FAKE_INSTALL_PREFIX}/lib/python/")
137-
list(APPEND _env_vars "LD_LIBRARY_PATH=${FAKE_INSTALL_PREFIX}/lib:$ENV{LD_LIBRARY_PATH}")
138-
set_tests_properties(${test}.py PROPERTIES
139-
ENVIRONMENT "${_env_vars}")
140-
endforeach()
130+
set(_env_vars)
131+
list(APPEND _env_vars "PYTHONPATH=${FAKE_INSTALL_PREFIX}/lib/python/")
132+
list(APPEND _env_vars "LD_LIBRARY_PATH=${FAKE_INSTALL_PREFIX}/lib:$ENV{LD_LIBRARY_PATH}")
133+
set_tests_properties(${test}.py PROPERTIES
134+
ENVIRONMENT "${_env_vars}")
135+
endforeach()
141136

142-
endif()
143137
endif()

0 commit comments

Comments
 (0)