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