Skip to content

Commit 8329919

Browse files
authored
feature: add an unused-libs alias (#12623)
* feature: unused-libs alias Introduce an alias to detect unused libraries in libraries and executable stanzas. This alias relies on information extracted using ocamlobjinfo to detect which modules are actually used Signed-off-by: Rudi Grinberg <[email protected]>
1 parent 7e320d5 commit 8329919

37 files changed

+848
-150
lines changed

boot/libs.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
open Types
2-
let external_libraries = [ "unix"; "threads" ]
2+
let external_libraries = [ "pp"; "unix"; "csexp"; "threads" ]
33

44
let local_libraries =
55
[ { path = "otherlibs/top-closure"

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` alias to detect unused libraries.
2+
(#12623, fixes #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

0 commit comments

Comments
 (0)