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