Skip to main content

run_make_support/external_deps/
rustc.rs

1use std::ffi::{OsStr, OsString};
2use std::path::{Path, PathBuf};
3use std::str::FromStr as _;
4
5use crate::command::Command;
6use crate::env::env_var;
7use crate::path_helpers::{cwd, source_root};
8use crate::util::set_host_compiler_dylib_path;
9use crate::{is_aix, is_darwin, is_windows, is_windows_msvc, target, uname};
10
11/// Construct a new `rustc` invocation. This will automatically set the library
12/// search path as `-L cwd()`. Use [`bare_rustc`] to avoid this.
13#[track_caller]
14pub fn rustc() -> Rustc {
15    Rustc::new()
16}
17
18/// Construct a plain `rustc` invocation with no flags set. Note that [`set_host_compiler_dylib_path`]
19/// still presets the environment variable `HOST_RUSTC_DYLIB_PATH` by default.
20#[track_caller]
21pub fn bare_rustc() -> Rustc {
22    Rustc::bare()
23}
24
25/// Construct a `rustc` invocation for building `minicore`.
26///
27/// This function:
28///
29/// - adds `tests/auxiliary/minicore.rs` as an input
30/// - sets the crate name to `"minicore"`
31/// - sets the crate type to `rlib`
32///
33/// # Example
34///
35/// ```ignore (illustrative)
36/// rustc_minicore().target("wasm32-wasip1").target_cpu("mvp").output("libminicore.rlib").run();
37///
38/// rustc()
39///     .input("foo.rs")
40///     .target("wasm32-wasip1")
41///     .target_cpu("mvp")
42///     .extern_("minicore", path("libminicore.rlib"))
43///     // ...
44///     .run()
45/// ```
46#[track_caller]
47pub fn rustc_minicore() -> Rustc {
48    let mut builder = rustc();
49
50    let minicore_path = source_root().join("tests/auxiliary/minicore.rs");
51    builder.input(minicore_path).crate_name("minicore").crate_type("rlib");
52
53    builder
54}
55
56/// A `rustc` invocation builder.
57#[derive(Debug)]
58#[must_use]
59pub struct Rustc {
60    cmd: Command,
61    target: Option<String>,
62}
63
64// Only fill in the target just before execution, so that it can be overridden.
65crate::macros::impl_common_helpers!(Rustc, |rustc: &mut Rustc| {
66    if let Some(target) = &rustc.target {
67        rustc.cmd.arg(&format!("--target={target}"));
68    }
69});
70
71pub fn rustc_path() -> String {
72    env_var("RUSTC")
73}
74
75#[track_caller]
76fn setup_common() -> Command {
77    let mut cmd = Command::new(rustc_path());
78    set_host_compiler_dylib_path(&mut cmd);
79    if let Ok(codegen_backend) = std::env::var("RUSTC_CODEGEN_BACKEND") {
80        cmd.arg(format!("-Zcodegen-backend={codegen_backend}"));
81    }
82    cmd
83}
84
85impl Rustc {
86    // `rustc` invocation constructor methods
87
88    /// Construct a new `rustc` invocation. This will automatically set the library
89    /// search path as `-L cwd()`, configure the compilation target and enable
90    /// dynamic linkage by default on musl hosts.
91    /// Use [`bare_rustc`] to avoid this.
92    #[track_caller]
93    pub fn new() -> Self {
94        let mut cmd = setup_common();
95        cmd.arg("-L").arg(cwd());
96
97        // FIXME: On musl hosts, we currently default to static linkage, while
98        // for running run-make tests, we rely on dynamic linkage by default
99        if std::env::var("IS_MUSL_HOST").is_ok_and(|i| i == "1") {
100            cmd.arg("-Ctarget-feature=-crt-static");
101        }
102
103        // Automatically default to cross-compilation
104        Self { cmd, target: Some(target()) }
105    }
106
107    /// Construct a bare `rustc` invocation with no flags set.
108    #[track_caller]
109    pub fn bare() -> Self {
110        let cmd = setup_common();
111        Self { cmd, target: None }
112    }
113
114    // Argument provider methods
115
116    /// Configure the compilation environment.
117    pub fn cfg(&mut self, s: &str) -> &mut Self {
118        self.cmd.arg("--cfg");
119        self.cmd.arg(s);
120        self
121    }
122
123    /// Specify default optimization level `-O` (alias for `-C opt-level=3`).
124    pub fn opt(&mut self) -> &mut Self {
125        self.cmd.arg("-O");
126        self
127    }
128
129    /// Specify a specific optimization level.
130    pub fn opt_level(&mut self, option: &str) -> &mut Self {
131        self.cmd.arg(format!("-Copt-level={option}"));
132        self
133    }
134
135    /// Incorporate a hashed string to mangled symbols.
136    pub fn metadata(&mut self, meta: &str) -> &mut Self {
137        self.cmd.arg(format!("-Cmetadata={meta}"));
138        self
139    }
140
141    /// Add a suffix in each output filename.
142    pub fn extra_filename(&mut self, suffix: &str) -> &mut Self {
143        self.cmd.arg(format!("-Cextra-filename={suffix}"));
144        self
145    }
146
147    /// Specify type(s) of output files to generate.
148    pub fn emit<S: AsRef<str>>(&mut self, kinds: S) -> &mut Self {
149        let kinds = kinds.as_ref();
150        self.cmd.arg(format!("--emit={kinds}"));
151        self
152    }
153
154    /// Specify where an external library is located.
155    pub fn extern_<P: AsRef<Path>>(&mut self, crate_name: &str, path: P) -> &mut Self {
156        assert!(
157            !crate_name.contains(|c: char| c.is_whitespace() || c == '\\' || c == '/'),
158            "crate name cannot contain whitespace or path separators"
159        );
160
161        let path = path.as_ref().to_string_lossy();
162
163        self.cmd.arg("--extern");
164        self.cmd.arg(format!("{crate_name}={path}"));
165
166        self
167    }
168
169    /// Remap source path prefixes in all output.
170    pub fn remap_path_prefix<P: AsRef<Path>, P2: AsRef<Path>>(
171        &mut self,
172        from: P,
173        to: P2,
174    ) -> &mut Self {
175        let from = from.as_ref().to_string_lossy();
176        let to = to.as_ref().to_string_lossy();
177
178        self.cmd.arg("--remap-path-prefix");
179        self.cmd.arg(format!("{from}={to}"));
180
181        self
182    }
183
184    /// Specify path to the input file.
185    pub fn input<P: AsRef<Path>>(&mut self, path: P) -> &mut Self {
186        self.cmd.arg(path.as_ref());
187        self
188    }
189
190    //Adjust the backtrace level, displaying more detailed information at higher levels.
191    pub fn set_backtrace_level<R: AsRef<OsStr>>(&mut self, level: R) -> &mut Self {
192        self.cmd.env("RUST_BACKTRACE", level);
193        self
194    }
195
196    /// Specify path to the output file. Equivalent to `-o` in rustc.
197    pub fn output<P: AsRef<Path>>(&mut self, path: P) -> &mut Self {
198        self.cmd.arg("-o");
199        self.cmd.arg(path.as_ref());
200        self
201    }
202
203    /// Specify path to the output directory. Equivalent to `--out-dir` in rustc.
204    pub fn out_dir<P: AsRef<Path>>(&mut self, path: P) -> &mut Self {
205        self.cmd.arg("--out-dir");
206        self.cmd.arg(path.as_ref());
207        self
208    }
209
210    /// This flag enables LTO in the specified form.
211    pub fn lto(&mut self, option: &str) -> &mut Self {
212        self.cmd.arg(format!("-Clto={option}"));
213        self
214    }
215
216    /// This flag defers LTO optimizations to the linker.
217    pub fn linker_plugin_lto(&mut self, option: &str) -> &mut Self {
218        self.cmd.arg(format!("-Clinker-plugin-lto={option}"));
219        self
220    }
221
222    /// Specify what happens when the code panics.
223    pub fn panic(&mut self, option: &str) -> &mut Self {
224        self.cmd.arg(format!("-Cpanic={option}"));
225        self
226    }
227
228    /// Specify number of codegen units
229    pub fn codegen_units(&mut self, units: usize) -> &mut Self {
230        self.cmd.arg(format!("-Ccodegen-units={units}"));
231        self
232    }
233
234    /// Specify directory path used for incremental cache
235    pub fn incremental<P: AsRef<Path>>(&mut self, path: P) -> &mut Self {
236        let mut arg = OsString::new();
237        arg.push("-Cincremental=");
238        arg.push(path.as_ref());
239        self.cmd.arg(&arg);
240        self
241    }
242
243    /// Specify directory path used for profile generation
244    pub fn profile_generate<P: AsRef<Path>>(&mut self, path: P) -> &mut Self {
245        let mut arg = OsString::new();
246        arg.push("-Cprofile-generate=");
247        arg.push(path.as_ref());
248        self.cmd.arg(&arg);
249        self
250    }
251
252    /// Specify directory path used for profile usage
253    pub fn profile_use<P: AsRef<Path>>(&mut self, path: P) -> &mut Self {
254        let mut arg = OsString::new();
255        arg.push("-Cprofile-use=");
256        arg.push(path.as_ref());
257        self.cmd.arg(&arg);
258        self
259    }
260
261    /// Specify option of `-C symbol-mangling-version`.
262    pub fn symbol_mangling_version(&mut self, option: &str) -> &mut Self {
263        self.cmd.arg(format!("-Csymbol-mangling-version={option}"));
264        self
265    }
266
267    /// Specify `-C prefer-dynamic`.
268    pub fn prefer_dynamic(&mut self) -> &mut Self {
269        self.cmd.arg(format!("-Cprefer-dynamic"));
270        self
271    }
272
273    /// Specify error format to use
274    pub fn error_format(&mut self, format: &str) -> &mut Self {
275        self.cmd.arg(format!("--error-format={format}"));
276        self
277    }
278
279    /// Specify json messages printed by the compiler
280    pub fn json(&mut self, items: &str) -> &mut Self {
281        self.cmd.arg(format!("--json={items}"));
282        self
283    }
284
285    /// Normalize the line number in the stderr output
286    pub fn ui_testing(&mut self) -> &mut Self {
287        self.cmd.arg(format!("-Zui-testing"));
288        self
289    }
290
291    /// Specify the target triple, or a path to a custom target json spec file.
292    pub fn target<S: AsRef<str>>(&mut self, target: S) -> &mut Self {
293        // We store the target as a separate field, so that it can be specified multiple times.
294        // This is in particular useful to override the default target set in Rustc::new().
295        self.target = Some(target.as_ref().to_string());
296        self
297    }
298
299    /// Specify the target CPU.
300    pub fn target_cpu<S: AsRef<str>>(&mut self, target_cpu: S) -> &mut Self {
301        let target_cpu = target_cpu.as_ref();
302        self.cmd.arg(format!("-Ctarget-cpu={target_cpu}"));
303        self
304    }
305
306    /// Specify the crate type.
307    pub fn crate_type(&mut self, crate_type: &str) -> &mut Self {
308        self.cmd.arg("--crate-type");
309        self.cmd.arg(crate_type);
310        self
311    }
312
313    /// Add a directory to the library search path. Equivalent to `-L` in rustc.
314    pub fn library_search_path<P: AsRef<Path>>(&mut self, path: P) -> &mut Self {
315        self.cmd.arg("-L");
316        self.cmd.arg(path.as_ref());
317        self
318    }
319
320    /// Add a directory to the library search path with a restriction, where `kind` is a dependency
321    /// type. Equivalent to `-L KIND=PATH` in rustc.
322    pub fn specific_library_search_path<P: AsRef<Path>>(
323        &mut self,
324        kind: &str,
325        path: P,
326    ) -> &mut Self {
327        assert!(["dependency", "native", "all", "framework", "crate"].contains(&kind));
328        let path = path.as_ref().to_string_lossy();
329        self.cmd.arg(format!("-L{kind}={path}"));
330        self
331    }
332
333    /// Override the system root. Equivalent to `--sysroot` in rustc.
334    pub fn sysroot<P: AsRef<Path>>(&mut self, path: P) -> &mut Self {
335        self.cmd.arg("--sysroot");
336        self.cmd.arg(path.as_ref());
337        self
338    }
339
340    /// Specify the edition year.
341    pub fn edition(&mut self, edition: &str) -> &mut Self {
342        self.cmd.arg("--edition");
343        self.cmd.arg(edition);
344        self
345    }
346
347    /// Specify the print request.
348    pub fn print(&mut self, request: &str) -> &mut Self {
349        self.cmd.arg("--print");
350        self.cmd.arg(request);
351        self
352    }
353
354    /// Add an extra argument to the linker invocation, via `-Clink-arg`.
355    pub fn link_arg(&mut self, link_arg: &str) -> &mut Self {
356        self.cmd.arg(format!("-Clink-arg={link_arg}"));
357        self
358    }
359
360    /// Add multiple extra arguments to the linker invocation, via `-Clink-args`.
361    pub fn link_args(&mut self, link_args: &str) -> &mut Self {
362        self.cmd.arg(format!("-Clink-args={link_args}"));
363        self
364    }
365
366    /// Specify a stdin input buffer.
367    pub fn stdin_buf<I: AsRef<[u8]>>(&mut self, input: I) -> &mut Self {
368        self.cmd.stdin_buf(input);
369        self
370    }
371
372    /// Specify the crate name.
373    pub fn crate_name<S: AsRef<OsStr>>(&mut self, name: S) -> &mut Self {
374        self.cmd.arg("--crate-name");
375        self.cmd.arg(name.as_ref());
376        self
377    }
378
379    /// Specify the linker
380    pub fn linker(&mut self, linker: &str) -> &mut Self {
381        self.cmd.arg(format!("-Clinker={linker}"));
382        self
383    }
384
385    /// Specify the linker flavor
386    pub fn linker_flavor(&mut self, linker_flavor: &str) -> &mut Self {
387        self.cmd.arg(format!("-Clinker-flavor={linker_flavor}"));
388        self
389    }
390
391    /// Specify `-C debuginfo=...`.
392    pub fn debuginfo(&mut self, level: &str) -> &mut Self {
393        self.cmd.arg(format!("-Cdebuginfo={level}"));
394        self
395    }
396
397    /// Specify `-C split-debuginfo={packed,unpacked,off}`.
398    pub fn split_debuginfo(&mut self, split_kind: &str) -> &mut Self {
399        self.cmd.arg(format!("-Csplit-debuginfo={split_kind}"));
400        self
401    }
402
403    /// Specify `-C link-self-contained={y,n}`.
404    pub fn link_self_contained(&mut self, enabled: bool) -> &mut Self {
405        let enabled = if enabled { "y" } else { "n" };
406        self.cmd.arg(format!("-Clink-self-contained={enabled}"));
407        self
408    }
409
410    pub fn split_dwarf_out_dir(&mut self, out_dir: Option<&str>) -> &mut Self {
411        if let Some(out_dir) = out_dir {
412            self.cmd.arg(format!("-Zsplit-dwarf-out-dir={out_dir}"));
413        }
414        self
415    }
416
417    /// Pass the `--verbose` flag.
418    pub fn verbose(&mut self) -> &mut Self {
419        self.cmd.arg("--verbose");
420        self
421    }
422
423    /// `EXTRARSCXXFLAGS`
424    pub fn extra_rs_cxx_flags(&mut self) -> &mut Self {
425        if is_windows() {
426            // So this is a bit hacky: we can't use the DLL version of libstdc++ because
427            // it pulls in the DLL version of libgcc, which means that we end up with 2
428            // instances of the DW2 unwinding implementation. This is a problem on
429            // i686-pc-windows-gnu because each module (DLL/EXE) needs to register its
430            // unwind information with the unwinding implementation, and libstdc++'s
431            // __cxa_throw won't see the unwinding info we registered with our statically
432            // linked libgcc.
433            //
434            // Now, simply statically linking libstdc++ would fix this problem, except
435            // that it is compiled with the expectation that pthreads is dynamically
436            // linked as a DLL and will fail to link with a statically linked libpthread.
437            //
438            // So we end up with the following hack: we link use static:-bundle to only
439            // link the parts of libstdc++ that we actually use, which doesn't include
440            // the dependency on the pthreads DLL.
441            if !is_windows_msvc() {
442                self.cmd.arg("-lstatic:-bundle=stdc++");
443            };
444        } else if is_darwin() {
445            self.cmd.arg("-lc++");
446        } else if is_aix() {
447            self.cmd.arg("-lc++");
448            self.cmd.arg("-lc++abi");
449        } else {
450            if !matches!(&uname()[..], "FreeBSD" | "SunOS" | "OpenBSD") {
451                self.cmd.arg("-lstdc++");
452            };
453        };
454        self
455    }
456
457    /// Make that the generated LLVM IR is in source order.
458    pub fn codegen_source_order(&mut self) -> &mut Self {
459        self.cmd.arg("-Zcodegen-source-order");
460        self
461    }
462
463    /// Specify `-Z function-sections={yes, no}`.
464    pub fn function_sections(&mut self, enable: bool) -> &mut Self {
465        let flag = match enable {
466            true => "-Zfunction-sections=yes",
467            false => "-Zfunction-sections=no",
468        };
469        self.cmd.arg(flag);
470        self
471    }
472}
473
474/// Query the sysroot path corresponding `rustc --print=sysroot`.
475#[track_caller]
476pub fn sysroot() -> PathBuf {
477    let path = rustc().print("sysroot").run().stdout_utf8();
478    PathBuf::from_str(path.trim()).unwrap()
479}