forked from commercialhaskell/stack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCache.hs
More file actions
398 lines (368 loc) · 15.4 KB
/
Cache.hs
File metadata and controls
398 lines (368 loc) · 15.4 KB
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
{-# LANGUAGE CPP #-}
{-# LANGUAGE NoImplicitPrelude #-}
{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE TemplateHaskell #-}
{-# LANGUAGE TupleSections #-}
{-# LANGUAGE ConstraintKinds #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE FlexibleContexts #-}
{-# OPTIONS_GHC -fno-warn-orphans #-}
-- | Cache information about previous builds
module Stack.Build.Cache
( tryGetBuildCache
, tryGetConfigCache
, tryGetCabalMod
, getInstalledExes
, tryGetFlagCache
, deleteCaches
, markExeInstalled
, markExeNotInstalled
, writeFlagCache
, writeBuildCache
, writeConfigCache
, writeCabalMod
, setTestSuccess
, unsetTestSuccess
, checkTestSuccess
, writePrecompiledCache
, readPrecompiledCache
-- Exported for testing
, BuildCache(..)
) where
import Stack.Prelude
import Crypto.Hash (hashWith, SHA256(..))
import Control.Monad.Trans.Maybe
import qualified Data.ByteArray as Mem (convert)
import qualified Data.ByteString.Base64.URL as B64URL
import qualified Data.ByteString as B
import qualified Data.ByteString.Char8 as S8
#ifdef mingw32_HOST_OS
import Data.Char (ord)
#endif
import qualified Data.Map as M
import qualified Data.Set as Set
import qualified Data.Store as Store
import Data.Store.VersionTagged
import qualified Data.Text as T
import qualified Data.Text.Encoding as TE
import Path
import Path.IO
import Stack.Constants.Config
import Stack.Types.Build
import Stack.Types.BuildPlan
import Stack.Types.Compiler
import Stack.Types.Config
import Stack.Types.GhcPkgId
import Stack.Types.NamedComponent
import Stack.Types.Package
import Stack.Types.PackageIdentifier
import Stack.Types.Version
import qualified System.FilePath as FP
-- | Directory containing files to mark an executable as installed
exeInstalledDir :: (MonadReader env m, HasEnvConfig env, MonadThrow m)
=> InstallLocation -> m (Path Abs Dir)
exeInstalledDir Snap = (</> $(mkRelDir "installed-packages")) `liftM` installationRootDeps
exeInstalledDir Local = (</> $(mkRelDir "installed-packages")) `liftM` installationRootLocal
-- | Get all of the installed executables
getInstalledExes :: (MonadReader env m, HasEnvConfig env, MonadIO m, MonadThrow m)
=> InstallLocation -> m [PackageIdentifier]
getInstalledExes loc = do
dir <- exeInstalledDir loc
(_, files) <- liftIO $ handleIO (const $ return ([], [])) $ listDir dir
return $
concat $
M.elems $
-- If there are multiple install records (from a stack version
-- before https://github.com/commercialhaskell/stack/issues/2373
-- was fixed), then we don't know which is correct - ignore them.
M.fromListWith (\_ _ -> []) $
map (\x -> (packageIdentifierName x, [x])) $
mapMaybe (parsePackageIdentifierFromString . toFilePath . filename) files
-- | Mark the given executable as installed
markExeInstalled :: (MonadReader env m, HasEnvConfig env, MonadIO m, MonadThrow m)
=> InstallLocation -> PackageIdentifier -> m ()
markExeInstalled loc ident = do
dir <- exeInstalledDir loc
ensureDir dir
ident' <- parseRelFile $ packageIdentifierString ident
let fp = toFilePath $ dir </> ident'
-- Remove old install records for this package.
-- TODO: This is a bit in-efficient. Put all this metadata into one file?
installed <- getInstalledExes loc
forM_ (filter (\x -> packageIdentifierName ident == packageIdentifierName x) installed)
(markExeNotInstalled loc)
-- TODO consideration for the future: list all of the executables
-- installed, and invalidate this file in getInstalledExes if they no
-- longer exist
liftIO $ B.writeFile fp "Installed"
-- | Mark the given executable as not installed
markExeNotInstalled :: (MonadReader env m, HasEnvConfig env, MonadIO m, MonadThrow m)
=> InstallLocation -> PackageIdentifier -> m ()
markExeNotInstalled loc ident = do
dir <- exeInstalledDir loc
ident' <- parseRelFile $ packageIdentifierString ident
liftIO $ ignoringAbsence (removeFile $ dir </> ident')
buildCacheFile :: (HasEnvConfig env, MonadReader env m, MonadThrow m)
=> Path Abs Dir
-> NamedComponent
-> m (Path Abs File)
buildCacheFile dir component = do
cachesDir <- buildCachesDir dir
let nonLibComponent prefix name = prefix <> "-" <> T.unpack name
cacheFileName <- parseRelFile $ case component of
CLib -> "lib"
CInternalLib name -> nonLibComponent "internal-lib" name
CExe name -> nonLibComponent "exe" name
CTest name -> nonLibComponent "test" name
CBench name -> nonLibComponent "bench" name
return $ cachesDir </> cacheFileName
-- | Try to read the dirtiness cache for the given package directory.
tryGetBuildCache :: HasEnvConfig env
=> Path Abs Dir
-> NamedComponent
-> RIO env (Maybe (Map FilePath FileCacheInfo))
tryGetBuildCache dir component = liftM (fmap buildCacheTimes) . $(versionedDecodeFile buildCacheVC) =<< buildCacheFile dir component
-- | Try to read the dirtiness cache for the given package directory.
tryGetConfigCache :: HasEnvConfig env
=> Path Abs Dir -> RIO env (Maybe ConfigCache)
tryGetConfigCache dir = $(versionedDecodeFile configCacheVC) =<< configCacheFile dir
-- | Try to read the mod time of the cabal file from the last build
tryGetCabalMod :: HasEnvConfig env
=> Path Abs Dir -> RIO env (Maybe ModTime)
tryGetCabalMod dir = $(versionedDecodeFile modTimeVC) =<< configCabalMod dir
-- | Write the dirtiness cache for this package's files.
writeBuildCache :: HasEnvConfig env
=> Path Abs Dir
-> NamedComponent
-> Map FilePath FileCacheInfo -> RIO env ()
writeBuildCache dir component times = do
fp <- buildCacheFile dir component
$(versionedEncodeFile buildCacheVC) fp BuildCache
{ buildCacheTimes = times
}
-- | Write the dirtiness cache for this package's configuration.
writeConfigCache :: HasEnvConfig env
=> Path Abs Dir
-> ConfigCache
-> RIO env ()
writeConfigCache dir x = do
fp <- configCacheFile dir
$(versionedEncodeFile configCacheVC) fp x
-- | See 'tryGetCabalMod'
writeCabalMod :: HasEnvConfig env
=> Path Abs Dir
-> ModTime
-> RIO env ()
writeCabalMod dir x = do
fp <- configCabalMod dir
$(versionedEncodeFile modTimeVC) fp x
-- | Delete the caches for the project.
deleteCaches :: (MonadIO m, MonadReader env m, HasEnvConfig env, MonadThrow m)
=> Path Abs Dir -> m ()
deleteCaches dir = do
{- FIXME confirm that this is acceptable to remove
bfp <- buildCacheFile dir
removeFileIfExists bfp
-}
cfp <- configCacheFile dir
liftIO $ ignoringAbsence (removeFile cfp)
flagCacheFile :: (MonadIO m, MonadThrow m, MonadReader env m, HasEnvConfig env)
=> Installed
-> m (Path Abs File)
flagCacheFile installed = do
rel <- parseRelFile $
case installed of
Library _ gid _ -> ghcPkgIdString gid
Executable ident -> packageIdentifierString ident
dir <- flagCacheLocal
return $ dir </> rel
-- | Loads the flag cache for the given installed extra-deps
tryGetFlagCache :: HasEnvConfig env
=> Installed
-> RIO env (Maybe ConfigCache)
tryGetFlagCache gid = do
fp <- flagCacheFile gid
$(versionedDecodeFile configCacheVC) fp
writeFlagCache :: HasEnvConfig env
=> Installed
-> ConfigCache
-> RIO env ()
writeFlagCache gid cache = do
file <- flagCacheFile gid
ensureDir (parent file)
$(versionedEncodeFile configCacheVC) file cache
-- | Mark a test suite as having succeeded
setTestSuccess :: HasEnvConfig env
=> Path Abs Dir
-> RIO env ()
setTestSuccess dir = do
fp <- testSuccessFile dir
$(versionedEncodeFile testSuccessVC) fp True
-- | Mark a test suite as not having succeeded
unsetTestSuccess :: HasEnvConfig env
=> Path Abs Dir
-> RIO env ()
unsetTestSuccess dir = do
fp <- testSuccessFile dir
$(versionedEncodeFile testSuccessVC) fp False
-- | Check if the test suite already passed
checkTestSuccess :: HasEnvConfig env
=> Path Abs Dir
-> RIO env Bool
checkTestSuccess dir =
liftM
(fromMaybe False)
($(versionedDecodeFile testSuccessVC) =<< testSuccessFile dir)
--------------------------------------
-- Precompiled Cache
--
-- Idea is simple: cache information about packages built in other snapshots,
-- and then for identical matches (same flags, config options, dependencies)
-- just copy over the executables and reregister the libraries.
--------------------------------------
-- | The file containing information on the given package/configuration
-- combination. The filename contains a hash of the non-directory configure
-- options for quick lookup if there's a match.
--
-- It also returns an action yielding the location of the precompiled
-- path based on the old binary encoding.
--
-- We only pay attention to non-directory options. We don't want to avoid a
-- cache hit just because it was installed in a different directory.
precompiledCacheFile :: HasEnvConfig env
=> PackageLocationIndex FilePath
-> ConfigureOpts
-> Set GhcPkgId -- ^ dependencies
-> RIO env (Maybe (Path Abs File))
precompiledCacheFile loc copts installedPackageIDs = do
ec <- view envConfigL
compiler <- view actualCompilerVersionL >>= parseRelDir . compilerVersionString
cabal <- view cabalVersionL >>= parseRelDir . versionString
let mpkgRaw =
-- The goal here is to come up with a string representing the
-- package location which is unique. For archives and repos,
-- we rely upon cryptographic hashes paired with
-- subdirectories to identify this specific package version.
case loc of
PLIndex pir -> Just $ packageIdentifierRevisionString pir
PLOther other -> case other of
PLFilePath _ -> assert False Nothing -- no PLFilePaths should end up in a snapshot
PLArchive a -> fmap
(\h -> T.unpack (staticSHA256ToText h) ++ archiveSubdirs a)
(archiveHash a)
PLRepo r -> Just $ T.unpack (repoCommit r) ++ repoSubdirs r
forM mpkgRaw $ \pkgRaw -> do
platformRelDir <- platformGhcRelDir
let precompiledDir =
view stackRootL ec
</> $(mkRelDir "precompiled")
</> platformRelDir
</> compiler
</> cabal
pkg <-
case parseRelDir pkgRaw of
Just x -> return x
Nothing -> parseRelDir
$ T.unpack
$ TE.decodeUtf8
$ B64URL.encode
$ TE.encodeUtf8
$ T.pack pkgRaw
-- In Cabal versions 1.22 and later, the configure options contain the
-- installed package IDs, which is what we need for a unique hash.
-- Unfortunately, earlier Cabals don't have the information, so we must
-- supplement it with the installed package IDs directly.
-- See issue: https://github.com/commercialhaskell/stack/issues/1103
let input = (coNoDirs copts, installedPackageIDs)
hashPath <- parseRelFile $ S8.unpack $ B64URL.encode
$ Mem.convert $ hashWith SHA256 $ Store.encode input
let longPath = precompiledDir </> pkg </> hashPath
-- See #3649 - shorten the paths on windows if MAX_PATH will be
-- violated. Doing this only when necessary allows use of existing
-- precompiled packages.
if pathTooLong (toFilePath longPath) then do
shortPkg <- shaPath pkg
shortHash <- shaPath hashPath
return $ precompiledDir </> shortPkg </> shortHash
else
return longPath
-- | Write out information about a newly built package
writePrecompiledCache :: HasEnvConfig env
=> BaseConfigOpts
-> PackageLocationIndex FilePath
-> ConfigureOpts
-> Set GhcPkgId -- ^ dependencies
-> Installed -- ^ library
-> [GhcPkgId] -- ^ sublibraries, in the GhcPkgId format
-> Set Text -- ^ executables
-> RIO env ()
writePrecompiledCache baseConfigOpts loc copts depIDs mghcPkgId sublibs exes = do
mfile <- precompiledCacheFile loc copts depIDs
forM_ mfile $ \file -> do
ensureDir (parent file)
ec <- view envConfigL
let stackRootRelative = makeRelative (view stackRootL ec)
mlibpath <- case mghcPkgId of
Executable _ -> return Nothing
Library _ ipid _ -> liftM Just $ pathFromPkgId stackRootRelative ipid
sublibpaths <- mapM (pathFromPkgId stackRootRelative) sublibs
exes' <- forM (Set.toList exes) $ \exe -> do
name <- parseRelFile $ T.unpack exe
relPath <- stackRootRelative $ bcoSnapInstallRoot baseConfigOpts </> bindirSuffix </> name
return $ toFilePath relPath
$(versionedEncodeFile precompiledCacheVC) file PrecompiledCache
{ pcLibrary = mlibpath
, pcSubLibs = sublibpaths
, pcExes = exes'
}
where
pathFromPkgId stackRootRelative ipid = do
ipid' <- parseRelFile $ ghcPkgIdString ipid ++ ".conf"
relPath <- stackRootRelative $ bcoSnapDB baseConfigOpts </> ipid'
return $ toFilePath relPath
-- | Check the cache for a precompiled package matching the given
-- configuration.
readPrecompiledCache :: forall env. HasEnvConfig env
=> PackageLocationIndex FilePath -- ^ target package
-> ConfigureOpts
-> Set GhcPkgId -- ^ dependencies
-> RIO env (Maybe PrecompiledCache)
readPrecompiledCache loc copts depIDs = runMaybeT $
MaybeT (precompiledCacheFile loc copts depIDs) >>=
MaybeT . $(versionedDecodeFile precompiledCacheVC) >>=
lift . mkAbs
where
-- Since commit ed9ccc08f327bad68dd2d09a1851ce0d055c0422,
-- pcLibrary paths are stored as relative to the stack
-- root. Therefore, we need to prepend the stack root when
-- checking that the file exists. For the older cached paths, the
-- file will contain an absolute path, which will make `stackRoot
-- </>` a no-op.
mkAbs :: PrecompiledCache -> RIO env PrecompiledCache
mkAbs pc0 = do
stackRoot <- view stackRootL
let mkAbs' = (toFilePath stackRoot FP.</>)
return PrecompiledCache
{ pcLibrary = mkAbs' <$> pcLibrary pc0
, pcSubLibs = mkAbs' <$> pcSubLibs pc0
, pcExes = mkAbs' <$> pcExes pc0
}
-- | Check if a filesystem path is too long.
pathTooLong :: FilePath -> Bool
#ifdef mingw32_HOST_OS
pathTooLong path = utf16StringLength path >= win32MaxPath
where
win32MaxPath = 260
-- Calculate the length of a string in 16-bit units
-- if it were converted to utf-16.
utf16StringLength :: String -> Integer
utf16StringLength = sum . map utf16CharLength
where
utf16CharLength c | ord c < 0x10000 = 1
| otherwise = 2
#else
pathTooLong _ = False
#endif