Skip to content

Commit

Permalink
[Sites] delay the computation of content of generate_sites_module
Browse files Browse the repository at this point in the history
        Fixes ocaml#4415

Signed-off-by: François Bobot <[email protected]>
  • Loading branch information
bobot committed Jun 24, 2021
1 parent 1582055 commit 58ef72b
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 25 deletions.
5 changes: 0 additions & 5 deletions otherlibs/site/test/run.t
Original file line number Diff line number Diff line change
Expand Up @@ -155,11 +155,6 @@ Test with an opam like installation
]

$ dune build -p a @install
File "d/dune", line 5, characters 45-46:
5 | (generate_sites_module (module sites) (sites d))
^
Error: dune_site used outside a package
[1]



Expand Down
45 changes: 25 additions & 20 deletions src/dune_rules/generate_sites_module_rules.ml
Original file line number Diff line number Diff line change
Expand Up @@ -57,30 +57,35 @@ let plugins_code sctx buf pkg sites =
(String.capitalize site) plugins plugins site)

let setup_rules sctx ~dir (def : Dune_file.Generate_sites_module.t) =
let buf = Buffer.create 1024 in
if def.sourceroot then sourceroot_code buf;
if def.relocatable then relocatable_code buf;
let sites =
List.sort_uniq
~compare:(fun (_, pkga) (_, pkgb) -> Package.Name.compare pkga pkgb)
(def.sites @ List.map ~f:(fun (loc, (pkg, _)) -> (loc, pkg)) def.plugins)
let impl () =
let buf = Buffer.create 1024 in
if def.sourceroot then sourceroot_code buf;
if def.relocatable then relocatable_code buf;
let sites =
List.sort_uniq
~compare:(fun (_, pkga) (_, pkgb) -> Package.Name.compare pkga pkgb)
(def.sites @ List.map ~f:(fun (loc, (pkg, _)) -> (loc, pkg)) def.plugins)
in
if List.is_non_empty sites then (
pr buf "module Sites = struct";
List.iter sites ~f:(sites_code sctx buf);
pr buf "end"
);
let plugins =
Package.Name.Map.of_list_multi (List.map ~f:snd def.plugins)
in
if not (Package.Name.Map.is_empty plugins) then (
pr buf "module Plugins = struct";
Package.Name.Map.iteri plugins ~f:(plugins_code sctx buf);
pr buf "end"
);
Buffer.contents buf
in
if List.is_non_empty sites then (
pr buf "module Sites = struct";
List.iter sites ~f:(sites_code sctx buf);
pr buf "end"
);
let plugins = Package.Name.Map.of_list_multi (List.map ~f:snd def.plugins) in
if not (Package.Name.Map.is_empty plugins) then (
pr buf "module Plugins = struct";
Package.Name.Map.iteri plugins ~f:(plugins_code sctx buf);
pr buf "end"
);
let impl = Buffer.contents buf in
let module_ = Module_name.to_string def.module_ ^ ".ml" in
let file = Path.Build.relative dir module_ in
let open Memo.Build.O in
let+ () =
Super_context.add_rule sctx ~dir (Action_builder.write_file file impl)
Super_context.add_rule sctx ~dir
(Action_builder.write_file_dyn file (Action_builder.delayed impl))
in
module_

0 comments on commit 58ef72b

Please sign in to comment.