1
- #! /bin/bash
1
+ #! /bin/sh
2
2
#
3
3
# Copyright 2021 The Bazel Authors. All rights reserved.
4
4
#
16
16
17
17
# shellcheck disable=SC1083
18
18
19
- set -euo pipefail
19
+ set -euo
20
20
21
- CLEANUP_FILES=()
22
-
23
- function cleanup() {
24
- if [[ ${# CLEANUP_FILES[@]} -gt 0 ]]; then
25
- rm -f " ${CLEANUP_FILES[@]} "
26
- fi
21
+ cleanup () {
22
+ while read -r f; do
23
+ rm -f " ${f} "
24
+ done < " ${CLEANUP_FILES} "
27
25
}
28
26
27
+ CLEANUP_FILES=" "
28
+
29
29
trap cleanup EXIT
30
30
31
31
# See note in toolchain/internal/configure.bzl where we define
@@ -50,13 +50,13 @@ trap cleanup EXIT
50
50
#
51
51
52
52
dirname_shim () {
53
- local path=" $1 "
53
+ path=" $1 "
54
54
55
55
# Remove trailing slashes
56
56
path=" ${path%/ } "
57
57
58
58
# If there's no slash, return "."
59
- if [[ " ${path} " != * /* ] ]; then
59
+ if [ " ${path} " != " */*" ]; then
60
60
echo " ."
61
61
return
62
62
fi
@@ -68,74 +68,91 @@ dirname_shim() {
68
68
echo " ${path:-/ } "
69
69
}
70
70
71
- script_dir=$( dirname_shim " ${BASH_SOURCE[0]} " )
71
+ script_dir=$( dirname_shim " $0 " )
72
72
toolchain_path_prefix=" %{toolchain_path_prefix}"
73
73
74
74
# Sometimes this path may be an absolute path in which case we dont do anything because
75
75
# This is using the host toolchain to build.
76
- if [[ ${toolchain_path_prefix} != /* ]]; then
77
- toolchain_path_prefix=" ${script_dir} /../../${toolchain_path_prefix# external/ } "
78
- fi
76
+ case " ${toolchain_path_prefix} " in
77
+ /* ) ;;
78
+ * ) toolchain_path_prefix=" ${script_dir} /../../${toolchain_path_prefix# external/ } " ;;
79
+ esac
79
80
80
- if [[ ! -f ${toolchain_path_prefix} bin/clang ] ]; then
81
+ if [ ! -f " ${toolchain_path_prefix} bin/clang" ]; then
81
82
echo >&2 " ERROR: could not find clang; PWD=\" ${PWD} \" ; PATH=\" ${PATH} \" ; toolchain_path_prefix=${toolchain_path_prefix} ."
82
83
exit 5
83
84
fi
84
85
85
86
OUTPUT=
86
87
87
- function parse_option() {
88
- local -r opt =" $1 "
89
- if [[ " ${OUTPUT} " = " 1" ] ]; then
90
- OUTPUT=${opt }
91
- elif [[ " ${opt } " = " -o" ] ]; then
88
+ parse_option () {
89
+ po_opt =" $1 "
90
+ if [ " ${OUTPUT} " = " 1" ]; then
91
+ OUTPUT=${po_opt }
92
+ elif [ " ${po_opt } " = " -o" ]; then
92
93
# output is coming
93
94
OUTPUT=1
94
95
fi
95
96
}
96
97
97
- function sanitize_option() {
98
- local -r opt=$1
99
- if [[ ${opt} == * /cc_wrapper.sh ]]; then
100
- printf " %s" " ${toolchain_path_prefix} bin/clang"
101
- elif [[ ${opt} =~ ^-fsanitize-(ignore| black)list= [^/] ]] && [[ ${script_dir} == /* ]]; then
102
- # shellcheck disable=SC2206
103
- parts=(${opt/ =/ } ) # Split flag name and value into array.
104
- printf " %s" " ${parts[0]} =${script_dir} /../../../${parts[1]} "
105
- else
106
- printf " %s" " ${opt} "
107
- fi
98
+ sanitize_option () {
99
+ so_opt=" $1 "
100
+ case ${so_opt} in
101
+ * /cc_wrapper.sh) printf " %s" " ${toolchain_path_prefix} bin/clang" ;;
102
+ * )
103
+ if eval " case ${so_opt} in -fsanitize-ignorelist=*|-fsanitize-blacklist=*) [ ${script_dir} == /* ] ;; esac" ; then
104
+ # Split flag name and value.
105
+ #
106
+ # shellcheck disable=SC2206
107
+ part0=$( echo " ${so_opt} " | cut -d ' =' -f 1)
108
+ part1=$( echo " ${so_opt} " | cut -d ' =' -f 2)
109
+ printf " %s" " ${part0} =${script_dir} /../../../${part1} "
110
+ else
111
+ printf " %s" " ${so_opt} "
112
+ fi
113
+ ;;
114
+ esac
108
115
}
109
116
110
- cmd=()
111
- for (( i = 0 ; i <= $# ; i++ )) ; do
112
- if [[ ${! i} == @* && -r " ${i: 1} " ]]; then
117
+ COUNT=$#
118
+ i=0
119
+ while [ " ${i} " -le " ${COUNT} " ]; do
120
+ temp=" "
121
+ eval " temp=\$ {${i} }"
122
+ substr=" ${temp# ?} "
123
+ if eval " case ${temp} in @*) [ -r \" {substr}\" ] ;; esac" ; then
113
124
# Create a new, sanitized file.
114
125
tmpfile=$( mktemp)
115
- CLEANUP_FILES+=(" ${tmpfile} " )
126
+ # POSIX shell does not support arrays, so we write the cleanup files as an
127
+ # array-separated list. We do not need to worry about spaces in filenames,
128
+ # because `mktemp` cannot use them when using the default template.
129
+ CLEANUP_FILES=" ${CLEANUP_FILES} ${tmpfile} "
116
130
while IFS= read -r opt; do
117
131
opt=" $(
118
132
set -e
119
133
sanitize_option " ${opt} "
120
134
) "
121
135
parse_option " ${opt} "
122
136
echo " ${opt} " >> " ${tmpfile} "
123
- done < " ${! i : 1 } "
124
- cmd+=( " @ ${ tmpfile}" )
137
+ done < " ${substr } "
138
+ cmd= " ${cmd} ${ tmpfile}"
125
139
else
126
140
opt=" $(
127
141
set -e
128
- sanitize_option " ${! i } "
142
+ sanitize_option " ${temp } "
129
143
) "
130
144
parse_option " ${opt} "
131
- cmd+=(" ${opt} " )
145
+ # The items within $cmd also cannot contain spaces, because of how
146
+ # `sanitize_option` behaves.
147
+ cmd=" ${cmd} ${opt} "
132
148
fi
149
+ i=$(( i + 1 ))
133
150
done
134
151
135
152
# Call the C++ compiler.
136
- " ${cmd[@]} "
153
+ eval \" " ${cmd} " \ "
137
154
138
155
# Generate an empty file if header processing succeeded.
139
- if [[ " ${OUTPUT} " == * .h.processed ] ]; then
140
- echo -n > " ${OUTPUT} "
156
+ if [ " ${OUTPUT} " = " *.h.processed" ]; then
157
+ true > " ${OUTPUT} "
141
158
fi
0 commit comments