From c5ea5670dc68f24edb1cc560d7d3d4fe2d15ba0c Mon Sep 17 00:00:00 2001 From: ivg Date: Wed, 17 Aug 2022 16:05:33 -0400 Subject: [PATCH] adds support for loading plugins in toplevels 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 Signed-off-by: Richard L Ford --- otherlibs/site/src/plugins/dune | 2 +- otherlibs/site/src/plugins/linker/dune | 5 +++++ otherlibs/site/src/plugins/linker/dynlink/dune | 5 +++++ otherlibs/site/src/plugins/linker/dynlink/linker.ml | 1 + otherlibs/site/src/plugins/linker/linker.mli | 1 + otherlibs/site/src/plugins/linker/toplevel/dune | 6 ++++++ otherlibs/site/src/plugins/linker/toplevel/linker.ml | 10 ++++++++++ otherlibs/site/src/plugins/plugins.ml | 2 +- 8 files changed, 30 insertions(+), 2 deletions(-) create mode 100644 otherlibs/site/src/plugins/linker/dune create mode 100644 otherlibs/site/src/plugins/linker/dynlink/dune create mode 100644 otherlibs/site/src/plugins/linker/dynlink/linker.ml create mode 100644 otherlibs/site/src/plugins/linker/linker.mli create mode 100644 otherlibs/site/src/plugins/linker/toplevel/dune create mode 100644 otherlibs/site/src/plugins/linker/toplevel/linker.ml diff --git a/otherlibs/site/src/plugins/dune b/otherlibs/site/src/plugins/dune index 378af3bb8750..7bed1ce9cbd6 100644 --- a/otherlibs/site/src/plugins/dune +++ b/otherlibs/site/src/plugins/dune @@ -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))) diff --git a/otherlibs/site/src/plugins/linker/dune b/otherlibs/site/src/plugins/linker/dune new file mode 100644 index 000000000000..acff16bb2002 --- /dev/null +++ b/otherlibs/site/src/plugins/linker/dune @@ -0,0 +1,5 @@ +(library + (name dune_site_backend) + (public_name dune-site.linker) + (virtual_modules linker) + (default_implementation dune-site.dynlink)) diff --git a/otherlibs/site/src/plugins/linker/dynlink/dune b/otherlibs/site/src/plugins/linker/dynlink/dune new file mode 100644 index 000000000000..851802c02d98 --- /dev/null +++ b/otherlibs/site/src/plugins/linker/dynlink/dune @@ -0,0 +1,5 @@ +(library + (name dune_site_dynlink_linker) + (public_name dune-site.dynlink) + (implements dune-site.linker) + (libraries dynlink)) diff --git a/otherlibs/site/src/plugins/linker/dynlink/linker.ml b/otherlibs/site/src/plugins/linker/dynlink/linker.ml new file mode 100644 index 000000000000..73bf7a03a50f --- /dev/null +++ b/otherlibs/site/src/plugins/linker/dynlink/linker.ml @@ -0,0 +1 @@ +let load = Dynlink.loadfile diff --git a/otherlibs/site/src/plugins/linker/linker.mli b/otherlibs/site/src/plugins/linker/linker.mli new file mode 100644 index 000000000000..63fbb5a31860 --- /dev/null +++ b/otherlibs/site/src/plugins/linker/linker.mli @@ -0,0 +1 @@ +val load : string -> unit diff --git a/otherlibs/site/src/plugins/linker/toplevel/dune b/otherlibs/site/src/plugins/linker/toplevel/dune new file mode 100644 index 000000000000..c56cd49f8249 --- /dev/null +++ b/otherlibs/site/src/plugins/linker/toplevel/dune @@ -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)) diff --git a/otherlibs/site/src/plugins/linker/toplevel/linker.ml b/otherlibs/site/src/plugins/linker/toplevel/linker.ml new file mode 100644 index 000000000000..3001b70181e8 --- /dev/null +++ b/otherlibs/site/src/plugins/linker/toplevel/linker.ml @@ -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) diff --git a/otherlibs/site/src/plugins/plugins.ml b/otherlibs/site/src/plugins/plugins.ml index 581a8b9f803a..2fd2475b9554 100644 --- a/otherlibs/site/src/plugins/plugins.ml +++ b/otherlibs/site/src/plugins/plugins.ml @@ -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 =