1 #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"
16 static cl::opt<std::string>
17 InputFilename(cl::Positional, cl::desc("<input bitcode>"), cl::init("-"));
19 static cl::opt<std::string>
20 OutputFilename("o", cl::desc("Output filename"),
21 cl::value_desc("filename"));
23 int main(int argc, char **argv) {
24 LLVMContext &Context = getGlobalContext();
25 llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
27 cl::ParseCommandLineOptions(argc, argv, "libclc builtin preparation tool\n");
29 std::string ErrorMessage;
30 std::auto_ptr<Module> M;
33 OwningPtr<MemoryBuffer> BufferPtr;
34 if (error_code ec = MemoryBuffer::getFileOrSTDIN(InputFilename, BufferPtr))
35 ErrorMessage = ec.message();
37 M.reset(ParseBitcodeFile(BufferPtr.get(), Context, &ErrorMessage));
41 errs() << argv[0] << ": ";
42 if (ErrorMessage.size())
43 errs() << ErrorMessage << "\n";
45 errs() << "bitcode didn't read correctly.\n";
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);
55 for (Module::global_iterator i = M->global_begin(), e = M->global_end();
57 if (!i->isDeclaration() && i->getLinkage() == GlobalValue::ExternalLinkage)
58 i->setLinkage(GlobalValue::LinkOnceODRLinkage);
61 if (OutputFilename.empty()) {
62 errs() << "no output file\n";
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';
75 WriteBitcodeToFile(M.get(), Out->os());