Skip to content

Commit 59c6666

Browse files
author
Connor Baker
committed
cudaPackages: use modules to parse redistrib and feature manifests
1 parent ff43b1e commit 59c6666

17 files changed

Lines changed: 1014 additions & 386 deletions

File tree

Lines changed: 141 additions & 151 deletions
Original file line numberDiff line numberDiff line change
@@ -1,174 +1,164 @@
1-
# Type Aliases
2-
#
3-
# See ./extension.nix:
4-
# - ReleaseAttrs
5-
# - ReleaseFeaturesAttrs
6-
#
7-
# General callPackage-supplied arguments
8-
{ lib
9-
, stdenv
10-
, backendStdenv
11-
, fetchurl
12-
, autoPatchelfHook
13-
, autoAddOpenGLRunpathHook
14-
, markForCudatoolkitRootHook
15-
, lndir
16-
, symlinkJoin
17-
}:
18-
# Function arguments
191
{
2+
# General callPackage-supplied arguments
3+
autoAddOpenGLRunpathHook,
4+
autoPatchelfHook,
5+
backendStdenv,
6+
fetchurl,
7+
lib,
8+
lndir,
9+
markForCudatoolkitRootHook,
10+
stdenv,
11+
# Function arguments
2012
# Short package name (e.g., "cuda_cccl")
2113
# pname : String
22-
pname
23-
, # Long package name (e.g., "CXX Core Compute Libraries")
14+
pname,
15+
# Long package name (e.g., "CXX Core Compute Libraries")
2416
# description : String
25-
description
26-
, # platforms : List System
27-
platforms
28-
, # version : Version
29-
version
30-
, # releaseAttrs : ReleaseAttrs
31-
releaseAttrs
32-
, # releaseFeaturesAttrs : ReleaseFeaturesAttrs
33-
releaseFeaturesAttrs
34-
,
35-
}:
36-
let
17+
description,
18+
# platforms : List System
19+
platforms,
20+
# version : Version
21+
version,
22+
# See ./modules/redistrib_manifest/package.nix
23+
redistrib_package,
24+
# See ./modules/feature_manifest/package.nix
25+
feature_package,
26+
}: let
3727
# Useful imports
3828
inherit (lib.lists) optionals;
3929
inherit (lib.meta) getExe;
4030
inherit (lib.strings) optionalString;
4131
in
42-
backendStdenv.mkDerivation {
43-
# NOTE: Even though there's no actual buildPhase going on here, the derivations of the
44-
# redistributables are sensitive to the compiler flags provided to stdenv. The patchelf package
45-
# is sensitive to the compiler flags provided to stdenv, and we depend on it. As such, we are
46-
# also sensitive to the compiler flags provided to stdenv.
47-
inherit pname version;
48-
strictDeps = true;
32+
backendStdenv.mkDerivation {
33+
# NOTE: Even though there's no actual buildPhase going on here, the derivations of the
34+
# redistributables are sensitive to the compiler flags provided to stdenv. The patchelf package
35+
# is sensitive to the compiler flags provided to stdenv, and we depend on it. As such, we are
36+
# also sensitive to the compiler flags provided to stdenv.
37+
inherit pname version;
38+
strictDeps = true;
4939

50-
outputs = with releaseFeaturesAttrs;
51-
[ "out" ]
52-
++ optionals hasBin [ "bin" ]
53-
++ optionals hasLib [ "lib" ]
54-
++ optionals hasStatic [ "static" ]
55-
++ optionals hasDev [ "dev" ]
56-
++ optionals hasDoc [ "doc" ]
57-
++ optionals hasSample [ "sample" ];
40+
outputs = with feature_package.outputs;
41+
["out"]
42+
++ optionals hasBin ["bin"]
43+
++ optionals hasLib ["lib"]
44+
++ optionals hasStatic ["static"]
45+
++ optionals hasDev ["dev"]
46+
++ optionals hasDoc ["doc"]
47+
++ optionals hasSample ["sample"];
5848

59-
src = fetchurl {
60-
url = "https://developer.download.nvidia.com/compute/cuda/redist/${releaseAttrs.relative_path}";
61-
inherit (releaseAttrs) sha256;
62-
};
49+
src = fetchurl {
50+
url = "https://developer.download.nvidia.com/compute/cuda/redist/${redistrib_package.relative_path}";
51+
inherit (redistrib_package) sha256;
52+
};
6353

64-
# We do need some other phases, like configurePhase, so the multiple-output setup hook works.
65-
dontBuild = true;
54+
# We do need some other phases, like configurePhase, so the multiple-output setup hook works.
55+
dontBuild = true;
6656

67-
nativeBuildInputs = [
68-
autoPatchelfHook
69-
# This hook will make sure libcuda can be found
70-
# in typically /lib/opengl-driver by adding that
71-
# directory to the rpath of all ELF binaries.
72-
# Check e.g. with `patchelf --print-rpath path/to/my/binary
73-
autoAddOpenGLRunpathHook
74-
markForCudatoolkitRootHook
75-
];
57+
nativeBuildInputs = [
58+
autoPatchelfHook
59+
# This hook will make sure libcuda can be found
60+
# in typically /lib/opengl-driver by adding that
61+
# directory to the rpath of all ELF binaries.
62+
# Check e.g. with `patchelf --print-rpath path/to/my/binary
63+
autoAddOpenGLRunpathHook
64+
markForCudatoolkitRootHook
65+
];
7666

77-
buildInputs = [
78-
# autoPatchelfHook will search for a libstdc++ and we're giving it
79-
# one that is compatible with the rest of nixpkgs, even when
80-
# nvcc forces us to use an older gcc
81-
# NB: We don't actually know if this is the right thing to do
82-
stdenv.cc.cc.lib
83-
];
67+
buildInputs = [
68+
# autoPatchelfHook will search for a libstdc++ and we're giving it
69+
# one that is compatible with the rest of nixpkgs, even when
70+
# nvcc forces us to use an older gcc
71+
# NB: We don't actually know if this is the right thing to do
72+
stdenv.cc.cc.lib
73+
];
8474

85-
# Picked up by autoPatchelf
86-
# Needed e.g. for libnvrtc to locate (dlopen) libnvrtc-builtins
87-
appendRunpaths = [
88-
"$ORIGIN"
89-
];
75+
# Picked up by autoPatchelf
76+
# Needed e.g. for libnvrtc to locate (dlopen) libnvrtc-builtins
77+
appendRunpaths = [
78+
"$ORIGIN"
79+
];
9080

91-
installPhase = with releaseFeaturesAttrs;
81+
installPhase = with feature_package.outputs;
9282
# Pre-install hook
93-
''
94-
runHook preInstall
95-
''
96-
# doc and dev have special output handling. Other outputs need to be moved to their own
97-
# output.
98-
# Note that moveToOutput operates on all outputs:
99-
# https://github.com/NixOS/nixpkgs/blob/2920b6fc16a9ed5d51429e94238b28306ceda79e/pkgs/build-support/setup-hooks/multiple-outputs.sh#L105-L107
100-
+ ''
83+
''
84+
runHook preInstall
85+
''
86+
# doc and dev have special output handling. Other outputs need to be moved to their own
87+
# output.
88+
# Note that moveToOutput operates on all outputs:
89+
# https://github.com/NixOS/nixpkgs/blob/2920b6fc16a9ed5d51429e94238b28306ceda79e/pkgs/build-support/setup-hooks/multiple-outputs.sh#L105-L107
90+
+ ''
91+
mkdir -p "$out"
92+
rm LICENSE
93+
mv * "$out"
94+
''
95+
# Handle bin, which defaults to out
96+
+ optionalString hasBin ''
97+
moveToOutput "bin" "$bin"
98+
''
99+
# Handle lib, which defaults to out
100+
+ optionalString hasLib ''
101+
moveToOutput "lib" "$lib"
102+
''
103+
# Handle static libs, which isn't handled by the setup hook
104+
+ optionalString hasStatic ''
105+
moveToOutput "**/*.a" "$static"
106+
''
107+
# Handle samples, which isn't handled by the setup hook
108+
+ optionalString hasSample ''
109+
moveToOutput "samples" "$sample"
110+
''
111+
# Post-install hook
112+
+ ''
113+
runHook postInstall
114+
'';
115+
116+
# The out output leverages the same functionality which backs the `symlinkJoin` function in
117+
# Nixpkgs:
118+
# https://github.com/NixOS/nixpkgs/blob/d8b2a92df48f9b08d68b0132ce7adfbdbc1fbfac/pkgs/build-support/trivial-builders/default.nix#L510
119+
#
120+
# That should allow us to emulate "fat" default outputs without having to actually create them.
121+
#
122+
# It is important that this run after the autoPatchelfHook, otherwise the symlinks in out will reference libraries in lib, creating a circular dependency.
123+
postPhases = ["postPatchelf"];
124+
# For each output, create a symlink to it in the out output.
125+
# NOTE: We must recreate the out output here, because the setup hook will have deleted it
126+
# if it was empty.
127+
# NOTE: Do not use optionalString based on whether `outputs` contains only `out` -- phases
128+
# which are empty strings are skipped/unset and result in errors of the form "command not
129+
# found: <customPhaseName>".
130+
postPatchelf = ''
101131
mkdir -p "$out"
102-
rm LICENSE
103-
mv * "$out"
104-
''
105-
# Handle bin, which defaults to out
106-
+ optionalString hasBin ''
107-
moveToOutput "bin" "$bin"
108-
''
109-
# Handle lib, which defaults to out
110-
+ optionalString hasLib ''
111-
moveToOutput "lib" "$lib"
112-
''
113-
# Handle static libs, which isn't handled by the setup hook
114-
+ optionalString hasStatic ''
115-
moveToOutput "**/*.a" "$static"
116-
''
117-
# Handle samples, which isn't handled by the setup hook
118-
+ optionalString hasSample ''
119-
moveToOutput "samples" "$sample"
120-
''
121-
# Post-install hook
122-
+ ''
123-
runHook postInstall
132+
for output in $outputs; do
133+
if [ "$output" = "out" ]; then
134+
continue
135+
fi
136+
${getExe lndir} "''${!output}" "$out"
137+
done
124138
'';
125139

126-
# The out output leverages the same functionality which backs the `symlinkJoin` function in
127-
# Nixpkgs:
128-
# https://github.com/NixOS/nixpkgs/blob/d8b2a92df48f9b08d68b0132ce7adfbdbc1fbfac/pkgs/build-support/trivial-builders/default.nix#L510
129-
#
130-
# That should allow us to emulate "fat" default outputs without having to actually create them.
131-
#
132-
# It is important that this run after the autoPatchelfHook, otherwise the symlinks in out will reference libraries in lib, creating a circular dependency.
133-
postPhases = [ "postPatchelf" ];
134-
# For each output, create a symlink to it in the out output.
135-
# NOTE: We must recreate the out output here, because the setup hook will have deleted it
136-
# if it was empty.
137-
# NOTE: Do not use optionalString based on whether `outputs` contains only `out` -- phases
138-
# which are empty strings are skipped/unset and result in errors of the form "command not
139-
# found: <customPhaseName>".
140-
postPatchelf = ''
141-
mkdir -p "$out"
142-
for output in $outputs; do
143-
if [ "$output" = "out" ]; then
144-
continue
145-
fi
146-
${getExe lndir} "''${!output}" "$out"
147-
done
148-
'';
149-
150-
# Make the CUDA-patched stdenv available
151-
passthru.stdenv = backendStdenv;
140+
# Make the CUDA-patched stdenv available
141+
passthru.stdenv = backendStdenv;
152142

153-
# Setting propagatedBuildInputs to false will prevent outputs known to the multiple-outputs
154-
# from depending on `out` by default.
155-
# https://github.com/NixOS/nixpkgs/blob/2920b6fc16a9ed5d51429e94238b28306ceda79e/pkgs/build-support/setup-hooks/multiple-outputs.sh#L196
156-
# Indeed, we want to do the opposite -- fat "out" outputs that contain all the other outputs.
157-
propagatedBuildOutputs = false;
143+
# Setting propagatedBuildInputs to false will prevent outputs known to the multiple-outputs
144+
# from depending on `out` by default.
145+
# https://github.com/NixOS/nixpkgs/blob/2920b6fc16a9ed5d51429e94238b28306ceda79e/pkgs/build-support/setup-hooks/multiple-outputs.sh#L196
146+
# Indeed, we want to do the opposite -- fat "out" outputs that contain all the other outputs.
147+
propagatedBuildOutputs = false;
158148

159-
# By default, if the dev output exists it just uses that.
160-
# However, because we disabled propagatedBuildOutputs, dev doesn't contain libraries or
161-
# anything of the sort. To remedy this, we set outputSpecified to true, and use
162-
# outputsToInstall, which tells Nix which outputs to use when the package name is used
163-
# unqualified (that is, without an explicit output).
164-
outputSpecified = true;
149+
# By default, if the dev output exists it just uses that.
150+
# However, because we disabled propagatedBuildOutputs, dev doesn't contain libraries or
151+
# anything of the sort. To remedy this, we set outputSpecified to true, and use
152+
# outputsToInstall, which tells Nix which outputs to use when the package name is used
153+
# unqualified (that is, without an explicit output).
154+
outputSpecified = true;
165155

166-
meta = {
167-
inherit description platforms;
168-
license = lib.licenses.unfree;
169-
maintainers = lib.teams.cuda.members;
170-
# Force the use of the default, fat output by default (even though `dev` exists, which
171-
# causes Nix to prefer that output over the others if outputSpecified isn't set).
172-
outputsToInstall = [ "out" ];
173-
};
174-
}
156+
meta = {
157+
inherit description platforms;
158+
license = lib.licenses.unfree;
159+
maintainers = lib.teams.cuda.members;
160+
# Force the use of the default, fat output by default (even though `dev` exists, which
161+
# causes Nix to prefer that output over the others if outputSpecified isn't set).
162+
outputsToInstall = ["out"];
163+
};
164+
}

0 commit comments

Comments
 (0)