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