Skip to content

Commit

Permalink
Merge pull request #3626 from rgrinberg/fix-3622
Browse files Browse the repository at this point in the history
Fix separate compilation of jsoo
  • Loading branch information
rgrinberg authored Jul 15, 2020
2 parents e3e5902 + 20be9fd commit 2a10ee5
Show file tree
Hide file tree
Showing 40 changed files with 201 additions and 124 deletions.
2 changes: 2 additions & 0 deletions .travis-ci.sh
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ opam_install_test_deps () {
result.1.4 \
"utop>=2.6.0" \
mdx.1.6.0 \
js_of_ocaml-ppx \
js_of_ocaml-compiler \
bisect_ppx
# We install Coq separatedly as to be more resistant w.r.t. the 10
# minutes Travis timeout; the travis_wait hack doesn't work well
Expand Down
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
8 changes: 3 additions & 5 deletions bin/top.ml
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,8 @@ let man =

let info = Term.info "top" ~doc ~man

let link_deps link ~lib_config =
List.concat_map link ~f:(fun t ->
Dune.Lib.link_deps t Dune.Link_mode.Byte lib_config)
let link_deps link =
List.concat_map link ~f:(fun t -> Dune.Lib.link_deps t Dune.Link_mode.Byte)

let term =
let+ common = Common.term
Expand All @@ -44,8 +43,7 @@ let term =
let libs = Dune.Utop.libs_under_dir sctx ~db ~dir:(Path.build dir) in
let requires = Dune.Lib.closure ~linking:true libs |> Result.ok_exn in
let include_paths = Dune.Lib.L.include_paths requires in
let lib_config = sctx |> Super_context.context |> Context.lib_config in
let files = link_deps requires ~lib_config in
let files = link_deps requires in
let* () = do_build (List.map files ~f:(fun f -> Target.File f)) in
let files_to_load =
List.filter files ~f:(fun p ->
Expand Down
4 changes: 2 additions & 2 deletions src/dune/exe.ml
Original file line number Diff line number Diff line change
Expand Up @@ -184,10 +184,10 @@ let link_js ~name ~cm_files ~promote cctx =
let src = exe_path_from_name cctx ~name ~linkage:Linkage.byte in
let flags =
Expander.expand_and_eval_set expander js_of_ocaml.flags
~standard:(Build.return (Js_of_ocaml_rules.standard sctx))
~standard:(Build.return (Jsoo_rules.standard sctx))
in
let top_sorted_cms = Cm_files.top_sorted_cms cm_files ~mode:Mode.Byte in
Js_of_ocaml_rules.build_exe cctx ~js_of_ocaml ~src ~cm:top_sorted_cms
Jsoo_rules.build_exe cctx ~js_of_ocaml ~src ~cm:top_sorted_cms
~flags:(Command.Args.dyn flags) ~promote

let build_and_link_many ~programs ~linkages ~promote ?link_args ?o_files
Expand Down
2 changes: 1 addition & 1 deletion src/dune/gen_rules.ml
Original file line number Diff line number Diff line change
Expand Up @@ -304,7 +304,7 @@ let gen_rules ~sctx ~dir components : Build_system.extra_sub_directories_to_keep
(Build.write_file (Path.Build.relative dir "configurator") "");
These String.Set.empty
| ".js" :: rest -> (
Js_of_ocaml_rules.setup_separate_compilation_rules sctx rest;
Jsoo_rules.setup_separate_compilation_rules sctx rest;
match rest with
| [] -> All
| _ -> These String.Set.empty )
Expand Down
49 changes: 30 additions & 19 deletions src/dune/js_of_ocaml_rules.ml → src/dune/jsoo_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,29 @@ 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 =
let stdlib_dir = (Lib.lib_config pkg).stdlib_dir in
Path.relative 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
File renamed without changes.
30 changes: 15 additions & 15 deletions src/dune/lib.ml
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@ module T = struct
; resolved_selects : Resolved_select.t list
; user_written_deps : Dune_file.Lib_deps.t
; implements : t Or_exn.t option
; stdlib_dir : Path.t
; lib_config : Lib_config.t
; (* these fields cannot be forced until the library is instantiated *)
default_implementation : t Or_exn.t Lazy.t option
; (* This is mutable to avoid this error:
Expand Down Expand Up @@ -317,7 +317,7 @@ type db =
; resolve : Lib_name.t -> resolve_result
; table : (Lib_name.t, Status.t) Table.t
; all : Lib_name.t list Lazy.t
; stdlib_dir : Path.t
; lib_config : Lib_config.t
; instrument_with : Lib_name.t list
}

Expand All @@ -328,6 +328,8 @@ and resolve_result =
| Invalid of exn
| Redirect of db option * (Loc.t * Lib_name.t)

let lib_config (t : lib) = t.lib_config

let name t = t.name

let info t = t.info
Expand Down Expand Up @@ -390,7 +392,7 @@ module Link_params = struct
not appear on the command line *)
}

