Skip to content

Commit 00c8778

Browse files
committed
Set --enable-tests and --enable-benchmarks optimistically #805
1 parent 4159ec0 commit 00c8778

File tree

7 files changed

+82
-18
lines changed

7 files changed

+82
-18
lines changed

ChangeLog.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ Other enhancements:
1212
* Code page changes on Windows only apply to the build command (and its synonyms), and can be controlled via a command line flag (still defaults to on) [#757](https://github.com/commercialhaskell/stack/issues/757)
1313
* Implicitly add packages to extra-deps when a flag for them is set [#807](https://github.com/commercialhaskell/stack/issues/807)
1414
* Use a precompiled Setup.hs for simple build types [#801](https://github.com/commercialhaskell/stack/issues/801)
15+
* Set --enable-tests and --enable-benchmarks optimistically [#805](https://github.com/commercialhaskell/stack/issues/805)
1516

1617
Bug fixes:
1718

src/Stack/Build.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ build setLocalFiles mbuildLk bopts = do
9898

9999
if boptsDryrun bopts
100100
then printPlan plan
101-
else executePlan menv bopts baseConfigOpts locals sourceMap plan
101+
else executePlan menv bopts baseConfigOpts locals sourceMap installedMap plan
102102
where
103103
profiling = boptsLibProfile bopts || boptsExeProfile bopts
104104

src/Stack/Build/ConstructPlan.hs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -445,14 +445,20 @@ checkDirtiness ps installed package present wanted = do
445445
case moldOpts of
446446
Nothing -> Just "old configure information not found"
447447
Just oldOpts
448-
| oldOpts /= wantConfigCache -> Just $ describeConfigDiff oldOpts wantConfigCache
448+
| stripIgnoredOpts oldOpts /= stripIgnoredOpts wantConfigCache -> Just $ describeConfigDiff oldOpts wantConfigCache
449449
| psDirty ps -> Just "local file changes"
450450
| otherwise -> Nothing
451451
case mreason of
452452
Nothing -> return False
453453
Just reason -> do
454454
tell mempty { wDirty = Map.singleton (packageName package) reason }
455455
return True
456+
where
457+
stripIgnoredOpts cc = cc
458+
{ configCacheOpts = filter
459+
(`Set.notMember` Set.fromList ["--enable-tests", "--enable-benchmarks"])
460+
(configCacheOpts cc)
461+
}
456462

457463
describeConfigDiff :: ConfigCache -> ConfigCache -> Text
458464
describeConfigDiff old new

src/Stack/Build/Execute.hs

Lines changed: 54 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -317,10 +317,11 @@ executePlan :: M env m
317317
-> BaseConfigOpts
318318
-> [LocalPackage]
319319
-> SourceMap
320+
-> InstalledMap
320321
-> Plan
321322
-> m ()
322-
executePlan menv bopts baseConfigOpts locals sourceMap plan = do
323-
withExecuteEnv menv bopts baseConfigOpts locals sourceMap (executePlan' plan)
323+
executePlan menv bopts baseConfigOpts locals sourceMap installedMap plan = do
324+
withExecuteEnv menv bopts baseConfigOpts locals sourceMap (executePlan' installedMap plan)
324325

325326
unless (Map.null $ planInstallExes plan) $ do
326327
snapBin <- (</> bindirSuffix) `liftM` installationRootDeps
@@ -400,10 +401,11 @@ windowsRenameCopy src dest = do
400401

401402
-- | Perform the actual plan (internal)
402403
executePlan' :: M env m
403-
=> Plan
404+
=> InstalledMap
405+
-> Plan
404406
-> ExecuteEnv
405407
-> m ()
406-
executePlan' plan ee@ExecuteEnv {..} = do
408+
executePlan' installedMap plan ee@ExecuteEnv {..} = do
407409
wc <- getWhichCompiler
408410
case Map.toList $ planUnregisterLocal plan of
409411
[] -> return ()
@@ -424,7 +426,7 @@ executePlan' plan ee@ExecuteEnv {..} = do
424426
-- stack always using transformer stacks that are safe for this use case.
425427
runInBase <- liftBaseWith $ \run -> return (void . run)
426428

427-
let actions = concatMap (toActions runInBase ee) $ Map.elems $ Map.mergeWithKey
429+
let actions = concatMap (toActions installedMap runInBase ee) $ Map.elems $ Map.mergeWithKey
428430
(\_ b f -> Just (Just b, Just f))
429431
(fmap (\b -> (Just b, Nothing)))
430432
(fmap (\f -> (Nothing, Just f)))
@@ -468,11 +470,12 @@ executePlan' plan ee@ExecuteEnv {..} = do
468470
when (toCoverage $ boptsTestOpts eeBuildOpts) generateHpcMarkupIndex
469471

470472
toActions :: M env m
471-
=> (m () -> IO ())
473+
=> InstalledMap
474+
-> (m () -> IO ())
472475
-> ExecuteEnv
473476
-> (Maybe Task, Maybe (Task, LocalPackageTB)) -- build and final
474477
-> [Action]
475-
toActions runInBase ee (mbuild, mfinal) =
478+
toActions installedMap runInBase ee (mbuild, mfinal) =
476479
abuild ++ afinal
477480
where
478481
abuild =
@@ -483,7 +486,7 @@ toActions runInBase ee (mbuild, mfinal) =
483486
{ actionId = ActionId taskProvides ATBuild
484487
, actionDeps =
485488
(Set.map (\ident -> ActionId ident ATBuild) (tcoMissing taskConfigOpts))
486-
, actionDo = \ac -> runInBase $ singleBuild ac ee task
489+
, actionDo = \ac -> runInBase $ singleBuild ac ee task installedMap
487490
}
488491
]
489492
afinal =
@@ -496,9 +499,9 @@ toActions runInBase ee (mbuild, mfinal) =
496499
(Set.map (\ident -> ActionId ident ATBuild) (tcoMissing taskConfigOpts))
497500
, actionDo = \ac -> runInBase $ do
498501
unless (Set.null $ lptbTests lptb) $ do
499-
singleTest topts lptb ac ee task
502+
singleTest topts lptb ac ee task installedMap
500503
unless (Set.null $ lptbBenches lptb) $ do
501-
singleBench beopts lptb ac ee task
504+
singleBench beopts lptb ac ee task installedMap
502505
}
503506
]
504507
where
@@ -752,10 +755,22 @@ singleBuild :: M env m
752755
=> ActionContext
753756
-> ExecuteEnv
754757
-> Task
758+
-> InstalledMap
755759
-> m ()
756-
singleBuild ac@ActionContext {..} ee@ExecuteEnv {..} task@Task {..} =
760+
singleBuild ac@ActionContext {..} ee@ExecuteEnv {..} task@Task {..} installedMap =
757761
withSingleContext ac ee task Nothing $ \package cabalfp pkgDir cabal announce console _mlogFile -> do
758-
(cache, _neededConfig) <- ensureConfig pkgDir ee task (announce "configure") cabal cabalfp []
762+
(cache, _neededConfig) <- ensureConfig pkgDir ee task (announce "configure") cabal cabalfp $
763+
-- We enable tests if the test suite dependencies are already
764+
-- installed, so that we avoid unnecessary recompilation based on
765+
-- cabal_macros.h changes when switching between 'stack build' and
766+
-- 'stack test'. See:
767+
-- https://github.com/commercialhaskell/stack/issues/805
768+
case taskType of
769+
TTLocal lp -> concat
770+
[ ["--enable-tests" | depsPresent installedMap $ lpTestDeps lp]
771+
, ["--enable-benchmarks" | depsPresent installedMap $ lpBenchDeps lp]
772+
]
773+
_ -> []
759774
wc <- getWhichCompiler
760775

761776
markExeNotInstalled (taskLocation task) taskProvides
@@ -821,16 +836,32 @@ singleBuild ac@ActionContext {..} ee@ExecuteEnv {..} task@Task {..} =
821836
(PackageIdentifier (packageName package) (packageVersion package))
822837
Set.empty
823838

839+
-- | Determine if all of the dependencies given are installed
840+
depsPresent :: InstalledMap -> Map PackageName VersionRange -> Bool
841+
depsPresent installedMap deps = all
842+
(\(name, range) ->
843+
case Map.lookup name installedMap of
844+
Just (version, _, _) -> version `withinRange` range
845+
Nothing -> False)
846+
(Map.toList deps)
847+
824848
singleTest :: M env m
825849
=> TestOpts
826850
-> LocalPackageTB
827851
-> ActionContext
828852
-> ExecuteEnv
829853
-> Task
854+
-> InstalledMap
830855
-> m ()
831-
singleTest topts lptb ac ee task =
856+
singleTest topts lptb ac ee task installedMap =
832857
withSingleContext ac ee task (Just "test") $ \package cabalfp pkgDir cabal announce console mlogFile -> do
833-
(_cache, neededConfig) <- ensureConfig pkgDir ee task (announce "configure (test)") cabal cabalfp ["--enable-tests"]
858+
(_cache, neededConfig) <- ensureConfig pkgDir ee task (announce "configure (test)") cabal cabalfp $
859+
case taskType task of
860+
TTLocal lp -> concat
861+
[ ["--enable-tests"]
862+
, ["--enable-benchmarks" | depsPresent installedMap $ lpBenchDeps lp]
863+
]
864+
_ -> []
834865
config <- asks getConfig
835866

836867
testBuilt <- checkTestBuilt pkgDir
@@ -968,10 +999,17 @@ singleBench :: M env m
968999
-> ActionContext
9691000
-> ExecuteEnv
9701001
-> Task
1002+
-> InstalledMap
9711003
-> m ()
972-
singleBench beopts _lptb ac ee task =
1004+
singleBench beopts _lptb ac ee task installedMap =
9731005
withSingleContext ac ee task (Just "bench") $ \_package cabalfp pkgDir cabal announce console _mlogFile -> do
974-
(_cache, neededConfig) <- ensureConfig pkgDir ee task (announce "configure (benchmarks)") cabal cabalfp ["--enable-benchmarks"]
1006+
(_cache, neededConfig) <- ensureConfig pkgDir ee task (announce "configure (benchmarks)") cabal cabalfp $
1007+
case taskType task of
1008+
TTLocal lp -> concat
1009+
[ ["--enable-tests" | depsPresent installedMap $ lpTestDeps lp]
1010+
, ["--enable-benchmarks"]
1011+
]
1012+
_ -> []
9751013

9761014
benchBuilt <- checkBenchBuilt pkgDir
9771015

src/Stack/Build/Source.hs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -287,13 +287,24 @@ loadLocalPackage bopts targets (name, (lpv, gpkg)) = do
287287
{ packageConfigEnableTests = not $ Set.null tests
288288
, packageConfigEnableBenchmarks = not $ Set.null benches
289289
}
290+
testconfig = config
291+
{ packageConfigEnableTests = True
292+
, packageConfigEnableBenchmarks = False
293+
}
294+
benchconfig = config
295+
{ packageConfigEnableTests = False
296+
, packageConfigEnableBenchmarks = True
297+
}
298+
290299
btpkg
291300
| Set.null tests && Set.null benches = Nothing
292301
| otherwise = Just $ LocalPackageTB
293302
{ lptbPackage = resolvePackage btconfig gpkg
294303
, lptbTests = tests
295304
, lptbBenches = benches
296305
}
306+
testpkg = resolvePackage testconfig gpkg
307+
benchpkg = resolvePackage benchconfig gpkg
297308
mbuildCache <- tryGetBuildCache $ lpvRoot lpv
298309
(_,modFiles,otherFiles,mainFiles,extraFiles) <- getPackageFiles (packageFiles pkg) (lpvCabalFP lpv)
299310
let files =
@@ -307,6 +318,8 @@ loadLocalPackage bopts targets (name, (lpv, gpkg)) = do
307318

308319
return LocalPackage
309320
{ lpPackage = pkg
321+
, lpTestDeps = packageDeps $ testpkg
322+
, lpBenchDeps = packageDeps $ benchpkg
310323
, lpExeComponents =
311324
case mtarget of
312325
Nothing -> Nothing

src/Stack/SDist.hs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,8 @@ readLocalPackage pkgDir = do
9595
, lpCabalFile = cabalfp
9696
-- NOTE: these aren't the 'correct values, but aren't used in
9797
-- the usage of this function in this module.
98+
, lpTestDeps = Map.empty
99+
, lpBenchDeps = Map.empty
98100
, lpTestBench = Nothing
99101
, lpDirtyFiles = True
100102
, lpNewBuildCache = Map.empty

src/Stack/Types/Package.hs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,10 @@ data LocalPackageTB = LocalPackageTB
182182
-- | Information on a locally available package of source code
183183
data LocalPackage = LocalPackage
184184
{ lpPackage :: !Package -- ^ The @Package@ info itself, after resolution with package flags, not including any tests or benchmarks
185+
, lpTestDeps :: !(Map PackageName VersionRange)
186+
-- ^ Used for determining if we can use --enable-tests in a normal build
187+
, lpBenchDeps :: !(Map PackageName VersionRange)
188+
-- ^ Used for determining if we can use --enable-benchmarks in a normal build
185189
, lpExeComponents :: !(Maybe (Set Text)) -- ^ Executable components to build, Nothing if not a target
186190

187191
, lpTestBench :: !(Maybe LocalPackageTB)

0 commit comments

Comments
 (0)