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