Some fixes to relative file lookups in modules and tests

This makes sure `catala module` finds the local runtime when run from the catala
source tree; and fixes lookup of the catala exec on custom uses of `clerk runtest`.
This commit is contained in:
Louis Gesbert 2023-08-25 15:55:50 +02:00
parent 17172cf47d
commit a71b4e7f73
4 changed files with 42 additions and 27 deletions

View File

@ -197,7 +197,13 @@ let run_inline_tests
let cmd_out_rd, cmd_out_wr = Unix.pipe () in
let ic = Unix.in_channel_of_descr cmd_out_rd in
let file_dir, file = Filename.dirname file, Filename.basename file in
let catala_exe = Unix.realpath catala_exe in
let catala_exe =
(* If the exe name contains directories, make it absolute. Otherwise
don't modify it so that it can be looked up in PATH. *)
if String.contains catala_exe Filename.dir_sep.[0] then
Unix.realpath catala_exe
else catala_exe
in
let cmd =
Array.of_list ((catala_exe :: catala_opts) @ test.params @ [file])
in

View File

@ -84,24 +84,37 @@ let ocaml_libdir =
"Could not locate the OCaml library directory, make sure OCaml or \
opam is installed")))
let rec find_catala_project_root dir =
if Sys.file_exists File.(dir / "catala.opam") then Some dir
else
let dir' = Unix.realpath File.(dir / Filename.parent_dir_name) in
if dir' = dir then None else find_catala_project_root dir'
let runtime_dir =
lazy
(match
List.find_map File.check_directory
[
"_build/install/default/lib/catala/runtime_ocaml";
(* Relative dir when running from catala source *)
File.(Lazy.force ocaml_libdir / "catala" / "runtime");
]
with
| Some dir ->
Message.emit_debug "Catala runtime libraries found at @{<bold>%s@}." dir;
dir
| None ->
Message.raise_error
"Could not locate the Catala runtime library.@ Make sure that either \
catala is correctly installed,@ or you are running from the root of a \
compiled source tree.")
(let d =
match find_catala_project_root (Sys.getcwd ()) with
| Some root ->
(* Relative dir when running from catala source *)
File.(
root
/ "_build"
/ "install"
/ "default"
/ "lib"
/ "catala"
/ "runtime_ocaml")
| None -> File.(Lazy.force ocaml_libdir / "catala" / "runtime")
in
match File.check_directory d with
| Some dir ->
Message.emit_debug "Catala runtime libraries found at @{<bold>%s@}." dir;
dir
| None ->
Message.raise_error
"@[<hov>Could not locate the Catala runtime library.@ Make sure that \
either catala is correctly installed,@ or you are running from the \
root of a compiled source tree.@]")
let compile options link_modules optimize check_invariants =
let modname =

View File

@ -910,5 +910,10 @@ let load_runtime_modules = function
List.iter
Dynlink.(
fun m ->
loadfile (adapt_filename (Filename.remove_extension m ^ ".cmo")))
try loadfile (adapt_filename (Filename.remove_extension m ^ ".cmo"))
with Dynlink.Error dl_err ->
Message.raise_error
"Could not load module %s, has it been suitably compiled?@;\
<1 2>@[<hov>%a@]" m Format.pp_print_text
(Dynlink.error_message dl_err))
modules

View File

@ -101,15 +101,6 @@ let external_ref fmt er =
| External_value v -> TopdefName.format fmt v
| External_scope s -> ScopeName.format fmt s
let rec module_ctx ctx = function
| [] -> ctx
| (modname, mpos) :: path -> (
match ModuleName.Map.find_opt modname ctx.ctx_modules with
| None ->
Message.raise_spanned_error mpos "Module %a not found" ModuleName.format
modname
| Some ctx -> module_ctx ctx path)
let rec typ_gen
(ctx : decl_ctx option)
~(colors : Ocolor_types.color4 list)