]> git.lizzy.rs Git - rust.git/blob - src/bootstrap/config.rs
Rollup merge of #76050 - matklad:pos, 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     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.ninja = true;
454         config.llvm_version_check = true;
455         config.backtrace = true;
456         config.rust_optimize = true;
457         config.rust_optimize_tests = true;
458         config.submodules = true;
459         config.fast_submodules = true;
460         config.docs = true;
461         config.rust_rpath = true;
462         config.channel = "dev".to_string();
463         config.codegen_tests = true;
464         config.ignore_git = false;
465         config.rust_dist_src = true;
466         config.rust_codegen_backends = vec![INTERNER.intern_str("llvm")];
467         config.deny_warnings = true;
468         config.missing_tools = false;
469
470         // set by bootstrap.py
471         config.build = TargetSelection::from_user(&env::var("BUILD").expect("'BUILD' to be set"));
472         config.src = Config::path_from_python("SRC");
473         config.out = Config::path_from_python("BUILD_DIR");
474
475         config.initial_rustc = Config::path_from_python("RUSTC");
476         config.initial_cargo = Config::path_from_python("CARGO");
477         config.initial_rustfmt = env::var_os("RUSTFMT").map(Config::normalize_python_path);
478
479         config
480     }
481
482     pub fn parse(args: &[String]) -> Config {
483         let flags = Flags::parse(&args);
484         let file = flags.config.clone();
485         let mut config = Config::default_opts();
486         config.exclude = flags.exclude;
487         config.rustc_error_format = flags.rustc_error_format;
488         config.json_output = flags.json_output;
489         config.on_fail = flags.on_fail;
490         config.stage = flags.stage;
491         config.jobs = flags.jobs.map(threads_from_config);
492         config.cmd = flags.cmd;
493         config.incremental = flags.incremental;
494         config.dry_run = flags.dry_run;
495         config.keep_stage = flags.keep_stage;
496         config.bindir = "bin".into(); // default
497         if let Some(value) = flags.deny_warnings {
498             config.deny_warnings = value;
499         }
500
501         if config.dry_run {
502             let dir = config.out.join("tmp-dry-run");
503             t!(fs::create_dir_all(&dir));
504             config.out = dir;
505         }
506
507         // If --target was specified but --host wasn't specified, don't run any host-only tests.
508         let has_hosts = !flags.host.is_empty();
509         let has_targets = !flags.target.is_empty();
510         config.skip_only_host_steps = !has_hosts && has_targets;
511
512         let toml = file
513             .map(|file| {
514                 let contents = t!(fs::read_to_string(&file));
515                 match toml::from_str(&contents) {
516                     Ok(table) => table,
517                     Err(err) => {
518                         println!(
519                             "failed to parse TOML configuration '{}': {}",
520                             file.display(),
521                             err
522                         );
523                         process::exit(2);
524                     }
525                 }
526             })
527             .unwrap_or_else(TomlConfig::default);
528
529         let build = toml.build.clone().unwrap_or_default();
530         // set by bootstrap.py
531         config.hosts.push(config.build);
532         for host in build.host.iter().map(|h| TargetSelection::from_user(h)) {
533             if !config.hosts.contains(&host) {
534                 config.hosts.push(host);
535             }
536         }
537         for target in config
538             .hosts
539             .iter()
540             .copied()
541             .chain(build.target.iter().map(|h| TargetSelection::from_user(h)))
542         {
543             if !config.targets.contains(&target) {
544                 config.targets.push(target);
545             }
546         }
547         config.hosts = if !flags.host.is_empty() { flags.host } else { config.hosts };
548         config.targets = if !flags.target.is_empty() { flags.target } else { config.targets };
549
550         config.nodejs = build.nodejs.map(PathBuf::from);
551         config.gdb = build.gdb.map(PathBuf::from);
552         config.python = build.python.map(PathBuf::from);
553         set(&mut config.low_priority, build.low_priority);
554         set(&mut config.compiler_docs, build.compiler_docs);
555         set(&mut config.docs, build.docs);
556         set(&mut config.submodules, build.submodules);
557         set(&mut config.fast_submodules, build.fast_submodules);
558         set(&mut config.locked_deps, build.locked_deps);
559         set(&mut config.vendor, build.vendor);
560         set(&mut config.full_bootstrap, build.full_bootstrap);
561         set(&mut config.extended, build.extended);
562         config.tools = build.tools;
563         set(&mut config.verbose, build.verbose);
564         set(&mut config.sanitizers, build.sanitizers);
565         set(&mut config.profiler, build.profiler);
566         set(&mut config.cargo_native_static, build.cargo_native_static);
567         set(&mut config.configure_args, build.configure_args);
568         set(&mut config.local_rebuild, build.local_rebuild);
569         set(&mut config.print_step_timings, build.print_step_timings);
570         config.verbose = cmp::max(config.verbose, flags.verbose);
571
572         if let Some(ref install) = toml.install {
573             config.prefix = install.prefix.clone().map(PathBuf::from);
574             config.sysconfdir = install.sysconfdir.clone().map(PathBuf::from);
575             config.datadir = install.datadir.clone().map(PathBuf::from);
576             config.docdir = install.docdir.clone().map(PathBuf::from);
577             set(&mut config.bindir, install.bindir.clone().map(PathBuf::from));
578             config.libdir = install.libdir.clone().map(PathBuf::from);
579             config.mandir = install.mandir.clone().map(PathBuf::from);
580         }
581
582         // We want the llvm-skip-rebuild flag to take precedence over the
583         // skip-rebuild config.toml option so we store it separately
584         // so that we can infer the right value
585         let mut llvm_skip_rebuild = flags.llvm_skip_rebuild;
586
587         // Store off these values as options because if they're not provided
588         // we'll infer default values for them later
589         let mut llvm_assertions = None;
590         let mut debug = None;
591         let mut debug_assertions = None;
592         let mut debug_assertions_std = None;
593         let mut debuginfo_level = None;
594         let mut debuginfo_level_rustc = None;
595         let mut debuginfo_level_std = None;
596         let mut debuginfo_level_tools = None;
597         let mut debuginfo_level_tests = None;
598         let mut optimize = None;
599         let mut ignore_git = None;
600
601         if let Some(ref llvm) = toml.llvm {
602             match llvm.ccache {
603                 Some(StringOrBool::String(ref s)) => config.ccache = Some(s.to_string()),
604                 Some(StringOrBool::Bool(true)) => {
605                     config.ccache = Some("ccache".to_string());
606                 }
607                 Some(StringOrBool::Bool(false)) | None => {}
608             }
609             set(&mut config.ninja, llvm.ninja);
610             llvm_assertions = llvm.assertions;
611             llvm_skip_rebuild = llvm_skip_rebuild.or(llvm.skip_rebuild);
612             set(&mut config.llvm_optimize, llvm.optimize);
613             set(&mut config.llvm_thin_lto, llvm.thin_lto);
614             set(&mut config.llvm_release_debuginfo, llvm.release_debuginfo);
615             set(&mut config.llvm_version_check, llvm.version_check);
616             set(&mut config.llvm_static_stdcpp, llvm.static_libstdcpp);
617             set(&mut config.llvm_link_shared, llvm.link_shared);
618             config.llvm_targets = llvm.targets.clone();
619             config.llvm_experimental_targets = llvm.experimental_targets.clone();
620             config.llvm_link_jobs = llvm.link_jobs;
621             config.llvm_version_suffix = llvm.version_suffix.clone();
622             config.llvm_clang_cl = llvm.clang_cl.clone();
623
624             config.llvm_cflags = llvm.cflags.clone();
625             config.llvm_cxxflags = llvm.cxxflags.clone();
626             config.llvm_ldflags = llvm.ldflags.clone();
627             set(&mut config.llvm_use_libcxx, llvm.use_libcxx);
628             config.llvm_use_linker = llvm.use_linker.clone();
629             config.llvm_allow_old_toolchain = llvm.allow_old_toolchain;
630         }
631
632         if let Some(ref rust) = toml.rust {
633             debug = rust.debug;
634             debug_assertions = rust.debug_assertions;
635             debug_assertions_std = rust.debug_assertions_std;
636             debuginfo_level = rust.debuginfo_level;
637             debuginfo_level_rustc = rust.debuginfo_level_rustc;
638             debuginfo_level_std = rust.debuginfo_level_std;
639             debuginfo_level_tools = rust.debuginfo_level_tools;
640             debuginfo_level_tests = rust.debuginfo_level_tests;
641             optimize = rust.optimize;
642             ignore_git = rust.ignore_git;
643             set(&mut config.rust_new_symbol_mangling, rust.new_symbol_mangling);
644             set(&mut config.rust_optimize_tests, rust.optimize_tests);
645             set(&mut config.codegen_tests, rust.codegen_tests);
646             set(&mut config.rust_rpath, rust.rpath);
647             set(&mut config.jemalloc, rust.jemalloc);
648             set(&mut config.test_compare_mode, rust.test_compare_mode);
649             set(&mut config.llvm_libunwind, rust.llvm_libunwind);
650             set(&mut config.backtrace, rust.backtrace);
651             set(&mut config.channel, rust.channel.clone());
652             set(&mut config.rust_dist_src, rust.dist_src);
653             set(&mut config.verbose_tests, rust.verbose_tests);
654             // in the case "false" is set explicitly, do not overwrite the command line args
655             if let Some(true) = rust.incremental {
656                 config.incremental = true;
657             }
658             set(&mut config.use_lld, rust.use_lld);
659             set(&mut config.lld_enabled, rust.lld);
660             set(&mut config.llvm_tools_enabled, rust.llvm_tools);
661             config.rustc_parallel = rust.parallel_compiler.unwrap_or(false);
662             config.rustc_default_linker = rust.default_linker.clone();
663             config.musl_root = rust.musl_root.clone().map(PathBuf::from);
664             config.save_toolstates = rust.save_toolstates.clone().map(PathBuf::from);
665             set(&mut config.deny_warnings, flags.deny_warnings.or(rust.deny_warnings));
666             set(&mut config.backtrace_on_ice, rust.backtrace_on_ice);
667             set(&mut config.rust_verify_llvm_ir, rust.verify_llvm_ir);
668             config.rust_thin_lto_import_instr_limit = rust.thin_lto_import_instr_limit;
669             set(&mut config.rust_remap_debuginfo, rust.remap_debuginfo);
670             set(&mut config.control_flow_guard, rust.control_flow_guard);
671
672             if let Some(ref backends) = rust.codegen_backends {
673                 config.rust_codegen_backends =
674                     backends.iter().map(|s| INTERNER.intern_str(s)).collect();
675             }
676
677             config.rust_codegen_units = rust.codegen_units.map(threads_from_config);
678             config.rust_codegen_units_std = rust.codegen_units_std.map(threads_from_config);
679         }
680
681         if let Some(ref t) = toml.target {
682             for (triple, cfg) in t {
683                 let mut target = Target::from_triple(triple);
684
685                 if let Some(ref s) = cfg.llvm_config {
686                     target.llvm_config = Some(config.src.join(s));
687                 }
688                 if let Some(ref s) = cfg.llvm_filecheck {
689                     target.llvm_filecheck = Some(config.src.join(s));
690                 }
691                 if let Some(ref s) = cfg.android_ndk {
692                     target.ndk = Some(config.src.join(s));
693                 }
694                 if let Some(s) = cfg.no_std {
695                     target.no_std = s;
696                 }
697                 target.cc = cfg.cc.clone().map(PathBuf::from);
698                 target.cxx = cfg.cxx.clone().map(PathBuf::from);
699                 target.ar = cfg.ar.clone().map(PathBuf::from);
700                 target.ranlib = cfg.ranlib.clone().map(PathBuf::from);
701                 target.linker = cfg.linker.clone().map(PathBuf::from);
702                 target.crt_static = cfg.crt_static;
703                 target.musl_root = cfg.musl_root.clone().map(PathBuf::from);
704                 target.musl_libdir = cfg.musl_libdir.clone().map(PathBuf::from);
705                 target.wasi_root = cfg.wasi_root.clone().map(PathBuf::from);
706                 target.qemu_rootfs = cfg.qemu_rootfs.clone().map(PathBuf::from);
707
708                 config.target_config.insert(TargetSelection::from_user(triple), target);
709             }
710         }
711
712         if let Some(ref t) = toml.dist {
713             config.dist_sign_folder = t.sign_folder.clone().map(PathBuf::from);
714             config.dist_gpg_password_file = t.gpg_password_file.clone().map(PathBuf::from);
715             config.dist_upload_addr = t.upload_addr.clone();
716             set(&mut config.rust_dist_src, t.src_tarball);
717             set(&mut config.missing_tools, t.missing_tools);
718         }
719
720         // Now that we've reached the end of our configuration, infer the
721         // default values for all options that we haven't otherwise stored yet.
722
723         set(&mut config.initial_rustc, build.rustc.map(PathBuf::from));
724         set(&mut config.initial_cargo, build.cargo.map(PathBuf::from));
725
726         config.llvm_skip_rebuild = llvm_skip_rebuild.unwrap_or(false);
727
728         let default = false;
729         config.llvm_assertions = llvm_assertions.unwrap_or(default);
730
731         let default = true;
732         config.rust_optimize = optimize.unwrap_or(default);
733
734         let default = debug == Some(true);
735         config.rust_debug_assertions = debug_assertions.unwrap_or(default);
736         config.rust_debug_assertions_std =
737             debug_assertions_std.unwrap_or(config.rust_debug_assertions);
738
739         let with_defaults = |debuginfo_level_specific: Option<u32>| {
740             debuginfo_level_specific.or(debuginfo_level).unwrap_or(if debug == Some(true) {
741                 1
742             } else {
743                 0
744             })
745         };
746         config.rust_debuginfo_level_rustc = with_defaults(debuginfo_level_rustc);
747         config.rust_debuginfo_level_std = with_defaults(debuginfo_level_std);
748         config.rust_debuginfo_level_tools = with_defaults(debuginfo_level_tools);
749         config.rust_debuginfo_level_tests = debuginfo_level_tests.unwrap_or(0);
750
751         let default = config.channel == "dev";
752         config.ignore_git = ignore_git.unwrap_or(default);
753
754         config
755     }
756
757     /// Try to find the relative path of `bindir`, otherwise return it in full.
758     pub fn bindir_relative(&self) -> &Path {
759         let bindir = &self.bindir;
760         if bindir.is_absolute() {
761             // Try to make it relative to the prefix.
762             if let Some(prefix) = &self.prefix {
763                 if let Ok(stripped) = bindir.strip_prefix(prefix) {
764                     return stripped;
765                 }
766             }
767         }
768         bindir
769     }
770
771     /// Try to find the relative path of `libdir`.
772     pub fn libdir_relative(&self) -> Option<&Path> {
773         let libdir = self.libdir.as_ref()?;
774         if libdir.is_relative() {
775             Some(libdir)
776         } else {
777             // Try to make it relative to the prefix.
778             libdir.strip_prefix(self.prefix.as_ref()?).ok()
779         }
780     }
781
782     pub fn verbose(&self) -> bool {
783         self.verbose > 0
784     }
785
786     pub fn very_verbose(&self) -> bool {
787         self.verbose > 1
788     }
789
790     pub fn llvm_enabled(&self) -> bool {
791         self.rust_codegen_backends.contains(&INTERNER.intern_str("llvm"))
792     }
793 }
794
795 fn set<T>(field: &mut T, val: Option<T>) {
796     if let Some(v) = val {
797         *field = v;
798     }
799 }
800
801 fn threads_from_config(v: u32) -> u32 {
802     match v {
803         0 => num_cpus::get() as u32,
804         n => n,
805     }
806 }