Skip to content

Commit 0153f76

Browse files
authored
Jsoo shapes (#10767)
* Add support for jsoo shapes Signed-off-by: Hugo Heuzard <[email protected]> * read shapes for cmo.js Signed-off-by: Hugo Heuzard <[email protected]> * accept diff Signed-off-by: Hugo Heuzard <[email protected]> --------- Signed-off-by: Hugo Heuzard <[email protected]>
1 parent 4892ee4 commit 0153f76

File tree

8 files changed

+106
-28
lines changed

8 files changed

+106
-28
lines changed

src/dune_rules/jsoo/jsoo_rules.ml

Lines changed: 84 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -316,6 +316,15 @@ let wasmoo ~dir sctx =
316316
"wasm_of_ocaml"
317317
;;
318318

319+
let jsoo_has_shapes jsoo_version =
320+
match jsoo_version with
321+
| Some version ->
322+
(match Version.compare version (6, 1) with
323+
| Lt -> false
324+
| Gt | Eq -> true)
325+
| None -> false
326+
;;
327+
319328
type sub_command =
320329
| Compile
321330
| Link
@@ -522,6 +531,12 @@ let jsoo_archives ~mode ctx config lib =
522531
]))
523532
;;
524533

534+
let cmo_js_of_module ~mode m =
535+
Module_name.Unique.artifact_filename
536+
(Module.obj_name m)
537+
~ext:(Js_of_ocaml.Ext.cmo ~mode)
538+
;;
539+
525540
let link_rule
526541
~mode
527542
cc
@@ -537,11 +552,6 @@ let link_rule
537552
=
538553
let sctx = Compilation_context.super_context cc in
539554
let dir = Compilation_context.dir cc in
540-
let mod_name m =
541-
Module_name.Unique.artifact_filename
542-
(Module.obj_name m)
543-
~ext:(Js_of_ocaml.Ext.cmo ~mode)
544-
in
545555
let ctx = Super_context.context sctx |> Context.build_context in
546556
let get_all =
547557
let open Action_builder.O in
@@ -567,12 +577,13 @@ let link_rule
567577
let special_units =
568578
List.concat_map to_link ~f:(function
569579
| Lib_flags.Lib_and_module.Lib _lib -> []
570-
| Module (obj_dir, m) -> [ in_obj_dir' ~obj_dir ~config:None [ mod_name m ] ])
580+
| Module (obj_dir, m) ->
581+
[ in_obj_dir' ~obj_dir ~config:None [ cmo_js_of_module ~mode m ] ])
571582
in
572583
let all_libs = List.concat_map libs ~f:(jsoo_archives ~mode ctx config) in
573584
let all_other_modules =
574585
List.map cm ~f:(fun m ->
575-
Path.build (in_obj_dir ~obj_dir ~config:None [ mod_name m ]))
586+
Path.build (in_obj_dir ~obj_dir ~config:None [ cmo_js_of_module ~mode m ]))
576587
in
577588
let std_exit =
578589
Path.build
@@ -606,8 +617,25 @@ let link_rule
606617
~sourcemap
607618
;;
608619

609-
let build_cm' sctx ~dir ~in_context ~mode ~src ~target ~config ~sourcemap =
610-
let spec = Command.Args.Dep src in
620+
let build_cm' sctx ~dir ~in_context ~mode ~src ~target ~config ~shapes ~sourcemap =
621+
let spec =
622+
Command.Args.(
623+
S
624+
[ Dep src
625+
; Dyn
626+
(let open Action_builder.O in
627+
let* jsoo_version =
628+
let* jsoo = jsoo ~dir sctx in
629+
Action_builder.of_memo @@ Version.jsoo_version jsoo
630+
in
631+
let+ shapes =
632+
match jsoo_has_shapes jsoo_version with
633+
| false -> Action_builder.return []
634+
| true -> shapes
635+
in
636+
S (List.map shapes ~f:(fun s -> S [ A "--load-shape"; Dep s ])))
637+
])
638+
in
611639
let flags = in_context.Js_of_ocaml.In_context.flags in
612640
js_of_ocaml_rule
613641
sctx
@@ -622,17 +650,38 @@ let build_cm' sctx ~dir ~in_context ~mode ~src ~target ~config ~sourcemap =
622650
~sourcemap
623651
;;
624652

625-
let build_cm sctx ~dir ~in_context ~mode ~src ~obj_dir ~config =
653+
let build_cm cctx ~dir ~in_context ~mode ~src ~obj_dir ~deps ~config:config_opt =
626654
let name = with_js_ext ~mode (Path.basename src) in
627-
let target = in_obj_dir ~obj_dir ~config [ name ] in
655+
let target = in_obj_dir ~obj_dir ~config:config_opt [ name ] in
656+
let sctx = Compilation_context.super_context cctx in
657+
let ctx = Super_context.context sctx |> Context.build_context in
658+
let shapes =
659+
let open Action_builder.O in
660+
let+ libs = Resolve.Memo.read (Compilation_context.requires_link cctx)
661+
and+ deps = deps
662+
and+ config =
663+
match config_opt with
664+
| None ->
665+
let flags = in_context.Js_of_ocaml.In_context.flags in
666+
js_of_ocaml_flags sctx ~dir ~mode flags
667+
|> Action_builder.bind ~f:(fun (x : _ Js_of_ocaml.Flags.t) -> x.compile)
668+
|> Action_builder.map ~f:Config.of_flags
669+
| Some config -> Action_builder.return config
670+
in
671+
(Path.build (in_build_dir ctx ~config [ "stdlib"; with_js_ext ~mode "stdlib.cma" ])
672+
:: List.concat_map libs ~f:(fun lib -> jsoo_archives ~mode ctx config lib))
673+
@ List.map deps ~f:(fun m ->
674+
Path.build (in_obj_dir ~obj_dir ~config:config_opt [ cmo_js_of_module ~mode m ]))
675+
in
628676
build_cm'
629677
sctx
630678
~dir
631679
~in_context
632680
~mode
633681
~src
634682
~target
635-
~config:(Option.map config ~f:Action_builder.return)
683+
~shapes
684+
~config:(Option.map config_opt ~f:Action_builder.return)
636685
~sourcemap:Js_of_ocaml.Sourcemap.Inline
637686
;;
638687

@@ -649,6 +698,11 @@ let setup_separate_compilation_rules sctx components =
649698
| None -> Memo.return ()
650699
| Some pkg ->
651700
let info = Lib.info pkg in
701+
let requires =
702+
let open Resolve.Memo.O in
703+
let* reqs = Lib.requires pkg in
704+
Lib.closure ~linking:false reqs
705+
in
652706
let lib_name = Lib_name.to_string (Lib.name pkg) in
653707
let* archives =
654708
let archives = (Lib_info.archives info).byte in
@@ -679,6 +733,23 @@ let setup_separate_compilation_rules sctx components =
679733
let target =
680734
in_build_dir build_context ~config [ lib_name; with_js_ext ~mode name ]
681735
in
736+
let shapes =
737+
let open Action_builder.O in
738+
let+ requires = Resolve.Memo.read requires in
739+
let l =
740+
List.concat_map requires ~f:(fun lib ->
741+
jsoo_archives ~mode build_context config lib)
742+
in
743+
match lib_name with
744+
| "stdlib" -> l
745+
| _ ->
746+
Path.build
747+
(in_build_dir
748+
build_context
749+
~config
750+
[ "stdlib"; with_js_ext ~mode "stdlib.cma" ])
751+
:: l
752+
in
682753
build_cm'
683754
sctx
684755
~dir
@@ -688,6 +759,7 @@ let setup_separate_compilation_rules sctx components =
688759
~target
689760
~config:(Some (Action_builder.return config))
690761
~sourcemap:Js_of_ocaml.Sourcemap.Inline
762+
~shapes
691763
|> Super_context.add_rule sctx ~dir)))
692764
;;
693765

