Skip to content

Commit 20f7765

Browse files
committed
Switch to (dir target), adjust tests
Signed-off-by: Andrey Mokhov <[email protected]>
1 parent 044fea4 commit 20f7765

File tree

10 files changed

+134
-139
lines changed

10 files changed

+134
-139
lines changed

bin/print_rules.ml

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -54,22 +54,25 @@ let print_rule_sexp ppf (rule : Dune_engine.Reflection.Rule.t) =
5454
Action.for_shell action |> Action.For_shell.encode
5555
in
5656
let paths ps = Dune_lang.Encoder.list Dpath.encode (Path.Set.to_list ps) in
57-
let file_targets, dir_targets =
58-
Dune_engine.Targets.map rule.targets ~f:(fun ~files ~dirs -> (files, dirs))
59-
in
60-
let targets =
61-
Path.Build.Set.union file_targets
62-
(Path.Build.Set.map dir_targets ~f:(fun target ->
63-
Path.Build.relative target "*"))
57+
let file_targets =
58+
Dune_engine.Targets.map rule.targets ~f:(fun ~files ~dirs ->
59+
if not (Path.Build.Set.is_empty dirs) then
60+
User_error.raise
61+
[ Pp.text
62+
"Printing rules with directory targets is currently not \
63+
supported"
64+
];
65+
66+
files)
6467
in
6568
let sexp =
6669
Dune_lang.Encoder.record
6770
(List.concat
6871
[ [ ("deps", Dep.Set.encode rule.deps)
6972
; ( "targets"
7073
, paths
71-
(Path.Build.Set.to_list targets |> Path.set_of_build_paths_list)
72-
)
74+
(Path.Build.Set.to_list file_targets
75+
|> Path.set_of_build_paths_list) )
7376
]
7477
; (match rule.context with
7578
| None -> []

src/dune_engine/string_with_vars.ml

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -312,14 +312,6 @@ let text_only t =
312312
| [ Text s ] -> Some s
313313
| _ -> None
314314

315-
let last_text_part t =
316-
List.filter_map t.parts ~f:(function
317-
| Text s -> Some s
318-
| Error _
319-
| Pform _ ->
320-
None)
321-
|> List.last
322-
323315
let has_pforms t = Option.is_none (text_only t)
324316

325317
let encode t =

src/dune_engine/string_with_vars.mli

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,9 +54,6 @@ val has_pforms : t -> bool
5454
(** If [t] contains no variable, returns the contents of [t]. *)
5555
val text_only : t -> string option
5656

57-
(** The last text part of [t], if any. *)
58-
val last_text_part : t -> string option
59-
6057
module Mode : sig
6158
(** How many values expansion of a template must produce.
6259

src/dune_rules/action_unexpanded.ml

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -516,11 +516,8 @@ let expand t ~loc ~deps:deps_written_by_user ~targets_dir
516516
let deps_builder, expander =
517517
Dep_conf_eval.named ~expander deps_written_by_user
518518
in
519-
let untagged_targets_written_by_user =
520-
Targets_spec.untag targets_written_by_user
521-
in
522519
let expander =
523-
match (untagged_targets_written_by_user : _ Targets_spec.t) with
520+
match (targets_written_by_user : _ Targets_spec.t) with
524521
| Infer -> expander
525522
| Static { targets; multiplicity } ->
526523
Expander.add_bindings_full expander
@@ -532,11 +529,13 @@ let expand t ~loc ~deps:deps_written_by_user ~targets_dir
532529
| Multiple -> Targets))
533530
(Expander.Deps.Without
534531
(Memo.Build.return
535-
(Value.L.paths (List.map targets ~f:Path.build)))))
532+
(Value.L.paths
533+
(List.map targets
534+
~f:(fun (target, (_ : Targets_spec.Kind.t)) ->
535+
Path.build target))))))
536536
in
537537
let expander =
538-
Expander.set_expanding_what expander
539-
(User_action untagged_targets_written_by_user)
538+
Expander.set_expanding_what expander (User_action targets_written_by_user)
540539
in
541540
let+! { Action_builder.With_targets.build; targets } =
542541
Action_expander.run (expand t) ~expander
@@ -546,17 +545,17 @@ let expand t ~loc ~deps:deps_written_by_user ~targets_dir
546545
| Infer -> targets
547546
| Static { targets = targets_written_by_user; multiplicity = _ } ->
548547
let files, dirs =
549-
List.partition_map targets_written_by_user ~f:(fun (path, tag) ->
548+
List.partition_map targets_written_by_user ~f:(fun (path, kind) ->
550549
if Path.Build.(parent_exn path <> targets_dir) then
551550
User_error.raise ~loc
552551
[ Pp.text
553552
"This action has targets in a different directory than the \
554553
current one, this is not allowed by dune at the moment:"
555554
; Targets.pp targets
556555
];
557-
match tag with
558-
| None -> Left path
559-
| Star -> Right path)
556+
match kind with
557+
| File -> Left path
558+
| Directory -> Right path)
560559
in
561560
let files = Path.Build.Set.of_list files in
562561
let dirs = Path.Build.Set.of_list dirs in

