8 #include <sigc++/signal.h>
9 #include <msp/time/timestamp.h>
10 #include "libbuilder_api.h"
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.
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.
28 class LIBBUILDER_API Target
31 using Dependencies = std::vector<Target *>;
45 sigc::signal<void> signal_bubble_rebuild;
46 sigc::signal<void> signal_modified;
50 const SourcePackage *package = 0;
51 const Component *component = 0;
56 std::string rebuild_reason;
57 std::vector<std::string> problems;
60 Dependencies trans_depends;
61 Dependencies side_effects;
62 Target *primary_target = 0;
64 Target(Builder &, const std::string &);
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; }
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 &);
78 void add_transitive_dependency(Target &);
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 &);
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() { }
91 /// Returns the dependencies of the target, in the order they were added.
92 const Dependencies &get_dependencies() const { return depends; }
94 const Dependencies &get_transitive_dependencies() const { return trans_depends; }
96 /// Returns the side effects of the target.
97 const Dependencies &get_side_effects() const { return side_effects; }
99 /// Returns the primary target associated with a side effect target.
100 Target *get_primary_target() const { return primary_target; }
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),
106 virtual Target *get_buildable_target();
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; }
113 void set_tool(Tool &);
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; }
119 virtual void collect_build_info(BuildInfo &) const;
121 /** Indicates if it's possible to build this target. */
122 bool is_buildable() const { return tool!=0; }
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; }
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; }
132 /** Forces rebuild of the target. */
133 void force_rebuild();
136 /** Marks the target to be rebuilt and specified a reason for it. */
137 void mark_rebuild(const std::string &);
139 /** Checks if the target needs to be rebuilt and why. */
140 virtual void check_rebuild() = 0;
143 bool is_broken() const { return state==BROKEN; }
145 const std::vector<std::string> &get_problems() const { return problems; }
147 /** Prepares the target by finding dependencies, recursively preparing them
148 and then checking whether rebuilding is needed. */
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();
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 &) { }
160 /** Handler for Task::signal_finished. */
161 virtual void build_finished(bool);
163 virtual void modified() { }
166 /** Removes any results of building the target. */
167 virtual void clean() { }