Skip to content

Commit

Permalink
fix: link time code gen (ocaml#6606)
Browse files Browse the repository at this point in the history
do not "guess" object names when generating link time modules.
Actually look up the generated module and find the object name

Signed-off-by: Rudi Grinberg <[email protected]>
  • Loading branch information
rgrinberg authored and moyodiallo committed Dec 2, 2022
1 parent 9bfb1bf commit a872a4b
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 25 deletions.
34 changes: 12 additions & 22 deletions src/dune_rules/link_time_code_gen.ml
Original file line number Diff line number Diff line change
Expand Up @@ -7,32 +7,22 @@ type t =

let generate_and_compile_module cctx ~precompiled_cmi ~name ~lib ~code ~requires
=
let open Resolve.Memo.O in
let sctx = Compilation_context.super_context cctx in
let open Memo.O in
let* module_ =
let gen_module =
let src_dir =
let obj_dir = Compilation_context.obj_dir cctx in
Obj_dir.obj_dir obj_dir
in
Module.generated ~kind:Impl ~src_dir name
let+ modules = Dir_contents.modules_of_lib sctx lib in
let obj_name =
Option.map modules ~f:(fun modules ->
let mli_only = Modules.find modules name |> Option.value_exn in
Module.obj_name mli_only)
in
let* wrapped = Lib.wrapped lib in
match wrapped with
| None -> Resolve.Memo.return gen_module
| Some (Yes_with_transition _) ->
(* XXX this needs a comment. Why is this impossible? *)
assert false
| Some (Simple false) -> Resolve.Memo.return gen_module
| Some (Simple true) ->
let+ main_module_name = Lib.main_module_name lib in
let main_module_name = Option.value_exn main_module_name in
(* XXX this is fishy. We shouldn't be introducing a toplevel module into a
wrapped library with a single module *)
Module.with_wrapper gen_module ~main_module_name
let src_dir =
let obj_dir = Compilation_context.obj_dir cctx in
Obj_dir.obj_dir obj_dir
in
Module.generated ?obj_name ~kind:Impl ~src_dir name
in
let open Memo.O in
let* () =
let sctx = Compilation_context.super_context cctx in
let dir = Compilation_context.dir cctx in
Super_context.add_rule ~dir sctx
(let ml =
Expand Down
8 changes: 6 additions & 2 deletions src/dune_rules/module.ml
Original file line number Diff line number Diff line change
Expand Up @@ -315,8 +315,12 @@ let ml_source =

let set_src_dir t ~src_dir = map_files t ~f:(fun _ -> File.set_src_dir ~src_dir)

let generated ~(kind : Kind.t) ~src_dir name =
let obj_name = Module_name.Unique.of_name_assuming_needs_no_mangling name in
let generated ?obj_name ~(kind : Kind.t) ~src_dir name =
let obj_name =
match obj_name with
| Some obj_name -> obj_name
| None -> Module_name.Unique.of_name_assuming_needs_no_mangling name
in
let source =
let impl =
let basename = String.uncapitalize (Module_name.to_string name) in
Expand Down
7 changes: 6 additions & 1 deletion src/dune_rules/module.mli
Original file line number Diff line number Diff line change
Expand Up @@ -130,4 +130,9 @@ val set_src_dir : t -> src_dir:Path.t -> t
XXX should this return the path of the source as well? it will almost always
be used to create the rule to generate this file *)
val generated : kind:Kind.t -> src_dir:Path.Build.t -> Module_name.t -> t
val generated :
?obj_name:Module_name.Unique.t
-> kind:Kind.t
-> src_dir:Path.Build.t
-> Module_name.t
-> t

0 comments on commit a872a4b

Please sign in to comment.