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