Skip to content

Commit

Permalink
Some clean-up
Browse files Browse the repository at this point in the history
Signed-off-by: Jérôme Vouillon <[email protected]>
  • Loading branch information
vouillon committed Nov 5, 2024
1 parent fd5acfc commit d31295f
Show file tree
Hide file tree
Showing 7 changed files with 29 additions and 52 deletions.
52 changes: 21 additions & 31 deletions src/dune_rules/jsoo/js_of_ocaml.ml
Original file line number Diff line number Diff line change
Expand Up @@ -5,25 +5,6 @@ open Dune_lang.Decoder
wasm_of_ocaml, since the compilation process is similar and
generates a JavaScript file with basically the same behavior. *)

let field_oslu name = Ordered_set_lang.Unexpanded.field name

module Sourcemap = struct
type t =
| No
| Inline
| File

let decode = enum [ "no", No; "inline", Inline; "file", File ]

let equal x y =
match x, y with
| No, No -> true
| Inline, Inline -> true
| File, File -> true
| No, _ | Inline, _ | File, _ -> false
;;
end

module Mode = struct
type t =
| JS
Expand All @@ -46,8 +27,7 @@ module Mode = struct
;;

let decode =
let open Dune_sexp in
let open Decoder in
let open Dune_sexp.Decoder in
sum [ "js", return JS; "wasm", return Wasm ]
;;

Expand All @@ -58,7 +38,7 @@ module Mode = struct
;;

let to_dyn t = Dyn.variant (to_string t) []
let encode t = Dune_sexp.atom (to_string t)
let all = [ JS; Wasm ]

module Pair = struct
type 'a t =
Expand All @@ -73,7 +53,6 @@ module Mode = struct
;;

let make v = { js = v; wasm = v }
let init ~f = { js = f JS; wasm = f Wasm }
let map ~f { js; wasm } = { js = f js; wasm = f wasm }
let mapi ~f { js; wasm } = { js = f JS js; wasm = f Wasm wasm }

Expand All @@ -86,16 +65,26 @@ module Mode = struct
type t = bool Pair.t

let inter = Pair.map2 ~f:( && )
let union = Pair.map2 ~f:( || )
let is_empty (x : t) = not (x.js || x.wasm)

let to_list ({ js; wasm } : t) =
let l = if wasm then [ Wasm ] else [] in
if js then JS :: l else l
;;
end
end

module Sourcemap = struct
type t =
| No
| Inline
| File

let decode = enum [ "no", No; "inline", Inline; "file", File ]

let equal x y =
match x, y with
| No, No -> true
| Inline, Inline -> true
| File, File -> true
| No, _ | Inline, _ | File, _ -> false
;;
end

