Unverified Commit 5825210d authored by Connor Baker's avatar Connor Baker Committed by GitHub
Browse files

Merge pull request #233581 from SomeoneSerge/CUDAToolkit_ROOT

cudaPackages.setupCudaHook: init
parents 49587518 251d3166
Loading
Loading
Loading
Loading
+6 −12
Original line number Diff line number Diff line
@@ -24,6 +24,7 @@ args@
, libkrb5
, krb5
, makeWrapper
, markForCudatoolkitRootHook
, ncurses5
, numactl
, nss
@@ -31,6 +32,7 @@ args@
, python3 # FIXME: CUDAToolkit 10 may still need python27
, pulseaudio
, requireFile
, setupCudaHook
, stdenv
, backendStdenv # E.g. gcc11Stdenv, set in extension.nix
, unixODBC
@@ -80,11 +82,15 @@ backendStdenv.mkDerivation rec {
    addOpenGLRunpath
    autoPatchelfHook
    autoAddOpenGLRunpathHook
    markForCudatoolkitRootHook
  ] ++ lib.optionals (lib.versionOlder version "11") [
    libsForQt5.wrapQtAppsHook
  ] ++ lib.optionals (lib.versionAtLeast version "11.8") [
    qt6Packages.wrapQtAppsHook
  ];
  depsTargetTargetPropagated = [
    setupCudaHook
  ];
  buildInputs = lib.optionals (lib.versionOlder version "11") [
    libsForQt5.qt5.qtwebengine
    freeglut
@@ -280,24 +286,12 @@ backendStdenv.mkDerivation rec {
    sed -i "1 i#define _BITS_FLOATN_H" "$out/include/host_defines.h"
  '' +
  # Point NVCC at a compatible compiler
  # FIXME: redist cuda_nvcc copy-pastes this code
  # Refer to comments in the overrides for cuda_nvcc for explanation
  # CUDA_TOOLKIT_ROOT_DIR is legacy,
  # Cf. https://cmake.org/cmake/help/latest/module/FindCUDA.html#input-variables
  # NOTE: We unconditionally set -Xfatbin=-compress-all, which reduces the size of the compiled
  #   binaries. If binaries grow over 2GB, they will fail to link. This is a problem for us, as
  #   the default set of CUDA capabilities we build can regularly cause this to occur (for
  #   example, with Magma).
  ''
    mkdir -p $out/nix-support
    cat <<EOF >> $out/nix-support/setup-hook
    cmakeFlags+=' -DCUDA_TOOLKIT_ROOT_DIR=$out'
    cmakeFlags+=' -DCUDA_HOST_COMPILER=${backendStdenv.cc}/bin'
    cmakeFlags+=' -DCMAKE_CUDA_HOST_COMPILER=${backendStdenv.cc}/bin'
    if [ -z "\''${CUDAHOSTCXX-}" ]; then
      export CUDAHOSTCXX=${backendStdenv.cc}/bin;
    fi
    export NVCC_PREPEND_FLAGS+=' --compiler-bindir=${backendStdenv.cc}/bin -Xfatbin=-compress-all'
    EOF

    # Move some libraries to the lib output so that programs that
+30 −1
Original line number Diff line number Diff line
@@ -38,10 +38,39 @@ final: prev: let

  cudaFlags = final.callPackage ./flags.nix {};

  # Internal hook, used by cudatoolkit and cuda redist packages
  # to accommodate automatic CUDAToolkit_ROOT construction
  markForCudatoolkitRootHook = (final.callPackage
    ({ makeSetupHook }:
      makeSetupHook
        { name = "mark-for-cudatoolkit-root-hook"; }
        ./hooks/mark-for-cudatoolkit-root-hook.sh)
    { });

  # Normally propagated by cuda_nvcc or cudatoolkit through their depsHostHostPropagated
  setupCudaHook = (final.callPackage
    ({ makeSetupHook, backendStdenv }:
      makeSetupHook
        {
          name = "setup-cuda-hook";

          substitutions.ccRoot = "${backendStdenv.cc}";

          # Required in addition to ccRoot as otherwise bin/gcc is looked up
          # when building CMakeCUDACompilerId.cu
          substitutions.ccFullPath = "${backendStdenv.cc}/bin/${backendStdenv.cc.targetPrefix}c++";
        }
        ./hooks/setup-cuda-hook.sh)
    { });

in
{
  inherit
    backendStdenv
    cudatoolkit
    cudaFlags;
    cudaFlags
    markForCudatoolkitRootHook
    setupCudaHook;

    saxpy = final.callPackage ./saxpy { };
}
+8 −0
Original line number Diff line number Diff line
# shellcheck shell=bash

markForCUDAToolkit_ROOT() {
    mkdir -p "${prefix}/nix-support"
    touch "${prefix}/nix-support/include-in-cudatoolkit-root"
}

fixupOutputHooks+=(markForCUDAToolkit_ROOT)
+5 −0
Original line number Diff line number Diff line
# shellcheck shell=bash

# CMake's enable_language(CUDA) runs a compiler test and it doesn't account for
# CUDAToolkit_ROOT. We have to help it locate libcudart
export NVCC_APPEND_FLAGS+=" -L@cudartRoot@/lib -I@cudartRoot@/include"
+68 −0
Original line number Diff line number Diff line
# shellcheck shell=bash

echo Sourcing setup-cuda-hook >&2

extendCUDAToolkit_ROOT() {
    if [[ -f "$1/nix-support/include-in-cudatoolkit-root" ]] ; then
        addToSearchPathWithCustomDelimiter ";" CUDAToolkit_ROOT "$1"

        if [[ -d "$1/include" ]] ; then
            addToSearchPathWithCustomDelimiter ";" CUDAToolkit_INCLUDE_DIR "$1/include"
        fi
    fi
}

addEnvHooks "$targetOffset" extendCUDAToolkit_ROOT

setupCUDAToolkitCompilers() {
    echo Executing setupCUDAToolkitCompilers >&2

    if [[ -n "${dontSetupCUDAToolkitCompilers-}" ]] ; then
        return
    fi

    # Point NVCC at a compatible compiler

    # For CMake-based projects:
    # https://cmake.org/cmake/help/latest/module/FindCUDA.html#input-variables
    # https://cmake.org/cmake/help/latest/envvar/CUDAHOSTCXX.html
    # https://cmake.org/cmake/help/latest/variable/CMAKE_CUDA_HOST_COMPILER.html

    export cmakeFlags+=" -DCUDA_HOST_COMPILER=@ccFullPath@"
    export cmakeFlags+=" -DCMAKE_CUDA_HOST_COMPILER=@ccFullPath@"

    # For non-CMake projects:
    # We prepend --compiler-bindir to nvcc flags.
    # Downstream packages can override these, because NVCC
    # uses the last --compiler-bindir it gets on the command line.
    # FIXME: this results in "incompatible redefinition" warnings.
    # https://docs.nvidia.com/cuda/cuda-compiler-driver-nvcc/index.html#compiler-bindir-directory-ccbin
    if [ -z "${CUDAHOSTCXX-}" ]; then
      export CUDAHOSTCXX="@ccFullPath@";
    fi

    export NVCC_PREPEND_FLAGS+=" --compiler-bindir=@ccRoot@/bin"

    # NOTE: We set -Xfatbin=-compress-all, which reduces the size of the compiled
    #   binaries. If binaries grow over 2GB, they will fail to link. This is a problem for us, as
    #   the default set of CUDA capabilities we build can regularly cause this to occur (for
    #   example, with Magma).
    #
    # @SomeoneSerge: original comment was made by @ConnorBaker in .../cudatoolkit/common.nix
    if [[ -z "${dontCompressFatbin-}" ]]; then
        export NVCC_PREPEND_FLAGS+=" -Xfatbin=-compress-all"
    fi

    # CMake's enable_language(CUDA) runs a compiler test and it doesn't account for
    # CUDAToolkit_ROOT. We have to help it locate libcudart
    if [[ -z "${nvccDontPrependCudartFlags-}" ]] ; then
        export NVCC_APPEND_FLAGS+=" -L@cudartRoot@/lib -I@cudartRoot@/include"
    fi
}

setupCMakeCUDAToolkit_ROOT() {
    export cmakeFlags+=" -DCUDAToolkit_INCLUDE_DIR=$CUDAToolkit_INCLUDE_DIR -DCUDAToolkit_ROOT=$CUDAToolkit_ROOT"
}

postHooks+=(setupCUDAToolkitCompilers)
preConfigureHooks+=(setupCMakeCUDAToolkit_ROOT)
Loading