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