1 //===-- llvm-rc.cpp - Compile .rc scripts into .res -------------*- C++ -*-===//
2 //
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 
15 #include "ResourceScriptToken.h"
16 
17 #include "llvm/Option/Arg.h"
18 #include "llvm/Option/ArgList.h"
19 #include "llvm/Support/Error.h"
20 #include "llvm/Support/ManagedStatic.h"
21 #include "llvm/Support/PrettyStackTrace.h"
22 #include "llvm/Support/Process.h"
23 #include "llvm/Support/Signals.h"
24 #include "llvm/Support/raw_ostream.h"
25 
26 #include <system_error>
27 
28 using namespace llvm;
29 
30 namespace {
31 
32 // Input options tables.
33 
34 enum ID {
35   OPT_INVALID = 0, // This is not a correct option ID.
36 #define OPTION(PREFIX, NAME, ID, KIND, GROUP, ALIAS, ALIASARGS, FLAGS, PARAM,  \
37                HELPTEXT, METAVAR, VALUES)                                      \
38   OPT_##ID,
39 #include "Opts.inc"
40 #undef OPTION
41 };
42 
43 #define PREFIX(NAME, VALUE) const char *const NAME[] = VALUE;
44 #include "Opts.inc"
45 #undef PREFIX
46 
47 static const opt::OptTable::Info InfoTable[] = {
48 #define OPTION(PREFIX, NAME, ID, KIND, GROUP, ALIAS, ALIASARGS, FLAGS, PARAM,  \
49                HELPTEXT, METAVAR, VALUES)                                      \
50   {                                                                            \
51       PREFIX,      NAME,      HELPTEXT,                                        \
52       METAVAR,     OPT_##ID,  opt::Option::KIND##Class,                        \
53       PARAM,       FLAGS,     OPT_##GROUP,                                     \
54       OPT_##ALIAS, ALIASARGS, VALUES},
55 #include "Opts.inc"
56 #undef OPTION
57 };
58 
59 class RcOptTable : public opt::OptTable {
60 public:
61   RcOptTable() : OptTable(InfoTable, /* IgnoreCase = */ true) {}
62 };
63 
64 static ExitOnError ExitOnErr;
65 
66 LLVM_ATTRIBUTE_NORETURN static void fatalError(Twine Message) {
67   errs() << Message << "\n";
68   exit(1);
69 }
70 
71 } // anonymous namespace
72 
73 int main(int argc_, const char *argv_[]) {
74   sys::PrintStackTraceOnErrorSignal(argv_[0]);
75   PrettyStackTraceProgram X(argc_, argv_);
76 
77   ExitOnErr.setBanner("llvm-rc: ");
78 
79   SmallVector<const char *, 256> argv;
80   SpecificBumpPtrAllocator<char> ArgAllocator;
81   ExitOnErr(errorCodeToError(sys::Process::GetArgumentVector(
82       argv, makeArrayRef(argv_, argc_), ArgAllocator)));
83 
84   llvm_shutdown_obj Y;
85 
86   RcOptTable T;
87   unsigned MAI, MAC;
88   ArrayRef<const char *> ArgsArr = makeArrayRef(argv_ + 1, argc_);
89   opt::InputArgList InputArgs = T.ParseArgs(ArgsArr, MAI, MAC);
90 
91   // The tool prints nothing when invoked with no command-line arguments.
92   if (InputArgs.hasArg(OPT_HELP)) {
93     T.PrintHelp(outs(), "rc", "Resource Converter", false);
94     return 0;
95   }
96 
97   const bool BeVerbose = InputArgs.hasArg(OPT_VERBOSE);
98 
99   std::vector<std::string> InArgsInfo = InputArgs.getAllArgValues(OPT_INPUT);
100   if (InArgsInfo.size() != 1) {
101     fatalError("Exactly one input file should be provided.");
102   }
103 
104   // Read and tokenize the input file.
105   const Twine &Filename = InArgsInfo[0];
106   ErrorOr<std::unique_ptr<MemoryBuffer>> File = MemoryBuffer::getFile(Filename);
107   if (!File) {
108     fatalError("Error opening file '" + Filename +
109                "': " + File.getError().message());
110   }
111 
112   std::unique_ptr<MemoryBuffer> FileContents = std::move(*File);
113   StringRef Contents = FileContents->getBuffer();
114 
115   std::vector<RCToken> Tokens = ExitOnErr(tokenizeRC(Contents));
116 
117   if (BeVerbose) {
118     const Twine TokenNames[] = {
119 #define TOKEN(Name) #Name,
120 #define SHORT_TOKEN(Name, Ch) #Name,
121 #include "ResourceScriptTokenList.h"
122 #undef TOKEN
123 #undef SHORT_TOKEN
124     };
125 
126     for (const RCToken &Token : Tokens) {
127       outs() << TokenNames[static_cast<int>(Token.kind())] << ": "
128              << Token.value();
129       if (Token.kind() == RCToken::Kind::Int)
130         outs() << "; int value = " << Token.intValue();
131 
132       outs() << "\n";
133     }
134   }
135 
136   return 0;
137 }
138