]> git.tdb.fi Git - builder.git/blob - source/tool.cpp
Rework Tool::prepare to be able to work on other objects than self
[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(Tool *tool)
41 {
42         if(!tool)
43                 tool = this;
44         else if(tool->get_base_tool()!=this)
45                 throw invalid_argument("Tool::prepare");
46
47         if(tool->prepared)
48                 return;
49
50         tool->prepared = true;
51         if(!tool->command.empty())
52                 tool->executable = builder.get_vfs().find_binary(tool->command);
53         do_prepare(*tool);
54         if(!command.empty() && !executable)
55         {
56                 builder.get_logger().log("problems", "Can't find executable %s for %s", tool->command, tool->tag);
57                 tool->problems.push_back(format("Can't find executable %s", tool->command));
58         }
59 }
60
61 string Tool::create_build_signature(const BuildInfo &) const
62 {
63         if(executable)
64                 return format("%s=%s", tag, FS::basename(executable->get_path()));
65         else
66                 return string();
67 }
68
69
70 void operator>>(const LexicalConverter &conv, Tool::ProcessingUnit &unit)
71 {
72         const string &str = conv.get();
73         if(str=="FILE")
74                 unit = Tool::ONE_FILE;
75         else if(str=="DIRECTORY")
76                 unit = Tool::DIRECTORY;
77         else if(str=="COMPONENT")
78                 unit = Tool::COMPONENT;
79         else
80                 throw lexical_error(format("conversion of '%s' to ProcessingUnit", str));
81 }