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 "lld/Config/Version.h" 19 #include "llvm/ADT/STLExtras.h" 20 #include "llvm/Support/CommandLine.h" 21 #include "llvm/Support/FileSystem.h" 22 #include "llvm/Support/Path.h" 23 #include "llvm/Support/StringSaver.h" 24 25 using namespace llvm; 26 27 using namespace lld; 28 using namespace lld::elf; 29 30 // Create OptTable 31 32 // Create prefix string literals used in Options.td 33 #define PREFIX(NAME, VALUE) const char *const NAME[] = VALUE; 34 #include "Options.inc" 35 #undef PREFIX 36 37 // Create table mapping all options defined in Options.td 38 static const opt::OptTable::Info infoTable[] = { 39 #define OPTION(X1, X2, ID, KIND, GROUP, ALIAS, X6, X7, X8, X9, X10) \ 40 { \ 41 X1, X2, X9, X10, OPT_##ID, opt::Option::KIND##Class, X8, X7, OPT_##GROUP, \ 42 OPT_##ALIAS, X6 \ 43 } \ 44 , 45 #include "Options.inc" 46 #undef OPTION 47 }; 48 49 class ELFOptTable : public opt::OptTable { 50 public: 51 ELFOptTable() : OptTable(infoTable) {} 52 }; 53 54 // Parses a given list of options. 55 opt::InputArgList elf::parseArgs(llvm::BumpPtrAllocator *A, 56 ArrayRef<const char *> Argv) { 57 // Make InputArgList from string vectors. 58 ELFOptTable Table; 59 unsigned MissingIndex; 60 unsigned MissingCount; 61 62 // Expand response files. '@<filename>' is replaced by the file's contents. 63 SmallVector<const char *, 256> Vec(Argv.data(), Argv.data() + Argv.size()); 64 StringSaver Saver(*A); 65 llvm::cl::ExpandResponseFiles(Saver, llvm::cl::TokenizeGNUCommandLine, Vec); 66 67 // Parse options and then do error checking. 68 opt::InputArgList Args = Table.ParseArgs(Vec, MissingIndex, MissingCount); 69 if (MissingCount) 70 error(Twine("missing arg value for \"") + Args.getArgString(MissingIndex) + 71 "\", expected " + Twine(MissingCount) + 72 (MissingCount == 1 ? " argument.\n" : " arguments")); 73 74 iterator_range<opt::arg_iterator> Unknowns = Args.filtered(OPT_UNKNOWN); 75 for (auto *Arg : Unknowns) 76 warning("warning: unknown argument: " + Arg->getSpelling()); 77 if (Unknowns.begin() != Unknowns.end()) 78 error("unknown argument(s) found"); 79 return Args; 80 } 81 82 void elf::printHelp(const char *Argv0) { 83 ELFOptTable Table; 84 Table.PrintHelp(outs(), Argv0, "lld", false); 85 } 86 87 void elf::printVersion() { 88 outs() << "LLD " << getLLDVersion(); 89 std::string S = getLLDRepositoryVersion(); 90 if (!S.empty()) 91 outs() << " " << S; 92 outs() << "\n"; 93 } 94 95 std::string elf::findFromSearchPaths(StringRef Path) { 96 for (StringRef Dir : Config->SearchPaths) { 97 std::string FullPath = buildSysrootedPath(Dir, Path); 98 if (sys::fs::exists(FullPath)) 99 return FullPath; 100 } 101 return ""; 102 } 103 104 // Searches a given library from input search paths, which are filled 105 // from -L command line switches. Returns a path to an existent library file. 106 std::string elf::searchLibrary(StringRef Path) { 107 std::vector<std::string> Names; 108 if (Path[0] == ':') { 109 Names.push_back(Path.drop_front()); 110 } else { 111 if (!Config->Static) 112 Names.push_back(("lib" + Path + ".so").str()); 113 Names.push_back(("lib" + Path + ".a").str()); 114 } 115 for (const std::string &Name : Names) { 116 std::string S = findFromSearchPaths(Name); 117 if (!S.empty()) 118 return S; 119 } 120 return ""; 121 } 122 123 // Makes a path by concatenating Dir and File. 124 // If Dir starts with '=' the result will be preceded by Sysroot, 125 // which can be set with --sysroot command line switch. 126 std::string elf::buildSysrootedPath(StringRef Dir, StringRef File) { 127 SmallString<128> Path; 128 if (Dir.startswith("=")) 129 sys::path::append(Path, Config->Sysroot, Dir.substr(1), File); 130 else 131 sys::path::append(Path, Dir, File); 132 return Path.str(); 133 } 134