]> git.tdb.fi Git - builder.git/blob - source/buildgraph.cpp
Save caches before starting the build
[builder.git] / source / buildgraph.cpp
1 #include "builder.h"
2 #include "buildgraph.h"
3 #include "component.h"
4 #include "sourcepackage.h"
5 #include "tool.h"
6 #include "virtualtarget.h"
7
8 using namespace std;
9
10 BuildGraph::BuildGraph(Builder &b):
11         builder(b),
12         goals(new VirtualTarget(builder, "goals"))
13 {
14         Target *world = new VirtualTarget(builder, "world");
15         world->add_dependency(*new VirtualTarget(builder, "default"));
16         world->add_dependency(*new VirtualTarget(builder, "install"));
17         world->add_dependency(*new VirtualTarget(builder, "archives"));
18 }
19
20 BuildGraph::~BuildGraph()
21 {
22         for(TargetMap::iterator i=targets.begin(); i!=targets.end(); ++i)
23                 delete i->second;
24 }
25
26 Target *BuildGraph::get_target(const string &n) const
27 {
28         TargetMap::const_iterator i = targets.find(n);
29         if(i!=targets.end())
30                 return i->second;
31         return 0;
32 }
33
34 void BuildGraph::add_target(Target *t)
35 {
36         targets.insert(TargetMap::value_type(t->get_name(), t));
37 }
38
39 void BuildGraph::add_primary_target(Target &t)
40 {
41         get_target("world")->add_dependency(t);
42
43         Package *main_pkg = &builder.get_package_manager().get_main_package();
44         if(t.get_package()==main_pkg && t.get_component() && t.get_component()->is_default())
45                 get_target("default")->add_dependency(t);
46 }
47
48 void BuildGraph::add_installed_target(Target &t)
49 {
50         Target *inst_tgt = 0;
51         if(const Tool *tool = t.get_tool())
52                 inst_tgt = tool->create_install(t);
53         if(!inst_tgt)
54                 inst_tgt = builder.get_toolchain().get_tool("CP").create_target(t);
55         get_target("install")->add_dependency(*inst_tgt);
56 }
57
58 void BuildGraph::add_goal(Target &t)
59 {
60         goals->add_dependency(t);
61 }
62
63 void BuildGraph::prepare()
64 {
65         if(goals->get_dependencies().empty())
66                 add_goal(*get_target("default"));
67         goals->prepare();
68 }
69
70 void BuildGraph::force_full_rebuild()
71 {
72         for(TargetMap::iterator i=targets.begin(); i!=targets.end(); ++i)
73                 if(i->second->is_buildable() && !i->second->needs_rebuild())
74                         i->second->force_rebuild();
75 }
76
77 unsigned BuildGraph::count_rebuild_targets() const
78 {
79         unsigned count = 0;
80         for(map<string, Target *>::const_iterator i=targets.begin(); i!=targets.end(); ++i)
81                 if(i->second->is_buildable() && i->second->needs_rebuild())
82                         ++count;
83         return count;
84 }
85
86 Target *BuildGraph::get_buildable_target() const
87 {
88         return goals->get_buildable_target();
89 }