blob: 9bc9d332ebf587e70519934890ca1043e271fedf [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"
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"
Eric Beckmann36be14c2017-07-26 01:21:55 +000026#include "llvm/WindowsManifest/WindowsManifestMerger.h"
Eric Beckmann82998502017-07-17 21:35:12 +000027
28#include <system_error>
29
30using namespace llvm;
31
32namespace {
33
34enum 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
47static 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
59class CvtResOptTable : public opt::OptTable {
60public:
61 CvtResOptTable() : OptTable(InfoTable, true) {}
62};
63
64static ExitOnError ExitOnErr;
65} // namespace
66
67LLVM_ATTRIBUTE_NORETURN void reportError(Twine Msg) {
68 errs() << "llvm-mt error: " << Msg << "\n";
69 exit(1);
70}
71
Eric Beckmann7d50c3892017-07-20 21:42:04 +000072static void reportError(StringRef Input, std::error_code EC) {
73 reportError(Twine(Input) + ": " + EC.message());
74}
75
76void error(std::error_code EC) {
77 if (EC)
78 reportError(EC.message());
79}
80
81void error(Error EC) {
82 if (EC)
83 handleAllErrors(std::move(EC), [&](const ErrorInfoBase &EI) {
84 reportError(EI.message());
85 });
86}
87
Eric Beckmann94c74872017-07-18 03:37:34 +000088int main(int argc, const char **argv) {
Eric Beckmann94c74872017-07-18 03:37:34 +000089 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)));
Eric Beckmann94c74872017-07-18 03:37:34 +000098 llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
99
Eric Beckmann82998502017-07-17 21:35:12 +0000100 CvtResOptTable T;
101 unsigned MAI, MAC;
Eric Beckmannd782ca3a2017-07-18 03:38:04 +0000102 ArrayRef<const char *> ArgsArr = makeArrayRef(argv + 1, argc);
Eric Beckmann82998502017-07-17 21:35:12 +0000103 opt::InputArgList InputArgs = T.ParseArgs(ArgsArr, MAI, MAC);
104
Eric Beckmann91d8af52017-08-19 00:37:41 +0000105 for (auto *Arg : InputArgs.filtered(OPT_INPUT))
106 reportError(Twine("invalid option ") + Arg->getSpelling());
107
Eric Beckmann82998502017-07-17 21:35:12 +0000108 for (auto &Arg : InputArgs) {
109 if (Arg->getOption().matches(OPT_unsupported)) {
110 outs() << "llvm-mt: ignoring unsupported '" << Arg->getOption().getName()
111 << "' option\n";
Eric Beckmann82998502017-07-17 21:35:12 +0000112 }
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;
Eric Beckmann82998502017-07-17 21:35:12 +0000127 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
Eric Beckmann91d8af52017-08-19 00:37:41 +0000135 windows_manifest::WindowsManifestMerger Merger;
Eric Beckmann7d50c3892017-07-20 21:42:04 +0000136
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 ErrorOr<std::unique_ptr<FileOutputBuffer>> FileOrErr =
150 FileOutputBuffer::create(OutputFile, OutputBuffer->getBufferSize());
151 if (!FileOrErr)
152 reportError(OutputFile, FileOrErr.getError());
153 std::unique_ptr<FileOutputBuffer> FileBuffer = std::move(*FileOrErr);
154 std::copy(OutputBuffer->getBufferStart(), OutputBuffer->getBufferEnd(),
155 FileBuffer->getBufferStart());
156 error(FileBuffer->commit());
Eric Beckmann82998502017-07-17 21:35:12 +0000157 return 0;
158}