2017-08-10 18:21:44 +02:00
|
|
|
//===-- llvm-rc.cpp - Compile .rc scripts into .res -------------*- C++ -*-===//
|
2017-07-25 02:25:18 +02:00
|
|
|
//
|
2019-01-19 09:50:56 +01:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2017-07-25 02:25:18 +02:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// Compile .rc scripts into .res files. This is intended to be a
|
|
|
|
// platform-independent port of Microsoft's rc.exe tool.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2017-09-29 19:14:09 +02:00
|
|
|
#include "ResourceFileWriter.h"
|
2018-05-09 20:21:03 +02:00
|
|
|
#include "ResourceScriptCppFilter.h"
|
2017-08-18 20:24:17 +02:00
|
|
|
#include "ResourceScriptParser.h"
|
2017-09-29 19:14:09 +02:00
|
|
|
#include "ResourceScriptStmt.h"
|
|
|
|
#include "ResourceScriptToken.h"
|
2017-08-10 18:21:44 +02:00
|
|
|
|
2017-07-25 02:25:18 +02:00
|
|
|
#include "llvm/Option/Arg.h"
|
|
|
|
#include "llvm/Option/ArgList.h"
|
|
|
|
#include "llvm/Support/Error.h"
|
2017-09-29 19:14:09 +02:00
|
|
|
#include "llvm/Support/FileSystem.h"
|
2018-04-13 20:26:06 +02:00
|
|
|
#include "llvm/Support/InitLLVM.h"
|
2017-07-25 02:25:18 +02:00
|
|
|
#include "llvm/Support/ManagedStatic.h"
|
2017-11-17 02:00:35 +01:00
|
|
|
#include "llvm/Support/MemoryBuffer.h"
|
2018-05-02 23:15:24 +02:00
|
|
|
#include "llvm/Support/Path.h"
|
2017-07-25 02:25:18 +02:00
|
|
|
#include "llvm/Support/PrettyStackTrace.h"
|
|
|
|
#include "llvm/Support/Process.h"
|
|
|
|
#include "llvm/Support/Signals.h"
|
|
|
|
#include "llvm/Support/raw_ostream.h"
|
|
|
|
|
2019-01-16 09:09:22 +01:00
|
|
|
#include <algorithm>
|
2017-07-25 02:25:18 +02:00
|
|
|
#include <system_error>
|
|
|
|
|
|
|
|
using namespace llvm;
|
2017-09-29 19:14:09 +02:00
|
|
|
using namespace llvm::rc;
|
2017-07-25 02:25:18 +02:00
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
// Input options tables.
|
|
|
|
|
|
|
|
enum ID {
|
|
|
|
OPT_INVALID = 0, // This is not a correct option ID.
|
|
|
|
#define OPTION(PREFIX, NAME, ID, KIND, GROUP, ALIAS, ALIASARGS, FLAGS, PARAM, \
|
|
|
|
HELPTEXT, METAVAR, VALUES) \
|
|
|
|
OPT_##ID,
|
|
|
|
#include "Opts.inc"
|
|
|
|
#undef OPTION
|
|
|
|
};
|
|
|
|
|
|
|
|
#define PREFIX(NAME, VALUE) const char *const NAME[] = VALUE;
|
|
|
|
#include "Opts.inc"
|
|
|
|
#undef PREFIX
|
|
|
|
|
|
|
|
static const opt::OptTable::Info InfoTable[] = {
|
|
|
|
#define OPTION(PREFIX, NAME, ID, KIND, GROUP, ALIAS, ALIASARGS, FLAGS, PARAM, \
|
|
|
|
HELPTEXT, METAVAR, VALUES) \
|
|
|
|
{ \
|
|
|
|
PREFIX, NAME, HELPTEXT, \
|
|
|
|
METAVAR, OPT_##ID, opt::Option::KIND##Class, \
|
|
|
|
PARAM, FLAGS, OPT_##GROUP, \
|
|
|
|
OPT_##ALIAS, ALIASARGS, VALUES},
|
|
|
|
#include "Opts.inc"
|
|
|
|
#undef OPTION
|
|
|
|
};
|
|
|
|
|
|
|
|
class RcOptTable : public opt::OptTable {
|
|
|
|
public:
|
|
|
|
RcOptTable() : OptTable(InfoTable, /* IgnoreCase = */ true) {}
|
|
|
|
};
|
|
|
|
|
|
|
|
static ExitOnError ExitOnErr;
|
2017-08-10 18:21:44 +02:00
|
|
|
|
2017-10-11 22:12:09 +02:00
|
|
|
LLVM_ATTRIBUTE_NORETURN static void fatalError(const Twine &Message) {
|
2017-08-10 18:21:44 +02:00
|
|
|
errs() << Message << "\n";
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
2017-07-25 02:25:18 +02:00
|
|
|
} // anonymous namespace
|
|
|
|
|
2018-04-13 20:26:06 +02:00
|
|
|
int main(int Argc, const char **Argv) {
|
|
|
|
InitLLVM X(Argc, Argv);
|
2017-07-25 02:25:18 +02:00
|
|
|
ExitOnErr.setBanner("llvm-rc: ");
|
|
|
|
|
|
|
|
RcOptTable T;
|
|
|
|
unsigned MAI, MAC;
|
2019-01-16 09:09:22 +01:00
|
|
|
const char **DashDash = std::find_if(
|
|
|
|
Argv + 1, Argv + Argc, [](StringRef Str) { return Str == "--"; });
|
|
|
|
ArrayRef<const char *> ArgsArr = makeArrayRef(Argv + 1, DashDash);
|
|
|
|
|
2017-07-25 02:25:18 +02:00
|
|
|
opt::InputArgList InputArgs = T.ParseArgs(ArgsArr, MAI, MAC);
|
|
|
|
|
|
|
|
// The tool prints nothing when invoked with no command-line arguments.
|
2017-08-10 18:21:44 +02:00
|
|
|
if (InputArgs.hasArg(OPT_HELP)) {
|
2018-10-10 02:15:31 +02:00
|
|
|
T.PrintHelp(outs(), "rc [options] file...", "Resource Converter", false);
|
2017-08-10 18:21:44 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
const bool BeVerbose = InputArgs.hasArg(OPT_VERBOSE);
|
|
|
|
|
|
|
|
std::vector<std::string> InArgsInfo = InputArgs.getAllArgValues(OPT_INPUT);
|
2019-01-16 09:09:22 +01:00
|
|
|
if (DashDash != Argv + Argc)
|
|
|
|
InArgsInfo.insert(InArgsInfo.end(), DashDash + 1, Argv + Argc);
|
2017-08-10 18:21:44 +02:00
|
|
|
if (InArgsInfo.size() != 1) {
|
|
|
|
fatalError("Exactly one input file should be provided.");
|
|
|
|
}
|
|
|
|
|
|
|
|
// Read and tokenize the input file.
|
2017-10-11 22:12:09 +02:00
|
|
|
ErrorOr<std::unique_ptr<MemoryBuffer>> File =
|
|
|
|
MemoryBuffer::getFile(InArgsInfo[0]);
|
2017-08-10 18:21:44 +02:00
|
|
|
if (!File) {
|
2017-10-11 22:12:09 +02:00
|
|
|
fatalError("Error opening file '" + Twine(InArgsInfo[0]) +
|
2017-08-10 18:21:44 +02:00
|
|
|
"': " + File.getError().message());
|
|
|
|
}
|
|
|
|
|
|
|
|
std::unique_ptr<MemoryBuffer> FileContents = std::move(*File);
|
|
|
|
StringRef Contents = FileContents->getBuffer();
|
|
|
|
|
2018-05-09 20:21:03 +02:00
|
|
|
std::string FilteredContents = filterCppOutput(Contents);
|
|
|
|
std::vector<RCToken> Tokens = ExitOnErr(tokenizeRC(FilteredContents));
|
2017-08-10 18:21:44 +02:00
|
|
|
|
|
|
|
if (BeVerbose) {
|
|
|
|
const Twine TokenNames[] = {
|
|
|
|
#define TOKEN(Name) #Name,
|
|
|
|
#define SHORT_TOKEN(Name, Ch) #Name,
|
2017-11-21 01:23:19 +01:00
|
|
|
#include "ResourceScriptTokenList.def"
|
2017-08-10 18:21:44 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
for (const RCToken &Token : Tokens) {
|
|
|
|
outs() << TokenNames[static_cast<int>(Token.kind())] << ": "
|
|
|
|
<< Token.value();
|
|
|
|
if (Token.kind() == RCToken::Kind::Int)
|
|
|
|
outs() << "; int value = " << Token.intValue();
|
|
|
|
|
|
|
|
outs() << "\n";
|
|
|
|
}
|
|
|
|
}
|
2017-07-25 02:25:18 +02:00
|
|
|
|
2018-05-02 21:43:44 +02:00
|
|
|
WriterParams Params;
|
2017-10-11 22:12:09 +02:00
|
|
|
SmallString<128> InputFile(InArgsInfo[0]);
|
|
|
|
llvm::sys::fs::make_absolute(InputFile);
|
|
|
|
Params.InputFilePath = InputFile;
|
|
|
|
Params.Include = InputArgs.getAllArgValues(OPT_INCLUDE);
|
|
|
|
Params.NoInclude = InputArgs.getAllArgValues(OPT_NOINCLUDE);
|
|
|
|
|
2018-05-02 21:43:44 +02:00
|
|
|
if (InputArgs.hasArg(OPT_CODEPAGE)) {
|
|
|
|
if (InputArgs.getLastArgValue(OPT_CODEPAGE)
|
|
|
|
.getAsInteger(10, Params.CodePage))
|
|
|
|
fatalError("Invalid code page: " +
|
|
|
|
InputArgs.getLastArgValue(OPT_CODEPAGE));
|
|
|
|
switch (Params.CodePage) {
|
|
|
|
case CpAcp:
|
|
|
|
case CpWin1252:
|
|
|
|
case CpUtf8:
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
fatalError(
|
|
|
|
"Unsupported code page, only 0, 1252 and 65001 are supported!");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-29 19:14:09 +02:00
|
|
|
std::unique_ptr<ResourceFileWriter> Visitor;
|
|
|
|
bool IsDryRun = InputArgs.hasArg(OPT_DRY_RUN);
|
|
|
|
|
|
|
|
if (!IsDryRun) {
|
|
|
|
auto OutArgsInfo = InputArgs.getAllArgValues(OPT_FILEOUT);
|
2018-05-02 23:15:24 +02:00
|
|
|
if (OutArgsInfo.empty()) {
|
|
|
|
SmallString<128> OutputFile = InputFile;
|
|
|
|
llvm::sys::path::replace_extension(OutputFile, "res");
|
|
|
|
OutArgsInfo.push_back(OutputFile.str());
|
|
|
|
}
|
|
|
|
|
2017-09-29 19:14:09 +02:00
|
|
|
if (OutArgsInfo.size() != 1)
|
|
|
|
fatalError(
|
2018-05-02 23:15:24 +02:00
|
|
|
"No more than one output file should be provided (using /FO flag).");
|
2017-09-29 19:14:09 +02:00
|
|
|
|
|
|
|
std::error_code EC;
|
2019-08-15 17:54:37 +02:00
|
|
|
auto FOut = std::make_unique<raw_fd_ostream>(
|
2018-06-07 21:58:58 +02:00
|
|
|
OutArgsInfo[0], EC, sys::fs::FA_Read | sys::fs::FA_Write);
|
2017-09-29 19:14:09 +02:00
|
|
|
if (EC)
|
|
|
|
fatalError("Error opening output file '" + OutArgsInfo[0] +
|
|
|
|
"': " + EC.message());
|
2019-08-15 17:54:37 +02:00
|
|
|
Visitor = std::make_unique<ResourceFileWriter>(Params, std::move(FOut));
|
2017-10-06 23:30:55 +02:00
|
|
|
Visitor->AppendNull = InputArgs.hasArg(OPT_ADD_NULL);
|
2017-09-29 19:14:09 +02:00
|
|
|
|
|
|
|
ExitOnErr(NullResource().visit(Visitor.get()));
|
|
|
|
|
|
|
|
// Set the default language; choose en-US arbitrarily.
|
|
|
|
ExitOnErr(LanguageResource(0x09, 0x01).visit(Visitor.get()));
|
|
|
|
}
|
|
|
|
|
2017-08-18 20:24:17 +02:00
|
|
|
rc::RCParser Parser{std::move(Tokens)};
|
|
|
|
while (!Parser.isEof()) {
|
|
|
|
auto Resource = ExitOnErr(Parser.parseSingleResource());
|
|
|
|
if (BeVerbose)
|
|
|
|
Resource->log(outs());
|
2017-09-29 19:14:09 +02:00
|
|
|
if (!IsDryRun)
|
|
|
|
ExitOnErr(Resource->visit(Visitor.get()));
|
2017-08-18 20:24:17 +02:00
|
|
|
}
|
|
|
|
|
2017-10-06 23:30:55 +02:00
|
|
|
// STRINGTABLE resources come at the very end.
|
|
|
|
if (!IsDryRun)
|
|
|
|
ExitOnErr(Visitor->dumpAllStringTables());
|
|
|
|
|
2017-07-25 02:25:18 +02:00
|
|
|
return 0;
|
|
|
|
}
|