Skip to content

Commit

Permalink
finish root_module
Browse files Browse the repository at this point in the history
Signed-off-by: Rudi Grinberg <[email protected]>
  • Loading branch information
rgrinberg committed Nov 16, 2020
1 parent d797c34 commit ae14871
Show file tree
Hide file tree
Showing 21 changed files with 229 additions and 325 deletions.
48 changes: 13 additions & 35 deletions src/dune_rules/compilation_context.ml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ open! Stdune
open Import
module SC = Super_context

let modules_of_lib = Fdecl.create Dyn.Encoder.opaque

module Includes = struct
type t = Command.Args.dynamic Command.Args.t Cm_kind.Dict.t

Expand Down Expand Up @@ -70,7 +72,6 @@ type t =
; vimpl : Vimpl.t option
; modes : Mode.Dict.Set.t
; bin_annot : bool
; modules_of_lib : Lib.t -> Modules.t Or_exn.t
}

let super_context t = t.super_context
Expand Down Expand Up @@ -115,31 +116,10 @@ let bin_annot t = t.bin_annot

let context t = Super_context.context t.super_context

type rename =
{ new_name : Module_name.t
; old_name : Module_name.t
}

let renames t =
let open Result.O in
let* renames = t.renames in
Result.List.map renames ~f:(fun (lib, new_name) ->
let* main_module_name = Lib.main_module_name lib in
let+ old_name =
match main_module_name with
| Some m -> Ok m
| None ->
Error
(User_error.E
(User_error.make
[ Pp.text "renaming unwrapped not supported yet" ]))
in
{ new_name; old_name })

let create ~super_context ~scope ~expander ~obj_dir ~modules ~flags
~requires_compile ~requires_link ?(preprocessing = Pp_spec.dummy) ~opaque
?stdlib ~js_of_ocaml ~dynlink ~package ?vimpl ?modes ?(bin_annot = true)
?(renames = Ok []) () =
?stdlib ~js_of_ocaml ~dynlink ~package ?vimpl ?modes ?(bin_annot = true) ()
=
let project = Scope.project scope in
let requires_compile =
if Dune_project.implicit_transitive_deps project then
Expand Down Expand Up @@ -180,15 +160,14 @@ let create ~super_context ~scope ~expander ~obj_dir ~modules ~flags
; vimpl
; modes
; bin_annot
; renames
}

let for_alias_module t =
let flags =
let project = Scope.project t.scope in
let dune_version = Dune_project.dune_version project in
Ocaml_flags.default ~profile:(Super_context.context t.super_context).profile
~dune_version
let profile = (Super_context.context t.super_context).profile in
Ocaml_flags.default ~dune_version ~profile
in
let sandbox =
let ctx = Super_context.context t.super_context in
Expand All @@ -213,8 +192,8 @@ let for_root_module t =
let flags =
let project = Scope.project t.scope in
let dune_version = Dune_project.dune_version project in
Ocaml_flags.default ~profile:(Super_context.context t.super_context).profile
~dune_version
let profile = (Super_context.context t.super_context).profile in
Ocaml_flags.default ~profile ~dune_version
in
{ t with
flags =
Expand Down Expand Up @@ -252,9 +231,8 @@ let for_plugin_executable t ~embed_in_plugin_libraries =

let without_bin_annot t = { t with bin_annot = false }

let root_module_entries t =
Some (
let+ requires = t.requires_compile in
List.map requires ~f:(fun require ->
)
)
let root_module_entries t : Module_name.t list Or_exn.t =
let open Result.O in
let* requires = t.requires_compile in
let local_lib = Fdecl.get modules_of_lib t.super_context in
Result.List.concat_map requires ~f:(Lib.entry_module_names ~local_lib)
7 changes: 5 additions & 2 deletions src/dune_rules/compilation_context.mli
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ type opaque =
| Inherit_from_settings
(** Determined from the version of OCaml and the profile *)

val modules_of_lib :
(* to avoid a cycle with [Dir_contents] *)
(Super_context.t -> dir:Path.Build.t -> name:Lib_name.t -> Modules.t) Fdecl.t

(** Create a compilation context. *)
val create :
super_context:Super_context.t
Expand Down Expand Up @@ -102,5 +106,4 @@ val bin_annot : t -> bool

val without_bin_annot : t -> t

val root_module_entries :
t -> (Module_name.t * Module_name.t list) list Or_exn.t option
val root_module_entries : t -> Module_name.t list Or_exn.t
8 changes: 8 additions & 0 deletions src/dune_rules/dir_contents.ml
Original file line number Diff line number Diff line change
Expand Up @@ -336,6 +336,14 @@ end = struct
| See_above _ -> assert false
| Here { t; rules = _; subdirs = _ } -> t )