module Flags = struct
type 'flags t =
{ build_runtime : 'flags
Expand All @@ -110,6 +99,7 @@ module Flags = struct
let build_runtime t = t.build_runtime
let compile t = t.compile
let link t = t.link
let field_oslu name = Ordered_set_lang.Unexpanded.field name

let decode =
let+ build_runtime = field_oslu "build_runtime_flags"
Expand Down Expand Up @@ -332,7 +322,7 @@ module Env = struct
{ compilation_mode; sourcemap; runtest_alias; flags; enabled_if }
;;

let equal { compilation_mode; sourcemap; enabled_if; runtest_alias; flags } t =
let equal { compilation_mode; sourcemap; runtest_alias; flags; enabled_if } t =
Option.equal Compilation_mode.equal compilation_mode t.compilation_mode
&& Option.equal Sourcemap.equal sourcemap t.sourcemap
&& Option.equal Alias.Name.equal runtest_alias t.runtest_alias
Expand Down
6 changes: 1 addition & 5 deletions src/dune_rules/jsoo/js_of_ocaml.mli
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ module Mode : sig
val equal : t -> t -> bool
val compare : t -> t -> Ordering.t
val decode : t Dune_lang.Decoder.t
val encode : t Dune_lang.Encoder.t
val to_dyn : t -> Dyn.t
val all : t list

module Pair : sig
type 'a t =
Expand All @@ -22,7 +22,6 @@ module Mode : sig

val select : mode:mode -> 'a t -> 'a
val make : 'a -> 'a t
val init : f:(mode -> 'a) -> 'a t
val map : f:('a -> 'b) -> 'a t -> 'b t
val mapi : f:(mode -> 'a -> 'b) -> 'a t -> 'b t
end
Expand All @@ -31,9 +30,6 @@ module Mode : sig
type t = bool Pair.t

val inter : t -> t -> t
val union : t -> t -> t
val to_list : t -> mode list
val is_empty : t -> bool
end
end

Expand Down
13 changes: 3 additions & 10 deletions src/dune_rules/jsoo/jsoo_rules.ml
Original file line number Diff line number Diff line change
Expand Up @@ -492,8 +492,6 @@ let build_cm' sctx ~dir ~in_context ~mode ~src ~target ~config ~sourcemap =
~sourcemap
;;

let iter_jsoo_modes ~f = Memo.parallel_iter [ Js_of_ocaml.Mode.JS; Wasm ] ~f

let build_cm sctx ~dir ~in_context ~mode ~src ~obj_dir ~config =
let name = with_js_ext ~mode (Path.basename src) in
let target = in_obj_dir ~obj_dir ~config [ name ] in
Expand Down Expand Up @@ -535,7 +533,7 @@ let setup_separate_compilation_rules sctx components =
archive "stdlib.cma" :: archive "std_exit.cmo" :: archives
| _ -> archives
in
iter_jsoo_modes ~f:(fun mode ->
Memo.parallel_iter Js_of_ocaml.Mode.all ~f:(fun mode ->
Memo.parallel_iter archives ~f:(fun fn ->
let build_context = Context.build_context ctx in
let name = Path.basename fn in
Expand Down Expand Up @@ -635,13 +633,8 @@ let build_exe
=
let sctx = Compilation_context.super_context cc in
let dir = Compilation_context.dir cc in
let { Js_of_ocaml.In_context.javascript_files
; wasm_files
; flags
; compilation_mode
; sourcemap
; _
}
let { javascript_files; wasm_files; flags; compilation_mode; sourcemap; _ }
: Js_of_ocaml.In_context.t
=
in_context
in
Expand Down
2 changes: 0 additions & 2 deletions src/dune_rules/jsoo/jsoo_rules.mli
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,6 @@ val jsoo_is_whole_program
-> dir:Path.Build.t
-> Js_of_ocaml.Mode.Set.t Memo.t

val iter_jsoo_modes : f:(Js_of_ocaml.Mode.t -> unit Memo.t) -> unit Memo.t

val js_of_ocaml_compilation_mode
: Super_context.t
-> dir:Path.Build.t
Expand Down
4 changes: 2 additions & 2 deletions src/dune_rules/lib_rules.ml
Original file line number Diff line number Diff line change
Expand Up @@ -474,7 +474,7 @@ let setup_build_archives (lib : Library.t) ~top_sorted_modules ~cctx ~expander ~
(* Build *.cma.js / *.wasma *)
Memo.when_ modes.ocaml.byte (fun () ->
let src = Library.archive lib ~dir ~ext:(Mode.compiled_lib_ext Mode.Byte) in
Jsoo_rules.iter_jsoo_modes ~f:(fun mode ->
Memo.parallel_iter Js_of_ocaml.Mode.all ~f:(fun mode ->
let action_with_targets =
List.map Jsoo_rules.Config.all ~f:(fun config ->
Jsoo_rules.build_cm
Expand Down Expand Up @@ -542,7 +542,7 @@ let cctx (lib : Library.t) ~sctx ~source_modules ~dir ~expander ~scope ~compile_
~requires_link
~preprocessing:pp
~opaque:Inherit_from_settings
~js_of_ocaml:(Js_of_ocaml.Mode.Pair.map ~f:(fun x -> Some x) js_of_ocaml)
~js_of_ocaml:(Js_of_ocaml.Mode.Pair.map ~f:Option.some js_of_ocaml)
?stdlib:lib.stdlib
~package
?vimpl
Expand Down
2 changes: 1 addition & 1 deletion src/dune_rules/module_compilation.ml
Original file line number Diff line number Diff line change
Expand Up @@ -311,7 +311,7 @@ let build_module ?(force_write_cmi = false) ?(precompiled_cmi = false) cctx m =
match Obj_dir.Module.cm_file obj_dir m ~kind:(Ocaml Cmo) with
| None -> Memo.return ()
| Some src ->
Jsoo_rules.iter_jsoo_modes ~f:(fun mode ->
Memo.parallel_iter Js_of_ocaml.Mode.all ~f:(fun mode ->
Compilation_context.js_of_ocaml cctx
|> Js_of_ocaml.Mode.Pair.select ~mode
|> Memo.Option.iter ~f:(fun in_context ->
Expand Down
2 changes: 1 addition & 1 deletion src/dune_rules/stanzas/buildable.ml
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ let decode (for_ : for_) =
; extra_objects
; libraries
; flags
; js_of_ocaml = { Js_of_ocaml.Mode.Pair.js = js_of_ocaml; wasm = wasm_of_ocaml }
; js_of_ocaml = { js = js_of_ocaml; wasm = wasm_of_ocaml }
; allow_overlapping_dependencies
; ctypes
}
Expand Down

0 comments on commit d31295f

Please sign in to comment.