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