Skip to content

Commit

Permalink
adds support for loading plugins in toplevels
Browse files Browse the repository at this point in the history
Uses virtual libraries to select the proper dynamic linker
facility. To load in the toplevel add `dune-site.toplevel` library as
the dependency to your toplevel.

Signed-off-by: ivg <[email protected]>
Signed-off-by: Richard L Ford <[email protected]>
  • Loading branch information
ivg authored and richardlford committed Mar 6, 2023
1 parent a65ba82 commit c5ea567
Show file tree
Hide file tree
Showing 8 changed files with 30 additions and 2 deletions.
2 changes: 1 addition & 1 deletion otherlibs/site/src/plugins/dune
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@
(dune_site
(plugins)
(data_module dune_site_plugins_data)))
(libraries dune-site dune-private-libs.meta_parser dynlink)
(libraries dune-site dune-private-libs.meta_parser dune-site.linker)
(instrumentation
(backend bisect_ppx)))
5 changes: 5 additions & 0 deletions otherlibs/site/src/plugins/linker/dune
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
(library
(name dune_site_backend)
(public_name dune-site.linker)
(virtual_modules linker)
(default_implementation dune-site.dynlink))
5 changes: 5 additions & 0 deletions otherlibs/site/src/plugins/linker/dynlink/dune
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
(library
(name dune_site_dynlink_linker)
(public_name dune-site.dynlink)
(implements dune-site.linker)
(libraries dynlink))
1 change: 1 addition & 0 deletions otherlibs/site/src/plugins/linker/dynlink/linker.ml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
let load = Dynlink.loadfile
1 change: 1 addition & 0 deletions otherlibs/site/src/plugins/linker/linker.mli
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
val load : string -> unit
6 changes: 6 additions & 0 deletions otherlibs/site/src/plugins/linker/toplevel/dune
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
(library
(name dune_site_toplevel_linker)
(modes byte)
(public_name dune-site.toplevel)
(implements dune-site.linker)
(libraries compiler-libs.toplevel))
10 changes: 10 additions & 0 deletions otherlibs/site/src/plugins/linker/toplevel/linker.ml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
let load filename =
let buf = Buffer.create 16 in
let ppf = Format.formatter_of_buffer buf in
match Toploop.load_file ppf filename with
| true -> ()
| false ->
Format.pp_print_flush ppf ();
failwith
@@ Format.asprintf "Failed to load file `%s': %s" filename
(Buffer.contents buf)
2 changes: 1 addition & 1 deletion otherlibs/site/src/plugins/plugins.ml
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,7 @@ let load_gen ~load_requires dirs name =
List.iter
(fun p ->
let file = Filename.concat directory p in
Dynlink.loadfile file)
Dune_site_backend.Linker.load file)
plugins)

let rec load_requires name =
Expand Down

0 comments on commit c5ea567

Please sign in to comment.