Skip to content

Commit 342f860

Browse files
committed
Pull out on_stderr_line options into a struct.
1 parent f6d30e9 commit 342f860

File tree

1 file changed

+53
-62
lines changed

1 file changed

+53
-62
lines changed

src/cargo/core/compiler/mod.rs

Lines changed: 53 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -217,9 +217,7 @@ fn rustc<'a, 'cfg>(
217217
let dep_info_loc = fingerprint::dep_info_loc(cx, unit);
218218

219219
rustc.args(cx.bcx.rustflags_args(unit));
220-
let emit_json = cx.bcx.build_config.emit_json();
221-
let mut cache_cell = new_cache_cell(cx, unit);
222-
let color = cx.bcx.config.shell().supports_color();
220+
let mut output_options = OutputOptions::new(cx, unit);
223221
let package_id = unit.pkg.package_id();
224222
let target = unit.target.clone();
225223
let mode = unit.mode;
@@ -234,7 +232,6 @@ fn rustc<'a, 'cfg>(
234232
.unwrap_or_else(|| cx.bcx.config.cwd())
235233
.to_path_buf();
236234
let fingerprint_dir = cx.files().fingerprint_dir(unit);
237-
let rmeta_produced = cx.rmeta_required(unit);
238235

239236
return Ok(Work::new(move |state| {
240237
// Only at runtime have we discovered what the extra -L and -l
@@ -296,18 +293,7 @@ fn rustc<'a, 'cfg>(
296293
&target,
297294
mode,
298295
&mut |line| on_stdout_line(state, line, package_id, &target),
299-
&mut |line| {
300-
on_stderr_line(
301-
state,
302-
line,
303-
package_id,
304-
&target,
305-
!emit_json,
306-
rmeta_produced,
307-
color,
308-
&mut cache_cell,
309-
)
310-
},
296+
&mut |line| on_stderr_line(state, line, package_id, &target, &mut output_options),
311297
)
312298
.map_err(internal_if_simple_exit_code)
313299
.chain_err(|| format!("Could not compile `{}`.", name))?;
@@ -649,11 +635,9 @@ fn rustdoc<'a, 'cfg>(cx: &mut Context<'a, 'cfg>, unit: &Unit<'a>) -> CargoResult
649635
let name = unit.pkg.name().to_string();
650636
let build_state = cx.build_state.clone();
651637
let key = (unit.pkg.package_id(), unit.kind);
652-
let emit_json = bcx.build_config.emit_json();
653-
let color = bcx.config.shell().supports_color();
654638
let package_id = unit.pkg.package_id();
655639
let target = unit.target.clone();
656-
let mut cache_cell = new_cache_cell(cx, unit);
640+
let mut output_options = OutputOptions::new(cx, unit);
657641

658642
Ok(Work::new(move |state| {
659643
if let Some(output) = build_state.outputs.lock().unwrap().get(&key) {
@@ -669,18 +653,7 @@ fn rustdoc<'a, 'cfg>(cx: &mut Context<'a, 'cfg>, unit: &Unit<'a>) -> CargoResult
669653
rustdoc
670654
.exec_with_streaming(
671655
&mut |line| on_stdout_line(state, line, package_id, &target),
672-
&mut |line| {
673-
on_stderr_line(
674-
state,
675-
line,
676-
package_id,
677-
&target,
678-
!emit_json,
679-
false,
680-
color,
681-
&mut cache_cell,
682-
)
683-
},
656+
&mut |line| on_stderr_line(state, line, package_id, &target, &mut output_options),
684657
false,
685658
)
686659
.chain_err(|| format!("Could not document `{}`.", name))?;
@@ -1114,6 +1087,43 @@ impl Kind {
11141087
}
11151088
}
11161089

1090+
struct OutputOptions {
1091+
/// Get the `"rendered"` field from the JSON output and display it on
1092+
/// stderr instead of the JSON message.
1093+
extract_rendered_messages: bool,
1094+
/// Look for JSON message that indicates .rmeta file is available for
1095+
/// pipelined compilation.
1096+
look_for_metadata_directive: bool,
1097+
/// Whether or not to display messages in color.
1098+
color: bool,
1099+
/// Where to write the JSON messages to support playback later if the unit
1100+
/// is fresh. The file is created lazily so that in the normal case, lots
1101+
/// of empty files are not created. This is None if caching is disabled.
1102+
cache_cell: Option<(PathBuf, LazyCell<File>)>,
1103+
}
1104+
1105+
impl OutputOptions {
1106+
fn new<'a>(cx: &Context<'a, '_>, unit: &Unit<'a>) -> OutputOptions {
1107+
let extract_rendered_messages = cx.bcx.build_config.message_format != MessageFormat::Json;
1108+
let look_for_metadata_directive = cx.rmeta_required(unit);
1109+
let color = cx.bcx.config.shell().supports_color();
1110+
let cache_cell = if cx.bcx.build_config.cache_messages() {
1111+
let path = cx.files().message_cache_path(unit);
1112+
// Remove old cache, ignore ENOENT, which is the common case.
1113+
drop(fs::remove_file(&path));
1114+
Some((path, LazyCell::new()))
1115+
} else {
1116+
None
1117+
};
1118+
OutputOptions {
1119+
extract_rendered_messages,
1120+
look_for_metadata_directive,
1121+
color,
1122+
cache_cell,
1123+
}
1124+
}
1125+
}
1126+
11171127
fn on_stdout_line(
11181128
state: &JobState<'_>,
11191129
line: &str,
@@ -1129,16 +1139,11 @@ fn on_stderr_line(
11291139
line: &str,
11301140
package_id: PackageId,
11311141
target: &Target,
1132-
extract_rendered_messages: bool,
1133-
look_for_metadata_directive: bool,
1134-
color: bool,
1135-
cache_cell: &mut Option<(PathBuf, LazyCell<File>)>,
1142+
options: &mut OutputOptions,
11361143
) -> CargoResult<()> {
11371144
// Check if caching is enabled.
1138-
if let Some((path, cell)) = cache_cell {
1145+
if let Some((path, cell)) = &mut options.cache_cell {
11391146
// Cache the output, which will be replayed later when Fresh.
1140-
// The file is created lazily so that in the normal case, lots of
1141-
// empty files are not created.
11421147
let f = cell.try_borrow_mut_with(|| File::create(path))?;
11431148
debug_assert!(!line.contains('\n'));
11441149
f.write_all(line.as_bytes())?;
@@ -1173,7 +1178,7 @@ fn on_stderr_line(
11731178
// colorized diagnostics. In those cases (`extract_rendered_messages`) we
11741179
// take a look at the JSON blob we go, see if it's a relevant diagnostics,
11751180
// and if so forward just that diagnostic for us to print.
1176-
if extract_rendered_messages {
1181+
if options.extract_rendered_messages {
11771182
#[derive(serde::Deserialize)]
11781183
struct CompilerMessage {
11791184
rendered: String,
@@ -1183,7 +1188,7 @@ fn on_stderr_line(
11831188
if error.rendered.ends_with('\n') {
11841189
error.rendered.pop();
11851190
}
1186-
let rendered = if color {
1191+
let rendered = if options.color {
11871192
error.rendered
11881193
} else {
11891194
// Strip only fails if the the Writer fails, which is Cursor
@@ -1227,7 +1232,7 @@ fn on_stderr_line(
12271232
//
12281233
// In these cases look for a matching directive and inform Cargo internally
12291234
// that a metadata file has been produced.
1230-
if look_for_metadata_directive {
1235+
if options.look_for_metadata_directive {
12311236
#[derive(serde::Deserialize)]
12321237
struct ArtifactNotification {
12331238
artifact: String,
@@ -1273,35 +1278,21 @@ fn replay_output_cache(
12731278
// FIXME: short not supported.
12741279
MessageFormat::Short => false,
12751280
};
1281+
let mut options = OutputOptions {
1282+
extract_rendered_messages,
1283+
look_for_metadata_directive: false,
1284+
color,
1285+
cache_cell: None,
1286+
};
12761287
Work::new(move |state| {
12771288
if !path.exists() {
12781289
// No cached output, probably didn't emit anything.
12791290
return Ok(());
12801291
}
12811292
let contents = fs::read_to_string(&path)?;
12821293
for line in contents.lines() {
1283-
on_stderr_line(
1284-
state,
1285-
&line,
1286-
package_id,
1287-
&target,
1288-
extract_rendered_messages,
1289-
false, // look_for_metadata_directive
1290-
color,
1291-
&mut None,
1292-
)?;
1294+
on_stderr_line(state, &line, package_id, &target, &mut options)?;
12931295
}
12941296
Ok(())
12951297
})
12961298
}
1297-
1298-
fn new_cache_cell(cx: &Context<'_, '_>, unit: &Unit<'_>) -> Option<(PathBuf, LazyCell<File>)> {
1299-
if cx.bcx.build_config.cache_messages() {
1300-
let path = cx.files().message_cache_path(unit);
1301-
// Remove old cache, ignore ENOENT, which is the common case.
1302-
drop(fs::remove_file(&path));
1303-
Some((path, LazyCell::new()))
1304-
} else {
1305-
None
1306-
}
1307-
}

0 commit comments

Comments
 (0)