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