Skip to content

Commit ee34ca3

Browse files
committed
[llvm-cvtres] Reduce the set of dependencies of llvm-cvtres. NFC.
Don't use createBinary() but call the WindowsResource class directly. The createBinary() function references all supported object file types and ends up pulling way more from all the underlying libraries than what is necessary. This shrinks a stripped llvm-cvtres from 4.6 MB to 463 KB. Differential Revision: https://reviews.llvm.org/D100833
1 parent 11072a0 commit ee34ca3

File tree

2 files changed

+37
-13
lines changed

2 files changed

+37
-13
lines changed

llvm/tools/llvm-cvtres/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
set(LLVM_LINK_COMPONENTS
2+
BinaryFormat
23
Object
34
Option
45
Support

llvm/tools/llvm-cvtres/llvm-cvtres.cpp

Lines changed: 36 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
//
1212
//===----------------------------------------------------------------------===//
1313

14+
#include "llvm/BinaryFormat/Magic.h"
1415
#include "llvm/Object/Binary.h"
1516
#include "llvm/Object/WindowsMachineFlag.h"
1617
#include "llvm/Object/WindowsResource.h"
@@ -75,6 +76,14 @@ static void reportError(StringRef Input, std::error_code EC) {
7576
reportError(Twine(Input) + ": " + EC.message() + ".\n");
7677
}
7778

79+
static void error(StringRef Input, Error EC) {
80+
if (!EC)
81+
return;
82+
handleAllErrors(std::move(EC), [&](const ErrorInfoBase &EI) {
83+
reportError(Twine(Input) + ": " + EI.message() + ".\n");
84+
});
85+
}
86+
7887
static void error(Error EC) {
7988
if (!EC)
8089
return;
@@ -95,6 +104,16 @@ template <typename T> T error(Expected<T> EC) {
95104
return std::move(EC.get());
96105
}
97106

107+
template <typename T> T error(StringRef Input, Expected<T> EC) {
108+
if (!EC)
109+
error(Input, EC.takeError());
110+
return std::move(EC.get());
111+
}
112+
113+
template <typename T> T error(StringRef Input, ErrorOr<T> &&EC) {
114+
return error(Input, errorOrToExpected(std::move(EC)));
115+
}
116+
98117
int main(int Argc, const char **Argv) {
99118
InitLLVM X(Argc, Argv);
100119

@@ -155,15 +174,17 @@ int main(int Argc, const char **Argv) {
155174
WindowsResourceParser Parser;
156175

157176
for (const auto &File : InputFiles) {
158-
Expected<OwningBinary<Binary>> BinaryOrErr = createBinary(File);
159-
if (!BinaryOrErr)
160-
reportError(File, errorToErrorCode(BinaryOrErr.takeError()));
161-
162-
Binary &Binary = *BinaryOrErr.get().getBinary();
163-
164-
WindowsResource *RF = dyn_cast<WindowsResource>(&Binary);
165-
if (!RF)
177+
std::unique_ptr<MemoryBuffer> Buffer = error(
178+
File, MemoryBuffer::getFileOrSTDIN(File, /*IsText=*/false,
179+
/*RequiresNullTerminator=*/false));
180+
file_magic Type = identify_magic(Buffer->getMemBufferRef().getBuffer());
181+
if (Type != file_magic::windows_resource)
166182
reportError(File + ": unrecognized file format.\n");
183+
std::unique_ptr<WindowsResource> Binary = error(
184+
File,
185+
WindowsResource::createWindowsResource(Buffer->getMemBufferRef()));
186+
187+
WindowsResource *RF = Binary.get();
167188

168189
if (Verbose) {
169190
int EntryNumber = 0;
@@ -199,12 +220,14 @@ int main(int Argc, const char **Argv) {
199220
error(FileBuffer->commit());
200221

201222
if (Verbose) {
202-
Expected<OwningBinary<Binary>> BinaryOrErr = createBinary(OutputFile);
203-
if (!BinaryOrErr)
204-
reportError(OutputFile, errorToErrorCode(BinaryOrErr.takeError()));
205-
Binary &Binary = *BinaryOrErr.get().getBinary();
223+
std::unique_ptr<MemoryBuffer> Buffer =
224+
error(OutputFile,
225+
MemoryBuffer::getFileOrSTDIN(OutputFile, /*IsText=*/false,
226+
/*RequiresNullTerminator=*/false));
227+
206228
ScopedPrinter W(errs());
207-
W.printBinaryBlock("Output File Raw Data", Binary.getData());
229+
W.printBinaryBlock("Output File Raw Data",
230+
Buffer->getMemBufferRef().getBuffer());
208231
}
209232

210233
return 0;

0 commit comments

Comments
 (0)