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