| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
) |
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
g |
| 13 |
|
| 14 |
) |
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
let _debug = false |
| 21 |
|
| 22 |
module FlagUtil = struct |
| 23 |
|
| 24 |
|
| 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 |
|
| 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 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 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 |
|
| 65 |
|
| 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 |
|
| 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 |
|
| 95 |
|
| 96 |
type t = |
| 97 |
{ directory : Path.t |
| 98 |
|
| 99 |
; dirname: string list |
| 100 |
|
| 101 |
; implicit : bool |
| 102 |
|
| 103 |
; deps : string list; |
| 104 |
|
| 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 |
|
| 116 |
|
| 117 |
module Boot_type = struct |
| 118 |
type t = Corelib | NoInit | Regular of { corelib : Theory.t; noinit : bool } |
| 119 |
end |
| 120 |
|
| 121 |
|
| 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 |
| 137 |
; rule : Coq_module.Rule_type.t |
| 138 |
; boot : Boot_type.t |
| 139 |
; dep_info : Dep_info.t |
| 140 |
; async_deps : string list |
| 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 |
|
| 158 |
let build_async_deps = ["(package rocq-runtime)"] |
| 159 |
|
| 160 |
|
| 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 |
|
| 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 |
|
| 189 |
let prelude_path = "Init/Prelude.vo" |
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
|
| 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 |
|
| 209 |
let module_rule ~(cctx : Context.t) coq_module = |
| 210 |
let tname, rule = cctx.theory.dirname, cctx.rule in |
| 211 |
|
| 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 |
|
| 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 |
|
| 225 |
let lvl = cctx.root_lvl + (Coq_module.prefix coq_module |> List.length) in |
| 226 |
let flags = Arg.List.to_string (flags @ timeflags) in |
| 227 |
let deps = List.map (Path.adjust ~lvl) vfile_deps |> List.map Path.to_string in |
| 228 |
|
| 229 |
let deps = cctx.async_deps @ deps in |
| 230 |
|
| 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 |
|
| 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 |
|
| 247 |
let vo_rules ~dir_info ~cctx = gen_rules ~dir_info ~cctx ~f:module_rule |
| 248 |
|
| 249 |
|
| 250 |
let coqnative_module_rule ~(cctx: Context.t) coq_module = |
| 251 |
let tname = cctx.theory.dirname in |
| 252 |
|
| 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 |
|
| 256 |
let vofile_base = Path.(replace_ext ~ext:".vo" vfile |> basename) in |
| 257 |
|
| 258 |
let boot_flags, boot_deps = boot_module_setup ~cctx coq_module in |
| 259 |
|
| 260 |
let boot_deps = List.map translate_boot_to_native boot_deps in |
| 261 |
|
| 262 |
let flags = boot_flags @ cctx.flags.loadpath @ cctx.flags.native_common in |
| 263 |
let vofile_deps = boot_deps @ vofile_deps in |
| 264 |
|
| 265 |
let lvl = cctx.root_lvl + (Coq_module.prefix coq_module |> List.length) in |
| 266 |
let flags = Arg.List.to_string flags in |
| 267 |
let deps = List.map (Path.adjust ~lvl) vofile_deps |> List.map Path.to_string in |
| 268 |
|
| 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 |
|
| 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 |