Skip to content

Commit

Permalink
refactor(rules): simplify project/package map
Browse files Browse the repository at this point in the history
move into the library database as it's the only place where it's being
used.

Signed-off-by: Rudi Grinberg <me@rgrinberg.com>

ps-id: 5A151254-FE73-4D1F-A269-59FB869A95A9
  • Loading branch information
rgrinberg committed May 30, 2022
1 parent fd35b56 commit ac7beac
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 50 deletions.
28 changes: 19 additions & 9 deletions src/dune_rules/lib.ml
Original file line number Diff line number Diff line change
Expand Up @@ -376,7 +376,6 @@ type db =
; instrument_with : Lib_name.t list
; modules_of_lib :
(dir:Path.Build.t -> name:Lib_name.t -> Modules.t Memo.t) Fdecl.t
; projects_by_package : Dune_project.t Package.Name.Map.t
}

and resolve_result =
Expand Down Expand Up @@ -834,6 +833,18 @@ module rec Resolve_names : sig
end = struct
open Resolve_names

let projects_by_package =
Memo.lazy_ (fun () ->
let open Memo.O in
let+ conf = Dune_load.load () in
List.concat_map conf.projects ~f:(fun project ->
Dune_project.packages project
|> Package.Name.Map.values
|> List.map ~f:(fun (pkg : Package.t) ->
let name = Package.name pkg in
(name, project)))
|> Package.Name.Map.of_list_exn)

let instantiate_impl (db, name, info, hidden) =
let open Memo.O in
let unique_id = Id.make ~name ~path:(Lib_info.src_dir info) in
Expand Down Expand Up @@ -962,14 +973,15 @@ end = struct
in
let requires = map_error requires in
let ppx_runtime_deps = map_error ppx_runtime_deps in
let project =
let* project =
let status = Lib_info.status info in
match Lib_info.Status.project status with
| Some _ as project -> project
| Some _ as project -> Memo.return project
| None ->
let+ projects_by_package = Memo.Lazy.force projects_by_package in
let open Option.O in
let* package = Lib_info.package info in
Package.Name.Map.find db.projects_by_package package
Package.Name.Map.find projects_by_package package
in
let modules =
match Path.as_in_build_dir (Lib_info.src_dir info) with
Expand Down Expand Up @@ -1692,19 +1704,17 @@ module DB = struct

type t = db

let create ~parent ~resolve ~projects_by_package ~all ~modules_of_lib
~lib_config () =
let create ~parent ~resolve ~all ~modules_of_lib ~lib_config () =
{ parent
; resolve
; all = Memo.lazy_ all
; lib_config
; instrument_with = lib_config.Lib_config.instrument_with
; projects_by_package
; modules_of_lib
}

