blob: a3c33839065dc30e0f3e2363d356a887eb3041d3 [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
Jeroen Ketemad253e662014-06-21 09:20:31 +000015#include <system_error>
16
Jeroen Ketemad253e662014-06-21 09:20:31 +000017using namespace llvm;
18
Peter Collingbourned5395fb2012-01-08 22:09:58 +000019static cl::opt<std::string>
20InputFilename(cl::Positional, cl::desc("<input bitcode>"), cl::init("-"));
21
22static cl::opt<std::string>
23OutputFilename("o", cl::desc("Output filename"),
24 cl::value_desc("filename"));
25
26int main(int argc, char **argv) {
27 LLVMContext &Context = getGlobalContext();
28 llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
29
30 cl::ParseCommandLineOptions(argc, argv, "libclc builtin preparation tool\n");
31
32 std::string ErrorMessage;
33 std::auto_ptr<Module> M;
34
35 {
Tom Stellard8d92c392014-07-07 17:46:45 +000036 ErrorOr<std::unique_ptr<MemoryBuffer>> BufferOrErr =
37 MemoryBuffer::getFile(InputFilename);
38 std::unique_ptr<MemoryBuffer> &BufferPtr = BufferOrErr.get();
39 if (std::error_code ec = BufferOrErr.getError())
Peter Collingbourned5395fb2012-01-08 22:09:58 +000040 ErrorMessage = ec.message();
Tom Stellard8a63b152014-01-20 20:28:48 +000041 else {
Michel Danzer7b77ab72014-08-28 06:19:37 +000042 ErrorOr<Module *> ModuleOrErr =
43 parseBitcodeFile(BufferPtr.get()->getMemBufferRef(), Context);
Tom Stellard1d770712014-12-31 15:27:53 +000044 if (std::error_code ec = ModuleOrErr.getError())
Tom Stellard8a63b152014-01-20 20:28:48 +000045 ErrorMessage = ec.message();
46 M.reset(ModuleOrErr.get());
Tom Stellard8a63b152014-01-20 20:28:48 +000047 }
Peter Collingbourned5395fb2012-01-08 22:09:58 +000048 }
49
50 if (M.get() == 0) {
51 errs() << argv[0] << ": ";
52 if (ErrorMessage.size())
53 errs() << ErrorMessage << "\n";
54 else
55 errs() << "bitcode didn't read correctly.\n";
56 return 1;
57 }
58
59 // Set linkage of every external definition to linkonce_odr.
60 for (Module::iterator i = M->begin(), e = M->end(); i != e; ++i) {
61 if (!i->isDeclaration() && i->getLinkage() == GlobalValue::ExternalLinkage)
62 i->setLinkage(GlobalValue::LinkOnceODRLinkage);
63 }
64
65 for (Module::global_iterator i = M->global_begin(), e = M->global_end();
66 i != e; ++i) {
67 if (!i->isDeclaration() && i->getLinkage() == GlobalValue::ExternalLinkage)
68 i->setLinkage(GlobalValue::LinkOnceODRLinkage);
69 }
70
71 if (OutputFilename.empty()) {
72 errs() << "no output file\n";
73 return 1;
74 }
75
Michel Danzera10b4922014-08-28 06:19:33 +000076 std::error_code EC;
Tom Stellard1d770712014-12-31 15:27:53 +000077 std::unique_ptr<tool_output_file> Out
Michel Danzera10b4922014-08-28 06:19:33 +000078 (new tool_output_file(OutputFilename, EC, sys::fs::F_None));
79 if (EC) {
80 errs() << EC.message() << '\n';
81 exit(1);
82 }
Peter Collingbourned5395fb2012-01-08 22:09:58 +000083
84 WriteBitcodeToFile(M.get(), Out->os());
85
86 // Declare success.
87 Out->keep();
88 return 0;
89}
90