/
Source.hs
477 lines (444 loc) · 18.9 KB
/
Source.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
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
75
76
77
78
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
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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
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
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
{-# LANGUAGE NoImplicitPrelude #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE TupleSections #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE ConstraintKinds #-}
-- Load information on package sources
module Stack.Build.Source
( loadSourceMap
, loadSourceMapFull
, SourceMap
, getLocalFlags
, getGhcOptions
, addUnlistedToBuildCache
, getDefaultPackageConfig
) where
import Stack.Prelude
import Crypto.Hash (Digest, SHA256(..))
import Crypto.Hash.Conduit (sinkHash)
import qualified Data.ByteArray as Mem (convert)
import qualified Data.ByteString as S
import Data.Conduit (($$), ZipSink (..))
import qualified Data.Conduit.Binary as CB
import qualified Data.Conduit.List as CL
import qualified Data.HashSet as HashSet
import Data.List
import qualified Data.Map as Map
import qualified Data.Map.Strict as M
import qualified Data.Set as Set
import Stack.Build.Cache
import Stack.Build.Target
import Stack.Config (getLocalPackages)
import Stack.Constants (wiredInPackages)
import Stack.Package
import Stack.Types.Build
import Stack.Types.BuildPlan
import Stack.Types.Config
import Stack.Types.FlagName
import Stack.Types.Package
import Stack.Types.PackageName
import qualified System.Directory as D
import System.FilePath (takeFileName)
import System.IO.Error (isDoesNotExistError)
-- | Like 'loadSourceMapFull', but doesn't return values that aren't as
-- commonly needed.
loadSourceMap :: HasEnvConfig env
=> NeedTargets
-> BuildOptsCLI
-> RIO env ([LocalPackage], SourceMap)
loadSourceMap needTargets boptsCli = do
(_, _, locals, _, sourceMap) <- loadSourceMapFull needTargets boptsCli
return (locals, sourceMap)
-- | Given the build commandline options, does the following:
--
-- * Parses the build targets.
--
-- * Loads the 'LoadedSnapshot' from the resolver, with extra-deps
-- shadowing any packages that should be built locally.
--
-- * Loads up the 'LocalPackage' info.
--
-- * Builds a 'SourceMap', which contains info for all the packages that
-- will be involved in the build.
loadSourceMapFull :: HasEnvConfig env
=> NeedTargets
-> BuildOptsCLI
-> RIO env
( Map PackageName Target
, LoadedSnapshot
, [LocalPackage]
, Set PackageName -- non-project targets
, SourceMap
)
loadSourceMapFull needTargets boptsCli = do
bconfig <- view buildConfigL
(ls, localDeps, targets) <- parseTargets needTargets boptsCli
lp <- getLocalPackages
locals <- mapM (loadLocalPackage boptsCli targets) $ Map.toList $ lpProject lp
checkFlagsUsed boptsCli locals localDeps (lsPackages ls)
checkComponentsBuildable locals
-- TODO for extra sanity, confirm that the targets we threw away are all TargetAll
let nonProjectTargets = Map.keysSet targets `Set.difference` Map.keysSet (lpProject lp)
-- Combine the local packages, extra-deps, and LoadedSnapshot into
-- one unified source map.
let sourceMap = Map.unions
[ Map.fromList $ map (\lp' -> (packageName $ lpPackage lp', PSLocal lp')) locals
, flip Map.mapWithKey localDeps $ \n lpi ->
let configOpts = getGhcOptions bconfig boptsCli n False False
-- NOTE: configOpts includes lpiGhcOptions for now, this may get refactored soon
in PSUpstream (lpiVersion lpi) Local (lpiFlags lpi) configOpts (lpiLocation lpi)
, flip Map.mapWithKey (lsPackages ls) $ \n lpi ->
let configOpts = getGhcOptions bconfig boptsCli n False False
-- NOTE: configOpts includes lpiGhcOptions for now, this may get refactored soon
in PSUpstream (lpiVersion lpi) Snap (lpiFlags lpi) configOpts (lpiLocation lpi)
]
`Map.difference` Map.fromList (map (, ()) (HashSet.toList wiredInPackages))
return
( targets
, ls
, locals
, nonProjectTargets
, sourceMap
)
-- | All flags for a local package.
getLocalFlags
:: BuildConfig
-> BuildOptsCLI
-> PackageName
-> Map FlagName Bool
getLocalFlags bconfig boptsCli name = Map.unions
[ Map.findWithDefault Map.empty (Just name) cliFlags
, Map.findWithDefault Map.empty Nothing cliFlags
, Map.findWithDefault Map.empty name (bcFlags bconfig)
]
where
cliFlags = boptsCLIFlags boptsCli
-- | Get the configured options to pass from GHC, based on the build
-- configuration and commandline.
getGhcOptions :: BuildConfig -> BuildOptsCLI -> PackageName -> Bool -> Bool -> [Text]
getGhcOptions bconfig boptsCli name isTarget isLocal = concat
[ Map.findWithDefault [] name (configGhcOptionsByName config)
, configGhcOptionsAll config
, concat [["-fhpc"] | isLocal && toCoverage (boptsTestOpts bopts)]
, if boptsLibProfile bopts || boptsExeProfile bopts
then ["-auto-all","-caf-all"]
else []
, if not $ boptsLibStrip bopts || boptsExeStrip bopts
then ["-g"]
else []
, if includeExtraOptions
then boptsCLIGhcOptions boptsCli
else []
]
where
bopts = configBuild config
config = view configL bconfig
includeExtraOptions =
case configApplyGhcOptions config of
AGOTargets -> isTarget
AGOLocals -> isLocal
AGOEverything -> True
splitComponents :: [NamedComponent]
-> (Set Text, Set Text, Set Text)
splitComponents =
go id id id
where
go a b c [] = (Set.fromList $ a [], Set.fromList $ b [], Set.fromList $ c [])
go a b c (CLib:xs) = go a b c xs
go a b c (CExe x:xs) = go (a . (x:)) b c xs
go a b c (CTest x:xs) = go a (b . (x:)) c xs
go a b c (CBench x:xs) = go a b (c . (x:)) xs
-- | Upgrade the initial local package info to a full-blown @LocalPackage@
-- based on the selected components
loadLocalPackage
:: forall env. HasEnvConfig env
=> BuildOptsCLI
-> Map PackageName Target
-> (PackageName, LocalPackageView)
-> RIO env LocalPackage
loadLocalPackage boptsCli targets (name, lpv) = do
let mtarget = Map.lookup name targets
config <- getPackageConfig boptsCli name (isJust mtarget) True
bopts <- view buildOptsL
let (exeCandidates, testCandidates, benchCandidates) =
case mtarget of
Just (TargetComps comps) -> splitComponents $ Set.toList comps
Just (TargetAll packageType) -> assert (packageType == ProjectPackage)
( packageExes pkg
, if boptsTests bopts
then Map.keysSet (packageTests pkg)
else Set.empty
, if boptsBenchmarks bopts
then packageBenchmarks pkg
else Set.empty
)
Nothing -> mempty
-- See https://github.com/commercialhaskell/stack/issues/2862
isWanted = case mtarget of
Nothing -> False
-- FIXME: When issue #1406 ("stack 0.1.8 lost ability to
-- build individual executables or library") is resolved,
-- 'packageHasLibrary' is only relevant if the library is
-- part of the target spec.
Just _ -> packageHasLibrary pkg || not (Set.null allComponents)
filterSkippedComponents = Set.filter (not . (`elem` boptsSkipComponents bopts))
(exes, tests, benches) = (filterSkippedComponents exeCandidates,
filterSkippedComponents testCandidates,
filterSkippedComponents benchCandidates)
allComponents = toComponents exes tests benches
toComponents e t b = Set.unions
[ Set.map CExe e
, Set.map CTest t
, Set.map CBench b
]
btconfig = config
{ packageConfigEnableTests = not $ Set.null tests
, packageConfigEnableBenchmarks = not $ Set.null benches
}
testconfig = config
{ packageConfigEnableTests = True
, packageConfigEnableBenchmarks = False
}
benchconfig = config
{ packageConfigEnableTests = False
, packageConfigEnableBenchmarks = True
}
-- We resolve the package in 4 different configurations:
--
-- - pkg doesn't have tests or benchmarks enabled.
--
-- - btpkg has them enabled if they are present.
--
-- - testpkg has tests enabled, but not benchmarks.
--
-- - benchpkg has benchmarks enablde, but not tests.
--
-- The latter two configurations are used to compute the deps
-- when --enable-benchmarks or --enable-tests are configured.
-- This allows us to do an optimization where these are passed
-- if the deps are present. This can avoid doing later
-- unnecessary reconfigures.
gpkg = lpvGPD lpv
pkg = resolvePackage config gpkg
btpkg
| Set.null tests && Set.null benches = Nothing
| otherwise = Just (resolvePackage btconfig gpkg)
testpkg = resolvePackage testconfig gpkg
benchpkg = resolvePackage benchconfig gpkg
mbuildCache <- tryGetBuildCache $ lpvRoot lpv
(files,_) <- getPackageFilesSimple pkg (lpvCabalFP lpv)
(dirtyFiles, newBuildCache) <- checkBuildCache
(fromMaybe Map.empty mbuildCache)
(Set.toList files)
return LocalPackage
{ lpPackage = pkg
, lpTestDeps = packageDeps testpkg
, lpBenchDeps = packageDeps benchpkg
, lpTestBench = btpkg
, lpFiles = files
, lpForceDirty = boptsForceDirty bopts
, lpDirtyFiles =
if not (Set.null dirtyFiles)
then let tryStripPrefix y =
fromMaybe y (stripPrefix (toFilePath $ lpvRoot lpv) y)
in Just $ Set.map tryStripPrefix dirtyFiles
else Nothing
, lpNewBuildCache = newBuildCache
, lpCabalFile = lpvCabalFP lpv
, lpDir = lpvRoot lpv
, lpWanted = isWanted
, lpComponents = allComponents
-- TODO: refactor this so that it's easier to be sure that these
-- components are indeed unbuildable.
--
-- The reasoning here is that if the STLocalComps specification
-- made it through component parsing, but the components aren't
-- present, then they must not be buildable.
, lpUnbuildable = toComponents
(exes `Set.difference` packageExes pkg)
(tests `Set.difference` Map.keysSet (packageTests pkg))
(benches `Set.difference` packageBenchmarks pkg)
}
-- | Ensure that the flags specified in the stack.yaml file and on the command
-- line are used.
checkFlagsUsed :: (MonadThrow m, MonadReader env m, HasBuildConfig env)
=> BuildOptsCLI
-> [LocalPackage]
-> Map PackageName (LoadedPackageInfo (PackageLocationIndex FilePath)) -- ^ local deps
-> Map PackageName snapshot -- ^ snapshot, for error messages
-> m ()
checkFlagsUsed boptsCli lps extraDeps snapshot = do
bconfig <- view buildConfigL
-- Check if flags specified in stack.yaml and the command line are
-- used, see https://github.com/commercialhaskell/stack/issues/617
let flags = map (, FSCommandLine) [(k, v) | (Just k, v) <- Map.toList $ boptsCLIFlags boptsCli]
++ map (, FSStackYaml) (Map.toList $ bcFlags bconfig)
localNameMap = Map.fromList $ map (packageName . lpPackage &&& lpPackage) lps
checkFlagUsed ((name, userFlags), source) =
case Map.lookup name localNameMap of
-- Package is not available locally
Nothing ->
if Map.member name extraDeps
-- We don't check for flag presence for extra deps
then Nothing
-- Also not in extra-deps, it's an error
else
case Map.lookup name snapshot of
Nothing -> Just $ UFNoPackage source name
Just _ -> Just $ UFSnapshot name
-- Package exists locally, let's check if the flags are defined
Just pkg ->
let unused = Set.difference (Map.keysSet userFlags) (packageDefinedFlags pkg)
in if Set.null unused
-- All flags are defined, nothing to do
then Nothing
-- Error about the undefined flags
else Just $ UFFlagsNotDefined source pkg unused
unusedFlags = mapMaybe checkFlagUsed flags
unless (null unusedFlags)
$ throwM
$ InvalidFlagSpecification
$ Set.fromList unusedFlags
-- | Compare the current filesystem state to the cached information, and
-- determine (1) if the files are dirty, and (2) the new cache values.
checkBuildCache :: forall m. (MonadIO m)
=> Map FilePath FileCacheInfo -- ^ old cache
-> [Path Abs File] -- ^ files in package
-> m (Set FilePath, Map FilePath FileCacheInfo)
checkBuildCache oldCache files = do
fileTimes <- liftM Map.fromList $ forM files $ \fp -> do
mmodTime <- liftIO (getModTimeMaybe (toFilePath fp))
return (toFilePath fp, mmodTime)
liftM (mconcat . Map.elems) $ sequence $
Map.mergeWithKey
(\fp mmodTime fci -> Just (go fp mmodTime (Just fci)))
(Map.mapWithKey (\fp mmodTime -> go fp mmodTime Nothing))
(Map.mapWithKey (\fp fci -> go fp Nothing (Just fci)))
fileTimes
oldCache
where
go :: FilePath -> Maybe ModTime -> Maybe FileCacheInfo -> m (Set FilePath, Map FilePath FileCacheInfo)
-- Filter out the cabal_macros file to avoid spurious recompilations
go fp _ _ | takeFileName fp == "cabal_macros.h" = return (Set.empty, Map.empty)
-- Common case where it's in the cache and on the filesystem.
go fp (Just modTime') (Just fci)
| fciModTime fci == modTime' = return (Set.empty, Map.empty)
| otherwise = do
newFci <- calcFci modTime' fp
let isDirty =
fciSize fci /= fciSize newFci ||
fciHash fci /= fciHash newFci
newDirty = if isDirty then Set.singleton fp else Set.empty
return (newDirty, Map.singleton fp newFci)
-- Missing file. Add it to dirty files, but no FileCacheInfo.
go fp Nothing _ = return (Set.singleton fp, Map.empty)
-- Missing cache. Add it to dirty files and compute FileCacheInfo.
go fp (Just modTime') Nothing = do
newFci <- calcFci modTime' fp
return (Set.singleton fp, Map.singleton fp newFci)
-- | Returns entries to add to the build cache for any newly found unlisted modules
addUnlistedToBuildCache
:: HasEnvConfig env
=> ModTime
-> Package
-> Path Abs File
-> Map FilePath a
-> RIO env ([Map FilePath FileCacheInfo], [PackageWarning])
addUnlistedToBuildCache preBuildTime pkg cabalFP buildCache = do
(files,warnings) <- getPackageFilesSimple pkg cabalFP
let newFiles =
Set.toList $
Set.map toFilePath files `Set.difference` Map.keysSet buildCache
addBuildCache <- mapM addFileToCache newFiles
return (addBuildCache, warnings)
where
addFileToCache fp = do
mmodTime <- getModTimeMaybe fp
case mmodTime of
Nothing -> return Map.empty
Just modTime' ->
if modTime' < preBuildTime
then do
newFci <- calcFci modTime' fp
return (Map.singleton fp newFci)
else return Map.empty
-- | Gets list of Paths for files in a package
getPackageFilesSimple
:: HasEnvConfig env
=> Package -> Path Abs File -> RIO env (Set (Path Abs File), [PackageWarning])
getPackageFilesSimple pkg cabalFP = do
(_,compFiles,cabalFiles,warnings) <-
getPackageFiles (packageFiles pkg) cabalFP
return
( Set.map dotCabalGetPath (mconcat (M.elems compFiles)) <> cabalFiles
, warnings)
-- | Get file modification time, if it exists.
getModTimeMaybe :: MonadIO m => FilePath -> m (Maybe ModTime)
getModTimeMaybe fp =
liftIO
(catch
(liftM
(Just . modTime)
(D.getModificationTime fp))
(\e ->
if isDoesNotExistError e
then return Nothing
else throwM e))
-- | Create FileCacheInfo for a file.
calcFci :: MonadIO m => ModTime -> FilePath -> m FileCacheInfo
calcFci modTime' fp = liftIO $
withBinaryFile fp ReadMode $ \h -> do
(size, digest) <- CB.sourceHandle h $$ getZipSink
((,)
<$> ZipSink (CL.fold
(\x y -> x + fromIntegral (S.length y))
0)
<*> ZipSink sinkHash)
return FileCacheInfo
{ fciModTime = modTime'
, fciSize = size
, fciHash = Mem.convert (digest :: Digest SHA256)
}
checkComponentsBuildable :: MonadThrow m => [LocalPackage] -> m ()
checkComponentsBuildable lps =
unless (null unbuildable) $ throwM $ SomeTargetsNotBuildable unbuildable
where
unbuildable =
[ (packageName (lpPackage lp), c)
| lp <- lps
, c <- Set.toList (lpUnbuildable lp)
]
getDefaultPackageConfig :: (MonadIO m, MonadReader env m, HasEnvConfig env)
=> m PackageConfig
getDefaultPackageConfig = do
platform <- view platformL
compilerVersion <- view actualCompilerVersionL
return PackageConfig
{ packageConfigEnableTests = False
, packageConfigEnableBenchmarks = False
, packageConfigFlags = M.empty
, packageConfigGhcOptions = []
, packageConfigCompilerVersion = compilerVersion
, packageConfigPlatform = platform
}
-- | Get 'PackageConfig' for package given its name.
getPackageConfig :: (MonadIO m, MonadReader env m, HasEnvConfig env)
=> BuildOptsCLI
-> PackageName
-> Bool
-> Bool
-> m PackageConfig
getPackageConfig boptsCli name isTarget isLocal = do
bconfig <- view buildConfigL
platform <- view platformL
compilerVersion <- view actualCompilerVersionL
return PackageConfig
{ packageConfigEnableTests = False
, packageConfigEnableBenchmarks = False
, packageConfigFlags = getLocalFlags bconfig boptsCli name
, packageConfigGhcOptions = getGhcOptions bconfig boptsCli name isTarget isLocal
, packageConfigCompilerVersion = compilerVersion
, packageConfigPlatform = platform
}