{-# LANGUAGE CPP               #-}
{-# LANGUAGE OverloadedStrings #-}
-- | The provided 'generateBuildModule' generates 'Build_doctests' module.
-- That module exports enough configuration, so your doctests could be simply
--
-- @
-- module Main where
--
-- import Build_doctests (flags, pkgs, module_sources)
-- import Data.Foldable (traverse_)
-- import Test.Doctest (doctest)
--
-- main :: IO ()
-- main = do
--     traverse_ putStrLn args -- optionally print arguments
--     doctest args
--   where
--     args = flags ++ pkgs ++ module_sources
-- @
--
-- To use this library in the @Setup.hs@, you should specify a @custom-setup@
-- section in the cabal file, for example:
--
-- @
-- custom-setup
--  setup-depends:
--    base >= 4 && <5,
--    cabal-doctest >= 1 && <1.1
-- @
--
-- /Note:/ you don't need to depend on @Cabal@  if you use only
-- 'defaultMainWithDoctests' in the @Setup.hs@.
--
module Distribution.Extra.Doctest (
    defaultMainWithDoctests,
    defaultMainAutoconfWithDoctests,
    addDoctestsUserHook,
    doctestsUserHooks,
    generateBuildModule,
    ) where

-- Hacky way to suppress few deprecation warnings.
#if MIN_VERSION_Cabal(1,24,0)
#define InstalledPackageId UnitId
#endif

import Control.Monad
       (when)
import Data.IORef
       (modifyIORef, newIORef, readIORef)
import Data.List
       (nub)
import Data.Maybe
       (mapMaybe, maybeToList)
import Data.String
       (fromString)
import Distribution.Package
       (InstalledPackageId, Package (..))
import Distribution.PackageDescription
       (BuildInfo (..), Executable (..), GenericPackageDescription,
       Library (..), PackageDescription, TestSuite (..))
import Distribution.Simple
       (UserHooks (..), autoconfUserHooks, defaultMainWithHooks,
       simpleUserHooks)
import Distribution.Simple.Compiler
       (CompilerFlavor (GHC), CompilerId (..), PackageDB (..), compilerId)
import Distribution.Simple.LocalBuildInfo
       (ComponentLocalBuildInfo (componentPackageDeps), LocalBuildInfo,
       compiler, withExeLBI, withLibLBI, withPackageDB, withTestLBI)
import Distribution.Simple.Setup
       (BuildFlags (buildDistPref, buildVerbosity),
       HaddockFlags (haddockDistPref, haddockVerbosity), emptyBuildFlags,
       fromFlag)
import Distribution.Simple.Utils
       (createDirectoryIfMissingVerbose, info)
import Distribution.Text
       (display)
import System.FilePath
       ((</>))

import qualified Data.Foldable    as F
                 (for_)
import qualified Data.Traversable as T
                 (traverse)

#if MIN_VERSION_Cabal(1,25,0)
import Distribution.Simple.BuildPaths
       (autogenComponentModulesDir)
#else
import Distribution.Simple.BuildPaths
       (autogenModulesDir)
#endif

#if MIN_VERSION_Cabal(2,0,0)
import Distribution.Types.MungedPackageId
       (MungedPackageId)
import Distribution.Types.UnqualComponentName
       (unUnqualComponentName)

-- For amendGPD
import Distribution.PackageDescription
       (CondTree (..))
import Distribution.Types.GenericPackageDescription
       (GenericPackageDescription (condTestSuites))

import Distribution.Version
       (mkVersion)
#else
import Data.Version
       (Version (..))
import Distribution.Package
       (PackageId)
#endif

#if MIN_VERSION_Cabal(3,0,0)
import Distribution.Simple.Utils
       (findFileEx)
#else
import Distribution.Simple.Utils
       (findFile)
#endif

#if MIN_VERSION_Cabal(3,0,0)
import Distribution.Types.LibraryName
       (libraryNameString)
#endif

#if MIN_VERSION_Cabal(3,6,0)
import Distribution.Utils.Path
       (getSymbolicPath)
#endif

#if MIN_VERSION_directory(1,2,2)
import System.Directory
       (makeAbsolute)
#else
import System.Directory
       (getCurrentDirectory)
import System.FilePath
       (isAbsolute)

makeAbsolute :: FilePath -> IO FilePath
makeAbsolute p | isAbsolute p = return p
               | otherwise    = do
    cwd <- getCurrentDirectory
    return $ cwd </> p
#endif

#if !MIN_VERSION_Cabal(3,0,0)
findFileEx :: verbosity -> [FilePath] -> FilePath -> IO FilePath
findFileEx _ = findFile
#endif

#if !MIN_VERSION_Cabal(2,0,0)
mkVersion :: [Int] -> Version
mkVersion ds = Version ds []
#endif

-------------------------------------------------------------------------------
-- Mains
-------------------------------------------------------------------------------

-- | A default main with doctests:
--
-- @
-- import Distribution.Extra.Doctest
--        (defaultMainWithDoctests)
--
-- main :: IO ()
-- main = defaultMainWithDoctests "doctests"
-- @
defaultMainWithDoctests
    :: String  -- ^ doctests test-suite name
    -> IO ()
defaultMainWithDoctests :: String -> IO ()
defaultMainWithDoctests = UserHooks -> IO ()
defaultMainWithHooks (UserHooks -> IO ()) -> (String -> UserHooks) -> String -> IO ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> UserHooks
doctestsUserHooks

-- | Like 'defaultMainWithDoctests', for 'build-type: Configure' packages.
--
-- @since 1.0.2
defaultMainAutoconfWithDoctests
    :: String  -- ^ doctests test-suite name
    -> IO ()
defaultMainAutoconfWithDoctests :: String -> IO ()
defaultMainAutoconfWithDoctests String
n =
    UserHooks -> IO ()
defaultMainWithHooks (String -> UserHooks -> UserHooks
addDoctestsUserHook String
n UserHooks
autoconfUserHooks)

-- | 'simpleUserHooks' with 'generateBuildModule' prepended to the 'buildHook'.
doctestsUserHooks
    :: String  -- ^ doctests test-suite name
    -> UserHooks
doctestsUserHooks :: String -> UserHooks
doctestsUserHooks String
testsuiteName =
    String -> UserHooks -> UserHooks
addDoctestsUserHook String
testsuiteName UserHooks
simpleUserHooks

-- |
--
-- @since 1.0.2
addDoctestsUserHook :: String -> UserHooks -> UserHooks
addDoctestsUserHook :: String -> UserHooks -> UserHooks
addDoctestsUserHook String
testsuiteName UserHooks
uh = UserHooks
uh
    { buildHook :: PackageDescription
-> LocalBuildInfo -> UserHooks -> BuildFlags -> IO ()
buildHook = \PackageDescription
pkg LocalBuildInfo
lbi UserHooks
hooks BuildFlags
flags -> do
        String
-> BuildFlags -> PackageDescription -> LocalBuildInfo -> IO ()
generateBuildModule String
testsuiteName BuildFlags
flags PackageDescription
pkg LocalBuildInfo
lbi
        UserHooks
-> PackageDescription
-> LocalBuildInfo
-> UserHooks
-> BuildFlags
-> IO ()
buildHook UserHooks
uh PackageDescription
pkg LocalBuildInfo
lbi UserHooks
hooks BuildFlags
flags
    -- We use confHook to add "Build_Doctests" to otherModules and autogenModules.
    --
    -- We cannot use HookedBuildInfo as it let's alter only the library and executables.
    , confHook :: (GenericPackageDescription, HookedBuildInfo)
-> ConfigFlags -> IO LocalBuildInfo
confHook = \(GenericPackageDescription
gpd, HookedBuildInfo
hbi) ConfigFlags
flags ->
        UserHooks
-> (GenericPackageDescription, HookedBuildInfo)
-> ConfigFlags
-> IO LocalBuildInfo
confHook UserHooks
uh (String -> GenericPackageDescription -> GenericPackageDescription
amendGPD String
testsuiteName GenericPackageDescription
gpd, HookedBuildInfo
hbi) ConfigFlags
flags
    , haddockHook :: PackageDescription
-> LocalBuildInfo -> UserHooks -> HaddockFlags -> IO ()
haddockHook = \PackageDescription
pkg LocalBuildInfo
lbi UserHooks
hooks HaddockFlags
flags -> do
        String
-> BuildFlags -> PackageDescription -> LocalBuildInfo -> IO ()
generateBuildModule String
testsuiteName (HaddockFlags -> BuildFlags
haddockToBuildFlags HaddockFlags
flags) PackageDescription
pkg LocalBuildInfo
lbi
        UserHooks
-> PackageDescription
-> LocalBuildInfo
-> UserHooks
-> HaddockFlags
-> IO ()
haddockHook UserHooks
uh PackageDescription
pkg LocalBuildInfo
lbi UserHooks
hooks HaddockFlags
flags
    }

-- | Convert only flags used by 'generateBuildModule'.
haddockToBuildFlags :: HaddockFlags -> BuildFlags
haddockToBuildFlags :: HaddockFlags -> BuildFlags
haddockToBuildFlags HaddockFlags
f = BuildFlags
emptyBuildFlags
    { buildVerbosity :: Flag Verbosity
buildVerbosity = HaddockFlags -> Flag Verbosity
haddockVerbosity HaddockFlags
f
    , buildDistPref :: Flag String
buildDistPref  = HaddockFlags -> Flag String
haddockDistPref HaddockFlags
f
    }

data Name = NameLib (Maybe String) | NameExe String deriving (Name -> Name -> Bool
(Name -> Name -> Bool) -> (Name -> Name -> Bool) -> Eq Name
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: Name -> Name -> Bool
$c/= :: Name -> Name -> Bool
== :: Name -> Name -> Bool
$c== :: Name -> Name -> Bool
Eq, Int -> Name -> ShowS
[Name] -> ShowS
Name -> String
(Int -> Name -> ShowS)
-> (Name -> String) -> ([Name] -> ShowS) -> Show Name
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [Name] -> ShowS
$cshowList :: [Name] -> ShowS
show :: Name -> String
$cshow :: Name -> String
showsPrec :: Int -> Name -> ShowS
$cshowsPrec :: Int -> Name -> ShowS
Show)

nameToString :: Name -> String
nameToString :: Name -> String
nameToString Name
n = case Name
n of
  NameLib Maybe String
x -> String -> ShowS -> Maybe String -> String
forall b a. b -> (a -> b) -> Maybe a -> b
maybe String
"" ((String
"_lib_" String -> ShowS
forall a. [a] -> [a] -> [a]
++) ShowS -> ShowS -> ShowS
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Char -> Char) -> ShowS
forall a b. (a -> b) -> [a] -> [b]
map Char -> Char
fixchar) Maybe String
x
  NameExe String
x -> String
"_exe_" String -> ShowS
forall a. [a] -> [a] -> [a]
++ (Char -> Char) -> ShowS
forall a b. (a -> b) -> [a] -> [b]
map Char -> Char
fixchar String
x
  where
    -- Taken from Cabal:
    -- https://github.com/haskell/cabal/blob/20de0bfea72145ba1c37e3f500cee5258cc18e51/Cabal/Distribution/Simple/Build/Macros.hs#L156-L158
    --
    -- Needed to fix component names with hyphens in them, as hyphens aren't
    -- allowed in Haskell identifier names.
    fixchar :: Char -> Char
    fixchar :: Char -> Char
fixchar Char
'-' = Char
'_'
    fixchar Char
c   = Char
c

data Component = Component Name [String] [String] [String]
  deriving Int -> Component -> ShowS
[Component] -> ShowS
Component -> String
(Int -> Component -> ShowS)
-> (Component -> String)
-> ([Component] -> ShowS)
-> Show Component
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [Component] -> ShowS
$cshowList :: [Component] -> ShowS
show :: Component -> String
$cshow :: Component -> String
showsPrec :: Int -> Component -> ShowS
$cshowsPrec :: Int -> Component -> ShowS
Show

-- | Generate a build module for the test suite.
--
-- @
-- import Distribution.Simple
--        (defaultMainWithHooks, UserHooks(..), simpleUserHooks)
-- import Distribution.Extra.Doctest
--        (generateBuildModule)
--
-- main :: IO ()
-- main = defaultMainWithHooks simpleUserHooks
--     { buildHook = \pkg lbi hooks flags -> do
--         generateBuildModule "doctests" flags pkg lbi
--         buildHook simpleUserHooks pkg lbi hooks flags
--     }
-- @
generateBuildModule
    :: String -- ^ doctests test-suite name
    -> BuildFlags -> PackageDescription -> LocalBuildInfo -> IO ()
generateBuildModule :: String
-> BuildFlags -> PackageDescription -> LocalBuildInfo -> IO ()
generateBuildModule String
testSuiteName BuildFlags
flags PackageDescription
pkg LocalBuildInfo
lbi = do
  let verbosity :: Verbosity
verbosity = Flag Verbosity -> Verbosity
forall a. WithCallStack (Flag a -> a)
fromFlag (BuildFlags -> Flag Verbosity
buildVerbosity BuildFlags
flags)
  let distPref :: String
distPref = Flag String -> String
forall a. WithCallStack (Flag a -> a)
fromFlag (BuildFlags -> Flag String
buildDistPref BuildFlags
flags)

  -- Package DBs & environments
  let dbStack :: [PackageDB]
dbStack = LocalBuildInfo -> [PackageDB]
withPackageDB LocalBuildInfo
lbi [PackageDB] -> [PackageDB] -> [PackageDB]
forall a. [a] -> [a] -> [a]
++ [ String -> PackageDB
SpecificPackageDB (String -> PackageDB) -> String -> PackageDB
forall a b. (a -> b) -> a -> b
$ String
distPref String -> ShowS
</> String
"package.conf.inplace" ]
  let dbFlags :: [String]
dbFlags = String
"-hide-all-packages" String -> [String] -> [String]
forall a. a -> [a] -> [a]
: [PackageDB] -> [String]
packageDbArgs [PackageDB]
dbStack
  let envFlags :: [String]
envFlags
        | Bool
ghcCanBeToldToIgnorePkgEnvs = [ String
"-package-env=-" ]
        | Bool
otherwise = []

  PackageDescription
-> LocalBuildInfo
-> (TestSuite -> ComponentLocalBuildInfo -> IO ())
-> IO ()
withTestLBI PackageDescription
pkg LocalBuildInfo
lbi ((TestSuite -> ComponentLocalBuildInfo -> IO ()) -> IO ())
-> (TestSuite -> ComponentLocalBuildInfo -> IO ()) -> IO ()
forall a b. (a -> b) -> a -> b
$ \TestSuite
suite ComponentLocalBuildInfo
suitecfg -> Bool -> IO () -> IO ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (TestSuite -> UnqualComponentName
testName TestSuite
suite UnqualComponentName -> UnqualComponentName -> Bool
forall a. Eq a => a -> a -> Bool
== String -> UnqualComponentName
forall a. IsString a => String -> a
fromString String
testSuiteName) (IO () -> IO ()) -> IO () -> IO ()
forall a b. (a -> b) -> a -> b
$ do
#if MIN_VERSION_Cabal(1,25,0)
    let testAutogenDir :: String
testAutogenDir = LocalBuildInfo -> ComponentLocalBuildInfo -> String
autogenComponentModulesDir LocalBuildInfo
lbi ComponentLocalBuildInfo
suitecfg
#else
    let testAutogenDir = autogenModulesDir lbi
#endif

    Verbosity -> Bool -> String -> IO ()
createDirectoryIfMissingVerbose Verbosity
verbosity Bool
True String
testAutogenDir

    let buildDoctestsFile :: String
buildDoctestsFile = String
testAutogenDir String -> ShowS
</> String
"Build_doctests.hs"

    -- First, we create the autogen'd module Build_doctests.
    -- Initially populate Build_doctests with a simple preamble.
    Verbosity -> String -> IO ()
info Verbosity
verbosity (String -> IO ()) -> String -> IO ()
forall a b. (a -> b) -> a -> b
$ String
"cabal-doctest: writing Build_doctests to " String -> ShowS
forall a. [a] -> [a] -> [a]
++ String
buildDoctestsFile
    String -> String -> IO ()
writeFile String
buildDoctestsFile (String -> IO ()) -> String -> IO ()
forall a b. (a -> b) -> a -> b
$ [String] -> String
unlines
      [ String
"module Build_doctests where"
      , String
""
      , String
"import Prelude"
      , String
""
      , String
"data Name = NameLib (Maybe String) | NameExe String deriving (Eq, Show)"
      , String
"data Component = Component Name [String] [String] [String] deriving (Eq, Show)"
      , String
""
      ]

    -- we cannot traverse, only traverse_
    -- so we use IORef to collect components
    IORef [Component]
componentsRef <- [Component] -> IO (IORef [Component])
forall a. a -> IO (IORef a)
newIORef []

    let testBI :: BuildInfo
testBI = TestSuite -> BuildInfo
testBuildInfo TestSuite
suite

    -- TODO: `words` is not proper parser (no support for quotes)
    let additionalFlags :: [String]
additionalFlags = [String] -> (String -> [String]) -> Maybe String -> [String]
forall b a. b -> (a -> b) -> Maybe a -> b
maybe [] String -> [String]
words
          (Maybe String -> [String]) -> Maybe String -> [String]
forall a b. (a -> b) -> a -> b
$ String -> [(String, String)] -> Maybe String
forall a b. Eq a => a -> [(a, b)] -> Maybe b
lookup String
"x-doctest-options"
          ([(String, String)] -> Maybe String)
-> [(String, String)] -> Maybe String
forall a b. (a -> b) -> a -> b
$ BuildInfo -> [(String, String)]
customFieldsBI BuildInfo
testBI

    let additionalModules :: [String]
additionalModules = [String] -> (String -> [String]) -> Maybe String -> [String]
forall b a. b -> (a -> b) -> Maybe a -> b
maybe [] String -> [String]
words
          (Maybe String -> [String]) -> Maybe String -> [String]
forall a b. (a -> b) -> a -> b
$ String -> [(String, String)] -> Maybe String
forall a b. Eq a => a -> [(a, b)] -> Maybe b
lookup String
"x-doctest-modules"
          ([(String, String)] -> Maybe String)
-> [(String, String)] -> Maybe String
forall a b. (a -> b) -> a -> b
$ BuildInfo -> [(String, String)]
customFieldsBI BuildInfo
testBI

    let additionalDirs' :: [String]
additionalDirs' = [String] -> (String -> [String]) -> Maybe String -> [String]
forall b a. b -> (a -> b) -> Maybe a -> b
maybe [] String -> [String]
words
          (Maybe String -> [String]) -> Maybe String -> [String]
forall a b. (a -> b) -> a -> b
$ String -> [(String, String)] -> Maybe String
forall a b. Eq a => a -> [(a, b)] -> Maybe b
lookup String
"x-doctest-source-dirs"
          ([(String, String)] -> Maybe String)
-> [(String, String)] -> Maybe String
forall a b. (a -> b) -> a -> b
$ BuildInfo -> [(String, String)]
customFieldsBI BuildInfo
testBI

    [String]
additionalDirs <- (String -> IO String) -> [String] -> IO [String]
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
mapM (ShowS -> IO String -> IO String
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (String
"-i" String -> ShowS
forall a. [a] -> [a] -> [a]
++) (IO String -> IO String)
-> (String -> IO String) -> String -> IO String
forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> IO String
makeAbsolute) [String]
additionalDirs'

    -- Next, for each component (library or executable), we get to Build_doctests
    -- the sets of flags needed to run doctest on that component.
    let getBuildDoctests :: (PackageDescription
 -> LocalBuildInfo -> (t -> ComponentLocalBuildInfo -> IO ()) -> b)
-> (t -> Name)
-> (t -> [ModuleName])
-> (t -> Maybe String)
-> (t -> BuildInfo)
-> b
getBuildDoctests PackageDescription
-> LocalBuildInfo -> (t -> ComponentLocalBuildInfo -> IO ()) -> b
withCompLBI t -> Name
mbCompName t -> [ModuleName]
compExposedModules t -> Maybe String
compMainIs t -> BuildInfo
compBuildInfo =
         PackageDescription
-> LocalBuildInfo -> (t -> ComponentLocalBuildInfo -> IO ()) -> b
withCompLBI PackageDescription
pkg LocalBuildInfo
lbi ((t -> ComponentLocalBuildInfo -> IO ()) -> b)
-> (t -> ComponentLocalBuildInfo -> IO ()) -> b
forall a b. (a -> b) -> a -> b
$ \t
comp ComponentLocalBuildInfo
compCfg -> do
           let compBI :: BuildInfo
compBI = t -> BuildInfo
compBuildInfo t
comp

           -- modules
           let modules :: [ModuleName]
modules = t -> [ModuleName]
compExposedModules t
comp [ModuleName] -> [ModuleName] -> [ModuleName]
forall a. [a] -> [a] -> [a]
++ BuildInfo -> [ModuleName]
otherModules BuildInfo
compBI
           -- it seems that doctest is happy to take in module names, not actual files!
           let module_sources :: [ModuleName]
module_sources = [ModuleName]
modules

           -- We need the directory with the component's cabal_macros.h!
#if MIN_VERSION_Cabal(1,25,0)
           let compAutogenDir :: String
compAutogenDir = LocalBuildInfo -> ComponentLocalBuildInfo -> String
autogenComponentModulesDir LocalBuildInfo
lbi ComponentLocalBuildInfo
compCfg
#else
           let compAutogenDir = autogenModulesDir lbi
#endif

           -- Lib sources and includes
           [String]
iArgsNoPrefix
              <- (String -> IO String) -> [String] -> IO [String]
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
mapM String -> IO String
makeAbsolute
               ([String] -> IO [String]) -> [String] -> IO [String]
forall a b. (a -> b) -> a -> b
$ String
compAutogenDir           -- autogenerated files
               String -> [String] -> [String]
forall a. a -> [a] -> [a]
: (String
distPref String -> ShowS
forall a. [a] -> [a] -> [a]
++ String
"/build")   -- preprocessed files (.hsc -> .hs); "build" is hardcoded in Cabal.
#if MIN_VERSION_Cabal(3,6,0)
               String -> [String] -> [String]
forall a. a -> [a] -> [a]
: (SymbolicPath PackageDir SourceDir -> String)
-> [SymbolicPath PackageDir SourceDir] -> [String]
forall a b. (a -> b) -> [a] -> [b]
map SymbolicPath PackageDir SourceDir -> String
forall from to. SymbolicPath from to -> String
getSymbolicPath (BuildInfo -> [SymbolicPath PackageDir SourceDir]
hsSourceDirs BuildInfo
compBI)
#else
               : hsSourceDirs compBI
#endif
           [String]
includeArgs <- (String -> IO String) -> [String] -> IO [String]
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
mapM (ShowS -> IO String -> IO String
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (String
"-I"String -> ShowS
forall a. [a] -> [a] -> [a]
++) (IO String -> IO String)
-> (String -> IO String) -> String -> IO String
forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> IO String
makeAbsolute) ([String] -> IO [String]) -> [String] -> IO [String]
forall a b. (a -> b) -> a -> b
$ BuildInfo -> [String]
includeDirs BuildInfo
compBI
           -- We clear all includes, so the CWD isn't used.
           let iArgs' :: [String]
iArgs' = ShowS -> [String] -> [String]
forall a b. (a -> b) -> [a] -> [b]
map (String
"-i"String -> ShowS
forall a. [a] -> [a] -> [a]
++) [String]
iArgsNoPrefix
               iArgs :: [String]
iArgs  = String
"-i" String -> [String] -> [String]
forall a. a -> [a] -> [a]
: [String]
iArgs'

           -- default-extensions
           let extensionArgs :: [String]
extensionArgs = (Extension -> String) -> [Extension] -> [String]
forall a b. (a -> b) -> [a] -> [b]
map ((String
"-X"String -> ShowS
forall a. [a] -> [a] -> [a]
++) ShowS -> (Extension -> String) -> Extension -> String
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Extension -> String
forall a. Pretty a => a -> String
display) ([Extension] -> [String]) -> [Extension] -> [String]
forall a b. (a -> b) -> a -> b
$ BuildInfo -> [Extension]
defaultExtensions BuildInfo
compBI

           -- CPP includes, i.e. include cabal_macros.h
           let cppFlags :: [String]
cppFlags = ShowS -> [String] -> [String]
forall a b. (a -> b) -> [a] -> [b]
map (String
"-optP"String -> ShowS
forall a. [a] -> [a] -> [a]
++) ([String] -> [String]) -> [String] -> [String]
forall a b. (a -> b) -> a -> b
$
                   [ String
"-include", String
compAutogenDir String -> ShowS
forall a. [a] -> [a] -> [a]
++ String
"/cabal_macros.h" ]
                   [String] -> [String] -> [String]
forall a. [a] -> [a] -> [a]
++ BuildInfo -> [String]
cppOptions BuildInfo
compBI

           -- Unlike other modules, the main-is module of an executable is not
           -- guaranteed to share a module name with its filepath name. That is,
           -- even though the main-is module is named Main, its filepath might
           -- actually be Something.hs. To account for this possibility, we simply
           -- pass the full path to the main-is module instead.
           Maybe String
mainIsPath <- (String -> IO String) -> Maybe String -> IO (Maybe String)
forall (t :: * -> *) (f :: * -> *) a b.
(Traversable t, Applicative f) =>
(a -> f b) -> t a -> f (t b)
T.traverse (Verbosity -> [String] -> String -> IO String
findFileEx Verbosity
verbosity [String]
iArgsNoPrefix) (t -> Maybe String
compMainIs t
comp)

           let all_sources :: [String]
all_sources = (ModuleName -> String) -> [ModuleName] -> [String]
forall a b. (a -> b) -> [a] -> [b]
map ModuleName -> String
forall a. Pretty a => a -> String
display [ModuleName]
module_sources
                             [String] -> [String] -> [String]
forall a. [a] -> [a] -> [a]
++ [String]
additionalModules
                             [String] -> [String] -> [String]
forall a. [a] -> [a] -> [a]
++ Maybe String -> [String]
forall a. Maybe a -> [a]
maybeToList Maybe String
mainIsPath

           let component :: Component
component = Name -> [String] -> [String] -> [String] -> Component
Component
                (t -> Name
mbCompName t
comp)
                ([(UnitId, MungedPackageId)] -> [String]
formatDeps ([(UnitId, MungedPackageId)] -> [String])
-> [(UnitId, MungedPackageId)] -> [String]
forall a b. (a -> b) -> a -> b
$ ComponentLocalBuildInfo
-> ComponentLocalBuildInfo -> [(UnitId, MungedPackageId)]
testDeps ComponentLocalBuildInfo
compCfg ComponentLocalBuildInfo
suitecfg)
                ([[String]] -> [String]
forall (t :: * -> *) a. Foldable t => t [a] -> [a]
concat
                  [ [String]
iArgs
                  , [String]
additionalDirs
                  , [String]
includeArgs
                  , [String]
envFlags
                  , [String]
dbFlags
                  , [String]
cppFlags
                  , [String]
extensionArgs
                  , [String]
additionalFlags
                  ])
                [String]
all_sources

           -- modify IORef, append component
           IORef [Component] -> ([Component] -> [Component]) -> IO ()
forall a. IORef a -> (a -> a) -> IO ()
modifyIORef IORef [Component]
componentsRef (\[Component]
cs -> [Component]
cs [Component] -> [Component] -> [Component]
forall a. [a] -> [a] -> [a]
++ [Component
component])

    -- For now, we only check for doctests in libraries and executables.
    (PackageDescription
 -> LocalBuildInfo
 -> (Library -> ComponentLocalBuildInfo -> IO ())
 -> IO ())
-> (Library -> Name)
-> (Library -> [ModuleName])
-> (Library -> Maybe String)
-> (Library -> BuildInfo)
-> IO ()
forall {t} {b}.
(PackageDescription
 -> LocalBuildInfo -> (t -> ComponentLocalBuildInfo -> IO ()) -> b)
-> (t -> Name)
-> (t -> [ModuleName])
-> (t -> Maybe String)
-> (t -> BuildInfo)
-> b
getBuildDoctests PackageDescription
-> LocalBuildInfo
-> (Library -> ComponentLocalBuildInfo -> IO ())
-> IO ()
withLibLBI Library -> Name
mbLibraryName           Library -> [ModuleName]
exposedModules (Maybe String -> Library -> Maybe String
forall a b. a -> b -> a
const Maybe String
forall a. Maybe a
Nothing)     Library -> BuildInfo
libBuildInfo
    (PackageDescription
 -> LocalBuildInfo
 -> (Executable -> ComponentLocalBuildInfo -> IO ())
 -> IO ())
-> (Executable -> Name)
-> (Executable -> [ModuleName])
-> (Executable -> Maybe String)
-> (Executable -> BuildInfo)
-> IO ()
forall {t} {b}.
(PackageDescription
 -> LocalBuildInfo -> (t -> ComponentLocalBuildInfo -> IO ()) -> b)
-> (t -> Name)
-> (t -> [ModuleName])
-> (t -> Maybe String)
-> (t -> BuildInfo)
-> b
getBuildDoctests PackageDescription
-> LocalBuildInfo
-> (Executable -> ComponentLocalBuildInfo -> IO ())
-> IO ()
withExeLBI (String -> Name
NameExe (String -> Name) -> (Executable -> String) -> Executable -> Name
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Executable -> String
executableName) ([ModuleName] -> Executable -> [ModuleName]
forall a b. a -> b -> a
const [])     (String -> Maybe String
forall a. a -> Maybe a
Just (String -> Maybe String)
-> (Executable -> String) -> Executable -> Maybe String
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Executable -> String
modulePath) Executable -> BuildInfo
buildInfo

    [Component]
components <- IORef [Component] -> IO [Component]
forall a. IORef a -> IO a
readIORef IORef [Component]
componentsRef
    [Component] -> (Component -> IO ()) -> IO ()
forall (t :: * -> *) (f :: * -> *) a b.
(Foldable t, Applicative f) =>
t a -> (a -> f b) -> f ()
F.for_ [Component]
components ((Component -> IO ()) -> IO ()) -> (Component -> IO ()) -> IO ()
forall a b. (a -> b) -> a -> b
$ \(Component Name
cmpName [String]
cmpPkgs [String]
cmpFlags [String]
cmpSources) -> do
       let compSuffix :: String
compSuffix          = Name -> String
nameToString Name
cmpName
           pkgs_comp :: String
pkgs_comp           = String
"pkgs"           String -> ShowS
forall a. [a] -> [a] -> [a]
++ String
compSuffix
           flags_comp :: String
flags_comp          = String
"flags"          String -> ShowS
forall a. [a] -> [a] -> [a]
++ String
compSuffix
           module_sources_comp :: String
module_sources_comp = String
"module_sources" String -> ShowS
forall a. [a] -> [a] -> [a]
++ String
compSuffix

       -- write autogen'd file
       String -> String -> IO ()
appendFile String
buildDoctestsFile (String -> IO ()) -> String -> IO ()
forall a b. (a -> b) -> a -> b
$ [String] -> String
unlines
         [ -- -package-id etc. flags
           String
pkgs_comp String -> ShowS
forall a. [a] -> [a] -> [a]
++ String
" :: [String]"
         , String
pkgs_comp String -> ShowS
forall a. [a] -> [a] -> [a]
++ String
" = " String -> ShowS
forall a. [a] -> [a] -> [a]
++ [String] -> String
forall a. Show a => a -> String
show [String]
cmpPkgs
         , String
""
         , String
flags_comp String -> ShowS
forall a. [a] -> [a] -> [a]
++ String
" :: [String]"
         , String
flags_comp String -> ShowS
forall a. [a] -> [a] -> [a]
++ String
" = " String -> ShowS
forall a. [a] -> [a] -> [a]
++ [String] -> String
forall a. Show a => a -> String
show [String]
cmpFlags
         , String
""
         , String
module_sources_comp String -> ShowS
forall a. [a] -> [a] -> [a]
++ String
" :: [String]"
         , String
module_sources_comp String -> ShowS
forall a. [a] -> [a] -> [a]
++ String
" = " String -> ShowS
forall a. [a] -> [a] -> [a]
++ [String] -> String
forall a. Show a => a -> String
show [String]
cmpSources
         , String
""
         ]

    -- write enabled components, i.e. x-doctest-components
    -- if none enabled, pick library
    let enabledComponents :: [Name]
enabledComponents = [Name] -> (String -> [Name]) -> Maybe String -> [Name]
forall b a. b -> (a -> b) -> Maybe a -> b
maybe [Maybe String -> Name
NameLib Maybe String
forall a. Maybe a
Nothing] ((String -> Maybe Name) -> [String] -> [Name]
forall a b. (a -> Maybe b) -> [a] -> [b]
mapMaybe String -> Maybe Name
parseComponentName ([String] -> [Name]) -> (String -> [String]) -> String -> [Name]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> [String]
words)
           (Maybe String -> [Name]) -> Maybe String -> [Name]
forall a b. (a -> b) -> a -> b
$ String -> [(String, String)] -> Maybe String
forall a b. Eq a => a -> [(a, b)] -> Maybe b
lookup String
"x-doctest-components"
           ([(String, String)] -> Maybe String)
-> [(String, String)] -> Maybe String
forall a b. (a -> b) -> a -> b
$ BuildInfo -> [(String, String)]
customFieldsBI BuildInfo
testBI

    let components' :: [Component]
components' =
         (Component -> Bool) -> [Component] -> [Component]
forall a. (a -> Bool) -> [a] -> [a]
filter (\(Component Name
n [String]
_ [String]
_ [String]
_) -> Name
n Name -> [Name] -> Bool
forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`elem` [Name]
enabledComponents) [Component]
components
    String -> String -> IO ()
appendFile String
buildDoctestsFile (String -> IO ()) -> String -> IO ()
forall a b. (a -> b) -> a -> b
$ [String] -> String
unlines
      [ String
"-- " String -> ShowS
forall a. [a] -> [a] -> [a]
++ [Name] -> String
forall a. Show a => a -> String
show [Name]
enabledComponents
      , String
"components :: [Component]"
      , String
"components = " String -> ShowS
forall a. [a] -> [a] -> [a]
++ [Component] -> String
forall a. Show a => a -> String
show [Component]
components'
      ]

  where
    parseComponentName :: String -> Maybe Name
    parseComponentName :: String -> Maybe Name
parseComponentName String
"lib"                       = Name -> Maybe Name
forall a. a -> Maybe a
Just (Maybe String -> Name
NameLib Maybe String
forall a. Maybe a
Nothing)
    parseComponentName (Char
'l' : Char
'i' : Char
'b' : Char
':' : String
x) = Name -> Maybe Name
forall a. a -> Maybe a
Just (Maybe String -> Name
NameLib (String -> Maybe String
forall a. a -> Maybe a
Just String
x))
    parseComponentName (Char
'e' : Char
'x' : Char
'e' : Char
':' : String
x) = Name -> Maybe Name
forall a. a -> Maybe a
Just (String -> Name
NameExe String
x)
    parseComponentName String
_ = Maybe Name
forall a. Maybe a
Nothing

    -- we do this check in Setup, as then doctests don't need to depend on Cabal
    isNewCompiler :: Bool
isNewCompiler = case Compiler -> CompilerId
compilerId (Compiler -> CompilerId) -> Compiler -> CompilerId
forall a b. (a -> b) -> a -> b
$ LocalBuildInfo -> Compiler
compiler LocalBuildInfo
lbi of
      CompilerId CompilerFlavor
GHC Version
v -> Version
v Version -> Version -> Bool
forall a. Ord a => a -> a -> Bool
>= [Int] -> Version
mkVersion [Int
7,Int
6]
      CompilerId
_                -> Bool
False

    ghcCanBeToldToIgnorePkgEnvs :: Bool
    ghcCanBeToldToIgnorePkgEnvs :: Bool
ghcCanBeToldToIgnorePkgEnvs = case Compiler -> CompilerId
compilerId (Compiler -> CompilerId) -> Compiler -> CompilerId
forall a b. (a -> b) -> a -> b
$ LocalBuildInfo -> Compiler
compiler LocalBuildInfo
lbi of
      CompilerId CompilerFlavor
GHC Version
v -> Version
v Version -> Version -> Bool
forall a. Ord a => a -> a -> Bool
>= [Int] -> Version
mkVersion [Int
8,Int
4,Int
4]
      CompilerId
_                -> Bool
False

    formatDeps :: [(UnitId, MungedPackageId)] -> [String]
formatDeps = ((UnitId, MungedPackageId) -> String)
-> [(UnitId, MungedPackageId)] -> [String]
forall a b. (a -> b) -> [a] -> [b]
map (UnitId, MungedPackageId) -> String
forall {a} {a}. (Pretty a, Pretty a) => (a, a) -> String
formatOne
    formatOne :: (a, a) -> String
formatOne (a
installedPkgId, a
pkgId)
      -- The problem is how different cabal executables handle package databases
      -- when doctests depend on the library
      --
      -- If the pkgId is current package, we don't output the full package-id
      -- but only the name
      --
      -- Because of MungedPackageId we compare display version of identifiers
      -- not the identifiers themfselves.
      | PackageIdentifier -> String
forall a. Pretty a => a -> String
display (PackageDescription -> PackageIdentifier
forall pkg. Package pkg => pkg -> PackageIdentifier
packageId PackageDescription
pkg) String -> String -> Bool
forall a. Eq a => a -> a -> Bool
== a -> String
forall a. Pretty a => a -> String
display a
pkgId = String
"-package=" String -> ShowS
forall a. [a] -> [a] -> [a]
++ a -> String
forall a. Pretty a => a -> String
display a
pkgId
      | Bool
otherwise              = String
"-package-id=" String -> ShowS
forall a. [a] -> [a] -> [a]
++ a -> String
forall a. Pretty a => a -> String
display a
installedPkgId

    -- From Distribution.Simple.Program.GHC
    packageDbArgs :: [PackageDB] -> [String]
    packageDbArgs :: [PackageDB] -> [String]
packageDbArgs | Bool
isNewCompiler = [PackageDB] -> [String]
packageDbArgsDb
                  | Bool
otherwise     = [PackageDB] -> [String]
packageDbArgsConf

    -- GHC <7.6 uses '-package-conf' instead of '-package-db'.
    packageDbArgsConf :: [PackageDB] -> [String]
    packageDbArgsConf :: [PackageDB] -> [String]
packageDbArgsConf [PackageDB]
dbstack = case [PackageDB]
dbstack of
      (PackageDB
GlobalPackageDB:PackageDB
UserPackageDB:[PackageDB]
dbs) -> (PackageDB -> [String]) -> [PackageDB] -> [String]
forall (t :: * -> *) a b. Foldable t => (a -> [b]) -> t a -> [b]
concatMap PackageDB -> [String]
specific [PackageDB]
dbs
      (PackageDB
GlobalPackageDB:[PackageDB]
dbs)               -> (String
"-no-user-package-conf")
                                           String -> [String] -> [String]
forall a. a -> [a] -> [a]
: (PackageDB -> [String]) -> [PackageDB] -> [String]
forall (t :: * -> *) a b. Foldable t => (a -> [b]) -> t a -> [b]
concatMap PackageDB -> [String]
specific [PackageDB]
dbs
      [PackageDB]
_ -> [String]
forall {a}. a
ierror
      where
        specific :: PackageDB -> [String]
specific (SpecificPackageDB String
db) = [ String
"-package-conf=" String -> ShowS
forall a. [a] -> [a] -> [a]
++ String
db ]
        specific PackageDB
_                      = [String]
forall {a}. a
ierror
        ierror :: a
ierror = String -> a
forall a. HasCallStack => String -> a
error (String -> a) -> String -> a
forall a b. (a -> b) -> a -> b
$ String
"internal error: unexpected package db stack: "
                      String -> ShowS
forall a. [a] -> [a] -> [a]
++ [PackageDB] -> String
forall a. Show a => a -> String
show [PackageDB]
dbstack

    -- GHC >= 7.6 uses the '-package-db' flag. See
    -- https://ghc.haskell.org/trac/ghc/ticket/5977.
    packageDbArgsDb :: [PackageDB] -> [String]
    -- special cases to make arguments prettier in common scenarios
    packageDbArgsDb :: [PackageDB] -> [String]
packageDbArgsDb [PackageDB]
dbstack = case [PackageDB]
dbstack of
      (PackageDB
GlobalPackageDB:PackageDB
UserPackageDB:[PackageDB]
dbs)
        | (PackageDB -> Bool) -> [PackageDB] -> Bool
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
all PackageDB -> Bool
isSpecific [PackageDB]
dbs              -> (PackageDB -> [String]) -> [PackageDB] -> [String]
forall (t :: * -> *) a b. Foldable t => (a -> [b]) -> t a -> [b]
concatMap PackageDB -> [String]
single [PackageDB]
dbs
      (PackageDB
GlobalPackageDB:[PackageDB]
dbs)
        | (PackageDB -> Bool) -> [PackageDB] -> Bool
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
all PackageDB -> Bool
isSpecific [PackageDB]
dbs              -> String
"-no-user-package-db"
                                           String -> [String] -> [String]
forall a. a -> [a] -> [a]
: (PackageDB -> [String]) -> [PackageDB] -> [String]
forall (t :: * -> *) a b. Foldable t => (a -> [b]) -> t a -> [b]
concatMap PackageDB -> [String]
single [PackageDB]
dbs
      [PackageDB]
dbs                                 -> String
"-clear-package-db"
                                           String -> [String] -> [String]
forall a. a -> [a] -> [a]
: (PackageDB -> [String]) -> [PackageDB] -> [String]
forall (t :: * -> *) a b. Foldable t => (a -> [b]) -> t a -> [b]
concatMap PackageDB -> [String]
single [PackageDB]
dbs
     where
       single :: PackageDB -> [String]
single (SpecificPackageDB String
db) = [ String
"-package-db=" String -> ShowS
forall a. [a] -> [a] -> [a]
++ String
db ]
       single PackageDB
GlobalPackageDB        = [ String
"-global-package-db" ]
       single PackageDB
UserPackageDB          = [ String
"-user-package-db" ]
       isSpecific :: PackageDB -> Bool
isSpecific (SpecificPackageDB String
_) = Bool
True
       isSpecific PackageDB
_                     = Bool
False

    mbLibraryName :: Library -> Name
#if MIN_VERSION_Cabal(3,0,0)
    mbLibraryName :: Library -> Name
mbLibraryName = Maybe String -> Name
NameLib (Maybe String -> Name)
-> (Library -> Maybe String) -> Library -> Name
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (UnqualComponentName -> String)
-> Maybe UnqualComponentName -> Maybe String
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap UnqualComponentName -> String
unUnqualComponentName (Maybe UnqualComponentName -> Maybe String)
-> (Library -> Maybe UnqualComponentName)
-> Library
-> Maybe String
forall b c a. (b -> c) -> (a -> b) -> a -> c
. LibraryName -> Maybe UnqualComponentName
libraryNameString (LibraryName -> Maybe UnqualComponentName)
-> (Library -> LibraryName) -> Library -> Maybe UnqualComponentName
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Library -> LibraryName
libName
#elif MIN_VERSION_Cabal(2,0,0)
    -- Cabal-2.0 introduced internal libraries, which are named.
    mbLibraryName = NameLib . fmap unUnqualComponentName . libName
#else
    -- Before that, there was only ever at most one library per
    -- .cabal file, which has no name.
    mbLibraryName _ = NameLib Nothing
#endif

    executableName :: Executable -> String
#if MIN_VERSION_Cabal(2,0,0)
    executableName :: Executable -> String
executableName = UnqualComponentName -> String
unUnqualComponentName (UnqualComponentName -> String)
-> (Executable -> UnqualComponentName) -> Executable -> String
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Executable -> UnqualComponentName
exeName
#else
    executableName = exeName
#endif

-- | In compat settings it's better to omit the type-signature
testDeps :: ComponentLocalBuildInfo -> ComponentLocalBuildInfo
#if MIN_VERSION_Cabal(2,0,0)
         -> [(InstalledPackageId, MungedPackageId)]
#else
         -> [(InstalledPackageId, PackageId)]
#endif
testDeps :: ComponentLocalBuildInfo
-> ComponentLocalBuildInfo -> [(UnitId, MungedPackageId)]
testDeps ComponentLocalBuildInfo
xs ComponentLocalBuildInfo
ys = [(UnitId, MungedPackageId)] -> [(UnitId, MungedPackageId)]
forall a. Eq a => [a] -> [a]
nub ([(UnitId, MungedPackageId)] -> [(UnitId, MungedPackageId)])
-> [(UnitId, MungedPackageId)] -> [(UnitId, MungedPackageId)]
forall a b. (a -> b) -> a -> b
$ ComponentLocalBuildInfo -> [(UnitId, MungedPackageId)]
componentPackageDeps ComponentLocalBuildInfo
xs [(UnitId, MungedPackageId)]
-> [(UnitId, MungedPackageId)] -> [(UnitId, MungedPackageId)]
forall a. [a] -> [a] -> [a]
++ ComponentLocalBuildInfo -> [(UnitId, MungedPackageId)]
componentPackageDeps ComponentLocalBuildInfo
ys

amendGPD
    :: String -- ^ doctests test-suite name
    -> GenericPackageDescription
    -> GenericPackageDescription
#if !(MIN_VERSION_Cabal(2,0,0))
amendGPD _ gpd = gpd
#else
amendGPD :: String -> GenericPackageDescription -> GenericPackageDescription
amendGPD String
testSuiteName GenericPackageDescription
gpd = GenericPackageDescription
gpd
    { condTestSuites :: [(UnqualComponentName, CondTree ConfVar [Dependency] TestSuite)]
condTestSuites = ((UnqualComponentName, CondTree ConfVar [Dependency] TestSuite)
 -> (UnqualComponentName, CondTree ConfVar [Dependency] TestSuite))
-> [(UnqualComponentName, CondTree ConfVar [Dependency] TestSuite)]
-> [(UnqualComponentName, CondTree ConfVar [Dependency] TestSuite)]
forall a b. (a -> b) -> [a] -> [b]
map (UnqualComponentName, CondTree ConfVar [Dependency] TestSuite)
-> (UnqualComponentName, CondTree ConfVar [Dependency] TestSuite)
forall {a} {v} {c}.
(Eq a, IsString a) =>
(a, CondTree v c TestSuite) -> (a, CondTree v c TestSuite)
f (GenericPackageDescription
-> [(UnqualComponentName, CondTree ConfVar [Dependency] TestSuite)]
condTestSuites GenericPackageDescription
gpd)
    }
  where
    f :: (a, CondTree v c TestSuite) -> (a, CondTree v c TestSuite)
f (a
name, CondTree v c TestSuite
condTree)
        | a
name a -> a -> Bool
forall a. Eq a => a -> a -> Bool
== String -> a
forall a. IsString a => String -> a
fromString String
testSuiteName = (a
name, CondTree v c TestSuite
condTree')
        | Bool
otherwise                        = (a
name, CondTree v c TestSuite
condTree)
      where
        -- I miss 'lens'
        testSuite :: TestSuite
testSuite = CondTree v c TestSuite -> TestSuite
forall v c a. CondTree v c a -> a
condTreeData CondTree v c TestSuite
condTree
        bi :: BuildInfo
bi = TestSuite -> BuildInfo
testBuildInfo TestSuite
testSuite
        om :: [ModuleName]
om = BuildInfo -> [ModuleName]
otherModules BuildInfo
bi
        am :: [ModuleName]
am = BuildInfo -> [ModuleName]
autogenModules BuildInfo
bi

        -- Cons the module to both other-modules and autogen-modules.
        -- At the moment, cabal-spec-2.0 and cabal-spec-2.2 don't have
        -- "all autogen-modules are other-modules if they aren't exposed-modules"
        -- rule. Hopefully cabal-spec-3.0 will have.
        --
        -- Note: we `nub`, because it's unclear if that's ok to have duplicate
        -- modules in the lists.
        om' :: [ModuleName]
om' = [ModuleName] -> [ModuleName]
forall a. Eq a => [a] -> [a]
nub ([ModuleName] -> [ModuleName]) -> [ModuleName] -> [ModuleName]
forall a b. (a -> b) -> a -> b
$ ModuleName
mn ModuleName -> [ModuleName] -> [ModuleName]
forall a. a -> [a] -> [a]
: [ModuleName]
om
        am' :: [ModuleName]
am' = [ModuleName] -> [ModuleName]
forall a. Eq a => [a] -> [a]
nub ([ModuleName] -> [ModuleName]) -> [ModuleName] -> [ModuleName]
forall a b. (a -> b) -> a -> b
$ ModuleName
mn ModuleName -> [ModuleName] -> [ModuleName]
forall a. a -> [a] -> [a]
: [ModuleName]
am

        mn :: ModuleName
mn = String -> ModuleName
forall a. IsString a => String -> a
fromString String
"Build_doctests"

        bi' :: BuildInfo
bi' = BuildInfo
bi { otherModules :: [ModuleName]
otherModules = [ModuleName]
om', autogenModules :: [ModuleName]
autogenModules = [ModuleName]
am' }
        testSuite' :: TestSuite
testSuite' = TestSuite
testSuite { testBuildInfo :: BuildInfo
testBuildInfo = BuildInfo
bi' }
        condTree' :: CondTree v c TestSuite
condTree' = CondTree v c TestSuite
condTree { condTreeData :: TestSuite
condTreeData = TestSuite
testSuite' }
#endif