Base.hs 8.44 KB
Newer Older
Andrey Mokhov's avatar
Andrey Mokhov committed
1
2
{-# LANGUAGE FlexibleInstances #-}

3
module Expression.Base (
Andrey Mokhov's avatar
Andrey Mokhov committed
4
5
    Guard,
    Settings,
6
7
8
9
10
    ArgList (..),
    Ways, targetWays,
    remove,
    opt, opts, subSettings, optBuildPath, optBuildDist, optPath,
    optBootPkgConstraints,
Andrey Mokhov's avatar
Andrey Mokhov committed
11
    packages, package, setPackage,
12
    builders, builder, setBuilder, setBuilderFamily,
Andrey Mokhov's avatar
Andrey Mokhov committed
13
14
15
    stages, stage, notStage, setStage,
    ways, way, setWay,
    files, file, setFile,
16
17
18
19
20
21
22
    optKeyValue, optBuilder, optStagedBuilder,
    crossCompiling,
    keyValues, keyValue, keyYes, keyNo, keyNonEmpty, setKeyValue,
    packageKey, packageDeps, packageDepKeys,
    supportsPackageKey, targetPlatforms, targetPlatform,
    targetOss, targetArchs,
    dynamicGhcPrograms, ghcWithInterpreter
Andrey Mokhov's avatar
Andrey Mokhov committed
23
24
25
26
27
28
29
30
    ) where

import Base
import Ways
import Package.Base (Package)
import Oracles.Builder
import Expression.PG
import Expression.Predicate
31
32
import Expression.PGPredicate
-- import Expression.ArgList
Andrey Mokhov's avatar
Andrey Mokhov committed
33
34
35
36
37
38
39
40
41
42

data BuildParameter = WhenPackage  Package
                    | WhenBuilder  Builder
                    | WhenStage    Stage
                    | WhenWay      Way
                    | WhenFile     FilePattern
                    | WhenKeyValue String String -- from config files

type Guard = Predicate BuildParameter

43
type Matcher = TruthTeller BuildParameter
Andrey Mokhov's avatar
Andrey Mokhov committed
44

45
type Expression v = PGPredicate BuildParameter v
Andrey Mokhov's avatar
Andrey Mokhov committed
46

47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
type Settings = Expression ArgList

type Ways = Expression Way

data ArgList = Plain [String]
             | Complex String Settings
             | Path String Settings
             | BuildPath
             | BuildDist
             | BootPkgConstraints
             | KeyValue String
             | BuilderPath Builder
             | PackageKey String
             | PackageDeps String
             | PackageDepKeys String

plain :: String -> ArgList
plain s = Plain [s]

subSettings :: String -> Settings -> Settings
subSettings prefix = Vertex . Complex prefix

type ArgsTeller = ArgList -> Maybe [String]

-- Monoid instance for args-tellers (asks them one by one)
instance Monoid ArgsTeller where
    mempty        = const Nothing
    p `mappend` q = \a -> getFirst $ First (p a) <> First (q a)
Andrey Mokhov's avatar
Andrey Mokhov committed
75

76
77
78
fromPlain :: ArgsTeller
fromPlain (Plain list) = Just list
fromPlain _            = Nothing
Andrey Mokhov's avatar
Andrey Mokhov committed
79

80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
tellArgs :: ArgsTeller -> ArgList -> ArgList
tellArgs t a = case t a of
    Just list -> Plain list
    Nothing   -> a

-- TODO: move to Targets.hs
targetWays :: Ways
targetWays = Vertex vanilla
    <> notStage Stage0 ? Vertex profiling
    <> platformSupportsSharedLibs ? Vertex dynamic

-- TODO: rename to 'arg' and args'
opt :: String -> Settings
opt = Vertex . plain

opts :: [String] -> Settings
opts = mconcat . map opt

optBuildPath, optBuildDist, optBootPkgConstraints :: Settings
optBuildPath          = Vertex $ BuildPath
optBuildDist          = Vertex $ BuildDist
optBootPkgConstraints = Vertex $ BootPkgConstraints

optPath :: String -> Settings -> Settings
optPath prefix = Vertex . Path prefix

---- Extract all plain and unconditional arguments.
---- Overlay subexpressions are evaluated in arbitrary order.
--plainArgs :: PGPredicate p v -> [v]
--plainArgs Epsilon         = []
--plainArgs (Vertex (Plain args)) = args
--plainArgs (Vertex _)            = []
--plainArgs (Overlay   l r) = (++) <$> plainArgs l <*> plainArgs r -- TODO: union
--plainArgs (Sequence  l r) = (++) <$> plainArgs l <*> plainArgs r
--plainArgs (Condition x r) = case tellTruth x of
--    Just True -> plainArgs r
--    _         -> []

-- Partially evaluate Settings using a truth-teller (compute a 'projection')
project :: Matcher -> Settings -> Settings
project _ Epsilon         = Epsilon
project m (Vertex v)  = case v of
    Complex l r -> Vertex $ Complex l (project m r)
    _           -> Vertex v
project m (Overlay   l r) = Overlay   (project  m l) (project m r)
project m (Sequence  l r) = Sequence  (project  m l) (project m r)
project m (Condition l r) = Condition (evaluate m l) (project m r)

remove :: [String] -> Settings -> Settings
remove _ Epsilon         = Epsilon
remove a v @ (Vertex (Plain b))
    | a == b    = Epsilon
    | otherwise = v
remove _ v @ (Vertex _)  = v
remove a (Overlay   l r) = Overlay   (remove a l) (remove a r)
remove a (Sequence  l r) = Sequence  (remove a l) (remove a r)
remove a (Condition x r) = Condition x            (remove a r)
Andrey Mokhov's avatar
Andrey Mokhov committed
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182


alternatives :: (a -> BuildParameter) -> [a] -> Guard
alternatives p = multiOr . map (Parameter . p)

-- Basic GHC build guards

packages :: [Package] -> Guard
packages = alternatives WhenPackage

builders :: [Builder] -> Guard
builders = alternatives WhenBuilder

stages :: [Stage] -> Guard
stages = alternatives WhenStage

ways :: [Way] -> Guard
ways = alternatives WhenWay

files :: [FilePattern] -> Guard
files = alternatives WhenFile

keyValues :: String -> [String] -> Guard
keyValues key = alternatives (WhenKeyValue key)

package :: Package -> Guard
package p = packages [p]

builder :: Builder -> Guard
builder b = builders [b]

stage :: Stage -> Guard
stage s = stages [s]

notStage :: Stage -> Guard
notStage = Not . Parameter . WhenStage

way :: Way -> Guard
way w = ways [w]

file :: FilePattern -> Guard
file f = files [f]

keyValue :: String -> String -> Guard
keyValue key value = keyValues key [value]

183
184
185
186
keyYes, keyNo, keyNonEmpty :: String -> Guard
keyYes      key = keyValues key ["YES"]
keyNo       key = keyValues key ["NO" ]
keyNonEmpty key = Not $ keyValues key [""]
Andrey Mokhov's avatar
Andrey Mokhov committed
187
188
189
190
191
192
193
194
195

-- Partial evaluation of settings

setPackage :: Package -> Settings -> Settings
setPackage = project . matchPackage

setBuilder :: Builder -> Settings -> Settings
setBuilder = project . matchBuilder

196
197
198
setBuilderFamily :: (Stage -> Builder) -> Settings -> Settings
setBuilderFamily f = mconcat $ map (setBuilder . f) [Stage0 ..]

Andrey Mokhov's avatar
Andrey Mokhov committed
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
setStage :: Stage -> Settings -> Settings
setStage = project . matchStage

setWay :: Way -> Settings -> Settings
setWay = project . matchWay

setFile :: FilePath -> Settings -> Settings
setFile = project . matchFile

setKeyValue :: String -> String -> Settings -> Settings
setKeyValue key = project . matchKeyValue key

-- Truth-tellers for partial evaluation

matchPackage :: Package -> Matcher
matchPackage p (WhenPackage p') = Just $ p == p'
matchPackage _ _                = Nothing

matchBuilder :: Builder -> Matcher
matchBuilder b (WhenBuilder b') = Just $ b == b'
matchBuilder _ _                = Nothing

matchStage :: Stage -> Matcher
matchStage s (WhenStage s') = Just $ s == s'
matchStage _ _              = Nothing

matchWay :: Way -> Matcher
matchWay w (WhenWay w') = Just $ w == w'
matchWay _ _            = Nothing

matchFile :: FilePath -> Matcher
matchFile file (WhenFile pattern) = Just $ pattern ?== file
matchFile _ _                     = Nothing

matchKeyValue :: String -> String -> Matcher
matchKeyValue key value (WhenKeyValue key' value')
    | key == key' = Just $ value == value'
    | otherwise   = Nothing
matchKeyValue _ _ _ = Nothing

-- Argument templates

241
242
243
244
245
246
247
248
249
250
optKeyValue :: String -> Settings
optKeyValue = Vertex . KeyValue

optBuilder :: Builder -> Settings
optBuilder = Vertex . BuilderPath

optStagedBuilder :: (Stage -> Builder) -> Settings
optStagedBuilder f =
    mconcat $ map (\s -> builder (f s) ? optBuilder (f s)) [Stage0 ..]

Andrey Mokhov's avatar
Andrey Mokhov committed
251
252
253
254
255
256
257
258
packageKey :: String -> Settings
packageKey = Vertex . PackageKey

packageDeps :: String -> Settings
packageDeps = Vertex . PackageDeps

packageDepKeys :: String -> Settings
packageDepKeys = Vertex . PackageDepKeys
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301

-- Derived guards

supportsPackageKey :: Guard
supportsPackageKey = keyYes "supports-package-key"

targetPlatforms :: [String] -> Guard
targetPlatforms = keyValues "target-platform-full"

targetPlatform :: String -> Guard
targetPlatform s = targetPlatforms [s]

targetOss :: [String] -> Guard
targetOss = keyValues "target-os"

targetArchs :: [String] -> Guard
targetArchs = keyValues "target-arch"

solarisBrokenShld :: Guard
solarisBrokenShld = keyYes "solaris-broken-shld"

platformSupportsSharedLibs :: Guard
platformSupportsSharedLibs =
    Not $ (targetPlatforms [ "powerpc-unknown-linux"
                           , "x86_64-unknown-mingw32"
                           , "i386-unknown-mingw32" ]
           `Or`
           solarisBrokenShld `And` targetPlatform "i386-unknown-solaris2")

dynamicGhcPrograms :: Guard
dynamicGhcPrograms = keyYes "dynamic-ghc-programs"

ghcWithInterpreter :: Guard
ghcWithInterpreter =
    targetOss [ "mingw32", "cygwin32", "linux", "solaris2"
              , "freebsd", "dragonfly", "netbsd", "openbsd"
              , "darwin", "kfreebsdgnu" ]
    `And`
    targetArchs ["i386", "x86_64", "powerpc", "sparc", "sparc64", "arm"]

crossCompiling :: Guard
crossCompiling = keyYes "cross-compiling"