summaryrefslogtreecommitdiff
path: root/wrappers/shadow-cc.sh
blob: 586c9d52a140f955fdda6e46bb350e7d4e76dd64 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
#!/bin/bash

set -euf -o pipefail

top="#TOP#"
shadow_top="#SHADOW_TOP#"

orig_cc="#ORIG_TOOL#"
shadow_cc="#SHADOW_TOOL#"

errors=()
warnings=()
skips=()

mode=()
target=()
opt_level=()
lang=()
stdin_input=()

orig_out=()
shadow_out=()
orig_input=()
shadow_input=()
orig_opts=()
shadow_opts=()

dump_log ()
{
    (
    set +u
    IFS=";"
    cat <<EOF
errors=(${errors[@]})
warnings=(${warnings[@]})
skips=(${skips[@]})
mode=(${mode[@]})
target=(${target[@]})
opt_level=(${opt_level[@]})
lang=(${lang[@]})
stdin_input=(${stdin_input[@]})
orig_out=(${orig_out[@]})
shadow_out=(${shadow_out[@]})
orig_input=(${orig_input[@]})
shadow_input=(${shadow_input[@]})
orig_opts=($0 ${orig_opts[@]})
shadow_opts=(${shadow_opts[@]})

EOF
    )
}

report_errors () {
    res=$?
    if [ $res != 0 ]; then
	errors+=("wrapper failed with $res")
    fi
    if [ ${#errors[@]} != 0 ] \
	   || [ ${#warnings[@]} != 0 ] \
	   || [ ${#skips[@]} != 0 ]; then
	(
	    /usr/bin/flock -e 9

	    if [ ${#errors[@]} != 0 ]; then
		log=$shadow_top.errors
	    elif [ ${#warnings[@]} != 0 ]; then
		log=$shadow_top.warnings
	    else
		log=$shadow_top.skips
	    fi
	    dump_log >>$log
	) 9>>$shadow_top.lock
    fi

    # Log code-size
    if [ ${#skips[@]} = 0 ] && [ -x "${orig_out[*]}" ]; then
	if [ -x "${shadow_out[*]}" ]; then
	    text=$(/usr/bin/size "${shadow_out[*]}" | tail -n1 | awk '{ print $1 }')
	else
	    text="-1"
	fi
	(
	    /usr/bin/flock -e 9
	    echo "${orig_out[*]},binary,$text" >> $shadow_top.size
	) 9>>$shadow_top.size
    fi
}

trap report_errors EXIT

while [ "$#" -gt 0 ]; do
    opt="$1"
    shift

    orig_opts+=("$opt")

    case "$opt" in
	"-c"|"-S"|"-E") mode+=("$opt") ;;
	"-o")
	    if [ "$#" -gt 0 ]; then
		orig_out=("$1" "${orig_out[@]}")
		orig_opts+=("$1")
		shift
	    else
		errors+=("$opt with no argument")
	    fi
	    ;;
	"-O"*)
	    opt_level+=("$opt")
	    ;;
	"-target")
	    if [ "$#" -gt 0 ]; then
		target=("$1" "${target[@]}")
		orig_opts+=("$1")
		shift
	    else
		errors+=("$opt with no argument")
	    fi
	    ;;
	"-x")
	    if [ "$#" -gt 0 ]; then
		lang+=("$1")
		orig_opts+=("$1")
		shift
	    else
		errors+=("$opt with no argument")
	    fi
	    ;;
	"-isystem"|"--sysroot"|"-MF"|"-MQ"|"-I"|"-include"|"-z"|"-D"|"-iquote")
	    if [ "$#" -gt 0 ]; then
		orig_opts+=("$1")
		shift
	    else
		errors+=("$opt with no argument")
	    fi
	    ;;
	"-")
	    orig_input+=("$opt")
	    stdin_input+=("$opt")
	    ;;
	"-"*)
	    ;;
	*)
	    orig_input+=("$opt")
	    ;;
    esac
done

if [ ${#mode[@]} = 0 ]; then
    # Link mode
    mode=("-L")
elif [ ${#mode[@]} != 1 ]; then
    warnings+=("Several modes: ${mode[*]}")
fi

if [ ${#stdin_input[@]} = 1 ]; then
    skips+=("Stdin input")
    if [ x"${orig_input[*]}" != x"${stdin_input[*]}" ]; then
	errors+=("Extra inputs in presence of stdin: ${orig_input[*]}")
    fi
    if [ x"${mode[*]}" != x"-E" ]; then
	warnings+=("Stdin input with mode: ${mode[*]}")
    fi
elif [ ${#stdin_input[@]} -gt 1 ]; then
    errors+=("Several stdin inputs")
fi

if [ ${#lang[@]} -gt 1 ]; then
    warnings+=("Several -x LANG options: ${lang[*]}")
fi

if [ ${#opt_level[@]} -gt 1 ]; then
    warnings+=("Several opt_levels: ${opt_level[*]}")
fi

if [ x"${mode[*]}" != x"-L" ]; then
    if [ ${#orig_input[@]} != 1 ]; then
	errors+=("Several inputs with mode ${mode[*]}: ${orig_input[*]}")
    fi
fi

if [ ${#orig_out[@]} = 0 ]; then
    skips+=("No orig_out")
elif [ ${#orig_out[@]} != 1 ]; then
    errors+=("Several orig_outs: ${orig_out[*]}")
fi

if [ "${#target[@]}" -gt 1 ]; then
    errors+=("Several targets: ${target[*]}")
fi

if ! echo "${target[*]}" | grep -e "^aarch64" >/dev/null; then
    skips+=("Skipping target: ${target[*]}")
fi

if [ ${#errors[@]} = 0 ] && [ ${#skips[@]} != 0 ] \
       && [ ${#orig_out[@]} = 0 ] && [ ${#stdin_input[@]} != 0 ]; then
    # Bionic has several cases where orig_cc [expectedly?] fails.
    # Ignore these.
    report_errors
    exec "$orig_cc" "${orig_opts[@]}"
fi

print_shadow_path ()
{
    local path="$1"
    local prefix="${2-}"

    case "$path" in
	"/dev/null") echo "$path"; return ;;
	"/"*) errors+=("Absolute path: $path") ;;
    esac

    # Check that we are under $top
    case "$(pwd -P | sed -e "s#^$top##")" in
	"/"*) errors+=("Running outside of $top") ;;
    esac

    echo "$(pwd -P)/$path" | sed -e "s#$top#$shadow_top${prefix}#"
}

generate_shadow_rsp ()
{
    local orig_rsp="$1"

    local shadow_rsp orig_file shadow_file

    shadow_rsp=$(print_shadow_path "$orig_rsp")
    mkdir -p "$(dirname "$shadow_rsp")"
    rm -f "$shadow_rsp"

    if [ "$(stat --format=%s "$orig_rsp")" = 0 ]; then
	touch "$shadow_rsp"
    else
	local quote delim=""
	while read -d '' -r orig_file; do
            # unquote orig
            case "$orig_file" in
                \'*\') quote="'"; orig_file=${orig_file:1:-1} ;;
                *) quote="" ;;
            esac
	    shadow_file=$(print_shadow_path "$orig_file")
	    if ! [ -e "$shadow_file" ]; then
		shadow_file="$orig_file"
	    fi
            # quote shadow
	    echo -ne "$delim$quote$shadow_file$quote" >> "$shadow_rsp"
	    delim=" "
	done < <({ cat "$orig_rsp"; echo; } | xargs printf '%s\0')
    fi

    echo "$shadow_rsp"
}

make_shadow_opts ()
{
    local opt
    local shadow_file

    while [ "$#" -gt 0 ]; do
	opt="$1"
	shift

	case "$opt" in
	    "-o")
		shadow_opts+=("$opt")
		opt=$(print_shadow_path "$1")
		shadow_out+=("$opt")
		shift
		;;
	    "-fuse-ld=lld")
		opt="-fuse-ld=$(dirname "$shadow_cc")/ld.lld"
		;;
	    "-target"|"-x"|"-isystem"|"--sysroot"|"-MF"|"-MQ"|"-I"|"-include"|"-z"|"-D"|"-iquote")
		shadow_opts+=("$opt")
		opt="$1"
		shift
		;;
	    "-Werror"*) continue ;;
	    "-")
		errors+=("Stdin input should have been skipped")
		;;
	    "-"*) ;;
	    "@"*".rsp")
		opt="${opt#@}"
		if [ x"$(basename "$opt" .rsp)" = x"$(basename "${orig_out[*]}")" ]; then
		    opt=$(generate_shadow_rsp "$opt")
		else
		    errors+=("RSP file $opt does not match orig_out ${orig_out[*]}")
		fi
		opt="@$opt"
		;;
	    "@"*)
		errors+=("Unexpected option: $opt")
		opt="${opt#@}"
		# shellcheck disable=SC2046
		make_shadow_opts $(cat "$opt")
		continue
		;;
	    *)
		shadow_file=$(print_shadow_path "$opt")
		if [ -e "$shadow_file" ]; then
		    opt="$shadow_file"
		    shadow_input+=("$shadow_file")
		fi
		;;
	esac
	shadow_opts+=("$opt")
    done
}

if [ ${#errors[@]} = 0 ] && [ ${#skips[@]} = 0 ]; then
    make_shadow_opts "${orig_opts[@]}"
fi

if [ ${#errors[@]} = 0 ] && [ ${#skips[@]} = 0 ]; then
    shadow_log=$(print_shadow_path "${orig_out[*]}" "-logs")

    mkdir -p "$(dirname "${shadow_out[*]}")"
    mkdir -p "$(dirname "$shadow_log")"

    dump_log > "$shadow_log"
    echo "$shadow_cc" "${shadow_opts[@]}" >> "$shadow_log"
    "$shadow_cc" "${shadow_opts[@]}" >> "$shadow_log" 2>&1 &
    shadow_res=0 && wait $! || shadow_res=$?

    if [ $shadow_res != 0 ]; then
	errors+=("shadow_cc failed with $shadow_res")
    elif ! [ -e "${shadow_out[*]}" ]; then
	errors+=("shadow_cc failed to generate output")
    elif [ x"${shadow_out[*]}" != x"/dev/null" ]; then
	shadow_link=$(IFS=""; print_shadow_path "${orig_out[*]}" "-${target[*]-default}${mode[*]}")
	mkdir -p "$(dirname "$shadow_link")"
	ln -s -f "${shadow_out[*]}" "$shadow_link"
    fi
fi

#CCACHE# "$orig_cc" "${orig_opts[@]}" &
orig_res=0 && wait $! || orig_res=$?

if [ $orig_res != 0 ]; then
    errors+=("orig_cc failed with $orig_res")
fi

exit $orig_res