Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

gz-math7: patch to build multiple python bindings #2840

Merged
merged 5 commits into from
Oct 31, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 48 additions & 17 deletions Formula/gz-math7.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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 "[email protected]" => [:build, :test]
depends_on "[email protected]" => [:build, :test]
depends_on "pkg-config" => :test
depends_on "eigen"
depends_on "gz-cmake3"
depends_on "gz-utils2"
depends_on "[email protected]"
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 = "[email protected]".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
Expand All @@ -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"
Expand All @@ -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["[email protected]"].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
Loading