Skip to content

Commit

Permalink
Change Meta.load to return the simplified meta
Browse files Browse the repository at this point in the history
It's always simplified anyway
  • Loading branch information
rgrinberg committed Mar 23, 2018
1 parent ab37e8e commit fae5e78
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 35 deletions.
22 changes: 7 additions & 15 deletions src/findlib.ml
Original file line number Diff line number Diff line change
Expand Up @@ -99,11 +99,7 @@ module Config = struct
if not (Path.exists conf_file) then
die "@{<error>Error@}: ocamlfind toolchain %s isn't defined in %a \
(context: %s)" toolchain Path.pp path context;
let vars =
(Meta.simplify { name = ""
; entries = Meta.load (Path.to_string conf_file)
}).vars
in
let vars = (Meta.load ~name:"" ~fn:(Path.to_string conf_file)).vars in
{ vars = String_map.map vars ~f:Rules.of_meta_rules
; preds = Ps.make [toolchain]
}
Expand Down Expand Up @@ -167,7 +163,7 @@ end
type t =
{ stdlib_dir : Path.t
; path : Path.t list
; builtins : Meta.t String_map.t
; builtins : Meta.Simplified.t String_map.t
; packages : (string, (Package.t, Unavailable_reason.t) result) Hashtbl.t
}

Expand Down Expand Up @@ -233,7 +229,7 @@ let parse_package t ~meta_file ~name ~parent_dir ~vars =

(* Parse all the packages defined in a META file and add them to
[t.packages] *)
let parse_and_acknowledge_meta t ~dir ~meta_file (meta : Meta.t) =
let parse_and_acknowledge_meta t ~dir ~meta_file (meta : Meta.Simplified.t) =
let rec loop ~dir ~full_name (meta : Meta.Simplified.t) =
let vars = String_map.map meta.vars ~f:Rules.of_meta_rules in
let dir, res =
Expand All @@ -243,32 +239,28 @@ let parse_and_acknowledge_meta t ~dir ~meta_file (meta : Meta.t) =
List.iter meta.subs ~f:(fun (meta : Meta.Simplified.t) ->
loop ~dir ~full_name:(sprintf "%s.%s" full_name meta.name) meta)
in
loop ~dir ~full_name:meta.name (Meta.simplify meta)
loop ~dir ~full_name:meta.name meta

(* Search for a <package>/META file in the findlib search path, parse
it and add its contents to [t.packages] *)
let find_and_acknowledge_meta t ~fq_name =
let root_name = root_package_name fq_name in
let rec loop dirs : (Path.t * Path.t * Meta.t) option =
let rec loop dirs : (Path.t * Path.t * Meta.Simplified.t) option =
match dirs with
| dir :: dirs ->
let sub_dir = Path.relative dir root_name in
let fn = Path.relative sub_dir "META" in
if Path.exists fn then
Some (sub_dir,
fn,
{ name = root_name
; entries = Meta.load (Path.to_string fn)
})
Meta.load ~name:root_name ~fn:(Path.to_string fn))
else
(* Alternative layout *)
let fn = Path.relative dir ("META." ^ root_name) in
if Path.exists fn then
Some (dir,
fn,
{ name = root_name
; entries = Meta.load (Path.to_string fn)
})
Meta.load ~fn:(Path.to_string fn) ~name:root_name)
else
loop dirs
| [] ->
Expand Down
9 changes: 8 additions & 1 deletion src/import.ml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,14 @@ let ksprintf = Printf.ksprintf
let initial_cwd = Sys.getcwd ()

module String_set = Set.Make(String)
module String_map = Map.Make(String)
module String_map = struct
include Map.Make(String)

let pp f fmt t =
Format.pp_print_list (fun fmt (k, v) ->
Format.fprintf fmt "@[<hov 2>(%s@ =@ %a)@]" k f v
) fmt (to_list t)
end

module Int_set = Set.Make(Int)
module Int_map = Map.Make(Int)
Expand Down
14 changes: 9 additions & 5 deletions src/meta.ml
Original file line number Diff line number Diff line change
Expand Up @@ -102,10 +102,6 @@ module Parse = struct
error lb "'package' or variable name expected"
end

let load fn =
Io.with_lexbuf_from_file fn ~f:(fun lb ->
Parse.entries lb 0 [])

module Simplified = struct
module Rules = struct
type t =
Expand Down Expand Up @@ -145,6 +141,14 @@ let rec simplify t =
in
{ pkg with vars = String_map.add pkg.vars rule.var rules })

let load ~fn ~name =
{ name
; entries =
Io.with_lexbuf_from_file fn ~f:(fun lb ->
Parse.entries lb 0 [])
}
|> simplify

let rule var predicates action value =
Rule { var; predicates; action; value }
let requires ?(preds=[]) pkgs =
Expand Down Expand Up @@ -225,7 +229,7 @@ let builtins ~stdlib_dir =
else
[ compiler_libs; str; unix; bigarray; threads ]
in
List.map libs ~f:(fun t -> t.name, t)
List.map libs ~f:(fun t -> t.name, simplify t)
|> String_map.of_list_exn

let string_of_action = function
Expand Down
6 changes: 2 additions & 4 deletions src/meta.mli
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,6 @@ and predicate =
| Pos of string
| Neg of string

val load : string -> entry list

module Simplified : sig
module Rules : sig
type t =
Expand All @@ -42,10 +40,10 @@ module Simplified : sig
}
end

val simplify : t -> Simplified.t
val load : fn:string -> name:string -> Simplified.t

(** Builtin META files for libraries distributed with the compiler. For when ocamlfind is
not installed. *)
val builtins : stdlib_dir:Path.t -> t String_map.t
val builtins : stdlib_dir:Path.t -> Simplified.t String_map.t

val pp : Format.formatter -> entry list -> unit
24 changes: 14 additions & 10 deletions test/unit-tests/tests.mlt
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ let print_pkg ppf pkg =
;;

#install_printer print_pkg;;
#install_printer String_map.pp;;

[%%expect{|
val print_pkg : Format.formatter -> Jbuilder.Findlib.Package.t -> unit =
Expand Down Expand Up @@ -50,16 +51,19 @@ Findlib.Package.requires pkg;;
open Meta

let meta =
{ name = "foo"
; entries = Meta.load "test/unit-tests/findlib-db/foo/META"
}
Meta.load ~name:"foo" ~fn:"test/unit-tests/findlib-db/foo/META"

[%%expect{|
val meta : Jbuilder.Meta.t =
{name = "foo";
entries =
[Rule {var = "requires"; predicates = []; action = Set; value = "bar"};
Rule
{var = "requires"; predicates = [Pos "ppx_driver"]; action = Set;
value = "baz"}]}
val meta : Jbuilder.Meta.Simplified.t =
{Jbuilder.Meta.Simplified.name = "foo";
vars =
(requires =
{Jbuilder.Meta.Simplified.Rules.set_rules =
[{Jbuilder__Meta.var = "requires"; predicates = [];
action = Jbuilder__Meta.Set; value = "bar"};
{Jbuilder__Meta.var = "requires";
predicates = [Jbuilder__Meta.Pos "ppx_driver"];
action = Jbuilder__Meta.Set; value = "baz"}];
add_rules = []});
subs = []}
|}]

0 comments on commit fae5e78

Please sign in to comment.