Skip to content

Commit 6571cde

Browse files
authored
Adds per_crate_rustc_flag build setting. (#1827)
* Adds per_crate_rustc_flag build setting. * Regenerate documentation
1 parent 7a47449 commit 6571cde

File tree

11 files changed

+235
-7
lines changed

11 files changed

+235
-7
lines changed

BUILD.bazel

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
load("@bazel_skylib//:bzl_library.bzl", "bzl_library")
2-
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")
2+
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", "per_crate_rustc_flag")
33

44
exports_files(["LICENSE"])
55

@@ -76,6 +76,12 @@ extra_exec_rustc_flag(
7676
visibility = ["//visibility:public"],
7777
)
7878

79+
per_crate_rustc_flag(
80+
name = "experimental_per_crate_rustc_flag",
81+
build_setting_default = "",
82+
visibility = ["//visibility:public"],
83+
)
84+
7985
# This setting is used by the clippy rules. See https://bazelbuild.github.io/rules_rust/rust_clippy.html
8086
label_flag(
8187
name = "clippy.toml",

docs/flatten.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1137,8 +1137,8 @@ Run the test with `bazel test //hello_lib:greeting_test`.
11371137
rust_toolchain(<a href="#rust_toolchain-name">name</a>, <a href="#rust_toolchain-allocator_library">allocator_library</a>, <a href="#rust_toolchain-binary_ext">binary_ext</a>, <a href="#rust_toolchain-cargo">cargo</a>, <a href="#rust_toolchain-clippy_driver">clippy_driver</a>, <a href="#rust_toolchain-debug_info">debug_info</a>,
11381138
<a href="#rust_toolchain-default_edition">default_edition</a>, <a href="#rust_toolchain-dylib_ext">dylib_ext</a>, <a href="#rust_toolchain-env">env</a>, <a href="#rust_toolchain-exec_triple">exec_triple</a>, <a href="#rust_toolchain-experimental_use_cc_common_link">experimental_use_cc_common_link</a>,
11391139
<a href="#rust_toolchain-extra_exec_rustc_flags">extra_exec_rustc_flags</a>, <a href="#rust_toolchain-extra_rustc_flags">extra_rustc_flags</a>, <a href="#rust_toolchain-llvm_cov">llvm_cov</a>, <a href="#rust_toolchain-llvm_profdata">llvm_profdata</a>, <a href="#rust_toolchain-llvm_tools">llvm_tools</a>,
1140-
<a href="#rust_toolchain-opt_level">opt_level</a>, <a href="#rust_toolchain-os">os</a>, <a href="#rust_toolchain-rust_doc">rust_doc</a>, <a href="#rust_toolchain-rust_std">rust_std</a>, <a href="#rust_toolchain-rustc">rustc</a>, <a href="#rust_toolchain-rustc_lib">rustc_lib</a>, <a href="#rust_toolchain-rustfmt">rustfmt</a>, <a href="#rust_toolchain-staticlib_ext">staticlib_ext</a>,
1141-
<a href="#rust_toolchain-stdlib_linkflags">stdlib_linkflags</a>, <a href="#rust_toolchain-target_json">target_json</a>, <a href="#rust_toolchain-target_triple">target_triple</a>)
1140+
<a href="#rust_toolchain-opt_level">opt_level</a>, <a href="#rust_toolchain-os">os</a>, <a href="#rust_toolchain-per_crate_rustc_flags">per_crate_rustc_flags</a>, <a href="#rust_toolchain-rust_doc">rust_doc</a>, <a href="#rust_toolchain-rust_std">rust_std</a>, <a href="#rust_toolchain-rustc">rustc</a>, <a href="#rust_toolchain-rustc_lib">rustc_lib</a>, <a href="#rust_toolchain-rustfmt">rustfmt</a>,
1141+
<a href="#rust_toolchain-staticlib_ext">staticlib_ext</a>, <a href="#rust_toolchain-stdlib_linkflags">stdlib_linkflags</a>, <a href="#rust_toolchain-target_json">target_json</a>, <a href="#rust_toolchain-target_triple">target_triple</a>)
11421142
</pre>
11431143

11441144
Declares a Rust toolchain for use.
@@ -1205,6 +1205,7 @@ See @rules_rust//rust:repositories.bzl for examples of defining the @rust_cpuX r
12051205
| <a id="rust_toolchain-llvm_tools"></a>llvm_tools | LLVM tools that are shipped with the Rust toolchain. | <a href="https://bazel.build/concepts/labels">Label</a> | optional | <code>None</code> |
12061206
| <a id="rust_toolchain-opt_level"></a>opt_level | Rustc optimization levels. | <a href="https://bazel.build/rules/lib/dict">Dictionary: String -> String</a> | optional | <code>{"dbg": "0", "fastbuild": "0", "opt": "3"}</code> |
12071207
| <a id="rust_toolchain-os"></a>os | The operating system for the current toolchain | String | optional | <code>""</code> |
1208+
| <a id="rust_toolchain-per_crate_rustc_flags"></a>per_crate_rustc_flags | Extra flags to pass to rustc in non-exec configuration | List of strings | optional | <code>[]</code> |
12081209
| <a id="rust_toolchain-rust_doc"></a>rust_doc | The location of the <code>rustdoc</code> binary. Can be a direct source or a filegroup containing one item. | <a href="https://bazel.build/concepts/labels">Label</a> | required | |
12091210
| <a id="rust_toolchain-rust_std"></a>rust_std | The Rust standard library. | <a href="https://bazel.build/concepts/labels">Label</a> | optional | <code>None</code> |
12101211
| <a id="rust_toolchain-rustc"></a>rustc | The location of the <code>rustc</code> binary. Can be a direct source or a filegroup containing one item. | <a href="https://bazel.build/concepts/labels">Label</a> | required | |

docs/rust_repositories.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,8 @@ A dedicated filegroup-like rule for Rust stdlib artifacts.
3939
rust_toolchain(<a href="#rust_toolchain-name">name</a>, <a href="#rust_toolchain-allocator_library">allocator_library</a>, <a href="#rust_toolchain-binary_ext">binary_ext</a>, <a href="#rust_toolchain-cargo">cargo</a>, <a href="#rust_toolchain-clippy_driver">clippy_driver</a>, <a href="#rust_toolchain-debug_info">debug_info</a>,
4040
<a href="#rust_toolchain-default_edition">default_edition</a>, <a href="#rust_toolchain-dylib_ext">dylib_ext</a>, <a href="#rust_toolchain-env">env</a>, <a href="#rust_toolchain-exec_triple">exec_triple</a>, <a href="#rust_toolchain-experimental_use_cc_common_link">experimental_use_cc_common_link</a>,
4141
<a href="#rust_toolchain-extra_exec_rustc_flags">extra_exec_rustc_flags</a>, <a href="#rust_toolchain-extra_rustc_flags">extra_rustc_flags</a>, <a href="#rust_toolchain-llvm_cov">llvm_cov</a>, <a href="#rust_toolchain-llvm_profdata">llvm_profdata</a>, <a href="#rust_toolchain-llvm_tools">llvm_tools</a>,
42-
<a href="#rust_toolchain-opt_level">opt_level</a>, <a href="#rust_toolchain-os">os</a>, <a href="#rust_toolchain-rust_doc">rust_doc</a>, <a href="#rust_toolchain-rust_std">rust_std</a>, <a href="#rust_toolchain-rustc">rustc</a>, <a href="#rust_toolchain-rustc_lib">rustc_lib</a>, <a href="#rust_toolchain-rustfmt">rustfmt</a>, <a href="#rust_toolchain-staticlib_ext">staticlib_ext</a>,
43-
<a href="#rust_toolchain-stdlib_linkflags">stdlib_linkflags</a>, <a href="#rust_toolchain-target_json">target_json</a>, <a href="#rust_toolchain-target_triple">target_triple</a>)
42+
<a href="#rust_toolchain-opt_level">opt_level</a>, <a href="#rust_toolchain-os">os</a>, <a href="#rust_toolchain-per_crate_rustc_flags">per_crate_rustc_flags</a>, <a href="#rust_toolchain-rust_doc">rust_doc</a>, <a href="#rust_toolchain-rust_std">rust_std</a>, <a href="#rust_toolchain-rustc">rustc</a>, <a href="#rust_toolchain-rustc_lib">rustc_lib</a>, <a href="#rust_toolchain-rustfmt">rustfmt</a>,
43+
<a href="#rust_toolchain-staticlib_ext">staticlib_ext</a>, <a href="#rust_toolchain-stdlib_linkflags">stdlib_linkflags</a>, <a href="#rust_toolchain-target_json">target_json</a>, <a href="#rust_toolchain-target_triple">target_triple</a>)
4444
</pre>
4545

