]> git.tdb.fi Git - builder.git/blob - source/target.h
Implement rdeps mode and sorting in Analyzer
[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         bool deps_ready;
43
44         bool preparing;
45         bool prepared;
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         /**
89         Changes the mtime of the target to the current time.
90         */
91         void touch();
92 protected:
93         void mark_rebuild(const std::string &);
94
95         /**
96         Checks if the target needs to be rebuilt and why.
97         */
98         virtual void check_rebuild();
99
100         /**
101         Creates and returns an Action suitable for building this target.
102         */
103         virtual Action *create_action() =0;
104
105         /**
106         Handler for the build_done signal of Action.
107         */
108         virtual void build_done();
109 };
110
111 typedef std::map<std::string, Target *> TargetMap;
112
113 #endif