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