Driver.h revision 10ffa9a4887d9376e3eb3598e40523d1b58773c9
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 clang {
25namespace driver {
26  class Action;
27  class ArgList;
28  class Compilation;
29  class HostInfo;
30  class InputInfo;
31  class JobAction;
32  class OptTable;
33  class PipedJob;
34  class ToolChain;
35
36/// Driver - Encapsulate logic for constructing compilation processes
37/// from a set of gcc-driver-like command line arguments.
38class Driver {
39  OptTable *Opts;
40
41  Diagnostic &Diags;
42
43  // Diag - Forwarding function for diagnostics.
44  DiagnosticBuilder Diag(unsigned DiagID) const {
45    return Diags.Report(FullSourceLoc(), DiagID);
46  }
47
48  // FIXME: Privatize once interface is stable.
49public:
50  /// The name the driver was invoked as.
51  std::string Name;
52
53  /// The path the driver executable was in, as invoked from the
54  /// command line.
55  std::string Dir;
56
57  /// Default host triple.
58  std::string DefaultHostTriple;
59
60  /// Default name for linked images (e.g., "a.out").
61  std::string DefaultImageName;
62
63  /// Host information for the platform the driver is running as. This
64  /// will generally be the actual host platform, but not always.
65  const HostInfo *Host;
66
67  /// Information about the host which can be overriden by the user.
68  std::string HostBits, HostMachine, HostSystem, HostRelease;
69
70  /// Whether the driver should follow g++ like behavior.
71  bool CCCIsCXX : 1;
72
73  /// Echo commands while executing (in -v style).
74  bool CCCEcho : 1;
75
76  /// Only print tool bindings, don't build any jobs.
77  bool CCCPrintBindings : 1;
78
79  /// Don't use clang for any tasks.
80  bool CCCNoClang : 1;
81
82  /// Don't use clang for handling C++ and Objective-C++ inputs.
83  bool CCCNoClangCXX : 1;
84
85  /// Don't use clang as a preprocessor (clang's preprocessor will
86  /// still be used where an integrated CPP would).
87  bool CCCNoClangCPP : 1;
88
89  /// Only use clang for the given architectures. Only used when
90  /// non-empty.
91  std::set<std::string> CCCClangArchs;
92
93  /// Certain options suppress the 'no input files' warning.
94  bool SuppressMissingInputWarning : 1;
95
96  std::list<std::string> TempFiles;
97  std::list<std::string> ResultFiles;
98
99public:
100  Driver(const char *_Name, const char *_Dir,
101         const char *_DefaultHostTriple,
102         const char *_DefaultImageName,
103         Diagnostic &_Diags);
104  ~Driver();
105
106  /// @name Accessors
107  /// @{
108
109  const OptTable &getOpts() const { return *Opts; }
110
111  /// @}
112  /// @name Primary Functionality
113  /// @{
114
115  /// BuildCompilation - Construct a compilation object for a command
116  /// line argument vector.
117  ///
118  /// \return A compilation, or 0 if none was built for the given
119  /// argument vector. A null return value does not necessarily
120  /// indicate an error condition, the diagnostics should be queried
121  /// to determine if an error occurred.
122  Compilation *BuildCompilation(int argc, const char **argv);
123
124  /// @name Driver Steps
125  /// @{
126
127  /// ParseArgStrings - Parse the given list of strings into an
128  /// ArgList.
129  ArgList *ParseArgStrings(const char **ArgBegin, const char **ArgEnd);
130
131  /// BuildActions - Construct the list of actions to perform for the
132  /// given arguments, which are only done for a single architecture.
133  ///
134  /// \param Args - The input arguments.
135  /// \param Actions - The list to store the resulting actions onto.
136  void BuildActions(const ArgList &Args, ActionList &Actions) const;
137
138  /// BuildUniversalActions - Construct the list of actions to perform
139  /// for the given arguments, which may require a universal build.
140  ///
141  /// \param Args - The input arguments.
142  /// \param Actions - The list to store the resulting actions onto.
143  void BuildUniversalActions(const ArgList &Args, ActionList &Actions) const;
144
145  /// BuildJobs - Bind actions to concrete tools and translate
146  /// arguments to form the list of jobs to run.
147  ///
148  /// \arg C - The compilation that is being built.
149  void BuildJobs(Compilation &C) const;
150
151  /// @}
152  /// @name Helper Methods
153  /// @{
154
155  /// PrintOptions - Print the list of arguments.
156  void PrintOptions(const ArgList &Args) const;
157
158  /// PrintVersion - Print the driver version.
159  void PrintVersion() const;
160
161  /// PrintActions - Print the list of actions.
162  void PrintActions(const Compilation &C) const;
163
164  /// GetFilePath - Lookup \arg Name in the list of file search paths.
165  ///
166  /// \arg TC - The tool chain for additional information on
167  /// directories to search.
168  // FIXME: This should be in CompilationInfo.
169  llvm::sys::Path GetFilePath(const char *Name, const ToolChain &TC) const;
170
171  /// GetProgramPath - Lookup \arg Name in the list of program search
172  /// paths.
173  ///
174  /// \arg TC - The provided tool chain for additional information on
175  /// directories to search.
176  // FIXME: This should be in CompilationInfo.
177  llvm::sys::Path GetProgramPath(const char *Name, const ToolChain &TC) const;
178
179  /// HandleImmediateArgs - Handle any arguments which should be
180  /// treated before building actions or binding tools.
181  ///
182  /// \return Whether any compilation should be built for this
183  /// invocation.
184  bool HandleImmediateArgs(const Compilation &C);
185
186  /// ConstructAction - Construct the appropriate action to do for
187  /// \arg Phase on the \arg Input, taking in to account arguments
188  /// like -fsyntax-only or --analyze.
189  Action *ConstructPhaseAction(const ArgList &Args, phases::ID Phase,
190                               Action *Input) const;
191
192
193  /// BuildJobsForAction - Construct the jobs to perform for the
194  /// action \arg A.
195  void BuildJobsForAction(Compilation &C,
196                          const Action *A,
197                          const ToolChain *TC,
198                          bool CanAcceptPipe,
199                          bool AtTopLevel,
200                          const char *LinkingOutput,
201                          InputInfo &Result) const;
202
203  /// GetNamedOutputPath - Return the name to use for the output of
204  /// the action \arg JA. The result is appended to the compilation's
205  /// list of temporary or result files, as appropriate.
206  ///
207  /// \param C - The compilation.
208  /// \param JA - The action of interest.
209  /// \param BaseInput - The original input file that this action was
210  /// triggered by.
211  /// \param AtTopLevel - Whether this is a "top-level" action.
212  const char *GetNamedOutputPath(Compilation &C,
213                                 const JobAction &JA,
214                                 const char *BaseInput,
215                                 bool AtTopLevel) const;
216
217  /// GetHostInfo - Construct a new host info object for the given
218  /// host triple.
219  const HostInfo *GetHostInfo(const char *HostTriple) const;
220
221  /// @}
222};
223
224} // end namespace driver
225} // end namespace clang
226
227#endif
228