Skip to content

Commit

Permalink
Merge pull request #4104 from rgrinberg/build-context-create-once
Browse files Browse the repository at this point in the history
Add build_context field to context
  • Loading branch information
rgrinberg authored Jan 13, 2021
2 parents 20cdd87 + 9b58d84 commit d31dc36
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 20 deletions.
20 changes: 12 additions & 8 deletions src/dune_rules/context.ml
Original file line number Diff line number Diff line change
Expand Up @@ -97,16 +97,14 @@ module T = struct
; supports_shared_libraries : Dynlink_supported.By_the_os.t
; which : string -> Path.t option
; lib_config : Lib_config.t
; build_context : Build_context.t
}

let equal x y = Context_name.equal x.name y.name

let hash t = Context_name.hash t.name

let rec to_build_context
{ name; build_dir; env; for_host; stdlib_dir; default_ocamlpath; _ } =
Build_context.create ~name ~build_dir ~env ~stdlib_dir ~default_ocamlpath
~host:(Option.map ~f:to_build_context for_host)
let build_context t = t.build_context

let to_dyn t : Dyn.t =
let open Dyn.Encoder in
Expand Down Expand Up @@ -419,7 +417,7 @@ let create ~(kind : Kind.t) ~path ~env ~env_nodes ~name ~merlin ~targets
| Error (Makefile_config file, msg) ->
User_error.raise ~loc:(Loc.in_file file) [ Pp.text msg ]
in
let* default_findlib_paths, (ocaml_config_vars, ocfg) =
let* default_ocamlpath, (ocaml_config_vars, ocfg) =
Fiber.fork_and_join default_findlib_paths (fun () ->
let+ lines =
Process.run_capture_lines ~env Strict ocamlc [ "-config" ]
Expand All @@ -432,7 +430,7 @@ let create ~(kind : Kind.t) ~path ~env ~env_nodes ~name ~merlin ~targets
(vars, ocfg)
| Error msg -> Error (Ocamlc_config, msg) ))
in
let findlib_paths = ocamlpath @ default_findlib_paths in
let findlib_paths = ocamlpath @ default_ocamlpath in
let version = Ocaml_version.of_ocaml_config ocfg in
let env =
(* See comment in ansi_color.ml for setup_env_for_colors. For versions
Expand Down Expand Up @@ -473,7 +471,7 @@ let create ~(kind : Kind.t) ~path ~env ~env_nodes ~name ~merlin ~targets
; ( "DUNE_OCAML_HARDCODED"
, String.concat
~sep:(Char.escaped ocamlpath_sep)
(List.map ~f:Path.to_string default_findlib_paths) )
(List.map ~f:Path.to_string default_ocamlpath) )
; extend_var "OCAMLTOP_INCLUDE_PATH"
(Path.Build.relative local_lib_root "toplevel")
; extend_var "OCAMLFIND_IGNORE_DUPS_IN" ~path_sep:ocamlpath_sep
Expand Down Expand Up @@ -545,6 +543,11 @@ let create ~(kind : Kind.t) ~path ~env ~env_nodes ~name ~merlin ~targets
supports_shared_libraries && dynamically_linked_foreign_archives
in
let t =
let build_context =
Build_context.create ~name ~build_dir ~env ~stdlib_dir
~default_ocamlpath
~host:(Option.map host ~f:(fun c -> c.build_context))
in
{ name
; implicit
; kind
Expand All @@ -570,7 +573,7 @@ let create ~(kind : Kind.t) ~path ~env ~env_nodes ~name ~merlin ~targets
; env
; findlib = Findlib.create ~paths:findlib_paths ~lib_config
; findlib_toolchain
; default_ocamlpath = default_findlib_paths
; default_ocamlpath
; arch_sixtyfour
; install_prefix
; stdlib_dir
Expand All @@ -581,6 +584,7 @@ let create ~(kind : Kind.t) ~path ~env ~env_nodes ~name ~merlin ~targets
Dynlink_supported.By_the_os.of_bool supports_shared_libraries
; which
; lib_config
; build_context
}
in
if Ocaml_version.supports_response_file version then (
Expand Down
3 changes: 2 additions & 1 deletion src/dune_rules/context.mli
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ type t = private
(** Given a program name, e.g. ["ocaml"], find the path to a preferred
executable in PATH, e.g. [Some "/path/to/ocaml.opt.exe"]. *)
; lib_config : Lib_config.t
; build_context : Build_context.t
}

