Make Findlib.package abstract

This commit is contained in:
Jeremie Dimino 2018-02-07 17:51:40 +00:00 committed by Jérémie Dimino
parent f949588742
commit b46cef533a
13 changed files with 117 additions and 74 deletions

View File

@ -497,14 +497,15 @@ let installed_libraries =
Fiber.return ()
end else begin
let pkgs = Findlib.all_packages findlib in
let max_len = List.longest_map pkgs ~f:(fun p -> p.name) in
let max_len = List.longest_map pkgs ~f:Findlib.Package.name in
List.iter pkgs ~f:(fun pkg ->
let ver =
match pkg.Findlib.version with
match Findlib.Package.version pkg with
| "" -> "n/a"
| v -> v
in
Printf.printf "%-*s (version: %s)\n" max_len pkg.name ver);
Printf.printf "%-*s (version: %s)\n" max_len
(Findlib.Package.name pkg) ver);
Fiber.return ()
end)
in

View File

@ -92,12 +92,12 @@ let file_of_lib t ~from ~lib ~file =
~required_by:[With_required_by.Entry.jbuild_file_in ~dir:from]
with
| Some pkg ->
Ok (Path.relative pkg.dir file)
Ok (Path.relative (Findlib.Package.dir pkg) file)
| None ->
Error
{ fail = fun () ->
ignore (Findlib.find_exn t.context.findlib lib
~required_by:[With_required_by.Entry.jbuild_file_in ~dir:from]
: Findlib.package);
: Findlib.Package.t);
assert false
}

View File

