blob: 28499f19881b5bea9bea0f2e4e7d6cc257d67742 [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
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 Lattner952d3652001-12-08 20:31:32 +000032
33// FileExists - Return true if the specified string is an openable file...
Chris Lattner697954c2002-01-20 22:54:45 +000034static inline bool FileExists(const std::string &FN) {
Chris Lattner952d3652001-12-08 20:31:32 +000035 struct stat StatBuf;
36 return stat(FN.c_str(), &StatBuf) != -1;
37}
38
39// LoadFile - Read the specified bytecode file in and return it. This routine
40// searches the link path for the specified file to try to find it...
41//
Chris Lattner697954c2002-01-20 22:54:45 +000042static inline std::auto_ptr<Module> LoadFile(const std::string &FN) {
43 std::string Filename = FN;
44 std::string ErrorMessage;
Chris Lattner65be3212001-10-24 06:23:00 +000045
46 unsigned NextLibPathIdx = 0;
Chris Lattner952d3652001-12-08 20:31:32 +000047 bool FoundAFile = false;
Chris Lattner65be3212001-10-24 06:23:00 +000048
49 while (1) {
50 if (Verbose) cerr << "Loading '" << Filename << "'\n";
Chris Lattner952d3652001-12-08 20:31:32 +000051 if (FileExists(Filename)) FoundAFile = true;
Chris Lattner65be3212001-10-24 06:23:00 +000052 Module *Result = ParseBytecodeFile(Filename, &ErrorMessage);
53 if (Result) return std::auto_ptr<Module>(Result); // Load successful!
54
55 if (Verbose) {
56 cerr << "Error opening bytecode file: '" << Filename << "'";
57 if (ErrorMessage.size()) cerr << ": " << ErrorMessage;
Chris Lattner5bdf1612002-04-07 22:34:44 +000058 cerr << "\n";
Chris Lattner65be3212001-10-24 06:23:00 +000059 }
60
61 if (NextLibPathIdx == LibPaths.size()) break;
62 Filename = LibPaths[NextLibPathIdx++] + "/" + FN;
63 }
64
Chris Lattner952d3652001-12-08 20:31:32 +000065 if (FoundAFile)
66 cerr << "Bytecode file '" << FN << "' corrupt! "
67 << "Use 'link -v ...' for more info.\n";
68 else
69 cerr << "Could not locate bytecode file: '" << FN << "'\n";
Chris Lattner65be3212001-10-24 06:23:00 +000070 return std::auto_ptr<Module>();
71}
Chris Lattner075a0b72001-10-13 07:06:23 +000072
Chris Lattner952d3652001-12-08 20:31:32 +000073
74
75
Chris Lattner075a0b72001-10-13 07:06:23 +000076int main(int argc, char **argv) {
Chris Lattnerde3b8622001-11-26 19:18:30 +000077 cl::ParseCommandLineOptions(argc, argv, " llvm linker\n",
78 cl::EnableSingleLetterArgValue |
79 cl::DisableSingleLetterArgGrouping);
Chris Lattner075a0b72001-10-13 07:06:23 +000080 assert(InputFilenames.size() > 0 && "OneOrMore is not working");
81
Chris Lattner65be3212001-10-24 06:23:00 +000082 unsigned BaseArg = 0;
Chris Lattner697954c2002-01-20 22:54:45 +000083 std::string ErrorMessage;
Chris Lattner65be3212001-10-24 06:23:00 +000084
85 // TODO: TEST argv[0] for llvm-ar forms... for now, this is a huge hack.
86 if (InputFilenames.size() >= 3 && InputFilenames[0] == "rc" &&
87 OutputFilename == "-") {
88 BaseArg = 2;
89 OutputFilename = InputFilenames[1];
Chris Lattner075a0b72001-10-13 07:06:23 +000090 }
91
Chris Lattner65be3212001-10-24 06:23:00 +000092 std::auto_ptr<Module> Composite(LoadFile(InputFilenames[BaseArg]));
93 if (Composite.get() == 0) return 1;
94
95 for (unsigned i = BaseArg+1; i < InputFilenames.size(); ++i) {
Chris Lattner697954c2002-01-20 22:54:45 +000096 std::auto_ptr<Module> M(LoadFile(InputFilenames[i]));
Chris Lattner65be3212001-10-24 06:23:00 +000097 if (M.get() == 0) return 1;
Chris Lattnerb81adf12001-10-23 20:44:55 +000098
99 if (Verbose) cerr << "Linking in '" << InputFilenames[i] << "'\n";
100
Chris Lattner075a0b72001-10-13 07:06:23 +0000101 if (LinkModules(Composite.get(), M.get(), &ErrorMessage)) {
102 cerr << "Error linking in '" << InputFilenames[i] << "': "
Chris Lattner5bdf1612002-04-07 22:34:44 +0000103 << ErrorMessage << "\n";
Chris Lattner075a0b72001-10-13 07:06:23 +0000104 return 1;
105 }
106 }
107
Chris Lattnerd43035e2002-04-28 05:13:45 +0000108 if (DumpAsm) cerr << "Here's the assembly:\n" << Composite.get();
Chris Lattner164cb692001-10-14 23:23:33 +0000109
Anand Shuklacf17bcc2002-06-25 21:57:48 +0000110 std::ostream *Out = &std::cout; // Default to printing to stdout...
Chris Lattner075a0b72001-10-13 07:06:23 +0000111 if (OutputFilename != "-") {
Chris Lattner888912d2002-01-22 21:07:24 +0000112 if (!Force && std::ifstream(OutputFilename.c_str())) {
Chris Lattner697954c2002-01-20 22:54:45 +0000113 // If force is not specified, make sure not to overwrite a file!
114 cerr << "Error opening '" << OutputFilename << "': File exists!\n"
115 << "Use -f command line argument to force output\n";
116 return 1;
117 }
118 Out = new std::ofstream(OutputFilename.c_str());
Chris Lattner075a0b72001-10-13 07:06:23 +0000119 if (!Out->good()) {
120 cerr << "Error opening '" << OutputFilename << "'!\n";
121 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 Lattnerb81adf12001-10-23 20:44:55 +0000129 if (Verbose) cerr << "Writing bytecode...\n";
Chris Lattner075a0b72001-10-13 07:06:23 +0000130 WriteBytecodeToFile(Composite.get(), *Out);
131
Chris Lattner697954c2002-01-20 22:54:45 +0000132 if (Out != &std::cout) delete Out;
Chris Lattner075a0b72001-10-13 07:06:23 +0000133 return 0;
134}