Skip to content

Commit 00078eb

Browse files
author
Vinh Tran
committed
Set --sysroot in construct_arguments
1 parent 81f8922 commit 00078eb

File tree

4 files changed

+22
-21
lines changed

4 files changed

+22
-21
lines changed

rust/private/rust.bzl

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -634,6 +634,9 @@ _common_attrs = {
634634
"_per_crate_rustc_flag": attr.label(
635635
default = Label("//:experimental_per_crate_rustc_flag"),
636636
),
637+
"_experimental_toolchain_generated_sysroot": attr.label(
638+
default = Label("//rust/settings:experimental_toolchain_generated_sysroot"),
639+
),
637640
"_process_wrapper": attr.label(
638641
doc = "A process wrapper for running rustc on all platforms.",
639642
default = Label("//util/process_wrapper"),

rust/private/rustc.bzl

Lines changed: 10 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -788,7 +788,8 @@ def construct_arguments(
788788
use_json_output = False,
789789
build_metadata = False,
790790
force_depend_on_objects = False,
791-
skip_expanding_rustc_env = False):
791+
skip_expanding_rustc_env = False,
792+
sandboxed = True):
792793
"""Builds an Args object containing common rustc flags
793794
794795
Args:
@@ -965,6 +966,11 @@ def construct_arguments(
965966
if linker_script:
966967
rustc_flags.add(linker_script, format = "--codegen=link-arg=-T%s")
967968

969+
if sandboxed:
970+
rustc_flags.add("--sysroot", toolchain.sysroot)
971+
elif hasattr(ctx.attr, "_experimental_toolchain_generated_sysroot") and getattr(ctx.attr, "_experimental_toolchain_generated_sysroot") == True:
972+
rustc_flags.add(toolchain.sysroot_short_path, format = "--sysroot=${{pwd}}/%s")
973+
968974
# Tell Rustc where to find the standard library (or libcore)
969975
rustc_flags.add_all(toolchain.rust_std_paths, before_each = "-L", format_each = "%s")
970976
rustc_flags.add_all(rust_flags)
@@ -1264,22 +1270,14 @@ def rustc_compile_action(
12641270
dsym_folder = ctx.actions.declare_directory(crate_info.output.basename + ".dSYM", sibling = crate_info.output)
12651271
action_outputs.append(dsym_folder)
12661272

1267-
rustc_sysroot_arg = ctx.actions.args()
1268-
rustc_sysroot_arg.add("--sysroot", toolchain.sysroot)
1269-
12701273
if ctx.executable._process_wrapper:
12711274
# Run as normal
12721275
ctx.actions.run(
12731276
executable = ctx.executable._process_wrapper,
12741277
inputs = compile_inputs,
12751278
outputs = action_outputs,
12761279
env = env,
1277-
arguments = [
1278-
args.process_wrapper_flags,
1279-
args.rustc_path,
1280-
rustc_sysroot_arg,
1281-
args.rustc_flags,
1282-
],
1280+
arguments = args.all,
12831281
mnemonic = "Rustc",
12841282
progress_message = "Compiling Rust {} {}{} ({} files)".format(
12851283
crate_info.type,
@@ -1295,12 +1293,7 @@ def rustc_compile_action(
12951293
inputs = compile_inputs,
12961294
outputs = [build_metadata],
12971295
env = env,
1298-
arguments = [
1299-
args_metadata.process_wrapper_flags,
1300-
args_metadata.rustc_path,
1301-
rustc_sysroot_arg,
1302-
args_metadata.rustc_flags,
1303-
],
1296+
arguments = args_metadata.all,
13041297
mnemonic = "RustcMetadata",
13051298
progress_message = "Compiling Rust metadata {} {}{} ({} files)".format(
13061299
crate_info.type,
@@ -1319,7 +1312,7 @@ def rustc_compile_action(
13191312
inputs = compile_inputs,
13201313
outputs = action_outputs,
13211314
env = env,
1322-
arguments = [args.rustc_path, rustc_sysroot_arg, args.rustc_flags],
1315+
arguments = [args.rustc_path, args.rustc_flags],
13231316
mnemonic = "Rustc",
13241317
progress_message = "Compiling Rust (without process_wrapper) {} {}{} ({} files)".format(
13251318
crate_info.type,

rust/private/rustdoc.bzl

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,9 @@ def rustdoc_compile_action(
126126
rustdoc = True,
127127
force_depend_on_objects = is_test,
128128
skip_expanding_rustc_env = True,
129+
is_test = True,
130+
# rustdoc tests compile tests outside of the sandbox
131+
sandboxed = not is_test,
129132
)
130133

131134
# Because rustdoc tests compile tests outside of the sandbox, the sysroot
@@ -137,10 +140,6 @@ def rustdoc_compile_action(
137140
if "OUT_DIR" in env:
138141
env.update({"OUT_DIR": "${{pwd}}/{}".format(build_info.out_dir.short_path)})
139142

140-
# `rustdoc` does not support the SYSROOT environment variable. To account
141-
# for this, the flag must be explicitly passed to the `rustdoc` binary.
142-
args.rustc_flags.add(toolchain.sysroot_short_path, format = "--sysroot=${{pwd}}/%s")
143-
144143
return struct(
145144
executable = ctx.executable._process_wrapper,
146145
inputs = depset([crate_info.output], transitive = [compile_inputs]),

rust/settings/BUILD.bazel

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,3 +63,9 @@ bzl_library(
6363
name = "bzl_lib",
6464
srcs = glob(["**/*.bzl"]),
6565
)
66+
67+
# A flag to set rustc --sysroot flag to the sysroot generated by rust_toolchain
68+
bool_flag(
69+
name = "experimental_toolchain_generated_sysroot",
70+
build_setting_default = False,
71+
)

0 commit comments

Comments
 (0)