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