1 //===- llvm-mt.cpp - Merge .manifest files ---------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===---------------------------------------------------------------------===//
10 // Merge .manifest files. This is intended to be a platform-independent port
11 // of Microsoft's mt.exe.
13 //===---------------------------------------------------------------------===//
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/InitLLVM.h"
21 #include "llvm/Support/ManagedStatic.h"
22 #include "llvm/Support/MemoryBuffer.h"
23 #include "llvm/Support/Path.h"
24 #include "llvm/Support/PrettyStackTrace.h"
25 #include "llvm/Support/Process.h"
26 #include "llvm/Support/Signals.h"
27 #include "llvm/Support/WithColor.h"
28 #include "llvm/Support/raw_ostream.h"
29 #include "llvm/WindowsManifest/WindowsManifestMerger.h"
31 #include <system_error>
38 OPT_INVALID
= 0, // This is not an option ID.
39 #define OPTION(PREFIX, NAME, ID, KIND, GROUP, ALIAS, ALIASARGS, FLAGS, PARAM, \
40 HELPTEXT, METAVAR, VALUES) \
46 #define PREFIX(NAME, VALUE) const char *const NAME[] = VALUE;
50 static const opt::OptTable::Info InfoTable
[] = {
51 #define OPTION(PREFIX, NAME, ID, KIND, GROUP, ALIAS, ALIASARGS, FLAGS, PARAM, \
52 HELPTEXT, METAVAR, VALUES) \
54 PREFIX, NAME, HELPTEXT, \
55 METAVAR, OPT_##ID, opt::Option::KIND##Class, \
56 PARAM, FLAGS, OPT_##GROUP, \
57 OPT_##ALIAS, ALIASARGS, VALUES},
62 class CvtResOptTable
: public opt::OptTable
{
64 CvtResOptTable() : OptTable(InfoTable
, true) {}
68 LLVM_ATTRIBUTE_NORETURN
void reportError(Twine Msg
) {
69 WithColor::error(errs(), "llvm-mt") << Msg
<< '\n';
73 static void reportError(StringRef Input
, std::error_code EC
) {
74 reportError(Twine(Input
) + ": " + EC
.message());
77 void error(std::error_code EC
) {
79 reportError(EC
.message());
82 void error(Error EC
) {
84 handleAllErrors(std::move(EC
), [&](const ErrorInfoBase
&EI
) {
85 reportError(EI
.message());
89 int main(int Argc
, const char **Argv
) {
90 InitLLVM
X(Argc
, Argv
);
94 ArrayRef
<const char *> ArgsArr
= makeArrayRef(Argv
+ 1, Argc
- 1);
95 opt::InputArgList InputArgs
= T
.ParseArgs(ArgsArr
, MAI
, MAC
);
97 for (auto *Arg
: InputArgs
.filtered(OPT_INPUT
)) {
98 auto ArgString
= Arg
->getAsString(InputArgs
);
100 raw_string_ostream
OS(Diag
);
101 OS
<< "invalid option '" << ArgString
<< "'";
104 if (T
.findNearest(ArgString
, Nearest
) < 2)
105 OS
<< ", did you mean '" << Nearest
<< "'?";
107 reportError(OS
.str());
110 for (auto &Arg
: InputArgs
) {
111 if (Arg
->getOption().matches(OPT_unsupported
)) {
112 outs() << "llvm-mt: ignoring unsupported '" << Arg
->getOption().getName()
117 if (InputArgs
.hasArg(OPT_help
)) {
118 T
.PrintHelp(outs(), "llvm-mt [options] file...", "Manifest Tool", false);
122 std::vector
<std::string
> InputFiles
= InputArgs
.getAllArgValues(OPT_manifest
);
124 if (InputFiles
.size() == 0) {
125 reportError("no input file specified");
128 StringRef OutputFile
;
129 if (InputArgs
.hasArg(OPT_out
)) {
130 OutputFile
= InputArgs
.getLastArgValue(OPT_out
);
131 } else if (InputFiles
.size() == 1) {
132 OutputFile
= InputFiles
[0];
134 reportError("no output file specified");
137 windows_manifest::WindowsManifestMerger Merger
;
139 for (const auto &File
: InputFiles
) {
140 ErrorOr
<std::unique_ptr
<MemoryBuffer
>> ManifestOrErr
=
141 MemoryBuffer::getFile(File
);
143 reportError(File
, ManifestOrErr
.getError());
144 MemoryBuffer
&Manifest
= *ManifestOrErr
.get();
145 error(Merger
.merge(Manifest
));
148 std::unique_ptr
<MemoryBuffer
> OutputBuffer
= Merger
.getMergedManifest();
150 reportError("empty manifest not written");
151 Expected
<std::unique_ptr
<FileOutputBuffer
>> FileOrErr
=
152 FileOutputBuffer::create(OutputFile
, OutputBuffer
->getBufferSize());
154 reportError(OutputFile
, errorToErrorCode(FileOrErr
.takeError()));
155 std::unique_ptr
<FileOutputBuffer
> FileBuffer
= std::move(*FileOrErr
);
156 std::copy(OutputBuffer
->getBufferStart(), OutputBuffer
->getBufferEnd(),
157 FileBuffer
->getBufferStart());
158 error(FileBuffer
->commit());