Skip to content

Commit ac7378f

Browse files
authored
Use the rust-src tool for fetching rust source (#844)
This tool only includes the relevant rust source files, significantly slimming the size of the download, and significantly decreasing the number of bazel targets generated. The concept of a "host tool" is introduced, which is targetless, to accommodate this change.
1 parent 19acfd8 commit ac7378f

File tree

6 files changed

+91
-59
lines changed

6 files changed

+91
-59
lines changed

rust/known_shas.bzl

Lines changed: 37 additions & 48 deletions
Large diffs are not rendered by default.

rust/private/repository_utils.bzl

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -316,11 +316,11 @@ def load_rust_src(ctx):
316316
Args:
317317
ctx (ctx): A repository_ctx.
318318
"""
319-
tool_suburl = produce_tool_suburl("rustc", "src", ctx.attr.version, ctx.attr.iso_date)
319+
tool_suburl = produce_tool_suburl("rust-src", None, ctx.attr.version, ctx.attr.iso_date)
320320
static_rust = ctx.os.environ.get("STATIC_RUST_URL", "https://static.rust-lang.org")
321321
url = "{}/dist/{}.tar.gz".format(static_rust, tool_suburl)
322322

323-
tool_path = produce_tool_path("rustc", "src", ctx.attr.version)
323+
tool_path = produce_tool_path("rust-src", None, ctx.attr.version)
324324
archive_path = tool_path + ".tar.gz"
325325
ctx.download(
326326
url,
@@ -330,7 +330,7 @@ def load_rust_src(ctx):
330330
ctx.extract(
331331
archive_path,
332332
output = "lib/rustlib/src",
333-
stripPrefix = tool_path,
333+
stripPrefix = "{}/rust-src/lib/rustlib/src/rust".format(tool_path),
334334
)
335335
ctx.file(
336336
"lib/rustlib/src/BUILD.bazel",
@@ -463,12 +463,12 @@ def produce_tool_suburl(tool_name, target_triple, version, iso_date = None):
463463
target_triple: The rust-style target triple of the tool
464464
version: The version of the tool among "nightly", "beta', or an exact version.
465465
iso_date: The date of the tool (or None, if the version is a specific version).
466-
"""
467466
468-
if iso_date:
469-
return "{}/{}-{}-{}".format(iso_date, tool_name, version, target_triple)
470-
else:
471-
return "{}-{}-{}".format(tool_name, version, target_triple)
467+
Returns:
468+
str: The fully qualified url path for the specified tool.
469+
"""
470+
path = produce_tool_path(tool_name, target_triple, version)
471+
return iso_date + "/" + path if iso_date else path
472472

473473
def produce_tool_path(tool_name, target_triple, version):
474474
"""Produces a qualified Rust tool name
@@ -477,9 +477,15 @@ def produce_tool_path(tool_name, target_triple, version):
477477
tool_name: The name of the tool per static.rust-lang.org
478478
target_triple: The rust-style target triple of the tool
479479
version: The version of the tool among "nightly", "beta', or an exact version.
480-
"""
481480
482-
return "{}-{}-{}".format(tool_name, version, target_triple)
481+
Returns:
482+
str: The qualified path for the specified tool.
483+
"""
484+
if not tool_name:
485+
fail("No tool name was provided")
486+
if not version:
487+
fail("No tool version was provided")
488+
return "-".join([e for e in [tool_name, version, target_triple] if e])
483489

484490
def load_arbitrary_tool(ctx, tool_name, tool_subdirectories, version, iso_date, target_triple, sha256 = ""):
485491
"""Loads a Rust tool, downloads, and extracts into the common workspace.

test/unit/repository_utils/repository_utils_test.bzl

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,25 @@ def _produce_tool_suburl_test_impl(ctx):
2626
target_triple = "x86_64-unknown-linux-gnu",
2727
),
2828
)
29+
asserts.equals(
30+
env,
31+
"2020-05-22/rust-src-nightly",
32+
produce_tool_suburl(
33+
iso_date = "2020-05-22",
34+
tool_name = "rust-src",
35+
version = "nightly",
36+
target_triple = None,
37+
),
38+
)
39+
asserts.equals(
40+
env,
41+
"rust-src-nightly",
42+
produce_tool_suburl(
43+
tool_name = "rust-src",
44+
version = "nightly",
45+
target_triple = None,
46+
),
47+
)
2948
return unittest.end(env)
3049

3150
def _produce_tool_path_test_impl(ctx):
@@ -39,6 +58,15 @@ def _produce_tool_path_test_impl(ctx):
3958
target_triple = "x86_64-unknown-linux-gnu",
4059
),
4160
)
61+
asserts.equals(
62+
env,
63+
"rust-src-nightly",
64+
produce_tool_path(
65+
tool_name = "rust-src",
66+
version = "nightly",
67+
target_triple = None,
68+
),
69+
)
4270
return unittest.end(env)
4371

4472
produce_tool_suburl_test = unittest.make(_produce_tool_suburl_test_impl)

util/fetch_shas.sh

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ if [[ -z "${BUILD_WORKSPACE_DIRECTORY}" ]]; then
1212
fi
1313

1414
TOOLS="$(cat "${BUILD_WORKSPACE_DIRECTORY}/util/fetch_shas_TOOLS.txt")"
15+
HOST_TOOLS="$(cat "${BUILD_WORKSPACE_DIRECTORY}/util/fetch_shas_HOST_TOOLS.txt")"
1516
TARGETS="$(cat "${BUILD_WORKSPACE_DIRECTORY}/util/fetch_shas_TARGETS.txt")"
1617
VERSIONS="$(cat "${BUILD_WORKSPACE_DIRECTORY}/util/fetch_shas_VERSIONS.txt")"
1718
BETA_ISO_DATES="$(cat "${BUILD_WORKSPACE_DIRECTORY}/util/fetch_shas_BETA_ISO_DATES.txt")"
@@ -39,6 +40,14 @@ enumerate_keys() {
3940
done
4041
done
4142
done
43+
44+
for HOST_TOOL in $HOST_TOOLS
45+
do
46+
for VERSION in $VERSIONS
47+
do
48+
echo "$HOST_TOOL-$VERSION"
49+
done
50+
done
4251
}
4352

4453
emit_bzl_file_contents() {

util/fetch_shas_HOST_TOOLS.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
rust-src

util/fetch_shas_TARGETS.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
aarch64-apple-darwin
22
aarch64-unknown-linux-gnu
33
aarch64-unknown-linux-musl
4-
src
54
wasm32-unknown-unknown
65
wasm32-wasi
76
x86_64-apple-darwin

0 commit comments

Comments
 (0)