1 //===- yaml2obj - Convert YAML to a binary object file --------------------===//
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 // This program takes a YAML description of an object file and outputs the
12 // This is used for writing tests that require binary files.
14 //===----------------------------------------------------------------------===//
16 #include "llvm/ObjectYAML/yaml2obj.h"
17 #include "llvm/ADT/StringExtras.h"
18 #include "llvm/ObjectYAML/ObjectYAML.h"
19 #include "llvm/Support/CommandLine.h"
20 #include "llvm/Support/FileSystem.h"
21 #include "llvm/Support/InitLLVM.h"
22 #include "llvm/Support/MemoryBuffer.h"
23 #include "llvm/Support/ToolOutputFile.h"
24 #include "llvm/Support/WithColor.h"
25 #include "llvm/Support/YAMLTraits.h"
26 #include "llvm/Support/raw_ostream.h"
27 #include <system_error>
31 static cl::opt
<std::string
>
32 Input(cl::Positional
, cl::desc("<input>"), cl::init("-"));
35 DocNum("docnum", cl::init(1),
36 cl::desc("Read specified document from input (default = 1)"));
38 static cl::opt
<std::string
> OutputFilename("o", cl::desc("Output filename"),
39 cl::value_desc("filename"));
41 int main(int argc
, char **argv
) {
42 InitLLVM
X(argc
, argv
);
43 cl::ParseCommandLineOptions(argc
, argv
);
45 if (OutputFilename
.empty())
48 auto ErrHandler
= [](const Twine
&Msg
) {
49 WithColor::error(errs(), "yaml2obj") << Msg
<< "\n";
53 std::unique_ptr
<ToolOutputFile
> Out(
54 new ToolOutputFile(OutputFilename
, EC
, sys::fs::OF_None
));
56 ErrHandler("failed to open '" + OutputFilename
+ "': " + EC
.message());
60 ErrorOr
<std::unique_ptr
<MemoryBuffer
>> Buf
=
61 MemoryBuffer::getFileOrSTDIN(Input
);
65 yaml::Input
YIn(Buf
.get()->getBuffer());
66 if (!convertYAML(YIn
, Out
->os(), ErrHandler
, DocNum
))