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