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+ let child_stderr = Box :: new ( child. stderr . take ( ) . unwrap ( ) ) ;
93+
94+ let mut was_killed = false ;
95+ let stderr_thread = if !opts. rustc_quit_on_rmeta {
96+ // Process output normally by forwarding stderr
97+ process_output ( child_stderr, stderr, Some )
98+ } else {
99+ let format = opts. rustc_output_format ;
100+ // Process json rustc output and kill the subprocess when we get a signal
101+ // that we emitted a metadata file.
102+ // This receiver will block until a corresponding send happens.
103+ let ( stop_sender, stop) = sync_channel ( 0 ) ;
104+ let thread = process_output ( child_stderr, stderr, move |line| {
105+ rustc:: stop_on_rmeta_completion ( line, format, & stop_sender)
106+ } ) ;
107+ if stop. recv ( ) . is_ok ( ) {
108+ // If recv returns Ok(), a signal was sent in this channel so we should terminate the child process.
109+ // We can safely ignore the Result from kill() as we don't care if the process already terminated.
110+ let _ = child. kill ( ) ;
111+ was_killed = true ;
112+ }
113+ thread
114+ } ;
115+ stderr_thread. join ( ) . unwrap ( ) . unwrap ( ) ;
116+
117+ let status = child
118+ . wait ( )
119+ . expect ( "process wrapper error: failed to wait for child process" ) ;
120+ // If the child process is rustc and is killed after metadata generation, that's also a success.
121+ let code = status_code ( status, was_killed) ;
122+ let success = code == 0 ;
123+ if success {
61124 if let Some ( tf) = opts. touch_file {
62125 OpenOptions :: new ( )
63126 . create ( true )
@@ -75,5 +138,5 @@ fn main() {
75138 }
76139 }
77140
78- exit ( status . code ( ) . unwrap ( ) )
141+ exit ( code)
79142}
0 commit comments