-
Notifications
You must be signed in to change notification settings - Fork 411
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for loading libraries from toplevel
* Start work on loading dune libraries from toplevel * Finish draft version of toplevel-init-file * Add toplevel loading script * Add install stanza for dune.mlt * Remove dependency on Unix in the toplevel script * Add documentation for toplevel integration * Add a very basic test for toplevel-init-file
- Loading branch information
Showing
14 changed files
with
143 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
let exception AbnormalExit of string * int in | ||
let run_and_get_lines cmd tmp_filename = | ||
let err_code = Sys.command (cmd ^ " > " ^ tmp_filename) in | ||
if err_code != 0 then | ||
raise (AbnormalExit (cmd, err_code)); | ||
let inp = open_in tmp_filename in | ||
let res = ref [] in | ||
let () = try | ||
while true do | ||
let line = input_line inp in | ||
res := List.cons line !res | ||
done | ||
with End_of_file -> | ||
close_in inp | ||
in | ||
List.rev(!res) | ||
in | ||
let exec s = | ||
let l = Lexing.from_string s in | ||
let ph = !Toploop.parse_toplevel_phrase l in | ||
let fmt = Format.make_formatter (fun _ _ _ -> ()) (fun _ -> ()) in | ||
let _ = Toploop.execute_phrase false fmt ph in () | ||
in | ||
let tmp_filename = Filename.temp_file "libraries" "top" in | ||
let lines = run_and_get_lines "dune.exe toplevel-init-file" tmp_filename in | ||
List.iter (fun l -> exec l) lines |
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,54 @@ | ||
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 "toplevel-init-file" ~doc ~man | ||
|
||
let link_deps link ~lib_config = | ||
List.map link ~f:(fun t -> Dune.Lib.link_deps t Dune.Link_mode.Byte lib_config) | ||
|> List.flatten | ||
|
||
let term = | ||
let+ common = Common.term | ||
and+ dir = Arg.(value & pos 0 string "" & Arg.info [] ~docv:"DIR") 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 Dune.Context_name.default | ||
|> 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 -> | ||
match Path.extension p with | ||
| ".cma" | ".cmo" -> true | ||
| _ -> false) | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
(library | ||
(name test) | ||
(public_name test)) |
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,9 @@ | ||
Test toplevel-init-file on empty project | ||
---------------------------------------------------- | ||
$ dune toplevel-init-file | ||
Info: Creating file dune-project with this contents: | ||
| (lang dune 2.1) | ||
| (name test) | ||
#directory "$TESTCASE_ROOT/_build/default/.test.objs/byte";; | ||
#directory "$TESTCASE_ROOT/_build/default/.test.objs/native";; | ||
#load "$TESTCASE_ROOT/_build/default/test.cma";; |
Empty file.