Skip to content

Commit 3e172af

Browse files
ConnorBakerSomeoneSerge
authored andcommitted
cudaPackages.buildRedist: include removeStubsFromRunpathHook
Signed-off-by: Connor Baker <ConnorBaker01@gmail.com>
1 parent be1e48f commit 3e172af

5 files changed

Lines changed: 95 additions & 5 deletions

File tree

pkgs/development/cuda-modules/buildRedist/buildRedistHook.bash

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,12 @@ buildRedistHookRegistration() {
2929

3030
postFixupHooks+=(fixupCudaPropagatedBuildOutputsToOut)
3131
nixLog "added fixupCudaPropagatedBuildOutputsToOut to postFixupHooks"
32+
33+
# NOTE: We need to do this in postFixup since we don't write the dependency on removeStubsFromRunpathHook until
34+
# postFixup -- recall recordPropagatedDependencies happens during fixupPhase.
35+
# NOTE: Iff is shorthand for "if and only if" -- the logical biconditional.
36+
postFixupHooks+=(checkCudaHasStubsIffIncludeRemoveStubsFromRunpathHook)
37+
nixLog "added checkCudaHasStubsIffIncludeRemoveStubsFromRunpathHook to postFixupHooks"
3238
}
3339

3440
buildRedistHookRegistration
@@ -140,6 +146,62 @@ checkCudaNonEmptyOutputs() {
140146
return 0
141147
}
142148