src/dune_rules/jsoo/jsoo_rules.mli

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,13 @@ module Version : sig
1616
end
1717

1818
val build_cm
19-
: Super_context.t
19+
: Compilation_context.t
2020
-> dir:Path.Build.t
2121
-> in_context:Js_of_ocaml.In_context.t
2222
-> mode:Js_of_ocaml.Mode.t
2323
-> src:Path.t
2424
-> obj_dir:Path.Build.t Obj_dir.t
25+
-> deps:Module.t list Action_builder.t
2526
-> config:Config.t option
2627
-> Action.Full.t Action_builder.With_targets.t
2728

src/dune_rules/lib_rules.ml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -459,14 +459,15 @@ let setup_build_archives (lib : Library.t) ~top_sorted_modules ~cctx ~expander ~
459459
let action_with_targets =
460460
List.map Jsoo_rules.Config.all ~f:(fun config ->
461461
Jsoo_rules.build_cm
462-
sctx
462+
cctx
463463
~dir
464464
~in_context:
465465
(Js_of_ocaml.In_context.make ~dir lib.buildable.js_of_ocaml
466466
|> Js_of_ocaml.Mode.Pair.select ~mode)
467467
~mode
468468
~config:(Some config)
469469
~src:(Path.build src)
470+
~deps:(Action_builder.return [])
470471
~obj_dir)
471472
in
472473
Memo.parallel_iter action_with_targets ~f:(fun rule ->

src/dune_rules/module_compilation.ml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -311,6 +311,9 @@ let build_module ?(force_write_cmi = false) ?(precompiled_cmi = false) cctx m =
311311
match Obj_dir.Module.cm_file obj_dir m ~kind:(Ocaml Cmo) with
312312
| None -> Memo.return ()
313313
| Some src ->
314+
let ml_kind = Ml_kind.Impl in
315+
let dep_graph = Ml_kind.Dict.get (Compilation_context.dep_graphs cctx) ml_kind in
316+
let module_deps = Dep_graph.deps_of dep_graph m in
314317
Memo.parallel_iter Js_of_ocaml.Mode.all ~f:(fun mode ->
315318
Compilation_context.js_of_ocaml cctx
316319
|> Js_of_ocaml.Mode.Pair.select ~mode
@@ -320,12 +323,13 @@ let build_module ?(force_write_cmi = false) ?(precompiled_cmi = false) cctx m =
320323
let dir = Compilation_context.dir cctx in
321324
let action_with_targets =
322325
Jsoo_rules.build_cm
323-
sctx
326+
cctx
324327
~dir
325328
~in_context
326329
~mode
327330
~src:(Path.build src)
328331
~obj_dir
332+
~deps:module_deps
329333
~config:None
330334
in
331335
Super_context.add_rule sctx ~dir action_with_targets)))

