Skip to content

Commit ad01d1b

Browse files
authored
[crate_universe] add an annotation to disable pipelining (#1733)
The recent support for pipelined compilation (#1275) is great. There are some situations, however, where we need to disable pipelining for specific crates, e.g. #1584. This PR adds a crate annotation option to disable pipelining for rust_library targets generated by cargo_bazel. The alternatives would be patching the crate with the annotations system or disabling pipelining globally.
1 parent f651cd1 commit ad01d1b

File tree

16 files changed

+684
-581
lines changed

16 files changed

+684
-581
lines changed

crate_universe/private/crate.bzl

+3
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ def _annotation(
7979
data_glob = None,
8080
deps = None,
8181
gen_binaries = [],
82+
disable_pipelining = False,
8283
gen_build_script = None,
8384
patch_args = None,
8485
patch_tool = None,
@@ -118,6 +119,7 @@ def _annotation(
118119
deps (list, optional): A list of labels to add to a crate's `rust_library::deps` attribute.
119120
gen_binaries (list or bool, optional): As a list, the subset of the crate's bins that should get `rust_binary`
120121
targets produced. Or `True` to generate all, `False` to generate none.
122+
disable_pipelining (bool, optional): If True, disables pipelining for library targets for this crate.
121123
gen_build_script (bool, optional): An authorative flag to determine whether or not to produce
122124
`cargo_build_script` targets for the current crate.
123125
patch_args (list, optional): The `patch_args` attribute of a Bazel repository rule. See
@@ -164,6 +166,7 @@ def _annotation(
164166
data_glob = data_glob,
165167
deps = deps,
166168
gen_binaries = gen_binaries,
169+
disable_pipelining = disable_pipelining,
167170
gen_build_script = gen_build_script,
168171
patch_args = patch_args,
169172
patch_tool = patch_tool,

crate_universe/src/config.rs

+4
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,9 @@ pub struct CrateAnnotations {
178178
/// [compile_data](https://bazelbuild.github.io/rules_rust/defs.html#rust_library-compile_data) attribute.
179179
pub compile_data_glob: Option<BTreeSet<String>>,
180180

181+
/// If true, disables pipelining for library targets generated for this crate.
182+
pub disable_pipelining: bool,
183+
181184
/// Additional data to pass to the target's
182185
/// [rustc_env](https://bazelbuild.github.io/rules_rust/defs.html#rust_library-rustc_env) attribute.
183186
pub rustc_env: Option<BTreeMap<String, String>>,
@@ -278,6 +281,7 @@ impl Add for CrateAnnotations {
278281
crate_features: joined_extra_member!(self.crate_features, rhs.crate_features, BTreeSet::new, BTreeSet::extend),
279282
data: joined_extra_member!(self.data, rhs.data, BTreeSet::new, BTreeSet::extend),
280283
data_glob: joined_extra_member!(self.data_glob, rhs.data_glob, BTreeSet::new, BTreeSet::extend),
284+
disable_pipelining: self.disable_pipelining || rhs.disable_pipelining,
281285
compile_data: joined_extra_member!(self.compile_data, rhs.compile_data, BTreeSet::new, BTreeSet::extend),
282286
compile_data_glob: joined_extra_member!(self.compile_data_glob, rhs.compile_data_glob, BTreeSet::new, BTreeSet::extend),
283287
rustc_env: joined_extra_member!(self.rustc_env, rhs.rustc_env, BTreeMap::new, BTreeMap::extend),

crate_universe/src/context/crate_context.rs

+10
Original file line numberDiff line numberDiff line change
@@ -247,6 +247,10 @@ pub struct CrateContext {
247247
/// Additional text to add to the generated BUILD file.
248248
#[serde(skip_serializing_if = "Option::is_none")]
249249
pub additive_build_file_content: Option<String>,
250+
251+
/// If true, disables pipelining for library targets generated for this crate
252+
#[serde(skip_serializing_if = "std::ops::Not::not")]
253+
pub disable_pipelining: bool,
250254
}
251255

252256
impl CrateContext {
@@ -394,6 +398,7 @@ impl CrateContext {
394398
build_script_attrs,
395399
license,
396400
additive_build_file_content: None,
401+
disable_pipelining: false,
397402
}
398403
.with_overrides(extras)
399404
}
@@ -446,6 +451,11 @@ impl CrateContext {
446451
self.common_attrs.data_glob.extend(extra.clone());
447452
}
448453

454+
// Disable pipelining
455+
if crate_extra.disable_pipelining {
456+
self.disable_pipelining = true;
457+
}
458+
449459
// Rustc flags
450460
if let Some(extra) = &crate_extra.rustc_flags {
451461
self.common_attrs.rustc_flags.append(&mut extra.clone());

crate_universe/src/lockfile.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -255,7 +255,7 @@ mod test {
255255

256256
assert_eq!(
257257
digest,
258-
Digest("712442b3d89756257cf2739fa4ab58c2154d1bda3fb2c4c3647c613403351694".to_owned())
258+
Digest("0485e1ac3d5a868679b2ee4b59443eec3f8b54e1f4824f7c251b20031542f64c".to_owned())
259259
);
260260
}
261261

crate_universe/src/rendering.rs

+26
Original file line numberDiff line numberDiff line change
@@ -436,6 +436,7 @@ impl Renderer {
436436
.make_aliases(krate, false, false)
437437
.remap_configurations(platforms),
438438
common: self.make_common_attrs(platforms, krate, target)?,
439+
disable_pipelining: krate.disable_pipelining,
439440
})
440441
}
441442

@@ -771,6 +772,31 @@ mod test {
771772
assert!(build_file_content.contains("\"crate-name=mock_crate\""));
772773
}
773774

775+
#[test]
776+
fn test_disable_pipelining() {
777+
let mut context = Context::default();
778+
let crate_id = CrateId::new("mock_crate".to_owned(), "0.1.0".to_owned());
779+
context.crates.insert(
780+
crate_id.clone(),
781+
CrateContext {
782+
name: crate_id.name,
783+
version: crate_id.version,
784+
targets: BTreeSet::from([Rule::Library(mock_target_attributes())]),
785+
disable_pipelining: true,
786+
..CrateContext::default()
787+
},
788+
);
789+
790+
let renderer = Renderer::new(mock_render_config());
791+
let output = renderer.render(&context).unwrap();
792+
793+
let build_file_content = output
794+
.get(&PathBuf::from("BUILD.mock_crate-0.1.0.bazel"))
795+
.unwrap();
796+
797+
assert!(build_file_content.contains("disable_pipelining = True"));
798+
}
799+
774800
#[test]
775801
fn render_cargo_build_script() {
776802
let mut context = Context::default();

crate_universe/src/utils/starlark.rs

+2
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,8 @@ pub struct RustLibrary {
175175
pub aliases: SelectDict<WithOriginalConfigurations<String>>,
176176
#[serde(flatten)]
177177
pub common: CommonAttrs,
178+
#[serde(skip_serializing_if = "std::ops::Not::not")]
179+
pub disable_pipelining: bool,
178180
}
179181

180182
#[derive(Serialize)]

docs/crate_universe.md

+3-2
Original file line numberDiff line numberDiff line change
@@ -602,8 +602,8 @@ crate.annotation(<a href="#crate.annotation-version">version</a>, <a href="#crat
602602
<a href="#crate.annotation-build_script_tools">build_script_tools</a>, <a href="#crate.annotation-build_script_data_glob">build_script_data_glob</a>, <a href="#crate.annotation-build_script_deps">build_script_deps</a>, <a href="#crate.annotation-build_script_env">build_script_env</a>,
603603
<a href="#crate.annotation-build_script_proc_macro_deps">build_script_proc_macro_deps</a>, <a href="#crate.annotation-build_script_rustc_env">build_script_rustc_env</a>, <a href="#crate.annotation-build_script_toolchains">build_script_toolchains</a>,
604604
<a href="#crate.annotation-compile_data">compile_data</a>, <a href="#crate.annotation-compile_data_glob">compile_data_glob</a>, <a href="#crate.annotation-crate_features">crate_features</a>, <a href="#crate.annotation-data">data</a>, <a href="#crate.annotation-data_glob">data_glob</a>, <a href="#crate.annotation-deps">deps</a>, <a href="#crate.annotation-gen_binaries">gen_binaries</a>,
605-
<a href="#crate.annotation-gen_build_script">gen_build_script</a>, <a href="#crate.annotation-patch_args">patch_args</a>, <a href="#crate.annotation-patch_tool">patch_tool</a>, <a href="#crate.annotation-patches">patches</a>, <a href="#crate.annotation-proc_macro_deps">proc_macro_deps</a>, <a href="#crate.annotation-rustc_env">rustc_env</a>,
606-
<a href="#crate.annotation-rustc_env_files">rustc_env_files</a>, <a href="#crate.annotation-rustc_flags">rustc_flags</a>, <a href="#crate.annotation-shallow_since">shallow_since</a>)
605+
<a href="#crate.annotation-disable_pipelining">disable_pipelining</a>, <a href="#crate.annotation-gen_build_script">gen_build_script</a>, <a href="#crate.annotation-patch_args">patch_args</a>, <a href="#crate.annotation-patch_tool">patch_tool</a>, <a href="#crate.annotation-patches">patches</a>,
606+
<a href="#crate.annotation-proc_macro_deps">proc_macro_deps</a>, <a href="#crate.annotation-rustc_env">rustc_env</a>, <a href="#crate.annotation-rustc_env_files">rustc_env_files</a>, <a href="#crate.annotation-rustc_flags">rustc_flags</a>, <a href="#crate.annotation-shallow_since">shallow_since</a>)
607607
</pre>
608608

609609
A collection of extra attributes and settings for a particular crate
@@ -631,6 +631,7 @@ A collection of extra attributes and settings for a particular crate
631631
| <a id="crate.annotation-data_glob"></a>data_glob | A list of glob patterns to add to a crate's <code>rust_library::data</code> attribute. | `None` |
632632
| <a id="crate.annotation-deps"></a>deps | A list of labels to add to a crate's <code>rust_library::deps</code> attribute. | `None` |
633633
| <a id="crate.annotation-gen_binaries"></a>gen_binaries | As a list, the subset of the crate's bins that should get <code>rust_binary</code> targets produced. Or <code>True</code> to generate all, <code>False</code> to generate none. | `[]` |
634+
| <a id="crate.annotation-disable_pipelining"></a>disable_pipelining | If True, disables pipelining for library targets for this crate. | `False` |
634635
| <a id="crate.annotation-gen_build_script"></a>gen_build_script | An authorative flag to determine whether or not to produce <code>cargo_build_script</code> targets for the current crate. | `None` |
635636
| <a id="crate.annotation-patch_args"></a>patch_args | The <code>patch_args</code> attribute of a Bazel repository rule. See [http_archive.patch_args](https://docs.bazel.build/versions/main/repo/http.html#http_archive-patch_args) | `None` |
636637
| <a id="crate.annotation-patch_tool"></a>patch_tool | The <code>patch_tool</code> attribute of a Bazel repository rule. See [http_archive.patch_tool](https://docs.bazel.build/versions/main/repo/http.html#http_archive-patch_tool) | `None` |

examples/crate_universe/cargo_aliases/Cargo.Bazel.lock

+12-12
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)