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