Rename (do ...) to (rule ...)

This commit is contained in:
Jeremie Dimino 2017-06-01 16:53:55 +01:00
parent 543354c070
commit 5e06daa5b2
4 changed files with 43 additions and 54 deletions

View File

@ -288,8 +288,8 @@ Note that contrary to makefiles or other build systems, user rules currently
don't support patterns, such as a rule to produce ``%.y`` from ``%.x`` for any
given ``%``. This might be supported in the future.
do (inferred rules)
-------------------
inferred rules
~~~~~~~~~~~~~~
When using the action DSL (see `User actions`_), it is most of the
time obvious what are the dependencies and targets.
@ -304,28 +304,28 @@ For instance:
(action (copy ${<} ${@}))))))
In this example it is obvious by inspecting the action what the
dependencies and targets are. For this reason Jbuilder provides a
simpler way to define rules where Jbuilder infers dependencies and
targets for you. This is available through the ``do`` stanza:
dependencies and targets are. When this is the case you can use the
following shorter syntax, where Jbuilder infers dependencies and
targets for you:
.. code:: scheme
(do <action>)
(rule <action>)
For instance:
.. code:: scheme
(do (copy a b))
(rule (copy a b))
Note that in Jbuilder, targets must always be known
statically. Especially, this mean that Jbuilder must be able to
statically determine all targets. For instance, this ``(do ...)``
statically determine all targets. For instance, this ``(rule ...)``
stanza is rejected by Jbuilder:
.. code:: scheme
(do (copy a b.${read:file}))
(rule (copy a b.${read:file}))
ocamllex
--------

View File

