lol
1#! @shell@ -e
2
3if [ -n "$NIX_LD_WRAPPER_START_HOOK" ]; then
4 source "$NIX_LD_WRAPPER_START_HOOK"
5fi
6
7if [ -z "$NIX_CC_WRAPPER_FLAGS_SET" ]; then
8 source @out@/nix-support/add-flags.sh
9fi
10
11source @out@/nix-support/utils.sh
12
13
14# Optionally filter out paths not refering to the store.
15params=("$@")
16if [ "$NIX_ENFORCE_PURITY" = 1 -a -n "$NIX_STORE" \
17 -a \( -z "$NIX_IGNORE_LD_THROUGH_GCC" -o -z "$NIX_LDFLAGS_SET" \) ]; then
18 rest=()
19 n=0
20 while [ $n -lt ${#params[*]} ]; do
21 p=${params[n]}
22 p2=${params[$((n+1))]}
23 if [ "${p:0:3}" = -L/ ] && badPath "${p:2}"; then
24 skip $p
25 elif [ "$p" = -L ] && badPath "$p2"; then
26 n=$((n + 1)); skip $p2
27 elif [ "$p" = -rpath ] && badPath "$p2"; then
28 n=$((n + 1)); skip $p2
29 elif [ "$p" = -dynamic-linker ] && badPath "$p2"; then
30 n=$((n + 1)); skip $p2
31 elif [ "${p:0:1}" = / ] && badPath "$p"; then
32 # We cannot skip this; barf.
33 echo "impure path \`$p' used in link" >&2
34 exit 1
35 elif [ "${p:0:9}" = --sysroot ]; then
36 # Our ld is not built with sysroot support (Can we fix that?)
37 :
38 else
39 rest=("${rest[@]}" "$p")
40 fi
41 n=$((n + 1))
42 done
43 params=("${rest[@]}")
44fi
45
46
47extra=()
48extraBefore=()
49
50if [ -z "$NIX_LDFLAGS_SET" ]; then
51 extra+=($NIX_LDFLAGS)
52 extraBefore+=($NIX_LDFLAGS_BEFORE)
53fi
54
55extra+=($NIX_LDFLAGS_AFTER)
56
57
58# Add all used dynamic libraries to the rpath.
59if [ "$NIX_DONT_SET_RPATH" != 1 ]; then
60
61 libPath=""
62 addToLibPath() {
63 local path="$1"
64 if [ "${path:0:1}" != / ]; then return 0; fi
65 case "$path" in
66 *..*|*./*|*/.*|*//*)
67 local path2
68 if path2=$(readlink -f "$path"); then
69 path="$path2"
70 fi
71 ;;
72 esac
73 case $libPath in
74 *\ $path\ *) return 0 ;;
75 esac
76 libPath="$libPath $path "
77 }
78
79 addToRPath() {
80 # If the path is not in the store, don't add it to the rpath.
81 # This typically happens for libraries in /tmp that are later
82 # copied to $out/lib. If not, we're screwed.
83 if [ "${1:0:${#NIX_STORE}}" != "$NIX_STORE" ]; then return 0; fi
84 case $rpath in
85 *\ $1\ *) return 0 ;;
86 esac
87 rpath="$rpath $1 "
88 }
89
90 libs=""
91 addToLibs() {
92 libs="$libs $1"
93 }
94
95 rpath=""
96
97 # First, find all -L... switches.
98 allParams=("${params[@]}" ${extra[@]})
99 n=0
100 while [ $n -lt ${#allParams[*]} ]; do
101 p=${allParams[n]}
102 p2=${allParams[$((n+1))]}
103 if [ "${p:0:3}" = -L/ ]; then
104 addToLibPath ${p:2}
105 elif [ "$p" = -L ]; then
106 addToLibPath ${p2}
107 n=$((n + 1))
108 elif [ "$p" = -l ]; then
109 addToLibs ${p2}
110 n=$((n + 1))
111 elif [ "${p:0:2}" = -l ]; then
112 addToLibs ${p:2}
113 elif [ "$p" = -dynamic-linker ]; then
114 # Ignore the dynamic linker argument, or it
115 # will get into the next 'elif'. We don't want
116 # the dynamic linker path rpath to go always first.
117 n=$((n + 1))
118 elif [[ "$p" =~ ^[^-].*\.so($|\.) ]]; then
119 # This is a direct reference to a shared library, so add
120 # its directory to the rpath.
121 path="$(dirname "$p")";
122 addToRPath "${path}"
123 fi
124 n=$((n + 1))
125 done
126
127 # Second, for each directory in the library search path (-L...),
128 # see if it contains a dynamic library used by a -l... flag. If
129 # so, add the directory to the rpath.
130 # It's important to add the rpath in the order of -L..., so
131 # the link time chosen objects will be those of runtime linking.
132
133 for i in $libPath; do
134 for j in $libs; do
135 if [ -f "$i/lib$j.so" ]; then
136 addToRPath $i
137 break
138 fi
139 done
140 done
141
142
143 # Finally, add `-rpath' switches.
144 for i in $rpath; do
145 extra=(${extra[@]} -rpath $i)
146 done
147fi
148
149
150# Optionally print debug info.
151if [ -n "$NIX_DEBUG" ]; then
152 echo "original flags to @prog@:" >&2
153 for i in "${params[@]}"; do
154 echo " $i" >&2
155 done
156 echo "extra flags to @prog@:" >&2
157 for i in ${extra[@]}; do
158 echo " $i" >&2
159 done
160fi
161
162if [ -n "$NIX_LD_WRAPPER_EXEC_HOOK" ]; then
163 source "$NIX_LD_WRAPPER_EXEC_HOOK"
164fi
165
166exec @prog@ ${extraBefore[@]} "${params[@]}" ${extra[@]}