Skip to content

Commit 55bb5e5

Browse files
committed
auto merge of #11327 : nielsle/rust/rustpkg_argparse, r=alexcrichton
Rearrange some code in rustpkg: * Move command line argument parsing to parse_args.rs * Introduce an enum to descibe commands such as Build, Install, and Clean. * Move sysroot from Context to BuildContext, to make parse_args more modular. This is my first pull request, so please tell me if there is anything I need to do.
2 parents 03a38b9 + 4140b29 commit 55bb5e5

File tree

11 files changed

+331
-274
lines changed

11 files changed

+331
-274
lines changed

src/librustpkg/api.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,8 @@ pub fn new_default_context(c: workcache::Context, p: Path) -> BuildContext {
4949
cfgs: ~[],
5050
rustc_flags: RustcFlags::default(),
5151
use_rust_path_hack: false,
52-
sysroot: p
5352
},
53+
sysroot: p,
5454
workcache_context: c
5555
}
5656
}

src/librustpkg/context.rs

Lines changed: 69 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -27,25 +27,35 @@ pub struct Context {
2727
// FOO/src/bar-0.1 instead of FOO). The flag doesn't affect where
2828
// rustpkg stores build artifacts.
2929
use_rust_path_hack: bool,
30-
// The root directory containing the Rust standard libraries
31-
sysroot: Path
3230
}
3331

3432
#[deriving(Clone)]
3533
pub struct BuildContext {
3634
// Context for workcache
3735
workcache_context: workcache::Context,
38-
// Everything else
39-
context: Context
36+
// Parsed command line options
37+
context: Context,
38+
// The root directory containing the Rust standard libraries
39+
sysroot: Path
4040
}
4141

