1 //===-- llvm-rc.cpp - Compile .rc scripts into .res -------------*- C++ -*-===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 // Compile .rc scripts into .res files. This is intended to be a
10 // platform-independent port of Microsoft's rc.exe tool.
12 //===----------------------------------------------------------------------===//
14 #include "ResourceFileWriter.h"
15 #include "ResourceScriptCppFilter.h"
16 #include "ResourceScriptParser.h"
17 #include "ResourceScriptStmt.h"
18 #include "ResourceScriptToken.h"
20 #include "llvm/Config/llvm-config.h"
21 #include "llvm/Object/WindowsResource.h"
22 #include "llvm/Option/Arg.h"
23 #include "llvm/Option/ArgList.h"
24 #include "llvm/Option/OptTable.h"
25 #include "llvm/Support/CommandLine.h"
26 #include "llvm/Support/Error.h"
27 #include "llvm/Support/FileSystem.h"
28 #include "llvm/Support/FileUtilities.h"
29 #include "llvm/Support/InitLLVM.h"
30 #include "llvm/Support/LLVMDriver.h"
31 #include "llvm/Support/MemoryBuffer.h"
32 #include "llvm/Support/Path.h"
33 #include "llvm/Support/PrettyStackTrace.h"
34 #include "llvm/Support/Process.h"
35 #include "llvm/Support/Program.h"
36 #include "llvm/Support/Signals.h"
37 #include "llvm/Support/StringSaver.h"
38 #include "llvm/Support/raw_ostream.h"
39 #include "llvm/TargetParser/Host.h"
40 #include "llvm/TargetParser/Triple.h"
43 #include <system_error>
46 using namespace llvm::rc
;
47 using namespace llvm::opt
;
51 // Input options tables.
54 OPT_INVALID
= 0, // This is not a correct option ID.
55 #define OPTION(...) LLVM_MAKE_OPT_ID(__VA_ARGS__),
61 #define PREFIX(NAME, VALUE) \
62 static constexpr StringLiteral NAME##_init[] = VALUE; \
63 static constexpr ArrayRef<StringLiteral> NAME(NAME##_init, \
64 std::size(NAME##_init) - 1);
68 static constexpr opt::OptTable::Info InfoTable
[] = {
69 #define OPTION(...) LLVM_CONSTRUCT_OPT_INFO(__VA_ARGS__),
75 class RcOptTable
: public opt::GenericOptTable
{
77 RcOptTable() : GenericOptTable(rc_opt::InfoTable
, /* IgnoreCase = */ true) {}
81 WINDRES_INVALID
= 0, // This is not a correct option ID.
82 #define OPTION(...) LLVM_MAKE_OPT_ID_WITH_ID_PREFIX(WINDRES_, __VA_ARGS__),
83 #include "WindresOpts.inc"
87 namespace windres_opt
{
88 #define PREFIX(NAME, VALUE) \
89 static constexpr StringLiteral NAME##_init[] = VALUE; \
90 static constexpr ArrayRef<StringLiteral> NAME(NAME##_init, \
91 std::size(NAME##_init) - 1);
92 #include "WindresOpts.inc"
95 static constexpr opt::OptTable::Info InfoTable
[] = {
97 LLVM_CONSTRUCT_OPT_INFO_WITH_ID_PREFIX(WINDRES_, __VA_ARGS__),
98 #include "WindresOpts.inc"
101 } // namespace windres_opt
103 class WindresOptTable
: public opt::GenericOptTable
{
106 : GenericOptTable(windres_opt::InfoTable
, /* IgnoreCase = */ false) {}
109 static ExitOnError ExitOnErr
;
110 static FileRemover TempPreprocFile
;
111 static FileRemover TempResFile
;
113 [[noreturn
]] static void fatalError(const Twine
&Message
) {
114 errs() << Message
<< "\n";
118 std::string
createTempFile(const Twine
&Prefix
, StringRef Suffix
) {
120 SmallString
<128> FileName
;
121 if ((EC
= sys::fs::createTemporaryFile(Prefix
, Suffix
, FileName
)))
122 fatalError("Unable to create temp file: " + EC
.message());
123 return static_cast<std::string
>(FileName
);
126 ErrorOr
<std::string
> findClang(const char *Argv0
, StringRef Triple
) {
127 // This just needs to be some symbol in the binary.
128 void *P
= (void*) (intptr_t) findClang
;
129 std::string MainExecPath
= llvm::sys::fs::getMainExecutable(Argv0
, P
);
130 if (MainExecPath
.empty())
131 MainExecPath
= Argv0
;
133 ErrorOr
<std::string
> Path
= std::error_code();
134 std::string TargetClang
= (Triple
+ "-clang").str();
135 std::string VersionedClang
= ("clang-" + Twine(LLVM_VERSION_MAJOR
)).str();
136 for (const auto *Name
:
137 {TargetClang
.c_str(), VersionedClang
.c_str(), "clang", "clang-cl"}) {
138 for (const StringRef Parent
:
139 {llvm::sys::path::parent_path(MainExecPath
),
140 llvm::sys::path::parent_path(Argv0
)}) {
141 // Look for various versions of "clang" first in the MainExecPath parent
142 // directory and then in the argv[0] parent directory.
143 // On Windows (but not Unix) argv[0] is overwritten with the eqiuvalent
144 // of MainExecPath by InitLLVM.
145 Path
= sys::findProgramByName(Name
, Parent
);
151 // If no parent directory known, or not found there, look everywhere in PATH
152 for (const auto *Name
: {"clang", "clang-cl"}) {
153 Path
= sys::findProgramByName(Name
);
160 bool isUsableArch(Triple::ArchType Arch
) {
166 case Triple::aarch64
:
167 // These work properly with the clang driver, setting the expected
168 // defines such as _WIN32 etc.
171 // Other archs aren't set up for use with windows as target OS, (clang
172 // doesn't define e.g. _WIN32 etc), so with them we need to set a
173 // different default arch.
178 Triple::ArchType
getDefaultFallbackArch() {
179 return Triple::x86_64
;
182 std::string
getClangClTriple() {
183 Triple
T(sys::getDefaultTargetTriple());
184 if (!isUsableArch(T
.getArch()))
185 T
.setArch(getDefaultFallbackArch());
186 T
.setOS(Triple::Win32
);
187 T
.setVendor(Triple::PC
);
188 T
.setEnvironment(Triple::MSVC
);
189 T
.setObjectFormat(Triple::COFF
);
193 std::string
getMingwTriple() {
194 Triple
T(sys::getDefaultTargetTriple());
195 if (!isUsableArch(T
.getArch()))
196 T
.setArch(getDefaultFallbackArch());
197 if (T
.isWindowsGNUEnvironment())
199 // Write out the literal form of the vendor/env here, instead of
200 // constructing them with enum values (which end up with them in
201 // normalized form). The literal form of the triple can matter for
202 // finding include files.
203 return (Twine(T
.getArchName()) + "-w64-mingw32").str();
206 enum Format
{ Rc
, Res
, Coff
, Unknown
};
209 bool Preprocess
= true;
210 bool PrintCmdAndExit
= false;
212 std::vector
<std::string
> PreprocessCmd
;
213 std::vector
<std::string
> PreprocessArgs
;
215 std::string InputFile
;
216 Format InputFormat
= Rc
;
217 std::string OutputFile
;
218 Format OutputFormat
= Res
;
220 bool IsWindres
= false;
221 bool BeVerbose
= false;
223 bool AppendNull
= false;
224 bool IsDryRun
= false;
225 // Set the default language; choose en-US arbitrarily.
226 unsigned LangId
= (/*PrimaryLangId*/ 0x09) | (/*SubLangId*/ 0x01 << 10);
229 void preprocess(StringRef Src
, StringRef Dst
, const RcOptions
&Opts
,
232 if (Opts
.PrintCmdAndExit
|| !Opts
.PreprocessCmd
.empty()) {
235 ErrorOr
<std::string
> ClangOrErr
= findClang(Argv0
, Opts
.Triple
);
239 errs() << "llvm-rc: Unable to find clang for preprocessing."
241 StringRef OptionName
=
242 Opts
.IsWindres
? "--no-preprocess" : "-no-preprocess";
243 errs() << "Pass " << OptionName
<< " to disable preprocessing.\n";
244 fatalError("llvm-rc: Unable to preprocess.");
248 SmallVector
<StringRef
, 8> Args
= {
249 Clang
, "--driver-mode=gcc", "-target", Opts
.Triple
, "-E",
250 "-xc", "-DRC_INVOKED"};
251 if (!Opts
.PreprocessCmd
.empty()) {
253 for (const auto &S
: Opts
.PreprocessCmd
)
257 Args
.push_back("-o");
259 for (const auto &S
: Opts
.PreprocessArgs
)
261 if (Opts
.PrintCmdAndExit
|| Opts
.BeVerbose
) {
262 for (const auto &A
: Args
) {
264 sys::printArg(outs(), A
, Opts
.PrintCmdAndExit
);
267 if (Opts
.PrintCmdAndExit
)
270 // The llvm Support classes don't handle reading from stdout of a child
271 // process; otherwise we could avoid using a temp file.
272 int Res
= sys::ExecuteAndWait(Args
[0], Args
);
274 fatalError("llvm-rc: Preprocessing failed.");
278 static std::pair
<bool, std::string
> isWindres(llvm::StringRef Argv0
) {
279 StringRef ProgName
= llvm::sys::path::stem(Argv0
);
280 // x86_64-w64-mingw32-windres -> x86_64-w64-mingw32, windres
281 // llvm-rc -> "", llvm-rc
282 // aarch64-w64-mingw32-llvm-windres-10.exe -> aarch64-w64-mingw32, llvm-windres
283 ProgName
= ProgName
.rtrim("0123456789.-");
284 if (!ProgName
.consume_back_insensitive("windres"))
285 return std::make_pair
<bool, std::string
>(false, "");
286 ProgName
.consume_back_insensitive("llvm-");
287 ProgName
.consume_back_insensitive("-");
288 return std::make_pair
<bool, std::string
>(true, ProgName
.str());
291 Format
parseFormat(StringRef S
) {
292 Format F
= StringSwitch
<Format
>(S
.lower())
298 fatalError("Unable to parse '" + Twine(S
) + "' as a format");
302 void deduceFormat(Format
&Dest
, StringRef File
) {
303 Format F
= StringSwitch
<Format
>(sys::path::extension(File
.lower()))
313 std::string
unescape(StringRef S
) {
315 Out
.reserve(S
.size());
316 for (int I
= 0, E
= S
.size(); I
< E
; I
++) {
319 Out
.push_back(S
[++I
]);
321 fatalError("Unterminated escape");
323 } else if (S
[I
] == '"') {
324 // This eats an individual unescaped quote, like a shell would do.
332 std::vector
<std::string
> unescapeSplit(StringRef S
) {
333 std::vector
<std::string
> OutArgs
;
335 bool InQuote
= false;
336 for (int I
= 0, E
= S
.size(); I
< E
; I
++) {
339 Out
.push_back(S
[++I
]);
341 fatalError("Unterminated escape");
348 if (S
[I
] == ' ' && !InQuote
) {
349 OutArgs
.push_back(Out
);
356 fatalError("Unterminated quote");
358 OutArgs
.push_back(Out
);
362 RcOptions
parseWindresOptions(ArrayRef
<const char *> ArgsArr
,
363 ArrayRef
<const char *> InputArgsArray
,
364 std::string Prefix
) {
368 opt::InputArgList InputArgs
= T
.ParseArgs(ArgsArr
, MAI
, MAC
);
370 Opts
.IsWindres
= true;
372 // The tool prints nothing when invoked with no command-line arguments.
373 if (InputArgs
.hasArg(WINDRES_help
)) {
374 T
.printHelp(outs(), "windres [options] file...",
375 "LLVM windres (GNU windres compatible)", false, true);
379 if (InputArgs
.hasArg(WINDRES_version
)) {
380 outs() << "llvm-windres, compatible with GNU windres\n";
381 cl::PrintVersionMessage();
385 std::vector
<std::string
> FileArgs
= InputArgs
.getAllArgValues(WINDRES_INPUT
);
386 FileArgs
.insert(FileArgs
.end(), InputArgsArray
.begin(), InputArgsArray
.end());
388 if (InputArgs
.hasArg(WINDRES_input
)) {
389 Opts
.InputFile
= InputArgs
.getLastArgValue(WINDRES_input
).str();
390 } else if (!FileArgs
.empty()) {
391 Opts
.InputFile
= FileArgs
.front();
392 FileArgs
.erase(FileArgs
.begin());
394 // TODO: GNU windres takes input on stdin in this case.
395 fatalError("Missing input file");
398 if (InputArgs
.hasArg(WINDRES_output
)) {
399 Opts
.OutputFile
= InputArgs
.getLastArgValue(WINDRES_output
).str();
400 } else if (!FileArgs
.empty()) {
401 Opts
.OutputFile
= FileArgs
.front();
402 FileArgs
.erase(FileArgs
.begin());
404 // TODO: GNU windres writes output in rc form to stdout in this case.
405 fatalError("Missing output file");
408 if (InputArgs
.hasArg(WINDRES_input_format
)) {
410 parseFormat(InputArgs
.getLastArgValue(WINDRES_input_format
));
412 deduceFormat(Opts
.InputFormat
, Opts
.InputFile
);
414 if (Opts
.InputFormat
== Coff
)
415 fatalError("Unsupported input format");
417 if (InputArgs
.hasArg(WINDRES_output_format
)) {
419 parseFormat(InputArgs
.getLastArgValue(WINDRES_output_format
));
421 // The default in windres differs from the default in RcOptions
422 Opts
.OutputFormat
= Coff
;
423 deduceFormat(Opts
.OutputFormat
, Opts
.OutputFile
);
425 if (Opts
.OutputFormat
== Rc
)
426 fatalError("Unsupported output format");
427 if (Opts
.InputFormat
== Opts
.OutputFormat
) {
428 outs() << "Nothing to do.\n";
432 Opts
.PrintCmdAndExit
= InputArgs
.hasArg(WINDRES__HASH_HASH_HASH
);
433 Opts
.Preprocess
= !InputArgs
.hasArg(WINDRES_no_preprocess
);
435 if (InputArgs
.hasArg(WINDRES_target
)) {
436 StringRef Value
= InputArgs
.getLastArgValue(WINDRES_target
);
437 if (Value
== "pe-i386")
438 Opts
.Triple
= "i686-w64-mingw32";
439 else if (Value
== "pe-x86-64")
440 Opts
.Triple
= "x86_64-w64-mingw32";
442 // Implicit extension; if the --target value isn't one of the known
443 // BFD targets, allow setting the full triple string via this instead.
444 Opts
.Triple
= Value
.str();
445 } else if (TT
.getArch() != Triple::UnknownArch
)
446 Opts
.Triple
= Prefix
;
448 Opts
.Triple
= getMingwTriple();
450 for (const auto *Arg
:
451 InputArgs
.filtered(WINDRES_include_dir
, WINDRES_define
, WINDRES_undef
,
452 WINDRES_preprocessor_arg
)) {
453 // GNU windres passes the arguments almost as-is on to popen() (it only
454 // backslash escapes spaces in the arguments), where a shell would
455 // unescape backslash escapes for quotes and similar. This means that
456 // when calling GNU windres, callers need to double escape chars like
457 // quotes, e.g. as -DSTRING=\\\"1.2.3\\\".
459 // Exactly how the arguments are interpreted depends on the platform
460 // though - but the cases where this matters (where callers would have
461 // done this double escaping) probably is confined to cases like these
462 // quoted string defines, and those happen to work the same across unix
465 // If GNU windres is executed with --use-temp-file, it doesn't use
466 // popen() to invoke the preprocessor, but uses another function which
467 // actually preserves tricky characters better. To mimic this behaviour,
468 // don't unescape arguments here.
469 std::string Value
= Arg
->getValue();
470 if (!InputArgs
.hasArg(WINDRES_use_temp_file
))
471 Value
= unescape(Value
);
472 switch (Arg
->getOption().getID()) {
473 case WINDRES_include_dir
:
474 // Technically, these are handled the same way as e.g. defines, but
475 // the way we consistently unescape the unix way breaks windows paths
476 // with single backslashes. Alternatively, our unescape function would
477 // need to mimic the platform specific command line parsing/unescaping
479 Opts
.Params
.Include
.push_back(Arg
->getValue());
480 Opts
.PreprocessArgs
.push_back("-I");
481 Opts
.PreprocessArgs
.push_back(Arg
->getValue());
484 Opts
.PreprocessArgs
.push_back("-D");
485 Opts
.PreprocessArgs
.push_back(Value
);
488 Opts
.PreprocessArgs
.push_back("-U");
489 Opts
.PreprocessArgs
.push_back(Value
);
491 case WINDRES_preprocessor_arg
:
492 Opts
.PreprocessArgs
.push_back(Value
);
496 // TODO: If --use-temp-file is set, we shouldn't be unescaping
497 // the --preprocessor argument either, only splitting it.
498 if (InputArgs
.hasArg(WINDRES_preprocessor
))
500 unescapeSplit(InputArgs
.getLastArgValue(WINDRES_preprocessor
));
502 Opts
.Params
.CodePage
= CpWin1252
; // Different default
503 if (InputArgs
.hasArg(WINDRES_codepage
)) {
504 if (InputArgs
.getLastArgValue(WINDRES_codepage
)
505 .getAsInteger(0, Opts
.Params
.CodePage
))
506 fatalError("Invalid code page: " +
507 InputArgs
.getLastArgValue(WINDRES_codepage
));
509 if (InputArgs
.hasArg(WINDRES_language
)) {
510 StringRef Val
= InputArgs
.getLastArgValue(WINDRES_language
);
511 Val
.consume_front_insensitive("0x");
512 if (Val
.getAsInteger(16, Opts
.LangId
))
513 fatalError("Invalid language id: " +
514 InputArgs
.getLastArgValue(WINDRES_language
));
517 Opts
.BeVerbose
= InputArgs
.hasArg(WINDRES_verbose
);
522 RcOptions
parseRcOptions(ArrayRef
<const char *> ArgsArr
,
523 ArrayRef
<const char *> InputArgsArray
) {
527 opt::InputArgList InputArgs
= T
.ParseArgs(ArgsArr
, MAI
, MAC
);
529 // The tool prints nothing when invoked with no command-line arguments.
530 if (InputArgs
.hasArg(OPT_help
)) {
531 T
.printHelp(outs(), "llvm-rc [options] file...", "LLVM Resource Converter",
536 std::vector
<std::string
> InArgsInfo
= InputArgs
.getAllArgValues(OPT_INPUT
);
537 InArgsInfo
.insert(InArgsInfo
.end(), InputArgsArray
.begin(),
538 InputArgsArray
.end());
539 if (InArgsInfo
.size() != 1) {
540 fatalError("Exactly one input file should be provided.");
543 Opts
.PrintCmdAndExit
= InputArgs
.hasArg(OPT__HASH_HASH_HASH
);
544 Opts
.Triple
= getClangClTriple();
545 for (const auto *Arg
:
546 InputArgs
.filtered(OPT_includepath
, OPT_define
, OPT_undef
)) {
547 switch (Arg
->getOption().getID()) {
548 case OPT_includepath
:
549 Opts
.PreprocessArgs
.push_back("-I");
552 Opts
.PreprocessArgs
.push_back("-D");
555 Opts
.PreprocessArgs
.push_back("-U");
558 Opts
.PreprocessArgs
.push_back(Arg
->getValue());
561 Opts
.InputFile
= InArgsInfo
[0];
562 Opts
.BeVerbose
= InputArgs
.hasArg(OPT_verbose
);
563 Opts
.Preprocess
= !InputArgs
.hasArg(OPT_no_preprocess
);
564 Opts
.Params
.Include
= InputArgs
.getAllArgValues(OPT_includepath
);
565 Opts
.Params
.NoInclude
= InputArgs
.hasArg(OPT_noinclude
);
566 if (Opts
.Params
.NoInclude
) {
567 // Clear the INLCUDE variable for the external preprocessor
569 ::_putenv("INCLUDE=");
571 ::unsetenv("INCLUDE");
574 if (InputArgs
.hasArg(OPT_codepage
)) {
575 if (InputArgs
.getLastArgValue(OPT_codepage
)
576 .getAsInteger(10, Opts
.Params
.CodePage
))
577 fatalError("Invalid code page: " +
578 InputArgs
.getLastArgValue(OPT_codepage
));
580 Opts
.IsDryRun
= InputArgs
.hasArg(OPT_dry_run
);
581 auto OutArgsInfo
= InputArgs
.getAllArgValues(OPT_fileout
);
582 if (OutArgsInfo
.empty()) {
583 SmallString
<128> OutputFile(Opts
.InputFile
);
584 llvm::sys::fs::make_absolute(OutputFile
);
585 llvm::sys::path::replace_extension(OutputFile
, "res");
586 OutArgsInfo
.push_back(std::string(OutputFile
.str()));
588 if (!Opts
.IsDryRun
) {
589 if (OutArgsInfo
.size() != 1)
591 "No more than one output file should be provided (using /FO flag).");
592 Opts
.OutputFile
= OutArgsInfo
[0];
594 Opts
.AppendNull
= InputArgs
.hasArg(OPT_add_null
);
595 if (InputArgs
.hasArg(OPT_lang_id
)) {
596 StringRef Val
= InputArgs
.getLastArgValue(OPT_lang_id
);
597 Val
.consume_front_insensitive("0x");
598 if (Val
.getAsInteger(16, Opts
.LangId
))
599 fatalError("Invalid language id: " +
600 InputArgs
.getLastArgValue(OPT_lang_id
));
605 RcOptions
getOptions(const char *Argv0
, ArrayRef
<const char *> ArgsArr
,
606 ArrayRef
<const char *> InputArgs
) {
609 std::tie(IsWindres
, Prefix
) = isWindres(Argv0
);
611 return parseWindresOptions(ArgsArr
, InputArgs
, Prefix
);
613 return parseRcOptions(ArgsArr
, InputArgs
);
616 void doRc(std::string Src
, std::string Dest
, RcOptions
&Opts
,
618 std::string PreprocessedFile
= Src
;
619 if (Opts
.Preprocess
) {
620 std::string OutFile
= createTempFile("preproc", "rc");
621 TempPreprocFile
.setFile(OutFile
);
622 preprocess(Src
, OutFile
, Opts
, Argv0
);
623 PreprocessedFile
= OutFile
;
626 // Read and tokenize the input file.
627 ErrorOr
<std::unique_ptr
<MemoryBuffer
>> File
=
628 MemoryBuffer::getFile(PreprocessedFile
);
630 fatalError("Error opening file '" + Twine(PreprocessedFile
) +
631 "': " + File
.getError().message());
634 std::unique_ptr
<MemoryBuffer
> FileContents
= std::move(*File
);
635 StringRef Contents
= FileContents
->getBuffer();
637 std::string FilteredContents
= filterCppOutput(Contents
);
638 std::vector
<RCToken
> Tokens
= ExitOnErr(tokenizeRC(FilteredContents
));
640 if (Opts
.BeVerbose
) {
641 const Twine TokenNames
[] = {
642 #define TOKEN(Name) #Name,
643 #define SHORT_TOKEN(Name, Ch) #Name,
644 #include "ResourceScriptTokenList.def"
647 for (const RCToken
&Token
: Tokens
) {
648 outs() << TokenNames
[static_cast<int>(Token
.kind())] << ": "
650 if (Token
.kind() == RCToken::Kind::Int
)
651 outs() << "; int value = " << Token
.intValue();
657 WriterParams
&Params
= Opts
.Params
;
658 SmallString
<128> InputFile(Src
);
659 llvm::sys::fs::make_absolute(InputFile
);
660 Params
.InputFilePath
= InputFile
;
662 switch (Params
.CodePage
) {
668 fatalError("Unsupported code page, only 0, 1252 and 65001 are supported!");
671 std::unique_ptr
<ResourceFileWriter
> Visitor
;
673 if (!Opts
.IsDryRun
) {
675 auto FOut
= std::make_unique
<raw_fd_ostream
>(
676 Dest
, EC
, sys::fs::FA_Read
| sys::fs::FA_Write
);
678 fatalError("Error opening output file '" + Dest
+ "': " + EC
.message());
679 Visitor
= std::make_unique
<ResourceFileWriter
>(Params
, std::move(FOut
));
680 Visitor
->AppendNull
= Opts
.AppendNull
;
682 ExitOnErr(NullResource().visit(Visitor
.get()));
684 unsigned PrimaryLangId
= Opts
.LangId
& 0x3ff;
685 unsigned SubLangId
= Opts
.LangId
>> 10;
686 ExitOnErr(LanguageResource(PrimaryLangId
, SubLangId
).visit(Visitor
.get()));
689 rc::RCParser Parser
{std::move(Tokens
)};
690 while (!Parser
.isEof()) {
691 auto Resource
= ExitOnErr(Parser
.parseSingleResource());
693 Resource
->log(outs());
695 ExitOnErr(Resource
->visit(Visitor
.get()));
698 // STRINGTABLE resources come at the very end.
700 ExitOnErr(Visitor
->dumpAllStringTables());
703 void doCvtres(std::string Src
, std::string Dest
, std::string TargetTriple
) {
704 object::WindowsResourceParser Parser
;
706 ErrorOr
<std::unique_ptr
<MemoryBuffer
>> BufferOrErr
=
707 MemoryBuffer::getFile(Src
);
709 fatalError("Error opening file '" + Twine(Src
) +
710 "': " + BufferOrErr
.getError().message());
711 std::unique_ptr
<MemoryBuffer
> &Buffer
= BufferOrErr
.get();
712 std::unique_ptr
<object::WindowsResource
> Binary
=
713 ExitOnErr(object::WindowsResource::createWindowsResource(
714 Buffer
->getMemBufferRef()));
716 std::vector
<std::string
> Duplicates
;
717 ExitOnErr(Parser
.parse(Binary
.get(), Duplicates
));
718 for (const auto &DupeDiag
: Duplicates
)
719 fatalError("Duplicate resources: " + DupeDiag
);
721 Triple
T(TargetTriple
);
722 COFF::MachineTypes MachineType
;
723 switch (T
.getArch()) {
725 MachineType
= COFF::IMAGE_FILE_MACHINE_I386
;
728 MachineType
= COFF::IMAGE_FILE_MACHINE_AMD64
;
732 MachineType
= COFF::IMAGE_FILE_MACHINE_ARMNT
;
734 case Triple::aarch64
:
735 MachineType
= COFF::IMAGE_FILE_MACHINE_ARM64
;
738 fatalError("Unsupported architecture in target '" + Twine(TargetTriple
) +
742 std::unique_ptr
<MemoryBuffer
> OutputBuffer
=
743 ExitOnErr(object::writeWindowsResourceCOFF(MachineType
, Parser
,
744 /*DateTimeStamp*/ 0));
745 std::unique_ptr
<FileOutputBuffer
> FileBuffer
=
746 ExitOnErr(FileOutputBuffer::create(Dest
, OutputBuffer
->getBufferSize()));
747 std::copy(OutputBuffer
->getBufferStart(), OutputBuffer
->getBufferEnd(),
748 FileBuffer
->getBufferStart());
749 ExitOnErr(FileBuffer
->commit());
752 } // anonymous namespace
754 int llvm_rc_main(int Argc
, char **Argv
, const llvm::ToolContext
&) {
755 InitLLVM
X(Argc
, Argv
);
756 ExitOnErr
.setBanner("llvm-rc: ");
758 char **DashDash
= std::find_if(Argv
+ 1, Argv
+ Argc
,
759 [](StringRef Str
) { return Str
== "--"; });
760 ArrayRef
<const char *> ArgsArr
= ArrayRef(Argv
+ 1, DashDash
);
761 ArrayRef
<const char *> FileArgsArr
;
762 if (DashDash
!= Argv
+ Argc
)
763 FileArgsArr
= ArrayRef(DashDash
+ 1, Argv
+ Argc
);
765 RcOptions Opts
= getOptions(Argv
[0], ArgsArr
, FileArgsArr
);
767 std::string ResFile
= Opts
.OutputFile
;
768 if (Opts
.InputFormat
== Rc
) {
769 if (Opts
.OutputFormat
== Coff
) {
770 ResFile
= createTempFile("rc", "res");
771 TempResFile
.setFile(ResFile
);
773 doRc(Opts
.InputFile
, ResFile
, Opts
, Argv
[0]);
775 ResFile
= Opts
.InputFile
;
777 if (Opts
.OutputFormat
== Coff
) {
778 doCvtres(ResFile
, Opts
.OutputFile
, Opts
.Triple
);