1
1
{-# LANGUAGE CPP #-}
2
2
{-# LANGUAGE OverloadedStrings #-}
3
+ {-# LANGUAGE FlexibleInstances #-}
4
+ #if MIN_VERSION_Cabal(3,14,0)
5
+ {-# LANGUAGE DataKinds #-}
6
+ #endif
7
+ {-# LANGUAGE MultiParamTypeClasses #-}
8
+
3
9
-- | See cabal-doctest README for full-fledged recipes & caveats.
4
10
--
5
11
-- The provided 'generateBuildModule' generates a @Build_{suffix}@ module, with
@@ -67,25 +73,28 @@ import Distribution.Simple
67
73
(UserHooks (.. ), autoconfUserHooks , defaultMainWithHooks ,
68
74
simpleUserHooks )
69
75
import Distribution.Simple.Compiler
70
- (CompilerFlavor (GHC ), CompilerId (.. ), PackageDB ( .. ), compilerId )
76
+ (CompilerFlavor (GHC ), CompilerId (.. ), compilerId )
71
77
import Distribution.Simple.LocalBuildInfo
72
78
(ComponentLocalBuildInfo (componentPackageDeps ), LocalBuildInfo ,
73
79
compiler , withExeLBI , withLibLBI , withPackageDB , withTestLBI )
74
80
import Distribution.Simple.Setup
75
- (BuildFlags (buildDistPref , buildVerbosity ),
76
- HaddockFlags ( haddockDistPref , haddockVerbosity ), emptyBuildFlags ,
81
+ (BuildFlags (.. ),
82
+ emptyBuildFlags ,
77
83
fromFlag )
78
84
import Distribution.Simple.Utils
79
85
(createDirectoryIfMissingVerbose , info )
80
86
import Distribution.Text
81
87
(display )
82
- import System.FilePath
83
- ((</>) )
84
88
85
89
import qualified Data.Foldable as F
86
90
(for_ )
87
91
import qualified Data.Traversable as T
88
92
(traverse )
93
+ import qualified System.FilePath ((</>) )
94
+
95
+ #if MIN_VERSION_base(4,11,0)
96
+ import Data.Functor ((<&>) )
97
+ #endif
89
98
90
99
#if MIN_VERSION_Cabal(1,25,0)
91
100
import Distribution.Simple.BuildPaths
@@ -134,6 +143,24 @@ import Distribution.Utils.Path
134
143
(getSymbolicPath )
135
144
#endif
136
145
146
+ #if MIN_VERSION_Cabal(3,14,0)
147
+ -- https://github.com/haskell/cabal/issues/10559
148
+ import Distribution.Simple.Compiler
149
+ (PackageDB , PackageDBX (GlobalPackageDB , UserPackageDB , SpecificPackageDB ))
150
+ import Distribution.Simple.LocalBuildInfo
151
+ (absoluteWorkingDirLBI , interpretSymbolicPathLBI )
152
+ import Distribution.Simple.Setup
153
+ (HaddockFlags , haddockCommonFlags )
154
+ import Distribution.Utils.Path
155
+ (FileOrDir (.. ), SymbolicPath , interpretSymbolicPathAbsolute , makeRelativePathEx , makeSymbolicPath )
156
+ import qualified Distribution.Utils.Path as SymPath ((</>) )
157
+ #else
158
+ import Distribution.Simple.Compiler
159
+ (PackageDB (GlobalPackageDB , UserPackageDB , SpecificPackageDB ))
160
+ import Distribution.Simple.Setup
161
+ (HaddockFlags (haddockDistPref , haddockVerbosity ))
162
+ #endif
163
+
137
164
#if MIN_VERSION_directory(1,2,2)
138
165
import System.Directory
139
166
(makeAbsolute )
@@ -142,7 +169,42 @@ import System.Directory
142
169
(getCurrentDirectory )
143
170
import System.FilePath
144
171
(isAbsolute )
172
+ #endif
173
+
174
+ {- HLINT ignore "Use fewer imports" -}
175
+
176
+ -------------------------------------------------------------------------------
177
+ -- Compat
178
+ -------------------------------------------------------------------------------
179
+
180
+ #if !MIN_VERSION_base(4,11,0)
181
+ (<&>) :: Functor f => f a -> (a -> b ) -> f b
182
+ (<&>) = flip fmap
183
+ infixl 1 <&>
184
+ #endif
185
+
186
+ class CompatSymPath p q where
187
+ (</>) :: p -> FilePath -> q
188
+ infixr 5 </>
189
+ instance CompatSymPath FilePath FilePath where
190
+ (</>) = (System.FilePath. </>)
191
+ #if MIN_VERSION_Cabal(3,14,0)
192
+ instance CompatSymPath (SymbolicPath allowAbs ('Dir loc1 ))
193
+ (SymbolicPath allowAbs ('Dir loc2 )) where
194
+ dir </> name = dir SymPath. </> makeRelativePathEx name
195
+ #endif
196
+
197
+ #if MIN_VERSION_Cabal(3,14,0)
198
+ unsymbolizePath = getSymbolicPath
199
+ #else
200
+ makeSymbolicPath :: FilePath -> FilePath
201
+ makeSymbolicPath = id
202
+ unsymbolizePath :: FilePath -> FilePath
203
+ unsymbolizePath = id
204
+ #endif
205
+
145
206
207
+ #if !MIN_VERSION_directory(1,2,2)
146
208
makeAbsolute :: FilePath -> IO FilePath
147
209
makeAbsolute p | isAbsolute p = return p
148
210
| otherwise = do
@@ -216,10 +278,16 @@ addDoctestsUserHook testsuiteName uh = uh
216
278
217
279
-- | Convert only flags used by 'generateBuildModule'.
218
280
haddockToBuildFlags :: HaddockFlags -> BuildFlags
219
- haddockToBuildFlags f = emptyBuildFlags
281
+ haddockToBuildFlags f =
282
+ #if MIN_VERSION_Cabal(3,14,0)
283
+ emptyBuildFlags
284
+ { buildCommonFlags = haddockCommonFlags f }
285
+ #else
286
+ emptyBuildFlags
220
287
{ buildVerbosity = haddockVerbosity f
221
288
, buildDistPref = haddockDistPref f
222
289
}
290
+ #endif
223
291
224
292
data Name = NameLib (Maybe String ) | NameExe String deriving (Eq , Show )
225
293
@@ -270,12 +338,16 @@ generateBuildModule testSuiteName flags pkg lbi = do
270
338
| otherwise = []
271
339
272
340
withTestLBI pkg lbi $ \ suite suitecfg -> when (testName suite == fromString testSuiteName) $ do
273
- #if MIN_VERSION_Cabal(1,25,0)
341
+
342
+ -- Locate autogen dir, to put our output into.
343
+ #if MIN_VERSION_Cabal(3,14,0)
344
+ let testAutogenDir = interpretSymbolicPathLBI lbi
345
+ $ autogenComponentModulesDir lbi suitecfg
346
+ #elif MIN_VERSION_Cabal(1,25,0)
274
347
let testAutogenDir = autogenComponentModulesDir lbi suitecfg
275
348
#else
276
349
let testAutogenDir = autogenModulesDir lbi
277
350
#endif
278
-
279
351
createDirectoryIfMissingVerbose verbosity True testAutogenDir
280
352
281
353
let buildDoctestsFile = testAutogenDir </> " Build_doctests.hs"
@@ -326,23 +398,35 @@ generateBuildModule testSuiteName flags pkg lbi = do
326
398
let module_sources = modules
327
399
328
400
-- We need the directory with the component's cabal_macros.h!
329
- #if MIN_VERSION_Cabal(1,25,0)
401
+ #if MIN_VERSION_Cabal(3,14,0)
402
+ let compAutogenDir = interpretSymbolicPathLBI lbi
403
+ $ autogenComponentModulesDir lbi compCfg
404
+ #elif MIN_VERSION_Cabal(1,25,0)
330
405
let compAutogenDir = autogenComponentModulesDir lbi compCfg
331
406
#else
332
407
let compAutogenDir = autogenModulesDir lbi
333
408
#endif
334
409
335
410
-- Lib sources and includes
336
- iArgsNoPrefix
337
- <- mapM makeAbsolute
338
- $ compAutogenDir -- autogenerated files
339
- : (distPref ++ " /build" ) -- preprocessed files (.hsc -> .hs); "build" is hardcoded in Cabal.
340
- #if MIN_VERSION_Cabal(3,5,0)
341
- : map getSymbolicPath (hsSourceDirs compBI)
411
+ let iArgsSymbolic =
412
+ makeSymbolicPath compAutogenDir -- autogen dir
413
+ -- preprocessed files (.hsc -> .hs); "build" is hardcoded in Cabal.
414
+ : (distPref </> " build" )
415
+ #if MIN_VERSION_Cabal(3,14,0)
416
+ : hsSourceDirs compBI
417
+ #elif MIN_VERSION_Cabal(3,5,0)
418
+ : (hsSourceDirs compBI <&> getSymbolicPath)
342
419
#else
343
- : hsSourceDirs compBI
420
+ : hsSourceDirs compBI
344
421
#endif
422
+ #if MIN_VERSION_Cabal(3,14,0)
423
+ pkgWorkdir <- absoluteWorkingDirLBI lbi
424
+ let iArgsNoPrefix = iArgsSymbolic <&> interpretSymbolicPathAbsolute pkgWorkdir
425
+ let includeArgs = includeDirs compBI <&> (" -I" ++ ) . interpretSymbolicPathAbsolute pkgWorkdir
426
+ #else
427
+ iArgsNoPrefix <- mapM makeAbsolute iArgsSymbolic
345
428
includeArgs <- mapM (fmap (" -I" ++ ) . makeAbsolute) $ includeDirs compBI
429
+ #endif
346
430
-- We clear all includes, so the CWD isn't used.
347
431
let iArgs' = map (" -i" ++ ) iArgsNoPrefix
348
432
iArgs = " -i" : iArgs'
@@ -360,11 +444,11 @@ generateBuildModule testSuiteName flags pkg lbi = do
360
444
-- even though the main-is module is named Main, its filepath might
361
445
-- actually be Something.hs. To account for this possibility, we simply
362
446
-- pass the full path to the main-is module instead.
363
- mainIsPath <- T. traverse (findFileEx verbosity iArgsNoPrefix ) (compMainIs comp)
447
+ mainIsPath <- T. traverse (findFileEx verbosity iArgsSymbolic ) (compMainIs comp)
364
448
365
449
let all_sources = map display module_sources
366
450
++ additionalModules
367
- ++ maybeToList mainIsPath
451
+ ++ maybeToList ( mainIsPath <&> unsymbolizePath)
368
452
369
453
let component = Component
370
454
(mbCompName comp)
@@ -462,11 +546,11 @@ generateBuildModule testSuiteName flags pkg lbi = do
462
546
packageDbArgsConf :: [PackageDB ] -> [String ]
463
547
packageDbArgsConf dbstack = case dbstack of
464
548
(GlobalPackageDB : UserPackageDB : dbs) -> concatMap specific dbs
465
- (GlobalPackageDB : dbs) -> ( " -no-user-package-conf" )
549
+ (GlobalPackageDB : dbs) -> " -no-user-package-conf"
466
550
: concatMap specific dbs
467
551
_ -> ierror
468
552
where
469
- specific (SpecificPackageDB db) = [ " -package-conf=" ++ db ]
553
+ specific (SpecificPackageDB db) = [ " -package-conf=" ++ unsymbolizePath db ]
470
554
specific _ = ierror
471
555
ierror = error $ " internal error: unexpected package db stack: "
472
556
++ show dbstack
@@ -484,7 +568,7 @@ generateBuildModule testSuiteName flags pkg lbi = do
484
568
dbs -> " -clear-package-db"
485
569
: concatMap single dbs
486
570
where
487
- single (SpecificPackageDB db) = [ " -package-db=" ++ db ]
571
+ single (SpecificPackageDB db) = [ " -package-db=" ++ unsymbolizePath db ]
488
572
single GlobalPackageDB = [ " -global-package-db" ]
489
573
single UserPackageDB = [ " -user-package-db" ]
490
574
isSpecific (SpecificPackageDB _) = True
0 commit comments