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