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;
18 use std::fs::{self, File};
19 use std::io::prelude::*;
20 use std::path::PathBuf;
25 use util::{exe, push_exe_path};
27 /// Global configuration for the entire build and/or bootstrap.
29 /// This structure is derived from a combination of both `config.toml` and
30 /// `config.mk`. As of the time of this writing it's unlikely that `config.toml`
31 /// is used all that much, so this is primarily filled out by `config.mk` which
32 /// is generated from `./configure`.
34 /// Note that this structure is not decoded directly into, but rather it is
35 /// filled out from the decoded forms of the structs below. For documentation
36 /// each field, see the corresponding fields in
37 /// `src/bootstrap/config.toml.example`.
40 pub ccache: Option<String>,
44 pub compiler_docs: bool,
46 pub locked_deps: bool,
48 pub target_config: HashMap<String, Target>,
49 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_experimental_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 local_rebuild: bool,
86 pub dist_sign_folder: Option<PathBuf>,
87 pub dist_upload_addr: Option<String>,
88 pub dist_gpg_password_file: Option<PathBuf>,
91 pub debug_jemalloc: bool,
92 pub use_jemalloc: bool,
93 pub backtrace: bool, // support for RUST_BACKTRACE
96 pub low_priority: bool,
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 sysconfdir: Option<PathBuf>,
103 pub docdir: Option<PathBuf>,
104 pub bindir: Option<PathBuf>,
105 pub libdir: Option<PathBuf>,
106 pub libdir_relative: Option<PathBuf>,
107 pub mandir: Option<PathBuf>,
108 pub codegen_tests: bool,
109 pub nodejs: Option<PathBuf>,
110 pub gdb: Option<PathBuf>,
111 pub python: Option<PathBuf>,
112 pub configure_args: Vec<String>,
113 pub openssl_static: bool,
116 // These are either the stage0 downloaded binaries or the locally installed ones.
117 pub initial_cargo: PathBuf,
118 pub initial_rustc: PathBuf,
122 /// Per-target configuration stored in the global configuration structure.
125 /// Some(path to llvm-config) if using an external LLVM.
126 pub llvm_config: Option<PathBuf>,
127 pub jemalloc: Option<PathBuf>,
128 pub cc: Option<PathBuf>,
129 pub cxx: Option<PathBuf>,
130 pub ndk: Option<PathBuf>,
131 pub musl_root: Option<PathBuf>,
132 pub qemu_rootfs: Option<PathBuf>,
135 /// Structure of the `config.toml` file that configuration is read from.
137 /// This structure uses `Decodable` to automatically decode a TOML configuration
138 /// file into this format, and then this is traversed and written into the above
139 /// `Config` structure.
140 #[derive(Deserialize, Default)]
141 #[serde(deny_unknown_fields)]
142 #[serde(rename_all = "kebab-case")]
144 build: Option<Build>,
145 install: Option<Install>,
148 target: Option<HashMap<String, TomlTarget>>,
152 /// TOML representation of various global build decisions.
153 #[derive(Deserialize, Default, Clone)]
154 #[serde(deny_unknown_fields)]
155 #[serde(rename_all = "kebab-case")]
157 build: Option<String>,
162 cargo: Option<String>,
163 rustc: Option<String>,
164 low_priority: Option<bool>,
165 compiler_docs: Option<bool>,
167 submodules: Option<bool>,
169 locked_deps: Option<bool>,
170 vendor: Option<bool>,
171 nodejs: Option<String>,
172 python: Option<String>,
173 full_bootstrap: Option<bool>,
174 extended: Option<bool>,
175 verbose: Option<usize>,
176 sanitizers: Option<bool>,
177 profiler: Option<bool>,
178 openssl_static: Option<bool>,
181 /// TOML representation of various global install decisions.
182 #[derive(Deserialize, Default, Clone)]
183 #[serde(deny_unknown_fields)]
184 #[serde(rename_all = "kebab-case")]
186 prefix: Option<String>,
187 sysconfdir: Option<String>,
188 docdir: Option<String>,
189 bindir: Option<String>,
190 libdir: Option<String>,
191 mandir: Option<String>,
194 /// TOML representation of how the LLVM build is configured.
195 #[derive(Deserialize, Default)]
196 #[serde(deny_unknown_fields)]
197 #[serde(rename_all = "kebab-case")]
199 ccache: Option<StringOrBool>,
201 assertions: Option<bool>,
202 optimize: Option<bool>,
203 release_debuginfo: Option<bool>,
204 version_check: Option<bool>,
205 static_libstdcpp: Option<bool>,
206 targets: Option<String>,
207 experimental_targets: Option<String>,
208 link_jobs: Option<u32>,
209 clean_rebuild: Option<bool>,
212 #[derive(Deserialize, Default, Clone)]
213 #[serde(deny_unknown_fields)]
214 #[serde(rename_all = "kebab-case")]
216 sign_folder: Option<String>,
217 gpg_password_file: Option<String>,
218 upload_addr: Option<String>,
219 src_tarball: Option<bool>,
222 #[derive(Deserialize)]
229 impl Default for StringOrBool {
230 fn default() -> StringOrBool {
231 StringOrBool::Bool(false)
235 /// TOML representation of how the Rust build is configured.
236 #[derive(Deserialize, Default)]
237 #[serde(deny_unknown_fields)]
238 #[serde(rename_all = "kebab-case")]
240 optimize: Option<bool>,
241 codegen_units: Option<u32>,
242 debug_assertions: Option<bool>,
243 debuginfo: Option<bool>,
244 debuginfo_lines: Option<bool>,
245 debuginfo_only_std: Option<bool>,
246 debug_jemalloc: Option<bool>,
247 use_jemalloc: Option<bool>,
248 backtrace: Option<bool>,
249 default_linker: Option<String>,
250 default_ar: Option<String>,
251 channel: Option<String>,
252 musl_root: Option<String>,
254 optimize_tests: Option<bool>,
255 debuginfo_tests: Option<bool>,
256 codegen_tests: Option<bool>,
259 /// TOML representation of how each build target is configured.
260 #[derive(Deserialize, Default)]
261 #[serde(deny_unknown_fields)]
262 #[serde(rename_all = "kebab-case")]
264 llvm_config: Option<String>,
265 jemalloc: Option<String>,
268 android_ndk: Option<String>,
269 musl_root: Option<String>,
270 qemu_rootfs: Option<String>,
274 pub fn parse(build: &str, file: Option<PathBuf>) -> Config {
275 let mut config = Config::default();
276 config.llvm_optimize = true;
277 config.use_jemalloc = true;
278 config.backtrace = true;
279 config.rust_optimize = true;
280 config.rust_optimize_tests = true;
281 config.submodules = true;
283 config.rust_rpath = true;
284 config.rust_codegen_units = 1;
285 config.build = build.to_string();
286 config.channel = "dev".to_string();
287 config.codegen_tests = true;
288 config.rust_dist_src = true;
290 let toml = file.map(|file| {
291 let mut f = t!(File::open(&file));
292 let mut contents = String::new();
293 t!(f.read_to_string(&mut contents));
294 match toml::from_str(&contents) {
297 println!("failed to parse TOML configuration '{}': {}",
298 file.display(), err);
302 }).unwrap_or_else(|| TomlConfig::default());
304 let build = toml.build.clone().unwrap_or(Build::default());
305 set(&mut config.build, build.build.clone());
306 config.host.push(config.build.clone());
307 for host in build.host.iter() {
308 if !config.host.contains(host) {
309 config.host.push(host.clone());
312 for target in config.host.iter().chain(&build.target) {
313 if !config.target.contains(target) {
314 config.target.push(target.clone());
317 config.nodejs = build.nodejs.map(PathBuf::from);
318 config.gdb = build.gdb.map(PathBuf::from);
319 config.python = build.python.map(PathBuf::from);
320 set(&mut config.low_priority, build.low_priority);
321 set(&mut config.compiler_docs, build.compiler_docs);
322 set(&mut config.docs, build.docs);
323 set(&mut config.submodules, build.submodules);
324 set(&mut config.locked_deps, build.locked_deps);
325 set(&mut config.vendor, build.vendor);
326 set(&mut config.full_bootstrap, build.full_bootstrap);
327 set(&mut config.extended, build.extended);
328 set(&mut config.verbose, build.verbose);
329 set(&mut config.sanitizers, build.sanitizers);
330 set(&mut config.profiler, build.profiler);
331 set(&mut config.openssl_static, build.openssl_static);
333 if let Some(ref install) = toml.install {
334 config.prefix = install.prefix.clone().map(PathBuf::from);
335 config.sysconfdir = install.sysconfdir.clone().map(PathBuf::from);
336 config.docdir = install.docdir.clone().map(PathBuf::from);
337 config.bindir = install.bindir.clone().map(PathBuf::from);
338 config.libdir = install.libdir.clone().map(PathBuf::from);
339 config.mandir = install.mandir.clone().map(PathBuf::from);
342 if let Some(ref llvm) = toml.llvm {
344 Some(StringOrBool::String(ref s)) => {
345 config.ccache = Some(s.to_string())
347 Some(StringOrBool::Bool(true)) => {
348 config.ccache = Some("ccache".to_string());
350 Some(StringOrBool::Bool(false)) | None => {}
352 set(&mut config.ninja, llvm.ninja);
353 set(&mut config.llvm_assertions, llvm.assertions);
354 set(&mut config.llvm_optimize, llvm.optimize);
355 set(&mut config.llvm_release_debuginfo, llvm.release_debuginfo);
356 set(&mut config.llvm_version_check, llvm.version_check);
357 set(&mut config.llvm_static_stdcpp, llvm.static_libstdcpp);
358 set(&mut config.llvm_clean_rebuild, llvm.clean_rebuild);
359 config.llvm_targets = llvm.targets.clone();
360 config.llvm_experimental_targets = llvm.experimental_targets.clone();
361 config.llvm_link_jobs = llvm.link_jobs;
364 if let Some(ref rust) = toml.rust {
365 set(&mut config.rust_debug_assertions, rust.debug_assertions);
366 set(&mut config.rust_debuginfo, rust.debuginfo);
367 set(&mut config.rust_debuginfo_lines, rust.debuginfo_lines);
368 set(&mut config.rust_debuginfo_only_std, rust.debuginfo_only_std);
369 set(&mut config.rust_optimize, rust.optimize);
370 set(&mut config.rust_optimize_tests, rust.optimize_tests);
371 set(&mut config.rust_debuginfo_tests, rust.debuginfo_tests);
372 set(&mut config.codegen_tests, rust.codegen_tests);
373 set(&mut config.rust_rpath, rust.rpath);
374 set(&mut config.debug_jemalloc, rust.debug_jemalloc);
375 set(&mut config.use_jemalloc, rust.use_jemalloc);
376 set(&mut config.backtrace, rust.backtrace);
377 set(&mut config.channel, rust.channel.clone());
378 config.rustc_default_linker = rust.default_linker.clone();
379 config.rustc_default_ar = rust.default_ar.clone();
380 config.musl_root = rust.musl_root.clone().map(PathBuf::from);
382 match rust.codegen_units {
383 Some(0) => config.rust_codegen_units = num_cpus::get() as u32,
384 Some(n) => config.rust_codegen_units = n,
389 if let Some(ref t) = toml.target {
390 for (triple, cfg) in t {
391 let mut target = Target::default();
393 if let Some(ref s) = cfg.llvm_config {
394 target.llvm_config = Some(env::current_dir().unwrap().join(s));
396 if let Some(ref s) = cfg.jemalloc {
397 target.jemalloc = Some(env::current_dir().unwrap().join(s));
399 if let Some(ref s) = cfg.android_ndk {
400 target.ndk = Some(env::current_dir().unwrap().join(s));
402 target.cxx = cfg.cxx.clone().map(PathBuf::from);
403 target.cc = cfg.cc.clone().map(PathBuf::from);
404 target.musl_root = cfg.musl_root.clone().map(PathBuf::from);
405 target.qemu_rootfs = cfg.qemu_rootfs.clone().map(PathBuf::from);
407 config.target_config.insert(triple.clone(), target);
411 if let Some(ref t) = toml.dist {
412 config.dist_sign_folder = t.sign_folder.clone().map(PathBuf::from);
413 config.dist_gpg_password_file = t.gpg_password_file.clone().map(PathBuf::from);
414 config.dist_upload_addr = t.upload_addr.clone();
415 set(&mut config.rust_dist_src, t.src_tarball);
418 let cwd = t!(env::current_dir());
419 let out = cwd.join("build");
421 let stage0_root = out.join(&config.build).join("stage0/bin");
422 config.initial_rustc = match build.rustc {
423 Some(s) => PathBuf::from(s),
424 None => stage0_root.join(exe("rustc", &config.build)),
426 config.initial_cargo = match build.cargo {
427 Some(s) => PathBuf::from(s),
428 None => stage0_root.join(exe("cargo", &config.build)),
431 // compat with `./configure` while we're still using that
432 if fs::metadata("config.mk").is_ok() {
433 config.update_with_config_mk();
439 /// "Temporary" routine to parse `config.mk` into this configuration.
441 /// While we still have `./configure` this implements the ability to decode
442 /// that configuration into this. This isn't exactly a full-blown makefile
443 /// parser, but hey it gets the job done!
444 fn update_with_config_mk(&mut self) {
445 let mut config = String::new();
446 File::open("config.mk").unwrap().read_to_string(&mut config).unwrap();
447 for line in config.lines() {
448 let mut parts = line.splitn(2, ":=").map(|s| s.trim());
449 let key = parts.next().unwrap();
450 let value = match parts.next() {
451 Some(n) if n.starts_with('\"') => &n[1..n.len() - 1],
457 ($(($name:expr, $val:expr),)*) => {
460 if key == concat!("CFG_ENABLE_", $name) {
464 if key == concat!("CFG_DISABLE_", $name) {
474 ("MANAGE_SUBMODULES", self.submodules),
475 ("COMPILER_DOCS", self.compiler_docs),
477 ("LLVM_ASSERTIONS", self.llvm_assertions),
478 ("LLVM_RELEASE_DEBUGINFO", self.llvm_release_debuginfo),
479 ("OPTIMIZE_LLVM", self.llvm_optimize),
480 ("LLVM_VERSION_CHECK", self.llvm_version_check),
481 ("LLVM_STATIC_STDCPP", self.llvm_static_stdcpp),
482 ("LLVM_LINK_SHARED", self.llvm_link_shared),
483 ("LLVM_CLEAN_REBUILD", self.llvm_clean_rebuild),
484 ("OPTIMIZE", self.rust_optimize),
485 ("DEBUG_ASSERTIONS", self.rust_debug_assertions),
486 ("DEBUGINFO", self.rust_debuginfo),
487 ("DEBUGINFO_LINES", self.rust_debuginfo_lines),
488 ("DEBUGINFO_ONLY_STD", self.rust_debuginfo_only_std),
489 ("JEMALLOC", self.use_jemalloc),
490 ("DEBUG_JEMALLOC", self.debug_jemalloc),
491 ("RPATH", self.rust_rpath),
492 ("OPTIMIZE_TESTS", self.rust_optimize_tests),
493 ("DEBUGINFO_TESTS", self.rust_debuginfo_tests),
494 ("QUIET_TESTS", self.quiet_tests),
495 ("LOCAL_REBUILD", self.local_rebuild),
496 ("NINJA", self.ninja),
497 ("CODEGEN_TESTS", self.codegen_tests),
498 ("LOCKED_DEPS", self.locked_deps),
499 ("VENDOR", self.vendor),
500 ("FULL_BOOTSTRAP", self.full_bootstrap),
501 ("EXTENDED", self.extended),
502 ("SANITIZERS", self.sanitizers),
503 ("PROFILER", self.profiler),
504 ("DIST_SRC", self.rust_dist_src),
505 ("CARGO_OPENSSL_STATIC", self.openssl_static),
509 "CFG_BUILD" if value.len() > 0 => self.build = value.to_string(),
510 "CFG_HOST" if value.len() > 0 => {
511 self.host.extend(value.split(" ").map(|s| s.to_string()));
514 "CFG_TARGET" if value.len() > 0 => {
515 self.target.extend(value.split(" ").map(|s| s.to_string()));
517 "CFG_EXPERIMENTAL_TARGETS" if value.len() > 0 => {
518 self.llvm_experimental_targets = Some(value.to_string());
520 "CFG_MUSL_ROOT" if value.len() > 0 => {
521 self.musl_root = Some(parse_configure_path(value));
523 "CFG_MUSL_ROOT_X86_64" if value.len() > 0 => {
524 let target = "x86_64-unknown-linux-musl".to_string();
525 let target = self.target_config.entry(target)
526 .or_insert(Target::default());
527 target.musl_root = Some(parse_configure_path(value));
529 "CFG_MUSL_ROOT_I686" if value.len() > 0 => {
530 let target = "i686-unknown-linux-musl".to_string();
531 let target = self.target_config.entry(target)
532 .or_insert(Target::default());
533 target.musl_root = Some(parse_configure_path(value));
535 "CFG_MUSL_ROOT_ARM" if value.len() > 0 => {
536 let target = "arm-unknown-linux-musleabi".to_string();
537 let target = self.target_config.entry(target)
538 .or_insert(Target::default());
539 target.musl_root = Some(parse_configure_path(value));
541 "CFG_MUSL_ROOT_ARMHF" if value.len() > 0 => {
542 let target = "arm-unknown-linux-musleabihf".to_string();
543 let target = self.target_config.entry(target)
544 .or_insert(Target::default());
545 target.musl_root = Some(parse_configure_path(value));
547 "CFG_MUSL_ROOT_ARMV7" if value.len() > 0 => {
548 let target = "armv7-unknown-linux-musleabihf".to_string();
549 let target = self.target_config.entry(target)
550 .or_insert(Target::default());
551 target.musl_root = Some(parse_configure_path(value));
553 "CFG_DEFAULT_AR" if value.len() > 0 => {
554 self.rustc_default_ar = Some(value.to_string());
556 "CFG_DEFAULT_LINKER" if value.len() > 0 => {
557 self.rustc_default_linker = Some(value.to_string());
559 "CFG_GDB" if value.len() > 0 => {
560 self.gdb = Some(parse_configure_path(value));
562 "CFG_RELEASE_CHANNEL" => {
563 self.channel = value.to_string();
566 self.prefix = Some(PathBuf::from(value));
568 "CFG_SYSCONFDIR" => {
569 self.sysconfdir = Some(PathBuf::from(value));
572 self.docdir = Some(PathBuf::from(value));
575 self.bindir = Some(PathBuf::from(value));
578 self.libdir = Some(PathBuf::from(value));
580 "CFG_LIBDIR_RELATIVE" => {
581 self.libdir_relative = Some(PathBuf::from(value));
584 self.mandir = Some(PathBuf::from(value));
586 "CFG_LLVM_ROOT" if value.len() > 0 => {
587 let target = self.target_config.entry(self.build.clone())
588 .or_insert(Target::default());
589 let root = parse_configure_path(value);
590 target.llvm_config = Some(push_exe_path(root, &["bin", "llvm-config"]));
592 "CFG_JEMALLOC_ROOT" if value.len() > 0 => {
593 let target = self.target_config.entry(self.build.clone())
594 .or_insert(Target::default());
595 target.jemalloc = Some(parse_configure_path(value).join("libjemalloc_pic.a"));
597 "CFG_ARM_LINUX_ANDROIDEABI_NDK" if value.len() > 0 => {
598 let target = "arm-linux-androideabi".to_string();
599 let target = self.target_config.entry(target)
600 .or_insert(Target::default());
601 target.ndk = Some(parse_configure_path(value));
603 "CFG_ARMV7_LINUX_ANDROIDEABI_NDK" if value.len() > 0 => {
604 let target = "armv7-linux-androideabi".to_string();
605 let target = self.target_config.entry(target)
606 .or_insert(Target::default());
607 target.ndk = Some(parse_configure_path(value));
609 "CFG_I686_LINUX_ANDROID_NDK" if value.len() > 0 => {
610 let target = "i686-linux-android".to_string();
611 let target = self.target_config.entry(target)
612 .or_insert(Target::default());
613 target.ndk = Some(parse_configure_path(value));
615 "CFG_AARCH64_LINUX_ANDROID_NDK" if value.len() > 0 => {
616 let target = "aarch64-linux-android".to_string();
617 let target = self.target_config.entry(target)
618 .or_insert(Target::default());
619 target.ndk = Some(parse_configure_path(value));
621 "CFG_X86_64_LINUX_ANDROID_NDK" if value.len() > 0 => {
622 let target = "x86_64-linux-android".to_string();
623 let target = self.target_config.entry(target)
624 .or_insert(Target::default());
625 target.ndk = Some(parse_configure_path(value));
627 "CFG_LOCAL_RUST_ROOT" if value.len() > 0 => {
628 let path = parse_configure_path(value);
629 self.initial_rustc = push_exe_path(path.clone(), &["bin", "rustc"]);
630 self.initial_cargo = push_exe_path(path, &["bin", "cargo"]);
632 "CFG_PYTHON" if value.len() > 0 => {
633 let path = parse_configure_path(value);
634 self.python = Some(path);
636 "CFG_ENABLE_CCACHE" if value == "1" => {
637 self.ccache = Some(exe("ccache", &self.build));
639 "CFG_ENABLE_SCCACHE" if value == "1" => {
640 self.ccache = Some(exe("sccache", &self.build));
642 "CFG_CONFIGURE_ARGS" if value.len() > 0 => {
643 self.configure_args = value.split_whitespace()
644 .map(|s| s.to_string())
647 "CFG_QEMU_ARMHF_ROOTFS" if value.len() > 0 => {
648 let target = "arm-unknown-linux-gnueabihf".to_string();
649 let target = self.target_config.entry(target)
650 .or_insert(Target::default());
651 target.qemu_rootfs = Some(parse_configure_path(value));
658 pub fn verbose(&self) -> bool {
662 pub fn very_verbose(&self) -> bool {
668 fn parse_configure_path(path: &str) -> PathBuf {
673 fn parse_configure_path(path: &str) -> PathBuf {
674 // on windows, configure produces unix style paths e.g. /c/some/path but we
675 // only want real windows paths
677 use std::process::Command;
680 // '/' is invalid in windows paths, so we can detect unix paths by the presence of it
681 if !path.contains('/') {
685 let win_path = build_helper::output(Command::new("cygpath").arg("-w").arg(path));
686 let win_path = win_path.trim();
691 fn set<T>(field: &mut T, val: Option<T>) {
692 if let Some(v) = val {