Skip to content

Commit 4aafa0a

Browse files
author
Vinh Tran
authored
Set --sysroot to sysroot generated by rust_toolchain (#2223)
Addresses #2039
1 parent 841a733 commit 4aafa0a

File tree

5 files changed

+36
-5
lines changed

5 files changed

+36
-5
lines changed

.bazelci/presubmit.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,8 +92,10 @@ tasks:
9292
test_targets: *default_linux_targets
9393
build_flags:
9494
- "--compilation_mode=opt"
95+
- "--@rules_rust//rust/settings:experimental_toolchain_generated_sysroot=True"
9596
test_flags:
9697
- "--compilation_mode=opt"
98+
- "--@rules_rust//rust/settings:experimental_toolchain_generated_sysroot=True"
9799
macos_opt:
98100
name: Opt Mode
99101
platform: macos

rust/private/rustdoc.bzl

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -137,10 +137,6 @@ def rustdoc_compile_action(
137137
if "OUT_DIR" in env:
138138
env.update({"OUT_DIR": "${{pwd}}/{}".format(build_info.out_dir.short_path)})
139139

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-
144140
return struct(
145141
executable = ctx.executable._process_wrapper,
146142
inputs = depset([crate_info.output], transitive = [compile_inputs]),

rust/settings/BUILD.bazel

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
load("@bazel_skylib//:bzl_library.bzl", "bzl_library")
22
load("@bazel_skylib//rules:common_settings.bzl", "bool_flag", "string_flag")
3+
load(":incompatible.bzl", "incompatible_flag")
34

45
package(default_visibility = ["//visibility:public"])
56

@@ -63,3 +64,10 @@ bzl_library(
6364
name = "bzl_lib",
6465
srcs = glob(["**/*.bzl"]),
6566
)
67+
68+
# A flag to set rustc --sysroot flag to the sysroot generated by rust_toolchain
69+
incompatible_flag(
70+
name = "experimental_toolchain_generated_sysroot",
71+
build_setting_default = False,
72+
issue = "https://github.com/bazelbuild/rules_rust/issues/2039",
73+
)

rust/toolchain.bzl

Lines changed: 15 additions & 1 deletion
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,12 @@ def _rust_toolchain_impl(ctx):
537538
sysroot_path = sysroot.sysroot_anchor.dirname
538539
sysroot_short_path, _, _ = sysroot.sysroot_anchor.short_path.rpartition("/")
539540

541+
# Override "rustc --print sysroot" with sysroot generated by `_generate_sysroot`
542+
# in this rule implementation
543+
rustc_flags = ctx.attr.extra_rustc_flags
544+
if ctx.attr._experimental_toolchain_generated_sysroot[IncompatibleFlagInfo].enabled == True:
545+
rustc_flags = ["--sysroot=" + sysroot_path] + rustc_flags
546+
540547
# Variables for make variable expansion
541548
make_variables = {
542549
"RUSTC": sysroot.rustc.path,
@@ -623,7 +630,7 @@ def _rust_toolchain_impl(ctx):
623630
rustfmt = sysroot.rustfmt,
624631
staticlib_ext = ctx.attr.staticlib_ext,
625632
stdlib_linkflags = stdlib_linkflags_cc_info,
626-
extra_rustc_flags = ctx.attr.extra_rustc_flags,
633+
extra_rustc_flags = rustc_flags,
627634
extra_exec_rustc_flags = ctx.attr.extra_exec_rustc_flags,
628635
per_crate_rustc_flags = ctx.attr.per_crate_rustc_flags,
629636
sysroot = sysroot_path,
@@ -787,6 +794,13 @@ rust_toolchain = rule(
787794
"_cc_toolchain": attr.label(
788795
default = Label("@bazel_tools//tools/cpp:current_cc_toolchain"),
789796
),
797+
"_experimental_toolchain_generated_sysroot": attr.label(
798+
default = Label("//rust/settings:experimental_toolchain_generated_sysroot"),
799+
doc = (
800+
"Label to a boolean build setting that lets the rule knows wheter to set --sysroot to rustc" +
801+
"This flag is only relevant when used together with --@rules_rust//rust/settings:experimental_toolchain_generated_sysroot."
802+
),
803+
),
790804
"_experimental_use_coverage_metadata_files": attr.label(
791805
default = Label("//rust/settings:experimental_use_coverage_metadata_files"),
792806
),

test/toolchain/toolchain_test.bzl

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,12 +36,23 @@ def _toolchain_adds_rustc_flags_impl(ctx):
3636
"Found exec toolchain flag ({}) in rustc flags: {}".format(EXEC_TOOLCHAIN_FLAG, action.argv),
3737
)
3838

39+
found_sysroot = False
40+
for arg in action.argv:
41+
if arg.startswith("--sysroot") and arg.endswith("test/toolchain/rust_extra_flags_toolchain"):
42+
found_sysroot = True
43+
asserts.true(
44+
env,
45+
found_sysroot,
46+
"Missing --sysroot flag or --sysroot does not point to correct sysroot directory",
47+
)
48+
3949
return analysistest.end(env)
4050

4151
toolchain_adds_rustc_flags_test = analysistest.make(
4252
_toolchain_adds_rustc_flags_impl,
4353
config_settings = {
4454
str(Label("//:extra_rustc_flags")): [CONFIG_FLAG],
55+
str(Label("//rust/settings:experimental_toolchain_generated_sysroot")): True,
4556
},
4657
)
4758

0 commit comments

Comments
 (0)