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