Skip to content

Commit 86b3b8f

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

File tree

7 files changed

+83
-17
lines changed

7 files changed

+83
-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: 60 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)))
@@ -465,13 +467,20 @@ executePlan' plan ee@ExecuteEnv {..} = do
465467
generateDepsHaddockIndex eeEnvOverride wc eeBaseConfigOpts eeLocals
466468
generateSnapHaddockIndex eeEnvOverride wc eeBaseConfigOpts eeGlobalDB
467469
when (toCoverage $ boptsTestOpts eeBuildOpts) generateHpcMarkupIndex
470+
where
471+
installedMap' = Map.difference installedMap
472+
$ Map.fromList
473+
$ map (\gid -> (packageIdentifierName $ ghcPkgIdPackageIdentifier gid, ()))
474+
$ Map.keys
475+
$ planUnregisterLocal plan
468476

469477
toActions :: M env m
470-
=> (m () -> IO ())
478+
=> InstalledMap
479+
-> (m () -> IO ())
471480
-> ExecuteEnv
472481
-> (Maybe Task, Maybe (Task, LocalPackageTB)) -- build and final
473482
-> [Action]
474-
toActions runInBase ee (mbuild, mfinal) =
483+
toActions installedMap runInBase ee (mbuild, mfinal) =
475484
abuild ++ afinal
476485
where
477486
abuild =
@@ -482,7 +491,7 @@ toActions runInBase ee (mbuild, mfinal) =
482491
{ actionId = ActionId taskProvides ATBuild
483492
, actionDeps =
484493
(Set.map (\ident -> ActionId ident ATBuild) (tcoMissing taskConfigOpts))
485-
, actionDo = \ac -> runInBase $ singleBuild ac ee task
494+
, actionDo = \ac -> runInBase $ singleBuild ac ee task installedMap
486495
}
487496
]
488497
afinal =
@@ -495,9 +504,9 @@ toActions runInBase ee (mbuild, mfinal) =
495504
(Set.map (\ident -> ActionId ident ATBuild) (tcoMissing taskConfigOpts))
496505
, actionDo = \ac -> runInBase $ do
497506
unless (Set.null $ lptbTests lptb) $ do
498-
singleTest topts lptb ac ee task
507+
singleTest topts lptb ac ee task installedMap
499508
unless (Set.null $ lptbBenches lptb) $ do
500-
singleBench beopts lptb ac ee task
509+
singleBench beopts lptb ac ee task installedMap
501510
}
502511
]
503512
where
@@ -751,10 +760,22 @@ singleBuild :: M env m
751760
=> ActionContext
752761
-> ExecuteEnv
753762
-> Task
763+
-> InstalledMap
754764
-> m ()
755-
singleBuild ac@ActionContext {..} ee@ExecuteEnv {..} task@Task {..} =
765+
singleBuild ac@ActionContext {..} ee@ExecuteEnv {..} task@Task {..} installedMap =
756766
withSingleContext ac ee task Nothing $ \package cabalfp pkgDir cabal announce console _mlogFile -> do
757-
(cache, _neededConfig) <- ensureConfig pkgDir ee task (announce "configure") cabal cabalfp []
767+
(cache, _neededConfig) <- ensureConfig pkgDir ee task (announce "configure") cabal cabalfp $
768+
-- We enable tests if the test suite dependencies are already
769+
-- installed, so that we avoid unnecessary recompilation based on
770+
-- cabal_macros.h changes when switching between 'stack build' and
771+
-- 'stack test'. See:
772+
-- https://github.com/commercialhaskell/stack/issues/805
773+
case taskType of
774+
TTLocal lp -> concat
775+
[ ["--enable-tests" | depsPresent installedMap $ lpTestDeps lp]
776+
, ["--enable-benchmarks" | depsPresent installedMap $ lpBenchDeps lp]
777+
]
778+
_ -> []
758779
wc <- getWhichCompiler
759780

760781
markExeNotInstalled (taskLocation task) taskProvides
@@ -826,16 +847,32 @@ singleBuild ac@ActionContext {..} ee@ExecuteEnv {..} task@Task {..} =
826847
(PackageIdentifier (packageName package) (packageVersion package))
827848
Set.empty
828849

850+
-- | Determine if all of the dependencies given are installed
851+
depsPresent :: InstalledMap -> Map PackageName VersionRange -> Bool
852+
depsPresent installedMap deps = all
853+
(\(name, range) ->
854+
case Map.lookup name installedMap of
855+
Just (version, _, _) -> version `withinRange` range
856+
Nothing -> False)
857+
(Map.toList deps)
858+
829859
singleTest :: M env m
830860
=> TestOpts
831861
-> LocalPackageTB
832862
-> ActionContext
833863
-> ExecuteEnv
834864
-> Task
865+
-> InstalledMap
835866
-> m ()
836-
singleTest topts lptb ac ee task =
867+
singleTest topts lptb ac ee task installedMap =
837868
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"]
869+
(_cache, neededConfig) <- ensureConfig pkgDir ee task (announce "configure (test)") cabal cabalfp $
870+
case taskType task of
871+
TTLocal lp -> concat
872+
[ ["--enable-tests"]
873+
, ["--enable-benchmarks" | depsPresent installedMap $ lpBenchDeps lp]
874+
]
875+
_ -> []
839876
config <- asks getConfig
840877

841878
testBuilt <- checkTestBuilt pkgDir
@@ -973,10 +1010,17 @@ singleBench :: M env m
9731010
-> ActionContext
9741011
-> ExecuteEnv
9751012
-> Task
1013+
-> InstalledMap
9761014
-> m ()
977-
singleBench beopts _lptb ac ee task =
1015+
singleBench beopts _lptb ac ee task installedMap =
9781016
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"]
1017+
(_cache, neededConfig) <- ensureConfig pkgDir ee task (announce "configure (benchmarks)") cabal cabalfp $
1018+
case taskType task of
1019+
TTLocal lp -> concat
1020+
[ ["--enable-tests" | depsPresent installedMap $ lpTestDeps lp]
1021+
, ["--enable-benchmarks"]
1022+
]
1023+
_ -> []
9801024

9811025
benchBuilt <- checkBenchBuilt pkgDir
9821026

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)