1 //===- DriverUtils.cpp ----------------------------------------------------===// 2 // 3 // The LLVM Linker 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 // 10 // This file contains utility functions for the driver. Because there 11 // are so many small functions, we created this separate file to make 12 // Driver.cpp less cluttered. 13 // 14 //===----------------------------------------------------------------------===// 15 16 #include "Driver.h" 17 #include "Error.h" 18 #include "Memory.h" 19 #include "ScriptParser.h" 20 #include "lld/Config/Version.h" 21 #include "lld/Core/Reproduce.h" 22 #include "llvm/ADT/Optional.h" 23 #include "llvm/ADT/STLExtras.h" 24 #include "llvm/ADT/Triple.h" 25 #include "llvm/Option/Option.h" 26 #include "llvm/Support/CommandLine.h" 27 #include "llvm/Support/FileSystem.h" 28 #include "llvm/Support/Path.h" 29 #include "llvm/Support/Process.h" 30 31 using namespace llvm; 32 using namespace llvm::sys; 33 34 using namespace lld; 35 using namespace lld::elf; 36 37 // Create OptTable 38 39 // Create prefix string literals used in Options.td 40 #define PREFIX(NAME, VALUE) const char *const NAME[] = VALUE; 41 #include "Options.inc" 42 #undef PREFIX 43 44 // Create table mapping all options defined in Options.td 45 static const opt::OptTable::Info OptInfo[] = { 46 #define OPTION(X1, X2, ID, KIND, GROUP, ALIAS, X6, X7, X8, X9, X10) \ 47 {X1, X2, X9, X10, OPT_##ID, opt::Option::KIND##Class, \ 48 X8, X7, OPT_##GROUP, OPT_##ALIAS, X6}, 49 #include "Options.inc" 50 #undef OPTION 51 }; 52 53 ELFOptTable::ELFOptTable() : OptTable(OptInfo) {} 54 55 // Parse -color-diagnostics={auto,always,never} or -no-color-diagnostics. 56 static bool getColorDiagnostics(opt::InputArgList &Args) { 57 bool Default = (ErrorOS == &errs() && Process::StandardErrHasColors()); 58 59 auto *Arg = Args.getLastArg(OPT_color_diagnostics, OPT_color_diagnostics_eq, 60 OPT_no_color_diagnostics); 61 if (!Arg) 62 return Default; 63 if (Arg->getOption().getID() == OPT_color_diagnostics) 64 return true; 65 if (Arg->getOption().getID() == OPT_no_color_diagnostics) 66 return false; 67 68 StringRef S = Arg->getValue(); 69 if (S == "auto") 70 return Default; 71 if (S == "always") 72 return true; 73 if (S != "never") 74 error("unknown option: -color-diagnostics=" + S); 75 return false; 76 } 77 78 static cl::TokenizerCallback getQuotingStyle(opt::InputArgList &Args) { 79 if (auto *Arg = Args.getLastArg(OPT_rsp_quoting)) { 80 StringRef S = Arg->getValue(); 81 if (S != "windows" && S != "posix") 82 error("invalid response file quoting: " + S); 83 if (S == "windows") 84 return cl::TokenizeWindowsCommandLine; 85 return cl::TokenizeGNUCommandLine; 86 } 87 if (Triple(sys::getProcessTriple()).getOS() == Triple::Win32) 88 return cl::TokenizeWindowsCommandLine; 89 return cl::TokenizeGNUCommandLine; 90 } 91 92 // Parses a given list of options. 93 opt::InputArgList ELFOptTable::parse(ArrayRef<const char *> Argv) { 94 // Make InputArgList from string vectors. 95 unsigned MissingIndex; 96 unsigned MissingCount; 97 SmallVector<const char *, 256> Vec(Argv.data(), Argv.data() + Argv.size()); 98 99 // We need to get the quoting style for response files before parsing all 100 // options so we parse here before and ignore all the options but 101 // --rsp-quoting. 102 opt::InputArgList Args = this->ParseArgs(Vec, MissingIndex, MissingCount); 103 104 // Expand response files (arguments in the form of @<filename>) 105 // and then parse the argument again. 106 cl::ExpandResponseFiles(Saver, getQuotingStyle(Args), Vec); 107 Args = this->ParseArgs(Vec, MissingIndex, MissingCount); 108 109 // Interpret -color-diagnostics early so that error messages 110 // for unknown flags are colored. 111 Config->ColorDiagnostics = getColorDiagnostics(Args); 112 if (MissingCount) 113 error(Twine(Args.getArgString(MissingIndex)) + ": missing argument"); 114 115 for (auto *Arg : Args.filtered(OPT_UNKNOWN)) 116 error("unknown argument: " + Arg->getSpelling()); 117 return Args; 118 } 119 120 void elf::printHelp(const char *Argv0) { 121 ELFOptTable Table; 122 Table.PrintHelp(outs(), Argv0, "lld", false); 123 } 124 125 // Reconstructs command line arguments so that so that you can re-run 126 // the same command with the same inputs. This is for --reproduce. 127 std::string elf::createResponseFile(const opt::InputArgList &Args) { 128 SmallString<0> Data; 129 raw_svector_ostream OS(Data); 130 131 // Copy the command line to the output while rewriting paths. 132 for (auto *Arg : Args) { 133 switch (Arg->getOption().getID()) { 134 case OPT_reproduce: 135 break; 136 case OPT_INPUT: 137 OS << quote(rewritePath(Arg->getValue())) << "\n"; 138 break; 139 case OPT_L: 140 case OPT_dynamic_list: 141 case OPT_rpath: 142 case OPT_alias_script_T: 143 case OPT_script: 144 case OPT_version_script: 145 OS << Arg->getSpelling() << " " << quote(rewritePath(Arg->getValue())) 146 << "\n"; 147 break; 148 default: 149 OS << toString(Arg) << "\n"; 150 } 151 } 152 return Data.str(); 153 } 154 155 // Find a file by concatenating given paths. If a resulting path 156 // starts with "=", the character is replaced with a --sysroot value. 157 static Optional<std::string> findFile(StringRef Path1, const Twine &Path2) { 158 SmallString<128> S; 159 if (Path1.startswith("=")) 160 path::append(S, Config->Sysroot, Path1.substr(1), Path2); 161 else 162 path::append(S, Path1, Path2); 163 164 if (fs::exists(S)) 165 return S.str().str(); 166 return None; 167 } 168 169 Optional<std::string> elf::findFromSearchPaths(StringRef Path) { 170 for (StringRef Dir : Config->SearchPaths) 171 if (Optional<std::string> S = findFile(Dir, Path)) 172 return S; 173 return None; 174 } 175 176 // This is for -lfoo. We'll look for libfoo.so or libfoo.a from 177 // search paths. 178 Optional<std::string> elf::searchLibrary(StringRef Name) { 179 if (Name.startswith(":")) 180 return findFromSearchPaths(Name.substr(1)); 181 182 for (StringRef Dir : Config->SearchPaths) { 183 if (!Config->Static) 184 if (Optional<std::string> S = findFile(Dir, "lib" + Name + ".so")) 185 return S; 186 if (Optional<std::string> S = findFile(Dir, "lib" + Name + ".a")) 187 return S; 188 } 189 return None; 190 } 191