Merge pull request #119126 from fabaff/pycomfoconnect
[NixPkgs.git] / pkgs / development / compilers / cudatoolkit / common.nix
blobc18eb9d534a265edc17ced00add169faa7284708
1 args@
2 { version
3 , sha256
4 , url ? ""
5 , name ? ""
6 , developerProgram ? false
7 , runPatches ? []
8 , addOpenGLRunpath
9 , alsaLib
10 , expat
11 , fetchurl
12 , fontconfig
13 , freetype
14 , gcc
15 , gdk-pixbuf
16 , glib
17 , glibc
18 , gtk2
19 , lib
20 , makeWrapper
21 , ncurses5
22 , perl
23 , python27
24 , requireFile
25 , stdenv
26 , unixODBC
27 , xorg
28 , zlib
31 stdenv.mkDerivation rec {
32   pname = "cudatoolkit";
33   inherit version runPatches;
35   dontPatchELF = true;
36   dontStrip = true;
38   src =
39     if developerProgram then
40       requireFile {
41         message = ''
42           This nix expression requires that ${args.name} is already part of the store.
43           Register yourself to NVIDIA Accelerated Computing Developer Program, retrieve the CUDA toolkit
44           at https://developer.nvidia.com/cuda-toolkit, and run the following command in the download directory:
45           nix-prefetch-url file://\$PWD/${args.name}
46         '';
47         inherit (args) name sha256;
48       }
49     else
50       fetchurl {
51         inherit (args) url sha256;
52       };
54   outputs = [ "out" "lib" "doc" ];
56   nativeBuildInputs = [ perl makeWrapper addOpenGLRunpath ];
57   buildInputs = [ gdk-pixbuf ]; # To get $GDK_PIXBUF_MODULE_FILE via setup-hook
58   runtimeDependencies = [
59     ncurses5 expat python27 zlib glibc
60     xorg.libX11 xorg.libXext xorg.libXrender xorg.libXt xorg.libXtst xorg.libXi xorg.libXext
61     gtk2 glib fontconfig freetype unixODBC alsaLib
62   ];
64   rpath = "${lib.makeLibraryPath runtimeDependencies}:${stdenv.cc.cc.lib}/lib64";
66   unpackPhase = ''
67     sh $src --keep --noexec
69     ${lib.optionalString (lib.versionOlder version "10.1") ''
70       cd pkg/run_files
71       sh cuda-linux*.run --keep --noexec
72       sh cuda-samples*.run --keep --noexec
73       mv pkg ../../$(basename $src)
74       cd ../..
75       rm -rf pkg
77       for patch in $runPatches; do
78         sh $patch --keep --noexec
79         mv pkg $(basename $patch)
80       done
81     ''}
82   '';
84   installPhase = ''
85     runHook preInstall
86     mkdir $out
87     ${lib.optionalString (lib.versionOlder version "10.1") ''
88     cd $(basename $src)
89     export PERL5LIB=.
90     perl ./install-linux.pl --prefix="$out"
91     cd ..
92     for patch in $runPatches; do
93       cd $(basename $patch)
94       perl ./install_patch.pl --silent --accept-eula --installdir="$out"
95       cd ..
96     done
97     ''}
98     ${lib.optionalString (lib.versionAtLeast version "10.1" && lib.versionOlder version "11") ''
99       cd pkg/builds/cuda-toolkit
100       mv * $out/
101     ''}
102     ${lib.optionalString (lib.versionAtLeast version "11") ''
103       mkdir -p $out/bin $out/lib64 $out/include $out/doc
104       for dir in pkg/builds/* pkg/builds/cuda_nvcc/nvvm pkg/builds/cuda_cupti/extras/CUPTI; do
105         if [ -d $dir/bin ]; then
106           mv $dir/bin/* $out/bin
107         fi
108         if [ -d $dir/doc ]; then
109           (cd $dir/doc && find . -type d -exec mkdir -p $out/doc/\{} \;)
110           (cd $dir/doc && find . \( -type f -o -type l \) -exec mv \{} $out/doc/\{} \;)
111         fi
112         if [ -L $dir/include ] || [ -d $dir/include ]; then
113           (cd $dir/include && find . -type d -exec mkdir -p $out/include/\{} \;)
114           (cd $dir/include && find . \( -type f -o -type l \) -exec mv \{} $out/include/\{} \;)
115         fi
116         if [ -L $dir/lib64 ] || [ -d $dir/lib64 ]; then
117           (cd $dir/lib64 && find . -type d -exec mkdir -p $out/lib64/\{} \;)
118           (cd $dir/lib64 && find . \( -type f -o -type l \) -exec mv \{} $out/lib64/\{} \;)
119         fi
120       done
121       mv pkg/builds/cuda_nvcc/nvvm $out/nvvm
122     ''}
124     rm -f $out/tools/CUDA_Occupancy_Calculator.xls # FIXME: why?
126     ${lib.optionalString (lib.versionOlder version "10.1") ''
127     # let's remove the 32-bit libraries, they confuse the lib64->lib mover
128     rm -rf $out/lib
129     ''}
131     # Remove some cruft.
132     ${lib.optionalString ((lib.versionAtLeast version "7.0") && (lib.versionOlder version "10.1"))
133       "rm $out/bin/uninstall*"}
135     # Fixup path to samples (needed for cuda 6.5 or else nsight will not find them)
136     if [ -d "$out"/cuda-samples ]; then
137         mv "$out"/cuda-samples "$out"/samples
138     fi
140     # Change the #error on GCC > 4.9 to a #warning.
141     sed -i $out/include/host_config.h -e 's/#error\(.*unsupported GNU version\)/#warning\1/'
143     # Fix builds with newer glibc version
144     sed -i "1 i#define _BITS_FLOATN_H" "$out/include/host_defines.h"
146     # Ensure that cmake can find CUDA.
147     mkdir -p $out/nix-support
148     echo "cmakeFlags+=' -DCUDA_TOOLKIT_ROOT_DIR=$out'" >> $out/nix-support/setup-hook
150     # Move some libraries to the lib output so that programs that
151     # depend on them don't pull in this entire monstrosity.
152     mkdir -p $lib/lib
153     mv -v $out/lib64/libcudart* $lib/lib/
155     # Remove OpenCL libraries as they are provided by ocl-icd and driver.
156     rm -f $out/lib64/libOpenCL*
157     ${lib.optionalString (lib.versionAtLeast version "10.1" && (lib.versionOlder version "11")) ''
158       mv $out/lib64 $out/lib
159       mv $out/extras/CUPTI/lib64/libcupti* $out/lib
160     ''}
162     # Set compiler for NVCC.
163     wrapProgram $out/bin/nvcc \
164       --prefix PATH : ${gcc}/bin
166     # nvprof do not find any program to profile if LD_LIBRARY_PATH is not set
167     wrapProgram $out/bin/nvprof \
168       --prefix LD_LIBRARY_PATH : $out/lib
169   '' + lib.optionalString (lib.versionOlder version "8.0") ''
170     # Hack to fix building against recent Glibc/GCC.
171     echo "NIX_CFLAGS_COMPILE+=' -D_FORCE_INLINES'" >> $out/nix-support/setup-hook
172   '' + ''
173     runHook postInstall
174   '';
176   postInstall = ''
177     for b in nvvp ${lib.optionalString (lib.versionOlder version "11") "nsight"}; do
178       wrapProgram "$out/bin/$b" \
179         --set GDK_PIXBUF_MODULE_FILE "$GDK_PIXBUF_MODULE_FILE"
180     done
181   '';
183   preFixup = ''
184     while IFS= read -r -d ''$'\0' i; do
185       if ! isELF "$i"; then continue; fi
186       echo "patching $i..."
187       if [[ ! $i =~ \.so ]]; then
188         patchelf \
189           --set-interpreter "''$(cat $NIX_CC/nix-support/dynamic-linker)" $i
190       fi
191       if [[ $i =~ libcudart ]]; then
192         rpath2=
193       else
194         rpath2=$rpath:$lib/lib:$out/jre/lib/amd64/jli:$out/lib:$out/lib64:$out/nvvm/lib:$out/nvvm/lib64
195       fi
196       patchelf --set-rpath "$rpath2" --force-rpath $i
197     done < <(find $out $lib $doc -type f -print0)
198   '';
200   # Set RPATH so that libcuda and other libraries in
201   # /run/opengl-driver(-32)/lib can be found. See the explanation in
202   # addOpenGLRunpath.  Don't try to figure out which libraries really need
203   # it, just patch all (but not the stubs libraries). Note that
204   # --force-rpath prevents changing RPATH (set above) to RUNPATH.
205   postFixup = ''
206     addOpenGLRunpath --force-rpath {$out,$lib}/lib/lib*.so
207   '';
209   # cuda-gdb doesn't run correctly when not using sandboxing, so
210   # temporarily disabling the install check.  This should be set to true
211   # when we figure out how to get `cuda-gdb --version` to run correctly
212   # when not using sandboxing.
213   doInstallCheck = false;
214   postInstallCheck = let
215   in ''
216     # Smoke test binaries
217     pushd $out/bin
218     for f in *; do
219       case $f in
220         crt)                           continue;;
221         nvcc.profile)                  continue;;
222         nsight_ee_plugins_manage.sh)   continue;;
223         uninstall_cuda_toolkit_6.5.pl) continue;;
224         computeprof|nvvp|nsight)       continue;; # GUIs don't feature "--version"
225         *)                             echo "Executing '$f --version':"; ./$f --version;;
226       esac
227     done
228     popd
229   '';
230   passthru = {
231     cc = gcc;
232     majorVersion = lib.versions.majorMinor version;
233   };
235   meta = with lib; {
236     description = "A compiler for NVIDIA GPUs, math libraries, and tools";
237     homepage = "https://developer.nvidia.com/cuda-toolkit";
238     platforms = [ "x86_64-linux" ];
239     license = licenses.unfree;
240   };