blob: 83141c86e7d438edd3e0965b86390c361a9c5e8b [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/Module.h"
17#include "llvm/Method.h"
Chris Lattnercee8f9a2001-11-27 00:03:19 +000018#include "Support/CommandLine.h"
19#include <fstream>
Chris Lattner075a0b72001-10-13 07:06:23 +000020#include <memory>
Chris Lattner952d3652001-12-08 20:31:32 +000021#include <sys/types.h> // For FileExists
22#include <sys/stat.h>
Chris Lattner075a0b72001-10-13 07:06:23 +000023
24
25cl::StringList InputFilenames("", "Load <arg> files, linking them together",
26 cl::OneOrMore);
27cl::String OutputFilename("o", "Override output filename", cl::NoFlags, "-");
28cl::Flag Force ("f", "Overwrite output files", cl::NoFlags, false);
Chris Lattnerb81adf12001-10-23 20:44:55 +000029cl::Flag Verbose ("v", "Print information about actions taken");
Chris Lattner164cb692001-10-14 23:23:33 +000030cl::Flag DumpAsm ("d", "Print assembly as linked", cl::Hidden, false);
Chris Lattner65be3212001-10-24 06:23:00 +000031cl::StringList LibPaths ("L", "Specify a library search path", cl::ZeroOrMore);
Chris Lattner075a0b72001-10-13 07:06:23 +000032
Chris Lattner952d3652001-12-08 20:31:32 +000033
34// FileExists - Return true if the specified string is an openable file...
35static inline bool FileExists(const string &FN) {
36 struct stat StatBuf;
37 return stat(FN.c_str(), &StatBuf) != -1;
38}
39
40// LoadFile - Read the specified bytecode file in and return it. This routine
41// searches the link path for the specified file to try to find it...
42//
Chris Lattner65be3212001-10-24 06:23:00 +000043static inline std::auto_ptr<Module> LoadFile(const string &FN) {
44 string Filename = FN;
45 string ErrorMessage;
46
47 unsigned NextLibPathIdx = 0;
Chris Lattner952d3652001-12-08 20:31:32 +000048 bool FoundAFile = false;
Chris Lattner65be3212001-10-24 06:23:00 +000049
50 while (1) {
51 if (Verbose) cerr << "Loading '" << Filename << "'\n";
Chris Lattner952d3652001-12-08 20:31:32 +000052 if (FileExists(Filename)) FoundAFile = true;
Chris Lattner65be3212001-10-24 06:23:00 +000053 Module *Result = ParseBytecodeFile(Filename, &ErrorMessage);
54 if (Result) return std::auto_ptr<Module>(Result); // Load successful!
55
56 if (Verbose) {
57 cerr << "Error opening bytecode file: '" << Filename << "'";
58 if (ErrorMessage.size()) cerr << ": " << ErrorMessage;
59 cerr << endl;
60 }
61
62 if (NextLibPathIdx == LibPaths.size()) break;
63 Filename = LibPaths[NextLibPathIdx++] + "/" + FN;
64 }
65
Chris Lattner952d3652001-12-08 20:31:32 +000066 if (FoundAFile)
67 cerr << "Bytecode file '" << FN << "' corrupt! "
68 << "Use 'link -v ...' for more info.\n";
69 else
70 cerr << "Could not locate bytecode file: '" << FN << "'\n";
Chris Lattner65be3212001-10-24 06:23:00 +000071 return std::auto_ptr<Module>();
72}
Chris Lattner075a0b72001-10-13 07:06:23 +000073
Chris Lattner952d3652001-12-08 20:31:32 +000074
75
76
Chris Lattner075a0b72001-10-13 07:06:23 +000077int main(int argc, char **argv) {
Chris Lattnerde3b8622001-11-26 19:18:30 +000078 cl::ParseCommandLineOptions(argc, argv, " llvm linker\n",
79 cl::EnableSingleLetterArgValue |
80 cl::DisableSingleLetterArgGrouping);
Chris Lattner075a0b72001-10-13 07:06:23 +000081 assert(InputFilenames.size() > 0 && "OneOrMore is not working");
82
Chris Lattner65be3212001-10-24 06:23:00 +000083 unsigned BaseArg = 0;
Chris Lattnerd6b65252001-10-24 01:15:12 +000084 string ErrorMessage;
Chris Lattner65be3212001-10-24 06:23:00 +000085
86 // TODO: TEST argv[0] for llvm-ar forms... for now, this is a huge hack.
87 if (InputFilenames.size() >= 3 && InputFilenames[0] == "rc" &&
88 OutputFilename == "-") {
89 BaseArg = 2;
90 OutputFilename = InputFilenames[1];
Chris Lattner075a0b72001-10-13 07:06:23 +000091 }
92
Chris Lattner65be3212001-10-24 06:23:00 +000093 std::auto_ptr<Module> Composite(LoadFile(InputFilenames[BaseArg]));
94 if (Composite.get() == 0) return 1;
95
96 for (unsigned i = BaseArg+1; i < InputFilenames.size(); ++i) {
97 auto_ptr<Module> M(LoadFile(InputFilenames[i]));
98 if (M.get() == 0) return 1;
Chris Lattnerb81adf12001-10-23 20:44:55 +000099
100 if (Verbose) cerr << "Linking in '" << InputFilenames[i] << "'\n";
101
Chris Lattner075a0b72001-10-13 07:06:23 +0000102 if (LinkModules(Composite.get(), M.get(), &ErrorMessage)) {
103 cerr << "Error linking in '" << InputFilenames[i] << "': "
104 << ErrorMessage << endl;
105 return 1;
106 }
107 }
108
Chris Lattner164cb692001-10-14 23:23:33 +0000109 if (DumpAsm)
110 cerr << "Here's the assembly:\n" << Composite.get();
111
Chris Lattner075a0b72001-10-13 07:06:23 +0000112 ostream *Out = &cout; // Default to printing to stdout...
113 if (OutputFilename != "-") {
114 Out = new ofstream(OutputFilename.c_str(),
115 (Force ? 0 : ios::noreplace)|ios::out);
116 if (!Out->good()) {
117 cerr << "Error opening '" << OutputFilename << "'!\n";
118 return 1;
119 }
120 }
121
Chris Lattnerb81adf12001-10-23 20:44:55 +0000122 if (Verbose) cerr << "Writing bytecode...\n";
Chris Lattner075a0b72001-10-13 07:06:23 +0000123 WriteBytecodeToFile(Composite.get(), *Out);
124
125 if (Out != &cout) delete Out;
126 return 0;
127}