let get t (mode : Link_mode.t) (lib_config : Lib_config.t) =
let get t (mode : Link_mode.t) =
let lib_files = Lib_info.foreign_archives t.info
and dll_files = Lib_info.foreign_dll_files t.info in
(* OCaml library archives [*.cma] and [*.cmxa] are directly listed in the
Expand Down Expand Up @@ -440,14 +442,14 @@ module Link_params = struct
Path.extend_basename obj_name ~suffix:(Cm_kind.ext Cmo) :: hidden_deps
| Native ->
Path.extend_basename obj_name ~suffix:(Cm_kind.ext Cmx)
:: Path.extend_basename obj_name ~suffix:lib_config.ext_obj
:: Path.extend_basename obj_name ~suffix:t.lib_config.ext_obj
:: hidden_deps )
in
{ deps; hidden_deps; include_dirs }
end

let link_deps t mode lib_config =
let x = Link_params.get t mode lib_config in
let link_deps t mode =
let x = Link_params.get t mode in
List.rev_append x.hidden_deps x.deps

module L = struct
Expand All @@ -470,7 +472,7 @@ module L = struct
in
match ts with
| [] -> dirs
| x :: _ -> Path.Set.remove dirs x.stdlib_dir
| x :: _ -> Path.Set.remove dirs x.lib_config.stdlib_dir

let include_flags ts = to_iflags (include_paths ts)

Expand All @@ -482,14 +484,12 @@ module L = struct
in
match ts with
| [] -> dirs
| x :: _ -> Path.Set.remove dirs x.stdlib_dir
| x :: _ -> Path.Set.remove dirs x.lib_config.stdlib_dir

let c_include_flags ts = to_iflags (c_include_paths ts)

let compile_and_link_flags ~compile ~link ~mode ~lib_config =
let params =
List.map link ~f:(fun t -> Link_params.get t mode lib_config)
in
let compile_and_link_flags ~compile ~link ~mode =
let params = List.map link ~f:(fun t -> Link_params.get t mode) in
let dirs =
let dirs =
Path.Set.union (include_paths compile) (c_include_paths link)
Expand Down Expand Up @@ -534,7 +534,7 @@ module Lib_and_module = struct
Command.Args.S
(List.map ts ~f:(function
| Lib t ->
let p = Link_params.get t mode lib_config in
let p = Link_params.get t mode in
Command.Args.S
( Deps p.deps
:: Hidden_deps (Dep.Set.of_files p.hidden_deps)
Expand Down Expand Up @@ -1088,7 +1088,7 @@ end = struct
; sub_systems = Sub_system_name.Map.empty
; implements
; default_implementation
; stdlib_dir = db.stdlib_dir
; lib_config = db.lib_config
; re_exports
}
in
Expand Down Expand Up @@ -1650,7 +1650,7 @@ module DB = struct
; resolve
; table = Table.create (module Lib_name) 1024
; all = Lazy.from_fun all
; stdlib_dir = lib_config.Lib_config.stdlib_dir
; lib_config
; instrument_with = lib_config.Lib_config.instrument_with
}

Expand Down
10 changes: 4 additions & 6 deletions src/dune/lib.mli
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ val to_dyn : t -> Dyn.t
or the [name] if not. *)
val name : t -> Lib_name.t

val lib_config : t -> Lib_config.t

val implements : t -> t Or_exn.t option

(** Directory where the object files for the library are located. *)
Expand Down Expand Up @@ -53,7 +55,7 @@ val hash : t -> int

(** The list of files that will be read by the compiler when linking an
executable against this library *)
val link_deps : t -> Link_mode.t -> Lib_config.t -> Path.t list
val link_deps : t -> Link_mode.t -> Path.t list

(** Operations on list of libraries *)
module L : sig
Expand All @@ -70,11 +72,7 @@ module L : sig
val c_include_flags : t -> _ Command.Args.t

val compile_and_link_flags :
compile:t
-> link:t
-> mode:Link_mode.t
-> lib_config:Lib_config.t
-> _ Command.Args.t
compile:t -> link:t -> mode:Link_mode.t -> _ Command.Args.t

