Skip to content

Commit

Permalink
new formula pybind11 using py310
Browse files Browse the repository at this point in the history
  • Loading branch information
ipatch committed Dec 24, 2023
1 parent 37477ad commit 347a30e
Showing 1 changed file with 110 additions and 0 deletions.
110 changes: 110 additions & 0 deletions Formula/pybind11_py310.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
class Pybind11Py310 < Formula
desc "Seamless operability between C++11 and Python"
homepage "https://github.com/pybind/pybind11"
url "https://github.com/pybind/pybind11/archive/refs/tags/v2.11.1.tar.gz"
sha256 "d475978da0cdc2d43b73f30910786759d593a9d8ee05b1b6846d1eb16c6d2e0c"
license "BSD-3-Clause"

livecheck do
url :stable
regex(/^v?(\d+(?:\.\d+)+)$/i)
end

keg_only :versioned_formula

depends_on "cmake" => :build
depends_on "[email protected]" => [:build, :test]

on_macos do
depends_on "gettext" if MacOS.version == :mojave
end

def python3
"python3.10"
end

def install
# Install /include and /share/cmake to the global location
system "cmake", "-S", ".", "-B", "build", "-DPYBIND11_TEST=OFF", "-DPYBIND11_NOPYTHON=ON", *std_cmake_args
system "cmake", "--install", "build"

# Install Python package too
python_exe = Formula["[email protected]"].opt_bin/python3
system python_exe, "-m", "pip", "install", *std_pip_args(prefix: libexec), "."

site_packages = Language::Python.site_packages(python_exe)
pth_contents = "import site; site.addsitedir('#{libexec/site_packages}')\n"
(prefix/site_packages/"homebrew-pybind11.pth").write pth_contents

pyversion = Language::Python.major_minor_version(python_exe)
bin.install libexec/"bin/pybind11-config" => "pybind11-config-#{pyversion}"

# The newest one is used as the default
bin.install_symlink "pybind11-config-#{pyversion}" => "pybind11-config"
end

def caveats
<<-EOS
this is a versioned formula designed to work with the homebrew-freecad tap
out of the box, the `pybind11` python module will not be accessible
due to the formula being keg-only
EOS
end

test do

Check failure on line 54 in Formula/pybind11_py310.rb

View workflow job for this annotation

GitHub Actions / test-bot (self-hosted-mojavevm)

`brew test --verbose freecad/freecad/pybind11_py310` failed on macOS Mojave (10.14)!

