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