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