blob: 80a39b2d9c159d38a2361a55dbc3da627fede106 [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) {
Chris Lattnerde3b8622001-11-26 19:18:30 +000057 cl::ParseCommandLineOptions(argc, argv, " llvm linker\n",
58 cl::EnableSingleLetterArgValue |
59 cl::DisableSingleLetterArgGrouping);
Chris Lattner075a0b72001-10-13 07:06:23 +000060 assert(InputFilenames.size() > 0 && "OneOrMore is not working");
61
Chris Lattner65be3212001-10-24 06:23:00 +000062 unsigned BaseArg = 0;
Chris Lattnerd6b65252001-10-24 01:15:12 +000063 string ErrorMessage;
Chris Lattner65be3212001-10-24 06:23:00 +000064
65 // TODO: TEST argv[0] for llvm-ar forms... for now, this is a huge hack.
66 if (InputFilenames.size() >= 3 && InputFilenames[0] == "rc" &&
67 OutputFilename == "-") {
68 BaseArg = 2;
69 OutputFilename = InputFilenames[1];
Chris Lattner075a0b72001-10-13 07:06:23 +000070 }
71
Chris Lattner65be3212001-10-24 06:23:00 +000072 std::auto_ptr<Module> Composite(LoadFile(InputFilenames[BaseArg]));
73 if (Composite.get() == 0) return 1;
74
75 for (unsigned i = BaseArg+1; i < InputFilenames.size(); ++i) {
76 auto_ptr<Module> M(LoadFile(InputFilenames[i]));
77 if (M.get() == 0) return 1;
Chris Lattnerb81adf12001-10-23 20:44:55 +000078
79 if (Verbose) cerr << "Linking in '" << InputFilenames[i] << "'\n";
80
Chris Lattner075a0b72001-10-13 07:06:23 +000081 if (LinkModules(Composite.get(), M.get(), &ErrorMessage)) {
82 cerr << "Error linking in '" << InputFilenames[i] << "': "
83 << ErrorMessage << endl;
84 return 1;
85 }
86 }
87
Chris Lattner164cb692001-10-14 23:23:33 +000088 if (DumpAsm)
89 cerr << "Here's the assembly:\n" << Composite.get();
90
Chris Lattner075a0b72001-10-13 07:06:23 +000091 ostream *Out = &cout; // Default to printing to stdout...
92 if (OutputFilename != "-") {
93 Out = new ofstream(OutputFilename.c_str(),
94 (Force ? 0 : ios::noreplace)|ios::out);
95 if (!Out->good()) {
96 cerr << "Error opening '" << OutputFilename << "'!\n";
97 return 1;
98 }
99 }
100
Chris Lattnerb81adf12001-10-23 20:44:55 +0000101 if (Verbose) cerr << "Writing bytecode...\n";
Chris Lattner075a0b72001-10-13 07:06:23 +0000102 WriteBytecodeToFile(Composite.get(), *Out);
103
104 if (Out != &cout) delete Out;
105 return 0;
106}