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