From 1985c90c47ee6cb64fb892aa4d310790ea07847e Mon Sep 17 00:00:00 2001 From: Silvio Traversaro Date: Fri, 27 Sep 2024 13:27:06 +0200 Subject: [PATCH 1/3] Support building bindings as standalone project Signed-off-by: Silvio Traversaro --- python/CMakeLists.txt | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/python/CMakeLists.txt b/python/CMakeLists.txt index 1ad2cc7a7..8e90b1e28 100644 --- a/python/CMakeLists.txt +++ b/python/CMakeLists.txt @@ -1,3 +1,19 @@ +# Detect if we are doing a standalone build of the bindings, using an external gz-math +if(CMAKE_SOURCE_DIR STREQUAL CMAKE_CURRENT_SOURCE_DIR) + cmake_minimum_required(VERSION 3.16) + set(SDF_VER 15) + set(GZ_UTILS_VER 3) + project(sdformat${SDF_VER}-python VERSION ${SDF_VER}) + find_package(Python3 COMPONENTS Interpreter Development REQUIRED) + find_package(pybind11 REQUIRED) + find_package(sdformat${SDF_VER} REQUIRED) + set(PROJECT_LIBRARY_TARGET_NAME "sdformat${PROJECT_VERSION_MAJOR}::sdformat${PROJECT_VERSION_MAJOR}") + include(CTest) + if(BUILD_TESTING) + enable_testing() + endif() +endif() + if(USE_SYSTEM_PATHS_FOR_PYTHON_INSTALLATION) if(NOT Python3_SITEARCH) # Get variable from Python3 module From 4644d3b3b28351d5b2c5e36af6e7d0475c0d8eb5 Mon Sep 17 00:00:00 2001 From: Steve Peters Date: Tue, 15 Oct 2024 13:35:35 +0200 Subject: [PATCH 2/3] Fix python install path Use CMAKE_INSTALL_LIBDIR from GNUInstallDirs instead of GZ_LIB_INSTALL_DIR, which won't be available if only building python bindings. Signed-off-by: Steve Peters --- python/CMakeLists.txt | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/python/CMakeLists.txt b/python/CMakeLists.txt index 8e90b1e28..14c6607b9 100644 --- a/python/CMakeLists.txt +++ b/python/CMakeLists.txt @@ -8,6 +8,7 @@ if(CMAKE_SOURCE_DIR STREQUAL CMAKE_CURRENT_SOURCE_DIR) find_package(pybind11 REQUIRED) find_package(sdformat${SDF_VER} REQUIRED) set(PROJECT_LIBRARY_TARGET_NAME "sdformat${PROJECT_VERSION_MAJOR}::sdformat${PROJECT_VERSION_MAJOR}") + include(GNUInstallDirs) include(CTest) if(BUILD_TESTING) enable_testing() @@ -28,7 +29,7 @@ if(USE_SYSTEM_PATHS_FOR_PYTHON_INSTALLATION) endif() else() # If not a system installation, respect local paths - set(GZ_PYTHON_INSTALL_PATH ${GZ_LIB_INSTALL_DIR}/python) + set(GZ_PYTHON_INSTALL_PATH ${CMAKE_INSTALL_LIBDIR}/python) endif() # Set the build location and install location for a CPython extension From 1d2462a4d2d31389809c26136d4bdcec44c189b4 Mon Sep 17 00:00:00 2001 From: Steve Peters Date: Tue, 15 Oct 2024 14:30:46 +0200 Subject: [PATCH 3/3] Add details about building python bindings * Document SKIP_PYBIND11 cmake option * Document how to build bindings separately from the main libsdformat library Signed-off-by: Steve Peters --- README.md | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/README.md b/README.md index c70b18b9b..e157f9a05 100644 --- a/README.md +++ b/README.md @@ -165,9 +165,32 @@ sdformat supported cmake parameters at configuring time: | Name | Type | Default | Description | |-----------------------|------|----------|--------------------------------------| +| `SKIP_PYBIND11` | BOOL | False | Skip generating Python bindings via pybind11 | | `USE_INTERNAL_URDF` | BOOL | False | Use an internal copy of urdfdom 1.0.0 instead of looking for one installed in the system | | `USE_UPSTREAM_CFLAGS` | BOOL | True | Use the sdformat team compilation flags instead of the common set defined by cmake. | +### Build python bindings separately from main library + +If you want to build Python bindings separately from the main libsdformat library +(for example if you want to build Python bindings for multiple versions of Python), +you can invoke cmake on the `python` folder instead of the root folder. +Specify the path to the python executable with which you wish to build bindings +in the `Python3_EXECUTABLE` cmake variable. +Specify the install path for the bindings in the `CMAKE_INSTALL_PREFIX` +variable, and be sure to set your `PYTHONPATH` accordingly after install. + +```bash +cd sdformat +mkdir build_python3 +cd build_python3 +cmake ../python \ + -DPython3_EXECUTABLE=/usr/local/bin/python3.12 \ + -DCMAKE_INSTALL_PREFIX= +``` + +See the homebrew [sdformat15 formula](https://github.com/osrf/homebrew-simulation/blob/027d06f5be49da1e40d01180aedae7f76dc7ff47/Formula/sdformat15.rb#L12-L56) +for an example of building bindings for multiple versions of Python. + ## Uninstallation To uninstall the software installed with the previous steps: