Skip to content

Commit

Permalink
refactor: remove [Lib_config] from [Lib] (#11105)
Browse files Browse the repository at this point in the history
Signed-off-by: Rudi Grinberg <[email protected]>
  • Loading branch information
rgrinberg authored Nov 7, 2024
1 parent 619c098 commit f7af2d0
Show file tree
Hide file tree
Showing 14 changed files with 125 additions and 62 deletions.
22 changes: 18 additions & 4 deletions bin/ocaml/top.ml
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,12 @@ let info = Cmd.info "top" ~doc ~man

let link_deps sctx link =
let open Memo.O in
let* lib_config =
let+ ocaml = Super_context.context sctx |> Context.ocaml in
ocaml.lib_config
in
Memo.parallel_map link ~f:(fun t ->
Dune_rules.Lib_flags.link_deps sctx t Dune_rules.Link_mode.Byte)
Dune_rules.Lib_flags.link_deps sctx t Dune_rules.Link_mode.Byte lib_config)
>>| List.concat
;;

Expand All @@ -47,9 +51,10 @@ let term =
let sctx =
Dune_engine.Context_name.Map.find setup.scontexts ctx_name |> Option.value_exn
in
let context = Super_context.context sctx in
let* libs =
let dir =
let build_dir = Super_context.context sctx |> Context.build_dir in
let build_dir = Context.build_dir context in
Path.Build.relative build_dir (Common.prefix_target common dir)
in
let* db =
Expand All @@ -62,7 +67,13 @@ let term =
let* requires =
Dune_rules.Resolve.Memo.read_memo (Dune_rules.Lib.closure ~linking:true libs)
in
let include_paths = Dune_rules.Lib_flags.L.toplevel_include_paths requires in
let* lib_config =
let+ ocaml = Context.ocaml context in
ocaml.lib_config
in
let include_paths =
Dune_rules.Lib_flags.L.toplevel_include_paths requires lib_config
in
let+ files_to_load = files_to_load_of_requires sctx requires in
Dune_rules.Toplevel.print_toplevel_init_file
{ include_paths; files_to_load; uses = []; pp = None; ppx = None; code = [] }))
Expand Down Expand Up @@ -116,7 +127,10 @@ module Module = struct
in
let private_obj_dir = Top_module.private_obj_dir ctx mod_ in
let include_paths =
let libs = Dune_rules.Lib_flags.L.toplevel_include_paths requires in
let libs =
let lib_config = (Compilation_context.ocaml cctx).lib_config in
Dune_rules.Lib_flags.L.toplevel_include_paths requires lib_config
in
Path.Set.add libs (Path.build (Obj_dir.byte_dir private_obj_dir))
in
let files_to_load () =
Expand Down
9 changes: 6 additions & 3 deletions bin/ocaml/utop.ml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ let term =
let dir = Common.prefix_target common dir in
if not (Path.is_directory (Path.of_string dir))
then User_error.raise [ Pp.textf "cannot find directory: %s" (String.maybe_quoted dir) ];
let env, utop_path, requires =
let env, lib_config, utop_path, requires =
Scheduler.go ~common ~config (fun () ->
let open Fiber.O in
let* setup = Import.Main.setup () in
Expand All @@ -46,12 +46,15 @@ let term =
Utop.requires_under_dir sctx ~dir
in
let+ requires = Resolve.read_memo requires
and+ lib_config =
let+ ocaml = Context.ocaml context in
ocaml.lib_config
and+ env = Super_context.context_env sctx in
env, Path.to_string utop_target, requires))
env, lib_config, Path.to_string utop_target, requires))
in
Hooks.End_of_build.run ();
let env =
Dune_rules.Lib_flags.L.toplevel_ld_paths requires
Dune_rules.Lib_flags.L.toplevel_ld_paths requires lib_config
|> Path.Set.fold
~f:(fun dir env -> Env_path.cons ~var:Ocaml.Env.caml_ld_library_path env ~dir)
~init:env
Expand Down
10 changes: 7 additions & 3 deletions src/dune_rules/compilation_context.ml
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@ open Memo.O
module Includes = struct
type t = Command.Args.without_targets Command.Args.t Lib_mode.Cm_kind.Map.t

let make ~project ~opaque ~direct_requires ~hidden_requires : _ Lib_mode.Cm_kind.Map.t =
let make ~project ~opaque ~direct_requires ~hidden_requires lib_config
: _ Lib_mode.Cm_kind.Map.t
=
(* TODO : some of the requires can filtered out using [ocamldep] info *)
let open Resolve.Memo.O in
let iflags direct_libs hidden_libs mode =
Lib_flags.L.include_flags ~project ~direct_libs ~hidden_libs mode
Lib_flags.L.include_flags ~project ~direct_libs ~hidden_libs mode lib_config
in
let make_includes_args ~mode groups =
Command.Args.memo
Expand Down Expand Up @@ -203,7 +205,8 @@ let create
; requires_compile = direct_requires
; requires_hidden = hidden_requires
; requires_link
; includes = Includes.make ~project ~opaque ~direct_requires ~hidden_requires
; includes =
Includes.make ~project ~opaque ~direct_requires ~hidden_requires ocaml.lib_config
; preprocessing
; opaque
; stdlib
Expand Down Expand Up @@ -293,6 +296,7 @@ let for_module_generated_at_link_time cctx ~requires ~module_ =
~opaque
~direct_requires
~hidden_requires
cctx.ocaml.lib_config
in
{ cctx with
opaque
Expand Down
3 changes: 2 additions & 1 deletion src/dune_rules/ctypes/ctypes_rules.ml
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,8 @@ let build_c_program
Lib.DB.resolve (Scope.libs scope) (Loc.none, ctypes) |> Resolve.Memo.read
in
let ctypes_include_dirs =
Lib_flags.L.include_paths [ lib ] (Ocaml Native) |> Path.Set.to_list
Lib_flags.L.include_paths [ lib ] (Ocaml Native) ocaml.lib_config
|> Path.Set.to_list
in
let include_dirs = ocaml_where :: ctypes_include_dirs in
Command.Args.S
Expand Down
10 changes: 7 additions & 3 deletions src/dune_rules/jsoo/jsoo_rules.ml
Original file line number Diff line number Diff line change
Expand Up @@ -521,18 +521,22 @@ let setup_separate_compilation_rules sctx components =
| Some pkg ->
let info = Lib.info pkg in
let lib_name = Lib_name.to_string (Lib.name pkg) in
let archives =
let* archives =
let archives = (Lib_info.archives info).byte in
(* Special case for the stdlib because it is not referenced in the
META *)
match lib_name with
| "stdlib" ->
let+ lib_config =
let+ ocaml = Context.ocaml ctx in
ocaml.lib_config
in
let archive =
let stdlib_dir = (Lib.lib_config pkg).stdlib_dir in
let stdlib_dir = lib_config.stdlib_dir in
Path.relative stdlib_dir
in
archive "stdlib.cma" :: archive "std_exit.cmo" :: archives
| _ -> archives
| _ -> Memo.return archives
in
Memo.parallel_iter Js_of_ocaml.Mode.all ~f:(fun mode ->
Memo.parallel_iter archives ~f:(fun fn ->
Expand Down
11 changes: 2 additions & 9 deletions src/dune_rules/lib.ml
Original file line number Diff line number Diff line change
Expand Up @@ -337,7 +337,6 @@ module T = struct
; pps : t list Resolve.t
; resolved_selects : Resolved_select.t list Resolve.t
; implements : t Resolve.t option
; lib_config : Lib_config.t
; project : Dune_project.t option
; (* these fields cannot be forced until the library is instantiated *)
default_implementation : t Resolve.t Memo.Lazy.t option
Expand Down Expand Up @@ -413,7 +412,6 @@ type db =
; instantiate :
(Lib_name.t -> Path.t Lib_info.t -> hidden:string option -> Status.t Memo.t) Lazy.t
; all : Lib_name.t list Memo.Lazy.t
; lib_config : Lib_config.t
; instrument_with : Lib_name.t list
}

Expand All @@ -427,7 +425,6 @@ and resolve_result =
| Redirect_by_name of db * (Loc.t * Lib_name.t)
| Redirect_by_id of db * Lib_id.t

let lib_config (t : lib) = t.lib_config
let name t = t.name
let info t = t.info
let project t = t.project
Expand Down Expand Up @@ -1070,7 +1067,6 @@ end = struct
; re_exports
; implements
; default_implementation
; lib_config = db.lib_config
; project
; sub_systems =
Sub_system_name.Map.mapi (Lib_info.sub_systems info) ~f:(fun name info ->
Expand Down Expand Up @@ -1909,14 +1905,13 @@ module DB = struct

type t = db

let create ~parent ~resolve ~resolve_lib_id ~all ~lib_config ~instrument_with () =
let create ~parent ~resolve ~resolve_lib_id ~all ~instrument_with () =
let rec t =
lazy
{ parent
; resolve
; resolve_lib_id
; all = Memo.lazy_ all
; lib_config
; instrument_with
; instantiate
}
Expand All @@ -1926,7 +1921,7 @@ module DB = struct

let create_from_findlib =
let bigarray = Lib_name.of_string "bigarray" in
fun findlib ~has_bigarray_library ~lib_config ->
fun findlib ~has_bigarray_library ->
let resolve name =
let open Memo.O in
Findlib.find findlib name
Expand All @@ -1951,7 +1946,6 @@ module DB = struct
create
()
~parent:None
~lib_config
~resolve
~resolve_lib_id:(fun lib_id ->
let open Memo.O in
Expand All @@ -1969,7 +1963,6 @@ module DB = struct
findlib
~has_bigarray_library:(Ocaml.Version.has_bigarray_library ocaml.version)
~instrument_with:(Context.instrument_with context)
~lib_config:ocaml.lib_config
;;

let find t name =
Expand Down
2 changes: 0 additions & 2 deletions src/dune_rules/lib.mli
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ val to_dyn : t -> Dyn.t
or the [name] if not. *)
val name : t -> Lib_name.t

val lib_config : t -> Lib_config.t
val implements : t -> t Resolve.Memo.t option

(** [is_local t] returns [true] whenever [t] is defined in the local workspace *)
Expand Down Expand Up @@ -116,7 +115,6 @@ module DB : sig
-> resolve:(Lib_name.t -> Resolve_result.t list Memo.t)
-> resolve_lib_id:(Lib_id.t -> Resolve_result.t Memo.t)
-> all:(unit -> Lib_name.t list Memo.t)
-> lib_config:Lib_config.t
-> instrument_with:Lib_name.t list
-> unit
-> t
Expand Down
56 changes: 35 additions & 21 deletions src/dune_rules/lib_flags.ml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ module Link_params = struct
not appear on the command line *)
}

let get sctx (t : Lib.t) (mode : Link_mode.t) =
let get sctx (t : Lib.t) (mode : Link_mode.t) (lib_config : Lib_config.t) =
let info = Lib.info t in
let lib_files = Lib_info.foreign_archives info
and dll_files = Lib_info.foreign_dll_files info in
Expand Down Expand Up @@ -69,15 +69,15 @@ module Link_params = struct
Path.extend_basename obj_name ~suffix:(Cm_kind.ext Cmo) :: hidden_deps
| Native ->
Path.extend_basename obj_name ~suffix:(Cm_kind.ext Cmx)
:: Path.extend_basename obj_name ~suffix:(Lib.lib_config t).ext_obj
:: Path.extend_basename obj_name ~suffix:lib_config.ext_obj
:: hidden_deps)
in
{ deps; hidden_deps; include_dirs }
;;
end

let link_deps sctx t mode =
let+ x = Link_params.get sctx t mode in
let link_deps sctx t mode lib_config =
let+ x = Link_params.get sctx t mode lib_config in
List.rev_append x.hidden_deps x.deps
;;

Expand All @@ -94,10 +94,8 @@ module L = struct
let to_iflags dir = to_flags "-I" dir
let to_hflags dir = to_flags "-H" dir

let remove_stdlib dirs libs =
match libs with
| [] -> dirs
| lib :: _ -> Path.Set.remove dirs (Lib.lib_config lib).stdlib_dir
let remove_stdlib dirs (lib_config : Lib_config.t) =
Path.Set.remove dirs lib_config.stdlib_dir
;;

type mode =
Expand Down Expand Up @@ -125,7 +123,7 @@ module L = struct
in
List.fold_left public_cmi_dirs ~init:acc ~f:Path.Set.add
in
fun ?project ts mode ->
fun ?project ts mode lib_config ->
let visible_cmi =
match project with
| None -> fun _ -> true
Expand Down Expand Up @@ -154,33 +152,35 @@ module L = struct
let native_dir = Obj_dir.native_dir obj_dir in
Path.Set.add acc native_dir))
in
remove_stdlib dirs ts
remove_stdlib dirs lib_config
;;