@ -476,20 +476,12 @@ module Gen(P : Params) = struct
| User rules |
+-----------------------------------------------------------------+ *)
let do_rule (conf : Do.t) ~dir ~package_context =
SC.add_rule sctx
(Build.return []
>>>
SC.Action.run
sctx
conf.action
~dir
~dep_kind:Required
~targets:Infer
~package_context)
let user_rule (rule : Rule.t) ~dir ~package_context =
let targets = List.map rule.targets ~f:(Path.relative dir) in
let targets : SC.Action.targets =
match rule.targets with
| Infer -> Infer
| Static fns -> Static (List.map fns ~f:(Path.relative dir))
in
SC.add_rule sctx
(SC.Deps.interpret sctx ~dir rule.deps
>>>
@ -498,7 +490,7 @@ module Gen(P : Params) = struct
rule.action
~dir
~dep_kind:Required
~targets:(Static targets)
~targets
~package_context)
let alias_rules (alias_conf : Alias_conf.t) ~dir ~package_context =
@ -612,7 +604,6 @@ module Gen(P : Params) = struct
let dir = ctx_dir in
match (stanza : Stanza.t) with
| Rule rule -> user_rule rule ~dir ~package_context
| Do conf -> do_rule conf ~dir ~package_context
| Alias alias -> alias_rules alias ~dir ~package_context
| Library _ | Executables _ | Provides _ | Install _ -> ());
let files = lazy (

View File

@ -692,25 +692,38 @@ module Executables = struct
end
module Rule = struct
module Targets = struct
type t =
| Static of string list (* List of files in the current directory *)
| Infer
end
type t =
{ targets : string list (** List of files in the current directory *)
{ targets : Targets.t
; deps : Dep_conf.t list
; action : Action.Unexpanded.t
}
let v1 =
record
(field "targets" (list file_in_current_dir) >>= fun targets ->
field "deps" (list Dep_conf.t) ~default:[] >>= fun deps ->
field "action" Action.Unexpanded.t >>= fun action ->
return { targets; deps; action })
let v1 = function
| List (_, [sexp]) ->
{ targets = Infer
; deps = []
; action = Action.Unexpanded.t sexp
}
| sexp ->
record
(field "targets" (list file_in_current_dir) >>= fun targets ->
field "deps" (list Dep_conf.t) ~default:[] >>= fun deps ->
field "action" Action.Unexpanded.t >>= fun action ->
return { targets = Static targets; deps; action })
sexp
let ocamllex_v1 names =
let str s = String_with_vars.of_string s ~loc:Loc.none in
List.map names ~f:(fun name ->
let src = name ^ ".mll" in
let dst = name ^ ".ml" in
{ targets = [dst]
{ targets = Static [dst]
; deps = [File (str src)]
; action =
Chdir
@ -723,7 +736,7 @@ module Rule = struct
let str s = String_with_vars.of_string s ~loc:Loc.none in
List.map names ~f:(fun name ->
let src = name ^ ".mly" in
{ targets = [name ^ ".ml"; name ^ ".mli"]
{ targets = Static [name ^ ".ml"; name ^ ".mli"]
; deps = [File (str src)]
; action =
Chdir
@ -731,25 +744,14 @@ module Rule = struct
Run (str "${bin:ocamlyacc}",
[str "${<}"]))
})
end
module Do = struct
type t =
{ loc : Loc.t
; action : Action.Unexpanded.t
}
let v1 sexp =
{ loc = Sexp.Ast.loc sexp
; action = Action.Unexpanded.t sexp
}
let ml_of_mli names =
List.map names ~f:(fun (loc, name) ->
let strf fmt = Printf.ksprintf (String_with_vars.of_string ~loc) fmt in
let m = String.capitalize_ascii name in
{ loc
; action =
{ targets = Infer
; deps = []
; action =
Redirect
(Stdout,
strf "%s.ml" name,
@ -788,7 +790,7 @@ module Menhir = struct
List.map t.modules ~f:(fun name ->
let src = name ^ ".mly" in
{ Rule.
targets = targets name
targets = Static (targets name)
; deps = [Dep_conf.File (str src)]
; action =
Chdir
@ -799,7 +801,7 @@ module Menhir = struct
| Some base ->
let mly m = str (m ^ ".mly") in
[{ Rule.
targets = targets base
targets = Static (targets base)
; deps = List.map ~f:(fun m -> Dep_conf.File (mly m)) t.modules
; action =
Chdir
@ -915,7 +917,6 @@ module Stanza = struct
| Library of Library.t
| Executables of Executables.t
| Rule of Rule.t
| Do of Do.t
| Provides of Provides.t
| Install of Install_conf.t
| Alias of Alias_conf.t
@ -938,13 +939,11 @@ module Stanza = struct
; cstr "menhir" (Menhir.v1 @> nil) (fun x -> rules (Menhir.v1_to_rule x))
; cstr "install" (Install_conf.v1 pkgs @> nil) (fun x -> [Install x])
; cstr "alias" (Alias_conf.v1 pkgs @> nil) (fun x -> [Alias x])
; cstr "do" (Do.v1 @> nil) (fun x -> [Do x])
; cstr_rest "foreach" (Foreach.pattern @> Foreach.values @> nil) (fun x -> x)
(fun pat vals sexps ->
let sexps = Foreach.expand pat vals sexps in
List.concat_map sexps ~f:(v1 pkgs))
; cstr "ml_of_mli" (list (located string) @> nil)
(fun x -> List.map (Do.ml_of_mli x) ~f:(fun x -> Do x))
; cstr "ml_of_mli" (list (located string) @> nil) (fun x -> rules (Rule.ml_of_mli x))
(* Just for validation and error messages *)
; cstr "jbuild_version" (Jbuild_version.t @> nil) (fun _ -> [])
]

View File

@ -39,7 +39,6 @@ val artifacts : t -> Artifacts.t
val stanzas_to_consider_for_install : t -> (Path.t * Stanza.t) list
val cxx_flags : t -> string list
val expand_var_no_root : t -> string -> string option
val expand_vars : t -> dir:Path.t -> String_with_vars.t -> string
val add_rule : t -> ?sandbox:bool -> (unit, Action.t) Build.t -> unit