{-# LANGUAGE DataKinds #-}
module Distribution.Simple.GHC.Build where
import Distribution.Compat.Prelude
import Prelude ()
import Control.Monad.IO.Class
import Distribution.PackageDescription as PD hiding (buildInfo)
import Distribution.Simple.Build.Inputs
import Distribution.Simple.Flag (Flag)
import Distribution.Simple.GHC.Build.ExtraSources
import Distribution.Simple.GHC.Build.Link
import Distribution.Simple.GHC.Build.Modules
import Distribution.Simple.GHC.Build.Utils (compilerBuildWay, isHaskell, withDynFLib)
import Distribution.Simple.LocalBuildInfo
import Distribution.Simple.Program.Builtin (ghcProgram)
import Distribution.Simple.Program.Db (requireProgram)
import Distribution.Simple.Utils
import Distribution.Types.ComponentLocalBuildInfo
import Distribution.Types.PackageName.Magic (fakePackageId)
import Distribution.Types.ParStrat
import Distribution.Utils.NubList (fromNubListR)
import Distribution.Utils.Path
import System.FilePath (splitDirectories)
build
:: Flag ParStrat
-> PackageDescription
-> PreBuildComponentInputs
-> IO ()
build :: Flag ParStrat
-> PackageDescription -> PreBuildComponentInputs -> IO ()
build Flag ParStrat
numJobs PackageDescription
pkg_descr PreBuildComponentInputs
pbci = do
let
verbosity :: Verbosity
verbosity = PreBuildComponentInputs -> Verbosity
buildVerbosity PreBuildComponentInputs
pbci
isLib :: Bool
isLib = PreBuildComponentInputs -> Bool
buildIsLib PreBuildComponentInputs
pbci
lbi :: LocalBuildInfo
lbi = PreBuildComponentInputs -> LocalBuildInfo
localBuildInfo PreBuildComponentInputs
pbci
bi :: BuildInfo
bi = PreBuildComponentInputs -> BuildInfo
buildBI PreBuildComponentInputs
pbci
clbi :: ComponentLocalBuildInfo
clbi = PreBuildComponentInputs -> ComponentLocalBuildInfo
buildCLBI PreBuildComponentInputs
pbci
isIndef :: Bool
isIndef = ComponentLocalBuildInfo -> Bool
componentIsIndefinite ComponentLocalBuildInfo
clbi
mbWorkDir :: Maybe (SymbolicPath CWD ('Dir Pkg))
mbWorkDir = LocalBuildInfo -> Maybe (SymbolicPath CWD ('Dir Pkg))
mbWorkDirLBI LocalBuildInfo
lbi
i :: SymbolicPathX allowAbsolute Pkg to -> FilePath
i = LocalBuildInfo -> SymbolicPathX allowAbsolute Pkg to -> FilePath
forall (allowAbsolute :: AllowAbsolute) (to :: FileOrDir).
LocalBuildInfo -> SymbolicPathX allowAbsolute Pkg to -> FilePath
interpretSymbolicPathLBI LocalBuildInfo
lbi
let targetDir0 :: SymbolicPath Pkg ('Dir Build)
targetDir0 :: SymbolicPath Pkg ('Dir Build)
targetDir0 = LocalBuildInfo
-> ComponentLocalBuildInfo -> SymbolicPath Pkg ('Dir Build)
componentBuildDir LocalBuildInfo
lbi ComponentLocalBuildInfo
clbi
buildTargetDir0 :: SymbolicPath Pkg ('Dir Artifacts)
buildTargetDir0 :: SymbolicPath Pkg ('Dir Artifacts)
buildTargetDir0
| Bool
isLib = SymbolicPath Pkg ('Dir Build) -> SymbolicPath Pkg ('Dir Artifacts)
forall (allowAbsolute :: AllowAbsolute) from (to1 :: FileOrDir)
(to2 :: FileOrDir).
SymbolicPathX allowAbsolute from to1
-> SymbolicPathX allowAbsolute from to2
coerceSymbolicPath SymbolicPath Pkg ('Dir Build)
targetDir0
| FilePath
targetDirName : [FilePath]
_ <- [FilePath] -> [FilePath]
forall a. [a] -> [a]
reverse ([FilePath] -> [FilePath]) -> [FilePath] -> [FilePath]
forall a b. (a -> b) -> a -> b
$ FilePath -> [FilePath]
splitDirectories (FilePath -> [FilePath]) -> FilePath -> [FilePath]
forall a b. (a -> b) -> a -> b
$ SymbolicPath Pkg ('Dir Build) -> FilePath
forall (allowAbsolute :: AllowAbsolute) from (to :: FileOrDir).
SymbolicPathX allowAbsolute from to -> FilePath
getSymbolicPath SymbolicPath Pkg ('Dir Build)
targetDir0 =
SymbolicPath Pkg ('Dir Build)
-> SymbolicPathX 'AllowAbsolute Pkg ('Dir (ZonkAny 0))
forall (allowAbsolute :: AllowAbsolute) from (to1 :: FileOrDir)
(to2 :: FileOrDir).
SymbolicPathX allowAbsolute from to1
-> SymbolicPathX allowAbsolute from to2
coerceSymbolicPath SymbolicPath Pkg ('Dir Build)
targetDir0 SymbolicPathX 'AllowAbsolute Pkg ('Dir (ZonkAny 0))
-> RelativePath (ZonkAny 0) ('Dir Artifacts)
-> SymbolicPath Pkg ('Dir Artifacts)
forall p q r. PathLike p q r => p -> q -> r
</> FilePath -> RelativePath (ZonkAny 0) ('Dir Artifacts)
forall from (to :: FileOrDir).
HasCallStack =>
FilePath -> RelativePath from to
makeRelativePathEx (FilePath
targetDirName FilePath -> FilePath -> FilePath
forall a. [a] -> [a] -> [a]
++ FilePath
"-tmp")
| Bool
otherwise = FilePath -> SymbolicPath Pkg ('Dir Artifacts)
forall a. HasCallStack => FilePath -> a
error FilePath
"GHC.build: targetDir is empty"
IO () -> IO ()
forall a. IO a -> IO a
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO () -> IO ()) -> IO () -> IO ()
forall a b. (a -> b) -> a -> b
$ do
Verbosity -> Bool -> FilePath -> IO ()
createDirectoryIfMissingVerbose Verbosity
verbosity Bool
True (FilePath -> IO ()) -> FilePath -> IO ()
forall a b. (a -> b) -> a -> b
$ SymbolicPath Pkg ('Dir Build) -> FilePath
forall {allowAbsolute :: AllowAbsolute} {to :: FileOrDir}.
SymbolicPathX allowAbsolute Pkg to -> FilePath
i SymbolicPath Pkg ('Dir Build)
targetDir0
Verbosity -> Bool -> FilePath -> IO ()
createDirectoryIfMissingVerbose Verbosity
verbosity Bool
True (FilePath -> IO ()) -> FilePath -> IO ()
forall a b. (a -> b) -> a -> b
$ SymbolicPath Pkg ('Dir Artifacts) -> FilePath
forall {allowAbsolute :: AllowAbsolute} {to :: FileOrDir}.
SymbolicPathX allowAbsolute Pkg to -> FilePath
i SymbolicPath Pkg ('Dir Artifacts)
buildTargetDir0
let targetDir :: SymbolicPath Pkg ('Dir Build)
targetDir = SymbolicPath Pkg ('Dir Build)
targetDir0
buildTargetDir <-
if Bool
isLib
then
Maybe (SymbolicPath CWD ('Dir Pkg))
-> SymbolicPath Pkg ('Dir Artifacts)
-> IO (SymbolicPath Pkg ('Dir Artifacts))
forall dir (to :: FileOrDir).
Maybe (SymbolicPath CWD ('Dir dir))
-> SymbolicPath dir to -> IO (SymbolicPath dir to)
tryMakeRelative Maybe (SymbolicPath CWD ('Dir Pkg))
mbWorkDir SymbolicPath Pkg ('Dir Artifacts)
buildTargetDir0
else SymbolicPath Pkg ('Dir Artifacts)
-> IO (SymbolicPath Pkg ('Dir Artifacts))
forall a. a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return SymbolicPath Pkg ('Dir Artifacts)
buildTargetDir0
(ghcProg, _) <- liftIO $ requireProgram verbosity ghcProgram (withPrograms lbi)
let wantedWays@(wantedLibWays, wantedFLibWay, wantedExeWay) = buildWays lbi
let doingTH = BuildInfo -> Bool
usesTemplateHaskellOrQQ BuildInfo
bi
defaultGhcWay = Compiler -> BuildWay
compilerBuildWay (PreBuildComponentInputs -> Compiler
buildCompiler PreBuildComponentInputs
pbci)
wantedModBuildWays = case PreBuildComponentInputs -> Component
buildComponent PreBuildComponentInputs
pbci of
CLib Library
_ -> Bool -> [BuildWay]
wantedLibWays Bool
isIndef
CFLib ForeignLib
fl -> [Bool -> BuildWay
wantedFLibWay (ForeignLib -> Bool
withDynFLib ForeignLib
fl)]
CExe Executable
_ -> [BuildWay
wantedExeWay]
CTest TestSuite
_ -> [BuildWay
wantedExeWay]
CBench Benchmark
_ -> [BuildWay
wantedExeWay]
finalModBuildWays =
[BuildWay]
wantedModBuildWays
[BuildWay] -> [BuildWay] -> [BuildWay]
forall a. [a] -> [a] -> [a]
++ [BuildWay
defaultGhcWay | Bool
doingTH Bool -> Bool -> Bool
&& BuildWay
defaultGhcWay BuildWay -> [BuildWay] -> Bool
forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`notElem` [BuildWay]
wantedModBuildWays]
compNameStr = ComponentName -> FilePath
showComponentName (ComponentName -> FilePath) -> ComponentName -> FilePath
forall a b. (a -> b) -> a -> b
$ Component -> ComponentName
componentName (Component -> ComponentName) -> Component -> ComponentName
forall a b. (a -> b) -> a -> b
$ PreBuildComponentInputs -> Component
buildComponent PreBuildComponentInputs
pbci
liftIO $ info verbosity ("Wanted module build ways(" ++ compNameStr ++ "): " ++ show wantedModBuildWays)
liftIO $ info verbosity ("Final module build ways(" ++ compNameStr ++ "): " ++ show finalModBuildWays)
(mbMainFile, inputModules) <- componentInputs buildTargetDir pkg_descr pbci
let (hsMainFile, nonHsMainFile) =
case mbMainFile of
Just SymbolicPath Pkg 'File
mainFile
| PackageDescription -> PackageIdentifier
PD.package PackageDescription
pkg_descr PackageIdentifier -> PackageIdentifier -> Bool
forall a. Eq a => a -> a -> Bool
== PackageIdentifier
fakePackageId
Bool -> Bool -> Bool
|| FilePath -> Bool
isHaskell (SymbolicPath Pkg 'File -> FilePath
forall (allowAbsolute :: AllowAbsolute) from (to :: FileOrDir).
SymbolicPathX allowAbsolute from to -> FilePath
getSymbolicPath SymbolicPath Pkg 'File
mainFile) ->
(SymbolicPath Pkg 'File -> Maybe (SymbolicPath Pkg 'File)
forall a. a -> Maybe a
Just SymbolicPath Pkg 'File
mainFile, Maybe (SymbolicPath Pkg 'File)
forall a. Maybe a
Nothing)
| Bool
otherwise ->
(Maybe (SymbolicPath Pkg 'File)
forall a. Maybe a
Nothing, SymbolicPath Pkg 'File -> Maybe (SymbolicPath Pkg 'File)
forall a. a -> Maybe a
Just SymbolicPath Pkg 'File
mainFile)
Maybe (SymbolicPath Pkg 'File)
Nothing -> (Maybe (SymbolicPath Pkg 'File)
forall a. Maybe a
Nothing, Maybe (SymbolicPath Pkg 'File)
forall a. Maybe a
Nothing)
buildOpts <- buildHaskellModules numJobs ghcProg hsMainFile inputModules buildTargetDir finalModBuildWays pbci
extraSources <- buildAllExtraSources nonHsMainFile ghcProg buildTargetDir wantedWays pbci
linkOrLoadComponent
ghcProg
pkg_descr
(fromNubListR extraSources)
(buildTargetDir, targetDir)
(wantedWays, buildOpts)
pbci