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_debug_assertions: bool,
85 pub rust_debuginfo: bool,
86 pub rust_debuginfo_lines: bool,
87 pub rust_debuginfo_only_std: bool,
89 pub rustc_parallel_queries: 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,
94 pub rust_codegen_backends: Vec<Interned<String>>,
96 pub build: Interned<String>,
97 pub hosts: Vec<Interned<String>>,
98 pub targets: Vec<Interned<String>>,
99 pub local_rebuild: bool,
102 pub dist_sign_folder: Option<PathBuf>,
103 pub dist_upload_addr: Option<String>,
104 pub dist_gpg_password_file: Option<PathBuf>,
107 pub debug_jemalloc: bool,
108 pub use_jemalloc: bool,
109 pub backtrace: bool, // support for RUST_BACKTRACE
112 pub low_priority: bool,
114 pub quiet_tests: bool,
116 pub save_toolstates: Option<PathBuf>,
118 // Fallback musl-root for all targets
119 pub musl_root: Option<PathBuf>,
120 pub prefix: Option<PathBuf>,
121 pub sysconfdir: Option<PathBuf>,
122 pub docdir: Option<PathBuf>,
123 pub bindir: Option<PathBuf>,
124 pub libdir: Option<PathBuf>,
125 pub mandir: Option<PathBuf>,
126 pub codegen_tests: bool,
127 pub nodejs: Option<PathBuf>,
128 pub gdb: Option<PathBuf>,
129 pub python: Option<PathBuf>,
130 pub openssl_static: bool,
131 pub configure_args: Vec<String>,
133 // These are either the stage0 downloaded binaries or the locally installed ones.
134 pub initial_cargo: PathBuf,
135 pub initial_rustc: PathBuf,
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>,
211 // standard paths, currently unused
212 datadir: Option<String>,
213 infodir: Option<String>,
214 localstatedir: Option<String>,
217 /// TOML representation of how the LLVM build is configured.
218 #[derive(Deserialize, Default)]
219 #[serde(deny_unknown_fields, rename_all = "kebab-case")]
221 enabled: Option<bool>,
222 ccache: Option<StringOrBool>,
224 assertions: Option<bool>,
225 optimize: Option<bool>,
226 release_debuginfo: Option<bool>,
227 version_check: Option<bool>,
228 static_libstdcpp: Option<bool>,
229 targets: Option<String>,
230 experimental_targets: Option<String>,
231 link_jobs: Option<u32>,
232 link_shared: Option<bool>,
235 #[derive(Deserialize, Default, Clone)]
236 #[serde(deny_unknown_fields, rename_all = "kebab-case")]
238 sign_folder: Option<String>,
239 gpg_password_file: Option<String>,
240 upload_addr: Option<String>,
241 src_tarball: Option<bool>,
244 #[derive(Deserialize)]
251 impl Default for StringOrBool {
252 fn default() -> StringOrBool {
253 StringOrBool::Bool(false)
257 /// TOML representation of how the Rust build is configured.
258 #[derive(Deserialize, Default)]
259 #[serde(deny_unknown_fields, rename_all = "kebab-case")]
261 optimize: Option<bool>,
262 codegen_units: Option<u32>,
263 debug_assertions: Option<bool>,
264 debuginfo: Option<bool>,
265 debuginfo_lines: Option<bool>,
266 debuginfo_only_std: Option<bool>,
267 experimental_parallel_queries: Option<bool>,
268 debug_jemalloc: Option<bool>,
269 use_jemalloc: Option<bool>,
270 backtrace: Option<bool>,
271 default_linker: Option<String>,
272 channel: Option<String>,
273 musl_root: Option<String>,
275 optimize_tests: Option<bool>,
276 debuginfo_tests: Option<bool>,
277 codegen_tests: Option<bool>,
278 ignore_git: Option<bool>,
280 dist_src: Option<bool>,
281 quiet_tests: Option<bool>,
282 test_miri: Option<bool>,
283 save_toolstates: Option<String>,
284 codegen_backends: Option<Vec<String>>,
287 /// TOML representation of how each build target is configured.
288 #[derive(Deserialize, Default)]
289 #[serde(deny_unknown_fields, rename_all = "kebab-case")]
291 llvm_config: Option<String>,
292 jemalloc: Option<String>,
296 linker: Option<String>,
297 android_ndk: Option<String>,
298 crt_static: Option<bool>,
299 musl_root: Option<String>,
300 qemu_rootfs: Option<String>,
304 pub fn parse(args: &[String]) -> Config {
305 let flags = Flags::parse(&args);
306 let file = flags.config.clone();
307 let mut config = Config::default();
308 config.llvm_enabled = true;
309 config.llvm_optimize = true;
310 config.llvm_version_check = true;
311 config.use_jemalloc = true;
312 config.backtrace = true;
313 config.rust_optimize = true;
314 config.rust_optimize_tests = true;
315 config.submodules = true;
317 config.rust_rpath = true;
318 config.channel = "dev".to_string();
319 config.codegen_tests = true;
320 config.ignore_git = false;
321 config.rust_dist_src = true;
322 config.test_miri = false;
323 config.rust_codegen_backends = vec![INTERNER.intern_str("llvm")];
325 config.on_fail = flags.on_fail;
326 config.stage = flags.stage;
327 config.src = flags.src;
328 config.jobs = flags.jobs;
329 config.cmd = flags.cmd;
330 config.incremental = flags.incremental;
331 config.keep_stage = flags.keep_stage;
333 // If --target was specified but --host wasn't specified, don't run any host-only tests.
334 config.run_host_only = flags.host.is_empty() && !flags.target.is_empty();
336 let toml = file.map(|file| {
337 let mut f = t!(File::open(&file));
338 let mut contents = String::new();
339 t!(f.read_to_string(&mut contents));
340 match toml::from_str(&contents) {
343 println!("failed to parse TOML configuration '{}': {}",
344 file.display(), err);
348 }).unwrap_or_else(|| TomlConfig::default());
350 let build = toml.build.clone().unwrap_or(Build::default());
351 set(&mut config.build, build.build.clone().map(|x| INTERNER.intern_string(x)));
352 set(&mut config.build, flags.build);
353 if config.build.is_empty() {
354 // set by bootstrap.py
355 config.build = INTERNER.intern_str(&env::var("BUILD").unwrap());
357 config.hosts.push(config.build.clone());
358 for host in build.host.iter() {
359 let host = INTERNER.intern_str(host);
360 if !config.hosts.contains(&host) {
361 config.hosts.push(host);
364 for target in config.hosts.iter().cloned()
365 .chain(build.target.iter().map(|s| INTERNER.intern_str(s)))
367 if !config.targets.contains(&target) {
368 config.targets.push(target);
371 config.hosts = if !flags.host.is_empty() {
376 config.targets = if !flags.target.is_empty() {
383 config.nodejs = build.nodejs.map(PathBuf::from);
384 config.gdb = build.gdb.map(PathBuf::from);
385 config.python = build.python.map(PathBuf::from);
386 set(&mut config.low_priority, build.low_priority);
387 set(&mut config.compiler_docs, build.compiler_docs);
388 set(&mut config.docs, build.docs);
389 set(&mut config.submodules, build.submodules);
390 set(&mut config.locked_deps, build.locked_deps);
391 set(&mut config.vendor, build.vendor);
392 set(&mut config.full_bootstrap, build.full_bootstrap);
393 set(&mut config.extended, build.extended);
394 set(&mut config.verbose, build.verbose);
395 set(&mut config.sanitizers, build.sanitizers);
396 set(&mut config.profiler, build.profiler);
397 set(&mut config.openssl_static, build.openssl_static);
398 set(&mut config.configure_args, build.configure_args);
399 set(&mut config.local_rebuild, build.local_rebuild);
400 config.verbose = cmp::max(config.verbose, flags.verbose);
402 if let Some(ref install) = toml.install {
403 config.prefix = install.prefix.clone().map(PathBuf::from);
404 config.sysconfdir = install.sysconfdir.clone().map(PathBuf::from);
405 config.docdir = install.docdir.clone().map(PathBuf::from);
406 config.bindir = install.bindir.clone().map(PathBuf::from);
407 config.libdir = install.libdir.clone().map(PathBuf::from);
408 config.mandir = install.mandir.clone().map(PathBuf::from);
411 // Store off these values as options because if they're not provided
412 // we'll infer default values for them later
413 let mut llvm_assertions = None;
414 let mut debuginfo_lines = None;
415 let mut debuginfo_only_std = None;
416 let mut debug = None;
417 let mut debug_jemalloc = None;
418 let mut debuginfo = None;
419 let mut debug_assertions = None;
420 let mut optimize = None;
421 let mut ignore_git = None;
423 if let Some(ref llvm) = toml.llvm {
425 Some(StringOrBool::String(ref s)) => {
426 config.ccache = Some(s.to_string())
428 Some(StringOrBool::Bool(true)) => {
429 config.ccache = Some("ccache".to_string());
431 Some(StringOrBool::Bool(false)) | None => {}
433 set(&mut config.ninja, llvm.ninja);
434 set(&mut config.llvm_enabled, llvm.enabled);
435 llvm_assertions = llvm.assertions;
436 set(&mut config.llvm_optimize, llvm.optimize);
437 set(&mut config.llvm_release_debuginfo, llvm.release_debuginfo);
438 set(&mut config.llvm_version_check, llvm.version_check);
439 set(&mut config.llvm_static_stdcpp, llvm.static_libstdcpp);
440 set(&mut config.llvm_link_shared, llvm.link_shared);
441 config.llvm_targets = llvm.targets.clone();
442 config.llvm_experimental_targets = llvm.experimental_targets.clone()
443 .unwrap_or("WebAssembly".to_string());
444 config.llvm_link_jobs = llvm.link_jobs;
447 if let Some(ref rust) = toml.rust {
449 debug_assertions = rust.debug_assertions;
450 debuginfo = rust.debuginfo;
451 debuginfo_lines = rust.debuginfo_lines;
452 debuginfo_only_std = rust.debuginfo_only_std;
453 optimize = rust.optimize;
454 ignore_git = rust.ignore_git;
455 debug_jemalloc = rust.debug_jemalloc;
456 set(&mut config.rust_optimize_tests, rust.optimize_tests);
457 set(&mut config.rust_debuginfo_tests, rust.debuginfo_tests);
458 set(&mut config.codegen_tests, rust.codegen_tests);
459 set(&mut config.rust_rpath, rust.rpath);
460 set(&mut config.use_jemalloc, rust.use_jemalloc);
461 set(&mut config.backtrace, rust.backtrace);
462 set(&mut config.channel, rust.channel.clone());
463 set(&mut config.rust_dist_src, rust.dist_src);
464 set(&mut config.quiet_tests, rust.quiet_tests);
465 set(&mut config.test_miri, rust.test_miri);
466 config.rustc_parallel_queries = rust.experimental_parallel_queries.unwrap_or(false);
467 config.rustc_default_linker = rust.default_linker.clone();
468 config.musl_root = rust.musl_root.clone().map(PathBuf::from);
469 config.save_toolstates = rust.save_toolstates.clone().map(PathBuf::from);
471 if let Some(ref backends) = rust.codegen_backends {
472 config.rust_codegen_backends = backends.iter()
473 .map(|s| INTERNER.intern_str(s))
477 match rust.codegen_units {
478 Some(0) => config.rust_codegen_units = Some(num_cpus::get() as u32),
479 Some(n) => config.rust_codegen_units = Some(n),
484 if let Some(ref t) = toml.target {
485 for (triple, cfg) in t {
486 let mut target = Target::default();
488 if let Some(ref s) = cfg.llvm_config {
489 target.llvm_config = Some(env::current_dir().unwrap().join(s));
491 if let Some(ref s) = cfg.jemalloc {
492 target.jemalloc = Some(env::current_dir().unwrap().join(s));
494 if let Some(ref s) = cfg.android_ndk {
495 target.ndk = Some(env::current_dir().unwrap().join(s));
497 target.cc = cfg.cc.clone().map(PathBuf::from);
498 target.cxx = cfg.cxx.clone().map(PathBuf::from);
499 target.ar = cfg.ar.clone().map(PathBuf::from);
500 target.linker = cfg.linker.clone().map(PathBuf::from);
501 target.crt_static = cfg.crt_static.clone();
502 target.musl_root = cfg.musl_root.clone().map(PathBuf::from);
503 target.qemu_rootfs = cfg.qemu_rootfs.clone().map(PathBuf::from);
505 config.target_config.insert(INTERNER.intern_string(triple.clone()), target);
509 if let Some(ref t) = toml.dist {
510 config.dist_sign_folder = t.sign_folder.clone().map(PathBuf::from);
511 config.dist_gpg_password_file = t.gpg_password_file.clone().map(PathBuf::from);
512 config.dist_upload_addr = t.upload_addr.clone();
513 set(&mut config.rust_dist_src, t.src_tarball);
516 let cwd = t!(env::current_dir());
517 let out = cwd.join("build");
519 let stage0_root = out.join(&config.build).join("stage0/bin");
520 config.initial_rustc = match build.rustc {
521 Some(s) => PathBuf::from(s),
522 None => stage0_root.join(exe("rustc", &config.build)),
524 config.initial_cargo = match build.cargo {
525 Some(s) => PathBuf::from(s),
526 None => stage0_root.join(exe("cargo", &config.build)),
529 // Now that we've reached the end of our configuration, infer the
530 // default values for all options that we haven't otherwise stored yet.
533 config.llvm_assertions = llvm_assertions.unwrap_or(default);
535 let default = match &config.channel[..] {
536 "stable" | "beta" | "nightly" => true,
539 config.rust_debuginfo_lines = debuginfo_lines.unwrap_or(default);
540 config.rust_debuginfo_only_std = debuginfo_only_std.unwrap_or(default);
542 let default = debug == Some(true);
543 config.debug_jemalloc = debug_jemalloc.unwrap_or(default);
544 config.rust_debuginfo = debuginfo.unwrap_or(default);
545 config.rust_debug_assertions = debug_assertions.unwrap_or(default);
546 config.rust_optimize = optimize.unwrap_or(!default);
548 let default = config.channel == "dev";
549 config.ignore_git = ignore_git.unwrap_or(default);
554 pub fn verbose(&self) -> bool {
558 pub fn very_verbose(&self) -> bool {
563 fn set<T>(field: &mut T, val: Option<T>) {
564 if let Some(v) = val {