Skip to content

Commit

Permalink
Allow for proper expansoin of vars in super contexts
Browse files Browse the repository at this point in the history
Multivalues are no longer allowed when unquoted, and paths are no longer
needlessly converted.

Signed-off-by: Rudi Grinberg <rudi.grinberg@gmail.com>
  • Loading branch information
rgrinberg committed Jun 5, 2018
1 parent 7856f28 commit 1b45114
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 19 deletions.
3 changes: 1 addition & 2 deletions src/gen_rules.ml
Original file line number Diff line number Diff line change
Expand Up @@ -176,8 +176,7 @@ module Gen(P : Install_rules.Params) = struct
+-----------------------------------------------------------------+ *)

let interpret_locks ~dir ~scope locks =
List.map locks ~f:(fun s ->
Path.relative dir (SC.expand_vars sctx ~dir ~scope s))
List.map locks ~f:(SC.expand_vars_path sctx ~dir ~scope)

let user_rule (rule : Rule.t) ~dir ~scope =
let targets : SC.Action.targets =
Expand Down
32 changes: 15 additions & 17 deletions src/super_context.ml
Original file line number Diff line number Diff line change
Expand Up @@ -76,16 +76,18 @@ let find_scope_by_name t name = Scope.DB.find_by_name t.scopes name

let expand_var_no_root t var = String.Map.find t.vars var

let expand_vars t ~scope ~dir ?(extra_vars=String.Map.empty) s =
String_with_vars.expand s ~f:(fun _loc -> function
| "ROOT" -> Some (Path.reach ~from:dir t.context.build_dir)
| "SCOPE_ROOT" ->
Some (Path.reach ~from:dir (Scope.root scope))
| var ->
Option.map ~f:(fun e -> Var_expansion.to_string dir e)
let (expand_vars, expand_vars_path) =
let make expander t ~scope ~dir ?(extra_vars=String.Map.empty) s =
expander ~dir s ~f:(fun _loc -> function
| "ROOT" -> Some (Var_expansion.Paths [t.context.build_dir])
| "SCOPE_ROOT" -> Some (Paths [Scope.root scope])
| var ->
(match expand_var_no_root t var with
| Some _ as x -> x
| None -> String.Map.find extra_vars var))
| None -> String.Map.find extra_vars var)) in
( make Var_expansion.Single.string
, make Var_expansion.Single.path
)

let expand_and_eval_set t ~scope ~dir ?extra_vars set ~standard =
let open Build.O in
Expand Down Expand Up @@ -460,13 +462,11 @@ module Deps = struct

let make_alias t ~scope ~dir s =
let loc = String_with_vars.loc s in
Alias.of_user_written_path ~loc
(Path.relative ~error_loc:loc dir (expand_vars t ~scope ~dir s))
Alias.of_user_written_path ~loc ((expand_vars_path t ~scope ~dir s))

let dep t ~scope ~dir = function
| File s ->
let path = Path.relative ~error_loc:(String_with_vars.loc s) dir
(expand_vars t ~scope ~dir s) in
let path = expand_vars_path t ~scope ~dir s in
Build.path path
>>^ fun () -> [path]
| Alias s ->
Expand All @@ -478,19 +478,17 @@ module Deps = struct
>>^ fun () -> []
| Glob_files s -> begin
let loc = String_with_vars.loc s in
let path =
Path.relative ~error_loc:loc dir (expand_vars t ~scope ~dir s) in
let path = expand_vars_path t ~scope ~dir s in
match Glob_lexer.parse_string (Path.basename path) with
| Ok re ->
let dir = Path.parent_exn path in
Build.paths_glob ~loc ~dir (Re.compile re)
>>^ Path.Set.to_list
| Error (_pos, msg) ->
Loc.fail loc "invalid glob: %s" msg
Loc.fail (String_with_vars.loc s) "invalid glob: %s" msg
end
| Files_recursively_in s ->
let path = Path.relative ~error_loc:(String_with_vars.loc s)
dir (expand_vars t ~scope ~dir s) in
let path = expand_vars_path t ~scope ~dir s in
Build.files_recursively_in ~dir:path ~file_tree:t.file_tree
>>^ Path.Set.to_list
| Package p ->
Expand Down
8 changes: 8 additions & 0 deletions src/super_context.mli
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,14 @@ val expand_vars
-> String_with_vars.t
-> string

val expand_vars_path
: t
-> scope:Scope.t
-> dir:Path.t
-> ?extra_vars:Var_expansion.t String.Map.t
-> String_with_vars.t
-> Path.t

val expand_and_eval_set
: t
-> scope:Scope.t
Expand Down
17 changes: 17 additions & 0 deletions src/var_expansion.ml
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,20 @@ let to_path dir = function
| Paths [p] -> p
| Paths l ->
path_of_string dir (concat (List.map l ~f:(string_of_path ~dir)))

module Single = struct
let path ~dir sw ~f =
let relative = Path.relative ~error_loc:(String_with_vars.loc sw) in
match Expand.expand dir sw ~allow_multivalue:false ~f with
| String s
| Expansion (Strings [s]) -> relative dir s
| Expansion (Paths [s]) -> Path.append dir s
| _ -> assert false (* multivalues aren't allowed *)

let string ~dir sw ~f =
match Expand.expand dir sw ~allow_multivalue:false ~f with
| String s
| Expansion (Strings [s]) -> s
| Expansion (Paths [s]) -> string_of_path ~dir s
| _ -> assert false (* multivalues aren't allowed *)
end
15 changes: 15 additions & 0 deletions src/var_expansion.mli
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,18 @@ val to_path : Path.t -> t -> Path.t

module Expand : String_with_vars.Expand_intf
with type expansion = t and type context = Path.t

(** Specialized expansion that produce only a single value *)
module Single : sig
val path
: dir:Path.t
-> String_with_vars.t
-> f:(Loc.t -> string -> t option)
-> Path.t

val string
: dir:Path.t
-> String_with_vars.t
-> f:(Loc.t -> string -> t option)
-> string
end

0 comments on commit 1b45114

Please sign in to comment.