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" 19 #include "llvm/Support/FileOutputBuffer.h" 20 #include "llvm/Support/ManagedStatic.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/raw_ostream.h" 26 #include "llvm/WindowsManifest/WindowsManifestMerger.h" 27 28 #include <system_error> 29 30 using namespace llvm; 31 32 namespace { 33 34 enum ID { 35 OPT_INVALID = 0, // This is not an option ID. 36 #define OPTION(PREFIX, NAME, ID, KIND, GROUP, ALIAS, ALIASARGS, FLAGS, PARAM, \ 37 HELPTEXT, METAVAR, VALUES) \ 38 OPT_##ID, 39 #include "Opts.inc" 40 #undef OPTION 41 }; 42 43 #define PREFIX(NAME, VALUE) const char *const NAME[] = VALUE; 44 #include "Opts.inc" 45 #undef PREFIX 46 47 static const opt::OptTable::Info InfoTable[] = { 48 #define OPTION(PREFIX, NAME, ID, KIND, GROUP, ALIAS, ALIASARGS, FLAGS, PARAM, \ 49 HELPTEXT, METAVAR, VALUES) \ 50 { \ 51 PREFIX, NAME, HELPTEXT, \ 52 METAVAR, OPT_##ID, opt::Option::KIND##Class, \ 53 PARAM, FLAGS, OPT_##GROUP, \ 54 OPT_##ALIAS, ALIASARGS, VALUES}, 55 #include "Opts.inc" 56 #undef OPTION 57 }; 58 59 class CvtResOptTable : public opt::OptTable { 60 public: 61 CvtResOptTable() : OptTable(InfoTable, true) {} 62 }; 63 64 static ExitOnError ExitOnErr; 65 } // namespace 66 67 LLVM_ATTRIBUTE_NORETURN void reportError(Twine Msg) { 68 errs() << "llvm-mt error: " << Msg << "\n"; 69 exit(1); 70 } 71 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 88 int main(int argc, const char **argv) { 89 sys::PrintStackTraceOnErrorSignal(argv[0]); 90 PrettyStackTraceProgram X(argc, argv); 91 92 ExitOnErr.setBanner("llvm-mt: "); 93 94 SmallVector<const char *, 256> argv_buf; 95 SpecificBumpPtrAllocator<char> ArgAllocator; 96 ExitOnErr(errorCodeToError(sys::Process::GetArgumentVector( 97 argv_buf, makeArrayRef(argv, argc), ArgAllocator))); 98 llvm_shutdown_obj Y; // Call llvm_shutdown() on exit. 99 100 CvtResOptTable T; 101 unsigned MAI, MAC; 102 ArrayRef<const char *> ArgsArr = makeArrayRef(argv + 1, argc); 103 opt::InputArgList InputArgs = T.ParseArgs(ArgsArr, MAI, MAC); 104 105 for (auto *Arg : InputArgs.filtered(OPT_INPUT)) 106 reportError(Twine("invalid option ") + Arg->getSpelling()); 107 108 for (auto &Arg : InputArgs) { 109 if (Arg->getOption().matches(OPT_unsupported)) { 110 outs() << "llvm-mt: ignoring unsupported '" << Arg->getOption().getName() 111 << "' option\n"; 112 } 113 } 114 115 if (InputArgs.hasArg(OPT_help)) { 116 T.PrintHelp(outs(), "mt", "Manifest Tool", false); 117 return 0; 118 } 119 120 std::vector<std::string> InputFiles = InputArgs.getAllArgValues(OPT_manifest); 121 122 if (InputFiles.size() == 0) { 123 reportError("no input file specified"); 124 } 125 126 StringRef OutputFile; 127 if (InputArgs.hasArg(OPT_out)) { 128 OutputFile = InputArgs.getLastArgValue(OPT_out); 129 } else if (InputFiles.size() == 1) { 130 OutputFile = InputFiles[0]; 131 } else { 132 reportError("no output file specified"); 133 } 134 135 windows_manifest::WindowsManifestMerger Merger; 136 137 for (const auto &File : InputFiles) { 138 ErrorOr<std::unique_ptr<MemoryBuffer>> ManifestOrErr = 139 MemoryBuffer::getFile(File); 140 if (!ManifestOrErr) 141 reportError(File, ManifestOrErr.getError()); 142 MemoryBuffer &Manifest = *ManifestOrErr.get(); 143 error(Merger.merge(Manifest)); 144 } 145 146 std::unique_ptr<MemoryBuffer> OutputBuffer = Merger.getMergedManifest(); 147 if (!OutputBuffer) 148 reportError("empty manifest not written"); 149 Expected<std::unique_ptr<FileOutputBuffer>> FileOrErr = 150 FileOutputBuffer::create(OutputFile, OutputBuffer->getBufferSize()); 151 if (!FileOrErr) 152 reportError(OutputFile, errorToErrorCode(FileOrErr.takeError())); 153 std::unique_ptr<FileOutputBuffer> FileBuffer = std::move(*FileOrErr); 154 std::copy(OutputBuffer->getBufferStart(), OutputBuffer->getBufferEnd(), 155 FileBuffer->getBufferStart()); 156 error(FileBuffer->commit()); 157 return 0; 158 } 159