forked from ocaml/opam
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathopamFileTools.ml
1504 lines (1464 loc) · 53.9 KB
/
opamFileTools.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-2020 OCamlPro *)
(* Copyright 2012 INRIA *)
(* *)
(* All rights reserved. This file is distributed under the terms of the *)
(* GNU Lesser General Public License version 2.1, with the special *)
(* exception on linking described in the file LICENSE. *)
(* *)
(**************************************************************************)
open OpamParserTypes.FullPos
open OpamTypes
open OpamTypesBase
let log ?level fmt = OpamConsole.log "opam-file" ?level fmt
let slog = OpamConsole.slog
open OpamFile.OPAM
let is_valid_license_id s =
match Spdx_licenses.parse s with
| Ok _ -> true
| Error _ -> false
(** manipulation utilities *)
let names_of_formula flag f =
OpamPackageVar.filter_depends_formula
~build:true ~post:true ~dev:true ~test:flag ~doc:flag ~dev_setup:flag
~default:false ~env:OpamStd.Option.none f
|> OpamFormula.atoms
|> List.map fst
|> OpamPackage.Name.Set.of_list
let all_commands t =
t.build @ t.install @ t.remove @ t.run_test @ t.deprecated_build_doc
let all_urls t =
let urlf_urls uf = OpamFile.URL.url uf :: OpamFile.URL.mirrors uf in
(match t.url with Some uf -> urlf_urls uf | None -> []) @
(match t.dev_repo with Some u -> [u] | None -> []) @
List.fold_left (fun acc (_, uf) -> urlf_urls uf @ acc) [] t.extra_sources @
List.map snd t.pin_depends
let filters_of_formula f =
OpamFormula.fold_left (fun acc (_, f) ->
OpamFormula.fold_left (fun acc -> function
| Constraint (_,f) -> f :: acc
| Filter f -> f :: acc)
acc f)
[] f
(* Doesn't include filters in commands *)
let all_filters ?(exclude_post=false) t =
OpamStd.List.filter_map snd t.patches @
OpamStd.List.filter_map snd t.messages @
(if exclude_post then [] else OpamStd.List.filter_map snd t.post_messages) @
List.map snd t.depexts @
OpamStd.List.filter_map snd t.libraries @
OpamStd.List.filter_map snd t.syntax @
[t.available] @
filters_of_formula
(OpamFormula.ands
(t.depends ::
t.depopts ::
t.conflicts ::
List.map (fun (_,f,_) -> f) t.features))
let map_all_filters f t =
let mapsnd x =
List.map (fun (x, ft) -> x, f ft) x
in
let mapsndopt x =
List.map (function
| (x, Some ft) -> x, Some (f ft)
| nf -> nf)
x
in
let map_commands =
List.map
(fun (args, filter) ->
List.map (function
| s, Some ft -> s, Some (f ft)
| nf -> nf)
args,
OpamStd.Option.map f filter)
in
let map_filtered_formula =
OpamFormula.map (fun (name, fc) ->
let fc =
OpamFormula.map (function
| Filter flt -> Atom (Filter (f flt))
| Constraint (relop, flt) -> Atom (Constraint (relop, (f flt))))
fc
in
Atom (name, fc))
in
let map_features =
List.map (fun (var, fformula, doc) ->
var, map_filtered_formula fformula, doc)
in
t |>
with_patches (mapsndopt t.patches) |>
with_messages (mapsndopt t.messages) |>
with_post_messages (mapsndopt t.post_messages) |>
with_depexts (mapsnd t.depexts) |>
with_libraries (mapsndopt t.libraries) |>
with_syntax (mapsndopt t.syntax) |>
with_available (f t.available) |>
with_depends (map_filtered_formula t.depends) |>
with_depopts (map_filtered_formula t.depopts) |>
with_conflicts (map_filtered_formula t.conflicts) |>
with_features (map_features t.features) |>
with_build (map_commands t.build) |>
with_run_test (map_commands t.run_test) |>
with_install (map_commands t.install) |>
with_remove (map_commands t.remove) |>
with_deprecated_build_test (map_commands t.deprecated_build_test) |>
with_deprecated_build_doc (map_commands t.deprecated_build_doc)
(* unguarded_commands_variables is an alternative implementation of
OpamFilter.commands_variables which excludes package variables which are
guarded by an unambiguous {package:installed} filter. That is, at each level,
if assuming !package:installed reduces the filter to false, then the uses of
package:variable are not returned. This allows expressions like:
["--with-foo=%{foo:share}%" {foo:installed}] or even
["--with-foo"] {foo:installed & foo:bar != "baz"} not to trigger warning 41
if the package is not explicitly depended on. *)
let unguarded_commands_variables commands =
let is_installed_variable filter guarded_packages v =
match OpamVariable.Full.package v with
| None -> guarded_packages
| (Some name) as package ->
let is_installed var =
String.equal "installed"
(OpamVariable.to_string (OpamVariable.Full.variable var))
in
let env var =
if Option.equal OpamPackage.Name.equal
(OpamVariable.Full.package var) package &&
is_installed var then
Some (B false)
else
None
in
if is_installed v &&
OpamFilter.partial_eval env filter = FBool false then
OpamPackage.Name.Set.add name guarded_packages
else
guarded_packages
in
let filter_guarded variables guarded_packages =
let is_unguarded v =
match OpamVariable.Full.package v with
| Some package ->
not (OpamPackage.Name.Set.mem package guarded_packages)
| None -> true
in
List.filter is_unguarded variables
in
let unguarded_packages_from_filter guarded_packages = function
| None -> guarded_packages, []
| Some f ->
let filter_variables = OpamFilter.variables f in
let guarded_packages =
List.fold_left (is_installed_variable f)
guarded_packages filter_variables
in
guarded_packages, filter_guarded filter_variables guarded_packages
in
let unguarded_argument_variables guarded_packages (argument, filter) =
let guarded_packages, filter_variables =
unguarded_packages_from_filter guarded_packages filter
in
let variables_from_arguments =
filter_guarded (OpamFilter.simple_arg_variables argument) guarded_packages
in
guarded_packages, variables_from_arguments @ filter_variables
in
let unguarded_command_variables guarded_packages (command, filter) =
let filter_guarded_packages, filter_variables =
unguarded_packages_from_filter OpamPackage.Name.Set.empty filter
in
let add_argument (guarded_packages, acc) argument =
let guarded_packages, unguarded_variables =
unguarded_argument_variables guarded_packages argument
in
guarded_packages, unguarded_variables @ acc
in
let command_guarded_packages, unguarded_variables =
List.fold_left add_argument (filter_guarded_packages, filter_variables)
command
in
OpamPackage.Name.Set.union guarded_packages command_guarded_packages,
unguarded_variables
in
let f (guarded_packages, acc) c =
let guarded_packages, unguarded_variables =
unguarded_command_variables guarded_packages c
in
guarded_packages, (unguarded_variables @ acc)
in
List.fold_left f (OpamPackage.Name.Set.empty, []) commands
(* Returns all variables from all commands (or on given [command]) and all filters *)
let all_variables ?exclude_post ?command t =
let commands =
match command with
| Some cmd -> cmd
| None -> all_commands t
in
OpamFilter.commands_variables commands @
List.fold_left (fun acc f -> OpamFilter.variables f @ acc)
[] (all_filters ?exclude_post t)
(* As all_variables, but any commands or arguments which are fully guarded by
package:installed are excluded; used for Warning 41 so that
["%{foo:share}%" {foo:installed}] doesn't trigger a warning on foo *)
let all_unguarded_variables ?exclude_post t =
let guarded_packages, unguarded_commands_variables =
unguarded_commands_variables (all_commands t)
in
guarded_packages,
unguarded_commands_variables @
List.fold_left (fun acc f -> OpamFilter.variables f @ acc)
[] (all_filters ?exclude_post t)
let map_all_variables f t =
let map_fld (x, flt) = x, OpamFilter.map_variables f flt in
let map_optfld = function
| x, Some flt -> x, Some (OpamFilter.map_variables f flt)
| _, None as optfld -> optfld
in
let map_commands =
let map_args =
List.map
(fun (s, filter) ->
(match s with
| CString s -> CString (OpamFilter.map_variables_in_string f s)
| CIdent id ->
let id =
try filter_ident_of_string id |>
OpamFilter.map_variables_in_fident f |>
string_of_filter_ident
with Failure _ -> id
in
CIdent id),
OpamStd.Option.Op.(filter >>| OpamFilter.map_variables f))
in
List.map
(fun (args, filter) ->
map_args args,
OpamStd.Option.Op.(filter >>| OpamFilter.map_variables f))
in
let map_filtered_formula =
OpamFormula.map (fun (name, fc) ->
let fc =
OpamFormula.map (function
| Filter flt ->
Atom (Filter (OpamFilter.map_variables f flt))
| Constraint (relop, flt) ->
Atom (Constraint (relop, (OpamFilter.map_variables f flt))))
fc
in
Atom (name, fc)
)
in
let map_features =
List.map (fun (var, fformula, doc) ->
var, map_filtered_formula fformula, doc)
in
t |>
with_patches (List.map map_optfld t.patches) |>
with_messages (List.map map_optfld t.messages) |>
with_post_messages (List.map map_optfld t.post_messages) |>
with_depexts (List.map map_fld t.depexts) |>
with_libraries (List.map map_optfld t.libraries) |>
with_syntax (List.map map_optfld t.syntax) |>
with_build (map_commands t.build) |>
with_run_test (map_commands t.run_test) |>
with_install (map_commands t.install) |>
with_remove (map_commands t.remove) |>
with_depends (map_filtered_formula t.depends) |>
with_depopts (map_filtered_formula t.depopts) |>
with_conflicts (map_filtered_formula t.conflicts) |>
with_available (OpamFilter.map_variables f t.available) |>
with_features (map_features t.features) |>
with_deprecated_build_test (map_commands t.deprecated_build_test) |>
with_deprecated_build_doc (map_commands t.deprecated_build_doc)
let all_expanded_strings t =
List.map fst t.messages @
List.map fst t.post_messages @
List.fold_left (fun acc (args, _) ->
List.fold_left
(fun acc -> function CString s, _ -> s :: acc | _ -> acc)
acc args)
[] (all_commands t) @
List.fold_left
(OpamFilter.fold_down_left
(fun acc -> function FString s -> s :: acc | _ -> acc))
[] (all_filters t)
let all_depends t =
OpamPackage.Name.Set.union
(names_of_formula true t.depends)
(names_of_formula true t.depopts)
(* Templating & linting *)
let template nv =
let maintainer =
let from_git = try
match
OpamSystem.read_command_output
["git"; "config"; "--get"; "user.name"],
OpamSystem.read_command_output
["git"; "config"; "--get"; "user.email"]
with
| [name], [email] ->
Some [Printf.sprintf "%s <%s>" name email]
| _ -> raise Not_found
with e -> OpamStd.Exn.fatal e; None
in
match from_git with
| Some u -> u
| None ->
let email =
try Some (Sys.getenv "EMAIL") with Not_found -> None in
try
let open Unix in
let pw = getpwuid (getuid ()) in
let email = match email with
| Some e -> e
| None -> pw.pw_name^"@"^gethostname () in
match OpamStd.String.split pw.pw_gecos ',' with
| name::_ -> [Printf.sprintf "%s <%s>" name email]
| _ -> [email]
with Not_found -> match email with
| Some e -> [e]
| None -> []
in
create nv
|> with_name_opt None
|> with_maintainer maintainer
|> with_build
[[CString "./configure", None;
CString "--prefix=%{prefix}%", None], None;
[CIdent "make", None], None]
|> with_install
[[CIdent "make", None; CString "install", None], None]
|> with_depends
(Atom (OpamPackage.Name.of_string "specify-dependencies-here",
(Atom (Constraint (`Geq, FString "optional-version")))))
|> with_author maintainer
|> with_homepage [""]
|> with_license [""]
|> with_dev_repo (OpamUrl.of_string "git+https://")
|> with_bug_reports [""]
|> with_synopsis ""
let t_lint ?check_extra_files ?(check_upstream=false) ?(all=false) t =
let format_errors =
List.map (fun (field, (pos, msg)) ->
3, `Error,
Printf.sprintf "File format error in '%s'%s: %s"
field
(match pos with
| Some {start=li,col; _} when li >= 0 && col >= 0 ->
Printf.sprintf " at line %d, column %d" li col
| _ -> "")
msg)
(OpamFile.OPAM.format_errors t)
in
let cond num level msg ?detail cd =
if all then Some (num, level, msg)
else if cd then
let msg = match detail with
| None | Some [] -> msg
| Some d ->
Printf.sprintf "%s: \"%s\"" msg (String.concat "\", \"" d)
in
Some (num, level, msg)
else None
in
let all_commands = all_commands t in
let all_expanded_strings = all_expanded_strings t in
let all_depends = all_depends t in
(* Upstream is checked only if it is an archive and non vcs backend *)
let url_vcs =
let open OpamStd.Option.Op in
t.url >>| OpamFile.URL.url >>| (fun u ->
match u.OpamUrl.backend with
| #OpamUrl.version_control -> true
| _ -> false)
in
let url_archive =
let open OpamStd.Option.Op in
t.url >>| OpamFile.URL.url >>| (fun u ->
OpamSystem.is_archive_from_string u.OpamUrl.path)
in
let is_url_archive =
not (OpamFile.OPAM.has_flag Pkgflag_Conf t)
&& url_vcs = Some false
&& url_archive = Some true
in
let check_upstream = check_upstream && is_url_archive in
let check_double compare to_str lst =
let double =
List.sort compare lst
|> List.fold_left (fun (last, dbl) elem ->
match last with
| Some last ->
if compare last elem = 0 then
Some elem, OpamStd.String.Map.update (to_str elem) ((+) 1) 1 dbl
else
Some elem, dbl
| None -> Some elem, dbl)
(None, OpamStd.String.Map.empty)
|> snd
in
if OpamStd.String.Map.is_empty double then false, None else
true,
Some (List.map (fun (elem, occ) ->
Printf.sprintf "%s: %d occurence%s"
elem occ (if occ = 1 then "" else "s"))
(OpamStd.String.Map.bindings double))
in
let warnings = [
cond 20 `Warning
"Field 'opam-version' refers to the patch version of opam, it \
should be of the form MAJOR.MINOR"
~detail:[OpamVersion.to_string t.opam_version]
(OpamVersion.nopatch t.opam_version <> t.opam_version);
cond 21 `Error
"Field 'opam-version' doesn't match the current version, \
validation may not be accurate"
~detail:[OpamVersion.to_string t.opam_version]
(OpamVersion.compare t.opam_version OpamFile.OPAM.format_version <> 0);
(*
cond (t.name = None)
"Missing field 'name' or directory in the form 'name.version'";
cond (t.version = None)
"Missing field 'version' or directory in the form 'name.version'";
*)
(let empty_fields =
OpamStd.List.filter_map (function n,[""] -> Some n | _ -> None)
["maintainer", t.maintainer; "homepage", t.homepage;
"author", t.author; "license", t.license; "doc", t.doc;
"tags", t.tags; "bug_reports", t.bug_reports]
in
cond 22 `Error
"Some fields are present but empty; remove or fill them"
~detail:empty_fields
(empty_fields <> []));
cond 23 `Error
"Missing field 'maintainer'"
(t.maintainer = []);
cond 24 `Error
"Field 'maintainer' has the old default value"
(List.mem "contact@ocamlpro.com" t.maintainer &&
not (List.mem "org:ocamlpro" t.tags));
cond 25 `Warning
"Missing field 'authors'"
(t.author = []);
cond 26 `Warning
"No field 'install', but a field 'remove': install instructions \
probably part of 'build'. Use the 'install' field or a .install \
file"
(t.install = [] && t.build <> [] && t.remove <> []);
(*
cond 27 `Warning
"No field 'remove' while a field 'install' is present, uncomplete \
uninstallation suspected"
(t.install <> [] && t.remove = []);
*)
(let unk_flags =
OpamStd.List.filter_map (function
| Pkgflag_Unknown s -> Some s
| _ -> None)
t.flags
in
cond 28 `Error
"Unknown package flags found"
~detail:unk_flags
(unk_flags <> []));
(let filtered_vars =
OpamFilter.variables_of_filtered_formula t.depends @
OpamFilter.variables_of_filtered_formula t.depopts @
OpamFilter.variables_of_filtered_formula t.conflicts
|> List.filter (fun v -> not (OpamVariable.Full.is_global v))
|> List.map OpamVariable.Full.to_string
in
cond 29 `Error
"Package dependencies or conflicts mention package variables"
~detail:filtered_vars
(filtered_vars <> []));
(*
cond 30 `Error
"Field 'depopts' is not a pure disjunction"
(List.exists (function
| OpamFormula.Atom _ -> false
| _ -> true)
(OpamFormula.ors_to_list t.depopts));
*)
(let dup_depends =
OpamPackage.Name.Set.inter
(names_of_formula false t.depends)
(names_of_formula true t.depopts)
in
cond 31 `Error
"Fields 'depends' and 'depopts' refer to the same package names"
~detail:OpamPackage.Name.
(List.map to_string (Set.elements dup_depends))
(not (OpamPackage.Name.Set.is_empty dup_depends)));
cond 32 `Error
"Field 'ocaml-version:' and variable 'ocaml-version' are deprecated, use \
a dependency towards the 'ocaml' package instead for availability, and \
the 'ocaml:version' package variable for scripts"
(t.ocaml_version <> None ||
List.mem (OpamVariable.Full.of_string "ocaml-version")
(all_variables t));
cond 33 `Error
"Field 'os' is deprecated, use 'available' and the 'os' variable \
instead"
(t.os <> Empty);
(let pkg_vars =
List.filter (fun v -> not (OpamVariable.Full.is_global v))
(OpamFilter.variables t.available)
in
cond 34 `Error
"Field 'available:' contains references to package-local variables. \
It should only be determined from global configuration variables"
~detail:(List.map OpamVariable.Full.to_string pkg_vars)
(pkg_vars <> []));
cond 35 `Warning
"Missing field 'homepage'"
(t.homepage = []);
(* cond (t.doc = []) *)
(* "Missing field 'doc'"; *)
cond 36 `Warning
"Missing field 'bug-reports'"
(t.bug_reports = []);
cond 37 `Warning
"Missing field 'dev-repo'"
(t.dev_repo = None && t.url <> None);
(*
cond 38 `Warning
"Package declares 'depexts', but has no 'post-messages' to help \
the user out when they are missing"
(t.depexts <> None && t.post_messages = []);
*)
cond 39 `Error
"Command 'make' called directly, use the built-in variable \
instead"
(List.exists (function
| (CString "make", _)::_, _ -> true
| _ -> false
) all_commands);
(*
cond 40 `Warning
"Field 'features' is still experimental and not yet to be used on \
the official repo"
(t.features <> []);
(let alpha_flags =
OpamStd.List.filter_map (function
| Pkgflag_LightUninstall | Pkgflag_Unknown _ -> None
| f ->
if List.exists (fun tag -> flag_of_tag tag = Some f) t.tags
then None
else Some (string_of_pkg_flag f))
t.flags
in
cond 40 `Warning
"Package uses flags that aren't recognised by earlier versions in \
OPAM 1.2 branch. At the moment, you should use a tag \"flags:foo\" \
instead for compatibility"
~detail:alpha_flags
(alpha_flags <> []));
*)
(let all_mentioned_packages =
OpamPackage.Name.Set.union
(OpamFormula.all_names t.depends)
(OpamFormula.all_names t.depopts)
in
let undep_pkgs =
let guarded_packages, all_unguarded_variables =
all_unguarded_variables ~exclude_post:true t
in
let first_lot =
List.fold_left
(fun acc v ->
match OpamVariable.Full.package v with
| Some n when
t.OpamFile.OPAM.name <> Some n &&
not (OpamPackage.Name.Set.mem n all_depends) &&
OpamVariable.(Full.variable v <> of_string "installed")
->
OpamPackage.Name.Set.add n acc
| _ -> acc)
OpamPackage.Name.Set.empty all_unguarded_variables
in
let second_lot =
OpamPackage.Name.Set.diff guarded_packages all_mentioned_packages
in
OpamPackage.Name.Set.union first_lot second_lot
in
cond 41 `Warning
"Some packages are mentioned in package scripts or features, but \
there is no dependency or depopt toward them"
~detail:OpamPackage.Name.
(List.map to_string (Set.elements undep_pkgs))
(not (OpamPackage.Name.Set.is_empty undep_pkgs)));
cond 42 `Error
"The 'dev-repo:' field doesn't use version control. You should use \
URLs of the form \"git://\", \"git+https://\", \"hg+https://\"..."
(match t.dev_repo with
| None -> false
| Some { OpamUrl.backend = #OpamUrl.version_control; _ } -> false
| Some _ -> true);
cond 43 `Error
"Conjunction used in 'conflicts:' field. Only '|' is allowed"
(OpamVersion.compare t.opam_version (OpamVersion.of_string "1.3") >= 0 &&
let rec ors_only_constraint = function
| Atom _ | Empty -> true
| Or (a, b) -> ors_only_constraint a && ors_only_constraint b
| And (a, Atom (Filter _)) | And (Atom (Filter _), a) | Block a ->
ors_only_constraint a
| And _ -> false
in
let rec check = function
| Atom (_, c) -> ors_only_constraint c
| Empty -> true
| Or (a, b) -> check a && check b
| Block a -> check a
| And _ -> false
in
not (check t.conflicts));
cond 44 `Warning
"The 'plugin' package flag is set but the package name doesn't \
begin with 'opam-'"
(OpamVersion.compare t.opam_version (OpamVersion.of_string "1.3") >= 0 &&
List.mem Pkgflag_Plugin t.flags &&
match t.OpamFile.OPAM.name with
| None -> false
| Some name ->
not (OpamStd.String.starts_with ~prefix:OpamPath.plugin_prefix
(OpamPackage.Name.to_string name)));
(let unclosed =
List.fold_left (fun acc s ->
List.rev_append (OpamFilter.unclosed_expansions s) acc)
[] all_expanded_strings
in
cond 45 `Error
"Unclosed variable interpolations in strings"
~detail:(List.map snd unclosed)
(unclosed <> []));
cond 46 `Error
"Package is flagged \"conf\" but has source, install or remove \
instructions"
(has_flag Pkgflag_Conf t &&
(t.install <> [] || t.remove <> [] || t.url <> None ||
t.extra_sources <> []));
cond 47 `Warning
"Synopsis should start with a capital and not end with a dot"
(let valid_re =
Re.(compile (seq [bos; diff any (alt [blank; lower]); rep any;
diff any (alt [blank; char '.']); eos]))
in
match t.descr with None -> false | Some d ->
let synopsis = OpamFile.Descr.synopsis d in
synopsis <> "" && not (Re.execp valid_re synopsis));
cond 48 `Warning
"The fields 'build-test:' and 'build-doc:' are deprecated, and should be \
replaced by uses of the 'with-test' and 'with-doc' filter variables in \
the 'build:' and 'install:' fields, and by the newer 'run-test:' \
field"
(t.deprecated_build_test <> [] || t.deprecated_build_doc <> []);
(let suspicious_urls =
List.filter (fun u ->
OpamUrl.parse_opt ~handle_suffix:true (OpamUrl.to_string u) <> Some u)
(all_urls t)
in
cond 49 `Warning
"The following URLs don't use version control but look like version \
control URLs"
~detail:(List.map OpamUrl.to_string suspicious_urls)
(suspicious_urls <> []));
cond 50 `Warning
"The 'post' flag doesn't make sense with build or optional \
dependencies"
(List.mem (OpamVariable.Full.of_string "post")
(List.flatten
(List.map OpamFilter.variables
(filters_of_formula t.depopts))) ||
OpamFormula.fold_left (fun acc (_, f) ->
acc ||
let vars =
OpamFormula.fold_left (fun vars f ->
match f with
| Constraint _ -> vars
| Filter fi -> OpamFilter.variables fi @ vars)
[] f
in
List.mem (OpamVariable.Full.of_string "build") vars &&
List.mem (OpamVariable.Full.of_string "post") vars)
false
t.depends);
cond 51 `Error
"The behaviour for negated dependency flags 'build' or 'post' is \
unspecified"
(OpamFormula.fold_left (fun acc (_, f) ->
acc || OpamFormula.fold_left (fun acc f ->
acc || match f with
| Filter fi ->
OpamFilter.fold_down_left (fun acc fi ->
acc || match fi with
| FNot (FIdent ([], var, None)) ->
(match OpamVariable.to_string var with
| "build" | "post" -> true
| _ -> false)
| _ -> false)
false (OpamFilter.distribute_negations fi)
| _ -> false)
false f)
false
(OpamFormula.ands [t.depends; t.depopts]));
cond 52 `Error
"Package is needlessly flagged \"light-uninstall\", since it has no \
remove instructions"
(has_flag Pkgflag_LightUninstall t && t.remove = []);
(let mismatching_extra_files =
match t.extra_files, check_extra_files with
| None, _ | _, None -> []
| Some fs, Some [] -> List.map fst fs
| Some efiles, Some ffiles ->
OpamStd.List.filter_map (fun (n, _) ->
if OpamStd.List.mem_assoc OpamFilename.Base.equal
n ffiles then
None else Some n)
efiles @
OpamStd.List.filter_map (fun (n, check_f) ->
try
if check_f (OpamStd.List.assoc OpamFilename.Base.equal
n efiles) then
None else Some n
with Not_found -> Some n)
ffiles
in
cond 53 `Error
"Mismatching 'extra-files:' field"
~detail:(List.map OpamFilename.Base.to_string mismatching_extra_files)
(mismatching_extra_files <> []));
(let spaced_depexts =
List.concat (List.map (fun (dl,_) ->
OpamStd.List.filter_map
(fun s ->
let d = OpamSysPkg.to_string s in
if String.contains d ' ' || String.length d = 0 then
Some d
else None)
(OpamSysPkg.Set.elements dl))
t.depexts) in
cond 54 `Warning
"External dependencies should not contain spaces nor empty string"
~detail:spaced_depexts
(spaced_depexts <> []));
(let bad_os_arch_values =
List.fold_left
(OpamFilter.fold_down_left (fun acc -> function
| FOp (FIdent ([],vname,None), _, FString value)
| FOp (FString value, _, FIdent ([],vname,None)) ->
(match OpamVariable.to_string vname with
| "os" ->
let norm = OpamSysPoll.normalise_os value in
if value <> norm then (value, norm)::acc else acc
| "arch" ->
let norm = OpamSysPoll.normalise_arch value in
if value <> norm then (value, norm)::acc else acc
| _ -> acc)
| _ -> acc))
[] (all_filters t)
in
cond 55 `Error
"Non-normalised OS or arch string being tested"
~detail:(List.map
(fun (used,norm) -> Printf.sprintf "%s (use %s instead)"
used norm)
bad_os_arch_values)
(bad_os_arch_values <> []));
(* Retired, since `OPAM_LAST_ENV` allows environment updates to be reliably
reverted. *)
(*
cond 56 `Warning
"It is discouraged for non-compiler packages to use 'setenv:'"
(t.env <> [] && not (has_flag Pkgflag_Compiler t));
*)
cond 57 `Error
"Synopsis must not be empty"
(match t.descr with None -> true | Some d -> String.equal (OpamFile.Descr.synopsis d) "");
(let vars = all_variables ~exclude_post:false ~command:[] t in
let exists svar =
List.exists (fun v -> v = OpamVariable.Full.of_string svar) vars
in
let rem_test = exists "test" in
let rem_doc = exists "doc" in
cond 58 `Warning
(let var, s_, nvar =
match rem_test, rem_doc with
| true, true -> "`test` and `doc`", "s", "s are `with-test` and `with-doc`"
| true, false -> "`test`", "", " is `with-test`"
| false, true -> "`doc`", "", " is `with-doc`"
| _ -> "","",""
in
Printf.sprintf "Found %s variable%s, predefined one%s" var s_ nvar)
(rem_test || rem_doc));
cond 59 `Warning "url doesn't contain a checksum"
(is_url_archive &&
OpamStd.Option.map OpamFile.URL.checksum t.url = Some []);
(let upstream_error =
if not check_upstream then None else
match t.url with
| None -> Some "No url defined"
| Some urlf ->
let open OpamProcess.Job.Op in
let check_checksum f =
match OpamFile.URL.checksum urlf with
| [] -> None
| chks ->
let not_corresponding =
OpamStd.List.filter_map (fun chk ->
match OpamHash.mismatch (OpamFilename.to_string f) chk with
| Some m -> Some (m, chk)
| None -> None)
chks
in
if not_corresponding = [] then None
else
let msg =
let is_singular = function [_] -> true | _ -> false in
Printf.sprintf "The archive doesn't match checksum%s:\n%s."
(if is_singular not_corresponding then "" else "s")
(OpamStd.Format.itemize (fun (good, bad) ->
Printf.sprintf "archive: %s, in opam file: %s"
(OpamHash.to_string good) (OpamHash.to_string bad))
not_corresponding)
in
Some msg
in
let url = OpamFile.URL.url urlf in
OpamProcess.Job.run @@
OpamFilename.with_tmp_dir_job @@ fun dir ->
match url.backend with
| #OpamUrl.version_control -> Done None (* shouldn't happen *)
| `http ->
OpamProcess.Job.catch (function
| Failure msg -> Done (Some msg)
| OpamDownload.Download_fail (s,l) ->
Done (Some (OpamStd.Option.default l s))
| e -> Done (Some (Printexc.to_string e)))
@@ fun () ->
OpamDownload.download ~overwrite:false url dir
@@| check_checksum
| `rsync ->
let filename =
let open OpamStd.Option.Op in
(OpamFile.OPAM.name_opt t
>>| OpamPackage.Name.to_string)
+! "lint-check-upstream"
|> OpamFilename.Base.of_string
|> OpamFilename.create dir
in
OpamLocal.rsync_file url filename
@@| function
| Up_to_date f | Result f -> check_checksum f
| Not_available (_,src) ->
Some ("Source not found: "^src)
in
cond 60 `Error "Upstream check failed"
~detail:(OpamStd.Option.to_list upstream_error)
(upstream_error <> None));
(let with_test =
List.exists ((=) (OpamVariable.Full.of_string "with-test"))
(OpamFilter.commands_variables t.run_test)
in
cond 61 `Warning
"`with-test` variable in `run-test` is out of scope, it will be ignored"
with_test);
(let bad_licenses =
List.filter (fun s -> not (is_valid_license_id s)) t.license
in
cond 62 `Warning
"License doesn't adhere to the SPDX standard, see https://spdx.org/licenses/ "
~detail:bad_licenses
(bad_licenses <> []));
(*
(let subpath =
match OpamStd.String.Map.find_opt "x-subpath" (extensions t) with
| Some {pelem = String _; _} -> true
| _ -> false
in
let opam_restriction =
OpamFilter.fold_down_left (fun acc filter ->
acc ||
match filter with
| FOp (FIdent (_, var, _), op, FString version)
when OpamVariable.to_string var = "opam-version" ->
OpamFormula.simplify_version_formula
(OpamFormula.ands
[ Atom (`Lt, OpamPackage.Version.of_string "2.1");
Atom (op, OpamPackage.Version.of_string version) ])
= None
| _ -> false) false t.available
in
cond 63 `Error
"`subpath` field need `opam-version = 2.1` restriction"
(subpath && not opam_restriction));
(let subpath_string =
match OpamStd.String.Map.find_opt "x-subpath" (extensions t) with
| Some {pelem = String _; _} | None -> false
| _ -> true
in
cond 64 `Warning
"`x-subpath` must be a simple string to be considered as a subpath`"
subpath_string);
*)
(let relative =
let open OpamUrl in
List.filter (fun u ->
(* OpamUrl.local_dir is not used because it checks the existence of
the directory *)
(match u.backend, u.transport with
| (#version_control | `rsync),
("file" | "path" | "local" | "rsync") -> true
| _, _ -> false)
&& (Filename.is_relative u.path
|| OpamFilename.might_escape ~sep:`Unix u.path))
(all_urls t)
in
cond 65 `Error
"URLs must be absolute"
~detail:(List.map (fun u -> u.OpamUrl.path) relative)
(relative <> []));
(let maybe_bool =
(* Regexp from [OpamFilter.string_interp_regexp] *)
let re =
let open Re in
let notclose =
rep @@ alt [
diff notnl @@ set "}";
seq [char '}'; alt [diff notnl @@ set "%"; stop] ]
]
in
compile @@ seq [
bos; alt [
str "true"; str "false"; str "%%";
seq [str "%{"; greedy notclose; opt @@ str "}%"];
]; eos]
in
fun s ->
try let _ = Re.exec re s in true with Not_found -> false
in
let check_strings =
let rec aux acc oped = function
| FString s -> if oped || maybe_bool s then acc else s::acc
| FIdent _ | FBool _ -> acc
| FOp (fl,_,fr) -> (aux acc true fl) @ aux acc true fr
| FAnd (fl, fr) | FOr (fl, fr) ->
(aux acc false fl) @ aux acc false fr
| FNot f | FDefined f | FUndef f -> aux acc false f
in
aux [] false
in
let check_formula =
OpamFormula.fold_left (fun acc (_, form as ff) ->
match
OpamFormula.fold_left (fun acc fc ->
match fc with
| Filter f -> check_strings f @ acc
| Constraint _ -> acc) [] form
with
| [] -> acc
| strs -> (ff, List.rev strs)::acc
)
in
let not_bool_strings =
List.fold_left check_formula []
(t.depends :: t.depopts :: t.conflicts
:: List.map (fun (_,f,_) -> f) t.features)
in
cond 66 `Warning
"String that can't be resolved to bool in filtered package formula"
~detail:(List.map (fun (f, strs) ->
Printf.sprintf "%s in '%s'"
(OpamStd.Format.pretty_list (List.map (Printf.sprintf "%S") strs))
(OpamFilter.string_of_filtered_formula (Atom f)))
not_bool_strings)
(not_bool_strings <> []));