]> git.tdb.fi Git - builder.git/blob - source/target.cpp
3ee6bc5efe1528fc5573ed0ea8e8a9fc23107c93
[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         preparing(false),
30         prepared(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         if(preparing)
69         {
70                 builder.problem((package ? package->get_name() : string()), "Dependency cycle detected at "+name);
71                 return;
72         }
73
74         preparing = true;
75         for(TargetList::iterator i=depends.begin(); i!=depends.end(); ++i)
76                 (*i)->prepare();
77
78         check_rebuild();
79         preparing = false;
80         prepared = true;
81 }
82
83 Action *Target::build()
84 {
85         if(!buildable)
86         {
87                 rebuild = false;
88                 return 0;
89         }
90
91         if(FileTarget *ft = dynamic_cast<FileTarget *>(this))
92                 if(!builder.get_dry_run() && FS::exists(ft->get_path()))
93                         FS::unlink(ft->get_path());
94
95         Action *action = create_action();
96         if(action)
97         {
98                 action->signal_done.connect(sigc::mem_fun(this, &Target::build_done));
99
100                 building = true;
101         }
102
103         return action;
104 }
105
106 void Target::touch()
107 {
108         mtime = Time::now();
109 }
110
111 void Target::mark_rebuild(const std::string &reason)
112 {
113         rebuild = true;
114         rebuild_reason = reason;
115 }
116
117 void Target::check_rebuild()
118 {
119         if(!buildable)
120                 return;
121
122         if(builder.get_build_all())
123                 mark_rebuild("Rebuilding everything");
124         else if(!mtime)
125                 mark_rebuild("Does not exist");
126         else
127         {
128                 for(TargetList::iterator i=depends.begin(); (i!=depends.end() && !rebuild); ++i)
129                 {
130                         if((*i)->get_mtime()>mtime)
131                                 mark_rebuild(FS::basename((*i)->get_name())+" has changed");
132                         else if((*i)->get_rebuild())
133                                 mark_rebuild(FS::basename((*i)->get_name())+" needs rebuilding");
134                 }
135         }
136
137         const SourcePackage *spkg = dynamic_cast<const SourcePackage *>(package);
138         if(!rebuild && spkg && spkg->get_config().get_mtime()>mtime)
139                 mark_rebuild("Package options changed");
140 }
141
142 void Target::build_done()
143 {
144         building = false;
145         rebuild = false;
146 }