]> git.tdb.fi Git - builder.git/blob - source/target.cpp
Add a signal to propagate rebuild state after preparation
[builder.git] / source / target.cpp
1 #include <msp/fs/stat.h>
2 #include <msp/fs/utils.h>
3 #include "builder.h"
4 #include "filetarget.h"
5 #include "package.h"
6 #include "target.h"
7 #include "task.h"
8 #include "tool.h"
9
10 using namespace std;
11 using namespace Msp;
12
13 Target::Target(Builder &b, const string &n):
14         builder(b),
15         package(0),
16         component(0),
17         name(n),
18         tool(0),
19         state(INIT)
20 {
21         builder.add_target(this);
22 }
23
24 Target *Target::get_buildable_target()
25 {
26         if(!needs_rebuild())
27                 return 0;
28
29         bool self_ok = state!=BUILDING;
30         for(Dependencies::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)->needs_rebuild())
36                         self_ok = false;
37         }
38
39         if(self_ok)
40                 return this;
41
42         return 0;
43 }
44
45 void Target::set_tool(const Tool &t)
46 {
47         tool = &t;
48 }
49
50 void Target::add_depend(Target *dep)
51 {
52         if(dep==this)
53                 throw invalid_argument("Target::add_depend");
54         depends.push_back(dep);
55         dep->signal_bubble_rebuild.connect(sigc::mem_fun(this, &Target::check_rebuild));
56 }
57
58 void Target::prepare()
59 {
60         if(state>PREPARING)
61                 return;
62         if(state==PREPARING)
63         {
64                 builder.problem((package ? package->get_name() : string()), "Dependency cycle detected at "+name);
65                 return;
66         }
67
68         state = PREPARING;
69         find_depends();
70
71         for(Dependencies::iterator i=depends.begin(); i!=depends.end(); ++i)
72                 (*i)->prepare();
73
74         check_rebuild();
75         if(state==PREPARING)
76                 state = UPTODATE;
77 }
78
79 Task *Target::build()
80 {
81         if(!tool)
82         {
83                 state = UPTODATE;
84                 return 0;
85         }
86
87         // XXX Minor breach of OO here
88         if(FileTarget *ft = dynamic_cast<FileTarget *>(this))
89                 if(!builder.get_dry_run() && FS::exists(ft->get_path()))
90                         FS::unlink(ft->get_path());
91
92         Task *task = tool->run(*this);
93         task->signal_finished.connect(sigc::mem_fun(this, &Target::build_finished));
94         state = BUILDING;
95
96         return task;
97 }
98
99 void Target::mark_rebuild(const std::string &reason)
100 {
101         if(reason.empty())
102                 throw invalid_argument("No reason given for rebuilding "+name);
103
104         state = REBUILD;
105         rebuild_reason = reason;
106         signal_bubble_rebuild.emit();
107 }
108
109 void Target::build_finished(bool /*success*/)
110 {
111         state = UPTODATE;
112 }