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 | f353c8c | 2009-03-16 06:56:51 +0000 | [diff] [blame] | 18 | #include "clang/Driver/Job.h" |
Daniel Dunbar | 0648262 | 2009-03-05 06:38:47 +0000 | [diff] [blame] | 19 | #include "clang/Driver/Option.h" |
Daniel Dunbar | 1b3bb6e | 2009-03-04 20:49:20 +0000 | [diff] [blame] | 20 | #include "clang/Driver/Options.h" |
Daniel Dunbar | f353c8c | 2009-03-16 06:56:51 +0000 | [diff] [blame] | 21 | #include "clang/Driver/Tool.h" |
| 22 | #include "clang/Driver/ToolChain.h" |
Daniel Dunbar | 53ec552 | 2009-03-12 07:58:46 +0000 | [diff] [blame] | 23 | #include "clang/Driver/Types.h" |
Daniel Dunbar | 0648262 | 2009-03-05 06:38:47 +0000 | [diff] [blame] | 24 | |
Douglas Gregor | ab41e63 | 2009-04-27 22:23:34 +0000 | [diff] [blame] | 25 | #include "clang/Basic/Version.h" |
| 26 | |
Daniel Dunbar | 1368954 | 2009-03-13 20:33:35 +0000 | [diff] [blame] | 27 | #include "llvm/ADT/StringSet.h" |
Daniel Dunbar | 8f25c79 | 2009-03-18 01:38:48 +0000 | [diff] [blame] | 28 | #include "llvm/Support/PrettyStackTrace.h" |
Daniel Dunbar | 0648262 | 2009-03-05 06:38:47 +0000 | [diff] [blame] | 29 | #include "llvm/Support/raw_ostream.h" |
Daniel Dunbar | 53ec552 | 2009-03-12 07:58:46 +0000 | [diff] [blame] | 30 | #include "llvm/System/Path.h" |
Daniel Dunbar | 632f50e | 2009-03-18 21:34:08 +0000 | [diff] [blame] | 31 | #include "llvm/System/Program.h" |
Daniel Dunbar | ba10213 | 2009-03-13 12:19:02 +0000 | [diff] [blame] | 32 | |
Daniel Dunbar | f353c8c | 2009-03-16 06:56:51 +0000 | [diff] [blame] | 33 | #include "InputInfo.h" |
| 34 | |
Daniel Dunbar | ba10213 | 2009-03-13 12:19:02 +0000 | [diff] [blame] | 35 | #include <map> |
| 36 | |
Daniel Dunbar | 1b3bb6e | 2009-03-04 20:49:20 +0000 | [diff] [blame] | 37 | using namespace clang::driver; |
Chris Lattner | 92b3699 | 2009-03-26 05:56:24 +0000 | [diff] [blame] | 38 | using namespace clang; |
Daniel Dunbar | 1b3bb6e | 2009-03-04 20:49:20 +0000 | [diff] [blame] | 39 | |
Daniel Dunbar | dd98e2c | 2009-03-10 23:41:59 +0000 | [diff] [blame] | 40 | Driver::Driver(const char *_Name, const char *_Dir, |
Daniel Dunbar | 4ad4b3e | 2009-03-12 08:55:43 +0000 | [diff] [blame] | 41 | const char *_DefaultHostTriple, |
Daniel Dunbar | f353c8c | 2009-03-16 06:56:51 +0000 | [diff] [blame] | 42 | const char *_DefaultImageName, |
Daniel Dunbar | 4ad4b3e | 2009-03-12 08:55:43 +0000 | [diff] [blame] | 43 | Diagnostic &_Diags) |
| 44 | : Opts(new OptTable()), Diags(_Diags), |
Daniel Dunbar | dd98e2c | 2009-03-10 23:41:59 +0000 | [diff] [blame] | 45 | Name(_Name), Dir(_Dir), DefaultHostTriple(_DefaultHostTriple), |
Daniel Dunbar | f353c8c | 2009-03-16 06:56:51 +0000 | [diff] [blame] | 46 | DefaultImageName(_DefaultImageName), |
Daniel Dunbar | dd98e2c | 2009-03-10 23:41:59 +0000 | [diff] [blame] | 47 | Host(0), |
Daniel Dunbar | 5c3c1d7 | 2009-03-17 22:47:06 +0000 | [diff] [blame] | 48 | CCCIsCXX(false), CCCEcho(false), CCCPrintBindings(false), |
Daniel Dunbar | dfaf4b3 | 2009-07-23 17:48:59 +0000 | [diff] [blame^] | 49 | CCCGenericGCCName("gcc"), CCCUseClang(true), CCCUseClangCXX(true), |
Douglas Gregor | 214e872 | 2009-04-28 22:44:02 +0000 | [diff] [blame] | 50 | CCCUseClangCPP(true), CCCUsePCH(true), |
Daniel Dunbar | 8b1604e | 2009-03-13 00:17:48 +0000 | [diff] [blame] | 51 | SuppressMissingInputWarning(false) |
Daniel Dunbar | 365c02f | 2009-03-10 20:52:46 +0000 | [diff] [blame] | 52 | { |
Daniel Dunbar | 3ede8d0 | 2009-03-02 19:59:07 +0000 | [diff] [blame] | 53 | } |
| 54 | |
| 55 | Driver::~Driver() { |
Daniel Dunbar | 1b3bb6e | 2009-03-04 20:49:20 +0000 | [diff] [blame] | 56 | delete Opts; |
Daniel Dunbar | 7e4534d | 2009-03-18 01:09:40 +0000 | [diff] [blame] | 57 | delete Host; |
Daniel Dunbar | 3ede8d0 | 2009-03-02 19:59:07 +0000 | [diff] [blame] | 58 | } |
| 59 | |
Daniel Dunbar | f3cad36 | 2009-03-25 04:13:45 +0000 | [diff] [blame] | 60 | InputArgList *Driver::ParseArgStrings(const char **ArgBegin, |
| 61 | const char **ArgEnd) { |
Daniel Dunbar | 8f25c79 | 2009-03-18 01:38:48 +0000 | [diff] [blame] | 62 | llvm::PrettyStackTraceString CrashInfo("Command line argument parsing"); |
Daniel Dunbar | f3cad36 | 2009-03-25 04:13:45 +0000 | [diff] [blame] | 63 | InputArgList *Args = new InputArgList(ArgBegin, ArgEnd); |
Daniel Dunbar | 0648262 | 2009-03-05 06:38:47 +0000 | [diff] [blame] | 64 | |
Daniel Dunbar | ad2a9af | 2009-03-13 11:38:42 +0000 | [diff] [blame] | 65 | // FIXME: Handle '@' args (or at least error on them). |
| 66 | |
Daniel Dunbar | 0648262 | 2009-03-05 06:38:47 +0000 | [diff] [blame] | 67 | unsigned Index = 0, End = ArgEnd - ArgBegin; |
| 68 | while (Index < End) { |
Daniel Dunbar | 4139340 | 2009-03-13 01:01:44 +0000 | [diff] [blame] | 69 | // gcc's handling of empty arguments doesn't make |
| 70 | // sense, but this is not a common use case. :) |
| 71 | // |
| 72 | // We just ignore them here (note that other things may |
| 73 | // still take them as arguments). |
| 74 | if (Args->getArgString(Index)[0] == '\0') { |
| 75 | ++Index; |
| 76 | continue; |
| 77 | } |
| 78 | |
Daniel Dunbar | 0648262 | 2009-03-05 06:38:47 +0000 | [diff] [blame] | 79 | unsigned Prev = Index; |
Daniel Dunbar | b0c4df5 | 2009-03-22 23:26:43 +0000 | [diff] [blame] | 80 | Arg *A = getOpts().ParseOneArg(*Args, Index); |
| 81 | assert(Index > Prev && "Parser failed to consume argument."); |
Daniel Dunbar | 53ec552 | 2009-03-12 07:58:46 +0000 | [diff] [blame] | 82 | |
Daniel Dunbar | b0c4df5 | 2009-03-22 23:26:43 +0000 | [diff] [blame] | 83 | // Check for missing argument error. |
| 84 | if (!A) { |
| 85 | assert(Index >= End && "Unexpected parser error."); |
| 86 | Diag(clang::diag::err_drv_missing_argument) |
| 87 | << Args->getArgString(Prev) |
| 88 | << (Index - Prev - 1); |
| 89 | break; |
Daniel Dunbar | 53ec552 | 2009-03-12 07:58:46 +0000 | [diff] [blame] | 90 | } |
Daniel Dunbar | 0648262 | 2009-03-05 06:38:47 +0000 | [diff] [blame] | 91 | |
Daniel Dunbar | b0c4df5 | 2009-03-22 23:26:43 +0000 | [diff] [blame] | 92 | if (A->getOption().isUnsupported()) { |
| 93 | Diag(clang::diag::err_drv_unsupported_opt) << A->getAsString(*Args); |
| 94 | continue; |
| 95 | } |
| 96 | Args->append(A); |
Daniel Dunbar | 0648262 | 2009-03-05 06:38:47 +0000 | [diff] [blame] | 97 | } |
| 98 | |
| 99 | return Args; |
| 100 | } |
| 101 | |
Daniel Dunbar | 3ede8d0 | 2009-03-02 19:59:07 +0000 | [diff] [blame] | 102 | Compilation *Driver::BuildCompilation(int argc, const char **argv) { |
Daniel Dunbar | 8f25c79 | 2009-03-18 01:38:48 +0000 | [diff] [blame] | 103 | llvm::PrettyStackTraceString CrashInfo("Compilation construction"); |
| 104 | |
Daniel Dunbar | cb88167 | 2009-03-13 00:51:18 +0000 | [diff] [blame] | 105 | // FIXME: Handle environment options which effect driver behavior, |
| 106 | // somewhere (client?). GCC_EXEC_PREFIX, COMPILER_PATH, |
| 107 | // LIBRARY_PATH, LPATH, CC_PRINT_OPTIONS, QA_OVERRIDE_GCC3_OPTIONS. |
| 108 | |
| 109 | // FIXME: What are we going to do with -V and -b? |
| 110 | |
Daniel Dunbar | 365c02f | 2009-03-10 20:52:46 +0000 | [diff] [blame] | 111 | // FIXME: This stuff needs to go into the Compilation, not the |
| 112 | // driver. |
Daniel Dunbar | 53ec552 | 2009-03-12 07:58:46 +0000 | [diff] [blame] | 113 | bool CCCPrintOptions = false, CCCPrintActions = false; |
Daniel Dunbar | 0648262 | 2009-03-05 06:38:47 +0000 | [diff] [blame] | 114 | |
Daniel Dunbar | 365c02f | 2009-03-10 20:52:46 +0000 | [diff] [blame] | 115 | const char **Start = argv + 1, **End = argv + argc; |
Daniel Dunbar | dd98e2c | 2009-03-10 23:41:59 +0000 | [diff] [blame] | 116 | const char *HostTriple = DefaultHostTriple.c_str(); |
Daniel Dunbar | 365c02f | 2009-03-10 20:52:46 +0000 | [diff] [blame] | 117 | |
| 118 | // Read -ccc args. |
| 119 | // |
| 120 | // FIXME: We need to figure out where this behavior should |
| 121 | // live. Most of it should be outside in the client; the parts that |
| 122 | // aren't should have proper options, either by introducing new ones |
| 123 | // or by overloading gcc ones like -V or -b. |
| 124 | for (; Start != End && memcmp(*Start, "-ccc-", 5) == 0; ++Start) { |
| 125 | const char *Opt = *Start + 5; |
| 126 | |
| 127 | if (!strcmp(Opt, "print-options")) { |
| 128 | CCCPrintOptions = true; |
| 129 | } else if (!strcmp(Opt, "print-phases")) { |
Daniel Dunbar | 53ec552 | 2009-03-12 07:58:46 +0000 | [diff] [blame] | 130 | CCCPrintActions = true; |
Daniel Dunbar | 5c3c1d7 | 2009-03-17 22:47:06 +0000 | [diff] [blame] | 131 | } else if (!strcmp(Opt, "print-bindings")) { |
| 132 | CCCPrintBindings = true; |
Daniel Dunbar | 365c02f | 2009-03-10 20:52:46 +0000 | [diff] [blame] | 133 | } else if (!strcmp(Opt, "cxx")) { |
| 134 | CCCIsCXX = true; |
| 135 | } else if (!strcmp(Opt, "echo")) { |
| 136 | CCCEcho = true; |
| 137 | |
Daniel Dunbar | 78d8a08 | 2009-04-01 23:34:41 +0000 | [diff] [blame] | 138 | } else if (!strcmp(Opt, "gcc-name")) { |
| 139 | assert(Start+1 < End && "FIXME: -ccc- argument handling."); |
| 140 | CCCGenericGCCName = *++Start; |
| 141 | |
Daniel Dunbar | 0f99d2e | 2009-03-24 19:02:31 +0000 | [diff] [blame] | 142 | } else if (!strcmp(Opt, "clang-cxx")) { |
| 143 | CCCUseClangCXX = true; |
Daniel Dunbar | dfaf4b3 | 2009-07-23 17:48:59 +0000 | [diff] [blame^] | 144 | } else if (!strcmp(Opt, "no-clang-cxx")) { |
| 145 | CCCUseClangCXX = false; |
Douglas Gregor | df91ef3 | 2009-04-18 00:34:01 +0000 | [diff] [blame] | 146 | } else if (!strcmp(Opt, "pch-is-pch")) { |
| 147 | CCCUsePCH = true; |
| 148 | } else if (!strcmp(Opt, "pch-is-pth")) { |
| 149 | CCCUsePCH = false; |
Daniel Dunbar | 365c02f | 2009-03-10 20:52:46 +0000 | [diff] [blame] | 150 | } else if (!strcmp(Opt, "no-clang")) { |
Daniel Dunbar | 0f99d2e | 2009-03-24 19:02:31 +0000 | [diff] [blame] | 151 | CCCUseClang = false; |
Daniel Dunbar | 365c02f | 2009-03-10 20:52:46 +0000 | [diff] [blame] | 152 | } else if (!strcmp(Opt, "no-clang-cpp")) { |
Daniel Dunbar | 0f99d2e | 2009-03-24 19:02:31 +0000 | [diff] [blame] | 153 | CCCUseClangCPP = false; |
Daniel Dunbar | 365c02f | 2009-03-10 20:52:46 +0000 | [diff] [blame] | 154 | } else if (!strcmp(Opt, "clang-archs")) { |
| 155 | assert(Start+1 < End && "FIXME: -ccc- argument handling."); |
| 156 | const char *Cur = *++Start; |
| 157 | |
Daniel Dunbar | 0f99d2e | 2009-03-24 19:02:31 +0000 | [diff] [blame] | 158 | CCCClangArchs.clear(); |
Daniel Dunbar | 365c02f | 2009-03-10 20:52:46 +0000 | [diff] [blame] | 159 | for (;;) { |
| 160 | const char *Next = strchr(Cur, ','); |
| 161 | |
| 162 | if (Next) { |
Daniel Dunbar | 0f99d2e | 2009-03-24 19:02:31 +0000 | [diff] [blame] | 163 | if (Cur != Next) |
| 164 | CCCClangArchs.insert(std::string(Cur, Next)); |
Daniel Dunbar | 365c02f | 2009-03-10 20:52:46 +0000 | [diff] [blame] | 165 | Cur = Next + 1; |
| 166 | } else { |
Daniel Dunbar | 0f99d2e | 2009-03-24 19:02:31 +0000 | [diff] [blame] | 167 | if (*Cur != '\0') |
| 168 | CCCClangArchs.insert(std::string(Cur)); |
Daniel Dunbar | 365c02f | 2009-03-10 20:52:46 +0000 | [diff] [blame] | 169 | break; |
| 170 | } |
| 171 | } |
| 172 | |
Daniel Dunbar | dd98e2c | 2009-03-10 23:41:59 +0000 | [diff] [blame] | 173 | } else if (!strcmp(Opt, "host-triple")) { |
Daniel Dunbar | 365c02f | 2009-03-10 20:52:46 +0000 | [diff] [blame] | 174 | assert(Start+1 < End && "FIXME: -ccc- argument handling."); |
Daniel Dunbar | dd98e2c | 2009-03-10 23:41:59 +0000 | [diff] [blame] | 175 | HostTriple = *++Start; |
Daniel Dunbar | 365c02f | 2009-03-10 20:52:46 +0000 | [diff] [blame] | 176 | |
| 177 | } else { |
| 178 | // FIXME: Error handling. |
| 179 | llvm::errs() << "invalid option: " << *Start << "\n"; |
| 180 | exit(1); |
| 181 | } |
| 182 | } |
Daniel Dunbar | dd98e2c | 2009-03-10 23:41:59 +0000 | [diff] [blame] | 183 | |
Daniel Dunbar | f3cad36 | 2009-03-25 04:13:45 +0000 | [diff] [blame] | 184 | InputArgList *Args = ParseArgStrings(Start, End); |
Daniel Dunbar | 365c02f | 2009-03-10 20:52:46 +0000 | [diff] [blame] | 185 | |
Daniel Dunbar | e504952 | 2009-03-17 20:45:45 +0000 | [diff] [blame] | 186 | Host = GetHostInfo(HostTriple); |
Daniel Dunbar | cb88167 | 2009-03-13 00:51:18 +0000 | [diff] [blame] | 187 | |
Daniel Dunbar | 586dc23 | 2009-03-16 06:42:30 +0000 | [diff] [blame] | 188 | // The compilation takes ownership of Args. |
Daniel Dunbar | e530ad4 | 2009-03-18 22:16:03 +0000 | [diff] [blame] | 189 | Compilation *C = new Compilation(*this, *Host->getToolChain(*Args), Args); |
Daniel Dunbar | 2154923 | 2009-03-18 02:55:38 +0000 | [diff] [blame] | 190 | |
| 191 | // FIXME: This behavior shouldn't be here. |
| 192 | if (CCCPrintOptions) { |
| 193 | PrintOptions(C->getArgs()); |
| 194 | return C; |
| 195 | } |
| 196 | |
| 197 | if (!HandleImmediateArgs(*C)) |
| 198 | return C; |
| 199 | |
| 200 | // Construct the list of abstract actions to perform for this |
| 201 | // compilation. We avoid passing a Compilation here simply to |
| 202 | // enforce the abstraction that pipelining is not host or toolchain |
| 203 | // dependent (other than the driver driver test). |
| 204 | if (Host->useDriverDriver()) |
| 205 | BuildUniversalActions(C->getArgs(), C->getActions()); |
| 206 | else |
| 207 | BuildActions(C->getArgs(), C->getActions()); |
| 208 | |
| 209 | if (CCCPrintActions) { |
Daniel Dunbar | 10ffa9a | 2009-03-18 03:13:20 +0000 | [diff] [blame] | 210 | PrintActions(*C); |
Daniel Dunbar | 2154923 | 2009-03-18 02:55:38 +0000 | [diff] [blame] | 211 | return C; |
| 212 | } |
| 213 | |
| 214 | BuildJobs(*C); |
Daniel Dunbar | 8d2554a | 2009-03-15 01:38:15 +0000 | [diff] [blame] | 215 | |
| 216 | return C; |
Daniel Dunbar | 365c02f | 2009-03-10 20:52:46 +0000 | [diff] [blame] | 217 | } |
| 218 | |
Daniel Dunbar | c88a88f | 2009-07-01 20:03:04 +0000 | [diff] [blame] | 219 | int Driver::ExecuteCompilation(const Compilation &C) const { |
| 220 | // Just print if -### was present. |
| 221 | if (C.getArgs().hasArg(options::OPT__HASH_HASH_HASH)) { |
| 222 | C.PrintJob(llvm::errs(), C.getJobs(), "\n", true); |
| 223 | return 0; |
| 224 | } |
| 225 | |
| 226 | // If there were errors building the compilation, quit now. |
| 227 | if (getDiags().getNumErrors()) |
| 228 | return 1; |
| 229 | |
| 230 | const Command *FailingCommand = 0; |
| 231 | int Res = C.ExecuteJob(C.getJobs(), FailingCommand); |
| 232 | |
| 233 | // Remove temp files. |
| 234 | C.CleanupFileList(C.getTempFiles()); |
| 235 | |
| 236 | // If the compilation failed, remove result files as well. |
| 237 | if (Res != 0 && !C.getArgs().hasArg(options::OPT_save_temps)) |
| 238 | C.CleanupFileList(C.getResultFiles(), true); |
| 239 | |
| 240 | // Print extra information about abnormal failures, if possible. |
| 241 | if (Res) { |
| 242 | // This is ad-hoc, but we don't want to be excessively noisy. If the result |
| 243 | // status was 1, assume the command failed normally. In particular, if it |
| 244 | // was the compiler then assume it gave a reasonable error code. Failures in |
| 245 | // other tools are less common, and they generally have worse diagnostics, |
| 246 | // so always print the diagnostic there. |
| 247 | const Action &Source = FailingCommand->getSource(); |
| 248 | bool IsFriendlyTool = (isa<PreprocessJobAction>(Source) || |
| 249 | isa<PrecompileJobAction>(Source) || |
| 250 | isa<AnalyzeJobAction>(Source) || |
| 251 | isa<CompileJobAction>(Source)); |
| 252 | |
| 253 | if (!IsFriendlyTool || Res != 1) { |
| 254 | // FIXME: See FIXME above regarding result code interpretation. |
| 255 | if (Res < 0) |
| 256 | Diag(clang::diag::err_drv_command_signalled) |
| 257 | << Source.getClassName() << -Res; |
| 258 | else |
| 259 | Diag(clang::diag::err_drv_command_failed) |
| 260 | << Source.getClassName() << Res; |
| 261 | } |
| 262 | } |
| 263 | |
| 264 | return Res; |
| 265 | } |
| 266 | |
Daniel Dunbar | d65bddc | 2009-03-12 18:24:49 +0000 | [diff] [blame] | 267 | void Driver::PrintOptions(const ArgList &Args) const { |
Daniel Dunbar | 0648262 | 2009-03-05 06:38:47 +0000 | [diff] [blame] | 268 | unsigned i = 0; |
Daniel Dunbar | 53ec552 | 2009-03-12 07:58:46 +0000 | [diff] [blame] | 269 | for (ArgList::const_iterator it = Args.begin(), ie = Args.end(); |
Daniel Dunbar | 0648262 | 2009-03-05 06:38:47 +0000 | [diff] [blame] | 270 | it != ie; ++it, ++i) { |
| 271 | Arg *A = *it; |
| 272 | llvm::errs() << "Option " << i << " - " |
| 273 | << "Name: \"" << A->getOption().getName() << "\", " |
| 274 | << "Values: {"; |
| 275 | for (unsigned j = 0; j < A->getNumValues(); ++j) { |
| 276 | if (j) |
| 277 | llvm::errs() << ", "; |
Daniel Dunbar | 53ec552 | 2009-03-12 07:58:46 +0000 | [diff] [blame] | 278 | llvm::errs() << '"' << A->getValue(Args, j) << '"'; |
Daniel Dunbar | 0648262 | 2009-03-05 06:38:47 +0000 | [diff] [blame] | 279 | } |
| 280 | llvm::errs() << "}\n"; |
Daniel Dunbar | 0648262 | 2009-03-05 06:38:47 +0000 | [diff] [blame] | 281 | } |
Daniel Dunbar | 3ede8d0 | 2009-03-02 19:59:07 +0000 | [diff] [blame] | 282 | } |
Daniel Dunbar | dd98e2c | 2009-03-10 23:41:59 +0000 | [diff] [blame] | 283 | |
Daniel Dunbar | 91e28af | 2009-03-31 21:38:17 +0000 | [diff] [blame] | 284 | static std::string getOptionHelpName(const OptTable &Opts, options::ID Id) { |
| 285 | std::string Name = Opts.getOptionName(Id); |
| 286 | |
| 287 | // Add metavar, if used. |
| 288 | switch (Opts.getOptionKind(Id)) { |
| 289 | case Option::GroupClass: case Option::InputClass: case Option::UnknownClass: |
| 290 | assert(0 && "Invalid option with help text."); |
| 291 | |
| 292 | case Option::MultiArgClass: case Option::JoinedAndSeparateClass: |
| 293 | assert(0 && "Cannot print metavar for this kind of option."); |
| 294 | |
| 295 | case Option::FlagClass: |
| 296 | break; |
| 297 | |
| 298 | case Option::SeparateClass: case Option::JoinedOrSeparateClass: |
| 299 | Name += ' '; |
| 300 | // FALLTHROUGH |
| 301 | case Option::JoinedClass: case Option::CommaJoinedClass: |
| 302 | Name += Opts.getOptionMetaVar(Id); |
| 303 | break; |
| 304 | } |
| 305 | |
| 306 | return Name; |
| 307 | } |
| 308 | |
Daniel Dunbar | c35d71f | 2009-04-15 16:34:29 +0000 | [diff] [blame] | 309 | void Driver::PrintHelp(bool ShowHidden) const { |
Daniel Dunbar | 91e28af | 2009-03-31 21:38:17 +0000 | [diff] [blame] | 310 | llvm::raw_ostream &OS = llvm::outs(); |
| 311 | |
| 312 | OS << "OVERVIEW: clang \"gcc-compatible\" driver\n"; |
| 313 | OS << '\n'; |
| 314 | OS << "USAGE: " << Name << " [options] <input files>\n"; |
| 315 | OS << '\n'; |
| 316 | OS << "OPTIONS:\n"; |
| 317 | |
| 318 | // Render help text into (option, help) pairs. |
| 319 | std::vector< std::pair<std::string, const char*> > OptionHelp; |
| 320 | |
| 321 | for (unsigned i = options::OPT_INPUT, e = options::LastOption; i != e; ++i) { |
| 322 | options::ID Id = (options::ID) i; |
| 323 | if (const char *Text = getOpts().getOptionHelpText(Id)) |
| 324 | OptionHelp.push_back(std::make_pair(getOptionHelpName(getOpts(), Id), |
| 325 | Text)); |
| 326 | } |
| 327 | |
Daniel Dunbar | c35d71f | 2009-04-15 16:34:29 +0000 | [diff] [blame] | 328 | if (ShowHidden) { |
| 329 | OptionHelp.push_back(std::make_pair("\nDRIVER OPTIONS:","")); |
| 330 | OptionHelp.push_back(std::make_pair("-ccc-cxx", |
| 331 | "Act as a C++ driver")); |
| 332 | OptionHelp.push_back(std::make_pair("-ccc-gcc-name", |
| 333 | "Name for native GCC compiler")); |
| 334 | OptionHelp.push_back(std::make_pair("-ccc-clang-cxx", |
| 335 | "Use the clang compiler for C++")); |
| 336 | OptionHelp.push_back(std::make_pair("-ccc-no-clang", |
| 337 | "Never use the clang compiler")); |
| 338 | OptionHelp.push_back(std::make_pair("-ccc-no-clang-cpp", |
| 339 | "Never use the clang preprocessor")); |
| 340 | OptionHelp.push_back(std::make_pair("-ccc-clang-archs", |
| 341 | "Comma separate list of architectures " |
| 342 | "to use the clang compiler for")); |
Douglas Gregor | df91ef3 | 2009-04-18 00:34:01 +0000 | [diff] [blame] | 343 | OptionHelp.push_back(std::make_pair("-ccc-pch-is-pch", |
| 344 | "Use lazy PCH for precompiled headers")); |
| 345 | OptionHelp.push_back(std::make_pair("-ccc-pch-is-pth", |
| 346 | "Use pretokenized headers for precompiled headers")); |
Daniel Dunbar | c35d71f | 2009-04-15 16:34:29 +0000 | [diff] [blame] | 347 | |
| 348 | OptionHelp.push_back(std::make_pair("\nDEBUG/DEVELOPMENT OPTIONS:","")); |
| 349 | OptionHelp.push_back(std::make_pair("-ccc-host-triple", |
| 350 | "Simulate running on the given target")); |
| 351 | OptionHelp.push_back(std::make_pair("-ccc-print-options", |
| 352 | "Dump parsed command line arguments")); |
| 353 | OptionHelp.push_back(std::make_pair("-ccc-print-phases", |
| 354 | "Dump list of actions to perform")); |
| 355 | OptionHelp.push_back(std::make_pair("-ccc-print-bindings", |
| 356 | "Show bindings of tools to actions")); |
| 357 | OptionHelp.push_back(std::make_pair("CCC_ADD_ARGS", |
| 358 | "(ENVIRONMENT VARIABLE) Comma separated list of " |
| 359 | "arguments to prepend to the command line")); |
| 360 | } |
| 361 | |
Daniel Dunbar | 91e28af | 2009-03-31 21:38:17 +0000 | [diff] [blame] | 362 | // Find the maximum option length. |
| 363 | unsigned OptionFieldWidth = 0; |
| 364 | for (unsigned i = 0, e = OptionHelp.size(); i != e; ++i) { |
Daniel Dunbar | c35d71f | 2009-04-15 16:34:29 +0000 | [diff] [blame] | 365 | // Skip titles. |
| 366 | if (!OptionHelp[i].second) |
| 367 | continue; |
| 368 | |
Daniel Dunbar | 91e28af | 2009-03-31 21:38:17 +0000 | [diff] [blame] | 369 | // Limit the amount of padding we are willing to give up for |
| 370 | // alignment. |
| 371 | unsigned Length = OptionHelp[i].first.size(); |
| 372 | if (Length <= 23) |
| 373 | OptionFieldWidth = std::max(OptionFieldWidth, Length); |
| 374 | } |
| 375 | |
| 376 | for (unsigned i = 0, e = OptionHelp.size(); i != e; ++i) { |
| 377 | const std::string &Option = OptionHelp[i].first; |
| 378 | OS << " " << Option; |
| 379 | for (int j = Option.length(), e = OptionFieldWidth; j < e; ++j) |
| 380 | OS << ' '; |
| 381 | OS << ' ' << OptionHelp[i].second << '\n'; |
| 382 | } |
| 383 | |
| 384 | OS.flush(); |
| 385 | } |
| 386 | |
Daniel Dunbar | 7930072 | 2009-07-21 20:06:58 +0000 | [diff] [blame] | 387 | void Driver::PrintVersion(const Compilation &C, llvm::raw_ostream &OS) const { |
Mike Stump | 5d023c3 | 2009-03-18 14:00:02 +0000 | [diff] [blame] | 388 | static char buf[] = "$URL$"; |
| 389 | char *zap = strstr(buf, "/lib/Driver"); |
| 390 | if (zap) |
| 391 | *zap = 0; |
| 392 | zap = strstr(buf, "/clang/tools/clang"); |
| 393 | if (zap) |
| 394 | *zap = 0; |
Mike Stump | e70295b | 2009-03-18 15:19:35 +0000 | [diff] [blame] | 395 | const char *vers = buf+6; |
Mike Stump | 8944c38 | 2009-03-18 18:45:55 +0000 | [diff] [blame] | 396 | // FIXME: Add cmake support and remove #ifdef |
| 397 | #ifdef SVN_REVISION |
| 398 | const char *revision = SVN_REVISION; |
| 399 | #else |
| 400 | const char *revision = ""; |
| 401 | #endif |
Daniel Dunbar | cb88167 | 2009-03-13 00:51:18 +0000 | [diff] [blame] | 402 | // FIXME: The following handlers should use a callback mechanism, we |
| 403 | // don't know what the client would like to do. |
Daniel Dunbar | 7930072 | 2009-07-21 20:06:58 +0000 | [diff] [blame] | 404 | OS << "clang version " CLANG_VERSION_STRING " (" |
Daniel Dunbar | 3ee96ba | 2009-06-16 23:32:58 +0000 | [diff] [blame] | 405 | << vers << " " << revision << ")" << '\n'; |
Daniel Dunbar | 70c8db1 | 2009-03-26 16:09:13 +0000 | [diff] [blame] | 406 | |
| 407 | const ToolChain &TC = C.getDefaultToolChain(); |
Daniel Dunbar | 7930072 | 2009-07-21 20:06:58 +0000 | [diff] [blame] | 408 | OS << "Target: " << TC.getTripleString() << '\n'; |
Daniel Dunbar | 3ee96ba | 2009-06-16 23:32:58 +0000 | [diff] [blame] | 409 | |
| 410 | // Print the threading model. |
| 411 | // |
| 412 | // FIXME: Implement correctly. |
Daniel Dunbar | 7930072 | 2009-07-21 20:06:58 +0000 | [diff] [blame] | 413 | OS << "Thread model: " << "posix" << '\n'; |
Daniel Dunbar | cb88167 | 2009-03-13 00:51:18 +0000 | [diff] [blame] | 414 | } |
| 415 | |
Daniel Dunbar | 2154923 | 2009-03-18 02:55:38 +0000 | [diff] [blame] | 416 | bool Driver::HandleImmediateArgs(const Compilation &C) { |
Daniel Dunbar | cb88167 | 2009-03-13 00:51:18 +0000 | [diff] [blame] | 417 | // The order these options are handled in in gcc is all over the |
| 418 | // place, but we don't expect inconsistencies w.r.t. that to matter |
| 419 | // in practice. |
Daniel Dunbar | 91e28af | 2009-03-31 21:38:17 +0000 | [diff] [blame] | 420 | |
Daniel Dunbar | e06dc21 | 2009-04-04 05:17:38 +0000 | [diff] [blame] | 421 | if (C.getArgs().hasArg(options::OPT_dumpversion)) { |
Douglas Gregor | ab41e63 | 2009-04-27 22:23:34 +0000 | [diff] [blame] | 422 | llvm::outs() << CLANG_VERSION_STRING "\n"; |
Daniel Dunbar | e06dc21 | 2009-04-04 05:17:38 +0000 | [diff] [blame] | 423 | return false; |
| 424 | } |
| 425 | |
Daniel Dunbar | c35d71f | 2009-04-15 16:34:29 +0000 | [diff] [blame] | 426 | if (C.getArgs().hasArg(options::OPT__help) || |
| 427 | C.getArgs().hasArg(options::OPT__help_hidden)) { |
| 428 | PrintHelp(C.getArgs().hasArg(options::OPT__help_hidden)); |
Daniel Dunbar | 91e28af | 2009-03-31 21:38:17 +0000 | [diff] [blame] | 429 | return false; |
| 430 | } |
| 431 | |
Daniel Dunbar | 6cc73de | 2009-04-02 15:05:41 +0000 | [diff] [blame] | 432 | if (C.getArgs().hasArg(options::OPT__version)) { |
Daniel Dunbar | 7930072 | 2009-07-21 20:06:58 +0000 | [diff] [blame] | 433 | // Follow gcc behavior and use stdout for --version and stderr for -v |
| 434 | PrintVersion(C, llvm::outs()); |
Daniel Dunbar | 6cc73de | 2009-04-02 15:05:41 +0000 | [diff] [blame] | 435 | return false; |
| 436 | } |
| 437 | |
Daniel Dunbar | 2154923 | 2009-03-18 02:55:38 +0000 | [diff] [blame] | 438 | if (C.getArgs().hasArg(options::OPT_v) || |
| 439 | C.getArgs().hasArg(options::OPT__HASH_HASH_HASH)) { |
Daniel Dunbar | 7930072 | 2009-07-21 20:06:58 +0000 | [diff] [blame] | 440 | PrintVersion(C, llvm::errs()); |
Daniel Dunbar | cb88167 | 2009-03-13 00:51:18 +0000 | [diff] [blame] | 441 | SuppressMissingInputWarning = true; |
| 442 | } |
| 443 | |
Daniel Dunbar | 2154923 | 2009-03-18 02:55:38 +0000 | [diff] [blame] | 444 | const ToolChain &TC = C.getDefaultToolChain(); |
Daniel Dunbar | ca3459e | 2009-03-20 04:37:21 +0000 | [diff] [blame] | 445 | if (C.getArgs().hasArg(options::OPT_print_search_dirs)) { |
| 446 | llvm::outs() << "programs: ="; |
| 447 | for (ToolChain::path_list::const_iterator it = TC.getProgramPaths().begin(), |
| 448 | ie = TC.getProgramPaths().end(); it != ie; ++it) { |
| 449 | if (it != TC.getProgramPaths().begin()) |
| 450 | llvm::outs() << ':'; |
| 451 | llvm::outs() << *it; |
| 452 | } |
| 453 | llvm::outs() << "\n"; |
| 454 | llvm::outs() << "libraries: ="; |
| 455 | for (ToolChain::path_list::const_iterator it = TC.getFilePaths().begin(), |
| 456 | ie = TC.getFilePaths().end(); it != ie; ++it) { |
| 457 | if (it != TC.getFilePaths().begin()) |
| 458 | llvm::outs() << ':'; |
| 459 | llvm::outs() << *it; |
| 460 | } |
| 461 | llvm::outs() << "\n"; |
Daniel Dunbar | 91e28af | 2009-03-31 21:38:17 +0000 | [diff] [blame] | 462 | return false; |
Daniel Dunbar | ca3459e | 2009-03-20 04:37:21 +0000 | [diff] [blame] | 463 | } |
| 464 | |
Daniel Dunbar | cb88167 | 2009-03-13 00:51:18 +0000 | [diff] [blame] | 465 | // FIXME: The following handlers should use a callback mechanism, we |
| 466 | // don't know what the client would like to do. |
Daniel Dunbar | 2154923 | 2009-03-18 02:55:38 +0000 | [diff] [blame] | 467 | if (Arg *A = C.getArgs().getLastArg(options::OPT_print_file_name_EQ)) { |
| 468 | llvm::outs() << GetFilePath(A->getValue(C.getArgs()), TC).toString() |
| 469 | << "\n"; |
Daniel Dunbar | cb88167 | 2009-03-13 00:51:18 +0000 | [diff] [blame] | 470 | return false; |
| 471 | } |
| 472 | |
Daniel Dunbar | 2154923 | 2009-03-18 02:55:38 +0000 | [diff] [blame] | 473 | if (Arg *A = C.getArgs().getLastArg(options::OPT_print_prog_name_EQ)) { |
| 474 | llvm::outs() << GetProgramPath(A->getValue(C.getArgs()), TC).toString() |
| 475 | << "\n"; |
Daniel Dunbar | cb88167 | 2009-03-13 00:51:18 +0000 | [diff] [blame] | 476 | return false; |
| 477 | } |
| 478 | |
Daniel Dunbar | 2154923 | 2009-03-18 02:55:38 +0000 | [diff] [blame] | 479 | if (C.getArgs().hasArg(options::OPT_print_libgcc_file_name)) { |
Daniel Dunbar | 08c65e0 | 2009-03-27 14:26:33 +0000 | [diff] [blame] | 480 | llvm::outs() << GetFilePath("libgcc.a", TC).toString() << "\n"; |
Daniel Dunbar | cb88167 | 2009-03-13 00:51:18 +0000 | [diff] [blame] | 481 | return false; |
| 482 | } |
| 483 | |
Daniel Dunbar | 12cfe03 | 2009-06-16 23:25:22 +0000 | [diff] [blame] | 484 | if (C.getArgs().hasArg(options::OPT_print_multi_lib)) { |
| 485 | // FIXME: We need tool chain support for this. |
| 486 | llvm::outs() << ".;\n"; |
| 487 | |
| 488 | switch (C.getDefaultToolChain().getTriple().getArch()) { |
| 489 | default: |
| 490 | break; |
| 491 | |
| 492 | case llvm::Triple::x86_64: |
| 493 | llvm::outs() << "x86_64;@m64" << "\n"; |
| 494 | break; |
| 495 | |
| 496 | case llvm::Triple::ppc64: |
| 497 | llvm::outs() << "ppc64;@m64" << "\n"; |
| 498 | break; |
| 499 | } |
| 500 | return false; |
| 501 | } |
| 502 | |
| 503 | // FIXME: What is the difference between print-multi-directory and |
| 504 | // print-multi-os-directory? |
| 505 | if (C.getArgs().hasArg(options::OPT_print_multi_directory) || |
| 506 | C.getArgs().hasArg(options::OPT_print_multi_os_directory)) { |
| 507 | switch (C.getDefaultToolChain().getTriple().getArch()) { |
| 508 | default: |
| 509 | case llvm::Triple::x86: |
| 510 | case llvm::Triple::ppc: |
| 511 | llvm::outs() << "." << "\n"; |
| 512 | break; |
| 513 | |
| 514 | case llvm::Triple::x86_64: |
| 515 | llvm::outs() << "x86_64" << "\n"; |
| 516 | break; |
| 517 | |
| 518 | case llvm::Triple::ppc64: |
| 519 | llvm::outs() << "ppc64" << "\n"; |
| 520 | break; |
| 521 | } |
| 522 | return false; |
| 523 | } |
| 524 | |
Daniel Dunbar | cb88167 | 2009-03-13 00:51:18 +0000 | [diff] [blame] | 525 | return true; |
| 526 | } |
| 527 | |
Daniel Dunbar | 10ffa9a | 2009-03-18 03:13:20 +0000 | [diff] [blame] | 528 | static unsigned PrintActions1(const Compilation &C, |
Daniel Dunbar | ba10213 | 2009-03-13 12:19:02 +0000 | [diff] [blame] | 529 | Action *A, |
| 530 | std::map<Action*, unsigned> &Ids) { |
| 531 | if (Ids.count(A)) |
| 532 | return Ids[A]; |
| 533 | |
| 534 | std::string str; |
| 535 | llvm::raw_string_ostream os(str); |
| 536 | |
| 537 | os << Action::getClassName(A->getKind()) << ", "; |
| 538 | if (InputAction *IA = dyn_cast<InputAction>(A)) { |
Daniel Dunbar | 10ffa9a | 2009-03-18 03:13:20 +0000 | [diff] [blame] | 539 | os << "\"" << IA->getInputArg().getValue(C.getArgs()) << "\""; |
Daniel Dunbar | ba10213 | 2009-03-13 12:19:02 +0000 | [diff] [blame] | 540 | } else if (BindArchAction *BIA = dyn_cast<BindArchAction>(A)) { |
Daniel Dunbar | 10ffa9a | 2009-03-18 03:13:20 +0000 | [diff] [blame] | 541 | os << '"' << (BIA->getArchName() ? BIA->getArchName() : |
| 542 | C.getDefaultToolChain().getArchName()) << '"' |
| 543 | << ", {" << PrintActions1(C, *BIA->begin(), Ids) << "}"; |
Daniel Dunbar | ba10213 | 2009-03-13 12:19:02 +0000 | [diff] [blame] | 544 | } else { |
| 545 | os << "{"; |
| 546 | for (Action::iterator it = A->begin(), ie = A->end(); it != ie;) { |
Daniel Dunbar | 10ffa9a | 2009-03-18 03:13:20 +0000 | [diff] [blame] | 547 | os << PrintActions1(C, *it, Ids); |
Daniel Dunbar | ba10213 | 2009-03-13 12:19:02 +0000 | [diff] [blame] | 548 | ++it; |
| 549 | if (it != ie) |
| 550 | os << ", "; |
| 551 | } |
| 552 | os << "}"; |
| 553 | } |
| 554 | |
| 555 | unsigned Id = Ids.size(); |
| 556 | Ids[A] = Id; |
Daniel Dunbar | b269c32 | 2009-03-13 17:20:20 +0000 | [diff] [blame] | 557 | llvm::errs() << Id << ": " << os.str() << ", " |
Daniel Dunbar | ba10213 | 2009-03-13 12:19:02 +0000 | [diff] [blame] | 558 | << types::getTypeName(A->getType()) << "\n"; |
| 559 | |
| 560 | return Id; |
| 561 | } |
| 562 | |
Daniel Dunbar | 10ffa9a | 2009-03-18 03:13:20 +0000 | [diff] [blame] | 563 | void Driver::PrintActions(const Compilation &C) const { |
Daniel Dunbar | ba10213 | 2009-03-13 12:19:02 +0000 | [diff] [blame] | 564 | std::map<Action*, unsigned> Ids; |
Daniel Dunbar | 10ffa9a | 2009-03-18 03:13:20 +0000 | [diff] [blame] | 565 | for (ActionList::const_iterator it = C.getActions().begin(), |
| 566 | ie = C.getActions().end(); it != ie; ++it) |
| 567 | PrintActions1(C, *it, Ids); |
Daniel Dunbar | 53ec552 | 2009-03-12 07:58:46 +0000 | [diff] [blame] | 568 | } |
| 569 | |
Daniel Dunbar | 2154923 | 2009-03-18 02:55:38 +0000 | [diff] [blame] | 570 | void Driver::BuildUniversalActions(const ArgList &Args, |
| 571 | ActionList &Actions) const { |
Daniel Dunbar | 8f25c79 | 2009-03-18 01:38:48 +0000 | [diff] [blame] | 572 | llvm::PrettyStackTraceString CrashInfo("Building actions for universal build"); |
Daniel Dunbar | 1368954 | 2009-03-13 20:33:35 +0000 | [diff] [blame] | 573 | // Collect the list of architectures. Duplicates are allowed, but |
| 574 | // should only be handled once (in the order seen). |
| 575 | llvm::StringSet<> ArchNames; |
| 576 | llvm::SmallVector<const char *, 4> Archs; |
Daniel Dunbar | 2fe63e6 | 2009-03-12 18:40:18 +0000 | [diff] [blame] | 577 | for (ArgList::const_iterator it = Args.begin(), ie = Args.end(); |
| 578 | it != ie; ++it) { |
| 579 | Arg *A = *it; |
| 580 | |
| 581 | if (A->getOption().getId() == options::OPT_arch) { |
Daniel Dunbar | 1368954 | 2009-03-13 20:33:35 +0000 | [diff] [blame] | 582 | const char *Name = A->getValue(Args); |
| 583 | |
Daniel Dunbar | 2fe63e6 | 2009-03-12 18:40:18 +0000 | [diff] [blame] | 584 | // FIXME: We need to handle canonicalization of the specified |
| 585 | // arch? |
| 586 | |
Daniel Dunbar | 7587719 | 2009-03-19 07:55:12 +0000 | [diff] [blame] | 587 | A->claim(); |
Daniel Dunbar | 1368954 | 2009-03-13 20:33:35 +0000 | [diff] [blame] | 588 | if (ArchNames.insert(Name)) |
| 589 | Archs.push_back(Name); |
Daniel Dunbar | 2fe63e6 | 2009-03-12 18:40:18 +0000 | [diff] [blame] | 590 | } |
| 591 | } |
| 592 | |
Daniel Dunbar | 10ffa9a | 2009-03-18 03:13:20 +0000 | [diff] [blame] | 593 | // When there is no explicit arch for this platform, make sure we |
| 594 | // still bind the architecture (to the default) so that -Xarch_ is |
| 595 | // handled correctly. |
| 596 | if (!Archs.size()) |
| 597 | Archs.push_back(0); |
Daniel Dunbar | 2fe63e6 | 2009-03-12 18:40:18 +0000 | [diff] [blame] | 598 | |
| 599 | // FIXME: We killed off some others but these aren't yet detected in |
| 600 | // a functional manner. If we added information to jobs about which |
| 601 | // "auxiliary" files they wrote then we could detect the conflict |
| 602 | // these cause downstream. |
| 603 | if (Archs.size() > 1) { |
| 604 | // No recovery needed, the point of this is just to prevent |
| 605 | // overwriting the same files. |
Daniel Dunbar | 2fe63e6 | 2009-03-12 18:40:18 +0000 | [diff] [blame] | 606 | if (const Arg *A = Args.getLastArg(options::OPT_save_temps)) |
| 607 | Diag(clang::diag::err_drv_invalid_opt_with_multiple_archs) |
Daniel Dunbar | 38dd3d5 | 2009-03-20 06:14:23 +0000 | [diff] [blame] | 608 | << A->getAsString(Args); |
Daniel Dunbar | 2fe63e6 | 2009-03-12 18:40:18 +0000 | [diff] [blame] | 609 | } |
| 610 | |
| 611 | ActionList SingleActions; |
| 612 | BuildActions(Args, SingleActions); |
| 613 | |
| 614 | // Add in arch binding and lipo (if necessary) for every top level |
| 615 | // action. |
| 616 | for (unsigned i = 0, e = SingleActions.size(); i != e; ++i) { |
| 617 | Action *Act = SingleActions[i]; |
| 618 | |
| 619 | // Make sure we can lipo this kind of output. If not (and it is an |
| 620 | // actual output) then we disallow, since we can't create an |
| 621 | // output file with the right name without overwriting it. We |
| 622 | // could remove this oddity by just changing the output names to |
| 623 | // include the arch, which would also fix |
| 624 | // -save-temps. Compatibility wins for now. |
| 625 | |
Daniel Dunbar | 3dbd6c5 | 2009-03-13 17:46:02 +0000 | [diff] [blame] | 626 | if (Archs.size() > 1 && !types::canLipoType(Act->getType())) |
Daniel Dunbar | 2fe63e6 | 2009-03-12 18:40:18 +0000 | [diff] [blame] | 627 | Diag(clang::diag::err_drv_invalid_output_with_multiple_archs) |
| 628 | << types::getTypeName(Act->getType()); |
| 629 | |
| 630 | ActionList Inputs; |
Daniel Dunbar | 7587719 | 2009-03-19 07:55:12 +0000 | [diff] [blame] | 631 | for (unsigned i = 0, e = Archs.size(); i != e; ++i) |
Daniel Dunbar | 1368954 | 2009-03-13 20:33:35 +0000 | [diff] [blame] | 632 | Inputs.push_back(new BindArchAction(Act, Archs[i])); |
Daniel Dunbar | 2fe63e6 | 2009-03-12 18:40:18 +0000 | [diff] [blame] | 633 | |
| 634 | // Lipo if necessary, We do it this way because we need to set the |
| 635 | // arch flag so that -Xarch_ gets overwritten. |
| 636 | if (Inputs.size() == 1 || Act->getType() == types::TY_Nothing) |
| 637 | Actions.append(Inputs.begin(), Inputs.end()); |
| 638 | else |
| 639 | Actions.push_back(new LipoJobAction(Inputs, Act->getType())); |
| 640 | } |
Daniel Dunbar | 53ec552 | 2009-03-12 07:58:46 +0000 | [diff] [blame] | 641 | } |
| 642 | |
Daniel Dunbar | 2154923 | 2009-03-18 02:55:38 +0000 | [diff] [blame] | 643 | void Driver::BuildActions(const ArgList &Args, ActionList &Actions) const { |
Daniel Dunbar | 8f25c79 | 2009-03-18 01:38:48 +0000 | [diff] [blame] | 644 | llvm::PrettyStackTraceString CrashInfo("Building compilation actions"); |
Daniel Dunbar | af61c71 | 2009-03-12 23:55:14 +0000 | [diff] [blame] | 645 | // Start by constructing the list of inputs and their types. |
| 646 | |
Daniel Dunbar | 83dd21f | 2009-03-13 17:57:10 +0000 | [diff] [blame] | 647 | // Track the current user specified (-x) input. We also explicitly |
| 648 | // track the argument used to set the type; we only want to claim |
| 649 | // the type when we actually use it, so we warn about unused -x |
| 650 | // arguments. |
| 651 | types::ID InputType = types::TY_Nothing; |
| 652 | Arg *InputTypeArg = 0; |
| 653 | |
Daniel Dunbar | 53ec552 | 2009-03-12 07:58:46 +0000 | [diff] [blame] | 654 | llvm::SmallVector<std::pair<types::ID, const Arg*>, 16> Inputs; |
| 655 | for (ArgList::const_iterator it = Args.begin(), ie = Args.end(); |
| 656 | it != ie; ++it) { |
| 657 | Arg *A = *it; |
| 658 | |
| 659 | if (isa<InputOption>(A->getOption())) { |
| 660 | const char *Value = A->getValue(Args); |
| 661 | types::ID Ty = types::TY_INVALID; |
| 662 | |
| 663 | // Infer the input type if necessary. |
Daniel Dunbar | 83dd21f | 2009-03-13 17:57:10 +0000 | [diff] [blame] | 664 | if (InputType == types::TY_Nothing) { |
| 665 | // If there was an explicit arg for this, claim it. |
| 666 | if (InputTypeArg) |
| 667 | InputTypeArg->claim(); |
| 668 | |
Daniel Dunbar | 53ec552 | 2009-03-12 07:58:46 +0000 | [diff] [blame] | 669 | // stdin must be handled specially. |
| 670 | if (memcmp(Value, "-", 2) == 0) { |
| 671 | // If running with -E, treat as a C input (this changes the |
| 672 | // builtin macros, for example). This may be overridden by |
| 673 | // -ObjC below. |
| 674 | // |
| 675 | // Otherwise emit an error but still use a valid type to |
| 676 | // avoid spurious errors (e.g., no inputs). |
Daniel Dunbar | 8022fd4 | 2009-03-15 00:48:16 +0000 | [diff] [blame] | 677 | if (!Args.hasArg(options::OPT_E, false)) |
Daniel Dunbar | b897f5d | 2009-03-12 09:13:48 +0000 | [diff] [blame] | 678 | Diag(clang::diag::err_drv_unknown_stdin_type); |
Daniel Dunbar | 53ec552 | 2009-03-12 07:58:46 +0000 | [diff] [blame] | 679 | Ty = types::TY_C; |
| 680 | } else { |
| 681 | // Otherwise lookup by extension, and fallback to ObjectType |
Daniel Dunbar | e33bea4 | 2009-03-20 23:39:23 +0000 | [diff] [blame] | 682 | // if not found. We use a host hook here because Darwin at |
| 683 | // least has its own idea of what .s is. |
Daniel Dunbar | 53ec552 | 2009-03-12 07:58:46 +0000 | [diff] [blame] | 684 | if (const char *Ext = strrchr(Value, '.')) |
Daniel Dunbar | e33bea4 | 2009-03-20 23:39:23 +0000 | [diff] [blame] | 685 | Ty = Host->lookupTypeForExtension(Ext + 1); |
| 686 | |
Daniel Dunbar | 53ec552 | 2009-03-12 07:58:46 +0000 | [diff] [blame] | 687 | if (Ty == types::TY_INVALID) |
| 688 | Ty = types::TY_Object; |
| 689 | } |
| 690 | |
Daniel Dunbar | 683ca38 | 2009-05-18 21:47:54 +0000 | [diff] [blame] | 691 | // -ObjC and -ObjC++ override the default language, but only for "source |
| 692 | // files". We just treat everything that isn't a linker input as a |
| 693 | // source file. |
Daniel Dunbar | 53ec552 | 2009-03-12 07:58:46 +0000 | [diff] [blame] | 694 | // |
Daniel Dunbar | 683ca38 | 2009-05-18 21:47:54 +0000 | [diff] [blame] | 695 | // FIXME: Clean this up if we move the phase sequence into the type. |
Daniel Dunbar | 53ec552 | 2009-03-12 07:58:46 +0000 | [diff] [blame] | 696 | if (Ty != types::TY_Object) { |
| 697 | if (Args.hasArg(options::OPT_ObjC)) |
| 698 | Ty = types::TY_ObjC; |
| 699 | else if (Args.hasArg(options::OPT_ObjCXX)) |
| 700 | Ty = types::TY_ObjCXX; |
| 701 | } |
| 702 | } else { |
| 703 | assert(InputTypeArg && "InputType set w/o InputTypeArg"); |
| 704 | InputTypeArg->claim(); |
| 705 | Ty = InputType; |
| 706 | } |
| 707 | |
| 708 | // Check that the file exists. It isn't clear this is worth |
| 709 | // doing, since the tool presumably does this anyway, and this |
| 710 | // just adds an extra stat to the equation, but this is gcc |
| 711 | // compatible. |
| 712 | if (memcmp(Value, "-", 2) != 0 && !llvm::sys::Path(Value).exists()) |
Daniel Dunbar | b897f5d | 2009-03-12 09:13:48 +0000 | [diff] [blame] | 713 | Diag(clang::diag::err_drv_no_such_file) << A->getValue(Args); |
Daniel Dunbar | 53ec552 | 2009-03-12 07:58:46 +0000 | [diff] [blame] | 714 | else |
| 715 | Inputs.push_back(std::make_pair(Ty, A)); |
| 716 | |
| 717 | } else if (A->getOption().isLinkerInput()) { |
| 718 | // Just treat as object type, we could make a special type for |
| 719 | // this if necessary. |
| 720 | Inputs.push_back(std::make_pair(types::TY_Object, A)); |
| 721 | |
| 722 | } else if (A->getOption().getId() == options::OPT_x) { |
| 723 | InputTypeArg = A; |
| 724 | InputType = types::lookupTypeForTypeSpecifier(A->getValue(Args)); |
| 725 | |
| 726 | // Follow gcc behavior and treat as linker input for invalid -x |
| 727 | // options. Its not clear why we shouldn't just revert to |
| 728 | // unknown; but this isn't very important, we might as well be |
| 729 | // bug comatible. |
| 730 | if (!InputType) { |
Daniel Dunbar | b897f5d | 2009-03-12 09:13:48 +0000 | [diff] [blame] | 731 | Diag(clang::diag::err_drv_unknown_language) << A->getValue(Args); |
Daniel Dunbar | 53ec552 | 2009-03-12 07:58:46 +0000 | [diff] [blame] | 732 | InputType = types::TY_Object; |
| 733 | } |
| 734 | } |
| 735 | } |
| 736 | |
Daniel Dunbar | 8b1604e | 2009-03-13 00:17:48 +0000 | [diff] [blame] | 737 | if (!SuppressMissingInputWarning && Inputs.empty()) { |
Daniel Dunbar | af61c71 | 2009-03-12 23:55:14 +0000 | [diff] [blame] | 738 | Diag(clang::diag::err_drv_no_input_files); |
| 739 | return; |
| 740 | } |
| 741 | |
| 742 | // Determine which compilation mode we are in. We look for options |
| 743 | // which affect the phase, starting with the earliest phases, and |
| 744 | // record which option we used to determine the final phase. |
Daniel Dunbar | ad2a9af | 2009-03-13 11:38:42 +0000 | [diff] [blame] | 745 | Arg *FinalPhaseArg = 0; |
| 746 | phases::ID FinalPhase; |
Daniel Dunbar | af61c71 | 2009-03-12 23:55:14 +0000 | [diff] [blame] | 747 | |
| 748 | // -{E,M,MM} only run the preprocessor. |
Daniel Dunbar | ad2a9af | 2009-03-13 11:38:42 +0000 | [diff] [blame] | 749 | if ((FinalPhaseArg = Args.getLastArg(options::OPT_E)) || |
| 750 | (FinalPhaseArg = Args.getLastArg(options::OPT_M)) || |
| 751 | (FinalPhaseArg = Args.getLastArg(options::OPT_MM))) { |
| 752 | FinalPhase = phases::Preprocess; |
Daniel Dunbar | af61c71 | 2009-03-12 23:55:14 +0000 | [diff] [blame] | 753 | |
Daniel Dunbar | 8022fd4 | 2009-03-15 00:48:16 +0000 | [diff] [blame] | 754 | // -{fsyntax-only,-analyze,emit-llvm,S} only run up to the compiler. |
| 755 | } else if ((FinalPhaseArg = Args.getLastArg(options::OPT_fsyntax_only)) || |
Daniel Dunbar | 63be57a | 2009-05-06 02:12:32 +0000 | [diff] [blame] | 756 | (FinalPhaseArg = Args.getLastArg(options::OPT__analyze, |
| 757 | options::OPT__analyze_auto)) || |
Daniel Dunbar | ad2a9af | 2009-03-13 11:38:42 +0000 | [diff] [blame] | 758 | (FinalPhaseArg = Args.getLastArg(options::OPT_S))) { |
| 759 | FinalPhase = phases::Compile; |
Daniel Dunbar | af61c71 | 2009-03-12 23:55:14 +0000 | [diff] [blame] | 760 | |
| 761 | // -c only runs up to the assembler. |
Daniel Dunbar | ad2a9af | 2009-03-13 11:38:42 +0000 | [diff] [blame] | 762 | } else if ((FinalPhaseArg = Args.getLastArg(options::OPT_c))) { |
| 763 | FinalPhase = phases::Assemble; |
Daniel Dunbar | af61c71 | 2009-03-12 23:55:14 +0000 | [diff] [blame] | 764 | |
| 765 | // Otherwise do everything. |
| 766 | } else |
Daniel Dunbar | ad2a9af | 2009-03-13 11:38:42 +0000 | [diff] [blame] | 767 | FinalPhase = phases::Link; |
Daniel Dunbar | af61c71 | 2009-03-12 23:55:14 +0000 | [diff] [blame] | 768 | |
Daniel Dunbar | af61c71 | 2009-03-12 23:55:14 +0000 | [diff] [blame] | 769 | // Reject -Z* at the top level, these options should never have been |
| 770 | // exposed by gcc. |
Daniel Dunbar | d7b88c2 | 2009-03-26 16:12:09 +0000 | [diff] [blame] | 771 | if (Arg *A = Args.getLastArg(options::OPT_Z_Joined)) |
Daniel Dunbar | 38dd3d5 | 2009-03-20 06:14:23 +0000 | [diff] [blame] | 772 | Diag(clang::diag::err_drv_use_of_Z_option) << A->getAsString(Args); |
Daniel Dunbar | af61c71 | 2009-03-12 23:55:14 +0000 | [diff] [blame] | 773 | |
Daniel Dunbar | ad2a9af | 2009-03-13 11:38:42 +0000 | [diff] [blame] | 774 | // Construct the actions to perform. |
| 775 | ActionList LinkerInputs; |
Daniel Dunbar | 53ec552 | 2009-03-12 07:58:46 +0000 | [diff] [blame] | 776 | for (unsigned i = 0, e = Inputs.size(); i != e; ++i) { |
Daniel Dunbar | ad2a9af | 2009-03-13 11:38:42 +0000 | [diff] [blame] | 777 | types::ID InputType = Inputs[i].first; |
| 778 | const Arg *InputArg = Inputs[i].second; |
| 779 | |
| 780 | unsigned NumSteps = types::getNumCompilationPhases(InputType); |
| 781 | assert(NumSteps && "Invalid number of steps!"); |
| 782 | |
| 783 | // If the first step comes after the final phase we are doing as |
| 784 | // part of this compilation, warn the user about it. |
| 785 | phases::ID InitialPhase = types::getCompilationPhase(InputType, 0); |
| 786 | if (InitialPhase > FinalPhase) { |
Daniel Dunbar | 05494a7 | 2009-03-19 07:57:08 +0000 | [diff] [blame] | 787 | // Claim here to avoid the more general unused warning. |
| 788 | InputArg->claim(); |
Daniel Dunbar | ad2a9af | 2009-03-13 11:38:42 +0000 | [diff] [blame] | 789 | Diag(clang::diag::warn_drv_input_file_unused) |
Daniel Dunbar | 38dd3d5 | 2009-03-20 06:14:23 +0000 | [diff] [blame] | 790 | << InputArg->getAsString(Args) |
Daniel Dunbar | ad2a9af | 2009-03-13 11:38:42 +0000 | [diff] [blame] | 791 | << getPhaseName(InitialPhase) |
| 792 | << FinalPhaseArg->getOption().getName(); |
| 793 | continue; |
| 794 | } |
| 795 | |
| 796 | // Build the pipeline for this file. |
| 797 | Action *Current = new InputAction(*InputArg, InputType); |
| 798 | for (unsigned i = 0; i != NumSteps; ++i) { |
| 799 | phases::ID Phase = types::getCompilationPhase(InputType, i); |
| 800 | |
| 801 | // We are done if this step is past what the user requested. |
| 802 | if (Phase > FinalPhase) |
| 803 | break; |
| 804 | |
| 805 | // Queue linker inputs. |
| 806 | if (Phase == phases::Link) { |
| 807 | assert(i + 1 == NumSteps && "linking must be final compilation step."); |
| 808 | LinkerInputs.push_back(Current); |
| 809 | Current = 0; |
| 810 | break; |
| 811 | } |
| 812 | |
Daniel Dunbar | 337a627 | 2009-03-24 20:17:30 +0000 | [diff] [blame] | 813 | // Some types skip the assembler phase (e.g., llvm-bc), but we |
| 814 | // can't encode this in the steps because the intermediate type |
| 815 | // depends on arguments. Just special case here. |
| 816 | if (Phase == phases::Assemble && Current->getType() != types::TY_PP_Asm) |
| 817 | continue; |
| 818 | |
Daniel Dunbar | ad2a9af | 2009-03-13 11:38:42 +0000 | [diff] [blame] | 819 | // Otherwise construct the appropriate action. |
| 820 | Current = ConstructPhaseAction(Args, Phase, Current); |
| 821 | if (Current->getType() == types::TY_Nothing) |
| 822 | break; |
| 823 | } |
| 824 | |
| 825 | // If we ended with something, add to the output list. |
| 826 | if (Current) |
| 827 | Actions.push_back(Current); |
Daniel Dunbar | 53ec552 | 2009-03-12 07:58:46 +0000 | [diff] [blame] | 828 | } |
Daniel Dunbar | ad2a9af | 2009-03-13 11:38:42 +0000 | [diff] [blame] | 829 | |
| 830 | // Add a link action if necessary. |
| 831 | if (!LinkerInputs.empty()) |
| 832 | Actions.push_back(new LinkJobAction(LinkerInputs, types::TY_Image)); |
| 833 | } |
| 834 | |
| 835 | Action *Driver::ConstructPhaseAction(const ArgList &Args, phases::ID Phase, |
| 836 | Action *Input) const { |
Daniel Dunbar | 8f25c79 | 2009-03-18 01:38:48 +0000 | [diff] [blame] | 837 | llvm::PrettyStackTraceString CrashInfo("Constructing phase actions"); |
Daniel Dunbar | ad2a9af | 2009-03-13 11:38:42 +0000 | [diff] [blame] | 838 | // Build the appropriate action. |
| 839 | switch (Phase) { |
| 840 | case phases::Link: assert(0 && "link action invalid here."); |
| 841 | case phases::Preprocess: { |
Daniel Dunbar | cd8e4c4 | 2009-03-30 06:36:42 +0000 | [diff] [blame] | 842 | types::ID OutputTy; |
| 843 | // -{M, MM} alter the output type. |
| 844 | if (Args.hasArg(options::OPT_M) || Args.hasArg(options::OPT_MM)) { |
| 845 | OutputTy = types::TY_Dependencies; |
| 846 | } else { |
| 847 | OutputTy = types::getPreprocessedType(Input->getType()); |
| 848 | assert(OutputTy != types::TY_INVALID && |
| 849 | "Cannot preprocess this input type!"); |
| 850 | } |
Daniel Dunbar | ad2a9af | 2009-03-13 11:38:42 +0000 | [diff] [blame] | 851 | return new PreprocessJobAction(Input, OutputTy); |
| 852 | } |
| 853 | case phases::Precompile: |
| 854 | return new PrecompileJobAction(Input, types::TY_PCH); |
| 855 | case phases::Compile: { |
| 856 | if (Args.hasArg(options::OPT_fsyntax_only)) { |
| 857 | return new CompileJobAction(Input, types::TY_Nothing); |
Daniel Dunbar | 63be57a | 2009-05-06 02:12:32 +0000 | [diff] [blame] | 858 | } else if (Args.hasArg(options::OPT__analyze, options::OPT__analyze_auto)) { |
Daniel Dunbar | ad2a9af | 2009-03-13 11:38:42 +0000 | [diff] [blame] | 859 | return new AnalyzeJobAction(Input, types::TY_Plist); |
Daniel Dunbar | 337a627 | 2009-03-24 20:17:30 +0000 | [diff] [blame] | 860 | } else if (Args.hasArg(options::OPT_emit_llvm) || |
| 861 | Args.hasArg(options::OPT_flto) || |
| 862 | Args.hasArg(options::OPT_O4)) { |
Daniel Dunbar | ad2a9af | 2009-03-13 11:38:42 +0000 | [diff] [blame] | 863 | types::ID Output = |
| 864 | Args.hasArg(options::OPT_S) ? types::TY_LLVMAsm : types::TY_LLVMBC; |
| 865 | return new CompileJobAction(Input, Output); |
| 866 | } else { |
| 867 | return new CompileJobAction(Input, types::TY_PP_Asm); |
| 868 | } |
| 869 | } |
| 870 | case phases::Assemble: |
| 871 | return new AssembleJobAction(Input, types::TY_Object); |
| 872 | } |
| 873 | |
| 874 | assert(0 && "invalid phase in ConstructPhaseAction"); |
| 875 | return 0; |
Daniel Dunbar | 53ec552 | 2009-03-12 07:58:46 +0000 | [diff] [blame] | 876 | } |
| 877 | |
Daniel Dunbar | 2154923 | 2009-03-18 02:55:38 +0000 | [diff] [blame] | 878 | void Driver::BuildJobs(Compilation &C) const { |
Daniel Dunbar | 8f25c79 | 2009-03-18 01:38:48 +0000 | [diff] [blame] | 879 | llvm::PrettyStackTraceString CrashInfo("Building compilation jobs"); |
Daniel Dunbar | f353c8c | 2009-03-16 06:56:51 +0000 | [diff] [blame] | 880 | bool SaveTemps = C.getArgs().hasArg(options::OPT_save_temps); |
| 881 | bool UsePipes = C.getArgs().hasArg(options::OPT_pipe); |
Daniel Dunbar | 60ccc76 | 2009-03-18 23:18:19 +0000 | [diff] [blame] | 882 | |
| 883 | // FIXME: Pipes are forcibly disabled until we support executing |
| 884 | // them. |
| 885 | if (!CCCPrintBindings) |
| 886 | UsePipes = false; |
Daniel Dunbar | f353c8c | 2009-03-16 06:56:51 +0000 | [diff] [blame] | 887 | |
| 888 | // -save-temps inhibits pipes. |
| 889 | if (SaveTemps && UsePipes) { |
| 890 | Diag(clang::diag::warn_drv_pipe_ignored_with_save_temps); |
| 891 | UsePipes = true; |
| 892 | } |
| 893 | |
| 894 | Arg *FinalOutput = C.getArgs().getLastArg(options::OPT_o); |
| 895 | |
| 896 | // It is an error to provide a -o option if we are making multiple |
| 897 | // output files. |
| 898 | if (FinalOutput) { |
| 899 | unsigned NumOutputs = 0; |
Daniel Dunbar | 2154923 | 2009-03-18 02:55:38 +0000 | [diff] [blame] | 900 | for (ActionList::const_iterator it = C.getActions().begin(), |
| 901 | ie = C.getActions().end(); it != ie; ++it) |
Daniel Dunbar | f353c8c | 2009-03-16 06:56:51 +0000 | [diff] [blame] | 902 | if ((*it)->getType() != types::TY_Nothing) |
| 903 | ++NumOutputs; |
| 904 | |
| 905 | if (NumOutputs > 1) { |
| 906 | Diag(clang::diag::err_drv_output_argument_with_multiple_files); |
| 907 | FinalOutput = 0; |
| 908 | } |
| 909 | } |
| 910 | |
Daniel Dunbar | 2154923 | 2009-03-18 02:55:38 +0000 | [diff] [blame] | 911 | for (ActionList::const_iterator it = C.getActions().begin(), |
| 912 | ie = C.getActions().end(); it != ie; ++it) { |
Daniel Dunbar | f353c8c | 2009-03-16 06:56:51 +0000 | [diff] [blame] | 913 | Action *A = *it; |
| 914 | |
| 915 | // If we are linking an image for multiple archs then the linker |
| 916 | // wants -arch_multiple and -final_output <final image |
| 917 | // name>. Unfortunately, this doesn't fit in cleanly because we |
| 918 | // have to pass this information down. |
| 919 | // |
| 920 | // FIXME: This is a hack; find a cleaner way to integrate this |
| 921 | // into the process. |
| 922 | const char *LinkingOutput = 0; |
Daniel Dunbar | d7b88c2 | 2009-03-26 16:12:09 +0000 | [diff] [blame] | 923 | if (isa<LipoJobAction>(A)) { |
Daniel Dunbar | f353c8c | 2009-03-16 06:56:51 +0000 | [diff] [blame] | 924 | if (FinalOutput) |
| 925 | LinkingOutput = FinalOutput->getValue(C.getArgs()); |
| 926 | else |
| 927 | LinkingOutput = DefaultImageName.c_str(); |
| 928 | } |
| 929 | |
| 930 | InputInfo II; |
Daniel Dunbar | 10ffa9a | 2009-03-18 03:13:20 +0000 | [diff] [blame] | 931 | BuildJobsForAction(C, A, &C.getDefaultToolChain(), |
Daniel Dunbar | f353c8c | 2009-03-16 06:56:51 +0000 | [diff] [blame] | 932 | /*CanAcceptPipe*/ true, |
| 933 | /*AtTopLevel*/ true, |
| 934 | /*LinkingOutput*/ LinkingOutput, |
| 935 | II); |
| 936 | } |
Daniel Dunbar | 586dc23 | 2009-03-16 06:42:30 +0000 | [diff] [blame] | 937 | |
Daniel Dunbar | bf4a676 | 2009-04-03 22:09:23 +0000 | [diff] [blame] | 938 | // If the user passed -Qunused-arguments or there were errors, don't |
| 939 | // warn about any unused arguments. |
Daniel Dunbar | 1e23f5f | 2009-04-07 19:04:18 +0000 | [diff] [blame] | 940 | if (Diags.getNumErrors() || |
| 941 | C.getArgs().hasArg(options::OPT_Qunused_arguments)) |
Daniel Dunbar | af2e4ba | 2009-03-18 18:03:46 +0000 | [diff] [blame] | 942 | return; |
| 943 | |
Daniel Dunbar | a2094e7 | 2009-03-29 22:24:54 +0000 | [diff] [blame] | 944 | // Claim -### here. |
| 945 | (void) C.getArgs().hasArg(options::OPT__HASH_HASH_HASH); |
| 946 | |
Daniel Dunbar | 586dc23 | 2009-03-16 06:42:30 +0000 | [diff] [blame] | 947 | for (ArgList::const_iterator it = C.getArgs().begin(), ie = C.getArgs().end(); |
| 948 | it != ie; ++it) { |
| 949 | Arg *A = *it; |
Daniel Dunbar | af2e4ba | 2009-03-18 18:03:46 +0000 | [diff] [blame] | 950 | |
Daniel Dunbar | 586dc23 | 2009-03-16 06:42:30 +0000 | [diff] [blame] | 951 | // FIXME: It would be nice to be able to send the argument to the |
| 952 | // Diagnostic, so that extra values, position, and so on could be |
| 953 | // printed. |
Daniel Dunbar | 4f53b29 | 2009-04-04 00:52:26 +0000 | [diff] [blame] | 954 | if (!A->isClaimed()) { |
Daniel Dunbar | 1e23f5f | 2009-04-07 19:04:18 +0000 | [diff] [blame] | 955 | if (A->getOption().hasNoArgumentUnused()) |
| 956 | continue; |
| 957 | |
Daniel Dunbar | 4f53b29 | 2009-04-04 00:52:26 +0000 | [diff] [blame] | 958 | // Suppress the warning automatically if this is just a flag, |
| 959 | // and it is an instance of an argument we already claimed. |
| 960 | const Option &Opt = A->getOption(); |
| 961 | if (isa<FlagOption>(Opt)) { |
| 962 | bool DuplicateClaimed = false; |
| 963 | |
| 964 | // FIXME: Use iterator. |
| 965 | for (ArgList::const_iterator it = C.getArgs().begin(), |
| 966 | ie = C.getArgs().end(); it != ie; ++it) { |
| 967 | if ((*it)->isClaimed() && (*it)->getOption().matches(Opt.getId())) { |
| 968 | DuplicateClaimed = true; |
| 969 | break; |
| 970 | } |
| 971 | } |
| 972 | |
| 973 | if (DuplicateClaimed) |
| 974 | continue; |
| 975 | } |
| 976 | |
Daniel Dunbar | 586dc23 | 2009-03-16 06:42:30 +0000 | [diff] [blame] | 977 | Diag(clang::diag::warn_drv_unused_argument) |
Daniel Dunbar | 38dd3d5 | 2009-03-20 06:14:23 +0000 | [diff] [blame] | 978 | << A->getAsString(C.getArgs()); |
Daniel Dunbar | 4f53b29 | 2009-04-04 00:52:26 +0000 | [diff] [blame] | 979 | } |
Daniel Dunbar | 586dc23 | 2009-03-16 06:42:30 +0000 | [diff] [blame] | 980 | } |
Daniel Dunbar | 57b704d | 2009-03-13 22:12:33 +0000 | [diff] [blame] | 981 | } |
| 982 | |
Daniel Dunbar | f353c8c | 2009-03-16 06:56:51 +0000 | [diff] [blame] | 983 | void Driver::BuildJobsForAction(Compilation &C, |
| 984 | const Action *A, |
| 985 | const ToolChain *TC, |
| 986 | bool CanAcceptPipe, |
| 987 | bool AtTopLevel, |
| 988 | const char *LinkingOutput, |
| 989 | InputInfo &Result) const { |
Daniel Dunbar | 8f25c79 | 2009-03-18 01:38:48 +0000 | [diff] [blame] | 990 | llvm::PrettyStackTraceString CrashInfo("Building compilation jobs for action"); |
Daniel Dunbar | 60ccc76 | 2009-03-18 23:18:19 +0000 | [diff] [blame] | 991 | |
| 992 | bool UsePipes = C.getArgs().hasArg(options::OPT_pipe); |
| 993 | // FIXME: Pipes are forcibly disabled until we support executing |
| 994 | // them. |
| 995 | if (!CCCPrintBindings) |
| 996 | UsePipes = false; |
| 997 | |
Daniel Dunbar | f353c8c | 2009-03-16 06:56:51 +0000 | [diff] [blame] | 998 | if (const InputAction *IA = dyn_cast<InputAction>(A)) { |
Daniel Dunbar | 115a792 | 2009-03-19 07:29:38 +0000 | [diff] [blame] | 999 | // FIXME: It would be nice to not claim this here; maybe the old |
| 1000 | // scheme of just using Args was better? |
| 1001 | const Arg &Input = IA->getInputArg(); |
| 1002 | Input.claim(); |
| 1003 | if (isa<PositionalArg>(Input)) { |
| 1004 | const char *Name = Input.getValue(C.getArgs()); |
| 1005 | Result = InputInfo(Name, A->getType(), Name); |
| 1006 | } else |
| 1007 | Result = InputInfo(&Input, A->getType(), ""); |
Daniel Dunbar | f353c8c | 2009-03-16 06:56:51 +0000 | [diff] [blame] | 1008 | return; |
| 1009 | } |
| 1010 | |
| 1011 | if (const BindArchAction *BAA = dyn_cast<BindArchAction>(A)) { |
| 1012 | const char *ArchName = BAA->getArchName(); |
Daniel Dunbar | cb8ab23 | 2009-05-22 02:53:45 +0000 | [diff] [blame] | 1013 | std::string Arch; |
| 1014 | if (!ArchName) { |
| 1015 | Arch = C.getDefaultToolChain().getArchName(); |
| 1016 | ArchName = Arch.c_str(); |
| 1017 | } |
Daniel Dunbar | f353c8c | 2009-03-16 06:56:51 +0000 | [diff] [blame] | 1018 | BuildJobsForAction(C, |
| 1019 | *BAA->begin(), |
| 1020 | Host->getToolChain(C.getArgs(), ArchName), |
| 1021 | CanAcceptPipe, |
| 1022 | AtTopLevel, |
| 1023 | LinkingOutput, |
| 1024 | Result); |
| 1025 | return; |
| 1026 | } |
| 1027 | |
| 1028 | const JobAction *JA = cast<JobAction>(A); |
| 1029 | const Tool &T = TC->SelectTool(C, *JA); |
| 1030 | |
| 1031 | // See if we should use an integrated preprocessor. We do so when we |
| 1032 | // have exactly one input, since this is the only use case we care |
| 1033 | // about (irrelevant since we don't support combine yet). |
| 1034 | bool UseIntegratedCPP = false; |
| 1035 | const ActionList *Inputs = &A->getInputs(); |
| 1036 | if (Inputs->size() == 1 && isa<PreprocessJobAction>(*Inputs->begin())) { |
| 1037 | if (!C.getArgs().hasArg(options::OPT_no_integrated_cpp) && |
| 1038 | !C.getArgs().hasArg(options::OPT_traditional_cpp) && |
| 1039 | !C.getArgs().hasArg(options::OPT_save_temps) && |
| 1040 | T.hasIntegratedCPP()) { |
| 1041 | UseIntegratedCPP = true; |
| 1042 | Inputs = &(*Inputs)[0]->getInputs(); |
| 1043 | } |
| 1044 | } |
| 1045 | |
| 1046 | // Only use pipes when there is exactly one input. |
| 1047 | bool TryToUsePipeInput = Inputs->size() == 1 && T.acceptsPipedInput(); |
Daniel Dunbar | 47ac7d2 | 2009-03-18 06:00:36 +0000 | [diff] [blame] | 1048 | InputInfoList InputInfos; |
Daniel Dunbar | f353c8c | 2009-03-16 06:56:51 +0000 | [diff] [blame] | 1049 | for (ActionList::const_iterator it = Inputs->begin(), ie = Inputs->end(); |
| 1050 | it != ie; ++it) { |
| 1051 | InputInfo II; |
| 1052 | BuildJobsForAction(C, *it, TC, TryToUsePipeInput, |
| 1053 | /*AtTopLevel*/false, |
| 1054 | LinkingOutput, |
| 1055 | II); |
| 1056 | InputInfos.push_back(II); |
| 1057 | } |
| 1058 | |
| 1059 | // Determine if we should output to a pipe. |
| 1060 | bool OutputToPipe = false; |
| 1061 | if (CanAcceptPipe && T.canPipeOutput()) { |
| 1062 | // Some actions default to writing to a pipe if they are the top |
| 1063 | // level phase and there was no user override. |
| 1064 | // |
| 1065 | // FIXME: Is there a better way to handle this? |
| 1066 | if (AtTopLevel) { |
| 1067 | if (isa<PreprocessJobAction>(A) && !C.getArgs().hasArg(options::OPT_o)) |
| 1068 | OutputToPipe = true; |
Daniel Dunbar | 60ccc76 | 2009-03-18 23:18:19 +0000 | [diff] [blame] | 1069 | } else if (UsePipes) |
Daniel Dunbar | f353c8c | 2009-03-16 06:56:51 +0000 | [diff] [blame] | 1070 | OutputToPipe = true; |
| 1071 | } |
| 1072 | |
| 1073 | // Figure out where to put the job (pipes). |
| 1074 | Job *Dest = &C.getJobs(); |
| 1075 | if (InputInfos[0].isPipe()) { |
Daniel Dunbar | 441d060 | 2009-03-17 17:53:55 +0000 | [diff] [blame] | 1076 | assert(TryToUsePipeInput && "Unrequested pipe!"); |
Daniel Dunbar | f353c8c | 2009-03-16 06:56:51 +0000 | [diff] [blame] | 1077 | assert(InputInfos.size() == 1 && "Unexpected pipe with multiple inputs."); |
| 1078 | Dest = &InputInfos[0].getPipe(); |
| 1079 | } |
| 1080 | |
| 1081 | // Always use the first input as the base input. |
| 1082 | const char *BaseInput = InputInfos[0].getBaseInput(); |
Daniel Dunbar | 441d060 | 2009-03-17 17:53:55 +0000 | [diff] [blame] | 1083 | |
| 1084 | // Determine the place to write output to (nothing, pipe, or |
| 1085 | // filename) and where to put the new job. |
Daniel Dunbar | 441d060 | 2009-03-17 17:53:55 +0000 | [diff] [blame] | 1086 | if (JA->getType() == types::TY_Nothing) { |
Daniel Dunbar | 5c3c1d7 | 2009-03-17 22:47:06 +0000 | [diff] [blame] | 1087 | Result = InputInfo(A->getType(), BaseInput); |
Daniel Dunbar | 441d060 | 2009-03-17 17:53:55 +0000 | [diff] [blame] | 1088 | } else if (OutputToPipe) { |
| 1089 | // Append to current piped job or create a new one as appropriate. |
Daniel Dunbar | 5c3c1d7 | 2009-03-17 22:47:06 +0000 | [diff] [blame] | 1090 | PipedJob *PJ = dyn_cast<PipedJob>(Dest); |
| 1091 | if (!PJ) { |
| 1092 | PJ = new PipedJob(); |
Daniel Dunbar | b7b61b2 | 2009-03-20 00:11:04 +0000 | [diff] [blame] | 1093 | // FIXME: Temporary hack so that -ccc-print-bindings work until |
| 1094 | // we have pipe support. Please remove later. |
| 1095 | if (!CCCPrintBindings) |
| 1096 | cast<JobList>(Dest)->addJob(PJ); |
Daniel Dunbar | 871adcf | 2009-03-18 07:06:02 +0000 | [diff] [blame] | 1097 | Dest = PJ; |
Daniel Dunbar | 441d060 | 2009-03-17 17:53:55 +0000 | [diff] [blame] | 1098 | } |
Daniel Dunbar | 5c3c1d7 | 2009-03-17 22:47:06 +0000 | [diff] [blame] | 1099 | Result = InputInfo(PJ, A->getType(), BaseInput); |
Daniel Dunbar | 441d060 | 2009-03-17 17:53:55 +0000 | [diff] [blame] | 1100 | } else { |
Daniel Dunbar | 5c3c1d7 | 2009-03-17 22:47:06 +0000 | [diff] [blame] | 1101 | Result = InputInfo(GetNamedOutputPath(C, *JA, BaseInput, AtTopLevel), |
| 1102 | A->getType(), BaseInput); |
Daniel Dunbar | 441d060 | 2009-03-17 17:53:55 +0000 | [diff] [blame] | 1103 | } |
| 1104 | |
Daniel Dunbar | 5c3c1d7 | 2009-03-17 22:47:06 +0000 | [diff] [blame] | 1105 | if (CCCPrintBindings) { |
Daniel Dunbar | cd8e4c4 | 2009-03-30 06:36:42 +0000 | [diff] [blame] | 1106 | llvm::errs() << "# \"" << T.getToolChain().getTripleString() << '"' |
| 1107 | << " - \"" << T.getName() << "\", inputs: ["; |
Daniel Dunbar | 5c3c1d7 | 2009-03-17 22:47:06 +0000 | [diff] [blame] | 1108 | for (unsigned i = 0, e = InputInfos.size(); i != e; ++i) { |
| 1109 | llvm::errs() << InputInfos[i].getAsString(); |
| 1110 | if (i + 1 != e) |
| 1111 | llvm::errs() << ", "; |
| 1112 | } |
| 1113 | llvm::errs() << "], output: " << Result.getAsString() << "\n"; |
| 1114 | } else { |
Daniel Dunbar | f3cad36 | 2009-03-25 04:13:45 +0000 | [diff] [blame] | 1115 | T.ConstructJob(C, *JA, *Dest, Result, InputInfos, |
| 1116 | C.getArgsForToolChain(TC), LinkingOutput); |
Daniel Dunbar | 5c3c1d7 | 2009-03-17 22:47:06 +0000 | [diff] [blame] | 1117 | } |
Daniel Dunbar | f353c8c | 2009-03-16 06:56:51 +0000 | [diff] [blame] | 1118 | } |
| 1119 | |
Daniel Dunbar | 441d060 | 2009-03-17 17:53:55 +0000 | [diff] [blame] | 1120 | const char *Driver::GetNamedOutputPath(Compilation &C, |
| 1121 | const JobAction &JA, |
| 1122 | const char *BaseInput, |
| 1123 | bool AtTopLevel) const { |
Daniel Dunbar | 8f25c79 | 2009-03-18 01:38:48 +0000 | [diff] [blame] | 1124 | llvm::PrettyStackTraceString CrashInfo("Computing output path"); |
Daniel Dunbar | 441d060 | 2009-03-17 17:53:55 +0000 | [diff] [blame] | 1125 | // Output to a user requested destination? |
| 1126 | if (AtTopLevel) { |
| 1127 | if (Arg *FinalOutput = C.getArgs().getLastArg(options::OPT_o)) |
| 1128 | return C.addResultFile(FinalOutput->getValue(C.getArgs())); |
| 1129 | } |
| 1130 | |
| 1131 | // Output to a temporary file? |
| 1132 | if (!AtTopLevel && !C.getArgs().hasArg(options::OPT_save_temps)) { |
Daniel Dunbar | 214399e | 2009-03-18 19:34:39 +0000 | [diff] [blame] | 1133 | std::string TmpName = |
| 1134 | GetTemporaryPath(types::getTypeTempSuffix(JA.getType())); |
| 1135 | return C.addTempFile(C.getArgs().MakeArgString(TmpName.c_str())); |
Daniel Dunbar | 441d060 | 2009-03-17 17:53:55 +0000 | [diff] [blame] | 1136 | } |
| 1137 | |
| 1138 | llvm::sys::Path BasePath(BaseInput); |
Daniel Dunbar | 5796bf4 | 2009-03-18 02:00:31 +0000 | [diff] [blame] | 1139 | std::string BaseName(BasePath.getLast()); |
Daniel Dunbar | 441d060 | 2009-03-17 17:53:55 +0000 | [diff] [blame] | 1140 | |
| 1141 | // Determine what the derived output name should be. |
| 1142 | const char *NamedOutput; |
| 1143 | if (JA.getType() == types::TY_Image) { |
| 1144 | NamedOutput = DefaultImageName.c_str(); |
| 1145 | } else { |
| 1146 | const char *Suffix = types::getTypeTempSuffix(JA.getType()); |
| 1147 | assert(Suffix && "All types used for output should have a suffix."); |
| 1148 | |
| 1149 | std::string::size_type End = std::string::npos; |
| 1150 | if (!types::appendSuffixForType(JA.getType())) |
| 1151 | End = BaseName.rfind('.'); |
| 1152 | std::string Suffixed(BaseName.substr(0, End)); |
| 1153 | Suffixed += '.'; |
| 1154 | Suffixed += Suffix; |
| 1155 | NamedOutput = C.getArgs().MakeArgString(Suffixed.c_str()); |
| 1156 | } |
| 1157 | |
| 1158 | // As an annoying special case, PCH generation doesn't strip the |
| 1159 | // pathname. |
| 1160 | if (JA.getType() == types::TY_PCH) { |
| 1161 | BasePath.eraseComponent(); |
Daniel Dunbar | 56c5594 | 2009-03-18 09:58:30 +0000 | [diff] [blame] | 1162 | if (BasePath.isEmpty()) |
| 1163 | BasePath = NamedOutput; |
| 1164 | else |
| 1165 | BasePath.appendComponent(NamedOutput); |
Daniel Dunbar | 441d060 | 2009-03-17 17:53:55 +0000 | [diff] [blame] | 1166 | return C.addResultFile(C.getArgs().MakeArgString(BasePath.c_str())); |
| 1167 | } else { |
| 1168 | return C.addResultFile(NamedOutput); |
| 1169 | } |
| 1170 | } |
| 1171 | |
Daniel Dunbar | 2ba38ba | 2009-03-16 05:25:36 +0000 | [diff] [blame] | 1172 | llvm::sys::Path Driver::GetFilePath(const char *Name, |
Daniel Dunbar | 2154923 | 2009-03-18 02:55:38 +0000 | [diff] [blame] | 1173 | const ToolChain &TC) const { |
Daniel Dunbar | 0edefeb | 2009-03-18 20:26:19 +0000 | [diff] [blame] | 1174 | const ToolChain::path_list &List = TC.getFilePaths(); |
| 1175 | for (ToolChain::path_list::const_iterator |
| 1176 | it = List.begin(), ie = List.end(); it != ie; ++it) { |
| 1177 | llvm::sys::Path P(*it); |
| 1178 | P.appendComponent(Name); |
| 1179 | if (P.exists()) |
| 1180 | return P; |
| 1181 | } |
| 1182 | |
Daniel Dunbar | cb88167 | 2009-03-13 00:51:18 +0000 | [diff] [blame] | 1183 | return llvm::sys::Path(Name); |
| 1184 | } |
| 1185 | |
Daniel Dunbar | 2ba38ba | 2009-03-16 05:25:36 +0000 | [diff] [blame] | 1186 | llvm::sys::Path Driver::GetProgramPath(const char *Name, |
Mike Stump | 950bedd | 2009-03-27 00:40:20 +0000 | [diff] [blame] | 1187 | const ToolChain &TC, |
| 1188 | bool WantFile) const { |
Daniel Dunbar | 0edefeb | 2009-03-18 20:26:19 +0000 | [diff] [blame] | 1189 | const ToolChain::path_list &List = TC.getProgramPaths(); |
| 1190 | for (ToolChain::path_list::const_iterator |
| 1191 | it = List.begin(), ie = List.end(); it != ie; ++it) { |
| 1192 | llvm::sys::Path P(*it); |
| 1193 | P.appendComponent(Name); |
Mike Stump | 950bedd | 2009-03-27 00:40:20 +0000 | [diff] [blame] | 1194 | if (WantFile ? P.exists() : P.canExecute()) |
Daniel Dunbar | 0edefeb | 2009-03-18 20:26:19 +0000 | [diff] [blame] | 1195 | return P; |
| 1196 | } |
| 1197 | |
Daniel Dunbar | c50b00d | 2009-03-23 16:15:50 +0000 | [diff] [blame] | 1198 | // If all else failed, search the path. |
| 1199 | llvm::sys::Path P(llvm::sys::Program::FindProgramByName(Name)); |
Daniel Dunbar | 632f50e | 2009-03-18 21:34:08 +0000 | [diff] [blame] | 1200 | if (!P.empty()) |
| 1201 | return P; |
| 1202 | |
Daniel Dunbar | cb88167 | 2009-03-13 00:51:18 +0000 | [diff] [blame] | 1203 | return llvm::sys::Path(Name); |
| 1204 | } |
| 1205 | |
Daniel Dunbar | 214399e | 2009-03-18 19:34:39 +0000 | [diff] [blame] | 1206 | std::string Driver::GetTemporaryPath(const char *Suffix) const { |
| 1207 | // FIXME: This is lame; sys::Path should provide this function (in |
| 1208 | // particular, it should know how to find the temporary files dir). |
| 1209 | std::string Error; |
Daniel Dunbar | b03417f | 2009-04-20 20:28:21 +0000 | [diff] [blame] | 1210 | const char *TmpDir = ::getenv("TMPDIR"); |
| 1211 | if (!TmpDir) |
| 1212 | TmpDir = ::getenv("TEMP"); |
| 1213 | if (!TmpDir) |
Daniel Dunbar | 3ca7ee9 | 2009-04-21 00:25:10 +0000 | [diff] [blame] | 1214 | TmpDir = ::getenv("TMP"); |
| 1215 | if (!TmpDir) |
Daniel Dunbar | b03417f | 2009-04-20 20:28:21 +0000 | [diff] [blame] | 1216 | TmpDir = "/tmp"; |
| 1217 | llvm::sys::Path P(TmpDir); |
Daniel Dunbar | f60c63a | 2009-04-20 17:32:49 +0000 | [diff] [blame] | 1218 | P.appendComponent("cc"); |
Daniel Dunbar | 214399e | 2009-03-18 19:34:39 +0000 | [diff] [blame] | 1219 | if (P.makeUnique(false, &Error)) { |
| 1220 | Diag(clang::diag::err_drv_unable_to_make_temp) << Error; |
| 1221 | return ""; |
| 1222 | } |
| 1223 | |
Daniel Dunbar | 84603bc | 2009-03-18 23:08:52 +0000 | [diff] [blame] | 1224 | // FIXME: Grumble, makeUnique sometimes leaves the file around!? |
| 1225 | // PR3837. |
| 1226 | P.eraseFromDisk(false, 0); |
| 1227 | |
Daniel Dunbar | 214399e | 2009-03-18 19:34:39 +0000 | [diff] [blame] | 1228 | P.appendSuffix(Suffix); |
| 1229 | return P.toString(); |
| 1230 | } |
| 1231 | |
Daniel Dunbar | cb8ab23 | 2009-05-22 02:53:45 +0000 | [diff] [blame] | 1232 | const HostInfo *Driver::GetHostInfo(const char *TripleStr) const { |
Daniel Dunbar | 8f25c79 | 2009-03-18 01:38:48 +0000 | [diff] [blame] | 1233 | llvm::PrettyStackTraceString CrashInfo("Constructing host"); |
Daniel Dunbar | cb8ab23 | 2009-05-22 02:53:45 +0000 | [diff] [blame] | 1234 | llvm::Triple Triple(TripleStr); |
Daniel Dunbar | dd98e2c | 2009-03-10 23:41:59 +0000 | [diff] [blame] | 1235 | |
Daniel Dunbar | 1fd6c4b | 2009-03-17 19:00:50 +0000 | [diff] [blame] | 1236 | // Normalize Arch a bit. |
| 1237 | // |
Daniel Dunbar | cb8ab23 | 2009-05-22 02:53:45 +0000 | [diff] [blame] | 1238 | // FIXME: We shouldn't need to do this once everything goes through the triple |
| 1239 | // interface. |
| 1240 | if (Triple.getArchName() == "i686") |
| 1241 | Triple.setArchName("i386"); |
| 1242 | else if (Triple.getArchName() == "amd64") |
| 1243 | Triple.setArchName("x86_64"); |
| 1244 | else if (Triple.getArchName() == "ppc" || |
| 1245 | Triple.getArchName() == "Power Macintosh") |
| 1246 | Triple.setArchName("powerpc"); |
| 1247 | else if (Triple.getArchName() == "ppc64") |
| 1248 | Triple.setArchName("powerpc64"); |
Daniel Dunbar | 11e1b40 | 2009-05-02 18:28:39 +0000 | [diff] [blame] | 1249 | |
Daniel Dunbar | cb8ab23 | 2009-05-22 02:53:45 +0000 | [diff] [blame] | 1250 | switch (Triple.getOS()) { |
| 1251 | case llvm::Triple::Darwin: |
| 1252 | return createDarwinHostInfo(*this, Triple); |
| 1253 | case llvm::Triple::DragonFly: |
| 1254 | return createDragonFlyHostInfo(*this, Triple); |
Daniel Dunbar | f7b8eec | 2009-06-29 20:52:51 +0000 | [diff] [blame] | 1255 | case llvm::Triple::OpenBSD: |
| 1256 | return createOpenBSDHostInfo(*this, Triple); |
Daniel Dunbar | cb8ab23 | 2009-05-22 02:53:45 +0000 | [diff] [blame] | 1257 | case llvm::Triple::FreeBSD: |
| 1258 | return createFreeBSDHostInfo(*this, Triple); |
Eli Friedman | 6b3454a | 2009-05-26 07:52:18 +0000 | [diff] [blame] | 1259 | case llvm::Triple::Linux: |
| 1260 | return createLinuxHostInfo(*this, Triple); |
Daniel Dunbar | cb8ab23 | 2009-05-22 02:53:45 +0000 | [diff] [blame] | 1261 | default: |
| 1262 | return createUnknownHostInfo(*this, Triple); |
| 1263 | } |
Daniel Dunbar | dd98e2c | 2009-03-10 23:41:59 +0000 | [diff] [blame] | 1264 | } |
Daniel Dunbar | af80e1f | 2009-03-24 18:57:02 +0000 | [diff] [blame] | 1265 | |
| 1266 | bool Driver::ShouldUseClangCompiler(const Compilation &C, const JobAction &JA, |
Daniel Dunbar | bf54a06 | 2009-04-01 20:33:11 +0000 | [diff] [blame] | 1267 | const std::string &ArchNameStr) const { |
| 1268 | // FIXME: Remove this hack. |
| 1269 | const char *ArchName = ArchNameStr.c_str(); |
| 1270 | if (ArchNameStr == "powerpc") |
| 1271 | ArchName = "ppc"; |
| 1272 | else if (ArchNameStr == "powerpc64") |
| 1273 | ArchName = "ppc64"; |
| 1274 | |
Daniel Dunbar | af80e1f | 2009-03-24 18:57:02 +0000 | [diff] [blame] | 1275 | // Check if user requested no clang, or clang doesn't understand |
| 1276 | // this type (we only handle single inputs for now). |
Daniel Dunbar | 0f99d2e | 2009-03-24 19:02:31 +0000 | [diff] [blame] | 1277 | if (!CCCUseClang || JA.size() != 1 || |
Daniel Dunbar | af80e1f | 2009-03-24 18:57:02 +0000 | [diff] [blame] | 1278 | !types::isAcceptedByClang((*JA.begin())->getType())) |
| 1279 | return false; |
| 1280 | |
Daniel Dunbar | 0f99d2e | 2009-03-24 19:02:31 +0000 | [diff] [blame] | 1281 | // Otherwise make sure this is an action clang understands. |
Daniel Dunbar | af80e1f | 2009-03-24 18:57:02 +0000 | [diff] [blame] | 1282 | if (isa<PreprocessJobAction>(JA)) { |
Daniel Dunbar | 6256d36 | 2009-03-24 19:14:56 +0000 | [diff] [blame] | 1283 | if (!CCCUseClangCPP) { |
| 1284 | Diag(clang::diag::warn_drv_not_using_clang_cpp); |
Daniel Dunbar | af80e1f | 2009-03-24 18:57:02 +0000 | [diff] [blame] | 1285 | return false; |
Daniel Dunbar | 6256d36 | 2009-03-24 19:14:56 +0000 | [diff] [blame] | 1286 | } |
Daniel Dunbar | af80e1f | 2009-03-24 18:57:02 +0000 | [diff] [blame] | 1287 | } else if (!isa<PrecompileJobAction>(JA) && !isa<CompileJobAction>(JA)) |
| 1288 | return false; |
| 1289 | |
Daniel Dunbar | 0f99d2e | 2009-03-24 19:02:31 +0000 | [diff] [blame] | 1290 | // Use clang for C++? |
Daniel Dunbar | 6256d36 | 2009-03-24 19:14:56 +0000 | [diff] [blame] | 1291 | if (!CCCUseClangCXX && types::isCXX((*JA.begin())->getType())) { |
| 1292 | Diag(clang::diag::warn_drv_not_using_clang_cxx); |
Daniel Dunbar | af80e1f | 2009-03-24 18:57:02 +0000 | [diff] [blame] | 1293 | return false; |
Daniel Dunbar | 6256d36 | 2009-03-24 19:14:56 +0000 | [diff] [blame] | 1294 | } |
Daniel Dunbar | af80e1f | 2009-03-24 18:57:02 +0000 | [diff] [blame] | 1295 | |
Daniel Dunbar | fec26bd | 2009-04-16 23:10:13 +0000 | [diff] [blame] | 1296 | // Always use clang for precompiling, regardless of archs. PTH is |
| 1297 | // platform independent, and this allows the use of the static |
| 1298 | // analyzer on platforms we don't have full IRgen support for. |
| 1299 | if (isa<PrecompileJobAction>(JA)) |
| 1300 | return true; |
| 1301 | |
Daniel Dunbar | af80e1f | 2009-03-24 18:57:02 +0000 | [diff] [blame] | 1302 | // Finally, don't use clang if this isn't one of the user specified |
| 1303 | // archs to build. |
Daniel Dunbar | 6256d36 | 2009-03-24 19:14:56 +0000 | [diff] [blame] | 1304 | if (!CCCClangArchs.empty() && !CCCClangArchs.count(ArchName)) { |
| 1305 | Diag(clang::diag::warn_drv_not_using_clang_arch) << ArchName; |
Daniel Dunbar | af80e1f | 2009-03-24 18:57:02 +0000 | [diff] [blame] | 1306 | return false; |
Daniel Dunbar | 6256d36 | 2009-03-24 19:14:56 +0000 | [diff] [blame] | 1307 | } |
Daniel Dunbar | af80e1f | 2009-03-24 18:57:02 +0000 | [diff] [blame] | 1308 | |
| 1309 | return true; |
| 1310 | } |
Daniel Dunbar | d73fe9b | 2009-03-26 15:58:36 +0000 | [diff] [blame] | 1311 | |
| 1312 | /// GetReleaseVersion - Parse (([0-9]+)(.([0-9]+)(.([0-9]+)?))?)? and |
| 1313 | /// return the grouped values as integers. Numbers which are not |
| 1314 | /// provided are set to 0. |
| 1315 | /// |
| 1316 | /// \return True if the entire string was parsed (9.2), or all groups |
| 1317 | /// were parsed (10.3.5extrastuff). |
| 1318 | bool Driver::GetReleaseVersion(const char *Str, unsigned &Major, |
| 1319 | unsigned &Minor, unsigned &Micro, |
| 1320 | bool &HadExtra) { |
| 1321 | HadExtra = false; |
| 1322 | |
| 1323 | Major = Minor = Micro = 0; |
| 1324 | if (*Str == '\0') |
| 1325 | return true; |
| 1326 | |
| 1327 | char *End; |
| 1328 | Major = (unsigned) strtol(Str, &End, 10); |
| 1329 | if (*Str != '\0' && *End == '\0') |
| 1330 | return true; |
| 1331 | if (*End != '.') |
| 1332 | return false; |
| 1333 | |
| 1334 | Str = End+1; |
| 1335 | Minor = (unsigned) strtol(Str, &End, 10); |
| 1336 | if (*Str != '\0' && *End == '\0') |
| 1337 | return true; |
| 1338 | if (*End != '.') |
| 1339 | return false; |
| 1340 | |
| 1341 | Str = End+1; |
| 1342 | Micro = (unsigned) strtol(Str, &End, 10); |
| 1343 | if (*Str != '\0' && *End == '\0') |
| 1344 | return true; |
| 1345 | if (Str == End) |
| 1346 | return false; |
| 1347 | HadExtra = true; |
| 1348 | return true; |
| 1349 | } |