let () =
let f sctx ~dir ~name =
let t = get sctx ~dir in
let ml_sources = ocaml t in
Ml_sources.modules_of_library ml_sources ~name
in
Fdecl.set Compilation_context.modules_of_lib f

let gen_rules sctx ~dir =
match Memo.exec memo0 (sctx, dir) with
| See_above group_root -> Group_part group_root
Expand Down
12 changes: 8 additions & 4 deletions src/dune_rules/dune_file.ml
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,10 @@ module Buildable = struct
repeat (String_with_vars.decode >>| version_check)
in
(libname, flags))) ))
and+ root_module = field_o "root_module" Module_name.decode_loc in
and+ root_module =
field_o "root_module"
(Dune_lang.Syntax.since Stanza.syntax (2, 8) >>> Module_name.decode_loc)
in
let preprocess =
let init =
let f libname = Preprocess.With_instrumentation.Ordinary libname in
Expand Down Expand Up @@ -913,13 +916,14 @@ module Library = struct
let wrapped = Some conf.wrapped in
let special_builtin_support = conf.special_builtin_support in
let instrumentation_backend = conf.instrumentation_backend in
let entry_modules = Lib_info.Source.Local in
Lib_info.create ~loc ~name ~kind ~status ~src_dir ~orig_src_dir ~obj_dir
~version ~synopsis ~main_module_name ~sub_systems ~requires
~foreign_objects ~plugins ~archives ~ppx_runtime_deps ~foreign_archives
~native_archives ~foreign_dll_files ~jsoo_runtime ~jsoo_archive
~preprocess ~enabled ~virtual_deps ~dune_version ~virtual_ ~implements
~default_implementation ~modes ~wrapped ~special_builtin_support
~exit_module ~instrumentation_backend
~preprocess ~enabled ~virtual_deps ~dune_version ~virtual_ ~entry_modules
~implements ~default_implementation ~modes ~wrapped
~special_builtin_support ~exit_module ~instrumentation_backend
end

module Plugin = struct
Expand Down
18 changes: 10 additions & 8 deletions src/dune_rules/dune_package.ml
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ module Lib = struct
and+ orig_src_dir = field_o "orig_src_dir" path
and+ modules =
let src_dir = Obj_dir.dir obj_dir in
field_o "modules"
field "modules"
(Modules.decode
~implements:(Option.is_some implements)
~src_dir ~version:lang.version)
Expand All @@ -153,6 +153,9 @@ module Lib = struct
field_o "instrumentation.backend" (located Lib_name.decode)
in
let modes = Mode.Dict.Set.of_list modes in
let entry_modules =
Modules.entry_modules modules |> List.map ~f:Module.name
in
let info : Path.t Lib_info.t =
let src_dir = Obj_dir.dir obj_dir in
let enabled = Lib_info.Enabled_status.Normal in
Expand All @@ -170,25 +173,24 @@ module Lib = struct
let dune_version = None in
let virtual_ =
if virtual_ then
let modules = Option.value_exn modules in
Some (Lib_info.Source.External modules)
else
None
in
let wrapped =
Option.map modules ~f:Modules.wrapped
|> Option.map ~f:(fun w -> Lib_info.Inherited.This w)
Some (Lib_info.Inherited.This (Modules.wrapped modules))
in
let entry_modules = Lib_info.Source.External (Ok entry_modules) in
Lib_info.create ~loc ~name ~kind ~status ~src_dir ~orig_src_dir
~obj_dir ~version ~synopsis ~main_module_name ~sub_systems ~requires
~foreign_objects ~plugins ~archives ~ppx_runtime_deps
~foreign_archives ~native_archives ~foreign_dll_files:[]
~jsoo_runtime ~jsoo_archive ~preprocess ~enabled ~virtual_deps
~dune_version ~virtual_ ~implements ~default_implementation ~modes
~wrapped ~special_builtin_support ~exit_module:None
~instrumentation_backend
~dune_version ~virtual_ ~entry_modules ~implements
~default_implementation ~modes ~wrapped ~special_builtin_support
~exit_module:None ~instrumentation_backend
in
{ info; main_module_name; modules })
{ info; main_module_name; modules = Some modules })

let modules t = t.modules

