Skip to content

Commit 2fd1f06

Browse files
committed
Enable pipelined compilation by default
This commit enables pipelined compilation by default in Cargo now that the requisite support has been stablized in rust-lang/rust#62766. This involved minor updates in a number of locations here and there, but nothing of meat has changed from the original implementation (just tweaks to how rustc is called).
1 parent f4aca6d commit 2fd1f06

File tree

8 files changed

+97
-76
lines changed

8 files changed

+97
-76
lines changed

src/cargo/core/compiler/build_context/target_info.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ pub struct TargetInfo {
1717
pub sysroot_libdir: Option<PathBuf>,
1818
pub rustflags: Vec<String>,
1919
pub rustdocflags: Vec<String>,
20+
pub supports_pipelining: bool,
2021
}
2122

2223
/// Type of each file generated by a Unit.
@@ -77,6 +78,12 @@ impl TargetInfo {
7778
.args(&rustflags)
7879
.env_remove("RUSTC_LOG");
7980

81+
// NOTE: set this unconditionally to `true` once support for `--json`
82+
// rides to stable
83+
let mut pipelining_test = process.clone();
84+
pipelining_test.args(&["--error-format=json", "--json=artifacts"]);
85+
let supports_pipelining = rustc.cached_output(&pipelining_test).is_ok();
86+
8087
let target_triple = requested_target
8188
.as_ref()
8289
.map(|s| s.as_str())
@@ -167,6 +174,7 @@ impl TargetInfo {
167174
"RUSTDOCFLAGS",
168175
)?,
169176
cfg,
177+
supports_pipelining,
170178
})
171179
}
172180

