Unverified Commit 4735963a authored by Someone Serge's avatar Someone Serge
Browse files

opencv4: cuda: fix libstdc++ errors

parent a122884c
Loading
Loading
Loading
Loading
+26 −29
Original line number Diff line number Diff line
@@ -83,11 +83,16 @@
, Accelerate
, bzip2
, callPackage
}:
}@inputs:

let
  version = "4.7.0";

  # It's necessary to consistently use backendStdenv when building with CUDA
  # support, otherwise we get libstdc++ errors downstream
  stdenv = throw "Use effectiveStdenv instead";
  effectiveStdenv = if enableCuda then cudaPackages.backendStdenv else inputs.stdenv;

  src = fetchFromGitHub {
    owner = "opencv";
    repo = "opencv";
@@ -121,11 +126,11 @@ let
      sha256 = "1msbkc3zixx61rcg6a04i1bcfhw1phgsrh93glq1n80hgsk3nbjq";
    } + "/ippicv";
    files = let name = platform: "ippicv_2019_${platform}_general_20180723.tgz"; in
      if stdenv.hostPlatform.system == "x86_64-linux" then
      if effectiveStdenv.hostPlatform.system == "x86_64-linux" then
        { ${name "lnx_intel64"} = "c0bd78adb4156bbf552c1dfe90599607"; }
      else if stdenv.hostPlatform.system == "i686-linux" then
      else if effectiveStdenv.hostPlatform.system == "i686-linux" then
        { ${name "lnx_ia32"} = "4f38432c30bfd6423164b7a24bbc98a0"; }
      else if stdenv.hostPlatform.system == "x86_64-darwin" then
      else if effectiveStdenv.hostPlatform.system == "x86_64-darwin" then
        { ${name "mac_intel64"} = "fe6b2bb75ae0e3f19ad3ae1a31dfa4a2"; }
      else
        throw "ICV is not available for this platform (or not yet supported by this package)";
@@ -232,10 +237,11 @@ let
  #https://github.com/xianyi/OpenBLAS/wiki/Faq/4bded95e8dc8aadc70ce65267d1093ca7bdefc4c#multi-threaded
  openblas_ = blas.provider.override { singleThreaded = true; };

  inherit (cudaPackages) backendStdenv cudaFlags cudaVersion;
  inherit (cudaPackages) cudaFlags cudaVersion;
  inherit (cudaFlags) cudaCapabilities;

  cuda-common-redist = with cudaPackages; [
    cuda_cudart
    cuda_cccl # <thrust/*>
    libnpp # npp.h
  ] ++ lib.optionals enableCublas [
@@ -246,21 +252,13 @@ let
    libcufft # cufft.h
  ];

  cuda-native-redist = symlinkJoin {
    name = "cuda-native-redist-${cudaVersion}";
    paths = with cudaPackages; [
      cuda_cudart # cuda_runtime.h
      cuda_nvcc
    ] ++ cuda-common-redist;
   };

  cuda-redist = symlinkJoin {
    name = "cuda-redist-${cudaVersion}";
    paths = cuda-common-redist;
   };
in

