Debian Codegraph @

topbin/rocqshim.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 worker = {
12 package : string;
13 basename : string;
14 }
15
16 let get_worker_path {package; basename} =
17 let dir = try Findlib.package_directory package
18 with Findlib.No_such_package _ ->
19 Printf.eprintf "Error: Could not find package %s.\n%!" package;
20 exit 1
21 in
22 let exe = if Sys.(os_type = "Win32" || os_type = "Cygwin") then ".exe" else "" in
23 Filename.concat dir (basename^exe)
24
25 type init_opts = {
26 boot : bool;
27 coqlib : string option;
28 ml_includes : string list;
29 queries : Boot.Usage.query list;
30 }
31
32 let default_opts = {
33 boot = false;
34 coqlib = None;
35 ml_includes = [];
36 queries = [];
37 }
38
39 let add_query q opts = { opts with queries = q :: opts.queries }
40
41 let parse_query = let open Boot.Usage in function
42 | "-config"|"--config" -> Some PrintConfig
43 | "-where" -> Some PrintWhere
44 | "-v"|"-version"|"--version" -> Some PrintVersion
45 | "-print-version"|"--print-version" -> Some PrintMachineReadableVersion
46 | _ -> None
47
48 let rec parse_args opts = function
49 | [] -> opts
50 | "-boot" :: rest -> parse_args {opts with boot = true} rest
51 | "-coqlib" :: lib :: rest -> parse_args {opts with coqlib = Some lib} rest
52 | "-I" :: ml :: rest -> parse_args {opts with ml_includes = ml :: opts.ml_includes} rest
53 | arg :: rest ->
54 match parse_query arg with
55 | Some q -> parse_args (add_query q opts) rest
56 | None -> parse_args opts rest
57
58 let parse_args args =
59 let opts = parse_args default_opts args in
60 let opts = {
61 opts with
62 ml_includes = List.rev opts.ml_includes;
63 queries = List.rev opts.queries;
64 }
65 in
66 opts
67
68 let with_ic file f =
69 let ic = open_in file in
70 try
71 let rc = f ic in
72 close_in ic;
73 rc
74 with e -> close_in ic; raise e
75
76 let parse_env_line l =
77 try Scanf.sscanf l "%[^=]=%S" (fun name value -> Some(name,value))
78 with Scanf.Scan_failure _ | End_of_file -> None
79
80 (** We [putenv] instead of wrapping [getenv] calls because the
81 subprocess also needs the updated env, and usually doesn't have
82 the env file next to its binary. *)
83 let putenv_from_file ~debug () =
84 let base = Filename.dirname Sys.executable_name in
85 let f = base ^ "/coq_environment.txt" in
86 try
87 with_ic f (fun ic ->
88 let () = if debug then Printf.eprintf "using env vars from %s\n%!" f in
89 let rec iter () =
90 match input_line ic with
91 | exception End_of_file -> ()
92 | l ->
93 let () = match parse_env_line l with
94 | Some(n,v) ->
95 begin match Sys.getenv_opt n with
96 | None -> Unix.putenv n v
97 | Some _ -> ()
98 end
99 | None -> ()
100 in
101 iter ()
102 in
103 iter ())
104 with
105 | Sys_error s -> ()
106
107 let make_ocamlpath envopt opts =
108 let boot_ml_path = match envopt with
109 | Boot.Env.Boot -> []
110 | Boot.Env.Env coqenv ->
111 Boot.Env.Path.[to_string (relative (Boot.Env.runtimelib coqenv) "..")]
112 in
113 let env_ocamlpath =
114 try [Sys.getenv "OCAMLPATH"]
115 with Not_found -> []
116 in
117 let path = List.concat [opts.ml_includes; boot_ml_path; env_ocamlpath] in
118 let ocamlpathsep = if Sys.unix then ":" else ";" in
119 String.concat ocamlpathsep path
120
121 let exec_or_create_process prog argv =
122 if Sys.os_type <> "Win32" then
123 Unix.execv prog argv
124 else
125 let pid = Unix.create_process prog argv Unix.stdin Unix.stdout Unix.stderr in
126 if pid = 0 then begin
127 Printf.eprintf "coqc shim: create_process failed\n%!";
128 exit 127
129 end;
130 let _, status = Unix.waitpid [] pid in
131 match status with
132 | WEXITED n | WSIGNALED n -> exit n
133 | WSTOPPED _ ->
134 (* is there anything sensible to do with WSTOPPED? can it even happen on windows? *)
135 assert false
136
137 type opts = { debug_shim : bool }
138
139 let parse_opts = function
140 | "-debug-shim" :: rest -> { debug_shim = true }, rest
141 | args -> { debug_shim = false }, args
142
143 (* warning will be produced by the worker *)
144 let warn_ignored_coqlib () = ()
145
146 let boot_env opts =
147 match Boot.Env.print_queries_maybe_init ~boot:opts.boot ~coqlib:opts.coqlib
148 ~warn_ignored_coqlib
149 None opts.queries
150 with
151 | Ok env -> env
152 | Error msg -> Printf.eprintf "%s\n%!" msg; exit 1
153
154 let init { debug_shim=debug } args =
155 (* important to putenv before reading OCAMLPATH / ROCQLIB *)
156 let () = putenv_from_file ~debug () in
157 let opts = parse_args args in
158 let coqenv = boot_env opts in
159 let () = match opts.queries with [] -> () | _ :: _ -> exit 0 in
160 let env_ocamlpath = make_ocamlpath coqenv opts in
161 let () = if debug then Printf.eprintf "OCAMLPATH = %s\n%!" env_ocamlpath in
162 Findlib.init ~env_ocamlpath ()
163
164 let try_run_queries { debug_shim=debug } args =
165 let () = putenv_from_file ~debug () in
166 let opts = parse_args args in
167 match opts.queries with
168 | [] -> false
169 | _ :: _ ->
170 let _coqenv = boot_env opts in
171 true