From fae5e78fc25cc9b947d7f7dd22bad1211aa851bd Mon Sep 17 00:00:00 2001 From: Rudi Grinberg Date: Thu, 22 Mar 2018 16:39:43 +0800 Subject: [PATCH] Change Meta.load to return the simplified meta It's always simplified anyway --- src/findlib.ml | 22 +++++++--------------- src/import.ml | 9 ++++++++- src/meta.ml | 14 +++++++++----- src/meta.mli | 6 ++---- test/unit-tests/tests.mlt | 24 ++++++++++++++---------- 5 files changed, 40 insertions(+), 35 deletions(-) diff --git a/src/findlib.ml b/src/findlib.ml index 0de52d01..3f3c4539 100644 --- a/src/findlib.ml +++ b/src/findlib.ml @@ -99,11 +99,7 @@ module Config = struct if not (Path.exists conf_file) then die "@{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] } @@ -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 } @@ -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 = @@ -243,13 +239,13 @@ 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 /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 @@ -257,18 +253,14 @@ let find_and_acknowledge_meta t ~fq_name = 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 | [] -> diff --git a/src/import.ml b/src/import.ml index 1764e90d..961730a5 100644 --- a/src/import.ml +++ b/src/import.ml @@ -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 "@[(%s@ =@ %a)@]" k f v + ) fmt (to_list t) +end module Int_set = Set.Make(Int) module Int_map = Map.Make(Int) diff --git a/src/meta.ml b/src/meta.ml index 84581b83..d23bd4ae 100644 --- a/src/meta.ml +++ b/src/meta.ml @@ -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 = @@ -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 = @@ -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 diff --git a/src/meta.mli b/src/meta.mli index a2a1b242..fe5eda8a 100644 --- a/src/meta.mli +++ b/src/meta.mli @@ -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 = @@ -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 diff --git a/test/unit-tests/tests.mlt b/test/unit-tests/tests.mlt index 746bbb20..7b2c00f9 100644 --- a/test/unit-tests/tests.mlt +++ b/test/unit-tests/tests.mlt @@ -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 = @@ -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 = []} |}]