llvm-rc.cpp 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765
  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.
  11. //
  12. //===----------------------------------------------------------------------===//
  13. #include "ResourceFileWriter.h"
  14. #include "ResourceScriptCppFilter.h"
  15. #include "ResourceScriptParser.h"
  16. #include "ResourceScriptStmt.h"
  17. #include "ResourceScriptToken.h"
  18. #include "llvm/ADT/Triple.h"
  19. #include "llvm/Object/WindowsResource.h"
  20. #include "llvm/Option/Arg.h"
  21. #include "llvm/Option/ArgList.h"
  22. #include "llvm/Support/CommandLine.h"
  23. #include "llvm/Support/Error.h"
  24. #include "llvm/Support/FileSystem.h"
  25. #include "llvm/Support/FileUtilities.h"
  26. #include "llvm/Support/Host.h"
  27. #include "llvm/Support/InitLLVM.h"
  28. #include "llvm/Support/MemoryBuffer.h"
  29. #include "llvm/Support/Path.h"
  30. #include "llvm/Support/PrettyStackTrace.h"
  31. #include "llvm/Support/Process.h"
  32. #include "llvm/Support/Program.h"
  33. #include "llvm/Support/Signals.h"
  34. #include "llvm/Support/StringSaver.h"
  35. #include "llvm/Support/raw_ostream.h"
  36. #include <algorithm>
  37. #include <system_error>
  38. using namespace llvm;
  39. using namespace llvm::rc;
  40. namespace {
  41. // Input options tables.
  42. enum ID {
  43. OPT_INVALID = 0, // This is not a correct option ID.
  44. #define OPTION(PREFIX, NAME, ID, KIND, GROUP, ALIAS, ALIASARGS, FLAGS, PARAM, \
  45. HELPTEXT, METAVAR, VALUES) \
  46. OPT_##ID,
  47. #include "Opts.inc"
  48. #undef OPTION
  49. };
  50. namespace rc_opt {
  51. #define PREFIX(NAME, VALUE) \
  52. static constexpr StringLiteral NAME##_init[] = VALUE; \
  53. static constexpr ArrayRef<StringLiteral> NAME(NAME##_init, \
  54. std::size(NAME##_init) - 1);
  55. #include "Opts.inc"
  56. #undef PREFIX
  57. static constexpr opt::OptTable::Info InfoTable[] = {
  58. #define OPTION(PREFIX, NAME, ID, KIND, GROUP, ALIAS, ALIASARGS, FLAGS, PARAM, \
  59. HELPTEXT, METAVAR, VALUES) \
  60. { \
  61. PREFIX, NAME, HELPTEXT, \
  62. METAVAR, OPT_##ID, opt::Option::KIND##Class, \
  63. PARAM, FLAGS, OPT_##GROUP, \
  64. OPT_##ALIAS, ALIASARGS, VALUES},
  65. #include "Opts.inc"
  66. #undef OPTION
  67. };
  68. } // namespace rc_opt
  69. class RcOptTable : public opt::GenericOptTable {
  70. public:
  71. RcOptTable() : GenericOptTable(rc_opt::InfoTable, /* IgnoreCase = */ true) {}
  72. };
  73. enum Windres_ID {
  74. WINDRES_INVALID = 0, // This is not a correct option ID.
  75. #define OPTION(PREFIX, NAME, ID, KIND, GROUP, ALIAS, ALIASARGS, FLAGS, PARAM, \
  76. HELPTEXT, METAVAR, VALUES) \
  77. WINDRES_##ID,
  78. #include "WindresOpts.inc"
  79. #undef OPTION
  80. };
  81. namespace windres_opt {
  82. #define PREFIX(NAME, VALUE) \
  83. static constexpr StringLiteral NAME##_init[] = VALUE; \
  84. static constexpr ArrayRef<StringLiteral> NAME(NAME##_init, \
  85. std::size(NAME##_init) - 1);
  86. #include "WindresOpts.inc"
  87. #undef PREFIX
  88. static constexpr opt::OptTable::Info InfoTable[] = {
  89. #define OPTION(PREFIX, NAME, ID, KIND, GROUP, ALIAS, ALIASARGS, FLAGS, PARAM, \
  90. HELPTEXT, METAVAR, VALUES) \
  91. {PREFIX, NAME, HELPTEXT, \
  92. METAVAR, WINDRES_##ID, opt::Option::KIND##Class, \
  93. PARAM, FLAGS, WINDRES_##GROUP, \
  94. WINDRES_##ALIAS, ALIASARGS, VALUES},
  95. #include "WindresOpts.inc"
  96. #undef OPTION
  97. };
  98. } // namespace windres_opt
  99. class WindresOptTable : public opt::GenericOptTable {
  100. public:
  101. WindresOptTable()
  102. : GenericOptTable(windres_opt::InfoTable, /* IgnoreCase = */ false) {}
  103. };
  104. static ExitOnError ExitOnErr;
  105. static FileRemover TempPreprocFile;
  106. static FileRemover TempResFile;
  107. [[noreturn]] static void fatalError(const Twine &Message) {
  108. errs() << Message << "\n";
  109. exit(1);
  110. }
  111. std::string createTempFile(const Twine &Prefix, StringRef Suffix) {
  112. std::error_code EC;
  113. SmallString<128> FileName;
  114. if ((EC = sys::fs::createTemporaryFile(Prefix, Suffix, FileName)))
  115. fatalError("Unable to create temp file: " + EC.message());
  116. return static_cast<std::string>(FileName);
  117. }
  118. ErrorOr<std::string> findClang(const char *Argv0, StringRef Triple) {
  119. StringRef Parent = llvm::sys::path::parent_path(Argv0);
  120. ErrorOr<std::string> Path = std::error_code();
  121. std::string TargetClang = (Triple + "-clang").str();
  122. if (!Parent.empty()) {
  123. // First look for the tool with all potential names in the specific
  124. // directory of Argv0, if known
  125. for (const auto *Name : {TargetClang.c_str(), "clang", "clang-cl"}) {
  126. Path = sys::findProgramByName(Name, Parent);
  127. if (Path)
  128. return Path;
  129. }
  130. }
  131. // If no parent directory known, or not found there, look everywhere in PATH
  132. for (const auto *Name : {"clang", "clang-cl"}) {
  133. Path = sys::findProgramByName(Name);
  134. if (Path)
  135. return Path;
  136. }
  137. return Path;
  138. }
  139. bool isUsableArch(Triple::ArchType Arch) {
  140. switch (Arch) {
  141. case Triple::x86:
  142. case Triple::x86_64:
  143. case Triple::arm:
  144. case Triple::thumb:
  145. case Triple::aarch64:
  146. // These work properly with the clang driver, setting the expected
  147. // defines such as _WIN32 etc.
  148. return true;
  149. default:
  150. // Other archs aren't set up for use with windows as target OS, (clang
  151. // doesn't define e.g. _WIN32 etc), so with them we need to set a
  152. // different default arch.
  153. return false;
  154. }
  155. }
  156. Triple::ArchType getDefaultFallbackArch() {
  157. return Triple::x86_64;
  158. }
  159. std::string getClangClTriple() {
  160. Triple T(sys::getDefaultTargetTriple());
  161. if (!isUsableArch(T.getArch()))
  162. T.setArch(getDefaultFallbackArch());
  163. T.setOS(Triple::Win32);
  164. T.setVendor(Triple::PC);
  165. T.setEnvironment(Triple::MSVC);
  166. T.setObjectFormat(Triple::COFF);
  167. return T.str();
  168. }
  169. std::string getMingwTriple() {
  170. Triple T(sys::getDefaultTargetTriple());
  171. if (!isUsableArch(T.getArch()))
  172. T.setArch(getDefaultFallbackArch());
  173. if (T.isWindowsGNUEnvironment())
  174. return T.str();
  175. // Write out the literal form of the vendor/env here, instead of
  176. // constructing them with enum values (which end up with them in
  177. // normalized form). The literal form of the triple can matter for
  178. // finding include files.
  179. return (Twine(T.getArchName()) + "-w64-mingw32").str();
  180. }
  181. enum Format { Rc, Res, Coff, Unknown };
  182. struct RcOptions {
  183. bool Preprocess = true;
  184. bool PrintCmdAndExit = false;
  185. std::string Triple;
  186. std::vector<std::string> PreprocessCmd;
  187. std::vector<std::string> PreprocessArgs;
  188. std::string InputFile;
  189. Format InputFormat = Rc;
  190. std::string OutputFile;
  191. Format OutputFormat = Res;
  192. bool BeVerbose = false;
  193. WriterParams Params;
  194. bool AppendNull = false;
  195. bool IsDryRun = false;
  196. // Set the default language; choose en-US arbitrarily.
  197. unsigned LangId = (/*PrimaryLangId*/ 0x09) | (/*SubLangId*/ 0x01 << 10);
  198. };
  199. bool preprocess(StringRef Src, StringRef Dst, const RcOptions &Opts,
  200. const char *Argv0) {
  201. std::string Clang;
  202. if (Opts.PrintCmdAndExit) {
  203. Clang = "clang";
  204. } else {
  205. ErrorOr<std::string> ClangOrErr = findClang(Argv0, Opts.Triple);
  206. if (ClangOrErr) {
  207. Clang = *ClangOrErr;
  208. } else {
  209. errs() << "llvm-rc: Unable to find clang, skipping preprocessing."
  210. << "\n";
  211. errs() << "Pass -no-cpp to disable preprocessing. This will be an error "
  212. "in the future."
  213. << "\n";
  214. return false;
  215. }
  216. }
  217. SmallVector<StringRef, 8> Args = {
  218. Clang, "--driver-mode=gcc", "-target", Opts.Triple, "-E",
  219. "-xc", "-DRC_INVOKED"};
  220. if (!Opts.PreprocessCmd.empty()) {
  221. Args.clear();
  222. for (const auto &S : Opts.PreprocessCmd)
  223. Args.push_back(S);
  224. }
  225. Args.push_back(Src);
  226. Args.push_back("-o");
  227. Args.push_back(Dst);
  228. for (const auto &S : Opts.PreprocessArgs)
  229. Args.push_back(S);
  230. if (Opts.PrintCmdAndExit || Opts.BeVerbose) {
  231. for (const auto &A : Args) {
  232. outs() << " ";
  233. sys::printArg(outs(), A, Opts.PrintCmdAndExit);
  234. }
  235. outs() << "\n";
  236. if (Opts.PrintCmdAndExit)
  237. exit(0);
  238. }
  239. // The llvm Support classes don't handle reading from stdout of a child
  240. // process; otherwise we could avoid using a temp file.
  241. int Res = sys::ExecuteAndWait(Clang, Args);
  242. if (Res) {
  243. fatalError("llvm-rc: Preprocessing failed.");
  244. }
  245. return true;
  246. }
  247. static std::pair<bool, std::string> isWindres(llvm::StringRef Argv0) {
  248. StringRef ProgName = llvm::sys::path::stem(Argv0);
  249. // x86_64-w64-mingw32-windres -> x86_64-w64-mingw32, windres
  250. // llvm-rc -> "", llvm-rc
  251. // aarch64-w64-mingw32-llvm-windres-10.exe -> aarch64-w64-mingw32, llvm-windres
  252. ProgName = ProgName.rtrim("0123456789.-");
  253. if (!ProgName.consume_back_insensitive("windres"))
  254. return std::make_pair<bool, std::string>(false, "");
  255. ProgName.consume_back_insensitive("llvm-");
  256. ProgName.consume_back_insensitive("-");
  257. return std::make_pair<bool, std::string>(true, ProgName.str());
  258. }
  259. Format parseFormat(StringRef S) {
  260. Format F = StringSwitch<Format>(S.lower())
  261. .Case("rc", Rc)
  262. .Case("res", Res)
  263. .Case("coff", Coff)
  264. .Default(Unknown);
  265. if (F == Unknown)
  266. fatalError("Unable to parse '" + Twine(S) + "' as a format");
  267. return F;
  268. }
  269. void deduceFormat(Format &Dest, StringRef File) {
  270. Format F = StringSwitch<Format>(sys::path::extension(File.lower()))
  271. .Case(".rc", Rc)
  272. .Case(".res", Res)
  273. .Case(".o", Coff)
  274. .Case(".obj", Coff)
  275. .Default(Unknown);
  276. if (F != Unknown)
  277. Dest = F;
  278. }
  279. std::string unescape(StringRef S) {
  280. std::string Out;
  281. Out.reserve(S.size());
  282. for (int I = 0, E = S.size(); I < E; I++) {
  283. if (S[I] == '\\') {
  284. if (I + 1 < E)
  285. Out.push_back(S[++I]);
  286. else
  287. fatalError("Unterminated escape");
  288. continue;
  289. }
  290. Out.push_back(S[I]);
  291. }
  292. return Out;
  293. }
  294. std::vector<std::string> unescapeSplit(StringRef S) {
  295. std::vector<std::string> OutArgs;
  296. std::string Out;
  297. bool InQuote = false;
  298. for (int I = 0, E = S.size(); I < E; I++) {
  299. if (S[I] == '\\') {
  300. if (I + 1 < E)
  301. Out.push_back(S[++I]);
  302. else
  303. fatalError("Unterminated escape");
  304. continue;
  305. }
  306. if (S[I] == '"') {
  307. InQuote = !InQuote;
  308. continue;
  309. }
  310. if (S[I] == ' ' && !InQuote) {
  311. OutArgs.push_back(Out);
  312. Out.clear();
  313. continue;
  314. }
  315. Out.push_back(S[I]);
  316. }
  317. if (InQuote)
  318. fatalError("Unterminated quote");
  319. if (!Out.empty())
  320. OutArgs.push_back(Out);
  321. return OutArgs;
  322. }
  323. RcOptions parseWindresOptions(ArrayRef<const char *> ArgsArr,
  324. ArrayRef<const char *> InputArgsArray,
  325. std::string Prefix) {
  326. WindresOptTable T;
  327. RcOptions Opts;
  328. unsigned MAI, MAC;
  329. opt::InputArgList InputArgs = T.ParseArgs(ArgsArr, MAI, MAC);
  330. // The tool prints nothing when invoked with no command-line arguments.
  331. if (InputArgs.hasArg(WINDRES_help)) {
  332. T.printHelp(outs(), "windres [options] file...",
  333. "LLVM windres (GNU windres compatible)", false, true);
  334. exit(0);
  335. }
  336. if (InputArgs.hasArg(WINDRES_version)) {
  337. outs() << "llvm-windres, compatible with GNU windres\n";
  338. cl::PrintVersionMessage();
  339. exit(0);
  340. }
  341. std::vector<std::string> FileArgs = InputArgs.getAllArgValues(WINDRES_INPUT);
  342. FileArgs.insert(FileArgs.end(), InputArgsArray.begin(), InputArgsArray.end());
  343. if (InputArgs.hasArg(WINDRES_input)) {
  344. Opts.InputFile = InputArgs.getLastArgValue(WINDRES_input).str();
  345. } else if (!FileArgs.empty()) {
  346. Opts.InputFile = FileArgs.front();
  347. FileArgs.erase(FileArgs.begin());
  348. } else {
  349. // TODO: GNU windres takes input on stdin in this case.
  350. fatalError("Missing input file");
  351. }
  352. if (InputArgs.hasArg(WINDRES_output)) {
  353. Opts.OutputFile = InputArgs.getLastArgValue(WINDRES_output).str();
  354. } else if (!FileArgs.empty()) {
  355. Opts.OutputFile = FileArgs.front();
  356. FileArgs.erase(FileArgs.begin());
  357. } else {
  358. // TODO: GNU windres writes output in rc form to stdout in this case.
  359. fatalError("Missing output file");
  360. }
  361. if (InputArgs.hasArg(WINDRES_input_format)) {
  362. Opts.InputFormat =
  363. parseFormat(InputArgs.getLastArgValue(WINDRES_input_format));
  364. } else {
  365. deduceFormat(Opts.InputFormat, Opts.InputFile);
  366. }
  367. if (Opts.InputFormat == Coff)
  368. fatalError("Unsupported input format");
  369. if (InputArgs.hasArg(WINDRES_output_format)) {
  370. Opts.OutputFormat =
  371. parseFormat(InputArgs.getLastArgValue(WINDRES_output_format));
  372. } else {
  373. // The default in windres differs from the default in RcOptions
  374. Opts.OutputFormat = Coff;
  375. deduceFormat(Opts.OutputFormat, Opts.OutputFile);
  376. }
  377. if (Opts.OutputFormat == Rc)
  378. fatalError("Unsupported output format");
  379. if (Opts.InputFormat == Opts.OutputFormat) {
  380. outs() << "Nothing to do.\n";
  381. exit(0);
  382. }
  383. Opts.PrintCmdAndExit = InputArgs.hasArg(WINDRES__HASH_HASH_HASH);
  384. Opts.Preprocess = !InputArgs.hasArg(WINDRES_no_preprocess);
  385. Triple TT(Prefix);
  386. if (InputArgs.hasArg(WINDRES_target)) {
  387. StringRef Value = InputArgs.getLastArgValue(WINDRES_target);
  388. if (Value == "pe-i386")
  389. Opts.Triple = "i686-w64-mingw32";
  390. else if (Value == "pe-x86-64")
  391. Opts.Triple = "x86_64-w64-mingw32";
  392. else
  393. // Implicit extension; if the --target value isn't one of the known
  394. // BFD targets, allow setting the full triple string via this instead.
  395. Opts.Triple = Value.str();
  396. } else if (TT.getArch() != Triple::UnknownArch)
  397. Opts.Triple = Prefix;
  398. else
  399. Opts.Triple = getMingwTriple();
  400. for (const auto *Arg :
  401. InputArgs.filtered(WINDRES_include_dir, WINDRES_define, WINDRES_undef,
  402. WINDRES_preprocessor_arg)) {
  403. // GNU windres passes the arguments almost as-is on to popen() (it only
  404. // backslash escapes spaces in the arguments), where a shell would
  405. // unescape backslash escapes for quotes and similar. This means that
  406. // when calling GNU windres, callers need to double escape chars like
  407. // quotes, e.g. as -DSTRING=\\\"1.2.3\\\".
  408. //
  409. // Exactly how the arguments are interpreted depends on the platform
  410. // though - but the cases where this matters (where callers would have
  411. // done this double escaping) probably is confined to cases like these
  412. // quoted string defines, and those happen to work the same across unix
  413. // and windows.
  414. std::string Unescaped = unescape(Arg->getValue());
  415. switch (Arg->getOption().getID()) {
  416. case WINDRES_include_dir:
  417. // Technically, these are handled the same way as e.g. defines, but
  418. // the way we consistently unescape the unix way breaks windows paths
  419. // with single backslashes. Alternatively, our unescape function would
  420. // need to mimic the platform specific command line parsing/unescaping
  421. // logic.
  422. Opts.Params.Include.push_back(Arg->getValue());
  423. Opts.PreprocessArgs.push_back("-I");
  424. Opts.PreprocessArgs.push_back(Arg->getValue());
  425. break;
  426. case WINDRES_define:
  427. Opts.PreprocessArgs.push_back("-D");
  428. Opts.PreprocessArgs.push_back(Unescaped);
  429. break;
  430. case WINDRES_undef:
  431. Opts.PreprocessArgs.push_back("-U");
  432. Opts.PreprocessArgs.push_back(Unescaped);
  433. break;
  434. case WINDRES_preprocessor_arg:
  435. Opts.PreprocessArgs.push_back(Unescaped);
  436. break;
  437. }
  438. }
  439. if (InputArgs.hasArg(WINDRES_preprocessor))
  440. Opts.PreprocessCmd =
  441. unescapeSplit(InputArgs.getLastArgValue(WINDRES_preprocessor));
  442. Opts.Params.CodePage = CpWin1252; // Different default
  443. if (InputArgs.hasArg(WINDRES_codepage)) {
  444. if (InputArgs.getLastArgValue(WINDRES_codepage)
  445. .getAsInteger(0, Opts.Params.CodePage))
  446. fatalError("Invalid code page: " +
  447. InputArgs.getLastArgValue(WINDRES_codepage));
  448. }
  449. if (InputArgs.hasArg(WINDRES_language)) {
  450. StringRef Val = InputArgs.getLastArgValue(WINDRES_language);
  451. Val.consume_front_insensitive("0x");
  452. if (Val.getAsInteger(16, Opts.LangId))
  453. fatalError("Invalid language id: " +
  454. InputArgs.getLastArgValue(WINDRES_language));
  455. }
  456. Opts.BeVerbose = InputArgs.hasArg(WINDRES_verbose);
  457. return Opts;
  458. }
  459. RcOptions parseRcOptions(ArrayRef<const char *> ArgsArr,
  460. ArrayRef<const char *> InputArgsArray) {
  461. RcOptTable T;
  462. RcOptions Opts;
  463. unsigned MAI, MAC;
  464. opt::InputArgList InputArgs = T.ParseArgs(ArgsArr, MAI, MAC);
  465. // The tool prints nothing when invoked with no command-line arguments.
  466. if (InputArgs.hasArg(OPT_help)) {
  467. T.printHelp(outs(), "rc [options] file...", "Resource Converter", false);
  468. exit(0);
  469. }
  470. std::vector<std::string> InArgsInfo = InputArgs.getAllArgValues(OPT_INPUT);
  471. InArgsInfo.insert(InArgsInfo.end(), InputArgsArray.begin(),
  472. InputArgsArray.end());
  473. if (InArgsInfo.size() != 1) {
  474. fatalError("Exactly one input file should be provided.");
  475. }
  476. Opts.PrintCmdAndExit = InputArgs.hasArg(OPT__HASH_HASH_HASH);
  477. Opts.Triple = getClangClTriple();
  478. for (const auto *Arg :
  479. InputArgs.filtered(OPT_includepath, OPT_define, OPT_undef)) {
  480. switch (Arg->getOption().getID()) {
  481. case OPT_includepath:
  482. Opts.PreprocessArgs.push_back("-I");
  483. break;
  484. case OPT_define:
  485. Opts.PreprocessArgs.push_back("-D");
  486. break;
  487. case OPT_undef:
  488. Opts.PreprocessArgs.push_back("-U");
  489. break;
  490. }
  491. Opts.PreprocessArgs.push_back(Arg->getValue());
  492. }
  493. Opts.InputFile = InArgsInfo[0];
  494. Opts.BeVerbose = InputArgs.hasArg(OPT_verbose);
  495. Opts.Preprocess = !InputArgs.hasArg(OPT_no_preprocess);
  496. Opts.Params.Include = InputArgs.getAllArgValues(OPT_includepath);
  497. Opts.Params.NoInclude = InputArgs.hasArg(OPT_noinclude);
  498. if (Opts.Params.NoInclude) {
  499. // Clear the INLCUDE variable for the external preprocessor
  500. #ifdef _WIN32
  501. ::_putenv("INCLUDE=");
  502. #else
  503. ::unsetenv("INCLUDE");
  504. #endif
  505. }
  506. if (InputArgs.hasArg(OPT_codepage)) {
  507. if (InputArgs.getLastArgValue(OPT_codepage)
  508. .getAsInteger(10, Opts.Params.CodePage))
  509. fatalError("Invalid code page: " +
  510. InputArgs.getLastArgValue(OPT_codepage));
  511. }
  512. Opts.IsDryRun = InputArgs.hasArg(OPT_dry_run);
  513. auto OutArgsInfo = InputArgs.getAllArgValues(OPT_fileout);
  514. if (OutArgsInfo.empty()) {
  515. SmallString<128> OutputFile(Opts.InputFile);
  516. llvm::sys::fs::make_absolute(OutputFile);
  517. llvm::sys::path::replace_extension(OutputFile, "res");
  518. OutArgsInfo.push_back(std::string(OutputFile.str()));
  519. }
  520. if (!Opts.IsDryRun) {
  521. if (OutArgsInfo.size() != 1)
  522. fatalError(
  523. "No more than one output file should be provided (using /FO flag).");
  524. Opts.OutputFile = OutArgsInfo[0];
  525. }
  526. Opts.AppendNull = InputArgs.hasArg(OPT_add_null);
  527. if (InputArgs.hasArg(OPT_lang_id)) {
  528. StringRef Val = InputArgs.getLastArgValue(OPT_lang_id);
  529. Val.consume_front_insensitive("0x");
  530. if (Val.getAsInteger(16, Opts.LangId))
  531. fatalError("Invalid language id: " +
  532. InputArgs.getLastArgValue(OPT_lang_id));
  533. }
  534. return Opts;
  535. }
  536. RcOptions getOptions(const char *Argv0, ArrayRef<const char *> ArgsArr,
  537. ArrayRef<const char *> InputArgs) {
  538. std::string Prefix;
  539. bool IsWindres;
  540. std::tie(IsWindres, Prefix) = isWindres(Argv0);
  541. if (IsWindres)
  542. return parseWindresOptions(ArgsArr, InputArgs, Prefix);
  543. else
  544. return parseRcOptions(ArgsArr, InputArgs);
  545. }
  546. void doRc(std::string Src, std::string Dest, RcOptions &Opts,
  547. const char *Argv0) {
  548. std::string PreprocessedFile = Src;
  549. if (Opts.Preprocess) {
  550. std::string OutFile = createTempFile("preproc", "rc");
  551. TempPreprocFile.setFile(OutFile);
  552. if (preprocess(Src, OutFile, Opts, Argv0))
  553. PreprocessedFile = OutFile;
  554. }
  555. // Read and tokenize the input file.
  556. ErrorOr<std::unique_ptr<MemoryBuffer>> File =
  557. MemoryBuffer::getFile(PreprocessedFile);
  558. if (!File) {
  559. fatalError("Error opening file '" + Twine(PreprocessedFile) +
  560. "': " + File.getError().message());
  561. }
  562. std::unique_ptr<MemoryBuffer> FileContents = std::move(*File);
  563. StringRef Contents = FileContents->getBuffer();
  564. std::string FilteredContents = filterCppOutput(Contents);
  565. std::vector<RCToken> Tokens = ExitOnErr(tokenizeRC(FilteredContents));
  566. if (Opts.BeVerbose) {
  567. const Twine TokenNames[] = {
  568. #define TOKEN(Name) #Name,
  569. #define SHORT_TOKEN(Name, Ch) #Name,
  570. #include "ResourceScriptTokenList.def"
  571. };
  572. for (const RCToken &Token : Tokens) {
  573. outs() << TokenNames[static_cast<int>(Token.kind())] << ": "
  574. << Token.value();
  575. if (Token.kind() == RCToken::Kind::Int)
  576. outs() << "; int value = " << Token.intValue();
  577. outs() << "\n";
  578. }
  579. }
  580. WriterParams &Params = Opts.Params;
  581. SmallString<128> InputFile(Src);
  582. llvm::sys::fs::make_absolute(InputFile);
  583. Params.InputFilePath = InputFile;
  584. switch (Params.CodePage) {
  585. case CpAcp:
  586. case CpWin1252:
  587. case CpUtf8:
  588. break;
  589. default:
  590. fatalError("Unsupported code page, only 0, 1252 and 65001 are supported!");
  591. }
  592. std::unique_ptr<ResourceFileWriter> Visitor;
  593. if (!Opts.IsDryRun) {
  594. std::error_code EC;
  595. auto FOut = std::make_unique<raw_fd_ostream>(
  596. Dest, EC, sys::fs::FA_Read | sys::fs::FA_Write);
  597. if (EC)
  598. fatalError("Error opening output file '" + Dest + "': " + EC.message());
  599. Visitor = std::make_unique<ResourceFileWriter>(Params, std::move(FOut));
  600. Visitor->AppendNull = Opts.AppendNull;
  601. ExitOnErr(NullResource().visit(Visitor.get()));
  602. unsigned PrimaryLangId = Opts.LangId & 0x3ff;
  603. unsigned SubLangId = Opts.LangId >> 10;
  604. ExitOnErr(LanguageResource(PrimaryLangId, SubLangId).visit(Visitor.get()));
  605. }
  606. rc::RCParser Parser{std::move(Tokens)};
  607. while (!Parser.isEof()) {
  608. auto Resource = ExitOnErr(Parser.parseSingleResource());
  609. if (Opts.BeVerbose)
  610. Resource->log(outs());
  611. if (!Opts.IsDryRun)
  612. ExitOnErr(Resource->visit(Visitor.get()));
  613. }
  614. // STRINGTABLE resources come at the very end.
  615. if (!Opts.IsDryRun)
  616. ExitOnErr(Visitor->dumpAllStringTables());
  617. }
  618. void doCvtres(std::string Src, std::string Dest, std::string TargetTriple) {
  619. object::WindowsResourceParser Parser;
  620. ErrorOr<std::unique_ptr<MemoryBuffer>> BufferOrErr =
  621. MemoryBuffer::getFile(Src);
  622. if (!BufferOrErr)
  623. fatalError("Error opening file '" + Twine(Src) +
  624. "': " + BufferOrErr.getError().message());
  625. std::unique_ptr<MemoryBuffer> &Buffer = BufferOrErr.get();
  626. std::unique_ptr<object::WindowsResource> Binary =
  627. ExitOnErr(object::WindowsResource::createWindowsResource(
  628. Buffer->getMemBufferRef()));
  629. std::vector<std::string> Duplicates;
  630. ExitOnErr(Parser.parse(Binary.get(), Duplicates));
  631. for (const auto &DupeDiag : Duplicates)
  632. fatalError("Duplicate resources: " + DupeDiag);
  633. Triple T(TargetTriple);
  634. COFF::MachineTypes MachineType;
  635. switch (T.getArch()) {
  636. case Triple::x86:
  637. MachineType = COFF::IMAGE_FILE_MACHINE_I386;
  638. break;
  639. case Triple::x86_64:
  640. MachineType = COFF::IMAGE_FILE_MACHINE_AMD64;
  641. break;
  642. case Triple::arm:
  643. case Triple::thumb:
  644. MachineType = COFF::IMAGE_FILE_MACHINE_ARMNT;
  645. break;
  646. case Triple::aarch64:
  647. MachineType = COFF::IMAGE_FILE_MACHINE_ARM64;
  648. break;
  649. default:
  650. fatalError("Unsupported architecture in target '" + Twine(TargetTriple) +
  651. "'");
  652. }
  653. std::unique_ptr<MemoryBuffer> OutputBuffer =
  654. ExitOnErr(object::writeWindowsResourceCOFF(MachineType, Parser,
  655. /*DateTimeStamp*/ 0));
  656. std::unique_ptr<FileOutputBuffer> FileBuffer =
  657. ExitOnErr(FileOutputBuffer::create(Dest, OutputBuffer->getBufferSize()));
  658. std::copy(OutputBuffer->getBufferStart(), OutputBuffer->getBufferEnd(),
  659. FileBuffer->getBufferStart());
  660. ExitOnErr(FileBuffer->commit());
  661. }
  662. } // anonymous namespace
  663. int llvm_rc_main(int Argc, char **Argv) {
  664. InitLLVM X(Argc, Argv);
  665. ExitOnErr.setBanner("llvm-rc: ");
  666. char **DashDash = std::find_if(Argv + 1, Argv + Argc,
  667. [](StringRef Str) { return Str == "--"; });
  668. ArrayRef<const char *> ArgsArr = ArrayRef(Argv + 1, DashDash);
  669. ArrayRef<const char *> FileArgsArr;
  670. if (DashDash != Argv + Argc)
  671. FileArgsArr = ArrayRef(DashDash + 1, Argv + Argc);
  672. RcOptions Opts = getOptions(Argv[0], ArgsArr, FileArgsArr);
  673. std::string ResFile = Opts.OutputFile;
  674. if (Opts.InputFormat == Rc) {
  675. if (Opts.OutputFormat == Coff) {
  676. ResFile = createTempFile("rc", "res");
  677. TempResFile.setFile(ResFile);
  678. }
  679. doRc(Opts.InputFile, ResFile, Opts, Argv[0]);
  680. } else {
  681. ResFile = Opts.InputFile;
  682. }
  683. if (Opts.OutputFormat == Coff) {
  684. doCvtres(ResFile, Opts.OutputFile, Opts.Triple);
  685. }
  686. return 0;
  687. }