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