Skip to content

Commit

Permalink
opencv-maa: a minimal OpenCV build for MAA
Browse files Browse the repository at this point in the history
  • Loading branch information
wangl-cc committed May 26, 2024
1 parent e7810a3 commit 9afc3be
Show file tree
Hide file tree
Showing 4 changed files with 114 additions and 203 deletions.
65 changes: 4 additions & 61 deletions Formula/fastdeploy_ppocr.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,10 @@ class FastdeployPpocr < Formula

# opencv is a very large dependency, and we only need a small part of it
# so we build our own opencv if user does not want to install opencv by homebrew
depends_on "opencv" => :optional

unless build.with? "opencv"
depends_on "jpeg-turbo"
depends_on "libpng"
depends_on "libtiff"

resource "opencv" do
url "https://github.com/opencv/opencv/archive/refs/tags/4.9.0.tar.gz"
sha256 "ddf76f9dffd322c7c3cb1f721d0887f62d747b82059342213138dc190f28bc6c"
end
if build.with? "opencv"
depends_on "opencv"
else
depends_on "opencv-maa"
end

def install
Expand All @@ -45,56 +38,6 @@ def install
-DCMAKE_POSITION_INDEPENDENT_CODE=ON
]

unless build.with? "opencv"
resource("opencv").stage "opencv"

# Remove bundled libraries to make sure formula dependencies are used
libdirs = %w[ffmpeg libjasper libjpeg libjpeg-turbo libpng libtiff libwebp openexr openjpeg zlib]
libdirs.each { |l| (buildpath/"opencv/3rdparty"/l).rmtree }

# basic cmake args for opencv
opencv_cmake_args = %W[
-DCMAKE_CXX_STANDARD=17

-DBUILD_SHARED_LIBS=OFF

-DBUILD_ZLIB=OFF

-DWITH_PNG=ON
-DBUILD_PNG=OFF
-DWITH_JPEG=ON
-DBUILD_JPEG=OFF
-DWITH_TIFF=ON
-DBUILD_TIFF=OFF

-DWITH_WEBP=OFF
-DBUILD_WEBP=OFF
-DWITH_OPENJPEG=OFF
-DBUILD_OPENJPEG=OFF
-DWITH_JASPER=OFF
-DBUILD_JASPER=OFF
-DWITH_OPENEXR=OFF
-DBUILD_OPENEXR=OFF

-DWITH_FFMPEG=#{OS.linux? ? "ON" : "OFF"}
-DWITH_V4L=OFF
-DWITH_GSTREAMER=OFF
-DWITH_DSHOW=OFF
-DWITH_1394=OFF
-DWITH_CUDA=OFF
]

opencv_buildpath = buildpath/"opencv/build-fastdeploy"
system "cmake", "-S", "opencv", "-B", opencv_buildpath,
"-DBUILD_LIST=core,imgproc", "-DWITH_EIGEN=ON",
*opencv_cmake_args, *std_cmake_args

# Remove reference to shims directory
inreplace opencv_buildpath/"modules/core/version_string.inc", "#{Superenv.shims_path}/", ""
system "cmake", "--build", opencv_buildpath
cmake_args << "-DOpenCV_DIR=#{opencv_buildpath}"
end

system "cmake", "-S", ".", "-B", "build", *cmake_args, *std_cmake_args
system "cmake", "--build", "build"
system "cmake", "--install", "build"
Expand Down
80 changes: 9 additions & 71 deletions Formula/maa-core-beta.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,11 @@ class MaaCoreBeta < Formula

# opencv is a very large dependency, and we only need a small part of it
# so we build our own opencv if user does not want to install opencv by homebrew
depends_on "opencv" => :optional
if build.with? "opencv"
depends_on "opencv"
else
depends_on "opencv-maa"
end

uses_from_macos "curl"

Expand All @@ -43,27 +47,11 @@ class MaaCoreBeta < Formula

fails_with gcc: "11"

resource "fastdeploy_ppocr" do
url "https://github.com/MaaAssistantArknights/FastDeploy/archive/0db6000aaac250824266ac37451f43ce272d80a3.tar.gz"
sha256 "ac0bf5059f0339003e3e6e50c87e9455be508761e101e8898135f67b8a7c8115"
end

unless build.with? "opencv"
depends_on "jpeg-turbo"
depends_on "libpng"
depends_on "libtiff"

resource "opencv" do
url "https://github.com/opencv/opencv/archive/refs/tags/4.9.0.tar.gz"
sha256 "ddf76f9dffd322c7c3cb1f721d0887f62d747b82059342213138dc190f28bc6c"
end
end

# Force find onnxruntime in CONFIG mode
patch :DATA

