blob: 7f48cc4b7b129c2e826c3e50d2245c6e7189d7b0 [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
Chris Lattnerc8cc4cb2002-05-07 18:36:35 +000012#include "llvm/Transforms/Utils/Linker.h"
Chris Lattner075a0b72001-10-13 07:06:23 +000013#include "llvm/Bytecode/Reader.h"
14#include "llvm/Bytecode/Writer.h"
Chris Lattner075a0b72001-10-13 07:06:23 +000015#include "llvm/Module.h"
Chris Lattnercee8f9a2001-11-27 00:03:19 +000016#include "Support/CommandLine.h"
Chris Lattner76d12292002-04-18 19:55:25 +000017#include "Support/Signals.h"
Chris Lattnercee8f9a2001-11-27 00:03:19 +000018#include <fstream>
Chris Lattner075a0b72001-10-13 07:06:23 +000019#include <memory>
Chris Lattner952d3652001-12-08 20:31:32 +000020#include <sys/types.h> // For FileExists
21#include <sys/stat.h>
Chris Lattner075a0b72001-10-13 07:06:23 +000022
Anand Shuklacf17bcc2002-06-25 21:57:48 +000023using std::cerr;
Chris Lattner075a0b72001-10-13 07:06:23 +000024
Chris Lattner5ff62e92002-07-22 02:10:13 +000025static cl::list<std::string>
26InputFilenames(cl::Positional, cl::OneOrMore,
Chris Lattner54e05af2002-07-22 02:18:09 +000027 cl::desc("<input bytecode files>"));
Chris Lattner5ff62e92002-07-22 02:10:13 +000028
29static cl::opt<std::string>
30OutputFilename("o", cl::desc("Override output filename"), cl::init("-"),
31 cl::value_desc("filename"));
32
33static cl::opt<bool> Force("f", cl::desc("Overwrite output files"));
34
35static cl::opt<bool>
36Verbose("v", cl::desc("Print information about actions taken"));
37
38static cl::opt<bool>
39DumpAsm("d", cl::desc("Print assembly as linked"), cl::Hidden);
40
41static cl::list<std::string>
42LibPaths("L", cl::desc("Specify a library search path"), cl::ZeroOrMore,
43 cl::value_desc("directory"), cl::Prefix);
Chris Lattner952d3652001-12-08 20:31:32 +000044
45// FileExists - Return true if the specified string is an openable file...
Chris Lattner697954c2002-01-20 22:54:45 +000046static inline bool FileExists(const std::string &FN) {
Chris Lattner952d3652001-12-08 20:31:32 +000047 struct stat StatBuf;
48 return stat(FN.c_str(), &StatBuf) != -1;
49}
50
51// LoadFile - Read the specified bytecode file in and return it. This routine
52// searches the link path for the specified file to try to find it...
53//
Chris Lattner697954c2002-01-20 22:54:45 +000054static inline std::auto_ptr<Module> LoadFile(const std::string &FN) {
55 std::string Filename = FN;
56 std::string ErrorMessage;
Chris Lattner65be3212001-10-24 06:23:00 +000057
58 unsigned NextLibPathIdx = 0;
Chris Lattner952d3652001-12-08 20:31:32 +000059 bool FoundAFile = false;
Chris Lattner65be3212001-10-24 06:23:00 +000060
61 while (1) {
62 if (Verbose) cerr << "Loading '" << Filename << "'\n";
Chris Lattner952d3652001-12-08 20:31:32 +000063 if (FileExists(Filename)) FoundAFile = true;
Chris Lattner65be3212001-10-24 06:23:00 +000064 Module *Result = ParseBytecodeFile(Filename, &ErrorMessage);
65 if (Result) return std::auto_ptr<Module>(Result); // Load successful!
66
67 if (Verbose) {
68 cerr << "Error opening bytecode file: '" << Filename << "'";
69 if (ErrorMessage.size()) cerr << ": " << ErrorMessage;
Chris Lattner5bdf1612002-04-07 22:34:44 +000070 cerr << "\n";
Chris Lattner65be3212001-10-24 06:23:00 +000071 }
72
73 if (NextLibPathIdx == LibPaths.size()) break;
74 Filename = LibPaths[NextLibPathIdx++] + "/" + FN;
75 }
76
Chris Lattner952d3652001-12-08 20:31:32 +000077 if (FoundAFile)
78 cerr << "Bytecode file '" << FN << "' corrupt! "
79 << "Use 'link -v ...' for more info.\n";
80 else
81 cerr << "Could not locate bytecode file: '" << FN << "'\n";
Chris Lattner65be3212001-10-24 06:23:00 +000082 return std::auto_ptr<Module>();
83}
Chris Lattner075a0b72001-10-13 07:06:23 +000084
Chris Lattner952d3652001-12-08 20:31:32 +000085
86
87
Chris Lattner075a0b72001-10-13 07:06:23 +000088int main(int argc, char **argv) {
Chris Lattner5ff62e92002-07-22 02:10:13 +000089 cl::ParseCommandLineOptions(argc, argv, " llvm linker\n");
Chris Lattner075a0b72001-10-13 07:06:23 +000090 assert(InputFilenames.size() > 0 && "OneOrMore is not working");
91
Chris Lattner65be3212001-10-24 06:23:00 +000092 unsigned BaseArg = 0;
Chris Lattner697954c2002-01-20 22:54:45 +000093 std::string ErrorMessage;
Chris Lattner65be3212001-10-24 06:23:00 +000094
95 // TODO: TEST argv[0] for llvm-ar forms... for now, this is a huge hack.
96 if (InputFilenames.size() >= 3 && InputFilenames[0] == "rc" &&
97 OutputFilename == "-") {
98 BaseArg = 2;
99 OutputFilename = InputFilenames[1];
Chris Lattner075a0b72001-10-13 07:06:23 +0000100 }
101
Chris Lattner65be3212001-10-24 06:23:00 +0000102 std::auto_ptr<Module> Composite(LoadFile(InputFilenames[BaseArg]));
103 if (Composite.get() == 0) return 1;
104
105 for (unsigned i = BaseArg+1; i < InputFilenames.size(); ++i) {
Chris Lattner697954c2002-01-20 22:54:45 +0000106 std::auto_ptr<Module> M(LoadFile(InputFilenames[i]));
Chris Lattner65be3212001-10-24 06:23:00 +0000107 if (M.get() == 0) return 1;
Chris Lattnerb81adf12001-10-23 20:44:55 +0000108
109 if (Verbose) cerr << "Linking in '" << InputFilenames[i] << "'\n";
110
Chris Lattner075a0b72001-10-13 07:06:23 +0000111 if (LinkModules(Composite.get(), M.get(), &ErrorMessage)) {
112 cerr << "Error linking in '" << InputFilenames[i] << "': "
Chris Lattner5bdf1612002-04-07 22:34:44 +0000113 << ErrorMessage << "\n";
Chris Lattner075a0b72001-10-13 07:06:23 +0000114 return 1;
115 }
116 }
117
Chris Lattnerd43035e2002-04-28 05:13:45 +0000118 if (DumpAsm) cerr << "Here's the assembly:\n" << Composite.get();
Chris Lattner164cb692001-10-14 23:23:33 +0000119
Anand Shuklacf17bcc2002-06-25 21:57:48 +0000120 std::ostream *Out = &std::cout; // Default to printing to stdout...
Chris Lattner075a0b72001-10-13 07:06:23 +0000121 if (OutputFilename != "-") {
Chris Lattner888912d2002-01-22 21:07:24 +0000122 if (!Force && std::ifstream(OutputFilename.c_str())) {
Chris Lattner697954c2002-01-20 22:54:45 +0000123 // If force is not specified, make sure not to overwrite a file!
124 cerr << "Error opening '" << OutputFilename << "': File exists!\n"
125 << "Use -f command line argument to force output\n";
126 return 1;
127 }
128 Out = new std::ofstream(OutputFilename.c_str());
Chris Lattner075a0b72001-10-13 07:06:23 +0000129 if (!Out->good()) {
130 cerr << "Error opening '" << OutputFilename << "'!\n";
131 return 1;
132 }
Chris Lattner76d12292002-04-18 19:55:25 +0000133
134 // Make sure that the Out file gets unlink'd from the disk if we get a
135 // SIGINT
136 RemoveFileOnSignal(OutputFilename);
Chris Lattner075a0b72001-10-13 07:06:23 +0000137 }
138
Chris Lattnerb81adf12001-10-23 20:44:55 +0000139 if (Verbose) cerr << "Writing bytecode...\n";
Chris Lattner075a0b72001-10-13 07:06:23 +0000140 WriteBytecodeToFile(Composite.get(), *Out);
141
Chris Lattner697954c2002-01-20 22:54:45 +0000142 if (Out != &std::cout) delete Out;
Chris Lattner075a0b72001-10-13 07:06:23 +0000143 return 0;
144}