Add static build to CI
MODIFY
.github/workflows/opencv-rust.yml
.github/workflows/opencv-rust.yml
@@ -23,6 +23,10 @@ jobs:
- os-image: macos-11
version:
opencv: 3.4.17
+ include:
+ - os-image: ubuntu-20.04
+ version:
+ opencv: 4.6.0-static
runs-on: ${{ matrix.os-image }}
env:
Atlas_ROOT_DIR: /usr/include/ # for cmake to find lapacke.h

MODIFY
README.md
README.md
@@ -101,6 +101,14 @@ requires some additional configuration.
You need to set up the following environment variables to point to the installed files of your OpenCV build:
`OPENCV_LINK_LIBS`, `OPENCV_LINK_PATHS` and `OPENCV_INCLUDE_PATHS` (see below for details).
+### Static build
+
+Static linking to OpenCV is supported and tested at least on Linux. For some hints on building OpenCV statically
+please check this [comment](https://github.com/twistedfall/opencv-rust/issues/364#issuecomment-1308794985). Also,
+some you can get information on how to perform the build in CI scripts:
+[install-focal.sh](https://github.com/twistedfall/opencv-rust/blob/master/ci/install-focal.sh) and
+[script.sh](https://github.com/twistedfall/opencv-rust/blob/master/ci/script.sh), search for `non_static_version` variable.
+
## Troubleshooting
1. One of the common problems is link errors in the end of the build.

MODIFY
ci/install-focal.sh
ci/install-focal.sh
@@ -6,73 +6,13 @@ sudo apt-get update
sudo apt-get install -y clang
# workaround to make clang_sys crate detect installed libclang
-sudo ln -s libclang.so.1 /usr/lib/llvm-10/lib/libclang.so
+sudo ln -fs libclang.so.1 /usr/lib/llvm-10/lib/libclang.so
-# runtime deps
if [[ "$OPENCV_VERSION" == "4.2.0" ]]; then
sudo apt-get -y install "libopencv-dev=${OPENCV_VERSION}*"
-else
- sudo apt-get -y install \
- libatlas3-base \
- libavcodec58 \
- libavformat58 \
- libavresample4 \
- libceres1 \
- libdc1394-22 \
- libfreetype6 \
- libgdal26 \
- libgflags2.2 \
- libgoogle-glog0v5 \
- libgphoto2-6 \
- libgstreamer-plugins-base1.0-0 \
- libharfbuzz0b \
- libhdf5-103 \
- libjpeg8 \
- liblapacke \
- liblept5 \
- libopenexr24 \
- libpng16-16 \
- libswscale5 \
- libtbb2 \
- libtesseract4 \
- libvtk7.1 \
- libwebp6 \
- libqt5core5a \
- libqt5gui5 \
- libqt5opengl5 \
- libqt5test5 \
- libqt5widgets5
- # build deps
- sudo apt-get -y install \
- build-essential \
- cmake \
- libatlas-base-dev \
- libavcodec-dev \
- libavformat-dev \
- libavresample-dev \
- libceres-dev \
- libdc1394-22-dev \
- libeigen3-dev \
- libfreetype6-dev \
- libgdal-dev \
- libgflags-dev \
- libgoogle-glog-dev \
- libgphoto2-dev \
- libgstreamer-plugins-base1.0-dev \
- libharfbuzz-dev \
- libhdf5-dev \
- libjpeg-dev \
- liblapacke-dev \
- libleptonica-dev \
- libopenexr-dev \
- libpng-dev \
- libswscale-dev \
- libtbb-dev \
- libtesseract-dev \
- libtiff-dev \
- libvtk7-dev \
- libwebp-dev \
- qtbase5-dev
+ exit 0
+fi
+
BUILD_FLAGS="
-D BUILD_CUDA_STUBS=OFF
-D BUILD_DOCS=OFF
@@ -83,6 +23,7 @@ else
-D BUILD_JAVA=OFF
-D BUILD_JPEG=OFF
-D BUILD_OPENEXR=OFF
+ -D BUILD_OPENJPEG=OFF
-D BUILD_PERF_TESTS=OFF
-D BUILD_PNG=OFF
-D BUILD_PROTOBUF=ON
@@ -94,22 +35,23 @@ else
-D BUILD_WITH_DEBUG_INFO=OFF
-D BUILD_WITH_DYNAMIC_IPP=OFF
-D BUILD_ZLIB=OFF
- -D BUILD_opencv_apps=ALL
+ -D BUILD_opencv_apps=OFF
-D BUILD_opencv_python2=OFF
-D BUILD_opencv_python3=OFF
-D CMAKE_BUILD_TYPE=Release
-D CMAKE_INSTALL_PREFIX=/usr
-D CV_DISABLE_OPTIMIZATION=OFF
+ -D CV_ENABLE_INTRINSICS=ON
-D ENABLE_CONFIG_VERIFICATION=OFF
-D ENABLE_FAST_MATH=OFF
- -D CV_ENABLE_INTRINSICS=ON
-D ENABLE_LTO=OFF
-D ENABLE_PIC=ON
-D ENABLE_PRECOMPILED_HEADERS=OFF
-D INSTALL_CREATE_DISTRIB=OFF
- -D INSTALL_PYTHON_EXAMPLES=OFF
-D INSTALL_C_EXAMPLES=OFF
+ -D INSTALL_PYTHON_EXAMPLES=OFF
-D INSTALL_TESTS=OFF
+ -D OPENCV_ENABLE_MEMALIGN=OFF
-D OPENCV_ENABLE_NONFREE=ON
-D OPENCV_FORCE_3RDPARTY_BUILD=OFF
-D OPENCV_GENERATE_PKGCONFIG=OFF
@@ -118,7 +60,9 @@ else
-D WITH_ADE=ON
-D WITH_ARAVIS=OFF
-D WITH_CLP=OFF
+ -D WITH_CUBLAS=OFF
-D WITH_CUDA=OFF
+ -D WITH_CUFFT=OFF
-D WITH_EIGEN=ON
-D WITH_FFMPEG=ON
-D WITH_GDAL=ON
@@ -146,11 +90,11 @@ else
-D WITH_OPENCLAMDBLAS=OFF
-D WITH_OPENCLAMDFFT=OFF
-D WITH_OPENCL_SVM=OFF
- -D WITH_OPENEXR=ON
+ -D WITH_OPENEXR=OFF
-D WITH_OPENGL=ON
-D WITH_OPENMP=OFF
- -D WITH_OPENNI=OFF
-D WITH_OPENNI2=OFF
+ -D WITH_OPENNI=OFF
-D WITH_OPENVX=OFF
-D WITH_PNG=ON
-D WITH_PROTOBUF=ON
@@ -168,10 +112,102 @@ else
-D WITH_WEBP=ON
-D WITH_XIMEA=OFF
-D WITH_XINE=OFF
- -D OPENCV_ENABLE_MEMALIGN=OFF
"
+
+sudo apt-get -y install build-essential cmake
+
+non_static_version=${OPENCV_VERSION%-static}
+if [[ "$non_static_version" != "$OPENCV_VERSION" ]]; then # static build
+ OPENCV_VERSION="$non_static_version"
+ BUILD_FLAGS="$BUILD_FLAGS
+ -D BUILD_JPEG=ON
+ -D BUILD_OPENJPEG=ON
+ -D BUILD_PNG=ON
+ -D BUILD_SHARED_LIBS=OFF
+ -D BUILD_TBB=ON
+ -D BUILD_TIFF=ON
+ -D BUILD_WEBP=ON
+ -D BUILD_ZLIB=ON
+ -D BUILD_opencv_freetype=OFF
+ -D OPENCV_FORCE_3RDPARTY_BUILD=ON
+ -D WITH_1394=OFF
+ -D WITH_FFMPEG=OFF
+ -D WITH_FREETYPE=OFF
+ -D WITH_GDAL=OFF
+ -D WITH_GPHOTO2=OFF
+ -D WITH_GSTREAMER=OFF
+ -D WITH_GTK=OFF
+ -D WITH_LAPACK=OFF
+ -D WITH_OPENGL=OFF
+ -D WITH_QT=OFF
+"
+else # dynamic build
+ # runtime deps
+ sudo apt-get -y install \
+ libatlas3-base \
+ libavcodec58 \
+ libavformat58 \
+ libavresample4 \
+ libceres1 \
+ libdc1394-22 \
+ libfreetype6 \
+ libgdal26 \
+ libgflags2.2 \
+ libgoogle-glog0v5 \
+ libgphoto2-6 \
+ libgstreamer-plugins-base1.0-0 \
+ libharfbuzz0b \
+ libhdf5-103 \
+ libjpeg8 \
+ liblapacke \
+ liblept5 \
+ libopenexr24 \
+ libpng16-16 \
+ libswscale5 \
+ libtbb2 \
+ libtesseract4 \
+ libvtk7.1 \
+ libwebp6 \
+ libqt5core5a \
+ libqt5gui5 \
+ libqt5opengl5 \
+ libqt5test5 \
+ libqt5widgets5
+
+ # build deps
+ sudo apt-get -y install \
+ libatlas-base-dev \
+ libavcodec-dev \
+ libavformat-dev \
+ libavresample-dev \
+ libceres-dev \
+ libdc1394-22-dev \
+ libeigen3-dev \
+ libfreetype6-dev \
+ libgdal-dev \
+ libgflags-dev \
+ libgoogle-glog-dev \
+ libgphoto2-dev \
+ libgstreamer-plugins-base1.0-dev \
+ libharfbuzz-dev \
+ libhdf5-dev \
+ libjpeg-dev \
+ liblapacke-dev \
+ libleptonica-dev \
+ libopenexr-dev \
+ libpng-dev \
+ libswscale-dev \
+ libtbb-dev \
+ libtesseract-dev \
+ libtiff-dev \
+ libvtk7-dev \
+ libwebp-dev \
+ qtbase5-dev
+fi
+
base_dir="$HOME/build/opencv/"
mkdir -p "$base_dir"
+
opencv_dist="$base_dir/opencv-$OPENCV_VERSION.tar.gz"
if [ ! -f "$opencv_dist" ]; then
curl -L "https://github.com/opencv/opencv/archive/$OPENCV_VERSION.tar.gz" -o "$opencv_dist"
@@ -183,8 +219,10 @@ else
curl -L "https://github.com/opencv/opencv_contrib/archive/$OPENCV_VERSION.tar.gz" -o "$opencv_contrib_dist"
fi
tar -xzf "$opencv_contrib_dist" -C "$base_dir"
+
build_dir="$base_dir/opencv-$OPENCV_VERSION-build/"
mkdir -p "$build_dir"
+
pushd "$build_dir" > /dev/null
cmake $BUILD_FLAGS \
-D CMAKE_INSTALL_PREFIX=/usr \
@@ -193,4 +231,3 @@ else
make -j"$(nproc)"
sudo make -j"$(nproc)" install
popd > /dev/null
-fi

MODIFY
ci/script.sh
ci/script.sh
@@ -38,6 +38,11 @@ elif [[ "$OS_FAMILY" == "Linux" ]]; then
export VCPKG_ROOT="$HOME/build/vcpkg"
echo "=== Installed vcpkg packages:"
"$VCPKG_ROOT/vcpkg" list
+ else
+ non_static_version=${OPENCV_VERSION%-static}
+ if [[ "$non_static_version" != "$OPENCV_VERSION" ]]; then # static build
+ export OPENCV_LINK_LIBS=opencv_highgui,opencv_objdetect,opencv_dnn,opencv_calib3d,opencv_videostab,opencv_features2d,opencv_stitching,opencv_flann,opencv_videoio,opencv_rgbd,opencv_aruco,opencv_video,opencv_ml,opencv_imgcodecs,opencv_imgproc,opencv_core,ittnotify,tbb,liblibwebp,liblibtiff,liblibjpeg-turbo,liblibpng,liblibopenjp2,ippiw,ippicv,liblibprotobuf,quirc,zlib
+ fi
fi
fi

MODIFY
examples/README.md
examples/README.md
Some limited examples of how to use this crate are provided here.
-For more examples, refer to the hand-written tests at [/tests](/tests)
+For more examples, refer to the handwritten tests at [/tests](/tests)

MODIFY
tests/marshalling.rs
tests/marshalling.rs
@@ -54,12 +54,18 @@ fn callback() -> Result<()> {
#![cfg(ocvrs_has_module_highgui)]
use std::sync::{Arc, Mutex};
- use opencv::highgui;
+ use opencv::{core, highgui, Error};
// only run under X11 on linux
if cfg!(target_os = "linux") && option_env!("DISPLAY").is_some() {
{
- highgui::named_window("test_1", 0)?;
+ if let Err(Error {
+ code: core::StsError, ..
+ }) = highgui::named_window("test_1", 0)
+ {
+ // means that OpenCV is not built with GUI support, just skip the test
+ return Ok(());
+ }
let mut value = 50;
let cb_value = Arc::new(Mutex::new(0));
highgui::create_trackbar(

Add static build to CI