Add comments/todos.
[hadrian.git] / src / Target.hs
1 {-# LANGUAGE DeriveGeneric, FlexibleInstances #-}
2 module Target (
3 Target (..), PartialTarget (..), fromPartial, fullTarget, fullTargetWithWay
4 ) where
5
6 import Base
7 import Builder
8 import GHC.Generics (Generic)
9 import Package
10 import Stage
11 import Way
12
13 -- Target captures all parameters relevant to the current build target:
14 -- * Stage and Package being built,
15 -- * Builder to be invoked,
16 -- * Way to be built (set to vanilla for most targets),
17 -- * source file(s) to be passed to Builder,
18 -- * file(s) to be produced.
19 data Target = Target
20 {
21 stage :: Stage,
22 package :: Package,
23 builder :: Builder,
24 way :: Way,
25 sources :: [FilePath], -- input
26 files :: [FilePath] -- output
27 }
28 deriving (Show, Eq, Generic)
29
30 -- If values of type 'a' form a Monoid then we can also derive a Monoid instance
31 -- for values of type 'ReaderT Target Action a':
32 -- * the empty computation returns the identity element of the underlying type
33 -- * two computations can be combined by combining their results
34 instance Monoid a => Monoid (ReaderT Target Action a) where
35 mempty = return mempty
36 mappend = liftM2 mappend
37
38 -- PartialTarget is a partially constructed Target with fields Stage and
39 -- Package only. PartialTarget's are used for generating build rules.
40 data PartialTarget = PartialTarget Stage Package
41
42 -- Convert PartialTarget to Target assuming that unknown fields won't be used.
43 fromPartial :: PartialTarget -> Target
44 fromPartial (PartialTarget s p) = Target
45 {
46 stage = s,
47 package = p,
48 builder = error "fromPartial: builder not set",
49 way = error "fromPartial: way not set",
50 sources = error "fromPartial: sources not set",
51 files = error "fromPartial: files not set"
52 }
53
54 -- Construct a full target by augmenting a PartialTarget with missing fields.
55 -- Most targets are built only one way, vanilla, hence we set it by default.
56 fullTarget :: PartialTarget -> Builder -> [FilePath] -> [FilePath] -> Target
57 fullTarget (PartialTarget s p) b srcs fs = Target
58 {
59 stage = s,
60 package = p,
61 builder = b,
62 way = vanilla,
63 sources = map unifyPath srcs,
64 files = map unifyPath fs
65 }
66
67 -- Use this function to be explicit about the build way.
68 fullTargetWithWay :: PartialTarget -> Builder -> Way -> [FilePath] -> [FilePath] -> Target
69 fullTargetWithWay pt b w srcs fs = (fullTarget pt b srcs fs) { way = w }
70
71 -- Instances for storing in the Shake database
72 instance Binary Target
73 instance NFData Target
74 instance Hashable Target