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