Skip to content

Commit 75ce370

Browse files
committed
Set --enable-tests and --enable-benchmarks optimistically #805
1 parent a2bd923 commit 75ce370

File tree

7 files changed

+77
-17
lines changed

7 files changed

+77
-17
lines changed

ChangeLog.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ Other enhancements:
1616
* 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)
1717
* Implicitly add packages to extra-deps when a flag for them is set [#807](https://github.com/commercialhaskell/stack/issues/807)
1818
* Use a precompiled Setup.hs for simple build types [#801](https://github.com/commercialhaskell/stack/issues/801)
19+
* Set --enable-tests and --enable-benchmarks optimistically [#805](https://github.com/commercialhaskell/stack/issues/805)
1920

2021
Bug fixes:
2122

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: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -476,6 +476,8 @@ describeConfigDiff old new
476476
, "--package-db="
477477
, "--libdir="
478478
, "--bindir="
479+
, "--enable-tests"
480+
, "--enable-benchmarks"
479481
]
480482

481483
userOpts = filter (not . isStackOpt)

src/Stack/Build/Execute.hs

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

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

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

426-
let actions = concatMap (toActions runInBase ee) $ Map.elems $ Map.mergeWithKey
428+
let actions = concatMap (toActions installedMap runInBase ee) $ Map.elems $ Map.mergeWithKey
427429
(\_ b f -> Just (Just b, Just f))
428430
(fmap (\b -> (Just b, Nothing)))
429431
(fmap (\f -> (Nothing, Just f)))
@@ -467,11 +469,12 @@ executePlan' plan ee@ExecuteEnv {..} = do
467469
when (toCoverage $ boptsTestOpts eeBuildOpts) generateHpcMarkupIndex
468470

469471
toActions :: M env m
470-
=> (m () -> IO ())
472+
=> InstalledMap
473+
-> (m () -> IO ())
471474
-> ExecuteEnv
472475
-> (Maybe Task, Maybe (Task, LocalPackageTB)) -- build and final
473476
-> [Action]
474-
toActions runInBase ee (mbuild, mfinal) =
477+
toActions installedMap runInBase ee (mbuild, mfinal) =
475478
abuild ++ afinal
476479
where
477480
abuild =
@@ -482,7 +485,7 @@ toActions runInBase ee (mbuild, mfinal) =
482485
{ actionId = ActionId taskProvides ATBuild
483486
, actionDeps =
484487
(Set.map (\ident -> ActionId ident ATBuild) (tcoMissing taskConfigOpts))
485-
, actionDo = \ac -> runInBase $ singleBuild ac ee task
488+
, actionDo = \ac -> runInBase $ singleBuild ac ee task installedMap
486489
}
487490
]
488491
afinal =
@@ -495,9 +498,9 @@ toActions runInBase ee (mbuild, mfinal) =
495498
(Set.map (\ident -> ActionId ident ATBuild) (tcoMissing taskConfigOpts))
496499
, actionDo = \ac -> runInBase $ do
497500
unless (Set.null $ lptbTests lptb) $ do
498-
singleTest topts lptb ac ee task
501+
singleTest topts lptb ac ee task installedMap
499502
unless (Set.null $ lptbBenches lptb) $ do
500-
singleBench beopts lptb ac ee task
503+
singleBench beopts lptb ac ee task installedMap
501504
}
502505
]
503506
where
@@ -751,10 +754,22 @@ singleBuild :: M env m
751754
=> ActionContext
752755
-> ExecuteEnv
753756
-> Task
757+
-> InstalledMap
754758
-> m ()
755-
singleBuild ac@ActionContext {..} ee@ExecuteEnv {..} task@Task {..} =
759+
singleBuild ac@ActionContext {..} ee@ExecuteEnv {..} task@Task {..} installedMap =
756760
withSingleContext ac ee task Nothing $ \package cabalfp pkgDir cabal announce console _mlogFile -> do
757-
(cache, _neededConfig) <- ensureConfig pkgDir ee task (announce "configure") cabal cabalfp []
761+
(cache, _neededConfig) <- ensureConfig pkgDir ee task (announce "configure") cabal cabalfp $
762+
-- We enable tests if the test suite dependencies are already
763+
-- installed, so that we avoid unnecessary recompilation based on
764+
-- cabal_macros.h changes when switching between 'stack build' and
765+
-- 'stack test'. See:
766+
-- https://github.com/commercialhaskell/stack/issues/805
767+
case taskType of
768+
TTLocal lp -> concat
769+
[ ["--enable-tests" | depsPresent installedMap $ lpTestDeps lp]
770+
, ["--enable-benchmarks" | depsPresent installedMap $ lpBenchDeps lp]
771+
]
772+
_ -> []
758773
wc <- getWhichCompiler
759774

