]> git.tdb.fi Git - builder.git/blob - source/target.cpp
510dfbdf708fa83cd57ee0426a3b0b1c01fc5a56
[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         if(tool)
79         {
80                 if(FileTarget *tool_exe = tool->get_executable())
81                         add_depend(tool_exe);
82         }
83
84         for(Dependencies::iterator i=depends.begin(); i!=depends.end(); ++i)
85                 (*i)->prepare();
86
87         check_rebuild();
88         if(state==PREPARING)
89                 state = UPTODATE;
90 }
91
92 Task *Target::build()
93 {
94         if(!tool)
95         {
96                 state = UPTODATE;
97                 return 0;
98         }
99
100         // XXX Minor breach of OO here
101         if(FileTarget *ft = dynamic_cast<FileTarget *>(this))
102                 if(!builder.get_dry_run() && FS::exists(ft->get_path()))
103                         FS::unlink(ft->get_path());
104
105         Task *task = tool->run(*this);
106         task->signal_finished.connect(sigc::mem_fun(this, &Target::build_finished));
107         state = BUILDING;
108
109         return task;
110 }
111
112 void Target::mark_rebuild(const std::string &reason)
113 {
114         if(reason.empty())
115                 throw invalid_argument("No reason given for rebuilding "+name);
116
117         state = REBUILD;
118         rebuild_reason = reason;
119         signal_bubble_rebuild.emit();
120
121         builder.get_logger().log("rebuild", format("Rebuilding %s: %s", name, reason));
122 }
123
124 void Target::build_finished(bool /*success*/)
125 {
126         state = UPTODATE;
127 }