Program.hs 3.38 KB
Newer Older
Andrey Mokhov's avatar
Andrey Mokhov committed
1
2
module Rules.Program (buildProgram) where

Ben Gamari's avatar
Ben Gamari committed
3
4
import Data.Char

Ben Gamari's avatar
Ben Gamari committed
5
import Base
6
import Context
7
import Expression
8
import GHC
Andrey Mokhov's avatar
Andrey Mokhov committed
9
import Oracles.Config.Setting
Andrey Mokhov's avatar
Andrey Mokhov committed
10
import Oracles.Dependencies
11
import Oracles.ModuleFiles
Andrey Mokhov's avatar
Andrey Mokhov committed
12
import Oracles.PackageData
Andrey Mokhov's avatar
Andrey Mokhov committed
13
import Rules.Wrappers.Ghc
Moritz Angermann's avatar
Moritz Angermann committed
14
import Rules.Wrappers.GhcPkg
Andrey Mokhov's avatar
Andrey Mokhov committed
15
import Settings
16
import Settings.Path
17
import Target
Andrey Mokhov's avatar
Andrey Mokhov committed
18
import UserSettings
19
import Util
Andrey Mokhov's avatar
Andrey Mokhov committed
20

21
-- | Wrapper is an expression depending on the 'FilePath' to the wrapped binary.
Andrey Mokhov's avatar
Andrey Mokhov committed
22
23
type Wrapper = FilePath -> Expr String

Andrey Mokhov's avatar
Andrey Mokhov committed
24
-- | List of wrappers we build.
25
26
27
wrappers :: [(Context, Wrapper)]
wrappers = [ (vanillaContext Stage0 ghc   , ghcWrapper   )
           , (vanillaContext Stage1 ghc   , ghcWrapper   )
28
           , (vanillaContext Stage0 ghcPkg, ghcPkgWrapper) ]
Andrey Mokhov's avatar
Andrey Mokhov committed
29

30
buildProgram :: [(Resource, Int)] -> Context -> Rules ()
Andrey Mokhov's avatar
Andrey Mokhov committed
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
buildProgram rs context@Context {..} = when (isProgram package) $ do
    buildPath context -/- programName context <.> exe %>
        buildBinaryAndWrapper rs context

    -- Rules for programs built in install directories
    when (stage == Stage0 || package == ghc) $
        installPath package -/- programName context <.> exe %> \bin -> do
            latest <- latestBuildStage package -- isJust below is safe
            let binStage = if package == ghc then stage else fromJust latest
            buildBinaryAndWrapper rs (context { stage = binStage }) bin

buildBinaryAndWrapper :: [(Resource, Int)] -> Context -> FilePath -> Action ()
buildBinaryAndWrapper rs context bin = do
    windows <- windowsHost
    if windows
    then buildBinary rs context bin -- We don't build wrappers on Windows
    else case lookup context wrappers of
        Nothing      -> buildBinary rs context bin -- No wrapper found
        Just wrapper -> do
            let wrappedBin = programInplaceLibPath -/- takeFileName bin
            need [wrappedBin]
            buildWrapper context wrapper bin wrappedBin
Andrey Mokhov's avatar
Andrey Mokhov committed
53

54
buildWrapper :: Context -> Wrapper -> FilePath -> FilePath -> Action ()
55
buildWrapper context@Context {..} wrapper wrapperPath binPath = do
56
    contents <- interpretInContext context $ wrapper binPath
Andrey Mokhov's avatar
Andrey Mokhov committed
57
    writeFileChanged wrapperPath contents
58
    makeExecutable wrapperPath
Andrey Mokhov's avatar
Andrey Mokhov committed
59
60
    putSuccess $ "| Successfully created wrapper for " ++
        quote (pkgNameString package) ++ " (" ++ show stage ++ ")."
Andrey Mokhov's avatar
Andrey Mokhov committed
61
62
63

-- TODO: Get rid of the Paths_hsc2hs.o hack.
-- TODO: Do we need to consider other ways when building programs?
64
buildBinary :: [(Resource, Int)] -> Context -> FilePath -> Action ()
Andrey Mokhov's avatar
Andrey Mokhov committed
65
66
buildBinary rs context@Context {..} bin = do
    binDeps <- if stage == Stage0 && package == ghcCabal
67
        then hsSources context
Andrey Mokhov's avatar
Andrey Mokhov committed
68
69
70
71
72
        else do
            ways <- interpretInContext context getLibraryWays
            deps <- contextDependencies context
            needContext [ dep { way = w } | dep <- deps, w <- ways ]
            let path = buildPath context
73
74
75
76
77
            cObjs  <- map (objectPath context) <$> pkgDataList (CSrcs path)
            hsObjs <- hsObjects context
            return $ cObjs ++ hsObjs
                  ++ [ path -/- "Paths_hsc2hs.o"  | package == hsc2hs  ]
                  ++ [ path -/- "Paths_haddock.o" | package == haddock ]
Andrey Mokhov's avatar
Andrey Mokhov committed
78
    need binDeps
79
    buildWithResources rs $ Target context (Ghc LinkHs stage) binDeps [bin]
80
    synopsis <- interpretInContext context $ getPkgData Synopsis
Moritz Angermann's avatar
Moritz Angermann committed
81
    putSuccess $ renderProgram
Andrey Mokhov's avatar
Andrey Mokhov committed
82
        (quote (pkgNameString package) ++ " (" ++ show stage ++ ").")
Moritz Angermann's avatar
Moritz Angermann committed
83
84
        bin
        (dropWhileEnd isPunctuation synopsis)