val equal : t -> t -> bool
Expand Down Expand Up @@ -130,7 +131,7 @@ val lib_config : t -> Lib_config.t
the host build context. Otherwise, it just returns [exe]. *)
val map_exe : t -> Path.t -> Path.t

val to_build_context : t -> Build_context.t
val build_context : t -> Build_context.t

val init_configurator : t -> unit

Expand Down
2 changes: 1 addition & 1 deletion src/dune_rules/dep_conf_eval.ml
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ let dep expander = function
| Some pkg ->
Build.alias
(Build_system.Alias.package_install
~context:(Context.to_build_context context)
~context:(Context.build_context context)
~pkg)
| None ->
Build.fail
Expand Down
11 changes: 4 additions & 7 deletions src/dune_rules/install_rules.ml
Original file line number Diff line number Diff line change
Expand Up @@ -734,23 +734,20 @@ let install_rules sctx (package : Package.t) =
]
in
let () =
let context = Context.build_context ctx in
let target_alias =
Build_system.Alias.package_install
~context:(Context.to_build_context ctx)
~pkg:package
Build_system.Alias.package_install ~context ~pkg:package
in
Rules.Produce.Alias.add_deps target_alias files
~dyn_deps:
(let+ packages = packages in
Package.Id.Set.to_list packages
|> Path.Set.of_list_map ~f:(fun (pkg : Package.Id.t) ->
let name = Package.Id.name pkg in
let pkg =
let name = Package.Id.name pkg in
Package.Name.Map.find_exn (Super_context.packages sctx) name
in
Build_system.Alias.package_install
~context:(Context.to_build_context ctx)
~pkg
Build_system.Alias.package_install ~context ~pkg
|> Alias.stamp_file |> Path.build))
in
let action =
Expand Down
2 changes: 1 addition & 1 deletion src/dune_rules/main.ml
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ let init_build_system ?only_packages ~sandboxing_preference ?caching w =
Artifact_substitution.copy_file ?chmod ~src ~dst ~conf ()
in
Build_system.init ~sandboxing_preference ~promote_source
~contexts:(List.map ~f:Context.to_build_context w.contexts)
~contexts:(List.map ~f:Context.build_context w.contexts)
?caching ();
List.iter w.contexts ~f:Context.init_configurator;
let+ scontexts = Gen_rules.gen w.conf ~contexts:w.contexts ?only_packages in
Expand Down
4 changes: 2 additions & 2 deletions src/dune_rules/super_context.ml
Original file line number Diff line number Diff line change
Expand Up @@ -304,7 +304,7 @@ let make_rule t ?sandbox ?mode ?locks ?loc ~dir build =
let build = chdir_to_build_context_root t build in
let env = get_node t.env_tree ~dir |> Env_node.external_env in
Rule.make ?sandbox ?mode ?locks ~info:(Rule.Info.of_loc_opt loc)
~context:(Some (Context.to_build_context t.context))
~context:(Some (Context.build_context t.context))
~env:(Some env) build

let add_rule t ?sandbox ?mode ?locks ?loc ~dir build =
Expand All @@ -322,7 +322,7 @@ let add_rules t ?sandbox ~dir builds =
let add_alias_action t alias ~dir ~loc ?locks ~stamp action =
let env = Some (get_node t.env_tree ~dir |> Env_node.external_env) in
Rules.Produce.Alias.add_action
~context:(Context.to_build_context t.context)
~context:(Context.build_context t.context)
~env alias ~loc ?locks ~stamp action

let build_dir_is_vendored build_dir =
Expand Down

0 comments on commit d31dc36

Please sign in to comment.