Expand Down
30 changes: 28 additions & 2 deletions src/dune_rules/findlib/findlib.ml
Original file line number Diff line number Diff line change
Expand Up @@ -330,13 +330,14 @@ end = struct
let virtual_ = None in
let default_implementation = None in
let wrapped = None in
let dir_contents = Path.readdir_unsorted t.dir in
let foreign_archives, native_archives =
(* Here we scan [t.dir] and consider all files named [lib*.ext_lib] to
be foreign archives, and all other files with the extension
[ext_lib] to be native archives. The resulting lists of archives
will be used to compute appropriate flags for linking dependent
executables. *)
match Path.readdir_unsorted t.dir with
match dir_contents with
| Error _ ->
(* Raising an error is not an option here as we systematically delay
all library loading errors until the libraries are actually used
Expand Down Expand Up @@ -368,14 +369,39 @@ end = struct
let sort = List.sort ~compare:Path.compare in
(sort foreign_archives, sort native_archives)
in
let entry_modules =
Lib_info.Source.External
( match dir_contents with
| Error e ->
Error
(User_error.E
(User_message.make
[ Pp.textf "Unable to get entry modules of %s in %s. "
(Lib_name.to_string t.name)
(Path.to_string src_dir)
; Pp.textf "error: %s" (Unix.error_message e)
]))
| Ok files ->
let ext = Cm_kind.ext Cmi in
Result.List.filter_map files ~f:(fun fname ->
match Filename.check_suffix fname ext with
| false -> Ok None
| true -> (
match
let name = Filename.chop_extension fname in
Module_name.of_string_user_error (Loc.in_dir src_dir, name)
with
| Ok s -> Ok (Some s)
| Error e -> Error (User_error.E e) )) )
in
Lib_info.create ~loc ~name:t.name ~kind ~status ~src_dir ~orig_src_dir
~obj_dir ~version ~synopsis ~main_module_name ~sub_systems ~requires
~foreign_objects ~plugins ~archives ~ppx_runtime_deps
~foreign_archives ~native_archives ~foreign_dll_files:[] ~jsoo_runtime
~jsoo_archive ~preprocess ~enabled ~virtual_deps ~dune_version
~virtual_ ~implements ~default_implementation ~modes ~wrapped
~special_builtin_support ~exit_module:None
~instrumentation_backend:None
~instrumentation_backend:None ~entry_modules
in
Dune_package.Lib.make ~info ~modules:None ~main_module_name:None
end
Expand Down
8 changes: 8 additions & 0 deletions src/dune_rules/lib.ml
Original file line number Diff line number Diff line change
Expand Up @@ -366,6 +366,14 @@ let main_module_name t =
| This x -> x
| From _ -> assert false )

let entry_module_names t ~local_lib =
match Lib_info.entry_modules t.info with
| External d -> d
| Local ->
let info = Lib_info.as_local_exn t.info in
let modules = local_lib ~dir:(Lib_info.src_dir info) ~name:t.name in
Ok (Modules.entry_modules modules |> List.map ~f:Module.name)

let wrapped t =
let wrapped = Lib_info.wrapped t.info in
match wrapped with
Expand Down
5 changes: 5 additions & 0 deletions src/dune_rules/lib.mli
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,11 @@ val info : t -> Path.t Lib_info.t

val main_module_name : t -> Module_name.t option Or_exn.t

val entry_module_names :
t
-> local_lib:(dir:Path.Build.t -> name:Lib_name.t -> Modules.t)
-> Module_name.t list Or_exn.t

val wrapped : t -> Wrapped.t option Or_exn.t

