Skip to content

Instantly share code, notes, and snippets.

@bodokaiser
Created April 23, 2015 17:42
Show Gist options
  • Save bodokaiser/9568fb6c67d9377a41f5 to your computer and use it in GitHub Desktop.
Save bodokaiser/9568fb6c67d9377a41f5 to your computer and use it in GitHub Desktop.
Homebrew opencv file with python3 support.
class Opencv < Formula
homepage "http://opencv.org/"
head "https://github.com/Itseez/opencv.git"
stable do
url "https://github.com/Itseez/opencv/archive/2.4.11.tar.gz"
sha256 "b5331ea85a709b0fe871b1ce92e631afcd5ae822423863da6b559dd2cb7845bc"
# Avoid explicit links to a Python framework
# https://github.com/Itseez/opencv/pull/3865
patch do
url "https://gist.githubusercontent.com/tdsmith/484553cd2d0c19a4baa7/raw/b766154fa6c7ac1be3491b0c6b58b3d66c07f818/opencv_python.diff"
sha256 "cfe31c32d5a4ef0e89df684e210360602fb2d295b19f9ca4791731a9e274d776"
end
end
bottle do
root_url "https://homebrew.bintray.com/bottles-science"
sha256 "e158921e8fbaa19fece4c04ba86935179ec136a94d6d86f2f0ef1ab026f41e57" => :yosemite
sha256 "b1f00877317eda123a7bde6c1541738f70b808e84b398658464d068503875f37" => :mavericks
sha256 "c7dcc02ba840aeaabb629aa36b0ce41840fd8da0d77b4f637fadadeb05259e1d" => :mountain_lion
end
devel do
url "https://github.com/Itseez/opencv/archive/3.0.0-beta.tar.gz"
sha1 "560895197d1a61ed88fab9ec791328c4c57c0179"
version "3.0.0-beta"
# Avoid explicit links to a Python framework
# https://github.com/Itseez/opencv/pull/3866
patch do
url "https://gist.githubusercontent.com/tdsmith/f67fd567702f2b51dacd/raw/b4c5428b87a5551ad75f951d45c38aacec4f2441/opencv3_python.diff"
sha256 "be689ac2ce44edfb1f8f9bdaf11e3cd5933fe26e9195ff57e391e6c49b3a1607"
end
end
option "32-bit"
option "with-java", "Build with Java support"
option "with-qt", "Build the Qt4 backend to HighGUI"
option "with-tbb", "Enable parallel code in OpenCV using Intel TBB"
option "without-tests", "Build without accuracy & performance tests"
option "without-opencl", "Disable GPU code in OpenCV using OpenCL"
option "with-cuda", "Build with CUDA support"
option "with-quicktime", "Use QuickTime for Video I/O insted of QTKit"
option "with-opengl", "Build with OpenGL support"
option "without-numpy", "Use a numpy you've installed yourself instead of a Homebrew-packaged numpy"
option "without-python", "Build without Python support"
deprecated_option "without-brewed-numpy" => "without-numpy"
option :cxx11
depends_on :ant if build.with? "java"
depends_on "cmake" => :build
depends_on "eigen" => :recommended
depends_on "gstreamer" => :optional
depends_on "gst-plugins-good" if build.with? "gstreamer"
depends_on "jasper" => :optional
depends_on :java => :optional
depends_on "jpeg"
depends_on "libpng"
depends_on "libtiff"
depends_on "libdc1394" => :optional
depends_on "openexr" => :recommended
depends_on "openni" => :optional
depends_on "pkg-config" => :build
depends_on "qt" => :optional
depends_on "tbb" => :optional
depends_on :python => :recommended unless OS.mac? && MacOS.version > :snow_leopard
depends_on "homebrew/python/numpy" => :recommended if build.with? "python"
# Can also depend on ffmpeg, but this pulls in a lot of extra stuff that
# you don't need unless you're doing video analysis, and some of it isn't
# in Homebrew anyway. Will depend on openexr if it's installed.
depends_on "ffmpeg" => :optional
def arg_switch(opt)
(build.with? opt) ? "ON" : "OFF"
end
def install
ENV.cxx11 if build.cxx11?
jpeg = Formula["jpeg"]
dylib = OS.mac? ? "dylib" : "so"
py_ver = build.stable? ? "" : "2"
args = std_cmake_args + %W[
-DCMAKE_OSX_DEPLOYMENT_TARGET=
-DBUILD_ZLIB=OFF
-DBUILD_TIFF=OFF
-DBUILD_PNG=OFF
-DBUILD_OPENEXR=OFF
-DBUILD_JASPER=OFF
-DBUILD_JPEG=OFF
-DJPEG_INCLUDE_DIR=#{jpeg.opt_include}
-DJPEG_LIBRARY=#{jpeg.opt_lib}/libjpeg.#{dylib}
]
args << "-DBUILD_TESTS=OFF" << "-DBUILD_PERF_TESTS=OFF" if build.without? "tests"
args << "-DBUILD_opencv_python#{py_ver}=" + arg_switch("python")
args << "-DBUILD_opencv_java=" + arg_switch("java")
args << "-DWITH_OPENEXR=" + arg_switch("openexr")
args << "-DWITH_EIGEN=" + arg_switch("eigen")
args << "-DWITH_TBB=" + arg_switch("tbb")
args << "-DWITH_FFMPEG=" + arg_switch("ffmpeg")
args << "-DWITH_QUICKTIME=" + arg_switch("quicktime")
args << "-DWITH_1394=" + arg_switch("libdc1394")
args << "-DWITH_OPENGL=" + arg_switch("opengl")
args << "-DWITH_JASPER=" + arg_switch("jasper")
args << "-DWITH_QT=" + arg_switch("qt")
args << "-DWITH_GSTREAMER=" + arg_switch("gstreamer")
if build.with? "python"
pyprefix = "/usr/local/Cellar/python3/3.4.3/Frameworks/Python.framework/Versions/3.4"
args << "-DPYTHON3_LIBRARY=#{pyprefix}/lib/libpython3.4m.dylib"
args << "-DPYTHON3_EXECUTABLE=#{pyprefix}/bin/python3"
args << "-DPYTHON3_INCLUDE_DIR=#{pyprefix}/include/python3.4m"
args << "-DPYTHON3_NUMPY_INCLUDE_DIRS=/usr/local/lib/python3.4/site-packages/numpy/core/include"
args << "-DPYTHON3_PACKAGES_PATH=/usr/local/lib/python3.4/site-packages/"
#py_prefix = `python-config --prefix`.chomp
#py_lib = OS.linux? ? `python-config --configdir`.chomp : "#{py_prefix}/lib"
#args << "-DPYTHON#{py_ver}_LIBRARY=#{py_lib}/libpython2.7.#{dylib}"
#args << "-DPYTHON#{py_ver}_INCLUDE_DIR=#{py_prefix}/include/python2.7"
end
if build.with? "cuda"
ENV["CUDA_NVCC_FLAGS"] = "-Xcompiler -stdlib=libstdc++; -Xlinker -stdlib=libstdc++"
args << "-DWITH_CUDA=ON"
args << "-DCMAKE_CXX_FLAGS=-stdlib=libstdc++"
args << "-DCUDA_GENERATION=Kepler"
else
args << "-DWITH_CUDA=OFF"
end
# OpenCL 1.1 is required, but Snow Leopard and older come with 1.0
args << "-DWITH_OPENCL=OFF" if build.without?("opencl") || MacOS.version < :lion
if build.with? "openni"
args << "-DWITH_OPENNI=ON"
# Set proper path for Homebrew's openni
inreplace "cmake/OpenCVFindOpenNI.cmake" do |s|
s.gsub! "/usr/include/ni", "#{Formula["openni"].opt_include}/ni"
s.gsub! "/usr/lib", "#{Formula["openni"].opt_lib}"
end
end
if build.include? "32-bit"
args << "-DCMAKE_OSX_ARCHITECTURES=i386"
args << "-DOPENCV_EXTRA_C_FLAGS='-arch i386 -m32'"
args << "-DOPENCV_EXTRA_CXX_FLAGS='-arch i386 -m32'"
end
if ENV.compiler == :clang && !build.bottle?
args << "-DENABLE_SSSE3=ON" if Hardware::CPU.ssse3?
args << "-DENABLE_SSE41=ON" if Hardware::CPU.sse4?
args << "-DENABLE_SSE42=ON" if Hardware::CPU.sse4_2?
args << "-DENABLE_AVX=ON" if Hardware::CPU.avx?
end
mkdir "macbuild" do
system "cmake", "..", *args
system "make"
system "make", "install"
end
end
test do
(testpath/"test.cpp").write <<-EOS.undent
#include <opencv/cv.h>
#include <iostream>
int main()
{
std::cout << CV_VERSION << std::endl;
return 0;
}
EOS
system ENV.cxx, "test.cpp", "-I#{include}", "-L#{lib}", "-o", "test"
assert_equal `./test`.strip, version.to_s
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment