Skip to content

Commit ec321a5

Browse files
committed
Wasm_of_ocaml support: alternative design
Signed-off-by: Jérôme Vouillon <[email protected]>
1 parent fdbd71b commit ec321a5

File tree

58 files changed

+744
-683
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

58 files changed

+744
-683
lines changed

.github/workflows/workflow.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -258,7 +258,7 @@ jobs:
258258
uses: actions/checkout@v4
259259
with:
260260
repository: ocaml-wasm/wasm_of_ocaml
261-
ref: target-dir
261+
ref: wasm-dune
262262
path: wasm_of_ocaml
263263

264264
- name: Install Wasm_of_ocaml

bin/printenv.ml

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,25 @@ let dump sctx ~dir =
2727
|> Action_builder.of_memo
2828
>>= Dune_rules.Menhir_env.dump
2929
and+ coq_dump = Dune_rules.Coq.Coq_rules.coq_env ~dir >>| Dune_rules.Coq.Coq_flags.dump
30-
and+ jsoo_dump =
30+
and+ jsoo_js_dump =
3131
let module Js_of_ocaml = Dune_rules.Js_of_ocaml in
32-
let* jsoo = Action_builder.of_memo (Dune_rules.Jsoo_rules.jsoo_env ~dir) in
33-
Js_of_ocaml.Flags.dump jsoo.flags
32+
let* jsoo = Action_builder.of_memo (Dune_rules.Jsoo_rules.jsoo_env ~dir ~mode:JS) in
33+
Js_of_ocaml.Flags.dump ~mode:JS jsoo.flags
34+
and+ jsoo_wasm_dump =
35+
let module Js_of_ocaml = Dune_rules.Js_of_ocaml in
36+
let* jsoo = Action_builder.of_memo (Dune_rules.Jsoo_rules.jsoo_env ~dir ~mode:Wasm) in
37+
Js_of_ocaml.Flags.dump ~mode:Wasm jsoo.flags
3438
in
3539
let env =
36-
List.concat [ o_dump; c_dump; link_flags_dump; menhir_dump; coq_dump; jsoo_dump ]
40+
List.concat
41+
[ o_dump
42+
; c_dump
43+
; link_flags_dump
44+
; menhir_dump
45+
; coq_dump
46+
; jsoo_js_dump
47+
; jsoo_wasm_dump
48+
]
3749
in
3850
Super_context.context sctx |> Context.name, env
3951
;;

src/dune_lang/binary_kind.ml

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ type t =
66
| Object
77
| Shared_object
88
| Plugin
9-
| Js
109

1110
let compare x y =
1211
match x, y with
@@ -23,9 +22,6 @@ let compare x y =
2322
| Shared_object, _ -> Lt
2423
| _, Shared_object -> Gt
2524
| Plugin, Plugin -> Eq
26-
| Plugin, _ -> Lt
27-
| _, Plugin -> Gt
28-
| Js, Js -> Eq
2925
;;
3026

3127
let decode =
@@ -37,7 +33,6 @@ let decode =
3733
; "object", return Object
3834
; "shared_object", return Shared_object
3935
; "plugin", Syntax.since Stanza.syntax (2, 4) >>> return Plugin
40-
; "js", Syntax.since Stanza.syntax (1, 11) >>> return Js
4136
]
4237
;;
4338

@@ -47,9 +42,8 @@ let to_string = function
4742
| Object -> "object"
4843
| Shared_object -> "shared_object"
4944
| Plugin -> "plugin"
50-
| Js -> "js"
5145
;;
5246

5347
let to_dyn t = Dyn.variant (to_string t) []
5448
let encode t = Dune_sexp.atom (to_string t)
55-
let all = [ C; Exe; Object; Shared_object; Plugin; Js ]
49+
let all = [ C; Exe; Object; Shared_object; Plugin ]

src/dune_lang/binary_kind.mli

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ type t =
66
| Object
77
| Shared_object
88
| Plugin
9-
| Js
109

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

src/dune_rules/cinaps.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ let gen_rules sctx t ~dir ~scope =
179179
~requires_compile
180180
~requires_link
181181
~flags:(Ocaml_flags.of_list [ "-w"; "-24" ])
182-
~js_of_ocaml:None
182+
~js_of_ocaml:(Js_of_ocaml.Mode.Pair.make None)
183183
~melange_package_name:None
184184
~package:None
185185
in

src/dune_rules/compilation_context.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ type t =
8787
; preprocessing : Pp_spec.t
8888
; opaque : bool
8989
; stdlib : Ocaml_stdlib.t option
90-
; js_of_ocaml : Js_of_ocaml.In_context.t option
90+
; js_of_ocaml : Js_of_ocaml.In_context.t option Js_of_ocaml.Mode.Pair.t
9191
; sandbox : Sandbox_config.t
9292
; package : Package.t option
9393
; vimpl : Vimpl.t option

src/dune_rules/compilation_context.mli

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ val create
3030
-> ?preprocessing:Pp_spec.t
3131
-> opaque:opaque
3232
-> ?stdlib:Ocaml_stdlib.t
33-
-> js_of_ocaml:Js_of_ocaml.In_context.t option
33+
-> js_of_ocaml:Js_of_ocaml.In_context.t option Js_of_ocaml.Mode.Pair.t
3434
-> package:Package.t option
3535
-> melange_package_name:Lib_name.t option
3636
-> ?vimpl:Vimpl.t
@@ -61,7 +61,7 @@ val includes : t -> Command.Args.without_targets Command.Args.t Lib_mode.Cm_kind
6161
val preprocessing : t -> Pp_spec.t
6262
val opaque : t -> bool
6363
val stdlib : t -> Ocaml_stdlib.t option
64-
val js_of_ocaml : t -> Js_of_ocaml.In_context.t option
64+
val js_of_ocaml : t -> Js_of_ocaml.In_context.t option Js_of_ocaml.Mode.Pair.t
6565
val sandbox : t -> Sandbox_config.t
6666
val set_sandbox : t -> Sandbox_config.t -> t
6767
val package : t -> Package.t option

src/dune_rules/dir_status.ml

Lines changed: 20 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -134,36 +134,25 @@ let directory_targets_of_rule ~dir { Rule_conf.targets; loc = rule_loc; enabled_
134134
when_enabled ~dir ~enabled_if directory_targets
135135
;;
136136

137-
let jsoo_wasm_enabled
138-
~(jsoo_submodes :
139-
dir:Path.Build.t
140-
-> submodes:Js_of_ocaml.Submode.Set.t option
141-
-> Js_of_ocaml.Submode.t list Memo.t)
142-
~dir
143-
~submodes
144-
=
145-
let+ submodes = jsoo_submodes ~dir ~submodes in
146-
List.mem ~equal:Js_of_ocaml.Submode.equal submodes Wasm
137+
let jsoo_wasm_enabled ~jsoo_enabled ~dir ~(buildable : Buildable.t) =
138+
let* expander = Expander0.get ~dir in
139+
jsoo_enabled
140+
~eval:(Expander0.eval_blang expander)
141+
~dir
142+
~in_context:(Js_of_ocaml.In_context.make ~dir buildable.js_of_ocaml)
143+
~mode:Js_of_ocaml.Mode.Wasm
147144
;;
148145

149146
let directory_targets_of_executables
150-
~jsoo_submodes
147+
~jsoo_enabled
151148
~dir
152149
{ Executables.names; modes; enabled_if; buildable; _ }
153150
=
154151
let* directory_targets =
155152
(* CR-someday rgrinberg: we don't necessarily need to evalute
156-
[explicit_js_mode] or [wasm_enabled] here *)
157-
let+ wasm_enabled =
158-
jsoo_wasm_enabled ~jsoo_submodes ~dir ~submodes:buildable.js_of_ocaml.submodes
159-
and+ explicit_js_mode =
160-
Scope.DB.find_by_dir dir >>| Scope.project >>| Dune_project.explicit_js_mode
161-
in
162-
match
163-
Executables.Link_mode.(
164-
Map.mem modes js || ((not explicit_js_mode) && Map.mem modes byte))
165-
&& wasm_enabled
166-
with
153+
[wasm_enabled] here *)
154+
let+ wasm_enabled = jsoo_wasm_enabled ~jsoo_enabled ~dir ~buildable in
155+
match Executables.Link_mode.(Map.mem modes wasm) && wasm_enabled with
167156
| false -> Path.Build.Map.empty
168157
| true ->
169158
Nonempty_list.to_list names
@@ -175,15 +164,15 @@ let directory_targets_of_executables
175164
;;
176165

