Skip to content

Commit cd29859

Browse files
committed
Refactor
Rename `BuildCache` as `BuildFileCache`. Rename field of `BuildFileCache` as `fileCache` (`times` was a misnomer). Move `FileCacheInfo` from `Stack.Types.Package` to be co-located with `BuildFileCache`. Use type synonym `FileCache`. Move `toCachePkgSrc` to `Stack.Build.ConstructPlan` (the only place it is used). Move functions from `Stack.Types.ConfigureOpts` to `Stack.ConfigureOpts`, to avoid cyclic module dependencies. Rename `configureOpts` to `configureOptsFromBase` to avoid clash with `ConfigCache` field name.
1 parent 9549918 commit cd29859

File tree

12 files changed

+298
-273
lines changed

12 files changed

+298
-273
lines changed

package.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,7 @@ library:
201201
- Stack.Config.ConfigureScript
202202
- Stack.Config.Docker
203203
- Stack.Config.Nix
204+
- Stack.ConfigureOpts
204205
- Stack.ConfigCmd
205206
- Stack.Constants
206207
- Stack.Constants.Config

src/Stack/Build/Cache.hs

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ module Stack.Build.Cache
3333
, writePrecompiledCache
3434
, readPrecompiledCache
3535
-- Exported for testing
36-
, BuildCache (..)
36+
, BuildFileCache (..)
3737
) where
3838

3939
import Crypto.Hash ( hashWith, SHA256 (..) )
@@ -63,8 +63,8 @@ import Stack.Storage.User
6363
, precompiledCacheKey, savePrecompiledCache
6464
)
6565
import Stack.Types.Cache
66-
( BuildCache (..), ConfigCache, ConfigCacheType (..)
67-
, PrecompiledCache (..)
66+
( BuildFileCache (..), ConfigCache, ConfigCacheType (..)
67+
, FileCache, PrecompiledCache (..)
6868
)
6969
import Stack.Types.CompilerPaths ( cabalVersionL )
7070
import Stack.Types.ComponentUtils
@@ -84,7 +84,6 @@ import Stack.Types.Installed
8484
)
8585
import Stack.Types.NamedComponent
8686
( NamedComponent (..), componentCachePath )
87-
import Stack.Types.Package ( FileCacheInfo )
8887
import Stack.Types.SourceMap ( smRelDir )
8988
import System.PosixCompat.Files
9089
( getFileStatus, modificationTime, setFileTimes )
@@ -167,13 +166,13 @@ tryGetBuildCache ::
167166
-- ^ Package directory.
168167
-> NamedComponent
169168
-- ^ Package component.
170-
-> RIO env (Maybe (Map FilePath FileCacheInfo))
169+
-> RIO env (Maybe FileCache)
171170
tryGetBuildCache dir component = do
172171
fp <- buildCacheFile dir component
173172
ensureDir $ parent fp
174-
let decode :: MonadIO m => m BuildCache
173+
let decode :: MonadIO m => m BuildFileCache
175174
decode = Yaml.decodeFileThrow (toFilePath fp)
176-
either (const Nothing) (Just . (.times)) <$> liftIO (tryAny decode)
175+
either (const Nothing) (Just . (.fileCache)) <$> liftIO (tryAny decode)
177176

178177
-- | Try to read the Cabal configuration cache for the given package directory.
179178
tryGetConfigCache ::
@@ -230,10 +229,12 @@ writeBuildCache ::
230229
-- ^ Package directory.
231230
-> NamedComponent
232231
-- ^ Package component.
233-
-> Map FilePath FileCacheInfo -> RIO env ()
234-
writeBuildCache dir component times = do
232+
-> FileCache
233+
-- ^ File cache.
234+
-> RIO env ()
235+
writeBuildCache dir component fileCache = do
235236
fp <- toFilePath <$> buildCacheFile dir component
236-
liftIO $ Yaml.encodeFile fp BuildCache { times = times }
237+
liftIO $ Yaml.encodeFile fp BuildFileCache { fileCache }
237238

238239
-- | Write the given Cabal configuration cache for the given package directory.
239240
writeConfigCache ::
@@ -250,6 +251,7 @@ writeConfigCache dir =
250251
writeCabalMod ::
251252
HasEnvConfig env
252253
=> Path Abs Dir
254+
-- ^ Package directory.
253255
-> CTime
254256
-> RIO env ()
255257
writeCabalMod dir x = do

