Skip to content
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

feat: make source path prefix configurable #1529

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
11 changes: 10 additions & 1 deletion BUILD.bazel
Original file line number Diff line number Diff line change
@@ -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"])

Expand Down Expand Up @@ -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",
Expand Down
1 change: 1 addition & 0 deletions docs/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ PAGES = dict([
"error_format",
"extra_rustc_flag",
"extra_rustc_flags",
"source_path_prefix",
"capture_clippy_output",
],
),
Expand Down
19 changes: 19 additions & 0 deletions docs/defs.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)

<a id="capture_clippy_output"></a>
Expand Down Expand Up @@ -586,6 +587,24 @@ Run the test with `bazel test //hello_lib:greeting_test`.
| <a id="rust_test-version"></a>version | A version to inject in the cargo environment variable. | String | optional | "0.0.0" |


<a id="source_path_prefix"></a>

## source_path_prefix

<pre>
source_path_prefix(<a href="#source_path_prefix-name">name</a>)
</pre>

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 |
| :------------- | :------------- | :------------- | :------------- | :------------- |
| <a id="source_path_prefix-name"></a>name | A unique name for this target. | <a href="https://bazel.build/docs/build-ref.html#name">Name</a> | required | |


<a id="rust_test_suite"></a>

## rust_test_suite
Expand Down
19 changes: 19 additions & 0 deletions docs/flatten.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)


<a id="capture_clippy_output"></a>
Expand Down Expand Up @@ -1352,6 +1353,24 @@ A test rule for performing `rustfmt --check` on a set of targets
| <a id="rustfmt_test-targets"></a>targets | Rust targets to run <code>rustfmt --check</code> on. | <a href="https://bazel.build/docs/build-ref.html#labels">List of labels</a> | optional | [] |


<a id="source_path_prefix"></a>

## source_path_prefix

<pre>
source_path_prefix(<a href="#source_path_prefix-name">name</a>)
</pre>

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 |
| :------------- | :------------- | :------------- | :------------- | :------------- |
| <a id="source_path_prefix-name"></a>name | A unique name for this target. | <a href="https://bazel.build/docs/build-ref.html#name">Name</a> | required | |


<a id="CrateInfo"></a>

## CrateInfo
Expand Down
2 changes: 2 additions & 0 deletions docs/symbols.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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

Expand Down
4 changes: 4 additions & 0 deletions rust/defs.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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.

Expand Down
3 changes: 3 additions & 0 deletions rust/private/rust.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
Expand Down
26 changes: 22 additions & 4 deletions rust/private/rustc.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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):
Expand Down Expand Up @@ -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.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you preserve the remap_path_prefix (str, optional) and move the logic that picks it up from _source_path_prefix to clients; the implicit dependency on specific custom attributes in ctx.attr here is a thing we'd like to avoid at this layer, if possible. (We haven't been strictly enforcing this so you can see some instances of attributes used around).

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.
Expand Down Expand Up @@ -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))
Expand Down Expand Up @@ -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)

Expand Down
2 changes: 1 addition & 1 deletion rust/private/rustdoc.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -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,
)
Expand Down