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