Skip to content

Commit

Permalink
Wasm_of_ocaml support: alternative design
Browse files Browse the repository at this point in the history
Signed-off-by: Jérôme Vouillon <jerome.vouillon@gmail.com>
  • Loading branch information
vouillon committed Nov 4, 2024
1 parent fdbd71b commit ec321a5
Show file tree
Hide file tree
Showing 58 changed files with 744 additions and 683 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/workflow.yml
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,7 @@ jobs:
uses: actions/checkout@v4
with:
repository: ocaml-wasm/wasm_of_ocaml
ref: target-dir
ref: wasm-dune
path: wasm_of_ocaml

- name: Install Wasm_of_ocaml
Expand Down
20 changes: 16 additions & 4 deletions bin/printenv.ml
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,25 @@ let dump sctx ~dir =
|> Action_builder.of_memo
>>= Dune_rules.Menhir_env.dump
and+ coq_dump = Dune_rules.Coq.Coq_rules.coq_env ~dir >>| Dune_rules.Coq.Coq_flags.dump
and+ jsoo_dump =
and+ jsoo_js_dump =
let module Js_of_ocaml = Dune_rules.Js_of_ocaml in
let* jsoo = Action_builder.of_memo (Dune_rules.Jsoo_rules.jsoo_env ~dir) in
Js_of_ocaml.Flags.dump jsoo.flags
let* jsoo = Action_builder.of_memo (Dune_rules.Jsoo_rules.jsoo_env ~dir ~mode:JS) in
Js_of_ocaml.Flags.dump ~mode:JS jsoo.flags
and+ jsoo_wasm_dump =
let module Js_of_ocaml = Dune_rules.Js_of_ocaml in
let* jsoo = Action_builder.of_memo (Dune_rules.Jsoo_rules.jsoo_env ~dir ~mode:Wasm) in
Js_of_ocaml.Flags.dump ~mode:Wasm jsoo.flags
in
let env =
List.concat [ o_dump; c_dump; link_flags_dump; menhir_dump; coq_dump; jsoo_dump ]
List.concat
[ o_dump
; c_dump
; link_flags_dump
; menhir_dump
; coq_dump
; jsoo_js_dump
; jsoo_wasm_dump
]
in
Super_context.context sctx |> Context.name, env
;;
Expand Down
8 changes: 1 addition & 7 deletions src/dune_lang/binary_kind.ml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ type t =
| Object
| Shared_object
| Plugin
| Js

let compare x y =
match x, y with
Expand All @@ -23,9 +22,6 @@ let compare x y =
| Shared_object, _ -> Lt
| _, Shared_object -> Gt
| Plugin, Plugin -> Eq
| Plugin, _ -> Lt
| _, Plugin -> Gt
| Js, Js -> Eq
;;

let decode =
Expand All @@ -37,7 +33,6 @@ let decode =
; "object", return Object
; "shared_object", return Shared_object
; "plugin", Syntax.since Stanza.syntax (2, 4) >>> return Plugin
; "js", Syntax.since Stanza.syntax (1, 11) >>> return Js
]
;;

Expand All @@ -47,9 +42,8 @@ let to_string = function
| Object -> "object"
| Shared_object -> "shared_object"
| Plugin -> "plugin"
| Js -> "js"
;;

let to_dyn t = Dyn.variant (to_string t) []
let encode t = Dune_sexp.atom (to_string t)
let all = [ C; Exe; Object; Shared_object; Plugin; Js ]
let all = [ C; Exe; Object; Shared_object; Plugin ]
1 change: 0 additions & 1 deletion src/dune_lang/binary_kind.mli
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ type t =
| Object
| Shared_object
| Plugin
| Js

val compare : t -> t -> Ordering.t

Expand Down
2 changes: 1 addition & 1 deletion src/dune_rules/cinaps.ml
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ let gen_rules sctx t ~dir ~scope =
~requires_compile
~requires_link
~flags:(Ocaml_flags.of_list [ "-w"; "-24" ])
~js_of_ocaml:None
~js_of_ocaml:(Js_of_ocaml.Mode.Pair.make None)
~melange_package_name:None
~package:None
in
Expand Down
2 changes: 1 addition & 1 deletion src/dune_rules/compilation_context.ml
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ type t =
; preprocessing : Pp_spec.t
; opaque : bool
; stdlib : Ocaml_stdlib.t option
; js_of_ocaml : Js_of_ocaml.In_context.t option
; js_of_ocaml : Js_of_ocaml.In_context.t option Js_of_ocaml.Mode.Pair.t
; sandbox : Sandbox_config.t
; package : Package.t option
; vimpl : Vimpl.t option
Expand Down
4 changes: 2 additions & 2 deletions src/dune_rules/compilation_context.mli
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ val create
-> ?preprocessing:Pp_spec.t
-> opaque:opaque
-> ?stdlib:Ocaml_stdlib.t
-> js_of_ocaml:Js_of_ocaml.In_context.t option
-> js_of_ocaml:Js_of_ocaml.In_context.t option Js_of_ocaml.Mode.Pair.t
-> package:Package.t option
-> melange_package_name:Lib_name.t option
-> ?vimpl:Vimpl.t
Expand Down Expand Up @@ -61,7 +61,7 @@ val includes : t -> Command.Args.without_targets Command.Args.t Lib_mode.Cm_kind
val preprocessing : t -> Pp_spec.t
val opaque : t -> bool
val stdlib : t -> Ocaml_stdlib.t option
val js_of_ocaml : t -> Js_of_ocaml.In_context.t option
val js_of_ocaml : t -> Js_of_ocaml.In_context.t option Js_of_ocaml.Mode.Pair.t
val sandbox : t -> Sandbox_config.t
val set_sandbox : t -> Sandbox_config.t -> t
val package : t -> Package.t option
Expand Down
51 changes: 20 additions & 31 deletions src/dune_rules/dir_status.ml
Original file line number Diff line number Diff line change
Expand Up @@ -134,36 +134,25 @@ let directory_targets_of_rule ~dir { Rule_conf.targets; loc = rule_loc; enabled_
when_enabled ~dir ~enabled_if directory_targets
;;

let jsoo_wasm_enabled
~(jsoo_submodes :
dir:Path.Build.t
-> submodes:Js_of_ocaml.Submode.Set.t option
-> Js_of_ocaml.Submode.t list Memo.t)
~dir
~submodes
=
let+ submodes = jsoo_submodes ~dir ~submodes in
List.mem ~equal:Js_of_ocaml.Submode.equal submodes Wasm
let jsoo_wasm_enabled ~jsoo_enabled ~dir ~(buildable : Buildable.t) =
let* expander = Expander0.get ~dir in
jsoo_enabled
~eval:(Expander0.eval_blang expander)
~dir
~in_context:(Js_of_ocaml.In_context.make ~dir buildable.js_of_ocaml)
~mode:Js_of_ocaml.Mode.Wasm
;;

let directory_targets_of_executables
~jsoo_submodes
~jsoo_enabled
~dir
{ Executables.names; modes; enabled_if; buildable; _ }
=
let* directory_targets =
(* CR-someday rgrinberg: we don't necessarily need to evalute
[explicit_js_mode] or [wasm_enabled] here *)
let+ wasm_enabled =
jsoo_wasm_enabled ~jsoo_submodes ~dir ~submodes:buildable.js_of_ocaml.submodes
and+ explicit_js_mode =
Scope.DB.find_by_dir dir >>| Scope.project >>| Dune_project.explicit_js_mode
in
match
Executables.Link_mode.(
Map.mem modes js || ((not explicit_js_mode) && Map.mem modes byte))
&& wasm_enabled
with
[wasm_enabled] here *)
let+ wasm_enabled = jsoo_wasm_enabled ~jsoo_enabled ~dir ~buildable in
match Executables.Link_mode.(Map.mem modes wasm) && wasm_enabled with
| false -> Path.Build.Map.empty
| true ->
Nonempty_list.to_list names
Expand All @@ -175,15 +164,15 @@ let directory_targets_of_executables
;;

