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
42 /// Driver - Encapsulate logic for constructing compilation processes
43 /// from a set of gcc-driver-like command line arguments.
50 // Diag - Forwarding function for diagnostics.
51 DiagnosticBuilder Diag(unsigned DiagID) const {
52 return Diags.Report(DiagID);
55 // FIXME: Privatize once interface is stable.
57 /// The name the driver was invoked as.
60 /// The path the driver executable was in, as invoked from the
64 /// The path to the compiler resource directory.
65 std::string ResourceDir;
67 /// Default host triple.
68 std::string DefaultHostTriple;
70 /// Default name for linked images (e.g., "a.out").
71 std::string DefaultImageName;
73 /// Host information for the platform the driver is running as. This
74 /// will generally be the actual host platform, but not always.
77 /// Information about the host which can be overriden by the user.
78 std::string HostBits, HostMachine, HostSystem, HostRelease;
80 /// Name to use when calling the generic gcc.
81 std::string CCCGenericGCCName;
83 /// Whether the driver should follow g++ like behavior.
84 unsigned CCCIsCXX : 1;
86 /// Echo commands while executing (in -v style).
89 /// Only print tool bindings, don't build any jobs.
90 unsigned CCCPrintBindings : 1;
93 /// Whether to check that input files exist when constructing compilation
95 unsigned CheckInputsExist : 1;
97 /// Use the clang compiler where possible.
98 unsigned CCCUseClang : 1;
100 /// Use clang for handling C++ and Objective-C++ inputs.
101 unsigned CCCUseClangCXX : 1;
103 /// Use clang as a preprocessor (clang's preprocessor will still be
104 /// used where an integrated CPP would).
105 unsigned CCCUseClangCPP : 1;
108 /// Use lazy precompiled headers for PCH support.
109 unsigned CCCUsePCH : 1;
112 /// Only use clang for the given architectures (only used when
114 std::set<llvm::Triple::ArchType> CCCClangArchs;
116 /// Certain options suppress the 'no input files' warning.
117 bool SuppressMissingInputWarning : 1;
119 std::list<std::string> TempFiles;
120 std::list<std::string> ResultFiles;
123 Driver(llvm::StringRef _Name, llvm::StringRef _Dir,
124 llvm::StringRef _DefaultHostTriple,
125 llvm::StringRef _DefaultImageName,
126 bool IsProduction, Diagnostic &_Diags);
132 const OptTable &getOpts() const { return *Opts; }
134 const Diagnostic &getDiags() const { return Diags; }
136 bool getCheckInputsExist() const { return CheckInputsExist; }
138 void setCheckInputsExist(bool Value) { CheckInputsExist = Value; }
141 /// @name Primary Functionality
144 /// BuildCompilation - Construct a compilation object for a command
145 /// line argument vector.
147 /// \return A compilation, or 0 if none was built for the given
148 /// argument vector. A null return value does not necessarily
149 /// indicate an error condition, the diagnostics should be queried
150 /// to determine if an error occurred.
151 Compilation *BuildCompilation(int argc, const char **argv);
153 /// @name Driver Steps
156 /// ParseArgStrings - Parse the given list of strings into an
158 InputArgList *ParseArgStrings(const char **ArgBegin, const char **ArgEnd);
160 /// BuildActions - Construct the list of actions to perform for the
161 /// given arguments, which are only done for a single architecture.
163 /// \param Args - The input arguments.
164 /// \param Actions - The list to store the resulting actions onto.
165 void BuildActions(const ArgList &Args, ActionList &Actions) const;
167 /// BuildUniversalActions - Construct the list of actions to perform
168 /// for the given arguments, which may require a universal build.
170 /// \param Args - The input arguments.
171 /// \param Actions - The list to store the resulting actions onto.
172 void BuildUniversalActions(const ArgList &Args, ActionList &Actions) const;
174 /// BuildJobs - Bind actions to concrete tools and translate
175 /// arguments to form the list of jobs to run.
177 /// \arg C - The compilation that is being built.
178 void BuildJobs(Compilation &C) const;
180 /// ExecuteCompilation - Execute the compilation according to the command line
181 /// arguments and return an appropriate exit code.
183 /// This routine handles additional processing that must be done in addition
184 /// to just running the subprocesses, for example reporting errors, removing
185 /// temporary files, etc.
186 int ExecuteCompilation(const Compilation &C) const;
189 /// @name Helper Methods
192 /// PrintActions - Print the list of actions.
193 void PrintActions(const Compilation &C) const;
195 /// PrintHelp - Print the help text.
197 /// \param ShowHidden - Show hidden options.
198 void PrintHelp(bool ShowHidden) const;
200 /// PrintOptions - Print the list of arguments.
201 void PrintOptions(const ArgList &Args) const;
203 /// PrintVersion - Print the driver version.
204 void PrintVersion(const Compilation &C, llvm::raw_ostream &OS) const;
206 /// GetFilePath - Lookup \arg Name in the list of file search paths.
208 /// \arg TC - The tool chain for additional information on
209 /// directories to search.
211 // FIXME: This should be in CompilationInfo.
212 std::string GetFilePath(const char *Name, const ToolChain &TC) const;
214 /// GetProgramPath - Lookup \arg Name in the list of program search
217 /// \arg TC - The provided tool chain for additional information on
218 /// directories to search.
220 /// \arg WantFile - False when searching for an executable file, otherwise
221 /// true. Defaults to false.
223 // FIXME: This should be in CompilationInfo.
224 std::string GetProgramPath(const char *Name, const ToolChain &TC,
225 bool WantFile = false) const;
227 /// HandleImmediateArgs - Handle any arguments which should be
228 /// treated before building actions or binding tools.
230 /// \return Whether any compilation should be built for this
232 bool HandleImmediateArgs(const Compilation &C);
234 /// ConstructAction - Construct the appropriate action to do for
235 /// \arg Phase on the \arg Input, taking in to account arguments
236 /// like -fsyntax-only or --analyze.
237 Action *ConstructPhaseAction(const ArgList &Args, phases::ID Phase,
238 Action *Input) const;
241 /// BuildJobsForAction - Construct the jobs to perform for the
243 void BuildJobsForAction(Compilation &C,
246 const char *BoundArch,
249 const char *LinkingOutput,
250 InputInfo &Result) const;
252 /// GetNamedOutputPath - Return the name to use for the output of
253 /// the action \arg JA. The result is appended to the compilation's
254 /// list of temporary or result files, as appropriate.
256 /// \param C - The compilation.
257 /// \param JA - The action of interest.
258 /// \param BaseInput - The original input file that this action was
260 /// \param AtTopLevel - Whether this is a "top-level" action.
261 const char *GetNamedOutputPath(Compilation &C,
263 const char *BaseInput,
264 bool AtTopLevel) const;
266 /// GetTemporaryPath - Return the pathname of a temporary file to
267 /// use as part of compilation; the file will have the given suffix.
269 /// GCC goes to extra lengths here to be a bit more robust.
270 std::string GetTemporaryPath(const char *Suffix) const;
272 /// GetHostInfo - Construct a new host info object for the given
274 const HostInfo *GetHostInfo(const char *HostTriple) const;
276 /// ShouldUseClangCompilar - Should the clang compiler be used to
277 /// handle this action.
278 bool ShouldUseClangCompiler(const Compilation &C, const JobAction &JA,
279 const llvm::Triple &ArchName) const;
283 /// GetReleaseVersion - Parse (([0-9]+)(.([0-9]+)(.([0-9]+)?))?)? and
284 /// return the grouped values as integers. Numbers which are not
285 /// provided are set to 0.
287 /// \return True if the entire string was parsed (9.2), or all
288 /// groups were parsed (10.3.5extrastuff). HadExtra is true if all
289 /// groups were parsed but extra characters remain at the end.
290 static bool GetReleaseVersion(const char *Str, unsigned &Major,
291 unsigned &Minor, unsigned &Micro,
295 } // end namespace driver
296 } // end namespace clang