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/ManagedStatic.h"
21 #include "llvm/Support/MemoryBuffer.h"
22 #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/WithColor.h"
27 #include "llvm/Support/raw_ostream.h"
28 #include "llvm/WindowsManifest/WindowsManifestMerger.h"
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:
CvtResOptTable()63 CvtResOptTable() : OptTable(InfoTable, true) {}
64 };
65 } // namespace
66
reportError(Twine Msg)67 LLVM_ATTRIBUTE_NORETURN static void reportError(Twine Msg) {
68 WithColor::error(errs(), "llvm-mt") << Msg << '\n';
69 exit(1);
70 }
71
reportError(StringRef Input,std::error_code EC)72 static void reportError(StringRef Input, std::error_code EC) {
73 reportError(Twine(Input) + ": " + EC.message());
74 }
75
error(Error EC)76 static void error(Error EC) {
77 if (EC)
78 handleAllErrors(std::move(EC), [&](const ErrorInfoBase &EI) {
79 reportError(EI.message());
80 });
81 }
82
main(int Argc,const char ** Argv)83 int main(int Argc, const char **Argv) {
84 InitLLVM X(Argc, Argv);
85
86 CvtResOptTable T;
87 unsigned MAI, MAC;
88 ArrayRef<const char *> ArgsArr = makeArrayRef(Argv + 1, Argc - 1);
89 opt::InputArgList InputArgs = T.ParseArgs(ArgsArr, MAI, MAC);
90
91 for (auto *Arg : InputArgs.filtered(OPT_INPUT)) {
92 auto ArgString = Arg->getAsString(InputArgs);
93 std::string Diag;
94 raw_string_ostream OS(Diag);
95 OS << "invalid option '" << ArgString << "'";
96
97 std::string Nearest;
98 if (T.findNearest(ArgString, Nearest) < 2)
99 OS << ", did you mean '" << Nearest << "'?";
100
101 reportError(OS.str());
102 }
103
104 for (auto &Arg : InputArgs) {
105 if (Arg->getOption().matches(OPT_unsupported)) {
106 outs() << "llvm-mt: ignoring unsupported '" << Arg->getOption().getName()
107 << "' option\n";
108 }
109 }
110
111 if (InputArgs.hasArg(OPT_help)) {
112 T.PrintHelp(outs(), "llvm-mt [options] file...", "Manifest Tool", false);
113 return 0;
114 }
115
116 std::vector<std::string> InputFiles = InputArgs.getAllArgValues(OPT_manifest);
117
118 if (InputFiles.size() == 0) {
119 reportError("no input file specified");
120 }
121
122 StringRef OutputFile;
123 if (InputArgs.hasArg(OPT_out)) {
124 OutputFile = InputArgs.getLastArgValue(OPT_out);
125 } else if (InputFiles.size() == 1) {
126 OutputFile = InputFiles[0];
127 } else {
128 reportError("no output file specified");
129 }
130
131 windows_manifest::WindowsManifestMerger Merger;
132
133 for (const auto &File : InputFiles) {
134 ErrorOr<std::unique_ptr<MemoryBuffer>> ManifestOrErr =
135 MemoryBuffer::getFile(File);
136 if (!ManifestOrErr)
137 reportError(File, ManifestOrErr.getError());
138 MemoryBuffer &Manifest = *ManifestOrErr.get();
139 error(Merger.merge(Manifest));
140 }
141
142 std::unique_ptr<MemoryBuffer> OutputBuffer = Merger.getMergedManifest();
143 if (!OutputBuffer)
144 reportError("empty manifest not written");
145 Expected<std::unique_ptr<FileOutputBuffer>> FileOrErr =
146 FileOutputBuffer::create(OutputFile, OutputBuffer->getBufferSize());
147 if (!FileOrErr)
148 reportError(OutputFile, errorToErrorCode(FileOrErr.takeError()));
149 std::unique_ptr<FileOutputBuffer> FileBuffer = std::move(*FileOrErr);
150 std::copy(OutputBuffer->getBufferStart(), OutputBuffer->getBufferEnd(),
151 FileBuffer->getBufferStart());
152 error(FileBuffer->commit());
153 return 0;
154 }
155