@ -141,17 +141,34 @@ module Config = struct
Vars.get vars var preds
end
type package =
{ name : string
; dir : Path.t
; version : string
; description : string
; archives : Path.t list Mode.Dict.t
; plugins : Path.t list Mode.Dict.t
; jsoo_runtime : string list
; requires : package list
; ppx_runtime_deps : package list
}
module Package = struct
type t =
{ name : string
; dir : Path.t
; version : string
; description : string
; archives : Path.t list Mode.Dict.t
; plugins : Path.t list Mode.Dict.t
; jsoo_runtime : string list
; requires : t list
; ppx_runtime_deps : t list
}
let name t = t.name
let dir t = t.dir
let version t = t.version
let description t = t.description
let archives t mode = Mode.Dict.get t.archives mode
let plugins t mode = Mode.Dict.get t.plugins mode
let jsoo_runtime t = t.jsoo_runtime
let requires t = t.requires
let ppx_runtime_deps t = t.ppx_runtime_deps
end
open Package
module Package_not_available = struct
type t =
@ -219,7 +236,7 @@ module Package_not_available = struct
end
type present_or_not_available =
| Present of package
| Present of Package.t
| Not_available of Package_not_available.t
type t =
@ -240,7 +257,7 @@ let create ~stdlib_dir ~path =
module Pkg_step1 = struct
type t =
{ package : package
{ package : Package.t
; requires : string list
; ppx_runtime_deps : string list
; exists : bool
@ -521,7 +538,7 @@ let find t ~required_by name =
let available t ~required_by name =
match find_exn t name ~required_by with
| (_ : package) -> true
| (_ : Package.t) -> true
| exception (Findlib (Package_not_available _)) -> false
let check_deps_consistency ~required_by ~local_public_libs pkg requires =
@ -566,7 +583,7 @@ let root_packages t =
let all_packages t =
List.iter (root_packages t) ~f:(fun pkg ->
ignore (find t pkg ~required_by:[] : package option));
ignore (find t pkg ~required_by:[] : Package.t option));
Hashtbl.fold t.packages ~init:[] ~f:(fun ~key:_ ~data acc ->
match data with
| Present p -> p :: acc
@ -575,7 +592,7 @@ let all_packages t =
let all_unavailable_packages t =
List.iter (root_packages t) ~f:(fun pkg ->
ignore (find t pkg ~required_by:[] : package option));
ignore (find t pkg ~required_by:[] : Package.t option));
Hashtbl.fold t.packages ~init:[] ~f:(fun ~key:_ ~data acc ->
match data with
| Present _ -> acc

View File

@ -47,23 +47,39 @@ val create
val path : t -> Path.t list
type package =
{ name : string
; dir : Path.t
; version : string
; description : string
; archives : Path.t list Mode.Dict.t
; plugins : Path.t list Mode.Dict.t
; jsoo_runtime : string list
; requires : package list
; ppx_runtime_deps : package list
}
module Package : sig
(** Representation of a findlib package *)
type t
val find : t -> required_by:With_required_by.Entry.t list -> string -> package option
val find_exn : t -> required_by:With_required_by.Entry.t list -> string -> package
val name : t -> string
val dir : t -> Path.t
val version : t -> string
val description : t -> string
(** Package files *)
val archives : t -> Mode.t -> Path.t list
val plugins : t -> Mode.t -> Path.t list
val jsoo_runtime : t -> string list
val requires : t -> t list
val ppx_runtime_deps : t -> t list
end
val find
: t
-> required_by:With_required_by.Entry.t list
-> string
-> Package.t option
val find_exn
: t
-> required_by:With_required_by.Entry.t list
-> string
-> Package.t
(** Same as [Option.is_some (find t ...)] *)
val available : t -> required_by:With_required_by.Entry.t list -> string -> bool
(** [root_package_name "foo.*"] is "foo" *)
val root_package_name : string -> string
(** [local_public_libs] is a map from public library names to where they are defined in
@ -71,19 +87,19 @@ val root_package_name : string -> string
val closure
: required_by:With_required_by.Entry.t list
-> local_public_libs:Path.t String_map.t
-> package list
-> package list
-> Package.t list
-> Package.t list
val closed_ppx_runtime_deps_of
: required_by:With_required_by.Entry.t list
-> local_public_libs:Path.t String_map.t
-> package list
-> package list
-> Package.t list
-> Package.t list
val root_packages : t -> string list
val all_packages : t -> package list
val all_packages : t -> Package.t list
val all_unavailable_packages : t -> Package_not_available.t list
val stdlib_with_archives : t -> package
val stdlib_with_archives : t -> Package.t
module Config : sig
type t

View File

@ -126,13 +126,13 @@ end
in
let includes =
List.fold_left pkgs ~init:Path.Set.empty ~f:(fun acc pkg ->
Path.Set.add pkg.Findlib.dir acc)
Path.Set.add (Findlib.Package.dir pkg) acc)
|> Path.Set.elements
|> List.concat_map ~f:(fun path ->
[ "-I"; Path.to_string path ])
in
let cmas =
List.concat_map pkgs ~f:(fun pkg -> pkg.archives.byte)
List.concat_map pkgs ~f:(fun pkg -> Findlib.Package.archives pkg Byte)
in
let args =
List.concat

View File

@ -68,8 +68,10 @@ let link_rule ~sctx ~dir ~runtime ~target =
let all_libs =
List.concat_map (stdlib :: libs) ~f:(function
| Lib.External pkg ->
List.map (Mode.Dict.get pkg.archives Mode.Byte) ~f:(fun fn ->
in_build_dir ~ctx [pkg.name; sprintf "%s.js" (Path.basename fn)])
List.map (Findlib.Package.archives pkg Byte) ~f:(fun fn ->
in_build_dir ~ctx [ Findlib.Package.name pkg
; sprintf "%s.js" (Path.basename fn)
])
| Lib.Internal (dir, lib) ->
[ Path.relative dir (sprintf "%s.cma.js" lib.name) ]
)
@ -115,16 +117,18 @@ let setup_separate_compilation_rules sctx components =
| Some pkg ->
let pkg =
(* Special case for the stdlib because it is not referenced in the META *)
match pkg.Findlib.name with
match Findlib.Package.name pkg with
| "stdlib" -> Findlib.stdlib_with_archives ctx.findlib
| _ -> pkg
in
let archives = Mode.Dict.get pkg.Findlib.archives Mode.Byte in
let archives = Findlib.Package.archives pkg Byte in
List.iter archives ~f:(fun fn ->
let name = Path.basename fn in
let src = Path.relative pkg.dir name in
let target = in_build_dir ~ctx [ pkg.name; sprintf "%s.js" name] in
let dir = in_build_dir ~ctx [ pkg.name ] in
let src = Path.relative (Findlib.Package.dir pkg) name in
let target =
in_build_dir ~ctx [ Findlib.Package.name pkg; sprintf "%s.js" name]
in
let dir = in_build_dir ~ctx [ Findlib.Package.name pkg ] in
let spec = Arg_spec.Dep src in
SC.add_rule sctx
(Build.return (standard ())

View File

@ -1,5 +1,7 @@
open Import
module FP = Findlib.Package
module Internal = struct
type t = Path.t * Jbuild.Library.t
end
@ -7,10 +9,10 @@ end
module T = struct
type t =
| Internal of Internal.t
| External of Findlib.package
| External of FP.t
let best_name = function
| External pkg -> pkg.name
| External pkg -> FP.name pkg
| Internal (_, lib) -> Jbuild.Library.best_name lib
let compare a b = String.compare (best_name a) (best_name b)
@ -24,11 +26,11 @@ let lib_obj_dir dir lib =
let dir = function
| Internal (dir, _) -> dir
| External pkg -> pkg.dir
| External pkg -> FP.dir pkg
let obj_dir = function
| Internal (dir, lib) -> lib_obj_dir dir lib
| External pkg -> pkg.dir
| External pkg -> FP.dir pkg
let include_paths ts ~stdlib_dir =
List.fold_left ts ~init:Path.Set.empty ~f:(fun acc t ->
@ -56,7 +58,7 @@ let describe = function
| Some p -> p.name
| None -> lib.name)
| External pkg ->
sprintf "%s (external)" pkg.name
sprintf "%s (external)" (FP.name pkg)
let link_flags ts ~mode ~stdlib_dir =
Arg_spec.S
@ -64,14 +66,13 @@ let link_flags ts ~mode ~stdlib_dir =
List.map ts ~f:(fun t ->
match t with
| External pkg ->
Arg_spec.Deps (Mode.Dict.get pkg.archives mode)
Arg_spec.Deps (FP.archives pkg mode)
| Internal (dir, lib) ->
Dep (Path.relative dir (lib.name ^ Mode.compiled_lib_ext mode))))
let archive_files ts ~mode ~ext_lib =
List.concat_map ts ~f:(function
| External pkg ->
Mode.Dict.get pkg.archives mode
| External pkg -> FP.archives pkg mode
| Internal (dir, lib) ->
let l =
[Path.relative dir (lib.name ^ Mode.compiled_lib_ext mode)]
@ -84,7 +85,7 @@ let archive_files ts ~mode ~ext_lib =
let jsoo_runtime_files ts =
List.concat_map ts ~f:(function
| External pkg ->
List.map pkg.jsoo_runtime ~f:(Path.relative pkg.dir)
List.map (FP.jsoo_runtime pkg) ~f:(Path.relative (FP.dir pkg))
| Internal (dir, lib) ->
List.map lib.buildable.js_of_ocaml.javascript_files ~f:(Path.relative dir))
(*
@ -112,5 +113,5 @@ let remove_dups_preserve_order libs =
;;
let public_name = function
| External pkg -> Some pkg.name
| External pkg -> Some (FP.name pkg)
| Internal (_, lib) -> Option.map lib.public ~f:(fun p -> p.name)

View File

@ -6,7 +6,7 @@ end
type t =
| Internal of Internal.t
| External of Findlib.package
| External of Findlib.Package.t
module Set : Set.S with type elt := t

View File

@ -1,6 +1,8 @@
open Import
open Jbuild
module FP = Findlib.Package
type scope =
{ mutable libs : Lib.Internal.t String_map.t
; scope : Scope.t
@ -178,7 +180,7 @@ module Scope = struct
and process acc (lib : Lib.t) =
let unique_id =
match lib with
| External pkg -> pkg.name
| External pkg -> FP.name pkg
| Internal (dir, lib) ->
match lib.public with
| Some p -> p.name
@ -195,13 +197,13 @@ module Scope = struct
List.fold_left lib.buildable.libraries ~init:acc ~f:(loop scope)
| External pkg ->
if deep_traverse_externals then
List.fold_left pkg.requires ~init:acc ~f:(fun acc pkg ->
List.fold_left (FP.requires pkg) ~init:acc ~f:(fun acc pkg ->
process acc (External pkg))
else begin
seen :=
String_set.union !seen
(String_set.of_list
(List.map pkg.requires ~f:(fun p -> p.Findlib.name)));
(List.map (FP.requires pkg) ~f:FP.name));
acc
end
end
@ -220,7 +222,7 @@ module Scope = struct
List.map lib.ppx_runtime_libraries ~f:(fun name ->
Lib.best_name (find_exn (Lazy.force scope) name))
| External pkg ->
List.map pkg.ppx_runtime_deps ~f:(fun p -> p.Findlib.name)
List.map (FP.ppx_runtime_deps pkg) ~f:FP.name
in
String_set.union acc (String_set.of_list rt_deps))
end
@ -316,7 +318,7 @@ let internal_libs_without_non_installable_optional_ones t =
let unique_library_name t (lib : Lib.t) =
match lib with
| External pkg -> pkg.name
| External pkg -> FP.name pkg
| Internal (dir, lib) ->
match lib.public with
| Some x -> x.name

View File

@ -38,7 +38,7 @@ module Scope : sig
val interpret_lib_deps
: t With_required_by.t
-> Jbuild.Lib_dep.t list
-> Lib.Internal.t list * Findlib.package list * fail option
-> Lib.Internal.t list * Findlib.Package.t list * fail option
val resolve_selects
: t With_required_by.t

View File

@ -59,7 +59,7 @@ let dot_merlin sctx ~dir ~scope ({ requires; flags; _ } as t) =
let bpath = Path.reach (Lib.lib_obj_dir path lib) ~from:remaindir in
("S " ^ spath) :: ("B " ^ bpath) :: internals, externals
| Lib.External pkg ->
internals, ("PKG " ^ pkg.name) :: externals
internals, ("PKG " ^ Findlib.Package.name pkg) :: externals
)
in
let source_dirs =

View File

@ -363,7 +363,8 @@ module Libs = struct
List.fold_left libs ~init:[] ~f:(fun acc (lib : Lib.t) ->
match lib with
| External pkg ->
Build_system.stamp_file_for_files_of t.build_system ~dir:pkg.dir ~ext :: acc
Build_system.stamp_file_for_files_of t.build_system
~dir:(Findlib.Package.dir pkg) ~ext :: acc
| Internal lib ->
Alias.stamp_file (lib_files_alias lib ~ext) :: acc)))
@ -794,7 +795,7 @@ module PP = struct
let libs, drivers =
List.partition_map libs ~f:(fun lib ->
if (match lib with
| External pkg -> is_driver pkg.name
| External pkg -> is_driver (Findlib.Package.name pkg)
| Internal (_, lib) ->
is_driver lib.name ||
match lib.public with

View File

@ -6,13 +6,14 @@ open Jbuilder
open Import
let print_pkg ppf pkg =
Format.fprintf ppf "<package:%s>" pkg.Findlib.name
Format.fprintf ppf "<package:%s>" (Findlib.Package.name pkg)
;;
#install_printer print_pkg;;
[%%expect{|
val print_pkg : Format.formatter -> Jbuilder.Findlib.package -> unit = <fun>
val print_pkg : Format.formatter -> Jbuilder.Findlib.Package.t -> unit =
<fun>
|}]
let findlib =
@ -29,14 +30,14 @@ val findlib : Jbuilder.Findlib.t = <abstr>
let pkg = Findlib.find_exn findlib ~required_by:[] "foo";;
[%%expect{|
val pkg : Jbuilder.Findlib.package = <package:foo>
val pkg : Jbuilder.Findlib.Package.t = <package:foo>
|}]
(* "foo" should depend on "baz" *)
pkg.requires;;
Findlib.Package.requires pkg;;
[%%expect{|
- : Jbuilder.Findlib.package list = [<package:baz>]
- : Jbuilder.Findlib.Package.t list = [<package:baz>]
|}]
(* +-----------------------------------------------------------------+