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, |
Brian Gaeke | 69a7960 | 2003-05-23 20:27:07 +0000 | [diff] [blame] | 12 | // I'm not too worried about this. |
Chris Lattner | e7fca51 | 2002-01-24 19:12:12 +0000 | [diff] [blame] | 13 | // |
| 14 | //===----------------------------------------------------------------------===// |
| 15 | |
Chris Lattner | 6e27123 | 2003-09-22 20:21:34 +0000 | [diff] [blame] | 16 | #include "gccld.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 | 9c3b55e | 2003-04-24 19:13:02 +0000 | [diff] [blame] | 21 | #include "llvm/Target/TargetData.h" |
Chris Lattner | d9d8c07 | 2002-07-23 22:04:43 +0000 | [diff] [blame] | 22 | #include "llvm/Transforms/IPO.h" |
Chris Lattner | d9d8c07 | 2002-07-23 22:04:43 +0000 | [diff] [blame] | 23 | #include "llvm/Transforms/Scalar.h" |
Misha Brukman | e97fbed | 2003-09-30 17:59:25 +0000 | [diff] [blame] | 24 | #include "llvm/Transforms/Utils/Linker.h" |
Chris Lattner | e7fca51 | 2002-01-24 19:12:12 +0000 | [diff] [blame] | 25 | #include "Support/CommandLine.h" |
Misha Brukman | e97fbed | 2003-09-30 17:59:25 +0000 | [diff] [blame] | 26 | #include "Support/FileUtilities.h" |
Chris Lattner | 76d1229 | 2002-04-18 19:55:25 +0000 | [diff] [blame] | 27 | #include "Support/Signals.h" |
Misha Brukman | e97fbed | 2003-09-30 17:59:25 +0000 | [diff] [blame] | 28 | #include "Support/SystemUtils.h" |
Chris Lattner | e7fca51 | 2002-01-24 19:12:12 +0000 | [diff] [blame] | 29 | #include <fstream> |
| 30 | #include <memory> |
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 | a2b2dc9 | 2003-08-22 19:18:45 +0000 | [diff] [blame] | 58 | static cl::alias |
| 59 | ExportDynamic("export-dynamic", cl::desc("Alias for -disable-internalize"), |
| 60 | cl::aliasopt(NoInternalize)); |
Chris Lattner | a856db2 | 2003-04-18 23:38:22 +0000 | [diff] [blame] | 61 | |
Chris Lattner | 10970eb | 2003-04-19 22:44:38 +0000 | [diff] [blame] | 62 | cl::opt<bool> |
| 63 | LinkAsLibrary("link-as-library", cl::desc("Link the .bc files together as a" |
| 64 | " library, not an executable")); |
| 65 | |
John Criswell | dabaf7d | 2003-09-16 21:27:35 +0000 | [diff] [blame] | 66 | cl::opt<bool> |
Chris Lattner | 6e27123 | 2003-09-22 20:21:34 +0000 | [diff] [blame] | 67 | Native("native", |
| 68 | cl::desc("Generate a native binary instead of a shell script")); |
John Criswell | dabaf7d | 2003-09-16 21:27:35 +0000 | [diff] [blame] | 69 | |
John Criswell | dc0de4f | 2003-09-18 16:22:26 +0000 | [diff] [blame] | 70 | // Compatibility options that are ignored but supported by LD |
Chris Lattner | a856db2 | 2003-04-18 23:38:22 +0000 | [diff] [blame] | 71 | cl::opt<std::string> |
| 72 | CO3("soname", cl::Hidden, cl::desc("Compatibility option: ignored")); |
| 73 | cl::opt<std::string> |
| 74 | CO4("version-script", cl::Hidden, cl::desc("Compatibility option: ignored")); |
| 75 | cl::opt<bool> |
| 76 | CO5("eh-frame-hdr", cl::Hidden, cl::desc("Compatibility option: ignored")); |
Chris Lattner | 6ac79d1 | 2003-05-27 19:15:11 +0000 | [diff] [blame] | 77 | cl::opt<bool> |
| 78 | CO6("r", cl::Hidden, cl::desc("Compatibility option: ignored")); |
Chris Lattner | f3d4f17 | 2003-04-18 23:01:25 +0000 | [diff] [blame] | 79 | } |
Chris Lattner | e7fca51 | 2002-01-24 19:12:12 +0000 | [diff] [blame] | 80 | |
Chris Lattner | 10970eb | 2003-04-19 22:44:38 +0000 | [diff] [blame] | 81 | // |
John Criswell | 71478b7 | 2003-09-19 20:24:23 +0000 | [diff] [blame] | 82 | // Function: PrintAndReturn () |
| 83 | // |
| 84 | // Description: |
| 85 | // Prints a message (usually error message) to standard error (stderr) and |
| 86 | // returns a value usable for an exit status. |
| 87 | // |
| 88 | // Inputs: |
| 89 | // progname - The name of the program (i.e. argv[0]). |
| 90 | // Message - The message to print to standard error. |
| 91 | // Extra - Extra information to print between the program name and thei |
| 92 | // message. It is optional. |
| 93 | // |
| 94 | // Outputs: |
| 95 | // None. |
| 96 | // |
| 97 | // Return value: |
| 98 | // Returns a value that can be used as the exit status (i.e. for exit()). |
| 99 | // |
| 100 | int |
| 101 | PrintAndReturn (const char *progname, |
| 102 | const std::string &Message, |
| 103 | const std::string &Extra) |
| 104 | { |
| 105 | std::cerr << progname << Extra << ": " << Message << "\n"; |
| 106 | return 1; |
Chris Lattner | 10970eb | 2003-04-19 22:44:38 +0000 | [diff] [blame] | 107 | } |
| 108 | |
John Criswell | 71478b7 | 2003-09-19 20:24:23 +0000 | [diff] [blame] | 109 | // |
| 110 | // |
| 111 | // Function: CopyEnv() |
| 112 | // |
| 113 | // Description: |
| 114 | // This function takes an array of environment variables and makes a |
| 115 | // copy of it. This copy can then be manipulated any way the caller likes |
| 116 | // without affecting the process's real environment. |
| 117 | // |
| 118 | // Inputs: |
| 119 | // envp - An array of C strings containing an environment. |
| 120 | // |
| 121 | // Outputs: |
| 122 | // None. |
| 123 | // |
| 124 | // Return value: |
| 125 | // NULL - An error occurred. |
| 126 | // |
| 127 | // Otherwise, a pointer to a new array of C strings is returned. Every string |
| 128 | // in the array is a duplicate of the one in the original array (i.e. we do |
| 129 | // not copy the char *'s from one array to another). |
| 130 | // |
Misha Brukman | e97fbed | 2003-09-30 17:59:25 +0000 | [diff] [blame] | 131 | char ** CopyEnv(char ** const envp) { |
John Criswell | 71478b7 | 2003-09-19 20:24:23 +0000 | [diff] [blame] | 132 | // Count the number of entries in the old list; |
Chris Lattner | 6e27123 | 2003-09-22 20:21:34 +0000 | [diff] [blame] | 133 | unsigned entries; // The number of entries in the old environment list |
John Criswell | 71478b7 | 2003-09-19 20:24:23 +0000 | [diff] [blame] | 134 | for (entries = 0; envp[entries] != NULL; entries++) |
| 135 | { |
| 136 | ; |
Chris Lattner | 7cb77e1 | 2003-05-13 22:14:13 +0000 | [diff] [blame] | 137 | } |
| 138 | |
John Criswell | 71478b7 | 2003-09-19 20:24:23 +0000 | [diff] [blame] | 139 | // Add one more entry for the NULL pointer that ends the list. |
John Criswell | 71478b7 | 2003-09-19 20:24:23 +0000 | [diff] [blame] | 140 | ++entries; |
Chris Lattner | 10970eb | 2003-04-19 22:44:38 +0000 | [diff] [blame] | 141 | |
John Criswell | 71478b7 | 2003-09-19 20:24:23 +0000 | [diff] [blame] | 142 | // If there are no entries at all, just return NULL. |
John Criswell | 71478b7 | 2003-09-19 20:24:23 +0000 | [diff] [blame] | 143 | if (entries == 0) |
John Criswell | 71478b7 | 2003-09-19 20:24:23 +0000 | [diff] [blame] | 144 | return NULL; |
Chris Lattner | 10970eb | 2003-04-19 22:44:38 +0000 | [diff] [blame] | 145 | |
John Criswell | 71478b7 | 2003-09-19 20:24:23 +0000 | [diff] [blame] | 146 | // Allocate a new environment list. |
Chris Lattner | 6e27123 | 2003-09-22 20:21:34 +0000 | [diff] [blame] | 147 | char **newenv; |
John Criswell | 71478b7 | 2003-09-19 20:24:23 +0000 | [diff] [blame] | 148 | if ((newenv = new (char *) [entries]) == NULL) |
John Criswell | 71478b7 | 2003-09-19 20:24:23 +0000 | [diff] [blame] | 149 | return NULL; |
Chris Lattner | 10970eb | 2003-04-19 22:44:38 +0000 | [diff] [blame] | 150 | |
John Criswell | 71478b7 | 2003-09-19 20:24:23 +0000 | [diff] [blame] | 151 | // Make a copy of the list. Don't forget the NULL that ends the list. |
John Criswell | 71478b7 | 2003-09-19 20:24:23 +0000 | [diff] [blame] | 152 | entries = 0; |
Misha Brukman | e97fbed | 2003-09-30 17:59:25 +0000 | [diff] [blame] | 153 | while (envp[entries] != NULL) { |
John Criswell | 71478b7 | 2003-09-19 20:24:23 +0000 | [diff] [blame] | 154 | newenv[entries] = new char[strlen (envp[entries]) + 1]; |
| 155 | strcpy (newenv[entries], envp[entries]); |
| 156 | ++entries; |
| 157 | } |
| 158 | newenv[entries] = NULL; |
Chris Lattner | 10970eb | 2003-04-19 22:44:38 +0000 | [diff] [blame] | 159 | |
John Criswell | 71478b7 | 2003-09-19 20:24:23 +0000 | [diff] [blame] | 160 | return newenv; |
| 161 | } |
| 162 | |
| 163 | |
| 164 | // |
| 165 | // Function: RemoveEnv() |
| 166 | // |
| 167 | // Description: |
| 168 | // Remove the specified environment variable from the environment array. |
| 169 | // |
| 170 | // Inputs: |
| 171 | // name - The name of the variable to remove. It cannot be NULL. |
| 172 | // envp - The array of environment variables. It cannot be NULL. |
| 173 | // |
| 174 | // Outputs: |
| 175 | // envp - The pointer to the specified variable name is removed. |
| 176 | // |
| 177 | // Return value: |
| 178 | // None. |
| 179 | // |
| 180 | // Notes: |
| 181 | // This is mainly done because functions to remove items from the environment |
| 182 | // are not available across all platforms. In particular, Solaris does not |
| 183 | // seem to have an unsetenv() function or a setenv() function (or they are |
| 184 | // undocumented if they do exist). |
| 185 | // |
Misha Brukman | e97fbed | 2003-09-30 17:59:25 +0000 | [diff] [blame] | 186 | void RemoveEnv(const char * name, char ** const envp) { |
Chris Lattner | 6e27123 | 2003-09-22 20:21:34 +0000 | [diff] [blame] | 187 | for (unsigned index=0; envp[index] != NULL; index++) { |
John Criswell | 71478b7 | 2003-09-19 20:24:23 +0000 | [diff] [blame] | 188 | // Find the first equals sign in the array and make it an EOS character. |
Chris Lattner | 6e27123 | 2003-09-22 20:21:34 +0000 | [diff] [blame] | 189 | char *p = strchr (envp[index], '='); |
John Criswell | 71478b7 | 2003-09-19 20:24:23 +0000 | [diff] [blame] | 190 | if (p == NULL) |
John Criswell | 71478b7 | 2003-09-19 20:24:23 +0000 | [diff] [blame] | 191 | continue; |
John Criswell | 71478b7 | 2003-09-19 20:24:23 +0000 | [diff] [blame] | 192 | else |
John Criswell | 71478b7 | 2003-09-19 20:24:23 +0000 | [diff] [blame] | 193 | *p = '\0'; |
John Criswell | 71478b7 | 2003-09-19 20:24:23 +0000 | [diff] [blame] | 194 | |
John Criswell | 71478b7 | 2003-09-19 20:24:23 +0000 | [diff] [blame] | 195 | // Compare the two strings. If they are equal, zap this string. |
| 196 | // Otherwise, restore it. |
Misha Brukman | e97fbed | 2003-09-30 17:59:25 +0000 | [diff] [blame] | 197 | if (!strcmp(name, envp[index])) |
John Criswell | 71478b7 | 2003-09-19 20:24:23 +0000 | [diff] [blame] | 198 | *envp[index] = '\0'; |
John Criswell | 71478b7 | 2003-09-19 20:24:23 +0000 | [diff] [blame] | 199 | else |
John Criswell | 71478b7 | 2003-09-19 20:24:23 +0000 | [diff] [blame] | 200 | *p = '='; |
Chris Lattner | 10970eb | 2003-04-19 22:44:38 +0000 | [diff] [blame] | 201 | } |
John Criswell | 71478b7 | 2003-09-19 20:24:23 +0000 | [diff] [blame] | 202 | |
| 203 | return; |
Chris Lattner | 10970eb | 2003-04-19 22:44:38 +0000 | [diff] [blame] | 204 | } |
| 205 | |
Chris Lattner | 10970eb | 2003-04-19 22:44:38 +0000 | [diff] [blame] | 206 | |
Misha Brukman | e97fbed | 2003-09-30 17:59:25 +0000 | [diff] [blame] | 207 | int main(int argc, char **argv, char **envp) { |
Chris Lattner | 5ff62e9 | 2002-07-22 02:10:13 +0000 | [diff] [blame] | 208 | cl::ParseCommandLineOptions(argc, argv, " llvm linker for GCC\n"); |
Chris Lattner | e7fca51 | 2002-01-24 19:12:12 +0000 | [diff] [blame] | 209 | |
Chris Lattner | e7fca51 | 2002-01-24 19:12:12 +0000 | [diff] [blame] | 210 | std::string ErrorMessage; |
Chris Lattner | 10970eb | 2003-04-19 22:44:38 +0000 | [diff] [blame] | 211 | std::auto_ptr<Module> Composite(LoadObject(InputFilenames[0], ErrorMessage)); |
| 212 | if (Composite.get() == 0) |
| 213 | return PrintAndReturn(argv[0], ErrorMessage); |
Chris Lattner | e7fca51 | 2002-01-24 19:12:12 +0000 | [diff] [blame] | 214 | |
Brian Gaeke | 69a7960 | 2003-05-23 20:27:07 +0000 | [diff] [blame] | 215 | // We always look first in the current directory when searching for libraries. |
| 216 | LibPaths.insert(LibPaths.begin(), "."); |
| 217 | |
Misha Brukman | cf00c4a | 2003-10-10 17:57:28 +0000 | [diff] [blame^] | 218 | // If the user specified an extra search path in their environment, respect |
| 219 | // it. |
Chris Lattner | d34a51d | 2003-04-21 19:53:24 +0000 | [diff] [blame] | 220 | if (char *SearchPath = getenv("LLVM_LIB_SEARCH_PATH")) |
| 221 | LibPaths.push_back(SearchPath); |
| 222 | |
Chris Lattner | c65b104 | 2003-04-19 23:07:33 +0000 | [diff] [blame] | 223 | // Remove any consecutive duplicates of the same library... |
| 224 | Libraries.erase(std::unique(Libraries.begin(), Libraries.end()), |
| 225 | Libraries.end()); |
| 226 | |
John Criswell | 71478b7 | 2003-09-19 20:24:23 +0000 | [diff] [blame] | 227 | // Link in all of the files |
Brian Gaeke | e98ddfc | 2003-09-30 14:03:48 +0000 | [diff] [blame] | 228 | if (LinkFiles(argv[0], Composite.get(), InputFilenames, Verbose)) |
| 229 | return 1; // Error already printed |
Chris Lattner | 6e27123 | 2003-09-22 20:21:34 +0000 | [diff] [blame] | 230 | LinkLibraries(argv[0], Composite.get(), Libraries, LibPaths, Verbose, Native); |
John Criswell | 71478b7 | 2003-09-19 20:24:23 +0000 | [diff] [blame] | 231 | |
Chris Lattner | 10970eb | 2003-04-19 22:44:38 +0000 | [diff] [blame] | 232 | // Link in all of the libraries next... |
Chris Lattner | e7fca51 | 2002-01-24 19:12:12 +0000 | [diff] [blame] | 233 | |
John Criswell | dc0de4f | 2003-09-18 16:22:26 +0000 | [diff] [blame] | 234 | // Create the output file. |
Chris Lattner | 10970eb | 2003-04-19 22:44:38 +0000 | [diff] [blame] | 235 | std::string RealBytecodeOutput = OutputFilename; |
| 236 | if (!LinkAsLibrary) RealBytecodeOutput += ".bc"; |
| 237 | std::ofstream Out(RealBytecodeOutput.c_str()); |
| 238 | if (!Out.good()) |
| 239 | return PrintAndReturn(argv[0], "error opening '" + RealBytecodeOutput + |
| 240 | "' for writing!"); |
Chris Lattner | e7fca51 | 2002-01-24 19:12:12 +0000 | [diff] [blame] | 241 | |
John Criswell | dc0de4f | 2003-09-18 16:22:26 +0000 | [diff] [blame] | 242 | // Ensure that the bytecode file gets removed from the disk if we get a |
| 243 | // SIGINT signal. |
Chris Lattner | 10970eb | 2003-04-19 22:44:38 +0000 | [diff] [blame] | 244 | RemoveFileOnSignal(RealBytecodeOutput); |
Chris Lattner | 76d1229 | 2002-04-18 19:55:25 +0000 | [diff] [blame] | 245 | |
John Criswell | dc0de4f | 2003-09-18 16:22:26 +0000 | [diff] [blame] | 246 | // Generate the bytecode file. |
Misha Brukman | e97fbed | 2003-09-30 17:59:25 +0000 | [diff] [blame] | 247 | if (GenerateBytecode(Composite.get(), Strip, !NoInternalize, &Out)) { |
John Criswell | dc0de4f | 2003-09-18 16:22:26 +0000 | [diff] [blame] | 248 | Out.close(); |
| 249 | return PrintAndReturn(argv[0], "error generating bytcode"); |
| 250 | } |
| 251 | |
John Criswell | dc0de4f | 2003-09-18 16:22:26 +0000 | [diff] [blame] | 252 | // Close the bytecode file. |
Chris Lattner | e7fca51 | 2002-01-24 19:12:12 +0000 | [diff] [blame] | 253 | Out.close(); |
| 254 | |
John Criswell | dc0de4f | 2003-09-18 16:22:26 +0000 | [diff] [blame] | 255 | // If we are not linking a library, generate either a native executable |
| 256 | // or a JIT shell script, depending upon what the user wants. |
Chris Lattner | 10970eb | 2003-04-19 22:44:38 +0000 | [diff] [blame] | 257 | if (!LinkAsLibrary) { |
John Criswell | dabaf7d | 2003-09-16 21:27:35 +0000 | [diff] [blame] | 258 | // If the user wants to generate a native executable, compile it from the |
| 259 | // bytecode file. |
| 260 | // |
| 261 | // Otherwise, create a script that will run the bytecode through the JIT. |
Chris Lattner | 6e27123 | 2003-09-22 20:21:34 +0000 | [diff] [blame] | 262 | if (Native) { |
John Criswell | dc0de4f | 2003-09-18 16:22:26 +0000 | [diff] [blame] | 263 | // Name of the Assembly Language output file |
| 264 | std::string AssemblyFile = OutputFilename + ".s"; |
| 265 | |
John Criswell | dc0de4f | 2003-09-18 16:22:26 +0000 | [diff] [blame] | 266 | // Mark the output files for removal if we get an interrupt. |
Misha Brukman | e97fbed | 2003-09-30 17:59:25 +0000 | [diff] [blame] | 267 | RemoveFileOnSignal(AssemblyFile); |
| 268 | RemoveFileOnSignal(OutputFilename); |
John Criswell | dabaf7d | 2003-09-16 21:27:35 +0000 | [diff] [blame] | 269 | |
John Criswell | 83ca6ec | 2003-09-17 19:04:22 +0000 | [diff] [blame] | 270 | // Determine the locations of the llc and gcc programs. |
Misha Brukman | e97fbed | 2003-09-30 17:59:25 +0000 | [diff] [blame] | 271 | std::string llc = FindExecutable("llc", argv[0]); |
| 272 | std::string gcc = FindExecutable("gcc", argv[0]); |
John Criswell | 83ca6ec | 2003-09-17 19:04:22 +0000 | [diff] [blame] | 273 | if (llc.empty()) |
Misha Brukman | e97fbed | 2003-09-30 17:59:25 +0000 | [diff] [blame] | 274 | return PrintAndReturn(argv[0], "Failed to find llc"); |
John Criswell | 83ca6ec | 2003-09-17 19:04:22 +0000 | [diff] [blame] | 275 | |
| 276 | if (gcc.empty()) |
Misha Brukman | e97fbed | 2003-09-30 17:59:25 +0000 | [diff] [blame] | 277 | return PrintAndReturn(argv[0], "Failed to find gcc"); |
John Criswell | 83ca6ec | 2003-09-17 19:04:22 +0000 | [diff] [blame] | 278 | |
John Criswell | dc0de4f | 2003-09-18 16:22:26 +0000 | [diff] [blame] | 279 | // Generate an assembly language file for the bytecode. |
John Criswell | 71478b7 | 2003-09-19 20:24:23 +0000 | [diff] [blame] | 280 | if (Verbose) std::cout << "Generating Assembly Code\n"; |
Chris Lattner | 6e27123 | 2003-09-22 20:21:34 +0000 | [diff] [blame] | 281 | GenerateAssembly(AssemblyFile, RealBytecodeOutput, llc, envp); |
John Criswell | 71478b7 | 2003-09-19 20:24:23 +0000 | [diff] [blame] | 282 | if (Verbose) std::cout << "Generating Native Code\n"; |
Chris Lattner | 6e27123 | 2003-09-22 20:21:34 +0000 | [diff] [blame] | 283 | GenerateNative(OutputFilename, AssemblyFile, Libraries, LibPaths, |
| 284 | gcc, envp); |
John Criswell | dabaf7d | 2003-09-16 21:27:35 +0000 | [diff] [blame] | 285 | |
John Criswell | dc0de4f | 2003-09-18 16:22:26 +0000 | [diff] [blame] | 286 | // Remove the assembly language file. |
John Criswell | dc0de4f | 2003-09-18 16:22:26 +0000 | [diff] [blame] | 287 | removeFile (AssemblyFile); |
Chris Lattner | 6e27123 | 2003-09-22 20:21:34 +0000 | [diff] [blame] | 288 | } else { |
John Criswell | dabaf7d | 2003-09-16 21:27:35 +0000 | [diff] [blame] | 289 | // Output the script to start the program... |
| 290 | std::ofstream Out2(OutputFilename.c_str()); |
| 291 | if (!Out2.good()) |
| 292 | return PrintAndReturn(argv[0], "error opening '" + OutputFilename + |
| 293 | "' for writing!"); |
| 294 | Out2 << "#!/bin/sh\nlli -q $0.bc $*\n"; |
| 295 | Out2.close(); |
| 296 | } |
Chris Lattner | e7fca51 | 2002-01-24 19:12:12 +0000 | [diff] [blame] | 297 | |
Chris Lattner | 10970eb | 2003-04-19 22:44:38 +0000 | [diff] [blame] | 298 | // Make the script executable... |
Misha Brukman | e97fbed | 2003-09-30 17:59:25 +0000 | [diff] [blame] | 299 | MakeFileExecutable(OutputFilename); |
Misha Brukman | c1fdca8 | 2003-08-20 20:38:15 +0000 | [diff] [blame] | 300 | |
John Criswell | 22edc39 | 2003-09-02 21:11:22 +0000 | [diff] [blame] | 301 | // Make the bytecode file readable and directly executable in LLEE as well |
Misha Brukman | e97fbed | 2003-09-30 17:59:25 +0000 | [diff] [blame] | 302 | MakeFileExecutable(RealBytecodeOutput); |
| 303 | MakeFileReadable(RealBytecodeOutput); |
Chris Lattner | 10970eb | 2003-04-19 22:44:38 +0000 | [diff] [blame] | 304 | } |
Chris Lattner | e7fca51 | 2002-01-24 19:12:12 +0000 | [diff] [blame] | 305 | |
| 306 | return 0; |
| 307 | } |