GHC.hs 6.38 KB
Newer Older
Andrey Mokhov's avatar
Andrey Mokhov committed
1
{-# OPTIONS_GHC -fno-warn-missing-signatures #-}
2
module GHC (
3
    -- * GHC packages
4 5 6 7 8
    array, base, binary, bytestring, cabal, checkPpr, compareSizes, compiler,
    containers, deepseq, deriveConstants, directory, filepath, genapply,
    genprimopcode, ghc, ghcBoot, ghcBootTh, ghcCabal, ghcCompact, ghci, ghcPkg,
    ghcPrim, ghcTags, ghcSplit, haddock, haskeline, hsc2hs, hp2ps, hpc, hpcBin,
    integerGmp, integerSimple, iservBin, libffi, mtl, parsec, parallel, pretty,
Andrey Mokhov's avatar
Andrey Mokhov committed
9 10
    primitive, process, rts, runGhc, stm, templateHaskell, terminfo, text, time,
    touchy, transformers, unlit, unix, win32, xhtml, ghcPackages, isGhcPackage,
11
    defaultPackages, testsuitePackages,
12 13

    -- * Package information
Andrey Mokhov's avatar
Andrey Mokhov committed
14
    programName, nonCabalContext, nonHsMainPackage, autogenPath, installStage,
15 16

    -- * Miscellaneous
17
    programPath, buildDll0
18
    ) where
19

20
import Base
Andrey Mokhov's avatar
Andrey Mokhov committed
21
import Context
22 23
import Flavour
import GHC.Packages
24
import Oracles.Flag
25
import Oracles.Setting
26
import Settings (flavour)
27

Andrey Mokhov's avatar
Andrey Mokhov committed
28 29 30 31 32 33 34 35 36 37
-- | Packages that are built by default. You can change this in "UserSettings".
defaultPackages :: Stage -> Action [Package]
defaultPackages Stage0 = stage0Packages
defaultPackages Stage1 = stage1Packages
defaultPackages Stage2 = stage2Packages
defaultPackages Stage3 = return []

stage0Packages :: Action [Package]
stage0Packages = do
    win <- windowsHost
38
    cross <- crossCompiling
Andrey Mokhov's avatar
Andrey Mokhov committed
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
    return $ [ binary
             , cabal
             , compareSizes
             , compiler
             , deriveConstants
             , genapply
             , genprimopcode
             , ghc
             , ghcBoot
             , ghcBootTh
             , ghci
             , ghcPkg
             , ghcTags
             , hsc2hs
             , hp2ps
             , hpc
             , mtl
             , parsec
             , templateHaskell
             , text
             , transformers
60 61 62
             , unlit                         ]
          ++ [ terminfo | not win, not cross ]
          ++ [ touchy   | win                ]
Andrey Mokhov's avatar
Andrey Mokhov committed
63 64 65 66

stage1Packages :: Action [Package]
stage1Packages = do
    win        <- windowsHost
67
    intLib     <- integerLibrary =<< flavour
Andrey Mokhov's avatar
Andrey Mokhov committed
68
    libraries0 <- filter isLibrary <$> stage0Packages
69
    cross      <- crossCompiling
Andrey Mokhov's avatar
Andrey Mokhov committed
70 71 72 73 74 75 76 77 78 79
    return $ libraries0 -- Build all Stage0 libraries in Stage1
          ++ [ array
             , base
             , bytestring
             , containers
             , deepseq
             , directory
             , filepath
             , ghc
             , ghcCompact
80
             , ghcPkg
Andrey Mokhov's avatar
Andrey Mokhov committed
81 82 83
             , ghcPrim
             , haskeline
             , hsc2hs
84
             , intLib
Andrey Mokhov's avatar
Andrey Mokhov committed
85 86 87
             , pretty
             , process
             , rts
88
             , stm
89
             , time
90 91 92 93 94 95 96 97
             , unlit
             , xhtml                         ]
          ++ [ haddock  | not cross          ]
          ++ [ runGhc   | not cross          ]
          ++ [ hpcBin   | not cross          ]
          ++ [ iservBin | not win, not cross ]
          ++ [ unix     | not win            ]
          ++ [ win32    | win                ]
Andrey Mokhov's avatar
Andrey Mokhov committed
98 99

stage2Packages :: Action [Package]
100
stage2Packages = return [haddock]
Andrey Mokhov's avatar
Andrey Mokhov committed
101

102 103 104 105
-- | Packages that are built only for the testsuite.
testsuitePackages :: Action [Package]
testsuitePackages = return [checkPpr]

Andrey Mokhov's avatar
Andrey Mokhov committed
106 107 108 109
-- | Given a 'Context', compute the name of the program that is built in it
-- assuming that the corresponding package's type is 'Program'. For example, GHC
-- built in 'Stage0' is called @ghc-stage1@. If the given package is a
-- 'Library', the function simply returns its name.
110 111 112 113 114 115 116 117 118 119 120
programName :: Context -> Action String
programName Context {..} = do
    cross <- crossCompiling
    targetPlatform <- setting TargetPlatformFull
    let prefix = if cross then targetPlatform ++ "-" else ""
      in return $ prefix ++ case package of
                              p | p == ghc      -> "ghc"
                                | p == hpcBin   -> "hpc"
                                | p == runGhc   -> "runhaskell"
                                | p == iservBin -> "ghc-iserv"
                              _                 ->  pkgName package
121

Andrey Mokhov's avatar
Andrey Mokhov committed
122 123 124 125 126 127
-- | The build stage whose results are used when installing a package, or
-- @Nothing@ if the package is not installed, e.g. because it is a user package.
-- The current implementation installs the /latest/ build stage of a package.
installStage :: Package -> Action (Maybe Stage)
installStage pkg
    | not (isGhcPackage pkg) = return Nothing -- Only GHC packages are installed
128
    | otherwise = do
129
        stages <- filterM (fmap (pkg `elem`) . defaultPackages) [Stage0 ..]
Andrey Mokhov's avatar
Andrey Mokhov committed
130
        return $ if null stages then Nothing else Just (maximum stages)
131

132 133 134
-- | The 'FilePath' to a program executable in a given 'Context'.
programPath :: Context -> Action FilePath
programPath context@Context {..} = do
Andrey Mokhov's avatar
Andrey Mokhov committed
135 136 137
    -- The @touchy@ utility lives in the @lib/bin@ directory instead of @bin@,
    -- which is likely just a historical accident that will hopefully be fixed.
    -- See: https://github.com/snowleopard/hadrian/issues/570
138 139 140 141
    -- Likewise for 'unlit'.
    path <- if package `elem` [touchy, unlit]
      then stageLibPath stage <&> (-/- "bin")
      else stageBinPath stage
Andrey Mokhov's avatar
Andrey Mokhov committed
142
    pgm  <- programName context
143
    return $ path -/- pgm <.> exe
144

Douglas Wilson's avatar
Douglas Wilson committed
145
-- | Some contexts are special: their packages do not have @.cabal@ metadata or
146 147 148
-- we cannot run @ghc-cabal@ on them, e.g. because the latter hasn't been built
-- yet (this is the case with the 'ghcCabal' package in 'Stage0').
nonCabalContext :: Context -> Bool
149 150 151
nonCabalContext Context {..} = (package `elem` [ hp2ps
                                               , touchy
                                               ])
152
    || package == ghcCabal && stage == Stage0
153 154 155 156

-- | Some program packages should not be linked with Haskell main function.
nonHsMainPackage :: Package -> Bool
nonHsMainPackage = (`elem` [ghc, hp2ps, iservBin, touchy, unlit])
157 158 159 160 161 162 163 164

-- | Path to the autogen directory generated by @ghc-cabal@ of a given 'Context'.
autogenPath :: Context -> Action FilePath
autogenPath context@Context {..}
    | isLibrary package   = autogen "build"
    | package == ghc      = autogen "build/ghc"
    | package == hpcBin   = autogen "build/hpc"
    | package == iservBin = autogen "build/iserv"
Andrey Mokhov's avatar
Andrey Mokhov committed
165
    | otherwise           = autogen $ "build" -/- pkgName package
166
  where
167 168
    autogen dir = contextPath context <&> (-/- dir -/- "autogen")

Andrey Mokhov's avatar
Andrey Mokhov committed
169 170 171 172
buildDll0 :: Context -> Action Bool
buildDll0 Context {..} = do
    windows <- windowsHost
    return $ windows && stage == Stage1 && package == compiler