Skip to content

chore: refactor linux build process #42

chore: refactor linux build process

chore: refactor linux build process #42

Workflow file for this run

# This is a basic workflow to help you get started with Actions
name: build
on:
push:
branches: [ "**" ]
workflow_dispatch:
inputs:
debug_enabled:
type: boolean
description: 'Run the build with tmate debugging enabled (https://github.com/marketplace/actions/debugging-with-tmate)'
required: false
default: false
jobs:
build:
strategy:
fail-fast: false
matrix:
os-image:
- ubuntu-22.04
opencv-version:
- 4.10.0
linkage:
- static
include:
- os-image: ubuntu-22.04
opencv-version: 4.10.0
linkage: static
runs-on: ${{ matrix.os-image }}
env:
Atlas_ROOT_DIR: /usr/include/ # for cmake to find lapacke.h
OPENCV_VERSION: ${{ matrix.opencv-version }}
OPENCV_LINKAGE: ${{ matrix.linkage }}
SCCACHE_GHA_ENABLED: "true"
RUSTC_WRAPPER: "sccache"
CMAKE_C_COMPILER_LAUNCHER: "sccache"
CMAKE_CXX_COMPILER_LAUNCHER: "sccache"
steps:
- uses: actions/checkout@v4
- uses: dtolnay/rust-toolchain@stable
- uses: mozilla-actions/[email protected]
- uses: actions/cache@v4
with:
path: ~/dist
key: dist-${{ matrix.opencv-version }}
- uses: actions/cache@v4
with:
path: ~/build
key: build-${{ matrix.opencv-version }}-${{ matrix.linkage }}-${{ matrix.os-image }}
- name: Install dependencies
run: scripts/install.sh
shell: bash
- name: Build project
run: scripts/build.sh
shell: bash
windows-build:
# The type of runner that the job will run on
runs-on: windows-latest
# Steps represent a sequence of tasks that will be executed as part of the job
steps:
# Checks-out your repository under $GITHUB_WORKSPACE, so your job can access it
- uses: actions/checkout@v4
# Install rust
- name: Install rust
run: curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh -s -- -y
- name: Chocolatey Action
# You may pin to the exact commit or the version.
# uses: crazy-max/ghaction-chocolatey@0e015857dd851f84fcb7fb53380eb5c4c8202333
uses: crazy-max/[email protected]
with:
# Arguments to pass to Chocolatey
# don't install opencv here as we build the staticlib below
args: install llvm wget strawberryperl
# Docker image to use
#image: # optional, default is ghcr.io/crazy-max/ghaction-chocolatey
- name: Cache OpenCV build
uses: actions/cache@v4
with:
path: |
D:/opt/opencv
key: ${{ runner.os }}-opencv-windows-${{ hashFiles('**/CMakeLists.txt') }} # 使用 CMakeLists.txt 的哈希作为缓存键
restore-keys: |
${{ runner.os }}-opencv-windows
# Note: in order to set config profile to Release and use a prefix path, we have to separate cmake steps for building/installing. CMake nuances...
# See https://stackoverflow.com/questions/19024259/how-to-change-the-build-type-to-release-mode-in-cmake, https://stackoverflow.com/questions/50028570/is-it-possible-to-build-cmake-projects-directly-using-msbuildtools, https://cmake.org/cmake/help/latest/variable/CMAKE_INSTALL_PREFIX.html
# Pull opencv source, unzip, configure cmake, build, and install
# Disable image formats like jpeg, png, tiff, as we use rust image crate instead. See https://docs.opencv.org/4.x/db/d05/tutorial_config_reference.html
# Inspired from https://github.com/twistedfall/opencv-rust/issues/364
- name: Install OpenCV static lib
run: |
if (-Not (Test-Path "D:/opt/opencv")) {
wget -O opencv.zip https://github.com/opencv/opencv/archive/refs/tags/4.8.1.zip
wget -O opencv_contrib.zip https://github.com/opencv/opencv_contrib/archive/refs/tags/4.8.1.zip
unzip opencv.zip
rm opencv.zip
unzip opencv_contrib.zip
rm opencv_contrib.zip
mkdir -p build
cd build
cmake -DCMAKE_BUILD_TYPE=Release -DBUILD_SHARED_LIBS=NO -DCMAKE_INSTALL_PREFIX="D:/opt/opencv" -DOPENCV_BUILD_3RDPARTY_LIBS=ON -DBUILD_DOCS=OFF -DBUILD_EXAMPLES=OFF -DBUILD_TESTS=OFF -DBUILD_PERF_TESTS=OFF -DBUILD_opencv_java=OFF -DBUILD_opencv_python=OFF -DOPENCV_EXTRA_MODULES_PATH="../opencv_contrib-4.8.1/modules" ../opencv-4.8.1
cmake --build . --target install --config Release --parallel 8
cmake --install . --prefix D:/opt/opencv
cd ..
} else {
Write-Host "Using cached OpenCV build"
}
- name: Setup tmate session
uses: mxschmitt/action-tmate@v3
if: ${{ github.event_name == 'workflow_dispatch' && inputs.debug_enabled }}
- uses: Swatinem/rust-cache@v2
with:
cache-directories: |
"D:/opt/opencv"
# OPENCV_LINK_LIBS includes both opencv modules and 3rdparty. Note: 3rdparty libs may be named differently on different platforms
# OPENCV_LINK_PATHS points to the installed staticlib folder after cmake install
# OPENCV_INCLUDE_PATHS points to installed include folder after cmake install
# OPENCV_MSVC_CRT is for CRT library to be static or dynamic using windows MSVC: https://github.com/twistedfall/opencv-rust/blob/master/README.md#troubleshooting
# Build
- name: Build
#run: $env:OPENCV_LINK_LIBS=$(Get-ChildItem -Path C:\tools -Recurse -Filter 'opencv_world*.lib' | Select-Object -first 1 | Select-Object -ExpandProperty Name); $env:OPENCV_LINK_PATHS=$(Get-ChildItem -Path C:\tools -Recurse -Filter 'opencv_world*.lib' | Select-Object -first 1 | Select-Object -ExpandProperty FullName | Split-Path -parent); $env:OPENCV_INCLUDE_PATHS="C:\tools\opencv\build\include"; cargo build --release
run: |
$env:OPENCV_LINK_LIBS="opencv_core481,opencv_videoio481,opencv_imgcodecs481,opencv_imgproc481,ippiw,ittnotify,ippicvmt,zlib,IlmImf,libjpeg-turbo,libopenjp2,libpng,libtiff,libwebp"
$env:OPENCV_LINK_PATHS="D:/opt/opencv/staticlib"
$env:OPENCV_INCLUDE_PATHS="D:/opt/opencv/include"
$env:OPENCV_MSVC_CRT="static"
cargo build --release
# Upload artifact: https://github.com/actions/upload-artifact
- name: Upload Artifacts
uses: actions/upload-artifact@v4
with:
name: imagetools
path: |
target/release/imagetools.exe
target/release/imagetools.pdb
retention-days: 14