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