Skip to content

Commit 258c8a7

Browse files
committed
feat: %{cmt} and %{cmti} artifact variables
Signed-off-by: Ali Caglayan <alizter@gmail.com>
1 parent 8544e48 commit 258c8a7

File tree

13 files changed

+246
-31
lines changed

13 files changed

+246
-31
lines changed

doc/advanced/variables-artifacts.rst

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,18 @@ interpreted relative to the current directory:
2323
``<path>`` should be the name of a module as specified in a ``(modules)``
2424
field.
2525

26-
- ``cma:<path>`` and ``cmxa:<path>`` expands to the corresponding
27-
artifact's path for the library specified by ``<path>``. The basename of ``<path>``
26+
- ``cma:<path>`` and ``cmxa:<path>`` expands to the corresponding artifact's
27+
path for the library specified by ``<path>``. The basename of ``<path>``
2828
should be the name of the library as specified in the ``(name)`` field of a
2929
``library`` stanza (*not* its public name).
3030

31+
- ``cmt:<path>`` and ``cmti:<path>`` expand to the corresponding compiled
32+
annotation files for the module specified by ``<path>``. These files contain
33+
the typed abstract syntax tree with precise location information and type
34+
annotations, generated with the ``-bin-annot`` flag. They are particularly
35+
useful for IDE tools to provide tooltips and type information.
36+
37+
.. versionadded:: 3.21
38+
3139
In each case, the expansion of the variable is a path pointing inside the build
3240
context (i.e., ``_build/<context>``).

