1 #include "llvm/Bitcode/ReaderWriter.h"
2 #include "llvm/IR/Function.h"
3 #include "llvm/IR/GlobalVariable.h"
4 #include "llvm/IR/LLVMContext.h"
5 #include "llvm/IR/Module.h"
6 #include "llvm/Support/CommandLine.h"
7 #include "llvm/Support/ManagedStatic.h"
8 #include "llvm/Support/MemoryBuffer.h"
9 #include "llvm/Support/FileSystem.h"
10 #include "llvm/Support/raw_ostream.h"
11 #include "llvm/Support/ErrorOr.h"
12 #include "llvm/Support/ToolOutputFile.h"
13 #include "llvm/Config/llvm-config.h"
14 
15 #define LLVM_360_AND_NEWER \
16   (LLVM_VERSION_MAJOR > 3 || (LLVM_VERSION_MAJOR == 3 && LLVM_VERSION_MINOR >= 6))
17 
18 #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
22 #include <system_error>
23 
24 #define ERROR_CODE std::error_code
25 #define UNIQUE_PTR std::unique_ptr
26 #else
27 #include "llvm/ADT/OwningPtr.h"
28 #include "llvm/Support/system_error.h"
29 
30 #define ERROR_CODE error_code
31 #define UNIQUE_PTR OwningPtr
32 #endif
33 
34 using namespace llvm;
35 
36 static cl::opt<std::string>
37 InputFilename(cl::Positional, cl::desc("<input bitcode>"), cl::init("-"));
38 
39 static cl::opt<std::string>
40 OutputFilename("o", cl::desc("Output filename"),
41                cl::value_desc("filename"));
42 
43 int 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   {
53 #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
59     UNIQUE_PTR<MemoryBuffer> BufferPtr;
60     if (ERROR_CODE ec = MemoryBuffer::getFileOrSTDIN(InputFilename, BufferPtr))
61 #endif
62       ErrorMessage = ec.message();
63     else {
64 #if LLVM_VERSION_MAJOR > 3 || (LLVM_VERSION_MAJOR == 3 && LLVM_VERSION_MINOR > 4)
65 # if LLVM_360_AND_NEWER
66       ErrorOr<Module *> ModuleOrErr =
67 	parseBitcodeFile(BufferPtr.get()->getMemBufferRef(), Context);
68 # else
69       ErrorOr<Module *> ModuleOrErr = parseBitcodeFile(BufferPtr.get(), Context);
70 # endif
71       if (ERROR_CODE ec = ModuleOrErr.getError())
72         ErrorMessage = ec.message();
73       M.reset(ModuleOrErr.get());
74 #else
75       M.reset(ParseBitcodeFile(BufferPtr.get(), Context, &ErrorMessage));
76 #endif
77     }
78   }
79 
80   if (M.get() == 0) {
81     errs() << argv[0] << ": ";
82     if (ErrorMessage.size())
83       errs() << ErrorMessage << "\n";
84     else
85       errs() << "bitcode didn't read correctly.\n";
86     return 1;
87   }
88 
89   // Set linkage of every external definition to linkonce_odr.
90   for (Module::iterator i = M->begin(), e = M->end(); i != e; ++i) {
91     if (!i->isDeclaration() && i->getLinkage() == GlobalValue::ExternalLinkage)
92       i->setLinkage(GlobalValue::LinkOnceODRLinkage);
93   }
94 
95   for (Module::global_iterator i = M->global_begin(), e = M->global_end();
96        i != e; ++i) {
97     if (!i->isDeclaration() && i->getLinkage() == GlobalValue::ExternalLinkage)
98       i->setLinkage(GlobalValue::LinkOnceODRLinkage);
99   }
100 
101   if (OutputFilename.empty()) {
102     errs() << "no output file\n";
103     return 1;
104   }
105 
106 #if LLVM_360_AND_NEWER
107   std::error_code EC;
108   UNIQUE_PTR<tool_output_file> Out
109   (new tool_output_file(OutputFilename, EC, sys::fs::F_None));
110   if (EC) {
111     errs() << EC.message() << '\n';
112     exit(1);
113   }
114 #else
115   std::string ErrorInfo;
116   UNIQUE_PTR<tool_output_file> Out
117   (new tool_output_file(OutputFilename.c_str(), ErrorInfo,
118 #if (LLVM_VERSION_MAJOR == 3 && LLVM_VERSION_MINOR == 4)
119                         sys::fs::F_Binary));
120 #elif LLVM_VERSION_MAJOR > 3 || (LLVM_VERSION_MAJOR == 3 && LLVM_VERSION_MINOR >= 5)
121                         sys::fs::F_None));
122 #else
123                         raw_fd_ostream::F_Binary));
124 #endif
125   if (!ErrorInfo.empty()) {
126     errs() << ErrorInfo << '\n';
127     exit(1);
128   }
129 #endif // LLVM_360_AND_NEWER
130 
131   WriteBitcodeToFile(M.get(), Out->os());
132 
133   // Declare success.
134   Out->keep();
135   return 0;
136 }
137 
138