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 | |
Daniel Dunbar | 1368954 | 2009-03-13 20:33:35 +0000 | [diff] [blame] | 25 | #include "llvm/ADT/StringSet.h" |
Daniel Dunbar | 8f25c79 | 2009-03-18 01:38:48 +0000 | [diff] [blame] | 26 | #include "llvm/Support/PrettyStackTrace.h" |
Daniel Dunbar | 0648262 | 2009-03-05 06:38:47 +0000 | [diff] [blame] | 27 | #include "llvm/Support/raw_ostream.h" |
Daniel Dunbar | 53ec552 | 2009-03-12 07:58:46 +0000 | [diff] [blame] | 28 | #include "llvm/System/Path.h" |
Daniel Dunbar | 632f50e | 2009-03-18 21:34:08 +0000 | [diff] [blame] | 29 | #include "llvm/System/Program.h" |
Daniel Dunbar | ba10213 | 2009-03-13 12:19:02 +0000 | [diff] [blame] | 30 | |
Daniel Dunbar | f353c8c | 2009-03-16 06:56:51 +0000 | [diff] [blame] | 31 | #include "InputInfo.h" |
| 32 | |
Daniel Dunbar | ba10213 | 2009-03-13 12:19:02 +0000 | [diff] [blame] | 33 | #include <map> |
| 34 | |
Daniel Dunbar | 1b3bb6e | 2009-03-04 20:49:20 +0000 | [diff] [blame] | 35 | using namespace clang::driver; |
| 36 | |
Daniel Dunbar | dd98e2c | 2009-03-10 23:41:59 +0000 | [diff] [blame] | 37 | Driver::Driver(const char *_Name, const char *_Dir, |
Daniel Dunbar | 4ad4b3e | 2009-03-12 08:55:43 +0000 | [diff] [blame] | 38 | const char *_DefaultHostTriple, |
Daniel Dunbar | f353c8c | 2009-03-16 06:56:51 +0000 | [diff] [blame] | 39 | const char *_DefaultImageName, |
Daniel Dunbar | 4ad4b3e | 2009-03-12 08:55:43 +0000 | [diff] [blame] | 40 | Diagnostic &_Diags) |
| 41 | : Opts(new OptTable()), Diags(_Diags), |
Daniel Dunbar | dd98e2c | 2009-03-10 23:41:59 +0000 | [diff] [blame] | 42 | Name(_Name), Dir(_Dir), DefaultHostTriple(_DefaultHostTriple), |
Daniel Dunbar | f353c8c | 2009-03-16 06:56:51 +0000 | [diff] [blame] | 43 | DefaultImageName(_DefaultImageName), |
Daniel Dunbar | dd98e2c | 2009-03-10 23:41:59 +0000 | [diff] [blame] | 44 | Host(0), |
Daniel Dunbar | 5c3c1d7 | 2009-03-17 22:47:06 +0000 | [diff] [blame] | 45 | CCCIsCXX(false), CCCEcho(false), CCCPrintBindings(false), |
Daniel Dunbar | 8b1604e | 2009-03-13 00:17:48 +0000 | [diff] [blame] | 46 | CCCNoClang(false), CCCNoClangCXX(false), CCCNoClangCPP(false), |
| 47 | SuppressMissingInputWarning(false) |
Daniel Dunbar | 365c02f | 2009-03-10 20:52:46 +0000 | [diff] [blame] | 48 | { |
Daniel Dunbar | 3ede8d0 | 2009-03-02 19:59:07 +0000 | [diff] [blame] | 49 | } |
| 50 | |
| 51 | Driver::~Driver() { |
Daniel Dunbar | 1b3bb6e | 2009-03-04 20:49:20 +0000 | [diff] [blame] | 52 | delete Opts; |
Daniel Dunbar | 7e4534d | 2009-03-18 01:09:40 +0000 | [diff] [blame] | 53 | delete Host; |
Daniel Dunbar | 3ede8d0 | 2009-03-02 19:59:07 +0000 | [diff] [blame] | 54 | } |
| 55 | |
Daniel Dunbar | 0648262 | 2009-03-05 06:38:47 +0000 | [diff] [blame] | 56 | ArgList *Driver::ParseArgStrings(const char **ArgBegin, const char **ArgEnd) { |
Daniel Dunbar | 8f25c79 | 2009-03-18 01:38:48 +0000 | [diff] [blame] | 57 | llvm::PrettyStackTraceString CrashInfo("Command line argument parsing"); |
Daniel Dunbar | 0648262 | 2009-03-05 06:38:47 +0000 | [diff] [blame] | 58 | ArgList *Args = new ArgList(ArgBegin, ArgEnd); |
| 59 | |
Daniel Dunbar | ad2a9af | 2009-03-13 11:38:42 +0000 | [diff] [blame] | 60 | // FIXME: Handle '@' args (or at least error on them). |
| 61 | |
Daniel Dunbar | 0648262 | 2009-03-05 06:38:47 +0000 | [diff] [blame] | 62 | unsigned Index = 0, End = ArgEnd - ArgBegin; |
| 63 | while (Index < End) { |
Daniel Dunbar | 4139340 | 2009-03-13 01:01:44 +0000 | [diff] [blame] | 64 | // gcc's handling of empty arguments doesn't make |
| 65 | // sense, but this is not a common use case. :) |
| 66 | // |
| 67 | // We just ignore them here (note that other things may |
| 68 | // still take them as arguments). |
| 69 | if (Args->getArgString(Index)[0] == '\0') { |
| 70 | ++Index; |
| 71 | continue; |
| 72 | } |
| 73 | |
Daniel Dunbar | 0648262 | 2009-03-05 06:38:47 +0000 | [diff] [blame] | 74 | unsigned Prev = Index; |
| 75 | Arg *A = getOpts().ParseOneArg(*Args, Index, End); |
Daniel Dunbar | 53ec552 | 2009-03-12 07:58:46 +0000 | [diff] [blame] | 76 | if (A) { |
| 77 | if (A->getOption().isUnsupported()) { |
Daniel Dunbar | b897f5d | 2009-03-12 09:13:48 +0000 | [diff] [blame] | 78 | Diag(clang::diag::err_drv_unsupported_opt) << A->getOption().getName(); |
Daniel Dunbar | 53ec552 | 2009-03-12 07:58:46 +0000 | [diff] [blame] | 79 | continue; |
| 80 | } |
| 81 | |
Daniel Dunbar | 0648262 | 2009-03-05 06:38:47 +0000 | [diff] [blame] | 82 | Args->append(A); |
Daniel Dunbar | 53ec552 | 2009-03-12 07:58:46 +0000 | [diff] [blame] | 83 | } |
Daniel Dunbar | 0648262 | 2009-03-05 06:38:47 +0000 | [diff] [blame] | 84 | |
| 85 | assert(Index > Prev && "Parser failed to consume argument."); |
Daniel Dunbar | 70c1684 | 2009-03-17 04:12:06 +0000 | [diff] [blame] | 86 | (void) Prev; |
Daniel Dunbar | 0648262 | 2009-03-05 06:38:47 +0000 | [diff] [blame] | 87 | } |
| 88 | |
| 89 | return Args; |
| 90 | } |
| 91 | |
Daniel Dunbar | 3ede8d0 | 2009-03-02 19:59:07 +0000 | [diff] [blame] | 92 | Compilation *Driver::BuildCompilation(int argc, const char **argv) { |
Daniel Dunbar | 8f25c79 | 2009-03-18 01:38:48 +0000 | [diff] [blame] | 93 | llvm::PrettyStackTraceString CrashInfo("Compilation construction"); |
| 94 | |
Daniel Dunbar | cb88167 | 2009-03-13 00:51:18 +0000 | [diff] [blame] | 95 | // FIXME: Handle environment options which effect driver behavior, |
| 96 | // somewhere (client?). GCC_EXEC_PREFIX, COMPILER_PATH, |
| 97 | // LIBRARY_PATH, LPATH, CC_PRINT_OPTIONS, QA_OVERRIDE_GCC3_OPTIONS. |
| 98 | |
| 99 | // FIXME: What are we going to do with -V and -b? |
| 100 | |
| 101 | // FIXME: Handle CCC_ADD_ARGS. |
| 102 | |
Daniel Dunbar | 365c02f | 2009-03-10 20:52:46 +0000 | [diff] [blame] | 103 | // FIXME: This stuff needs to go into the Compilation, not the |
| 104 | // driver. |
Daniel Dunbar | 53ec552 | 2009-03-12 07:58:46 +0000 | [diff] [blame] | 105 | bool CCCPrintOptions = false, CCCPrintActions = false; |
Daniel Dunbar | 0648262 | 2009-03-05 06:38:47 +0000 | [diff] [blame] | 106 | |
Daniel Dunbar | 365c02f | 2009-03-10 20:52:46 +0000 | [diff] [blame] | 107 | const char **Start = argv + 1, **End = argv + argc; |
Daniel Dunbar | dd98e2c | 2009-03-10 23:41:59 +0000 | [diff] [blame] | 108 | const char *HostTriple = DefaultHostTriple.c_str(); |
Daniel Dunbar | 365c02f | 2009-03-10 20:52:46 +0000 | [diff] [blame] | 109 | |
| 110 | // Read -ccc args. |
| 111 | // |
| 112 | // FIXME: We need to figure out where this behavior should |
| 113 | // live. Most of it should be outside in the client; the parts that |
| 114 | // aren't should have proper options, either by introducing new ones |
| 115 | // or by overloading gcc ones like -V or -b. |
| 116 | for (; Start != End && memcmp(*Start, "-ccc-", 5) == 0; ++Start) { |
| 117 | const char *Opt = *Start + 5; |
| 118 | |
| 119 | if (!strcmp(Opt, "print-options")) { |
| 120 | CCCPrintOptions = true; |
| 121 | } else if (!strcmp(Opt, "print-phases")) { |
Daniel Dunbar | 53ec552 | 2009-03-12 07:58:46 +0000 | [diff] [blame] | 122 | CCCPrintActions = true; |
Daniel Dunbar | 5c3c1d7 | 2009-03-17 22:47:06 +0000 | [diff] [blame] | 123 | } else if (!strcmp(Opt, "print-bindings")) { |
| 124 | CCCPrintBindings = true; |
Daniel Dunbar | 365c02f | 2009-03-10 20:52:46 +0000 | [diff] [blame] | 125 | } else if (!strcmp(Opt, "cxx")) { |
| 126 | CCCIsCXX = true; |
| 127 | } else if (!strcmp(Opt, "echo")) { |
| 128 | CCCEcho = true; |
| 129 | |
| 130 | } else if (!strcmp(Opt, "no-clang")) { |
| 131 | CCCNoClang = true; |
| 132 | } else if (!strcmp(Opt, "no-clang-cxx")) { |
| 133 | CCCNoClangCXX = true; |
| 134 | } else if (!strcmp(Opt, "no-clang-cpp")) { |
| 135 | CCCNoClangCPP = true; |
| 136 | } else if (!strcmp(Opt, "clang-archs")) { |
| 137 | assert(Start+1 < End && "FIXME: -ccc- argument handling."); |
| 138 | const char *Cur = *++Start; |
| 139 | |
| 140 | for (;;) { |
| 141 | const char *Next = strchr(Cur, ','); |
| 142 | |
| 143 | if (Next) { |
| 144 | CCCClangArchs.insert(std::string(Cur, Next)); |
| 145 | Cur = Next + 1; |
| 146 | } else { |
| 147 | CCCClangArchs.insert(std::string(Cur)); |
| 148 | break; |
| 149 | } |
| 150 | } |
| 151 | |
Daniel Dunbar | dd98e2c | 2009-03-10 23:41:59 +0000 | [diff] [blame] | 152 | } else if (!strcmp(Opt, "host-triple")) { |
Daniel Dunbar | 365c02f | 2009-03-10 20:52:46 +0000 | [diff] [blame] | 153 | assert(Start+1 < End && "FIXME: -ccc- argument handling."); |
Daniel Dunbar | dd98e2c | 2009-03-10 23:41:59 +0000 | [diff] [blame] | 154 | HostTriple = *++Start; |
Daniel Dunbar | 365c02f | 2009-03-10 20:52:46 +0000 | [diff] [blame] | 155 | |
| 156 | } else { |
| 157 | // FIXME: Error handling. |
| 158 | llvm::errs() << "invalid option: " << *Start << "\n"; |
| 159 | exit(1); |
| 160 | } |
| 161 | } |
Daniel Dunbar | dd98e2c | 2009-03-10 23:41:59 +0000 | [diff] [blame] | 162 | |
Daniel Dunbar | 365c02f | 2009-03-10 20:52:46 +0000 | [diff] [blame] | 163 | ArgList *Args = ParseArgStrings(Start, End); |
| 164 | |
Daniel Dunbar | e504952 | 2009-03-17 20:45:45 +0000 | [diff] [blame] | 165 | Host = GetHostInfo(HostTriple); |
Daniel Dunbar | cb88167 | 2009-03-13 00:51:18 +0000 | [diff] [blame] | 166 | |
Daniel Dunbar | 586dc23 | 2009-03-16 06:42:30 +0000 | [diff] [blame] | 167 | // The compilation takes ownership of Args. |
Daniel Dunbar | e530ad4 | 2009-03-18 22:16:03 +0000 | [diff] [blame^] | 168 | Compilation *C = new Compilation(*this, *Host->getToolChain(*Args), Args); |
Daniel Dunbar | 2154923 | 2009-03-18 02:55:38 +0000 | [diff] [blame] | 169 | |
| 170 | // FIXME: This behavior shouldn't be here. |
| 171 | if (CCCPrintOptions) { |
| 172 | PrintOptions(C->getArgs()); |
| 173 | return C; |
| 174 | } |
| 175 | |
| 176 | if (!HandleImmediateArgs(*C)) |
| 177 | return C; |
| 178 | |
| 179 | // Construct the list of abstract actions to perform for this |
| 180 | // compilation. We avoid passing a Compilation here simply to |
| 181 | // enforce the abstraction that pipelining is not host or toolchain |
| 182 | // dependent (other than the driver driver test). |
| 183 | if (Host->useDriverDriver()) |
| 184 | BuildUniversalActions(C->getArgs(), C->getActions()); |
| 185 | else |
| 186 | BuildActions(C->getArgs(), C->getActions()); |
| 187 | |
| 188 | if (CCCPrintActions) { |
Daniel Dunbar | 10ffa9a | 2009-03-18 03:13:20 +0000 | [diff] [blame] | 189 | PrintActions(*C); |
Daniel Dunbar | 2154923 | 2009-03-18 02:55:38 +0000 | [diff] [blame] | 190 | return C; |
| 191 | } |
| 192 | |
| 193 | BuildJobs(*C); |
Daniel Dunbar | 8d2554a | 2009-03-15 01:38:15 +0000 | [diff] [blame] | 194 | |
| 195 | return C; |
Daniel Dunbar | 365c02f | 2009-03-10 20:52:46 +0000 | [diff] [blame] | 196 | } |
| 197 | |
Daniel Dunbar | d65bddc | 2009-03-12 18:24:49 +0000 | [diff] [blame] | 198 | void Driver::PrintOptions(const ArgList &Args) const { |
Daniel Dunbar | 0648262 | 2009-03-05 06:38:47 +0000 | [diff] [blame] | 199 | unsigned i = 0; |
Daniel Dunbar | 53ec552 | 2009-03-12 07:58:46 +0000 | [diff] [blame] | 200 | for (ArgList::const_iterator it = Args.begin(), ie = Args.end(); |
Daniel Dunbar | 0648262 | 2009-03-05 06:38:47 +0000 | [diff] [blame] | 201 | it != ie; ++it, ++i) { |
| 202 | Arg *A = *it; |
| 203 | llvm::errs() << "Option " << i << " - " |
| 204 | << "Name: \"" << A->getOption().getName() << "\", " |
| 205 | << "Values: {"; |
| 206 | for (unsigned j = 0; j < A->getNumValues(); ++j) { |
| 207 | if (j) |
| 208 | llvm::errs() << ", "; |
Daniel Dunbar | 53ec552 | 2009-03-12 07:58:46 +0000 | [diff] [blame] | 209 | llvm::errs() << '"' << A->getValue(Args, j) << '"'; |
Daniel Dunbar | 0648262 | 2009-03-05 06:38:47 +0000 | [diff] [blame] | 210 | } |
| 211 | llvm::errs() << "}\n"; |
Daniel Dunbar | 0648262 | 2009-03-05 06:38:47 +0000 | [diff] [blame] | 212 | } |
Daniel Dunbar | 3ede8d0 | 2009-03-02 19:59:07 +0000 | [diff] [blame] | 213 | } |
Daniel Dunbar | dd98e2c | 2009-03-10 23:41:59 +0000 | [diff] [blame] | 214 | |
Daniel Dunbar | cb88167 | 2009-03-13 00:51:18 +0000 | [diff] [blame] | 215 | void Driver::PrintVersion() const { |
Mike Stump | 5d023c3 | 2009-03-18 14:00:02 +0000 | [diff] [blame] | 216 | static char buf[] = "$URL$"; |
| 217 | char *zap = strstr(buf, "/lib/Driver"); |
| 218 | if (zap) |
| 219 | *zap = 0; |
| 220 | zap = strstr(buf, "/clang/tools/clang"); |
| 221 | if (zap) |
| 222 | *zap = 0; |
Mike Stump | e70295b | 2009-03-18 15:19:35 +0000 | [diff] [blame] | 223 | const char *vers = buf+6; |
Mike Stump | 8944c38 | 2009-03-18 18:45:55 +0000 | [diff] [blame] | 224 | // FIXME: Add cmake support and remove #ifdef |
| 225 | #ifdef SVN_REVISION |
| 226 | const char *revision = SVN_REVISION; |
| 227 | #else |
| 228 | const char *revision = ""; |
| 229 | #endif |
Daniel Dunbar | cb88167 | 2009-03-13 00:51:18 +0000 | [diff] [blame] | 230 | // FIXME: The following handlers should use a callback mechanism, we |
| 231 | // don't know what the client would like to do. |
Mike Stump | d227fe7 | 2009-03-18 21:19:11 +0000 | [diff] [blame] | 232 | llvm::errs() << "clang version 1.0 (" << vers << " " << revision << ")" << "\n"; |
Mike Stump | 5d023c3 | 2009-03-18 14:00:02 +0000 | [diff] [blame] | 233 | // FIXME: Add cmake support and remove #ifdef |
| 234 | #ifdef TARGET_TRIPLE |
| 235 | llvm::errs() << "Target: " << TARGET_TRIPLE << "\n"; |
| 236 | #endif |
Daniel Dunbar | cb88167 | 2009-03-13 00:51:18 +0000 | [diff] [blame] | 237 | } |
| 238 | |
Daniel Dunbar | 2154923 | 2009-03-18 02:55:38 +0000 | [diff] [blame] | 239 | bool Driver::HandleImmediateArgs(const Compilation &C) { |
Daniel Dunbar | cb88167 | 2009-03-13 00:51:18 +0000 | [diff] [blame] | 240 | // The order these options are handled in in gcc is all over the |
| 241 | // place, but we don't expect inconsistencies w.r.t. that to matter |
| 242 | // in practice. |
Daniel Dunbar | 2154923 | 2009-03-18 02:55:38 +0000 | [diff] [blame] | 243 | if (C.getArgs().hasArg(options::OPT_v) || |
| 244 | C.getArgs().hasArg(options::OPT__HASH_HASH_HASH)) { |
Daniel Dunbar | cb88167 | 2009-03-13 00:51:18 +0000 | [diff] [blame] | 245 | PrintVersion(); |
| 246 | SuppressMissingInputWarning = true; |
| 247 | } |
| 248 | |
Daniel Dunbar | 2154923 | 2009-03-18 02:55:38 +0000 | [diff] [blame] | 249 | const ToolChain &TC = C.getDefaultToolChain(); |
Daniel Dunbar | cb88167 | 2009-03-13 00:51:18 +0000 | [diff] [blame] | 250 | // FIXME: The following handlers should use a callback mechanism, we |
| 251 | // don't know what the client would like to do. |
Daniel Dunbar | 2154923 | 2009-03-18 02:55:38 +0000 | [diff] [blame] | 252 | if (Arg *A = C.getArgs().getLastArg(options::OPT_print_file_name_EQ)) { |
| 253 | llvm::outs() << GetFilePath(A->getValue(C.getArgs()), TC).toString() |
| 254 | << "\n"; |
Daniel Dunbar | cb88167 | 2009-03-13 00:51:18 +0000 | [diff] [blame] | 255 | return false; |
| 256 | } |
| 257 | |
Daniel Dunbar | 2154923 | 2009-03-18 02:55:38 +0000 | [diff] [blame] | 258 | if (Arg *A = C.getArgs().getLastArg(options::OPT_print_prog_name_EQ)) { |
| 259 | llvm::outs() << GetProgramPath(A->getValue(C.getArgs()), TC).toString() |
| 260 | << "\n"; |
Daniel Dunbar | cb88167 | 2009-03-13 00:51:18 +0000 | [diff] [blame] | 261 | return false; |
| 262 | } |
| 263 | |
Daniel Dunbar | 2154923 | 2009-03-18 02:55:38 +0000 | [diff] [blame] | 264 | if (C.getArgs().hasArg(options::OPT_print_libgcc_file_name)) { |
| 265 | llvm::outs() << GetProgramPath("libgcc.a", TC).toString() << "\n"; |
Daniel Dunbar | cb88167 | 2009-03-13 00:51:18 +0000 | [diff] [blame] | 266 | return false; |
| 267 | } |
| 268 | |
| 269 | return true; |
| 270 | } |
| 271 | |
Daniel Dunbar | 10ffa9a | 2009-03-18 03:13:20 +0000 | [diff] [blame] | 272 | static unsigned PrintActions1(const Compilation &C, |
Daniel Dunbar | ba10213 | 2009-03-13 12:19:02 +0000 | [diff] [blame] | 273 | Action *A, |
| 274 | std::map<Action*, unsigned> &Ids) { |
| 275 | if (Ids.count(A)) |
| 276 | return Ids[A]; |
| 277 | |
| 278 | std::string str; |
| 279 | llvm::raw_string_ostream os(str); |
| 280 | |
| 281 | os << Action::getClassName(A->getKind()) << ", "; |
| 282 | if (InputAction *IA = dyn_cast<InputAction>(A)) { |
Daniel Dunbar | 10ffa9a | 2009-03-18 03:13:20 +0000 | [diff] [blame] | 283 | os << "\"" << IA->getInputArg().getValue(C.getArgs()) << "\""; |
Daniel Dunbar | ba10213 | 2009-03-13 12:19:02 +0000 | [diff] [blame] | 284 | } else if (BindArchAction *BIA = dyn_cast<BindArchAction>(A)) { |
Daniel Dunbar | 10ffa9a | 2009-03-18 03:13:20 +0000 | [diff] [blame] | 285 | os << '"' << (BIA->getArchName() ? BIA->getArchName() : |
| 286 | C.getDefaultToolChain().getArchName()) << '"' |
| 287 | << ", {" << PrintActions1(C, *BIA->begin(), Ids) << "}"; |
Daniel Dunbar | ba10213 | 2009-03-13 12:19:02 +0000 | [diff] [blame] | 288 | } else { |
| 289 | os << "{"; |
| 290 | for (Action::iterator it = A->begin(), ie = A->end(); it != ie;) { |
Daniel Dunbar | 10ffa9a | 2009-03-18 03:13:20 +0000 | [diff] [blame] | 291 | os << PrintActions1(C, *it, Ids); |
Daniel Dunbar | ba10213 | 2009-03-13 12:19:02 +0000 | [diff] [blame] | 292 | ++it; |
| 293 | if (it != ie) |
| 294 | os << ", "; |
| 295 | } |
| 296 | os << "}"; |
| 297 | } |
| 298 | |
| 299 | unsigned Id = Ids.size(); |
| 300 | Ids[A] = Id; |
Daniel Dunbar | b269c32 | 2009-03-13 17:20:20 +0000 | [diff] [blame] | 301 | llvm::errs() << Id << ": " << os.str() << ", " |
Daniel Dunbar | ba10213 | 2009-03-13 12:19:02 +0000 | [diff] [blame] | 302 | << types::getTypeName(A->getType()) << "\n"; |
| 303 | |
| 304 | return Id; |
| 305 | } |
| 306 | |
Daniel Dunbar | 10ffa9a | 2009-03-18 03:13:20 +0000 | [diff] [blame] | 307 | void Driver::PrintActions(const Compilation &C) const { |
Daniel Dunbar | ba10213 | 2009-03-13 12:19:02 +0000 | [diff] [blame] | 308 | std::map<Action*, unsigned> Ids; |
Daniel Dunbar | 10ffa9a | 2009-03-18 03:13:20 +0000 | [diff] [blame] | 309 | for (ActionList::const_iterator it = C.getActions().begin(), |
| 310 | ie = C.getActions().end(); it != ie; ++it) |
| 311 | PrintActions1(C, *it, Ids); |
Daniel Dunbar | 53ec552 | 2009-03-12 07:58:46 +0000 | [diff] [blame] | 312 | } |
| 313 | |
Daniel Dunbar | 2154923 | 2009-03-18 02:55:38 +0000 | [diff] [blame] | 314 | void Driver::BuildUniversalActions(const ArgList &Args, |
| 315 | ActionList &Actions) const { |
Daniel Dunbar | 8f25c79 | 2009-03-18 01:38:48 +0000 | [diff] [blame] | 316 | llvm::PrettyStackTraceString CrashInfo("Building actions for universal build"); |
Daniel Dunbar | 1368954 | 2009-03-13 20:33:35 +0000 | [diff] [blame] | 317 | // Collect the list of architectures. Duplicates are allowed, but |
| 318 | // should only be handled once (in the order seen). |
| 319 | llvm::StringSet<> ArchNames; |
| 320 | llvm::SmallVector<const char *, 4> Archs; |
Daniel Dunbar | 2fe63e6 | 2009-03-12 18:40:18 +0000 | [diff] [blame] | 321 | for (ArgList::const_iterator it = Args.begin(), ie = Args.end(); |
| 322 | it != ie; ++it) { |
| 323 | Arg *A = *it; |
| 324 | |
| 325 | if (A->getOption().getId() == options::OPT_arch) { |
Daniel Dunbar | 1368954 | 2009-03-13 20:33:35 +0000 | [diff] [blame] | 326 | const char *Name = A->getValue(Args); |
| 327 | |
Daniel Dunbar | 2fe63e6 | 2009-03-12 18:40:18 +0000 | [diff] [blame] | 328 | // FIXME: We need to handle canonicalization of the specified |
| 329 | // arch? |
| 330 | |
Daniel Dunbar | 1368954 | 2009-03-13 20:33:35 +0000 | [diff] [blame] | 331 | if (ArchNames.insert(Name)) |
| 332 | Archs.push_back(Name); |
Daniel Dunbar | 2fe63e6 | 2009-03-12 18:40:18 +0000 | [diff] [blame] | 333 | } |
| 334 | } |
| 335 | |
Daniel Dunbar | 10ffa9a | 2009-03-18 03:13:20 +0000 | [diff] [blame] | 336 | // When there is no explicit arch for this platform, make sure we |
| 337 | // still bind the architecture (to the default) so that -Xarch_ is |
| 338 | // handled correctly. |
| 339 | if (!Archs.size()) |
| 340 | Archs.push_back(0); |
Daniel Dunbar | 2fe63e6 | 2009-03-12 18:40:18 +0000 | [diff] [blame] | 341 | |
| 342 | // FIXME: We killed off some others but these aren't yet detected in |
| 343 | // a functional manner. If we added information to jobs about which |
| 344 | // "auxiliary" files they wrote then we could detect the conflict |
| 345 | // these cause downstream. |
| 346 | if (Archs.size() > 1) { |
| 347 | // No recovery needed, the point of this is just to prevent |
| 348 | // overwriting the same files. |
| 349 | if (const Arg *A = Args.getLastArg(options::OPT_M_Group)) |
| 350 | Diag(clang::diag::err_drv_invalid_opt_with_multiple_archs) |
| 351 | << A->getOption().getName(); |
| 352 | if (const Arg *A = Args.getLastArg(options::OPT_save_temps)) |
| 353 | Diag(clang::diag::err_drv_invalid_opt_with_multiple_archs) |
| 354 | << A->getOption().getName(); |
| 355 | } |
| 356 | |
| 357 | ActionList SingleActions; |
| 358 | BuildActions(Args, SingleActions); |
| 359 | |
| 360 | // Add in arch binding and lipo (if necessary) for every top level |
| 361 | // action. |
| 362 | for (unsigned i = 0, e = SingleActions.size(); i != e; ++i) { |
| 363 | Action *Act = SingleActions[i]; |
| 364 | |
| 365 | // Make sure we can lipo this kind of output. If not (and it is an |
| 366 | // actual output) then we disallow, since we can't create an |
| 367 | // output file with the right name without overwriting it. We |
| 368 | // could remove this oddity by just changing the output names to |
| 369 | // include the arch, which would also fix |
| 370 | // -save-temps. Compatibility wins for now. |
| 371 | |
Daniel Dunbar | 3dbd6c5 | 2009-03-13 17:46:02 +0000 | [diff] [blame] | 372 | if (Archs.size() > 1 && !types::canLipoType(Act->getType())) |
Daniel Dunbar | 2fe63e6 | 2009-03-12 18:40:18 +0000 | [diff] [blame] | 373 | Diag(clang::diag::err_drv_invalid_output_with_multiple_archs) |
| 374 | << types::getTypeName(Act->getType()); |
| 375 | |
| 376 | ActionList Inputs; |
Daniel Dunbar | 1368954 | 2009-03-13 20:33:35 +0000 | [diff] [blame] | 377 | for (unsigned i = 0, e = Archs.size(); i != e; ++i ) |
| 378 | Inputs.push_back(new BindArchAction(Act, Archs[i])); |
Daniel Dunbar | 2fe63e6 | 2009-03-12 18:40:18 +0000 | [diff] [blame] | 379 | |
| 380 | // Lipo if necessary, We do it this way because we need to set the |
| 381 | // arch flag so that -Xarch_ gets overwritten. |
| 382 | if (Inputs.size() == 1 || Act->getType() == types::TY_Nothing) |
| 383 | Actions.append(Inputs.begin(), Inputs.end()); |
| 384 | else |
| 385 | Actions.push_back(new LipoJobAction(Inputs, Act->getType())); |
| 386 | } |
Daniel Dunbar | 53ec552 | 2009-03-12 07:58:46 +0000 | [diff] [blame] | 387 | } |
| 388 | |
Daniel Dunbar | 2154923 | 2009-03-18 02:55:38 +0000 | [diff] [blame] | 389 | void Driver::BuildActions(const ArgList &Args, ActionList &Actions) const { |
Daniel Dunbar | 8f25c79 | 2009-03-18 01:38:48 +0000 | [diff] [blame] | 390 | llvm::PrettyStackTraceString CrashInfo("Building compilation actions"); |
Daniel Dunbar | af61c71 | 2009-03-12 23:55:14 +0000 | [diff] [blame] | 391 | // Start by constructing the list of inputs and their types. |
| 392 | |
Daniel Dunbar | 83dd21f | 2009-03-13 17:57:10 +0000 | [diff] [blame] | 393 | // Track the current user specified (-x) input. We also explicitly |
| 394 | // track the argument used to set the type; we only want to claim |
| 395 | // the type when we actually use it, so we warn about unused -x |
| 396 | // arguments. |
| 397 | types::ID InputType = types::TY_Nothing; |
| 398 | Arg *InputTypeArg = 0; |
| 399 | |
Daniel Dunbar | 53ec552 | 2009-03-12 07:58:46 +0000 | [diff] [blame] | 400 | llvm::SmallVector<std::pair<types::ID, const Arg*>, 16> Inputs; |
| 401 | for (ArgList::const_iterator it = Args.begin(), ie = Args.end(); |
| 402 | it != ie; ++it) { |
| 403 | Arg *A = *it; |
| 404 | |
| 405 | if (isa<InputOption>(A->getOption())) { |
| 406 | const char *Value = A->getValue(Args); |
| 407 | types::ID Ty = types::TY_INVALID; |
| 408 | |
| 409 | // Infer the input type if necessary. |
Daniel Dunbar | 83dd21f | 2009-03-13 17:57:10 +0000 | [diff] [blame] | 410 | if (InputType == types::TY_Nothing) { |
| 411 | // If there was an explicit arg for this, claim it. |
| 412 | if (InputTypeArg) |
| 413 | InputTypeArg->claim(); |
| 414 | |
Daniel Dunbar | 53ec552 | 2009-03-12 07:58:46 +0000 | [diff] [blame] | 415 | // stdin must be handled specially. |
| 416 | if (memcmp(Value, "-", 2) == 0) { |
| 417 | // If running with -E, treat as a C input (this changes the |
| 418 | // builtin macros, for example). This may be overridden by |
| 419 | // -ObjC below. |
| 420 | // |
| 421 | // Otherwise emit an error but still use a valid type to |
| 422 | // avoid spurious errors (e.g., no inputs). |
Daniel Dunbar | 8022fd4 | 2009-03-15 00:48:16 +0000 | [diff] [blame] | 423 | if (!Args.hasArg(options::OPT_E, false)) |
Daniel Dunbar | b897f5d | 2009-03-12 09:13:48 +0000 | [diff] [blame] | 424 | Diag(clang::diag::err_drv_unknown_stdin_type); |
Daniel Dunbar | 53ec552 | 2009-03-12 07:58:46 +0000 | [diff] [blame] | 425 | Ty = types::TY_C; |
| 426 | } else { |
| 427 | // Otherwise lookup by extension, and fallback to ObjectType |
| 428 | // if not found. |
| 429 | if (const char *Ext = strrchr(Value, '.')) |
| 430 | Ty = types::lookupTypeForExtension(Ext + 1); |
| 431 | if (Ty == types::TY_INVALID) |
| 432 | Ty = types::TY_Object; |
| 433 | } |
| 434 | |
| 435 | // -ObjC and -ObjC++ override the default language, but only |
| 436 | // -for "source files". We just treat everything that isn't a |
| 437 | // -linker input as a source file. |
| 438 | // |
| 439 | // FIXME: Clean this up if we move the phase sequence into the |
| 440 | // type. |
| 441 | if (Ty != types::TY_Object) { |
| 442 | if (Args.hasArg(options::OPT_ObjC)) |
| 443 | Ty = types::TY_ObjC; |
| 444 | else if (Args.hasArg(options::OPT_ObjCXX)) |
| 445 | Ty = types::TY_ObjCXX; |
| 446 | } |
| 447 | } else { |
| 448 | assert(InputTypeArg && "InputType set w/o InputTypeArg"); |
| 449 | InputTypeArg->claim(); |
| 450 | Ty = InputType; |
| 451 | } |
| 452 | |
| 453 | // Check that the file exists. It isn't clear this is worth |
| 454 | // doing, since the tool presumably does this anyway, and this |
| 455 | // just adds an extra stat to the equation, but this is gcc |
| 456 | // compatible. |
| 457 | if (memcmp(Value, "-", 2) != 0 && !llvm::sys::Path(Value).exists()) |
Daniel Dunbar | b897f5d | 2009-03-12 09:13:48 +0000 | [diff] [blame] | 458 | Diag(clang::diag::err_drv_no_such_file) << A->getValue(Args); |
Daniel Dunbar | 53ec552 | 2009-03-12 07:58:46 +0000 | [diff] [blame] | 459 | else |
| 460 | Inputs.push_back(std::make_pair(Ty, A)); |
| 461 | |
| 462 | } else if (A->getOption().isLinkerInput()) { |
| 463 | // Just treat as object type, we could make a special type for |
| 464 | // this if necessary. |
| 465 | Inputs.push_back(std::make_pair(types::TY_Object, A)); |
| 466 | |
| 467 | } else if (A->getOption().getId() == options::OPT_x) { |
| 468 | InputTypeArg = A; |
| 469 | InputType = types::lookupTypeForTypeSpecifier(A->getValue(Args)); |
| 470 | |
| 471 | // Follow gcc behavior and treat as linker input for invalid -x |
| 472 | // options. Its not clear why we shouldn't just revert to |
| 473 | // unknown; but this isn't very important, we might as well be |
| 474 | // bug comatible. |
| 475 | if (!InputType) { |
Daniel Dunbar | b897f5d | 2009-03-12 09:13:48 +0000 | [diff] [blame] | 476 | Diag(clang::diag::err_drv_unknown_language) << A->getValue(Args); |
Daniel Dunbar | 53ec552 | 2009-03-12 07:58:46 +0000 | [diff] [blame] | 477 | InputType = types::TY_Object; |
| 478 | } |
| 479 | } |
| 480 | } |
| 481 | |
Daniel Dunbar | 8b1604e | 2009-03-13 00:17:48 +0000 | [diff] [blame] | 482 | if (!SuppressMissingInputWarning && Inputs.empty()) { |
Daniel Dunbar | af61c71 | 2009-03-12 23:55:14 +0000 | [diff] [blame] | 483 | Diag(clang::diag::err_drv_no_input_files); |
| 484 | return; |
| 485 | } |
| 486 | |
| 487 | // Determine which compilation mode we are in. We look for options |
| 488 | // which affect the phase, starting with the earliest phases, and |
| 489 | // record which option we used to determine the final phase. |
Daniel Dunbar | ad2a9af | 2009-03-13 11:38:42 +0000 | [diff] [blame] | 490 | Arg *FinalPhaseArg = 0; |
| 491 | phases::ID FinalPhase; |
Daniel Dunbar | af61c71 | 2009-03-12 23:55:14 +0000 | [diff] [blame] | 492 | |
| 493 | // -{E,M,MM} only run the preprocessor. |
Daniel Dunbar | ad2a9af | 2009-03-13 11:38:42 +0000 | [diff] [blame] | 494 | if ((FinalPhaseArg = Args.getLastArg(options::OPT_E)) || |
| 495 | (FinalPhaseArg = Args.getLastArg(options::OPT_M)) || |
| 496 | (FinalPhaseArg = Args.getLastArg(options::OPT_MM))) { |
| 497 | FinalPhase = phases::Preprocess; |
Daniel Dunbar | af61c71 | 2009-03-12 23:55:14 +0000 | [diff] [blame] | 498 | |
Daniel Dunbar | 8022fd4 | 2009-03-15 00:48:16 +0000 | [diff] [blame] | 499 | // -{fsyntax-only,-analyze,emit-llvm,S} only run up to the compiler. |
| 500 | } else if ((FinalPhaseArg = Args.getLastArg(options::OPT_fsyntax_only)) || |
| 501 | (FinalPhaseArg = Args.getLastArg(options::OPT__analyze)) || |
| 502 | (FinalPhaseArg = Args.getLastArg(options::OPT_emit_llvm)) || |
Daniel Dunbar | ad2a9af | 2009-03-13 11:38:42 +0000 | [diff] [blame] | 503 | (FinalPhaseArg = Args.getLastArg(options::OPT_S))) { |
| 504 | FinalPhase = phases::Compile; |
Daniel Dunbar | af61c71 | 2009-03-12 23:55:14 +0000 | [diff] [blame] | 505 | |
| 506 | // -c only runs up to the assembler. |
Daniel Dunbar | ad2a9af | 2009-03-13 11:38:42 +0000 | [diff] [blame] | 507 | } else if ((FinalPhaseArg = Args.getLastArg(options::OPT_c))) { |
| 508 | FinalPhase = phases::Assemble; |
Daniel Dunbar | af61c71 | 2009-03-12 23:55:14 +0000 | [diff] [blame] | 509 | |
| 510 | // Otherwise do everything. |
| 511 | } else |
Daniel Dunbar | ad2a9af | 2009-03-13 11:38:42 +0000 | [diff] [blame] | 512 | FinalPhase = phases::Link; |
Daniel Dunbar | af61c71 | 2009-03-12 23:55:14 +0000 | [diff] [blame] | 513 | |
Daniel Dunbar | af61c71 | 2009-03-12 23:55:14 +0000 | [diff] [blame] | 514 | // Reject -Z* at the top level, these options should never have been |
| 515 | // exposed by gcc. |
| 516 | if (Arg *A = Args.getLastArg(options::OPT_Z)) |
| 517 | Diag(clang::diag::err_drv_use_of_Z_option) << A->getValue(Args); |
| 518 | |
Daniel Dunbar | ad2a9af | 2009-03-13 11:38:42 +0000 | [diff] [blame] | 519 | // Construct the actions to perform. |
| 520 | ActionList LinkerInputs; |
Daniel Dunbar | 53ec552 | 2009-03-12 07:58:46 +0000 | [diff] [blame] | 521 | for (unsigned i = 0, e = Inputs.size(); i != e; ++i) { |
Daniel Dunbar | ad2a9af | 2009-03-13 11:38:42 +0000 | [diff] [blame] | 522 | types::ID InputType = Inputs[i].first; |
| 523 | const Arg *InputArg = Inputs[i].second; |
| 524 | |
| 525 | unsigned NumSteps = types::getNumCompilationPhases(InputType); |
| 526 | assert(NumSteps && "Invalid number of steps!"); |
| 527 | |
| 528 | // If the first step comes after the final phase we are doing as |
| 529 | // part of this compilation, warn the user about it. |
| 530 | phases::ID InitialPhase = types::getCompilationPhase(InputType, 0); |
| 531 | if (InitialPhase > FinalPhase) { |
| 532 | Diag(clang::diag::warn_drv_input_file_unused) |
| 533 | << InputArg->getValue(Args) |
| 534 | << getPhaseName(InitialPhase) |
| 535 | << FinalPhaseArg->getOption().getName(); |
| 536 | continue; |
| 537 | } |
| 538 | |
| 539 | // Build the pipeline for this file. |
| 540 | Action *Current = new InputAction(*InputArg, InputType); |
| 541 | for (unsigned i = 0; i != NumSteps; ++i) { |
| 542 | phases::ID Phase = types::getCompilationPhase(InputType, i); |
| 543 | |
| 544 | // We are done if this step is past what the user requested. |
| 545 | if (Phase > FinalPhase) |
| 546 | break; |
| 547 | |
| 548 | // Queue linker inputs. |
| 549 | if (Phase == phases::Link) { |
| 550 | assert(i + 1 == NumSteps && "linking must be final compilation step."); |
| 551 | LinkerInputs.push_back(Current); |
| 552 | Current = 0; |
| 553 | break; |
| 554 | } |
| 555 | |
| 556 | // Otherwise construct the appropriate action. |
| 557 | Current = ConstructPhaseAction(Args, Phase, Current); |
| 558 | if (Current->getType() == types::TY_Nothing) |
| 559 | break; |
| 560 | } |
| 561 | |
| 562 | // If we ended with something, add to the output list. |
| 563 | if (Current) |
| 564 | Actions.push_back(Current); |
Daniel Dunbar | 53ec552 | 2009-03-12 07:58:46 +0000 | [diff] [blame] | 565 | } |
Daniel Dunbar | ad2a9af | 2009-03-13 11:38:42 +0000 | [diff] [blame] | 566 | |
| 567 | // Add a link action if necessary. |
| 568 | if (!LinkerInputs.empty()) |
| 569 | Actions.push_back(new LinkJobAction(LinkerInputs, types::TY_Image)); |
| 570 | } |
| 571 | |
| 572 | Action *Driver::ConstructPhaseAction(const ArgList &Args, phases::ID Phase, |
| 573 | Action *Input) const { |
Daniel Dunbar | 8f25c79 | 2009-03-18 01:38:48 +0000 | [diff] [blame] | 574 | llvm::PrettyStackTraceString CrashInfo("Constructing phase actions"); |
Daniel Dunbar | ad2a9af | 2009-03-13 11:38:42 +0000 | [diff] [blame] | 575 | // Build the appropriate action. |
| 576 | switch (Phase) { |
| 577 | case phases::Link: assert(0 && "link action invalid here."); |
| 578 | case phases::Preprocess: { |
| 579 | types::ID OutputTy = types::getPreprocessedType(Input->getType()); |
| 580 | assert(OutputTy != types::TY_INVALID && |
| 581 | "Cannot preprocess this input type!"); |
| 582 | return new PreprocessJobAction(Input, OutputTy); |
| 583 | } |
| 584 | case phases::Precompile: |
| 585 | return new PrecompileJobAction(Input, types::TY_PCH); |
| 586 | case phases::Compile: { |
| 587 | if (Args.hasArg(options::OPT_fsyntax_only)) { |
| 588 | return new CompileJobAction(Input, types::TY_Nothing); |
| 589 | } else if (Args.hasArg(options::OPT__analyze)) { |
| 590 | return new AnalyzeJobAction(Input, types::TY_Plist); |
| 591 | } else if (Args.hasArg(options::OPT_emit_llvm)) { |
| 592 | types::ID Output = |
| 593 | Args.hasArg(options::OPT_S) ? types::TY_LLVMAsm : types::TY_LLVMBC; |
| 594 | return new CompileJobAction(Input, Output); |
| 595 | } else { |
| 596 | return new CompileJobAction(Input, types::TY_PP_Asm); |
| 597 | } |
| 598 | } |
| 599 | case phases::Assemble: |
| 600 | return new AssembleJobAction(Input, types::TY_Object); |
| 601 | } |
| 602 | |
| 603 | assert(0 && "invalid phase in ConstructPhaseAction"); |
| 604 | return 0; |
Daniel Dunbar | 53ec552 | 2009-03-12 07:58:46 +0000 | [diff] [blame] | 605 | } |
| 606 | |
Daniel Dunbar | 2154923 | 2009-03-18 02:55:38 +0000 | [diff] [blame] | 607 | void Driver::BuildJobs(Compilation &C) const { |
Daniel Dunbar | 8f25c79 | 2009-03-18 01:38:48 +0000 | [diff] [blame] | 608 | llvm::PrettyStackTraceString CrashInfo("Building compilation jobs"); |
Daniel Dunbar | f353c8c | 2009-03-16 06:56:51 +0000 | [diff] [blame] | 609 | bool SaveTemps = C.getArgs().hasArg(options::OPT_save_temps); |
| 610 | bool UsePipes = C.getArgs().hasArg(options::OPT_pipe); |
| 611 | |
| 612 | // -save-temps inhibits pipes. |
| 613 | if (SaveTemps && UsePipes) { |
| 614 | Diag(clang::diag::warn_drv_pipe_ignored_with_save_temps); |
| 615 | UsePipes = true; |
| 616 | } |
| 617 | |
| 618 | Arg *FinalOutput = C.getArgs().getLastArg(options::OPT_o); |
| 619 | |
| 620 | // It is an error to provide a -o option if we are making multiple |
| 621 | // output files. |
| 622 | if (FinalOutput) { |
| 623 | unsigned NumOutputs = 0; |
Daniel Dunbar | 2154923 | 2009-03-18 02:55:38 +0000 | [diff] [blame] | 624 | for (ActionList::const_iterator it = C.getActions().begin(), |
| 625 | ie = C.getActions().end(); it != ie; ++it) |
Daniel Dunbar | f353c8c | 2009-03-16 06:56:51 +0000 | [diff] [blame] | 626 | if ((*it)->getType() != types::TY_Nothing) |
| 627 | ++NumOutputs; |
| 628 | |
| 629 | if (NumOutputs > 1) { |
| 630 | Diag(clang::diag::err_drv_output_argument_with_multiple_files); |
| 631 | FinalOutput = 0; |
| 632 | } |
| 633 | } |
| 634 | |
Daniel Dunbar | 2154923 | 2009-03-18 02:55:38 +0000 | [diff] [blame] | 635 | for (ActionList::const_iterator it = C.getActions().begin(), |
| 636 | ie = C.getActions().end(); it != ie; ++it) { |
Daniel Dunbar | f353c8c | 2009-03-16 06:56:51 +0000 | [diff] [blame] | 637 | Action *A = *it; |
| 638 | |
| 639 | // If we are linking an image for multiple archs then the linker |
| 640 | // wants -arch_multiple and -final_output <final image |
| 641 | // name>. Unfortunately, this doesn't fit in cleanly because we |
| 642 | // have to pass this information down. |
| 643 | // |
| 644 | // FIXME: This is a hack; find a cleaner way to integrate this |
| 645 | // into the process. |
| 646 | const char *LinkingOutput = 0; |
| 647 | if (isa<LinkJobAction>(A)) { |
| 648 | if (FinalOutput) |
| 649 | LinkingOutput = FinalOutput->getValue(C.getArgs()); |
| 650 | else |
| 651 | LinkingOutput = DefaultImageName.c_str(); |
| 652 | } |
| 653 | |
| 654 | InputInfo II; |
Daniel Dunbar | 10ffa9a | 2009-03-18 03:13:20 +0000 | [diff] [blame] | 655 | BuildJobsForAction(C, A, &C.getDefaultToolChain(), |
Daniel Dunbar | f353c8c | 2009-03-16 06:56:51 +0000 | [diff] [blame] | 656 | /*CanAcceptPipe*/ true, |
| 657 | /*AtTopLevel*/ true, |
| 658 | /*LinkingOutput*/ LinkingOutput, |
| 659 | II); |
| 660 | } |
Daniel Dunbar | 586dc23 | 2009-03-16 06:42:30 +0000 | [diff] [blame] | 661 | |
Daniel Dunbar | af2e4ba | 2009-03-18 18:03:46 +0000 | [diff] [blame] | 662 | // If there were errors, don't warn about any unused arguments. |
| 663 | if (Diags.getNumErrors()) |
| 664 | return; |
| 665 | |
Daniel Dunbar | 586dc23 | 2009-03-16 06:42:30 +0000 | [diff] [blame] | 666 | for (ArgList::const_iterator it = C.getArgs().begin(), ie = C.getArgs().end(); |
| 667 | it != ie; ++it) { |
| 668 | Arg *A = *it; |
Daniel Dunbar | af2e4ba | 2009-03-18 18:03:46 +0000 | [diff] [blame] | 669 | |
Daniel Dunbar | 586dc23 | 2009-03-16 06:42:30 +0000 | [diff] [blame] | 670 | // FIXME: It would be nice to be able to send the argument to the |
| 671 | // Diagnostic, so that extra values, position, and so on could be |
| 672 | // printed. |
| 673 | if (!A->isClaimed()) |
| 674 | Diag(clang::diag::warn_drv_unused_argument) |
| 675 | << A->getOption().getName(); |
| 676 | } |
Daniel Dunbar | 57b704d | 2009-03-13 22:12:33 +0000 | [diff] [blame] | 677 | } |
| 678 | |
Daniel Dunbar | f353c8c | 2009-03-16 06:56:51 +0000 | [diff] [blame] | 679 | void Driver::BuildJobsForAction(Compilation &C, |
| 680 | const Action *A, |
| 681 | const ToolChain *TC, |
| 682 | bool CanAcceptPipe, |
| 683 | bool AtTopLevel, |
| 684 | const char *LinkingOutput, |
| 685 | InputInfo &Result) const { |
Daniel Dunbar | 8f25c79 | 2009-03-18 01:38:48 +0000 | [diff] [blame] | 686 | llvm::PrettyStackTraceString CrashInfo("Building compilation jobs for action"); |
Daniel Dunbar | f353c8c | 2009-03-16 06:56:51 +0000 | [diff] [blame] | 687 | if (const InputAction *IA = dyn_cast<InputAction>(A)) { |
Daniel Dunbar | d09986a | 2009-03-18 08:02:40 +0000 | [diff] [blame] | 688 | // FIXME: This is broken, linker inputs won't work here. |
| 689 | assert(isa<PositionalArg>(IA->getInputArg()) && "FIXME: Linker inputs"); |
| 690 | |
Daniel Dunbar | 5ab483b | 2009-03-18 06:21:12 +0000 | [diff] [blame] | 691 | IA->getInputArg().claim(); |
Daniel Dunbar | f353c8c | 2009-03-16 06:56:51 +0000 | [diff] [blame] | 692 | const char *Name = IA->getInputArg().getValue(C.getArgs()); |
| 693 | Result = InputInfo(Name, A->getType(), Name); |
| 694 | return; |
| 695 | } |
| 696 | |
| 697 | if (const BindArchAction *BAA = dyn_cast<BindArchAction>(A)) { |
| 698 | const char *ArchName = BAA->getArchName(); |
Daniel Dunbar | 10ffa9a | 2009-03-18 03:13:20 +0000 | [diff] [blame] | 699 | if (!ArchName) |
| 700 | ArchName = C.getDefaultToolChain().getArchName().c_str(); |
Daniel Dunbar | f353c8c | 2009-03-16 06:56:51 +0000 | [diff] [blame] | 701 | BuildJobsForAction(C, |
| 702 | *BAA->begin(), |
| 703 | Host->getToolChain(C.getArgs(), ArchName), |
| 704 | CanAcceptPipe, |
| 705 | AtTopLevel, |
| 706 | LinkingOutput, |
| 707 | Result); |
| 708 | return; |
| 709 | } |
| 710 | |
| 711 | const JobAction *JA = cast<JobAction>(A); |
| 712 | const Tool &T = TC->SelectTool(C, *JA); |
| 713 | |
| 714 | // See if we should use an integrated preprocessor. We do so when we |
| 715 | // have exactly one input, since this is the only use case we care |
| 716 | // about (irrelevant since we don't support combine yet). |
| 717 | bool UseIntegratedCPP = false; |
| 718 | const ActionList *Inputs = &A->getInputs(); |
| 719 | if (Inputs->size() == 1 && isa<PreprocessJobAction>(*Inputs->begin())) { |
| 720 | if (!C.getArgs().hasArg(options::OPT_no_integrated_cpp) && |
| 721 | !C.getArgs().hasArg(options::OPT_traditional_cpp) && |
| 722 | !C.getArgs().hasArg(options::OPT_save_temps) && |
| 723 | T.hasIntegratedCPP()) { |
| 724 | UseIntegratedCPP = true; |
| 725 | Inputs = &(*Inputs)[0]->getInputs(); |
| 726 | } |
| 727 | } |
| 728 | |
| 729 | // Only use pipes when there is exactly one input. |
| 730 | bool TryToUsePipeInput = Inputs->size() == 1 && T.acceptsPipedInput(); |
Daniel Dunbar | 47ac7d2 | 2009-03-18 06:00:36 +0000 | [diff] [blame] | 731 | InputInfoList InputInfos; |
Daniel Dunbar | f353c8c | 2009-03-16 06:56:51 +0000 | [diff] [blame] | 732 | for (ActionList::const_iterator it = Inputs->begin(), ie = Inputs->end(); |
| 733 | it != ie; ++it) { |
| 734 | InputInfo II; |
| 735 | BuildJobsForAction(C, *it, TC, TryToUsePipeInput, |
| 736 | /*AtTopLevel*/false, |
| 737 | LinkingOutput, |
| 738 | II); |
| 739 | InputInfos.push_back(II); |
| 740 | } |
| 741 | |
| 742 | // Determine if we should output to a pipe. |
| 743 | bool OutputToPipe = false; |
| 744 | if (CanAcceptPipe && T.canPipeOutput()) { |
| 745 | // Some actions default to writing to a pipe if they are the top |
| 746 | // level phase and there was no user override. |
| 747 | // |
| 748 | // FIXME: Is there a better way to handle this? |
| 749 | if (AtTopLevel) { |
| 750 | if (isa<PreprocessJobAction>(A) && !C.getArgs().hasArg(options::OPT_o)) |
| 751 | OutputToPipe = true; |
| 752 | } else if (C.getArgs().hasArg(options::OPT_pipe)) |
| 753 | OutputToPipe = true; |
| 754 | } |
| 755 | |
| 756 | // Figure out where to put the job (pipes). |
| 757 | Job *Dest = &C.getJobs(); |
| 758 | if (InputInfos[0].isPipe()) { |
Daniel Dunbar | 441d060 | 2009-03-17 17:53:55 +0000 | [diff] [blame] | 759 | assert(TryToUsePipeInput && "Unrequested pipe!"); |
Daniel Dunbar | f353c8c | 2009-03-16 06:56:51 +0000 | [diff] [blame] | 760 | assert(InputInfos.size() == 1 && "Unexpected pipe with multiple inputs."); |
| 761 | Dest = &InputInfos[0].getPipe(); |
| 762 | } |
| 763 | |
| 764 | // Always use the first input as the base input. |
| 765 | const char *BaseInput = InputInfos[0].getBaseInput(); |
Daniel Dunbar | 441d060 | 2009-03-17 17:53:55 +0000 | [diff] [blame] | 766 | |
| 767 | // Determine the place to write output to (nothing, pipe, or |
| 768 | // filename) and where to put the new job. |
Daniel Dunbar | 441d060 | 2009-03-17 17:53:55 +0000 | [diff] [blame] | 769 | if (JA->getType() == types::TY_Nothing) { |
Daniel Dunbar | 5c3c1d7 | 2009-03-17 22:47:06 +0000 | [diff] [blame] | 770 | Result = InputInfo(A->getType(), BaseInput); |
Daniel Dunbar | 441d060 | 2009-03-17 17:53:55 +0000 | [diff] [blame] | 771 | } else if (OutputToPipe) { |
| 772 | // Append to current piped job or create a new one as appropriate. |
Daniel Dunbar | 5c3c1d7 | 2009-03-17 22:47:06 +0000 | [diff] [blame] | 773 | PipedJob *PJ = dyn_cast<PipedJob>(Dest); |
| 774 | if (!PJ) { |
| 775 | PJ = new PipedJob(); |
| 776 | cast<JobList>(Dest)->addJob(PJ); |
Daniel Dunbar | 871adcf | 2009-03-18 07:06:02 +0000 | [diff] [blame] | 777 | Dest = PJ; |
Daniel Dunbar | 441d060 | 2009-03-17 17:53:55 +0000 | [diff] [blame] | 778 | } |
Daniel Dunbar | 5c3c1d7 | 2009-03-17 22:47:06 +0000 | [diff] [blame] | 779 | Result = InputInfo(PJ, A->getType(), BaseInput); |
Daniel Dunbar | 441d060 | 2009-03-17 17:53:55 +0000 | [diff] [blame] | 780 | } else { |
Daniel Dunbar | 5c3c1d7 | 2009-03-17 22:47:06 +0000 | [diff] [blame] | 781 | Result = InputInfo(GetNamedOutputPath(C, *JA, BaseInput, AtTopLevel), |
| 782 | A->getType(), BaseInput); |
Daniel Dunbar | 441d060 | 2009-03-17 17:53:55 +0000 | [diff] [blame] | 783 | } |
| 784 | |
Daniel Dunbar | 5c3c1d7 | 2009-03-17 22:47:06 +0000 | [diff] [blame] | 785 | if (CCCPrintBindings) { |
| 786 | llvm::errs() << "bind - \"" << T.getName() << "\", inputs: ["; |
| 787 | for (unsigned i = 0, e = InputInfos.size(); i != e; ++i) { |
| 788 | llvm::errs() << InputInfos[i].getAsString(); |
| 789 | if (i + 1 != e) |
| 790 | llvm::errs() << ", "; |
| 791 | } |
| 792 | llvm::errs() << "], output: " << Result.getAsString() << "\n"; |
| 793 | } else { |
Daniel Dunbar | 47ac7d2 | 2009-03-18 06:00:36 +0000 | [diff] [blame] | 794 | const ArgList &TCArgs = C.getArgsForToolChain(TC); |
Daniel Dunbar | 871adcf | 2009-03-18 07:06:02 +0000 | [diff] [blame] | 795 | T.ConstructJob(C, *JA, *Dest, Result, InputInfos, TCArgs, LinkingOutput); |
Daniel Dunbar | 5c3c1d7 | 2009-03-17 22:47:06 +0000 | [diff] [blame] | 796 | } |
Daniel Dunbar | f353c8c | 2009-03-16 06:56:51 +0000 | [diff] [blame] | 797 | } |
| 798 | |
Daniel Dunbar | 441d060 | 2009-03-17 17:53:55 +0000 | [diff] [blame] | 799 | const char *Driver::GetNamedOutputPath(Compilation &C, |
| 800 | const JobAction &JA, |
| 801 | const char *BaseInput, |
| 802 | bool AtTopLevel) const { |
Daniel Dunbar | 8f25c79 | 2009-03-18 01:38:48 +0000 | [diff] [blame] | 803 | llvm::PrettyStackTraceString CrashInfo("Computing output path"); |
Daniel Dunbar | 441d060 | 2009-03-17 17:53:55 +0000 | [diff] [blame] | 804 | // Output to a user requested destination? |
| 805 | if (AtTopLevel) { |
| 806 | if (Arg *FinalOutput = C.getArgs().getLastArg(options::OPT_o)) |
| 807 | return C.addResultFile(FinalOutput->getValue(C.getArgs())); |
| 808 | } |
| 809 | |
| 810 | // Output to a temporary file? |
| 811 | if (!AtTopLevel && !C.getArgs().hasArg(options::OPT_save_temps)) { |
Daniel Dunbar | 214399e | 2009-03-18 19:34:39 +0000 | [diff] [blame] | 812 | std::string TmpName = |
| 813 | GetTemporaryPath(types::getTypeTempSuffix(JA.getType())); |
| 814 | return C.addTempFile(C.getArgs().MakeArgString(TmpName.c_str())); |
Daniel Dunbar | 441d060 | 2009-03-17 17:53:55 +0000 | [diff] [blame] | 815 | } |
| 816 | |
| 817 | llvm::sys::Path BasePath(BaseInput); |
Daniel Dunbar | 5796bf4 | 2009-03-18 02:00:31 +0000 | [diff] [blame] | 818 | std::string BaseName(BasePath.getLast()); |
Daniel Dunbar | 441d060 | 2009-03-17 17:53:55 +0000 | [diff] [blame] | 819 | |
| 820 | // Determine what the derived output name should be. |
| 821 | const char *NamedOutput; |
| 822 | if (JA.getType() == types::TY_Image) { |
| 823 | NamedOutput = DefaultImageName.c_str(); |
| 824 | } else { |
| 825 | const char *Suffix = types::getTypeTempSuffix(JA.getType()); |
| 826 | assert(Suffix && "All types used for output should have a suffix."); |
| 827 | |
| 828 | std::string::size_type End = std::string::npos; |
| 829 | if (!types::appendSuffixForType(JA.getType())) |
| 830 | End = BaseName.rfind('.'); |
| 831 | std::string Suffixed(BaseName.substr(0, End)); |
| 832 | Suffixed += '.'; |
| 833 | Suffixed += Suffix; |
| 834 | NamedOutput = C.getArgs().MakeArgString(Suffixed.c_str()); |
| 835 | } |
| 836 | |
| 837 | // As an annoying special case, PCH generation doesn't strip the |
| 838 | // pathname. |
| 839 | if (JA.getType() == types::TY_PCH) { |
| 840 | BasePath.eraseComponent(); |
Daniel Dunbar | 56c5594 | 2009-03-18 09:58:30 +0000 | [diff] [blame] | 841 | if (BasePath.isEmpty()) |
| 842 | BasePath = NamedOutput; |
| 843 | else |
| 844 | BasePath.appendComponent(NamedOutput); |
Daniel Dunbar | 441d060 | 2009-03-17 17:53:55 +0000 | [diff] [blame] | 845 | return C.addResultFile(C.getArgs().MakeArgString(BasePath.c_str())); |
| 846 | } else { |
| 847 | return C.addResultFile(NamedOutput); |
| 848 | } |
| 849 | } |
| 850 | |
Daniel Dunbar | 2ba38ba | 2009-03-16 05:25:36 +0000 | [diff] [blame] | 851 | llvm::sys::Path Driver::GetFilePath(const char *Name, |
Daniel Dunbar | 2154923 | 2009-03-18 02:55:38 +0000 | [diff] [blame] | 852 | const ToolChain &TC) const { |
Daniel Dunbar | 0edefeb | 2009-03-18 20:26:19 +0000 | [diff] [blame] | 853 | const ToolChain::path_list &List = TC.getFilePaths(); |
| 854 | for (ToolChain::path_list::const_iterator |
| 855 | it = List.begin(), ie = List.end(); it != ie; ++it) { |
| 856 | llvm::sys::Path P(*it); |
| 857 | P.appendComponent(Name); |
| 858 | if (P.exists()) |
| 859 | return P; |
| 860 | } |
| 861 | |
Daniel Dunbar | cb88167 | 2009-03-13 00:51:18 +0000 | [diff] [blame] | 862 | return llvm::sys::Path(Name); |
| 863 | } |
| 864 | |
Daniel Dunbar | 2ba38ba | 2009-03-16 05:25:36 +0000 | [diff] [blame] | 865 | llvm::sys::Path Driver::GetProgramPath(const char *Name, |
Daniel Dunbar | 2154923 | 2009-03-18 02:55:38 +0000 | [diff] [blame] | 866 | const ToolChain &TC) const { |
Daniel Dunbar | 0edefeb | 2009-03-18 20:26:19 +0000 | [diff] [blame] | 867 | const ToolChain::path_list &List = TC.getProgramPaths(); |
| 868 | for (ToolChain::path_list::const_iterator |
| 869 | it = List.begin(), ie = List.end(); it != ie; ++it) { |
| 870 | llvm::sys::Path P(*it); |
| 871 | P.appendComponent(Name); |
| 872 | if (P.exists()) |
| 873 | return P; |
| 874 | } |
| 875 | |
| 876 | // As a last resort, always search in our directory before pulling |
| 877 | // from the path. |
| 878 | llvm::sys::Path P(Dir); |
| 879 | P.appendComponent(Name); |
| 880 | if (P.exists()) |
| 881 | return P; |
| 882 | |
Daniel Dunbar | 632f50e | 2009-03-18 21:34:08 +0000 | [diff] [blame] | 883 | // Search path to increase accuracy of logging output. |
| 884 | P = llvm::sys::Program::FindProgramByName(Name); |
| 885 | if (!P.empty()) |
| 886 | return P; |
| 887 | |
Daniel Dunbar | cb88167 | 2009-03-13 00:51:18 +0000 | [diff] [blame] | 888 | return llvm::sys::Path(Name); |
| 889 | } |
| 890 | |
Daniel Dunbar | 214399e | 2009-03-18 19:34:39 +0000 | [diff] [blame] | 891 | std::string Driver::GetTemporaryPath(const char *Suffix) const { |
| 892 | // FIXME: This is lame; sys::Path should provide this function (in |
| 893 | // particular, it should know how to find the temporary files dir). |
| 894 | std::string Error; |
| 895 | llvm::sys::Path P("/tmp/cc"); |
| 896 | if (P.makeUnique(false, &Error)) { |
| 897 | Diag(clang::diag::err_drv_unable_to_make_temp) << Error; |
| 898 | return ""; |
| 899 | } |
| 900 | |
| 901 | P.appendSuffix(Suffix); |
| 902 | return P.toString(); |
| 903 | } |
| 904 | |
Daniel Dunbar | e504952 | 2009-03-17 20:45:45 +0000 | [diff] [blame] | 905 | const HostInfo *Driver::GetHostInfo(const char *Triple) const { |
Daniel Dunbar | 8f25c79 | 2009-03-18 01:38:48 +0000 | [diff] [blame] | 906 | llvm::PrettyStackTraceString CrashInfo("Constructing host"); |
Daniel Dunbar | dd98e2c | 2009-03-10 23:41:59 +0000 | [diff] [blame] | 907 | // Dice into arch, platform, and OS. This matches |
| 908 | // arch,platform,os = '(.*?)-(.*?)-(.*?)' |
| 909 | // and missing fields are left empty. |
| 910 | std::string Arch, Platform, OS; |
| 911 | |
| 912 | if (const char *ArchEnd = strchr(Triple, '-')) { |
| 913 | Arch = std::string(Triple, ArchEnd); |
| 914 | |
| 915 | if (const char *PlatformEnd = strchr(ArchEnd+1, '-')) { |
| 916 | Platform = std::string(ArchEnd+1, PlatformEnd); |
| 917 | OS = PlatformEnd+1; |
| 918 | } else |
| 919 | Platform = ArchEnd+1; |
| 920 | } else |
| 921 | Arch = Triple; |
| 922 | |
Daniel Dunbar | 1fd6c4b | 2009-03-17 19:00:50 +0000 | [diff] [blame] | 923 | // Normalize Arch a bit. |
| 924 | // |
| 925 | // FIXME: This is very incomplete. |
| 926 | if (Arch == "i686") |
| 927 | Arch = "i386"; |
| 928 | else if (Arch == "amd64") |
| 929 | Arch = "x86_64"; |
Daniel Dunbar | c811b6c | 2009-03-18 04:41:46 +0000 | [diff] [blame] | 930 | else if (Arch == "powerpc" || Arch == "Power Macintosh") |
| 931 | Arch = "ppc"; |
Daniel Dunbar | 1fd6c4b | 2009-03-17 19:00:50 +0000 | [diff] [blame] | 932 | |
Daniel Dunbar | a88162c | 2009-03-13 12:23:29 +0000 | [diff] [blame] | 933 | if (memcmp(&OS[0], "darwin", 6) == 0) |
Daniel Dunbar | e504952 | 2009-03-17 20:45:45 +0000 | [diff] [blame] | 934 | return createDarwinHostInfo(*this, Arch.c_str(), Platform.c_str(), |
| 935 | OS.c_str()); |
Daniel Dunbar | dd98e2c | 2009-03-10 23:41:59 +0000 | [diff] [blame] | 936 | |
Daniel Dunbar | e504952 | 2009-03-17 20:45:45 +0000 | [diff] [blame] | 937 | return createUnknownHostInfo(*this, Arch.c_str(), Platform.c_str(), |
| 938 | OS.c_str()); |
Daniel Dunbar | dd98e2c | 2009-03-10 23:41:59 +0000 | [diff] [blame] | 939 | } |