Debian Codegraph @

sysinit/coqinit.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 (** GC tweaking *)
12
13 (** Rocq is a heavy user of persistent data structures and symbolic ASTs, so the
14 minor heap is heavily solicited. Unfortunately, the default size is far too
15 small, so we enlarge it a lot (128 times larger).
16
17 To better handle huge memory consumers, we also augment the default major
18 heap increment and the GC pressure coefficient.
19 *)
20
21 let set_gc_policy () =
22 Gc.set { (Gc.get ()) with
23 Gc.minor_heap_size = 32*1024*1024 (* 32Mwords x 8 bytes/word = 256Mb *)
24 ; Gc.space_overhead = 120
25 }
26
27 let set_gc_best_fit () =
28 Gc.set { (Gc.get ()) with
29 Gc.allocation_policy = 2 (* best-fit *)
30 ; Gc.space_overhead = 200
31 }
32
33 let init_gc () =
34 try
35 (* OCAMLRUNPARAM environment variable is set.
36 * In that case, we let ocamlrun to use the values provided by the user.
37 *)
38 ignore (Sys.getenv "OCAMLRUNPARAM")
39
40 with Not_found ->
41 (* OCAMLRUNPARAM environment variable is not set.
42 * In this case, we put in place our preferred configuration.
43 *)
44 set_gc_policy ();
45 if Coq_config.caml_version_nums >= [4;10;0] then set_gc_best_fit () else ()
46
47 let init_ocaml () =
48 init_gc ();
49 (* Get error message (and backtrace if enabled) on Ctrl-C instead of just exiting the process *)
50 Sys.catch_break true
51
52 let dirpath_of_file f =
53 let ldir0 =
54 try
55 let lp = Loadpath.find_load_path (Filename.dirname f) in
56 Loadpath.logical lp
57 with Not_found -> Libnames.default_root_prefix
58 in
59 let f = try Filename.chop_extension (Filename.basename f) with Invalid_argument _ -> f in
60 let id = Names.Id.of_string f in
61 let ldir = Libnames.add_dirpath_suffix ldir0 id in
62 ldir
63
64 let dirpath_of_top = function
65 | Coqargs.TopPhysical f -> dirpath_of_file f
66 | TopLogical dp -> Libnames.dirpath_of_string dp
67
68 let parse_arguments ~parse_extra ?(initial_args=Coqargs.default) args =
69 let opts, extras = Coqargs.parse_args ~init:initial_args args in
70 let customopts, extras = parse_extra opts extras in
71 if not (CList.is_empty extras) then begin
72 prerr_endline ("Don't know what to do with "^String.concat " " extras);
73 prerr_endline "See -help for the list of supported options";
74 exit 1
75 end;
76 opts, customopts
77
78 let print_memory_stat () =
79 let open Pp in
80 (* -m|--memory from the command-line *)
81 Feedback.msg_notice
82 (str "total heap size = " ++ int (CObj.heap_size_kb ()) ++ str " kbytes" ++ fnl ());
83 (* operf-macro interface:
84 https://github.com/OCamlPro/operf-macro *)
85 try
86 let fn = Sys.getenv "OCAML_GC_STATS" in
87 let oc = open_out fn in
88 Gc.print_stat oc;
89 close_out oc
90 with exn when CErrors.noncritical exn -> ()
91
92 let to_vo_path (x:Coqargs.vo_path) : Loadpath.vo_path = {
93 implicit = x.implicit;
94 unix_path = x.unix_path;
95 coq_path = Libnames.dirpath_of_string x.rocq_path;
96 recursive = true;
97 installed = false;
98 }
99
100 let boot_env usage opts =
101 let open Coqargs in
102 let with_err = function
103 | Ok x -> x
104 | Error msg -> CErrors.user_err Pp.(str msg)
105 in
106 let boot = opts.pre.boot in
107 let coqlib = opts.config.coqlib in
108 let warn_ignored_coqlib = CWarnings.warn_ignored_coqlib in
109 match opts.main with
110 | Run -> Boot.Env.maybe_init ~boot ~coqlib ~warn_ignored_coqlib
111 | Queries qs ->
112 let _ : Boot.Env.maybe_env =
113 with_err
114 (Boot.Env.print_queries_maybe_init ~warn_ignored_coqlib ~boot ~coqlib (Some usage) qs)
115 in
116 exit 0
117
118 let init_load_paths (coqenv:Boot.Env.maybe_env) opts =
119 let open Coqargs in
120 let (boot_ml_path, boot_vo_path), native_ml_path =
121 match coqenv with
122 | Boot ->
123 ([],[]),opts.config.native_include_dirs
124 | Env coqenv ->
125 let nI = match opts.config.native_include_dirs with
126 | [] -> Nativelib.default_include_dirs coqenv
127 | _ :: _ as nI -> nI
128 in
129 Coqloadpath.init_load_path ~coqenv, nI
130 in
131 let () = Nativelib.include_dirs := native_ml_path in
132 let ml_path = opts.pre.ml_includes @ boot_ml_path in
133 List.iter Mltop.add_ml_dir (List.rev ml_path);
134 List.iter Loadpath.add_vo_path boot_vo_path;
135 let env_ocamlpath =
136 try [Sys.getenv "OCAMLPATH"]
137 with Not_found -> []
138 in
139 let env_ocamlpath = ml_path @ env_ocamlpath in
140 let ocamlpathsep = if Sys.unix then ":" else ";" in
141 let env_ocamlpath = String.concat ocamlpathsep env_ocamlpath in
142 Findlib.init ~env_ocamlpath ()
143
144 let init_profile ~file =
145 let ch = open_out file in
146 let fname = Filename.basename file in
147 NewProfile.init { output = Format.formatter_of_out_channel ch; fname; };
148 at_exit (fun () ->
149 NewProfile.finish ();
150 close_out ch)
151
152 let init_runtime ~usage opts =
153 let open Coqargs in
154 Vernacextend.static_linking_done ();
155 Option.iter (fun file -> init_profile ~file) opts.config.profile;
156 Lib.init ();
157 if opts.post.memory_stat then at_exit print_memory_stat;
158
159 (* excluded directories *)
160 List.iter System.exclude_directory opts.config.exclude_dirs;
161
162 let coqenv = boot_env usage opts in
163
164 (* Paths for loading stuff *)
165 init_load_paths coqenv opts;
166
167 ()
168
169 let init_document opts =
170 let open Coqargs in
171
172 (* this isn't in init_load_paths because processes (typically
173 vscoqtop) are allowed to have states with differing vo paths (but
174 not with differing -boot or ml paths) *)
175 List.iter (fun x -> Loadpath.add_vo_path @@ to_vo_path x) opts.pre.vo_includes;
176
177 (* Kernel configuration *)
178 Global.set_impredicative_set opts.config.logic.impredicative_set;
179 Global.set_indices_matter opts.config.logic.indices_matter;
180 Global.set_check_universes (not opts.config.logic.type_in_type);
181 Global.set_VM opts.config.enable_VM;
182 Global.set_native_compiler (match opts.config.native_compiler with NativeOff -> false | NativeOn _ -> true);
183 Global.set_rewrite_rules_allowed opts.config.logic.rewrite_rules;
184
185 (* XXX these flags should probably be in the summary *)
186 (* Native output dir *)
187 Nativelib.output_dir := opts.config.native_output_dir;
188
189 (* Default output dir *)
190 System.output_directory := opts.config.output_directory;
191
192 (* Test mode *)
193 Flags.test_mode := opts.config.test_mode;
194
195 (* beautify *)
196 if opts.config.beautify then begin
197 Flags.beautify := true;
198 CLexer.record_comments := true;
199 end;
200
201 if opts.config.quiet then begin
202 Flags.quiet := true;
203 end;
204
205 ()
206
207 let warn_require_not_found =
208 CWarnings.create ~name:"compatibility-module-not-found"
209 ~category:CWarnings.CoreCategories.filesystem
210 Pp.(fun (prefix, lib) ->
211 strbrk "Did not find compatibility module " ++ Libnames.pr_qualid lib ++
212 pr_opt (fun prefix -> str "with prefix " ++ Libnames.pr_qualid prefix)
213 prefix ++ str ".")
214
215 let require_file ~intern ~prefix ~lib ~export ~allow_failure =
216 let mp = Libnames.qualid_of_string lib in
217 let mfrom = Option.map Libnames.qualid_of_string prefix in
218 let exp = Option.map (function
219 | Coqargs.Import -> Vernacexpr.Import, None
220 | Coqargs.Export -> Vernacexpr.Export, None)
221 export
222 in
223 try
224 Flags.silently (Vernacentries.vernac_require ~intern mfrom exp) [mp,Vernacexpr.ImportAll]
225 with (Synterp.UnmappedLibrary _ | Synterp.NotFoundLibrary _) when allow_failure ->
226 warn_require_not_found (mfrom, mp)
227
228 let warn_no_native_compiler =
229 CWarnings.create_in Nativeconv.w_native_disabled
230 Pp.(fun s -> strbrk "Native compiler is disabled," ++
231 strbrk " -native-compiler " ++ strbrk s ++
232 strbrk " option ignored.")
233
234 let warn_deprecated_native_compiler =
235 CWarnings.create ~name:"deprecated-native-compiler-option" ~category:Deprecation.Version.v8_14
236 (fun () ->
237 Pp.strbrk "The native-compiler option is deprecated. To compile native \
238 files ahead of time, use the coqnative binary instead.")
239
240 let interp_set_option (type a) opt (v:string) (k:a Goptions.option_kind) : a =
241 let opt = String.concat " " opt in
242 match k with
243 | BoolKind -> (Coqargs.get_bool ~opt v)
244 | IntKind -> (Coqargs.get_int_opt ~opt v)
245 | StringKind -> v
246 | StringOptKind -> Some v
247
248 let interp_set_option opt =
249 { Goptions.check_and_cast = (fun v k -> interp_set_option opt v k ) }
250
251 let set_option (opt,v) =
252 let open Goptions in
253 match (v:Coqargs.option_command) with
254 | OptionUnset -> unset_option_value_gen ~locality:OptLocal opt
255 | OptionSet None -> set_bool_option_value_gen ~locality:OptLocal opt true
256 | OptionSet (Some v) -> set_option_value ~locality:OptLocal (interp_set_option opt) opt v
257
258 let handle_injection ~intern = let open Coqargs in function
259 | RequireInjection {lib;prefix;export;allow_failure} ->
260 require_file ~intern ~lib ~prefix ~export ~allow_failure
261 | OptionInjection o -> set_option o
262 | WarnNoNative s -> warn_no_native_compiler s
263 | WarnNativeDeprecated -> warn_deprecated_native_compiler ()
264
265 let start_library ~intern ~top injections =
266 Flags.verbosely Declaremods.start_library top;
267 CWarnings.override_unknown_warning[@ocaml.warning "-3"] := true;
268 List.iter (handle_injection ~intern) injections;
269 CWarnings.override_unknown_warning[@ocaml.warning "-3"] := false