Skip to content

Instantly share code, notes, and snippets.

@slavik112211
Last active August 9, 2018 22:50
Show Gist options
  • Save slavik112211/f433cae2426864836bfc4e4bf59d41cf to your computer and use it in GitHub Desktop.
Save slavik112211/f433cae2426864836bfc4e4bf59d41cf to your computer and use it in GitHub Desktop.
class BoostPython < Formula
desc "C++ library for C++/Python interoperability"
homepage "https://www.boost.org/"
url "https://downloads.sourceforge.net/project/boost/boost/1.63.0/boost_1_63_0.tar.bz2"
sha256 "beae2529f759f6b3bf3f4969a19c2e9d6f0c503edcb2de4a61d1428519fcb3b0"
head "https://github.com/boostorg/boost.git"
bottle do
cellar :any
sha256 "3b47066b435fc7b9acb677969bf2fa54e634a45ee91d089f222169d012ab487a" => :sierra
sha256 "0958d5f3c4de00cc9b391c5ba4225c798056941be69cda4c6df913bf81f57c33" => :el_capitan
sha256 "14b55a75a1328adfe89f9234c72ed769d159f93fc0a7e140c956de2c3c5d54c9" => :yosemite
end
option :universal
option :cxx11
option "without-python", "Build without python 2 support"
if build.cxx11?
depends_on "boost" => "c++11"
else
depends_on "boost"
end
def install
ENV.universal_binary if build.universal?
# "layout" should be synchronized with boost
args = ["--prefix=#{prefix}",
"--libdir=#{lib}",
"-d2",
"-j#{ENV.make_jobs}",
"--layout=tagged",
"--user-config=user-config.jam",
"threading=multi,single",
"link=shared,static"]
args << "address-model=32_64" << "architecture=x86" << "pch=off" if build.universal?
# Build in C++11 mode if boost was built in C++11 mode.
# Trunk starts using "clang++ -x c" to select C compiler which breaks C++11
# handling using ENV.cxx11. Using "cxxflags" and "linkflags" still works.
if build.cxx11?
args << "cxxflags=-std=c++11"
if ENV.compiler == :clang
args << "cxxflags=-stdlib=libc++" << "linkflags=-stdlib=libc++"
end
elsif Tab.for_name("boost").cxx11?
odie "boost was built in C++11 mode so boost-python must be built with --c++11."
end
# disable python detection in bootstrap.sh; it guesses the wrong include directory
# for Python 3 headers, so we configure python manually in user-config.jam below.
inreplace "bootstrap.sh", "using python", "#using python"
Language::Python.each_python(build) do |python, version|
py_prefix = `#{python} -c "from __future__ import print_function; import sys; print(sys.prefix)"`.strip
py_include = `#{python} -c "from __future__ import print_function; import distutils.sysconfig; print(distutils.sysconfig.get_python_inc(True))"`.strip
open("user-config.jam", "w") do |file|
# Force boost to compile with the desired compiler
file.write "using darwin : : #{ENV.cxx} ;\n"
file.write <<-EOS.undent
using python : #{version}
: #{python}
: #{py_include}
: #{py_prefix}/lib ;
EOS
end
system "./bootstrap.sh", "--prefix=#{prefix}", "--libdir=#{lib}", "--with-libraries=python",
"--with-python=#{python}", "--with-python-root=#{py_prefix}"
system "./b2", "--build-dir=build-#{python}", "--stagedir=stage-#{python}",
"python=#{version}", *args
end
lib.install Dir["stage-python3/lib/*py*"] if build.with?("python3")
lib.install Dir["stage-python/lib/*py*"] if build.with?("python")
doc.install Dir["libs/python/doc/*"]
end
test do
(testpath/"hello.cpp").write <<-EOS.undent
#include <boost/python.hpp>
char const* greet() {
return "Hello, world!";
}
BOOST_PYTHON_MODULE(hello)
{
boost::python::def("greet", greet);
}
EOS
Language::Python.each_python(build) do |python, _|
pyflags = (`#{python}-config --includes`.strip + " " +
`#{python}-config --ldflags`.strip).split(" ")
system ENV.cxx, "-shared", "hello.cpp", "-L#{lib}", "-lboost_#{python}", "-o", "hello.so", *pyflags
output = `#{python} -c "from __future__ import print_function; import hello; print(hello.greet())"`
assert_match "Hello, world!", output
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment