homebrew-core/Formula/opencv.rb

161 lines
5.5 KiB
Ruby

class Opencv < Formula
desc "Open source computer vision library"
homepage "https://opencv.org/"
url "https://github.com/opencv/opencv/archive/refs/tags/4.7.0.tar.gz"
sha256 "8df0079cdbe179748a18d44731af62a245a45ebf5085223dc03133954c662973"
license "Apache-2.0"
livecheck do
url :stable
regex(/^v?(\d+(?:\.\d+)+)$/i)
end
bottle do
sha256 arm64_ventura: "c5c3a06d72de799d97ce739b5a631037b17378fe0600401afc3794bdc8091dcb"
sha256 arm64_monterey: "f315062c56bfd02d6242fa6616efdb15a29cf8bccb4d2c4b49f9d4548a89e720"
sha256 arm64_big_sur: "13e56f75b31e8a2f8ab34d6849c2ff140e884e982fa5d7146e575b9e7ece5eac"
sha256 ventura: "58863f63932395e12b6b1415e2a7213ceb9c2ff898ce758766490946c4161b86"
sha256 monterey: "aefd1832552069ee04b702ba7ab3352e59d2ac405a5e3239ec4ef971dc5ec0b3"
sha256 big_sur: "63f54c956d02c2132ae1899d49ad9efe554c0acc6f232df62df008628c582945"
sha256 x86_64_linux: "8f8354d696a4f85fd056cc3e353420993adfccd798391a93c76aaaefc0b99436"
end
depends_on "cmake" => :build
depends_on "pkg-config" => :build
depends_on "ceres-solver"
depends_on "eigen"
depends_on "ffmpeg"
depends_on "glog"
depends_on "harfbuzz"
depends_on "jpeg-turbo"
depends_on "libpng"
depends_on "libtiff"
depends_on "numpy"
depends_on "openblas"
depends_on "openexr"
depends_on "openjpeg"
depends_on "protobuf"
depends_on "python@3.11"
depends_on "tbb"
depends_on "vtk"
depends_on "webp"
uses_from_macos "zlib"
fails_with gcc: "5" # ffmpeg is compiled with GCC
resource "contrib" do
url "https://github.com/opencv/opencv_contrib/archive/refs/tags/4.7.0.tar.gz"
sha256 "42df840cf9055e59d0e22c249cfb19f04743e1bdad113d31b1573d3934d62584"
end
def python3
"python3.11"
end
def install
resource("contrib").stage buildpath/"opencv_contrib"
# Avoid Accelerate.framework
ENV["OpenBLAS_HOME"] = Formula["openblas"].opt_prefix
# Reset PYTHONPATH, workaround for https://github.com/Homebrew/homebrew-science/pull/4885
ENV.delete("PYTHONPATH")
# Remove bundled libraries to make sure formula dependencies are used
libdirs = %w[ffmpeg libjasper libjpeg libjpeg-turbo libpng libtiff libwebp openexr openjpeg protobuf tbb zlib]
libdirs.each { |l| (buildpath/"3rdparty"/l).rmtree }
args = std_cmake_args + %W[
-DCMAKE_CXX_STANDARD=11
-DCMAKE_OSX_DEPLOYMENT_TARGET=
-DBUILD_JASPER=OFF
-DBUILD_JPEG=OFF
-DBUILD_OPENEXR=OFF
-DBUILD_OPENJPEG=OFF
-DBUILD_PERF_TESTS=OFF
-DBUILD_PNG=OFF
-DBUILD_PROTOBUF=OFF
-DBUILD_TBB=OFF
-DBUILD_TESTS=OFF
-DBUILD_TIFF=OFF
-DBUILD_WEBP=OFF
-DBUILD_ZLIB=OFF
-DBUILD_opencv_hdf=OFF
-DBUILD_opencv_java=OFF
-DBUILD_opencv_text=ON
-DOPENCV_ENABLE_NONFREE=ON
-DOPENCV_EXTRA_MODULES_PATH=#{buildpath}/opencv_contrib/modules
-DOPENCV_GENERATE_PKGCONFIG=ON
-DPROTOBUF_UPDATE_FILES=ON
-DWITH_1394=OFF
-DWITH_CUDA=OFF
-DWITH_EIGEN=ON
-DWITH_FFMPEG=ON
-DWITH_GPHOTO2=OFF
-DWITH_GSTREAMER=OFF
-DWITH_JASPER=OFF
-DWITH_OPENEXR=ON
-DWITH_OPENGL=OFF
-DWITH_QT=OFF
-DWITH_TBB=ON
-DWITH_VTK=ON
-DBUILD_opencv_python2=OFF
-DBUILD_opencv_python3=ON
-DPYTHON3_EXECUTABLE=#{which(python3)}
]
# Disable precompiled headers and force opencv to use brewed libraries on Linux
if OS.linux?
args += %W[
-DENABLE_PRECOMPILED_HEADERS=OFF
-DJPEG_LIBRARY=#{Formula["jpeg-turbo"].opt_lib}/libjpeg.so
-DOpenBLAS_LIB=#{Formula["openblas"].opt_lib}/libopenblas.so
-DOPENEXR_ILMIMF_LIBRARY=#{Formula["openexr"].opt_lib}/libIlmImf.so
-DOPENEXR_ILMTHREAD_LIBRARY=#{Formula["openexr"].opt_lib}/libIlmThread.so
-DPNG_LIBRARY=#{Formula["libpng"].opt_lib}/libpng.so
-DPROTOBUF_LIBRARY=#{Formula["protobuf"].opt_lib}/libprotobuf.so
-DTIFF_LIBRARY=#{Formula["libtiff"].opt_lib}/libtiff.so
-DWITH_V4L=OFF
-DZLIB_LIBRARY=#{Formula["zlib"].opt_lib}/libz.so
]
end
# Ref: https://github.com/opencv/opencv/wiki/CPU-optimizations-build-options
ENV.runtime_cpu_detection
if Hardware::CPU.intel? && build.bottle?
cpu_baseline = MacOS.version.requires_sse42? ? "SSE4_2" : "SSSE3"
args += %W[-DCPU_BASELINE=#{cpu_baseline} -DCPU_BASELINE_REQUIRE=#{cpu_baseline}]
end
system "cmake", "-S", ".", "-B", "build_shared", *args
inreplace "build_shared/modules/core/version_string.inc", "#{Superenv.shims_path}/", ""
system "cmake", "--build", "build_shared"
system "cmake", "--install", "build_shared"
system "cmake", "-S", ".", "-B", "build_static", *args, "-DBUILD_SHARED_LIBS=OFF"
inreplace "build_static/modules/core/version_string.inc", "#{Superenv.shims_path}/", ""
system "cmake", "--build", "build_static"
lib.install buildpath.glob("build_static/{lib,3rdparty/**}/*.a")
# Prevent dependents from using fragile Cellar paths
inreplace lib/"pkgconfig/opencv#{version.major}.pc", prefix, opt_prefix
end
test do
(testpath/"test.cpp").write <<~EOS
#include <opencv2/opencv.hpp>
#include <iostream>
int main() {
std::cout << CV_VERSION << std::endl;
return 0;
}
EOS
system ENV.cxx, "-std=c++11", "test.cpp", "-I#{include}/opencv4", "-o", "test"
assert_equal shell_output("./test").strip, version.to_s
output = shell_output("#{python3} -c 'import cv2; print(cv2.__version__)'")
assert_equal version.to_s, output.chomp
end
end