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