Debian Codegraph @

sysinit/coqargs.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 let error_wrong_arg msg =
12 prerr_endline msg; exit 1
13
14 let error_missing_arg s =
15 prerr_endline ("Error: extra argument expected after option "^s^".");
16 prerr_endline "See -help for the syntax of supported options.";
17 exit 1
18
19 let error_debug () =
20 prerr_endline "Error: The -debug option has been removed.";
21 prerr_endline "Use the -d option for enabling debug output.";
22 prerr_endline "For an OCaml backtrace use -bt instead.";
23 prerr_endline "See -help for the syntax of supported options.";
24 exit 1
25
26 (******************************************************************************)
27
28 type native_compiler = Coq_config.native_compiler =
29 NativeOff | NativeOn of { ondemand : bool }
30
31 type top = TopLogical of string | TopPhysical of string
32
33 type option_command =
34 | OptionSet of string option
35 | OptionUnset
36
37 type export_flag = Export | Import
38
39 type require_injection = { lib: string; prefix: string option; export: export_flag option; allow_failure: bool }
40
41 type injection_command =
42 | OptionInjection of (string list * option_command)
43 | RequireInjection of require_injection
44 | WarnNoNative of string
45 | WarnNativeDeprecated
46
47 type coqargs_logic_config = {
48 impredicative_set : bool;
49 indices_matter : bool;
50 type_in_type : bool;
51 rewrite_rules : bool;
52 toplevel_name : top;
53 }
54
55 type time_config = ToFeedback | ToFile of string
56
57 type coqargs_config = {
58 logic : coqargs_logic_config;
59 rcfile : string option;
60 coqlib : string option;
61 enable_VM : bool;
62 native_compiler : native_compiler;
63 native_output_dir : CUnix.physical_path;
64 native_include_dirs : CUnix.physical_path list;
65 output_directory : CUnix.physical_path option;
66 exclude_dirs : CUnix.physical_path list;
67 beautify : bool;
68 quiet : bool;
69 time : time_config option;
70 test_mode : bool;
71 profile : string option;
72 print_emacs : bool;
73 }
74
75 type vo_path = {
76 implicit : bool;
77 unix_path : string;
78 rocq_path : string;
79 }
80
81 type coqargs_pre = {
82 boot : bool;
83 load_init : bool;
84 load_rcfile : bool;
85
86 ml_includes : string list;
87 vo_includes : vo_path list;
88
89 load_vernacular_list : (string * bool) list;
90 injections : injection_command list;
91 }
92
93 type coqargs_main =
94 | Queries of Boot.Usage.query list
95 | Run
96
97 type coqargs_post = {
98 memory_stat : bool;
99 }
100
101 type t = {
102 config : coqargs_config;
103 pre : coqargs_pre;
104 main : coqargs_main;
105 post : coqargs_post;
106 }
107
108 let default_toplevel = "Top"
109
110 let default_native = Coq_config.native_compiler
111
112 let default_logic_config = {
113 impredicative_set = false;
114 indices_matter = false;
115 type_in_type = false;
116 rewrite_rules = false;
117 toplevel_name = TopLogical default_toplevel;
118 }
119
120 let default_config = {
121 logic = default_logic_config;
122 rcfile = None;
123 coqlib = None;
124 enable_VM = true;
125 native_compiler = default_native;
126 native_output_dir = ".coq-native";
127 native_include_dirs = [];
128 output_directory = None;
129 exclude_dirs = [];
130 beautify = false;
131 quiet = false;
132 time = None;
133 test_mode = false;
134 profile = None;
135 print_emacs = false;
136
137 (* Quiet / verbosity options should be here *)
138 }
139
140 let default_pre = {
141 boot = false;
142 load_init = true;
143 load_rcfile = true;
144 ml_includes = [];
145 vo_includes = [];
146 load_vernacular_list = [];
147 injections = [];
148 }
149
150 let default_queries = []
151
152 let default_post = {
153 memory_stat = false;
154 }
155
156 let default = {
157 config = default_config;
158 pre = default_pre;
159 main = Run;
160 post = default_post;
161 }
162
163 (******************************************************************************)
164 (* Functional arguments *)
165 (******************************************************************************)
166 let add_ml_include opts s =
167 { opts with pre = { opts.pre with ml_includes = s :: opts.pre.ml_includes }}
168
169 let add_vo_include opts unix_path rocq_path implicit =
170 let v = { unix_path; rocq_path; implicit; } in
171 { opts with pre = { opts.pre with vo_includes = v :: opts.pre.vo_includes }}
172
173 let add_vo_require opts d ?(allow_failure=false) p export =
174 { opts with pre = { opts.pre with injections = RequireInjection {lib=d; prefix=p; export; allow_failure} :: opts.pre.injections }}
175
176 let add_load_vernacular opts verb s =
177 { opts with pre = { opts.pre with load_vernacular_list = (CUnix.make_suffix s ".v",verb) :: opts.pre.load_vernacular_list }}
178
179 let add_set_option opts opt_name value =
180 { opts with pre = { opts.pre with injections = OptionInjection (opt_name, value) :: opts.pre.injections }}
181
182 let add_set_warnings opts flags =
183 add_set_option opts ["Warnings"] (OptionSet (Some flags))
184
185 let add_set_debug opts flags =
186 add_set_option opts ["Debug"] (OptionSet (Some flags))
187
188 (** Options for proof general *)
189 let set_emacs opts =
190 let opts = add_set_option opts ["Printing";"Goal";"Tags"] (OptionSet None) in
191 { opts with config = { opts.config with print_emacs = true }}
192
193 let set_logic f oval =
194 { oval with config = { oval.config with logic = f oval.config.logic }}
195
196 let set_query opts q =
197 { opts with main = match opts.main with
198 | Run -> Queries (default_queries@[q])
199 | Queries queries -> Queries (queries@[q])
200 }
201
202 (******************************************************************************)
203 (* Parsing helpers *)
204 (******************************************************************************)
205 let get_bool ~opt = function
206 | "yes" | "on" -> true
207 | "no" | "off" -> false
208 | _ ->
209 error_wrong_arg ("Error: yes/no expected after option "^opt^".")
210
211 let get_int ~opt n =
212 try int_of_string n
213 with Failure _ ->
214 error_wrong_arg ("Error: integer expected after option "^opt^".")
215 let get_int_opt ~opt n =
216 if n = "" then None
217 else Some (get_int ~opt n)
218
219 let get_float ~opt n =
220 try float_of_string n
221 with Failure _ ->
222 error_wrong_arg ("Error: float expected after option "^opt^".")
223
224 let to_opt_key = Str.(split (regexp " +"))
225
226 let parse_option_set opt =
227 match String.index_opt opt '=' with
228 | None -> to_opt_key opt, None
229 | Some eqi ->
230 let len = String.length opt in
231 let v = String.sub opt (eqi+1) (len - eqi - 1) in
232 to_opt_key (String.sub opt 0 eqi), Some v
233
234 let get_native_compiler s =
235 (* We use two boolean flags because the four states make sense, even if
236 only three are accessible to the user at the moment. The selection of the
237 produced artifact(s) (`.vo`, `.coq-native`, ...) should be done by
238 a separate flag, and the "ondemand" value removed. Once this is done, use
239 [get_bool] here. *)
240 let n = match s with
241 | ("yes" | "on") -> NativeOn {ondemand=false}
242 | "ondemand" -> NativeOn {ondemand=true}
243 | ("no" | "off") -> NativeOff
244 | _ ->
245 error_wrong_arg ("Error: (yes|no|ondemand) expected after option -native-compiler.")
246 in
247 if n = NativeOff then n, []
248 else if Coq_config.native_compiler = NativeOff then
249 NativeOff, [WarnNativeDeprecated; WarnNoNative s]
250 else n, [WarnNativeDeprecated]
251
252 (* Main parsing routine *)
253 (*s Parsing of the command line *)
254
255 let parse_args ~init arglist : t * string list =
256 let args = ref arglist in
257 let extras = ref [] in
258 let rec parse oval = match !args with
259 | [] ->
260 (oval, List.rev !extras)
261 | opt :: rem ->
262 args := rem;
263 let next () = match !args with
264 | x::rem -> args := rem; x
265 | [] -> error_missing_arg opt
266 in
267 let noval = begin match opt with
268
269 (* Complex options with many args *)
270 |"-I"|"-include" -> add_ml_include oval (next())
271 |"-Q" ->
272 let d = next () in
273 let p = next () in
274 let p = if String.equal p "Coq" then "Corelib" else p in
275 add_vo_include oval d p false
276 |"-R" ->
277 let d = next () in
278 let p = next () in
279 let p = if String.equal p "Coq" then "Corelib" else p in
280 (* -R Coq ... is only used by Dune in conjunction with the -boot
281 option. The above line should be removed once we require an
282 updated version of Dune. *)
283 add_vo_include oval d p true
284
285 (* Options with one arg *)
286 |"-coqlib" ->
287 { oval with config = { oval.config with coqlib = Some (next ())
288 }}
289
290 |"-compat" ->
291 let arg = String.split_on_char '.' (next ()) in
292 let rocq_name = match arg with "8" :: _ -> "Coq" | _ -> "Rocq" in
293 (* remove the above and replace by "Rocq" once theories/Compat/Coq820.v is removed *)
294 let xy = String.concat "" (rocq_name :: arg) in
295 add_vo_require oval xy ~allow_failure:true (Some "Stdlib") (Some Import)
296
297 |"-exclude-dir" ->
298 { oval with config = { oval.config with exclude_dirs = next() :: oval.config.exclude_dirs } }
299
300 |"-init-file" ->
301 { oval with config = { oval.config with rcfile = Some (next ()); }}
302
303 |"-load-vernac-source"|"-l" ->
304 add_load_vernacular oval false (next ())
305
306 |"-load-vernac-source-verbose"|"-lv" ->
307 add_load_vernacular oval true (next ())
308
309 |"-mangle-names" ->
310 let oval = add_set_option oval ["Mangle"; "Names"] (OptionSet None) in
311 add_set_option oval ["Mangle"; "Names"; "Prefix"] (OptionSet(Some(next ())))
312
313 |"-profile-ltac-cutoff" ->
314 let oval = add_set_option oval ["Ltac"; "Profiling"] (OptionSet None) in
315 add_set_option oval ["Ltac"; "Profiling"; "Cutoff"] (OptionSet (Some (next ())))
316
317 |"-load-vernac-object"|"-require" ->
318 add_vo_require oval (next ()) None None
319
320 |"-require-import" | "-ri" -> add_vo_require oval (next ()) None (Some Import)
321
322 |"-require-export" | "-re" -> add_vo_require oval (next ()) None (Some Export)
323
324 |"-require-from"|"-rfrom" ->
325 let from = next () in add_vo_require oval (next ()) (Some from) None
326
327 |"-compat-from" ->
328 let from = next () in add_vo_require oval (next ()) ~allow_failure:true (Some from) (Some Import)
329
330 |"-require-import-from" | "-rifrom" ->
331 let from = next () in add_vo_require oval (next ()) (Some from) (Some Import)
332
333 |"-require-export-from" | "-refrom" ->
334 let from = next () in add_vo_require oval (next ()) (Some from) (Some Export)
335
336 |"-top" ->
337 let topname = next () in
338 if CString.is_empty topname then
339 error_wrong_arg "Need a non empty toplevel module name.";
340 { oval with
341 config = {
342 oval.config with
343 logic = {
344 oval.config.logic with
345 toplevel_name = TopLogical topname }}}
346
347 |"-topfile" ->
348 { oval with config = { oval.config with logic = { oval.config.logic with toplevel_name = TopPhysical (next()) }}}
349
350 |"-w" | "-W" -> add_set_warnings oval (next())
351
352 |"-bytecode-compiler" ->
353 { oval with config = { oval.config with enable_VM = get_bool ~opt (next ()) }}
354
355 |"-native-compiler" ->
356 let native_compiler, warn = get_native_compiler (next ()) in
357 { oval with config = { oval.config with native_compiler };
358 pre = { oval.pre with injections = warn @ oval.pre.injections }}
359
360 | "-set" ->
361 let opt, v = parse_option_set @@ next() in
362 add_set_option oval opt (OptionSet v)
363
364 | "-unset" ->
365 add_set_option oval (to_opt_key @@ next ()) OptionUnset
366
367 |"-native-output-dir" ->
368 let native_output_dir = next () in
369 { oval with config = { oval.config with native_output_dir } }
370
371 |"-output-dir" | "-output-directory" ->
372 let dir = next () in
373 let dir = if Filename.is_relative dir then Filename.concat (Sys.getcwd ()) dir else dir in
374 { oval with config = { oval.config with output_directory = Some dir } }
375
376 |"-nI" ->
377 let include_dir = next () in
378 { oval with config = {oval.config with native_include_dirs = include_dir :: oval.config.native_include_dirs } }
379
380 (* Options with zero arg *)
381 |"-test-mode" -> { oval with config = { oval.config with test_mode = true } }
382 |"-beautify" -> { oval with config = { oval.config with beautify = true } }
383 |"-config"|"--config" -> set_query oval PrintConfig
384
385 |"-bt" -> add_set_debug oval "backtrace"
386 (* -debug is deprecated *)
387 |"-debug" -> error_debug ()
388 |"-d" | "-D" -> add_set_debug oval (next())
389
390 |"-diffs" ->
391 add_set_option oval ["Diffs"] @@ OptionSet (Some (next ()))
392 |"-emacs" -> set_emacs oval
393 |"-impredicative-set" ->
394 set_logic (fun o -> { o with impredicative_set = true }) oval
395 |"-allow-sprop" ->
396 add_set_option oval ["Allow"; "StrictProp"] (OptionSet None)
397 |"-disallow-sprop" ->
398 add_set_option oval ["Allow"; "StrictProp"] OptionUnset
399 |"-allow-rewrite-rules" -> set_logic (fun o -> { o with rewrite_rules = true }) oval
400 |"-indices-matter" -> set_logic (fun o -> { o with indices_matter = true }) oval
401 |"-m"|"--memory" -> { oval with post = { memory_stat = true }}
402 |"-noinit"|"-nois" -> { oval with pre = { oval.pre with load_init = false }}
403 |"-boot" -> { oval with pre = { oval.pre with boot = true }}
404 |"-profile-ltac" -> add_set_option oval ["Ltac"; "Profiling"] (OptionSet None)
405 |"-q" -> { oval with pre = { oval.pre with load_rcfile = false; }}
406 |"-quiet"|"-silent" ->
407 add_set_warnings { oval with config = { oval.config with quiet = true } } "-all"
408 |"-time" -> { oval with config = { oval.config with time = Some ToFeedback }}
409 |"-time-file" -> { oval with config = { oval.config with time = Some (ToFile (next())) }}
410 | "-profile" -> { oval with config = { oval.config with profile = Some (next()) } }
411 |"-type-in-type" -> set_logic (fun o -> { o with type_in_type = true }) oval
412 |"-unicode" -> add_vo_require oval "Utf8_core" None (Some Import)
413 |"-where" -> set_query oval PrintWhere
414 |"-h"|"-H"|"-?"|"-help"|"--help" -> set_query oval PrintHelp
415 |"-v"|"--version" -> set_query oval PrintVersion
416 |"-print-version"|"--print-version" -> set_query oval PrintMachineReadableVersion
417
418 (* Unknown option *)
419 | s ->
420 extras := s :: !extras;
421 oval
422 end in
423 parse noval
424 in
425 parse init
426
427 (* We need to reverse a few lists *)
428 let parse_args ~init args =
429 let opts, extra = parse_args ~init args in
430 let opts =
431 { opts with
432 pre = { opts.pre with
433 ml_includes = List.rev opts.pre.ml_includes
434 ; vo_includes = List.rev opts.pre.vo_includes
435 ; load_vernacular_list = List.rev opts.pre.load_vernacular_list
436 ; injections = List.rev opts.pre.injections
437 }
438 } in
439 opts, extra
440
441 (******************************************************************************)
442 (* Startup LoadPath and Modules *)
443 (******************************************************************************)
444
445 (* prelude_data == From Corelib Require Import Prelude. *)
446 let prelude_data = RequireInjection { lib = "Prelude"; prefix = Some "Corelib"; export = Some Import; allow_failure = false }
447
448 let injection_commands opts =
449 if opts.pre.load_init then prelude_data :: opts.pre.injections else opts.pre.injections