Skip to content

Commit a88ec65

Browse files
authored
refactor: represent virtual libraries via Lib_kind.t (#12276)
Signed-off-by: Shon Feder <[email protected]>
1 parent 98af77a commit a88ec65

File tree

21 files changed

+180
-81
lines changed

21 files changed

+180
-81
lines changed

src/dune_findlib/package0.ml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,9 @@ let ppx_runtime_deps t =
4444

4545
let kind t : Lib_kind.t =
4646
match Vars.get t.vars "library_kind" Ps.empty with
47-
| Some "ppx_rewriter" -> Ppx_rewriter Lib_kind.Ppx_args.empty
48-
| Some "ppx_deriver" -> Ppx_deriver Lib_kind.Ppx_args.empty
49-
| None | Some _ -> Normal
47+
| Some "ppx_rewriter" -> Dune_file (Ppx_rewriter Lib_kind.Ppx_args.empty)
48+
| Some "ppx_deriver" -> Dune_file (Ppx_deriver Lib_kind.Ppx_args.empty)
49+
| None | Some _ -> Dune_file Normal
5050
;;
5151

5252
let archives t = make_archives t "archive" preds

src/dune_lang/lib_kind.ml

Lines changed: 54 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -58,44 +58,77 @@ module Ppx_args = struct
5858
;;
5959
end
6060

61+
module Dune_file = struct
62+
type t =
63+
| Normal
64+
| Ppx_deriver of Ppx_args.t
65+
| Ppx_rewriter of Ppx_args.t
66+
67+
let cstr_name = function
68+
| Normal -> "normal"
69+
| Ppx_deriver _ -> "ppx_deriver"
70+
| Ppx_rewriter _ -> "ppx_rewriter"
71+
;;
72+
73+
let decode =
74+
let open Decoder in
75+
sum
76+
[ "normal", return Normal
77+
; ( "ppx_deriver"
78+
, let+ args = Ppx_args.decode in
79+
Ppx_deriver args )
80+
; ( "ppx_rewriter"
81+
, let+ args = Ppx_args.decode in
82+
Ppx_rewriter args )
83+
]
84+
;;
85+
86+
let encode t =
87+
match
88+
match t with
89+
| Normal -> Dune_sexp.atom (cstr_name t)
90+
| Ppx_deriver x | Ppx_rewriter x ->
91+
List (Dune_sexp.atom (cstr_name t) :: Ppx_args.encode x)
92+
with
93+
| List [ x ] -> x
94+
| x -> x
95+
;;
96+
97+
let to_dyn x =
98+
let open Dyn in
99+
match x with
100+
| Normal -> variant "Normal" []
101+
| Ppx_deriver args -> variant "Ppx_deriver" [ Ppx_args.to_dyn args ]
102+
| Ppx_rewriter args -> variant "Ppx_rewriter" [ Ppx_args.to_dyn args ]
103+
;;
104+
end
105+
61106
type t =
62-
| Normal
63-
| Ppx_deriver of Ppx_args.t
64-
| Ppx_rewriter of Ppx_args.t
107+
| Virtual
65108
| Parameter
66-
67-
let equal = Poly.equal
109+
| Dune_file of Dune_file.t
68110

69111
let to_dyn x =
70112
let open Dyn in
71113
match x with
72-
| Normal -> variant "Normal" []
114+
| Virtual -> variant "Virtual" []
73115
| Parameter -> variant "Parameter" []
74-
| Ppx_deriver args -> variant "Ppx_deriver" [ Ppx_args.to_dyn args ]
75-
| Ppx_rewriter args -> variant "Ppx_rewriter" [ Ppx_args.to_dyn args ]
116+
| Dune_file t -> variant "Dune_file" [ Dune_file.to_dyn t ]
76117
;;
77118

78119
let decode =
79120
let open Decoder in
80-
sum
81-
[ "normal", return Normal
82-
; "parameter", return Parameter
83-
; ( "ppx_deriver"
84-
, let+ args = Ppx_args.decode in
85-
Ppx_deriver args )
86-
; ( "ppx_rewriter"
87-
, let+ args = Ppx_args.decode in
88-
Ppx_rewriter args )
89-
]
121+
(* TODO: Less code reuse with either? *)
122+
map ~f:(fun k -> Dune_file k) Dune_file.decode
123+
<|> enum [ "parameter", Parameter; "virtual", Virtual ]
90124
;;
91125

92126
let encode t =
93127
match
94128
match t with
95-
| Normal -> Dune_sexp.atom "normal"
129+
| Virtual -> Dune_sexp.atom "virtual"
96130
| Parameter -> Dune_sexp.atom "parameter"
97-
| Ppx_deriver x -> List (Dune_sexp.atom "ppx_deriver" :: Ppx_args.encode x)
98-
| Ppx_rewriter x -> List (Dune_sexp.atom "ppx_rewriter" :: Ppx_args.encode x)
131+
| Dune_file d -> Dune_file.encode d
99132
with
100133
| List [ x ] -> x
101134
| x -> x

src/dune_lang/lib_kind.mli

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,32 @@ module Ppx_args : sig
1313
val empty : t
1414
end
1515

16+
module Dune_file : sig
17+
(** Representation of of valid dune lang values for the library [kind]
18+
field *)
19+
type t =
20+
| Normal
21+
| Ppx_deriver of Ppx_args.t
22+
| Ppx_rewriter of Ppx_args.t
23+
24+
(** [cstr_name k] is the name of the kind constructor for [k] in dune lang.
25+
E.g., [cstr_name Normal = "normal"] *)
26+
val cstr_name : t -> string
27+
28+
include Conv.S with type t := t
29+
end
30+
31+
(** Internal representation the of the possible kinds of libraries *)
1632
type t =
17-
| Normal
18-
| Ppx_deriver of Ppx_args.t
19-
| Ppx_rewriter of Ppx_args.t
33+
| Virtual
2034
| Parameter
35+
| Dune_file of Dune_file.t
36+
(** A kind which is represented explicitly in the [kind] field of a dune
37+
library stanza.
38+
39+
The remaining variants are derived from other fields in the library
40+
stanza. *)
2141

2242
val to_dyn : t Dyn.builder
23-
val equal : t -> t -> bool
2443

2544
include Conv.S with type t := t

src/dune_rules/dune_package.ml

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,6 @@ module Lib = struct
105105
let melange_runtime_deps = additional_paths (Lib_info.melange_runtime_deps info) in
106106
let jsoo_runtime = Lib_info.jsoo_runtime info in
107107
let wasmoo_runtime = Lib_info.wasmoo_runtime info in
108-
let virtual_ = Lib_info.virtual_ info in
109108
let instrumentation_backend = Lib_info.instrumentation_backend info in
110109
let native_archives =
111110
match Lib_info.native_archives info with
@@ -124,7 +123,6 @@ module Lib = struct
124123
record_fields
125124
@@ [ field "name" Lib_name.encode name
126125
; field "kind" Lib_kind.encode kind
127-
; field_b "virtual" virtual_
128126
; field_o "synopsis" string synopsis
129127
; field_o "orig_src_dir" path orig_src_dir
130128
; mode_paths "archives" archives
@@ -192,7 +190,20 @@ module Lib = struct
192190
let+ synopsis = field_o "synopsis" string
193191
and+ loc = loc
194192
and+ modes = field_l "modes" Lib_mode.decode
195-
and+ kind = field "kind" Lib_kind.decode
193+
and+ kind =
194+
let* kind = field "kind" Lib_kind.decode in
195+
let+ virtual_ = field_b "virtual" in
196+
match kind with
197+
| (Dune_file Normal | Virtual) when virtual_ ->
198+
(* Backward compatible support for dune-project files
199+
that include the [(virtual)] field. *)
200+
Lib_kind.Virtual
201+
| incompatible_kind when virtual_ ->
202+
Code_error.raise
203+
"invalid combination of 'kind' and 'virtual' fields in library stanza of \
204+
dune-package file"
205+
[ "kind", Lib_kind.to_dyn incompatible_kind; "virtual", Dyn.Bool virtual_ ]
206+
| otherwise -> otherwise
196207
and+ archives = mode_paths "archives"
197208
and+ plugins = mode_paths "plugins"
198209
and+ foreign_objects = paths "foreign_objects"
@@ -218,7 +229,6 @@ module Lib = struct
218229
and+ melange_runtime_deps = paths "melange_runtime_deps"
219230
and+ requires = field_l "requires" (Lib_dep.decode ~allow_re_export:true)
220231
and+ ppx_runtime_deps = libs "ppx_runtime_deps"
221-
and+ virtual_ = field_b "virtual"
222232
and+ sub_systems = Sub_system_info.record_parser
223233
and+ orig_src_dir = field_o "orig_src_dir" path
224234
and+ modules = field "modules" (Modules.decode ~src_dir:base)
@@ -284,7 +294,6 @@ module Lib = struct
284294
~enabled
285295
~virtual_deps
286296
~dune_version
287-
~virtual_
288297
~entry_modules
289298
~implements
290299
~default_implementation

src/dune_rules/dune_package.mli

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
(** Representation of dune-package files *)
1+
(** Representation of dune-package files.
2+
3+
dune-package files record package data for the purposes of installation.
4+
They are not intended to be written or read by human users. *)
25

36
open Import
47

src/dune_rules/findlib.ml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,6 @@ let to_dune_library (t : Findlib.Package.t) ~dir_contents ~ext_lib ~external_loc
168168
let wasmoo_runtime = Findlib.Package.wasmoo_runtime t in
169169
let melange_runtime_deps = Lib_info.File_deps.External [] in
170170
let preprocess = Preprocess.Per_module.no_preprocessing () in
171-
let virtual_ = false in
172171
let default_implementation = None in
173172
let wrapped = None in
174173
let foreign_archives, native_archives =
@@ -268,7 +267,6 @@ let to_dune_library (t : Findlib.Package.t) ~dir_contents ~ext_lib ~external_loc
268267
~enabled
269268
~virtual_deps
270269
~dune_version
271-
~virtual_
272270
~entry_modules
273271
~implements
274272
~default_implementation

src/dune_rules/gen_meta.ml

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -76,8 +76,8 @@ let gen_lib pub_name lib ~version =
7676
in
7777
let preds =
7878
match kind with
79-
| Normal | Parameter -> []
80-
| Ppx_rewriter _ | Ppx_deriver _ -> [ Pos "ppx_driver" ]
79+
| Virtual | Parameter | Dune_file Normal -> []
80+
| Dune_file (Ppx_rewriter _ | Ppx_deriver _) -> [ Pos "ppx_driver" ]
8181
in
8282
let name lib =
8383
let name = Lib.name lib in
@@ -127,8 +127,8 @@ let gen_lib pub_name lib ~version =
127127
; ppx_runtime_deps ppx_rt_deps
128128
])
129129
; (match kind with
130-
| Normal | Parameter -> []
131-
| Ppx_rewriter _ | Ppx_deriver _ ->
130+
| Virtual | Parameter | Dune_file Normal -> []
131+
| Dune_file (Ppx_rewriter _ | Ppx_deriver _) ->
132132
(* Deprecated ppx method support *)
133133
let no_ppx_driver = Neg "ppx_driver"
134134
and no_custom_ppx = Neg "custom_ppx" in
@@ -139,12 +139,12 @@ let gen_lib pub_name lib ~version =
139139
; requires ~preds:[ no_ppx_driver ] ppx_runtime_deps_for_deprecated_method
140140
]
141141
; (match kind with
142-
| Normal | Parameter -> assert false
143-
| Ppx_rewriter _ ->
142+
| Virtual | Parameter | Dune_file Normal -> assert false
143+
| Dune_file (Ppx_rewriter _) ->
144144
[ rule "ppx" [ no_ppx_driver; no_custom_ppx ] Set "./ppx.exe --as-ppx"
145145
; rule "library_kind" [] Set "ppx_rewriter"
146146
]
147-
| Ppx_deriver _ ->
147+
| Dune_file (Ppx_deriver _) ->
148148
[ rule "requires" [ no_ppx_driver; no_custom_ppx ] Add "ppx_deriving"
149149
; rule
150150
"ppxopt"

src/dune_rules/install_rules.ml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,8 +79,8 @@ module Stanzas_to_entries : sig
7979
end = struct
8080
let lib_ppxs ctx ~scope ~(lib : Library.t) =
8181
match lib.kind with
82-
| Normal | Parameter | Ppx_deriver _ -> Memo.return []
83-
| Ppx_rewriter _ ->
82+
| Virtual | Parameter | Dune_file (Normal | Ppx_deriver _) -> Memo.return []
83+
| Dune_file (Ppx_rewriter _) ->
8484
Library.best_name lib
8585
|> Ppx_driver.ppx_exe ctx ~scope
8686
|> Resolve.Memo.read_memo

src/dune_rules/lib.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1454,7 +1454,7 @@ end = struct
14541454
let open Resolve.O in
14551455
let* lib = lib in
14561456
(match allow_only_ppx_deps, Lib_info.kind lib.info with
1457-
| true, Normal -> Error.only_ppx_deps_allowed ~loc lib.info
1457+
| true, Dune_file Normal -> Error.only_ppx_deps_allowed ~loc lib.info
14581458
| _ -> Resolve.return (Some lib)))
14591459
>>= linking_closure_with_overlap_checks None ~forbidden_libraries:Map.empty
14601460
in

src/dune_rules/lib_info.ml

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -324,7 +324,6 @@ type 'path t =
324324
; virtual_deps : (Loc.t * Lib_name.t) list
325325
; dune_version : Dune_lang.Syntax.Version.t option
326326
; sub_systems : Sub_system_info.t Sub_system_name.Map.t
327-
; virtual_ : bool
328327
; entry_modules : (Module_name.t list, User_message.t) result Source.t
329328
; implements : (Loc.t * Lib_name.t) option
330329
; default_implementation : (Loc.t * Lib_name.t) option
@@ -368,7 +367,7 @@ let status t = t.status
368367
let kind t = t.kind
369368
let default_implementation t = t.default_implementation
370369
let obj_dir t = t.obj_dir
371-
let virtual_ t = t.virtual_
370+
let virtual_ t = t.kind = Virtual
372371
let is_parameter t = t.kind = Parameter
373372
let implements t = t.implements
374373
let synopsis t = t.synopsis
@@ -420,7 +419,6 @@ let create
420419
~enabled
421420
~virtual_deps
422421
~dune_version
423-
~virtual_
424422
~entry_modules
425423
~implements
426424
~default_implementation
@@ -460,7 +458,6 @@ let create
460458
; virtual_deps
461459
; dune_version
462460
; sub_systems
463-
; virtual_
464461
; entry_modules
465462
; implements
466463
; default_implementation
@@ -557,7 +554,6 @@ let to_dyn
557554
; virtual_deps
558555
; dune_version
559556
; sub_systems
560-
; virtual_
561557
; implements
562558
; default_implementation
563559
; modes
@@ -598,7 +594,6 @@ let to_dyn
598594
; "virtual_deps", list (snd Lib_name.to_dyn) virtual_deps
599595
; "dune_version", option Dune_lang.Syntax.Version.to_dyn dune_version
600596
; "sub_systems", Sub_system_name.Map.to_dyn Dyn.opaque sub_systems
601-
; "virtual_", bool virtual_
602597
; ( "entry_modules"
603598
, Source.to_dyn
604599
(Result.to_dyn (list Module_name.to_dyn) string)

0 commit comments

Comments
 (0)