Skip to content

Commit 76958c5

Browse files
committed
Generate [_CoqProject] files for Coq theories.
Signed-off-by: Rodolphe Lepigre <[email protected]>
1 parent 82edd33 commit 76958c5

File tree

1 file changed

+143
-5
lines changed

1 file changed

+143
-5
lines changed

src/dune_rules/coq/coq_rules.ml

Lines changed: 143 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -486,6 +486,131 @@ let dep_theory_file ~dir ~wrapper_name =
486486
|> Path.Build.set_extension ~ext:".theory.d"
487487
;;
488488

489+
let theory_coq_args
490+
~sctx
491+
~dir
492+
~wrapper_name
493+
~boot_flags
494+
~stanza_flags
495+
~ml_flags
496+
~theories_deps
497+
~theory_dirs
498+
=
499+
let+ coq_stanza_flags =
500+
let+ expander = Super_context.expander sctx ~dir in
501+
let coq_flags =
502+
let coq_flags = coq_flags ~expander ~dir ~stanza_flags ~per_file_flags:None in
503+
(* By default we have the -q flag. We don't want to pass this to coqtop to
504+
allow users to load their .coqrc files for interactive development.
505+
Therefore we manually scrub the -q setting when passing arguments to
506+
coqtop. *)
507+
let rec remove_q = function
508+
| "-q" :: l -> remove_q l
509+
| x :: l -> x :: remove_q l
510+
| [] -> []
511+
in
512+
let open Action_builder.O in
513+
coq_flags >>| remove_q
514+
in
515+
Command.Args.dyn coq_flags (* stanza flags *)
516+
in
517+
let coq_native_flags =
518+
let mode = Coq_mode.VoOnly in
519+
coqc_native_flags ~sctx ~dir ~theories_deps ~theory_dirs ~mode
520+
in
521+
let file_flags = coqc_file_flags ~dir ~theories_deps ~wrapper_name ~ml_flags in
522+
[ coq_stanza_flags; coq_native_flags; Dyn boot_flags; S file_flags ]
523+
;;
524+
525+
let setup_coqproject_for_theory_rule
526+
~scope
527+
~sctx
528+
~dir
529+
~loc
530+
~theories_deps
531+
~wrapper_name
532+
~use_stdlib
533+
~ml_flags
534+
~coq_lang_version
535+
~stanza_flags
536+
~theory_dirs
537+
coq_modules
538+
=
539+
(* Process coqdep and generate rules *)
540+
let boot_type =
541+
match coq_modules with
542+
| [] -> Resolve.Memo.return Bootstrap.empty
543+
| m :: _ -> Bootstrap.make ~scope ~use_stdlib ~wrapper_name ~coq_lang_version m
544+
in
545+
let boot_flags = Resolve.Memo.read boot_type |> Action_builder.map ~f:Bootstrap.flags in
546+
let* args =
547+
theory_coq_args
548+
~sctx
549+
~dir
550+
~wrapper_name
551+
~boot_flags
552+
~stanza_flags
553+
~ml_flags
554+
~theories_deps
555+
~theory_dirs
556+
in
557+
let contents : string With_targets.t =
558+
let open With_targets.O in
559+
let dir = Path.build dir in
560+
let+ args_bld = Command.expand ~dir (Command.Args.S args)
561+
and+ args_src =
562+
let dir = Path.source (Path.drop_build_context_exn dir) in
563+
Command.expand ~dir (Command.Args.S args)
564+
in
565+
let contents = Buffer.create 73 in
566+
let rec add_args args_bld args_src =
567+
match args_bld, args_src with
568+
| (("-R" | "-Q") as o) :: db :: mb :: args_bld, _ :: ds :: ms :: args_src ->
569+
Buffer.add_string contents o;
570+
Buffer.add_char contents ' ';
571+
Buffer.add_string contents db;
572+
Buffer.add_char contents ' ';
573+
Buffer.add_string contents mb;
574+
Buffer.add_char contents '\n';
575+
if db <> ds
576+
then (
577+
Buffer.add_string contents o;
578+
Buffer.add_char contents ' ';
579+
Buffer.add_string contents ds;
580+
Buffer.add_char contents ' ';
581+
Buffer.add_string contents ms;
582+
Buffer.add_char contents '\n');
583+
add_args args_bld args_src
584+
| "-I" :: _ :: args_bld, "-I" :: d :: args_src ->
585+
Buffer.add_string contents "-I ";
586+
Buffer.add_string contents d;
587+
Buffer.add_char contents '\n';
588+
add_args args_bld args_src
589+
| o :: args_bld, _ :: args_src ->
590+
Buffer.add_string contents "-arg ";
591+
Buffer.add_string contents o;
592+
Buffer.add_char contents '\n';
593+
add_args args_bld args_src
594+
| [], [] -> ()
595+
| _, _ -> assert false
596+
in
597+
add_args args_bld args_src;
598+
Buffer.contents contents
599+
in
600+
let mode =
601+
let open Rule.Promote in
602+
let lifetime = Lifetime.Until_clean in
603+
Rule.Mode.Promote { lifetime; into = None; only = None }
604+
in
605+
let coqproject = Path.Build.relative dir "_CoqProject" in
606+
Super_context.add_rule
607+
~mode
608+
~loc
609+
sctx
610+
~dir
611+
(Action_builder.write_file_dyn coqproject contents.build)
612+
;;
613+
489614
let setup_coqdep_for_theory_rule
490615
~sctx
491616
~dir
@@ -989,18 +1114,31 @@ let setup_theory_rules ~sctx ~dir ~dir_contents (s : Coq_stanza.Theory.t) =
9891114
| m :: _ -> Bootstrap.make ~scope ~use_stdlib ~wrapper_name ~coq_lang_version m
9901115
in
9911116
let boot_flags = Resolve.Memo.read boot_type |> Action_builder.map ~f:Bootstrap.flags in
992-
setup_coqdep_for_theory_rule
1117+
setup_coqproject_for_theory_rule
1118+
~scope
9931119
~sctx
9941120
~dir
9951121
~loc
9961122
~theories_deps
9971123
~wrapper_name
998-
~source_rule
1124+
~use_stdlib
9991125
~ml_flags
1000-
~mlpack_rule
1001-
~boot_flags
1002-
~stanza_coqdep_flags:s.coqdep_flags
1126+
~coq_lang_version
1127+
~stanza_flags
1128+
~theory_dirs
10031129
coq_modules
1130+
>>> setup_coqdep_for_theory_rule
1131+
~sctx
1132+
~dir
1133+
~loc
1134+
~theories_deps
1135+
~wrapper_name
1136+
~source_rule
1137+
~ml_flags
1138+
~mlpack_rule
1139+
~boot_flags
1140+
~stanza_coqdep_flags:s.coqdep_flags
1141+
coq_modules
10041142
>>> Memo.parallel_iter
10051143
coq_modules
10061144
~f:

0 commit comments

Comments
 (0)