Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions toolchain/BUILD.toolchain.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ filegroup(
name = "internal-use-wrapped-tools",
srcs = [
"%{wrapper_bin_prefix}cc_wrapper.sh",
"%{wrapper_bin_prefix}cc_wrapper_inner.sh",
],
visibility = ["//visibility:private"],
)
Expand Down
137 changes: 16 additions & 121 deletions toolchain/cc_wrapper.sh.tpl
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/bin/bash
#!/bin/sh
#
# Copyright 2021 The Bazel Authors. All rights reserved.
#
Expand All @@ -14,128 +14,23 @@
# See the License for the specific language governing permissions and
# limitations under the License.

# shellcheck disable=SC1083
SCRIPT_DIR=$(dirname "$0")

set -euo pipefail
# Search for `bash` on the system, and then execute cc_wrapper_inner.sh with
# it.

CLEANUP_FILES=()
# Attempt #1: /bin/bash -- present on FHS-compliant systems, but notably absent
# on others, including NixOS.
test -e /bin/bash && exec /bin/bash "${SCRIPT_DIR}"/cc_wrapper_inner.sh "$@"

function cleanup() {
if [[ ${#CLEANUP_FILES[@]} -gt 0 ]]; then
rm -f "${CLEANUP_FILES[@]}"
fi
}
# Attempt #2: /usr/bin/env bash -- /usr/bin/env is required by POSIX, but some
# callers to the LLVM toolchain, such as rules_rust, clear $PATH and leave
# nothing for /usr/bin/env to search for.
test -e /usr/bin/env && test /usr/bin/env bash true &&
exec /usr/bin/env bash "${SCRIPT_DIR}"/cc_wrapper_inner.sh "$@"

trap cleanup EXIT
# Attempt #3: Try `command -v`.
command -v bash && exec $(command -v bash) "${SCRIPT_DIR}"/cc_wrapper_inner.sh "$@"

# See note in toolchain/internal/configure.bzl where we define
# `wrapper_bin_prefix` for why this wrapper is needed.

# this script is located at either
# - <execroot>/external/<repo_name>/bin/cc_wrapper.sh
# - <runfiles>/<repo_name>/bin/cc_wrapper.sh
# The clang is located at
# - <execroot>/external/<repo_name2>/bin/clang
# - <runfiles>/<repo_name2>/bin/clang
#
# In both cases, getting to clang can be done via
# Finding the current dir of this script,
# - <execroot>/external/<repo_name>/bin/
# - <runfiles>/<repo_name>/bin/
# going back 2 directories
# - <execroot>/external
# - <runfiles>
#
# Going into %{toolchain_path_prefix} without the `external/` prefix + `bin/clang`
#

dirname_shim() {
local path="$1"

# Remove trailing slashes
path="${path%/}"

# If there's no slash, return "."
if [[ "${path}" != */* ]]; then
echo "."
return
fi

# Remove the last component after the final slash
path="${path%/*}"

# If it becomes empty, it means root "/"
echo "${path:-/}"
}

script_dir=$(dirname_shim "${BASH_SOURCE[0]}")
toolchain_path_prefix="%{toolchain_path_prefix}"

# Sometimes this path may be an absolute path in which case we dont do anything because
# This is using the host toolchain to build.
if [[ ${toolchain_path_prefix} != /* ]]; then
toolchain_path_prefix="${script_dir}/../../${toolchain_path_prefix#external/}"
fi

if [[ ! -f ${toolchain_path_prefix}bin/clang ]]; then
echo >&2 "ERROR: could not find clang; PWD=\"${PWD}\"; PATH=\"${PATH}\"; toolchain_path_prefix=${toolchain_path_prefix}."
exit 5
fi

OUTPUT=

function parse_option() {
local -r opt="$1"
if [[ "${OUTPUT}" = "1" ]]; then
OUTPUT=${opt}
elif [[ "${opt}" = "-o" ]]; then
# output is coming
OUTPUT=1
fi
}

function sanitize_option() {
local -r opt=$1
if [[ ${opt} == */cc_wrapper.sh ]]; then
printf "%s" "${toolchain_path_prefix}bin/clang"
elif [[ ${opt} =~ ^-fsanitize-(ignore|black)list=[^/] ]] && [[ ${script_dir} == /* ]]; then
# shellcheck disable=SC2206
parts=(${opt/=/ }) # Split flag name and value into array.
printf "%s" "${parts[0]}=${script_dir}/../../../${parts[1]}"
else
printf "%s" "${opt}"
fi
}

cmd=()
for ((i = 0; i <= $#; i++)); do
if [[ ${!i} == @* && -r "${i:1}" ]]; then
# Create a new, sanitized file.
tmpfile=$(mktemp)
CLEANUP_FILES+=("${tmpfile}")
while IFS= read -r opt; do
opt="$(
set -e
sanitize_option "${opt}"
)"
parse_option "${opt}"
echo "${opt}" >>"${tmpfile}"
done <"${!i:1}"
cmd+=("@${tmpfile}")
else
opt="$(
set -e
sanitize_option "${!i}"
)"
parse_option "${opt}"
cmd+=("${opt}")
fi
done

# Call the C++ compiler.
"${cmd[@]}"

# Generate an empty file if header processing succeeded.
if [[ "${OUTPUT}" == *.h.processed ]]; then
echo -n >"${OUTPUT}"
fi
echo >&2 'Failed to find bash at /bin/bash or in PATH.'
exit 1
158 changes: 158 additions & 0 deletions toolchain/cc_wrapper_inner.sh.tpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,158 @@
#!/bin/sh
#
# 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.

# shellcheck disable=SC1083

set -euo

cleanup() {
while read -r f; do
rm -f "${f}"
done <"${CLEANUP_FILES}"
}

CLEANUP_FILES=""

trap cleanup EXIT

# See note in toolchain/internal/configure.bzl where we define
# `wrapper_bin_prefix` for why this wrapper is needed.

# this script is located at either
# - <execroot>/external/<repo_name>/bin/cc_wrapper.sh
# - <runfiles>/<repo_name>/bin/cc_wrapper.sh
# The clang is located at
# - <execroot>/external/<repo_name2>/bin/clang
# - <runfiles>/<repo_name2>/bin/clang
#
# In both cases, getting to clang can be done via
# Finding the current dir of this script,
# - <execroot>/external/<repo_name>/bin/
# - <runfiles>/<repo_name>/bin/
# going back 2 directories
# - <execroot>/external
# - <runfiles>
#
# Going into %{toolchain_path_prefix} without the `external/` prefix + `bin/clang`
#

dirname_shim() {
path="$1"

# Remove trailing slashes
path="${path%/}"

# If there's no slash, return "."
if [ "${path}" != "*/*" ]; then
echo "."
return
fi

# Remove the last component after the final slash
path="${path%/*}"

# If it becomes empty, it means root "/"
echo "${path:-/}"
}

script_dir=$(dirname_shim "$0")
toolchain_path_prefix="%{toolchain_path_prefix}"

# Sometimes this path may be an absolute path in which case we dont do anything because
# This is using the host toolchain to build.
case "${toolchain_path_prefix}" in
/*) ;;
*) toolchain_path_prefix="${script_dir}/../../${toolchain_path_prefix#external/}" ;;
esac

if [ ! -f "${toolchain_path_prefix}bin/clang" ]; then
echo >&2 "ERROR: could not find clang; PWD=\"${PWD}\"; PATH=\"${PATH}\"; toolchain_path_prefix=${toolchain_path_prefix}."
exit 5
fi

OUTPUT=

parse_option() {
po_opt="$1"
if [ "${OUTPUT}" = "1" ]; then
OUTPUT=${po_opt}
elif [ "${po_opt}" = "-o" ]; then
# output is coming
OUTPUT=1
fi
}

sanitize_option() {
so_opt="$1"
case ${so_opt} in
*/cc_wrapper.sh) printf "%s" "${toolchain_path_prefix}bin/clang" ;;
*)
if eval "case ${so_opt} in -fsanitize-ignorelist=*|-fsanitize-blacklist=*) [ ${script_dir} == /* ] ;; esac"; then
# Split flag name and value.
#
# shellcheck disable=SC2206
part0=$(echo "${so_opt}" | cut -d '=' -f 1)
part1=$(echo "${so_opt}" | cut -d '=' -f 2)
printf "%s" "${part0}=${script_dir}/../../../${part1}"
else
printf "%s" "${so_opt}"
fi
;;
esac
}

COUNT=$#
i=0
while [ "${i}" -le "${COUNT}" ]; do
temp=""
eval "temp=\${${i}}"
substr="${temp#?}"
if eval "case ${temp} in @*) [ -r \"{substr}\" ] ;; esac"; then
# Create a new, sanitized file.
tmpfile=$(mktemp)
# POSIX shell does not support arrays, so we write the cleanup files as an
# array-separated list. We do not need to worry about spaces in filenames,
# because `mktemp` cannot use them when using the default template.
CLEANUP_FILES="${CLEANUP_FILES} ${tmpfile}"
while IFS= read -r opt; do
opt="$(
set -e
sanitize_option "${opt}"
)"
parse_option "${opt}"
echo "${opt}" >>"${tmpfile}"
done <"${substr}"
cmd="${cmd} ${tmpfile}"
else
opt="$(
set -e
sanitize_option "${temp}"
)"
parse_option "${opt}"
# The items within $cmd also cannot contain spaces, because of how
# `sanitize_option` behaves.
cmd="${cmd} ${opt}"
fi
i=$((i + 1))
done

# Call the C++ compiler.
eval \""${cmd}"\"

# Generate an empty file if header processing succeeded.
if [ "${OUTPUT}" = "*.h.processed" ]; then
true >"${OUTPUT}"
fi
11 changes: 11 additions & 0 deletions toolchain/internal/configure.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,17 @@ def llvm_config_impl(rctx):
},
)

# Inner CC wrapper script (redirect used for shell compatibility on Linux
# platforms).
if os != "darwin":
rctx.template(
"bin/cc_wrapper_inner.sh",
rctx.attr._cc_wrapper_inner_sh_tpl,
{
"%{toolchain_path_prefix}": llvm_dist_path_prefix,
},
)

if hasattr(rctx, "repo_metadata"):
return rctx.repo_metadata(reproducible = True)
else:
Expand Down
3 changes: 3 additions & 0 deletions toolchain/internal/repo.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -322,6 +322,9 @@ llvm_config_attrs.update({
"_cc_wrapper_sh_tpl": attr.label(
default = "//toolchain:cc_wrapper.sh.tpl",
),
"_cc_wrapper_inner_sh_tpl": attr.label(
default = "//toolchain:cc_wrapper_inner.sh.tpl",
),
})

def llvm_repo_impl(rctx):
Expand Down
Loading