Skip to content

Commit

Permalink
Read package flags from OPAM metadata
Browse files Browse the repository at this point in the history
This changes the logic from hardcoding the compiler "base packages" to
instead read the OPAM flags to check whether the package in question is
a compiler in which case it will be considered a base package.

It also adds code to check for packages whether they are `conf` packages,
which could be used to check for virtual packages.

Inspired by tarides#327, this would allow `dkml-base-compiler` to be considered
a base package and not accidentally vendor it. However it doesn't yet
allow using the DKML compiler, because the name of the `ocaml` conf
package is still hardcoded with no way to override it (yet).
  • Loading branch information
Leonidas-from-XIV committed Nov 25, 2022
1 parent 07ca215 commit 4e91684
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 21 deletions.
12 changes: 1 addition & 11 deletions lib/config.ml
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,7 @@
open Import

let base_packages =
[
"jbuilder";
"dune";
"ocamlbuild";
"ocamlmod";
"oasis";
"ocamlify";
"ocaml";
"ocaml-base-compiler";
"ocaml-variants";
]
[ "jbuilder"; "dune"; "ocamlbuild"; "ocamlmod"; "oasis"; "ocamlify" ]
|> List.map ~f:OpamPackage.Name.of_string
|> OpamPackage.Name.Set.of_list

Expand Down
40 changes: 31 additions & 9 deletions lib/opam.ml
Original file line number Diff line number Diff line change
Expand Up @@ -174,25 +174,40 @@ module Pp = struct
let url = Fmt.using OpamUrl.to_string Fmt.string
end

module Package_flag = struct
type t = OpamTypes.package_flag

let pp pps (v : t) =
match v with
| Pkgflag_LightUninstall -> Fmt.pf pps "light-uninstall"
| Pkgflag_Verbose -> Fmt.pf pps "verbose"
| Pkgflag_Plugin -> Fmt.pf pps "plugin"
| Pkgflag_Compiler -> Fmt.pf pps "compiler"
| Pkgflag_Conf -> Fmt.pf pps "conf"
| Pkgflag_AvoidVersion -> Fmt.pf pps "avoid-version"
| Pkgflag_Unknown unknown -> Fmt.pf pps "unknown(%s)" unknown
end

module Package_summary = struct
type t = {
package : OpamPackage.t;
url_src : Url.t option;
hashes : OpamHash.t list;
dev_repo : string option;
depexts : (OpamSysPkg.Set.t * OpamTypes.filter) list;
flags : Package_flag.t list;
}

let pp fmt { package; url_src; hashes; dev_repo; depexts } =
let pp fmt { package; url_src; hashes; dev_repo; depexts; flags } =
let open Pp_combinators.Ocaml in
Format.fprintf fmt
"@[<hov 2>{ name = %a;@ version = %a;@ url_src = %a;@ hashes = %a;@ \
dev_repo = %a;@ depexts = %a }@]"
dev_repo = %a;@ depexts = %a;@ flags = %a }@]"
Pp.package_name package.name Pp.version package.version
(option ~brackets:true Url.pp)
url_src (list Hash.pp) hashes
(option ~brackets:true string)
dev_repo Depexts.pp depexts
dev_repo Depexts.pp depexts (list Package_flag.pp) flags

let from_opam package opam_file =
let url_field = OpamFile.OPAM.url opam_file in
Expand All @@ -204,18 +219,25 @@ module Package_summary = struct
Option.map ~f:OpamUrl.to_string (OpamFile.OPAM.dev_repo opam_file)
in
let depexts = OpamFile.OPAM.depexts opam_file in
{ package; url_src; hashes; dev_repo; depexts }
let flags = OpamFile.OPAM.flags opam_file in
{ package; url_src; hashes; dev_repo; depexts; flags }

let has_flag flag { flags; _ } = List.mem flag ~set:flags
let is_compiler v = has_flag OpamTypes.Pkgflag_Compiler v

let is_virtual = function
| { url_src = None; _ } -> true
| { dev_repo = None | Some ""; _ } -> true
| _ -> false

let is_base_package = function
| { package; _ }
when OpamPackage.Name.Set.mem package.name Config.base_packages ->
true
| _ -> false
let is_base_package v =
let in_base_pkgs { package; _ } =
OpamPackage.Name.Set.mem package.name Config.base_packages
in
let is_compiler_pkg { package; _ } =
OpamPackage.Name.equal package.name Config.compiler_package_name
in
is_compiler v || in_base_pkgs v || is_compiler_pkg v
end

module Dependency_entry = struct
Expand Down
1 change: 1 addition & 0 deletions lib/opam.mli
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ module Package_summary : sig
hashes : OpamHash.t list;
dev_repo : string option;
depexts : (OpamSysPkg.Set.t * OpamTypes.filter) list;
flags : OpamTypes.package_flag list;
}

val pp : t Fmt.t
Expand Down
3 changes: 2 additions & 1 deletion test/lib/test_duniverse.ml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ let opam_factory ~name ~version =
let summary_factory ?(name = "undefined") ?(version = "1") ?dev_repo ?url_src
?(hashes = []) ?(depexts = []) () =
let package = opam_factory ~name ~version in
{ Opam.Package_summary.package; dev_repo; url_src; hashes; depexts }
let flags = [] in
{ Opam.Package_summary.package; dev_repo; url_src; hashes; depexts; flags }

let dependency_factory ?(vendored = true) ?name ?version ?dev_repo ?url_src
?hashes ?depexts () =
Expand Down

0 comments on commit 4e91684

Please sign in to comment.