Skip to content

Commit ce8005c

Browse files
authored
Add incompatible flag for splitting rust_library (#847)
* Add incomaptible flag for splitting rust_library * Add docs * Add load * Regenerate documentation * Fix load order * Regenerate documentation * Buildifier * Stuff
1 parent 37982c5 commit ce8005c

File tree

8 files changed

+166
-0
lines changed

8 files changed

+166
-0
lines changed

docs/BUILD.bazel

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,13 @@ PAGES = dict([
4545
"crate",
4646
],
4747
),
48+
page(
49+
name = "settings",
50+
symbols = [
51+
"incompatible_flag",
52+
"fail_when_enabled",
53+
],
54+
),
4855
page(
4956
name = "defs",
5057
symbols = [

docs/flatten.md

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
* [cargo_build_script](#cargo_build_script)
44
* [crate](#crate)
55
* [crate_universe](#crate_universe)
6+
* [fail_when_enabled](#fail_when_enabled)
7+
* [incompatible_flag](#incompatible_flag)
68
* [rust_analyzer](#rust_analyzer)
79
* [rust_analyzer_aspect](#rust_analyzer_aspect)
810
* [rust_benchmark](#rust_benchmark)
@@ -81,6 +83,44 @@ Environment Variables:
8183
| <a id="crate_universe-version"></a>version | The version of cargo the resolver should use | String | optional | "1.53.0" |
8284

8385

86+
<a id="#fail_when_enabled"></a>
87+
88+
## fail_when_enabled
89+
90+
<pre>
91+
fail_when_enabled(<a href="#fail_when_enabled-name">name</a>, <a href="#fail_when_enabled-flag">flag</a>)
92+
</pre>
93+
94+
A rule that will fail analysis when a flag is enabled.
95+
96+
**ATTRIBUTES**
97+
98+
99+
| Name | Description | Type | Mandatory | Default |
100+
| :------------- | :------------- | :------------- | :------------- | :------------- |
101+
| <a id="fail_when_enabled-name"></a>name | A unique name for this target. | <a href="https://bazel.build/docs/build-ref.html#name">Name</a> | required | |
102+
| <a id="fail_when_enabled-flag"></a>flag | The incompatible flag to check | String | required | |
103+
104+
105+
<a id="#incompatible_flag"></a>
106+
107+
## incompatible_flag
108+
109+
<pre>
110+
incompatible_flag(<a href="#incompatible_flag-name">name</a>, <a href="#incompatible_flag-issue">issue</a>)
111+
</pre>
112+
113+
A rule defining an incompatible flag.
114+
115+
**ATTRIBUTES**
116+
117+
118+
| Name | Description | Type | Mandatory | Default |
119+
| :------------- | :------------- | :------------- | :------------- | :------------- |
120+
| <a id="incompatible_flag-name"></a>name | A unique name for this target. | <a href="https://bazel.build/docs/build-ref.html#name">Name</a> | required | |
121+
| <a id="incompatible_flag-issue"></a>issue | The link to the github issue associated with this flag | String | required | |
122+
123+
84124
<a id="#rust_analyzer"></a>
85125

86126
## rust_analyzer

docs/settings.md

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<!-- Generated with Stardoc: http://skydoc.bazel.build -->
2+
# Settings
3+
4+
* [incompatible_flag](#incompatible_flag)
5+
* [fail_when_enabled](#fail_when_enabled)
6+
7+
<a id="#fail_when_enabled"></a>
8+
9+
## fail_when_enabled
10+
11+
<pre>
12+
fail_when_enabled(<a href="#fail_when_enabled-name">name</a>, <a href="#fail_when_enabled-flag">flag</a>)
13+
</pre>
14+
15+
A rule that will fail analysis when a flag is enabled.
16+
17+
**ATTRIBUTES**
18+
19+
20+
| Name | Description | Type | Mandatory | Default |
21+
| :------------- | :------------- | :------------- | :------------- | :------------- |
22+
| <a id="fail_when_enabled-name"></a>name | A unique name for this target. | <a href="https://bazel.build/docs/build-ref.html#name">Name</a> | required | |
23+
| <a id="fail_when_enabled-flag"></a>flag | The incompatible flag to check | String | required | |
24+
25+
26+
<a id="#incompatible_flag"></a>
27+
28+
## incompatible_flag
29+
30+
<pre>
31+
incompatible_flag(<a href="#incompatible_flag-name">name</a>, <a href="#incompatible_flag-issue">issue</a>)
32+
</pre>
33+
34+
A rule defining an incompatible flag.
35+
36+
**ATTRIBUTES**
37+
38+
39+
| Name | Description | Type | Mandatory | Default |
40+
| :------------- | :------------- | :------------- | :------------- | :------------- |
41+
| <a id="incompatible_flag-name"></a>name | A unique name for this target. | <a href="https://bazel.build/docs/build-ref.html#name">Name</a> | required | |
42+
| <a id="incompatible_flag-issue"></a>issue | The link to the github issue associated with this flag | String | required | |
43+
44+

docs/symbols.bzl

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,11 @@ load(
6565
_rust_stdlib_filegroup = "rust_stdlib_filegroup",
6666
_rust_toolchain = "rust_toolchain",
6767
)
68+
load(
69+
"@rules_rust//rust/settings:incompatible.bzl",
70+
_fail_when_enabled = "fail_when_enabled",
71+
_incompatible_flag = "incompatible_flag",
72+
)
6873
load(
6974
"@rules_rust//wasm_bindgen:repositories.bzl",
7075
_rust_wasm_bindgen_repositories = "rust_wasm_bindgen_repositories",
@@ -120,3 +125,6 @@ crate = _crate
120125

121126
rustfmt_aspect = _rustfmt_aspect
122127
rustfmt_test = _rustfmt_test
128+
129+
incompatible_flag = _incompatible_flag
130+
fail_when_enabled = _fail_when_enabled

rust/BUILD.bazel

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,5 +20,6 @@ bzl_library(
2020
deps = [
2121
"//rust/platform:rules",
2222
"//rust/private:rules",
23+
"//rust/settings:rules",
2324
],
2425
)

rust/rust.bzl

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ load(
3333
_rust_test = "rust_test",
3434
_rust_test_binary = "rust_test_binary",
3535
)
36+
load("//rust/settings:incompatible.bzl", "fail_when_enabled")
3637

3738
def rust_library(**args):
3839
"""Deprecated. Use the version from "@rules_rust//rust:defs.bzl" instead.
@@ -43,6 +44,11 @@ def rust_library(**args):
4344
Returns:
4445
a target.
4546
"""
47+
fail_when_enabled(
48+
name = "fail_" + args["name"],
49+
flag = "split_rust_library",
50+
)
51+
4652
if "crate_type" in args:
4753
crate_type = args.pop("crate_type")
4854
if crate_type in ["lib", "rlib", "dylib"]:

rust/settings/BUILD.bazel

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
load("@bazel_skylib//:bzl_library.bzl", "bzl_library")
2+
load(":incompatible.bzl", "incompatible_flag")
3+
4+
package(default_visibility = ["//visibility:public"])
5+
6+
incompatible_flag(
7+
name = "split_rust_library",
8+
build_setting_default = False,
9+
issue = "https://github.com/bazelbuild/rules_rust/issues/591",
10+
)
11+
12+
bzl_library(
13+
name = "rules",
14+
srcs = glob(["**/*.bzl"]),
15+
)

rust/settings/incompatible.bzl

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
"""This file contains definitions of all current incompatible flags.
2+
3+
See COMPATIBILITY.md for the backwards compatibility policy.
4+
"""
5+
6+
IncompatibleFlagInfo = provider(
7+
doc = "Provider for the current value of an incompatible flag.",
8+
fields = {
9+
"enabled": "(bool) whether the flag is enabled",
10+
"issue": "(string) link to the github issue associated with this flag",
11+
},
12+
)
13+
14+
def _incompatible_flag_impl(ctx):
15+
return [IncompatibleFlagInfo(enabled = ctx.build_setting_value, issue = ctx.attr.issue)]
16+
17+
incompatible_flag = rule(
18+
doc = "A rule defining an incompatible flag.",
19+
implementation = _incompatible_flag_impl,
20+
build_setting = config.bool(flag = True),
21+
attrs = {
22+
"issue": attr.string(
23+
doc = "The link to the github issue associated with this flag",
24+
mandatory = True,
25+
),
26+
},
27+
)
28+
29+
def _fail_when_enabled_impl(ctx):
30+
flag = ctx.attr.flag
31+
flag_info = getattr(ctx.attr, "_" + flag)[IncompatibleFlagInfo]
32+
if flag_info.enabled:
33+
fail("Incompatible flag {} has been flipped, see {} for details.".format(flag, flag_info.issue))
34+
35+
fail_when_enabled = rule(
36+
doc = "A rule that will fail analysis when a flag is enabled.",
37+
implementation = _fail_when_enabled_impl,
38+
attrs = {
39+
"flag": attr.string(
40+
doc = "The incompatible flag to check",
41+
mandatory = True,
42+
),
43+
"_split_rust_library": attr.label(default = "//rust/settings:split_rust_library"),
44+
},
45+
)

0 commit comments

Comments
 (0)