zonr | 6315f76 | 2010-10-05 15:35:14 +0800 | [diff] [blame] | 1 | #include <memory> |
| 2 | #include <string> |
| 3 | #include <vector> |
| 4 | |
Shih-wei Liao | 835a7b7 | 2010-08-06 02:29:11 -0700 | [diff] [blame] | 5 | #include "llvm/Linker.h" |
| 6 | #include "llvm/LLVMContext.h" |
| 7 | #include "llvm/Metadata.h" |
| 8 | #include "llvm/Module.h" |
| 9 | #include "llvm/Analysis/Verifier.h" |
| 10 | #include "llvm/Bitcode/ReaderWriter.h" |
| 11 | #include "llvm/Support/CommandLine.h" |
| 12 | #include "llvm/Support/ManagedStatic.h" |
| 13 | #include "llvm/Support/MemoryBuffer.h" |
| 14 | #include "llvm/Support/PrettyStackTrace.h" |
| 15 | #include "llvm/Support/StandardPasses.h" |
| 16 | #include "llvm/Support/SystemUtils.h" |
| 17 | #include "llvm/Support/raw_ostream.h" |
| 18 | #include "llvm/System/Signals.h" |
| 19 | #include "llvm/System/Path.h" |
| 20 | #include "llvm/Target/TargetData.h" |
| 21 | #include "llvm/Target/TargetMachine.h" |
| 22 | #include "llvm/LinkAllVMCore.h" |
Shih-wei Liao | 835a7b7 | 2010-08-06 02:29:11 -0700 | [diff] [blame] | 23 | |
| 24 | using namespace llvm; |
| 25 | |
| 26 | static cl::list<std::string> |
| 27 | InputFilenames(cl::Positional, cl::OneOrMore, |
| 28 | cl::desc("<input bitcode files>")); |
| 29 | |
| 30 | static cl::opt<std::string> |
| 31 | OutputFilename("o", cl::Required, cl::desc("Override output filename"), |
| 32 | cl::value_desc("<output bitcode file>")); |
| 33 | |
Shih-wei Liao | 9c9bbc8 | 2010-08-18 03:08:44 -0700 | [diff] [blame] | 34 | static cl::opt<bool> |
Shih-wei Liao | 0714af7 | 2010-08-19 16:57:39 -0700 | [diff] [blame] | 35 | Externalize("e", cl::Optional, cl::desc("To externalize")); |
Shih-wei Liao | 9c9bbc8 | 2010-08-18 03:08:44 -0700 | [diff] [blame] | 36 | |
Shih-wei Liao | 835a7b7 | 2010-08-06 02:29:11 -0700 | [diff] [blame] | 37 | // GetExported - ... |
| 38 | static void GetExported(NamedMDNode *N, std::vector<std::string> &Names) { |
| 39 | for (int i = 0, e = N->getNumOperands(); i != e; ++i) { |
| 40 | MDNode *exported_var = N->getOperand(i); |
| 41 | if (!exported_var) continue; |
| 42 | |
| 43 | if (exported_var->getNumOperands() == 0) { |
| 44 | errs() << "Invalid RenderScript reflection data.\n"; |
| 45 | abort(); |
| 46 | } |
| 47 | |
| 48 | MDString *name = dyn_cast<MDString>(exported_var->getOperand(0)); |
| 49 | if (!name) { |
| 50 | errs() << "Invalid RenderScript reflection data.\n"; |
| 51 | abort(); |
| 52 | } |
| 53 | |
| 54 | Names.push_back(name->getString()); |
| 55 | } |
| 56 | } |
| 57 | |
| 58 | // GetExportedGlobals - ... |
| 59 | static void GetExportedGlobals(Module *M, std::vector<std::string> &Names) { |
| 60 | if (NamedMDNode *rs_export_var = M->getNamedMetadata("#rs_export_var")) { |
| 61 | GetExported(rs_export_var, Names); |
| 62 | } |
| 63 | if (NamedMDNode *rs_export_func = M->getNamedMetadata("#rs_export_func")) { |
| 64 | GetExported(rs_export_func, Names); |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | // LoadFile - Read the specified bitcode file in and return it. This routine |
| 69 | // searches the link path for the specified file to try to find it... |
| 70 | // |
| 71 | static inline std::auto_ptr<Module> LoadFile(const char *argv0, |
| 72 | const std::string &FN, |
| 73 | LLVMContext& Context) { |
| 74 | sys::Path Filename; |
| 75 | if (!Filename.set(FN)) { |
| 76 | errs() << "Invalid file name: '" << FN << "'\n"; |
| 77 | return std::auto_ptr<Module>(); |
| 78 | } |
| 79 | |
| 80 | std::string Err; |
| 81 | Module *Result = 0; |
| 82 | |
| 83 | const std::string &FNStr = Filename.str(); |
| 84 | MemoryBuffer *Buffer = MemoryBuffer::getFile(FNStr, &Err); |
| 85 | if (!Buffer) { |
| 86 | errs() << Err; |
| 87 | return std::auto_ptr<Module>(); |
| 88 | } |
| 89 | Result = ParseBitcodeFile(Buffer, Context, &Err); |
| 90 | if (Result) return std::auto_ptr<Module>(Result); // Load successful! |
| 91 | |
| 92 | errs() << Err; |
| 93 | return std::auto_ptr<Module>(); |
| 94 | } |
| 95 | |
| 96 | int main(int argc, char **argv) { |
| 97 | llvm_shutdown_obj X; |
| 98 | |
Shih-wei Liao | 9c9bbc8 | 2010-08-18 03:08:44 -0700 | [diff] [blame] | 99 | cl::ParseCommandLineOptions(argc, argv, "llvm-rs-link\n"); |
Shih-wei Liao | 835a7b7 | 2010-08-06 02:29:11 -0700 | [diff] [blame] | 100 | |
| 101 | LLVMContext &Context = getGlobalContext(); |
| 102 | std::auto_ptr<Module> Composite = LoadFile(argv[0], InputFilenames[0], |
| 103 | Context); |
| 104 | std::string ErrorMessage; |
| 105 | if (Composite.get() == 0) { |
| 106 | errs() << argv[0] << ": error loading file '" |
| 107 | << InputFilenames[0] << "'\n"; |
| 108 | return 1; |
| 109 | } |
| 110 | for (unsigned i = 1; i < InputFilenames.size(); ++i) { |
| 111 | std::auto_ptr<Module> M(LoadFile(argv[0], InputFilenames[i], Context)); |
| 112 | if (M.get() == 0) { |
zonr | 6315f76 | 2010-10-05 15:35:14 +0800 | [diff] [blame] | 113 | errs() << argv[0] << ": error loading file '" << InputFilenames[i] |
| 114 | << "'\n"; |
Shih-wei Liao | 835a7b7 | 2010-08-06 02:29:11 -0700 | [diff] [blame] | 115 | return 1; |
| 116 | } |
| 117 | if (Linker::LinkModules(Composite.get(), M.get(), &ErrorMessage)) { |
| 118 | errs() << argv[0] << ": link error in '" << InputFilenames[i] |
| 119 | << "': " << ErrorMessage << "\n"; |
| 120 | return 1; |
| 121 | } |
| 122 | } |
| 123 | |
| 124 | std::string ErrorInfo; |
| 125 | std::auto_ptr<raw_ostream> |
| 126 | Out(new raw_fd_ostream(OutputFilename.c_str(), ErrorInfo, |
| 127 | raw_fd_ostream::F_Binary)); |
| 128 | if (!ErrorInfo.empty()) { |
| 129 | errs() << ErrorInfo << '\n'; |
| 130 | return 1; |
| 131 | } |
| 132 | |
| 133 | if (verifyModule(*Composite)) { |
| 134 | errs() << argv[0] << ": linked module is broken!\n"; |
| 135 | return 1; |
| 136 | } |
| 137 | |
| 138 | PassManager Passes; |
| 139 | |
| 140 | const std::string &ModuleDataLayout = Composite.get()->getDataLayout(); |
| 141 | if (!ModuleDataLayout.empty()) { |
| 142 | if (TargetData *TD = new TargetData(ModuleDataLayout)) |
| 143 | Passes.add(TD); |
| 144 | } |
| 145 | |
Shih-wei Liao | 9c9bbc8 | 2010-08-18 03:08:44 -0700 | [diff] [blame] | 146 | if (!Externalize) { |
| 147 | std::vector<std::string> PublicAPINames; |
| 148 | PublicAPINames.push_back("init"); |
| 149 | PublicAPINames.push_back("root"); |
| 150 | GetExportedGlobals(Composite.get(), PublicAPINames); |
Shih-wei Liao | 835a7b7 | 2010-08-06 02:29:11 -0700 | [diff] [blame] | 151 | |
Shih-wei Liao | 9c9bbc8 | 2010-08-18 03:08:44 -0700 | [diff] [blame] | 152 | std::vector<const char *> PublicAPINamesCStr; |
| 153 | for (int i = 0, e = PublicAPINames.size(); i != e; ++i) { |
| 154 | // errs() << "not internalizing: " << PublicAPINames[i] << '\n'; |
| 155 | PublicAPINamesCStr.push_back(PublicAPINames[i].c_str()); |
| 156 | } |
| 157 | |
| 158 | Passes.add(createInternalizePass(PublicAPINamesCStr)); |
Shih-wei Liao | 835a7b7 | 2010-08-06 02:29:11 -0700 | [diff] [blame] | 159 | } |
| 160 | |
Shih-wei Liao | 835a7b7 | 2010-08-06 02:29:11 -0700 | [diff] [blame] | 161 | createStandardLTOPasses(&Passes, false, true, false); |
| 162 | Passes.run(*Composite.get()); |
| 163 | |
| 164 | WriteBitcodeToFile(Composite.get(), *Out); |
| 165 | return 0; |
| 166 | } |