Move menhir rules to own module

This commit is contained in:
Rudi Grinberg 2018-03-03 23:12:55 +07:00
parent 09aa2cd1cf
commit 288de19920
5 changed files with 64 additions and 41 deletions

View File

@ -1,4 +1,5 @@
open Import
module Menhir_rules = Menhir
open Jbuild
open Build.O
open! No_io
@ -223,6 +224,10 @@ module Gen(P : Install_rules.Params) = struct
let generated_files =
List.concat_map stanzas ~f:(fun stanza ->
match (stanza : Stanza.t) with
| Menhir menhir ->
Menhir_rules.to_rules menhir
|> List.concat_map ~f:(user_rule ~dir ~scope)
|> List.map ~f:Path.basename
| Rule rule ->
List.map (user_rule rule ~dir ~scope) ~f:Path.basename
| Copy_files def ->

View File

@ -945,6 +945,7 @@ module Menhir = struct
; flags : String_with_vars.t list
; modules : string list
; mode : Rule.Mode.t
; loc : Loc.t
}
let v1 =
@ -958,48 +959,9 @@ module Menhir = struct
; flags
; modules
; mode
; loc = Loc.none
}
)
let v1_to_rule loc t =
let module S = String_with_vars in
let targets n = [n ^ ".ml"; n ^ ".mli"] in
match t.merge_into with
| None ->
List.map t.modules ~f:(fun name ->
let src = name ^ ".mly" in
{ Rule.
targets = Static (targets name)
; deps = [Dep_conf.File (S.virt_text __POS__ src)]
; action =
Chdir
(S.virt_var __POS__ "ROOT",
Run (S.virt_text __POS__ "menhir",
t.flags @ [S.virt_var __POS__ "<"]))
; mode = t.mode
; locks = []
; loc
})
| Some merge_into ->
let mly m = S.virt_text __POS__ (m ^ ".mly") in
[{ Rule.
targets = Static (targets merge_into)
; deps = List.map ~f:(fun m -> Dep_conf.File (mly m)) t.modules
; action =
Chdir
(S.virt_var __POS__ "ROOT",
Run (S.virt_text __POS__ "menhir",
List.concat
[ [ S.virt_text __POS__ "--base"
; S.virt_var __POS__ ("path-no-dep:" ^ merge_into)
]
; t.flags
; [ S.virt_var __POS__ "^" ]
]))
; mode = t.mode
; locks = []
; loc
}]
end
module Provides = struct
@ -1067,6 +1029,7 @@ module Stanza = struct
| Install of Install_conf.t
| Alias of Alias_conf.t
| Copy_files of Copy_files.t
| Menhir of Menhir.t
end
module Stanzas = struct
@ -1096,7 +1059,7 @@ module Stanzas = struct
; cstr_loc "ocamlyacc" (Rule.ocamlyacc_v1 @> nil)
(fun loc x -> rules (Rule.ocamlyacc_to_rule loc x))
; cstr_loc "menhir" (Menhir.v1 @> nil)
(fun loc x -> rules (Menhir.v1_to_rule loc x))
(fun loc x -> [Menhir { x with loc }])
; cstr "install" (Install_conf.v1 pkgs @> nil) (fun x -> [Install x])
; cstr "alias" (Alias_conf.v1 pkgs @> nil) (fun x -> [Alias x])
; cstr "copy_files" (Copy_files.v1 @> nil)

View File

@ -284,6 +284,16 @@ module Rule : sig
}
end
module Menhir : sig
type t =
{ merge_into : string option
; flags : String_with_vars.t list
; modules : string list
; mode : Rule.Mode.t
; loc : Loc.t
}
end
module Provides : sig
type t =
{ name : string
@ -317,6 +327,7 @@ module Stanza : sig
| Install of Install_conf.t
| Alias of Alias_conf.t
| Copy_files of Copy_files.t
| Menhir of Menhir.t
end
module Stanzas : sig

42
src/menhir.ml Normal file
View File

@ -0,0 +1,42 @@
open Import
open Jbuild
let to_rules (t : Jbuild.Menhir.t) =
let module S = String_with_vars in
let targets n = [n ^ ".ml"; n ^ ".mli"] in
match t.merge_into with
| None ->
List.map t.modules ~f:(fun name ->
let src = name ^ ".mly" in
{ Jbuild.Rule.
targets = Static (targets name)
; deps = [Dep_conf.File (S.virt_text __POS__ src)]
; action =
Chdir
(S.virt_var __POS__ "ROOT",
Run (S.virt_text __POS__ "menhir",
t.flags @ [S.virt_var __POS__ "<"]))
; mode = t.mode
; locks = []
; loc = t.loc
})
| Some merge_into ->
let mly m = S.virt_text __POS__ (m ^ ".mly") in
[{ Rule.
targets = Static (targets merge_into)
; deps = List.map ~f:(fun m -> Dep_conf.File (mly m)) t.modules
; action =
Chdir
(S.virt_var __POS__ "ROOT",
Run (S.virt_text __POS__ "menhir",
List.concat
[ [ S.virt_text __POS__ "--base"
; S.virt_var __POS__ ("path-no-dep:" ^ merge_into)
]
; t.flags
; [ S.virt_var __POS__ "^" ]
]))
; mode = t.mode
; locks = []
; loc = t.loc
}]

2
src/menhir.mli Normal file
View File

@ -0,0 +1,2 @@
val to_rules : Jbuild.Menhir.t -> Jbuild.Rule.t list