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;
19 use std::io::prelude::*;
20 use std::path::PathBuf;
27 use cache::{INTERNER, Interned};
29 pub use flags::Subcommand;
31 /// Global configuration for the entire build and/or bootstrap.
33 /// This structure is derived from a combination of both `config.toml` and
34 /// `config.mk`. As of the time of this writing it's unlikely that `config.toml`
35 /// is used all that much, so this is primarily filled out by `config.mk` which
36 /// is generated from `./configure`.
38 /// Note that this structure is not decoded directly into, but rather it is
39 /// filled out from the decoded forms of the structs below. For documentation
40 /// each field, see the corresponding fields in
41 /// `config.toml.example`.
44 pub ccache: Option<String>,
48 pub compiler_docs: bool,
50 pub locked_deps: bool,
52 pub target_config: HashMap<Interned<String>, Target>,
53 pub full_bootstrap: bool,
59 pub run_host_only: bool,
61 pub on_fail: Option<String>,
62 pub stage: Option<u32>,
63 pub keep_stage: Option<u32>,
65 pub jobs: Option<u32>,
67 pub incremental: bool,
69 // llvm codegen options
70 pub llvm_enabled: bool,
71 pub llvm_assertions: bool,
72 pub llvm_optimize: bool,
73 pub llvm_release_debuginfo: bool,
74 pub llvm_version_check: bool,
75 pub llvm_static_stdcpp: bool,
76 pub llvm_link_shared: bool,
77 pub llvm_targets: Option<String>,
78 pub llvm_experimental_targets: String,
79 pub llvm_link_jobs: Option<u32>,
81 // rust codegen options
82 pub rust_optimize: bool,
83 pub rust_codegen_units: Option<u32>,
84 pub rust_thinlto: bool,
85 pub rust_debug_assertions: bool,
86 pub rust_debuginfo: bool,
87 pub rust_debuginfo_lines: bool,
88 pub rust_debuginfo_only_std: bool,
90 pub rustc_parallel_queries: bool,
91 pub rustc_default_linker: Option<String>,
92 pub rust_optimize_tests: bool,
93 pub rust_debuginfo_tests: bool,
94 pub rust_dist_src: bool,
95 pub rust_codegen_backends: Vec<Interned<String>>,
97 pub build: Interned<String>,
98 pub hosts: Vec<Interned<String>>,
99 pub targets: Vec<Interned<String>>,
100 pub local_rebuild: bool,
103 pub dist_sign_folder: Option<PathBuf>,
104 pub dist_upload_addr: Option<String>,
105 pub dist_gpg_password_file: Option<PathBuf>,
108 pub debug_jemalloc: bool,
109 pub use_jemalloc: bool,
110 pub backtrace: bool, // support for RUST_BACKTRACE
111 pub wasm_syscall: bool,
114 pub low_priority: bool,
116 pub quiet_tests: bool,
118 pub save_toolstates: Option<PathBuf>,
120 // Fallback musl-root for all targets
121 pub musl_root: Option<PathBuf>,
122 pub prefix: Option<PathBuf>,
123 pub sysconfdir: Option<PathBuf>,
124 pub docdir: Option<PathBuf>,
125 pub bindir: Option<PathBuf>,
126 pub libdir: Option<PathBuf>,
127 pub mandir: Option<PathBuf>,
128 pub codegen_tests: bool,
129 pub nodejs: Option<PathBuf>,
130 pub gdb: Option<PathBuf>,
131 pub python: Option<PathBuf>,
132 pub openssl_static: bool,
133 pub configure_args: Vec<String>,
135 // These are either the stage0 downloaded binaries or the locally installed ones.
136 pub initial_cargo: PathBuf,
137 pub initial_rustc: PathBuf,
140 /// Per-target configuration stored in the global configuration structure.
143 /// Some(path to llvm-config) if using an external LLVM.
144 pub llvm_config: Option<PathBuf>,
145 pub jemalloc: Option<PathBuf>,
146 pub cc: Option<PathBuf>,
147 pub cxx: Option<PathBuf>,
148 pub ar: Option<PathBuf>,
149 pub linker: Option<PathBuf>,
150 pub ndk: Option<PathBuf>,
151 pub crt_static: Option<bool>,
152 pub musl_root: Option<PathBuf>,
153 pub qemu_rootfs: Option<PathBuf>,
156 /// Structure of the `config.toml` file that configuration is read from.
158 /// This structure uses `Decodable` to automatically decode a TOML configuration
159 /// file into this format, and then this is traversed and written into the above
160 /// `Config` structure.
161 #[derive(Deserialize, Default)]
162 #[serde(deny_unknown_fields, rename_all = "kebab-case")]
164 build: Option<Build>,
165 install: Option<Install>,
168 target: Option<HashMap<String, TomlTarget>>,
172 /// TOML representation of various global build decisions.
173 #[derive(Deserialize, Default, Clone)]
174 #[serde(deny_unknown_fields, rename_all = "kebab-case")]
176 build: Option<String>,
181 cargo: Option<String>,
182 rustc: Option<String>,
183 low_priority: Option<bool>,
184 compiler_docs: Option<bool>,
186 submodules: Option<bool>,
188 locked_deps: Option<bool>,
189 vendor: Option<bool>,
190 nodejs: Option<String>,
191 python: Option<String>,
192 full_bootstrap: Option<bool>,
193 extended: Option<bool>,
194 verbose: Option<usize>,
195 sanitizers: Option<bool>,
196 profiler: Option<bool>,
197 openssl_static: Option<bool>,
198 configure_args: Option<Vec<String>>,
199 local_rebuild: Option<bool>,
202 /// TOML representation of various global install decisions.
203 #[derive(Deserialize, Default, Clone)]
204 #[serde(deny_unknown_fields, rename_all = "kebab-case")]
206 prefix: Option<String>,
207 sysconfdir: Option<String>,
208 docdir: Option<String>,
209 bindir: Option<String>,
210 libdir: Option<String>,
211 mandir: Option<String>,
213 // standard paths, currently unused
214 datadir: Option<String>,
215 infodir: Option<String>,
216 localstatedir: Option<String>,
219 /// TOML representation of how the LLVM build is configured.
220 #[derive(Deserialize, Default)]
221 #[serde(deny_unknown_fields, rename_all = "kebab-case")]
223 enabled: Option<bool>,
224 ccache: Option<StringOrBool>,
226 assertions: Option<bool>,
227 optimize: Option<bool>,
228 release_debuginfo: Option<bool>,
229 version_check: Option<bool>,
230 static_libstdcpp: Option<bool>,
231 targets: Option<String>,
232 experimental_targets: Option<String>,
233 link_jobs: Option<u32>,
234 link_shared: Option<bool>,
237 #[derive(Deserialize, Default, Clone)]
238 #[serde(deny_unknown_fields, rename_all = "kebab-case")]
240 sign_folder: Option<String>,
241 gpg_password_file: Option<String>,
242 upload_addr: Option<String>,
243 src_tarball: Option<bool>,
246 #[derive(Deserialize)]
253 impl Default for StringOrBool {
254 fn default() -> StringOrBool {
255 StringOrBool::Bool(false)
259 /// TOML representation of how the Rust build is configured.
260 #[derive(Deserialize, Default)]
261 #[serde(deny_unknown_fields, rename_all = "kebab-case")]
263 optimize: Option<bool>,
264 codegen_units: Option<u32>,
265 thinlto: Option<bool>,
266 debug_assertions: Option<bool>,
267 debuginfo: Option<bool>,
268 debuginfo_lines: Option<bool>,
269 debuginfo_only_std: Option<bool>,
270 experimental_parallel_queries: Option<bool>,
271 debug_jemalloc: Option<bool>,
272 use_jemalloc: Option<bool>,
273 backtrace: Option<bool>,
274 default_linker: Option<String>,
275 channel: Option<String>,
276 musl_root: Option<String>,
278 optimize_tests: Option<bool>,
279 debuginfo_tests: Option<bool>,
280 codegen_tests: Option<bool>,
281 ignore_git: Option<bool>,
283 dist_src: Option<bool>,
284 quiet_tests: Option<bool>,
285 test_miri: Option<bool>,
286 save_toolstates: Option<String>,
287 codegen_backends: Option<Vec<String>>,
288 wasm_syscall: Option<bool>,
291 /// TOML representation of how each build target is configured.
292 #[derive(Deserialize, Default)]
293 #[serde(deny_unknown_fields, rename_all = "kebab-case")]
295 llvm_config: Option<String>,
296 jemalloc: Option<String>,
300 linker: Option<String>,
301 android_ndk: Option<String>,
302 crt_static: Option<bool>,
303 musl_root: Option<String>,
304 qemu_rootfs: Option<String>,
308 pub fn parse(args: &[String]) -> Config {
309 let flags = Flags::parse(&args);
310 let file = flags.config.clone();
311 let mut config = Config::default();
312 config.llvm_enabled = true;
313 config.llvm_optimize = true;
314 config.llvm_version_check = true;
315 config.use_jemalloc = true;
316 config.backtrace = true;
317 config.rust_optimize = true;
318 config.rust_optimize_tests = true;
319 config.submodules = true;
321 config.rust_rpath = true;
322 config.channel = "dev".to_string();
323 config.codegen_tests = true;
324 config.ignore_git = false;
325 config.rust_dist_src = true;
326 config.test_miri = false;
327 config.rust_codegen_backends = vec![INTERNER.intern_str("llvm")];
329 config.on_fail = flags.on_fail;
330 config.stage = flags.stage;
331 config.src = flags.src;
332 config.jobs = flags.jobs;
333 config.cmd = flags.cmd;
334 config.incremental = flags.incremental;
335 config.keep_stage = flags.keep_stage;
337 // If --target was specified but --host wasn't specified, don't run any host-only tests.
338 config.run_host_only = flags.host.is_empty() && !flags.target.is_empty();
340 let toml = file.map(|file| {
341 let mut f = t!(File::open(&file));
342 let mut contents = String::new();
343 t!(f.read_to_string(&mut contents));
344 match toml::from_str(&contents) {
347 println!("failed to parse TOML configuration '{}': {}",
348 file.display(), err);
352 }).unwrap_or_else(|| TomlConfig::default());
354 let build = toml.build.clone().unwrap_or(Build::default());
355 set(&mut config.build, build.build.clone().map(|x| INTERNER.intern_string(x)));
356 set(&mut config.build, flags.build);
357 if config.build.is_empty() {
358 // set by bootstrap.py
359 config.build = INTERNER.intern_str(&env::var("BUILD").unwrap());
361 config.hosts.push(config.build.clone());
362 for host in build.host.iter() {
363 let host = INTERNER.intern_str(host);
364 if !config.hosts.contains(&host) {
365 config.hosts.push(host);
368 for target in config.hosts.iter().cloned()
369 .chain(build.target.iter().map(|s| INTERNER.intern_str(s)))
371 if !config.targets.contains(&target) {
372 config.targets.push(target);
375 config.hosts = if !flags.host.is_empty() {
380 config.targets = if !flags.target.is_empty() {
387 config.nodejs = build.nodejs.map(PathBuf::from);
388 config.gdb = build.gdb.map(PathBuf::from);
389 config.python = build.python.map(PathBuf::from);
390 set(&mut config.low_priority, build.low_priority);
391 set(&mut config.compiler_docs, build.compiler_docs);
392 set(&mut config.docs, build.docs);
393 set(&mut config.submodules, build.submodules);
394 set(&mut config.locked_deps, build.locked_deps);
395 set(&mut config.vendor, build.vendor);
396 set(&mut config.full_bootstrap, build.full_bootstrap);
397 set(&mut config.extended, build.extended);
398 set(&mut config.verbose, build.verbose);
399 set(&mut config.sanitizers, build.sanitizers);
400 set(&mut config.profiler, build.profiler);
401 set(&mut config.openssl_static, build.openssl_static);
402 set(&mut config.configure_args, build.configure_args);
403 set(&mut config.local_rebuild, build.local_rebuild);
404 config.verbose = cmp::max(config.verbose, flags.verbose);
406 if let Some(ref install) = toml.install {
407 config.prefix = install.prefix.clone().map(PathBuf::from);
408 config.sysconfdir = install.sysconfdir.clone().map(PathBuf::from);
409 config.docdir = install.docdir.clone().map(PathBuf::from);
410 config.bindir = install.bindir.clone().map(PathBuf::from);
411 config.libdir = install.libdir.clone().map(PathBuf::from);
412 config.mandir = install.mandir.clone().map(PathBuf::from);
415 // Store off these values as options because if they're not provided
416 // we'll infer default values for them later
417 let mut thinlto = None;
418 let mut llvm_assertions = None;
419 let mut debuginfo_lines = None;
420 let mut debuginfo_only_std = None;
421 let mut debug = None;
422 let mut debug_jemalloc = None;
423 let mut debuginfo = None;
424 let mut debug_assertions = None;
425 let mut optimize = None;
426 let mut ignore_git = None;
428 if let Some(ref llvm) = toml.llvm {
430 Some(StringOrBool::String(ref s)) => {
431 config.ccache = Some(s.to_string())
433 Some(StringOrBool::Bool(true)) => {
434 config.ccache = Some("ccache".to_string());
436 Some(StringOrBool::Bool(false)) | None => {}
438 set(&mut config.ninja, llvm.ninja);
439 set(&mut config.llvm_enabled, llvm.enabled);
440 llvm_assertions = llvm.assertions;
441 set(&mut config.llvm_optimize, llvm.optimize);
442 set(&mut config.llvm_release_debuginfo, llvm.release_debuginfo);
443 set(&mut config.llvm_version_check, llvm.version_check);
444 set(&mut config.llvm_static_stdcpp, llvm.static_libstdcpp);
445 set(&mut config.llvm_link_shared, llvm.link_shared);
446 config.llvm_targets = llvm.targets.clone();
447 config.llvm_experimental_targets = llvm.experimental_targets.clone()
448 .unwrap_or("WebAssembly".to_string());
449 config.llvm_link_jobs = llvm.link_jobs;
452 if let Some(ref rust) = toml.rust {
454 debug_assertions = rust.debug_assertions;
455 debuginfo = rust.debuginfo;
456 debuginfo_lines = rust.debuginfo_lines;
457 debuginfo_only_std = rust.debuginfo_only_std;
458 optimize = rust.optimize;
459 ignore_git = rust.ignore_git;
460 debug_jemalloc = rust.debug_jemalloc;
461 thinlto = rust.thinlto;
462 set(&mut config.rust_optimize_tests, rust.optimize_tests);
463 set(&mut config.rust_debuginfo_tests, rust.debuginfo_tests);
464 set(&mut config.codegen_tests, rust.codegen_tests);
465 set(&mut config.rust_rpath, rust.rpath);
466 set(&mut config.use_jemalloc, rust.use_jemalloc);
467 set(&mut config.backtrace, rust.backtrace);
468 set(&mut config.channel, rust.channel.clone());
469 set(&mut config.rust_dist_src, rust.dist_src);
470 set(&mut config.quiet_tests, rust.quiet_tests);
471 set(&mut config.test_miri, rust.test_miri);
472 set(&mut config.wasm_syscall, rust.wasm_syscall);
473 config.rustc_parallel_queries = rust.experimental_parallel_queries.unwrap_or(false);
474 config.rustc_default_linker = rust.default_linker.clone();
475 config.musl_root = rust.musl_root.clone().map(PathBuf::from);
476 config.save_toolstates = rust.save_toolstates.clone().map(PathBuf::from);
478 if let Some(ref backends) = rust.codegen_backends {
479 config.rust_codegen_backends = backends.iter()
480 .map(|s| INTERNER.intern_str(s))
484 match rust.codegen_units {
485 Some(0) => config.rust_codegen_units = Some(num_cpus::get() as u32),
486 Some(n) => config.rust_codegen_units = Some(n),
491 if let Some(ref t) = toml.target {
492 for (triple, cfg) in t {
493 let mut target = Target::default();
495 if let Some(ref s) = cfg.llvm_config {
496 target.llvm_config = Some(env::current_dir().unwrap().join(s));
498 if let Some(ref s) = cfg.jemalloc {
499 target.jemalloc = Some(env::current_dir().unwrap().join(s));
501 if let Some(ref s) = cfg.android_ndk {
502 target.ndk = Some(env::current_dir().unwrap().join(s));
504 target.cc = cfg.cc.clone().map(PathBuf::from);
505 target.cxx = cfg.cxx.clone().map(PathBuf::from);
506 target.ar = cfg.ar.clone().map(PathBuf::from);
507 target.linker = cfg.linker.clone().map(PathBuf::from);
508 target.crt_static = cfg.crt_static.clone();
509 target.musl_root = cfg.musl_root.clone().map(PathBuf::from);
510 target.qemu_rootfs = cfg.qemu_rootfs.clone().map(PathBuf::from);
512 config.target_config.insert(INTERNER.intern_string(triple.clone()), target);
516 if let Some(ref t) = toml.dist {
517 config.dist_sign_folder = t.sign_folder.clone().map(PathBuf::from);
518 config.dist_gpg_password_file = t.gpg_password_file.clone().map(PathBuf::from);
519 config.dist_upload_addr = t.upload_addr.clone();
520 set(&mut config.rust_dist_src, t.src_tarball);
523 let cwd = t!(env::current_dir());
524 let out = cwd.join("build");
526 let stage0_root = out.join(&config.build).join("stage0/bin");
527 config.initial_rustc = match build.rustc {
528 Some(s) => PathBuf::from(s),
529 None => stage0_root.join(exe("rustc", &config.build)),
531 config.initial_cargo = match build.cargo {
532 Some(s) => PathBuf::from(s),
533 None => stage0_root.join(exe("cargo", &config.build)),
536 // Now that we've reached the end of our configuration, infer the
537 // default values for all options that we haven't otherwise stored yet.
540 config.llvm_assertions = llvm_assertions.unwrap_or(default);
542 let default = match &config.channel[..] {
543 "stable" | "beta" | "nightly" => true,
546 config.rust_thinlto = thinlto.unwrap_or(true);
547 config.rust_debuginfo_lines = debuginfo_lines.unwrap_or(default);
548 config.rust_debuginfo_only_std = debuginfo_only_std.unwrap_or(default);
550 let default = debug == Some(true);
551 config.debug_jemalloc = debug_jemalloc.unwrap_or(default);
552 config.rust_debuginfo = debuginfo.unwrap_or(default);
553 config.rust_debug_assertions = debug_assertions.unwrap_or(default);
554 config.rust_optimize = optimize.unwrap_or(!default);
556 let default = config.channel == "dev";
557 config.ignore_git = ignore_git.unwrap_or(default);
562 pub fn verbose(&self) -> bool {
566 pub fn very_verbose(&self) -> bool {
571 fn set<T>(field: &mut T, val: Option<T>) {
572 if let Some(v) = val {