]> git.lizzy.rs Git - rust.git/blob - src/bootstrap/bin/rustc.rs
Auto merge of #97841 - nvzqz:inline-encode-wide, r=thomcc
[rust.git] / src / bootstrap / bin / rustc.rs
1 //! Shim which is passed to Cargo as "rustc" when running the bootstrap.
2 //!
3 //! This shim will take care of some various tasks that our build process
4 //! requires that Cargo can't quite do through normal configuration:
5 //!
6 //! 1. When compiling build scripts and build dependencies, we need a guaranteed
7 //!    full standard library available. The only compiler which actually has
8 //!    this is the snapshot, so we detect this situation and always compile with
9 //!    the snapshot compiler.
10 //! 2. We pass a bunch of `--cfg` and other flags based on what we're compiling
11 //!    (and this slightly differs based on a whether we're using a snapshot or
12 //!    not), so we do that all here.
13 //!
14 //! This may one day be replaced by RUSTFLAGS, but the dynamic nature of
15 //! switching compilers for the bootstrap and for build scripts will probably
16 //! never get replaced.
17
18 include!("../dylib_util.rs");
19
20 use std::env;
21 use std::path::PathBuf;
22 use std::process::{Child, Command};
23 use std::str::FromStr;
24 use std::time::Instant;
25
26 fn main() {
27     let args = env::args_os().skip(1).collect::<Vec<_>>();
28
29     // Detect whether or not we're a build script depending on whether --target
30     // is passed (a bit janky...)
31     let target = args.windows(2).find(|w| &*w[0] == "--target").and_then(|w| w[1].to_str());
32     let version = args.iter().find(|w| &**w == "-vV");
33
34     let verbose = match env::var("RUSTC_VERBOSE") {
35         Ok(s) => usize::from_str(&s).expect("RUSTC_VERBOSE should be an integer"),
36         Err(_) => 0,
37     };
38
39     // Use a different compiler for build scripts, since there may not yet be a
40     // libstd for the real compiler to use. However, if Cargo is attempting to
41     // determine the version of the compiler, the real compiler needs to be
42     // used. Currently, these two states are differentiated based on whether
43     // --target and -vV is/isn't passed.
44     let (rustc, libdir) = if target.is_none() && version.is_none() {
45         ("RUSTC_SNAPSHOT", "RUSTC_SNAPSHOT_LIBDIR")
46     } else {
47         ("RUSTC_REAL", "RUSTC_LIBDIR")
48     };
49     let stage = env::var("RUSTC_STAGE").expect("RUSTC_STAGE was not set");
50     let sysroot = env::var_os("RUSTC_SYSROOT").expect("RUSTC_SYSROOT was not set");
51     let on_fail = env::var_os("RUSTC_ON_FAIL").map(Command::new);
52
53     let rustc = env::var_os(rustc).unwrap_or_else(|| panic!("{:?} was not set", rustc));
54     let libdir = env::var_os(libdir).unwrap_or_else(|| panic!("{:?} was not set", libdir));
55     let mut dylib_path = dylib_path();
56     dylib_path.insert(0, PathBuf::from(&libdir));
57
58     let mut cmd = Command::new(rustc);
59     cmd.args(&args).env(dylib_path_var(), env::join_paths(&dylib_path).unwrap());
60
61     // Get the name of the crate we're compiling, if any.
62     let crate_name =
63         args.windows(2).find(|args| args[0] == "--crate-name").and_then(|args| args[1].to_str());
64
65     if let Some(crate_name) = crate_name {
66         if let Some(target) = env::var_os("RUSTC_TIME") {
67             if target == "all"
68                 || target.into_string().unwrap().split(',').any(|c| c.trim() == crate_name)
69             {
70                 cmd.arg("-Ztime");
71             }
72         }
73     }
74
75     // Print backtrace in case of ICE
76     if env::var("RUSTC_BACKTRACE_ON_ICE").is_ok() && env::var("RUST_BACKTRACE").is_err() {
77         cmd.env("RUST_BACKTRACE", "1");
78     }
79
80     if let Ok(lint_flags) = env::var("RUSTC_LINT_FLAGS") {
81         cmd.args(lint_flags.split_whitespace());
82     }
83
84     if target.is_some() {
85         // The stage0 compiler has a special sysroot distinct from what we
86         // actually downloaded, so we just always pass the `--sysroot` option,
87         // unless one is already set.
88         if !args.iter().any(|arg| arg == "--sysroot") {
89             cmd.arg("--sysroot").arg(&sysroot);
90         }
91
92         // If we're compiling specifically the `panic_abort` crate then we pass
93         // the `-C panic=abort` option. Note that we do not do this for any
94         // other crate intentionally as this is the only crate for now that we
95         // ship with panic=abort.
96         //
97         // This... is a bit of a hack how we detect this. Ideally this
98         // information should be encoded in the crate I guess? Would likely
99         // require an RFC amendment to RFC 1513, however.
100         //
101         // `compiler_builtins` are unconditionally compiled with panic=abort to
102         // workaround undefined references to `rust_eh_unwind_resume` generated
103         // otherwise, see issue https://github.com/rust-lang/rust/issues/43095.
104         if crate_name == Some("panic_abort")
105             || crate_name == Some("compiler_builtins") && stage != "0"
106         {
107             cmd.arg("-C").arg("panic=abort");
108         }
109     } else {
110         // FIXME(rust-lang/cargo#5754) we shouldn't be using special env vars
111         // here, but rather Cargo should know what flags to pass rustc itself.
112
113         // Override linker if necessary.
114         if let Ok(host_linker) = env::var("RUSTC_HOST_LINKER") {
115             cmd.arg(format!("-Clinker={}", host_linker));
116         }
117         if env::var_os("RUSTC_HOST_FUSE_LD_LLD").is_some() {
118             cmd.arg("-Clink-args=-fuse-ld=lld");
119         }
120
121         if let Ok(s) = env::var("RUSTC_HOST_CRT_STATIC") {
122             if s == "true" {
123                 cmd.arg("-C").arg("target-feature=+crt-static");
124             }
125             if s == "false" {
126                 cmd.arg("-C").arg("target-feature=-crt-static");
127             }
128         }
129
130         // Cargo doesn't pass RUSTFLAGS to proc_macros:
131         // https://github.com/rust-lang/cargo/issues/4423
132         // Thus, if we are on stage 0, we explicitly set `--cfg=bootstrap`.
133         // We also declare that the flag is expected, which is mainly needed for
134         // later stages so that they don't warn about #[cfg(bootstrap)],
135         // but enabling it for stage 0 too lets any warnings, if they occur,
136         // occur more early on, e.g. about #[cfg(bootstrap = "foo")].
137         if stage == "0" {
138             cmd.arg("--cfg=bootstrap");
139         }
140         cmd.arg("-Zunstable-options");
141         cmd.arg("--check-cfg=values(bootstrap)");
142     }
143
144     if let Ok(map) = env::var("RUSTC_DEBUGINFO_MAP") {
145         cmd.arg("--remap-path-prefix").arg(&map);
146     }
147
148     // Force all crates compiled by this compiler to (a) be unstable and (b)
149     // allow the `rustc_private` feature to link to other unstable crates
150     // also in the sysroot. We also do this for host crates, since those
151     // may be proc macros, in which case we might ship them.
152     if env::var_os("RUSTC_FORCE_UNSTABLE").is_some() && (stage != "0" || target.is_some()) {
153         cmd.arg("-Z").arg("force-unstable-if-unmarked");
154     }
155
156     if let Ok(flags) = env::var("MAGIC_EXTRA_RUSTFLAGS") {
157         for flag in flags.split(' ') {
158             cmd.arg(flag);
159         }
160     }
161
162     let is_test = args.iter().any(|a| a == "--test");
163     if verbose > 2 {
164         let rust_env_vars =
165             env::vars().filter(|(k, _)| k.starts_with("RUST") || k.starts_with("CARGO"));
166         let prefix = if is_test { "[RUSTC-SHIM] rustc --test" } else { "[RUSTC-SHIM] rustc" };
167         let prefix = match crate_name {
168             Some(crate_name) => format!("{} {}", prefix, crate_name),
169             None => prefix.to_string(),
170         };
171         for (i, (k, v)) in rust_env_vars.enumerate() {
172             eprintln!("{} env[{}]: {:?}={:?}", prefix, i, k, v);
173         }
174         eprintln!("{} working directory: {}", prefix, env::current_dir().unwrap().display());
175         eprintln!(
176             "{} command: {:?}={:?} {:?}",
177             prefix,
178             dylib_path_var(),
179             env::join_paths(&dylib_path).unwrap(),
180             cmd,
181         );
182         eprintln!("{} sysroot: {:?}", prefix, sysroot);
183         eprintln!("{} libdir: {:?}", prefix, libdir);
184     }
185
186     let start = Instant::now();
187     let (child, status) = {
188         let errmsg = format!("\nFailed to run:\n{:?}\n-------------", cmd);
189         let mut child = cmd.spawn().expect(&errmsg);
190         let status = child.wait().expect(&errmsg);
191         (child, status)
192     };
193
194     if env::var_os("RUSTC_PRINT_STEP_TIMINGS").is_some()
195         || env::var_os("RUSTC_PRINT_STEP_RUSAGE").is_some()
196     {
197         if let Some(crate_name) = crate_name {
198             let dur = start.elapsed();
199             // If the user requested resource usage data, then
200             // include that in addition to the timing output.
201             let rusage_data =
202                 env::var_os("RUSTC_PRINT_STEP_RUSAGE").and_then(|_| format_rusage_data(child));
203             eprintln!(
204                 "[RUSTC-TIMING] {} test:{} {}.{:03}{}{}",
205                 crate_name,
206                 is_test,
207                 dur.as_secs(),
208                 dur.subsec_millis(),
209                 if rusage_data.is_some() { " " } else { "" },
210                 rusage_data.unwrap_or(String::new()),
211             );
212         }
213     }
214
215     if status.success() {
216         std::process::exit(0);
217         // note: everything below here is unreachable. do not put code that
218         // should run on success, after this block.
219     }
220     if verbose > 0 {
221         println!("\nDid not run successfully: {}\n{:?}\n-------------", status, cmd);
222     }
223
224     if let Some(mut on_fail) = on_fail {
225         on_fail.status().expect("Could not run the on_fail command");
226     }
227
228     // Preserve the exit code. In case of signal, exit with 0xfe since it's
229     // awkward to preserve this status in a cross-platform way.
230     match status.code() {
231         Some(i) => std::process::exit(i),
232         None => {
233             eprintln!("rustc exited with {}", status);
234             std::process::exit(0xfe);
235         }
236     }
237 }
238
239 #[cfg(all(not(unix), not(windows)))]
240 // In the future we can add this for more platforms
241 fn format_rusage_data(_child: Child) -> Option<String> {
242     None
243 }
244
245 #[cfg(windows)]
246 fn format_rusage_data(child: Child) -> Option<String> {
247     use std::os::windows::io::AsRawHandle;
248     use winapi::um::{processthreadsapi, psapi, timezoneapi};
249     let handle = child.as_raw_handle();
250     macro_rules! try_bool {
251         ($e:expr) => {
252             if $e != 1 {
253                 return None;
254             }
255         };
256     }
257
258     let mut user_filetime = Default::default();
259     let mut user_time = Default::default();
260     let mut kernel_filetime = Default::default();
261     let mut kernel_time = Default::default();
262     let mut memory_counters = psapi::PROCESS_MEMORY_COUNTERS::default();
263
264     unsafe {
265         try_bool!(processthreadsapi::GetProcessTimes(
266             handle,
267             &mut Default::default(),
268             &mut Default::default(),
269             &mut kernel_filetime,
270             &mut user_filetime,
271         ));
272         try_bool!(timezoneapi::FileTimeToSystemTime(&user_filetime, &mut user_time));
273         try_bool!(timezoneapi::FileTimeToSystemTime(&kernel_filetime, &mut kernel_time));
274
275         // Unlike on Linux with RUSAGE_CHILDREN, this will only return memory information for the process
276         // with the given handle and none of that process's children.
277         try_bool!(psapi::GetProcessMemoryInfo(
278             handle as _,
279             &mut memory_counters as *mut _ as _,
280             std::mem::size_of::<psapi::PROCESS_MEMORY_COUNTERS_EX>() as u32,
281         ));
282     }
283
284     // Guide on interpreting these numbers:
285     // https://docs.microsoft.com/en-us/windows/win32/psapi/process-memory-usage-information
286     let peak_working_set = memory_counters.PeakWorkingSetSize / 1024;
287     let peak_page_file = memory_counters.PeakPagefileUsage / 1024;
288     let peak_paged_pool = memory_counters.QuotaPeakPagedPoolUsage / 1024;
289     let peak_nonpaged_pool = memory_counters.QuotaPeakNonPagedPoolUsage / 1024;
290     Some(format!(
291         "user: {USER_SEC}.{USER_USEC:03} \
292          sys: {SYS_SEC}.{SYS_USEC:03} \
293          peak working set (kb): {PEAK_WORKING_SET} \
294          peak page file usage (kb): {PEAK_PAGE_FILE} \
295          peak paged pool usage (kb): {PEAK_PAGED_POOL} \
296          peak non-paged pool usage (kb): {PEAK_NONPAGED_POOL} \
297          page faults: {PAGE_FAULTS}",
298         USER_SEC = user_time.wSecond + (user_time.wMinute * 60),
299         USER_USEC = user_time.wMilliseconds,
300         SYS_SEC = kernel_time.wSecond + (kernel_time.wMinute * 60),
301         SYS_USEC = kernel_time.wMilliseconds,
302         PEAK_WORKING_SET = peak_working_set,
303         PEAK_PAGE_FILE = peak_page_file,
304         PEAK_PAGED_POOL = peak_paged_pool,
305         PEAK_NONPAGED_POOL = peak_nonpaged_pool,
306         PAGE_FAULTS = memory_counters.PageFaultCount,
307     ))
308 }
309
310 #[cfg(unix)]
311 /// Tries to build a string with human readable data for several of the rusage
312 /// fields. Note that we are focusing mainly on data that we believe to be
313 /// supplied on Linux (the `rusage` struct has other fields in it but they are
314 /// currently unsupported by Linux).
315 fn format_rusage_data(_child: Child) -> Option<String> {
316     let rusage: libc::rusage = unsafe {
317         let mut recv = std::mem::zeroed();
318         // -1 is RUSAGE_CHILDREN, which means to get the rusage for all children
319         // (and grandchildren, etc) processes that have respectively terminated
320         // and been waited for.
321         let retval = libc::getrusage(-1, &mut recv);
322         if retval != 0 {
323             return None;
324         }
325         recv
326     };
327     // Mac OS X reports the maxrss in bytes, not kb.
328     let divisor = if env::consts::OS == "macos" { 1024 } else { 1 };
329     let maxrss = (rusage.ru_maxrss + (divisor - 1)) / divisor;
330
331     let mut init_str = format!(
332         "user: {USER_SEC}.{USER_USEC:03} \
333          sys: {SYS_SEC}.{SYS_USEC:03} \
334          max rss (kb): {MAXRSS}",
335         USER_SEC = rusage.ru_utime.tv_sec,
336         USER_USEC = rusage.ru_utime.tv_usec,
337         SYS_SEC = rusage.ru_stime.tv_sec,
338         SYS_USEC = rusage.ru_stime.tv_usec,
339         MAXRSS = maxrss
340     );
341
342     // The remaining rusage stats vary in platform support. So we treat
343     // uniformly zero values in each category as "not worth printing", since it
344     // either means no events of that type occurred, or that the platform
345     // does not support it.
346
347     let minflt = rusage.ru_minflt;
348     let majflt = rusage.ru_majflt;
349     if minflt != 0 || majflt != 0 {
350         init_str.push_str(&format!(" page reclaims: {} page faults: {}", minflt, majflt));
351     }
352
353     let inblock = rusage.ru_inblock;
354     let oublock = rusage.ru_oublock;
355     if inblock != 0 || oublock != 0 {
356         init_str.push_str(&format!(" fs block inputs: {} fs block outputs: {}", inblock, oublock));
357     }
358
359     let nvcsw = rusage.ru_nvcsw;
360     let nivcsw = rusage.ru_nivcsw;
361     if nvcsw != 0 || nivcsw != 0 {
362         init_str.push_str(&format!(
363             " voluntary ctxt switches: {} involuntary ctxt switches: {}",
364             nvcsw, nivcsw
365         ));
366     }
367
368     return Some(init_str);
369 }