Skip to content

Commit

Permalink
Simple plugin stanza
Browse files Browse the repository at this point in the history
just install an alias meta
  • Loading branch information
bobot committed Feb 8, 2020
1 parent 31e34f5 commit 7978a31
Show file tree
Hide file tree
Showing 21 changed files with 418 additions and 89 deletions.
1 change: 1 addition & 0 deletions bin/import.ml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ module Dep = Dune.Dep
module Action_to_sh = Dune.Action_to_sh
module Dpath = Dune.Dpath
module Install = Dune.Install
module Section = Dune.Section
module Watermarks = Dune.Watermarks
module Promotion = Dune.Promotion
module Colors = Dune.Colors
Expand Down
14 changes: 7 additions & 7 deletions bin/install_uninstall.ml
Original file line number Diff line number Diff line change
Expand Up @@ -264,13 +264,13 @@ end
module Sections = struct
type t =
| All
| Only of Install.Section.Set.t
| Only of Section.Set.t

let sections_conv : Install.Section.t list Cmdliner.Arg.converter =
let sections_conv : Section.t list Cmdliner.Arg.converter =
let all =
Install.Section.all |> Install.Section.Set.to_list
Section.all |> Section.Set.to_list
|> List.map ~f:(fun section ->
(Install.Section.to_string section, section))
(Section.to_string section, section))
in
Arg.list ~sep:',' (Arg.enum all)

Expand All @@ -282,12 +282,12 @@ module Sections = struct
in
match sections with
| None -> All
| Some sections -> Only (Install.Section.Set.of_list sections)
| Some sections -> Only (Section.Set.of_list sections)

let should_install t section =
match t with
| All -> true
| Only set -> Install.Section.Set.mem set section
| Only set -> Section.Set.mem set section
end

let file_operations ~dry_run ~workspace : (module File_operations) =
Expand Down Expand Up @@ -473,7 +473,7 @@ let install_uninstall ~what =
(Path.to_string_maybe_quoted dst);
Ops.mkdir_p dir;
let executable =
Install.Section.should_set_executable_bit
Section.should_set_executable_bit
entry.section
in
let get_location section package =
Expand Down
93 changes: 69 additions & 24 deletions otherlibs/sites_locations/test/run.t
Original file line number Diff line number Diff line change
Expand Up @@ -3,49 +3,92 @@ Test embedding of sites locations information

$ mkdir -p a b c

$ for i in a b c; do
$ for i in a b; do
> mkdir -p $i
> cat >$i/dune-project <<EOF
> (lang dune 2.2)
> (name $i)
> (package (name $i) (sites_locations (share data)))
> EOF
> done

$ for i in c; do
> mkdir -p $i
> cat >$i/dune-project <<EOF
> (lang dune 2.2)
> (name $i)
> (package (name $i) (sites_locations (share data)))
> (package (name $i) (sites_locations (share data) (lib plugins)))
> EOF
> done

$ for i in a b; do
> cat >$i/dune <<EOF
$ cat >a/dune <<EOF
> (library
> (public_name $i)
> (public_name a)
> (libraries dune-sites-locations)
> )
> (sites (module sites) (package $i))
> (sites (module sites) (package a))
> EOF
> cat >$i/$i.ml <<EOF
> let v = "$i"
> let () = Printf.printf "run $i\n%!"
> let () = List.iter (Printf.printf "$i: %s\n%!") Sites.data

$ cat >a/a.ml <<EOF
> let v = "a"
> let () = Printf.printf "run a\n%!"
> let () = List.iter (Printf.printf "a: %s\n%!") Sites.data
> EOF

$ cat >b/dune <<EOF
> (library
> (public_name b)
> (libraries c.register dune-sites-locations)
> )
> (sites (module sites) (package b))
> (plugin (name c-plugins-b) (libraries b) (site (c plugins)))
> EOF

$ cat >b/b.ml <<EOF
> let v = "b"
> let () = Printf.printf "run b\n%!"
> let () = C_register.b_registered := true
> let () = List.iter (Printf.printf "b: %s\n%!") Sites.data
> EOF
> done

$ cat >c/dune <<EOF
> (executable
> (public_name c)
> (promote (until-clean))
> (libraries a b dune-sites-locations))
> (modules c sites)
> (libraries a c.register findlib.dynload dune-sites-locations))
> (library
> (public_name c.register)
> (name c_register)
> (modules c_register)
> )
> (sites (module sites) (package c))
> (rule
> (targets out.log)
> (deps (package c))
> (action (with-stdout-to out.log (run %{bin:c}))))
> EOF

$ cat >c/c_register.ml <<EOF
> let b_registered = ref false
> EOF