177166
let directory_targets_of_library
178-
~jsoo_submodes
167+
~jsoo_enabled
179168
~dir
180169
{ Library.sub_systems; name; enabled_if; buildable; _ }
181170
=
182171
let* directory_targets =
183172
match Sub_system_name.Map.find sub_systems Inline_tests_info.Tests.name with
184173
| Some (Inline_tests_info.Tests.T { modes; loc; enabled_if; _ })
185-
when Inline_tests_info.Mode_conf.Set.mem modes Javascript ->
186-
jsoo_wasm_enabled ~jsoo_submodes ~dir ~submodes:buildable.js_of_ocaml.submodes
174+
when Inline_tests_info.Mode_conf.Set.mem modes (Jsoo Wasm) ->
175+
jsoo_wasm_enabled ~jsoo_enabled ~dir ~buildable
187176
>>| (function
188177
| false -> Path.Build.Map.empty
189178
| true ->
@@ -207,13 +196,13 @@ let directory_targets_of_library
207196
when_enabled ~dir ~enabled_if directory_targets
208197
;;
209198

210-
let extract_directory_targets ~jsoo_submodes ~dir stanzas =
199+
let extract_directory_targets ~jsoo_enabled ~dir stanzas =
211200
Memo.parallel_map stanzas ~f:(fun stanza ->
212201
match Stanza.repr stanza with
213202
| Rule_conf.T rule -> directory_targets_of_rule ~dir rule
214203
| Executables.T exes | Tests.T { exes; _ } ->
215-
directory_targets_of_executables ~jsoo_submodes ~dir exes
216-
| Library.T lib -> directory_targets_of_library ~jsoo_submodes ~dir lib
204+
directory_targets_of_executables ~jsoo_enabled ~dir exes
205+
| Library.T lib -> directory_targets_of_library ~jsoo_enabled ~dir lib
217206
| Coq_stanza.Theory.T m ->
218207
(* It's unfortunate that we need to pull in the coq rules here. But
219208
we don't have a generic mechanism for this yet. *)
@@ -360,15 +349,15 @@ end = struct
360349
;;
361350
end
362351

363-
let directory_targets t ~jsoo_submodes ~dir =
352+
let directory_targets t ~jsoo_enabled ~dir =
364353
match t with
365354
| Lock_dir | Generated | Source_only _ | Is_component_of_a_group_but_not_the_root _ ->
366355
Memo.return Path.Build.Map.empty
367356
| Standalone (_, dune_file) ->
368-
Dune_file.stanzas dune_file >>= extract_directory_targets ~jsoo_submodes ~dir
357+
Dune_file.stanzas dune_file >>= extract_directory_targets ~jsoo_enabled ~dir
369358
| Group_root { components; dune_file; _ } ->
370359
let f ~dir stanzas acc =
371-
extract_directory_targets ~jsoo_submodes ~dir stanzas
360+
extract_directory_targets ~jsoo_enabled ~dir stanzas
372361
>>| Path.Build.Map.superpose acc
373362
in
374363
let* init =

src/dune_rules/dir_status.mli

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,11 @@ end
4444

4545
val directory_targets
4646
: t
47-
-> jsoo_submodes:
48-
(dir:Path.Build.t
49-
-> submodes:Js_of_ocaml.Submode.Set.t option
50-
-> Js_of_ocaml.Submode.t list Memo.t)
47+
-> jsoo_enabled:
48+
(eval:(Blang.t -> bool Memo.t)
49+
-> dir:Path.Build.t
50+
-> in_context:Js_of_ocaml.In_context.t Js_of_ocaml.Mode.Pair.t
51+
-> mode:Js_of_ocaml.Mode.t
52+
-> bool Memo.t)
5153
-> dir:Path.Build.t
5254
-> Loc.t Path.Build.Map.t Memo.t

src/dune_rules/dune_env.ml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ type config =
8080
; menhir : Ordered_set_lang.Unexpanded.t Menhir_env.t
8181
; odoc : Odoc.t
8282
; js_of_ocaml : Ordered_set_lang.Unexpanded.t Js_of_ocaml.Env.t
83+
; wasm_of_ocaml : Ordered_set_lang.Unexpanded.t Js_of_ocaml.Env.t
8384
; coq : Coq_env.t
8485
; format_config : Format_config.t option
8586
; error_on_use : User_message.t option
@@ -102,6 +103,7 @@ let equal_config
102103
; menhir
103104
; odoc
104105
; js_of_ocaml
106+
; wasm_of_ocaml
105107
; coq
106108
; format_config
107109
; error_on_use
@@ -124,6 +126,7 @@ let equal_config
124126
&& Coq_env.equal coq t.coq
125127
&& Option.equal Format_config.equal format_config t.format_config
126128
&& Js_of_ocaml.Env.equal js_of_ocaml t.js_of_ocaml
129+
&& Js_of_ocaml.Env.equal wasm_of_ocaml t.wasm_of_ocaml
127130
&& Option.equal User_message.equal error_on_use t.error_on_use
128131
&& Option.equal User_message.equal warn_on_load t.warn_on_load
129132
&& Option.equal Bool.equal bin_annot t.bin_annot
@@ -141,6 +144,7 @@ let empty_config =
141144
; menhir = Menhir_env.empty
142145
; odoc = Odoc.empty
143146
; js_of_ocaml = Js_of_ocaml.Env.empty
147+
; wasm_of_ocaml = Js_of_ocaml.Env.empty
144148
; coq = Coq_env.default
145149
; format_config = None
146150
; error_on_use = None
@@ -224,6 +228,13 @@ let js_of_ocaml_field =
224228
(Dune_lang.Syntax.since Stanza.syntax (3, 0) >>> Js_of_ocaml.Env.decode)
225229
;;
226230

231+
let wasm_of_ocaml_field =
232+
field
233+
"wasm_of_ocaml"
234+
~default:Js_of_ocaml.Env.empty
235+
(Dune_lang.Syntax.since Stanza.syntax (3, 17) >>> Js_of_ocaml.Env.decode)
236+
;;
237+
227238
let bin_annot = field_o "bin_annot" (Dune_lang.Syntax.since Stanza.syntax (3, 8) >>> bool)
228239

229240
let config =
@@ -241,6 +252,7 @@ let config =
241252
and+ menhir_flags = menhir_flags ~since:(2, 1) ~deleted_in:Menhir_stanza.explain_since
242253
and+ odoc = odoc_field
243254
and+ js_of_ocaml = js_of_ocaml_field
255+
and+ wasm_of_ocaml = wasm_of_ocaml_field
244256
and+ coq = Coq_env.decode
245257
and+ format_config = Format_config.field ~since:(2, 8)
246258
and+ bin_annot = bin_annot in
@@ -261,6 +273,7 @@ let config =
261273
; menhir
262274
; odoc
263275
; js_of_ocaml
276+
; wasm_of_ocaml
264277
; coq
265278
; format_config
266279
; error_on_use = None

0 commit comments

Comments
 (0)