2015-11-20 00:20:12 +01:00
|
|
|
// Copyright 2015 The Rust Project Developers. See the COPYRIGHT
|
|
|
|
// file at the top-level directory of this distribution and at
|
|
|
|
// http://rust-lang.org/COPYRIGHT.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
|
|
|
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
|
|
|
// option. This file may not be copied, modified, or distributed
|
|
|
|
// except according to those terms.
|
|
|
|
|
2016-05-03 00:16:15 +02:00
|
|
|
//! Command-line interface of the rustbuild build system.
|
|
|
|
//!
|
|
|
|
//! This module implements the command-line parsing of the build system which
|
|
|
|
//! has various flags to configure how it's run.
|
|
|
|
|
2016-10-21 22:18:09 +02:00
|
|
|
use std::env;
|
2015-11-20 00:20:12 +01:00
|
|
|
use std::fs;
|
|
|
|
use std::path::PathBuf;
|
|
|
|
use std::process;
|
|
|
|
|
2016-10-21 22:18:09 +02:00
|
|
|
use getopts::{Matches, Options};
|
|
|
|
|
|
|
|
use Build;
|
|
|
|
use config::Config;
|
|
|
|
use metadata;
|
|
|
|
use step;
|
2015-11-20 00:20:12 +01:00
|
|
|
|
2016-05-03 00:16:15 +02:00
|
|
|
/// Deserialized version of all flags for this compile.
|
2015-11-20 00:20:12 +01:00
|
|
|
pub struct Flags {
|
2016-11-17 00:02:56 +01:00
|
|
|
pub verbose: usize, // verbosity level: 0 == not verbose, 1 == verbose, 2 == very verbose
|
2017-02-16 19:52:56 +01:00
|
|
|
pub on_fail: Option<String>,
|
2015-11-20 00:20:12 +01:00
|
|
|
pub stage: Option<u32>,
|
2016-12-12 23:21:42 +01:00
|
|
|
pub keep_stage: Option<u32>,
|
2015-11-20 00:20:12 +01:00
|
|
|
pub build: String,
|
2016-10-21 22:18:09 +02:00
|
|
|
pub host: Vec<String>,
|
|
|
|
pub target: Vec<String>,
|
2015-11-20 00:20:12 +01:00
|
|
|
pub config: Option<PathBuf>,
|
|
|
|
pub src: Option<PathBuf>,
|
|
|
|
pub jobs: Option<u32>,
|
2016-10-21 22:18:09 +02:00
|
|
|
pub cmd: Subcommand,
|
2016-11-17 00:02:56 +01:00
|
|
|
pub incremental: bool,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Flags {
|
|
|
|
pub fn verbose(&self) -> bool {
|
|
|
|
self.verbose > 0
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn very_verbose(&self) -> bool {
|
|
|
|
self.verbose > 1
|
|
|
|
}
|
2015-11-20 00:20:12 +01:00
|
|
|
}
|
|
|
|
|
2016-10-21 22:18:09 +02:00
|
|
|
pub enum Subcommand {
|
|
|
|
Build {
|
|
|
|
paths: Vec<PathBuf>,
|
|
|
|
},
|
|
|
|
Doc {
|
|
|
|
paths: Vec<PathBuf>,
|
|
|
|
},
|
|
|
|
Test {
|
|
|
|
paths: Vec<PathBuf>,
|
|
|
|
test_args: Vec<String>,
|
|
|
|
},
|
2016-11-25 22:13:59 +01:00
|
|
|
Bench {
|
|
|
|
paths: Vec<PathBuf>,
|
|
|
|
test_args: Vec<String>,
|
|
|
|
},
|
2016-10-21 22:18:09 +02:00
|
|
|
Clean,
|
|
|
|
Dist {
|
2017-01-01 02:42:40 +01:00
|
|
|
paths: Vec<PathBuf>,
|
2016-10-21 22:18:09 +02:00
|
|
|
install: bool,
|
|
|
|
},
|
2015-11-20 00:20:12 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Flags {
|
|
|
|
pub fn parse(args: &[String]) -> Flags {
|
|
|
|
let mut opts = Options::new();
|
2016-11-17 00:02:56 +01:00
|
|
|
opts.optflagmulti("v", "verbose", "use verbose output (-vv for very verbose)");
|
|
|
|
opts.optflag("i", "incremental", "use incremental compilation");
|
2015-11-20 00:20:12 +01:00
|
|
|
opts.optopt("", "config", "TOML configuration file for build", "FILE");
|
2016-10-21 22:18:09 +02:00
|
|
|
opts.optopt("", "build", "build target of the stage0 compiler", "BUILD");
|
2015-11-20 00:20:12 +01:00
|
|
|
opts.optmulti("", "host", "host targets to build", "HOST");
|
2016-10-21 22:18:09 +02:00
|
|
|
opts.optmulti("", "target", "target targets to build", "TARGET");
|
2017-02-16 19:52:56 +01:00
|
|
|
opts.optopt("", "on-fail", "command to run on failure", "CMD");
|
2015-11-20 00:20:12 +01:00
|
|
|
opts.optopt("", "stage", "stage to build", "N");
|
2016-12-12 23:21:42 +01:00
|
|
|
opts.optopt("", "keep-stage", "stage to keep without recompiling", "N");
|
2016-10-21 22:18:09 +02:00
|
|
|
opts.optopt("", "src", "path to the root of the rust checkout", "DIR");
|
2015-11-20 00:20:12 +01:00
|
|
|
opts.optopt("j", "jobs", "number of jobs to run in parallel", "JOBS");
|
|
|
|
opts.optflag("h", "help", "print this help message");
|
|
|
|
|
2016-10-21 22:18:09 +02:00
|
|
|
let usage = |n, opts: &Options| -> ! {
|
2017-03-31 04:49:06 +02:00
|
|
|
let subcommand = args.get(0).map(|s| &**s);
|
|
|
|
let brief = format!("Usage: x.py <subcommand> [options] [<args>...]");
|
2016-10-21 22:18:09 +02:00
|
|
|
|
|
|
|
println!("{}", opts.usage(&brief));
|
2017-03-31 04:49:06 +02:00
|
|
|
match subcommand {
|
2016-10-21 22:18:09 +02:00
|
|
|
Some("build") => {
|
|
|
|
println!("\
|
|
|
|
Arguments:
|
|
|
|
This subcommand accepts a number of positional arguments of directories to
|
|
|
|
the crates and/or artifacts to compile. For example:
|
|
|
|
|
|
|
|
./x.py build src/libcore
|
|
|
|
./x.py build src/libproc_macro
|
|
|
|
./x.py build src/libstd --stage 1
|
|
|
|
|
|
|
|
If no arguments are passed then the complete artifacts for that stage are
|
|
|
|
also compiled.
|
|
|
|
|
|
|
|
./x.py build
|
|
|
|
./x.py build --stage 1
|
|
|
|
|
|
|
|
For a quick build with a usable compile, you can pass:
|
|
|
|
|
|
|
|
./x.py build --stage 1 src/libtest
|
|
|
|
");
|
|
|
|
}
|
|
|
|
|
|
|
|
Some("test") => {
|
|
|
|
println!("\
|
|
|
|
Arguments:
|
|
|
|
This subcommand accepts a number of positional arguments of directories to
|
|
|
|
tests that should be compiled and run. For example:
|
|
|
|
|
|
|
|
./x.py test src/test/run-pass
|
|
|
|
./x.py test src/libstd --test-args hash_map
|
|
|
|
./x.py test src/libstd --stage 0
|
|
|
|
|
|
|
|
If no arguments are passed then the complete artifacts for that stage are
|
|
|
|
compiled and tested.
|
|
|
|
|
|
|
|
./x.py test
|
|
|
|
./x.py test --stage 1
|
|
|
|
");
|
|
|
|
}
|
|
|
|
|
|
|
|
Some("doc") => {
|
|
|
|
println!("\
|
|
|
|
Arguments:
|
|
|
|
This subcommand accepts a number of positional arguments of directories of
|
|
|
|
documentation to build. For example:
|
|
|
|
|
|
|
|
./x.py doc src/doc/book
|
|
|
|
./x.py doc src/doc/nomicon
|
|
|
|
./x.py doc src/libstd
|
|
|
|
|
|
|
|
If no arguments are passed then everything is documented:
|
|
|
|
|
|
|
|
./x.py doc
|
|
|
|
./x.py doc --stage 1
|
|
|
|
");
|
|
|
|
}
|
|
|
|
|
|
|
|
_ => {}
|
|
|
|
}
|
|
|
|
|
2017-03-31 04:49:06 +02:00
|
|
|
if let Some(subcommand) = subcommand {
|
|
|
|
if subcommand == "build" ||
|
|
|
|
subcommand == "dist" ||
|
|
|
|
subcommand == "doc" ||
|
|
|
|
subcommand == "test" ||
|
|
|
|
subcommand == "bench" ||
|
|
|
|
subcommand == "clean" {
|
2016-10-21 22:18:09 +02:00
|
|
|
println!("Available invocations:");
|
|
|
|
if args.iter().any(|a| a == "-v") {
|
|
|
|
let flags = Flags::parse(&["build".to_string()]);
|
|
|
|
let mut config = Config::default();
|
|
|
|
config.build = flags.build.clone();
|
|
|
|
let mut build = Build::new(flags, config);
|
|
|
|
metadata::build(&mut build);
|
2017-03-31 04:49:06 +02:00
|
|
|
step::build_rules(&build).print_help(subcommand);
|
2016-10-21 22:18:09 +02:00
|
|
|
} else {
|
|
|
|
println!(" ... elided, run `./x.py {} -h -v` to see",
|
2017-03-31 04:49:06 +02:00
|
|
|
subcommand);
|
2016-10-21 22:18:09 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
println!("");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
println!("\
|
|
|
|
Subcommands:
|
|
|
|
build Compile either the compiler or libraries
|
|
|
|
test Build and run some test suites
|
2016-11-25 22:13:59 +01:00
|
|
|
bench Build and run some benchmarks
|
2016-10-21 22:18:09 +02:00
|
|
|
doc Build documentation
|
|
|
|
clean Clean out build directories
|
|
|
|
dist Build and/or install distribution artifacts
|
|
|
|
|
2017-03-31 04:49:06 +02:00
|
|
|
To learn more about a subcommand, run `./x.py <subcommand> -h`
|
2016-10-21 22:18:09 +02:00
|
|
|
");
|
|
|
|
|
2015-11-20 00:20:12 +01:00
|
|
|
process::exit(n);
|
|
|
|
};
|
2016-10-21 22:18:09 +02:00
|
|
|
if args.len() == 0 {
|
2017-03-31 04:49:06 +02:00
|
|
|
println!("a subcommand must be passed");
|
2016-10-21 22:18:09 +02:00
|
|
|
usage(1, &opts);
|
2015-11-20 00:20:12 +01:00
|
|
|
}
|
2016-10-21 22:18:09 +02:00
|
|
|
let parse = |opts: &Options| {
|
|
|
|
let m = opts.parse(&args[1..]).unwrap_or_else(|e| {
|
|
|
|
println!("failed to parse options: {}", e);
|
|
|
|
usage(1, opts);
|
|
|
|
});
|
|
|
|
if m.opt_present("h") {
|
|
|
|
usage(0, opts);
|
|
|
|
}
|
|
|
|
return m
|
|
|
|
};
|
|
|
|
|
|
|
|
let cwd = t!(env::current_dir());
|
|
|
|
let remaining_as_path = |m: &Matches| {
|
|
|
|
m.free.iter().map(|p| cwd.join(p)).collect::<Vec<_>>()
|
|
|
|
};
|
|
|
|
|
|
|
|
let m: Matches;
|
|
|
|
let cmd = match &args[0][..] {
|
|
|
|
"build" => {
|
|
|
|
m = parse(&opts);
|
|
|
|
Subcommand::Build { paths: remaining_as_path(&m) }
|
|
|
|
}
|
|
|
|
"doc" => {
|
|
|
|
m = parse(&opts);
|
|
|
|
Subcommand::Doc { paths: remaining_as_path(&m) }
|
|
|
|
}
|
|
|
|
"test" => {
|
|
|
|
opts.optmulti("", "test-args", "extra arguments", "ARGS");
|
|
|
|
m = parse(&opts);
|
|
|
|
Subcommand::Test {
|
|
|
|
paths: remaining_as_path(&m),
|
|
|
|
test_args: m.opt_strs("test-args"),
|
|
|
|
}
|
|
|
|
}
|
2016-11-25 22:13:59 +01:00
|
|
|
"bench" => {
|
|
|
|
opts.optmulti("", "test-args", "extra arguments", "ARGS");
|
|
|
|
m = parse(&opts);
|
|
|
|
Subcommand::Bench {
|
|
|
|
paths: remaining_as_path(&m),
|
|
|
|
test_args: m.opt_strs("test-args"),
|
|
|
|
}
|
|
|
|
}
|
2016-10-21 22:18:09 +02:00
|
|
|
"clean" => {
|
|
|
|
m = parse(&opts);
|
|
|
|
if m.free.len() > 0 {
|
|
|
|
println!("clean takes no arguments");
|
|
|
|
usage(1, &opts);
|
|
|
|
}
|
|
|
|
Subcommand::Clean
|
|
|
|
}
|
|
|
|
"dist" => {
|
|
|
|
opts.optflag("", "install", "run installer as well");
|
|
|
|
m = parse(&opts);
|
|
|
|
Subcommand::Dist {
|
2017-01-01 02:42:40 +01:00
|
|
|
paths: remaining_as_path(&m),
|
2016-10-21 22:18:09 +02:00
|
|
|
install: m.opt_present("install"),
|
|
|
|
}
|
|
|
|
}
|
2016-11-16 21:31:19 +01:00
|
|
|
"--help" => usage(0, &opts),
|
2016-10-21 22:18:09 +02:00
|
|
|
cmd => {
|
2017-03-31 04:49:06 +02:00
|
|
|
println!("unknown subcommand: {}", cmd);
|
2016-10-21 22:18:09 +02:00
|
|
|
usage(1, &opts);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2015-11-20 00:20:12 +01:00
|
|
|
|
|
|
|
let cfg_file = m.opt_str("config").map(PathBuf::from).or_else(|| {
|
|
|
|
if fs::metadata("config.toml").is_ok() {
|
|
|
|
Some(PathBuf::from("config.toml"))
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2016-11-17 00:02:56 +01:00
|
|
|
let mut stage = m.opt_str("stage").map(|j| j.parse().unwrap());
|
|
|
|
|
|
|
|
let incremental = m.opt_present("i");
|
|
|
|
|
|
|
|
if incremental {
|
|
|
|
if stage.is_none() {
|
|
|
|
stage = Some(1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-11-20 00:20:12 +01:00
|
|
|
Flags {
|
2016-11-17 00:02:56 +01:00
|
|
|
verbose: m.opt_count("v"),
|
2016-12-20 18:38:13 +01:00
|
|
|
stage: stage,
|
2017-02-16 19:52:56 +01:00
|
|
|
on_fail: m.opt_str("on-fail"),
|
2016-12-12 23:21:42 +01:00
|
|
|
keep_stage: m.opt_str("keep-stage").map(|j| j.parse().unwrap()),
|
2016-10-21 22:18:09 +02:00
|
|
|
build: m.opt_str("build").unwrap_or_else(|| {
|
|
|
|
env::var("BUILD").unwrap()
|
|
|
|
}),
|
2017-01-16 23:15:12 +01:00
|
|
|
host: split(m.opt_strs("host")),
|
|
|
|
target: split(m.opt_strs("target")),
|
2015-11-20 00:20:12 +01:00
|
|
|
config: cfg_file,
|
|
|
|
src: m.opt_str("src").map(PathBuf::from),
|
|
|
|
jobs: m.opt_str("jobs").map(|j| j.parse().unwrap()),
|
2016-10-21 22:18:09 +02:00
|
|
|
cmd: cmd,
|
2016-11-17 00:02:56 +01:00
|
|
|
incremental: incremental,
|
2015-11-20 00:20:12 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-21 22:18:09 +02:00
|
|
|
impl Subcommand {
|
|
|
|
pub fn test_args(&self) -> Vec<&str> {
|
|
|
|
match *self {
|
2016-11-25 22:13:59 +01:00
|
|
|
Subcommand::Test { ref test_args, .. } |
|
|
|
|
Subcommand::Bench { ref test_args, .. } => {
|
2016-10-21 22:18:09 +02:00
|
|
|
test_args.iter().flat_map(|s| s.split_whitespace()).collect()
|
|
|
|
}
|
|
|
|
_ => Vec::new(),
|
|
|
|
}
|
2015-11-20 00:20:12 +01:00
|
|
|
}
|
|
|
|
}
|
2017-01-16 23:15:12 +01:00
|
|
|
|
|
|
|
fn split(s: Vec<String>) -> Vec<String> {
|
|
|
|
s.iter().flat_map(|s| s.split(',')).map(|s| s.to_string()).collect()
|
|
|
|
}
|