src/cargo/core/compiler/context/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ impl<'a, 'cfg> Context<'a, 'cfg> {
7575
.config
7676
.get_bool("build.pipelining")?
7777
.map(|t| t.val)
78-
.unwrap_or(false);
78+
.unwrap_or(bcx.host_info.supports_pipelining);
7979

8080
Ok(Self {
8181
bcx,

src/cargo/core/compiler/mod.rs

Lines changed: 27 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ use std::io::Write;
1818
use std::path::{Path, PathBuf};
1919
use std::sync::Arc;
2020

21-
use failure::{bail, Error};
21+
use failure::Error;
2222
use lazycell::LazyCell;
2323
use log::debug;
2424
use same_file::is_same_file;
@@ -607,7 +607,6 @@ fn rustdoc<'a, 'cfg>(cx: &mut Context<'a, 'cfg>, unit: &Unit<'a>) -> CargoResult
607607
rustdoc.arg("--crate-name").arg(&unit.target.crate_name());
608608
add_path_args(bcx, unit, &mut rustdoc);
609609
add_cap_lints(bcx, unit, &mut rustdoc);
610-
add_color(bcx, &mut rustdoc);
611610

612611
if unit.kind != Kind::Host {
613612
if let Some(ref target) = bcx.build_config.requested_target {
@@ -628,7 +627,7 @@ fn rustdoc<'a, 'cfg>(cx: &mut Context<'a, 'cfg>, unit: &Unit<'a>) -> CargoResult
628627
rustdoc.arg("--cfg").arg(&format!("feature=\"{}\"", feat));
629628
}
630629

631-
add_error_format(cx, &mut rustdoc, false, false)?;
630+
add_error_format_and_color(cx, &mut rustdoc, false)?;
632631

633632
if let Some(args) = bcx.extra_args_for(unit) {
634633
rustdoc.args(args);
@@ -715,39 +714,20 @@ fn add_cap_lints(bcx: &BuildContext<'_, '_>, unit: &Unit<'_>, cmd: &mut ProcessB
715714
}
716715
}
717716

718-
fn add_color(bcx: &BuildContext<'_, '_>, cmd: &mut ProcessBuilder) {
719-
let shell = bcx.config.shell();
720-
let color = if shell.supports_color() {
721-
"always"
722-
} else {
723-
"never"
724-
};
725-
cmd.args(&["--color", color]);
726-
}
727-
728717
/// Add error-format flags to the command.
729718
///
730-
/// This is rather convoluted right now. The general overview is:
731-
/// - If -Zcache-messages or `build.pipelining` is enabled, Cargo always uses
732-
/// JSON output. This has several benefits, such as being easier to parse,
733-
/// handles changing formats (for replaying cached messages), ensures
734-
/// atomic output (so messages aren't interleaved), etc.
735-
/// - `supports_termcolor` is a temporary flag. rustdoc does not yet support
736-
/// the `--json-rendered` flag, but it is intended to fix that soon.
737-
/// - `short` output is not yet supported for JSON output. We haven't yet
738-
/// decided how this problem will be resolved. Probably either adding
739-
/// "short" to the JSON output, or more ambitiously moving diagnostic
740-
/// rendering to an external library that Cargo can share with rustc.
719+
/// This is somewhat odd right now, but the general overview is that if
720+
/// `-Zcache-messages` or `pipelined` is enabled then Cargo always uses JSON
721+
/// output. This has several benefits, such as being easier to parse, handles
722+
/// changing formats (for replaying cached messages), ensures atomic output (so
723+
/// messages aren't interleaved), etc.
741724
///
742-
/// It is intended in the future that Cargo *always* uses the JSON output, and
743-
/// this function can be simplified. The above issues need to be resolved, the
744-
/// flags need to be stabilized, and we need more testing to ensure there
745-
/// aren't any regressions.
746-
fn add_error_format(
725+
/// It is intended in the future that Cargo *always* uses the JSON output (by
726+
/// turning on cache-messages by default), and this function can be simplified.
727+
fn add_error_format_and_color(
747728
cx: &Context<'_, '_>,
748729
cmd: &mut ProcessBuilder,
749730
pipelined: bool,
750-
supports_termcolor: bool,
751731
) -> CargoResult<()> {
752732
// If this unit is producing a required rmeta file then we need to know
753733
// when the rmeta file is ready so we can signal to the rest of Cargo that
@@ -762,26 +742,15 @@ fn add_error_format(
762742
// internally understand that we should extract the `rendered` field and
763743
// present it if we can.
764744
if cx.bcx.build_config.cache_messages() || pipelined {
765-
cmd.arg("--error-format=json").arg("-Zunstable-options");
766-
if supports_termcolor {
767-
cmd.arg("--json-rendered=termcolor");
745+
cmd.arg("--error-format=json");
746+
let mut json = String::from("--json=diagnostic-rendered-ansi");
747+
if pipelined {
748+
json.push_str(",artifacts");
768749
}
769750
if cx.bcx.build_config.message_format == MessageFormat::Short {
770-
// FIXME(rust-lang/rust#60419): right now we have no way of
771-
// turning on JSON messages from the compiler and also asking
772-
// the rendered field to be in the `short` format.
773-
bail!(
774-
"currently `--message-format short` is incompatible with {}",
775-
if pipelined {
776-
"pipelined compilation"
777-
} else {
778-
"cached output"
779-
}
780-
);
781-
}
782-
if pipelined {
783-
cmd.arg("-Zemit-artifact-notifications");
751+
json.push_str(",diagnostic-short");
784752
}
753+
cmd.arg(json);
785754
} else {
786755
match cx.bcx.build_config.message_format {
787756
MessageFormat::Human => (),
@@ -792,6 +761,13 @@ fn add_error_format(
792761
cmd.arg("--error-format").arg("short");
793762
}
794763
}
764+
765+
let color = if cx.bcx.config.shell().supports_color() {
766+
"always"
767+
} else {
768+
"never"
769+
};
770+
cmd.args(&["--color", color]);
795771
}
796772
Ok(())
797773
}
@@ -822,8 +798,7 @@ fn build_base_args<'a, 'cfg>(
822798
cmd.arg("--crate-name").arg(&unit.target.crate_name());
823799

824800
add_path_args(bcx, unit, cmd);
825-
add_color(bcx, cmd);
826-
add_error_format(cx, cmd, cx.rmeta_required(unit), true)?;
801+
add_error_format_and_color(cx, cmd, cx.rmeta_required(unit))?;
827802

828803
if !test {
829804
for crate_type in crate_types.iter() {
@@ -1227,11 +1202,11 @@ fn on_stderr_line(
12271202
} else {
12281203
// Remove color information from the rendered string. rustc has not
12291204
// included color in the past, so to avoid breaking anything, strip it
1230-
// out when --json-rendered=termcolor is used. This runs
1205+
// out when --json=diagnostic-rendered-ansi is used. This runs
12311206
// unconditionally under the assumption that Cargo will eventually
12321207
// move to this as the default mode. Perhaps in the future, cargo
12331208
// could allow the user to enable/disable color (such as with a
1234-
// `--json-rendered` or `--color` or `--message-format` flag).
1209+
// `--json` or `--color` or `--message-format` flag).
12351210
#[derive(serde::Deserialize, serde::Serialize)]
12361211
struct CompilerMessage {
12371212
rendered: String,
@@ -1297,10 +1272,8 @@ fn replay_output_cache(
12971272
) -> Work {
12981273
let target = target.clone();
12991274
let extract_rendered_messages = match format {
1300-
MessageFormat::Human => true,
1275+
MessageFormat::Human | MessageFormat::Short => true,
13011276
MessageFormat::Json => false,
1302-
// FIXME: short not supported.
1303-
MessageFormat::Short => false,
13041277
};
13051278
let mut options = OutputOptions {
13061279
extract_rendered_messages,

src/cargo/ops/fix.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -595,7 +595,7 @@ impl FixArgs {
595595
ret.enabled_edition = Some(s[prefix.len()..].to_string());
596596
continue;
597597
}
598-
if s.starts_with("--error-format=") || s.starts_with("--json-rendered=") {
598+
if s.starts_with("--error-format=") || s.starts_with("--json=") {
599599
// Cargo may add error-format in some cases, but `cargo
600600
// fix` wants to add its own.
601601
continue;

tests/testsuite/build_script.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2229,7 +2229,7 @@ fn diamond_passes_args_only_once() {
22292229
[COMPILING] a v0.5.0 ([..]
22302230
[RUNNING] `rustc [..]`
22312231
[COMPILING] foo v0.5.0 ([..]
2232-
[RUNNING] `[..]rlib -L native=test`
2232+
[RUNNING] `[..]rmeta -L native=test`
22332233
[FINISHED] dev [unoptimized + debuginfo] target(s) in [..]
22342234
",
22352235
)

tests/testsuite/cache_messages.rs

Lines changed: 46 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,52 @@ fn simple() {
5454
assert!(cargo_output2.stdout.is_empty());
5555
}
5656

57+
// same as `simple`, except everything is using the short format
58+
#[cargo_test]
59+
fn simple_short() {
60+
if !is_nightly() {
61+
// --json-rendered is unstable
62+
return;
63+
}
64+
let p = project()
65+
.file(
66+
"src/lib.rs",
67+
"
68+
fn a() {}
69+
fn b() {}
70+
",
71+
)
72+
.build();
73+
74+
let agnostic_path = Path::new("src").join("lib.rs");
75+
let agnostic_path_s = agnostic_path.to_str().unwrap();
76+
77+
let rustc_output = process("rustc")
78+
.cwd(p.root())
79+
.args(&["--crate-type=lib", agnostic_path_s, "--error-format=short"])
80+
.exec_with_output()
81+
.expect("rustc to run");
82+
83+
assert!(rustc_output.stdout.is_empty());
84+
assert!(rustc_output.status.success());
85+
86+
let cargo_output1 = p
87+
.cargo("check -Zcache-messages -q --color=never --message-format=short")
88+
.masquerade_as_nightly_cargo()
89+
.exec_with_output()
90+
.expect("cargo to run");
91+
assert_eq!(as_str(&rustc_output.stderr), as_str(&cargo_output1.stderr));
92+
// assert!(cargo_output1.stdout.is_empty());
93+
let cargo_output2 = p
94+
.cargo("check -Zcache-messages -q --message-format=short")
95+
.masquerade_as_nightly_cargo()
96+
.exec_with_output()
97+
.expect("cargo to run");
98+
println!("{}", String::from_utf8_lossy(&cargo_output2.stdout));
99+
assert_eq!(as_str(&rustc_output.stderr), as_str(&cargo_output2.stderr));
100+
assert!(cargo_output2.stdout.is_empty());
101+
}
102+
57103
#[cargo_test]
58104
fn color() {
59105
if !is_nightly() {
@@ -334,15 +380,3 @@ fn very_verbose() {
334380
.with_stderr_contains("[..]not_used[..]")
335381
.run();
336382
}
337-
338-
#[cargo_test]
339-
fn short_incompatible() {
340-
let p = project().file("src/lib.rs", "").build();
341-
p.cargo("check -Zcache-messages --message-format=short")
342-
.masquerade_as_nightly_cargo()
343-
.with_stderr(
344-
"[ERROR] currently `--message-format short` is incompatible with cached output",
345-
)
346-
.with_status(101)
347-
.run();
348-
}

tests/testsuite/profile_overrides.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -321,17 +321,17 @@ fn profile_override_hierarchy() {
321321
p.cargo("build -v").masquerade_as_nightly_cargo().with_stderr_unordered("\
322322
[COMPILING] m3 [..]
323323
[COMPILING] dep [..]
324-
[RUNNING] `rustc --crate-name m3 m3/src/lib.rs --color never --crate-type lib --emit=[..]link -C codegen-units=4 [..]
325-
[RUNNING] `rustc --crate-name dep [..]dep/src/lib.rs --color never --crate-type lib --emit=[..]link -C codegen-units=3 [..]
326-
[RUNNING] `rustc --crate-name m3 m3/src/lib.rs --color never --crate-type lib --emit=[..]link -C codegen-units=1 [..]
327-
[RUNNING] `rustc --crate-name build_script_build m1/build.rs --color never --crate-type bin --emit=[..]link -C codegen-units=4 [..]
324+
[RUNNING] `rustc --crate-name m3 m3/src/lib.rs [..] --crate-type lib --emit=[..]link -C codegen-units=4 [..]
325+
[RUNNING] `rustc --crate-name dep [..]dep/src/lib.rs [..] --crate-type lib --emit=[..]link -C codegen-units=3 [..]
326+
[RUNNING] `rustc --crate-name m3 m3/src/lib.rs [..] --crate-type lib --emit=[..]link -C codegen-units=1 [..]
327+
[RUNNING] `rustc --crate-name build_script_build m1/build.rs [..] --crate-type bin --emit=[..]link -C codegen-units=4 [..]
328328
[COMPILING] m2 [..]
329-
[RUNNING] `rustc --crate-name build_script_build m2/build.rs --color never --crate-type bin --emit=[..]link -C codegen-units=2 [..]
329+
[RUNNING] `rustc --crate-name build_script_build m2/build.rs [..] --crate-type bin --emit=[..]link -C codegen-units=2 [..]
330330
[RUNNING] `[..]/m1-[..]/build-script-build`
331331
[RUNNING] `[..]/m2-[..]/build-script-build`
332-
[RUNNING] `rustc --crate-name m2 m2/src/lib.rs --color never --crate-type lib --emit=[..]link -C codegen-units=2 [..]
332+
[RUNNING] `rustc --crate-name m2 m2/src/lib.rs [..] --crate-type lib --emit=[..]link -C codegen-units=2 [..]
333333
[COMPILING] m1 [..]
334-
[RUNNING] `rustc --crate-name m1 m1/src/lib.rs --color never --crate-type lib --emit=[..]link -C codegen-units=1 [..]
334+
[RUNNING] `rustc --crate-name m1 m1/src/lib.rs [..] --crate-type lib --emit=[..]link -C codegen-units=1 [..]
335335
[FINISHED] dev [unoptimized + debuginfo] [..]
336336
",
337337
)

tests/testsuite/rustdoc.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ fn rustdoc_simple() {
1010
[DOCUMENTING] foo v0.0.1 ([CWD])
1111
[RUNNING] `rustdoc --crate-name foo src/lib.rs [..]\
1212
-o [CWD]/target/doc \
13+
[..] \
1314
-L dependency=[CWD]/target/debug/deps`
1415
[FINISHED] dev [unoptimized + debuginfo] target(s) in [..]
1516
",
@@ -27,6 +28,7 @@ fn rustdoc_args() {
2728
[DOCUMENTING] foo v0.0.1 ([CWD])
2829
[RUNNING] `rustdoc --crate-name foo src/lib.rs [..]\
2930
-o [CWD]/target/doc \
31+
[..] \
3032
--cfg=foo \
3133
-L dependency=[CWD]/target/debug/deps`
3234
[FINISHED] dev [unoptimized + debuginfo] target(s) in [..]
@@ -66,6 +68,7 @@ fn rustdoc_foo_with_bar_dependency() {
6668
[DOCUMENTING] foo v0.0.1 ([CWD])
6769
[RUNNING] `rustdoc --crate-name foo src/lib.rs [..]\
6870
-o [CWD]/target/doc \
71+
[..] \
6972
--cfg=foo \
7073
-L dependency=[CWD]/target/debug/deps \
7174
--extern [..]`
@@ -104,6 +107,7 @@ fn rustdoc_only_bar_dependency() {
104107
[DOCUMENTING] bar v0.0.1 ([..])
105108
[RUNNING] `rustdoc --crate-name bar [..]bar/src/lib.rs [..]\
106109
-o [CWD]/target/doc \
110+
[..] \
107111
--cfg=foo \
108112
-L dependency=[CWD]/target/debug/deps`
109113
[FINISHED] dev [unoptimized + debuginfo] target(s) in [..]
@@ -125,6 +129,7 @@ fn rustdoc_same_name_documents_lib() {
125129
[DOCUMENTING] foo v0.0.1 ([..])
126130
[RUNNING] `rustdoc --crate-name foo src/lib.rs [..]\
127131
-o [CWD]/target/doc \
132+
[..] \
128133
--cfg=foo \
129134
-L dependency=[CWD]/target/debug/deps`
130135
[FINISHED] dev [unoptimized + debuginfo] target(s) in [..]
@@ -168,6 +173,7 @@ fn rustdoc_target() {
168173
[RUNNING] `rustdoc --crate-name foo src/lib.rs [..]\
169174
--target x86_64-unknown-linux-gnu \
170175
-o [CWD]/target/x86_64-unknown-linux-gnu/doc \
176+
[..] \
171177
-L dependency=[CWD]/target/x86_64-unknown-linux-gnu/debug/deps \
172178
-L dependency=[CWD]/target/debug/deps`
173179
[FINISHED] dev [unoptimized + debuginfo] target(s) in [..]",

0 commit comments

Comments
 (0)