src/dune_rules/action_unexpanded.mli

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ val expand :
3131
-> loc:Loc.t
3232
-> deps:Dep_conf.t Bindings.t
3333
-> targets_dir:Path.Build.t
34-
-> targets:(Path.Build.t * Targets_spec.Tag.t) Targets_spec.t
34+
-> targets:Path.Build.t Targets_spec.t
3535
-> expander:Expander.t
3636
-> Action.t Action_builder.With_targets.t Memo.Build.t
3737

src/dune_rules/dune_file.ml

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1639,14 +1639,14 @@ module Rule = struct
16391639
field "deps" (Bindings.decode Dep_conf.decode) ~default:Bindings.empty
16401640
in
16411641
let* project = Dune_project.get_exn () in
1642-
let disallow_directory_targets =
1643-
Option.is_none
1642+
let allow_directory_targets =
1643+
Option.is_some
16441644
(Dune_project.find_extension_args project directory_targets_extension)
16451645
in
16461646
String_with_vars.add_user_vars_to_decoding_env (Bindings.var_names deps)
16471647
(let+ loc = loc
16481648
and+ action = field "action" (located Action_dune_lang.decode)
1649-
and+ targets = Targets_spec.field
1649+
and+ targets = Targets_spec.field ~allow_directory_targets
16501650
and+ locks = field "locks" (repeat String_with_vars.decode) ~default:[]
16511651
and+ () =
16521652
let+ fallback =
@@ -1672,13 +1672,6 @@ module Rule = struct
16721672
field_o "alias"
16731673
(Dune_lang.Syntax.since Stanza.syntax (2, 0) >>> Alias.Name.decode)
16741674
in
1675-
if
1676-
disallow_directory_targets && Targets_spec.has_target_directory targets
1677-
then
1678-
User_error.raise ~loc
1679-
[ Pp.text
1680-
"Directory targets require the 'directory-targets' extension"
1681-
];
16821675
{ targets; deps; action; mode; locks; loc; enabled_if; alias; package })
16831676

