Skip to content

Commit 42ac569

Browse files
committed
Working on including named targets in parser
1 parent 104efb6 commit 42ac569

18 files changed

+440
-629
lines changed

src/dune_lang/action.ml

Lines changed: 8 additions & 145 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,6 @@
11
open Stdune
22
open Dune_sexp
33
open Dune_util.Action
4-
open Pform.Macro
5-
open Pform
6-
7-
module Named_targets = struct
8-
type t = (string * String_with_vars.t) list
9-
end
104

115
module Action_plugin = struct
126
let syntax =
@@ -18,55 +12,6 @@ module Action_plugin = struct
1812
;;
1913
end
2014

21-
module Pform = struct
22-
module Var = struct
23-
type t =
24-
| Values
25-
| Loc
26-
| C_flags
27-
| Cxx_flags
28-
| Cpp_flags
29-
| Target of string (* New variant for named targets *)
30-
| Ocaml
31-
| Ocamlc
32-
| Ocamlopt
33-
| Ocamldep
34-
| Ocamlmklib
35-
| Dev_null
36-
| Null
37-
| Ext_obj
38-
| Ext_asm
39-
| Ext_lib
40-
| Ext_dll
41-
| Ext_exe
42-
| Ext_plugin
43-
| Profile
44-
| Context_name
45-
| Os_type
46-
| Architecture
47-
| System
48-
| Model
49-
| Ignoring_promoted_rules
50-
| Project_root
51-
| Workspace_root
52-
| Build_context
53-
| First_dep
54-
| Input_file
55-
| Library_name
56-
| Partition
57-
| Impl_files
58-
| Intf_files
59-
| Test
60-
| Corrected_suffix
61-
| Inline_tests
62-
| Toolchain
63-
64-
let compare = compare
65-
end
66-
67-
type t = Var of Var.t
68-
end
69-
7015
module Diff = struct
7116
include Diff
7217

@@ -163,7 +108,7 @@ module Env_update = struct
163108

164109
type 'a t =
165110
{ op : Op.t
166-
; var : Var.t
111+
; var : Env.Var.t
167112
; value : 'a
168113
}
169114

@@ -175,36 +120,28 @@ module Env_update = struct
175120
{ op = other_op; var = other_var; value = other_value }
176121
=
177122
Op.equal op other_op
178-
&& Var.compare var other_var = Ordering.Eq
123+
&& Ordering.is_eq (Env.Var.compare var other_var)
179124
&& value_equal value other_value
180125
;;
181126

182127
let to_dyn value_to_dyn { op; var; value } =
183-
Dyn.record [ "op", Op.to_dyn op; "var", Var.to_dyn var; "value", value_to_dyn value ]
128+
Dyn.record
129+
[ "op", Op.to_dyn op; "var", Env.Var.to_dyn var; "value", value_to_dyn value ]
184130
;;
185131

186132
let decode =
187133
let open Decoder in
188134
let env_update_op = enum Op.all in
189135
let+ op, var, value = triple env_update_op string String_with_vars.decode in
190-
{ op; var = Var.User_var var; value }
136+
{ op; var; value }
191137
;;
192138

193139
let encode { op; var; value } =
194-
let op_str =
140+
let op =
195141
List.find_map Op.all ~f:(fun (k, v) -> if Poly.equal v op then Some k else None)
196142
|> Option.value_exn
197143
in
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 ]
144+
List [ atom op; atom var; String_with_vars.encode value ]
208145
;;
209146
end
210147

@@ -241,12 +178,6 @@ type t =
241178
| When of Slang.blang * t
242179
| Format_dune_file of String_with_vars.t * String_with_vars.t
243180

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-
250181
let is_dev_null t = String_with_vars.is_pform t (Var Dev_null)
251182

252183
let translate_to_ignore fn output action =
@@ -255,59 +186,6 @@ let translate_to_ignore fn output action =
255186
else Redirect_out (output, fn, Normal, action)
256187
;;
257188

258-
let expand_str ~context sw =
259-
let module S = String_with_vars in
260-
match S.text_only sw with
261-
| Some s -> s
262-
| None ->
263-
let expand_var = function
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))
293-
in
294-
S.expand sw ~f:expand_var
295-
;;
296-
297-
let create_action targets action =
298-
let named_targets =
299-
List.filter_map targets ~f:(fun (target, _, name) ->
300-
Option.map name ~f:(fun n -> n, target))
301-
in
302-
{ action with
303-
expansion_context =
304-
{ dir = Path.root
305-
; named_targets
306-
; named_deps = [] (* Will be populated from rule.deps later *)
307-
}
308-
}
309-
;;
310-
311189
let two_or_more decode =
312190
let open Decoder in
313191
let+ n1 = decode
@@ -377,21 +255,6 @@ let decode_with_accepted_exit_codes =
377255
]
378256
;;
379257

380-
let decode_rule =
381-
let open Decoder in
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-
}
393-
;;
394-
395258
let sw = String_with_vars.decode
396259

397260
let cstrs_dune_file t =
@@ -768,4 +631,4 @@ let decode_pkg = make_decode decode_pkg
768631
let to_dyn a = to_dyn (encode a)
769632
let equal x y = Poly.equal x y
770633
let chdir dir t = Chdir (dir, t)
771-
let run prog args = Run (Slang.Literal prog :: List.map args ~f:(fun x -> Slang.Literal x))
634+
let run prog args = Run (Slang.Literal prog :: List.map args ~f:(fun x -> Slang.Literal x))

0 commit comments

Comments
 (0)