src/Stack/Build/ConstructPlan.hs

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,10 @@ import RIO.Writer ( WriterT (..), pass, tell )
3434
import Stack.Build.Cache ( tryGetFlagCache )
3535
import Stack.Build.Haddock ( shouldHaddockDeps )
3636
import Stack.Build.Source ( loadLocalPackage )
37+
import Stack.ConfigureOpts
38+
( configureOptsFromBase, packageConfigureOptsFromPackage
39+
, renderConfigureOpts
40+
)
3741
import Stack.Constants ( compilerOptionsCabalFlag )
3842
import Stack.Package
3943
( applyForceCustomBuild, buildableExes, packageUnknownTools
@@ -56,16 +60,14 @@ import Stack.Types.BuildConfig
5660
import Stack.Types.BuildOpts ( BuildOpts (..) )
5761
import Stack.Types.BuildOptsCLI
5862
( BuildOptsCLI (..), BuildSubset (..) )
59-
import Stack.Types.Cache
60-
( CachePkgSrc (..), ConfigCache (..), toCachePkgSrc )
63+
import Stack.Types.Cache ( CachePkgSrc (..), ConfigCache (..) )
6164
import Stack.Types.CompCollection ( collectionMember )
6265
import Stack.Types.Compiler ( WhichCompiler (..), getGhcVersion )
6366
import Stack.Types.CompilerPaths
6467
( CompilerPaths (..), HasCompiler (..) )
6568
import Stack.Types.ComponentUtils ( unqualCompFromText )
6669
import Stack.Types.Config ( Config (..), HasConfig (..), stackRootL )
6770
import Stack.Types.ConfigureOpts ( BaseConfigOpts (..) )
68-
import qualified Stack.Types.ConfigureOpts as ConfigureOpts
6971
import Stack.Types.Curator ( Curator (..) )
7072
import Stack.Types.Dependency ( DepValue (..), isDepTypeLibrary )
7173
import Stack.Types.DumpPackage ( DumpPackage (..), sublibParentPkgId )
@@ -468,7 +470,7 @@ addFinal lp package allInOne buildHaddocks = do
468470
res <- addPackageDeps package >>= \case
469471
Left e -> pure $ Left e
470472
Right (MissingPresentDeps missing present _minLoc) -> do
471-
let pkgConfigOpts = ConfigureOpts.packageConfigureOptsFromPackage package
473+
let pkgConfigOpts = packageConfigureOptsFromPackage package
472474
ctx <- ask
473475
let configOpts = TaskConfigOpts
474476
{ missing
@@ -778,7 +780,7 @@ installPackageGivenDeps allInOne buildHaddocks ps package minstalled
778780
ctx <- ask
779781
let loc = psLocation ps
780782
isMutable = installLocationIsMutable loc <> minMutable
781-
pkgConfigOpts = ConfigureOpts.packageConfigureOptsFromPackage package
783+
pkgConfigOpts = packageConfigureOptsFromPackage package
782784
configOpts = TaskConfigOpts
783785
{ missing
784786
, envConfig = ctx.ctxEnvConfig
@@ -1041,9 +1043,8 @@ checkDirtiness ::
10411043
checkDirtiness ps installed package present buildHaddocks = do
10421044
ctx <- ask
10431045
moldOpts <- runRIO ctx $ tryGetFlagCache installed
1044-
let packageConfigureOpt =
1045-
ConfigureOpts.packageConfigureOptsFromPackage package
1046-
configureOpts = ConfigureOpts.configureOpts
1046+
let packageConfigureOpt = packageConfigureOptsFromPackage package
1047+
configureOpts = configureOptsFromBase
10471048
(view envConfigL ctx)
10481049
ctx.baseConfigOpts
10491050
present
@@ -1138,7 +1139,7 @@ describeConfigDiff config old new
11381139
then id
11391140
else stripGhcOptions)
11401141
. map T.pack
1141-
. ConfigureOpts.renderConfigureOpts
1142+
. renderConfigureOpts
11421143
. (.configureOpts)
11431144
where
11441145
-- options set by Stack
@@ -1286,3 +1287,8 @@ combineMap = Map.merge
12861287
(Map.mapMissing (\_ s -> PIOnlySource s))
12871288
(Map.mapMissing (\_ i -> uncurry PIOnlyInstalled i))
12881289
(Map.zipWithMatched (\_ s i -> combineSourceInstalled s i))
1290+
1291+
toCachePkgSrc :: PackageSource -> CachePkgSrc
1292+
toCachePkgSrc (PSFilePath lp) =
1293+
CacheSrcLocal (toFilePath (parent lp.cabalFP))
1294+
toCachePkgSrc PSRemote{} = CacheSrcUpstream

src/Stack/Build/ExecutePackage.hs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,8 @@ import Stack.Build.ExecuteEnv
7070
)
7171
import Stack.Build.Source ( addUnlistedToBuildCache )
7272
import Stack.Config.ConfigureScript ( ensureConfigureScript )
73+
import Stack.ConfigureOpts
74+
( configureOptsFromBase, renderConfigureOpts )
7375
import Stack.Constants
7476
( bindirSuffix, compilerOptionsCabalFlag, testGhcEnvRelFile )
7577
import Stack.Constants.Config
@@ -114,7 +116,6 @@ import Stack.Types.ComponentUtils
114116
import Stack.Types.Config ( Config (..), HasConfig (..) )
115117
import Stack.Types.ConfigureOpts
116118
( BaseConfigOpts (..), ConfigureOpts (..) )
117-
import qualified Stack.Types.ConfigureOpts as ConfigureOpts
118119
import Stack.Types.Curator ( Curator (..) )
119120
import Stack.Types.DumpPackage ( DumpPackage (..) )
120121
import Stack.Types.EnvConfig
@@ -195,7 +196,7 @@ getConfigCache ee task installedMap enableTest enableBench = do
195196
-- where it was the opposite resulted in this. It doesn't seem to make any
196197
-- difference anyway.
197198
allDepsMap = Map.union missing' task.present
198-
configureOpts' = ConfigureOpts.configureOpts
199+
configureOpts' = configureOptsFromBase
199200
cOpts.envConfig
200201
cOpts.baseConfigOpts
201202
allDepsMap
@@ -304,7 +305,7 @@ ensureConfig newConfigCache pkgDir buildOpts announce cabal cabalFP task = do
304305
Right x -> pure $ concat ["--with-", name, "=", x]
305306
let allOpts =
306307
concat exes
307-
<> ConfigureOpts.renderConfigureOpts newConfigCache.configureOpts
308+
<> renderConfigureOpts newConfigCache.configureOpts
308309
-- Configure cabal with arguments determined by
309310
-- Stack.Types.Build.configureOpts
310311
cabal KeepTHLoading $ "configure" : allOpts

src/Stack/Build/Source.hs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ import Stack.Types.BuildOptsCLI
5252
, boptsCLIAllProgOptions
5353
)
5454
import Stack.Types.CabalConfigKey ( CabalConfigKey (..) )
55+
import Stack.Types.Cache ( FileCache, FileCacheInfo (..) )
5556
import Stack.Types.CompilerPaths ( HasCompiler, getCompilerPath )
5657
import Stack.Types.Config ( Config (..), HasConfig (..), buildOptsL )
5758
import Stack.Types.Curator ( Curator (..) )
@@ -64,9 +65,8 @@ import Stack.Types.FileDigestCache ( readFileDigest )
6465
import Stack.Types.NamedComponent
6566
( NamedComponent (..), isCSubLib, splitComponents )
6667
import Stack.Types.Package
67-
( FileCacheInfo (..), LocalPackage (..), Package (..)
68-
, PackageConfig (..), dotCabalGetPath, memoizeRefWith
69-
, runMemoizedWith
68+
( LocalPackage (..), Package (..), PackageConfig (..)
69+
, dotCabalGetPath, memoizeRefWith, runMemoizedWith
7070
)
7171
import Stack.Types.PackageFile
7272
( PackageComponentFile (..), PackageWarning )
@@ -488,9 +488,9 @@ loadLocalPackage pp = do
488488
-- determine (1) if the files are dirty, and (2) the new cache values.
489489
checkBuildCache ::
490490
HasEnvConfig env
491-
=> Map FilePath FileCacheInfo -- ^ old cache
491+
=> FileCache -- ^ old cache
492492
-> [Path Abs File] -- ^ files in package
493-
-> RIO env (Set FilePath, Map FilePath FileCacheInfo)
493+
-> RIO env (Set FilePath, FileCache)
494494
checkBuildCache oldCache files = do
495495
fileDigests <- fmap Map.fromList $ forM files $ \fp -> do
496496
mdigest <- getFileDigestMaybe (toFilePath fp)
@@ -507,7 +507,7 @@ checkBuildCache oldCache files = do
507507
FilePath
508508
-> Maybe SHA256
509509
-> Maybe FileCacheInfo
510-
-> RIO env (Set FilePath, Map FilePath FileCacheInfo)
510+
-> RIO env (Set FilePath, FileCache)
511511
-- Filter out the cabal_macros file to avoid spurious recompilations
512512
go fp _ _ | takeFileName fp == "cabal_macros.h" = pure (Set.empty, Map.empty)
513513
-- Common case where it's in the cache and on the filesystem.
@@ -529,7 +529,7 @@ addUnlistedToBuildCache ::
529529
-> Path Abs File
530530
-> Set NamedComponent
531531
-> Map NamedComponent (Map FilePath a)
532-
-> RIO env (Map NamedComponent [Map FilePath FileCacheInfo], [PackageWarning])
532+
-> RIO env (Map NamedComponent [FileCache], [PackageWarning])
533533
addUnlistedToBuildCache pkg cabalFP nonLibComponents buildCaches = do
534534
(componentFiles, warnings) <-
535535
getPackageFilesForTargets pkg cabalFP nonLibComponents

0 commit comments

Comments
 (0)