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