Driver.h revision 205408
1//===--- Driver.h - Clang GCC Compatible Driver -----------------*- C++ -*-===//
2//
3//                     The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10#ifndef CLANG_DRIVER_DRIVER_H_
11#define CLANG_DRIVER_DRIVER_H_
12
13#include "clang/Basic/Diagnostic.h"
14
15#include "clang/Driver/Phases.h"
16#include "clang/Driver/Util.h"
17
18#include "llvm/ADT/StringRef.h"
19#include "llvm/ADT/Triple.h"
20#include "llvm/System/Path.h" // FIXME: Kill when CompilationInfo
21                              // lands.
22#include <list>
23#include <set>
24#include <string>
25
26namespace llvm {
27  class raw_ostream;
28}
29namespace clang {
30namespace driver {
31  class Action;
32  class ArgList;
33  class Compilation;
34  class HostInfo;
35  class InputArgList;
36  class InputInfo;
37  class JobAction;
38  class OptTable;
39  class PipedJob;
40  class ToolChain;
41
42/// Driver - Encapsulate logic for constructing compilation processes
43/// from a set of gcc-driver-like command line arguments.
44class Driver {
45  OptTable *Opts;
46
47  Diagnostic &Diags;
48
49public:
50  // Diag - Forwarding function for diagnostics.
51  DiagnosticBuilder Diag(unsigned DiagID) const {
52    return Diags.Report(DiagID);
53  }
54
55  // FIXME: Privatize once interface is stable.
56public:
57  /// The name the driver was invoked as.
58  std::string Name;
59
60  /// The path the driver executable was in, as invoked from the
61  /// command line.
62  std::string Dir;
63
64  /// The path to the compiler resource directory.
65  std::string ResourceDir;
66
67  /// Default host triple.
68  std::string DefaultHostTriple;
69
70  /// Default name for linked images (e.g., "a.out").
71  std::string DefaultImageName;
72
73  /// Driver title to use with help.
74  std::string DriverTitle;
75
76  /// Host information for the platform the driver is running as. This
77  /// will generally be the actual host platform, but not always.
78  const HostInfo *Host;
79
80  /// Information about the host which can be overriden by the user.
81  std::string HostBits, HostMachine, HostSystem, HostRelease;
82
83  /// Name to use when calling the generic gcc.
84  std::string CCCGenericGCCName;
85
86  /// The file to log CC_PRINT_OPTIONS output to, if enabled.
87  const char *CCPrintOptionsFilename;
88
89  /// Whether the driver should follow g++ like behavior.
90  unsigned CCCIsCXX : 1;
91
92  /// Echo commands while executing (in -v style).
93  unsigned CCCEcho : 1;
94
95  /// Only print tool bindings, don't build any jobs.
96  unsigned CCCPrintBindings : 1;
97
98  /// Set CC_PRINT_OPTIONS mode, which is like -v but logs the commands to
99  /// CCPrintOptionsFilename or to stderr.
100  unsigned CCPrintOptions : 1;
101
102private:
103  /// Whether to check that input files exist when constructing compilation
104  /// jobs.
105  unsigned CheckInputsExist : 1;
106
107  /// Use the clang compiler where possible.
108  unsigned CCCUseClang : 1;
109
110  /// Use clang for handling C++ and Objective-C++ inputs.
111  unsigned CCCUseClangCXX : 1;
112
113  /// Use clang as a preprocessor (clang's preprocessor will still be
114  /// used where an integrated CPP would).
115  unsigned CCCUseClangCPP : 1;
116
117public:
118  /// Use lazy precompiled headers for PCH support.
119  unsigned CCCUsePCH : 1;
120
121private:
122  /// Only use clang for the given architectures (only used when
123  /// non-empty).
124  std::set<llvm::Triple::ArchType> CCCClangArchs;
125
126  /// Certain options suppress the 'no input files' warning.
127  bool SuppressMissingInputWarning : 1;
128
129  std::list<std::string> TempFiles;
130  std::list<std::string> ResultFiles;
131
132public:
133  Driver(llvm::StringRef _Name, llvm::StringRef _Dir,
134         llvm::StringRef _DefaultHostTriple,
135         llvm::StringRef _DefaultImageName,
136         bool IsProduction, Diagnostic &_Diags);
137  ~Driver();
138
139  /// @name Accessors
140  /// @{
141
142  const OptTable &getOpts() const { return *Opts; }
143
144  const Diagnostic &getDiags() const { return Diags; }
145
146  bool getCheckInputsExist() const { return CheckInputsExist; }
147
148  void setCheckInputsExist(bool Value) { CheckInputsExist = Value; }
149
150  const std::string &getTitle() { return DriverTitle; }
151  void setTitle(std::string Value) { DriverTitle = Value; }
152
153  /// @}
154  /// @name Primary Functionality
155  /// @{
156
157  /// BuildCompilation - Construct a compilation object for a command
158  /// line argument vector.
159  ///
160  /// \return A compilation, or 0 if none was built for the given
161  /// argument vector. A null return value does not necessarily
162  /// indicate an error condition, the diagnostics should be queried
163  /// to determine if an error occurred.
164  Compilation *BuildCompilation(int argc, const char **argv);
165
166  /// @name Driver Steps
167  /// @{
168
169  /// ParseArgStrings - Parse the given list of strings into an
170  /// ArgList.
171  InputArgList *ParseArgStrings(const char **ArgBegin, const char **ArgEnd);
172
173  /// BuildActions - Construct the list of actions to perform for the
174  /// given arguments, which are only done for a single architecture.
175  ///
176  /// \param Args - The input arguments.
177  /// \param Actions - The list to store the resulting actions onto.
178  void BuildActions(const ArgList &Args, ActionList &Actions) const;
179
180  /// BuildUniversalActions - Construct the list of actions to perform
181  /// for the given arguments, which may require a universal build.
182  ///
183  /// \param Args - The input arguments.
184  /// \param Actions - The list to store the resulting actions onto.
185  void BuildUniversalActions(const ArgList &Args, ActionList &Actions) const;
186
187  /// BuildJobs - Bind actions to concrete tools and translate
188  /// arguments to form the list of jobs to run.
189  ///
190  /// \arg C - The compilation that is being built.
191  void BuildJobs(Compilation &C) const;
192
193  /// ExecuteCompilation - Execute the compilation according to the command line
194  /// arguments and return an appropriate exit code.
195  ///
196  /// This routine handles additional processing that must be done in addition
197  /// to just running the subprocesses, for example reporting errors, removing
198  /// temporary files, etc.
199  int ExecuteCompilation(const Compilation &C) const;
200
201  /// @}
202  /// @name Helper Methods
203  /// @{
204
205  /// PrintActions - Print the list of actions.
206  void PrintActions(const Compilation &C) const;
207
208  /// PrintHelp - Print the help text.
209  ///
210  /// \param ShowHidden - Show hidden options.
211  void PrintHelp(bool ShowHidden) const;
212
213  /// PrintOptions - Print the list of arguments.
214  void PrintOptions(const ArgList &Args) const;
215
216  /// PrintVersion - Print the driver version.
217  void PrintVersion(const Compilation &C, llvm::raw_ostream &OS) const;
218
219  /// GetFilePath - Lookup \arg Name in the list of file search paths.
220  ///
221  /// \arg TC - The tool chain for additional information on
222  /// directories to search.
223  //
224  // FIXME: This should be in CompilationInfo.
225  std::string GetFilePath(const char *Name, const ToolChain &TC) const;
226
227  /// GetProgramPath - Lookup \arg Name in the list of program search
228  /// paths.
229  ///
230  /// \arg TC - The provided tool chain for additional information on
231  /// directories to search.
232  ///
233  /// \arg WantFile - False when searching for an executable file, otherwise
234  /// true.  Defaults to false.
235  //
236  // FIXME: This should be in CompilationInfo.
237  std::string GetProgramPath(const char *Name, const ToolChain &TC,
238                              bool WantFile = false) const;
239
240  /// HandleImmediateArgs - Handle any arguments which should be
241  /// treated before building actions or binding tools.
242  ///
243  /// \return Whether any compilation should be built for this
244  /// invocation.
245  bool HandleImmediateArgs(const Compilation &C);
246
247  /// ConstructAction - Construct the appropriate action to do for
248  /// \arg Phase on the \arg Input, taking in to account arguments
249  /// like -fsyntax-only or --analyze.
250  Action *ConstructPhaseAction(const ArgList &Args, phases::ID Phase,
251                               Action *Input) const;
252
253
254  /// BuildJobsForAction - Construct the jobs to perform for the
255  /// action \arg A.
256  void BuildJobsForAction(Compilation &C,
257                          const Action *A,
258                          const ToolChain *TC,
259                          const char *BoundArch,
260                          bool CanAcceptPipe,
261                          bool AtTopLevel,
262                          const char *LinkingOutput,
263                          InputInfo &Result) const;
264
265  /// GetNamedOutputPath - Return the name to use for the output of
266  /// the action \arg JA. The result is appended to the compilation's
267  /// list of temporary or result files, as appropriate.
268  ///
269  /// \param C - The compilation.
270  /// \param JA - The action of interest.
271  /// \param BaseInput - The original input file that this action was
272  /// triggered by.
273  /// \param AtTopLevel - Whether this is a "top-level" action.
274  const char *GetNamedOutputPath(Compilation &C,
275                                 const JobAction &JA,
276                                 const char *BaseInput,
277                                 bool AtTopLevel) const;
278
279  /// GetTemporaryPath - Return the pathname of a temporary file to
280  /// use as part of compilation; the file will have the given suffix.
281  ///
282  /// GCC goes to extra lengths here to be a bit more robust.
283  std::string GetTemporaryPath(const char *Suffix) const;
284
285  /// GetHostInfo - Construct a new host info object for the given
286  /// host triple.
287  const HostInfo *GetHostInfo(const char *HostTriple) const;
288
289  /// ShouldUseClangCompilar - Should the clang compiler be used to
290  /// handle this action.
291  bool ShouldUseClangCompiler(const Compilation &C, const JobAction &JA,
292                              const llvm::Triple &ArchName) const;
293
294  /// @}
295
296  /// GetReleaseVersion - Parse (([0-9]+)(.([0-9]+)(.([0-9]+)?))?)? and
297  /// return the grouped values as integers. Numbers which are not
298  /// provided are set to 0.
299  ///
300  /// \return True if the entire string was parsed (9.2), or all
301  /// groups were parsed (10.3.5extrastuff). HadExtra is true if all
302  /// groups were parsed but extra characters remain at the end.
303  static bool GetReleaseVersion(const char *Str, unsigned &Major,
304                                unsigned &Minor, unsigned &Micro,
305                                bool &HadExtra);
306};
307
308} // end namespace driver
309} // end namespace clang
310
311#endif
312