]> git.tdb.fi Git - builder.git/blob - source/target.cpp
Do dependency discovery in a single pass
[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 }
56
57 void Target::prepare()
58 {
59         if(state>PREPARING)
60                 return;
61         if(state==PREPARING)
62         {
63                 builder.problem((package ? package->get_name() : string()), "Dependency cycle detected at "+name);
64                 return;
65         }
66
67         state = PREPARING;
68         find_depends();
69
70         for(Dependencies::iterator i=depends.begin(); i!=depends.end(); ++i)
71                 (*i)->prepare();
72
73         check_rebuild();
74         if(state==PREPARING)
75                 state = UPTODATE;
76 }
77
78 Task *Target::build()
79 {
80         if(!tool)
81         {
82                 state = UPTODATE;
83                 return 0;
84         }
85
86         // XXX Minor breach of OO here
87         if(FileTarget *ft = dynamic_cast<FileTarget *>(this))
88                 if(!builder.get_dry_run() && FS::exists(ft->get_path()))
89                         FS::unlink(ft->get_path());
90
91         Task *task = tool->run(*this);
92         task->signal_finished.connect(sigc::mem_fun(this, &Target::build_finished));
93         state = BUILDING;
94
95         return task;
96 }
97
98 void Target::mark_rebuild(const std::string &reason)
99 {
100         state = REBUILD;
101         rebuild_reason = reason;
102 }
103
104 void Target::build_finished(bool /*success*/)
105 {
106         state = UPTODATE;
107 }