-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Expand file tree
/
Copy pathDockerfile
More file actions
186 lines (171 loc) · 8.23 KB
/
Copy pathDockerfile
File metadata and controls
186 lines (171 loc) · 8.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
########## Build arguments ##########
ARG OPENCV_VERSION=5.0.0
########## Build OpenCV ##########
FROM ubuntu:resolute AS opencv-builder
ARG OPENCV_VERSION
ENV DEBIAN_FRONTEND=noninteractive
ENV OPENCV_VERSION=${OPENCV_VERSION}
ENV CCACHE_DIR=/root/.ccache
ENV CCACHE_MAXSIZE=2G
WORKDIR /
# Install build and OpenCV dependencies (with BuildKit cache for apt)
RUN --mount=type=cache,target=/var/cache/apt \
--mount=type=cache,target=/var/lib/apt/lists \
set -eux; \
apt-get -o Acquire::Retries=3 update; \
apt-get -y install --no-install-recommends \
apt-transport-https \
software-properties-common \
wget \
unzip \
ca-certificates \
build-essential \
cmake \
git \
ccache \
libtbb-dev \
libopenblas-dev \
liblapacke-dev \
libgtk-3-dev \
libavcodec-dev \
libavformat-dev \
libswscale-dev \
libdc1394-dev \
libxine2-dev \
libv4l-dev \
libtheora-dev \
libvorbis-dev \
libxvidcore-dev \
libopencore-amrnb-dev \
libopencore-amrwb-dev \
x264 \
libtesseract-dev \
libgdiplus \
pkg-config \
libavutil-dev \
ninja-build
# Copy cmake options (needed before OpenCV configure)
COPY cmake/opencv_build_options.cmake /opencv_build_options.cmake
# Fetch OpenCV and opencv_contrib sources
RUN set -eux; \
wget -q https://github.com/opencv/opencv/archive/${OPENCV_VERSION}.zip; \
unzip -q ${OPENCV_VERSION}.zip; \
rm ${OPENCV_VERSION}.zip; \
mv opencv-${OPENCV_VERSION} /opencv; \
wget -q https://github.com/opencv/opencv_contrib/archive/${OPENCV_VERSION}.zip; \
unzip -q ${OPENCV_VERSION}.zip; \
rm ${OPENCV_VERSION}.zip; \
mv opencv_contrib-${OPENCV_VERSION} /opencv_contrib
# Configure, build, and install OpenCV with ccache and parallel build
#
# -D CMAKE_ASM_COMPILER="": disables the ASM language so OpenCV 5's vendored
# MLAS turns off and DNN falls back to its built-in SGEMM. The MLAS FP16/GQA
# path references MlasHGemmSupported(), which OpenCV 5.0.0 declares and calls
# but never defines, breaking the extern link with an undefined reference
# (same fix as windows.yml / linux-arm64.yml / macos.yml).
RUN --mount=type=cache,target=/root/.ccache \
set -eux; \
cmake -G Ninja \
-C /opencv_build_options.cmake \
-S /opencv -B /opencv/build \
-D OPENCV_EXTRA_MODULES_PATH=/opencv_contrib/modules \
-D CMAKE_BUILD_TYPE=RELEASE \
-D CMAKE_C_COMPILER_LAUNCHER=ccache \
-D CMAKE_CXX_COMPILER_LAUNCHER=ccache \
-D CMAKE_INSTALL_PREFIX=/opencv_artifacts \
-D CMAKE_ASM_COMPILER=""; \
cmake --build /opencv/build --parallel "$(nproc)"; \
cmake --install /opencv/build; \
ldconfig
########## Build OpenCvSharp native extern ##########
FROM opencv-builder AS opencvsharp-builder
# Copy local OpenCvSharp sources
COPY src/ /opencvsharp/src/
# Build and install OpenCvSharp native extern with ccache
RUN --mount=type=cache,target=/root/.ccache \
set -eux; \
mkdir -p /opencvsharp/make; \
cmake -G Ninja \
-S /opencvsharp/src -B /opencvsharp/make \
-D CMAKE_INSTALL_PREFIX=/opencvsharp/make \
-D CMAKE_C_COMPILER_LAUNCHER=ccache \
-D CMAKE_CXX_COMPILER_LAUNCHER=ccache \
-D CMAKE_PREFIX_PATH=/opencv_artifacts; \
cmake --build /opencvsharp/make --parallel "$(nproc)"; \
cmake --install /opencvsharp/make; \
rm -rf /opencv /opencv_contrib; \
cp /opencvsharp/make/OpenCvSharpExtern/libOpenCvSharpExtern.so /usr/lib/; \
mkdir -p /artifacts; \
cp /opencvsharp/make/OpenCvSharpExtern/libOpenCvSharpExtern.so /artifacts/
########## Test native .so file ##########
FROM mcr.microsoft.com/dotnet/sdk:10.0-noble AS test-native
# libopenblas0/liblapacke: runtime deps of libOpenCvSharpExtern.so (linked against
# OpenBLAS/LAPACKE in the opencv-builder stage). Installed fresh here (rather than
# relying on the /usr/lib copy below) because the update-alternatives symlinks that
# make libopenblas.so.0 resolvable live outside /usr/lib and would otherwise be lost.
RUN apt-get update && apt-get -y install --no-install-recommends gcc libopenblas0 liblapacke && rm -rf /var/lib/apt/lists/*
# OpenBLAS's own pthread-based internal thread pool crashes (SIGABRT) when called
# concurrently from OpenCV's own parallel_for_ (e.g. stitching's BestOf2NearestMatcher
# matches image pairs on multiple threads, each hitting LAPACK/SVD). Force OpenBLAS to
# stay single-threaded internally; OpenCV's own parallelism is unaffected.
ENV OPENBLAS_NUM_THREADS=1
COPY --from=opencvsharp-builder /usr/lib /usr/lib
RUN echo "\n\
#include <stdio.h> \n\
int core_Mat_sizeof(); \n\
int main(){ \n\
int i = core_Mat_sizeof(); \n\
printf(\"sizeof(Mat) = %d\", i); \n\
return 0; \n\
}" > /test.c && \
gcc -I./ -L./ test.c -o test -lOpenCvSharpExtern && \
LD_LIBRARY_PATH=. ./test
########## Test .NET class libraries ##########
FROM mcr.microsoft.com/dotnet/sdk:10.0-noble AS test-dotnet
# See test-native above for why these are installed fresh instead of relying on
# the /usr/lib copy. clang/zlib1g-dev are link-time dependencies of PublishAot's ILCompiler
# (used by the NativeAOT smoke test further below) - installed here, in the same apt-get pass as
# everything else, rather than in a second `apt-get update` after the test run below: a second
# pass hit a reproducible `apt-get` segfault (`libapt-private.so.0.0: no version information
# available` followed by SIGSEGV) in this same container stage, immediately after the 1700+ tests
# above it had run. Root cause unconfirmed, but installing everything up front sidesteps it.
RUN apt-get update && apt-get -y install --no-install-recommends libopenblas0 liblapacke clang zlib1g-dev && rm -rf /var/lib/apt/lists/*
# See test-native above: keep OpenBLAS single-threaded to avoid crashing when called
# from OpenCV's own parallel_for_ (e.g. stitching's BestOf2NearestMatcher).
ENV OPENBLAS_NUM_THREADS=1
COPY --from=opencvsharp-builder /usr/lib /usr/lib
COPY global.json /opencvsharp/global.json
COPY src/ /opencvsharp/src/
COPY test/ /opencvsharp/test/
WORKDIR /opencvsharp
RUN cd /opencvsharp/src/OpenCvSharp && \
dotnet build -c Release -f net8.0 && \
cd /opencvsharp/src/OpenCvSharp.GdipExtensions && \
dotnet build -c Release -f net8.0
RUN dotnet test --project /opencvsharp/test/OpenCvSharp.Tests/OpenCvSharp.Tests.csproj -c Release -f net10.0 --runtime linux-x64 --results-directory /opencvsharp/test/OpenCvSharp.Tests/TestResults --report-xunit-trx --report-xunit-trx-filename test-results.trx < /dev/null
# Restore and publish as separate steps: see windows.yml's "Test NativeAOT publish" step for why
# a combined restore+publish invocation fails restore with NETSDK1207 (NuGet's static graph
# restore doesn't honor the OpenCvSharp.Analyzers ProjectReference's UndefineProperties the way
# an actual build does). libOpenCvSharpExtern.so resolves via /usr/lib's default linker search
# path (COPY'd there above), so no LD_LIBRARY_PATH is needed to run the published exe.
RUN cd /opencvsharp/test/OpenCvSharp.Tests.NativeAot && \
dotnet restore -r linux-x64 && \
dotnet publish -c Release -f net10.0 --runtime linux-x64 -p:PublishAot=true --no-restore && \
./bin/Release/net10.0/linux-x64/publish/OpenCvSharp.Tests.NativeAot
########## Final images ##########
FROM mcr.microsoft.com/dotnet/sdk:10.0-noble AS final-sdk
# See test-native above for why these are installed fresh instead of relying on
# the /usr/lib copy.
RUN apt-get update && apt-get -y install --no-install-recommends libopenblas0 liblapacke && rm -rf /var/lib/apt/lists/*
# See test-native above: keep OpenBLAS single-threaded to avoid crashing when called
# from OpenCV's own parallel_for_ (e.g. stitching's BestOf2NearestMatcher).
ENV OPENBLAS_NUM_THREADS=1
COPY --from=opencvsharp-builder /usr/lib /usr/lib
COPY --from=opencvsharp-builder /artifacts /artifacts
FROM mcr.microsoft.com/dotnet/aspnet:10.0-noble AS final
RUN apt-get update && apt-get -y install --no-install-recommends libopenblas0 liblapacke && rm -rf /var/lib/apt/lists/*
# See test-native above: keep OpenBLAS single-threaded to avoid crashing when called
# from OpenCV's own parallel_for_ (e.g. stitching's BestOf2NearestMatcher).
ENV OPENBLAS_NUM_THREADS=1
COPY --from=opencvsharp-builder /usr/lib /usr/lib
COPY --from=opencvsharp-builder /artifacts /artifacts