Skip to content

Set --sysroot to sysroot generated by rust_toolchain #2223

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
7 changes: 7 additions & 0 deletions rust/private/rust.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -609,6 +609,13 @@ _common_attrs = {
"_error_format": attr.label(
default = Label("//:error_format"),
),
"_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."
),
),
"_extra_exec_rustc_flag": attr.label(
default = Label("//:extra_exec_rustc_flag"),
),
Expand Down
5 changes: 5 additions & 0 deletions rust/private/rustc.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ load(
"make_static_lib_symlink",
"relativize",
)
load("//rust/settings:incompatible.bzl", "IncompatibleFlagInfo")

BuildInfo = _BuildInfo

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

if hasattr(attr, "_experimental_toolchain_generated_sysroot"):
if attr._experimental_toolchain_generated_sysroot[IncompatibleFlagInfo].enabled == True:
rustc_flags.add("--sysroot", toolchain.sysroot)

# Tell Rustc where to find the standard library (or libcore)
rustc_flags.add_all(toolchain.rust_std_paths, before_each = "-L", format_each = "%s")
rustc_flags.add_all(rust_flags)
Expand Down
11 changes: 7 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 Expand Up @@ -325,6 +321,13 @@ rust_doc = rule(
"_error_format": attr.label(
default = Label("//:error_format"),
),
"_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."
),
),
"_process_wrapper": attr.label(
doc = "A process wrapper for running rustdoc on all platforms",
default = Label("@rules_rust//util/process_wrapper"),
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",
)
13 changes: 13 additions & 0 deletions test/toolchain/toolchain_test.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,25 @@ def _toolchain_adds_rustc_flags_impl(ctx):
"Found exec toolchain flag ({}) in rustc flags: {}".format(EXEC_TOOLCHAIN_FLAG, action.argv),
)

asserts.true(
env,
"--sysroot" in action.argv,
"Missing --sysroot flag",
)

asserts.true(
env,
action.argv[action.argv.index("--sysroot") + 1].endswith("test/toolchain/rust_extra_flags_toolchain"),
"--sysroot does not set to directory generated by rust_toolchain",
)

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