Skip to content

Commit 9f7438c

Browse files
committed
feature: unused-libs alias
Introduce an alias to detect unused libraries in libraries and executable stanzas. This stanza relies on information extracted using ocamlobjinfo to detect which modules are actually used Signed-off-by: Rudi Grinberg <me@rgrinberg.com> feature: allow_unused_libraries Signed-off-by: Rudi Grinberg <me@rgrinberg.com>
1 parent b11b1e1 commit 9f7438c

36 files changed

+830
-149
lines changed

doc/changes/added/12623.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
- Introduce an `unused-libs` stanza to detect unused libraries (#12623, fixes
2+
#650, @rgrinberg)

doc/reference/aliases.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ Some aliases are defined and managed by Dune itself:
8282
aliases/runtest
8383
aliases/fmt
8484
aliases/lint
85+
aliases/unused-libs
8586

8687
.. grid-item::
8788

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
@unused-libs
2+
============
3+
4+
This alias is used to detect unused entries in the libraries field of
5+
executables and stanzas.

src/dune_rules/alias0.ml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ let install = standard "install"
2626
let pkg_install = Alias.Name.of_string "pkg-install"
2727
let ocaml_index = standard "ocaml-index"
2828
let runtest = standard "runtest"
29+
let unused_libs = standard "unused-libs"
2930
let all = standard "all"
3031
let default = standard "default"
3132
let empty = standard "empty"

src/dune_rules/alias0.mli

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ val ocaml_index : Name.t
1515
val install : Name.t
1616
val pkg_install : Name.t
1717
val runtest : Name.t
18+
val unused_libs : Name.t
1819
val empty : Name.t
1920
val all : Name.t
2021
val default : Name.t

src/dune_rules/artifacts_db.ml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ let available_exes ~dir (exes : Executables.t) =
2323
libs
2424
(`Exe exes.names)
2525
exes.buildable.libraries
26+
~allow_unused_libraries:exes.buildable.allow_unused_libraries
2627
~pps
2728
~dune_version
2829
~forbidden_libraries:exes.forbidden_libraries

src/dune_rules/cinaps.ml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,7 @@ let gen_rules sctx t ~dir ~scope =
140140
(Scope.libs scope)
141141
(`Exe names)
142142
(Lib_dep.Direct (loc, Lib_name.of_string "cinaps.runtime") :: t.libraries)
143+
~allow_unused_libraries:[]
143144
~pps:(Preprocess.Per_module.pps t.preprocess)
144145
~dune_version
145146
~allow_overlaps:false

src/dune_rules/dep_rules.ml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,10 @@ let ooi_deps
3636
let ctx = Super_context.context sctx in
3737
Context.ocaml ctx
3838
in
39-
Ocamlobjinfo.rules ocaml ~sandbox ~dir ~unit
39+
Ocamlobjinfo.rules ocaml ~sandbox ~dir ~units:[ unit ]
40+
|> Action_builder.map ~f:(function
41+
| [ x ] -> x
42+
| [] | _ :: _ -> assert false)
4043
in
4144
let add_rule = Super_context.add_rule sctx ~dir in
4245
let read =

src/dune_rules/dune_package.ml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -289,6 +289,7 @@ module Lib = struct
289289
~plugins
290290
~archives
291291
~ppx_runtime_deps
292+
~allow_unused_libraries:[]
292293
~foreign_archives
293294
~native_archives:(Files native_archives)
294295
~foreign_dll_files

src/dune_rules/exe_rules.ml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,20 @@ let executables_rules
208208
in
209209
let lib_config = ocaml.lib_config in
210210
let* requires_compile = Compilation_context.requires_compile cctx in
211+
let* () =
212+
let toolchain = Compilation_context.ocaml cctx in
213+
let direct_requires = Lib.Compile.direct_requires compile_info in
214+
let allow_unused_libraries = Lib.Compile.allow_unused_libraries compile_info in
215+
Unused_libs_rules.gen_rules
216+
sctx
217+
toolchain
218+
exes.buildable.loc
219+
~obj_dir
220+
~modules
221+
~dir
222+
~direct_requires
223+
~allow_unused_libraries
224+
in
211225
let* () =
212226
let* dep_graphs =
213227
(* Building an archive for foreign stubs, we link the corresponding object
@@ -328,6 +342,7 @@ let compile_info ~scope (exes : Executables.t) =
328342
(Scope.libs scope)
329343
(`Exe exes.names)
330344
exes.buildable.libraries
345+
~allow_unused_libraries:exes.buildable.allow_unused_libraries
331346
~pps
332347
~dune_version
333348
~allow_overlaps:exes.buildable.allow_overlapping_dependencies

0 commit comments

Comments
 (0)