-
Notifications
You must be signed in to change notification settings - Fork 413
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
which allows to easily dynlink packages and their dependencies. Dune is needed for putting in the binary the list of package statically linked. Signed-off-by: François Bobot <francois.bobot@cea.fr>
- Loading branch information
Showing
27 changed files
with
329 additions
and
4 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
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,64 @@ | ||
open Import | ||
|
||
module CC = Compilation_context | ||
module SC = Super_context | ||
|
||
let of_libs = List.map ~f:(fun l -> Lib.Lib_and_module.Lib l) | ||
|
||
let rec cut_after_libs ~pkg_name before = function | ||
| [] -> None | ||
| a::l when Lib_name.compare (Lib.name a) pkg_name = Eq -> Some (List.rev (a::before),l) | ||
| a::l -> cut_after_libs (a::before) ~pkg_name l | ||
|
||
let findlib_dynload = Lib_name.of_string_exn ~loc:None "findlib.dynload" | ||
|
||
let libraries_link ~name ~mode cctx libs = | ||
let sctx = CC.super_context cctx in | ||
let ctx = SC.context sctx in | ||
let obj_dir = CC.obj_dir cctx in | ||
let stdlib_dir = ctx.stdlib_dir in | ||
match cut_after_libs [] ~pkg_name:findlib_dynload libs with | ||
| Some (before, after) -> | ||
(* If findlib.dynload is linked, we stores in the binary the packages linked by linking just | ||
after findlib.dynload a module containing the info *) | ||
let libs = | ||
List.filter | ||
~f:(fun lib -> match Lib.status lib with | Lib.Status.Private _ -> false | _ -> true) | ||
libs | ||
in | ||
let preds = Variant.Set.add Findlib.Package.preds (Mode.variant mode) in | ||
let s = | ||
Format.asprintf "%a@\nFindlib.record_package_predicates %a;;@." | ||
(Fmt.list ~pp_sep:Fmt.nl (fun fmt lib -> | ||
Format.fprintf fmt "Findlib.record_package Findlib.Record_core %a;;" | ||
Lib_name.pp_quoted (Lib.name lib))) | ||
libs | ||
(Fmt.ocaml_list Variant.pp) (Variant.Set.to_list preds) | ||
in | ||
let basename = Format.asprintf "%s_findlib_initl_%a" name Mode.pp mode in | ||
let ml = Path.relative obj_dir (basename ^ ".ml") in | ||
SC.add_rule sctx (Build.write_file ml s); | ||
let impl = Module.File.make OCaml ml in | ||
let name = Module.Name.of_string basename in | ||
let module_ = Module.make ~impl name in | ||
let cctx = Compilation_context.( | ||
create | ||
~super_context:sctx | ||
~scope:(scope cctx) | ||
~dir:(dir cctx) | ||
~dir_kind:(dir_kind cctx) | ||
~obj_dir:(obj_dir cctx) | ||
~modules:(Module.Name.Map.singleton name module_) | ||
~requires:(Lib.DB.find_many (SC.public_libs sctx) [Lib_name.of_string_exn ~loc:None "findlib"]) | ||
~flags:Ocaml_flags.empty | ||
~opaque:true | ||
()) | ||
in | ||
Module_compilation.build_module ~dynlink:false | ||
~dep_graphs:(Ocamldep.Dep_graphs.dummy module_) | ||
cctx | ||
module_; | ||
let lm = (of_libs before)@[Lib.Lib_and_module.Module (module_,obj_dir)]@(of_libs after) in | ||
Arg_spec.S [A "-linkall"; Lib.Lib_and_module.link_flags lm ~mode ~stdlib_dir] | ||
| None -> | ||
Lib.L.link_flags libs ~mode ~stdlib_dir |
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 @@ | ||
(** {1 Handle link time code generation} *) | ||
|
||
val libraries_link | ||
: name:string | ||
-> mode:Mode.t | ||
-> Compilation_context.t | ||
-> Lib.L.t | ||
-> _ Arg_spec.t | ||
(** Insert link time generated code for findlib_dynload in the list *) |
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
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,2 @@ | ||
let () = print_endline "a: init" | ||
let called () = print_endline "a: called" |
Empty file.
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,5 @@ | ||
let t = | ||
Thread.create | ||
(fun () -> Thread.delay 0.0001 ) () | ||
|
||
let () = Mytool.Register.register "c_thread" (fun () -> Thread.join t) |
Empty file.
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 @@ | ||
(library | ||
(name a) | ||
(public_name a) | ||
(modules a) | ||
) | ||
|
||
(library | ||
(name mytool_plugin_b) | ||
(public_name mytool-plugin-b) | ||
(modules mytool_plugin_b) | ||
(libraries a mytool) | ||
) | ||
|
||
(library | ||
(name mytool) | ||
(public_name mytool) | ||
(modules register) | ||
) | ||
|
||
(executable | ||
(name main) | ||
(modules main) | ||
(public_name mytool) | ||
(package mytool) | ||
(libraries mytool findlib.dynload threads) | ||
) | ||
|
||
|
||
(rule (copy main.ml main_with_a.ml)) | ||
|
||
(executable | ||
(name main_with_a) | ||
(modules main_with_a) | ||
(public_name mytool_with_a) | ||
(package mytool) | ||
(libraries mytool findlib.dynload a threads) | ||
) | ||
|
||
|
||
(executable | ||
(name main_auto) | ||
(modules main_auto) | ||
(public_name mytool_auto) | ||
(package mytool) | ||
(libraries mytool findlib.dynload findlib threads) | ||
) | ||
|
||
|
||
(library | ||
(name c_thread) | ||
(public_name c_thread) | ||
(modules c_thread) | ||
(libraries threads mytool) | ||
) |
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 @@ | ||
(lang dune 1.0) |
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,12 @@ | ||
let () = print_endline "m: init" | ||
|
||
let () = | ||
for i = 1 to (Array.length Sys.argv - 1); do | ||
try | ||
Fl_dynload.load_packages [Sys.argv.(i)] | ||
with | ||
| Fl_package_base.No_such_package(pkg, _) -> | ||
Printf.printf "The package %S can't be found.\n%!" pkg | ||
| Dynlink.Error error -> | ||
Printf.printf "Error during dynlink: %s\n%!" (Dynlink.error_message error) | ||
done |
11 changes: 11 additions & 0 deletions
11
test/blackbox-tests/test-cases/findlib-dynload/main_auto.ml
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,11 @@ | ||
let () = print_endline "m: init" | ||
|
||
let () = Findlib.init () | ||
let () = | ||
let pkgs = Fl_package_base.list_packages () in | ||
let pkgs = | ||
List.filter | ||
(fun pkg -> 14 <= String.length pkg && String.sub pkg 0 14 = "mytool-plugin-") | ||
pkgs | ||
in | ||
Fl_dynload.load_packages pkgs |
Empty file.
Empty file.
4 changes: 4 additions & 0 deletions
4
test/blackbox-tests/test-cases/findlib-dynload/mytool_plugin_b.ml
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,4 @@ | ||
let () = print_endline "b: init" | ||
let called () = print_endline "b: called"; A.called () | ||
|
||
let () = Mytool.Register.register "b" called |
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 @@ | ||
let register s f = print_endline (s^": registering"); f () |
Oops, something went wrong.