blob: f3773953234fc6edb35af3f5cea77d93b47adefb [file] [log] [blame]
Chris Lattner825937d2003-09-20 02:42:54 +00001//===- llvm-link.cpp - Low-level LLVM linker ------------------------------===//
Chris Lattner9d810e02001-10-13 07:06:23 +00002//
3// This utility may be invoked in the following manner:
Misha Brukmancf0c7442003-09-15 18:34:34 +00004// llvm-link a.bc b.bc c.bc -o x.bc
Chris Lattner9d810e02001-10-13 07:06:23 +00005//
6//===----------------------------------------------------------------------===//
7
Chris Lattneraa6a44f2003-08-28 16:25:34 +00008#include "llvm/Module.h"
9#include "llvm/Analysis/Verifier.h"
Chris Lattner9d810e02001-10-13 07:06:23 +000010#include "llvm/Bytecode/Reader.h"
11#include "llvm/Bytecode/Writer.h"
Chris Lattneraa6a44f2003-08-28 16:25:34 +000012#include "llvm/Transforms/Utils/Linker.h"
Chris Lattner5de22042001-11-27 00:03:19 +000013#include "Support/CommandLine.h"
Chris Lattnerc065ad82002-04-18 19:55:25 +000014#include "Support/Signals.h"
Chris Lattner5de22042001-11-27 00:03:19 +000015#include <fstream>
Chris Lattner9d810e02001-10-13 07:06:23 +000016#include <memory>
Chris Lattner5053ba92001-12-08 20:31:32 +000017#include <sys/types.h> // For FileExists
18#include <sys/stat.h>
Chris Lattner9d810e02001-10-13 07:06:23 +000019
Chris Lattnerf5cad152002-07-22 02:10:13 +000020static cl::list<std::string>
21InputFilenames(cl::Positional, cl::OneOrMore,
Chris Lattner17570e12002-07-22 02:18:09 +000022 cl::desc("<input bytecode files>"));
Chris Lattnerf5cad152002-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 Lattner5053ba92001-12-08 20:31:32 +000039
40// FileExists - Return true if the specified string is an openable file...
Chris Lattner7f74a562002-01-20 22:54:45 +000041static inline bool FileExists(const std::string &FN) {
Chris Lattner5053ba92001-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 Lattner7f74a562002-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 Lattnerae31f5b2001-10-24 06:23:00 +000052
53 unsigned NextLibPathIdx = 0;
Chris Lattner5053ba92001-12-08 20:31:32 +000054 bool FoundAFile = false;
Chris Lattnerae31f5b2001-10-24 06:23:00 +000055
56 while (1) {
Chris Lattner02a16832003-05-22 20:13:16 +000057 if (Verbose) std::cerr << "Loading '" << Filename << "'\n";
Chris Lattner5053ba92001-12-08 20:31:32 +000058 if (FileExists(Filename)) FoundAFile = true;
Chris Lattnerae31f5b2001-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 Lattner02a16832003-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 Lattnerae31f5b2001-10-24 06:23:00 +000066 }
67
68 if (NextLibPathIdx == LibPaths.size()) break;
69 Filename = LibPaths[NextLibPathIdx++] + "/" + FN;
70 }
71
Chris Lattner5053ba92001-12-08 20:31:32 +000072 if (FoundAFile)
Chris Lattner02a16832003-05-22 20:13:16 +000073 std::cerr << "Bytecode file '" << FN << "' corrupt! "
Misha Brukmancf0c7442003-09-15 18:34:34 +000074 << "Use 'llvm-link -v ...' for more info.\n";
Chris Lattner5053ba92001-12-08 20:31:32 +000075 else
Chris Lattner02a16832003-05-22 20:13:16 +000076 std::cerr << "Could not locate bytecode file: '" << FN << "'\n";
Chris Lattnerae31f5b2001-10-24 06:23:00 +000077 return std::auto_ptr<Module>();
78}
Chris Lattner9d810e02001-10-13 07:06:23 +000079
Chris Lattner5053ba92001-12-08 20:31:32 +000080
81
82
Chris Lattner9d810e02001-10-13 07:06:23 +000083int main(int argc, char **argv) {
Chris Lattnerf5cad152002-07-22 02:10:13 +000084 cl::ParseCommandLineOptions(argc, argv, " llvm linker\n");
Chris Lattner9d810e02001-10-13 07:06:23 +000085 assert(InputFilenames.size() > 0 && "OneOrMore is not working");
86
Chris Lattnerae31f5b2001-10-24 06:23:00 +000087 unsigned BaseArg = 0;
Chris Lattner7f74a562002-01-20 22:54:45 +000088 std::string ErrorMessage;
Chris Lattnerae31f5b2001-10-24 06:23:00 +000089
Chris Lattnerae31f5b2001-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 Lattner7f74a562002-01-20 22:54:45 +000094 std::auto_ptr<Module> M(LoadFile(InputFilenames[i]));
Chris Lattnerae31f5b2001-10-24 06:23:00 +000095 if (M.get() == 0) return 1;
Chris Lattnerebaa7882001-10-23 20:44:55 +000096
Chris Lattner02a16832003-05-22 20:13:16 +000097 if (Verbose) std::cerr << "Linking in '" << InputFilenames[i] << "'\n";
Chris Lattnerebaa7882001-10-23 20:44:55 +000098
Chris Lattner9d810e02001-10-13 07:06:23 +000099 if (LinkModules(Composite.get(), M.get(), &ErrorMessage)) {
Chris Lattner02a16832003-05-22 20:13:16 +0000100 std::cerr << argv[0] << ": error linking in '" << InputFilenames[i]
101 << "': " << ErrorMessage << "\n";
Chris Lattner9d810e02001-10-13 07:06:23 +0000102 return 1;
103 }
104 }
105
Chris Lattner02a16832003-05-22 20:13:16 +0000106 if (DumpAsm) std::cerr << "Here's the assembly:\n" << Composite.get();
Chris Lattnerd6311652001-10-14 23:23:33 +0000107
Anand Shuklafef32412002-06-25 21:57:48 +0000108 std::ostream *Out = &std::cout; // Default to printing to stdout...
Chris Lattner3aa32572003-06-13 16:10:26 +0000109 if (OutputFilename != "-") {
Chris Lattner0e11e542002-01-22 21:07:24 +0000110 if (!Force && std::ifstream(OutputFilename.c_str())) {
Chris Lattner7f74a562002-01-20 22:54:45 +0000111 // If force is not specified, make sure not to overwrite a file!
Chris Lattner02a16832003-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 Lattner7f74a562002-01-20 22:54:45 +0000115 return 1;
116 }
117 Out = new std::ofstream(OutputFilename.c_str());
Chris Lattner9d810e02001-10-13 07:06:23 +0000118 if (!Out->good()) {
Chris Lattner02a16832003-05-22 20:13:16 +0000119 std::cerr << argv[0] << ": error opening '" << OutputFilename << "'!\n";
Chris Lattner9d810e02001-10-13 07:06:23 +0000120 return 1;
121 }
Chris Lattnerc065ad82002-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 Lattner9d810e02001-10-13 07:06:23 +0000126 }
127
Chris Lattneraa6a44f2003-08-28 16:25:34 +0000128 if (verifyModule(*Composite.get())) {
129 std::cerr << argv[0] << ": linked module is broken!\n";
130 return 1;
131 }
132
Chris Lattner02a16832003-05-22 20:13:16 +0000133 if (Verbose) std::cerr << "Writing bytecode...\n";
Chris Lattner9d810e02001-10-13 07:06:23 +0000134 WriteBytecodeToFile(Composite.get(), *Out);
135
Chris Lattner7f74a562002-01-20 22:54:45 +0000136 if (Out != &std::cout) delete Out;
Chris Lattner9d810e02001-10-13 07:06:23 +0000137 return 0;
138}