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