]> git.tdb.fi Git - builder.git/blob - source/tool.cpp
f3a29905b1651f12cc2c4dc92ef95f55e38b5a0d
[builder.git] / source / tool.cpp
1 #include <algorithm>
2 #include <msp/strings/format.h>
3 #include "architecture.h"
4 #include "builder.h"
5 #include "tool.h"
6
7 using namespace std;
8 using namespace Msp;
9
10 Tool::Tool(Builder &b, const string &t):
11         builder(b),
12         architecture(0),
13         tag(t),
14         executable(0),
15         processing_unit(ONE_FILE),
16         prepared(false)
17 { }
18
19 Tool::Tool(Builder &b, const Architecture &a, const string &t):
20         builder(b),
21         architecture(&a),
22         tag(t),
23         executable(0),
24         processing_unit(ONE_FILE),
25         prepared(false)
26 { }
27
28 void Tool::set_command(const string &cmd, bool cross)
29 {
30         if(cmd.empty())
31                 throw invalid_argument("Tool::set_command");
32
33         if(cross && architecture->is_cross() && !FS::Path(cmd).is_absolute())
34                 command = format("%s-%s", architecture->get_cross_prefix(), cmd);
35         else
36                 command = cmd;
37 }
38
39 bool Tool::accepts_suffix(const string &suffix, bool aux) const
40 {
41         if(find(input_suffixes.begin(), input_suffixes.end(), suffix)!=input_suffixes.end())
42                 return true;
43         else if(aux)
44                 return find(aux_suffixes.begin(), aux_suffixes.end(), suffix)!=aux_suffixes.end();
45         else
46                 return false;
47 }
48
49 Target *Tool::create_target(Target &source, const string &arg)
50 {
51         list<Target *> sources;
52         sources.push_back(&source);
53         return create_target(sources, arg);
54 }
55
56 void Tool::prepare()
57 {
58         if(prepared)
59                 return;
60
61         prepared = true;
62         do_prepare();
63         if(!executable && !command.empty())
64         {
65                 executable = builder.get_vfs().find_binary(command);
66                 if(!executable)
67                         problems.push_back(format("Can't find executable %s", command));
68         }
69 }
70
71
72 SubTool::SubTool(Tool &p):
73         Tool(p),
74         parent(p)
75 { }
76
77 Target *SubTool::create_source(const Component &c, const FS::Path &p) const
78 {
79         return parent.create_source(c, p);
80 }
81
82 Target *SubTool::create_source(const FS::Path &p) const
83 {
84         return parent.create_source(p);
85 }
86
87 Target *SubTool::create_target(const list<Target *> &s, const string &a)
88 {
89         return parent.create_target(s, a);
90 }
91
92 Target *SubTool::create_install(Target &t) const
93 {
94         return parent.create_install(t);
95 }
96
97 string SubTool::create_build_signature(const BuildInfo &bi) const
98 {
99         return parent.create_build_signature(bi);
100 }
101
102
103 void operator>>(const LexicalConverter &conv, Tool::ProcessingUnit &unit)
104 {
105         const string &str = conv.get();
106         if(str=="FILE")
107                 unit = Tool::ONE_FILE;
108         else if(str=="DIRECTORY")
109                 unit = Tool::DIRECTORY;
110         else if(str=="COMPONENT")
111                 unit = Tool::COMPONENT;
112         else
113                 throw lexical_error(format("conversion of '%s' to ProcessingUnit", str));
114 }