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