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_release_debuginfo: bool,
81 pub llvm_version_check: bool,
82 pub llvm_static_stdcpp: bool,
83 pub llvm_link_shared: bool,
84 pub llvm_clang_cl: Option<String>,
85 pub llvm_targets: Option<String>,
86 pub llvm_experimental_targets: String,
87 pub llvm_link_jobs: Option<u32>,
89 pub lld_enabled: bool,
90 pub llvm_tools_enabled: bool,
92 // rust codegen options
93 pub rust_optimize: bool,
94 pub rust_codegen_units: Option<u32>,
95 pub rust_debug_assertions: bool,
96 pub rust_debuginfo: bool,
97 pub rust_debuginfo_lines: bool,
98 pub rust_debuginfo_only_std: bool,
99 pub rust_debuginfo_tools: bool,
100 pub rust_rpath: bool,
101 pub rustc_parallel_queries: bool,
102 pub rustc_default_linker: Option<String>,
103 pub rust_optimize_tests: bool,
104 pub rust_debuginfo_tests: bool,
105 pub rust_dist_src: bool,
106 pub rust_codegen_backends: Vec<Interned<String>>,
107 pub rust_codegen_backends_dir: String,
108 pub rust_verify_llvm_ir: bool,
110 pub build: Interned<String>,
111 pub hosts: Vec<Interned<String>>,
112 pub targets: Vec<Interned<String>>,
113 pub local_rebuild: bool,
116 pub dist_sign_folder: Option<PathBuf>,
117 pub dist_upload_addr: Option<String>,
118 pub dist_gpg_password_file: Option<PathBuf>,
121 pub debug_jemalloc: bool,
122 pub use_jemalloc: bool,
123 pub backtrace: bool, // support for RUST_BACKTRACE
124 pub wasm_syscall: bool,
127 pub low_priority: bool,
129 pub verbose_tests: bool,
131 pub save_toolstates: Option<PathBuf>,
132 pub print_step_timings: bool,
134 // Fallback musl-root for all targets
135 pub musl_root: Option<PathBuf>,
136 pub prefix: Option<PathBuf>,
137 pub sysconfdir: Option<PathBuf>,
138 pub datadir: Option<PathBuf>,
139 pub docdir: Option<PathBuf>,
140 pub bindir: Option<PathBuf>,
141 pub libdir: Option<PathBuf>,
142 pub mandir: Option<PathBuf>,
143 pub codegen_tests: bool,
144 pub nodejs: Option<PathBuf>,
145 pub gdb: Option<PathBuf>,
146 pub python: Option<PathBuf>,
147 pub openssl_static: bool,
148 pub configure_args: Vec<String>,
150 // These are either the stage0 downloaded binaries or the locally installed ones.
151 pub initial_cargo: PathBuf,
152 pub initial_rustc: PathBuf,
156 /// Per-target configuration stored in the global configuration structure.
159 /// Some(path to llvm-config) if using an external LLVM.
160 pub llvm_config: Option<PathBuf>,
161 pub jemalloc: Option<PathBuf>,
162 pub cc: Option<PathBuf>,
163 pub cxx: Option<PathBuf>,
164 pub ar: Option<PathBuf>,
165 pub linker: Option<PathBuf>,
166 pub ndk: Option<PathBuf>,
167 pub crt_static: Option<bool>,
168 pub musl_root: Option<PathBuf>,
169 pub qemu_rootfs: Option<PathBuf>,
173 /// Structure of the `config.toml` file that configuration is read from.
175 /// This structure uses `Decodable` to automatically decode a TOML configuration
176 /// file into this format, and then this is traversed and written into the above
177 /// `Config` structure.
178 #[derive(Deserialize, Default)]
179 #[serde(deny_unknown_fields, rename_all = "kebab-case")]
181 build: Option<Build>,
182 install: Option<Install>,
185 target: Option<HashMap<String, TomlTarget>>,
189 /// TOML representation of various global build decisions.
190 #[derive(Deserialize, Default, Clone)]
191 #[serde(deny_unknown_fields, rename_all = "kebab-case")]
193 build: Option<String>,
198 cargo: Option<String>,
199 rustc: Option<String>,
200 low_priority: Option<bool>,
201 compiler_docs: Option<bool>,
203 submodules: Option<bool>,
204 fast_submodules: Option<bool>,
206 locked_deps: Option<bool>,
207 vendor: Option<bool>,
208 nodejs: Option<String>,
209 python: Option<String>,
210 full_bootstrap: Option<bool>,
211 extended: Option<bool>,
212 tools: Option<HashSet<String>>,
213 verbose: Option<usize>,
214 sanitizers: Option<bool>,
215 profiler: Option<bool>,
216 openssl_static: Option<bool>,
217 configure_args: Option<Vec<String>>,
218 local_rebuild: Option<bool>,
219 print_step_timings: Option<bool>,
222 /// TOML representation of various global install decisions.
223 #[derive(Deserialize, Default, Clone)]
224 #[serde(deny_unknown_fields, rename_all = "kebab-case")]
226 prefix: Option<String>,
227 sysconfdir: Option<String>,
228 datadir: Option<String>,
229 docdir: Option<String>,
230 bindir: Option<String>,
231 libdir: Option<String>,
232 mandir: Option<String>,
234 // standard paths, currently unused
235 infodir: Option<String>,
236 localstatedir: Option<String>,
239 /// TOML representation of how the LLVM build is configured.
240 #[derive(Deserialize, Default)]
241 #[serde(deny_unknown_fields, rename_all = "kebab-case")]
243 enabled: Option<bool>,
244 ccache: Option<StringOrBool>,
246 assertions: Option<bool>,
247 optimize: Option<bool>,
248 release_debuginfo: Option<bool>,
249 version_check: Option<bool>,
250 static_libstdcpp: Option<bool>,
251 targets: Option<String>,
252 experimental_targets: Option<String>,
253 link_jobs: Option<u32>,
254 link_shared: Option<bool>,
255 clang_cl: Option<String>
258 #[derive(Deserialize, Default, Clone)]
259 #[serde(deny_unknown_fields, rename_all = "kebab-case")]
261 sign_folder: Option<String>,
262 gpg_password_file: Option<String>,
263 upload_addr: Option<String>,
264 src_tarball: Option<bool>,
267 #[derive(Deserialize)]
274 impl Default for StringOrBool {
275 fn default() -> StringOrBool {
276 StringOrBool::Bool(false)
280 /// TOML representation of how the Rust build is configured.
281 #[derive(Deserialize, Default)]
282 #[serde(deny_unknown_fields, rename_all = "kebab-case")]
284 optimize: Option<bool>,
285 codegen_units: Option<u32>,
286 debug_assertions: Option<bool>,
287 debuginfo: Option<bool>,
288 debuginfo_lines: Option<bool>,
289 debuginfo_only_std: Option<bool>,
290 debuginfo_tools: Option<bool>,
291 experimental_parallel_queries: Option<bool>,
292 debug_jemalloc: Option<bool>,
293 use_jemalloc: Option<bool>,
294 backtrace: Option<bool>,
295 default_linker: Option<String>,
296 channel: Option<String>,
297 musl_root: Option<String>,
299 optimize_tests: Option<bool>,
300 debuginfo_tests: Option<bool>,
301 codegen_tests: Option<bool>,
302 ignore_git: Option<bool>,
304 dist_src: Option<bool>,
305 verbose_tests: Option<bool>,
306 test_miri: Option<bool>,
307 incremental: Option<bool>,
308 save_toolstates: Option<String>,
309 codegen_backends: Option<Vec<String>>,
310 codegen_backends_dir: Option<String>,
311 wasm_syscall: Option<bool>,
313 llvm_tools: Option<bool>,
314 deny_warnings: Option<bool>,
315 backtrace_on_ice: Option<bool>,
316 verify_llvm_ir: Option<bool>,
319 /// TOML representation of how each build target is configured.
320 #[derive(Deserialize, Default)]
321 #[serde(deny_unknown_fields, rename_all = "kebab-case")]
323 llvm_config: Option<String>,
324 jemalloc: Option<String>,
328 linker: Option<String>,
329 android_ndk: Option<String>,
330 crt_static: Option<bool>,
331 musl_root: Option<String>,
332 qemu_rootfs: Option<String>,
336 fn path_from_python(var_key: &str) -> PathBuf {
337 match env::var_os(var_key) {
338 // Do not trust paths from Python and normalize them slightly (#49785).
339 Some(var_val) => Path::new(&var_val).components().collect(),
340 _ => panic!("expected '{}' to be set", var_key),
344 pub fn default_opts() -> Config {
345 let mut config = Config::default();
346 config.llvm_enabled = true;
347 config.llvm_optimize = true;
348 config.llvm_version_check = true;
349 config.use_jemalloc = true;
350 config.backtrace = true;
351 config.rust_optimize = true;
352 config.rust_optimize_tests = true;
353 config.submodules = true;
354 config.fast_submodules = true;
356 config.rust_rpath = true;
357 config.channel = "dev".to_string();
358 config.codegen_tests = true;
359 config.ignore_git = false;
360 config.rust_dist_src = true;
361 config.test_miri = false;
362 config.rust_codegen_backends = vec![INTERNER.intern_str("llvm")];
363 config.rust_codegen_backends_dir = "codegen-backends".to_owned();
364 config.deny_warnings = true;
366 // set by bootstrap.py
367 config.build = INTERNER.intern_str(&env::var("BUILD").expect("'BUILD' to be set"));
368 config.src = Config::path_from_python("SRC");
369 config.out = Config::path_from_python("BUILD_DIR");
371 config.initial_rustc = Config::path_from_python("RUSTC");
372 config.initial_cargo = Config::path_from_python("CARGO");
377 pub fn parse(args: &[String]) -> Config {
378 let flags = Flags::parse(&args);
379 let file = flags.config.clone();
380 let mut config = Config::default_opts();
381 config.exclude = flags.exclude;
382 config.rustc_error_format = flags.rustc_error_format;
383 config.on_fail = flags.on_fail;
384 config.stage = flags.stage;
385 config.jobs = flags.jobs;
386 config.cmd = flags.cmd;
387 config.incremental = flags.incremental;
388 config.dry_run = flags.dry_run;
389 config.keep_stage = flags.keep_stage;
390 if let Some(value) = flags.warnings {
391 config.deny_warnings = value;
395 let dir = config.out.join("tmp-dry-run");
396 t!(fs::create_dir_all(&dir));
400 // If --target was specified but --host wasn't specified, don't run any host-only tests.
401 config.run_host_only = !(flags.host.is_empty() && !flags.target.is_empty());
403 let toml = file.map(|file| {
404 let mut f = t!(File::open(&file));
405 let mut contents = String::new();
406 t!(f.read_to_string(&mut contents));
407 match toml::from_str(&contents) {
410 println!("failed to parse TOML configuration '{}': {}",
411 file.display(), err);
415 }).unwrap_or_else(|| TomlConfig::default());
417 let build = toml.build.clone().unwrap_or(Build::default());
418 // set by bootstrap.py
419 config.hosts.push(config.build.clone());
420 for host in build.host.iter() {
421 let host = INTERNER.intern_str(host);
422 if !config.hosts.contains(&host) {
423 config.hosts.push(host);
426 for target in config.hosts.iter().cloned()
427 .chain(build.target.iter().map(|s| INTERNER.intern_str(s)))
429 if !config.targets.contains(&target) {
430 config.targets.push(target);
433 config.hosts = if !flags.host.is_empty() {
438 config.targets = if !flags.target.is_empty() {
445 config.nodejs = build.nodejs.map(PathBuf::from);
446 config.gdb = build.gdb.map(PathBuf::from);
447 config.python = build.python.map(PathBuf::from);
448 set(&mut config.low_priority, build.low_priority);
449 set(&mut config.compiler_docs, build.compiler_docs);
450 set(&mut config.docs, build.docs);
451 set(&mut config.submodules, build.submodules);
452 set(&mut config.fast_submodules, build.fast_submodules);
453 set(&mut config.locked_deps, build.locked_deps);
454 set(&mut config.vendor, build.vendor);
455 set(&mut config.full_bootstrap, build.full_bootstrap);
456 set(&mut config.extended, build.extended);
457 config.tools = build.tools;
458 set(&mut config.verbose, build.verbose);
459 set(&mut config.sanitizers, build.sanitizers);
460 set(&mut config.profiler, build.profiler);
461 set(&mut config.openssl_static, build.openssl_static);
462 set(&mut config.configure_args, build.configure_args);
463 set(&mut config.local_rebuild, build.local_rebuild);
464 set(&mut config.print_step_timings, build.print_step_timings);
465 config.verbose = cmp::max(config.verbose, flags.verbose);
467 if let Some(ref install) = toml.install {
468 config.prefix = install.prefix.clone().map(PathBuf::from);
469 config.sysconfdir = install.sysconfdir.clone().map(PathBuf::from);
470 config.datadir = install.datadir.clone().map(PathBuf::from);
471 config.docdir = install.docdir.clone().map(PathBuf::from);
472 config.bindir = install.bindir.clone().map(PathBuf::from);
473 config.libdir = install.libdir.clone().map(PathBuf::from);
474 config.mandir = install.mandir.clone().map(PathBuf::from);
477 // Store off these values as options because if they're not provided
478 // we'll infer default values for them later
479 let mut llvm_assertions = None;
480 let mut debuginfo_lines = None;
481 let mut debuginfo_only_std = None;
482 let mut debuginfo_tools = None;
483 let mut debug = None;
484 let mut debug_jemalloc = None;
485 let mut debuginfo = None;
486 let mut debug_assertions = None;
487 let mut optimize = None;
488 let mut ignore_git = None;
490 if let Some(ref llvm) = toml.llvm {
492 Some(StringOrBool::String(ref s)) => {
493 config.ccache = Some(s.to_string())
495 Some(StringOrBool::Bool(true)) => {
496 config.ccache = Some("ccache".to_string());
498 Some(StringOrBool::Bool(false)) | None => {}
500 set(&mut config.ninja, llvm.ninja);
501 set(&mut config.llvm_enabled, llvm.enabled);
502 llvm_assertions = llvm.assertions;
503 set(&mut config.llvm_optimize, llvm.optimize);
504 set(&mut config.llvm_release_debuginfo, llvm.release_debuginfo);
505 set(&mut config.llvm_version_check, llvm.version_check);
506 set(&mut config.llvm_static_stdcpp, llvm.static_libstdcpp);
507 set(&mut config.llvm_link_shared, llvm.link_shared);
508 config.llvm_targets = llvm.targets.clone();
509 config.llvm_experimental_targets = llvm.experimental_targets.clone()
510 .unwrap_or("WebAssembly;RISCV".to_string());
511 config.llvm_link_jobs = llvm.link_jobs;
512 config.llvm_clang_cl = llvm.clang_cl.clone();
515 if let Some(ref rust) = toml.rust {
517 debug_assertions = rust.debug_assertions;
518 debuginfo = rust.debuginfo;
519 debuginfo_lines = rust.debuginfo_lines;
520 debuginfo_only_std = rust.debuginfo_only_std;
521 debuginfo_tools = rust.debuginfo_tools;
522 optimize = rust.optimize;
523 ignore_git = rust.ignore_git;
524 debug_jemalloc = rust.debug_jemalloc;
525 set(&mut config.rust_optimize_tests, rust.optimize_tests);
526 set(&mut config.rust_debuginfo_tests, rust.debuginfo_tests);
527 set(&mut config.codegen_tests, rust.codegen_tests);
528 set(&mut config.rust_rpath, rust.rpath);
529 set(&mut config.use_jemalloc, rust.use_jemalloc);
530 set(&mut config.backtrace, rust.backtrace);
531 set(&mut config.channel, rust.channel.clone());
532 set(&mut config.rust_dist_src, rust.dist_src);
533 set(&mut config.verbose_tests, rust.verbose_tests);
534 set(&mut config.test_miri, rust.test_miri);
535 // in the case "false" is set explicitly, do not overwrite the command line args
536 if let Some(true) = rust.incremental {
537 config.incremental = true;
539 set(&mut config.wasm_syscall, rust.wasm_syscall);
540 set(&mut config.lld_enabled, rust.lld);
541 set(&mut config.llvm_tools_enabled, rust.llvm_tools);
542 config.rustc_parallel_queries = rust.experimental_parallel_queries.unwrap_or(false);
543 config.rustc_default_linker = rust.default_linker.clone();
544 config.musl_root = rust.musl_root.clone().map(PathBuf::from);
545 config.save_toolstates = rust.save_toolstates.clone().map(PathBuf::from);
546 set(&mut config.deny_warnings, rust.deny_warnings.or(flags.warnings));
547 set(&mut config.backtrace_on_ice, rust.backtrace_on_ice);
548 set(&mut config.rust_verify_llvm_ir, rust.verify_llvm_ir);
550 if let Some(ref backends) = rust.codegen_backends {
551 config.rust_codegen_backends = backends.iter()
552 .map(|s| INTERNER.intern_str(s))
556 set(&mut config.rust_codegen_backends_dir, rust.codegen_backends_dir.clone());
558 match rust.codegen_units {
559 Some(0) => config.rust_codegen_units = Some(num_cpus::get() as u32),
560 Some(n) => config.rust_codegen_units = Some(n),
565 if let Some(ref t) = toml.target {
566 for (triple, cfg) in t {
567 let mut target = Target::default();
569 if let Some(ref s) = cfg.llvm_config {
570 target.llvm_config = Some(config.src.join(s));
572 if let Some(ref s) = cfg.jemalloc {
573 target.jemalloc = Some(config.src.join(s));
575 if let Some(ref s) = cfg.android_ndk {
576 target.ndk = Some(config.src.join(s));
578 target.cc = cfg.cc.clone().map(PathBuf::from);
579 target.cxx = cfg.cxx.clone().map(PathBuf::from);
580 target.ar = cfg.ar.clone().map(PathBuf::from);
581 target.linker = cfg.linker.clone().map(PathBuf::from);
582 target.crt_static = cfg.crt_static.clone();
583 target.musl_root = cfg.musl_root.clone().map(PathBuf::from);
584 target.qemu_rootfs = cfg.qemu_rootfs.clone().map(PathBuf::from);
586 config.target_config.insert(INTERNER.intern_string(triple.clone()), target);
590 if let Some(ref t) = toml.dist {
591 config.dist_sign_folder = t.sign_folder.clone().map(PathBuf::from);
592 config.dist_gpg_password_file = t.gpg_password_file.clone().map(PathBuf::from);
593 config.dist_upload_addr = t.upload_addr.clone();
594 set(&mut config.rust_dist_src, t.src_tarball);
597 // Now that we've reached the end of our configuration, infer the
598 // default values for all options that we haven't otherwise stored yet.
600 set(&mut config.initial_rustc, build.rustc.map(PathBuf::from));
601 set(&mut config.initial_cargo, build.cargo.map(PathBuf::from));
604 config.llvm_assertions = llvm_assertions.unwrap_or(default);
606 let default = match &config.channel[..] {
607 "stable" | "beta" | "nightly" => true,
610 config.rust_debuginfo_lines = debuginfo_lines.unwrap_or(default);
611 config.rust_debuginfo_only_std = debuginfo_only_std.unwrap_or(default);
612 config.rust_debuginfo_tools = debuginfo_tools.unwrap_or(false);
614 let default = debug == Some(true);
615 config.debug_jemalloc = debug_jemalloc.unwrap_or(default);
616 config.rust_debuginfo = debuginfo.unwrap_or(default);
617 config.rust_debug_assertions = debug_assertions.unwrap_or(default);
618 config.rust_optimize = optimize.unwrap_or(!default);
620 let default = config.channel == "dev";
621 config.ignore_git = ignore_git.unwrap_or(default);
626 /// Try to find the relative path of `libdir`.
627 pub fn libdir_relative(&self) -> Option<&Path> {
628 let libdir = self.libdir.as_ref()?;
629 if libdir.is_relative() {
632 // Try to make it relative to the prefix.
633 libdir.strip_prefix(self.prefix.as_ref()?).ok()
637 pub fn verbose(&self) -> bool {
641 pub fn very_verbose(&self) -> bool {
646 fn set<T>(field: &mut T, val: Option<T>) {
647 if let Some(v) = val {