| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
type compilation_mode = BuildVo | BuildVos | BuildVok |
| 12 |
|
| 13 |
type glob_output = |
| 14 |
| NoGlob |
| 15 |
| GlobFile of string |
| 16 |
|
| 17 |
type t = |
| 18 |
{ compilation_mode : compilation_mode |
| 19 |
|
| 20 |
; compile_file: (string * bool) option |
| 21 |
; compilation_output_name : string option |
| 22 |
|
| 23 |
; echo : bool |
| 24 |
|
| 25 |
; glob_out : glob_output option |
| 26 |
|
| 27 |
; output_context : bool |
| 28 |
} |
| 29 |
|
| 30 |
let default = |
| 31 |
{ compilation_mode = BuildVo |
| 32 |
|
| 33 |
; compile_file = None |
| 34 |
; compilation_output_name = None |
| 35 |
|
| 36 |
; echo = false |
| 37 |
|
| 38 |
; glob_out = None |
| 39 |
|
| 40 |
; output_context = false |
| 41 |
} |
| 42 |
|
| 43 |
let depr opt = |
| 44 |
Feedback.msg_warning Pp.(seq[str "Option "; str opt; str " is a noop and deprecated"]) |
| 45 |
|
| 46 |
|
| 47 |
let fatal_error exn = |
| 48 |
Topfmt.(in_phase ~phase:ParsingCommandLine print_err_exn exn); |
| 49 |
let exit_code = CErrors.exit_code exn in |
| 50 |
exit exit_code |
| 51 |
|
| 52 |
let error_missing_arg s = |
| 53 |
prerr_endline ("Error: extra argument expected after option "^s); |
| 54 |
prerr_endline "See -help for the syntax of supported options"; |
| 55 |
exit 1 |
| 56 |
|
| 57 |
let arg_error msg = CErrors.user_err msg |
| 58 |
|
| 59 |
let is_dash_argument s = String.length s > 0 && s.[0] = '-' |
| 60 |
|
| 61 |
let add_compile ?echo copts s = |
| 62 |
if is_dash_argument s then |
| 63 |
arg_error Pp.(str "Unknown option " ++ str s); |
| 64 |
|
| 65 |
let echo = Option.default copts.echo echo in |
| 66 |
let s = |
| 67 |
let open Filename in |
| 68 |
if is_implicit s |
| 69 |
then concat current_dir_name s |
| 70 |
else s |
| 71 |
in |
| 72 |
{ copts with compile_file = Some (s,echo) } |
| 73 |
|
| 74 |
let add_compile ?echo copts v_file = |
| 75 |
match copts.compile_file with |
| 76 |
| Some (first,_) -> |
| 77 |
arg_error Pp.(str "More than one file to compile: " ++ str first ++ spc() ++ |
| 78 |
str "and " ++ str v_file) |
| 79 |
| None -> |
| 80 |
add_compile ?echo copts v_file |
| 81 |
|
| 82 |
let parse arglist : t = |
| 83 |
let echo = ref false in |
| 84 |
let args = ref arglist in |
| 85 |
let extras = ref [] in |
| 86 |
let rec parse (oval : t) = match !args with |
| 87 |
| [] -> |
| 88 |
(oval, List.rev !extras) |
| 89 |
| opt :: rem -> |
| 90 |
args := rem; |
| 91 |
let next () = match !args with |
| 92 |
| x::rem -> args := rem; x |
| 93 |
| [] -> error_missing_arg opt |
| 94 |
in |
| 95 |
let noval : t = begin match opt with |
| 96 |
|
| 97 |
| "-opt" |
| 98 |
| "-byte" as opt -> |
| 99 |
depr opt; |
| 100 |
oval |
| 101 |
| "-image" as opt -> |
| 102 |
depr opt; |
| 103 |
let _ = next () in |
| 104 |
oval |
| 105 |
|
| 106 |
|
| 107 |
| "-output-context" -> |
| 108 |
{ oval with output_context = true } |
| 109 |
|
| 110 |
| "-verbose" -> |
| 111 |
echo := true; |
| 112 |
oval |
| 113 |
|
| 114 |
| "-o" -> |
| 115 |
{ oval with compilation_output_name = Some (next ()) } |
| 116 |
|"-vos" -> |
| 117 |
Loadpath.load_vos_libraries := true; |
| 118 |
{ oval with compilation_mode = BuildVos } |
| 119 |
|"-vok" -> |
| 120 |
Loadpath.load_vos_libraries := true; |
| 121 |
{ oval with compilation_mode = BuildVok } |
| 122 |
|
| 123 |
|
| 124 |
|"-no-glob" | "-noglob" -> |
| 125 |
{ oval with glob_out = Some NoGlob } |
| 126 |
|
| 127 |
|"-dump-glob" -> |
| 128 |
let file = next () in |
| 129 |
{ oval with glob_out = Some (GlobFile file) } |
| 130 |
|
| 131 |
|
| 132 |
| s -> |
| 133 |
extras := s :: !extras; |
| 134 |
oval |
| 135 |
end in |
| 136 |
parse noval |
| 137 |
in |
| 138 |
try |
| 139 |
let opts, extra = parse default in |
| 140 |
let args = List.fold_left add_compile opts extra in |
| 141 |
args |
| 142 |
with any -> fatal_error any |