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