blob: cdd543461165b518d8aee410e3156c197830c006 [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//
Chris Lattnerb81adf12001-10-23 20:44:55 +00007// Alternatively, this can be used as an 'ar' tool as well. If invoked as
8// either 'ar' or 'llvm-ar', it accepts a 'cr' parameter as well.
9//
Chris Lattner075a0b72001-10-13 07:06:23 +000010//===----------------------------------------------------------------------===//
11
12#include "llvm/Transforms/Linker.h"
13#include "llvm/Bytecode/Reader.h"
14#include "llvm/Bytecode/Writer.h"
Chris Lattner164cb692001-10-14 23:23:33 +000015#include "llvm/Assembly/Writer.h"
Chris Lattner075a0b72001-10-13 07:06:23 +000016#include "llvm/Support/CommandLine.h"
17#include "llvm/Module.h"
18#include "llvm/Method.h"
19#include <fstream.h>
20#include <memory>
21
22
23cl::StringList InputFilenames("", "Load <arg> files, linking them together",
24 cl::OneOrMore);
25cl::String OutputFilename("o", "Override output filename", cl::NoFlags, "-");
26cl::Flag Force ("f", "Overwrite output files", cl::NoFlags, false);
Chris Lattnerb81adf12001-10-23 20:44:55 +000027cl::Flag Verbose ("v", "Print information about actions taken");
Chris Lattner164cb692001-10-14 23:23:33 +000028cl::Flag DumpAsm ("d", "Print assembly as linked", cl::Hidden, false);
Chris Lattner075a0b72001-10-13 07:06:23 +000029
30
31int main(int argc, char **argv) {
32 cl::ParseCommandLineOptions(argc, argv, " llvm linker\n");
33 assert(InputFilenames.size() > 0 && "OneOrMore is not working");
34
Chris Lattnerb81adf12001-10-23 20:44:55 +000035 // TODO: TEST argv[0]
36
37
38 if (Verbose) cerr << "Loading '" << InputFilenames[0] << "'\n";
Chris Lattner075a0b72001-10-13 07:06:23 +000039 std::auto_ptr<Module> Composite(ParseBytecodeFile(InputFilenames[0]));
40 if (Composite.get() == 0) {
41 cerr << "Error opening bytecode file: '" << InputFilenames[0] << "'\n";
42 return 1;
43 }
44
45 for (unsigned i = 1; i < InputFilenames.size(); ++i) {
Chris Lattnerb81adf12001-10-23 20:44:55 +000046 if (Verbose) cerr << "Loading '" << InputFilenames[i] << "'\n";
Chris Lattner075a0b72001-10-13 07:06:23 +000047 auto_ptr<Module> M(ParseBytecodeFile(InputFilenames[i]));
48 if (M.get() == 0) {
49 cerr << "Error opening bytecode file: '" << InputFilenames[i] << "'\n";
50 return 1;
51 }
Chris Lattnerb81adf12001-10-23 20:44:55 +000052
53 if (Verbose) cerr << "Linking in '" << InputFilenames[i] << "'\n";
54
Chris Lattner075a0b72001-10-13 07:06:23 +000055 string ErrorMessage;
56 if (LinkModules(Composite.get(), M.get(), &ErrorMessage)) {
57 cerr << "Error linking in '" << InputFilenames[i] << "': "
58 << ErrorMessage << endl;
59 return 1;
60 }
61 }
62
Chris Lattner164cb692001-10-14 23:23:33 +000063 if (DumpAsm)
64 cerr << "Here's the assembly:\n" << Composite.get();
65
Chris Lattner075a0b72001-10-13 07:06:23 +000066 ostream *Out = &cout; // Default to printing to stdout...
67 if (OutputFilename != "-") {
68 Out = new ofstream(OutputFilename.c_str(),
69 (Force ? 0 : ios::noreplace)|ios::out);
70 if (!Out->good()) {
71 cerr << "Error opening '" << OutputFilename << "'!\n";
72 return 1;
73 }
74 }
75
Chris Lattnerb81adf12001-10-23 20:44:55 +000076 if (Verbose) cerr << "Writing bytecode...\n";
Chris Lattner075a0b72001-10-13 07:06:23 +000077 WriteBytecodeToFile(Composite.get(), *Out);
78
79 if (Out != &cout) delete Out;
80 return 0;
81}