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>,
90 pub lld_enabled: bool,
91 pub lldb_enabled: bool,
92 pub llvm_tools_enabled: bool,
94 // rust codegen options
95 pub rust_optimize: bool,
96 pub rust_codegen_units: Option<u32>,
97 pub rust_debug_assertions: bool,
98 pub rust_debuginfo: bool,
99 pub rust_debuginfo_lines: bool,
100 pub rust_debuginfo_only_std: bool,
101 pub rust_debuginfo_tools: bool,
102 pub rust_rpath: bool,
103 pub rustc_parallel_queries: bool,
104 pub rustc_default_linker: Option<String>,
105 pub rust_optimize_tests: bool,
106 pub rust_debuginfo_tests: bool,
107 pub rust_dist_src: bool,
108 pub rust_codegen_backends: Vec<Interned<String>>,
109 pub rust_codegen_backends_dir: String,
110 pub rust_verify_llvm_ir: bool,
112 pub build: Interned<String>,
113 pub hosts: Vec<Interned<String>>,
114 pub targets: Vec<Interned<String>>,
115 pub local_rebuild: bool,
118 pub dist_sign_folder: Option<PathBuf>,
119 pub dist_upload_addr: Option<String>,
120 pub dist_gpg_password_file: Option<PathBuf>,
123 pub debug_jemalloc: bool,
124 pub use_jemalloc: bool,
125 pub backtrace: bool, // support for RUST_BACKTRACE
126 pub wasm_syscall: bool,
129 pub low_priority: bool,
131 pub verbose_tests: bool,
133 pub save_toolstates: Option<PathBuf>,
134 pub print_step_timings: bool,
136 // Fallback musl-root for all targets
137 pub musl_root: Option<PathBuf>,
138 pub prefix: Option<PathBuf>,
139 pub sysconfdir: Option<PathBuf>,
140 pub datadir: Option<PathBuf>,
141 pub docdir: Option<PathBuf>,
142 pub bindir: Option<PathBuf>,
143 pub libdir: Option<PathBuf>,
144 pub mandir: Option<PathBuf>,
145 pub codegen_tests: bool,
146 pub nodejs: Option<PathBuf>,
147 pub gdb: Option<PathBuf>,
148 pub python: Option<PathBuf>,
149 pub openssl_static: bool,
150 pub configure_args: Vec<String>,
152 // These are either the stage0 downloaded binaries or the locally installed ones.
153 pub initial_cargo: PathBuf,
154 pub initial_rustc: PathBuf,
158 /// Per-target configuration stored in the global configuration structure.
161 /// Some(path to llvm-config) if using an external LLVM.
162 pub llvm_config: Option<PathBuf>,
163 pub jemalloc: Option<PathBuf>,
164 pub cc: Option<PathBuf>,
165 pub cxx: Option<PathBuf>,
166 pub ar: Option<PathBuf>,
167 pub ranlib: Option<PathBuf>,
168 pub linker: Option<PathBuf>,
169 pub ndk: Option<PathBuf>,
170 pub crt_static: Option<bool>,
171 pub musl_root: Option<PathBuf>,
172 pub qemu_rootfs: Option<PathBuf>,
176 /// Structure of the `config.toml` file that configuration is read from.
178 /// This structure uses `Decodable` to automatically decode a TOML configuration
179 /// file into this format, and then this is traversed and written into the above
180 /// `Config` structure.
181 #[derive(Deserialize, Default)]
182 #[serde(deny_unknown_fields, rename_all = "kebab-case")]
184 build: Option<Build>,
185 install: Option<Install>,
188 target: Option<HashMap<String, TomlTarget>>,
192 /// TOML representation of various global build decisions.
193 #[derive(Deserialize, Default, Clone)]
194 #[serde(deny_unknown_fields, rename_all = "kebab-case")]
196 build: Option<String>,
201 cargo: Option<String>,
202 rustc: Option<String>,
203 low_priority: Option<bool>,
204 compiler_docs: Option<bool>,
206 submodules: Option<bool>,
207 fast_submodules: Option<bool>,
209 locked_deps: Option<bool>,
210 vendor: Option<bool>,
211 nodejs: Option<String>,
212 python: Option<String>,
213 full_bootstrap: Option<bool>,
214 extended: Option<bool>,
215 tools: Option<HashSet<String>>,
216 verbose: Option<usize>,
217 sanitizers: Option<bool>,
218 profiler: Option<bool>,
219 openssl_static: Option<bool>,
220 configure_args: Option<Vec<String>>,
221 local_rebuild: Option<bool>,
222 print_step_timings: Option<bool>,
225 /// TOML representation of various global install decisions.
226 #[derive(Deserialize, Default, Clone)]
227 #[serde(deny_unknown_fields, rename_all = "kebab-case")]
229 prefix: Option<String>,
230 sysconfdir: Option<String>,
231 datadir: Option<String>,
232 docdir: Option<String>,
233 bindir: Option<String>,
234 libdir: Option<String>,
235 mandir: Option<String>,
237 // standard paths, currently unused
238 infodir: Option<String>,
239 localstatedir: Option<String>,
242 /// TOML representation of how the LLVM build is configured.
243 #[derive(Deserialize, Default)]
244 #[serde(deny_unknown_fields, rename_all = "kebab-case")]
246 enabled: Option<bool>,
247 ccache: Option<StringOrBool>,
249 assertions: Option<bool>,
250 optimize: Option<bool>,
251 thin_lto: Option<bool>,
252 release_debuginfo: Option<bool>,
253 version_check: Option<bool>,
254 static_libstdcpp: Option<bool>,
255 targets: Option<String>,
256 experimental_targets: Option<String>,
257 link_jobs: Option<u32>,
258 link_shared: Option<bool>,
259 clang_cl: Option<String>
262 #[derive(Deserialize, Default, Clone)]
263 #[serde(deny_unknown_fields, rename_all = "kebab-case")]
265 sign_folder: Option<String>,
266 gpg_password_file: Option<String>,
267 upload_addr: Option<String>,
268 src_tarball: Option<bool>,
271 #[derive(Deserialize)]
278 impl Default for StringOrBool {
279 fn default() -> StringOrBool {
280 StringOrBool::Bool(false)
284 /// TOML representation of how the Rust build is configured.
285 #[derive(Deserialize, Default)]
286 #[serde(deny_unknown_fields, rename_all = "kebab-case")]
288 optimize: Option<bool>,
289 codegen_units: Option<u32>,
290 debug_assertions: Option<bool>,
291 debuginfo: Option<bool>,
292 debuginfo_lines: Option<bool>,
293 debuginfo_only_std: Option<bool>,
294 debuginfo_tools: Option<bool>,
295 experimental_parallel_queries: Option<bool>,
296 debug_jemalloc: Option<bool>,
297 use_jemalloc: Option<bool>,
298 backtrace: Option<bool>,
299 default_linker: Option<String>,
300 channel: Option<String>,
301 musl_root: Option<String>,
303 optimize_tests: Option<bool>,
304 debuginfo_tests: Option<bool>,
305 codegen_tests: Option<bool>,
306 ignore_git: Option<bool>,
308 dist_src: Option<bool>,
309 verbose_tests: Option<bool>,
310 test_miri: Option<bool>,
311 incremental: Option<bool>,
312 save_toolstates: Option<String>,
313 codegen_backends: Option<Vec<String>>,
314 codegen_backends_dir: Option<String>,
315 wasm_syscall: Option<bool>,
318 llvm_tools: Option<bool>,
319 deny_warnings: Option<bool>,
320 backtrace_on_ice: Option<bool>,
321 verify_llvm_ir: Option<bool>,
324 /// TOML representation of how each build target is configured.
325 #[derive(Deserialize, Default)]
326 #[serde(deny_unknown_fields, rename_all = "kebab-case")]
328 llvm_config: Option<String>,
329 jemalloc: Option<String>,
333 ranlib: Option<String>,
334 linker: Option<String>,
335 android_ndk: Option<String>,
336 crt_static: Option<bool>,
337 musl_root: Option<String>,
338 qemu_rootfs: Option<String>,
342 fn path_from_python(var_key: &str) -> PathBuf {
343 match env::var_os(var_key) {
344 // Do not trust paths from Python and normalize them slightly (#49785).
345 Some(var_val) => Path::new(&var_val).components().collect(),
346 _ => panic!("expected '{}' to be set", var_key),
350 pub fn default_opts() -> Config {
351 let mut config = Config::default();
352 config.llvm_enabled = true;
353 config.llvm_optimize = true;
354 config.llvm_version_check = true;
355 config.use_jemalloc = true;
356 config.backtrace = true;
357 config.rust_optimize = true;
358 config.rust_optimize_tests = true;
359 config.submodules = true;
360 config.fast_submodules = true;
362 config.rust_rpath = true;
363 config.channel = "dev".to_string();
364 config.codegen_tests = true;
365 config.ignore_git = false;
366 config.rust_dist_src = true;
367 config.test_miri = false;
368 config.rust_codegen_backends = vec![INTERNER.intern_str("llvm")];
369 config.rust_codegen_backends_dir = "codegen-backends".to_owned();
370 config.deny_warnings = true;
372 // set by bootstrap.py
373 config.build = INTERNER.intern_str(&env::var("BUILD").expect("'BUILD' to be set"));
374 config.src = Config::path_from_python("SRC");
375 config.out = Config::path_from_python("BUILD_DIR");
377 config.initial_rustc = Config::path_from_python("RUSTC");
378 config.initial_cargo = Config::path_from_python("CARGO");
383 pub fn parse(args: &[String]) -> Config {
384 let flags = Flags::parse(&args);
385 let file = flags.config.clone();
386 let mut config = Config::default_opts();
387 config.exclude = flags.exclude;
388 config.rustc_error_format = flags.rustc_error_format;
389 config.on_fail = flags.on_fail;
390 config.stage = flags.stage;
391 config.jobs = flags.jobs;
392 config.cmd = flags.cmd;
393 config.incremental = flags.incremental;
394 config.dry_run = flags.dry_run;
395 config.keep_stage = flags.keep_stage;
396 if let Some(value) = flags.warnings {
397 config.deny_warnings = value;
401 let dir = config.out.join("tmp-dry-run");
402 t!(fs::create_dir_all(&dir));
406 // If --target was specified but --host wasn't specified, don't run any host-only tests.
407 config.run_host_only = !(flags.host.is_empty() && !flags.target.is_empty());
409 let toml = file.map(|file| {
410 let mut f = t!(File::open(&file));
411 let mut contents = String::new();
412 t!(f.read_to_string(&mut contents));
413 match toml::from_str(&contents) {
416 println!("failed to parse TOML configuration '{}': {}",
417 file.display(), err);
421 }).unwrap_or_else(|| TomlConfig::default());
423 let build = toml.build.clone().unwrap_or(Build::default());
424 // set by bootstrap.py
425 config.hosts.push(config.build.clone());
426 for host in build.host.iter() {
427 let host = INTERNER.intern_str(host);
428 if !config.hosts.contains(&host) {
429 config.hosts.push(host);
432 for target in config.hosts.iter().cloned()
433 .chain(build.target.iter().map(|s| INTERNER.intern_str(s)))
435 if !config.targets.contains(&target) {
436 config.targets.push(target);
439 config.hosts = if !flags.host.is_empty() {
444 config.targets = if !flags.target.is_empty() {
451 config.nodejs = build.nodejs.map(PathBuf::from);
452 config.gdb = build.gdb.map(PathBuf::from);
453 config.python = build.python.map(PathBuf::from);
454 set(&mut config.low_priority, build.low_priority);
455 set(&mut config.compiler_docs, build.compiler_docs);
456 set(&mut config.docs, build.docs);
457 set(&mut config.submodules, build.submodules);
458 set(&mut config.fast_submodules, build.fast_submodules);
459 set(&mut config.locked_deps, build.locked_deps);
460 set(&mut config.vendor, build.vendor);
461 set(&mut config.full_bootstrap, build.full_bootstrap);
462 set(&mut config.extended, build.extended);
463 config.tools = build.tools;
464 set(&mut config.verbose, build.verbose);
465 set(&mut config.sanitizers, build.sanitizers);
466 set(&mut config.profiler, build.profiler);
467 set(&mut config.openssl_static, build.openssl_static);
468 set(&mut config.configure_args, build.configure_args);
469 set(&mut config.local_rebuild, build.local_rebuild);
470 set(&mut config.print_step_timings, build.print_step_timings);
471 config.verbose = cmp::max(config.verbose, flags.verbose);
473 if let Some(ref install) = toml.install {
474 config.prefix = install.prefix.clone().map(PathBuf::from);
475 config.sysconfdir = install.sysconfdir.clone().map(PathBuf::from);
476 config.datadir = install.datadir.clone().map(PathBuf::from);
477 config.docdir = install.docdir.clone().map(PathBuf::from);
478 config.bindir = install.bindir.clone().map(PathBuf::from);
479 config.libdir = install.libdir.clone().map(PathBuf::from);
480 config.mandir = install.mandir.clone().map(PathBuf::from);
483 // Store off these values as options because if they're not provided
484 // we'll infer default values for them later
485 let mut llvm_assertions = None;
486 let mut debuginfo_lines = None;
487 let mut debuginfo_only_std = None;
488 let mut debuginfo_tools = None;
489 let mut debug = None;
490 let mut debug_jemalloc = None;
491 let mut debuginfo = None;
492 let mut debug_assertions = None;
493 let mut optimize = None;
494 let mut ignore_git = None;
496 if let Some(ref llvm) = toml.llvm {
498 Some(StringOrBool::String(ref s)) => {
499 config.ccache = Some(s.to_string())
501 Some(StringOrBool::Bool(true)) => {
502 config.ccache = Some("ccache".to_string());
504 Some(StringOrBool::Bool(false)) | None => {}
506 set(&mut config.ninja, llvm.ninja);
507 set(&mut config.llvm_enabled, llvm.enabled);
508 llvm_assertions = llvm.assertions;
509 set(&mut config.llvm_optimize, llvm.optimize);
510 set(&mut config.llvm_thin_lto, llvm.thin_lto);
511 set(&mut config.llvm_release_debuginfo, llvm.release_debuginfo);
512 set(&mut config.llvm_version_check, llvm.version_check);
513 set(&mut config.llvm_static_stdcpp, llvm.static_libstdcpp);
514 set(&mut config.llvm_link_shared, llvm.link_shared);
515 config.llvm_targets = llvm.targets.clone();
516 config.llvm_experimental_targets = llvm.experimental_targets.clone()
517 .unwrap_or("WebAssembly;RISCV".to_string());
518 config.llvm_link_jobs = llvm.link_jobs;
519 config.llvm_clang_cl = llvm.clang_cl.clone();
522 if let Some(ref rust) = toml.rust {
524 debug_assertions = rust.debug_assertions;
525 debuginfo = rust.debuginfo;
526 debuginfo_lines = rust.debuginfo_lines;
527 debuginfo_only_std = rust.debuginfo_only_std;
528 debuginfo_tools = rust.debuginfo_tools;
529 optimize = rust.optimize;
530 ignore_git = rust.ignore_git;
531 debug_jemalloc = rust.debug_jemalloc;
532 set(&mut config.rust_optimize_tests, rust.optimize_tests);
533 set(&mut config.rust_debuginfo_tests, rust.debuginfo_tests);
534 set(&mut config.codegen_tests, rust.codegen_tests);
535 set(&mut config.rust_rpath, rust.rpath);
536 set(&mut config.use_jemalloc, rust.use_jemalloc);
537 set(&mut config.backtrace, rust.backtrace);
538 set(&mut config.channel, rust.channel.clone());
539 set(&mut config.rust_dist_src, rust.dist_src);
540 set(&mut config.verbose_tests, rust.verbose_tests);
541 set(&mut config.test_miri, rust.test_miri);
542 // in the case "false" is set explicitly, do not overwrite the command line args
543 if let Some(true) = rust.incremental {
544 config.incremental = true;
546 set(&mut config.wasm_syscall, rust.wasm_syscall);
547 set(&mut config.lld_enabled, rust.lld);
548 set(&mut config.lldb_enabled, rust.lldb);
549 set(&mut config.llvm_tools_enabled, rust.llvm_tools);
550 config.rustc_parallel_queries = rust.experimental_parallel_queries.unwrap_or(false);
551 config.rustc_default_linker = rust.default_linker.clone();
552 config.musl_root = rust.musl_root.clone().map(PathBuf::from);
553 config.save_toolstates = rust.save_toolstates.clone().map(PathBuf::from);
554 set(&mut config.deny_warnings, rust.deny_warnings.or(flags.warnings));
555 set(&mut config.backtrace_on_ice, rust.backtrace_on_ice);
556 set(&mut config.rust_verify_llvm_ir, rust.verify_llvm_ir);
558 if let Some(ref backends) = rust.codegen_backends {
559 config.rust_codegen_backends = backends.iter()
560 .map(|s| INTERNER.intern_str(s))
564 set(&mut config.rust_codegen_backends_dir, rust.codegen_backends_dir.clone());
566 match rust.codegen_units {
567 Some(0) => config.rust_codegen_units = Some(num_cpus::get() as u32),
568 Some(n) => config.rust_codegen_units = Some(n),
573 if let Some(ref t) = toml.target {
574 for (triple, cfg) in t {
575 let mut target = Target::default();
577 if let Some(ref s) = cfg.llvm_config {
578 target.llvm_config = Some(config.src.join(s));
580 if let Some(ref s) = cfg.jemalloc {
581 target.jemalloc = Some(config.src.join(s));
583 if let Some(ref s) = cfg.android_ndk {
584 target.ndk = Some(config.src.join(s));
586 target.cc = cfg.cc.clone().map(PathBuf::from);
587 target.cxx = cfg.cxx.clone().map(PathBuf::from);
588 target.ar = cfg.ar.clone().map(PathBuf::from);
589 target.ranlib = cfg.ranlib.clone().map(PathBuf::from);
590 target.linker = cfg.linker.clone().map(PathBuf::from);
591 target.crt_static = cfg.crt_static.clone();
592 target.musl_root = cfg.musl_root.clone().map(PathBuf::from);
593 target.qemu_rootfs = cfg.qemu_rootfs.clone().map(PathBuf::from);
595 config.target_config.insert(INTERNER.intern_string(triple.clone()), target);
599 if let Some(ref t) = toml.dist {
600 config.dist_sign_folder = t.sign_folder.clone().map(PathBuf::from);
601 config.dist_gpg_password_file = t.gpg_password_file.clone().map(PathBuf::from);
602 config.dist_upload_addr = t.upload_addr.clone();
603 set(&mut config.rust_dist_src, t.src_tarball);
606 // Now that we've reached the end of our configuration, infer the
607 // default values for all options that we haven't otherwise stored yet.
609 set(&mut config.initial_rustc, build.rustc.map(PathBuf::from));
610 set(&mut config.initial_cargo, build.cargo.map(PathBuf::from));
613 config.llvm_assertions = llvm_assertions.unwrap_or(default);
615 let default = match &config.channel[..] {
616 "stable" | "beta" | "nightly" => true,
619 config.rust_debuginfo_lines = debuginfo_lines.unwrap_or(default);
620 config.rust_debuginfo_only_std = debuginfo_only_std.unwrap_or(default);
621 config.rust_debuginfo_tools = debuginfo_tools.unwrap_or(false);
623 let default = debug == Some(true);
624 config.debug_jemalloc = debug_jemalloc.unwrap_or(default);
625 config.rust_debuginfo = debuginfo.unwrap_or(default);
626 config.rust_debug_assertions = debug_assertions.unwrap_or(default);
627 config.rust_optimize = optimize.unwrap_or(!default);
629 let default = config.channel == "dev";
630 config.ignore_git = ignore_git.unwrap_or(default);
635 /// Try to find the relative path of `libdir`.
636 pub fn libdir_relative(&self) -> Option<&Path> {
637 let libdir = self.libdir.as_ref()?;
638 if libdir.is_relative() {
641 // Try to make it relative to the prefix.
642 libdir.strip_prefix(self.prefix.as_ref()?).ok()
646 pub fn verbose(&self) -> bool {
650 pub fn very_verbose(&self) -> bool {
655 fn set<T>(field: &mut T, val: Option<T>) {
656 if let Some(v) = val {