forked from ocaml/opam
/
file.ml
1333 lines (1102 loc) · 37.6 KB
/
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
(***********************************************************************)
(* *)
(* Copyright 2012 OCamlPro *)
(* Copyright 2012 INRIA *)
(* *)
(* All rights reserved. This file is distributed under the terms of *)
(* the GNU Public License version 3.0. *)
(* *)
(* TypeRex is distributed in the hope that it will be useful, *)
(* but WITHOUT ANY WARRANTY; without even the implied warranty of *)
(* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *)
(* GNU General Public License for more details. *)
(* *)
(***********************************************************************)
open Types
open Utils
open File_format
exception Parsing of string
module Lines = struct
let internal = "lines"
(* Lines of space separated words *)
type t = string list list
let empty = []
let of_string _ raw =
let lexbuf = Lexing.from_string (Raw.to_string raw) in
Linelexer.main lexbuf
let to_string _ lines =
let buf = Buffer.create 1024 in
List.iter (fun l ->
Buffer.add_string buf (String.concat " " l);
Buffer.add_string buf "\n"
) lines;
Raw.of_string (Buffer.contents buf)
end
module Syntax = struct
let internal = "syntax"
type t = File_format.file
let empty = File_format.empty
let of_string f str =
try
let lexbuf = Lexing.from_string (Raw.to_string str) in
let filename = Filename.to_string f in
lexbuf.Lexing.lex_curr_p <- { lexbuf.Lexing.lex_curr_p with Lexing.pos_fname = filename };
Parser.main Lexer.token lexbuf filename
with e ->
Globals.error "Parsing error while reading %s" (Filename.to_string f);
raise e
let to_string _ t =
Raw.of_string (File_format.string_of_file t)
let check f fields =
if not (File_format.is_valid f.contents fields) then
Globals.error_and_exit "{ %s } are invalid field names in %s. Valid fields are { %s }"
(String.concat ", " (invalid_fields f.contents fields))
f.filename
(String.concat ", " fields)
end
module X = struct
module Filenames = struct
let internal = "filenames"
type t = Filename.Set.t
let empty = Filename.Set.empty
let of_string f s =
let lines = Lines.of_string f s in
let lines = Utils.filter_map (function
| [] -> None
| [f] -> Some (Filename.of_string f)
| s ->
Globals.error_and_exit "%S is not a valid filename" (String.concat " " s)
) lines in
Filename.Set.of_list lines
let to_string f s =
let lines =
List.map (fun f -> [Filename.to_string f]) (Filename.Set.elements s) in
Lines.to_string f lines
end
module Urls_txt = struct
let internal = "urls-txt"
type t = (basename * int * string) list
let empty = []
let of_string f s =
let lines = Lines.of_string f s in
Utils.filter_map (function
| [] -> None
| [name;perm;digest] -> Some (Basename.of_string name, int_of_string perm, digest)
| s ->
Globals.error_and_exit "%s is not a valid index entry" (String.concat " " s)
) lines
let to_string f t =
let lines =
List.map (fun (f,p,d) -> [Basename.to_string f; Printf.sprintf "0o%o" p; d]) t in
Lines.to_string f lines
end
module URL = struct
let internal = "url"
type t = {
url : string;
kind : string option;
checksum: string option;
}
let empty = {
url = "<none>";
kind = None;
checksum= None;
}
let url t = t.url
let kind t = t.kind
let of_string f s =
let lines = Lines.of_string f s in
let lines = Utils.filter_map (function
| [] -> None
| [url] -> Some {url; kind=None; checksum=None}
| [url;kind] -> Some {url; kind=Some kind; checksum=None}
| h -> Globals.error_and_exit "%s is not a valid url" (String.concat " " h)
) lines in
match lines with
| [x] -> x
| _ -> Globals.error_and_exit "too many lines (%d)" (List.length lines)
let to_string f t =
let line = match t.kind with
| None -> [t.url]
| Some k -> [t.url; k] in
Lines.to_string f [line]
end
module Updated = struct
let internal = "updated"
type t = NV.Set.t
let empty = NV.Set.empty
let of_string f s =
let lines = Lines.of_string f s in
let map = ref empty in
let add n v = map := NV.Set.add (NV.create n v) !map in
List.iter (function
| [] -> ()
| [name; version] -> add (N.of_string name) (V.of_string version)
| _ -> Globals.error_and_exit "[file.ml/module Installed]"
) lines;
!map
let to_string _ t =
let buf = Buffer.create 1024 in
NV.Set.iter
(fun nv -> Printf.bprintf buf "%s %s\n" (N.to_string (NV.name nv)) (V.to_string (NV.version nv)))
t;
Raw.of_string (Buffer.contents buf)
end
module Installed = struct
include Updated
let internal = "installed"
let check t =
let map = NV.to_map t in
N.Map.iter (fun n vs ->
if V.Set.cardinal vs <> 1 then
Globals.error_and_exit "Multiple versions installed for package %s: %s"
(N.to_string n) (V.Set.to_string vs)
) map
let to_string f t =
check t;
Updated.to_string f t
end
module Reinstall = struct
include Installed
let internal = "reinstall"
end
module Repo_index = struct
let internal = "repo-index"
type t = string N.Map.t
let empty = N.Map.empty
let of_string filename str =
let lines = Lines.of_string filename str in
List.fold_left (fun map -> function
| [name_s; repo_s] ->
let name = N.of_string name_s in
if N.Map.mem name map then
Globals.error_and_exit "multiple lines for package %s" name_s
else
N.Map.add name repo_s map
| [] -> map
| x ->
Globals.error_and_exit "'%s' is not a valid repository index line" (String.concat " " x)
) N.Map.empty lines
let to_string filename map =
let lines = N.Map.fold (fun name repo lines ->
[ N.to_string name; repo] :: lines
) map [] in
Lines.to_string filename (List.rev lines)
end
module Pinned = struct
let internal = "pinned"
type t = pin_option N.Map.t
let empty = N.Map.empty
let of_string filename str =
let m = Repo_index.of_string filename str in
N.Map.map pin_option_of_string m
let to_string filename map =
Repo_index.to_string filename (N.Map.map string_of_pin_option map)
end
module Repo_config = struct
let internal = "repo-config"
type t = repository
let empty =
Repository.create ~name:"<none>" ~address:"<none>" ~kind:"<none>"
let s_name = "name"
let s_kind = "kind"
let s_address = "address"
let of_string filename str =
let s = Syntax.of_string filename str in
let name = assoc s.contents s_name parse_string in
let address = assoc s.contents s_address parse_string in
let kind = assoc s.contents s_kind parse_string in
Repository.create ~name ~address ~kind
let to_string filename t =
let s = {
filename = Filename.to_string filename;
contents = [
Variable (s_name , String (Repository.name t));
Variable (s_address, String (Dirname.to_string (Repository.address t)));
Variable (s_kind , String (Repository.kind t));
] } in
Syntax.to_string filename s
end
module Descr = struct
let internal = "descr"
type t = Lines.t
let empty = []
let create str =
[[str]]
let synopsis = function
| [] -> ""
| h::_ -> String.concat " " h
let full l =
let one l = String.concat " " l in
String.concat "\n" (List.map one l)
let of_string = Lines.of_string
let to_string = Lines.to_string
end
let s_opam_version = "opam-version"
module Aliases = struct
let internal = "aliases"
type t = (Alias.t * OCaml_V.t) list
let empty = []
let to_string filename t =
let l =
List.map
(fun (alias,oversion) -> [Alias.to_string alias; OCaml_V.to_string oversion])
t in
Lines.to_string filename l
let of_string filename s =
let l = Lines.of_string filename s in
List.fold_left (fun accu -> function
| [] -> accu
| [alias; oversion] -> (Alias.of_string alias, OCaml_V.of_string oversion) :: accu
| _ -> failwith "switches"
) [] l
end
module Config = struct
let internal = "config"
let to_repo (name, option) =
let address, kind = match option with
| Some (address, kind) -> address, kind
| None ->
Globals.default_repository_kind,
Globals.default_repository_address in
Repository.create ~name ~address ~kind
let of_repo r =
Option (String (Repository.name r),
[ String (Dirname.to_string (Repository.address r));
String (Repository.kind r) ])
type t = {
opam_version : OPAM_V.t ;
repositories : repository list ;
ocaml_version : Alias.t option ;
system_ocaml_version: OCaml_V.t option ;
cores : int;
}
let with_repositories t repositories = { t with repositories }
let with_ocaml_version t ocaml_version = { t with ocaml_version = Some ocaml_version }
let with_system_ocaml_version t system_ocaml_version = { t with system_ocaml_version }
let opam_version t = t.opam_version
let repositories t = t.repositories
let ocaml_version t = match t.ocaml_version with None -> Alias.of_string "<none>" | Some v -> v
let system_ocaml_version t = t.system_ocaml_version
let cores t = t.cores
let create opam_version repositories cores =
{ opam_version ; repositories ; ocaml_version = None ; system_ocaml_version = None ; cores }
let empty = {
opam_version = OPAM_V.of_string Globals.opam_version;
repositories = [];
ocaml_version = None;
system_ocaml_version = None;
cores = Globals.default_cores;
}
open File_format
let s_repositories = "repositories"
let s_ocaml_version = "ocaml-version"
let s_system_ocaml_version = "system-ocaml-version"
let s_system_ocaml_version2 = "system_ocaml-version"
let s_cores = "cores"
let valid_fields = [
s_opam_version;
s_repositories;
s_ocaml_version;
s_system_ocaml_version;
s_system_ocaml_version2;
s_cores;
]
let of_string filename f =
let s = Syntax.of_string filename f in
Syntax.check s valid_fields;
let opam_version =
assoc s.contents s_opam_version (parse_string |> OPAM_V.of_string) in
let repositories =
assoc s.contents s_repositories
(parse_list (parse_string_option parse_string_pair_of_list |> to_repo)) in
let ocaml_version =
assoc_option s.contents s_ocaml_version (parse_string |> Alias.of_string) in
let system_ocaml_version =
assoc_option s.contents s_system_ocaml_version (parse_string |> OCaml_V.of_string) in
let system_ocaml_version2 =
assoc_option s.contents s_system_ocaml_version2 (parse_string |> OCaml_V.of_string) in
let system_ocaml_version =
match system_ocaml_version, system_ocaml_version2 with
| Some v, _
| _ , Some v -> Some v
| None , None -> None in
let cores = assoc s.contents s_cores parse_int in
{ opam_version; repositories; ocaml_version; system_ocaml_version; cores }
let to_string filename t =
let s = {
filename = Filename.to_string filename;
contents = [
Variable (s_opam_version , make_string (OPAM_V.to_string t.opam_version));
Variable (s_repositories , make_list of_repo t.repositories);
Variable (s_cores , make_int t.cores);
]
@ (
match t.ocaml_version with
| None -> []
| Some v -> [ Variable (s_ocaml_version, make_string (Alias.to_string v)) ]
)
@ (
match t.system_ocaml_version with
| None -> []
| Some v -> [ Variable (s_system_ocaml_version, make_string (OCaml_V.to_string v)) ]
)
} in
Syntax.to_string filename s
end
module OPAM = struct
let internal = "opam"
type section = Types.section
type t = {
name : N.t;
version : V.t;
maintainer : string;
substs : basename list;
build_env : (string * string * string) list;
build : string list list;
remove : string list list;
depends : cnf_formula;
depopts : cnf_formula;
conflicts : and_formula;
libraries : section list;
syntax : section list;
others : (string * value) list;
ocaml_version: ocaml_constraint option;
}
let empty = {
name = N.of_string "<none>";
version = V.of_string "<none>";
maintainer = "<none>";
substs = [];
build_env = [];
build = [];
remove = [];
depends = [];
depopts = [];
conflicts = [];
libraries = [];
syntax = [];
others = [];
ocaml_version = None;
}
let make
~name ~version ~maintainer ~substs ~build_env ~build ~remove
~depends ~depopts ~conflicts ~libraries ~syntax ~others
~ocaml_version =
{ name; version; maintainer;
substs; build_env; build;
remove; depends; depopts; conflicts;
libraries; syntax; others;
ocaml_version }
let create nv =
let name = NV.name nv in
let version = NV.version nv in
{ empty with name; version }
let s_version = "version"
let s_name = "name"
let s_maintainer = "maintainer"
let s_substs = "substs"
let s_build = "build"
let s_build_env = "build-env"
let s_remove = "remove"
let s_depends = "depends"
let s_depopts = "depopts"
let s_conflicts = "conflicts"
let s_libraries = "libraries"
let s_syntax = "syntax"
let s_license = "license"
let s_authors = "authors"
let s_homepage = "homepage"
let s_ocaml_version = "ocaml-version"
(* to convert to cudf *)
(* see [Debcudf.add_inst] for more details about the format *)
let s_status = "status"
(* see [Debcudf.add_inst] for more details about the format *)
let s_installed = " installed"
let useful_fields = [
s_opam_version;
s_maintainer;
s_substs;
s_build;
s_remove;
s_depends;
s_depopts;
s_conflicts;
s_libraries;
s_syntax;
s_ocaml_version;
s_build_env;
]
let valid_fields =
useful_fields @ [
s_license;
s_authors;
s_homepage;
s_version;
s_name;
]
let name t = t.name
let maintainer t = t.maintainer
let version t = t.version
let substs t = t.substs
let build t = t.build
let remove t = t.remove
let depends t = t.depends
let depopts t = t.depopts
let conflicts t = t.conflicts
let libraries t = t.libraries
let syntax t = t.syntax
let ocaml_version t = t.ocaml_version
let build_env t = t.build_env
let with_depends t depends = { t with depends }
let with_depopts t depopts = { t with depopts }
let with_build t build = { t with build }
let with_remove t remove = { t with remove }
module D = Debian.Packages
(* XXX: Pre-encode the depends and conflict fields to avoid
headaches when interfacing with the solver *)
let lencode = List.map (fun ((n,a),c) -> (Common.CudfAdd.encode n,a), c)
let llencode = List.map lencode
let default_package t =
let depopts =
string_of_value (File_format.make_cnf_formula (llencode t.depopts)) in
{ D.default_package with
D.name = N.to_string t.name ;
D.version = V.to_string t.version ;
D.depends = llencode t.depends ;
D.conflicts = lencode t.conflicts ;
D.extras = (s_depopts, depopts) :: D.default_package.D.extras }
let to_package t ~installed =
let p = default_package t in
if installed then
{ p with D.extras = (s_status, s_installed) :: p.D.extras }
else
p
let to_string filename t =
let s = {
filename = Filename.to_string filename;
contents = [
Variable (s_opam_version, String Globals.opam_version);
(* Variable (s_name, String (N.to_string t.name));
Variable (s_version, String (V.to_string t.version)); *)
Variable (s_maintainer, String t.maintainer);
Variable (s_substs, make_list (Basename.to_string |> make_string) t.substs);
Variable (s_build_env, make_list make_env_variable t.build_env);
Variable (s_build, make_list (make_list make_string) t.build);
Variable (s_remove, make_list (make_list make_string) t.remove);
Variable (s_depends, make_cnf_formula t.depends);
Variable (s_depopts, make_cnf_formula t.depopts);
Variable (s_conflicts, make_and_formula t.conflicts);
Variable (s_libraries, make_list (Section.to_string |> make_string) t.libraries);
Variable (s_syntax, make_list (Section.to_string |> make_string) t.syntax);
] @ (
match t.ocaml_version with
| None -> []
| Some v -> [ Variable (s_ocaml_version, make_constraint v) ]
) @
List.map (fun (s, v) -> Variable (s, v)) t.others;
} in
Syntax.to_string filename s
let parse_command =
parse_or [
("string", parse_string);
("symbol", parse_symbol)
]
let of_string filename str =
let nv = NV.of_filename filename in
let s = Syntax.of_string filename str in
Syntax.check s valid_fields;
let s = s.contents in
let opam_version = assoc s s_opam_version parse_string in
if opam_version <> Globals.opam_version then
Globals.error_and_exit "%s is not a supported OPAM version" opam_version;
let name_f = assoc_option s s_name (parse_string |> N.of_string) in
let name = match name_f, nv with
| None , None ->
Globals.error_and_exit "%s is an invalid OPAM filename" (Filename.to_string filename);
| Some n, None -> n
| None , Some nv -> NV.name nv
| Some n, Some nv ->
if NV.name nv <> n then
Globals.error_and_exit
"Inconsistant naming scheme in %s"
(Filename.to_string filename)
else
n in
let version_f = assoc_option s s_version (parse_string |> V.of_string) in
let version = match version_f, nv with
| None , None ->
Globals.error_and_exit "%s is an invalid OPAM filename" (Filename.to_string filename);
| Some v, None -> v
| None , Some nv -> NV.version nv
| Some v, Some nv ->
if NV.version nv <> v then
Globals.error_and_exit
"Inconsistant versioning scheme in %s"
(Filename.to_string filename)
else
v in
let parse_commands = parse_or [
"list", (fun x -> [parse_list parse_command x]);
"list-list", parse_list (parse_list parse_command);
] in
let maintainer = assoc s s_maintainer parse_string in
let substs =
assoc_list s s_substs (parse_list (parse_string |> Basename.of_string)) in
let build_env = assoc_list s s_build_env (parse_list parse_env_variable) in
let build =
assoc_default Globals.default_build_command s s_build parse_commands in
let remove = assoc_list s s_remove parse_commands in
let depends = assoc_list s s_depends parse_cnf_formula in
let depopts = assoc_list s s_depopts parse_cnf_formula in
let conflicts = assoc_list s s_conflicts parse_and_formula in
let libraries = assoc_list s s_libraries (parse_list (parse_string |> Section.of_string)) in
let syntax = assoc_list s s_syntax (parse_list (parse_string |> Section.of_string)) in
let ocaml_version = assoc_option s s_ocaml_version parse_constraint in
let others =
Utils.filter_map (function
| Variable (x,v) -> if List.mem x useful_fields then None else Some (x,v)
| _ -> None
) s in
{ name; version; maintainer; substs; build; remove;
depends; depopts; conflicts; libraries; syntax; others;
ocaml_version; build_env }
end
module Dot_install_raw = struct
let internal = ".install(*raw*)"
type t = {
lib : string list ;
bin : (string * string option) list ;
toplevel: string list;
misc: (string * string option) list ;
}
let lib t = t.lib
let bin t = t.bin
let toplevel t = t.toplevel
let misc t = t.misc
let with_bin t bin = { t with bin }
let empty = {
lib = [] ;
bin = [] ;
toplevel = [] ;
misc = [] ;
}
let s_lib = "lib"
let s_bin = "bin"
let s_misc = "misc"
let s_toplevel = "toplevel"
let valid_fields = [
s_opam_version;
s_lib;
s_bin;
s_toplevel;
s_misc;
]
let to_string filename t =
let make_option = function
| src, None -> String src
| src, Some dst -> Option (String src, [String dst]) in
let s = {
filename = Filename.to_string filename;
contents = [
Variable (s_lib , make_list make_string t.lib);
Variable (s_bin , make_list make_option t.bin);
Variable (s_toplevel, make_list make_string t.toplevel);
Variable (s_misc, make_list make_option t.misc);
]
} in
Syntax.to_string filename s
let of_string filename str =
let s = Syntax.of_string filename str in
Syntax.check s valid_fields;
let lib = assoc_list s.contents s_lib (parse_list parse_string) in
let bin = assoc_list s.contents s_bin (parse_list (parse_string_option parse_single_string)) in
let toplevel = assoc_list s.contents s_toplevel (parse_list parse_string) in
let misc = assoc_list s.contents s_misc (parse_list (parse_string_option parse_single_string)) in
{ lib; bin; misc; toplevel }
end
module Dot_install = struct
let internal = ".install"
type t = {
lib : filename list ;
bin : (filename * basename) list ;
toplevel : filename list;
misc: (filename * filename) list ;
}
let string_of_move (src, dst) =
let src = Filename.to_string src in
let dst = Filename.to_string dst in
Printf.sprintf "%s => %s" src dst
let lib t = t.lib
let bin t = t.bin
let misc t = t.misc
let toplevel t = t.toplevel
module R = Dot_install_raw
let empty = {
lib = [] ;
bin = [] ;
toplevel = [];
misc = [] ;
}
let to_string filename t =
let to_bin (src, dst) =
Filename.to_string src,
if Filename.basename src = dst then None else Some (Basename.to_string dst) in
let to_misc (src, dst) =
Filename.to_string src,
Some (Filename.to_string dst) in
R.to_string filename
{ lib = List.map Filename.to_string t.lib
; bin = List.map to_bin t.bin
; toplevel = List.map Filename.to_string t.toplevel
; R.misc = List.map to_misc t.misc }
let of_string filename str =
let t = R.of_string filename str in
let of_bin = function
| s , None -> let f = Filename.of_string s in (f, Filename.basename f)
| src, Some dst -> (Filename.of_string src, Basename.of_string dst) in
let of_misc = function
| s , None -> let f = Filename.of_string s in (f, f)
| src, Some dst -> (Filename.of_string src, Filename.of_string dst) in
{ lib = List.map Filename.of_string t.R.lib
; bin = List.map of_bin t.R.bin
; toplevel = List.map Filename.of_string t.R.toplevel
; misc = List.map of_misc t.R.misc }
end
module Dot_config = struct
let internal = ".config"
let s str = S str
let b bool = B bool
type section = Types.section
type s = {
name : section;
kind : string ;
bytecomp : string list ;
asmcomp : string list ;
bytelink : string list ;
asmlink : string list ;
requires : section list;
lvariables: (variable * variable_contents) list;
}
type t = {
sections : s list;
variables: (variable * variable_contents) list;
}
let create variables =
{ variables; sections = [] }
let empty = {
sections = [];
variables = [];
}
let s_bytecomp = "bytecomp"
let s_asmcomp = "asmcomp"
let s_bytelink = "bytelink"
let s_asmlink = "asmlink"
let s_requires = "requires"
let valid_fields = [
s_opam_version;
s_bytecomp;
s_asmcomp;
s_bytelink;
s_asmlink;
s_requires;
]
let of_string filename str =
let file = Syntax.of_string filename str in
let parse_value = parse_or [
"string", (parse_string |> s);
"bool" , (parse_bool |> b);
] in
let parse_variables items =
let l = List.filter (fun (x,_) -> not (List.mem x valid_fields)) (variables items) in
List.map (fun (k,v) -> Variable.of_string k, parse_value v) l in
let parse_requires = parse_list (parse_string |> Section.of_string) in
let parse_section kind s =
let name = Section.of_string s.File_format.name in
let bytecomp = assoc_string_list s.items s_bytecomp in
let asmcomp = assoc_string_list s.items s_asmcomp in
let bytelink = assoc_string_list s.items s_bytecomp in
let asmlink = assoc_string_list s.items s_asmlink in
let requires = assoc_list s.items s_requires parse_requires in
let lvariables = parse_variables s.items in
{ name; kind; bytecomp; asmcomp; bytelink; asmlink; lvariables; requires } in
let libraries = assoc_sections file.contents "library" (parse_section "library") in
let syntax = assoc_sections file.contents "syntax" (parse_section "syntax") in
let sections = libraries @ syntax in
let variables = parse_variables file.contents in
{ sections; variables }
let rec to_string filename t =
let of_value = function
| B b -> Bool b
| S s -> String s in
let of_variables l =
List.map (fun (k,v) -> Variable (Variable.to_string k, of_value v)) l in
let make_require = Section.to_string |> make_string in
let of_section s =
Section
{ File_format.name = Section.to_string s.name;
kind = s.kind;
items = [
Variable (s_bytecomp, make_list make_string s.bytecomp);
Variable (s_asmcomp , make_list make_string s.asmcomp);
Variable (s_bytelink, make_list make_string s.bytelink);
Variable (s_asmlink , make_list make_string s.asmlink);
Variable (s_requires, make_list make_require s.requires);
] @ of_variables s.lvariables
} in
Syntax.to_string filename {
filename = Filename.to_string filename;
contents =
of_variables t.variables
@ List.map of_section t.sections
}
let variables t = List.map fst t.variables
let variable t s = List.assoc s t.variables
module type SECTION = sig
val available: t -> section list
val kind : t -> section -> string
val asmcomp : t -> section -> string list
val bytecomp : t -> section -> string list
val asmlink : t -> section -> string list
val bytelink : t -> section -> string list
val requires : t -> section -> section list
val variable : t -> section -> variable -> variable_contents
val variables: t -> section -> variable list
end
module MK (M : sig val get : t -> s list end) : SECTION = struct
let find t name =
List.find (fun s -> s.name = name) (M.get t)
let available t = List.map (fun s -> s.name) (M.get t)
let kind t s = (find t s).kind
let bytecomp t s = (find t s).bytecomp
let asmcomp t s = (find t s).asmcomp
let bytelink t s = (find t s).bytelink
let asmlink t s = (find t s).asmlink
let requires t s = (find t s).requires
let variable t n s = List.assoc s (find t n).lvariables
let variables t n = List.map fst (find t n).lvariables
end
let filter t n = List.filter (fun s -> s.kind = n) t.sections
module Library = MK (struct let get t = filter t "library" end)
module Syntax = MK (struct let get t = filter t "syntax" end)
module Section = MK (struct let get t = t.sections end)
end
module Env = struct
let internal = "env"
type t = (string * string) list
let empty = []
let of_string filename s =
let l = Lines.of_string filename s in
List.fold_left (fun accu -> function
| [] -> accu
| [s] ->
(match Utils.cut_at s '=' with
| None -> failwith (s ^ ": invalid env variable")
| Some(k,v) -> (k, v) :: accu)
| x -> failwith (String.concat " " x ^ ": invalid env variable")
) [] l
let to_string filename t =
let l = List.map (fun (k,v) -> [ k^"="^v ]) t in
Lines.to_string filename l
end
module Comp = struct
let internal = "comp"
type section = Types.section
type t = {
opam_version : OPAM_V.t ;
name : OCaml_V.t ;
preinstalled : bool;
src : filename ;
patches : filename list ;
configure : string list ;
make : string list ;
build : string list list ;
bytecomp : string list ;
asmcomp : string list ;
bytelink : string list ;
asmlink : string list ;
packages : and_formula ;