Debian Codegraph @

tools/dune_rule_gen/coq_rules.ml

raw symbols SCIP
1 (************************************************************************)
2 (* This file is licensed under The MIT License *)
3 (* See LICENSE for more information *)
4 (* (c) MINES ParisTech 2018-2019 *)
5 (* (c) INRIA 2020-2022 *)
6 (* Written by: Emilio Jesús Gallego Arias *)
7 (* Written by: Rudi Grinberg *)
8 (************************************************************************)
9
10 (* coq_rules: generate dune build rules for Coq's test-suite and init *)
11
12 (* Originally written by Emilio Jesús Gallego Arias and Rudi Grinberg
13 for Dune's coq_rules.ml , which followed a first coq_dune.exe
14 implementation by Emilio Jesús Gallego Arias *)
15
16 (* note that this file closely follows Dune's own coq_rules.ml [and
17 eventually they could be merged], hence the license. Other modules
18 such as Arg or Path are straight copies from Dune's *)
19
20 let _debug = false
21
22 module FlagUtil = struct
23
24 (* Native flag *)
25 let findlib_native_dir () =
26 try
27 Findlib.package_directory ("rocq-runtime.kernel") |> Path.make
28 with
29 Fl_package_base.No_such_package (p,_) ->
30 raise (Invalid_argument ("failed to locate Coq kernel package in split build mode: " ^ p))
31
32 let local_native_dir =
33 Path.make "kernel/.kernel.objs/byte"
34
35 let kernel_cmi_dir ~split () =
36 if split then findlib_native_dir () else local_native_dir
37
38 end
39
40 (* dep to vo *)
41 let path_of_dep ~vo_ext dep =
42 let open Coqdeplib.Dep_info in
43 let file = match dep with
44 | Dep.Require dep -> dep ^ vo_ext
45 | Dep.Ml dep-> dep ^ ".cmxs"
46 | Dep.Other dep -> dep
47 in
48 (* when called by dune, the cmxs and META files are in
49 ../install/... relative to cwd (= _build/default) but the
50 generated dune file will be moved to ../theories so adjusting the
51 path to be relative to the .v won't work
52
53 (it would be relative to the .v in
54 workspace_root/_build/default/theories but dune would read it as
55 relative to the .v in project_root/theories, the number of .. to
56 insert to get to project_root doesn't match) *)
57 let file = if CString.is_prefix ".." file then
58 (Filename.concat "%{workspace_root}" "_build")
59 ^ String.sub file 2 (String.length file - 2)
60 else file
61 in
62 Path.make file
63
64 (* dep to cmi, this is hacky, cleanup. A better way is to keep a
65 reverse map from files to modules *)
66 let dot_path s = String.equal s "." || String.equal s ".."
67 let not_dot_path s = not (dot_path s)
68 let translate_to_native ~tname dep =
69 let dir = Filename.dirname dep in
70 let components = String.split_on_char '/' dep |> List.filter not_dot_path in
71 let name = String.concat "_" (tname @ components) in
72 Filename.concat dir ("N" ^ name ^ ".cmi")
73
74 (* Case for prelude.vo *)
75 let not_dot_path_or_coqlib s = not (dot_path s) && not (String.equal "theories" s)
76 let translate_boot_to_native dep =
77 let dep = Path.to_string dep |> Filename.remove_extension in
78 let dir = Filename.dirname dep in
79 let components = String.split_on_char '/' dep |> List.filter not_dot_path_or_coqlib in
80 let name = String.concat "_" ("Corelib"::components) in
81 Filename.concat dir ("N" ^ name ^ ".cmi") |> Path.make
82
83 let cmi_of_dep ~tname dep =
84 let open Coqdeplib.Dep_info in
85 let file = match dep with
86 | Dep.Require dep ->
87 Some (translate_to_native ~tname dep)
88 | Dep.Ml _dep-> None
89 | Dep.Other _ -> None
90 in
91 Option.map Path.make file
92
93 module Theory = struct
94 (** A theory binding; directories should be relative to Coq's
95 sources root *)
96 type t =
97 { directory : Path.t
98 (** Directory of the theory *)
99 ; dirname: string list
100 (** Coq's logical path *)
101 ; implicit : bool
102 (** Use -R or -Q *)
103 ; deps : string list;
104 (** Adds as -Q user-contrib/X X *)
105 }
106
107 let args { directory; dirname; implicit; deps } =
108 let barg = if implicit then "-R" else "-Q" in
109 Arg.[ A barg; Path directory; A (String.concat "." dirname) ]
110 @ List.flatten (deps |> List.map (fun dep ->
111 Arg.[A "-Q"; Path (Path.make @@ "user-contrib"^Filename.dir_sep^dep); A dep]))
112
113 end
114
115 (** [Regular _] contains the info about the core theory, see
116 documentation in the .mli file *)
117 module Boot_type = struct
118 type t = Corelib | NoInit | Regular of { corelib : Theory.t; noinit : bool }
119 end
120
121 (* Context for a Coq theory *)
122 module Context = struct
123
124 module Flags = struct
125 type t =
126 { user : Arg.t list
127 ; loadpath : Arg.t list
128 ; common : Arg.t list
129 ; native_common : Arg.t list
130 ; native_coqc : Arg.t list
131 }
132 end
133
134 type t =
135 { theory : Theory.t
136 ; flags : Flags.t (* flags *)
137 ; rule : Coq_module.Rule_type.t (* rule kind *)
138 ; boot : Boot_type.t (* type of library *)
139 ; dep_info : Dep_info.t
140 ; async_deps : string list (* whether coqc needs the workers *)
141 ; root_lvl : int
142 }
143
144 let native_common ~split () =
145 let path_coq_kernel_cmi = FlagUtil.kernel_cmi_dir ~split () in
146 [ Arg.A "-nI"; Path path_coq_kernel_cmi
147 ; A "-native-output-dir"; A "."
148 ]
149
150 let native_coqc ~native_common ~native =
151 let native_string = if native then "on" else "off" in
152 (if native then native_common else [])
153 @ Arg.[ A "-w"; A "-deprecated-native-compiler-option"
154 ; A "-native-compiler"; A native_string
155 ]
156
157 (* XXX: we could put the workers here, but they need a complete OCaml runtime so this is better *)
158 let build_async_deps = ["(package rocq-runtime)"]
159
160 (* args are for coqdep *)
161 let build_dep_info ~coqdep_args dir_info =
162 Dep_info.make ~args:coqdep_args ~dir_info
163
164 let make ~root_lvl ~theory ~user_flags ~boot ~rule ~async ~dir_info ~split =
165
166 let flags =
167 let boot_paths = match boot with
168 | Boot_type.NoInit -> []
169 | Corelib -> Theory.args theory
170 | Regular { corelib; noinit = _ } -> Theory.args corelib @ Theory.args theory
171 in
172 let loadpath = Arg.(A "-boot") :: boot_paths in
173 let native_common = native_common ~split () in
174 let native_coqc = native_coqc ~native_common ~native:(Coq_module.Rule_type.native_coqc rule) in
175 let common = Arg.[ A "-w"; A "+default"; A "-q" ] in
176 { Flags.user = user_flags; common; loadpath; native_common; native_coqc }
177 in
178
179 (* coqdep and dep info *)
180 let coqdep_args = flags.loadpath in
181 let dep_info = build_dep_info ~coqdep_args dir_info in
182
183 let async_deps = if async then build_async_deps else [] in
184 { theory; flags; rule; boot; dep_info; async_deps; root_lvl }
185
186 end
187
188 (* Return flags and deps to inject *)
189 let prelude_path = "Init/Prelude.vo"
190
191 (* Return extra flags and deps for a concrete file; the case of
192 interest is to determine when a file needs [-nonit]. If it
193 doesn't, we must inject the [Init/Prelude] dependency. Note that
194 we can't compute this in Context.make due to the per-file check for
195 "Init" *)
196 let boot_module_setup ~cctx coq_module =
197 match cctx.Context.boot with
198 | Boot_type.NoInit -> [Arg.A "-noinit"], []
199 | Corelib ->
200 (match Coq_module.prefix coq_module with
201 | ["Init"] -> [ Arg.A "-noinit" ], []
202 | _ -> [ ], [ Path.relative (Path.relative (Path.make "theories") "Corelib") prelude_path ]
203 )
204 | Regular { corelib; noinit } ->
205 if noinit then [ Arg.A "-noinit" ], []
206 else [], [ Path.relative corelib.directory prelude_path ]
207
208 (* rule generation for a module *)
209 let module_rule ~(cctx : Context.t) coq_module =
210 let tname, rule = cctx.theory.dirname, cctx.rule in
211 (* retrieve deps *)
212 let vfile = Coq_module.source coq_module in
213 let vo_ext = ".vo" in
214 let vfile_deps = Dep_info.lookup ~dep_info:cctx.dep_info vfile |> List.map (path_of_dep ~vo_ext) in
215 (* handle -noinit, inject prelude.vo if needed *)
216 let boot_flags, boot_deps = boot_module_setup ~cctx coq_module in
217 let coqc_flags = cctx.flags.loadpath @ cctx.flags.user @ cctx.flags.common @ cctx.flags.native_coqc in
218 let vfile_deps, flags = boot_deps @ vfile_deps, boot_flags @ coqc_flags in
219 let vfile_base = Path.basename vfile in
220 let timeflags = if Coq_module.with_timing then
221 Arg.[A "-time-file"; Path Path.(replace_ext vfile ~ext:".timing")]
222 else []
223 in
224 (* Adjust paths *)
225 let lvl = cctx.root_lvl + (Coq_module.prefix coq_module |> List.length) in
226 let flags = (* flags are relative to the root path *) Arg.List.to_string (flags @ timeflags) in
227 let deps = List.map (Path.adjust ~lvl) vfile_deps |> List.map Path.to_string in
228 (* Depend on the workers if async *)
229 let deps = cctx.async_deps @ deps in
230 (* Build rule *)
231 let updir = Path.(to_string (adjust ~lvl (make "."))) in
232 let action = Format.asprintf "(chdir %s (run rocq c %s %%{dep:%s}))" updir flags vfile_base in
233 let targets = Coq_module.obj_files ~tname ~rule coq_module in
234 let alias = None in
235 { Dune_file.Rule.targets; deps; action; alias }
236
237 (* Helper for Dir_info to Subdir *)
238 let gen_rules ~dir_info ~cctx ~f =
239 let f ~prefix sub_acc mods =
240 let subdir = Coq_module.prefix_to_dir prefix in
241 let payload = List.map (f ~cctx) mods in
242 Dune_file.Subdir.{ subdir; payload } :: sub_acc
243 in
244 Dir_info.fold ~f ~init:[] dir_info
245
246 (* Has to be called in the current dir *)
247 let vo_rules ~dir_info ~cctx = gen_rules ~dir_info ~cctx ~f:module_rule
248
249 (* rule generation for .vo -> .{cmi,cmxs} *)
250 let coqnative_module_rule ~(cctx: Context.t) coq_module =
251 let tname = cctx.theory.dirname in
252 (* deps *)
253 let vfile = Coq_module.source coq_module in
254 let vofile_deps = Dep_info.lookup ~dep_info:cctx.dep_info vfile |> List.filter_map (cmi_of_dep ~tname) in
255 (* base [maybe this should go to coq_module] *)
256 let vofile_base = Path.(replace_ext ~ext:".vo" vfile |> basename) in
257 (* handle -noinit, inject prelude.vo if needed *)
258 let boot_flags, boot_deps = boot_module_setup ~cctx coq_module in
259 (* Improve this code *)
260 let boot_deps = List.map translate_boot_to_native boot_deps in
261 (* Should we pass user and common flags here? They are ignored as of today so we don't *)
262 let flags = boot_flags @ cctx.flags.loadpath @ cctx.flags.native_common in
263 let vofile_deps = boot_deps @ vofile_deps in
264 (* Adjust paths *)
265 let lvl = cctx.root_lvl + (Coq_module.prefix coq_module |> List.length) in
266 let flags = (* flags are relative to the root path *) Arg.List.to_string flags in
267 let deps = List.map (Path.adjust ~lvl) vofile_deps |> List.map Path.to_string in
268 (* Build rule *)
269 let updir = Path.(to_string (adjust ~lvl (make "."))) in
270 let action = Format.asprintf "(chdir %s (run coqnative %s %s))" updir flags vofile_base in
271 let targets = Coq_module.native_obj_files ~tname coq_module in
272 let deps = vofile_base :: deps in
273 let alias = None in
274 { Dune_file.Rule.targets; deps; action; alias }
275
276 let coqnative_rules ~dir_info ~cctx = gen_rules ~dir_info ~cctx ~f:coqnative_module_rule
277
278 let install_rule ~(cctx : Context.t) coq_module =
279 let tname, rule = cctx.theory.dirname, cctx.rule in
280 let dst_base = match tname with
281 | ["Corelib"] -> Filename.concat "coq" "theories"
282 | _ -> Filename.concat "coq" @@ Filename.concat "user-contrib" @@ (String.concat "." tname)
283 in
284 let files =
285 Coq_module.install_files ~tname ~rule coq_module
286 |> List.map (fun (src,dst) -> src, Filename.concat dst_base dst) in
287 (* May need to woraround invalid empty `(install )` stanza if that happens *)
288 Dune_file.Install.{ section = "lib_root"; package = "rocq-core"; files }
289
290 let install_rules ~dir_info ~cctx = gen_rules ~dir_info ~cctx ~f:install_rule