Driver.h revision 202879
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  /// Host information for the platform the driver is running as. This
74  /// will generally be the actual host platform, but not always.
75  const HostInfo *Host;
76
77  /// Information about the host which can be overriden by the user.
78  std::string HostBits, HostMachine, HostSystem, HostRelease;
79
80  /// Whether the driver should follow g++ like behavior.
81  bool CCCIsCXX : 1;
82
83  /// Echo commands while executing (in -v style).
84  bool CCCEcho : 1;
85
86  /// Only print tool bindings, don't build any jobs.
87  bool CCCPrintBindings : 1;
88
89  /// Name to use when calling the generic gcc.
90  std::string CCCGenericGCCName;
91
92private:
93  /// Use the clang compiler where possible.
94  bool CCCUseClang : 1;
95
96  /// Use clang for handling C++ and Objective-C++ inputs.
97  bool CCCUseClangCXX : 1;
98
99  /// Use clang as a preprocessor (clang's preprocessor will still be
100  /// used where an integrated CPP would).
101  bool CCCUseClangCPP : 1;
102
103public:
104  /// Use lazy precompiled headers for PCH support.
105  bool CCCUsePCH;
106
107private:
108  /// Only use clang for the given architectures (only used when
109  /// non-empty).
110  std::set<llvm::Triple::ArchType> CCCClangArchs;
111
112  /// Certain options suppress the 'no input files' warning.
113  bool SuppressMissingInputWarning : 1;
114
115  std::list<std::string> TempFiles;
116  std::list<std::string> ResultFiles;
117
118public:
119  Driver(llvm::StringRef _Name, llvm::StringRef _Dir,
120         llvm::StringRef _DefaultHostTriple,
121         llvm::StringRef _DefaultImageName,
122         bool IsProduction, Diagnostic &_Diags);
123  ~Driver();
124
125  /// @name Accessors
126  /// @{
127
128  const OptTable &getOpts() const { return *Opts; }
129
130  const Diagnostic &getDiags() const { return Diags; }
131
132  /// @}
133  /// @name Primary Functionality
134  /// @{
135
136  /// BuildCompilation - Construct a compilation object for a command
137  /// line argument vector.
138  ///
139  /// \return A compilation, or 0 if none was built for the given
140  /// argument vector. A null return value does not necessarily
141  /// indicate an error condition, the diagnostics should be queried
142  /// to determine if an error occurred.
143  Compilation *BuildCompilation(int argc, const char **argv);
144
145  /// @name Driver Steps
146  /// @{
147
148  /// ParseArgStrings - Parse the given list of strings into an
149  /// ArgList.
150  InputArgList *ParseArgStrings(const char **ArgBegin, const char **ArgEnd);
151
152  /// BuildActions - Construct the list of actions to perform for the
153  /// given arguments, which are only done for a single architecture.
154  ///
155  /// \param Args - The input arguments.
156  /// \param Actions - The list to store the resulting actions onto.
157  void BuildActions(const ArgList &Args, ActionList &Actions) const;
158
159  /// BuildUniversalActions - Construct the list of actions to perform
160  /// for the given arguments, which may require a universal build.
161  ///
162  /// \param Args - The input arguments.
163  /// \param Actions - The list to store the resulting actions onto.
164  void BuildUniversalActions(const ArgList &Args, ActionList &Actions) const;
165
166  /// BuildJobs - Bind actions to concrete tools and translate
167  /// arguments to form the list of jobs to run.
168  ///
169  /// \arg C - The compilation that is being built.
170  void BuildJobs(Compilation &C) const;
171
172  /// ExecuteCompilation - Execute the compilation according to the command line
173  /// arguments and return an appropriate exit code.
174  ///
175  /// This routine handles additional processing that must be done in addition
176  /// to just running the subprocesses, for example reporting errors, removing
177  /// temporary files, etc.
178  int ExecuteCompilation(const Compilation &C) const;
179
180  /// @}
181  /// @name Helper Methods
182  /// @{
183
184  /// PrintActions - Print the list of actions.
185  void PrintActions(const Compilation &C) const;
186
187  /// PrintHelp - Print the help text.
188  ///
189  /// \param ShowHidden - Show hidden options.
190  void PrintHelp(bool ShowHidden) const;
191
192  /// PrintOptions - Print the list of arguments.
193  void PrintOptions(const ArgList &Args) const;
194
195  /// PrintVersion - Print the driver version.
196  void PrintVersion(const Compilation &C, llvm::raw_ostream &OS) const;
197
198  /// GetFilePath - Lookup \arg Name in the list of file search paths.
199  ///
200  /// \arg TC - The tool chain for additional information on
201  /// directories to search.
202  //
203  // FIXME: This should be in CompilationInfo.
204  std::string GetFilePath(const char *Name, const ToolChain &TC) const;
205
206  /// GetProgramPath - Lookup \arg Name in the list of program search
207  /// paths.
208  ///
209  /// \arg TC - The provided tool chain for additional information on
210  /// directories to search.
211  ///
212  /// \arg WantFile - False when searching for an executable file, otherwise
213  /// true.  Defaults to false.
214  //
215  // FIXME: This should be in CompilationInfo.
216  std::string GetProgramPath(const char *Name, const ToolChain &TC,
217                              bool WantFile = false) const;
218
219  /// HandleImmediateArgs - Handle any arguments which should be
220  /// treated before building actions or binding tools.
221  ///
222  /// \return Whether any compilation should be built for this
223  /// invocation.
224  bool HandleImmediateArgs(const Compilation &C);
225
226  /// ConstructAction - Construct the appropriate action to do for
227  /// \arg Phase on the \arg Input, taking in to account arguments
228  /// like -fsyntax-only or --analyze.
229  Action *ConstructPhaseAction(const ArgList &Args, phases::ID Phase,
230                               Action *Input) const;
231
232
233  /// BuildJobsForAction - Construct the jobs to perform for the
234  /// action \arg A.
235  void BuildJobsForAction(Compilation &C,
236                          const Action *A,
237                          const ToolChain *TC,
238                          const char *BoundArch,
239                          bool CanAcceptPipe,
240                          bool AtTopLevel,
241                          const char *LinkingOutput,
242                          InputInfo &Result) const;
243
244  /// GetNamedOutputPath - Return the name to use for the output of
245  /// the action \arg JA. The result is appended to the compilation's
246  /// list of temporary or result files, as appropriate.
247  ///
248  /// \param C - The compilation.
249  /// \param JA - The action of interest.
250  /// \param BaseInput - The original input file that this action was
251  /// triggered by.
252  /// \param AtTopLevel - Whether this is a "top-level" action.
253  const char *GetNamedOutputPath(Compilation &C,
254                                 const JobAction &JA,
255                                 const char *BaseInput,
256                                 bool AtTopLevel) const;
257
258  /// GetTemporaryPath - Return the pathname of a temporary file to
259  /// use as part of compilation; the file will have the given suffix.
260  ///
261  /// GCC goes to extra lengths here to be a bit more robust.
262  std::string GetTemporaryPath(const char *Suffix) const;
263
264  /// GetHostInfo - Construct a new host info object for the given
265  /// host triple.
266  const HostInfo *GetHostInfo(const char *HostTriple) const;
267
268  /// ShouldUseClangCompilar - Should the clang compiler be used to
269  /// handle this action.
270  bool ShouldUseClangCompiler(const Compilation &C, const JobAction &JA,
271                              const llvm::Triple &ArchName) const;
272
273  /// @}
274
275  /// GetReleaseVersion - Parse (([0-9]+)(.([0-9]+)(.([0-9]+)?))?)? and
276  /// return the grouped values as integers. Numbers which are not
277  /// provided are set to 0.
278  ///
279  /// \return True if the entire string was parsed (9.2), or all
280  /// groups were parsed (10.3.5extrastuff). HadExtra is true if all
281  /// groups were parsed but extra characters remain at the end.
282  static bool GetReleaseVersion(const char *Str, unsigned &Major,
283                                unsigned &Minor, unsigned &Micro,
284                                bool &HadExtra);
285};
286
287} // end namespace driver
288} // end namespace clang
289
290#endif
291