forked from ocaml/opam
-
Notifications
You must be signed in to change notification settings - Fork 4
/
client.ml
1922 lines (1732 loc) · 69.6 KB
/
client.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
let log fmt =
Globals.log "CLIENT" fmt
open Solver
type t = {
(* ~/.opam/ *)
global: Path.G.t;
(* ~/.opam/$oversion/ *)
compiler: Path.C.t;
(* ~/.opam/repo/$repo/ *)
repositories: (repository * Path.R.t) list;
(* ~/.opam/opam/ files *)
available: NV.Set.t;
(* [available] but restricted to packages that are supported by the current version of the OCaml installed *)
available_current: NV.Set.t option;
(* WARNING
Instead of [option] we can put [NV.Set.t].
However, at the time of writing, the semantic will change and "make tests" will fail.
This is because [load_state] calls "ocaml -version" as side effect for example.
Note that putting [option] gives a quite optimized data structure now. *)
(* ~/.opam/aliases *)
aliases: (Alias.t * OCaml_V.t) list;
(* ~/.opam/$oversion/pinned *)
pinned: pin_option N.Map.t;
(* ~/.opam/$oversion/installed contents *)
installed: NV.Set.t;
(* ~/.opam/$oversion/reinstall contents *)
reinstall: NV.Set.t;
(* ~/.opam/config contents *)
config: File.Config.t;
(* ~/.opam/repo/index contents *)
repo_index: string list N.Map.t;
}
let print_state t =
let string_of_repos r =
let s (r,p) =
Printf.sprintf "%s:%s"
(Repository.to_string r)
(Dirname.to_string (Path.R.root p)) in
String.concat ", " (List.map s r) in
log "GLOBAL : %s" (Dirname.to_string (Path.G.root t.global));
log "COMPILER : %s" (Dirname.to_string (Path.C.root t.compiler));
log "REPO : %s" (string_of_repos t.repositories);
log "AVAILABLE : %s" (NV.Set.to_string t.available);
log "AVAILABLE_CURRENT : %s" (match t.available_current with None -> "<none>" | Some set -> NV.Set.to_string set);
log "INSTALLED : %s" (NV.Set.to_string t.installed);
log "REINSTALL : %s" (NV.Set.to_string t.reinstall);
log "REPO_INDEX: %s" (N.Map.to_string (String.concat ",") t.repo_index)
let current_ocaml_version t =
let alias = File.Config.ocaml_version t.config in
let aliases = File.Aliases.read (Path.G.aliases t.global) in
List.assoc alias aliases
let confirm fmt =
Printf.kprintf (fun msg ->
Globals.msg "%s [Y/n] " msg;
if not !Globals.yes then
match read_line () with
| "y" | "Y"
| "" -> true
| _ -> false
else
true
) fmt
let update_available_current t =
{ t with available_current =
(* Remove the package which does not fullfil the compiler constraints *)
let ocaml_version =
if File.Config.ocaml_version t.config = Alias.of_string Globals.default_compiler_version then (
let current = OCaml_V.current () in
let system = File.Config.system_ocaml_version t.config in
match current, system with
| None , None -> Globals.error_and_exit "No OCaml compiler installed."
| None , Some s ->
if not (confirm "No OCaml compiler found. Continue ?") then
Globals.exit 1
else
s
| Some c, Some s ->
if s <> c
&& not (confirm "The version of OCaml in your path (%S) \
is not the same as the one OPAM has been \
initialized with (%S). Continue ?"
(OCaml_V.to_string c)
(OCaml_V.to_string s)) then
Globals.exit 1
else
s
| Some c, None -> c
) else
current_ocaml_version t in
let filter nv =
let opam = File.OPAM.read (Path.G.opam t.global nv) in
let consistent_ocaml_version =
match File.OPAM.ocaml_version opam with
| None -> true
| Some (r,v) -> OCaml_V.compare ocaml_version r v in
let consistent_pinned_version =
not (N.Map.mem (NV.name nv) t.pinned) ||
match N.Map.find (NV.name nv) t.pinned with
| Version v -> v = NV.version nv
| _ -> true (* any version is fine, as this will be overloaded on install *) in
consistent_ocaml_version && consistent_pinned_version in
Some (NV.Set.filter filter t.available) }
let get_available_current t =
match t.available_current with
| None -> assert false
| Some v -> v
(* - Look into the content of ~/.opam/config to build the client
state
- Also try to check package consistency as much as possible *)
let load_state () =
log "root path is %s" !Globals.root_path;
let global = Path.G.create () in
let config = File.Config.read (Path.G.config global) in
let ocaml_version = File.Config.ocaml_version config in
let aliases = File.Aliases.safe_read (Path.G.aliases global) in
let compiler = Path.C.create ocaml_version in
let repositories = File.Config.repositories config in
let repositories = List.map (fun r -> r, Path.R.create r) repositories in
let repo_index =
let repo_index = File.Repo_index.safe_read (Path.G.repo_index global) in
let l_wrong =
List.fold_left (fun accu (n, repo_s) ->
List.fold_left (fun accu repo ->
if List.for_all (fun (r, _) -> Repository.name r <> repo) repositories then
(n, repo) :: accu
else
accu
) accu repo_s
) [] (N.Map.bindings repo_index) in
let () = List.iter
(fun (n, repo) ->
Globals.error "File %S: unbound repository %S associated to name %S"
(Filename.to_string (Path.G.repo_index global)) repo (N.to_string n))
l_wrong in
if l_wrong = [] then
repo_index
else
Globals.exit 66 in
let pinned = File.Pinned.safe_read (Path.C.pinned compiler) in
let installed = File.Installed.safe_read (Path.C.installed compiler) in
let reinstall = File.Reinstall.safe_read (Path.C.reinstall compiler) in
let available = Path.G.available_packages global in
let available_current = None in
let t = {
global; compiler; repositories;
available; available_current; installed; reinstall;
repo_index; config; aliases; pinned;
} in
print_state t;
t
type main_function =
| Read_only of (unit -> unit)
| Write_lock of (unit -> unit)
let check f =
if Dirname.exists (Dirname.of_string !Globals.root_path) then
match f with
| Write_lock f -> Run.with_flock f
| Read_only f ->
let warn msg e = Globals.warning "%s: %s" msg (Printexc.to_string e) in
try f () with e ->
if
None = try Some (current_ocaml_version (load_state ())) with e -> let () = warn "check" e in None
then
let () = warn "main" e in
Globals.warning "initialization is not yet finished (or the state %s is inconsistent)" !Globals.root_path
(* NOTE it is feasible to determine here if initialization is finished or not *)
else
raise e
else
Globals.error_and_exit
"Cannot find %s. Have you run 'opam init first ?"
!Globals.root_path
let find_repository_path t name =
let _, r = List.find (fun (r,_) -> Repository.name r = name) t.repositories in
r
let mem_repository, find_repository =
let f name (r,_) = Repository.name r = name in
(fun t name -> List.exists (f name) t.repositories),
fun t name ->
let r, _ = List.find (f name) t.repositories in
r
let string_of_repositories t =
String.concat ", " (List.map (fun (r, _) -> Repository.name r) t.repositories)
let mem_installed_package_by_name t name =
not (NV.Set.is_empty (NV.Set.filter (fun nv -> NV.name nv = name) t.installed))
let find_installed_package_by_name t name =
try NV.Set.choose (NV.Set.filter (fun nv -> NV.name nv = name) t.installed)
with Not_found ->
Globals.error_and_exit "Package %s is not installed" (N.to_string name)
let find_available_package_by_name t name =
let s = NV.Set.filter (fun nv -> NV.name nv = name) (get_available_current t) in
if NV.Set.is_empty s then
None
else
Some s
let print_updated t updated pinned_updated =
if not (NV.Set.is_empty (NV.Set.union updated pinned_updated)) then
Globals.msg "New packages available:\n";
NV.Set.iter (fun nv ->
Globals.msg " - %s%s\n"
(NV.to_string nv)
(if NV.Set.mem nv t.installed then " (*)" else "")
) updated;
NV.Set.iter (fun nv ->
Globals.msg " - %s(+)\n" (N.to_string (NV.name nv))
) pinned_updated
let print_compilers compilers repo =
let repo_compilers = Path.R.available_compilers repo in
let new_compilers = OCaml_V.Set.diff repo_compilers compilers in
if not (OCaml_V.Set.is_empty new_compilers) then
Globals.msg "New compiler descriptions available:\n";
OCaml_V.Set.iter (fun v ->
Globals.msg " - %s\n" (OCaml_V.to_string v)
) new_compilers
let install_conf_ocaml_config t =
let name = N.of_string Globals.default_package in
(* .config *)
let vars =
let map f l = List.map (fun (s,p) -> Variable.of_string s, S (f p)) l in
map Dirname.to_string
[
("prefix", Path.C.root t.compiler);
("lib", Path.C.lib_dir t.compiler);
("bin", Path.C.bin t.compiler);
("doc", Path.C.doc_dir t.compiler);
("stublibs", Path.C.stublibs t.compiler);
("toplevel", Path.C.toplevel t.compiler);
]
@
map (fun x -> x)
[
("user", (Unix.getpwuid (Unix.getuid ())).Unix.pw_name);
("group", (Unix.getgrgid (Unix.getgid ())).Unix.gr_name);
] in
let config = File.Dot_config.create vars in
File.Dot_config.write (Path.C.config t.compiler name) config
let install_conf_ocaml () =
log "installing conf-ocaml";
let t = load_state () in
let name = N.of_string Globals.default_package in
let version = V.of_string (Alias.to_string (File.Config.ocaml_version t.config)) in
let nv = NV.create name version in
(* .opam *)
let opam = File.OPAM.create nv in
File.OPAM.write (Path.G.opam t.global nv) opam;
(* description *)
let descr = File.Descr.create "Compiler configuration flags" in
File.Descr.write (Path.G.descr t.global nv) descr;
install_conf_ocaml_config t;
(* installed *)
let installed_p = Path.C.installed t.compiler in
let installed = File.Installed.safe_read installed_p in
let installed = NV.Set.add nv installed in
File.Installed.write installed_p installed;
(* stublibs *)
let stublibs = Path.C.stublibs t.compiler in
Dirname.mkdir stublibs;
(* toplevel dir *)
let toplevel = Path.C.toplevel t.compiler in
Dirname.mkdir toplevel
let update_repo_index t =
(* Update repo_index *)
let repo_index =
List.fold_left (fun repo_index (r,p) ->
let available = Path.R.available_packages p in
log "repo=%s packages=%s" (Repository.name r) (NV.Set.to_string available);
NV.Set.fold (fun nv repo_index ->
let name = NV.name nv in
let repo = Repository.name r in
if not (N.Map.mem name repo_index) then
N.Map.add name [repo] repo_index
else
let repo_s = N.Map.find name repo_index in
if not (List.mem repo repo_s) then
let repo_index = N.Map.remove name repo_index in
N.Map.add name (repo_s @ [repo]) repo_index
else
repo_index
) available repo_index
) t.repo_index t.repositories in
File.Repo_index.write (Path.G.repo_index t.global) repo_index;
(* suppress previous links *)
Dirname.rmdir (Path.G.opam_dir t.global);
Dirname.mkdir (Path.G.opam_dir t.global);
Dirname.rmdir (Path.G.descr_dir t.global);
Dirname.mkdir (Path.G.descr_dir t.global);
install_conf_ocaml ();
(* Create symbolic links from $repo dirs to main dir *)
N.Map.iter (fun n repo_s ->
let all_versions = ref V.Set.empty in
List.iter (fun r ->
let repo_p = find_repository_path t r in
let available_versions = Path.R.available_versions repo_p n in
V.Set.iter (fun v ->
if not (V.Set.mem v !all_versions) then (
let nv = NV.create n v in
let opam_g = Path.G.opam t.global nv in
let opam_r = Path.R.opam repo_p nv in
let descr_g = Path.G.descr t.global nv in
let descr_r = Path.R.descr repo_p nv in
Filename.link opam_r opam_g;
if Filename.exists descr_r then
Filename.link descr_r descr_g
else
Globals.msg "WARNING: %s does not exist\n" (Filename.to_string descr_r)
)
) available_versions
) repo_s
) repo_index
let update_repo ~show_compilers =
log "update_repo";
let t = load_state () in
let compilers = Path.G.available_compilers t.global in
(* first update all the repo *)
List.iter (fun (r,_) -> Repositories.update r) t.repositories;
(* Display the new compilers available *)
List.iter (fun (_, r) -> if show_compilers then print_compilers compilers r) t.repositories;
(* XXX: we could have a special index for compiler descriptions as
well, but that's become a bit too heavy *)
List.iter (fun (r,p) ->
let comps = Path.R.available_compilers p in
let comp_dir = Path.G.compilers_dir t.global in
OCaml_V.Set.iter (fun o ->
let comp_f = Path.R.compiler p o in
Filename.link_in comp_f comp_dir
) comps
) t.repositories
let update_package ~show_packages =
log "update_packages";
let t = load_state () in
(* Update the pinned packages *)
let pinned_updated =
NV.Set.of_list (
Utils.filter_map
(function
| n, Path p ->
if mem_installed_package_by_name t n then
let nv = find_installed_package_by_name t n in
let build = Path.C.build t.compiler nv in
Globals.msg "Synchronizing %s with %s ...\n" (NV.to_string nv) (Dirname.to_string p);
(* XXX: make it more generic *)
if Dirname.exists build then
match
Run.read_command_output
[ "rsync"; "-arv"; Dirname.to_string p ^ "/"; Dirname.to_string build ]
with
| None -> None
| Some lines ->
match Utils.rsync_trim lines with
| [] -> None
| l -> Some nv
else
None
else
None
| _ -> None)
(N.Map.bindings t.pinned)) in
(* then update $opam/repo/index *)
update_repo_index t;
let updated =
N.Map.fold (fun n repo_s accu ->
(* we do not try to upgrade pinned packages *)
if N.Map.mem n t.pinned then
accu
else (
let all_versions = ref V.Set.empty in
List.fold_left (fun accu repo ->
let p = find_repository_path t repo in
let available_versions = Path.R.available_versions p n in
let new_versions = V.Set.diff available_versions !all_versions in
log "new_versions: %s" (V.Set.to_string new_versions);
if not (V.Set.is_empty new_versions) then (
all_versions := V.Set.union !all_versions new_versions;
let all_updated = File.Updated.safe_read (Path.R.updated p) in
let updated =
NV.Set.filter (fun nv ->
NV.name nv = n && V.Set.mem (NV.version nv) new_versions
) all_updated in
NV.Set.union updated accu
) else
accu
) accu repo_s
)
) t.repo_index NV.Set.empty in
if show_packages then
print_updated t updated pinned_updated;
let updated = NV.Set.union pinned_updated updated in
(* update $opam/$oversion/reinstall *)
Alias.Set.iter (fun alias ->
let t = Path.C.create alias in
let installed = File.Installed.safe_read (Path.C.installed t) in
let reinstall = File.Reinstall.safe_read (Path.C.reinstall t) in
let reinstall =
NV.Set.fold (fun nv reinstall ->
if NV.Set.mem nv installed then
NV.Set.add nv reinstall
else
reinstall
) updated reinstall in
if not (NV.Set.is_empty reinstall) then
File.Reinstall.write (Path.C.reinstall t) reinstall
) (Path.G.available_aliases t.global);
(* Check all the dependencies exist *)
let t = update_available_current (load_state ()) in
let has_error = ref false in
NV.Set.iter (fun nv ->
let opam = File.OPAM.read (Path.G.opam t.global nv) in
let name = File.OPAM.name opam in
let version = File.OPAM.version opam in
if nv <> NV.create name version then
(Globals.error
"The file %s is not consistent with the package %s (%s)"
(Filename.to_string (Path.G.opam t.global nv))
(N.to_string name)
(V.to_string version);
has_error := true);
let depends = File.OPAM.depends opam in
let depopts = File.OPAM.depopts opam in
List.iter (List.iter (fun ((d,_),_) ->
match find_available_package_by_name t (N.of_string d) with
| None ->
let _ = Globals.error
"Package %s depends on the unknown package %s"
(N.to_string (NV.name nv)) d in
has_error := true
| Some _ -> ()
)) (depends @ depopts)
) (get_available_current t);
if !has_error then
Globals.exit 1
let update () =
log "update";
update_repo ~show_compilers:true;
update_package ~show_packages:true
(* Return the contents of a fully qualified variable *)
let contents_of_variable t v =
let name = Full_variable.package v in
let var = Full_variable.variable v in
let installed = mem_installed_package_by_name t name in
if var = Variable.enable && installed then
S "enable"
else if var = Variable.enable && not installed then
S "disable"
else if var = Variable.installed then
B installed
else if not installed then
Globals.error_and_exit "Package %s is not installed" (N.to_string name)
else begin
let c = File.Dot_config.safe_read (Path.C.config t.compiler name) in
try match Full_variable.section v with
| None -> File.Dot_config.variable c var
| Some s -> File.Dot_config.Section.variable c s var
with Not_found ->
Globals.error_and_exit "%s is not defined" (Full_variable.to_string v)
end
(* Substitute the file contents *)
let substitute_file t f =
let f = Filename.of_basename f in
let src = Filename.add_extension f "in" in
let contents = File.Subst.read src in
let newcontents = File.Subst.replace contents (contents_of_variable t) in
File.Subst.write f newcontents
(* Substitue the string contents *)
let substitute_string t s =
File.Subst.replace_string s (contents_of_variable t)
let base_packages = [ "base-unix"; "base-bigarray"; "base-threads" ]
let create_default_compiler_description t =
let ocaml_version = OCaml_V.of_string Globals.default_compiler_version in
let mk name = ((name,None),None) in
let f =
File.Comp.create_preinstalled
ocaml_version
(List.map mk (if !Globals.base_packages then base_packages else []))
[ ("CAML_LD_LIBRARY_PATH", "=",
(Dirname.to_string (Path.C.stublibs t.compiler))
^ ":" ^
(match Run.ocamlc_where () with
| Some d -> Stdlib_filename.concat d "stublibs"
| None -> assert false))
] in
let comp = Path.G.compiler t.global ocaml_version in
File.Comp.write comp f
let add_alias alias ocaml_version =
log "adding alias %s %s" (Alias.to_string alias) (OCaml_V.to_string ocaml_version);
let t = load_state () in
if ocaml_version = OCaml_V.of_string Globals.default_compiler_version then
(* we create a dummy compiler description file the the system-wide
OCaml configuration *)
create_default_compiler_description t;
let aliases_f = Path.G.aliases t.global in
let aliases = File.Aliases.safe_read aliases_f in
if not (List.mem_assoc alias aliases) then begin
(* Install the initial package and reload the global state *)
install_conf_ocaml ();
(* Update the list of aliases *)
File.Aliases.write aliases_f ((alias, ocaml_version) :: aliases);
end
(* We assume that we have the right ocaml-version in $opam/config. Then we:
- create $opam/$alias
- compiles and install $opam/compiler/$descr.comp
Some explanations:
- [f_exists] is called if alias already exists
- if [default_allowed] is set, then 'system' is an allowed alias argument
*)
let init_ocaml f_exists alias default_allowed ocaml_version =
log "init_ocaml";
let t = load_state () in
let default = OCaml_V.of_string Globals.default_compiler_version in
let system_ocaml_version, ocaml_version =
let current () =
match OCaml_V.current () with
| None -> Globals.error_and_exit "No OCaml compiler found in path"
| Some system_ocaml -> Some system_ocaml, default in
match ocaml_version with
| None -> current ()
| Some ocaml_version when default_allowed && ocaml_version = default -> current ()
| Some ocaml_version ->
let comp_f = Path.G.compiler t.global ocaml_version in
if not default_allowed && ocaml_version = default || not (Filename.exists comp_f) then
begin
Globals.msg
" %S is not a valid compiler description%s. The available compilers descriptions are:\n"
(OCaml_V.to_string ocaml_version)
(if ocaml_version = default then " (because it is a reserved name)" else "");
OCaml_V.Set.iter
(fun v -> Globals.msg " - %s\n" (OCaml_V.to_string v))
(Path.G.available_compilers t.global);
Globals.exit 2
end
else
None, ocaml_version in
let alias =
match alias with
| None -> Alias.of_string (OCaml_V.to_string ocaml_version)
| Some alias -> alias in
log "init_ocaml (alias=%s, ocaml_version=%s)" (Alias.to_string alias) (OCaml_V.to_string ocaml_version);
let alias_p = Path.C.create alias in
let alias_p_dir = Path.C.root alias_p in
if Dirname.exists alias_p_dir then
f_exists alias_p_dir
else
begin
Dirname.mkdir alias_p_dir;
(if ocaml_version <> default then
try
let comp_f = Path.G.compiler t.global ocaml_version in
let comp = File.Comp.read comp_f in
if not (File.Comp.preinstalled comp) then begin
(* Install the compiler *)
let comp_src = File.Comp.src comp in
let build_dir = Path.C.build_ocaml alias_p in
Dirname.with_tmp_dir (fun download_dir ->
begin match Filename.download comp_src download_dir with
| None -> Globals.error_and_exit "Cannot download %s" (Filename.to_string comp_src)
| Some f -> Filename.extract f build_dir
end;
let patches = File.Comp.patches comp in
let patches =
Utils.filter_map (fun f ->
match Filename.download f build_dir with
| None -> Globals.error_and_exit "Cannot download %s" (Filename.to_string f)
| Some f -> Some f
) patches in
List.iter (fun f ->
if not (Filename.patch f build_dir) then
Globals.error_and_exit "Cannot apply %s" (Filename.to_string f)
) patches;
let err =
let t =
{ t with
compiler = alias_p;
installed =
let name = N.of_string Globals.default_package in
let version = V.of_string (Alias.to_string alias) in
let nv = NV.create name version in
NV.Set.add nv NV.Set.empty } in
install_conf_ocaml_config t;
if File.Comp.configure comp @ File.Comp.make comp <> [] then begin
Dirname.exec build_dir
[ ( "./configure" :: File.Comp.configure comp )
@ [ "-prefix"; Dirname.to_string alias_p_dir ]
(*-bindir %s/bin -libdir %s/lib -mandir %s/man*)
(* NOTE In case it exists 2 '-prefix', in general the script
./configure will only consider the last one, others will be
discarded. *)
; ( "make" :: File.Comp.make comp )
; [ "make" ; "install" ]
]
end else begin
let builds =
List.map (List.map (substitute_string t)) (File.Comp.build comp) in
Dirname.exec build_dir builds
end in
if err <> 0 then
Globals.error_and_exit
"The compilation of compiler version %s failed"
(OCaml_V.to_string ocaml_version)
)
end
with e ->
if not !Globals.debug then
Dirname.rmdir alias_p_dir;
raise e);
end;
(* write the new version in the configuration file *)
let config = File.Config.with_ocaml_version t.config alias in
let config = File.Config.with_system_ocaml_version config system_ocaml_version in
File.Config.write (Path.G.config t.global) config;
add_alias alias ocaml_version;
ocaml_version
let indent_left s nb =
let nb = nb - String.length s in
if nb <= 0 then
s
else
s ^ String.make nb ' '
let indent_right s nb =
let nb = nb - String.length s in
if nb <= 0 then
s
else
String.make nb ' ' ^ s
let s_not_installed = "--"
let list print_short =
log "list";
let t = load_state () in
(* Get all the installed packages *)
let installed = File.Installed.read (Path.C.installed t.compiler) in
let map, max_n, max_v =
NV.Set.fold
(fun nv (map, max_n, max_v) ->
let name = NV.name nv in
let version = NV.version nv in
if
N.Map.mem name map (* If the packet has been processed yet *)
&&
fst (N.Map.find name map) <> None
(* If moreover the version processed was the version that is installed.
NB at the time of writing there is at most only 1 [version]
installed for a given [name]. *)
then
map, max_n, max_v
else
let is_installed = NV.Set.mem nv installed in
let descr_f = File.Descr.safe_read (Path.G.descr t.global nv) in
let synopsis = File.Descr.synopsis descr_f in
let map = N.Map.add name ((if is_installed then Some version else None), synopsis) map in
let max_n = max max_n (String.length (N.to_string name)) in
let max_v = if is_installed then max max_v (String.length (V.to_string version)) else max_v in
map, max_n, max_v)
t.available
(N.Map.empty, min_int, String.length s_not_installed)
in
N.Map.iter (
if print_short then
fun name _ -> Globals.msg "%s " (N.to_string name)
else
fun name (version, description) ->
let version = match version with
| None -> s_not_installed
| Some v -> V.to_string v in
Globals.msg "%s %s %s\n"
(indent_left (N.to_string name) max_n)
(indent_right version max_v)
description) map
let info package =
log "info %s" (N.to_string package);
let t = load_state () in
let o_v =
let installed = File.Installed.read (Path.C.installed t.compiler) in
try Some (V.Set.choose_one (N.Map.find package (NV.to_map installed)))
with Not_found -> None in
let v_set =
let v_set =
try Path.G.available_versions t.global package
with Not_found ->
Globals.error_and_exit "unknown package %s" (N.to_string package) in
match o_v with
| None -> v_set
| Some v -> V.Set.remove v v_set in
let installed_version = match o_v with
| None -> []
| Some v -> [ "installed-version", V.to_string v ] in
let available_versions =
match List.map V.to_string (V.Set.elements v_set) with
| [] -> []
| [v] -> [ "available-version" , v ]
| l -> [ "available-versions", String.concat ", " l ] in
let libraries, syntax = match o_v with
| None -> [], []
| Some v ->
let opam = File.OPAM.read (Path.G.opam t.global (NV.create package v)) in
let libraries = match File.OPAM.libraries opam with
| [] -> []
| l -> [ "libraries", String.concat ", " (List.map Section.to_string l) ] in
let syntax = match File.OPAM.syntax opam with
| [] -> []
| l -> [ "syntax", String.concat ", " (List.map Section.to_string l) ] in
libraries, syntax in
List.iter
(fun (tit, desc) -> Globals.msg "%20s: %s\n" tit desc)
( [ "package", N.to_string package ]
@ installed_version
@ available_versions
@ libraries
@ syntax
@ let latest = match o_v with
| Some v -> Some v
| None ->
try Some (V.Set.max_elt v_set)
with Not_found -> None in
let descr =
match latest with
| None -> File.Descr.empty
| Some v ->
File.Descr.safe_read (Path.G.descr t.global (NV.create package v)) in
[ "description", File.Descr.full descr ]
)
let proceed_toinstall t nv =
Globals.msg "Installing %s ...\n" (NV.to_string nv);
let t = load_state () in
let name = NV.name nv in
let opam_f = Path.G.opam t.global nv in
let opam = File.OPAM.read opam_f in
let config_f = Path.C.build_config t.compiler nv in
let config = File.Dot_config.safe_read config_f in
let install_f = Path.C.build_install t.compiler nv in
let install = File.Dot_install.safe_read install_f in
Dirname.chdir (Path.C.build t.compiler nv);
(* check that libraries and syntax extensions specified in .opam and
.config are in sync *)
let check kind config_sections opam_sections =
List.iter (fun cs ->
if not (List.mem cs opam_sections) then
Globals.error_and_exit "The %s %s does not appear in %s"
kind (Section.to_string cs) (Filename.to_string opam_f)
) config_sections;
List.iter (fun os ->
if not (List.mem os config_sections) then
Globals.error_and_exit "The %s %s does not appear in %s"
kind (Section.to_string os) (Filename.to_string config_f)
) opam_sections in
if not (Filename.exists config_f) &&
(File.OPAM.libraries opam <> [] || File.OPAM.syntax opam <> []) then
Globals.error_and_exit
"%s does not exists but %s defines some libraries and syntax extensions"
(Filename.to_string config_f)
(Filename.to_string opam_f);
check "library"
(File.Dot_config.Library.available config)
(File.OPAM.libraries opam);
check "syntax"
(File.Dot_config.Syntax.available config)
(File.OPAM.syntax opam);
(* check that depends (in .opam) and requires (in .config) fields
are in almost in sync *)
(* NOTES: the check is partial as we don't know which clause is valid
in depends (XXX there is surely a way to get it from the solver) *)
let local_sections = File.Dot_config.Section.available config in
let libraries_in_opam =
List.fold_left (fun accu l ->
List.fold_left (fun accu ((n,_),_) ->
let n = N.of_string n in
let nv = find_installed_package_by_name t n in
let opam = File.OPAM.read (Path.G.opam t.global nv) in
let libs = File.OPAM.libraries opam in
let syntax = File.OPAM.syntax opam in
List.fold_right Section.Set.add (libs @ syntax) accu
) accu l
) Section.Set.empty (File.OPAM.depends opam) in
let libraries_in_config =
List.fold_left (fun accu s ->
List.fold_left (fun accu r ->
Section.Set.add r accu
) accu (File.Dot_config.Section.requires config s)
) Section.Set.empty local_sections in
Section.Set.iter (fun s ->
if not (List.mem s local_sections)
&& not (Section.Set.mem s libraries_in_opam) then
let config_f = Filename.to_string (Path.C.build_config t.compiler nv) in
let opam_f = Filename.to_string (Path.G.opam t.global nv) in
let local_sections = List.map Section.to_string local_sections in
let opam_sections = List.map Section.to_string (Section.Set.elements libraries_in_opam) in
Globals.error_and_exit
"%s appears as a library dependency in %s, but:\n\
- %s defines the libraries {%s}\n\
- Packages in %s defines the libraries {%s}"
(Section.to_string s) config_f
config_f (String.concat ", " local_sections)
opam_f (String.concat ", " opam_sections)
) libraries_in_config;
(* .install *)
File.Dot_install.write (Path.C.install t.compiler name) install;
(* .config *)
File.Dot_config.write (Path.C.config t.compiler name) config;
(* lib *)
let lib = Path.C.lib t.compiler name in
List.iter (fun f -> Filename.copy_in f lib) (File.Dot_install.lib install);
(* toplevel *)
let toplevel = Path.C.toplevel t.compiler in
List.iter (fun f -> Filename.copy_in f toplevel) (File.Dot_install.toplevel install);
(* bin *)
List.iter (fun (src, dst) ->
let dst = Path.C.bin t.compiler // (Basename.to_string dst) in
Filename.copy src dst
) (File.Dot_install.bin install);
(* misc *)
List.iter
(fun (src, dst) ->
if Filename.exists dst && confirm "Overwriting %s ?" (Filename.to_string dst) then
Filename.copy src dst
else begin
Globals.msg "Installing %s to %s.\n" (Filename.to_string src) (Filename.to_string dst);
if confirm "Continue ?" then
Filename.copy src dst
end
) (File.Dot_install.misc install)
let pinned_path t nv =
let name = NV.name nv in
if N.Map.mem name t.pinned then
match N.Map.find name t.pinned with
| Path p -> Some p
| _ -> None
else
None
let get_archive t nv =
log "get_archive %s" (NV.to_string nv);
let name = NV.name nv in
let rec aux = function
| [] ->
Globals.error_and_exit
"Unable to find a repository containing %s"
(NV.to_string nv)
| repo :: repo_s ->
let repo_p = find_repository_path t repo in
let repo = find_repository t repo in
let opam_f = Path.R.opam repo_p nv in
if Filename.exists opam_f then (
Repositories.download repo nv;
let src = Path.R.archive repo_p nv in
let dst = Path.G.archive t.global nv in
Filename.link src dst;
dst
) else
aux repo_s in
aux (N.Map.find name t.repo_index)
let extract_package t nv =
log "extract_package: %s" (NV.to_string nv);
let p_build = Path.C.build t.compiler nv in
Dirname.rmdir p_build;
match pinned_path t nv with
| None ->
let archive = get_archive t nv in
Globals.msg "Extracting ...\n";
Filename.extract archive p_build;
p_build
| Some p ->
(* XXX: make it a bit more generic ... *)
Globals.msg "Synchronizing %s with %s ...\n" (NV.to_string nv) (Dirname.to_string p);
let err = Run.command [ "rsync"; "-arv"; Dirname.to_string p ^ "/"; Dirname.to_string p_build ] in
if err <> 0 then
Globals.error_and_exit "Cannot synchronize %s with %s." (Dirname.to_string p) (Dirname.to_string p_build);
p_build
let proceed_todelete t nv =
log "deleting %s" (NV.to_string nv);
Globals.msg "Uninstalling %s ...\n" (NV.to_string nv);
let name = NV.name nv in
(* Run the remove script *)
let opam = File.OPAM.read (Path.G.opam t.global nv) in
let remove = List.map (List.map (substitute_string t)) (File.OPAM.remove opam) in
let p_build = Path.C.build t.compiler nv in
if not (Dirname.exists p_build) then
ignore (extract_package t nv);
(* We try to run the remove scripts in the folder where it was extracted
If it does not exist, we don't really care. *)
let err = Dirname.exec ~add_to_path:[Path.C.bin t.compiler] p_build remove in
if err <> 0 then
Globals.error_and_exit "Cannot uninstall %s" (NV.to_string nv);
(* Remove the libraries *)
Dirname.rmdir (Path.C.lib t.compiler name);
Dirname.rmdir (Path.C.build t.compiler nv);
(* Clean-up the repositories *)
let repos =
try N.Map.find (NV.name nv) t.repo_index
with _ -> [] in
List.iter (fun r ->
let p = find_repository_path t r in
let archive = Path.R.archive p nv in
let tmp_dir = Path.R.tmp_dir p nv in
Filename.remove archive;
Dirname.rmdir tmp_dir
) repos;