blob: 58959cfa41629b20bae0ff60c0882f99a7513df5 [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 Lattnerc8cc4cb2002-05-07 18:36:35 +00009#include "llvm/Transforms/Utils/Linker.h"
Chris Lattner075a0b72001-10-13 07:06:23 +000010#include "llvm/Bytecode/Reader.h"
11#include "llvm/Bytecode/Writer.h"
Chris Lattner075a0b72001-10-13 07:06:23 +000012#include "llvm/Module.h"
Chris Lattnercee8f9a2001-11-27 00:03:19 +000013#include "Support/CommandLine.h"
Chris Lattner76d12292002-04-18 19:55:25 +000014#include "Support/Signals.h"
Chris Lattnercee8f9a2001-11-27 00:03:19 +000015#include <fstream>
Chris Lattner075a0b72001-10-13 07:06:23 +000016#include <memory>
Chris Lattner952d3652001-12-08 20:31:32 +000017#include <sys/types.h> // For FileExists
18#include <sys/stat.h>
Chris Lattner075a0b72001-10-13 07:06:23 +000019
Chris Lattner5ff62e92002-07-22 02:10:13 +000020static cl::list<std::string>
21InputFilenames(cl::Positional, cl::OneOrMore,
Chris Lattner54e05af2002-07-22 02:18:09 +000022 cl::desc("<input bytecode files>"));
Chris Lattner5ff62e92002-07-22 02:10:13 +000023
24static cl::opt<std::string>
25OutputFilename("o", cl::desc("Override output filename"), cl::init("-"),
26 cl::value_desc("filename"));
27
28static cl::opt<bool> Force("f", cl::desc("Overwrite output files"));
29
30static cl::opt<bool>
31Verbose("v", cl::desc("Print information about actions taken"));
32
33static cl::opt<bool>
34DumpAsm("d", cl::desc("Print assembly as linked"), cl::Hidden);
35
36static cl::list<std::string>
37LibPaths("L", cl::desc("Specify a library search path"), cl::ZeroOrMore,
38 cl::value_desc("directory"), cl::Prefix);
Chris Lattner952d3652001-12-08 20:31:32 +000039
40// FileExists - Return true if the specified string is an openable file...
Chris Lattner697954c2002-01-20 22:54:45 +000041static inline bool FileExists(const std::string &FN) {
Chris Lattner952d3652001-12-08 20:31:32 +000042 struct stat StatBuf;
43 return stat(FN.c_str(), &StatBuf) != -1;
44}
45
46// LoadFile - Read the specified bytecode file in and return it. This routine
47// searches the link path for the specified file to try to find it...
48//
Chris Lattner697954c2002-01-20 22:54:45 +000049static inline std::auto_ptr<Module> LoadFile(const std::string &FN) {
50 std::string Filename = FN;
51 std::string ErrorMessage;
Chris Lattner65be3212001-10-24 06:23:00 +000052
53 unsigned NextLibPathIdx = 0;
Chris Lattner952d3652001-12-08 20:31:32 +000054 bool FoundAFile = false;
Chris Lattner65be3212001-10-24 06:23:00 +000055
56 while (1) {
Chris Lattner6c8103f2003-05-22 20:13:16 +000057 if (Verbose) std::cerr << "Loading '" << Filename << "'\n";
Chris Lattner952d3652001-12-08 20:31:32 +000058 if (FileExists(Filename)) FoundAFile = true;
Chris Lattner65be3212001-10-24 06:23:00 +000059 Module *Result = ParseBytecodeFile(Filename, &ErrorMessage);
60 if (Result) return std::auto_ptr<Module>(Result); // Load successful!
61
62 if (Verbose) {
Chris Lattner6c8103f2003-05-22 20:13:16 +000063 std::cerr << "Error opening bytecode file: '" << Filename << "'";
64 if (ErrorMessage.size()) std::cerr << ": " << ErrorMessage;
65 std::cerr << "\n";
Chris Lattner65be3212001-10-24 06:23:00 +000066 }
67
68 if (NextLibPathIdx == LibPaths.size()) break;
69 Filename = LibPaths[NextLibPathIdx++] + "/" + FN;
70 }
71
Chris Lattner952d3652001-12-08 20:31:32 +000072 if (FoundAFile)
Chris Lattner6c8103f2003-05-22 20:13:16 +000073 std::cerr << "Bytecode file '" << FN << "' corrupt! "
74 << "Use 'link -v ...' for more info.\n";
Chris Lattner952d3652001-12-08 20:31:32 +000075 else
Chris Lattner6c8103f2003-05-22 20:13:16 +000076 std::cerr << "Could not locate bytecode file: '" << FN << "'\n";
Chris Lattner65be3212001-10-24 06:23:00 +000077 return std::auto_ptr<Module>();
78}
Chris Lattner075a0b72001-10-13 07:06:23 +000079
Chris Lattner952d3652001-12-08 20:31:32 +000080
81
82
Chris Lattner075a0b72001-10-13 07:06:23 +000083int main(int argc, char **argv) {
Chris Lattner5ff62e92002-07-22 02:10:13 +000084 cl::ParseCommandLineOptions(argc, argv, " llvm linker\n");
Chris Lattner075a0b72001-10-13 07:06:23 +000085 assert(InputFilenames.size() > 0 && "OneOrMore is not working");
86
Chris Lattner65be3212001-10-24 06:23:00 +000087 unsigned BaseArg = 0;
Chris Lattner697954c2002-01-20 22:54:45 +000088 std::string ErrorMessage;
Chris Lattner65be3212001-10-24 06:23:00 +000089
Chris Lattner65be3212001-10-24 06:23:00 +000090 std::auto_ptr<Module> Composite(LoadFile(InputFilenames[BaseArg]));
91 if (Composite.get() == 0) return 1;
92
93 for (unsigned i = BaseArg+1; i < InputFilenames.size(); ++i) {
Chris Lattner697954c2002-01-20 22:54:45 +000094 std::auto_ptr<Module> M(LoadFile(InputFilenames[i]));
Chris Lattner65be3212001-10-24 06:23:00 +000095 if (M.get() == 0) return 1;
Chris Lattnerb81adf12001-10-23 20:44:55 +000096
Chris Lattner6c8103f2003-05-22 20:13:16 +000097 if (Verbose) std::cerr << "Linking in '" << InputFilenames[i] << "'\n";
Chris Lattnerb81adf12001-10-23 20:44:55 +000098
Chris Lattner075a0b72001-10-13 07:06:23 +000099 if (LinkModules(Composite.get(), M.get(), &ErrorMessage)) {
Chris Lattner6c8103f2003-05-22 20:13:16 +0000100 std::cerr << argv[0] << ": error linking in '" << InputFilenames[i]
101 << "': " << ErrorMessage << "\n";
Chris Lattner075a0b72001-10-13 07:06:23 +0000102 return 1;
103 }
104 }
105
Chris Lattner6c8103f2003-05-22 20:13:16 +0000106 if (DumpAsm) std::cerr << "Here's the assembly:\n" << Composite.get();
Chris Lattner164cb692001-10-14 23:23:33 +0000107
Anand Shuklacf17bcc2002-06-25 21:57:48 +0000108 std::ostream *Out = &std::cout; // Default to printing to stdout...
Chris Lattner4a4daba2003-06-13 16:10:26 +0000109 if (OutputFilename != "-") {
Chris Lattner888912d2002-01-22 21:07:24 +0000110 if (!Force && std::ifstream(OutputFilename.c_str())) {
Chris Lattner697954c2002-01-20 22:54:45 +0000111 // If force is not specified, make sure not to overwrite a file!
Chris Lattner6c8103f2003-05-22 20:13:16 +0000112 std::cerr << argv[0] << ": error opening '" << OutputFilename
113 << "': file exists!\n"
114 << "Use -f command line argument to force output\n";
Chris Lattner697954c2002-01-20 22:54:45 +0000115 return 1;
116 }
117 Out = new std::ofstream(OutputFilename.c_str());
Chris Lattner075a0b72001-10-13 07:06:23 +0000118 if (!Out->good()) {
Chris Lattner6c8103f2003-05-22 20:13:16 +0000119 std::cerr << argv[0] << ": error opening '" << OutputFilename << "'!\n";
Chris Lattner075a0b72001-10-13 07:06:23 +0000120 return 1;
121 }
Chris Lattner76d12292002-04-18 19:55:25 +0000122
123 // Make sure that the Out file gets unlink'd from the disk if we get a
124 // SIGINT
125 RemoveFileOnSignal(OutputFilename);
Chris Lattner075a0b72001-10-13 07:06:23 +0000126 }
127
Chris Lattner6c8103f2003-05-22 20:13:16 +0000128 if (Verbose) std::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}