4646
Declares a Rust toolchain for use.
@@ -107,6 +107,7 @@ See @rules_rust//rust:repositories.bzl for examples of defining the @rust_cpuX r
107107
| <a id="rust_toolchain-llvm_tools"></a>llvm_tools | LLVM tools that are shipped with the Rust toolchain. | <a href="https://bazel.build/concepts/labels">Label</a> | optional | <code>None</code> |
108108
| <a id="rust_toolchain-opt_level"></a>opt_level | Rustc optimization levels. | <a href="https://bazel.build/rules/lib/dict">Dictionary: String -> String</a> | optional | <code>{"dbg": "0", "fastbuild": "0", "opt": "3"}</code> |
109109
| <a id="rust_toolchain-os"></a>os | The operating system for the current toolchain | String | optional | <code>""</code> |
110+
| <a id="rust_toolchain-per_crate_rustc_flags"></a>per_crate_rustc_flags | Extra flags to pass to rustc in non-exec configuration | List of strings | optional | <code>[]</code> |
110111
| <a id="rust_toolchain-rust_doc"></a>rust_doc | The location of the <code>rustdoc</code> binary. Can be a direct source or a filegroup containing one item. | <a href="https://bazel.build/concepts/labels">Label</a> | required | |
111112
| <a id="rust_toolchain-rust_std"></a>rust_std | The Rust standard library. | <a href="https://bazel.build/concepts/labels">Label</a> | optional | <code>None</code> |
112113
| <a id="rust_toolchain-rustc"></a>rustc | The location of the <code>rustc</code> binary. Can be a direct source or a filegroup containing one item. | <a href="https://bazel.build/concepts/labels">Label</a> | required | |

