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