Skip to content

Commit 41212a5

Browse files
authored
(no_dynlink): do not build .cmxs (#11176)
* Do not build .cmxs when library is (no_dynlink) Signed-off-by: Nicolás Ojeda Bär <n.oje.bar@gmail.com> * Add test Signed-off-by: Nicolás Ojeda Bär <n.oje.bar@gmail.com> * Changes Signed-off-by: Nicolás Ojeda Bär <n.oje.bar@gmail.com> * Improve test Signed-off-by: Nicolás Ojeda Bär <n.oje.bar@gmail.com> * Add test Signed-off-by: Nicolás Ojeda Bär <n.oje.bar@gmail.com> --------- Signed-off-by: Nicolás Ojeda Bär <n.oje.bar@gmail.com>
1 parent af2e9b6 commit 41212a5

File tree

5 files changed

+69
-1
lines changed

5 files changed

+69
-1
lines changed

doc/changes/11176.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
- #11176: when a library declares `(no_dynlink)`, then the `.cmxs` file for it
2+
is no longer built. (@nojb)

src/dune_rules/lib_info.ml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -378,6 +378,7 @@ let orig_src_dir t = t.orig_src_dir
378378
let best_src_dir t = Option.value ~default:t.src_dir t.orig_src_dir
379379
let set_version t version = { t with version }
380380
let entry_modules t = t.entry_modules
381+
let dynlink_supported t = Mode.Dict.get t.plugins Native <> []
381382

382383
let eval_native_archives_exn (type path) (t : path t) ~modules =
383384
match t.native_archives, modules with

src/dune_rules/lib_info.mli

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,7 @@ val enabled : _ t -> Enabled_status.t Memo.t
155155
val orig_src_dir : 'path t -> 'path option
156156
val version : _ t -> Package_version.t option
157157
val dune_version : _ t -> Dune_lang.Syntax.Version.t option
158+
val dynlink_supported : _ t -> bool
158159

159160
(** Directory where the source files for the library are located. Returns the
160161
original src dir when it exists *)

src/dune_rules/lib_rules.ml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -490,7 +490,9 @@ let setup_build_archives (lib : Library.t) ~top_sorted_modules ~cctx ~expander ~
490490
Super_context.add_rule sctx ~dir ~loc:lib.buildable.loc rule)))
491491
in
492492
Memo.when_
493-
(Dynlink_supported.By_the_os.get natdynlink_supported && modes.ocaml.native)
493+
(Lib_info.dynlink_supported lib_info
494+
&& Dynlink_supported.By_the_os.get natdynlink_supported
495+
&& modes.ocaml.native)
494496
(fun () -> build_shared ~native_archives ~sctx lib ~dir ~flags)
495497
;;
496498

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
This test checks that if a library is declared with `(no_dynlink)`, then the
2+
corresponding `.cmxs` file is *not* built.
3+
4+
$ cat >dune-project <<EOF
5+
> (lang dune 3.17)
6+
> EOF
7+
8+
First we check the behaviour when `(no_dynlink)` is not present.
9+
10+
$ cat >dune <<EOF
11+
> (library
12+
> (name mylib))
13+
> EOF
14+
15+
$ touch a.ml
16+
17+
$ dune build _build/default/mylib.cmxs
18+
19+
Now with `(no_dynlink)`.
20+
21+
$ cat >dune <<EOF
22+
> (library
23+
> (name mylib)
24+
> (no_dynlink))
25+
> EOF
26+
27+
$ dune clean
28+
29+
$ dune build _build/default/mylib.cmxs
30+
Error: Don't know how to build _build/default/mylib.cmxs
31+
Hint: did you mean _build/default/mylib.cma or _build/default/mylib.cmxa?
32+
[1]
33+
34+
Next, we check that the .cmxs is installed without `(no_dynlink)`:
35+
36+
$ cat >dune-project <<EOF
37+
> (lang dune 3.17)
38+
> (package (name mylib))
39+
> EOF
40+
41+
$ cat >dune <<EOF
42+
> (library
43+
> (public_name mylib))
44+
> EOF
45+
46+
$ dune build _build/default/mylib.install
47+
48+
$ grep cmxs _build/default/mylib.install
49+
"_build/install/default/lib/mylib/mylib.cmxs"
50+
51+
And *not* installed with `(no_dynlink)`:
52+
53+
$ cat >dune <<EOF
54+
> (library
55+
> (public_name mylib)
56+
> (no_dynlink))
57+
> EOF
58+
59+
$ dune build _build/default/mylib.install
60+
61+
$ grep cmxs _build/default/mylib.install
62+
[1]

0 commit comments

Comments
 (0)