let include_flags ?project ~direct_libs ~hidden_libs mode =
let include_flags ?project ~direct_libs ~hidden_libs mode lib_config =
let include_paths ts =
include_paths ?project ts { lib_mode = mode; melange_emit = false }
in
let hidden_includes = to_hflags (include_paths hidden_libs) in
let direct_includes = to_iflags (include_paths direct_libs) in
let hidden_includes = to_hflags (include_paths hidden_libs lib_config) in
let direct_includes = to_iflags (include_paths direct_libs lib_config) in
Command.Args.S [ direct_includes; hidden_includes ]
;;

let melange_emission_include_flags ?project ts =
to_iflags (include_paths ?project ts { lib_mode = Melange; melange_emit = true })
let melange_emission_include_flags ?project ts lib_config =
to_iflags
(include_paths ?project ts { lib_mode = Melange; melange_emit = true } lib_config)
;;

let include_paths ?project ts mode =
include_paths ?project ts { lib_mode = mode; melange_emit = false }
;;

let c_include_paths ts =
let c_include_paths ts lib_config =
let dirs =
List.fold_left ts ~init:Path.Set.empty ~f:(fun acc t ->
let src_dir = Lib_info.src_dir (Lib.info t) in
Path.Set.add acc src_dir)
in
remove_stdlib dirs ts
(* I don't remember why this is being done anymore. Anyone else has a clue? *)
remove_stdlib dirs lib_config
;;

let c_include_flags ts sctx =
Expand All @@ -205,7 +205,17 @@ module L = struct
let+ () = Action_builder.all_unit bindings in
Command.Args.empty
in
Command.Args.S [ Dyn local; Hidden_deps external_; to_iflags (c_include_paths ts) ]
let include_flags =
let open Action_builder.O in
let+ lib_config =
Action_builder.of_memo
Memo.O.(
let+ ocaml = Super_context.context sctx |> Context.ocaml in
ocaml.lib_config)
in
to_iflags (c_include_paths ts lib_config)
in
Command.Args.S [ Dyn local; Hidden_deps external_; Dyn include_flags ]
;;

let toplevel_ld_paths ts =
Expand All @@ -218,8 +228,10 @@ module L = struct
c_include_paths with_dlls
;;

let toplevel_include_paths ts =
Path.Set.union (include_paths ts (Lib_mode.Ocaml Byte)) (toplevel_ld_paths ts)
let toplevel_include_paths ts lib_config =
Path.Set.union
(include_paths ts (Lib_mode.Ocaml Byte) lib_config)
(toplevel_ld_paths ts lib_config)
;;
end

Expand All @@ -238,7 +250,9 @@ module Lib_and_module = struct
Action_builder.all
(List.map ts ~f:(function
| Lib t ->
let+ p = Action_builder.of_memo (Link_params.get sctx t mode) in
let+ p =
Action_builder.of_memo (Link_params.get sctx t mode lib_config)
in
Command.Args.S
(Deps p.deps
:: Hidden_deps (Dep.Set.of_files p.hidden_deps)
Expand Down
Loading

0 comments on commit f7af2d0

Please sign in to comment.