let create_from_findlib ~lib_config ~projects_by_package findlib =
create () ~parent:None ~lib_config ~projects_by_package
let create_from_findlib ~lib_config findlib =
create () ~parent:None ~lib_config
~modules_of_lib:
(let t = Fdecl.create Dyn.opaque in
Fdecl.set t (fun ~dir ~name ->
Expand Down
7 changes: 1 addition & 6 deletions src/dune_rules/lib.mli
Original file line number Diff line number Diff line change
Expand Up @@ -126,19 +126,14 @@ module DB : sig
val create :
parent:t option
-> resolve:(Lib_name.t -> Resolve_result.t Memo.t)
-> projects_by_package:Dune_project.t Package.Name.Map.t
-> all:(unit -> Lib_name.t list Memo.t)
-> modules_of_lib:
(dir:Path.Build.t -> name:Lib_name.t -> Modules.t Memo.t) Fdecl.t
-> lib_config:Lib_config.t
-> unit
-> t

val create_from_findlib :
lib_config:Lib_config.t
-> projects_by_package:Dune_project.t Package.Name.Map.t
-> Findlib.t
-> t
val create_from_findlib : lib_config:Lib_config.t -> Findlib.t -> t

val find : t -> Lib_name.t -> lib option Memo.t

Expand Down
34 changes: 15 additions & 19 deletions src/dune_rules/scope.ml
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,7 @@ module DB = struct
| Deprecated_library_name of Dune_file.Deprecated_library_name.t
end

let create_db_from_stanzas ~parent ~lib_config ~modules_of_lib
~projects_by_package stanzas =
let create_db_from_stanzas ~parent ~lib_config ~modules_of_lib stanzas =
let open Memo.O in
let+ (map : Found_or_redirect.t Lib_name.Map.t) =
Memo.List.map stanzas ~f:(fun stanza ->
Expand Down Expand Up @@ -92,7 +91,7 @@ module DB = struct
; Pp.textf "- %s" (Loc.to_file_colon_line loc2)
])
in
Lib.DB.create () ~parent:(Some parent) ~projects_by_package
Lib.DB.create () ~parent:(Some parent)
~resolve:(fun name ->
Memo.return
(match Lib_name.Map.find map name with
Expand Down Expand Up @@ -133,8 +132,7 @@ module DB = struct
| Some (Name name) -> Lib.DB.Resolve_result.redirect None name

(* Create a database from the public libraries defined in the stanzas *)
let public_libs t ~installed_libs ~modules_of_lib ~lib_config
~projects_by_package stanzas =
let public_libs t ~installed_libs ~modules_of_lib ~lib_config stanzas =
let public_libs =
List.filter_map stanzas ~f:(fun (stanza : Library_related_stanza.t) ->
match stanza with
Expand Down Expand Up @@ -171,14 +169,13 @@ module DB = struct
in
let resolve lib = Memo.return (resolve t public_libs lib) in
Lib.DB.create ~parent:(Some installed_libs) ~resolve ~modules_of_lib
~projects_by_package
~all:(fun () -> Lib_name.Map.keys public_libs |> Memo.return)
~lib_config ()

module Path_source_map_traversals = Memo.Make_map_traversals (Path.Source.Map)

let scopes_by_dir context ~projects_by_package ~modules_of_lib ~projects
~public_libs stanzas coq_stanzas =
let scopes_by_dir context ~modules_of_lib ~projects ~public_libs stanzas
coq_stanzas =
let open Memo.O in
let projects_by_dir =
List.map projects ~f:(fun (project : Dune_project.t) ->
Expand Down Expand Up @@ -214,7 +211,7 @@ module DB = struct
~f:(fun _dir (project, stanzas) ->
let+ db =
create_db_from_stanzas stanzas ~parent:public_libs
~modules_of_lib ~projects_by_package ~lib_config
~modules_of_lib ~lib_config
in
(project, db))
in
Expand All @@ -231,18 +228,17 @@ module DB = struct
in
Some { project; db; coq_db; root })

let create ~projects_by_package ~context ~installed_libs ~modules_of_lib
~projects stanzas coq_stanzas =
let create ~context ~installed_libs ~modules_of_lib ~projects stanzas
coq_stanzas =
let open Memo.O in
let t = Fdecl.create Dyn.opaque in
let public_libs =
let lib_config = Context.lib_config context in
public_libs t ~installed_libs ~lib_config ~projects_by_package
~modules_of_lib stanzas
public_libs t ~installed_libs ~lib_config ~modules_of_lib stanzas
in
let+ by_dir =
scopes_by_dir context ~projects ~projects_by_package ~public_libs
~modules_of_lib stanzas coq_stanzas
scopes_by_dir context ~projects ~public_libs ~modules_of_lib stanzas
coq_stanzas
in
let value = { by_dir } in
Fdecl.set t value;
Expand All @@ -254,8 +250,8 @@ module DB = struct
[ ("dir", Path.Build.to_dyn dir) ];
find_by_dir t (Path.Build.drop_build_context_exn dir)

let create_from_stanzas ~projects ~projects_by_package ~context
~installed_libs ~modules_of_lib stanzas =
let create_from_stanzas ~projects ~context ~installed_libs ~modules_of_lib
stanzas =
let stanzas, coq_stanzas =
Dune_file.fold_stanzas stanzas ~init:([], [])
~f:(fun dune_file stanza (acc, coq_acc) ->
Expand All @@ -275,6 +271,6 @@ module DB = struct
(acc, (ctx_dir, coq_lib) :: coq_acc)
| _ -> (acc, coq_acc))
in
create ~projects ~context ~installed_libs ~modules_of_lib
~projects_by_package stanzas coq_stanzas
create ~projects ~context ~installed_libs ~modules_of_lib stanzas
coq_stanzas
end
1 change: 0 additions & 1 deletion src/dune_rules/scope.mli
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ module DB : sig
(** Return the new scope database as well as the public libraries database *)
val create_from_stanzas :
projects:Dune_project.t list
-> projects_by_package:Dune_project.t Package.Name.Map.t
-> context:Context.t
-> installed_libs:Lib.DB.t
-> modules_of_lib:
Expand Down
18 changes: 3 additions & 15 deletions src/dune_rules/super_context.ml
Original file line number Diff line number Diff line change
Expand Up @@ -563,27 +563,15 @@ let create_lib_entries_by_package ~public_libs stanzas =
(List.sort ~compare:(fun a b ->
Lib_name.compare (Lib_entry.name a) (Lib_entry.name b)))

let create_projects_by_package projects : Dune_project.t Package.Name.Map.t =
List.concat_map projects ~f:(fun project ->
Dune_project.packages project
|> Package.Name.Map.values
|> List.map ~f:(fun (pkg : Package.t) ->
let name = Package.name pkg in
(name, project)))
|> Package.Name.Map.of_list_exn

let modules_of_lib = Fdecl.create Dyn.opaque

let create ~(context : Context.t) ~host ~projects ~packages ~stanzas =
let lib_config = Context.lib_config context in
let projects_by_package = create_projects_by_package projects in
let installed_libs =
Lib.DB.create_from_findlib context.findlib ~lib_config ~projects_by_package
in
let installed_libs = Lib.DB.create_from_findlib context.findlib ~lib_config in
let modules_of_lib_for_scope = Fdecl.create Dyn.opaque in
let* scopes, public_libs =
Scope.DB.create_from_stanzas ~projects ~projects_by_package ~context
~installed_libs ~modules_of_lib:modules_of_lib_for_scope stanzas
Scope.DB.create_from_stanzas ~projects ~context ~installed_libs
~modules_of_lib:modules_of_lib_for_scope stanzas
in
let stanzas =
List.map stanzas ~f:(fun { Dune_file.dir; project; stanzas } ->
Expand Down

0 comments on commit ac7beac

Please sign in to comment.