lol
1#! @shell@
2
3set -eu -o pipefail
4
5path_backup="$PATH"
6if [ -n "@coreutils_bin@" ]; then
7 PATH="@coreutils_bin@/bin"
8fi
9
10declare -r recurThreshold=300
11
12declare overflowCount=0
13for ((n=0; n < $#; ++n)); do
14 case "${!n}" in
15 -l*) let overflowCount+=1 ;;
16 -reexport-l*) let overflowCount+=1 ;;
17 *) ;;
18 esac
19done
20
21declare -a allArgs=()
22
23if (( "$overflowCount" <= "$recurThreshold" )); then
24 allArgs=("$@")
25else
26 declare -a childrenLookup=() childrenLink=()
27
28 while (( $# )); do
29 case "$1" in
30 -L/*)
31 childrenLookup+=("$1")
32 allArgs+=("$1")
33 ;;
34 -L)
35 echo "cctools LD does not support '-L foo' or '-l foo'" >&2
36 exit 1
37 ;;
38 -l)
39 echo "cctools LD does not support '-L foo' or '-l foo'" >&2
40 exit 1
41 ;;
42 -lazy_library | -lazy_framework | -lto_library)
43 # We aren't linking any "azy_library", "to_library", etc.
44 allArgs+=("$1")
45 ;;
46 -lazy-l | -weak-l) allArgs+=("$1") ;;
47 # We can't so easily prevent header issues from these.
48 -lSystem) allArgs+=("$1") ;;
49 # Special case as indirection seems like a bad idea for something
50 # so fundamental. Can be removed for simplicity.
51 -l?* | -reexport-l?*) childrenLink+=("$1") ;;
52 *) allArgs+=("$1") ;;
53 esac
54
55 shift
56 done
57
58 declare n=0
59 while (( $n < "${#childrenLink[@]}" )); do
60 if [[ "${childrenLink[n]}" = -l* ]]; then
61 childrenLink[n]="-reexport${childrenLink[n]}"
62 fi
63 let ++n
64 done
65 unset n
66
67 declare -r outputNameLibless=$(basename $( \
68 if [[ -z "${outputName:+isUndefined}" ]]; then
69 echo unnamed
70 elif [[ "${outputName:0:3}" = lib ]]; then
71 echo "${outputName:3}"
72 else
73 echo "${outputName}"
74 fi))
75 declare -ra children=("$outputNameLibless-reexport-delegate-0" \
76 "$outputNameLibless-reexport-delegate-1")
77
78 mkdir -p "$out/lib"
79
80 PATH="$PATH:@out@/bin"
81
82 symbolBloatObject=$outputNameLibless-symbol-hack.o
83 if [[ ! -e $symbolBloatObject ]]; then
84 printf '.private_extern _______child_hack_foo\nchild_hack_foo:\n' \
85 | @binPrefix@as -- -o $symbolBloatObject
86 fi
87
88 # first half of libs
89 @binPrefix@ld -macosx_version_min $MACOSX_DEPLOYMENT_TARGET -arch x86_64 -dylib \
90 -o "$out/lib/lib${children[0]}.dylib" \
91 -install_name "$out/lib/lib${children[0]}.dylib" \
92 "${childrenLookup[@]}" "$symbolBloatObject" \
93 "${childrenLink[@]:0:$((${#childrenLink[@]} / 2 ))}"
94
95 # second half of libs
96 @binPrefix@ld -macosx_version_min $MACOSX_DEPLOYMENT_TARGET -arch x86_64 -dylib \
97 -o "$out/lib/lib${children[1]}.dylib" \
98 -install_name "$out/lib/lib${children[1]}.dylib" \
99 "${childrenLookup[@]}" "$symbolBloatObject" \
100 "${childrenLink[@]:$((${#childrenLink[@]} / 2 ))}"
101
102 allArgs+=("-L$out/lib" "-l${children[0]}" "-l${children[1]}")
103fi
104
105PATH="$path_backup"
106exec @prog@ "${allArgs[@]}"