Chris Lattner | 075a0b7 | 2001-10-13 07:06:23 +0000 | [diff] [blame] | 1 | //===----------------------------------------------------------------------===// |
| 2 | // LLVM 'LINK' UTILITY |
| 3 | // |
| 4 | // This utility may be invoked in the following manner: |
| 5 | // link a.bc b.bc c.bc -o x.bc |
| 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | |
| 9 | #include "llvm/Transforms/Linker.h" |
| 10 | #include "llvm/Bytecode/Reader.h" |
| 11 | #include "llvm/Bytecode/Writer.h" |
Chris Lattner | 164cb69 | 2001-10-14 23:23:33 +0000 | [diff] [blame] | 12 | #include "llvm/Assembly/Writer.h" |
Chris Lattner | 075a0b7 | 2001-10-13 07:06:23 +0000 | [diff] [blame] | 13 | #include "llvm/Support/CommandLine.h" |
| 14 | #include "llvm/Module.h" |
| 15 | #include "llvm/Method.h" |
| 16 | #include <fstream.h> |
| 17 | #include <memory> |
| 18 | |
| 19 | |
| 20 | cl::StringList InputFilenames("", "Load <arg> files, linking them together", |
| 21 | cl::OneOrMore); |
| 22 | cl::String OutputFilename("o", "Override output filename", cl::NoFlags, "-"); |
| 23 | cl::Flag Force ("f", "Overwrite output files", cl::NoFlags, false); |
Chris Lattner | 164cb69 | 2001-10-14 23:23:33 +0000 | [diff] [blame] | 24 | cl::Flag DumpAsm ("d", "Print assembly as linked", cl::Hidden, false); |
Chris Lattner | 075a0b7 | 2001-10-13 07:06:23 +0000 | [diff] [blame] | 25 | |
| 26 | |
| 27 | int main(int argc, char **argv) { |
| 28 | cl::ParseCommandLineOptions(argc, argv, " llvm linker\n"); |
| 29 | assert(InputFilenames.size() > 0 && "OneOrMore is not working"); |
| 30 | |
| 31 | std::auto_ptr<Module> Composite(ParseBytecodeFile(InputFilenames[0])); |
| 32 | if (Composite.get() == 0) { |
| 33 | cerr << "Error opening bytecode file: '" << InputFilenames[0] << "'\n"; |
| 34 | return 1; |
| 35 | } |
| 36 | |
| 37 | for (unsigned i = 1; i < InputFilenames.size(); ++i) { |
| 38 | auto_ptr<Module> M(ParseBytecodeFile(InputFilenames[i])); |
| 39 | if (M.get() == 0) { |
| 40 | cerr << "Error opening bytecode file: '" << InputFilenames[i] << "'\n"; |
| 41 | return 1; |
| 42 | } |
| 43 | |
| 44 | string ErrorMessage; |
| 45 | if (LinkModules(Composite.get(), M.get(), &ErrorMessage)) { |
| 46 | cerr << "Error linking in '" << InputFilenames[i] << "': " |
| 47 | << ErrorMessage << endl; |
| 48 | return 1; |
| 49 | } |
| 50 | } |
| 51 | |
Chris Lattner | 164cb69 | 2001-10-14 23:23:33 +0000 | [diff] [blame] | 52 | if (DumpAsm) |
| 53 | cerr << "Here's the assembly:\n" << Composite.get(); |
| 54 | |
Chris Lattner | 075a0b7 | 2001-10-13 07:06:23 +0000 | [diff] [blame] | 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); |
| 59 | if (!Out->good()) { |
| 60 | cerr << "Error opening '" << OutputFilename << "'!\n"; |
| 61 | return 1; |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | WriteBytecodeToFile(Composite.get(), *Out); |
| 66 | |
| 67 | if (Out != &cout) delete Out; |
| 68 | return 0; |
| 69 | } |