module GHC.Settings.Platform where
import Prelude
import GHC.BaseDir
import GHC.Platform
import GHC.Settings.Utils
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"
targetWordBigEndian <- getBooleanSetting "target word big endian"
targetLeadingUnderscore <- getBooleanSetting "Leading underscore"
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"
tablesNextToCode <- getBooleanSetting "Tables next to code"
pure $ Platform
{ platformMini = PlatformMini
{ platformMini_arch = targetArch
, platformMini_os = targetOS
}
, platformWordSize = targetWordSize
, platformByteOrder = if targetWordBigEndian then BigEndian else LittleEndian
, platformUnregisterised = targetUnregisterised
, platformHasGnuNonexecStack = targetHasGnuNonexecStack
, platformHasIdentDirective = targetHasIdentDirective
, platformHasSubsectionsViaSymbols = targetHasSubsectionsViaSymbols
, platformIsCrossCompiling = crossCompiling
, platformLeadingUnderscore = targetLeadingUnderscore
, platformTablesNextToCode = tablesNextToCode
}
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