Skip to content

Commit 171a84e

Browse files
committed
Modified the named target
Signed-off-by: Anthony Onah <[email protected]>
1 parent 569b03d commit 171a84e

File tree

10 files changed

+244
-142
lines changed

10 files changed

+244
-142
lines changed

src/dune_lang/action.ml

+77-56
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ open Stdune
22
open Dune_sexp
33
open Dune_util.Action
44
open Pform.Macro
5+
open Pform
56

67
module Named_targets = struct
78
type t = (string * String_with_vars.t) list
@@ -25,7 +26,7 @@ module Pform = struct
2526
| C_flags
2627
| Cxx_flags
2728
| Cpp_flags
28-
| Target of string (* New variant for named targets *)
29+
| Target of string (* New variant for named targets *)
2930
| Ocaml
3031
| Ocamlc
3132
| Ocamlopt
@@ -62,8 +63,8 @@ module Pform = struct
6263

6364
let compare = compare
6465
end
65-
type t =
66-
| Var of Var.t
66+
67+
type t = Var of Var.t
6768
end
6869

6970
module Diff = struct
@@ -162,7 +163,7 @@ module Env_update = struct
162163

163164
type 'a t =
164165
{ op : Op.t
165-
; var : Env.Var.t
166+
; var : Var.t
166167
; value : 'a
167168
}
168169

@@ -174,28 +175,36 @@ module Env_update = struct
174175
{ op = other_op; var = other_var; value = other_value }
175176
=
176177
Op.equal op other_op
177-
&& Ordering.is_eq (Env.Var.compare var other_var)
178+
&& Var.compare var other_var = Ordering.Eq
178179
&& value_equal value other_value
179180
;;
180181

181182
let to_dyn value_to_dyn { op; var; value } =
182-
Dyn.record
183-
[ "op", Op.to_dyn op; "var", Env.Var.to_dyn var; "value", value_to_dyn value ]
183+
Dyn.record [ "op", Op.to_dyn op; "var", Var.to_dyn var; "value", value_to_dyn value ]
184184
;;
185185

186186
let decode =
187187
let open Decoder in
188188
let env_update_op = enum Op.all in
189189
let+ op, var, value = triple env_update_op string String_with_vars.decode in
190-
{ op; var; value }
190+
{ op; var = Var.User_var var; value }
191191
;;
192192

193193
let encode { op; var; value } =
194-
let op =
194+
let op_str =
195195
List.find_map Op.all ~f:(fun (k, v) -> if Poly.equal v op then Some k else None)
196196
|> Option.value_exn
197197
in
198-
List [ atom op; atom var; String_with_vars.encode value ]
198+
let var_str =
199+
match var with
200+
| Var.User_var s -> s
201+
| Var.Target -> "target"
202+
| _ ->
203+
(* Handle other cases or raise an error *)
204+
Code_error.raise "Action.encode: unsupported variable" []
205+
in
206+
Dune_sexp.List
207+
[ Dune_sexp.atom op_str; Dune_sexp.atom var_str; String_with_vars.encode value ]
199208
;;
200209
end
201210

@@ -232,63 +241,73 @@ type t =
232241
| When of Slang.blang * t
233242
| Format_dune_file of String_with_vars.t * String_with_vars.t
234243

235-
type expansion_context = {
236-
dir : Path.t;
237-
(* ... other fields ... *)
238-
named_targets : Named_targets.t;
239-
}
244+
type expansion_context =
245+
{ dir : Path.t
246+
; named_targets : (string * String_with_vars.t) list
247+
; named_deps : (string * Dep_conf.t) list
248+
}
249+
240250
let is_dev_null t = String_with_vars.is_pform t (Var Dev_null)
241251

