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

(library (extra_objects)): avoid double linking #10783

Merged
merged 8 commits into from
Jul 30, 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
3 changes: 3 additions & 0 deletions doc/changes/10783.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
- In a `(library)` stanza with `(extra_objects)` and `(foreign_stubs)`, avoid
double linking the extra object files in the final executable.
(#10783, @nojb)
21 changes: 7 additions & 14 deletions src/dune_rules/lib_rules.ml
Original file line number Diff line number Diff line change
Expand Up @@ -289,20 +289,13 @@ let build_stubs lib ~cctx ~dir ~expander ~requires ~dir_contents ~vlib_stubs_o_f
Foreign_sources.for_lib foreign_sources ~name
in
let* o_files =
let lib_foreign_o_files =
let { Lib_config.ext_obj; _ } = (Compilation_context.ocaml cctx).lib_config in
Foreign.Objects.build_paths lib.buildable.extra_objects ~ext_obj ~dir
in
let+ tbl =
Foreign_rules.build_o_files
~sctx
~dir
~expander
~requires
~dir_contents
~foreign_sources
in
Mode.Map.Multi.add_all tbl Mode.Select.All lib_foreign_o_files
Foreign_rules.build_o_files
~sctx
~dir
~expander
~requires
~dir_contents
~foreign_sources
in
let all_o_files = Mode.Map.Multi.to_flat_list o_files in
let* () = Check_rules.add_files sctx ~dir all_o_files in
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
----------------------------------------------------------------------------------
Build an library which indirectly depends on foreign object files.

----------------------------------------------------------------------------------
* Building library with indirect dependency on object file

$ echo "(lang dune 3.5)" > dune-project

$ mkdir -p lib

$ cat >lib/dune <<EOF
> (library
> (name calc)
> (extra_objects add)
> (foreign_stubs (language c) (names dep)))
> EOF

$ cat >lib/calc.ml <<EOF
> external add : int -> int -> int = "add"
> EOF

$ cat >lib/dep.c <<EOF
> #include <caml/mlvalues.h>
> extern value add(value x, value y);
> value dummy() { return add(Val_int(1), Val_int(2)); }
> EOF

$ cat >lib/add.c <<EOF
> #include <caml/mlvalues.h>
> value add(value x, value y) { return Val_int(Int_val(x) + Int_val(y)); }
> EOF

$ cat >>lib/dune <<EOF
> (rule
> (target add.o)
> (deps add.c)
> (action (run %{bin:ocamlc} add.c)))
> EOF

$ dune build
Loading