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