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