Reid Spencer | c0af3f0 | 2004-09-13 01:27:53 +0000 | [diff] [blame] | 1 | //===- llvm-ld.cpp - LLVM 'ld' compatible linker --------------------------===// |
Misha Brukman | 3da94ae | 2005-04-22 00:00:37 +0000 | [diff] [blame] | 2 | // |
Reid Spencer | c0af3f0 | 2004-09-13 01:27:53 +0000 | [diff] [blame] | 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. |
Misha Brukman | 3da94ae | 2005-04-22 00:00:37 +0000 | [diff] [blame] | 7 | // |
Reid Spencer | c0af3f0 | 2004-09-13 01:27:53 +0000 | [diff] [blame] | 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This utility is intended to be compatible with GCC, and follows standard |
| 11 | // system 'ld' conventions. As such, the default output file is ./a.out. |
| 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, |
| 19 | // I'm not too worried about this. |
| 20 | // |
| 21 | //===----------------------------------------------------------------------===// |
| 22 | |
Reid Spencer | af303d5 | 2006-06-07 23:03:13 +0000 | [diff] [blame] | 23 | #include "llvm/LinkAllVMCore.h" |
Reid Spencer | 605b9e2 | 2004-11-14 23:00:08 +0000 | [diff] [blame] | 24 | #include "llvm/Linker.h" |
Reid Spencer | 6da1e0d | 2004-12-14 04:20:08 +0000 | [diff] [blame] | 25 | #include "llvm/System/Program.h" |
Reid Spencer | c0af3f0 | 2004-09-13 01:27:53 +0000 | [diff] [blame] | 26 | #include "llvm/Module.h" |
| 27 | #include "llvm/PassManager.h" |
| 28 | #include "llvm/Bytecode/Reader.h" |
Reid Spencer | 445564a | 2004-11-20 20:02:56 +0000 | [diff] [blame] | 29 | #include "llvm/Bytecode/Writer.h" |
Reid Spencer | c0af3f0 | 2004-09-13 01:27:53 +0000 | [diff] [blame] | 30 | #include "llvm/Target/TargetData.h" |
Reid Spencer | aefd04b | 2004-09-25 16:00:07 +0000 | [diff] [blame] | 31 | #include "llvm/Target/TargetMachine.h" |
| 32 | #include "llvm/Target/TargetMachineRegistry.h" |
Reid Spencer | c0af3f0 | 2004-09-13 01:27:53 +0000 | [diff] [blame] | 33 | #include "llvm/Support/CommandLine.h" |
| 34 | #include "llvm/Support/FileUtilities.h" |
Chris Lattner | c30598b | 2006-12-06 01:18:01 +0000 | [diff] [blame] | 35 | #include "llvm/Support/ManagedStatic.h" |
Bill Wendling | 68fe61d | 2006-11-29 00:19:40 +0000 | [diff] [blame] | 36 | #include "llvm/Support/Streams.h" |
Reid Spencer | c0af3f0 | 2004-09-13 01:27:53 +0000 | [diff] [blame] | 37 | #include "llvm/Support/SystemUtils.h" |
Reid Spencer | 445564a | 2004-11-20 20:02:56 +0000 | [diff] [blame] | 38 | #include "llvm/System/Signals.h" |
Reid Spencer | c0af3f0 | 2004-09-13 01:27:53 +0000 | [diff] [blame] | 39 | #include <fstream> |
| 40 | #include <memory> |
| 41 | using namespace llvm; |
| 42 | |
Reid Spencer | 445564a | 2004-11-20 20:02:56 +0000 | [diff] [blame] | 43 | // Input/Output Options |
| 44 | static cl::list<std::string> InputFilenames(cl::Positional, cl::OneOrMore, |
| 45 | cl::desc("<input bytecode files>")); |
Reid Spencer | c0af3f0 | 2004-09-13 01:27:53 +0000 | [diff] [blame] | 46 | |
Reid Spencer | 445564a | 2004-11-20 20:02:56 +0000 | [diff] [blame] | 47 | static cl::opt<std::string> OutputFilename("o", cl::init("a.out"), |
Misha Brukman | 3da94ae | 2005-04-22 00:00:37 +0000 | [diff] [blame] | 48 | cl::desc("Override output filename"), |
Reid Spencer | 445564a | 2004-11-20 20:02:56 +0000 | [diff] [blame] | 49 | cl::value_desc("filename")); |
Reid Spencer | c0af3f0 | 2004-09-13 01:27:53 +0000 | [diff] [blame] | 50 | |
Misha Brukman | 3da94ae | 2005-04-22 00:00:37 +0000 | [diff] [blame] | 51 | static cl::opt<bool> Verbose("v", |
Reid Spencer | 445564a | 2004-11-20 20:02:56 +0000 | [diff] [blame] | 52 | cl::desc("Print information about actions taken")); |
Misha Brukman | 3da94ae | 2005-04-22 00:00:37 +0000 | [diff] [blame] | 53 | |
Reid Spencer | 445564a | 2004-11-20 20:02:56 +0000 | [diff] [blame] | 54 | static cl::list<std::string> LibPaths("L", cl::Prefix, |
Misha Brukman | 3da94ae | 2005-04-22 00:00:37 +0000 | [diff] [blame] | 55 | cl::desc("Specify a library search path"), |
Reid Spencer | 445564a | 2004-11-20 20:02:56 +0000 | [diff] [blame] | 56 | cl::value_desc("directory")); |
Reid Spencer | c0af3f0 | 2004-09-13 01:27:53 +0000 | [diff] [blame] | 57 | |
Reid Spencer | 445564a | 2004-11-20 20:02:56 +0000 | [diff] [blame] | 58 | static cl::list<std::string> Libraries("l", cl::Prefix, |
Misha Brukman | 3da94ae | 2005-04-22 00:00:37 +0000 | [diff] [blame] | 59 | cl::desc("Specify libraries to link to"), |
Reid Spencer | 445564a | 2004-11-20 20:02:56 +0000 | [diff] [blame] | 60 | cl::value_desc("library prefix")); |
Reid Spencer | c0af3f0 | 2004-09-13 01:27:53 +0000 | [diff] [blame] | 61 | |
Reid Spencer | 708585a | 2007-02-09 03:08:06 +0000 | [diff] [blame] | 62 | // Options to control the linking, optimization, and code gen processes |
Misha Brukman | 3da94ae | 2005-04-22 00:00:37 +0000 | [diff] [blame] | 63 | static cl::opt<bool> LinkAsLibrary("link-as-library", |
Reid Spencer | 445564a | 2004-11-20 20:02:56 +0000 | [diff] [blame] | 64 | cl::desc("Link the .bc files together as a library, not an executable")); |
Reid Spencer | c0af3f0 | 2004-09-13 01:27:53 +0000 | [diff] [blame] | 65 | |
Reid Spencer | 445564a | 2004-11-20 20:02:56 +0000 | [diff] [blame] | 66 | static cl::alias Relink("r", cl::aliasopt(LinkAsLibrary), |
| 67 | cl::desc("Alias for -link-as-library")); |
Reid Spencer | c0af3f0 | 2004-09-13 01:27:53 +0000 | [diff] [blame] | 68 | |
Reid Spencer | 445564a | 2004-11-20 20:02:56 +0000 | [diff] [blame] | 69 | static cl::opt<const TargetMachineRegistry::Entry*, false, TargetNameParser> |
Reid Spencer | aefd04b | 2004-09-25 16:00:07 +0000 | [diff] [blame] | 70 | MachineArch("march", cl::desc("Architecture to generate assembly for:")); |
| 71 | |
Reid Spencer | 445564a | 2004-11-20 20:02:56 +0000 | [diff] [blame] | 72 | static cl::opt<bool> Native("native", |
| 73 | cl::desc("Generate a native binary instead of a shell script")); |
Reid Spencer | c0af3f0 | 2004-09-13 01:27:53 +0000 | [diff] [blame] | 74 | |
Reid Spencer | 445564a | 2004-11-20 20:02:56 +0000 | [diff] [blame] | 75 | static cl::opt<bool>NativeCBE("native-cbe", |
| 76 | cl::desc("Generate a native binary with the C backend and GCC")); |
| 77 | |
Chris Lattner | 17be679 | 2007-01-21 06:34:18 +0000 | [diff] [blame] | 78 | static cl::opt<bool>DisableCompression("disable-compression", cl::init(true), |
Reid Spencer | 445564a | 2004-11-20 20:02:56 +0000 | [diff] [blame] | 79 | cl::desc("Disable writing of compressed bytecode files")); |
Misha Brukman | 3da94ae | 2005-04-22 00:00:37 +0000 | [diff] [blame] | 80 | |
Reid Spencer | 73a74be | 2005-12-21 05:03:23 +0000 | [diff] [blame] | 81 | static cl::list<std::string> PostLinkOpts("post-link-opts", |
Reid Spencer | af303d5 | 2006-06-07 23:03:13 +0000 | [diff] [blame] | 82 | cl::value_desc("path"), |
Reid Spencer | 73a74be | 2005-12-21 05:03:23 +0000 | [diff] [blame] | 83 | cl::desc("Run one or more optimization programs after linking")); |
| 84 | |
Reid Spencer | af303d5 | 2006-06-07 23:03:13 +0000 | [diff] [blame] | 85 | static cl::list<std::string> XLinker("Xlinker", cl::value_desc("option"), |
| 86 | cl::desc("Pass options to the system linker")); |
| 87 | |
Reid Spencer | 708585a | 2007-02-09 03:08:06 +0000 | [diff] [blame] | 88 | // Compatibility options that llvm-ld ignores but are supported for |
| 89 | // compatibility with LD |
Misha Brukman | 3da94ae | 2005-04-22 00:00:37 +0000 | [diff] [blame] | 90 | static cl::opt<std::string> CO3("soname", cl::Hidden, |
Reid Spencer | 445564a | 2004-11-20 20:02:56 +0000 | [diff] [blame] | 91 | cl::desc("Compatibility option: ignored")); |
| 92 | |
Misha Brukman | 3da94ae | 2005-04-22 00:00:37 +0000 | [diff] [blame] | 93 | static cl::opt<std::string> CO4("version-script", cl::Hidden, |
Reid Spencer | 445564a | 2004-11-20 20:02:56 +0000 | [diff] [blame] | 94 | cl::desc("Compatibility option: ignored")); |
| 95 | |
Misha Brukman | 3da94ae | 2005-04-22 00:00:37 +0000 | [diff] [blame] | 96 | static cl::opt<bool> CO5("eh-frame-hdr", cl::Hidden, |
Reid Spencer | 445564a | 2004-11-20 20:02:56 +0000 | [diff] [blame] | 97 | cl::desc("Compatibility option: ignored")); |
| 98 | |
Misha Brukman | 3da94ae | 2005-04-22 00:00:37 +0000 | [diff] [blame] | 99 | static cl::opt<std::string> CO6("h", cl::Hidden, |
Reid Spencer | 445564a | 2004-11-20 20:02:56 +0000 | [diff] [blame] | 100 | cl::desc("Compatibility option: ignored")); |
| 101 | |
Reid Spencer | 98a030c | 2007-02-08 19:03:11 +0000 | [diff] [blame] | 102 | static cl::opt<bool> CO7("start-group", cl::Hidden, |
| 103 | cl::desc("Compatibility option: ignored")); |
| 104 | |
| 105 | static cl::opt<bool> CO8("end-group", cl::Hidden, |
| 106 | cl::desc("Compatibility option: ignored")); |
Reid Spencer | af303d5 | 2006-06-07 23:03:13 +0000 | [diff] [blame] | 107 | |
Reid Spencer | 445564a | 2004-11-20 20:02:56 +0000 | [diff] [blame] | 108 | /// This is just for convenience so it doesn't have to be passed around |
| 109 | /// everywhere. |
Reid Spencer | c406413 | 2004-12-13 03:01:14 +0000 | [diff] [blame] | 110 | static std::string progname; |
Reid Spencer | c0af3f0 | 2004-09-13 01:27:53 +0000 | [diff] [blame] | 111 | |
Reid Spencer | 708585a | 2007-02-09 03:08:06 +0000 | [diff] [blame] | 112 | /// PrintAndExit - Prints a message to standard error and exits with error code |
Reid Spencer | c0af3f0 | 2004-09-13 01:27:53 +0000 | [diff] [blame] | 113 | /// |
| 114 | /// Inputs: |
Reid Spencer | c0af3f0 | 2004-09-13 01:27:53 +0000 | [diff] [blame] | 115 | /// Message - The message to print to standard error. |
| 116 | /// |
Reid Spencer | 708585a | 2007-02-09 03:08:06 +0000 | [diff] [blame] | 117 | static void PrintAndExit(const std::string &Message, int errcode = 1) { |
Bill Wendling | e815619 | 2006-12-07 01:30:32 +0000 | [diff] [blame] | 118 | cerr << progname << ": " << Message << "\n"; |
Reid Spencer | 708585a | 2007-02-09 03:08:06 +0000 | [diff] [blame] | 119 | llvm_shutdown(); |
| 120 | exit(errcode); |
Reid Spencer | c0af3f0 | 2004-09-13 01:27:53 +0000 | [diff] [blame] | 121 | } |
| 122 | |
Reid Spencer | 3b72639 | 2007-04-29 23:59:47 +0000 | [diff] [blame^] | 123 | static void PrintCommand(const std::vector<const char*> &args) { |
| 124 | std::vector<const char*>::const_iterator I = args.begin(), E = args.end(); |
| 125 | for (; I != E; ++I) |
| 126 | if (*I) |
| 127 | cout << "'" << *I << "'" << " "; |
| 128 | cout << "\n" << std::flush; |
| 129 | } |
| 130 | |
Reid Spencer | 445564a | 2004-11-20 20:02:56 +0000 | [diff] [blame] | 131 | /// CopyEnv - This function takes an array of environment variables and makes a |
| 132 | /// copy of it. This copy can then be manipulated any way the caller likes |
| 133 | /// without affecting the process's real environment. |
| 134 | /// |
| 135 | /// Inputs: |
| 136 | /// envp - An array of C strings containing an environment. |
| 137 | /// |
| 138 | /// Return value: |
| 139 | /// NULL - An error occurred. |
| 140 | /// |
| 141 | /// Otherwise, a pointer to a new array of C strings is returned. Every string |
| 142 | /// in the array is a duplicate of the one in the original array (i.e. we do |
| 143 | /// not copy the char *'s from one array to another). |
| 144 | /// |
| 145 | static char ** CopyEnv(char ** const envp) { |
| 146 | // Count the number of entries in the old list; |
| 147 | unsigned entries; // The number of entries in the old environment list |
| 148 | for (entries = 0; envp[entries] != NULL; entries++) |
| 149 | /*empty*/; |
| 150 | |
| 151 | // Add one more entry for the NULL pointer that ends the list. |
| 152 | ++entries; |
| 153 | |
| 154 | // If there are no entries at all, just return NULL. |
| 155 | if (entries == 0) |
| 156 | return NULL; |
| 157 | |
| 158 | // Allocate a new environment list. |
| 159 | char **newenv = new char* [entries]; |
| 160 | if ((newenv = new char* [entries]) == NULL) |
| 161 | return NULL; |
| 162 | |
| 163 | // Make a copy of the list. Don't forget the NULL that ends the list. |
| 164 | entries = 0; |
| 165 | while (envp[entries] != NULL) { |
| 166 | newenv[entries] = new char[strlen (envp[entries]) + 1]; |
| 167 | strcpy (newenv[entries], envp[entries]); |
| 168 | ++entries; |
| 169 | } |
| 170 | newenv[entries] = NULL; |
| 171 | |
| 172 | return newenv; |
| 173 | } |
| 174 | |
| 175 | |
| 176 | /// RemoveEnv - Remove the specified environment variable from the environment |
| 177 | /// array. |
| 178 | /// |
| 179 | /// Inputs: |
| 180 | /// name - The name of the variable to remove. It cannot be NULL. |
| 181 | /// envp - The array of environment variables. It cannot be NULL. |
| 182 | /// |
| 183 | /// Notes: |
| 184 | /// This is mainly done because functions to remove items from the environment |
| 185 | /// are not available across all platforms. In particular, Solaris does not |
| 186 | /// seem to have an unsetenv() function or a setenv() function (or they are |
| 187 | /// undocumented if they do exist). |
| 188 | /// |
| 189 | static void RemoveEnv(const char * name, char ** const envp) { |
| 190 | for (unsigned index=0; envp[index] != NULL; index++) { |
| 191 | // Find the first equals sign in the array and make it an EOS character. |
| 192 | char *p = strchr (envp[index], '='); |
| 193 | if (p == NULL) |
| 194 | continue; |
| 195 | else |
| 196 | *p = '\0'; |
| 197 | |
| 198 | // Compare the two strings. If they are equal, zap this string. |
| 199 | // Otherwise, restore it. |
| 200 | if (!strcmp(name, envp[index])) |
| 201 | *envp[index] = '\0'; |
| 202 | else |
| 203 | *p = '='; |
| 204 | } |
| 205 | |
| 206 | return; |
| 207 | } |
| 208 | |
| 209 | /// GenerateBytecode - generates a bytecode file from the module provided |
| 210 | void GenerateBytecode(Module* M, const std::string& FileName) { |
| 211 | |
Reid Spencer | 3b72639 | 2007-04-29 23:59:47 +0000 | [diff] [blame^] | 212 | if (Verbose) |
| 213 | cout << "Generating Bytecode To " << FileName << '\n'; |
| 214 | |
Reid Spencer | 445564a | 2004-11-20 20:02:56 +0000 | [diff] [blame] | 215 | // Create the output file. |
Jeff Cohen | 5fb6ed4 | 2005-01-22 17:36:17 +0000 | [diff] [blame] | 216 | std::ios::openmode io_mode = std::ios::out | std::ios::trunc | |
| 217 | std::ios::binary; |
| 218 | std::ofstream Out(FileName.c_str(), io_mode); |
Reid Spencer | 708585a | 2007-02-09 03:08:06 +0000 | [diff] [blame] | 219 | if (!Out.good()) |
| 220 | PrintAndExit("error opening '" + FileName + "' for writing!"); |
Reid Spencer | 445564a | 2004-11-20 20:02:56 +0000 | [diff] [blame] | 221 | |
| 222 | // Ensure that the bytecode file gets removed from the disk if we get a |
| 223 | // terminating signal. |
| 224 | sys::RemoveFileOnSignal(sys::Path(FileName)); |
| 225 | |
| 226 | // Write it out |
Bill Wendling | e815619 | 2006-12-07 01:30:32 +0000 | [diff] [blame] | 227 | OStream L(Out); |
Bill Wendling | 68fe61d | 2006-11-29 00:19:40 +0000 | [diff] [blame] | 228 | WriteBytecodeToFile(M, L, !DisableCompression); |
Reid Spencer | 445564a | 2004-11-20 20:02:56 +0000 | [diff] [blame] | 229 | |
| 230 | // Close the bytecode file. |
| 231 | Out.close(); |
| 232 | } |
| 233 | |
| 234 | /// GenerateAssembly - generates a native assembly language source file from the |
| 235 | /// specified bytecode file. |
| 236 | /// |
| 237 | /// Inputs: |
Devang Patel | e2f8ad8 | 2006-06-27 18:07:29 +0000 | [diff] [blame] | 238 | /// InputFilename - The name of the input bytecode file. |
Reid Spencer | 445564a | 2004-11-20 20:02:56 +0000 | [diff] [blame] | 239 | /// OutputFilename - The name of the file to generate. |
| 240 | /// llc - The pathname to use for LLC. |
| 241 | /// envp - The environment to use when running LLC. |
| 242 | /// |
| 243 | /// Return non-zero value on error. |
| 244 | /// |
| 245 | static int GenerateAssembly(const std::string &OutputFilename, |
| 246 | const std::string &InputFilename, |
Reid Spencer | 8ea5ecb | 2006-08-21 06:04:45 +0000 | [diff] [blame] | 247 | const sys::Path &llc, |
| 248 | std::string &ErrMsg ) { |
Reid Spencer | 445564a | 2004-11-20 20:02:56 +0000 | [diff] [blame] | 249 | // Run LLC to convert the bytecode file into assembly code. |
Reid Spencer | f6358c7 | 2004-12-19 18:00:56 +0000 | [diff] [blame] | 250 | std::vector<const char*> args; |
Chris Lattner | bf9add4 | 2005-04-10 20:59:38 +0000 | [diff] [blame] | 251 | args.push_back(llc.c_str()); |
| 252 | args.push_back("-f"); |
| 253 | args.push_back("-o"); |
| 254 | args.push_back(OutputFilename.c_str()); |
| 255 | args.push_back(InputFilename.c_str()); |
Chris Lattner | 7456e3c | 2005-02-13 23:10:45 +0000 | [diff] [blame] | 256 | args.push_back(0); |
Reid Spencer | 445564a | 2004-11-20 20:02:56 +0000 | [diff] [blame] | 257 | |
Reid Spencer | 3b72639 | 2007-04-29 23:59:47 +0000 | [diff] [blame^] | 258 | if (Verbose) { |
| 259 | cout << "Generating Assembly With: \n"; |
| 260 | PrintCommand(args); |
| 261 | } |
| 262 | |
Anton Korobeynikov | 9ba8a76 | 2007-02-16 19:11:07 +0000 | [diff] [blame] | 263 | return sys::Program::ExecuteAndWait(llc, &args[0], 0, 0, 0, 0, &ErrMsg); |
Reid Spencer | 445564a | 2004-11-20 20:02:56 +0000 | [diff] [blame] | 264 | } |
| 265 | |
Devang Patel | e2f8ad8 | 2006-06-27 18:07:29 +0000 | [diff] [blame] | 266 | /// GenerateCFile - generates a C source file from the specified bytecode file. |
Reid Spencer | 445564a | 2004-11-20 20:02:56 +0000 | [diff] [blame] | 267 | static int GenerateCFile(const std::string &OutputFile, |
| 268 | const std::string &InputFile, |
Reid Spencer | 8ea5ecb | 2006-08-21 06:04:45 +0000 | [diff] [blame] | 269 | const sys::Path &llc, |
| 270 | std::string& ErrMsg) { |
Reid Spencer | 445564a | 2004-11-20 20:02:56 +0000 | [diff] [blame] | 271 | // Run LLC to convert the bytecode file into C. |
Reid Spencer | f6358c7 | 2004-12-19 18:00:56 +0000 | [diff] [blame] | 272 | std::vector<const char*> args; |
Chris Lattner | bf9add4 | 2005-04-10 20:59:38 +0000 | [diff] [blame] | 273 | args.push_back(llc.c_str()); |
| 274 | args.push_back("-march=c"); |
| 275 | args.push_back("-f"); |
| 276 | args.push_back("-o"); |
| 277 | args.push_back(OutputFile.c_str()); |
| 278 | args.push_back(InputFile.c_str()); |
Chris Lattner | 7456e3c | 2005-02-13 23:10:45 +0000 | [diff] [blame] | 279 | args.push_back(0); |
Reid Spencer | 3b72639 | 2007-04-29 23:59:47 +0000 | [diff] [blame^] | 280 | |
| 281 | if (Verbose) { |
| 282 | cout << "Generating C Source With: \n"; |
| 283 | PrintCommand(args); |
| 284 | } |
| 285 | |
Anton Korobeynikov | 9ba8a76 | 2007-02-16 19:11:07 +0000 | [diff] [blame] | 286 | return sys::Program::ExecuteAndWait(llc, &args[0], 0, 0, 0, 0, &ErrMsg); |
Reid Spencer | 445564a | 2004-11-20 20:02:56 +0000 | [diff] [blame] | 287 | } |
| 288 | |
Devang Patel | e2f8ad8 | 2006-06-27 18:07:29 +0000 | [diff] [blame] | 289 | /// GenerateNative - generates a native object file from the |
| 290 | /// specified bytecode file. |
Reid Spencer | 445564a | 2004-11-20 20:02:56 +0000 | [diff] [blame] | 291 | /// |
| 292 | /// Inputs: |
Reid Spencer | c82a5da | 2007-04-04 06:34:22 +0000 | [diff] [blame] | 293 | /// InputFilename - The name of the input bytecode file. |
| 294 | /// OutputFilename - The name of the file to generate. |
| 295 | /// NativeLinkItems - The native libraries, files, code with which to link |
| 296 | /// LibPaths - The list of directories in which to find libraries. |
| 297 | /// gcc - The pathname to use for GGC. |
| 298 | /// envp - A copy of the process's current environment. |
Reid Spencer | 445564a | 2004-11-20 20:02:56 +0000 | [diff] [blame] | 299 | /// |
| 300 | /// Outputs: |
| 301 | /// None. |
| 302 | /// |
| 303 | /// Returns non-zero value on error. |
| 304 | /// |
| 305 | static int GenerateNative(const std::string &OutputFilename, |
| 306 | const std::string &InputFilename, |
Reid Spencer | 4952143 | 2006-11-11 11:54:25 +0000 | [diff] [blame] | 307 | const Linker::ItemList &LinkItems, |
Reid Spencer | 8ea5ecb | 2006-08-21 06:04:45 +0000 | [diff] [blame] | 308 | const sys::Path &gcc, char ** const envp, |
| 309 | std::string& ErrMsg) { |
Reid Spencer | 445564a | 2004-11-20 20:02:56 +0000 | [diff] [blame] | 310 | // Remove these environment variables from the environment of the |
| 311 | // programs that we will execute. It appears that GCC sets these |
| 312 | // environment variables so that the programs it uses can configure |
| 313 | // themselves identically. |
| 314 | // |
| 315 | // However, when we invoke GCC below, we want it to use its normal |
| 316 | // configuration. Hence, we must sanitize its environment. |
| 317 | char ** clean_env = CopyEnv(envp); |
| 318 | if (clean_env == NULL) |
| 319 | return 1; |
| 320 | RemoveEnv("LIBRARY_PATH", clean_env); |
| 321 | RemoveEnv("COLLECT_GCC_OPTIONS", clean_env); |
| 322 | RemoveEnv("GCC_EXEC_PREFIX", clean_env); |
| 323 | RemoveEnv("COMPILER_PATH", clean_env); |
| 324 | RemoveEnv("COLLECT_GCC", clean_env); |
| 325 | |
Reid Spencer | 445564a | 2004-11-20 20:02:56 +0000 | [diff] [blame] | 326 | |
| 327 | // Run GCC to assemble and link the program into native code. |
| 328 | // |
| 329 | // Note: |
| 330 | // We can't just assemble and link the file with the system assembler |
| 331 | // and linker because we don't know where to put the _start symbol. |
| 332 | // GCC mysteriously knows how to do it. |
Reid Spencer | f6358c7 | 2004-12-19 18:00:56 +0000 | [diff] [blame] | 333 | std::vector<const char*> args; |
Chris Lattner | bf9add4 | 2005-04-10 20:59:38 +0000 | [diff] [blame] | 334 | args.push_back(gcc.c_str()); |
Reid Spencer | 6da1e0d | 2004-12-14 04:20:08 +0000 | [diff] [blame] | 335 | args.push_back("-fno-strict-aliasing"); |
| 336 | args.push_back("-O3"); |
| 337 | args.push_back("-o"); |
Reid Spencer | f6358c7 | 2004-12-19 18:00:56 +0000 | [diff] [blame] | 338 | args.push_back(OutputFilename.c_str()); |
| 339 | args.push_back(InputFilename.c_str()); |
Reid Spencer | 445564a | 2004-11-20 20:02:56 +0000 | [diff] [blame] | 340 | |
Reid Spencer | af303d5 | 2006-06-07 23:03:13 +0000 | [diff] [blame] | 341 | // Add in the library paths |
| 342 | for (unsigned index = 0; index < LibPaths.size(); index++) { |
| 343 | args.push_back("-L"); |
| 344 | args.push_back(LibPaths[index].c_str()); |
| 345 | } |
| 346 | |
| 347 | // Add the requested options |
| 348 | for (unsigned index = 0; index < XLinker.size(); index++) { |
| 349 | args.push_back(XLinker[index].c_str()); |
| 350 | args.push_back(Libraries[index].c_str()); |
| 351 | } |
| 352 | |
Reid Spencer | 445564a | 2004-11-20 20:02:56 +0000 | [diff] [blame] | 353 | // Add in the libraries to link. |
Reid Spencer | 4952143 | 2006-11-11 11:54:25 +0000 | [diff] [blame] | 354 | for (unsigned index = 0; index < LinkItems.size(); index++) |
| 355 | if (LinkItems[index].first != "crtend") { |
| 356 | if (LinkItems[index].second) { |
Reid Spencer | 2803b4c | 2006-11-11 19:59:25 +0000 | [diff] [blame] | 357 | std::string lib_name = "-l" + LinkItems[index].first; |
| 358 | args.push_back(lib_name.c_str()); |
Reid Spencer | 4952143 | 2006-11-11 11:54:25 +0000 | [diff] [blame] | 359 | } else |
Reid Spencer | 2803b4c | 2006-11-11 19:59:25 +0000 | [diff] [blame] | 360 | args.push_back(LinkItems[index].first.c_str()); |
Reid Spencer | f6358c7 | 2004-12-19 18:00:56 +0000 | [diff] [blame] | 361 | } |
Reid Spencer | af303d5 | 2006-06-07 23:03:13 +0000 | [diff] [blame] | 362 | |
Chris Lattner | 7456e3c | 2005-02-13 23:10:45 +0000 | [diff] [blame] | 363 | args.push_back(0); |
Reid Spencer | 445564a | 2004-11-20 20:02:56 +0000 | [diff] [blame] | 364 | |
Reid Spencer | 3b72639 | 2007-04-29 23:59:47 +0000 | [diff] [blame^] | 365 | if (Verbose) { |
| 366 | cout << "Generating Native Executable With:\n"; |
| 367 | PrintCommand(args); |
| 368 | } |
| 369 | |
Reid Spencer | 445564a | 2004-11-20 20:02:56 +0000 | [diff] [blame] | 370 | // Run the compiler to assembly and link together the program. |
Reid Spencer | 8ea5ecb | 2006-08-21 06:04:45 +0000 | [diff] [blame] | 371 | int R = sys::Program::ExecuteAndWait( |
Anton Korobeynikov | 9ba8a76 | 2007-02-16 19:11:07 +0000 | [diff] [blame] | 372 | gcc, &args[0], (const char**)clean_env, 0, 0, 0, &ErrMsg); |
Chris Lattner | 2f86362 | 2006-05-14 18:38:13 +0000 | [diff] [blame] | 373 | delete [] clean_env; |
| 374 | return R; |
Reid Spencer | 445564a | 2004-11-20 20:02:56 +0000 | [diff] [blame] | 375 | } |
| 376 | |
Reid Spencer | c0af3f0 | 2004-09-13 01:27:53 +0000 | [diff] [blame] | 377 | /// EmitShellScript - Output the wrapper file that invokes the JIT on the LLVM |
| 378 | /// bytecode file for the program. |
| 379 | static void EmitShellScript(char **argv) { |
Reid Spencer | 3b72639 | 2007-04-29 23:59:47 +0000 | [diff] [blame^] | 380 | if (Verbose) |
| 381 | cout << "Emitting Shell Script\n"; |
Reid Spencer | c0af3f0 | 2004-09-13 01:27:53 +0000 | [diff] [blame] | 382 | #if defined(_WIN32) || defined(__CYGWIN__) |
| 383 | // Windows doesn't support #!/bin/sh style shell scripts in .exe files. To |
| 384 | // support windows systems, we copy the llvm-stub.exe executable from the |
| 385 | // build tree to the destination file. |
Anton Korobeynikov | 7d51544 | 2006-09-01 20:35:17 +0000 | [diff] [blame] | 386 | std::string ErrMsg; |
Reid Spencer | 8d8b41d | 2004-12-22 13:50:17 +0000 | [diff] [blame] | 387 | sys::Path llvmstub = FindExecutable("llvm-stub.exe", argv[0]); |
Reid Spencer | 708585a | 2007-02-09 03:08:06 +0000 | [diff] [blame] | 388 | if (llvmstub.isEmpty()) |
| 389 | PrintAndExit("Could not find llvm-stub.exe executable!"); |
Anton Korobeynikov | 7d51544 | 2006-09-01 20:35:17 +0000 | [diff] [blame] | 390 | |
Reid Spencer | 708585a | 2007-02-09 03:08:06 +0000 | [diff] [blame] | 391 | if (0 != sys::CopyFile(sys::Path(OutputFilename), llvmstub, &ErrMsg)) |
| 392 | PrintAndExit(ErrMsg); |
Anton Korobeynikov | 7d51544 | 2006-09-01 20:35:17 +0000 | [diff] [blame] | 393 | |
Reid Spencer | c0af3f0 | 2004-09-13 01:27:53 +0000 | [diff] [blame] | 394 | return; |
| 395 | #endif |
| 396 | |
| 397 | // Output the script to start the program... |
| 398 | std::ofstream Out2(OutputFilename.c_str()); |
| 399 | if (!Out2.good()) |
Reid Spencer | 708585a | 2007-02-09 03:08:06 +0000 | [diff] [blame] | 400 | PrintAndExit("error opening '" + OutputFilename + "' for writing!"); |
Reid Spencer | c0af3f0 | 2004-09-13 01:27:53 +0000 | [diff] [blame] | 401 | |
| 402 | Out2 << "#!/bin/sh\n"; |
| 403 | // Allow user to setenv LLVMINTERP if lli is not in their PATH. |
| 404 | Out2 << "lli=${LLVMINTERP-lli}\n"; |
| 405 | Out2 << "exec $lli \\\n"; |
| 406 | // gcc accepts -l<lib> and implicitly searches /lib and /usr/lib. |
| 407 | LibPaths.push_back("/lib"); |
| 408 | LibPaths.push_back("/usr/lib"); |
| 409 | LibPaths.push_back("/usr/X11R6/lib"); |
| 410 | // We don't need to link in libc! In fact, /usr/lib/libc.so may not be a |
| 411 | // shared object at all! See RH 8: plain text. |
Misha Brukman | 3da94ae | 2005-04-22 00:00:37 +0000 | [diff] [blame] | 412 | std::vector<std::string>::iterator libc = |
Reid Spencer | c0af3f0 | 2004-09-13 01:27:53 +0000 | [diff] [blame] | 413 | std::find(Libraries.begin(), Libraries.end(), "c"); |
| 414 | if (libc != Libraries.end()) Libraries.erase(libc); |
| 415 | // List all the shared object (native) libraries this executable will need |
| 416 | // on the command line, so that we don't have to do this manually! |
Misha Brukman | 3da94ae | 2005-04-22 00:00:37 +0000 | [diff] [blame] | 417 | for (std::vector<std::string>::iterator i = Libraries.begin(), |
Reid Spencer | c0af3f0 | 2004-09-13 01:27:53 +0000 | [diff] [blame] | 418 | e = Libraries.end(); i != e; ++i) { |
Reid Spencer | c406413 | 2004-12-13 03:01:14 +0000 | [diff] [blame] | 419 | sys::Path FullLibraryPath = sys::Path::FindLibrary(*i); |
| 420 | if (!FullLibraryPath.isEmpty() && FullLibraryPath.isDynamicLibrary()) |
| 421 | Out2 << " -load=" << FullLibraryPath.toString() << " \\\n"; |
Reid Spencer | c0af3f0 | 2004-09-13 01:27:53 +0000 | [diff] [blame] | 422 | } |
| 423 | Out2 << " $0.bc ${1+\"$@\"}\n"; |
| 424 | Out2.close(); |
| 425 | } |
| 426 | |
Reid Spencer | c406413 | 2004-12-13 03:01:14 +0000 | [diff] [blame] | 427 | // BuildLinkItems -- This function generates a LinkItemList for the LinkItems |
| 428 | // linker function by combining the Files and Libraries in the order they were |
| 429 | // declared on the command line. |
| 430 | static void BuildLinkItems( |
| 431 | Linker::ItemList& Items, |
| 432 | const cl::list<std::string>& Files, |
| 433 | const cl::list<std::string>& Libraries) { |
| 434 | |
Misha Brukman | 3da94ae | 2005-04-22 00:00:37 +0000 | [diff] [blame] | 435 | // Build the list of linkage items for LinkItems. |
Reid Spencer | c406413 | 2004-12-13 03:01:14 +0000 | [diff] [blame] | 436 | |
| 437 | cl::list<std::string>::const_iterator fileIt = Files.begin(); |
| 438 | cl::list<std::string>::const_iterator libIt = Libraries.begin(); |
| 439 | |
| 440 | int libPos = -1, filePos = -1; |
Reid Spencer | 05f7e79 | 2004-12-13 17:18:19 +0000 | [diff] [blame] | 441 | while ( libIt != Libraries.end() || fileIt != Files.end() ) { |
Reid Spencer | c406413 | 2004-12-13 03:01:14 +0000 | [diff] [blame] | 442 | if (libIt != Libraries.end()) |
| 443 | libPos = Libraries.getPosition(libIt - Libraries.begin()); |
| 444 | else |
| 445 | libPos = -1; |
| 446 | if (fileIt != Files.end()) |
| 447 | filePos = Files.getPosition(fileIt - Files.begin()); |
| 448 | else |
| 449 | filePos = -1; |
| 450 | |
| 451 | if (filePos != -1 && (libPos == -1 || filePos < libPos)) { |
| 452 | // Add a source file |
| 453 | Items.push_back(std::make_pair(*fileIt++, false)); |
| 454 | } else if (libPos != -1 && (filePos == -1 || libPos < filePos)) { |
| 455 | // Add a library |
| 456 | Items.push_back(std::make_pair(*libIt++, true)); |
Reid Spencer | c406413 | 2004-12-13 03:01:14 +0000 | [diff] [blame] | 457 | } |
| 458 | } |
| 459 | } |
| 460 | |
Reid Spencer | 445564a | 2004-11-20 20:02:56 +0000 | [diff] [blame] | 461 | // Rightly this should go in a header file but it just seems such a waste. |
| 462 | namespace llvm { |
| 463 | extern void Optimize(Module*); |
| 464 | } |
| 465 | |
Reid Spencer | c0af3f0 | 2004-09-13 01:27:53 +0000 | [diff] [blame] | 466 | int main(int argc, char **argv, char **envp) { |
Chris Lattner | c30598b | 2006-12-06 01:18:01 +0000 | [diff] [blame] | 467 | llvm_shutdown_obj X; // Call llvm_shutdown() on exit. |
Reid Spencer | 1ef8bda | 2004-12-30 05:36:08 +0000 | [diff] [blame] | 468 | try { |
| 469 | // Initial global variable above for convenience printing of program name. |
| 470 | progname = sys::Path(argv[0]).getBasename(); |
Misha Brukman | 3da94ae | 2005-04-22 00:00:37 +0000 | [diff] [blame] | 471 | |
Reid Spencer | 1ef8bda | 2004-12-30 05:36:08 +0000 | [diff] [blame] | 472 | // Parse the command line options |
| 473 | cl::ParseCommandLineOptions(argc, argv, " llvm linker\n"); |
| 474 | sys::PrintStackTraceOnErrorSignal(); |
Reid Spencer | c0af3f0 | 2004-09-13 01:27:53 +0000 | [diff] [blame] | 475 | |
Reid Spencer | 4952143 | 2006-11-11 11:54:25 +0000 | [diff] [blame] | 476 | // Construct a Linker (now that Verbose is set) |
| 477 | Linker TheLinker(progname, OutputFilename, Verbose); |
Reid Spencer | c82a5da | 2007-04-04 06:34:22 +0000 | [diff] [blame] | 478 | |
| 479 | // Keep track of the native link items (versus the bytecode items) |
| 480 | Linker::ItemList NativeLinkItems; |
Reid Spencer | 4952143 | 2006-11-11 11:54:25 +0000 | [diff] [blame] | 481 | |
| 482 | // Add library paths to the linker |
Reid Spencer | 78df5c3 | 2006-03-06 06:38:19 +0000 | [diff] [blame] | 483 | TheLinker.addPaths(LibPaths); |
| 484 | TheLinker.addSystemPaths(); |
| 485 | |
Reid Spencer | 1ef8bda | 2004-12-30 05:36:08 +0000 | [diff] [blame] | 486 | // Remove any consecutive duplicates of the same library... |
| 487 | Libraries.erase(std::unique(Libraries.begin(), Libraries.end()), |
| 488 | Libraries.end()); |
Reid Spencer | c0af3f0 | 2004-09-13 01:27:53 +0000 | [diff] [blame] | 489 | |
Reid Spencer | 1ef8bda | 2004-12-30 05:36:08 +0000 | [diff] [blame] | 490 | if (LinkAsLibrary) { |
| 491 | std::vector<sys::Path> Files; |
| 492 | for (unsigned i = 0; i < InputFilenames.size(); ++i ) |
| 493 | Files.push_back(sys::Path(InputFilenames[i])); |
| 494 | if (TheLinker.LinkInFiles(Files)) |
| 495 | return 1; // Error already printed |
Reid Spencer | 6b463b2 | 2004-12-08 05:17:40 +0000 | [diff] [blame] | 496 | |
Reid Spencer | 1ef8bda | 2004-12-30 05:36:08 +0000 | [diff] [blame] | 497 | // The libraries aren't linked in but are noted as "dependent" in the |
| 498 | // module. |
Misha Brukman | 3da94ae | 2005-04-22 00:00:37 +0000 | [diff] [blame] | 499 | for (cl::list<std::string>::const_iterator I = Libraries.begin(), |
Reid Spencer | 1ef8bda | 2004-12-30 05:36:08 +0000 | [diff] [blame] | 500 | E = Libraries.end(); I != E ; ++I) { |
| 501 | TheLinker.getModule()->addLibrary(*I); |
| 502 | } |
Reid Spencer | c0af3f0 | 2004-09-13 01:27:53 +0000 | [diff] [blame] | 503 | } else { |
Reid Spencer | 1ef8bda | 2004-12-30 05:36:08 +0000 | [diff] [blame] | 504 | // Build a list of the items from our command line |
| 505 | Linker::ItemList Items; |
| 506 | BuildLinkItems(Items, InputFilenames, Libraries); |
| 507 | |
| 508 | // Link all the items together |
Reid Spencer | c82a5da | 2007-04-04 06:34:22 +0000 | [diff] [blame] | 509 | if (TheLinker.LinkInItems(Items, NativeLinkItems) ) |
Reid Spencer | 708585a | 2007-02-09 03:08:06 +0000 | [diff] [blame] | 510 | return 1; // Error already printed |
Reid Spencer | c0af3f0 | 2004-09-13 01:27:53 +0000 | [diff] [blame] | 511 | } |
Reid Spencer | c0af3f0 | 2004-09-13 01:27:53 +0000 | [diff] [blame] | 512 | |
Reid Spencer | 1ef8bda | 2004-12-30 05:36:08 +0000 | [diff] [blame] | 513 | std::auto_ptr<Module> Composite(TheLinker.releaseModule()); |
| 514 | |
| 515 | // Optimize the module |
| 516 | Optimize(Composite.get()); |
| 517 | |
| 518 | // Generate the bytecode for the optimized module. |
| 519 | std::string RealBytecodeOutput = OutputFilename; |
| 520 | if (!LinkAsLibrary) RealBytecodeOutput += ".bc"; |
| 521 | GenerateBytecode(Composite.get(), RealBytecodeOutput); |
| 522 | |
| 523 | // If we are not linking a library, generate either a native executable |
| 524 | // or a JIT shell script, depending upon what the user wants. |
| 525 | if (!LinkAsLibrary) { |
Reid Spencer | 73a74be | 2005-12-21 05:03:23 +0000 | [diff] [blame] | 526 | // If the user wants to run a post-link optimization, run it now. |
| 527 | if (!PostLinkOpts.empty()) { |
| 528 | std::vector<std::string> opts = PostLinkOpts; |
| 529 | for (std::vector<std::string>::iterator I = opts.begin(), |
| 530 | E = opts.end(); I != E; ++I) { |
| 531 | sys::Path prog(*I); |
| 532 | if (!prog.canExecute()) { |
| 533 | prog = sys::Program::FindProgramByName(*I); |
| 534 | if (prog.isEmpty()) |
Reid Spencer | 708585a | 2007-02-09 03:08:06 +0000 | [diff] [blame] | 535 | PrintAndExit(std::string("Optimization program '") + *I + |
Reid Spencer | 73a74be | 2005-12-21 05:03:23 +0000 | [diff] [blame] | 536 | "' is not found or not executable."); |
| 537 | } |
| 538 | // Get the program arguments |
| 539 | sys::Path tmp_output("opt_result"); |
Reid Spencer | e5c9cb5 | 2006-08-23 00:39:35 +0000 | [diff] [blame] | 540 | std::string ErrMsg; |
Reid Spencer | 708585a | 2007-02-09 03:08:06 +0000 | [diff] [blame] | 541 | if (tmp_output.createTemporaryFileOnDisk(true, &ErrMsg)) |
| 542 | PrintAndExit(ErrMsg); |
| 543 | |
Reid Spencer | 73a74be | 2005-12-21 05:03:23 +0000 | [diff] [blame] | 544 | const char* args[4]; |
| 545 | args[0] = I->c_str(); |
| 546 | args[1] = RealBytecodeOutput.c_str(); |
| 547 | args[2] = tmp_output.c_str(); |
| 548 | args[3] = 0; |
Anton Korobeynikov | 9ba8a76 | 2007-02-16 19:11:07 +0000 | [diff] [blame] | 549 | if (0 == sys::Program::ExecuteAndWait(prog, args, 0,0,0,0, &ErrMsg)) { |
Reid Spencer | 73a74be | 2005-12-21 05:03:23 +0000 | [diff] [blame] | 550 | if (tmp_output.isBytecodeFile()) { |
| 551 | sys::Path target(RealBytecodeOutput); |
| 552 | target.eraseFromDisk(); |
Reid Spencer | 708585a | 2007-02-09 03:08:06 +0000 | [diff] [blame] | 553 | if (tmp_output.renamePathOnDisk(target, &ErrMsg)) |
| 554 | PrintAndExit(ErrMsg, 2); |
Reid Spencer | 73a74be | 2005-12-21 05:03:23 +0000 | [diff] [blame] | 555 | } else |
Reid Spencer | 708585a | 2007-02-09 03:08:06 +0000 | [diff] [blame] | 556 | PrintAndExit("Post-link optimization output is not bytecode"); |
Reid Spencer | 8ea5ecb | 2006-08-21 06:04:45 +0000 | [diff] [blame] | 557 | } else { |
Reid Spencer | 708585a | 2007-02-09 03:08:06 +0000 | [diff] [blame] | 558 | PrintAndExit(ErrMsg); |
Reid Spencer | 73a74be | 2005-12-21 05:03:23 +0000 | [diff] [blame] | 559 | } |
| 560 | } |
| 561 | } |
| 562 | |
Reid Spencer | 1ef8bda | 2004-12-30 05:36:08 +0000 | [diff] [blame] | 563 | // If the user wants to generate a native executable, compile it from the |
| 564 | // bytecode file. |
| 565 | // |
| 566 | // Otherwise, create a script that will run the bytecode through the JIT. |
| 567 | if (Native) { |
| 568 | // Name of the Assembly Language output file |
| 569 | sys::Path AssemblyFile ( OutputFilename); |
| 570 | AssemblyFile.appendSuffix("s"); |
| 571 | |
| 572 | // Mark the output files for removal if we get an interrupt. |
| 573 | sys::RemoveFileOnSignal(AssemblyFile); |
| 574 | sys::RemoveFileOnSignal(sys::Path(OutputFilename)); |
| 575 | |
| 576 | // Determine the locations of the llc and gcc programs. |
| 577 | sys::Path llc = FindExecutable("llc", argv[0]); |
| 578 | if (llc.isEmpty()) |
Reid Spencer | 708585a | 2007-02-09 03:08:06 +0000 | [diff] [blame] | 579 | PrintAndExit("Failed to find llc"); |
Reid Spencer | 1ef8bda | 2004-12-30 05:36:08 +0000 | [diff] [blame] | 580 | |
| 581 | sys::Path gcc = FindExecutable("gcc", argv[0]); |
| 582 | if (gcc.isEmpty()) |
Reid Spencer | 708585a | 2007-02-09 03:08:06 +0000 | [diff] [blame] | 583 | PrintAndExit("Failed to find gcc"); |
Reid Spencer | 1ef8bda | 2004-12-30 05:36:08 +0000 | [diff] [blame] | 584 | |
| 585 | // Generate an assembly language file for the bytecode. |
Reid Spencer | 8ea5ecb | 2006-08-21 06:04:45 +0000 | [diff] [blame] | 586 | std::string ErrMsg; |
| 587 | if (0 != GenerateAssembly(AssemblyFile.toString(), RealBytecodeOutput, |
Reid Spencer | 708585a | 2007-02-09 03:08:06 +0000 | [diff] [blame] | 588 | llc, ErrMsg)) |
| 589 | PrintAndExit(ErrMsg); |
Reid Spencer | 8ea5ecb | 2006-08-21 06:04:45 +0000 | [diff] [blame] | 590 | |
Reid Spencer | 8ea5ecb | 2006-08-21 06:04:45 +0000 | [diff] [blame] | 591 | if (0 != GenerateNative(OutputFilename, AssemblyFile.toString(), |
Reid Spencer | c82a5da | 2007-04-04 06:34:22 +0000 | [diff] [blame] | 592 | NativeLinkItems, gcc, envp, ErrMsg)) |
Reid Spencer | 708585a | 2007-02-09 03:08:06 +0000 | [diff] [blame] | 593 | PrintAndExit(ErrMsg); |
Reid Spencer | 1ef8bda | 2004-12-30 05:36:08 +0000 | [diff] [blame] | 594 | |
| 595 | // Remove the assembly language file. |
Reid Spencer | a229c5c | 2005-07-08 03:08:58 +0000 | [diff] [blame] | 596 | AssemblyFile.eraseFromDisk(); |
Reid Spencer | 1ef8bda | 2004-12-30 05:36:08 +0000 | [diff] [blame] | 597 | } else if (NativeCBE) { |
| 598 | sys::Path CFile (OutputFilename); |
| 599 | CFile.appendSuffix("cbe.c"); |
| 600 | |
| 601 | // Mark the output files for removal if we get an interrupt. |
| 602 | sys::RemoveFileOnSignal(CFile); |
| 603 | sys::RemoveFileOnSignal(sys::Path(OutputFilename)); |
| 604 | |
| 605 | // Determine the locations of the llc and gcc programs. |
| 606 | sys::Path llc = FindExecutable("llc", argv[0]); |
| 607 | if (llc.isEmpty()) |
Reid Spencer | 708585a | 2007-02-09 03:08:06 +0000 | [diff] [blame] | 608 | PrintAndExit("Failed to find llc"); |
Reid Spencer | 1ef8bda | 2004-12-30 05:36:08 +0000 | [diff] [blame] | 609 | |
| 610 | sys::Path gcc = FindExecutable("gcc", argv[0]); |
| 611 | if (gcc.isEmpty()) |
Reid Spencer | 708585a | 2007-02-09 03:08:06 +0000 | [diff] [blame] | 612 | PrintAndExit("Failed to find gcc"); |
Reid Spencer | 1ef8bda | 2004-12-30 05:36:08 +0000 | [diff] [blame] | 613 | |
| 614 | // Generate an assembly language file for the bytecode. |
Reid Spencer | 8ea5ecb | 2006-08-21 06:04:45 +0000 | [diff] [blame] | 615 | std::string ErrMsg; |
| 616 | if (0 != GenerateCFile( |
Reid Spencer | 708585a | 2007-02-09 03:08:06 +0000 | [diff] [blame] | 617 | CFile.toString(), RealBytecodeOutput, llc, ErrMsg)) |
| 618 | PrintAndExit(ErrMsg); |
Reid Spencer | 8ea5ecb | 2006-08-21 06:04:45 +0000 | [diff] [blame] | 619 | |
Reid Spencer | c82a5da | 2007-04-04 06:34:22 +0000 | [diff] [blame] | 620 | if (0 != GenerateNative(OutputFilename, CFile.toString(), |
| 621 | NativeLinkItems, gcc, envp, ErrMsg)) |
Reid Spencer | 708585a | 2007-02-09 03:08:06 +0000 | [diff] [blame] | 622 | PrintAndExit(ErrMsg); |
Reid Spencer | 1ef8bda | 2004-12-30 05:36:08 +0000 | [diff] [blame] | 623 | |
| 624 | // Remove the assembly language file. |
Reid Spencer | a229c5c | 2005-07-08 03:08:58 +0000 | [diff] [blame] | 625 | CFile.eraseFromDisk(); |
Reid Spencer | 1ef8bda | 2004-12-30 05:36:08 +0000 | [diff] [blame] | 626 | |
| 627 | } else { |
| 628 | EmitShellScript(argv); |
| 629 | } |
Misha Brukman | 3da94ae | 2005-04-22 00:00:37 +0000 | [diff] [blame] | 630 | |
Reid Spencer | 1ef8bda | 2004-12-30 05:36:08 +0000 | [diff] [blame] | 631 | // Make the script executable... |
Reid Spencer | e1647f4 | 2006-08-22 23:27:23 +0000 | [diff] [blame] | 632 | std::string ErrMsg; |
Reid Spencer | 708585a | 2007-02-09 03:08:06 +0000 | [diff] [blame] | 633 | if (sys::Path(OutputFilename).makeExecutableOnDisk(&ErrMsg)) |
| 634 | PrintAndExit(ErrMsg); |
Reid Spencer | 1ef8bda | 2004-12-30 05:36:08 +0000 | [diff] [blame] | 635 | |
| 636 | // Make the bytecode file readable and directly executable in LLEE as well |
Reid Spencer | 708585a | 2007-02-09 03:08:06 +0000 | [diff] [blame] | 637 | if (sys::Path(RealBytecodeOutput).makeExecutableOnDisk(&ErrMsg)) |
| 638 | PrintAndExit(ErrMsg); |
Reid Spencer | 1ef8bda | 2004-12-30 05:36:08 +0000 | [diff] [blame] | 639 | |
Reid Spencer | 708585a | 2007-02-09 03:08:06 +0000 | [diff] [blame] | 640 | if (sys::Path(RealBytecodeOutput).makeReadableOnDisk(&ErrMsg)) |
| 641 | PrintAndExit(ErrMsg); |
| 642 | } |
Reid Spencer | 1ef8bda | 2004-12-30 05:36:08 +0000 | [diff] [blame] | 643 | } catch (const std::string& msg) { |
Reid Spencer | 708585a | 2007-02-09 03:08:06 +0000 | [diff] [blame] | 644 | PrintAndExit(msg,2); |
Reid Spencer | 1ef8bda | 2004-12-30 05:36:08 +0000 | [diff] [blame] | 645 | } catch (...) { |
Reid Spencer | 708585a | 2007-02-09 03:08:06 +0000 | [diff] [blame] | 646 | PrintAndExit("Unexpected unknown exception occurred.", 2); |
Reid Spencer | c0af3f0 | 2004-09-13 01:27:53 +0000 | [diff] [blame] | 647 | } |
Reid Spencer | 708585a | 2007-02-09 03:08:06 +0000 | [diff] [blame] | 648 | |
| 649 | // Graceful exit |
| 650 | return 0; |
Reid Spencer | c0af3f0 | 2004-09-13 01:27:53 +0000 | [diff] [blame] | 651 | } |