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