package ocaml_plugin

  1. Overview
  2. Docs
Automatically build and dynlink OCaml source files

Install

Dune Dependency

Authors

Maintainers

Sources

ocaml_plugin-v0.14.0.tar.gz
md5=58a772c5f3909a74f421f07a28cbfcb3

Description

Published: 31 May 2020

README

ocaml_plugin - a wrapper around dynlink for OCaml

ocaml_plugin is a library meant to make dynlink more easier. It offers a high-level kind of api where you can get a first class module out of a few ml source files, while handling the compilation of the files automatically.

Installation via opam

ocaml_plugin can be installed via opam:

$ opam install ocaml_plugin

Usage

A simple example is provided under the hello_world directory. A recommended set-up usually involves 2 steps in the code

  • defining the interface of the plugin, as well as an univ value

  • applying a functor to get a customized plugin loader plugin_intf.ml:

module type S = sig
  val message : string
end
let univ_constr =
  (Univ_constr.create "Plugin_intf.S" sexp_of_opaque : (module S) Univ_constr.t)

run.ml:

module Plugin = Compiler.Make(struct
  type t = (module Plugin_intf.S)
  let t_repr = "Plugin_intf.S"
  let univ_constr = Plugin_intf.univ_constr
  let univ_constr_repr = "Plugin_intf.univ_constr"
end)
Plugin.load_ocaml_src_files (files:string list) >>= function
| Error err ->
  Printf.eprintf "loading failed:\n%s%!" (Error.to_string_hum err)
| Ok plugin ->
  let module M = (val plugin : Plugin_intf.S) in
  Printf.printf "loaded plugin's message : %S\n%!" M.message

Standalone executable

It is possible to embed the compiler in an executable such that a full ocaml environment is not mandatory to actually load plugins. The way it is done in this version of the library is by embedding ocamlopt.opt and some cmi files inside a tgz archive that is getting amended at the end of the exec. At runtime, this archive is extracted into a temporary directory where the compilation will happen. To create this standalone version of an exec (exec+ocamlopt+cmi), you would typically run something like:

$ ../bin/ocaml_embed_compiler.exe -exe ./run.exe -cc $(which ocamlopt) \
     dsl.cmi ../lib/ocaml_plugin.cmi $(ocamlopt -where)/pervasives.cmi \
     -o ./run-standalone.exe

opam will install this executable as ocaml-embed-compiler.

Dependencies (8)

  1. dune >= "2.0.0"
  2. sexplib >= "v0.14" & < "v0.15"
  3. ppx_optcomp >= "v0.14" & < "v0.15"
  4. ppx_jane >= "v0.14" & < "v0.15"
  5. core_kernel >= "v0.14" & < "v0.15"
  6. core >= "v0.14" & < "v0.15"
  7. async >= "v0.14" & < "v0.15"
  8. ocaml >= "4.08.0"

Dev Dependencies

None

Used by

None

Conflicts

None

OCaml

Innovation. Community. Security.