149+
# Any redistributable providing stubs should set includeRemoveStubsFromRunpathHook to true -- since we don't track the
150+
# contents of the redistributables, it's only included by default if there is a stubs output.
151+
# This check additionally requires that any output which has a stubs directory includes a dependency on
152+
# includeRemoveStubsFromRunpathHook -- that way, if *any* of them are used, the hook is brought in as well.
153+
# Since includeRemoveStubsFromRunpathHook only adds the hook to whatever outputStubs resolves to, having stubs present
154+
# across multiple outputs will result in an error.
155+
checkCudaHasStubsIffIncludeRemoveStubsFromRunpathHook() {
156+
local outputName
157+
local -i hasStubs
158+
local -i hasRemoveStubsFromRunpathHook
159+
local -a outputNamesWronglyExcludingHook=()
160+
local -a outputNamesWronglyIncludingHook=()
161+
162+
for outputName in $(getAllOutputNames); do
163+
# Record the output if it contains a directory named "stubs" and doesn't include a dependency on
164+
# removeStubsFromRunpathHook.
165+
hasStubs=0
166+
if find "${!outputName:?}" -mindepth 1 -type d -name stubs -print -quit | grep --silent .; then
167+
hasStubs=1
168+
fi
169+
170+
hasRemoveStubsFromRunpathHook=0
171+
if
172+
grep --silent --no-messages removeStubsFromRunpathHook "${!outputName:?}/nix-support/propagated-build-inputs"
173+
then
174+
hasRemoveStubsFromRunpathHook=1
175+
fi
176+
177+
if ((hasStubs && !hasRemoveStubsFromRunpathHook)); then
178+
# Outputs with stubs must include the hook.
179+
outputNamesWronglyExcludingHook+=("${outputName:?}")
180+
elif ((!hasStubs && hasRemoveStubsFromRunpathHook)); then
181+
# Outputs without stubs cannot include the hook.
182+
outputNamesWronglyIncludingHook+=("${outputName:?}")
183+
fi
184+
done
185+
186+
if ((${#outputNamesWronglyExcludingHook[@]})); then
187+
nixErrorLog "includeRemoveStubsFromRunpathHook is false but we detected outputs containing a stubs" \
188+
"directory: ${outputNamesWronglyExcludingHook[*]}"
189+
nixErrorLog "ensure redistributables providing stubs set includeRemoveStubsFromRunpathHook to true"
190+
fi
191+
192+
if ((${#outputNamesWronglyIncludingHook[@]})); then
193+
nixErrorLog "includeRemoveStubsFromRunpathHook is true but we detected outputs without a stubs" \
194+
"directory: ${outputNamesWronglyIncludingHook[*]}"
195+
nixErrorLog "ensure redistributables without stubs do not set includeRemoveStubsFromRunpathHook to true"
196+
fi
197+
198+
if ((${#outputNamesWronglyExcludingHook[@]} || ${#outputNamesWronglyIncludingHook[@]})); then
199+
exit 1
200+
fi
201+
202+
return 0
203+
}
204+
143205
# TODO(@connorbaker): https://github.com/NixOS/nixpkgs/issues/323126.
144206
# _multioutPropagateDev() currently expects a space-separated string rather than an array.
145207
# NOTE: Because _multioutPropagateDev is a postFixup hook, we correct it in preFixup.

pkgs/development/cuda-modules/buildRedist/default.nix

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,14 @@
66
autoAddDriverRunpath,
77
autoPatchelfHook,
88
backendStdenv,
9-
config,
109
cudaMajorMinorVersion,
1110
cudaMajorVersion,
1211
cudaNamePrefix,
1312
fetchurl,
1413
lib,
1514
manifests,
1615
markForCudatoolkitRootHook,
17-
setupCudaHook,
16+
removeStubsFromRunpathHook,
1817
srcOnly,
1918
stdenv,
2019
stdenvNoCC,
@@ -24,6 +23,7 @@ let
2423
inherit (_cuda.lib) getNixSystems _mkCudaVariant mkRedistUrl;
2524
inherit (lib.attrsets)
2625
foldlAttrs
26+
getDev
2727
hasAttr
2828
isAttrs
2929
attrNames
@@ -55,6 +55,7 @@ let
5555
;
5656
inherit (lib.strings)
5757
concatMapStringsSep
58+
optionalString
5859
toUpper
5960
stringLength
6061
substring
@@ -138,6 +139,8 @@ extendMkDerivation {
138139

139140
# Fixups
140141
appendRunpaths ? [ ],
142+
includeRemoveStubsFromRunpathHook ? elem "stubs" finalAttrs.outputs,
143+
postFixup ? "",
141144

142145
# Extra
143146
passthru ? { },
@@ -201,7 +204,10 @@ extendMkDerivation {
201204
outputPython = [ "python" ];
202205
outputSamples = [ "samples" ];
203206
outputStatic = [ "static" ];
204-
outputStubs = [ "stubs" ];
207+
outputStubs = [
208+
"stubs"
209+
"lib"
210+
];
205211
},
206212
...
207213
}:
@@ -266,6 +272,9 @@ extendMkDerivation {
266272
# in typically /lib/opengl-driver by adding that
267273
# directory to the rpath of all ELF binaries.
268274
# Check e.g. with `patchelf --print-rpath path/to/my/binary
275+
# TODO(@connorbaker): Given we'll have stubs available, we can switch from autoPatchelfIgnoreMissingDeps to
276+
# allowing autoPatchelf to find and link against the stub files and rely on removeStubsFromRunpathHook to
277+
# automatically find and replace those references with ones to the driver link lib directory.
269278
autoAddDriverRunpath
270279
markForCudatoolkitRootHook
271280
]
@@ -332,6 +341,20 @@ extendMkDerivation {
332341

333342
inherit doInstallCheck;
334343
inherit allowFHSReferences;
344+
inherit includeRemoveStubsFromRunpathHook;
345+
346+
postFixup =
347+
postFixup
348+
# Register removeStubsFromRunpathHook.
349+
# This must happen in postFixup after recordPropagatedDependencies (fixupPhase).
350+
+ ''
351+
if [[ -n "''${includeRemoveStubsFromRunpathHook:-}" ]] ; then
352+
nixLog "installing stub removal runpath hook"
353+
mkdir -p "''${!outputStubs:?}/nix-support"
354+
printWords >>"''${!outputStubs:?}/nix-support/propagated-build-inputs" \
355+
"${getDev removeStubsFromRunpathHook}"
356+
fi
357+
'';
335358

336359
passthru = passthru // {
337360
inherit redistName release;

pkgs/development/cuda-modules/default.nix

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,6 @@ let
132132
buildRedist = import ./buildRedist {
133133
inherit
134134
_cuda
135-
config
136135
lib
137136
;
138137
inherit (pkgs)
@@ -151,7 +150,7 @@ let
151150
cudaNamePrefix
152151
manifests
153152
markForCudatoolkitRootHook
154-
setupCudaHook
153+
removeStubsFromRunpathHook
155154
;
156155
};
157156

pkgs/development/cuda-modules/packages/cuda_cudart.nix

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@ buildRedist (finalAttrs: {
2222
"out"
2323
];
2424

25+
# We have stubs but we don't have an explicit stubs output.
26+
includeRemoveStubsFromRunpathHook = true;
27+
2528
propagatedBuildOutputs =
2629
# required by CMake
2730
lib.optionals (lib.elem "static" finalAttrs.outputs) [ "static" ]

pkgs/development/cuda-modules/packages/libnvfatbin.nix

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@ buildRedist {
55

66
outputs = [ "out" ];
77

8+
# Includes stubs.
9+
includeRemoveStubsFromRunpathHook = true;
10+
811
meta = {
912
description = "APIs which can be used at runtime to combine multiple CUDA objects into one CUDA fat binary (fatbin)";
1013
homepage = "https://docs.nvidia.com/cuda/nvfatbin";

0 commit comments

Comments
 (0)