Chris Lattner | bb37a69 | 2003-09-20 02:42:54 +0000 | [diff] [blame] | 1 | //===- llvm-link.cpp - Low-level LLVM linker ------------------------------===// |
John Criswell | 7c0e022 | 2003-10-20 17:47:21 +0000 | [diff] [blame] | 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file was developed by the LLVM research group and is distributed under |
| 6 | // the University of Illinois Open Source License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
Chris Lattner | 075a0b7 | 2001-10-13 07:06:23 +0000 | [diff] [blame] | 9 | // |
| 10 | // This utility may be invoked in the following manner: |
Misha Brukman | 3ef3beb | 2003-09-15 18:34:34 +0000 | [diff] [blame] | 11 | // llvm-link a.bc b.bc c.bc -o x.bc |
Chris Lattner | 075a0b7 | 2001-10-13 07:06:23 +0000 | [diff] [blame] | 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
Chris Lattner | a55c4b1 | 2003-08-28 16:25:34 +0000 | [diff] [blame] | 15 | #include "llvm/Module.h" |
| 16 | #include "llvm/Analysis/Verifier.h" |
Chris Lattner | 075a0b7 | 2001-10-13 07:06:23 +0000 | [diff] [blame] | 17 | #include "llvm/Bytecode/Reader.h" |
| 18 | #include "llvm/Bytecode/Writer.h" |
Chris Lattner | a55c4b1 | 2003-08-28 16:25:34 +0000 | [diff] [blame] | 19 | #include "llvm/Transforms/Utils/Linker.h" |
Chris Lattner | cee8f9a | 2001-11-27 00:03:19 +0000 | [diff] [blame] | 20 | #include "Support/CommandLine.h" |
Chris Lattner | 76d1229 | 2002-04-18 19:55:25 +0000 | [diff] [blame] | 21 | #include "Support/Signals.h" |
Chris Lattner | cee8f9a | 2001-11-27 00:03:19 +0000 | [diff] [blame] | 22 | #include <fstream> |
Chris Lattner | 075a0b7 | 2001-10-13 07:06:23 +0000 | [diff] [blame] | 23 | #include <memory> |
Chris Lattner | 952d365 | 2001-12-08 20:31:32 +0000 | [diff] [blame] | 24 | #include <sys/types.h> // For FileExists |
| 25 | #include <sys/stat.h> |
Chris Lattner | 075a0b7 | 2001-10-13 07:06:23 +0000 | [diff] [blame] | 26 | |
Brian Gaeke | d0fde30 | 2003-11-11 22:41:34 +0000 | [diff] [blame^] | 27 | using namespace llvm; |
| 28 | |
Chris Lattner | 5ff62e9 | 2002-07-22 02:10:13 +0000 | [diff] [blame] | 29 | static cl::list<std::string> |
| 30 | InputFilenames(cl::Positional, cl::OneOrMore, |
Chris Lattner | 54e05af | 2002-07-22 02:18:09 +0000 | [diff] [blame] | 31 | cl::desc("<input bytecode files>")); |
Chris Lattner | 5ff62e9 | 2002-07-22 02:10:13 +0000 | [diff] [blame] | 32 | |
| 33 | static cl::opt<std::string> |
| 34 | OutputFilename("o", cl::desc("Override output filename"), cl::init("-"), |
| 35 | cl::value_desc("filename")); |
| 36 | |
| 37 | static cl::opt<bool> Force("f", cl::desc("Overwrite output files")); |
| 38 | |
| 39 | static cl::opt<bool> |
| 40 | Verbose("v", cl::desc("Print information about actions taken")); |
| 41 | |
| 42 | static cl::opt<bool> |
| 43 | DumpAsm("d", cl::desc("Print assembly as linked"), cl::Hidden); |
| 44 | |
| 45 | static cl::list<std::string> |
| 46 | LibPaths("L", cl::desc("Specify a library search path"), cl::ZeroOrMore, |
| 47 | cl::value_desc("directory"), cl::Prefix); |
Chris Lattner | 952d365 | 2001-12-08 20:31:32 +0000 | [diff] [blame] | 48 | |
| 49 | // FileExists - Return true if the specified string is an openable file... |
Chris Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 50 | static inline bool FileExists(const std::string &FN) { |
Chris Lattner | 952d365 | 2001-12-08 20:31:32 +0000 | [diff] [blame] | 51 | struct stat StatBuf; |
| 52 | return stat(FN.c_str(), &StatBuf) != -1; |
| 53 | } |
| 54 | |
| 55 | // LoadFile - Read the specified bytecode file in and return it. This routine |
| 56 | // searches the link path for the specified file to try to find it... |
| 57 | // |
Chris Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 58 | static inline std::auto_ptr<Module> LoadFile(const std::string &FN) { |
| 59 | std::string Filename = FN; |
| 60 | std::string ErrorMessage; |
Chris Lattner | 65be321 | 2001-10-24 06:23:00 +0000 | [diff] [blame] | 61 | |
| 62 | unsigned NextLibPathIdx = 0; |
Chris Lattner | 952d365 | 2001-12-08 20:31:32 +0000 | [diff] [blame] | 63 | bool FoundAFile = false; |
Chris Lattner | 65be321 | 2001-10-24 06:23:00 +0000 | [diff] [blame] | 64 | |
| 65 | while (1) { |
Chris Lattner | 6c8103f | 2003-05-22 20:13:16 +0000 | [diff] [blame] | 66 | if (Verbose) std::cerr << "Loading '" << Filename << "'\n"; |
Chris Lattner | 952d365 | 2001-12-08 20:31:32 +0000 | [diff] [blame] | 67 | if (FileExists(Filename)) FoundAFile = true; |
Chris Lattner | 65be321 | 2001-10-24 06:23:00 +0000 | [diff] [blame] | 68 | Module *Result = ParseBytecodeFile(Filename, &ErrorMessage); |
| 69 | if (Result) return std::auto_ptr<Module>(Result); // Load successful! |
| 70 | |
| 71 | if (Verbose) { |
Chris Lattner | 6c8103f | 2003-05-22 20:13:16 +0000 | [diff] [blame] | 72 | std::cerr << "Error opening bytecode file: '" << Filename << "'"; |
| 73 | if (ErrorMessage.size()) std::cerr << ": " << ErrorMessage; |
| 74 | std::cerr << "\n"; |
Chris Lattner | 65be321 | 2001-10-24 06:23:00 +0000 | [diff] [blame] | 75 | } |
| 76 | |
| 77 | if (NextLibPathIdx == LibPaths.size()) break; |
| 78 | Filename = LibPaths[NextLibPathIdx++] + "/" + FN; |
| 79 | } |
| 80 | |
Chris Lattner | 952d365 | 2001-12-08 20:31:32 +0000 | [diff] [blame] | 81 | if (FoundAFile) |
Chris Lattner | 6c8103f | 2003-05-22 20:13:16 +0000 | [diff] [blame] | 82 | std::cerr << "Bytecode file '" << FN << "' corrupt! " |
Misha Brukman | 3ef3beb | 2003-09-15 18:34:34 +0000 | [diff] [blame] | 83 | << "Use 'llvm-link -v ...' for more info.\n"; |
Chris Lattner | 952d365 | 2001-12-08 20:31:32 +0000 | [diff] [blame] | 84 | else |
Chris Lattner | 6c8103f | 2003-05-22 20:13:16 +0000 | [diff] [blame] | 85 | std::cerr << "Could not locate bytecode file: '" << FN << "'\n"; |
Chris Lattner | 65be321 | 2001-10-24 06:23:00 +0000 | [diff] [blame] | 86 | return std::auto_ptr<Module>(); |
| 87 | } |
Chris Lattner | 075a0b7 | 2001-10-13 07:06:23 +0000 | [diff] [blame] | 88 | |
Chris Lattner | 952d365 | 2001-12-08 20:31:32 +0000 | [diff] [blame] | 89 | |
| 90 | |
| 91 | |
Chris Lattner | 075a0b7 | 2001-10-13 07:06:23 +0000 | [diff] [blame] | 92 | int main(int argc, char **argv) { |
Chris Lattner | 5ff62e9 | 2002-07-22 02:10:13 +0000 | [diff] [blame] | 93 | cl::ParseCommandLineOptions(argc, argv, " llvm linker\n"); |
Chris Lattner | 075a0b7 | 2001-10-13 07:06:23 +0000 | [diff] [blame] | 94 | assert(InputFilenames.size() > 0 && "OneOrMore is not working"); |
| 95 | |
Chris Lattner | 65be321 | 2001-10-24 06:23:00 +0000 | [diff] [blame] | 96 | unsigned BaseArg = 0; |
Chris Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 97 | std::string ErrorMessage; |
Chris Lattner | 65be321 | 2001-10-24 06:23:00 +0000 | [diff] [blame] | 98 | |
Chris Lattner | 65be321 | 2001-10-24 06:23:00 +0000 | [diff] [blame] | 99 | std::auto_ptr<Module> Composite(LoadFile(InputFilenames[BaseArg])); |
| 100 | if (Composite.get() == 0) return 1; |
| 101 | |
| 102 | for (unsigned i = BaseArg+1; i < InputFilenames.size(); ++i) { |
Chris Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 103 | std::auto_ptr<Module> M(LoadFile(InputFilenames[i])); |
Chris Lattner | 65be321 | 2001-10-24 06:23:00 +0000 | [diff] [blame] | 104 | if (M.get() == 0) return 1; |
Chris Lattner | b81adf1 | 2001-10-23 20:44:55 +0000 | [diff] [blame] | 105 | |
Chris Lattner | 6c8103f | 2003-05-22 20:13:16 +0000 | [diff] [blame] | 106 | if (Verbose) std::cerr << "Linking in '" << InputFilenames[i] << "'\n"; |
Chris Lattner | b81adf1 | 2001-10-23 20:44:55 +0000 | [diff] [blame] | 107 | |
Chris Lattner | 075a0b7 | 2001-10-13 07:06:23 +0000 | [diff] [blame] | 108 | if (LinkModules(Composite.get(), M.get(), &ErrorMessage)) { |
Chris Lattner | 6c8103f | 2003-05-22 20:13:16 +0000 | [diff] [blame] | 109 | std::cerr << argv[0] << ": error linking in '" << InputFilenames[i] |
| 110 | << "': " << ErrorMessage << "\n"; |
Chris Lattner | 075a0b7 | 2001-10-13 07:06:23 +0000 | [diff] [blame] | 111 | return 1; |
| 112 | } |
| 113 | } |
| 114 | |
Chris Lattner | 6c8103f | 2003-05-22 20:13:16 +0000 | [diff] [blame] | 115 | if (DumpAsm) std::cerr << "Here's the assembly:\n" << Composite.get(); |
Chris Lattner | 164cb69 | 2001-10-14 23:23:33 +0000 | [diff] [blame] | 116 | |
Anand Shukla | cf17bcc | 2002-06-25 21:57:48 +0000 | [diff] [blame] | 117 | std::ostream *Out = &std::cout; // Default to printing to stdout... |
Chris Lattner | 4a4daba | 2003-06-13 16:10:26 +0000 | [diff] [blame] | 118 | if (OutputFilename != "-") { |
Chris Lattner | 888912d | 2002-01-22 21:07:24 +0000 | [diff] [blame] | 119 | if (!Force && std::ifstream(OutputFilename.c_str())) { |
Chris Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 120 | // If force is not specified, make sure not to overwrite a file! |
Chris Lattner | 6c8103f | 2003-05-22 20:13:16 +0000 | [diff] [blame] | 121 | std::cerr << argv[0] << ": error opening '" << OutputFilename |
| 122 | << "': file exists!\n" |
| 123 | << "Use -f command line argument to force output\n"; |
Chris Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 124 | return 1; |
| 125 | } |
| 126 | Out = new std::ofstream(OutputFilename.c_str()); |
Chris Lattner | 075a0b7 | 2001-10-13 07:06:23 +0000 | [diff] [blame] | 127 | if (!Out->good()) { |
Chris Lattner | 6c8103f | 2003-05-22 20:13:16 +0000 | [diff] [blame] | 128 | std::cerr << argv[0] << ": error opening '" << OutputFilename << "'!\n"; |
Chris Lattner | 075a0b7 | 2001-10-13 07:06:23 +0000 | [diff] [blame] | 129 | return 1; |
| 130 | } |
Chris Lattner | 76d1229 | 2002-04-18 19:55:25 +0000 | [diff] [blame] | 131 | |
Misha Brukman | 452fea9 | 2003-10-10 17:56:49 +0000 | [diff] [blame] | 132 | // Make sure that the Out file gets unlinked from the disk if we get a |
Chris Lattner | 76d1229 | 2002-04-18 19:55:25 +0000 | [diff] [blame] | 133 | // SIGINT |
| 134 | RemoveFileOnSignal(OutputFilename); |
Chris Lattner | 075a0b7 | 2001-10-13 07:06:23 +0000 | [diff] [blame] | 135 | } |
| 136 | |
Chris Lattner | a55c4b1 | 2003-08-28 16:25:34 +0000 | [diff] [blame] | 137 | if (verifyModule(*Composite.get())) { |
| 138 | std::cerr << argv[0] << ": linked module is broken!\n"; |
| 139 | return 1; |
| 140 | } |
| 141 | |
Chris Lattner | 6c8103f | 2003-05-22 20:13:16 +0000 | [diff] [blame] | 142 | if (Verbose) std::cerr << "Writing bytecode...\n"; |
Chris Lattner | 075a0b7 | 2001-10-13 07:06:23 +0000 | [diff] [blame] | 143 | WriteBytecodeToFile(Composite.get(), *Out); |
| 144 | |
Chris Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 145 | if (Out != &std::cout) delete Out; |
Chris Lattner | 075a0b7 | 2001-10-13 07:06:23 +0000 | [diff] [blame] | 146 | return 0; |
| 147 | } |