doc/changes/added/12634.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
- Add support for `%{cmt:...}` and `%{cmti:...}` variables to reference
2+
compiled annotation files (.cmt and .cmti) containing typed abstract syntax
3+
trees with location and type information. (#12634, grants #12633, @Alizter)

src/dune_lang/lib_mode.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ module Cm_kind = struct
7171
end
7272

7373
let of_cm_kind : Cm_kind.t -> t = function
74-
| Ocaml (Cmi | Cmo) -> Ocaml Byte
74+
| Ocaml (Cmi | Cmo | Cmt | Cmti) -> Ocaml Byte
7575
| Ocaml Cmx -> Ocaml Native
7676
| Melange (Cmi | Cmj) -> Melange
7777
;;

src/dune_lang/pform.ml

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -610,7 +610,13 @@ module Env = struct
610610
let macros =
611611
let macro (x : Macro.t) = No_info x in
612612
let artifact x =
613-
String.drop (Artifact.ext x) 1, since ~version:(2, 0) (Macro.Artifact x)
613+
let name = String.drop (Artifact.ext x) 1 in
614+
let version =
615+
match x with
616+
| Mod Cmt | Mod Cmti -> 3, 21
617+
| _ -> 2, 0
618+
in
619+
name, since ~version (Macro.Artifact x)
614620
in
615621
String.Map.of_list_exn
616622
([ "exe", macro Exe

src/dune_rules/check_rules.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
open Import
22

33
let dev_files =
4-
[ Ml_kind.cmt_ext Impl; Ml_kind.cmt_ext Intf; Cm_kind.ext Cmi ]
4+
[ Cm_kind.ext Cmt; Cm_kind.ext Cmti; Cm_kind.ext Cmi ]
55
|> List.map ~f:(String.drop_prefix_if_exists ~prefix:".")
66
|> Glob.matching_extensions
77
;;

src/dune_rules/compilation_context.ml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,8 @@ module Includes = struct
4848
])
4949
|> Resolve.Memo.args
5050
|> Command.Args.memo
51+
; cmt = cmi_includes
52+
; cmti = cmi_includes
5153
})
5254
; melange =
5355
{ cmi = make_includes_args ~mode:Melange [ Melange Cmi ]

src/dune_rules/module_compilation.ml

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ let copy_interface ~sctx ~dir ~obj_dir ~cm_kind m =
6262

6363
let melange_args (cctx : Compilation_context.t) (cm_kind : Lib_mode.Cm_kind.t) module_ =
6464
match cm_kind with
65-
| Ocaml (Cmi | Cmo | Cmx) | Melange Cmi -> []
65+
| Ocaml (Cmi | Cmo | Cmx | Cmt | Cmti) | Melange Cmi -> []
6666
| Melange Cmj ->
6767
let melange_extension_version =
6868
let scope = Compilation_context.scope cctx in
@@ -188,15 +188,15 @@ let build_cm
188188
Command.Args.Hidden_targets
189189
[ Obj_dir.Module.cm_file_exn obj_dir m ~kind:cmi_kind ]
190190
| (Ocaml Cmo | Melange Cmj), None, true
191-
| (Ocaml (Cmo | Cmx) | Melange Cmj), _, _ ->
191+
| (Ocaml (Cmo | Cmx | Cmt) | Melange Cmj), _, _ ->
192192
Memo.return (force_read_cmi ~obj_dir ~version:ocaml.version ~cm_kind ~src m)
193-
| (Ocaml Cmi | Melange Cmi), _, _ ->
193+
| (Ocaml (Cmi | Cmti) | Melange Cmi), _, _ ->
194194
let+ () = copy_interface ~dir ~obj_dir ~sctx ~cm_kind m in
195195
Command.Args.empty))
196196
in
197197
let other_targets =
198198
match cm_kind with
199-
| Ocaml (Cmi | Cmo) | Melange (Cmi | Cmj) -> Command.Args.empty
199+
| Ocaml (Cmi | Cmo | Cmt | Cmti) | Melange (Cmi | Cmj) -> Command.Args.empty
200200
| Ocaml Cmx ->
201201
(match phase with
202202
| Some Compile ->
@@ -215,6 +215,7 @@ let build_cm
215215
let cmt_args =
216216
match cm_kind with
217217
| Ocaml Cmx -> Command.Args.empty
218+
| Ocaml (Cmt | Cmti) -> As [ "-bin-annot" ]
218219
| Ocaml (Cmi | Cmo) | Melange (Cmi | Cmj) ->
219220
if Compilation_context.bin_annot cctx
220221
then (

src/dune_rules/obj_dir.ml

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ module External = struct
9090
| Ocaml Cmi, Public, _ -> public_cmi_ocaml_dir t
9191
| Melange Cmi, Public, _ -> public_cmi_melange_dir t
9292
| Melange Cmj, _, _ -> t.melange_dir
93-
| Ocaml (Cmo | Cmx), _, _ -> t.public_dir
93+
| Ocaml (Cmo | Cmx | Cmt | Cmti), _, _ -> t.public_dir
9494
;;
9595

9696
let encode
@@ -165,7 +165,7 @@ module External = struct
165165
let cm_public_dir t (cm_kind : Lib_mode.Cm_kind.t) =
166166
match cm_kind with
167167
| Ocaml Cmx -> native_dir t
168-
| Ocaml Cmo -> byte_dir t
168+
| Ocaml (Cmo | Cmt | Cmti) -> byte_dir t
169169
| Ocaml Cmi -> public_cmi_ocaml_dir t
170170
| Melange Cmj -> melange_dir t
171171
| Melange Cmi -> public_cmi_melange_dir t
@@ -321,14 +321,14 @@ module Local = struct
321321
let cm_dir t (cm_kind : Lib_mode.Cm_kind.t) _ =
322322
match cm_kind with
323323
| Ocaml Cmx -> native_dir t
324-
| Ocaml (Cmo | Cmi) -> byte_dir t
324+
| Ocaml (Cmo | Cmi | Cmt | Cmti) -> byte_dir t
325325
| Melange (Cmj | Cmi) -> melange_dir t
326326
;;
327327

328328
let cm_public_dir t (cm_kind : Lib_mode.Cm_kind.t) =
329329
match cm_kind with
330330
| Ocaml Cmx -> native_dir t
331-
| Ocaml Cmo -> byte_dir t
331+
| Ocaml (Cmo | Cmt | Cmti) -> byte_dir t
332332
| Ocaml Cmi -> public_cmi_ocaml_dir t
333333
| Melange Cmj -> melange_dir t
334334
| Melange Cmi -> public_cmi_melange_dir t
@@ -513,7 +513,8 @@ module Module = struct
513513

514514
let has_impl_if_needed m ~(kind : Lib_mode.Cm_kind.t) =
515515
match kind with
516-
| Ocaml (Cmo | Cmx) | Melange Cmj -> Module.has m ~ml_kind:Impl
516+
| Ocaml (Cmo | Cmx | Cmt) | Melange Cmj -> Module.has m ~ml_kind:Impl
517+
| Ocaml Cmti -> Module.has m ~ml_kind:Intf
517518
| Ocaml Cmi | Melange Cmi -> true
518519
;;
519520

@@ -575,17 +576,20 @@ module Module = struct
575576

576577
let cmt_file t m ~(ml_kind : Ml_kind.t) ~cm_kind =
577578
let file = Module.file m ~ml_kind in
578-
let ext = Ml_kind.cmt_ext ml_kind in
579+
let ext =
580+
match ml_kind with
581+
| Impl -> Cm_kind.ext Cmt
582+
| Intf -> Cm_kind.ext Cmti
583+
in
579584
let cmi_kind = Lib_mode.Cm_kind.cmi cm_kind in
580585
Option.map file ~f:(fun _ -> obj_file t m ~kind:cmi_kind ~ext)
581586
;;
582587

583588
let cmti_file t m ~cm_kind =
584589
let ext =
585-
Ml_kind.cmt_ext
586-
(match Module.file m ~ml_kind:Intf with
587-
| None -> Impl
588-
| Some _ -> Intf)
590+
match Module.file m ~ml_kind:Intf with
591+
| None -> Cm_kind.ext Cmt
592+
| Some _ -> Cm_kind.ext Cmti
589593
in
590594
let cmi_kind = Lib_mode.Cm_kind.cmi cm_kind in
591595
obj_file t m ~kind:cmi_kind ~ext

src/ocaml/cm_kind.ml

Lines changed: 30 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ type t =
44
| Cmi
55
| Cmo
66
| Cmx
7+
| Cmt
8+
| Cmti
79

810
let compare x y : Ordering.t =
911
match x, y with
@@ -14,34 +16,54 @@ let compare x y : Ordering.t =
1416
| Cmo, _ -> Lt
1517
| _, Cmo -> Gt
1618
| Cmx, Cmx -> Eq
19+
| Cmx, _ -> Lt
20+
| _, Cmx -> Gt
21+
| Cmt, Cmt -> Eq
22+
| Cmt, _ -> Lt
23+
| _, Cmt -> Gt
24+
| Cmti, Cmti -> Eq
1725
;;
1826

19-
let all = [ Cmi; Cmo; Cmx ]
27+
let all = [ Cmi; Cmo; Cmx; Cmt; Cmti ]
2028

21-
let choose cmi cmo cmx = function
29+
let choose cmi cmo cmx cmt cmti = function
2230
| Cmi -> cmi
2331
| Cmo -> cmo
2432
| Cmx -> cmx
33+
| Cmt -> cmt
34+
| Cmti -> cmti
2535
;;
2636

27-
let ext = choose ".cmi" ".cmo" ".cmx"
28-
let source = choose Ml_kind.Intf Impl Impl
37+
let ext = choose ".cmi" ".cmo" ".cmx" ".cmt" ".cmti"
38+
let source = choose Ml_kind.Intf Impl Impl Impl Intf
2939

3040
module Dict = struct
3141
type 'a t =
3242
{ cmi : 'a
3343
; cmo : 'a
3444
; cmx : 'a
45+
; cmt : 'a
46+
; cmti : 'a
3547
}
3648

3749
let get t = function
3850
| Cmi -> t.cmi
3951
| Cmo -> t.cmo
4052
| Cmx -> t.cmx
53+
| Cmt -> t.cmt
54+
| Cmti -> t.cmti
4155
;;
4256

43-
let of_func f = { cmi = f ~cm_kind:Cmi; cmo = f ~cm_kind:Cmo; cmx = f ~cm_kind:Cmx }
44-
let make_all x = { cmi = x; cmo = x; cmx = x }
57+
let of_func f =
58+
{ cmi = f ~cm_kind:Cmi
59+
; cmo = f ~cm_kind:Cmo
60+
; cmx = f ~cm_kind:Cmx
61+
; cmt = f ~cm_kind:Cmt
62+
; cmti = f ~cm_kind:Cmti
63+
}
64+
;;
65+
66+
let make_all x = { cmi = x; cmo = x; cmx = x; cmt = x; cmti = x }
4567
end
4668

4769
let to_dyn =
@@ -50,4 +72,6 @@ let to_dyn =
5072
| Cmi -> variant "cmi" []
5173
| Cmo -> variant "cmo" []
5274
| Cmx -> variant "cmx" []
75+
| Cmt -> variant "cmt" []
76+
| Cmti -> variant "cmti" []
5377
;;

src/ocaml/cm_kind.mli

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ type t =
44
| Cmi
55
| Cmo
66
| Cmx
7+
| Cmt
8+
| Cmti
79

810
val compare : t -> t -> Ordering.t
911
val all : t list
@@ -18,6 +20,8 @@ module Dict : sig
1820
{ cmi : 'a
1921
; cmo : 'a
2022
; cmx : 'a
23+
; cmt : 'a
24+
; cmti : 'a
2125
}
2226

2327
val get : 'a t -> cm_kind -> 'a

0 commit comments

Comments
 (0)