blob: ae7731b03b077535844326d0a88ed274a6fdcca9 [file] [log] [blame]
Peter Collingbourned5395fb2012-01-08 22:09:58 +00001#include "llvm/ADT/OwningPtr.h"
2#include "llvm/Bitcode/ReaderWriter.h"
3#include "llvm/Function.h"
4#include "llvm/GlobalVariable.h"
5#include "llvm/LLVMContext.h"
6#include "llvm/Module.h"
7#include "llvm/Support/CommandLine.h"
8#include "llvm/Support/ManagedStatic.h"
9#include "llvm/Support/MemoryBuffer.h"
10#include "llvm/Support/raw_ostream.h"
11#include "llvm/Support/system_error.h"
12#include "llvm/Support/ToolOutputFile.h"
13
14using namespace llvm;
15
16static cl::opt<std::string>
17InputFilename(cl::Positional, cl::desc("<input bitcode>"), cl::init("-"));
18
19static cl::opt<std::string>
20OutputFilename("o", cl::desc("Output filename"),
21 cl::value_desc("filename"));
22
23int main(int argc, char **argv) {
24 LLVMContext &Context = getGlobalContext();
25 llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
26
27 cl::ParseCommandLineOptions(argc, argv, "libclc builtin preparation tool\n");
28
29 std::string ErrorMessage;
30 std::auto_ptr<Module> M;
31
32 {
33 OwningPtr<MemoryBuffer> BufferPtr;
34 if (error_code ec = MemoryBuffer::getFileOrSTDIN(InputFilename, BufferPtr))
35 ErrorMessage = ec.message();
36 else
37 M.reset(ParseBitcodeFile(BufferPtr.get(), Context, &ErrorMessage));
38 }
39
40 if (M.get() == 0) {
41 errs() << argv[0] << ": ";
42 if (ErrorMessage.size())
43 errs() << ErrorMessage << "\n";
44 else
45 errs() << "bitcode didn't read correctly.\n";
46 return 1;
47 }
48
49 // Set linkage of every external definition to linkonce_odr.
50 for (Module::iterator i = M->begin(), e = M->end(); i != e; ++i) {
51 if (!i->isDeclaration() && i->getLinkage() == GlobalValue::ExternalLinkage)
52 i->setLinkage(GlobalValue::LinkOnceODRLinkage);
53 }
54
55 for (Module::global_iterator i = M->global_begin(), e = M->global_end();
56 i != e; ++i) {
57 if (!i->isDeclaration() && i->getLinkage() == GlobalValue::ExternalLinkage)
58 i->setLinkage(GlobalValue::LinkOnceODRLinkage);
59 }
60
61 if (OutputFilename.empty()) {
62 errs() << "no output file\n";
63 return 1;
64 }
65
66 std::string ErrorInfo;
67 OwningPtr<tool_output_file> Out
68 (new tool_output_file(OutputFilename.c_str(), ErrorInfo,
69 raw_fd_ostream::F_Binary));
70 if (!ErrorInfo.empty()) {
71 errs() << ErrorInfo << '\n';
72 exit(1);
73 }
74
75 WriteBitcodeToFile(M.get(), Out->os());
76
77 // Declare success.
78 Out->keep();
79 return 0;
80}
81