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/Path.h"
23 #include "llvm/Support/raw_ostream.h"
31 #define OPTION(_1, _2, ID, _4, _5, _6, _7, _8, _9, _10, _11) OPT_##ID,
32 #include "Options.inc"
36 #define PREFIX(NAME, VALUE) const char *const NAME[] = VALUE;
37 #include "Options.inc"
40 static const llvm::opt::OptTable::Info infoTable[] = {
41 #define OPTION(X1, X2, ID, KIND, GROUP, ALIAS, X6, X7, X8, X9, X10) \
43 X1, X2, X9, X10, OPT_##ID, llvm::opt::Option::KIND##Class, X8, X7, \
44 OPT_##GROUP, OPT_##ALIAS, X6 \
46 #include "Options.inc"
50 class LibOptTable : public llvm::opt::OptTable {
52 LibOptTable() : OptTable(infoTable, llvm::array_lengthof(infoTable), true) {}
57 static std::string getOutputPath(llvm::opt::InputArgList *Args) {
58 if (auto *Arg = Args->getLastArg(OPT_out))
59 return Arg->getValue();
60 for (auto *Arg : Args->filtered(OPT_INPUT)) {
61 if (!StringRef(Arg->getValue()).endswith_lower(".obj"))
63 SmallString<128> Val = StringRef(Arg->getValue());
64 llvm::sys::path::replace_extension(Val, ".lib");
67 llvm_unreachable("internal error");
71 // FIXME: Should re-use StringSaver from lld.
72 class StrDupSaver : public cl::StringSaver {
73 std::vector<char *> Dups;
76 ~StrDupSaver() override {
77 for (std::vector<char *>::iterator I = Dups.begin(), E = Dups.end(); I != E;
83 const char *SaveString(const char *Str) override {
84 char *Dup = strdup(Str);
91 int llvm::libDriverMain(int Argc, const char **Argv) {
92 SmallVector<const char *, 20> NewArgv(Argv, Argv + Argc);
94 cl::ExpandResponseFiles(Saver, cl::TokenizeWindowsCommandLine, NewArgv);
96 Argc = static_cast<int>(NewArgv.size());
99 unsigned MissingIndex;
100 unsigned MissingCount;
101 std::unique_ptr<llvm::opt::InputArgList> Args(
102 Table.ParseArgs(&Argv[1], &Argv[Argc], MissingIndex, MissingCount));
104 llvm::errs() << "missing arg value for \""
105 << Args->getArgString(MissingIndex)
106 << "\", expected " << MissingCount
107 << (MissingCount == 1 ? " argument.\n" : " arguments.\n");
110 for (auto *Arg : Args->filtered(OPT_UNKNOWN))
111 llvm::errs() << "ignoring unknown argument: " << Arg->getSpelling() << "\n";
113 if (Args->filtered_begin(OPT_INPUT) == Args->filtered_end()) {
114 llvm::errs() << "no input files.\n";
118 std::vector<llvm::NewArchiveIterator> Members;
119 for (auto *Arg : Args->filtered(OPT_INPUT))
120 Members.emplace_back(Arg->getValue(),
121 llvm::sys::path::filename(Arg->getValue()));
123 std::pair<StringRef, std::error_code> Result = llvm::writeArchive(
124 getOutputPath(Args.get()), Members, /*WriteSymtab=*/true);
126 if (Result.first.empty())
127 Result.first = Argv[0];
128 llvm::errs() << Result.first << ": " << Result.second.message() << "\n";