4242
impl BuildContext {
4343
pub fn sysroot(&self) -> Path {
44-
self.context.sysroot.clone()
44+
self.sysroot.clone()
4545
}
4646

47+
// Hack so that rustpkg can run either out of a rustc target dir,
48+
// or the host dir
4749
pub fn sysroot_to_use(&self) -> Path {
48-
self.context.sysroot_to_use()
50+
if !in_target(&self.sysroot) {
51+
self.sysroot.clone()
52+
} else {
53+
let mut p = self.sysroot.clone();
54+
p.pop();
55+
p.pop();
56+
p.pop();
57+
p
58+
}
4959
}
5060

5161
/// Returns the flags to pass to rustc, as a vector of strings
@@ -132,28 +142,6 @@ pub enum StopBefore {
132142
}
133143

134144
impl Context {
135-
pub fn sysroot(&self) -> Path {
136-
self.sysroot.clone()
137-
}
138-
139-
/// Debugging
140-
pub fn sysroot_str(&self) -> ~str {
141-
self.sysroot.as_str().unwrap().to_owned()
142-
}
143-
144-
// Hack so that rustpkg can run either out of a rustc target dir,
145-
// or the host dir
146-
pub fn sysroot_to_use(&self) -> Path {
147-
if !in_target(&self.sysroot) {
148-
self.sysroot.clone()
149-
} else {
150-
let mut p = self.sysroot.clone();
151-
p.pop();
152-
p.pop();
153-
p.pop();
154-
p
155-
}
156-
}
157145

158146
/// Returns the flags to pass to rustc, as a vector of strings
159147
pub fn flag_strs(&self) -> ~[~str] {
@@ -235,85 +223,122 @@ impl RustcFlags {
235223
}
236224
}
237225

226+
227+
#[deriving(Eq)]
228+
pub enum Command {
229+
BuildCmd,
230+
CleanCmd,
231+
DoCmd,
232+
InfoCmd,
233+
InstallCmd,
234+
ListCmd,
235+
PreferCmd,
236+
TestCmd,
237+
InitCmd,
238+
UninstallCmd,
239+
UnpreferCmd,
240+
}
241+
242+
impl FromStr for Command {
243+
244+
fn from_str(s: &str) -> Option<Command> {
245+
match s {
246+
&"build" => Some(BuildCmd),
247+
&"clean" => Some(CleanCmd),
248+
&"do" => Some(DoCmd),
249+
&"info" => Some(InfoCmd),
250+
&"install" => Some(InstallCmd),
251+
&"list" => Some(ListCmd),
252+
&"prefer" => Some(PreferCmd),
253+
&"test" => Some(TestCmd),
254+
&"init" => Some(InitCmd),
255+
&"uninstall" => Some(UninstallCmd),
256+
&"unprefer" => Some(UnpreferCmd),
257+
_ => None
258+
}
259+
}
260+
}
261+
238262
/// Returns true if any of the flags given are incompatible with the cmd
239263
pub fn flags_forbidden_for_cmd(flags: &RustcFlags,
240264
cfgs: &[~str],
241-
cmd: &str, user_supplied_opt_level: bool) -> bool {
265+
cmd: Command, user_supplied_opt_level: bool) -> bool {
242266
let complain = |s| {
243267
println!("The {} option can only be used with the `build` command:
244268
rustpkg [options..] build {} [package-ID]", s, s);
245269
};
246270

247-
if flags.linker.is_some() && cmd != "build" && cmd != "install" {
271+
if flags.linker.is_some() && cmd != BuildCmd && cmd != InstallCmd {
248272
println("The --linker option can only be used with the build or install commands.");
249273
return true;
250274
}
251-
if flags.link_args.is_some() && cmd != "build" && cmd != "install" {
275+
if flags.link_args.is_some() && cmd != BuildCmd && cmd != InstallCmd {
252276
println("The --link-args option can only be used with the build or install commands.");
253277
return true;
254278
}
255279

256-
if !cfgs.is_empty() && cmd != "build" && cmd != "install" && cmd != "test" {
280+
if !cfgs.is_empty() && cmd != BuildCmd && cmd != InstallCmd && cmd != TestCmd {
257281
println("The --cfg option can only be used with the build, test, or install commands.");
258282
return true;
259283
}
260284

261-
if user_supplied_opt_level && cmd != "build" && cmd != "install" {
285+
if user_supplied_opt_level && cmd != BuildCmd && cmd != InstallCmd {
262286
println("The -O and --opt-level options can only be used with the build \
263287
or install commands.");
264288
return true;
265289
}
266290

267-
if flags.save_temps && cmd != "build" && cmd != "install" {
291+
if flags.save_temps && cmd != BuildCmd && cmd != InstallCmd {
268292
println("The --save-temps option can only be used with the build \
269293
or install commands.");
270294
return true;
271295
}
272296

273-
if flags.target.is_some() && cmd != "build" && cmd != "install" {
297+
if flags.target.is_some() && cmd != BuildCmd && cmd != InstallCmd {
274298
println("The --target option can only be used with the build \
275299
or install commands.");
276300
return true;
277301
}
278-
if flags.target_cpu.is_some() && cmd != "build" && cmd != "install" {
302+
if flags.target_cpu.is_some() && cmd != BuildCmd && cmd != InstallCmd {
279303
println("The --target-cpu option can only be used with the build \
280304
or install commands.");
281305
return true;
282306
}
283-
if flags.experimental_features.is_some() && cmd != "build" && cmd != "install" {
307+
if flags.experimental_features.is_some() && cmd != BuildCmd && cmd != InstallCmd {
284308
println("The -Z option can only be used with the build or install commands.");
285309
return true;
286310
}
287311

288312
match flags.compile_upto {
289-
Link if cmd != "build" => {
313+
Link if cmd != BuildCmd => {
290314
complain("--no-link");
291315
true
292316
}
293-
Trans if cmd != "build" => {
317+
Trans if cmd != BuildCmd => {
294318
complain("--no-trans");
295319
true
296320
}
297-
Assemble if cmd != "build" => {
321+
Assemble if cmd != BuildCmd => {
298322
complain("-S");
299323
true
300324
}
301-
Pretty if cmd != "build" => {
325+
Pretty if cmd != BuildCmd => {
302326
complain("--pretty");
303327
true
304328
}
305-
Analysis if cmd != "build" => {
329+
Analysis if cmd != BuildCmd => {
306330
complain("--parse-only");
307331
true
308332
}
309-
LLVMCompileBitcode if cmd != "build" => {
333+
LLVMCompileBitcode if cmd != BuildCmd => {
310334
complain("--emit-llvm");
311335
true
312336
}
313-
LLVMAssemble if cmd != "build" => {
337+
LLVMAssemble if cmd != BuildCmd => {
314338
complain("--emit-llvm");
315339
true
316340
}
317341
_ => false
318342
}
319343
}
344+

0 commit comments

Comments
 (0)