-
Notifications
You must be signed in to change notification settings - Fork 697
/
Setup.hs
79 lines (72 loc) · 2.97 KB
/
Setup.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
{-# LANGUAGE Haskell2010 #-}
module Main (main) where
import Distribution.Backpack
import Distribution.Simple
import Distribution.Simple.BuildPaths
import Distribution.Simple.LocalBuildInfo
import Distribution.Simple.Setup
import Distribution.Simple.Utils
import Distribution.Types.LocalBuildInfo
import Distribution.Types.ModuleRenaming
import Distribution.Types.UnqualComponentName
import Distribution.Verbosity
import System.Directory
import System.FilePath
main :: IO ()
main = defaultMainWithHooks simpleUserHooks
{ confHook = \args flags -> do
lbi <- confHook simpleUserHooks args flags
generateScriptEnvModule lbi (fromFlagOrDefault minBound (configVerbosity flags))
pure lbi
}
generateScriptEnvModule :: LocalBuildInfo -> Verbosity -> IO ()
generateScriptEnvModule lbi verbosity = do
lbiPackageDbStack <- mapM canonicalizePackageDB (withPackageDB lbi)
createDirectoryIfMissing True moduledir
rewriteFileEx verbosity (moduledir </> "ScriptEnv0.hs") $ unlines
[ "module Test.Cabal.ScriptEnv0 where"
, ""
, "import Distribution.Simple"
, "import Distribution.System (Platform(..), Arch(..), OS(..))"
, "import Distribution.Types.ModuleRenaming"
, "import Distribution.Simple.Program.Db"
, "import Distribution.Backpack (OpenUnitId)"
, "import Data.Map (fromList)"
, ""
, "lbiPackageDbStack :: PackageDBStack"
, "lbiPackageDbStack = " ++ show lbiPackageDbStack
, ""
, "lbiPlatform :: Platform"
, "lbiPlatform = " ++ show (hostPlatform lbi)
, ""
, "lbiCompiler :: Compiler"
, "lbiCompiler = " ++ show (compiler lbi)
, ""
, "lbiPackages :: [(OpenUnitId, ModuleRenaming)]"
, "lbiPackages = read " ++ show (show (cabalTestsPackages lbi))
, ""
, "lbiProgramDb :: ProgramDb"
, "lbiProgramDb = read " ++ show (show (withPrograms lbi))
, ""
, "lbiWithSharedLib :: Bool"
, "lbiWithSharedLib = " ++ show (withSharedLib lbi)
]
where
moduledir = libAutogenDir </> "Test" </> "Cabal"
-- fixme: use component-specific folder
libAutogenDir = autogenPackageModulesDir lbi
-- | Convert package database into absolute path, so that
-- if we change working directories in a subprocess we get the correct database.
canonicalizePackageDB :: PackageDB -> IO PackageDB
canonicalizePackageDB (SpecificPackageDB path)
= SpecificPackageDB `fmap` canonicalizePath path
canonicalizePackageDB x = return x
-- | Compute the set of @-package-id@ flags which would be passed when
-- building the public library. Assumes that the public library is
-- non-Backpack.
cabalTestsPackages :: LocalBuildInfo -> [(OpenUnitId, ModuleRenaming)]
cabalTestsPackages lbi =
case componentNameCLBIs lbi (CExeName (mkUnqualComponentName "cabal-tests")) of
[clbi] -> -- [ (unUnitId $ unDefUnitId duid,rn) | (DefiniteUnitId duid, rn) <- componentIncludes clbi ]
componentIncludes clbi
_ -> error "cabalTestsPackages"