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 | 0648262 | 2009-03-05 06:38:47 +0000 | [diff] [blame] | 18 | #include "clang/Driver/Option.h" |
Daniel Dunbar | 1b3bb6e | 2009-03-04 20:49:20 +0000 | [diff] [blame] | 19 | #include "clang/Driver/Options.h" |
Daniel Dunbar | 53ec552 | 2009-03-12 07:58:46 +0000 | [diff] [blame] | 20 | #include "clang/Driver/Types.h" |
Daniel Dunbar | 0648262 | 2009-03-05 06:38:47 +0000 | [diff] [blame] | 21 | |
Daniel Dunbar | 2fe63e6 | 2009-03-12 18:40:18 +0000 | [diff] [blame] | 22 | #include "llvm/ADT/StringMap.h" |
Daniel Dunbar | 0648262 | 2009-03-05 06:38:47 +0000 | [diff] [blame] | 23 | #include "llvm/Support/raw_ostream.h" |
Daniel Dunbar | 53ec552 | 2009-03-12 07:58:46 +0000 | [diff] [blame] | 24 | #include "llvm/System/Path.h" |
Daniel Dunbar | ba10213 | 2009-03-13 12:19:02 +0000 | [diff] [blame] | 25 | |
| 26 | #include <map> |
| 27 | |
Daniel Dunbar | 1b3bb6e | 2009-03-04 20:49:20 +0000 | [diff] [blame] | 28 | using namespace clang::driver; |
| 29 | |
Daniel Dunbar | dd98e2c | 2009-03-10 23:41:59 +0000 | [diff] [blame] | 30 | Driver::Driver(const char *_Name, const char *_Dir, |
Daniel Dunbar | 4ad4b3e | 2009-03-12 08:55:43 +0000 | [diff] [blame] | 31 | const char *_DefaultHostTriple, |
| 32 | Diagnostic &_Diags) |
| 33 | : Opts(new OptTable()), Diags(_Diags), |
Daniel Dunbar | dd98e2c | 2009-03-10 23:41:59 +0000 | [diff] [blame] | 34 | Name(_Name), Dir(_Dir), DefaultHostTriple(_DefaultHostTriple), |
| 35 | Host(0), |
Daniel Dunbar | 365c02f | 2009-03-10 20:52:46 +0000 | [diff] [blame] | 36 | CCCIsCXX(false), CCCEcho(false), |
Daniel Dunbar | 8b1604e | 2009-03-13 00:17:48 +0000 | [diff] [blame] | 37 | CCCNoClang(false), CCCNoClangCXX(false), CCCNoClangCPP(false), |
| 38 | SuppressMissingInputWarning(false) |
Daniel Dunbar | 365c02f | 2009-03-10 20:52:46 +0000 | [diff] [blame] | 39 | { |
Daniel Dunbar | 3ede8d0 | 2009-03-02 19:59:07 +0000 | [diff] [blame] | 40 | } |
| 41 | |
| 42 | Driver::~Driver() { |
Daniel Dunbar | 1b3bb6e | 2009-03-04 20:49:20 +0000 | [diff] [blame] | 43 | delete Opts; |
Daniel Dunbar | 3ede8d0 | 2009-03-02 19:59:07 +0000 | [diff] [blame] | 44 | } |
| 45 | |
Daniel Dunbar | 0648262 | 2009-03-05 06:38:47 +0000 | [diff] [blame] | 46 | ArgList *Driver::ParseArgStrings(const char **ArgBegin, const char **ArgEnd) { |
| 47 | ArgList *Args = new ArgList(ArgBegin, ArgEnd); |
| 48 | |
Daniel Dunbar | ad2a9af | 2009-03-13 11:38:42 +0000 | [diff] [blame] | 49 | // FIXME: Handle '@' args (or at least error on them). |
| 50 | |
Daniel Dunbar | 0648262 | 2009-03-05 06:38:47 +0000 | [diff] [blame] | 51 | unsigned Index = 0, End = ArgEnd - ArgBegin; |
| 52 | while (Index < End) { |
Daniel Dunbar | 4139340 | 2009-03-13 01:01:44 +0000 | [diff] [blame] | 53 | // gcc's handling of empty arguments doesn't make |
| 54 | // sense, but this is not a common use case. :) |
| 55 | // |
| 56 | // We just ignore them here (note that other things may |
| 57 | // still take them as arguments). |
| 58 | if (Args->getArgString(Index)[0] == '\0') { |
| 59 | ++Index; |
| 60 | continue; |
| 61 | } |
| 62 | |
Daniel Dunbar | 0648262 | 2009-03-05 06:38:47 +0000 | [diff] [blame] | 63 | unsigned Prev = Index; |
| 64 | Arg *A = getOpts().ParseOneArg(*Args, Index, End); |
Daniel Dunbar | 53ec552 | 2009-03-12 07:58:46 +0000 | [diff] [blame] | 65 | if (A) { |
| 66 | if (A->getOption().isUnsupported()) { |
Daniel Dunbar | b897f5d | 2009-03-12 09:13:48 +0000 | [diff] [blame] | 67 | Diag(clang::diag::err_drv_unsupported_opt) << A->getOption().getName(); |
Daniel Dunbar | 53ec552 | 2009-03-12 07:58:46 +0000 | [diff] [blame] | 68 | continue; |
| 69 | } |
| 70 | |
Daniel Dunbar | 0648262 | 2009-03-05 06:38:47 +0000 | [diff] [blame] | 71 | Args->append(A); |
Daniel Dunbar | 53ec552 | 2009-03-12 07:58:46 +0000 | [diff] [blame] | 72 | } |
Daniel Dunbar | 0648262 | 2009-03-05 06:38:47 +0000 | [diff] [blame] | 73 | |
| 74 | assert(Index > Prev && "Parser failed to consume argument."); |
| 75 | } |
| 76 | |
| 77 | return Args; |
| 78 | } |
| 79 | |
Daniel Dunbar | 3ede8d0 | 2009-03-02 19:59:07 +0000 | [diff] [blame] | 80 | Compilation *Driver::BuildCompilation(int argc, const char **argv) { |
Daniel Dunbar | cb88167 | 2009-03-13 00:51:18 +0000 | [diff] [blame] | 81 | // FIXME: Handle environment options which effect driver behavior, |
| 82 | // somewhere (client?). GCC_EXEC_PREFIX, COMPILER_PATH, |
| 83 | // LIBRARY_PATH, LPATH, CC_PRINT_OPTIONS, QA_OVERRIDE_GCC3_OPTIONS. |
| 84 | |
| 85 | // FIXME: What are we going to do with -V and -b? |
| 86 | |
| 87 | // FIXME: Handle CCC_ADD_ARGS. |
| 88 | |
Daniel Dunbar | 365c02f | 2009-03-10 20:52:46 +0000 | [diff] [blame] | 89 | // FIXME: This stuff needs to go into the Compilation, not the |
| 90 | // driver. |
Daniel Dunbar | 53ec552 | 2009-03-12 07:58:46 +0000 | [diff] [blame] | 91 | bool CCCPrintOptions = false, CCCPrintActions = false; |
Daniel Dunbar | 0648262 | 2009-03-05 06:38:47 +0000 | [diff] [blame] | 92 | |
Daniel Dunbar | 365c02f | 2009-03-10 20:52:46 +0000 | [diff] [blame] | 93 | const char **Start = argv + 1, **End = argv + argc; |
Daniel Dunbar | dd98e2c | 2009-03-10 23:41:59 +0000 | [diff] [blame] | 94 | const char *HostTriple = DefaultHostTriple.c_str(); |
Daniel Dunbar | 365c02f | 2009-03-10 20:52:46 +0000 | [diff] [blame] | 95 | |
| 96 | // Read -ccc args. |
| 97 | // |
| 98 | // FIXME: We need to figure out where this behavior should |
| 99 | // live. Most of it should be outside in the client; the parts that |
| 100 | // aren't should have proper options, either by introducing new ones |
| 101 | // or by overloading gcc ones like -V or -b. |
| 102 | for (; Start != End && memcmp(*Start, "-ccc-", 5) == 0; ++Start) { |
| 103 | const char *Opt = *Start + 5; |
| 104 | |
| 105 | if (!strcmp(Opt, "print-options")) { |
| 106 | CCCPrintOptions = true; |
| 107 | } else if (!strcmp(Opt, "print-phases")) { |
Daniel Dunbar | 53ec552 | 2009-03-12 07:58:46 +0000 | [diff] [blame] | 108 | CCCPrintActions = true; |
Daniel Dunbar | 365c02f | 2009-03-10 20:52:46 +0000 | [diff] [blame] | 109 | } else if (!strcmp(Opt, "cxx")) { |
| 110 | CCCIsCXX = true; |
| 111 | } else if (!strcmp(Opt, "echo")) { |
| 112 | CCCEcho = true; |
| 113 | |
| 114 | } else if (!strcmp(Opt, "no-clang")) { |
| 115 | CCCNoClang = true; |
| 116 | } else if (!strcmp(Opt, "no-clang-cxx")) { |
| 117 | CCCNoClangCXX = true; |
| 118 | } else if (!strcmp(Opt, "no-clang-cpp")) { |
| 119 | CCCNoClangCPP = true; |
| 120 | } else if (!strcmp(Opt, "clang-archs")) { |
| 121 | assert(Start+1 < End && "FIXME: -ccc- argument handling."); |
| 122 | const char *Cur = *++Start; |
| 123 | |
| 124 | for (;;) { |
| 125 | const char *Next = strchr(Cur, ','); |
| 126 | |
| 127 | if (Next) { |
| 128 | CCCClangArchs.insert(std::string(Cur, Next)); |
| 129 | Cur = Next + 1; |
| 130 | } else { |
| 131 | CCCClangArchs.insert(std::string(Cur)); |
| 132 | break; |
| 133 | } |
| 134 | } |
| 135 | |
Daniel Dunbar | dd98e2c | 2009-03-10 23:41:59 +0000 | [diff] [blame] | 136 | } else if (!strcmp(Opt, "host-triple")) { |
Daniel Dunbar | 365c02f | 2009-03-10 20:52:46 +0000 | [diff] [blame] | 137 | assert(Start+1 < End && "FIXME: -ccc- argument handling."); |
Daniel Dunbar | dd98e2c | 2009-03-10 23:41:59 +0000 | [diff] [blame] | 138 | HostTriple = *++Start; |
Daniel Dunbar | 365c02f | 2009-03-10 20:52:46 +0000 | [diff] [blame] | 139 | |
| 140 | } else { |
| 141 | // FIXME: Error handling. |
| 142 | llvm::errs() << "invalid option: " << *Start << "\n"; |
| 143 | exit(1); |
| 144 | } |
| 145 | } |
Daniel Dunbar | dd98e2c | 2009-03-10 23:41:59 +0000 | [diff] [blame] | 146 | |
Daniel Dunbar | 365c02f | 2009-03-10 20:52:46 +0000 | [diff] [blame] | 147 | ArgList *Args = ParseArgStrings(Start, End); |
| 148 | |
Daniel Dunbar | cb88167 | 2009-03-13 00:51:18 +0000 | [diff] [blame] | 149 | Host = Driver::GetHostInfo(HostTriple); |
| 150 | DefaultToolChain = Host->getToolChain(*Args); |
| 151 | |
Daniel Dunbar | 365c02f | 2009-03-10 20:52:46 +0000 | [diff] [blame] | 152 | // FIXME: This behavior shouldn't be here. |
| 153 | if (CCCPrintOptions) { |
Daniel Dunbar | 53ec552 | 2009-03-12 07:58:46 +0000 | [diff] [blame] | 154 | PrintOptions(*Args); |
Daniel Dunbar | ab83543 | 2009-03-13 17:24:34 +0000 | [diff] [blame^] | 155 | return 0; |
Daniel Dunbar | 365c02f | 2009-03-10 20:52:46 +0000 | [diff] [blame] | 156 | } |
Daniel Dunbar | 53ec552 | 2009-03-12 07:58:46 +0000 | [diff] [blame] | 157 | |
Daniel Dunbar | cb88167 | 2009-03-13 00:51:18 +0000 | [diff] [blame] | 158 | if (!HandleImmediateArgs(*Args)) |
| 159 | return 0; |
| 160 | |
Daniel Dunbar | 53ec552 | 2009-03-12 07:58:46 +0000 | [diff] [blame] | 161 | // Construct the list of abstract actions to perform for this |
| 162 | // compilation. |
Daniel Dunbar | d65bddc | 2009-03-12 18:24:49 +0000 | [diff] [blame] | 163 | ActionList Actions; |
Daniel Dunbar | 53ec552 | 2009-03-12 07:58:46 +0000 | [diff] [blame] | 164 | if (Host->useDriverDriver()) |
| 165 | BuildUniversalActions(*Args, Actions); |
| 166 | else |
| 167 | BuildActions(*Args, Actions); |
| 168 | |
Daniel Dunbar | 53ec552 | 2009-03-12 07:58:46 +0000 | [diff] [blame] | 169 | if (CCCPrintActions) { |
Daniel Dunbar | ba10213 | 2009-03-13 12:19:02 +0000 | [diff] [blame] | 170 | PrintActions(*Args, Actions); |
Daniel Dunbar | ab83543 | 2009-03-13 17:24:34 +0000 | [diff] [blame^] | 171 | return 0; |
Daniel Dunbar | 53ec552 | 2009-03-12 07:58:46 +0000 | [diff] [blame] | 172 | } |
Daniel Dunbar | ab83543 | 2009-03-13 17:24:34 +0000 | [diff] [blame^] | 173 | |
| 174 | |
Daniel Dunbar | 365c02f | 2009-03-10 20:52:46 +0000 | [diff] [blame] | 175 | assert(0 && "FIXME: Implement"); |
| 176 | |
| 177 | return new Compilation(); |
| 178 | } |
| 179 | |
Daniel Dunbar | d65bddc | 2009-03-12 18:24:49 +0000 | [diff] [blame] | 180 | void Driver::PrintOptions(const ArgList &Args) const { |
Daniel Dunbar | 0648262 | 2009-03-05 06:38:47 +0000 | [diff] [blame] | 181 | unsigned i = 0; |
Daniel Dunbar | 53ec552 | 2009-03-12 07:58:46 +0000 | [diff] [blame] | 182 | for (ArgList::const_iterator it = Args.begin(), ie = Args.end(); |
Daniel Dunbar | 0648262 | 2009-03-05 06:38:47 +0000 | [diff] [blame] | 183 | it != ie; ++it, ++i) { |
| 184 | Arg *A = *it; |
| 185 | llvm::errs() << "Option " << i << " - " |
| 186 | << "Name: \"" << A->getOption().getName() << "\", " |
| 187 | << "Values: {"; |
| 188 | for (unsigned j = 0; j < A->getNumValues(); ++j) { |
| 189 | if (j) |
| 190 | llvm::errs() << ", "; |
Daniel Dunbar | 53ec552 | 2009-03-12 07:58:46 +0000 | [diff] [blame] | 191 | llvm::errs() << '"' << A->getValue(Args, j) << '"'; |
Daniel Dunbar | 0648262 | 2009-03-05 06:38:47 +0000 | [diff] [blame] | 192 | } |
| 193 | llvm::errs() << "}\n"; |
Daniel Dunbar | 0648262 | 2009-03-05 06:38:47 +0000 | [diff] [blame] | 194 | } |
Daniel Dunbar | 3ede8d0 | 2009-03-02 19:59:07 +0000 | [diff] [blame] | 195 | } |
Daniel Dunbar | dd98e2c | 2009-03-10 23:41:59 +0000 | [diff] [blame] | 196 | |
Daniel Dunbar | cb88167 | 2009-03-13 00:51:18 +0000 | [diff] [blame] | 197 | void Driver::PrintVersion() const { |
| 198 | // FIXME: Get a reasonable version number. |
| 199 | |
| 200 | // FIXME: The following handlers should use a callback mechanism, we |
| 201 | // don't know what the client would like to do. |
| 202 | llvm::outs() << "ccc version 1.0" << "\n"; |
| 203 | } |
| 204 | |
| 205 | bool Driver::HandleImmediateArgs(const ArgList &Args) { |
| 206 | // The order these options are handled in in gcc is all over the |
| 207 | // place, but we don't expect inconsistencies w.r.t. that to matter |
| 208 | // in practice. |
| 209 | if (Args.hasArg(options::OPT_v) || |
| 210 | Args.hasArg(options::OPT__HASH_HASH_HASH)) { |
| 211 | PrintVersion(); |
| 212 | SuppressMissingInputWarning = true; |
| 213 | } |
| 214 | |
| 215 | // FIXME: The following handlers should use a callback mechanism, we |
| 216 | // don't know what the client would like to do. |
| 217 | if (Arg *A = Args.getLastArg(options::OPT_print_file_name_EQ)) { |
| 218 | llvm::outs() << GetFilePath(A->getValue(Args)).toString() << "\n"; |
| 219 | return false; |
| 220 | } |
| 221 | |
| 222 | if (Arg *A = Args.getLastArg(options::OPT_print_prog_name_EQ)) { |
| 223 | llvm::outs() << GetProgramPath(A->getValue(Args)).toString() << "\n"; |
| 224 | return false; |
| 225 | } |
| 226 | |
Daniel Dunbar | 4139340 | 2009-03-13 01:01:44 +0000 | [diff] [blame] | 227 | if (Args.hasArg(options::OPT_print_libgcc_file_name)) { |
Daniel Dunbar | cb88167 | 2009-03-13 00:51:18 +0000 | [diff] [blame] | 228 | llvm::outs() << GetProgramPath("libgcc.a").toString() << "\n"; |
| 229 | return false; |
| 230 | } |
| 231 | |
| 232 | return true; |
| 233 | } |
| 234 | |
Daniel Dunbar | ba10213 | 2009-03-13 12:19:02 +0000 | [diff] [blame] | 235 | static unsigned PrintActions1(const ArgList &Args, |
| 236 | Action *A, |
| 237 | std::map<Action*, unsigned> &Ids) { |
| 238 | if (Ids.count(A)) |
| 239 | return Ids[A]; |
| 240 | |
| 241 | std::string str; |
| 242 | llvm::raw_string_ostream os(str); |
| 243 | |
| 244 | os << Action::getClassName(A->getKind()) << ", "; |
| 245 | if (InputAction *IA = dyn_cast<InputAction>(A)) { |
| 246 | os << IA->getInputArg().getValue(Args) << "\""; |
| 247 | } else if (BindArchAction *BIA = dyn_cast<BindArchAction>(A)) { |
| 248 | os << "\"" << BIA->getArchName() << "\", " |
| 249 | << "{" << PrintActions1(Args, *BIA->begin(), Ids) << "}"; |
| 250 | } else { |
| 251 | os << "{"; |
| 252 | for (Action::iterator it = A->begin(), ie = A->end(); it != ie;) { |
| 253 | os << PrintActions1(Args, *it, Ids); |
| 254 | ++it; |
| 255 | if (it != ie) |
| 256 | os << ", "; |
| 257 | } |
| 258 | os << "}"; |
| 259 | } |
| 260 | |
| 261 | unsigned Id = Ids.size(); |
| 262 | Ids[A] = Id; |
Daniel Dunbar | b269c32 | 2009-03-13 17:20:20 +0000 | [diff] [blame] | 263 | llvm::errs() << Id << ": " << os.str() << ", " |
Daniel Dunbar | ba10213 | 2009-03-13 12:19:02 +0000 | [diff] [blame] | 264 | << types::getTypeName(A->getType()) << "\n"; |
| 265 | |
| 266 | return Id; |
| 267 | } |
| 268 | |
| 269 | void Driver::PrintActions(const ArgList &Args, |
| 270 | const ActionList &Actions) const { |
| 271 | std::map<Action*, unsigned> Ids; |
| 272 | for (ActionList::const_iterator it = Actions.begin(), ie = Actions.end(); |
Daniel Dunbar | b269c32 | 2009-03-13 17:20:20 +0000 | [diff] [blame] | 273 | it != ie; ++it) |
Daniel Dunbar | ba10213 | 2009-03-13 12:19:02 +0000 | [diff] [blame] | 274 | PrintActions1(Args, *it, Ids); |
Daniel Dunbar | 53ec552 | 2009-03-12 07:58:46 +0000 | [diff] [blame] | 275 | } |
| 276 | |
Daniel Dunbar | d65bddc | 2009-03-12 18:24:49 +0000 | [diff] [blame] | 277 | void Driver::BuildUniversalActions(ArgList &Args, ActionList &Actions) { |
Daniel Dunbar | 2fe63e6 | 2009-03-12 18:40:18 +0000 | [diff] [blame] | 278 | llvm::StringMap<Arg *> Archs; |
| 279 | for (ArgList::const_iterator it = Args.begin(), ie = Args.end(); |
| 280 | it != ie; ++it) { |
| 281 | Arg *A = *it; |
| 282 | |
| 283 | if (A->getOption().getId() == options::OPT_arch) { |
| 284 | // FIXME: We need to handle canonicalization of the specified |
| 285 | // arch? |
| 286 | |
| 287 | Archs[A->getValue(Args)] = A; |
| 288 | } |
| 289 | } |
| 290 | |
| 291 | // When there is no explicit arch for this platform, get one from |
| 292 | // the host so that -Xarch_ is handled correctly. |
| 293 | if (!Archs.size()) { |
| 294 | const char *Arch = Host->getArchName().c_str(); |
| 295 | Archs[Arch] = Args.MakeSeparateArg(getOpts().getOption(options::OPT_arch), |
| 296 | Arch); |
| 297 | } |
| 298 | |
| 299 | // FIXME: We killed off some others but these aren't yet detected in |
| 300 | // a functional manner. If we added information to jobs about which |
| 301 | // "auxiliary" files they wrote then we could detect the conflict |
| 302 | // these cause downstream. |
| 303 | if (Archs.size() > 1) { |
| 304 | // No recovery needed, the point of this is just to prevent |
| 305 | // overwriting the same files. |
| 306 | if (const Arg *A = Args.getLastArg(options::OPT_M_Group)) |
| 307 | Diag(clang::diag::err_drv_invalid_opt_with_multiple_archs) |
| 308 | << A->getOption().getName(); |
| 309 | if (const Arg *A = Args.getLastArg(options::OPT_save_temps)) |
| 310 | Diag(clang::diag::err_drv_invalid_opt_with_multiple_archs) |
| 311 | << A->getOption().getName(); |
| 312 | } |
| 313 | |
| 314 | ActionList SingleActions; |
| 315 | BuildActions(Args, SingleActions); |
| 316 | |
| 317 | // Add in arch binding and lipo (if necessary) for every top level |
| 318 | // action. |
| 319 | for (unsigned i = 0, e = SingleActions.size(); i != e; ++i) { |
| 320 | Action *Act = SingleActions[i]; |
| 321 | |
| 322 | // Make sure we can lipo this kind of output. If not (and it is an |
| 323 | // actual output) then we disallow, since we can't create an |
| 324 | // output file with the right name without overwriting it. We |
| 325 | // could remove this oddity by just changing the output names to |
| 326 | // include the arch, which would also fix |
| 327 | // -save-temps. Compatibility wins for now. |
| 328 | |
| 329 | if (Archs.size() > 1 && types::canLipoType(Act->getType())) |
| 330 | Diag(clang::diag::err_drv_invalid_output_with_multiple_archs) |
| 331 | << types::getTypeName(Act->getType()); |
| 332 | |
| 333 | ActionList Inputs; |
| 334 | for (llvm::StringMap<Arg*>::iterator it = Archs.begin(), ie = Archs.end(); |
| 335 | it != ie; ++it) |
| 336 | Inputs.push_back(new BindArchAction(Act, it->second->getValue(Args))); |
| 337 | |
| 338 | // Lipo if necessary, We do it this way because we need to set the |
| 339 | // arch flag so that -Xarch_ gets overwritten. |
| 340 | if (Inputs.size() == 1 || Act->getType() == types::TY_Nothing) |
| 341 | Actions.append(Inputs.begin(), Inputs.end()); |
| 342 | else |
| 343 | Actions.push_back(new LipoJobAction(Inputs, Act->getType())); |
| 344 | } |
Daniel Dunbar | 53ec552 | 2009-03-12 07:58:46 +0000 | [diff] [blame] | 345 | } |
| 346 | |
Daniel Dunbar | d65bddc | 2009-03-12 18:24:49 +0000 | [diff] [blame] | 347 | void Driver::BuildActions(ArgList &Args, ActionList &Actions) { |
Daniel Dunbar | 53ec552 | 2009-03-12 07:58:46 +0000 | [diff] [blame] | 348 | types::ID InputType = types::TY_INVALID; |
| 349 | Arg *InputTypeArg = 0; |
Daniel Dunbar | af61c71 | 2009-03-12 23:55:14 +0000 | [diff] [blame] | 350 | |
| 351 | // Start by constructing the list of inputs and their types. |
| 352 | |
Daniel Dunbar | 53ec552 | 2009-03-12 07:58:46 +0000 | [diff] [blame] | 353 | llvm::SmallVector<std::pair<types::ID, const Arg*>, 16> Inputs; |
| 354 | for (ArgList::const_iterator it = Args.begin(), ie = Args.end(); |
| 355 | it != ie; ++it) { |
| 356 | Arg *A = *it; |
| 357 | |
| 358 | if (isa<InputOption>(A->getOption())) { |
| 359 | const char *Value = A->getValue(Args); |
| 360 | types::ID Ty = types::TY_INVALID; |
| 361 | |
| 362 | // Infer the input type if necessary. |
Daniel Dunbar | 4ad4b3e | 2009-03-12 08:55:43 +0000 | [diff] [blame] | 363 | if (InputType == types::TY_INVALID) { |
Daniel Dunbar | 53ec552 | 2009-03-12 07:58:46 +0000 | [diff] [blame] | 364 | // stdin must be handled specially. |
| 365 | if (memcmp(Value, "-", 2) == 0) { |
| 366 | // If running with -E, treat as a C input (this changes the |
| 367 | // builtin macros, for example). This may be overridden by |
| 368 | // -ObjC below. |
| 369 | // |
| 370 | // Otherwise emit an error but still use a valid type to |
| 371 | // avoid spurious errors (e.g., no inputs). |
| 372 | if (!Args.hasArg(options::OPT_E)) |
Daniel Dunbar | b897f5d | 2009-03-12 09:13:48 +0000 | [diff] [blame] | 373 | Diag(clang::diag::err_drv_unknown_stdin_type); |
Daniel Dunbar | 53ec552 | 2009-03-12 07:58:46 +0000 | [diff] [blame] | 374 | Ty = types::TY_C; |
| 375 | } else { |
| 376 | // Otherwise lookup by extension, and fallback to ObjectType |
| 377 | // if not found. |
| 378 | if (const char *Ext = strrchr(Value, '.')) |
| 379 | Ty = types::lookupTypeForExtension(Ext + 1); |
| 380 | if (Ty == types::TY_INVALID) |
| 381 | Ty = types::TY_Object; |
| 382 | } |
| 383 | |
| 384 | // -ObjC and -ObjC++ override the default language, but only |
| 385 | // -for "source files". We just treat everything that isn't a |
| 386 | // -linker input as a source file. |
| 387 | // |
| 388 | // FIXME: Clean this up if we move the phase sequence into the |
| 389 | // type. |
| 390 | if (Ty != types::TY_Object) { |
| 391 | if (Args.hasArg(options::OPT_ObjC)) |
| 392 | Ty = types::TY_ObjC; |
| 393 | else if (Args.hasArg(options::OPT_ObjCXX)) |
| 394 | Ty = types::TY_ObjCXX; |
| 395 | } |
| 396 | } else { |
| 397 | assert(InputTypeArg && "InputType set w/o InputTypeArg"); |
| 398 | InputTypeArg->claim(); |
| 399 | Ty = InputType; |
| 400 | } |
| 401 | |
| 402 | // Check that the file exists. It isn't clear this is worth |
| 403 | // doing, since the tool presumably does this anyway, and this |
| 404 | // just adds an extra stat to the equation, but this is gcc |
| 405 | // compatible. |
| 406 | if (memcmp(Value, "-", 2) != 0 && !llvm::sys::Path(Value).exists()) |
Daniel Dunbar | b897f5d | 2009-03-12 09:13:48 +0000 | [diff] [blame] | 407 | Diag(clang::diag::err_drv_no_such_file) << A->getValue(Args); |
Daniel Dunbar | 53ec552 | 2009-03-12 07:58:46 +0000 | [diff] [blame] | 408 | else |
| 409 | Inputs.push_back(std::make_pair(Ty, A)); |
| 410 | |
| 411 | } else if (A->getOption().isLinkerInput()) { |
| 412 | // Just treat as object type, we could make a special type for |
| 413 | // this if necessary. |
| 414 | Inputs.push_back(std::make_pair(types::TY_Object, A)); |
| 415 | |
| 416 | } else if (A->getOption().getId() == options::OPT_x) { |
| 417 | InputTypeArg = A; |
| 418 | InputType = types::lookupTypeForTypeSpecifier(A->getValue(Args)); |
| 419 | |
| 420 | // Follow gcc behavior and treat as linker input for invalid -x |
| 421 | // options. Its not clear why we shouldn't just revert to |
| 422 | // unknown; but this isn't very important, we might as well be |
| 423 | // bug comatible. |
| 424 | if (!InputType) { |
Daniel Dunbar | b897f5d | 2009-03-12 09:13:48 +0000 | [diff] [blame] | 425 | Diag(clang::diag::err_drv_unknown_language) << A->getValue(Args); |
Daniel Dunbar | 53ec552 | 2009-03-12 07:58:46 +0000 | [diff] [blame] | 426 | InputType = types::TY_Object; |
| 427 | } |
| 428 | } |
| 429 | } |
| 430 | |
Daniel Dunbar | 8b1604e | 2009-03-13 00:17:48 +0000 | [diff] [blame] | 431 | if (!SuppressMissingInputWarning && Inputs.empty()) { |
Daniel Dunbar | af61c71 | 2009-03-12 23:55:14 +0000 | [diff] [blame] | 432 | Diag(clang::diag::err_drv_no_input_files); |
| 433 | return; |
| 434 | } |
| 435 | |
| 436 | // Determine which compilation mode we are in. We look for options |
| 437 | // which affect the phase, starting with the earliest phases, and |
| 438 | // record which option we used to determine the final phase. |
Daniel Dunbar | ad2a9af | 2009-03-13 11:38:42 +0000 | [diff] [blame] | 439 | Arg *FinalPhaseArg = 0; |
| 440 | phases::ID FinalPhase; |
Daniel Dunbar | af61c71 | 2009-03-12 23:55:14 +0000 | [diff] [blame] | 441 | |
| 442 | // -{E,M,MM} only run the preprocessor. |
Daniel Dunbar | ad2a9af | 2009-03-13 11:38:42 +0000 | [diff] [blame] | 443 | if ((FinalPhaseArg = Args.getLastArg(options::OPT_E)) || |
| 444 | (FinalPhaseArg = Args.getLastArg(options::OPT_M)) || |
| 445 | (FinalPhaseArg = Args.getLastArg(options::OPT_MM))) { |
| 446 | FinalPhase = phases::Preprocess; |
Daniel Dunbar | af61c71 | 2009-03-12 23:55:14 +0000 | [diff] [blame] | 447 | |
| 448 | // -{-analyze,fsyntax-only,S} only run up to the compiler. |
Daniel Dunbar | ad2a9af | 2009-03-13 11:38:42 +0000 | [diff] [blame] | 449 | } else if ((FinalPhaseArg = Args.getLastArg(options::OPT__analyze)) || |
| 450 | (FinalPhaseArg = Args.getLastArg(options::OPT_fsyntax_only)) || |
| 451 | (FinalPhaseArg = Args.getLastArg(options::OPT_S))) { |
| 452 | FinalPhase = phases::Compile; |
Daniel Dunbar | af61c71 | 2009-03-12 23:55:14 +0000 | [diff] [blame] | 453 | |
| 454 | // -c only runs up to the assembler. |
Daniel Dunbar | ad2a9af | 2009-03-13 11:38:42 +0000 | [diff] [blame] | 455 | } else if ((FinalPhaseArg = Args.getLastArg(options::OPT_c))) { |
| 456 | FinalPhase = phases::Assemble; |
Daniel Dunbar | af61c71 | 2009-03-12 23:55:14 +0000 | [diff] [blame] | 457 | |
| 458 | // Otherwise do everything. |
| 459 | } else |
Daniel Dunbar | ad2a9af | 2009-03-13 11:38:42 +0000 | [diff] [blame] | 460 | FinalPhase = phases::Link; |
Daniel Dunbar | af61c71 | 2009-03-12 23:55:14 +0000 | [diff] [blame] | 461 | |
Daniel Dunbar | ad2a9af | 2009-03-13 11:38:42 +0000 | [diff] [blame] | 462 | if (FinalPhaseArg) |
| 463 | FinalPhaseArg->claim(); |
Daniel Dunbar | af61c71 | 2009-03-12 23:55:14 +0000 | [diff] [blame] | 464 | |
| 465 | // Reject -Z* at the top level, these options should never have been |
| 466 | // exposed by gcc. |
| 467 | if (Arg *A = Args.getLastArg(options::OPT_Z)) |
| 468 | Diag(clang::diag::err_drv_use_of_Z_option) << A->getValue(Args); |
| 469 | |
Daniel Dunbar | ad2a9af | 2009-03-13 11:38:42 +0000 | [diff] [blame] | 470 | // Construct the actions to perform. |
| 471 | ActionList LinkerInputs; |
Daniel Dunbar | 53ec552 | 2009-03-12 07:58:46 +0000 | [diff] [blame] | 472 | for (unsigned i = 0, e = Inputs.size(); i != e; ++i) { |
Daniel Dunbar | ad2a9af | 2009-03-13 11:38:42 +0000 | [diff] [blame] | 473 | types::ID InputType = Inputs[i].first; |
| 474 | const Arg *InputArg = Inputs[i].second; |
| 475 | |
| 476 | unsigned NumSteps = types::getNumCompilationPhases(InputType); |
| 477 | assert(NumSteps && "Invalid number of steps!"); |
| 478 | |
| 479 | // If the first step comes after the final phase we are doing as |
| 480 | // part of this compilation, warn the user about it. |
| 481 | phases::ID InitialPhase = types::getCompilationPhase(InputType, 0); |
| 482 | if (InitialPhase > FinalPhase) { |
| 483 | Diag(clang::diag::warn_drv_input_file_unused) |
| 484 | << InputArg->getValue(Args) |
| 485 | << getPhaseName(InitialPhase) |
| 486 | << FinalPhaseArg->getOption().getName(); |
| 487 | continue; |
| 488 | } |
| 489 | |
| 490 | // Build the pipeline for this file. |
| 491 | Action *Current = new InputAction(*InputArg, InputType); |
| 492 | for (unsigned i = 0; i != NumSteps; ++i) { |
| 493 | phases::ID Phase = types::getCompilationPhase(InputType, i); |
| 494 | |
| 495 | // We are done if this step is past what the user requested. |
| 496 | if (Phase > FinalPhase) |
| 497 | break; |
| 498 | |
| 499 | // Queue linker inputs. |
| 500 | if (Phase == phases::Link) { |
| 501 | assert(i + 1 == NumSteps && "linking must be final compilation step."); |
| 502 | LinkerInputs.push_back(Current); |
| 503 | Current = 0; |
| 504 | break; |
| 505 | } |
| 506 | |
| 507 | // Otherwise construct the appropriate action. |
| 508 | Current = ConstructPhaseAction(Args, Phase, Current); |
| 509 | if (Current->getType() == types::TY_Nothing) |
| 510 | break; |
| 511 | } |
| 512 | |
| 513 | // If we ended with something, add to the output list. |
| 514 | if (Current) |
| 515 | Actions.push_back(Current); |
Daniel Dunbar | 53ec552 | 2009-03-12 07:58:46 +0000 | [diff] [blame] | 516 | } |
Daniel Dunbar | ad2a9af | 2009-03-13 11:38:42 +0000 | [diff] [blame] | 517 | |
| 518 | // Add a link action if necessary. |
| 519 | if (!LinkerInputs.empty()) |
| 520 | Actions.push_back(new LinkJobAction(LinkerInputs, types::TY_Image)); |
| 521 | } |
| 522 | |
| 523 | Action *Driver::ConstructPhaseAction(const ArgList &Args, phases::ID Phase, |
| 524 | Action *Input) const { |
| 525 | // Build the appropriate action. |
| 526 | switch (Phase) { |
| 527 | case phases::Link: assert(0 && "link action invalid here."); |
| 528 | case phases::Preprocess: { |
| 529 | types::ID OutputTy = types::getPreprocessedType(Input->getType()); |
| 530 | assert(OutputTy != types::TY_INVALID && |
| 531 | "Cannot preprocess this input type!"); |
| 532 | return new PreprocessJobAction(Input, OutputTy); |
| 533 | } |
| 534 | case phases::Precompile: |
| 535 | return new PrecompileJobAction(Input, types::TY_PCH); |
| 536 | case phases::Compile: { |
| 537 | if (Args.hasArg(options::OPT_fsyntax_only)) { |
| 538 | return new CompileJobAction(Input, types::TY_Nothing); |
| 539 | } else if (Args.hasArg(options::OPT__analyze)) { |
| 540 | return new AnalyzeJobAction(Input, types::TY_Plist); |
| 541 | } else if (Args.hasArg(options::OPT_emit_llvm)) { |
| 542 | types::ID Output = |
| 543 | Args.hasArg(options::OPT_S) ? types::TY_LLVMAsm : types::TY_LLVMBC; |
| 544 | return new CompileJobAction(Input, Output); |
| 545 | } else { |
| 546 | return new CompileJobAction(Input, types::TY_PP_Asm); |
| 547 | } |
| 548 | } |
| 549 | case phases::Assemble: |
| 550 | return new AssembleJobAction(Input, types::TY_Object); |
| 551 | } |
| 552 | |
| 553 | assert(0 && "invalid phase in ConstructPhaseAction"); |
| 554 | return 0; |
Daniel Dunbar | 53ec552 | 2009-03-12 07:58:46 +0000 | [diff] [blame] | 555 | } |
| 556 | |
Daniel Dunbar | cb88167 | 2009-03-13 00:51:18 +0000 | [diff] [blame] | 557 | llvm::sys::Path Driver::GetFilePath(const char *Name) const { |
| 558 | // FIXME: Implement. |
| 559 | return llvm::sys::Path(Name); |
| 560 | } |
| 561 | |
| 562 | llvm::sys::Path Driver::GetProgramPath(const char *Name) const { |
| 563 | // FIXME: Implement. |
| 564 | return llvm::sys::Path(Name); |
| 565 | } |
| 566 | |
Daniel Dunbar | dd98e2c | 2009-03-10 23:41:59 +0000 | [diff] [blame] | 567 | HostInfo *Driver::GetHostInfo(const char *Triple) { |
| 568 | // Dice into arch, platform, and OS. This matches |
| 569 | // arch,platform,os = '(.*?)-(.*?)-(.*?)' |
| 570 | // and missing fields are left empty. |
| 571 | std::string Arch, Platform, OS; |
| 572 | |
| 573 | if (const char *ArchEnd = strchr(Triple, '-')) { |
| 574 | Arch = std::string(Triple, ArchEnd); |
| 575 | |
| 576 | if (const char *PlatformEnd = strchr(ArchEnd+1, '-')) { |
| 577 | Platform = std::string(ArchEnd+1, PlatformEnd); |
| 578 | OS = PlatformEnd+1; |
| 579 | } else |
| 580 | Platform = ArchEnd+1; |
| 581 | } else |
| 582 | Arch = Triple; |
| 583 | |
Daniel Dunbar | a88162c | 2009-03-13 12:23:29 +0000 | [diff] [blame] | 584 | if (memcmp(&OS[0], "darwin", 6) == 0) |
Daniel Dunbar | dd98e2c | 2009-03-10 23:41:59 +0000 | [diff] [blame] | 585 | return new DarwinHostInfo(Arch.c_str(), Platform.c_str(), OS.c_str()); |
| 586 | |
| 587 | return new UnknownHostInfo(Arch.c_str(), Platform.c_str(), OS.c_str()); |
| 588 | } |