• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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::{exit, Child, Command};
23 use std::str::FromStr;
24 use std::time::Instant;
25 
main()26 fn main() {
27     let args = env::args_os().skip(1).collect::<Vec<_>>();
28     let arg = |name| args.windows(2).find(|args| args[0] == name).and_then(|args| args[1].to_str());
29 
30     // Detect whether or not we're a build script depending on whether --target
31     // is passed (a bit janky...)
32     let target = arg("--target");
33     let version = args.iter().find(|w| &**w == "-vV");
34 
35     let verbose = match env::var("RUSTC_VERBOSE") {
36         Ok(s) => usize::from_str(&s).expect("RUSTC_VERBOSE should be an integer"),
37         Err(_) => 0,
38     };
39 
40     // Use a different compiler for build scripts, since there may not yet be a
41     // libstd for the real compiler to use. However, if Cargo is attempting to
42     // determine the version of the compiler, the real compiler needs to be
43     // used. Currently, these two states are differentiated based on whether
44     // --target and -vV is/isn't passed.
45     let (rustc, libdir) = if target.is_none() && version.is_none() {
46         ("RUSTC_SNAPSHOT", "RUSTC_SNAPSHOT_LIBDIR")
47     } else {
48         ("RUSTC_REAL", "RUSTC_LIBDIR")
49     };
50     let stage = env::var("RUSTC_STAGE").unwrap_or_else(|_| {
51         // Don't panic here; it's reasonable to try and run these shims directly. Give a helpful error instead.
52         eprintln!("rustc shim: fatal: RUSTC_STAGE was not set");
53         eprintln!("rustc shim: note: use `x.py build -vvv` to see all environment variables set by bootstrap");
54         exit(101);
55     });
56     let sysroot = env::var_os("RUSTC_SYSROOT").expect("RUSTC_SYSROOT was not set");
57     let on_fail = env::var_os("RUSTC_ON_FAIL").map(Command::new);
58 
59     let rustc = env::var_os(rustc).unwrap_or_else(|| panic!("{:?} was not set", rustc));
60     let libdir = env::var_os(libdir).unwrap_or_else(|| panic!("{:?} was not set", libdir));
61     let mut dylib_path = dylib_path();
62     dylib_path.insert(0, PathBuf::from(&libdir));
63 
64     let mut cmd = Command::new(rustc);
65     cmd.args(&args).env(dylib_path_var(), env::join_paths(&dylib_path).unwrap());
66 
67     // Get the name of the crate we're compiling, if any.
68     let crate_name = arg("--crate-name");
69 
70     if let Some(crate_name) = crate_name {
71         if let Some(target) = env::var_os("RUSTC_TIME") {
72             if target == "all"
73                 || target.into_string().unwrap().split(',').any(|c| c.trim() == crate_name)
74             {
75                 cmd.arg("-Ztime-passes");
76             }
77         }
78     }
79 
80     // Print backtrace in case of ICE
81     if env::var("RUSTC_BACKTRACE_ON_ICE").is_ok() && env::var("RUST_BACKTRACE").is_err() {
82         cmd.env("RUST_BACKTRACE", "1");
83     }
84 
85     if let Ok(lint_flags) = env::var("RUSTC_LINT_FLAGS") {
86         cmd.args(lint_flags.split_whitespace());
87     }
88 
89     if target.is_some() {
90         // The stage0 compiler has a special sysroot distinct from what we
91         // actually downloaded, so we just always pass the `--sysroot` option,
92         // unless one is already set.
93         if !args.iter().any(|arg| arg == "--sysroot") {
94             cmd.arg("--sysroot").arg(&sysroot);
95         }
96 
97         // If we're compiling specifically the `panic_abort` crate then we pass
98         // the `-C panic=abort` option. Note that we do not do this for any
99         // other crate intentionally as this is the only crate for now that we
100         // ship with panic=abort.
101         //
102         // This... is a bit of a hack how we detect this. Ideally this
103         // information should be encoded in the crate I guess? Would likely
104         // require an RFC amendment to RFC 1513, however.
105         if crate_name == Some("panic_abort") {
106             cmd.arg("-C").arg("panic=abort");
107         }
108 
109         // `-Ztls-model=initial-exec` must not be applied to proc-macros, see
110         // issue https://github.com/rust-lang/rust/issues/100530
111         if env::var("RUSTC_TLS_MODEL_INITIAL_EXEC").is_ok()
112             && arg("--crate-type") != Some("proc-macro")
113             && !matches!(crate_name, Some("proc_macro2" | "quote" | "syn" | "synstructure"))
114         {
115             cmd.arg("-Ztls-model=initial-exec");
116         }
117     } else {
118         // FIXME(rust-lang/cargo#5754) we shouldn't be using special env vars
119         // here, but rather Cargo should know what flags to pass rustc itself.
120 
121         // Override linker if necessary.
122         if let Ok(host_linker) = env::var("RUSTC_HOST_LINKER") {
123             cmd.arg(format!("-Clinker={}", host_linker));
124         }
125         if env::var_os("RUSTC_HOST_FUSE_LD_LLD").is_some() {
126             cmd.arg("-Clink-args=-fuse-ld=lld");
127         }
128 
129         if let Ok(s) = env::var("RUSTC_HOST_CRT_STATIC") {
130             if s == "true" {
131                 cmd.arg("-C").arg("target-feature=+crt-static");
132             }
133             if s == "false" {
134                 cmd.arg("-C").arg("target-feature=-crt-static");
135             }
136         }
137 
138         // Cargo doesn't pass RUSTFLAGS to proc_macros:
139         // https://github.com/rust-lang/cargo/issues/4423
140         // Thus, if we are on stage 0, we explicitly set `--cfg=bootstrap`.
141         // We also declare that the flag is expected, which we need to do to not
142         // get warnings about it being unexpected.
143         if stage == "0" {
144             cmd.arg("--cfg=bootstrap");
145         }
146         cmd.arg("-Zunstable-options");
147         cmd.arg("--check-cfg=values(bootstrap)");
148     }
149 
150     if let Ok(map) = env::var("RUSTC_DEBUGINFO_MAP") {
151         cmd.arg("--remap-path-prefix").arg(&map);
152     }
153 
154     // Force all crates compiled by this compiler to (a) be unstable and (b)
155     // allow the `rustc_private` feature to link to other unstable crates
156     // also in the sysroot. We also do this for host crates, since those
157     // may be proc macros, in which case we might ship them.
158     if env::var_os("RUSTC_FORCE_UNSTABLE").is_some() {
159         cmd.arg("-Z").arg("force-unstable-if-unmarked");
160     }
161 
162     // allow-features is handled from within this rustc wrapper because of
163     // issues with build scripts. Some packages use build scripts to
164     // dynamically detect if certain nightly features are available.
165     // There are different ways this causes problems:
166     //
167     // * rustix runs `rustc` on a small test program to see if the feature is
168     //   available (and sets a `cfg` if it is). It does not honor
169     //   CARGO_ENCODED_RUSTFLAGS.
170     // * proc-macro2 detects if `rustc -vV` says "nighty" or "dev" and enables
171     //   nightly features. It will scan CARGO_ENCODED_RUSTFLAGS for
172     //   -Zallow-features. Unfortunately CARGO_ENCODED_RUSTFLAGS is not set
173     //   for build-dependencies when --target is used.
174     //
175     // The issues above means we can't just use RUSTFLAGS, and we can't use
176     // `cargo -Zallow-features=…`. Passing it through here ensures that it
177     // always gets set. Unfortunately that also means we need to enable more
178     // features than we really want (like those for proc-macro2), but there
179     // isn't much of a way around it.
180     //
181     // I think it is unfortunate that build scripts are doing this at all,
182     // since changes to nightly features can cause crates to break even if the
183     // user didn't want or care about the use of the nightly features. I think
184     // nightly features should be opt-in only. Unfortunately the dynamic
185     // checks are now too wide spread that we just need to deal with it.
186     //
187     // If you want to try to remove this, I suggest working with the crate
188     // authors to remove the dynamic checking. Another option is to pursue
189     // https://github.com/rust-lang/cargo/issues/11244 and
190     // https://github.com/rust-lang/cargo/issues/4423, which will likely be
191     // very difficult, but could help expose -Zallow-features into build
192     // scripts so they could try to honor them.
193     if let Ok(allow_features) = env::var("RUSTC_ALLOW_FEATURES") {
194         cmd.arg(format!("-Zallow-features={allow_features}"));
195     }
196 
197     if let Ok(flags) = env::var("MAGIC_EXTRA_RUSTFLAGS") {
198         for flag in flags.split(' ') {
199             cmd.arg(flag);
200         }
201     }
202 
203     let is_test = args.iter().any(|a| a == "--test");
204     if verbose > 2 {
205         let rust_env_vars =
206             env::vars().filter(|(k, _)| k.starts_with("RUST") || k.starts_with("CARGO"));
207         let prefix = if is_test { "[RUSTC-SHIM] rustc --test" } else { "[RUSTC-SHIM] rustc" };
208         let prefix = match crate_name {
209             Some(crate_name) => format!("{} {}", prefix, crate_name),
210             None => prefix.to_string(),
211         };
212         for (i, (k, v)) in rust_env_vars.enumerate() {
213             eprintln!("{} env[{}]: {:?}={:?}", prefix, i, k, v);
214         }
215         eprintln!("{} working directory: {}", prefix, env::current_dir().unwrap().display());
216         eprintln!(
217             "{} command: {:?}={:?} {:?}",
218             prefix,
219             dylib_path_var(),
220             env::join_paths(&dylib_path).unwrap(),
221             cmd,
222         );
223         eprintln!("{} sysroot: {:?}", prefix, sysroot);
224         eprintln!("{} libdir: {:?}", prefix, libdir);
225     }
226 
227     let start = Instant::now();
228     let (child, status) = {
229         let errmsg = format!("\nFailed to run:\n{:?}\n-------------", cmd);
230         let mut child = cmd.spawn().expect(&errmsg);
231         let status = child.wait().expect(&errmsg);
232         (child, status)
233     };
234 
235     if env::var_os("RUSTC_PRINT_STEP_TIMINGS").is_some()
236         || env::var_os("RUSTC_PRINT_STEP_RUSAGE").is_some()
237     {
238         if let Some(crate_name) = crate_name {
239             let dur = start.elapsed();
240             // If the user requested resource usage data, then
241             // include that in addition to the timing output.
242             let rusage_data =
243                 env::var_os("RUSTC_PRINT_STEP_RUSAGE").and_then(|_| format_rusage_data(child));
244             eprintln!(
245                 "[RUSTC-TIMING] {} test:{} {}.{:03}{}{}",
246                 crate_name,
247                 is_test,
248                 dur.as_secs(),
249                 dur.subsec_millis(),
250                 if rusage_data.is_some() { " " } else { "" },
251                 rusage_data.unwrap_or(String::new()),
252             );
253         }
254     }
255 
256     if status.success() {
257         std::process::exit(0);
258         // note: everything below here is unreachable. do not put code that
259         // should run on success, after this block.
260     }
261     if verbose > 0 {
262         println!("\nDid not run successfully: {}\n{:?}\n-------------", status, cmd);
263     }
264 
265     if let Some(mut on_fail) = on_fail {
266         on_fail.status().expect("Could not run the on_fail command");
267     }
268 
269     // Preserve the exit code. In case of signal, exit with 0xfe since it's
270     // awkward to preserve this status in a cross-platform way.
271     match status.code() {
272         Some(i) => std::process::exit(i),
273         None => {
274             eprintln!("rustc exited with {}", status);
275             std::process::exit(0xfe);
276         }
277     }
278 }
279 
280 #[cfg(all(not(unix), not(windows)))]
281 // In the future we can add this for more platforms
format_rusage_data(_child: Child) -> Option<String>282 fn format_rusage_data(_child: Child) -> Option<String> {
283     None
284 }
285 
286 #[cfg(windows)]
format_rusage_data(child: Child) -> Option<String>287 fn format_rusage_data(child: Child) -> Option<String> {
288     use std::os::windows::io::AsRawHandle;
289 
290     use windows::{
291         Win32::Foundation::HANDLE,
292         Win32::System::ProcessStatus::{
293             K32GetProcessMemoryInfo, PROCESS_MEMORY_COUNTERS, PROCESS_MEMORY_COUNTERS_EX,
294         },
295         Win32::System::Threading::GetProcessTimes,
296         Win32::System::Time::FileTimeToSystemTime,
297     };
298 
299     let handle = HANDLE(child.as_raw_handle() as isize);
300 
301     let mut user_filetime = Default::default();
302     let mut user_time = Default::default();
303     let mut kernel_filetime = Default::default();
304     let mut kernel_time = Default::default();
305     let mut memory_counters = PROCESS_MEMORY_COUNTERS::default();
306 
307     unsafe {
308         GetProcessTimes(
309             handle,
310             &mut Default::default(),
311             &mut Default::default(),
312             &mut kernel_filetime,
313             &mut user_filetime,
314         )
315     }
316     .ok()
317     .ok()?;
318     unsafe { FileTimeToSystemTime(&user_filetime, &mut user_time) }.ok().ok()?;
319     unsafe { FileTimeToSystemTime(&kernel_filetime, &mut kernel_time) }.ok().ok()?;
320 
321     // Unlike on Linux with RUSAGE_CHILDREN, this will only return memory information for the process
322     // with the given handle and none of that process's children.
323     unsafe {
324         K32GetProcessMemoryInfo(
325             handle,
326             &mut memory_counters,
327             std::mem::size_of::<PROCESS_MEMORY_COUNTERS_EX>() as u32,
328         )
329     }
330     .ok()
331     .ok()?;
332 
333     // Guide on interpreting these numbers:
334     // https://docs.microsoft.com/en-us/windows/win32/psapi/process-memory-usage-information
335     let peak_working_set = memory_counters.PeakWorkingSetSize / 1024;
336     let peak_page_file = memory_counters.PeakPagefileUsage / 1024;
337     let peak_paged_pool = memory_counters.QuotaPeakPagedPoolUsage / 1024;
338     let peak_nonpaged_pool = memory_counters.QuotaPeakNonPagedPoolUsage / 1024;
339     Some(format!(
340         "user: {USER_SEC}.{USER_USEC:03} \
341          sys: {SYS_SEC}.{SYS_USEC:03} \
342          peak working set (kb): {PEAK_WORKING_SET} \
343          peak page file usage (kb): {PEAK_PAGE_FILE} \
344          peak paged pool usage (kb): {PEAK_PAGED_POOL} \
345          peak non-paged pool usage (kb): {PEAK_NONPAGED_POOL} \
346          page faults: {PAGE_FAULTS}",
347         USER_SEC = user_time.wSecond + (user_time.wMinute * 60),
348         USER_USEC = user_time.wMilliseconds,
349         SYS_SEC = kernel_time.wSecond + (kernel_time.wMinute * 60),
350         SYS_USEC = kernel_time.wMilliseconds,
351         PEAK_WORKING_SET = peak_working_set,
352         PEAK_PAGE_FILE = peak_page_file,
353         PEAK_PAGED_POOL = peak_paged_pool,
354         PEAK_NONPAGED_POOL = peak_nonpaged_pool,
355         PAGE_FAULTS = memory_counters.PageFaultCount,
356     ))
357 }
358 
359 #[cfg(unix)]
360 /// Tries to build a string with human readable data for several of the rusage
361 /// fields. Note that we are focusing mainly on data that we believe to be
362 /// supplied on Linux (the `rusage` struct has other fields in it but they are
363 /// currently unsupported by Linux).
format_rusage_data(_child: Child) -> Option<String>364 fn format_rusage_data(_child: Child) -> Option<String> {
365     let rusage: libc::rusage = unsafe {
366         let mut recv = std::mem::zeroed();
367         // -1 is RUSAGE_CHILDREN, which means to get the rusage for all children
368         // (and grandchildren, etc) processes that have respectively terminated
369         // and been waited for.
370         let retval = libc::getrusage(-1, &mut recv);
371         if retval != 0 {
372             return None;
373         }
374         recv
375     };
376     // Mac OS X reports the maxrss in bytes, not kb.
377     let divisor = if env::consts::OS == "macos" { 1024 } else { 1 };
378     let maxrss = (rusage.ru_maxrss + (divisor - 1)) / divisor;
379 
380     let mut init_str = format!(
381         "user: {USER_SEC}.{USER_USEC:03} \
382          sys: {SYS_SEC}.{SYS_USEC:03} \
383          max rss (kb): {MAXRSS}",
384         USER_SEC = rusage.ru_utime.tv_sec,
385         USER_USEC = rusage.ru_utime.tv_usec,
386         SYS_SEC = rusage.ru_stime.tv_sec,
387         SYS_USEC = rusage.ru_stime.tv_usec,
388         MAXRSS = maxrss
389     );
390 
391     // The remaining rusage stats vary in platform support. So we treat
392     // uniformly zero values in each category as "not worth printing", since it
393     // either means no events of that type occurred, or that the platform
394     // does not support it.
395 
396     let minflt = rusage.ru_minflt;
397     let majflt = rusage.ru_majflt;
398     if minflt != 0 || majflt != 0 {
399         init_str.push_str(&format!(" page reclaims: {} page faults: {}", minflt, majflt));
400     }
401 
402     let inblock = rusage.ru_inblock;
403     let oublock = rusage.ru_oublock;
404     if inblock != 0 || oublock != 0 {
405         init_str.push_str(&format!(" fs block inputs: {} fs block outputs: {}", inblock, oublock));
406     }
407 
408     let nvcsw = rusage.ru_nvcsw;
409     let nivcsw = rusage.ru_nivcsw;
410     if nvcsw != 0 || nivcsw != 0 {
411         init_str.push_str(&format!(
412             " voluntary ctxt switches: {} involuntary ctxt switches: {}",
413             nvcsw, nivcsw
414         ));
415     }
416 
417     return Some(init_str);
418 }
419