-
Notifications
You must be signed in to change notification settings - Fork 696
/
Check.hs
2711 lines (2408 loc) · 113 KB
/
Check.hs
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
-----------------------------------------------------------------------------
-- |
-- Module : Distribution.PackageDescription.Check
-- Copyright : Lennart Kolmodin 2008
-- License : BSD3
--
-- Maintainer : cabal-devel@haskell.org
-- Portability : portable
--
-- This has code for checking for various problems in packages. There is one
-- set of checks that just looks at a 'PackageDescription' in isolation and
-- another set of checks that also looks at files in the package. Some of the
-- checks are basic sanity checks, others are portability standards that we'd
-- like to encourage. There is a 'PackageCheck' type that distinguishes the
-- different kinds of checks so we can see which ones are appropriate to report
-- in different situations. This code gets used when configuring a package when
-- we consider only basic problems. The higher standard is used when
-- preparing a source tarball and by Hackage when uploading new packages. The
-- reason for this is that we want to hold packages that are expected to be
-- distributed to a higher standard than packages that are only ever expected
-- to be used on the author's own environment.
module Distribution.PackageDescription.Check (
-- * Package Checking
CheckExplanation(..),
PackageCheck(..),
checkPackage,
checkConfiguredPackage,
wrapParseWarning,
-- ** Checking package contents
checkPackageFiles,
checkPackageContent,
CheckPackageContentOps(..),
checkPackageFileNames,
) where
import Distribution.Compat.Prelude
import Prelude ()
import Data.List (group)
import Distribution.CabalSpecVersion
import Distribution.Compat.Lens
import Distribution.Compiler
import Distribution.License
import Distribution.ModuleName (ModuleName)
import Distribution.Package
import Distribution.PackageDescription
import Distribution.PackageDescription.Configuration
import Distribution.Parsec.Warning (PWarning, showPWarning)
import Distribution.Pretty (prettyShow)
import Distribution.Simple.BuildPaths (autogenPathsModuleName)
import Distribution.Simple.BuildToolDepends
import Distribution.Simple.CCompiler
import Distribution.Simple.Glob
import Distribution.Simple.Utils hiding (findPackageDesc, notice)
import Distribution.System
import Distribution.Types.ComponentRequestedSpec
import Distribution.Types.PackageName.Magic
import Distribution.Utils.Generic (isAscii)
import Distribution.Verbosity
import Distribution.Version
import Distribution.Utils.Path
import Language.Haskell.Extension
import System.FilePath
(splitDirectories, splitExtension, splitPath, takeExtension, takeFileName, (<.>), (</>))
import qualified Data.ByteString.Lazy as BS
import qualified Data.Map as Map
import qualified Distribution.Compat.DList as DList
import qualified Distribution.SPDX as SPDX
import qualified System.Directory as System
import qualified System.Directory (getDirectoryContents)
import qualified System.FilePath.Windows as FilePath.Windows (isValid)
import qualified Data.Set as Set
import qualified Distribution.Utils.ShortText as ShortText
import qualified Distribution.Types.BuildInfo.Lens as L
import qualified Distribution.Types.GenericPackageDescription.Lens as L
import qualified Distribution.Types.PackageDescription.Lens as L
-- $setup
-- >>> import Control.Arrow ((&&&))
-- ------------------------------------------------------------
-- * Warning messages
-- ------------------------------------------------------------
-- | Which stanza does `CheckExplanation` refer to?
--
data CEType = CETLibrary | CETExecutable | CETTest | CETBenchmark
deriving (Eq, Ord, Show)
-- | Pretty printing `CEType`.
--
ppCE :: CEType -> String
ppCE CETLibrary = "library"
ppCE CETExecutable = "executable"
ppCE CETTest = "test suite"
ppCE CETBenchmark = "benchmark"
-- | Which field does `CheckExplanation` refer to?
--
data CEField = CEFCategory | CEFMaintainer | CEFSynopsis
| CEFDescription | CEFSynOrDesc
deriving (Eq, Ord, Show)
-- | Pretty printing `CEField`.
--
ppCEField :: CEField -> String
ppCEField CEFCategory = "category"
ppCEField CEFMaintainer = "maintainer"
ppCEField CEFSynopsis = "synopsis"
ppCEField CEFDescription = "description"
ppCEField CEFSynOrDesc = "synopsis' or 'description"
-- | Explanations of 'PackageCheck`'s errors/warnings.
--
data CheckExplanation =
ParseWarning FilePath PWarning
| NoNameField
| NoVersionField
| NoTarget
| UnnamedInternal
| DuplicateSections [UnqualComponentName]
| IllegalLibraryName PackageDescription
| NoModulesExposed Library
| SignaturesCabal2
| AutogenNotExposed
| AutogenIncludesNotIncluded
| NoMainIs Executable
| NoHsLhsMain
| MainCCabal1_18
| AutogenNoOther CEType UnqualComponentName
| AutogenIncludesNotIncludedExe
| TestsuiteTypeNotKnown TestType
| TestsuiteNotSupported TestType
| BenchmarkTypeNotKnown BenchmarkType
| BenchmarkNotSupported BenchmarkType
| NoHsLhsMainBench
| InvalidNameWin PackageDescription
| ZPrefix
| NoBuildType
| NoCustomSetup
| UnknownCompilers [String]
| UnknownLanguages [String]
| UnknownExtensions [String]
| LanguagesAsExtension [String]
| DeprecatedExtensions [(Extension, Maybe Extension)]
| MissingField CEField
| SynopsisTooLong
| ShortDesc
| InvalidTestWith [Dependency]
| ImpossibleInternalDep [Dependency]
| ImpossibleInternalExe [ExeDependency]
| MissingInternalExe [ExeDependency]
| NONELicense
| NoLicense
| AllRightsReservedLicense
| LicenseMessParse PackageDescription
| UnrecognisedLicense String
| UncommonBSD4
| UnknownLicenseVersion License [Version]
| NoLicenseFile
| UnrecognisedSourceRepo String
| MissingType
| MissingLocation
| MissingModule
| MissingTag
| SubdirRelPath
| SubdirGoodRelPath String
| OptFasm String
| OptViaC String
| OptHpc String
| OptProf String
| OptO String
| OptHide String
| OptMake String
| OptMain String
| OptONot String
| OptOOne String
| OptOTwo String
| OptSplitSections String
| OptSplitObjs String
| OptWls String
| OptExts String
| OptThreaded String
| OptRts String
| OptWithRts String
| COptONumber String String
| COptCPP String
| OptAlternatives String String [(String, String)]
| RelativeOutside String FilePath
| AbsolutePath String FilePath
| BadRelativePAth String FilePath String
| DistPoint (Maybe String) FilePath
| GlobSyntaxError String String
| InvalidOnWin [FilePath]
| FilePathTooLong FilePath
| FilePathNameTooLong FilePath
| FilePathSplitTooLong FilePath
| FilePathEmpty
| CVTestSuite
| CVDefaultLanguage
| CVDefaultLanguageComponent
| CVExtraDocFiles
| CVMultiLib
| CVReexported
| CVMixins
| CVExtraFrameworkDirs
| CVDefaultExtensions
| CVExtensionsDeprecated
| CVSources
| CVExtraDynamic [[String]]
| CVVirtualModules
| CVSourceRepository
| CVExtensions CabalSpecVersion [Extension]
| CVCustomSetup
| CVExpliticDepsCustomSetup
| CVAutogenPaths
| GlobNoMatch String String
| GlobExactMatch String String FilePath
| GlobNoDir String String FilePath
| UnknownOS [String]
| UnknownArch [String]
| UnknownCompiler [String]
| BaseNoUpperBounds
| SuspiciousFlagName [String]
| DeclaredUsedFlags (Set FlagName) (Set FlagName)
| NonASCIICustomField [String]
| RebindableClash
| WErrorUnneeded String
| JUnneeded String
| FDeferTypeErrorsUnneeded String
| DynamicUnneeded String
| ProfilingUnneeded String
| UpperBoundSetup String
| DuplicateModule String [ModuleName]
| PotentialDupModule String [ModuleName]
| BOMStart FilePath
| NotPackageName FilePath String
| NoDesc
| MultiDesc [String]
| UnknownFile String (SymbolicPath PackageDir LicenseFile)
| MissingSetupFile
| MissingConfigureScript
| UnknownDirectory String FilePath
| MissingSourceControl
deriving (Eq, Ord, Show)
-- | Wraps `ParseWarning` into `PackageCheck`.
--
wrapParseWarning :: FilePath -> PWarning -> PackageCheck
wrapParseWarning fp pw = PackageDistSuspicious (ParseWarning fp pw)
-- TODO: as Jul 2022 there is no severity indication attached PWarnType.
-- Once that is added, we can output something more appropriate
-- than PackageDistSuspicious for every parse warning.
-- (see: Cabal-syntax/src/Distribution/Parsec/Warning.hs)
-- | Pretty printing `CheckExplanation`.
--
ppExplanation :: CheckExplanation -> String
ppExplanation (ParseWarning fp pp) = showPWarning fp pp
ppExplanation NoNameField = "No 'name' field."
ppExplanation NoVersionField = "No 'version' field."
ppExplanation NoTarget =
"No executables, libraries, tests, or benchmarks found. Nothing to do."
ppExplanation UnnamedInternal =
"Found one or more unnamed internal libraries. Only the non-internal"
++ " library can have the same name as the package."
ppExplanation (DuplicateSections duplicateNames) =
"Duplicate sections: "
++ commaSep (map unUnqualComponentName duplicateNames)
++ ". The name of every library, executable, test suite,"
++ " and benchmark section in the package must be unique."
ppExplanation (IllegalLibraryName pkg) =
"Illegal internal library name "
++ prettyShow (packageName pkg)
++ ". Internal libraries cannot have the same name as the package."
++ " Maybe you wanted a non-internal library?"
++ " If so, rewrite the section stanza"
++ " from 'library: '" ++ prettyShow (packageName pkg)
++ "' to 'library'."
ppExplanation (NoModulesExposed lib) =
showLibraryName (libName lib) ++ " does not expose any modules"
ppExplanation SignaturesCabal2 =
"To use the 'signatures' field the package needs to specify "
++ "at least 'cabal-version: 2.0'."
ppExplanation AutogenNotExposed =
"An 'autogen-module' is neither on 'exposed-modules' or 'other-modules'."
ppExplanation AutogenIncludesNotIncluded =
"An include in 'autogen-includes' is neither in 'includes' or "
++ "'install-includes'."
ppExplanation (NoMainIs exe) =
"No 'main-is' field found for executable " ++ prettyShow (exeName exe)
ppExplanation NoHsLhsMain =
"The 'main-is' field must specify a '.hs' or '.lhs' file "
++ "(even if it is generated by a preprocessor), "
++ "or it may specify a C/C++/obj-C source file."
ppExplanation MainCCabal1_18 =
"The package uses a C/C++/obj-C source file for the 'main-is' field. "
++ "To use this feature you need to specify 'cabal-version: 1.18' or"
++ " higher."
ppExplanation (AutogenNoOther ct ucn) =
"On " ++ ppCE ct ++ " '" ++ prettyShow ucn ++ "' an 'autogen-module'"
++ " is not on 'other-modules'"
ppExplanation AutogenIncludesNotIncludedExe =
"An include in 'autogen-includes' is not in 'includes'."
ppExplanation (TestsuiteTypeNotKnown tt) =
quote (prettyShow tt) ++ " is not a known type of test suite. "
++ "Either remove the 'type' field or use a known type. "
++ "The known test suite types are: "
++ commaSep (map prettyShow knownTestTypes)
ppExplanation (TestsuiteNotSupported tt) =
quote (prettyShow tt) ++ " is not a supported test suite version. "
++ "Either remove the 'type' field or use a known type. "
++ "The known test suite types are: "
++ commaSep (map prettyShow knownTestTypes)
ppExplanation (BenchmarkTypeNotKnown tt) =
quote (prettyShow tt) ++ " is not a known type of benchmark. "
++ "Either remove the 'type' field or use a known type. "
++ "The known benchmark types are: "
++ commaSep (map prettyShow knownBenchmarkTypes)
ppExplanation (BenchmarkNotSupported tt) =
quote (prettyShow tt) ++ " is not a supported benchmark version. "
++ "Either remove the 'type' field or use a known type. "
++ "The known benchmark types are: "
++ commaSep (map prettyShow knownBenchmarkTypes)
ppExplanation NoHsLhsMainBench =
"The 'main-is' field must specify a '.hs' or '.lhs' file "
++ "(even if it is generated by a preprocessor)."
ppExplanation (InvalidNameWin pkg) =
"The package name '" ++ prettyShow (packageName pkg) ++ "' is "
++ "invalid on Windows. Many tools need to convert package names to "
++ "file names so using this name would cause problems."
ppExplanation ZPrefix =
"Package names with the prefix 'z-' are reserved by Cabal and "
++ "cannot be used."
ppExplanation NoBuildType =
"No 'build-type' specified. If you do not need a custom Setup.hs or "
++ "./configure script then use 'build-type: Simple'."
ppExplanation NoCustomSetup =
"Ignoring the 'custom-setup' section because the 'build-type' is "
++ "not 'Custom'. Use 'build-type: Custom' if you need to use a "
++ "custom Setup.hs script."
ppExplanation (UnknownCompilers unknownCompilers) =
"Unknown compiler " ++ commaSep (map quote unknownCompilers)
++ " in 'tested-with' field."
ppExplanation (UnknownLanguages unknownLanguages) =
"Unknown languages: " ++ commaSep unknownLanguages
ppExplanation (UnknownExtensions unknownExtensions) =
"Unknown extensions: " ++ commaSep unknownExtensions
ppExplanation (LanguagesAsExtension languagesUsedAsExtensions) =
"Languages listed as extensions: "
++ commaSep languagesUsedAsExtensions
++ ". Languages must be specified in either the 'default-language' "
++ " or the 'other-languages' field."
ppExplanation (DeprecatedExtensions ourDeprecatedExtensions) =
"Deprecated extensions: "
++ commaSep (map (quote . prettyShow . fst) ourDeprecatedExtensions)
++ ". " ++ unwords
[ "Instead of '" ++ prettyShow ext
++ "' use '" ++ prettyShow replacement ++ "'."
| (ext, Just replacement) <- ourDeprecatedExtensions ]
ppExplanation (MissingField cef) =
"No '" ++ ppCEField cef ++ "' field."
ppExplanation SynopsisTooLong =
"The 'synopsis' field is rather long (max 80 chars is recommended)."
ppExplanation ShortDesc =
"The 'description' field should be longer than the 'synopsis' field. "
++ "It's useful to provide an informative 'description' to allow "
++ "Haskell programmers who have never heard about your package to "
++ "understand the purpose of your package. "
++ "The 'description' field content is typically shown by tooling "
++ "(e.g. 'cabal info', Haddock, Hackage) below the 'synopsis' which "
++ "serves as a headline. "
++ "Please refer to <https://cabal.readthedocs.io/en/stable/"
++ "cabal-package.html#package-properties> for more details."
ppExplanation (InvalidTestWith testedWithImpossibleRanges) =
"Invalid 'tested-with' version range: "
++ commaSep (map prettyShow testedWithImpossibleRanges)
++ ". To indicate that you have tested a package with multiple "
++ "different versions of the same compiler use multiple entries, "
++ "for example 'tested-with: GHC==6.10.4, GHC==6.12.3' and not "
++ "'tested-with: GHC==6.10.4 && ==6.12.3'."
ppExplanation (ImpossibleInternalDep depInternalLibWithImpossibleVersion) =
"The package has an impossible version range for a dependency on an "
++ "internal library: "
++ commaSep (map prettyShow depInternalLibWithImpossibleVersion)
++ ". This version range does not include the current package, and must "
++ "be removed as the current package's library will always be used."
ppExplanation (ImpossibleInternalExe depInternalExecWithImpossibleVersion) =
"The package has an impossible version range for a dependency on an "
++ "internal executable: "
++ commaSep (map prettyShow depInternalExecWithImpossibleVersion)
++ ". This version range does not include the current package, and must "
++ "be removed as the current package's executable will always be used."
ppExplanation (MissingInternalExe depInternalExeWithImpossibleVersion) =
"The package depends on a missing internal executable: "
++ commaSep (map prettyShow depInternalExeWithImpossibleVersion)
ppExplanation NONELicense = "The 'license' field is missing or is NONE."
ppExplanation NoLicense = "The 'license' field is missing."
ppExplanation AllRightsReservedLicense =
"The 'license' is AllRightsReserved. Is that really what you want?"
ppExplanation (LicenseMessParse pkg) =
"Unfortunately the license " ++ quote (prettyShow (license pkg))
++ " messes up the parser in earlier Cabal versions so you need to "
++ "specify 'cabal-version: >= 1.4'. Alternatively if you require "
++ "compatibility with earlier Cabal versions then use 'OtherLicense'."
ppExplanation (UnrecognisedLicense l) =
quote ("license: " ++ l) ++ " is not a recognised license. The "
++ "known licenses are: " ++ commaSep (map prettyShow knownLicenses)
ppExplanation UncommonBSD4 =
"Using 'license: BSD4' is almost always a misunderstanding. 'BSD4' "
++ "refers to the old 4-clause BSD license with the advertising "
++ "clause. 'BSD3' refers the new 3-clause BSD license."
ppExplanation (UnknownLicenseVersion lic known) =
"'license: " ++ prettyShow lic ++ "' is not a known "
++ "version of that license. The known versions are "
++ commaSep (map prettyShow known)
++ ". If this is not a mistake and you think it should be a known "
++ "version then please file a ticket."
ppExplanation NoLicenseFile = "A 'license-file' is not specified."
ppExplanation (UnrecognisedSourceRepo kind) =
quote kind ++ " is not a recognised kind of source-repository. "
++ "The repo kind is usually 'head' or 'this'"
ppExplanation MissingType =
"The source-repository 'type' is a required field."
ppExplanation MissingLocation =
"The source-repository 'location' is a required field."
ppExplanation MissingModule =
"For a CVS source-repository, the 'module' is a required field."
ppExplanation MissingTag =
"For the 'this' kind of source-repository, the 'tag' is a required "
++ "field. It should specify the tag corresponding to this version "
++ "or release of the package."
ppExplanation SubdirRelPath =
"The 'subdir' field of a source-repository must be a relative path."
ppExplanation (SubdirGoodRelPath err) =
"The 'subdir' field of a source-repository is not a good relative path: "
++ show err
ppExplanation (OptFasm fieldName) =
"'" ++ fieldName ++ ": -fasm' is unnecessary and will not work on CPU "
++ "architectures other than x86, x86-64, ppc or sparc."
ppExplanation (OptViaC fieldName) =
"'" ++ fieldName ++": -fvia-C' is usually unnecessary. If your package "
++ "needs -via-C for correctness rather than performance then it "
++ "is using the FFI incorrectly and will probably not work with GHC "
++ "6.10 or later."
ppExplanation (OptHpc fieldName) =
"'" ++ fieldName ++ ": -fhpc' is not necessary. Use the configure flag "
++ " --enable-coverage instead."
ppExplanation (OptProf fieldName) =
"'" ++ fieldName ++ ": -prof' is not necessary and will lead to problems "
++ "when used on a library. Use the configure flag "
++ "--enable-library-profiling and/or --enable-profiling."
ppExplanation (OptO fieldName) =
"'" ++ fieldName ++ ": -o' is not needed. "
++ "The output files are named automatically."
ppExplanation (OptHide fieldName) =
"'" ++ fieldName ++ ": -hide-package' is never needed. "
++ "Cabal hides all packages."
ppExplanation (OptMake fieldName) =
"'" ++ fieldName
++ ": --make' is never needed. Cabal uses this automatically."
ppExplanation (OptMain fieldName) =
"'" ++ fieldName ++ ": -main-is' is not portable."
ppExplanation (OptONot fieldName) =
"'" ++ fieldName ++ ": -O0' is not needed. "
++ "Use the --disable-optimization configure flag."
ppExplanation (OptOOne fieldName) =
"'" ++ fieldName ++ ": -O' is not needed. "
++ "Cabal automatically adds the '-O' flag. "
++ "Setting it yourself interferes with the --disable-optimization flag."
ppExplanation (OptOTwo fieldName) =
"'" ++ fieldName ++ ": -O2' is rarely needed. "
++ "Check that it is giving a real benefit "
++ "and not just imposing longer compile times on your users."
ppExplanation (OptSplitSections fieldName) =
"'" ++ fieldName ++ ": -split-sections' is not needed. "
++ "Use the --enable-split-sections configure flag."
ppExplanation (OptSplitObjs fieldName) =
"'" ++ fieldName ++ ": -split-objs' is not needed. "
++ "Use the --enable-split-objs configure flag."
ppExplanation (OptWls fieldName) =
"'" ++ fieldName ++ ": -optl-Wl,-s' is not needed and is not portable to"
++ " all operating systems. Cabal 1.4 and later automatically strip"
++ " executables. Cabal also has a flag --disable-executable-stripping"
++ " which is necessary when building packages for some Linux"
++ " distributions and using '-optl-Wl,-s' prevents that from working."
ppExplanation (OptExts fieldName) =
"Instead of '" ++ fieldName ++ ": -fglasgow-exts' it is preferable to use "
++ "the 'extensions' field."
ppExplanation (OptThreaded fieldName) =
"'" ++ fieldName ++ ": -threaded' has no effect for libraries. It should "
++ "only be used for executables."
ppExplanation (OptRts fieldName) =
"'" ++ fieldName ++ ": -rtsopts' has no effect for libraries. It should "
++ "only be used for executables."
ppExplanation (OptWithRts fieldName) =
"'" ++ fieldName ++ ": -with-rtsopts' has no effect for libraries. It "
++ "should only be used for executables."
ppExplanation (COptONumber prefix label) =
"'" ++ prefix ++": -O[n]' is generally not needed. When building with "
++ " optimisations Cabal automatically adds '-O2' for " ++ label
++ " code. Setting it yourself interferes with the"
++ " --disable-optimization flag."
ppExplanation (COptCPP opt) =
"'cpp-options: " ++ opt ++ "' is not a portable C-preprocessor flag."
ppExplanation (OptAlternatives badField goodField flags) =
"Instead of " ++ quote (badField ++ ": " ++ unwords badFlags)
++ " use " ++ quote (goodField ++ ": " ++ unwords goodFlags)
where (badFlags, goodFlags) = unzip flags
ppExplanation (RelativeOutside field path) =
quote (field ++ ": " ++ path)
++ " is a relative path outside of the source tree. "
++ "This will not work when generating a tarball with 'sdist'."
ppExplanation (AbsolutePath field path) =
quote (field ++ ": " ++ path) ++ " specifies an absolute path, but the "
++ quote field ++ " field must use relative paths."
ppExplanation (BadRelativePAth field path err) =
quote (field ++ ": " ++ path)
++ " is not a good relative path: " ++ show err
ppExplanation (DistPoint mfield path) =
incipit ++ " points inside the 'dist' "
++ "directory. This is not reliable because the location of this "
++ "directory is configurable by the user (or package manager). In "
++ "addition the layout of the 'dist' directory is subject to change "
++ "in future versions of Cabal."
where -- mfiled Nothing -> the path is inside `ghc-options`
incipit = maybe ("'ghc-options' path " ++ quote path)
(\field -> quote (field ++ ": " ++ path))
mfield
ppExplanation (GlobSyntaxError field expl) =
"In the '" ++ field ++ "' field: " ++ expl
ppExplanation (InvalidOnWin paths) =
"The " ++ quotes paths ++ " invalid on Windows, which "
++ "would cause portability problems for this package. Windows file "
++ "names cannot contain any of the characters \":*?<>|\" and there "
++ "a few reserved names including \"aux\", \"nul\", \"con\", "
++ "\"prn\", \"com1-9\", \"lpt1-9\" and \"clock$\"."
where quotes [failed] = "path " ++ quote failed ++ " is"
quotes failed = "paths " ++ intercalate ", " (map quote failed)
++ " are"
ppExplanation (FilePathTooLong path) =
"The following file name is too long to store in a portable POSIX "
++ "format tar archive. The maximum length is 255 ASCII characters.\n"
++ "The file in question is:\n " ++ path
ppExplanation (FilePathNameTooLong path) =
"The following file name is too long to store in a portable POSIX "
++ "format tar archive. The maximum length for the name part (including "
++ "extension) is 100 ASCII characters. The maximum length for any "
++ "individual directory component is 155.\n"
++ "The file in question is:\n " ++ path
ppExplanation (FilePathSplitTooLong path) =
"The following file name is too long to store in a portable POSIX "
++ "format tar archive. While the total length is less than 255 ASCII "
++ "characters, there are unfortunately further restrictions. It has to "
++ "be possible to split the file path on a directory separator into "
++ "two parts such that the first part fits in 155 characters or less "
++ "and the second part fits in 100 characters or less. Basically you "
++ "have to make the file name or directory names shorter, or you could "
++ "split a long directory name into nested subdirectories with shorter "
++ "names.\nThe file in question is:\n " ++ path
ppExplanation FilePathEmpty =
"Encountered a file with an empty name, something is very wrong! "
++ "Files with an empty name cannot be stored in a tar archive or in "
++ "standard file systems."
ppExplanation CVTestSuite =
"The 'test-suite' section is new in Cabal 1.10. "
++ "Unfortunately it messes up the parser in older Cabal versions "
++ "so you must specify at least 'cabal-version: >= 1.8', but note "
++ "that only Cabal 1.10 and later can actually run such test suites."
ppExplanation CVDefaultLanguage =
"To use the 'default-language' field the package needs to specify "
++ "at least 'cabal-version: >= 1.10'."
ppExplanation CVDefaultLanguageComponent =
"Packages using 'cabal-version: >= 1.10' and before 'cabal-version: 3.4' "
++ "must specify the 'default-language' field for each component (e.g. "
++ "Haskell98 or Haskell2010). If a component uses different languages "
++ "in different modules then list the other ones in the "
++ "'other-languages' field."
ppExplanation CVExtraDocFiles =
"To use the 'extra-doc-files' field the package needs to specify "
++ "'cabal-version: 1.18' or higher."
ppExplanation CVMultiLib =
"To use multiple 'library' sections or a named library section "
++ "the package needs to specify at least 'cabal-version: 2.0'."
ppExplanation CVReexported =
"To use the 'reexported-module' field the package needs to specify "
++ "'cabal-version: 1.22' or higher."
ppExplanation CVMixins =
"To use the 'mixins' field the package needs to specify "
++ "at least 'cabal-version: 2.0'."
ppExplanation CVExtraFrameworkDirs =
"To use the 'extra-framework-dirs' field the package needs to specify"
++ " 'cabal-version: 1.24' or higher."
ppExplanation CVDefaultExtensions =
"To use the 'default-extensions' field the package needs to specify "
++ "at least 'cabal-version: >= 1.10'."
ppExplanation CVExtensionsDeprecated =
"For packages using 'cabal-version: >= 1.10' the 'extensions' "
++ "field is deprecated. The new 'default-extensions' field lists "
++ "extensions that are used in all modules in the component, while "
++ "the 'other-extensions' field lists extensions that are used in "
++ "some modules, e.g. via the {-# LANGUAGE #-} pragma."
ppExplanation CVSources =
"The use of 'asm-sources', 'cmm-sources', 'extra-bundled-libraries' "
++ " and 'extra-library-flavours' requires the package "
++ " to specify at least 'cabal-version: 3.0'."
ppExplanation (CVExtraDynamic flavs) =
"The use of 'extra-dynamic-library-flavours' requires the package "
++ " to specify at least 'cabal-version: 3.0'. The flavours are: "
++ commaSep (concat flavs)
ppExplanation CVVirtualModules =
"The use of 'virtual-modules' requires the package "
++ " to specify at least 'cabal-version: 2.2'."
ppExplanation CVSourceRepository =
"The 'source-repository' section is new in Cabal 1.6. "
++ "Unfortunately it messes up the parser in earlier Cabal versions "
++ "so you need to specify 'cabal-version: >= 1.6'."
ppExplanation (CVExtensions version extCab12) =
"Unfortunately the language extensions "
++ commaSep (map (quote . prettyShow) extCab12)
++ " break the parser in earlier Cabal versions so you need to "
++ "specify 'cabal-version: >= " ++ showCabalSpecVersion version
++ "'. Alternatively if you require compatibility with earlier "
++ "Cabal versions then you may be able to use an equivalent "
++ "compiler-specific flag."
ppExplanation CVCustomSetup =
"Packages using 'cabal-version: 1.24' or higher with 'build-type: Custom' "
++ "must use a 'custom-setup' section with a 'setup-depends' field "
++ "that specifies the dependencies of the Setup.hs script itself. "
++ "The 'setup-depends' field uses the same syntax as 'build-depends', "
++ "so a simple example would be 'setup-depends: base, Cabal'."
ppExplanation CVExpliticDepsCustomSetup =
"From version 1.24 cabal supports specifying explicit dependencies "
++ "for Custom setup scripts. Consider using 'cabal-version: 1.24' or "
++ "higher and adding a 'custom-setup' section with a 'setup-depends' "
++ "field that specifies the dependencies of the Setup.hs script "
++ "itself. The 'setup-depends' field uses the same syntax as "
++ "'build-depends', so a simple example would be 'setup-depends: base, "
++ "Cabal'."
ppExplanation CVAutogenPaths =
"Packages using 'cabal-version: 2.0' and the autogenerated "
++ "module Paths_* must include it also on the 'autogen-modules' field "
++ "besides 'exposed-modules' and 'other-modules'. This specifies that "
++ "the module does not come with the package and is generated on "
++ "setup. Modules built with a custom Setup.hs script also go here "
++ "to ensure that commands like sdist don't fail."
ppExplanation (GlobNoMatch field glob) =
"In '" ++ field ++ "': the pattern '" ++ glob ++ "' does not"
++ " match any files."
ppExplanation (GlobExactMatch field glob file) =
"In '" ++ field ++ "': the pattern '" ++ glob ++ "' does not"
++ " match the file '" ++ file ++ "' because the extensions do not"
++ " exactly match (e.g., foo.en.html does not exactly match *.html)."
++ " To enable looser suffix-only matching, set 'cabal-version: 2.4' or"
++ " higher."
ppExplanation (GlobNoDir field glob dir) =
"In '" ++ field ++ "': the pattern '" ++ glob ++ "' attempts to"
++ " match files in the directory '" ++ dir ++ "', but there is no"
++ " directory by that name."
ppExplanation (UnknownOS unknownOSs) =
"Unknown operating system name " ++ commaSep (map quote unknownOSs)
ppExplanation (UnknownArch unknownArches) =
"Unknown architecture name " ++ commaSep (map quote unknownArches)
ppExplanation (UnknownCompiler unknownImpls) =
"Unknown compiler name " ++ commaSep (map quote unknownImpls)
ppExplanation BaseNoUpperBounds =
"The dependency 'build-depends: base' does not specify an upper "
++ "bound on the version number. Each major release of the 'base' "
++ "package changes the API in various ways and most packages will "
++ "need some changes to compile with it. The recommended practice "
++ "is to specify an upper bound on the version of the 'base' "
++ "package. This ensures your package will continue to build when a "
++ "new major version of the 'base' package is released. If you are "
++ "not sure what upper bound to use then use the next major "
++ "version. For example if you have tested your package with 'base' "
++ "version 4.5 and 4.6 then use 'build-depends: base >= 4.5 && < 4.7'."
ppExplanation (SuspiciousFlagName invalidFlagNames) =
"Suspicious flag names: " ++ unwords invalidFlagNames ++ ". "
++ "To avoid ambiguity in command line interfaces, flag shouldn't "
++ "start with a dash. Also for better compatibility, flag names "
++ "shouldn't contain non-ascii characters."
ppExplanation (DeclaredUsedFlags declared used) =
"Declared and used flag sets differ: "
++ s declared ++ " /= " ++ s used ++ ". "
where s :: Set.Set FlagName -> String
s = commaSep . map unFlagName . Set.toList
ppExplanation (NonASCIICustomField nonAsciiXFields) =
"Non ascii custom fields: " ++ unwords nonAsciiXFields ++ ". "
++ "For better compatibility, custom field names "
++ "shouldn't contain non-ascii characters."
ppExplanation RebindableClash =
"Packages using RebindableSyntax with OverloadedStrings or"
++ " OverloadedLists in default-extensions, in conjunction with the"
++ " autogenerated module Paths_*, are known to cause compile failures"
++ " with Cabal < 2.2. To use these default-extensions with a Paths_*"
++ " autogen module, specify at least 'cabal-version: 2.2'."
ppExplanation (WErrorUnneeded fieldName) = addConditionalExp $
"'" ++ fieldName ++ ": -Werror' makes the package easy to "
++ "break with future GHC versions because new GHC versions often "
++ "add new warnings."
ppExplanation (JUnneeded fieldName) = addConditionalExp $
"'" ++ fieldName ++ ": -j[N]' can make sense for specific user's setup,"
++ " but it is not appropriate for a distributed package."
ppExplanation (FDeferTypeErrorsUnneeded fieldName) = addConditionalExp $
"'" ++ fieldName ++ ": -fdefer-type-errors' is fine during development "
++ "but is not appropriate for a distributed package."
ppExplanation (DynamicUnneeded fieldName) = addConditionalExp $
"'" ++ fieldName ++ ": -d*' debug flags are not appropriate "
++ "for a distributed package."
ppExplanation (ProfilingUnneeded fieldName) = addConditionalExp $
"'" ++ fieldName ++ ": -fprof*' profiling flags are typically not "
++ "appropriate for a distributed library package. These flags are "
++ "useful to profile this package, but when profiling other packages "
++ "that use this one these flags clutter the profile output with "
++ "excessive detail. If you think other packages really want to see "
++ "cost centres from this package then use '-fprof-auto-exported' "
++ "which puts cost centres only on exported functions."
ppExplanation (UpperBoundSetup nm) =
"The dependency 'setup-depends: '"++nm++"' does not specify an "
++ "upper bound on the version number. Each major release of the "
++ "'"++nm++"' package changes the API in various ways and most "
++ "packages will need some changes to compile with it. If you are "
++ "not sure what upper bound to use then use the next major "
++ "version."
ppExplanation (DuplicateModule s dupLibsLax) =
"Duplicate modules in " ++ s ++ ": "
++ commaSep (map prettyShow dupLibsLax)
ppExplanation (PotentialDupModule s dupLibsStrict) =
"Potential duplicate modules (subject to conditionals) in " ++ s
++ ": " ++ commaSep (map prettyShow dupLibsStrict)
ppExplanation (BOMStart pdfile) =
pdfile ++ " starts with an Unicode byte order mark (BOM)."
++ " This may cause problems with older cabal versions."
ppExplanation (NotPackageName pdfile expectedCabalname) =
"The filename " ++ quote pdfile ++ " does not match package name "
++ "(expected: " ++ quote expectedCabalname ++ ")"
ppExplanation NoDesc =
"No cabal file found.\n"
++ "Please create a package description file <pkgname>.cabal"
ppExplanation (MultiDesc multiple) =
"Multiple cabal files found while checking.\n"
++ "Please use only one of: "
++ intercalate ", " multiple
ppExplanation (UnknownFile fieldname file) =
"The '" ++ fieldname ++ "' field refers to the file "
++ quote (getSymbolicPath file) ++ " which does not exist."
ppExplanation MissingSetupFile =
"The package is missing a Setup.hs or Setup.lhs script."
ppExplanation MissingConfigureScript =
"The 'build-type' is 'Configure' but there is no 'configure' script. "
++ "You probably need to run 'autoreconf -i' to generate it."
ppExplanation (UnknownDirectory kind dir) =
quote (kind ++ ": " ++ dir)
++ " specifies a directory which does not exist."
ppExplanation MissingSourceControl =
"When distributing packages it is encouraged to specify source "
++ "control information in the .cabal file using one or more "
++ "'source-repository' sections. See the Cabal user guide for "
++ "details."
-- | Results of some kind of failed package check.
--
-- There are a range of severities, from merely dubious to totally insane.
-- All of them come with a human readable explanation. In future we may augment
-- them with more machine readable explanations, for example to help an IDE
-- suggest automatic corrections.
--
data PackageCheck =
-- | This package description is no good. There's no way it's going to
-- build sensibly. This should give an error at configure time.
PackageBuildImpossible { explanation :: CheckExplanation }
-- | A problem that is likely to affect building the package, or an
-- issue that we'd like every package author to be aware of, even if
-- the package is never distributed.
| PackageBuildWarning { explanation :: CheckExplanation }
-- | An issue that might not be a problem for the package author but
-- might be annoying or detrimental when the package is distributed to
-- users. We should encourage distributed packages to be free from these
-- issues, but occasionally there are justifiable reasons so we cannot
-- ban them entirely.
| PackageDistSuspicious { explanation :: CheckExplanation }
-- | Like PackageDistSuspicious but will only display warnings
-- rather than causing abnormal exit when you run 'cabal check'.
| PackageDistSuspiciousWarn { explanation :: CheckExplanation }
-- | An issue that is OK in the author's environment but is almost
-- certain to be a portability problem for other environments. We can
-- quite legitimately refuse to publicly distribute packages with these
-- problems.
| PackageDistInexcusable { explanation :: CheckExplanation }
deriving (Eq, Ord)
instance Show PackageCheck where
show notice = ppExplanation (explanation notice)
check :: Bool -> PackageCheck -> Maybe PackageCheck
check False _ = Nothing
check True pc = Just pc
checkSpecVersion :: PackageDescription -> CabalSpecVersion -> Bool -> PackageCheck
-> Maybe PackageCheck
checkSpecVersion pkg specver cond pc
| specVersion pkg >= specver = Nothing
| otherwise = check cond pc
-- ------------------------------------------------------------
-- * Standard checks
-- ------------------------------------------------------------
-- | Check for common mistakes and problems in package descriptions.
--
-- This is the standard collection of checks covering all aspects except
-- for checks that require looking at files within the package. For those
-- see 'checkPackageFiles'.
--
-- It requires the 'GenericPackageDescription' and optionally a particular
-- configuration of that package. If you pass 'Nothing' then we just check
-- a version of the generic description using 'flattenPackageDescription'.
--
checkPackage :: GenericPackageDescription
-> Maybe PackageDescription
-> [PackageCheck]
checkPackage gpkg mpkg =
checkConfiguredPackage pkg
++ checkConditionals gpkg
++ checkPackageVersions gpkg
++ checkDevelopmentOnlyFlags gpkg
++ checkFlagNames gpkg
++ checkUnusedFlags gpkg
++ checkUnicodeXFields gpkg
++ checkPathsModuleExtensions pkg
++ checkSetupVersions gpkg
++ checkDuplicateModules gpkg
where
pkg = fromMaybe (flattenPackageDescription gpkg) mpkg
--TODO: make this variant go away
-- we should always know the GenericPackageDescription
checkConfiguredPackage :: PackageDescription -> [PackageCheck]
checkConfiguredPackage pkg =
checkSanity pkg
++ checkFields pkg
++ checkLicense pkg
++ checkSourceRepos pkg
++ checkAllGhcOptions pkg
++ checkCCOptions pkg
++ checkCxxOptions pkg
++ checkCPPOptions pkg
++ checkPaths pkg
++ checkCabalVersion pkg
-- ------------------------------------------------------------
-- * Basic sanity checks
-- ------------------------------------------------------------
-- | Check that this package description is sane.
--
checkSanity :: PackageDescription -> [PackageCheck]
checkSanity pkg =
catMaybes [
check (null . unPackageName . packageName $ pkg) $
PackageBuildImpossible NoNameField
, check (nullVersion == packageVersion pkg) $
PackageBuildImpossible NoVersionField
, check (all ($ pkg) [ null . executables
, null . testSuites
, null . benchmarks
, null . allLibraries
, null . foreignLibs ]) $
PackageBuildImpossible NoTarget
, check (any (== LMainLibName) (map libName $ subLibraries pkg)) $
PackageBuildImpossible UnnamedInternal
, check (not (null duplicateNames)) $
PackageBuildImpossible (DuplicateSections duplicateNames)
-- NB: but it's OK for executables to have the same name!
-- TODO shouldn't need to compare on the string level
, check (any (== prettyShow (packageName pkg))
(prettyShow <$> subLibNames)) $
PackageBuildImpossible (IllegalLibraryName pkg)
]
--TODO: check for name clashes case insensitively: windows file systems cannot
--cope.
++ concatMap (checkLibrary pkg) (allLibraries pkg)
++ concatMap (checkExecutable pkg) (executables pkg)
++ concatMap (checkTestSuite pkg) (testSuites pkg)
++ concatMap (checkBenchmark pkg) (benchmarks pkg)
where
-- The public 'library' gets special dispensation, because it
-- is common practice to export a library and name the executable
-- the same as the package.
subLibNames = mapMaybe (libraryNameString . libName) $ subLibraries pkg
exeNames = map exeName $ executables pkg
testNames = map testName $ testSuites pkg
bmNames = map benchmarkName $ benchmarks pkg
duplicateNames = dups $ subLibNames ++ exeNames ++ testNames ++ bmNames
checkLibrary :: PackageDescription -> Library -> [PackageCheck]
checkLibrary pkg lib =
catMaybes [
-- TODO: This check is bogus if a required-signature was passed through
check (null (explicitLibModules lib) && null (reexportedModules lib)) $
PackageDistSuspiciousWarn (NoModulesExposed lib)
-- check use of signatures sections
, checkVersion CabalSpecV2_0 (not (null (signatures lib))) $
PackageDistInexcusable SignaturesCabal2
-- check that all autogen-modules appear on other-modules or exposed-modules
, check
(not $ and $ map (flip elem (explicitLibModules lib)) (libModulesAutogen lib)) $
PackageBuildImpossible AutogenNotExposed
-- check that all autogen-includes appear on includes or install-includes
, check
(not $ and $ map (flip elem (allExplicitIncludes lib)) (view L.autogenIncludes lib)) $
PackageBuildImpossible AutogenIncludesNotIncluded
]
where
checkVersion :: CabalSpecVersion -> Bool -> PackageCheck -> Maybe PackageCheck
checkVersion ver cond pc
| specVersion pkg >= ver = Nothing
| otherwise = check cond pc
allExplicitIncludes :: L.HasBuildInfo a => a -> [FilePath]
allExplicitIncludes x = view L.includes x ++ view L.installIncludes x
checkExecutable :: PackageDescription -> Executable -> [PackageCheck]
checkExecutable pkg exe =
catMaybes [
check (null (modulePath exe)) $
PackageBuildImpossible (NoMainIs exe)
-- This check does not apply to scripts.
, check (package pkg /= fakePackageId
&& not (null (modulePath exe))
&& not (fileExtensionSupportedLanguage $ modulePath exe)) $
PackageBuildImpossible NoHsLhsMain
, checkSpecVersion pkg CabalSpecV1_18
(fileExtensionSupportedLanguage (modulePath exe)
&& takeExtension (modulePath exe) `notElem` [".hs", ".lhs"]) $
PackageDistInexcusable MainCCabal1_18
-- check that all autogen-modules appear on other-modules
, check
(not $ and $ map (flip elem (exeModules exe)) (exeModulesAutogen exe)) $
PackageBuildImpossible (AutogenNoOther CETExecutable (exeName exe))
-- check that all autogen-includes appear on includes
, check
(not $ and $ map (flip elem (view L.includes exe)) (view L.autogenIncludes exe)) $
PackageBuildImpossible AutogenIncludesNotIncludedExe
]
checkTestSuite :: PackageDescription -> TestSuite -> [PackageCheck]
checkTestSuite pkg test =
catMaybes [
case testInterface test of
TestSuiteUnsupported tt@(TestTypeUnknown _ _) -> Just $
PackageBuildWarning (TestsuiteTypeNotKnown tt)
TestSuiteUnsupported tt -> Just $
PackageBuildWarning (TestsuiteNotSupported tt)
_ -> Nothing
, check mainIsWrongExt $
PackageBuildImpossible NoHsLhsMain
, checkSpecVersion pkg CabalSpecV1_18 (mainIsNotHsExt && not mainIsWrongExt) $
PackageDistInexcusable MainCCabal1_18
-- check that all autogen-modules appear on other-modules
, check
(not $ and $ map (flip elem (testModules test)) (testModulesAutogen test)) $
PackageBuildImpossible (AutogenNoOther CETTest (testName test))