[dsymutil] Introduce exit helper. NFC.
[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 "dsymutil.h"
17 #include "llvm/Support/ManagedStatic.h"
18 #include "llvm/Support/Options.h"
19 #include "llvm/Support/PrettyStackTrace.h"
20 #include "llvm/Support/Signals.h"
21 #include "llvm/Support/raw_ostream.h"
22 #include "llvm/Support/TargetSelect.h"
23 #include <string>
24
25 using namespace llvm::dsymutil;
26
27 namespace {
28 using namespace llvm::cl;
29
30 OptionCategory DsymCategory("Specific Options");
31 static opt<bool> Help("h", desc("Alias for -help"), Hidden);
32 static opt<bool> Version("v", desc("Alias for -version"), Hidden);
33
34 static list<std::string> InputFiles(Positional, OneOrMore,
35                                     desc("<input files>"), cat(DsymCategory));
36
37 static opt<std::string>
38     OutputFileOpt("o",
39                   desc("Specify the output file. default: <input file>.dwarf"),
40                   value_desc("filename"), cat(DsymCategory));
41
42 static opt<std::string> OsoPrependPath(
43     "oso-prepend-path",
44     desc("Specify a directory to prepend to the paths of object files."),
45     value_desc("path"), cat(DsymCategory));
46
47 static opt<bool> Verbose("verbose", desc("Verbosity level"), init(false),
48                          cat(DsymCategory));
49
50 static opt<bool>
51     NoOutput("no-output",
52              desc("Do the link in memory, but do not emit the result file."),
53              init(false), cat(DsymCategory));
54
55 static opt<bool>
56     NoODR("no-odr",
57           desc("Do not use ODR (One Definition Rule) for type uniquing."),
58           init(false), cat(DsymCategory));
59
60 static opt<bool> DumpDebugMap(
61     "dump-debug-map",
62     desc("Parse and dump the debug map to standard output. Not DWARF link "
63          "will take place."),
64     init(false), cat(DsymCategory));
65
66 static opt<bool> InputIsYAMLDebugMap(
67     "y", desc("Treat the input file is a YAML debug map rather than a binary."),
68     init(false), cat(DsymCategory));
69 }
70
71 static std::string getOutputFileName(llvm::StringRef InputFile) {
72   if (OutputFileOpt.empty()) {
73     if (InputFile == "-")
74       return "a.out.dwarf";
75     return (InputFile + ".dwarf").str();
76   }
77   return OutputFileOpt;
78 }
79
80 void llvm::dsymutil::exitDsymutil(int ExitStatus) {
81   exit(ExitStatus);
82 }
83
84 int main(int argc, char **argv) {
85   llvm::sys::PrintStackTraceOnErrorSignal();
86   llvm::PrettyStackTraceProgram StackPrinter(argc, argv);
87   llvm::llvm_shutdown_obj Shutdown;
88   LinkOptions Options;
89
90   HideUnrelatedOptions(DsymCategory);
91   llvm::cl::ParseCommandLineOptions(
92       argc, argv,
93       "manipulate archived DWARF debug symbol files.\n\n"
94       "dsymutil links the DWARF debug information found in the object files\n"
95       "for the executable <input file> by using debug symbols information\n"
96       "contained in its symbol table.\n");
97
98   if (Help)
99     PrintHelpMessage();
100
101   if (Version) {
102     llvm::cl::PrintVersionMessage();
103     return 0;
104   }
105
106   Options.Verbose = Verbose;
107   Options.NoOutput = NoOutput;
108   Options.NoODR = NoODR;
109
110   llvm::InitializeAllTargetInfos();
111   llvm::InitializeAllTargetMCs();
112   llvm::InitializeAllTargets();
113   llvm::InitializeAllAsmPrinters();
114
115   if (InputFiles.size() > 1 && !OutputFileOpt.empty()) {
116     llvm::errs() << "error: cannot use -o with multiple inputs\n";
117     return 1;
118   }
119
120   for (auto &InputFile : InputFiles) {
121     auto DebugMapPtrOrErr =
122         parseDebugMap(InputFile, OsoPrependPath, Verbose, InputIsYAMLDebugMap);
123
124     if (auto EC = DebugMapPtrOrErr.getError()) {
125       llvm::errs() << "error: cannot parse the debug map for \"" << InputFile
126                    << "\": " << EC.message() << '\n';
127       exitDsymutil(1);
128     }
129
130     if (Verbose || DumpDebugMap)
131       (*DebugMapPtrOrErr)->print(llvm::outs());
132
133     if (DumpDebugMap)
134       continue;
135
136     std::string OutputFile = getOutputFileName(InputFile);
137     if (!linkDwarf(OutputFile, **DebugMapPtrOrErr, Options))
138       exitDsymuti(1);
139   }
140
141   exitDsymutil(0);
142 }