blob: c62a037874b31441641598056f2714f3a71cb1ef [file] [log] [blame]
Marek Sokolowski719e22d2017-08-10 16:21:44 +00001//===-- llvm-rc.cpp - Compile .rc scripts into .res -------------*- C++ -*-===//
Marek Sokolowski2ce2fa42017-07-25 00:25:18 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// Compile .rc scripts into .res files. This is intended to be a
11// platform-independent port of Microsoft's rc.exe tool.
12//
13//===----------------------------------------------------------------------===//
14
Marek Sokolowski8f193432017-09-29 17:14:09 +000015#include "ResourceFileWriter.h"
Marek Sokolowski5cd3d5c2017-08-18 18:24:17 +000016#include "ResourceScriptParser.h"
Marek Sokolowski8f193432017-09-29 17:14:09 +000017#include "ResourceScriptStmt.h"
18#include "ResourceScriptToken.h"
Marek Sokolowski719e22d2017-08-10 16:21:44 +000019
Marek Sokolowski2ce2fa42017-07-25 00:25:18 +000020#include "llvm/Option/Arg.h"
21#include "llvm/Option/ArgList.h"
22#include "llvm/Support/Error.h"
Marek Sokolowski8f193432017-09-29 17:14:09 +000023#include "llvm/Support/FileSystem.h"
Marek Sokolowski2ce2fa42017-07-25 00:25:18 +000024#include "llvm/Support/ManagedStatic.h"
25#include "llvm/Support/PrettyStackTrace.h"
26#include "llvm/Support/Process.h"
27#include "llvm/Support/Signals.h"
28#include "llvm/Support/raw_ostream.h"
29
30#include <system_error>
31
32using namespace llvm;
Marek Sokolowski8f193432017-09-29 17:14:09 +000033using namespace llvm::rc;
Marek Sokolowski2ce2fa42017-07-25 00:25:18 +000034
35namespace {
36
37// Input options tables.
38
39enum ID {
40 OPT_INVALID = 0, // This is not a correct option ID.
41#define OPTION(PREFIX, NAME, ID, KIND, GROUP, ALIAS, ALIASARGS, FLAGS, PARAM, \
42 HELPTEXT, METAVAR, VALUES) \
43 OPT_##ID,
44#include "Opts.inc"
45#undef OPTION
46};
47
48#define PREFIX(NAME, VALUE) const char *const NAME[] = VALUE;
49#include "Opts.inc"
50#undef PREFIX
51
52static const opt::OptTable::Info InfoTable[] = {
53#define OPTION(PREFIX, NAME, ID, KIND, GROUP, ALIAS, ALIASARGS, FLAGS, PARAM, \
54 HELPTEXT, METAVAR, VALUES) \
55 { \
56 PREFIX, NAME, HELPTEXT, \
57 METAVAR, OPT_##ID, opt::Option::KIND##Class, \
58 PARAM, FLAGS, OPT_##GROUP, \
59 OPT_##ALIAS, ALIASARGS, VALUES},
60#include "Opts.inc"
61#undef OPTION
62};
63
64class RcOptTable : public opt::OptTable {
65public:
66 RcOptTable() : OptTable(InfoTable, /* IgnoreCase = */ true) {}
67};
68
69static ExitOnError ExitOnErr;
Marek Sokolowski719e22d2017-08-10 16:21:44 +000070
71LLVM_ATTRIBUTE_NORETURN static void fatalError(Twine Message) {
72 errs() << Message << "\n";
73 exit(1);
74}
75
Marek Sokolowski2ce2fa42017-07-25 00:25:18 +000076} // anonymous namespace
77
78int main(int argc_, const char *argv_[]) {
79 sys::PrintStackTraceOnErrorSignal(argv_[0]);
80 PrettyStackTraceProgram X(argc_, argv_);
81
82 ExitOnErr.setBanner("llvm-rc: ");
83
84 SmallVector<const char *, 256> argv;
85 SpecificBumpPtrAllocator<char> ArgAllocator;
86 ExitOnErr(errorCodeToError(sys::Process::GetArgumentVector(
87 argv, makeArrayRef(argv_, argc_), ArgAllocator)));
88
89 llvm_shutdown_obj Y;
90
91 RcOptTable T;
92 unsigned MAI, MAC;
93 ArrayRef<const char *> ArgsArr = makeArrayRef(argv_ + 1, argc_);
94 opt::InputArgList InputArgs = T.ParseArgs(ArgsArr, MAI, MAC);
95
96 // The tool prints nothing when invoked with no command-line arguments.
Marek Sokolowski719e22d2017-08-10 16:21:44 +000097 if (InputArgs.hasArg(OPT_HELP)) {
Marek Sokolowski2ce2fa42017-07-25 00:25:18 +000098 T.PrintHelp(outs(), "rc", "Resource Converter", false);
Marek Sokolowski719e22d2017-08-10 16:21:44 +000099 return 0;
100 }
101
102 const bool BeVerbose = InputArgs.hasArg(OPT_VERBOSE);
103
104 std::vector<std::string> InArgsInfo = InputArgs.getAllArgValues(OPT_INPUT);
105 if (InArgsInfo.size() != 1) {
106 fatalError("Exactly one input file should be provided.");
107 }
108
109 // Read and tokenize the input file.
110 const Twine &Filename = InArgsInfo[0];
111 ErrorOr<std::unique_ptr<MemoryBuffer>> File = MemoryBuffer::getFile(Filename);
112 if (!File) {
113 fatalError("Error opening file '" + Filename +
114 "': " + File.getError().message());
115 }
116
117 std::unique_ptr<MemoryBuffer> FileContents = std::move(*File);
118 StringRef Contents = FileContents->getBuffer();
119
120 std::vector<RCToken> Tokens = ExitOnErr(tokenizeRC(Contents));
121
122 if (BeVerbose) {
123 const Twine TokenNames[] = {
124#define TOKEN(Name) #Name,
125#define SHORT_TOKEN(Name, Ch) #Name,
126#include "ResourceScriptTokenList.h"
127#undef TOKEN
128#undef SHORT_TOKEN
129 };
130
131 for (const RCToken &Token : Tokens) {
132 outs() << TokenNames[static_cast<int>(Token.kind())] << ": "
133 << Token.value();
134 if (Token.kind() == RCToken::Kind::Int)
135 outs() << "; int value = " << Token.intValue();
136
137 outs() << "\n";
138 }
139 }
Marek Sokolowski2ce2fa42017-07-25 00:25:18 +0000140
Marek Sokolowski8f193432017-09-29 17:14:09 +0000141 std::unique_ptr<ResourceFileWriter> Visitor;
142 bool IsDryRun = InputArgs.hasArg(OPT_DRY_RUN);
143
144 if (!IsDryRun) {
145 auto OutArgsInfo = InputArgs.getAllArgValues(OPT_FILEOUT);
146 if (OutArgsInfo.size() != 1)
147 fatalError(
148 "Exactly one output file should be provided (using /FO flag).");
149
150 std::error_code EC;
151 auto FOut =
152 llvm::make_unique<raw_fd_ostream>(OutArgsInfo[0], EC, sys::fs::F_RW);
153 if (EC)
154 fatalError("Error opening output file '" + OutArgsInfo[0] +
155 "': " + EC.message());
156 Visitor = llvm::make_unique<ResourceFileWriter>(std::move(FOut));
157
158 ExitOnErr(NullResource().visit(Visitor.get()));
159
160 // Set the default language; choose en-US arbitrarily.
161 ExitOnErr(LanguageResource(0x09, 0x01).visit(Visitor.get()));
162 }
163
Marek Sokolowski5cd3d5c2017-08-18 18:24:17 +0000164 rc::RCParser Parser{std::move(Tokens)};
165 while (!Parser.isEof()) {
166 auto Resource = ExitOnErr(Parser.parseSingleResource());
167 if (BeVerbose)
168 Resource->log(outs());
Marek Sokolowski8f193432017-09-29 17:14:09 +0000169 if (!IsDryRun)
170 ExitOnErr(Resource->visit(Visitor.get()));
Marek Sokolowski5cd3d5c2017-08-18 18:24:17 +0000171 }
172
Marek Sokolowski2ce2fa42017-07-25 00:25:18 +0000173 return 0;
174}