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::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_save_analysis: 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
99 pub quiet_tests: bool,
100 // Fallback musl-root for all targets
101 pub musl_root: Option<PathBuf>,
102 pub prefix: Option<PathBuf>,
103 pub docdir: Option<PathBuf>,
104 pub libdir: Option<PathBuf>,
105 pub libdir_relative: Option<PathBuf>,
106 pub mandir: Option<PathBuf>,
107 pub codegen_tests: bool,
108 pub nodejs: Option<PathBuf>,
109 pub gdb: Option<PathBuf>,
110 pub python: Option<PathBuf>,
111 pub configure_args: Vec<String>,
112 pub openssl_static: bool,
115 /// Per-target configuration stored in the global configuration structure.
118 pub llvm_config: Option<PathBuf>,
119 pub jemalloc: Option<PathBuf>,
120 pub cc: Option<PathBuf>,
121 pub cxx: Option<PathBuf>,
122 pub ndk: Option<PathBuf>,
123 pub musl_root: Option<PathBuf>,
124 pub qemu_rootfs: Option<PathBuf>,
127 /// Structure of the `config.toml` file that configuration is read from.
129 /// This structure uses `Decodable` to automatically decode a TOML configuration
130 /// file into this format, and then this is traversed and written into the above
131 /// `Config` structure.
132 #[derive(RustcDecodable, Default)]
134 build: Option<Build>,
135 install: Option<Install>,
138 target: Option<HashMap<String, TomlTarget>>,
142 /// TOML representation of various global build decisions.
143 #[derive(RustcDecodable, Default, Clone)]
145 build: Option<String>,
148 cargo: Option<String>,
149 rustc: Option<String>,
150 compiler_docs: Option<bool>,
152 submodules: Option<bool>,
154 locked_deps: Option<bool>,
155 vendor: Option<bool>,
156 nodejs: Option<String>,
157 python: Option<String>,
158 full_bootstrap: Option<bool>,
159 extended: Option<bool>,
160 verbose: Option<usize>,
161 sanitizers: Option<bool>,
162 openssl_static: Option<bool>,
165 /// TOML representation of various global install decisions.
166 #[derive(RustcDecodable, Default, Clone)]
168 prefix: Option<String>,
169 mandir: Option<String>,
170 docdir: Option<String>,
171 libdir: Option<String>,
174 /// TOML representation of how the LLVM build is configured.
175 #[derive(RustcDecodable, Default)]
177 ccache: Option<StringOrBool>,
179 assertions: Option<bool>,
180 optimize: Option<bool>,
181 release_debuginfo: Option<bool>,
182 version_check: Option<bool>,
183 static_libstdcpp: Option<bool>,
184 targets: Option<String>,
185 link_jobs: Option<u32>,
186 clean_rebuild: Option<bool>,
189 #[derive(RustcDecodable, Default, Clone)]
191 sign_folder: Option<String>,
192 gpg_password_file: Option<String>,
193 upload_addr: Option<String>,
194 src_tarball: Option<bool>,
197 #[derive(RustcDecodable)]
203 impl Default for StringOrBool {
204 fn default() -> StringOrBool {
205 StringOrBool::Bool(false)
209 /// TOML representation of how the Rust build is configured.
210 #[derive(RustcDecodable, Default)]
212 optimize: Option<bool>,
213 codegen_units: Option<u32>,
214 debug_assertions: Option<bool>,
215 debuginfo: Option<bool>,
216 debuginfo_lines: Option<bool>,
217 debuginfo_only_std: Option<bool>,
218 debug_jemalloc: Option<bool>,
219 use_jemalloc: Option<bool>,
220 backtrace: Option<bool>,
221 default_linker: Option<String>,
222 default_ar: Option<String>,
223 channel: Option<String>,
224 musl_root: Option<String>,
226 optimize_tests: Option<bool>,
227 debuginfo_tests: Option<bool>,
228 codegen_tests: Option<bool>,
229 save_analysis: Option<bool>,
232 /// TOML representation of how each build target is configured.
233 #[derive(RustcDecodable, Default)]
235 llvm_config: Option<String>,
236 jemalloc: Option<String>,
239 android_ndk: Option<String>,
240 musl_root: Option<String>,
241 qemu_rootfs: Option<String>,
245 pub fn parse(build: &str, file: Option<PathBuf>) -> Config {
246 let mut config = Config::default();
247 config.llvm_optimize = true;
248 config.use_jemalloc = true;
249 config.backtrace = true;
250 config.rust_optimize = true;
251 config.rust_optimize_tests = true;
252 config.submodules = true;
254 config.rust_rpath = true;
255 config.rust_codegen_units = 1;
256 config.build = build.to_string();
257 config.channel = "dev".to_string();
258 config.codegen_tests = true;
259 config.rust_dist_src = true;
261 let toml = file.map(|file| {
262 let mut f = t!(File::open(&file));
263 let mut toml = String::new();
264 t!(f.read_to_string(&mut toml));
265 let mut p = Parser::new(&toml);
266 let table = match p.parse() {
267 Some(table) => table,
269 println!("failed to parse TOML configuration:");
270 for err in p.errors.iter() {
271 let (loline, locol) = p.to_linecol(err.lo);
272 let (hiline, hicol) = p.to_linecol(err.hi);
273 println!("{}:{}-{}:{}: {}", loline, locol, hiline,
279 let mut d = Decoder::new(Value::Table(table));
280 match Decodable::decode(&mut d) {
283 println!("failed to decode TOML: {}", e);
287 }).unwrap_or_else(|| TomlConfig::default());
289 let build = toml.build.clone().unwrap_or(Build::default());
290 set(&mut config.build, build.build.clone());
291 config.host.push(config.build.clone());
292 for host in build.host.iter() {
293 if !config.host.contains(host) {
294 config.host.push(host.clone());
297 for target in config.host.iter().chain(&build.target) {
298 if !config.target.contains(target) {
299 config.target.push(target.clone());
302 config.rustc = build.rustc.map(PathBuf::from);
303 config.cargo = build.cargo.map(PathBuf::from);
304 config.nodejs = build.nodejs.map(PathBuf::from);
305 config.gdb = build.gdb.map(PathBuf::from);
306 config.python = build.python.map(PathBuf::from);
307 set(&mut config.compiler_docs, build.compiler_docs);
308 set(&mut config.docs, build.docs);
309 set(&mut config.submodules, build.submodules);
310 set(&mut config.locked_deps, build.locked_deps);
311 set(&mut config.vendor, build.vendor);
312 set(&mut config.full_bootstrap, build.full_bootstrap);
313 set(&mut config.extended, build.extended);
314 set(&mut config.verbose, build.verbose);
315 set(&mut config.sanitizers, build.sanitizers);
316 set(&mut config.openssl_static, build.openssl_static);
318 if let Some(ref install) = toml.install {
319 config.prefix = install.prefix.clone().map(PathBuf::from);
320 config.mandir = install.mandir.clone().map(PathBuf::from);
321 config.docdir = install.docdir.clone().map(PathBuf::from);
322 config.libdir = install.libdir.clone().map(PathBuf::from);
325 if let Some(ref llvm) = toml.llvm {
327 Some(StringOrBool::String(ref s)) => {
328 config.ccache = Some(s.to_string())
330 Some(StringOrBool::Bool(true)) => {
331 config.ccache = Some("ccache".to_string());
333 Some(StringOrBool::Bool(false)) | None => {}
335 set(&mut config.ninja, llvm.ninja);
336 set(&mut config.llvm_assertions, llvm.assertions);
337 set(&mut config.llvm_optimize, llvm.optimize);
338 set(&mut config.llvm_release_debuginfo, llvm.release_debuginfo);
339 set(&mut config.llvm_version_check, llvm.version_check);
340 set(&mut config.llvm_static_stdcpp, llvm.static_libstdcpp);
341 set(&mut config.llvm_clean_rebuild, llvm.clean_rebuild);
342 config.llvm_targets = llvm.targets.clone();
343 config.llvm_link_jobs = llvm.link_jobs;
346 if let Some(ref rust) = toml.rust {
347 set(&mut config.rust_debug_assertions, rust.debug_assertions);
348 set(&mut config.rust_debuginfo, rust.debuginfo);
349 set(&mut config.rust_debuginfo_lines, rust.debuginfo_lines);
350 set(&mut config.rust_debuginfo_only_std, rust.debuginfo_only_std);
351 set(&mut config.rust_optimize, rust.optimize);
352 set(&mut config.rust_optimize_tests, rust.optimize_tests);
353 set(&mut config.rust_debuginfo_tests, rust.debuginfo_tests);
354 set(&mut config.codegen_tests, rust.codegen_tests);
355 set(&mut config.rust_save_analysis, rust.save_analysis);
356 set(&mut config.rust_rpath, rust.rpath);
357 set(&mut config.debug_jemalloc, rust.debug_jemalloc);
358 set(&mut config.use_jemalloc, rust.use_jemalloc);
359 set(&mut config.backtrace, rust.backtrace);
360 set(&mut config.channel, rust.channel.clone());
361 config.rustc_default_linker = rust.default_linker.clone();
362 config.rustc_default_ar = rust.default_ar.clone();
363 config.musl_root = rust.musl_root.clone().map(PathBuf::from);
365 match rust.codegen_units {
366 Some(0) => config.rust_codegen_units = num_cpus::get() as u32,
367 Some(n) => config.rust_codegen_units = n,
372 if let Some(ref t) = toml.target {
373 for (triple, cfg) in t {
374 let mut target = Target::default();
376 if let Some(ref s) = cfg.llvm_config {
377 target.llvm_config = Some(env::current_dir().unwrap().join(s));
379 if let Some(ref s) = cfg.jemalloc {
380 target.jemalloc = Some(env::current_dir().unwrap().join(s));
382 if let Some(ref s) = cfg.android_ndk {
383 target.ndk = Some(env::current_dir().unwrap().join(s));
385 target.cxx = cfg.cxx.clone().map(PathBuf::from);
386 target.cc = cfg.cc.clone().map(PathBuf::from);
387 target.musl_root = cfg.musl_root.clone().map(PathBuf::from);
388 target.qemu_rootfs = cfg.qemu_rootfs.clone().map(PathBuf::from);
390 config.target_config.insert(triple.clone(), target);
394 if let Some(ref t) = toml.dist {
395 config.dist_sign_folder = t.sign_folder.clone().map(PathBuf::from);
396 config.dist_gpg_password_file = t.gpg_password_file.clone().map(PathBuf::from);
397 config.dist_upload_addr = t.upload_addr.clone();
398 set(&mut config.rust_dist_src, t.src_tarball);
404 /// "Temporary" routine to parse `config.mk` into this configuration.
406 /// While we still have `./configure` this implements the ability to decode
407 /// that configuration into this. This isn't exactly a full-blown makefile
408 /// parser, but hey it gets the job done!
409 pub fn update_with_config_mk(&mut self) {
410 let mut config = String::new();
411 File::open("config.mk").unwrap().read_to_string(&mut config).unwrap();
412 for line in config.lines() {
413 let mut parts = line.splitn(2, ":=").map(|s| s.trim());
414 let key = parts.next().unwrap();
415 let value = match parts.next() {
416 Some(n) if n.starts_with('\"') => &n[1..n.len() - 1],
422 ($(($name:expr, $val:expr),)*) => {
425 if key == concat!("CFG_ENABLE_", $name) {
429 if key == concat!("CFG_DISABLE_", $name) {
439 ("MANAGE_SUBMODULES", self.submodules),
440 ("COMPILER_DOCS", self.compiler_docs),
442 ("LLVM_ASSERTIONS", self.llvm_assertions),
443 ("LLVM_RELEASE_DEBUGINFO", self.llvm_release_debuginfo),
444 ("OPTIMIZE_LLVM", self.llvm_optimize),
445 ("LLVM_VERSION_CHECK", self.llvm_version_check),
446 ("LLVM_STATIC_STDCPP", self.llvm_static_stdcpp),
447 ("LLVM_LINK_SHARED", self.llvm_link_shared),
448 ("LLVM_CLEAN_REBUILD", self.llvm_clean_rebuild),
449 ("OPTIMIZE", self.rust_optimize),
450 ("DEBUG_ASSERTIONS", self.rust_debug_assertions),
451 ("DEBUGINFO", self.rust_debuginfo),
452 ("DEBUGINFO_LINES", self.rust_debuginfo_lines),
453 ("DEBUGINFO_ONLY_STD", self.rust_debuginfo_only_std),
454 ("JEMALLOC", self.use_jemalloc),
455 ("DEBUG_JEMALLOC", self.debug_jemalloc),
456 ("RPATH", self.rust_rpath),
457 ("OPTIMIZE_TESTS", self.rust_optimize_tests),
458 ("DEBUGINFO_TESTS", self.rust_debuginfo_tests),
459 ("QUIET_TESTS", self.quiet_tests),
460 ("LOCAL_REBUILD", self.local_rebuild),
461 ("NINJA", self.ninja),
462 ("CODEGEN_TESTS", self.codegen_tests),
463 ("SAVE_ANALYSIS", self.rust_save_analysis),
464 ("LOCKED_DEPS", self.locked_deps),
465 ("VENDOR", self.vendor),
466 ("FULL_BOOTSTRAP", self.full_bootstrap),
467 ("EXTENDED", self.extended),
468 ("SANITIZERS", self.sanitizers),
469 ("DIST_SRC", self.rust_dist_src),
470 ("CARGO_OPENSSL_STATIC", self.openssl_static),
474 "CFG_BUILD" if value.len() > 0 => self.build = value.to_string(),
475 "CFG_HOST" if value.len() > 0 => {
476 self.host.extend(value.split(" ").map(|s| s.to_string()));
479 "CFG_TARGET" if value.len() > 0 => {
480 self.target.extend(value.split(" ").map(|s| s.to_string()));
482 "CFG_MUSL_ROOT" if value.len() > 0 => {
483 self.musl_root = Some(parse_configure_path(value));
485 "CFG_MUSL_ROOT_X86_64" if value.len() > 0 => {
486 let target = "x86_64-unknown-linux-musl".to_string();
487 let target = self.target_config.entry(target)
488 .or_insert(Target::default());
489 target.musl_root = Some(parse_configure_path(value));
491 "CFG_MUSL_ROOT_I686" if value.len() > 0 => {
492 let target = "i686-unknown-linux-musl".to_string();
493 let target = self.target_config.entry(target)
494 .or_insert(Target::default());
495 target.musl_root = Some(parse_configure_path(value));
497 "CFG_MUSL_ROOT_ARM" if value.len() > 0 => {
498 let target = "arm-unknown-linux-musleabi".to_string();
499 let target = self.target_config.entry(target)
500 .or_insert(Target::default());
501 target.musl_root = Some(parse_configure_path(value));
503 "CFG_MUSL_ROOT_ARMHF" if value.len() > 0 => {
504 let target = "arm-unknown-linux-musleabihf".to_string();
505 let target = self.target_config.entry(target)
506 .or_insert(Target::default());
507 target.musl_root = Some(parse_configure_path(value));
509 "CFG_MUSL_ROOT_ARMV7" if value.len() > 0 => {
510 let target = "armv7-unknown-linux-musleabihf".to_string();
511 let target = self.target_config.entry(target)
512 .or_insert(Target::default());
513 target.musl_root = Some(parse_configure_path(value));
515 "CFG_DEFAULT_AR" if value.len() > 0 => {
516 self.rustc_default_ar = Some(value.to_string());
518 "CFG_DEFAULT_LINKER" if value.len() > 0 => {
519 self.rustc_default_linker = Some(value.to_string());
521 "CFG_GDB" if value.len() > 0 => {
522 self.gdb = Some(parse_configure_path(value));
524 "CFG_RELEASE_CHANNEL" => {
525 self.channel = value.to_string();
528 self.prefix = Some(PathBuf::from(value));
531 self.docdir = Some(PathBuf::from(value));
534 self.libdir = Some(PathBuf::from(value));
536 "CFG_LIBDIR_RELATIVE" => {
537 self.libdir_relative = Some(PathBuf::from(value));
540 self.mandir = Some(PathBuf::from(value));
542 "CFG_LLVM_ROOT" if value.len() > 0 => {
543 let target = self.target_config.entry(self.build.clone())
544 .or_insert(Target::default());
545 let root = parse_configure_path(value);
546 target.llvm_config = Some(push_exe_path(root, &["bin", "llvm-config"]));
548 "CFG_JEMALLOC_ROOT" if value.len() > 0 => {
549 let target = self.target_config.entry(self.build.clone())
550 .or_insert(Target::default());
551 target.jemalloc = Some(parse_configure_path(value).join("libjemalloc_pic.a"));
553 "CFG_ARM_LINUX_ANDROIDEABI_NDK" if value.len() > 0 => {
554 let target = "arm-linux-androideabi".to_string();
555 let target = self.target_config.entry(target)
556 .or_insert(Target::default());
557 target.ndk = Some(parse_configure_path(value));
559 "CFG_ARMV7_LINUX_ANDROIDEABI_NDK" if value.len() > 0 => {
560 let target = "armv7-linux-androideabi".to_string();
561 let target = self.target_config.entry(target)
562 .or_insert(Target::default());
563 target.ndk = Some(parse_configure_path(value));
565 "CFG_I686_LINUX_ANDROID_NDK" if value.len() > 0 => {
566 let target = "i686-linux-android".to_string();
567 let target = self.target_config.entry(target)
568 .or_insert(Target::default());
569 target.ndk = Some(parse_configure_path(value));
571 "CFG_AARCH64_LINUX_ANDROID_NDK" if value.len() > 0 => {
572 let target = "aarch64-linux-android".to_string();
573 let target = self.target_config.entry(target)
574 .or_insert(Target::default());
575 target.ndk = Some(parse_configure_path(value));
577 "CFG_LOCAL_RUST_ROOT" if value.len() > 0 => {
578 let path = parse_configure_path(value);
579 self.rustc = Some(push_exe_path(path.clone(), &["bin", "rustc"]));
580 self.cargo = Some(push_exe_path(path, &["bin", "cargo"]));
582 "CFG_PYTHON" if value.len() > 0 => {
583 let path = parse_configure_path(value);
584 self.python = Some(path);
586 "CFG_ENABLE_CCACHE" if value == "1" => {
587 self.ccache = Some("ccache".to_string());
589 "CFG_ENABLE_SCCACHE" if value == "1" => {
590 self.ccache = Some("sccache".to_string());
592 "CFG_CONFIGURE_ARGS" if value.len() > 0 => {
593 self.configure_args = value.split_whitespace()
594 .map(|s| s.to_string())
597 "CFG_QEMU_ARMHF_ROOTFS" if value.len() > 0 => {
598 let target = "arm-unknown-linux-gnueabihf".to_string();
599 let target = self.target_config.entry(target)
600 .or_insert(Target::default());
601 target.qemu_rootfs = Some(parse_configure_path(value));
608 pub fn verbose(&self) -> bool {
612 pub fn very_verbose(&self) -> bool {
618 fn parse_configure_path(path: &str) -> PathBuf {
623 fn parse_configure_path(path: &str) -> PathBuf {
624 // on windows, configure produces unix style paths e.g. /c/some/path but we
625 // only want real windows paths
627 use std::process::Command;
630 // '/' is invalid in windows paths, so we can detect unix paths by the presence of it
631 if !path.contains('/') {
635 let win_path = build_helper::output(Command::new("cygpath").arg("-w").arg(path));
636 let win_path = win_path.trim();
641 fn set<T>(field: &mut T, val: Option<T>) {
642 if let Some(v) = val {