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