val jsoo_runtime_files : t -> Path.t list

Expand Down
2 changes: 1 addition & 1 deletion src/dune/lib_rules.ml
Original file line number Diff line number Diff line change
Expand Up @@ -325,7 +325,7 @@ let setup_build_archives (lib : Dune_file.Library.t) ~dir_contents ~cctx
Path.Build.relative (Obj_dir.obj_dir obj_dir) (Path.Build.basename src)
|> Path.Build.extend_basename ~suffix:".js"
in
Js_of_ocaml_rules.build_cm cctx ~js_of_ocaml ~src ~target);
Jsoo_rules.build_cm cctx ~js_of_ocaml ~src ~target);
if Dynlink_supported.By_the_os.get natdynlink_supported && modes.native then
build_shared ~dir_contents ~sctx lib ~dir ~flags

Expand Down
2 changes: 1 addition & 1 deletion src/dune/module_compilation.ml
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ let build_module ~dep_graphs ?(precompiled_cmi = false) cctx m =
let src = Obj_dir.Module.cm_file_unsafe obj_dir m ~kind:Cm_kind.Cmo in
let target = Path.Build.extend_basename src ~suffix:".js" in
SC.add_rules sctx ~dir
(Js_of_ocaml_rules.build_cm cctx ~js_of_ocaml ~src ~target))
(Jsoo_rules.build_cm cctx ~js_of_ocaml ~src ~target))

let ocamlc_i ?(flags = []) ~dep_graphs cctx (m : Module.t) ~output =
let sctx = CC.super_context cctx in
Expand Down
2 changes: 1 addition & 1 deletion src/dune/preprocessing.ml
Original file line number Diff line number Diff line change
Expand Up @@ -347,7 +347,7 @@ let build_ppx_driver sctx ~dep_kind ~target ~pps ~pp_names =
(Result.map driver_and_libs ~f:(fun (_driver, libs) ->
Command.Args.S
[ Lib.L.compile_and_link_flags ~mode:link_mode
~compile:libs ~link:libs ~lib_config:ctx.lib_config
~compile:libs ~link:libs
; Hidden_deps
(Lib_file_deps.deps libs ~groups:[ Cmi; Cmx ])
]))
Expand Down
39 changes: 35 additions & 4 deletions test/blackbox-tests/dune.inc
Original file line number Diff line number Diff line change
Expand Up @@ -1797,17 +1797,45 @@
(progn (run dune-cram run run.t) (diff? run.t run.t.corrected)))))

(rule
(alias js_of_ocaml)
(alias jsoo-github3622)
(deps
(package dune)
(source_tree test-cases/js_of_ocaml.t)
(source_tree test-cases/jsoo/github3622.t)
(alias test-deps))
(action
(setenv
NODE
%{bin:node}
(chdir
test-cases/js_of_ocaml.t
test-cases/jsoo/github3622.t
(progn (run dune-cram run run.t) (diff? run.t run.t.corrected))))))

(rule
(alias jsoo-inline-tests)
(deps
(package dune)
(source_tree test-cases/jsoo/inline-tests.t)
(alias test-deps))
(action
(setenv
NODE
%{bin:node}
(chdir
test-cases/jsoo/inline-tests.t
(progn (run dune-cram run run.t) (diff? run.t run.t.corrected))))))

(rule
(alias jsoo-simple)
(deps
(package dune)
(source_tree test-cases/jsoo/simple.t)
(alias test-deps))
(action
(setenv
NODE
%{bin:node}
(chdir
test-cases/jsoo/simple.t
(progn (run dune-cram run run.t) (diff? run.t run.t.corrected))))))

(rule
Expand Down Expand Up @@ -3311,6 +3339,9 @@
(alias invalid-dune-package)
(alias invalid-module-name)
(alias invalid-pform)
(alias jsoo-github3622)
(alias jsoo-inline-tests)
(alias jsoo-simple)
(alias lib)
(alias lib-available)
(alias lib-errors)
Expand Down Expand Up @@ -3713,6 +3744,6 @@
(alias env-envs-and-contexts)
(alias fdo)))

(alias (name runtest-js) (deps (alias explicit_js_mode) (alias js_of_ocaml)))
(alias (name runtest-js) (deps (alias explicit_js_mode)))

(alias (name runtest-coq) (deps (alias coq-extract) (alias coq-main)))
Loading

0 comments on commit 2a10ee5

Please sign in to comment.