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