blob: e5751914105d2ccdd0ba16efd59b61110a4e64ae [file] [log] [blame]
Brian Silverman7d89e282021-11-17 17:36:54 -08001#!/bin/bash
2#
3# Copyright 2021 The Bazel Authors. All rights reserved.
4#
5# Licensed under the Apache License, Version 2.0 (the "License");
6# you may not use this file except in compliance with the License.
7# You may obtain a copy of the License at
8#
9# http://www.apache.org/licenses/LICENSE-2.0
10#
11# Unless required by applicable law or agreed to in writing, software
12# distributed under the License is distributed on an "AS IS" BASIS,
13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14# See the License for the specific language governing permissions and
15# limitations under the License.
16#
17# OS X relpath is not really working. This is a wrapper script around gcc
18# to simulate relpath behavior.
19#
20# This wrapper uses install_name_tool to replace all paths in the binary
21# (bazel-out/.../path/to/original/library.so) by the paths relative to
22# the binary. It parses the command line to behave as rpath is supposed
23# to work.
24#
25# See https://blogs.oracle.com/dipol/entry/dynamic_libraries_rpath_and_mac
26# on how to set those paths for Mach-O binaries.
27#
28set -eu
29
30# See note in toolchain/internal/configure.bzl where we define
31# `wrapper_bin_prefix` for why this wrapper is needed.
32
33# Call the C++ compiler.
34if [[ -f %{toolchain_path_prefix}bin/clang ]]; then
Austin Schuh94dbdf32024-04-11 22:51:09 -070035 export LD_LIBRARY_PATH=external/llvm_toolchain/llvm/lib/
Brian Silverman7d89e282021-11-17 17:36:54 -080036 exec %{toolchain_path_prefix}bin/clang "$@"
37elif [[ "${BASH_SOURCE[0]}" == "/"* ]]; then
38 # Some consumers of `CcToolchainConfigInfo` (e.g. `cmake` from rules_foreign_cc)
39 # change CWD and call $CC (this script) with its absolute path.
40 # the execroot (i.e. `cmake` from `rules_foreign_cc`) and call CC . For cases like this,
41 # we'll try to find `clang` relative to this script.
42 # This script is at _execroot_/external/_repo_name_/bin/clang_wrapper.sh
43 execroot_path="${BASH_SOURCE[0]%/*/*/*/*}"
44 clang="${execroot_path}/%{toolchain_path_prefix}bin/clang"
Austin Schuh94dbdf32024-04-11 22:51:09 -070045 export LD_LIBRARY_PATH="${execroot_path}/external/llvm_toolchain/llvm/lib/"
Brian Silverman7d89e282021-11-17 17:36:54 -080046 exec "${clang}" "${@}"
47else
48 >&2 echo "ERROR: could not find clang; PWD=\"$(pwd)\"; PATH=\"${PATH}\"."
49 exit 5
50fi