packages feed

tilia-0.2.0.0: src/Tilia/Cabal/Package.hs

{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE RecordWildCards #-}

-- | Information coming from @.cabal@ files.
module Tilia.Cabal.Package
  ( PackageProblem (..),
    describePackageProblem,
    readPackageDescription,
    parsePackageDescription,
    declaredModules,
    declaredModuleNames,
    modulesOf,
    renamedModules,
    packageSourceDirs,
    sourceDirsOf,
    moduleExtensions,
    PackageReader,
    newPackageReader,
    conditionValues,
    ComponentSection (..),
    BranchSections (..),
    Kind (..),
    packageOf,
    packageComponents,
    addedSources,
    sourceExtensions,
    isDeclaredModule,
    joined,
    setupScript,
  )
where

import Control.Applicative ((<|>))
import Control.Concurrent.MVar (modifyMVar, newMVar)
import Control.Monad ((<=<))
import Data.ByteString qualified as BS
import Data.Either (rights)
import Data.Foldable (for_, toList)
import Data.Generics.Schemes (listify)
import Data.IORef
import Data.List (nub, sortOn, stripPrefix)
import Data.List.NonEmpty qualified as NE
import Data.Map.Strict (Map)
import Data.Map.Strict qualified as Map
import Data.Maybe (fromMaybe, mapMaybe)
import Data.Ord (Down (..))
import Data.Set (Set)
import Data.Set qualified as Set
import Data.Text (Text)
import Data.Text qualified as T
import Data.Time.Clock (UTCTime)
import Distribution.Compiler (CompilerFlavor (..))
import Distribution.Fields.ParseResult (runParseResult)
import Distribution.ModuleName qualified as ModuleName
import Distribution.PackageDescription
  ( Benchmark (..),
    BenchmarkInterface (..),
    BuildInfo (..),
    CondBranch (..),
    CondTree (..),
    ConfVar (..),
    Executable (..),
    FlagAssignment,
    ForeignLib (..),
    GenericPackageDescription (..),
    IncludeRenaming (..),
    Library (..),
    Mixin (..),
    ModuleRenaming (..),
    PackageDescription (..),
    PackageFlag (..),
    TestSuite (..),
    TestSuiteInterface (..),
    lookupFlagAssignment,
    pkgName,
    unPackageName,
    unUnqualComponentName,
  )
import Distribution.PackageDescription.Parsec (parseGenericPackageDescription)
import Distribution.Parsec (PErrorWithSource (..), showPError, simpleParsec)
import Distribution.System (buildArch, buildOS)
import Distribution.Types.BuildInfo (usedExtensions)
import Distribution.Types.CondTree (simplifyCondTree)
import Distribution.Utils.Path (SymbolicPathX, getSymbolicPath)
import Distribution.Version (Version, withinRange)
import GHC.Driver.Session qualified as GHC
import GHC.LanguageExtensions.Type (Extension)
import Language.Haskell.Extension qualified as Cabal
import System.Directory
  ( canonicalizePath,
    doesDirectoryExist,
    getModificationTime,
  )
import System.FilePath
  ( dropExtension,
    equalFilePath,
    joinPath,
    normalise,
    splitDirectories,
    takeDirectory,
    takeExtension,
    (</>),
  )
import System.IO.Unsafe (unsafePerformIO)

import Tilia.Cabal.Project (cabalFilesIn, startingDirectory)
import Tilia.Cpp.Macros (Macros (..))
import Tilia.Pragma
  ( Defaults (..),
    defaultsOf,
    editionExtensions,
    lookupEdition,
    nameSwitch,
  )
import Tilia.Process (readProgramOutput)
import Tilia.Utils (pathUnder, quietly, readBytes, tshow)

-- | Why a file's extensions could not be settled.
data PackageProblem
  = -- | No @.cabal@ file at or above the module.
    NoPackageFile
  | -- | A @.cabal@ file that could not be read at all, and what went wrong.
    PackageUnreadable FilePath Text
  | -- | A @.cabal@ file that was read but did not parse, and everything the
    -- parser had to say about it: a message for each complaint, of one line
    -- or more, carrying the position it refers to.
    PackageMalformed FilePath [Text]
  | -- | A @.cabal@ file naming no component whose @hs-source-dirs@ holds
    -- the module.
    FileUnclaimed FilePath
  deriving (Eq, Show)

-- | Say what went wrong.
describePackageProblem :: PackageProblem -> Text
describePackageProblem = \case
  NoPackageFile -> "no .cabal file above it"
  PackageUnreadable file why -> T.pack file <> " could not be read: " <> why
  PackageMalformed file complaints ->
    T.pack file
      <> " does not parse:"
      <> foldMap ("\n  " <>) (concatMap T.lines complaints)
  FileUnclaimed file ->
    T.pack file <> " names no component whose hs-source-dirs holds it"

-- | What we retain from reading a .cabal file.
type PackageReader = FilePath -> IO (Either PackageProblem Defaults)

-- | A 'PackageReader' that remembers what it has already worked out.
newPackageReader ::
  -- | The compiler's version, where the build plan names it, which spares
  -- asking the compiler.
  Maybe Version ->
  -- | The flags the build plan gives each package, by name.
  Map Text FlagAssignment ->
  -- | The @.cabal@ files of the project, which a module is looked for in
  -- where the nearest @.cabal@ file above it does not claim it or cannot be
  -- read.
  [FilePath] ->
  IO PackageReader
newPackageReader planned flags project = do
  covering <- newIORef Map.empty
  described <- newIORef Map.empty
  known <- newMVar (Just <$> planned)
  let compiler = modifyMVar known $ \case
        Just version -> pure (Just version, version)
        Nothing -> do
          version <- compilerVersion
          pure (Just version, version)
      elsewhere file problem = do
        found <- traverse (componentBranchesOf compiler flags described) project
        pure $ case claiming file (concat (rights found)) of
          Just c -> Right (componentExtensions c)
          Nothing -> Left problem
  pure $ \path -> quietly (Left NoPackageFile) $ do
    file <- canonicalizePath path
    from <- startingDirectory file
    findCabalFile covering from >>= \case
      Nothing -> elsewhere file NoPackageFile
      Just cabalFile ->
        componentBranchesOf compiler flags described cabalFile >>= \case
          Left problem -> elsewhere file problem
          Right components
            | equalFilePath file (takeDirectory cabalFile </> setupScript) ->
                pure (Right (defaultsOf False (editionExtensions Nothing) []))
            | Just c <- claiming file components ->
                pure (Right (componentExtensions c))
            | otherwise -> elsewhere file (FileUnclaimed cabalFile)

-- | One branch of a component, with everything in force in it already
-- worked out.
data ComponentBranch = ComponentBranch
  { -- | Its source directories, absolute and canonical.
    componentDirs :: [FilePath],
    -- | The modules this branch names, each as a path without an
    -- extension relative to whichever of its source directories holds it.
    componentModules :: Set FilePath,
    -- | Its entry points, relative to its source directories in the same
    -- way but with their extensions.
    componentEntries :: Set FilePath,
    -- | What it puts in force.
    componentExtensions :: Defaults
  }

-- | Which branch holds the file, of those whose directories cover it.
claiming :: FilePath -> [ComponentBranch] -> Maybe ComponentBranch
claiming file components =
  case sortOn
    (Down . fst)
    [((named c, nearness c), c) | c <- components, covered c] of
    ((_, c) : _) -> Just c
    [] -> Nothing
  where
    covered = not . null . covering
    named c = any (declares c) (covering c)
    declares c (_, under) =
      isDeclaredModule
        (componentModules c)
        (componentEntries c)
        (joinPath under)
    nearness = maximum . fmap (length . fst) . covering
    covering c =
      [ (d, under)
      | d <- componentDirs c,
        Just under@(_ : _) <- [pathUnder d file]
      ]

-- | Is a file, named relative to a source directory, one of the modules or
-- entry points declared there?
isDeclaredModule ::
  -- | The modules, without their extensions.
  Set FilePath ->
  -- | The entry points.
  Set FilePath ->
  -- | The file, named relative to the source directory.
  FilePath ->
  Bool
isDeclaredModule modules entries path =
  takeExtension path `elem` sourceExtensions
    && (Set.member path entries || Set.member (dropExtension path) modules)

-- | The nearest @.cabal@ file at or above a directory.
findCabalFile ::
  -- | What is known already, by directory: the file covering it, or
  -- 'Nothing' for one with no @.cabal@ anywhere above it. Read before the
  -- walk and added to after it, for every directory the walk passed through
  -- rather than only the one asked about—none of the others held a @.cabal@
  -- either, which is why the walk went through them, so the answer is
  -- theirs as well.
  IORef (Map FilePath (Maybe FilePath)) ->
  -- | Where to start, which is walked upwards until a @.cabal@ file turns
  -- up or the filesystem root is reached.
  FilePath ->
  IO (Maybe FilePath)
findCabalFile ref = climb []
  where
    climb passed directory = do
      known <- readIORef ref
      case Map.lookup directory known of
        Just answer -> settle passed answer
        Nothing -> do
          cabalFilesIn directory >>= \case
            (found : _) -> settle (directory : passed) (Just found)
            [] ->
              let parent = takeDirectory directory
               in if parent == directory
                    then settle (directory : passed) Nothing
                    else climb (directory : passed) parent
    settle passed answer = do
      modifyIORef'
        ref
        (\m -> foldl' (\acc d -> Map.insert d answer acc) m passed)
      pure answer

-- | What a @.cabal@ file amounts to.
componentBranchesOf ::
  -- | The compiler's version, asked for only where a condition names it.
  IO (Maybe Version) ->
  -- | The flags the build plan gives each package, by name.
  Map Text FlagAssignment ->
  -- | What is known already, by @.cabal@ file.
  IORef (Map FilePath (Either PackageProblem [ComponentBranch])) ->
  -- | The @.cabal@ file.
  FilePath ->
  IO (Either PackageProblem [ComponentBranch])
componentBranchesOf compiler flags ref cabalFile = do
  known <- readIORef ref
  case Map.lookup cabalFile known of
    Just answer -> pure answer
    Nothing -> do
      answer <- settle
      modifyIORef' ref (Map.insert cabalFile answer)
      pure answer
  where
    settle =
      readPackageDescription cabalFile >>= \case
        Left problem -> pure (Left problem)
        Right description -> do
          valueOf <-
            conditionValues
              compiler
              (Map.findWithDefault mempty (packageOf description) flags)
              description
          Right
            <$> traverse
              ( componentBranch
                  (takeDirectory cabalFile)
                  ( foldMap
                      (definedBy . sectionInfo . ownSection)
                      (packageBranches description)
                  )
              )
              (sectionsInForce valueOf description)

-- | Read and parse a @.cabal@ file, or say why it cannot be, parsing it
-- again only once it has been modified, as an editor may start a session
-- for every request.
readPackageDescription ::
  FilePath ->
  IO (Either PackageProblem GenericPackageDescription)
readPackageDescription cabalFile = do
  modified <- quietly Nothing (Just <$> getModificationTime path)
  Map.lookup path <$> readIORef descriptionsRead >>= \case
    Just (at, found) | Just at == modified -> pure found
    _ -> do
      found <-
        readBytes path >>= \case
          Left why -> pure (Left (PackageUnreadable path why))
          Right bytes -> pure (parsePackageDescription path bytes)
      for_ modified $ \at ->
        atomicModifyIORef'
          descriptionsRead
          ((,()) . Map.insert path (at, found))
      pure found
  where
    path = normalise cabalFile

-- | What 'readPackageDescription' has read, by file, with the time each
-- file was modified when it was read.
descriptionsRead ::
  IORef
    (Map FilePath (UTCTime, Either PackageProblem GenericPackageDescription))
descriptionsRead = unsafePerformIO (newIORef Map.empty)
{-# NOINLINE descriptionsRead #-}

-- | Parse what a @.cabal@ file holds, or say why it cannot be.
parsePackageDescription ::
  -- | Where it comes from, named in what goes wrong.
  FilePath ->
  -- | What it holds.
  BS.ByteString ->
  Either PackageProblem GenericPackageDescription
parsePackageDescription cabalFile bytes =
  case snd (runParseResult (parseGenericPackageDescription bytes)) of
    Left (_, complaints) ->
      Left (PackageMalformed cabalFile (fmap said (NE.toList complaints)))
    Right description -> Right description
  where
    said = T.pack . showPError cabalFile . perror

-- | The value a build of a package on this platform gives each variable its
-- conditions ask about.
conditionValues ::
  -- | The compiler's version, asked for only where a condition names it.
  IO (Maybe Version) ->
  -- | The flags the build plan gives the package.
  FlagAssignment ->
  -- | The package, whose flags the plan does not set take their defaults.
  GenericPackageDescription ->
  IO (ConfVar -> Either ConfVar Bool)
conditionValues compiler planned described = do
  version <-
    if null (listify namesCompiler described)
      then pure Nothing
      else compiler
  pure $ \case
    OS os -> Right (os == buildOS)
    Arch arch -> Right (arch == buildArch)
    v@(PackageFlag name) ->
      maybe
        (Left v)
        Right
        (lookupFlagAssignment name planned <|> lookup name defaults)
    v@(Impl GHC range) -> maybe (Left v) (Right . (`withinRange` range)) version
    Impl _ _ -> Right False
  where
    namesCompiler = \case
      Impl _ _ -> True
      _ -> False
    defaults = [(flagName f, flagDefault f) | f <- genPackageFlags described]

-- | The version of the @ghc@ on the path, which builds the package.
compilerVersion :: IO (Maybe Version)
compilerVersion =
  (simpleParsec . T.unpack . T.strip =<<)
    <$> readProgramOutput "ghc" ["--numeric-version"]

-- | One branch, with its directories resolved and what it puts in force
-- settled.
componentBranch ::
  -- | The directory of the package.
  FilePath ->
  -- | The macros the package's @cpp-options@ define in any branch.
  Set Text ->
  -- | The branch.
  ComponentSection ->
  IO ComponentBranch
componentBranch root anywhere ComponentSection{..} = do
  dirs <- traverse (quietlyCanonical . (root </>)) (sourceDirsOf sectionInfo)
  pure
    ComponentBranch
      { componentDirs = concat dirs,
        componentModules = Set.fromList (fmap (uncurry joined) sectionModules),
        componentEntries = Set.fromList (fmap (uncurry joined) sectionEntries),
        componentExtensions =
          (extensionsInForce sectionInfo)
            { defaultsMacros =
                mempty
                  { macroDefined = here,
                    macroUndefined = Set.difference anywhere here
                  }
            }
      }
  where
    here = definedBy sectionInfo
    quietlyCanonical d =
      quietly [] $
        doesDirectoryExist d >>= \case
          True -> pure <$> canonicalizePath d
          False -> pure []

-- | What is in force in each branch of every component, in the order they
-- are declared.
sectionsInForce ::
  -- | The value the build gives each variable a condition asks about.
  (ConfVar -> Either ConfVar Bool) ->
  -- | The package.
  GenericPackageDescription ->
  [ComponentSection]
sectionsInForce valueOf described =
  ( \BranchSections{..} ->
      inheritedSection
        <> ownSection
        <> ComponentSection (sectionInfo (builtSection valueOf)) [] []
  )
    <$> packageBranches described

-- | Every branch of every component of a package, in the order declared.
packageBranches :: GenericPackageDescription -> [BranchSections]
packageBranches described =
  [b | (_, _, branches) <- packageComponents described, b <- branches]

-- | The kinds of component a @.cabal@ file can declare, and the setup
-- script beside it.
data Kind = Lib | FLib | Exe | Test | Bench | Setup
  deriving (Eq, Ord, Show)

-- | Every component of a package, in the order declared: its kind, its
-- name, which for the main library is the package's own, and its branches.
packageComponents ::
  GenericPackageDescription -> [(Kind, Text, [BranchSections])]
packageComponents described =
  concat
    [ [(Lib, packageOf described, libraryBranches t) | t <- toList (condLibrary described)],
      [(Lib, nameOf n, libraryBranches t) | (n, t) <- condSubLibraries described],
      [(FLib, nameOf n, foreignLibBranches t) | (n, t) <- condForeignLibs described],
      [(Exe, nameOf n, executableBranches t) | (n, t) <- condExecutables described],
      [(Test, nameOf n, suiteBranches t) | (n, t) <- condTestSuites described],
      [(Bench, nameOf n, benchmarkBranches t) | (n, t) <- condBenchmarks described]
    ]
  where
    nameOf = T.pack . unUnqualComponentName

-- | The name of a package.
packageOf :: GenericPackageDescription -> Text
packageOf =
  T.pack . unPackageName . pkgName . package . packageDescription

-- | Each source directory the branches given name, with every module they
-- declare there and where it would be written under it, without an
-- extension.
declaredModules :: [BranchSections] -> [(FilePath, [(Text, FilePath)])]
declaredModules branches =
  [ (d, modulesOf section)
  | branch <- branches,
    (d, section) <- addedSources branch
  ]

-- | Every module a section declares, with where it would be written under
-- a source directory, without an extension.
modulesOf :: ComponentSection -> [(Text, FilePath)]
modulesOf section =
  [ ( T.intercalate "." (fmap T.pack (splitDirectories sub <> [name])),
      joined sub name
    )
  | (sub, name) <- sectionModules section
  ]

-- | The modules of libraries a component's @mixins@ give other names: each
-- such name, with the library's package and the module's name there.
renamedModules :: BuildInfo -> [(Text, (Text, Text))]
renamedModules bi =
  [ ( dotted new,
      (T.pack (unPackageName (mixinPackageName mixin)), dotted original)
    )
  | mixin <- mixins bi,
    ModuleRenaming pairs <- [includeProvidesRn (mixinIncludeRenaming mixin)],
    (original, new) <- pairs
  ]
  where
    dotted = T.intercalate "." . fmap T.pack . ModuleName.components

-- | The names of the modules a package declares, whatever the conditions in
-- its @.cabal@ file.
declaredModuleNames :: GenericPackageDescription -> [Text]
declaredModuleNames =
  fmap fst . concatMap snd . declaredModules . packageBranches

-- | Every directory any branch of a package names for its files, and then
-- the package's own.
packageSourceDirs :: GenericPackageDescription -> [FilePath]
packageSourceDirs described = nub (named <> ["."])
  where
    named =
      [ getSymbolicPath d
      | branch <- packageBranches described,
        d <- hsSourceDirs (sectionInfo (ownSection branch))
      ]

-- | What the components of a package that list a module put in force, or
-- every component where none lists it, all together: every edition they
-- name and every extension they turn on or off, in order.
moduleExtensions ::
  -- | The package.
  GenericPackageDescription ->
  -- | The module.
  Text ->
  Defaults
moduleExtensions described = \modName ->
  maybe whole (byListing Map.!) (Map.lookup modName listing)
  where
    components = [branches | (_, _, branches) <- packageComponents described]
    listing =
      Map.fromListWith
        (flip (<>))
        [ (m, [i])
        | (i, branches) <- zip [0 :: Int ..] components,
          m <- nub (fmap fst (concatMap snd (declaredModules branches)))
        ]
    byListing =
      Map.fromList
        [ (is, inForce (concatMap (components !!) is))
        | is <- nub (Map.elems listing)
        ]
    whole = inForce (concat components)
    inForce branches =
      defaultsOf
        (namesSafeMode used)
        baseline
        (mapMaybe extensionSwitch used)
      where
        infos = [sectionInfo (ownSection branch) | branch <- branches]
        used = concatMap usedExtensions infos
        baseline = case mapMaybe (ghcLanguage <=< defaultLanguage) infos of
          [] -> editionExtensions (Just GHC.Haskell98)
          editions -> foldMap (editionExtensions . Just) editions

-- | What one branch of a component declares.
data ComponentSection = ComponentSection
  { -- | Its build settings.
    sectionInfo :: BuildInfo,
    -- | Its modules, each as the directory that holds it relative to a
    -- source directory and its file name without an extension.
    sectionModules :: [(FilePath, FilePath)],
    -- | Its entry points, each as the directory that holds it relative to a
    -- source directory and its file name.
    sectionEntries :: [(FilePath, FilePath)]
  }

instance Semigroup ComponentSection where
  ComponentSection i m e <> ComponentSection i' m' e' =
    ComponentSection (i <> i') (m <> m') (e <> e')

instance Monoid ComponentSection where
  mempty = ComponentSection mempty [] []

-- | The sections of one branch of a component.
data BranchSections = BranchSections
  { -- | What it inherits from the branches around it.
    inheritedSection :: ComponentSection,
    -- | What it declares itself.
    ownSection :: ComponentSection,
    -- | Its component as a build configures it, given the value the build
    -- gives each variable a condition asks about.
    builtSection :: (ConfVar -> Either ConfVar Bool) -> ComponentSection
  }

-- | Every branch of a library.
libraryBranches :: CondTree ConfVar Library -> [BranchSections]
libraryBranches = branchesOf $ \l ->
  declaring (libBuildInfo l) (exposedModules l <> signatures l) []

-- | Every branch of a foreign library.
foreignLibBranches :: CondTree ConfVar ForeignLib -> [BranchSections]
foreignLibBranches = branchesOf $ \f -> declaring (foreignLibBuildInfo f) [] []

-- | Every branch of an executable.
executableBranches :: CondTree ConfVar Executable -> [BranchSections]
executableBranches = branchesOf $ \e ->
  declaring (buildInfo e) [] [entryPoint (modulePath e)]

-- | Every branch of a test suite.
suiteBranches :: CondTree ConfVar TestSuite -> [BranchSections]
suiteBranches = branchesOf $ \s ->
  case testInterface s of
    TestSuiteExeV10 _ path -> declaring (testBuildInfo s) [] [entryPoint path]
    TestSuiteLibV09 _ modName -> declaring (testBuildInfo s) [modName] []
    _ -> declaring (testBuildInfo s) [] []

-- | Every branch of a benchmark.
benchmarkBranches :: CondTree ConfVar Benchmark -> [BranchSections]
benchmarkBranches = branchesOf $ \b ->
  case benchmarkInterface b of
    BenchmarkExeV10 _ path ->
      declaring (benchmarkBuildInfo b) [] [entryPoint path]
    _ -> declaring (benchmarkBuildInfo b) [] []

-- | Every branch of a component.
branchesOf ::
  Semigroup a =>
  (a -> ComponentSection) ->
  CondTree ConfVar a ->
  [BranchSections]
branchesOf f root = go mempty root
  where
    built valueOf = f (simplifyCondTree valueOf root)
    go inherited node =
      let own = f (condTreeData node)
       in BranchSections inherited own built
            : concatMap (branches (inherited <> own)) (condTreeComponents node)
    branches inherited (CondBranch _ yes no) =
      go inherited yes <> foldMap (go inherited) no

-- | Where the files a branch adds to its component may be: each source
-- directory, with what it may hold.
--
-- Only what the branches around it have not already paired, which is its
-- own declarations in every directory in force and what it inherits in the
-- directories it adds. A pair made for one branch is thus never made again
-- for the branches inside it.
addedSources :: BranchSections -> [(FilePath, ComponentSection)]
addedSources BranchSections{..} =
  [ (d, ownSection)
  | d <- sourceDirsOf (sectionInfo inheritedSection <> sectionInfo ownSection)
  ]
    <> [ (d, inheritedSection)
       | d <- fmap getSymbolicPath (hsSourceDirs (sectionInfo ownSection))
       ]

-- | What a component's own section declares, modules together with the
-- ones its build settings name.
declaring ::
  BuildInfo ->
  [ModuleName.ModuleName] ->
  [FilePath] ->
  ComponentSection
declaring bi ms entries =
  ComponentSection
    { sectionInfo = bi,
      sectionModules = fmap split (ms <> otherModules bi),
      sectionEntries = fmap splitPath entries
    }
  where
    split m = case ModuleName.components m of
      [] -> ("", "")
      cs -> (joinPath (init cs), last cs)
    splitPath path = case splitDirectories path of
      [] -> ("", "")
      ps -> (joinPath (init ps), last ps)

-- | The extensions a Haskell source file can have.
sourceExtensions :: [String]
sourceExtensions = [".hs", ".hs-boot", ".hsig"]

-- | Two relative paths one after the other, where either may be empty or
-- @.@.
joined :: FilePath -> FilePath -> FilePath
joined a b
  | a `elem` ["", "."] = b
  | b `elem` ["", "."] = a
  | otherwise = a </> b

-- | The path of an entry point.
entryPoint :: SymbolicPathX allowAbsolute from to -> FilePath
entryPoint = normalise . getSymbolicPath

-- | The setup script of a package, beside its @.cabal@ file.
--
-- @cabal@ compiles it with no language or extensions of its own, whatever
-- the components around it put in force.
setupScript :: FilePath
setupScript = "Setup.hs"

-- | The @hs-source-dirs@ of a component, @.@ where it names none.
sourceDirsOf :: BuildInfo -> [FilePath]
sourceDirsOf bi = case fmap getSymbolicPath (hsSourceDirs bi) of
  [] -> ["."]
  ds -> ds

-- | What a component puts in force, before any module's pragmas.
extensionsInForce :: BuildInfo -> Defaults
extensionsInForce bi =
  defaultsOf
    (namesSafeMode (usedExtensions bi))
    (editionExtensions edition)
    (mapMaybe extensionSwitch (usedExtensions bi))
  where
    edition = ghcLanguage (fromMaybe Cabal.Haskell98 (defaultLanguage bi))

-- | The macros the @-D@ options among a component's @cpp-options@ define.
definedBy :: BuildInfo -> Set Text
definedBy bi =
  Set.fromList
    [ T.pack (takeWhile (/= '=') name)
    | Just name@(_ : _) <- stripPrefix "-D" <$> cppOptions bi
    ]

-- | Does a component name a Safe Haskell mode among its extensions?
namesSafeMode :: [Cabal.Extension] -> Bool
namesSafeMode = any $ \case
  Cabal.EnableExtension e ->
    e `elem` [Cabal.Safe, Cabal.Trustworthy, Cabal.Unsafe]
  _ -> False

-- | GHC's reading of an extension Cabal turns on or off, where GHC knows it.
extensionSwitch :: Cabal.Extension -> Maybe (GHC.OnOff Extension)
extensionSwitch = \case
  Cabal.EnableExtension e -> nameSwitch (tshow e)
  Cabal.DisableExtension e -> nameSwitch ("No" <> tshow e)
  _ -> Nothing

-- | GHC's name for a language edition, when it has one.
ghcLanguage :: Cabal.Language -> Maybe GHC.Language
ghcLanguage = lookupEdition . tshow