Install Catala plugins

Fixes #378

- the plugins are compiled as libraries rather than with `executable`, so that
  dune is able to install them
- they get installed to `lib/catala/plugins/<plugin-name>/<plugin-name>.cmxs`
- the lookup for plugins is now recursive to cope with the plugin subdirectories
  in the point above
This commit is contained in:
Louis Gesbert 2023-01-17 13:46:21 +01:00
parent 7ae7c9ceaf
commit 467a338b6c
4 changed files with 27 additions and 11 deletions

View File

@ -172,7 +172,7 @@ let plugins_dirs =
let default = let default =
let ( / ) = Filename.concat in let ( / ) = Filename.concat in
[ [
Sys.executable_name Filename.dirname Sys.executable_name
/ Filename.parent_dir_name / Filename.parent_dir_name
/ "lib" / "lib"
/ "catala" / "catala"

View File

@ -73,7 +73,15 @@ let driver source_file (options : Cli.options) : int =
try `Plugin (Plugin.find s) try `Plugin (Plugin.find s)
with Not_found -> with Not_found ->
Errors.raise_error Errors.raise_error
"The selected backend (%s) is not supported by Catala" backend) "The selected backend (%s) is not supported by Catala, nor was a \
plugin by this name found under %a"
backend
(Format.pp_print_list
~pp_sep:(fun ppf () -> Format.fprintf ppf "@ or @ ")
(fun ppf dir ->
Format.pp_print_string ppf
(try Unix.readlink dir with _ -> dir)))
options.plugins_dirs)
in in
let prgm = let prgm =
Surface.Parser_driver.parse_top_level_file source_file language Surface.Parser_driver.parse_top_level_file source_file language

View File

@ -58,12 +58,16 @@ let load_file f =
Errors.format_warning "Could not load plugin %S: %s" f Errors.format_warning "Could not load plugin %S: %s" f
(Printexc.to_string e) (Printexc.to_string e)
let load_dir d = let rec load_dir d =
let dynlink_exts = let dynlink_exts =
if Dynlink.is_native then [".cmxs"] else [".cmo"; ".cma"] if Dynlink.is_native then [".cmxs"] else [".cmo"; ".cma"]
in in
Array.iter Array.iter
(fun f -> (fun f ->
if List.exists (Filename.check_suffix f) dynlink_exts then if f.[0] = '.' then ()
load_file (Filename.concat d f)) else
let f = Filename.concat d f in
if Sys.is_directory f then load_dir f
else if List.exists (Filename.check_suffix f) dynlink_exts then
load_file f)
(Sys.readdir d) (Sys.readdir d)

View File

@ -1,18 +1,22 @@
(executable (library
(name python) (name python)
(modes plugin) (public_name catala.plugins.python)
(synopsis
"Demonstration Catala plugin that reproduces the behaviour of the built-in python backend")
(modules python) (modules python)
(libraries catala.driver)) (libraries catala.driver))
(executable (library
(name api_web) (name api_web)
(modes plugin) (public_name catala.plugins.api_web)
(synopsis "Catala plugin for interaction with a web interface")
(modules api_web) (modules api_web)
(libraries catala.driver)) (libraries catala.driver))
(executable (library
(name json_schema) (name json_schema)
(modes plugin) (public_name catala.plugins.json_schema)
(synopsis "Catala plugin generating JSON schemas useful to build web-forms")
(modules json_schema) (modules json_schema)
(libraries catala.driver)) (libraries catala.driver))