Skip to content

Commit

Permalink
Introduce jsoo shapes
Browse files Browse the repository at this point in the history
  • Loading branch information
hhugo committed Nov 7, 2024
1 parent 619c098 commit aa79af2
Show file tree
Hide file tree
Showing 8 changed files with 109 additions and 23 deletions.
1 change: 1 addition & 0 deletions src/dune_rules/jsoo/js_of_ocaml.ml
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,7 @@ module Ext = struct
let exe ~mode = Mode.select ~mode ~js:".bc.js" ~wasm:".bc.wasm.js"
let cmo ~mode = Mode.select ~mode ~js:".cmo.js" ~wasm:".wasmo"
let cma ~mode = Mode.select ~mode ~js:".cma.js" ~wasm:".wasma"
let js_shape = ".jsoo-shape"
let runtime ~mode = Mode.select ~mode ~js:".bc.runtime.js" ~wasm:".bc.runtime.wasma"
let wasm_dir = ".bc.wasm.assets"
end
Expand Down
1 change: 1 addition & 0 deletions src/dune_rules/jsoo/js_of_ocaml.mli
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ module Ext : sig
val cma : mode:Mode.t -> t
val runtime : mode:Mode.t -> t
val wasm_dir : t
val js_shape : t
end

module Env : sig
Expand Down
95 changes: 89 additions & 6 deletions src/dune_rules/jsoo/jsoo_rules.ml
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,27 @@ let wasmoo ~dir sctx =
Super_context.resolve_program sctx ~dir ~loc:None "wasm_of_ocaml"
;;

let jsoo_memo ~dir sctx =
Super_context.resolve_program_memo
sctx
~dir
~loc:None
~where:Original_path
~hint:install_jsoo_hint
"js_of_ocaml"
;;

let jsoo_has_shapes ~dir sctx =
let* jsoo = jsoo_memo ~dir sctx in
let+ jsoo_version = Version.jsoo_version jsoo in
match jsoo_version with
| Some version ->
(match Version.compare version (5, 8) with
| Lt -> false
| Gt | Eq -> true)
| None -> false
;;

type sub_command =
| Compile
| Link
Expand Down Expand Up @@ -477,8 +498,26 @@ let link_rule
~config:None
;;

let build_cm' sctx ~dir ~in_context ~mode ~src ~target ~config ~sourcemap =
let spec = Command.Args.Dep src in
let build_cm' sctx ~dir ~in_context ~mode ~src ~target ~config ~shapes ~sourcemap =
let spec =
Command.Args.(
S
[ Dep src
; (match shapes, (mode : Js_of_ocaml.Mode.t) with
| Some shapes, JS ->
S
[ A "--shapes"
; Hidden_targets
[ Path.Build.set_extension target ~ext:Js_of_ocaml.Ext.js_shape ]
; Dyn
(let open Action_builder.O in
let+ shapes = shapes in
S (List.map shapes ~f:(fun s -> S [ A "--load"; Dep s ])))
]
| Some _, Wasm -> S []
| None, _ -> S [])
])
in
let flags = in_context.Js_of_ocaml.In_context.flags in
js_of_ocaml_rule
sctx
Expand All @@ -493,17 +532,51 @@ let build_cm' sctx ~dir ~in_context ~mode ~src ~target ~config ~sourcemap =
~sourcemap
;;

