Skip to content

Commit aa4bd0e

Browse files
committed
Finish the improvements I planned.
- No more manual args manipulation -- getopts used for everything. As a result, options can be in any position, now, even before the subcommand. - The additional options for test, bench, and dist now appear in the help output. - No more single-letter variable bindings used internally for large scopes. - Don't output the time measurement when just invoking 'x.py' - Logic is now much more linear. We build strings up, and then print them.
1 parent 5ba579e commit aa4bd0e

File tree

3 files changed

+113
-125
lines changed

3 files changed

+113
-125
lines changed

src/bootstrap/bootstrap.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -591,17 +591,18 @@ def bootstrap():
591591

592592
def main():
593593
start_time = time()
594+
help_triggered = ('-h' in sys.argv) or ('--help' in sys.argv) or (len(sys.argv) == 1)
594595
try:
595596
bootstrap()
596-
if ('-h' not in sys.argv) and ('--help' not in sys.argv):
597+
if not help_triggered:
597598
print("Build completed successfully in %s" % format_build_time(time() - start_time))
598599
except (SystemExit, KeyboardInterrupt) as e:
599600
if hasattr(e, 'code') and isinstance(e.code, int):
600601
exit_code = e.code
601602
else:
602603
exit_code = 1
603604
print(e)
604-
if ('-h' not in sys.argv) and ('--help' not in sys.argv):
605+
if not help_triggered:
605606
print("Build completed unsuccessfully in %s" % format_build_time(time() - start_time))
606607
sys.exit(exit_code)
607608

src/bootstrap/flags.rs

Lines changed: 105 additions & 117 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ use std::fs;
1818
use std::path::PathBuf;
1919
use std::process;
2020

21-
use getopts::{Matches, Options};
21+
use getopts::{Options};
2222

