Debian Codegraph @

toplevel/coqcargs.ml

raw symbols SCIP
1 (************************************************************************)
2 (* * The Rocq Prover / The Rocq Development Team *)
3 (* v * Copyright INRIA, CNRS and contributors *)
4 (* <O___,, * (see version control and CREDITS file for authors & dates) *)
5 (* \VV/ **************************************************************)
6 (* // * This file is distributed under the terms of the *)
7 (* * GNU Lesser General Public License Version 2.1 *)
8 (* * (see LICENSE file for the text of the license) *)
9 (************************************************************************)
10
11 type compilation_mode = BuildVo | BuildVos | BuildVok
12
13 type glob_output =
14 | NoGlob
15 | GlobFile of string (* Single file for all coqc arguments *)
16
17 type t =
18 { compilation_mode : compilation_mode
19
20 ; compile_file: (string * bool) option (* bool is verbosity *)
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 (* XXX Remove this duplication with Coqargs *)
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 (* make the file name explicit; needed not to break up Rocq loadpath stuff. *)
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 (* Deprecated options *)
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 (* Non deprecated options *)
107 | "-output-context" ->
108 { oval with output_context = true }
109 (* Verbose == echo mode *)
110 | "-verbose" ->
111 echo := true;
112 oval
113 (* Output filename *)
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 (* Glob options *)
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 (* Rest *)
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