module GHC.Settings where
import Prelude
import GHC.BaseDir
import GHC.Platform
import Data.Char (isSpace)
import Data.Map (Map)
import qualified Data.Map as Map
getTargetPlatform
:: FilePath -> RawSettings -> Either String Platform
getTargetPlatform settingsFile mySettings = do
let
getBooleanSetting = getBooleanSetting0 settingsFile mySettings
readSetting :: (Show a, Read a) => String -> Either String a
readSetting = readSetting0 settingsFile mySettings
targetArch <- readSetting "target arch"
targetOS <- readSetting "target os"
targetWordSize <- readSetting "target word size"
targetUnregisterised <- getBooleanSetting "Unregisterised"
targetHasGnuNonexecStack <- getBooleanSetting "target has GNU nonexec stack"
targetHasIdentDirective <- getBooleanSetting "target has .ident directive"
targetHasSubsectionsViaSymbols <- getBooleanSetting "target has subsections via symbols"
crossCompiling <- getBooleanSetting "cross compiling"
pure $ Platform
{ platformMini = PlatformMini
{ platformMini_arch = targetArch
, platformMini_os = targetOS
}
, platformWordSize = targetWordSize
, platformUnregisterised = targetUnregisterised
, platformHasGnuNonexecStack = targetHasGnuNonexecStack
, platformHasIdentDirective = targetHasIdentDirective
, platformHasSubsectionsViaSymbols = targetHasSubsectionsViaSymbols
, platformIsCrossCompiling = crossCompiling
}
type RawSettings = Map String String
getSetting0
:: FilePath -> RawSettings -> String -> Either String String
getSetting0 settingsFile mySettings key = case Map.lookup key mySettings of
Just xs -> Right xs
Nothing -> Left $ "No entry for " ++ show key ++ " in " ++ show settingsFile
getFilePathSetting0
:: FilePath -> FilePath -> RawSettings -> String -> Either String String
getFilePathSetting0 top_dir settingsFile mySettings key =
expandTopDir top_dir <$> getSetting0 settingsFile mySettings key
getBooleanSetting0
:: FilePath -> RawSettings -> String -> Either String Bool
getBooleanSetting0 settingsFile mySettings key = do
rawValue <- getSetting0 settingsFile mySettings key
case rawValue of
"YES" -> Right True
"NO" -> Right False
xs -> Left $ "Bad value for " ++ show key ++ ": " ++ show xs
readSetting0
:: (Show a, Read a) => FilePath -> RawSettings -> String -> Either String a
readSetting0 settingsFile mySettings key = case Map.lookup key mySettings of
Just xs -> case maybeRead xs of
Just v -> Right v
Nothing -> Left $ "Failed to read " ++ show key ++ " value " ++ show xs
Nothing -> Left $ "No entry for " ++ show key ++ " in " ++ show settingsFile
maybeRead :: Read a => String -> Maybe a
maybeRead str = case reads str of
[(x, "")] -> Just x
_ -> Nothing
maybeReadFuzzy :: Read a => String -> Maybe a
maybeReadFuzzy str = case reads str of
[(x, s)] | all isSpace s -> Just x
_ -> Nothing