Skip to content

Commit ee1f087

Browse files
committed
cudaPackages.removeStubsFromRunpathHook: init
Signed-off-by: Connor Baker <ConnorBaker01@gmail.com>
1 parent caac266 commit ee1f087

2 files changed

Lines changed: 110 additions & 0 deletions

File tree

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
{
2+
addDriverRunpath,
3+
arrayUtilities,
4+
autoFixElfFiles,
5+
makeSetupHook,
6+
}:
7+
makeSetupHook {
8+
name = "removeStubsFromRunpathHook";
9+
propagatedBuildInputs = [
10+
arrayUtilities.getRunpathEntries
11+
autoFixElfFiles
12+
];
13+
14+
substitutions = {
15+
driverLinkLib = addDriverRunpath.driverLink + "/lib";
16+
};
17+
} ./removeStubsFromRunpathHook.bash
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
# shellcheck shell=bash
2+
3+
# Only run the hook from nativeBuildInputs when strictDeps is set
4+
if [[ -n ${removeStubsFromRunpathHookOnce-} ]]; then
5+
# shellcheck disable=SC2154
6+
nixDebugLog "skipping sourcing removeStubsFromRunpathHook.bash (hostOffset=$hostOffset) (targetOffset=$targetOffset)" \
7+
"because it has already been sourced"
8+
return 0
9+
elif [[ -n ${strictDeps:-} ]] && ! ((hostOffset == -1 && targetOffset == 0)); then
10+
nixDebugLog "skipping sourcing removeStubsFromRunpathHook.bash (hostOffset=$hostOffset) (targetOffset=$targetOffset)" \
11+
"because it is not in nativeBuildInputs"
12+
return 0
13+
fi
14+
15+
declare -g removeStubsFromRunpathHookOnce=1
16+
17+
nixLog "sourcing removeStubsFromRunpathHook.bash (hostOffset=$hostOffset) (targetOffset=$targetOffset)"
18+
19+
# NOTE: Adding to prePhases to ensure all setup hooks are sourced prior to adding our hook.
20+
appendToVar prePhases removeStubsFromRunpathHookRegistration
21+
nixLog "added removeStubsFromRunpathHookRegistration to prePhases"
22+
23+
# Registering during prePhases ensures that all setup hooks are sourced prior to installing ours,
24+
# allowing us to always go after autoAddDriverRunpath and autoPatchelfHook.
25+
removeStubsFromRunpathHookRegistration() {
26+
local postFixupHook
27+
28+
for postFixupHook in "${postFixupHooks[@]}"; do
29+
if [[ $postFixupHook == "autoFixElfFiles addDriverRunpath" ]]; then
30+
nixLog "discovered 'autoFixElfFiles addDriverRunpath' in postFixupHooks; this hook should be unnecessary when" \
31+
"linking against stub files!"
32+
fi
33+
done
34+
35+
postFixupHooks+=("autoFixElfFiles removeStubsFromRunpath")
36+
nixLog "added removeStubsFromRunpath to postFixupHooks"
37+
38+
return 0
39+
}
40+
41+
removeStubsFromRunpath() {
42+
local libPath
43+
local runpathEntry
44+
local -a origRunpathEntries=()
45+
local -a newRunpathEntries=()
46+
local -r driverLinkLib="@driverLinkLib@"
47+
local -i driverLinkLibSightings=0
48+
49+
if [[ $# -eq 0 ]]; then
50+
nixErrorLog "no library path provided" >&2
51+
exit 1
52+
elif [[ $# -gt 1 ]]; then
53+
nixErrorLog "too many arguments" >&2
54+
exit 1
55+
elif [[ $1 == "" ]]; then
56+
nixErrorLog "empty library path" >&2
57+
exit 1
58+
else
59+
libPath="$1"
60+
fi
61+
62+
getRunpathEntries "$libPath" origRunpathEntries
63+
64+
# NOTE: Always pre-increment since (( 0 )) sets an exit code of 1.
65+
for runpathEntry in "${origRunpathEntries[@]}"; do
66+
case $runpathEntry in
67+
# NOTE: This assumes all CUDA redistributables with stubs use cudaNamePrefix in name;
68+
# that should be safe given the redistributables are built with buildRedist, which does
69+
# this automatically.
70+
# Match on (sub)directories named stubs or lib inside a stubs output.
71+
*-cuda*/stubs|*-cuda*-stubs/lib*)
72+
if ((driverLinkLibSightings)); then
73+
# No need to add another copy of the driverLinkLib; just drop the stubs entry.
74+
nixDebugLog "dropping $libPath runpath entry: $runpathEntry"
75+
else
76+
# We haven't observed a driverLinkLib yet, so replace the stubs entry with one.
77+
nixDebugLog "replacing $libPath runpath entry: $runpathEntry -> $driverLinkLib"
78+
newRunpathEntries+=("$driverLinkLib")
79+
((++driverLinkLibSightings))
80+
fi
81+
;;
82+
83+
*)
84+
nixDebugLog "keeping $libPath runpath entry: $runpathEntry"
85+
newRunpathEntries+=("$runpathEntry")
86+
[[ $runpathEntry == "$driverLinkLib" ]] && ((++driverLinkLibSightings))
87+
;;
88+
esac
89+
done
90+
91+
local -r newRunpath=$(concatStringsSep ":" newRunpathEntries)
92+
patchelf --set-rpath "$newRunpath" "$libPath"
93+
}

0 commit comments

Comments
 (0)