Skip to content

Commit

Permalink
feature: multiple alias support in rules
Browse files Browse the repository at this point in the history
Signed-off-by: Ali Caglayan <[email protected]>

ps-id: 7f58c532-3611-4290-b07c-9ad973f2a995
Signed-off-by: Ali Caglayan <[email protected]>
  • Loading branch information
Alizter committed Oct 6, 2022
1 parent 7782fe6 commit 37298d9
Show file tree
Hide file tree
Showing 6 changed files with 107 additions and 31 deletions.
20 changes: 12 additions & 8 deletions src/dune_rules/dune_file.ml
Original file line number Diff line number Diff line change
Expand Up @@ -1476,7 +1476,7 @@ module Rule = struct
; locks : Locks.t
; loc : Loc.t
; enabled_if : Blang.t
; alias : Alias.Name.t option
; aliases : Alias.Name.t list
; package : Package.t option
}

Expand Down Expand Up @@ -1528,7 +1528,7 @@ module Rule = struct
; locks = []
; loc
; enabled_if = Blang.true_
; alias = None
; aliases = []
; package = None
}

Expand Down Expand Up @@ -1574,9 +1574,13 @@ module Rule = struct
field_o "package"
(Dune_lang.Syntax.since Stanza.syntax (2, 0)
>>> Stanza_common.Pkg.decode)
and+ alias =
field_o "alias"
(Dune_lang.Syntax.since Stanza.syntax (2, 0) >>> Alias.Name.decode)
and+ aliases =
field "alias" ~default:[]
(repeat Alias.Name.decode >>= function
| [] as l -> return l
| [ _ ] as l ->
Dune_lang.Syntax.since Stanza.syntax (2, 0) >>> return l
| l -> Dune_lang.Syntax.since Stanza.syntax (3, 5) >>> return l)
in
let mode, patch_back_source_tree =
match mode with
Expand All @@ -1602,7 +1606,7 @@ module Rule = struct
; locks
; loc
; enabled_if
; alias
; aliases
; package
; patch_back_source_tree
})
Expand Down Expand Up @@ -1672,7 +1676,7 @@ module Rule = struct
; locks = []
; loc
; enabled_if
; alias = None
; aliases = []
; package = None
})

Expand Down Expand Up @@ -1702,7 +1706,7 @@ module Rule = struct
; locks = []
; loc
; enabled_if
; alias = None
; aliases = []
; package = None
})
end
Expand Down
2 changes: 1 addition & 1 deletion src/dune_rules/dune_file.mli
Original file line number Diff line number Diff line change
Expand Up @@ -292,7 +292,7 @@ module Rule : sig
; locks : Locks.t
; loc : Loc.t
; enabled_if : Blang.t
; alias : Alias.Name.t option
; aliases : Alias.Name.t list
; package : Package.t option
}
end
Expand Down
48 changes: 27 additions & 21 deletions src/dune_rules/simple_rules.ml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,12 @@ module Alias_rules = struct
let add_empty sctx ~loc ~alias =
let action = Action_builder.return (Action.Full.make Action.empty) in
add sctx ~loc ~alias action

let add_list sctx ~aliases ~loc build =
Memo.parallel_iter aliases ~f:(fun alias -> add sctx ~alias ~loc build)

let add_list_empty sctx ~loc ~aliases =
Memo.parallel_iter aliases ~f:(fun alias -> add_empty sctx ~loc ~alias)
end

let check_filename =
Expand Down Expand Up @@ -37,17 +43,17 @@ let check_filename =
| Dir p -> not_in_dir ~kind ~error_loc (Path.to_string p)

type rule_kind =
| Alias_only of Alias.Name.t
| Alias_with_targets of Alias.Name.t * Path.Build.t
| Aliases_only of Alias.Name.t list
| Aliases_with_targets of Alias.Name.t list * Path.Build.t
| No_alias

let rule_kind ~(rule : Rule.t) ~(action : _ Action_builder.With_targets.t) =
match rule.alias with
| None -> No_alias
| Some alias -> (
match rule.aliases with
| [] -> No_alias
| aliases -> (
match Targets.head action.targets with
| None -> Alias_only alias
| Some target -> Alias_with_targets (alias, target))
| None -> Aliases_only aliases
| Some target -> Aliases_with_targets (aliases, target))

