blob: 5c9e21c0b0ef1628f3e899ea1800f8a01ab2a8d0 [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//
7//===----------------------------------------------------------------------===//
8
Chris Lattnera55c4b12003-08-28 16:25:34 +00009#include "llvm/Module.h"
10#include "llvm/Analysis/Verifier.h"
Chris Lattner075a0b72001-10-13 07:06:23 +000011#include "llvm/Bytecode/Reader.h"
12#include "llvm/Bytecode/Writer.h"
Chris Lattnera55c4b12003-08-28 16:25:34 +000013#include "llvm/Transforms/Utils/Linker.h"
Chris Lattnercee8f9a2001-11-27 00:03:19 +000014#include "Support/CommandLine.h"
Chris Lattner76d12292002-04-18 19:55:25 +000015#include "Support/Signals.h"
Chris Lattnercee8f9a2001-11-27 00:03:19 +000016#include <fstream>
Chris Lattner075a0b72001-10-13 07:06:23 +000017#include <memory>
Chris Lattner952d3652001-12-08 20:31:32 +000018#include <sys/types.h> // For FileExists
19#include <sys/stat.h>
Chris Lattner075a0b72001-10-13 07:06:23 +000020
Chris Lattner5ff62e92002-07-22 02:10:13 +000021static cl::list<std::string>
22InputFilenames(cl::Positional, cl::OneOrMore,
Chris Lattner54e05af2002-07-22 02:18:09 +000023 cl::desc("<input bytecode files>"));
Chris Lattner5ff62e92002-07-22 02:10:13 +000024
25static cl::opt<std::string>
26OutputFilename("o", cl::desc("Override output filename"), cl::init("-"),
27 cl::value_desc("filename"));
28
29static cl::opt<bool> Force("f", cl::desc("Overwrite output files"));
30
31static cl::opt<bool>
32Verbose("v", cl::desc("Print information about actions taken"));
33
34static cl::opt<bool>
35DumpAsm("d", cl::desc("Print assembly as linked"), cl::Hidden);
36
37static cl::list<std::string>
38LibPaths("L", cl::desc("Specify a library search path"), cl::ZeroOrMore,
39 cl::value_desc("directory"), cl::Prefix);
Chris Lattner952d3652001-12-08 20:31:32 +000040
41// FileExists - Return true if the specified string is an openable file...
Chris Lattner697954c2002-01-20 22:54:45 +000042static inline bool FileExists(const std::string &FN) {
Chris Lattner952d3652001-12-08 20:31:32 +000043 struct stat StatBuf;
44 return stat(FN.c_str(), &StatBuf) != -1;
45}
46
47// LoadFile - Read the specified bytecode file in and return it. This routine
48// searches the link path for the specified file to try to find it...
49//
Chris Lattner697954c2002-01-20 22:54:45 +000050static inline std::auto_ptr<Module> LoadFile(const std::string &FN) {
51 std::string Filename = FN;
52 std::string ErrorMessage;
Chris Lattner65be3212001-10-24 06:23:00 +000053
54 unsigned NextLibPathIdx = 0;
Chris Lattner952d3652001-12-08 20:31:32 +000055 bool FoundAFile = false;
Chris Lattner65be3212001-10-24 06:23:00 +000056
57 while (1) {
Chris Lattner6c8103f2003-05-22 20:13:16 +000058 if (Verbose) std::cerr << "Loading '" << Filename << "'\n";
Chris Lattner952d3652001-12-08 20:31:32 +000059 if (FileExists(Filename)) FoundAFile = true;
Chris Lattner65be3212001-10-24 06:23:00 +000060 Module *Result = ParseBytecodeFile(Filename, &ErrorMessage);
61 if (Result) return std::auto_ptr<Module>(Result); // Load successful!
62
63 if (Verbose) {
Chris Lattner6c8103f2003-05-22 20:13:16 +000064 std::cerr << "Error opening bytecode file: '" << Filename << "'";
65 if (ErrorMessage.size()) std::cerr << ": " << ErrorMessage;
66 std::cerr << "\n";
Chris Lattner65be3212001-10-24 06:23:00 +000067 }
68
69 if (NextLibPathIdx == LibPaths.size()) break;
70 Filename = LibPaths[NextLibPathIdx++] + "/" + FN;
71 }
72
Chris Lattner952d3652001-12-08 20:31:32 +000073 if (FoundAFile)
Chris Lattner6c8103f2003-05-22 20:13:16 +000074 std::cerr << "Bytecode file '" << FN << "' corrupt! "
75 << "Use 'link -v ...' for more info.\n";
Chris Lattner952d3652001-12-08 20:31:32 +000076 else
Chris Lattner6c8103f2003-05-22 20:13:16 +000077 std::cerr << "Could not locate bytecode file: '" << FN << "'\n";
Chris Lattner65be3212001-10-24 06:23:00 +000078 return std::auto_ptr<Module>();
79}
Chris Lattner075a0b72001-10-13 07:06:23 +000080
Chris Lattner952d3652001-12-08 20:31:32 +000081
82
83
Chris Lattner075a0b72001-10-13 07:06:23 +000084int main(int argc, char **argv) {
Chris Lattner5ff62e92002-07-22 02:10:13 +000085 cl::ParseCommandLineOptions(argc, argv, " llvm linker\n");
Chris Lattner075a0b72001-10-13 07:06:23 +000086 assert(InputFilenames.size() > 0 && "OneOrMore is not working");
87
Chris Lattner65be3212001-10-24 06:23:00 +000088 unsigned BaseArg = 0;
Chris Lattner697954c2002-01-20 22:54:45 +000089 std::string ErrorMessage;
Chris Lattner65be3212001-10-24 06:23:00 +000090
Chris Lattner65be3212001-10-24 06:23:00 +000091 std::auto_ptr<Module> Composite(LoadFile(InputFilenames[BaseArg]));
92 if (Composite.get() == 0) return 1;
93
94 for (unsigned i = BaseArg+1; i < InputFilenames.size(); ++i) {
Chris Lattner697954c2002-01-20 22:54:45 +000095 std::auto_ptr<Module> M(LoadFile(InputFilenames[i]));
Chris Lattner65be3212001-10-24 06:23:00 +000096 if (M.get() == 0) return 1;
Chris Lattnerb81adf12001-10-23 20:44:55 +000097
Chris Lattner6c8103f2003-05-22 20:13:16 +000098 if (Verbose) std::cerr << "Linking in '" << InputFilenames[i] << "'\n";
Chris Lattnerb81adf12001-10-23 20:44:55 +000099
Chris Lattner075a0b72001-10-13 07:06:23 +0000100 if (LinkModules(Composite.get(), M.get(), &ErrorMessage)) {
Chris Lattner6c8103f2003-05-22 20:13:16 +0000101 std::cerr << argv[0] << ": error linking in '" << InputFilenames[i]
102 << "': " << ErrorMessage << "\n";
Chris Lattner075a0b72001-10-13 07:06:23 +0000103 return 1;
104 }
105 }
106
Chris Lattner6c8103f2003-05-22 20:13:16 +0000107 if (DumpAsm) std::cerr << "Here's the assembly:\n" << Composite.get();
Chris Lattner164cb692001-10-14 23:23:33 +0000108
Anand Shuklacf17bcc2002-06-25 21:57:48 +0000109 std::ostream *Out = &std::cout; // Default to printing to stdout...
Chris Lattner4a4daba2003-06-13 16:10:26 +0000110 if (OutputFilename != "-") {
Chris Lattner888912d2002-01-22 21:07:24 +0000111 if (!Force && std::ifstream(OutputFilename.c_str())) {
Chris Lattner697954c2002-01-20 22:54:45 +0000112 // If force is not specified, make sure not to overwrite a file!
Chris Lattner6c8103f2003-05-22 20:13:16 +0000113 std::cerr << argv[0] << ": error opening '" << OutputFilename
114 << "': file exists!\n"
115 << "Use -f command line argument to force output\n";
Chris Lattner697954c2002-01-20 22:54:45 +0000116 return 1;
117 }
118 Out = new std::ofstream(OutputFilename.c_str());
Chris Lattner075a0b72001-10-13 07:06:23 +0000119 if (!Out->good()) {
Chris Lattner6c8103f2003-05-22 20:13:16 +0000120 std::cerr << argv[0] << ": error opening '" << OutputFilename << "'!\n";
Chris Lattner075a0b72001-10-13 07:06:23 +0000121 return 1;
122 }
Chris Lattner76d12292002-04-18 19:55:25 +0000123
124 // Make sure that the Out file gets unlink'd from the disk if we get a
125 // SIGINT
126 RemoveFileOnSignal(OutputFilename);
Chris Lattner075a0b72001-10-13 07:06:23 +0000127 }
128
Chris Lattnera55c4b12003-08-28 16:25:34 +0000129 if (verifyModule(*Composite.get())) {
130 std::cerr << argv[0] << ": linked module is broken!\n";
131 return 1;
132 }
133
Chris Lattner6c8103f2003-05-22 20:13:16 +0000134 if (Verbose) std::cerr << "Writing bytecode...\n";
Chris Lattner075a0b72001-10-13 07:06:23 +0000135 WriteBytecodeToFile(Composite.get(), *Out);
136
Chris Lattner697954c2002-01-20 22:54:45 +0000137 if (Out != &std::cout) delete Out;
Chris Lattner075a0b72001-10-13 07:06:23 +0000138 return 0;
139}