(** [is_impl lib] returns [true] if the library is an implementation of a
Expand Down
13 changes: 10 additions & 3 deletions src/dune_rules/lib_info.ml
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,7 @@ type 'path t =
; dune_version : Dune_lang.Syntax.Version.t option
; sub_systems : Sub_system_info.t Sub_system_name.Map.t
; virtual_ : Modules.t Source.t option
; entry_modules : Module_name.t list Or_exn.t Source.t
; implements : (Loc.t * Lib_name.t) option
; default_implementation : (Loc.t * Lib_name.t) option
; wrapped : Wrapped.t Inherited.t option
Expand Down Expand Up @@ -389,9 +390,9 @@ let create ~loc ~name ~kind ~status ~src_dir ~orig_src_dir ~obj_dir ~version
~synopsis ~main_module_name ~sub_systems ~requires ~foreign_objects ~plugins
~archives ~ppx_runtime_deps ~foreign_archives ~native_archives
~foreign_dll_files ~jsoo_runtime ~jsoo_archive ~preprocess ~enabled
~virtual_deps ~dune_version ~virtual_ ~implements ~default_implementation
~modes ~wrapped ~special_builtin_support ~exit_module
~instrumentation_backend =
~virtual_deps ~dune_version ~virtual_ ~entry_modules ~implements
~default_implementation ~modes ~wrapped ~special_builtin_support
~exit_module ~instrumentation_backend =
{ loc
; name
; kind
Expand All @@ -418,6 +419,7 @@ let create ~loc ~name ~kind ~status ~src_dir ~orig_src_dir ~obj_dir ~version
; dune_version
; sub_systems
; virtual_
; entry_modules
; implements
; default_implementation
; modes
Expand Down Expand Up @@ -490,6 +492,7 @@ let to_dyn path
; special_builtin_support
; exit_module
; instrumentation_backend
; entry_modules
} =
let open Dyn.Encoder in
let snd f (_, x) = f x in
Expand Down Expand Up @@ -518,6 +521,8 @@ let to_dyn path
; ("dune_version", option Dune_lang.Syntax.Version.to_dyn dune_version)
; ("sub_systems", Sub_system_name.Map.to_dyn Dyn.Encoder.opaque sub_systems)
; ("virtual_", option (Source.to_dyn Modules.to_dyn) virtual_)
; ( "entry_modules"
, Source.to_dyn (Or_exn.to_dyn (list Module_name.to_dyn)) entry_modules )
; ("implements", option (snd Lib_name.to_dyn) implements)
; ( "default_implementation"
, option (snd Lib_name.to_dyn) default_implementation )
Expand All @@ -543,3 +548,5 @@ let has_native_archive lib_config modules =
Lib_config.linker_can_create_empty_archives lib_config
&& Ocaml_version.ocamlopt_always_calls_library_linker lib_config.ocaml_version
|| not (Modules.is_empty modules)

let entry_modules t = t.entry_modules
3 changes: 3 additions & 0 deletions src/dune_rules/lib_info.mli
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,8 @@ val obj_dir : 'path t -> 'path Obj_dir.t

val virtual_ : _ t -> Modules.t Source.t option

val entry_modules : _ t -> Module_name.t list Or_exn.t Source.t

val main_module_name : _ t -> Main_module_name.t

val wrapped : _ t -> Wrapped.t Inherited.t option
Expand Down Expand Up @@ -215,6 +217,7 @@ val create :
-> virtual_deps:(Loc.t * Lib_name.t) list
-> dune_version:Dune_lang.Syntax.Version.t option
-> virtual_:Modules.t Source.t option
-> entry_modules:Module_name.t list Or_exn.t Source.t
-> implements:(Loc.t * Lib_name.t) option
-> default_implementation:(Loc.t * Lib_name.t) option
-> modes:Mode.Dict.Set.t
Expand Down
9 changes: 6 additions & 3 deletions src/dune_rules/ml_sources.ml
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,7 @@ let make_lib_modules (d : _ Dir_with_dune.t) ~lookup_vlib ~(lib : Library.t)
Modules_field_evaluator.eval ~modules ~buildable:lib.buildable ~kind
~private_modules:
(Option.value ~default:Ordered_set_lang.standard lib.private_modules)
~src_dir
in
let stdlib = lib.stdlib in
let implements = Option.is_some lib.implements in
Expand All @@ -280,20 +281,22 @@ let libs_and_exes (d : _ Dir_with_dune.t) ~lookup_vlib ~modules =
match (stanza : Stanza.t) with
| Library lib ->
let modules =
make_lib_modules d ~lookup_vlib ~modules ~lib ~force_alias_module:false
make_lib_modules d ~lookup_vlib ~modules ~lib
~force_alias_module:false
in
Left (lib, modules)
| Executables exes
| Tests { exes; _ } ->
let src_dir = d.ctx_dir in
let modules =
Modules_field_evaluator.eval ~modules ~buildable:exes.buildable
~kind:Modules_field_evaluator.Exe_or_normal_lib
~private_modules:Ordered_set_lang.standard
~private_modules:Ordered_set_lang.standard ~src_dir
in
let modules =
let project = Scope.project d.scope in
if Dune_project.wrapped_executables project then
Modules_group.exe_wrapped ~src_dir:d.ctx_dir ~modules
Modules_group.exe_wrapped ~src_dir ~modules
else
Modules_group.exe_unwrapped modules
in
Expand Down
Loading

0 comments on commit ae14871

Please sign in to comment.