760775
markExeNotInstalled (taskLocation task) taskProvides
@@ -826,16 +841,32 @@ singleBuild ac@ActionContext {..} ee@ExecuteEnv {..} task@Task {..} =
826841
(PackageIdentifier (packageName package) (packageVersion package))
827842
Set.empty
828843

844+
-- | Determine if all of the dependencies given are installed
845+
depsPresent :: InstalledMap -> Map PackageName VersionRange -> Bool
846+
depsPresent installedMap deps = all
847+
(\(name, range) ->
848+
case Map.lookup name installedMap of
849+
Just (version, _, _) -> version `withinRange` range
850+
Nothing -> False)
851+
(Map.toList deps)
852+
829853
singleTest :: M env m
830854
=> TestOpts
831855
-> LocalPackageTB
832856
-> ActionContext
833857
-> ExecuteEnv
834858
-> Task
859+
-> InstalledMap
835860
-> m ()
836-
singleTest topts lptb ac ee task =
861+
singleTest topts lptb ac ee task installedMap =
837862
withSingleContext ac ee task (Just "test") $ \package cabalfp pkgDir cabal announce console mlogFile -> do
838-
(_cache, neededConfig) <- ensureConfig pkgDir ee task (announce "configure (test)") cabal cabalfp ["--enable-tests"]
863+
(_cache, neededConfig) <- ensureConfig pkgDir ee task (announce "configure (test)") cabal cabalfp $
864+
case taskType task of
865+
TTLocal lp -> concat
866+
[ ["--enable-tests"]
867+
, ["--enable-benchmarks" | depsPresent installedMap $ lpBenchDeps lp]
868+
]
869+
_ -> []
839870
config <- asks getConfig
840871

841872
testBuilt <- checkTestBuilt pkgDir
@@ -973,10 +1004,17 @@ singleBench :: M env m
9731004
-> ActionContext
9741005
-> ExecuteEnv
9751006
-> Task
1007+
-> InstalledMap
9761008
-> m ()
977-
singleBench beopts _lptb ac ee task =
1009+
singleBench beopts _lptb ac ee task installedMap =
9781010
withSingleContext ac ee task (Just "bench") $ \_package cabalfp pkgDir cabal announce console _mlogFile -> do
979-
(_cache, neededConfig) <- ensureConfig pkgDir ee task (announce "configure (benchmarks)") cabal cabalfp ["--enable-benchmarks"]
1011+
(_cache, neededConfig) <- ensureConfig pkgDir ee task (announce "configure (benchmarks)") cabal cabalfp $
1012+
case taskType task of
1013+
TTLocal lp -> concat
1014+
[ ["--enable-tests" | depsPresent installedMap $ lpTestDeps lp]
1015+
, ["--enable-benchmarks"]
1016+
]
1017+
_ -> []
9801018

9811019
benchBuilt <- checkBenchBuilt pkgDir
9821020

src/Stack/Build/Source.hs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -302,13 +302,24 @@ loadLocalPackage bopts targets (name, (lpv, gpkg)) = do
302302
{ packageConfigEnableTests = not $ Set.null tests
303303
, packageConfigEnableBenchmarks = not $ Set.null benches
304304
}
305+
testconfig = config
306+
{ packageConfigEnableTests = True
307+
, packageConfigEnableBenchmarks = False
308+
}
309+
benchconfig = config
310+
{ packageConfigEnableTests = False
311+
, packageConfigEnableBenchmarks = True
312+
}
313+
305314
btpkg
306315
| Set.null tests && Set.null benches = Nothing
307316
| otherwise = Just $ LocalPackageTB
308317
{ lptbPackage = resolvePackage btconfig gpkg
309318
, lptbTests = tests
310319
, lptbBenches = benches
311320
}
321+
testpkg = resolvePackage testconfig gpkg
322+
benchpkg = resolvePackage benchconfig gpkg
312323
mbuildCache <- tryGetBuildCache $ lpvRoot lpv
313324
(_,modFiles,otherFiles,mainFiles,extraFiles) <- getPackageFiles (packageFiles pkg) (lpvCabalFP lpv)
314325
let files =
@@ -322,6 +333,8 @@ loadLocalPackage bopts targets (name, (lpv, gpkg)) = do
322333

323334
return LocalPackage
324335
{ lpPackage = pkg
336+
, lpTestDeps = packageDeps $ testpkg
337+
, lpBenchDeps = packageDeps $ benchpkg
325338
, lpExeComponents =
326339
case mtarget of
327340
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)