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