blob: 7aa8a040761ad301ef0169a3278084eaa05aecaa [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
Chris Lattner65be3212001-10-24 06:23:00 +00008// either 'ar' or 'llvm-ar', it accepts a 'rc' parameter as well.
Chris Lattnerb81adf12001-10-23 20:44:55 +00009//
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 Lattner65be3212001-10-24 06:23:00 +000029cl::StringList LibPaths ("L", "Specify a library search path", cl::ZeroOrMore);
Chris Lattner075a0b72001-10-13 07:06:23 +000030
Chris Lattner65be3212001-10-24 06:23:00 +000031static inline std::auto_ptr<Module> LoadFile(const string &FN) {
32 string Filename = FN;
33 string ErrorMessage;
34
35 unsigned NextLibPathIdx = 0;
36
37 while (1) {
38 if (Verbose) cerr << "Loading '" << Filename << "'\n";
39 Module *Result = ParseBytecodeFile(Filename, &ErrorMessage);
40 if (Result) return std::auto_ptr<Module>(Result); // Load successful!
41
42 if (Verbose) {
43 cerr << "Error opening bytecode file: '" << Filename << "'";
44 if (ErrorMessage.size()) cerr << ": " << ErrorMessage;
45 cerr << endl;
46 }
47
48 if (NextLibPathIdx == LibPaths.size()) break;
49 Filename = LibPaths[NextLibPathIdx++] + "/" + FN;
50 }
51
52 cerr << "Could not locate bytecode file: '" << FN << "'\n";
53 return std::auto_ptr<Module>();
54}
Chris Lattner075a0b72001-10-13 07:06:23 +000055
56int main(int argc, char **argv) {
57 cl::ParseCommandLineOptions(argc, argv, " llvm linker\n");
58 assert(InputFilenames.size() > 0 && "OneOrMore is not working");
59
Chris Lattner65be3212001-10-24 06:23:00 +000060 unsigned BaseArg = 0;
Chris Lattnerd6b65252001-10-24 01:15:12 +000061 string ErrorMessage;
Chris Lattner65be3212001-10-24 06:23:00 +000062
63 // TODO: TEST argv[0] for llvm-ar forms... for now, this is a huge hack.
64 if (InputFilenames.size() >= 3 && InputFilenames[0] == "rc" &&
65 OutputFilename == "-") {
66 BaseArg = 2;
67 OutputFilename = InputFilenames[1];
Chris Lattner075a0b72001-10-13 07:06:23 +000068 }
69
Chris Lattner65be3212001-10-24 06:23:00 +000070 std::auto_ptr<Module> Composite(LoadFile(InputFilenames[BaseArg]));
71 if (Composite.get() == 0) return 1;
72
73 for (unsigned i = BaseArg+1; i < InputFilenames.size(); ++i) {
74 auto_ptr<Module> M(LoadFile(InputFilenames[i]));
75 if (M.get() == 0) return 1;
Chris Lattnerb81adf12001-10-23 20:44:55 +000076
77 if (Verbose) cerr << "Linking in '" << InputFilenames[i] << "'\n";
78
Chris Lattner075a0b72001-10-13 07:06:23 +000079 if (LinkModules(Composite.get(), M.get(), &ErrorMessage)) {
80 cerr << "Error linking in '" << InputFilenames[i] << "': "
81 << ErrorMessage << endl;
82 return 1;
83 }
84 }
85
Chris Lattner164cb692001-10-14 23:23:33 +000086 if (DumpAsm)
87 cerr << "Here's the assembly:\n" << Composite.get();
88
Chris Lattner075a0b72001-10-13 07:06:23 +000089 ostream *Out = &cout; // Default to printing to stdout...
90 if (OutputFilename != "-") {
91 Out = new ofstream(OutputFilename.c_str(),
92 (Force ? 0 : ios::noreplace)|ios::out);
93 if (!Out->good()) {
94 cerr << "Error opening '" << OutputFilename << "'!\n";
95 return 1;
96 }
97 }
98
Chris Lattnerb81adf12001-10-23 20:44:55 +000099 if (Verbose) cerr << "Writing bytecode...\n";
Chris Lattner075a0b72001-10-13 07:06:23 +0000100 WriteBytecodeToFile(Composite.get(), *Out);
101
102 if (Out != &cout) delete Out;
103 return 0;
104}