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