8bdc3876aff599d55d1fd675e959226d8ffe224f
[hadrian.git] / src / Oracles / Config / Setting.hs
1 module Oracles.Config.Setting (
2 Setting (..), SettingList (..), setting, settingList, getSetting,
3 getSettingList, anyTargetPlatform, anyTargetOs, anyTargetArch, anyHostOs,
4 ghcWithInterpreter, ghcEnableTablesNextToCode, useLibFFIForAdjustors,
5 ghcCanonVersion, cmdLineLengthLimit, iosHost, osxHost, windowsHost,
6 relocatableBuild, installDocDir, installGhcLibDir
7 ) where
8
9 import Control.Monad.Trans.Reader
10
11 import Base
12 import Oracles.Config
13 import Stage
14
15 -- TODO: Reduce the variety of similar flags (e.g. CPP and non-CPP versions).
16 -- | Each 'Setting' comes from @system.config@ file, e.g. 'target-os = mingw32'.
17 -- @setting TargetOs@ looks up the config file and returns "mingw32".
18 -- 'SettingList' is used for multiple string values separated by spaces, such
19 -- as @gmp-include-dirs = a b@.
20 -- @settingList GmpIncludeDirs@ therefore returns a list of strings ["a", "b"].
21 data Setting = BuildArch
22 | BuildOs
23 | BuildPlatform
24 | BuildVendor
25 | CcClangBackend
26 | CcLlvmBackend
27 | DynamicExtension
28 | GhcMajorVersion
29 | GhcMinorVersion
30 | GhcPatchLevel
31 | GhcVersion
32 | GhcSourcePath
33 | HostArch
34 | HostOs
35 | HostPlatform
36 | HostVendor
37 | ProjectGitCommitId
38 | ProjectName
39 | ProjectVersion
40 | ProjectVersionInt
41 | ProjectPatchLevel
42 | ProjectPatchLevel1
43 | ProjectPatchLevel2
44 | TargetArch
45 | TargetOs
46 | TargetPlatform
47 | TargetPlatformFull
48 | TargetVendor
49 | FfiIncludeDir
50 | FfiLibDir
51 | GmpIncludeDir
52 | GmpLibDir
53 | IconvIncludeDir
54 | IconvLibDir
55 -- Paths to where GHC is installed
56 | InstallPrefix
57 | InstallBinDir
58 | InstallLibDir
59 | InstallDataRootDir
60 -- "install" utility
61 | Install
62 | InstallData
63 | InstallProgram
64 | InstallScript
65 | InstallDir
66 -- symbolic link
67 | LnS
68
69 data SettingList = ConfCcArgs Stage
70 | ConfCppArgs Stage
71 | ConfGccLinkerArgs Stage
72 | ConfLdLinkerArgs Stage
73 | HsCppArgs
74
75 setting :: Setting -> Action String
76 setting key = unsafeAskConfig $ case key of
77 BuildArch -> "build-arch"
78 BuildOs -> "build-os"
79 BuildPlatform -> "build-platform"
80 BuildVendor -> "build-vendor"
81 CcClangBackend -> "cc-clang-backend"
82 CcLlvmBackend -> "cc-llvm-backend"
83 DynamicExtension -> "dynamic-extension"
84 GhcMajorVersion -> "ghc-major-version"
85 GhcMinorVersion -> "ghc-minor-version"
86 GhcPatchLevel -> "ghc-patch-level"
87 GhcVersion -> "ghc-version"
88 GhcSourcePath -> "ghc-source-path"
89 HostArch -> "host-arch"
90 HostOs -> "host-os"
91 HostPlatform -> "host-platform"
92 HostVendor -> "host-vendor"
93 ProjectGitCommitId -> "project-git-commit-id"
94 ProjectName -> "project-name"
95 ProjectVersion -> "project-version"
96 ProjectVersionInt -> "project-version-int"
97 ProjectPatchLevel -> "project-patch-level"
98 ProjectPatchLevel1 -> "project-patch-level1"
99 ProjectPatchLevel2 -> "project-patch-level2"
100 TargetArch -> "target-arch"
101 TargetOs -> "target-os"
102 TargetPlatform -> "target-platform"
103 TargetPlatformFull -> "target-platform-full"
104 TargetVendor -> "target-vendor"
105 FfiIncludeDir -> "ffi-include-dir"
106 FfiLibDir -> "ffi-lib-dir"
107 GmpIncludeDir -> "gmp-include-dir"
108 GmpLibDir -> "gmp-lib-dir"
109 IconvIncludeDir -> "iconv-include-dir"
110 IconvLibDir -> "iconv-lib-dir"
111 InstallPrefix -> "install-prefix"
112 InstallBinDir -> "install-bindir"
113 InstallLibDir -> "install-libdir"
114 InstallDataRootDir -> "install-datarootdir"
115 Install -> "install"
116 InstallDir -> "install-dir"
117 InstallProgram -> "install-program"
118 InstallScript -> "install-script"
119 InstallData -> "install-data"
120 LnS -> "ln-s"
121
122 settingList :: SettingList -> Action [String]
123 settingList key = fmap words $ unsafeAskConfig $ case key of
124 ConfCcArgs stage -> "conf-cc-args-" ++ stageString stage
125 ConfCppArgs stage -> "conf-cpp-args-" ++ stageString stage
126 ConfGccLinkerArgs stage -> "conf-gcc-linker-args-" ++ stageString stage
127 ConfLdLinkerArgs stage -> "conf-ld-linker-args-" ++ stageString stage
128 HsCppArgs -> "hs-cpp-args"
129
130 getSetting :: Setting -> ReaderT a Action String
131 getSetting = lift . setting
132
133 getSettingList :: SettingList -> ReaderT a Action [String]
134 getSettingList = lift . settingList
135
136 matchSetting :: Setting -> [String] -> Action Bool
137 matchSetting key values = fmap (`elem` values) $ setting key
138
139 anyTargetPlatform :: [String] -> Action Bool
140 anyTargetPlatform = matchSetting TargetPlatformFull
141
142 anyTargetOs :: [String] -> Action Bool
143 anyTargetOs = matchSetting TargetOs
144
145 anyTargetArch :: [String] -> Action Bool
146 anyTargetArch = matchSetting TargetArch
147
148 anyHostOs :: [String] -> Action Bool
149 anyHostOs = matchSetting HostOs
150
151 iosHost :: Action Bool
152 iosHost = anyHostOs ["ios"]
153
154 osxHost :: Action Bool
155 osxHost = anyHostOs ["darwin"]
156
157 windowsHost :: Action Bool
158 windowsHost = anyHostOs ["mingw32", "cygwin32"]
159
160 ghcWithInterpreter :: Action Bool
161 ghcWithInterpreter = do
162 goodOs <- anyTargetOs [ "mingw32", "cygwin32", "linux", "solaris2"
163 , "freebsd", "dragonfly", "netbsd", "openbsd"
164 , "darwin", "kfreebsdgnu" ]
165 goodArch <- anyTargetArch [ "i386", "x86_64", "powerpc", "sparc"
166 , "sparc64", "arm" ]
167 return $ goodOs && goodArch
168
169 ghcEnableTablesNextToCode :: Action Bool
170 ghcEnableTablesNextToCode = notM $ anyTargetArch ["ia64", "powerpc64", "powerpc64le"]
171
172 useLibFFIForAdjustors :: Action Bool
173 useLibFFIForAdjustors = notM $ anyTargetArch ["i386", "x86_64"]
174
175 -- | Canonicalised GHC version number, used for integer version comparisons. We
176 -- expand GhcMinorVersion to two digits by adding a leading zero if necessary.
177 ghcCanonVersion :: Action String
178 ghcCanonVersion = do
179 ghcMajorVersion <- setting GhcMajorVersion
180 ghcMinorVersion <- setting GhcMinorVersion
181 let leadingZero = [ '0' | length ghcMinorVersion == 1 ]
182 return $ ghcMajorVersion ++ leadingZero ++ ghcMinorVersion
183
184 -- | Command lines have limited size on Windows. Since Windows 7 the limit is
185 -- 32768 characters (theoretically). In practice we use 31000 to leave some
186 -- breathing space for the builder's path & name, auxiliary flags, and other
187 -- overheads. Use this function to set limits for other OSs if necessary.
188 cmdLineLengthLimit :: Action Int
189 cmdLineLengthLimit = do
190 windows <- windowsHost
191 osx <- osxHost
192 return $ case (windows, osx) of
193 -- Windows:
194 (True, False) -> 31000
195 -- On Mac OSX ARG_MAX is 262144, yet when using @xargs@ on OSX this is
196 -- reduced by over 20 000. Hence, 200 000 seems like a sensible limit.
197 (False, True) -> 200000
198 -- On all other systems, we try this:
199 _ -> 4194304 -- Cabal library needs a bit more than 2MB!
200
201 -- | On Windows we normally want to make a relocatable bindist,
202 -- to we ignore flags like libdir
203 -- ref: mk/config.mk.in:232
204 relocatableBuild :: Action Bool
205 relocatableBuild = windowsHost
206
207 installDocDir :: Action String
208 installDocDir = do
209 version <- setting ProjectVersion
210 (-/- ("doc/ghc-" ++ version)) <$> setting InstallDataRootDir
211
212 -- | Unix: override libdir and datadir to put ghc-specific stuff in
213 -- a subdirectory with the version number included.
214 -- ref: mk/install.mk:101
215 -- TODO: CroosCompilePrefix
216 installGhcLibDir :: Action String
217 installGhcLibDir = do
218 r <- relocatableBuild
219 libdir <- setting InstallLibDir
220 if r then return libdir
221 else do
222 v <- setting ProjectVersion
223 return (libdir -/- ("ghc-" ++ v))