blob: 45ce77e6ddecd0650c47931261def6fcd3a87451 [file] [log] [blame]
zonr6315f762010-10-05 15:35:14 +08001#include <memory>
2#include <string>
3#include <vector>
4
Shih-wei Liao835a7b72010-08-06 02:29:11 -07005#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 Liao835a7b72010-08-06 02:29:11 -070023
24using namespace llvm;
25
26static cl::list<std::string>
27InputFilenames(cl::Positional, cl::OneOrMore,
28 cl::desc("<input bitcode files>"));
29
30static cl::opt<std::string>
31OutputFilename("o", cl::Required, cl::desc("Override output filename"),
32 cl::value_desc("<output bitcode file>"));
33
Shih-wei Liao9c9bbc82010-08-18 03:08:44 -070034static cl::opt<bool>
Shih-wei Liao0714af72010-08-19 16:57:39 -070035Externalize("e", cl::Optional, cl::desc("To externalize"));
Shih-wei Liao9c9bbc82010-08-18 03:08:44 -070036
Shih-wei Liao835a7b72010-08-06 02:29:11 -070037// GetExported - ...
38static 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 - ...
59static 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//
71static 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
96int main(int argc, char **argv) {
97 llvm_shutdown_obj X;
98
Shih-wei Liao9c9bbc82010-08-18 03:08:44 -070099 cl::ParseCommandLineOptions(argc, argv, "llvm-rs-link\n");
Shih-wei Liao835a7b72010-08-06 02:29:11 -0700100
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) {
zonr6315f762010-10-05 15:35:14 +0800113 errs() << argv[0] << ": error loading file '" << InputFilenames[i]
114 << "'\n";
Shih-wei Liao835a7b72010-08-06 02:29:11 -0700115 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 Liao9c9bbc82010-08-18 03:08:44 -0700146 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 Liao835a7b72010-08-06 02:29:11 -0700151
Shih-wei Liao9c9bbc82010-08-18 03:08:44 -0700152 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 Liao835a7b72010-08-06 02:29:11 -0700159 }
160
Shih-wei Liao835a7b72010-08-06 02:29:11 -0700161 createStandardLTOPasses(&Passes, false, true, false);
162 Passes.run(*Composite.get());
163
164 WriteBitcodeToFile(Composite.get(), *Out);
165 return 0;
166}