]> git.lizzy.rs Git - rust.git/blob - src/bootstrap/config.rs
Add an unstable FileTypeExt extension trait for Windows
[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: String,
79     pub llvm_link_jobs: Option<u32>,
80
81     // rust codegen options
82     pub rust_optimize: bool,
83     pub rust_codegen_units: Option<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_parallel_queries: 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     pub rust_codegen_backends: Vec<Interned<String>>,
95
96     pub build: Interned<String>,
97     pub hosts: Vec<Interned<String>>,
98     pub targets: Vec<Interned<String>>,
99     pub local_rebuild: bool,
100
101     // dist misc
102     pub dist_sign_folder: Option<PathBuf>,
103     pub dist_upload_addr: Option<String>,
104     pub dist_gpg_password_file: Option<PathBuf>,
105
106     // libstd features
107     pub debug_jemalloc: bool,
108     pub use_jemalloc: bool,
109     pub backtrace: bool, // support for RUST_BACKTRACE
110
111     // misc
112     pub low_priority: bool,
113     pub channel: String,
114     pub quiet_tests: bool,
115     pub test_miri: bool,
116     pub save_toolstates: Option<PathBuf>,
117
118     // Fallback musl-root for all targets
119     pub musl_root: Option<PathBuf>,
120     pub prefix: Option<PathBuf>,
121     pub sysconfdir: Option<PathBuf>,
122     pub docdir: Option<PathBuf>,
123     pub bindir: Option<PathBuf>,
124     pub libdir: Option<PathBuf>,
125     pub mandir: Option<PathBuf>,
126     pub codegen_tests: bool,
127     pub nodejs: Option<PathBuf>,
128     pub gdb: Option<PathBuf>,
129     pub python: Option<PathBuf>,
130     pub openssl_static: bool,
131     pub configure_args: Vec<String>,
132
133     // These are either the stage0 downloaded binaries or the locally installed ones.
134     pub initial_cargo: PathBuf,
135     pub initial_rustc: PathBuf,
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     // standard paths, currently unused
212     datadir: Option<String>,
213     infodir: Option<String>,
214     localstatedir: Option<String>,
215 }
216
217 /// TOML representation of how the LLVM build is configured.
218 #[derive(Deserialize, Default)]
219 #[serde(deny_unknown_fields, rename_all = "kebab-case")]
220 struct Llvm {
221     enabled: Option<bool>,
222     ccache: Option<StringOrBool>,
223     ninja: Option<bool>,
224     assertions: Option<bool>,
225     optimize: Option<bool>,
226     release_debuginfo: Option<bool>,
227     version_check: Option<bool>,
228     static_libstdcpp: Option<bool>,
229     targets: Option<String>,
230     experimental_targets: Option<String>,
231     link_jobs: Option<u32>,
232     link_shared: Option<bool>,
233 }
234
235 #[derive(Deserialize, Default, Clone)]
236 #[serde(deny_unknown_fields, rename_all = "kebab-case")]
237 struct Dist {
238     sign_folder: Option<String>,
239     gpg_password_file: Option<String>,
240     upload_addr: Option<String>,
241     src_tarball: Option<bool>,
242 }
243
244 #[derive(Deserialize)]
245 #[serde(untagged)]
246 enum StringOrBool {
247     String(String),
248     Bool(bool),
249 }
250
251 impl Default for StringOrBool {
252     fn default() -> StringOrBool {
253         StringOrBool::Bool(false)
254     }
255 }
256
257 /// TOML representation of how the Rust build is configured.
258 #[derive(Deserialize, Default)]
259 #[serde(deny_unknown_fields, rename_all = "kebab-case")]
260 struct Rust {
261     optimize: Option<bool>,
262     codegen_units: Option<u32>,
263     debug_assertions: Option<bool>,
264     debuginfo: Option<bool>,
265     debuginfo_lines: Option<bool>,
266     debuginfo_only_std: Option<bool>,
267     experimental_parallel_queries: Option<bool>,
268     debug_jemalloc: Option<bool>,
269     use_jemalloc: Option<bool>,
270     backtrace: Option<bool>,
271     default_linker: Option<String>,
272     channel: Option<String>,
273     musl_root: Option<String>,
274     rpath: Option<bool>,
275     optimize_tests: Option<bool>,
276     debuginfo_tests: Option<bool>,
277     codegen_tests: Option<bool>,
278     ignore_git: Option<bool>,
279     debug: Option<bool>,
280     dist_src: Option<bool>,
281     quiet_tests: Option<bool>,
282     test_miri: Option<bool>,
283     save_toolstates: Option<String>,
284     codegen_backends: Option<Vec<String>>,
285 }
286
287 /// TOML representation of how each build target is configured.
288 #[derive(Deserialize, Default)]
289 #[serde(deny_unknown_fields, rename_all = "kebab-case")]
290 struct TomlTarget {
291     llvm_config: Option<String>,
292     jemalloc: Option<String>,
293     cc: Option<String>,
294     cxx: Option<String>,
295     ar: Option<String>,
296     linker: Option<String>,
297     android_ndk: Option<String>,
298     crt_static: Option<bool>,
299     musl_root: Option<String>,
300     qemu_rootfs: Option<String>,
301 }
302
303 impl Config {
304     pub fn parse(args: &[String]) -> Config {
305         let flags = Flags::parse(&args);
306         let file = flags.config.clone();
307         let mut config = Config::default();
308         config.llvm_enabled = true;
309         config.llvm_optimize = true;
310         config.llvm_version_check = true;
311         config.use_jemalloc = true;
312         config.backtrace = true;
313         config.rust_optimize = true;
314         config.rust_optimize_tests = true;
315         config.submodules = true;
316         config.docs = true;
317         config.rust_rpath = true;
318         config.channel = "dev".to_string();
319         config.codegen_tests = true;
320         config.ignore_git = false;
321         config.rust_dist_src = true;
322         config.test_miri = false;
323         config.rust_codegen_backends = vec![INTERNER.intern_str("llvm")];
324
325         config.on_fail = flags.on_fail;
326         config.stage = flags.stage;
327         config.src = flags.src;
328         config.jobs = flags.jobs;
329         config.cmd = flags.cmd;
330         config.incremental = flags.incremental;
331         config.keep_stage = flags.keep_stage;
332
333         // If --target was specified but --host wasn't specified, don't run any host-only tests.
334         config.run_host_only = flags.host.is_empty() && !flags.target.is_empty();
335
336         let toml = file.map(|file| {
337             let mut f = t!(File::open(&file));
338             let mut contents = String::new();
339             t!(f.read_to_string(&mut contents));
340             match toml::from_str(&contents) {
341                 Ok(table) => table,
342                 Err(err) => {
343                     println!("failed to parse TOML configuration '{}': {}",
344                         file.display(), err);
345                     process::exit(2);
346                 }
347             }
348         }).unwrap_or_else(|| TomlConfig::default());
349
350         let build = toml.build.clone().unwrap_or(Build::default());
351         set(&mut config.build, build.build.clone().map(|x| INTERNER.intern_string(x)));
352         set(&mut config.build, flags.build);
353         if config.build.is_empty() {
354             // set by bootstrap.py
355             config.build = INTERNER.intern_str(&env::var("BUILD").unwrap());
356         }
357         config.hosts.push(config.build.clone());
358         for host in build.host.iter() {
359             let host = INTERNER.intern_str(host);
360             if !config.hosts.contains(&host) {
361                 config.hosts.push(host);
362             }
363         }
364         for target in config.hosts.iter().cloned()
365             .chain(build.target.iter().map(|s| INTERNER.intern_str(s)))
366         {
367             if !config.targets.contains(&target) {
368                 config.targets.push(target);
369             }
370         }
371         config.hosts = if !flags.host.is_empty() {
372             flags.host
373         } else {
374             config.hosts
375         };
376         config.targets = if !flags.target.is_empty() {
377             flags.target
378         } else {
379             config.targets
380         };
381
382
383         config.nodejs = build.nodejs.map(PathBuf::from);
384         config.gdb = build.gdb.map(PathBuf::from);
385         config.python = build.python.map(PathBuf::from);
386         set(&mut config.low_priority, build.low_priority);
387         set(&mut config.compiler_docs, build.compiler_docs);
388         set(&mut config.docs, build.docs);
389         set(&mut config.submodules, build.submodules);
390         set(&mut config.locked_deps, build.locked_deps);
391         set(&mut config.vendor, build.vendor);
392         set(&mut config.full_bootstrap, build.full_bootstrap);
393         set(&mut config.extended, build.extended);
394         set(&mut config.verbose, build.verbose);
395         set(&mut config.sanitizers, build.sanitizers);
396         set(&mut config.profiler, build.profiler);
397         set(&mut config.openssl_static, build.openssl_static);
398         set(&mut config.configure_args, build.configure_args);
399         set(&mut config.local_rebuild, build.local_rebuild);
400         config.verbose = cmp::max(config.verbose, flags.verbose);
401
402         if let Some(ref install) = toml.install {
403             config.prefix = install.prefix.clone().map(PathBuf::from);
404             config.sysconfdir = install.sysconfdir.clone().map(PathBuf::from);
405             config.docdir = install.docdir.clone().map(PathBuf::from);
406             config.bindir = install.bindir.clone().map(PathBuf::from);
407             config.libdir = install.libdir.clone().map(PathBuf::from);
408             config.mandir = install.mandir.clone().map(PathBuf::from);
409         }
410
411         // Store off these values as options because if they're not provided
412         // we'll infer default values for them later
413         let mut llvm_assertions = None;
414         let mut debuginfo_lines = None;
415         let mut debuginfo_only_std = None;
416         let mut debug = None;
417         let mut debug_jemalloc = None;
418         let mut debuginfo = None;
419         let mut debug_assertions = None;
420         let mut optimize = None;
421         let mut ignore_git = None;
422
423         if let Some(ref llvm) = toml.llvm {
424             match llvm.ccache {
425                 Some(StringOrBool::String(ref s)) => {
426                     config.ccache = Some(s.to_string())
427                 }
428                 Some(StringOrBool::Bool(true)) => {
429                     config.ccache = Some("ccache".to_string());
430                 }
431                 Some(StringOrBool::Bool(false)) | None => {}
432             }
433             set(&mut config.ninja, llvm.ninja);
434             set(&mut config.llvm_enabled, llvm.enabled);
435             llvm_assertions = llvm.assertions;
436             set(&mut config.llvm_optimize, llvm.optimize);
437             set(&mut config.llvm_release_debuginfo, llvm.release_debuginfo);
438             set(&mut config.llvm_version_check, llvm.version_check);
439             set(&mut config.llvm_static_stdcpp, llvm.static_libstdcpp);
440             set(&mut config.llvm_link_shared, llvm.link_shared);
441             config.llvm_targets = llvm.targets.clone();
442             config.llvm_experimental_targets = llvm.experimental_targets.clone()
443                 .unwrap_or("WebAssembly".to_string());
444             config.llvm_link_jobs = llvm.link_jobs;
445         }
446
447         if let Some(ref rust) = toml.rust {
448             debug = rust.debug;
449             debug_assertions = rust.debug_assertions;
450             debuginfo = rust.debuginfo;
451             debuginfo_lines = rust.debuginfo_lines;
452             debuginfo_only_std = rust.debuginfo_only_std;
453             optimize = rust.optimize;
454             ignore_git = rust.ignore_git;
455             debug_jemalloc = rust.debug_jemalloc;
456             set(&mut config.rust_optimize_tests, rust.optimize_tests);
457             set(&mut config.rust_debuginfo_tests, rust.debuginfo_tests);
458             set(&mut config.codegen_tests, rust.codegen_tests);
459             set(&mut config.rust_rpath, rust.rpath);
460             set(&mut config.use_jemalloc, rust.use_jemalloc);
461             set(&mut config.backtrace, rust.backtrace);
462             set(&mut config.channel, rust.channel.clone());
463             set(&mut config.rust_dist_src, rust.dist_src);
464             set(&mut config.quiet_tests, rust.quiet_tests);
465             set(&mut config.test_miri, rust.test_miri);
466             config.rustc_parallel_queries = rust.experimental_parallel_queries.unwrap_or(false);
467             config.rustc_default_linker = rust.default_linker.clone();
468             config.musl_root = rust.musl_root.clone().map(PathBuf::from);
469             config.save_toolstates = rust.save_toolstates.clone().map(PathBuf::from);
470
471             if let Some(ref backends) = rust.codegen_backends {
472                 config.rust_codegen_backends = backends.iter()
473                     .map(|s| INTERNER.intern_str(s))
474                     .collect();
475             }
476
477             match rust.codegen_units {
478                 Some(0) => config.rust_codegen_units = Some(num_cpus::get() as u32),
479                 Some(n) => config.rust_codegen_units = Some(n),
480                 None => {}
481             }
482         }
483
484         if let Some(ref t) = toml.target {
485             for (triple, cfg) in t {
486                 let mut target = Target::default();
487
488                 if let Some(ref s) = cfg.llvm_config {
489                     target.llvm_config = Some(env::current_dir().unwrap().join(s));
490                 }
491                 if let Some(ref s) = cfg.jemalloc {
492                     target.jemalloc = Some(env::current_dir().unwrap().join(s));
493                 }
494                 if let Some(ref s) = cfg.android_ndk {
495                     target.ndk = Some(env::current_dir().unwrap().join(s));
496                 }
497                 target.cc = cfg.cc.clone().map(PathBuf::from);
498                 target.cxx = cfg.cxx.clone().map(PathBuf::from);
499                 target.ar = cfg.ar.clone().map(PathBuf::from);
500                 target.linker = cfg.linker.clone().map(PathBuf::from);
501                 target.crt_static = cfg.crt_static.clone();
502                 target.musl_root = cfg.musl_root.clone().map(PathBuf::from);
503                 target.qemu_rootfs = cfg.qemu_rootfs.clone().map(PathBuf::from);
504
505                 config.target_config.insert(INTERNER.intern_string(triple.clone()), target);
506             }
507         }
508
509         if let Some(ref t) = toml.dist {
510             config.dist_sign_folder = t.sign_folder.clone().map(PathBuf::from);
511             config.dist_gpg_password_file = t.gpg_password_file.clone().map(PathBuf::from);
512             config.dist_upload_addr = t.upload_addr.clone();
513             set(&mut config.rust_dist_src, t.src_tarball);
514         }
515
516         let cwd = t!(env::current_dir());
517         let out = cwd.join("build");
518
519         let stage0_root = out.join(&config.build).join("stage0/bin");
520         config.initial_rustc = match build.rustc {
521             Some(s) => PathBuf::from(s),
522             None => stage0_root.join(exe("rustc", &config.build)),
523         };
524         config.initial_cargo = match build.cargo {
525             Some(s) => PathBuf::from(s),
526             None => stage0_root.join(exe("cargo", &config.build)),
527         };
528
529         // Now that we've reached the end of our configuration, infer the
530         // default values for all options that we haven't otherwise stored yet.
531
532         let default = false;
533         config.llvm_assertions = llvm_assertions.unwrap_or(default);
534
535         let default = match &config.channel[..] {
536             "stable" | "beta" | "nightly" => true,
537             _ => false,
538         };
539         config.rust_debuginfo_lines = debuginfo_lines.unwrap_or(default);
540         config.rust_debuginfo_only_std = debuginfo_only_std.unwrap_or(default);
541
542         let default = debug == Some(true);
543         config.debug_jemalloc = debug_jemalloc.unwrap_or(default);
544         config.rust_debuginfo = debuginfo.unwrap_or(default);
545         config.rust_debug_assertions = debug_assertions.unwrap_or(default);
546         config.rust_optimize = optimize.unwrap_or(!default);
547
548         let default = config.channel == "dev";
549         config.ignore_git = ignore_git.unwrap_or(default);
550
551         config
552     }
553
554     pub fn verbose(&self) -> bool {
555         self.verbose > 0
556     }
557
558     pub fn very_verbose(&self) -> bool {
559         self.verbose > 1
560     }
561 }
562
563 fn set<T>(field: &mut T, val: Option<T>) {
564     if let Some(v) = val {
565         *field = v;
566     }
567 }