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/Util.h"
18 #include "llvm/ADT/StringRef.h"
19 #include "llvm/ADT/Triple.h"
20 #include "llvm/System/Path.h" // FIXME: Kill when CompilationInfo
43 /// Driver - Encapsulate logic for constructing compilation processes
44 /// from a set of gcc-driver-like command line arguments.
51 // Diag - Forwarding function for diagnostics.
52 DiagnosticBuilder Diag(unsigned DiagID) const {
53 return Diags.Report(DiagID);
56 // FIXME: Privatize once interface is stable.
58 /// The name the driver was invoked as.
61 /// The path the driver executable was in, as invoked from the
65 /// The original path to the clang executable.
66 std::string ClangExecutable;
68 /// The path to the compiler resource directory.
69 std::string ResourceDir;
71 /// A prefix directory used to emulated a limited subset of GCC's '-Bprefix'
73 /// FIXME: This type of customization should be removed in favor of the
74 /// universal driver when it is ready.
75 std::string PrefixDir;
77 /// Default host triple.
78 std::string DefaultHostTriple;
80 /// Default name for linked images (e.g., "a.out").
81 std::string DefaultImageName;
83 /// Driver title to use with help.
84 std::string DriverTitle;
86 /// Host information for the platform the driver is running as. This
87 /// will generally be the actual host platform, but not always.
90 /// Information about the host which can be overriden by the user.
91 std::string HostBits, HostMachine, HostSystem, HostRelease;
93 /// Name to use when calling the generic gcc.
94 std::string CCCGenericGCCName;
96 /// The file to log CC_PRINT_OPTIONS output to, if enabled.
97 const char *CCPrintOptionsFilename;
99 /// Whether the driver should follow g++ like behavior.
100 unsigned CCCIsCXX : 1;
102 /// Echo commands while executing (in -v style).
103 unsigned CCCEcho : 1;
105 /// Only print tool bindings, don't build any jobs.
106 unsigned CCCPrintBindings : 1;
108 /// Set CC_PRINT_OPTIONS mode, which is like -v but logs the commands to
109 /// CCPrintOptionsFilename or to stderr.
110 unsigned CCPrintOptions : 1;
113 /// Whether to check that input files exist when constructing compilation
115 unsigned CheckInputsExist : 1;
117 /// Use the clang compiler where possible.
118 unsigned CCCUseClang : 1;
120 /// Use clang for handling C++ and Objective-C++ inputs.
121 unsigned CCCUseClangCXX : 1;
123 /// Use clang as a preprocessor (clang's preprocessor will still be
124 /// used where an integrated CPP would).
125 unsigned CCCUseClangCPP : 1;
128 /// Use lazy precompiled headers for PCH support.
129 unsigned CCCUsePCH : 1;
132 /// Only use clang for the given architectures (only used when
134 std::set<llvm::Triple::ArchType> CCCClangArchs;
136 /// Certain options suppress the 'no input files' warning.
137 bool SuppressMissingInputWarning : 1;
139 std::list<std::string> TempFiles;
140 std::list<std::string> ResultFiles;
143 /// TranslateInputArgs - Create a new derived argument list from the input
144 /// arguments, after applying the standard argument translations.
145 DerivedArgList *TranslateInputArgs(const InputArgList &Args) const;
148 Driver(llvm::StringRef _Name, llvm::StringRef _Dir,
149 llvm::StringRef _DefaultHostTriple,
150 llvm::StringRef _DefaultImageName,
151 bool IsProduction, bool CXXIsProduction,
158 const OptTable &getOpts() const { return *Opts; }
160 const Diagnostic &getDiags() const { return Diags; }
162 bool getCheckInputsExist() const { return CheckInputsExist; }
164 void setCheckInputsExist(bool Value) { CheckInputsExist = Value; }
166 const std::string &getTitle() { return DriverTitle; }
167 void setTitle(std::string Value) { DriverTitle = Value; }
169 /// \brief Get the path to the main clang executable.
170 std::string getClangProgramPath() const {
171 return ClangExecutable;
175 /// @name Primary Functionality
178 /// BuildCompilation - Construct a compilation object for a command
179 /// line argument vector.
181 /// \return A compilation, or 0 if none was built for the given
182 /// argument vector. A null return value does not necessarily
183 /// indicate an error condition, the diagnostics should be queried
184 /// to determine if an error occurred.
185 Compilation *BuildCompilation(int argc, const char **argv);
187 /// @name Driver Steps
190 /// ParseArgStrings - Parse the given list of strings into an
192 InputArgList *ParseArgStrings(const char **ArgBegin, const char **ArgEnd);
194 /// BuildActions - Construct the list of actions to perform for the
195 /// given arguments, which are only done for a single architecture.
197 /// \param Args - The input arguments.
198 /// \param Actions - The list to store the resulting actions onto.
199 void BuildActions(const ArgList &Args, ActionList &Actions) const;
201 /// BuildUniversalActions - Construct the list of actions to perform
202 /// for the given arguments, which may require a universal build.
204 /// \param Args - The input arguments.
205 /// \param Actions - The list to store the resulting actions onto.
206 void BuildUniversalActions(const ArgList &Args, ActionList &Actions) const;
208 /// BuildJobs - Bind actions to concrete tools and translate
209 /// arguments to form the list of jobs to run.
211 /// \arg C - The compilation that is being built.
212 void BuildJobs(Compilation &C) const;
214 /// ExecuteCompilation - Execute the compilation according to the command line
215 /// arguments and return an appropriate exit code.
217 /// This routine handles additional processing that must be done in addition
218 /// to just running the subprocesses, for example reporting errors, removing
219 /// temporary files, etc.
220 int ExecuteCompilation(const Compilation &C) const;
223 /// @name Helper Methods
226 /// PrintActions - Print the list of actions.
227 void PrintActions(const Compilation &C) const;
229 /// PrintHelp - Print the help text.
231 /// \param ShowHidden - Show hidden options.
232 void PrintHelp(bool ShowHidden) const;
234 /// PrintOptions - Print the list of arguments.
235 void PrintOptions(const ArgList &Args) const;
237 /// PrintVersion - Print the driver version.
238 void PrintVersion(const Compilation &C, llvm::raw_ostream &OS) const;
240 /// GetFilePath - Lookup \arg Name in the list of file search paths.
242 /// \arg TC - The tool chain for additional information on
243 /// directories to search.
245 // FIXME: This should be in CompilationInfo.
246 std::string GetFilePath(const char *Name, const ToolChain &TC) const;
248 /// GetProgramPath - Lookup \arg Name in the list of program search
251 /// \arg TC - The provided tool chain for additional information on
252 /// directories to search.
254 /// \arg WantFile - False when searching for an executable file, otherwise
255 /// true. Defaults to false.
257 // FIXME: This should be in CompilationInfo.
258 std::string GetProgramPath(const char *Name, const ToolChain &TC,
259 bool WantFile = false) const;
261 /// HandleImmediateArgs - Handle any arguments which should be
262 /// treated before building actions or binding tools.
264 /// \return Whether any compilation should be built for this
266 bool HandleImmediateArgs(const Compilation &C);
268 /// ConstructAction - Construct the appropriate action to do for
269 /// \arg Phase on the \arg Input, taking in to account arguments
270 /// like -fsyntax-only or --analyze.
271 Action *ConstructPhaseAction(const ArgList &Args, phases::ID Phase,
272 Action *Input) const;
275 /// BuildJobsForAction - Construct the jobs to perform for the
277 void BuildJobsForAction(Compilation &C,
280 const char *BoundArch,
283 const char *LinkingOutput,
284 InputInfo &Result) const;
286 /// GetNamedOutputPath - Return the name to use for the output of
287 /// the action \arg JA. The result is appended to the compilation's
288 /// list of temporary or result files, as appropriate.
290 /// \param C - The compilation.
291 /// \param JA - The action of interest.
292 /// \param BaseInput - The original input file that this action was
294 /// \param AtTopLevel - Whether this is a "top-level" action.
295 const char *GetNamedOutputPath(Compilation &C,
297 const char *BaseInput,
298 bool AtTopLevel) const;
300 /// GetTemporaryPath - Return the pathname of a temporary file to
301 /// use as part of compilation; the file will have the given suffix.
303 /// GCC goes to extra lengths here to be a bit more robust.
304 std::string GetTemporaryPath(const char *Suffix) const;
306 /// GetHostInfo - Construct a new host info object for the given
308 const HostInfo *GetHostInfo(const char *HostTriple) const;
310 /// ShouldUseClangCompilar - Should the clang compiler be used to
311 /// handle this action.
312 bool ShouldUseClangCompiler(const Compilation &C, const JobAction &JA,
313 const llvm::Triple &ArchName) const;
317 /// GetReleaseVersion - Parse (([0-9]+)(.([0-9]+)(.([0-9]+)?))?)? and
318 /// return the grouped values as integers. Numbers which are not
319 /// provided are set to 0.
321 /// \return True if the entire string was parsed (9.2), or all
322 /// groups were parsed (10.3.5extrastuff). HadExtra is true if all
323 /// groups were parsed but extra characters remain at the end.
324 static bool GetReleaseVersion(const char *Str, unsigned &Major,
325 unsigned &Minor, unsigned &Micro,
329 } // end namespace driver
330 } // end namespace clang