-
-
Notifications
You must be signed in to change notification settings - Fork 220
/
cc_wrapper.sh.tpl
81 lines (73 loc) · 2.51 KB
/
cc_wrapper.sh.tpl
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
#!/bin/bash
#
# Copyright 2021 The Bazel Authors. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# OS X relpath is not really working. This is a wrapper script around gcc
# to simulate relpath behavior.
#
# This wrapper uses install_name_tool to replace all paths in the binary
# (bazel-out/.../path/to/original/library.so) by the paths relative to
# the binary. It parses the command line to behave as rpath is supposed
# to work.
#
# See https://blogs.oracle.com/dipol/entry/dynamic_libraries_rpath_and_mac
# on how to set those paths for Mach-O binaries.
# shellcheck disable=SC1083
set -euo pipefail
# See note in toolchain/internal/configure.bzl where we define
# `wrapper_bin_prefix` for why this wrapper is needed.
if [[ -f %{toolchain_path_prefix}bin/clang ]]; then
execroot_path=""
elif [[ ${BASH_SOURCE[0]} == "/"* ]]; then
# Some consumers of `CcToolchainConfigInfo` (e.g. `cmake` from rules_foreign_cc)
# change CWD and call $CC (this script) with its absolute path.
# For cases like this, we'll try to find `clang` through an absolute path.
# This script is at _execroot_/external/_repo_name_/bin/cc_wrapper.sh
execroot_path="${BASH_SOURCE[0]%/*/*/*/*}/"
else
echo >&2 "ERROR: could not find clang; PWD=\"${PWD}\"; PATH=\"${PATH}\"."
exit 5
fi
function sanitize_option() {
local -r opt=$1
if [[ ${opt} == */cc_wrapper.sh ]]; then
printf "%s" "${execroot_path}%{toolchain_path_prefix}bin/clang"
elif [[ ${opt} =~ ^-fsanitize-(ignore|black)list=[^/] ]]; then
# shellcheck disable=SC2206
parts=(${opt/=/ }) # Split flag name and value into array.
printf "%s" "${parts[0]}=${execroot_path}${parts[1]}"
else
printf "%s" "${opt}"
fi
}
cmd=()
for ((i = 0; i <= $#; i++)); do
if [[ ${!i} == @* ]]; then
while IFS= read -r opt; do
opt="$(
set -e
sanitize_option "${opt}"
)"
cmd+=("${opt}")
done <"${!i:1}"
else
opt="$(
set -e
sanitize_option "${!i}"
)"
cmd+=("${opt}")
fi
done
# Call the C++ compiler.
exec "${cmd[@]}"