Refactor alias handling

* Improve documentation of alias module
* Add add_alias helper function to help create rules in alias
This commit is contained in:
Jérémie Dimino 2018-01-13 19:44:06 +08:00 committed by Rudi Grinberg
parent a1026f46d1
commit d3410e0659
4 changed files with 90 additions and 83 deletions

View File

@ -185,18 +185,26 @@ let rules store =
in in
rule :: acc) rule :: acc)
let add_stamp_dep (store: Store.t) (t : t) ~data = let add_build store t ~stamp build =
let digest = Digest.string (Sexp.to_string data) in let digest = Digest.string (Sexp.to_string stamp) in
let digest_path = file_with_digest_suffix t ~digest in let digest_path = file_with_digest_suffix t ~digest in
add_deps store t [digest_path]; add_deps store t [digest_path];
digest_path Build.progn
[ build
; Build.create_file digest_path
]
let add_action_dep (store: Store.t) (t : t) ~action ~action_deps = let add_builds store t builds =
let data = let digest_files, actions =
let deps = Sexp.To_sexp.list Jbuild.Dep_conf.sexp_of_t action_deps in List.split
let action = (List.map builds ~f:(fun (stamp, build) ->
match action with let digest = Digest.string (Sexp.to_string stamp) in
| None -> Sexp.Atom "none" let digest_path = file_with_digest_suffix t ~digest in
| Some a -> List [Atom "some"; Action.Unexpanded.sexp_of_t a] in (digest_path,
Sexp.List [deps ; action] in Build.progn
add_stamp_dep store t ~data [ build
; Build.create_file digest_path
])))
in
add_deps store t digest_files;
actions

View File

@ -67,24 +67,29 @@ module Store : sig
val unlink : t -> string list -> unit val unlink : t -> string list -> unit
end end
(** [add_build store alias deps] arrange things so that all [deps] are built as part of
the build of alias [alias]. *)
val add_deps : Store.t -> t -> Path.t list -> unit val add_deps : Store.t -> t -> Path.t list -> unit
(** [add_build store alias ~stamp build] arrange things so that [build] is part of the
build of alias [alias]. [stamp] is any S-expression that is unique and persistent
S-expression.
Return a rule that must be added with [Super_context.add_rule].
*)
val add_build
: Store.t
-> t
-> stamp:Sexp.t
-> (unit, Action.t) Build.t
-> (unit, Action.t) Build.t
(** Same as calling [add_build] in a loop but slightly more efficient. *)
val add_builds
: Store.t
-> t
-> (Sexp.t * (unit, Action.t) Build.t) list
-> (unit, Action.t) Build.t list
val rules : Store.t -> Build_interpret.Rule.t list val rules : Store.t -> Build_interpret.Rule.t list
(** Create an alias dependency for an action and its inputs represented by
[~data]. The path returned is the file that should be represented by the
file the action will create following execution.*)
val add_stamp_dep
: Store.t
-> t
-> data:Sexp.t
-> Path.t
(** Like [add_stamp_dep] but an action (if present) and the dependencies can be
passed in directly. *)
val add_action_dep
: Store.t
-> t
-> action:Action.Unexpanded.t option
-> action_deps:Jbuild.Dep_conf.t list
-> Path.t

View File

@ -283,7 +283,7 @@ module Gen(P : Params) = struct
Option.iter alias_module ~f:(fun m -> Option.iter alias_module ~f:(fun m ->
let flags = Ocaml_flags.default () in let flags = Ocaml_flags.default () in
Module_compilation.build_module sctx m Module_compilation.build_module sctx m
~js_of_ocaml ~js_of_ocaml
~dynlink ~dynlink
~sandbox:alias_module_build_sandbox ~sandbox:alias_module_build_sandbox
~flags:(Ocaml_flags.append_common flags ["-w"; "-49"]) ~flags:(Ocaml_flags.append_common flags ["-w"; "-49"])
@ -573,33 +573,37 @@ module Gen(P : Params) = struct
~targets ~targets
~scope) ~scope)
let add_alias ~dir ~name ~stamp ?(locks=[]) build =
let alias = Alias.make name ~dir in
SC.add_rule sctx ~locks
(Alias.add_build (SC.aliases sctx) alias ~stamp build)
let alias_rules (alias_conf : Alias_conf.t) ~dir ~scope = let alias_rules (alias_conf : Alias_conf.t) ~dir ~scope =
let alias = Alias.make alias_conf.name ~dir in let stamp =
let digest_path = let module S = Sexp.To_sexp in
Alias.add_action_dep (SC.aliases sctx) alias Sexp.List
~action:alias_conf.action [ Atom "user-alias"
~action_deps:alias_conf.deps in ; S.list Jbuild.Dep_conf.sexp_of_t alias_conf.deps
let deps = SC.Deps.interpret sctx ~scope ~dir alias_conf.deps in ; S.option Action.Unexpanded.sexp_of_t alias_conf.action
SC.add_rule sctx ]
in
add_alias
~dir
~name:alias_conf.name
~stamp
~locks:(interpret_locks ~dir ~scope alias_conf.locks) ~locks:(interpret_locks ~dir ~scope alias_conf.locks)
(match alias_conf.action with (SC.Deps.interpret sctx ~scope ~dir alias_conf.deps
| None -> >>>
deps match alias_conf.action with
>>> | None -> Build.progn []
Build.create_file digest_path
| Some action -> | Some action ->
deps SC.Action.run
>>> sctx
Build.progn action
[ SC.Action.run ~dir
sctx ~dep_kind:Required
action ~targets:(Static [])
~dir ~scope)
~dep_kind:Required
~targets:(Static [])
~scope
; Build.create_file digest_path
])
let copy_files_rules (def: Copy_files.t) ~src_dir ~dir ~scope = let copy_files_rules (def: Copy_files.t) ~src_dir ~dir ~scope =
let loc = String_with_vars.loc def.glob in let loc = String_with_vars.loc def.glob in

