Skip to content

Commit 9d38d18

Browse files
committed
cudaPackages.buildRedist: include removeStubsFromRunpathHook
Signed-off-by: Connor Baker <ConnorBaker01@gmail.com>
1 parent ee1f087 commit 9d38d18

File tree

5 files changed

+86
-1
lines changed

5 files changed

+86
-1
lines changed

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

Lines changed: 55 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
@@ -142,6 +148,55 @@ checkCudaNonEmptyOutputs() {
142148
return 0
143149
}
144150

151+
checkCudaHasStubsIffIncludeRemoveStubsFromRunpathHook() {
152+
local outputName
153+
local -i hasStubs
154+
local -i hasRemoveStubsFromRunpathHook
155+
local -a outputNamesWronglyExcludingHook=()
156+
local -a outputNamesWronglyIncludingHook=()
157+
158+
for outputName in $(getAllOutputNames); do
159+
hasStubs=0
160+
if find "${!outputName:?}" -mindepth 1 -type d -name stubs -print -quit | grep --silent .; then
161+
hasStubs=1
162+
fi
163+
164+
# The dependency should be recorded in both propagated-native-build-inputs and propagated-build-inputs, so the
165+
# hook is propagated regardless of which dependency array includes the stubs-providing output.
166+
hasRemoveStubsFromRunpathHook=0
167+
if
168+
grep --silent --no-messages removeStubsFromRunpathHook "${!outputName:?}/nix-support/propagated-native-build-inputs" &&
169+
grep --silent --no-messages removeStubsFromRunpathHook "${!outputName:?}/nix-support/propagated-build-inputs"
170+
then
171+
hasRemoveStubsFromRunpathHook=1
172+
fi
173+
174+
if ((hasStubs && !hasRemoveStubsFromRunpathHook)); then
175+
outputNamesWronglyExcludingHook+=("${outputName:?}")
176+
elif ((!hasStubs && hasRemoveStubsFromRunpathHook)); then
177+
outputNamesWronglyIncludingHook+=("${outputName:?}")
178+
fi
179+
done
180+
181+
if ((${#outputNamesWronglyExcludingHook[@]})); then
182+
nixErrorLog "we detected outputs containing a stubs directory without a dependency on" \
183+
"removeStubsFromRunpathHook: ${outputNamesWronglyExcludingHook[*]}"
184+
nixErrorLog "ensure redistributables providing stubs set includeRemoveStubsFromRunpathHook to true"
185+
fi
186+
187+
if ((${#outputNamesWronglyIncludingHook[@]})); then
188+
nixErrorLog "we detected outputs without a stubs directory with a dependency on" \
189+
"removeStubsFromRunpathHook: ${outputNamesWronglyIncludingHook[*]}"
190+
nixErrorLog "ensure redistributables without stubs do not set includeRemoveStubsFromRunpathHook to true"
191+
fi
192+
193+
if ((${#outputNamesWronglyExcludingHook[@]} || ${#outputNamesWronglyIncludingHook[@]})); then
194+
exit 1
195+
fi
196+
197+
return 0
198+
}
199+
145200
# TODO(@connorbaker): https://github.com/NixOS/nixpkgs/issues/323126.
146201
# _multioutPropagateDev() currently expects a space-separated string rather than an array.
147202
# NOTE: Because _multioutPropagateDev is a postFixup hook, we correct it in preFixup.

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

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
lib,
1414
manifests,
1515
markForCudatoolkitRootHook,
16+
removeStubsFromRunpathHook,
1617
srcOnly,
1718
stdenv,
1819
stdenvNoCC,
@@ -22,6 +23,7 @@ let
2223
inherit (_cuda.lib) getNixSystems _mkCudaVariant mkRedistUrl;
2324
inherit (lib.attrsets)
2425
foldlAttrs
26+
getDev
2527
hasAttr
2628
isAttrs
2729
attrNames
@@ -53,6 +55,7 @@ let
5355
;
5456
inherit (lib.strings)
5557
concatMapStringsSep
58+
optionalString
5659
toUpper
5760
stringLength
5861
substring
@@ -136,6 +139,8 @@ extendMkDerivation {
136139

137140
# Fixups
138141
appendRunpaths ? [ ],
142+
includeRemoveStubsFromRunpathHook ? elem "stubs" finalAttrs.outputs,
143+
postFixup ? "",
139144

140145
# Extra
141146
passthru ? { },
@@ -199,7 +204,10 @@ extendMkDerivation {
199204
outputPython = [ "python" ];
200205
outputSamples = [ "samples" ];
201206
outputStatic = [ "static" ];
202-
outputStubs = [ "stubs" ];
207+
outputStubs = [
208+
"stubs"
209+
"lib"
210+
];
203211
},
204212
...
205213
}:
@@ -264,6 +272,9 @@ extendMkDerivation {
264272
# in typically /lib/opengl-driver by adding that
265273
# directory to the rpath of all ELF binaries.
266274
# 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.
267278
autoAddDriverRunpath
268279
markForCudatoolkitRootHook
269280
]
@@ -330,6 +341,18 @@ extendMkDerivation {
330341

331342
inherit doInstallCheck;
332343
inherit allowFHSReferences;
344+
inherit includeRemoveStubsFromRunpathHook;
345+
346+
postFixup =
347+
postFixup
348+
+ optionalString finalAttrs.includeRemoveStubsFromRunpathHook ''
349+
nixLog "installing stub removal runpath hook"
350+
mkdir -p "''${!outputStubs:?}/nix-support"
351+
printWords >>"''${!outputStubs:?}/nix-support/propagated-native-build-inputs" \
352+
"${getDev removeStubsFromRunpathHook.__spliced.buildHost or removeStubsFromRunpathHook}"
353+
printWords >>"''${!outputStubs:?}/nix-support/propagated-build-inputs" \
354+
"${getDev removeStubsFromRunpathHook.__spliced.hostTarget or removeStubsFromRunpathHook}"
355+
'';
333356

334357
passthru = passthru // {
335358
inherit redistName release;

pkgs/development/cuda-modules/default.nix

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,7 @@ let
150150
cudaNamePrefix
151151
manifests
152152
markForCudatoolkitRootHook
153+
removeStubsFromRunpathHook
153154
;
154155
};
155156

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)