$ cat >c/c.ml <<EOF
> let () = Printf.printf "run c: %s %s\n%!" A.v B.v
> let () = Printf.printf "run c: %s b_registered:%b\n%!" A.v !C_register.b_registered
> let path_sep = if Sys.win32 then ";" else ":"
> let env_ocamlpath = (String.concat path_sep (Sites.plugins@[Sys.getenv "OCAMLPATH"]))
> let () = Findlib.init ~env_ocamlpath ()
> let plugins =
> List.concat
> (List.map (fun dir -> (Array.to_list (Sys.readdir dir)))
> ((List.filter Sys.file_exists Sites.plugins)))
> let () = Fl_dynload.load_packages plugins
> let () = Printf.printf "run c: %s b_registered:%b\n%!" A.v !C_register.b_registered
> let () = List.iter (Printf.printf "c: %s\n%!") Sites.data
> EOF

$ cat > dune-project << EOF
> (lang dune 2.0)
> (lang dune 2.2)
> EOF

$ dune build
Expand All @@ -55,17 +98,18 @@ Inside _build, we have no sites information:

$ _build/default/c/c.exe
run a
run b
run c: a b
run c: a b_registered:false
run c: a b_registered:false

Once installed, we have the sites information:

$ _install/bin/c
$ OCAMLPATH=_install/lib:$OCAMLPATH _install/bin/c
run a
a: $TESTCASE_ROOT/_install/share/a/data
run c: a b_registered:false
run b
b: $TESTCASE_ROOT/_install/share/b/data
run c: a b
run c: a b_registered:true
c: $TESTCASE_ROOT/_install/share/c/data

Test substitution when promoting
Expand All @@ -74,10 +118,9 @@ Test substitution when promoting
$ c/c.exe
run a
a: $TESTCASE_ROOT/_build/install/default/share/a/data
run b
b: $TESTCASE_ROOT/_build/install/default/share/b/data
run c: a b
c: $TESTCASE_ROOT/_build/install/default/share/c/data
run c: a b_registered:false
Fatal error: exception Fl_package_base.No_such_package("b", "required by `c-plugins-b'")
[2]

Test within dune rules
--------------------------------
Expand All @@ -86,9 +129,10 @@ Test within dune rules
$ cat _build/default/c/out.log
run a
a: $TESTCASE_ROOT/_build/install/default/share/a/data
run c: a b_registered:false
run b
b: $TESTCASE_ROOT/_build/install/default/share/b/data
run c: a b
run c: a b_registered:true
c: $TESTCASE_ROOT/_build/install/default/share/c/data


Expand All @@ -97,7 +141,8 @@ Test with dune exec
$ dune exec -- c/c.exe
run a
a: $TESTCASE_ROOT/_build/install/default/share/a/data
run c: a b_registered:false
run b
b: $TESTCASE_ROOT/_build/install/default/share/b/data
run c: a b
run c: a b_registered:true
c: $TESTCASE_ROOT/_build/install/default/share/c/data
2 changes: 1 addition & 1 deletion src/dune/coq_rules.ml
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ let install_rules ~sctx ~dir s =
let dst = Path.Local.relative dst_dir vofile_rel in
( None
, Install.(
Entry.make Section.Lib_root ~dst:(Path.Local.to_string dst)
Entry.make Lib_root ~dst:(Path.Local.to_string dst)
vofile) ))
|> List.rev_append (coq_plugins_install_rules ~scope ~package ~dst_dir s)

Expand Down
29 changes: 26 additions & 3 deletions src/dune/dune_file.ml
Original file line number Diff line number Diff line change
Expand Up @@ -1151,16 +1151,33 @@ module Library = struct
~special_builtin_support ~exit_module
end

module Plugin = struct
type t = {
package: Package.t;
name: Package.Name.t;
libraries: (Loc.t * Lib_name.t) list;
site: (Loc.t * (Package.Name.t * Package.Name.t));
}

let decode =
fields
(let+ name = field "name" Package.Name.decode
and+ libraries = field "libraries" (repeat (located Lib_name.decode))
and+ site = field "site" (located (pair Package.Name.decode Package.Name.decode))
and+ package = Pkg.field "package" in
{ name; libraries; site; package })
end

module Install_conf = struct
type 'file t =
{ section : Install.Section.t
{ section : Install.SectionWithSite.t
; files : 'file list
; package : Package.t
}

let decode =
fields
(let+ section = field "section" Install.Section.decode
(let+ section = field "section" Install.SectionWithSite.decode
and+ files = field "files" File_binding.Unexpanded.L.decode
and+ package = Pkg.field "install" in
{ section; files; package })
Expand Down Expand Up @@ -1348,7 +1365,7 @@ module Executables = struct
~dst:(locp, pub)))
|> List.filter_opt
in
{ Install_conf.section = Bin; files; package })
{ Install_conf.section = Section Bin; files; package })
end

module Link_mode = struct
Expand Down Expand Up @@ -2189,6 +2206,7 @@ type Stanza.t +=
| External_variant of External_variant.t
| Deprecated_library_name of Deprecated_library_name.t
| Sites_locations of Sites_locations.t
| Plugin of Plugin.t

