Advertisement
Guest User

3.x.nix

a guest
Sep 23rd, 2019
247
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.25 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 ? true
  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. postFetch = "set -x";
  148.  
  149. postUnpack = lib.optionalString buildContrib ''
  150. cp --no-preserve=mode -r "${contribSrc}/modules" "$NIX_BUILD_TOP/opencv_contrib"
  151. '';
  152.  
  153. # This prevents cmake from using libraries in impure paths (which
  154. # causes build failure on non NixOS)
  155. # Also, work around https://github.com/NixOS/nixpkgs/issues/26304 with
  156. # what appears to be some stray headers in dnn/misc/tensorflow
  157. # in contrib when generating the Python bindings:
  158. postPatch = ''
  159. sed -i '/Add these standard paths to the search paths for FIND_LIBRARY/,/^\s*$/{d}' CMakeLists.txt
  160. sed -i -e 's|if len(decls) == 0:|if len(decls) == 0 or "opencv2/" not in hdr:|' ./modules/python/src2/gen2.py
  161. '';
  162.  
  163. preConfigure =
  164. lib.optionalString enableIpp (installExtraFiles ippicv) + (
  165. lib.optionalString buildContrib ''
  166. cmakeFlagsArray+=("-DOPENCV_EXTRA_MODULES_PATH=$NIX_BUILD_TOP/opencv_contrib")
  167.  
  168. ${installExtraFiles vgg}
  169. ${installExtraFiles boostdesc}
  170. ${installExtraFiles face}
  171. '');
  172.  
  173. postConfigure = ''
  174. [ -e modules/core/version_string.inc ]
  175. echo '"(build info elided)"' > modules/core/version_string.inc
  176. '';
  177.  
  178. buildInputs =
  179. [ zlib pcre hdf5 glog boost google-gflags ]
  180. ++ lib.optional useSystemProtobuf protobuf
  181. ++ lib.optional enablePython pythonPackages.python
  182. ++ lib.optional enableGtk2 gtk2
  183. ++ lib.optional enableGtk3 gtk3
  184. ++ lib.optional enableVtk vtk
  185. ++ lib.optional enableJPEG libjpeg
  186. ++ lib.optional enablePNG libpng
  187. ++ lib.optional enableTIFF libtiff
  188. ++ lib.optional enableWebP libwebp
  189. ++ lib.optionals enableEXR [ openexr ilmbase ]
  190. ++ lib.optional enableJPEG2K jasper
  191. ++ lib.optional enableFfmpeg ffmpeg
  192. ++ lib.optionals (enableFfmpeg && stdenv.isDarwin)
  193. [ VideoDecodeAcceleration bzip2 ]
  194. ++ lib.optionals enableGStreamer (with gst_all_1; [ gstreamer gst-plugins-base ])
  195. ++ lib.optional enableOvis ogre
  196. ++ lib.optional enableGPhoto2 libgphoto2
  197. ++ lib.optional enableDC1394 libdc1394
  198. ++ lib.optional enableEigen eigen
  199. ++ lib.optional enableOpenblas openblas
  200. # There is seemingly no compile-time flag for Tesseract. It's
  201. # simply enabled automatically if contrib is built, and it detects
  202. # tesseract & leptonica.
  203. ++ lib.optionals enableTesseract [ tesseract leptonica ]
  204. ++ lib.optional enableTbb tbb
  205. ++ lib.optional enableCuda cudatoolkit
  206. ++ lib.optionals stdenv.isDarwin [ cf-private AVFoundation Cocoa QTKit VideoDecodeAcceleration bzip2 ]
  207. ++ lib.optionals enableDocs [ doxygen graphviz-nox ];
  208.  
  209. propagatedBuildInputs = lib.optional enablePython pythonPackages.numpy;
  210.  
  211. nativeBuildInputs = [ cmake pkgconfig unzip ];
  212.  
  213. NIX_CFLAGS_COMPILE = lib.optional enableEXR "-I${ilmbase.dev}/include/OpenEXR";
  214.  
  215. # Configure can't find the library without this.
  216. OpenBLAS_HOME = lib.optionalString enableOpenblas openblas;
  217.  
  218. cmakeFlags = [
  219. "-DWITH_OPENMP=ON"
  220. "-DBUILD_PROTOBUF=${printEnabled (!useSystemProtobuf)}"
  221. "-DPROTOBUF_UPDATE_FILES=${printEnabled useSystemProtobuf}"
  222. "-DOPENCV_ENABLE_NONFREE=${printEnabled enableUnfree}"
  223. "-DBUILD_TESTS=OFF"
  224. "-DBUILD_PERF_TESTS=OFF"
  225. "-DBUILD_DOCS=${printEnabled enableDocs}"
  226. "-DCMAKE_INSTALL_PREFIX=$out"
  227. (opencvFlag "IPP" enableIpp)
  228. (opencvFlag "TIFF" enableTIFF)
  229. (opencvFlag "JASPER" enableJPEG2K)
  230. (opencvFlag "WEBP" enableWebP)
  231. (opencvFlag "JPEG" enableJPEG)
  232. (opencvFlag "PNG" enablePNG)
  233. (opencvFlag "OPENEXR" enableEXR)
  234. (opencvFlag "CUDA" enableCuda)
  235. (opencvFlag "CUBLAS" enableCuda)
  236. (opencvFlag "TBB" enableTbb)
  237. ] ++ lib.optionals enableCuda [
  238. "-DCUDA_FAST_MATH=ON"
  239. "-DCUDA_HOST_COMPILER=${cudatoolkit.cc}/bin/cc"
  240. "-DCUDA_NVCC_FLAGS=--expt-relaxed-constexpr"
  241. ] ++ lib.optionals stdenv.isDarwin [
  242. "-DWITH_OPENCL=OFF"
  243. "-DWITH_LAPACK=OFF"
  244. ] ++ lib.optionals enablePython [
  245. "-DOPENCV_SKIP_PYTHON_LOADER=ON"
  246. ] ++ lib.optional enableBuildWorld [
  247. "-D BUILD_opencv_world=ON"
  248. ];
  249.  
  250. enableParallelBuilding = true;
  251.  
  252. postBuild = lib.optionalString enableDocs ''
  253. make doxygen
  254. '';
  255.  
  256. # By default $out/lib/pkgconfig/opencv.pc looks something like this:
  257. #
  258. # prefix=/nix/store/10pzq1a8fkh8q4sysj8n6mv0w0nl0miq-opencv-3.4.1
  259. # exec_prefix=${prefix}
  260. # libdir=${exec_prefix}//nix/store/10pzq1a8fkh8q4sysj8n6mv0w0nl0miq-opencv-3.4.1/lib
  261. # ...
  262. # Libs: -L${exec_prefix}//nix/store/10pzq1a8fkh8q4sysj8n6mv0w0nl0miq-opencv-3.4.1/lib ...
  263. #
  264. # Note that ${exec_prefix} is set to $out but that $out is also appended to
  265. # ${exec_prefix}. This causes linker errors in downstream packages so we strip
  266. # of $out after the ${exec_prefix} prefix:
  267. postInstall = ''
  268. sed -i "s|{exec_prefix}/$out|{exec_prefix}|" \
  269. "$out/lib/pkgconfig/opencv.pc"
  270. '';
  271.  
  272. hardeningDisable = [ "bindnow" "relro" ];
  273.  
  274. passthru = lib.optionalAttrs enablePython { pythonPath = []; };
  275.  
  276. meta = with stdenv.lib; {
  277. description = "Open Computer Vision Library with more than 500 algorithms";
  278. homepage = https://opencv.org/;
  279. license = with licenses; if enableUnfree then unfree else bsd3;
  280. maintainers = with maintainers; [mdaiter basvandijk];
  281. platforms = with platforms; linux ++ darwin;
  282. };
  283. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement