7 #include <msp/fs/path.h>
9 #include "internaltask.h"
10 #include "virtualfilesystem.h"
22 VirtualFileSystem::SearchPath system_path;
24 Msp::Variant extra_data;
25 std::vector<std::string> problems;
29 Base class for tools. Tools are used to turn targets into other targets.
30 Examples include compilers and linkers.
32 class Tool: protected ToolData
44 const Architecture *architecture = 0;
47 FileTarget *executable = 0;
48 std::vector<std::string> input_suffixes;
49 std::vector<std::string> aux_suffixes;
50 ProcessingUnit processing_unit = ONE_FILE;
51 std::function<Task *(const Target &)> run_func;
52 bool prepared = false;
54 Tool(Builder &b, const std::string &t): Tool(b, 0, t) { }
55 Tool(Builder &b, const Architecture *a, const std::string &t): builder(b), architecture(a), tag(t) { }
60 Builder &get_builder() const { return builder; }
62 const std::string &get_tag() const { return tag; }
64 /** Returns the architecture this tool builds for. May return null if the
65 tool is architecture-agnostic. */
66 const Architecture *get_architecture() const { return architecture; }
68 virtual const Tool *get_base_tool() const { return this; }
71 void set_run(std::function<Task *(const Target &)>);
74 void set_run(Task *(*)(const T &));
77 void set_run_internal(bool (*)(const T &));
80 /** Overrides the command used by the tool. The new command should accept
81 the same command line arguments. Only works on tools that use an external
82 command. If cross is true and the architecture is not native, a cross
83 prefix is added to the command. May have no effect after prepare() has been
85 void set_command(const std::string &cmd, bool cross = false);
87 /** Returns a target for the tool's own executable. If the tool does not
88 use an external program, returns null. The tool must be prepared first. */
89 FileTarget *get_executable() const { return executable; }
91 /// Returns a list of suffixes that can be processed with this tool.
92 const std::vector<std::string> &get_input_suffixes() const { return input_suffixes; }
94 /** Returns a list of suffixes that are associated with this tool, but can't
95 be processed directly. For example C and C++ headers. */
96 const std::vector<std::string> &get_auxiliary_suffixes() const { return aux_suffixes; }
98 /** Indicates whether the tool can accept a suffix. If aux is true,
99 auxiliary suffixes are considered as well */
100 bool accepts_suffix(const std::string &, bool aux = false) const;
102 /** Returns the grouping unit this tool prefers to process. */
103 ProcessingUnit get_processing_unit() const { return processing_unit; }
105 /// Returns the systemwide search path for source files.
106 const VirtualFileSystem::SearchPath &get_system_path() const { return system_path; }
108 /** Returns tool-specific build info. This can be used by other tools down
110 const BuildInfo &get_build_info() const { return build_info; }
112 const Msp::Variant &get_extra_data() const { return extra_data; }
114 /// Creates a source file appropriate for this tool.
115 virtual Target *create_source(const Component &, const Msp::FS::Path &) const { return 0; }
117 /** Creates a package-less source file appropriate for this tool. This is
118 called during dependency discovery when no package has created a target for
120 virtual Target *create_source(const Msp::FS::Path &) const { return 0; }
122 /// Convenience function to create a target from a single source.
123 Target *create_target(Target &, const std::string & = std::string());
125 /** Creates a target from sources. The exact types of accepted sources
126 depends on the tool. The optional second argument can be used to select an
127 alternative target type for tools that can create multiple kinds of targets. */
128 virtual Target *create_target(const std::vector<Target *> &, const std::string & = std::string()) = 0;
130 /** Creates an install target for a target created by this tool. Can return
131 null if the tool does not want to handle installing in a special way. */
132 virtual Target *create_install(Target &) const { return 0; }
134 virtual std::string create_build_signature(const BuildInfo &) const;
137 void prepare(Tool &) const;
140 virtual void do_prepare(ToolData &) const { }
143 const std::vector<std::string> &get_problems() const { return problems; }
145 /** Invokes the tool to build a target. This should not be called directly;
146 use Target::build() instead. */
147 Task *run(const Target &t) const { return run_func(t); }
152 void Tool::set_run(Task *(*f)(const T &))
154 set_run([f](const Target &t){ return f(dynamic_cast<const T &>(t)); });
158 void Tool::set_run_internal(bool (*f)(const T &))
160 set_run([f](const Target &t){
161 const T &ct = dynamic_cast<const T &>(t);
162 return new InternalTask([f, &ct]{ return f(ct); });
167 void operator>>(const Msp::LexicalConverter &, Tool::ProcessingUnit &);