Actions.hs 2.22 KB
Newer Older
Andrey Mokhov's avatar
Andrey Mokhov committed
1
module Rules.Actions (
2
    build, buildWhen, run, verboseRun,
Andrey Mokhov's avatar
Andrey Mokhov committed
3
4
5
6
7
    ) where

import Util
import Builder
import Expression
8
import Settings.Args
Andrey Mokhov's avatar
Andrey Mokhov committed
9
import Oracles.ArgsHash
10
import Development.Shake
Andrey Mokhov's avatar
Andrey Mokhov committed
11
12
13
14
15
16
17
18
19
20
21
22
23

-- Build a given target using an appropriate builder. Force a rebuilt if the
-- argument list has changed since the last built (that is, track changes in
-- the build system).
build :: FullTarget -> Action ()
build target = do
    argList <- interpret target args
    putColoured Green (show target)
    putColoured Green (show argList)
    -- The line below forces the rule to be rerun if the args hash has changed
    argsHash <- askArgsHash target
    run (getBuilder target) argList

24
25
26
27
28
buildWhen :: Predicate -> FullTarget -> Action ()
buildWhen predicate target = do
    bool <- interpretExpr target predicate
    when bool $ build target

Andrey Mokhov's avatar
Andrey Mokhov committed
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
-- Run the builder with a given collection of arguments
verboseRun :: Builder -> [String] -> Action ()
verboseRun builder args = do
    needBuilder builder
    path <- builderPath builder
    cmd [path] args

-- Run the builder with a given collection of arguments printing out a
-- terse commentary with only 'interesting' info for the builder.
run :: Builder -> [String] -> Action ()
run builder args = do
    putColoured White $ "/--------\n" ++
        "| Running " ++ show builder ++ " with arguments:"
    mapM_ (putColoured White . ("|   " ++)) $ interestingInfo builder args
    putColoured White $ "\\--------"
    quietly $ verboseRun builder args

interestingInfo :: Builder -> [String] -> [String]
interestingInfo builder ss = case builder of
    Ar       -> prefixAndSuffix 2 1 ss
    Ld       -> prefixAndSuffix 4 0 ss
    Gcc _    -> if head ss == "-MM"
                then prefixAndSuffix 1 1 ss
                else prefixAndSuffix 0 4 ss
    Ghc _    -> if head ss == "-M"
                then prefixAndSuffix 1 1 ss
                else prefixAndSuffix 0 4 ss
    GhcPkg _ -> prefixAndSuffix 3 0 ss
    GhcCabal -> prefixAndSuffix 3 0 ss
    _        -> ss
  where
    prefixAndSuffix n m ss =
        if length ss <= n + m + 1
        then ss
        else take n ss
             ++ ["... skipping "
             ++ show (length ss - n - m)
             ++ " arguments ..."]
             ++ drop (length ss - m) ss