1 //===--- Driver.h - Clang GCC Compatible Driver -----------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #ifndef CLANG_DRIVER_DRIVER_H_
11 #define CLANG_DRIVER_DRIVER_H_
13 #include "clang/Basic/Diagnostic.h"
15 #include "clang/Driver/Phases.h"
16 #include "clang/Driver/Types.h"
17 #include "clang/Driver/Util.h"
19 #include "llvm/ADT/StringRef.h"
20 #include "llvm/ADT/Triple.h"
21 #include "llvm/Support/Path.h" // FIXME: Kill when CompilationInfo
28 template<typename T> class ArrayRef;
45 /// Driver - Encapsulate logic for constructing compilation processes
46 /// from a set of gcc-driver-like command line arguments.
50 DiagnosticsEngine &Diags;
53 // Diag - Forwarding function for diagnostics.
54 DiagnosticBuilder Diag(unsigned DiagID) const {
55 return Diags.Report(DiagID);
58 // FIXME: Privatize once interface is stable.
60 /// The name the driver was invoked as.
63 /// The path the driver executable was in, as invoked from the
67 /// The original path to the clang executable.
68 std::string ClangExecutable;
70 /// The path to the installed clang directory, if any.
71 std::string InstalledDir;
73 /// The path to the compiler resource directory.
74 std::string ResourceDir;
76 /// A prefix directory used to emulated a limited subset of GCC's '-Bprefix'
78 /// FIXME: This type of customization should be removed in favor of the
79 /// universal driver when it is ready.
80 typedef SmallVector<std::string, 4> prefix_list;
81 prefix_list PrefixDirs;
83 /// sysroot, if present
86 /// If the standard library is used
89 /// Default host triple.
90 std::string DefaultHostTriple;
92 /// Default name for linked images (e.g., "a.out").
93 std::string DefaultImageName;
95 /// Driver title to use with help.
96 std::string DriverTitle;
98 /// Host information for the platform the driver is running as. This
99 /// will generally be the actual host platform, but not always.
100 const HostInfo *Host;
102 /// Information about the host which can be overridden by the user.
103 std::string HostBits, HostMachine, HostSystem, HostRelease;
105 /// The file to log CC_PRINT_OPTIONS output to, if enabled.
106 const char *CCPrintOptionsFilename;
108 /// The file to log CC_PRINT_HEADERS output to, if enabled.
109 const char *CCPrintHeadersFilename;
111 /// The file to log CC_LOG_DIAGNOSTICS output to, if enabled.
112 const char *CCLogDiagnosticsFilename;
114 /// A list of inputs and their types for the given arguments.
115 typedef SmallVector<std::pair<types::ID, const Arg*>, 16> InputList;
117 /// Whether the driver should follow g++ like behavior.
118 unsigned CCCIsCXX : 1;
120 /// Whether the driver is just the preprocessor
121 unsigned CCCIsCPP : 1;
123 /// Echo commands while executing (in -v style).
124 unsigned CCCEcho : 1;
126 /// Only print tool bindings, don't build any jobs.
127 unsigned CCCPrintBindings : 1;
129 /// Set CC_PRINT_OPTIONS mode, which is like -v but logs the commands to
130 /// CCPrintOptionsFilename or to stderr.
131 unsigned CCPrintOptions : 1;
133 /// Set CC_PRINT_HEADERS mode, which causes the frontend to log header include
134 /// information to CCPrintHeadersFilename or to stderr.
135 unsigned CCPrintHeaders : 1;
137 /// Set CC_LOG_DIAGNOSTICS mode, which causes the frontend to log diagnostics
138 /// to CCLogDiagnosticsFilename or to stderr, in a stable machine readable
140 unsigned CCLogDiagnostics : 1;
142 /// Whether the driver is generating diagnostics for debugging purposes.
143 unsigned CCGenDiagnostics : 1;
146 /// Name to use when invoking gcc/g++.
147 std::string CCCGenericGCCName;
149 /// Whether to check that input files exist when constructing compilation
151 unsigned CheckInputsExist : 1;
153 /// Use the clang compiler where possible.
154 unsigned CCCUseClang : 1;
156 /// Use clang for handling C++ and Objective-C++ inputs.
157 unsigned CCCUseClangCXX : 1;
159 /// Use clang as a preprocessor (clang's preprocessor will still be
160 /// used where an integrated CPP would).
161 unsigned CCCUseClangCPP : 1;
164 /// Use lazy precompiled headers for PCH support.
165 unsigned CCCUsePCH : 1;
168 /// Only use clang for the given architectures (only used when
170 std::set<llvm::Triple::ArchType> CCCClangArchs;
172 /// Certain options suppress the 'no input files' warning.
173 bool SuppressMissingInputWarning : 1;
175 std::list<std::string> TempFiles;
176 std::list<std::string> ResultFiles;
179 /// TranslateInputArgs - Create a new derived argument list from the input
180 /// arguments, after applying the standard argument translations.
181 DerivedArgList *TranslateInputArgs(const InputArgList &Args) const;
183 // getFinalPhase - Determine which compilation mode we are in and record
184 // which option we used to determine the final phase.
185 phases::ID getFinalPhase(const DerivedArgList &DAL, Arg **FinalPhaseArg = 0)
189 Driver(StringRef _ClangExecutable,
190 StringRef _DefaultHostTriple,
191 StringRef _DefaultImageName,
193 DiagnosticsEngine &_Diags);
199 /// Name to use when invoking gcc/g++.
200 const std::string &getCCCGenericGCCName() const { return CCCGenericGCCName; }
203 const OptTable &getOpts() const { return *Opts; }
205 const DiagnosticsEngine &getDiags() const { return Diags; }
207 bool getCheckInputsExist() const { return CheckInputsExist; }
209 void setCheckInputsExist(bool Value) { CheckInputsExist = Value; }
211 const std::string &getTitle() { return DriverTitle; }
212 void setTitle(std::string Value) { DriverTitle = Value; }
214 /// \brief Get the path to the main clang executable.
215 const char *getClangProgramPath() const {
216 return ClangExecutable.c_str();
219 /// \brief Get the path to where the clang executable was installed.
220 const char *getInstalledDir() const {
221 if (!InstalledDir.empty())
222 return InstalledDir.c_str();
225 void setInstalledDir(StringRef Value) {
226 InstalledDir = Value;
230 /// @name Primary Functionality
233 /// BuildCompilation - Construct a compilation object for a command
234 /// line argument vector.
236 /// \return A compilation, or 0 if none was built for the given
237 /// argument vector. A null return value does not necessarily
238 /// indicate an error condition, the diagnostics should be queried
239 /// to determine if an error occurred.
240 Compilation *BuildCompilation(ArrayRef<const char *> Args);
242 /// @name Driver Steps
245 /// ParseArgStrings - Parse the given list of strings into an
247 InputArgList *ParseArgStrings(ArrayRef<const char *> Args);
249 /// BuildInputs - Construct the list of inputs and their types from
250 /// the given arguments.
252 /// \param TC - The default host tool chain.
253 /// \param Args - The input arguments.
254 /// \param Inputs - The list to store the resulting compilation
256 void BuildInputs(const ToolChain &TC, const DerivedArgList &Args,
257 InputList &Inputs) const;
259 /// BuildActions - Construct the list of actions to perform for the
260 /// given arguments, which are only done for a single architecture.
262 /// \param TC - The default host tool chain.
263 /// \param Args - The input arguments.
264 /// \param Actions - The list to store the resulting actions onto.
265 void BuildActions(const ToolChain &TC, const DerivedArgList &Args,
266 const InputList &Inputs, ActionList &Actions) const;
268 /// BuildUniversalActions - Construct the list of actions to perform
269 /// for the given arguments, which may require a universal build.
271 /// \param TC - The default host tool chain.
272 /// \param Args - The input arguments.
273 /// \param Actions - The list to store the resulting actions onto.
274 void BuildUniversalActions(const ToolChain &TC, const DerivedArgList &Args,
275 const InputList &BAInputs,
276 ActionList &Actions) const;
278 /// BuildJobs - Bind actions to concrete tools and translate
279 /// arguments to form the list of jobs to run.
281 /// \arg C - The compilation that is being built.
282 void BuildJobs(Compilation &C) const;
284 /// ExecuteCompilation - Execute the compilation according to the command line
285 /// arguments and return an appropriate exit code.
287 /// This routine handles additional processing that must be done in addition
288 /// to just running the subprocesses, for example reporting errors, removing
289 /// temporary files, etc.
290 int ExecuteCompilation(const Compilation &C,
291 const Command *&FailingCommand) const;
293 /// generateCompilationDiagnostics - Generate diagnostics information
294 /// including preprocessed source file(s).
296 void generateCompilationDiagnostics(Compilation &C,
297 const Command *FailingCommand);
300 /// @name Helper Methods
303 /// PrintActions - Print the list of actions.
304 void PrintActions(const Compilation &C) const;
306 /// PrintHelp - Print the help text.
308 /// \param ShowHidden - Show hidden options.
309 void PrintHelp(bool ShowHidden) const;
311 /// PrintOptions - Print the list of arguments.
312 void PrintOptions(const ArgList &Args) const;
314 /// PrintVersion - Print the driver version.
315 void PrintVersion(const Compilation &C, raw_ostream &OS) const;
317 /// GetFilePath - Lookup \arg Name in the list of file search paths.
319 /// \arg TC - The tool chain for additional information on
320 /// directories to search.
322 // FIXME: This should be in CompilationInfo.
323 std::string GetFilePath(const char *Name, const ToolChain &TC) const;
325 /// GetProgramPath - Lookup \arg Name in the list of program search
328 /// \arg TC - The provided tool chain for additional information on
329 /// directories to search.
331 /// \arg WantFile - False when searching for an executable file, otherwise
332 /// true. Defaults to false.
334 // FIXME: This should be in CompilationInfo.
335 std::string GetProgramPath(const char *Name, const ToolChain &TC,
336 bool WantFile = false) const;
338 /// HandleImmediateArgs - Handle any arguments which should be
339 /// treated before building actions or binding tools.
341 /// \return Whether any compilation should be built for this
343 bool HandleImmediateArgs(const Compilation &C);
345 /// ConstructAction - Construct the appropriate action to do for
346 /// \arg Phase on the \arg Input, taking in to account arguments
347 /// like -fsyntax-only or --analyze.
348 Action *ConstructPhaseAction(const ArgList &Args, phases::ID Phase,
349 Action *Input) const;
352 /// BuildJobsForAction - Construct the jobs to perform for the
354 void BuildJobsForAction(Compilation &C,
357 const char *BoundArch,
359 const char *LinkingOutput,
360 InputInfo &Result) const;
362 /// GetNamedOutputPath - Return the name to use for the output of
363 /// the action \arg JA. The result is appended to the compilation's
364 /// list of temporary or result files, as appropriate.
366 /// \param C - The compilation.
367 /// \param JA - The action of interest.
368 /// \param BaseInput - The original input file that this action was
370 /// \param AtTopLevel - Whether this is a "top-level" action.
371 const char *GetNamedOutputPath(Compilation &C,
373 const char *BaseInput,
374 bool AtTopLevel) const;
376 /// GetTemporaryPath - Return the pathname of a temporary file to use
377 /// as part of compilation; the file will have the given prefix and suffix.
379 /// GCC goes to extra lengths here to be a bit more robust.
380 std::string GetTemporaryPath(StringRef Prefix, const char *Suffix) const;
382 /// GetHostInfo - Construct a new host info object for the given
384 const HostInfo *GetHostInfo(const char *HostTriple) const;
386 /// ShouldUseClangCompilar - Should the clang compiler be used to
387 /// handle this action.
388 bool ShouldUseClangCompiler(const Compilation &C, const JobAction &JA,
389 const llvm::Triple &ArchName) const;
391 bool IsUsingLTO(const ArgList &Args) const;
395 /// GetReleaseVersion - Parse (([0-9]+)(.([0-9]+)(.([0-9]+)?))?)? and
396 /// return the grouped values as integers. Numbers which are not
397 /// provided are set to 0.
399 /// \return True if the entire string was parsed (9.2), or all
400 /// groups were parsed (10.3.5extrastuff). HadExtra is true if all
401 /// groups were parsed but extra characters remain at the end.
402 static bool GetReleaseVersion(const char *Str, unsigned &Major,
403 unsigned &Minor, unsigned &Micro,
407 } // end namespace driver
408 } // end namespace clang