Skip to content

Commit 9be552e

Browse files
committed
Decouple lib:Cabal version used by custom-setup from the lib component
This might require more refinement in future and is a first iteration trying to address #5916
1 parent 9b93fea commit 9be552e

File tree

6 files changed

+170
-87
lines changed

6 files changed

+170
-87
lines changed

cabal-testsuite/Setup.hs

Lines changed: 76 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,78 @@
1+
{-# LANGUAGE Haskell2010 #-}
2+
module Main (main) where
3+
4+
import Distribution.Backpack
15
import Distribution.Simple
6+
import Distribution.Simple.BuildPaths
7+
import Distribution.Simple.LocalBuildInfo
8+
import Distribution.Simple.Setup
9+
import Distribution.Simple.Utils
10+
import Distribution.Types.LocalBuildInfo
11+
import Distribution.Types.ModuleRenaming
12+
import Distribution.Types.UnqualComponentName
13+
14+
import System.Directory
15+
import System.FilePath
16+
217
main :: IO ()
3-
main = defaultMain
4-
5-
-- Although this looks like the Simple build type, it is in fact vital that
6-
-- we use this Setup.hs because we need to compile against the very same
7-
-- version of the Cabal library that the test suite will be compiled
8-
-- against. When this happens, it will mean that we'll be able to
9-
-- read the LocalBuildInfo of our build environment, which we will
10-
-- subsequently use to make decisions about PATHs etc. Important!
18+
main = defaultMainWithHooks simpleUserHooks
19+
{ buildHook = \pkg lbi hooks flags -> do
20+
generateScriptEnvModule lbi flags
21+
buildHook simpleUserHooks pkg lbi hooks flags
22+
}
23+
24+
generateScriptEnvModule :: LocalBuildInfo -> BuildFlags -> IO ()
25+
generateScriptEnvModule lbi flags = do
26+
lbiPackageDbStack <- mapM canonicalizePackageDB (withPackageDB lbi)
27+
28+
createDirectoryIfMissing True moduledir
29+
rewriteFileEx verbosity (moduledir </> "ScriptEnv0.hs") $ unlines
30+
[ "module Test.Cabal.ScriptEnv0 where"
31+
, ""
32+
, "import Distribution.Simple"
33+
, "import Distribution.System (Platform(..), Arch(..), OS(..))"
34+
, "import Distribution.Types.ModuleRenaming"
35+
, "import Distribution.Simple.Program.Db"
36+
, "import Distribution.Backpack (OpenUnitId)"
37+
, "import Data.Map (fromList)"
38+
, ""
39+
, "lbiPackageDbStack :: PackageDBStack"
40+
, "lbiPackageDbStack = " ++ show lbiPackageDbStack
41+
, ""
42+
, "lbiPlatform :: Platform"
43+
, "lbiPlatform = " ++ show (hostPlatform lbi)
44+
, ""
45+
, "lbiCompiler :: Compiler"
46+
, "lbiCompiler = " ++ show (compiler lbi)
47+
, ""
48+
, "lbiPackages :: [(OpenUnitId, ModuleRenaming)]"
49+
, "lbiPackages = read " ++ show (show (cabalTestsPackages lbi))
50+
, ""
51+
, "lbiProgramDb :: ProgramDb"
52+
, "lbiProgramDb = read " ++ show (show (withPrograms lbi))
53+
, ""
54+
, "lbiWithSharedLib :: Bool"
55+
, "lbiWithSharedLib = " ++ show (withSharedLib lbi)
56+
]
57+
where
58+
verbosity = fromFlagOrDefault minBound (buildVerbosity flags)
59+
moduledir = libAutogenDir </> "Test" </> "Cabal"
60+
-- fixme: use component-specific folder
61+
libAutogenDir = autogenPackageModulesDir lbi
62+
63+
-- | Convert package database into absolute path, so that
64+
-- if we change working directories in a subprocess we get the correct database.
65+
canonicalizePackageDB :: PackageDB -> IO PackageDB
66+
canonicalizePackageDB (SpecificPackageDB path)
67+
= SpecificPackageDB `fmap` canonicalizePath path
68+
canonicalizePackageDB x = return x
69+
70+
-- | Compute the set of @-package-id@ flags which would be passed when
71+
-- building the public library. Assumes that the public library is
72+
-- non-Backpack.
73+
cabalTestsPackages :: LocalBuildInfo -> [(OpenUnitId, ModuleRenaming)]
74+
cabalTestsPackages lbi =
75+
case componentNameCLBIs lbi (CExeName (mkUnqualComponentName "cabal-tests")) of
76+
[clbi] -> -- [ (unUnitId $ unDefUnitId duid,rn) | (DefiniteUnitId duid, rn) <- componentIncludes clbi ]
77+
componentIncludes clbi
78+
_ -> error "cabalTestsPackages"

cabal-testsuite/Setup.simple.hs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
module Main (main) where
2+
3+
import Distribution.Simple
4+
5+
main :: IO ()
6+
main = defaultMain

cabal-testsuite/Test/Cabal/Monad.hs

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,7 @@ import Distribution.System
6666
import Distribution.Simple.Program.Db
6767
import Distribution.Simple.Program
6868
import Distribution.Simple.Configure
69-
( getPersistBuildConfig, configCompilerEx )
70-
import Distribution.Types.LocalBuildInfo
69+
( configCompilerEx )
7170
import Distribution.Version
7271
import Distribution.Text
7372
import Distribution.Package
@@ -240,20 +239,19 @@ runTestM mode m = withSystemTempDirectory "cabal-testsuite" $ \tmp_dir -> do
240239
script_base = dropExtensions script_filename
241240
-- Canonicalize this so that it is stable across working directory changes
242241
script_dir <- canonicalizePath script_dir0
243-
lbi <- getPersistBuildConfig dist_dir
244242
let verbosity = normal -- TODO: configurable
245-
senv <- mkScriptEnv verbosity lbi
243+
senv <- mkScriptEnv verbosity
246244
-- Add test suite specific programs
247245
let program_db0 =
248246
addKnownPrograms
249247
([gitProgram, hackageRepoToolProgram, cabalProgram, diffProgram] ++ builtinPrograms)
250-
(withPrograms lbi)
248+
(runnerProgramDb senv)
251249
-- Reconfigure according to user flags
252250
let cargs = testCommonArgs args
253251

254252
-- Reconfigure GHC
255253
(comp, platform, program_db2) <- case argGhcPath cargs of
256-
Nothing -> return (compiler lbi, hostPlatform lbi, program_db0)
254+
Nothing -> return (runnerCompiler senv, runnerPlatform senv, program_db0)
257255
Just ghc_path -> do
258256
-- All the things that get updated paths from
259257
-- configCompilerEx. The point is to make sure
@@ -274,7 +272,7 @@ runTestM mode m = withSystemTempDirectory "cabal-testsuite" $ \tmp_dir -> do
274272
-- we don't pay for things we don't need. A bit difficult
275273
-- to do in the current design.
276274
configCompilerEx
277-
(Just (compilerFlavor (compiler lbi)))
275+
(Just (compilerFlavor (runnerCompiler senv)))
278276
(Just ghc_path)
279277
Nothing
280278
program_db1
@@ -294,7 +292,7 @@ runTestM mode m = withSystemTempDirectory "cabal-testsuite" $ \tmp_dir -> do
294292

295293
let db_stack =
296294
case argGhcPath (testCommonArgs args) of
297-
Nothing -> withPackageDB lbi
295+
Nothing -> runnerPackageDbStack senv -- NB: canonicalized
298296
-- Can't use the build package db stack since they
299297
-- are all for the wrong versions! TODO: Make
300298
-- this configurable
@@ -311,9 +309,9 @@ runTestM mode m = withSystemTempDirectory "cabal-testsuite" $ \tmp_dir -> do
311309
testVerbosity = verbosity,
312310
testMtimeChangeDelay = Nothing,
313311
testScriptEnv = senv,
314-
testSetupPath = dist_dir </> "setup" </> "setup",
312+
testSetupPath = dist_dir </> "build" </> "setup" </> "setup",
315313
testSkipSetupTests = argSkipSetupTests (testCommonArgs args),
316-
testHaveCabalShared = withSharedLib lbi,
314+
testHaveCabalShared = runnerWithSharedLib senv,
317315
testEnvironment =
318316
-- Try to avoid Unicode output
319317
[ ("LC_ALL", Just "C")

cabal-testsuite/Test/Cabal/Script.hs

Lines changed: 15 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,10 @@ module Test.Cabal.Script (
99
) where
1010

1111
import Test.Cabal.Run
12+
import Test.Cabal.ScriptEnv0
1213

1314
import Distribution.Backpack
1415
import Distribution.Types.ModuleRenaming
15-
import Distribution.Types.LocalBuildInfo
16-
import Distribution.Types.ComponentLocalBuildInfo
17-
import Distribution.Types.ComponentName
18-
import Distribution.Types.UnqualComponentName
1916
import Distribution.Utils.NubList
2017
import Distribution.Simple.Program.Db
2118
import Distribution.Simple.Program.Builtin
@@ -26,9 +23,9 @@ import Distribution.Verbosity
2623
import Distribution.System
2724
import Distribution.Simple.Setup (Flag(..))
2825

29-
import System.Directory
3026
import qualified Data.Monoid as M
3127

28+
3229
-- | The runner environment, which contains all of the important
3330
-- parameters for invoking GHC. Mostly subset of 'LocalBuildInfo'.
3431
data ScriptEnv = ScriptEnv
@@ -38,40 +35,36 @@ data ScriptEnv = ScriptEnv
3835
, runnerPlatform :: Platform
3936
, runnerCompiler :: Compiler
4037
, runnerPackages :: [(OpenUnitId, ModuleRenaming)]
38+
, runnerWithSharedLib :: Bool
4139
}
4240

41+
{-
42+
4343
-- | Convert package database into absolute path, so that
4444
-- if we change working directories in a subprocess we get the correct database.
4545
canonicalizePackageDB :: PackageDB -> IO PackageDB
4646
canonicalizePackageDB (SpecificPackageDB path)
4747
= SpecificPackageDB `fmap` canonicalizePath path
4848
canonicalizePackageDB x = return x
4949
50+
-}
51+
5052
-- | Create a 'ScriptEnv' from a 'LocalBuildInfo' configured with
5153
-- the GHC that we want to use.
52-
mkScriptEnv :: Verbosity -> LocalBuildInfo -> IO ScriptEnv
53-
mkScriptEnv verbosity lbi = do
54-
package_db <- mapM canonicalizePackageDB (withPackageDB lbi)
54+
mkScriptEnv :: Verbosity -> IO ScriptEnv
55+
mkScriptEnv verbosity =
5556
return $ ScriptEnv
5657
{ runnerVerbosity = verbosity
57-
, runnerProgramDb = withPrograms lbi
58-
, runnerPackageDbStack = package_db
59-
, runnerPlatform = hostPlatform lbi
60-
, runnerCompiler = compiler lbi
58+
, runnerProgramDb = lbiProgramDb
59+
, runnerPackageDbStack = lbiPackageDbStack
60+
, runnerPlatform = lbiPlatform
61+
, runnerCompiler = lbiCompiler
6162
-- NB: the set of packages available to test.hs scripts will COINCIDE
6263
-- with the dependencies on the cabal-testsuite library
63-
, runnerPackages = cabalTestsPackages lbi
64+
, runnerPackages = lbiPackages
65+
, runnerWithSharedLib = lbiWithSharedLib
6466
}
6567

66-
-- | Compute the set of @-package-id@ flags which would be passed when
67-
-- building the public library. Assumes that the public library is
68-
-- non-Backpack.
69-
cabalTestsPackages :: LocalBuildInfo -> [(OpenUnitId, ModuleRenaming)]
70-
cabalTestsPackages lbi =
71-
case componentNameCLBIs lbi (CExeName (mkUnqualComponentName "cabal-tests")) of
72-
[clbi] -> componentIncludes clbi
73-
_ -> error "cabalTestsPackages"
74-
7568
-- | Run a script with 'runghc', under the 'ScriptEnv'.
7669
runghc :: ScriptEnv -> Maybe FilePath -> [(String, Maybe String)]
7770
-> FilePath -> [String] -> IO Result

cabal-testsuite/cabal-testsuite.cabal

Lines changed: 64 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1+
cabal-version: 2.2
12
name: cabal-testsuite
23
version: 3.0.0.0
34
copyright: 2003-2019, Cabal Development Team (see AUTHORS file)
4-
license: BSD3
5+
license: BSD-3-Clause
56
license-file: LICENSE
67
author: Cabal Development Team <[email protected]>
78
maintainer: [email protected]
@@ -11,7 +12,6 @@ synopsis: Test suite for Cabal and cabal-install
1112
description:
1213
This package defines a shared test suite for Cabal and cabal-install.
1314
category: Distribution
14-
cabal-version: >=1.10
1515
build-type: Custom
1616

1717
extra-source-files:
@@ -22,7 +22,19 @@ source-repository head
2222
location: https://github.com/haskell/cabal/
2323
subdir: cabal-testsuite
2424

25+
common shared
26+
default-language: Haskell2010
27+
28+
build-depends:
29+
, base >= 4.8 && <5
30+
-- this needs to match the in-tree lib:Cabal version
31+
, Cabal == 3.0.0.0
32+
33+
ghc-options: -Wall -fwarn-tabs
34+
2535
library
36+
import: shared
37+
2638
exposed-modules:
2739
Test.Cabal.Workdir
2840
Test.Cabal.Script
@@ -32,54 +44,63 @@ library
3244
Test.Cabal.Server
3345
Test.Cabal.Monad
3446
Test.Cabal.CheckArMetadata
47+
48+
other-modules:
49+
Test.Cabal.ScriptEnv0
50+
autogen-modules:
51+
Test.Cabal.ScriptEnv0
52+
3553
build-depends:
36-
aeson ==1.4.*,
37-
attoparsec,
38-
async,
39-
base,
40-
bytestring,
41-
transformers,
42-
optparse-applicative >=0.14 && <0.15,
43-
process,
44-
directory,
45-
filepath,
46-
regex-compat-tdfa,
47-
regex-tdfa,
48-
temporary,
49-
text,
50-
cryptohash-sha256,
51-
base16-bytestring,
52-
Cabal >= 2.3
53-
ghc-options: -Wall -fwarn-tabs
54+
, aeson ^>= 1.4.2.0
55+
, async ^>= 2.2.1
56+
, attoparsec ^>= 0.13.2.2
57+
, base16-bytestring ^>= 0.1.1.6
58+
, bytestring ^>= 0.10.6.0
59+
, containers ^>= 0.5.6.2 || ^>= 0.6.0.1
60+
, cryptohash-sha256 ^>= 0.11.101.0
61+
, directory ^>= 1.2.2.0 || ^>= 1.3.0.0
62+
, exceptions ^>= 0.10.0
63+
, filepath ^>= 1.4.0.0
64+
, optparse-applicative ^>= 0.14.3.0
65+
, process ^>= 1.2.3.0 || ^>= 1.4.2.0 || ^>= 1.6.1.0
66+
, regex-compat-tdfa ^>= 0.95.1.4
67+
, regex-tdfa ^>= 1.2.3.1
68+
, temporary ^>= 1.3
69+
, text ^>= 1.2.3.1
70+
, transformers ^>= 0.4.2.0 || ^>= 0.5.2.0
71+
5472
if !os(windows)
55-
build-depends: unix, exceptions
73+
build-depends:
74+
, unix ^>= 2.7.1.0
5675
else
57-
build-depends: Win32
58-
default-language: Haskell2010
76+
build-depends:
77+
, Win32
5978

6079
executable cabal-tests
80+
import: shared
6181
main-is: cabal-tests.hs
6282
hs-source-dirs: main
63-
ghc-options: -threaded -Wall -fwarn-tabs
83+
ghc-options: -threaded
6484
build-depends:
65-
async,
66-
base,
67-
Cabal == 3.0.0.0,
68-
clock,
69-
filepath,
70-
process,
71-
optparse-applicative,
72-
cabal-testsuite,
73-
transformers,
74-
exceptions
75-
default-language: Haskell2010
85+
, cabal-testsuite
86+
-- cosntraints inherited via lib:cabal-testsuite component
87+
, async
88+
, exceptions
89+
, filepath
90+
, optparse-applicative
91+
, process
92+
, transformers
93+
-- dependencies specific to exe:cabal-tests
94+
, clock ^>= 0.7.2
95+
96+
build-tool-depends: cabal-testsuite:setup
97+
98+
-- this executable is needed by lib:cabal-testsuite
99+
executable setup
100+
import: shared
101+
main-is: Setup.simple.hs
76102

77103
custom-setup
78-
-- It's important that we pick the exact same version of lib:Cabal
79-
-- both here and for cabal-tests itself. Without this constraint,
80-
-- the solver would pick the in-tree Cabal for cabal-tests's
81-
-- lib:Cabal dependency, and some stable lib:Cabal version for its
82-
-- custom-setup's one (due to 'setupMaxCabalVersionConstraint' in
83-
-- 'D.C.ProjectPlanning').
84-
setup-depends: Cabal == 3.0.0.0,
85-
base
104+
-- we only depend on even stable releases of lib:Cabal
105+
setup-depends: Cabal == 2.2.* || == 2.4.* || == 3.0.*,
106+
base, filepath, directory

0 commit comments

Comments
 (0)