stdenv.mkDerivation {
effectiveStdenv.mkDerivation {
  pname = "opencv";
  inherit version src;

@@ -319,7 +317,7 @@ stdenv.mkDerivation {

  buildInputs = [ zlib pcre boost gflags protobuf3_21 ]
    ++ lib.optional enablePython pythonPackages.python
    ++ lib.optional (stdenv.buildPlatform == stdenv.hostPlatform) hdf5
    ++ lib.optional (effectiveStdenv.buildPlatform == effectiveStdenv.hostPlatform) hdf5
    ++ lib.optional enableGtk2 gtk2
    ++ lib.optional enableGtk3 gtk3
    ++ lib.optional enableVtk vtk
@@ -330,7 +328,7 @@ stdenv.mkDerivation {
    ++ lib.optionals enableEXR [ openexr ilmbase ]
    ++ lib.optional enableJPEG2000 openjpeg
    ++ lib.optional enableFfmpeg ffmpeg
    ++ lib.optionals (enableFfmpeg && stdenv.isDarwin)
    ++ lib.optionals (enableFfmpeg && effectiveStdenv.isDarwin)
      [ VideoDecodeAcceleration bzip2 ]
    ++ lib.optionals enableGStreamer (with gst_all_1; [ gstreamer gst-plugins-base gst-plugins-good ])
    ++ lib.optional enableOvis ogre
@@ -343,7 +341,7 @@ stdenv.mkDerivation {
    # tesseract & leptonica.
    ++ lib.optionals enableTesseract [ tesseract leptonica ]
    ++ lib.optional enableTbb tbb
    ++ lib.optionals stdenv.isDarwin [
    ++ lib.optionals effectiveStdenv.isDarwin [
      bzip2 AVFoundation Cocoa VideoDecodeAcceleration CoreMedia MediaToolbox Accelerate
    ]
    ++ lib.optionals enableDocs [ doxygen graphviz-nox ]
@@ -357,7 +355,9 @@ stdenv.mkDerivation {
    pythonPackages.pip
    pythonPackages.wheel
    pythonPackages.setuptools
  ] ++ lib.optionals enableCuda [ cuda-native-redist ];
  ] ++ lib.optionals enableCuda [
    cudaPackages.cuda_nvcc
  ];

  env.NIX_CFLAGS_COMPILE = lib.optionalString enableEXR "-I${ilmbase.dev}/include/OpenEXR";

@@ -400,17 +400,14 @@ stdenv.mkDerivation {
    (opencvFlag "ENABLE_LTO" enableLto)
    (opencvFlag "ENABLE_THIN_LTO" (
      enableLto && (
        # Only clang supports thin LTO, so we must either be using clang through the stdenv,
        stdenv.cc.isClang ||
          # or through the backend stdenv.
          (enableCuda && backendStdenv.cc.isClang)
        # Only clang supports thin LTO, so we must either be using clang through the effectiveStdenv,
        effectiveStdenv.cc.isClang ||
          # or through the backend effectiveStdenv.
          (enableCuda && effectiveStdenv.cc.isClang)
      )
    ))
  ] ++ lib.optionals enableCuda [
    "-DCUDA_FAST_MATH=ON"
    # We need to set the C and C++ host compilers for CUDA to the same compiler.
    "-DCMAKE_C_COMPILER=${backendStdenv.cc}/bin/cc"
    "-DCMAKE_CXX_COMPILER=${backendStdenv.cc}/bin/c++"
    "-DCUDA_NVCC_FLAGS=--expt-relaxed-constexpr"

    # OpenCV respects at least three variables:
@@ -421,7 +418,7 @@ stdenv.mkDerivation {
    "-DCUDA_ARCH_PTX=${lib.last cudaCapabilities}"

    "-DNVIDIA_OPTICAL_FLOW_2_0_HEADERS_PATH=${nvidia-optical-flow-sdk}"
  ] ++ lib.optionals stdenv.isDarwin [
  ] ++ lib.optionals effectiveStdenv.isDarwin [
    "-DWITH_OPENCL=OFF"
    "-DWITH_LAPACK=OFF"

@@ -435,7 +432,7 @@ stdenv.mkDerivation {
    "-DBUILD_JPEG=OFF"
    "-DBUILD_PNG=OFF"
    "-DBUILD_WEBP=OFF"
  ] ++ lib.optionals (!stdenv.isDarwin) [
  ] ++ lib.optionals (!effectiveStdenv.isDarwin) [
    "-DOPENCL_LIBRARY=${ocl-icd}/lib/libOpenCL.so"
  ] ++ lib.optionals enablePython [
    "-DOPENCV_SKIP_PYTHON_LOADER=ON"
@@ -489,9 +486,9 @@ stdenv.mkDerivation {
    tests = {
      inherit (gst_all_1) gst-plugins-bad;
    }
    // lib.optionalAttrs (!stdenv.isDarwin) { inherit qimgv; }
    // lib.optionalAttrs (!effectiveStdenv.isDarwin) { inherit qimgv; }
    // lib.optionalAttrs (!enablePython) { pythonEnabled = pythonPackages.opencv4; }
    // lib.optionalAttrs (stdenv.buildPlatform != "x86_64-darwin") {
    // lib.optionalAttrs (effectiveStdenv.buildPlatform != "x86_64-darwin") {
      opencv4-tests = callPackage ./tests.nix {
        inherit enableGStreamer enableGtk2 enableGtk3 runAccuracyTests runPerformanceTests testDataSrc;
        inherit opencv4;