sdrangel: fix build on x86_64-darwin
[NixPkgs.git] / pkgs / build-support / bintools-wrapper / macos-sierra-reexport-hack.bash
blob255071adf68193828adcbfa57cd5d89d3d9b7259
1 #! @shell@
3 set -eu -o pipefail
5 # For cmd | while read; do ...; done
6 shopt -s lastpipe
8 path_backup="$PATH"
9 if [ -n "@coreutils_bin@" ]; then
10 PATH="@coreutils_bin@/bin"
13 declare -ri recurThreshold=200
14 declare -i overflowCount=0
16 declare -ar origArgs=("$@")
18 # Throw away what we won't need
19 declare -a parentArgs=()
21 while (( $# )); do
22 case "$1" in
23 -l)
24 echo "cctools LD does not support '-l foo'" >&2
25 exit 1
27 -lazy_library | -reexport_library | -upward_library | -weak_library)
28 overflowCount+=1
29 shift 2
31 -l* | *.so.* | *.dylib | -lazy-l* | -reexport-l* | -upward-l* | -weak-l*)
32 overflowCount+=1
33 shift 1
35 *.a | *.o)
36 shift 1
38 -L | -F)
39 # Evidentally ld doesn't like using the child's RPATH, so it still
40 # needs these.
41 parentArgs+=("$1" "$2")
42 shift 2
44 -L?* | -F?*)
45 parentArgs+=("$1")
46 shift 1
48 -o)
49 outputName="$2"
50 parentArgs+=("$1" "$2")
51 shift 2
53 -install_name | -dylib_install_name | -dynamic-linker | -plugin)
54 parentArgs+=("$1" "$2")
55 shift 2
57 -rpath)
58 # Only an rpath to the child is needed, which we will add
59 shift 2
62 if [[ -f "$1" ]]; then
63 # Propabably a non-standard object file like Haskell's
64 # `.dyn_o`. Skip it like other inputs
66 else
67 parentArgs+=("$1")
69 shift 1
71 esac
72 done
76 if (( "$overflowCount" <= "$recurThreshold" )); then
77 if [ -n "${NIX_DEBUG:-}" ]; then
78 echo "ld-wrapper: Only ${overflowCount} inputs counted while ${recurThreshold} is the ceiling, linking normally. " >&2
80 PATH="$path_backup"
81 exec @prog@ "${origArgs[@]}"
86 if [ -n "${NIX_DEBUG:-}" ]; then
87 echo "ld-wrapper: ${overflowCount} inputs counted when ${recurThreshold} is the ceiling, inspecting further. " >&2
90 # Collect the normalized linker input
91 declare -a norm=()
93 # Arguments are null-separated
94 @prog@ --dump-normalized-lib-args "${origArgs[@]}" |
95 while IFS= read -r -d '' input; do
96 norm+=("$input")
97 done
99 declare -i leafCount=0
100 declare lastLeaf=''
101 declare -a childrenInputs=() trailingInputs=()
102 while (( "${#norm[@]}" )); do
103 case "${norm[0]}" in
104 -lazy_library | -upward_library)
105 # TODO(@Ericson2314): Don't do that, but intersperse children
106 # between such args.
107 echo "ld-wrapper: Warning: Potentially changing link order" >&2
108 trailingInputs+=("${norm[0]}" "${norm[1]}")
109 norm=("${norm[@]:2}")
111 -reexport_library | -weak_library)
112 childrenInputs+=("${norm[0]}" "${norm[1]}")
113 if [[ "${norm[1]}" != "$lastLeaf" ]]; then
114 leafCount+=1
115 lastLeaf="${norm[1]}"
117 norm=("${norm[@]:2}")
119 *.so | *.dylib)
120 childrenInputs+=(-reexport_library "${norm[0]}")
121 if [[ "${norm[0]}" != "$lastLeaf" ]]; then
122 leafCount+=1
123 lastLeaf="${norm[0]}"
125 norm=("${norm[@]:1}")
127 *.o | *.a)
128 # Don't delegate object files or static libs
129 parentArgs+=("${norm[0]}")
130 norm=("${norm[@]:1}")
133 if [[ -f "${norm[0]}" ]]; then
134 # Propabably a non-standard object file. We'll let it by.
135 parentArgs+=("${norm[0]}")
136 norm=("${norm[@]:1}")
137 else
138 echo "ld-wrapper: Internal Error: Invalid normalized argument" >&2
139 exit 255
142 esac
143 done
147 if (( "$leafCount" <= "$recurThreshold" )); then
148 if [ -n "${NIX_DEBUG:-}" ]; then
149 echo "ld-wrapper: Only ${leafCount} *dynamic* inputs counted while ${recurThreshold} is the ceiling, linking normally. " >&2
151 PATH="$path_backup"
152 exec @prog@ "${origArgs[@]}"
157 if [ -n "${NIX_DEBUG:-}" ]; then
158 echo "ld-wrapper: ${leafCount} *dynamic* inputs counted when ${recurThreshold} is the ceiling, delegating to children. " >&2
161 declare -r outputNameLibless=$( \
162 if [[ -z "${outputName:+isUndefined}" ]]; then
163 echo unnamed
164 return 0;
166 baseName=$(basename ${outputName})
167 if [[ "$baseName" = lib* ]]; then
168 baseName="${baseName:3}"
170 echo "$baseName")
172 declare -ra children=(
173 "$outputNameLibless-reexport-delegate-0"
174 "$outputNameLibless-reexport-delegate-1"
177 mkdir -p "$out/lib"
179 symbolBloatObject=$outputNameLibless-symbol-hack.o
180 if [[ ! -f $symbolBloatObject ]]; then
181 # `-Q` means use GNU Assembler rather than Clang, avoiding an awkward
182 # dependency cycle.
183 printf '.private_extern _______child_hack_foo\nchild_hack_foo:\n' |
184 PATH="$PATH:@out@/bin" @targetPrefix@as -Q -- -o $symbolBloatObject
187 # Split inputs between children
188 declare -a child0Inputs=() child1Inputs=("${childrenInputs[@]}")
189 let "countFirstChild = $leafCount / 2" || true
190 lastLeaf=''
191 while (( "$countFirstChild" )); do
192 case "${child1Inputs[0]}" in
193 -reexport_library | -weak_library)
194 child0Inputs+=("${child1Inputs[0]}" "${child1Inputs[1]}")
195 if [[ "${child1Inputs[1]}" != "$lastLeaf" ]]; then
196 let countFirstChild-=1 || true
197 lastLeaf="${child1Inputs[1]}"
199 child1Inputs=("${child1Inputs[@]:2}")
201 *.so | *.dylib)
202 child0Inputs+=(-reexport_library "${child1Inputs[0]}")
203 if [[ "${child1Inputs[0]}" != "$lastLeaf" ]]; then
204 let countFirstChild-=1 || true
205 lastLeaf="${child1Inputs[1]}"
207 child1Inputs=("${child1Inputs[@]:2}")
210 echo "ld-wrapper: Internal Error: Invalid delegated input" >&2
211 exit -1
213 esac
214 done
217 # First half of libs
218 @out@/bin/@targetPrefix@ld \
219 -macosx_version_min $MACOSX_DEPLOYMENT_TARGET -arch x86_64 -dylib \
220 -o "$out/lib/lib${children[0]}.dylib" \
221 -install_name "$out/lib/lib${children[0]}.dylib" \
222 "$symbolBloatObject" "${child0Inputs[@]}" "${trailingInputs[@]}"
224 # Second half of libs
225 @out@/bin/@targetPrefix@ld \
226 -macosx_version_min $MACOSX_DEPLOYMENT_TARGET -arch x86_64 -dylib \
227 -o "$out/lib/lib${children[1]}.dylib" \
228 -install_name "$out/lib/lib${children[1]}.dylib" \
229 "$symbolBloatObject" "${child1Inputs[@]}" "${trailingInputs[@]}"
231 parentArgs+=("-L$out/lib" -rpath "$out/lib")
232 if [[ $outputName != *reexport-delegate* ]]; then
233 parentArgs+=("-l${children[0]}" "-l${children[1]}")
234 else
235 parentArgs+=("-reexport-l${children[0]}" "-reexport-l${children[1]}")
238 parentArgs+=("${trailingInputs[@]}")
240 if [ -n "${NIX_DEBUG:-}" ]; then
241 echo "flags using delegated children to @prog@:" >&2
242 printf " %q\n" "${parentArgs[@]}" >&2
245 PATH="$path_backup"
246 exec @prog@ "${parentArgs[@]}"