Skip to content

Commit d1bb016

Browse files
author
Vinh Tran
committed
Add --sysroot in rust_toolchain rule instead
1 parent 6eb7a7c commit d1bb016

File tree

5 files changed

+20
-27
lines changed

5 files changed

+20
-27
lines changed

rust/private/rust.bzl

-7
Original file line numberDiff line numberDiff line change
@@ -609,13 +609,6 @@ _common_attrs = {
609609
"_error_format": attr.label(
610610
default = Label("//:error_format"),
611611
),
612-
"_experimental_toolchain_generated_sysroot": attr.label(
613-
default = Label("//rust/settings:experimental_toolchain_generated_sysroot"),
614-
doc = (
615-
"Label to a boolean build setting that lets the rule knows wheter to set --sysroot to rustc" +
616-
"This flag is only relevant when used together with --@rules_rust//rust/settings:experimental_toolchain_generated_sysroot."
617-
),
618-
),
619612
"_extra_exec_rustc_flag": attr.label(
620613
default = Label("//:extra_exec_rustc_flag"),
621614
),

rust/private/rustc.bzl

-5
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@ load(
3838
"make_static_lib_symlink",
3939
"relativize",
4040
)
41-
load("//rust/settings:incompatible.bzl", "IncompatibleFlagInfo")
4241

4342
BuildInfo = _BuildInfo
4443

@@ -966,10 +965,6 @@ def construct_arguments(
966965
if linker_script:
967966
rustc_flags.add(linker_script, format = "--codegen=link-arg=-T%s")
968967

969-
if hasattr(attr, "_experimental_toolchain_generated_sysroot"):
970-
if attr._experimental_toolchain_generated_sysroot[IncompatibleFlagInfo].enabled == True:
971-
rustc_flags.add("--sysroot", toolchain.sysroot)
972-
973968
# Tell Rustc where to find the standard library (or libcore)
974969
rustc_flags.add_all(toolchain.rust_std_paths, before_each = "-L", format_each = "%s")
975970
rustc_flags.add_all(rust_flags)

rust/private/rustdoc.bzl

-7
Original file line numberDiff line numberDiff line change
@@ -321,13 +321,6 @@ rust_doc = rule(
321321
"_error_format": attr.label(
322322
default = Label("//:error_format"),
323323
),
324-
"_experimental_toolchain_generated_sysroot": attr.label(
325-
default = Label("//rust/settings:experimental_toolchain_generated_sysroot"),
326-
doc = (
327-
"Label to a boolean build setting that lets the rule knows wheter to set --sysroot to rustc" +
328-
"This flag is only relevant when used together with --@rules_rust//rust/settings:experimental_toolchain_generated_sysroot."
329-
),
330-
),
331324
"_process_wrapper": attr.label(
332325
doc = "A process wrapper for running rustdoc on all platforms",
333326
default = Label("@rules_rust//util/process_wrapper"),

rust/toolchain.bzl

+13-1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ load(
1616
"find_cc_toolchain",
1717
"make_static_lib_symlink",
1818
)
19+
load("//rust/settings:incompatible.bzl", "IncompatibleFlagInfo")
1920

2021
rust_analyzer_toolchain = _rust_analyzer_toolchain
2122
rustfmt_toolchain = _rustfmt_toolchain
@@ -537,6 +538,10 @@ def _rust_toolchain_impl(ctx):
537538
sysroot_path = sysroot.sysroot_anchor.dirname
538539
sysroot_short_path, _, _ = sysroot.sysroot_anchor.short_path.rpartition("/")
539540

541+
rustc_flags = ctx.attr.extra_rustc_flags
542+
if ctx.attr._experimental_toolchain_generated_sysroot[IncompatibleFlagInfo].enabled == True:
543+
rustc_flags = ["--sysroot=" + sysroot_path] + rustc_flags
544+
540545
# Variables for make variable expansion
541546
make_variables = {
542547
"RUSTC": sysroot.rustc.path,
@@ -623,7 +628,7 @@ def _rust_toolchain_impl(ctx):
623628
rustfmt = sysroot.rustfmt,
624629
staticlib_ext = ctx.attr.staticlib_ext,
625630
stdlib_linkflags = stdlib_linkflags_cc_info,
626-
extra_rustc_flags = ctx.attr.extra_rustc_flags,
631+
extra_rustc_flags = rustc_flags,
627632
extra_exec_rustc_flags = ctx.attr.extra_exec_rustc_flags,
628633
per_crate_rustc_flags = ctx.attr.per_crate_rustc_flags,
629634
sysroot = sysroot_path,
@@ -787,6 +792,13 @@ rust_toolchain = rule(
787792
"_cc_toolchain": attr.label(
788793
default = Label("@bazel_tools//tools/cpp:current_cc_toolchain"),
789794
),
795+
"_experimental_toolchain_generated_sysroot": attr.label(
796+
default = Label("//rust/settings:experimental_toolchain_generated_sysroot"),
797+
doc = (
798+
"Label to a boolean build setting that lets the rule knows wheter to set --sysroot to rustc" +
799+
"This flag is only relevant when used together with --@rules_rust//rust/settings:experimental_toolchain_generated_sysroot."
800+
),
801+
),
790802
"_experimental_use_coverage_metadata_files": attr.label(
791803
default = Label("//rust/settings:experimental_use_coverage_metadata_files"),
792804
),

test/toolchain/toolchain_test.bzl

+7-7
Original file line numberDiff line numberDiff line change
@@ -36,16 +36,16 @@ def _toolchain_adds_rustc_flags_impl(ctx):
3636
"Found exec toolchain flag ({}) in rustc flags: {}".format(EXEC_TOOLCHAIN_FLAG, action.argv),
3737
)
3838

39-
asserts.true(
40-
env,
41-
"--sysroot" in action.argv,
42-
"Missing --sysroot flag",
43-
)
39+
found_sysroot = False
40+
41+
for arg in action.argv:
42+
if arg.startswith("--sysroot") and arg.endswith("test/toolchain/rust_extra_flags_toolchain"):
43+
found_sysroot = True
4444

4545
asserts.true(
4646
env,
47-
action.argv[action.argv.index("--sysroot") + 1].endswith("test/toolchain/rust_extra_flags_toolchain"),
48-
"--sysroot does not set to directory generated by rust_toolchain",
47+
found_sysroot,
48+
"Missing --sysroot flag or --sysroot does not point to correct sysroot directory",
4949
)
5050

5151
return analysistest.end(env)

0 commit comments

Comments
 (0)