Generalize expansion to be in own module

The result is Super_context.Expander which can be used for expandding OSL as well

Signed-off-by: Rudi Grinberg <rudi.grinberg@gmail.com>
This commit is contained in:
Rudi Grinberg 2018-07-11 20:34:48 +02:00
parent 6c6a5b7866
commit 72bbd06a1d
1 changed files with 206 additions and 137 deletions

View File

@ -560,15 +560,44 @@ module Scope_key = struct
sprintf "%s@%s" key (Dune_project.Name.encode scope)
end
module Action = struct
open Build.O
module U = Action.Unexpanded
type targets =
| Static of Path.t list
| Infer
| Alias
module Expander : sig
module Resolved_forms : sig
type t
(* Failed resolutions *)
val failures : t -> fail list
(* All "name" for %{lib:name:...}/%{lib-available:name} forms *)
val lib_deps : t -> Build.lib_deps
(* Static deps from %{...} variables. For instance %{exe:...} *)
val sdeps : t -> Path.Set.t
(* Dynamic deps from %{...} variables. For instance %{read:...} *)
val ddeps : t -> (unit, Value.t list) Build.t String.Map.t
end
type sctx = t
type expander
= sctx
-> dir:Path.t
-> dep_kind:Build.lib_dep_kind
-> scope:Scope.t
-> targets_written_by_user:targets
-> map_exe:(Path.t -> Path.t)
-> bindings:Pform.Map.t
-> String_with_vars.Var.t
-> Syntax.Version.t
-> Value.t list option
val with_expander : (expander -> 'a) -> 'a * Resolved_forms.t
end = struct
module Resolved_forms = struct
type t =
{ (* Failed resolutions *)
@ -581,6 +610,11 @@ module Action = struct
mutable ddeps : (unit, Value.t list) Build.t String.Map.t
}
let failures t = t.failures
let lib_deps t = t.lib_deps
let sdeps t = t.sdeps
let ddeps t = t.ddeps
let empty () =
{ failures = []
; lib_deps = String.Map.empty
@ -600,29 +634,33 @@ module Action = struct
None
end
type sctx = t
type expander
= sctx
-> dir:Path.t
-> dep_kind:Build.lib_dep_kind
-> scope:Scope.t
-> targets_written_by_user:targets
-> map_exe:(Path.t -> Path.t)
-> bindings:Pform.Map.t
-> String_with_vars.Var.t
-> Syntax.Version.t
-> Value.t list option
let path_exp path = [Value.Path path]
let str_exp str = [Value.String str]
let map_exe sctx =
match sctx.host with
| None -> (fun exe -> exe)
| Some host ->
fun exe ->
match Path.extract_build_context_dir exe with
| Some (dir, exe) when dir = sctx.context.build_dir ->
Path.append host.context.build_dir exe
| _ -> exe
let parse_lib_file ~loc s =
match String.lsplit2 s ~on:':' with
| None ->
Loc.fail loc "invalid %%{lib:...} form: %s" s
| Some x -> x
let expand_step1 sctx ~dir ~dep_kind ~scope ~targets_written_by_user
~map_exe ~bindings t =
let acc = Resolved_forms.empty () in
let expand pform syntax_version =
open Build.O
let expander ~acc sctx ~dir ~dep_kind ~scope ~targets_written_by_user
~map_exe ~bindings pform syntax_version =
let loc = String_with_vars.Var.loc pform in
let key = String_with_vars.Var.full_name pform in
let res =
@ -738,9 +776,40 @@ module Action = struct
(Path.Set.of_list (Value.L.deps_only v)) acc.sdeps
);
res
in
let t = U.partial_expand t ~dir ~map_exe ~f:expand in
(t, acc)
let with_expander
: 'a. (expander -> 'a) -> 'a * Resolved_forms.t
= fun f ->
let acc = Resolved_forms.empty () in
(f (expander ~acc), acc)
end
module Action = struct
open Build.O
module U = Action.Unexpanded
type nonrec targets = targets =
| Static of Path.t list
| Infer
| Alias
let map_exe sctx =
match sctx.host with
| None -> (fun exe -> exe)
| Some host ->
fun exe ->
match Path.extract_build_context_dir exe with
| Some (dir, exe) when dir = sctx.context.build_dir ->
Path.append host.context.build_dir exe
| _ -> exe
let expand_step1 sctx ~dir ~dep_kind ~scope ~targets_written_by_user
~map_exe ~bindings t =
Expander.with_expander (fun expander ->
let f = expander sctx ~dir ~dep_kind ~scope ~targets_written_by_user
~map_exe ~bindings in
U.partial_expand t ~dir ~map_exe ~f)
let expand_step2 ~dir ~dynamic_expansions ~bindings
~(deps_written_by_user : Path.t Jbuild.Bindings.t)
@ -826,13 +895,13 @@ module Action = struct
sprintf "- %s" (Utils.describe_target target))
|> String.concat ~sep:"\n"));
let build =
Build.record_lib_deps_simple forms.lib_deps
Build.record_lib_deps_simple (Expander.Resolved_forms.lib_deps forms)
>>>
Build.path_set (Path.Set.union deps forms.sdeps)
Build.path_set (Path.Set.union deps (Expander.Resolved_forms.sdeps forms))
>>>
Build.arr (fun paths -> ((), paths))
>>>
let ddeps = String.Map.to_list forms.ddeps in
let ddeps = String.Map.to_list (Expander.Resolved_forms.ddeps forms) in
Build.first (Build.all (List.map ddeps ~f:snd))
>>^ (fun (vals, deps_written_by_user) ->
let dynamic_expansions =
@ -857,7 +926,7 @@ module Action = struct
>>>
Build.action_dyn () ~dir ~targets
in
match forms.failures with
match Expander.Resolved_forms.failures forms with
| [] -> build
| fail :: _ -> Build.fail fail >>> build
end