-
Notifications
You must be signed in to change notification settings - Fork 697
/
Script.hs
113 lines (100 loc) · 4.39 KB
/
Script.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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE RankNTypes #-}
-- | Functionality for invoking Haskell scripts with the correct
-- package database setup.
module Test.Cabal.Script (
ScriptEnv(..),
mkScriptEnv,
runnerGhcArgs,
runnerCommand,
runghc,
) where
import Test.Cabal.Run
import Test.Cabal.ScriptEnv0
import Distribution.Backpack
import Distribution.Types.ModuleRenaming
import Distribution.Utils.NubList
import Distribution.Utils.Path
import Distribution.Simple.Program.Db
import Distribution.Simple.Program.Builtin
import Distribution.Simple.Program.GHC
import Distribution.Simple.Program
import Distribution.Simple.Compiler
import Distribution.Verbosity
import Distribution.System
import Distribution.Simple.Setup (Flag(..))
import qualified Data.Monoid as M
-- | The runner environment, which contains all of the important
-- parameters for invoking GHC. Mostly subset of 'LocalBuildInfo'.
data ScriptEnv = ScriptEnv
{ runnerProgramDb :: ProgramDb
, runnerPackageDbStack :: PackageDBStackCWD
, runnerVerbosity :: Verbosity
, runnerPlatform :: Platform
, runnerCompiler :: Compiler
, runnerPackages :: [(OpenUnitId, ModuleRenaming)]
, runnerWithSharedLib :: Bool
}
deriving Show
{-
-- | 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
-}
-- | Create a 'ScriptEnv' from a 'LocalBuildInfo' configured with
-- the GHC that we want to use.
mkScriptEnv :: Verbosity -> IO ScriptEnv
mkScriptEnv verbosity =
return $ ScriptEnv
{ runnerVerbosity = verbosity
, runnerProgramDb = lbiProgramDb
, runnerPackageDbStack = lbiPackageDbStack
, runnerPlatform = lbiPlatform
, runnerCompiler = lbiCompiler
-- NB: the set of packages available to test.hs scripts will COINCIDE
-- with the dependencies on the cabal-testsuite library
, runnerPackages = lbiPackages
, runnerWithSharedLib = lbiWithSharedLib
}
-- | Run a script with 'runghc', under the 'ScriptEnv'.
runghc :: ScriptEnv -> Maybe FilePath -> [(String, Maybe String)]
-> FilePath -> [String] -> IO Result
runghc senv mb_cwd env_overrides script_path args = do
(real_path, real_args) <- runnerCommand senv mb_cwd env_overrides script_path args
run (runnerVerbosity senv) mb_cwd env_overrides real_path real_args Nothing
-- | Compute the command line which should be used to run a Haskell
-- script with 'runghc'.
runnerCommand :: ScriptEnv -> Maybe FilePath -> [(String, Maybe String)]
-> FilePath -> [String] -> IO (FilePath, [String])
runnerCommand senv mb_cwd _env_overrides script_path args = do
(prog, _) <- requireProgram verbosity runghcProgram (runnerProgramDb senv)
return $
(programPath prog,
runghc_args ++ ["--"] ++ map ("--ghc-arg="++) ghc_args ++ [script_path] ++ args)
where
verbosity = runnerVerbosity senv
runghc_args = []
ghc_args = runnerGhcArgs senv mb_cwd
-- | Compute the GHC flags to invoke 'runghc' with under a 'ScriptEnv'.
runnerGhcArgs :: ScriptEnv -> Maybe FilePath -> [String]
runnerGhcArgs senv mb_cwd =
renderGhcOptions (runnerCompiler senv) (runnerPlatform senv) ghc_options
where
ghc_options = M.mempty { ghcOptPackageDBs = fmap (fmap makeSymbolicPath) (runnerPackageDbStack senv)
, ghcOptPackages = toNubListR (runnerPackages senv)
, ghcOptHideAllPackages = Flag True
-- Avoid picking stray module files that look
-- like our imports...
, ghcOptSourcePathClear = Flag True
-- ... yet retain the current directory as an included
-- directory, e.g. so that we can compile a Setup.hs
-- script which imports a locally defined module.
-- See the PackageTests/SetupDep test.
, ghcOptSourcePath = toNubListR $
case mb_cwd of
Nothing -> []
Just {} -> [sameDirectory]
}