From 015b317f437bc634eaaff27c1a79bc680250d267 Mon Sep 17 00:00:00 2001 From: Rudi Grinberg Date: Mon, 9 Jul 2018 23:14:36 +0700 Subject: [PATCH] Introduce a bindings variable for actions We need to know the bindings statically whenever they overwrite existing vars Signed-off-by: Rudi Grinberg --- src/gen_rules.ml | 2 ++ src/inline_tests.ml | 1 + src/jbuild.ml | 2 ++ src/jbuild.mli | 2 ++ src/pform.ml | 13 +++++++++++++ src/pform.mli | 7 +++++++ src/preprocessing.ml | 2 ++ src/super_context.ml | 3 ++- src/super_context.mli | 1 + 9 files changed, 32 insertions(+), 1 deletion(-) diff --git a/src/gen_rules.ml b/src/gen_rules.ml index 7f88c6a2..467d4f54 100644 --- a/src/gen_rules.ml +++ b/src/gen_rules.ml @@ -194,6 +194,7 @@ module Gen(P : Install_rules.Params) = struct (snd rule.action) ~loc:(fst rule.action) ~dir + ~bindings:(Pform.Map.of_bindings rule.deps) ~dep_kind:Required ~targets ~scope) @@ -950,6 +951,7 @@ module Gen(P : Install_rules.Params) = struct ~loc ~dir ~dep_kind:Required + ~bindings:(Pform.Map.of_bindings alias_conf.deps) ~targets:Alias ~scope) diff --git a/src/inline_tests.ml b/src/inline_tests.ml index b62d638c..4e7522a1 100644 --- a/src/inline_tests.ml +++ b/src/inline_tests.ml @@ -220,6 +220,7 @@ include Sub_system.Register_end_point( (List.filter_map backends ~f:(fun (backend : Backend.t) -> Option.map backend.info.generate_runner ~f:(fun (loc, action) -> SC.Action.run sctx action ~loc + ~bindings:Pform.Map.empty ~extra_vars ~dir ~dep_kind:Required ~targets:Alias ~scope))) >>^ (fun actions -> Action.with_stdout_to target diff --git a/src/jbuild.ml b/src/jbuild.ml index f6b3a9e6..8a36823a 100644 --- a/src/jbuild.ml +++ b/src/jbuild.ml @@ -239,6 +239,8 @@ module Bindings = struct type 'a t = 'a one list + let fold t ~f ~init = List.fold_left ~f:(fun acc x -> f x acc) ~init t + let to_list = List.concat_map ~f:(function | Unnamed x -> [x] diff --git a/src/jbuild.mli b/src/jbuild.mli index c9871a5f..2b796925 100644 --- a/src/jbuild.mli +++ b/src/jbuild.mli @@ -90,6 +90,8 @@ module Bindings : sig val find : 'a t -> string -> 'a list option + val fold : 'a t -> f:('a one -> 'acc -> 'acc) -> init:'acc -> 'acc + val empty : 'a t val to_list : 'a t -> 'a list diff --git a/src/pform.ml b/src/pform.ml index 3b0be7bf..f2f96eec 100644 --- a/src/pform.ml +++ b/src/pform.ml @@ -7,6 +7,7 @@ module Var = struct | First_dep | Deps | Targets + | Named_local let to_value_no_deps_or_targets t ~scope = match t with @@ -14,6 +15,7 @@ module Var = struct | Project_root -> Some [Value.Dir (Scope.root scope)] | First_dep | Deps + | Named_local | Targets -> None end @@ -141,6 +143,8 @@ module Map = struct let static_vars = String.Map.of_list_exn static_vars + let superpose = String.Map.superpose + let rec expand t ~syntax_version ~var = let name = String_with_vars.Var.name var in Option.bind (String.Map.find t name) ~f:(fun v -> @@ -176,4 +180,13 @@ module Map = struct else Syntax.Error.deleted_in (String_with_vars.Var.loc var) Stanza.syntax syntax_version ~what:(what var) ?repl) + + let empty = String.Map.empty + + let of_bindings = + Jbuild.Bindings.fold ~f:(fun x acc -> + match x with + | Unnamed _ -> acc + | Named (s, _) -> String.Map.add acc s (No_info Var.Named_local) + ) ~init:empty end diff --git a/src/pform.mli b/src/pform.mli index 71ab99ae..9e1149a8 100644 --- a/src/pform.mli +++ b/src/pform.mli @@ -5,6 +5,7 @@ module Var : sig | First_dep | Deps | Targets + | Named_local val to_value_no_deps_or_targets : t -> scope:Scope.t -> Value.t list option end @@ -41,9 +42,15 @@ module Map : sig val static_vars : Var.t t + val superpose : 'a t -> 'a t -> 'a t + + val of_bindings : 'a Jbuild.Bindings.t -> Var.t t + val expand : 'a t -> syntax_version:Syntax.Version.t -> var:String_with_vars.Var.t -> 'a option + + val empty : 'a t end with type 'a var := 'a t diff --git a/src/preprocessing.ml b/src/preprocessing.ml index 6179e412..d7584716 100644 --- a/src/preprocessing.ml +++ b/src/preprocessing.ml @@ -462,6 +462,7 @@ let lint_module sctx ~dir ~dep_kind ~lint ~lib_name ~scope ~dir_kind = ~loc ~dir ~dep_kind + ~bindings:Pform.Map.empty ~targets:(Static []) ~scope))) | Pps { loc; pps; flags } -> @@ -542,6 +543,7 @@ let make sctx ~dir ~dep_kind ~lint ~preprocess ~loc ~dir ~dep_kind + ~bindings:Pform.Map.empty ~targets:(Static [dst]) ~scope)) |> setup_reason_rules sctx ~dir in diff --git a/src/super_context.ml b/src/super_context.ml index 7c4b629b..37da70f2 100644 --- a/src/super_context.ml +++ b/src/super_context.ml @@ -807,9 +807,10 @@ module Action = struct end end) - let run sctx ~loc ?(extra_vars=String.Map.empty) + let run sctx ~loc ?(extra_vars=String.Map.empty) ~bindings t ~dir ~dep_kind ~targets:targets_written_by_user ~scope : (Path.t Bindings.t, Action.t) Build.t = + ignore bindings; let map_exe = map_exe sctx in if targets_written_by_user = Alias then begin match Action.Infer.unexpanded_targets t with diff --git a/src/super_context.mli b/src/super_context.mli index 7e318771..f203db79 100644 --- a/src/super_context.mli +++ b/src/super_context.mli @@ -240,6 +240,7 @@ module Action : sig : t -> loc:Loc.t -> ?extra_vars:Value.t list String.Map.t + -> bindings:Pform.Var.t Pform.Map.t -> Action.Unexpanded.t -> dir:Path.t -> dep_kind:Build.lib_dep_kind