module Distribution.Simple.Build (
build,
makefile,
initialBuildSteps,
writeAutogenFiles,
) where
import qualified Distribution.Simple.GHC as GHC
import qualified Distribution.Simple.JHC as JHC
import qualified Distribution.Simple.NHC as NHC
import qualified Distribution.Simple.Hugs as Hugs
import qualified Distribution.Simple.Build.Macros as Build.Macros
import qualified Distribution.Simple.Build.PathsModule as Build.PathsModule
import Distribution.Package
( Package(..) )
import Distribution.Simple.Compiler
( CompilerFlavor(..), compilerFlavor )
import Distribution.PackageDescription
( PackageDescription(..), BuildInfo(..)
, Executable(..), Library(..), hasLibs )
import qualified Distribution.ModuleName as ModuleName
import Distribution.Simple.Setup
( BuildFlags(..), MakefileFlags(..), fromFlag )
import Distribution.Simple.PreProcess
( preprocessSources, PPSuffixHandler )
import Distribution.Simple.LocalBuildInfo
( LocalBuildInfo(compiler, buildDir) )
import Distribution.Simple.BuildPaths
( autogenModulesDir, autogenModuleName, cppHeaderName )
import Distribution.Simple.Utils
( createDirectoryIfMissingVerbose, die, setupMessage, rewriteFile )
import Distribution.Verbosity
( Verbosity )
import Distribution.Text
( display )
import Data.Maybe
( maybeToList )
import Control.Monad
( unless, when )
import System.FilePath
( (</>), (<.>) )
build :: PackageDescription
-> LocalBuildInfo
-> BuildFlags
-> [ PPSuffixHandler ]
-> IO ()
build pkg_descr lbi flags suffixes = do
let distPref = fromFlag (buildDistPref flags)
verbosity = fromFlag (buildVerbosity flags)
initialBuildSteps distPref pkg_descr lbi verbosity suffixes
setupMessage verbosity "Building" (packageId pkg_descr)
case compilerFlavor (compiler lbi) of
GHC -> GHC.build pkg_descr lbi verbosity
JHC -> JHC.build pkg_descr lbi verbosity
Hugs -> Hugs.build pkg_descr lbi verbosity
NHC -> NHC.build pkg_descr lbi verbosity
_ -> die ("Building is not supported with this compiler.")
makefile :: PackageDescription
-> LocalBuildInfo
-> MakefileFlags
-> [ PPSuffixHandler ]
-> IO ()
makefile pkg_descr lbi flags suffixes = do
let distPref = fromFlag (makefileDistPref flags)
verbosity = fromFlag (makefileVerbosity flags)
initialBuildSteps distPref pkg_descr lbi verbosity suffixes
when (not (hasLibs pkg_descr)) $
die ("Makefile is only supported for libraries, currently.")
setupMessage verbosity "Generating Makefile" (packageId pkg_descr)
case compilerFlavor (compiler lbi) of
GHC -> GHC.makefile pkg_descr lbi flags
_ -> die ("Generating a Makefile is not supported for this compiler.")
initialBuildSteps :: FilePath
-> PackageDescription
-> LocalBuildInfo
-> Verbosity
-> [ PPSuffixHandler ]
-> IO ()
initialBuildSteps _distPref pkg_descr lbi verbosity suffixes = do
let buildInfos =
map libBuildInfo (maybeToList (library pkg_descr)) ++
map buildInfo (executables pkg_descr)
unless (any buildable buildInfos) $ do
let name = display (packageId pkg_descr)
die ("Package " ++ name ++ " can't be built on this system.")
createDirectoryIfMissingVerbose verbosity True (buildDir lbi)
writeAutogenFiles verbosity pkg_descr lbi
preprocessSources pkg_descr lbi False verbosity suffixes
writeAutogenFiles :: Verbosity
-> PackageDescription
-> LocalBuildInfo
-> IO ()
writeAutogenFiles verbosity pkg lbi = do
createDirectoryIfMissingVerbose verbosity True (autogenModulesDir lbi)
let pathsModulePath = autogenModulesDir lbi
</> ModuleName.toFilePath (autogenModuleName pkg) <.> "hs"
rewriteFile pathsModulePath (Build.PathsModule.generate pkg lbi)
let cppHeaderPath = autogenModulesDir lbi </> cppHeaderName
rewriteFile cppHeaderPath (Build.Macros.generate pkg lbi)