Skip to content

Commit aa50ebd

Browse files
committed
Move command line parsing to parse_args.rs
1 parent e1c5982 commit aa50ebd

File tree

2 files changed

+219
-160
lines changed

2 files changed

+219
-160
lines changed

src/librustpkg/lib.rs

Lines changed: 29 additions & 160 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,8 @@ extern mod extra;
2020
extern mod rustc;
2121
extern mod syntax;
2222

23-
use std::{os, result, run, str, task};
23+
use std::{os, run, str, task};
2424
use std::io::process;
25-
use std::hashmap::HashSet;
2625
use std::io;
2726
use std::io::fs;
2827
pub use std::path::Path;
@@ -32,27 +31,26 @@ use rustc::driver::{driver, session};
3231
use rustc::metadata::filesearch;
3332
use rustc::metadata::filesearch::rust_path;
3433
use rustc::util::sha2;
35-
use extra::{getopts};
3634
use syntax::{ast, diagnostic};
3735
use messages::{error, warn, note};
36+
use parse_args::{ParseResult, parse_args};
3837
use path_util::{build_pkg_id_in_workspace, built_test_in_workspace};
3938
use path_util::in_rust_path;
4039
use path_util::{built_executable_in_workspace, built_library_in_workspace, default_workspace};
4140
use path_util::{target_executable_in_workspace, target_library_in_workspace, dir_has_crate_file};
4241
use source_control::{CheckedOutSources, is_git_dir, make_read_only};
4342
use workspace::{each_pkg_parent_workspace, pkg_parent_workspaces, cwd_to_workspace};
4443
use workspace::determine_destination;
45-
use context::{Context, BuildContext,
46-
RustcFlags, Trans, Link, Nothing, Pretty, Analysis, Assemble,
47-
LLVMAssemble, LLVMCompileBitcode};
44+
use context::{BuildContext, Trans, Nothing, Pretty, Analysis,
45+
LLVMAssemble, LLVMCompileBitcode};
4846
use context::{Command, BuildCmd, CleanCmd, DoCmd, InfoCmd, InstallCmd, ListCmd,
4947
PreferCmd, TestCmd, InitCmd, UninstallCmd, UnpreferCmd};
5048
use crate_id::CrateId;
5149
use package_source::PkgSrc;
5250
use target::{WhatToBuild, Everything, is_lib, is_main, is_test, is_bench};
5351
use target::{Main, Tests, MaybeCustom, Inferred, JustOne};
5452
use workcache_support::digest_only_date;
55-
use exit_codes::{COPY_FAILED_CODE, BAD_FLAG_CODE};
53+
use exit_codes::{COPY_FAILED_CODE};
5654

5755
pub mod api;
5856
mod conditions;
@@ -63,6 +61,7 @@ mod installed_packages;
6361
mod messages;
6462
pub mod crate_id;
6563
pub mod package_source;
64+
mod parse_args;
6665
mod path_util;
6766
mod source_control;
6867
mod target;
@@ -751,173 +750,43 @@ pub fn main() {
751750
}
752751