let interpret_and_add_locks ~expander locks action =
let+ locks = Expander.expand_locks expander ~base:`Of_expander locks in
Expand All @@ -66,13 +72,10 @@ let add_user_rule sctx ~dir ~(rule : Rule.t)

let user_rule sctx ?extra_bindings ~dir ~expander (rule : Rule.t) =
Expander.eval_blang expander rule.enabled_if >>= function
| false -> (
match rule.alias with
| None -> Memo.return None
| Some name ->
let alias = Alias.make ~dir name in
let+ () = Alias_rules.add_empty sctx ~alias ~loc:(Some rule.loc) in
None)
| false ->
let aliases = List.map rule.aliases ~f:(Alias.make ~dir) in
let+ () = Alias_rules.add_list_empty sctx ~aliases ~loc:(Some rule.loc) in
None
| true -> (
let* targets =
match rule.targets with
Expand Down Expand Up @@ -120,18 +123,21 @@ let user_rule sctx ?extra_bindings ~dir ~expander (rule : Rule.t) =
| No_alias ->
let+ targets = add_user_rule sctx ~dir ~rule ~action ~expander in
Some targets
| Alias_with_targets (alias, alias_target) ->
| Aliases_with_targets (aliases, alias_target) ->
let* () =
let alias = Alias.make alias ~dir in
Rules.Produce.Alias.add_deps alias
(Action_builder.path (Path.build alias_target))
let aliases = List.map ~f:(Alias.make ~dir) aliases in
Memo.parallel_iter aliases ~f:(fun alias ->
Rules.Produce.Alias.add_deps alias
(Action_builder.path (Path.build alias_target)))
in
let+ targets = add_user_rule sctx ~dir ~rule ~action ~expander in
Some targets
| Alias_only name ->
let alias = Alias.make ~dir name in
| Aliases_only aliases ->
let aliases = List.map ~f:(Alias.make ~dir) aliases in
let* action = interpret_and_add_locks ~expander rule.locks action.build in
let+ () = Alias_rules.add sctx ~alias ~loc:(Some rule.loc) action in
let+ () =
Alias_rules.add_list sctx ~aliases ~loc:(Some rule.loc) action
in
None)

let copy_files sctx ~dir ~expander ~src_dir (def : Copy_files.t) =
Expand Down
13 changes: 13 additions & 0 deletions src/dune_rules/simple_rules.mli
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,19 @@ module Alias_rules : sig

val add_empty :
Super_context.t -> loc:Stdune.Loc.t option -> alias:Alias.t -> unit Memo.t

val add_list :
Super_context.t
-> aliases:Alias.t list
-> loc:Loc.t option
-> Action.Full.t Action_builder.t
-> unit Memo.t

val add_list_empty :
Super_context.t
-> loc:Stdune.Loc.t option
-> aliases:Alias.t list
-> unit Memo.t
end

(** Interpret a [(rule ...)] stanza and return the targets it produces, if any. *)
Expand Down
2 changes: 1 addition & 1 deletion src/dune_rules/test_rules.ml
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ let rules (t : Dune_file.Tests.t) ~sctx ~dir ~scope ~expander ~dir_contents =
; locks = t.locks
; loc
; enabled_if = t.enabled_if
; alias = None
; aliases = []
; package = t.package
}
in
Expand Down
53 changes: 53 additions & 0 deletions test/blackbox-tests/test-cases/alias-multiple.t
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
Testing multiple aliases in rules stanza

First we start with a dune-project before alias was introduced:
$ cat > dune-project << EOF
> (lang dune 1.9)
> EOF

$ cat > dune << EOF
> (rule
> (alias a)
> (action (echo "I have run")))
> EOF

$ dune build @a
File "dune", line 2, characters 0-9:
2 | (alias a)
^^^^^^^^^
Error: 'alias' is only available since version 2.0 of the dune language.
Please update your dune-project file to have (lang dune 2.0).
[1]

Next we update the dune-project file to use dune 2.0:
$ cat > dune-project << EOF
> (lang dune 2.0)
> EOF

$ dune build @a
I have run

We now update the dune file to use multiple aliases
$ cat > dune << EOF
> (rule
> (alias a b)
> (action (echo "I have run")))
> EOF

$ dune build @a @b
File "dune", line 2, characters 0-11:
2 | (alias a b)
^^^^^^^^^^^
Error: 'alias' is only available since version 3.5 of the dune language.
Please update your dune-project file to have (lang dune 3.5).
[1]

Updating the dune-project file to use dune 3.5 allows the build to succeed:
$ cat > dune-project << EOF
> (lang dune 3.5)
> EOF

$ dune build @a
I have run
$ dune build @b
I have run

0 comments on commit 37298d9

Please sign in to comment.