[NFC][LLD] Refactor some copy-paste into the Common library (#67598)

This commit is contained in:
Matheus Izvekov 2023-09-28 00:06:48 +02:00 committed by GitHub
parent 9d488569bb
commit 8ff77a8f04
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 33 additions and 51 deletions

View File

@ -13,6 +13,7 @@
#include "Symbols.h"
#include "lld/Common/Args.h"
#include "lld/Common/CommonLinkerContext.h"
#include "lld/Common/Filesystem.h"
#include "lld/Common/Strings.h"
#include "lld/Common/TargetOptionsCommandFlags.h"
#include "llvm/ADT/STLExtras.h"
@ -42,18 +43,6 @@ using namespace llvm::object;
using namespace lld;
using namespace lld::coff;
// Creates an empty file to and returns a raw_fd_ostream to write to it.
static std::unique_ptr<raw_fd_ostream> openFile(StringRef file) {
std::error_code ec;
auto ret =
std::make_unique<raw_fd_ostream>(file, ec, sys::fs::OpenFlags::OF_None);
if (ec) {
error("cannot open " + file + ": " + ec.message());
return nullptr;
}
return ret;
}
std::string BitcodeCompiler::getThinLTOOutputFile(StringRef path) {
return lto::getThinLTOOutputFile(path, ctx.config.thinLTOPrefixReplaceOld,
ctx.config.thinLTOPrefixReplaceNew);

View File

@ -11,6 +11,7 @@
//===----------------------------------------------------------------------===//
#include "lld/Common/Filesystem.h"
#include "lld/Common/ErrorHandler.h"
#include "llvm/Config/llvm-config.h"
#include "llvm/Support/FileOutputBuffer.h"
#include "llvm/Support/FileSystem.h"
@ -127,3 +128,28 @@ std::error_code lld::tryCreateFile(StringRef path) {
return std::error_code();
return errorToErrorCode(FileOutputBuffer::create(path, 1).takeError());
}
// Creates an empty file to and returns a raw_fd_ostream to write to it.
std::unique_ptr<raw_fd_ostream> lld::openFile(StringRef file) {
std::error_code ec;
auto ret =
std::make_unique<raw_fd_ostream>(file, ec, sys::fs::OpenFlags::OF_None);
if (ec) {
error("cannot open " + file + ": " + ec.message());
return nullptr;
}
return ret;
}
// The merged bitcode after LTO is large. Try opening a file stream that
// supports reading, seeking and writing. Such a file allows BitcodeWriter to
// flush buffered data to reduce memory consumption. If this fails, open a file
// stream that supports only write.
std::unique_ptr<raw_fd_ostream> lld::openLTOOutputFile(StringRef file) {
std::error_code ec;
std::unique_ptr<raw_fd_ostream> fs =
std::make_unique<raw_fd_stream>(file, ec);
if (!ec)
return fs;
return openFile(file);
}

View File

@ -13,6 +13,7 @@
#include "Symbols.h"
#include "lld/Common/Args.h"
#include "lld/Common/ErrorHandler.h"
#include "lld/Common/Filesystem.h"
#include "lld/Common/Strings.h"
#include "lld/Common/TargetOptionsCommandFlags.h"
#include "llvm/ADT/SmallString.h"
@ -40,32 +41,6 @@ using namespace llvm::ELF;
using namespace lld;
using namespace lld::elf;
// Creates an empty file to store a list of object files for final
// linking of distributed ThinLTO.
static std::unique_ptr<raw_fd_ostream> openFile(StringRef file) {
std::error_code ec;
auto ret =
std::make_unique<raw_fd_ostream>(file, ec, sys::fs::OpenFlags::OF_None);
if (ec) {
error("cannot open " + file + ": " + ec.message());
return nullptr;
}
return ret;
}
// The merged bitcode after LTO is large. Try opening a file stream that
// supports reading, seeking and writing. Such a file allows BitcodeWriter to
// flush buffered data to reduce memory consumption. If this fails, open a file
// stream that supports only write.
static std::unique_ptr<raw_fd_ostream> openLTOOutputFile(StringRef file) {
std::error_code ec;
std::unique_ptr<raw_fd_ostream> fs =
std::make_unique<raw_fd_stream>(file, ec);
if (!ec)
return fs;
return openFile(file);
}
static std::string getThinLTOOutputFile(StringRef modulePath) {
return lto::getThinLTOOutputFile(modulePath, config->thinLTOPrefixReplaceOld,
config->thinLTOPrefixReplaceNew);

View File

@ -15,6 +15,7 @@
#include "lld/Common/Args.h"
#include "lld/Common/CommonLinkerContext.h"
#include "lld/Common/Filesystem.h"
#include "lld/Common/Strings.h"
#include "lld/Common/TargetOptionsCommandFlags.h"
#include "llvm/Bitcode/BitcodeWriter.h"
@ -32,19 +33,6 @@ using namespace llvm;
using namespace llvm::MachO;
using namespace llvm::sys;
// Creates an empty file to store a list of object files for final
// linking of distributed ThinLTO.
static std::unique_ptr<raw_fd_ostream> openFile(StringRef file) {
std::error_code ec;
auto ret =
std::make_unique<raw_fd_ostream>(file, ec, sys::fs::OpenFlags::OF_None);
if (ec) {
error("cannot open " + file + ": " + ec.message());
return nullptr;
}
return ret;
}
static std::string getThinLTOOutputFile(StringRef modulePath) {
return lto::getThinLTOOutputFile(modulePath, config->thinLTOPrefixReplaceOld,
config->thinLTOPrefixReplaceNew);

View File

@ -10,11 +10,15 @@
#define LLD_FILESYSTEM_H
#include "lld/Common/LLVM.h"
#include "llvm/Support/raw_ostream.h"
#include <memory>
#include <system_error>
namespace lld {
void unlinkAsync(StringRef path);
std::error_code tryCreateFile(StringRef path);
std::unique_ptr<llvm::raw_fd_ostream> openFile(StringRef file);
std::unique_ptr<llvm::raw_fd_ostream> openLTOOutputFile(StringRef file);
} // namespace lld
#endif