]> git.tdb.fi Git - builder.git/blob - source/target.h
Check an ObjectFile's dependencies again if a dependency is modified
[builder.git] / source / target.h
1 #ifndef TARGET_H_
2 #define TARGET_H_
3
4 #include <list>
5 #include <map>
6 #include <set>
7 #include <string>
8 #include <sigc++/signal.h>
9 #include <msp/time/timestamp.h>
10
11 class Builder;
12 class Component;
13 class SourcePackage;
14 class Task;
15 class Tool;
16
17 /**
18 Targets make up the build graph.  This class is a base for all target types and
19 handles many common tasks.  See also FileTarget and VirtualTarget.
20
21 Targets can depend on other targets.  There are two kinds of dependencies:
22 normal and transitive.  Normal dependencies will need to be built before the
23 target itself, and will cause the target to be rebuilt if modified.  Transitive
24 dependencies can be used by other targets further down the chain.
25 */
26 class Target
27 {
28 public:
29         typedef std::list<Target *> Dependencies;
30
31 protected:
32         enum State
33         {
34                 INIT,
35                 PREPARING,
36                 REBUILD,
37                 BUILDING,
38                 UPTODATE,
39                 BROKEN
40         };
41
42 public:
43         sigc::signal<void> signal_bubble_rebuild;
44         sigc::signal<void> signal_modified;
45
46 protected:
47         Builder &builder;
48         const SourcePackage *package;
49         const Component *component;
50         std::string name;
51
52         Tool *tool;
53         State state;
54         std::string rebuild_reason;
55         std::list<std::string> problems;
56
57         Dependencies depends;
58         Dependencies trans_depends;
59         Dependencies side_effects;
60         Target *primary_target;
61
62         Target(Builder &, const std::string &);
63 public:
64         virtual ~Target() { }
65
66         virtual const char *get_type() const = 0;
67         const std::string &get_name() const { return name; }
68         const SourcePackage *get_package() const { return package; }
69         const Component *get_component() const { return component; }
70
71         /** Adds a dependency for the target.  Order is preseved and is important
72         for some target types.  It is an error to create dependency cycles, although
73         this won't be detected until the targets are prepared. */
74         void add_dependency(Target &);
75
76         void add_transitive_dependency(Target &);
77
78         /** Adds a side effect for the target.  Side effects are not built on their
79         own, but together with their primary target. */
80         void add_side_effect(Target &);
81
82 protected:
83         /** Finds dependencies for the target.  Called during preparation.  If the
84         target needs to recursively inspect its dependencies, it should prepare its
85         direct dependencies first. */
86         virtual void find_dependencies() { }
87
88 public:
89         /// Returns the dependencies of the target, in the order they were added.
90         const Dependencies &get_dependencies() const { return depends; }
91
92         const Dependencies &get_transitive_dependencies() const { return trans_depends; }
93
94         /// Returns the side effects of the target.
95         const Dependencies &get_side_effects() const { return side_effects; }
96
97         /// Returns the primary target associated with a side effect target.
98         Target *get_primary_target() const { return primary_target; }
99
100         /** Tries to locate a target that will help getting this target built.  If
101         all dependencies are up-to-date, returns this target.  If there are no
102         targets ready to be built (maybe because they are being built right now),
103         returns 0. */
104         virtual Target *get_buildable_target();
105
106         /** If this target is a proxy for another (such as InstalledFile), return
107         that target.  Otherwise, return the target itself.  Implementors should call
108         the function recursively to find the final target. */
109         virtual Target *get_real_target() { return this; }
110
111         void set_tool(Tool &);
112
113         /** Returns the tool used to build the target.  To actually build it, call
114         the build() function. */
115         const Tool *get_tool() const { return tool; }
116
117         /** Indicates if it's possible to build this target. */
118         bool is_buildable() const { return tool!=0; }
119
120         /** Indicates if this target needs rebuilding.  Only makes sense after the
121         target has been prepared. */
122         bool needs_rebuild() const { return state>PREPARING && state<UPTODATE; }
123
124         /** Returns the reason for rebuilding this target.  Only makes sense after
125         the target has been prepared. */
126         const std::string &get_rebuild_reason() const { return rebuild_reason; }
127
128         /** Forces rebuild of the target. */
129         void force_rebuild();
130
131 protected:
132         /** Marks the target to be rebuilt and specified a reason for it. */
133         void mark_rebuild(const std::string &);
134
135         /** Checks if the target needs to be rebuilt and why. */
136         virtual void check_rebuild() = 0;
137
138 public:
139         bool is_broken() const { return state==BROKEN; }
140
141         const std::list<std::string> &get_problems() const { return problems; }
142
143         /** Prepares the target by finding dependencies, recursively preparing them
144         and then checking whether rebuilding is needed. */
145         void prepare();
146
147         /** Invokes the associated Tool to build the target and returns the
148         resulting Task.  The task must be started by the caller. */
149         virtual Task *build();
150
151 protected:
152         /** Targets can override this to do additional setup on the Task.  This is
153         also called on side effects, which normally do not get built by themselves. */
154         virtual void build(Task &) { }
155
156         /** Handler for Task::signal_finished. */
157         virtual void build_finished(bool);
158
159         virtual void modified() { }
160
161 public:
162         /** Removes any results of building the target. */
163         virtual void clean() { }
164 };
165
166 #endif