Skip to content

Commit

Permalink
Set --sysroot to sysroot generated by rust_toolchain (#2223)
Browse files Browse the repository at this point in the history
Addresses #2039
  • Loading branch information
Vinh Tran authored Oct 31, 2023
1 parent 841a733 commit 4aafa0a
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 5 deletions.
2 changes: 2 additions & 0 deletions .bazelci/presubmit.yml
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,10 @@ tasks:
test_targets: *default_linux_targets
build_flags:
- "--compilation_mode=opt"
- "--@rules_rust//rust/settings:experimental_toolchain_generated_sysroot=True"
test_flags:
- "--compilation_mode=opt"
- "--@rules_rust//rust/settings:experimental_toolchain_generated_sysroot=True"
macos_opt:
name: Opt Mode
platform: macos
Expand Down
4 changes: 0 additions & 4 deletions rust/private/rustdoc.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -137,10 +137,6 @@ def rustdoc_compile_action(
if "OUT_DIR" in env:
env.update({"OUT_DIR": "${{pwd}}/{}".format(build_info.out_dir.short_path)})

# `rustdoc` does not support the SYSROOT environment variable. To account
# for this, the flag must be explicitly passed to the `rustdoc` binary.
args.rustc_flags.add(toolchain.sysroot_short_path, format = "--sysroot=${{pwd}}/%s")

return struct(
executable = ctx.executable._process_wrapper,
inputs = depset([crate_info.output], transitive = [compile_inputs]),
Expand Down
8 changes: 8 additions & 0 deletions rust/settings/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
load("@bazel_skylib//:bzl_library.bzl", "bzl_library")
load("@bazel_skylib//rules:common_settings.bzl", "bool_flag", "string_flag")
load(":incompatible.bzl", "incompatible_flag")

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

Expand Down Expand Up @@ -63,3 +64,10 @@ bzl_library(
name = "bzl_lib",
srcs = glob(["**/*.bzl"]),
)

# A flag to set rustc --sysroot flag to the sysroot generated by rust_toolchain
incompatible_flag(
name = "experimental_toolchain_generated_sysroot",
build_setting_default = False,
issue = "https://github.com/bazelbuild/rules_rust/issues/2039",
)
16 changes: 15 additions & 1 deletion rust/toolchain.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ load(
"find_cc_toolchain",
"make_static_lib_symlink",
)
load("//rust/settings:incompatible.bzl", "IncompatibleFlagInfo")

rust_analyzer_toolchain = _rust_analyzer_toolchain
rustfmt_toolchain = _rustfmt_toolchain
Expand Down Expand Up @@ -537,6 +538,12 @@ def _rust_toolchain_impl(ctx):
sysroot_path = sysroot.sysroot_anchor.dirname
sysroot_short_path, _, _ = sysroot.sysroot_anchor.short_path.rpartition("/")

# Override "rustc --print sysroot" with sysroot generated by `_generate_sysroot`
# in this rule implementation
rustc_flags = ctx.attr.extra_rustc_flags
if ctx.attr._experimental_toolchain_generated_sysroot[IncompatibleFlagInfo].enabled == True:
rustc_flags = ["--sysroot=" + sysroot_path] + rustc_flags

# Variables for make variable expansion
make_variables = {
"RUSTC": sysroot.rustc.path,
Expand Down Expand Up @@ -623,7 +630,7 @@ def _rust_toolchain_impl(ctx):
rustfmt = sysroot.rustfmt,
staticlib_ext = ctx.attr.staticlib_ext,
stdlib_linkflags = stdlib_linkflags_cc_info,
extra_rustc_flags = ctx.attr.extra_rustc_flags,
extra_rustc_flags = rustc_flags,
extra_exec_rustc_flags = ctx.attr.extra_exec_rustc_flags,
per_crate_rustc_flags = ctx.attr.per_crate_rustc_flags,
sysroot = sysroot_path,
Expand Down Expand Up @@ -787,6 +794,13 @@ rust_toolchain = rule(
"_cc_toolchain": attr.label(
default = Label("@bazel_tools//tools/cpp:current_cc_toolchain"),
),
"_experimental_toolchain_generated_sysroot": attr.label(
default = Label("//rust/settings:experimental_toolchain_generated_sysroot"),
doc = (
"Label to a boolean build setting that lets the rule knows wheter to set --sysroot to rustc" +
"This flag is only relevant when used together with --@rules_rust//rust/settings:experimental_toolchain_generated_sysroot."
),
),
"_experimental_use_coverage_metadata_files": attr.label(
default = Label("//rust/settings:experimental_use_coverage_metadata_files"),
),
Expand Down
11 changes: 11 additions & 0 deletions test/toolchain/toolchain_test.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,23 @@ def _toolchain_adds_rustc_flags_impl(ctx):
"Found exec toolchain flag ({}) in rustc flags: {}".format(EXEC_TOOLCHAIN_FLAG, action.argv),
)

found_sysroot = False
for arg in action.argv:
if arg.startswith("--sysroot") and arg.endswith("test/toolchain/rust_extra_flags_toolchain"):
found_sysroot = True
asserts.true(
env,
found_sysroot,
"Missing --sysroot flag or --sysroot does not point to correct sysroot directory",
)

return analysistest.end(env)

toolchain_adds_rustc_flags_test = analysistest.make(
_toolchain_adds_rustc_flags_impl,
config_settings = {
str(Label("//:extra_rustc_flags")): [CONFIG_FLAG],
str(Label("//rust/settings:experimental_toolchain_generated_sysroot")): True,
},
)

Expand Down

0 comments on commit 4aafa0a

Please sign in to comment.