Move Var_expansion to own module
These variables can occur outside actions so such expansions shouldn't live under Var_expansion. Signed-off-by: Rudi Grinberg <rudi.grinberg@gmail.com>
This commit is contained in:
parent
cadee0e661
commit
588129d582
|
@ -270,53 +270,14 @@ module Unresolved = struct
|
||||||
| Search s -> Ok (f s))
|
| Search s -> Ok (f s))
|
||||||
end
|
end
|
||||||
|
|
||||||
module Var_expansion = struct
|
let var_expansion_to_prog_and_args dir exp : Unresolved.Program.t * string list =
|
||||||
type t =
|
let module P = Unresolved.Program in
|
||||||
| Paths of Path.t list
|
match (exp : Var_expansion.t) with
|
||||||
| Strings of string list
|
| Paths (x::xs) -> (This x, Var_expansion.to_strings dir (Paths xs))
|
||||||
|
| Strings (s::xs) -> ( P.of_string ~dir s
|
||||||
|
, Var_expansion.to_strings dir (Strings xs))
|
||||||
|
| Paths [] | Strings [] -> (Search "", [])
|
||||||
|
|
||||||
let length = function
|
|
||||||
| Paths x -> List.length x
|
|
||||||
| Strings x -> List.length x
|
|
||||||
|
|
||||||
let is_multivalued = function
|
|
||||||
| Paths [_] -> false
|
|
||||||
| Strings [_] -> false
|
|
||||||
| _ -> true
|
|
||||||
|
|
||||||
type context = Path.t (* For String_with_vars.Expand_to *)
|
|
||||||
|
|
||||||
let concat = function
|
|
||||||
| [s] -> s
|
|
||||||
| l -> String.concat ~sep:" " l
|
|
||||||
|
|
||||||
let string_of_path ~dir p = Path.reach ~from:dir p
|
|
||||||
let path_of_string dir s = Path.relative dir s
|
|
||||||
|
|
||||||
let to_strings dir = function
|
|
||||||
| Strings l -> l
|
|
||||||
| Paths l -> List.map l ~f:(string_of_path ~dir)
|
|
||||||
|
|
||||||
let to_string (dir: context) = function
|
|
||||||
| Strings l -> concat l
|
|
||||||
| Paths l -> concat (List.map l ~f:(string_of_path ~dir))
|
|
||||||
|
|
||||||
let to_path dir = function
|
|
||||||
| Strings l -> path_of_string dir (concat l)
|
|
||||||
| Paths [p] -> p
|
|
||||||
| Paths l ->
|
|
||||||
path_of_string dir (concat (List.map l ~f:(string_of_path ~dir)))
|
|
||||||
|
|
||||||
let to_prog_and_args dir exp : Unresolved.Program.t * string list =
|
|
||||||
let module P = Unresolved.Program in
|
|
||||||
match exp with
|
|
||||||
| Paths (x::xs) -> (This x, to_strings dir (Paths xs))
|
|
||||||
| Strings (s::xs) -> (P.of_string ~dir s, to_strings dir (Strings xs))
|
|
||||||
| Paths [] | Strings [] -> (Search "", [])
|
|
||||||
end
|
|
||||||
|
|
||||||
module VE = Var_expansion
|
|
||||||
module To_VE = String_with_vars.Expand_to(VE)
|
|
||||||
module SW = String_with_vars
|
module SW = String_with_vars
|
||||||
|
|
||||||
module Unexpanded = struct
|
module Unexpanded = struct
|
||||||
|
@ -357,37 +318,39 @@ module Unexpanded = struct
|
||||||
let expand ~generic ~special ~map ~dir ~allow_multivalue ~f = function
|
let expand ~generic ~special ~map ~dir ~allow_multivalue ~f = function
|
||||||
| Left x -> map x
|
| Left x -> map x
|
||||||
| Right template ->
|
| Right template ->
|
||||||
match To_VE.expand dir template ~f ~allow_multivalue with
|
match
|
||||||
| Expansion e -> special dir e
|
Var_expansion.Expand.expand dir template ~f ~allow_multivalue
|
||||||
| String s -> generic dir s
|
with
|
||||||
|
| Expansion e -> special dir e
|
||||||
|
| String s -> generic dir s
|
||||||
[@@inlined always]
|
[@@inlined always]
|
||||||
|
|
||||||
let string ~dir ~f x =
|
let string ~dir ~f x =
|
||||||
expand ~dir ~f x
|
expand ~dir ~f x
|
||||||
~allow_multivalue:false
|
~allow_multivalue:false
|
||||||
~generic:(fun _dir x -> x)
|
~generic:(fun _dir x -> x)
|
||||||
~special:VE.to_string
|
~special:Var_expansion.to_string
|
||||||
~map:(fun x -> x)
|
~map:(fun x -> x)
|
||||||
|
|
||||||
let strings ~dir ~f x =
|
let strings ~dir ~f x =
|
||||||
expand ~dir ~f x
|
expand ~dir ~f x
|
||||||
~allow_multivalue:true
|
~allow_multivalue:true
|
||||||
~generic:(fun _dir x -> [x])
|
~generic:(fun _dir x -> [x])
|
||||||
~special:VE.to_strings
|
~special:Var_expansion.to_strings
|
||||||
~map:(fun x -> [x])
|
~map:(fun x -> [x])
|
||||||
|
|
||||||
let path ~dir ~f x =
|
let path ~dir ~f x =
|
||||||
expand ~dir ~f x
|
expand ~dir ~f x
|
||||||
~allow_multivalue:false
|
~allow_multivalue:false
|
||||||
~generic:VE.path_of_string
|
~generic:Var_expansion.path_of_string
|
||||||
~special:VE.to_path
|
~special:Var_expansion.to_path
|
||||||
~map:(fun x -> x)
|
~map:(fun x -> x)
|
||||||
|
|
||||||
let prog_and_args ~dir ~f x =
|
let prog_and_args ~dir ~f x =
|
||||||
expand ~dir ~f x
|
expand ~dir ~f x
|
||||||
~allow_multivalue:true
|
~allow_multivalue:true
|
||||||
~generic:(fun _dir s -> (Program.of_string ~dir s, []))
|
~generic:(fun _dir s -> (Program.of_string ~dir s, []))
|
||||||
~special:VE.to_prog_and_args
|
~special:var_expansion_to_prog_and_args
|
||||||
~map:(fun x -> (x, []))
|
~map:(fun x -> (x, []))
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -452,7 +415,9 @@ module Unexpanded = struct
|
||||||
|
|
||||||
module E = struct
|
module E = struct
|
||||||
let expand ~generic ~special ~dir ~allow_multivalue ~f template =
|
let expand ~generic ~special ~dir ~allow_multivalue ~f template =
|
||||||
match To_VE.partial_expand dir template ~allow_multivalue ~f with
|
match
|
||||||
|
Var_expansion.Expand.partial_expand dir template ~allow_multivalue ~f
|
||||||
|
with
|
||||||
| Expansion e -> Left (special dir e)
|
| Expansion e -> Left (special dir e)
|
||||||
| String s -> Left (generic dir s)
|
| String s -> Left (generic dir s)
|
||||||
| Unexpanded x -> Right x
|
| Unexpanded x -> Right x
|
||||||
|
@ -461,25 +426,25 @@ module Unexpanded = struct
|
||||||
expand ~dir ~f x
|
expand ~dir ~f x
|
||||||
~allow_multivalue:false
|
~allow_multivalue:false
|
||||||
~generic:(fun _dir x -> x)
|
~generic:(fun _dir x -> x)
|
||||||
~special:VE.to_string
|
~special:Var_expansion.to_string
|
||||||
|
|
||||||
let strings ~dir ~f x =
|
let strings ~dir ~f x =
|
||||||
expand ~dir ~f x
|
expand ~dir ~f x
|
||||||
~allow_multivalue:true
|
~allow_multivalue:true
|
||||||
~generic:(fun _dir x -> [x])
|
~generic:(fun _dir x -> [x])
|
||||||
~special:VE.to_strings
|
~special:Var_expansion.to_strings
|
||||||
|
|
||||||
let path ~dir ~f x =
|
let path ~dir ~f x =
|
||||||
expand ~dir ~f x
|
expand ~dir ~f x
|
||||||
~allow_multivalue:false
|
~allow_multivalue:false
|
||||||
~generic:VE.path_of_string
|
~generic:Var_expansion.path_of_string
|
||||||
~special:VE.to_path
|
~special:Var_expansion.to_path
|
||||||
|
|
||||||
let prog_and_args ~dir ~f x =
|
let prog_and_args ~dir ~f x =
|
||||||
expand ~dir ~f x
|
expand ~dir ~f x
|
||||||
~allow_multivalue:true
|
~allow_multivalue:true
|
||||||
~generic:(fun dir s -> (Unresolved.Program.of_string ~dir s, []))
|
~generic:(fun dir s -> (Unresolved.Program.of_string ~dir s, []))
|
||||||
~special:VE.to_prog_and_args
|
~special:var_expansion_to_prog_and_args
|
||||||
end
|
end
|
||||||
|
|
||||||
let rec partial_expand t ~dir ~map_exe ~f : Partial.t =
|
let rec partial_expand t ~dir ~map_exe ~f : Partial.t =
|
||||||
|
|
|
@ -1,16 +1,5 @@
|
||||||
open! Import
|
open! Import
|
||||||
|
|
||||||
module Var_expansion : sig
|
|
||||||
type t =
|
|
||||||
| Paths of Path.t list
|
|
||||||
| Strings of string list
|
|
||||||
|
|
||||||
val to_string : Path.t -> t -> string
|
|
||||||
(** [to_string dir v] convert the variable expansion to a string.
|
|
||||||
If it is a path, the corresponding string will be relative to
|
|
||||||
[dir]. *)
|
|
||||||
end
|
|
||||||
|
|
||||||
module Outputs : module type of struct include Action_intf.Outputs end
|
module Outputs : module type of struct include Action_intf.Outputs end
|
||||||
|
|
||||||
(** result of the lookup of a program, the path to it or information about the
|
(** result of the lookup of a program, the path to it or information about the
|
||||||
|
|
|
@ -187,8 +187,7 @@ include Sub_system.Register_end_point(
|
||||||
in
|
in
|
||||||
|
|
||||||
let extra_vars =
|
let extra_vars =
|
||||||
String.Map.singleton "library-name"
|
String.Map.singleton "library-name" (Var_expansion.Strings [lib.name])
|
||||||
(Action.Var_expansion.Strings [lib.name])
|
|
||||||
in
|
in
|
||||||
|
|
||||||
let runner_libs =
|
let runner_libs =
|
||||||
|
@ -210,7 +209,7 @@ include Sub_system.Register_end_point(
|
||||||
let target = Path.relative inline_test_dir main_module_filename in
|
let target = Path.relative inline_test_dir main_module_filename in
|
||||||
let source_modules = Module.Name.Map.values source_modules in
|
let source_modules = Module.Name.Map.values source_modules in
|
||||||
let files ml_kind =
|
let files ml_kind =
|
||||||
Action.Var_expansion.Paths (
|
Var_expansion.Paths (
|
||||||
List.filter_map source_modules ~f:(fun m ->
|
List.filter_map source_modules ~f:(fun m ->
|
||||||
Module.file m ~dir ml_kind))
|
Module.file m ~dir ml_kind))
|
||||||
in
|
in
|
||||||
|
|
|
@ -45,7 +45,7 @@ type t =
|
||||||
; artifacts : Artifacts.t
|
; artifacts : Artifacts.t
|
||||||
; stanzas_to_consider_for_install : Installable.t list
|
; stanzas_to_consider_for_install : Installable.t list
|
||||||
; cxx_flags : string list
|
; cxx_flags : string list
|
||||||
; vars : Action.Var_expansion.t String.Map.t
|
; vars : Var_expansion.t String.Map.t
|
||||||
; chdir : (Action.t, Action.t) Build.t
|
; chdir : (Action.t, Action.t) Build.t
|
||||||
; host : t option
|
; host : t option
|
||||||
; libs_by_package : (Package.t * Lib.Set.t) Package.Name.Map.t
|
; libs_by_package : (Package.t * Lib.Set.t) Package.Name.Map.t
|
||||||
|
@ -92,7 +92,7 @@ let expand_vars t ~scope ~dir ?(extra_vars=String.Map.empty) s =
|
||||||
| "SCOPE_ROOT" ->
|
| "SCOPE_ROOT" ->
|
||||||
Some (Path.reach ~from:dir (Scope.root scope))
|
Some (Path.reach ~from:dir (Scope.root scope))
|
||||||
| var ->
|
| var ->
|
||||||
Option.map ~f:(fun e -> Action.Var_expansion.to_string dir e)
|
Option.map ~f:(fun e -> Var_expansion.to_string dir e)
|
||||||
(match expand_var_no_root t var with
|
(match expand_var_no_root t var with
|
||||||
| Some _ as x -> x
|
| Some _ as x -> x
|
||||||
| None -> String.Map.find extra_vars var))
|
| None -> String.Map.find extra_vars var))
|
||||||
|
@ -270,7 +270,7 @@ let create
|
||||||
| None -> Path.relative context.ocaml_bin "ocamlopt"
|
| None -> Path.relative context.ocaml_bin "ocamlopt"
|
||||||
| Some p -> p
|
| Some p -> p
|
||||||
in
|
in
|
||||||
let open Action.Var_expansion in
|
let open Var_expansion in
|
||||||
let make =
|
let make =
|
||||||
match Bin.make with
|
match Bin.make with
|
||||||
| None -> Strings ["make"]
|
| None -> Strings ["make"]
|
||||||
|
@ -588,7 +588,7 @@ module Action = struct
|
||||||
; (* Static deps from ${...} variables. For instance ${exe:...} *)
|
; (* Static deps from ${...} variables. For instance ${exe:...} *)
|
||||||
mutable sdeps : Path.Set.t
|
mutable sdeps : Path.Set.t
|
||||||
; (* Dynamic deps from ${...} variables. For instance ${read:...} *)
|
; (* Dynamic deps from ${...} variables. For instance ${read:...} *)
|
||||||
mutable ddeps : (unit, Action.Var_expansion.t) Build.t String.Map.t
|
mutable ddeps : (unit, Var_expansion.t) Build.t String.Map.t
|
||||||
}
|
}
|
||||||
|
|
||||||
let add_lib_dep acc lib kind =
|
let add_lib_dep acc lib kind =
|
||||||
|
@ -602,8 +602,8 @@ module Action = struct
|
||||||
acc.ddeps <- String.Map.add acc.ddeps key dep;
|
acc.ddeps <- String.Map.add acc.ddeps key dep;
|
||||||
None
|
None
|
||||||
|
|
||||||
let path_exp path = Action.Var_expansion.Paths [path]
|
let path_exp path = Var_expansion.Paths [path]
|
||||||
let str_exp path = Action.Var_expansion.Strings [path]
|
let str_exp path = Var_expansion.Strings [path]
|
||||||
|
|
||||||
let map_exe sctx =
|
let map_exe sctx =
|
||||||
match sctx.host with
|
match sctx.host with
|
||||||
|
@ -630,7 +630,7 @@ module Action = struct
|
||||||
; ddeps = String.Map.empty
|
; ddeps = String.Map.empty
|
||||||
}
|
}
|
||||||
in
|
in
|
||||||
let open Action.Var_expansion in
|
let open Var_expansion in
|
||||||
let expand loc key var = function
|
let expand loc key var = function
|
||||||
| Some ("exe" , s) -> Some (path_exp (map_exe (Path.relative dir s)))
|
| Some ("exe" , s) -> Some (path_exp (map_exe (Path.relative dir s)))
|
||||||
| Some ("path" , s) -> Some (path_exp (Path.relative dir s) )
|
| Some ("path" , s) -> Some (path_exp (Path.relative dir s) )
|
||||||
|
@ -751,7 +751,7 @@ module Action = struct
|
||||||
(t, acc)
|
(t, acc)
|
||||||
|
|
||||||
let expand_step2 ~dir ~dynamic_expansions ~deps_written_by_user ~map_exe t =
|
let expand_step2 ~dir ~dynamic_expansions ~deps_written_by_user ~map_exe t =
|
||||||
let open Action.Var_expansion in
|
let open Var_expansion in
|
||||||
U.Partial.expand t ~dir ~map_exe ~f:(fun loc key ->
|
U.Partial.expand t ~dir ~map_exe ~f:(fun loc key ->
|
||||||
match String.Map.find dynamic_expansions key with
|
match String.Map.find dynamic_expansions key with
|
||||||
| Some _ as opt -> opt
|
| Some _ as opt -> opt
|
||||||
|
@ -762,9 +762,9 @@ module Action = struct
|
||||||
Some
|
Some
|
||||||
(match deps_written_by_user with
|
(match deps_written_by_user with
|
||||||
| [] ->
|
| [] ->
|
||||||
Loc.warn loc "Variable '<' used with no explicit \
|
Loc.warn loc "Variable '<' used with no explicit \
|
||||||
dependencies@.";
|
dependencies@.";
|
||||||
Strings [""]
|
Strings [""]
|
||||||
| dep :: _ ->
|
| dep :: _ ->
|
||||||
Paths [dep])
|
Paths [dep])
|
||||||
| "^" -> Some (Paths deps_written_by_user)
|
| "^" -> Some (Paths deps_written_by_user)
|
||||||
|
|
|
@ -82,7 +82,7 @@ val expand_vars
|
||||||
: t
|
: t
|
||||||
-> scope:Scope.t
|
-> scope:Scope.t
|
||||||
-> dir:Path.t
|
-> dir:Path.t
|
||||||
-> ?extra_vars:Action.Var_expansion.t String.Map.t
|
-> ?extra_vars:Var_expansion.t String.Map.t
|
||||||
-> String_with_vars.t
|
-> String_with_vars.t
|
||||||
-> string
|
-> string
|
||||||
|
|
||||||
|
@ -90,7 +90,7 @@ val expand_and_eval_set
|
||||||
: t
|
: t
|
||||||
-> scope:Scope.t
|
-> scope:Scope.t
|
||||||
-> dir:Path.t
|
-> dir:Path.t
|
||||||
-> ?extra_vars:Action.Var_expansion.t String.Map.t
|
-> ?extra_vars:Var_expansion.t String.Map.t
|
||||||
-> Ordered_set_lang.Unexpanded.t
|
-> Ordered_set_lang.Unexpanded.t
|
||||||
-> standard:(unit, string list) Build.t
|
-> standard:(unit, string list) Build.t
|
||||||
-> (unit, string list) Build.t
|
-> (unit, string list) Build.t
|
||||||
|
@ -224,7 +224,7 @@ module Action : sig
|
||||||
val run
|
val run
|
||||||
: t
|
: t
|
||||||
-> loc:Loc.t
|
-> loc:Loc.t
|
||||||
-> ?extra_vars:Action.Var_expansion.t String.Map.t
|
-> ?extra_vars:Var_expansion.t String.Map.t
|
||||||
-> Action.Unexpanded.t
|
-> Action.Unexpanded.t
|
||||||
-> dir:Path.t
|
-> dir:Path.t
|
||||||
-> dep_kind:Build.lib_dep_kind
|
-> dep_kind:Build.lib_dep_kind
|
||||||
|
|
|
@ -0,0 +1,44 @@
|
||||||
|
open Stdune
|
||||||
|
|
||||||
|
module T = struct
|
||||||
|
type t =
|
||||||
|
| Paths of Path.t list
|
||||||
|
| Strings of string list
|
||||||
|
|
||||||
|
let length = function
|
||||||
|
| Paths x -> List.length x
|
||||||
|
| Strings x -> List.length x
|
||||||
|
|
||||||
|
let is_multivalued = function
|
||||||
|
| Paths [_] -> false
|
||||||
|
| Strings [_] -> false
|
||||||
|
| _ -> true
|
||||||
|
|
||||||
|
type context = Path.t (* For String_with_vars.Expand_to *)
|
||||||
|
|
||||||
|
let concat = function
|
||||||
|
| [s] -> s
|
||||||
|
| l -> String.concat ~sep:" " l
|
||||||
|
|
||||||
|
let string_of_path ~dir p = Path.reach ~from:dir p
|
||||||
|
|
||||||
|
let to_string (dir: context) = function
|
||||||
|
| Strings l -> concat l
|
||||||
|
| Paths l -> concat (List.map l ~f:(string_of_path ~dir))
|
||||||
|
end
|
||||||
|
|
||||||
|
include T
|
||||||
|
|
||||||
|
module Expand = String_with_vars.Expand_to(T)
|
||||||
|
|
||||||
|
let path_of_string dir s = Path.relative dir s
|
||||||
|
|
||||||
|
let to_strings dir = function
|
||||||
|
| Strings l -> l
|
||||||
|
| Paths l -> List.map l ~f:(string_of_path ~dir)
|
||||||
|
|
||||||
|
let to_path dir = function
|
||||||
|
| Strings l -> path_of_string dir (concat l)
|
||||||
|
| Paths [p] -> p
|
||||||
|
| Paths l ->
|
||||||
|
path_of_string dir (concat (List.map l ~f:(string_of_path ~dir)))
|
|
@ -0,0 +1,19 @@
|
||||||
|
open Stdune
|
||||||
|
|
||||||
|
type t =
|
||||||
|
| Paths of Path.t list
|
||||||
|
| Strings of string list
|
||||||
|
|
||||||
|
val to_string : Path.t -> t -> string
|
||||||
|
(** [to_string dir v] convert the variable expansion to a string.
|
||||||
|
If it is a path, the corresponding string will be relative to
|
||||||
|
[dir]. *)
|
||||||
|
|
||||||
|
val path_of_string : Path.t -> string -> Path.t
|
||||||
|
|
||||||
|
val to_strings : Path.t -> t -> string list
|
||||||
|
|
||||||
|
val to_path : Path.t -> t -> Path.t
|
||||||
|
|
||||||
|
module Expand : String_with_vars.Expand_intf
|
||||||
|
with type expansion = t and type context = Path.t
|
Loading…
Reference in New Issue