]> git.lizzy.rs Git - rust.git/blob - src/bootstrap/config.rs
Rollup merge of #68127 - varkor:clarify-extended-option, r=alexcrichton
[rust.git] / src / bootstrap / config.rs
1 //! Serialized configuration of a build.
2 //!
3 //! This module implements parsing `config.toml` configuration files to tweak
4 //! how the build runs.
5
6 use std::cmp;
7 use std::collections::{HashMap, HashSet};
8 use std::env;
9 use std::ffi::OsString;
10 use std::fs;
11 use std::path::{Path, PathBuf};
12 use std::process;
13
14 use crate::cache::{Interned, INTERNER};
15 use crate::flags::Flags;
16 pub use crate::flags::Subcommand;
17 use build_helper::t;
18 use serde::Deserialize;
19 use toml;
20
21 /// Global configuration for the entire build and/or bootstrap.
22 ///
23 /// This structure is derived from a combination of both `config.toml` and
24 /// `config.mk`. As of the time of this writing it's unlikely that `config.toml`
25 /// is used all that much, so this is primarily filled out by `config.mk` which
26 /// is generated from `./configure`.
27 ///
28 /// Note that this structure is not decoded directly into, but rather it is
29 /// filled out from the decoded forms of the structs below. For documentation
30 /// each field, see the corresponding fields in
31 /// `config.toml.example`.
32 #[derive(Default)]
33 pub struct Config {
34     pub ccache: Option<String>,
35     pub ninja: bool,
36     pub verbose: usize,
37     pub submodules: bool,
38     pub fast_submodules: bool,
39     pub compiler_docs: bool,
40     pub docs: bool,
41     pub locked_deps: bool,
42     pub vendor: bool,
43     pub target_config: HashMap<Interned<String>, Target>,
44     pub full_bootstrap: bool,
45     pub extended: bool,
46     pub tools: Option<HashSet<String>>,
47     pub sanitizers: bool,
48     pub profiler: bool,
49     pub ignore_git: bool,
50     pub exclude: Vec<PathBuf>,
51     pub rustc_error_format: Option<String>,
52     pub test_compare_mode: bool,
53     pub llvm_libunwind: bool,
54
55     pub skip_only_host_steps: bool,
56
57     pub on_fail: Option<String>,
58     pub stage: Option<u32>,
59     pub keep_stage: Vec<u32>,
60     pub src: PathBuf,
61     pub jobs: Option<u32>,
62     pub cmd: Subcommand,
63     pub incremental: bool,
64     pub dry_run: bool,
65
66     pub deny_warnings: bool,
67     pub backtrace_on_ice: bool,
68
69     // llvm codegen options
70     pub llvm_skip_rebuild: bool,
71     pub llvm_assertions: bool,
72     pub llvm_optimize: bool,
73     pub llvm_thin_lto: bool,
74     pub llvm_release_debuginfo: bool,
75     pub llvm_version_check: bool,
76     pub llvm_static_stdcpp: bool,
77     pub llvm_link_shared: bool,
78     pub llvm_clang_cl: Option<String>,
79     pub llvm_targets: Option<String>,
80     pub llvm_experimental_targets: Option<String>,
81     pub llvm_link_jobs: Option<u32>,
82     pub llvm_version_suffix: Option<String>,
83     pub llvm_use_linker: Option<String>,
84     pub llvm_allow_old_toolchain: Option<bool>,
85
86     pub lld_enabled: bool,
87     pub lldb_enabled: bool,
88     pub llvm_tools_enabled: bool,
89
90     pub llvm_cflags: Option<String>,
91     pub llvm_cxxflags: Option<String>,
92     pub llvm_ldflags: Option<String>,
93     pub llvm_use_libcxx: bool,
94
95     // rust codegen options
96     pub rust_optimize: bool,
97     pub rust_codegen_units: Option<u32>,
98     pub rust_codegen_units_std: Option<u32>,
99     pub rust_debug_assertions: bool,
100     pub rust_debuginfo_level_rustc: u32,
101     pub rust_debuginfo_level_std: u32,
102     pub rust_debuginfo_level_tools: u32,
103     pub rust_debuginfo_level_tests: u32,
104     pub rust_rpath: bool,
105     pub rustc_parallel: bool,
106     pub rustc_default_linker: Option<String>,
107     pub rust_optimize_tests: bool,
108     pub rust_dist_src: bool,
109     pub rust_codegen_backends: Vec<Interned<String>>,
110     pub rust_verify_llvm_ir: bool,
111     pub rust_thin_lto_import_instr_limit: Option<u32>,
112     pub rust_remap_debuginfo: bool,
113
114     pub build: Interned<String>,
115     pub hosts: Vec<Interned<String>>,
116     pub targets: Vec<Interned<String>>,
117     pub local_rebuild: bool,
118     pub jemalloc: bool,
119
120     // dist misc
121     pub dist_sign_folder: Option<PathBuf>,
122     pub dist_upload_addr: Option<String>,
123     pub dist_gpg_password_file: Option<PathBuf>,
124
125     // libstd features
126     pub backtrace: bool, // support for RUST_BACKTRACE
127
128     // misc
129     pub low_priority: bool,
130     pub channel: String,
131     pub verbose_tests: bool,
132     pub save_toolstates: Option<PathBuf>,
133     pub print_step_timings: bool,
134     pub missing_tools: bool,
135
136     // Fallback musl-root for all targets
137     pub musl_root: Option<PathBuf>,
138     pub prefix: Option<PathBuf>,
139     pub sysconfdir: Option<PathBuf>,
140     pub datadir: Option<PathBuf>,
141     pub docdir: Option<PathBuf>,
142     pub bindir: PathBuf,
143     pub libdir: Option<PathBuf>,
144     pub mandir: Option<PathBuf>,
145     pub codegen_tests: bool,
146     pub nodejs: Option<PathBuf>,
147     pub gdb: Option<PathBuf>,
148     pub python: Option<PathBuf>,
149     pub cargo_native_static: bool,
150     pub configure_args: Vec<String>,
151
152     // These are either the stage0 downloaded binaries or the locally installed ones.
153     pub initial_cargo: PathBuf,
154     pub initial_rustc: PathBuf,
155     pub initial_rustfmt: Option<PathBuf>,
156     pub out: PathBuf,
157 }
158
159 /// Per-target configuration stored in the global configuration structure.
160 #[derive(Default)]
161 pub struct Target {
162     /// Some(path to llvm-config) if using an external LLVM.
163     pub llvm_config: Option<PathBuf>,
164     /// Some(path to FileCheck) if one was specified.
165     pub llvm_filecheck: Option<PathBuf>,
166     pub cc: Option<PathBuf>,
167     pub cxx: Option<PathBuf>,
168     pub ar: Option<PathBuf>,
169     pub ranlib: Option<PathBuf>,
170     pub linker: Option<PathBuf>,
171     pub ndk: Option<PathBuf>,
172     pub crt_static: Option<bool>,
173     pub musl_root: Option<PathBuf>,
174     pub wasi_root: Option<PathBuf>,
175     pub qemu_rootfs: Option<PathBuf>,
176     pub no_std: bool,
177 }
178
179 /// Structure of the `config.toml` file that configuration is read from.
180 ///
181 /// This structure uses `Decodable` to automatically decode a TOML configuration
182 /// file into this format, and then this is traversed and written into the above
183 /// `Config` structure.
184 #[derive(Deserialize, Default)]
185 #[serde(deny_unknown_fields, rename_all = "kebab-case")]
186 struct TomlConfig {
187     build: Option<Build>,
188     install: Option<Install>,
189     llvm: Option<Llvm>,
190     rust: Option<Rust>,
191     target: Option<HashMap<String, TomlTarget>>,
192     dist: Option<Dist>,
193 }
194
195 /// TOML representation of various global build decisions.
196 #[derive(Deserialize, Default, Clone)]
197 #[serde(deny_unknown_fields, rename_all = "kebab-case")]
198 struct Build {
199     build: Option<String>,
200     #[serde(default)]
201     host: Vec<String>,
202     #[serde(default)]
203     target: Vec<String>,
204     cargo: Option<String>,
205     rustc: Option<String>,
206     rustfmt: Option<String>, /* allow bootstrap.py to use rustfmt key */
207     docs: Option<bool>,
208     compiler_docs: Option<bool>,
209     submodules: Option<bool>,
210     fast_submodules: Option<bool>,
211     gdb: Option<String>,
212     nodejs: Option<String>,
213     python: Option<String>,
214     locked_deps: Option<bool>,
215     vendor: Option<bool>,
216     full_bootstrap: Option<bool>,
217     extended: Option<bool>,
218     tools: Option<HashSet<String>>,
219     verbose: Option<usize>,
220     sanitizers: Option<bool>,
221     profiler: Option<bool>,
222     cargo_native_static: Option<bool>,
223     low_priority: Option<bool>,
224     configure_args: Option<Vec<String>>,
225     local_rebuild: Option<bool>,
226     print_step_timings: Option<bool>,
227 }
228
229 /// TOML representation of various global install decisions.
230 #[derive(Deserialize, Default, Clone)]
231 #[serde(deny_unknown_fields, rename_all = "kebab-case")]
232 struct Install {
233     prefix: Option<String>,
234     sysconfdir: Option<String>,
235     docdir: Option<String>,
236     bindir: Option<String>,
237     libdir: Option<String>,
238     mandir: Option<String>,
239     datadir: Option<String>,
240
241     // standard paths, currently unused
242     infodir: Option<String>,
243     localstatedir: Option<String>,
244 }
245
246 /// TOML representation of how the LLVM build is configured.
247 #[derive(Deserialize, Default)]
248 #[serde(deny_unknown_fields, rename_all = "kebab-case")]
249 struct Llvm {
250     skip_rebuild: Option<bool>,
251     optimize: Option<bool>,
252     thin_lto: Option<bool>,
253     release_debuginfo: Option<bool>,
254     assertions: Option<bool>,
255     ccache: Option<StringOrBool>,
256     version_check: Option<bool>,
257     static_libstdcpp: Option<bool>,
258     ninja: Option<bool>,
259     targets: Option<String>,
260     experimental_targets: Option<String>,
261     link_jobs: Option<u32>,
262     link_shared: Option<bool>,
263     version_suffix: Option<String>,
264     clang_cl: Option<String>,
265     cflags: Option<String>,
266     cxxflags: Option<String>,
267     ldflags: Option<String>,
268     use_libcxx: Option<bool>,
269     use_linker: Option<String>,
270     allow_old_toolchain: Option<bool>,
271 }
272
273 #[derive(Deserialize, Default, Clone)]
274 #[serde(deny_unknown_fields, rename_all = "kebab-case")]
275 struct Dist {
276     sign_folder: Option<String>,
277     gpg_password_file: Option<String>,
278     upload_addr: Option<String>,
279     src_tarball: Option<bool>,
280     missing_tools: Option<bool>,
281 }
282
283 #[derive(Deserialize)]
284 #[serde(untagged)]
285 enum StringOrBool {
286     String(String),
287     Bool(bool),
288 }
289
290 impl Default for StringOrBool {
291     fn default() -> StringOrBool {
292         StringOrBool::Bool(false)
293     }
294 }
295
296 /// TOML representation of how the Rust build is configured.
297 #[derive(Deserialize, Default)]
298 #[serde(deny_unknown_fields, rename_all = "kebab-case")]
299 struct Rust {
300     optimize: Option<bool>,
301     debug: Option<bool>,
302     codegen_units: Option<u32>,
303     codegen_units_std: Option<u32>,
304     debug_assertions: Option<bool>,
305     debuginfo_level: Option<u32>,
306     debuginfo_level_rustc: Option<u32>,
307     debuginfo_level_std: Option<u32>,
308     debuginfo_level_tools: Option<u32>,
309     debuginfo_level_tests: Option<u32>,
310     backtrace: Option<bool>,
311     incremental: Option<bool>,
312     parallel_compiler: Option<bool>,
313     default_linker: Option<String>,
314     channel: Option<String>,
315     musl_root: Option<String>,
316     rpath: Option<bool>,
317     verbose_tests: Option<bool>,
318     optimize_tests: Option<bool>,
319     codegen_tests: Option<bool>,
320     ignore_git: Option<bool>,
321     dist_src: Option<bool>,
322     save_toolstates: Option<String>,
323     codegen_backends: Option<Vec<String>>,
324     lld: Option<bool>,
325     llvm_tools: Option<bool>,
326     lldb: Option<bool>,
327     deny_warnings: Option<bool>,
328     backtrace_on_ice: Option<bool>,
329     verify_llvm_ir: Option<bool>,
330     thin_lto_import_instr_limit: Option<u32>,
331     remap_debuginfo: Option<bool>,
332     jemalloc: Option<bool>,
333     test_compare_mode: Option<bool>,
334     llvm_libunwind: Option<bool>,
335 }
336
337 /// TOML representation of how each build target is configured.
338 #[derive(Deserialize, Default)]
339 #[serde(deny_unknown_fields, rename_all = "kebab-case")]
340 struct TomlTarget {
341     cc: Option<String>,
342     cxx: Option<String>,
343     ar: Option<String>,
344     ranlib: Option<String>,
345     linker: Option<String>,
346     llvm_config: Option<String>,
347     llvm_filecheck: Option<String>,
348     android_ndk: Option<String>,
349     crt_static: Option<bool>,
350     musl_root: Option<String>,
351     wasi_root: Option<String>,
352     qemu_rootfs: Option<String>,
353 }
354
355 impl Config {
356     fn path_from_python(var_key: &str) -> PathBuf {
357         match env::var_os(var_key) {
358             Some(var_val) => Self::normalize_python_path(var_val),
359             _ => panic!("expected '{}' to be set", var_key),
360         }
361     }
362
363     /// Normalizes paths from Python slightly. We don't trust paths from Python (#49785).
364     fn normalize_python_path(path: OsString) -> PathBuf {
365         Path::new(&path).components().collect()
366     }
367
368     pub fn default_opts() -> Config {
369         let mut config = Config::default();
370         config.llvm_optimize = true;
371         config.llvm_version_check = true;
372         config.backtrace = true;
373         config.rust_optimize = true;
374         config.rust_optimize_tests = true;
375         config.submodules = true;
376         config.fast_submodules = true;
377         config.docs = true;
378         config.rust_rpath = true;
379         config.channel = "dev".to_string();
380         config.codegen_tests = true;
381         config.ignore_git = false;
382         config.rust_dist_src = true;
383         config.rust_codegen_backends = vec![INTERNER.intern_str("llvm")];
384         config.deny_warnings = true;
385         config.missing_tools = false;
386
387         // set by bootstrap.py
388         config.build = INTERNER.intern_str(&env::var("BUILD").expect("'BUILD' to be set"));
389         config.src = Config::path_from_python("SRC");
390         config.out = Config::path_from_python("BUILD_DIR");
391
392         config.initial_rustc = Config::path_from_python("RUSTC");
393         config.initial_cargo = Config::path_from_python("CARGO");
394         config.initial_rustfmt = env::var_os("RUSTFMT").map(Config::normalize_python_path);
395
396         config
397     }
398
399     pub fn parse(args: &[String]) -> Config {
400         let flags = Flags::parse(&args);
401         let file = flags.config.clone();
402         let mut config = Config::default_opts();
403         config.exclude = flags.exclude;
404         config.rustc_error_format = flags.rustc_error_format;
405         config.on_fail = flags.on_fail;
406         config.stage = flags.stage;
407         config.jobs = flags.jobs.map(threads_from_config);
408         config.cmd = flags.cmd;
409         config.incremental = flags.incremental;
410         config.dry_run = flags.dry_run;
411         config.keep_stage = flags.keep_stage;
412         config.bindir = "bin".into(); // default
413         if let Some(value) = flags.deny_warnings {
414             config.deny_warnings = value;
415         }
416
417         if config.dry_run {
418             let dir = config.out.join("tmp-dry-run");
419             t!(fs::create_dir_all(&dir));
420             config.out = dir;
421         }
422
423         // If --target was specified but --host wasn't specified, don't run any host-only tests.
424         let has_hosts = !flags.host.is_empty();
425         let has_targets = !flags.target.is_empty();
426         config.skip_only_host_steps = !has_hosts && has_targets;
427
428         let toml = file
429             .map(|file| {
430                 let contents = t!(fs::read_to_string(&file));
431                 match toml::from_str(&contents) {
432                     Ok(table) => table,
433                     Err(err) => {
434                         println!(
435                             "failed to parse TOML configuration '{}': {}",
436                             file.display(),
437                             err
438                         );
439                         process::exit(2);
440                     }
441                 }
442             })
443             .unwrap_or_else(|| TomlConfig::default());
444
445         let build = toml.build.clone().unwrap_or_default();
446         // set by bootstrap.py
447         config.hosts.push(config.build.clone());
448         for host in build.host.iter() {
449             let host = INTERNER.intern_str(host);
450             if !config.hosts.contains(&host) {
451                 config.hosts.push(host);
452             }
453         }
454         for target in
455             config.hosts.iter().cloned().chain(build.target.iter().map(|s| INTERNER.intern_str(s)))
456         {
457             if !config.targets.contains(&target) {
458                 config.targets.push(target);
459             }
460         }
461         config.hosts = if !flags.host.is_empty() { flags.host } else { config.hosts };
462         config.targets = if !flags.target.is_empty() { flags.target } else { config.targets };
463
464         config.nodejs = build.nodejs.map(PathBuf::from);
465         config.gdb = build.gdb.map(PathBuf::from);
466         config.python = build.python.map(PathBuf::from);
467         set(&mut config.low_priority, build.low_priority);
468         set(&mut config.compiler_docs, build.compiler_docs);
469         set(&mut config.docs, build.docs);
470         set(&mut config.submodules, build.submodules);
471         set(&mut config.fast_submodules, build.fast_submodules);
472         set(&mut config.locked_deps, build.locked_deps);
473         set(&mut config.vendor, build.vendor);
474         set(&mut config.full_bootstrap, build.full_bootstrap);
475         set(&mut config.extended, build.extended);
476         config.tools = build.tools;
477         set(&mut config.verbose, build.verbose);
478         set(&mut config.sanitizers, build.sanitizers);
479         set(&mut config.profiler, build.profiler);
480         set(&mut config.cargo_native_static, build.cargo_native_static);
481         set(&mut config.configure_args, build.configure_args);
482         set(&mut config.local_rebuild, build.local_rebuild);
483         set(&mut config.print_step_timings, build.print_step_timings);
484         config.verbose = cmp::max(config.verbose, flags.verbose);
485
486         if let Some(ref install) = toml.install {
487             config.prefix = install.prefix.clone().map(PathBuf::from);
488             config.sysconfdir = install.sysconfdir.clone().map(PathBuf::from);
489             config.datadir = install.datadir.clone().map(PathBuf::from);
490             config.docdir = install.docdir.clone().map(PathBuf::from);
491             set(&mut config.bindir, install.bindir.clone().map(PathBuf::from));
492             config.libdir = install.libdir.clone().map(PathBuf::from);
493             config.mandir = install.mandir.clone().map(PathBuf::from);
494         }
495
496         // We want the llvm-skip-rebuild flag to take precedence over the
497         // skip-rebuild config.toml option so we store it separately
498         // so that we can infer the right value
499         let mut llvm_skip_rebuild = flags.llvm_skip_rebuild;
500
501         // Store off these values as options because if they're not provided
502         // we'll infer default values for them later
503         let mut llvm_assertions = None;
504         let mut debug = None;
505         let mut debug_assertions = None;
506         let mut debuginfo_level = None;
507         let mut debuginfo_level_rustc = None;
508         let mut debuginfo_level_std = None;
509         let mut debuginfo_level_tools = None;
510         let mut debuginfo_level_tests = None;
511         let mut optimize = None;
512         let mut ignore_git = None;
513
514         if let Some(ref llvm) = toml.llvm {
515             match llvm.ccache {
516                 Some(StringOrBool::String(ref s)) => config.ccache = Some(s.to_string()),
517                 Some(StringOrBool::Bool(true)) => {
518                     config.ccache = Some("ccache".to_string());
519                 }
520                 Some(StringOrBool::Bool(false)) | None => {}
521             }
522             set(&mut config.ninja, llvm.ninja);
523             llvm_assertions = llvm.assertions;
524             llvm_skip_rebuild = llvm_skip_rebuild.or(llvm.skip_rebuild);
525             set(&mut config.llvm_optimize, llvm.optimize);
526             set(&mut config.llvm_thin_lto, llvm.thin_lto);
527             set(&mut config.llvm_release_debuginfo, llvm.release_debuginfo);
528             set(&mut config.llvm_version_check, llvm.version_check);
529             set(&mut config.llvm_static_stdcpp, llvm.static_libstdcpp);
530             set(&mut config.llvm_link_shared, llvm.link_shared);
531             config.llvm_targets = llvm.targets.clone();
532             config.llvm_experimental_targets = llvm.experimental_targets.clone();
533             config.llvm_link_jobs = llvm.link_jobs;
534             config.llvm_version_suffix = llvm.version_suffix.clone();
535             config.llvm_clang_cl = llvm.clang_cl.clone();
536
537             config.llvm_cflags = llvm.cflags.clone();
538             config.llvm_cxxflags = llvm.cxxflags.clone();
539             config.llvm_ldflags = llvm.ldflags.clone();
540             set(&mut config.llvm_use_libcxx, llvm.use_libcxx);
541             config.llvm_use_linker = llvm.use_linker.clone();
542             config.llvm_allow_old_toolchain = llvm.allow_old_toolchain.clone();
543         }
544
545         if let Some(ref rust) = toml.rust {
546             debug = rust.debug;
547             debug_assertions = rust.debug_assertions;
548             debuginfo_level = rust.debuginfo_level;
549             debuginfo_level_rustc = rust.debuginfo_level_rustc;
550             debuginfo_level_std = rust.debuginfo_level_std;
551             debuginfo_level_tools = rust.debuginfo_level_tools;
552             debuginfo_level_tests = rust.debuginfo_level_tests;
553             optimize = rust.optimize;
554             ignore_git = rust.ignore_git;
555             set(&mut config.rust_optimize_tests, rust.optimize_tests);
556             set(&mut config.codegen_tests, rust.codegen_tests);
557             set(&mut config.rust_rpath, rust.rpath);
558             set(&mut config.jemalloc, rust.jemalloc);
559             set(&mut config.test_compare_mode, rust.test_compare_mode);
560             set(&mut config.llvm_libunwind, rust.llvm_libunwind);
561             set(&mut config.backtrace, rust.backtrace);
562             set(&mut config.channel, rust.channel.clone());
563             set(&mut config.rust_dist_src, rust.dist_src);
564             set(&mut config.verbose_tests, rust.verbose_tests);
565             // in the case "false" is set explicitly, do not overwrite the command line args
566             if let Some(true) = rust.incremental {
567                 config.incremental = true;
568             }
569             set(&mut config.lld_enabled, rust.lld);
570             set(&mut config.lldb_enabled, rust.lldb);
571             set(&mut config.llvm_tools_enabled, rust.llvm_tools);
572             config.rustc_parallel = rust.parallel_compiler.unwrap_or(false);
573             config.rustc_default_linker = rust.default_linker.clone();
574             config.musl_root = rust.musl_root.clone().map(PathBuf::from);
575             config.save_toolstates = rust.save_toolstates.clone().map(PathBuf::from);
576             set(&mut config.deny_warnings, flags.deny_warnings.or(rust.deny_warnings));
577             set(&mut config.backtrace_on_ice, rust.backtrace_on_ice);
578             set(&mut config.rust_verify_llvm_ir, rust.verify_llvm_ir);
579             config.rust_thin_lto_import_instr_limit = rust.thin_lto_import_instr_limit;
580             set(&mut config.rust_remap_debuginfo, rust.remap_debuginfo);
581
582             if let Some(ref backends) = rust.codegen_backends {
583                 config.rust_codegen_backends =
584                     backends.iter().map(|s| INTERNER.intern_str(s)).collect();
585             }
586
587             config.rust_codegen_units = rust.codegen_units.map(threads_from_config);
588             config.rust_codegen_units_std = rust.codegen_units_std.map(threads_from_config);
589         }
590
591         if let Some(ref t) = toml.target {
592             for (triple, cfg) in t {
593                 let mut target = Target::default();
594
595                 if let Some(ref s) = cfg.llvm_config {
596                     target.llvm_config = Some(config.src.join(s));
597                 }
598                 if let Some(ref s) = cfg.llvm_filecheck {
599                     target.llvm_filecheck = Some(config.src.join(s));
600                 }
601                 if let Some(ref s) = cfg.android_ndk {
602                     target.ndk = Some(config.src.join(s));
603                 }
604                 target.cc = cfg.cc.clone().map(PathBuf::from);
605                 target.cxx = cfg.cxx.clone().map(PathBuf::from);
606                 target.ar = cfg.ar.clone().map(PathBuf::from);
607                 target.ranlib = cfg.ranlib.clone().map(PathBuf::from);
608                 target.linker = cfg.linker.clone().map(PathBuf::from);
609                 target.crt_static = cfg.crt_static.clone();
610                 target.musl_root = cfg.musl_root.clone().map(PathBuf::from);
611                 target.wasi_root = cfg.wasi_root.clone().map(PathBuf::from);
612                 target.qemu_rootfs = cfg.qemu_rootfs.clone().map(PathBuf::from);
613
614                 config.target_config.insert(INTERNER.intern_string(triple.clone()), target);
615             }
616         }
617
618         if let Some(ref t) = toml.dist {
619             config.dist_sign_folder = t.sign_folder.clone().map(PathBuf::from);
620             config.dist_gpg_password_file = t.gpg_password_file.clone().map(PathBuf::from);
621             config.dist_upload_addr = t.upload_addr.clone();
622             set(&mut config.rust_dist_src, t.src_tarball);
623             set(&mut config.missing_tools, t.missing_tools);
624         }
625
626         // Now that we've reached the end of our configuration, infer the
627         // default values for all options that we haven't otherwise stored yet.
628
629         set(&mut config.initial_rustc, build.rustc.map(PathBuf::from));
630         set(&mut config.initial_cargo, build.cargo.map(PathBuf::from));
631
632         config.llvm_skip_rebuild = llvm_skip_rebuild.unwrap_or(false);
633
634         let default = false;
635         config.llvm_assertions = llvm_assertions.unwrap_or(default);
636
637         let default = true;
638         config.rust_optimize = optimize.unwrap_or(default);
639
640         let default = debug == Some(true);
641         config.rust_debug_assertions = debug_assertions.unwrap_or(default);
642
643         let with_defaults = |debuginfo_level_specific: Option<u32>| {
644             debuginfo_level_specific.or(debuginfo_level).unwrap_or(if debug == Some(true) {
645                 2
646             } else {
647                 0
648             })
649         };
650         config.rust_debuginfo_level_rustc = with_defaults(debuginfo_level_rustc);
651         config.rust_debuginfo_level_std = with_defaults(debuginfo_level_std);
652         config.rust_debuginfo_level_tools = with_defaults(debuginfo_level_tools);
653         config.rust_debuginfo_level_tests = debuginfo_level_tests.unwrap_or(0);
654
655         let default = config.channel == "dev";
656         config.ignore_git = ignore_git.unwrap_or(default);
657
658         config
659     }
660
661     /// Try to find the relative path of `bindir`, otherwise return it in full.
662     pub fn bindir_relative(&self) -> &Path {
663         let bindir = &self.bindir;
664         if bindir.is_absolute() {
665             // Try to make it relative to the prefix.
666             if let Some(prefix) = &self.prefix {
667                 if let Ok(stripped) = bindir.strip_prefix(prefix) {
668                     return stripped;
669                 }
670             }
671         }
672         bindir
673     }
674
675     /// Try to find the relative path of `libdir`.
676     pub fn libdir_relative(&self) -> Option<&Path> {
677         let libdir = self.libdir.as_ref()?;
678         if libdir.is_relative() {
679             Some(libdir)
680         } else {
681             // Try to make it relative to the prefix.
682             libdir.strip_prefix(self.prefix.as_ref()?).ok()
683         }
684     }
685
686     pub fn verbose(&self) -> bool {
687         self.verbose > 0
688     }
689
690     pub fn very_verbose(&self) -> bool {
691         self.verbose > 1
692     }
693
694     pub fn llvm_enabled(&self) -> bool {
695         self.rust_codegen_backends.contains(&INTERNER.intern_str("llvm"))
696     }
697 }
698
699 fn set<T>(field: &mut T, val: Option<T>) {
700     if let Some(v) = val {
701         *field = v;
702     }
703 }
704
705 fn threads_from_config(v: u32) -> u32 {
706     match v {
707         0 => num_cpus::get() as u32,
708         n => n,
709     }
710 }