really fix the windows build.
[oota-llvm.git] / tools / yaml2obj / yaml2obj.cpp
1 //===- yaml2obj - Convert YAML to a binary object file --------------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This program takes a YAML description of an object file and outputs the
11 // binary equivalent.
12 //
13 // This is used for writing tests that require binary files.
14 //
15 //===----------------------------------------------------------------------===//
16
17 #include "yaml2obj.h"
18 #include "llvm/Support/CommandLine.h"
19 #include "llvm/Support/FileSystem.h"
20 #include "llvm/Support/ManagedStatic.h"
21 #include "llvm/Support/MemoryBuffer.h"
22 #include "llvm/Support/PrettyStackTrace.h"
23 #include "llvm/Support/Signals.h"
24 #include "llvm/Support/raw_ostream.h"
25 #include "llvm/Support/system_error.h"
26 #include "llvm/Support/ToolOutputFile.h"
27
28 using namespace llvm;
29
30 static cl::opt<std::string>
31   Input(cl::Positional, cl::desc("<input>"), cl::init("-"));
32
33 // TODO: The "right" way to tell what kind of object file a given YAML file
34 // corresponds to is to look at YAML "tags" (e.g. `!Foo`). Then, different
35 // tags (`!ELF`, `!COFF`, etc.) would be used to discriminate between them.
36 // Interpreting the tags is needed eventually for when writing test cases,
37 // so that we can e.g. have `!Archive` contain a sequence of `!ELF`, and
38 // just Do The Right Thing. However, interpreting these tags and acting on
39 // them appropriately requires some work in the YAML parser and the YAMLIO
40 // library.
41 enum YAMLObjectFormat {
42   YOF_COFF,
43   YOF_ELF
44 };
45
46 cl::opt<YAMLObjectFormat> Format(
47   "format",
48   cl::desc("Interpret input as this type of object file"),
49   cl::values(
50     clEnumValN(YOF_COFF, "coff", "COFF object file format"),
51     clEnumValN(YOF_ELF, "elf", "ELF object file format"),
52   clEnumValEnd));
53
54 static cl::opt<std::string> OutputFilename("o", cl::desc("Output filename"),
55                                            cl::value_desc("filename"));
56
57 int main(int argc, char **argv) {
58   cl::ParseCommandLineOptions(argc, argv);
59   sys::PrintStackTraceOnErrorSignal();
60   PrettyStackTraceProgram X(argc, argv);
61   llvm_shutdown_obj Y;  // Call llvm_shutdown() on exit.
62
63   if (OutputFilename.empty())
64     OutputFilename = "-";
65
66   std::string ErrorInfo;
67   std::unique_ptr<tool_output_file> Out(
68       new tool_output_file(OutputFilename.c_str(), ErrorInfo, sys::fs::F_None));
69   if (!ErrorInfo.empty()) {
70     errs() << ErrorInfo << '\n';
71     return 1;
72   }
73
74   std::unique_ptr<MemoryBuffer> Buf;
75   if (MemoryBuffer::getFileOrSTDIN(Input, Buf))
76     return 1;
77
78   int Res = 1;
79   if (Format == YOF_COFF)
80     Res = yaml2coff(Out->os(), Buf.get());
81   else if (Format == YOF_ELF)
82     Res = yaml2elf(Out->os(), Buf.get());
83   else
84     errs() << "Not yet implemented\n";
85
86   if (Res == 0)
87     Out->keep();
88
89   return Res;
90 }