]> git.tdb.fi Git - builder.git/blob - source/tool.cpp
Add a sanity check for derived tool commands
[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
47         if(!command.empty())
48                 executable = builder.get_vfs().find_binary(command);
49         prepare(*this);
50         if(!command.empty() && !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 void Tool::prepare(Tool &tool) const
58 {
59         if(&tool!=this && tool.get_base_tool()!=this)
60                 throw invalid_argument("Tool::prepare");
61
62         if(&tool!=this && !command.empty() && tool.command.empty())
63                 throw logic_error("Derived tool has no command");
64
65         do_prepare(tool);
66 }
67
68 string Tool::create_build_signature(const BuildInfo &) const
69 {
70         if(executable)
71                 return format("%s=%s", tag, FS::basename(executable->get_path()));
72         else
73                 return string();
74 }
75
76
77 void operator>>(const LexicalConverter &conv, Tool::ProcessingUnit &unit)
78 {
79         const string &str = conv.get();
80         if(str=="FILE")
81                 unit = Tool::ONE_FILE;
82         else if(str=="DIRECTORY")
83                 unit = Tool::DIRECTORY;
84         else if(str=="COMPONENT")
85                 unit = Tool::COMPONENT;
86         else
87                 throw lexical_error(format("conversion of '%s' to ProcessingUnit", str));
88 }