]> git.tdb.fi Git - builder.git/blob - source/target.h
Move the Component reference to Target and make it a pointer
[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 <msp/time/timestamp.h>
9
10 class Builder;
11 class Component;
12 class Package;
13 class Task;
14 class Tool;
15
16 class Target;
17
18 /**
19 Targets make up the build graph.  This class is a base for all target types and
20 handles many common tasks.  See also FileTarget and VirtualTarget.
21 */
22 class Target
23 {
24 public:
25         typedef std::list<Target *> Dependencies;
26
27 protected:
28         enum State
29         {
30                 INIT,
31                 PREPARING,
32                 REBUILD,
33                 BUILDING,
34                 UPTODATE
35         };
36
37         Builder &builder;
38         const Package *package;
39         const Component *component;
40         std::string name;
41
42         const Tool *tool;
43         State state;
44         std::string rebuild_reason;
45         std::string install_location;
46
47         Dependencies depends;
48         bool deps_ready;
49
50         Target(Builder &, const std::string &);
51 public:
52         virtual ~Target() { }
53
54         virtual const char *get_type() const = 0;
55         const std::string &get_name() const { return name; }
56         const Package *get_package() const { return package; }
57         const Component *get_component() const { return component; }
58
59         /**
60         Tries to locate a target that will help getting this target built.  If all
61         dependencies are up-to-date, returns this target.  If there are no targets
62         ready to be built (maybe because they are being built right now), returns 0.
63         */
64         virtual Target *get_buildable_target();
65
66         /**
67         If this target is a proxy for another (such as Install or Symlink), return
68         that target.  Otherwise, return the target itself.
69
70         Implementors should call the function recursively to find the final target.
71         */
72         virtual Target *get_real_target() { return this; }
73
74         void set_tool(const Tool &);
75         const Tool *get_tool() const { return tool; }
76
77         bool is_buildable() const { return tool!=0; }
78         bool needs_rebuild() const { return state>PREPARING && state<UPTODATE; }
79         const std::string &get_rebuild_reason() const { return rebuild_reason; }
80         bool is_installable() const { return !install_location.empty(); }
81         const std::string &get_install_location() const { return install_location; }
82         void add_depend(Target *);
83         const Dependencies &get_depends() const { return depends; }
84         bool get_depends_ready() const { return deps_ready; }
85
86         /**
87         Finds dependencies for the target.  When all dependencies have been found,
88         the function should set deps_ready to true.
89         */
90         virtual void find_depends() { deps_ready = true; }
91
92         /**
93         Prepares the target by recursively preparing dependencies, then checking
94         whether rebuilding is needed.  A flag is used to prevent unnecessary
95         executions.
96         */
97         virtual void prepare();
98
99         /**
100         Starts building the target.  Returns the Action used for building.
101         */
102         Task *build();
103 protected:
104         void mark_rebuild(const std::string &);
105
106         /**
107         Checks if the target needs to be rebuilt and why.
108         */
109         virtual void check_rebuild() = 0;
110
111         /**
112         Handler for the build_finished signal of Task.
113         */
114         virtual void build_finished(bool);
115 };
116
117 #endif