blob: afa64a8b93c684bd2badc2021389158746e53caa [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
Michel Danzera10b4922014-08-28 06:19:33 +000015#define LLVM_360_AND_NEWER \
16 (LLVM_VERSION_MAJOR > 3 || (LLVM_VERSION_MAJOR == 3 && LLVM_VERSION_MINOR >= 6))
17
Tom Stellard0ab10342014-06-13 01:30:14 +000018#define LLVM_350_AND_NEWER \
19 (LLVM_VERSION_MAJOR > 3 || (LLVM_VERSION_MAJOR == 3 && LLVM_VERSION_MINOR >= 5))
20
21#if LLVM_350_AND_NEWER
Jeroen Ketemad253e662014-06-21 09:20:31 +000022#include <system_error>
23
Tom Stellard0ab10342014-06-13 01:30:14 +000024#define ERROR_CODE std::error_code
Jeroen Ketemad253e662014-06-21 09:20:31 +000025#define UNIQUE_PTR std::unique_ptr
Tom Stellard0ab10342014-06-13 01:30:14 +000026#else
Jeroen Ketemad253e662014-06-21 09:20:31 +000027#include "llvm/ADT/OwningPtr.h"
28#include "llvm/Support/system_error.h"
29
Tom Stellard0ab10342014-06-13 01:30:14 +000030#define ERROR_CODE error_code
Jeroen Ketemad253e662014-06-21 09:20:31 +000031#define UNIQUE_PTR OwningPtr
Tom Stellard0ab10342014-06-13 01:30:14 +000032#endif
33
Jeroen Ketemad253e662014-06-21 09:20:31 +000034using namespace llvm;
35
Peter Collingbourned5395fb2012-01-08 22:09:58 +000036static cl::opt<std::string>
37InputFilename(cl::Positional, cl::desc("<input bitcode>"), cl::init("-"));
38
39static cl::opt<std::string>
40OutputFilename("o", cl::desc("Output filename"),
41 cl::value_desc("filename"));
42
43int main(int argc, char **argv) {
44 LLVMContext &Context = getGlobalContext();
45 llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
46
47 cl::ParseCommandLineOptions(argc, argv, "libclc builtin preparation tool\n");
48
49 std::string ErrorMessage;
50 std::auto_ptr<Module> M;
51
52 {
Tom Stellard8d92c392014-07-07 17:46:45 +000053#if LLVM_350_AND_NEWER
54 ErrorOr<std::unique_ptr<MemoryBuffer>> BufferOrErr =
55 MemoryBuffer::getFile(InputFilename);
56 std::unique_ptr<MemoryBuffer> &BufferPtr = BufferOrErr.get();
57 if (std::error_code ec = BufferOrErr.getError())
58#else
Jeroen Ketemad253e662014-06-21 09:20:31 +000059 UNIQUE_PTR<MemoryBuffer> BufferPtr;
Tom Stellard0ab10342014-06-13 01:30:14 +000060 if (ERROR_CODE ec = MemoryBuffer::getFileOrSTDIN(InputFilename, BufferPtr))
Tom Stellard8d92c392014-07-07 17:46:45 +000061#endif
Peter Collingbourned5395fb2012-01-08 22:09:58 +000062 ErrorMessage = ec.message();
Tom Stellard8a63b152014-01-20 20:28:48 +000063 else {
64#if LLVM_VERSION_MAJOR > 3 || (LLVM_VERSION_MAJOR == 3 && LLVM_VERSION_MINOR > 4)
65 ErrorOr<Module *> ModuleOrErr = parseBitcodeFile(BufferPtr.get(), Context);
Tom Stellard0ab10342014-06-13 01:30:14 +000066 if (ERROR_CODE ec = ModuleOrErr.getError())
Tom Stellard8a63b152014-01-20 20:28:48 +000067 ErrorMessage = ec.message();
68 M.reset(ModuleOrErr.get());
69#else
Peter Collingbourned5395fb2012-01-08 22:09:58 +000070 M.reset(ParseBitcodeFile(BufferPtr.get(), Context, &ErrorMessage));
Tom Stellard8a63b152014-01-20 20:28:48 +000071#endif
72 }
Peter Collingbourned5395fb2012-01-08 22:09:58 +000073 }
74
75 if (M.get() == 0) {
76 errs() << argv[0] << ": ";
77 if (ErrorMessage.size())
78 errs() << ErrorMessage << "\n";
79 else
80 errs() << "bitcode didn't read correctly.\n";
81 return 1;
82 }
83
84 // Set linkage of every external definition to linkonce_odr.
85 for (Module::iterator i = M->begin(), e = M->end(); i != e; ++i) {
86 if (!i->isDeclaration() && i->getLinkage() == GlobalValue::ExternalLinkage)
87 i->setLinkage(GlobalValue::LinkOnceODRLinkage);
88 }
89
90 for (Module::global_iterator i = M->global_begin(), e = M->global_end();
91 i != e; ++i) {
92 if (!i->isDeclaration() && i->getLinkage() == GlobalValue::ExternalLinkage)
93 i->setLinkage(GlobalValue::LinkOnceODRLinkage);
94 }
95
96 if (OutputFilename.empty()) {
97 errs() << "no output file\n";
98 return 1;
99 }
100
Michel Danzera10b4922014-08-28 06:19:33 +0000101#if LLVM_360_AND_NEWER
102 std::error_code EC;
103 UNIQUE_PTR<tool_output_file> Out
104 (new tool_output_file(OutputFilename, EC, sys::fs::F_None));
105 if (EC) {
106 errs() << EC.message() << '\n';
107 exit(1);
108 }
109#else
Peter Collingbourned5395fb2012-01-08 22:09:58 +0000110 std::string ErrorInfo;
Jeroen Ketemad253e662014-06-21 09:20:31 +0000111 UNIQUE_PTR<tool_output_file> Out
Peter Collingbourned5395fb2012-01-08 22:09:58 +0000112 (new tool_output_file(OutputFilename.c_str(), ErrorInfo,
Tom Stellard7aee1cf2014-02-24 21:31:56 +0000113#if (LLVM_VERSION_MAJOR == 3 && LLVM_VERSION_MINOR == 4)
Aaron Watry0da3d3b52013-07-18 21:24:35 +0000114 sys::fs::F_Binary));
Tom Stellard7aee1cf2014-02-24 21:31:56 +0000115#elif LLVM_VERSION_MAJOR > 3 || (LLVM_VERSION_MAJOR == 3 && LLVM_VERSION_MINOR >= 5)
116 sys::fs::F_None));
Aaron Watry0da3d3b52013-07-18 21:24:35 +0000117#else
Peter Collingbourned5395fb2012-01-08 22:09:58 +0000118 raw_fd_ostream::F_Binary));
Aaron Watry0da3d3b52013-07-18 21:24:35 +0000119#endif
Peter Collingbourned5395fb2012-01-08 22:09:58 +0000120 if (!ErrorInfo.empty()) {
121 errs() << ErrorInfo << '\n';
122 exit(1);
123 }
Michel Danzera10b4922014-08-28 06:19:33 +0000124#endif // LLVM_360_AND_NEWER
Peter Collingbourned5395fb2012-01-08 22:09:58 +0000125
126 WriteBitcodeToFile(M.get(), Out->os());
127
128 // Declare success.
129 Out->keep();
130 return 0;
131}
132