blob: 5188e9d4a1c9e0bce13eed627caa4a553e7e8e88 [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
81// Searches a given library from input search paths, which are filled
82// from -L command line switches. Returns a path to an existent library file.
83std::string lld::elf2::searchLibrary(StringRef Path) {
84 std::vector<std::string> Names;
85 if (Path[0] == ':') {
86 Names.push_back(Path.drop_front());
87 } else {
88 if (!Config->Static)
89 Names.push_back(("lib" + Path + ".so").str());
90 Names.push_back(("lib" + Path + ".a").str());
91 }
92 for (StringRef Dir : Config->InputSearchPaths) {
93 for (const std::string &Name : Names) {
94 std::string FullPath = buildSysrootedPath(Dir, Name);
95 if (sys::fs::exists(FullPath))
96 return FullPath;
97 }
98 }
99 error("Unable to find library -l" + Path);
100}
101
102// Makes a path by concatenating Dir and File.
103// If Dir starts with '=' the result will be preceded by Sysroot,
104// which can be set with --sysroot command line switch.
105std::string lld::elf2::buildSysrootedPath(StringRef Dir, StringRef File) {
106 SmallString<128> Path;
107 if (Dir.startswith("="))
108 sys::path::append(Path, Config->Sysroot, Dir.substr(1), File);
109 else
110 sys::path::append(Path, Dir, File);
111 return Path.str();
112}