]> git.tdb.fi Git - builder.git/blob - source/target.h
Big rewrite for a more tool-centric approach
[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 typedef std::list<Target *> TargetList;
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 protected:
25         Builder &builder;
26         const Package *package;
27         std::string name;
28
29         const Tool *tool;
30         bool buildable;
31         bool building;
32         bool rebuild;
33         std::string rebuild_reason;
34
35         TargetList depends;
36         bool deps_ready;
37
38         bool preparing;
39         bool prepared;
40
41         Target(Builder &, const Package *, const std::string &);
42 public:
43         virtual ~Target() { }
44
45         virtual const char *get_type() const = 0;
46         const std::string &get_name() const { return name; }
47         const Package *get_package() const { return package; }
48
49         /**
50         Tries to locate a target that will help getting this target built.  If all
51         dependencies are up-to-date, returns this target.  If there are no targets
52         ready to be built (maybe because they are being built right now), returns 0.
53         */
54         virtual Target *get_buildable_target();
55
56         /**
57         If this target is a proxy for another (such as Install or Symlink), return
58         that target.  Otherwise, return the target itself.
59
60         Implementors should call the function recursively to find the final target.
61         */
62         virtual Target *get_real_target() { return this; }
63
64         void set_tool(const Tool &);
65         const Tool *get_tool() const { return tool; }
66
67         bool is_buildable() const { return buildable; }
68         bool get_rebuild() const { return rebuild; }
69         const std::string &get_rebuild_reason() const { return rebuild_reason; }
70         void add_depend(Target *);
71         const TargetList &get_depends() const { return depends; }
72         bool get_depends_ready() const { return deps_ready; }
73
74         /**
75         Finds dependencies for the target.  When all dependencies have been found,
76         the function should set deps_ready to true.
77         */
78         virtual void find_depends() { deps_ready = true; }
79
80         /**
81         Prepares the target by recursively preparing dependencies, then checking
82         whether rebuilding is needed.  A flag is used to prevent unnecessary
83         executions.
84         */
85         virtual void prepare();
86
87         /**
88         Starts building the target.  Returns the Action used for building.
89         */
90         Task *build();
91 protected:
92         void mark_rebuild(const std::string &);
93
94         /**
95         Checks if the target needs to be rebuilt and why.
96         */
97         virtual void check_rebuild() = 0;
98
99         /**
100         Handler for the build_finished signal of Task.
101         */
102         virtual void build_finished(bool);
103 };
104
105 #endif