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