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