1414
1515mod flags;
1616mod options;
17+ mod output;
18+ mod rustc;
1719mod util;
1820
1921use std:: fs:: { copy, OpenOptions } ;
20- use std:: process:: { exit, Command , Stdio } ;
22+ use std:: io;
23+ use std:: process:: { exit, Command , ExitStatus , Stdio } ;
24+ use std:: sync:: mpsc:: sync_channel;
25+
26+ use output:: process_output;
2127
2228use crate :: options:: options;
2329
30+ #[ cfg( windows) ]
31+ fn status_code ( status : ExitStatus , was_killed : bool ) -> i32 {
32+ // On windows, there's no good way to know if the process was killed by a signal.
33+ // If we killed the process, we override the code to signal success.
34+ if was_killed {
35+ 0
36+ } else {
37+ status. code ( ) . unwrap_or ( 1 )
38+ }
39+ }
40+
41+ #[ cfg( not( windows) ) ]
42+ fn status_code ( status : ExitStatus , was_killed : bool ) -> i32 {
43+ // On unix, if code is None it means that the process was killed by a signal.
44+ // https://doc.rust-lang.org/std/process/struct.ExitStatus.html#method.success
45+ match status. code ( ) {
46+ Some ( code) => code,
47+ // If we killed the process, we expect None here
48+ None if was_killed => 0 ,
49+ // Otherwise it's some unexpected signal
50+ None => 1 ,
51+ }
52+ }
53+
2454fn main ( ) {
2555 let opts = match options ( ) {
2656 Err ( err) => panic ! ( "process wrapper error: {}" , err) ,
2757 Ok ( v) => v,
2858 } ;
29- let stdout = if let Some ( stdout_file) = opts. stdout_file {
30- OpenOptions :: new ( )
31- . create ( true )
32- . truncate ( true )
33- . write ( true )
34- . open ( stdout_file)
35- . expect ( "process wrapper error: unable to open stdout file" )
36- . into ( )
37- } else {
38- Stdio :: inherit ( )
39- } ;
40- let stderr = if let Some ( stderr_file) = opts. stderr_file {
41- OpenOptions :: new ( )
42- . create ( true )
43- . truncate ( true )
44- . write ( true )
45- . open ( stderr_file)
46- . expect ( "process wrapper error: unable to open stderr file" )
47- . into ( )
59+
60+ let stderr: Box < dyn io:: Write + Send > = if let Some ( stderr_file) = opts. stderr_file {
61+ Box :: new (
62+ OpenOptions :: new ( )
63+ . create ( true )
64+ . truncate ( true )
65+ . write ( true )
66+ . open ( stderr_file)
67+ . expect ( "process wrapper error: unable to open stderr file" ) ,
68+ )
4869 } else {
49- Stdio :: inherit ( )
70+ Box :: new ( io :: stderr ( ) )
5071 } ;
51- let status = Command :: new ( opts. executable )
72+
73+ let mut child = Command :: new ( opts. executable )
5274 . args ( opts. child_arguments )
5375 . env_clear ( )
5476 . envs ( opts. child_environment )
55- . stdout ( stdout)
56- . stderr ( stderr)
57- . status ( )
77+ . stdout ( if let Some ( stdout_file) = opts. stdout_file {
78+ OpenOptions :: new ( )
79+ . create ( true )
80+ . truncate ( true )
81+ . write ( true )
82+ . open ( stdout_file)
83+ . expect ( "process wrapper error: unable to open stdout file" )
84+ . into ( )
85+ } else {
86+ Stdio :: inherit ( )
87+ } )
88+ . stderr ( Stdio :: piped ( ) )
89+ . spawn ( )
5890 . expect ( "process wrapper error: failed to spawn child process" ) ;
5991
60- if status. success ( ) {
92+
93+ let child_stderr = Box :: new ( child. stderr . take ( ) . unwrap ( ) ) ;
94+
95+ let mut was_killed = false ;
96+ let stderr_thread = if !opts. rustc_quit_on_rmeta {
97+ // Process output normally by forwarding stderr
98+ process_output ( child_stderr, stderr, Some )
99+ } else {
100+ let format = opts. rustc_output_format ;
101+ // Process json rustc output and kill the subprocess when we get a signal
102+ // that we emitted a metadata file.
103+ // This receiver will block until a corresponding send happens.
104+ let ( stop_sender, stop) = sync_channel ( 0 ) ;
105+ let thread = process_output ( child_stderr, stderr, move |line| {
106+ rustc:: stop_on_rmeta_completion ( line, format, & stop_sender)
107+ } ) ;
108+ if stop. recv ( ) . is_ok ( ) {
109+ // If recv returns Ok(), a signal was sent in this channel so we should terminate the child process.
110+ // We can safely ignore the Result from kill() as we don't care if the process already terminated.
111+ let _ = child. kill ( ) ;
112+ was_killed = true ;
113+ }
114+ thread
115+ } ;
116+ stderr_thread. join ( ) . unwrap ( ) . unwrap ( ) ;
117+
118+ let status = child
119+ . wait ( )
120+ . expect ( "process wrapper error: failed to wait for child process" ) ;
121+ // If the child process is rustc and is killed after metadata generation, that's also a success.
122+ let code = status_code ( status, was_killed) ;
123+ let success = code == 0 ;
124+ if success {
61125 if let Some ( tf) = opts. touch_file {
62126 OpenOptions :: new ( )
63127 . create ( true )
@@ -75,5 +139,5 @@ fn main() {
75139 }
76140 }
77141
78- exit ( status . code ( ) . unwrap ( ) )
142+ exit ( code)
79143}
0 commit comments