]> git.proxmox.com Git - rustc.git/blob - src/bootstrap/build/flags.rs
Imported Upstream version 1.11.0+dfsg1
[rustc.git] / src / bootstrap / build / flags.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 //! Command-line interface of the rustbuild build system.
12 //!
13 //! This module implements the command-line parsing of the build system which
14 //! has various flags to configure how it's run.
15
16 use std::fs;
17 use std::path::PathBuf;
18 use std::process;
19 use std::slice;
20
21 use getopts::Options;
22
23 /// Deserialized version of all flags for this compile.
24 pub struct Flags {
25 pub verbose: bool,
26 pub stage: Option<u32>,
27 pub build: String,
28 pub host: Filter,
29 pub target: Filter,
30 pub step: Vec<String>,
31 pub config: Option<PathBuf>,
32 pub src: Option<PathBuf>,
33 pub jobs: Option<u32>,
34 pub args: Vec<String>,
35 pub clean: bool,
36 }
37
38 pub struct Filter {
39 values: Vec<String>,
40 }
41
42 impl Flags {
43 pub fn parse(args: &[String]) -> Flags {
44 let mut opts = Options::new();
45 opts.optflag("v", "verbose", "use verbose output");
46 opts.optopt("", "config", "TOML configuration file for build", "FILE");
47 opts.optmulti("", "host", "host targets to build", "HOST");
48 opts.reqopt("", "build", "build target of the stage0 compiler", "BUILD");
49 opts.optmulti("", "target", "targets to build", "TARGET");
50 opts.optmulti("s", "step", "build step to execute", "STEP");
51 opts.optopt("", "stage", "stage to build", "N");
52 opts.optopt("", "src", "path to repo root", "DIR");
53 opts.optopt("j", "jobs", "number of jobs to run in parallel", "JOBS");
54 opts.optflag("", "clean", "clean output directory");
55 opts.optflag("h", "help", "print this help message");
56
57 let usage = |n| -> ! {
58 let brief = format!("Usage: rust.py [options]");
59 print!("{}", opts.usage(&brief));
60 process::exit(n);
61 };
62
63 let m = opts.parse(args).unwrap_or_else(|e| {
64 println!("failed to parse options: {}", e);
65 usage(1);
66 });
67 if m.opt_present("h") {
68 usage(0);
69 }
70
71 let cfg_file = m.opt_str("config").map(PathBuf::from).or_else(|| {
72 if fs::metadata("config.toml").is_ok() {
73 Some(PathBuf::from("config.toml"))
74 } else {
75 None
76 }
77 });
78
79 Flags {
80 verbose: m.opt_present("v"),
81 clean: m.opt_present("clean"),
82 stage: m.opt_str("stage").map(|j| j.parse().unwrap()),
83 build: m.opt_str("build").unwrap(),
84 host: Filter { values: m.opt_strs("host") },
85 target: Filter { values: m.opt_strs("target") },
86 step: m.opt_strs("step"),
87 config: cfg_file,
88 src: m.opt_str("src").map(PathBuf::from),
89 jobs: m.opt_str("jobs").map(|j| j.parse().unwrap()),
90 args: m.free.clone(),
91 }
92 }
93 }
94
95 impl Filter {
96 pub fn contains(&self, name: &str) -> bool {
97 self.values.len() == 0 || self.values.iter().any(|s| s == name)
98 }
99
100 pub fn iter(&self) -> slice::Iter<String> {
101 self.values.iter()
102 }
103 }