module Coolbal.Target.Executable (makeExeTarget) where import Data.List (find) import Distribution.Compiler (perCompilerFlavorToList, CompilerFlavor(GHC)) import qualified Distribution.ModuleName as Module import Distribution.Types.BuildInfo (BuildInfo(..)) import Distribution.Types.Executable (Executable(..)) import Distribution.Types.ExecutableScope (ExecutableScope(ExecutablePublic)) import Distribution.Types.UnqualComponentName (unUnqualComponentName) import Language.Haskell.Extension (knownLanguages) import Coolbal.CabalPlan import Coolbal.Directory import Coolbal.Target makeExeTarget :: Executable -> CabalPlan -> Maybe ExeTarget makeExeTarget exe plan | exeScope exe == ExecutablePublic , let bi = buildInfo exe , buildable bi , all (null . ($ bi)) [asmSources, cmmSources, cSources, cxxSources, jsSources] , all (null . ($ bi)) [virtualModules, autogenModules] , null (defaultExtensions bi) , all (null . ($ bi)) [extraLibs, extraBundledLibs] , let name = unUnqualComponentName (exeName exe) , Just planpkg@Configured{} <- find ((== name) . ppName) (planPackages plan) , Just language <- defaultLanguage bi , True <- language `elem` knownLanguages , Just flags <- lookup GHC (perCompilerFlavorToList (options bi)) = Just (ExeTarget { exeTargetName = unUnqualComponentName (exeName exe) , exeTargetPkgDbDir = currentHomeDirectory ++ "/.cabal/store/" ++ planCompiler plan ++ "/package.db" , exeTargetDeps = ppDepends planpkg , exeTargetLanguage = show language , exeTargetMain = modulePath exe , exeTargetSrcDirs = hsSourceDirs bi , exeTargetModules = map Module.components (otherModules bi) , exeTargetFlags = flags }) | otherwise = Nothing