Run DCE after a LoopFlatten test to reduce spurious output [nfc]
[llvm-project.git] / llvm / tools / llvm-rc / llvm-rc.cpp
blobb955347f2a8646e9626fd672cb0e171a47a9245c
1 //===-- llvm-rc.cpp - Compile .rc scripts into .res -------------*- C++ -*-===//
2 //
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
6 //
7 //===----------------------------------------------------------------------===//
8 //
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"
42 #include <algorithm>
43 #include <system_error>
45 using namespace llvm;
46 using namespace llvm::rc;
47 using namespace llvm::opt;
49 namespace {
51 // Input options tables.
53 enum ID {
54 OPT_INVALID = 0, // This is not a correct option ID.
55 #define OPTION(...) LLVM_MAKE_OPT_ID(__VA_ARGS__),
56 #include "Opts.inc"
57 #undef OPTION
60 namespace rc_opt {
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);
65 #include "Opts.inc"
66 #undef PREFIX
68 static constexpr opt::OptTable::Info InfoTable[] = {
69 #define OPTION(...) LLVM_CONSTRUCT_OPT_INFO(__VA_ARGS__),
70 #include "Opts.inc"
71 #undef OPTION
73 } // namespace rc_opt
75 class RcOptTable : public opt::GenericOptTable {
76 public:
77 RcOptTable() : GenericOptTable(rc_opt::InfoTable, /* IgnoreCase = */ true) {}
80 enum Windres_ID {
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"
84 #undef OPTION
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"
93 #undef PREFIX
95 static constexpr opt::OptTable::Info InfoTable[] = {
96 #define OPTION(...) \
97 LLVM_CONSTRUCT_OPT_INFO_WITH_ID_PREFIX(WINDRES_, __VA_ARGS__),
98 #include "WindresOpts.inc"
99 #undef OPTION
101 } // namespace windres_opt
103 class WindresOptTable : public opt::GenericOptTable {
104 public:
105 WindresOptTable()
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";
115 exit(1);
118 std::string createTempFile(const Twine &Prefix, StringRef Suffix) {
119 std::error_code EC;
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);
146 if (Path)
147 return Path;
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);
154 if (Path)
155 return Path;
157 return Path;
160 bool isUsableArch(Triple::ArchType Arch) {
161 switch (Arch) {
162 case Triple::x86:
163 case Triple::x86_64:
164 case Triple::arm:
165 case Triple::thumb:
166 case Triple::aarch64:
167 // These work properly with the clang driver, setting the expected
168 // defines such as _WIN32 etc.
169 return true;
170 default:
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.
174 return false;
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);
190 return T.str();
193 std::string getMingwTriple() {
194 Triple T(sys::getDefaultTargetTriple());
195 if (!isUsableArch(T.getArch()))
196 T.setArch(getDefaultFallbackArch());
197 if (T.isWindowsGNUEnvironment())
198 return T.str();
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 };
208 struct RcOptions {
209 bool Preprocess = true;
210 bool PrintCmdAndExit = false;
211 std::string Triple;
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;
222 WriterParams Params;
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,
230 const char *Argv0) {
231 std::string Clang;
232 if (Opts.PrintCmdAndExit || !Opts.PreprocessCmd.empty()) {
233 Clang = "clang";
234 } else {
235 ErrorOr<std::string> ClangOrErr = findClang(Argv0, Opts.Triple);
236 if (ClangOrErr) {
237 Clang = *ClangOrErr;
238 } else {
239 errs() << "llvm-rc: Unable to find clang for preprocessing."
240 << "\n";
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()) {
252 Args.clear();
253 for (const auto &S : Opts.PreprocessCmd)
254 Args.push_back(S);
256 Args.push_back(Src);
257 Args.push_back("-o");
258 Args.push_back(Dst);
259 for (const auto &S : Opts.PreprocessArgs)
260 Args.push_back(S);
261 if (Opts.PrintCmdAndExit || Opts.BeVerbose) {
262 for (const auto &A : Args) {
263 outs() << " ";
264 sys::printArg(outs(), A, Opts.PrintCmdAndExit);
266 outs() << "\n";
267 if (Opts.PrintCmdAndExit)
268 exit(0);
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);
273 if (Res) {
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())
293 .Case("rc", Rc)
294 .Case("res", Res)
295 .Case("coff", Coff)
296 .Default(Unknown);
297 if (F == Unknown)
298 fatalError("Unable to parse '" + Twine(S) + "' as a format");
299 return F;
302 void deduceFormat(Format &Dest, StringRef File) {
303 Format F = StringSwitch<Format>(sys::path::extension(File.lower()))
304 .Case(".rc", Rc)
305 .Case(".res", Res)
306 .Case(".o", Coff)
307 .Case(".obj", Coff)
308 .Default(Unknown);
309 if (F != Unknown)
310 Dest = F;
313 std::string unescape(StringRef S) {
314 std::string Out;
315 Out.reserve(S.size());
316 for (int I = 0, E = S.size(); I < E; I++) {
317 if (S[I] == '\\') {
318 if (I + 1 < E)
319 Out.push_back(S[++I]);
320 else
321 fatalError("Unterminated escape");
322 continue;
323 } else if (S[I] == '"') {
324 // This eats an individual unescaped quote, like a shell would do.
325 continue;
327 Out.push_back(S[I]);
329 return Out;
332 std::vector<std::string> unescapeSplit(StringRef S) {
333 std::vector<std::string> OutArgs;
334 std::string Out;
335 bool InQuote = false;
336 for (int I = 0, E = S.size(); I < E; I++) {
337 if (S[I] == '\\') {
338 if (I + 1 < E)
339 Out.push_back(S[++I]);
340 else
341 fatalError("Unterminated escape");
342 continue;
344 if (S[I] == '"') {
345 InQuote = !InQuote;
346 continue;
348 if (S[I] == ' ' && !InQuote) {
349 OutArgs.push_back(Out);
350 Out.clear();
351 continue;
353 Out.push_back(S[I]);
355 if (InQuote)
356 fatalError("Unterminated quote");
357 if (!Out.empty())
358 OutArgs.push_back(Out);
359 return OutArgs;
362 RcOptions parseWindresOptions(ArrayRef<const char *> ArgsArr,
363 ArrayRef<const char *> InputArgsArray,
364 std::string Prefix) {
365 WindresOptTable T;
366 RcOptions Opts;
367 unsigned MAI, MAC;
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);
376 exit(0);
379 if (InputArgs.hasArg(WINDRES_version)) {
380 outs() << "llvm-windres, compatible with GNU windres\n";
381 cl::PrintVersionMessage();
382 exit(0);
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());
393 } else {
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());
403 } else {
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)) {
409 Opts.InputFormat =
410 parseFormat(InputArgs.getLastArgValue(WINDRES_input_format));
411 } else {
412 deduceFormat(Opts.InputFormat, Opts.InputFile);
414 if (Opts.InputFormat == Coff)
415 fatalError("Unsupported input format");
417 if (InputArgs.hasArg(WINDRES_output_format)) {
418 Opts.OutputFormat =
419 parseFormat(InputArgs.getLastArgValue(WINDRES_output_format));
420 } else {
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";
429 exit(0);
432 Opts.PrintCmdAndExit = InputArgs.hasArg(WINDRES__HASH_HASH_HASH);
433 Opts.Preprocess = !InputArgs.hasArg(WINDRES_no_preprocess);
434 Triple TT(Prefix);
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";
441 else
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;
447 else
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
463 // and windows.
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
478 // logic.
479 Opts.Params.Include.push_back(Arg->getValue());
480 Opts.PreprocessArgs.push_back("-I");
481 Opts.PreprocessArgs.push_back(Arg->getValue());
482 break;
483 case WINDRES_define:
484 Opts.PreprocessArgs.push_back("-D");
485 Opts.PreprocessArgs.push_back(Value);
486 break;
487 case WINDRES_undef:
488 Opts.PreprocessArgs.push_back("-U");
489 Opts.PreprocessArgs.push_back(Value);
490 break;
491 case WINDRES_preprocessor_arg:
492 Opts.PreprocessArgs.push_back(Value);
493 break;
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))
499 Opts.PreprocessCmd =
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);
519 return Opts;
522 RcOptions parseRcOptions(ArrayRef<const char *> ArgsArr,
523 ArrayRef<const char *> InputArgsArray) {
524 RcOptTable T;
525 RcOptions Opts;
526 unsigned MAI, MAC;
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",
532 false);
533 exit(0);
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");
550 break;
551 case OPT_define:
552 Opts.PreprocessArgs.push_back("-D");
553 break;
554 case OPT_undef:
555 Opts.PreprocessArgs.push_back("-U");
556 break;
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
568 #ifdef _WIN32
569 ::_putenv("INCLUDE=");
570 #else
571 ::unsetenv("INCLUDE");
572 #endif
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)
590 fatalError(
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));
602 return Opts;
605 RcOptions getOptions(const char *Argv0, ArrayRef<const char *> ArgsArr,
606 ArrayRef<const char *> InputArgs) {
607 std::string Prefix;
608 bool IsWindres;
609 std::tie(IsWindres, Prefix) = isWindres(Argv0);
610 if (IsWindres)
611 return parseWindresOptions(ArgsArr, InputArgs, Prefix);
612 else
613 return parseRcOptions(ArgsArr, InputArgs);
616 void doRc(std::string Src, std::string Dest, RcOptions &Opts,
617 const char *Argv0) {
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);
629 if (!File) {
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())] << ": "
649 << Token.value();
650 if (Token.kind() == RCToken::Kind::Int)
651 outs() << "; int value = " << Token.intValue();
653 outs() << "\n";
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) {
663 case CpAcp:
664 case CpWin1252:
665 case CpUtf8:
666 break;
667 default:
668 fatalError("Unsupported code page, only 0, 1252 and 65001 are supported!");
671 std::unique_ptr<ResourceFileWriter> Visitor;
673 if (!Opts.IsDryRun) {
674 std::error_code EC;
675 auto FOut = std::make_unique<raw_fd_ostream>(
676 Dest, EC, sys::fs::FA_Read | sys::fs::FA_Write);
677 if (EC)
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());
692 if (Opts.BeVerbose)
693 Resource->log(outs());
694 if (!Opts.IsDryRun)
695 ExitOnErr(Resource->visit(Visitor.get()));
698 // STRINGTABLE resources come at the very end.
699 if (!Opts.IsDryRun)
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);
708 if (!BufferOrErr)
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()) {
724 case Triple::x86:
725 MachineType = COFF::IMAGE_FILE_MACHINE_I386;
726 break;
727 case Triple::x86_64:
728 MachineType = COFF::IMAGE_FILE_MACHINE_AMD64;
729 break;
730 case Triple::arm:
731 case Triple::thumb:
732 MachineType = COFF::IMAGE_FILE_MACHINE_ARMNT;
733 break;
734 case Triple::aarch64:
735 MachineType = COFF::IMAGE_FILE_MACHINE_ARM64;
736 break;
737 default:
738 fatalError("Unsupported architecture in target '" + Twine(TargetTriple) +
739 "'");
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]);
774 } else {
775 ResFile = Opts.InputFile;
777 if (Opts.OutputFormat == Coff) {
778 doCvtres(ResFile, Opts.OutputFile, Opts.Triple);
781 return 0;