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 | d25acaa | 2009-03-10 23:41:59 +0000 | [diff] [blame] | 16 | #include "clang/Driver/HostInfo.h" |
Daniel Dunbar | 7dc2a04 | 2009-03-05 06:38:47 +0000 | [diff] [blame] | 17 | #include "clang/Driver/Option.h" |
Daniel Dunbar | d6f0e37 | 2009-03-04 20:49:20 +0000 | [diff] [blame] | 18 | #include "clang/Driver/Options.h" |
Daniel Dunbar | db62cc3 | 2009-03-12 07:58:46 +0000 | [diff] [blame] | 19 | #include "clang/Driver/Types.h" |
Daniel Dunbar | 7dc2a04 | 2009-03-05 06:38:47 +0000 | [diff] [blame] | 20 | |
| 21 | #include "llvm/Support/raw_ostream.h" |
Daniel Dunbar | db62cc3 | 2009-03-12 07:58:46 +0000 | [diff] [blame] | 22 | #include "llvm/System/Path.h" |
Daniel Dunbar | d6f0e37 | 2009-03-04 20:49:20 +0000 | [diff] [blame] | 23 | using namespace clang::driver; |
| 24 | |
Daniel Dunbar | d25acaa | 2009-03-10 23:41:59 +0000 | [diff] [blame] | 25 | Driver::Driver(const char *_Name, const char *_Dir, |
| 26 | const char *_DefaultHostTriple) |
Daniel Dunbar | b282ced | 2009-03-10 20:52:46 +0000 | [diff] [blame] | 27 | : Opts(new OptTable()), |
Daniel Dunbar | d25acaa | 2009-03-10 23:41:59 +0000 | [diff] [blame] | 28 | Name(_Name), Dir(_Dir), DefaultHostTriple(_DefaultHostTriple), |
| 29 | Host(0), |
Daniel Dunbar | b282ced | 2009-03-10 20:52:46 +0000 | [diff] [blame] | 30 | CCCIsCXX(false), CCCEcho(false), |
| 31 | CCCNoClang(false), CCCNoClangCXX(false), CCCNoClangCPP(false) |
| 32 | { |
Daniel Dunbar | d6f0e37 | 2009-03-04 20:49:20 +0000 | [diff] [blame] | 33 | |
Daniel Dunbar | 63c4da9 | 2009-03-02 19:59:07 +0000 | [diff] [blame] | 34 | } |
| 35 | |
| 36 | Driver::~Driver() { |
Daniel Dunbar | d6f0e37 | 2009-03-04 20:49:20 +0000 | [diff] [blame] | 37 | delete Opts; |
Daniel Dunbar | 63c4da9 | 2009-03-02 19:59:07 +0000 | [diff] [blame] | 38 | } |
| 39 | |
Daniel Dunbar | 7dc2a04 | 2009-03-05 06:38:47 +0000 | [diff] [blame] | 40 | ArgList *Driver::ParseArgStrings(const char **ArgBegin, const char **ArgEnd) { |
| 41 | ArgList *Args = new ArgList(ArgBegin, ArgEnd); |
| 42 | |
| 43 | unsigned Index = 0, End = ArgEnd - ArgBegin; |
| 44 | while (Index < End) { |
| 45 | unsigned Prev = Index; |
| 46 | Arg *A = getOpts().ParseOneArg(*Args, Index, End); |
Daniel Dunbar | db62cc3 | 2009-03-12 07:58:46 +0000 | [diff] [blame] | 47 | if (A) { |
| 48 | if (A->getOption().isUnsupported()) { |
| 49 | Diag("unsupported option: ") << A->getOption().getName() << "\n"; |
| 50 | continue; |
| 51 | } |
| 52 | |
Daniel Dunbar | 7dc2a04 | 2009-03-05 06:38:47 +0000 | [diff] [blame] | 53 | Args->append(A); |
Daniel Dunbar | db62cc3 | 2009-03-12 07:58:46 +0000 | [diff] [blame] | 54 | } |
Daniel Dunbar | 7dc2a04 | 2009-03-05 06:38:47 +0000 | [diff] [blame] | 55 | |
| 56 | assert(Index > Prev && "Parser failed to consume argument."); |
| 57 | } |
| 58 | |
| 59 | return Args; |
| 60 | } |
| 61 | |
Daniel Dunbar | 63c4da9 | 2009-03-02 19:59:07 +0000 | [diff] [blame] | 62 | Compilation *Driver::BuildCompilation(int argc, const char **argv) { |
Daniel Dunbar | b282ced | 2009-03-10 20:52:46 +0000 | [diff] [blame] | 63 | // FIXME: This stuff needs to go into the Compilation, not the |
| 64 | // driver. |
Daniel Dunbar | db62cc3 | 2009-03-12 07:58:46 +0000 | [diff] [blame] | 65 | bool CCCPrintOptions = false, CCCPrintActions = false; |
Daniel Dunbar | 7dc2a04 | 2009-03-05 06:38:47 +0000 | [diff] [blame] | 66 | |
Daniel Dunbar | b282ced | 2009-03-10 20:52:46 +0000 | [diff] [blame] | 67 | const char **Start = argv + 1, **End = argv + argc; |
Daniel Dunbar | d25acaa | 2009-03-10 23:41:59 +0000 | [diff] [blame] | 68 | const char *HostTriple = DefaultHostTriple.c_str(); |
Daniel Dunbar | b282ced | 2009-03-10 20:52:46 +0000 | [diff] [blame] | 69 | |
| 70 | // Read -ccc args. |
| 71 | // |
| 72 | // FIXME: We need to figure out where this behavior should |
| 73 | // live. Most of it should be outside in the client; the parts that |
| 74 | // aren't should have proper options, either by introducing new ones |
| 75 | // or by overloading gcc ones like -V or -b. |
| 76 | for (; Start != End && memcmp(*Start, "-ccc-", 5) == 0; ++Start) { |
| 77 | const char *Opt = *Start + 5; |
| 78 | |
| 79 | if (!strcmp(Opt, "print-options")) { |
| 80 | CCCPrintOptions = true; |
| 81 | } else if (!strcmp(Opt, "print-phases")) { |
Daniel Dunbar | db62cc3 | 2009-03-12 07:58:46 +0000 | [diff] [blame] | 82 | CCCPrintActions = true; |
Daniel Dunbar | b282ced | 2009-03-10 20:52:46 +0000 | [diff] [blame] | 83 | } else if (!strcmp(Opt, "cxx")) { |
| 84 | CCCIsCXX = true; |
| 85 | } else if (!strcmp(Opt, "echo")) { |
| 86 | CCCEcho = true; |
| 87 | |
| 88 | } else if (!strcmp(Opt, "no-clang")) { |
| 89 | CCCNoClang = true; |
| 90 | } else if (!strcmp(Opt, "no-clang-cxx")) { |
| 91 | CCCNoClangCXX = true; |
| 92 | } else if (!strcmp(Opt, "no-clang-cpp")) { |
| 93 | CCCNoClangCPP = true; |
| 94 | } else if (!strcmp(Opt, "clang-archs")) { |
| 95 | assert(Start+1 < End && "FIXME: -ccc- argument handling."); |
| 96 | const char *Cur = *++Start; |
| 97 | |
| 98 | for (;;) { |
| 99 | const char *Next = strchr(Cur, ','); |
| 100 | |
| 101 | if (Next) { |
| 102 | CCCClangArchs.insert(std::string(Cur, Next)); |
| 103 | Cur = Next + 1; |
| 104 | } else { |
| 105 | CCCClangArchs.insert(std::string(Cur)); |
| 106 | break; |
| 107 | } |
| 108 | } |
| 109 | |
Daniel Dunbar | d25acaa | 2009-03-10 23:41:59 +0000 | [diff] [blame] | 110 | } else if (!strcmp(Opt, "host-triple")) { |
Daniel Dunbar | b282ced | 2009-03-10 20:52:46 +0000 | [diff] [blame] | 111 | assert(Start+1 < End && "FIXME: -ccc- argument handling."); |
Daniel Dunbar | d25acaa | 2009-03-10 23:41:59 +0000 | [diff] [blame] | 112 | HostTriple = *++Start; |
Daniel Dunbar | b282ced | 2009-03-10 20:52:46 +0000 | [diff] [blame] | 113 | |
| 114 | } else { |
| 115 | // FIXME: Error handling. |
| 116 | llvm::errs() << "invalid option: " << *Start << "\n"; |
| 117 | exit(1); |
| 118 | } |
| 119 | } |
Daniel Dunbar | d25acaa | 2009-03-10 23:41:59 +0000 | [diff] [blame] | 120 | |
| 121 | Host = Driver::GetHostInfo(HostTriple); |
| 122 | |
Daniel Dunbar | b282ced | 2009-03-10 20:52:46 +0000 | [diff] [blame] | 123 | ArgList *Args = ParseArgStrings(Start, End); |
| 124 | |
| 125 | // FIXME: This behavior shouldn't be here. |
| 126 | if (CCCPrintOptions) { |
Daniel Dunbar | db62cc3 | 2009-03-12 07:58:46 +0000 | [diff] [blame] | 127 | PrintOptions(*Args); |
Daniel Dunbar | b282ced | 2009-03-10 20:52:46 +0000 | [diff] [blame] | 128 | exit(0); |
| 129 | } |
Daniel Dunbar | db62cc3 | 2009-03-12 07:58:46 +0000 | [diff] [blame] | 130 | |
| 131 | // Construct the list of abstract actions to perform for this |
| 132 | // compilation. |
| 133 | llvm::SmallVector<Action*, 2> Actions; |
| 134 | if (Host->useDriverDriver()) |
| 135 | BuildUniversalActions(*Args, Actions); |
| 136 | else |
| 137 | BuildActions(*Args, Actions); |
| 138 | |
| 139 | // FIXME: This behavior shouldn't be here. |
| 140 | if (CCCPrintActions) { |
| 141 | PrintActions(Actions); |
| 142 | exit(0); |
| 143 | } |
| 144 | |
Daniel Dunbar | b282ced | 2009-03-10 20:52:46 +0000 | [diff] [blame] | 145 | assert(0 && "FIXME: Implement"); |
| 146 | |
| 147 | return new Compilation(); |
| 148 | } |
| 149 | |
Daniel Dunbar | db62cc3 | 2009-03-12 07:58:46 +0000 | [diff] [blame] | 150 | void Driver::PrintOptions(const ArgList &Args) { |
Daniel Dunbar | 7dc2a04 | 2009-03-05 06:38:47 +0000 | [diff] [blame] | 151 | unsigned i = 0; |
Daniel Dunbar | db62cc3 | 2009-03-12 07:58:46 +0000 | [diff] [blame] | 152 | for (ArgList::const_iterator it = Args.begin(), ie = Args.end(); |
Daniel Dunbar | 7dc2a04 | 2009-03-05 06:38:47 +0000 | [diff] [blame] | 153 | it != ie; ++it, ++i) { |
| 154 | Arg *A = *it; |
| 155 | llvm::errs() << "Option " << i << " - " |
| 156 | << "Name: \"" << A->getOption().getName() << "\", " |
| 157 | << "Values: {"; |
| 158 | for (unsigned j = 0; j < A->getNumValues(); ++j) { |
| 159 | if (j) |
| 160 | llvm::errs() << ", "; |
Daniel Dunbar | db62cc3 | 2009-03-12 07:58:46 +0000 | [diff] [blame] | 161 | llvm::errs() << '"' << A->getValue(Args, j) << '"'; |
Daniel Dunbar | 7dc2a04 | 2009-03-05 06:38:47 +0000 | [diff] [blame] | 162 | } |
| 163 | llvm::errs() << "}\n"; |
Daniel Dunbar | 7dc2a04 | 2009-03-05 06:38:47 +0000 | [diff] [blame] | 164 | } |
Daniel Dunbar | 63c4da9 | 2009-03-02 19:59:07 +0000 | [diff] [blame] | 165 | } |
Daniel Dunbar | d25acaa | 2009-03-10 23:41:59 +0000 | [diff] [blame] | 166 | |
Daniel Dunbar | db62cc3 | 2009-03-12 07:58:46 +0000 | [diff] [blame] | 167 | void Driver::PrintActions(const llvm::SmallVector<Action*, 2> &Actions) { |
| 168 | llvm::errs() << "FIXME: Print actions."; |
| 169 | } |
| 170 | |
| 171 | void Driver::BuildUniversalActions(const ArgList &Args, |
| 172 | llvm::SmallVector<Action*, 2> &Actions) { |
| 173 | // FIXME: Implement |
| 174 | BuildActions(Args, Actions); |
| 175 | } |
| 176 | |
| 177 | void Driver::BuildActions(const ArgList &Args, |
| 178 | llvm::SmallVector<Action*, 2> &Actions) { |
| 179 | types::ID InputType = types::TY_INVALID; |
| 180 | Arg *InputTypeArg = 0; |
| 181 | |
| 182 | llvm::SmallVector<std::pair<types::ID, const Arg*>, 16> Inputs; |
| 183 | for (ArgList::const_iterator it = Args.begin(), ie = Args.end(); |
| 184 | it != ie; ++it) { |
| 185 | Arg *A = *it; |
| 186 | |
| 187 | if (isa<InputOption>(A->getOption())) { |
| 188 | const char *Value = A->getValue(Args); |
| 189 | types::ID Ty = types::TY_INVALID; |
| 190 | |
| 191 | // Infer the input type if necessary. |
| 192 | if (!InputType) { |
| 193 | // stdin must be handled specially. |
| 194 | if (memcmp(Value, "-", 2) == 0) { |
| 195 | // If running with -E, treat as a C input (this changes the |
| 196 | // builtin macros, for example). This may be overridden by |
| 197 | // -ObjC below. |
| 198 | // |
| 199 | // Otherwise emit an error but still use a valid type to |
| 200 | // avoid spurious errors (e.g., no inputs). |
| 201 | if (!Args.hasArg(options::OPT_E)) |
| 202 | Diag("-E or -x required when input is from standard input"); |
| 203 | Ty = types::TY_C; |
| 204 | } else { |
| 205 | // Otherwise lookup by extension, and fallback to ObjectType |
| 206 | // if not found. |
| 207 | if (const char *Ext = strrchr(Value, '.')) |
| 208 | Ty = types::lookupTypeForExtension(Ext + 1); |
| 209 | if (Ty == types::TY_INVALID) |
| 210 | Ty = types::TY_Object; |
| 211 | } |
| 212 | |
| 213 | // -ObjC and -ObjC++ override the default language, but only |
| 214 | // -for "source files". We just treat everything that isn't a |
| 215 | // -linker input as a source file. |
| 216 | // |
| 217 | // FIXME: Clean this up if we move the phase sequence into the |
| 218 | // type. |
| 219 | if (Ty != types::TY_Object) { |
| 220 | if (Args.hasArg(options::OPT_ObjC)) |
| 221 | Ty = types::TY_ObjC; |
| 222 | else if (Args.hasArg(options::OPT_ObjCXX)) |
| 223 | Ty = types::TY_ObjCXX; |
| 224 | } |
| 225 | } else { |
| 226 | assert(InputTypeArg && "InputType set w/o InputTypeArg"); |
| 227 | InputTypeArg->claim(); |
| 228 | Ty = InputType; |
| 229 | } |
| 230 | |
| 231 | // Check that the file exists. It isn't clear this is worth |
| 232 | // doing, since the tool presumably does this anyway, and this |
| 233 | // just adds an extra stat to the equation, but this is gcc |
| 234 | // compatible. |
| 235 | if (memcmp(Value, "-", 2) != 0 && !llvm::sys::Path(Value).exists()) |
| 236 | Diag("no such file or directory: ") << A->getValue(Args) << "\n"; |
| 237 | else |
| 238 | Inputs.push_back(std::make_pair(Ty, A)); |
| 239 | |
| 240 | } else if (A->getOption().isLinkerInput()) { |
| 241 | // Just treat as object type, we could make a special type for |
| 242 | // this if necessary. |
| 243 | Inputs.push_back(std::make_pair(types::TY_Object, A)); |
| 244 | |
| 245 | } else if (A->getOption().getId() == options::OPT_x) { |
| 246 | InputTypeArg = A; |
| 247 | InputType = types::lookupTypeForTypeSpecifier(A->getValue(Args)); |
| 248 | |
| 249 | // Follow gcc behavior and treat as linker input for invalid -x |
| 250 | // options. Its not clear why we shouldn't just revert to |
| 251 | // unknown; but this isn't very important, we might as well be |
| 252 | // bug comatible. |
| 253 | if (!InputType) { |
| 254 | Diag("language not recognized: ") << A->getValue(Args) << "\n"; |
| 255 | InputType = types::TY_Object; |
| 256 | } |
| 257 | } |
| 258 | } |
| 259 | |
| 260 | for (unsigned i = 0, e = Inputs.size(); i != e; ++i) { |
| 261 | llvm::errs() << "input " << i << ": " |
| 262 | << Inputs[i].second->getValue(Args) << "\n"; |
| 263 | } |
| 264 | exit(0); |
| 265 | } |
| 266 | |
Daniel Dunbar | d25acaa | 2009-03-10 23:41:59 +0000 | [diff] [blame] | 267 | HostInfo *Driver::GetHostInfo(const char *Triple) { |
| 268 | // Dice into arch, platform, and OS. This matches |
| 269 | // arch,platform,os = '(.*?)-(.*?)-(.*?)' |
| 270 | // and missing fields are left empty. |
| 271 | std::string Arch, Platform, OS; |
| 272 | |
| 273 | if (const char *ArchEnd = strchr(Triple, '-')) { |
| 274 | Arch = std::string(Triple, ArchEnd); |
| 275 | |
| 276 | if (const char *PlatformEnd = strchr(ArchEnd+1, '-')) { |
| 277 | Platform = std::string(ArchEnd+1, PlatformEnd); |
| 278 | OS = PlatformEnd+1; |
| 279 | } else |
| 280 | Platform = ArchEnd+1; |
| 281 | } else |
| 282 | Arch = Triple; |
| 283 | |
| 284 | if (memcmp(&Platform[0], "darwin", 6) == 0) |
| 285 | return new DarwinHostInfo(Arch.c_str(), Platform.c_str(), OS.c_str()); |
| 286 | |
| 287 | return new UnknownHostInfo(Arch.c_str(), Platform.c_str(), OS.c_str()); |
| 288 | } |
Daniel Dunbar | db62cc3 | 2009-03-12 07:58:46 +0000 | [diff] [blame] | 289 | |
| 290 | // FIXME: Migrate to a normal diagnostics client. |
| 291 | llvm::raw_ostream &Driver::Diag(const char *Message) const { |
| 292 | return (llvm::errs() << Message); |
| 293 | } |