Compile.hs 2.35 KB
Newer Older
1
{-# LANGUAGE NoImplicitPrelude #-}
Andrey Mokhov's avatar
Andrey Mokhov committed
2
3
4
5
6
module Package.Compile (buildPackageCompile) where

import Package.Base
import Development.Shake.Util

7
8
argListDir :: FilePath
argListDir = "shake/arg/buildPackageCompile"
Andrey Mokhov's avatar
Andrey Mokhov committed
9
10

suffixArgs :: Way -> Args
11
12
suffixArgs way =
    return ["-hisuf", hisuf way, "-osuf", osuf way, "-hcsuf", hcsuf way]
Andrey Mokhov's avatar
Andrey Mokhov committed
13

14
15
ghcArgs :: Package -> TodoItem -> Way -> [FilePath] -> FilePath -> Args
ghcArgs (Package _ path _) (stage, dist, _) way srcs result =
Andrey Mokhov's avatar
Andrey Mokhov committed
16
17
    let pathDist = path </> dist
        buildDir = toStandard $ pathDist </> "build"
18
19
20
21
22
23
24
25
26
27
28
29
30
    in args [ suffixArgs way
            , wayHcArgs way
            , args SrcHcArgs
            , packageArgs stage pathDist
            , includeArgs path dist
            , concatArgs ["-optP"] $ CppArgs pathDist
            , args $ HsArgs pathDist
            -- TODO: now we have both -O and -O2
            -- <> arg ["-O2"]
            , productArgs ["-odir", "-hidir", "-stubdir"] buildDir
            , when (splitObjects stage) $ arg "-split-objs"
            , args ("-c":srcs)
            , args ["-o", result] ]
31
32
33
34

buildRule :: Package -> TodoItem -> Rules ()
buildRule pkg @ (Package name path _) todo @ (stage, dist, _) =
    let buildDir = toStandard $ path </> dist </> "build"
35
        depFile  = buildDir </> takeBaseName name <.> "m"
Andrey Mokhov's avatar
Andrey Mokhov committed
36
    in
37
    [buildDir <//> "*o", buildDir <//> "*hi"] &%> \[out, _] -> do
Andrey Mokhov's avatar
Andrey Mokhov committed
38
        let way  = detectWay $ tail $ takeExtension out
39
        need [argListPath argListDir pkg stage, depFile]
Andrey Mokhov's avatar
Andrey Mokhov committed
40
41
        depContents <- parseMakefile <$> (liftIO $ readFile depFile)
        let deps = concat $ snd $ unzip $ filter ((== out) . fst) depContents
42
            srcs = filter ("//*hs" ?==) deps -- TODO: handle *.c sources
Andrey Mokhov's avatar
Andrey Mokhov committed
43
        need deps
44
        terseRun (Ghc stage) $ ghcArgs pkg todo way srcs (toStandard out)
Andrey Mokhov's avatar
Andrey Mokhov committed
45

46
47
48
49
50
51
argListRule :: Package -> TodoItem -> Rules ()
argListRule pkg todo @ (stage, _, settings) =
    (argListPath argListDir pkg stage) %> \out -> do
        need $ ["shake/src/Package/Compile.hs"] ++ sourceDependecies
        ways' <- ways settings
        ghcList <- forM ways' $ \way ->
52
53
54
            argListWithComment
                ("way '" ++ tag way ++ "'")
                (Ghc stage)
55
56
57
58
59
                (ghcArgs pkg todo way ["input.hs"] $ "output" <.> osuf way)
        writeFileChanged out $ unlines ghcList

buildPackageCompile :: Package -> TodoItem -> Rules ()
buildPackageCompile = argListRule <> buildRule