def install
maacore_cmake_args = %W[
cmake_args = %W[
-DBUILD_SHARED_LIBS=ON
-DCMAKE_POSITION_INDEPENDENT_CODE=ON
-DUSE_MAADEPS=OFF
Expand All @@ -74,59 +62,9 @@ def install
-DMAA_VERSION=v#{version}
]

maacore_cmake_args << "-DUSE_RANGE_V3=ON" if OS.mac? && MacOS.version <= :ventura

unless build.with? "opencv"
resource("opencv").stage "opencv"

# Remove bundled libraries to make sure formula dependencies are used
libdirs = %w[ffmpeg libjasper libjpeg libjpeg-turbo libpng libtiff libwebp openexr openjpeg zlib]
libdirs.each { |l| (buildpath/"opencv/3rdparty"/l).rmtree }

# basic cmake args for opencv
opencv_cmake_args = %W[
-DCMAKE_CXX_STANDARD=17

-DBUILD_SHARED_LIBS=OFF

-DBUILD_LIST=core,imgproc,imgcodecs,videoio

-DBUILD_ZLIB=OFF

-DWITH_PNG=ON
-DBUILD_PNG=OFF
-DWITH_JPEG=ON
-DBUILD_JPEG=OFF
-DWITH_TIFF=ON
-DBUILD_TIFF=OFF

-DWITH_WEBP=OFF
-DBUILD_WEBP=OFF
-DWITH_OPENJPEG=OFF
-DBUILD_OPENJPEG=OFF
-DWITH_JASPER=OFF
-DBUILD_JASPER=OFF
-DWITH_OPENEXR=OFF
-DBUILD_OPENEXR=OFF

-DWITH_FFMPEG=#{OS.linux? ? "ON" : "OFF"}
-DWITH_V4L=OFF
-DWITH_GSTREAMER=OFF
-DWITH_DSHOW=OFF
-DWITH_1394=OFF
-DWITH_CUDA=OFF
]

opencv_buildpath = buildpath/"build/opencv"
system "cmake", "-S", "opencv", "-B", opencv_buildpath,
*opencv_cmake_args, *std_cmake_args
# Remove reference to shims directory
inreplace opencv_buildpath/"modules/core/version_string.inc", "#{Superenv.shims_path}/", ""
system "cmake", "--build", opencv_buildpath
maacore_cmake_args << "-DOpenCV_DIR=#{opencv_buildpath}"
end

system "cmake", "-S", ".", "-B", "build", *maacore_cmake_args, *std_cmake_args
cmake_args << "-DUSE_RANGE_V3=ON" if OS.mac? && MacOS.version <= :ventura

system "cmake", "-S", ".", "-B", "build", *cmake_args, *std_cmake_args
system "cmake", "--build", "build"
system "cmake", "--install", "build"

Expand Down
80 changes: 9 additions & 71 deletions Formula/maa-core.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,11 @@ class MaaCore < Formula

# opencv is a very large dependency, and we only need a small part of it
# so we build our own opencv if user does not want to install opencv by homebrew
depends_on "opencv" => :optional
if build.with? "opencv"
depends_on "opencv"
else
depends_on "opencv-maa"
end

uses_from_macos "curl"

Expand All @@ -43,27 +47,11 @@ class MaaCore < Formula

fails_with gcc: "11"

resource "fastdeploy_ppocr" do
url "https://github.com/MaaAssistantArknights/FastDeploy/archive/0db6000aaac250824266ac37451f43ce272d80a3.tar.gz"
sha256 "ac0bf5059f0339003e3e6e50c87e9455be508761e101e8898135f67b8a7c8115"
end

unless build.with? "opencv"
depends_on "jpeg-turbo"
depends_on "libpng"
depends_on "libtiff"

resource "opencv" do
url "https://github.com/opencv/opencv/archive/refs/tags/4.9.0.tar.gz"
sha256 "ddf76f9dffd322c7c3cb1f721d0887f62d747b82059342213138dc190f28bc6c"
end
end

# Force find onnxruntime in CONFIG mode
patch :DATA

def install
maacore_cmake_args = %W[
cmake_args = %W[
-DBUILD_SHARED_LIBS=ON
-DCMAKE_POSITION_INDEPENDENT_CODE=ON
-DUSE_MAADEPS=OFF
Expand All @@ -74,59 +62,9 @@ def install
-DMAA_VERSION=v#{version}
]

maacore_cmake_args << "-DUSE_RANGE_V3=ON" if OS.mac? && MacOS.version <= :ventura

unless build.with? "opencv"
resource("opencv").stage "opencv"

# Remove bundled libraries to make sure formula dependencies are used
libdirs = %w[ffmpeg libjasper libjpeg libjpeg-turbo libpng libtiff libwebp openexr openjpeg zlib]
libdirs.each { |l| (buildpath/"opencv/3rdparty"/l).rmtree }

# basic cmake args for opencv
opencv_cmake_args = %W[
-DCMAKE_CXX_STANDARD=17

-DBUILD_SHARED_LIBS=OFF

-DBUILD_LIST=core,imgproc,imgcodecs,videoio

-DBUILD_ZLIB=OFF

-DWITH_PNG=ON
-DBUILD_PNG=OFF
-DWITH_JPEG=ON
-DBUILD_JPEG=OFF
-DWITH_TIFF=ON
-DBUILD_TIFF=OFF

-DWITH_WEBP=OFF
-DBUILD_WEBP=OFF
-DWITH_OPENJPEG=OFF
-DBUILD_OPENJPEG=OFF
-DWITH_JASPER=OFF
-DBUILD_JASPER=OFF
-DWITH_OPENEXR=OFF
-DBUILD_OPENEXR=OFF

-DWITH_FFMPEG=#{OS.linux? ? "ON" : "OFF"}
-DWITH_V4L=OFF
-DWITH_GSTREAMER=OFF
-DWITH_DSHOW=OFF
-DWITH_1394=OFF
-DWITH_CUDA=OFF
]

opencv_buildpath = buildpath/"build/opencv"
system "cmake", "-S", "opencv", "-B", opencv_buildpath,
*opencv_cmake_args, *std_cmake_args
# Remove reference to shims directory
inreplace opencv_buildpath/"modules/core/version_string.inc", "#{Superenv.shims_path}/", ""
system "cmake", "--build", opencv_buildpath
maacore_cmake_args << "-DOpenCV_DIR=#{opencv_buildpath}"
end

system "cmake", "-S", ".", "-B", "build", *maacore_cmake_args, *std_cmake_args
cmake_args << "-DUSE_RANGE_V3=ON" if OS.mac? && MacOS.version <= :ventura

system "cmake", "-S", ".", "-B", "build", *cmake_args, *std_cmake_args
system "cmake", "--build", "build"
system "cmake", "--install", "build"

Expand Down
92 changes: 92 additions & 0 deletions Formula/opencv-maa.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
class OpencvMaa < Formula

Check warning on line 1 in Formula/opencv-maa.rb

View workflow job for this annotation

GitHub Actions / Build (ubuntu-22.04)

`brew linkage --cached --test --strict maaassistantarknights/tap/opencv-maa` failed on Linux!

Indirect dependencies with linkage: openblas
desc "Minimal OpenCV build use by MAA"
homepage "https://opencv.org/"
url "https://github.com/opencv/opencv/archive/refs/tags/4.9.0.tar.gz"
sha256 "ddf76f9dffd322c7c3cb1f721d0887f62d747b82059342213138dc190f28bc6c"
license "Apache-2.0"

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

depends_on "cmake" => :build
depends_on "eigen" => :build

depends_on "jpeg-turbo"
depends_on "libpng"

uses_from_macos "zlib"

on_linux do
depends_on "ffmpeg@6"
end

conflicts_with "opencv", { because: "this is a minimal build of OpenCV" }

def install
# 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 }

cmake_args = %W[
-DCMAKE_CXX_STANDARD=17

-DBUILD_LIST=core,imgproc,imgcodecs,videoio

-DBUILD_ZLIB=OFF

-DWITH_EIGEN=ON

-DWITH_PNG=ON
-DBUILD_PNG=OFF
-DWITH_JPEG=ON
-DBUILD_JPEG=OFF
-DWITH_TIFF=OFF
-DWITH_WEBP=OFF
-DWITH_OPENJPEG=OFF
-DWITH_JASPER=OFF
-DWITH_OPENEXR=OFF

-DWITH_FFMPEG=#{OS.linux? ? "ON" : "OFF"}
-DWITH_V4L=OFF
-DWITH_GSTREAMER=OFF
-DWITH_DSHOW=OFF
-DWITH_1394=OFF

-DWITH_CUDA=OFF

-DWITH_PROTOBUF=OFF
-DBUILD_opencv_python3=OFF
]

# HACK: avoid opencv install data
cmake_args << "-DOPENCV_OTHER_INSTALL_PATH=#{buildpath}/tmp"

if OS.linux?
cmake_args += %W[
-DPNG_LIBRARY=#{Formula["libpng"].opt_lib}/libpng.so
-DJPEG_LIBRARY=#{Formula["jpeg-turbo"].opt_lib}/libjpeg.so
-DZLIB_LIBRARY=#{Formula["zlib"].opt_lib}/libz.so
]
end

system "cmake", "-S", ".", "-B", "build", *cmake_args, *std_cmake_args
inreplace "build/modules/core/version_string.inc", "#{Superenv.shims_path}/", ""
system "cmake", "--build", "build"
system "cmake", "--install", "build"
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++17", "test.cpp", "-I#{include}/opencv4", "-o", "test"
assert_equal shell_output("./test").strip, version.to_s
end
end

0 comments on commit 9afc3be

Please sign in to comment.