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