Clean up code, add comments.
[hadrian.git] / src / Oracles / Config / Setting.hs
1 module Oracles.Config.Setting (
2 Setting (..), SettingList (..),
3 setting, settingList, getSetting, getSettingList,
4 targetPlatform, targetPlatforms, targetOs, targetOss, notTargetOs,
5 targetArchs, windowsHost, notWindowsHost, ghcWithInterpreter,
6 ghcEnableTablesNextToCode, cmdLineLengthLimit
7 ) where
8
9 import Base
10 import Stage
11 import Oracles.Config
12
13 -- Each Setting comes from the system.config file, e.g. 'target-os = mingw32'.
14 -- setting TargetOs looks up the config file and returns "mingw32".
15 --
16 -- SettingList is used for multiple string values separated by spaces, such
17 -- as 'gmp-include-dirs = a b'.
18 -- settingList GmpIncludeDirs therefore returns a list of strings ["a", "b"].
19 data Setting = TargetOs
20 | TargetArch
21 | TargetPlatformFull
22 | HostOsCpp
23 | DynamicExtension
24 | ProjectVersion
25 | GhcSourcePath
26
27 data SettingList = ConfCcArgs Stage
28 | ConfGccLinkerArgs Stage
29 | ConfLdLinkerArgs Stage
30 | ConfCppArgs Stage
31 | IconvIncludeDirs
32 | IconvLibDirs
33 | GmpIncludeDirs
34 | GmpLibDirs
35
36 setting :: Setting -> Action String
37 setting key = askConfig $ case key of
38 TargetOs -> "target-os"
39 TargetArch -> "target-arch"
40 TargetPlatformFull -> "target-platform-full"
41 HostOsCpp -> "host-os-cpp"
42 DynamicExtension -> "dynamic-extension"
43 ProjectVersion -> "project-version"
44 GhcSourcePath -> "ghc-source-path"
45
46 settingList :: SettingList -> Action [String]
47 settingList key = fmap words $ askConfig $ case key of
48 ConfCcArgs stage -> "conf-cc-args-stage" ++ show stage
49 ConfCppArgs stage -> "conf-cpp-args-stage" ++ show stage
50 ConfGccLinkerArgs stage -> "conf-gcc-linker-args-stage" ++ show stage
51 ConfLdLinkerArgs stage -> "conf-ld-linker-args-stage" ++ show stage
52 IconvIncludeDirs -> "iconv-include-dirs"
53 IconvLibDirs -> "iconv-lib-dirs"
54 GmpIncludeDirs -> "gmp-include-dirs"
55 GmpLibDirs -> "gmp-lib-dirs"
56
57 getSetting :: Setting -> ReaderT a Action String
58 getSetting = lift . setting
59
60 getSettingList :: SettingList -> ReaderT a Action [String]
61 getSettingList = lift . settingList
62
63 matchSetting :: Setting -> [String] -> Action Bool
64 matchSetting key values = do
65 value <- setting key
66 return $ value `elem` values
67
68 targetPlatforms :: [String] -> Action Bool
69 targetPlatforms = matchSetting TargetPlatformFull
70
71 targetPlatform :: String -> Action Bool
72 targetPlatform s = targetPlatforms [s]
73
74 targetOss :: [String] -> Action Bool
75 targetOss = matchSetting TargetOs
76
77 targetOs :: String -> Action Bool
78 targetOs s = targetOss [s]
79
80 notTargetOs :: String -> Action Bool
81 notTargetOs = fmap not . targetOs
82
83 targetArchs :: [String] -> Action Bool
84 targetArchs = matchSetting TargetArch
85
86 windowsHost :: Action Bool
87 windowsHost = do
88 hostOsCpp <- setting HostOsCpp
89 return $ hostOsCpp `elem` ["mingw32", "cygwin32"]
90
91 notWindowsHost :: Action Bool
92 notWindowsHost = fmap not windowsHost
93
94 ghcWithInterpreter :: Action Bool
95 ghcWithInterpreter = do
96 goodOs <- targetOss [ "mingw32", "cygwin32", "linux", "solaris2"
97 , "freebsd", "dragonfly", "netbsd", "openbsd"
98 , "darwin", "kfreebsdgnu" ]
99 goodArch <- targetArchs [ "i386", "x86_64", "powerpc", "sparc"
100 , "sparc64", "arm" ]
101 return $ goodOs && goodArch
102
103 ghcEnableTablesNextToCode :: Action Bool
104 ghcEnableTablesNextToCode = targetArchs ["ia64", "powerpc64"]
105
106 -- Command lines have limited size on Windows. Since Windows 7 the limit is
107 -- 32768 characters (theoretically). In practice we use 31000 to leave some
108 -- breathing space for the builder's path & name, auxiliary flags, and other
109 -- overheads. Use this function to set limits for other OSs if necessary.
110 cmdLineLengthLimit :: Action Int
111 cmdLineLengthLimit = do
112 windows <- windowsHost
113 return $ if windows
114 then 31000
115 else 4194304 -- Cabal needs a bit more than 2MB!