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;
|
|
|
|
|
2017-04-02 18:28:36 +02:00
|
|
|
use getopts::Options;
|
2016-10-21 22:18:09 +02:00
|
|
|
|
|
|
|
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>,
|
2017-06-02 18:27:44 +02:00
|
|
|
no_fail_fast: bool,
|
2016-10-21 22:18:09 +02:00
|
|
|
},
|
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>,
|
2017-05-18 22:48:14 +02:00
|
|
|
},
|
|
|
|
Install {
|
|
|
|
paths: Vec<PathBuf>,
|
2016-10-21 22:18:09 +02:00
|
|
|
},
|
2015-11-20 00:20:12 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Flags {
|
|
|
|
pub fn parse(args: &[String]) -> Flags {
|
2017-04-01 23:48:03 +02:00
|
|
|
let mut extra_help = String::new();
|
|
|
|
let mut subcommand_help = format!("\
|
|
|
|
Usage: x.py <subcommand> [options] [<paths>...]
|
|
|
|
|
|
|
|
Subcommands:
|
|
|
|
build Compile either the compiler or libraries
|
|
|
|
test Build and run some test suites
|
|
|
|
bench Build and run some benchmarks
|
|
|
|
doc Build documentation
|
|
|
|
clean Clean out build directories
|
2017-05-18 22:48:14 +02:00
|
|
|
dist Build distribution artifacts
|
|
|
|
install Install distribution artifacts
|
2017-04-01 23:48:03 +02:00
|
|
|
|
|
|
|
To learn more about a subcommand, run `./x.py <subcommand> -h`");
|
|
|
|
|
2015-11-20 00:20:12 +01:00
|
|
|
let mut opts = Options::new();
|
2017-04-01 23:48:03 +02:00
|
|
|
// Options common to all subcommands
|
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");
|
|
|
|
|
2017-04-01 23:48:03 +02:00
|
|
|
// fn usage()
|
|
|
|
let usage = |exit_code: i32, opts: &Options, subcommand_help: &str, extra_help: &str| -> ! {
|
|
|
|
println!("{}", opts.usage(subcommand_help));
|
|
|
|
if !extra_help.is_empty() {
|
|
|
|
println!("{}", extra_help);
|
|
|
|
}
|
|
|
|
process::exit(exit_code);
|
|
|
|
};
|
2016-10-21 22:18:09 +02:00
|
|
|
|
2017-04-02 07:16:46 +02:00
|
|
|
// We can't use getopt to parse the options until we have completed specifying which
|
|
|
|
// options are valid, but under the current implementation, some options are conditional on
|
|
|
|
// the subcommand. Therefore we must manually identify the subcommand first, so that we can
|
|
|
|
// complete the definition of the options. Then we can use the getopt::Matches object from
|
|
|
|
// there on out.
|
2017-06-21 19:15:49 +02:00
|
|
|
let subcommand = args.iter().find(|&s|
|
|
|
|
(s == "build")
|
|
|
|
|| (s == "test")
|
|
|
|
|| (s == "bench")
|
|
|
|
|| (s == "doc")
|
|
|
|
|| (s == "clean")
|
|
|
|
|| (s == "dist")
|
|
|
|
|| (s == "install"));
|
|
|
|
let subcommand = match subcommand {
|
2017-04-02 07:16:46 +02:00
|
|
|
Some(s) => s,
|
|
|
|
None => {
|
|
|
|
// No subcommand -- show the general usage and subcommand help
|
2017-04-01 23:48:03 +02:00
|
|
|
println!("{}\n", subcommand_help);
|
|
|
|
process::exit(0);
|
|
|
|
}
|
|
|
|
};
|
2017-03-31 06:18:27 +02:00
|
|
|
|
2017-04-04 03:15:31 +02:00
|
|
|
// Some subcommands get extra options
|
|
|
|
match subcommand.as_str() {
|
2017-06-02 18:27:44 +02:00
|
|
|
"test" => {
|
|
|
|
opts.optflag("", "no-fail-fast", "Run all tests regardless of failure");
|
|
|
|
opts.optmulti("", "test-args", "extra arguments", "ARGS");
|
|
|
|
},
|
2017-04-04 21:50:24 +02:00
|
|
|
"bench" => { opts.optmulti("", "test-args", "extra arguments", "ARGS"); },
|
|
|
|
_ => { },
|
2017-04-04 03:15:31 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
// Done specifying what options are possible, so do the getopts parsing
|
|
|
|
let matches = opts.parse(&args[..]).unwrap_or_else(|e| {
|
|
|
|
// Invalid argument/option format
|
|
|
|
println!("\n{}\n", e);
|
|
|
|
usage(1, &opts, &subcommand_help, &extra_help);
|
|
|
|
});
|
|
|
|
// Extra sanity check to make sure we didn't hit this crazy corner case:
|
|
|
|
//
|
|
|
|
// ./x.py --frobulate clean build
|
|
|
|
// ^-- option ^ ^- actual subcommand
|
|
|
|
// \_ arg to option could be mistaken as subcommand
|
|
|
|
let mut pass_sanity_check = true;
|
|
|
|
match matches.free.get(0) {
|
|
|
|
Some(check_subcommand) => {
|
2017-06-22 01:03:14 +02:00
|
|
|
if check_subcommand != subcommand {
|
2017-04-04 03:15:31 +02:00
|
|
|
pass_sanity_check = false;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
None => {
|
|
|
|
pass_sanity_check = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !pass_sanity_check {
|
|
|
|
println!("{}\n", subcommand_help);
|
|
|
|
println!("Sorry, I couldn't figure out which subcommand you were trying to specify.\n\
|
|
|
|
You may need to move some options to after the subcommand.\n");
|
|
|
|
process::exit(1);
|
|
|
|
}
|
|
|
|
// Extra help text for some commands
|
2017-04-01 23:48:03 +02:00
|
|
|
match subcommand.as_str() {
|
|
|
|
"build" => {
|
|
|
|
subcommand_help.push_str("\n
|
2016-10-21 22:18:09 +02:00
|
|
|
Arguments:
|
2017-04-02 07:16:46 +02:00
|
|
|
This subcommand accepts a number of paths to directories to the crates
|
2017-03-31 06:18:27 +02:00
|
|
|
and/or artifacts to compile. For example:
|
2016-10-21 22:18:09 +02:00
|
|
|
|
|
|
|
./x.py build src/libcore
|
2017-03-31 06:18:27 +02:00
|
|
|
./x.py build src/libcore src/libproc_macro
|
2016-10-21 22:18:09 +02:00
|
|
|
./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
|
|
|
|
|
2017-06-08 07:08:48 +02:00
|
|
|
For a quick build of a usable compiler, you can pass:
|
2016-10-21 22:18:09 +02:00
|
|
|
|
2017-06-06 23:26:56 +02:00
|
|
|
./x.py build --stage 1 src/libtest
|
|
|
|
|
|
|
|
This will first build everything once (like --stage 0 without further
|
|
|
|
arguments would), and then use the compiler built in stage 0 to build
|
2017-06-08 07:08:48 +02:00
|
|
|
src/libtest and its dependencies.
|
|
|
|
Once this is done, build/$ARCH/stage1 contains a usable compiler.");
|
2017-04-01 23:48:03 +02:00
|
|
|
}
|
|
|
|
"test" => {
|
|
|
|
subcommand_help.push_str("\n
|
2016-10-21 22:18:09 +02:00
|
|
|
Arguments:
|
2017-03-31 06:18:27 +02:00
|
|
|
This subcommand accepts a number of paths to directories to tests that
|
|
|
|
should be compiled and run. For example:
|
2016-10-21 22:18:09 +02:00
|
|
|
|
|
|
|
./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
|
2017-04-01 23:48:03 +02:00
|
|
|
./x.py test --stage 1");
|
|
|
|
}
|
|
|
|
"doc" => {
|
|
|
|
subcommand_help.push_str("\n
|
2016-10-21 22:18:09 +02:00
|
|
|
Arguments:
|
2017-03-31 06:18:27 +02:00
|
|
|
This subcommand accepts a number of paths to directories of documentation
|
|
|
|
to build. For example:
|
2016-10-21 22:18:09 +02:00
|
|
|
|
|
|
|
./x.py doc src/doc/book
|
|
|
|
./x.py doc src/doc/nomicon
|
2017-03-31 06:18:27 +02:00
|
|
|
./x.py doc src/doc/book src/libstd
|
2016-10-21 22:18:09 +02:00
|
|
|
|
|
|
|
If no arguments are passed then everything is documented:
|
|
|
|
|
|
|
|
./x.py doc
|
2017-04-01 23:48:03 +02:00
|
|
|
./x.py doc --stage 1");
|
2016-10-21 22:18:09 +02:00
|
|
|
}
|
2017-04-01 23:48:03 +02:00
|
|
|
_ => { }
|
2015-11-20 00:20:12 +01:00
|
|
|
};
|
2017-04-02 07:16:46 +02:00
|
|
|
// Get any optional paths which occur after the subcommand
|
|
|
|
let cwd = t!(env::current_dir());
|
|
|
|
let paths = matches.free[1..].iter().map(|p| cwd.join(p)).collect::<Vec<_>>();
|
|
|
|
|
2017-06-16 15:44:09 +02:00
|
|
|
let cfg_file = matches.opt_str("config").map(PathBuf::from).or_else(|| {
|
|
|
|
if fs::metadata("config.toml").is_ok() {
|
|
|
|
Some(PathBuf::from("config.toml"))
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
});
|
2017-04-02 07:16:46 +02:00
|
|
|
|
2017-04-01 23:48:03 +02:00
|
|
|
// All subcommands can have an optional "Available paths" section
|
|
|
|
if matches.opt_present("verbose") {
|
|
|
|
let flags = Flags::parse(&["build".to_string()]);
|
2017-06-16 15:44:09 +02:00
|
|
|
let mut config = Config::parse(&flags.build, cfg_file.clone());
|
2017-04-01 23:48:03 +02:00
|
|
|
config.build = flags.build.clone();
|
|
|
|
let mut build = Build::new(flags, config);
|
|
|
|
metadata::build(&mut build);
|
|
|
|
let maybe_rules_help = step::build_rules(&build).get_help(subcommand);
|
|
|
|
if maybe_rules_help.is_some() {
|
|
|
|
extra_help.push_str(maybe_rules_help.unwrap().as_str());
|
2016-10-21 22:18:09 +02:00
|
|
|
}
|
2017-04-01 23:48:03 +02:00
|
|
|
} else {
|
|
|
|
extra_help.push_str(format!("Run `./x.py {} -h -v` to see a list of available paths.",
|
|
|
|
subcommand).as_str());
|
|
|
|
}
|
2016-10-21 22:18:09 +02:00
|
|
|
|
2017-04-01 23:48:03 +02:00
|
|
|
// User passed in -h/--help?
|
|
|
|
if matches.opt_present("help") {
|
|
|
|
usage(0, &opts, &subcommand_help, &extra_help);
|
|
|
|
}
|
2016-10-21 22:18:09 +02:00
|
|
|
|
2017-04-01 23:48:03 +02:00
|
|
|
let cmd = match subcommand.as_str() {
|
2016-10-21 22:18:09 +02:00
|
|
|
"build" => {
|
2017-04-01 23:48:03 +02:00
|
|
|
Subcommand::Build { paths: paths }
|
2016-10-21 22:18:09 +02:00
|
|
|
}
|
|
|
|
"test" => {
|
|
|
|
Subcommand::Test {
|
2017-04-01 23:48:03 +02:00
|
|
|
paths: paths,
|
|
|
|
test_args: matches.opt_strs("test-args"),
|
2017-06-02 18:27:44 +02:00
|
|
|
no_fail_fast: matches.opt_present("no-fail-fast"),
|
2016-10-21 22:18:09 +02:00
|
|
|
}
|
|
|
|
}
|
2016-11-25 22:13:59 +01:00
|
|
|
"bench" => {
|
|
|
|
Subcommand::Bench {
|
2017-04-01 23:48:03 +02:00
|
|
|
paths: paths,
|
|
|
|
test_args: matches.opt_strs("test-args"),
|
2016-11-25 22:13:59 +01:00
|
|
|
}
|
|
|
|
}
|
2017-03-31 04:58:07 +02:00
|
|
|
"doc" => {
|
2017-04-01 23:48:03 +02:00
|
|
|
Subcommand::Doc { paths: paths }
|
2017-03-31 04:58:07 +02:00
|
|
|
}
|
2016-10-21 22:18:09 +02:00
|
|
|
"clean" => {
|
2017-04-02 21:11:53 +02:00
|
|
|
if paths.len() > 0 {
|
2017-04-01 23:48:03 +02:00
|
|
|
println!("\nclean takes no arguments\n");
|
|
|
|
usage(1, &opts, &subcommand_help, &extra_help);
|
2016-10-21 22:18:09 +02:00
|
|
|
}
|
|
|
|
Subcommand::Clean
|
|
|
|
}
|
|
|
|
"dist" => {
|
|
|
|
Subcommand::Dist {
|
2017-04-01 23:48:03 +02:00
|
|
|
paths: paths,
|
2017-05-18 22:48:14 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
"install" => {
|
|
|
|
Subcommand::Install {
|
|
|
|
paths: paths,
|
2016-10-21 22:18:09 +02:00
|
|
|
}
|
|
|
|
}
|
2017-03-31 06:18:27 +02:00
|
|
|
_ => {
|
2017-04-01 23:48:03 +02:00
|
|
|
usage(1, &opts, &subcommand_help, &extra_help);
|
2016-10-21 22:18:09 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2015-11-20 00:20:12 +01:00
|
|
|
|
2017-04-01 23:48:03 +02:00
|
|
|
let mut stage = matches.opt_str("stage").map(|j| j.parse().unwrap());
|
2016-11-17 00:02:56 +01:00
|
|
|
|
2017-04-01 23:48:03 +02:00
|
|
|
if matches.opt_present("incremental") {
|
2016-11-17 00:02:56 +01:00
|
|
|
if stage.is_none() {
|
|
|
|
stage = Some(1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-11-20 00:20:12 +01:00
|
|
|
Flags {
|
2017-04-01 23:48:03 +02:00
|
|
|
verbose: matches.opt_count("verbose"),
|
2016-12-20 18:38:13 +01:00
|
|
|
stage: stage,
|
2017-04-01 23:48:03 +02:00
|
|
|
on_fail: matches.opt_str("on-fail"),
|
|
|
|
keep_stage: matches.opt_str("keep-stage").map(|j| j.parse().unwrap()),
|
|
|
|
build: matches.opt_str("build").unwrap_or_else(|| {
|
2016-10-21 22:18:09 +02:00
|
|
|
env::var("BUILD").unwrap()
|
|
|
|
}),
|
2017-04-01 23:48:03 +02:00
|
|
|
host: split(matches.opt_strs("host")),
|
|
|
|
target: split(matches.opt_strs("target")),
|
2015-11-20 00:20:12 +01:00
|
|
|
config: cfg_file,
|
2017-04-01 23:48:03 +02:00
|
|
|
src: matches.opt_str("src").map(PathBuf::from),
|
|
|
|
jobs: matches.opt_str("jobs").map(|j| j.parse().unwrap()),
|
2016-10-21 22:18:09 +02:00
|
|
|
cmd: cmd,
|
2017-04-01 23:48:03 +02:00
|
|
|
incremental: matches.opt_present("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-06-02 18:27:44 +02:00
|
|
|
|
|
|
|
pub fn no_fail_fast(&self) -> bool {
|
|
|
|
match *self {
|
|
|
|
Subcommand::Test { no_fail_fast, .. } => no_fail_fast,
|
|
|
|
_ => false,
|
|
|
|
}
|
|
|
|
}
|
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()
|
|
|
|
}
|