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