]> git.tdb.fi Git - builder.git/blob - source/target.cpp
Rework the Target class hierarchy
[builder.git] / source / target.cpp
1 /* $Id$
2
3 This file is part of builder
4 Copyright © 2006-2009  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 "filetarget.h"
14 #include "package.h"
15 #include "sourcepackage.h"
16 #include "target.h"
17
18 using namespace std;
19 using namespace Msp;
20
21 Target::Target(Builder &b, const Package *p, const string &n):
22         builder(b),
23         package(p),
24         name(n),
25         buildable(false),
26         building(false),
27         rebuild(false),
28         deps_ready(false),
29         prepared(false),
30         counted(false)
31 {
32         builder.add_target(this);
33 }
34
35 Target *Target::get_buildable_target()
36 {
37         if(!rebuild)
38                 return 0;
39
40         bool self_ok=!building;
41         for(TargetList::iterator i=depends.begin(); i!=depends.end(); ++i)
42         {
43                 Target *tgt=(*i)->get_buildable_target();
44                 if(tgt)
45                         return tgt;
46                 else if((*i)->get_rebuild())
47                         self_ok=false;
48         }
49
50         if(self_ok)
51                 return this;
52
53         return 0;
54 }
55
56 void Target::add_depend(Target *dep)
57 {
58         if(dep==this)
59                 throw InvalidParameterValue("A target can't depend on itself");
60         depends.push_back(dep);
61         dep->rdepends.push_back(this);
62 }
63
64 void Target::prepare()
65 {
66         if(prepared)
67                 return;
68
69         prepared=true;
70         for(TargetList::iterator i=depends.begin(); i!=depends.end(); ++i)
71                 (*i)->prepare();
72
73         check_rebuild();
74
75 }
76
77 Action *Target::build()
78 {
79         if(!buildable)
80         {
81                 rebuild=false;
82                 return 0;
83         }
84
85         if(FileTarget *ft=dynamic_cast<FileTarget *>(this))
86                 if(!builder.get_dry_run() && FS::exists(ft->get_path()))
87                         FS::unlink(ft->get_path());
88
89         Action *action=create_action();
90         if(action)
91         {
92                 action->signal_done.connect(sigc::mem_fun(this, &Target::build_done));
93
94                 building=true;
95         }
96
97         return action;
98 }
99
100 unsigned Target::count_rebuild()
101 {
102         if(counted)
103                 return 0;
104
105         counted=true;
106         unsigned count=rebuild;
107         for(TargetList::iterator i=depends.begin(); i!=depends.end(); ++i)
108                 count+=(*i)->count_rebuild();
109         return count;
110 }
111
112 void Target::touch()
113 {
114         mtime=Time::now();
115 }
116
117 void Target::mark_rebuild(const std::string &reason)
118 {
119         rebuild=true;
120         rebuild_reason=reason;
121 }
122
123 void Target::check_rebuild()
124 {
125         if(!buildable)
126                 return;
127
128         if(builder.get_build_all())
129                 mark_rebuild("Rebuilding everything");
130         else if(!mtime)
131                 mark_rebuild("Does not exist");
132         else
133         {
134                 for(TargetList::iterator i=depends.begin(); (i!=depends.end() && !rebuild); ++i)
135                 {
136                         if((*i)->get_mtime()>mtime)
137                                 mark_rebuild(FS::basename((*i)->get_name())+" has changed");
138                         else if((*i)->get_rebuild())
139                                 mark_rebuild(FS::basename((*i)->get_name())+" needs rebuilding");
140                 }
141         }
142
143         const SourcePackage *spkg=dynamic_cast<const SourcePackage *>(package);
144         if(!rebuild && spkg && spkg->get_config().get_mtime()>mtime)
145                 mark_rebuild("Package options changed");
146 }
147
148 void Target::build_done()
149 {
150         building=false;
151         rebuild=false;
152 }