Skip to content

Commit 78fb953

Browse files
committed
Implement untaring in rust
This avoids relying on the system path The exposed `untar` attribute is pretty ugly, but I'm not sure the best alternative. We need to be able to access `untar` from anything which has an `out_dir_tar` attribute, which may be any of our rules, but need to specifically not depend on it from the targets which make `untar` itself. This means it can't live in a toolchain. Ideally it'd be an attribute conditionally set only iff `out_dir_tar` is set, which we could do in a macro, but introducing a layer of macros to the rules just to make our handful of bootstrap rust_libraries not need to fake out the attribute seems overkill. The binary will only _actually_ be depended on and built if it's needed.
1 parent 1d58102 commit 78fb953

File tree

23 files changed

+407
-10
lines changed

23 files changed

+407
-10
lines changed

WORKSPACE

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ new_git_repository(
3232
name = "libc",
3333
build_file = "@io_bazel_rules_rust//:libc.BUILD",
3434
remote = "https://github.com/rust-lang/libc",
35-
tag = "0.2.20",
35+
tag = "0.2.71",
3636
)
3737

3838
load("@io_bazel_rules_rust//proto:repositories.bzl", "rust_proto_repositories")
@@ -102,3 +102,6 @@ bazel_version(name = "bazel_version")
102102

103103
load("@examples//hello_sys:workspace.bzl", "remote_deps")
104104
remote_deps()
105+
106+
load("//tar_wrapper/remote_crates:remote_crates.bzl", "fetch_remote_crates")
107+
fetch_remote_crates()

cargo/cargo_build_script_runner/BUILD

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,5 +14,8 @@ rust_binary(
1414
name = "cargo_build_script_runner",
1515
srcs = ["bin.rs"],
1616
visibility = ["//visibility:public"],
17-
deps = [":cargo_build_script_output_parser"],
17+
deps = [
18+
":cargo_build_script_output_parser",
19+
"//tar_wrapper",
20+
],
1821
)

cargo/cargo_build_script_runner/bin.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
// A simple wrapper around a build_script execution to generate file to reuse
1616
// by rust_library/rust_binary.
1717
extern crate cargo_build_script_output_parser;
18+
extern crate tar_wrapper;
1819

1920
use cargo_build_script_output_parser::{BuildScriptOutput, CompileAndLinkFlags};
2021
use std::env;

libc.BUILD

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,5 @@ rust_library(
44
name = "libc",
55
srcs = glob(["src/**/*.rs"]),
66
visibility = ["//visibility:public"],
7+
untar = "@io_bazel_rules_rust//tar_wrapper:false",
78
)

rust/private/clippy.bzl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ def _clippy_aspect_impl(target, ctx):
6060
toolchain,
6161
)
6262

63-
compile_inputs, prep_commands, dynamic_env, dynamic_build_flags = collect_inputs(
63+
compile_inputs, tool_inputs, prep_commands, dynamic_env, dynamic_build_flags = collect_inputs(
6464
ctx,
6565
ctx.rule.file,
6666
ctx.rule.files,
@@ -111,7 +111,7 @@ def _clippy_aspect_impl(target, ctx):
111111
inputs = compile_inputs,
112112
outputs = [clippy_marker],
113113
env = env,
114-
tools = [toolchain.clippy_driver],
114+
tools = tool_inputs + [toolchain.clippy_driver],
115115
arguments = [args],
116116
mnemonic = "Clippy",
117117
)

rust/private/rust.bzl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -360,6 +360,7 @@ _rust_common_attrs = {
360360
],
361361
),
362362
"_cc_toolchain": attr.label(default = "@bazel_tools//tools/cpp:current_cc_toolchain"),
363+
"untar": attr.label(default = "@io_bazel_rules_rust//tar_wrapper:untar", cfg = "exec", allow_single_file = True, executable = True),
363364
}
364365

