1 //===- LibDriver.cpp - lib.exe-compatible driver --------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // Defines an interface to a lib.exe-compatible driver that also understands
11 // bitcode files. Used by llvm-lib and lld-link2 /lib.
13 //===----------------------------------------------------------------------===//
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"
22 #include "llvm/Support/StringSaver.h"
23 #include "llvm/Support/Path.h"
24 #include "llvm/Support/Process.h"
25 #include "llvm/Support/raw_ostream.h"
33 #define OPTION(_1, _2, ID, _4, _5, _6, _7, _8, _9, _10, _11) OPT_##ID,
34 #include "Options.inc"
38 #define PREFIX(NAME, VALUE) const char *const NAME[] = VALUE;
39 #include "Options.inc"
42 static const llvm::opt::OptTable::Info infoTable[] = {
43 #define OPTION(X1, X2, ID, KIND, GROUP, ALIAS, X6, X7, X8, X9, X10) \
45 X1, X2, X9, X10, OPT_##ID, llvm::opt::Option::KIND##Class, X8, X7, \
46 OPT_##GROUP, OPT_##ALIAS, X6 \
48 #include "Options.inc"
52 class LibOptTable : public llvm::opt::OptTable {
54 LibOptTable() : OptTable(infoTable, llvm::array_lengthof(infoTable), true) {}
59 static 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"))
65 SmallString<128> Val = StringRef(Arg->getValue());
66 llvm::sys::path::replace_extension(Val, ".lib");
69 llvm_unreachable("internal error");
72 static std::vector<StringRef> getSearchPaths(llvm::opt::InputArgList *Args,
74 std::vector<StringRef> Ret;
75 // Add current directory as first item of the search path.
78 // Add /libpath flags.
79 for (auto *Arg : Args->filtered(OPT_libpath))
80 Ret.push_back(Arg->getValue());
83 Optional<std::string> EnvOpt = sys::Process::GetEnv("LIB");
84 if (!EnvOpt.hasValue())
86 StringRef Env = Saver.save(*EnvOpt);
87 while (!Env.empty()) {
89 std::tie(Path, Env) = Env.split(';');
95 static 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();
103 return Optional<std::string>();
106 int llvm::libDriverMain(llvm::ArrayRef<const char*> ArgsArr) {
107 SmallVector<const char *, 20> NewArgs(ArgsArr.begin(), ArgsArr.end());
108 BumpPtrAllocator Alloc;
109 BumpPtrStringSaver Saver(Alloc);
110 cl::ExpandResponseFiles(Saver, cl::TokenizeWindowsCommandLine, NewArgs);
114 unsigned MissingIndex;
115 unsigned MissingCount;
116 llvm::opt::InputArgList Args =
117 Table.ParseArgs(ArgsArr.slice(1), MissingIndex, MissingCount);
119 llvm::errs() << "missing arg value for \""
120 << Args.getArgString(MissingIndex) << "\", expected "
122 << (MissingCount == 1 ? " argument.\n" : " arguments.\n");
125 for (auto *Arg : Args.filtered(OPT_UNKNOWN))
126 llvm::errs() << "ignoring unknown argument: " << Arg->getSpelling() << "\n";
128 if (Args.filtered_begin(OPT_INPUT) == Args.filtered_end()) {
129 llvm::errs() << "no input files.\n";
133 std::vector<StringRef> SearchPaths = getSearchPaths(&Args, Saver);
135 std::vector<llvm::NewArchiveIterator> Members;
136 for (auto *Arg : Args.filtered(OPT_INPUT)) {
137 Optional<std::string> Path = findInputFile(Arg->getValue(), SearchPaths);
138 if (!Path.hasValue()) {
139 llvm::errs() << Arg->getValue() << ": no such file or directory\n";
142 Members.emplace_back(Saver.save(*Path),
143 llvm::sys::path::filename(Arg->getValue()));
146 std::pair<StringRef, std::error_code> Result =
147 llvm::writeArchive(getOutputPath(&Args), Members, /*WriteSymtab=*/true);
149 if (Result.first.empty())
150 Result.first = ArgsArr[0];
151 llvm::errs() << Result.first << ": " << Result.second.message() << "\n";