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