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