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 LLVM_ATTRIBUTE_NORETURN
static void error(Twine Message
) {
42 errs() << Message
<< "\n";
46 int main(int argc
, char **argv
) {
47 InitLLVM
X(argc
, argv
);
48 cl::ParseCommandLineOptions(argc
, argv
);
50 if (OutputFilename
.empty())
54 std::unique_ptr
<ToolOutputFile
> Out(
55 new ToolOutputFile(OutputFilename
, EC
, sys::fs::OF_None
));
57 error("yaml2obj: Error opening '" + OutputFilename
+ "': " + EC
.message());
59 ErrorOr
<std::unique_ptr
<MemoryBuffer
>> Buf
=
60 MemoryBuffer::getFileOrSTDIN(Input
);
64 yaml::Input
YIn(Buf
.get()->getBuffer());
65 if (Error E
= convertYAML(YIn
, Out
->os(), DocNum
)) {
66 logAllUnhandledErrors(std::move(E
), WithColor::error(errs(), argv
[0]));