aboutsummaryrefslogtreecommitdiff
path: root/tests/GhcSession.hs
blob: 8ed76be9d4f3fa0306926330ccbc032e003aa819 (plain) (blame)
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
{-# LANGUAGE TupleSections, ScopedTypeVariables, CPP #-}
module Main where

import GHC
#if __GLASGOW_HASKELL__ <= 706
import GhcMonad
#endif
import GHC.Paths (libdir)
import DynFlags

import qualified Control.Exception as E
import Control.Monad
import Control.Monad.IO.Class
import Data.List
import Data.Version
import System.Environment (getArgs)
import System.Exit
import System.FilePath ((</>))
import System.Directory
import System.IO.Temp
import System.Process (readProcess)

import Distribution.Helper

import CabalHelper.Shared.Common


main :: IO ()
main = do
  args <- getArgs
  topdir <- getCurrentDirectory
  res <- mapM (setup topdir test) $ case args of
    [] -> [ ("tests/exelib"   , parseVer "1.10")
          , ("tests/exeintlib", parseVer "2.0")
          ]
    xs -> map (,parseVer "0") xs

  if any (==False) $ concat res
    then exitFailure
    else exitSuccess

cabalInstallVersion :: IO Version
cabalInstallVersion =
    parseVer . trim <$> readProcess "cabal" ["--numeric-version"] ""

cabalInstallBuiltinCabalVersion :: IO Version
cabalInstallBuiltinCabalVersion =
    parseVer . trim <$> readProcess "cabal"
        ["act-as-setup", "--", "--numeric-version"] ""

setup :: FilePath -> (FilePath -> IO [Bool]) -> (FilePath, Version) -> IO [Bool]
setup topdir act (srcdir, min_cabal_ver) = do
    ci_ver <- cabalInstallVersion
    c_ver <- cabalInstallBuiltinCabalVersion
    let mreason
          | (ci_ver < parseVer "1.24") =
            Just $ "cabal-install-" ++ showVersion ci_ver ++ " is too old"
          | c_ver < min_cabal_ver =
            Just $ "Cabal-" ++ showVersion c_ver
                   ++ " < " ++ showVersion min_cabal_ver
          | otherwise =
            Nothing

    case mreason of
      Just reason -> do
        putStrLn $ "Skipping test '" ++ srcdir ++ "' because " ++ reason ++ "."
        return []
      Nothing -> do
        withSystemTempDirectory "cabal-helper.ghc-session.test" $ \dir -> do
          setCurrentDirectory $ topdir </> srcdir
          run "cabal" [ "sdist", "--output-dir", dir ]

          setCurrentDirectory dir
          run "cabal" [ "configure" ]
--          run "cabal" [ "build" ]

          act dir
  where
    run x xs = do
      print $ x:xs
      o <- readProcess x xs ""
      putStrLn o
      return ()

test :: FilePath -> IO [Bool]
test dir = do
    let qe = mkQueryEnv dir (dir </> "dist")
    cs <- runQuery qe $ components $ (,,) <$> entrypoints <.> ghcOptions
    forM cs $ \(ep, opts, cn) -> do
        let sopts = intercalate " " $ map formatArg $ "ghc" : opts
        putStrLn $ "\n" ++ show cn ++ ": " ++ sopts
        compileModule ep opts
  where
    formatArg x
        | "-" `isPrefixOf` x = "\n  "++x
        | otherwise          = x

compileModule :: ChEntrypoint -> [String] -> IO Bool
compileModule ep opts = do

    E.handle (\(ec :: ExitCode) -> print ec >> return False) $ do

#if __GLASGOW_HASKELL__ <= 704
    defaultErrorHandler defaultLogAction $ do
#else
    defaultErrorHandler defaultFatalMessager defaultFlushOut $ do
#endif

    runGhc (Just libdir) $ do

    dflags0 <- getSessionDynFlags
    let dflags1 = dflags0 {
        ghcMode   = CompManager
      , ghcLink   = LinkInMemory
      , hscTarget = HscInterpreted
      , optLevel  = 0
      }

    (dflags2, _, _) <- parseDynamicFlags dflags1 (map noLoc opts)
    _ <- setSessionDynFlags dflags2

    ts <- mapM (\t -> guessTarget t Nothing) $
         case ep of
           ChLibEntrypoint ms ms' -> map unChModuleName $ ms ++ ms'
           ChExeEntrypoint m  ms  -> [m] ++ map unChModuleName ms
           ChSetupEntrypoint      -> ["Setup.hs"]
    let ts' = map (\t -> t { targetAllowObjCode = False }) ts

    setTargets ts'
    _ <- load LoadAllTargets

#if __GLASGOW_HASKELL__ >= 706
    setContext $ case ep of
      ChLibEntrypoint ms ms' ->
          map (IIModule . mkModuleName . unChModuleName) $ ms ++ ms'
      ChExeEntrypoint _  ms  ->
          map (IIModule . mkModuleName . unChModuleName) $ ChModuleName "Main" : ms
      ChSetupEntrypoint      ->
          map (IIModule . mkModuleName) ["Main"]
#endif

#if __GLASGOW_HASKELL__ <= 706
    GhcMonad.liftIO $ print ExitSuccess
#else
    liftIO $ print ExitSuccess
#endif
    return True

unChModuleName :: ChModuleName -> String
unChModuleName (ChModuleName  mn) = mn