-#include <algorithm>
+#include <msp/core/algorithm.h>
+#include <msp/fs/utils.h>
+#include <msp/strings/format.h>
+#include "architecture.h"
+#include "builder.h"
+#include "filetarget.h"
#include "tool.h"
using namespace std;
+using namespace Msp;
-Tool::Tool(Builder &b, const string &t):
- builder(b),
- architecture(0),
- tag(t),
- executable(0)
-{ }
+void Tool::set_command(const string &cmd, bool cross)
+{
+ if(cmd.empty())
+ throw invalid_argument("Tool::set_command");
+
+ if(cross && architecture->is_cross() && !FS::Path(cmd).is_absolute())
+ command = format("%s-%s", architecture->get_cross_prefix(), cmd);
+ else
+ command = cmd;
+}
-Tool::Tool(Builder &b, const Architecture &a, const string &t):
- builder(b),
- architecture(&a),
- tag(t),
- executable(0)
-{ }
+void Tool::set_run(function<Task *(const Target &)> f)
+{
+ run_func = move(f);
+}
bool Tool::accepts_suffix(const string &suffix, bool aux) const
{
- if(find(input_suffixes.begin(), input_suffixes.end(), suffix)!=input_suffixes.end())
- return true;
- else if(aux)
- return find(aux_suffixes.begin(), aux_suffixes.end(), suffix)!=aux_suffixes.end();
- else
- return false;
+ return (any_equals(input_suffixes, suffix) || (aux && any_equals(aux_suffixes, suffix)));
}
-Target *Tool::create_target(Target &source, const string &arg) const
+Target *Tool::create_target(Target &source, const string &arg)
{
- list<Target *> sources;
+ vector<Target *> sources;
sources.push_back(&source);
return create_target(sources, arg);
}
+void Tool::prepare()
+{
+ if(prepared)
+ return;
+
+ prepared = true;
+
+ if(!command.empty())
+ executable = builder.get_vfs().find_binary(command);
+ prepare(*this);
+ if(!command.empty() && !executable)
+ {
+ builder.get_logger().log("problems", "Can't find executable %s for %s", command, tag);
+ problems.push_back(format("Can't find executable %s", command));
+ }
+}
+
+void Tool::prepare(Tool &tool) const
+{
+ if(&tool!=this && tool.get_base_tool()!=this)
+ throw invalid_argument("Tool::prepare");
+
+ if(&tool!=this && !command.empty() && tool.command.empty())
+ throw logic_error("Derived tool has no command");
+
+ do_prepare(tool);
+}
+
+string Tool::create_build_signature(const BuildInfo &) const
+{
+ if(executable)
+ return format("%s=%s", tag, FS::basename(executable->get_path()));
+ else
+ return string();
+}
+
-SubTool::SubTool(Tool &p):
- Tool(p),
- parent(p)
-{ }
+void operator>>(const LexicalConverter &conv, Tool::ProcessingUnit &unit)
+{
+ const string &str = conv.get();
+ if(str=="FILE")
+ unit = Tool::ONE_FILE;
+ else if(str=="DIRECTORY")
+ unit = Tool::DIRECTORY;
+ else if(str=="COMPONENT")
+ unit = Tool::COMPONENT;
+ else
+ throw lexical_error(format("conversion of '%s' to ProcessingUnit", str));
+}