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