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