Initial dsymutil tool commit.
[oota-llvm.git] / tools / dsymutil / dsymutil.cpp
1 //===-- dsymutil.cpp - Debug info dumping utility for llvm ----------------===//
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 is a utility that aims to be a dropin replacement for
11 // Darwin's dsymutil.
12 //
13 //===----------------------------------------------------------------------===//
14
15 #include "DebugMap.h"
16 #include "DwarfLinker.h"
17 #include "MachODebugMapParser.h"
18
19 #include "llvm/Support/ManagedStatic.h"
20 #include "llvm/Support/PrettyStackTrace.h"
21 #include "llvm/Support/Options.h"
22 #include "llvm/Support/raw_ostream.h"
23 #include "llvm/Support/Signals.h"
24
25 #include <string>
26
27 static llvm::cl::opt<std::string> InputFile(llvm::cl::Positional,
28                                             llvm::cl::desc("<input file>"),
29                                             llvm::cl::init("-"));
30
31 static llvm::cl::opt<std::string> OsoPrependPath("oso-prepend-path",
32                                                  llvm::cl::desc("<path>"));
33
34 static llvm::cl::opt<bool> Verbose("v", llvm::cl::desc("Verbosity level"),
35                                    llvm::cl::init(false));
36
37 static llvm::cl::opt<bool> ParseOnly("parse-only",
38                                      llvm::cl::desc("Only parse the debug map, do "
39                                                     "not actaully link the DWARF."),
40                                      llvm::cl::init(false));
41
42 int main(int argc, char **argv) {
43   llvm::sys::PrintStackTraceOnErrorSignal();
44   llvm::PrettyStackTraceProgram StackPrinter(argc, argv);
45   llvm::llvm_shutdown_obj Shutdown;
46
47   llvm::cl::ParseCommandLineOptions(argc, argv, "llvm dsymutil\n");
48
49   llvm::MachODebugMapParser Parser(InputFile);
50   Parser.setPreprendPath(OsoPrependPath);
51   llvm::ErrorOr<std::unique_ptr<llvm::DebugMap>> DebugMap = Parser.parse();
52
53   if (auto EC = DebugMap.getError()) {
54     llvm::errs() << "error: cannot parse the debug map for \"" << InputFile <<
55       "\": " << EC.message() << '\n';
56     return 1;
57   }
58
59   if (Verbose)
60     (*DebugMap)->print(llvm::outs());
61
62   if (ParseOnly)
63     return 0;
64
65   llvm::DwarfLinker Linker(InputFile + ".dwarf");
66   return !Linker.link(*DebugMap.get());
67 }