blob: e9e9ac17263e695081aa01530de669af1282d186 [file] [log] [blame]
Chris Lattner075a0b72001-10-13 07:06:23 +00001//===----------------------------------------------------------------------===//
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 Lattner164cb692001-10-14 23:23:33 +000012#include "llvm/Assembly/Writer.h"
Chris Lattner075a0b72001-10-13 07:06:23 +000013#include "llvm/Support/CommandLine.h"
14#include "llvm/Module.h"
15#include "llvm/Method.h"
16#include <fstream.h>
17#include <memory>
18
19
20cl::StringList InputFilenames("", "Load <arg> files, linking them together",
21 cl::OneOrMore);
22cl::String OutputFilename("o", "Override output filename", cl::NoFlags, "-");
23cl::Flag Force ("f", "Overwrite output files", cl::NoFlags, false);
Chris Lattner164cb692001-10-14 23:23:33 +000024cl::Flag DumpAsm ("d", "Print assembly as linked", cl::Hidden, false);
Chris Lattner075a0b72001-10-13 07:06:23 +000025
26
27int 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 Lattner164cb692001-10-14 23:23:33 +000052 if (DumpAsm)
53 cerr << "Here's the assembly:\n" << Composite.get();
54
Chris Lattner075a0b72001-10-13 07:06:23 +000055 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}