]> git.lizzy.rs Git - rust.git/blob - src/bootstrap/config.rs
Properly encode spans with a dummy location and non-root `SyntaxContext`
[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     host: Option<Vec<String>>,
277     target: Option<Vec<String>>,
278     // This is ignored, the rust code always gets the build directory from the `BUILD_DIR` env variable
279     build_dir: Option<String>,
280     cargo: Option<String>,
281     rustc: Option<String>,
282     rustfmt: Option<String>, /* allow bootstrap.py to use rustfmt key */
283     docs: Option<bool>,
284     compiler_docs: Option<bool>,
285     submodules: Option<bool>,
286     fast_submodules: Option<bool>,
287     gdb: Option<String>,
288     nodejs: Option<String>,
289     python: Option<String>,
290     locked_deps: Option<bool>,
291     vendor: Option<bool>,
292     full_bootstrap: Option<bool>,
293     extended: Option<bool>,
294     tools: Option<HashSet<String>>,
295     verbose: Option<usize>,
296     sanitizers: Option<bool>,
297     profiler: Option<bool>,
298     cargo_native_static: Option<bool>,
299     low_priority: Option<bool>,
300     configure_args: Option<Vec<String>>,
301     local_rebuild: Option<bool>,
302     print_step_timings: Option<bool>,
303 }
304
305 /// TOML representation of various global install decisions.
306 #[derive(Deserialize, Default, Clone)]
307 #[serde(deny_unknown_fields, rename_all = "kebab-case")]
308 struct Install {
309     prefix: Option<String>,
310     sysconfdir: Option<String>,
311     docdir: Option<String>,
312     bindir: Option<String>,
313     libdir: Option<String>,
314     mandir: Option<String>,
315     datadir: Option<String>,
316
317     // standard paths, currently unused
318     infodir: Option<String>,
319     localstatedir: Option<String>,
320 }
321
322 /// TOML representation of how the LLVM build is configured.
323 #[derive(Deserialize, Default)]
324 #[serde(deny_unknown_fields, rename_all = "kebab-case")]
325 struct Llvm {
326     skip_rebuild: Option<bool>,
327     optimize: Option<bool>,
328     thin_lto: Option<bool>,
329     release_debuginfo: Option<bool>,
330     assertions: Option<bool>,
331     ccache: Option<StringOrBool>,
332     version_check: Option<bool>,
333     static_libstdcpp: Option<bool>,
334     ninja: Option<bool>,
335     targets: Option<String>,
336     experimental_targets: Option<String>,
337     link_jobs: Option<u32>,
338     link_shared: Option<bool>,
339     version_suffix: Option<String>,
340     clang_cl: Option<String>,
341     cflags: Option<String>,
342     cxxflags: Option<String>,
343     ldflags: Option<String>,
344     use_libcxx: Option<bool>,
345     use_linker: Option<String>,
346     allow_old_toolchain: Option<bool>,
347 }
348
349 #[derive(Deserialize, Default, Clone)]
350 #[serde(deny_unknown_fields, rename_all = "kebab-case")]
351 struct Dist {
352     sign_folder: Option<String>,
353     gpg_password_file: Option<String>,
354     upload_addr: Option<String>,
355     src_tarball: Option<bool>,
356     missing_tools: Option<bool>,
357 }
358
359 #[derive(Deserialize)]
360 #[serde(untagged)]
361 enum StringOrBool {
362     String(String),
363     Bool(bool),
364 }
365
366 impl Default for StringOrBool {
367     fn default() -> StringOrBool {
368         StringOrBool::Bool(false)
369     }
370 }
371
372 /// TOML representation of how the Rust build is configured.
373 #[derive(Deserialize, Default)]
374 #[serde(deny_unknown_fields, rename_all = "kebab-case")]
375 struct Rust {
376     optimize: Option<bool>,
377     debug: Option<bool>,
378     codegen_units: Option<u32>,
379     codegen_units_std: Option<u32>,
380     debug_assertions: Option<bool>,
381     debug_assertions_std: Option<bool>,
382     debuginfo_level: Option<u32>,
383     debuginfo_level_rustc: Option<u32>,
384     debuginfo_level_std: Option<u32>,
385     debuginfo_level_tools: Option<u32>,
386     debuginfo_level_tests: Option<u32>,
387     backtrace: Option<bool>,
388     incremental: Option<bool>,
389     parallel_compiler: Option<bool>,
390     default_linker: Option<String>,
391     channel: Option<String>,
392     musl_root: Option<String>,
393     rpath: Option<bool>,
394     verbose_tests: Option<bool>,
395     optimize_tests: Option<bool>,
396     codegen_tests: Option<bool>,
397     ignore_git: Option<bool>,
398     dist_src: Option<bool>,
399     save_toolstates: Option<String>,
400     codegen_backends: Option<Vec<String>>,
401     lld: Option<bool>,
402     use_lld: Option<bool>,
403     llvm_tools: Option<bool>,
404     deny_warnings: Option<bool>,
405     backtrace_on_ice: Option<bool>,
406     verify_llvm_ir: Option<bool>,
407     thin_lto_import_instr_limit: Option<u32>,
408     remap_debuginfo: Option<bool>,
409     jemalloc: Option<bool>,
410     test_compare_mode: Option<bool>,
411     llvm_libunwind: Option<bool>,
412     control_flow_guard: Option<bool>,
413     new_symbol_mangling: Option<bool>,
414 }
415
416 /// TOML representation of how each build target is configured.
417 #[derive(Deserialize, Default)]
418 #[serde(deny_unknown_fields, rename_all = "kebab-case")]
419 struct TomlTarget {
420     cc: Option<String>,
421     cxx: Option<String>,
422     ar: Option<String>,
423     ranlib: Option<String>,
424     linker: Option<String>,
425     llvm_config: Option<String>,
426     llvm_filecheck: Option<String>,
427     android_ndk: Option<String>,
428     crt_static: Option<bool>,
429     musl_root: Option<String>,
430     musl_libdir: Option<String>,
431     wasi_root: Option<String>,
432     qemu_rootfs: Option<String>,
433     no_std: Option<bool>,
434 }
435
436 impl Config {
437     fn path_from_python(var_key: &str) -> PathBuf {
438         match env::var_os(var_key) {
439             Some(var_val) => Self::normalize_python_path(var_val),
440             _ => panic!("expected '{}' to be set", var_key),
441         }
442     }
443
444     /// Normalizes paths from Python slightly. We don't trust paths from Python (#49785).
445     fn normalize_python_path(path: OsString) -> PathBuf {
446         Path::new(&path).components().collect()
447     }
448
449     pub fn default_opts() -> Config {
450         let mut config = Config::default();
451         config.llvm_optimize = true;
452         config.ninja_in_file = 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         let toml = file
507             .map(|file| {
508                 let contents = t!(fs::read_to_string(&file));
509                 match toml::from_str(&contents) {
510                     Ok(table) => table,
511                     Err(err) => {
512                         println!(
513                             "failed to parse TOML configuration '{}': {}",
514                             file.display(),
515                             err
516                         );
517                         process::exit(2);
518                     }
519                 }
520             })
521             .unwrap_or_else(TomlConfig::default);
522
523         let build = toml.build.clone().unwrap_or_default();
524
525         // If --target was specified but --host wasn't specified, don't run any host-only tests.
526         let has_hosts = build.host.is_some() || flags.host.is_some();
527         let has_targets = build.target.is_some() || flags.target.is_some();
528         config.skip_only_host_steps = !has_hosts && has_targets;
529
530         config.hosts = if let Some(arg_host) = flags.host.clone() {
531             arg_host
532         } else if let Some(file_host) = build.host {
533             file_host.iter().map(|h| TargetSelection::from_user(h)).collect()
534         } else {
535             vec![config.build]
536         };
537         config.targets = if let Some(arg_target) = flags.target.clone() {
538             arg_target
539         } else if let Some(file_target) = build.target {
540             file_target.iter().map(|h| TargetSelection::from_user(h)).collect()
541         } else {
542             // If target is *not* configured, then default to the host
543             // toolchains.
544             config.hosts.clone()
545         };
546
547         config.nodejs = build.nodejs.map(PathBuf::from);
548         config.gdb = build.gdb.map(PathBuf::from);
549         config.python = build.python.map(PathBuf::from);
550         set(&mut config.low_priority, build.low_priority);
551         set(&mut config.compiler_docs, build.compiler_docs);
552         set(&mut config.docs, build.docs);
553         set(&mut config.submodules, build.submodules);
554         set(&mut config.fast_submodules, build.fast_submodules);
555         set(&mut config.locked_deps, build.locked_deps);
556         set(&mut config.vendor, build.vendor);
557         set(&mut config.full_bootstrap, build.full_bootstrap);
558         set(&mut config.extended, build.extended);
559         config.tools = build.tools;
560         set(&mut config.verbose, build.verbose);
561         set(&mut config.sanitizers, build.sanitizers);
562         set(&mut config.profiler, build.profiler);
563         set(&mut config.cargo_native_static, build.cargo_native_static);
564         set(&mut config.configure_args, build.configure_args);
565         set(&mut config.local_rebuild, build.local_rebuild);
566         set(&mut config.print_step_timings, build.print_step_timings);
567         config.verbose = cmp::max(config.verbose, flags.verbose);
568
569         if let Some(ref install) = toml.install {
570             config.prefix = install.prefix.clone().map(PathBuf::from);
571             config.sysconfdir = install.sysconfdir.clone().map(PathBuf::from);
572             config.datadir = install.datadir.clone().map(PathBuf::from);
573             config.docdir = install.docdir.clone().map(PathBuf::from);
574             set(&mut config.bindir, install.bindir.clone().map(PathBuf::from));
575             config.libdir = install.libdir.clone().map(PathBuf::from);
576             config.mandir = install.mandir.clone().map(PathBuf::from);
577         }
578
579         // We want the llvm-skip-rebuild flag to take precedence over the
580         // skip-rebuild config.toml option so we store it separately
581         // so that we can infer the right value
582         let mut llvm_skip_rebuild = flags.llvm_skip_rebuild;
583
584         // Store off these values as options because if they're not provided
585         // we'll infer default values for them later
586         let mut llvm_assertions = None;
587         let mut debug = None;
588         let mut debug_assertions = None;
589         let mut debug_assertions_std = None;
590         let mut debuginfo_level = None;
591         let mut debuginfo_level_rustc = None;
592         let mut debuginfo_level_std = None;
593         let mut debuginfo_level_tools = None;
594         let mut debuginfo_level_tests = None;
595         let mut optimize = None;
596         let mut ignore_git = None;
597
598         if let Some(ref llvm) = toml.llvm {
599             match llvm.ccache {
600                 Some(StringOrBool::String(ref s)) => config.ccache = Some(s.to_string()),
601                 Some(StringOrBool::Bool(true)) => {
602                     config.ccache = Some("ccache".to_string());
603                 }
604                 Some(StringOrBool::Bool(false)) | None => {}
605             }
606             set(&mut config.ninja_in_file, llvm.ninja);
607             llvm_assertions = llvm.assertions;
608             llvm_skip_rebuild = llvm_skip_rebuild.or(llvm.skip_rebuild);
609             set(&mut config.llvm_optimize, llvm.optimize);
610             set(&mut config.llvm_thin_lto, llvm.thin_lto);
611             set(&mut config.llvm_release_debuginfo, llvm.release_debuginfo);
612             set(&mut config.llvm_version_check, llvm.version_check);
613             set(&mut config.llvm_static_stdcpp, llvm.static_libstdcpp);
614             set(&mut config.llvm_link_shared, llvm.link_shared);
615             config.llvm_targets = llvm.targets.clone();
616             config.llvm_experimental_targets = llvm.experimental_targets.clone();
617             config.llvm_link_jobs = llvm.link_jobs;
618             config.llvm_version_suffix = llvm.version_suffix.clone();
619             config.llvm_clang_cl = llvm.clang_cl.clone();
620
621             config.llvm_cflags = llvm.cflags.clone();
622             config.llvm_cxxflags = llvm.cxxflags.clone();
623             config.llvm_ldflags = llvm.ldflags.clone();
624             set(&mut config.llvm_use_libcxx, llvm.use_libcxx);
625             config.llvm_use_linker = llvm.use_linker.clone();
626             config.llvm_allow_old_toolchain = llvm.allow_old_toolchain;
627         }
628
629         if let Some(ref rust) = toml.rust {
630             debug = rust.debug;
631             debug_assertions = rust.debug_assertions;
632             debug_assertions_std = rust.debug_assertions_std;
633             debuginfo_level = rust.debuginfo_level;
634             debuginfo_level_rustc = rust.debuginfo_level_rustc;
635             debuginfo_level_std = rust.debuginfo_level_std;
636             debuginfo_level_tools = rust.debuginfo_level_tools;
637             debuginfo_level_tests = rust.debuginfo_level_tests;
638             optimize = rust.optimize;
639             ignore_git = rust.ignore_git;
640             set(&mut config.rust_new_symbol_mangling, rust.new_symbol_mangling);
641             set(&mut config.rust_optimize_tests, rust.optimize_tests);
642             set(&mut config.codegen_tests, rust.codegen_tests);
643             set(&mut config.rust_rpath, rust.rpath);
644             set(&mut config.jemalloc, rust.jemalloc);
645             set(&mut config.test_compare_mode, rust.test_compare_mode);
646             set(&mut config.llvm_libunwind, rust.llvm_libunwind);
647             set(&mut config.backtrace, rust.backtrace);
648             set(&mut config.channel, rust.channel.clone());
649             set(&mut config.rust_dist_src, rust.dist_src);
650             set(&mut config.verbose_tests, rust.verbose_tests);
651             // in the case "false" is set explicitly, do not overwrite the command line args
652             if let Some(true) = rust.incremental {
653                 config.incremental = true;
654             }
655             set(&mut config.use_lld, rust.use_lld);
656             set(&mut config.lld_enabled, rust.lld);
657             set(&mut config.llvm_tools_enabled, rust.llvm_tools);
658             config.rustc_parallel = rust.parallel_compiler.unwrap_or(false);
659             config.rustc_default_linker = rust.default_linker.clone();
660             config.musl_root = rust.musl_root.clone().map(PathBuf::from);
661             config.save_toolstates = rust.save_toolstates.clone().map(PathBuf::from);
662             set(&mut config.deny_warnings, flags.deny_warnings.or(rust.deny_warnings));
663             set(&mut config.backtrace_on_ice, rust.backtrace_on_ice);
664             set(&mut config.rust_verify_llvm_ir, rust.verify_llvm_ir);
665             config.rust_thin_lto_import_instr_limit = rust.thin_lto_import_instr_limit;
666             set(&mut config.rust_remap_debuginfo, rust.remap_debuginfo);
667             set(&mut config.control_flow_guard, rust.control_flow_guard);
668
669             if let Some(ref backends) = rust.codegen_backends {
670                 config.rust_codegen_backends =
671                     backends.iter().map(|s| INTERNER.intern_str(s)).collect();
672             }
673
674             config.rust_codegen_units = rust.codegen_units.map(threads_from_config);
675             config.rust_codegen_units_std = rust.codegen_units_std.map(threads_from_config);
676         }
677
678         if let Some(ref t) = toml.target {
679             for (triple, cfg) in t {
680                 let mut target = Target::from_triple(triple);
681
682                 if let Some(ref s) = cfg.llvm_config {
683                     target.llvm_config = Some(config.src.join(s));
684                 }
685                 if let Some(ref s) = cfg.llvm_filecheck {
686                     target.llvm_filecheck = Some(config.src.join(s));
687                 }
688                 if let Some(ref s) = cfg.android_ndk {
689                     target.ndk = Some(config.src.join(s));
690                 }
691                 if let Some(s) = cfg.no_std {
692                     target.no_std = s;
693                 }
694                 target.cc = cfg.cc.clone().map(PathBuf::from);
695                 target.cxx = cfg.cxx.clone().map(PathBuf::from);
696                 target.ar = cfg.ar.clone().map(PathBuf::from);
697                 target.ranlib = cfg.ranlib.clone().map(PathBuf::from);
698                 target.linker = cfg.linker.clone().map(PathBuf::from);
699                 target.crt_static = cfg.crt_static;
700                 target.musl_root = cfg.musl_root.clone().map(PathBuf::from);
701                 target.musl_libdir = cfg.musl_libdir.clone().map(PathBuf::from);
702                 target.wasi_root = cfg.wasi_root.clone().map(PathBuf::from);
703                 target.qemu_rootfs = cfg.qemu_rootfs.clone().map(PathBuf::from);
704
705                 config.target_config.insert(TargetSelection::from_user(triple), target);
706             }
707         }
708
709         if let Some(ref t) = toml.dist {
710             config.dist_sign_folder = t.sign_folder.clone().map(PathBuf::from);
711             config.dist_gpg_password_file = t.gpg_password_file.clone().map(PathBuf::from);
712             config.dist_upload_addr = t.upload_addr.clone();
713             set(&mut config.rust_dist_src, t.src_tarball);
714             set(&mut config.missing_tools, t.missing_tools);
715         }
716
717         // Now that we've reached the end of our configuration, infer the
718         // default values for all options that we haven't otherwise stored yet.
719
720         set(&mut config.initial_rustc, build.rustc.map(PathBuf::from));
721         set(&mut config.initial_cargo, build.cargo.map(PathBuf::from));
722
723         config.llvm_skip_rebuild = llvm_skip_rebuild.unwrap_or(false);
724
725         let default = false;
726         config.llvm_assertions = llvm_assertions.unwrap_or(default);
727
728         let default = true;
729         config.rust_optimize = optimize.unwrap_or(default);
730
731         let default = debug == Some(true);
732         config.rust_debug_assertions = debug_assertions.unwrap_or(default);
733         config.rust_debug_assertions_std =
734             debug_assertions_std.unwrap_or(config.rust_debug_assertions);
735
736         let with_defaults = |debuginfo_level_specific: Option<u32>| {
737             debuginfo_level_specific.or(debuginfo_level).unwrap_or(if debug == Some(true) {
738                 1
739             } else {
740                 0
741             })
742         };
743         config.rust_debuginfo_level_rustc = with_defaults(debuginfo_level_rustc);
744         config.rust_debuginfo_level_std = with_defaults(debuginfo_level_std);
745         config.rust_debuginfo_level_tools = with_defaults(debuginfo_level_tools);
746         config.rust_debuginfo_level_tests = debuginfo_level_tests.unwrap_or(0);
747
748         let default = config.channel == "dev";
749         config.ignore_git = ignore_git.unwrap_or(default);
750
751         config
752     }
753
754     /// Try to find the relative path of `bindir`, otherwise return it in full.
755     pub fn bindir_relative(&self) -> &Path {
756         let bindir = &self.bindir;
757         if bindir.is_absolute() {
758             // Try to make it relative to the prefix.
759             if let Some(prefix) = &self.prefix {
760                 if let Ok(stripped) = bindir.strip_prefix(prefix) {
761                     return stripped;
762                 }
763             }
764         }
765         bindir
766     }
767
768     /// Try to find the relative path of `libdir`.
769     pub fn libdir_relative(&self) -> Option<&Path> {
770         let libdir = self.libdir.as_ref()?;
771         if libdir.is_relative() {
772             Some(libdir)
773         } else {
774             // Try to make it relative to the prefix.
775             libdir.strip_prefix(self.prefix.as_ref()?).ok()
776         }
777     }
778
779     pub fn verbose(&self) -> bool {
780         self.verbose > 0
781     }
782
783     pub fn very_verbose(&self) -> bool {
784         self.verbose > 1
785     }
786
787     pub fn llvm_enabled(&self) -> bool {
788         self.rust_codegen_backends.contains(&INTERNER.intern_str("llvm"))
789     }
790 }
791
792 fn set<T>(field: &mut T, val: Option<T>) {
793     if let Some(v) = val {
794         *field = v;
795     }
796 }
797
798 fn threads_from_config(v: u32) -> u32 {
799     match v {
800         0 => num_cpus::get() as u32,
801         n => n,
802     }
803 }