blob: 9f2b12f75b8b2c25e07bc789cf65c514d809ef0b [file] [log] [blame]
Peter Collingbournebc051632015-06-09 21:50:22 +00001//===- LibDriver.cpp - lib.exe-compatible driver --------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// Defines an interface to a lib.exe-compatible driver that also understands
11// bitcode files. Used by llvm-lib and lld-link2 /lib.
12//
13//===----------------------------------------------------------------------===//
14
15#include "llvm/LibDriver/LibDriver.h"
16#include "llvm/ADT/STLExtras.h"
17#include "llvm/Object/ArchiveWriter.h"
18#include "llvm/Option/Arg.h"
19#include "llvm/Option/ArgList.h"
20#include "llvm/Option/Option.h"
21#include "llvm/Support/CommandLine.h"
Rafael Espindola454adf62015-06-13 12:49:52 +000022#include "llvm/Support/StringSaver.h"
Peter Collingbournebc051632015-06-09 21:50:22 +000023#include "llvm/Support/Path.h"
Peter Collingbourne70708272015-06-20 00:57:12 +000024#include "llvm/Support/Process.h"
Peter Collingbournebc051632015-06-09 21:50:22 +000025#include "llvm/Support/raw_ostream.h"
26
27using namespace llvm;
28
29namespace {
30
31enum {
32 OPT_INVALID = 0,
33#define OPTION(_1, _2, ID, _4, _5, _6, _7, _8, _9, _10, _11) OPT_##ID,
34#include "Options.inc"
35#undef OPTION
36};
37
38#define PREFIX(NAME, VALUE) const char *const NAME[] = VALUE;
39#include "Options.inc"
40#undef PREFIX
41
42static const llvm::opt::OptTable::Info infoTable[] = {
43#define OPTION(X1, X2, ID, KIND, GROUP, ALIAS, X6, X7, X8, X9, X10) \
44 { \
45 X1, X2, X9, X10, OPT_##ID, llvm::opt::Option::KIND##Class, X8, X7, \
46 OPT_##GROUP, OPT_##ALIAS, X6 \
47 },
48#include "Options.inc"
49#undef OPTION
50};
51
52class LibOptTable : public llvm::opt::OptTable {
53public:
54 LibOptTable() : OptTable(infoTable, llvm::array_lengthof(infoTable), true) {}
55};
56
Alexander Kornienko70bc5f12015-06-19 15:57:42 +000057} // namespace
Peter Collingbournebc051632015-06-09 21:50:22 +000058
59static std::string getOutputPath(llvm::opt::InputArgList *Args) {
60 if (auto *Arg = Args->getLastArg(OPT_out))
61 return Arg->getValue();
62 for (auto *Arg : Args->filtered(OPT_INPUT)) {
63 if (!StringRef(Arg->getValue()).endswith_lower(".obj"))
64 continue;
65 SmallString<128> Val = StringRef(Arg->getValue());
66 llvm::sys::path::replace_extension(Val, ".lib");
67 return Val.str();
68 }
69 llvm_unreachable("internal error");
70}
71
Peter Collingbourne70708272015-06-20 00:57:12 +000072static std::vector<StringRef> getSearchPaths(llvm::opt::InputArgList *Args,
73 StringSaver &Saver) {
74 std::vector<StringRef> Ret;
75 // Add current directory as first item of the search path.
76 Ret.push_back("");
77
78 // Add /libpath flags.
79 for (auto *Arg : Args->filtered(OPT_libpath))
80 Ret.push_back(Arg->getValue());
81
82 // Add $LIB.
83 Optional<std::string> EnvOpt = sys::Process::GetEnv("LIB");
84 if (!EnvOpt.hasValue())
85 return Ret;
86 StringRef Env = Saver.save(*EnvOpt);
87 while (!Env.empty()) {
88 StringRef Path;
89 std::tie(Path, Env) = Env.split(';');
90 Ret.push_back(Path);
91 }
92 return Ret;
93}
94
95static Optional<std::string> findInputFile(StringRef File,
96 ArrayRef<StringRef> Paths) {
97 for (auto Dir : Paths) {
98 SmallString<128> Path = Dir;
99 sys::path::append(Path, File);
100 if (sys::fs::exists(Path))
101 return Path.str().str();
102 }
103 return Optional<std::string>();
104}
105
David Blaikie8b31d412015-06-21 06:31:56 +0000106int llvm::libDriverMain(llvm::ArrayRef<const char*> ArgsArr) {
107 SmallVector<const char *, 20> NewArgs(ArgsArr.begin(), ArgsArr.end());
Rafael Espindola454adf62015-06-13 12:49:52 +0000108 BumpPtrAllocator Alloc;
109 BumpPtrStringSaver Saver(Alloc);
David Blaikie8b31d412015-06-21 06:31:56 +0000110 cl::ExpandResponseFiles(Saver, cl::TokenizeWindowsCommandLine, NewArgs);
111 ArgsArr = NewArgs;
Peter Collingbournebc051632015-06-09 21:50:22 +0000112
113 LibOptTable Table;
114 unsigned MissingIndex;
115 unsigned MissingCount;
David Blaikiedb3d31d2015-06-22 22:06:37 +0000116 llvm::opt::InputArgList Args =
117 Table.ParseArgs(ArgsArr.slice(1), MissingIndex, MissingCount);
Peter Collingbournebc051632015-06-09 21:50:22 +0000118 if (MissingCount) {
119 llvm::errs() << "missing arg value for \""
David Blaikiedb3d31d2015-06-22 22:06:37 +0000120 << Args.getArgString(MissingIndex) << "\", expected "
121 << MissingCount
Peter Collingbournebc051632015-06-09 21:50:22 +0000122 << (MissingCount == 1 ? " argument.\n" : " arguments.\n");
123 return 1;
124 }
David Blaikiedb3d31d2015-06-22 22:06:37 +0000125 for (auto *Arg : Args.filtered(OPT_UNKNOWN))
Peter Collingbournebc051632015-06-09 21:50:22 +0000126 llvm::errs() << "ignoring unknown argument: " << Arg->getSpelling() << "\n";
127
David Blaikiedb3d31d2015-06-22 22:06:37 +0000128 if (Args.filtered_begin(OPT_INPUT) == Args.filtered_end()) {
Peter Collingbournebc051632015-06-09 21:50:22 +0000129 llvm::errs() << "no input files.\n";
130 return 1;
131 }
132
David Blaikiedb3d31d2015-06-22 22:06:37 +0000133 std::vector<StringRef> SearchPaths = getSearchPaths(&Args, Saver);
Peter Collingbourne70708272015-06-20 00:57:12 +0000134
Peter Collingbournebc051632015-06-09 21:50:22 +0000135 std::vector<llvm::NewArchiveIterator> Members;
David Blaikiedb3d31d2015-06-22 22:06:37 +0000136 for (auto *Arg : Args.filtered(OPT_INPUT)) {
Peter Collingbourne70708272015-06-20 00:57:12 +0000137 Optional<std::string> Path = findInputFile(Arg->getValue(), SearchPaths);
138 if (!Path.hasValue()) {
139 llvm::errs() << Arg->getValue() << ": no such file or directory\n";
140 return 1;
141 }
142 Members.emplace_back(Saver.save(*Path),
Peter Collingbournebc051632015-06-09 21:50:22 +0000143 llvm::sys::path::filename(Arg->getValue()));
Peter Collingbourne70708272015-06-20 00:57:12 +0000144 }
Peter Collingbournebc051632015-06-09 21:50:22 +0000145
David Blaikiedb3d31d2015-06-22 22:06:37 +0000146 std::pair<StringRef, std::error_code> Result =
147 llvm::writeArchive(getOutputPath(&Args), Members, /*WriteSymtab=*/true);
Peter Collingbournebc051632015-06-09 21:50:22 +0000148 if (Result.second) {
149 if (Result.first.empty())
David Blaikie8b31d412015-06-21 06:31:56 +0000150 Result.first = ArgsArr[0];
Peter Collingbournebc051632015-06-09 21:50:22 +0000151 llvm::errs() << Result.first << ": " << Result.second.message() << "\n";
152 return 1;
153 }
154
155 return 0;
156}