]> git.tdb.fi Git - builder.git/blob - source/toolchain.cpp
Use priorities to determine the default toolchain
[builder.git] / source / toolchain.cpp
1 #include <msp/core/algorithm.h>
2 #include <msp/core/maputils.h>
3 #include "tool.h"
4 #include "toolchain.h"
5
6 using namespace std;
7 using namespace Msp;
8
9 Toolchain::~Toolchain()
10 {
11         for(const auto &kvp: tools)
12                 delete kvp.second;
13         for(Toolchain *c: chains)
14                 delete c;
15 }
16
17 void Toolchain::add_tool(Tool *tool)
18 {
19         insert_unique(tools, tool->get_tag(), tool);
20 }
21
22 void Toolchain::add_toolchain(Toolchain *chain)
23 {
24         auto i = upper_bound(chains, chain->get_priority(), [](int p, Toolchain *tc){ return p>tc->get_priority(); });
25         chains.insert(i, chain);
26 }
27
28 bool Toolchain::has_tool(const string &tag) const
29 {
30         if(tools.count(tag))
31                 return true;
32         return any_of(chains.begin(), chains.end(), [&tag](Toolchain *tc){ return tc->has_tool(tag); });
33 }
34
35 Tool &Toolchain::get_tool(const string &tag) const
36 {
37         if(!tools.count(tag))
38         {
39                 for(const Toolchain *c: chains)
40                         if(c->has_tool(tag))
41                                 return c->get_tool(tag);
42         }
43
44         return *get_item(tools, tag);
45 }
46
47 Tool *Toolchain::get_tool_for_suffix(const string &suffix, bool aux) const
48 {
49         for(const auto &kvp: tools)
50                 if(kvp.second->accepts_suffix(suffix, aux))
51                         return kvp.second;
52
53         for(const Toolchain *c: chains)
54                 if(Tool *tool = c->get_tool_for_suffix(suffix, aux))
55                         return tool;
56
57         return 0;
58 }