llvm-mt.cpp 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. //===- llvm-mt.cpp - Merge .manifest files ---------------------*- 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. // Merge .manifest files. This is intended to be a platform-independent port
  10. // of Microsoft's mt.exe.
  11. //
  12. //===---------------------------------------------------------------------===//
  13. #include "llvm/Option/Arg.h"
  14. #include "llvm/Option/ArgList.h"
  15. #include "llvm/Option/Option.h"
  16. #include "llvm/Support/Error.h"
  17. #include "llvm/Support/FileOutputBuffer.h"
  18. #include "llvm/Support/InitLLVM.h"
  19. #include "llvm/Support/ManagedStatic.h"
  20. #include "llvm/Support/MemoryBuffer.h"
  21. #include "llvm/Support/Path.h"
  22. #include "llvm/Support/PrettyStackTrace.h"
  23. #include "llvm/Support/Process.h"
  24. #include "llvm/Support/Signals.h"
  25. #include "llvm/Support/WithColor.h"
  26. #include "llvm/Support/raw_ostream.h"
  27. #include "llvm/WindowsManifest/WindowsManifestMerger.h"
  28. #include <system_error>
  29. using namespace llvm;
  30. namespace {
  31. enum ID {
  32. OPT_INVALID = 0, // This is not an option ID.
  33. #define OPTION(PREFIX, NAME, ID, KIND, GROUP, ALIAS, ALIASARGS, FLAGS, PARAM, \
  34. HELPTEXT, METAVAR, VALUES) \
  35. OPT_##ID,
  36. #include "Opts.inc"
  37. #undef OPTION
  38. };
  39. #define PREFIX(NAME, VALUE) const char *const NAME[] = VALUE;
  40. #include "Opts.inc"
  41. #undef PREFIX
  42. const opt::OptTable::Info InfoTable[] = {
  43. #define OPTION(PREFIX, NAME, ID, KIND, GROUP, ALIAS, ALIASARGS, FLAGS, PARAM, \
  44. HELPTEXT, METAVAR, VALUES) \
  45. { \
  46. PREFIX, NAME, HELPTEXT, \
  47. METAVAR, OPT_##ID, opt::Option::KIND##Class, \
  48. PARAM, FLAGS, OPT_##GROUP, \
  49. OPT_##ALIAS, ALIASARGS, VALUES},
  50. #include "Opts.inc"
  51. #undef OPTION
  52. };
  53. class CvtResOptTable : public opt::OptTable {
  54. public:
  55. CvtResOptTable() : OptTable(InfoTable, true) {}
  56. };
  57. } // namespace
  58. [[noreturn]] static void reportError(Twine Msg) {
  59. WithColor::error(errs(), "llvm-mt") << Msg << '\n';
  60. exit(1);
  61. }
  62. static void reportError(StringRef Input, std::error_code EC) {
  63. reportError(Twine(Input) + ": " + EC.message());
  64. }
  65. static void error(Error EC) {
  66. if (EC)
  67. handleAllErrors(std::move(EC), [&](const ErrorInfoBase &EI) {
  68. reportError(EI.message());
  69. });
  70. }
  71. int main(int Argc, const char **Argv) {
  72. InitLLVM X(Argc, Argv);
  73. CvtResOptTable T;
  74. unsigned MAI, MAC;
  75. ArrayRef<const char *> ArgsArr = makeArrayRef(Argv + 1, Argc - 1);
  76. opt::InputArgList InputArgs = T.ParseArgs(ArgsArr, MAI, MAC);
  77. for (auto *Arg : InputArgs.filtered(OPT_INPUT)) {
  78. auto ArgString = Arg->getAsString(InputArgs);
  79. std::string Diag;
  80. raw_string_ostream OS(Diag);
  81. OS << "invalid option '" << ArgString << "'";
  82. std::string Nearest;
  83. if (T.findNearest(ArgString, Nearest) < 2)
  84. OS << ", did you mean '" << Nearest << "'?";
  85. reportError(OS.str());
  86. }
  87. for (auto &Arg : InputArgs) {
  88. if (Arg->getOption().matches(OPT_unsupported)) {
  89. outs() << "llvm-mt: ignoring unsupported '" << Arg->getOption().getName()
  90. << "' option\n";
  91. }
  92. }
  93. if (InputArgs.hasArg(OPT_help)) {
  94. T.printHelp(outs(), "llvm-mt [options] file...", "Manifest Tool", false);
  95. return 0;
  96. }
  97. std::vector<std::string> InputFiles = InputArgs.getAllArgValues(OPT_manifest);
  98. if (InputFiles.size() == 0) {
  99. reportError("no input file specified");
  100. }
  101. StringRef OutputFile;
  102. if (InputArgs.hasArg(OPT_out)) {
  103. OutputFile = InputArgs.getLastArgValue(OPT_out);
  104. } else if (InputFiles.size() == 1) {
  105. OutputFile = InputFiles[0];
  106. } else {
  107. reportError("no output file specified");
  108. }
  109. windows_manifest::WindowsManifestMerger Merger;
  110. for (const auto &File : InputFiles) {
  111. ErrorOr<std::unique_ptr<MemoryBuffer>> ManifestOrErr =
  112. MemoryBuffer::getFile(File);
  113. if (!ManifestOrErr)
  114. reportError(File, ManifestOrErr.getError());
  115. error(Merger.merge(*ManifestOrErr.get()));
  116. }
  117. std::unique_ptr<MemoryBuffer> OutputBuffer = Merger.getMergedManifest();
  118. if (!OutputBuffer)
  119. reportError("empty manifest not written");
  120. int ExitCode = 0;
  121. if (InputArgs.hasArg(OPT_notify_update)) {
  122. ErrorOr<std::unique_ptr<MemoryBuffer>> OutBuffOrErr =
  123. MemoryBuffer::getFile(OutputFile);
  124. // Assume if we couldn't open the output file then it doesn't exist meaning
  125. // there was a change.
  126. bool Same = false;
  127. if (OutBuffOrErr) {
  128. const std::unique_ptr<MemoryBuffer> &FileBuffer = *OutBuffOrErr;
  129. Same = std::equal(OutputBuffer->getBufferStart(),
  130. OutputBuffer->getBufferEnd(),
  131. FileBuffer->getBufferStart());
  132. }
  133. if (!Same) {
  134. #if LLVM_ON_UNIX
  135. ExitCode = 0xbb;
  136. #elif defined(_WIN32)
  137. ExitCode = 0x41020001;
  138. #endif
  139. }
  140. }
  141. Expected<std::unique_ptr<FileOutputBuffer>> FileOrErr =
  142. FileOutputBuffer::create(OutputFile, OutputBuffer->getBufferSize());
  143. if (!FileOrErr)
  144. reportError(OutputFile, errorToErrorCode(FileOrErr.takeError()));
  145. std::unique_ptr<FileOutputBuffer> FileBuffer = std::move(*FileOrErr);
  146. std::copy(OutputBuffer->getBufferStart(), OutputBuffer->getBufferEnd(),
  147. FileBuffer->getBufferStart());
  148. error(FileBuffer->commit());
  149. return ExitCode;
  150. }