]> git.tdb.fi Git - builder.git/blob - source/target.h
805b64a09a891ecbea2c2039c1a7c60379b672fd
[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 <set>
14 #include <string>
15 #include <msp/time/timestamp.h>
16
17 class Action;
18 class Builder;
19 class Package;
20
21 class Target;
22 typedef std::list<Target *> TargetList;
23
24 /**
25 Targets make up the build graph.  This class is a base for all target types and
26 handles many common tasks.  Most targets are associated with a file.
27 */
28 class Target
29 {
30 protected:
31         Builder &builder;
32         const Package *package;
33         std::string name;
34         Msp::Time::TimeStamp mtime;
35
36         bool buildable;
37         bool building;
38         bool rebuild;
39         std::string rebuild_reason;
40
41         TargetList depends;
42         TargetList rdepends;
43         bool deps_ready;
44
45         bool preparing;
46         bool prepared;
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         const Msp::Time::TimeStamp &get_mtime() const { return mtime; }
56
57         /**
58         Tries to locate a target that will help getting this target built.  If all
59         dependencies are up-to-date, returns this target.  If there are no targets
60         ready to be built (maybe because they are being built right now), returns 0.
61         */
62         Target *get_buildable_target();
63
64         bool is_buildable() const { return buildable; }
65         bool get_rebuild() const { return rebuild; }
66         const std::string &get_rebuild_reason() const { return rebuild_reason; }
67         void add_depend(Target *);
68         const TargetList &get_depends() const { return depends; }
69         bool get_depends_ready() const { return deps_ready; }
70
71         /**
72         Finds dependencies for the target.  When all dependencies have been found,
73         the function should set deps_ready to true.
74         */
75         virtual void find_depends() { deps_ready=true; }
76
77         /**
78         Prepares the target by recursively preparing dependencies, then checking
79         whether rebuilding is needed.  A flag is used to prevent unnecessary
80         executions.
81         */
82         virtual void prepare();
83
84         /**
85         Starts building the target.  Returns the Action used for building.
86         */
87         Action *build();
88
89         /**
90         Changes the mtime of the target to the current time.
91         */
92         void touch();
93 protected:
94         void mark_rebuild(const std::string &);
95
96         /**
97         Checks if the target needs to be rebuilt and why.
98         */
99         virtual void check_rebuild();
100
101         /**
102         Creates and returns an Action suitable for building this target.
103         */
104         virtual Action *create_action() =0;
105
106         /**
107         Handler for the build_done signal of Action.
108         */
109         virtual void build_done();
110 };
111
112 typedef std::map<std::string, Target *> TargetMap;
113
114 #endif