753752
pub fn main_args(args: &[~str]) -> int {
754-
let opts = ~[getopts::optflag("h"), getopts::optflag("help"),
755-
getopts::optflag("no-link"),
756-
getopts::optflag("no-trans"),
757-
// n.b. Ignores different --pretty options for now
758-
getopts::optflag("pretty"),
759-
getopts::optflag("parse-only"),
760-
getopts::optflag("S"), getopts::optflag("assembly"),
761-
getopts::optmulti("c"), getopts::optmulti("cfg"),
762-
getopts::optflag("v"), getopts::optflag("version"),
763-
getopts::optflag("r"), getopts::optflag("rust-path-hack"),
764-
getopts::optopt("sysroot"),
765-
getopts::optflag("emit-llvm"),
766-
getopts::optopt("linker"),
767-
getopts::optopt("link-args"),
768-
getopts::optopt("opt-level"),
769-
getopts::optflag("O"),
770-
getopts::optflag("save-temps"),
771-
getopts::optopt("target"),
772-
getopts::optopt("target-cpu"),
773-
getopts::optmulti("Z") ];
774-
let matches = &match getopts::getopts(args, opts) {
775-
result::Ok(m) => m,
776-
result::Err(f) => {
777-
error(format!("{}", f.to_err_msg()));
778-
779-
return 1;
780-
}
781-
};
782-
let help = matches.opt_present("h") ||
783-
matches.opt_present("help");
784-
let no_link = matches.opt_present("no-link");
785-
let no_trans = matches.opt_present("no-trans");
786-
let supplied_sysroot = matches.opt_str("sysroot");
787-
let generate_asm = matches.opt_present("S") ||
788-
matches.opt_present("assembly");
789-
let parse_only = matches.opt_present("parse-only");
790-
let pretty = matches.opt_present("pretty");
791-
let emit_llvm = matches.opt_present("emit-llvm");
792-
793-
if matches.opt_present("v") ||
794-
matches.opt_present("version") {
795-
rustc::version(args[0]);
796-
return 0;
797-
}
798-
799-
let use_rust_path_hack = matches.opt_present("r") ||
800-
matches.opt_present("rust-path-hack");
801-
802-
let linker = matches.opt_str("linker");
803-
let link_args = matches.opt_str("link-args");
804-
let cfgs = matches.opt_strs("cfg") + matches.opt_strs("c");
805-
let mut user_supplied_opt_level = true;
806-
let opt_level = match matches.opt_str("opt-level") {
807-
Some(~"0") => session::No,
808-
Some(~"1") => session::Less,
809-
Some(~"2") => session::Default,
810-
Some(~"3") => session::Aggressive,
811-
_ if matches.opt_present("O") => session::Default,
812-
_ => {
813-
user_supplied_opt_level = false;
814-
session::No
815-
}
816-
};
817753

818-
let save_temps = matches.opt_present("save-temps");
819-
let target = matches.opt_str("target");
820-
let target_cpu = matches.opt_str("target-cpu");
821-
let experimental_features = {
822-
let strs = matches.opt_strs("Z");
823-
if matches.opt_present("Z") {
824-
Some(strs)
825-
}
826-
else {
827-
None
754+
let (command, args, context, supplied_sysroot) = match parse_args(args) {
755+
Ok(ParseResult {
756+
command: cmd,
757+
args: args,
758+
context: ctx,
759+
sysroot: sroot}) => (cmd, args, ctx, sroot),
760+
Err(error_code) => {
761+
debug!("Parsing failed. Returning error code {}", error_code);
762+
return error_code
828763
}
829764
};
830-
831-
let mut args = matches.free.clone();
832-
args.shift();
833-
834-
if (args.len() < 1) {
835-
usage::general();
836-
return 1;
837-
}
838-
839-
let rustc_flags = RustcFlags {
840-
linker: linker,
841-
link_args: link_args,
842-
optimization_level: opt_level,
843-
compile_upto: if no_trans {
844-
Trans
845-
} else if no_link {
846-
Link
847-
} else if pretty {
848-
Pretty
849-
} else if parse_only {
850-
Analysis
851-
} else if emit_llvm && generate_asm {
852-
LLVMAssemble
853-
} else if generate_asm {
854-
Assemble
855-
} else if emit_llvm {
856-
LLVMCompileBitcode
857-
} else {
858-
Nothing
859-
},
860-
save_temps: save_temps,
861-
target: target,
862-
target_cpu: target_cpu,
863-
additional_library_paths:
864-
HashSet::new(), // No way to set this from the rustpkg command line
865-
experimental_features: experimental_features
866-
};
867-
868-
let cmd_opt = args.iter().filter_map( |s| from_str(s.clone())).next();
869-
let command = match(cmd_opt) {
870-
None => {
871-
usage::general();
872-
return 0;
873-
}
874-
Some(cmd) => {
875-
let bad_option = context::flags_forbidden_for_cmd(&rustc_flags,
876-
cfgs,
877-
cmd,
878-
user_supplied_opt_level);
879-
if help || bad_option {
880-
usage::usage_for_command(cmd);
881-
if bad_option {
882-
return BAD_FLAG_CODE;
883-
}
884-
else {
885-
return 0;
886-
}
887-
} else {
888-
cmd
889-
}
890-
}
891-
};
892-
893-
// Pop off all flags, plus the command
894-
let mut remaining_args: ~[~str] = args.iter().skip_while(|&s| {
895-
let maybe_command: Option<Command> = from_str(*s);
896-
maybe_command.is_none()
897-
}).map(|s| s.clone()).collect();
898-
remaining_args.shift();
899-
let sroot = match supplied_sysroot {
765+
debug!("Finished parsing commandline args {:?}", args);
766+
debug!(" Using command: {:?}", command);
767+
debug!(" Using args {:?}", args);
768+
debug!(" Using cflags: {:?}", context.rustc_flags);
769+
debug!(" Using rust_path_hack {:b}", context.use_rust_path_hack);
770+
debug!(" Using cfgs: {:?}", context.cfgs);
771+
debug!(" Using supplied_sysroot: {:?}", supplied_sysroot);
772+
773+
let sysroot = match supplied_sysroot {
900774
Some(s) => Path::new(s),
901775
_ => filesearch::get_or_default_sysroot()
902776
};
903777

904-
debug!("Using sysroot: {}", sroot.display());
778+
debug!("Using sysroot: {}", sysroot.display());
905779
let ws = default_workspace();
906780
debug!("Will store workcache in {}", ws.display());
907781

908-
let rm_args = remaining_args.clone();
909782
// Wrap the rest in task::try in case of a condition failure in a task
910783
let result = do task::try {
911784
BuildContext {
912-
context: Context {
913-
cfgs: cfgs.clone(),
914-
rustc_flags: rustc_flags.clone(),
915-
use_rust_path_hack: use_rust_path_hack,
916-
},
917-
sysroot: sroot.clone(), // Currently, only tests override this
918-
workcache_context: api::default_context(sroot.clone(),
785+
context: context,
786+
sysroot: sysroot.clone(), // Currently, only tests override this
787+
workcache_context: api::default_context(sysroot.clone(),
919788
default_workspace()).workcache_context
920-
}.run(command, rm_args.clone())
789+
}.run(command, args.clone())
921790
};
922791
// FIXME #9262: This is using the same error code for all errors,
923792
// and at least one test case succeeds if rustpkg returns COPY_FAILED_CODE,

0 commit comments

Comments
 (0)