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