Skip to content

Commit 169456e

Browse files
authored
cc: toolchain: add support for hermetic llvm on x86_64-darwin [BUILD-562] (#62)
Adds support for using the hermetic llvm toolchain on x86_64-darwin. The configuration settings for darwin are mostly copied from: https://github.com/grailbio/bazel-toolchain/blob/master/toolchain/cc_toolchain_config.bzl One major difference is we don't need to worry about supporting arbitrary versions of llvm, which simplifies things a little. Also worth mentioning is on mac the toolchain is configured to use the system libc++, ld, and ar. This makes the toolchain less hermetic than the linux version. See the comments for the reasons why.
1 parent 9ecf0ce commit 169456e

File tree

31 files changed

+521
-214
lines changed

31 files changed

+521
-214
lines changed

cc/repositories.bzl

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,17 +11,29 @@
1111
load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
1212
load("@bazel_tools//tools/build_defs/repo:utils.bzl", "maybe")
1313

14-
LLVM_DISTRIBUTION_URL = "https://github.com/llvm/llvm-project/releases/download/llvmorg-14.0.0/clang%2Bllvm-14.0.0-x86_64-linux-gnu-ubuntu-18.04.tar.xz"
14+
X86_64_LINUX_LLVM = "https://github.com/llvm/llvm-project/releases/download/llvmorg-14.0.0/clang%2Bllvm-14.0.0-x86_64-linux-gnu-ubuntu-18.04.tar.xz"
15+
16+
X86_64_DARWIN_LLVM = "https://github.com/llvm/llvm-project/releases/download/llvmorg-14.0.0/clang%2Bllvm-14.0.0-x86_64-apple-darwin.tar.xz"
1517

1618
def swift_cc_toolchain():
1719
maybe(
1820
http_archive,
19-
name = "llvm-distribution",
20-
build_file = Label("//cc/toolchains:llvm.BUILD.bzl"),
21-
url = LLVM_DISTRIBUTION_URL,
21+
name = "x86_64-linux-llvm",
22+
build_file = Label("//cc/toolchains/llvm:llvm.BUILD.bzl"),
23+
url = X86_64_LINUX_LLVM,
2224
strip_prefix = "clang+llvm-14.0.0-x86_64-linux-gnu-ubuntu-18.04",
2325
sha256 = "61582215dafafb7b576ea30cc136be92c877ba1f1c31ddbbd372d6d65622fef5",
2426
)
2527

28+
maybe(
29+
http_archive,
30+
name = "x86_64-darwin-llvm",
31+
build_file = Label("//cc/toolchains/llvm:llvm.BUILD.bzl"),
32+
url = X86_64_DARWIN_LLVM,
33+
strip_prefix = "clang+llvm-14.0.0-x86_64-apple-darwin",
34+
sha256 = "cf5af0f32d78dcf4413ef6966abbfd5b1445fe80bba57f2ff8a08f77e672b9b3",
35+
)
36+
2637
def register_swift_cc_toolchains():
27-
native.register_toolchains("@rules_swiftnav//cc/toolchains:cc-toolchain-x86_64-linux")
38+
native.register_toolchains("@rules_swiftnav//cc/toolchains/llvm/x86_64-linux:cc-toolchain-x86_64-linux")
39+
native.register_toolchains("@rules_swiftnav//cc/toolchains/llvm/x86_64-darwin:cc-toolchain-x86_64-darwin")

cc/toolchains/BUILD.bazel

Lines changed: 0 additions & 121 deletions
Original file line numberDiff line numberDiff line change
@@ -1,121 +0,0 @@
1-
# Copyright (C) 2022 Swift Navigation Inc.
2-
# Contact: Swift Navigation <dev@swift-nav.com>
3-
#
4-
# This source is subject to the license found in the file 'LICENSE' which must
5-
# be be distributed together with this source. All other rights reserved.
6-
#
7-
# THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND,
8-
# EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED
9-
# WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE.
10-
11-
package(default_visibility = ["//visibility:public"])
12-
13-
load(":cc_toolchain_config.bzl", "cc_toolchain_config")
14-
15-
filegroup(name = "empty")
16-
17-
filegroup(
18-
name = "wrappers",
19-
srcs = glob([
20-
"wrappers/**",
21-
]),
22-
)
23-
24-
filegroup(
25-
name = "ar_files",
26-
srcs = [
27-
":wrappers",
28-
"@llvm-distribution//:ar",
29-
],
30-
)
31-
32-
filegroup(
33-
name = "as_files",
34-
srcs = [
35-
":wrappers",
36-
"@llvm-distribution//:as",
37-
],
38-
)
39-
40-
filegroup(
41-
name = "compiler_files",
42-
srcs = [
43-
":wrappers",
44-
"@llvm-distribution//:clang",
45-
"@llvm-distribution//:include",
46-
],
47-
)
48-
49-
filegroup(
50-
name = "dwp_files",
51-
srcs = [
52-
":wrappers",
53-
"@llvm-distribution//:dwp",
54-
],
55-
)
56-
57-
filegroup(
58-
name = "linker_files",
59-
srcs = [
60-
":wrappers",
61-
"@llvm-distribution//:ar",
62-
"@llvm-distribution//:clang",
63-
"@llvm-distribution//:ld",
64-
"@llvm-distribution//:lib",
65-
],
66-
)
67-
68-
filegroup(
69-
name = "objcopy_files",
70-
srcs = [
71-
":wrappers",
72-
"@llvm-distribution//:objcopy",
73-
],
74-
)
75-
76-
filegroup(
77-
name = "strip_files",
78-
srcs = [
79-
":wrappers",
80-
"@llvm-distribution//:strip",
81-
],
82-
)
83-
84-
filegroup(
85-
name = "all_files",
86-
srcs = [
87-
"linker_files",
88-
":compiler_files",
89-
"@llvm-distribution//:bin",
90-
],
91-
)
92-
93-
cc_toolchain_config(name = "local-x86_64-linux")
94-
95-
cc_toolchain(
96-
name = "cc-clang-x86_64-linux",
97-
all_files = ":all_files",
98-
ar_files = ":ar_files",
99-
as_files = ":as_files",
100-
compiler_files = ":compiler_files",
101-
dwp_files = ":dwp_files",
102-
linker_files = ":linker_files",
103-
objcopy_files = ":objcopy_files",
104-
strip_files = ":strip_files",
105-
toolchain_config = ":local-x86_64-linux",
106-
)
107-
108-
toolchain(
109-
name = "cc-toolchain-x86_64-linux",
110-
exec_compatible_with = [
111-
"@platforms//cpu:x86_64",
112-
"@platforms//os:linux",
113-
],
114-
target_compatible_with = [
115-
"@platforms//cpu:x86_64",
116-
"@platforms//os:linux",
117-
],
118-
target_settings = None,
119-
toolchain = ":cc-clang-x86_64-linux",
120-
toolchain_type = "@bazel_tools//tools/cpp:toolchain_type",
121-
)

cc/toolchains/llvm/BUILD.bazel

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# Copyright (C) 2022 Swift Navigation Inc.
2+
# Contact: Swift Navigation <dev@swift-nav.com>
3+
#
4+
# This source is subject to the license found in the file 'LICENSE' which must
5+
# be be distributed together with this source. All other rights reserved.
6+
#
7+
# THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND,
8+
# EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED
9+
# WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE.
10+
11+
package(default_visibility = ["//visibility:public"])

cc/toolchains/cc_toolchain_config.bzl renamed to cc/toolchains/llvm/cc_toolchain_config.bzl

Lines changed: 83 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -13,40 +13,21 @@ load(
1313
unix_cc_toolchain_config = "cc_toolchain_config",
1414
)
1515

16-
def cc_toolchain_config(name):
17-
# These variables are passed directly through to unix_cc_toolchain_config
18-
# below. As far as I can tell they are just metadata that doesn't affect
19-
# the build.
20-
host_system_name = "linux-x86_64"
21-
toolchain_identifier = "clang-x86_64-linux"
22-
target_cpu = "k8"
23-
target_libc = "glibc_unknown"
24-
compiler = "clang"
25-
abi_version = "clang"
26-
abi_libc_version = "glibc_unknown"
27-
28-
cxx_builtin_include_directories = [
29-
"/include",
30-
"/usr/include",
31-
"/usr/local/include",
32-
]
33-
34-
tool_paths = {
35-
"ar": "wrappers/llvm-ar",
36-
"cpp": "wrappers/clang-cpp",
37-
"gcc": "wrappers/clang",
38-
"gcov": "wrappers/llvm-profdata",
39-
"llvm-cov": "wrappers/llvm-cov",
40-
"llvm-profdata": "wrappers/llvm-profdata",
41-
"ld": "wrappers/ld.ldd",
42-
"nm": "wrappers/llvm-nm",
43-
"objcopy": "wrappers/llvm-objcopy",
44-
"objdump": "wrappers/llvm-objdump",
45-
"strip": "wrappers/llvm-strip",
46-
}
47-
48-
target_system_name = "x86_64-unknown-linux-gnu"
49-
16+
def cc_toolchain_config(
17+
name,
18+
host_system_name,
19+
toolchain_identifier,
20+
toolchain_path_prefix,
21+
target_cpu,
22+
target_libc,
23+
compiler,
24+
abi_version,
25+
abi_libc_version,
26+
cxx_builtin_include_directories,
27+
tool_paths,
28+
target_system_name,
29+
builtin_sysroot = None,
30+
is_darwin = False):
5031
# Default compiler flags:
5132
compile_flags = [
5233
"--target=" + target_system_name,
@@ -93,32 +74,79 @@ def cc_toolchain_config(name):
9374
"--target=" + target_system_name,
9475
"-lm",
9576
"-no-canonical-prefixes",
96-
# Below this line, assumes libc++ & lld
97-
"-l:libc++.a",
98-
"-l:libc++abi.a",
99-
"-l:libunwind.a",
100-
# Compiler runtime features.
101-
"-rtlib=compiler-rt",
102-
# To support libunwind
103-
# It's ok to assume posix when using this toolchain
104-
"-lpthread",
105-
"-ldl",
10677
]
10778

108-
# linux/lld only!
109-
link_flags.extend([
110-
"-fuse-ld=lld",
111-
"-Wl,--build-id=md5",
112-
"-Wl,--hash-style=gnu",
113-
"-Wl,-z,relro,-z,now",
114-
])
115-
11679
# Similar to link_flags, but placed later in the command line such that
11780
# unused symbols are not stripped.
11881
link_libs = []
11982

83+
if is_darwin:
84+
# Mach-O support in lld is experimental, so on mac
85+
# we use the system linker.
86+
use_lld = False
87+
link_flags.extend([
88+
"-headerpad_max_install_names",
89+
# This will issue a warning on macOS ventura; see:
90+
# https://github.com/python/cpython/issues/97524
91+
# https://developer.apple.com/forums/thread/719961
92+
"-undefined",
93+
"dynamic_lookup",
94+
])
95+
else:
96+
use_lld = True
97+
link_flags.extend([
98+
"-fuse-ld=lld",
99+
"-Wl,--build-id=md5",
100+
"-Wl,--hash-style=gnu",
101+
"-Wl,-z,relro,-z,now",
102+
])
103+
104+
if use_lld:
105+
link_flags.extend([
106+
# Below this line, assumes libc++ & lld
107+
"-l:libc++.a",
108+
"-l:libc++abi.a",
109+
"-l:libunwind.a",
110+
# Compiler runtime features.
111+
"-rtlib=compiler-rt",
112+
# To support libunwind
113+
# It's ok to assume posix when using this toolchain
114+
"-lpthread",
115+
"-ldl",
116+
])
117+
else:
118+
# The comments below were copied directly from:
119+
# https://github.com/grailbio/bazel-toolchain/blob/795d76fd03e0b17c0961f0981a8512a00cba4fa2/toolchain/cc_toolchain_config.bzl#L202
120+
121+
# The only known mechanism to static link libraries in ld64 is to
122+
# not have the corresponding .dylib files in the library search
123+
# path. The link time sandbox does not include the .dylib files, so
124+
# anything we pick up from the toolchain should be statically
125+
# linked. However, several system libraries on macOS dynamically
126+
# link libc++ and libc++abi, so static linking them becomes a problem.
127+
# We need to ensure that they are dynamic linked from the system
128+
# sysroot and not static linked from the toolchain, so explicitly
129+
# have the sysroot directory on the search path and then add the
130+
# toolchain directory back after we are done.
131+
link_flags.extend([
132+
"-L{}/usr/lib".format(builtin_sysroot),
133+
"-lc++",
134+
"-lc++abi",
135+
])
136+
137+
# Let's provide the path to the toolchain library directory
138+
# explicitly as part of the search path to make it easy for a user
139+
# to pick up something. This also makes the behavior consistent with
140+
# targets when a user explicitly depends on something like
141+
# libomp.dylib, which adds this directory to the search path, and would
142+
# (unintentionally) lead to static linking of libraries from the
143+
# toolchain.
144+
link_flags.extend([
145+
"-L{}/lib".format(toolchain_path_prefix),
146+
])
147+
120148
# linux/lld only
121-
opt_link_flags = ["-Wl,--gc-sections"]
149+
opt_link_flags = ["-Wl,--gc-sections"] if not is_darwin else []
122150

123151
# Unfiltered compiler flags; these are placed at the end of the command
124152
# line, so take precendence over any user supplied flags through --copts or
@@ -140,8 +168,7 @@ def cc_toolchain_config(name):
140168
coverage_compile_flags = ["-fprofile-instr-generate", "-fcoverage-mapping"]
141169
coverage_link_flags = ["-fprofile-instr-generate"]
142170

143-
# true if using lld
144-
supports_start_end_lib = True
171+
supports_start_end_lib = use_lld
145172

146173
# Calls https://github.com/bazelbuild/bazel/blob/master/tools/cpp/unix_cc_toolchain_config.bzl
147174
# Which defines the rule that actually sets up the cc toolchain.
@@ -168,4 +195,5 @@ def cc_toolchain_config(name):
168195
coverage_compile_flags = coverage_compile_flags,
169196
coverage_link_flags = coverage_link_flags,
170197
supports_start_end_lib = supports_start_end_lib,
198+
builtin_sysroot = builtin_sysroot,
171199
)
File renamed without changes.

0 commit comments

Comments
 (0)