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/raw_ostream.h"
32 #define OPTION(_1, _2, ID, _4, _5, _6, _7, _8, _9, _10, _11) OPT_##ID,
33 #include "Options.inc"
37 #define PREFIX(NAME, VALUE) const char *const NAME[] = VALUE;
38 #include "Options.inc"
41 static const llvm::opt::OptTable::Info infoTable[] = {
42 #define OPTION(X1, X2, ID, KIND, GROUP, ALIAS, X6, X7, X8, X9, X10) \
44 X1, X2, X9, X10, OPT_##ID, llvm::opt::Option::KIND##Class, X8, X7, \
45 OPT_##GROUP, OPT_##ALIAS, X6 \
47 #include "Options.inc"
51 class LibOptTable : public llvm::opt::OptTable {
53 LibOptTable() : OptTable(infoTable, llvm::array_lengthof(infoTable), true) {}
58 static std::string getOutputPath(llvm::opt::InputArgList *Args) {
59 if (auto *Arg = Args->getLastArg(OPT_out))
60 return Arg->getValue();
61 for (auto *Arg : Args->filtered(OPT_INPUT)) {
62 if (!StringRef(Arg->getValue()).endswith_lower(".obj"))
64 SmallString<128> Val = StringRef(Arg->getValue());
65 llvm::sys::path::replace_extension(Val, ".lib");
68 llvm_unreachable("internal error");
71 int llvm::libDriverMain(int Argc, const char **Argv) {
72 SmallVector<const char *, 20> NewArgv(Argv, Argv + Argc);
73 BumpPtrAllocator Alloc;
74 BumpPtrStringSaver Saver(Alloc);
75 cl::ExpandResponseFiles(Saver, cl::TokenizeWindowsCommandLine, NewArgv);
77 Argc = static_cast<int>(NewArgv.size());
80 unsigned MissingIndex;
81 unsigned MissingCount;
82 std::unique_ptr<llvm::opt::InputArgList> Args(
83 Table.ParseArgs(&Argv[1], &Argv[Argc], MissingIndex, MissingCount));
85 llvm::errs() << "missing arg value for \""
86 << Args->getArgString(MissingIndex)
87 << "\", expected " << MissingCount
88 << (MissingCount == 1 ? " argument.\n" : " arguments.\n");
91 for (auto *Arg : Args->filtered(OPT_UNKNOWN))
92 llvm::errs() << "ignoring unknown argument: " << Arg->getSpelling() << "\n";
94 if (Args->filtered_begin(OPT_INPUT) == Args->filtered_end()) {
95 llvm::errs() << "no input files.\n";
99 std::vector<llvm::NewArchiveIterator> Members;
100 for (auto *Arg : Args->filtered(OPT_INPUT))
101 Members.emplace_back(Arg->getValue(),
102 llvm::sys::path::filename(Arg->getValue()));
104 std::pair<StringRef, std::error_code> Result = llvm::writeArchive(
105 getOutputPath(Args.get()), Members, /*WriteSymtab=*/true);
107 if (Result.first.empty())
108 Result.first = Argv[0];
109 llvm::errs() << Result.first << ": " << Result.second.message() << "\n";