@@ -217,9 +217,7 @@ fn rustc<'a, 'cfg>(
217
217
let dep_info_loc = fingerprint:: dep_info_loc ( cx, unit) ;
218
218
219
219
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) ;
223
221
let package_id = unit. pkg . package_id ( ) ;
224
222
let target = unit. target . clone ( ) ;
225
223
let mode = unit. mode ;
@@ -234,7 +232,6 @@ fn rustc<'a, 'cfg>(
234
232
. unwrap_or_else ( || cx. bcx . config . cwd ( ) )
235
233
. to_path_buf ( ) ;
236
234
let fingerprint_dir = cx. files ( ) . fingerprint_dir ( unit) ;
237
- let rmeta_produced = cx. rmeta_required ( unit) ;
238
235
239
236
return Ok ( Work :: new ( move |state| {
240
237
// Only at runtime have we discovered what the extra -L and -l
@@ -296,18 +293,7 @@ fn rustc<'a, 'cfg>(
296
293
& target,
297
294
mode,
298
295
& 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) ,
311
297
)
312
298
. map_err ( internal_if_simple_exit_code)
313
299
. chain_err ( || format ! ( "Could not compile `{}`." , name) ) ?;
@@ -649,11 +635,9 @@ fn rustdoc<'a, 'cfg>(cx: &mut Context<'a, 'cfg>, unit: &Unit<'a>) -> CargoResult
649
635
let name = unit. pkg . name ( ) . to_string ( ) ;
650
636
let build_state = cx. build_state . clone ( ) ;
651
637
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 ( ) ;
654
638
let package_id = unit. pkg . package_id ( ) ;
655
639
let target = unit. target . clone ( ) ;
656
- let mut cache_cell = new_cache_cell ( cx, unit) ;
640
+ let mut output_options = OutputOptions :: new ( cx, unit) ;
657
641
658
642
Ok ( Work :: new ( move |state| {
659
643
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
669
653
rustdoc
670
654
. exec_with_streaming (
671
655
& 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) ,
684
657
false ,
685
658
)
686
659
. chain_err ( || format ! ( "Could not document `{}`." , name) ) ?;
@@ -1114,6 +1087,43 @@ impl Kind {
1114
1087
}
1115
1088
}
1116
1089
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
+
1117
1127
fn on_stdout_line (
1118
1128
state : & JobState < ' _ > ,
1119
1129
line : & str ,
@@ -1129,16 +1139,11 @@ fn on_stderr_line(
1129
1139
line : & str ,
1130
1140
package_id : PackageId ,
1131
1141
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 ,
1136
1143
) -> CargoResult < ( ) > {
1137
1144
// Check if caching is enabled.
1138
- if let Some ( ( path, cell) ) = cache_cell {
1145
+ if let Some ( ( path, cell) ) = & mut options . cache_cell {
1139
1146
// 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.
1142
1147
let f = cell. try_borrow_mut_with ( || File :: create ( path) ) ?;
1143
1148
debug_assert ! ( !line. contains( '\n' ) ) ;
1144
1149
f. write_all ( line. as_bytes ( ) ) ?;
@@ -1173,7 +1178,7 @@ fn on_stderr_line(
1173
1178
// colorized diagnostics. In those cases (`extract_rendered_messages`) we
1174
1179
// take a look at the JSON blob we go, see if it's a relevant diagnostics,
1175
1180
// and if so forward just that diagnostic for us to print.
1176
- if extract_rendered_messages {
1181
+ if options . extract_rendered_messages {
1177
1182
#[ derive( serde:: Deserialize ) ]
1178
1183
struct CompilerMessage {
1179
1184
rendered : String ,
@@ -1183,7 +1188,7 @@ fn on_stderr_line(
1183
1188
if error. rendered . ends_with ( '\n' ) {
1184
1189
error. rendered . pop ( ) ;
1185
1190
}
1186
- let rendered = if color {
1191
+ let rendered = if options . color {
1187
1192
error. rendered
1188
1193
} else {
1189
1194
// Strip only fails if the the Writer fails, which is Cursor
@@ -1227,7 +1232,7 @@ fn on_stderr_line(
1227
1232
//
1228
1233
// In these cases look for a matching directive and inform Cargo internally
1229
1234
// that a metadata file has been produced.
1230
- if look_for_metadata_directive {
1235
+ if options . look_for_metadata_directive {
1231
1236
#[ derive( serde:: Deserialize ) ]
1232
1237
struct ArtifactNotification {
1233
1238
artifact : String ,
@@ -1273,35 +1278,21 @@ fn replay_output_cache(
1273
1278
// FIXME: short not supported.
1274
1279
MessageFormat :: Short => false ,
1275
1280
} ;
1281
+ let mut options = OutputOptions {
1282
+ extract_rendered_messages,
1283
+ look_for_metadata_directive : false ,
1284
+ color,
1285
+ cache_cell : None ,
1286
+ } ;
1276
1287
Work :: new ( move |state| {
1277
1288
if !path. exists ( ) {
1278
1289
// No cached output, probably didn't emit anything.
1279
1290
return Ok ( ( ) ) ;
1280
1291
}
1281
1292
let contents = fs:: read_to_string ( & path) ?;
1282
1293
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) ?;
1293
1295
}
1294
1296
Ok ( ( ) )
1295
1297
} )
1296
1298
}
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