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