Program.hs 4.8 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
13
import Oracles.Path (topDirectory)
Zhen Zhang's avatar
Zhen Zhang committed
14
import Rules.Wrappers (WrappedBinary(..), Wrapper, inplaceWrappers)
Andrey Mokhov's avatar
Andrey Mokhov committed
15
import Settings
16
import Settings.Path (buildPath, inplaceLibBinPath, rtsContext, objectPath,
17
                      inplaceLibPath, inplaceBinPath, inplaceLibCopyTargets)
18
import Target
Andrey Mokhov's avatar
Andrey Mokhov committed
19
import UserSettings
20
import Util
Andrey Mokhov's avatar
Andrey Mokhov committed
21

22
buildProgram :: [(Resource, Int)] -> Context -> Rules ()
Andrey Mokhov's avatar
Andrey Mokhov committed
23
buildProgram rs context@Context {..} = when (isProgram package) $ do
Andrey Mokhov's avatar
Andrey Mokhov committed
24
    let installStage = do
Andrey Mokhov's avatar
Andrey Mokhov committed
25
            latest <- latestBuildStage package -- fromJust below is safe
Andrey Mokhov's avatar
Andrey Mokhov committed
26
27
            return $ if package == ghc then stage else fromJust latest

Andrey Mokhov's avatar
Andrey Mokhov committed
28
29
30
    buildPath context -/- programName context <.> exe %>
        buildBinaryAndWrapper rs context

31
32
    when (package == ghc) $ want inplaceLibCopyTargets

Andrey Mokhov's avatar
Andrey Mokhov committed
33
    -- Rules for programs built in install directories
Andrey Mokhov's avatar
Andrey Mokhov committed
34
    when (stage == Stage0 || package == ghc) $ do
35
36
        -- Some binaries in inplace/bin are wrapped
        inplaceBinPath -/- programName context <.> exe %> \bin -> do
Andrey Mokhov's avatar
Andrey Mokhov committed
37
            binStage <- installStage
Andrey Mokhov's avatar
Andrey Mokhov committed
38
            buildBinaryAndWrapper rs (context { stage = binStage }) bin
Zhen Zhang's avatar
Zhen Zhang committed
39

40
        inplaceLibBinPath -/- programName context <.> exe %> \bin -> do
Zhen Zhang's avatar
Zhen Zhang committed
41
42
43
44
45
46
47
48
49
50
            binStage <- installStage
            if package /= iservBin then
                -- We *normally* build only unwrapped binaries in inplace/lib/bin,
                buildBinary rs (context { stage = binStage }) bin
            else
                -- build both binary and wrapper in inplace/lib/bin
                -- for ghc-iserv on *nix platform now
                buildBinaryAndWrapperLib rs (context { stage = binStage }) bin

        inplaceLibBinPath -/- programName context <.> "bin" %> \bin -> do
Andrey Mokhov's avatar
Andrey Mokhov committed
51
52
            binStage <- installStage
            buildBinary rs (context { stage = binStage }) bin
Andrey Mokhov's avatar
Andrey Mokhov committed
53

Zhen Zhang's avatar
Zhen Zhang committed
54
55
56
57
58
59
60
61
62
63
64
65
66
67
buildBinaryAndWrapperLib :: [(Resource, Int)] -> Context -> FilePath -> Action ()
buildBinaryAndWrapperLib rs context bin = do
    windows <- windowsHost
    if windows
    then buildBinary rs context bin -- We don't build wrappers on Windows
    else case lookup context inplaceWrappers of
        Nothing      -> buildBinary rs context bin -- No wrapper found
        Just wrapper -> do
            top <- topDirectory
            let libdir = top -/- inplaceLibPath
            let wrappedBin = inplaceLibBinPath -/- programName context <.> "bin"
            need [wrappedBin]
            buildWrapper context wrapper bin (WrappedBinary libdir (takeFileName wrappedBin))

Andrey Mokhov's avatar
Andrey Mokhov committed
68
69
70
71
72
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
Zhen Zhang's avatar
Zhen Zhang committed
73
    else case lookup context inplaceWrappers of
Andrey Mokhov's avatar
Andrey Mokhov committed
74
75
        Nothing      -> buildBinary rs context bin -- No wrapper found
        Just wrapper -> do
76
77
78
            top <- topDirectory
            let libdir = top -/- inplaceLibPath
            let wrappedBin = inplaceLibBinPath -/- takeFileName bin
Andrey Mokhov's avatar
Andrey Mokhov committed
79
            need [wrappedBin]
80
            buildWrapper context wrapper bin (WrappedBinary libdir (takeFileName bin))
Andrey Mokhov's avatar
Andrey Mokhov committed
81

82
83
84
buildWrapper :: Context -> Wrapper -> FilePath -> WrappedBinary -> Action ()
buildWrapper context@Context {..} wrapper wrapperPath wrapped = do
    contents <- interpretInContext context $ wrapper wrapped
Andrey Mokhov's avatar
Andrey Mokhov committed
85
    writeFileChanged wrapperPath contents
86
    makeExecutable wrapperPath
Andrey Mokhov's avatar
Andrey Mokhov committed
87
88
    putSuccess $ "| Successfully created wrapper for " ++
        quote (pkgNameString package) ++ " (" ++ show stage ++ ")."
Andrey Mokhov's avatar
Andrey Mokhov committed
89
90

-- TODO: Get rid of the Paths_hsc2hs.o hack.
91
buildBinary :: [(Resource, Int)] -> Context -> FilePath -> Action ()
Andrey Mokhov's avatar
Andrey Mokhov committed
92
93
buildBinary rs context@Context {..} bin = do
    binDeps <- if stage == Stage0 && package == ghcCabal
94
        then hsSources context
Andrey Mokhov's avatar
Andrey Mokhov committed
95
        else do
96
            needLibrary =<< contextDependencies context
97
            when (stage > Stage0) $ do
98
                ways <- interpretInContext context (getLibraryWays <> getRtsWays)
99
                needLibrary [ rtsContext { way = w } | w <- ways ]
Andrey Mokhov's avatar
Andrey Mokhov committed
100
            let path = buildPath context
101
102
103
104
105
            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
106
    need binDeps
107
    buildWithResources rs $ target context (Ghc LinkHs stage) binDeps [bin]
108
    synopsis <- interpretInContext context $ getPkgData Synopsis
Moritz Angermann's avatar
Moritz Angermann committed
109
    putSuccess $ renderProgram
Andrey Mokhov's avatar
Andrey Mokhov committed
110
        (quote (pkgNameString package) ++ " (" ++ show stage ++ ").")
Moritz Angermann's avatar
Moritz Angermann committed
111
112
        bin
        (dropWhileEnd isPunctuation synopsis)