Skip to content

Commit

Permalink
Fix santizers on MacOS that require dylibs in the LLVM toolchain. (ba…
Browse files Browse the repository at this point in the history
…zel-contrib#250)

Add dylibs to the `lib` target so that santizers work properly on MacOS.

Resolves bazel-contrib#192.

---------

Co-authored-by: Siddhartha Bagaria <starsid@gmail.com>
  • Loading branch information
2 people authored and jez committed Apr 30, 2024
1 parent f16a6b0 commit 0050abc
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 23 deletions.
7 changes: 4 additions & 3 deletions toolchain/BUILD.llvm_repo
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ filegroup(
[
"lib/**/lib*.a",
"lib/clang/*/lib/**/*.a",
"lib/clang/*/lib/**/*.dylib",
# clang_rt.*.o supply crtbegin and crtend sections.
"lib/**/clang_rt.*.o",
],
Expand All @@ -67,9 +68,9 @@ filegroup(
"lib/liblld*.a",
],
),
# Do not include the .dylib files in the linker sandbox because they will
# not be available at runtime. Any library linked from the toolchain should
# be linked statically.
# Include the .dylib files in the linker sandbox even though they will
# not be available at runtime to allow sanitizers to work locally.
# Any library linked from the toolchain to be released should be linked statically.
)

filegroup(
Expand Down
30 changes: 10 additions & 20 deletions toolchain/cc_toolchain_config.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -196,32 +196,22 @@ def cc_toolchain_config(
"-ldl",
])
else:
# The only known mechanism to static link libraries in ld64 is to
# not have the corresponding .dylib files in the library search
# path. The link time sandbox does not include the .dylib files, so
# anything we pick up from the toolchain should be statically
# linked. However, several system libraries on macOS dynamically
# link libc++ and libc++abi, so static linking them becomes a problem.
# We need to ensure that they are dynamic linked from the system
# sysroot and not static linked from the toolchain, so explicitly
# have the sysroot directory on the search path and then add the
# toolchain directory back after we are done.
# Several system libraries on macOS dynamically link libc++ and
# libc++abi, so static linking them becomes a problem. We need to
# ensure that they are dynamic linked from the system sysroot and
# not static linked from the toolchain, so explicitly have the
# sysroot directory on the search path and then add the toolchain
# directory back after we are done.
link_flags.extend([
"-L{}/usr/lib".format(compiler_configuration["sysroot_path"]),
"-lc++",
"-lc++abi",
])

# Let's provide the path to the toolchain library directory
# explicitly as part of the search path to make it easy for a user
# to pick up something. This also makes the behavior consistent with
# targets when a user explicitly depends on something like
# libomp.dylib, which adds this directory to the search path, and would
# (unintentionally) lead to static linking of libraries from the
# toolchain.
link_flags.extend([
"-Bstatic",
"-lunwind",
"-Bdynamic",
"-L{}lib".format(toolchain_path_prefix),
])

elif stdlib == "libc++":
cxx_flags = [
"-std=" + cxx_standard,
Expand Down

0 comments on commit 0050abc

Please sign in to comment.