Skip to content

Commit 7ef0e45

Browse files
committed
fix: support cabal 3.14
Adaptations to API breakages in Cabal 3.14.0.0, discussed in haskell/cabal#10559 Resolves #85.
1 parent 76e3743 commit 7ef0e45

File tree

5 files changed

+117
-22
lines changed

5 files changed

+117
-22
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@ dist/
22
dist-newstyle/
33
.stack-work/
44
.ghc.environment.*
5+
cabal.project.local

cabal-doctest.cabal

+1-1
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ library
5353
-- In any case, revisions may set tighter bounds afterwards, if exceptional
5454
-- circumstances would warrant that.
5555
base >=4.9 && <5
56-
, Cabal >=1.10 && <3.14
56+
, Cabal >=1.10 && <3.16
5757
, directory >=1.3 && <2
5858
, filepath >=1.4 && <2
5959

cabal.project

+4
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,7 @@ packages: . simple-example multiple-components-example
1313
-- allow-newer: *:ghc
1414
-- allow-newer: *:base
1515
-- allow-newer: *:Cabal
16+
17+
tests: true
18+
19+
-- constraints: Cabal==3.14.*

changelog.md

+6
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
# 1.0.11 -- unreleased
2+
3+
* Support Cabal 3.14.0.0. [cabal-doctest#85][].
4+
5+
[cabal-doctest#85]: https://github.com/ulidtko/cabal-doctest/issues/85
6+
17
# 1.0.10 -- 2024-06-26
28

39
* Maintainership hand-over. See [cabal-doctest#79][].

src/Distribution/Extra/Doctest.hs

+105-21
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
{-# LANGUAGE CPP #-}
22
{-# LANGUAGE OverloadedStrings #-}
3+
{-# LANGUAGE FlexibleInstances #-}
4+
#if MIN_VERSION_Cabal(3,14,0)
5+
{-# LANGUAGE DataKinds #-}
6+
#endif
7+
{-# LANGUAGE MultiParamTypeClasses #-}
8+
39
-- | See cabal-doctest README for full-fledged recipes & caveats.
410
--
511
-- The provided 'generateBuildModule' generates a @Build_{suffix}@ module, with
@@ -67,25 +73,28 @@ import Distribution.Simple
6773
(UserHooks (..), autoconfUserHooks, defaultMainWithHooks,
6874
simpleUserHooks)
6975
import Distribution.Simple.Compiler
70-
(CompilerFlavor (GHC), CompilerId (..), PackageDB (..), compilerId)
76+
(CompilerFlavor (GHC), CompilerId (..), compilerId)
7177
import Distribution.Simple.LocalBuildInfo
7278
(ComponentLocalBuildInfo (componentPackageDeps), LocalBuildInfo,
7379
compiler, withExeLBI, withLibLBI, withPackageDB, withTestLBI)
7480
import Distribution.Simple.Setup
75-
(BuildFlags (buildDistPref, buildVerbosity),
76-
HaddockFlags (haddockDistPref, haddockVerbosity), emptyBuildFlags,
81+
(BuildFlags (..),
82+
emptyBuildFlags,
7783
fromFlag)
7884
import Distribution.Simple.Utils
7985
(createDirectoryIfMissingVerbose, info)
8086
import Distribution.Text
8187
(display)
82-
import System.FilePath
83-
((</>))
8488

8589
import qualified Data.Foldable as F
8690
(for_)
8791
import qualified Data.Traversable as T
8892
(traverse)
93+
import qualified System.FilePath ((</>))
94+
95+
#if MIN_VERSION_base(4,11,0)
96+
import Data.Functor ((<&>))
97+
#endif
8998

9099
#if MIN_VERSION_Cabal(1,25,0)
91100
import Distribution.Simple.BuildPaths
@@ -134,6 +143,24 @@ import Distribution.Utils.Path
134143
(getSymbolicPath)
135144
#endif
136145

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+
137164
#if MIN_VERSION_directory(1,2,2)
138165
import System.Directory
139166
(makeAbsolute)
@@ -142,7 +169,42 @@ import System.Directory
142169
(getCurrentDirectory)
143170
import System.FilePath
144171
(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+
145206

207+
#if !MIN_VERSION_directory(1,2,2)
146208
makeAbsolute :: FilePath -> IO FilePath
147209
makeAbsolute p | isAbsolute p = return p
148210
| otherwise = do
@@ -216,10 +278,16 @@ addDoctestsUserHook testsuiteName uh = uh
216278

217279
-- | Convert only flags used by 'generateBuildModule'.
218280
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
220287
{ buildVerbosity = haddockVerbosity f
221288
, buildDistPref = haddockDistPref f
222289
}
290+
#endif
223291

224292
data Name = NameLib (Maybe String) | NameExe String deriving (Eq, Show)
225293

@@ -270,12 +338,16 @@ generateBuildModule testSuiteName flags pkg lbi = do
270338
| otherwise = []
271339

272340
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)
274347
let testAutogenDir = autogenComponentModulesDir lbi suitecfg
275348
#else
276349
let testAutogenDir = autogenModulesDir lbi
277350
#endif
278-
279351
createDirectoryIfMissingVerbose verbosity True testAutogenDir
280352

281353
let buildDoctestsFile = testAutogenDir </> "Build_doctests.hs"
@@ -326,23 +398,35 @@ generateBuildModule testSuiteName flags pkg lbi = do
326398
let module_sources = modules
327399

328400
-- 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)
330405
let compAutogenDir = autogenComponentModulesDir lbi compCfg
331406
#else
332407
let compAutogenDir = autogenModulesDir lbi
333408
#endif
334409

335410
-- 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)
342419
#else
343-
: hsSourceDirs compBI
420+
: hsSourceDirs compBI
344421
#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
345428
includeArgs <- mapM (fmap ("-I"++) . makeAbsolute) $ includeDirs compBI
429+
#endif
346430
-- We clear all includes, so the CWD isn't used.
347431
let iArgs' = map ("-i"++) iArgsNoPrefix
348432
iArgs = "-i" : iArgs'
@@ -360,11 +444,11 @@ generateBuildModule testSuiteName flags pkg lbi = do
360444
-- even though the main-is module is named Main, its filepath might
361445
-- actually be Something.hs. To account for this possibility, we simply
362446
-- 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)
364448

365449
let all_sources = map display module_sources
366450
++ additionalModules
367-
++ maybeToList mainIsPath
451+
++ maybeToList (mainIsPath <&> unsymbolizePath)
368452

369453
let component = Component
370454
(mbCompName comp)
@@ -462,11 +546,11 @@ generateBuildModule testSuiteName flags pkg lbi = do
462546
packageDbArgsConf :: [PackageDB] -> [String]
463547
packageDbArgsConf dbstack = case dbstack of
464548
(GlobalPackageDB:UserPackageDB:dbs) -> concatMap specific dbs
465-
(GlobalPackageDB:dbs) -> ("-no-user-package-conf")
549+
(GlobalPackageDB:dbs) -> "-no-user-package-conf"
466550
: concatMap specific dbs
467551
_ -> ierror
468552
where
469-
specific (SpecificPackageDB db) = [ "-package-conf=" ++ db ]
553+
specific (SpecificPackageDB db) = [ "-package-conf=" ++ unsymbolizePath db ]
470554
specific _ = ierror
471555
ierror = error $ "internal error: unexpected package db stack: "
472556
++ show dbstack
@@ -484,7 +568,7 @@ generateBuildModule testSuiteName flags pkg lbi = do
484568
dbs -> "-clear-package-db"
485569
: concatMap single dbs
486570
where
487-
single (SpecificPackageDB db) = [ "-package-db=" ++ db ]
571+
single (SpecificPackageDB db) = [ "-package-db=" ++ unsymbolizePath db ]
488572
single GlobalPackageDB = [ "-global-package-db" ]
489573
single UserPackageDB = [ "-user-package-db" ]
490574
isSpecific (SpecificPackageDB _) = True

0 commit comments

Comments
 (0)