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