blob: c15cb51fc5b79e5f4b7a525d7926a68a90c92603 [file] [log] [blame]
Michael J. Spencer84487f12015-07-24 21:03:07 +00001//===- 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 Espindola192e1fa2015-08-06 15:08:23 +000017#include "Error.h"
Michael J. Spencer84487f12015-07-24 21:03:07 +000018#include "llvm/ADT/STLExtras.h"
Rui Ueyamaeeb22f82015-09-25 15:37:33 +000019#include "llvm/Support/CommandLine.h"
Rui Ueyama4b02ca92015-10-11 03:28:39 +000020#include "llvm/Support/FileSystem.h"
21#include "llvm/Support/Path.h"
Michael J. Spencer84487f12015-07-24 21:03:07 +000022
23using namespace llvm;
24
25using namespace lld;
26using 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
36static 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
47class ELFOptTable : public opt::OptTable {
48public:
49 ELFOptTable() : OptTable(infoTable, array_lengthof(infoTable)) {}
50};
51
Rui Ueyamab9732562015-10-11 01:53:07 +000052ArgParser::ArgParser(BumpPtrAllocator *A) : Saver(*A) {}
53
Michael J. Spencer84487f12015-07-24 21:03:07 +000054// Parses a given list of options.
55opt::InputArgList ArgParser::parse(ArrayRef<const char *> Argv) {
56 // Make InputArgList from string vectors.
57 ELFOptTable Table;
58 unsigned MissingIndex;
59 unsigned MissingCount;
60
Rui Ueyamaeeb22f82015-09-25 15:37:33 +000061 // Expand response files. '@<filename>' is replaced by the file's contents.
62 SmallVector<const char *, 256> Vec(Argv.data(), Argv.data() + Argv.size());
Rui Ueyamaeeb22f82015-09-25 15:37:33 +000063 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. Spencer84487f12015-07-24 21:03:07 +000067 if (MissingCount)
68 error(Twine("missing arg value for \"") + Args.getArgString(MissingIndex) +
69 "\", expected " + Twine(MissingCount) +
70 (MissingCount == 1 ? " argument.\n" : " arguments"));
Rui Ueyamad5b5ab72015-09-24 18:55:33 +000071
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. Spencer84487f12015-07-24 21:03:07 +000078 return Args;
79}
Rui Ueyama4b02ca92015-10-11 03:28:39 +000080
Rui Ueyama52a15092015-10-11 03:28:42 +000081std::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 Ueyama4b02ca92015-10-11 03:28:39 +000090// 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.
92std::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 Ueyama52a15092015-10-11 03:28:42 +0000101 for (const std::string &Name : Names) {
102 std::string S = findFromSearchPaths(Name);
103 if (!S.empty())
104 return S;
Rui Ueyama4b02ca92015-10-11 03:28:39 +0000105 }
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.
112std::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}