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