dune/src/lib.ml

115 lines
3.1 KiB
OCaml
Raw Normal View History

2016-12-02 13:54:32 +00:00
open Import
2016-12-15 11:20:46 +00:00
module Internal = struct
type t = Path.t * Jbuild_types.Library.t
end
2016-12-02 13:54:32 +00:00
module T = struct
type t =
2016-12-15 11:20:46 +00:00
| Internal of Internal.t
2016-12-02 13:54:32 +00:00
| External of Findlib.package
let best_name = function
| External pkg -> pkg.name
| Internal (_, lib) -> Option.value lib.public_name ~default:lib.name
let compare a b = String.compare (best_name a) (best_name b)
end
include T
module Set = Set.Make(T)
2016-12-15 11:20:46 +00:00
(*
2016-12-02 13:54:32 +00:00
let deps = function
| Internal (_, lib) -> lib.libraries
| External pkg -> pkg.requires
2016-12-15 11:20:46 +00:00
*)
2016-12-02 13:54:32 +00:00
let dir = function
| Internal (dir, _) -> dir
| External pkg -> pkg.dir
2016-12-31 13:26:29 +00:00
let header_files ts =
List.fold_left ts ~init:[] ~f:(fun acc t ->
match t with
| External _ -> []
| Internal (dir, lib) ->
2017-01-19 13:19:13 +00:00
match lib.install_c_headers with
2016-12-31 13:26:29 +00:00
| [] -> acc
| l ->
List.fold_left l ~init:acc ~f:(fun acc fn ->
Path.relative dir (fn ^ ".h") :: acc))
2016-12-02 13:54:32 +00:00
let include_flags ts =
let dirs =
List.fold_left ts ~init:Path.Set.empty ~f:(fun acc t ->
Path.Set.add (dir t) acc)
in
Arg_spec.S (List.concat_map (Path.Set.elements dirs) ~f:(fun dir ->
[Arg_spec.A "-I"; Path dir]))
2016-12-15 11:20:46 +00:00
let has_headers = function
2017-01-19 13:19:13 +00:00
| Internal (_, lib) -> lib.install_c_headers <> []
2016-12-15 11:20:46 +00:00
| External pkg -> pkg.has_headers
let c_include_flags ts =
let dirs =
List.fold_left ts ~init:Path.Set.empty ~f:(fun acc t ->
if has_headers t then
Path.Set.add (dir t) acc
else
acc)
in
Arg_spec.S (List.concat_map (Path.Set.elements dirs) ~f:(fun dir ->
[Arg_spec.A "-I"; Path dir]))
2016-12-02 13:54:32 +00:00
let describe = function
| Internal (_, lib) ->
sprintf "%s (local)" (Option.value lib.public_name ~default:lib.name)
| External pkg ->
sprintf "%s (external)" pkg.name
let link_flags ts ~mode =
Arg_spec.S
(include_flags ts ::
List.map ts ~f:(fun t : _ Arg_spec.t ->
match t with
| External pkg ->
Deps_rel (pkg.dir, Mode.Dict.get pkg.archives mode)
| Internal (dir, lib) ->
Dep_rel (dir, lib.name ^ Mode.compiled_lib_ext mode)))
2016-12-31 13:26:29 +00:00
let archive_files ts ~mode ~ext_lib =
2016-12-02 13:54:32 +00:00
List.concat_map ts ~f:(function
| External pkg ->
List.map (Mode.Dict.get pkg.archives mode) ~f:(Path.relative pkg.dir)
| Internal (dir, lib) ->
2016-12-31 13:26:29 +00:00
let l =
[Path.relative dir (lib.name ^ Mode.compiled_lib_ext mode)]
in
if Jbuild_types.Library.has_stubs lib then
Jbuild_types.Library.stubs_archive lib ~dir ~ext_lib :: l
else
l)
2016-12-15 11:20:46 +00:00
(*
2016-12-02 13:54:32 +00:00
let ppx_runtime_libraries ts =
List.fold_left ts ~init:String_set.empty ~f:(fun acc t ->
match t with
| Internal (_, lib) ->
String_set.union acc (String_set.of_list lib.ppx_runtime_libraries)
| External pkg ->
String_set.union acc (String_set.of_list pkg.ppx_runtime_deps))
2016-12-15 11:20:46 +00:00
*)
let remove_dups_preserve_order libs =
let rec loop seen libs acc =
match libs with
| [] -> List.rev acc
| lib :: libs ->
let name = best_name lib in
if String_set.mem name seen then
loop seen libs acc
else
loop (String_set.add name seen) libs (lib :: acc)
in
loop String_set.empty libs []
;;