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