]> git.lizzy.rs Git - rust.git/blob - src/bootstrap/config.rs
Separate codepaths for fat and thin LTO in write.rs
[rust.git] / src / bootstrap / config.rs
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.
4 //
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.
10
11 //! Serialized configuration of a build.
12 //!
13 //! This module implements parsing `config.toml` configuration files to tweak
14 //! how the build runs.
15
16 use std::collections::{HashMap, HashSet};
17 use std::env;
18 use std::fs::{self, File};
19 use std::io::prelude::*;
20 use std::path::{Path, PathBuf};
21 use std::process;
22 use std::cmp;
23
24 use num_cpus;
25 use toml;
26 use cache::{INTERNER, Interned};
27 use flags::Flags;
28 pub use flags::Subcommand;
29
30 /// Global configuration for the entire build and/or bootstrap.
31 ///
32 /// This structure is derived from a combination of both `config.toml` and
33 /// `config.mk`. As of the time of this writing it's unlikely that `config.toml`
34 /// is used all that much, so this is primarily filled out by `config.mk` which
35 /// is generated from `./configure`.
36 ///
37 /// Note that this structure is not decoded directly into, but rather it is
38 /// filled out from the decoded forms of the structs below. For documentation
39 /// each field, see the corresponding fields in
40 /// `config.toml.example`.
41 #[derive(Default)]
42 pub struct Config {
43     pub ccache: Option<String>,
44     pub ninja: bool,
45     pub verbose: usize,
46     pub submodules: bool,
47     pub fast_submodules: bool,
48     pub compiler_docs: bool,
49     pub docs: bool,
50     pub locked_deps: bool,
51     pub vendor: bool,
52     pub target_config: HashMap<Interned<String>, Target>,
53     pub full_bootstrap: bool,
54     pub extended: bool,
55     pub tools: Option<HashSet<String>>,
56     pub sanitizers: bool,
57     pub profiler: bool,
58     pub ignore_git: bool,
59     pub exclude: Vec<PathBuf>,
60     pub rustc_error_format: Option<String>,
61     pub test_compare_mode: bool,
62
63     pub run_host_only: bool,
64
65     pub on_fail: Option<String>,
66     pub stage: Option<u32>,
67     pub keep_stage: Vec<u32>,
68     pub src: PathBuf,
69     pub jobs: Option<u32>,
70     pub cmd: Subcommand,
71     pub incremental: bool,
72     pub dry_run: bool,
73
74     pub deny_warnings: bool,
75     pub backtrace_on_ice: bool,
76
77     // llvm codegen options
78     pub llvm_enabled: bool,
79     pub llvm_assertions: bool,
80     pub llvm_optimize: bool,
81     pub llvm_thin_lto: bool,
82     pub llvm_release_debuginfo: bool,
83     pub llvm_version_check: bool,
84     pub llvm_static_stdcpp: bool,
85     pub llvm_link_shared: bool,
86     pub llvm_clang_cl: Option<String>,
87     pub llvm_targets: Option<String>,
88     pub llvm_experimental_targets: String,
89     pub llvm_link_jobs: Option<u32>,
90     pub llvm_version_suffix: Option<String>,
91
92     pub lld_enabled: bool,
93     pub lldb_enabled: bool,
94     pub llvm_tools_enabled: bool,
95
96     // rust codegen options
97     pub rust_optimize: bool,
98     pub rust_codegen_units: Option<u32>,
99     pub rust_codegen_units_std: Option<u32>,
100     pub rust_debug_assertions: bool,
101     pub rust_debuginfo: bool,
102     pub rust_debuginfo_lines: bool,
103     pub rust_debuginfo_only_std: bool,
104     pub rust_debuginfo_tools: bool,
105     pub rust_rpath: bool,
106     pub rustc_parallel_queries: bool,
107     pub rustc_default_linker: Option<String>,
108     pub rust_optimize_tests: bool,
109     pub rust_debuginfo_tests: bool,
110     pub rust_dist_src: bool,
111     pub rust_codegen_backends: Vec<Interned<String>>,
112     pub rust_codegen_backends_dir: String,
113     pub rust_verify_llvm_ir: bool,
114     pub rust_remap_debuginfo: bool,
115
116     pub build: Interned<String>,
117     pub hosts: Vec<Interned<String>>,
118     pub targets: Vec<Interned<String>>,
119     pub local_rebuild: bool,
120     pub jemalloc: bool,
121
122     // dist misc
123     pub dist_sign_folder: Option<PathBuf>,
124     pub dist_upload_addr: Option<String>,
125     pub dist_gpg_password_file: Option<PathBuf>,
126
127     // libstd features
128     pub backtrace: bool, // support for RUST_BACKTRACE
129     pub wasm_syscall: bool,
130
131     // misc
132     pub low_priority: bool,
133     pub channel: String,
134     pub verbose_tests: bool,
135     pub test_miri: bool,
136     pub save_toolstates: Option<PathBuf>,
137     pub print_step_timings: bool,
138     pub missing_tools: bool,
139
140     // Fallback musl-root for all targets
141     pub musl_root: Option<PathBuf>,
142     pub prefix: Option<PathBuf>,
143     pub sysconfdir: Option<PathBuf>,
144     pub datadir: Option<PathBuf>,
145     pub docdir: Option<PathBuf>,
146     pub bindir: Option<PathBuf>,
147     pub libdir: Option<PathBuf>,
148     pub mandir: Option<PathBuf>,
149     pub codegen_tests: bool,
150     pub nodejs: Option<PathBuf>,
151     pub gdb: Option<PathBuf>,
152     pub python: Option<PathBuf>,
153     pub cargo_native_static: bool,
154     pub configure_args: Vec<String>,
155
156     // These are either the stage0 downloaded binaries or the locally installed ones.
157     pub initial_cargo: PathBuf,
158     pub initial_rustc: PathBuf,
159     pub out: PathBuf,
160 }
161
162 /// Per-target configuration stored in the global configuration structure.
163 #[derive(Default)]
164 pub struct Target {
165     /// Some(path to llvm-config) if using an external LLVM.
166     pub llvm_config: Option<PathBuf>,
167     /// Some(path to FileCheck) if one was specified.
168     pub llvm_filecheck: Option<PathBuf>,
169     pub cc: Option<PathBuf>,
170     pub cxx: Option<PathBuf>,
171     pub ar: Option<PathBuf>,
172     pub ranlib: Option<PathBuf>,
173     pub linker: Option<PathBuf>,
174     pub ndk: Option<PathBuf>,
175     pub crt_static: Option<bool>,
176     pub musl_root: Option<PathBuf>,
177     pub qemu_rootfs: Option<PathBuf>,
178     pub no_std: bool,
179 }
180
181 /// Structure of the `config.toml` file that configuration is read from.
182 ///
183 /// This structure uses `Decodable` to automatically decode a TOML configuration
184 /// file into this format, and then this is traversed and written into the above
185 /// `Config` structure.
186 #[derive(Deserialize, Default)]
187 #[serde(deny_unknown_fields, rename_all = "kebab-case")]
188 struct TomlConfig {
189     build: Option<Build>,
190     install: Option<Install>,
191     llvm: Option<Llvm>,
192     rust: Option<Rust>,
193     target: Option<HashMap<String, TomlTarget>>,
194     dist: Option<Dist>,
195 }
196
197 /// TOML representation of various global build decisions.
198 #[derive(Deserialize, Default, Clone)]
199 #[serde(deny_unknown_fields, rename_all = "kebab-case")]
200 struct Build {
201     build: Option<String>,
202     #[serde(default)]
203     host: Vec<String>,
204     #[serde(default)]
205     target: Vec<String>,
206     cargo: Option<String>,
207     rustc: Option<String>,
208     low_priority: Option<bool>,
209     compiler_docs: Option<bool>,
210     docs: Option<bool>,
211     submodules: Option<bool>,
212     fast_submodules: Option<bool>,
213     gdb: Option<String>,
214     locked_deps: Option<bool>,
215     vendor: Option<bool>,
216     nodejs: Option<String>,
217     python: Option<String>,
218     full_bootstrap: Option<bool>,
219     extended: Option<bool>,
220     tools: Option<HashSet<String>>,
221     verbose: Option<usize>,
222     sanitizers: Option<bool>,
223     profiler: Option<bool>,
224     cargo_native_static: Option<bool>,
225     configure_args: Option<Vec<String>>,
226     local_rebuild: Option<bool>,
227     print_step_timings: Option<bool>,
228 }
229
230 /// TOML representation of various global install decisions.
231 #[derive(Deserialize, Default, Clone)]
232 #[serde(deny_unknown_fields, rename_all = "kebab-case")]
233 struct Install {
234     prefix: Option<String>,
235     sysconfdir: Option<String>,
236     datadir: Option<String>,
237     docdir: Option<String>,
238     bindir: Option<String>,
239     libdir: Option<String>,
240     mandir: Option<String>,
241
242     // standard paths, currently unused
243     infodir: Option<String>,
244     localstatedir: Option<String>,
245 }
246
247 /// TOML representation of how the LLVM build is configured.
248 #[derive(Deserialize, Default)]
249 #[serde(deny_unknown_fields, rename_all = "kebab-case")]
250 struct Llvm {
251     enabled: Option<bool>,
252     ccache: Option<StringOrBool>,
253     ninja: Option<bool>,
254     assertions: Option<bool>,
255     optimize: Option<bool>,
256     thin_lto: Option<bool>,
257     release_debuginfo: Option<bool>,
258     version_check: Option<bool>,
259     static_libstdcpp: Option<bool>,
260     targets: Option<String>,
261     experimental_targets: Option<String>,
262     link_jobs: Option<u32>,
263     link_shared: Option<bool>,
264     version_suffix: Option<String>,
265     clang_cl: Option<String>,
266 }
267
268 #[derive(Deserialize, Default, Clone)]
269 #[serde(deny_unknown_fields, rename_all = "kebab-case")]
270 struct Dist {
271     sign_folder: Option<String>,
272     gpg_password_file: Option<String>,
273     upload_addr: Option<String>,
274     src_tarball: Option<bool>,
275     missing_tools: Option<bool>,
276 }
277
278 #[derive(Deserialize)]
279 #[serde(untagged)]
280 enum StringOrBool {
281     String(String),
282     Bool(bool),
283 }
284
285 impl Default for StringOrBool {
286     fn default() -> StringOrBool {
287         StringOrBool::Bool(false)
288     }
289 }
290
291 /// TOML representation of how the Rust build is configured.
292 #[derive(Deserialize, Default)]
293 #[serde(deny_unknown_fields, rename_all = "kebab-case")]
294 struct Rust {
295     optimize: Option<bool>,
296     codegen_units: Option<u32>,
297     codegen_units_std: Option<u32>,
298     debug_assertions: Option<bool>,
299     debuginfo: Option<bool>,
300     debuginfo_lines: Option<bool>,
301     debuginfo_only_std: Option<bool>,
302     debuginfo_tools: Option<bool>,
303     experimental_parallel_queries: Option<bool>,
304     backtrace: Option<bool>,
305     default_linker: Option<String>,
306     channel: Option<String>,
307     musl_root: Option<String>,
308     rpath: Option<bool>,
309     optimize_tests: Option<bool>,
310     debuginfo_tests: Option<bool>,
311     codegen_tests: Option<bool>,
312     ignore_git: Option<bool>,
313     debug: Option<bool>,
314     dist_src: Option<bool>,
315     verbose_tests: Option<bool>,
316     test_miri: Option<bool>,
317     incremental: Option<bool>,
318     save_toolstates: Option<String>,
319     codegen_backends: Option<Vec<String>>,
320     codegen_backends_dir: Option<String>,
321     wasm_syscall: Option<bool>,
322     lld: Option<bool>,
323     lldb: Option<bool>,
324     llvm_tools: Option<bool>,
325     deny_warnings: Option<bool>,
326     backtrace_on_ice: Option<bool>,
327     verify_llvm_ir: Option<bool>,
328     remap_debuginfo: Option<bool>,
329     jemalloc: Option<bool>,
330     test_compare_mode: Option<bool>,
331 }
332
333 /// TOML representation of how each build target is configured.
334 #[derive(Deserialize, Default)]
335 #[serde(deny_unknown_fields, rename_all = "kebab-case")]
336 struct TomlTarget {
337     llvm_config: Option<String>,
338     llvm_filecheck: Option<String>,
339     cc: Option<String>,
340     cxx: Option<String>,
341     ar: Option<String>,
342     ranlib: Option<String>,
343     linker: Option<String>,
344     android_ndk: Option<String>,
345     crt_static: Option<bool>,
346     musl_root: Option<String>,
347     qemu_rootfs: Option<String>,
348 }
349
350 impl Config {
351     fn path_from_python(var_key: &str) -> PathBuf {
352         match env::var_os(var_key) {
353             // Do not trust paths from Python and normalize them slightly (#49785).
354             Some(var_val) => Path::new(&var_val).components().collect(),
355             _ => panic!("expected '{}' to be set", var_key),
356         }
357     }
358
359     pub fn default_opts() -> Config {
360         let mut config = Config::default();
361         config.llvm_enabled = true;
362         config.llvm_optimize = true;
363         config.llvm_version_check = true;
364         config.backtrace = true;
365         config.rust_optimize = true;
366         config.rust_optimize_tests = true;
367         config.submodules = true;
368         config.fast_submodules = true;
369         config.docs = true;
370         config.rust_rpath = true;
371         config.channel = "dev".to_string();
372         config.codegen_tests = true;
373         config.ignore_git = false;
374         config.rust_dist_src = true;
375         config.test_miri = false;
376         config.rust_codegen_backends = vec![INTERNER.intern_str("llvm")];
377         config.rust_codegen_backends_dir = "codegen-backends".to_owned();
378         config.deny_warnings = true;
379         config.missing_tools = false;
380
381         // set by bootstrap.py
382         config.build = INTERNER.intern_str(&env::var("BUILD").expect("'BUILD' to be set"));
383         config.src = Config::path_from_python("SRC");
384         config.out = Config::path_from_python("BUILD_DIR");
385
386         config.initial_rustc = Config::path_from_python("RUSTC");
387         config.initial_cargo = Config::path_from_python("CARGO");
388
389         config
390     }
391
392     pub fn parse(args: &[String]) -> Config {
393         let flags = Flags::parse(&args);
394         let file = flags.config.clone();
395         let mut config = Config::default_opts();
396         config.exclude = flags.exclude;
397         config.rustc_error_format = flags.rustc_error_format;
398         config.on_fail = flags.on_fail;
399         config.stage = flags.stage;
400         config.jobs = flags.jobs;
401         config.cmd = flags.cmd;
402         config.incremental = flags.incremental;
403         config.dry_run = flags.dry_run;
404         config.keep_stage = flags.keep_stage;
405         if let Some(value) = flags.warnings {
406             config.deny_warnings = value;
407         }
408
409         if config.dry_run {
410             let dir = config.out.join("tmp-dry-run");
411             t!(fs::create_dir_all(&dir));
412             config.out = dir;
413         }
414
415         // If --target was specified but --host wasn't specified, don't run any host-only tests.
416         config.run_host_only = !(flags.host.is_empty() && !flags.target.is_empty());
417
418         let toml = file.map(|file| {
419             let mut f = t!(File::open(&file));
420             let mut contents = String::new();
421             t!(f.read_to_string(&mut contents));
422             match toml::from_str(&contents) {
423                 Ok(table) => table,
424                 Err(err) => {
425                     println!("failed to parse TOML configuration '{}': {}",
426                         file.display(), err);
427                     process::exit(2);
428                 }
429             }
430         }).unwrap_or_else(|| TomlConfig::default());
431
432         let build = toml.build.clone().unwrap_or_default();
433         // set by bootstrap.py
434         config.hosts.push(config.build.clone());
435         for host in build.host.iter() {
436             let host = INTERNER.intern_str(host);
437             if !config.hosts.contains(&host) {
438                 config.hosts.push(host);
439             }
440         }
441         for target in config.hosts.iter().cloned()
442             .chain(build.target.iter().map(|s| INTERNER.intern_str(s)))
443         {
444             if !config.targets.contains(&target) {
445                 config.targets.push(target);
446             }
447         }
448         config.hosts = if !flags.host.is_empty() {
449             flags.host
450         } else {
451             config.hosts
452         };
453         config.targets = if !flags.target.is_empty() {
454             flags.target
455         } else {
456             config.targets
457         };
458
459
460         config.nodejs = build.nodejs.map(PathBuf::from);
461         config.gdb = build.gdb.map(PathBuf::from);
462         config.python = build.python.map(PathBuf::from);
463         set(&mut config.low_priority, build.low_priority);
464         set(&mut config.compiler_docs, build.compiler_docs);
465         set(&mut config.docs, build.docs);
466         set(&mut config.submodules, build.submodules);
467         set(&mut config.fast_submodules, build.fast_submodules);
468         set(&mut config.locked_deps, build.locked_deps);
469         set(&mut config.vendor, build.vendor);
470         set(&mut config.full_bootstrap, build.full_bootstrap);
471         set(&mut config.extended, build.extended);
472         config.tools = build.tools;
473         set(&mut config.verbose, build.verbose);
474         set(&mut config.sanitizers, build.sanitizers);
475         set(&mut config.profiler, build.profiler);
476         set(&mut config.cargo_native_static, build.cargo_native_static);
477         set(&mut config.configure_args, build.configure_args);
478         set(&mut config.local_rebuild, build.local_rebuild);
479         set(&mut config.print_step_timings, build.print_step_timings);
480         config.verbose = cmp::max(config.verbose, flags.verbose);
481
482         if let Some(ref install) = toml.install {
483             config.prefix = install.prefix.clone().map(PathBuf::from);
484             config.sysconfdir = install.sysconfdir.clone().map(PathBuf::from);
485             config.datadir = install.datadir.clone().map(PathBuf::from);
486             config.docdir = install.docdir.clone().map(PathBuf::from);
487             config.bindir = install.bindir.clone().map(PathBuf::from);
488             config.libdir = install.libdir.clone().map(PathBuf::from);
489             config.mandir = install.mandir.clone().map(PathBuf::from);
490         }
491
492         // Store off these values as options because if they're not provided
493         // we'll infer default values for them later
494         let mut llvm_assertions = None;
495         let mut debuginfo_lines = None;
496         let mut debuginfo_only_std = None;
497         let mut debuginfo_tools = None;
498         let mut debug = None;
499         let mut debuginfo = None;
500         let mut debug_assertions = None;
501         let mut optimize = None;
502         let mut ignore_git = None;
503
504         if let Some(ref llvm) = toml.llvm {
505             match llvm.ccache {
506                 Some(StringOrBool::String(ref s)) => {
507                     config.ccache = Some(s.to_string())
508                 }
509                 Some(StringOrBool::Bool(true)) => {
510                     config.ccache = Some("ccache".to_string());
511                 }
512                 Some(StringOrBool::Bool(false)) | None => {}
513             }
514             set(&mut config.ninja, llvm.ninja);
515             set(&mut config.llvm_enabled, llvm.enabled);
516             llvm_assertions = llvm.assertions;
517             set(&mut config.llvm_optimize, llvm.optimize);
518             set(&mut config.llvm_thin_lto, llvm.thin_lto);
519             set(&mut config.llvm_release_debuginfo, llvm.release_debuginfo);
520             set(&mut config.llvm_version_check, llvm.version_check);
521             set(&mut config.llvm_static_stdcpp, llvm.static_libstdcpp);
522             set(&mut config.llvm_link_shared, llvm.link_shared);
523             config.llvm_targets = llvm.targets.clone();
524             config.llvm_experimental_targets = llvm.experimental_targets.clone()
525                 .unwrap_or_else(|| "WebAssembly;RISCV".to_string());
526             config.llvm_link_jobs = llvm.link_jobs;
527             config.llvm_version_suffix = llvm.version_suffix.clone();
528             config.llvm_clang_cl = llvm.clang_cl.clone();
529         }
530
531         if let Some(ref rust) = toml.rust {
532             debug = rust.debug;
533             debug_assertions = rust.debug_assertions;
534             debuginfo = rust.debuginfo;
535             debuginfo_lines = rust.debuginfo_lines;
536             debuginfo_only_std = rust.debuginfo_only_std;
537             debuginfo_tools = rust.debuginfo_tools;
538             optimize = rust.optimize;
539             ignore_git = rust.ignore_git;
540             set(&mut config.rust_optimize_tests, rust.optimize_tests);
541             set(&mut config.rust_debuginfo_tests, rust.debuginfo_tests);
542             set(&mut config.codegen_tests, rust.codegen_tests);
543             set(&mut config.rust_rpath, rust.rpath);
544             set(&mut config.jemalloc, rust.jemalloc);
545             set(&mut config.test_compare_mode, rust.test_compare_mode);
546             set(&mut config.backtrace, rust.backtrace);
547             set(&mut config.channel, rust.channel.clone());
548             set(&mut config.rust_dist_src, rust.dist_src);
549             set(&mut config.verbose_tests, rust.verbose_tests);
550             set(&mut config.test_miri, rust.test_miri);
551             // in the case "false" is set explicitly, do not overwrite the command line args
552             if let Some(true) = rust.incremental {
553                 config.incremental = true;
554             }
555             set(&mut config.wasm_syscall, rust.wasm_syscall);
556             set(&mut config.lld_enabled, rust.lld);
557             set(&mut config.lldb_enabled, rust.lldb);
558             set(&mut config.llvm_tools_enabled, rust.llvm_tools);
559             config.rustc_parallel_queries = rust.experimental_parallel_queries.unwrap_or(false);
560             config.rustc_default_linker = rust.default_linker.clone();
561             config.musl_root = rust.musl_root.clone().map(PathBuf::from);
562             config.save_toolstates = rust.save_toolstates.clone().map(PathBuf::from);
563             set(&mut config.deny_warnings, rust.deny_warnings.or(flags.warnings));
564             set(&mut config.backtrace_on_ice, rust.backtrace_on_ice);
565             set(&mut config.rust_verify_llvm_ir, rust.verify_llvm_ir);
566             set(&mut config.rust_remap_debuginfo, rust.remap_debuginfo);
567
568             if let Some(ref backends) = rust.codegen_backends {
569                 config.rust_codegen_backends = backends.iter()
570                     .map(|s| INTERNER.intern_str(s))
571                     .collect();
572             }
573
574             set(&mut config.rust_codegen_backends_dir, rust.codegen_backends_dir.clone());
575
576             match rust.codegen_units {
577                 Some(0) => config.rust_codegen_units = Some(num_cpus::get() as u32),
578                 Some(n) => config.rust_codegen_units = Some(n),
579                 None => {}
580             }
581
582             config.rust_codegen_units_std = rust.codegen_units_std;
583         }
584
585         if let Some(ref t) = toml.target {
586             for (triple, cfg) in t {
587                 let mut target = Target::default();
588
589                 if let Some(ref s) = cfg.llvm_config {
590                     target.llvm_config = Some(config.src.join(s));
591                 }
592                 if let Some(ref s) = cfg.llvm_filecheck {
593                     target.llvm_filecheck = Some(config.src.join(s));
594                 }
595                 if let Some(ref s) = cfg.android_ndk {
596                     target.ndk = Some(config.src.join(s));
597                 }
598                 target.cc = cfg.cc.clone().map(PathBuf::from);
599                 target.cxx = cfg.cxx.clone().map(PathBuf::from);
600                 target.ar = cfg.ar.clone().map(PathBuf::from);
601                 target.ranlib = cfg.ranlib.clone().map(PathBuf::from);
602                 target.linker = cfg.linker.clone().map(PathBuf::from);
603                 target.crt_static = cfg.crt_static.clone();
604                 target.musl_root = cfg.musl_root.clone().map(PathBuf::from);
605                 target.qemu_rootfs = cfg.qemu_rootfs.clone().map(PathBuf::from);
606
607                 config.target_config.insert(INTERNER.intern_string(triple.clone()), target);
608             }
609         }
610
611         if let Some(ref t) = toml.dist {
612             config.dist_sign_folder = t.sign_folder.clone().map(PathBuf::from);
613             config.dist_gpg_password_file = t.gpg_password_file.clone().map(PathBuf::from);
614             config.dist_upload_addr = t.upload_addr.clone();
615             set(&mut config.rust_dist_src, t.src_tarball);
616             set(&mut config.missing_tools, t.missing_tools);
617         }
618
619         // Now that we've reached the end of our configuration, infer the
620         // default values for all options that we haven't otherwise stored yet.
621
622         set(&mut config.initial_rustc, build.rustc.map(PathBuf::from));
623         set(&mut config.initial_cargo, build.cargo.map(PathBuf::from));
624
625         let default = false;
626         config.llvm_assertions = llvm_assertions.unwrap_or(default);
627
628         let default = true;
629         config.rust_optimize = optimize.unwrap_or(default);
630
631         let default = match &config.channel[..] {
632             "stable" | "beta" | "nightly" => true,
633             _ => false,
634         };
635         config.rust_debuginfo_lines = debuginfo_lines.unwrap_or(default);
636         config.rust_debuginfo_only_std = debuginfo_only_std.unwrap_or(default);
637         config.rust_debuginfo_tools = debuginfo_tools.unwrap_or(false);
638
639         let default = debug == Some(true);
640         config.rust_debuginfo = debuginfo.unwrap_or(default);
641         config.rust_debug_assertions = debug_assertions.unwrap_or(default);
642
643         let default = config.channel == "dev";
644         config.ignore_git = ignore_git.unwrap_or(default);
645
646         config
647     }
648
649     /// Try to find the relative path of `libdir`.
650     pub fn libdir_relative(&self) -> Option<&Path> {
651         let libdir = self.libdir.as_ref()?;
652         if libdir.is_relative() {
653             Some(libdir)
654         } else {
655             // Try to make it relative to the prefix.
656             libdir.strip_prefix(self.prefix.as_ref()?).ok()
657         }
658     }
659
660     pub fn verbose(&self) -> bool {
661         self.verbose > 0
662     }
663
664     pub fn very_verbose(&self) -> bool {
665         self.verbose > 1
666     }
667 }
668
669 fn set<T>(field: &mut T, val: Option<T>) {
670     if let Some(v) = val {
671         *field = v;
672     }
673 }