16841677
let decode =
@@ -1726,7 +1719,9 @@ module Rule = struct
17261719
can't because this is might get parsed with old dune syntax where
17271720
[multiplicity = One] is not supported. *)
17281721
Static
1729-
{ targets = [ S.make_text loc dst ]; multiplicity = Multiple }
1722+
{ targets = [ (S.make_text loc dst, File) ]
1723+
; multiplicity = Multiple
1724+
}
17301725
; deps = Bindings.singleton (Dep_conf.File (S.virt_text __POS__ src))
17311726
; action =
17321727
( loc
@@ -1754,7 +1749,8 @@ module Rule = struct
17541749
{ targets =
17551750
Static
17561751
{ targets =
1757-
List.map ~f:(S.make_text loc) [ name ^ ".ml"; name ^ ".mli" ]
1752+
List.map [ name ^ ".ml"; name ^ ".mli" ] ~f:(fun target ->
1753+
(S.make_text loc target, Targets_spec.Kind.File))
17581754
; multiplicity = Multiple
17591755
}
17601756
; deps = Bindings.singleton (Dep_conf.File (S.virt_text __POS__ src))

src/dune_rules/simple_rules.ml

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -28,17 +28,12 @@ let check_filename =
2828
User_error.raise ~loc:error_loc
2929
[ Pp.text "'.' and '..' are not valid filenames" ]
3030
| String s ->
31-
let s, tag =
32-
match String.drop_suffix s ~suffix:"/*" with
33-
| None -> (s, Targets_spec.Tag.None)
34-
| Some s -> (s, Star)
35-
in
3631
if Filename.dirname s <> Filename.current_dir_name then
3732
not_in_dir ~error_loc s;
38-
(Path.Build.relative ~error_loc dir s, tag)
33+
Path.Build.relative ~error_loc dir s
3934
| Path p -> (
4035
match Option.equal Path.equal (Path.parent p) (Some (Path.build dir)) with
41-
| true -> (Path.as_in_build_dir_exn p, Targets_spec.Tag.None)
36+
| true -> Path.as_in_build_dir_exn p
4237
| false -> not_in_dir ~error_loc (Path.to_string p))
4338
| Dir p -> not_in_dir ~error_loc (Path.to_string p)
4439

@@ -81,14 +76,15 @@ let user_rule sctx ?extra_bindings ~dir ~expander (rule : Rule.t) =
8176
| Infer -> Memo.Build.return Targets_spec.Infer
8277
| Static { targets; multiplicity } ->
8378
let+ targets =
84-
Memo.Build.List.concat_map targets ~f:(fun target ->
79+
Memo.Build.List.concat_map targets ~f:(fun (target, kind) ->
8580
let error_loc = String_with_vars.loc target in
8681
(match multiplicity with
8782
| One ->
8883
let+ x = Expander.No_deps.expand expander ~mode:Single target in
8984
[ x ]
9085
| Multiple -> Expander.No_deps.expand expander ~mode:Many target)
91-
>>| List.map ~f:(check_filename ~dir ~error_loc))
86+
>>| List.map ~f:(fun value ->
87+
(check_filename ~dir ~error_loc value, kind)))
9288
in
9389
Targets_spec.Static { multiplicity; targets }
9490
in

src/dune_rules/targets_spec.ml

Lines changed: 30 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -25,15 +25,15 @@ end
2525

2626
(* CR-someday amokhov: Add more interesting tags, for example, to allow the user
2727
to specify file patterns like "*.ml" for directory targets. *)
28-
module Tag = struct
28+
module Kind = struct
2929
type t =
30-
| None
31-
| Star
30+
| File
31+
| Directory
3232
end
3333

3434
module Static = struct
3535
type 'path t =
36-
{ targets : 'path list
36+
{ targets : ('path * Kind.t) list
3737
; multiplicity : Multiplicity.t
3838
}
3939
end
@@ -42,39 +42,44 @@ type 'a t =
4242
| Static of 'a Static.t
4343
| Infer
4444

45-
let decode_static =
45+
let decode_target ~allow_directory_targets =
46+
let open Dune_lang.Decoder in
47+
let file =
48+
let+ file = String_with_vars.decode in
49+
(file, Kind.File)
50+
in
51+
let dir =
52+
let+ dir = sum ~force_parens:true [ ("dir", String_with_vars.decode) ] in
53+
if not allow_directory_targets then
54+
User_error.raise ~loc:(String_with_vars.loc dir)
55+
[ Pp.text "Directory targets require the 'directory-targets' extension"
56+
];
57+
58+
(dir, Kind.Directory)
59+
in
60+
file <|> dir
61+
62+
let decode_static ~allow_directory_targets =
4663
let open Dune_lang.Decoder in
4764
let+ syntax_version = Dune_lang.Syntax.get_exn Stanza.syntax
48-
and+ targets = repeat String_with_vars.decode in
65+
and+ targets = repeat (decode_target ~allow_directory_targets) in
4966
if syntax_version < (1, 3) then
50-
List.iter targets ~f:(fun target ->
67+
List.iter targets ~f:(fun (target, (_ : Kind.t)) ->
5168
if String_with_vars.has_pforms target then
5269
Dune_lang.Syntax.Error.since
5370
(String_with_vars.loc target)
5471
Stanza.syntax (1, 3) ~what:"Using variables in the targets field");
5572
Static { targets; multiplicity = Multiple }
5673

57-
let decode_one_static =
74+
let decode_one_static ~allow_directory_targets =
5875
let open Dune_lang.Decoder in
5976
let+ () = Dune_lang.Syntax.since Stanza.syntax (1, 11)
60-
and+ target = String_with_vars.decode in
77+
and+ target = decode_target ~allow_directory_targets in
6178
Static { targets = [ target ]; multiplicity = One }
6279

63-
let field =
80+
let field ~allow_directory_targets =
6481
let open Dune_lang.Decoder in
6582
fields_mutually_exclusive ~default:Infer
66-
[ ("targets", decode_static); ("target", decode_one_static) ]
67-
68-
let has_target_directory = function
69-
| Infer -> false
70-
| Static { targets; _ } ->
71-
List.exists targets ~f:(fun target ->
72-
match String_with_vars.last_text_part target with
73-
| None -> false
74-
| Some part -> Option.is_some (String.drop_suffix ~suffix:"/*" part))
75-
76-
let untag = function
77-
| Infer -> Infer
78-
| Static { targets; multiplicity } ->
79-
let targets = List.map targets ~f:fst in
80-
Static { targets; multiplicity }
83+
[ ("targets", decode_static ~allow_directory_targets)
84+
; ("target", decode_one_static ~allow_directory_targets)
85+
]

src/dune_rules/targets_spec.mli

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -11,32 +11,27 @@ module Multiplicity : sig
1111
val check_variable_matches_field : loc:Loc.t -> field:t -> variable:t -> unit
1212
end
1313

14-
(** Tags are used to distinguish file and directory targets. Specifically, a
15-
directory target is specified by adding "/*" at the end. *)
16-
module Tag : sig
14+
module Kind : sig
1715
type t =
18-
| None
19-
| Star (** Ends with "/*", i.e. "output/*" *)
16+
| File
17+
| Directory
2018
end
2119

2220
module Static : sig
2321
type 'path t =
24-
{ targets : 'path list (** Here ['path] may be tagged with [Tag.t]. *)
22+
{ targets : ('path * Kind.t) list
2523
; multiplicity : Multiplicity.t
2624
}
2725
end
2826

29-
(** Static targets are listed by the user while [Infer] denotes that dune must
30-
discover all the targets. In the [Static] case, dune still implicitly adds
27+
(** Static targets are listed by the user while [Infer] denotes that Dune must
28+
discover all the targets. In the [Static] case, Dune still implicitly adds
3129
the list of inferred targets. *)
3230
type 'a t =
3331
| Static of 'a Static.t
3432
| Infer
3533

3634
(** [target] or [targets] field with the correct multiplicity. *)
37-
val field : String_with_vars.t t Dune_lang.Decoder.fields_parser
38-
39-
(** Contains a directory target. *)
40-
val has_target_directory : String_with_vars.t t -> bool
41-
42-
val untag : ('a * Tag.t) t -> 'a t
35+
val field :
36+
allow_directory_targets:bool
37+
-> String_with_vars.t t Dune_lang.Decoder.fields_parser

0 commit comments

Comments
 (0)