Program.hs 3.73 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
buildProgram rs context@Context {..} = when (isProgram package) $ do
Andrey Mokhov's avatar
Andrey Mokhov committed
32
    let installStage = do
Andrey Mokhov's avatar
Andrey Mokhov committed
33
            latest <- latestBuildStage package -- fromJust below is safe
Andrey Mokhov's avatar
Andrey Mokhov committed
34
35
            return $ if package == ghc then stage else fromJust latest

Andrey Mokhov's avatar
Andrey Mokhov committed
36
37
38
39
    buildPath context -/- programName context <.> exe %>
        buildBinaryAndWrapper rs context

    -- Rules for programs built in install directories
Andrey Mokhov's avatar
Andrey Mokhov committed
40
41
42
43
    when (stage == Stage0 || package == ghc) $ do
        -- Some binaries in programInplacePath are wrapped
        programInplacePath -/- programName context <.> exe %> \bin -> do
            binStage <- installStage
Andrey Mokhov's avatar
Andrey Mokhov committed
44
            buildBinaryAndWrapper rs (context { stage = binStage }) bin
Andrey Mokhov's avatar
Andrey Mokhov committed
45
46
47
48
        -- We build only unwrapped binaries in programInplaceLibPath
        programInplaceLibPath -/- programName context <.> exe %> \bin -> do
            binStage <- installStage
            buildBinary rs (context { stage = binStage }) bin
Andrey Mokhov's avatar
Andrey Mokhov committed
49
50
51
52
53
54
55
56
57
58
59
60

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
61

62
buildWrapper :: Context -> Wrapper -> FilePath -> FilePath -> Action ()
63
buildWrapper context@Context {..} wrapper wrapperPath binPath = do
64
    contents <- interpretInContext context $ wrapper binPath
Andrey Mokhov's avatar
Andrey Mokhov committed
65
    writeFileChanged wrapperPath contents
66
    makeExecutable wrapperPath
Andrey Mokhov's avatar
Andrey Mokhov committed
67
68
    putSuccess $ "| Successfully created wrapper for " ++
        quote (pkgNameString package) ++ " (" ++ show stage ++ ")."
Andrey Mokhov's avatar
Andrey Mokhov committed
69
70

-- TODO: Get rid of the Paths_hsc2hs.o hack.
71
buildBinary :: [(Resource, Int)] -> Context -> FilePath -> Action ()
Andrey Mokhov's avatar
Andrey Mokhov committed
72
73
buildBinary rs context@Context {..} bin = do
    binDeps <- if stage == Stage0 && package == ghcCabal
74
        then hsSources context
Andrey Mokhov's avatar
Andrey Mokhov committed
75
        else do
76
77
78
79
            needContext =<< contextDependencies context
            when (package == ghc) $ do
                ways <- interpretInContext context (getLibraryWays <> getRtsWays)
                needContext [ rtsContext { way = w } | w <- ways ]
Andrey Mokhov's avatar
Andrey Mokhov committed
80
            let path = buildPath context
81
82
83
84
85
            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
86
    need binDeps
87
    buildWithResources rs $ Target context (Ghc LinkHs stage) binDeps [bin]
88
    synopsis <- interpretInContext context $ getPkgData Synopsis
Moritz Angermann's avatar
Moritz Angermann committed
89
    putSuccess $ renderProgram
Andrey Mokhov's avatar
Andrey Mokhov committed
90
        (quote (pkgNameString package) ++ " (" ++ show stage ++ ").")
Moritz Angermann's avatar
Moritz Angermann committed
91
92
        bin
        (dropWhileEnd isPunctuation synopsis)