forked from ocaml/dune
-
Notifications
You must be signed in to change notification settings - Fork 0
/
jbuild_types.ml
817 lines (719 loc) · 25.5 KB
/
jbuild_types.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
open Import
open Sexp.Of_sexp
(* This file defines the jbuild types as well as the S-expression syntax for the various
supported version of the specification.
[vN] is for the version [N] of the specification and [vjs] is for the rolling
[jane_street] version. When they are all the same, sexp parsers are just named [t].
*)
module Jbuild_version = struct
type t =
| V1
| Vjs
let t =
enum
[ "1", V1
; "jane_street", Vjs
]
let latest_stable = V1
end
let invalid_module_name sexp =
of_sexp_error sexp "invalid module name"
let module_name sexp =
match string sexp with
| "" -> invalid_module_name sexp
| s ->
if s.[0] = '_' then invalid_module_name sexp;
String.iter s ~f:(function
| 'A'..'Z' | 'a'..'z' | '_' -> ()
| _ -> invalid_module_name sexp);
String.capitalize_ascii s
let module_names sexp = String_set.of_list (list module_name sexp)
let invalid_lib_name sexp =
of_sexp_error sexp "invalid library name"
let library_name sexp =
match string sexp with
| "" -> invalid_lib_name sexp
| s ->
if s.[0] = '.' then invalid_lib_name sexp;
String.iter s ~f:(function
| 'A'..'Z' | 'a'..'z' | '_' | '.' | '0'..'9' -> ()
| _ -> invalid_lib_name sexp);
s
let file sexp =
match string sexp with
| "." | ".." ->
of_sexp_error sexp "'.' and '..' are not valid filenames"
| fn -> fn
let file_in_current_dir sexp =
match string sexp with
| "." | ".." ->
of_sexp_error sexp "'.' and '..' are not valid filenames"
| fn ->
if Filename.dirname fn <> Filename.current_dir_name then
of_sexp_error sexp "file in current directory expected";
fn
module Raw_string () : sig
type t = private string
val to_string : t -> string
val of_string : string -> t
val t : t Sexp.Of_sexp.t
end = struct
type t = string
let to_string t = t
let of_string t = t
let t = string
end
module Raw_command = Raw_string ()
module Pp = struct
include Raw_string ()
let of_string s =
assert (not (String.is_prefix s ~prefix:"-"));
of_string s
let t sexp =
let s = string sexp in
if String.is_prefix s ~prefix:"-" then
of_sexp_error sexp "flag not allowed here"
else
of_string s
let compare : t -> t -> int = Pervasives.compare
end
module Pp_or_flags = struct
type t =
| PP of Pp.t
| Flags of string list
let of_string s =
if String.is_prefix s ~prefix:"-" then
Flags [s]
else
PP (Pp.of_string s)
let t = function
| Atom (_, s) -> of_string s
| List (_, l) -> Flags (List.map l ~f:string)
let split l =
let pps, flags =
List.partition_map l ~f:(function
| PP pp -> Inl pp
| Flags s -> Inr s)
in
(pps, List.concat flags)
end
module Dep_conf = struct
type t =
| File of String_with_vars.t
| Alias of String_with_vars.t
| Glob_files of String_with_vars.t
| Files_recursively_in of String_with_vars.t
let t =
let t =
let cstr name f =
cstr name (String_with_vars.t @> nil) f
in
sum
[ cstr "file" (fun x -> File x)
; cstr "alias" (fun x -> Alias x)
; cstr "glob_files" (fun x -> Glob_files x)
; cstr "files_recursively_in" (fun x -> Files_recursively_in x)
]
in
fun sexp ->
match sexp with
| Atom _ -> File (String_with_vars.t sexp)
| List _ -> t sexp
open Sexp
let sexp_of_t = function
| File t ->
List [Atom "file" ; String_with_vars.sexp_of_t t]
| Alias t ->
List [Atom "alias" ; String_with_vars.sexp_of_t t]
| Glob_files t ->
List [Atom "glob_files" ; String_with_vars.sexp_of_t t]
| Files_recursively_in t ->
List [Atom "files_recursively_in" ; String_with_vars.sexp_of_t t]
end
module Preprocess = struct
type pps = { pps : Pp.t list; flags : string list }
type t =
| No_preprocessing
| Action of Action.Mini_shexp.Unexpanded.t
| Pps of pps
let t =
sum
[ cstr "no_preprocessing" nil No_preprocessing
; cstr "action" (Action.Mini_shexp.Unexpanded.t @> nil) (fun x -> Action x)
; cstr "pps" (list Pp_or_flags.t @> nil) (fun l ->
let pps, flags = Pp_or_flags.split l in
Pps { pps; flags })
]
let pps = function
| Pps { pps; _ } -> pps
| _ -> []
end
module Per_file = struct
type 'a t =
| For_all of 'a
| Per_file of 'a String_map.t
let t a sexp =
match sexp with
| List (_, Atom (_, "per_file") :: rest) -> begin
List.concat_map rest ~f:(fun sexp ->
let pp, names = pair a module_names sexp in
List.map (String_set.elements names) ~f:(fun name -> (name, pp)))
|> String_map.of_alist
|> function
| Ok map -> Per_file map
| Error (name, _, _) ->
of_sexp_error sexp (sprintf "module %s present in two different sets" name)
end
| sexp -> For_all (a sexp)
end
module Preprocess_map = struct
type t = Preprocess.t Per_file.t
let t = Per_file.t Preprocess.t
let find module_name (t : t) =
match t with
| For_all pp -> pp
| Per_file map -> String_map.find_default module_name map ~default:No_preprocessing
let default : t = For_all No_preprocessing
module Pp_set = Set.Make(Pp)
let pps : t -> _ = function
| For_all pp -> Preprocess.pps pp
| Per_file map ->
String_map.fold map ~init:Pp_set.empty ~f:(fun ~key:_ ~data:pp acc ->
Pp_set.union acc (Pp_set.of_list (Preprocess.pps pp)))
|> Pp_set.elements
end
module Lint = struct
type t = Pps of Preprocess.pps
let t =
sum
[ cstr "pps" (list Pp_or_flags.t @> nil) (fun l ->
let pps, flags = Pp_or_flags.split l in
Pps { pps; flags })
]
end
let field_osl name =
field name Ordered_set_lang.t ~default:Ordered_set_lang.standard
let field_oslu name =
field name Ordered_set_lang.Unexpanded.t ~default:Ordered_set_lang.Unexpanded.standard
module Js_of_ocaml = struct
type t =
{ flags : string list
; javascript_files : string list
}
let t =
record
(field "flags" (list string) ~default:[] >>= fun flags ->
field "javascript_files" (list string) ~default:[] >>= fun javascript_files ->
return { flags; javascript_files })
end
module Lib_dep = struct
type literal = Pos of string | Neg of string
type choice =
{ lits : literal list
; file : string
}
type select = { result_fn : string; choices : choice list }
type t =
| Direct of string
| Select of select
let choice = function
| List (_, l) as sexp ->
let rec loop acc = function
| [Atom (_, "->"); sexp] ->
{ lits = List.rev acc
; file = file sexp
}
| Atom (_, "->") :: _ | List _ :: _ | [] ->
of_sexp_error sexp "(<[!]libraries>... -> <file>) expected"
| Atom (_, s) :: l ->
let len = String.length s in
if len > 0 && s.[0] = '!' then
let s = String.sub s ~pos:1 ~len:(len - 1) in
loop (Neg s :: acc) l
else
loop (Pos s :: acc) l
in
loop [] l
| sexp -> of_sexp_error sexp "(<library-name> <code>) expected"
let sexp_of_choice { lits; file } : Sexp.t =
List (List.fold_right lits ~init:[Atom "->"; Atom file]
~f:(fun lit acc ->
match lit with
| Pos s -> Sexp.Atom s :: acc
| Neg s -> Sexp.Atom ("!" ^ s) :: acc))
let t = function
| Atom (_, s) ->
Direct s
| List (_, Atom (_, "select") :: m :: Atom (_, "from") :: libs) ->
Select { result_fn = file m
; choices = List.map libs ~f:choice
}
| sexp ->
of_sexp_error sexp "<library> or (select <module> from <libraries...>) expected"
let to_lib_names = function
| Direct s -> [s]
| Select s ->
List.concat_map s.choices ~f:(fun x ->
List.map x.lits ~f:(function
| Pos x -> x
| Neg x -> x))
let direct s = Direct s
end
module Buildable = struct
type t =
{ modules : Ordered_set_lang.t
; libraries : Lib_dep.t list
; preprocess : Preprocess_map.t
; preprocessor_deps : Dep_conf.t list
; lint : Lint.t Per_file.t option
; flags : Ordered_set_lang.t
; ocamlc_flags : Ordered_set_lang.t
; ocamlopt_flags : Ordered_set_lang.t
}
let common =
field "preprocess" Preprocess_map.t ~default:Preprocess_map.default
>>= fun preprocess ->
field "preprocessor_deps" (list Dep_conf.t) ~default:[]
>>= fun preprocessor_deps ->
field_o "lint" (Per_file.t Lint.t)
>>= fun lint ->
field "modules" (fun s -> Ordered_set_lang.(map (t s)) ~f:String.capitalize_ascii)
~default:Ordered_set_lang.standard
>>= fun modules ->
field "libraries" (list Lib_dep.t) ~default:[]
>>= fun libraries ->
field_osl "flags" >>= fun flags ->
field_osl "ocamlc_flags" >>= fun ocamlc_flags ->
field_osl "ocamlopt_flags" >>= fun ocamlopt_flags ->
return
{ preprocess
; preprocessor_deps
; lint
; modules
; libraries
; flags
; ocamlc_flags
; ocamlopt_flags
}
let v1 = common
let vjs = v1
let single_preprocess t =
match t.preprocess with
| For_all pp -> pp
| Per_file _ -> No_preprocessing
end
module Public_lib = struct
type t =
{ name : string (* Full public name *)
; package : string (* Package it is part of *)
; sub_dir : string option (* Subdirectory inside the installation directory *)
}
let of_public_name s =
match String.split s ~on:'.' with
| [] -> assert false
| pkg :: rest ->
{ package = pkg
; sub_dir = if rest = [] then None else Some (String.concat rest ~sep:"/")
; name = s
}
let t sexp = of_public_name (string sexp)
end
module Library = struct
module Kind = struct
type t =
| Normal
| Ppx_deriver
| Ppx_rewriter
let t =
enum
[ "normal" , Normal
; "ppx_deriver" , Ppx_deriver
; "ppx_rewriter" , Ppx_rewriter
]
end
type t =
{ name : string
; public : Public_lib.t option
; synopsis : string option
; install_c_headers : string list
; ppx_runtime_libraries : string list
; modes : Mode.t list
; kind : Kind.t
; c_flags : Ordered_set_lang.Unexpanded.t
; c_names : string list
; cxx_flags : Ordered_set_lang.Unexpanded.t
; cxx_names : string list
; includes : String_with_vars.t list
; library_flags : String_with_vars.t list
; c_library_flags : Ordered_set_lang.Unexpanded.t
; self_build_stubs_archive : string option
; js_of_ocaml : Js_of_ocaml.t option
; virtual_deps : string list
; wrapped : bool
; optional : bool
; buildable : Buildable.t
}
let v1 =
record
(Buildable.v1 >>= fun buildable ->
field "name" library_name >>= fun name ->
field_o "public_name" Public_lib.t >>= fun public ->
field_o "synopsis" string >>= fun synopsis ->
field "install_c_headers" (list string) ~default:[] >>= fun install_c_headers ->
field "ppx_runtime_libraries" (list string) ~default:[] >>= fun ppx_runtime_libraries ->
field_oslu "c_flags" >>= fun c_flags ->
field_oslu "cxx_flags" >>= fun cxx_flags ->
field "c_names" (list string) ~default:[] >>= fun c_names ->
field "cxx_names" (list string) ~default:[] >>= fun cxx_names ->
field "library_flags" (list String_with_vars.t) ~default:[] >>= fun library_flags ->
field_oslu "c_library_flags" >>= fun c_library_flags ->
field_o "js_of_ocaml" Js_of_ocaml.t >>= fun js_of_ocaml ->
field "virtual_deps" (list string) ~default:[] >>= fun virtual_deps ->
field "modes" (list Mode.t) ~default:Mode.all >>= fun modes ->
field "kind" Kind.t ~default:Kind.Normal >>= fun kind ->
field "wrapped" bool ~default:true >>= fun wrapped ->
field_b "optional" >>= fun optional ->
field "self_build_stubs_archive" (option string) ~default:None >>= fun self_build_stubs_archive ->
return
{ name
; public
; synopsis
; install_c_headers
; ppx_runtime_libraries
; modes
; kind
; c_names
; c_flags
; cxx_names
; cxx_flags
; includes = []
; library_flags
; c_library_flags
; self_build_stubs_archive
; js_of_ocaml
; virtual_deps
; wrapped
; optional
; buildable
})
let vjs =
record
(ignore_fields [] >>= fun () ->
Buildable.vjs >>= fun buildable ->
field "name" library_name >>= fun name ->
field_o "public_name" Public_lib.t >>= fun public ->
field_o "synopsis" string >>= fun synopsis ->
field "install_c_headers" (list string) ~default:[] >>= fun install_c_headers ->
field "ppx_runtime_libraries" (list string) ~default:[] >>= fun ppx_runtime_libraries ->
field_oslu "c_flags" >>= fun c_flags ->
field_oslu "cxx_flags" >>= fun cxx_flags ->
field "c_names" (list string) ~default:[] >>= fun c_names ->
field "cxx_names" (list string) ~default:[] >>= fun cxx_names ->
field "library_flags" (list String_with_vars.t) ~default:[] >>= fun library_flags ->
field_oslu "c_library_flags" >>= fun c_library_flags ->
field "self_build_stubs_archive" (option string) ~default:None >>= fun self_build_stubs_archive ->
field_o "js_of_ocaml" Js_of_ocaml.t >>= fun js_of_ocaml ->
field "virtual_deps" (list string) ~default:[] >>= fun virtual_deps ->
field "modes" (list Mode.t) ~default:Mode.all >>= fun modes ->
field "includes" (list String_with_vars.t) ~default:[] >>= fun includes ->
field "kind" Kind.t ~default:Kind.Normal >>= fun kind ->
field "wrapped" bool ~default:true >>= fun wrapped ->
field_b "optional" >>= fun optional ->
return
{ name
; public
; synopsis
; install_c_headers
; ppx_runtime_libraries
; modes
; kind
; c_names
; c_flags
; cxx_names
; cxx_flags
; includes
; library_flags
; c_library_flags
; self_build_stubs_archive
; js_of_ocaml
; virtual_deps
; wrapped
; optional
; buildable
})
let has_stubs t =
match t.c_names, t.cxx_names, t.self_build_stubs_archive with
| [], [], None -> false
| _ -> true
let stubs_archive t ~dir ~ext_lib =
Path.relative dir (sprintf "lib%s_stubs%s" t.name ext_lib)
let all_lib_deps t =
List.map t.virtual_deps ~f:(fun s -> Lib_dep.Direct s) @ t.buildable.libraries
end
module Executables = struct
type t =
{ names : string list
; link_executables : bool
; link_flags : string list
; buildable : Buildable.t
}
let v1 =
record
(Buildable.v1 >>= fun buildable ->
field "names" (list string) >>= fun names ->
field "link_executables" bool ~default:true >>= fun link_executables ->
field "link_flags" (list string) ~default:[] >>= fun link_flags ->
return
{ names
; link_executables
; link_flags
; buildable
})
let vjs =
record
(ignore_fields []
>>= fun () ->
Buildable.vjs >>= fun buildable ->
field "names" (list string) >>= fun names ->
field "link_executables" bool ~default:true >>= fun link_executables ->
field "link_flags" (list string) ~default:[] >>= fun link_flags ->
return
{ names
; link_executables
; link_flags
; buildable
})
end
module Rule = struct
type t =
{ targets : string list (** List of files in the current directory *)
; deps : Dep_conf.t list
; action : Action.Mini_shexp.Unexpanded.t
}
let common =
field "targets" (list file_in_current_dir) >>= fun targets ->
field "deps" (list Dep_conf.t) ~default:[] >>= fun deps ->
field "action" Action.Mini_shexp.Unexpanded.t >>= fun action ->
return { targets; deps; action }
let v1 = record common
let vjs =
record
(ignore_fields [] >>= fun () ->
common)
let ocamllex_v1 names =
let str s = String_with_vars.of_string s in
List.map names ~f:(fun name ->
let src = name ^ ".mll" in
let dst = name ^ ".ml" in
{ targets = [dst]
; deps = [File (str src)]
; action =
Chdir
(str "${ROOT}",
Run (str "${bin:ocamllex}",
[str "-q"; str "-o"; str "${@}"; str "${<}"]))
})
let ocamllex_vjs = ocamllex_v1
let ocamlyacc_v1 names =
let str s = String_with_vars.of_string s in
List.map names ~f:(fun name ->
let src = name ^ ".mly" in
{ targets = [name ^ ".ml"; name ^ ".mli"]
; deps = [File (str src)]
; action =
Chdir
(str "${ROOT}",
Run (str "${bin:ocamlyacc}",
[str "${<}"]))
})
let ocamlyacc_vjs = ocamlyacc_v1
end
module Provides = struct
type t =
{ name : string
; file : string
}
let v1 sexp =
match sexp with
| Atom (_, s) ->
{ name = s
; file =
match String.lsplit2 s ~on:':' with
| None -> s
| Some (_, s) -> s
}
| List (_, [Atom (_, s); List (_, [Atom (_, "file"); Atom (_, file)])]) ->
{ name = s
; file
}
| sexp ->
of_sexp_error sexp "[<name>] or [<name> (file <file>)] expected"
let vjs = v1
end
module Install_conf = struct
type file =
{ src : string
; dst : string option
}
let file sexp =
match sexp with
| Atom (_, src) -> { src; dst = None }
| List (_, [Atom (_, src); Atom (_, "as"); Atom (_, dst)]) ->
{ src; dst = Some dst }
| _ ->
of_sexp_error sexp
"invalid format, <name> or (<name> as <install-as>) expected"
type t =
{ section : Install.Section.t
; files : file list
; package : string option
}
let v1 =
record
(field "section" Install.Section.t >>= fun section ->
field "files" (list file) >>= fun files ->
field_o "package" string >>= fun package ->
return
{ section
; files
; package
})
let vjs = v1
end
module Alias_conf = struct
type t =
{ name : string
; deps : Dep_conf.t list
; action : Action.Mini_shexp.Unexpanded.t option
}
let common =
field "name" string >>= fun name ->
field "deps" (list Dep_conf.t) ~default:[] >>= fun deps ->
field_o "action" Action.Mini_shexp.Unexpanded.t >>= fun action ->
return
{ name
; deps
; action
}
let v1 = record common
let vjs =
record
(ignore_fields [] >>= fun () ->
common)
end
module Stanza = struct
type t =
| Library of Library.t
| Executables of Executables.t
| Rule of Rule.t
| Provides of Provides.t
| Install of Install_conf.t
| Alias of Alias_conf.t
let rules l = List.map l ~f:(fun x -> Rule x)
let v1 =
sum
[ cstr "library" (Library.v1 @> nil) (fun x -> [Library x])
; cstr "executables" (Executables.v1 @> nil) (fun x -> [Executables x])
; cstr "rule" (Rule.v1 @> nil) (fun x -> [Rule x])
; cstr "ocamllex" (list string @> nil) (fun x -> rules (Rule.ocamllex_v1 x))
; cstr "ocamlyacc" (list string @> nil) (fun x -> rules (Rule.ocamlyacc_v1 x))
; cstr "install" (Install_conf.v1 @> nil) (fun x -> [Install x])
; cstr "alias" (Alias_conf.v1 @> nil) (fun x -> [Alias x])
(* Just for validation and error messages *)
; cstr "jbuild_version" (Jbuild_version.t @> nil) (fun _ -> [])
]
let vjs =
let ign name = cstr name ((fun _ -> ()) @> nil) (fun () -> []) in
sum
[ cstr "library" (Library.vjs @> nil) (fun x -> [Library x])
; cstr "executables" (Executables.vjs @> nil) (fun x -> [Executables x])
; cstr "rule" (Rule.vjs @> nil) (fun x -> [Rule x])
; cstr "ocamllex" (list string @> nil) (fun x -> rules (Rule.ocamllex_vjs x))
; cstr "ocamlyacc" (list string @> nil) (fun x -> rules (Rule.ocamlyacc_vjs x))
; cstr "provides" (Provides.vjs @> nil) (fun x -> [Provides x])
; cstr "install" (Install_conf.vjs @> nil) (fun x -> [Install x])
; cstr "alias" (Alias_conf.vjs @> nil) (fun x -> [Alias x])
; ign "enforce_style"
; ign "toplevel_expect_tests"
; ign "unified_tests"
; ign "embed"
(* Just for validation and error messages *)
; cstr "jbuild_version" (Jbuild_version.t @> nil) (fun _ -> [])
]
let select : Jbuild_version.t -> t list Sexp.Of_sexp.t = function
| V1 -> v1
| Vjs -> vjs
let lib_names ts =
List.fold_left ts ~init:String_set.empty ~f:(fun acc (_, stanzas) ->
List.fold_left stanzas ~init:acc ~f:(fun acc -> function
| Library lib ->
String_set.add lib.name
(match lib.public with
| None -> acc
| Some { name; _ } -> String_set.add name acc)
| _ -> acc))
end
module Stanzas = struct
type t = Stanza.t list
let resolve_packages ts ~dir
~(visible_packages : Package.t String_map.t)
~(closest_packages : Package.t list) =
let error fmt =
Loc.fail (Loc.in_file (Path.to_string (Path.relative dir "jbuild"))) fmt
in
let package_listing packages =
let longest_pkg = List.longest_map packages ~f:(fun p -> p.Package.name) in
String.concat ~sep:"\n"
(List.map packages ~f:(fun pkg ->
sprintf "- %-*s (because of %s)" longest_pkg pkg.Package.name
(Path.to_string (Path.relative pkg.path (pkg.name ^ ".opam")))))
in
let check pkg =
if not (String_map.mem pkg visible_packages) then
error "package %S is not visible here.\n\
The only packages I know of in %S are:\n\
%s%s"
pkg
(Path.to_string dir)
(package_listing (String_map.values visible_packages))
(hint pkg (String_map.keys visible_packages))
in
let default () =
match closest_packages with
| [pkg] -> pkg
| [] -> error "no packages are defined here"
| _ :: _ :: _ ->
error "I can't determine automatically which package your (install ...) \
stanzas are for in this directory. I have the choice between these ones:\n\
%s\n\
You need to add a (package ...) field in your (install ...) stanzas"
(package_listing closest_packages)
in
List.map ts ~f:(fun (stanza : Stanza.t) ->
match stanza with
| Library { public = Some { package; _ }; _ } ->
check package;
stanza
| Install { package = Some pkg; _ } ->
check pkg;
stanza
| Install ({ package = None; _ } as install) ->
Install { install with package = Some (default ()).name }
| _ -> stanza)
let parse sexps ~dir ~visible_packages =
let versions, sexps =
List.partition_map sexps ~f:(function
| List (loc, [Atom (_, "jbuild_version"); ver]) ->
Inl (Jbuild_version.t ver, loc)
| sexp -> Inr sexp)
in
let version =
match versions with
| [] -> Jbuild_version.latest_stable
| [(v, _)] -> v
| _ :: (_, loc) :: _ ->
Loc.fail loc "jbuild_version specified too many times"
in
List.concat_map sexps ~f:(Stanza.select version)
|> resolve_packages ~dir ~visible_packages
end