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