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

View File

@ -476,20 +476,12 @@ module Gen(P : Params) = struct
| User rules | | 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 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.add_rule sctx
(SC.Deps.interpret sctx ~dir rule.deps (SC.Deps.interpret sctx ~dir rule.deps
>>> >>>
@ -498,7 +490,7 @@ module Gen(P : Params) = struct
rule.action rule.action
~dir ~dir
~dep_kind:Required ~dep_kind:Required
~targets:(Static targets) ~targets
~package_context) ~package_context)
let alias_rules (alias_conf : Alias_conf.t) ~dir ~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 let dir = ctx_dir in
match (stanza : Stanza.t) with match (stanza : Stanza.t) with
| Rule rule -> user_rule rule ~dir ~package_context | Rule rule -> user_rule rule ~dir ~package_context
| Do conf -> do_rule conf ~dir ~package_context
| Alias alias -> alias_rules alias ~dir ~package_context | Alias alias -> alias_rules alias ~dir ~package_context
| Library _ | Executables _ | Provides _ | Install _ -> ()); | Library _ | Executables _ | Provides _ | Install _ -> ());
let files = lazy ( let files = lazy (

View File

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