242252
let translate_to_ignore fn output action =
243253
if is_dev_null fn
244254
then Ignore (output, action)
245255
else Redirect_out (output, fn, Normal, action)
246256
;;
247-
let expand_target_var ~loc ~named_targets name =
248-
match List.assoc_opt name named_targets with
249-
| Some target ->
250-
(match String_with_vars.text_only target with
251-
| Some text -> text
252-
| None ->
253-
User_error.raise ~loc [
254-
Pp.textf "Named target '%s' contains variables and cannot be expanded here" name
255-
])
256-
| None ->
257-
User_error.raise ~loc [
258-
Pp.textf "Undefined named target: %s" name;
259-
Pp.text "Available named targets:";
260-
Pp.enumerate named_targets ~f:(fun (name, _) -> Pp.text name)
261-
]
262-
;;
257+
263258
let expand_str ~context sw =
264259
let module S = String_with_vars in
265-
266260
match S.text_only sw with
267261
| Some s -> s
268262
| None ->
269263
let expand_var = function
270-
| Pform.Var var ->
271-
if String.equal (Pform.Var.to_string var) "target" then
272-
(match context.named_targets with
273-
| (name, target) :: _ -> S.to_string target
274-
| [] -> "%{target}")
275-
else
276-
S.to_string (S.make_var (S.loc sw) (Pform.Var var))
264+
| Pform.Var (Var.User_var name) ->
265+
(* Fixed pattern *)
266+
(match List.assoc_opt name context.named_targets with
267+
| Some target -> S.to_string target
268+
| None ->
269+
(match List.assoc_opt name context.named_deps with
270+
| Some dep -> Dep_conf.to_string dep
271+
| None ->
272+
User_error.raise
273+
~loc:(S.loc sw)
274+
[ Pp.textf "Undefined variable %%{%s}" name
275+
; Pp.text "Available variables:"
276+
; Pp.enumerate
277+
(List.map
278+
(context.named_targets @ context.named_deps)
279+
~f:(fun (n, _) -> n))
280+
]))
281+
| Pform.Var Var.Target ->
282+
(* Correct pattern *)
283+
(match List.assoc_opt "target" context.named_targets with
284+
| Some target -> S.to_string target
285+
| None ->
286+
User_error.raise ~loc:(S.loc sw) [ Pp.text "Undefined target %{target}" ])
287+
| Pform.Var other_var ->
288+
(* Handle other built-in variables *)
289+
S.to_string (S.make_var (S.loc sw) (Pform.Var other_var))
290+
| Pform.Macro macro ->
291+
(* Handle macros *)
292+
S.to_string (S.make_var (S.loc sw) (Pform.Macro macro))
277293
in
278294
S.expand sw ~f:expand_var
279-
;;
295+
;;
280296

281297
let create_action targets action =
282-
let named_targets =
298+
let named_targets =
283299
List.filter_map targets ~f:(fun (target, _, name) ->
284-
Option.map name ~f:(fun n -> (n, target)))
300+
Option.map name ~f:(fun n -> n, target))
285301
in
286-
{ action with
287-
expansion_context = {
288-
dir = Path.root;
289-
named_targets
290-
}
302+
{ action with
303+
expansion_context =
304+
{ dir = Path.root
305+
; named_targets
306+
; named_deps = [] (* Will be populated from rule.deps later *)
307+
}
291308
}
309+
;;
310+
292311
let two_or_more decode =
293312
let open Decoder in
294313
let+ n1 = decode
@@ -360,15 +379,17 @@ let decode_with_accepted_exit_codes =
360379

361380
let decode_rule =
362381
let open Decoder in
363-
let* targets =
364-
repeat (
365-
let* target = String_with_vars.decode in
366-
let* name = optional string in
367-
return (target, (), name)
368-
)
369-
in
370-
let* action = decode_dune_file in
371-
return (create_action targets action)
382+
let+ targets =
383+
repeat
384+
(let* target = String_with_vars.decode in
385+
let* name = optional string in
386+
return (target, (), name))
387+
and+ deps = field "deps" (Bindings.decode Dep_conf.decode) ~default:Bindings.empty
388+
and+ action = decode_dune_file in
389+
let rule = create_action targets action in
390+
{ rule with
391+
expansion_context = { rule.expansion_context with named_deps = Bindings.to_list deps }
392+
}
372393
;;
373394

374395
let sw = String_with_vars.decode

src/dune_lang/bindings.ml

+24
Original file line numberDiff line numberDiff line change
@@ -90,3 +90,27 @@ let to_pform_map t =
9090
| Unnamed _ -> None
9191
| Named (name, l) -> Some (Pform.Var (User_var name), l)))
9292
;;
93+
94+
let is_empty b = b = []
95+
96+
let get_target_by_name name bindings =
97+
let rec find_target = function
98+
| [] -> None
99+
| Unnamed target :: rest ->
100+
(match String_with_vars.text_only target with
101+
| Some s when s = name -> Some target
102+
| _ -> find_target rest)
103+
| Named (_, targets) :: rest ->
104+
(match
105+
List.find
106+
~f:(fun t ->
107+
match String_with_vars.text_only t with
108+
| Some s -> s = name
109+
| None -> false)
110+
targets
111+
with
112+
| Some target -> Some target
113+
| None -> find_target rest)
114+
in
115+
find_target bindings
116+
;;

src/dune_lang/bindings.mli

+2
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,5 @@ val decode : 'a Dune_sexp.Decoder.t -> 'a t Dune_sexp.Decoder.t
1818
val encode : 'a Dune_sexp.Encoder.t -> 'a t -> Dune_sexp.t
1919
val var_names : _ t -> string list
2020
val to_pform_map : 'a t -> 'a list Pform.Map.t
21+
val is_empty : 'a t -> bool
22+
val get_target_by_name : string -> String_with_vars.t t -> String_with_vars.t option

0 commit comments

Comments
 (0)