1 //===- llvm-mt.cpp - Merge .manifest files ---------------------*- C++ -*-===//
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
7 //===---------------------------------------------------------------------===//
9 // Merge .manifest files. This is intended to be a platform-independent port
10 // of Microsoft's mt.exe.
12 //===---------------------------------------------------------------------===//
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"
30 #include <system_error>
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) \
45 #define PREFIX(NAME, VALUE) const char *const NAME[] = VALUE;
49 static const opt::OptTable::Info InfoTable
[] = {
50 #define OPTION(PREFIX, NAME, ID, KIND, GROUP, ALIAS, ALIASARGS, FLAGS, PARAM, \
51 HELPTEXT, METAVAR, VALUES) \
53 PREFIX, NAME, HELPTEXT, \
54 METAVAR, OPT_##ID, opt::Option::KIND##Class, \
55 PARAM, FLAGS, OPT_##GROUP, \
56 OPT_##ALIAS, ALIASARGS, VALUES},
61 class CvtResOptTable
: public opt::OptTable
{
63 CvtResOptTable() : OptTable(InfoTable
, true) {}
67 LLVM_ATTRIBUTE_NORETURN
void reportError(Twine Msg
) {
68 WithColor::error(errs(), "llvm-mt") << Msg
<< '\n';
72 static void reportError(StringRef Input
, std::error_code EC
) {
73 reportError(Twine(Input
) + ": " + EC
.message());
76 void error(std::error_code EC
) {
78 reportError(EC
.message());
81 void error(Error EC
) {
83 handleAllErrors(std::move(EC
), [&](const ErrorInfoBase
&EI
) {
84 reportError(EI
.message());
88 int main(int Argc
, const char **Argv
) {
89 InitLLVM
X(Argc
, Argv
);
93 ArrayRef
<const char *> ArgsArr
= makeArrayRef(Argv
+ 1, Argc
- 1);
94 opt::InputArgList InputArgs
= T
.ParseArgs(ArgsArr
, MAI
, MAC
);
96 for (auto *Arg
: InputArgs
.filtered(OPT_INPUT
)) {
97 auto ArgString
= Arg
->getAsString(InputArgs
);
99 raw_string_ostream
OS(Diag
);
100 OS
<< "invalid option '" << ArgString
<< "'";
103 if (T
.findNearest(ArgString
, Nearest
) < 2)
104 OS
<< ", did you mean '" << Nearest
<< "'?";
106 reportError(OS
.str());
109 for (auto &Arg
: InputArgs
) {
110 if (Arg
->getOption().matches(OPT_unsupported
)) {
111 outs() << "llvm-mt: ignoring unsupported '" << Arg
->getOption().getName()
116 if (InputArgs
.hasArg(OPT_help
)) {
117 T
.PrintHelp(outs(), "llvm-mt [options] file...", "Manifest Tool", false);
121 std::vector
<std::string
> InputFiles
= InputArgs
.getAllArgValues(OPT_manifest
);
123 if (InputFiles
.size() == 0) {
124 reportError("no input file specified");
127 StringRef OutputFile
;
128 if (InputArgs
.hasArg(OPT_out
)) {
129 OutputFile
= InputArgs
.getLastArgValue(OPT_out
);
130 } else if (InputFiles
.size() == 1) {
131 OutputFile
= InputFiles
[0];
133 reportError("no output file specified");
136 windows_manifest::WindowsManifestMerger Merger
;
138 for (const auto &File
: InputFiles
) {
139 ErrorOr
<std::unique_ptr
<MemoryBuffer
>> ManifestOrErr
=
140 MemoryBuffer::getFile(File
);
142 reportError(File
, ManifestOrErr
.getError());
143 MemoryBuffer
&Manifest
= *ManifestOrErr
.get();
144 error(Merger
.merge(Manifest
));
147 std::unique_ptr
<MemoryBuffer
> OutputBuffer
= Merger
.getMergedManifest();
149 reportError("empty manifest not written");
150 Expected
<std::unique_ptr
<FileOutputBuffer
>> FileOrErr
=
151 FileOutputBuffer::create(OutputFile
, OutputBuffer
->getBufferSize());
153 reportError(OutputFile
, errorToErrorCode(FileOrErr
.takeError()));
154 std::unique_ptr
<FileOutputBuffer
> FileBuffer
= std::move(*FileOrErr
);
155 std::copy(OutputBuffer
->getBufferStart(), OutputBuffer
->getBufferEnd(),
156 FileBuffer
->getBufferStart());
157 error(FileBuffer
->commit());