Chris Lattner | 7915a1e | 2003-10-14 21:34:11 +0000 | [diff] [blame] | 1 | //===-- ToolRunner.cpp ----------------------------------------------------===// |
John Criswell | b576c94 | 2003-10-20 19:43: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 | 7915a1e | 2003-10-14 21:34:11 +0000 | [diff] [blame] | 9 | // |
| 10 | // This file implements the interfaces described in the ToolRunner.h file. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
Chris Lattner | 0b1fe84 | 2003-10-19 02:27:40 +0000 | [diff] [blame] | 14 | #define DEBUG_TYPE "toolrunner" |
Misha Brukman | 6873450 | 2003-10-06 18:37:24 +0000 | [diff] [blame] | 15 | #include "llvm/Support/ToolRunner.h" |
Brian Gaeke | c8db76c | 2003-11-18 06:31:17 +0000 | [diff] [blame] | 16 | #include "Config/config.h" // for HAVE_LINK_R |
Misha Brukman | 9558c6a | 2003-09-29 22:39:25 +0000 | [diff] [blame] | 17 | #include "Support/Debug.h" |
| 18 | #include "Support/FileUtilities.h" |
Chris Lattner | 7915a1e | 2003-10-14 21:34:11 +0000 | [diff] [blame] | 19 | #include <iostream> |
| 20 | #include <fstream> |
Misha Brukman | 9558c6a | 2003-09-29 22:39:25 +0000 | [diff] [blame] | 21 | |
Brian Gaeke | d0fde30 | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 22 | namespace llvm { |
| 23 | |
Misha Brukman | 9558c6a | 2003-09-29 22:39:25 +0000 | [diff] [blame] | 24 | //===---------------------------------------------------------------------===// |
| 25 | // LLI Implementation of AbstractIntepreter interface |
| 26 | // |
| 27 | class LLI : public AbstractInterpreter { |
| 28 | std::string LLIPath; // The path to the LLI executable |
| 29 | public: |
| 30 | LLI(const std::string &Path) : LLIPath(Path) { } |
| 31 | |
| 32 | |
| 33 | virtual int ExecuteProgram(const std::string &Bytecode, |
Chris Lattner | 7915a1e | 2003-10-14 21:34:11 +0000 | [diff] [blame] | 34 | const std::vector<std::string> &Args, |
Misha Brukman | 9558c6a | 2003-09-29 22:39:25 +0000 | [diff] [blame] | 35 | const std::string &InputFile, |
| 36 | const std::string &OutputFile, |
Chris Lattner | eeed983 | 2003-10-14 21:52:52 +0000 | [diff] [blame] | 37 | const std::vector<std::string> &SharedLibs = |
| 38 | std::vector<std::string>()); |
Misha Brukman | 9558c6a | 2003-09-29 22:39:25 +0000 | [diff] [blame] | 39 | }; |
| 40 | |
| 41 | int LLI::ExecuteProgram(const std::string &Bytecode, |
Chris Lattner | 7915a1e | 2003-10-14 21:34:11 +0000 | [diff] [blame] | 42 | const std::vector<std::string> &Args, |
Misha Brukman | 9558c6a | 2003-09-29 22:39:25 +0000 | [diff] [blame] | 43 | const std::string &InputFile, |
| 44 | const std::string &OutputFile, |
Chris Lattner | eeed983 | 2003-10-14 21:52:52 +0000 | [diff] [blame] | 45 | const std::vector<std::string> &SharedLibs) { |
| 46 | if (!SharedLibs.empty()) { |
Misha Brukman | 9558c6a | 2003-09-29 22:39:25 +0000 | [diff] [blame] | 47 | std::cerr << "LLI currently does not support loading shared libraries.\n" |
| 48 | << "Exiting.\n"; |
| 49 | exit(1); |
| 50 | } |
| 51 | |
| 52 | std::vector<const char*> LLIArgs; |
| 53 | LLIArgs.push_back(LLIPath.c_str()); |
Misha Brukman | 9558c6a | 2003-09-29 22:39:25 +0000 | [diff] [blame] | 54 | LLIArgs.push_back("-quiet"); |
| 55 | LLIArgs.push_back("-force-interpreter=true"); |
| 56 | LLIArgs.push_back(Bytecode.c_str()); |
| 57 | // Add optional parameters to the running program from Argv |
| 58 | for (unsigned i=0, e = Args.size(); i != e; ++i) |
| 59 | LLIArgs.push_back(Args[i].c_str()); |
| 60 | LLIArgs.push_back(0); |
| 61 | |
| 62 | std::cout << "<lli>" << std::flush; |
Chris Lattner | 0b1fe84 | 2003-10-19 02:27:40 +0000 | [diff] [blame] | 63 | DEBUG(std::cerr << "\nAbout to run:\t"; |
Chris Lattner | 7b2ccff | 2003-10-19 02:14:58 +0000 | [diff] [blame] | 64 | for (unsigned i=0, e = LLIArgs.size()-1; i != e; ++i) |
Misha Brukman | 9558c6a | 2003-09-29 22:39:25 +0000 | [diff] [blame] | 65 | std::cerr << " " << LLIArgs[i]; |
| 66 | std::cerr << "\n"; |
| 67 | ); |
| 68 | return RunProgramWithTimeout(LLIPath, &LLIArgs[0], |
| 69 | InputFile, OutputFile, OutputFile); |
| 70 | } |
| 71 | |
| 72 | // LLI create method - Try to find the LLI executable |
Chris Lattner | 7915a1e | 2003-10-14 21:34:11 +0000 | [diff] [blame] | 73 | AbstractInterpreter *AbstractInterpreter::createLLI(const std::string &ProgPath, |
| 74 | std::string &Message) { |
| 75 | std::string LLIPath = FindExecutable("lli", ProgPath); |
Misha Brukman | 9558c6a | 2003-09-29 22:39:25 +0000 | [diff] [blame] | 76 | if (!LLIPath.empty()) { |
| 77 | Message = "Found lli: " + LLIPath + "\n"; |
| 78 | return new LLI(LLIPath); |
| 79 | } |
| 80 | |
| 81 | Message = "Cannot find `lli' in executable directory or PATH!\n"; |
| 82 | return 0; |
| 83 | } |
| 84 | |
| 85 | //===----------------------------------------------------------------------===// |
| 86 | // LLC Implementation of AbstractIntepreter interface |
| 87 | // |
Chris Lattner | 7915a1e | 2003-10-14 21:34:11 +0000 | [diff] [blame] | 88 | int LLC::OutputAsm(const std::string &Bytecode, std::string &OutputAsmFile) { |
Misha Brukman | 9558c6a | 2003-09-29 22:39:25 +0000 | [diff] [blame] | 89 | OutputAsmFile = getUniqueFilename(Bytecode+".llc.s"); |
| 90 | const char *LLCArgs[] = { |
| 91 | LLCPath.c_str(), |
| 92 | "-o", OutputAsmFile.c_str(), // Output to the Asm file |
| 93 | "-f", // Overwrite as necessary... |
| 94 | Bytecode.c_str(), // This is the input bytecode |
| 95 | 0 |
| 96 | }; |
| 97 | |
| 98 | std::cout << "<llc>" << std::flush; |
| 99 | if (RunProgramWithTimeout(LLCPath, LLCArgs, "/dev/null", "/dev/null", |
| 100 | "/dev/null")) { |
| 101 | // If LLC failed on the bytecode, print error... |
| 102 | std::cerr << "Error: `llc' failed!\n"; |
| 103 | removeFile(OutputAsmFile); |
| 104 | return 1; |
| 105 | } |
| 106 | |
| 107 | return 0; |
| 108 | } |
| 109 | |
| 110 | int LLC::ExecuteProgram(const std::string &Bytecode, |
Chris Lattner | 7915a1e | 2003-10-14 21:34:11 +0000 | [diff] [blame] | 111 | const std::vector<std::string> &Args, |
Misha Brukman | 9558c6a | 2003-09-29 22:39:25 +0000 | [diff] [blame] | 112 | const std::string &InputFile, |
| 113 | const std::string &OutputFile, |
Chris Lattner | eeed983 | 2003-10-14 21:52:52 +0000 | [diff] [blame] | 114 | const std::vector<std::string> &SharedLibs) { |
| 115 | |
Misha Brukman | 9558c6a | 2003-09-29 22:39:25 +0000 | [diff] [blame] | 116 | std::string OutputAsmFile; |
| 117 | if (OutputAsm(Bytecode, OutputAsmFile)) { |
| 118 | std::cerr << "Could not generate asm code with `llc', exiting.\n"; |
| 119 | exit(1); |
| 120 | } |
| 121 | |
| 122 | // Assuming LLC worked, compile the result with GCC and run it. |
Chris Lattner | 7915a1e | 2003-10-14 21:34:11 +0000 | [diff] [blame] | 123 | int Result = gcc->ExecuteProgram(OutputAsmFile, Args, GCC::AsmFile, |
Chris Lattner | eeed983 | 2003-10-14 21:52:52 +0000 | [diff] [blame] | 124 | InputFile, OutputFile, SharedLibs); |
Misha Brukman | 9558c6a | 2003-09-29 22:39:25 +0000 | [diff] [blame] | 125 | removeFile(OutputAsmFile); |
| 126 | return Result; |
| 127 | } |
| 128 | |
Chris Lattner | 7915a1e | 2003-10-14 21:34:11 +0000 | [diff] [blame] | 129 | /// createLLC - Try to find the LLC executable |
Misha Brukman | 9558c6a | 2003-09-29 22:39:25 +0000 | [diff] [blame] | 130 | /// |
Chris Lattner | 7915a1e | 2003-10-14 21:34:11 +0000 | [diff] [blame] | 131 | LLC *AbstractInterpreter::createLLC(const std::string &ProgramPath, |
| 132 | std::string &Message) { |
Misha Brukman | 9558c6a | 2003-09-29 22:39:25 +0000 | [diff] [blame] | 133 | std::string LLCPath = FindExecutable("llc", ProgramPath); |
| 134 | if (LLCPath.empty()) { |
| 135 | Message = "Cannot find `llc' in executable directory or PATH!\n"; |
| 136 | return 0; |
| 137 | } |
| 138 | |
| 139 | Message = "Found llc: " + LLCPath + "\n"; |
Chris Lattner | 7915a1e | 2003-10-14 21:34:11 +0000 | [diff] [blame] | 140 | GCC *gcc = GCC::create(ProgramPath, Message); |
Misha Brukman | 9558c6a | 2003-09-29 22:39:25 +0000 | [diff] [blame] | 141 | if (!gcc) { |
| 142 | std::cerr << Message << "\n"; |
| 143 | exit(1); |
| 144 | } |
| 145 | return new LLC(LLCPath, gcc); |
| 146 | } |
| 147 | |
| 148 | //===---------------------------------------------------------------------===// |
| 149 | // JIT Implementation of AbstractIntepreter interface |
| 150 | // |
| 151 | class JIT : public AbstractInterpreter { |
| 152 | std::string LLIPath; // The path to the LLI executable |
| 153 | public: |
| 154 | JIT(const std::string &Path) : LLIPath(Path) { } |
| 155 | |
| 156 | |
| 157 | virtual int ExecuteProgram(const std::string &Bytecode, |
Chris Lattner | 7915a1e | 2003-10-14 21:34:11 +0000 | [diff] [blame] | 158 | const std::vector<std::string> &Args, |
Misha Brukman | 9558c6a | 2003-09-29 22:39:25 +0000 | [diff] [blame] | 159 | const std::string &InputFile, |
| 160 | const std::string &OutputFile, |
Chris Lattner | eeed983 | 2003-10-14 21:52:52 +0000 | [diff] [blame] | 161 | const std::vector<std::string> &SharedLibs = |
| 162 | std::vector<std::string>()); |
Misha Brukman | 9558c6a | 2003-09-29 22:39:25 +0000 | [diff] [blame] | 163 | }; |
| 164 | |
| 165 | int JIT::ExecuteProgram(const std::string &Bytecode, |
Chris Lattner | 7915a1e | 2003-10-14 21:34:11 +0000 | [diff] [blame] | 166 | const std::vector<std::string> &Args, |
Misha Brukman | 9558c6a | 2003-09-29 22:39:25 +0000 | [diff] [blame] | 167 | const std::string &InputFile, |
| 168 | const std::string &OutputFile, |
Chris Lattner | eeed983 | 2003-10-14 21:52:52 +0000 | [diff] [blame] | 169 | const std::vector<std::string> &SharedLibs) { |
Misha Brukman | 9558c6a | 2003-09-29 22:39:25 +0000 | [diff] [blame] | 170 | // Construct a vector of parameters, incorporating those from the command-line |
| 171 | std::vector<const char*> JITArgs; |
| 172 | JITArgs.push_back(LLIPath.c_str()); |
| 173 | JITArgs.push_back("-quiet"); |
| 174 | JITArgs.push_back("-force-interpreter=false"); |
Chris Lattner | eeed983 | 2003-10-14 21:52:52 +0000 | [diff] [blame] | 175 | |
| 176 | for (unsigned i = 0, e = SharedLibs.size(); i != e; ++i) { |
Misha Brukman | 9558c6a | 2003-09-29 22:39:25 +0000 | [diff] [blame] | 177 | JITArgs.push_back("-load"); |
Chris Lattner | eeed983 | 2003-10-14 21:52:52 +0000 | [diff] [blame] | 178 | JITArgs.push_back(SharedLibs[i].c_str()); |
Misha Brukman | 9558c6a | 2003-09-29 22:39:25 +0000 | [diff] [blame] | 179 | } |
| 180 | JITArgs.push_back(Bytecode.c_str()); |
| 181 | // Add optional parameters to the running program from Argv |
| 182 | for (unsigned i=0, e = Args.size(); i != e; ++i) |
| 183 | JITArgs.push_back(Args[i].c_str()); |
| 184 | JITArgs.push_back(0); |
| 185 | |
| 186 | std::cout << "<jit>" << std::flush; |
Chris Lattner | 0b1fe84 | 2003-10-19 02:27:40 +0000 | [diff] [blame] | 187 | DEBUG(std::cerr << "\nAbout to run:\t"; |
Chris Lattner | 7b2ccff | 2003-10-19 02:14:58 +0000 | [diff] [blame] | 188 | for (unsigned i=0, e = JITArgs.size()-1; i != e; ++i) |
Misha Brukman | 9558c6a | 2003-09-29 22:39:25 +0000 | [diff] [blame] | 189 | std::cerr << " " << JITArgs[i]; |
| 190 | std::cerr << "\n"; |
| 191 | ); |
| 192 | DEBUG(std::cerr << "\nSending output to " << OutputFile << "\n"); |
| 193 | return RunProgramWithTimeout(LLIPath, &JITArgs[0], |
| 194 | InputFile, OutputFile, OutputFile); |
| 195 | } |
| 196 | |
Chris Lattner | 7915a1e | 2003-10-14 21:34:11 +0000 | [diff] [blame] | 197 | /// createJIT - Try to find the LLI executable |
Misha Brukman | 9558c6a | 2003-09-29 22:39:25 +0000 | [diff] [blame] | 198 | /// |
Chris Lattner | 7915a1e | 2003-10-14 21:34:11 +0000 | [diff] [blame] | 199 | AbstractInterpreter *AbstractInterpreter::createJIT(const std::string &ProgPath, |
| 200 | std::string &Message) { |
| 201 | std::string LLIPath = FindExecutable("lli", ProgPath); |
Misha Brukman | 9558c6a | 2003-09-29 22:39:25 +0000 | [diff] [blame] | 202 | if (!LLIPath.empty()) { |
| 203 | Message = "Found lli: " + LLIPath + "\n"; |
| 204 | return new JIT(LLIPath); |
| 205 | } |
| 206 | |
| 207 | Message = "Cannot find `lli' in executable directory or PATH!\n"; |
| 208 | return 0; |
| 209 | } |
| 210 | |
| 211 | int CBE::OutputC(const std::string &Bytecode, |
| 212 | std::string &OutputCFile) { |
| 213 | OutputCFile = getUniqueFilename(Bytecode+".cbe.c"); |
| 214 | const char *DisArgs[] = { |
| 215 | DISPath.c_str(), |
| 216 | "-o", OutputCFile.c_str(), // Output to the C file |
| 217 | "-c", // Output to C |
| 218 | "-f", // Overwrite as necessary... |
| 219 | Bytecode.c_str(), // This is the input bytecode |
| 220 | 0 |
| 221 | }; |
| 222 | |
| 223 | std::cout << "<cbe>" << std::flush; |
| 224 | if (RunProgramWithTimeout(DISPath, DisArgs, "/dev/null", "/dev/null", |
| 225 | "/dev/null")) { |
| 226 | // If dis failed on the bytecode, print error... |
| 227 | std::cerr << "Error: `llvm-dis -c' failed!\n"; |
| 228 | return 1; |
| 229 | } |
| 230 | |
| 231 | return 0; |
| 232 | } |
| 233 | |
| 234 | int CBE::ExecuteProgram(const std::string &Bytecode, |
Chris Lattner | 7915a1e | 2003-10-14 21:34:11 +0000 | [diff] [blame] | 235 | const std::vector<std::string> &Args, |
Misha Brukman | 9558c6a | 2003-09-29 22:39:25 +0000 | [diff] [blame] | 236 | const std::string &InputFile, |
| 237 | const std::string &OutputFile, |
Chris Lattner | eeed983 | 2003-10-14 21:52:52 +0000 | [diff] [blame] | 238 | const std::vector<std::string> &SharedLibs) { |
Misha Brukman | 9558c6a | 2003-09-29 22:39:25 +0000 | [diff] [blame] | 239 | std::string OutputCFile; |
| 240 | if (OutputC(Bytecode, OutputCFile)) { |
| 241 | std::cerr << "Could not generate C code with `llvm-dis', exiting.\n"; |
| 242 | exit(1); |
| 243 | } |
| 244 | |
Chris Lattner | 7915a1e | 2003-10-14 21:34:11 +0000 | [diff] [blame] | 245 | int Result = gcc->ExecuteProgram(OutputCFile, Args, GCC::CFile, |
Chris Lattner | eeed983 | 2003-10-14 21:52:52 +0000 | [diff] [blame] | 246 | InputFile, OutputFile, SharedLibs); |
Misha Brukman | 9558c6a | 2003-09-29 22:39:25 +0000 | [diff] [blame] | 247 | removeFile(OutputCFile); |
| 248 | |
| 249 | return Result; |
| 250 | } |
| 251 | |
Chris Lattner | 7915a1e | 2003-10-14 21:34:11 +0000 | [diff] [blame] | 252 | /// createCBE - Try to find the 'llvm-dis' executable |
Misha Brukman | 9558c6a | 2003-09-29 22:39:25 +0000 | [diff] [blame] | 253 | /// |
Chris Lattner | 7915a1e | 2003-10-14 21:34:11 +0000 | [diff] [blame] | 254 | CBE *AbstractInterpreter::createCBE(const std::string &ProgramPath, |
| 255 | std::string &Message) { |
Misha Brukman | 9558c6a | 2003-09-29 22:39:25 +0000 | [diff] [blame] | 256 | std::string DISPath = FindExecutable("llvm-dis", ProgramPath); |
| 257 | if (DISPath.empty()) { |
| 258 | Message = |
| 259 | "Cannot find `llvm-dis' in executable directory or PATH!\n"; |
| 260 | return 0; |
| 261 | } |
| 262 | |
| 263 | Message = "Found llvm-dis: " + DISPath + "\n"; |
Chris Lattner | 7915a1e | 2003-10-14 21:34:11 +0000 | [diff] [blame] | 264 | GCC *gcc = GCC::create(ProgramPath, Message); |
Misha Brukman | 9558c6a | 2003-09-29 22:39:25 +0000 | [diff] [blame] | 265 | if (!gcc) { |
| 266 | std::cerr << Message << "\n"; |
| 267 | exit(1); |
| 268 | } |
| 269 | return new CBE(DISPath, gcc); |
| 270 | } |
| 271 | |
| 272 | //===---------------------------------------------------------------------===// |
| 273 | // GCC abstraction |
| 274 | // |
Misha Brukman | 9558c6a | 2003-09-29 22:39:25 +0000 | [diff] [blame] | 275 | int GCC::ExecuteProgram(const std::string &ProgramFile, |
Chris Lattner | 7915a1e | 2003-10-14 21:34:11 +0000 | [diff] [blame] | 276 | const std::vector<std::string> &Args, |
Misha Brukman | 9558c6a | 2003-09-29 22:39:25 +0000 | [diff] [blame] | 277 | FileType fileType, |
| 278 | const std::string &InputFile, |
| 279 | const std::string &OutputFile, |
Chris Lattner | eeed983 | 2003-10-14 21:52:52 +0000 | [diff] [blame] | 280 | const std::vector<std::string> &SharedLibs) { |
Misha Brukman | 9558c6a | 2003-09-29 22:39:25 +0000 | [diff] [blame] | 281 | std::vector<const char*> GCCArgs; |
| 282 | |
| 283 | GCCArgs.push_back(GCCPath.c_str()); |
Chris Lattner | eeed983 | 2003-10-14 21:52:52 +0000 | [diff] [blame] | 284 | |
| 285 | // Specify the shared libraries to link in... |
| 286 | for (unsigned i = 0, e = SharedLibs.size(); i != e; ++i) |
| 287 | GCCArgs.push_back(SharedLibs[i].c_str()); |
| 288 | |
| 289 | // Specify -x explicitly in case the extension is wonky |
Misha Brukman | 9558c6a | 2003-09-29 22:39:25 +0000 | [diff] [blame] | 290 | GCCArgs.push_back("-x"); |
| 291 | if (fileType == CFile) { |
| 292 | GCCArgs.push_back("c"); |
| 293 | GCCArgs.push_back("-fno-strict-aliasing"); |
| 294 | } else { |
| 295 | GCCArgs.push_back("assembler"); |
| 296 | } |
| 297 | GCCArgs.push_back(ProgramFile.c_str()); // Specify the input filename... |
| 298 | GCCArgs.push_back("-o"); |
Chris Lattner | eeed983 | 2003-10-14 21:52:52 +0000 | [diff] [blame] | 299 | std::string OutputBinary = getUniqueFilename(ProgramFile+".gcc.exe"); |
Misha Brukman | 9558c6a | 2003-09-29 22:39:25 +0000 | [diff] [blame] | 300 | GCCArgs.push_back(OutputBinary.c_str()); // Output to the right file... |
| 301 | GCCArgs.push_back("-lm"); // Hard-code the math library... |
| 302 | GCCArgs.push_back("-O2"); // Optimize the program a bit... |
Brian Gaeke | c8db76c | 2003-11-18 06:31:17 +0000 | [diff] [blame] | 303 | #if defined (HAVE_LINK_R) |
Chris Lattner | 1f0f162 | 2003-10-18 21:54:47 +0000 | [diff] [blame] | 304 | GCCArgs.push_back("-Wl,-R."); // Search this dir for .so files |
Brian Gaeke | c8db76c | 2003-11-18 06:31:17 +0000 | [diff] [blame] | 305 | #endif |
Misha Brukman | 9558c6a | 2003-09-29 22:39:25 +0000 | [diff] [blame] | 306 | GCCArgs.push_back(0); // NULL terminator |
| 307 | |
| 308 | std::cout << "<gcc>" << std::flush; |
| 309 | if (RunProgramWithTimeout(GCCPath, &GCCArgs[0], "/dev/null", "/dev/null", |
| 310 | "/dev/null")) { |
| 311 | ProcessFailure(&GCCArgs[0]); |
| 312 | exit(1); |
| 313 | } |
| 314 | |
| 315 | std::vector<const char*> ProgramArgs; |
| 316 | ProgramArgs.push_back(OutputBinary.c_str()); |
| 317 | // Add optional parameters to the running program from Argv |
| 318 | for (unsigned i=0, e = Args.size(); i != e; ++i) |
| 319 | ProgramArgs.push_back(Args[i].c_str()); |
| 320 | ProgramArgs.push_back(0); // NULL terminator |
| 321 | |
| 322 | // Now that we have a binary, run it! |
| 323 | std::cout << "<program>" << std::flush; |
Chris Lattner | 0b1fe84 | 2003-10-19 02:27:40 +0000 | [diff] [blame] | 324 | DEBUG(std::cerr << "\nAbout to run:\t"; |
Chris Lattner | 7b2ccff | 2003-10-19 02:14:58 +0000 | [diff] [blame] | 325 | for (unsigned i=0, e = ProgramArgs.size()-1; i != e; ++i) |
Misha Brukman | 9558c6a | 2003-09-29 22:39:25 +0000 | [diff] [blame] | 326 | std::cerr << " " << ProgramArgs[i]; |
| 327 | std::cerr << "\n"; |
| 328 | ); |
| 329 | int ProgramResult = RunProgramWithTimeout(OutputBinary, &ProgramArgs[0], |
| 330 | InputFile, OutputFile, OutputFile); |
| 331 | removeFile(OutputBinary); |
| 332 | return ProgramResult; |
| 333 | } |
| 334 | |
Chris Lattner | 1798e4a | 2003-10-14 21:07:25 +0000 | [diff] [blame] | 335 | int GCC::MakeSharedObject(const std::string &InputFile, FileType fileType, |
Misha Brukman | 9558c6a | 2003-09-29 22:39:25 +0000 | [diff] [blame] | 336 | std::string &OutputFile) { |
| 337 | OutputFile = getUniqueFilename(InputFile+".so"); |
| 338 | // Compile the C/asm file into a shared object |
| 339 | const char* GCCArgs[] = { |
| 340 | GCCPath.c_str(), |
| 341 | "-x", (fileType == AsmFile) ? "assembler" : "c", |
| 342 | "-fno-strict-aliasing", |
| 343 | InputFile.c_str(), // Specify the input filename... |
| 344 | #if defined(sparc) || defined(__sparc__) || defined(__sparcv9) |
| 345 | "-G", // Compile a shared library, `-G' for Sparc |
| 346 | #else |
| 347 | "-shared", // `-shared' for Linux/X86, maybe others |
| 348 | #endif |
| 349 | "-o", OutputFile.c_str(), // Output to the right filename... |
| 350 | "-O2", // Optimize the program a bit... |
| 351 | 0 |
| 352 | }; |
| 353 | |
| 354 | std::cout << "<gcc>" << std::flush; |
Chris Lattner | 1798e4a | 2003-10-14 21:07:25 +0000 | [diff] [blame] | 355 | if (RunProgramWithTimeout(GCCPath, GCCArgs, "/dev/null", "/dev/null", |
| 356 | "/dev/null")) { |
Misha Brukman | 9558c6a | 2003-09-29 22:39:25 +0000 | [diff] [blame] | 357 | ProcessFailure(GCCArgs); |
Chris Lattner | 1798e4a | 2003-10-14 21:07:25 +0000 | [diff] [blame] | 358 | return 1; |
Misha Brukman | 9558c6a | 2003-09-29 22:39:25 +0000 | [diff] [blame] | 359 | } |
| 360 | return 0; |
| 361 | } |
| 362 | |
| 363 | void GCC::ProcessFailure(const char** GCCArgs) { |
| 364 | std::cerr << "\n*** Error: invocation of the C compiler failed!\n"; |
| 365 | for (const char **Arg = GCCArgs; *Arg; ++Arg) |
| 366 | std::cerr << " " << *Arg; |
| 367 | std::cerr << "\n"; |
| 368 | |
| 369 | // Rerun the compiler, capturing any error messages to print them. |
| 370 | std::string ErrorFilename = getUniqueFilename("gcc.errors"); |
| 371 | RunProgramWithTimeout(GCCPath, GCCArgs, "/dev/null", ErrorFilename.c_str(), |
| 372 | ErrorFilename.c_str()); |
| 373 | |
| 374 | // Print out the error messages generated by GCC if possible... |
| 375 | std::ifstream ErrorFile(ErrorFilename.c_str()); |
| 376 | if (ErrorFile) { |
| 377 | std::copy(std::istreambuf_iterator<char>(ErrorFile), |
| 378 | std::istreambuf_iterator<char>(), |
| 379 | std::ostreambuf_iterator<char>(std::cerr)); |
| 380 | ErrorFile.close(); |
| 381 | std::cerr << "\n"; |
| 382 | } |
| 383 | |
| 384 | removeFile(ErrorFilename); |
| 385 | } |
| 386 | |
Chris Lattner | 7915a1e | 2003-10-14 21:34:11 +0000 | [diff] [blame] | 387 | /// create - Try to find the `gcc' executable |
Misha Brukman | 9558c6a | 2003-09-29 22:39:25 +0000 | [diff] [blame] | 388 | /// |
Chris Lattner | 7915a1e | 2003-10-14 21:34:11 +0000 | [diff] [blame] | 389 | GCC *GCC::create(const std::string &ProgramPath, std::string &Message) { |
Misha Brukman | 9558c6a | 2003-09-29 22:39:25 +0000 | [diff] [blame] | 390 | std::string GCCPath = FindExecutable("gcc", ProgramPath); |
| 391 | if (GCCPath.empty()) { |
| 392 | Message = "Cannot find `gcc' in executable directory or PATH!\n"; |
| 393 | return 0; |
| 394 | } |
| 395 | |
| 396 | Message = "Found gcc: " + GCCPath + "\n"; |
| 397 | return new GCC(GCCPath); |
| 398 | } |
Brian Gaeke | d0fde30 | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 399 | |
| 400 | } // End llvm namespace |