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