forked from ocaml/opam
/
opamState.ml
2515 lines (2309 loc) · 90.1 KB
/
opamState.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-2013 OCamlPro *)
(* Copyright 2012 INRIA *)
(* *)
(* All rights reserved.This file is distributed under the terms of the *)
(* GNU Lesser General Public License version 3.0 with linking *)
(* exception. *)
(* *)
(* OPAM 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 OpamTypes
open OpamTypesBase
open OpamMisc.OP
open OpamFilename.OP
open OpamPackage.Set.Op
let log fmt = OpamGlobals.log "STATE" fmt
let slog = OpamGlobals.slog
let () =
OpamHTTP.register ();
OpamGit.register ();
OpamDarcs.register();
OpamLocal.register ();
OpamHg.register ()
let confirm fmt =
Printf.ksprintf (fun msg ->
try
let rec loop () =
OpamGlobals.msg "%s [Y/n] %!" msg;
if !OpamGlobals.yes then true
else match String.lowercase (read_line ()) with
| "y" | "yes" | "" -> true
| "n" | "no" -> false
| _ -> loop ()
in loop ()
with End_of_file -> false
) fmt
let read fmt =
Printf.ksprintf (fun msg ->
OpamGlobals.msg "%s %!" msg;
if not !OpamGlobals.yes then (
try match read_line () with
| "" -> None
| s -> Some s
with End_of_file ->
OpamGlobals.msg "\n";
None
) else
None
) fmt
let switch_reinstall_hook = ref (fun _ -> assert false)
module Types = struct
type t = {
partial: bool;
root: OpamPath.t;
switch: switch;
compiler: compiler;
compiler_version: compiler_version lazy_t;
opams: OpamFile.OPAM.t package_map;
repositories: OpamFile.Repo_config.t repository_name_map;
packages: package_set;
available_packages: package_set Lazy.t;
aliases: OpamFile.Aliases.t;
compilers: compiler_set;
pinned: OpamFile.Pinned.t;
installed: OpamFile.Installed.t;
installed_roots: OpamFile.Installed_roots.t;
reinstall: OpamFile.Reinstall.t;
config: OpamFile.Config.t;
package_index: OpamFile.Package_index.t;
compiler_index: OpamFile.Compiler_index.t;
}
end
type state = Types.t
open Types
let string_of_repositories r =
OpamMisc.string_of_list
OpamRepositoryName.to_string
(OpamRepositoryName.Map.keys r)
let print_state t =
let packages p =
if OpamPackage.Set.cardinal p <= 20 then
OpamPackage.Set.to_string p
else
Printf.sprintf "%d packages" (OpamPackage.Set.cardinal p) in
log "ROOT : %a" (slog OpamFilename.Dir.to_string) t.root;
log "SWITCH : %a" (slog OpamSwitch.to_string) t.switch;
log "COMPILER : %a" (slog OpamCompiler.to_string) t.compiler;
log "COMPILERS : %a" (slog OpamCompiler.Set.to_string) t.compilers;
log "REPOS : %a" (slog string_of_repositories) t.repositories;
log "PACKAGES : %a" (slog packages) t.packages;
log "INSTALLED : %a" (slog OpamPackage.Set.to_string) t.installed;
log "ROOTS : %a" (slog OpamPackage.Set.to_string) t.installed_roots;
log "REINSTALL : %a" (slog OpamPackage.Set.to_string) t.reinstall
let compiler_comp t c =
OpamFile.Comp.read (OpamPath.compiler_comp t.root c)
let is_name_installed_aux installed name =
OpamPackage.Set.exists (fun nv -> OpamPackage.name nv = name) installed
let is_name_installed t name =
is_name_installed_aux t.installed name
let find_installed_package_by_name_aux installed name =
OpamPackage.Set.find (fun nv -> OpamPackage.name nv = name) installed
let find_installed_package_by_name t name =
find_installed_package_by_name_aux t.installed name
let find_packages_by_name t name =
let r = OpamPackage.packages_of_name t.packages name in
if OpamPackage.Set.is_empty r then None
else Some r
let installed_map t =
OpamPackage.Name.Map.map OpamPackage.Version.Set.choose_one
(OpamPackage.to_map t.installed)
let dot_config t nv =
OpamFile.Dot_config.safe_read (OpamPath.Switch.config t.root t.switch nv)
let is_package_installed t nv =
OpamPackage.Set.mem nv t.installed
let jobs t =
match !OpamGlobals.jobs with
| None -> OpamFile.Config.jobs t.config
| Some j -> j
let dl_jobs t =
match !OpamGlobals.dl_jobs with
| None -> OpamFile.Config.dl_jobs t.config
| Some j -> j
let sorted_repositories t =
OpamRepository.sort t.repositories
let mem_repository t repo_name =
OpamRepositoryName.Map.mem repo_name t.repositories
let find_repository_aux repo_name repositories =
try OpamRepositoryName.Map.find repo_name repositories
with Not_found ->
OpamGlobals.error_and_exit
"%s is not a valid repository name."
(OpamRepositoryName.to_string repo_name)
let find_repository t repo_name =
find_repository_aux repo_name t.repositories
let find_repository_exn t repo_name =
OpamRepositoryName.Map.find repo_name t.repositories
let find_repository_opt t repo_name =
try Some (find_repository_exn t repo_name)
with Not_found -> None
let compiler_index t =
OpamRepository.compiler_index t.repositories
let package_index t =
OpamRepository.package_index t.repositories
let package_state_one t all nv =
let opam = OpamPath.opam t.root nv in
let descr = OpamPath.descr t.root nv in
let url = OpamPath.url t.root nv in
let files = OpamPath.files t.root nv in
let archive = OpamPath.archive t.root nv in
if not (OpamFilename.exists opam) then []
else match all with
| `all ->
OpamFilename.checksum opam
@ OpamFilename.checksum descr
@ OpamRepository.url_checksum url
@ OpamFilename.checksum_dir files
@ OpamFilename.checksum archive
| `partial true ->
OpamRepository.url_checksum url
@ OpamFilename.checksum_dir files
@ OpamFilename.checksum archive
| `partial false ->
OpamRepository.url_checksum url
@ OpamFilename.checksum_dir files
let all_installed t =
OpamSwitch.Map.fold (fun switch _ accu ->
let installed_f = OpamPath.Switch.installed t.root switch in
let installed = OpamFile.Installed.safe_read installed_f in
installed ++ accu
) t.aliases OpamPackage.Set.empty
let package_state t =
let installed = OpamPackage.Set.fold (fun nv map ->
let state = package_state_one t `all nv in
OpamPackage.Map.add nv state map
) (all_installed t) OpamPackage.Map.empty in
OpamPackage.Map.fold (fun nv (repo, prefix) map ->
if OpamPackage.Map.mem nv map then map
else if OpamFilename.exists (OpamPath.opam t.root nv) then
let state = package_state_one t `all nv in
OpamPackage.Map.add nv state map
else
let repo = find_repository_exn t repo in
let state = OpamRepository.package_state repo prefix nv `all in
OpamPackage.Map.add nv state map
) t.package_index installed
let package_partial_state t nv ~archive =
match package_state_one t (`partial archive) nv with
| [] -> false, []
| state ->
let archive = OpamPath.archive t.root nv in
OpamFilename.exists archive, state
let package_repository_state t =
OpamPackage.Map.fold (fun nv (repo, prefix) map ->
let repo = find_repository_exn t repo in
match OpamRepository.package_state repo prefix nv `all with
| [] -> map
| state -> OpamPackage.Map.add nv state map
) t.package_index OpamPackage.Map.empty
let package_repository_partial_state t nv ~archive =
let repo, prefix = OpamPackage.Map.find nv t.package_index in
let repo = find_repository_exn t repo in
let exists_archive = OpamFilename.exists (OpamPath.Repository.archive repo nv) in
exists_archive, OpamRepository.package_state repo prefix nv (`partial archive)
let repository_and_prefix_of_package t nv =
try
let repo, prefix = OpamPackage.Map.find nv t.package_index in
let repo = find_repository_exn t repo in
Some (repo, prefix)
with Not_found ->
None
let repository_of_package t nv =
try
let repo, _ = OpamPackage.Map.find nv t.package_index in
let repo = OpamRepositoryName.Map.find repo t.repositories in
Some repo
with Not_found ->
None
let compiler_state_one t c =
let comp = OpamPath.compiler_comp t.root c in
let descr = OpamPath.compiler_descr t.root c in
if OpamFilename.exists comp then
Some (OpamFilename.checksum comp @ OpamFilename.checksum descr)
else
None
let compiler_state t =
OpamCompiler.Set.fold (fun c map ->
match compiler_state_one t c with
| None -> map
| Some s -> OpamCompiler.Map.add c s map
) t.compilers OpamCompiler.Map.empty
let compiler_repository_state t =
OpamCompiler.Map.fold (fun comp (repo, prefix) map ->
let repo = find_repository_exn t repo in
match OpamRepository.compiler_state repo prefix comp with
| [] -> map
| l -> OpamCompiler.Map.add comp l map
) t.compiler_index OpamCompiler.Map.empty
let repository_and_prefix_of_compiler t comp =
try
let repo, prefix = OpamCompiler.Map.find comp t.compiler_index in
let repo = find_repository_exn t repo in
Some (repo, prefix)
with Not_found ->
None
let is_pinned t n =
OpamPackage.Name.Map.mem n t.pinned
let locally_pinned_package t n =
let option = OpamPackage.Name.Map.find n t.pinned in
let path = string_of_pin_option option in
let kind = kind_of_pin_option option in
match repository_kind_of_pin_kind kind with
| None -> OpamSystem.internal_error "locally pinned"
| Some kind -> (address_of_string path, kind)
let url_of_locally_pinned_package t n =
let path, kind = locally_pinned_package t n in
OpamFile.URL.create kind path
(* Returns the directory holding the original metadata of the package.
This is a low-level function, you generally want to handle different
locations, like overlays for pinned packages *)
let package_repo_dir root repositories package_index nv =
if OpamFilename.exists (OpamPath.opam root nv) then
OpamPath.packages root nv
else
let repo_name, prefix = OpamPackage.Map.find nv package_index in
let repo = OpamRepositoryName.Map.find repo_name repositories in
OpamPath.Repository.packages repo prefix nv
(* Copies package definition from the repository to the overlay *)
let add_pinned_overlay ?(template=false) t name =
let open OpamFile in
let module Ov = OpamPath.Switch.Overlay in
let pkg_overlay f = f t.root t.switch name in
let get_orig_meta rv =
let orig = package_repo_dir t.root t.repositories t.package_index rv in
let files = OpamFilename.rec_files orig in
let opam_f = orig // "opam" in
let url_f = orig // "url" in
let files = List.filter (fun f -> f <> opam_f && f <> url_f) files in
let opam = OPAM.read opam_f in
let url =
try Some (URL.read url_f) with e -> OpamMisc.fatal e; None in
opam, url, orig, files
in
try match OpamPackage.Name.Map.find name t.pinned with
| Version v ->
let opam, url, root, files = get_orig_meta (OpamPackage.create name v) in
List.iter (fun f -> OpamFilename.copy_in ~root f (pkg_overlay Ov.package))
files;
OPAM.write (pkg_overlay Ov.opam) (OPAM.with_version opam v);
OpamMisc.Option.iter (URL.write (pkg_overlay Ov.url)) url
| _ ->
let rv = (* Lookup in package_index to ignore pinned versions *)
let versions =
OpamPackage.Map.fold (fun nv _ acc ->
if OpamPackage.name nv = name then
OpamPackage.Set.add nv acc
else acc)
t.package_index OpamPackage.Set.empty
in
OpamPackage.max_version versions name in
let v = OpamPackage.version rv in
let opam, _url, root, files = get_orig_meta rv in
let url = url_of_locally_pinned_package t name in
List.iter (fun f -> OpamFilename.copy_in ~root f (pkg_overlay Ov.package))
files;
OPAM.write (pkg_overlay Ov.opam) (OPAM.with_version opam v);
URL.write (pkg_overlay Ov.url) url
with Not_found -> (* No original meta *)
let url = url_of_locally_pinned_package t name in
let version =
OpamPackage.Version.of_string (if template then "0.1" else "0") in
let nv = OpamPackage.create name version in
let opam = if template then OPAM.template nv else OPAM.create nv in
OPAM.write (pkg_overlay Ov.opam) opam;
URL.write (pkg_overlay Ov.url) url
let overlay_of_name t name =
let f = OpamPath.Switch.Overlay.opam t.root t.switch name in
if not (OpamFilename.exists f) then
(log "overlay missing for %s !" (OpamPackage.Name.to_string name);
add_pinned_overlay t name);
f
let version_of_pin t name = function
| Version v -> v
| _ ->
let overlay = overlay_of_name t name in
let opam = OpamFile.OPAM.read overlay in
if OpamFile.OPAM.version_opt opam = None then
let nv =
try OpamPackage.max_version t.packages name
with Not_found ->
OpamPackage.create name (OpamPackage.Version.of_string "0") in
OpamGlobals.warning "Setting missing version in %s to %s"
(OpamFilename.to_string overlay)
(OpamPackage.Version.to_string @@ OpamPackage.version nv);
OpamFile.OPAM.(write overlay (with_version opam (OpamPackage.version nv)));
OpamPackage.version nv
else
OpamFile.OPAM.version opam
let pinned t name =
let v = version_of_pin t name (OpamPackage.Name.Map.find name t.pinned) in
OpamPackage.create name v
let pinned_opt t name = try Some (pinned t name) with Not_found -> None
let is_locally_pinned t name =
try match OpamPackage.Name.Map.find name t.pinned with
| Version _ -> false
| _ -> true
with Not_found -> false
let opam_opt t nv =
let name = OpamPackage.name nv in
try
if not (OpamPackage.Name.Map.mem name t.pinned) then raise Not_found else
let overlay = OpamPath.Switch.Overlay.opam t.root t.switch name in
let o = OpamFile.OPAM.read overlay in
if OpamFile.OPAM.version o = OpamPackage.version nv then Some o
else
(log "Looking for %s which is pinned to %s (not using overlay)"
(OpamPackage.to_string nv) (OpamPackage.Version.to_string (OpamFile.OPAM.version o));
raise Not_found)
with Not_found -> (* not pinned *)
try Some (OpamPackage.Map.find nv t.opams)
with Not_found ->
if OpamPackage.Set.mem nv t.installed then
(* Work-around for installed packages with no more metadata *)
Some (OpamFile.OPAM.create nv)
else
None
let opam t nv =
match opam_opt t nv with
| None -> OpamPackage.unknown (OpamPackage.name nv) (Some (OpamPackage.version nv))
| Some nv -> nv
let locate_meta overlay global repo exists t nv =
let name = OpamPackage.name nv in
if Some nv = pinned_opt t name &&
OpamFilename.exists_dir
(OpamPath.Switch.Overlay.package t.root t.switch name)
then
let meta = overlay t.root t.switch name in
if exists meta then Some meta else None
else
let meta = global t.root nv in
if exists meta then Some meta else
try
let r, prefix = OpamPackage.Map.find nv t.package_index in
let r = find_repository_exn t r in
let meta = repo r prefix nv in
if exists meta then Some meta else None
with Not_found ->
None
let descr_file =
locate_meta
OpamPath.Switch.Overlay.descr OpamPath.descr OpamPath.Repository.descr
OpamFilename.exists
let url_file =
locate_meta
OpamPath.Switch.Overlay.url OpamPath.url OpamPath.Repository.url
OpamFilename.exists
let files =
locate_meta
OpamPath.Switch.Overlay.files OpamPath.files OpamPath.Repository.files
OpamFilename.exists_dir
let install_metadata t nv =
if OpamFilename.exists (OpamPath.opam t.root nv) then ()
else
let opam = opam t nv in
OpamFile.OPAM.write (OpamPath.opam t.root nv) opam;
let onsome f = function None -> () | Some x -> f x in
onsome (fun f ->
OpamFile.Descr.write (OpamPath.descr t.root nv) (OpamFile.Descr.read f))
(descr_file t nv);
onsome (fun f ->
OpamFile.URL.write (OpamPath.url t.root nv) (OpamFile.URL.read f))
(url_file t nv);
onsome (fun d ->
OpamFilename.copy_dir ~src:d ~dst:(OpamPath.files t.root nv))
(files t nv)
let remove_metadata t packages =
let all_installed = all_installed t in
let packages = packages -- all_installed in
OpamPackage.Set.iter (fun nv ->
let dir = OpamPath.packages t.root nv in
OpamFilename.rmdir dir;
let archive = OpamPath.archive t.root nv in
OpamFilename.remove archive;
) packages
(* Returns [opam, descr_file, files_dir]. We don't consider [url] since
this is for pinned packages. if [root], don't look for a subdir [opam]
to find [files] and [descr]. *)
let local_opam ?(root=false) ?(version_override=true) nv dir =
let has_dir d = if OpamFilename.exists_dir d then Some d else None in
let has_file f = if OpamFilename.exists f then Some f else None in
let opam_dir, descr, files_dir =
if root then dir, has_file (dir // "descr"), has_dir (dir / "files") else
match has_dir (dir / "opam") with
| Some dir -> dir, has_file (dir // "descr"), has_dir (dir / "files")
| None -> dir, None, None
in
let opam =
match has_file (opam_dir // "opam") with
| None -> None
| Some local_opam ->
try
let opam = OpamFile.OPAM.read local_opam in
let opam = OpamFile.OPAM.with_name opam (OpamPackage.name nv) in
let version_override =
version_override || OpamFile.OPAM.version_opt opam = None
in
if version_override then
Some (OpamFile.OPAM.with_version opam (OpamPackage.version nv))
else Some opam
with e ->
OpamMisc.fatal e;
OpamGlobals.error "opam file for %s contains errors, ignoring"
(OpamPackage.to_string nv);
None
in
opam, descr, files_dir
let remove_overlay t name =
OpamFilename.rmdir (OpamPath.Switch.Overlay.package t.root t.switch name)
let has_url_overlay t name =
OpamFilename.exists (OpamPath.Switch.Overlay.url t.root t.switch name)
let dev_package t nv =
if has_url_overlay t (OpamPackage.name nv) &&
pinned_opt t (OpamPackage.name nv) = Some nv
then OpamPath.Switch.dev_package t.root t.switch (OpamPackage.name nv)
else OpamPath.dev_package t.root nv
let pinned_packages t =
OpamPackage.Name.Map.fold
(fun name _ -> OpamPackage.Set.add (pinned t name))
t.pinned OpamPackage.Set.empty
let descr_opt t nv =
OpamMisc.Option.map OpamFile.Descr.read
(descr_file t nv)
let descr t nv =
match descr_file t nv with
| None -> OpamFile.Descr.empty
| Some f -> OpamFile.Descr.read f
let url t nv =
OpamMisc.Option.map OpamFile.URL.read
(url_file t nv)
(* For documentation *)
let global_variable_names =
[ "ocaml-version"; "opam-version"; "compiler";
"preinstalled"; "switch"; "jobs"; "ocaml-native"; "ocaml-tools-opt";
"ocaml-native-dynlink"]
let package_variable_names =
[ "enable"; "installed"; "pinned";
"bin"; "sbin"; "lib"; "man"; "doc"; "share"; "etc";
"name"; "build"; "version"; "depends"; "hash" ]
let string str = Some (S str)
let bool b = Some (B b)
let int i = string (string_of_int i)
let is_global_conf v =
OpamVariable.Full.package v = OpamPackage.Name.global_config
(* Read the env variables *)
let get_env_var v =
let var_str = OpamVariable.to_string (OpamVariable.Full.variable v) in
let var_hook =
if is_global_conf v then
OpamMisc.string_map (function '-' -> '_' | c -> c) var_str
else
Printf.sprintf "%s_%s"
(OpamPackage.Name.to_string (OpamVariable.Full.package v))
var_str
in
try match OpamMisc.getenv ("OPAMVAR_" ^ var_hook) with
| "true" | "1" -> bool true
| "false" | "0" -> bool false
| s -> string s
with Not_found -> None
(* filter handling *)
let resolve_variable t ?opam local_variables v =
let dirname dir = string (OpamFilename.Dir.to_string dir) in
let read_var v =
let var = OpamVariable.Full.variable v in
let c = dot_config t (OpamVariable.Full.package v) in
try OpamFile.Dot_config.variable c var
with Not_found -> None in
let get_local_var v =
if not (is_global_conf v) then None else
let var = OpamVariable.Full.variable v in
try Some (OpamVariable.Map.find var local_variables)
with Not_found -> None
in
let get_global_var v =
if not (is_global_conf v) then None else
match OpamVariable.to_string (OpamVariable.Full.variable v) with
| "ocaml-version" -> string (OpamCompiler.Version.to_string
(Lazy.force t.compiler_version))
| "opam-version" -> string (OpamVersion.to_string OpamVersion.current)
| "compiler" -> string (OpamCompiler.to_string t.compiler)
| "preinstalled" -> bool (OpamFile.Comp.preinstalled
(compiler_comp t t.compiler))
| "switch" -> string (OpamSwitch.to_string t.switch)
| "jobs" -> int (jobs t)
| "ocaml-native" -> bool (Lazy.force OpamSystem.ocaml_native_available)
| "ocaml-tools-opt" -> bool (Lazy.force OpamSystem.ocaml_opt_available)
| "ocaml-native-dynlink" ->
bool (OpamSystem.ocaml_natdynlink_available
(OpamFilename.Dir.to_string (OpamPath.Switch.lib_dir t.root t.switch)))
| _ -> None
in
let get_package_var opam v =
if is_global_conf v then None else
let var_str = OpamVariable.to_string (OpamVariable.Full.variable v) in
let name = OpamVariable.Full.package v in
let opam = (* ensure opam, if not None, corresponds to name *)
match opam with
| Some o when OpamFile.OPAM.name o = name -> opam
| _ when is_name_installed t name ->
opam_opt t (find_installed_package_by_name t name)
| _ -> None
in
let get_nv opam = OpamPackage.create name (OpamFile.OPAM.version opam) in
match var_str, opam with
| "enable", Some _ -> string "enable"
| "enable", None -> string "disable"
| "installed", Some _ -> bool true
| "installed", None -> bool false
| "pinned", _ -> bool (OpamPackage.Name.Map.mem name t.pinned)
| _, None -> None
| "bin", _ -> dirname (OpamPath.Switch.bin t.root t.switch)
| "sbin", _ -> dirname (OpamPath.Switch.sbin t.root t.switch)
| "lib", _ -> dirname (OpamPath.Switch.lib t.root t.switch name)
| "man", _ -> dirname (OpamPath.Switch.man_dir t.root t.switch)
| "doc", _ -> dirname (OpamPath.Switch.doc t.root t.switch name)
| "share", _ -> dirname (OpamPath.Switch.share t.root t.switch name)
| "etc", _ -> dirname (OpamPath.Switch.etc t.root t.switch name)
| "name", _ -> string (OpamPackage.Name.to_string name)
| "build", Some opam ->
dirname (OpamPath.Switch.build t.root t.switch (get_nv opam))
| "version", Some opam ->
let ver = OpamFile.OPAM.version opam in
string (OpamPackage.Version.to_string ver)
| "depends", Some opam ->
let deps =
OpamFormula.atoms (OpamFile.OPAM.depends opam) @
OpamFormula.atoms (OpamFile.OPAM.depopts opam)
in
let installed_deps =
OpamMisc.filter_map
(fun (n,cstr) ->
try
let nv =
OpamPackage.Set.find (fun nv -> OpamPackage.name nv = n) t.installed in
let version = OpamPackage.version nv in
match cstr with
| None -> Some nv
| Some (op,v) when OpamFormula.eval_relop op version v -> Some nv
| Some _ -> None
with Not_found -> None)
deps
in
string (String.concat " " (List.map OpamPackage.to_string installed_deps))
| "hash", Some opam ->
(try
let nv = get_nv opam in
let f = OpamPath.archive t.root nv in
if OpamFilename.exists f then string (OpamFilename.digest f)
else string ""
with Not_found -> string "")
| _, _ -> None
in
let make_package_local v =
(* [var] within the opam file of [pkg] is tried as [pkg:var] *)
match is_global_conf v, opam with
| true, Some opam ->
OpamVariable.Full.create (OpamFile.OPAM.name opam) (OpamVariable.Full.variable v)
| _ -> v
in
let skip _ = None in
let v' = make_package_local v in
let contents =
List.fold_left
(function None -> (fun (f,v) -> f v) | r -> (fun _ -> r))
None
[
get_local_var, v;
get_env_var, v;
(if v' <> v then get_env_var else skip), v';
read_var, v;
(if v' <> v then read_var else skip), v';
get_global_var, v;
get_package_var opam, v';
]
in
if contents = None then
OpamGlobals.error "Variable %s is not defined" (OpamVariable.Full.to_string v);
contents
let eval_filter t ?opam local_variables =
OpamFilter.eval_opt (resolve_variable t ?opam local_variables)
let filter_commands t ?opam local_variables =
OpamFilter.commands (resolve_variable t ?opam local_variables)
let substitute_file t ?opam local_variables =
OpamFilter.substitute_file (resolve_variable t ?opam local_variables)
let substitute_string t ?opam local_variables =
OpamFilter.substitute_string (resolve_variable t ?opam local_variables)
let contents_of_variable t ?opam local_variables =
OpamFilter.contents_of_variable (resolve_variable t ?opam local_variables)
let contents_of_variable_exn t ?opam local_variables =
OpamFilter.contents_of_variable_exn
(resolve_variable t ?opam local_variables)
let redirect t repo =
if repo.repo_kind <> `http then None else
let redirect =
repo
|> OpamPath.Repository.repo
|> OpamFile.Repo.safe_read
|> OpamFile.Repo.redirect
in
let redirect = List.fold_left (fun acc (redirect, filter) ->
if eval_filter t OpamVariable.Map.empty filter then
(redirect, filter) :: acc
else
acc
) [] redirect in
match redirect with
| [] -> None
| (r,f) :: _ ->
let config_f = OpamPath.Repository.config repo in
let config = OpamFile.Repo_config.read config_f in
let repo_address = address_of_string r in
if repo_address <> config.repo_address then (
let config = { config with repo_address } in
OpamFile.Repo_config.write config_f config;
Some (config, f)
) else
None
let copy_files t nv dst =
match files t nv with
| None -> ()
| Some src -> OpamFilename.copy_files ~src ~dst
let consistent_ocaml_version t opam =
let atom (r,v) =
match OpamCompiler.Version.to_string v with
| "system" ->
begin match r with
| `Eq -> t.compiler = OpamCompiler.system
| `Neq -> t.compiler <> OpamCompiler.system
| _ -> OpamSystem.internal_error
"%s is not a valid constraint for the system compiler \
(only '=' and '!=' are valid)."
(OpamFormula.string_of_relop r)
end
| _ -> OpamCompiler.Version.eval_relop r
(Lazy.force t.compiler_version) v
in
match OpamFile.OPAM.ocaml_version opam with
| None -> true
| Some c -> OpamFormula.eval atom c
let consistent_os opam =
match OpamFile.OPAM.os opam with
| Empty -> true
| f ->
let atom (b, os) =
let ($) = if b then (=) else (<>) in
os $ OpamGlobals.os_string () in
OpamFormula.eval atom f
let consistent_available_field t opam =
let env opam = resolve_variable t ~opam OpamVariable.Map.empty in
OpamFilter.eval (env opam) (OpamFile.OPAM.available opam)
(* List the packages which do fulfil the compiler and OS constraints *)
let available_packages t =
let filter nv =
match opam_opt t nv with
| None -> false
| Some opam ->
let has_repository () =
OpamPackage.Map.mem nv t.package_index ||
OpamPackage.Name.Map.mem (OpamPackage.name nv) t.pinned in
consistent_ocaml_version t opam
&& consistent_os opam
&& consistent_available_field t opam
&& has_repository ()
in
let pinned = pinned_packages t in
let pinned_names =
OpamPackage.Name.Map.fold
(fun n _ s -> OpamPackage.Name.Set.add n s)
t.pinned OpamPackage.Name.Set.empty in
let packages =
pinned ++
OpamPackage.Set.filter
(fun nv -> not (OpamPackage.Name.Set.mem (OpamPackage.name nv) pinned_names))
t.packages in
OpamPackage.Set.filter filter packages
(* Display a meaningful error for a package that doesn't exist *)
let unknown_package t (name, _ as atom) =
if OpamPackage.Set.exists (fun nv -> OpamPackage.name nv = name) t.packages
then
Printf.sprintf "No package matches %s."
(OpamFormula.short_string_of_atom atom)
else
Printf.sprintf "No package named %s found."
(OpamPackage.Name.to_string name)
(* Display a meaningful error for an unavailable package *)
let unavailable_reason t (name, _ as atom) =
let reasons () =
let candidates =
OpamPackage.Set.filter (OpamFormula.check atom) t.packages in
let nv = OpamPackage.max_version candidates name in
let r =
let opam = opam t nv in
(if consistent_ocaml_version t opam then [] else [
Printf.sprintf "it requires OCaml %s"
(OpamFormula.string_of_formula
(fun (op,v) ->
OpamFormula.string_of_relop op ^ " " ^
OpamCompiler.Version.to_string v)
(match OpamFile.OPAM.ocaml_version opam with
| Some v -> v
| None -> assert false))
]) @
(if consistent_os opam then [] else [
Printf.sprintf "your OS doesn't match %s"
(OpamFormula.string_of_formula
(fun (b,s) -> (if b then "= " else "!= ") ^ s)
(OpamFile.OPAM.os opam))
]) @
(if consistent_available_field t opam then [] else [
Printf.sprintf "your system doesn't comply with %s"
(string_of_filter (OpamFile.OPAM.available opam))
]) in
match r with
| [] -> raise Not_found
| [r] -> " because " ^ r ^ "."
| rs -> " because:\n" ^ String.concat "\n" (List.map ((^) " - ") rs) in
try
Printf.sprintf
"%s is not available%s"
(OpamFormula.short_string_of_atom atom)
(reasons ())
with Not_found -> try
let pin = OpamPackage.Name.Map.find name t.pinned in
Printf.sprintf
"%s is not available because the package is pinned to %s."
(OpamFormula.short_string_of_atom atom)
(string_of_pin_option pin)
with Not_found ->
unknown_package t atom
let base_packages =
List.map OpamPackage.Name.of_string [ "base-unix"; "base-bigarray"; "base-threads" ]
let create_system_compiler_description root = function
| None -> ()
| Some version ->
log "create-system-compiler-description %a"
(slog OpamCompiler.Version.to_string) version;
match Lazy.force OpamSystem.system_ocamlc_where with
| None -> ()
| Some dir ->
let comp = OpamPath.compiler_comp root OpamCompiler.system in
OpamFilename.remove comp;
let f =
OpamFile.Comp.create_preinstalled
OpamCompiler.system version
(if not !OpamGlobals.no_base_packages then base_packages else [])
[ "CAML_LD_LIBRARY_PATH", "=",
"%{lib}%/stublibs" ^ String.make 1 OpamSystem.path_sep ^
Filename.concat dir "stublibs" ] in
OpamFile.Comp.write comp f
let system_needs_upgrade_displayed = ref false
let system_needs_upgrade t =
t.compiler = OpamCompiler.system
&& match OpamCompiler.Version.system () with
| None ->
if not !system_needs_upgrade_displayed then (
system_needs_upgrade_displayed := true;
OpamGlobals.error
"You current switch use the system compiler, but no OCaml compiler \
has been found in the current path.\n\
You should either:\n\
\ (i) reinstall OCaml version %s on your system; or\n\
\ (ii) use a working compiler switch."
(OpamCompiler.Version.to_string (Lazy.force t.compiler_version))
);
false
| Some v ->
OpamFilename.exists (OpamPath.compiler_comp t.root t.compiler)
&& (Lazy.force t.compiler_version) <> v
let read_repositories root config =
let names = OpamFile.Config.repositories config in
List.fold_left (fun map repo_name ->
let repo = OpamFile.Repo_config.read
(OpamPath.Repository.raw_config root repo_name) in
OpamRepositoryName.Map.add repo_name repo map
) OpamRepositoryName.Map.empty names
(* load partial state to be able to read env variables *)
let load_env_state call_site =
log "LOAD-ENV-STATE(%s)" call_site;
let root = OpamPath.root () in
let config_p = OpamPath.config root in
let config = OpamFile.Config.read config_p in
let switch = match !OpamGlobals.switch with
| `Command_line s
| `Env s -> OpamSwitch.of_string s
| `Not_set -> OpamFile.Config.switch config in
let aliases = OpamFile.Aliases.safe_read (OpamPath.aliases root) in
let compiler =
try OpamSwitch.Map.find switch aliases
with Not_found ->
OpamGlobals.error_and_exit
"The current switch (%s) is an unknown compiler switch."
(OpamSwitch.to_string switch) in
let partial = true in
(* evertything else is empty *)
let compilers = OpamCompiler.Set.empty in
let repositories = OpamRepositoryName.Map.empty in
let compiler_version = lazy (OpamCompiler.Version.of_string "none") in
let opams = OpamPackage.Map.empty in
let packages = OpamPackage.Set.empty in
let available_packages = lazy OpamPackage.Set.empty in
let installed = OpamPackage.Set.empty in
let installed_roots = OpamPackage.Set.empty in
let reinstall = OpamPackage.Set.empty in
let pinned = OpamPackage.Name.Map.empty in
let package_index = OpamPackage.Map.empty in
let compiler_index = OpamCompiler.Map.empty in
{
partial; root; switch; compiler; compiler_version; repositories; opams;
packages; available_packages; installed; installed_roots; reinstall;
config; aliases; pinned; compilers;
package_index; compiler_index;
}
let get_compiler_packages t comp =
let comp = compiler_comp t comp in
let available = OpamPackage.to_map (Lazy.force t.available_packages) in
if OpamPackage.Name.Map.is_empty available then
[]
else (
let pkg_available, pkg_not =
List.partition
(fun (n, _) -> OpamPackage.Name.Map.mem n available)
(OpamFormula.atoms (OpamFile.Comp.packages comp)) in
(* check that all packages in [comp] are in [available] except for
"base-..." (depending if "-no-base-packages" is set or not) *)
let pkg_not = List.rev_map (function (n, _) -> n) pkg_not in
let pkg_not =
if not !OpamGlobals.no_base_packages then
pkg_not
else
List.filter (fun n -> not (List.mem n base_packages)) pkg_not in
if pkg_not <> [] then (
List.iter
(OpamPackage.Name.to_string @> OpamGlobals.error "Package %s not found")
pkg_not;
OpamGlobals.exit 1
);
pkg_available
)
let is_compiler_installed t comp =
OpamSwitch.Map.exists (fun _ c -> c = comp) t.aliases
let is_switch_installed t switch =
OpamSwitch.Map.mem switch t.aliases
let universe t action =
let opams = (* Read overlays of pinned packages *)
OpamPackage.Name.Map.fold (fun name pin map ->
let v = version_of_pin t name pin in
let overlay = OpamPath.Switch.Overlay.opam t.root t.switch name in