Skip to content

Commit b0c59d7

Browse files
committed
Add a repository rule for efficient sysroots
1 parent cebf1ed commit b0c59d7

File tree

4 files changed

+68
-21
lines changed

4 files changed

+68
-21
lines changed

MODULE.bazel

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,18 @@ module(
2121

2222
bazel_dep(name = "bazel_features", version = "1.36.0")
2323
bazel_dep(name = "bazel_skylib", version = "1.5.0")
24+
bazel_dep(name = "aspect_bazel_lib", version = "2.0.0")
2425
bazel_dep(name = "rules_cc", version = "0.2.2")
2526
bazel_dep(name = "platforms", version = "0.0.8")
2627
bazel_dep(name = "helly25_bzl", version = "0.1.2")
2728

29+
bazel_dep(name = "tar.bzl", version = "0.6.0")
30+
tar_toolchains = use_extension("@tar.bzl//tar:extensions.bzl", "toolchains")
31+
use_repo(tar_toolchains,
32+
"bsd_tar_toolchains_darwin_arm64",
33+
"bsd_tar_toolchains_linux_arm64",
34+
"bsd_tar_toolchains_linux_amd64",
35+
)
36+
2837
# TODO: Remove when protobuf is released with a version of rules_python that supports 8.x
2938
bazel_dep(name = "rules_python", version = "1.0.0", dev_dependency = True)

tests/MODULE.bazel

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ llvm.toolchain_root(
171171
)
172172
llvm.sysroot(
173173
name = "llvm_toolchain_with_sysroot",
174-
label = "@org_chromium_sysroot_linux_x64//:sysroot",
174+
label = "@org_chromium_sysroot_linux_x64//sysroot",
175175
targets = ["linux-x86_64"],
176176
)
177177
use_repo(llvm, "llvm_toolchain_with_sysroot")
@@ -253,18 +253,11 @@ libclang_rt_wasm32 = use_repo_rule("//wasm:wasi_sdk.bzl", "libclang_rt_wasm32")
253253

254254
libclang_rt_wasm32(name = "libclang_rt_wasm32")
255255

256-
http_archive = use_repo_rule("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
256+
sysroot = use_repo_rule("@toolchains_llvm//toolchain:sysroot.bzl", "sysroot")
257257

258258
# This sysroot is used by github.com/vsco/bazel-toolchains.
259-
http_archive(
259+
sysroot(
260260
name = "org_chromium_sysroot_linux_x64",
261-
build_file_content = """
262-
filegroup(
263-
name = "sysroot",
264-
srcs = glob(["*/**"]),
265-
visibility = ["//visibility:public"],
266-
)
267-
""",
268261
sha256 = "84656a6df544ecef62169cfe3ab6e41bb4346a62d3ba2a045dc5a0a2ecea94a3",
269-
urls = ["https://commondatastorage.googleapis.com/chrome-linux-sysroot/toolchain/2202c161310ffde63729f29d27fe7bb24a0bc540/debian_stretch_amd64_sysroot.tar.xz"],
262+
url = "https://commondatastorage.googleapis.com/chrome-linux-sysroot/toolchain/2202c161310ffde63729f29d27fe7bb24a0bc540/debian_stretch_amd64_sysroot.tar.xz",
270263
)

tests/WORKSPACE

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -114,24 +114,19 @@ llvm_toolchain(
114114
## Toolchain example with a sysroot.
115115

116116
# This sysroot is used by github.com/vsco/bazel-toolchains.
117-
http_archive(
117+
load("@toolchains_llvm//toolchain:sysroot.bzl", "sysroot")
118+
119+
sysroot(
118120
name = "org_chromium_sysroot_linux_x64",
119-
build_file_content = """
120-
filegroup(
121-
name = "sysroot",
122-
srcs = glob(["*/**"]),
123-
visibility = ["//visibility:public"],
124-
)
125-
""",
126121
sha256 = "84656a6df544ecef62169cfe3ab6e41bb4346a62d3ba2a045dc5a0a2ecea94a3",
127-
urls = ["https://commondatastorage.googleapis.com/chrome-linux-sysroot/toolchain/2202c161310ffde63729f29d27fe7bb24a0bc540/debian_stretch_amd64_sysroot.tar.xz"],
122+
url = "https://commondatastorage.googleapis.com/chrome-linux-sysroot/toolchain/2202c161310ffde63729f29d27fe7bb24a0bc540/debian_stretch_amd64_sysroot.tar.xz",
128123
)
129124

130125
llvm_toolchain(
131126
name = "llvm_toolchain_with_sysroot",
132127
llvm_versions = LLVM_VERSIONS,
133128
sysroot = {
134-
"linux-x86_64": "@org_chromium_sysroot_linux_x64//:sysroot",
129+
"linux-x86_64": "@org_chromium_sysroot_linux_x64//sysroot",
135130
},
136131
# We can share the downloaded LLVM distribution with the first configuration.
137132
toolchain_roots = {

toolchain/sysroot.bzl

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
load("@aspect_bazel_lib//lib:repo_utils.bzl", "repo_utils")
2+
3+
def _sysroot_impl(rctx):
4+
url = rctx.attr.url
5+
_, _, archive = url.rpartition("/")
6+
7+
rctx.download(url, archive, sha256 = rctx.attr.sha256)
8+
9+
# Declare the sysroot files as a source directory so they can be
10+
# optimized in the Merkle tree cache more effectively.
11+
rctx.file("sysroot/BUILD.bazel", \
12+
"""filegroup(
13+
name = "sysroot",
14+
srcs = ["."],
15+
visibility = ["//visibility:public"],
16+
)""")
17+
18+
host_bsdtar = Label("@bsd_tar_toolchains_%s//:tar" % repo_utils.platform(rctx))
19+
20+
cmd = [
21+
str(rctx.path(host_bsdtar)),
22+
"--extract",
23+
"--no-same-owner",
24+
"--no-same-permissions",
25+
"--file", archive,
26+
"--directory", "sysroot",
27+
]
28+
29+
for include in rctx.attr.include_patterns:
30+
cmd.extend(["--include", include])
31+
32+
for exclude in rctx.attr.exclude_patterns:
33+
cmd.extend(["--exclude", exclude])
34+
35+
result = rctx.execute(cmd)
36+
if result.return_code != 0:
37+
fail(result.stdout + result.stderr)
38+
39+
rctx.delete(archive)
40+
41+
sysroot = repository_rule(
42+
implementation = _sysroot_impl,
43+
attrs = {
44+
"url": attr.string(),
45+
"sha256": attr.string(),
46+
"include_patterns": attr.string_list(),
47+
"exclude_patterns": attr.string_list(),
48+
},
49+
)
50+

0 commit comments

Comments
 (0)