blob: ad4cbdbe8cb2c7154aa9a95d62a981ca12000923 [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
Chris Lattner697954c2002-01-20 22:54:45 +000022typedef int blksize_t; // SYS/TYPES is broken!!!
Chris Lattner952d3652001-12-08 20:31:32 +000023#include <sys/stat.h>
Chris Lattner075a0b72001-10-13 07:06:23 +000024
25
26cl::StringList InputFilenames("", "Load <arg> files, linking them together",
27 cl::OneOrMore);
28cl::String OutputFilename("o", "Override output filename", cl::NoFlags, "-");
29cl::Flag Force ("f", "Overwrite output files", cl::NoFlags, false);
Chris Lattnerb81adf12001-10-23 20:44:55 +000030cl::Flag Verbose ("v", "Print information about actions taken");
Chris Lattner164cb692001-10-14 23:23:33 +000031cl::Flag DumpAsm ("d", "Print assembly as linked", cl::Hidden, false);
Chris Lattner65be3212001-10-24 06:23:00 +000032cl::StringList LibPaths ("L", "Specify a library search path", cl::ZeroOrMore);
Chris Lattner075a0b72001-10-13 07:06:23 +000033
Chris Lattner952d3652001-12-08 20:31:32 +000034
35// FileExists - Return true if the specified string is an openable file...
Chris Lattner697954c2002-01-20 22:54:45 +000036static inline bool FileExists(const std::string &FN) {
Chris Lattner952d3652001-12-08 20:31:32 +000037 struct stat StatBuf;
38 return stat(FN.c_str(), &StatBuf) != -1;
39}
40
41// LoadFile - Read the specified bytecode file in and return it. This routine
42// searches the link path for the specified file to try to find it...
43//
Chris Lattner697954c2002-01-20 22:54:45 +000044static inline std::auto_ptr<Module> LoadFile(const std::string &FN) {
45 std::string Filename = FN;
46 std::string ErrorMessage;
Chris Lattner65be3212001-10-24 06:23:00 +000047
48 unsigned NextLibPathIdx = 0;
Chris Lattner952d3652001-12-08 20:31:32 +000049 bool FoundAFile = false;
Chris Lattner65be3212001-10-24 06:23:00 +000050
51 while (1) {
52 if (Verbose) cerr << "Loading '" << Filename << "'\n";
Chris Lattner952d3652001-12-08 20:31:32 +000053 if (FileExists(Filename)) FoundAFile = true;
Chris Lattner65be3212001-10-24 06:23:00 +000054 Module *Result = ParseBytecodeFile(Filename, &ErrorMessage);
55 if (Result) return std::auto_ptr<Module>(Result); // Load successful!
56
57 if (Verbose) {
58 cerr << "Error opening bytecode file: '" << Filename << "'";
59 if (ErrorMessage.size()) cerr << ": " << ErrorMessage;
60 cerr << endl;
61 }
62
63 if (NextLibPathIdx == LibPaths.size()) break;
64 Filename = LibPaths[NextLibPathIdx++] + "/" + FN;
65 }
66
Chris Lattner952d3652001-12-08 20:31:32 +000067 if (FoundAFile)
68 cerr << "Bytecode file '" << FN << "' corrupt! "
69 << "Use 'link -v ...' for more info.\n";
70 else
71 cerr << "Could not locate bytecode file: '" << FN << "'\n";
Chris Lattner65be3212001-10-24 06:23:00 +000072 return std::auto_ptr<Module>();
73}
Chris Lattner075a0b72001-10-13 07:06:23 +000074
Chris Lattner952d3652001-12-08 20:31:32 +000075
76
77
Chris Lattner075a0b72001-10-13 07:06:23 +000078int main(int argc, char **argv) {
Chris Lattnerde3b8622001-11-26 19:18:30 +000079 cl::ParseCommandLineOptions(argc, argv, " llvm linker\n",
80 cl::EnableSingleLetterArgValue |
81 cl::DisableSingleLetterArgGrouping);
Chris Lattner075a0b72001-10-13 07:06:23 +000082 assert(InputFilenames.size() > 0 && "OneOrMore is not working");
83
Chris Lattner65be3212001-10-24 06:23:00 +000084 unsigned BaseArg = 0;
Chris Lattner697954c2002-01-20 22:54:45 +000085 std::string ErrorMessage;
Chris Lattner65be3212001-10-24 06:23:00 +000086
87 // TODO: TEST argv[0] for llvm-ar forms... for now, this is a huge hack.
88 if (InputFilenames.size() >= 3 && InputFilenames[0] == "rc" &&
89 OutputFilename == "-") {
90 BaseArg = 2;
91 OutputFilename = InputFilenames[1];
Chris Lattner075a0b72001-10-13 07:06:23 +000092 }
93
Chris Lattner65be3212001-10-24 06:23:00 +000094 std::auto_ptr<Module> Composite(LoadFile(InputFilenames[BaseArg]));
95 if (Composite.get() == 0) return 1;
96
97 for (unsigned i = BaseArg+1; i < InputFilenames.size(); ++i) {
Chris Lattner697954c2002-01-20 22:54:45 +000098 std::auto_ptr<Module> M(LoadFile(InputFilenames[i]));
Chris Lattner65be3212001-10-24 06:23:00 +000099 if (M.get() == 0) return 1;
Chris Lattnerb81adf12001-10-23 20:44:55 +0000100
101 if (Verbose) cerr << "Linking in '" << InputFilenames[i] << "'\n";
102
Chris Lattner075a0b72001-10-13 07:06:23 +0000103 if (LinkModules(Composite.get(), M.get(), &ErrorMessage)) {
104 cerr << "Error linking in '" << InputFilenames[i] << "': "
105 << ErrorMessage << endl;
106 return 1;
107 }
108 }
109
Chris Lattner164cb692001-10-14 23:23:33 +0000110 if (DumpAsm)
111 cerr << "Here's the assembly:\n" << Composite.get();
112
Chris Lattner075a0b72001-10-13 07:06:23 +0000113 ostream *Out = &cout; // Default to printing to stdout...
114 if (OutputFilename != "-") {
Chris Lattner697954c2002-01-20 22:54:45 +0000115 if (!Force && !std::ifstream(OutputFilename.c_str())) {
116 // If force is not specified, make sure not to overwrite a file!
117 cerr << "Error opening '" << OutputFilename << "': File exists!\n"
118 << "Use -f command line argument to force output\n";
119 return 1;
120 }
121 Out = new std::ofstream(OutputFilename.c_str());
Chris Lattner075a0b72001-10-13 07:06:23 +0000122 if (!Out->good()) {
123 cerr << "Error opening '" << OutputFilename << "'!\n";
124 return 1;
125 }
126 }
127
Chris Lattnerb81adf12001-10-23 20:44:55 +0000128 if (Verbose) cerr << "Writing bytecode...\n";
Chris Lattner075a0b72001-10-13 07:06:23 +0000129 WriteBytecodeToFile(Composite.get(), *Out);
130
Chris Lattner697954c2002-01-20 22:54:45 +0000131 if (Out != &std::cout) delete Out;
Chris Lattner075a0b72001-10-13 07:06:23 +0000132 return 0;
133}