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,
54 // llvm codegen options
55 pub llvm_assertions: bool,
56 pub llvm_optimize: bool,
57 pub llvm_release_debuginfo: bool,
58 pub llvm_version_check: bool,
59 pub llvm_static_stdcpp: bool,
60 pub llvm_link_shared: bool,
61 pub llvm_targets: Option<String>,
62 pub llvm_link_jobs: Option<u32>,
63 pub llvm_clean_rebuild: bool,
65 // rust codegen options
66 pub rust_optimize: bool,
67 pub rust_codegen_units: u32,
68 pub rust_debug_assertions: bool,
69 pub rust_debuginfo: bool,
70 pub rust_debuginfo_lines: bool,
71 pub rust_debuginfo_only_std: bool,
73 pub rustc_default_linker: Option<String>,
74 pub rustc_default_ar: Option<String>,
75 pub rust_optimize_tests: bool,
76 pub rust_debuginfo_tests: bool,
77 pub rust_dist_src: bool,
80 pub host: Vec<String>,
81 pub target: Vec<String>,
82 pub rustc: Option<PathBuf>,
83 pub cargo: Option<PathBuf>,
84 pub local_rebuild: bool,
87 pub dist_sign_folder: Option<PathBuf>,
88 pub dist_upload_addr: Option<String>,
89 pub dist_gpg_password_file: Option<PathBuf>,
92 pub debug_jemalloc: bool,
93 pub use_jemalloc: bool,
94 pub backtrace: bool, // support for RUST_BACKTRACE
98 pub quiet_tests: bool,
99 // Fallback musl-root for all targets
100 pub musl_root: Option<PathBuf>,
101 pub prefix: Option<PathBuf>,
102 pub docdir: Option<PathBuf>,
103 pub libdir: Option<PathBuf>,
104 pub libdir_relative: Option<PathBuf>,
105 pub mandir: Option<PathBuf>,
106 pub codegen_tests: bool,
107 pub nodejs: Option<PathBuf>,
108 pub gdb: Option<PathBuf>,
109 pub python: Option<PathBuf>,
110 pub configure_args: Vec<String>,
111 pub openssl_static: bool,
114 /// Per-target configuration stored in the global configuration structure.
117 pub llvm_config: Option<PathBuf>,
118 pub jemalloc: Option<PathBuf>,
119 pub cc: Option<PathBuf>,
120 pub cxx: Option<PathBuf>,
121 pub ndk: Option<PathBuf>,
122 pub musl_root: Option<PathBuf>,
123 pub qemu_rootfs: Option<PathBuf>,
126 /// Structure of the `config.toml` file that configuration is read from.
128 /// This structure uses `Decodable` to automatically decode a TOML configuration
129 /// file into this format, and then this is traversed and written into the above
130 /// `Config` structure.
131 #[derive(RustcDecodable, Default)]
133 build: Option<Build>,
134 install: Option<Install>,
137 target: Option<HashMap<String, TomlTarget>>,
141 /// TOML representation of various global build decisions.
142 #[derive(RustcDecodable, Default, Clone)]
144 build: Option<String>,
147 cargo: Option<String>,
148 rustc: Option<String>,
149 compiler_docs: Option<bool>,
151 submodules: Option<bool>,
153 locked_deps: Option<bool>,
154 vendor: Option<bool>,
155 nodejs: Option<String>,
156 python: Option<String>,
157 full_bootstrap: Option<bool>,
158 extended: Option<bool>,
159 verbose: Option<usize>,
160 sanitizers: Option<bool>,
161 openssl_static: Option<bool>,
164 /// TOML representation of various global install decisions.
165 #[derive(RustcDecodable, Default, Clone)]
167 prefix: Option<String>,
168 mandir: Option<String>,
169 docdir: Option<String>,
170 libdir: Option<String>,
173 /// TOML representation of how the LLVM build is configured.
174 #[derive(RustcDecodable, Default)]
176 ccache: Option<StringOrBool>,
178 assertions: Option<bool>,
179 optimize: Option<bool>,
180 release_debuginfo: Option<bool>,
181 version_check: Option<bool>,
182 static_libstdcpp: Option<bool>,
183 targets: Option<String>,
184 link_jobs: Option<u32>,
185 clean_rebuild: Option<bool>,
188 #[derive(RustcDecodable, Default, Clone)]
190 sign_folder: Option<String>,
191 gpg_password_file: Option<String>,
192 upload_addr: Option<String>,
193 src_tarball: Option<bool>,
196 #[derive(RustcDecodable)]
202 impl Default for StringOrBool {
203 fn default() -> StringOrBool {
204 StringOrBool::Bool(false)
208 /// TOML representation of how the Rust build is configured.
209 #[derive(RustcDecodable, Default)]
211 optimize: Option<bool>,
212 codegen_units: Option<u32>,
213 debug_assertions: Option<bool>,
214 debuginfo: Option<bool>,
215 debuginfo_lines: Option<bool>,
216 debuginfo_only_std: Option<bool>,
217 debug_jemalloc: Option<bool>,
218 use_jemalloc: Option<bool>,
219 backtrace: Option<bool>,
220 default_linker: Option<String>,
221 default_ar: Option<String>,
222 channel: Option<String>,
223 musl_root: Option<String>,
225 optimize_tests: Option<bool>,
226 debuginfo_tests: Option<bool>,
227 codegen_tests: Option<bool>,
230 /// TOML representation of how each build target is configured.
231 #[derive(RustcDecodable, Default)]
233 llvm_config: Option<String>,
234 jemalloc: Option<String>,
237 android_ndk: Option<String>,
238 musl_root: Option<String>,
239 qemu_rootfs: Option<String>,
243 pub fn parse(build: &str, file: Option<PathBuf>) -> Config {
244 let mut config = Config::default();
245 config.llvm_optimize = true;
246 config.use_jemalloc = true;
247 config.backtrace = true;
248 config.rust_optimize = true;
249 config.rust_optimize_tests = true;
250 config.submodules = true;
252 config.rust_rpath = true;
253 config.rust_codegen_units = 1;
254 config.build = build.to_string();
255 config.channel = "dev".to_string();
256 config.codegen_tests = true;
257 config.rust_dist_src = true;
259 let toml = file.map(|file| {
260 let mut f = t!(File::open(&file));
261 let mut toml = String::new();
262 t!(f.read_to_string(&mut toml));
263 let mut p = Parser::new(&toml);
264 let table = match p.parse() {
265 Some(table) => table,
267 println!("failed to parse TOML configuration:");
268 for err in p.errors.iter() {
269 let (loline, locol) = p.to_linecol(err.lo);
270 let (hiline, hicol) = p.to_linecol(err.hi);
271 println!("{}:{}-{}:{}: {}", loline, locol, hiline,
277 let mut d = Decoder::new(Value::Table(table));
278 match Decodable::decode(&mut d) {
281 println!("failed to decode TOML: {}", e);
285 }).unwrap_or_else(|| TomlConfig::default());
287 let build = toml.build.clone().unwrap_or(Build::default());
288 set(&mut config.build, build.build.clone());
289 config.host.push(config.build.clone());
290 for host in build.host.iter() {
291 if !config.host.contains(host) {
292 config.host.push(host.clone());
295 for target in config.host.iter().chain(&build.target) {
296 if !config.target.contains(target) {
297 config.target.push(target.clone());
300 config.rustc = build.rustc.map(PathBuf::from);
301 config.cargo = build.cargo.map(PathBuf::from);
302 config.nodejs = build.nodejs.map(PathBuf::from);
303 config.gdb = build.gdb.map(PathBuf::from);
304 config.python = build.python.map(PathBuf::from);
305 set(&mut config.compiler_docs, build.compiler_docs);
306 set(&mut config.docs, build.docs);
307 set(&mut config.submodules, build.submodules);
308 set(&mut config.locked_deps, build.locked_deps);
309 set(&mut config.vendor, build.vendor);
310 set(&mut config.full_bootstrap, build.full_bootstrap);
311 set(&mut config.extended, build.extended);
312 set(&mut config.verbose, build.verbose);
313 set(&mut config.sanitizers, build.sanitizers);
314 set(&mut config.openssl_static, build.openssl_static);
316 if let Some(ref install) = toml.install {
317 config.prefix = install.prefix.clone().map(PathBuf::from);
318 config.mandir = install.mandir.clone().map(PathBuf::from);
319 config.docdir = install.docdir.clone().map(PathBuf::from);
320 config.libdir = install.libdir.clone().map(PathBuf::from);
323 if let Some(ref llvm) = toml.llvm {
325 Some(StringOrBool::String(ref s)) => {
326 config.ccache = Some(s.to_string())
328 Some(StringOrBool::Bool(true)) => {
329 config.ccache = Some("ccache".to_string());
331 Some(StringOrBool::Bool(false)) | None => {}
333 set(&mut config.ninja, llvm.ninja);
334 set(&mut config.llvm_assertions, llvm.assertions);
335 set(&mut config.llvm_optimize, llvm.optimize);
336 set(&mut config.llvm_release_debuginfo, llvm.release_debuginfo);
337 set(&mut config.llvm_version_check, llvm.version_check);
338 set(&mut config.llvm_static_stdcpp, llvm.static_libstdcpp);
339 set(&mut config.llvm_clean_rebuild, llvm.clean_rebuild);
340 config.llvm_targets = llvm.targets.clone();
341 config.llvm_link_jobs = llvm.link_jobs;
344 if let Some(ref rust) = toml.rust {
345 set(&mut config.rust_debug_assertions, rust.debug_assertions);
346 set(&mut config.rust_debuginfo, rust.debuginfo);
347 set(&mut config.rust_debuginfo_lines, rust.debuginfo_lines);
348 set(&mut config.rust_debuginfo_only_std, rust.debuginfo_only_std);
349 set(&mut config.rust_optimize, rust.optimize);
350 set(&mut config.rust_optimize_tests, rust.optimize_tests);
351 set(&mut config.rust_debuginfo_tests, rust.debuginfo_tests);
352 set(&mut config.codegen_tests, rust.codegen_tests);
353 set(&mut config.rust_rpath, rust.rpath);
354 set(&mut config.debug_jemalloc, rust.debug_jemalloc);
355 set(&mut config.use_jemalloc, rust.use_jemalloc);
356 set(&mut config.backtrace, rust.backtrace);
357 set(&mut config.channel, rust.channel.clone());
358 config.rustc_default_linker = rust.default_linker.clone();
359 config.rustc_default_ar = rust.default_ar.clone();
360 config.musl_root = rust.musl_root.clone().map(PathBuf::from);
362 match rust.codegen_units {
363 Some(0) => config.rust_codegen_units = num_cpus::get() as u32,
364 Some(n) => config.rust_codegen_units = n,
369 if let Some(ref t) = toml.target {
370 for (triple, cfg) in t {
371 let mut target = Target::default();
373 if let Some(ref s) = cfg.llvm_config {
374 target.llvm_config = Some(env::current_dir().unwrap().join(s));
376 if let Some(ref s) = cfg.jemalloc {
377 target.jemalloc = Some(env::current_dir().unwrap().join(s));
379 if let Some(ref s) = cfg.android_ndk {
380 target.ndk = Some(env::current_dir().unwrap().join(s));
382 target.cxx = cfg.cxx.clone().map(PathBuf::from);
383 target.cc = cfg.cc.clone().map(PathBuf::from);
384 target.musl_root = cfg.musl_root.clone().map(PathBuf::from);
385 target.qemu_rootfs = cfg.qemu_rootfs.clone().map(PathBuf::from);
387 config.target_config.insert(triple.clone(), target);
391 if let Some(ref t) = toml.dist {
392 config.dist_sign_folder = t.sign_folder.clone().map(PathBuf::from);
393 config.dist_gpg_password_file = t.gpg_password_file.clone().map(PathBuf::from);
394 config.dist_upload_addr = t.upload_addr.clone();
395 set(&mut config.rust_dist_src, t.src_tarball);
401 /// "Temporary" routine to parse `config.mk` into this configuration.
403 /// While we still have `./configure` this implements the ability to decode
404 /// that configuration into this. This isn't exactly a full-blown makefile
405 /// parser, but hey it gets the job done!
406 pub fn update_with_config_mk(&mut self) {
407 let mut config = String::new();
408 File::open("config.mk").unwrap().read_to_string(&mut config).unwrap();
409 for line in config.lines() {
410 let mut parts = line.splitn(2, ":=").map(|s| s.trim());
411 let key = parts.next().unwrap();
412 let value = match parts.next() {
413 Some(n) if n.starts_with('\"') => &n[1..n.len() - 1],
419 ($(($name:expr, $val:expr),)*) => {
422 if key == concat!("CFG_ENABLE_", $name) {
426 if key == concat!("CFG_DISABLE_", $name) {
436 ("MANAGE_SUBMODULES", self.submodules),
437 ("COMPILER_DOCS", self.compiler_docs),
439 ("LLVM_ASSERTIONS", self.llvm_assertions),
440 ("LLVM_RELEASE_DEBUGINFO", self.llvm_release_debuginfo),
441 ("OPTIMIZE_LLVM", self.llvm_optimize),
442 ("LLVM_VERSION_CHECK", self.llvm_version_check),
443 ("LLVM_STATIC_STDCPP", self.llvm_static_stdcpp),
444 ("LLVM_LINK_SHARED", self.llvm_link_shared),
445 ("LLVM_CLEAN_REBUILD", self.llvm_clean_rebuild),
446 ("OPTIMIZE", self.rust_optimize),
447 ("DEBUG_ASSERTIONS", self.rust_debug_assertions),
448 ("DEBUGINFO", self.rust_debuginfo),
449 ("DEBUGINFO_LINES", self.rust_debuginfo_lines),
450 ("DEBUGINFO_ONLY_STD", self.rust_debuginfo_only_std),
451 ("JEMALLOC", self.use_jemalloc),
452 ("DEBUG_JEMALLOC", self.debug_jemalloc),
453 ("RPATH", self.rust_rpath),
454 ("OPTIMIZE_TESTS", self.rust_optimize_tests),
455 ("DEBUGINFO_TESTS", self.rust_debuginfo_tests),
456 ("QUIET_TESTS", self.quiet_tests),
457 ("LOCAL_REBUILD", self.local_rebuild),
458 ("NINJA", self.ninja),
459 ("CODEGEN_TESTS", self.codegen_tests),
460 ("LOCKED_DEPS", self.locked_deps),
461 ("VENDOR", self.vendor),
462 ("FULL_BOOTSTRAP", self.full_bootstrap),
463 ("EXTENDED", self.extended),
464 ("SANITIZERS", self.sanitizers),
465 ("DIST_SRC", self.rust_dist_src),
466 ("CARGO_OPENSSL_STATIC", self.openssl_static),
470 "CFG_BUILD" if value.len() > 0 => self.build = value.to_string(),
471 "CFG_HOST" if value.len() > 0 => {
472 self.host.extend(value.split(" ").map(|s| s.to_string()));
475 "CFG_TARGET" if value.len() > 0 => {
476 self.target.extend(value.split(" ").map(|s| s.to_string()));
478 "CFG_MUSL_ROOT" if value.len() > 0 => {
479 self.musl_root = Some(parse_configure_path(value));
481 "CFG_MUSL_ROOT_X86_64" if value.len() > 0 => {
482 let target = "x86_64-unknown-linux-musl".to_string();
483 let target = self.target_config.entry(target)
484 .or_insert(Target::default());
485 target.musl_root = Some(parse_configure_path(value));
487 "CFG_MUSL_ROOT_I686" if value.len() > 0 => {
488 let target = "i686-unknown-linux-musl".to_string();
489 let target = self.target_config.entry(target)
490 .or_insert(Target::default());
491 target.musl_root = Some(parse_configure_path(value));
493 "CFG_MUSL_ROOT_ARM" if value.len() > 0 => {
494 let target = "arm-unknown-linux-musleabi".to_string();
495 let target = self.target_config.entry(target)
496 .or_insert(Target::default());
497 target.musl_root = Some(parse_configure_path(value));
499 "CFG_MUSL_ROOT_ARMHF" if value.len() > 0 => {
500 let target = "arm-unknown-linux-musleabihf".to_string();
501 let target = self.target_config.entry(target)
502 .or_insert(Target::default());
503 target.musl_root = Some(parse_configure_path(value));
505 "CFG_MUSL_ROOT_ARMV7" if value.len() > 0 => {
506 let target = "armv7-unknown-linux-musleabihf".to_string();
507 let target = self.target_config.entry(target)
508 .or_insert(Target::default());
509 target.musl_root = Some(parse_configure_path(value));
511 "CFG_DEFAULT_AR" if value.len() > 0 => {
512 self.rustc_default_ar = Some(value.to_string());
514 "CFG_DEFAULT_LINKER" if value.len() > 0 => {
515 self.rustc_default_linker = Some(value.to_string());
517 "CFG_GDB" if value.len() > 0 => {
518 self.gdb = Some(parse_configure_path(value));
520 "CFG_RELEASE_CHANNEL" => {
521 self.channel = value.to_string();
524 self.prefix = Some(PathBuf::from(value));
527 self.docdir = Some(PathBuf::from(value));
530 self.libdir = Some(PathBuf::from(value));
532 "CFG_LIBDIR_RELATIVE" => {
533 self.libdir_relative = Some(PathBuf::from(value));
536 self.mandir = Some(PathBuf::from(value));
538 "CFG_LLVM_ROOT" if value.len() > 0 => {
539 let target = self.target_config.entry(self.build.clone())
540 .or_insert(Target::default());
541 let root = parse_configure_path(value);
542 target.llvm_config = Some(push_exe_path(root, &["bin", "llvm-config"]));
544 "CFG_JEMALLOC_ROOT" if value.len() > 0 => {
545 let target = self.target_config.entry(self.build.clone())
546 .or_insert(Target::default());
547 target.jemalloc = Some(parse_configure_path(value).join("libjemalloc_pic.a"));
549 "CFG_ARM_LINUX_ANDROIDEABI_NDK" if value.len() > 0 => {
550 let target = "arm-linux-androideabi".to_string();
551 let target = self.target_config.entry(target)
552 .or_insert(Target::default());
553 target.ndk = Some(parse_configure_path(value));
555 "CFG_ARMV7_LINUX_ANDROIDEABI_NDK" if value.len() > 0 => {
556 let target = "armv7-linux-androideabi".to_string();
557 let target = self.target_config.entry(target)
558 .or_insert(Target::default());
559 target.ndk = Some(parse_configure_path(value));
561 "CFG_I686_LINUX_ANDROID_NDK" if value.len() > 0 => {
562 let target = "i686-linux-android".to_string();
563 let target = self.target_config.entry(target)
564 .or_insert(Target::default());
565 target.ndk = Some(parse_configure_path(value));
567 "CFG_AARCH64_LINUX_ANDROID_NDK" if value.len() > 0 => {
568 let target = "aarch64-linux-android".to_string();
569 let target = self.target_config.entry(target)
570 .or_insert(Target::default());
571 target.ndk = Some(parse_configure_path(value));
573 "CFG_LOCAL_RUST_ROOT" if value.len() > 0 => {
574 let path = parse_configure_path(value);
575 self.rustc = Some(push_exe_path(path.clone(), &["bin", "rustc"]));
576 self.cargo = Some(push_exe_path(path, &["bin", "cargo"]));
578 "CFG_PYTHON" if value.len() > 0 => {
579 let path = parse_configure_path(value);
580 self.python = Some(path);
582 "CFG_ENABLE_CCACHE" if value == "1" => {
583 self.ccache = Some(exe("ccache", &self.build));
585 "CFG_ENABLE_SCCACHE" if value == "1" => {
586 self.ccache = Some(exe("sccache", &self.build));
588 "CFG_CONFIGURE_ARGS" if value.len() > 0 => {
589 self.configure_args = value.split_whitespace()
590 .map(|s| s.to_string())
593 "CFG_QEMU_ARMHF_ROOTFS" if value.len() > 0 => {
594 let target = "arm-unknown-linux-gnueabihf".to_string();
595 let target = self.target_config.entry(target)
596 .or_insert(Target::default());
597 target.qemu_rootfs = Some(parse_configure_path(value));
604 pub fn verbose(&self) -> bool {
608 pub fn very_verbose(&self) -> bool {
614 fn parse_configure_path(path: &str) -> PathBuf {
619 fn parse_configure_path(path: &str) -> PathBuf {
620 // on windows, configure produces unix style paths e.g. /c/some/path but we
621 // only want real windows paths
623 use std::process::Command;
626 // '/' is invalid in windows paths, so we can detect unix paths by the presence of it
627 if !path.contains('/') {
631 let win_path = build_helper::output(Command::new("cygpath").arg("-w").arg(path));
632 let win_path = win_path.trim();
637 fn set<T>(field: &mut T, val: Option<T>) {
638 if let Some(v) = val {