Chris Lattner | e7fca51 | 2002-01-24 19:12:12 +0000 | [diff] [blame] | 1 | //===----------------------------------------------------------------------===// |
| 2 | // LLVM 'GCCLD' UTILITY |
| 3 | // |
| 4 | // This utility is intended to be compatible with GCC, and follows standard |
| 5 | // system ld conventions. As such, the default output file is ./a.out. |
| 6 | // Additionally, this program outputs a shell script that is used to invoke LLI |
| 7 | // to execute the program. In this manner, the generated executable (a.out for |
| 8 | // example), is directly executable, whereas the bytecode file actually lives in |
| 9 | // the a.out.bc file generated by this program. Also, Force is on by default. |
| 10 | // |
| 11 | // Note that if someone (or a script) deletes the executable program generated, |
| 12 | // the .bc file will be left around. Considering that this is a temporary hack, |
| 13 | // I'm not to worried about this. |
| 14 | // |
| 15 | //===----------------------------------------------------------------------===// |
| 16 | |
| 17 | #include "llvm/Transforms/Linker.h" |
| 18 | #include "llvm/Bytecode/Reader.h" |
| 19 | #include "llvm/Bytecode/Writer.h" |
| 20 | #include "llvm/Module.h" |
Chris Lattner | e7fca51 | 2002-01-24 19:12:12 +0000 | [diff] [blame] | 21 | #include "Support/CommandLine.h" |
| 22 | #include <fstream> |
| 23 | #include <memory> |
Chris Lattner | 41c3465 | 2002-03-11 17:49:53 +0000 | [diff] [blame] | 24 | #include <algorithm> |
Chris Lattner | e7fca51 | 2002-01-24 19:12:12 +0000 | [diff] [blame] | 25 | #include <sys/types.h> // For FileExists |
| 26 | #include <sys/stat.h> |
| 27 | |
| 28 | |
| 29 | cl::StringList InputFilenames("", "Load <arg> files, linking them together", |
| 30 | cl::OneOrMore); |
| 31 | cl::String OutputFilename("o", "Override output filename", cl::NoFlags,"a.out"); |
| 32 | cl::Flag Verbose ("v", "Print information about actions taken"); |
| 33 | cl::StringList LibPaths ("L", "Specify a library search path", cl::ZeroOrMore); |
| 34 | cl::StringList Libraries ("l", "Specify libraries to link to", cl::ZeroOrMore); |
| 35 | |
| 36 | |
| 37 | // FileExists - Return true if the specified string is an openable file... |
| 38 | static inline bool FileExists(const std::string &FN) { |
| 39 | struct stat StatBuf; |
| 40 | return stat(FN.c_str(), &StatBuf) != -1; |
| 41 | } |
| 42 | |
| 43 | // LoadFile - Read the specified bytecode file in and return it. This routine |
| 44 | // searches the link path for the specified file to try to find it... |
| 45 | // |
| 46 | static inline std::auto_ptr<Module> LoadFile(const std::string &FN) { |
| 47 | std::string Filename = FN; |
| 48 | std::string ErrorMessage; |
| 49 | |
| 50 | unsigned NextLibPathIdx = 0; |
| 51 | bool FoundAFile = false; |
| 52 | |
| 53 | while (1) { |
| 54 | if (Verbose) cerr << "Loading '" << Filename << "'\n"; |
| 55 | if (FileExists(Filename)) FoundAFile = true; |
| 56 | Module *Result = ParseBytecodeFile(Filename, &ErrorMessage); |
| 57 | if (Result) return std::auto_ptr<Module>(Result); // Load successful! |
| 58 | |
| 59 | if (Verbose) { |
| 60 | cerr << "Error opening bytecode file: '" << Filename << "'"; |
| 61 | if (ErrorMessage.size()) cerr << ": " << ErrorMessage; |
| 62 | cerr << endl; |
| 63 | } |
| 64 | |
| 65 | if (NextLibPathIdx == LibPaths.size()) break; |
| 66 | Filename = LibPaths[NextLibPathIdx++] + "/" + FN; |
| 67 | } |
| 68 | |
| 69 | if (FoundAFile) |
| 70 | cerr << "Bytecode file '" << FN << "' corrupt! " |
Chris Lattner | 084235a | 2002-03-12 15:41:36 +0000 | [diff] [blame] | 71 | << "Use 'gccld -v ...' for more info.\n"; |
Chris Lattner | e7fca51 | 2002-01-24 19:12:12 +0000 | [diff] [blame] | 72 | else |
| 73 | cerr << "Could not locate bytecode file: '" << FN << "'\n"; |
| 74 | return std::auto_ptr<Module>(); |
| 75 | } |
| 76 | |
| 77 | |
| 78 | |
| 79 | |
| 80 | int main(int argc, char **argv) { |
| 81 | cl::ParseCommandLineOptions(argc, argv, " llvm linker for GCC\n", |
| 82 | cl::EnableSingleLetterArgValue | |
| 83 | cl::DisableSingleLetterArgGrouping); |
| 84 | assert(InputFilenames.size() > 0 && "OneOrMore is not working"); |
| 85 | |
| 86 | unsigned BaseArg = 0; |
| 87 | std::string ErrorMessage; |
| 88 | |
Chris Lattner | 41c3465 | 2002-03-11 17:49:53 +0000 | [diff] [blame] | 89 | if (!Libraries.empty()) { |
| 90 | // Sort libraries list... |
| 91 | sort(Libraries.begin(), Libraries.end()); |
| 92 | |
| 93 | // Remove duplicate libraries entries... |
| 94 | Libraries.erase(unique(Libraries.begin(), Libraries.end()), |
| 95 | Libraries.end()); |
| 96 | |
| 97 | // Add all of the libraries to the end of the link line... |
| 98 | for (unsigned i = 0; i < Libraries.size(); ++i) |
| 99 | InputFilenames.push_back("lib" + Libraries[i] + ".bc"); |
| 100 | } |
Chris Lattner | e7fca51 | 2002-01-24 19:12:12 +0000 | [diff] [blame] | 101 | |
| 102 | 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) { |
| 106 | std::auto_ptr<Module> M(LoadFile(InputFilenames[i])); |
| 107 | if (M.get() == 0) return 1; |
| 108 | |
| 109 | if (Verbose) cerr << "Linking in '" << InputFilenames[i] << "'\n"; |
| 110 | |
| 111 | if (LinkModules(Composite.get(), M.get(), &ErrorMessage)) { |
| 112 | cerr << "Error linking in '" << InputFilenames[i] << "': " |
| 113 | << ErrorMessage << endl; |
| 114 | return 1; |
| 115 | } |
| 116 | } |
| 117 | |
Chris Lattner | 2fbfdcf | 2002-04-07 20:49:59 +0000 | [diff] [blame^] | 118 | // Now that composite has been compiled, scan through the module, looking for |
| 119 | // a main function. If main is defined, mark all other functions internal. |
| 120 | // |
| 121 | |
| 122 | // Next run globaldce... |
| 123 | |
| 124 | // next ? |
| 125 | |
| 126 | |
Chris Lattner | e7fca51 | 2002-01-24 19:12:12 +0000 | [diff] [blame] | 127 | std::ofstream Out((OutputFilename+".bc").c_str()); |
| 128 | if (!Out.good()) { |
| 129 | cerr << "Error opening '" << OutputFilename << ".bc' for writing!\n"; |
| 130 | return 1; |
| 131 | } |
| 132 | |
| 133 | if (Verbose) cerr << "Writing bytecode...\n"; |
| 134 | WriteBytecodeToFile(Composite.get(), Out); |
| 135 | Out.close(); |
| 136 | |
| 137 | // Output the script to start the program... |
| 138 | std::ofstream Out2(OutputFilename.c_str()); |
| 139 | if (!Out2.good()) { |
| 140 | cerr << "Error openeing '" << OutputFilename << "' for writing!\n"; |
| 141 | return 1; |
| 142 | } |
Chris Lattner | 41c3465 | 2002-03-11 17:49:53 +0000 | [diff] [blame] | 143 | Out2 << "#!/bin/sh\nlli -q $0.bc $*\n"; |
Chris Lattner | e7fca51 | 2002-01-24 19:12:12 +0000 | [diff] [blame] | 144 | Out2.close(); |
| 145 | |
| 146 | // Make the script executable... |
| 147 | chmod(OutputFilename.c_str(), 0755); |
| 148 | |
| 149 | return 0; |
| 150 | } |