| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
let fatal_error fmt = Printf.kfprintf (fun _ -> exit 1) stderr (fmt^^"%!") |
| 12 |
|
| 13 |
let with_worker_gen opts ?(package="rocq-runtime") basename args = |
| 14 |
Rocqshim.init opts args; |
| 15 |
let prog = Rocqshim.get_worker_path { package; basename } in |
| 16 |
let () = if opts.debug_shim then Printf.eprintf "Using %s\n%!" prog in |
| 17 |
let argv = Array.of_list (prog :: args) in |
| 18 |
Rocqshim.exec_or_create_process prog argv |
| 19 |
|
| 20 |
let with_worker opts kind args = |
| 21 |
with_worker_gen opts "rocqworker" (("--kind="^kind) :: args) |
| 22 |
|
| 23 |
let with_sibling_exe opts prog args = |
| 24 |
let prog = System.get_toplevel_path prog in |
| 25 |
let () = if opts.Rocqshim.debug_shim then Printf.eprintf "Using %s\n%!" prog in |
| 26 |
let argv = Array.of_list (prog :: args) in |
| 27 |
Rocqshim.exec_or_create_process prog argv |
| 28 |
|
| 29 |
type subcommand = |
| 30 |
| Compile |
| 31 |
| Repl |
| 32 |
| ReplWithDrop |
| 33 |
| NativePrecompile |
| 34 |
| Check |
| 35 |
| Votour |
| 36 |
| PpMlg |
| 37 |
| Dep |
| 38 |
| Doc |
| 39 |
| Wc |
| 40 |
| Workmgr |
| 41 |
| Tex |
| 42 |
| Makefile |
| 43 |
| Timelog2Html |
| 44 |
|
| 45 |
let subcommands = [ |
| 46 |
("compile", "Compile a Rocq source file", Compile); |
| 47 |
("c", "Alias for compile", Compile); |
| 48 |
("repl", "Interactive read-eval-print loop", Repl); |
| 49 |
("top", "Alias for repl", Repl); |
| 50 |
("repl-with-drop", "repl with Drop command", ReplWithDrop); |
| 51 |
("top-with-drop", "Alias for repl-with-drop", ReplWithDrop); |
| 52 |
("native-precompile", "Preprocess a compiled Rocq file for use by native_compute", NativePrecompile); |
| 53 |
("check", "Check a compiled Rocq file (alias for command rocqchk)", Check); |
| 54 |
("votour", "Low level debugging of compiled files", Votour); |
| 55 |
("preprocess-mlg", "Preprocess Rocq grammar files (.mlg) to produce OCaml sources", PpMlg); |
| 56 |
("pp-mlg", "Alias for preprocess-mlg", PpMlg); |
| 57 |
("dep", "Print dependencies for compiling Rocq files", Dep); |
| 58 |
("doc", "Generate documentation from a Rocq source file", Doc); |
| 59 |
("wc", "Count lines of code in a Rocq source file", Wc); |
| 60 |
("workmgr", "Control the number of parallel workers used by Rocq", Workmgr); |
| 61 |
("tex", "Process Rocq code in a Latex document", Tex); |
| 62 |
("makefile", "Generate a Makefile to compile a Rocq project", Makefile); |
| 63 |
("timelog2html", "Combine timing information and a Rocq source file", Timelog2Html); |
| 64 |
] |
| 65 |
|
| 66 |
let print_usage fmt () = |
| 67 |
let longest_subcmd = |
| 68 |
List.fold_left (fun acc (cmd,_,_) -> max acc (String.length cmd)) 0 subcommands |
| 69 |
in |
| 70 |
let subcommands = |
| 71 |
List.map (fun (cmd,doc,_) -> |
| 72 |
let separator = String.make (3 + longest_subcmd - String.length cmd) ' ' in |
| 73 |
" "^cmd^separator^doc) |
| 74 |
subcommands |
| 75 |
in |
| 76 |
Printf.fprintf fmt "Usage: rocq [-debug-shim] {-v|--version|--print-version|--help|SUBCOMMAND} [ARGUMENTS...]\n\ |
| 77 |
\n -v, --version: print human readable version info\ |
| 78 |
\n --print-version: print machine readable version info\ |
| 79 |
\n\n\ |
| 80 |
Supported subcommands:\n\n\ |
| 81 |
%s\n\ |
| 82 |
\n\ |
| 83 |
Use \"rocq subcommand --help\" to get more information about a given subcommand.\n" (String.concat "\n" subcommands) |
| 84 |
|
| 85 |
let error_usage () = |
| 86 |
fatal_error "%a" print_usage () |
| 87 |
|
| 88 |
let run_subcommand opts args = function |
| 89 |
| Compile -> with_worker opts "compile" args |
| 90 |
| Repl -> with_worker opts "repl" args |
| 91 |
| ReplWithDrop -> with_worker_gen opts "rocqworker_with_drop" args |
| 92 |
| NativePrecompile -> with_worker_gen opts "rocqnative" args |
| 93 |
| Check -> with_sibling_exe opts "rocqchk" args |
| 94 |
| Votour -> with_sibling_exe opts "votour" args |
| 95 |
| PpMlg -> Coqpp_main.main args |
| 96 |
| Dep -> Coqdeplib.Rocqdep_main.main args |
| 97 |
| Doc -> Coqdoclib.Rocqdoc_main.main ~prog:(Sys.argv.(0) ^ " doc") args |
| 98 |
| Wc -> Rocqwc.main args |
| 99 |
| Workmgr -> Rocqworkmgr.main ~prog:(Sys.argv.(0) ^ " workmgr") args |
| 100 |
| Tex -> Rocqtex.main ~prog:(Sys.argv.(0) ^ " tex") args |
| 101 |
| Makefile -> Rocqmakefile.main ~prog:[Sys.argv.(0); "makefile"] args |
| 102 |
| Timelog2Html -> with_worker_gen opts ~package:"rocq-devtools" "timelog2html" args |
| 103 |
|
| 104 |
let () = |
| 105 |
if Array.length Sys.argv < 2 then error_usage (); |
| 106 |
|
| 107 |
let args = List.tl (Array.to_list Sys.argv) in |
| 108 |
let opts, args = Rocqshim.parse_opts args in |
| 109 |
match args with |
| 110 |
|
| 111 |
| ("-h" | "-H" | "-help" | "--help") :: _ -> |
| 112 |
Printf.printf "%a%!" print_usage (); |
| 113 |
exit 0 |
| 114 |
|
| 115 |
| cmd :: args -> |
| 116 |
begin match List.find_opt (fun (cmd',_,_) -> String.equal cmd cmd') subcommands with |
| 117 |
| Some (_,_,cmd) -> run_subcommand opts args cmd |
| 118 |
| None -> |
| 119 |
if Rocqshim.try_run_queries opts (cmd::args) then () |
| 120 |
else fatal_error "Unknown subcommand %s\n%a%!" cmd print_usage () |
| 121 |
end |
| 122 |
|
| 123 |
| [] -> error_usage () |