Skip to content

Commit 036d1fd

Browse files
alexeagledzbarsky
andcommitted
Use statically-linked bsdtar on all platforms (#804)
* [tar] Switch to statically linked binaries * chore: don't use bsdtar for extract yet * chore: add dzbarsky integrity hashes * Upgrade to 3.7.2.bcr.2 (#806) * chore: replace URLs with aspect-build fork --------- Co-authored-by: David Zbarsky <dzbarsky@gmail.com>
1 parent 4109b3c commit 036d1fd

File tree

2 files changed

+53
-135
lines changed

2 files changed

+53
-135
lines changed

MODULE.bazel

+1-6
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,7 @@ register_toolchains(
3030
"@yq_toolchains//:all",
3131
"@coreutils_toolchains//:all",
3232
"@expand_template_toolchains//:all",
33-
# Expand bsd_tar_toolchains
34-
"@bsd_tar_toolchains//:linux_amd64_toolchain",
35-
"@bsd_tar_toolchains//:linux_arm64_toolchain",
36-
"@bsd_tar_toolchains//:windows_amd64_toolchain",
37-
# host toolchain must be last, as it's only suitable as a fallback on macos
38-
"@bsd_tar_toolchains//:host_toolchain",
33+
"@bsd_tar_toolchains//:all",
3934
)
4035

4136
# To allow /tools to be built from source

lib/private/tar_toolchain.bzl

+52-129
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,18 @@
11
"Provide access to a BSD tar"
22

3-
load(":repo_utils.bzl", "repo_utils")
4-
53
BSDTAR_PLATFORMS = {
4+
"darwin_amd64": struct(
5+
compatible_with = [
6+
"@platforms//os:osx",
7+
"@platforms//cpu:x86_64",
8+
],
9+
),
10+
"darwin_arm64": struct(
11+
compatible_with = [
12+
"@platforms//os:osx",
13+
"@platforms//cpu:aarch64",
14+
],
15+
),
616
"linux_amd64": struct(
717
compatible_with = [
818
"@platforms//os:linux",
@@ -22,145 +32,58 @@ BSDTAR_PLATFORMS = {
2232
"@platforms//cpu:x86_64",
2333
],
2434
),
25-
# WARNING: host toolchain should always come last to make it a fallback toolchain.
26-
"host": struct(
27-
# loaded by the macro
28-
compatible_with = "HOST_CONSTRAINTS",
29-
),
3035
}
3136

32-
WINDOWS_DEPS = (
33-
"e06f10043b1b148eb38ad06cff678af05beade0bdd2edd8735a198c521fa3993",
34-
"https://github.com/libarchive/libarchive/releases/download/v3.7.2/libarchive-v3.7.2-amd64.zip",
35-
)
36-
37-
# note, using Ubuntu Focal packages as they link with older glibc versions.
38-
# Ubuntu Jammy packages will fail on ubuntu 20.02 with
39-
# bsdtar: /lib/x86_64-linux-gnu/libc.so.6: version `GLIBC_2.33' not found
40-
# bsdtar: /lib/x86_64-linux-gnu/libc.so.6: version `GLIBC_2.34' not found
41-
#
42-
# TODO: this is only a partial listing of the transitive deps of libarchive-tools
43-
# so we expect a bunch of compress modes are broken, for example.
44-
45-
LINUX_LIB_DEPS = {
46-
"linux_arm64": [
47-
(
48-
"6d18525e248e84b8a4ee39a226fd1195ca9b9d0d5a1c7909ae4f997d46378848",
49-
"http://ports.ubuntu.com/pool/main/n/nettle/libnettle7_3.5.1+really3.5.1-2ubuntu0.2_arm64.deb",
50-
),
51-
(
52-
"aa5e31d05a9d6bde8093137bd1c82b5a20a5f470bd5109642014f895c20f323a",
53-
"http://ports.ubuntu.com/pool/main/liba/libarchive/libarchive13_3.4.0-2ubuntu1_arm64.deb",
54-
),
55-
(
56-
"6d089f878507b536d8ca51b1ad80a80706a1dd7dbbcce7600800d3f9f98be2ab",
57-
"http://ports.ubuntu.com/pool/main/liba/libarchive/libarchive-tools_3.2.1-2~ubuntu16.04.1_arm64.deb",
58-
),
59-
(
60-
"6242892cb032859044ddfcfbe61bac5678a95c585d8fff4525acaf45512e3d39",
61-
"http://ports.ubuntu.com/pool/main/libx/libxml2/libxml2_2.9.10+dfsg-5_arm64.deb",
62-
),
63-
(
64-
"6302e309ab002af30ddfa0d68de26c68f7c034ed2f45b1d97a712bff1a03999a",
65-
"http://ports.ubuntu.com/pool/main/i/icu/libicu66_66.1-2ubuntu2_arm64.deb",
66-
),
67-
],
68-
"linux_amd64": [
69-
# https://packages.ubuntu.com/focal/amd64/libarchive-tools/download
70-
(
71-
"12a19878d34b407e6f4893d3b26b7758a26c5534a066d76184c8b764b2df1652",
72-
"http://security.ubuntu.com/ubuntu/pool/universe/liba/libarchive/libarchive-tools_3.4.0-2ubuntu1.2_amd64.deb",
73-
),
74-
# https://packages.ubuntu.com/focal/amd64/libarchive13/download
75-
(
76-
"8ba7507f61bb3ea8da488702ec0badcbfb726d36ea6886e3421ac59082aaf2d1",
77-
"http://security.ubuntu.com/ubuntu/pool/main/liba/libarchive/libarchive13_3.4.0-2ubuntu1.2_amd64.deb",
78-
),
79-
# https://packages.ubuntu.com/focal/amd64/libnettle7/download
80-
(
81-
"3496aed83407fde71e0dc5988b28e8fd7f07a2f27fcf3e0f214c7cd86667eecd",
82-
"http://security.ubuntu.com/ubuntu/pool/main/n/nettle/libnettle7_3.5.1+really3.5.1-2ubuntu0.2_amd64.deb",
83-
),
84-
# https://packages.ubuntu.com/focal/amd64/libxml2/download
85-
(
86-
"cef3871873e5e7e7f1d01afec4d35f15504a9d8a0afbe56f57037e92f7f8e850",
87-
"http://security.ubuntu.com/ubuntu/pool/main/libx/libxml2/libxml2_2.9.10+dfsg-5ubuntu0.20.04.7_amd64.deb",
88-
),
89-
# https://packages.ubuntu.com/focal/amd64/libicu66/download
90-
(
91-
"00d0de456134668f41bd9ea308a076bc0a6a805180445af8a37209d433f41efe",
92-
"http://security.ubuntu.com/ubuntu/pool/main/i/icu/libicu66_66.1-2ubuntu2.1_amd64.deb",
93-
),
94-
],
37+
BSDTAR_PREBUILT = {
38+
"darwin_amd64": (
39+
"https://github.com/aspect-build/bsdtar-prebuilt/releases/download/v3.7.2/tar_darwin_amd64",
40+
"a3bd0e7be92bcddcb70341f1efc48c29ef99b3ad57349b433e9a3182b68cb0c1",
41+
),
42+
"darwin_arm64": (
43+
"https://github.com/aspect-build/bsdtar-prebuilt/releases/download/v3.7.2/tar_darwin_arm64",
44+
"63ee769e2d870d1ed3542e292c919dc8a3934544d17b2de34213c18c41c5437f",
45+
),
46+
"linux_amd64": (
47+
"https://github.com/aspect-build/bsdtar-prebuilt/releases/download/v3.7.2/tar_linux_amd64",
48+
"d40582e64aace892e2f9588045edc5e67023ca3371cd575e7043b0c2a08205b4",
49+
),
50+
"linux_arm64": (
51+
"https://github.com/aspect-build/bsdtar-prebuilt/releases/download/v3.7.2/tar_linux_arm64",
52+
"e2527be38499e94e021c7c02476b4cff8083313d38c85bdf46fc5751d31d32aa",
53+
),
54+
"windows_amd64": (
55+
"https://github.com/libarchive/libarchive/releases/download/v3.7.2/libarchive-v3.7.2-amd64.zip",
56+
"e06f10043b1b148eb38ad06cff678af05beade0bdd2edd8735a198c521fa3993",
57+
),
9558
}
9659

97-
def _find_usable_system_tar(rctx, tar_name):
98-
tar = rctx.which(tar_name)
99-
if not tar:
100-
fail("tar not found on PATH, and we don't handle this case yet")
101-
102-
# Run tar --version and see if we are satisfied to use it
103-
tar_version = rctx.execute([tar, "--version"]).stdout.strip()
104-
105-
# TODO: also check if it's really ancient or compiled without gzip support or something?
106-
# TODO: document how users could fetch the source and compile it themselves
107-
if tar_version.find("bsdtar") >= 0:
108-
return tar
109-
110-
fail("tar isn't a BSD tar")
111-
11260
def _bsdtar_binary_repo(rctx):
113-
tar_name = "tar.exe" if repo_utils.is_windows(rctx) else "tar"
114-
build_header = """\
115-
# @generated by @aspect_bazel_lib//lib/private:tar_toolchain.bzl
116-
117-
load("@aspect_bazel_lib//lib/private:tar_toolchain.bzl", "tar_toolchain")
118-
119-
package(default_visibility = ["//visibility:public"])
120-
121-
"""
122-
123-
# On MacOS, the system `tar` binary on the PATH should already work
124-
if rctx.attr.platform == "host":
125-
tar = _find_usable_system_tar(rctx, tar_name)
126-
output = rctx.path(tar_name)
127-
rctx.symlink(tar, output)
128-
rctx.file("BUILD.bazel", build_header + """tar_toolchain(name = "bsdtar_toolchain", binary = "tar")""")
129-
return
130-
131-
if repo_utils.is_windows(rctx):
61+
(url, sha256) = BSDTAR_PREBUILT[rctx.attr.platform]
62+
if rctx.attr.platform.startswith("windows"):
13263
rctx.download_and_extract(
133-
url = WINDOWS_DEPS[1],
64+
url = url,
13465
type = "zip",
135-
sha256 = WINDOWS_DEPS[0],
66+
sha256 = sha256,
67+
)
68+
binary = "libarchive/bin/bsdtar.exe"
69+
else:
70+
rctx.download(
71+
url = url,
72+
output = "tar",
73+
executable = True,
74+
sha256 = sha256,
13675
)
137-
rctx.file("BUILD.bazel", build_header + """tar_toolchain(name = "bsdtar_toolchain", binary = "libarchive/bin/bsdtar.exe")""")
138-
return
76+
binary = "tar"
13977

140-
# Other platforms, we have more work to do.
141-
libs_dir = "usr/lib/x86_64-linux-gnu" if rctx.attr.platform.endswith("amd64") else "usr/lib/aarch64-linux-gnu"
78+
rctx.file("BUILD.bazel", """\
79+
# @generated by @aspect_bazel_lib//lib/private:tar_toolchain.bzl
14280
143-
for lib in LINUX_LIB_DEPS[rctx.attr.platform]:
144-
rctx.download_and_extract(
145-
url = lib[1],
146-
type = "deb",
147-
sha256 = lib[0],
148-
)
149-
rctx.extract("data.tar.xz")
81+
load("@aspect_bazel_lib//lib/private:tar_toolchain.bzl", "tar_toolchain")
15082
151-
rctx.file("bsdtar.sh", """#!/usr/bin/env bash
152-
readonly wksp="$(dirname "${{BASH_SOURCE[0]}}")"
153-
LD_LIBRARY_PATH=$wksp/{libs_dir} exec $wksp/usr/bin/bsdtar $@
154-
""".format(name = rctx.name, libs_dir = libs_dir))
83+
package(default_visibility = ["//visibility:public"])
15584
156-
rctx.file("BUILD.bazel", build_header + """\
157-
tar_toolchain(
158-
name = "bsdtar_toolchain",
159-
files = glob(["{libs}/*.so.*"]) + ["usr/bin/bsdtar"],
160-
binary = "bsdtar.sh",
161-
visibility = ["//visibility:public"],
162-
)
163-
""".format(libs = libs_dir, name = rctx.name))
85+
tar_toolchain(name = "bsdtar_toolchain", binary = "{}")
86+
""".format(binary))
16487

16588
bsdtar_binary_repo = repository_rule(
16689
implementation = _bsdtar_binary_repo,

0 commit comments

Comments
 (0)