View File

@ -892,30 +892,28 @@ module PP = struct
let lint_module sctx ~(source : Module.t) ~(ast : Module.t) ~dir let lint_module sctx ~(source : Module.t) ~(ast : Module.t) ~dir
~dep_kind ~lint ~lib_name ~scope = ~dep_kind ~lint ~lib_name ~scope =
let alias = Alias.lint ~dir in let alias = Alias.lint ~dir in
let add_alias fn build =
add_rule sctx
(Alias.add_build (aliases sctx) alias build
~stamp:(List [ Atom "lint"
; Sexp.To_sexp.(option string) lib_name
; Atom fn]))
in
match Preprocess_map.find source.name lint with match Preprocess_map.find source.name lint with
| No_preprocessing -> () | No_preprocessing -> ()
| Action action -> | Action action ->
let action = Action.U.Chdir (root_var, action) in let action = Action.U.Chdir (root_var, action) in
Module.iter source ~f:(fun _ (src : Module.File.t) -> Module.iter source ~f:(fun _ (src : Module.File.t) ->
let digest_path = let src_path = Path.relative dir src.name in
Alias.add_action_dep add_alias src.name
~action:(Some action) (Build.path src_path
~action_deps:[Dep_conf.File (String_with_vars.virt __POS__ src.name)] >>^ (fun _ -> [src_path])
(aliases sctx) alias in >>> Action.run sctx
let src = Path.relative dir src.name in
add_rule sctx
(Build.path src
>>^ (fun _ -> [src])
>>>
Build.progn
[ Action.run sctx
action action
~dir ~dir
~dep_kind ~dep_kind
~targets:(Static []) ~targets:(Static [])
~scope ~scope)
; Build.create_file digest_path
])
) )
| Pps { pps; flags } -> | Pps { pps; flags } ->
let ppx_exe = get_ppx_driver sctx pps ~dir ~dep_kind in let ppx_exe = get_ppx_driver sctx pps ~dir ~dep_kind in
@ -926,25 +924,17 @@ module PP = struct
; As (cookie_library_name lib_name) ; As (cookie_library_name lib_name)
; Ml_kind.ppx_driver_flag kind ; Ml_kind.ppx_driver_flag kind
; Dep src_path ; Dep src_path
] in ]
in
let args = let args =
(* This hack is needed until -null is standard: (* This hack is needed until -null is standard:
https://github.com/ocaml-ppx/ocaml-migrate-parsetree/issues/35 *) https://github.com/ocaml-ppx/ocaml-migrate-parsetree/issues/35 *)
match Option.map ~f:Pp.to_string (List.last pps) with match Option.map ~f:Pp.to_string (List.last pps) with
| Some "ppx_driver.runner" -> args @ [A "-null"] | Some "ppx_driver.runner" -> args @ [A "-null"]
| Some _ | None -> args in | Some _ | None -> args
let digest_path = in
Alias.add_stamp_dep (aliases sctx) alias add_alias src.name
~data:( (Build.run ~context:sctx.context (Ok ppx_exe) args)
Sexp.To_sexp.(
triple Path.sexp_of_t string (pair (list string) Path.Set.sexp_of_t)
) (ppx_exe, src.name, Arg_spec.expand ~dir args ())
) in
add_rule sctx
(Build.progn
[ Build.run ~context:sctx.context (Ok ppx_exe) args
; Build.create_file digest_path
])
) )
(* Generate rules to build the .pp files and return a new module map where all filenames (* Generate rules to build the .pp files and return a new module map where all filenames