blob: f95745e14f177e8803786639392120ffb1665d8d [file] [log] [blame]
Eric Beckmann82998502017-07-17 21:35:12 +00001//===- 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 Beckmann7d50c3892017-07-20 21:42:04 +000019#include "llvm/Support/FileOutputBuffer.h"
Eric Beckmann82998502017-07-17 21:35:12 +000020#include "llvm/Support/ManagedStatic.h"
Zachary Turnerbd159d32017-11-17 01:00:35 +000021#include "llvm/Support/MemoryBuffer.h"
Eric Beckmann82998502017-07-17 21:35:12 +000022#include "llvm/Support/Path.h"
23#include "llvm/Support/PrettyStackTrace.h"
24#include "llvm/Support/Process.h"
25#include "llvm/Support/Signals.h"
26#include "llvm/Support/raw_ostream.h"
Eric Beckmann36be14c2017-07-26 01:21:55 +000027#include "llvm/WindowsManifest/WindowsManifestMerger.h"
Eric Beckmann82998502017-07-17 21:35:12 +000028
29#include <system_error>
30
31using namespace llvm;
32
33namespace {
34
35enum 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
48static 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
60class CvtResOptTable : public opt::OptTable {
61public:
62 CvtResOptTable() : OptTable(InfoTable, true) {}
63};
64
65static ExitOnError ExitOnErr;
66} // namespace
67
68LLVM_ATTRIBUTE_NORETURN void reportError(Twine Msg) {
69 errs() << "llvm-mt error: " << Msg << "\n";
70 exit(1);
71}
72
Eric Beckmann7d50c3892017-07-20 21:42:04 +000073static void reportError(StringRef Input, std::error_code EC) {
74 reportError(Twine(Input) + ": " + EC.message());
75}
76
77void error(std::error_code EC) {
78 if (EC)
79 reportError(EC.message());
80}
81
82void error(Error EC) {
83 if (EC)
84 handleAllErrors(std::move(EC), [&](const ErrorInfoBase &EI) {
85 reportError(EI.message());
86 });
87}
88
Eric Beckmann94c74872017-07-18 03:37:34 +000089int main(int argc, const char **argv) {
Eric Beckmann94c74872017-07-18 03:37:34 +000090 sys::PrintStackTraceOnErrorSignal(argv[0]);
91 PrettyStackTraceProgram X(argc, argv);
92
93 ExitOnErr.setBanner("llvm-mt: ");
94
95 SmallVector<const char *, 256> argv_buf;
96 SpecificBumpPtrAllocator<char> ArgAllocator;
97 ExitOnErr(errorCodeToError(sys::Process::GetArgumentVector(
98 argv_buf, makeArrayRef(argv, argc), ArgAllocator)));
Eric Beckmann94c74872017-07-18 03:37:34 +000099 llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
100
Eric Beckmann82998502017-07-17 21:35:12 +0000101 CvtResOptTable T;
102 unsigned MAI, MAC;
Eric Beckmannd782ca3a2017-07-18 03:38:04 +0000103 ArrayRef<const char *> ArgsArr = makeArrayRef(argv + 1, argc);
Eric Beckmann82998502017-07-17 21:35:12 +0000104 opt::InputArgList InputArgs = T.ParseArgs(ArgsArr, MAI, MAC);
105
Brian Gesiak7b84de72018-01-05 17:10:39 +0000106 for (auto *Arg : InputArgs.filtered(OPT_INPUT)) {
107 auto ArgString = Arg->getAsString(InputArgs);
108 std::string Diag;
109 raw_string_ostream OS(Diag);
110 OS << "invalid option '" << ArgString << "'";
111
112 std::string Nearest;
113 if (T.findNearest(ArgString, Nearest) < 2)
114 OS << ", did you mean '" << Nearest << "'?";
115
116 reportError(OS.str());
117 }
Eric Beckmann91d8af52017-08-19 00:37:41 +0000118
Eric Beckmann82998502017-07-17 21:35:12 +0000119 for (auto &Arg : InputArgs) {
120 if (Arg->getOption().matches(OPT_unsupported)) {
121 outs() << "llvm-mt: ignoring unsupported '" << Arg->getOption().getName()
122 << "' option\n";
Eric Beckmann82998502017-07-17 21:35:12 +0000123 }
124 }
125
126 if (InputArgs.hasArg(OPT_help)) {
127 T.PrintHelp(outs(), "mt", "Manifest Tool", false);
128 return 0;
129 }
130
131 std::vector<std::string> InputFiles = InputArgs.getAllArgValues(OPT_manifest);
132
133 if (InputFiles.size() == 0) {
134 reportError("no input file specified");
135 }
136
137 StringRef OutputFile;
Eric Beckmann82998502017-07-17 21:35:12 +0000138 if (InputArgs.hasArg(OPT_out)) {
139 OutputFile = InputArgs.getLastArgValue(OPT_out);
140 } else if (InputFiles.size() == 1) {
141 OutputFile = InputFiles[0];
142 } else {
143 reportError("no output file specified");
144 }
145
Eric Beckmann91d8af52017-08-19 00:37:41 +0000146 windows_manifest::WindowsManifestMerger Merger;
Eric Beckmann7d50c3892017-07-20 21:42:04 +0000147
148 for (const auto &File : InputFiles) {
149 ErrorOr<std::unique_ptr<MemoryBuffer>> ManifestOrErr =
150 MemoryBuffer::getFile(File);
151 if (!ManifestOrErr)
152 reportError(File, ManifestOrErr.getError());
153 MemoryBuffer &Manifest = *ManifestOrErr.get();
154 error(Merger.merge(Manifest));
155 }
156
157 std::unique_ptr<MemoryBuffer> OutputBuffer = Merger.getMergedManifest();
158 if (!OutputBuffer)
159 reportError("empty manifest not written");
Rafael Espindolae0df3572017-11-08 01:05:44 +0000160 Expected<std::unique_ptr<FileOutputBuffer>> FileOrErr =
Eric Beckmann7d50c3892017-07-20 21:42:04 +0000161 FileOutputBuffer::create(OutputFile, OutputBuffer->getBufferSize());
162 if (!FileOrErr)
Rafael Espindolae0df3572017-11-08 01:05:44 +0000163 reportError(OutputFile, errorToErrorCode(FileOrErr.takeError()));
Eric Beckmann7d50c3892017-07-20 21:42:04 +0000164 std::unique_ptr<FileOutputBuffer> FileBuffer = std::move(*FileOrErr);
165 std::copy(OutputBuffer->getBufferStart(), OutputBuffer->getBufferEnd(),
166 FileBuffer->getBufferStart());
167 error(FileBuffer->commit());
Eric Beckmann82998502017-07-17 21:35:12 +0000168 return 0;
169}