Advertisement
Guest User

3.x.nix

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