From 9e0401b8281928054fefc6144cf973586a41d603 Mon Sep 17 00:00:00 2001 From: Steve Peters Date: Thu, 31 Oct 2024 13:15:03 -0700 Subject: [PATCH] gz-math7: patch to build multiple python bindings (#2840) * Build bindings for both python@3.12 and python@3.13. * revision bump * update bottle. Signed-off-by: Steve Peters Co-authored-by: OSRF Build Bot --- Formula/gz-math7.rb | 65 +++++++++++++++++++++++++++++++++------------ 1 file changed, 48 insertions(+), 17 deletions(-) diff --git a/Formula/gz-math7.rb b/Formula/gz-math7.rb index 3bcb2833c..d8d326335 100644 --- a/Formula/gz-math7.rb +++ b/Formula/gz-math7.rb @@ -4,43 +4,72 @@ class GzMath7 < Formula url "https://osrf-distributions.s3.amazonaws.com/gz-math/releases/gz-math-7.5.1.tar.bz2" sha256 "cb1fc3436fd57ff9663613576fc208018ed6c11776972e555400d1b8bb7d2426" license "Apache-2.0" + revision 1 head "https://github.com/gazebosim/gz-math.git", branch: "gz-math7" bottle do root_url "https://osrf-distributions.s3.amazonaws.com/bottles-simulation" - sha256 cellar: :any, sonoma: "8ce91642ee2f4bdc18be0a63b758a7d634a200eebc95cd4a4e20a0940c5202b3" - sha256 cellar: :any, ventura: "faef596eaa7c1686fca3bcfa3ff0595d21128b480dd70ff66cef4fb542c3de0e" - sha256 cellar: :any, monterey: "30ff425a806ecc8894772834cc3a7ef07cb5c6808c48504fee7876d9dc2ca13b" + sha256 cellar: :any, sonoma: "fc68e0969e29460070b13ce4cbb49e53c9802821267ef6aad64e55bffca068ec" + sha256 cellar: :any, ventura: "1d95728e7a681a67aa0ac9899e9bcc1e7ca0f03835ba4c79f3e17eaa0b26d0a7" end depends_on "cmake" => :build depends_on "doxygen" => :build depends_on "pybind11" => :build + depends_on "python@3.12" => [:build, :test] + depends_on "python@3.13" => [:build, :test] + depends_on "pkg-config" => :test depends_on "eigen" depends_on "gz-cmake3" depends_on "gz-utils2" - depends_on "python@3.12" depends_on "ruby" - def python_cmake_arg - "-DPython3_EXECUTABLE=#{which("python3")}" + patch do + # Support building python bindings against external gz-math library (1) + # Remove this patch with the next release + url "https://github.com/gazebosim/gz-math/commit/97ad436a0d561c77422de83cebb600379cc9c94a.patch?full_index=1" + sha256 "6d2ed400c7067ac123c80d762f796d2b75e3b4ab479ee71b019e253ca41724e8" + end + + patch do + # Support building python bindings against external gz-math library (2) + # Remove this patch with the next release + url "https://github.com/gazebosim/gz-math/commit/a48b8937eadd4010a69b9f9613ca07aaa1f87d63.patch?full_index=1" + sha256 "fbd10f7b886511586ab84c7c03771a43da2fbb5c9445037a6534bbdbd2d05062" + end + + def pythons + deps.map(&:to_formula) + .select { |f| f.name.match?(/^python@3\.\d+$/) } + end + + def python_cmake_arg(python = "python@3.13".to_formula) + "-DPython3_EXECUTABLE=#{python.opt_libexec}/bin/python" end def install cmake_args = std_cmake_args cmake_args << "-DBUILD_TESTING=OFF" cmake_args << "-DCMAKE_INSTALL_RPATH=#{rpath}" - cmake_args << python_cmake_arg - # Use a build folder + # first build without python bindings mkdir "build" do - system "cmake", "..", *cmake_args + system "cmake", "..", *cmake_args, "-DSKIP_PYBIND11=ON" system "make", "install" end - (lib/"python3.12/site-packages").install Dir[lib/"python/*"] - rmdir prefix/"lib/python" + # now build only the python bindings + pythons.each do |python| + # remove @ from formula name + python_name = python.name.tr("@", "") + mkdir "build_#{python_name}" do + system "cmake", "../src/python_pybind11", *cmake_args, python_cmake_arg(python) + system "make", "install" + (lib/"#{python_name}/site-packages").install Dir[lib/"python/*"] + rmdir prefix/"lib/python" + end + end end test do @@ -59,12 +88,12 @@ def install add_executable(test_cmake test.cpp) target_link_libraries(test_cmake gz-math7::gz-math7) EOS - # test building with manual compiler flags + system "pkg-config", "gz-math7" + cflags = `pkg-config --cflags gz-math7`.split + ldflags = `pkg-config --libs gz-math7`.split system ENV.cc, "test.cpp", - "--std=c++14", - "-I#{include}/gz/math7", - "-L#{lib}", - "-lgz-math7", + *cflags, + *ldflags, "-lc++", "-o", "test" system "./test" @@ -78,6 +107,8 @@ def install cmd_not_grep_xcode = "! grep -rnI 'Applications[/]Xcode' #{prefix}" system cmd_not_grep_xcode # check python import - system Formula["python@3.12"].opt_libexec/"bin/python", "-c", "import gz.math7" + pythons.each do |python| + system python.opt_libexec/"bin/python", "-c", "import gz.math7" + end end end