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