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