let build_cm sctx ~dir ~in_context ~mode ~src ~obj_dir ~config =
let build_cm cctx ~dir ~in_context ~mode ~src ~obj_dir ~config:config_opt =
let name = with_js_ext ~mode (Path.basename src) in
let target = in_obj_dir ~obj_dir ~config [ name ] in
let target = in_obj_dir ~obj_dir ~config:config_opt [ name ] in
let sctx = Compilation_context.super_context cctx in
let ctx = Super_context.context sctx |> Context.build_context in
let+ jsoo_has_shapes =
match mode with
| JS -> jsoo_has_shapes ~dir sctx
| Wasm -> Memo.return false
in
let shapes =
if jsoo_has_shapes
then
Some
(let open Action_builder.O in
let+ libs = Resolve.Memo.read (Compilation_context.requires_link cctx)
and+ config =
match config_opt with
| None ->
let flags = in_context.Js_of_ocaml.In_context.flags in
js_of_ocaml_flags sctx ~dir ~mode flags
|> Action_builder.bind ~f:(fun (x : _ Js_of_ocaml.Flags.t) -> x.compile)
|> Action_builder.map ~f:Config.of_flags
| Some config -> Action_builder.return config
in
Path.build
(in_build_dir
ctx
~config
[ "stdlib"; "stdlib.cma" ^ Js_of_ocaml.Ext.js_shape ])
:: List.concat_map libs ~f:(fun lib ->
List.map
(jsoo_archives ~mode ctx config lib)
~f:(Path.set_extension ~ext:Js_of_ocaml.Ext.js_shape)))
else None
in
build_cm'
sctx
~dir
~in_context
~mode
~src
~target
~config:(Option.map config ~f:Action_builder.return)
~shapes
~config:(Option.map config_opt ~f:Action_builder.return)
~sourcemap:Js_of_ocaml.Sourcemap.Inline
;;

Expand All @@ -514,7 +587,10 @@ let setup_separate_compilation_rules sctx components =
let config = Config.of_string s_config in
let pkg = Lib_name.parse_string_exn (Loc.none, s_pkg) in
let ctx = Super_context.context sctx in
let* installed_libs = Lib.DB.installed ctx in
let* installed_libs = Lib.DB.installed ctx
and* jsoo_has_shapes =
jsoo_has_shapes ~dir:(Context.build_context ctx).build_dir sctx
in
Lib.DB.find installed_libs pkg
>>= (function
| None -> Memo.return ()
Expand Down Expand Up @@ -546,6 +622,12 @@ let setup_separate_compilation_rules sctx components =
let target =
in_build_dir build_context ~config [ lib_name; with_js_ext ~mode name ]
in
let shapes =
if jsoo_has_shapes
then (* FIXME: we should load shapes *)
Some (Action_builder.return [])
else None
in
build_cm'
sctx
~dir
Expand All @@ -555,6 +637,7 @@ let setup_separate_compilation_rules sctx components =
~target
~config:(Some (Action_builder.return config))
~sourcemap:Js_of_ocaml.Sourcemap.Inline
~shapes
|> Super_context.add_rule sctx ~dir)))
;;

Expand Down
4 changes: 2 additions & 2 deletions src/dune_rules/jsoo/jsoo_rules.mli
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,14 @@ module Version : sig
end

val build_cm
: Super_context.t
: Compilation_context.t
-> dir:Path.Build.t
-> in_context:Js_of_ocaml.In_context.t
-> mode:Js_of_ocaml.Mode.t
-> src:Path.t
-> obj_dir:Path.Build.t Obj_dir.t
-> config:Config.t option
-> Action.Full.t Action_builder.With_targets.t
-> Action.Full.t Action_builder.With_targets.t Memo.t

