1 // Copyright 2015 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
11 //! Serialized configuration of a build.
13 //! This module implements parsing `config.toml` configuration files to tweak
14 //! how the build runs.
16 use std::collections::{HashMap, HashSet};
18 use std::fs::{self, File};
19 use std::io::prelude::*;
20 use std::path::{Path, PathBuf};
26 use cache::{INTERNER, Interned};
28 pub use flags::Subcommand;
30 /// Global configuration for the entire build and/or bootstrap.
32 /// This structure is derived from a combination of both `config.toml` and
33 /// `config.mk`. As of the time of this writing it's unlikely that `config.toml`
34 /// is used all that much, so this is primarily filled out by `config.mk` which
35 /// is generated from `./configure`.
37 /// Note that this structure is not decoded directly into, but rather it is
38 /// filled out from the decoded forms of the structs below. For documentation
39 /// each field, see the corresponding fields in
40 /// `config.toml.example`.
43 pub ccache: Option<String>,
47 pub fast_submodules: bool,
48 pub compiler_docs: bool,
50 pub locked_deps: bool,
52 pub target_config: HashMap<Interned<String>, Target>,
53 pub full_bootstrap: bool,
55 pub tools: Option<HashSet<String>>,
59 pub exclude: Vec<PathBuf>,
60 pub rustc_error_format: Option<String>,
62 pub run_host_only: bool,
64 pub on_fail: Option<String>,
65 pub stage: Option<u32>,
66 pub keep_stage: Vec<u32>,
68 pub jobs: Option<u32>,
70 pub incremental: bool,
73 pub deny_warnings: bool,
74 pub backtrace_on_ice: bool,
76 // llvm codegen options
77 pub llvm_enabled: bool,
78 pub llvm_assertions: bool,
79 pub llvm_optimize: bool,
80 pub llvm_thin_lto: bool,
81 pub llvm_release_debuginfo: bool,
82 pub llvm_version_check: bool,
83 pub llvm_static_stdcpp: bool,
84 pub llvm_link_shared: bool,
85 pub llvm_clang_cl: Option<String>,
86 pub llvm_targets: Option<String>,
87 pub llvm_experimental_targets: String,
88 pub llvm_link_jobs: Option<u32>,
89 pub llvm_version_suffix: Option<String>,
91 pub lld_enabled: bool,
92 pub lldb_enabled: bool,
93 pub llvm_tools_enabled: bool,
95 // rust codegen options
96 pub rust_optimize: bool,
97 pub rust_codegen_units: Option<u32>,
98 pub rust_debug_assertions: bool,
99 pub rust_debuginfo: bool,
100 pub rust_debuginfo_lines: bool,
101 pub rust_debuginfo_only_std: bool,
102 pub rust_debuginfo_tools: bool,
103 pub rust_rpath: bool,
104 pub rustc_parallel_queries: bool,
105 pub rustc_default_linker: Option<String>,
106 pub rust_optimize_tests: bool,
107 pub rust_debuginfo_tests: bool,
108 pub rust_dist_src: bool,
109 pub rust_codegen_backends: Vec<Interned<String>>,
110 pub rust_codegen_backends_dir: String,
111 pub rust_verify_llvm_ir: bool,
112 pub rust_remap_debuginfo: bool,
114 pub build: Interned<String>,
115 pub hosts: Vec<Interned<String>>,
116 pub targets: Vec<Interned<String>>,
117 pub local_rebuild: bool,
120 pub dist_sign_folder: Option<PathBuf>,
121 pub dist_upload_addr: Option<String>,
122 pub dist_gpg_password_file: Option<PathBuf>,
125 pub debug_jemalloc: bool,
126 pub use_jemalloc: bool,
127 pub backtrace: bool, // support for RUST_BACKTRACE
128 pub wasm_syscall: bool,
131 pub low_priority: bool,
133 pub verbose_tests: bool,
135 pub save_toolstates: Option<PathBuf>,
136 pub print_step_timings: bool,
137 pub missing_tools: bool,
139 // Fallback musl-root for all targets
140 pub musl_root: Option<PathBuf>,
141 pub prefix: Option<PathBuf>,
142 pub sysconfdir: Option<PathBuf>,
143 pub datadir: Option<PathBuf>,
144 pub docdir: Option<PathBuf>,
145 pub bindir: Option<PathBuf>,
146 pub libdir: Option<PathBuf>,
147 pub mandir: Option<PathBuf>,
148 pub codegen_tests: bool,
149 pub nodejs: Option<PathBuf>,
150 pub gdb: Option<PathBuf>,
151 pub python: Option<PathBuf>,
152 pub openssl_static: bool,
153 pub configure_args: Vec<String>,
155 // These are either the stage0 downloaded binaries or the locally installed ones.
156 pub initial_cargo: PathBuf,
157 pub initial_rustc: PathBuf,
161 /// Per-target configuration stored in the global configuration structure.
164 /// Some(path to llvm-config) if using an external LLVM.
165 pub llvm_config: Option<PathBuf>,
166 /// Some(path to FileCheck) if one was specified.
167 pub llvm_filecheck: Option<PathBuf>,
168 pub jemalloc: Option<PathBuf>,
169 pub cc: Option<PathBuf>,
170 pub cxx: Option<PathBuf>,
171 pub ar: Option<PathBuf>,
172 pub ranlib: Option<PathBuf>,
173 pub linker: Option<PathBuf>,
174 pub ndk: Option<PathBuf>,
175 pub crt_static: Option<bool>,
176 pub musl_root: Option<PathBuf>,
177 pub qemu_rootfs: Option<PathBuf>,
181 /// Structure of the `config.toml` file that configuration is read from.
183 /// This structure uses `Decodable` to automatically decode a TOML configuration
184 /// file into this format, and then this is traversed and written into the above
185 /// `Config` structure.
186 #[derive(Deserialize, Default)]
187 #[serde(deny_unknown_fields, rename_all = "kebab-case")]
189 build: Option<Build>,
190 install: Option<Install>,
193 target: Option<HashMap<String, TomlTarget>>,
197 /// TOML representation of various global build decisions.
198 #[derive(Deserialize, Default, Clone)]
199 #[serde(deny_unknown_fields, rename_all = "kebab-case")]
201 build: Option<String>,
206 cargo: Option<String>,
207 rustc: Option<String>,
208 low_priority: Option<bool>,
209 compiler_docs: Option<bool>,
211 submodules: Option<bool>,
212 fast_submodules: Option<bool>,
214 locked_deps: Option<bool>,
215 vendor: Option<bool>,
216 nodejs: Option<String>,
217 python: Option<String>,
218 full_bootstrap: Option<bool>,
219 extended: Option<bool>,
220 tools: Option<HashSet<String>>,
221 verbose: Option<usize>,
222 sanitizers: Option<bool>,
223 profiler: Option<bool>,
224 openssl_static: Option<bool>,
225 configure_args: Option<Vec<String>>,
226 local_rebuild: Option<bool>,
227 print_step_timings: Option<bool>,
230 /// TOML representation of various global install decisions.
231 #[derive(Deserialize, Default, Clone)]
232 #[serde(deny_unknown_fields, rename_all = "kebab-case")]
234 prefix: Option<String>,
235 sysconfdir: Option<String>,
236 datadir: Option<String>,
237 docdir: Option<String>,
238 bindir: Option<String>,
239 libdir: Option<String>,
240 mandir: Option<String>,
242 // standard paths, currently unused
243 infodir: Option<String>,
244 localstatedir: Option<String>,
247 /// TOML representation of how the LLVM build is configured.
248 #[derive(Deserialize, Default)]
249 #[serde(deny_unknown_fields, rename_all = "kebab-case")]
251 enabled: Option<bool>,
252 ccache: Option<StringOrBool>,
254 assertions: Option<bool>,
255 optimize: Option<bool>,
256 thin_lto: Option<bool>,
257 release_debuginfo: Option<bool>,
258 version_check: Option<bool>,
259 static_libstdcpp: Option<bool>,
260 targets: Option<String>,
261 experimental_targets: Option<String>,
262 link_jobs: Option<u32>,
263 link_shared: Option<bool>,
264 version_suffix: Option<String>,
265 clang_cl: Option<String>
268 #[derive(Deserialize, Default, Clone)]
269 #[serde(deny_unknown_fields, rename_all = "kebab-case")]
271 sign_folder: Option<String>,
272 gpg_password_file: Option<String>,
273 upload_addr: Option<String>,
274 src_tarball: Option<bool>,
275 missing_tools: Option<bool>,
278 #[derive(Deserialize)]
285 impl Default for StringOrBool {
286 fn default() -> StringOrBool {
287 StringOrBool::Bool(false)
291 /// TOML representation of how the Rust build is configured.
292 #[derive(Deserialize, Default)]
293 #[serde(deny_unknown_fields, rename_all = "kebab-case")]
295 optimize: Option<bool>,
296 codegen_units: Option<u32>,
297 debug_assertions: Option<bool>,
298 debuginfo: Option<bool>,
299 debuginfo_lines: Option<bool>,
300 debuginfo_only_std: Option<bool>,
301 debuginfo_tools: Option<bool>,
302 experimental_parallel_queries: Option<bool>,
303 debug_jemalloc: Option<bool>,
304 use_jemalloc: Option<bool>,
305 backtrace: Option<bool>,
306 default_linker: Option<String>,
307 channel: Option<String>,
308 musl_root: Option<String>,
310 optimize_tests: Option<bool>,
311 debuginfo_tests: Option<bool>,
312 codegen_tests: Option<bool>,
313 ignore_git: Option<bool>,
315 dist_src: Option<bool>,
316 verbose_tests: Option<bool>,
317 test_miri: Option<bool>,
318 incremental: Option<bool>,
319 save_toolstates: Option<String>,
320 codegen_backends: Option<Vec<String>>,
321 codegen_backends_dir: Option<String>,
322 wasm_syscall: Option<bool>,
325 llvm_tools: Option<bool>,
326 deny_warnings: Option<bool>,
327 backtrace_on_ice: Option<bool>,
328 verify_llvm_ir: Option<bool>,
329 remap_debuginfo: Option<bool>,
332 /// TOML representation of how each build target is configured.
333 #[derive(Deserialize, Default)]
334 #[serde(deny_unknown_fields, rename_all = "kebab-case")]
336 llvm_config: Option<String>,
337 llvm_filecheck: Option<String>,
338 jemalloc: Option<String>,
342 ranlib: Option<String>,
343 linker: Option<String>,
344 android_ndk: Option<String>,
345 crt_static: Option<bool>,
346 musl_root: Option<String>,
347 qemu_rootfs: Option<String>,
351 fn path_from_python(var_key: &str) -> PathBuf {
352 match env::var_os(var_key) {
353 // Do not trust paths from Python and normalize them slightly (#49785).
354 Some(var_val) => Path::new(&var_val).components().collect(),
355 _ => panic!("expected '{}' to be set", var_key),
359 pub fn default_opts() -> Config {
360 let mut config = Config::default();
361 config.llvm_enabled = true;
362 config.llvm_optimize = true;
363 config.llvm_version_check = true;
364 config.use_jemalloc = true;
365 config.backtrace = true;
366 config.rust_optimize = true;
367 config.rust_optimize_tests = true;
368 config.submodules = true;
369 config.fast_submodules = true;
371 config.rust_rpath = true;
372 config.channel = "dev".to_string();
373 config.codegen_tests = true;
374 config.ignore_git = false;
375 config.rust_dist_src = true;
376 config.test_miri = false;
377 config.rust_codegen_backends = vec![INTERNER.intern_str("llvm")];
378 config.rust_codegen_backends_dir = "codegen-backends".to_owned();
379 config.deny_warnings = true;
380 config.missing_tools = false;
382 // set by bootstrap.py
383 config.build = INTERNER.intern_str(&env::var("BUILD").expect("'BUILD' to be set"));
384 config.src = Config::path_from_python("SRC");
385 config.out = Config::path_from_python("BUILD_DIR");
387 config.initial_rustc = Config::path_from_python("RUSTC");
388 config.initial_cargo = Config::path_from_python("CARGO");
393 pub fn parse(args: &[String]) -> Config {
394 let flags = Flags::parse(&args);
395 let file = flags.config.clone();
396 let mut config = Config::default_opts();
397 config.exclude = flags.exclude;
398 config.rustc_error_format = flags.rustc_error_format;
399 config.on_fail = flags.on_fail;
400 config.stage = flags.stage;
401 config.jobs = flags.jobs;
402 config.cmd = flags.cmd;
403 config.incremental = flags.incremental;
404 config.dry_run = flags.dry_run;
405 config.keep_stage = flags.keep_stage;
406 if let Some(value) = flags.warnings {
407 config.deny_warnings = value;
411 let dir = config.out.join("tmp-dry-run");
412 t!(fs::create_dir_all(&dir));
416 // If --target was specified but --host wasn't specified, don't run any host-only tests.
417 config.run_host_only = !(flags.host.is_empty() && !flags.target.is_empty());
419 let toml = file.map(|file| {
420 let mut f = t!(File::open(&file));
421 let mut contents = String::new();
422 t!(f.read_to_string(&mut contents));
423 match toml::from_str(&contents) {
426 println!("failed to parse TOML configuration '{}': {}",
427 file.display(), err);
431 }).unwrap_or_else(|| TomlConfig::default());
433 let build = toml.build.clone().unwrap_or(Build::default());
434 // set by bootstrap.py
435 config.hosts.push(config.build.clone());
436 for host in build.host.iter() {
437 let host = INTERNER.intern_str(host);
438 if !config.hosts.contains(&host) {
439 config.hosts.push(host);
442 for target in config.hosts.iter().cloned()
443 .chain(build.target.iter().map(|s| INTERNER.intern_str(s)))
445 if !config.targets.contains(&target) {
446 config.targets.push(target);
449 config.hosts = if !flags.host.is_empty() {
454 config.targets = if !flags.target.is_empty() {
461 config.nodejs = build.nodejs.map(PathBuf::from);
462 config.gdb = build.gdb.map(PathBuf::from);
463 config.python = build.python.map(PathBuf::from);
464 set(&mut config.low_priority, build.low_priority);
465 set(&mut config.compiler_docs, build.compiler_docs);
466 set(&mut config.docs, build.docs);
467 set(&mut config.submodules, build.submodules);
468 set(&mut config.fast_submodules, build.fast_submodules);
469 set(&mut config.locked_deps, build.locked_deps);
470 set(&mut config.vendor, build.vendor);
471 set(&mut config.full_bootstrap, build.full_bootstrap);
472 set(&mut config.extended, build.extended);
473 config.tools = build.tools;
474 set(&mut config.verbose, build.verbose);
475 set(&mut config.sanitizers, build.sanitizers);
476 set(&mut config.profiler, build.profiler);
477 set(&mut config.openssl_static, build.openssl_static);
478 set(&mut config.configure_args, build.configure_args);
479 set(&mut config.local_rebuild, build.local_rebuild);
480 set(&mut config.print_step_timings, build.print_step_timings);
481 config.verbose = cmp::max(config.verbose, flags.verbose);
483 if let Some(ref install) = toml.install {
484 config.prefix = install.prefix.clone().map(PathBuf::from);
485 config.sysconfdir = install.sysconfdir.clone().map(PathBuf::from);
486 config.datadir = install.datadir.clone().map(PathBuf::from);
487 config.docdir = install.docdir.clone().map(PathBuf::from);
488 config.bindir = install.bindir.clone().map(PathBuf::from);
489 config.libdir = install.libdir.clone().map(PathBuf::from);
490 config.mandir = install.mandir.clone().map(PathBuf::from);
493 // Store off these values as options because if they're not provided
494 // we'll infer default values for them later
495 let mut llvm_assertions = None;
496 let mut debuginfo_lines = None;
497 let mut debuginfo_only_std = None;
498 let mut debuginfo_tools = None;
499 let mut debug = None;
500 let mut debug_jemalloc = None;
501 let mut debuginfo = None;
502 let mut debug_assertions = None;
503 let mut optimize = None;
504 let mut ignore_git = None;
506 if let Some(ref llvm) = toml.llvm {
508 Some(StringOrBool::String(ref s)) => {
509 config.ccache = Some(s.to_string())
511 Some(StringOrBool::Bool(true)) => {
512 config.ccache = Some("ccache".to_string());
514 Some(StringOrBool::Bool(false)) | None => {}
516 set(&mut config.ninja, llvm.ninja);
517 set(&mut config.llvm_enabled, llvm.enabled);
518 llvm_assertions = llvm.assertions;
519 set(&mut config.llvm_optimize, llvm.optimize);
520 set(&mut config.llvm_thin_lto, llvm.thin_lto);
521 set(&mut config.llvm_release_debuginfo, llvm.release_debuginfo);
522 set(&mut config.llvm_version_check, llvm.version_check);
523 set(&mut config.llvm_static_stdcpp, llvm.static_libstdcpp);
524 set(&mut config.llvm_link_shared, llvm.link_shared);
525 config.llvm_targets = llvm.targets.clone();
526 config.llvm_experimental_targets = llvm.experimental_targets.clone()
527 .unwrap_or("WebAssembly;RISCV".to_string());
528 config.llvm_link_jobs = llvm.link_jobs;
529 config.llvm_version_suffix = llvm.version_suffix.clone();
530 config.llvm_clang_cl = llvm.clang_cl.clone();
533 if let Some(ref rust) = toml.rust {
535 debug_assertions = rust.debug_assertions;
536 debuginfo = rust.debuginfo;
537 debuginfo_lines = rust.debuginfo_lines;
538 debuginfo_only_std = rust.debuginfo_only_std;
539 debuginfo_tools = rust.debuginfo_tools;
540 optimize = rust.optimize;
541 ignore_git = rust.ignore_git;
542 debug_jemalloc = rust.debug_jemalloc;
543 set(&mut config.rust_optimize_tests, rust.optimize_tests);
544 set(&mut config.rust_debuginfo_tests, rust.debuginfo_tests);
545 set(&mut config.codegen_tests, rust.codegen_tests);
546 set(&mut config.rust_rpath, rust.rpath);
547 set(&mut config.use_jemalloc, rust.use_jemalloc);
548 set(&mut config.backtrace, rust.backtrace);
549 set(&mut config.channel, rust.channel.clone());
550 set(&mut config.rust_dist_src, rust.dist_src);
551 set(&mut config.verbose_tests, rust.verbose_tests);
552 set(&mut config.test_miri, rust.test_miri);
553 // in the case "false" is set explicitly, do not overwrite the command line args
554 if let Some(true) = rust.incremental {
555 config.incremental = true;
557 set(&mut config.wasm_syscall, rust.wasm_syscall);
558 set(&mut config.lld_enabled, rust.lld);
559 set(&mut config.lldb_enabled, rust.lldb);
560 set(&mut config.llvm_tools_enabled, rust.llvm_tools);
561 config.rustc_parallel_queries = rust.experimental_parallel_queries.unwrap_or(false);
562 config.rustc_default_linker = rust.default_linker.clone();
563 config.musl_root = rust.musl_root.clone().map(PathBuf::from);
564 config.save_toolstates = rust.save_toolstates.clone().map(PathBuf::from);
565 set(&mut config.deny_warnings, rust.deny_warnings.or(flags.warnings));
566 set(&mut config.backtrace_on_ice, rust.backtrace_on_ice);
567 set(&mut config.rust_verify_llvm_ir, rust.verify_llvm_ir);
568 set(&mut config.rust_remap_debuginfo, rust.remap_debuginfo);
570 if let Some(ref backends) = rust.codegen_backends {
571 config.rust_codegen_backends = backends.iter()
572 .map(|s| INTERNER.intern_str(s))
576 set(&mut config.rust_codegen_backends_dir, rust.codegen_backends_dir.clone());
578 match rust.codegen_units {
579 Some(0) => config.rust_codegen_units = Some(num_cpus::get() as u32),
580 Some(n) => config.rust_codegen_units = Some(n),
585 if let Some(ref t) = toml.target {
586 for (triple, cfg) in t {
587 let mut target = Target::default();
589 if let Some(ref s) = cfg.llvm_config {
590 target.llvm_config = Some(config.src.join(s));
592 if let Some(ref s) = cfg.llvm_filecheck {
593 target.llvm_filecheck = Some(config.src.join(s));
595 if let Some(ref s) = cfg.jemalloc {
596 target.jemalloc = Some(config.src.join(s));
598 if let Some(ref s) = cfg.android_ndk {
599 target.ndk = Some(config.src.join(s));
601 target.cc = cfg.cc.clone().map(PathBuf::from);
602 target.cxx = cfg.cxx.clone().map(PathBuf::from);
603 target.ar = cfg.ar.clone().map(PathBuf::from);
604 target.ranlib = cfg.ranlib.clone().map(PathBuf::from);
605 target.linker = cfg.linker.clone().map(PathBuf::from);
606 target.crt_static = cfg.crt_static.clone();
607 target.musl_root = cfg.musl_root.clone().map(PathBuf::from);
608 target.qemu_rootfs = cfg.qemu_rootfs.clone().map(PathBuf::from);
610 config.target_config.insert(INTERNER.intern_string(triple.clone()), target);
614 if let Some(ref t) = toml.dist {
615 config.dist_sign_folder = t.sign_folder.clone().map(PathBuf::from);
616 config.dist_gpg_password_file = t.gpg_password_file.clone().map(PathBuf::from);
617 config.dist_upload_addr = t.upload_addr.clone();
618 set(&mut config.rust_dist_src, t.src_tarball);
619 set(&mut config.missing_tools, t.missing_tools);
622 // Now that we've reached the end of our configuration, infer the
623 // default values for all options that we haven't otherwise stored yet.
625 set(&mut config.initial_rustc, build.rustc.map(PathBuf::from));
626 set(&mut config.initial_cargo, build.cargo.map(PathBuf::from));
629 config.llvm_assertions = llvm_assertions.unwrap_or(default);
631 let default = match &config.channel[..] {
632 "stable" | "beta" | "nightly" => true,
635 config.rust_debuginfo_lines = debuginfo_lines.unwrap_or(default);
636 config.rust_debuginfo_only_std = debuginfo_only_std.unwrap_or(default);
637 config.rust_debuginfo_tools = debuginfo_tools.unwrap_or(false);
639 let default = debug == Some(true);
640 config.debug_jemalloc = debug_jemalloc.unwrap_or(default);
641 config.rust_debuginfo = debuginfo.unwrap_or(default);
642 config.rust_debug_assertions = debug_assertions.unwrap_or(default);
643 config.rust_optimize = optimize.unwrap_or(!default);
645 let default = config.channel == "dev";
646 config.ignore_git = ignore_git.unwrap_or(default);
651 /// Try to find the relative path of `libdir`.
652 pub fn libdir_relative(&self) -> Option<&Path> {
653 let libdir = self.libdir.as_ref()?;
654 if libdir.is_relative() {
657 // Try to make it relative to the prefix.
658 libdir.strip_prefix(self.prefix.as_ref()?).ok()
662 pub fn verbose(&self) -> bool {
666 pub fn very_verbose(&self) -> bool {
671 fn set<T>(field: &mut T, val: Option<T>) {
672 if let Some(v) = val {