25
25
# See https://blogs.oracle.com/dipol/entry/dynamic_libraries_rpath_and_mac
26
26
# on how to set those paths for Mach-O binaries.
27
27
28
- # shellcheck disable=all
28
+ # shellcheck disable=SC1083
29
29
30
- set -eu
30
+ set -euo pipefail
31
31
32
32
INSTALL_NAME_TOOL=" /usr/bin/install_name_tool"
33
33
@@ -39,29 +39,65 @@ OUTPUT=
39
39
function parse_option() {
40
40
local -r opt=" $1 "
41
41
if [[ ${OUTPUT} == " 1" ]]; then
42
- OUTPUT=$opt
43
- elif [[ $opt =~ ^-l(.* )$ ]]; then
44
- LIBS=" ${BASH_REMATCH[1]} $LIBS "
45
- elif [[ $opt =~ ^-L(.* )$ ]]; then
46
- LIB_DIRS=" ${BASH_REMATCH[1]} $LIB_DIRS "
47
- elif [[ $opt =~ ^\@ loader_path/(.* )$ ]]; then
42
+ OUTPUT=${ opt}
43
+ elif [[ ${ opt} =~ ^-l(.* )$ ]]; then
44
+ LIBS=" ${BASH_REMATCH[1]} ${ LIBS} "
45
+ elif [[ ${ opt} =~ ^-L(.* )$ ]]; then
46
+ LIB_DIRS=" ${BASH_REMATCH[1]} ${ LIB_DIRS} "
47
+ elif [[ ${ opt} =~ ^\@ loader_path/(.* )$ ]]; then
48
48
RPATHS=" ${BASH_REMATCH[1]} ${RPATHS} "
49
- elif [[ $opt =~ ^-Wl,-rpath,\@ loader_path/(.* )$ ]]; then
49
+ elif [[ ${ opt} =~ ^-Wl,-rpath,\@ loader_path/(.* )$ ]]; then
50
50
RPATHS=" ${BASH_REMATCH[1]} ${RPATHS} "
51
- elif [[ $opt == " -o" ]]; then
51
+ elif [[ ${ opt} == " -o" ]]; then
52
52
# output is coming
53
53
OUTPUT=1
54
54
fi
55
55
}
56
56
57
- # let parse the option list
58
- for i in " $@ " ; do
59
- if [[ $i == @* ]]; then
57
+ if [[ -f %{toolchain_path_prefix}bin/clang ]]; then
58
+ execroot_path=" "
59
+ elif [[ ${BASH_SOURCE[0]} == " /" * ]]; then
60
+ # Some consumers of `CcToolchainConfigInfo` (e.g. `cmake` from rules_foreign_cc)
61
+ # change CWD and call $CC (this script) with its absolute path.
62
+ # For cases like this, we'll try to find `clang` through an absolute path.
63
+ # This script is at _execroot_/external/_repo_name_/bin/cc_wrapper.sh
64
+ execroot_path=" ${BASH_SOURCE[0]%/*/*/*/* } /"
65
+ else
66
+ echo >&2 " ERROR: could not find clang; PWD=\" ${PWD} \" ; PATH=\" ${PATH} \" ."
67
+ exit 5
68
+ fi
69
+
70
+ function sanitize_option() {
71
+ local -r opt=$1
72
+ if [[ ${opt} == * /cc_wrapper.sh ]]; then
73
+ printf " %s" " ${execroot_path} %{toolchain_path_prefix}bin/clang"
74
+ elif [[ ${opt} =~ ^-fsanitize-(ignore| black)list= [^/] ]]; then
75
+ # shellcheck disable=SC2206
76
+ parts=(${opt/ =/ } ) # Split flag name and value into array.
77
+ printf " %s" " ${parts[0]} =${execroot_path}${parts[1]} "
78
+ else
79
+ printf " %s" " ${opt} "
80
+ fi
81
+ }
82
+
83
+ cmd=()
84
+ for (( i = 0 ; i <= $# ; i++ )) ; do
85
+ if [[ ${! i} == @* ]]; then
60
86
while IFS= read -r opt; do
61
- parse_option " $opt "
62
- done < " ${i: 1} " || exit 1
87
+ opt=" $(
88
+ set -e
89
+ sanitize_option " ${opt} "
90
+ ) "
91
+ parse_option " ${opt} "
92
+ cmd+=(" ${opt} " )
93
+ done < " ${! i: 1} "
63
94
else
64
- parse_option " $i "
95
+ opt=" $(
96
+ set -e
97
+ sanitize_option " ${! i} "
98
+ ) "
99
+ parse_option " ${opt} "
100
+ cmd+=(" ${opt} " )
65
101
fi
66
102
done
67
103
@@ -86,27 +122,13 @@ if [[ ":${PATH}:" != *":/usr/bin:"* ]]; then
86
122
fi
87
123
88
124
# Call the C++ compiler.
89
- if [[ -f %{toolchain_path_prefix}bin/clang ]]; then
90
- %{toolchain_path_prefix}bin/clang " $@ "
91
- elif [[ ${BASH_SOURCE[0]} == " /" * ]]; then
92
- # Some consumers of `CcToolchainConfigInfo` (e.g. `cmake` from rules_foreign_cc)
93
- # change CWD and call $CC (this script) with its absolute path.
94
- # the execroot (i.e. `cmake` from `rules_foreign_cc`) and call CC . For cases like this,
95
- # we'll try to find `clang` relative to this script.
96
- # This script is at _execroot_/external/_repo_name_/bin/cc_wrapper.sh
97
- execroot_path=" ${BASH_SOURCE[0]%/*/*/*/* } "
98
- clang=" ${execroot_path} /%{toolchain_path_prefix}bin/clang"
99
- " ${clang} " " ${@ } "
100
- else
101
- echo >&2 " ERROR: could not find clang; PWD=\" $( pwd) \" ; PATH=\" ${PATH} \" ."
102
- exit 5
103
- fi
125
+ " ${cmd[@]} "
104
126
105
127
function get_library_path() {
106
128
for libdir in ${LIB_DIRS} ; do
107
- if [ -f ${libdir} /lib$1 .so ]; then
129
+ if [[ -f " ${libdir} /lib$1 " .so ] ]; then
108
130
echo " ${libdir} /lib$1 .so"
109
- elif [ -f ${libdir} /lib$1 .dylib ]; then
131
+ elif [[ -f " ${libdir} " /lib" $1 " .dylib ] ]; then
110
132
echo " ${libdir} /lib$1 .dylib"
111
133
fi
112
134
done
@@ -116,8 +138,9 @@ function get_library_path() {
116
138
# and multi-level symlinks.
117
139
function get_realpath() {
118
140
local previous=" $1 "
119
- local next=$( readlink " ${previous} " )
120
- while [ -n " ${next} " ]; do
141
+ local next
142
+ next=" $( readlink " ${previous} " ) "
143
+ while [[ -n ${next} ]]; do
121
144
previous=" ${next} "
122
145
next=$( readlink " ${previous} " )
123
146
done
@@ -127,24 +150,25 @@ function get_realpath() {
127
150
# Get the path of a lib inside a tool
128
151
function get_otool_path() {
129
152
# the lib path is the path of the original lib relative to the workspace
130
- get_realpath $1 | sed ' s|^.*/bazel-out/|bazel-out/|'
153
+ get_realpath " $1 " | sed ' s|^.*/bazel-out/|bazel-out/|'
131
154
}
132
155
133
156
# Do replacements in the output
134
157
for rpath in ${RPATHS} ; do
135
158
for lib in ${LIBS} ; do
136
159
unset libname
137
- if [ -f " $( dirname ${OUTPUT} ) /${rpath} /lib${lib} .so" ]; then
160
+ if [[ -f " $( dirname " ${OUTPUT} " ) /${rpath} /lib${lib} .so" ] ]; then
138
161
libname=" lib${lib} .so"
139
- elif [ -f " $( dirname ${OUTPUT} ) /${rpath} /lib${lib} .dylib" ]; then
162
+ elif [[ -f " $( dirname " ${OUTPUT} " ) /${rpath} /lib${lib} .dylib" ] ]; then
140
163
libname=" lib${lib} .dylib"
141
164
fi
142
165
# ${libname-} --> return $libname if defined, or undefined otherwise. This is to make
143
166
# this set -e friendly
144
167
if [[ -n ${libname-} ]]; then
145
- libpath=$( get_library_path ${lib} )
146
- if [ -n " ${libpath} " ]; then
147
- ${INSTALL_NAME_TOOL} -change $( get_otool_path " ${libpath} " ) \
168
+ libpath=" $( get_library_path " ${lib} " ) "
169
+ if [[ -n ${libpath} ]]; then
170
+ otool_path=" $( get_otool_path " ${libpath} " ) "
171
+ " ${INSTALL_NAME_TOOL} " -change " ${otool_path} " \
148
172
" @loader_path/${rpath} /${libname} " " ${OUTPUT} "
149
173
fi
150
174
fi
0 commit comments