Skip to content

Commit

Permalink
Fix separation compilatoin of jsoo ocaml#3622
Browse files Browse the repository at this point in the history
Compile std_exit to js and link it last

Signed-off-by: Rudi Grinberg <[email protected]>
  • Loading branch information
rgrinberg committed Jul 15, 2020
1 parent 609f92c commit 8b8b730
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 21 deletions.
3 changes: 3 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ next
- Allow multiple libraries with `inline_tests` to be defined in the same
directory (#3621, @rgrinberg)

- Run exit hooks in jsoo separate compilation mode (#3626, fixes #3622,
@rgrinberg)

2.6.1 (02/07/2020)
------------------

Expand Down
46 changes: 27 additions & 19 deletions src/dune/js_of_ocaml_rules.ml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ let standard sctx = pretty sctx @ sourcemap sctx
let install_jsoo_hint = "opam install js_of_ocaml-compiler"

let in_build_dir ~ctx args =
Path.L.relative (Path.build ctx.Context.build_dir) (".js" :: args)
Path.Build.L.relative ctx.Context.build_dir (".js" :: args)

let jsoo ~dir sctx =
SC.resolve_program sctx ~dir ~loc:None ~hint:install_jsoo_hint "js_of_ocaml"
Expand Down Expand Up @@ -82,8 +82,11 @@ let jsoo_archives ~ctx lib =
| None ->
let archives = Lib_info.archives info in
List.map archives.byte ~f:(fun archive ->
in_build_dir ~ctx
[ Lib_name.to_string (Lib.name lib); Path.basename archive ^ ".js" ])
Path.build
(in_build_dir ~ctx
[ Lib_name.to_string (Lib.name lib)
; Path.basename archive ^ ".js"
]))

let link_rule cc ~runtime ~target cm =
let sctx = Compilation_context.super_context cc in
Expand All @@ -97,14 +100,20 @@ let link_rule cc ~runtime ~target cm =
(* Special case for the stdlib because it is not referenced in the
META *)
let all_libs =
in_build_dir ~ctx [ "stdlib"; "stdlib.cma.js" ] :: all_libs
Path.build (in_build_dir ~ctx [ "stdlib"; "stdlib.cma.js" ])
:: all_libs
in
let all_other_modules =
List.map cm ~f:(fun m -> Path.extend_basename m ~suffix:".js")
List.map cm ~f:(Path.extend_basename ~suffix:".js")
in
Deps (List.concat [ all_libs; all_other_modules ])))
in
let spec = Command.Args.S [ Dep (Path.build runtime); Dyn get_all ] in
let spec =
let std_exit =
Path.build (in_build_dir ~ctx [ "stdlib"; "std_exit.cmo.js" ])
in
Command.Args.S [ Dep (Path.build runtime); Dyn get_all; Dep std_exit ]
in
let flags = Command.Args.As (sourcemap sctx) in
js_of_ocaml_rule sctx ~sub_command:Link ~dir ~spec ~target ~flags

Expand Down Expand Up @@ -137,27 +146,26 @@ let setup_separate_compilation_rules sctx components =
| None -> ()
| Some pkg ->
let info = Lib.info pkg in
let archives = (Lib_info.archives info).byte in
let lib_name = Lib_name.to_string (Lib.name pkg) in
let archives =
let archives = (Lib_info.archives info).byte in
(* Special case for the stdlib because it is not referenced in the
META *)
match Lib_name.to_string (Lib.name pkg) with
| "stdlib" -> Path.relative ctx.stdlib_dir "stdlib.cma" :: archives
match lib_name with
| "stdlib" ->
let archive = Path.relative ctx.stdlib_dir in
archive "stdlib.cma" :: archive "std_exit.cmo" :: archives
| _ -> archives
in
List.iter archives ~f:(fun fn ->
let name = Path.basename fn in
let src_dir = Lib_info.src_dir info in
let src = Path.relative src_dir name in
let lib_name = Lib_name.to_string (Lib.name pkg) in
let target =
in_build_dir ~ctx [ lib_name; sprintf "%s.js" name ]
|> Path.as_in_build_dir_exn
in
let dir =
Path.as_in_build_dir_exn (in_build_dir ~ctx [ lib_name ])
let target = in_build_dir ~ctx [ lib_name; sprintf "%s.js" name ] in
let spec =
let src_dir = Lib_info.src_dir info in
let src = Path.relative src_dir name in
Command.Args.Dep src
in
let spec = Command.Args.Dep src in
let dir = in_build_dir ~ctx [ lib_name ] in
SC.add_rule sctx ~dir
(js_of_ocaml_rule sctx ~sub_command:Compile ~dir
~flags:(As (standard sctx))
Expand Down
1 change: 1 addition & 0 deletions test/blackbox-tests/test-cases/jsoo/github3622.t/run.t
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,6 @@ Test with separate compilation:

$ dune build --profile=dev ./main.bc.js
$ $NODE _build/default/main.bc.js
bla

The result should be the same
4 changes: 2 additions & 2 deletions test/blackbox-tests/test-cases/jsoo/inline-tests.t/run.t
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ Run inline tests using node js
> EOF

$ dune runtest
run alias byte/runtest
inline_test_runner_inline_tests_byte alias byte/runtest
inline tests (Byte)
inline tests (Byte)
run alias native/runtest
inline_test_runner_inline_tests_native alias native/runtest
inline tests (Native)
inline tests (Native)
node alias js/runtest
Expand Down
1 change: 1 addition & 0 deletions test/blackbox-tests/test-cases/jsoo/simple.t/run.t
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ Compilation using jsoo
ocamlc lib/stubs.o
ocamlopt .ppx/7b799aed44581cc79b02033532c5f775/ppx.exe
ocamlc lib/.x.objs/byte/x__.{cmi,cmo,cmt}
js_of_ocaml .js/stdlib/std_exit.cmo.js
js_of_ocaml bin/technologic.bc.runtime.js
ocamlmklib lib/dllx_stubs.so,lib/libx_stubs.a
ppx lib/x.pp.ml
Expand Down

0 comments on commit 8b8b730

Please sign in to comment.