forked from ocaml/dune
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a dune top command for better toplevel integration (ocaml#2952)
Add a "dune top" command one is expected to call via: # #use_output "dune top";; In the toplevel. This command should work in any toplevel.
- Loading branch information
1 parent
aad1824
commit 8c0479d
Showing
13 changed files
with
188 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
open Stdune | ||
open Import | ||
|
||
let doc = | ||
"Print a list of toplevel directives for including directories and loading \ | ||
cma files." | ||
|
||
let man = | ||
[ `S "DESCRIPTION" | ||
; `P | ||
{|Print a list of toplevel directives for including directories and loading cma files.|} | ||
; `P | ||
{|The output of $(b,dune toplevel-init-file) should be evaluated in a toplevel | ||
to make a library available there.|} | ||
; `Blocks Common.help_secs | ||
] | ||
|
||
let info = Term.info "top" ~doc ~man | ||
|
||
let link_deps link ~lib_config = | ||
List.concat_map link ~f:(fun t -> | ||
Dune.Lib.link_deps t Dune.Link_mode.Byte lib_config) | ||
|
||
let term = | ||
let+ common = Common.term | ||
and+ dir = Arg.(value & pos 0 string "" & Arg.info [] ~docv:"DIR") | ||
and+ ctx_name = | ||
Common.context_arg ~doc:{|Select context where to build/run utop.|} | ||
in | ||
Common.set_common common ~targets:[]; | ||
Scheduler.go ~common (fun () -> | ||
let open Fiber.O in | ||
let* setup = Import.Main.setup common in | ||
let sctx = | ||
Dune.Context_name.Map.find setup.scontexts ctx_name |> Option.value_exn | ||
in | ||
let dir = | ||
Path.Build.relative | ||
(Super_context.build_dir sctx) | ||
(Common.prefix_target common dir) | ||
in | ||
let scope = Super_context.find_scope_by_dir sctx dir in | ||
let db = Dune.Scope.libs scope in | ||
let libs = Dune.Utop.libs_under_dir sctx ~db ~dir:(Path.build dir) in | ||
let requires = Dune.Lib.closure ~linking:true libs |> Result.ok_exn in | ||
let include_paths = Dune.Lib.L.include_paths requires in | ||
let lib_config = sctx |> Super_context.context |> Context.lib_config in | ||
let files = link_deps requires ~lib_config in | ||
let* () = do_build (List.map files ~f:(fun f -> Target.File f)) in | ||
let files_to_load = | ||
List.filter files ~f:(fun p -> | ||
let ext = Path.extension p in | ||
ext = Dune.Mode.compiled_lib_ext Byte || ext = Dune.Cm_kind.ext Cmo) | ||
in | ||
Dune.Toplevel.print_toplevel_init_file ~include_paths ~files_to_load; | ||
Fiber.return ()) | ||
|
||
let command = (term, info) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -29,3 +29,4 @@ Welcome to dune's documentation! | |
known-issues | ||
migration | ||
caching | ||
toplevel-integration |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
******************** | ||
Toplevel integration | ||
******************** | ||
|
||
It's possible to load dune projects in any toplevel. This is achieved in two stages. | ||
|
||
First, `dune toplevel-init-file` builds the project and produces a list of toplevel pragmas | ||
(#directory and #load). Copying the output of this command to a toplevel lets you | ||
interact with the project's modules. | ||
|
||
Second, to enhance usability, dune also provides a toplevel script, which does the above | ||
manual work for you. To use it, make sure to have `topfind` available in your toplevel by | ||
invoking `#use "topfind";;`. Afterwards you can run `#use "dune";;` and your | ||
modules should be available. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
Test toplevel-init-file on a tiny project | ||
---------------------------------------------------- | ||
$ cat >dune-project <<EOF | ||
> (lang dune 2.1) | ||
> (name test) | ||
> EOF | ||
$ cat >dune <<EOF | ||
> (library | ||
> (name test) | ||
> (public_name test)) | ||
> EOF | ||
$ touch test.opam | ||
$ cat >main.ml <<EOF | ||
> let hello () = print_endline "hello" | ||
> EOF | ||
|
||
$ dune top | ||
#directory "$TESTCASE_ROOT/_build/default/.test.objs/byte";; | ||
#directory "$TESTCASE_ROOT/_build/default/.test.objs/native";; | ||
#load "$TESTCASE_ROOT/_build/default/test.cma";; | ||
|
||
$ ocaml -stdin <<EOF | ||
> #use "topfind";; | ||
> #use "use_output_compat";; | ||
> #use_output "dune top";; | ||
> Test.Main.hello ();; | ||
> EOF | ||
hello | ||
|
||
$ cat >error.ml <<EOF | ||
> let oops () = undefined_function () | ||
> EOF | ||
|
||
$ dune top | ||
File "error.ml", line 1, characters 14-32: | ||
1 | let oops () = undefined_function () | ||
^^^^^^^^^^^^^^^^^^ | ||
Error: Unbound value undefined_function | ||
[1] | ||
|
||
$ ocaml -stdin <<EOF | ||
> #use "topfind";; | ||
> #use "use_output_compat";; | ||
> #use_output "dune top";; | ||
> EOF | ||
File "error.ml", line 1, characters 14-32: | ||
1 | let oops () = undefined_function () | ||
^^^^^^^^^^^^^^^^^^ | ||
Error: Unbound value undefined_function | ||
Command exited with code 1. |
27 changes: 27 additions & 0 deletions
27
test/blackbox-tests/test-cases/toplevel-integration/use_output_compat
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
(* -*- tuareg -*- *) | ||
|
||
let try_finally ~always f = | ||
match f () with | ||
| x -> | ||
always (); | ||
x | ||
| exception e -> | ||
always (); | ||
raise e | ||
|
||
let use_output command = | ||
let fn = Filename.temp_file "ocaml" "_toploop.ml" in | ||
try_finally | ||
~always:(fun () -> try Sys.remove fn with Sys_error _ -> ()) | ||
(fun () -> | ||
match | ||
Printf.ksprintf Sys.command "%s > %s" command (Filename.quote fn) | ||
with | ||
| 0 -> ignore (Toploop.use_file Format.std_formatter fn : bool) | ||
| n -> Format.printf "Command exited with code %d.@." n) | ||
|
||
let () = | ||
let name = "use_output" in | ||
if not (Hashtbl.mem Toploop.directive_table name) then | ||
Hashtbl.add Toploop.directive_table name | ||
(Toploop.Directive_string use_output) |