Skip to content

Commit

Permalink
(library (extra_objects)): avoid double linking (ocaml#10783)
Browse files Browse the repository at this point in the history
Signed-off-by: Nicolás Ojeda Bär <n.oje.bar@gmail.com>
  • Loading branch information
nojb authored and anmonteiro committed Nov 17, 2024
1 parent 8babdb4 commit 9475228
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 14 deletions.
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

0 comments on commit 9475228

Please sign in to comment.