Skip to content
This repository has been archived by the owner on Aug 25, 2022. It is now read-only.

Commit

Permalink
Update config.ml build to reduce complexity
Browse files Browse the repository at this point in the history
Now `config.ml` and `dune.inc` are copied in their own `build-config`
directory, where `dune` is also generated. Tests don't rely anymore on
"include hacks".
  • Loading branch information
TheLortex committed Feb 25, 2019
1 parent b81e8d2 commit e438a7e
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 37 deletions.
44 changes: 24 additions & 20 deletions app/functoria_app.ml
Expand Up @@ -622,32 +622,34 @@ module Make (P: S) = struct
with_fmt f

(* Compile the configuration file. *)
let compile file_ml =
Log.info (fun m -> m "Compiling: %a" Fpath.pp file_ml);
let file = Dynlink.adapt_filename (Fpath.basename file_ml)
and cfg = Fpath.rem_ext file_ml
let compile file_ml_path =
let build_dir = get_build_dir () in
let build_root = Fpath.parent build_dir in
Log.info (fun m -> m "Compiling: %a" Fpath.pp file_ml_path);
let file = Dynlink.adapt_filename (Fpath.basename file_ml_path)
and cfg = Fpath.rem_ext file_ml_path
and config_dir = Fpath.parent !config_file
and file_ml = Fpath.basename file_ml
and file_ml = Fpath.basename file_ml_path
in
Bos.OS.Path.matches Fpath.(config_dir / "_build" // cfg + "$(ext)") >>= fun files ->
Bos.OS.Path.matches Fpath.(build_dir // cfg + "$(ext)") >>= fun files ->
List.fold_left (fun r p -> r >>= fun () -> Bos.OS.Path.delete p) (R.ok ()) files >>= fun () ->
with_current config_dir
with_current build_root
(fun () ->
let target_dir = Fpath.(v "_build")
let target_dir = Fpath.(v "build-config")
(* Import files in the build target *)
and target_path = Fpath.(v "_build" / file_ml)
and target_path_dune = Fpath.(v "_build" / "config.dune")
and target_path = Fpath.(v "build-config" / file_ml)
and target_path_dune = Fpath.(v "build-config" / "config.dune")
in
Bos.OS.Dir.create target_dir >>= fun _ ->
Bos.OS.Path.symlink ~force:true ~target:(Fpath.(v ".." / file_ml)) target_path >>= fun () ->
Bos.OS.Path.exists Fpath.(v "config.dune") >>= fun res ->
Bos.OS.Path.symlink ~force:true ~target:(Fpath.(build_root // file_ml_path)) target_path >>= fun () ->
Bos.OS.Path.exists Fpath.(config_dir / "config.dune") >>= fun res ->
(match res with
| true -> (Bos.OS.Path.symlink ~force:true ~target:(Fpath.(v ".." / "config.dune")) target_path_dune >>= fun () -> Ok true)
| true -> (Bos.OS.Path.symlink ~force:true ~target:(Fpath.(build_root // config_dir / "config.dune")) target_path_dune >>= fun () -> Ok true)
| false -> Ok false)
(* Generate dune configuration file *)
>>= fun has_dune_inc ->
let dune_file = Fpath.(v "_build" / "dune")
and dune_project_file = Fpath.(v "_build" / "dune-project") in
let dune_file = Fpath.(v "build-config" / "dune")
and dune_project_file = Fpath.(v "dune-project") in
let pkgs = match P.packages with
| [] -> ""
| pkgs ->
Expand All @@ -664,12 +666,14 @@ module Make (P: S) = struct
let dune_content_default = "(library (name config) (modules config) (libraries "^pkgs^"))" in
let dune_content = if has_dune_inc then ("(include config.dune)"^dune_content_default) else dune_content_default in
let write_dune_file = Bos.OS.File.delete dune_file >>= fun () -> Bos.OS.File.write dune_file dune_content
and write_dune_project_file = Bos.OS.File.delete dune_project_file >>= fun () -> Bos.OS.File.write dune_project_file "(lang dune 1.7)"
and write_dune_project_file = Bos.OS.File.exists dune_project_file
>>= function | false -> Bos.OS.File.write dune_project_file "(lang dune 1.7)"
| true -> Ok ()
in
(* Build config.cmxa with dune *)
let target_file = Fpath.(v "_build" / "default" / file) |> Fpath.to_string in
(* Build config.cmxs with dune *)
let target_file = Fpath.(v "_build" / "default" / "build-config" / file) |> Fpath.to_string in
let cmd =
Bos.Cmd.(v "dune" % "build" % "--no-print-directory" % "--root" % "_build" % target_file)
Bos.Cmd.(v "dune" % "build" % "--root" % "." % target_file)
in
write_dune_project_file >>= fun () -> write_dune_file >>= fun () ->
Bos.OS.Cmd.run_out cmd |> Bos.OS.Cmd.out_string >>= fun (out, status) ->
Expand All @@ -689,7 +693,7 @@ module Make (P: S) = struct
* side effect to this command *)
let dynlink file =
let file = Dynlink.adapt_filename (Fpath.basename file) in
let file = Fpath.(to_string (parent !config_file / "_build" / "_build" / "default" / file)) in
let file = Fpath.(to_string (v "_build" / "default" / "build-config" / file)) in
try Ok (Dynlink.loadfile file)
with Dynlink.Error err ->
let err = Dynlink.error_message err in
Expand Down
3 changes: 1 addition & 2 deletions tests/app/config.dune
@@ -1,6 +1,5 @@
(library
(name config_custom)
(libraries functoria.test)
(modules)
)

(env (_ (flags (-I ../../../../lib/.test_app.objs/byte -I ../../../../../lib/.functoria.objs/byte))))
2 changes: 1 addition & 1 deletion tests/dune
Expand Up @@ -11,5 +11,5 @@
(modules test_full)
(libraries test_app alcotest cmdliner rresult astring)
(package functoria-runtime)
(deps app/config.ml app/app.ml app/config.dune ../runtime/functoria_runtime.ml ../runtime/functoria_info.ml)
(deps app/config.ml app/app.ml app/config.dune (package functoria))
)
8 changes: 5 additions & 3 deletions tests/lib/dune
@@ -1,5 +1,7 @@
(library
(name test_app)
(libraries functoria.app)
(wrapped false)
(name test_app)
(public_name functoria.test)
(libraries functoria.app)
(wrapped false)
(modules test_app)
)
10 changes: 3 additions & 7 deletions tests/lib/test_app.ml
Expand Up @@ -46,7 +46,7 @@ let write_key i k f =

let split_root () =
let cwd = R.get_ok @@ Bos.OS.Dir.current () in
let root = Fpath.(root () / "_build" / "default") in
let root = root () in
match Fpath.relativize ~root cwd with
| None -> failwith "split root"
| Some path -> root, path
Expand Down Expand Up @@ -76,13 +76,9 @@ module C = struct

method! configure i =
let dune = Fmt.strf
"; An infortunate hack: bring stage 0 modules in scope of stage 1\n\
(rule (copy ../../runtime/functoria_runtime.ml functoria_runtime.ml))\n\
(rule (copy ../../runtime/functoria_info.ml functoria_info.ml))\n\
\n\
(executable\n\
"(executable\n\
\ (name %s)\n\
\ (libraries cmdliner fmt))\n"
\ (libraries cmdliner fmt functoria-runtime))\n"
(output i)
in
Bos.OS.File.write (dune_file i) dune
Expand Down
7 changes: 3 additions & 4 deletions tests/test_full.ml
Expand Up @@ -86,8 +86,7 @@ let test_configure () =
test "configure -vv --file app/config.ml";
Alcotest.(check files) "new files should be created in the source dir"
["app.ml"; "config.ml"; "config.dune"; "key_gen.ml";
"main.ml"; ".mirage.config"; "dune"; "_build"
] (list_files "app");
"main.ml"; ".mirage.config"; "dune"] (list_files "app");
clean_app ();

(* check that configure generates the file in the right dir when
Expand All @@ -96,8 +95,8 @@ let test_configure () =
Alcotest.(check files) "the usual files should be present before configure"
["app.ml"; "config.ml"; "config.dune"] (list_files "app");
test "configure -vv --file app/config.ml --build-dir custom_build_";
Alcotest.(check files) "only _build should be created in the source dir"
["app.ml"; "config.ml"; "config.dune"; "_build"]
Alcotest.(check files) "nothing should be created in the source dir"
["app.ml"; "config.ml"; "config.dune"]
(list_files "app");
Alcotest.(check files) "other files should be created in custom_build_"
["main.ml"; "app.ml"; "config.dune"; ".mirage.config"; "dune"; "key_gen.ml";
Expand Down

0 comments on commit e438a7e

Please sign in to comment.