]> git.tdb.fi Git - builder.git/blob - source/tool.h
5ea63356a4028b0d6afac2dfe0b6be02fce1c65c
[builder.git] / source / tool.h
1 #ifndef TOOL_H_
2 #define TOOL_H_
3
4 #include <functional>
5 #include <string>
6 #include <vector>
7 #include <msp/fs/path.h>
8 #include "buildinfo.h"
9 #include "internaltask.h"
10 #include "virtualfilesystem.h"
11
12 class Architecture;
13 class Builder;
14 class BuildInfo;
15 class Component;
16 class FileTarget;
17 class Target;
18
19 /**
20 Base class for tools.  Tools are used to turn targets into other targets.
21 Examples include compilers and linkers.
22 */
23 class Tool
24 {
25 public:
26         enum ProcessingUnit
27         {
28                 ONE_FILE,
29                 DIRECTORY,
30                 COMPONENT
31         };
32
33 protected:
34         Builder &builder;
35         const Architecture *architecture = 0;
36         std::string tag;
37         std::string command;
38         FileTarget *executable = 0;
39         std::vector<std::string> input_suffixes;
40         std::vector<std::string> aux_suffixes;
41         ProcessingUnit processing_unit = ONE_FILE;
42         VirtualFileSystem::SearchPath system_path;
43         BuildInfo build_info;
44         std::function<Task *(const Target &)> run_func;
45         bool prepared = false;
46         std::vector<std::string> problems;
47
48         Tool(Builder &b, const std::string &t): Tool(b, 0, t) { }
49         Tool(Builder &b, const Architecture *a, const std::string &t): builder(b), architecture(a), tag(t) { }
50 public:
51         virtual ~Tool() { }
52
53         Builder &get_builder() const { return builder; }
54
55         const std::string &get_tag() const { return tag; }
56
57         /** Returns the architecture this tool builds for.  May return null if the
58         tool is architecture-agnostic. */
59         const Architecture *get_architecture() const { return architecture; }
60
61 protected:
62         void set_run(std::function<Task *(const Target &)>);
63
64         template<typename T>
65         void set_run(Task *(*)(const T &));
66
67         template<typename T>
68         void set_run_internal(bool (*)(const T &));
69
70 public:
71         /** Overrides the command used by the tool.  The new command should accept
72         the same command line arguments.  Only works on tools that use an external
73         command.  If cross is true and the architecture is not native, a cross
74         prefix is added to the command.  May have no effect after prepare() has been
75         called. */
76         void set_command(const std::string &cmd, bool cross = false);
77
78         /** Returns a target for the tool's own executable.  If the tool does not
79         use an external program, returns null.  The tool must be prepared first. */
80         FileTarget *get_executable() const { return executable; }
81
82         /// Returns a list of suffixes that can be processed with this tool.
83         const std::vector<std::string> &get_input_suffixes() const { return input_suffixes; }
84
85         /** Returns a list of suffixes that are associated with this tool, but can't
86         be processed directly.  For example C and C++ headers. */
87         const std::vector<std::string> &get_auxiliary_suffixes() const { return aux_suffixes; }
88
89         /** Indicates whether the tool can accept a suffix.  If aux is true,
90         auxiliary suffixes are considered as well */
91         bool accepts_suffix(const std::string &, bool aux = false) const;
92
93         /** Returns the grouping unit this tool prefers to process. */
94         ProcessingUnit get_processing_unit() const { return processing_unit; }
95
96         /// Returns the systemwide search path for source files.
97         const VirtualFileSystem::SearchPath &get_system_path() const { return system_path; }
98
99         /** Returns tool-specific build info.  This can be used by other tools down
100         the chain. */
101         const BuildInfo &get_build_info() const { return build_info; }
102
103         /// Creates a source file appropriate for this tool.
104         virtual Target *create_source(const Component &, const Msp::FS::Path &) const { return 0; }
105
106         /** Creates a package-less source file appropriate for this tool.  This is
107         called during dependency discovery when no package has created a target for
108         the file. */
109         virtual Target *create_source(const Msp::FS::Path &) const { return 0; }
110
111         /// Convenience function to create a target from a single source.
112         Target *create_target(Target &, const std::string & = std::string());
113
114         /** Creates a target from sources.  The exact types of accepted sources
115         depends on the tool.  The optional second argument can be used to select an
116         alternative target type for tools that can create multiple kinds of targets. */ 
117         virtual Target *create_target(const std::vector<Target *> &, const std::string & = std::string()) = 0;
118
119         /** Creates an install target for a target created by this tool.  Can return
120         null if the tool does not want to handle installing in a special way. */
121         virtual Target *create_install(Target &) const { return 0; }
122
123         virtual std::string create_build_signature(const BuildInfo &) const;
124
125         void prepare();
126
127 protected:
128         virtual void do_prepare() { }
129
130 public:
131         const std::vector<std::string> &get_problems() const { return problems; }
132
133         /** Invokes the tool to build a target.  This should not be called directly;
134         use Target::build() instead. */
135         Task *run(const Target &t) const { return run_func(t); }
136 };
137
138
139 template<typename T>
140 void Tool::set_run(Task *(*f)(const T &))
141 {
142         set_run([f](const Target &t){ return f(dynamic_cast<const T &>(t)); });
143 }
144
145 template<typename T>
146 void Tool::set_run_internal(bool (*f)(const T &))
147 {
148         set_run([f](const Target &t){
149                 const T &ct = dynamic_cast<const T &>(t);
150                 return new InternalTask([f, &ct]{ return f(ct); });
151         });
152 }
153
154
155 void operator>>(const Msp::LexicalConverter &, Tool::ProcessingUnit &);
156
157 #endif