Chris Lattner | 10970eb | 2003-04-19 22:44:38 +0000 | [diff] [blame] | 1 | //===- gccld.cpp - LLVM 'ld' compatible linker ----------------------------===// |
Chris Lattner | e7fca51 | 2002-01-24 19:12:12 +0000 | [diff] [blame] | 2 | // |
| 3 | // This utility is intended to be compatible with GCC, and follows standard |
Chris Lattner | 10970eb | 2003-04-19 22:44:38 +0000 | [diff] [blame] | 4 | // system 'ld' conventions. As such, the default output file is ./a.out. |
Chris Lattner | e7fca51 | 2002-01-24 19:12:12 +0000 | [diff] [blame] | 5 | // Additionally, this program outputs a shell script that is used to invoke LLI |
| 6 | // to execute the program. In this manner, the generated executable (a.out for |
| 7 | // example), is directly executable, whereas the bytecode file actually lives in |
| 8 | // the a.out.bc file generated by this program. Also, Force is on by default. |
| 9 | // |
| 10 | // Note that if someone (or a script) deletes the executable program generated, |
| 11 | // the .bc file will be left around. Considering that this is a temporary hack, |
| 12 | // I'm not to worried about this. |
| 13 | // |
| 14 | //===----------------------------------------------------------------------===// |
| 15 | |
Chris Lattner | c8cc4cb | 2002-05-07 18:36:35 +0000 | [diff] [blame] | 16 | #include "llvm/Transforms/Utils/Linker.h" |
Chris Lattner | e7fca51 | 2002-01-24 19:12:12 +0000 | [diff] [blame] | 17 | #include "llvm/Module.h" |
Chris Lattner | ad202a0 | 2002-04-08 00:14:58 +0000 | [diff] [blame] | 18 | #include "llvm/PassManager.h" |
| 19 | #include "llvm/Bytecode/Reader.h" |
| 20 | #include "llvm/Bytecode/WriteBytecodePass.h" |
Chris Lattner | d9d8c07 | 2002-07-23 22:04:43 +0000 | [diff] [blame] | 21 | #include "llvm/Transforms/IPO.h" |
Chris Lattner | d9d8c07 | 2002-07-23 22:04:43 +0000 | [diff] [blame] | 22 | #include "llvm/Transforms/Scalar.h" |
Chris Lattner | e7fca51 | 2002-01-24 19:12:12 +0000 | [diff] [blame] | 23 | #include "Support/CommandLine.h" |
Chris Lattner | 76d1229 | 2002-04-18 19:55:25 +0000 | [diff] [blame] | 24 | #include "Support/Signals.h" |
Chris Lattner | e7fca51 | 2002-01-24 19:12:12 +0000 | [diff] [blame] | 25 | #include <fstream> |
| 26 | #include <memory> |
Chris Lattner | 10970eb | 2003-04-19 22:44:38 +0000 | [diff] [blame] | 27 | #include <set> |
Chris Lattner | 41c3465 | 2002-03-11 17:49:53 +0000 | [diff] [blame] | 28 | #include <algorithm> |
Chris Lattner | e7fca51 | 2002-01-24 19:12:12 +0000 | [diff] [blame] | 29 | #include <sys/types.h> // For FileExists |
| 30 | #include <sys/stat.h> |
Chris Lattner | e7fca51 | 2002-01-24 19:12:12 +0000 | [diff] [blame] | 31 | |
Chris Lattner | f3d4f17 | 2003-04-18 23:01:25 +0000 | [diff] [blame] | 32 | namespace { |
| 33 | cl::list<std::string> |
| 34 | InputFilenames(cl::Positional, cl::desc("<input bytecode files>"), |
| 35 | cl::OneOrMore); |
Chris Lattner | 5ff62e9 | 2002-07-22 02:10:13 +0000 | [diff] [blame] | 36 | |
Chris Lattner | f3d4f17 | 2003-04-18 23:01:25 +0000 | [diff] [blame] | 37 | cl::opt<std::string> |
| 38 | OutputFilename("o", cl::desc("Override output filename"), cl::init("a.out"), |
| 39 | cl::value_desc("filename")); |
Chris Lattner | 5ff62e9 | 2002-07-22 02:10:13 +0000 | [diff] [blame] | 40 | |
Chris Lattner | f3d4f17 | 2003-04-18 23:01:25 +0000 | [diff] [blame] | 41 | cl::opt<bool> |
| 42 | Verbose("v", cl::desc("Print information about actions taken")); |
| 43 | |
| 44 | cl::list<std::string> |
| 45 | LibPaths("L", cl::desc("Specify a library search path"), cl::Prefix, |
| 46 | cl::value_desc("directory")); |
Chris Lattner | 5ff62e9 | 2002-07-22 02:10:13 +0000 | [diff] [blame] | 47 | |
Chris Lattner | f3d4f17 | 2003-04-18 23:01:25 +0000 | [diff] [blame] | 48 | cl::list<std::string> |
| 49 | Libraries("l", cl::desc("Specify libraries to link to"), cl::Prefix, |
| 50 | cl::value_desc("library prefix")); |
Chris Lattner | 5ff62e9 | 2002-07-22 02:10:13 +0000 | [diff] [blame] | 51 | |
Chris Lattner | f3d4f17 | 2003-04-18 23:01:25 +0000 | [diff] [blame] | 52 | cl::opt<bool> |
| 53 | Strip("s", cl::desc("Strip symbol info from executable")); |
Chris Lattner | 5ff62e9 | 2002-07-22 02:10:13 +0000 | [diff] [blame] | 54 | |
Chris Lattner | f3d4f17 | 2003-04-18 23:01:25 +0000 | [diff] [blame] | 55 | cl::opt<bool> |
| 56 | NoInternalize("disable-internalize", |
| 57 | cl::desc("Do not mark all symbols as internal")); |
Chris Lattner | a856db2 | 2003-04-18 23:38:22 +0000 | [diff] [blame] | 58 | |
Chris Lattner | 10970eb | 2003-04-19 22:44:38 +0000 | [diff] [blame] | 59 | cl::opt<bool> |
| 60 | LinkAsLibrary("link-as-library", cl::desc("Link the .bc files together as a" |
| 61 | " library, not an executable")); |
| 62 | |
Chris Lattner | a856db2 | 2003-04-18 23:38:22 +0000 | [diff] [blame] | 63 | // Compatibility options that are ignored, but support by LD |
| 64 | cl::opt<std::string> |
| 65 | CO3("soname", cl::Hidden, cl::desc("Compatibility option: ignored")); |
| 66 | cl::opt<std::string> |
| 67 | CO4("version-script", cl::Hidden, cl::desc("Compatibility option: ignored")); |
| 68 | cl::opt<bool> |
| 69 | CO5("eh-frame-hdr", cl::Hidden, cl::desc("Compatibility option: ignored")); |
Chris Lattner | f3d4f17 | 2003-04-18 23:01:25 +0000 | [diff] [blame] | 70 | } |
Chris Lattner | e7fca51 | 2002-01-24 19:12:12 +0000 | [diff] [blame] | 71 | |
| 72 | // FileExists - Return true if the specified string is an openable file... |
| 73 | static inline bool FileExists(const std::string &FN) { |
| 74 | struct stat StatBuf; |
| 75 | return stat(FN.c_str(), &StatBuf) != -1; |
| 76 | } |
| 77 | |
Chris Lattner | e7fca51 | 2002-01-24 19:12:12 +0000 | [diff] [blame] | 78 | |
Chris Lattner | 10970eb | 2003-04-19 22:44:38 +0000 | [diff] [blame] | 79 | // LoadObject - Read the specified "object file", which should not search the |
| 80 | // library path to find it. |
| 81 | static inline std::auto_ptr<Module> LoadObject(const std::string &FN, |
| 82 | std::string &OutErrorMessage) { |
| 83 | if (Verbose) std::cerr << "Loading '" << FN << "'\n"; |
| 84 | if (!FileExists(FN)) { |
| 85 | OutErrorMessage = "could not find input file '" + FN + "'!"; |
| 86 | return std::auto_ptr<Module>(); |
Chris Lattner | e7fca51 | 2002-01-24 19:12:12 +0000 | [diff] [blame] | 87 | } |
| 88 | |
Chris Lattner | 10970eb | 2003-04-19 22:44:38 +0000 | [diff] [blame] | 89 | std::string ErrorMessage; |
| 90 | Module *Result = ParseBytecodeFile(FN, &ErrorMessage); |
| 91 | if (Result) return std::auto_ptr<Module>(Result); |
| 92 | |
| 93 | OutErrorMessage = "Bytecode file '" + FN + "' corrupt!"; |
| 94 | if (ErrorMessage.size()) OutErrorMessage += ": " + ErrorMessage; |
Chris Lattner | e7fca51 | 2002-01-24 19:12:12 +0000 | [diff] [blame] | 95 | return std::auto_ptr<Module>(); |
| 96 | } |
| 97 | |
| 98 | |
Chris Lattner | 10970eb | 2003-04-19 22:44:38 +0000 | [diff] [blame] | 99 | static Module *LoadSingleLibraryObject(const std::string &Filename) { |
| 100 | std::string ErrorMessage; |
| 101 | std::auto_ptr<Module> M = LoadObject(Filename, ErrorMessage); |
| 102 | if (M.get() == 0 && Verbose) { |
| 103 | std::cerr << "Error loading '" + Filename + "'"; |
| 104 | if (!ErrorMessage.empty()) std::cerr << ": " << ErrorMessage; |
| 105 | std::cerr << "\n"; |
| 106 | } |
| 107 | |
| 108 | return M.release(); |
| 109 | } |
| 110 | |
| 111 | |
| 112 | // LoadLibraryFromDirectory - This looks for a .a, .so, or .bc file in a |
| 113 | // particular directory. It returns true if no library is found, otherwise it |
| 114 | // puts the loaded modules into the Objects list, and sets isArchive to true if |
| 115 | // a .a file was loaded. |
| 116 | // |
| 117 | static inline bool LoadLibraryFromDirectory(const std::string &LibName, |
| 118 | const std::string &Directory, |
| 119 | std::vector<Module*> &Objects, |
| 120 | bool &isArchive) { |
| 121 | if (FileExists(Directory + "lib" + LibName + ".a")) { |
| 122 | std::string ErrorMessage; |
Chris Lattner | c65b104 | 2003-04-19 23:07:33 +0000 | [diff] [blame] | 123 | if (Verbose) std::cerr << " Loading '" << Directory << "lib" |
| 124 | << LibName << ".a'\n"; |
Chris Lattner | 10970eb | 2003-04-19 22:44:38 +0000 | [diff] [blame] | 125 | if (!ReadArchiveFile(Directory + "lib" + LibName + ".a", Objects, |
| 126 | &ErrorMessage)) { // Read the archive file |
| 127 | isArchive = true; |
| 128 | return false; // Success! |
| 129 | } |
| 130 | |
| 131 | if (Verbose) { |
Chris Lattner | c65b104 | 2003-04-19 23:07:33 +0000 | [diff] [blame] | 132 | std::cerr << " Error loading archive '" + Directory +"lib"+LibName+".a'"; |
Chris Lattner | 10970eb | 2003-04-19 22:44:38 +0000 | [diff] [blame] | 133 | if (!ErrorMessage.empty()) std::cerr << ": " << ErrorMessage; |
| 134 | std::cerr << "\n"; |
| 135 | } |
| 136 | } |
| 137 | |
| 138 | if (FileExists(Directory + "lib" + LibName + ".so")) |
| 139 | if (Module *M = LoadSingleLibraryObject(Directory + "lib" + LibName+".so")){ |
| 140 | isArchive = false; |
| 141 | Objects.push_back(M); |
| 142 | return false; |
| 143 | } |
| 144 | |
| 145 | if (FileExists(Directory + "lib" + LibName + ".bc")) |
| 146 | if (Module *M = LoadSingleLibraryObject(Directory + "lib" + LibName+".bc")){ |
| 147 | isArchive = false; |
| 148 | Objects.push_back(M); |
| 149 | return false; |
| 150 | } |
| 151 | return true; |
| 152 | } |
| 153 | |
| 154 | // LoadLibrary - This searches for a .a, .so, or .bc file which provides the |
| 155 | // LLVM bytecode for the library. It returns true if no library is found, |
| 156 | // otherwise it puts the loaded modules into the Objects list, and sets |
| 157 | // isArchive to true if a .a file was loaded. |
| 158 | // |
| 159 | static inline bool LoadLibrary(const std::string &LibName, |
| 160 | std::vector<Module*> &Objects, bool &isArchive, |
| 161 | std::string &ErrorMessage) { |
| 162 | std::string Directory; |
| 163 | unsigned NextLibPathIdx = 0; |
| 164 | |
| 165 | while (1) { |
| 166 | // Try loading from the current directory... |
| 167 | if (Verbose) std::cerr << " Looking in directory '" << Directory << "'\n"; |
| 168 | if (!LoadLibraryFromDirectory(LibName, Directory, Objects, isArchive)) |
| 169 | return false; |
| 170 | |
| 171 | if (NextLibPathIdx == LibPaths.size()) break; |
| 172 | Directory = LibPaths[NextLibPathIdx++]+"/"; |
| 173 | } |
| 174 | |
| 175 | ErrorMessage = "error linking library '-l" + LibName+ "': library not found!"; |
| 176 | return true; |
| 177 | } |
| 178 | |
| 179 | static void GetAllDefinedSymbols(Module *M, |
| 180 | std::set<std::string> &DefinedSymbols) { |
| 181 | for (Module::iterator I = M->begin(), E = M->end(); I != E; ++I) |
| 182 | if (I->hasName() && !I->isExternal() && !I->hasInternalLinkage()) |
| 183 | DefinedSymbols.insert(I->getName()); |
| 184 | for (Module::giterator I = M->gbegin(), E = M->gend(); I != E; ++I) |
| 185 | if (I->hasName() && !I->isExternal() && !I->hasInternalLinkage()) |
| 186 | DefinedSymbols.insert(I->getName()); |
| 187 | } |
| 188 | |
| 189 | // GetAllUndefinedSymbols - This calculates the set of undefined symbols that |
| 190 | // still exist in an LLVM module. This is a bit tricky because there may be two |
| 191 | // symbols with the same name, but different LLVM types that will be resolved to |
| 192 | // each other, but aren't currently (thus we need to treat it as resolved). |
| 193 | // |
| 194 | static void GetAllUndefinedSymbols(Module *M, |
| 195 | std::set<std::string> &UndefinedSymbols) { |
| 196 | std::set<std::string> DefinedSymbols; |
| 197 | UndefinedSymbols.clear(); // Start out empty |
| 198 | |
| 199 | for (Module::iterator I = M->begin(), E = M->end(); I != E; ++I) |
| 200 | if (I->hasName()) { |
| 201 | if (I->isExternal()) |
| 202 | UndefinedSymbols.insert(I->getName()); |
| 203 | else if (!I->hasInternalLinkage()) |
| 204 | DefinedSymbols.insert(I->getName()); |
| 205 | } |
| 206 | for (Module::giterator I = M->gbegin(), E = M->gend(); I != E; ++I) |
| 207 | if (I->hasName()) { |
| 208 | if (I->isExternal()) |
| 209 | UndefinedSymbols.insert(I->getName()); |
| 210 | else if (!I->hasInternalLinkage()) |
| 211 | DefinedSymbols.insert(I->getName()); |
| 212 | } |
| 213 | |
| 214 | // Prune out any defined symbols from the undefined symbols set... |
| 215 | for (std::set<std::string>::iterator I = UndefinedSymbols.begin(); |
| 216 | I != UndefinedSymbols.end(); ) |
| 217 | if (DefinedSymbols.count(*I)) |
| 218 | UndefinedSymbols.erase(I++); // This symbol really is defined! |
| 219 | else |
| 220 | ++I; // Keep this symbol in the undefined symbols list |
| 221 | } |
| 222 | |
| 223 | |
| 224 | static bool LinkLibrary(Module *M, const std::string &LibName, |
| 225 | std::string &ErrorMessage) { |
| 226 | std::vector<Module*> Objects; |
| 227 | bool isArchive; |
| 228 | if (LoadLibrary(LibName, Objects, isArchive, ErrorMessage)) return true; |
| 229 | |
| 230 | // Figure out which symbols are defined by all of the modules in the .a file |
| 231 | std::vector<std::set<std::string> > DefinedSymbols; |
| 232 | DefinedSymbols.resize(Objects.size()); |
| 233 | for (unsigned i = 0; i != Objects.size(); ++i) |
| 234 | GetAllDefinedSymbols(Objects[i], DefinedSymbols[i]); |
| 235 | |
| 236 | std::set<std::string> UndefinedSymbols; |
| 237 | GetAllUndefinedSymbols(M, UndefinedSymbols); |
| 238 | |
| 239 | bool Linked = true; |
| 240 | while (Linked) { // While we are linking in object files, loop. |
| 241 | Linked = false; |
| 242 | |
| 243 | for (unsigned i = 0; i != Objects.size(); ++i) { |
| 244 | // Consider whether we need to link in this module... we only need to |
| 245 | // link it in if it defines some symbol which is so far undefined. |
| 246 | // |
| 247 | const std::set<std::string> &DefSymbols = DefinedSymbols[i]; |
| 248 | |
| 249 | bool ObjectRequired = false; |
| 250 | for (std::set<std::string>::iterator I = UndefinedSymbols.begin(), |
| 251 | E = UndefinedSymbols.end(); I != E; ++I) |
| 252 | if (DefSymbols.count(*I)) { |
| 253 | if (Verbose) |
| 254 | std::cerr << " Found object providing symbol '" << *I << "'...\n"; |
| 255 | ObjectRequired = true; |
| 256 | break; |
| 257 | } |
| 258 | |
| 259 | // We DO need to link this object into the program... |
| 260 | if (ObjectRequired) { |
| 261 | if (LinkModules(M, Objects[i], &ErrorMessage)) |
| 262 | return true; // Couldn't link in the right object file... |
| 263 | |
| 264 | // Since we have linked in this object, delete it from the list of |
| 265 | // objects to consider in this archive file. |
| 266 | std::swap(Objects[i], Objects.back()); |
| 267 | std::swap(DefinedSymbols[i], DefinedSymbols.back()); |
| 268 | Objects.pop_back(); |
| 269 | DefinedSymbols.pop_back(); |
| 270 | --i; // Do not skip an entry |
| 271 | |
| 272 | // The undefined symbols set should have shrunk. |
| 273 | GetAllUndefinedSymbols(M, UndefinedSymbols); |
| 274 | Linked = true; // We have linked something in! |
| 275 | } |
| 276 | } |
| 277 | } |
| 278 | |
| 279 | return false; |
| 280 | } |
| 281 | |
| 282 | static int PrintAndReturn(const char *progname, const std::string &Message, |
| 283 | const std::string &Extra = "") { |
| 284 | std::cerr << progname << Extra << ": " << Message << "\n"; |
| 285 | return 1; |
| 286 | } |
| 287 | |
| 288 | |
Chris Lattner | e7fca51 | 2002-01-24 19:12:12 +0000 | [diff] [blame] | 289 | int main(int argc, char **argv) { |
Chris Lattner | 5ff62e9 | 2002-07-22 02:10:13 +0000 | [diff] [blame] | 290 | cl::ParseCommandLineOptions(argc, argv, " llvm linker for GCC\n"); |
Chris Lattner | e7fca51 | 2002-01-24 19:12:12 +0000 | [diff] [blame] | 291 | |
Chris Lattner | e7fca51 | 2002-01-24 19:12:12 +0000 | [diff] [blame] | 292 | std::string ErrorMessage; |
Chris Lattner | 10970eb | 2003-04-19 22:44:38 +0000 | [diff] [blame] | 293 | std::auto_ptr<Module> Composite(LoadObject(InputFilenames[0], ErrorMessage)); |
| 294 | if (Composite.get() == 0) |
| 295 | return PrintAndReturn(argv[0], ErrorMessage); |
Chris Lattner | e7fca51 | 2002-01-24 19:12:12 +0000 | [diff] [blame] | 296 | |
Chris Lattner | d34a51d | 2003-04-21 19:53:24 +0000 | [diff] [blame^] | 297 | // If the user specied an extra search path in their environment, respect it. |
| 298 | if (char *SearchPath = getenv("LLVM_LIB_SEARCH_PATH")) |
| 299 | LibPaths.push_back(SearchPath); |
| 300 | |
Chris Lattner | 10970eb | 2003-04-19 22:44:38 +0000 | [diff] [blame] | 301 | for (unsigned i = 1; i < InputFilenames.size(); ++i) { |
| 302 | std::auto_ptr<Module> M(LoadObject(InputFilenames[i], ErrorMessage)); |
| 303 | if (M.get() == 0) |
| 304 | return PrintAndReturn(argv[0], ErrorMessage); |
Chris Lattner | e7fca51 | 2002-01-24 19:12:12 +0000 | [diff] [blame] | 305 | |
Chris Lattner | f3d4f17 | 2003-04-18 23:01:25 +0000 | [diff] [blame] | 306 | if (Verbose) std::cerr << "Linking in '" << InputFilenames[i] << "'\n"; |
Chris Lattner | e7fca51 | 2002-01-24 19:12:12 +0000 | [diff] [blame] | 307 | |
Chris Lattner | 10970eb | 2003-04-19 22:44:38 +0000 | [diff] [blame] | 308 | if (LinkModules(Composite.get(), M.get(), &ErrorMessage)) |
| 309 | return PrintAndReturn(argv[0], ErrorMessage, |
| 310 | ": error linking in '" + InputFilenames[i] + "'"); |
| 311 | } |
| 312 | |
Chris Lattner | c65b104 | 2003-04-19 23:07:33 +0000 | [diff] [blame] | 313 | // Remove any consecutive duplicates of the same library... |
| 314 | Libraries.erase(std::unique(Libraries.begin(), Libraries.end()), |
| 315 | Libraries.end()); |
| 316 | |
Chris Lattner | 10970eb | 2003-04-19 22:44:38 +0000 | [diff] [blame] | 317 | // Link in all of the libraries next... |
| 318 | for (unsigned i = 0; i != Libraries.size(); ++i) { |
| 319 | if (Verbose) std::cerr << "Linking in library: -l" << Libraries[i] << "\n"; |
| 320 | if (LinkLibrary(Composite.get(), Libraries[i], ErrorMessage)) |
| 321 | return PrintAndReturn(argv[0], ErrorMessage); |
Chris Lattner | e7fca51 | 2002-01-24 19:12:12 +0000 | [diff] [blame] | 322 | } |
| 323 | |
Chris Lattner | f8b90ee | 2002-04-10 20:37:47 +0000 | [diff] [blame] | 324 | // In addition to just linking the input from GCC, we also want to spiff it up |
Chris Lattner | ad202a0 | 2002-04-08 00:14:58 +0000 | [diff] [blame] | 325 | // a little bit. Do this now. |
| 326 | // |
| 327 | PassManager Passes; |
| 328 | |
| 329 | // Linking modules together can lead to duplicated global constants, only keep |
| 330 | // one copy of each constant... |
| 331 | // |
| 332 | Passes.add(createConstantMergePass()); |
| 333 | |
Chris Lattner | 2b59837 | 2002-04-08 05:18:12 +0000 | [diff] [blame] | 334 | // If the -s command line option was specified, strip the symbols out of the |
| 335 | // resulting program to make it smaller. -s is a GCC option that we are |
| 336 | // supporting. |
| 337 | // |
| 338 | if (Strip) |
| 339 | Passes.add(createSymbolStrippingPass()); |
| 340 | |
Chris Lattner | f8b90ee | 2002-04-10 20:37:47 +0000 | [diff] [blame] | 341 | // Often if the programmer does not specify proper prototypes for the |
| 342 | // functions they are calling, they end up calling a vararg version of the |
| 343 | // function that does not get a body filled in (the real function has typed |
| 344 | // arguments). This pass merges the two functions. |
| 345 | // |
| 346 | Passes.add(createFunctionResolvingPass()); |
| 347 | |
Chris Lattner | dabaa46 | 2003-04-16 21:43:22 +0000 | [diff] [blame] | 348 | if (!NoInternalize) { |
| 349 | // Now that composite has been compiled, scan through the module, looking |
| 350 | // for a main function. If main is defined, mark all other functions |
| 351 | // internal. |
| 352 | // |
| 353 | Passes.add(createInternalizePass()); |
| 354 | } |
Chris Lattner | 2fbfdcf | 2002-04-07 20:49:59 +0000 | [diff] [blame] | 355 | |
Chris Lattner | ad202a0 | 2002-04-08 00:14:58 +0000 | [diff] [blame] | 356 | // Now that we have optimized the program, discard unreachable functions... |
| 357 | // |
| 358 | Passes.add(createGlobalDCEPass()); |
Chris Lattner | 2fbfdcf | 2002-04-07 20:49:59 +0000 | [diff] [blame] | 359 | |
Chris Lattner | ad202a0 | 2002-04-08 00:14:58 +0000 | [diff] [blame] | 360 | // Add the pass that writes bytecode to the output file... |
Chris Lattner | 10970eb | 2003-04-19 22:44:38 +0000 | [diff] [blame] | 361 | std::string RealBytecodeOutput = OutputFilename; |
| 362 | if (!LinkAsLibrary) RealBytecodeOutput += ".bc"; |
| 363 | std::ofstream Out(RealBytecodeOutput.c_str()); |
| 364 | if (!Out.good()) |
| 365 | return PrintAndReturn(argv[0], "error opening '" + RealBytecodeOutput + |
| 366 | "' for writing!"); |
Chris Lattner | ad202a0 | 2002-04-08 00:14:58 +0000 | [diff] [blame] | 367 | Passes.add(new WriteBytecodePass(&Out)); // Write bytecode to file... |
Chris Lattner | e7fca51 | 2002-01-24 19:12:12 +0000 | [diff] [blame] | 368 | |
Chris Lattner | 76d1229 | 2002-04-18 19:55:25 +0000 | [diff] [blame] | 369 | // Make sure that the Out file gets unlink'd from the disk if we get a SIGINT |
Chris Lattner | 10970eb | 2003-04-19 22:44:38 +0000 | [diff] [blame] | 370 | RemoveFileOnSignal(RealBytecodeOutput); |
Chris Lattner | 76d1229 | 2002-04-18 19:55:25 +0000 | [diff] [blame] | 371 | |
Chris Lattner | ad202a0 | 2002-04-08 00:14:58 +0000 | [diff] [blame] | 372 | // Run our queue of passes all at once now, efficiently. |
Chris Lattner | 0b12b5f | 2002-06-25 16:13:21 +0000 | [diff] [blame] | 373 | Passes.run(*Composite.get()); |
Chris Lattner | e7fca51 | 2002-01-24 19:12:12 +0000 | [diff] [blame] | 374 | Out.close(); |
| 375 | |
Chris Lattner | 10970eb | 2003-04-19 22:44:38 +0000 | [diff] [blame] | 376 | if (!LinkAsLibrary) { |
| 377 | // Output the script to start the program... |
| 378 | std::ofstream Out2(OutputFilename.c_str()); |
| 379 | if (!Out2.good()) |
| 380 | return PrintAndReturn(argv[0], "error opening '" + OutputFilename + |
| 381 | "' for writing!"); |
| 382 | Out2 << "#!/bin/sh\nlli -q -abort-on-exception $0.bc $*\n"; |
| 383 | Out2.close(); |
Chris Lattner | e7fca51 | 2002-01-24 19:12:12 +0000 | [diff] [blame] | 384 | |
Chris Lattner | 10970eb | 2003-04-19 22:44:38 +0000 | [diff] [blame] | 385 | // Make the script executable... |
| 386 | chmod(OutputFilename.c_str(), 0755); |
| 387 | } |
Chris Lattner | e7fca51 | 2002-01-24 19:12:12 +0000 | [diff] [blame] | 388 | |
| 389 | return 0; |
| 390 | } |