2323
use Build;
2424
use config::Config;
@@ -75,7 +75,22 @@ pub enum Subcommand {
7575

7676
impl Flags {
7777
pub fn parse(args: &[String]) -> Flags {
78+
let mut extra_help = String::new();
79+
let mut subcommand_help = format!("\
80+
Usage: x.py <subcommand> [options] [<paths>...]
81+
82+
Subcommands:
83+
build Compile either the compiler or libraries
84+
test Build and run some test suites
85+
bench Build and run some benchmarks
86+
doc Build documentation
87+
clean Clean out build directories
88+
dist Build and/or install distribution artifacts
89+
90+
To learn more about a subcommand, run `./x.py <subcommand> -h`");
91+
7892
let mut opts = Options::new();
93+
// Options common to all subcommands
7994
opts.optflagmulti("v", "verbose", "use verbose output (-vv for very verbose)");
8095
opts.optflag("i", "incremental", "use incremental compilation");
8196
opts.optopt("", "config", "TOML configuration file for build", "FILE");
@@ -89,26 +104,38 @@ impl Flags {
89104
opts.optopt("j", "jobs", "number of jobs to run in parallel", "JOBS");
90105
opts.optflag("h", "help", "print this help message");
91106

92-
let usage = |exit_code, opts: &Options| -> ! {
93-
let subcommand_help = format!("\
94-
Usage: x.py <subcommand> [options] [<paths>...]
95-
96-
Subcommands:
97-
build Compile either the compiler or libraries
98-
test Build and run some test suites
99-
bench Build and run some benchmarks
100-
doc Build documentation
101-
clean Clean out build directories
102-
dist Build and/or install distribution artifacts
107+
// fn usage()
108+
let usage = |exit_code: i32, opts: &Options, subcommand_help: &str, extra_help: &str| -> ! {
109+
println!("{}", opts.usage(subcommand_help));
110+
if !extra_help.is_empty() {
111+
println!("{}", extra_help);
112+
}
113+
process::exit(exit_code);
114+
};
103115

104-
To learn more about a subcommand, run `./x.py <subcommand> -h`");
116+
// Get subcommand
117+
let matches = opts.parse(&args[..]).unwrap_or_else(|e| {
118+
// Invalid argument/option format
119+
println!("\n{}\n", e);
120+
usage(1, &opts, &subcommand_help, &extra_help);
121+
});
122+
let subcommand = match matches.free.get(0) {
123+
Some(s) => { s },
124+
None => {
125+
// No subcommand -- lets only show the general usage and subcommand help in this case.
126+
println!("{}\n", subcommand_help);
127+
process::exit(0);
128+
}
129+
};
105130

106-
println!("{}", opts.usage(&subcommand_help));
131+
// Get any optional paths which occur after the subcommand
132+
let cwd = t!(env::current_dir());
133+
let paths = matches.free[1..].iter().map(|p| cwd.join(p)).collect::<Vec<_>>();
107134

108-
let subcommand = args.get(0).map(|s| &**s);
109-
match subcommand {
110-
Some("build") => {
111-
println!("\
135+
// Some subcommands have specific arguments help text
136+
match subcommand.as_str() {
137+
"build" => {
138+
subcommand_help.push_str("\n
112139
Arguments:
113140
This subcommand accepts a number of paths to directories to the crates
114141
and/or artifacts to compile. For example:
@@ -125,12 +152,11 @@ Arguments:
125152
126153
For a quick build with a usable compile, you can pass:
127154
128-
./x.py build --stage 1 src/libtest
129-
");
130-
}
131-
132-
Some("test") => {
133-
println!("\
155+
./x.py build --stage 1 src/libtest");
156+
}
157+
"test" => {
158+
opts.optmulti("", "test-args", "extra arguments", "ARGS");
159+
subcommand_help.push_str("\n
134160
Arguments:
135161
This subcommand accepts a number of paths to directories to tests that
136162
should be compiled and run. For example:
@@ -143,12 +169,13 @@ Arguments:
143169
compiled and tested.
144170
145171
./x.py test
146-
./x.py test --stage 1
147-
");
148-
}
149-
150-
Some("doc") => {
151-
println!("\
172+
./x.py test --stage 1");
173+
}
174+
"bench" => {
175+
opts.optmulti("", "test-args", "extra arguments", "ARGS");
176+
}
177+
"doc" => {
178+
subcommand_help.push_str("\n
152179
Arguments:
153180
This subcommand accepts a number of paths to directories of documentation
154181
to build. For example:
@@ -160,143 +187,104 @@ Arguments:
160187
If no arguments are passed then everything is documented:
161188
162189
./x.py doc
163-
./x.py doc --stage 1
164-
");
165-
}
166-
167-
_ => {}
190+
./x.py doc --stage 1");
168191
}
169-
170-
171-
if let Some(subcommand) = subcommand {
172-
if subcommand == "build" ||
173-
subcommand == "test" ||
174-
subcommand == "bench" ||
175-
subcommand == "doc" ||
176-
subcommand == "clean" ||
177-
subcommand == "dist" {
178-
if args.iter().any(|a| a == "-v") {
179-
let flags = Flags::parse(&["build".to_string()]);
180-
let mut config = Config::default();
181-
config.build = flags.build.clone();
182-
let mut build = Build::new(flags, config);
183-
metadata::build(&mut build);
184-
step::build_rules(&build).print_help(subcommand);
185-
} else {
186-
println!("Run `./x.py {} -h -v` to see a list of available paths.",
187-
subcommand);
188-
}
189-
190-
println!("");
191-
}
192+
"dist" => {
193+
opts.optflag("", "install", "run installer as well");
192194
}
193-
194-
process::exit(exit_code);
195+
_ => { }
195196
};
196-
if args.len() == 0 {
197-
println!("a subcommand must be passed");
198-
usage(1, &opts);
199-
}
200-
let parse = |opts: &Options| {
201-
let m = opts.parse(&args[1..]).unwrap_or_else(|e| {
202-
println!("failed to parse options: {}", e);
203-
usage(1, opts);
204-
});
205-
if m.opt_present("h") {
206-
usage(0, opts);
197+
198+
// All subcommands can have an optional "Available paths" section
199+
if matches.opt_present("verbose") {
200+
let flags = Flags::parse(&["build".to_string()]);
201+
let mut config = Config::default();
202+
config.build = flags.build.clone();
203+
let mut build = Build::new(flags, config);
204+
metadata::build(&mut build);
205+
let maybe_rules_help = step::build_rules(&build).get_help(subcommand);
206+
if maybe_rules_help.is_some() {
207+
extra_help.push_str(maybe_rules_help.unwrap().as_str());
207208
}
208-
return m
209-
};
209+
} else {
210+
extra_help.push_str(format!("Run `./x.py {} -h -v` to see a list of available paths.",
211+
subcommand).as_str());
212+
}
210213

211-
let cwd = t!(env::current_dir());
212-
let remaining_as_path = |m: &Matches| {
213-
m.free.iter().map(|p| cwd.join(p)).collect::<Vec<_>>()
214-
};
215-
// TODO: Parse subcommand nicely up at top, so options can occur before the subcommand.
216-
// TODO: Get the subcommand-specific options below into the help output
214+
// User passed in -h/--help?
215+
if matches.opt_present("help") {
216+
usage(0, &opts, &subcommand_help, &extra_help);
217+
}
217218

218-
let m: Matches;
219-
let cmd = match &args[0][..] {
219+
let cmd = match subcommand.as_str() {
220220
"build" => {
221-
m = parse(&opts);
222-
Subcommand::Build { paths: remaining_as_path(&m) }
221+
Subcommand::Build { paths: paths }
223222
}
224223
"test" => {
225-
opts.optmulti("", "test-args", "extra arguments", "ARGS");
226-
m = parse(&opts);
227224
Subcommand::Test {
228-
paths: remaining_as_path(&m),
229-
test_args: m.opt_strs("test-args"),
225+
paths: paths,
226+
test_args: matches.opt_strs("test-args"),
230227
}
231228
}
232229
"bench" => {
233-
opts.optmulti("", "test-args", "extra arguments", "ARGS");
234-
m = parse(&opts);
235230
Subcommand::Bench {
236-
paths: remaining_as_path(&m),
237-
test_args: m.opt_strs("test-args"),
231+
paths: paths,
232+
test_args: matches.opt_strs("test-args"),
238233
}
239234
}
240235
"doc" => {
241-
m = parse(&opts);
242-
Subcommand::Doc { paths: remaining_as_path(&m) }
236+
Subcommand::Doc { paths: paths }
243237
}
244238
"clean" => {
245-
m = parse(&opts);
246-
if m.free.len() > 0 {
247-
println!("clean takes no arguments");
248-
usage(1, &opts);
239+
if matches.free.len() > 0 {
240+
println!("\nclean takes no arguments\n");
241+
usage(1, &opts, &subcommand_help, &extra_help);
249242
}
250243
Subcommand::Clean
251244
}
252245
"dist" => {
253-
opts.optflag("", "install", "run installer as well");
254-
m = parse(&opts);
255246
Subcommand::Dist {
256-
paths: remaining_as_path(&m),
257-
install: m.opt_present("install"),
247+
paths: paths,
248+
install: matches.opt_present("install"),
258249
}
259250
}
260-
"--help" => usage(0, &opts),
261251
_ => {
262-
usage(1, &opts);
252+
usage(1, &opts, &subcommand_help, &extra_help);
263253
}
264254
};
265255

266256

267-
let cfg_file = m.opt_str("config").map(PathBuf::from).or_else(|| {
257+
let cfg_file = matches.opt_str("config").map(PathBuf::from).or_else(|| {
268258
if fs::metadata("config.toml").is_ok() {
269259
Some(PathBuf::from("config.toml"))
270260
} else {
271261
None
272262
}
273263
});
274264

275-
let mut stage = m.opt_str("stage").map(|j| j.parse().unwrap());
276-
277-
let incremental = m.opt_present("i");
265+
let mut stage = matches.opt_str("stage").map(|j| j.parse().unwrap());
278266

279-
if incremental {
267+
if matches.opt_present("incremental") {
280268
if stage.is_none() {
281269
stage = Some(1);
282270
}
283271
}
284272

285273
Flags {
286-
verbose: m.opt_count("v"),
274+
verbose: matches.opt_count("verbose"),
287275
stage: stage,
288-
on_fail: m.opt_str("on-fail"),
289-
keep_stage: m.opt_str("keep-stage").map(|j| j.parse().unwrap()),
290-
build: m.opt_str("build").unwrap_or_else(|| {
276+
on_fail: matches.opt_str("on-fail"),
277+
keep_stage: matches.opt_str("keep-stage").map(|j| j.parse().unwrap()),
278+
build: matches.opt_str("build").unwrap_or_else(|| {
291279
env::var("BUILD").unwrap()
292280
}),
293-
host: split(m.opt_strs("host")),
294-
target: split(m.opt_strs("target")),
281+
host: split(matches.opt_strs("host")),
282+
target: split(matches.opt_strs("target")),
295283
config: cfg_file,
296-
src: m.opt_str("src").map(PathBuf::from),
297-
jobs: m.opt_str("jobs").map(|j| j.parse().unwrap()),
284+
src: matches.opt_str("src").map(PathBuf::from),
285+
jobs: matches.opt_str("jobs").map(|j| j.parse().unwrap()),
298286
cmd: cmd,
299-
incremental: incremental,
287+
incremental: matches.opt_present("incremental"),
300288
}
301289
}
302290
}

src/bootstrap/step.rs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -978,26 +978,25 @@ invalid rule dependency graph detected, was a rule added and maybe typo'd?
978978
}
979979
}
980980

981-
pub fn print_help(&self, command: &str) {
981+
pub fn get_help(&self, command: &str) -> Option<String> {
982982
let kind = match command {
983983
"build" => Kind::Build,
984984
"doc" => Kind::Doc,
985985
"test" => Kind::Test,
986986
"bench" => Kind::Bench,
987987
"dist" => Kind::Dist,
988-
_ => return,
988+
_ => return None,
989989
};
990990
let rules = self.rules.values().filter(|r| r.kind == kind);
991991
let rules = rules.filter(|r| !r.path.contains("nowhere"));
992992
let mut rules = rules.collect::<Vec<_>>();
993993
rules.sort_by_key(|r| r.path);
994994

995-
println!("Available paths:\n");
995+
let mut help_string = String::from("Available paths:\n");
996996
for rule in rules {
997-
print!(" ./x.py {} {}", command, rule.path);
998-
999-
println!("");
997+
help_string.push_str(format!(" ./x.py {} {}\n", command, rule.path).as_str());
1000998
}
999+
Some(help_string)
10011000
}
10021001

10031002
/// Construct the top-level build steps that we're going to be executing,

0 commit comments

Comments
 (0)