@@ -20,9 +20,8 @@ extern mod extra;
20
20
extern mod rustc;
21
21
extern mod syntax;
22
22
23
- use std:: { os, result , run, str, task} ;
23
+ use std:: { os, run, str, task} ;
24
24
use std:: io:: process;
25
- use std:: hashmap:: HashSet ;
26
25
use std:: io;
27
26
use std:: io:: fs;
28
27
pub use std:: path:: Path ;
@@ -32,27 +31,26 @@ use rustc::driver::{driver, session};
32
31
use rustc:: metadata:: filesearch;
33
32
use rustc:: metadata:: filesearch:: rust_path;
34
33
use rustc:: util:: sha2;
35
- use extra:: { getopts} ;
36
34
use syntax:: { ast, diagnostic} ;
37
35
use messages:: { error, warn, note} ;
36
+ use parse_args:: { ParseResult , parse_args} ;
38
37
use path_util:: { build_pkg_id_in_workspace, built_test_in_workspace} ;
39
38
use path_util:: in_rust_path;
40
39
use path_util:: { built_executable_in_workspace, built_library_in_workspace, default_workspace} ;
41
40
use path_util:: { target_executable_in_workspace, target_library_in_workspace, dir_has_crate_file} ;
42
41
use source_control:: { CheckedOutSources , is_git_dir, make_read_only} ;
43
42
use workspace:: { each_pkg_parent_workspace, pkg_parent_workspaces, cwd_to_workspace} ;
44
43
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 } ;
48
46
use context:: { Command , BuildCmd , CleanCmd , DoCmd , InfoCmd , InstallCmd , ListCmd ,
49
47
PreferCmd , TestCmd , InitCmd , UninstallCmd , UnpreferCmd } ;
50
48
use crate_id:: CrateId ;
51
49
use package_source:: PkgSrc ;
52
50
use target:: { WhatToBuild , Everything , is_lib, is_main, is_test, is_bench} ;
53
51
use target:: { Main , Tests , MaybeCustom , Inferred , JustOne } ;
54
52
use workcache_support:: digest_only_date;
55
- use exit_codes:: { COPY_FAILED_CODE , BAD_FLAG_CODE } ;
53
+ use exit_codes:: { COPY_FAILED_CODE } ;
56
54
57
55
pub mod api;
58
56
mod conditions;
@@ -63,6 +61,7 @@ mod installed_packages;
63
61
mod messages;
64
62
pub mod crate_id;
65
63
pub mod package_source;
64
+ mod parse_args;
66
65
mod path_util;
67
66
mod source_control;
68
67
mod target;
@@ -751,173 +750,43 @@ pub fn main() {
751
750
}
752
751
753
752
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
- } ;
817
753
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
828
763
}
829
764
} ;
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 {
900
774
Some ( s) => Path :: new ( s) ,
901
775
_ => filesearch:: get_or_default_sysroot ( )
902
776
} ;
903
777
904
- debug ! ( "Using sysroot: {}" , sroot . display( ) ) ;
778
+ debug ! ( "Using sysroot: {}" , sysroot . display( ) ) ;
905
779
let ws = default_workspace ( ) ;
906
780
debug ! ( "Will store workcache in {}" , ws. display( ) ) ;
907
781
908
- let rm_args = remaining_args. clone ( ) ;
909
782
// Wrap the rest in task::try in case of a condition failure in a task
910
783
let result = do task:: try {
911
784
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 ( ) ,
919
788
default_workspace ( ) ) . workcache_context
920
- } . run ( command, rm_args . clone ( ) )
789
+ } . run ( command, args . clone ( ) )
921
790
} ;
922
791
// FIXME #9262: This is using the same error code for all errors,
923
792
// and at least one test case succeeds if rustpkg returns COPY_FAILED_CODE,
0 commit comments