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/StringMap.h"
20 #include "llvm/ADT/StringRef.h"
21 #include "llvm/ADT/Triple.h"
22 #include "llvm/Support/Path.h" // FIXME: Kill when CompilationInfo
29 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 target triple.
90 std::string DefaultTargetTriple;
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 /// Information about the host which can be overridden by the user.
99 std::string HostBits, HostMachine, HostSystem, HostRelease;
101 /// The file to log CC_PRINT_OPTIONS output to, if enabled.
102 const char *CCPrintOptionsFilename;
104 /// The file to log CC_PRINT_HEADERS output to, if enabled.
105 const char *CCPrintHeadersFilename;
107 /// The file to log CC_LOG_DIAGNOSTICS output to, if enabled.
108 const char *CCLogDiagnosticsFilename;
110 /// A list of inputs and their types for the given arguments.
111 typedef SmallVector<std::pair<types::ID, const Arg*>, 16> InputList;
113 /// Whether the driver should follow g++ like behavior.
114 unsigned CCCIsCXX : 1;
116 /// Whether the driver is just the preprocessor.
117 unsigned CCCIsCPP : 1;
119 /// Echo commands while executing (in -v style).
120 unsigned CCCEcho : 1;
122 /// Only print tool bindings, don't build any jobs.
123 unsigned CCCPrintBindings : 1;
125 /// Set CC_PRINT_OPTIONS mode, which is like -v but logs the commands to
126 /// CCPrintOptionsFilename or to stderr.
127 unsigned CCPrintOptions : 1;
129 /// Set CC_PRINT_HEADERS mode, which causes the frontend to log header include
130 /// information to CCPrintHeadersFilename or to stderr.
131 unsigned CCPrintHeaders : 1;
133 /// Set CC_LOG_DIAGNOSTICS mode, which causes the frontend to log diagnostics
134 /// to CCLogDiagnosticsFilename or to stderr, in a stable machine readable
136 unsigned CCLogDiagnostics : 1;
138 /// Whether the driver is generating diagnostics for debugging purposes.
139 unsigned CCGenDiagnostics : 1;
142 /// Name to use when invoking gcc/g++.
143 std::string CCCGenericGCCName;
145 /// Whether to check that input files exist when constructing compilation
147 unsigned CheckInputsExist : 1;
149 /// Use the clang compiler where possible.
150 unsigned CCCUseClang : 1;
152 /// Use clang for handling C++ and Objective-C++ inputs.
153 unsigned CCCUseClangCXX : 1;
155 /// Use clang as a preprocessor (clang's preprocessor will still be
156 /// used where an integrated CPP would).
157 unsigned CCCUseClangCPP : 1;
160 /// Use lazy precompiled headers for PCH support.
161 unsigned CCCUsePCH : 1;
164 /// Only use clang for the given architectures (only used when
166 std::set<llvm::Triple::ArchType> CCCClangArchs;
168 /// Certain options suppress the 'no input files' warning.
169 bool SuppressMissingInputWarning : 1;
171 std::list<std::string> TempFiles;
172 std::list<std::string> ResultFiles;
174 /// \brief Cache of all the ToolChains in use by the driver.
176 /// This maps from the string representation of a triple to a ToolChain
177 /// created targetting that triple. The driver owns all the ToolChain objects
178 /// stored in it, and will clean them up when torn down.
179 mutable llvm::StringMap<ToolChain *> ToolChains;
182 /// TranslateInputArgs - Create a new derived argument list from the input
183 /// arguments, after applying the standard argument translations.
184 DerivedArgList *TranslateInputArgs(const InputArgList &Args) const;
186 // getFinalPhase - Determine which compilation mode we are in and record
187 // which option we used to determine the final phase.
188 phases::ID getFinalPhase(const DerivedArgList &DAL, Arg **FinalPhaseArg = 0)
192 Driver(StringRef _ClangExecutable,
193 StringRef _DefaultTargetTriple,
194 StringRef _DefaultImageName,
196 DiagnosticsEngine &_Diags);
202 /// Name to use when invoking gcc/g++.
203 const std::string &getCCCGenericGCCName() const { return CCCGenericGCCName; }
206 const OptTable &getOpts() const { return *Opts; }
208 const DiagnosticsEngine &getDiags() const { return Diags; }
210 bool getCheckInputsExist() const { return CheckInputsExist; }
212 void setCheckInputsExist(bool Value) { CheckInputsExist = Value; }
214 const std::string &getTitle() { return DriverTitle; }
215 void setTitle(std::string Value) { DriverTitle = Value; }
217 /// \brief Get the path to the main clang executable.
218 const char *getClangProgramPath() const {
219 return ClangExecutable.c_str();
222 /// \brief Get the path to where the clang executable was installed.
223 const char *getInstalledDir() const {
224 if (!InstalledDir.empty())
225 return InstalledDir.c_str();
228 void setInstalledDir(StringRef Value) {
229 InstalledDir = Value;
233 /// @name Primary Functionality
236 /// BuildCompilation - Construct a compilation object for a command
237 /// line argument vector.
239 /// \return A compilation, or 0 if none was built for the given
240 /// argument vector. A null return value does not necessarily
241 /// indicate an error condition, the diagnostics should be queried
242 /// to determine if an error occurred.
243 Compilation *BuildCompilation(ArrayRef<const char *> Args);
245 /// @name Driver Steps
248 /// ParseArgStrings - Parse the given list of strings into an
250 InputArgList *ParseArgStrings(ArrayRef<const char *> Args);
252 /// BuildInputs - Construct the list of inputs and their types from
253 /// the given arguments.
255 /// \param TC - The default host tool chain.
256 /// \param Args - The input arguments.
257 /// \param Inputs - The list to store the resulting compilation
259 void BuildInputs(const ToolChain &TC, const DerivedArgList &Args,
260 InputList &Inputs) const;
262 /// BuildActions - Construct the list of actions to perform for the
263 /// given arguments, which are only done for a single architecture.
265 /// \param TC - The default host tool chain.
266 /// \param Args - The input arguments.
267 /// \param Actions - The list to store the resulting actions onto.
268 void BuildActions(const ToolChain &TC, const DerivedArgList &Args,
269 const InputList &Inputs, ActionList &Actions) const;
271 /// BuildUniversalActions - Construct the list of actions to perform
272 /// for the given arguments, which may require a universal build.
274 /// \param TC - The default host tool chain.
275 /// \param Args - The input arguments.
276 /// \param Actions - The list to store the resulting actions onto.
277 void BuildUniversalActions(const ToolChain &TC, const DerivedArgList &Args,
278 const InputList &BAInputs,
279 ActionList &Actions) const;
281 /// BuildJobs - Bind actions to concrete tools and translate
282 /// arguments to form the list of jobs to run.
284 /// \arg C - The compilation that is being built.
285 void BuildJobs(Compilation &C) const;
287 /// ExecuteCompilation - Execute the compilation according to the command line
288 /// arguments and return an appropriate exit code.
290 /// This routine handles additional processing that must be done in addition
291 /// to just running the subprocesses, for example reporting errors, removing
292 /// temporary files, etc.
293 int ExecuteCompilation(const Compilation &C,
294 const Command *&FailingCommand) const;
296 /// generateCompilationDiagnostics - Generate diagnostics information
297 /// including preprocessed source file(s).
299 void generateCompilationDiagnostics(Compilation &C,
300 const Command *FailingCommand);
303 /// @name Helper Methods
306 /// PrintActions - Print the list of actions.
307 void PrintActions(const Compilation &C) const;
309 /// PrintHelp - Print the help text.
311 /// \param ShowHidden - Show hidden options.
312 void PrintHelp(bool ShowHidden) const;
314 /// PrintOptions - Print the list of arguments.
315 void PrintOptions(const ArgList &Args) const;
317 /// PrintVersion - Print the driver version.
318 void PrintVersion(const Compilation &C, raw_ostream &OS) const;
320 /// GetFilePath - Lookup \arg Name in the list of file search paths.
322 /// \arg TC - The tool chain for additional information on
323 /// directories to search.
325 // FIXME: This should be in CompilationInfo.
326 std::string GetFilePath(const char *Name, const ToolChain &TC) const;
328 /// GetProgramPath - Lookup \arg Name in the list of program search
331 /// \arg TC - The provided tool chain for additional information on
332 /// directories to search.
334 /// \arg WantFile - False when searching for an executable file, otherwise
335 /// true. Defaults to false.
337 // FIXME: This should be in CompilationInfo.
338 std::string GetProgramPath(const char *Name, const ToolChain &TC,
339 bool WantFile = false) const;
341 /// HandleImmediateArgs - Handle any arguments which should be
342 /// treated before building actions or binding tools.
344 /// \return Whether any compilation should be built for this
346 bool HandleImmediateArgs(const Compilation &C);
348 /// ConstructAction - Construct the appropriate action to do for
349 /// \arg Phase on the \arg Input, taking in to account arguments
350 /// like -fsyntax-only or --analyze.
351 Action *ConstructPhaseAction(const ArgList &Args, phases::ID Phase,
352 Action *Input) const;
355 /// BuildJobsForAction - Construct the jobs to perform for the
357 void BuildJobsForAction(Compilation &C,
360 const char *BoundArch,
362 const char *LinkingOutput,
363 InputInfo &Result) const;
365 /// GetNamedOutputPath - Return the name to use for the output of
366 /// the action \arg JA. The result is appended to the compilation's
367 /// list of temporary or result files, as appropriate.
369 /// \param C - The compilation.
370 /// \param JA - The action of interest.
371 /// \param BaseInput - The original input file that this action was
373 /// \param AtTopLevel - Whether this is a "top-level" action.
374 const char *GetNamedOutputPath(Compilation &C,
376 const char *BaseInput,
377 bool AtTopLevel) const;
379 /// GetTemporaryPath - Return the pathname of a temporary file to use
380 /// as part of compilation; the file will have the given prefix and suffix.
382 /// GCC goes to extra lengths here to be a bit more robust.
383 std::string GetTemporaryPath(StringRef Prefix, const char *Suffix) const;
385 /// ShouldUseClangCompilar - Should the clang compiler be used to
386 /// handle this action.
387 bool ShouldUseClangCompiler(const Compilation &C, const JobAction &JA,
388 const llvm::Triple &ArchName) const;
390 bool IsUsingLTO(const ArgList &Args) const;
393 /// \brief Retrieves a ToolChain for a particular target triple.
395 /// Will cache ToolChains for the life of the driver object, and create them
397 const ToolChain &getToolChain(const ArgList &Args,
398 StringRef DarwinArchName = "") const;
403 /// GetReleaseVersion - Parse (([0-9]+)(.([0-9]+)(.([0-9]+)?))?)? and
404 /// return the grouped values as integers. Numbers which are not
405 /// provided are set to 0.
407 /// \return True if the entire string was parsed (9.2), or all
408 /// groups were parsed (10.3.5extrastuff). HadExtra is true if all
409 /// groups were parsed but extra characters remain at the end.
410 static bool GetReleaseVersion(const char *Str, unsigned &Major,
411 unsigned &Minor, unsigned &Micro,
415 } // end namespace driver
416 } // end namespace clang