rust/defs.bzl

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ load(
4949
_extra_rustc_flags = "extra_rustc_flags",
5050
_is_proc_macro_dep = "is_proc_macro_dep",
5151
_is_proc_macro_dep_enabled = "is_proc_macro_dep_enabled",
52+
_per_crate_rustc_flag = "per_crate_rustc_flag",
5253
)
5354
load(
5455
"//rust/private:rustdoc.bzl",
@@ -124,6 +125,9 @@ is_proc_macro_dep = _is_proc_macro_dep
124125
is_proc_macro_dep_enabled = _is_proc_macro_dep_enabled
125126
# See @rules_rust//rust/private:rustc.bzl for a complete description.
126127

128+
per_crate_rustc_flag = _per_crate_rustc_flag
129+
# See @rules_rust//rust/private:rustc.bzl for a complete description.
130+
127131
rust_common = _rust_common
128132
# See @rules_rust//rust/private:common.bzl for a complete description.
129133

rust/private/clippy.bzl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -220,8 +220,8 @@ rust_clippy_aspect = aspect(
220220
"_extra_rustc_flag": attr.label(
221221
default = Label("//:extra_rustc_flag"),
222222
),
223-
"_extra_rustc_flags": attr.label(
224-
default = Label("//:extra_rustc_flags"),
223+
"_per_crate_rustc_flag": attr.label(
224+
default = Label("//:experimental_per_crate_rustc_flag"),
225225
),
226226
"_process_wrapper": attr.label(
227227
doc = "A process wrapper for running clippy on all platforms",

rust/private/rust.bzl

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -682,6 +682,9 @@ _common_attrs = {
682682
"_is_proc_macro_dep_enabled": attr.label(
683683
default = Label("//:is_proc_macro_dep_enabled"),
684684
),
685+
"_per_crate_rustc_flag": attr.label(
686+
default = Label("//:experimental_per_crate_rustc_flag"),
687+
),
685688
"_process_wrapper": attr.label(
686689
doc = "A process wrapper for running rustc on all platforms.",
687690
default = Label("//util/process_wrapper"),

rust/private/rustc.bzl

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,11 @@ ExtraExecRustcFlagsInfo = provider(
6363
fields = {"extra_exec_rustc_flags": "List[string] Extra flags to pass to rustc in exec configuration"},
6464
)
6565

66+
PerCrateRustcFlagsInfo = provider(
67+
doc = "Pass each value as an additional flag to non-exec rustc invocations for crates matching the provided filter",
68+
fields = {"per_crate_rustc_flags": "List[string] Extra flags to pass to rustc in non-exec configuration"},
69+
)
70+
6671
IsProcMacroDepInfo = provider(
6772
doc = "Records if this is a transitive dependency of a proc-macro.",
6873
fields = {"is_proc_macro_dep": "Boolean"},
@@ -1003,6 +1008,10 @@ def construct_arguments(
10031008
if hasattr(ctx.attr, "_extra_rustc_flag") and not is_exec_configuration(ctx):
10041009
rustc_flags.add_all(ctx.attr._extra_rustc_flag[ExtraRustcFlagsInfo].extra_rustc_flags)
10051010

1011+
if hasattr(ctx.attr, "_per_crate_rustc_flag") and not is_exec_configuration(ctx):
1012+
per_crate_rustc_flags = ctx.attr._per_crate_rustc_flag[PerCrateRustcFlagsInfo].per_crate_rustc_flags
1013+
_add_per_crate_rustc_flags(ctx, rustc_flags, crate_info, per_crate_rustc_flags)
1014+
10061015
if hasattr(ctx.attr, "_extra_exec_rustc_flags") and is_exec_configuration(ctx):
10071016
rustc_flags.add_all(ctx.attr._extra_exec_rustc_flags[ExtraExecRustcFlagsInfo].extra_exec_rustc_flags)
10081017

@@ -1845,6 +1854,32 @@ def _get_dirname(file):
18451854
"""
18461855
return file.dirname
18471856

1857+
def _add_per_crate_rustc_flags(ctx, args, crate_info, per_crate_rustc_flags):
1858+
"""Adds matching per-crate rustc flags to an arguments object reference
1859+
1860+
Args:
1861+
ctx (ctx): The source rule's context object
1862+
args (Args): A reference to an Args object
1863+
crate_info (CrateInfo): A CrateInfo provider
1864+
per_crate_rustc_flags (list): A list of per_crate_rustc_flag values
1865+
"""
1866+
for per_crate_rustc_flag in per_crate_rustc_flags:
1867+
at_index = per_crate_rustc_flag.find("@")
1868+
if at_index == -1:
1869+
fail("per_crate_rustc_flag '{}' does not follow the expected format: prefix_filter@flag".format(per_crate_rustc_flag))
1870+
1871+
prefix_filter = per_crate_rustc_flag[:at_index]
1872+
flag = per_crate_rustc_flag[at_index + 1:]
1873+
if not flag:
1874+
fail("per_crate_rustc_flag '{}' does not follow the expected format: prefix_filter@flag".format(per_crate_rustc_flag))
1875+
1876+
label_string = str(ctx.label)
1877+
label = label_string[1:] if label_string.startswith("@//") else label_string
1878+
execution_path = crate_info.root.path
1879+
1880+
if label.startswith(prefix_filter) or execution_path.startswith(prefix_filter):
1881+
args.add(flag)
1882+
18481883
def _error_format_impl(ctx):
18491884
"""Implementation of the `error_format` rule
18501885
@@ -1923,3 +1958,19 @@ extra_exec_rustc_flag = rule(
19231958
implementation = _extra_exec_rustc_flag_impl,
19241959
build_setting = config.string(flag = True, allow_multiple = True),
19251960
)
1961+
1962+
def _per_crate_rustc_flag_impl(ctx):
1963+
return PerCrateRustcFlagsInfo(per_crate_rustc_flags = [f for f in ctx.build_setting_value if f != ""])
1964+
1965+
per_crate_rustc_flag = rule(
1966+
doc = (
1967+
"Add additional rustc_flag to matching crates from the command line with `--@rules_rust//:experimental_per_crate_rustc_flag`. " +
1968+
"The expected flag format is prefix_filter@flag, where any crate with a label or execution path starting with the prefix filter will be built with the given flag." +
1969+
"The label matching uses the canonical form of the label (i.e //package:label_name)." +
1970+
"The execution path is the relative path to your workspace directory including the base name (including extension) of the crate root." +
1971+
"This flag is only applied to the exec configuration (proc-macros, cargo_build_script, etc)." +
1972+
"Multiple uses are accumulated."
1973+
),
1974+
implementation = _per_crate_rustc_flag_impl,
1975+
build_setting = config.string(flag = True, allow_multiple = True),
1976+
)

rust/toolchain.bzl

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -578,6 +578,7 @@ def _rust_toolchain_impl(ctx):
578578
stdlib_linkflags = stdlib_linkflags_cc_info,
579579
extra_rustc_flags = ctx.attr.extra_rustc_flags,
580580
extra_exec_rustc_flags = ctx.attr.extra_exec_rustc_flags,
581+
per_crate_rustc_flags = ctx.attr.per_crate_rustc_flags,
581582
sysroot = sysroot_path,
582583
sysroot_short_path = sysroot_short_path,
583584
target_arch = target_arch,
@@ -680,6 +681,9 @@ rust_toolchain = rule(
680681
"os": attr.string(
681682
doc = "The operating system for the current toolchain",
682683
),
684+
"per_crate_rustc_flags": attr.string_list(
685+
doc = "Extra flags to pass to rustc in non-exec configuration",
686+
),
683687
"rust_doc": attr.label(
684688
doc = "The location of the `rustdoc` binary. Can be a direct source or a filegroup containing one item.",
685689
allow_single_file = True,
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
load(":per_crate_rustc_flag_test.bzl", "per_crate_rustc_flag_test_suite")
2+
3+
per_crate_rustc_flag_test_suite(
4+
name = "per_crate_rustc_flag_test_suite",
5+
)
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
pub fn call() {}

0 commit comments

Comments
 (0)