Skip to content

Commit

Permalink
refactor: introduce simpler env stanza db
Browse files Browse the repository at this point in the history
Signed-off-by: Rudi Grinberg <me@rgrinberg.com>

<!-- ps-id: 29fc65d4-e525-435a-8fd5-c692f204f7b5 -->
  • Loading branch information
rgrinberg committed Sep 28, 2023
1 parent 678e8f9 commit 8b810e9
Show file tree
Hide file tree
Showing 8 changed files with 94 additions and 16 deletions.
2 changes: 1 addition & 1 deletion src/dune_rules/compilation_context.ml
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ let create
and+ bin_annot =
match bin_annot with
| Some b -> Memo.return b
| None -> Super_context.bin_annot super_context ~dir:(Obj_dir.dir obj_dir)
| None -> Env_stanza_db.bin_annot ~dir:(Obj_dir.dir obj_dir)
in
{ super_context
; scope
Expand Down
8 changes: 0 additions & 8 deletions src/dune_rules/env_node.ml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ type t =
; js_of_ocaml : string list Action_builder.t Js_of_ocaml.Env.t Memo.Lazy.t
; coq : Coq.t Action_builder.t Memo.Lazy.t
; format_config : Format_config.t Memo.Lazy.t
; bin_annot : bool Memo.Lazy.t
}

let scope t = t.scope
Expand All @@ -47,7 +46,6 @@ let set_format_config t format_config =

let odoc t = Memo.Lazy.force t.odoc
let coq t = Memo.Lazy.force t.coq
let bin_annot t = Memo.Lazy.force t.bin_annot

let expand_str_lazy expander sw =
match String_with_vars.text_only sw with
Expand All @@ -70,7 +68,6 @@ let make
~default_context_flags
~default_env
~default_artifacts
~default_bin_annot
=
let open Memo.O in
let config = Dune_env.Stanza.find config_stanza ~profile in
Expand Down Expand Up @@ -225,10 +222,6 @@ let make
"format config should always have a default value taken from the project root"
[])
in
let bin_annot =
inherited ~field:bin_annot ~root:default_bin_annot (fun default ->
Memo.return (Option.value ~default config.bin_annot))
in
{ scope
; ocaml_flags
; foreign_flags
Expand All @@ -242,6 +235,5 @@ let make
; odoc
; coq
; format_config
; bin_annot
}
;;
2 changes: 0 additions & 2 deletions src/dune_rules/env_node.mli
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ val make
-> default_context_flags:string list Action_builder.t Foreign_language.Dict.t
-> default_env:Env.t
-> default_artifacts:Artifacts.t
-> default_bin_annot:bool
-> t

val scope : t -> Scope.t
Expand All @@ -45,4 +44,3 @@ val coq : t -> Coq.t Action_builder.t Memo.t
val menhir_flags : t -> string list Action_builder.t
val format_config : t -> Format_config.t Memo.t
val set_format_config : t -> Format_config.t -> t
val bin_annot : t -> bool Memo.t
83 changes: 83 additions & 0 deletions src/dune_rules/env_stanza_db.ml
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
open Import
open Memo.O

module Node = struct
type t =
{ value : Dune_env.Stanza.t
; parent : t option Memo.t
}

let by_context dir =
let open Memo.O in
let+ context = Context.DB.by_dir dir in
let { Context.Env_nodes.context; workspace } = Context.env_nodes context in
match context, workspace with
| None, None -> None
| Some value, None | None, Some value -> Some { value; parent = Memo.return None }
| Some context, Some workspace ->
Some
{ value = context
; parent = Memo.return (Some { value = workspace; parent = Memo.return None })
}
;;

let in_dir ~dir =
let+ stanzas = Only_packages.stanzas_in_dir dir in
match stanzas with
| None -> None
| Some stanzas ->
List.find_map stanzas.stanzas ~f:(function
| Dune_env.T config -> Some config
| _ -> None)
;;

let rec by_dir dir =
let parent =
let* scope = Scope.DB.find_by_dir dir in
if Path.Build.equal dir (Scope.root scope)
then by_context dir
else (
match Path.Build.parent dir with
| None -> by_context dir
| Some parent -> by_dir parent)
in
in_dir ~dir
>>= function
| Some value -> Memo.return (Some { value; parent })
| None -> parent
;;
end

let value ~default ~f =
let rec loop = function
| None -> Memo.return default
| Some { Node.value; parent } ->
let* next =
f value
>>| function
| Some x -> `Ok x
| None -> `Parent
in
(match next with
| `Ok x -> Memo.return x
| `Parent -> parent >>= loop)
in
fun ~dir -> Node.by_dir dir >>= loop
;;

let profile ~dir =
let name, _ = Path.Build.extract_build_context_exn dir in
let context = Context_name.of_string name in
Per_context.profile context
;;

let value ~default ~dir ~f =
let profile = lazy (profile ~dir) in
value ~default ~dir ~f:(fun stanza ->
let+ profile = Lazy.force profile in
Dune_env.Stanza.find_opt stanza ~profile |> Option.bind ~f)
;;

let bin_annot ~dir =
value ~default:true ~dir ~f:(fun (t : Dune_env.Stanza.config) -> t.bin_annot)
;;
9 changes: 9 additions & 0 deletions src/dune_rules/env_stanza_db.mli
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
open Import

val value
: default:'a
-> dir:Path.Build.t
-> f:(Dune_env.Stanza.config -> 'a option)
-> 'a Memo.t

val bin_annot : dir:Path.Build.t -> bool Memo.t
2 changes: 1 addition & 1 deletion src/dune_rules/install_rules.ml
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,7 @@ end = struct
in
let set_dir m = List.map ~f:(fun (cm_kind, p) -> cm_dir m cm_kind, p) in
let+ modules_impl =
let+ bin_annot = Super_context.bin_annot sctx ~dir in
let+ bin_annot = Env_stanza_db.bin_annot ~dir in
List.concat_map installable_modules.impl ~f:(fun m ->
let cmt_files =
match bin_annot with
Expand Down
3 changes: 0 additions & 3 deletions src/dune_rules/super_context.ml
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,6 @@ end = struct
~default_context_flags
~default_env:t.context_env
~default_artifacts:t.artifacts
~default_bin_annot:true
;;

(* Here we jump through some hoops to construct [t] as well as create a
Expand Down Expand Up @@ -285,7 +284,6 @@ let add_alias_action t alias ~dir ~loc action =

let local_binaries t ~dir = Env_tree.get_node t ~dir >>= Env_node.local_binaries
let env_node = Env_tree.get_node
let bin_annot t ~dir = Env_tree.get_node t ~dir >>= Env_node.bin_annot

let dump_env t ~dir =
let node = Env_tree.get_node t ~dir in
Expand Down Expand Up @@ -431,7 +429,6 @@ let make_default_env_node
~default_context_flags
~default_env:root_env
~default_artifacts:artifacts
~default_bin_annot:true
in
make
~config_stanza:env_nodes.context
Expand Down
1 change: 0 additions & 1 deletion src/dune_rules/super_context.mli
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ val context_env : t -> Env.t
val local_binaries : t -> dir:Path.Build.t -> File_binding.Expanded.t list Memo.t

val env_node : t -> dir:Path.Build.t -> Env_node.t Memo.t
val bin_annot : t -> dir:Path.Build.t -> bool Memo.t

(** Dump a directory environment in a readable form *)
val dump_env : t -> dir:Path.Build.t -> Dune_lang.t list Action_builder.t
Expand Down

0 comments on commit 8b810e9

Please sign in to comment.