Skip to content

Commit 6bf3f41

Browse files
committed
chore: move llvm toolchain setup to bzlmod
1 parent 9b595d8 commit 6bf3f41

File tree

4 files changed

+223
-80
lines changed

4 files changed

+223
-80
lines changed

MODULE.bazel

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ module(
66
bazel_dep(name = "aspect_bazel_lib", version = "2.10.0")
77
bazel_dep(name = "bazel_features", version = "1.19.0")
88

9+
include("//bazel/include:llvm.MODULE.bazel")
10+
911
include("//bazel/include:proto.MODULE.bazel")
1012

1113
include("//bazel/include:python.MODULE.bazel")

WORKSPACE

Lines changed: 0 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -59,86 +59,6 @@ http_archive(
5959
],
6060
)
6161

62-
http_archive(
63-
name = "com_grail_bazel_toolchain",
64-
# Note: these commits are on the silo branch of aspect-forks/bazel-toolchain
65-
sha256 = "a9fc7cf01d0ea0a935bd9e3674dd3103766db77dfc6aafcb447a7ddd6ca24a78",
66-
strip_prefix = "toolchains_llvm-c65ef7a45907016a754e5bf5bfabac76eb702fd3",
67-
urls = ["https://github.com/bazel-contrib/toolchains_llvm/archive/c65ef7a45907016a754e5bf5bfabac76eb702fd3.tar.gz"],
68-
)
69-
70-
_SYSROOT_LINUX_BUILD_FILE = """
71-
filegroup(
72-
name = "sysroot",
73-
srcs = glob(["*/**"]),
74-
visibility = ["//visibility:public"],
75-
)
76-
"""
77-
78-
_SYSROOT_DARWIN_BUILD_FILE = """
79-
filegroup(
80-
name = "sysroot",
81-
srcs = glob(
82-
include = ["**"],
83-
exclude = ["**/*:*"],
84-
),
85-
visibility = ["//visibility:public"],
86-
)
87-
"""
88-
89-
load("@com_grail_bazel_toolchain//toolchain:rules.bzl", "llvm_toolchain")
90-
91-
llvm_toolchain(
92-
name = "llvm_toolchain",
93-
llvm_version = "14.0.0",
94-
sha256 = {
95-
"darwin-aarch64": "1b8975db6b638b308c1ee437291f44cf8f67a2fb926eb2e6464efd180e843368",
96-
"linux-x86_64": "564fcbd79c991e93fdf75f262fa7ac6553ec1dd04622f5d7db2a764c5dc7fac6",
97-
},
98-
strip_prefix = {
99-
"darwin-aarch64": "clang+llvm-14.0.0-arm64-apple-darwin",
100-
"linux-x86_64": "clang+llvm-14.0.0-x86_64-linux-gnu",
101-
},
102-
sysroot = {
103-
"darwin-aarch64": "@sysroot_darwin_universal//:sysroot",
104-
"darwin-x86_64": "@sysroot_darwin_universal//:sysroot",
105-
"linux-aarch64": "@org_chromium_sysroot_linux_arm64//:sysroot",
106-
"linux-x86_64": "@org_chromium_sysroot_linux_x86_64//:sysroot",
107-
},
108-
urls = {
109-
"darwin-aarch64": ["https://github.com/aspect-forks/llvm-project/releases/download/aspect-release-14.0.0/clang+llvm-14.0.0-arm64-apple-darwin.tar.xz"],
110-
"linux-x86_64": ["https://github.com/aspect-forks/llvm-project/releases/download/aspect-release-14.0.0/clang+llvm-14.0.0-x86_64-linux-gnu.tar.xz"],
111-
},
112-
)
113-
114-
load("//platforms/toolchains:defs.bzl", "register_llvm_toolchains")
115-
116-
register_llvm_toolchains()
117-
118-
http_archive(
119-
name = "org_chromium_sysroot_linux_arm64",
120-
build_file_content = _SYSROOT_LINUX_BUILD_FILE,
121-
sha256 = "cf2fefded0449f06d3cf634bfa94ffed60dbe47f2a14d2900b00eb9bcfb104b8",
122-
urls = ["https://commondatastorage.googleapis.com/chrome-linux-sysroot/toolchain/80fc74e431f37f590d0c85f16a9d8709088929e8/debian_bullseye_arm64_sysroot.tar.xz"],
123-
)
124-
125-
http_archive(
126-
name = "org_chromium_sysroot_linux_x86_64",
127-
build_file_content = _SYSROOT_LINUX_BUILD_FILE,
128-
sha256 = "04b94ba1098b71f8543cb0ba6c36a6ea2890d4d417b04a08b907d96b38a48574",
129-
urls = ["https://commondatastorage.googleapis.com/chrome-linux-sysroot/toolchain/f5f68713249b52b35db9e08f67184cac392369ab/debian_bullseye_amd64_sysroot.tar.xz"],
130-
)
131-
132-
http_archive(
133-
name = "sysroot_darwin_universal",
134-
build_file_content = _SYSROOT_DARWIN_BUILD_FILE,
135-
# The ruby header has an infinite symlink that we need to remove.
136-
patch_cmds = ["rm System/Library/Frameworks/Ruby.framework/Versions/Current/Headers/ruby/ruby"],
137-
sha256 = "71ae00a90be7a8c382179014969cec30d50e6e627570af283fbe52132958daaf",
138-
strip_prefix = "MacOSX11.3.sdk",
139-
urls = ["https://s3.us-east-2.amazonaws.com/static.aspect.build/sysroots/MacOSX11.3.sdk.tar.xz"],
140-
)
141-
14262
# Ensure this version always matches the go.mod version.
14363
http_archive(
14464
name = "io_bazel_rules_go",

bazel/include/llvm.MODULE.bazel

Lines changed: 143 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
1+
"LLVM configuration"
2+
3+
bazel_dep(name = "toolchains_llvm", version = "1.3.0")
4+
single_version_override(
5+
module_name = "toolchains_llvm",
6+
patch_strip = 1,
7+
patches = [
8+
"//:patches/toolchains_llvm.patch",
9+
],
10+
)
11+
12+
llvm = use_extension("@toolchains_llvm//toolchain/extensions:llvm.bzl", "llvm")
13+
llvm.toolchain(
14+
name = "llvm_toolchain",
15+
llvm_versions = {
16+
# Default to version 16. Used for linux-aarch64
17+
"": "16.0.0",
18+
"darwin-aarch64": "15.0.6",
19+
"linux-x86_64": "14.0.0",
20+
},
21+
sha256 = {
22+
"darwin-aarch64": "32bc7b8eee3d98f72dd4e5651e6da990274ee2d28c5c19a7d8237eb817ce8d91",
23+
"linux-x86_64": "564fcbd79c991e93fdf75f262fa7ac6553ec1dd04622f5d7db2a764c5dc7fac6",
24+
},
25+
stdlib = {
26+
"darwin-aarch64": "libc",
27+
"linux-x86_64": "libc",
28+
},
29+
strip_prefix = {
30+
"darwin-aarch64": "clang+llvm-15.0.6-arm64-apple-darwin21.0",
31+
"linux-x86_64": "clang+llvm-14.0.0-x86_64-linux-gnu",
32+
},
33+
urls = {
34+
"darwin-aarch64": ["https://github.com/llvm/llvm-project/releases/download/llvmorg-15.0.6/clang+llvm-15.0.6-arm64-apple-darwin21.0.tar.xz"],
35+
"linux-x86_64": ["https://github.com/aspect-forks/llvm-project/releases/download/aspect-release-14.0.0/clang+llvm-14.0.0-x86_64-linux-gnu.tar.xz"],
36+
},
37+
)
38+
llvm.sysroot(
39+
name = "llvm_toolchain",
40+
label = "@org_chromium_sysroot_linux_x86_64//:sysroot",
41+
targets = ["linux-x86_64"],
42+
)
43+
llvm.sysroot(
44+
name = "llvm_toolchain",
45+
label = "@org_chromium_sysroot_linux_arm64//:sysroot",
46+
targets = ["linux-aarch64"],
47+
)
48+
llvm.sysroot(
49+
name = "llvm_toolchain",
50+
label = "@sysroot_darwin_universal//:sysroot",
51+
targets = [
52+
"darwin-aarch64",
53+
"darwin-x86_64",
54+
],
55+
)
56+
use_repo(llvm, "llvm_toolchain")
57+
58+
http_archive = use_repo_rule("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
59+
60+
http_archive(
61+
name = "org_chromium_sysroot_linux_arm64",
62+
build_file_content = """
63+
filegroup(
64+
name = "sysroot",
65+
srcs = glob(["*/**"]),
66+
visibility = ["//visibility:public"],
67+
)
68+
""",
69+
sha256 = "cf2fefded0449f06d3cf634bfa94ffed60dbe47f2a14d2900b00eb9bcfb104b8",
70+
urls = ["https://commondatastorage.googleapis.com/chrome-linux-sysroot/toolchain/80fc74e431f37f590d0c85f16a9d8709088929e8/debian_bullseye_arm64_sysroot.tar.xz"],
71+
)
72+
73+
http_archive(
74+
name = "org_chromium_sysroot_linux_x86_64",
75+
build_file_content = """
76+
filegroup(
77+
name = "sysroot",
78+
srcs = glob(["*/**"]),
79+
visibility = ["//visibility:public"],
80+
)
81+
""",
82+
sha256 = "04b94ba1098b71f8543cb0ba6c36a6ea2890d4d417b04a08b907d96b38a48574",
83+
urls = ["https://commondatastorage.googleapis.com/chrome-linux-sysroot/toolchain/f5f68713249b52b35db9e08f67184cac392369ab/debian_bullseye_amd64_sysroot.tar.xz"],
84+
)
85+
86+
http_archive(
87+
name = "sysroot_darwin_universal",
88+
build_file_content = """
89+
filegroup(
90+
name = "sysroot",
91+
srcs = glob(
92+
include = ["**"],
93+
exclude = ["**/*:*"],
94+
),
95+
visibility = ["//visibility:public"],
96+
)
97+
""",
98+
# The ruby header has an infinite symlink that we need to remove.
99+
patch_cmds = ["rm System/Library/Frameworks/Ruby.framework/Versions/Current/Headers/ruby/ruby"],
100+
sha256 = "71ae00a90be7a8c382179014969cec30d50e6e627570af283fbe52132958daaf",
101+
strip_prefix = "MacOSX11.3.sdk",
102+
urls = ["https://s3.us-east-2.amazonaws.com/static.aspect.build/sysroots/MacOSX11.3.sdk.tar.xz"],
103+
)
104+
105+
execution_oses = [
106+
"macos",
107+
"linux",
108+
]
109+
110+
execution_cpus = [
111+
"aarch64",
112+
"x86_64",
113+
]
114+
115+
target_oses = [
116+
"macos",
117+
"linux",
118+
]
119+
120+
target_cpus = [
121+
"aarch64",
122+
"x86_64",
123+
]
124+
125+
platforms = [
126+
{
127+
"exe_os": exe_os,
128+
"exe_cpu": exe_cpu,
129+
"tgt_os": tgt_os,
130+
"tgt_cpu": tgt_cpu,
131+
}
132+
for exe_os in execution_oses
133+
for exe_cpu in execution_cpus
134+
for tgt_os in target_oses
135+
for tgt_cpu in target_cpus
136+
]
137+
138+
[register_toolchains("//platforms/toolchains:{}_{}_{}_{}_llvm".format(
139+
p["exe_os"],
140+
p["exe_cpu"],
141+
p["tgt_os"],
142+
p["tgt_cpu"],
143+
)) for p in platforms]

patches/toolchains_llvm.patch

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
diff --git a/toolchain/cc_toolchain_config.bzl b/toolchain/cc_toolchain_config.bzl
2+
index 9dbb41d..12c4ccb 100644
3+
--- a/toolchain/cc_toolchain_config.bzl
4+
+++ b/toolchain/cc_toolchain_config.bzl
5+
@@ -168,11 +168,17 @@ def cc_toolchain_config(
6+
archive_flags = []
7+
8+
# Linker flags:
9+
- if exec_os == "darwin" and not is_xcompile:
10+
- # lld is experimental for Mach-O, so we use the native ld64 linker.
11+
- # TODO: How do we cross-compile from Linux to Darwin?
12+
- use_lld = False
13+
+ ld = "ld.lld"
14+
+ if target_os == "darwin":
15+
+ use_lld = True
16+
+
17+
+ ld = "ld64.lld"
18+
+ ld_path = toolchain_path_prefix + "/bin/" + ld
19+
+ compile_flags.append("-mmacosx-version-min=12.0")
20+
link_flags.extend([
21+
+ "-mmacosx-version-min=12.0",
22+
+ "-Wl,-platform_version,macos,12.0,12.0",
23+
+ "--ld-path=" + ld_path,
24+
"-headerpad_max_install_names",
25+
"-fobjc-link-runtime",
26+
])
27+
@@ -195,7 +201,9 @@ def cc_toolchain_config(
28+
# not an option because it is not a cross-linker, so lld is the
29+
# only option.
30+
use_lld = True
31+
+ ld_path = toolchain_path_prefix + "/bin/" + ld
32+
link_flags.extend([
33+
+ "--ld-path=" + ld_path,
34+
"-fuse-ld=lld",
35+
"-Wl,--build-id=md5",
36+
"-Wl,--hash-style=gnu",
37+
@@ -228,10 +236,6 @@ def cc_toolchain_config(
38+
if use_lld:
39+
# For single-platform builds, we can statically link the bundled
40+
# libraries.
41+
- link_flags.extend([
42+
- "-l:libc++.a",
43+
- "-l:libc++abi.a",
44+
- ])
45+
compiler_rt_link_flags = ["-rtlib=compiler-rt"]
46+
libunwind_link_flags = [
47+
"-l:libunwind.a",
48+
@@ -283,9 +287,6 @@ def cc_toolchain_config(
49+
"-stdlib=libstdc++",
50+
]
51+
52+
- link_flags.extend([
53+
- "-l:libstdc++.a",
54+
- ])
55+
elif stdlib == "libc":
56+
cxx_flags = [
57+
"-std=" + cxx_standard,
58+
@@ -323,7 +324,7 @@ def cc_toolchain_config(
59+
"dwp": tools_path_prefix + "llvm-dwp",
60+
"gcc": wrapper_bin_prefix + "cc_wrapper.sh",
61+
"gcov": tools_path_prefix + "llvm-profdata",
62+
- "ld": tools_path_prefix + "ld.lld" if use_lld else "/usr/bin/ld",
63+
+ "ld": tools_path_prefix + ld if use_lld else "/usr/bin/ld",
64+
"llvm-cov": tools_path_prefix + "llvm-cov",
65+
"llvm-profdata": tools_path_prefix + "llvm-profdata",
66+
"nm": tools_path_prefix + "llvm-nm",
67+
diff --git a/toolchain/internal/common.bzl b/toolchain/internal/common.bzl
68+
index 1feae98..6773380 100644
69+
--- a/toolchain/internal/common.bzl
70+
+++ b/toolchain/internal/common.bzl
71+
@@ -28,6 +28,7 @@ _toolchain_tools = {
72+
for name in [
73+
"clang-cpp",
74+
"ld.lld",
75+
+ "ld64.lld",
76+
"llvm-ar",
77+
"llvm-dwp",
78+
"llvm-profdata",

0 commit comments

Comments
 (0)