-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathget-cabal-configuration.hs
1066 lines (936 loc) · 33.2 KB
/
get-cabal-configuration.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
-- Copyright (C) 2016-2022 Sergey Vinokurov <[email protected]>
-- Copyright (C) 2014-2016 Sebastian Wiesner <[email protected]>
-- Copyright (C) 2016-2018 Danny Navarro <[email protected]>
-- Copyright (C) 2015 Mark Karpov <[email protected]>
-- Copyright (C) 2015 Michael Alan Dorman <[email protected]>
-- Copyright (C) 2014 Gracjan Polak <[email protected]>
-- This file is not part of GNU Emacs.
-- This program is free software; you can redistribute it and/or modify it under
-- the terms of the GNU General Public License as published by the Free Software
-- Foundation, either version 3 of the License, or (at your option) any later
-- version.
-- This program is distributed in the hope that it will be useful, but WITHOUT
-- ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
-- FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
-- details.
-- You should have received a copy of the GNU General Public License along with
-- this program. If not, see <http://www.gnu.org/licenses/>.
{-# LANGUAGE BangPatterns #-}
{-# LANGUAGE CPP #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE NamedFieldPuns #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE ScopedTypeVariables #-}
module Main (main) where
#if __GLASGOW_HASKELL__ >= 800
#define GHC_INCLUDES_VERSION_MACRO 1
#endif
#if defined(GHC_INCLUDES_VERSION_MACRO)
# if MIN_VERSION_Cabal(3, 8, 0)
# define Cabal38OrLater 1
# define Cabal36OrLater 1
# define Cabal32OrLater 1
# define Cabal30OrLater 1
# define Cabal24OrLater 1
# define Cabal22OrLater 1
# define Cabal20OrLater 1
# elif MIN_VERSION_Cabal(3, 6, 0)
# define Cabal36OrLater 1
# define Cabal32OrLater 1
# define Cabal30OrLater 1
# define Cabal24OrLater 1
# define Cabal22OrLater 1
# define Cabal20OrLater 1
# elif MIN_VERSION_Cabal(3, 2, 0)
# define Cabal32OrLater 1
# define Cabal30OrLater 1
# define Cabal24OrLater 1
# define Cabal22OrLater 1
# define Cabal20OrLater 1
# elif MIN_VERSION_Cabal(3, 0, 0)
# define Cabal30OrLater 1
# define Cabal24OrLater 1
# define Cabal22OrLater 1
# define Cabal20OrLater 1
# elif MIN_VERSION_Cabal(2, 3, 0)
# define Cabal24OrLater 1
# define Cabal22OrLater 1
# define Cabal20OrLater 1
# elif MIN_VERSION_Cabal(2, 1, 0)
# define Cabal22 1
# define Cabal22OrLater 1
# define Cabal20OrLater 1
# elif MIN_VERSION_Cabal(2, 0, 0)
# define Cabal20OrLater 1
# endif
# if MIN_VERSION_bytestring(0, 10, 2)
# define bytestring_10_2_or_later 1
# define bytestring_10_0_or_later 1
# elif MIN_VERSION_bytestring(0, 10, 0)
# define bytestring_10_0_or_later 1
# endif
#else
# if __GLASGOW_HASKELL__ > 810
# define bytestring_10_2_or_later 1
# define bytestring_10_0_or_later 1
# elif __GLASGOW_HASKELL__ > 704
# define bytestring_10_0_or_later 1
# define bytestring_10_0_or_later 1
# endif
#endif
#if __GLASGOW_HASKELL__ >= 704
# define Cabal114OrMore 1
#endif
#if __GLASGOW_HASKELL__ < 710
# define Cabal118OrLess 1
#endif
#if __GLASGOW_HASKELL__ <= 706
#undef HAVE_DATA_FUNCTOR_IDENTITY
#else
#define HAVE_DATA_FUNCTOR_IDENTITY
#endif
import qualified Data.ByteString.Char8 as C8
#if defined(bytestring_10_2_or_later)
import qualified Data.ByteString.Builder as BS.Builder
#elif defined(bytestring_10_0_or_later)
import qualified Data.ByteString.Lazy.Builder as BS.Builder
#else
import qualified Data.ByteString.Lazy.Char8 as CL8
#endif
import Distribution.ModuleName (ModuleName)
import qualified Distribution.ModuleName as ModuleName
import Distribution.PackageDescription ()
#if defined(Cabal20OrLater)
import Distribution.Types.Benchmark (benchmarkBuildInfo, benchmarkInterface)
import Distribution.Types.ForeignLib (foreignLibBuildInfo)
import Distribution.Types.TestSuite (testBuildInfo, testInterface)
import Distribution.PackageDescription (allLibraries, libName)
#else
import Distribution.PackageDescription (library)
#endif
import qualified Control.Applicative as A
import Control.Exception (SomeException, try)
import Control.Monad (when)
#if defined(Cabal22OrLater)
import qualified Data.ByteString as BS
#endif
import Data.Char (isSpace)
#if defined(HAVE_DATA_FUNCTOR_IDENTITY)
import Data.Functor.Identity
#endif
import Data.List (nub, foldl', intersperse)
import Data.Maybe (maybeToList)
#if __GLASGOW_HASKELL__ < 710
import Data.Monoid
#endif
#if defined(Cabal32OrLater)
import Data.List.NonEmpty (toList)
#endif
import Data.Set (Set)
import qualified Data.Set as S
#if defined(Cabal118OrLess)
import Distribution.Compiler
(CompilerFlavor(GHC), CompilerId(CompilerId), buildCompilerFlavor)
#else
import Distribution.Compiler
(AbiTag(NoAbiTag), CompilerFlavor(GHC), CompilerId(CompilerId),
CompilerInfo, buildCompilerFlavor, unknownCompilerInfo)
#endif
import Distribution.Package
(pkgName, Dependency(..))
import Distribution.PackageDescription
(GenericPackageDescription, PackageDescription(..),
TestSuiteInterface(..), BuildInfo(..), Library, Executable,
allBuildInfo, usedExtensions, allLanguages, hcOptions, exeName,
buildInfo, modulePath, libBuildInfo, exposedModules)
import Distribution.Simple.BuildPaths (defaultDistPref)
import Distribution.Simple.Utils (cabalVersion)
import Distribution.System (buildPlatform)
#if defined(Cabal20OrLater)
import Distribution.System (OS(OSX), buildArch, buildOS)
#endif
import Distribution.Text (display)
#if defined(Cabal20OrLater)
import Distribution.Types.PackageId (PackageId)
#endif
import Distribution.Verbosity (silent)
import Language.Haskell.Extension (Extension(..),Language(..))
import System.Console.GetOpt
import System.Environment (getArgs)
import System.Exit (ExitCode(..), exitFailure, exitSuccess)
import System.FilePath ((</>), dropFileName, normalise, isPathSeparator)
import System.Info (compilerVersion)
import System.IO (Handle, hGetContents, hPutStrLn, stderr, stdout)
import System.Process (readProcessWithExitCode)
import qualified System.Process as Process
#if __GLASGOW_HASKELL__ >= 710 && !defined(Cabal20OrLater) && !defined(Cabal22OrLater)
import Data.Version (Version)
#endif
#if defined(Cabal24OrLater)
import Distribution.PackageDescription (allBuildDepends)
#endif
#if defined(Cabal114OrMore)
import Distribution.PackageDescription (BenchmarkInterface(..),)
#endif
#if defined(Cabal20OrLater)
import Control.Monad (filterM)
import Distribution.Package (unPackageName, depPkgName, PackageName)
import Distribution.PackageDescription.Configuration (finalizePD)
import Distribution.Types.ComponentRequestedSpec (ComponentRequestedSpec(..))
import Distribution.Types.ForeignLib (ForeignLib(foreignLibName))
import Distribution.Types.UnqualComponentName (unUnqualComponentName)
import qualified Distribution.Version as CabalVersion
import Distribution.Types.Benchmark (Benchmark(benchmarkName))
import Distribution.Types.TestSuite (TestSuite(testName))
import System.Directory (doesDirectoryExist, doesFileExist)
#else
import Control.Arrow (second)
import Data.Version (showVersion)
import Distribution.Package (PackageName(..))
import Distribution.PackageDescription.Configuration
(finalizePackageDescription, mapTreeData)
# if defined(Cabal114OrMore)
import Distribution.PackageDescription
(TestSuite(..), Benchmark(..),
condTestSuites, condBenchmarks, benchmarkEnabled, testEnabled)
# else
import Distribution.PackageDescription
(TestSuite(..), condTestSuites, testEnabled)
# endif
#endif
#if defined(Cabal22OrLater)
import Distribution.Pretty (prettyShow)
#endif
#if defined(Cabal32OrLater)
import Distribution.PackageDescription (mkFlagAssignment)
#elif defined(Cabal22OrLater)
import Distribution.Types.GenericPackageDescription (mkFlagAssignment)
#endif
#if defined(Cabal22OrLater)
# if defined(Cabal38OrLater)
import Distribution.Simple.PackageDescription
(readGenericPackageDescription)
import Distribution.PackageDescription.Parsec
(runParseResult, parseGenericPackageDescription)
# else
import Distribution.PackageDescription.Parsec
(runParseResult, readGenericPackageDescription, parseGenericPackageDescription)
# endif
# if defined(Cabal30OrLater)
import Distribution.Parsec.Error (showPError)
#else
import Distribution.Parsec.Common (showPError)
# endif
#elif defined(Cabal20OrLater)
import Distribution.PackageDescription.Parse
(ParseResult(..), readGenericPackageDescription, parseGenericPackageDescription)
import Distribution.ParseUtils (locatedErrorMsg)
#else
import Distribution.PackageDescription.Parse
(ParseResult(..), parsePackageDescription, readPackageDescription)
import Distribution.ParseUtils (locatedErrorMsg)
#endif
#if defined(Cabal30OrLater)
import Distribution.Types.LibraryName (libraryNameString)
#endif
#if defined(Cabal36OrLater)
import Distribution.Utils.Path (getSymbolicPath)
#endif
newtype UnixFilepath = UnixFilepath { unUnixFilepath :: C8.ByteString }
deriving (Eq, Ord)
mkUnixFilepath :: FilePath -> UnixFilepath
mkUnixFilepath = UnixFilepath . C8.map (\c -> if isPathSeparator c then '/' else c) . C8.pack . normalise
data Sexp
= SList [Sexp]
| SString C8.ByteString
| SSymbol C8.ByteString
data TargetTool = Cabal | Stack
#if defined(Cabal20OrLater)
| CabalNew PackageId GhcVersion
type GhcVersion = String
#endif
#if defined(bytestring_10_0_or_later)
type Builder = BS.Builder.Builder
builderFromByteString :: C8.ByteString -> Builder
builderFromByteString = BS.Builder.byteString
builderFromChar :: Char -> Builder
builderFromChar = BS.Builder.char8
hPutBuilder :: Handle -> Builder -> IO ()
hPutBuilder = BS.Builder.hPutBuilder
#else
type Builder = Endo CL8.ByteString
builderFromByteString :: C8.ByteString -> Builder
builderFromByteString x = Endo (CL8.fromChunks [x] `mappend`)
builderFromChar :: Char -> Builder
builderFromChar c = Endo (CL8.singleton c `mappend`)
hPutBuilder :: Handle -> Builder -> IO ()
hPutBuilder h (Endo f) = CL8.hPut h $ f CL8.empty
#endif
sym :: C8.ByteString -> Sexp
sym = SSymbol
renderSexp :: Sexp -> Builder
renderSexp (SSymbol s) = builderFromByteString s
renderSexp (SString s) = dquote `mappend` builderFromByteString s `mappend` dquote
where
dquote = builderFromChar '"'
renderSexp (SList xs) =
lparen `mappend` mconcat (intersperse space (map renderSexp xs)) `mappend` rparen
where
lparen = builderFromChar '('
rparen = builderFromChar ')'
space = builderFromChar ' '
class ToSexp a where
toSexp :: a -> Sexp
instance ToSexp C8.ByteString where
toSexp = SString
instance ToSexp UnixFilepath where
toSexp = SString . unUnixFilepath
instance ToSexp Extension where
toSexp (EnableExtension ext) = toSexp (C8.pack (show ext))
toSexp (DisableExtension ext) = toSexp ("No" `mappend` C8.pack (show ext))
toSexp (UnknownExtension ext) = toSexp (C8.pack ext)
instance ToSexp Language where
toSexp (UnknownLanguage lang) = toSexp (C8.pack lang)
toSexp lang = toSexp (C8.pack (show lang))
instance ToSexp Dependency where
toSexp = toSexp . C8.pack . unPackageName' . depPkgName'
instance ToSexp Sexp where
toSexp = id
instance ToSexp Bool where
toSexp b = SSymbol $ if b then "t" else "nil"
instance ToSexp a => ToSexp [a] where
toSexp = SList . map toSexp
instance ToSexp a => ToSexp (Maybe a) where
toSexp Nothing = SSymbol "nil"
toSexp (Just x) = toSexp x
instance ToSexp ModuleName where
toSexp = toSexp . map C8.pack . ModuleName.components
instance (ToSexp a, ToSexp b, ToSexp c, ToSexp d) => ToSexp (a, b, c, d) where
toSexp (a, b, c, d) = SList [toSexp a, toSexp b, toSexp c, toSexp d]
cons :: (ToSexp a, ToSexp b) => a -> [b] -> Sexp
cons h t = SList (toSexp h : map toSexp t)
-- | Get possible dist directory
distDir :: TargetTool -> IO FilePath
distDir Cabal = return defaultDistPref
distDir Stack = do
res <- try $ readProcessWithExitCode "stack" ["path", "--dist-dir"] []
return $ case res of
Left (_ :: SomeException) -> defaultDistDir
Right (ExitSuccess, stdOut, _) -> stripWhitespace stdOut
Right (ExitFailure _, _, _) -> defaultDistDir
where
defaultDistDir :: FilePath
defaultDistDir =
".stack-work" </> defaultDistPref
</> display buildPlatform
</> "Cabal-" ++ cabalVersion'
#if defined(Cabal20OrLater)
distDir (CabalNew packageId ghcVersion) =
return $ "dist-newstyle/build" </> display buildPlatform
</> "ghc-" ++ ghcVersion
</> display packageId
#endif
getBuildDirectories
:: TargetTool
-> PackageDescription
-> FilePath
-> IO ([UnixFilepath], [UnixFilepath])
getBuildDirectories tool pkgDesc cabalDir = do
distDir' <- distDir tool
let buildDir :: FilePath
buildDir = cabalDir </> distDir' </> "build"
componentNames :: [String]
componentNames =
getExeNames pkgDesc ++
getTestNames pkgDesc ++
getBenchmarkNames pkgDesc ++
getForeignLibNames pkgDesc
autogenDirs <- getAutogenDirs buildDir componentNames
let componentBuildDir :: String -> FilePath
componentBuildDir componentName =
#if defined(Cabal20OrLater)
case tool of
CabalNew _ _ -> cabalDir </> distDir'
</> "build"
</> componentName
</> (componentName ++ "-tmp")
_ -> buildDir </> componentName </> (componentName ++ "-tmp")
#else
buildDir </> componentName </> (componentName ++ "-tmp")
#endif
buildDirs :: [FilePath]
buildDirs =
autogenDirs ++ map componentBuildDir componentNames
buildDirs' = case library pkgDesc of
Just _ -> buildDir : buildDirs
Nothing -> buildDirs
return (map mkUnixFilepath buildDirs', map mkUnixFilepath autogenDirs)
getAutogenDirs :: FilePath -> [String] -> IO [FilePath]
getAutogenDirs buildDir componentNames =
(autogenDir :) A.<$> componentsAutogenDirs buildDir componentNames
where
-- 'dist/bulid/autogen' OR
-- '.stack-work/dist/x86_64-linux/Cabal-1.24.2.0/build/autogen' OR
-- ./dist-newstyle/build/x86_64-linux/ghc-8.4.3/lens-4.17/build/autogen
autogenDir :: FilePath
autogenDir = buildDir </> "autogen"
getSourceDirectories :: [BuildInfo] -> FilePath -> [String]
getSourceDirectories buildInfo cabalDir =
map (cabalDir </>) (concatMap hsSourceDirs' buildInfo)
getIncludeDirectories :: [BuildInfo] -> FilePath -> [String]
getIncludeDirectories buildInfo cabalDir =
map (cabalDir </>) (concatMap includeDirs buildInfo)
hsSourceDirs' :: BuildInfo -> [FilePath]
hsSourceDirs' =
#if defined(Cabal36OrLater)
map getSymbolicPath . hsSourceDirs
#else
hsSourceDirs
#endif
#if defined(Cabal20OrLater)
doesPackageEnvExist :: GhcVersion -> FilePath -> IO Bool
doesPackageEnvExist ghcVersion projectDir = doesFileExist $ projectDir </> packageEnvFn
where
packageEnvFn =
-- The filename for package environments in MacOS uses the synonym "darwin
-- "instead of the "official" buildPlatform, "osx".
case buildOS of
OSX -> ".ghc.environment." ++ display buildArch ++ "-" ++ "darwin" ++ "-" ++ ghcVersion
_ -> ".ghc.environment." ++ display buildPlatform ++ "-" ++ ghcVersion
#endif
allowedOptions :: Set C8.ByteString
allowedOptions = S.fromList
[ "-w"
, "-fglasgow-exts"
, "-fpackage-trust"
, "-fhelpful-errors"
, "-F"
, "-cpp"
]
allowedOptionPrefixes :: [C8.ByteString]
allowedOptionPrefixes =
[ "-fwarn-"
, "-fno-warn-"
, "-W"
, "-fcontext-stack="
, "-firrefutable-tuples"
, "-D"
, "-U"
, "-I"
, "-fplugin="
, "-fplugin-opt="
, "-pgm"
, "-opt"
]
forbiddenOptions :: Set C8.ByteString
forbiddenOptions = S.fromList
[ "-Wmissing-home-modules"
, "-Werror=missing-home-modules"
]
isAllowedOption :: C8.ByteString -> Bool
isAllowedOption opt =
S.member opt allowedOptions || any (`C8.isPrefixOf` opt) allowedOptionPrefixes && S.notMember opt forbiddenOptions
dumpPackageDescription :: PackageDescription -> FilePath -> IO Sexp
dumpPackageDescription pkgDesc projectDir = do
(cabalDirs, cabalAutogen) <- getBuildDirectories Cabal pkgDesc projectDir
(stackDirs, stackAutogen) <- getBuildDirectories Stack pkgDesc projectDir
#if defined(Cabal20OrLater)
ghcVersion <- getGhcVersion
(cabalNewDirs, cabalNewAutogen) <- getBuildDirectories (CabalNew (package pkgDesc) ghcVersion) pkgDesc projectDir
packageEnvExists <- doesPackageEnvExist ghcVersion projectDir
let buildDirs = cabalDirs ++ stackDirs ++ cabalNewDirs
autogenDirs = cabalAutogen ++ stackAutogen ++ cabalNewAutogen
#else
let buildDirs = cabalDirs ++ stackDirs
autogenDirs = cabalAutogen ++ stackAutogen
#endif
let packageName = C8.pack $ unPackageName' $ pkgName $ package pkgDesc
return $
SList
[ cons (sym "build-directories") (ordNub (buildDirs :: [UnixFilepath]))
, cons (sym "source-directories") (sourceDirs :: [UnixFilepath])
, cons (sym "extensions") exts
, cons (sym "languages") langs
, cons (sym "dependencies") deps
, cons (sym "other-options") (cppOpts ++ ghcOpts)
, cons (sym "autogen-directories") (autogenDirs :: [UnixFilepath])
, cons (sym "should-include-version-header") [not ghcIncludesVersionMacro]
#if defined(Cabal20OrLater)
, cons (sym "package-env-exists") [packageEnvExists]
#endif
, cons (sym "package-name") [packageName]
, cons (sym "components") $ getComponents packageName pkgDesc
, cons (sym "include-directories") (includeDirs :: [UnixFilepath])
]
where
buildInfo :: [BuildInfo]
buildInfo = allBuildInfo pkgDesc
sourceDirs :: [UnixFilepath]
sourceDirs = ordNub (map mkUnixFilepath (getSourceDirectories buildInfo projectDir))
includeDirs :: [UnixFilepath]
includeDirs = ordNub (map mkUnixFilepath (getIncludeDirectories buildInfo projectDir))
exts :: [Extension]
exts = nub (concatMap usedExtensions buildInfo)
langs :: [Language]
langs = nub (concatMap allLanguages buildInfo)
thisPackage :: PackageName
thisPackage = pkgName (package pkgDesc)
deps :: [Dependency]
deps =
nub (filter ((/= thisPackage) . depPkgName') (buildDepends' pkgDesc))
-- The "cpp-options" configuration field.
cppOpts :: [C8.ByteString]
cppOpts =
ordNub (filter isAllowedOption (map C8.pack (concatMap cppOptions buildInfo)))
-- The "ghc-options" configuration field.
ghcOpts :: [C8.ByteString]
ghcOpts =
ordNub (filter isAllowedOption (map C8.pack (concatMap (hcOptions GHC) buildInfo)))
#if defined(Cabal20OrLater)
-- We don't care about the stack ghc compiler because we don't need it for
-- the stack checker
getGhcVersion :: IO String
getGhcVersion =
go "cabal"
["new-exec", "ghc", "--", "--numeric-version"]
(go "ghc" ["--numeric-version"] A.empty)
where
go :: String -> [String] -> IO String -> IO String
go comm opts cont = do
res <- try $ readProcessWithExitCode comm opts []
case res of
Left (_ :: SomeException) -> cont
Right (ExitSuccess, stdOut, _) -> return $ stripWhitespace stdOut
Right (ExitFailure _, _, _) -> cont
#endif
data ComponentType
= CTLibrary
| CTForeignLibrary
| CTExecutable
| CTTestSuite
| CTBenchmark
deriving (Eq, Ord, Show, Enum, Bounded)
componentTypePrefix :: ComponentType -> C8.ByteString
componentTypePrefix x = case x of
CTLibrary -> "lib"
CTForeignLibrary -> "flib"
CTExecutable -> "exe"
CTTestSuite -> "test"
CTBenchmark -> "bench"
instance ToSexp ComponentType where
toSexp = toSexp . componentTypePrefix
-- | Gather files and modules that constitute each component.
getComponents :: C8.ByteString -> PackageDescription -> [(ComponentType, C8.ByteString, Maybe C8.ByteString, [ModuleName])]
getComponents packageName pkgDescr =
[ (CTLibrary, name, Nothing, exposedModules lib ++ biMods bi)
| lib <- allLibraries' pkgDescr
, let bi = libBuildInfo lib
, let name = maybe packageName C8.pack $ libName' lib
] ++
#if defined(Cabal20OrLater)
[ (CTForeignLibrary, C8.pack (foreignLibName' flib), Nothing, biMods bi)
| flib <- foreignLibs pkgDescr
, let bi = foreignLibBuildInfo flib
] ++
#endif
[ (CTExecutable, C8.pack (exeName' exe), Just (C8.pack (modulePath exe)), biMods bi)
| exe <- executables pkgDescr
, let bi = buildInfo exe
] ++
[ (CTTestSuite, C8.pack (testName' tst), exeFile, maybeToList extraMod ++ biMods bi)
| tst <- testSuites pkgDescr
, let bi = testBuildInfo tst
, let (exeFile, extraMod) = case testInterface tst of
TestSuiteExeV10 _ path -> (Just (C8.pack path), Nothing)
TestSuiteLibV09 _ modName -> (Nothing, Just modName)
TestSuiteUnsupported{} -> (Nothing, Nothing)
]
#if defined(Cabal114OrMore)
++
[ (CTBenchmark, C8.pack (benchmarkName' tst), exeFile, biMods bi)
| tst <- benchmarks pkgDescr
, let bi = benchmarkBuildInfo tst
, let exeFile = case benchmarkInterface tst of
BenchmarkExeV10 _ path -> Just (C8.pack path)
BenchmarkUnsupported{} -> Nothing
]
#endif
where
#if defined(Cabal20OrLater)
biMods bi =
otherModules bi
#if defined(Cabal22OrLater)
++ virtualModules bi
#endif
++ autogenModules bi
#else
biMods = otherModules
#endif
getCabalConfiguration :: HPackExe -> ConfigurationFile -> IO Sexp
getCabalConfiguration hpackExe configFile = do
genericDesc <-
case configFile of
HPackFile path -> readHPackPkgDescr hpackExe path projectDir
CabalFile path -> readGenericPkgDescr path
case getConcretePackageDescription genericDesc of
Left e -> die' $ "Issue with package configuration\n" ++ show e
Right pkgDesc -> dumpPackageDescription pkgDesc projectDir
where
projectDir :: FilePath
projectDir = dropFileName $ configFilePath configFile
readHPackPkgDescr :: HPackExe -> FilePath -> FilePath -> IO GenericPackageDescription
readHPackPkgDescr exe configFile projectDir = do
(Nothing, Just out, Just err, procHandle) <- Process.createProcess p
cabalFileContents <- readCabalFileContentsFromHandle out
exitCode <- Process.waitForProcess procHandle
case exitCode of
ExitFailure{} -> do
err' <- hGetContents err
die' $ "Failed to obtain cabal configuration by running hpack on '" ++ configFile ++ "':\n" ++ err'
ExitSuccess ->
case parsePkgDescr "<generated by hpack>" cabalFileContents of
Left msgs ->
die' $ "Failed to parse cabal file produced by hpack from '" ++ configFile ++ "':\n" ++
unlines msgs
Right x -> return x
where
p = (Process.proc (unHPackExe exe) [configFile, "-"])
{ Process.std_in = Process.Inherit
, Process.std_out = Process.CreatePipe
, Process.std_err = Process.CreatePipe
, Process.cwd = Just projectDir
}
buildDepends' :: PackageDescription -> [Dependency]
buildDepends' =
#if defined(Cabal24OrLater)
allBuildDepends
#else
buildDepends
#endif
readGenericPkgDescr :: FilePath -> IO GenericPackageDescription
readGenericPkgDescr =
#if defined(Cabal20OrLater)
readGenericPackageDescription silent
#else
readPackageDescription silent
#endif
newtype CabalFileContents = CabalFileContents
{ unCabalFileContents ::
#if defined(Cabal22OrLater)
BS.ByteString
#else
String
#endif
}
readCabalFileContentsFromHandle :: Handle -> IO CabalFileContents
readCabalFileContentsFromHandle =
fmap CabalFileContents .
#if defined(Cabal22OrLater)
BS.hGetContents
#else
hGetContents
#endif
parsePkgDescr :: FilePath -> CabalFileContents -> Either [String] GenericPackageDescription
parsePkgDescr _fileName cabalFileContents =
#if defined(Cabal32OrLater)
case runParseResult $ parseGenericPackageDescription $ unCabalFileContents cabalFileContents of
(_warnings, res) ->
case res of
Left (_version, errs) -> Left $ map (showPError _fileName) $ toList errs
Right x -> return x
#elif defined(Cabal22OrLater)
case runParseResult $ parseGenericPackageDescription $ unCabalFileContents cabalFileContents of
(_warnings, res) ->
case res of
Left (_version, errs) -> Left $ map (showPError _fileName) errs
Right x -> return x
#elif defined(Cabal20OrLater)
case parseGenericPackageDescription $ unCabalFileContents cabalFileContents of
ParseFailed failure ->
let (_line, msg) = locatedErrorMsg failure
in Left [msg]
ParseOk _warnings x -> Right x
#else
case parsePackageDescription $ unCabalFileContents cabalFileContents of
ParseFailed failure ->
let (_line, msg) = locatedErrorMsg failure
in Left [msg]
ParseOk _warnings x -> Right x
#endif
getConcretePackageDescription
:: GenericPackageDescription
-> Either [Dependency] PackageDescription
getConcretePackageDescription genericDesc = do
#if defined(Cabal22OrLater)
let enabled :: ComponentRequestedSpec
enabled = ComponentRequestedSpec
{ testsRequested = True
, benchmarksRequested = True
}
fst A.<$> finalizePD
(mkFlagAssignment []) -- Flag assignment
enabled -- Enable all components
(const True) -- Whether given dependency is available
buildPlatform
buildCompilerId
[] -- Additional constraints
genericDesc
#elif defined(Cabal20OrLater)
let enabled :: ComponentRequestedSpec
enabled = ComponentRequestedSpec
{ testsRequested = True
, benchmarksRequested = True
}
fst A.<$> finalizePD
[] -- Flag assignment
enabled -- Enable all components
(const True) -- Whether given dependency is available
buildPlatform
buildCompilerId
[] -- Additional constraints
genericDesc
#elif Cabal114OrMore
-- This let block is eerily like one in Cabal.Distribution.Simple.Configure
let enableTest :: TestSuite -> TestSuite
enableTest t = t { testEnabled = True }
enableBenchmark :: Benchmark -> Benchmark
enableBenchmark bm = bm { benchmarkEnabled = True }
flaggedTests =
map (second (mapTreeData enableTest)) (condTestSuites genericDesc)
flaggedBenchmarks =
map
(second (mapTreeData enableBenchmark))
(condBenchmarks genericDesc)
genericDesc' =
genericDesc
{ condTestSuites = flaggedTests
, condBenchmarks = flaggedBenchmarks
}
fst A.<$> finalizePackageDescription
[]
(const True)
buildPlatform
buildCompilerId
[]
genericDesc'
#else
-- This let block is eerily like one in Cabal.Distribution.Simple.Configure
let enableTest :: TestSuite -> TestSuite
enableTest t = t { testEnabled = True }
flaggedTests =
map (second (mapTreeData enableTest)) (condTestSuites genericDesc)
genericDesc' =
genericDesc
{ condTestSuites = flaggedTests
}
fst A.<$> finalizePackageDescription
[]
(const True)
buildPlatform
buildCompilerId
[]
genericDesc'
#endif
componentsAutogenDirs :: FilePath -> [String] -> IO [FilePath]
#if defined(Cabal20OrLater)
componentsAutogenDirs buildDir componentNames =
filterM doesDirectoryExist $
map (\path -> buildDir </> path </> "autogen") componentNames
#else
componentsAutogenDirs _ _ = return []
#endif
#if defined(Cabal118OrLess)
buildCompilerId :: CompilerId
buildCompilerId = CompilerId buildCompilerFlavor compilerVersion
#else
buildCompilerId :: CompilerInfo
buildCompilerId = unknownCompilerInfo compId NoAbiTag
where
compId :: CompilerId
compId = CompilerId buildCompilerFlavor compVersion
# if defined(Cabal20OrLater)
compVersion :: CabalVersion.Version
compVersion = CabalVersion.mkVersion' compilerVersion
# else
compVersion :: Version
compVersion = compilerVersion
# endif
#endif
allLibraries' :: PackageDescription -> [Library]
allLibraries' =
#if defined(Cabal20OrLater)
allLibraries
#else
maybeToList . library
#endif
libName' :: Library -> Maybe String
libName' =
#if defined(Cabal30OrLater)
fmap unUnqualComponentName . libraryNameString . libName
#elif defined(Cabal20OrLater)
fmap unUnqualComponentName . libName
#else
const Nothing
#endif
exeName' :: Executable -> String
exeName' =
#if defined(Cabal20OrLater)
unUnqualComponentName . exeName
#else
exeName
#endif
#if defined(Cabal20OrLater)
foreignLibName' :: ForeignLib -> String
foreignLibName' =
unUnqualComponentName . foreignLibName
#endif
testName' :: TestSuite -> String
testName' =
#if defined(Cabal20OrLater)
unUnqualComponentName . testName
#else
testName
#endif
#if defined(Cabal114OrMore)
benchmarkName' :: Benchmark -> FilePath
benchmarkName' =
# if defined(Cabal20OrLater)
unUnqualComponentName . benchmarkName
# else
benchmarkName
# endif
#endif
getExeNames :: PackageDescription -> [String]
getExeNames =
map exeName' . executables
getForeignLibNames :: PackageDescription -> [String]
getForeignLibNames =
#if defined(Cabal20OrLater)
map foreignLibName' . foreignLibs
#else
const []
#endif
getTestNames :: PackageDescription -> [String]
getTestNames =
map testName' . testSuites
getBenchmarkNames :: PackageDescription -> [String]
getBenchmarkNames =
#if defined(Cabal114OrMore)
map benchmarkName' . benchmarks
#else
const []
#endif
depPkgName' :: Dependency -> PackageName
depPkgName' =
#if defined(Cabal20OrLater)
depPkgName
#else
let f (Dependency x _) = x in f
#endif
unPackageName' :: PackageName -> String
unPackageName' =
#if defined(Cabal20OrLater)
unPackageName
#else
let f (PackageName x) = x in f
#endif
-- Textual representation of cabal version
cabalVersion' :: String
cabalVersion' =
#if defined(Cabal22OrLater)
prettyShow cabalVersion
#elif defined(Cabal20OrLater)
CabalVersion.showVersion cabalVersion
#else
showVersion cabalVersion
#endif
ghcIncludesVersionMacro :: Bool
ghcIncludesVersionMacro =
#if defined(GHC_INCLUDES_VERSION_MACRO)
True
#else
False
#endif
ordNub :: forall a. Ord a => [a] -> [a]
ordNub = go S.empty
where
go :: Set a -> [a] -> [a]
go _ [] = []
go !acc (x:xs)
| S.member x acc = go acc xs
| otherwise = x : go (S.insert x acc) xs
stripWhitespace :: String -> String
stripWhitespace = reverse . dropWhile isSpace . reverse . dropWhile isSpace
die' :: String -> IO a
die' msg = do
hPutStrLn stderr msg
exitFailure
#if !defined(HAVE_DATA_FUNCTOR_IDENTITY)
newtype Identity a = Identity { runIdentity :: a }
#endif
data ConfigurationFile =
CabalFile FilePath
| HPackFile FilePath
configFilePath :: ConfigurationFile -> FilePath
configFilePath (CabalFile path) = path
configFilePath (HPackFile path) = path