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