Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix link of packed module with separate compilation #1642

Merged
merged 3 commits into from
Jul 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
## Bug fixes
* Runtime: fix parsing of unsigned integers (0u2147483648)
* Toplevel: fix missing primitives with separate compilation
* Compiler: fix link of packed modules with separate compilation

# 5.8.2 (2024-05-26) - Luc

Expand Down
15 changes: 15 additions & 0 deletions compiler/lib/ocaml_compiler.ml
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,21 @@ module Cmo_format = struct
let requires (t : t) = List.map t.cu_required_compunits ~f:(fun (Compunit u) -> u)
[@@if ocaml_version >= (5, 2, 0)]

let provides (t : t) =
List.filter_map t.cu_reloc ~f:(fun ((reloc : Cmo_format.reloc_info), _) ->
match reloc with
| Reloc_setglobal i -> Some (Ident.name i)
| Reloc_getglobal _ | Reloc_literal _ | Reloc_primitive _ -> None)
[@@if ocaml_version < (5, 2, 0)]

let provides (t : t) =
List.filter_map t.cu_reloc ~f:(fun ((reloc : Cmo_format.reloc_info), _) ->
match reloc with
| Reloc_setcompunit (Compunit u) -> Some u
| Reloc_getcompunit _ | Reloc_getpredef _ | Reloc_literal _ | Reloc_primitive _ ->
None)
[@@if ocaml_version >= (5, 2, 0)]

let primitives (t : t) = t.cu_primitives

let imports (t : t) = t.cu_imports
Expand Down
2 changes: 2 additions & 0 deletions compiler/lib/ocaml_compiler.mli
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@ module Cmo_format : sig

val requires : t -> string list

val provides : t -> string list

val primitives : t -> string list

val force_link : t -> bool
Expand Down
3 changes: 2 additions & 1 deletion compiler/lib/unit_info.ml
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@ let of_primitives l =

let of_cmo (cmo : Cmo_format.compilation_unit) =
let open Ocaml_compiler in
let provides = StringSet.singleton (Cmo_format.name cmo) in
(* A packed librariy register global for packed modules. *)
let provides = StringSet.of_list (Cmo_format.name cmo :: Cmo_format.provides cmo) in
let requires = StringSet.of_list (Cmo_format.requires cmo) in
let requires = StringSet.diff requires provides in
let effects_without_cps =
Expand Down
29 changes: 29 additions & 0 deletions compiler/tests-linkall/dune
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
;; Test that separate compilation behave correctly in presence of pack

(env
(_
(js_of_ocaml
(compilation_mode separate))))

(executable
(name test)
(modes byte js)
(libraries dynlink)
;; It doesn't seem possible to create a pack-ed module with dune.
;; However, dynlink uses pack to embed a copy
;; of the compiler up until OCaml 5.2.
;; Let's use it for the test
(link_flags
(:standard -linkall)))

(rule
(target test.output)
(action
(with-stdout-to
%{target}
(run node %{dep:test.bc.js}))))

(rule
(alias runtest)
(action
(diff test.reference test.output)))
1 change: 1 addition & 0 deletions compiler/tests-linkall/test.ml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
print_endline "hello world"
1 change: 1 addition & 0 deletions compiler/tests-linkall/test.reference
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
hello world
Loading