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 ELFOptTable::ELFOptTable() : OptTable(infoTable) {} 50 51 // Parses a given list of options. 52 opt::InputArgList ELFOptTable::parse(ArrayRef<const char *> Argv) { 53 // Make InputArgList from string vectors. 54 unsigned MissingIndex; 55 unsigned MissingCount; 56 57 // Expand response files. '@<filename>' is replaced by the file's contents. 58 SmallVector<const char *, 256> Vec(Argv.data(), Argv.data() + Argv.size()); 59 StringSaver Saver(Alloc); 60 llvm::cl::ExpandResponseFiles(Saver, llvm::cl::TokenizeGNUCommandLine, Vec); 61 62 // Parse options and then do error checking. 63 opt::InputArgList Args = this->ParseArgs(Vec, MissingIndex, MissingCount); 64 if (MissingCount) 65 error(Twine("missing arg value for \"") + Args.getArgString(MissingIndex) + 66 "\", expected " + Twine(MissingCount) + 67 (MissingCount == 1 ? " argument.\n" : " arguments")); 68 69 iterator_range<opt::arg_iterator> Unknowns = Args.filtered(OPT_UNKNOWN); 70 for (auto *Arg : Unknowns) 71 warning("warning: unknown argument: " + Arg->getSpelling()); 72 if (Unknowns.begin() != Unknowns.end()) 73 error("unknown argument(s) found"); 74 return Args; 75 } 76 77 void elf::printHelp(const char *Argv0) { 78 ELFOptTable Table; 79 Table.PrintHelp(outs(), Argv0, "lld", false); 80 } 81 82 void elf::printVersion() { 83 outs() << "LLD " << getLLDVersion(); 84 std::string S = getLLDRepositoryVersion(); 85 if (!S.empty()) 86 outs() << " " << S; 87 outs() << "\n"; 88 } 89 90 std::string elf::findFromSearchPaths(StringRef Path) { 91 for (StringRef Dir : Config->SearchPaths) { 92 std::string FullPath = buildSysrootedPath(Dir, Path); 93 if (sys::fs::exists(FullPath)) 94 return FullPath; 95 } 96 return ""; 97 } 98 99 // Searches a given library from input search paths, which are filled 100 // from -L command line switches. Returns a path to an existent library file. 101 std::string elf::searchLibrary(StringRef Path) { 102 std::vector<std::string> Names; 103 if (Path[0] == ':') { 104 Names.push_back(Path.drop_front()); 105 } else { 106 if (!Config->Static) 107 Names.push_back(("lib" + Path + ".so").str()); 108 Names.push_back(("lib" + Path + ".a").str()); 109 } 110 for (const std::string &Name : Names) { 111 std::string S = findFromSearchPaths(Name); 112 if (!S.empty()) 113 return S; 114 } 115 return ""; 116 } 117 118 // Makes a path by concatenating Dir and File. 119 // If Dir starts with '=' the result will be preceded by Sysroot, 120 // which can be set with --sysroot command line switch. 121 std::string elf::buildSysrootedPath(StringRef Dir, StringRef File) { 122 SmallString<128> Path; 123 if (Dir.startswith("=")) 124 sys::path::append(Path, Config->Sysroot, Dir.substr(1), File); 125 else 126 sys::path::append(Path, Dir, File); 127 return Path.str(); 128 } 129