nixpkgs mirror (for testing)
github.com/NixOS/nixpkgs
nix
1{ lib, stdenv
2, fetchFromGitHub
3, fetchpatch
4, cmake, pkg-config, unzip, zlib, pcre, hdf5
5, glog, boost, gflags, protobuf
6, config
7
8, enableJPEG ? true, libjpeg
9, enablePNG ? true, libpng
10, enableTIFF ? true, libtiff
11, enableWebP ? true, libwebp
12, enableEXR ? !stdenv.isDarwin, openexr, ilmbase
13, enableEigen ? true, eigen
14, enableOpenblas ? true, openblas, blas, lapack
15, enableContrib ? true
16
17, enableCuda ? (config.cudaSupport or false) &&
18 stdenv.hostPlatform.isx86_64
19, cudaPackages ? { }
20, enableUnfree ? false
21, enableIpp ? false
22, enablePython ? false, pythonPackages ? null
23, enableGtk2 ? false, gtk2
24, enableGtk3 ? false, gtk3
25, enableVtk ? false, vtk_8
26, enableFfmpeg ? false, ffmpeg
27, enableGStreamer ? false, gst_all_1
28, enableTesseract ? false, tesseract, leptonica
29, enableTbb ? false, tbb
30, enableOvis ? false, ogre
31, enableGPhoto2 ? false, libgphoto2
32, enableDC1394 ? false, libdc1394
33, enableDocs ? false, doxygen, graphviz-nox
34
35, AVFoundation, Cocoa, VideoDecodeAcceleration, bzip2, CoreMedia, MediaToolbox, Accelerate
36}:
37
38assert blas.implementation == "openblas" && lapack.implementation == "openblas";
39
40assert enablePython -> pythonPackages != null;
41
42let
43 inherit (cudaPackages) cudatoolkit;
44 inherit (cudaPackages.cudaFlags) cudaCapabilities;
45
46 version = "3.4.18";
47
48 src = fetchFromGitHub {
49 owner = "opencv";
50 repo = "opencv";
51 rev = version;
52 hash = "sha256-PgwAZNoPknFT0jCLt3TCzend6OYFY3iUIzDf/FptAYA=";
53 };
54
55 contribSrc = fetchFromGitHub {
56 owner = "opencv";
57 repo = "opencv_contrib";
58 rev = version;
59 hash = "sha256-TEF/GHglOmsshlC6q4iw14ZMpvA0SaKwlidomAN+sRc=";
60 };
61
62 # Contrib must be built in order to enable Tesseract support:
63 buildContrib = enableContrib || enableTesseract;
64
65 useSystemProtobuf = ! stdenv.isDarwin;
66
67 # See opencv/3rdparty/ippicv/ippicv.cmake
68 ippicv = {
69 src = fetchFromGitHub {
70 owner = "opencv";
71 repo = "opencv_3rdparty";
72 rev = "32e315a5b106a7b89dbed51c28f8120a48b368b4";
73 sha256 = "19w9f0r16072s59diqxsr5q6nmwyz9gnxjs49nglzhd66p3ddbkp";
74 } + "/ippicv";
75 files = let name = platform : "ippicv_2019_${platform}_general_20180723.tgz"; in
76 if stdenv.hostPlatform.system == "x86_64-linux" then
77 { ${name "lnx_intel64"} = "c0bd78adb4156bbf552c1dfe90599607"; }
78 else if stdenv.hostPlatform.system == "i686-linux" then
79 { ${name "lnx_ia32"} = "4f38432c30bfd6423164b7a24bbc98a0"; }
80 else if stdenv.hostPlatform.system == "x86_64-darwin" then
81 { ${name "mac_intel64"} = "fe6b2bb75ae0e3f19ad3ae1a31dfa4a2"; }
82 else
83 throw "ICV is not available for this platform (or not yet supported by this package)";
84 dst = ".cache/ippicv";
85 };
86
87 # See opencv_contrib/modules/xfeatures2d/cmake/download_vgg.cmake
88 vgg = {
89 src = fetchFromGitHub {
90 owner = "opencv";
91 repo = "opencv_3rdparty";
92 rev = "fccf7cd6a4b12079f73bbfb21745f9babcd4eb1d";
93 sha256 = "0r9fam8dplyqqsd3qgpnnfgf9l7lj44di19rxwbm8mxiw0rlcdvy";
94 };
95 files = {
96 "vgg_generated_48.i" = "e8d0dcd54d1bcfdc29203d011a797179";
97 "vgg_generated_64.i" = "7126a5d9a8884ebca5aea5d63d677225";
98 "vgg_generated_80.i" = "7cd47228edec52b6d82f46511af325c5";
99 "vgg_generated_120.i" = "151805e03568c9f490a5e3a872777b75";
100 };
101 dst = ".cache/xfeatures2d/vgg";
102 };
103
104 # See opencv_contrib/modules/xfeatures2d/cmake/download_boostdesc.cmake
105 boostdesc = {
106 src = fetchFromGitHub {
107 owner = "opencv";
108 repo = "opencv_3rdparty";
109 rev = "34e4206aef44d50e6bbcd0ab06354b52e7466d26";
110 sha256 = "13yig1xhvgghvxspxmdidss5lqiikpjr0ddm83jsi0k85j92sn62";
111 };
112 files = {
113 "boostdesc_bgm.i" = "0ea90e7a8f3f7876d450e4149c97c74f";
114 "boostdesc_bgm_bi.i" = "232c966b13651bd0e46a1497b0852191";
115 "boostdesc_bgm_hd.i" = "324426a24fa56ad9c5b8e3e0b3e5303e";
116 "boostdesc_binboost_064.i" = "202e1b3e9fec871b04da31f7f016679f";
117 "boostdesc_binboost_128.i" = "98ea99d399965c03d555cef3ea502a0b";
118 "boostdesc_binboost_256.i" = "e6dcfa9f647779eb1ce446a8d759b6ea";
119 "boostdesc_lbgm.i" = "0ae0675534aa318d9668f2a179c2a052";
120 };
121 dst = ".cache/xfeatures2d/boostdesc";
122 };
123
124 # See opencv_contrib/modules/face/CMakeLists.txt
125 face = {
126 src = fetchFromGitHub {
127 owner = "opencv";
128 repo = "opencv_3rdparty";
129 rev = "8afa57abc8229d611c4937165d20e2a2d9fc5a12";
130 sha256 = "061lsvqdidq9xa2hwrcvwi9ixflr2c2lfpc8drr159g68zi8bp4v";
131 };
132 files = {
133 "face_landmark_model.dat" = "7505c44ca4eb54b4ab1e4777cb96ac05";
134 };
135 dst = ".cache/data";
136 };
137
138 # See opencv/cmake/OpenCVDownload.cmake
139 installExtraFiles = extra : with lib; ''
140 mkdir -p "${extra.dst}"
141 '' + concatStrings (mapAttrsToList (name : md5 : ''
142 ln -s "${extra.src}/${name}" "${extra.dst}/${md5}-${name}"
143 '') extra.files);
144
145 opencvFlag = name: enabled: "-DWITH_${name}=${printEnabled enabled}";
146
147 printEnabled = enabled : if enabled then "ON" else "OFF";
148in
149
150stdenv.mkDerivation {
151 pname = "opencv";
152 inherit version src;
153
154 postUnpack = lib.optionalString buildContrib ''
155 cp --no-preserve=mode -r "${contribSrc}/modules" "$NIX_BUILD_TOP/opencv_contrib"
156 '';
157
158 # Ensures that we use the system OpenEXR rather than the vendored copy of the source included with OpenCV.
159 patches = [
160 ./cmake-don-t-use-OpenCVFindOpenEXR.patch
161 ];
162
163 # This prevents cmake from using libraries in impure paths (which
164 # causes build failure on non NixOS)
165 # Also, work around https://github.com/NixOS/nixpkgs/issues/26304 with
166 # what appears to be some stray headers in dnn/misc/tensorflow
167 # in contrib when generating the Python bindings:
168 postPatch = ''
169 sed -i '/Add these standard paths to the search paths for FIND_LIBRARY/,/^\s*$/{d}' CMakeLists.txt
170 sed -i -e 's|if len(decls) == 0:|if len(decls) == 0 or "opencv2/" not in hdr:|' ./modules/python/src2/gen2.py
171 '';
172
173 preConfigure =
174 lib.optionalString enableIpp (installExtraFiles ippicv) + (
175 lib.optionalString buildContrib ''
176 cmakeFlagsArray+=("-DOPENCV_EXTRA_MODULES_PATH=$NIX_BUILD_TOP/opencv_contrib")
177
178 ${installExtraFiles vgg}
179 ${installExtraFiles boostdesc}
180 ${installExtraFiles face}
181 '');
182
183 postConfigure = ''
184 [ -e modules/core/version_string.inc ]
185 echo '"(build info elided)"' > modules/core/version_string.inc
186 '';
187
188 buildInputs =
189 [ zlib pcre hdf5 glog boost gflags ]
190 ++ lib.optional useSystemProtobuf protobuf
191 ++ lib.optional enablePython pythonPackages.python
192 ++ lib.optional enableGtk2 gtk2
193 ++ lib.optional enableGtk3 gtk3
194 ++ lib.optional enableVtk vtk_8
195 ++ lib.optional enableJPEG libjpeg
196 ++ lib.optional enablePNG libpng
197 ++ lib.optional enableTIFF libtiff
198 ++ lib.optional enableWebP libwebp
199 ++ lib.optionals enableEXR [ openexr ilmbase ]
200 ++ lib.optional enableFfmpeg ffmpeg
201 ++ lib.optionals (enableFfmpeg && stdenv.isDarwin)
202 [ VideoDecodeAcceleration bzip2 ]
203 ++ lib.optionals enableGStreamer (with gst_all_1; [ gstreamer gst-plugins-base ])
204 ++ lib.optional enableOvis ogre
205 ++ lib.optional enableGPhoto2 libgphoto2
206 ++ lib.optional enableDC1394 libdc1394
207 ++ lib.optional enableEigen eigen
208 ++ lib.optional enableOpenblas openblas
209 # There is seemingly no compile-time flag for Tesseract. It's
210 # simply enabled automatically if contrib is built, and it detects
211 # tesseract & leptonica.
212 ++ lib.optionals enableTesseract [ tesseract leptonica ]
213 ++ lib.optional enableTbb tbb
214 ++ lib.optionals stdenv.isDarwin [
215 bzip2 AVFoundation Cocoa VideoDecodeAcceleration CoreMedia MediaToolbox Accelerate
216 ]
217 ++ lib.optionals enableDocs [ doxygen graphviz-nox ];
218
219 propagatedBuildInputs = lib.optional enablePython pythonPackages.numpy
220 ++ lib.optional enableCuda cudatoolkit;
221
222 nativeBuildInputs = [ cmake pkg-config unzip ];
223
224 env.NIX_CFLAGS_COMPILE = lib.optionalString enableEXR "-I${ilmbase.dev}/include/OpenEXR";
225
226 # Configure can't find the library without this.
227 OpenBLAS_HOME = lib.optionalString enableOpenblas openblas;
228
229 cmakeFlags = [
230 "-DWITH_OPENMP=ON"
231 "-DBUILD_PROTOBUF=${printEnabled (!useSystemProtobuf)}"
232 "-DPROTOBUF_UPDATE_FILES=${printEnabled useSystemProtobuf}"
233 "-DOPENCV_ENABLE_NONFREE=${printEnabled enableUnfree}"
234 "-DBUILD_TESTS=OFF"
235 "-DBUILD_PERF_TESTS=OFF"
236 "-DBUILD_DOCS=${printEnabled enableDocs}"
237 (opencvFlag "IPP" enableIpp)
238 (opencvFlag "TIFF" enableTIFF)
239 (opencvFlag "WEBP" enableWebP)
240 (opencvFlag "JPEG" enableJPEG)
241 (opencvFlag "PNG" enablePNG)
242 (opencvFlag "OPENEXR" enableEXR)
243 (opencvFlag "CUDA" enableCuda)
244 (opencvFlag "CUBLAS" enableCuda)
245 (opencvFlag "TBB" enableTbb)
246 ] ++ lib.optionals enableCuda [
247 "-DCUDA_FAST_MATH=ON"
248 "-DCUDA_HOST_COMPILER=${cudatoolkit.cc}/bin/cc"
249 "-DCUDA_NVCC_FLAGS=--expt-relaxed-constexpr"
250 "-DCUDA_ARCH_BIN=${lib.concatStringsSep ";" cudaCapabilities}"
251 "-DCUDA_ARCH_PTX=${lib.last cudaCapabilities}"
252 ] ++ lib.optionals stdenv.isDarwin [
253 "-DWITH_OPENCL=OFF"
254 "-DWITH_LAPACK=OFF"
255 ] ++ lib.optionals enablePython [
256 "-DOPENCV_SKIP_PYTHON_LOADER=ON"
257 ] ++ lib.optionals enableEigen [
258 # Autodetection broken by https://github.com/opencv/opencv/pull/13337
259 "-DEIGEN_INCLUDE_PATH=${eigen}/include/eigen3"
260 ];
261
262 postBuild = lib.optionalString enableDocs ''
263 make doxygen
264 '';
265
266 # By default $out/lib/pkgconfig/opencv.pc looks something like this:
267 #
268 # prefix=/nix/store/10pzq1a8fkh8q4sysj8n6mv0w0nl0miq-opencv-3.4.1
269 # exec_prefix=${prefix}
270 # libdir=${exec_prefix}//nix/store/10pzq1a8fkh8q4sysj8n6mv0w0nl0miq-opencv-3.4.1/lib
271 # ...
272 # Libs: -L${exec_prefix}//nix/store/10pzq1a8fkh8q4sysj8n6mv0w0nl0miq-opencv-3.4.1/lib ...
273 #
274 # Note that ${exec_prefix} is set to $out but that $out is also appended to
275 # ${exec_prefix}. This causes linker errors in downstream packages so we strip
276 # of $out after the ${exec_prefix} prefix:
277 postInstall = ''
278 sed -i "s|{exec_prefix}/$out|{exec_prefix}|" \
279 "$out/lib/pkgconfig/opencv.pc"
280 '';
281
282 hardeningDisable = [ "bindnow" "relro" ];
283
284 passthru = lib.optionalAttrs enablePython { pythonPath = []; };
285
286 meta = with lib; {
287 description = "Open Computer Vision Library with more than 500 algorithms";
288 homepage = "https://opencv.org/";
289 license = with licenses; if enableUnfree then unfree else bsd3;
290 maintainers = with maintainers; [mdaiter basvandijk];
291 platforms = with platforms; linux ++ darwin;
292 };
293}