let directory_targets_of_library
~jsoo_submodes
~jsoo_enabled
~dir
{ Library.sub_systems; name; enabled_if; buildable; _ }
=
let* directory_targets =
match Sub_system_name.Map.find sub_systems Inline_tests_info.Tests.name with
| Some (Inline_tests_info.Tests.T { modes; loc; enabled_if; _ })
when Inline_tests_info.Mode_conf.Set.mem modes Javascript ->
jsoo_wasm_enabled ~jsoo_submodes ~dir ~submodes:buildable.js_of_ocaml.submodes
when Inline_tests_info.Mode_conf.Set.mem modes (Jsoo Wasm) ->
jsoo_wasm_enabled ~jsoo_enabled ~dir ~buildable
>>| (function
| false -> Path.Build.Map.empty
| true ->
Expand All @@ -207,13 +196,13 @@ let directory_targets_of_library
when_enabled ~dir ~enabled_if directory_targets
;;

let extract_directory_targets ~jsoo_submodes ~dir stanzas =
let extract_directory_targets ~jsoo_enabled ~dir stanzas =
Memo.parallel_map stanzas ~f:(fun stanza ->
match Stanza.repr stanza with
| Rule_conf.T rule -> directory_targets_of_rule ~dir rule
| Executables.T exes | Tests.T { exes; _ } ->
directory_targets_of_executables ~jsoo_submodes ~dir exes
| Library.T lib -> directory_targets_of_library ~jsoo_submodes ~dir lib
directory_targets_of_executables ~jsoo_enabled ~dir exes
| Library.T lib -> directory_targets_of_library ~jsoo_enabled ~dir lib
| Coq_stanza.Theory.T m ->
(* It's unfortunate that we need to pull in the coq rules here. But
we don't have a generic mechanism for this yet. *)
Expand Down Expand Up @@ -360,15 +349,15 @@ end = struct
;;
end

let directory_targets t ~jsoo_submodes ~dir =
let directory_targets t ~jsoo_enabled ~dir =
match t with
| Lock_dir | Generated | Source_only _ | Is_component_of_a_group_but_not_the_root _ ->
Memo.return Path.Build.Map.empty
| Standalone (_, dune_file) ->
Dune_file.stanzas dune_file >>= extract_directory_targets ~jsoo_submodes ~dir
Dune_file.stanzas dune_file >>= extract_directory_targets ~jsoo_enabled ~dir
| Group_root { components; dune_file; _ } ->
let f ~dir stanzas acc =
extract_directory_targets ~jsoo_submodes ~dir stanzas
extract_directory_targets ~jsoo_enabled ~dir stanzas
>>| Path.Build.Map.superpose acc
in
let* init =
Expand Down
10 changes: 6 additions & 4 deletions src/dune_rules/dir_status.mli
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,11 @@ end

val directory_targets
: t
-> jsoo_submodes:
(dir:Path.Build.t
-> submodes:Js_of_ocaml.Submode.Set.t option
-> Js_of_ocaml.Submode.t list Memo.t)
-> jsoo_enabled:
(eval:(Blang.t -> bool Memo.t)
-> dir:Path.Build.t
-> in_context:Js_of_ocaml.In_context.t Js_of_ocaml.Mode.Pair.t
-> mode:Js_of_ocaml.Mode.t
-> bool Memo.t)
-> dir:Path.Build.t
-> Loc.t Path.Build.Map.t Memo.t
13 changes: 13 additions & 0 deletions src/dune_rules/dune_env.ml
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ type config =
; menhir : Ordered_set_lang.Unexpanded.t Menhir_env.t
; odoc : Odoc.t
; js_of_ocaml : Ordered_set_lang.Unexpanded.t Js_of_ocaml.Env.t
; wasm_of_ocaml : Ordered_set_lang.Unexpanded.t Js_of_ocaml.Env.t
; coq : Coq_env.t
; format_config : Format_config.t option
; error_on_use : User_message.t option
Expand All @@ -102,6 +103,7 @@ let equal_config
; menhir
; odoc
; js_of_ocaml
; wasm_of_ocaml
; coq
; format_config
; error_on_use
Expand All @@ -124,6 +126,7 @@ let equal_config
&& Coq_env.equal coq t.coq
&& Option.equal Format_config.equal format_config t.format_config
&& Js_of_ocaml.Env.equal js_of_ocaml t.js_of_ocaml
&& Js_of_ocaml.Env.equal wasm_of_ocaml t.wasm_of_ocaml
&& Option.equal User_message.equal error_on_use t.error_on_use
&& Option.equal User_message.equal warn_on_load t.warn_on_load
&& Option.equal Bool.equal bin_annot t.bin_annot
Expand All @@ -141,6 +144,7 @@ let empty_config =
; menhir = Menhir_env.empty
; odoc = Odoc.empty
; js_of_ocaml = Js_of_ocaml.Env.empty
; wasm_of_ocaml = Js_of_ocaml.Env.empty
; coq = Coq_env.default
; format_config = None
; error_on_use = None
Expand Down Expand Up @@ -224,6 +228,13 @@ let js_of_ocaml_field =
(Dune_lang.Syntax.since Stanza.syntax (3, 0) >>> Js_of_ocaml.Env.decode)
;;

let wasm_of_ocaml_field =
field
"wasm_of_ocaml"
~default:Js_of_ocaml.Env.empty
(Dune_lang.Syntax.since Stanza.syntax (3, 17) >>> Js_of_ocaml.Env.decode)
;;

let bin_annot = field_o "bin_annot" (Dune_lang.Syntax.since Stanza.syntax (3, 8) >>> bool)

let config =
Expand All @@ -241,6 +252,7 @@ let config =
and+ menhir_flags = menhir_flags ~since:(2, 1) ~deleted_in:Menhir_stanza.explain_since
and+ odoc = odoc_field
and+ js_of_ocaml = js_of_ocaml_field
and+ wasm_of_ocaml = wasm_of_ocaml_field
and+ coq = Coq_env.decode
and+ format_config = Format_config.field ~since:(2, 8)
and+ bin_annot = bin_annot in
Expand All @@ -261,6 +273,7 @@ let config =
; menhir
; odoc
; js_of_ocaml
; wasm_of_ocaml
; coq
; format_config
; error_on_use = None
Expand Down
1 change: 1 addition & 0 deletions src/dune_rules/dune_env.mli
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ type config =
; menhir : Ordered_set_lang.Unexpanded.t Menhir_env.t
; odoc : Odoc.t
; js_of_ocaml : Ordered_set_lang.Unexpanded.t Js_of_ocaml.Env.t
; wasm_of_ocaml : Ordered_set_lang.Unexpanded.t Js_of_ocaml.Env.t
; coq : Coq_env.t
; format_config : Format_config.t option
; error_on_use : User_message.t option
Expand Down
Loading

0 comments on commit ec321a5

Please sign in to comment.