COFF: Split Driver.cpp to Driver.cpp and DriverUtils.cpp. NFC.
The previous implementation's driver file is cluttered by lots of small functions, and it was hard to find important functions. Make a separate file to prevent that issue. llvm-svn: 238482
This commit is contained in:
parent
6ecbd064e1
commit
3500f6667a
|
@ -5,6 +5,7 @@ add_public_tablegen_target(COFFOptionsTableGen)
|
|||
add_llvm_library(lldCOFF
|
||||
Chunks.cpp
|
||||
Driver.cpp
|
||||
DriverUtils.cpp
|
||||
InputFiles.cpp
|
||||
SymbolTable.cpp
|
||||
Symbols.cpp
|
||||
|
|
|
@ -16,67 +16,21 @@
|
|||
#include "lld/Core/Error.h"
|
||||
#include "llvm/ADT/Optional.h"
|
||||
#include "llvm/ADT/STLExtras.h"
|
||||
#include "llvm/Object/COFF.h"
|
||||
#include "llvm/Option/Arg.h"
|
||||
#include "llvm/Option/ArgList.h"
|
||||
#include "llvm/Option/Option.h"
|
||||
#include "llvm/Support/CommandLine.h"
|
||||
#include "llvm/Support/Debug.h"
|
||||
#include "llvm/Support/Format.h"
|
||||
#include "llvm/Support/Path.h"
|
||||
#include "llvm/Support/Process.h"
|
||||
#include "llvm/Support/raw_ostream.h"
|
||||
#include <memory>
|
||||
|
||||
using namespace llvm;
|
||||
|
||||
// Create enum with OPT_xxx values for each option in Options.td
|
||||
enum {
|
||||
OPT_INVALID = 0,
|
||||
#define OPTION(PREFIX, NAME, ID, KIND, GROUP, ALIAS, ALIASARGS, FLAGS, PARAM, \
|
||||
HELP, META) \
|
||||
OPT_##ID,
|
||||
#include "Options.inc"
|
||||
#undef OPTION
|
||||
};
|
||||
namespace lld {
|
||||
namespace coff {
|
||||
|
||||
// Create prefix string literals used in Options.td
|
||||
#define PREFIX(NAME, VALUE) const char *const NAME[] = VALUE;
|
||||
#include "Options.inc"
|
||||
#undef PREFIX
|
||||
|
||||
// Create table mapping all options defined in Options.td
|
||||
static const llvm::opt::OptTable::Info infoTable[] = {
|
||||
#define OPTION(PREFIX, NAME, ID, KIND, GROUP, ALIAS, ALIASARGS, FLAGS, PARAM, \
|
||||
HELPTEXT, METAVAR) \
|
||||
{ \
|
||||
PREFIX, NAME, HELPTEXT, METAVAR, OPT_##ID, llvm::opt::Option::KIND##Class, \
|
||||
PARAM, FLAGS, OPT_##GROUP, OPT_##ALIAS, ALIASARGS \
|
||||
} \
|
||||
,
|
||||
#include "Options.inc"
|
||||
#undef OPTION
|
||||
};
|
||||
|
||||
namespace {
|
||||
|
||||
class COFFOptTable : public llvm::opt::OptTable {
|
||||
public:
|
||||
COFFOptTable() : OptTable(infoTable, llvm::array_lengthof(infoTable), true) {}
|
||||
};
|
||||
|
||||
class BumpPtrStringSaver : public llvm::cl::StringSaver {
|
||||
public:
|
||||
BumpPtrStringSaver(lld::coff::StringAllocator *A) : Alloc(A) {}
|
||||
|
||||
const char *SaveString(const char *S) override {
|
||||
return Alloc->save(S).data();
|
||||
}
|
||||
|
||||
private:
|
||||
lld::coff::StringAllocator *Alloc;
|
||||
};
|
||||
}
|
||||
Configuration *Config;
|
||||
|
||||
static std::string getOutputPath(llvm::opt::InputArgList *Args) {
|
||||
if (auto *Arg = Args->getLastArg(OPT_out))
|
||||
|
@ -91,86 +45,23 @@ static std::string getOutputPath(llvm::opt::InputArgList *Args) {
|
|||
llvm_unreachable("internal error");
|
||||
}
|
||||
|
||||
// Split the given string with the path separator.
|
||||
static std::vector<StringRef> splitPathList(StringRef str) {
|
||||
std::vector<StringRef> ret;
|
||||
while (!str.empty()) {
|
||||
StringRef path;
|
||||
std::tie(path, str) = str.split(';');
|
||||
ret.push_back(path);
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
namespace lld {
|
||||
namespace coff {
|
||||
|
||||
Configuration *Config;
|
||||
|
||||
ErrorOr<std::unique_ptr<llvm::opt::InputArgList>>
|
||||
parseArgs(int Argc, const char *Argv[]) {
|
||||
COFFOptTable Table;
|
||||
unsigned MissingIndex;
|
||||
unsigned MissingCount;
|
||||
std::unique_ptr<llvm::opt::InputArgList> Args(
|
||||
Table.ParseArgs(&Argv[1], &Argv[Argc], MissingIndex, MissingCount));
|
||||
if (MissingCount) {
|
||||
std::string S;
|
||||
llvm::raw_string_ostream OS(S);
|
||||
OS << llvm::format("missing arg value for \"%s\", expected %d argument%s.",
|
||||
Args->getArgString(MissingIndex), MissingCount,
|
||||
(MissingCount == 1 ? "" : "s"));
|
||||
OS.flush();
|
||||
return make_dynamic_error_code(StringRef(S));
|
||||
}
|
||||
for (auto *Arg : Args->filtered(OPT_UNKNOWN))
|
||||
llvm::errs() << "ignoring unknown argument: " << Arg->getSpelling() << "\n";
|
||||
return std::move(Args);
|
||||
}
|
||||
|
||||
std::string findLib(StringRef Filename) {
|
||||
if (llvm::sys::fs::exists(Filename))
|
||||
return Filename;
|
||||
std::string Name;
|
||||
if (Filename.endswith_lower(".lib")) {
|
||||
Name = Filename;
|
||||
} else {
|
||||
Name = (Filename + ".lib").str();
|
||||
}
|
||||
|
||||
llvm::Optional<std::string> Env = llvm::sys::Process::GetEnv("LIB");
|
||||
if (!Env.hasValue())
|
||||
return Filename;
|
||||
for (StringRef Dir : splitPathList(*Env)) {
|
||||
SmallString<128> Path = Dir;
|
||||
llvm::sys::path::append(Path, Name);
|
||||
if (llvm::sys::fs::exists(Path.str()))
|
||||
return Path.str();
|
||||
}
|
||||
return Filename;
|
||||
}
|
||||
|
||||
std::string findFile(StringRef Filename) {
|
||||
if (llvm::sys::fs::exists(Filename))
|
||||
return Filename;
|
||||
llvm::Optional<std::string> Env = llvm::sys::Process::GetEnv("LIB");
|
||||
if (!Env.hasValue())
|
||||
return Filename;
|
||||
for (StringRef Dir : splitPathList(*Env)) {
|
||||
SmallString<128> Path = Dir;
|
||||
llvm::sys::path::append(Path, Filename);
|
||||
if (llvm::sys::fs::exists(Path.str()))
|
||||
return Path.str();
|
||||
}
|
||||
return Filename;
|
||||
}
|
||||
|
||||
std::unique_ptr<InputFile> createFile(StringRef Path) {
|
||||
if (StringRef(Path).endswith_lower(".lib"))
|
||||
return llvm::make_unique<ArchiveFile>(Path);
|
||||
return llvm::make_unique<ObjectFile>(Path);
|
||||
}
|
||||
|
||||
namespace {
|
||||
class BumpPtrStringSaver : public llvm::cl::StringSaver {
|
||||
public:
|
||||
BumpPtrStringSaver(lld::coff::StringAllocator *A) : Alloc(A) {}
|
||||
const char *SaveString(const char *S) override {
|
||||
return Alloc->save(S).data();
|
||||
}
|
||||
lld::coff::StringAllocator *Alloc;
|
||||
};
|
||||
}
|
||||
|
||||
// Parses .drectve section contents and returns a list of files
|
||||
// specified by /defaultlib.
|
||||
std::error_code parseDirectives(StringRef S,
|
||||
|
|
|
@ -12,6 +12,9 @@
|
|||
|
||||
#include "Memory.h"
|
||||
#include "llvm/ADT/StringRef.h"
|
||||
#include "llvm/Object/COFF.h"
|
||||
#include "llvm/Option/Arg.h"
|
||||
#include "llvm/Option/ArgList.h"
|
||||
#include <memory>
|
||||
#include <system_error>
|
||||
#include <vector>
|
||||
|
@ -21,10 +24,27 @@ namespace coff {
|
|||
|
||||
class InputFile;
|
||||
|
||||
ErrorOr<std::unique_ptr<llvm::opt::InputArgList>>
|
||||
parseArgs(int Argc, const char *Argv[]);
|
||||
|
||||
std::error_code parseDirectives(StringRef S,
|
||||
std::vector<std::unique_ptr<InputFile>> *Res,
|
||||
StringAllocator *Alloc);
|
||||
|
||||
// Functions below this line are defined in DriverUtils.cpp.
|
||||
|
||||
// "ENV" environment variable-aware file finders.
|
||||
std::string findLib(StringRef Filename);
|
||||
std::string findFile(StringRef Filename);
|
||||
|
||||
// Create enum with OPT_xxx values for each option in Options.td
|
||||
enum {
|
||||
OPT_INVALID = 0,
|
||||
#define OPTION(_1, _2, ID, _4, _5, _6, _7, _8, _9, _10, _11) OPT_##ID,
|
||||
#include "Options.inc"
|
||||
#undef OPTION
|
||||
};
|
||||
|
||||
} // namespace coff
|
||||
} // namespace lld
|
||||
|
||||
|
|
|
@ -0,0 +1,135 @@
|
|||
//===- DriverUtils.cpp ----------------------------------------------------===//
|
||||
//
|
||||
// The LLVM Linker
|
||||
//
|
||||
// This file is distributed under the University of Illinois Open Source
|
||||
// License. See LICENSE.TXT for details.
|
||||
//
|
||||
//===----------------------------------------------------------------------===//
|
||||
//
|
||||
// This file contains utility functions for the driver. Because there
|
||||
// are so many small functions, we created this separate file to make
|
||||
// Driver.cpp less cluttered.
|
||||
//
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
||||
#include "Driver.h"
|
||||
#include "Memory.h"
|
||||
#include "lld/Core/Error.h"
|
||||
#include "llvm/ADT/Optional.h"
|
||||
#include "llvm/ADT/STLExtras.h"
|
||||
#include "llvm/ADT/StringSwitch.h"
|
||||
#include "llvm/Object/COFF.h"
|
||||
#include "llvm/Option/Arg.h"
|
||||
#include "llvm/Option/ArgList.h"
|
||||
#include "llvm/Option/Option.h"
|
||||
#include "llvm/Support/CommandLine.h"
|
||||
#include "llvm/Support/Format.h"
|
||||
#include "llvm/Support/Path.h"
|
||||
#include "llvm/Support/Process.h"
|
||||
#include "llvm/Support/raw_ostream.h"
|
||||
#include <memory>
|
||||
|
||||
using namespace llvm::COFF;
|
||||
using namespace llvm;
|
||||
using llvm::sys::Process;
|
||||
using llvm::sys::fs::file_magic;
|
||||
using llvm::sys::fs::identify_magic;
|
||||
|
||||
namespace lld {
|
||||
namespace coff {
|
||||
|
||||
// Split the given string with the path separator.
|
||||
static std::vector<StringRef> splitPathList(StringRef str) {
|
||||
std::vector<StringRef> ret;
|
||||
while (!str.empty()) {
|
||||
StringRef path;
|
||||
std::tie(path, str) = str.split(';');
|
||||
ret.push_back(path);
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
std::string findLib(StringRef Filename) {
|
||||
if (llvm::sys::fs::exists(Filename))
|
||||
return Filename;
|
||||
std::string Name;
|
||||
if (Filename.endswith_lower(".lib")) {
|
||||
Name = Filename;
|
||||
} else {
|
||||
Name = (Filename + ".lib").str();
|
||||
}
|
||||
|
||||
Optional<std::string> Env = Process::GetEnv("LIB");
|
||||
if (!Env.hasValue())
|
||||
return Filename;
|
||||
for (StringRef Dir : splitPathList(*Env)) {
|
||||
SmallString<128> Path = Dir;
|
||||
llvm::sys::path::append(Path, Name);
|
||||
if (llvm::sys::fs::exists(Path.str()))
|
||||
return Path.str();
|
||||
}
|
||||
return Filename;
|
||||
}
|
||||
|
||||
std::string findFile(StringRef Filename) {
|
||||
if (llvm::sys::fs::exists(Filename))
|
||||
return Filename;
|
||||
Optional<std::string> Env = Process::GetEnv("LIB");
|
||||
if (!Env.hasValue())
|
||||
return Filename;
|
||||
for (StringRef Dir : splitPathList(*Env)) {
|
||||
SmallString<128> Path = Dir;
|
||||
llvm::sys::path::append(Path, Filename);
|
||||
if (llvm::sys::fs::exists(Path.str()))
|
||||
return Path.str();
|
||||
}
|
||||
return Filename;
|
||||
}
|
||||
|
||||
// Create OptTable
|
||||
|
||||
// Create prefix string literals used in Options.td
|
||||
#define PREFIX(NAME, VALUE) const char *const NAME[] = VALUE;
|
||||
#include "Options.inc"
|
||||
#undef PREFIX
|
||||
|
||||
// Create table mapping all options defined in Options.td
|
||||
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 COFFOptTable : public llvm::opt::OptTable {
|
||||
public:
|
||||
COFFOptTable() : OptTable(infoTable, llvm::array_lengthof(infoTable), true) {}
|
||||
};
|
||||
|
||||
ErrorOr<std::unique_ptr<llvm::opt::InputArgList>>
|
||||
parseArgs(int Argc, const char *Argv[]) {
|
||||
COFFOptTable Table;
|
||||
unsigned MissingIndex;
|
||||
unsigned MissingCount;
|
||||
std::unique_ptr<llvm::opt::InputArgList> Args(
|
||||
Table.ParseArgs(&Argv[1], &Argv[Argc], MissingIndex, MissingCount));
|
||||
if (MissingCount) {
|
||||
std::string S;
|
||||
llvm::raw_string_ostream OS(S);
|
||||
OS << llvm::format("missing arg value for \"%s\", expected %d argument%s.",
|
||||
Args->getArgString(MissingIndex), MissingCount,
|
||||
(MissingCount == 1 ? "" : "s"));
|
||||
OS.flush();
|
||||
return make_dynamic_error_code(StringRef(S));
|
||||
}
|
||||
for (auto *Arg : Args->filtered(OPT_UNKNOWN))
|
||||
llvm::errs() << "ignoring unknown argument: " << Arg->getSpelling() << "\n";
|
||||
return std::move(Args);
|
||||
}
|
||||
|
||||
} // namespace coff
|
||||
} // namespace lld
|
Loading…
Reference in New Issue