1 //===----------------------------------------------------------------------===//
4 // This utility may be invoked in the following manner:
5 // link a.bc b.bc c.bc -o x.bc
7 //===----------------------------------------------------------------------===//
9 #include "llvm/Transforms/Linker.h"
10 #include "llvm/Bytecode/Reader.h"
11 #include "llvm/Bytecode/Writer.h"
12 #include "llvm/Assembly/Writer.h"
13 #include "llvm/Support/CommandLine.h"
14 #include "llvm/Module.h"
15 #include "llvm/Method.h"
20 cl::StringList InputFilenames("", "Load <arg> files, linking them together",
22 cl::String OutputFilename("o", "Override output filename", cl::NoFlags, "-");
23 cl::Flag Force ("f", "Overwrite output files", cl::NoFlags, false);
24 cl::Flag DumpAsm ("d", "Print assembly as linked", cl::Hidden, false);
27 int main(int argc, char **argv) {
28 cl::ParseCommandLineOptions(argc, argv, " llvm linker\n");
29 assert(InputFilenames.size() > 0 && "OneOrMore is not working");
31 std::auto_ptr<Module> Composite(ParseBytecodeFile(InputFilenames[0]));
32 if (Composite.get() == 0) {
33 cerr << "Error opening bytecode file: '" << InputFilenames[0] << "'\n";
37 for (unsigned i = 1; i < InputFilenames.size(); ++i) {
38 auto_ptr<Module> M(ParseBytecodeFile(InputFilenames[i]));
40 cerr << "Error opening bytecode file: '" << InputFilenames[i] << "'\n";
45 if (LinkModules(Composite.get(), M.get(), &ErrorMessage)) {
46 cerr << "Error linking in '" << InputFilenames[i] << "': "
47 << ErrorMessage << endl;
53 cerr << "Here's the assembly:\n" << Composite.get();
55 ostream *Out = &cout; // Default to printing to stdout...
56 if (OutputFilename != "-") {
57 Out = new ofstream(OutputFilename.c_str(),
58 (Force ? 0 : ios::noreplace)|ios::out);
60 cerr << "Error opening '" << OutputFilename << "'!\n";
65 WriteBytecodeToFile(Composite.get(), *Out);
67 if (Out != &cout) delete Out;