From 4aafa0af925b855432020e197e474bb180adf9e4 Mon Sep 17 00:00:00 2001 From: Vinh Tran Date: Tue, 31 Oct 2023 10:28:32 -0700 Subject: [PATCH] Set --sysroot to sysroot generated by rust_toolchain (#2223) Addresses https://github.com/bazelbuild/rules_rust/issues/2039 --- .bazelci/presubmit.yml | 2 ++ rust/private/rustdoc.bzl | 4 ---- rust/settings/BUILD.bazel | 8 ++++++++ rust/toolchain.bzl | 16 +++++++++++++++- test/toolchain/toolchain_test.bzl | 11 +++++++++++ 5 files changed, 36 insertions(+), 5 deletions(-) diff --git a/.bazelci/presubmit.yml b/.bazelci/presubmit.yml index e95a173c5b..d8e0b6eabc 100644 --- a/.bazelci/presubmit.yml +++ b/.bazelci/presubmit.yml @@ -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 diff --git a/rust/private/rustdoc.bzl b/rust/private/rustdoc.bzl index 1859c4aa3a..5fa82b8144 100644 --- a/rust/private/rustdoc.bzl +++ b/rust/private/rustdoc.bzl @@ -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]), diff --git a/rust/settings/BUILD.bazel b/rust/settings/BUILD.bazel index 0cf1253831..a86bb149bf 100644 --- a/rust/settings/BUILD.bazel +++ b/rust/settings/BUILD.bazel @@ -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"]) @@ -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", +) diff --git a/rust/toolchain.bzl b/rust/toolchain.bzl index 3a59d49d73..bc00ce3450 100644 --- a/rust/toolchain.bzl +++ b/rust/toolchain.bzl @@ -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 @@ -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, @@ -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, @@ -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"), ), diff --git a/test/toolchain/toolchain_test.bzl b/test/toolchain/toolchain_test.bzl index cfa429ebbf..5c74fae6d1 100644 --- a/test/toolchain/toolchain_test.bzl +++ b/test/toolchain/toolchain_test.bzl @@ -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, }, )