Driver.h revision e3fdca2ee0346a41d9cc5ee417a75e66274216f5
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/System/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} 29namespace clang { 30namespace driver { 31 class Action; 32 class ArgList; 33 class Compilation; 34 class HostInfo; 35 class InputArgList; 36 class InputInfo; 37 class JobAction; 38 class OptTable; 39 class PipedJob; 40 class ToolChain; 41 42/// Driver - Encapsulate logic for constructing compilation processes 43/// from a set of gcc-driver-like command line arguments. 44class Driver { 45 OptTable *Opts; 46 47 Diagnostic &Diags; 48 49public: 50 // Diag - Forwarding function for diagnostics. 51 DiagnosticBuilder Diag(unsigned DiagID) const { 52 return Diags.Report(DiagID); 53 } 54 55 // FIXME: Privatize once interface is stable. 56public: 57 /// The name the driver was invoked as. 58 std::string Name; 59 60 /// The path the driver executable was in, as invoked from the 61 /// command line. 62 std::string Dir; 63 64 /// Default host triple. 65 std::string DefaultHostTriple; 66 67 /// Default name for linked images (e.g., "a.out"). 68 std::string DefaultImageName; 69 70 /// Host information for the platform the driver is running as. This 71 /// will generally be the actual host platform, but not always. 72 const HostInfo *Host; 73 74 /// Information about the host which can be overriden by the user. 75 std::string HostBits, HostMachine, HostSystem, HostRelease; 76 77 /// Whether the driver should follow g++ like behavior. 78 bool CCCIsCXX : 1; 79 80 /// Echo commands while executing (in -v style). 81 bool CCCEcho : 1; 82 83 /// Only print tool bindings, don't build any jobs. 84 bool CCCPrintBindings : 1; 85 86 /// Name to use when calling the generic gcc. 87 std::string CCCGenericGCCName; 88 89private: 90 /// Use the clang compiler where possible. 91 bool CCCUseClang : 1; 92 93 /// Use clang for handling C++ and Objective-C++ inputs. 94 bool CCCUseClangCXX : 1; 95 96 /// Use clang as a preprocessor (clang's preprocessor will still be 97 /// used where an integrated CPP would). 98 bool CCCUseClangCPP : 1; 99 100public: 101 /// Use lazy precompiled headers for PCH support. 102 bool CCCUsePCH; 103 104private: 105 /// Only use clang for the given architectures (only used when 106 /// non-empty). 107 std::set<llvm::Triple::ArchType> CCCClangArchs; 108 109 /// Certain options suppress the 'no input files' warning. 110 bool SuppressMissingInputWarning : 1; 111 112 std::list<std::string> TempFiles; 113 std::list<std::string> ResultFiles; 114 115public: 116 Driver(llvm::StringRef _Name, llvm::StringRef _Dir, 117 llvm::StringRef _DefaultHostTriple, 118 llvm::StringRef _DefaultImageName, 119 bool IsProduction, Diagnostic &_Diags); 120 ~Driver(); 121 122 /// @name Accessors 123 /// @{ 124 125 const OptTable &getOpts() const { return *Opts; } 126 127 const Diagnostic &getDiags() const { return Diags; } 128 129 /// @} 130 /// @name Primary Functionality 131 /// @{ 132 133 /// BuildCompilation - Construct a compilation object for a command 134 /// line argument vector. 135 /// 136 /// \return A compilation, or 0 if none was built for the given 137 /// argument vector. A null return value does not necessarily 138 /// indicate an error condition, the diagnostics should be queried 139 /// to determine if an error occurred. 140 Compilation *BuildCompilation(int argc, const char **argv); 141 142 /// @name Driver Steps 143 /// @{ 144 145 /// ParseArgStrings - Parse the given list of strings into an 146 /// ArgList. 147 InputArgList *ParseArgStrings(const char **ArgBegin, const char **ArgEnd); 148 149 /// BuildActions - Construct the list of actions to perform for the 150 /// given arguments, which are only done for a single architecture. 151 /// 152 /// \param Args - The input arguments. 153 /// \param Actions - The list to store the resulting actions onto. 154 void BuildActions(const ArgList &Args, ActionList &Actions) const; 155 156 /// BuildUniversalActions - Construct the list of actions to perform 157 /// for the given arguments, which may require a universal build. 158 /// 159 /// \param Args - The input arguments. 160 /// \param Actions - The list to store the resulting actions onto. 161 void BuildUniversalActions(const ArgList &Args, ActionList &Actions) const; 162 163 /// BuildJobs - Bind actions to concrete tools and translate 164 /// arguments to form the list of jobs to run. 165 /// 166 /// \arg C - The compilation that is being built. 167 void BuildJobs(Compilation &C) const; 168 169 /// ExecuteCompilation - Execute the compilation according to the command line 170 /// arguments and return an appropriate exit code. 171 /// 172 /// This routine handles additional processing that must be done in addition 173 /// to just running the subprocesses, for example reporting errors, removing 174 /// temporary files, etc. 175 int ExecuteCompilation(const Compilation &C) const; 176 177 /// @} 178 /// @name Helper Methods 179 /// @{ 180 181 /// PrintActions - Print the list of actions. 182 void PrintActions(const Compilation &C) const; 183 184 /// PrintHelp - Print the help text. 185 /// 186 /// \param ShowHidden - Show hidden options. 187 void PrintHelp(bool ShowHidden) const; 188 189 /// PrintOptions - Print the list of arguments. 190 void PrintOptions(const ArgList &Args) const; 191 192 /// PrintVersion - Print the driver version. 193 void PrintVersion(const Compilation &C, llvm::raw_ostream &OS) const; 194 195 /// GetFilePath - Lookup \arg Name in the list of file search paths. 196 /// 197 /// \arg TC - The tool chain for additional information on 198 /// directories to search. 199 // 200 // FIXME: This should be in CompilationInfo. 201 std::string GetFilePath(const char *Name, const ToolChain &TC) const; 202 203 /// GetProgramPath - Lookup \arg Name in the list of program search 204 /// paths. 205 /// 206 /// \arg TC - The provided tool chain for additional information on 207 /// directories to search. 208 /// 209 /// \arg WantFile - False when searching for an executable file, otherwise 210 /// true. Defaults to false. 211 // 212 // FIXME: This should be in CompilationInfo. 213 std::string GetProgramPath(const char *Name, const ToolChain &TC, 214 bool WantFile = false) const; 215 216 /// HandleImmediateArgs - Handle any arguments which should be 217 /// treated before building actions or binding tools. 218 /// 219 /// \return Whether any compilation should be built for this 220 /// invocation. 221 bool HandleImmediateArgs(const Compilation &C); 222 223 /// ConstructAction - Construct the appropriate action to do for 224 /// \arg Phase on the \arg Input, taking in to account arguments 225 /// like -fsyntax-only or --analyze. 226 Action *ConstructPhaseAction(const ArgList &Args, phases::ID Phase, 227 Action *Input) const; 228 229 230 /// BuildJobsForAction - Construct the jobs to perform for the 231 /// action \arg A. 232 void BuildJobsForAction(Compilation &C, 233 const Action *A, 234 const ToolChain *TC, 235 const char *BoundArch, 236 bool CanAcceptPipe, 237 bool AtTopLevel, 238 const char *LinkingOutput, 239 InputInfo &Result) const; 240 241 /// GetNamedOutputPath - Return the name to use for the output of 242 /// the action \arg JA. The result is appended to the compilation's 243 /// list of temporary or result files, as appropriate. 244 /// 245 /// \param C - The compilation. 246 /// \param JA - The action of interest. 247 /// \param BaseInput - The original input file that this action was 248 /// triggered by. 249 /// \param AtTopLevel - Whether this is a "top-level" action. 250 const char *GetNamedOutputPath(Compilation &C, 251 const JobAction &JA, 252 const char *BaseInput, 253 bool AtTopLevel) const; 254 255 /// GetTemporaryPath - Return the pathname of a temporary file to 256 /// use as part of compilation; the file will have the given suffix. 257 /// 258 /// GCC goes to extra lengths here to be a bit more robust. 259 std::string GetTemporaryPath(const char *Suffix) const; 260 261 /// GetHostInfo - Construct a new host info object for the given 262 /// host triple. 263 const HostInfo *GetHostInfo(const char *HostTriple) const; 264 265 /// ShouldUseClangCompilar - Should the clang compiler be used to 266 /// handle this action. 267 bool ShouldUseClangCompiler(const Compilation &C, const JobAction &JA, 268 const llvm::Triple &ArchName) const; 269 270 /// @} 271 272 /// GetReleaseVersion - Parse (([0-9]+)(.([0-9]+)(.([0-9]+)?))?)? and 273 /// return the grouped values as integers. Numbers which are not 274 /// provided are set to 0. 275 /// 276 /// \return True if the entire string was parsed (9.2), or all 277 /// groups were parsed (10.3.5extrastuff). HadExtra is true if all 278 /// groups were parsed but extra characters remain at the end. 279 static bool GetReleaseVersion(const char *Str, unsigned &Major, 280 unsigned &Minor, unsigned &Micro, 281 bool &HadExtra); 282}; 283 284} // end namespace driver 285} // end namespace clang 286 287#endif 288