/
dune_file.ml
2080 lines (1873 loc) · 60.7 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! Stdune
open Import
open Stanza.Decoder
(* This file defines the jbuild types as well as the S-expression
syntax for the various supported version of the specification.
*)
(* Deprecated *)
module Jbuild_version = struct
type t =
| V1
let decode =
enum
[ "1", V1
]
end
let invalid_module_name ~loc =
of_sexp_errorf loc "invalid module name: %S"
let module_name =
plain_string (fun ~loc name ->
match name with
| "" -> invalid_module_name ~loc name
| s ->
try
(match s.[0] with
| 'A'..'Z' | 'a'..'z' -> ()
| _ -> raise_notrace Exit);
String.iter s ~f:(function
| 'A'..'Z' | 'a'..'z' | '0'..'9' | '\'' | '_' -> ()
| _ -> raise_notrace Exit);
Module.Name.of_string s
with Exit ->
invalid_module_name ~loc name)
let file =
plain_string (fun ~loc s ->
match s with
| "." | ".." ->
of_sexp_errorf loc "'.' and '..' are not valid filenames"
| fn -> fn)
let relative_file =
plain_string (fun ~loc fn ->
if Filename.is_relative fn then
fn
else
of_sexp_errorf loc "relative filename expected")
(* Parse and resolve "package" fields *)
module Pkg = struct
let listing packages =
let longest_pkg =
String.longest_map packages ~f:(fun p ->
Package.Name.to_string p.Package.name)
in
String.concat ~sep:"\n"
(List.map packages ~f:(fun pkg ->
sprintf "- %-*s (because of %s)" longest_pkg
(Package.Name.to_string pkg.Package.name)
(Path.to_string (Package.opam_file pkg))))
let default (project : Dune_project.t) stanza =
match Package.Name.Map.values (Dune_project.packages project) with
| [pkg] -> Ok pkg
| [] ->
Error
"The current project defines some public elements, \
but no opam packages are defined.\n\
Please add a <package>.opam file at the project root \
so that these elements are installed into it."
| _ :: _ :: _ ->
Error
(sprintf
"I can't determine automatically which package this \
stanza is for.\nI have the choice between these ones:\n\
%s\n\
You need to add a (package ...) field to this (%s) stanza."
(listing (Package.Name.Map.values (Dune_project.packages project)))
stanza)
let resolve (project : Dune_project.t) name =
let packages = Dune_project.packages project in
match Package.Name.Map.find packages name with
| Some pkg ->
Ok pkg
| None ->
let name_s = Package.Name.to_string name in
if Package.Name.Map.is_empty packages then
Error (sprintf
"You cannot declare items to be installed without \
adding a <package>.opam file at the root of your project.\n\
To declare elements to be installed as part of package %S, \
add a %S file at the root of your project."
name_s (Package.Name.opam_fn name))
else
Error (sprintf
"The current scope doesn't define package %S.\n\
The only packages for which you can declare \
elements to be installed in this directory are:\n\
%s%s"
name_s
(listing (Package.Name.Map.values packages))
(hint name_s (Package.Name.Map.keys packages
|> List.map ~f:Package.Name.to_string)))
let decode =
let%map p = Dune_project.get_exn ()
and (loc, name) = located Package.Name.decode in
match resolve p name with
| Ok x -> x
| Error e -> Errors.fail loc "%s" e
let field stanza =
map_validate
(let%map p = Dune_project.get_exn ()
and pkg = field_o "package" string in
(p, pkg))
~f:(fun (p, pkg) ->
match pkg with
| None -> default p stanza
| Some name -> resolve p (Package.Name.of_string name))
end
module Pps_and_flags = struct
module Jbuild_syntax = struct
let of_string ~loc s =
if String.is_prefix s ~prefix:"-" then
Right [s]
else
Left (loc, Lib_name.of_string_exn ~loc:(Some loc) s)
let item =
peek_exn >>= function
| Template { loc; _ } ->
no_templates loc "in the preprocessors field"
| Atom _ | Quoted_string _ -> plain_string of_string
| List _ -> list string >>| fun l -> Right l
let split l =
let pps, flags = List.partition_map l ~f:Fn.id in
(pps, List.concat flags)
let decode = list item >>| split
end
module Dune_syntax = struct
let decode =
let%map l, flags =
until_keyword "--"
~before:(plain_string (fun ~loc s -> (loc, s)))
~after:(repeat string)
in
let pps, more_flags =
List.partition_map l ~f:(fun (loc, s) ->
if String.is_prefix s ~prefix:"-" then
Right s
else
Left (loc, Lib_name.of_string_exn ~loc:(Some loc) s))
in
(pps, more_flags @ Option.value flags ~default:[])
end
let decode =
switch_file_kind
~jbuild:Jbuild_syntax.decode
~dune:Dune_syntax.decode
end
module Dep_conf = struct
type t =
| File of String_with_vars.t
| Alias of String_with_vars.t
| Alias_rec of String_with_vars.t
| Glob_files of String_with_vars.t
| Source_tree of String_with_vars.t
| Package of String_with_vars.t
| Universe
| Env_var of String_with_vars.t
let remove_locs = function
| File sw -> File (String_with_vars.remove_locs sw)
| Alias sw -> Alias (String_with_vars.remove_locs sw)
| Alias_rec sw -> Alias_rec (String_with_vars.remove_locs sw)
| Glob_files sw -> Glob_files (String_with_vars.remove_locs sw)
| Source_tree sw -> Source_tree (String_with_vars.remove_locs sw)
| Package sw -> Package (String_with_vars.remove_locs sw)
| Universe -> Universe
| Env_var sw -> Env_var sw
let decode =
let decode =
let sw = String_with_vars.decode in
sum
[ "file" , (sw >>| fun x -> File x)
; "alias" , (sw >>| fun x -> Alias x)
; "alias_rec" , (sw >>| fun x -> Alias_rec x)
; "glob_files" , (sw >>| fun x -> Glob_files x)
; "package" , (sw >>| fun x -> Package x)
; "universe" , return Universe
; "files_recursively_in",
(let%map () =
Syntax.renamed_in Stanza.syntax (1, 0) ~to_:"source_tree"
and x = sw in
Source_tree x)
; "source_tree",
(let%map () = Syntax.since Stanza.syntax (1, 0)
and x = sw in
Source_tree x)
; "env_var", (sw >>| fun x -> Env_var x)
]
in
if_list
~then_:decode
~else_:(String_with_vars.decode >>| fun x -> File x)
open Dune_lang
let encode = function
| File t ->
List [ Dune_lang.unsafe_atom_of_string "file"
; String_with_vars.encode t ]
| Alias t ->
List [ Dune_lang.unsafe_atom_of_string "alias"
; String_with_vars.encode t ]
| Alias_rec t ->
List [ Dune_lang.unsafe_atom_of_string "alias_rec"
; String_with_vars.encode t ]
| Glob_files t ->
List [ Dune_lang.unsafe_atom_of_string "glob_files"
; String_with_vars.encode t ]
| Source_tree t ->
List [ Dune_lang.unsafe_atom_of_string "files_recursively_in"
; String_with_vars.encode t ]
| Package t ->
List [ Dune_lang.unsafe_atom_of_string "package"
; String_with_vars.encode t]
| Universe ->
Dune_lang.unsafe_atom_of_string "universe"
| Env_var t ->
List [ Dune_lang.unsafe_atom_of_string "env_var"
; String_with_vars.encode t]
let to_sexp t = Dune_lang.to_sexp (encode t)
end
module Preprocess = struct
type pps =
{ loc : Loc.t
; pps : (Loc.t * Lib_name.t) list
; flags : string list
; staged : bool
}
type t =
| No_preprocessing
| Action of Loc.t * Action_dune_lang.t
| Pps of pps
let decode =
sum
[ "no_preprocessing", return No_preprocessing
; "action",
(located Action_dune_lang.decode >>| fun (loc, x) ->
Action (loc, x))
; "pps",
(let%map loc = loc
and pps, flags = Pps_and_flags.decode in
Pps { loc; pps; flags; staged = false })
; "staged_pps",
(let%map () = Syntax.since Stanza.syntax (1, 1)
and loc = loc
and pps, flags = Pps_and_flags.decode in
Pps { loc; pps; flags; staged = true })
]
let pps = function
| Pps { pps; _ } -> pps
| _ -> []
end
module Blang = struct
include Blang
let ops =
[ "=", Op.Eq
; ">=", Gte
; "<=", Lt
; ">", Gt
; "<", Lt
; "<>", Neq
]
let decode =
let ops =
List.map ops ~f:(fun (name, op) ->
( name
, (let%map x = String_with_vars.decode
and y = String_with_vars.decode
in
Compare (op, x, y))))
in
let decode =
fix begin fun t ->
if_list
~then_:(
[ "or", repeat t >>| (fun x -> Or x)
; "and", repeat t >>| (fun x -> And x)
] @ ops
|> sum)
~else_:(String_with_vars.decode >>| fun v -> Expr v)
end
in
let%map () = Syntax.since Stanza.syntax (1, 1)
and decode = decode
in
decode
end
let enabled_if =
field "enabled_if" ~default:Blang.true_
(Syntax.since Stanza.syntax (1, 4) >>> Blang.decode)
module Per_module = struct
include Per_item.Make(Module.Name)
let decode ~default a =
peek_exn >>= function
| List (loc, Atom (_, A "per_module") :: _) ->
sum [ "per_module",
repeat
(pair a (list module_name) >>| fun (pp, names) ->
(names, pp))
>>| fun x ->
of_mapping x ~default
|> function
| Ok t -> t
| Error (name, _, _) ->
of_sexp_errorf loc
"module %s present in two different sets"
(Module.Name.to_string name)
]
| _ -> a >>| for_all
end
module Preprocess_map = struct
type t = Preprocess.t Per_module.t
let decode = Per_module.decode Preprocess.decode ~default:Preprocess.No_preprocessing
let no_preprocessing = Per_module.for_all Preprocess.No_preprocessing
let find module_name t = Per_module.get t module_name
let default = Per_module.for_all Preprocess.No_preprocessing
let pps t =
Per_module.fold t ~init:Lib_name.Map.empty ~f:(fun pp acc ->
List.fold_left (Preprocess.pps pp) ~init:acc ~f:(fun acc (loc, pp) ->
Lib_name.Map.add acc pp loc))
|> Lib_name.Map.foldi ~init:[] ~f:(fun pp loc acc -> (loc, pp) :: acc)
end
module Lint = struct
type t = Preprocess_map.t
let decode = Preprocess_map.decode
let default = Preprocess_map.default
let no_lint = default
end
let field_oslu name = Ordered_set_lang.Unexpanded.field name
module Js_of_ocaml = struct
type t =
{ flags : Ordered_set_lang.Unexpanded.t
; javascript_files : string list
}
let decode =
record
(let%map flags = field_oslu "flags"
and javascript_files = field "javascript_files" (list string) ~default:[]
in
{ flags; javascript_files })
let default =
{ flags = Ordered_set_lang.Unexpanded.standard
; javascript_files = [] }
end
module Lib_dep = struct
type choice =
{ required : Lib_name.Set.t
; forbidden : Lib_name.Set.t
; file : string
}
type select =
{ result_fn : string
; choices : choice list
; loc : Loc.t (* For error messages *)
}
type t =
| Direct of (Loc.t * Lib_name.t)
| Select of select
let choice =
enter (
let%map loc = loc
and preds, file =
until_keyword "->"
~before:(let%map s = string
and loc = loc in
let len = String.length s in
if len > 0 && s.[0] = '!' then
Right (Lib_name.of_string_exn ~loc:(Some loc)
(String.drop s 1))
else
Left (Lib_name.of_string_exn ~loc:(Some loc) s))
~after:file
in
match file with
| None ->
of_sexp_errorf loc "(<[!]libraries>... -> <file>) expected"
| Some file ->
let rec loop required forbidden = function
| [] ->
let common = Lib_name.Set.inter required forbidden in
Option.iter (Lib_name.Set.choose common) ~f:(fun name ->
of_sexp_errorf loc
"library %S is both required and forbidden in this clause"
(Lib_name.to_string name));
{ required
; forbidden
; file
}
| Left s :: l ->
loop (Lib_name.Set.add required s) forbidden l
| Right s :: l ->
loop required (Lib_name.Set.add forbidden s) l
in
loop Lib_name.Set.empty Lib_name.Set.empty preds)
let decode =
if_list
~then_:(
enter
(let%map loc = loc
and () = keyword "select"
and result_fn = file
and () = keyword "from"
and choices = repeat choice in
Select { result_fn; choices; loc }))
~else_:(
let%map (loc, name) = located Lib_name.decode in
Direct (loc, name))
let to_lib_names = function
| Direct (_, s) -> [s]
| Select s ->
List.fold_left s.choices ~init:Lib_name.Set.empty ~f:(fun acc x ->
Lib_name.Set.union acc (Lib_name.Set.union x.required x.forbidden))
|> Lib_name.Set.to_list
let direct x = Direct x
let of_lib_name (loc, pp) = Direct (loc, pp)
end
module Lib_deps = struct
type t = Lib_dep.t list
type kind =
| Required
| Optional
| Forbidden
let decode =
let%map loc = loc
and t = repeat Lib_dep.decode
in
let add kind name acc =
match Lib_name.Map.find acc name with
| None -> Lib_name.Map.add acc name kind
| Some kind' ->
match kind, kind' with
| Required, Required ->
of_sexp_errorf loc "library %S is present twice"
(Lib_name.to_string name)
| (Optional|Forbidden), (Optional|Forbidden) ->
acc
| Optional, Required | Required, Optional ->
of_sexp_errorf loc
"library %S is present both as an optional \
and required dependency"
(Lib_name.to_string name)
| Forbidden, Required | Required, Forbidden ->
of_sexp_errorf loc
"library %S is present both as a forbidden \
and required dependency"
(Lib_name.to_string name)
in
ignore (
List.fold_left t ~init:Lib_name.Map.empty ~f:(fun acc x ->
match x with
| Lib_dep.Direct (_, s) -> add Required s acc
| Select { choices; _ } ->
List.fold_left choices ~init:acc ~f:(fun acc c ->
let acc =
Lib_name.Set.fold c.Lib_dep.required ~init:acc ~f:(add Optional)
in
Lib_name.Set.fold c.forbidden ~init:acc ~f:(add Forbidden)))
: kind Lib_name.Map.t);
t
let decode = parens_removed_in_dune decode
let of_pps pps =
List.map pps ~f:(fun pp -> Lib_dep.of_lib_name (Loc.none, pp))
let info t ~kind =
List.concat_map t ~f:(function
| Lib_dep.Direct (_, s) -> [(s, kind)]
| Select { choices; _ } ->
List.concat_map choices ~f:(fun c ->
Lib_name.Set.to_list c.Lib_dep.required
|> List.map ~f:(fun d -> (d, Lib_deps_info.Kind.Optional))))
|> Lib_name.Map.of_list_reduce ~f:Lib_deps_info.Kind.merge
end
let modules_field name = Ordered_set_lang.field name
module Auto_format = struct
let syntax =
Syntax.create ~name:"fmt"
~desc:"integration with automatic formatters"
[ (1, 1) ]
type language =
| Ocaml
| Reason
| Dune
let language_to_sexp = function
| Ocaml -> Sexp.Atom "ocaml"
| Reason -> Sexp.Atom "reason"
| Dune -> Sexp.Atom "dune"
let language =
sum
[ ("ocaml", return Ocaml)
; ("reason", return Reason)
; ("dune",
let%map () = Syntax.since syntax (1, 1) in
Dune)
]
type enabled_for =
| Default of Syntax.Version.t
| Only of language list
let enabled_for_field =
let%map r = field_o "enabled_for" (repeat language)
and version = Syntax.get_exn syntax
in
match r with
| Some l -> Only l
| None -> Default version
let enabled_for_to_sexp =
function
| Default v -> Sexp.List [Atom "default"; Syntax.Version.to_sexp v]
| Only l -> List [Atom "only"; List (List.map ~f:language_to_sexp l)]
type t =
{ loc : Loc.t
; enabled_for : enabled_for
}
let to_sexp {enabled_for; loc = _} =
Sexp.List
[ List [Atom "enabled_for"; enabled_for_to_sexp enabled_for]
]
let dparse_args =
let%map loc = loc
and enabled_for = record enabled_for_field
in
({loc; enabled_for}, [])
let key =
Dune_project.Extension.register syntax dparse_args to_sexp
let enabled_languages config =
match config.enabled_for with
| Default ver ->
let in_1_0 =
[Ocaml; Reason]
in
let extra =
match Syntax.Version.compare ver (1, 1) with
| Lt -> []
| Eq | Gt -> [Dune]
in
in_1_0 @ extra
| Only l -> l
let includes config language =
List.mem language ~set:(enabled_languages config)
let loc t = t.loc
end
module Buildable = struct
type t =
{ loc : Loc.t
; modules : Ordered_set_lang.t
; modules_without_implementation : Ordered_set_lang.t
; variants : Variant.Set.t
; libraries : Lib_dep.t list
; preprocess : Preprocess_map.t
; preprocessor_deps : Dep_conf.t list
; lint : Preprocess_map.t
; flags : Ordered_set_lang.Unexpanded.t
; ocamlc_flags : Ordered_set_lang.Unexpanded.t
; ocamlopt_flags : Ordered_set_lang.Unexpanded.t
; js_of_ocaml : Js_of_ocaml.t
; allow_overlapping_dependencies : bool
}
let decode =
let%map loc = loc
and preprocess =
field "preprocess" Preprocess_map.decode ~default:Preprocess_map.default
and variants =
field "variants" (list Variant.decode >>| Variant.Set.of_list)
~default:Variant.Set.empty
and preprocessor_deps =
field "preprocessor_deps" (list Dep_conf.decode) ~default:[]
and lint = field "lint" Lint.decode ~default:Lint.default
and modules = modules_field "modules"
and modules_without_implementation =
modules_field "modules_without_implementation"
and libraries = field "libraries" Lib_deps.decode ~default:[]
and flags = field_oslu "flags"
and ocamlc_flags = field_oslu "ocamlc_flags"
and ocamlopt_flags = field_oslu "ocamlopt_flags"
and js_of_ocaml =
field "js_of_ocaml" Js_of_ocaml.decode ~default:Js_of_ocaml.default
and allow_overlapping_dependencies =
field_b "allow_overlapping_dependencies"
in
{ loc
; preprocess
; preprocessor_deps
; lint
; modules
; modules_without_implementation
; variants
; libraries
; flags
; ocamlc_flags
; ocamlopt_flags
; js_of_ocaml
; allow_overlapping_dependencies
}
let single_preprocess t =
if Per_module.is_constant t.preprocess then
Per_module.get t.preprocess (Module.Name.of_string "")
else
Preprocess.No_preprocessing
end
module Public_lib = struct
type t =
{ name : Loc.t * Lib_name.t
; package : Package.t
; sub_dir : string option
}
let name t = snd t.name
let public_name_field =
map_validate
(let%map project = Dune_project.get_exn ()
and loc_name = field_o "public_name" (located Lib_name.decode) in
(project, loc_name))
~f:(fun (project, loc_name) ->
match loc_name with
| None -> Ok None
| Some ((_, s) as loc_name) ->
let (pkg, rest) = Lib_name.split s in
match Pkg.resolve project pkg with
| Ok pkg ->
Ok (Some
{ package = pkg
; sub_dir =
if rest = [] then None else
Some (String.concat rest ~sep:"/")
; name = loc_name
})
| Error _ as e -> e)
end
module Mode_conf = struct
module T = struct
type t =
| Byte
| Native
| Best
let compare (a : t) b = compare a b
end
include T
let decode =
enum
[ "byte" , Byte
; "native", Native
; "best" , Best
]
let to_string = function
| Byte -> "byte"
| Native -> "native"
| Best -> "best"
let pp fmt t =
Format.pp_print_string fmt (to_string t)
let encode t =
Dune_lang.unsafe_atom_of_string (to_string t)
module Set = struct
include Set.Make(T)
let decode = list decode >>| of_list
let default = of_list [Byte; Best]
let eval t ~has_native =
let has_best = mem t Best in
let byte = mem t Byte || (has_best && (not has_native)) in
let native = has_native && (mem t Native || has_best) in
{ Mode.Dict.byte; native }
end
end
module Library = struct
module Inherited = struct
type 'a t =
| This of 'a
| From of (Loc.t * Lib_name.t)
end
module Stdlib = struct
type t =
{ modules_before_stdlib : Module.Name.Set.t
; exit_module : Module.Name.t option
; internal_modules : Glob.t
}
let syntax =
let syntax =
Syntax.create ~name:"experimental_building_ocaml_compiler_with_dune"
~desc:"experimental feature for building the compiler with dune"
[ (0, 1) ]
in
Dune_project.Extension.register_simple ~experimental:true
syntax (Dune_lang.Decoder.return []);
syntax
let decode =
fields
(let%map modules_before_stdlib =
field "modules_before_stdlib" (list module_name) ~default:[]
and exit_module =
field_o "exit_module" module_name
and internal_modules =
field "internal_modules" Glob.decode ~default:Glob.empty
in
{ modules_before_stdlib = Module.Name.Set.of_list modules_before_stdlib
; exit_module
; internal_modules
})
end
module Wrapped = struct
include Wrapped
let value = function
| Inherited.From _ -> None
| This s -> Some (to_bool s)
let default = Simple true
let make ~wrapped ~implements : t Inherited.t =
match wrapped, implements with
| None, None -> This default
| None, Some w -> From w
| Some (_loc, w), None -> This w
| Some (loc, _), Some _ ->
of_sexp_error loc
"Wrapped cannot be set for implementations. \
It is inherited from the virtual library."
let field = field_o "wrapped" (located decode)
end
type t =
{ name : (Loc.t * Lib_name.Local.t)
; public : Public_lib.t option
; synopsis : string option
; install_c_headers : string list
; ppx_runtime_libraries : (Loc.t * Lib_name.t) list
; modes : Mode_conf.Set.t
; kind : Lib_kind.t
; c_flags : Ordered_set_lang.Unexpanded.t
; c_names : Ordered_set_lang.t option
; cxx_flags : Ordered_set_lang.Unexpanded.t
; cxx_names : Ordered_set_lang.t option
; library_flags : Ordered_set_lang.Unexpanded.t
; c_library_flags : Ordered_set_lang.Unexpanded.t
; self_build_stubs_archive : string option
; virtual_deps : (Loc.t * Lib_name.t) list
; wrapped : Wrapped.t 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
; no_keep_locs : bool
; dune_version : Syntax.Version.t
; virtual_modules : Ordered_set_lang.t option
; implements : (Loc.t * Lib_name.t) option
; variant : Variant.t option
; default_implementation : (Loc.t * Lib_name.t) option
; private_modules : Ordered_set_lang.t option
; stdlib : Stdlib.t option
}
let decode =
record
(let%map buildable = Buildable.decode
and loc = loc
and name = field_o "name" Lib_name.Local.decode_loc
and public = Public_lib.public_name_field
and synopsis = field_o "synopsis" string
and install_c_headers =
field "install_c_headers" (list string) ~default:[]
and ppx_runtime_libraries =
field "ppx_runtime_libraries" (list (located Lib_name.decode)) ~default:[]
and c_flags = field_oslu "c_flags"
and cxx_flags = field_oslu "cxx_flags"
and c_names = field_o "c_names" Ordered_set_lang.decode
and cxx_names = field_o "cxx_names" Ordered_set_lang.decode
and library_flags = field_oslu "library_flags"
and c_library_flags = field_oslu "c_library_flags"
and virtual_deps =
field "virtual_deps" (list (located Lib_name.decode)) ~default:[]
and modes = field "modes" Mode_conf.Set.decode ~default:Mode_conf.Set.default
and kind = field "kind" Lib_kind.decode ~default:Lib_kind.Normal
and wrapped = Wrapped.field
and optional = field_b "optional"
and self_build_stubs_archive =
located (field "self_build_stubs_archive" (option string) ~default:None)
and no_dynlink = field_b "no_dynlink"
and no_keep_locs = field_b "no_keep_locs"
~check:(Syntax.deprecated_in Stanza.syntax (1, 7))
and sub_systems =
return () >>= fun () ->
Sub_system_info.record_parser ()
and project = Dune_project.get_exn ()
and dune_version = Syntax.get_exn Stanza.syntax
and virtual_modules =
field_o "virtual_modules" (
Syntax.since Stanza.syntax (1, 7)
>>= fun () -> Ordered_set_lang.decode)
and implements =
field_o "implements" (
Syntax.since Stanza.syntax (1, 7)
>>= fun () -> located Lib_name.decode)
and variant =
field_o "variant" (
Syntax.since Stanza.syntax (1, 8)
>>= fun () -> located Variant.decode)
and default_implementation =
field_o "default_implementation" (
Syntax.since Stanza.syntax (1, 8)
>>= fun () -> located Lib_name.decode)
and private_modules =
field_o "private_modules" (
Syntax.since Stanza.syntax (1, 2)
>>= fun () -> Ordered_set_lang.decode)
and stdlib =
field_o "stdlib" (Syntax.since Stdlib.syntax (0, 1) >>> Stdlib.decode)
in
let wrapped = Wrapped.make ~wrapped ~implements in
let name =
let open Syntax.Version.Infix in
match name, public with
| Some (loc, res), _ ->
let wrapped = Wrapped.value wrapped in
(loc, Lib_name.Local.validate (loc, res) ~wrapped)
| None, Some { name = (loc, name) ; _ } ->
if dune_version >= (1, 1) then
match Lib_name.to_local name with
| Ok m -> (loc, m)
| Warn _ | Invalid ->
of_sexp_errorf loc
"%s.\n\
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."
Lib_name.Local.invalid_message
else
of_sexp_error loc "name field cannot be omitted before version \
1.1 of the dune language"
| None, None ->
of_sexp_error loc (
if dune_version >= (1, 1) then
"supply at least least one of name or public_name fields"
else
"name field is missing"
)
in
Option.both virtual_modules implements
|> Option.iter ~f:(fun (virtual_modules, (_, impl)) ->
of_sexp_errorf
(Ordered_set_lang.loc virtual_modules
|> Option.value_exn)
"A library cannot be both virtual and implement %s"
(Lib_name.to_string impl));
match virtual_modules, default_implementation with
| None, Some (loc, _) ->
of_sexp_error loc
"Only virtual libraries can specify a default implementation."
| _ -> ();
match implements, variant with
| None, Some (loc, _) ->
of_sexp_error loc
"Only implementations can specify a variant."
| _ -> ();
let variant = Option.map variant ~f:(fun (_, v) -> v) in
let self_build_stubs_archive =
let loc, self_build_stubs_archive = self_build_stubs_archive in
let err =
match c_names, cxx_names, self_build_stubs_archive with
| _, _, None -> None
| Some _, _, Some _ -> Some "c_names"
| _, Some _, Some _ -> Some "cxx_names"
| None, None, _ -> None
in
match err with
| None ->
self_build_stubs_archive
| Some name ->
of_sexp_errorf loc
"A library cannot use (self_build_stubs_archive ...) \
and (%s ...) simultaneously." name
in
{ name
; public
; synopsis
; install_c_headers
; ppx_runtime_libraries
; modes
; kind
; c_names
; c_flags
; cxx_names
; cxx_flags
; library_flags
; c_library_flags
; self_build_stubs_archive
; virtual_deps
; wrapped
; optional
; buildable
; dynlink = Dynlink_supported.of_bool (not no_dynlink)
; project
; sub_systems
; no_keep_locs
; dune_version
; virtual_modules
; implements
; variant
; default_implementation
; private_modules
; stdlib
})
let has_stubs t =
match t.c_names, t.cxx_names, t.self_build_stubs_archive with
| None, None, None -> false
| _ -> true