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