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.mk` and `config.toml` configuration
14 //! files to tweak how the build runs.
16 use std::collections::HashMap;
19 use std::io::prelude::*;
20 use std::path::PathBuf;
24 use rustc_serialize::Decodable;
25 use toml::{Parser, Decoder, Value};
26 use util::{exe, push_exe_path};
28 /// Global configuration for the entire build and/or bootstrap.
30 /// This structure is derived from a combination of both `config.toml` and
31 /// `config.mk`. As of the time of this writing it's unlikely that `config.toml`
32 /// is used all that much, so this is primarily filled out by `config.mk` which
33 /// is generated from `./configure`.
35 /// Note that this structure is not decoded directly into, but rather it is
36 /// filled out from the decoded forms of the structs below. For documentation
37 /// each field, see the corresponding fields in
38 /// `src/bootstrap/config.toml.example`.
41 pub ccache: Option<String>,
45 pub compiler_docs: bool,
47 pub locked_deps: bool,
49 pub target_config: HashMap<String, Target>,
50 pub full_bootstrap: bool,
55 // llvm codegen options
56 pub llvm_assertions: bool,
57 pub llvm_optimize: bool,
58 pub llvm_release_debuginfo: bool,
59 pub llvm_version_check: bool,
60 pub llvm_static_stdcpp: bool,
61 pub llvm_link_shared: bool,
62 pub llvm_targets: Option<String>,
63 pub llvm_link_jobs: Option<u32>,
64 pub llvm_clean_rebuild: bool,
66 // rust codegen options
67 pub rust_optimize: bool,
68 pub rust_codegen_units: u32,
69 pub rust_debug_assertions: bool,
70 pub rust_debuginfo: bool,
71 pub rust_debuginfo_lines: bool,
72 pub rust_debuginfo_only_std: bool,
74 pub rustc_default_linker: Option<String>,
75 pub rustc_default_ar: Option<String>,
76 pub rust_optimize_tests: bool,
77 pub rust_debuginfo_tests: bool,
78 pub rust_dist_src: bool,
81 pub host: Vec<String>,
82 pub target: Vec<String>,
83 pub rustc: Option<PathBuf>,
84 pub cargo: Option<PathBuf>,
85 pub local_rebuild: bool,
88 pub dist_sign_folder: Option<PathBuf>,
89 pub dist_upload_addr: Option<String>,
90 pub dist_gpg_password_file: Option<PathBuf>,
93 pub debug_jemalloc: bool,
94 pub use_jemalloc: bool,
95 pub backtrace: bool, // support for RUST_BACKTRACE
98 pub low_priority: bool,
100 pub quiet_tests: bool,
101 // Fallback musl-root for all targets
102 pub musl_root: Option<PathBuf>,
103 pub prefix: Option<PathBuf>,
104 pub sysconfdir: Option<PathBuf>,
105 pub docdir: Option<PathBuf>,
106 pub bindir: Option<PathBuf>,
107 pub libdir: Option<PathBuf>,
108 pub libdir_relative: Option<PathBuf>,
109 pub mandir: Option<PathBuf>,
110 pub codegen_tests: bool,
111 pub nodejs: Option<PathBuf>,
112 pub gdb: Option<PathBuf>,
113 pub python: Option<PathBuf>,
114 pub configure_args: Vec<String>,
115 pub openssl_static: bool,
118 /// Per-target configuration stored in the global configuration structure.
121 pub llvm_config: Option<PathBuf>,
122 pub jemalloc: Option<PathBuf>,
123 pub cc: Option<PathBuf>,
124 pub cxx: Option<PathBuf>,
125 pub ndk: Option<PathBuf>,
126 pub musl_root: Option<PathBuf>,
127 pub qemu_rootfs: Option<PathBuf>,
130 /// Structure of the `config.toml` file that configuration is read from.
132 /// This structure uses `Decodable` to automatically decode a TOML configuration
133 /// file into this format, and then this is traversed and written into the above
134 /// `Config` structure.
135 #[derive(RustcDecodable, Default)]
137 build: Option<Build>,
138 install: Option<Install>,
141 target: Option<HashMap<String, TomlTarget>>,
145 /// TOML representation of various global build decisions.
146 #[derive(RustcDecodable, Default, Clone)]
148 build: Option<String>,
151 cargo: Option<String>,
152 rustc: Option<String>,
153 low_priority: Option<bool>,
154 compiler_docs: Option<bool>,
156 submodules: Option<bool>,
158 locked_deps: Option<bool>,
159 vendor: Option<bool>,
160 nodejs: Option<String>,
161 python: Option<String>,
162 full_bootstrap: Option<bool>,
163 extended: Option<bool>,
164 verbose: Option<usize>,
165 sanitizers: Option<bool>,
166 profiler: Option<bool>,
167 openssl_static: Option<bool>,
170 /// TOML representation of various global install decisions.
171 #[derive(RustcDecodable, Default, Clone)]
173 prefix: Option<String>,
174 sysconfdir: Option<String>,
175 docdir: Option<String>,
176 bindir: Option<String>,
177 libdir: Option<String>,
178 mandir: Option<String>,
181 /// TOML representation of how the LLVM build is configured.
182 #[derive(RustcDecodable, Default)]
184 ccache: Option<StringOrBool>,
186 assertions: Option<bool>,
187 optimize: Option<bool>,
188 release_debuginfo: Option<bool>,
189 version_check: Option<bool>,
190 static_libstdcpp: Option<bool>,
191 targets: Option<String>,
192 link_jobs: Option<u32>,
193 clean_rebuild: Option<bool>,
196 #[derive(RustcDecodable, Default, Clone)]
198 sign_folder: Option<String>,
199 gpg_password_file: Option<String>,
200 upload_addr: Option<String>,
201 src_tarball: Option<bool>,
204 #[derive(RustcDecodable)]
210 impl Default for StringOrBool {
211 fn default() -> StringOrBool {
212 StringOrBool::Bool(false)
216 /// TOML representation of how the Rust build is configured.
217 #[derive(RustcDecodable, Default)]
219 optimize: Option<bool>,
220 codegen_units: Option<u32>,
221 debug_assertions: Option<bool>,
222 debuginfo: Option<bool>,
223 debuginfo_lines: Option<bool>,
224 debuginfo_only_std: Option<bool>,
225 debug_jemalloc: Option<bool>,
226 use_jemalloc: Option<bool>,
227 backtrace: Option<bool>,
228 default_linker: Option<String>,
229 default_ar: Option<String>,
230 channel: Option<String>,
231 musl_root: Option<String>,
233 optimize_tests: Option<bool>,
234 debuginfo_tests: Option<bool>,
235 codegen_tests: Option<bool>,
238 /// TOML representation of how each build target is configured.
239 #[derive(RustcDecodable, Default)]
241 llvm_config: Option<String>,
242 jemalloc: Option<String>,
245 android_ndk: Option<String>,
246 musl_root: Option<String>,
247 qemu_rootfs: Option<String>,
251 pub fn parse(build: &str, file: Option<PathBuf>) -> Config {
252 let mut config = Config::default();
253 config.llvm_optimize = true;
254 config.use_jemalloc = true;
255 config.backtrace = true;
256 config.rust_optimize = true;
257 config.rust_optimize_tests = true;
258 config.submodules = true;
260 config.rust_rpath = true;
261 config.rust_codegen_units = 1;
262 config.build = build.to_string();
263 config.channel = "dev".to_string();
264 config.codegen_tests = true;
265 config.rust_dist_src = true;
267 let toml = file.map(|file| {
268 let mut f = t!(File::open(&file));
269 let mut toml = String::new();
270 t!(f.read_to_string(&mut toml));
271 let mut p = Parser::new(&toml);
272 let table = match p.parse() {
273 Some(table) => table,
275 println!("failed to parse TOML configuration '{}':", file.to_str().unwrap());
276 for err in p.errors.iter() {
277 let (loline, locol) = p.to_linecol(err.lo);
278 let (hiline, hicol) = p.to_linecol(err.hi);
279 println!("{}:{}-{}:{}: {}", loline, locol, hiline,
285 let mut d = Decoder::new(Value::Table(table));
286 match Decodable::decode(&mut d) {
289 println!("failed to decode TOML: {}", e);
293 }).unwrap_or_else(|| TomlConfig::default());
295 let build = toml.build.clone().unwrap_or(Build::default());
296 set(&mut config.build, build.build.clone());
297 config.host.push(config.build.clone());
298 for host in build.host.iter() {
299 if !config.host.contains(host) {
300 config.host.push(host.clone());
303 for target in config.host.iter().chain(&build.target) {
304 if !config.target.contains(target) {
305 config.target.push(target.clone());
308 config.rustc = build.rustc.map(PathBuf::from);
309 config.cargo = build.cargo.map(PathBuf::from);
310 config.nodejs = build.nodejs.map(PathBuf::from);
311 config.gdb = build.gdb.map(PathBuf::from);
312 config.python = build.python.map(PathBuf::from);
313 set(&mut config.low_priority, build.low_priority);
314 set(&mut config.compiler_docs, build.compiler_docs);
315 set(&mut config.docs, build.docs);
316 set(&mut config.submodules, build.submodules);
317 set(&mut config.locked_deps, build.locked_deps);
318 set(&mut config.vendor, build.vendor);
319 set(&mut config.full_bootstrap, build.full_bootstrap);
320 set(&mut config.extended, build.extended);
321 set(&mut config.verbose, build.verbose);
322 set(&mut config.sanitizers, build.sanitizers);
323 set(&mut config.profiler, build.profiler);
324 set(&mut config.openssl_static, build.openssl_static);
326 if let Some(ref install) = toml.install {
327 config.prefix = install.prefix.clone().map(PathBuf::from);
328 config.sysconfdir = install.sysconfdir.clone().map(PathBuf::from);
329 config.docdir = install.docdir.clone().map(PathBuf::from);
330 config.bindir = install.bindir.clone().map(PathBuf::from);
331 config.libdir = install.libdir.clone().map(PathBuf::from);
332 config.mandir = install.mandir.clone().map(PathBuf::from);
335 if let Some(ref llvm) = toml.llvm {
337 Some(StringOrBool::String(ref s)) => {
338 config.ccache = Some(s.to_string())
340 Some(StringOrBool::Bool(true)) => {
341 config.ccache = Some("ccache".to_string());
343 Some(StringOrBool::Bool(false)) | None => {}
345 set(&mut config.ninja, llvm.ninja);
346 set(&mut config.llvm_assertions, llvm.assertions);
347 set(&mut config.llvm_optimize, llvm.optimize);
348 set(&mut config.llvm_release_debuginfo, llvm.release_debuginfo);
349 set(&mut config.llvm_version_check, llvm.version_check);
350 set(&mut config.llvm_static_stdcpp, llvm.static_libstdcpp);
351 set(&mut config.llvm_clean_rebuild, llvm.clean_rebuild);
352 config.llvm_targets = llvm.targets.clone();
353 config.llvm_link_jobs = llvm.link_jobs;
356 if let Some(ref rust) = toml.rust {
357 set(&mut config.rust_debug_assertions, rust.debug_assertions);
358 set(&mut config.rust_debuginfo, rust.debuginfo);
359 set(&mut config.rust_debuginfo_lines, rust.debuginfo_lines);
360 set(&mut config.rust_debuginfo_only_std, rust.debuginfo_only_std);
361 set(&mut config.rust_optimize, rust.optimize);
362 set(&mut config.rust_optimize_tests, rust.optimize_tests);
363 set(&mut config.rust_debuginfo_tests, rust.debuginfo_tests);
364 set(&mut config.codegen_tests, rust.codegen_tests);
365 set(&mut config.rust_rpath, rust.rpath);
366 set(&mut config.debug_jemalloc, rust.debug_jemalloc);
367 set(&mut config.use_jemalloc, rust.use_jemalloc);
368 set(&mut config.backtrace, rust.backtrace);
369 set(&mut config.channel, rust.channel.clone());
370 config.rustc_default_linker = rust.default_linker.clone();
371 config.rustc_default_ar = rust.default_ar.clone();
372 config.musl_root = rust.musl_root.clone().map(PathBuf::from);
374 match rust.codegen_units {
375 Some(0) => config.rust_codegen_units = num_cpus::get() as u32,
376 Some(n) => config.rust_codegen_units = n,
381 if let Some(ref t) = toml.target {
382 for (triple, cfg) in t {
383 let mut target = Target::default();
385 if let Some(ref s) = cfg.llvm_config {
386 target.llvm_config = Some(env::current_dir().unwrap().join(s));
388 if let Some(ref s) = cfg.jemalloc {
389 target.jemalloc = Some(env::current_dir().unwrap().join(s));
391 if let Some(ref s) = cfg.android_ndk {
392 target.ndk = Some(env::current_dir().unwrap().join(s));
394 target.cxx = cfg.cxx.clone().map(PathBuf::from);
395 target.cc = cfg.cc.clone().map(PathBuf::from);
396 target.musl_root = cfg.musl_root.clone().map(PathBuf::from);
397 target.qemu_rootfs = cfg.qemu_rootfs.clone().map(PathBuf::from);
399 config.target_config.insert(triple.clone(), target);
403 if let Some(ref t) = toml.dist {
404 config.dist_sign_folder = t.sign_folder.clone().map(PathBuf::from);
405 config.dist_gpg_password_file = t.gpg_password_file.clone().map(PathBuf::from);
406 config.dist_upload_addr = t.upload_addr.clone();
407 set(&mut config.rust_dist_src, t.src_tarball);
413 /// "Temporary" routine to parse `config.mk` into this configuration.
415 /// While we still have `./configure` this implements the ability to decode
416 /// that configuration into this. This isn't exactly a full-blown makefile
417 /// parser, but hey it gets the job done!
418 pub fn update_with_config_mk(&mut self) {
419 let mut config = String::new();
420 File::open("config.mk").unwrap().read_to_string(&mut config).unwrap();
421 for line in config.lines() {
422 let mut parts = line.splitn(2, ":=").map(|s| s.trim());
423 let key = parts.next().unwrap();
424 let value = match parts.next() {
425 Some(n) if n.starts_with('\"') => &n[1..n.len() - 1],
431 ($(($name:expr, $val:expr),)*) => {
434 if key == concat!("CFG_ENABLE_", $name) {
438 if key == concat!("CFG_DISABLE_", $name) {
448 ("MANAGE_SUBMODULES", self.submodules),
449 ("COMPILER_DOCS", self.compiler_docs),
451 ("LLVM_ASSERTIONS", self.llvm_assertions),
452 ("LLVM_RELEASE_DEBUGINFO", self.llvm_release_debuginfo),
453 ("OPTIMIZE_LLVM", self.llvm_optimize),
454 ("LLVM_VERSION_CHECK", self.llvm_version_check),
455 ("LLVM_STATIC_STDCPP", self.llvm_static_stdcpp),
456 ("LLVM_LINK_SHARED", self.llvm_link_shared),
457 ("LLVM_CLEAN_REBUILD", self.llvm_clean_rebuild),
458 ("OPTIMIZE", self.rust_optimize),
459 ("DEBUG_ASSERTIONS", self.rust_debug_assertions),
460 ("DEBUGINFO", self.rust_debuginfo),
461 ("DEBUGINFO_LINES", self.rust_debuginfo_lines),
462 ("DEBUGINFO_ONLY_STD", self.rust_debuginfo_only_std),
463 ("JEMALLOC", self.use_jemalloc),
464 ("DEBUG_JEMALLOC", self.debug_jemalloc),
465 ("RPATH", self.rust_rpath),
466 ("OPTIMIZE_TESTS", self.rust_optimize_tests),
467 ("DEBUGINFO_TESTS", self.rust_debuginfo_tests),
468 ("QUIET_TESTS", self.quiet_tests),
469 ("LOCAL_REBUILD", self.local_rebuild),
470 ("NINJA", self.ninja),
471 ("CODEGEN_TESTS", self.codegen_tests),
472 ("LOCKED_DEPS", self.locked_deps),
473 ("VENDOR", self.vendor),
474 ("FULL_BOOTSTRAP", self.full_bootstrap),
475 ("EXTENDED", self.extended),
476 ("SANITIZERS", self.sanitizers),
477 ("PROFILER", self.profiler),
478 ("DIST_SRC", self.rust_dist_src),
479 ("CARGO_OPENSSL_STATIC", self.openssl_static),
483 "CFG_BUILD" if value.len() > 0 => self.build = value.to_string(),
484 "CFG_HOST" if value.len() > 0 => {
485 self.host.extend(value.split(" ").map(|s| s.to_string()));
488 "CFG_TARGET" if value.len() > 0 => {
489 self.target.extend(value.split(" ").map(|s| s.to_string()));
491 "CFG_MUSL_ROOT" if value.len() > 0 => {
492 self.musl_root = Some(parse_configure_path(value));
494 "CFG_MUSL_ROOT_X86_64" if value.len() > 0 => {
495 let target = "x86_64-unknown-linux-musl".to_string();
496 let target = self.target_config.entry(target)
497 .or_insert(Target::default());
498 target.musl_root = Some(parse_configure_path(value));
500 "CFG_MUSL_ROOT_I686" if value.len() > 0 => {
501 let target = "i686-unknown-linux-musl".to_string();
502 let target = self.target_config.entry(target)
503 .or_insert(Target::default());
504 target.musl_root = Some(parse_configure_path(value));
506 "CFG_MUSL_ROOT_ARM" if value.len() > 0 => {
507 let target = "arm-unknown-linux-musleabi".to_string();
508 let target = self.target_config.entry(target)
509 .or_insert(Target::default());
510 target.musl_root = Some(parse_configure_path(value));
512 "CFG_MUSL_ROOT_ARMHF" if value.len() > 0 => {
513 let target = "arm-unknown-linux-musleabihf".to_string();
514 let target = self.target_config.entry(target)
515 .or_insert(Target::default());
516 target.musl_root = Some(parse_configure_path(value));
518 "CFG_MUSL_ROOT_ARMV7" if value.len() > 0 => {
519 let target = "armv7-unknown-linux-musleabihf".to_string();
520 let target = self.target_config.entry(target)
521 .or_insert(Target::default());
522 target.musl_root = Some(parse_configure_path(value));
524 "CFG_DEFAULT_AR" if value.len() > 0 => {
525 self.rustc_default_ar = Some(value.to_string());
527 "CFG_DEFAULT_LINKER" if value.len() > 0 => {
528 self.rustc_default_linker = Some(value.to_string());
530 "CFG_GDB" if value.len() > 0 => {
531 self.gdb = Some(parse_configure_path(value));
533 "CFG_RELEASE_CHANNEL" => {
534 self.channel = value.to_string();
537 self.prefix = Some(PathBuf::from(value));
539 "CFG_SYSCONFDIR" => {
540 self.sysconfdir = Some(PathBuf::from(value));
543 self.docdir = Some(PathBuf::from(value));
546 self.bindir = Some(PathBuf::from(value));
549 self.libdir = Some(PathBuf::from(value));
551 "CFG_LIBDIR_RELATIVE" => {
552 self.libdir_relative = Some(PathBuf::from(value));
555 self.mandir = Some(PathBuf::from(value));
557 "CFG_LLVM_ROOT" if value.len() > 0 => {
558 let target = self.target_config.entry(self.build.clone())
559 .or_insert(Target::default());
560 let root = parse_configure_path(value);
561 target.llvm_config = Some(push_exe_path(root, &["bin", "llvm-config"]));
563 "CFG_JEMALLOC_ROOT" if value.len() > 0 => {
564 let target = self.target_config.entry(self.build.clone())
565 .or_insert(Target::default());
566 target.jemalloc = Some(parse_configure_path(value).join("libjemalloc_pic.a"));
568 "CFG_ARM_LINUX_ANDROIDEABI_NDK" if value.len() > 0 => {
569 let target = "arm-linux-androideabi".to_string();
570 let target = self.target_config.entry(target)
571 .or_insert(Target::default());
572 target.ndk = Some(parse_configure_path(value));
574 "CFG_ARMV7_LINUX_ANDROIDEABI_NDK" if value.len() > 0 => {
575 let target = "armv7-linux-androideabi".to_string();
576 let target = self.target_config.entry(target)
577 .or_insert(Target::default());
578 target.ndk = Some(parse_configure_path(value));
580 "CFG_I686_LINUX_ANDROID_NDK" if value.len() > 0 => {
581 let target = "i686-linux-android".to_string();
582 let target = self.target_config.entry(target)
583 .or_insert(Target::default());
584 target.ndk = Some(parse_configure_path(value));
586 "CFG_AARCH64_LINUX_ANDROID_NDK" if value.len() > 0 => {
587 let target = "aarch64-linux-android".to_string();
588 let target = self.target_config.entry(target)
589 .or_insert(Target::default());
590 target.ndk = Some(parse_configure_path(value));
592 "CFG_X86_64_LINUX_ANDROID_NDK" if value.len() > 0 => {
593 let target = "x86_64-linux-android".to_string();
594 let target = self.target_config.entry(target)
595 .or_insert(Target::default());
596 target.ndk = Some(parse_configure_path(value));
598 "CFG_LOCAL_RUST_ROOT" if value.len() > 0 => {
599 let path = parse_configure_path(value);
600 self.rustc = Some(push_exe_path(path.clone(), &["bin", "rustc"]));
601 self.cargo = Some(push_exe_path(path, &["bin", "cargo"]));
603 "CFG_PYTHON" if value.len() > 0 => {
604 let path = parse_configure_path(value);
605 self.python = Some(path);
607 "CFG_ENABLE_CCACHE" if value == "1" => {
608 self.ccache = Some(exe("ccache", &self.build));
610 "CFG_ENABLE_SCCACHE" if value == "1" => {
611 self.ccache = Some(exe("sccache", &self.build));
613 "CFG_CONFIGURE_ARGS" if value.len() > 0 => {
614 self.configure_args = value.split_whitespace()
615 .map(|s| s.to_string())
618 "CFG_QEMU_ARMHF_ROOTFS" if value.len() > 0 => {
619 let target = "arm-unknown-linux-gnueabihf".to_string();
620 let target = self.target_config.entry(target)
621 .or_insert(Target::default());
622 target.qemu_rootfs = Some(parse_configure_path(value));
629 pub fn verbose(&self) -> bool {
633 pub fn very_verbose(&self) -> bool {
639 fn parse_configure_path(path: &str) -> PathBuf {
644 fn parse_configure_path(path: &str) -> PathBuf {
645 // on windows, configure produces unix style paths e.g. /c/some/path but we
646 // only want real windows paths
648 use std::process::Command;
651 // '/' is invalid in windows paths, so we can detect unix paths by the presence of it
652 if !path.contains('/') {
656 let win_path = build_helper::output(Command::new("cygpath").arg("-w").arg(path));
657 let win_path = win_path.trim();
662 fn set<T>(field: &mut T, val: Option<T>) {
663 if let Some(v) = val {