From 646de60598cb728b1f8df1ab1ba9394bbd8b8879 Mon Sep 17 00:00:00 2001 From: Silvio Traversaro Date: Thu, 26 Sep 2024 21:21:19 +0200 Subject: [PATCH 1/7] Permit to build bindings against an external gz-math Signed-off-by: Silvio Traversaro --- src/python_pybind11/CMakeLists.txt | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/src/python_pybind11/CMakeLists.txt b/src/python_pybind11/CMakeLists.txt index a3c6fc31..5ca06ce2 100644 --- a/src/python_pybind11/CMakeLists.txt +++ b/src/python_pybind11/CMakeLists.txt @@ -1,3 +1,18 @@ +# 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(GZ_MATH_VER 8) + project(gz-math${GZ_MATH_VER}-python VERSION ${GZ_MATH_VER}) + find_package(Python3 COMPONENTS Interpreter Development REQUIRED) + find_package(pybind11 REQUIRED) + find_package(gz-math${PROJECT_VERSION_MAJOR} REQUIRED) + set(PROJECT_LIBRARY_TARGET_NAME "gz-math${PROJECT_VERSION_MAJOR}::gz-math${PROJECT_VERSION_MAJOR}") + include(CTest) + if(BUILD_TESTING) + enable_testing() + endif() +endif() + message(STATUS "Building pybind11 interfaces") set(BINDINGS_MODULE_NAME "math${PROJECT_VERSION_MAJOR}") # Split from main extension and converted to pybind11 From d2bae803353fb929dbbc279fe003da77dfef000b Mon Sep 17 00:00:00 2001 From: Steve Peters Date: Fri, 11 Oct 2024 12:28:29 -0700 Subject: [PATCH 2/7] 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 --- src/python_pybind11/CMakeLists.txt | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/python_pybind11/CMakeLists.txt b/src/python_pybind11/CMakeLists.txt index 5ca06ce2..0b844652 100644 --- a/src/python_pybind11/CMakeLists.txt +++ b/src/python_pybind11/CMakeLists.txt @@ -7,6 +7,7 @@ if(CMAKE_SOURCE_DIR STREQUAL CMAKE_CURRENT_SOURCE_DIR) find_package(pybind11 REQUIRED) find_package(gz-math${PROJECT_VERSION_MAJOR} REQUIRED) set(PROJECT_LIBRARY_TARGET_NAME "gz-math${PROJECT_VERSION_MAJOR}::gz-math${PROJECT_VERSION_MAJOR}") + include(GNUInstallDirs) include(CTest) if(BUILD_TESTING) enable_testing() @@ -96,7 +97,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(GZ_PYTHON_INSTALL_PATH "${GZ_PYTHON_INSTALL_PATH}/gz") From a670b65a857ab3e53ad1d35b5a6cf6a3fabda925 Mon Sep 17 00:00:00 2001 From: Steve Peters Date: Mon, 14 Oct 2024 16:15:54 +0200 Subject: [PATCH 3/7] Reference brew install instructions in tutorial Refer to https://brew.sh instead of duplicating the brew installation command. Signed-off-by: Steve Peters --- tutorials/installation.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tutorials/installation.md b/tutorials/installation.md index 94e77b5d..9b29e848 100644 --- a/tutorials/installation.md +++ b/tutorials/installation.md @@ -38,9 +38,9 @@ you should use `ign-math<#>`. ### macOS -On macOS, add OSRF packages: +On macOS, after installing the [Homebrew package manager](https://brew.sh), +add OSRF packages: ``` - /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)" brew tap osrf/simulation ``` From 54978b92b2209d665fd6961f7e5f03fe846a44ca Mon Sep 17 00:00:00 2001 From: Steve Peters Date: Mon, 14 Oct 2024 16:16:47 +0200 Subject: [PATCH 4/7] Add details about building python bindings * Describe Pybind11 as a dependency * Document how to build bindings separately from the main gz-math library Signed-off-by: Steve Peters --- tutorials/installation.md | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/tutorials/installation.md b/tutorials/installation.md index 9b29e848..63252e1b 100644 --- a/tutorials/installation.md +++ b/tutorials/installation.md @@ -92,6 +92,13 @@ The optional Eigen component of Gazebo Math requires: sudo apt-get install libeigen3-dev ``` +The optional Python bindings of Gazebo Math require: + +* [Pybind11](https://pybind11.readthedocs.io/en/stable/index.html). Refer to the [Pybind11 Documentation](https://pybind11.readthedocs.io/en/stable/installing.html) for installation instructions. On Ubuntu systems, `apt-get` can be used to install Pybind11: + ``` + sudo apt-get install python3-pybind11 + ``` + The optional Ruby tests of Gazebo Math require: * [Ruby](https://www.ruby-lang.org/). Refer to the [Ruby Documentation](https://www.ruby-lang.org/downloads/) for installation instructions. On Ubuntu systems `apt-get` can be used to install Ubuntu Package `ruby-dev`: @@ -228,6 +235,34 @@ The optional Eigen component of Gazebo Math requires: cmake --install . --config Release ``` +### cmake parameters + +| Name | Type | Default | Description | +|-----------------|------|---------|--------------------------------------------| +| `SKIP_PYBIND11` | BOOL | OFF | Set to ON to skip building python bindings | + +### Building Python bindings separately from main library + +If you want to build Python bindings separately from the main gz-math library +(for example if you want to build Python bindings for multiple versions of Python), +you can invoke cmake on the `src/python_pybind11` 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 gz-math +mkdir build_python3 +cd build_python3 +cmake ../src/python_pybind11 \ + -DPython3_EXECUTABLE=/usr/local/bin/python3.12 \ + -DCMAKE_INSTALL_PREFIX= +``` + +See the homebrew [gz-math8 formula](https://github.com/osrf/homebrew-simulation/blob/ccda47647ed9aeb38f0ea1ec8804fd1501058de1/Formula/gz-math8.rb#L12-L52) +for an example of building bindings for multiple versions of Python. + # Documentation API and tutorials can be found at [https://gazebosim.org/libs/math](https://gazebosim.org/libs/math). From cb32516fdeea8d6885013da38e6c85f0a91e0c9c Mon Sep 17 00:00:00 2001 From: Steve Peters Date: Fri, 18 Oct 2024 14:06:05 +0200 Subject: [PATCH 5/7] Use same required cmake version Signed-off-by: Steve Peters --- src/python_pybind11/CMakeLists.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/python_pybind11/CMakeLists.txt b/src/python_pybind11/CMakeLists.txt index 0b844652..368b39cc 100644 --- a/src/python_pybind11/CMakeLists.txt +++ b/src/python_pybind11/CMakeLists.txt @@ -1,6 +1,6 @@ # 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) + cmake_minimum_required(VERSION 3.22.1) set(GZ_MATH_VER 8) project(gz-math${GZ_MATH_VER}-python VERSION ${GZ_MATH_VER}) find_package(Python3 COMPONENTS Interpreter Development REQUIRED) From 63d16a8ba78bbb9d66182e23eedbd65e0942c545 Mon Sep 17 00:00:00 2001 From: Steve Peters Date: Fri, 18 Oct 2024 14:06:17 +0200 Subject: [PATCH 6/7] Remove unneeded call to enable_testing() Signed-off-by: Steve Peters --- src/python_pybind11/CMakeLists.txt | 3 --- 1 file changed, 3 deletions(-) diff --git a/src/python_pybind11/CMakeLists.txt b/src/python_pybind11/CMakeLists.txt index 368b39cc..af81f33f 100644 --- a/src/python_pybind11/CMakeLists.txt +++ b/src/python_pybind11/CMakeLists.txt @@ -9,9 +9,6 @@ if(CMAKE_SOURCE_DIR STREQUAL CMAKE_CURRENT_SOURCE_DIR) set(PROJECT_LIBRARY_TARGET_NAME "gz-math${PROJECT_VERSION_MAJOR}::gz-math${PROJECT_VERSION_MAJOR}") include(GNUInstallDirs) include(CTest) - if(BUILD_TESTING) - enable_testing() - endif() endif() message(STATUS "Building pybind11 interfaces") From 06b8ab8b275650309f45578d9fe852d64dade47a Mon Sep 17 00:00:00 2001 From: Steve Peters Date: Fri, 18 Oct 2024 15:31:55 +0200 Subject: [PATCH 7/7] Find pybind11 in just one place Moves find_package(pybind11) call to src/python_pybind11 folder. When invoked through the root CMakeLists.txt, it treats pybind11 as an optional dependency, but when invoked from that folder, it treats it as required by setting CMAKE_REQUIRE_FIND_PACKAGE_pybind11 to TRUE. Signed-off-by: Steve Peters --- CMakeLists.txt | 10 ---------- src/CMakeLists.txt | 2 +- src/python_pybind11/CMakeLists.txt | 15 +++++++++++++-- 3 files changed, 14 insertions(+), 13 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index f9fc1f82..342d4465 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -105,16 +105,6 @@ else() ) if (NOT Python3_Development_FOUND) GZ_BUILD_WARNING("Python development libraries are missing: Python interfaces are disabled.") - else() - set(PYBIND11_PYTHON_VERSION 3) - find_package(pybind11 2.2 QUIET) - - if (${pybind11_FOUND}) - message (STATUS "Searching for pybind11 - found version ${pybind11_VERSION}.") - else() - GZ_BUILD_WARNING("pybind11 is missing: Python interfaces are disabled.") - message (STATUS "Searching for pybind11 - not found.") - endif() endif() endif() diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 0adbed14..85582b6f 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -18,7 +18,7 @@ gz_build_tests(TYPE UNIT SOURCES ${gtest_sources}) add_subdirectory(graph) # Bindings subdirectories -if (pybind11_FOUND AND NOT SKIP_PYBIND11) +if (Python3_Development_FOUND AND NOT SKIP_PYBIND11) add_subdirectory(python_pybind11) endif() diff --git a/src/python_pybind11/CMakeLists.txt b/src/python_pybind11/CMakeLists.txt index af81f33f..89af730d 100644 --- a/src/python_pybind11/CMakeLists.txt +++ b/src/python_pybind11/CMakeLists.txt @@ -3,14 +3,25 @@ if(CMAKE_SOURCE_DIR STREQUAL CMAKE_CURRENT_SOURCE_DIR) cmake_minimum_required(VERSION 3.22.1) set(GZ_MATH_VER 8) project(gz-math${GZ_MATH_VER}-python VERSION ${GZ_MATH_VER}) - find_package(Python3 COMPONENTS Interpreter Development REQUIRED) - find_package(pybind11 REQUIRED) find_package(gz-math${PROJECT_VERSION_MAJOR} REQUIRED) set(PROJECT_LIBRARY_TARGET_NAME "gz-math${PROJECT_VERSION_MAJOR}::gz-math${PROJECT_VERSION_MAJOR}") + # require python dependencies to be found + find_package(Python3 COMPONENTS Interpreter Development REQUIRED) + set(CMAKE_REQUIRE_FIND_PACKAGE_pybind11 TRUE) include(GNUInstallDirs) include(CTest) endif() +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(WARNING "pybind11 is missing: Python interfaces are disabled.") + return() +endif() + message(STATUS "Building pybind11 interfaces") set(BINDINGS_MODULE_NAME "math${PROJECT_VERSION_MAJOR}") # Split from main extension and converted to pybind11