blob: 726866e2a1ac2e0e6630cbbeaa297a6fe39c64a2 [file] [log] [blame]
Peter Collingbourned5395fb2012-01-08 22:09:58 +00001#include "llvm/Bitcode/ReaderWriter.h"
Tom Stellard976577e2013-06-26 18:20:32 +00002#include "llvm/IR/Function.h"
3#include "llvm/IR/GlobalVariable.h"
4#include "llvm/IR/LLVMContext.h"
5#include "llvm/IR/Module.h"
Peter Collingbourned5395fb2012-01-08 22:09:58 +00006#include "llvm/Support/CommandLine.h"
7#include "llvm/Support/ManagedStatic.h"
8#include "llvm/Support/MemoryBuffer.h"
Tom Stellard98dccb12014-04-30 18:35:20 +00009#include "llvm/Support/FileSystem.h"
Peter Collingbourned5395fb2012-01-08 22:09:58 +000010#include "llvm/Support/raw_ostream.h"
Tom Stellard8a63b152014-01-20 20:28:48 +000011#include "llvm/Support/ErrorOr.h"
Peter Collingbourned5395fb2012-01-08 22:09:58 +000012#include "llvm/Support/ToolOutputFile.h"
Niels Ole Salscheider109ce692014-08-22 22:24:28 +000013#include "llvm/Config/llvm-config.h"
Peter Collingbourned5395fb2012-01-08 22:09:58 +000014
Tom Stellard0ab10342014-06-13 01:30:14 +000015#define LLVM_350_AND_NEWER \
16 (LLVM_VERSION_MAJOR > 3 || (LLVM_VERSION_MAJOR == 3 && LLVM_VERSION_MINOR >= 5))
17
18#if LLVM_350_AND_NEWER
Jeroen Ketemad253e662014-06-21 09:20:31 +000019#include <system_error>
20
Tom Stellard0ab10342014-06-13 01:30:14 +000021#define ERROR_CODE std::error_code
Jeroen Ketemad253e662014-06-21 09:20:31 +000022#define UNIQUE_PTR std::unique_ptr
Tom Stellard0ab10342014-06-13 01:30:14 +000023#else
Jeroen Ketemad253e662014-06-21 09:20:31 +000024#include "llvm/ADT/OwningPtr.h"
25#include "llvm/Support/system_error.h"
26
Tom Stellard0ab10342014-06-13 01:30:14 +000027#define ERROR_CODE error_code
Jeroen Ketemad253e662014-06-21 09:20:31 +000028#define UNIQUE_PTR OwningPtr
Tom Stellard0ab10342014-06-13 01:30:14 +000029#endif
30
Jeroen Ketemad253e662014-06-21 09:20:31 +000031using namespace llvm;
32
Peter Collingbourned5395fb2012-01-08 22:09:58 +000033static cl::opt<std::string>
34InputFilename(cl::Positional, cl::desc("<input bitcode>"), cl::init("-"));
35
36static cl::opt<std::string>
37OutputFilename("o", cl::desc("Output filename"),
38 cl::value_desc("filename"));
39
40int main(int argc, char **argv) {
41 LLVMContext &Context = getGlobalContext();
42 llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
43
44 cl::ParseCommandLineOptions(argc, argv, "libclc builtin preparation tool\n");
45
46 std::string ErrorMessage;
47 std::auto_ptr<Module> M;
48
49 {
Tom Stellard8d92c392014-07-07 17:46:45 +000050#if LLVM_350_AND_NEWER
51 ErrorOr<std::unique_ptr<MemoryBuffer>> BufferOrErr =
52 MemoryBuffer::getFile(InputFilename);
53 std::unique_ptr<MemoryBuffer> &BufferPtr = BufferOrErr.get();
54 if (std::error_code ec = BufferOrErr.getError())
55#else
Jeroen Ketemad253e662014-06-21 09:20:31 +000056 UNIQUE_PTR<MemoryBuffer> BufferPtr;
Tom Stellard0ab10342014-06-13 01:30:14 +000057 if (ERROR_CODE ec = MemoryBuffer::getFileOrSTDIN(InputFilename, BufferPtr))
Tom Stellard8d92c392014-07-07 17:46:45 +000058#endif
Peter Collingbourned5395fb2012-01-08 22:09:58 +000059 ErrorMessage = ec.message();
Tom Stellard8a63b152014-01-20 20:28:48 +000060 else {
61#if LLVM_VERSION_MAJOR > 3 || (LLVM_VERSION_MAJOR == 3 && LLVM_VERSION_MINOR > 4)
62 ErrorOr<Module *> ModuleOrErr = parseBitcodeFile(BufferPtr.get(), Context);
Tom Stellard0ab10342014-06-13 01:30:14 +000063 if (ERROR_CODE ec = ModuleOrErr.getError())
Tom Stellard8a63b152014-01-20 20:28:48 +000064 ErrorMessage = ec.message();
65 M.reset(ModuleOrErr.get());
66#else
Peter Collingbourned5395fb2012-01-08 22:09:58 +000067 M.reset(ParseBitcodeFile(BufferPtr.get(), Context, &ErrorMessage));
Tom Stellard8a63b152014-01-20 20:28:48 +000068#endif
69 }
Peter Collingbourned5395fb2012-01-08 22:09:58 +000070 }
71
72 if (M.get() == 0) {
73 errs() << argv[0] << ": ";
74 if (ErrorMessage.size())
75 errs() << ErrorMessage << "\n";
76 else
77 errs() << "bitcode didn't read correctly.\n";
78 return 1;
79 }
80
81 // Set linkage of every external definition to linkonce_odr.
82 for (Module::iterator i = M->begin(), e = M->end(); i != e; ++i) {
83 if (!i->isDeclaration() && i->getLinkage() == GlobalValue::ExternalLinkage)
84 i->setLinkage(GlobalValue::LinkOnceODRLinkage);
85 }
86
87 for (Module::global_iterator i = M->global_begin(), e = M->global_end();
88 i != e; ++i) {
89 if (!i->isDeclaration() && i->getLinkage() == GlobalValue::ExternalLinkage)
90 i->setLinkage(GlobalValue::LinkOnceODRLinkage);
91 }
92
93 if (OutputFilename.empty()) {
94 errs() << "no output file\n";
95 return 1;
96 }
97
98 std::string ErrorInfo;
Jeroen Ketemad253e662014-06-21 09:20:31 +000099 UNIQUE_PTR<tool_output_file> Out
Peter Collingbourned5395fb2012-01-08 22:09:58 +0000100 (new tool_output_file(OutputFilename.c_str(), ErrorInfo,
Tom Stellard7aee1cf2014-02-24 21:31:56 +0000101#if (LLVM_VERSION_MAJOR == 3 && LLVM_VERSION_MINOR == 4)
Aaron Watry0da3d3b52013-07-18 21:24:35 +0000102 sys::fs::F_Binary));
Tom Stellard7aee1cf2014-02-24 21:31:56 +0000103#elif LLVM_VERSION_MAJOR > 3 || (LLVM_VERSION_MAJOR == 3 && LLVM_VERSION_MINOR >= 5)
104 sys::fs::F_None));
Aaron Watry0da3d3b52013-07-18 21:24:35 +0000105#else
Peter Collingbourned5395fb2012-01-08 22:09:58 +0000106 raw_fd_ostream::F_Binary));
Aaron Watry0da3d3b52013-07-18 21:24:35 +0000107#endif
Peter Collingbourned5395fb2012-01-08 22:09:58 +0000108 if (!ErrorInfo.empty()) {
109 errs() << ErrorInfo << '\n';
110 exit(1);
111 }
112
113 WriteBitcodeToFile(M.get(), Out->os());
114
115 // Declare success.
116 Out->keep();
117 return 0;
118}
119