Daniel Dunbar | 63c4da9 | 2009-03-02 19:59:07 +0000 | [diff] [blame] | 1 | //===--- Driver.cpp - Clang GCC Compatible Driver -----------------------*-===// |
| 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 | |
Daniel Dunbar | 63c4da9 | 2009-03-02 19:59:07 +0000 | [diff] [blame] | 10 | #include "clang/Driver/Driver.h" |
Daniel Dunbar | 63c4da9 | 2009-03-02 19:59:07 +0000 | [diff] [blame] | 11 | |
Daniel Dunbar | db62cc3 | 2009-03-12 07:58:46 +0000 | [diff] [blame] | 12 | #include "clang/Driver/Action.h" |
Daniel Dunbar | d6f0e37 | 2009-03-04 20:49:20 +0000 | [diff] [blame] | 13 | #include "clang/Driver/Arg.h" |
| 14 | #include "clang/Driver/ArgList.h" |
| 15 | #include "clang/Driver/Compilation.h" |
Daniel Dunbar | 9346849 | 2009-03-12 08:55:43 +0000 | [diff] [blame] | 16 | #include "clang/Driver/DriverDiagnostic.h" |
Daniel Dunbar | d25acaa | 2009-03-10 23:41:59 +0000 | [diff] [blame] | 17 | #include "clang/Driver/HostInfo.h" |
Daniel Dunbar | 7ce6add | 2009-03-16 06:56:51 +0000 | [diff] [blame] | 18 | #include "clang/Driver/Job.h" |
Daniel Dunbar | 7dc2a04 | 2009-03-05 06:38:47 +0000 | [diff] [blame] | 19 | #include "clang/Driver/Option.h" |
Daniel Dunbar | d6f0e37 | 2009-03-04 20:49:20 +0000 | [diff] [blame] | 20 | #include "clang/Driver/Options.h" |
Daniel Dunbar | 7ce6add | 2009-03-16 06:56:51 +0000 | [diff] [blame] | 21 | #include "clang/Driver/Tool.h" |
| 22 | #include "clang/Driver/ToolChain.h" |
Daniel Dunbar | db62cc3 | 2009-03-12 07:58:46 +0000 | [diff] [blame] | 23 | #include "clang/Driver/Types.h" |
Daniel Dunbar | 7dc2a04 | 2009-03-05 06:38:47 +0000 | [diff] [blame] | 24 | |
Douglas Gregor | b706474 | 2009-04-27 22:23:34 +0000 | [diff] [blame] | 25 | #include "clang/Basic/Version.h" |
| 26 | |
Daniel Dunbar | b1873cd | 2009-03-13 20:33:35 +0000 | [diff] [blame] | 27 | #include "llvm/ADT/StringSet.h" |
Daniel Dunbar | 16e04ff | 2009-03-18 01:38:48 +0000 | [diff] [blame] | 28 | #include "llvm/Support/PrettyStackTrace.h" |
Daniel Dunbar | 7dc2a04 | 2009-03-05 06:38:47 +0000 | [diff] [blame] | 29 | #include "llvm/Support/raw_ostream.h" |
Daniel Dunbar | db62cc3 | 2009-03-12 07:58:46 +0000 | [diff] [blame] | 30 | #include "llvm/System/Path.h" |
Daniel Dunbar | cb84b9a | 2009-03-18 21:34:08 +0000 | [diff] [blame] | 31 | #include "llvm/System/Program.h" |
Daniel Dunbar | 494646b | 2009-03-13 12:19:02 +0000 | [diff] [blame] | 32 | |
Daniel Dunbar | 7ce6add | 2009-03-16 06:56:51 +0000 | [diff] [blame] | 33 | #include "InputInfo.h" |
| 34 | |
Daniel Dunbar | 494646b | 2009-03-13 12:19:02 +0000 | [diff] [blame] | 35 | #include <map> |
| 36 | |
Daniel Dunbar | d6f0e37 | 2009-03-04 20:49:20 +0000 | [diff] [blame] | 37 | using namespace clang::driver; |
Chris Lattner | c272d26 | 2009-03-26 05:56:24 +0000 | [diff] [blame] | 38 | using namespace clang; |
Daniel Dunbar | d6f0e37 | 2009-03-04 20:49:20 +0000 | [diff] [blame] | 39 | |
Daniel Dunbar | d25acaa | 2009-03-10 23:41:59 +0000 | [diff] [blame] | 40 | Driver::Driver(const char *_Name, const char *_Dir, |
Daniel Dunbar | 9346849 | 2009-03-12 08:55:43 +0000 | [diff] [blame] | 41 | const char *_DefaultHostTriple, |
Daniel Dunbar | 7ce6add | 2009-03-16 06:56:51 +0000 | [diff] [blame] | 42 | const char *_DefaultImageName, |
Daniel Dunbar | 9346849 | 2009-03-12 08:55:43 +0000 | [diff] [blame] | 43 | Diagnostic &_Diags) |
| 44 | : Opts(new OptTable()), Diags(_Diags), |
Daniel Dunbar | d25acaa | 2009-03-10 23:41:59 +0000 | [diff] [blame] | 45 | Name(_Name), Dir(_Dir), DefaultHostTriple(_DefaultHostTriple), |
Daniel Dunbar | 7ce6add | 2009-03-16 06:56:51 +0000 | [diff] [blame] | 46 | DefaultImageName(_DefaultImageName), |
Daniel Dunbar | d25acaa | 2009-03-10 23:41:59 +0000 | [diff] [blame] | 47 | Host(0), |
Daniel Dunbar | 07c9a1d | 2009-03-17 22:47:06 +0000 | [diff] [blame] | 48 | CCCIsCXX(false), CCCEcho(false), CCCPrintBindings(false), |
Daniel Dunbar | 126da5e | 2009-04-01 23:34:41 +0000 | [diff] [blame] | 49 | CCCGenericGCCName("gcc"), CCCUseClang(true), CCCUseClangCXX(false), |
Douglas Gregor | f93696d | 2009-04-28 22:44:02 +0000 | [diff] [blame] | 50 | CCCUseClangCPP(true), CCCUsePCH(true), |
Daniel Dunbar | 5a5ec5c | 2009-03-13 00:17:48 +0000 | [diff] [blame] | 51 | SuppressMissingInputWarning(false) |
Daniel Dunbar | b282ced | 2009-03-10 20:52:46 +0000 | [diff] [blame] | 52 | { |
Daniel Dunbar | 0ea85f7 | 2009-03-24 19:02:31 +0000 | [diff] [blame] | 53 | // Only use clang on i386 and x86_64 by default. |
| 54 | CCCClangArchs.insert("i386"); |
| 55 | CCCClangArchs.insert("x86_64"); |
Daniel Dunbar | 63c4da9 | 2009-03-02 19:59:07 +0000 | [diff] [blame] | 56 | } |
| 57 | |
| 58 | Driver::~Driver() { |
Daniel Dunbar | d6f0e37 | 2009-03-04 20:49:20 +0000 | [diff] [blame] | 59 | delete Opts; |
Daniel Dunbar | 2f8b37e | 2009-03-18 01:09:40 +0000 | [diff] [blame] | 60 | delete Host; |
Daniel Dunbar | 63c4da9 | 2009-03-02 19:59:07 +0000 | [diff] [blame] | 61 | } |
| 62 | |
Daniel Dunbar | a16e4fe | 2009-03-25 04:13:45 +0000 | [diff] [blame] | 63 | InputArgList *Driver::ParseArgStrings(const char **ArgBegin, |
| 64 | const char **ArgEnd) { |
Daniel Dunbar | 16e04ff | 2009-03-18 01:38:48 +0000 | [diff] [blame] | 65 | llvm::PrettyStackTraceString CrashInfo("Command line argument parsing"); |
Daniel Dunbar | a16e4fe | 2009-03-25 04:13:45 +0000 | [diff] [blame] | 66 | InputArgList *Args = new InputArgList(ArgBegin, ArgEnd); |
Daniel Dunbar | 7dc2a04 | 2009-03-05 06:38:47 +0000 | [diff] [blame] | 67 | |
Daniel Dunbar | 85cb359 | 2009-03-13 11:38:42 +0000 | [diff] [blame] | 68 | // FIXME: Handle '@' args (or at least error on them). |
| 69 | |
Daniel Dunbar | 7dc2a04 | 2009-03-05 06:38:47 +0000 | [diff] [blame] | 70 | unsigned Index = 0, End = ArgEnd - ArgBegin; |
| 71 | while (Index < End) { |
Daniel Dunbar | b043ebd | 2009-03-13 01:01:44 +0000 | [diff] [blame] | 72 | // gcc's handling of empty arguments doesn't make |
| 73 | // sense, but this is not a common use case. :) |
| 74 | // |
| 75 | // We just ignore them here (note that other things may |
| 76 | // still take them as arguments). |
| 77 | if (Args->getArgString(Index)[0] == '\0') { |
| 78 | ++Index; |
| 79 | continue; |
| 80 | } |
| 81 | |
Daniel Dunbar | 7dc2a04 | 2009-03-05 06:38:47 +0000 | [diff] [blame] | 82 | unsigned Prev = Index; |
Daniel Dunbar | fb88ce0 | 2009-03-22 23:26:43 +0000 | [diff] [blame] | 83 | Arg *A = getOpts().ParseOneArg(*Args, Index); |
| 84 | assert(Index > Prev && "Parser failed to consume argument."); |
Daniel Dunbar | db62cc3 | 2009-03-12 07:58:46 +0000 | [diff] [blame] | 85 | |
Daniel Dunbar | fb88ce0 | 2009-03-22 23:26:43 +0000 | [diff] [blame] | 86 | // Check for missing argument error. |
| 87 | if (!A) { |
| 88 | assert(Index >= End && "Unexpected parser error."); |
| 89 | Diag(clang::diag::err_drv_missing_argument) |
| 90 | << Args->getArgString(Prev) |
| 91 | << (Index - Prev - 1); |
| 92 | break; |
Daniel Dunbar | db62cc3 | 2009-03-12 07:58:46 +0000 | [diff] [blame] | 93 | } |
Daniel Dunbar | 7dc2a04 | 2009-03-05 06:38:47 +0000 | [diff] [blame] | 94 | |
Daniel Dunbar | fb88ce0 | 2009-03-22 23:26:43 +0000 | [diff] [blame] | 95 | if (A->getOption().isUnsupported()) { |
| 96 | Diag(clang::diag::err_drv_unsupported_opt) << A->getAsString(*Args); |
| 97 | continue; |
| 98 | } |
| 99 | Args->append(A); |
Daniel Dunbar | 7dc2a04 | 2009-03-05 06:38:47 +0000 | [diff] [blame] | 100 | } |
| 101 | |
| 102 | return Args; |
| 103 | } |
| 104 | |
Daniel Dunbar | 63c4da9 | 2009-03-02 19:59:07 +0000 | [diff] [blame] | 105 | Compilation *Driver::BuildCompilation(int argc, const char **argv) { |
Daniel Dunbar | 16e04ff | 2009-03-18 01:38:48 +0000 | [diff] [blame] | 106 | llvm::PrettyStackTraceString CrashInfo("Compilation construction"); |
| 107 | |
Daniel Dunbar | cc00689 | 2009-03-13 00:51:18 +0000 | [diff] [blame] | 108 | // FIXME: Handle environment options which effect driver behavior, |
| 109 | // somewhere (client?). GCC_EXEC_PREFIX, COMPILER_PATH, |
| 110 | // LIBRARY_PATH, LPATH, CC_PRINT_OPTIONS, QA_OVERRIDE_GCC3_OPTIONS. |
| 111 | |
| 112 | // FIXME: What are we going to do with -V and -b? |
| 113 | |
Daniel Dunbar | b282ced | 2009-03-10 20:52:46 +0000 | [diff] [blame] | 114 | // FIXME: This stuff needs to go into the Compilation, not the |
| 115 | // driver. |
Daniel Dunbar | db62cc3 | 2009-03-12 07:58:46 +0000 | [diff] [blame] | 116 | bool CCCPrintOptions = false, CCCPrintActions = false; |
Daniel Dunbar | 7dc2a04 | 2009-03-05 06:38:47 +0000 | [diff] [blame] | 117 | |
Daniel Dunbar | b282ced | 2009-03-10 20:52:46 +0000 | [diff] [blame] | 118 | const char **Start = argv + 1, **End = argv + argc; |
Daniel Dunbar | d25acaa | 2009-03-10 23:41:59 +0000 | [diff] [blame] | 119 | const char *HostTriple = DefaultHostTriple.c_str(); |
Daniel Dunbar | b282ced | 2009-03-10 20:52:46 +0000 | [diff] [blame] | 120 | |
| 121 | // Read -ccc args. |
| 122 | // |
| 123 | // FIXME: We need to figure out where this behavior should |
| 124 | // live. Most of it should be outside in the client; the parts that |
| 125 | // aren't should have proper options, either by introducing new ones |
| 126 | // or by overloading gcc ones like -V or -b. |
| 127 | for (; Start != End && memcmp(*Start, "-ccc-", 5) == 0; ++Start) { |
| 128 | const char *Opt = *Start + 5; |
| 129 | |
| 130 | if (!strcmp(Opt, "print-options")) { |
| 131 | CCCPrintOptions = true; |
| 132 | } else if (!strcmp(Opt, "print-phases")) { |
Daniel Dunbar | db62cc3 | 2009-03-12 07:58:46 +0000 | [diff] [blame] | 133 | CCCPrintActions = true; |
Daniel Dunbar | 07c9a1d | 2009-03-17 22:47:06 +0000 | [diff] [blame] | 134 | } else if (!strcmp(Opt, "print-bindings")) { |
| 135 | CCCPrintBindings = true; |
Daniel Dunbar | b282ced | 2009-03-10 20:52:46 +0000 | [diff] [blame] | 136 | } else if (!strcmp(Opt, "cxx")) { |
| 137 | CCCIsCXX = true; |
| 138 | } else if (!strcmp(Opt, "echo")) { |
| 139 | CCCEcho = true; |
| 140 | |
Daniel Dunbar | 126da5e | 2009-04-01 23:34:41 +0000 | [diff] [blame] | 141 | } else if (!strcmp(Opt, "gcc-name")) { |
| 142 | assert(Start+1 < End && "FIXME: -ccc- argument handling."); |
| 143 | CCCGenericGCCName = *++Start; |
| 144 | |
Daniel Dunbar | 0ea85f7 | 2009-03-24 19:02:31 +0000 | [diff] [blame] | 145 | } else if (!strcmp(Opt, "clang-cxx")) { |
| 146 | CCCUseClangCXX = true; |
Douglas Gregor | 9573450 | 2009-04-18 00:34:01 +0000 | [diff] [blame] | 147 | } else if (!strcmp(Opt, "pch-is-pch")) { |
| 148 | CCCUsePCH = true; |
| 149 | } else if (!strcmp(Opt, "pch-is-pth")) { |
| 150 | CCCUsePCH = false; |
Daniel Dunbar | b282ced | 2009-03-10 20:52:46 +0000 | [diff] [blame] | 151 | } else if (!strcmp(Opt, "no-clang")) { |
Daniel Dunbar | 0ea85f7 | 2009-03-24 19:02:31 +0000 | [diff] [blame] | 152 | CCCUseClang = false; |
Daniel Dunbar | b282ced | 2009-03-10 20:52:46 +0000 | [diff] [blame] | 153 | } else if (!strcmp(Opt, "no-clang-cpp")) { |
Daniel Dunbar | 0ea85f7 | 2009-03-24 19:02:31 +0000 | [diff] [blame] | 154 | CCCUseClangCPP = false; |
Daniel Dunbar | b282ced | 2009-03-10 20:52:46 +0000 | [diff] [blame] | 155 | } else if (!strcmp(Opt, "clang-archs")) { |
| 156 | assert(Start+1 < End && "FIXME: -ccc- argument handling."); |
| 157 | const char *Cur = *++Start; |
| 158 | |
Daniel Dunbar | 0ea85f7 | 2009-03-24 19:02:31 +0000 | [diff] [blame] | 159 | CCCClangArchs.clear(); |
Daniel Dunbar | b282ced | 2009-03-10 20:52:46 +0000 | [diff] [blame] | 160 | for (;;) { |
| 161 | const char *Next = strchr(Cur, ','); |
| 162 | |
| 163 | if (Next) { |
Daniel Dunbar | 0ea85f7 | 2009-03-24 19:02:31 +0000 | [diff] [blame] | 164 | if (Cur != Next) |
| 165 | CCCClangArchs.insert(std::string(Cur, Next)); |
Daniel Dunbar | b282ced | 2009-03-10 20:52:46 +0000 | [diff] [blame] | 166 | Cur = Next + 1; |
| 167 | } else { |
Daniel Dunbar | 0ea85f7 | 2009-03-24 19:02:31 +0000 | [diff] [blame] | 168 | if (*Cur != '\0') |
| 169 | CCCClangArchs.insert(std::string(Cur)); |
Daniel Dunbar | b282ced | 2009-03-10 20:52:46 +0000 | [diff] [blame] | 170 | break; |
| 171 | } |
| 172 | } |
| 173 | |
Daniel Dunbar | d25acaa | 2009-03-10 23:41:59 +0000 | [diff] [blame] | 174 | } else if (!strcmp(Opt, "host-triple")) { |
Daniel Dunbar | b282ced | 2009-03-10 20:52:46 +0000 | [diff] [blame] | 175 | assert(Start+1 < End && "FIXME: -ccc- argument handling."); |
Daniel Dunbar | d25acaa | 2009-03-10 23:41:59 +0000 | [diff] [blame] | 176 | HostTriple = *++Start; |
Daniel Dunbar | b282ced | 2009-03-10 20:52:46 +0000 | [diff] [blame] | 177 | |
| 178 | } else { |
| 179 | // FIXME: Error handling. |
| 180 | llvm::errs() << "invalid option: " << *Start << "\n"; |
| 181 | exit(1); |
| 182 | } |
| 183 | } |
Daniel Dunbar | d25acaa | 2009-03-10 23:41:59 +0000 | [diff] [blame] | 184 | |
Daniel Dunbar | a16e4fe | 2009-03-25 04:13:45 +0000 | [diff] [blame] | 185 | InputArgList *Args = ParseArgStrings(Start, End); |
Daniel Dunbar | b282ced | 2009-03-10 20:52:46 +0000 | [diff] [blame] | 186 | |
Daniel Dunbar | 08966ca | 2009-03-17 20:45:45 +0000 | [diff] [blame] | 187 | Host = GetHostInfo(HostTriple); |
Daniel Dunbar | cc00689 | 2009-03-13 00:51:18 +0000 | [diff] [blame] | 188 | |
Daniel Dunbar | 9d625e1 | 2009-03-16 06:42:30 +0000 | [diff] [blame] | 189 | // The compilation takes ownership of Args. |
Daniel Dunbar | 31a76e3 | 2009-03-18 22:16:03 +0000 | [diff] [blame] | 190 | Compilation *C = new Compilation(*this, *Host->getToolChain(*Args), Args); |
Daniel Dunbar | 73f7b8c | 2009-03-18 02:55:38 +0000 | [diff] [blame] | 191 | |
| 192 | // FIXME: This behavior shouldn't be here. |
| 193 | if (CCCPrintOptions) { |
| 194 | PrintOptions(C->getArgs()); |
| 195 | return C; |
| 196 | } |
| 197 | |
| 198 | if (!HandleImmediateArgs(*C)) |
| 199 | return C; |
| 200 | |
| 201 | // Construct the list of abstract actions to perform for this |
| 202 | // compilation. We avoid passing a Compilation here simply to |
| 203 | // enforce the abstraction that pipelining is not host or toolchain |
| 204 | // dependent (other than the driver driver test). |
| 205 | if (Host->useDriverDriver()) |
| 206 | BuildUniversalActions(C->getArgs(), C->getActions()); |
| 207 | else |
| 208 | BuildActions(C->getArgs(), C->getActions()); |
| 209 | |
| 210 | if (CCCPrintActions) { |
Daniel Dunbar | 2b856ce | 2009-03-18 03:13:20 +0000 | [diff] [blame] | 211 | PrintActions(*C); |
Daniel Dunbar | 73f7b8c | 2009-03-18 02:55:38 +0000 | [diff] [blame] | 212 | return C; |
| 213 | } |
| 214 | |
| 215 | BuildJobs(*C); |
Daniel Dunbar | c413f82 | 2009-03-15 01:38:15 +0000 | [diff] [blame] | 216 | |
| 217 | return C; |
Daniel Dunbar | b282ced | 2009-03-10 20:52:46 +0000 | [diff] [blame] | 218 | } |
| 219 | |
Daniel Dunbar | 9fbdc88 | 2009-07-01 20:03:04 +0000 | [diff] [blame] | 220 | int Driver::ExecuteCompilation(const Compilation &C) const { |
| 221 | // Just print if -### was present. |
| 222 | if (C.getArgs().hasArg(options::OPT__HASH_HASH_HASH)) { |
| 223 | C.PrintJob(llvm::errs(), C.getJobs(), "\n", true); |
| 224 | return 0; |
| 225 | } |
| 226 | |
| 227 | // If there were errors building the compilation, quit now. |
| 228 | if (getDiags().getNumErrors()) |
| 229 | return 1; |
| 230 | |
| 231 | const Command *FailingCommand = 0; |
| 232 | int Res = C.ExecuteJob(C.getJobs(), FailingCommand); |
| 233 | |
| 234 | // Remove temp files. |
| 235 | C.CleanupFileList(C.getTempFiles()); |
| 236 | |
| 237 | // If the compilation failed, remove result files as well. |
| 238 | if (Res != 0 && !C.getArgs().hasArg(options::OPT_save_temps)) |
| 239 | C.CleanupFileList(C.getResultFiles(), true); |
| 240 | |
| 241 | // Print extra information about abnormal failures, if possible. |
| 242 | if (Res) { |
| 243 | // This is ad-hoc, but we don't want to be excessively noisy. If the result |
| 244 | // status was 1, assume the command failed normally. In particular, if it |
| 245 | // was the compiler then assume it gave a reasonable error code. Failures in |
| 246 | // other tools are less common, and they generally have worse diagnostics, |
| 247 | // so always print the diagnostic there. |
| 248 | const Action &Source = FailingCommand->getSource(); |
| 249 | bool IsFriendlyTool = (isa<PreprocessJobAction>(Source) || |
| 250 | isa<PrecompileJobAction>(Source) || |
| 251 | isa<AnalyzeJobAction>(Source) || |
| 252 | isa<CompileJobAction>(Source)); |
| 253 | |
| 254 | if (!IsFriendlyTool || Res != 1) { |
| 255 | // FIXME: See FIXME above regarding result code interpretation. |
| 256 | if (Res < 0) |
| 257 | Diag(clang::diag::err_drv_command_signalled) |
| 258 | << Source.getClassName() << -Res; |
| 259 | else |
| 260 | Diag(clang::diag::err_drv_command_failed) |
| 261 | << Source.getClassName() << Res; |
| 262 | } |
| 263 | } |
| 264 | |
| 265 | return Res; |
| 266 | } |
| 267 | |
Daniel Dunbar | a790d37 | 2009-03-12 18:24:49 +0000 | [diff] [blame] | 268 | void Driver::PrintOptions(const ArgList &Args) const { |
Daniel Dunbar | 7dc2a04 | 2009-03-05 06:38:47 +0000 | [diff] [blame] | 269 | unsigned i = 0; |
Daniel Dunbar | db62cc3 | 2009-03-12 07:58:46 +0000 | [diff] [blame] | 270 | for (ArgList::const_iterator it = Args.begin(), ie = Args.end(); |
Daniel Dunbar | 7dc2a04 | 2009-03-05 06:38:47 +0000 | [diff] [blame] | 271 | it != ie; ++it, ++i) { |
| 272 | Arg *A = *it; |
| 273 | llvm::errs() << "Option " << i << " - " |
| 274 | << "Name: \"" << A->getOption().getName() << "\", " |
| 275 | << "Values: {"; |
| 276 | for (unsigned j = 0; j < A->getNumValues(); ++j) { |
| 277 | if (j) |
| 278 | llvm::errs() << ", "; |
Daniel Dunbar | db62cc3 | 2009-03-12 07:58:46 +0000 | [diff] [blame] | 279 | llvm::errs() << '"' << A->getValue(Args, j) << '"'; |
Daniel Dunbar | 7dc2a04 | 2009-03-05 06:38:47 +0000 | [diff] [blame] | 280 | } |
| 281 | llvm::errs() << "}\n"; |
Daniel Dunbar | 7dc2a04 | 2009-03-05 06:38:47 +0000 | [diff] [blame] | 282 | } |
Daniel Dunbar | 63c4da9 | 2009-03-02 19:59:07 +0000 | [diff] [blame] | 283 | } |
Daniel Dunbar | d25acaa | 2009-03-10 23:41:59 +0000 | [diff] [blame] | 284 | |
Daniel Dunbar | e627b68 | 2009-03-31 21:38:17 +0000 | [diff] [blame] | 285 | static std::string getOptionHelpName(const OptTable &Opts, options::ID Id) { |
| 286 | std::string Name = Opts.getOptionName(Id); |
| 287 | |
| 288 | // Add metavar, if used. |
| 289 | switch (Opts.getOptionKind(Id)) { |
| 290 | case Option::GroupClass: case Option::InputClass: case Option::UnknownClass: |
| 291 | assert(0 && "Invalid option with help text."); |
| 292 | |
| 293 | case Option::MultiArgClass: case Option::JoinedAndSeparateClass: |
| 294 | assert(0 && "Cannot print metavar for this kind of option."); |
| 295 | |
| 296 | case Option::FlagClass: |
| 297 | break; |
| 298 | |
| 299 | case Option::SeparateClass: case Option::JoinedOrSeparateClass: |
| 300 | Name += ' '; |
| 301 | // FALLTHROUGH |
| 302 | case Option::JoinedClass: case Option::CommaJoinedClass: |
| 303 | Name += Opts.getOptionMetaVar(Id); |
| 304 | break; |
| 305 | } |
| 306 | |
| 307 | return Name; |
| 308 | } |
| 309 | |
Daniel Dunbar | a5f09bc | 2009-04-15 16:34:29 +0000 | [diff] [blame] | 310 | void Driver::PrintHelp(bool ShowHidden) const { |
Daniel Dunbar | e627b68 | 2009-03-31 21:38:17 +0000 | [diff] [blame] | 311 | llvm::raw_ostream &OS = llvm::outs(); |
| 312 | |
| 313 | OS << "OVERVIEW: clang \"gcc-compatible\" driver\n"; |
| 314 | OS << '\n'; |
| 315 | OS << "USAGE: " << Name << " [options] <input files>\n"; |
| 316 | OS << '\n'; |
| 317 | OS << "OPTIONS:\n"; |
| 318 | |
| 319 | // Render help text into (option, help) pairs. |
| 320 | std::vector< std::pair<std::string, const char*> > OptionHelp; |
| 321 | |
| 322 | for (unsigned i = options::OPT_INPUT, e = options::LastOption; i != e; ++i) { |
| 323 | options::ID Id = (options::ID) i; |
| 324 | if (const char *Text = getOpts().getOptionHelpText(Id)) |
| 325 | OptionHelp.push_back(std::make_pair(getOptionHelpName(getOpts(), Id), |
| 326 | Text)); |
| 327 | } |
| 328 | |
Daniel Dunbar | a5f09bc | 2009-04-15 16:34:29 +0000 | [diff] [blame] | 329 | if (ShowHidden) { |
| 330 | OptionHelp.push_back(std::make_pair("\nDRIVER OPTIONS:","")); |
| 331 | OptionHelp.push_back(std::make_pair("-ccc-cxx", |
| 332 | "Act as a C++ driver")); |
| 333 | OptionHelp.push_back(std::make_pair("-ccc-gcc-name", |
| 334 | "Name for native GCC compiler")); |
| 335 | OptionHelp.push_back(std::make_pair("-ccc-clang-cxx", |
| 336 | "Use the clang compiler for C++")); |
| 337 | OptionHelp.push_back(std::make_pair("-ccc-no-clang", |
| 338 | "Never use the clang compiler")); |
| 339 | OptionHelp.push_back(std::make_pair("-ccc-no-clang-cpp", |
| 340 | "Never use the clang preprocessor")); |
| 341 | OptionHelp.push_back(std::make_pair("-ccc-clang-archs", |
| 342 | "Comma separate list of architectures " |
| 343 | "to use the clang compiler for")); |
Douglas Gregor | 9573450 | 2009-04-18 00:34:01 +0000 | [diff] [blame] | 344 | OptionHelp.push_back(std::make_pair("-ccc-pch-is-pch", |
| 345 | "Use lazy PCH for precompiled headers")); |
| 346 | OptionHelp.push_back(std::make_pair("-ccc-pch-is-pth", |
| 347 | "Use pretokenized headers for precompiled headers")); |
Daniel Dunbar | a5f09bc | 2009-04-15 16:34:29 +0000 | [diff] [blame] | 348 | |
| 349 | OptionHelp.push_back(std::make_pair("\nDEBUG/DEVELOPMENT OPTIONS:","")); |
| 350 | OptionHelp.push_back(std::make_pair("-ccc-host-triple", |
| 351 | "Simulate running on the given target")); |
| 352 | OptionHelp.push_back(std::make_pair("-ccc-print-options", |
| 353 | "Dump parsed command line arguments")); |
| 354 | OptionHelp.push_back(std::make_pair("-ccc-print-phases", |
| 355 | "Dump list of actions to perform")); |
| 356 | OptionHelp.push_back(std::make_pair("-ccc-print-bindings", |
| 357 | "Show bindings of tools to actions")); |
| 358 | OptionHelp.push_back(std::make_pair("CCC_ADD_ARGS", |
| 359 | "(ENVIRONMENT VARIABLE) Comma separated list of " |
| 360 | "arguments to prepend to the command line")); |
| 361 | } |
| 362 | |
Daniel Dunbar | e627b68 | 2009-03-31 21:38:17 +0000 | [diff] [blame] | 363 | // Find the maximum option length. |
| 364 | unsigned OptionFieldWidth = 0; |
| 365 | for (unsigned i = 0, e = OptionHelp.size(); i != e; ++i) { |
Daniel Dunbar | a5f09bc | 2009-04-15 16:34:29 +0000 | [diff] [blame] | 366 | // Skip titles. |
| 367 | if (!OptionHelp[i].second) |
| 368 | continue; |
| 369 | |
Daniel Dunbar | e627b68 | 2009-03-31 21:38:17 +0000 | [diff] [blame] | 370 | // Limit the amount of padding we are willing to give up for |
| 371 | // alignment. |
| 372 | unsigned Length = OptionHelp[i].first.size(); |
| 373 | if (Length <= 23) |
| 374 | OptionFieldWidth = std::max(OptionFieldWidth, Length); |
| 375 | } |
| 376 | |
| 377 | for (unsigned i = 0, e = OptionHelp.size(); i != e; ++i) { |
| 378 | const std::string &Option = OptionHelp[i].first; |
| 379 | OS << " " << Option; |
| 380 | for (int j = Option.length(), e = OptionFieldWidth; j < e; ++j) |
| 381 | OS << ' '; |
| 382 | OS << ' ' << OptionHelp[i].second << '\n'; |
| 383 | } |
| 384 | |
| 385 | OS.flush(); |
| 386 | } |
| 387 | |
Daniel Dunbar | e00f1cd | 2009-07-21 20:06:58 +0000 | [diff] [blame] | 388 | void Driver::PrintVersion(const Compilation &C, llvm::raw_ostream &OS) const { |
Mike Stump | 74d9447 | 2009-03-18 14:00:02 +0000 | [diff] [blame] | 389 | static char buf[] = "$URL$"; |
| 390 | char *zap = strstr(buf, "/lib/Driver"); |
| 391 | if (zap) |
| 392 | *zap = 0; |
| 393 | zap = strstr(buf, "/clang/tools/clang"); |
| 394 | if (zap) |
| 395 | *zap = 0; |
Mike Stump | bc92729 | 2009-03-18 15:19:35 +0000 | [diff] [blame] | 396 | const char *vers = buf+6; |
Mike Stump | 3fc58f0 | 2009-03-18 18:45:55 +0000 | [diff] [blame] | 397 | // FIXME: Add cmake support and remove #ifdef |
| 398 | #ifdef SVN_REVISION |
| 399 | const char *revision = SVN_REVISION; |
| 400 | #else |
| 401 | const char *revision = ""; |
| 402 | #endif |
Daniel Dunbar | cc00689 | 2009-03-13 00:51:18 +0000 | [diff] [blame] | 403 | // FIXME: The following handlers should use a callback mechanism, we |
| 404 | // don't know what the client would like to do. |
Daniel Dunbar | e00f1cd | 2009-07-21 20:06:58 +0000 | [diff] [blame] | 405 | OS << "clang version " CLANG_VERSION_STRING " (" |
Daniel Dunbar | 41c34b3 | 2009-06-16 23:32:58 +0000 | [diff] [blame] | 406 | << vers << " " << revision << ")" << '\n'; |
Daniel Dunbar | 0ded7de | 2009-03-26 16:09:13 +0000 | [diff] [blame] | 407 | |
| 408 | const ToolChain &TC = C.getDefaultToolChain(); |
Daniel Dunbar | e00f1cd | 2009-07-21 20:06:58 +0000 | [diff] [blame] | 409 | OS << "Target: " << TC.getTripleString() << '\n'; |
Daniel Dunbar | 41c34b3 | 2009-06-16 23:32:58 +0000 | [diff] [blame] | 410 | |
| 411 | // Print the threading model. |
| 412 | // |
| 413 | // FIXME: Implement correctly. |
Daniel Dunbar | e00f1cd | 2009-07-21 20:06:58 +0000 | [diff] [blame] | 414 | OS << "Thread model: " << "posix" << '\n'; |
Daniel Dunbar | cc00689 | 2009-03-13 00:51:18 +0000 | [diff] [blame] | 415 | } |
| 416 | |
Daniel Dunbar | 73f7b8c | 2009-03-18 02:55:38 +0000 | [diff] [blame] | 417 | bool Driver::HandleImmediateArgs(const Compilation &C) { |
Daniel Dunbar | cc00689 | 2009-03-13 00:51:18 +0000 | [diff] [blame] | 418 | // The order these options are handled in in gcc is all over the |
| 419 | // place, but we don't expect inconsistencies w.r.t. that to matter |
| 420 | // in practice. |
Daniel Dunbar | e627b68 | 2009-03-31 21:38:17 +0000 | [diff] [blame] | 421 | |
Daniel Dunbar | 9a553c4 | 2009-04-04 05:17:38 +0000 | [diff] [blame] | 422 | if (C.getArgs().hasArg(options::OPT_dumpversion)) { |
Douglas Gregor | b706474 | 2009-04-27 22:23:34 +0000 | [diff] [blame] | 423 | llvm::outs() << CLANG_VERSION_STRING "\n"; |
Daniel Dunbar | 9a553c4 | 2009-04-04 05:17:38 +0000 | [diff] [blame] | 424 | return false; |
| 425 | } |
| 426 | |
Daniel Dunbar | a5f09bc | 2009-04-15 16:34:29 +0000 | [diff] [blame] | 427 | if (C.getArgs().hasArg(options::OPT__help) || |
| 428 | C.getArgs().hasArg(options::OPT__help_hidden)) { |
| 429 | PrintHelp(C.getArgs().hasArg(options::OPT__help_hidden)); |
Daniel Dunbar | e627b68 | 2009-03-31 21:38:17 +0000 | [diff] [blame] | 430 | return false; |
| 431 | } |
| 432 | |
Daniel Dunbar | 3b7c84c | 2009-04-02 15:05:41 +0000 | [diff] [blame] | 433 | if (C.getArgs().hasArg(options::OPT__version)) { |
Daniel Dunbar | e00f1cd | 2009-07-21 20:06:58 +0000 | [diff] [blame] | 434 | // Follow gcc behavior and use stdout for --version and stderr for -v |
| 435 | PrintVersion(C, llvm::outs()); |
Daniel Dunbar | 3b7c84c | 2009-04-02 15:05:41 +0000 | [diff] [blame] | 436 | return false; |
| 437 | } |
| 438 | |
Daniel Dunbar | 73f7b8c | 2009-03-18 02:55:38 +0000 | [diff] [blame] | 439 | if (C.getArgs().hasArg(options::OPT_v) || |
| 440 | C.getArgs().hasArg(options::OPT__HASH_HASH_HASH)) { |
Daniel Dunbar | e00f1cd | 2009-07-21 20:06:58 +0000 | [diff] [blame] | 441 | PrintVersion(C, llvm::errs()); |
Daniel Dunbar | cc00689 | 2009-03-13 00:51:18 +0000 | [diff] [blame] | 442 | SuppressMissingInputWarning = true; |
| 443 | } |
| 444 | |
Daniel Dunbar | 73f7b8c | 2009-03-18 02:55:38 +0000 | [diff] [blame] | 445 | const ToolChain &TC = C.getDefaultToolChain(); |
Daniel Dunbar | 91b9e20 | 2009-03-20 04:37:21 +0000 | [diff] [blame] | 446 | if (C.getArgs().hasArg(options::OPT_print_search_dirs)) { |
| 447 | llvm::outs() << "programs: ="; |
| 448 | for (ToolChain::path_list::const_iterator it = TC.getProgramPaths().begin(), |
| 449 | ie = TC.getProgramPaths().end(); it != ie; ++it) { |
| 450 | if (it != TC.getProgramPaths().begin()) |
| 451 | llvm::outs() << ':'; |
| 452 | llvm::outs() << *it; |
| 453 | } |
| 454 | llvm::outs() << "\n"; |
| 455 | llvm::outs() << "libraries: ="; |
| 456 | for (ToolChain::path_list::const_iterator it = TC.getFilePaths().begin(), |
| 457 | ie = TC.getFilePaths().end(); it != ie; ++it) { |
| 458 | if (it != TC.getFilePaths().begin()) |
| 459 | llvm::outs() << ':'; |
| 460 | llvm::outs() << *it; |
| 461 | } |
| 462 | llvm::outs() << "\n"; |
Daniel Dunbar | e627b68 | 2009-03-31 21:38:17 +0000 | [diff] [blame] | 463 | return false; |
Daniel Dunbar | 91b9e20 | 2009-03-20 04:37:21 +0000 | [diff] [blame] | 464 | } |
| 465 | |
Daniel Dunbar | cc00689 | 2009-03-13 00:51:18 +0000 | [diff] [blame] | 466 | // FIXME: The following handlers should use a callback mechanism, we |
| 467 | // don't know what the client would like to do. |
Daniel Dunbar | 73f7b8c | 2009-03-18 02:55:38 +0000 | [diff] [blame] | 468 | if (Arg *A = C.getArgs().getLastArg(options::OPT_print_file_name_EQ)) { |
| 469 | llvm::outs() << GetFilePath(A->getValue(C.getArgs()), TC).toString() |
| 470 | << "\n"; |
Daniel Dunbar | cc00689 | 2009-03-13 00:51:18 +0000 | [diff] [blame] | 471 | return false; |
| 472 | } |
| 473 | |
Daniel Dunbar | 73f7b8c | 2009-03-18 02:55:38 +0000 | [diff] [blame] | 474 | if (Arg *A = C.getArgs().getLastArg(options::OPT_print_prog_name_EQ)) { |
| 475 | llvm::outs() << GetProgramPath(A->getValue(C.getArgs()), TC).toString() |
| 476 | << "\n"; |
Daniel Dunbar | cc00689 | 2009-03-13 00:51:18 +0000 | [diff] [blame] | 477 | return false; |
| 478 | } |
| 479 | |
Daniel Dunbar | 73f7b8c | 2009-03-18 02:55:38 +0000 | [diff] [blame] | 480 | if (C.getArgs().hasArg(options::OPT_print_libgcc_file_name)) { |
Daniel Dunbar | ef68dd3 | 2009-03-27 14:26:33 +0000 | [diff] [blame] | 481 | llvm::outs() << GetFilePath("libgcc.a", TC).toString() << "\n"; |
Daniel Dunbar | cc00689 | 2009-03-13 00:51:18 +0000 | [diff] [blame] | 482 | return false; |
| 483 | } |
| 484 | |
Daniel Dunbar | 329aa99 | 2009-06-16 23:25:22 +0000 | [diff] [blame] | 485 | if (C.getArgs().hasArg(options::OPT_print_multi_lib)) { |
| 486 | // FIXME: We need tool chain support for this. |
| 487 | llvm::outs() << ".;\n"; |
| 488 | |
| 489 | switch (C.getDefaultToolChain().getTriple().getArch()) { |
| 490 | default: |
| 491 | break; |
| 492 | |
| 493 | case llvm::Triple::x86_64: |
| 494 | llvm::outs() << "x86_64;@m64" << "\n"; |
| 495 | break; |
| 496 | |
| 497 | case llvm::Triple::ppc64: |
| 498 | llvm::outs() << "ppc64;@m64" << "\n"; |
| 499 | break; |
| 500 | } |
| 501 | return false; |
| 502 | } |
| 503 | |
| 504 | // FIXME: What is the difference between print-multi-directory and |
| 505 | // print-multi-os-directory? |
| 506 | if (C.getArgs().hasArg(options::OPT_print_multi_directory) || |
| 507 | C.getArgs().hasArg(options::OPT_print_multi_os_directory)) { |
| 508 | switch (C.getDefaultToolChain().getTriple().getArch()) { |
| 509 | default: |
| 510 | case llvm::Triple::x86: |
| 511 | case llvm::Triple::ppc: |
| 512 | llvm::outs() << "." << "\n"; |
| 513 | break; |
| 514 | |
| 515 | case llvm::Triple::x86_64: |
| 516 | llvm::outs() << "x86_64" << "\n"; |
| 517 | break; |
| 518 | |
| 519 | case llvm::Triple::ppc64: |
| 520 | llvm::outs() << "ppc64" << "\n"; |
| 521 | break; |
| 522 | } |
| 523 | return false; |
| 524 | } |
| 525 | |
Daniel Dunbar | cc00689 | 2009-03-13 00:51:18 +0000 | [diff] [blame] | 526 | return true; |
| 527 | } |
| 528 | |
Daniel Dunbar | 2b856ce | 2009-03-18 03:13:20 +0000 | [diff] [blame] | 529 | static unsigned PrintActions1(const Compilation &C, |
Daniel Dunbar | 494646b | 2009-03-13 12:19:02 +0000 | [diff] [blame] | 530 | Action *A, |
| 531 | std::map<Action*, unsigned> &Ids) { |
| 532 | if (Ids.count(A)) |
| 533 | return Ids[A]; |
| 534 | |
| 535 | std::string str; |
| 536 | llvm::raw_string_ostream os(str); |
| 537 | |
| 538 | os << Action::getClassName(A->getKind()) << ", "; |
| 539 | if (InputAction *IA = dyn_cast<InputAction>(A)) { |
Daniel Dunbar | 2b856ce | 2009-03-18 03:13:20 +0000 | [diff] [blame] | 540 | os << "\"" << IA->getInputArg().getValue(C.getArgs()) << "\""; |
Daniel Dunbar | 494646b | 2009-03-13 12:19:02 +0000 | [diff] [blame] | 541 | } else if (BindArchAction *BIA = dyn_cast<BindArchAction>(A)) { |
Daniel Dunbar | 2b856ce | 2009-03-18 03:13:20 +0000 | [diff] [blame] | 542 | os << '"' << (BIA->getArchName() ? BIA->getArchName() : |
| 543 | C.getDefaultToolChain().getArchName()) << '"' |
| 544 | << ", {" << PrintActions1(C, *BIA->begin(), Ids) << "}"; |
Daniel Dunbar | 494646b | 2009-03-13 12:19:02 +0000 | [diff] [blame] | 545 | } else { |
| 546 | os << "{"; |
| 547 | for (Action::iterator it = A->begin(), ie = A->end(); it != ie;) { |
Daniel Dunbar | 2b856ce | 2009-03-18 03:13:20 +0000 | [diff] [blame] | 548 | os << PrintActions1(C, *it, Ids); |
Daniel Dunbar | 494646b | 2009-03-13 12:19:02 +0000 | [diff] [blame] | 549 | ++it; |
| 550 | if (it != ie) |
| 551 | os << ", "; |
| 552 | } |
| 553 | os << "}"; |
| 554 | } |
| 555 | |
| 556 | unsigned Id = Ids.size(); |
| 557 | Ids[A] = Id; |
Daniel Dunbar | 9dc28b8 | 2009-03-13 17:20:20 +0000 | [diff] [blame] | 558 | llvm::errs() << Id << ": " << os.str() << ", " |
Daniel Dunbar | 494646b | 2009-03-13 12:19:02 +0000 | [diff] [blame] | 559 | << types::getTypeName(A->getType()) << "\n"; |
| 560 | |
| 561 | return Id; |
| 562 | } |
| 563 | |
Daniel Dunbar | 2b856ce | 2009-03-18 03:13:20 +0000 | [diff] [blame] | 564 | void Driver::PrintActions(const Compilation &C) const { |
Daniel Dunbar | 494646b | 2009-03-13 12:19:02 +0000 | [diff] [blame] | 565 | std::map<Action*, unsigned> Ids; |
Daniel Dunbar | 2b856ce | 2009-03-18 03:13:20 +0000 | [diff] [blame] | 566 | for (ActionList::const_iterator it = C.getActions().begin(), |
| 567 | ie = C.getActions().end(); it != ie; ++it) |
| 568 | PrintActions1(C, *it, Ids); |
Daniel Dunbar | db62cc3 | 2009-03-12 07:58:46 +0000 | [diff] [blame] | 569 | } |
| 570 | |
Daniel Dunbar | 73f7b8c | 2009-03-18 02:55:38 +0000 | [diff] [blame] | 571 | void Driver::BuildUniversalActions(const ArgList &Args, |
| 572 | ActionList &Actions) const { |
Daniel Dunbar | 16e04ff | 2009-03-18 01:38:48 +0000 | [diff] [blame] | 573 | llvm::PrettyStackTraceString CrashInfo("Building actions for universal build"); |
Daniel Dunbar | b1873cd | 2009-03-13 20:33:35 +0000 | [diff] [blame] | 574 | // Collect the list of architectures. Duplicates are allowed, but |
| 575 | // should only be handled once (in the order seen). |
| 576 | llvm::StringSet<> ArchNames; |
| 577 | llvm::SmallVector<const char *, 4> Archs; |
Daniel Dunbar | fba157b | 2009-03-12 18:40:18 +0000 | [diff] [blame] | 578 | for (ArgList::const_iterator it = Args.begin(), ie = Args.end(); |
| 579 | it != ie; ++it) { |
| 580 | Arg *A = *it; |
| 581 | |
| 582 | if (A->getOption().getId() == options::OPT_arch) { |
Daniel Dunbar | b1873cd | 2009-03-13 20:33:35 +0000 | [diff] [blame] | 583 | const char *Name = A->getValue(Args); |
| 584 | |
Daniel Dunbar | fba157b | 2009-03-12 18:40:18 +0000 | [diff] [blame] | 585 | // FIXME: We need to handle canonicalization of the specified |
| 586 | // arch? |
| 587 | |
Daniel Dunbar | a345a44 | 2009-03-19 07:55:12 +0000 | [diff] [blame] | 588 | A->claim(); |
Daniel Dunbar | b1873cd | 2009-03-13 20:33:35 +0000 | [diff] [blame] | 589 | if (ArchNames.insert(Name)) |
| 590 | Archs.push_back(Name); |
Daniel Dunbar | fba157b | 2009-03-12 18:40:18 +0000 | [diff] [blame] | 591 | } |
| 592 | } |
| 593 | |
Daniel Dunbar | 2b856ce | 2009-03-18 03:13:20 +0000 | [diff] [blame] | 594 | // When there is no explicit arch for this platform, make sure we |
| 595 | // still bind the architecture (to the default) so that -Xarch_ is |
| 596 | // handled correctly. |
| 597 | if (!Archs.size()) |
| 598 | Archs.push_back(0); |
Daniel Dunbar | fba157b | 2009-03-12 18:40:18 +0000 | [diff] [blame] | 599 | |
| 600 | // FIXME: We killed off some others but these aren't yet detected in |
| 601 | // a functional manner. If we added information to jobs about which |
| 602 | // "auxiliary" files they wrote then we could detect the conflict |
| 603 | // these cause downstream. |
| 604 | if (Archs.size() > 1) { |
| 605 | // No recovery needed, the point of this is just to prevent |
| 606 | // overwriting the same files. |
Daniel Dunbar | fba157b | 2009-03-12 18:40:18 +0000 | [diff] [blame] | 607 | if (const Arg *A = Args.getLastArg(options::OPT_save_temps)) |
| 608 | Diag(clang::diag::err_drv_invalid_opt_with_multiple_archs) |
Daniel Dunbar | 7322593 | 2009-03-20 06:14:23 +0000 | [diff] [blame] | 609 | << A->getAsString(Args); |
Daniel Dunbar | fba157b | 2009-03-12 18:40:18 +0000 | [diff] [blame] | 610 | } |
| 611 | |
| 612 | ActionList SingleActions; |
| 613 | BuildActions(Args, SingleActions); |
| 614 | |
| 615 | // Add in arch binding and lipo (if necessary) for every top level |
| 616 | // action. |
| 617 | for (unsigned i = 0, e = SingleActions.size(); i != e; ++i) { |
| 618 | Action *Act = SingleActions[i]; |
| 619 | |
| 620 | // Make sure we can lipo this kind of output. If not (and it is an |
| 621 | // actual output) then we disallow, since we can't create an |
| 622 | // output file with the right name without overwriting it. We |
| 623 | // could remove this oddity by just changing the output names to |
| 624 | // include the arch, which would also fix |
| 625 | // -save-temps. Compatibility wins for now. |
| 626 | |
Daniel Dunbar | dd863aa | 2009-03-13 17:46:02 +0000 | [diff] [blame] | 627 | if (Archs.size() > 1 && !types::canLipoType(Act->getType())) |
Daniel Dunbar | fba157b | 2009-03-12 18:40:18 +0000 | [diff] [blame] | 628 | Diag(clang::diag::err_drv_invalid_output_with_multiple_archs) |
| 629 | << types::getTypeName(Act->getType()); |
| 630 | |
| 631 | ActionList Inputs; |
Daniel Dunbar | a345a44 | 2009-03-19 07:55:12 +0000 | [diff] [blame] | 632 | for (unsigned i = 0, e = Archs.size(); i != e; ++i) |
Daniel Dunbar | b1873cd | 2009-03-13 20:33:35 +0000 | [diff] [blame] | 633 | Inputs.push_back(new BindArchAction(Act, Archs[i])); |
Daniel Dunbar | fba157b | 2009-03-12 18:40:18 +0000 | [diff] [blame] | 634 | |
| 635 | // Lipo if necessary, We do it this way because we need to set the |
| 636 | // arch flag so that -Xarch_ gets overwritten. |
| 637 | if (Inputs.size() == 1 || Act->getType() == types::TY_Nothing) |
| 638 | Actions.append(Inputs.begin(), Inputs.end()); |
| 639 | else |
| 640 | Actions.push_back(new LipoJobAction(Inputs, Act->getType())); |
| 641 | } |
Daniel Dunbar | db62cc3 | 2009-03-12 07:58:46 +0000 | [diff] [blame] | 642 | } |
| 643 | |
Daniel Dunbar | 73f7b8c | 2009-03-18 02:55:38 +0000 | [diff] [blame] | 644 | void Driver::BuildActions(const ArgList &Args, ActionList &Actions) const { |
Daniel Dunbar | 16e04ff | 2009-03-18 01:38:48 +0000 | [diff] [blame] | 645 | llvm::PrettyStackTraceString CrashInfo("Building compilation actions"); |
Daniel Dunbar | 207a56d | 2009-03-12 23:55:14 +0000 | [diff] [blame] | 646 | // Start by constructing the list of inputs and their types. |
| 647 | |
Daniel Dunbar | 5cb75d6 | 2009-03-13 17:57:10 +0000 | [diff] [blame] | 648 | // Track the current user specified (-x) input. We also explicitly |
| 649 | // track the argument used to set the type; we only want to claim |
| 650 | // the type when we actually use it, so we warn about unused -x |
| 651 | // arguments. |
| 652 | types::ID InputType = types::TY_Nothing; |
| 653 | Arg *InputTypeArg = 0; |
| 654 | |
Daniel Dunbar | db62cc3 | 2009-03-12 07:58:46 +0000 | [diff] [blame] | 655 | llvm::SmallVector<std::pair<types::ID, const Arg*>, 16> Inputs; |
| 656 | for (ArgList::const_iterator it = Args.begin(), ie = Args.end(); |
| 657 | it != ie; ++it) { |
| 658 | Arg *A = *it; |
| 659 | |
| 660 | if (isa<InputOption>(A->getOption())) { |
| 661 | const char *Value = A->getValue(Args); |
| 662 | types::ID Ty = types::TY_INVALID; |
| 663 | |
| 664 | // Infer the input type if necessary. |
Daniel Dunbar | 5cb75d6 | 2009-03-13 17:57:10 +0000 | [diff] [blame] | 665 | if (InputType == types::TY_Nothing) { |
| 666 | // If there was an explicit arg for this, claim it. |
| 667 | if (InputTypeArg) |
| 668 | InputTypeArg->claim(); |
| 669 | |
Daniel Dunbar | db62cc3 | 2009-03-12 07:58:46 +0000 | [diff] [blame] | 670 | // stdin must be handled specially. |
| 671 | if (memcmp(Value, "-", 2) == 0) { |
| 672 | // If running with -E, treat as a C input (this changes the |
| 673 | // builtin macros, for example). This may be overridden by |
| 674 | // -ObjC below. |
| 675 | // |
| 676 | // Otherwise emit an error but still use a valid type to |
| 677 | // avoid spurious errors (e.g., no inputs). |
Daniel Dunbar | e9c70fa | 2009-03-15 00:48:16 +0000 | [diff] [blame] | 678 | if (!Args.hasArg(options::OPT_E, false)) |
Daniel Dunbar | d724e33 | 2009-03-12 09:13:48 +0000 | [diff] [blame] | 679 | Diag(clang::diag::err_drv_unknown_stdin_type); |
Daniel Dunbar | db62cc3 | 2009-03-12 07:58:46 +0000 | [diff] [blame] | 680 | Ty = types::TY_C; |
| 681 | } else { |
| 682 | // Otherwise lookup by extension, and fallback to ObjectType |
Daniel Dunbar | 7b6dfbd | 2009-03-20 23:39:23 +0000 | [diff] [blame] | 683 | // if not found. We use a host hook here because Darwin at |
| 684 | // least has its own idea of what .s is. |
Daniel Dunbar | db62cc3 | 2009-03-12 07:58:46 +0000 | [diff] [blame] | 685 | if (const char *Ext = strrchr(Value, '.')) |
Daniel Dunbar | 7b6dfbd | 2009-03-20 23:39:23 +0000 | [diff] [blame] | 686 | Ty = Host->lookupTypeForExtension(Ext + 1); |
| 687 | |
Daniel Dunbar | db62cc3 | 2009-03-12 07:58:46 +0000 | [diff] [blame] | 688 | if (Ty == types::TY_INVALID) |
| 689 | Ty = types::TY_Object; |
| 690 | } |
| 691 | |
Daniel Dunbar | 84266db | 2009-05-18 21:47:54 +0000 | [diff] [blame] | 692 | // -ObjC and -ObjC++ override the default language, but only for "source |
| 693 | // files". We just treat everything that isn't a linker input as a |
| 694 | // source file. |
Daniel Dunbar | db62cc3 | 2009-03-12 07:58:46 +0000 | [diff] [blame] | 695 | // |
Daniel Dunbar | 84266db | 2009-05-18 21:47:54 +0000 | [diff] [blame] | 696 | // FIXME: Clean this up if we move the phase sequence into the type. |
Daniel Dunbar | db62cc3 | 2009-03-12 07:58:46 +0000 | [diff] [blame] | 697 | if (Ty != types::TY_Object) { |
| 698 | if (Args.hasArg(options::OPT_ObjC)) |
| 699 | Ty = types::TY_ObjC; |
| 700 | else if (Args.hasArg(options::OPT_ObjCXX)) |
| 701 | Ty = types::TY_ObjCXX; |
| 702 | } |
| 703 | } else { |
| 704 | assert(InputTypeArg && "InputType set w/o InputTypeArg"); |
| 705 | InputTypeArg->claim(); |
| 706 | Ty = InputType; |
| 707 | } |
| 708 | |
| 709 | // Check that the file exists. It isn't clear this is worth |
| 710 | // doing, since the tool presumably does this anyway, and this |
| 711 | // just adds an extra stat to the equation, but this is gcc |
| 712 | // compatible. |
| 713 | if (memcmp(Value, "-", 2) != 0 && !llvm::sys::Path(Value).exists()) |
Daniel Dunbar | d724e33 | 2009-03-12 09:13:48 +0000 | [diff] [blame] | 714 | Diag(clang::diag::err_drv_no_such_file) << A->getValue(Args); |
Daniel Dunbar | db62cc3 | 2009-03-12 07:58:46 +0000 | [diff] [blame] | 715 | else |
| 716 | Inputs.push_back(std::make_pair(Ty, A)); |
| 717 | |
| 718 | } else if (A->getOption().isLinkerInput()) { |
| 719 | // Just treat as object type, we could make a special type for |
| 720 | // this if necessary. |
| 721 | Inputs.push_back(std::make_pair(types::TY_Object, A)); |
| 722 | |
| 723 | } else if (A->getOption().getId() == options::OPT_x) { |
| 724 | InputTypeArg = A; |
| 725 | InputType = types::lookupTypeForTypeSpecifier(A->getValue(Args)); |
| 726 | |
| 727 | // Follow gcc behavior and treat as linker input for invalid -x |
| 728 | // options. Its not clear why we shouldn't just revert to |
| 729 | // unknown; but this isn't very important, we might as well be |
| 730 | // bug comatible. |
| 731 | if (!InputType) { |
Daniel Dunbar | d724e33 | 2009-03-12 09:13:48 +0000 | [diff] [blame] | 732 | Diag(clang::diag::err_drv_unknown_language) << A->getValue(Args); |
Daniel Dunbar | db62cc3 | 2009-03-12 07:58:46 +0000 | [diff] [blame] | 733 | InputType = types::TY_Object; |
| 734 | } |
| 735 | } |
| 736 | } |
| 737 | |
Daniel Dunbar | 5a5ec5c | 2009-03-13 00:17:48 +0000 | [diff] [blame] | 738 | if (!SuppressMissingInputWarning && Inputs.empty()) { |
Daniel Dunbar | 207a56d | 2009-03-12 23:55:14 +0000 | [diff] [blame] | 739 | Diag(clang::diag::err_drv_no_input_files); |
| 740 | return; |
| 741 | } |
| 742 | |
| 743 | // Determine which compilation mode we are in. We look for options |
| 744 | // which affect the phase, starting with the earliest phases, and |
| 745 | // record which option we used to determine the final phase. |
Daniel Dunbar | 85cb359 | 2009-03-13 11:38:42 +0000 | [diff] [blame] | 746 | Arg *FinalPhaseArg = 0; |
| 747 | phases::ID FinalPhase; |
Daniel Dunbar | 207a56d | 2009-03-12 23:55:14 +0000 | [diff] [blame] | 748 | |
| 749 | // -{E,M,MM} only run the preprocessor. |
Daniel Dunbar | 85cb359 | 2009-03-13 11:38:42 +0000 | [diff] [blame] | 750 | if ((FinalPhaseArg = Args.getLastArg(options::OPT_E)) || |
| 751 | (FinalPhaseArg = Args.getLastArg(options::OPT_M)) || |
| 752 | (FinalPhaseArg = Args.getLastArg(options::OPT_MM))) { |
| 753 | FinalPhase = phases::Preprocess; |
Daniel Dunbar | 207a56d | 2009-03-12 23:55:14 +0000 | [diff] [blame] | 754 | |
Daniel Dunbar | e9c70fa | 2009-03-15 00:48:16 +0000 | [diff] [blame] | 755 | // -{fsyntax-only,-analyze,emit-llvm,S} only run up to the compiler. |
| 756 | } else if ((FinalPhaseArg = Args.getLastArg(options::OPT_fsyntax_only)) || |
Daniel Dunbar | 6e3de3d | 2009-05-06 02:12:32 +0000 | [diff] [blame] | 757 | (FinalPhaseArg = Args.getLastArg(options::OPT__analyze, |
| 758 | options::OPT__analyze_auto)) || |
Daniel Dunbar | 85cb359 | 2009-03-13 11:38:42 +0000 | [diff] [blame] | 759 | (FinalPhaseArg = Args.getLastArg(options::OPT_S))) { |
| 760 | FinalPhase = phases::Compile; |
Daniel Dunbar | 207a56d | 2009-03-12 23:55:14 +0000 | [diff] [blame] | 761 | |
| 762 | // -c only runs up to the assembler. |
Daniel Dunbar | 85cb359 | 2009-03-13 11:38:42 +0000 | [diff] [blame] | 763 | } else if ((FinalPhaseArg = Args.getLastArg(options::OPT_c))) { |
| 764 | FinalPhase = phases::Assemble; |
Daniel Dunbar | 207a56d | 2009-03-12 23:55:14 +0000 | [diff] [blame] | 765 | |
| 766 | // Otherwise do everything. |
| 767 | } else |
Daniel Dunbar | 85cb359 | 2009-03-13 11:38:42 +0000 | [diff] [blame] | 768 | FinalPhase = phases::Link; |
Daniel Dunbar | 207a56d | 2009-03-12 23:55:14 +0000 | [diff] [blame] | 769 | |
Daniel Dunbar | 207a56d | 2009-03-12 23:55:14 +0000 | [diff] [blame] | 770 | // Reject -Z* at the top level, these options should never have been |
| 771 | // exposed by gcc. |
Daniel Dunbar | 55e34e3 | 2009-03-26 16:12:09 +0000 | [diff] [blame] | 772 | if (Arg *A = Args.getLastArg(options::OPT_Z_Joined)) |
Daniel Dunbar | 7322593 | 2009-03-20 06:14:23 +0000 | [diff] [blame] | 773 | Diag(clang::diag::err_drv_use_of_Z_option) << A->getAsString(Args); |
Daniel Dunbar | 207a56d | 2009-03-12 23:55:14 +0000 | [diff] [blame] | 774 | |
Daniel Dunbar | 85cb359 | 2009-03-13 11:38:42 +0000 | [diff] [blame] | 775 | // Construct the actions to perform. |
| 776 | ActionList LinkerInputs; |
Daniel Dunbar | db62cc3 | 2009-03-12 07:58:46 +0000 | [diff] [blame] | 777 | for (unsigned i = 0, e = Inputs.size(); i != e; ++i) { |
Daniel Dunbar | 85cb359 | 2009-03-13 11:38:42 +0000 | [diff] [blame] | 778 | types::ID InputType = Inputs[i].first; |
| 779 | const Arg *InputArg = Inputs[i].second; |
| 780 | |
| 781 | unsigned NumSteps = types::getNumCompilationPhases(InputType); |
| 782 | assert(NumSteps && "Invalid number of steps!"); |
| 783 | |
| 784 | // If the first step comes after the final phase we are doing as |
| 785 | // part of this compilation, warn the user about it. |
| 786 | phases::ID InitialPhase = types::getCompilationPhase(InputType, 0); |
| 787 | if (InitialPhase > FinalPhase) { |
Daniel Dunbar | 923e703 | 2009-03-19 07:57:08 +0000 | [diff] [blame] | 788 | // Claim here to avoid the more general unused warning. |
| 789 | InputArg->claim(); |
Daniel Dunbar | 85cb359 | 2009-03-13 11:38:42 +0000 | [diff] [blame] | 790 | Diag(clang::diag::warn_drv_input_file_unused) |
Daniel Dunbar | 7322593 | 2009-03-20 06:14:23 +0000 | [diff] [blame] | 791 | << InputArg->getAsString(Args) |
Daniel Dunbar | 85cb359 | 2009-03-13 11:38:42 +0000 | [diff] [blame] | 792 | << getPhaseName(InitialPhase) |
| 793 | << FinalPhaseArg->getOption().getName(); |
| 794 | continue; |
| 795 | } |
| 796 | |
| 797 | // Build the pipeline for this file. |
| 798 | Action *Current = new InputAction(*InputArg, InputType); |
| 799 | for (unsigned i = 0; i != NumSteps; ++i) { |
| 800 | phases::ID Phase = types::getCompilationPhase(InputType, i); |
| 801 | |
| 802 | // We are done if this step is past what the user requested. |
| 803 | if (Phase > FinalPhase) |
| 804 | break; |
| 805 | |
| 806 | // Queue linker inputs. |
| 807 | if (Phase == phases::Link) { |
| 808 | assert(i + 1 == NumSteps && "linking must be final compilation step."); |
| 809 | LinkerInputs.push_back(Current); |
| 810 | Current = 0; |
| 811 | break; |
| 812 | } |
| 813 | |
Daniel Dunbar | dec1445 | 2009-03-24 20:17:30 +0000 | [diff] [blame] | 814 | // Some types skip the assembler phase (e.g., llvm-bc), but we |
| 815 | // can't encode this in the steps because the intermediate type |
| 816 | // depends on arguments. Just special case here. |
| 817 | if (Phase == phases::Assemble && Current->getType() != types::TY_PP_Asm) |
| 818 | continue; |
| 819 | |
Daniel Dunbar | 85cb359 | 2009-03-13 11:38:42 +0000 | [diff] [blame] | 820 | // Otherwise construct the appropriate action. |
| 821 | Current = ConstructPhaseAction(Args, Phase, Current); |
| 822 | if (Current->getType() == types::TY_Nothing) |
| 823 | break; |
| 824 | } |
| 825 | |
| 826 | // If we ended with something, add to the output list. |
| 827 | if (Current) |
| 828 | Actions.push_back(Current); |
Daniel Dunbar | db62cc3 | 2009-03-12 07:58:46 +0000 | [diff] [blame] | 829 | } |
Daniel Dunbar | 85cb359 | 2009-03-13 11:38:42 +0000 | [diff] [blame] | 830 | |
| 831 | // Add a link action if necessary. |
| 832 | if (!LinkerInputs.empty()) |
| 833 | Actions.push_back(new LinkJobAction(LinkerInputs, types::TY_Image)); |
| 834 | } |
| 835 | |
| 836 | Action *Driver::ConstructPhaseAction(const ArgList &Args, phases::ID Phase, |
| 837 | Action *Input) const { |
Daniel Dunbar | 16e04ff | 2009-03-18 01:38:48 +0000 | [diff] [blame] | 838 | llvm::PrettyStackTraceString CrashInfo("Constructing phase actions"); |
Daniel Dunbar | 85cb359 | 2009-03-13 11:38:42 +0000 | [diff] [blame] | 839 | // Build the appropriate action. |
| 840 | switch (Phase) { |
| 841 | case phases::Link: assert(0 && "link action invalid here."); |
| 842 | case phases::Preprocess: { |
Daniel Dunbar | 049b724 | 2009-03-30 06:36:42 +0000 | [diff] [blame] | 843 | types::ID OutputTy; |
| 844 | // -{M, MM} alter the output type. |
| 845 | if (Args.hasArg(options::OPT_M) || Args.hasArg(options::OPT_MM)) { |
| 846 | OutputTy = types::TY_Dependencies; |
| 847 | } else { |
| 848 | OutputTy = types::getPreprocessedType(Input->getType()); |
| 849 | assert(OutputTy != types::TY_INVALID && |
| 850 | "Cannot preprocess this input type!"); |
| 851 | } |
Daniel Dunbar | 85cb359 | 2009-03-13 11:38:42 +0000 | [diff] [blame] | 852 | return new PreprocessJobAction(Input, OutputTy); |
| 853 | } |
| 854 | case phases::Precompile: |
| 855 | return new PrecompileJobAction(Input, types::TY_PCH); |
| 856 | case phases::Compile: { |
| 857 | if (Args.hasArg(options::OPT_fsyntax_only)) { |
| 858 | return new CompileJobAction(Input, types::TY_Nothing); |
Daniel Dunbar | 6e3de3d | 2009-05-06 02:12:32 +0000 | [diff] [blame] | 859 | } else if (Args.hasArg(options::OPT__analyze, options::OPT__analyze_auto)) { |
Daniel Dunbar | 85cb359 | 2009-03-13 11:38:42 +0000 | [diff] [blame] | 860 | return new AnalyzeJobAction(Input, types::TY_Plist); |
Daniel Dunbar | dec1445 | 2009-03-24 20:17:30 +0000 | [diff] [blame] | 861 | } else if (Args.hasArg(options::OPT_emit_llvm) || |
| 862 | Args.hasArg(options::OPT_flto) || |
| 863 | Args.hasArg(options::OPT_O4)) { |
Daniel Dunbar | 85cb359 | 2009-03-13 11:38:42 +0000 | [diff] [blame] | 864 | types::ID Output = |
| 865 | Args.hasArg(options::OPT_S) ? types::TY_LLVMAsm : types::TY_LLVMBC; |
| 866 | return new CompileJobAction(Input, Output); |
| 867 | } else { |
| 868 | return new CompileJobAction(Input, types::TY_PP_Asm); |
| 869 | } |
| 870 | } |
| 871 | case phases::Assemble: |
| 872 | return new AssembleJobAction(Input, types::TY_Object); |
| 873 | } |
| 874 | |
| 875 | assert(0 && "invalid phase in ConstructPhaseAction"); |
| 876 | return 0; |
Daniel Dunbar | db62cc3 | 2009-03-12 07:58:46 +0000 | [diff] [blame] | 877 | } |
| 878 | |
Daniel Dunbar | 73f7b8c | 2009-03-18 02:55:38 +0000 | [diff] [blame] | 879 | void Driver::BuildJobs(Compilation &C) const { |
Daniel Dunbar | 16e04ff | 2009-03-18 01:38:48 +0000 | [diff] [blame] | 880 | llvm::PrettyStackTraceString CrashInfo("Building compilation jobs"); |
Daniel Dunbar | 7ce6add | 2009-03-16 06:56:51 +0000 | [diff] [blame] | 881 | bool SaveTemps = C.getArgs().hasArg(options::OPT_save_temps); |
| 882 | bool UsePipes = C.getArgs().hasArg(options::OPT_pipe); |
Daniel Dunbar | bac6b64 | 2009-03-18 23:18:19 +0000 | [diff] [blame] | 883 | |
| 884 | // FIXME: Pipes are forcibly disabled until we support executing |
| 885 | // them. |
| 886 | if (!CCCPrintBindings) |
| 887 | UsePipes = false; |
Daniel Dunbar | 7ce6add | 2009-03-16 06:56:51 +0000 | [diff] [blame] | 888 | |
| 889 | // -save-temps inhibits pipes. |
| 890 | if (SaveTemps && UsePipes) { |
| 891 | Diag(clang::diag::warn_drv_pipe_ignored_with_save_temps); |
| 892 | UsePipes = true; |
| 893 | } |
| 894 | |
| 895 | Arg *FinalOutput = C.getArgs().getLastArg(options::OPT_o); |
| 896 | |
| 897 | // It is an error to provide a -o option if we are making multiple |
| 898 | // output files. |
| 899 | if (FinalOutput) { |
| 900 | unsigned NumOutputs = 0; |
Daniel Dunbar | 73f7b8c | 2009-03-18 02:55:38 +0000 | [diff] [blame] | 901 | for (ActionList::const_iterator it = C.getActions().begin(), |
| 902 | ie = C.getActions().end(); it != ie; ++it) |
Daniel Dunbar | 7ce6add | 2009-03-16 06:56:51 +0000 | [diff] [blame] | 903 | if ((*it)->getType() != types::TY_Nothing) |
| 904 | ++NumOutputs; |
| 905 | |
| 906 | if (NumOutputs > 1) { |
| 907 | Diag(clang::diag::err_drv_output_argument_with_multiple_files); |
| 908 | FinalOutput = 0; |
| 909 | } |
| 910 | } |
| 911 | |
Daniel Dunbar | 73f7b8c | 2009-03-18 02:55:38 +0000 | [diff] [blame] | 912 | for (ActionList::const_iterator it = C.getActions().begin(), |
| 913 | ie = C.getActions().end(); it != ie; ++it) { |
Daniel Dunbar | 7ce6add | 2009-03-16 06:56:51 +0000 | [diff] [blame] | 914 | Action *A = *it; |
| 915 | |
| 916 | // If we are linking an image for multiple archs then the linker |
| 917 | // wants -arch_multiple and -final_output <final image |
| 918 | // name>. Unfortunately, this doesn't fit in cleanly because we |
| 919 | // have to pass this information down. |
| 920 | // |
| 921 | // FIXME: This is a hack; find a cleaner way to integrate this |
| 922 | // into the process. |
| 923 | const char *LinkingOutput = 0; |
Daniel Dunbar | 55e34e3 | 2009-03-26 16:12:09 +0000 | [diff] [blame] | 924 | if (isa<LipoJobAction>(A)) { |
Daniel Dunbar | 7ce6add | 2009-03-16 06:56:51 +0000 | [diff] [blame] | 925 | if (FinalOutput) |
| 926 | LinkingOutput = FinalOutput->getValue(C.getArgs()); |
| 927 | else |
| 928 | LinkingOutput = DefaultImageName.c_str(); |
| 929 | } |
| 930 | |
| 931 | InputInfo II; |
Daniel Dunbar | 2b856ce | 2009-03-18 03:13:20 +0000 | [diff] [blame] | 932 | BuildJobsForAction(C, A, &C.getDefaultToolChain(), |
Daniel Dunbar | 7ce6add | 2009-03-16 06:56:51 +0000 | [diff] [blame] | 933 | /*CanAcceptPipe*/ true, |
| 934 | /*AtTopLevel*/ true, |
| 935 | /*LinkingOutput*/ LinkingOutput, |
| 936 | II); |
| 937 | } |
Daniel Dunbar | 9d625e1 | 2009-03-16 06:42:30 +0000 | [diff] [blame] | 938 | |
Daniel Dunbar | 025de20 | 2009-04-03 22:09:23 +0000 | [diff] [blame] | 939 | // If the user passed -Qunused-arguments or there were errors, don't |
| 940 | // warn about any unused arguments. |
Daniel Dunbar | 1cfd891 | 2009-04-07 19:04:18 +0000 | [diff] [blame] | 941 | if (Diags.getNumErrors() || |
| 942 | C.getArgs().hasArg(options::OPT_Qunused_arguments)) |
Daniel Dunbar | 46c7082 | 2009-03-18 18:03:46 +0000 | [diff] [blame] | 943 | return; |
| 944 | |
Daniel Dunbar | 1e6f9ff | 2009-03-29 22:24:54 +0000 | [diff] [blame] | 945 | // Claim -### here. |
| 946 | (void) C.getArgs().hasArg(options::OPT__HASH_HASH_HASH); |
| 947 | |
Daniel Dunbar | 9d625e1 | 2009-03-16 06:42:30 +0000 | [diff] [blame] | 948 | for (ArgList::const_iterator it = C.getArgs().begin(), ie = C.getArgs().end(); |
| 949 | it != ie; ++it) { |
| 950 | Arg *A = *it; |
Daniel Dunbar | 46c7082 | 2009-03-18 18:03:46 +0000 | [diff] [blame] | 951 | |
Daniel Dunbar | 9d625e1 | 2009-03-16 06:42:30 +0000 | [diff] [blame] | 952 | // FIXME: It would be nice to be able to send the argument to the |
| 953 | // Diagnostic, so that extra values, position, and so on could be |
| 954 | // printed. |
Daniel Dunbar | 6c6e3f7 | 2009-04-04 00:52:26 +0000 | [diff] [blame] | 955 | if (!A->isClaimed()) { |
Daniel Dunbar | 1cfd891 | 2009-04-07 19:04:18 +0000 | [diff] [blame] | 956 | if (A->getOption().hasNoArgumentUnused()) |
| 957 | continue; |
| 958 | |
Daniel Dunbar | 6c6e3f7 | 2009-04-04 00:52:26 +0000 | [diff] [blame] | 959 | // Suppress the warning automatically if this is just a flag, |
| 960 | // and it is an instance of an argument we already claimed. |
| 961 | const Option &Opt = A->getOption(); |
| 962 | if (isa<FlagOption>(Opt)) { |
| 963 | bool DuplicateClaimed = false; |
| 964 | |
| 965 | // FIXME: Use iterator. |
| 966 | for (ArgList::const_iterator it = C.getArgs().begin(), |
| 967 | ie = C.getArgs().end(); it != ie; ++it) { |
| 968 | if ((*it)->isClaimed() && (*it)->getOption().matches(Opt.getId())) { |
| 969 | DuplicateClaimed = true; |
| 970 | break; |
| 971 | } |
| 972 | } |
| 973 | |
| 974 | if (DuplicateClaimed) |
| 975 | continue; |
| 976 | } |
| 977 | |
Daniel Dunbar | 9d625e1 | 2009-03-16 06:42:30 +0000 | [diff] [blame] | 978 | Diag(clang::diag::warn_drv_unused_argument) |
Daniel Dunbar | 7322593 | 2009-03-20 06:14:23 +0000 | [diff] [blame] | 979 | << A->getAsString(C.getArgs()); |
Daniel Dunbar | 6c6e3f7 | 2009-04-04 00:52:26 +0000 | [diff] [blame] | 980 | } |
Daniel Dunbar | 9d625e1 | 2009-03-16 06:42:30 +0000 | [diff] [blame] | 981 | } |
Daniel Dunbar | 47d762e | 2009-03-13 22:12:33 +0000 | [diff] [blame] | 982 | } |
| 983 | |
Daniel Dunbar | 7ce6add | 2009-03-16 06:56:51 +0000 | [diff] [blame] | 984 | void Driver::BuildJobsForAction(Compilation &C, |
| 985 | const Action *A, |
| 986 | const ToolChain *TC, |
| 987 | bool CanAcceptPipe, |
| 988 | bool AtTopLevel, |
| 989 | const char *LinkingOutput, |
| 990 | InputInfo &Result) const { |
Daniel Dunbar | 16e04ff | 2009-03-18 01:38:48 +0000 | [diff] [blame] | 991 | llvm::PrettyStackTraceString CrashInfo("Building compilation jobs for action"); |
Daniel Dunbar | bac6b64 | 2009-03-18 23:18:19 +0000 | [diff] [blame] | 992 | |
| 993 | bool UsePipes = C.getArgs().hasArg(options::OPT_pipe); |
| 994 | // FIXME: Pipes are forcibly disabled until we support executing |
| 995 | // them. |
| 996 | if (!CCCPrintBindings) |
| 997 | UsePipes = false; |
| 998 | |
Daniel Dunbar | 7ce6add | 2009-03-16 06:56:51 +0000 | [diff] [blame] | 999 | if (const InputAction *IA = dyn_cast<InputAction>(A)) { |
Daniel Dunbar | c3be67b | 2009-03-19 07:29:38 +0000 | [diff] [blame] | 1000 | // FIXME: It would be nice to not claim this here; maybe the old |
| 1001 | // scheme of just using Args was better? |
| 1002 | const Arg &Input = IA->getInputArg(); |
| 1003 | Input.claim(); |
| 1004 | if (isa<PositionalArg>(Input)) { |
| 1005 | const char *Name = Input.getValue(C.getArgs()); |
| 1006 | Result = InputInfo(Name, A->getType(), Name); |
| 1007 | } else |
| 1008 | Result = InputInfo(&Input, A->getType(), ""); |
Daniel Dunbar | 7ce6add | 2009-03-16 06:56:51 +0000 | [diff] [blame] | 1009 | return; |
| 1010 | } |
| 1011 | |
| 1012 | if (const BindArchAction *BAA = dyn_cast<BindArchAction>(A)) { |
| 1013 | const char *ArchName = BAA->getArchName(); |
Daniel Dunbar | 0830365 | 2009-05-22 02:53:45 +0000 | [diff] [blame] | 1014 | std::string Arch; |
| 1015 | if (!ArchName) { |
| 1016 | Arch = C.getDefaultToolChain().getArchName(); |
| 1017 | ArchName = Arch.c_str(); |
| 1018 | } |
Daniel Dunbar | 7ce6add | 2009-03-16 06:56:51 +0000 | [diff] [blame] | 1019 | BuildJobsForAction(C, |
| 1020 | *BAA->begin(), |
| 1021 | Host->getToolChain(C.getArgs(), ArchName), |
| 1022 | CanAcceptPipe, |
| 1023 | AtTopLevel, |
| 1024 | LinkingOutput, |
| 1025 | Result); |
| 1026 | return; |
| 1027 | } |
| 1028 | |
| 1029 | const JobAction *JA = cast<JobAction>(A); |
| 1030 | const Tool &T = TC->SelectTool(C, *JA); |
| 1031 | |
| 1032 | // See if we should use an integrated preprocessor. We do so when we |
| 1033 | // have exactly one input, since this is the only use case we care |
| 1034 | // about (irrelevant since we don't support combine yet). |
| 1035 | bool UseIntegratedCPP = false; |
| 1036 | const ActionList *Inputs = &A->getInputs(); |
| 1037 | if (Inputs->size() == 1 && isa<PreprocessJobAction>(*Inputs->begin())) { |
| 1038 | if (!C.getArgs().hasArg(options::OPT_no_integrated_cpp) && |
| 1039 | !C.getArgs().hasArg(options::OPT_traditional_cpp) && |
| 1040 | !C.getArgs().hasArg(options::OPT_save_temps) && |
| 1041 | T.hasIntegratedCPP()) { |
| 1042 | UseIntegratedCPP = true; |
| 1043 | Inputs = &(*Inputs)[0]->getInputs(); |
| 1044 | } |
| 1045 | } |
| 1046 | |
| 1047 | // Only use pipes when there is exactly one input. |
| 1048 | bool TryToUsePipeInput = Inputs->size() == 1 && T.acceptsPipedInput(); |
Daniel Dunbar | 00fd379 | 2009-03-18 06:00:36 +0000 | [diff] [blame] | 1049 | InputInfoList InputInfos; |
Daniel Dunbar | 7ce6add | 2009-03-16 06:56:51 +0000 | [diff] [blame] | 1050 | for (ActionList::const_iterator it = Inputs->begin(), ie = Inputs->end(); |
| 1051 | it != ie; ++it) { |
| 1052 | InputInfo II; |
| 1053 | BuildJobsForAction(C, *it, TC, TryToUsePipeInput, |
| 1054 | /*AtTopLevel*/false, |
| 1055 | LinkingOutput, |
| 1056 | II); |
| 1057 | InputInfos.push_back(II); |
| 1058 | } |
| 1059 | |
| 1060 | // Determine if we should output to a pipe. |
| 1061 | bool OutputToPipe = false; |
| 1062 | if (CanAcceptPipe && T.canPipeOutput()) { |
| 1063 | // Some actions default to writing to a pipe if they are the top |
| 1064 | // level phase and there was no user override. |
| 1065 | // |
| 1066 | // FIXME: Is there a better way to handle this? |
| 1067 | if (AtTopLevel) { |
| 1068 | if (isa<PreprocessJobAction>(A) && !C.getArgs().hasArg(options::OPT_o)) |
| 1069 | OutputToPipe = true; |
Daniel Dunbar | bac6b64 | 2009-03-18 23:18:19 +0000 | [diff] [blame] | 1070 | } else if (UsePipes) |
Daniel Dunbar | 7ce6add | 2009-03-16 06:56:51 +0000 | [diff] [blame] | 1071 | OutputToPipe = true; |
| 1072 | } |
| 1073 | |
| 1074 | // Figure out where to put the job (pipes). |
| 1075 | Job *Dest = &C.getJobs(); |
| 1076 | if (InputInfos[0].isPipe()) { |
Daniel Dunbar | 01fb26a | 2009-03-17 17:53:55 +0000 | [diff] [blame] | 1077 | assert(TryToUsePipeInput && "Unrequested pipe!"); |
Daniel Dunbar | 7ce6add | 2009-03-16 06:56:51 +0000 | [diff] [blame] | 1078 | assert(InputInfos.size() == 1 && "Unexpected pipe with multiple inputs."); |
| 1079 | Dest = &InputInfos[0].getPipe(); |
| 1080 | } |
| 1081 | |
| 1082 | // Always use the first input as the base input. |
| 1083 | const char *BaseInput = InputInfos[0].getBaseInput(); |
Daniel Dunbar | 01fb26a | 2009-03-17 17:53:55 +0000 | [diff] [blame] | 1084 | |
| 1085 | // Determine the place to write output to (nothing, pipe, or |
| 1086 | // filename) and where to put the new job. |
Daniel Dunbar | 01fb26a | 2009-03-17 17:53:55 +0000 | [diff] [blame] | 1087 | if (JA->getType() == types::TY_Nothing) { |
Daniel Dunbar | 07c9a1d | 2009-03-17 22:47:06 +0000 | [diff] [blame] | 1088 | Result = InputInfo(A->getType(), BaseInput); |
Daniel Dunbar | 01fb26a | 2009-03-17 17:53:55 +0000 | [diff] [blame] | 1089 | } else if (OutputToPipe) { |
| 1090 | // Append to current piped job or create a new one as appropriate. |
Daniel Dunbar | 07c9a1d | 2009-03-17 22:47:06 +0000 | [diff] [blame] | 1091 | PipedJob *PJ = dyn_cast<PipedJob>(Dest); |
| 1092 | if (!PJ) { |
| 1093 | PJ = new PipedJob(); |
Daniel Dunbar | 8eeb5a3 | 2009-03-20 00:11:04 +0000 | [diff] [blame] | 1094 | // FIXME: Temporary hack so that -ccc-print-bindings work until |
| 1095 | // we have pipe support. Please remove later. |
| 1096 | if (!CCCPrintBindings) |
| 1097 | cast<JobList>(Dest)->addJob(PJ); |
Daniel Dunbar | 933ac45 | 2009-03-18 07:06:02 +0000 | [diff] [blame] | 1098 | Dest = PJ; |
Daniel Dunbar | 01fb26a | 2009-03-17 17:53:55 +0000 | [diff] [blame] | 1099 | } |
Daniel Dunbar | 07c9a1d | 2009-03-17 22:47:06 +0000 | [diff] [blame] | 1100 | Result = InputInfo(PJ, A->getType(), BaseInput); |
Daniel Dunbar | 01fb26a | 2009-03-17 17:53:55 +0000 | [diff] [blame] | 1101 | } else { |
Daniel Dunbar | 07c9a1d | 2009-03-17 22:47:06 +0000 | [diff] [blame] | 1102 | Result = InputInfo(GetNamedOutputPath(C, *JA, BaseInput, AtTopLevel), |
| 1103 | A->getType(), BaseInput); |
Daniel Dunbar | 01fb26a | 2009-03-17 17:53:55 +0000 | [diff] [blame] | 1104 | } |
| 1105 | |
Daniel Dunbar | 07c9a1d | 2009-03-17 22:47:06 +0000 | [diff] [blame] | 1106 | if (CCCPrintBindings) { |
Daniel Dunbar | 049b724 | 2009-03-30 06:36:42 +0000 | [diff] [blame] | 1107 | llvm::errs() << "# \"" << T.getToolChain().getTripleString() << '"' |
| 1108 | << " - \"" << T.getName() << "\", inputs: ["; |
Daniel Dunbar | 07c9a1d | 2009-03-17 22:47:06 +0000 | [diff] [blame] | 1109 | for (unsigned i = 0, e = InputInfos.size(); i != e; ++i) { |
| 1110 | llvm::errs() << InputInfos[i].getAsString(); |
| 1111 | if (i + 1 != e) |
| 1112 | llvm::errs() << ", "; |
| 1113 | } |
| 1114 | llvm::errs() << "], output: " << Result.getAsString() << "\n"; |
| 1115 | } else { |
Daniel Dunbar | a16e4fe | 2009-03-25 04:13:45 +0000 | [diff] [blame] | 1116 | T.ConstructJob(C, *JA, *Dest, Result, InputInfos, |
| 1117 | C.getArgsForToolChain(TC), LinkingOutput); |
Daniel Dunbar | 07c9a1d | 2009-03-17 22:47:06 +0000 | [diff] [blame] | 1118 | } |
Daniel Dunbar | 7ce6add | 2009-03-16 06:56:51 +0000 | [diff] [blame] | 1119 | } |
| 1120 | |
Daniel Dunbar | 01fb26a | 2009-03-17 17:53:55 +0000 | [diff] [blame] | 1121 | const char *Driver::GetNamedOutputPath(Compilation &C, |
| 1122 | const JobAction &JA, |
| 1123 | const char *BaseInput, |
| 1124 | bool AtTopLevel) const { |
Daniel Dunbar | 16e04ff | 2009-03-18 01:38:48 +0000 | [diff] [blame] | 1125 | llvm::PrettyStackTraceString CrashInfo("Computing output path"); |
Daniel Dunbar | 01fb26a | 2009-03-17 17:53:55 +0000 | [diff] [blame] | 1126 | // Output to a user requested destination? |
| 1127 | if (AtTopLevel) { |
| 1128 | if (Arg *FinalOutput = C.getArgs().getLastArg(options::OPT_o)) |
| 1129 | return C.addResultFile(FinalOutput->getValue(C.getArgs())); |
| 1130 | } |
| 1131 | |
| 1132 | // Output to a temporary file? |
| 1133 | if (!AtTopLevel && !C.getArgs().hasArg(options::OPT_save_temps)) { |
Daniel Dunbar | b6ddc95 | 2009-03-18 19:34:39 +0000 | [diff] [blame] | 1134 | std::string TmpName = |
| 1135 | GetTemporaryPath(types::getTypeTempSuffix(JA.getType())); |
| 1136 | return C.addTempFile(C.getArgs().MakeArgString(TmpName.c_str())); |
Daniel Dunbar | 01fb26a | 2009-03-17 17:53:55 +0000 | [diff] [blame] | 1137 | } |
| 1138 | |
| 1139 | llvm::sys::Path BasePath(BaseInput); |
Daniel Dunbar | fb5e333 | 2009-03-18 02:00:31 +0000 | [diff] [blame] | 1140 | std::string BaseName(BasePath.getLast()); |
Daniel Dunbar | 01fb26a | 2009-03-17 17:53:55 +0000 | [diff] [blame] | 1141 | |
| 1142 | // Determine what the derived output name should be. |
| 1143 | const char *NamedOutput; |
| 1144 | if (JA.getType() == types::TY_Image) { |
| 1145 | NamedOutput = DefaultImageName.c_str(); |
| 1146 | } else { |
| 1147 | const char *Suffix = types::getTypeTempSuffix(JA.getType()); |
| 1148 | assert(Suffix && "All types used for output should have a suffix."); |
| 1149 | |
| 1150 | std::string::size_type End = std::string::npos; |
| 1151 | if (!types::appendSuffixForType(JA.getType())) |
| 1152 | End = BaseName.rfind('.'); |
| 1153 | std::string Suffixed(BaseName.substr(0, End)); |
| 1154 | Suffixed += '.'; |
| 1155 | Suffixed += Suffix; |
| 1156 | NamedOutput = C.getArgs().MakeArgString(Suffixed.c_str()); |
| 1157 | } |
| 1158 | |
| 1159 | // As an annoying special case, PCH generation doesn't strip the |
| 1160 | // pathname. |
| 1161 | if (JA.getType() == types::TY_PCH) { |
| 1162 | BasePath.eraseComponent(); |
Daniel Dunbar | 58a5126 | 2009-03-18 09:58:30 +0000 | [diff] [blame] | 1163 | if (BasePath.isEmpty()) |
| 1164 | BasePath = NamedOutput; |
| 1165 | else |
| 1166 | BasePath.appendComponent(NamedOutput); |
Daniel Dunbar | 01fb26a | 2009-03-17 17:53:55 +0000 | [diff] [blame] | 1167 | return C.addResultFile(C.getArgs().MakeArgString(BasePath.c_str())); |
| 1168 | } else { |
| 1169 | return C.addResultFile(NamedOutput); |
| 1170 | } |
| 1171 | } |
| 1172 | |
Daniel Dunbar | e1cef7d | 2009-03-16 05:25:36 +0000 | [diff] [blame] | 1173 | llvm::sys::Path Driver::GetFilePath(const char *Name, |
Daniel Dunbar | 73f7b8c | 2009-03-18 02:55:38 +0000 | [diff] [blame] | 1174 | const ToolChain &TC) const { |
Daniel Dunbar | cc7600c | 2009-03-18 20:26:19 +0000 | [diff] [blame] | 1175 | const ToolChain::path_list &List = TC.getFilePaths(); |
| 1176 | for (ToolChain::path_list::const_iterator |
| 1177 | it = List.begin(), ie = List.end(); it != ie; ++it) { |
| 1178 | llvm::sys::Path P(*it); |
| 1179 | P.appendComponent(Name); |
| 1180 | if (P.exists()) |
| 1181 | return P; |
| 1182 | } |
| 1183 | |
Daniel Dunbar | cc00689 | 2009-03-13 00:51:18 +0000 | [diff] [blame] | 1184 | return llvm::sys::Path(Name); |
| 1185 | } |
| 1186 | |
Daniel Dunbar | e1cef7d | 2009-03-16 05:25:36 +0000 | [diff] [blame] | 1187 | llvm::sys::Path Driver::GetProgramPath(const char *Name, |
Mike Stump | 32b1bbe | 2009-03-27 00:40:20 +0000 | [diff] [blame] | 1188 | const ToolChain &TC, |
| 1189 | bool WantFile) const { |
Daniel Dunbar | cc7600c | 2009-03-18 20:26:19 +0000 | [diff] [blame] | 1190 | const ToolChain::path_list &List = TC.getProgramPaths(); |
| 1191 | for (ToolChain::path_list::const_iterator |
| 1192 | it = List.begin(), ie = List.end(); it != ie; ++it) { |
| 1193 | llvm::sys::Path P(*it); |
| 1194 | P.appendComponent(Name); |
Mike Stump | 32b1bbe | 2009-03-27 00:40:20 +0000 | [diff] [blame] | 1195 | if (WantFile ? P.exists() : P.canExecute()) |
Daniel Dunbar | cc7600c | 2009-03-18 20:26:19 +0000 | [diff] [blame] | 1196 | return P; |
| 1197 | } |
| 1198 | |
Daniel Dunbar | 49a35de | 2009-03-23 16:15:50 +0000 | [diff] [blame] | 1199 | // If all else failed, search the path. |
| 1200 | llvm::sys::Path P(llvm::sys::Program::FindProgramByName(Name)); |
Daniel Dunbar | cb84b9a | 2009-03-18 21:34:08 +0000 | [diff] [blame] | 1201 | if (!P.empty()) |
| 1202 | return P; |
| 1203 | |
Daniel Dunbar | cc00689 | 2009-03-13 00:51:18 +0000 | [diff] [blame] | 1204 | return llvm::sys::Path(Name); |
| 1205 | } |
| 1206 | |
Daniel Dunbar | b6ddc95 | 2009-03-18 19:34:39 +0000 | [diff] [blame] | 1207 | std::string Driver::GetTemporaryPath(const char *Suffix) const { |
| 1208 | // FIXME: This is lame; sys::Path should provide this function (in |
| 1209 | // particular, it should know how to find the temporary files dir). |
| 1210 | std::string Error; |
Daniel Dunbar | 02e396a | 2009-04-20 20:28:21 +0000 | [diff] [blame] | 1211 | const char *TmpDir = ::getenv("TMPDIR"); |
| 1212 | if (!TmpDir) |
| 1213 | TmpDir = ::getenv("TEMP"); |
| 1214 | if (!TmpDir) |
Daniel Dunbar | a77aa00 | 2009-04-21 00:25:10 +0000 | [diff] [blame] | 1215 | TmpDir = ::getenv("TMP"); |
| 1216 | if (!TmpDir) |
Daniel Dunbar | 02e396a | 2009-04-20 20:28:21 +0000 | [diff] [blame] | 1217 | TmpDir = "/tmp"; |
| 1218 | llvm::sys::Path P(TmpDir); |
Daniel Dunbar | 121f251 | 2009-04-20 17:32:49 +0000 | [diff] [blame] | 1219 | P.appendComponent("cc"); |
Daniel Dunbar | b6ddc95 | 2009-03-18 19:34:39 +0000 | [diff] [blame] | 1220 | if (P.makeUnique(false, &Error)) { |
| 1221 | Diag(clang::diag::err_drv_unable_to_make_temp) << Error; |
| 1222 | return ""; |
| 1223 | } |
| 1224 | |
Daniel Dunbar | 9083abe | 2009-03-18 23:08:52 +0000 | [diff] [blame] | 1225 | // FIXME: Grumble, makeUnique sometimes leaves the file around!? |
| 1226 | // PR3837. |
| 1227 | P.eraseFromDisk(false, 0); |
| 1228 | |
Daniel Dunbar | b6ddc95 | 2009-03-18 19:34:39 +0000 | [diff] [blame] | 1229 | P.appendSuffix(Suffix); |
| 1230 | return P.toString(); |
| 1231 | } |
| 1232 | |
Daniel Dunbar | 0830365 | 2009-05-22 02:53:45 +0000 | [diff] [blame] | 1233 | const HostInfo *Driver::GetHostInfo(const char *TripleStr) const { |
Daniel Dunbar | 16e04ff | 2009-03-18 01:38:48 +0000 | [diff] [blame] | 1234 | llvm::PrettyStackTraceString CrashInfo("Constructing host"); |
Daniel Dunbar | 0830365 | 2009-05-22 02:53:45 +0000 | [diff] [blame] | 1235 | llvm::Triple Triple(TripleStr); |
Daniel Dunbar | d25acaa | 2009-03-10 23:41:59 +0000 | [diff] [blame] | 1236 | |
Daniel Dunbar | 7424b8a | 2009-03-17 19:00:50 +0000 | [diff] [blame] | 1237 | // Normalize Arch a bit. |
| 1238 | // |
Daniel Dunbar | 0830365 | 2009-05-22 02:53:45 +0000 | [diff] [blame] | 1239 | // FIXME: We shouldn't need to do this once everything goes through the triple |
| 1240 | // interface. |
| 1241 | if (Triple.getArchName() == "i686") |
| 1242 | Triple.setArchName("i386"); |
| 1243 | else if (Triple.getArchName() == "amd64") |
| 1244 | Triple.setArchName("x86_64"); |
| 1245 | else if (Triple.getArchName() == "ppc" || |
| 1246 | Triple.getArchName() == "Power Macintosh") |
| 1247 | Triple.setArchName("powerpc"); |
| 1248 | else if (Triple.getArchName() == "ppc64") |
| 1249 | Triple.setArchName("powerpc64"); |
Daniel Dunbar | 46363f5 | 2009-05-02 18:28:39 +0000 | [diff] [blame] | 1250 | |
Daniel Dunbar | 0830365 | 2009-05-22 02:53:45 +0000 | [diff] [blame] | 1251 | switch (Triple.getOS()) { |
| 1252 | case llvm::Triple::Darwin: |
| 1253 | return createDarwinHostInfo(*this, Triple); |
| 1254 | case llvm::Triple::DragonFly: |
| 1255 | return createDragonFlyHostInfo(*this, Triple); |
Daniel Dunbar | 64c77a1 | 2009-06-29 20:52:51 +0000 | [diff] [blame] | 1256 | case llvm::Triple::OpenBSD: |
| 1257 | return createOpenBSDHostInfo(*this, Triple); |
Daniel Dunbar | 0830365 | 2009-05-22 02:53:45 +0000 | [diff] [blame] | 1258 | case llvm::Triple::FreeBSD: |
| 1259 | return createFreeBSDHostInfo(*this, Triple); |
Eli Friedman | 8cac435 | 2009-05-26 07:52:18 +0000 | [diff] [blame] | 1260 | case llvm::Triple::Linux: |
| 1261 | return createLinuxHostInfo(*this, Triple); |
Daniel Dunbar | 0830365 | 2009-05-22 02:53:45 +0000 | [diff] [blame] | 1262 | default: |
| 1263 | return createUnknownHostInfo(*this, Triple); |
| 1264 | } |
Daniel Dunbar | d25acaa | 2009-03-10 23:41:59 +0000 | [diff] [blame] | 1265 | } |
Daniel Dunbar | 1a9c8ca | 2009-03-24 18:57:02 +0000 | [diff] [blame] | 1266 | |
| 1267 | bool Driver::ShouldUseClangCompiler(const Compilation &C, const JobAction &JA, |
Daniel Dunbar | b7daa2d | 2009-04-01 20:33:11 +0000 | [diff] [blame] | 1268 | const std::string &ArchNameStr) const { |
| 1269 | // FIXME: Remove this hack. |
| 1270 | const char *ArchName = ArchNameStr.c_str(); |
| 1271 | if (ArchNameStr == "powerpc") |
| 1272 | ArchName = "ppc"; |
| 1273 | else if (ArchNameStr == "powerpc64") |
| 1274 | ArchName = "ppc64"; |
| 1275 | |
Daniel Dunbar | 1a9c8ca | 2009-03-24 18:57:02 +0000 | [diff] [blame] | 1276 | // Check if user requested no clang, or clang doesn't understand |
| 1277 | // this type (we only handle single inputs for now). |
Daniel Dunbar | 0ea85f7 | 2009-03-24 19:02:31 +0000 | [diff] [blame] | 1278 | if (!CCCUseClang || JA.size() != 1 || |
Daniel Dunbar | 1a9c8ca | 2009-03-24 18:57:02 +0000 | [diff] [blame] | 1279 | !types::isAcceptedByClang((*JA.begin())->getType())) |
| 1280 | return false; |
| 1281 | |
Daniel Dunbar | 0ea85f7 | 2009-03-24 19:02:31 +0000 | [diff] [blame] | 1282 | // Otherwise make sure this is an action clang understands. |
Daniel Dunbar | 1a9c8ca | 2009-03-24 18:57:02 +0000 | [diff] [blame] | 1283 | if (isa<PreprocessJobAction>(JA)) { |
Daniel Dunbar | b1ae14d | 2009-03-24 19:14:56 +0000 | [diff] [blame] | 1284 | if (!CCCUseClangCPP) { |
| 1285 | Diag(clang::diag::warn_drv_not_using_clang_cpp); |
Daniel Dunbar | 1a9c8ca | 2009-03-24 18:57:02 +0000 | [diff] [blame] | 1286 | return false; |
Daniel Dunbar | b1ae14d | 2009-03-24 19:14:56 +0000 | [diff] [blame] | 1287 | } |
Daniel Dunbar | 1a9c8ca | 2009-03-24 18:57:02 +0000 | [diff] [blame] | 1288 | } else if (!isa<PrecompileJobAction>(JA) && !isa<CompileJobAction>(JA)) |
| 1289 | return false; |
| 1290 | |
Daniel Dunbar | 0ea85f7 | 2009-03-24 19:02:31 +0000 | [diff] [blame] | 1291 | // Use clang for C++? |
Daniel Dunbar | b1ae14d | 2009-03-24 19:14:56 +0000 | [diff] [blame] | 1292 | if (!CCCUseClangCXX && types::isCXX((*JA.begin())->getType())) { |
| 1293 | Diag(clang::diag::warn_drv_not_using_clang_cxx); |
Daniel Dunbar | 1a9c8ca | 2009-03-24 18:57:02 +0000 | [diff] [blame] | 1294 | return false; |
Daniel Dunbar | b1ae14d | 2009-03-24 19:14:56 +0000 | [diff] [blame] | 1295 | } |
Daniel Dunbar | 1a9c8ca | 2009-03-24 18:57:02 +0000 | [diff] [blame] | 1296 | |
Daniel Dunbar | 5a34ca8 | 2009-04-16 23:10:13 +0000 | [diff] [blame] | 1297 | // Always use clang for precompiling, regardless of archs. PTH is |
| 1298 | // platform independent, and this allows the use of the static |
| 1299 | // analyzer on platforms we don't have full IRgen support for. |
| 1300 | if (isa<PrecompileJobAction>(JA)) |
| 1301 | return true; |
| 1302 | |
Daniel Dunbar | 1a9c8ca | 2009-03-24 18:57:02 +0000 | [diff] [blame] | 1303 | // Finally, don't use clang if this isn't one of the user specified |
| 1304 | // archs to build. |
Daniel Dunbar | b1ae14d | 2009-03-24 19:14:56 +0000 | [diff] [blame] | 1305 | if (!CCCClangArchs.empty() && !CCCClangArchs.count(ArchName)) { |
| 1306 | Diag(clang::diag::warn_drv_not_using_clang_arch) << ArchName; |
Daniel Dunbar | 1a9c8ca | 2009-03-24 18:57:02 +0000 | [diff] [blame] | 1307 | return false; |
Daniel Dunbar | b1ae14d | 2009-03-24 19:14:56 +0000 | [diff] [blame] | 1308 | } |
Daniel Dunbar | 1a9c8ca | 2009-03-24 18:57:02 +0000 | [diff] [blame] | 1309 | |
| 1310 | return true; |
| 1311 | } |
Daniel Dunbar | 9cdd4d4 | 2009-03-26 15:58:36 +0000 | [diff] [blame] | 1312 | |
| 1313 | /// GetReleaseVersion - Parse (([0-9]+)(.([0-9]+)(.([0-9]+)?))?)? and |
| 1314 | /// return the grouped values as integers. Numbers which are not |
| 1315 | /// provided are set to 0. |
| 1316 | /// |
| 1317 | /// \return True if the entire string was parsed (9.2), or all groups |
| 1318 | /// were parsed (10.3.5extrastuff). |
| 1319 | bool Driver::GetReleaseVersion(const char *Str, unsigned &Major, |
| 1320 | unsigned &Minor, unsigned &Micro, |
| 1321 | bool &HadExtra) { |
| 1322 | HadExtra = false; |
| 1323 | |
| 1324 | Major = Minor = Micro = 0; |
| 1325 | if (*Str == '\0') |
| 1326 | return true; |
| 1327 | |
| 1328 | char *End; |
| 1329 | Major = (unsigned) strtol(Str, &End, 10); |
| 1330 | if (*Str != '\0' && *End == '\0') |
| 1331 | return true; |
| 1332 | if (*End != '.') |
| 1333 | return false; |
| 1334 | |
| 1335 | Str = End+1; |
| 1336 | Minor = (unsigned) strtol(Str, &End, 10); |
| 1337 | if (*Str != '\0' && *End == '\0') |
| 1338 | return true; |
| 1339 | if (*End != '.') |
| 1340 | return false; |
| 1341 | |
| 1342 | Str = End+1; |
| 1343 | Micro = (unsigned) strtol(Str, &End, 10); |
| 1344 | if (*Str != '\0' && *End == '\0') |
| 1345 | return true; |
| 1346 | if (Str == End) |
| 1347 | return false; |
| 1348 | HadExtra = true; |
| 1349 | return true; |
| 1350 | } |