]> git.tdb.fi Git - builder.git/blob - source/target.cpp
Better encapsulation of config inside Package
[builder.git] / source / target.cpp
1 #include <msp/path/utils.h>
2 #include <msp/time/utils.h>
3 #include "action.h"
4 #include "builder.h"
5 #include "package.h"
6 #include "target.h"
7
8 using namespace std;
9 using namespace Msp;
10
11 /**
12 Tries to locate a target that will help getting this target built.  If all
13 dependencies are up-to-date, returns this target.  If there are no targets
14 ready to be built (maybe because they are being built right now), returns 0.
15 */
16 Target *Target::get_buildable_target()
17 {
18         if(!rebuild)
19                 return 0;
20
21         bool self_ok=!building;
22         for(TargetList::iterator i=depends.begin(); i!=depends.end(); ++i)
23         {
24                 Target *tgt=(*i)->get_buildable_target();
25                 if(tgt)
26                         return tgt;
27                 else if((*i)->get_rebuild())
28                         self_ok=false;
29         }
30
31         if(self_ok)
32                 return this;
33
34         return 0;
35 }
36
37 void Target::add_depend(Target *dep)
38 {
39         depends.push_back(dep);
40         dep->rdepends.push_back(this);
41 }
42
43 /**
44 Prepares the target by recursively preparing dependencies, then checking
45 whether rebuilding is needed.  A flag is used to prevent unnecessary
46 executions.
47 */
48 void Target::prepare()
49 {
50         if(prepared)
51                 return;
52
53         prepared=true;
54         for(TargetList::iterator i=depends.begin(); i!=depends.end(); ++i)
55                 (*i)->prepare();
56
57         check_rebuild();
58
59 }
60
61 /**
62 Returns the number of targets that need to be rebuilt in order to get this
63 target up-to-date.
64 */
65 unsigned Target::count_rebuild()
66 {
67         if(counted)
68                 return 0;
69
70         counted=true;
71         unsigned count=rebuild;
72         for(TargetList::iterator i=depends.begin(); i!=depends.end(); ++i)
73                 count+=(*i)->count_rebuild();
74         return count;
75 }
76
77 /**
78 Changes the mtime of the target to the current time.
79 */
80 void Target::touch()
81 {
82         mtime=Time::now();
83 }
84
85 Target::Target(Builder &b, const Package *p, const string &n):
86         builder(b),
87         package(p),
88         name(n),
89         buildable(false),
90         building(false),
91         rebuild(false),
92         deps_ready(false),
93         prepared(false),
94         counted(false)
95 {
96         struct stat st;
97         if(!Path::stat(name, st))
98                 mtime=Time::TimeStamp::from_unixtime(st.st_mtime);
99 }
100
101 void Target::mark_rebuild(const std::string &reason)
102 {
103         rebuild=true;
104         rebuild_reason=reason;
105 }
106
107 /**
108 Checks if this target needs to be rebuilt and why.
109 */
110 void Target::check_rebuild()
111 {
112         if(!buildable)
113                 return;
114
115         if(builder.get_build_all())
116                 mark_rebuild("Rebuilding everything");
117         else if(!mtime)
118                 mark_rebuild("Does not exist");
119         else
120         {
121                 for(TargetList::iterator i=depends.begin(); (i!=depends.end() && !rebuild); ++i)
122                 {
123                         if((*i)->get_mtime()>mtime)
124                                 mark_rebuild(Path::basename((*i)->get_name())+" has changed");
125                         else if((*i)->get_rebuild())
126                                 mark_rebuild(Path::basename((*i)->get_name())+" needs rebuilding");
127                 }
128         }
129         if(!rebuild && package && package->get_config().get_mtime()>mtime)
130                 mark_rebuild("Package options changed");
131 }
132
133 /**
134 Hooks the target up with the given action, then returns it.  This should be
135 called from the public build() function of buildable targets.
136 */
137 Action *Target::build(Action *action)
138 {
139         building=true;
140         action->signal_done.connect(sigc::mem_fun(this, &Target::build_done));
141         return action;
142 }
143
144 /**
145 Handles for the build_done signal of Action.
146 */
147 void Target::build_done()
148 {
149         building=false;
150         rebuild=false;
151 }