Skip to content

Commit

Permalink
Avoid generating .bc rule twice, also apply to .cmo.js rules
Browse files Browse the repository at this point in the history
  • Loading branch information
nojb committed Mar 17, 2019
1 parent 0942858 commit 3e674b0
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 38 deletions.
1 change: 1 addition & 0 deletions src/dune_file.mli
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,7 @@ module Executables : sig
val object_ : t
val shared_object : t
val byte : t
val byte_js : t
val native : t

val compare : t -> t -> Ordering.t
Expand Down
57 changes: 24 additions & 33 deletions src/exe.ml
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,6 @@ module Linkage = struct
; flags = []
}

let js =
{ mode = Byte
; ext = ".bc.js"
; flags = []
}

let custom =
{ mode = Byte
; ext = ".exe"
Expand Down Expand Up @@ -131,15 +125,9 @@ let link_exe
~top_sorted_modules
~arg_spec_for_requires
?(link_flags=Build.arr (fun _ -> []))
?(js_of_ocaml=Dune_file.Js_of_ocaml.default)
?js_of_ocaml
cctx
=
let linkage, js =
if linkage.ext = ".bc.js" then
{linkage with ext = ".bc"}, true
else
linkage, false
in
let sctx = CC.super_context cctx in
let ctx = SC.context sctx in
let dir = CC.dir cctx in
Expand All @@ -149,9 +137,6 @@ let link_exe
let exe = exe_path_from_name cctx ~name ~linkage in
let compiler = Option.value_exn (Context.compiler ctx mode) in
let kind = Mode.cm_kind mode in
let explicit_js_mode =
Dune_project.explicit_js_mode (Scope.project (CC.scope cctx))
in
let artifacts ~ext modules =
List.map modules ~f:(Module.obj_file ~mode ~ext)
in
Expand Down Expand Up @@ -192,32 +177,38 @@ let link_exe
; Dyn (fun (cm_files, _, _) -> Deps cm_files)
]);
if linkage.ext = ".bc" then
let rules =
if not explicit_js_mode || js then
Js_of_ocaml_rules.build_exe cctx ~js_of_ocaml ~src:exe
else
[]
in
let cm_and_flags =
Build.fanout
(modules_and_cm_files >>^ snd)
(Expander.expand_and_eval_set expander
js_of_ocaml.flags
~standard:(Build.return (Js_of_ocaml_rules.standard sctx)))
in
SC.add_rules ~dir sctx (List.map rules ~f:(fun r -> cm_and_flags >>> r))
Option.iter ~f:(fun js_of_ocaml ->
let rules = Js_of_ocaml_rules.build_exe cctx ~js_of_ocaml ~src:exe in
let cm_and_flags =
Build.fanout
(modules_and_cm_files >>^ snd)
(Expander.expand_and_eval_set expander
js_of_ocaml.flags
~standard:(Build.return (Js_of_ocaml_rules.standard sctx)))
in
SC.add_rules ~dir sctx (List.map rules ~f:(fun r -> cm_and_flags >>> r))
) js_of_ocaml

let build_and_link_many
~programs
~linkages
?link_flags
?(js_of_ocaml=Dune_file.Js_of_ocaml.default)
?js_of_ocaml
cctx
=
let dep_graphs = Ocamldep.rules cctx in

let explicit_js_mode =
Dune_project.explicit_js_mode (Scope.project (CC.scope cctx))
in
let js_of_ocaml =
match explicit_js_mode, js_of_ocaml with
| false, None -> Some Dune_file.Js_of_ocaml.default
| _ -> js_of_ocaml
in

(* CR-someday jdimino: this should probably say [~dynlink:false] *)
Module_compilation.build_modules cctx ~js_of_ocaml ~dep_graphs;
Module_compilation.build_modules cctx ?js_of_ocaml ~dep_graphs;

List.iter programs ~f:(fun { Program.name; main_module_name ; loc } ->
let top_sorted_modules =
Expand All @@ -237,7 +228,7 @@ let build_and_link_many
~name
~linkage
~top_sorted_modules
~js_of_ocaml
?js_of_ocaml
~arg_spec_for_requires
?link_flags))

Expand Down
3 changes: 0 additions & 3 deletions src/exe.mli
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,6 @@ module Linkage : sig
(** Byte compilation, link with [-custom], extension [.exe] *)
val custom : t

(** Javascript compilation, extension [.bc.js] *)
val js : t

(** [native] if supported, [custom] if not *)
val native_or_custom : Context.t -> t

Expand Down
19 changes: 17 additions & 2 deletions src/exe_rules.ml
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,20 @@ let executables_rules ~sctx ~dir ~dir_kind ~expander
Module.Name.pp mod_name)
in

let has_js = Dune_file.Executables.Link_mode.(Set.mem exes.modes byte_js) in

let linkages =
let module L = Dune_file.Executables.Link_mode in
let ctx = SC.context sctx in
let modes =
if has_js then
L.Set.remove (L.Set.add exes.modes L.byte) L.byte_js
else
exes.modes
in
let l =
let has_native = Option.is_some ctx.ocamlopt in
List.filter_map (L.Set.to_list exes.modes) ~f:(fun (mode : L.t) ->
List.filter_map (L.Set.to_list modes) ~f:(fun (mode : L.t) ->
match has_native, mode.mode with
| false, Native ->
None
Expand Down Expand Up @@ -97,12 +105,19 @@ let executables_rules ~sctx ~dir ~dir_kind ~expander
in

let requires_compile = Compilation_context.requires_compile cctx in
let explicit_js_mode = Dune_project.explicit_js_mode (Scope.project scope) in
let js_of_ocaml =
if not explicit_js_mode || has_js then
Some exes.buildable.js_of_ocaml
else
None
in

Exe.build_and_link_many cctx
~programs
~linkages
~link_flags
~js_of_ocaml:exes.buildable.js_of_ocaml;
?js_of_ocaml;

(cctx,
Merlin.make ()
Expand Down

0 comments on commit 3e674b0

Please sign in to comment.