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