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" |
| 12 | #include "llvm/Support/CommandLine.h" |
| 13 | #include "llvm/Module.h" |
| 14 | #include "llvm/Method.h" |
| 15 | #include <fstream.h> |
| 16 | #include <memory> |
| 17 | |
| 18 | |
| 19 | cl::StringList InputFilenames("", "Load <arg> files, linking them together", |
| 20 | cl::OneOrMore); |
| 21 | cl::String OutputFilename("o", "Override output filename", cl::NoFlags, "-"); |
| 22 | cl::Flag Force ("f", "Overwrite output files", cl::NoFlags, false); |
| 23 | |
| 24 | |
| 25 | int main(int argc, char **argv) { |
| 26 | cl::ParseCommandLineOptions(argc, argv, " llvm linker\n"); |
| 27 | assert(InputFilenames.size() > 0 && "OneOrMore is not working"); |
| 28 | |
| 29 | std::auto_ptr<Module> Composite(ParseBytecodeFile(InputFilenames[0])); |
| 30 | if (Composite.get() == 0) { |
| 31 | cerr << "Error opening bytecode file: '" << InputFilenames[0] << "'\n"; |
| 32 | return 1; |
| 33 | } |
| 34 | |
| 35 | for (unsigned i = 1; i < InputFilenames.size(); ++i) { |
| 36 | auto_ptr<Module> M(ParseBytecodeFile(InputFilenames[i])); |
| 37 | if (M.get() == 0) { |
| 38 | cerr << "Error opening bytecode file: '" << InputFilenames[i] << "'\n"; |
| 39 | return 1; |
| 40 | } |
| 41 | |
| 42 | string ErrorMessage; |
| 43 | if (LinkModules(Composite.get(), M.get(), &ErrorMessage)) { |
| 44 | cerr << "Error linking in '" << InputFilenames[i] << "': " |
| 45 | << ErrorMessage << endl; |
| 46 | return 1; |
| 47 | } |
| 48 | } |
| 49 | |
| 50 | ostream *Out = &cout; // Default to printing to stdout... |
| 51 | if (OutputFilename != "-") { |
| 52 | Out = new ofstream(OutputFilename.c_str(), |
| 53 | (Force ? 0 : ios::noreplace)|ios::out); |
| 54 | if (!Out->good()) { |
| 55 | cerr << "Error opening '" << OutputFilename << "'!\n"; |
| 56 | return 1; |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | WriteBytecodeToFile(Composite.get(), *Out); |
| 61 | |
| 62 | if (Out != &cout) delete Out; |
| 63 | return 0; |
| 64 | } |