Skip to content

Commit

Permalink
Ensure /usr/bin is in PATH for macOS (bazel-contrib#106)
Browse files Browse the repository at this point in the history
This is an alternative fix to what was included in bazel-contrib#105.
  • Loading branch information
Siddhartha Bagaria authored Sep 26, 2021
1 parent 8c24bfd commit 70d91f1
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 5 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,8 @@ the / read-only mount. Note that this will make your builds non-hermetic.

#### Compatibility

The toolchain is tested to work with `rules_go` and `rules_foreign_cc`.
The toolchain is tested to work with `rules_go`, `rules_rust`, and
`rules_foreign_cc`.

#### Accessing tools

Expand Down
8 changes: 4 additions & 4 deletions toolchain/internal/repo.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ def llvm_repo_impl(rctx):

_download_llvm_preconfigured(rctx)

# darwin may use local 'ld' so symlink it to bin directory to help
# other programs locate it when called directly (e.g. rustc)
if os == "darwin":
rctx.symlink("/usr/bin/ld", "bin/ld")
# We try to avoid patches to the downloaded repo so that it is easier for
# users to bring their own LLVM distribution through `http_archive`. If we
# do want to make changes, then we should do it through a patch file, and
# document it for users of toolchain_roots attribute.
20 changes: 20 additions & 0 deletions toolchain/osx_cc_wrapper.sh.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,26 @@ for i in "$@"; do
fi
done

# On macOS, we use ld as the linker for single-platform builds (i.e., when not
# cross-compiling). Some applications may remove /usr/bin from PATH before
# calling this script, which would make /usr/bin/ld unreachable. For example,
# rules_rust does not set PATH (unless the user explicitly sets PATH in env
# through attributes) [1] when calling rustc, and rustc does not replace an
# unset PATH with a reasonable default either ([2], [3]), which results in CC
# being called with PATH={sysroot}/{rust_lib}/bin. Note that rules_cc [4] and
# rules_go [5] do ensure that /usr/bin is in PATH.
# [1]: https://github.com/bazelbuild/rules_rust/blob/e589105b4e8181dd1d0d8ccaa0cf3267efb06e86/cargo/cargo_build_script.bzl#L66-L68
# [2]: https://github.com/rust-lang/rust/blob/1c03f0d0ba4fee54b7aa458f4d3ad989d8bf7b34/compiler/rustc_session/src/session.rs#L804-L813
# [3]: https://github.com/rust-lang/rust/blob/1c03f0d0ba4fee54b7aa458f4d3ad989d8bf7b34/compiler/rustc_codegen_ssa/src/back/link.rs#L640-L645
# [4]: https://cs.opensource.google/bazel/bazel/+/master:src/main/java/com/google/devtools/build/lib/bazel/rules/BazelRuleClassProvider.java;l=529;drc=72caead7b428fd50164079956ec368fc54a9567c
# [5]: https://github.com/bazelbuild/rules_go/blob/63dfd99403076331fef0775d52a8039d502d4115/go/private/context.bzl#L434
# Let's restore /usr/bin to PATH in such cases. Note that /usr/bin is not a
# writeable directory on macOS even with sudo privileges, so it should be safe
# to add it to PATH even when the application wants to use a very strict PATH.
if [[ ":${PATH}:" != *":/usr/bin:"* ]]; then
PATH="${PATH}:/usr/bin"
fi

# Call the C++ compiler.
if [[ -f %{toolchain_path_prefix}bin/clang ]]; then
%{toolchain_path_prefix}bin/clang "$@"
Expand Down

0 comments on commit 70d91f1

Please sign in to comment.