/usr/local/Homebrew/Library/Homebrew/vendor/bundle/ruby/3.1.0/bin/bundle clean ==> Testing freecad/freecad/pybind11_py310 ==> /usr/bin/clang++ -shared -fPIC -O3 -std=c++11 example.cpp -o example.so -I/usr/local/opt/pybind11_py310/include -I/usr/local/opt/[email protected]/Frameworks/Python.framework/Versions/3.10/include/python3.10 -I/usr/local/opt/[email protected]/Frameworks/Python.framework/Versions/3.10/include/python3.10 -Wno-unused-result -Wsign-compare -Wunreachable-code -fno-common -dynamic -DNDEBUG -g -fwrapv -O3 -Wall -isysroot /Library/Developer/CommandLineTools/SDKs/MacOSX10.14.sdk -L/usr/local/opt/[email protected]/Frameworks/Python.framework/Versions/3.10/lib/python3.10/config-3.10-darwin -lpython3.10 -lintl -ldl -framework CoreFoundation -L/usr/local/opt/gettext/lib ==> /usr/local/opt/[email protected]/bin/python3.10 example.py ==> /usr/local/opt/[email protected]/bin/python3.10 -m pybind11 --includes ==> /usr/local/Cellar/pybind11_py310/2.11.1/bin/pybind11-config-3.10 --includes Error: freecad/freecad/pybind11_py310: failed An exception occurred within a child process: NameError: undefined local variable or method `pythons' for #<Formula pybind11_py310 (stable) /usr/local/Homebrew/Library/Taps/freecad/homebrew-freecad/Formula/pybind11_py310.rb> Did you mean? python python3 /usr/local/Homebrew/Library/Taps/freecad/homebrew-freecad/Formula/pybind11_py310.rb:109:in `block in <class:Pybind11Py310>' /usr/local/Homebrew/Library/Homebrew/formula.rb:2500:in `block (3 levels) in run_test' /usr/local/Homebrew/Library/Homebrew/extend/kernel.rb:493:in `with_env' /usr/local/Homebrew/Library/Homebrew/formula.rb:2499:in `block (2 levels) in run_test' /usr/local/Homebrew/Library/Homebrew/formula.rb:1055:in `with_logging' /usr/local/Homebrew/Library/Homebrew/formula.rb:2498:in `block in run_test' /usr/local/Homebrew/Library/Homebrew/mktemp.rb:75:in `block in run' /usr/local/Homebrew/Library/Homebrew/mktemp.rb:75:in `chdir' /usr/local/Homebrew/Library/Homebrew/mktemp.rb:75:in `run' /usr/local/Homebrew/Library/Homebrew/formula.rb:2792:in `mktemp' /usr/local/Homebrew/Library/Homebrew/formula.rb:2492:in `run_test' /usr/local/Homebrew/Library/Homebrew/test.rb:46:in `block in <main>' /usr/local/Homebrew/Library/Homebrew/vendor/portable-ruby/3.1.4/lib/ruby/3.1.0/timeout.rb:107:in `block in timeout' /usr/local/Homebrew/Library/Homebrew/vendor/portable-ruby/3.1.4/lib/ruby/3.1.0/timeout.rb:36:in `block in catch' /usr/local/Homebrew/Library/Homebrew/vendor/portable-ruby/3.1.4/lib/ruby/3.1.0/timeout.rb:36:in `catch' /usr/local/Homebrew/Library/Homebrew/vendor/portable-ruby/3.1.4/lib/ruby/3.1.0/timeout.rb:36:in `catch' /usr/local/Homebrew/Library/Homebrew/vendor/portable-ruby/3.1.4/lib/ruby/3.1.0/timeout.rb:123:in `timeout' /usr/local/Homebrew/Library/Homebrew/test.rb:50:in `<main>'
# NOTE: required env vars due to formula being keg-only
ENV.append_path "PATH", Formula["pybind11_py310"].opt_bin
ENV.append_path "PYTHONPATH", "#{Formula["pybind11_py310"].opt_libexec}/lib/python3.10/site-packages"
ENV.append_path "LD_LIBRARY_PATH", Formula["pybind11_py310"].opt_lib

(testpath/"example.cpp").write <<~EOS
#include <pybind11/pybind11.h>
int add(int i, int j) {
return i + j;
}
namespace py = pybind11;
PYBIND11_MODULE(example, m) {
m.doc() = "pybind11 example plugin";
m.def("add", &add, "A function which adds two numbers");
}
EOS

(testpath/"example.py").write <<~EOS
import example
example.add(1,2)
EOS

python_exe = "#{Formula["[email protected]"].opt_bin}/python3.10"
python = "#{Formula["[email protected]"].opt_bin}/python3.10"
python_config = "#{Formula["[email protected]"].opt_bin}/python3.10-config"
pyversion = Language::Python.major_minor_version(python_exe)

# NOTE: ci will fail with trailing `pybind11` in below but not locally
pybind11_include = Formula["pybind11_py310"].opt_include
gettext_lib = Formula["gettext"].opt_lib
python_flags = Utils.safe_popen_read(
python_config,
"--cflags",
"--ldflags",
"--embed",
).split
system ENV.cxx, "-shared", "-fPIC", "-O3", "-std=c++11", "example.cpp", "-o", "example.so",
"-I#{pybind11_include}", *python_flags, "-L#{gettext_lib}"
system python_exe, "example.py"

test_module = shell_output("#{python_exe} -m pybind11 --includes")
# Use assert_match method correctly to check if the output includes the expected string
expected_module_path = "#{Formula["pybind11_py310"].opt_libexec}/lib/python3.10/site-packages/pybind11/include"
assert_match expected_module_path, test_module

test_script = shell_output("#{bin}/pybind11-config-#{pyversion} --includes")
assert_match test_module, test_script

next if python != pythons.max_by(&:version)

test_module = shell_output("#{python_exe} -m pybind11 --includes")
test_script = shell_output("#{bin}/pybind11-config --includes")
assert_match test_module, test_script
end
end

0 comments on commit 347a30e

Please sign in to comment.