val build_exe
: Compilation_context.t
Expand Down
3 changes: 2 additions & 1 deletion src/dune_rules/lib_rules.ml
Original file line number Diff line number Diff line change
Expand Up @@ -478,7 +478,7 @@ let setup_build_archives (lib : Library.t) ~top_sorted_modules ~cctx ~expander ~
let action_with_targets =
List.map Jsoo_rules.Config.all ~f:(fun config ->
Jsoo_rules.build_cm
sctx
cctx
~dir
~in_context:(Js_of_ocaml.Mode.Pair.select ~mode js_of_ocaml)
~mode
Expand All @@ -487,6 +487,7 @@ let setup_build_archives (lib : Library.t) ~top_sorted_modules ~cctx ~expander ~
~obj_dir)
in
Memo.parallel_iter action_with_targets ~f:(fun rule ->
let* rule = rule in
Super_context.add_rule sctx ~dir ~loc:lib.buildable.loc rule)))
in
Memo.when_
Expand Down
4 changes: 2 additions & 2 deletions src/dune_rules/module_compilation.ml
Original file line number Diff line number Diff line change
Expand Up @@ -318,9 +318,9 @@ let build_module ?(force_write_cmi = false) ?(precompiled_cmi = false) cctx m =
(* Build *.cmo.js / *.wasmo *)
let sctx = Compilation_context.super_context cctx in
let dir = Compilation_context.dir cctx in
let action_with_targets =
let* action_with_targets =
Jsoo_rules.build_cm
sctx
cctx
~dir
~in_context
~mode
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,12 @@ specify js mode (#1940).
$ dune build --display short @@all 2>&1 | grep js_of_ocaml
js_of_ocaml .b.eobjs/jsoo/b.bc.runtime.js
js_of_ocaml .e.eobjs/jsoo/e.bc.runtime.js
js_of_ocaml .js/default/stdlib/std_exit.cmo.js
js_of_ocaml .js/default/stdlib/stdlib.cma.js
js_of_ocaml .b.eobjs/jsoo/b.cmo.js
js_of_ocaml .js/default/stdlib/stdlib.cma.{js,jsoo-shape}
js_of_ocaml .js/default/stdlib/std_exit.cmo.{js,jsoo-shape}
js_of_ocaml .b.eobjs/jsoo/b.cmo.{js,jsoo-shape}
js_of_ocaml b.bc.js
js_of_ocaml .e.eobjs/jsoo/e.cmo.js
js_of_ocaml .foo.objs/jsoo/default/foo.cma.js
js_of_ocaml .foo.objs/jsoo/default/foo.cma.{js,jsoo-shape}
js_of_ocaml .e.eobjs/jsoo/e.cmo.{js,jsoo-shape}
js_of_ocaml e.bc.js

Check that building a JS-enabled executable that depends on a library works.
Expand Down
14 changes: 7 additions & 7 deletions test/blackbox-tests/test-cases/jsoo/no-check-prim.t/run.t
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,21 @@ Compilation using jsoo
ocamldep bin/.technologic.eobjs/z.impl.d
ocamlopt lib/.x.objs/native/x__.{cmx,o}
ocamlc lib/.x.objs/byte/x__Y.{cmi,cmo,cmt}
js_of_ocaml .js/default/js_of_ocaml-compiler.runtime/jsoo_runtime.cma.js
js_of_ocaml .js/default/js_of_ocaml/js_of_ocaml.cma.js
js_of_ocaml .js/default/stdlib/std_exit.cmo.js
js_of_ocaml .js/default/stdlib/stdlib.cma.js
js_of_ocaml .js/default/js_of_ocaml-compiler.runtime/jsoo_runtime.cma.{js,jsoo-shape}
js_of_ocaml .js/default/js_of_ocaml/js_of_ocaml.cma.{js,jsoo-shape}
js_of_ocaml .js/default/stdlib/std_exit.cmo.{js,jsoo-shape}
js_of_ocaml .js/default/stdlib/stdlib.cma.{js,jsoo-shape}
ocamlopt lib/.x.objs/native/x__Y.{cmx,o}
ocamlc lib/.x.objs/byte/x.{cmi,cmo,cmt}
ocamlopt lib/.x.objs/native/x.{cmx,o}
ocamlc bin/.technologic.eobjs/byte/z.{cmi,cmo,cmt}
ocamlc lib/x.cma
ocamlopt lib/x.{a,cmxa}
ocamlc bin/.technologic.eobjs/byte/technologic.{cmi,cmo,cmt}
js_of_ocaml bin/.technologic.eobjs/jsoo/z.cmo.js
js_of_ocaml lib/.x.objs/jsoo/default/x.cma.js
js_of_ocaml lib/.x.objs/jsoo/default/x.cma.{js,jsoo-shape}
ocamlopt lib/x.cmxs
js_of_ocaml bin/.technologic.eobjs/jsoo/technologic.cmo.js
js_of_ocaml bin/.technologic.eobjs/jsoo/technologic.cmo.{js,jsoo-shape}
js_of_ocaml bin/.technologic.eobjs/jsoo/z.cmo.{js,jsoo-shape}
js_of_ocaml bin/technologic.bc.js
$ node ./_build/default/bin/technologic.bc.js
buy it
Expand Down

0 comments on commit aa79af2

Please sign in to comment.