test/blackbox-tests/test-cases/jsoo/explicit-js-mode-specified.t/run.t

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,12 @@ specify js mode (#1940).
2525
$ dune build --display short @@all 2>&1 | grep js_of_ocaml
2626
js_of_ocaml .b.eobjs/jsoo/b.bc.runtime.js
2727
js_of_ocaml .e.eobjs/jsoo/e.bc.runtime.js
28-
js_of_ocaml .js/default/stdlib/std_exit.cmo.js
2928
js_of_ocaml .js/default/stdlib/stdlib.cma.js
29+
js_of_ocaml .js/default/stdlib/std_exit.cmo.js
3030
js_of_ocaml .b.eobjs/jsoo/b.cmo.js
3131
js_of_ocaml b.bc.js
32-
js_of_ocaml .e.eobjs/jsoo/e.cmo.js
3332
js_of_ocaml .foo.objs/jsoo/default/foo.cma.js
33+
js_of_ocaml .e.eobjs/jsoo/e.cmo.js
3434
js_of_ocaml e.bc.js
3535

3636
Check that building a JS-enabled executable that depends on a library works.

test/blackbox-tests/test-cases/jsoo/inline-tests.t/run.t

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@ Run inline tests using node js
99
$ dune runtest
1010
inline tests (Byte)
1111
inline tests (Byte)
12-
Warning [missing-effects-backend]: your program contains effect handlers; you should probably run js_of_ocaml with option '--effects=cps'
1312
inline tests (Native)
1413
inline tests (Native)
14+
Warning [missing-effects-backend]: your program contains effect handlers; you should probably run js_of_ocaml with option '--effects=cps'
1515
inline tests (JS)
1616
inline tests (JS)
1717

test/blackbox-tests/test-cases/jsoo/no-check-prim.t/run.t

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,20 +10,20 @@ Compilation using jsoo
1010
ocamldep bin/.technologic.eobjs/z.impl.d
1111
ocamlopt lib/.x.objs/native/x__.{cmx,o}
1212
ocamlc lib/.x.objs/byte/x__Y.{cmi,cmo,cmt}
13-
js_of_ocaml .js/default/js_of_ocaml-compiler.runtime/jsoo_runtime.cma.js
14-
js_of_ocaml .js/default/js_of_ocaml/js_of_ocaml.cma.js
15-
js_of_ocaml .js/default/stdlib/std_exit.cmo.js
1613
js_of_ocaml .js/default/stdlib/stdlib.cma.js
14+
js_of_ocaml .js/default/stdlib/std_exit.cmo.js
1715
ocamlopt lib/.x.objs/native/x__Y.{cmx,o}
1816
ocamlc lib/.x.objs/byte/x.{cmi,cmo,cmt}
17+
js_of_ocaml .js/default/js_of_ocaml-compiler.runtime/jsoo_runtime.cma.js
1918
ocamlopt lib/.x.objs/native/x.{cmx,o}
2019
ocamlc bin/.technologic.eobjs/byte/z.{cmi,cmo,cmt}
2120
ocamlc lib/x.cma
21+
js_of_ocaml .js/default/js_of_ocaml/js_of_ocaml.cma.js
2222
ocamlopt lib/x.{a,cmxa}
2323
ocamlc bin/.technologic.eobjs/byte/technologic.{cmi,cmo,cmt}
24-
js_of_ocaml bin/.technologic.eobjs/jsoo/z.cmo.js
2524
js_of_ocaml lib/.x.objs/jsoo/default/x.cma.js
2625
ocamlopt lib/x.cmxs
26+
js_of_ocaml bin/.technologic.eobjs/jsoo/z.cmo.js
2727
js_of_ocaml bin/.technologic.eobjs/jsoo/technologic.cmo.js
2828
js_of_ocaml bin/technologic.bc.js
2929
$ node ./_build/default/bin/technologic.bc.js

test/blackbox-tests/test-cases/wasmoo/no-check-prim.t/run.t

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,24 +10,24 @@ Compilation using WasmOO
1010
ocamldep bin/.technologic.eobjs/dune__exe__Z.impl.d
1111
ocamlopt lib/.x.objs/native/x__.{cmx,o}
1212
ocamlc lib/.x.objs/byte/x__Y.{cmi,cmo,cmt}
13-
wasm_of_ocaml .js/default/js_of_ocaml-compiler.runtime/jsoo_runtime.wasma
14-
wasm_of_ocaml .js/default/js_of_ocaml/js_of_ocaml.wasma
15-
wasm_of_ocaml .js/default/stdlib/std_exit.wasmo
1613
wasm_of_ocaml .js/default/stdlib/stdlib.wasma
14+
wasm_of_ocaml .js/default/stdlib/std_exit.wasmo
1715
ocamlc bin/.technologic.eobjs/byte/dune__exe.{cmi,cmo,cmt}
1816
ocamldep bin/.technologic.eobjs/dune__exe__Technologic.intf.d
1917
ocamlopt lib/.x.objs/native/x__Y.{cmx,o}
2018
ocamlc lib/.x.objs/byte/x.{cmi,cmo,cmt}
21-
wasm_of_ocaml bin/.technologic.eobjs/jsoo/dune__exe.wasmo
19+
wasm_of_ocaml .js/default/js_of_ocaml-compiler.runtime/jsoo_runtime.wasma
2220
ocamlopt lib/.x.objs/native/x.{cmx,o}
2321
ocamlc bin/.technologic.eobjs/byte/dune__exe__Technologic.{cmi,cmti}
2422
ocamlc lib/x.cma
2523
ocamlc bin/.technologic.eobjs/byte/dune__exe__Z.{cmi,cmo,cmt}
24+
wasm_of_ocaml .js/default/js_of_ocaml/js_of_ocaml.wasma
2625
ocamlopt lib/x.{a,cmxa}
27-
wasm_of_ocaml lib/.x.objs/jsoo/default/x.wasma
2826
ocamlc bin/.technologic.eobjs/byte/dune__exe__Technologic.{cmo,cmt}
29-
wasm_of_ocaml bin/.technologic.eobjs/jsoo/dune__exe__Z.wasmo
27+
wasm_of_ocaml lib/.x.objs/jsoo/default/x.wasma
3028
ocamlopt lib/x.cmxs
29+
wasm_of_ocaml bin/.technologic.eobjs/jsoo/dune__exe.wasmo
30+
wasm_of_ocaml bin/.technologic.eobjs/jsoo/dune__exe__Z.wasmo
3131
wasm_of_ocaml bin/.technologic.eobjs/jsoo/dune__exe__Technologic.wasmo
3232
wasm_of_ocaml bin/technologic.bc.wasm.{js,assets}
3333
$ node ./_build/default/bin/technologic.bc.wasm.js

0 commit comments

Comments
 (0)