module Stanzas = struct
type t = Stanza.t list
Expand Down Expand Up @@ -2289,6 +2307,10 @@ module Stanzas = struct
, let+ () = Dune_lang.Syntax.since Stanza.syntax (2, 2)
and+ t = Sites_locations.decode in
[ Sites_locations t ] )
; ( "plugin"
, let+ () = Dune_lang.Syntax.since Stanza.syntax (2, 2)
and+ t = Plugin.decode in
[ Plugin t ] )
]

let () = Dune_project.Lang.register Stanza.syntax stanzas
Expand Down Expand Up @@ -2374,6 +2396,7 @@ let stanza_package = function
| Alias { package = Some package; _ }
| Rule { package = Some package; _ }
| Install { package; _ }
| Plugin { package; _ }
| Executables { install_conf = Some { package; _ }; _ }
| Documentation { package; _ }
| Tests { package = Some package; _ } ->
Expand Down
12 changes: 11 additions & 1 deletion src/dune/dune_file.mli
Original file line number Diff line number Diff line change
Expand Up @@ -243,9 +243,18 @@ module Library : sig
-> Lib_info.local
end

module Plugin : sig
type t = {
package: Package.t;
name: Package.Name.t;
libraries: (Loc.t * Lib_name.t) list;
site: (Loc.t * (Package.Name.t * Package.Name.t));
}
end

module Install_conf : sig
type 'file t =
{ section : Install.Section.t
{ section : Install.SectionWithSite.t
; files : 'file list
; package : Package.t
}
Expand Down Expand Up @@ -472,6 +481,7 @@ type Stanza.t +=
| External_variant of External_variant.t
| Deprecated_library_name of Deprecated_library_name.t
| Sites_locations of Sites_locations.t
| Plugin of Plugin.t

val stanza_package : Stanza.t -> Package.t option

Expand Down
19 changes: 16 additions & 3 deletions src/dune/dune_package.ml
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,7 @@ type t =
; entries : Entry.t list
; version : string option
; sites : Path.t Section.Map.t
; sites2 : Section.t Package.Name.Map.t
; dir : Path.t
}

Expand All @@ -285,6 +286,7 @@ let decode ~lang ~dir =
let+ name = field "name" Package.Name.decode
and+ version = field_o "version" string
and+ (loc,sites) = located (field ~default:[] "sites" (repeat (pair Section.decode Dpath.decode)))
and+ (loc2,sites2) = located (field ~default:[] "sites2" (repeat (pair Package.Name.decode Section.decode)))
and+ entries = leftover_fields_as_sums (Entry.cstrs ~lang ~dir) in
let entries =
List.map entries ~f:(fun e ->
Expand All @@ -303,6 +305,14 @@ let decode ~lang ~dir =
[ Pp.textf "The section %s appears multiple times"
(Section.to_string s)]
)
; sites2= Package.Name.Map.of_list sites2
|> (function
| Ok x -> x
| Error (s, _, _) ->
User_error.raise ~loc:loc2
[ Pp.textf "The section %s appears multiple times"
(Package.Name.to_string s)]
)
}

let () = Vfile.Lang.register Stanza.syntax ()
Expand All @@ -318,13 +328,15 @@ let prepend_version ~dune_version sexps =
]
@ sexps

let encode ~dune_version { entries; name; version; dir; sites } =
let encode ~dune_version { entries; name; version; dir; sites; sites2 } =
let open Dune_lang.Encoder in
let sites = (Section.Map.to_list (Section.Map.map ~f:Path.to_absolute_filename sites)) in
let sites2 = (Package.Name.Map.to_list sites2) in
let sexp = record_fields [
field "name" Package.Name.encode name;
field_o "version" string version;
field_l "sites" (pair Section.encode string) sites
field_l "sites" (pair Section.encode string) sites;
field_l "sites2" (pair Package.Name.encode Section.encode) sites2;
] in
let list s = Dune_lang.List s in
let entries =
Expand All @@ -338,14 +350,15 @@ let encode ~dune_version { entries; name; version; dir; sites } =
in
prepend_version ~dune_version (List.concat [ sexp; entries ])

let to_dyn { entries; name; version; dir; sites } =
let to_dyn { entries; name; version; dir; sites; sites2 } =
let open Dyn.Encoder in
record
[ ("entries", list Entry.to_dyn entries)
; ("name", Package.Name.to_dyn name)
; ("version", option string version)
; ("dir", Path.to_dyn dir)
; ("sites", Section.Map.to_dyn Path.to_dyn sites)
; ("sites2", Package.Name.Map.to_dyn Section.to_dyn sites2)
]

module Or_meta = struct
Expand Down
1 change: 1 addition & 0 deletions src/dune/dune_package.mli
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ type t =
; entries : Entry.t list
; version : string option
; sites : Path.t Section.Map.t
; sites2 : Section.t Package.Name.Map.t
; dir : Path.t
}

Expand Down
Loading

0 comments on commit 7978a31

Please sign in to comment.