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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
open OpamTypes
open OpamTypesBase
let log ?level fmt = OpamConsole.log ?level "CUDF" fmt
let slog = OpamConsole.slog
let s_source = "opam-name"
let s_source_number = "opam-version"
let s_reinstall = "reinstall"
let s_installed_root = "installed-root"
let s_pinned = "pinned"
let s_version_lag = "version-lag"
let opam_invariant_package_name =
Common.CudfAdd.encode "=opam-invariant"
let opam_invariant_package_version = 1
let opam_invariant_package =
opam_invariant_package_name, opam_invariant_package_version
let is_opam_invariant p =
p.Cudf.package = opam_invariant_package_name
let cudf2opam cpkg =
if is_opam_invariant cpkg then
OpamConsole.error_and_exit `Internal_error
"Internal error: tried to access the CUDF opam invariant as an opam \
package";
let sname = Cudf.lookup_package_property cpkg s_source in
let name = OpamPackage.Name.of_string sname in
let sver = Cudf.lookup_package_property cpkg s_source_number in
let version = OpamPackage.Version.of_string sver in
OpamPackage.create name version
let cudfnv2opam ?version_map ?cudf_universe (name,v) =
let nv = match cudf_universe with
| None -> None
| Some u ->
try Some (cudf2opam (Cudf.lookup_package u (name,v)))
with Not_found -> None
in
match nv with
| Some nv -> nv
| None ->
let name = OpamPackage.Name.of_string (Common.CudfAdd.decode name) in
match version_map with
| Some vmap ->
let nvset =
OpamPackage.Map.filter
(fun nv cv -> nv.name = name && cv = v)
vmap
in
fst (OpamPackage.Map.choose nvset)
| None -> raise Not_found
let string_of_package p =
let installed = if p.Cudf.installed then "installed" else "not-installed" in
Printf.sprintf "%s.%d(%s)"
p.Cudf.package
p.Cudf.version installed
let string_of_packages l =
OpamStd.List.to_string string_of_package l
module Json = struct
let (>>=) = OpamStd.Option.Op.(>>=)
let int_to_json n : OpamJson.t = `Float (float_of_int n)
let int_of_json = function
| `Float x -> Some (int_of_float x)
| _ -> None
let string_to_json s : OpamJson.t = `String s
let string_of_json = function
| `String s -> Some s
| _ -> None
let pkgname_to_json name : OpamJson.t = string_to_json name
let pkgname_of_json json = string_of_json json
let bool_to_json bool : OpamJson.t = `Bool bool
let bool_of_json = function
| `Bool b -> Some b
| _ -> None
let list_to_json elem_to_json li : OpamJson.t =
`A (List.map elem_to_json li)
let list_of_json elem_of_json = function
| `A jsons ->
begin try
let get = function
| None -> raise Not_found
| Some v -> v
in
Some (List.map (fun json -> get (elem_of_json json)) jsons)
with Not_found -> None
end
| _ -> None
let option_to_json elem_to_json = function
| None -> `Null
| Some elem ->
let json = elem_to_json elem in
assert (json <> `Null);
json
let option_of_json elem_of_json = function
| `Null -> Some None
| other ->
elem_of_json other >>= fun elem -> Some (Some elem)
let pair_to_json
fst_field fst_to_json
snd_field snd_to_json (fst, snd) =
`O [(fst_field, fst_to_json fst);
(snd_field, snd_to_json snd)]
let pair_of_json
fst_field fst_of_json
snd_field snd_of_json : OpamJson.t -> _ = function
| `O dict ->
begin try
fst_of_json (List.assoc fst_field dict) >>= fun fst ->
snd_of_json (List.assoc snd_field dict) >>= fun snd ->
Some (fst, snd)
with Not_found -> None
end
| _ -> None
let version_to_json n = int_to_json n
let version_of_json json = int_of_json json
let relop_to_json : Cudf_types.relop -> _ = function
| `Eq -> `String "eq"
| `Neq -> `String "neq"
| `Geq -> `String "geq"
| `Gt -> `String "gt"
| `Leq -> `String "leq"
| `Lt -> `String "lt"
let relop_of_json : _ -> Cudf_types.relop option = function
| `String "eq" -> Some `Eq
| `String "neq" -> Some `Neq
| `String "geq" -> Some `Geq
| `String "gt" -> Some `Gt
| `String "leq" -> Some `Leq
| `String "lt" -> Some `Lt
| _ -> None
let enum_keep_to_json = function
| `Keep_version -> `String "keep_version"
| `Keep_package -> `String "keep_package"
| `Keep_feature -> `String "keep_feature"
| `Keep_none -> `String "keep_none"
let enum_keep_of_json = function
| `String "keep_version" -> Some (`Keep_version)
| `String "keep_package" -> Some (`Keep_package)
| `String "keep_feature" -> Some (`Keep_feature)
| `String "keep_none" -> Some (`Keep_none)
| _ -> None
let constr_to_json constr =
option_to_json
(pair_to_json "relop" relop_to_json "version" version_to_json)
constr
let constr_of_json json =
option_of_json
(pair_of_json "relop" relop_of_json "version" version_of_json)
json
let vpkg_to_json v =
pair_to_json "pkgname" pkgname_to_json "constr" constr_to_json v
let vpkg_of_json json =
pair_of_json "pkgname" pkgname_of_json "constr" constr_of_json json
let vpkglist_to_json (vpkglist : Cudf_types.vpkglist) =
list_to_json vpkg_to_json vpkglist
let vpkglist_of_json jsons : Cudf_types.vpkglist option =
list_of_json vpkg_of_json jsons
let veqpkg_to_json veqpkg = vpkg_to_json (veqpkg :> Cudf_types.vpkg)
let veqpkg_of_json json =
vpkg_of_json json >>= function
| (pkgname, None) -> Some (pkgname, None)
| (pkgname, Some (`Eq, version)) -> Some (pkgname, Some (`Eq, version))
| (_pkgname, Some (_, _version)) -> None
let veqpkglist_to_json veqpkglist = list_to_json veqpkg_to_json veqpkglist
let veqpkglist_of_json jsons = list_of_json veqpkg_of_json jsons
let vpkgformula_to_json formula =
list_to_json (list_to_json vpkg_to_json) formula
let vpkgformula_of_json json =
list_of_json (list_of_json vpkg_of_json) json
let binding_to_json value_to_json v =
pair_to_json "key" string_to_json "value" value_to_json v
let binding_of_json value_of_json v =
pair_of_json "key" string_of_json "value" value_of_json v
let stanza_to_json value_to_json stanza =
list_to_json (binding_to_json value_to_json) stanza
let stanza_of_json value_of_json json =
list_of_json (binding_of_json value_of_json) json
let type_schema_to_json tag value_to_json value =
pair_to_json
"type" string_to_json
"default" (option_to_json value_to_json)
(tag, value)
let rec typedecl1_to_json = function
| `Int n ->
type_schema_to_json "int" int_to_json n
| `Posint n ->
type_schema_to_json "posint" int_to_json n
| `Nat n ->
type_schema_to_json "nat" int_to_json n
| `Bool b ->
type_schema_to_json "bool" bool_to_json b
| `String s ->
type_schema_to_json "string" string_to_json s
| `Pkgname s ->
type_schema_to_json "pkgname" pkgname_to_json s
| `Ident s ->
type_schema_to_json "ident" string_to_json s
| `Enum (enums, v) ->
pair_to_json
"type" string_to_json
"default" (pair_to_json
"set" (list_to_json string_to_json)
"default" (option_to_json string_to_json))
("enum", (enums, v))
| `Vpkg v ->
type_schema_to_json "vpkg" vpkg_to_json v
| `Vpkgformula v ->
type_schema_to_json "vpkgformula" vpkgformula_to_json v
| `Vpkglist v ->
type_schema_to_json "vpkglist" vpkglist_to_json v
| `Veqpkg v ->
type_schema_to_json "veqpkg" veqpkg_to_json v
| `Veqpkglist v ->
type_schema_to_json "veqpkglist" veqpkglist_to_json v
| `Typedecl td ->
type_schema_to_json "typedecl" typedecl_to_json td
and typedecl_to_json td =
stanza_to_json typedecl1_to_json td
let rec typedecl1_of_json json =
pair_of_json "type" string_of_json "default" (fun x -> Some x) json >>=
fun (tag, json) ->
match tag with
| "int" -> option_of_json int_of_json json >>= fun x -> Some (`Int x)
| "posint" -> option_of_json int_of_json json >>= fun x -> Some (`Posint x)
| "nat" -> option_of_json int_of_json json >>= fun x -> Some (`Nat x)
| "bool" -> option_of_json bool_of_json json >>= fun x -> Some (`Bool x)
| "string" -> option_of_json string_of_json json >>= fun x -> Some (`String x)
| "pkgname" -> option_of_json string_of_json json >>= fun x -> Some (`Pkgname x)
| "ident" -> option_of_json string_of_json json >>= fun x -> Some (`Ident x)
| "enum" ->
pair_of_json
"set" (list_of_json string_of_json)
"default" (option_of_json string_of_json)
json >>= fun x -> Some (`Enum x)
| "vpkg" ->
option_of_json vpkg_of_json json >>= fun x -> Some (`Vpkg x)
| "vpkgformula" ->
option_of_json vpkgformula_of_json json >>= fun x -> Some (`Vpkgformula x)
| "vpkglist" ->
option_of_json vpkglist_of_json json >>= fun x -> Some (`Vpkglist x)
| "veqpkg" ->
option_of_json veqpkg_of_json json >>= fun x -> Some (`Veqpkg x)
| "veqpkglist" ->
option_of_json veqpkglist_of_json json >>= fun x -> Some (`Veqpkglist x)
| "typedecl" ->
option_of_json typedecl_of_json json >>= fun x -> Some (`Typedecl x)
| _ -> None
and typedecl_of_json json =
stanza_of_json typedecl1_of_json json
let type_tagged_to_json tag value_to_json value =
pair_to_json "type" string_to_json "value" value_to_json (tag, value)
let typed_value_to_json : Cudf_types.typed_value -> _ = function
| `Int n ->
type_tagged_to_json "int" int_to_json n
| `Posint n ->
type_tagged_to_json "posint" int_to_json n
| `Nat n ->
type_tagged_to_json "nat" int_to_json n
| `Bool b ->
type_tagged_to_json "bool" bool_to_json b
| `String s ->
type_tagged_to_json "string" string_to_json s
| `Pkgname name ->
type_tagged_to_json "pkgname" pkgname_to_json name
| `Ident id ->
type_tagged_to_json "ident" string_to_json id
| `Enum (enums, value) ->
type_tagged_to_json "enum"
(pair_to_json
"set" (list_to_json string_to_json)
"choice" string_to_json) (enums, value)
| `Vpkg vpkg ->
type_tagged_to_json "vpkg" vpkg_to_json vpkg
| `Vpkgformula vpkgformula ->
type_tagged_to_json "vpkgformula" vpkgformula_to_json vpkgformula
| `Vpkglist vpkglist ->
type_tagged_to_json "vpkglist" vpkglist_to_json vpkglist
| `Veqpkg veqpkg ->
type_tagged_to_json "veqpkg" veqpkg_to_json veqpkg
| `Veqpkglist veqpkglist ->
type_tagged_to_json "veqpkglist" veqpkglist_to_json veqpkglist
| `Typedecl typedecl ->
type_tagged_to_json "typedecl" typedecl_to_json typedecl
let typed_value_of_json json : Cudf_types.typed_value option =
pair_of_json "type" string_of_json "value" (fun x -> Some x) json >>=
fun (tag, json) ->
match tag with
| "int" -> int_of_json json >>= fun x -> Some (`Int x)
| "posint" -> int_of_json json >>= fun x -> Some (`Posint x)
| "nat" -> int_of_json json >>= fun x -> Some (`Nat x)
| "bool" -> bool_of_json json >>= fun x -> Some (`Bool x)
| "string" -> string_of_json json >>= fun x -> Some (`String x)
| "pkgname" -> string_of_json json >>= fun x -> Some (`Pkgname x)
| "ident" -> string_of_json json >>= fun x -> Some (`Ident x)
| "enum" -> pair_of_json
"set" (list_of_json string_of_json)
"choice" string_of_json
json >>= fun p -> Some (`Enum p)
| "vpkg" -> vpkg_of_json json >>= fun x -> Some (`Vpkg x)
| "vpkgformula" -> vpkgformula_of_json json >>= fun x -> Some (`Vpkgformula x)
| "vpkglist" -> vpkglist_of_json json >>= fun x -> Some (`Vpkglist x)
| "veqpkg" -> veqpkg_of_json json >>= fun x -> Some (`Veqpkg x)
| "veqpkglist" -> veqpkglist_of_json json >>= fun x -> Some (`Veqpkglist x)
| "typedecl" -> typedecl_of_json json >>= fun x -> Some (`Typedecl x)
| _ -> None
let package_to_json p =
`O [ ("name", pkgname_to_json p.Cudf.package);
("version", version_to_json p.Cudf.version);
("depends", vpkgformula_to_json p.Cudf.depends);
("conflicts", vpkglist_to_json p.Cudf.conflicts);
("provides", veqpkglist_to_json p.Cudf.provides);
("installed", bool_to_json p.Cudf.installed);
("was_installed", bool_to_json p.Cudf.was_installed);
("keep", enum_keep_to_json p.Cudf.keep);
("pkg_extra", stanza_to_json typed_value_to_json p.Cudf.pkg_extra);
]
let package_of_json = function
| `O dict ->
begin try
pkgname_of_json (List.assoc "name" dict) >>= fun package ->
version_of_json (List.assoc "version" dict) >>= fun version ->
vpkgformula_of_json (List.assoc "depends" dict) >>= fun depends ->
vpkglist_of_json (List.assoc "conflicts" dict) >>= fun conflicts ->
veqpkglist_of_json (List.assoc "provides" dict) >>= fun provides ->
bool_of_json (List.assoc "installed" dict) >>= fun installed ->
bool_of_json (List.assoc "was_installed" dict) >>= fun was_installed ->
enum_keep_of_json (List.assoc "keep" dict) >>= fun keep ->
stanza_of_json typed_value_of_json (List.assoc "pkg_extra" dict) >>= fun ->
Some { Cudf.package = package;
version;
depends;
conflicts;
provides;
installed;
was_installed;
keep;
pkg_extra;
}
with Not_found -> None
end
| _ -> None
end
let to_json = Json.package_to_json
let of_json = Json.package_of_json
module Package = struct
type t = Cudf.package
include Common.CudfAdd
let to_string = string_of_package
let name_to_string t = t.Cudf.package
let version_to_string t = string_of_int t.Cudf.version
let to_json = to_json
let of_json = of_json
end
module Action = OpamActionGraph.MakeAction(Package)
module ActionGraph = OpamActionGraph.Make(Action)
let string_of_action = Action.to_string
let string_of_actions l =
OpamStd.List.to_string (fun a -> " - " ^ string_of_action a) l
exception Solver_failure of string
exception Cyclic_actions of Action.t list list
type conflict_case =
| Conflict_dep of (unit -> Algo.Diagnostic.reason list)
| Conflict_cycle of string list list
type conflict =
Cudf.universe * int package_map * conflict_case
module Map = OpamStd.Map.Make(Package)
module Set = OpamStd.Set.Make(Package)
let strong_and_weak_deps u deps =
List.fold_left (fun (strong_deps, weak_deps) l ->
let names =
List.fold_left (fun acc (n, _) ->
OpamStd.String.Map.add n Set.empty acc)
OpamStd.String.Map.empty l
in
let set =
List.fold_left (fun acc (n, cstr) ->
List.fold_left (fun s x -> Set.add x s)
acc (Cudf.lookup_packages ~filter:cstr u n))
Set.empty l
in
let by_name =
Set.fold (fun p ->
OpamStd.String.Map.update
p.Cudf.package (Set.add p) Set.empty)
set names
in
if OpamStd.String.Map.is_singleton by_name then
let name, versions = OpamStd.String.Map.choose by_name in
OpamStd.String.Map.update name (Set.inter versions) versions
strong_deps,
OpamStd.String.Map.remove name weak_deps
else
let by_name =
OpamStd.String.Map.filter
(fun name _ -> not (OpamStd.String.Map.mem name strong_deps))
by_name
in
strong_deps, OpamStd.String.Map.union Set.union weak_deps by_name)
(OpamStd.String.Map.empty, OpamStd.String.Map.empty)
deps
let dependency_set u deps =
let strong_deps, weak_deps = strong_and_weak_deps u deps in
OpamStd.String.Map.fold (fun _ -> Set.union) strong_deps @@
OpamStd.String.Map.fold (fun _ -> Set.union) weak_deps @@
Set.empty
let _strong_dependency_set u deps =
let strong_deps, _ = strong_and_weak_deps u deps in
OpamStd.String.Map.fold (fun _ -> Set.union) strong_deps Set.empty
let rec_strong_dependency_map u deps =
let module SM = OpamStd.String.Map in
let rec aux seen deps =
let strong_deps, _ = strong_and_weak_deps u deps in
OpamStd.String.Map.fold (fun name ps (seen, acc) ->
let seen, common_strong_deps =
Set.fold (fun p (seen, acc) ->
let seen, dmap =
try seen, Map.find p seen with Not_found ->
let seen, r = aux (Map.add p SM.empty seen) p.Cudf.depends in
Map.add p r seen, r
in
seen,
Some (match acc with
| None -> dmap
| Some m ->
SM.merge (fun _ a b -> match a, b with
| Some a, Some b -> Some (Set.union a b)
| _ -> None)
m dmap))
ps (seen, None)
in
let strong_deps =
SM.add name ps
(OpamStd.Option.default SM.empty common_strong_deps)
in
seen, SM.union Set.inter acc strong_deps)
strong_deps (seen, SM.empty)
in
snd (aux Map.empty deps)
let _rec_strong_dependency_set u deps =
OpamStd.String.Map.fold (fun _ -> Set.union)
(rec_strong_dependency_map u deps)
Set.empty
module Graph = struct
module PG = struct
include Algo.Defaultgraphs.PackageGraph.G
let succ g v =
try succ g v
with e -> OpamStd.Exn.fatal e; []
end
module PO = Algo.Defaultgraphs.GraphOper (PG)
module Topo = Graph.Topological.Make (PG)
let of_universe u =
let t = OpamConsole.timer () in
let g = PG.create ~size:(Cudf.universe_size u) () in
let iter_deps f deps =
Set.iter f (dependency_set u deps)
in
Cudf.iter_packages
(fun p ->
PG.add_vertex g p;
iter_deps (PG.add_edge g p) p.Cudf.depends)
u;
log ~level:3 "Graph generation: %.3f" (t ());
g
let output g filename =
let fd = open_out (filename ^ ".dot") in
Algo.Defaultgraphs.PackageGraph.DotPrinter.output_graph fd g;
close_out fd
let transitive_closure g =
PO.O.add_transitive_closure g
let linearize g pkgs =
Topo.fold (fun p acc -> if Set.mem p pkgs then p::acc else acc) g []
let mirror = PO.O.mirror
include PG
end
(** Special package used by Dose internally, should generally be filtered out *)
let dose_dummy_request = Algo.Depsolver.dummy_request.Cudf.package
let is_artefact cpkg =
is_opam_invariant cpkg ||
cpkg.Cudf.package = dose_dummy_request
let dependencies universe packages =
Set.fixpoint (fun p -> dependency_set universe p.Cudf.depends) packages
let reverse_dependencies universe packages =
let graph = Graph.of_universe universe in
Set.fixpoint (fun p -> Set.of_list (Graph.pred graph p)) packages
let dependency_sort universe packages =
let graph = Graph.of_universe universe in
Graph.linearize graph packages |> List.rev
let string_of_atom (p, c) =
let const = function
| None -> ""
| Some (r,v) -> Printf.sprintf " (%s %d)" (OpamPrinter.FullPos.relop_kind r) v in
Printf.sprintf "%s%s" p (const c)
let string_of_vpkgs constr =
let constr = List.sort (fun (a,_) (b,_) -> String.compare a b) constr in
OpamFormula.string_of_conjunction string_of_atom constr
let string_of_universe u =
string_of_packages (List.sort Common.CudfAdd.compare (Cudf.get_packages u))
let vpkg2atom cudfnv2opam (name,cstr) =
match cstr with
| None ->
OpamPackage.Name.of_string (Common.CudfAdd.decode name), None
| Some (relop,v) ->
let nv = cudfnv2opam (name,v) in
nv.name, Some (relop, nv.version)
let conflict_empty ~version_map univ =
Conflicts (univ, version_map, Conflict_dep (fun () -> []))
let make_conflicts ~version_map univ = function
| {Algo.Diagnostic.result = Algo.Diagnostic.Failure f; _} ->
Conflicts (univ, version_map, Conflict_dep f)
| {Algo.Diagnostic.result = Algo.Diagnostic.Success _; _} ->
raise (Invalid_argument "make_conflicts")
let cycle_conflict ~version_map univ cycle =
Conflicts (univ, version_map, Conflict_cycle cycle)
let arrow_concat sl =
let arrow =
Printf.sprintf " %s "
(OpamConsole.utf8_symbol OpamConsole.Symbols.rightwards_arrow "->")
in
String.concat (OpamConsole.colorise `yellow arrow) sl
let formula_of_vpkgl cudfnv2opam all_packages vpkgl =
let atoms =
List.map (fun vp ->
try vpkg2atom cudfnv2opam vp
with Not_found ->
OpamPackage.Name.of_string (Common.CudfAdd.decode (fst vp)), None)
vpkgl
in
let names = OpamStd.List.sort_nodup compare (List.map fst atoms) in
let by_name =
List.map (fun name ->
let formula =
OpamFormula.ors (List.map (function
| n, Some atom when n = name -> Atom atom
| _ -> Empty)
atoms)
in
let all_versions = OpamPackage.versions_of_name all_packages name in
let formula = OpamFormula.simplify_version_set all_versions formula in
Atom (name, formula))
names
in
OpamFormula.ors by_name
module ChainSet = struct
include OpamStd.Set.Make (struct
type t = Package.t list
let rec compare t1 t2 = match t1, t2 with
| [], [] -> 0
| [], _ -> -1
| _, [] -> 1
| p1::r1, p2::r2 ->
match Package.compare p1 p2 with 0 -> compare r1 r2 | n -> n
let to_string t =
arrow_concat (List.rev_map Package.to_string t)
let to_json t = Json.list_to_json Package.to_json t
let of_json j = Json.list_of_json Package.of_json j
end)
(** Turns a set of lists into a list of sets *)
let rec transpose cs =
let hds, tls =
fold (fun c (hds, tls) -> match c with
| hd::tl -> Set.add hd hds, add tl tls
| [] -> hds, tls)
cs (Set.empty, empty)
in
if Set.is_empty hds then []
else hds :: transpose tls
(** cs1 precludes cs2 if it contains a list that is prefix to all elements of
cs2 *)
let precludes cs1 cs2 =
let rec list_is_prefix pfx l = match pfx, l with
| [], _ -> true
| a::r1, b::r2 when Package.equal a b -> list_is_prefix r1 r2
| _ -> false
in
exists (fun pfx -> for_all (fun l -> list_is_prefix pfx l) cs2) cs1
let length cs = fold (fun l acc -> min (List.length l) acc) cs max_int
end
type explanation =
[ `Conflict of string option * string list * bool
| `Missing of string option * string *
(OpamPackage.Name.t * OpamFormula.version_formula)
OpamFormula.formula
]
let packages cudfnv2opam reasons : explanation list =
log "Conflict reporting";
let open Algo.Diagnostic in
let open Set.Op in
let module CS = ChainSet in
let all_opam =
let add p set =
if is_artefact p then set
else OpamPackage.Set.add (cudf2opam p) set
in
List.fold_left (fun acc -> function
| Conflict (l, r, _) -> add l @@ add r @@ acc
| Dependency (l, _, rs) ->
List.fold_left (fun acc p -> add p acc) (add l acc) rs
| Missing (p, _) -> add p acc)
OpamPackage.Set.empty
reasons
in
let print_set pkgs =
if Set.exists is_artefact pkgs then
if Set.exists is_opam_invariant pkgs then "(invariant)"
else "(request)"
else
let nvs =
OpamPackage.to_map @@
Set.fold (fun p s -> OpamPackage.Set.add (cudf2opam p) s)
pkgs OpamPackage.Set.empty
in
let strs =
OpamPackage.Name.Map.mapi (fun name versions ->
let all_versions = OpamPackage.versions_of_name all_opam name in
let formula =
OpamFormula.formula_of_version_set all_versions versions
in
OpamFormula.to_string (Atom (name, formula)))
nvs
in
String.concat ", " (OpamPackage.Name.Map.values strs)
in
let cs_to_string ?(hl_last=true) cs =
let rec aux vpkgl = function
| [] -> []
| pkgs :: r ->
let vpkgl1 =
List.fold_left (fun acc -> function
| Dependency (p1, vpl, _) when Set.mem p1 pkgs ->
List.rev_append vpl acc
| _ -> acc)
[] reasons
in
if Set.exists is_artefact pkgs then
if Set.exists is_opam_invariant pkgs then
Printf.sprintf "(invariant)"
:: aux vpkgl1 r
else if r = [] then ["(request)"]
else aux vpkgl1 r
else if vpkgl = [] then
print_set pkgs :: aux vpkgl1 r
else
let f =
let vpkgl =
List.filter
(fun (n, _) -> Set.exists (fun p -> p.package = n) pkgs)
vpkgl
in
formula_of_vpkgl cudfnv2opam packages vpkgl
in
let s = OpamFormula.to_string f in
(if hl_last && r = [] then OpamConsole.colorise' [`red;`bold] s else s)
:: aux vpkgl1 r
in
arrow_concat (aux [] (CS.transpose (CS.map List.rev cs)))
in
let get t x = try Hashtbl.find t x with Not_found -> Set.empty in
let add_set t l set =
match Hashtbl.find t l with
| exception Not_found -> Hashtbl.add t l set
| s -> Hashtbl.replace t l (Set.union set s)
in
let ct = Hashtbl.create 53 in
let deps = Hashtbl.create 53 in
let rdeps = Hashtbl.create 53 in
let missing = Hashtbl.create 53 in
List.iter (function
| Conflict (l, r, _) ->
add_set ct l (Set.singleton r);
add_set ct r (Set.singleton l)
| Dependency (l, _, rs) ->
add_set deps l (Set.of_list rs);
List.iter (fun r -> add_set rdeps r (Set.singleton l)) rs
| Missing (p, deps) ->
Hashtbl.add missing p deps)
reasons;
let roots =
let add_artefacts set =
Hashtbl.fold (fun p _ acc ->
if is_artefact p then Set.add p acc else acc)
set
in
Set.empty |> add_artefacts deps |> add_artefacts missing |> add_artefacts ct
in
let conflicting =
Hashtbl.fold (fun p _ -> Set.add p) ct Set.empty
in
let all_conflicting =
Hashtbl.fold (fun k _ acc -> Set.add k acc) missing conflicting
in
let ct_chains =
let rec aux pchains seen acc =
if Map.is_empty pchains then acc else
let seen, new_chains =
Map.fold (fun p chains (seen1, new_chains) ->
let append_to_chains pkg acc =
let chain = CS.map (fun c -> pkg :: c) chains in
Map.update pkg (CS.union chain) CS.empty acc
in
let ds = get deps p in
let dsc = ds %% all_conflicting in
if not (Set.is_empty dsc) then
dsc ++ seen1, Set.fold append_to_chains (dsc -- seen1) new_chains
else
Set.fold (fun d (seen1, new_chains) ->
if Set.mem d seen then seen1, new_chains
else Set.add d seen1, append_to_chains d new_chains)
ds (seen1, new_chains))
pchains (seen, Map.empty)
in
aux new_chains seen @@
Map.union (fun _ _ -> assert false) pchains acc
in
let init_chains =
Set.fold (fun p -> Map.add p (CS.singleton [p])) roots Map.empty
in
aux init_chains roots Map.empty
in
let reasons =
let clen p = try CS.length (Map.find p ct_chains) with Not_found -> 0 in
let version_conflict = function
| Conflict (l, r, _) -> l.Cudf.package = r.Cudf.package
| _ -> false
in
let cmp a b = match a, b with
| Conflict (l1, r1, _), Conflict (l2, r2, _) ->
let va = version_conflict a and vb = version_conflict b in
if va && not vb then -1 else
if vb && not va then 1 else
(match compare (clen l1 + clen r1) (clen l2 + clen r2) with
| 0 -> (match Package.compare l1 l2 with
| 0 -> Package.compare r1 r2
| n -> n)
| n -> n)
| _, Conflict _ -> 1
| Conflict _, _ -> -1
| Missing (p1, _), Missing (p2, _) ->
(match compare (clen p1) (clen p2) with
| 0 -> Package.compare p1 p2
| n -> n)
| _, Missing _ -> 1
| Missing _, _ -> -1
| Dependency _, Dependency _ -> 0
in
List.sort_uniq cmp reasons
in
let has_invariant p =
let chain_has_invariant cs =
CS.exists (List.exists is_opam_invariant) cs
in
try chain_has_invariant (Map.find p ct_chains)
with Not_found -> false
in
let explanations, _remaining_ct_chains =
List.fold_left (fun (explanations, ct_chains) re ->
let cst ?hl_last ct_chains p =
let chains = Map.find p ct_chains in
Map.filter (fun _ c -> not (CS.precludes chains c)) ct_chains,
cs_to_string ?hl_last chains
in
try
match re with
| Conflict (l, r, _) ->
let ct_chains, csl = cst ct_chains l in
let ct_chains, csr = cst ct_chains r in
let msg1 =
if l.Cudf.package = r.Cudf.package then
Some (Package.name_to_string l)
else
None
in
let msg2 = List.sort_uniq compare [csl; csr] in
let msg3 =
(has_invariant l || has_invariant r) &&
not (List.exists (function `Conflict (_,_,has_invariant) -> has_invariant | _ -> false) explanations)
in
let msg = `Conflict (msg1, msg2, msg3) in
if List.mem msg explanations then raise Not_found else
msg :: explanations, ct_chains
| Missing (p, deps) ->
let ct_chains, csp = cst ~hl_last:false ct_chains p in
let fdeps = formula_of_vpkgl cudfnv2opam packages deps in
let sdeps = OpamFormula.to_string fdeps in
let msg = `Missing (Some csp, sdeps, fdeps) in
if List.mem msg explanations then raise Not_found else
msg :: explanations, ct_chains
| Dependency _ ->
explanations, ct_chains
with Not_found ->
explanations, ct_chains)
([], ct_chains) reasons
in
let same_depexts sdeps fdeps =
List.for_all (function
| `Missing (_, sdeps', fdeps') -> sdeps = sdeps' && fdeps = fdeps'
| _ -> false)
in
match explanations with
| `Missing (_, sdeps, fdeps) :: rest when same_depexts sdeps fdeps rest ->
[`Missing (None, sdeps, fdeps)]
| _ -> explanations
let strings_of_cycles cycles =
List.map arrow_concat cycles
let string_of_conflict ?(start_column=0) (msg1, msg2, msg3) =
let width = OpamStd.Sys.terminal_columns () - start_column - 2 in
OpamStd.Format.reformat ~start_column ~indent:2 msg1 ^
OpamStd.List.concat_map ~left:"\n- " ~nil:"" "\n- "
(fun s -> OpamStd.Format.reformat ~indent:2 ~width s) msg2 ^
OpamStd.List.concat_map ~left:"\n" ~nil:"" "\n"
(fun s -> OpamStd.Format.reformat ~indent:2 ~width s) msg3
let conflict_explanations_raw packages = function
| univ, version_map, Conflict_dep reasons ->
let r = reasons () in
let cudfnv2opam = cudfnv2opam ~cudf_universe:univ ~version_map in
List.rev (extract_explanations packages cudfnv2opam r),
[]
| _univ, _version_map, Conflict_cycle cycles ->
[], cycles
let string_of_explanation unav_reasons = function
| `Conflict (kind, packages, has_invariant) ->
let msg1 =
let format_package_name p =
Printf.sprintf "No agreement on the version of %s:"
(OpamConsole.colorise `bold p)
in
OpamStd.Option.map_default format_package_name
"Incompatible packages:" kind
and msg3 =
if has_invariant then
["You can temporarily relax the switch invariant with \
`--update-invariant'"]
else
[]
in
(msg1, packages, msg3)
| `Missing (csp, sdeps, fdeps) ->
let sdeps = OpamConsole.colorise' [`red;`bold] sdeps in
let msg1 = "Missing dependency:"
and msg2 =
OpamStd.Option.map_default (fun csp -> arrow_concat [csp; sdeps]) sdeps csp
and msg3 = OpamFormula.fold_right (fun a x -> unav_reasons x::a) [] fdeps
in
(msg1, [msg2], msg3)
let conflict_explanations packages unav_reasons = function
| univ, version_map, Conflict_dep reasons ->
let r = reasons () in
let cudfnv2opam = cudfnv2opam ~cudf_universe:univ ~version_map in
let explanations = extract_explanations packages cudfnv2opam r in
List.rev_map (string_of_explanation unav_reasons) explanations, []
| _univ, _version_map, Conflict_cycle cycles ->
[], strings_of_cycles cycles
let string_of_explanations unav_reasons (cflts, cycles) =
let cflts = List.map (string_of_explanation unav_reasons) cflts in
let cycles = strings_of_cycles cycles in
let b = Buffer.create 1024 in
let pr_items b l =
Buffer.add_string b
(OpamStd.Format.itemize (fun s -> s) l)
in
if cycles <> [] then
Printf.bprintf b
"The actions to process have cyclic dependencies:\n%a"
pr_items cycles;
if cflts <> [] then
Buffer.add_string b
(OpamStd.Format.itemize ~bullet:(OpamConsole.colorise `red " * ")
(string_of_conflict ~start_column:4) cflts);
if cflts = [] && cycles = [] then
Printf.bprintf b
"Sorry, no solution found: \
there seems to be a problem with your request.\n";
Buffer.add_string b "\n";
Buffer.contents b
let string_of_conflicts packages unav_reasons conflict =
string_of_explanations unav_reasons
(conflict_explanations_raw packages conflict)
let check flag p =
try Cudf.lookup_typed_package_property p flag = `Bool true
with Not_found -> false
let need_reinstall = check s_reinstall
let default_preamble =
let l = [
(s_source, `String None);
(s_source_number, `String None);
(s_reinstall, `Bool (Some false));
(s_installed_root, `Bool (Some false));
(s_pinned, `Bool (Some false));
(s_version_lag, `Nat (Some 0));
] in
Common.CudfAdd.add_properties Cudf.default_preamble l
let remove universe name constr =
let filter p =
p.Cudf.package <> name
|| not (Cudf.version_matches p.Cudf.version constr) in
let packages = Cudf.get_packages ~filter universe in
Cudf.load_universe packages
let uninstall_all universe =
let packages = Cudf.get_packages universe in
let packages = List.rev_map (fun p -> { p with Cudf.installed = false }) packages in
Cudf.load_universe packages
let install universe package =
let p = Cudf.lookup_package universe (package.Cudf.package, package.Cudf.version) in
let p = { p with Cudf.installed = true } in
let packages =
let filter p =
p.Cudf.package <> package.Cudf.package
|| p.Cudf.version <> package.Cudf.version in
Cudf.get_packages ~filter universe in
Cudf.load_universe (p :: packages)
let remove_all_uninstalled_versions_but universe name constr =
let filter p =
p.Cudf.installed
|| p.Cudf.package <> name
|| Cudf.version_matches p.Cudf.version constr in
let packages = Cudf.get_packages ~filter universe in
Cudf.load_universe packages
let to_cudf univ req = (
Common.CudfAdd.add_properties default_preamble
(List.map (fun s -> s, `Int (Some 0)) req.extra_attributes),
univ,
{ Cudf.request_id = "opam";
install = req.wish_install;
remove = req.wish_remove;
upgrade = req.wish_upgrade;
req_extra = [] }
)
let string_of_request r =
Printf.sprintf "install:%s remove:%s upgrade:%s"
(string_of_vpkgs r.wish_install)
(string_of_vpkgs r.wish_remove)
(string_of_vpkgs r.wish_upgrade)
let solver_calls = ref 0
let dump_universe oc univ =
Cudf_printer.pp_cudf oc
(default_preamble, univ, Cudf.default_request)
let dump_cudf_request ~version_map (_, univ,_ as cudf) criteria =
function
| None -> None
| Some f ->
ignore ( version_map: int OpamPackage.Map.t );
incr solver_calls;
let filename = Printf.sprintf "%s-%d.cudf" f !solver_calls in
let oc = open_out filename in
let module Solver = (val OpamSolverConfig.(Lazy.force !r.solver)) in
Printf.fprintf oc "# Solver: %s\n"
(OpamCudfSolver.get_name (module Solver));
Printf.fprintf oc "# Criteria: %s\n" criteria;
Cudf_printer.pp_cudf oc cudf;
OpamPackage.Map.iter (fun (pkg:OpamPackage.t) (vnum: int) ->
let name = OpamPackage.name_to_string pkg in
let version = OpamPackage.version_to_string pkg in
Printf.fprintf oc "#v2v:%s:%d=%s\n" name vnum version;
) version_map;
close_out oc;
Graph.output (Graph.of_universe univ) f;
Some filename
let dump_cudf_error ~version_map univ req =
let cudf_file = match OpamSolverConfig.(!r.cudf_file) with
| Some f -> f
| None ->
let (/) = Filename.concat in
OpamCoreConfig.(!r.log_dir) /
("solver-error-"^string_of_int (OpamStubs.getpid())) in
match
dump_cudf_request (to_cudf univ req) ~version_map
(OpamSolverConfig.criteria req.criteria)
(Some cudf_file)
with
| Some f -> f
| None -> assert false
let preprocess_cudf_request (props, univ, creq) criteria =
let chrono = OpamConsole.timer () in
let univ0 = univ in
let do_trimming =
match OpamSolverConfig.(!r.cudf_trim) with
| Some "simple" -> Some false
| b ->
match OpamStd.Option.Op.(b >>= OpamStd.Config.bool_of_string) with
| Some false -> None
| Some true -> Some true
| None ->
let neg_crit_re =
Re.(seq [char '-';
rep1 (diff any (set ",[("));
opt (seq [set "[("; rep1 (diff any (set ")]")); set ")]"])])
in
let all_neg_re =
Re.(whole_string (seq [rep (seq [neg_crit_re; char ',']);
neg_crit_re]))
in
Some (Re.execp (Re.compile all_neg_re) criteria)
in
let univ =
let open Set.Op in
let vpkg2set vp = Set.of_list (Common.CudfAdd.resolve_deps univ vp) in
let to_install =
vpkg2set creq.Cudf.install
++ Set.of_list (Cudf.lookup_packages univ opam_invariant_package_name)
in
let to_install_formula =
List.map (fun x -> [x]) @@
(opam_invariant_package_name, None) ::
creq.Cudf.install @ creq.Cudf.upgrade
in
let to_map set =
Set.fold (fun p ->
OpamStd.String.Map.update p.Cudf.package (Set.add p) Set.empty)
set OpamStd.String.Map.empty
in
let packages =
match do_trimming with
| None ->
Set.of_list (Cudf.get_packages univ)
| Some false ->
let strong_deps_cone =
rec_strong_dependency_map univ to_install_formula
in
let filter p =
p.Cudf.installed ||
match OpamStd.String.Map.find_opt p.Cudf.package strong_deps_cone
with
| Some ps -> Set.mem p ps
| None -> true
in
Set.of_list (Cudf.get_packages ~filter univ)
| Some true ->
let strong_deps_cone =
rec_strong_dependency_map univ to_install_formula
in
let interesting_set =
List.fold_left (fun acc p ->
let name = p.Cudf.package in
if OpamStd.String.Map.mem name strong_deps_cone then acc
else acc ++ Set.of_list (Cudf.lookup_packages univ name))
(OpamStd.String.Map.fold (fun _ -> Set.union)
strong_deps_cone Set.empty)
(Cudf.get_packages ~filter:(fun p -> p.Cudf.installed) univ)
in
Set.fixpoint (fun p ->
Set.filter (fun d ->
not (OpamStd.String.Map.mem d.Cudf.package strong_deps_cone))
(dependency_set univ p.Cudf.depends))
interesting_set
in
let direct_conflicts p =
let base_conflicts =
Set.filter (fun q -> q.Cudf.package <> p.Cudf.package)
(vpkg2set p.Cudf.conflicts)
in
List.fold_left (fun acc -> function
| (n, c) :: disj when List.for_all (fun (m, _) -> m = n) disj ->
let coset = function
| Some (op, v) ->
let filter = Some (OpamFormula.neg_relop op, v) in
Set.of_list (Cudf.lookup_packages ~filter univ n)
| None -> Set.empty
in
acc ++
List.fold_left (fun acc (_, c) -> acc %% coset c) (coset c) disj
| _ -> acc)
base_conflicts p.Cudf.depends
in
let cache = Hashtbl.create 513 in
let cache_direct = Hashtbl.create 513 in
let max_dig_depth = OpamSolverConfig.(!r.dig_depth) in
let rec transitive_conflicts seen p =
try Hashtbl.find cache p with Not_found ->
let direct =
try Hashtbl.find cache_direct p with Not_found ->
let conflicts = direct_conflicts p in
Hashtbl.add cache_direct p conflicts;
conflicts
in
if Set.mem p seen || Set.cardinal seen >= max_dig_depth - 1 then direct
else
let seen = Set.add p seen in
let conflicts =
direct ++
List.fold_left (fun acc disj ->
acc ++
Set.map_reduce ~default:Set.empty
(transitive_conflicts seen)
Set.inter
(vpkg2set disj))
Set.empty
p.Cudf.depends
in
Hashtbl.add cache p conflicts;
conflicts
in
let conflicts =
OpamStd.String.Map.fold (fun _ ps acc ->
acc ++
Set.map_reduce ~default:Set.empty
(transitive_conflicts Set.empty)
Set.inter
ps)
(to_map to_install)
Set.empty
in
log "Conflicts: %a (%a) pkgs to remove"
(slog OpamStd.Op.(string_of_int @* Set.cardinal)) conflicts
(slog OpamStd.Op.(string_of_int @* Set.cardinal)) (conflicts %% packages);
Cudf.load_universe (Set.elements (packages -- conflicts))
in
log "Preprocess cudf request (trimming: %s): from %d to %d packages in %.2fs"
(match do_trimming with
None -> "none" | Some false -> "simple" | Some true -> "full")
(Cudf.universe_size univ0)
(Cudf.universe_size univ)
(chrono ());
props, univ, creq
exception Timeout of Algo.Depsolver.solver_result option
let call_external_solver ~version_map univ req =
let cudf_request = to_cudf univ req in
if Cudf.universe_size univ > 0 then
let criteria = OpamSolverConfig.criteria req.criteria in
let chrono = OpamConsole.timer () in
ignore (dump_cudf_request ~version_map cudf_request
criteria OpamSolverConfig.(!r.cudf_file));
let check_request_using ~call_solver ~criteria ~explain req =
let timed_out = ref false in
let call_solver args =
try call_solver args with
| OpamCudfSolver.Timeout (Some s) -> timed_out := true; s
| OpamCudfSolver.Timeout None -> raise (Timeout None)
in
let r =
Algo.Depsolver.check_request_using ~call_solver ~criteria ~explain req
in
if !timed_out then raise (Timeout (Some r)) else r
in
try
let cudf_request =
if not OpamSolverConfig.(!r.preprocess) then cudf_request
else preprocess_cudf_request cudf_request criteria
in
let r =
check_request_using
~call_solver:(OpamSolverConfig.call_solver ~criteria)
~criteria ~explain:true cudf_request
in
log "Solver call done in %.3fs" (chrono ());
r
with
| Timeout (Some sol) ->
log "Solver call TIMED OUT with solution after %.3fs" (chrono ());
OpamConsole.warning
"Resolution of the installation set timed out, so the following \
solution might not be optimal.\n\
You may want to make your request more precise, increase the value \
of OPAMSOLVERTIMEOUT (currently %.1fs), or try a different solver."
OpamSolverConfig.(OpamStd.Option.default 0. !r.solver_timeout);
sol
| Timeout None ->
let msg =
Printf.sprintf
"Sorry, resolution of the request timed out.\n\
Try to specify a more precise request, use a different solver, or \
increase the allowed time by setting OPAMSOLVERTIMEOUT to a bigger \
value (currently, it is set to %.1f seconds)."
OpamSolverConfig.(OpamStd.Option.default 0. !r.solver_timeout)
in
raise (Solver_failure msg)
| Failure msg ->
let msg =
Printf.sprintf
"Solver failure: %s\nThis may be due to bad settings (solver or \
solver criteria) or a broken solver solver installation. Check \
$OPAMROOT/config, and the --solver and --criteria options."
msg
in
raise (Solver_failure msg)
| e ->
OpamStd.Exn.fatal e;
let msg =
Printf.sprintf "Solver failed: %s" (Printexc.to_string e)
in
raise (Solver_failure msg)
else
Algo.Depsolver.Sat(None,Cudf.load_universe [])
let check_request ?(explain=true) ~version_map univ req =
match Algo.Depsolver.check_request ~explain (to_cudf univ req) with
| Algo.Depsolver.Unsat
(Some ({Algo.Diagnostic.result = Algo.Diagnostic.Failure _; _} as r)) ->
make_conflicts ~version_map univ r
| Algo.Depsolver.Sat (_,u) ->
Success (remove u dose_dummy_request None)
| Algo.Depsolver.Error msg ->
let f = dump_cudf_error ~version_map univ req in
let msg =
Printf.sprintf "Internal solver failed with %s Request saved to %S"
msg f
in
raise (Solver_failure msg)
| Algo.Depsolver.Unsat _ ->
conflict_empty ~version_map univ
let get_final_universe ~version_map univ req =
let fail msg =
let f = dump_cudf_error ~version_map univ req in
let msg =
Printf.sprintf "External solver failed with %s Request saved to %S"
msg f
in
raise (Solver_failure msg) in
match call_external_solver ~version_map univ req with
| Algo.Depsolver.Sat (_,u) -> Success (remove u dose_dummy_request None)
| Algo.Depsolver.Error "(CRASH) Solution file is empty" ->
Success (Cudf.load_universe [])
| Algo.Depsolver.Error str -> fail str
| Algo.Depsolver.Unsat r ->
let msg =
Printf.sprintf
"The solver (%s) pretends there is no solution while that's apparently \
false.\n\
This is likely an issue with the solver interface, please try a \
different solver and report if you were using a supported one."
(let module Solver = (val OpamSolverConfig.(Lazy.force !r.solver)) in
Solver.name)
in
match r with
| Some ({Algo.Diagnostic.result = Algo.Diagnostic.Failure _; _} as r) ->
OpamConsole.error "%s" msg;
make_conflicts ~version_map univ r
| Some {Algo.Diagnostic.result = Algo.Diagnostic.Success _; _}
| None ->
raise (Solver_failure msg)
let diff univ sol =
let before =
Set.of_list
(Cudf.get_packages ~filter:(fun p -> p.Cudf.installed) univ)
in
let after =
Set.of_list
(Cudf.get_packages ~filter:(fun p -> p.Cudf.installed) sol)
in
let open Set.Op in
let reinstall = Set.filter need_reinstall after in
let install = after -- before ++ reinstall in
let remove = before -- after ++ reinstall in
install, remove
let actions_of_diff (install, remove) =
let actions = [] in
let actions = Set.fold (fun p acc -> `Install p :: acc) install actions in
let actions = Set.fold (fun p acc -> `Remove p :: acc) remove actions in
actions
let resolve ~extern ~version_map universe request =
log "resolve request=%a" (slog string_of_request) request;
let resp =
match check_request ~version_map universe request with
| Success _ when extern -> get_final_universe ~version_map universe request
| resp -> resp
in
let cleanup univ =
Cudf.remove_package univ opam_invariant_package
in
let () = match resp with
| Success univ -> cleanup univ
| Conflicts (univ, _, _) -> cleanup univ
in
resp
let to_actions f universe result =
let aux u1 u2 =
let diff = diff (f u1) u2 in
actions_of_diff diff
in
map_success (aux universe) result
let create_graph filter universe =
let pkgs = Cudf.get_packages ~filter universe in
let u = Cudf.load_universe pkgs in
Graph.of_universe u
let find_cycles g =
let open ActionGraph in
let roots =
fold_vertex (fun v acc -> if in_degree g v = 0 then v::acc else acc) g [] in
let roots =
if roots = [] then fold_vertex (fun v acc -> v::acc) g []
else roots in
let rec prefix_find acc v = function
| x::_ when x = v -> Some (x::acc)
| x::r -> prefix_find (x::acc) v r
| [] -> None in
let seen = Hashtbl.create 17 in
let rec follow v path =
match prefix_find [] v path with
| Some cycle ->
Hashtbl.add seen v ();
[cycle@[v]]
| None ->
if Hashtbl.mem seen v then [] else
let path = v::path in
Hashtbl.add seen v ();
List.fold_left (fun acc s -> follow s path @ acc) []
(succ g v) in
List.fold_left (fun cycles root ->
follow root [] @ cycles
) [] roots
let compute_root_causes g requested reinstall =
let module StringSet = OpamStd.String.Set in
let requested_pkgnames =
OpamPackage.Name.Set.fold (fun n s ->
StringSet.add (Common.CudfAdd.encode (OpamPackage.Name.to_string n)) s)
requested StringSet.empty in
let reinstall_pkgnames =
OpamPackage.Set.fold (fun nv s ->
StringSet.add (Common.CudfAdd.encode (OpamPackage.name_to_string nv)) s)
reinstall StringSet.empty in
let actions =
ActionGraph.fold_vertex (fun a acc -> Map.add (action_contents a) a acc)
g Map.empty in
let requested_actions =
Map.filter (fun pkg _ ->
StringSet.mem pkg.Cudf.package requested_pkgnames)
actions in
let merge_causes (c1,depth1) (c2,depth2) =
if c2 = Unknown || depth1 < depth2 then c1, depth1 else
if c1 = Unknown || depth2 < depth1 then c2, depth2 else
let (@) =
List.fold_left (fun l a -> if List.mem a l then l else a::l)
in
match c1, c2 with
| Required_by a, Required_by b -> Required_by (a @ b), depth1
| Use a, Use b -> Use (a @ b), depth1
| Conflicts_with a, Conflicts_with b -> Conflicts_with (a @ b), depth1
| Requested, a | a, Requested
| Unknown, a | a, Unknown
| Upstream_changes , a | a, Upstream_changes -> a, depth1
| _, c -> c, depth1
in
let direct_cause consequence order cause =
match consequence, order, cause with
| (`Install _ | `Change _), `Before, (`Install p | `Change (_,_,p)) ->
Required_by [p]
| `Change _, `After, (`Install p | `Change (_,_,p)) ->
Use [p]
| `Reinstall _, `After, a ->
Use [action_contents a]
| (`Remove _ | `Change _ ), `Before, `Remove p ->
Use [p]
| `Remove _, `Before, (`Install p | `Change (_,_,p) | `Reinstall p) ->
Conflicts_with [p]
| (`Install _ | `Change _), `Before, `Reinstall p ->
Required_by [p]
| `Change _, _, _ ->
Upstream_changes
| (`Install _ | `Remove _), `After, _ ->
Unknown
| (`Install _ | `Reinstall _), `Before, _ ->
Unknown
| `Build _, _, _ | _, _, `Build _ -> assert false
| `Fetch _, _, _ | _, _, `Fetch _ -> assert false
in
let get_causes acc roots =
let rec aux seen depth pkgname causes =
if depth > 100 then
(OpamConsole.error
"Internal error computing action causes: sorry, please report.";
causes)
else
let action = Map.find pkgname actions in
let seen = Set.add pkgname seen in
let propagate causes actions direction =
List.fold_left (fun causes act ->
let p = action_contents act in
if Set.mem p seen then causes else
let cause = direct_cause act direction action in
if cause = Unknown then causes else
try
Map.add p (merge_causes (cause,depth) (Map.find p causes)) causes
with Not_found ->
aux seen (depth + 1) p (Map.add p (cause,depth) causes)
) causes actions in
let causes = propagate causes (ActionGraph.pred g action) `Before in
let causes = propagate causes (ActionGraph.succ g action) `After in
causes
in
let start = Map.fold (fun k _ acc -> Set.add k acc) roots Set.empty in
let acc = Map.union (fun a _ -> a) acc roots in
Set.fold (aux start 1) start acc
in
let make_roots causes base_cause f =
ActionGraph.fold_vertex (fun act acc ->
if Map.mem (action_contents act) causes then acc else
if f act then Map.add (action_contents act) (base_cause,0) acc else
acc)
g Map.empty in
let causes = Map.empty in
let causes =
let roots =
if Map.is_empty requested_actions then
make_roots causes Requested (function
| `Change (`Up,_,_) -> true
| _ -> false)
else (Map.map (fun _ -> Requested, 0) requested_actions) in
get_causes causes roots in
let causes =
let roots = make_roots causes Unknown (function
| `Change _ as act ->
List.for_all
(function `Change _ -> false | _ -> true)
(ActionGraph.pred g act)
| _ -> false) in
get_causes causes roots in
let causes =
let roots =
make_roots causes Upstream_changes (function
| `Reinstall p ->
StringSet.mem p.Cudf.package reinstall_pkgnames
| _ -> false)
in
get_causes causes roots in
Map.map fst causes
let atomic_actions ~simple_universe ~complete_universe root_actions =
log ~level:2 "graph_of_actions root_actions=%a"
(slog string_of_actions) root_actions;
let to_remove, to_install =
List.fold_left (fun (rm,inst) a -> match a with
| `Change (_,p1,p2) ->
Set.add p1 rm, Set.add p2 inst
| `Install p -> rm, Set.add p inst
| `Reinstall p -> Set.add p rm, Set.add p inst
| `Remove p -> Set.add p rm, inst)
(Set.empty, Set.empty) root_actions in
let to_remove, to_install =
let packages = Set.union to_remove to_install in
let package_graph =
let filter p = p.Cudf.installed || Set.mem p packages in
Graph.mirror (create_graph filter simple_universe)
in
Graph.Topo.fold (fun p (rm,inst) ->
let actionned p = Set.mem p rm || Set.mem p inst in
if not (actionned p) &&
List.exists actionned (Graph.pred package_graph p)
then Set.add p rm, Set.add p inst
else rm, inst)
package_graph (to_remove, to_install)
in
let pkggraph set = create_graph (fun p -> Set.mem p set) complete_universe in
let g = ActionGraph.create () in
Set.iter (fun p -> ActionGraph.add_vertex g (`Remove p)) to_remove;
Set.iter (fun p -> ActionGraph.add_vertex g (`Install (p))) to_install;
Set.iter
(fun p1 ->
try
let p2 =
Set.find (fun p2 -> p1.Cudf.package = p2.Cudf.package) to_install
in
ActionGraph.add_edge g (`Remove p1) (`Install (p2))
with Not_found -> ())
to_remove;
Graph.iter_edges (fun p1 p2 ->
ActionGraph.add_edge g (`Remove p1) (`Remove p2)
) (pkggraph to_remove);
Graph.iter_edges (fun p1 p2 ->
if Set.mem p1 to_install then
let cause =
if Set.mem p2 to_install then `Install ( p2) else `Remove p2
in
ActionGraph.add_edge g cause (`Install ( p1))
) (pkggraph (Set.union to_install to_remove));
let conflicts_graph =
let filter p = Set.mem p to_remove || Set.mem p to_install in
Algo.Defaultgraphs.PackageGraph.conflict_graph
(Cudf.load_universe (Cudf.get_packages ~filter complete_universe))
in
Algo.Defaultgraphs.PackageGraph.UG.iter_edges (fun p1 p2 ->
if Set.mem p1 to_remove && Set.mem p2 to_install then
ActionGraph.add_edge g (`Remove p1) (`Install ( p2))
else if Set.mem p2 to_remove && Set.mem p1 to_install then
ActionGraph.add_edge g (`Remove p2) (`Install ( p1)))
conflicts_graph;
match find_cycles g with
| [] -> g
| cycles -> raise (Cyclic_actions cycles)
let packages u = Cudf.get_packages u