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