-
Notifications
You must be signed in to change notification settings - Fork 414
/
Copy pathdune_file.ml
2406 lines (2196 loc) · 77.8 KB
/
dune_file.ml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
open Import
open Dune_lang.Decoder
(* This file defines Dune types as well as the S-expression syntax for the
various supported versions of the specification. *)
(* Deprecated *)
module Jbuild_version = struct
type t = V1
let decode = enum [ ("1", V1) ]
end
let () =
Dune_project.Extension.register_deleted ~name:"library_variants"
~deleted_in:(2, 6)
module Lint = struct
type t = Preprocess.Without_instrumentation.t Preprocess.Per_module.t
let decode = Preprocess.Per_module.decode
let default = Preprocess.Per_module.default ()
let no_lint = default
end
type for_ =
| Executable
| Library of Wrapped.t option
module Buildable = struct
type t =
{ loc : Loc.t
; modules : Stanza_common.Modules_settings.t
; empty_module_interface_if_absent : bool
; libraries : Lib_dep.t list
; foreign_archives : (Loc.t * Foreign.Archive.t) list
; extra_objects : Foreign.Objects.t
; foreign_stubs : Foreign.Stubs.t list
; preprocess : Preprocess.With_instrumentation.t Preprocess.Per_module.t
; preprocessor_deps : Dep_conf.t list
; lint : Preprocess.Without_instrumentation.t Preprocess.Per_module.t
; flags : Ocaml_flags.Spec.t
; js_of_ocaml : Js_of_ocaml.In_buildable.t
; allow_overlapping_dependencies : bool
; ctypes : Ctypes_field.t option
}
let decode (for_ : for_) =
let use_foreign =
Dune_lang.Syntax.deleted_in Stanza.syntax (2, 0)
~extra_info:"Use the (foreign_stubs ...) field instead."
in
let only_in_library decode =
match for_ with
| Executable -> return None
| Library _ -> decode
in
let add_stubs language ~loc ~names ~flags foreign_stubs =
match names with
| None -> foreign_stubs
| Some names ->
let names = Ordered_set_lang.replace_standard_with_empty names in
let flags =
Option.value ~default:Ordered_set_lang.Unexpanded.standard flags
in
Foreign.Stubs.make ~loc ~language ~names ~mode:Mode.Select.All ~flags
:: foreign_stubs
in
let+ loc = loc
and+ preprocess, preprocessor_deps = Stanza_common.preprocess_fields
and+ lint = field "lint" Lint.decode ~default:Lint.default
and+ foreign_stubs =
multi_field "foreign_stubs"
(Dune_lang.Syntax.since Stanza.syntax (2, 0) >>> Foreign.Stubs.decode)
and+ foreign_archives =
field_o "foreign_archives"
(Dune_lang.Syntax.since Stanza.syntax (2, 0)
>>> repeat (located Foreign.Archive.decode))
and+ extra_objects =
field_o "extra_objects"
(Dune_lang.Syntax.since Stanza.syntax (3, 5) >>> Foreign.Objects.decode)
and+ c_flags =
only_in_library
(field_o "c_flags" (use_foreign >>> Ordered_set_lang.Unexpanded.decode))
and+ cxx_flags =
only_in_library
(field_o "cxx_flags"
(use_foreign >>> Ordered_set_lang.Unexpanded.decode))
and+ c_names_loc, c_names =
located
(only_in_library
(field_o "c_names" (use_foreign >>> Ordered_set_lang.decode)))
and+ cxx_names_loc, cxx_names =
located
(only_in_library
(field_o "cxx_names" (use_foreign >>> Ordered_set_lang.decode)))
and+ modules = Stanza_common.Modules_settings.decode
and+ self_build_stubs_archive_loc, self_build_stubs_archive =
located
(only_in_library
(field ~default:None "self_build_stubs_archive"
(Dune_lang.Syntax.deleted_in Stanza.syntax (2, 0)
~extra_info:"Use the (foreign_archives ...) field instead."
>>> enter (maybe string))))
and+ libraries =
let allow_re_export =
match for_ with
| Library _ -> true
| Executable -> false
in
field "libraries" (Lib_dep.L.decode ~allow_re_export) ~default:[]
and+ flags = Ocaml_flags.Spec.decode
and+ js_of_ocaml =
field "js_of_ocaml" Js_of_ocaml.In_buildable.decode
~default:Js_of_ocaml.In_buildable.default
and+ allow_overlapping_dependencies =
field_b "allow_overlapping_dependencies"
and+ version = Dune_lang.Syntax.get_exn Stanza.syntax
and+ ctypes =
field_o "ctypes"
(Dune_lang.Syntax.since Ctypes_field.syntax (0, 1)
>>> Ctypes_field.decode)
and+ loc_instrumentation, instrumentation = Stanza_common.instrumentation
and+ empty_module_interface_if_absent =
field_b "empty_module_interface_if_absent"
~check:(Dune_lang.Syntax.since Stanza.syntax (3, 0))
in
let preprocess =
let init =
let f libname = Preprocess.With_instrumentation.Ordinary libname in
Module_name.Per_item.map preprocess ~f:(Preprocess.map ~f)
in
List.fold_left instrumentation ~init
~f:(fun accu ((backend, flags), deps) ->
Preprocess.Per_module.add_instrumentation accu
~loc:loc_instrumentation ~flags ~deps backend)
in
let foreign_stubs =
foreign_stubs
|> add_stubs C ~loc:c_names_loc ~names:c_names ~flags:c_flags
|> add_stubs Cxx ~loc:cxx_names_loc ~names:cxx_names ~flags:cxx_flags
in
let libraries =
let ctypes_libraries =
if Option.is_none ctypes then []
else Ctypes_stubs.libraries_needed_for_ctypes ~loc:Loc.none
in
libraries @ ctypes_libraries
in
let foreign_archives =
let foreign_archives = Option.value ~default:[] foreign_archives in
if
version < (2, 0)
&& List.is_non_empty foreign_stubs
&& Option.is_some self_build_stubs_archive
then
User_error.raise ~loc:self_build_stubs_archive_loc
[ Pp.concat
[ Pp.textf "A library cannot use "
; Pp.hbox (Pp.textf "(self_build_stubs_archive ...)")
; Pp.textf " and "
; Pp.hbox (Pp.textf "(c_names ...)")
; Pp.textf " simultaneously. This is supported starting from "
; Pp.hbox (Pp.textf "Dune 2.0.")
]
]
else
match self_build_stubs_archive with
| None -> foreign_archives
(* Note: we add "_stubs" to the name, since [self_build_stubs_archive]
used this naming convention; [foreign_archives] does not use it and
allows users to name archives as they like (they still need to add
the "lib" prefix, however, since standard linkers require it). *)
| Some name -> (loc, Foreign.Archive.stubs name) :: foreign_archives
in
let extra_objects =
Option.value ~default:Foreign.Objects.empty extra_objects
in
{ loc
; preprocess
; preprocessor_deps
; lint
; modules
; empty_module_interface_if_absent
; foreign_stubs
; foreign_archives
; extra_objects
; libraries
; flags
; js_of_ocaml
; allow_overlapping_dependencies
; ctypes
}
let has_foreign t =
List.is_non_empty t.foreign_stubs
|| List.is_non_empty t.foreign_archives
|| (not (Foreign.Objects.is_empty t.extra_objects))
|| Option.is_some t.ctypes
let has_foreign_cxx t =
List.exists
~f:(fun stub -> Foreign_language.(equal Cxx stub.Foreign.Stubs.language))
t.foreign_stubs
let has_mode_dependent_foreign_stubs t =
List.exists ~f:Foreign.Stubs.is_mode_dependent t.foreign_stubs
end
module Public_lib = struct
type t =
{ name : Loc.t * Lib_name.t
; package : Package.t
; sub_dir : string option
}
let sub_dir t = t.sub_dir
let loc t = fst t.name
let name t = snd t.name
let package t = t.package
(** if [~allow_deprecated_names] is set, then we allow the package name to be
attached to one of the deprecated packages *)
let make ~allow_deprecated_names project ((_, s) as loc_name) =
let pkg, rest = Lib_name.split s in
let x =
if not allow_deprecated_names then None
else
Dune_project.packages project
|> Package.Name.Map.values
|> List.find_map
~f:(fun ({ Package.deprecated_package_names; _ } as package) ->
if Package.Name.Map.mem deprecated_package_names pkg then
Some { package; sub_dir = None; name = loc_name }
else None)
in
match x with
| Some x -> Ok x
| None ->
Stanza_common.Pkg.resolve project pkg
|> Result.map ~f:(fun pkg ->
{ package = pkg
; sub_dir =
(if rest = [] then None
else Some (String.concat rest ~sep:"/"))
; name = loc_name
})
let decode ~allow_deprecated_names =
map_validate
(let+ project = Dune_project.get_exn ()
and+ loc_name = located Lib_name.decode in
(project, loc_name))
~f:(fun (project, loc_name) ->
make ~allow_deprecated_names project loc_name)
end
module Mode_conf = struct
module T = struct
type t =
| Byte
| Native
| Best
let all = [ Byte; Native; Best ]
let compare x y =
match (x, y) with
| Byte, Byte -> Eq
| Byte, _ -> Lt
| _, Byte -> Gt
| Native, Native -> Eq
| Native, _ -> Lt
| _, Native -> Gt
| Best, Best -> Eq
end
include T
let decode = enum [ ("byte", Byte); ("native", Native); ("best", Best) ]
let to_string = function
| Byte -> "byte"
| Native -> "native"
| Best -> "best"
let to_dyn t = Dyn.variant (to_string t) []
let encode t = Dune_lang.atom (to_string t)
module Kind = struct
type t =
| Inherited
| Requested of Loc.t
end
module Map = struct
type nonrec 'a t =
{ byte : 'a
; native : 'a
; best : 'a
}
let find t = function
| Byte -> t.byte
| Native -> t.native
| Best -> t.best
let update t key ~f =
match key with
| Byte -> { t with byte = f t.byte }
| Native -> { t with native = f t.native }
| Best -> { t with best = f t.best }
let make_one x = { byte = x; native = x; best = x }
end
type mode_conf = t
module Set = struct
type nonrec t = Kind.t option Map.t
let empty : t = Map.make_one None
let of_list (input : (mode_conf * Kind.t) list) : t =
List.fold_left ~init:empty input ~f:(fun acc (key, kind) ->
Map.update acc key ~f:(function
| None -> Some kind
| Some (Kind.Requested loc) ->
User_error.raise ~loc [ Pp.textf "already configured" ]
| Some Inherited ->
(* this doesn't happen as inherited can't be manually specified *)
assert false))
let to_list (t : t) : (mode_conf * Kind.t) list =
let get mode_conf =
match Map.find t mode_conf with
| None -> None
| Some k -> Some (mode_conf, k)
in
List.filter_map ~f:get all
let decode =
let decode =
let+ loc, t = located decode in
(t, Kind.Requested loc)
in
repeat decode >>| of_list
let default loc : t =
{ empty with byte = Some Inherited; best = Some (Requested loc) }
module Details = struct
type t = Kind.t option
let validate t ~if_ = if if_ then t else None
let ( ||| ) x y = if Option.is_some x then x else y
end
let eval_detailed t ~has_native =
let exists = function
| Best | Byte -> true
| Native -> has_native
in
let get key : Details.t =
match Map.find t key with
| None -> None
| Some Kind.Inherited -> Option.some_if (exists key) Kind.Inherited
| Some (Kind.Requested loc) ->
(* TODO always true for now, but we should delay this error *)
let exists =
exists key
|| User_error.raise ~loc [ Pp.text "this mode isn't available" ]
in
Option.some_if exists (Kind.Requested loc)
in
let best_mode = if has_native then Native else Byte in
let best = get Best in
let open Details in
let byte = get Byte ||| validate best ~if_:(best_mode = Byte) in
let native = get Native ||| validate best ~if_:(best_mode = Native) in
{ Mode.Dict.byte; native }
let eval t ~has_native =
eval_detailed t ~has_native |> Mode.Dict.map ~f:Option.is_some
end
module Lib = struct
type t =
| Ocaml of mode_conf
| Melange
let decode =
enum'
[ ("byte", return @@ Ocaml Byte)
; ("native", return @@ Ocaml Native)
; ("best", return @@ Ocaml Best)
; ( "melange"
, Dune_lang.Syntax.since Melange_stanzas.syntax (0, 1)
>>> return Melange )
]
let to_string = function
| Ocaml Byte -> "byte"
| Ocaml Native -> "native"
| Ocaml Best -> "best"
| Melange -> "melange"
let to_dyn t = Dyn.variant (to_string t) []
let equal x y =
match (x, y) with
| Ocaml o1, Ocaml o2 -> (
match compare o1 o2 with
| Eq -> true
| _ -> false)
| Ocaml _, _ | _, Ocaml _ -> false
| Melange, Melange -> true
module Map = struct
type nonrec 'a t =
{ ocaml : 'a Map.t
; melange : 'a
}
let find t = function
| Ocaml a -> Map.find t.ocaml a
| Melange -> t.melange
let update t key ~f =
match key with
| Ocaml key -> { t with ocaml = Map.update t.ocaml key ~f }
| Melange -> { t with melange = f t.melange }
let make_one x = { ocaml = Map.make_one x; melange = x }
end
module Set = struct
type mode_conf = t
type nonrec t = Kind.t option Map.t
let empty : t = Map.make_one None
let of_list (input : (mode_conf * Kind.t) list) : t =
List.fold_left ~init:empty input ~f:(fun acc (key, kind) ->
Map.update acc key ~f:(function
| None -> Some kind
| Some (Kind.Requested loc) ->
User_error.raise ~loc [ Pp.textf "already configured" ]
| Some Inherited ->
(* this doesn't happen as inherited can't be manually specified *)
assert false))
let decode_osl ~stanza_loc project =
let+ modes = Ordered_set_lang.decode in
let standard =
Set.default stanza_loc |> Set.to_list
|> List.map ~f:(fun (m, k) -> (Ocaml m, k))
in
Ordered_set_lang.eval modes ~standard
~eq:(fun (a, _) (b, _) -> equal a b)
~parse:(fun ~loc s ->
let mode =
Dune_lang.Decoder.parse
(Dune_project.set_parsing_context project decode)
Univ_map.empty
(Atom (loc, Dune_lang.Atom.of_string s))
in
(mode, Kind.Requested loc))
|> of_list
let decode =
let decode =
let+ loc, t = located decode in
(t, Kind.Requested loc)
in
repeat decode >>| of_list
let default loc : t = { empty with ocaml = Set.default loc }
module Details = struct
type t = Kind.t option
end
let eval_detailed t ~has_native =
let get key : Details.t = Map.find t key in
let melange = get Melange in
{ Lib_mode.Map.ocaml = Set.eval_detailed t.ocaml ~has_native; melange }
let eval t ~has_native =
eval_detailed t ~has_native |> Lib_mode.Map.map ~f:Option.is_some
end
end
end
module Library = struct
module Wrapped = struct
include Wrapped
let default = Simple true
let make ~wrapped ~implements ~special_builtin_support :
t Lib_info.Inherited.t =
(match (wrapped, special_builtin_support) with
| Some (loc, Yes_with_transition _), Some (_loc, _) ->
(* TODO use _loc *)
User_error.raise ~loc
[ Pp.text
"Cannot have transition modules for libraries with special \
builtin support"
]
| _, _ -> ());
match (wrapped, implements) with
| None, None -> This default
| None, Some w -> From w
| Some (_loc, w), None -> This w
| Some (loc, _), Some _ ->
User_error.raise ~loc
[ Pp.text
"Wrapped cannot be set for implementations. It is inherited from \
the virtual library."
]
let field = field_o "wrapped" (located decode)
end
module Modes = struct
let decode ~stanza_loc ~dune_version project =
let expected_version = (3, 8) in
if dune_version >= expected_version then
Mode_conf.Lib.Set.decode_osl ~stanza_loc project
else
(* Old behavior: if old parser succeeds, return that. Otherwise, if
parsing the ordered set language succeeds, ask the user to upgrade to
a supported version. Otherwise, fail with the first error. *)
try_
(Mode_conf.Lib.Set.decode >>| fun modes -> `Modes modes)
(fun exn ->
try_
( Mode_conf.Lib.Set.decode_osl ~stanza_loc project >>| fun modes ->
if dune_version >= expected_version then `Modes modes
else `Upgrade )
(fun _ -> raise exn))
>>| function
| `Modes modes -> modes
| `Upgrade ->
Syntax.Error.since stanza_loc Stanza.syntax expected_version
~what:"Ordered set language for modes"
end
type visibility =
| Public of Public_lib.t
| Private of Package.t option
type t =
{ name : Loc.t * Lib_name.Local.t
; visibility : visibility
; synopsis : string option
; install_c_headers : (Loc.t * string) list
; public_headers : Loc.t * Dep_conf.t list
; ppx_runtime_libraries : (Loc.t * Lib_name.t) list
; modes : Mode_conf.Lib.Set.t
; kind : Lib_kind.t
; library_flags : Ordered_set_lang.Unexpanded.t
; c_library_flags : Ordered_set_lang.Unexpanded.t
; virtual_deps : (Loc.t * Lib_name.t) list
; wrapped : Wrapped.t Lib_info.Inherited.t
; optional : bool
; buildable : Buildable.t
; dynlink : Dynlink_supported.t
; project : Dune_project.t
; sub_systems : Sub_system_info.t Sub_system_name.Map.t
; dune_version : Dune_lang.Syntax.Version.t
; virtual_modules : Ordered_set_lang.t option
; implements : (Loc.t * Lib_name.t) option
; default_implementation : (Loc.t * Lib_name.t) option
; private_modules : Ordered_set_lang.t option
; stdlib : Ocaml_stdlib.t option
; special_builtin_support :
(Loc.t * Lib_info.Special_builtin_support.t) option
; enabled_if : Blang.t
; instrumentation_backend : (Loc.t * Lib_name.t) option
; melange_runtime_deps : Loc.t * Dep_conf.t list
}
let decode =
fields
(let* stanza_loc = loc in
let* wrapped = Wrapped.field in
let* dune_version = Dune_lang.Syntax.get_exn Stanza.syntax in
let* project = Dune_project.get_exn () in
let+ buildable = Buildable.decode (Library (Option.map ~f:snd wrapped))
and+ name = field_o "name" Lib_name.Local.decode_loc
and+ public =
field_o "public_name" (Public_lib.decode ~allow_deprecated_names:false)
and+ synopsis = field_o "synopsis" string
and+ install_c_headers =
field "install_c_headers" (repeat (located string)) ~default:[]
and+ public_headers =
field "public_headers"
(Dune_lang.Syntax.since Stanza.syntax (3, 8)
>>> located (repeat Dep_conf.decode_no_files))
~default:(stanza_loc, [])
and+ ppx_runtime_libraries =
field "ppx_runtime_libraries"
(repeat (located Lib_name.decode))
~default:[]
and+ library_flags = Ordered_set_lang.Unexpanded.field "library_flags"
and+ c_library_flags =
Ordered_set_lang.Unexpanded.field "c_library_flags"
and+ virtual_deps =
field "virtual_deps" (repeat (located Lib_name.decode)) ~default:[]
and+ modes =
field "modes"
(Modes.decode ~stanza_loc ~dune_version project)
~default:(Mode_conf.Lib.Set.default stanza_loc)
and+ kind = field "kind" Lib_kind.decode ~default:Lib_kind.Normal
and+ optional = field_b "optional"
and+ no_dynlink = field_b "no_dynlink"
and+ () =
let check =
let+ loc = loc in
let is_error = dune_version >= (2, 0) in
User_warning.emit ~loc ~is_error
[ Pp.text "no_keep_locs is a no-op. Please delete it." ]
in
let+ _ = field_b "no_keep_locs" ~check in
()
and+ sub_systems =
let* () = return () in
Sub_system_info.record_parser ()
and+ virtual_modules =
field_o "virtual_modules"
(Dune_lang.Syntax.since Stanza.syntax (1, 7)
>>> Ordered_set_lang.decode)
and+ implements =
field_o "implements"
(Dune_lang.Syntax.since Stanza.syntax (1, 7)
>>> located Lib_name.decode)
and+ default_implementation =
field_o "default_implementation"
(Dune_lang.Syntax.since Stanza.syntax (2, 6)
>>> located Lib_name.decode)
and+ private_modules =
field_o "private_modules"
(let* () = Dune_lang.Syntax.since Stanza.syntax (1, 2) in
Ordered_set_lang.decode)
and+ stdlib =
field_o "stdlib"
(Dune_lang.Syntax.since Ocaml_stdlib.syntax (0, 1)
>>> Ocaml_stdlib.decode)
and+ special_builtin_support =
field_o "special_builtin_support"
(Dune_lang.Syntax.since Stanza.syntax (1, 10)
>>> located Lib_info.Special_builtin_support.decode)
and+ enabled_if =
let open Enabled_if in
let allowed_vars = Only Lib_config.allowed_in_enabled_if in
decode ~allowed_vars ~since:(Some (1, 10)) ()
and+ instrumentation_backend =
field_o "instrumentation.backend"
(Dune_lang.Syntax.since Stanza.syntax (2, 7)
>>> fields (field "ppx" (located Lib_name.decode)))
and+ package =
field_o "package"
(Dune_lang.Syntax.since Stanza.syntax (2, 8)
>>> located Stanza_common.Pkg.decode)
and+ melange_runtime_deps =
field "melange.runtime_deps"
(Dune_lang.Syntax.since Melange_stanzas.syntax (0, 1)
>>> located (repeat Dep_conf.decode))
~default:(stanza_loc, [])
in
let wrapped =
Wrapped.make ~wrapped ~implements ~special_builtin_support
in
let name =
let open Dune_lang.Syntax.Version.Infix in
match (name, public) with
| Some (loc, res), _ -> (loc, res)
| None, Some { name = loc, name; _ } ->
if dune_version >= (1, 1) then
match Lib_name.to_local (loc, name) with
| Ok m -> (loc, m)
| Error user_message ->
User_error.raise ~loc
[ Pp.textf "Invalid library name."
; Pp.text
"Public library names don't have this restriction. You \
can either change this public name to be a valid library \
name or add a \"name\" field with a valid library name."
]
~hints:(Lib_name.Local.valid_format_doc :: user_message.hints)
else
User_error.raise ~loc
[ Pp.text
"name field cannot be omitted before version 1.1 of the \
dune language"
]
| None, None ->
User_error.raise ~loc:stanza_loc
[ Pp.text
(if dune_version >= (1, 1) then
"supply at least one of name or public_name fields"
else "name field is missing")
]
in
let visibility =
match (public, package) with
| None, None -> Private None
| Some public, None -> Public public
| None, Some (_loc, package) -> Private (Some package)
| Some public, Some (loc, _) ->
User_error.raise ~loc
[ Pp.textf
"This library has a public_name, it already belongs to the \
package %s"
(Package.Name.to_string (Package.name public.package))
]
in
Option.both virtual_modules implements
|> Option.iter ~f:(fun (virtual_modules, (_, impl)) ->
User_error.raise
~loc:(Ordered_set_lang.loc virtual_modules |> Option.value_exn)
[ Pp.textf "A library cannot be both virtual and implement %s"
(Lib_name.to_string impl)
]);
(match (virtual_modules, default_implementation) with
| None, Some (loc, _) ->
User_error.raise ~loc
[ Pp.text
"Only virtual libraries can specify a default implementation."
]
| _ -> ());
{ name
; visibility
; synopsis
; install_c_headers
; public_headers
; ppx_runtime_libraries
; modes
; kind
; library_flags
; c_library_flags
; virtual_deps
; wrapped
; optional
; buildable
; dynlink = Dynlink_supported.of_bool (not no_dynlink)
; project
; sub_systems
; dune_version
; virtual_modules
; implements
; default_implementation
; private_modules
; stdlib
; special_builtin_support
; enabled_if
; instrumentation_backend
; melange_runtime_deps
})
let package t =
match t.visibility with
| Public p -> Some p.package
| Private p -> p
let sub_dir t =
match t.visibility with
| Public p -> p.sub_dir
| Private None -> None
| Private (Some _) ->
Lib_name.Local.mangled_path_under_package (snd t.name)
|> String.concat ~sep:"/" |> Option.some
let has_foreign t = Buildable.has_foreign t.buildable
let has_foreign_cxx t = Buildable.has_foreign_cxx t.buildable
let stubs_archive t =
if
List.is_empty t.buildable.foreign_stubs
&& Option.is_none t.buildable.ctypes
then None
else Some (Foreign.Archive.stubs (Lib_name.Local.to_string (snd t.name)))
let foreign_archives t = List.map ~f:snd t.buildable.foreign_archives
(* This function returns archives files for a given library and mode:
- For "all" modes it returns:
- the foreign archives (which are always not mode-dependent)
- the lib's stubs archive if they are not mode-dependent
- For a specific mode "m" it returns:
- the lib's stubs archive for that mode if they are mode-dependent
*)
let foreign_lib_files t ~dir ~ext_lib ~for_mode =
let stubs_archive = stubs_archive t in
let foreign_archives = foreign_archives t in
let stubs_are_mode_dependent =
Buildable.has_mode_dependent_foreign_stubs t.buildable
in
let lib_file ~for_mode archive =
Foreign.Archive.lib_file ~archive ~dir ~ext_lib ~mode:for_mode
in
let stubs_archive =
Option.bind stubs_archive ~f:(fun archive ->
match (stubs_are_mode_dependent, for_mode) with
| false, Mode.Select.All | true, Only _ ->
Some (lib_file ~for_mode archive)
| _ -> None)
in
if for_mode = Mode.Select.All then
let foreign_archives =
(* Stubs, and thus the lib archives can have mode-dependent versions, but
right now foreign archives cannot *)
List.map foreign_archives ~f:(lib_file ~for_mode)
in
Option.to_list stubs_archive @ foreign_archives
else Option.to_list stubs_archive
let foreign_dll_files t ~dir ~ext_dll =
let stubs_archive = stubs_archive t in
let foreign_archives = foreign_archives t in
let mode =
if Buildable.has_mode_dependent_foreign_stubs t.buildable then
(* Shared object are never created in Native mode where everything is
linked statically. *)
Mode.Select.Only Mode.Byte
else Mode.Select.All
in
let dll_file ~mode archive =
Foreign.Archive.dll_file ~archive ~dir ~ext_dll ~mode
in
let foreign_archives =
List.map foreign_archives ~f:(dll_file ~mode:Mode.Select.All)
in
(* Stubs can have mode-dependent versions, not foreign archives *)
match stubs_archive with
| Some stubs_archive -> dll_file ~mode stubs_archive :: foreign_archives
| None -> foreign_archives
let archive_basename t ~ext = Lib_name.Local.to_string (snd t.name) ^ ext
let archive t ~dir ~ext = Path.Build.relative dir (archive_basename t ~ext)
let best_name t =
match t.visibility with
| Private _ -> Lib_name.of_local t.name
| Public p -> snd p.name
let is_virtual t = Option.is_some t.virtual_modules
let is_impl t = Option.is_some t.implements
let obj_dir ~dir t =
let private_lib =
match t.visibility with
| Private (Some _) -> true
| Private None | Public _ -> false
in
Obj_dir.make_lib ~dir
~has_private_modules:
((* TODO instead of this fragile approximation, we should be looking at
[Modules.t] and deciding. Unfortunately, [Obj_dir.t] is currently
used in some places where [Modules.t] is not yet constructed. *)
t.private_modules <> None
|| t.buildable.modules.root_module <> None)
~private_lib (snd t.name)
let main_module_name t : Lib_info.Main_module_name.t =
match (t.implements, t.wrapped) with
| Some x, From _ -> From x
| Some _, This _ (* cannot specify for wrapped for implements *)
| None, From _ -> assert false (* cannot inherit for normal libs *)
| None, This (Simple false) -> (
match t.stdlib with
| None -> This None
| Some _ -> This (Some (Module_name.of_local_lib_name t.name)))
| None, This (Simple true | Yes_with_transition _) ->
This (Some (Module_name.of_local_lib_name t.name))
let to_lib_info conf ~dir
~lib_config:
({ Lib_config.has_native; ext_lib; ext_dll; natdynlink_supported; _ } as
lib_config) =
let open Memo.O in
let obj_dir = obj_dir ~dir conf in
let archive ?(dir = dir) ext = archive conf ~dir ~ext in
let modes = Mode_conf.Lib.Set.eval ~has_native conf.modes in
let archive_for_mode ~f_ext ~mode =
if Mode.Dict.get modes.ocaml mode then Some (archive (f_ext mode))
else None
in
let archives_for_mode ~f_ext =
Mode.Dict.of_func (fun ~mode ->
archive_for_mode ~f_ext ~mode |> Option.to_list)
in
let jsoo_runtime =
List.map conf.buildable.js_of_ocaml.javascript_files
~f:(Path.Build.relative dir)
in
let status =
match conf.visibility with
| Private pkg -> Lib_info.Status.Private (conf.project, pkg)
| Public p -> Public (conf.project, p.package)
in
let virtual_library = is_virtual conf in
let foreign_archives =
let init =
Mode.Map.Multi.create_for_all_modes
@@ foreign_lib_files conf ~dir ~ext_lib ~for_mode:All
in
Mode.Dict.foldi modes.ocaml ~init ~f:(fun mode enabled acc ->
if enabled then
let for_mode = Mode.Select.Only mode in
let libs = foreign_lib_files conf ~dir ~ext_lib ~for_mode in
Mode.Map.Multi.add_all acc for_mode libs
else acc)
in
let native_archives =
let archive = archive ext_lib in
if virtual_library || not modes.ocaml.native then Lib_info.Files []
else if
Option.is_some conf.implements
|| Lib_config.linker_can_create_empty_archives lib_config
&& Ocaml.Version.ocamlopt_always_calls_library_linker
lib_config.ocaml_version
then Lib_info.Files [ archive ]
else Lib_info.Needs_module_info archive
in
let foreign_dll_files = foreign_dll_files conf ~dir ~ext_dll in
let exit_module = Option.bind conf.stdlib ~f:(fun x -> x.exit_module) in
let virtual_ =
Option.map conf.virtual_modules ~f:(fun _ -> Lib_info.Source.Local)
in
let foreign_objects = Lib_info.Source.Local in
let archives, plugins =
if virtual_library then (Mode.Dict.make_both [], Mode.Dict.make_both [])
else
let plugins =
let archive_file ~mode =
archive_for_mode ~f_ext:Mode.plugin_ext ~mode |> Option.to_list
in
{ Mode.Dict.native =
(if Dynlink_supported.get conf.dynlink natdynlink_supported then
archive_file ~mode:Native
else [])
; byte = archive_file ~mode:Byte
}
in
(archives_for_mode ~f_ext:Mode.compiled_lib_ext, plugins)
in
let main_module_name = main_module_name conf in
let name = best_name conf in
let+ enabled =
let+ enabled_if_result =
Blang.eval conf.enabled_if ~dir:(Path.build dir)
~f:(fun ~source:_ pform ->
let value = Lib_config.get_for_enabled_if lib_config pform in
Memo.return [ Value.String value ])
in
if not enabled_if_result then
Lib_info.Enabled_status.Disabled_because_of_enabled_if
else if conf.optional then Optional
else Normal
in
let version =
match status with
| Public (_, pkg) -> pkg.version
| Installed_private | Installed | Private _ -> None
in
let requires = conf.buildable.libraries in
let loc = conf.buildable.loc in
let kind = conf.kind in
let src_dir = dir in
let orig_src_dir = None in
let synopsis = conf.synopsis in
let sub_systems = conf.sub_systems in
let ppx_runtime_deps = conf.ppx_runtime_libraries in
let preprocess = conf.buildable.preprocess in
let virtual_deps = conf.virtual_deps in
let dune_version = Some conf.dune_version in
let implements = conf.implements in
let default_implementation = conf.default_implementation in
let wrapped = Some conf.wrapped in
let special_builtin_support = conf.special_builtin_support in
let instrumentation_backend = conf.instrumentation_backend in
let entry_modules = Lib_info.Source.Local in
let melange_runtime_deps =
let loc, runtime_deps = conf.melange_runtime_deps in
Lib_info.File_deps.Local (loc, runtime_deps)
in