]> git.tdb.fi Git - builder.git/blob - source/target.cpp
577438b3528dce736aa3c22077e0857a24f5a0b4
[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 "sourcepackage.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         if(state>PREPARING)
64                 dep.signal_bubble_rebuild.connect(sigc::mem_fun(this, &Target::check_rebuild));
65 }
66
67 void Target::prepare()
68 {
69         if(state>PREPARING)
70                 return;
71         if(state==PREPARING)
72         {
73                 builder.problem((package ? package->get_name() : string()), "Dependency cycle detected at "+name);
74                 return;
75         }
76
77         state = PREPARING;
78         find_depends();
79         if(tool)
80         {
81                 if(FileTarget *tool_exe = tool->get_executable())
82                         add_depend(*tool_exe);
83         }
84
85         for(Dependencies::iterator i=depends.begin(); i!=depends.end(); ++i)
86                 (*i)->prepare();
87
88         check_rebuild();
89         if(state==PREPARING)
90                 state = UPTODATE;
91
92         for(Dependencies::iterator i=depends.begin(); i!=depends.end(); ++i)
93                 (*i)->signal_bubble_rebuild.connect(sigc::mem_fun(this, &Target::check_rebuild));
94 }
95
96 Task *Target::build()
97 {
98         if(!tool)
99         {
100                 // This special case is needed for VirtualTargets
101                 state = UPTODATE;
102                 return 0;
103         }
104
105         // XXX Minor breach of OO here
106         if(FileTarget *ft = dynamic_cast<FileTarget *>(this))
107                 if(!builder.get_dry_run() && FS::exists(ft->get_path()))
108                         FS::unlink(ft->get_path());
109
110         Task *task = tool->run(*this);
111         task->signal_finished.connect(sigc::mem_fun(this, &Target::build_finished));
112         state = BUILDING;
113
114         return task;
115 }
116
117 void Target::mark_rebuild(const string &reason)
118 {
119         if(reason.empty())
120                 throw invalid_argument("No reason given for rebuilding "+name);
121
122         state = REBUILD;
123         rebuild_reason = reason;
124
125         builder.get_logger().log("rebuild", format("Rebuilding %s: %s", name, reason));
126
127         signal_bubble_rebuild.emit();
128 }
129
130 void Target::build_finished(bool /*success*/)
131 {
132         state = UPTODATE;
133 }