2015-06-10 05:50:22 +08:00
|
|
|
//===- LibDriver.cpp - lib.exe-compatible driver --------------------------===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// Defines an interface to a lib.exe-compatible driver that also understands
|
2015-08-07 03:00:42 +08:00
|
|
|
// bitcode files. Used by llvm-lib and lld-link /lib.
|
2015-06-10 05:50:22 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "llvm/LibDriver/LibDriver.h"
|
|
|
|
#include "llvm/ADT/STLExtras.h"
|
|
|
|
#include "llvm/Object/ArchiveWriter.h"
|
|
|
|
#include "llvm/Option/Arg.h"
|
|
|
|
#include "llvm/Option/ArgList.h"
|
|
|
|
#include "llvm/Option/Option.h"
|
|
|
|
#include "llvm/Support/CommandLine.h"
|
2015-06-13 20:49:52 +08:00
|
|
|
#include "llvm/Support/StringSaver.h"
|
2015-06-10 05:50:22 +08:00
|
|
|
#include "llvm/Support/Path.h"
|
2015-06-20 08:57:12 +08:00
|
|
|
#include "llvm/Support/Process.h"
|
2015-06-10 05:50:22 +08:00
|
|
|
#include "llvm/Support/raw_ostream.h"
|
|
|
|
|
|
|
|
using namespace llvm;
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
enum {
|
|
|
|
OPT_INVALID = 0,
|
|
|
|
#define OPTION(_1, _2, ID, _4, _5, _6, _7, _8, _9, _10, _11) OPT_##ID,
|
|
|
|
#include "Options.inc"
|
|
|
|
#undef OPTION
|
|
|
|
};
|
|
|
|
|
|
|
|
#define PREFIX(NAME, VALUE) const char *const NAME[] = VALUE;
|
|
|
|
#include "Options.inc"
|
|
|
|
#undef PREFIX
|
|
|
|
|
|
|
|
static const llvm::opt::OptTable::Info infoTable[] = {
|
|
|
|
#define OPTION(X1, X2, ID, KIND, GROUP, ALIAS, X6, X7, X8, X9, X10) \
|
|
|
|
{ \
|
|
|
|
X1, X2, X9, X10, OPT_##ID, llvm::opt::Option::KIND##Class, X8, X7, \
|
|
|
|
OPT_##GROUP, OPT_##ALIAS, X6 \
|
|
|
|
},
|
|
|
|
#include "Options.inc"
|
|
|
|
#undef OPTION
|
|
|
|
};
|
|
|
|
|
|
|
|
class LibOptTable : public llvm::opt::OptTable {
|
|
|
|
public:
|
2015-10-22 00:30:42 +08:00
|
|
|
LibOptTable() : OptTable(infoTable, true) {}
|
2015-06-10 05:50:22 +08:00
|
|
|
};
|
|
|
|
|
2015-06-23 17:49:53 +08:00
|
|
|
}
|
2015-06-10 05:50:22 +08:00
|
|
|
|
2015-07-09 03:00:46 +08:00
|
|
|
static std::string getOutputPath(llvm::opt::InputArgList *Args,
|
2016-06-30 06:27:42 +08:00
|
|
|
const llvm::NewArchiveMember &FirstMember) {
|
2015-06-10 05:50:22 +08:00
|
|
|
if (auto *Arg = Args->getLastArg(OPT_out))
|
|
|
|
return Arg->getValue();
|
2016-06-30 06:27:42 +08:00
|
|
|
SmallString<128> Val = StringRef(FirstMember.Buf->getBufferIdentifier());
|
2015-07-09 03:00:46 +08:00
|
|
|
llvm::sys::path::replace_extension(Val, ".lib");
|
|
|
|
return Val.str();
|
2015-06-10 05:50:22 +08:00
|
|
|
}
|
|
|
|
|
2015-06-20 08:57:12 +08:00
|
|
|
static std::vector<StringRef> getSearchPaths(llvm::opt::InputArgList *Args,
|
|
|
|
StringSaver &Saver) {
|
|
|
|
std::vector<StringRef> Ret;
|
|
|
|
// Add current directory as first item of the search path.
|
|
|
|
Ret.push_back("");
|
|
|
|
|
|
|
|
// Add /libpath flags.
|
|
|
|
for (auto *Arg : Args->filtered(OPT_libpath))
|
|
|
|
Ret.push_back(Arg->getValue());
|
|
|
|
|
|
|
|
// Add $LIB.
|
|
|
|
Optional<std::string> EnvOpt = sys::Process::GetEnv("LIB");
|
|
|
|
if (!EnvOpt.hasValue())
|
|
|
|
return Ret;
|
|
|
|
StringRef Env = Saver.save(*EnvOpt);
|
|
|
|
while (!Env.empty()) {
|
|
|
|
StringRef Path;
|
|
|
|
std::tie(Path, Env) = Env.split(';');
|
|
|
|
Ret.push_back(Path);
|
|
|
|
}
|
|
|
|
return Ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static Optional<std::string> findInputFile(StringRef File,
|
|
|
|
ArrayRef<StringRef> Paths) {
|
|
|
|
for (auto Dir : Paths) {
|
|
|
|
SmallString<128> Path = Dir;
|
|
|
|
sys::path::append(Path, File);
|
|
|
|
if (sys::fs::exists(Path))
|
|
|
|
return Path.str().str();
|
|
|
|
}
|
|
|
|
return Optional<std::string>();
|
|
|
|
}
|
|
|
|
|
2015-06-21 14:31:56 +08:00
|
|
|
int llvm::libDriverMain(llvm::ArrayRef<const char*> ArgsArr) {
|
|
|
|
SmallVector<const char *, 20> NewArgs(ArgsArr.begin(), ArgsArr.end());
|
2015-06-13 20:49:52 +08:00
|
|
|
BumpPtrAllocator Alloc;
|
2015-08-13 09:07:02 +08:00
|
|
|
StringSaver Saver(Alloc);
|
2015-06-21 14:31:56 +08:00
|
|
|
cl::ExpandResponseFiles(Saver, cl::TokenizeWindowsCommandLine, NewArgs);
|
|
|
|
ArgsArr = NewArgs;
|
2015-06-10 05:50:22 +08:00
|
|
|
|
|
|
|
LibOptTable Table;
|
|
|
|
unsigned MissingIndex;
|
|
|
|
unsigned MissingCount;
|
2015-06-23 06:06:37 +08:00
|
|
|
llvm::opt::InputArgList Args =
|
|
|
|
Table.ParseArgs(ArgsArr.slice(1), MissingIndex, MissingCount);
|
2015-06-10 05:50:22 +08:00
|
|
|
if (MissingCount) {
|
|
|
|
llvm::errs() << "missing arg value for \""
|
2015-06-23 06:06:37 +08:00
|
|
|
<< Args.getArgString(MissingIndex) << "\", expected "
|
|
|
|
<< MissingCount
|
2015-06-10 05:50:22 +08:00
|
|
|
<< (MissingCount == 1 ? " argument.\n" : " arguments.\n");
|
|
|
|
return 1;
|
|
|
|
}
|
2015-06-23 06:06:37 +08:00
|
|
|
for (auto *Arg : Args.filtered(OPT_UNKNOWN))
|
2015-06-10 05:50:22 +08:00
|
|
|
llvm::errs() << "ignoring unknown argument: " << Arg->getSpelling() << "\n";
|
|
|
|
|
2015-06-23 06:06:37 +08:00
|
|
|
if (Args.filtered_begin(OPT_INPUT) == Args.filtered_end()) {
|
2016-04-14 03:36:04 +08:00
|
|
|
// No input files. To match lib.exe, silently do nothing.
|
|
|
|
return 0;
|
2015-06-10 05:50:22 +08:00
|
|
|
}
|
|
|
|
|
2015-06-23 06:06:37 +08:00
|
|
|
std::vector<StringRef> SearchPaths = getSearchPaths(&Args, Saver);
|
2015-06-20 08:57:12 +08:00
|
|
|
|
2016-06-30 06:27:42 +08:00
|
|
|
std::vector<llvm::NewArchiveMember> Members;
|
2015-06-23 06:06:37 +08:00
|
|
|
for (auto *Arg : Args.filtered(OPT_INPUT)) {
|
2015-06-20 08:57:12 +08:00
|
|
|
Optional<std::string> Path = findInputFile(Arg->getValue(), SearchPaths);
|
|
|
|
if (!Path.hasValue()) {
|
|
|
|
llvm::errs() << Arg->getValue() << ": no such file or directory\n";
|
|
|
|
return 1;
|
|
|
|
}
|
2016-06-30 06:27:42 +08:00
|
|
|
Expected<NewArchiveMember> MOrErr =
|
|
|
|
NewArchiveMember::getFile(Saver.save(*Path), /*Deterministic=*/true);
|
|
|
|
if (!MOrErr) {
|
|
|
|
handleAllErrors(MOrErr.takeError(), [&](const llvm::ErrorInfoBase &EIB) {
|
|
|
|
llvm::errs() << Arg->getValue() << ": " << EIB.message() << "\n";
|
|
|
|
});
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
Members.emplace_back(std::move(*MOrErr));
|
2015-06-20 08:57:12 +08:00
|
|
|
}
|
2015-06-10 05:50:22 +08:00
|
|
|
|
2015-07-09 04:47:32 +08:00
|
|
|
std::pair<StringRef, std::error_code> Result =
|
|
|
|
llvm::writeArchive(getOutputPath(&Args, Members[0]), Members,
|
2015-07-14 04:38:09 +08:00
|
|
|
/*WriteSymtab=*/true, object::Archive::K_GNU,
|
2015-07-18 00:01:11 +08:00
|
|
|
/*Deterministic*/ true, Args.hasArg(OPT_llvmlibthin));
|
2015-07-09 04:47:32 +08:00
|
|
|
|
2015-06-10 05:50:22 +08:00
|
|
|
if (Result.second) {
|
|
|
|
if (Result.first.empty())
|
2015-06-21 14:31:56 +08:00
|
|
|
Result.first = ArgsArr[0];
|
2015-06-10 05:50:22 +08:00
|
|
|
llvm::errs() << Result.first << ": " << Result.second.message() << "\n";
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|