From 3afe5d49601093f8450c0281a031fd0d335df4d1 Mon Sep 17 00:00:00 2001 From: Troy Benson Date: Sun, 29 Jun 2025 18:08:36 +0000 Subject: [PATCH 1/3] support runtime/test-time compiler invokations This commit changes the cc_wrapper.sh script to allow for it to find paths when invoked at runtime with runfiles as apposed to strictly using build-rule invokes. --- toolchain/cc_wrapper.sh.tpl | 51 +++++++++++++++++---------------- toolchain/osx_cc_wrapper.sh.tpl | 48 ++++++++++++++++++++----------- 2 files changed, 59 insertions(+), 40 deletions(-) diff --git a/toolchain/cc_wrapper.sh.tpl b/toolchain/cc_wrapper.sh.tpl index b3cb23386..01baf6708 100644 --- a/toolchain/cc_wrapper.sh.tpl +++ b/toolchain/cc_wrapper.sh.tpl @@ -14,17 +14,6 @@ # 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 @@ -42,27 +31,41 @@ trap cleanup EXIT # 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}\"." +# this script is located at either +# - /external//bin/cc_wrapper.sh +# - //bin/cc_wrapper.sh +# The clang is located at +# - /external//bin/clang +# - //bin/clang +# +# In both cases, getting to clang can be done via +# Finding the current dir of this script, +# - /external//bin/ +# - //bin/ +# going back 2 directories +# - /external +# - +# +# Going into %{toolchain_path_prefix} without the `external/` prefix + `bin/clang` +# + +script_dir=$(dirname ${BASH_SOURCE[0]}) +toolchain_path_prefix="%{toolchain_path_prefix}" +toolchain_path_prefix="$script_dir/../../${toolchain_path_prefix#external/}" + +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 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 + 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]}=${execroot_path}${parts[1]}" + printf "%s" "${parts[0]}=$script_dir/../../../${parts[1]}" else printf "%s" "${opt}" fi diff --git a/toolchain/osx_cc_wrapper.sh.tpl b/toolchain/osx_cc_wrapper.sh.tpl index a005caa30..0acf42811 100755 --- a/toolchain/osx_cc_wrapper.sh.tpl +++ b/toolchain/osx_cc_wrapper.sh.tpl @@ -63,31 +63,47 @@ function parse_option() { fi } -if [[ -f %{toolchain_path_prefix}bin/clang ]]; then - execroot_path="" - execroot_abs_path="${PWD}/" -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]%/*/*/*/*}/" - execroot_abs_path="$(cd "${execroot_path}" && pwd -P)/" -else - echo >&2 "ERROR: could not find clang; PWD=\"${PWD}\"; PATH=\"${PATH}\"." +# 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 +# - /external//bin/cc_wrapper.sh +# - //bin/cc_wrapper.sh +# The clang is located at +# - /external//bin/clang +# - //bin/clang +# +# In both cases, getting to clang can be done via +# Finding the current dir of this script, +# - /external//bin/ +# - //bin/ +# going back 2 directories +# - /external +# - +# +# Going into %{toolchain_path_prefix} without the `external/` prefix + `bin/clang` +# + +script_dir=$(dirname ${BASH_SOURCE[0]}) +toolchain_path_prefix="%{toolchain_path_prefix}" +toolchain_path_prefix="$script_dir/../../${toolchain_path_prefix#external/}" +toolchain_path_prefix_abs="$(cd "$toolchain_path_prefix" && pwd -P)/" + +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 function sanitize_option() { local -r opt=$1 if [[ ${opt} == */cc_wrapper.sh ]]; then - printf "%s" "${execroot_path}%{toolchain_path_prefix}bin/clang" + printf "%s" "${toolchain_path_prefix}bin/clang" elif [[ ${opt} == "-fuse-ld=ld64.lld" ]]; then - echo "--ld-path=${execroot_abs_path}%{toolchain_path_prefix}bin/ld64.lld" - elif [[ ${opt} =~ ^-fsanitize-(ignore|black)list=[^/] ]]; then + echo "--ld-path=${toolchain_path_prefix_abs}bin/ld64.lld" + 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]}=${execroot_path}${parts[1]}" + printf "%s" "${parts[0]}=$script_dir/../../../${parts[1]}" else printf "%s" "${opt}" fi From 4ab67b8bcf374709a3e922132ced5baa5b07b8fe Mon Sep 17 00:00:00 2001 From: Troy Benson Date: Mon, 30 Jun 2025 12:08:26 +0000 Subject: [PATCH 2/3] ci fixes --- toolchain/cc_wrapper.sh.tpl | 28 ++++++++++++++++++++++++++-- toolchain/osx_cc_wrapper.sh.tpl | 31 ++++++++++++++++++++++++++++--- 2 files changed, 54 insertions(+), 5 deletions(-) diff --git a/toolchain/cc_wrapper.sh.tpl b/toolchain/cc_wrapper.sh.tpl index 01baf6708..f4cbbd86a 100644 --- a/toolchain/cc_wrapper.sh.tpl +++ b/toolchain/cc_wrapper.sh.tpl @@ -49,9 +49,33 @@ trap cleanup EXIT # Going into %{toolchain_path_prefix} without the `external/` prefix + `bin/clang` # -script_dir=$(dirname ${BASH_SOURCE[0]}) +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}" -toolchain_path_prefix="$script_dir/../../${toolchain_path_prefix#external/}" + +# 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}." diff --git a/toolchain/osx_cc_wrapper.sh.tpl b/toolchain/osx_cc_wrapper.sh.tpl index 0acf42811..656b1e67d 100755 --- a/toolchain/osx_cc_wrapper.sh.tpl +++ b/toolchain/osx_cc_wrapper.sh.tpl @@ -84,10 +84,35 @@ function parse_option() { # Going into %{toolchain_path_prefix} without the `external/` prefix + `bin/clang` # -script_dir=$(dirname ${BASH_SOURCE[0]}) +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}" -toolchain_path_prefix="$script_dir/../../${toolchain_path_prefix#external/}" -toolchain_path_prefix_abs="$(cd "$toolchain_path_prefix" && pwd -P)/" +# 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/}" + toolchain_path_prefix_abs="$(cd "$toolchain_path_prefix" && pwd -P)/" +else + toolchain_path_prefix_abs="$toolchain_path_prefix" +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}." From 9b288a8c62e8f7a56a40453c742d56a26b84e432 Mon Sep 17 00:00:00 2001 From: Troy Benson Date: Mon, 30 Jun 2025 14:43:47 +0000 Subject: [PATCH 3/3] fix shfmt --- toolchain/cc_wrapper.sh.tpl | 8 ++++---- toolchain/osx_cc_wrapper.sh.tpl | 12 ++++++------ 2 files changed, 10 insertions(+), 10 deletions(-) diff --git a/toolchain/cc_wrapper.sh.tpl b/toolchain/cc_wrapper.sh.tpl index f4cbbd86a..16c24eff4 100644 --- a/toolchain/cc_wrapper.sh.tpl +++ b/toolchain/cc_wrapper.sh.tpl @@ -51,12 +51,12 @@ trap cleanup EXIT dirname_shim() { local path="$1" - + # Remove trailing slashes path="${path%/}" # If there's no slash, return "." - if [[ "$path" != */* ]]; then + if [[ "${path}" != */* ]]; then echo "." return fi @@ -74,7 +74,7 @@ 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/}" + toolchain_path_prefix="${script_dir}/../../${toolchain_path_prefix#external/}" fi if [[ ! -f ${toolchain_path_prefix}bin/clang ]]; then @@ -89,7 +89,7 @@ function sanitize_option() { 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]}" + printf "%s" "${parts[0]}=${script_dir}/../../../${parts[1]}" else printf "%s" "${opt}" fi diff --git a/toolchain/osx_cc_wrapper.sh.tpl b/toolchain/osx_cc_wrapper.sh.tpl index 656b1e67d..6363eb43a 100755 --- a/toolchain/osx_cc_wrapper.sh.tpl +++ b/toolchain/osx_cc_wrapper.sh.tpl @@ -86,12 +86,12 @@ function parse_option() { dirname_shim() { local path="$1" - + # Remove trailing slashes path="${path%/}" # If there's no slash, return "." - if [[ "$path" != */* ]]; then + if [[ "${path}" != */* ]]; then echo "." return fi @@ -108,10 +108,10 @@ 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/}" - toolchain_path_prefix_abs="$(cd "$toolchain_path_prefix" && pwd -P)/" + toolchain_path_prefix="${script_dir}/../../${toolchain_path_prefix#external/}" + toolchain_path_prefix_abs="$(cd "${toolchain_path_prefix}" && pwd -P)/" else - toolchain_path_prefix_abs="$toolchain_path_prefix" + toolchain_path_prefix_abs="${toolchain_path_prefix}" fi if [[ ! -f ${toolchain_path_prefix}bin/clang ]]; then @@ -128,7 +128,7 @@ function sanitize_option() { 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]}" + printf "%s" "${parts[0]}=${script_dir}/../../../${parts[1]}" else printf "%s" "${opt}" fi