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