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;
30 use toolstate::ToolStates;
32 /// Global configuration for the entire build and/or bootstrap.
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`.
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`.
45 pub ccache: Option<String>,
49 pub compiler_docs: bool,
51 pub locked_deps: bool,
53 pub target_config: HashMap<Interned<String>, Target>,
54 pub full_bootstrap: bool,
60 pub run_host_only: bool,
62 pub on_fail: Option<String>,
63 pub stage: Option<u32>,
64 pub keep_stage: Option<u32>,
66 pub jobs: Option<u32>,
68 pub incremental: bool,
70 // llvm codegen options
71 pub llvm_enabled: bool,
72 pub llvm_assertions: bool,
73 pub llvm_optimize: bool,
74 pub llvm_release_debuginfo: bool,
75 pub llvm_version_check: bool,
76 pub llvm_static_stdcpp: bool,
77 pub llvm_link_shared: bool,
78 pub llvm_targets: Option<String>,
79 pub llvm_experimental_targets: Option<String>,
80 pub llvm_link_jobs: Option<u32>,
82 // rust codegen options
83 pub rust_optimize: bool,
84 pub rust_codegen_units: u32,
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_default_linker: Option<String>,
91 pub rust_optimize_tests: bool,
92 pub rust_debuginfo_tests: bool,
93 pub rust_dist_src: bool,
95 pub build: Interned<String>,
96 pub hosts: Vec<Interned<String>>,
97 pub targets: Vec<Interned<String>>,
98 pub local_rebuild: bool,
101 pub dist_sign_folder: Option<PathBuf>,
102 pub dist_upload_addr: Option<String>,
103 pub dist_gpg_password_file: Option<PathBuf>,
106 pub debug_jemalloc: bool,
107 pub use_jemalloc: bool,
108 pub backtrace: bool, // support for RUST_BACKTRACE
111 pub low_priority: bool,
113 pub quiet_tests: bool,
115 // Fallback musl-root for all targets
116 pub musl_root: Option<PathBuf>,
117 pub prefix: Option<PathBuf>,
118 pub sysconfdir: Option<PathBuf>,
119 pub docdir: Option<PathBuf>,
120 pub bindir: Option<PathBuf>,
121 pub libdir: Option<PathBuf>,
122 pub libdir_relative: Option<PathBuf>,
123 pub mandir: Option<PathBuf>,
124 pub codegen_tests: bool,
125 pub nodejs: Option<PathBuf>,
126 pub gdb: Option<PathBuf>,
127 pub python: Option<PathBuf>,
128 pub openssl_static: bool,
129 pub configure_args: Vec<String>,
131 // These are either the stage0 downloaded binaries or the locally installed ones.
132 pub initial_cargo: PathBuf,
133 pub initial_rustc: PathBuf,
135 pub toolstate: ToolStates,
138 /// Per-target configuration stored in the global configuration structure.
141 /// Some(path to llvm-config) if using an external LLVM.
142 pub llvm_config: Option<PathBuf>,
143 pub jemalloc: Option<PathBuf>,
144 pub cc: Option<PathBuf>,
145 pub cxx: Option<PathBuf>,
146 pub ar: Option<PathBuf>,
147 pub linker: Option<PathBuf>,
148 pub ndk: Option<PathBuf>,
149 pub crt_static: Option<bool>,
150 pub musl_root: Option<PathBuf>,
151 pub qemu_rootfs: Option<PathBuf>,
154 /// Structure of the `config.toml` file that configuration is read from.
156 /// This structure uses `Decodable` to automatically decode a TOML configuration
157 /// file into this format, and then this is traversed and written into the above
158 /// `Config` structure.
159 #[derive(Deserialize, Default)]
160 #[serde(deny_unknown_fields, rename_all = "kebab-case")]
162 build: Option<Build>,
163 install: Option<Install>,
166 target: Option<HashMap<String, TomlTarget>>,
170 /// TOML representation of various global build decisions.
171 #[derive(Deserialize, Default, Clone)]
172 #[serde(deny_unknown_fields, rename_all = "kebab-case")]
174 build: Option<String>,
179 cargo: Option<String>,
180 rustc: Option<String>,
181 low_priority: Option<bool>,
182 compiler_docs: Option<bool>,
184 submodules: Option<bool>,
186 locked_deps: Option<bool>,
187 vendor: Option<bool>,
188 nodejs: Option<String>,
189 python: Option<String>,
190 full_bootstrap: Option<bool>,
191 extended: Option<bool>,
192 verbose: Option<usize>,
193 sanitizers: Option<bool>,
194 profiler: Option<bool>,
195 openssl_static: Option<bool>,
196 configure_args: Option<Vec<String>>,
197 local_rebuild: Option<bool>,
200 /// TOML representation of various global install decisions.
201 #[derive(Deserialize, Default, Clone)]
202 #[serde(deny_unknown_fields, rename_all = "kebab-case")]
204 prefix: Option<String>,
205 sysconfdir: Option<String>,
206 docdir: Option<String>,
207 bindir: Option<String>,
208 libdir: Option<String>,
209 mandir: Option<String>,
212 /// TOML representation of how the LLVM build is configured.
213 #[derive(Deserialize, Default)]
214 #[serde(deny_unknown_fields, rename_all = "kebab-case")]
216 enabled: Option<bool>,
217 ccache: Option<StringOrBool>,
219 assertions: Option<bool>,
220 optimize: Option<bool>,
221 release_debuginfo: Option<bool>,
222 version_check: Option<bool>,
223 static_libstdcpp: Option<bool>,
224 targets: Option<String>,
225 experimental_targets: Option<String>,
226 link_jobs: Option<u32>,
227 link_shared: Option<bool>,
230 #[derive(Deserialize, Default, Clone)]
231 #[serde(deny_unknown_fields, rename_all = "kebab-case")]
233 sign_folder: Option<String>,
234 gpg_password_file: Option<String>,
235 upload_addr: Option<String>,
236 src_tarball: Option<bool>,
239 #[derive(Deserialize)]
246 impl Default for StringOrBool {
247 fn default() -> StringOrBool {
248 StringOrBool::Bool(false)
252 /// TOML representation of how the Rust build is configured.
253 #[derive(Deserialize, Default)]
254 #[serde(deny_unknown_fields, rename_all = "kebab-case")]
256 optimize: Option<bool>,
257 codegen_units: Option<u32>,
258 debug_assertions: Option<bool>,
259 debuginfo: Option<bool>,
260 debuginfo_lines: Option<bool>,
261 debuginfo_only_std: Option<bool>,
262 debug_jemalloc: Option<bool>,
263 use_jemalloc: Option<bool>,
264 backtrace: Option<bool>,
265 default_linker: Option<String>,
266 channel: Option<String>,
267 musl_root: Option<String>,
269 optimize_tests: Option<bool>,
270 debuginfo_tests: Option<bool>,
271 codegen_tests: Option<bool>,
272 ignore_git: Option<bool>,
274 dist_src: Option<bool>,
275 quiet_tests: Option<bool>,
276 test_miri: Option<bool>,
279 /// TOML representation of how each build target is configured.
280 #[derive(Deserialize, Default)]
281 #[serde(deny_unknown_fields, rename_all = "kebab-case")]
283 llvm_config: Option<String>,
284 jemalloc: Option<String>,
288 linker: Option<String>,
289 android_ndk: Option<String>,
290 crt_static: Option<bool>,
291 musl_root: Option<String>,
292 qemu_rootfs: Option<String>,
296 pub fn parse(args: &[String]) -> Config {
297 let flags = Flags::parse(&args);
298 let file = flags.config.clone();
299 let mut config = Config::default();
300 config.llvm_enabled = true;
301 config.llvm_optimize = true;
302 config.use_jemalloc = true;
303 config.backtrace = true;
304 config.rust_optimize = true;
305 config.rust_optimize_tests = true;
306 config.submodules = true;
308 config.rust_rpath = true;
309 config.rust_codegen_units = 1;
310 config.channel = "dev".to_string();
311 config.codegen_tests = true;
312 config.ignore_git = false;
313 config.rust_dist_src = true;
314 config.test_miri = false;
316 config.on_fail = flags.on_fail;
317 config.stage = flags.stage;
318 config.src = flags.src;
319 config.jobs = flags.jobs;
320 config.cmd = flags.cmd;
321 config.incremental = flags.incremental;
322 config.keep_stage = flags.keep_stage;
324 // If --target was specified but --host wasn't specified, don't run any host-only tests.
325 config.run_host_only = flags.host.is_empty() && !flags.target.is_empty();
327 let toml = file.map(|file| {
328 let mut f = t!(File::open(&file));
329 let mut contents = String::new();
330 t!(f.read_to_string(&mut contents));
331 match toml::from_str(&contents) {
334 println!("failed to parse TOML configuration '{}': {}",
335 file.display(), err);
339 }).unwrap_or_else(|| TomlConfig::default());
341 let toolstate_toml_path = config.src.join("src/tools/toolstate.toml");
342 let parse_toolstate = || -> Result<_, Box<::std::error::Error>> {
343 let mut f = File::open(toolstate_toml_path)?;
344 let mut contents = String::new();
345 f.read_to_string(&mut contents)?;
346 Ok(toml::from_str(&contents)?)
348 config.toolstate = parse_toolstate().unwrap_or_else(|err| {
349 println!("failed to parse TOML configuration 'toolstate.toml': {}", err);
353 let build = toml.build.clone().unwrap_or(Build::default());
354 set(&mut config.build, build.build.clone().map(|x| INTERNER.intern_string(x)));
355 set(&mut config.build, flags.build);
356 if config.build.is_empty() {
357 // set by bootstrap.py
358 config.build = INTERNER.intern_str(&env::var("BUILD").unwrap());
360 config.hosts.push(config.build.clone());
361 for host in build.host.iter() {
362 let host = INTERNER.intern_str(host);
363 if !config.hosts.contains(&host) {
364 config.hosts.push(host);
367 for target in config.hosts.iter().cloned()
368 .chain(build.target.iter().map(|s| INTERNER.intern_str(s)))
370 if !config.targets.contains(&target) {
371 config.targets.push(target);
374 config.hosts = if !flags.host.is_empty() {
379 config.targets = if !flags.target.is_empty() {
386 config.nodejs = build.nodejs.map(PathBuf::from);
387 config.gdb = build.gdb.map(PathBuf::from);
388 config.python = build.python.map(PathBuf::from);
389 set(&mut config.low_priority, build.low_priority);
390 set(&mut config.compiler_docs, build.compiler_docs);
391 set(&mut config.docs, build.docs);
392 set(&mut config.submodules, build.submodules);
393 set(&mut config.locked_deps, build.locked_deps);
394 set(&mut config.vendor, build.vendor);
395 set(&mut config.full_bootstrap, build.full_bootstrap);
396 set(&mut config.extended, build.extended);
397 set(&mut config.verbose, build.verbose);
398 set(&mut config.sanitizers, build.sanitizers);
399 set(&mut config.profiler, build.profiler);
400 set(&mut config.openssl_static, build.openssl_static);
401 set(&mut config.configure_args, build.configure_args);
402 set(&mut config.local_rebuild, build.local_rebuild);
403 config.verbose = cmp::max(config.verbose, flags.verbose);
405 if let Some(ref install) = toml.install {
406 config.prefix = install.prefix.clone().map(PathBuf::from);
407 config.sysconfdir = install.sysconfdir.clone().map(PathBuf::from);
408 config.docdir = install.docdir.clone().map(PathBuf::from);
409 config.bindir = install.bindir.clone().map(PathBuf::from);
410 config.libdir = install.libdir.clone().map(PathBuf::from);
411 config.mandir = install.mandir.clone().map(PathBuf::from);
414 // Store off these values as options because if they're not provided
415 // we'll infer default values for them later
416 let mut llvm_assertions = None;
417 let mut debuginfo_lines = None;
418 let mut debuginfo_only_std = None;
419 let mut debug = None;
420 let mut debug_jemalloc = None;
421 let mut debuginfo = None;
422 let mut debug_assertions = None;
423 let mut optimize = None;
424 let mut ignore_git = None;
426 if let Some(ref llvm) = toml.llvm {
428 Some(StringOrBool::String(ref s)) => {
429 config.ccache = Some(s.to_string())
431 Some(StringOrBool::Bool(true)) => {
432 config.ccache = Some("ccache".to_string());
434 Some(StringOrBool::Bool(false)) | None => {}
436 set(&mut config.ninja, llvm.ninja);
437 set(&mut config.llvm_enabled, llvm.enabled);
438 llvm_assertions = llvm.assertions;
439 set(&mut config.llvm_optimize, llvm.optimize);
440 set(&mut config.llvm_release_debuginfo, llvm.release_debuginfo);
441 set(&mut config.llvm_version_check, llvm.version_check);
442 set(&mut config.llvm_static_stdcpp, llvm.static_libstdcpp);
443 set(&mut config.llvm_link_shared, llvm.link_shared);
444 config.llvm_targets = llvm.targets.clone();
445 config.llvm_experimental_targets = llvm.experimental_targets.clone();
446 config.llvm_link_jobs = llvm.link_jobs;
449 if let Some(ref rust) = toml.rust {
451 debug_assertions = rust.debug_assertions;
452 debuginfo = rust.debuginfo;
453 debuginfo_lines = rust.debuginfo_lines;
454 debuginfo_only_std = rust.debuginfo_only_std;
455 optimize = rust.optimize;
456 ignore_git = rust.ignore_git;
457 debug_jemalloc = rust.debug_jemalloc;
458 set(&mut config.rust_optimize_tests, rust.optimize_tests);
459 set(&mut config.rust_debuginfo_tests, rust.debuginfo_tests);
460 set(&mut config.codegen_tests, rust.codegen_tests);
461 set(&mut config.rust_rpath, rust.rpath);
462 set(&mut config.use_jemalloc, rust.use_jemalloc);
463 set(&mut config.backtrace, rust.backtrace);
464 set(&mut config.channel, rust.channel.clone());
465 set(&mut config.rust_dist_src, rust.dist_src);
466 set(&mut config.quiet_tests, rust.quiet_tests);
467 set(&mut config.test_miri, rust.test_miri);
468 config.rustc_default_linker = rust.default_linker.clone();
469 config.musl_root = rust.musl_root.clone().map(PathBuf::from);
471 match rust.codegen_units {
472 Some(0) => config.rust_codegen_units = num_cpus::get() as u32,
473 Some(n) => config.rust_codegen_units = n,
478 if let Some(ref t) = toml.target {
479 for (triple, cfg) in t {
480 let mut target = Target::default();
482 if let Some(ref s) = cfg.llvm_config {
483 target.llvm_config = Some(env::current_dir().unwrap().join(s));
485 if let Some(ref s) = cfg.jemalloc {
486 target.jemalloc = Some(env::current_dir().unwrap().join(s));
488 if let Some(ref s) = cfg.android_ndk {
489 target.ndk = Some(env::current_dir().unwrap().join(s));
491 target.cc = cfg.cc.clone().map(PathBuf::from);
492 target.cxx = cfg.cxx.clone().map(PathBuf::from);
493 target.ar = cfg.ar.clone().map(PathBuf::from);
494 target.linker = cfg.linker.clone().map(PathBuf::from);
495 target.crt_static = cfg.crt_static.clone();
496 target.musl_root = cfg.musl_root.clone().map(PathBuf::from);
497 target.qemu_rootfs = cfg.qemu_rootfs.clone().map(PathBuf::from);
499 config.target_config.insert(INTERNER.intern_string(triple.clone()), target);
503 if let Some(ref t) = toml.dist {
504 config.dist_sign_folder = t.sign_folder.clone().map(PathBuf::from);
505 config.dist_gpg_password_file = t.gpg_password_file.clone().map(PathBuf::from);
506 config.dist_upload_addr = t.upload_addr.clone();
507 set(&mut config.rust_dist_src, t.src_tarball);
510 let cwd = t!(env::current_dir());
511 let out = cwd.join("build");
513 let stage0_root = out.join(&config.build).join("stage0/bin");
514 config.initial_rustc = match build.rustc {
515 Some(s) => PathBuf::from(s),
516 None => stage0_root.join(exe("rustc", &config.build)),
518 config.initial_cargo = match build.cargo {
519 Some(s) => PathBuf::from(s),
520 None => stage0_root.join(exe("cargo", &config.build)),
523 // Now that we've reached the end of our configuration, infer the
524 // default values for all options that we haven't otherwise stored yet.
526 let default = config.channel == "nightly";
527 config.llvm_assertions = llvm_assertions.unwrap_or(default);
529 let default = match &config.channel[..] {
530 "stable" | "beta" | "nightly" => true,
533 config.rust_debuginfo_lines = debuginfo_lines.unwrap_or(default);
534 config.rust_debuginfo_only_std = debuginfo_only_std.unwrap_or(default);
536 let default = debug == Some(true);
537 config.debug_jemalloc = debug_jemalloc.unwrap_or(default);
538 config.rust_debuginfo = debuginfo.unwrap_or(default);
539 config.rust_debug_assertions = debug_assertions.unwrap_or(default);
540 config.rust_optimize = optimize.unwrap_or(!default);
542 let default = config.channel == "dev";
543 config.ignore_git = ignore_git.unwrap_or(default);
548 pub fn verbose(&self) -> bool {
552 pub fn very_verbose(&self) -> bool {
557 fn set<T>(field: &mut T, val: Option<T>) {
558 if let Some(v) = val {