module Distribution.Simple.ShowBuildInfo (mkBuildInfo) where
import Distribution.Compat.Prelude
import Prelude ()
import qualified Distribution.Simple.GHC as GHC
import qualified Distribution.Simple.Program.GHC as GHC
import Distribution.PackageDescription
import Distribution.Compiler
import Distribution.Verbosity
import Distribution.Simple.Compiler
import Distribution.Simple.LocalBuildInfo
import Distribution.Simple.Program
import Distribution.Simple.Setup
import Distribution.Simple.Utils (cabalVersion)
import Distribution.Simple.Utils.Json
import Distribution.Types.TargetInfo
import Distribution.Text
import Distribution.Pretty
import Distribution.Utils.Path
mkBuildInfo
:: PackageDescription
-> LocalBuildInfo
-> BuildFlags
-> [TargetInfo]
-> Json
mkBuildInfo :: PackageDescription
-> LocalBuildInfo -> BuildFlags -> [TargetInfo] -> Json
mkBuildInfo PackageDescription
pkg_descr LocalBuildInfo
lbi BuildFlags
_flags [TargetInfo]
targetsToBuild = Json
info
where
targetToNameAndLBI :: TargetInfo -> (ComponentName, ComponentLocalBuildInfo)
targetToNameAndLBI TargetInfo
target =
(ComponentLocalBuildInfo -> ComponentName
componentLocalName forall a b. (a -> b) -> a -> b
$ TargetInfo -> ComponentLocalBuildInfo
targetCLBI TargetInfo
target, TargetInfo -> ComponentLocalBuildInfo
targetCLBI TargetInfo
target)
componentsToBuild :: [(ComponentName, ComponentLocalBuildInfo)]
componentsToBuild = forall a b. (a -> b) -> [a] -> [b]
map TargetInfo -> (ComponentName, ComponentLocalBuildInfo)
targetToNameAndLBI [TargetInfo]
targetsToBuild
(.=) :: String -> Json -> (String, Json)
String
k .= :: String -> Json -> (String, Json)
.= Json
v = (String
k, Json
v)
info :: Json
info = [(String, Json)] -> Json
JsonObject
[ String
"cabal-version" String -> Json -> (String, Json)
.= String -> Json
JsonString (forall a. Pretty a => a -> String
display Version
cabalVersion)
, String
"compiler" String -> Json -> (String, Json)
.= Json
mkCompilerInfo
, String
"components" String -> Json -> (String, Json)
.= [Json] -> Json
JsonArray (forall a b. (a -> b) -> [a] -> [b]
map (ComponentName, ComponentLocalBuildInfo) -> Json
mkComponentInfo [(ComponentName, ComponentLocalBuildInfo)]
componentsToBuild)
]
mkCompilerInfo :: Json
mkCompilerInfo = [(String, Json)] -> Json
JsonObject
[ String
"flavour" String -> Json -> (String, Json)
.= String -> Json
JsonString (forall a. Pretty a => a -> String
prettyShow forall a b. (a -> b) -> a -> b
$ Compiler -> CompilerFlavor
compilerFlavor forall a b. (a -> b) -> a -> b
$ LocalBuildInfo -> Compiler
compiler LocalBuildInfo
lbi)
, String
"compiler-id" String -> Json -> (String, Json)
.= String -> Json
JsonString (Compiler -> String
showCompilerId forall a b. (a -> b) -> a -> b
$ LocalBuildInfo -> Compiler
compiler LocalBuildInfo
lbi)
, String
"path" String -> Json -> (String, Json)
.= Json
path
]
where
path :: Json
path = forall b a. b -> (a -> b) -> Maybe a -> b
maybe Json
JsonNull (String -> Json
JsonString forall b c a. (b -> c) -> (a -> b) -> a -> c
. ConfiguredProgram -> String
programPath)
forall a b. (a -> b) -> a -> b
$ (CompilerFlavor -> Maybe Program
flavorToProgram forall b c a. (b -> c) -> (a -> b) -> a -> c
. Compiler -> CompilerFlavor
compilerFlavor forall a b. (a -> b) -> a -> b
$ LocalBuildInfo -> Compiler
compiler LocalBuildInfo
lbi)
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= forall a b c. (a -> b -> c) -> b -> a -> c
flip Program -> ProgramDb -> Maybe ConfiguredProgram
lookupProgram (LocalBuildInfo -> ProgramDb
withPrograms LocalBuildInfo
lbi)
flavorToProgram :: CompilerFlavor -> Maybe Program
flavorToProgram :: CompilerFlavor -> Maybe Program
flavorToProgram CompilerFlavor
GHC = forall a. a -> Maybe a
Just Program
ghcProgram
flavorToProgram CompilerFlavor
GHCJS = forall a. a -> Maybe a
Just Program
ghcjsProgram
flavorToProgram CompilerFlavor
UHC = forall a. a -> Maybe a
Just Program
uhcProgram
flavorToProgram CompilerFlavor
JHC = forall a. a -> Maybe a
Just Program
jhcProgram
flavorToProgram CompilerFlavor
_ = forall a. Maybe a
Nothing
mkComponentInfo :: (ComponentName, ComponentLocalBuildInfo) -> Json
mkComponentInfo (ComponentName
name, ComponentLocalBuildInfo
clbi) = [(String, Json)] -> Json
JsonObject
[ String
"type" String -> Json -> (String, Json)
.= String -> Json
JsonString String
compType
, String
"name" String -> Json -> (String, Json)
.= String -> Json
JsonString (forall a. Pretty a => a -> String
prettyShow ComponentName
name)
, String
"unit-id" String -> Json -> (String, Json)
.= String -> Json
JsonString (forall a. Pretty a => a -> String
prettyShow forall a b. (a -> b) -> a -> b
$ ComponentLocalBuildInfo -> UnitId
componentUnitId ComponentLocalBuildInfo
clbi)
, String
"compiler-args" String -> Json -> (String, Json)
.= [Json] -> Json
JsonArray (forall a b. (a -> b) -> [a] -> [b]
map String -> Json
JsonString forall a b. (a -> b) -> a -> b
$ BuildInfo -> LocalBuildInfo -> ComponentLocalBuildInfo -> [String]
getCompilerArgs BuildInfo
bi LocalBuildInfo
lbi ComponentLocalBuildInfo
clbi)
, String
"modules" String -> Json -> (String, Json)
.= [Json] -> Json
JsonArray (forall a b. (a -> b) -> [a] -> [b]
map (String -> Json
JsonString forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. Pretty a => a -> String
display) [ModuleName]
modules)
, String
"src-files" String -> Json -> (String, Json)
.= [Json] -> Json
JsonArray (forall a b. (a -> b) -> [a] -> [b]
map String -> Json
JsonString [String]
sourceFiles)
, String
"src-dirs" String -> Json -> (String, Json)
.= [Json] -> Json
JsonArray (forall a b. (a -> b) -> [a] -> [b]
map String -> Json
JsonString forall a b. (a -> b) -> a -> b
$ forall a b. (a -> b) -> [a] -> [b]
map forall from to. SymbolicPath from to -> String
getSymbolicPath forall a b. (a -> b) -> a -> b
$ BuildInfo -> [SymbolicPath PackageDir SourceDir]
hsSourceDirs BuildInfo
bi)
]
where
bi :: BuildInfo
bi = Component -> BuildInfo
componentBuildInfo Component
comp
comp :: Component
comp = forall a. a -> Maybe a -> a
fromMaybe (forall a. HasCallStack => String -> a
error forall a b. (a -> b) -> a -> b
$ String
"mkBuildInfo: no component " forall a. [a] -> [a] -> [a]
++ forall a. Pretty a => a -> String
prettyShow ComponentName
name) forall a b. (a -> b) -> a -> b
$ PackageDescription -> ComponentName -> Maybe Component
lookupComponent PackageDescription
pkg_descr ComponentName
name
compType :: String
compType = case Component
comp of
CLib Library
_ -> String
"lib"
CExe Executable
_ -> String
"exe"
CTest TestSuite
_ -> String
"test"
CBench Benchmark
_ -> String
"bench"
CFLib ForeignLib
_ -> String
"flib"
modules :: [ModuleName]
modules = case Component
comp of
CLib Library
lib -> Library -> [ModuleName]
explicitLibModules Library
lib
CExe Executable
exe -> Executable -> [ModuleName]
exeModules Executable
exe
Component
_ -> []
sourceFiles :: [String]
sourceFiles = case Component
comp of
CLib Library
_ -> []
CExe Executable
exe -> [Executable -> String
modulePath Executable
exe]
Component
_ -> []
getCompilerArgs
:: BuildInfo
-> LocalBuildInfo
-> ComponentLocalBuildInfo
-> [String]
getCompilerArgs :: BuildInfo -> LocalBuildInfo -> ComponentLocalBuildInfo -> [String]
getCompilerArgs BuildInfo
bi LocalBuildInfo
lbi ComponentLocalBuildInfo
clbi =
case Compiler -> CompilerFlavor
compilerFlavor forall a b. (a -> b) -> a -> b
$ LocalBuildInfo -> Compiler
compiler LocalBuildInfo
lbi of
CompilerFlavor
GHC -> [String]
ghc
CompilerFlavor
GHCJS -> [String]
ghc
CompilerFlavor
c -> forall a. HasCallStack => String -> a
error forall a b. (a -> b) -> a -> b
$ String
"ShowBuildInfo.getCompilerArgs: Don't know how to get "forall a. [a] -> [a] -> [a]
++
String
"build arguments for compiler "forall a. [a] -> [a] -> [a]
++forall a. Show a => a -> String
show CompilerFlavor
c
where
ghc :: [String]
ghc = Compiler -> Platform -> GhcOptions -> [String]
GHC.renderGhcOptions (LocalBuildInfo -> Compiler
compiler LocalBuildInfo
lbi) (LocalBuildInfo -> Platform
hostPlatform LocalBuildInfo
lbi) GhcOptions
baseOpts
where
baseOpts :: GhcOptions
baseOpts = Verbosity
-> LocalBuildInfo
-> BuildInfo
-> ComponentLocalBuildInfo
-> String
-> GhcOptions
GHC.componentGhcOptions Verbosity
normal LocalBuildInfo
lbi BuildInfo
bi ComponentLocalBuildInfo
clbi (LocalBuildInfo -> String
buildDir LocalBuildInfo
lbi)