#ifndef TARGET_H_
#define TARGET_H_
-#include <list>
#include <map>
#include <set>
#include <string>
+#include <vector>
+#include <sigc++/signal.h>
#include <msp/time/timestamp.h>
class Builder;
-class Package;
+class BuildInfo;
+class Component;
+class SourcePackage;
class Task;
class Tool;
-class Target;
-typedef std::list<Target *> TargetList;
-
/**
Targets make up the build graph. This class is a base for all target types and
handles many common tasks. See also FileTarget and VirtualTarget.
+
+Targets can depend on other targets. There are two kinds of dependencies:
+normal and transitive. Normal dependencies will need to be built before the
+target itself, and will cause the target to be rebuilt if modified. Transitive
+dependencies can be used by other targets further down the chain.
*/
class Target
{
+public:
+ using Dependencies = std::vector<Target *>;
+
+protected:
+ enum State
+ {
+ INIT,
+ PREPARING,
+ REBUILD,
+ BUILDING,
+ UPTODATE,
+ BROKEN
+ };
+
+public:
+ sigc::signal<void> signal_bubble_rebuild;
+ sigc::signal<void> signal_modified;
+
protected:
Builder &builder;
- const Package *package;
+ const SourcePackage *package = 0;
+ const Component *component = 0;
std::string name;
- const Tool *tool;
- bool buildable;
- bool building;
- bool rebuild;
+ Tool *tool = 0;
+ State state = INIT;
std::string rebuild_reason;
+ std::vector<std::string> problems;
- TargetList depends;
- bool deps_ready;
-
- bool preparing;
- bool prepared;
+ Dependencies depends;
+ Dependencies trans_depends;
+ Dependencies side_effects;
+ Target *primary_target = 0;
- Target(Builder &, const Package *, const std::string &);
+ Target(Builder &, const std::string &);
public:
virtual ~Target() { }
virtual const char *get_type() const = 0;
const std::string &get_name() const { return name; }
- const Package *get_package() const { return package; }
+ const SourcePackage *get_package() const { return package; }
+ const Component *get_component() const { return component; }
- /**
- Tries to locate a target that will help getting this target built. If all
- dependencies are up-to-date, returns this target. If there are no targets
- ready to be built (maybe because they are being built right now), returns 0.
- */
- virtual Target *get_buildable_target();
+ /** Adds a dependency for the target. Order is preseved and is important
+ for some target types. It is an error to create dependency cycles, although
+ this won't be detected until the targets are prepared. */
+ void add_dependency(Target &);
+
+ void add_transitive_dependency(Target &);
+
+ /** Adds a side effect for the target. Side effects are not built on their
+ own, but together with their primary target. */
+ void add_side_effect(Target &);
+
+protected:
+ /** Finds dependencies for the target. Called during preparation. If the
+ target needs to recursively inspect its dependencies, it should prepare its
+ direct dependencies first. */
+ virtual void find_dependencies() { }
+
+public:
+ /// Returns the dependencies of the target, in the order they were added.
+ const Dependencies &get_dependencies() const { return depends; }
+
+ const Dependencies &get_transitive_dependencies() const { return trans_depends; }
+
+ /// Returns the side effects of the target.
+ const Dependencies &get_side_effects() const { return side_effects; }
- /**
- If this target is a proxy for another (such as Install or Symlink), return
- that target. Otherwise, return the target itself.
+ /// Returns the primary target associated with a side effect target.
+ Target *get_primary_target() const { return primary_target; }
- Implementors should call the function recursively to find the final target.
- */
+ /** Tries to locate a target that will help getting this target built. If
+ all dependencies are up-to-date, returns this target. If there are no
+ targets ready to be built (maybe because they are being built right now),
+ returns 0. */
+ virtual Target *get_buildable_target();
+
+ /** If this target is a proxy for another (such as InstalledFile), return
+ that target. Otherwise, return the target itself. Implementors should call
+ the function recursively to find the final target. */
virtual Target *get_real_target() { return this; }
- void set_tool(const Tool &);
+ void set_tool(Tool &);
+
+ /** Returns the tool used to build the target. To actually build it, call
+ the build() function. */
const Tool *get_tool() const { return tool; }
- bool is_buildable() const { return buildable; }
- bool get_rebuild() const { return rebuild; }
+ virtual void collect_build_info(BuildInfo &) const;
+
+ /** Indicates if it's possible to build this target. */
+ bool is_buildable() const { return tool!=0; }
+
+ /** Indicates if this target needs rebuilding. Only makes sense after the
+ target has been prepared. */
+ bool needs_rebuild() const { return state>PREPARING && state<UPTODATE; }
+
+ /** Returns the reason for rebuilding this target. Only makes sense after
+ the target has been prepared. */
const std::string &get_rebuild_reason() const { return rebuild_reason; }
- void add_depend(Target *);
- const TargetList &get_depends() const { return depends; }
- bool get_depends_ready() const { return deps_ready; }
-
- /**
- Finds dependencies for the target. When all dependencies have been found,
- the function should set deps_ready to true.
- */
- virtual void find_depends() { deps_ready = true; }
-
- /**
- Prepares the target by recursively preparing dependencies, then checking
- whether rebuilding is needed. A flag is used to prevent unnecessary
- executions.
- */
- virtual void prepare();
-
- /**
- Starts building the target. Returns the Action used for building.
- */
- Task *build();
+
+ /** Forces rebuild of the target. */
+ void force_rebuild();
+
protected:
+ /** Marks the target to be rebuilt and specified a reason for it. */
void mark_rebuild(const std::string &);
- /**
- Checks if the target needs to be rebuilt and why.
- */
+ /** Checks if the target needs to be rebuilt and why. */
virtual void check_rebuild() = 0;
- /**
- Handler for the build_finished signal of Task.
- */
+public:
+ bool is_broken() const { return state==BROKEN; }
+
+ const std::vector<std::string> &get_problems() const { return problems; }
+
+ /** Prepares the target by finding dependencies, recursively preparing them
+ and then checking whether rebuilding is needed. */
+ void prepare();
+
+ /** Invokes the associated Tool to build the target and returns the
+ resulting Task. The task must be started by the caller. */
+ virtual Task *build();
+
+protected:
+ /** Targets can override this to do additional setup on the Task. This is
+ also called on side effects, which normally do not get built by themselves. */
+ virtual void build(Task &) { }
+
+ /** Handler for Task::signal_finished. */
virtual void build_finished(bool);
+
+ virtual void modified() { }
+
+public:
+ /** Removes any results of building the target. */
+ virtual void clean() { }
};
#endif