365366
_rust_library_attrs = {

rust/private/rustc.bzl

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -249,10 +249,10 @@ def _process_build_scripts(
249249
build_info,
250250
dep_info,
251251
compile_inputs):
252-
extra_inputs, prep_commands, dynamic_env, dynamic_build_flags = _create_out_dir_action(ctx, file, build_info, dep_info)
252+
extra_inputs, tool_inputs, prep_commands, dynamic_env, dynamic_build_flags = _create_out_dir_action(ctx, file, build_info, dep_info)
253253
if extra_inputs:
254254
compile_inputs = depset(extra_inputs, transitive = [compile_inputs])
255-
return compile_inputs, prep_commands, dynamic_env, dynamic_build_flags
255+
return compile_inputs, tool_inputs, prep_commands, dynamic_env, dynamic_build_flags
256256

257257
def collect_inputs(
258258
ctx,
@@ -441,7 +441,7 @@ def rustc_compile_action(
441441
toolchain,
442442
)
443443

444-
compile_inputs, prep_commands, dynamic_env, dynamic_build_flags = collect_inputs(
444+
compile_inputs, tool_inputs, prep_commands, dynamic_env, dynamic_build_flags = collect_inputs(
445445
ctx,
446446
ctx.file,
447447
ctx.files,
@@ -482,6 +482,7 @@ def rustc_compile_action(
482482
ctx.actions.run_shell(
483483
command = command,
484484
inputs = compile_inputs,
485+
tools = tool_inputs,
485486
outputs = [crate_info.output],
486487
env = env,
487488
arguments = [args],
@@ -524,12 +525,12 @@ def _create_out_dir_action(ctx, file, build_info, dep_info):
524525

525526
prep_commands = []
526527
input_files = []
528+
tool_inputs = []
527529
# Env vars and build flags which need to be set in the action's command line, rather than on the action's env,
528530
# because they rely on other env vars or commands.
529531
dynamic_env = {}
530532
dynamic_build_flags = []
531533

532-
# TODO: Remove system tar usage
533534
if build_info:
534535
prep_commands.append("export $(cat %s)" % build_info.rustc_env.path)
535536
# out_dir will be added as input by the transitive_build_infos loop below.
@@ -538,8 +539,9 @@ def _create_out_dir_action(ctx, file, build_info, dep_info):
538539
elif tar_file_attr:
539540
out_dir = ".out-dir"
540541
prep_commands.append("mkdir -p $OUT_DIR")
541-
prep_commands.append("tar -xzf {tar} -C $OUT_DIR".format(tar=tar_file_attr.path))
542+
prep_commands.append("{untar} {tar} $OUT_DIR".format(untar = file.untar.path, tar=tar_file_attr.path))
542543
input_files.append(tar_file_attr)
544+
tool_inputs.append(file.untar)
543545
dynamic_env["OUT_DIR"] = "${{EXEC_ROOT}}/{}".format(out_dir)
544546

545547
# This should probably only actually be exposed to actions which link.
@@ -548,7 +550,7 @@ def _create_out_dir_action(ctx, file, build_info, dep_info):
548550
dynamic_build_flags.append("$(cat '{}' | sed -e \"s#\${{EXEC_ROOT}}#${{EXEC_ROOT}}#g\")".format(dep_build_info.link_flags.path))
549551
input_files.append(dep_build_info.link_flags)
550552

551-
return input_files, prep_commands, dynamic_env, dynamic_build_flags
553+
return input_files, tool_inputs, prep_commands, dynamic_env, dynamic_build_flags
552554

553555
def _compute_rpaths(toolchain, output_dir, dep_info):
554556
"""

tar_wrapper/BUILD

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
load("//rust:rust.bzl", "rust_binary", "rust_library", "rust_test")
2+
3+
rust_library(
4+
name = "tar_wrapper",
5+
srcs = ["src/lib.rs"],
6+
deps = [
7+
"@flate2//:flate2",
8+
"@tar//:tar",
9+
],
10+
edition = "2018",
11+
visibility = ["//visibility:public"],
12+
untar = ":false",
13+
)
14+
15+
rust_binary(
16+
name = "untar",
17+
srcs = ["src/untar.rs"],
18+
deps = [":tar_wrapper"],
19+
edition = "2018",
20+
untar = ":false",
21+
visibility = ["//visibility:public"],
22+
)
23+
24+
rust_test(
25+
name = "test",
26+
crate = ":tar_wrapper",
27+
data = ["//test:example.tar.gz"],
28+
)
29+
30+
# Hacky script we use as the value of the `untar` attribute for rust_libraries used to build untar itself, which don't
31+
# actually need untar, to avoid dependency cycles.
32+
exports_files(["false"])

tar_wrapper/false

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
#!/bin/bash -eu
2+
echo >&2 "Tried to run bogus untar"
3+
exit 1

tar_wrapper/remote_crates/BUILD

Whitespace-only changes.

0 commit comments

Comments
 (0)