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 | |
| 22 | #include "llvm/Support/raw_ostream.h" |
Daniel Dunbar | db62cc3 | 2009-03-12 07:58:46 +0000 | [diff] [blame] | 23 | #include "llvm/System/Path.h" |
Daniel Dunbar | d6f0e37 | 2009-03-04 20:49:20 +0000 | [diff] [blame] | 24 | using namespace clang::driver; |
| 25 | |
Daniel Dunbar | d25acaa | 2009-03-10 23:41:59 +0000 | [diff] [blame] | 26 | Driver::Driver(const char *_Name, const char *_Dir, |
Daniel Dunbar | 9346849 | 2009-03-12 08:55:43 +0000 | [diff] [blame] | 27 | const char *_DefaultHostTriple, |
| 28 | Diagnostic &_Diags) |
| 29 | : Opts(new OptTable()), Diags(_Diags), |
Daniel Dunbar | d25acaa | 2009-03-10 23:41:59 +0000 | [diff] [blame] | 30 | Name(_Name), Dir(_Dir), DefaultHostTriple(_DefaultHostTriple), |
| 31 | Host(0), |
Daniel Dunbar | b282ced | 2009-03-10 20:52:46 +0000 | [diff] [blame] | 32 | CCCIsCXX(false), CCCEcho(false), |
| 33 | CCCNoClang(false), CCCNoClangCXX(false), CCCNoClangCPP(false) |
| 34 | { |
Daniel Dunbar | d6f0e37 | 2009-03-04 20:49:20 +0000 | [diff] [blame] | 35 | |
Daniel Dunbar | 63c4da9 | 2009-03-02 19:59:07 +0000 | [diff] [blame] | 36 | } |
| 37 | |
| 38 | Driver::~Driver() { |
Daniel Dunbar | d6f0e37 | 2009-03-04 20:49:20 +0000 | [diff] [blame] | 39 | delete Opts; |
Daniel Dunbar | 63c4da9 | 2009-03-02 19:59:07 +0000 | [diff] [blame] | 40 | } |
| 41 | |
Daniel Dunbar | 7dc2a04 | 2009-03-05 06:38:47 +0000 | [diff] [blame] | 42 | ArgList *Driver::ParseArgStrings(const char **ArgBegin, const char **ArgEnd) { |
| 43 | ArgList *Args = new ArgList(ArgBegin, ArgEnd); |
| 44 | |
| 45 | unsigned Index = 0, End = ArgEnd - ArgBegin; |
| 46 | while (Index < End) { |
| 47 | unsigned Prev = Index; |
| 48 | Arg *A = getOpts().ParseOneArg(*Args, Index, End); |
Daniel Dunbar | db62cc3 | 2009-03-12 07:58:46 +0000 | [diff] [blame] | 49 | if (A) { |
| 50 | if (A->getOption().isUnsupported()) { |
Daniel Dunbar | d724e33 | 2009-03-12 09:13:48 +0000 | [diff] [blame] | 51 | Diag(clang::diag::err_drv_unsupported_opt) << A->getOption().getName(); |
Daniel Dunbar | db62cc3 | 2009-03-12 07:58:46 +0000 | [diff] [blame] | 52 | continue; |
| 53 | } |
| 54 | |
Daniel Dunbar | 7dc2a04 | 2009-03-05 06:38:47 +0000 | [diff] [blame] | 55 | Args->append(A); |
Daniel Dunbar | db62cc3 | 2009-03-12 07:58:46 +0000 | [diff] [blame] | 56 | } |
Daniel Dunbar | 7dc2a04 | 2009-03-05 06:38:47 +0000 | [diff] [blame] | 57 | |
| 58 | assert(Index > Prev && "Parser failed to consume argument."); |
| 59 | } |
| 60 | |
| 61 | return Args; |
| 62 | } |
| 63 | |
Daniel Dunbar | 63c4da9 | 2009-03-02 19:59:07 +0000 | [diff] [blame] | 64 | Compilation *Driver::BuildCompilation(int argc, const char **argv) { |
Daniel Dunbar | b282ced | 2009-03-10 20:52:46 +0000 | [diff] [blame] | 65 | // FIXME: This stuff needs to go into the Compilation, not the |
| 66 | // driver. |
Daniel Dunbar | db62cc3 | 2009-03-12 07:58:46 +0000 | [diff] [blame] | 67 | bool CCCPrintOptions = false, CCCPrintActions = false; |
Daniel Dunbar | 7dc2a04 | 2009-03-05 06:38:47 +0000 | [diff] [blame] | 68 | |
Daniel Dunbar | b282ced | 2009-03-10 20:52:46 +0000 | [diff] [blame] | 69 | const char **Start = argv + 1, **End = argv + argc; |
Daniel Dunbar | d25acaa | 2009-03-10 23:41:59 +0000 | [diff] [blame] | 70 | const char *HostTriple = DefaultHostTriple.c_str(); |
Daniel Dunbar | b282ced | 2009-03-10 20:52:46 +0000 | [diff] [blame] | 71 | |
| 72 | // Read -ccc args. |
| 73 | // |
| 74 | // FIXME: We need to figure out where this behavior should |
| 75 | // live. Most of it should be outside in the client; the parts that |
| 76 | // aren't should have proper options, either by introducing new ones |
| 77 | // or by overloading gcc ones like -V or -b. |
| 78 | for (; Start != End && memcmp(*Start, "-ccc-", 5) == 0; ++Start) { |
| 79 | const char *Opt = *Start + 5; |
| 80 | |
| 81 | if (!strcmp(Opt, "print-options")) { |
| 82 | CCCPrintOptions = true; |
| 83 | } else if (!strcmp(Opt, "print-phases")) { |
Daniel Dunbar | db62cc3 | 2009-03-12 07:58:46 +0000 | [diff] [blame] | 84 | CCCPrintActions = true; |
Daniel Dunbar | b282ced | 2009-03-10 20:52:46 +0000 | [diff] [blame] | 85 | } else if (!strcmp(Opt, "cxx")) { |
| 86 | CCCIsCXX = true; |
| 87 | } else if (!strcmp(Opt, "echo")) { |
| 88 | CCCEcho = true; |
| 89 | |
| 90 | } else if (!strcmp(Opt, "no-clang")) { |
| 91 | CCCNoClang = true; |
| 92 | } else if (!strcmp(Opt, "no-clang-cxx")) { |
| 93 | CCCNoClangCXX = true; |
| 94 | } else if (!strcmp(Opt, "no-clang-cpp")) { |
| 95 | CCCNoClangCPP = true; |
| 96 | } else if (!strcmp(Opt, "clang-archs")) { |
| 97 | assert(Start+1 < End && "FIXME: -ccc- argument handling."); |
| 98 | const char *Cur = *++Start; |
| 99 | |
| 100 | for (;;) { |
| 101 | const char *Next = strchr(Cur, ','); |
| 102 | |
| 103 | if (Next) { |
| 104 | CCCClangArchs.insert(std::string(Cur, Next)); |
| 105 | Cur = Next + 1; |
| 106 | } else { |
| 107 | CCCClangArchs.insert(std::string(Cur)); |
| 108 | break; |
| 109 | } |
| 110 | } |
| 111 | |
Daniel Dunbar | d25acaa | 2009-03-10 23:41:59 +0000 | [diff] [blame] | 112 | } else if (!strcmp(Opt, "host-triple")) { |
Daniel Dunbar | b282ced | 2009-03-10 20:52:46 +0000 | [diff] [blame] | 113 | assert(Start+1 < End && "FIXME: -ccc- argument handling."); |
Daniel Dunbar | d25acaa | 2009-03-10 23:41:59 +0000 | [diff] [blame] | 114 | HostTriple = *++Start; |
Daniel Dunbar | b282ced | 2009-03-10 20:52:46 +0000 | [diff] [blame] | 115 | |
| 116 | } else { |
| 117 | // FIXME: Error handling. |
| 118 | llvm::errs() << "invalid option: " << *Start << "\n"; |
| 119 | exit(1); |
| 120 | } |
| 121 | } |
Daniel Dunbar | d25acaa | 2009-03-10 23:41:59 +0000 | [diff] [blame] | 122 | |
| 123 | Host = Driver::GetHostInfo(HostTriple); |
| 124 | |
Daniel Dunbar | b282ced | 2009-03-10 20:52:46 +0000 | [diff] [blame] | 125 | ArgList *Args = ParseArgStrings(Start, End); |
| 126 | |
| 127 | // FIXME: This behavior shouldn't be here. |
| 128 | if (CCCPrintOptions) { |
Daniel Dunbar | db62cc3 | 2009-03-12 07:58:46 +0000 | [diff] [blame] | 129 | PrintOptions(*Args); |
Daniel Dunbar | b282ced | 2009-03-10 20:52:46 +0000 | [diff] [blame] | 130 | exit(0); |
| 131 | } |
Daniel Dunbar | db62cc3 | 2009-03-12 07:58:46 +0000 | [diff] [blame] | 132 | |
| 133 | // Construct the list of abstract actions to perform for this |
| 134 | // compilation. |
Daniel Dunbar | a790d37 | 2009-03-12 18:24:49 +0000 | [diff] [blame] | 135 | ActionList Actions; |
Daniel Dunbar | db62cc3 | 2009-03-12 07:58:46 +0000 | [diff] [blame] | 136 | if (Host->useDriverDriver()) |
| 137 | BuildUniversalActions(*Args, Actions); |
| 138 | else |
| 139 | BuildActions(*Args, Actions); |
| 140 | |
| 141 | // FIXME: This behavior shouldn't be here. |
| 142 | if (CCCPrintActions) { |
| 143 | PrintActions(Actions); |
| 144 | exit(0); |
| 145 | } |
| 146 | |
Daniel Dunbar | b282ced | 2009-03-10 20:52:46 +0000 | [diff] [blame] | 147 | assert(0 && "FIXME: Implement"); |
| 148 | |
| 149 | return new Compilation(); |
| 150 | } |
| 151 | |
Daniel Dunbar | a790d37 | 2009-03-12 18:24:49 +0000 | [diff] [blame] | 152 | void Driver::PrintOptions(const ArgList &Args) const { |
Daniel Dunbar | 7dc2a04 | 2009-03-05 06:38:47 +0000 | [diff] [blame] | 153 | unsigned i = 0; |
Daniel Dunbar | db62cc3 | 2009-03-12 07:58:46 +0000 | [diff] [blame] | 154 | for (ArgList::const_iterator it = Args.begin(), ie = Args.end(); |
Daniel Dunbar | 7dc2a04 | 2009-03-05 06:38:47 +0000 | [diff] [blame] | 155 | it != ie; ++it, ++i) { |
| 156 | Arg *A = *it; |
| 157 | llvm::errs() << "Option " << i << " - " |
| 158 | << "Name: \"" << A->getOption().getName() << "\", " |
| 159 | << "Values: {"; |
| 160 | for (unsigned j = 0; j < A->getNumValues(); ++j) { |
| 161 | if (j) |
| 162 | llvm::errs() << ", "; |
Daniel Dunbar | db62cc3 | 2009-03-12 07:58:46 +0000 | [diff] [blame] | 163 | llvm::errs() << '"' << A->getValue(Args, j) << '"'; |
Daniel Dunbar | 7dc2a04 | 2009-03-05 06:38:47 +0000 | [diff] [blame] | 164 | } |
| 165 | llvm::errs() << "}\n"; |
Daniel Dunbar | 7dc2a04 | 2009-03-05 06:38:47 +0000 | [diff] [blame] | 166 | } |
Daniel Dunbar | 63c4da9 | 2009-03-02 19:59:07 +0000 | [diff] [blame] | 167 | } |
Daniel Dunbar | d25acaa | 2009-03-10 23:41:59 +0000 | [diff] [blame] | 168 | |
Daniel Dunbar | a790d37 | 2009-03-12 18:24:49 +0000 | [diff] [blame] | 169 | void Driver::PrintActions(const ActionList &Actions) const { |
Daniel Dunbar | db62cc3 | 2009-03-12 07:58:46 +0000 | [diff] [blame] | 170 | llvm::errs() << "FIXME: Print actions."; |
| 171 | } |
| 172 | |
Daniel Dunbar | a790d37 | 2009-03-12 18:24:49 +0000 | [diff] [blame] | 173 | void Driver::BuildUniversalActions(ArgList &Args, ActionList &Actions) { |
Daniel Dunbar | db62cc3 | 2009-03-12 07:58:46 +0000 | [diff] [blame] | 174 | // FIXME: Implement |
| 175 | BuildActions(Args, Actions); |
| 176 | } |
| 177 | |
Daniel Dunbar | a790d37 | 2009-03-12 18:24:49 +0000 | [diff] [blame] | 178 | void Driver::BuildActions(ArgList &Args, ActionList &Actions) { |
Daniel Dunbar | db62cc3 | 2009-03-12 07:58:46 +0000 | [diff] [blame] | 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. |
Daniel Dunbar | 9346849 | 2009-03-12 08:55:43 +0000 | [diff] [blame] | 192 | if (InputType == types::TY_INVALID) { |
Daniel Dunbar | db62cc3 | 2009-03-12 07:58:46 +0000 | [diff] [blame] | 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)) |
Daniel Dunbar | d724e33 | 2009-03-12 09:13:48 +0000 | [diff] [blame] | 202 | Diag(clang::diag::err_drv_unknown_stdin_type); |
Daniel Dunbar | db62cc3 | 2009-03-12 07:58:46 +0000 | [diff] [blame] | 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()) |
Daniel Dunbar | d724e33 | 2009-03-12 09:13:48 +0000 | [diff] [blame] | 236 | Diag(clang::diag::err_drv_no_such_file) << A->getValue(Args); |
Daniel Dunbar | db62cc3 | 2009-03-12 07:58:46 +0000 | [diff] [blame] | 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) { |
Daniel Dunbar | d724e33 | 2009-03-12 09:13:48 +0000 | [diff] [blame] | 254 | Diag(clang::diag::err_drv_unknown_language) << A->getValue(Args); |
Daniel Dunbar | db62cc3 | 2009-03-12 07:58:46 +0000 | [diff] [blame] | 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 | } |