]> git.tdb.fi Git - builder.git/blob - source/target.h
Miscellaneous minor code cleanups
[builder.git] / source / target.h
1 /* $Id$
2
3 This file is part of builder
4 Copyright © 2006-2009  Mikko Rasa, Mikkosoft Productions
5 Distributed under the LGPL
6 */
7
8 #ifndef TARGET_H_
9 #define TARGET_H_
10
11 #include <list>
12 #include <map>
13 #include <string>
14 #include <msp/time/timestamp.h>
15
16 class Action;
17 class Builder;
18 class Package;
19
20 class Target;
21 typedef std::list<Target *> TargetList;
22
23 /**
24 Targets make up the build graph.  This class is a base for all target types and
25 handles many common tasks.  Most targets are associated with a file.
26 */
27 class Target
28 {
29 protected:
30         Builder &builder;
31         const Package *package;
32         std::string name;
33         Msp::Time::TimeStamp mtime;
34
35         bool buildable;
36         bool building;
37         bool rebuild;
38         std::string rebuild_reason;
39
40         TargetList depends;
41         TargetList rdepends;
42         bool deps_ready;
43
44         bool prepared;
45         bool counted;
46
47         Target(Builder &, const Package *, const std::string &);
48 public:
49         virtual ~Target() { }
50
51         virtual const char *get_type() const=0;
52         const std::string &get_name() const { return name; }
53         const Package *get_package() const { return package; }
54         const Msp::Time::TimeStamp &get_mtime() const { return mtime; }
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         Target *get_buildable_target();
62
63         bool is_buildable() const { return buildable; }
64         bool get_rebuild() const { return rebuild; }
65         const std::string &get_rebuild_reason() const { return rebuild_reason; }
66         void add_depend(Target *);
67         const TargetList &get_depends() const { return depends; }
68         bool get_depends_ready() const { return deps_ready; }
69
70         /**
71         Finds dependencies for the target.  When all dependencies have been found,
72         the function should set deps_ready to true.
73         */
74         virtual void find_depends() { deps_ready=true; }
75
76         /**
77         Prepares the target by recursively preparing dependencies, then checking
78         whether rebuilding is needed.  A flag is used to prevent unnecessary
79         executions.
80         */
81         virtual void prepare();
82
83         /**
84         Starts building the target.  Returns the Action used for building.
85         */
86         Action *build();
87
88         void reset_count() { counted=false; }
89
90         /**
91         Returns the number of targets that need to be rebuilt in order to get this
92         target up-to-date.
93         */
94         virtual unsigned count_rebuild();
95
96         /**
97         Changes the mtime of the target to the current time.
98         */
99         void touch();
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();
107
108         /**
109         Creates and returns an Action suitable for building this target.
110         */
111         virtual Action *create_action() =0;
112
113         /**
114         Handles for the build_done signal of Action.
115         */
116         virtual void build_done();
117 };
118
119 typedef std::map<std::string, Target *> TargetMap;
120
121 #endif