diff --git a/BUILD.bazel b/BUILD.bazel index 69e43c7e91..a460e94bf7 100644 --- a/BUILD.bazel +++ b/BUILD.bazel @@ -1,5 +1,5 @@ load("@bazel_skylib//:bzl_library.bzl", "bzl_library") -load("//rust:defs.bzl", "capture_clippy_output", "clippy_flags", "error_format", "extra_exec_rustc_flag", "extra_exec_rustc_flags", "extra_rustc_flag", "extra_rustc_flags", "is_proc_macro_dep", "is_proc_macro_dep_enabled") +load("//rust:defs.bzl", "capture_clippy_output", "clippy_flags", "error_format", "extra_exec_rustc_flag", "extra_exec_rustc_flags", "extra_rustc_flag", "extra_rustc_flags", "is_proc_macro_dep", "is_proc_macro_dep_enabled", "source_path_prefix") exports_files(["LICENSE"]) @@ -76,6 +76,15 @@ extra_exec_rustc_flag( visibility = ["//visibility:public"], ) +# This setting may be used to override the prefix for source paths rustc embeds into build artifacts. +# Setting this setting to a fixed value (e.g., "/source/") can help achieve reproducible builds and +# improve cache utilization. +source_path_prefix( + name = "source_path_prefix", + build_setting_default = ".", + visibility = ["//visibility:public"], +) + # This setting is used by the clippy rules. See https://bazelbuild.github.io/rules_rust/rust_clippy.html label_flag( name = "clippy.toml", diff --git a/docs/BUILD.bazel b/docs/BUILD.bazel index ebca2dcd0b..252bd19d55 100644 --- a/docs/BUILD.bazel +++ b/docs/BUILD.bazel @@ -56,6 +56,7 @@ PAGES = dict([ "error_format", "extra_rustc_flag", "extra_rustc_flags", + "source_path_prefix", "capture_clippy_output", ], ), diff --git a/docs/defs.md b/docs/defs.md index 577c7f6ea8..8d602f5edb 100644 --- a/docs/defs.md +++ b/docs/defs.md @@ -11,6 +11,7 @@ * [error_format](#error_format) * [extra_rustc_flag](#extra_rustc_flag) * [extra_rustc_flags](#extra_rustc_flags) +* [source_path_prefix](#source_path_prefix) * [capture_clippy_output](#capture_clippy_output) @@ -586,6 +587,24 @@ Run the test with `bazel test //hello_lib:greeting_test`. | version | A version to inject in the cargo environment variable. | String | optional | "0.0.0" | + + +## source_path_prefix + +
+source_path_prefix(name) ++ +Specify the path for the compiler to remap the source path prefix in all output, including compiler diagnostics,debug information, and macro expansions with `--@rules_rust//:source_path_prefix`.Setting the prefix a fixed value enables reproducible builds that do not depend on the location of the source directory. + +**ATTRIBUTES** + + +| Name | Description | Type | Mandatory | Default | +| :------------- | :------------- | :------------- | :------------- | :------------- | +| name | A unique name for this target. | Name | required | | + + ## rust_test_suite diff --git a/docs/flatten.md b/docs/flatten.md index dba68680d1..435b654c3d 100644 --- a/docs/flatten.md +++ b/docs/flatten.md @@ -50,6 +50,7 @@ * [rust_wasm_bindgen_toolchain](#rust_wasm_bindgen_toolchain) * [rustfmt_aspect](#rustfmt_aspect) * [rustfmt_test](#rustfmt_test) +* [source_path_prefix](#source_path_prefix) @@ -1352,6 +1353,24 @@ A test rule for performing `rustfmt --check` on a set of targets | targets | Rust targets to run
rustfmt --check
on. | List of labels | optional | [] |
+
+
+## source_path_prefix
+
++source_path_prefix(name) ++ +Specify the path for the compiler to remap the source path prefix in all output, including compiler diagnostics,debug information, and macro expansions with `--@rules_rust//:source_path_prefix`.Setting the prefix a fixed value enables reproducible builds that do not depend on the location of the source directory. + +**ATTRIBUTES** + + +| Name | Description | Type | Mandatory | Default | +| :------------- | :------------- | :------------- | :------------- | :------------- | +| name | A unique name for this target. | Name | required | | + + ## CrateInfo diff --git a/docs/symbols.bzl b/docs/symbols.bzl index 42e2f20a47..34ee059b3c 100644 --- a/docs/symbols.bzl +++ b/docs/symbols.bzl @@ -64,6 +64,7 @@ load( _rust_test_suite = "rust_test_suite", _rustfmt_aspect = "rustfmt_aspect", _rustfmt_test = "rustfmt_test", + _source_path_prefix = "source_path_prefix", ) load( "@rules_rust//rust:repositories.bzl", @@ -163,6 +164,7 @@ rustfmt_test = _rustfmt_test error_format = _error_format extra_rustc_flag = _extra_rustc_flag extra_rustc_flags = _extra_rustc_flags +source_path_prefix = _source_path_prefix incompatible_flag = _incompatible_flag capture_clippy_output = _capture_clippy_output diff --git a/rust/defs.bzl b/rust/defs.bzl index 92c8a77406..d8a5631366 100644 --- a/rust/defs.bzl +++ b/rust/defs.bzl @@ -49,6 +49,7 @@ load( _extra_rustc_flags = "extra_rustc_flags", _is_proc_macro_dep = "is_proc_macro_dep", _is_proc_macro_dep_enabled = "is_proc_macro_dep_enabled", + _source_path_prefix = "source_path_prefix", ) load( "//rust/private:rustdoc.bzl", @@ -124,6 +125,9 @@ is_proc_macro_dep = _is_proc_macro_dep is_proc_macro_dep_enabled = _is_proc_macro_dep_enabled # See @rules_rust//rust/private:rustc.bzl for a complete description. +source_path_prefix = _source_path_prefix +# See @rules_rust//rust/private:rustc.bzl for a complete description. + rust_common = _rust_common # See @rules_rust//rust/private:common.bzl for a complete description. diff --git a/rust/private/rust.bzl b/rust/private/rust.bzl index 6ebfbea4c7..b2bfbe1fed 100644 --- a/rust/private/rust.bzl +++ b/rust/private/rust.bzl @@ -718,6 +718,9 @@ _common_attrs = { allow_single_file = True, cfg = "exec", ), + "_source_path_prefix": attr.label( + default = Label("//:source_path_prefix"), + ), "_stamp_flag": attr.label( doc = "A setting used to determine whether or not the `--stamp` flag is enabled", default = Label("//rust/private:stamp"), diff --git a/rust/private/rustc.bzl b/rust/private/rustc.bzl index 490bfa1d65..1868a52977 100644 --- a/rust/private/rustc.bzl +++ b/rust/private/rustc.bzl @@ -90,6 +90,11 @@ is_proc_macro_dep_enabled = rule( build_setting = config.bool(flag = True), ) +SourcePathPrefixInfo = provider( + doc = "Remap source path prefixes in all output, including compiler diagnostics, debug information, macro expansions to a path", + fields = {"source_path_prefix": "string The path to substitute ${pwd} for"}, +) + def _get_rustc_env(attr, toolchain, crate_name): """Gathers rustc environment variables @@ -708,7 +713,7 @@ def construct_arguments( force_all_deps_direct = False, force_link = False, stamp = False, - remap_path_prefix = ".", + remap_path_prefix = True, use_json_output = False, build_metadata = False, force_depend_on_objects = False): @@ -737,7 +742,7 @@ def construct_arguments( force_link (bool, optional): Whether to add link flags to the command regardless of `emit`. stamp (bool, optional): Whether or not workspace status stamping is enabled. For more details see https://docs.bazel.build/versions/main/user-manual.html#flag--stamp - remap_path_prefix (str, optional): A value used to remap `${pwd}` to. If set to a falsey value, no prefix will be set. + remap_path_prefix (bool): Whether to apply the remap-path-prefix compiler setting. use_json_output (bool): Have rustc emit json and process_wrapper parse json messages to output rendered output. build_metadata (bool): Generate CLI arguments for building *only* .rmeta files. This requires use_json_output. force_depend_on_objects (bool): Force using `.rlib` object files instead of metadata (`.rmeta`) files even if they are available. @@ -874,8 +879,8 @@ def construct_arguments( rustc_flags.add("--codegen=debuginfo=" + compilation_mode.debug_info) # For determinism to help with build distribution and such - if remap_path_prefix: - rustc_flags.add("--remap-path-prefix=${{pwd}}={}".format(remap_path_prefix)) + if remap_path_prefix and hasattr(ctx.attr, "_source_path_prefix"): + rustc_flags.add("--remap-path-prefix=${{pwd}}={}".format(ctx.attr._source_path_prefix[SourcePathPrefixInfo].source_path_prefix)) if emit: rustc_flags.add("--emit=" + ",".join(emit_with_paths)) @@ -1838,6 +1843,19 @@ extra_rustc_flag = rule( build_setting = config.string(flag = True, allow_multiple = True), ) +def _source_path_prefix_impl(ctx): + return SourcePathPrefixInfo(source_path_prefix = ctx.build_setting_value) + +source_path_prefix = rule( + doc = ( + "Specify the path for the compiler to remap the source path prefix in all output, including compiler diagnostics," + + "debug information, and macro expansions with `--@rules_rust//:source_path_prefix`." + + "Setting the prefix a fixed value enables reproducible builds that do not depend on the location of the source directory." + ), + implementation = _source_path_prefix_impl, + build_setting = config.string(flag = True), +) + def _extra_exec_rustc_flags_impl(ctx): return ExtraExecRustcFlagsInfo(extra_exec_rustc_flags = ctx.build_setting_value) diff --git a/rust/private/rustdoc.bzl b/rust/private/rustdoc.bzl index ba3a12cec7..b9699f0c29 100644 --- a/rust/private/rustdoc.bzl +++ b/rust/private/rustdoc.bzl @@ -120,7 +120,7 @@ def rustdoc_compile_action( build_env_files = build_env_files, build_flags_files = build_flags_files, emit = [], - remap_path_prefix = None, + remap_path_prefix = False, force_link = True, force_depend_on_objects = is_test, )