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