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