1 -- This is Distribution.Extra.Doctest module from cabal-doctest-1.0.4
2 -- This isn't technically a Custom-Setup script, but it /was/.
4 {-# LANGUAGE FlexibleInstances #-}
8 Copyright (c) 2017, Oleg Grenrus
12 Redistribution and use in source and binary forms, with or without
13 modification, are permitted provided that the following conditions are met:
15 * Redistributions of source code must retain the above copyright
16 notice, this list of conditions and the following disclaimer.
18 * Redistributions in binary form must reproduce the above
19 copyright notice, this list of conditions and the following
20 disclaimer in the documentation and/or other materials provided
21 with the distribution.
23 * Neither the name of Oleg Grenrus nor the names of other
24 contributors may be used to endorse or promote products derived
25 from this software without specific prior written permission.
27 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
28 "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
29 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
30 A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
31 OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
32 SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
33 LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
34 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
35 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
36 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
37 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
42 {-# LANGUAGE OverloadedStrings #-}
43 -- | The provided 'generateBuildModule' generates 'Build_doctests' module.
44 -- That module exports enough configuration, so your doctests could be simply
49 -- import Build_doctests (flags, pkgs, module_sources)
50 -- import Data.Foldable (traverse_)
51 -- import Test.Doctest (doctest)
55 -- traverse_ putStrLn args -- optionally print arguments
58 -- args = flags ++ pkgs ++ module_sources
61 -- To use this library in the @Setup.hs@, you should specify a @custom-setup@
62 -- section in the cabal file, for example:
68 -- cabal-doctest >= 1 && <1.1
71 -- /Note:/ you don't need to depend on @Cabal@ if you use only
72 -- 'defaultMainWithDoctests' in the @Setup.hs@.
74 module CabalDoctestSetup
(
75 defaultMainWithDoctests
,
76 defaultMainAutoconfWithDoctests
,
82 -- Hacky way to suppress few deprecation warnings.
83 #if MIN_VERSION_Cabal
(1,24,0)
84 #define InstalledPackageId UnitId
90 (modifyIORef
, newIORef
, readIORef
)
94 (mapMaybe, maybeToList)
97 import Distribution
.Package
98 (InstalledPackageId
, Package
(..))
99 import Distribution
.PackageDescription
100 (BuildInfo
(..), Executable
(..), GenericPackageDescription
,
101 Library
(..), PackageDescription
, TestSuite
(..))
102 import Distribution
.Simple
103 (UserHooks
(..), autoconfUserHooks
, defaultMainWithHooks
,
105 import Distribution
.Simple
.Compiler
106 (CompilerFlavor
(GHC
), CompilerId
(..), PackageDB
(..), compilerId
)
107 import Distribution
.Simple
.LocalBuildInfo
108 (ComponentLocalBuildInfo
(componentPackageDeps
), LocalBuildInfo
,
109 compiler
, withExeLBI
, withLibLBI
, withPackageDB
, withTestLBI
111 import Distribution
.Simple
.Setup
112 ( CommonSetupFlags
(..)
117 import Distribution
.Simple
.Utils
118 (createDirectoryIfMissingVerbose
, info
)
119 import Distribution
.Text
121 import Distribution
.Verbosity
122 import System
.FilePath
125 import qualified Data
.Foldable
as F
127 import qualified Data
.Traversable
as T
130 #if MIN_VERSION_Cabal
(1,25,0)
131 import Distribution
.Simple
.BuildPaths
132 (autogenComponentModulesDir
)
134 import Distribution
.Simple
.BuildPaths
138 #if MIN_VERSION_Cabal
(2,0,0)
139 import Distribution
.Types
.MungedPackageId
141 import Distribution
.Types
.UnqualComponentName
142 (unUnqualComponentName
)
145 import Distribution
.PackageDescription
147 import Distribution
.Types
.GenericPackageDescription
148 (GenericPackageDescription
(condTestSuites
))
150 import Distribution
.Version
155 import Distribution
.Package
159 #if MIN_VERSION_Cabal
(3,11,0)
160 import Distribution
.Utils
.Path
163 , makeRelativePathEx
)
164 import qualified Distribution
.Utils
.Path
as Cabal
166 import Distribution
.Simple
.Utils
168 #elif MIN_VERSION_Cabal
(3,0,0)
169 import Distribution
.Utils
.Path
171 import qualified Distribution
.Utils
.Path
as Cabal
173 import Distribution
.Simple
.Utils
176 import Distribution
.Simple
.Utils
180 #if MIN_VERSION_Cabal
(3,0,0)
181 import Distribution
.Types
.LibraryName
185 #if MIN_VERSION_directory
(1,2,2)
186 import System
.Directory
189 import System
.Directory
190 (getCurrentDirectory)
191 import System
.FilePath
194 makeAbsolute
:: FilePath -> IO FilePath
195 makeAbsolute p | isAbsolute p
= return p
197 cwd
<- getCurrentDirectory
201 findFile
' :: Verbosity
-> [FilePath] -> FilePath -> IO FilePath
202 #if MIN_VERSION_Cabal
(3,11,0)
203 findFile
' verbosity searchPath fileName
206 (fmap makeSymbolicPath searchPath
) (makeRelativePathEx fileName
)
207 #elif MIN_VERSION_Cabal
(3,0,0)
208 findFile
' verbosity searchPath fileName
209 = findFileEx verbosity searchPath fileName
211 findFile
' _verbosity searchPath fileName
212 = findFile searchPath fileName
215 #if !MIN_VERSION_Cabal
(2,0,0)
216 mkVersion
:: [Int] -> Version
217 mkVersion ds
= Version ds
[]
220 class CompatPath p
where
221 toFilePath
:: p
-> FilePath
222 instance CompatPath
FilePath where
224 #if MIN_VERSION_Cabal
(3,11,0)
225 instance CompatPath
(SymbolicPathX allowAbs from to
) where
226 toFilePath
= Cabal
.getSymbolicPath
227 #elif MIN_VERSION_Cabal
(3,5,0)
228 instance CompatPath
(SymbolicPath from to
) where
229 toFilePath
= Cabal
.getSymbolicPath
232 -------------------------------------------------------------------------------
234 -------------------------------------------------------------------------------
236 -- | A default main with doctests:
239 -- import Distribution.Extra.Doctest
240 -- (defaultMainWithDoctests)
243 -- main = defaultMainWithDoctests "doctests"
245 defaultMainWithDoctests
246 :: String -- ^ doctests test-suite name
248 defaultMainWithDoctests
= defaultMainWithHooks
. doctestsUserHooks
250 -- | Like 'defaultMainWithDoctests', for 'build-type: Configure' packages.
253 defaultMainAutoconfWithDoctests
254 :: String -- ^ doctests test-suite name
256 defaultMainAutoconfWithDoctests n
=
257 defaultMainWithHooks
(addDoctestsUserHook n autoconfUserHooks
)
259 -- | 'simpleUserHooks' with 'generateBuildModule' prepended to the 'buildHook'.
261 :: String -- ^ doctests test-suite name
263 doctestsUserHooks testsuiteName
=
264 addDoctestsUserHook testsuiteName simpleUserHooks
269 addDoctestsUserHook
:: String -> UserHooks
-> UserHooks
270 addDoctestsUserHook testsuiteName uh
= uh
271 { buildHook
= \pkg lbi hooks flags
-> do
272 generateBuildModule testsuiteName flags pkg lbi
273 buildHook uh pkg lbi hooks flags
274 -- We use confHook to add "Build_Doctests" to otherModules and autogenModules.
276 -- We cannot use HookedBuildInfo as it let's alter only the library and executables.
277 , confHook
= \(gpd
, hbi
) flags
->
278 confHook uh
(amendGPD testsuiteName gpd
, hbi
) flags
279 , haddockHook
= \pkg lbi hooks flags
-> do
280 generateBuildModule testsuiteName
(haddockToBuildFlags flags
) pkg lbi
281 haddockHook uh pkg lbi hooks flags
284 -- | Convert only flags used by 'generateBuildModule'.
285 haddockToBuildFlags
:: HaddockFlags
-> BuildFlags
286 haddockToBuildFlags f
=
287 #if MIN_VERSION_Cabal
(3,11,0)
289 { buildCommonFlags
= haddockCommonFlags f
}
292 { buildVerbosity
= haddockVerbosity f
293 , buildDistPref
= haddockDistPref f
297 data Name
= NameLib
(Maybe String) | NameExe
String deriving (Eq
, Show)
299 nameToString
:: Name
-> String
300 nameToString n
= case n
of
301 NameLib x
-> maybe "" (("_lib_" ++) . map fixchar
) x
302 NameExe x
-> "_exe_" ++ map fixchar x
305 -- https://github.com/haskell/cabal/blob/20de0bfea72145ba1c37e3f500cee5258cc18e51/Cabal/Distribution/Simple/Build/Macros.hs#L156-L158
307 -- Needed to fix component names with hyphens in them, as hyphens aren't
308 -- allowed in Haskell identifier names.
309 fixchar
:: Char -> Char
313 data Component
= Component Name
[String] [String] [String]
316 -- | Generate a build module for the test suite.
319 -- import Distribution.Simple
320 -- (defaultMainWithHooks, UserHooks(..), simpleUserHooks)
321 -- import Distribution.Extra.Doctest
322 -- (generateBuildModule)
325 -- main = defaultMainWithHooks simpleUserHooks
326 -- { buildHook = \pkg lbi hooks flags -> do
327 -- generateBuildModule "doctests" flags pkg lbi
328 -- buildHook simpleUserHooks pkg lbi hooks flags
332 :: String -- ^ doctests test-suite name
333 -> BuildFlags
-> PackageDescription
-> LocalBuildInfo
-> IO ()
334 generateBuildModule testSuiteName flags pkg lbi
= do
335 let verbosity
= fromFlag
(buildVerbosity flags
)
336 let distPref
= fromFlag
(buildDistPref flags
)
338 -- Package DBs & environments
339 let dbStack
= withPackageDB lbi
++ [ SpecificPackageDB
$ toFilePath distPref
</> "package.conf.inplace" ]
340 let dbFlags
= "-hide-all-packages" : packageDbArgs dbStack
342 | ghcCanBeToldToIgnorePkgEnvs
= [ "-package-env=-" ]
345 withTestLBI pkg lbi
$ \suite suitecfg
-> when (testName suite
== fromString testSuiteName
) $ do
346 let testAutogenDir
= toFilePath
$
347 #if MIN_VERSION_Cabal
(1,25,0)
348 autogenComponentModulesDir lbi suitecfg
350 autogenModulesDir lbi
353 createDirectoryIfMissingVerbose verbosity
True testAutogenDir
355 let buildDoctestsFile
= testAutogenDir
</> "Build_doctests.hs"
357 -- First, we create the autogen'd module Build_doctests.
358 -- Initially populate Build_doctests with a simple preamble.
359 info verbosity
$ "cabal-doctest: writing Build_doctests to " ++ buildDoctestsFile
360 writeFile buildDoctestsFile
$ unlines
361 [ "module Build_doctests where"
365 , "data Name = NameLib (Maybe String) | NameExe String deriving (Eq, Show)"
366 , "data Component = Component Name [String] [String] [String] deriving (Eq, Show)"
370 -- we cannot traverse, only traverse_
371 -- so we use IORef to collect components
372 componentsRef
<- newIORef
[]
374 let testBI
= testBuildInfo suite
376 -- TODO: `words` is not proper parser (no support for quotes)
377 let additionalFlags
= maybe [] words
378 $ lookup "x-doctest-options"
379 $ customFieldsBI testBI
381 let additionalModules
= maybe [] words
382 $ lookup "x-doctest-modules"
383 $ customFieldsBI testBI
385 let additionalDirs
' = maybe [] words
386 $ lookup "x-doctest-source-dirs"
387 $ customFieldsBI testBI
389 additionalDirs
<- mapM (fmap ("-i" ++) . makeAbsolute
) additionalDirs
'
391 -- Next, for each component (library or executable), we get to Build_doctests
392 -- the sets of flags needed to run doctest on that component.
393 let getBuildDoctests withCompLBI mbCompName compExposedModules compMainIs compBuildInfo
=
394 withCompLBI pkg lbi
$ \comp compCfg
-> do
395 let compBI
= compBuildInfo comp
398 let modules
= compExposedModules comp
++ otherModules compBI
399 -- it seems that doctest is happy to take in module names, not actual files!
400 let module_sources
= modules
402 -- We need the directory with the component's cabal_macros.h!
406 #if MIN_VERSION_Cabal
(1,25,0)
407 autogenComponentModulesDir lbi compCfg
409 autogenModulesDir lbi
412 -- Lib sources and includes
415 $ compAutogenDir
-- autogenerated files
416 : (toFilePath distPref
++ "/build") -- preprocessed files (.hsc -> .hs); "build" is hardcoded in Cabal.
417 : map toFilePath
(hsSourceDirs compBI
)
418 includeArgs
<- mapM (fmap ("-I"++) . makeAbsolute
. toFilePath
) $ includeDirs compBI
419 -- We clear all includes, so the CWD isn't used.
420 let iArgs
' = map ("-i"++) iArgsNoPrefix
421 iArgs
= "-i" : iArgs
'
423 -- default-extensions
424 let extensionArgs
= map (("-X"++) . display
) $ defaultExtensions compBI
426 -- CPP includes, i.e. include cabal_macros.h
427 let cppFlags
= map ("-optP"++) $
428 [ "-include", compAutogenDir
++ "/cabal_macros.h" ]
431 -- Unlike other modules, the main-is module of an executable is not
432 -- guaranteed to share a module name with its filepath name. That is,
433 -- even though the main-is module is named Main, its filepath might
434 -- actually be Something.hs. To account for this possibility, we simply
435 -- pass the full path to the main-is module instead.
436 mainIsPath
<- T
.traverse
(findFile
' verbosity iArgsNoPrefix
) (compMainIs comp
)
438 let all_sources
= map display module_sources
440 ++ maybeToList mainIsPath
442 let component
= Component
444 (formatDeps
$ testDeps compCfg suitecfg
)
457 -- modify IORef, append component
458 modifyIORef componentsRef
(\cs
-> cs
++ [component
])
460 -- For now, we only check for doctests in libraries and executables.
461 getBuildDoctests withLibLBI mbLibraryName exposedModules
(const Nothing
) libBuildInfo
462 getBuildDoctests withExeLBI
(NameExe
. executableName
) (const []) (Just
. toFilePath
. modulePath
) buildInfo
464 components
<- readIORef componentsRef
465 F
.for_ components
$ \(Component cmpName cmpPkgs cmpFlags cmpSources
) -> do
466 let compSuffix
= nameToString cmpName
467 pkgs_comp
= "pkgs" ++ compSuffix
468 flags_comp
= "flags" ++ compSuffix
469 module_sources_comp
= "module_sources" ++ compSuffix
471 -- write autogen'd file
472 appendFile buildDoctestsFile
$ unlines
473 [ -- -package-id etc. flags
474 pkgs_comp
++ " :: [String]"
475 , pkgs_comp
++ " = " ++ show cmpPkgs
477 , flags_comp
++ " :: [String]"
478 , flags_comp
++ " = " ++ show cmpFlags
480 , module_sources_comp
++ " :: [String]"
481 , module_sources_comp
++ " = " ++ show cmpSources
485 -- write enabled components, i.e. x-doctest-components
486 -- if none enabled, pick library
487 let enabledComponents
= maybe [NameLib Nothing
] (mapMaybe parseComponentName
. words)
488 $ lookup "x-doctest-components"
489 $ customFieldsBI testBI
492 filter (\(Component n _ _ _
) -> n `
elem` enabledComponents
) components
493 appendFile buildDoctestsFile
$ unlines
494 [ "-- " ++ show enabledComponents
495 , "components :: [Component]"
496 , "components = " ++ show components
'
500 parseComponentName
:: String -> Maybe Name
501 parseComponentName
"lib" = Just
(NameLib Nothing
)
502 parseComponentName
('l
' : 'i
' : 'b
' : ':' : x
) = Just
(NameLib
(Just x
))
503 parseComponentName
('e
' : 'x
' : 'e
' : ':' : x
) = Just
(NameExe x
)
504 parseComponentName _
= Nothing
506 -- we do this check in Setup, as then doctests don't need to depend on Cabal
507 isNewCompiler
= case compilerId
$ compiler lbi
of
508 CompilerId GHC v
-> v
>= mkVersion
[7,6]
511 ghcCanBeToldToIgnorePkgEnvs
:: Bool
512 ghcCanBeToldToIgnorePkgEnvs
= case compilerId
$ compiler lbi
of
513 CompilerId GHC v
-> v
>= mkVersion
[8,4,4]
516 formatDeps
= map formatOne
517 formatOne
(installedPkgId
, pkgId
)
518 -- The problem is how different cabal executables handle package databases
519 -- when doctests depend on the library
521 -- If the pkgId is current package, we don't output the full package-id
524 -- Because of MungedPackageId we compare display version of identifiers
525 -- not the identifiers themfselves.
526 | display
(packageId pkg
) == display pkgId
= "-package=" ++ display pkgId
527 |
otherwise = "-package-id=" ++ display installedPkgId
529 -- From Distribution.Simple.Program.GHC
530 packageDbArgs
:: [PackageDB
] -> [String]
531 packageDbArgs | isNewCompiler
= packageDbArgsDb
532 |
otherwise = packageDbArgsConf
534 -- GHC <7.6 uses '-package-conf' instead of '-package-db'.
535 packageDbArgsConf
:: [PackageDB
] -> [String]
536 packageDbArgsConf dbstack
= case dbstack
of
537 (GlobalPackageDB
:UserPackageDB
:dbs
) -> concatMap specific dbs
538 (GlobalPackageDB
:dbs
) -> ("-no-user-package-conf")
539 : concatMap specific dbs
542 specific
(SpecificPackageDB db
) = [ "-package-conf=" ++ db
]
544 ierror
= error $ "internal error: unexpected package db stack: "
547 -- GHC >= 7.6 uses the '-package-db' flag. See
548 -- https://ghc.haskell.org/trac/ghc/ticket/5977.
549 packageDbArgsDb
:: [PackageDB
] -> [String]
550 -- special cases to make arguments prettier in common scenarios
551 packageDbArgsDb dbstack
= case dbstack
of
552 (GlobalPackageDB
:UserPackageDB
:dbs
)
553 |
all isSpecific dbs
-> concatMap single dbs
554 (GlobalPackageDB
:dbs
)
555 |
all isSpecific dbs
-> "-no-user-package-db"
556 : concatMap single dbs
557 dbs
-> "-clear-package-db"
558 : concatMap single dbs
560 single
(SpecificPackageDB db
) = [ "-package-db=" ++ db
]
561 single GlobalPackageDB
= [ "-global-package-db" ]
562 single UserPackageDB
= [ "-user-package-db" ]
563 isSpecific
(SpecificPackageDB _
) = True
566 mbLibraryName
:: Library
-> Name
567 #if MIN_VERSION_Cabal
(3,0,0)
568 mbLibraryName
= NameLib
. fmap unUnqualComponentName
. libraryNameString
. libName
569 #elif MIN_VERSION_Cabal
(2,0,0)
570 -- Cabal-2.0 introduced internal libraries, which are named.
571 mbLibraryName
= NameLib
. fmap unUnqualComponentName
. libName
573 -- Before that, there was only ever at most one library per
574 -- .cabal file, which has no name.
575 mbLibraryName _
= NameLib Nothing
578 executableName
:: Executable
-> String
579 #if MIN_VERSION_Cabal
(2,0,0)
580 executableName
= unUnqualComponentName
. exeName
582 executableName
= exeName
585 -- | In compat settings it's better to omit the type-signature
586 testDeps
:: ComponentLocalBuildInfo
-> ComponentLocalBuildInfo
587 #if MIN_VERSION_Cabal
(2,0,0)
588 -> [(InstalledPackageId
, MungedPackageId
)]
590 -> [(InstalledPackageId
, PackageId
)]
592 testDeps xs ys
= nub $ componentPackageDeps xs
++ componentPackageDeps ys
595 :: String -- ^ doctests test-suite name
596 -> GenericPackageDescription
597 -> GenericPackageDescription
598 #if !(MIN_VERSION_Cabal
(2,0,0))
601 amendGPD testSuiteName gpd
= gpd
602 { condTestSuites
= map f
(condTestSuites gpd
)
606 | name
== fromString testSuiteName
= (name
, condTree
')
607 |
otherwise = (name
, condTree
)
610 testSuite
= condTreeData condTree
611 bi
= testBuildInfo testSuite
613 am
= autogenModules bi
615 -- Cons the module to both other-modules and autogen-modules.
616 -- At the moment, cabal-spec-2.0 and cabal-spec-2.2 don't have
617 -- "all autogen-modules are other-modules if they aren't exposed-modules"
618 -- rule. Hopefully cabal-spec-3.0 will have.
620 -- Note: we `nub`, because it's unclear if that's ok to have duplicate
621 -- modules in the lists.
625 mn
= fromString
"Build_doctests"
627 bi
' = bi
{ otherModules
= om
', autogenModules
= am
' }
628 testSuite
' = testSuite
{ testBuildInfo
= bi
' }
629 condTree
' = condTree
{ condTreeData
= testSuite
' }