Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix native code incremental compilation #238

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 24 additions & 1 deletion src/gen_rules.ml
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,12 @@ module Gen(P : Params) = struct
else
fun x -> x
in
let objs (cm, _, _, _) =
if mode = Mode.Byte then
[]
else
List.map ~f:(Path.change_extension ~ext:ctx.ext_obj) cm
in
SC.add_rule sctx
(Build.fanout4
(dep_graph >>>
Expand All @@ -100,6 +106,8 @@ module Gen(P : Params) = struct
(Ocaml_flags.get flags mode)
(SC.expand_and_eval_set sctx ~scope ~dir lib.library_flags ~standard:[])
>>>
Build.dyn_paths (Build.arr objs)
>>>
Build.run ~context:ctx (Dep compiler)
~extra_targets:(
match mode with
Expand Down Expand Up @@ -376,6 +384,8 @@ module Gen(P : Params) = struct
let src = lib_archive lib ~dir ~ext:(Mode.compiled_lib_ext Native) in
let dst = lib_archive lib ~dir ~ext:".cmxs" in
let build =
Build.dyn_paths (Build.arr (fun () -> [lib_archive lib ~dir ~ext:ctx.ext_lib]))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My understanding of jbuilder's internals here is a bit shaky, so I'm curious:

why do you need Build.dyn_paths over Build.paths?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think (my understanding's often shaky too - there's a virtuous project in the making to write a big document on Jbuilder's internals, just for the exercise of thoroughly understanding it all oneself!) that it's because it's a generated target - so paths is things which should exist already (sources, etc.) and dyn_paths is things which are generated.

However, my change is based more on the fact that the .cmx and .cmxa files elsewhere are incorporated using dyn_paths (via Arg_spec, which generates that part of the build arrow as a side effect).

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rules are build arrow of types (unit, Action.t) Build.t, but really they could be (unit, { runtime_deps : Path.Set.t; action : Action.t }) Build.t. It is however simpler to build the set of runtime dependencies implicitly since all we do with it is always pass it through or extend it.

In the expanded version, Build.dyn_paths l basically means: Build.arr (fun x -> { x with runtime_deps = Path.Set.union x.runtime_deps (Path.Set.of_list l) }). In any case, Build.paths l is always the same as Build.dyn_paths (Build.arr (fun _ -> l)).

The only difference between the two is that with Build.paths, the dependencies are known statically. This means that we can get most of the DAG without running any command and this is enough to generate the list of package dependencies that goes into the opam file. We use that feature to generate the opam files of all Jane Street packages without hassle.

Additionally, in some cases using Build.paths can improve compilation times as static dependencies are known earlier.

In the end, we should use Build.paths wherever possible.

>>>
Ocaml_flags.get flags Native
>>>
Build.run ~context:ctx
Expand Down Expand Up @@ -440,8 +450,21 @@ module Gen(P : Params) = struct
~mode
[String.capitalize_ascii name]))
in
let objs (libs, cm) =
if mode = Mode.Byte then
[]
else
let libs =
let f = function
| Lib.Internal (dir, lib) -> Some (Path.relative dir (lib.name ^ ctx.ext_lib))
| External _ -> None
in
List.filter_map ~f libs
in
libs @ List.map ~f:(Path.change_extension ~ext:ctx.ext_obj) cm
in
SC.add_rule sctx
(libs_and_cm
((libs_and_cm >>> Build.dyn_paths (Build.arr objs))
&&&
Build.fanout
(Ocaml_flags.get flags mode)
Expand Down
4 changes: 4 additions & 0 deletions src/path.ml
Original file line number Diff line number Diff line change
Expand Up @@ -411,3 +411,7 @@ let rm_rf =
match Unix.lstat fn with
| exception Unix.Unix_error(ENOENT, _, _) -> ()
| _ -> loop fn

let change_extension ~ext t =
let t = try Filename.chop_extension t with Not_found -> t in
t ^ ext
3 changes: 3 additions & 0 deletions src/path.mli
Original file line number Diff line number Diff line change
Expand Up @@ -101,3 +101,6 @@ val rmdir : t -> unit
val unlink : t -> unit
val unlink_no_err : t -> unit
val rm_rf : t -> unit

(** Changes the extension of the filename (or adds an extension if there was none) *)
val change_extension : ext:string -> t -> t