blob: ad62474eac9e623e5837cdeeb298a7378d5bf9d2 [file] [log] [blame]
Chris Lattner7915a1e2003-10-14 21:34:11 +00001//===-- ToolRunner.cpp ----------------------------------------------------===//
John Criswellb576c942003-10-20 19:43:21 +00002//
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 Lattner7915a1e2003-10-14 21:34:11 +00009//
10// This file implements the interfaces described in the ToolRunner.h file.
11//
12//===----------------------------------------------------------------------===//
13
Chris Lattner0b1fe842003-10-19 02:27:40 +000014#define DEBUG_TYPE "toolrunner"
Misha Brukman68734502003-10-06 18:37:24 +000015#include "llvm/Support/ToolRunner.h"
Brian Gaekec8db76c2003-11-18 06:31:17 +000016#include "Config/config.h" // for HAVE_LINK_R
Misha Brukman9558c6a2003-09-29 22:39:25 +000017#include "Support/Debug.h"
18#include "Support/FileUtilities.h"
Chris Lattner7915a1e2003-10-14 21:34:11 +000019#include <fstream>
Chris Lattner89bf9ea2004-02-18 20:38:00 +000020#include <sstream>
Chris Lattner2cdd21c2003-12-14 21:35:53 +000021using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000022
Alkis Evlogimenos1d29a6d2004-02-19 07:39:26 +000023ToolExecutionError::~ToolExecutionError() throw() { }
24
Chris Lattner89bf9ea2004-02-18 20:38:00 +000025static void ProcessFailure(std::string ProgPath, const char** Args) {
26 std::ostringstream OS;
Chris Lattnera3de1172004-02-18 20:58:00 +000027 OS << "\nError running tool:\n ";
Chris Lattner89bf9ea2004-02-18 20:38:00 +000028 for (const char **Arg = Args; *Arg; ++Arg)
29 OS << " " << *Arg;
30 OS << "\n";
31
32 // Rerun the compiler, capturing any error messages to print them.
33 std::string ErrorFilename = getUniqueFilename("error_messages");
34 RunProgramWithTimeout(ProgPath, Args, "/dev/null", ErrorFilename.c_str(),
35 ErrorFilename.c_str());
36
37 // Print out the error messages generated by GCC if possible...
38 std::ifstream ErrorFile(ErrorFilename.c_str());
39 if (ErrorFile) {
40 std::copy(std::istreambuf_iterator<char>(ErrorFile),
41 std::istreambuf_iterator<char>(),
42 std::ostreambuf_iterator<char>(OS));
43 ErrorFile.close();
44 }
45
46 removeFile(ErrorFilename);
47 throw ToolExecutionError(OS.str());
48}
49
Misha Brukman9558c6a2003-09-29 22:39:25 +000050//===---------------------------------------------------------------------===//
51// LLI Implementation of AbstractIntepreter interface
52//
Chris Lattner2cdd21c2003-12-14 21:35:53 +000053namespace {
54 class LLI : public AbstractInterpreter {
55 std::string LLIPath; // The path to the LLI executable
Brian Gaeked11577b2004-05-04 21:09:01 +000056 std::vector<std::string> ToolArgs; // Args to pass to LLI
Chris Lattner2cdd21c2003-12-14 21:35:53 +000057 public:
Brian Gaeked11577b2004-05-04 21:09:01 +000058 LLI(const std::string &Path, const std::vector<std::string> *Args)
59 : LLIPath(Path) {
60 ToolArgs.clear ();
Brian Gaeke48b008d2004-05-04 22:02:41 +000061 if (Args) { ToolArgs = *Args; }
Brian Gaeked11577b2004-05-04 21:09:01 +000062 }
Chris Lattner2cdd21c2003-12-14 21:35:53 +000063
64 virtual int ExecuteProgram(const std::string &Bytecode,
65 const std::vector<std::string> &Args,
66 const std::string &InputFile,
67 const std::string &OutputFile,
68 const std::vector<std::string> &SharedLibs =
Chris Lattnereeed9832003-10-14 21:52:52 +000069 std::vector<std::string>());
Chris Lattner2cdd21c2003-12-14 21:35:53 +000070 };
71}
Misha Brukman9558c6a2003-09-29 22:39:25 +000072
73int LLI::ExecuteProgram(const std::string &Bytecode,
Chris Lattner7915a1e2003-10-14 21:34:11 +000074 const std::vector<std::string> &Args,
Misha Brukman9558c6a2003-09-29 22:39:25 +000075 const std::string &InputFile,
76 const std::string &OutputFile,
Chris Lattnereeed9832003-10-14 21:52:52 +000077 const std::vector<std::string> &SharedLibs) {
Chris Lattner8c56be52004-02-18 20:21:57 +000078 if (!SharedLibs.empty())
79 throw ToolExecutionError("LLI currently does not support "
80 "loading shared libraries.");
Misha Brukman9558c6a2003-09-29 22:39:25 +000081
82 std::vector<const char*> LLIArgs;
83 LLIArgs.push_back(LLIPath.c_str());
Misha Brukman9558c6a2003-09-29 22:39:25 +000084 LLIArgs.push_back("-force-interpreter=true");
Brian Gaeked11577b2004-05-04 21:09:01 +000085
86 // Add any extra LLI args.
87 for (unsigned i = 0, e = ToolArgs.size(); i != e; ++i)
88 LLIArgs.push_back(ToolArgs[i].c_str());
89
Misha Brukman9558c6a2003-09-29 22:39:25 +000090 LLIArgs.push_back(Bytecode.c_str());
91 // Add optional parameters to the running program from Argv
92 for (unsigned i=0, e = Args.size(); i != e; ++i)
93 LLIArgs.push_back(Args[i].c_str());
94 LLIArgs.push_back(0);
95
96 std::cout << "<lli>" << std::flush;
Chris Lattner0b1fe842003-10-19 02:27:40 +000097 DEBUG(std::cerr << "\nAbout to run:\t";
Chris Lattner7b2ccff2003-10-19 02:14:58 +000098 for (unsigned i=0, e = LLIArgs.size()-1; i != e; ++i)
Misha Brukman9558c6a2003-09-29 22:39:25 +000099 std::cerr << " " << LLIArgs[i];
100 std::cerr << "\n";
101 );
102 return RunProgramWithTimeout(LLIPath, &LLIArgs[0],
103 InputFile, OutputFile, OutputFile);
104}
105
106// LLI create method - Try to find the LLI executable
Chris Lattner7915a1e2003-10-14 21:34:11 +0000107AbstractInterpreter *AbstractInterpreter::createLLI(const std::string &ProgPath,
Brian Gaeked11577b2004-05-04 21:09:01 +0000108 std::string &Message,
109 const std::vector<std::string> *ToolArgs) {
Chris Lattner7915a1e2003-10-14 21:34:11 +0000110 std::string LLIPath = FindExecutable("lli", ProgPath);
Misha Brukman9558c6a2003-09-29 22:39:25 +0000111 if (!LLIPath.empty()) {
112 Message = "Found lli: " + LLIPath + "\n";
Brian Gaeked11577b2004-05-04 21:09:01 +0000113 return new LLI(LLIPath, ToolArgs);
Misha Brukman9558c6a2003-09-29 22:39:25 +0000114 }
115
116 Message = "Cannot find `lli' in executable directory or PATH!\n";
117 return 0;
118}
119
120//===----------------------------------------------------------------------===//
121// LLC Implementation of AbstractIntepreter interface
122//
Chris Lattner8c56be52004-02-18 20:21:57 +0000123void LLC::OutputAsm(const std::string &Bytecode, std::string &OutputAsmFile) {
Misha Brukman9558c6a2003-09-29 22:39:25 +0000124 OutputAsmFile = getUniqueFilename(Bytecode+".llc.s");
Brian Gaeked11577b2004-05-04 21:09:01 +0000125 std::vector<const char *> LLCArgs;
126 LLCArgs.push_back (LLCPath.c_str());
127
128 // Add any extra LLC args.
129 for (unsigned i = 0, e = ToolArgs.size(); i != e; ++i)
130 LLCArgs.push_back(ToolArgs[i].c_str());
131
132 LLCArgs.push_back ("-o");
133 LLCArgs.push_back (OutputAsmFile.c_str()); // Output to the Asm file
134 LLCArgs.push_back ("-f"); // Overwrite as necessary...
135 LLCArgs.push_back (Bytecode.c_str()); // This is the input bytecode
136 LLCArgs.push_back (0);
Misha Brukman9558c6a2003-09-29 22:39:25 +0000137
138 std::cout << "<llc>" << std::flush;
Brian Gaeked11577b2004-05-04 21:09:01 +0000139 DEBUG(std::cerr << "\nAbout to run:\t";
140 for (unsigned i=0, e = LLCArgs.size()-1; i != e; ++i)
141 std::cerr << " " << LLCArgs[i];
142 std::cerr << "\n";
143 );
144 if (RunProgramWithTimeout(LLCPath, &LLCArgs[0], "/dev/null", "/dev/null",
Chris Lattner8c56be52004-02-18 20:21:57 +0000145 "/dev/null"))
Brian Gaeked11577b2004-05-04 21:09:01 +0000146 ProcessFailure(LLCPath, &LLCArgs[0]);
Misha Brukman9558c6a2003-09-29 22:39:25 +0000147}
148
Chris Lattner9cbbee32004-02-18 23:24:41 +0000149void LLC::compileProgram(const std::string &Bytecode) {
150 std::string OutputAsmFile;
151 OutputAsm(Bytecode, OutputAsmFile);
152 removeFile(OutputAsmFile);
153}
154
Misha Brukman9558c6a2003-09-29 22:39:25 +0000155int LLC::ExecuteProgram(const std::string &Bytecode,
Chris Lattner7915a1e2003-10-14 21:34:11 +0000156 const std::vector<std::string> &Args,
Misha Brukman9558c6a2003-09-29 22:39:25 +0000157 const std::string &InputFile,
158 const std::string &OutputFile,
Chris Lattnereeed9832003-10-14 21:52:52 +0000159 const std::vector<std::string> &SharedLibs) {
160
Misha Brukman9558c6a2003-09-29 22:39:25 +0000161 std::string OutputAsmFile;
Chris Lattner8c56be52004-02-18 20:21:57 +0000162 OutputAsm(Bytecode, OutputAsmFile);
163 FileRemover OutFileRemover(OutputAsmFile);
Misha Brukman9558c6a2003-09-29 22:39:25 +0000164
165 // Assuming LLC worked, compile the result with GCC and run it.
Chris Lattner8c56be52004-02-18 20:21:57 +0000166 return gcc->ExecuteProgram(OutputAsmFile, Args, GCC::AsmFile,
167 InputFile, OutputFile, SharedLibs);
Misha Brukman9558c6a2003-09-29 22:39:25 +0000168}
169
Chris Lattner7915a1e2003-10-14 21:34:11 +0000170/// createLLC - Try to find the LLC executable
Misha Brukman9558c6a2003-09-29 22:39:25 +0000171///
Chris Lattner7915a1e2003-10-14 21:34:11 +0000172LLC *AbstractInterpreter::createLLC(const std::string &ProgramPath,
Brian Gaeked11577b2004-05-04 21:09:01 +0000173 std::string &Message,
174 const std::vector<std::string> *Args) {
Misha Brukman9558c6a2003-09-29 22:39:25 +0000175 std::string LLCPath = FindExecutable("llc", ProgramPath);
176 if (LLCPath.empty()) {
177 Message = "Cannot find `llc' in executable directory or PATH!\n";
178 return 0;
179 }
180
181 Message = "Found llc: " + LLCPath + "\n";
Chris Lattner7915a1e2003-10-14 21:34:11 +0000182 GCC *gcc = GCC::create(ProgramPath, Message);
Misha Brukman9558c6a2003-09-29 22:39:25 +0000183 if (!gcc) {
184 std::cerr << Message << "\n";
185 exit(1);
186 }
Brian Gaeked11577b2004-05-04 21:09:01 +0000187 return new LLC(LLCPath, gcc, Args);
Misha Brukman9558c6a2003-09-29 22:39:25 +0000188}
189
190//===---------------------------------------------------------------------===//
191// JIT Implementation of AbstractIntepreter interface
192//
Chris Lattner2cdd21c2003-12-14 21:35:53 +0000193namespace {
194 class JIT : public AbstractInterpreter {
195 std::string LLIPath; // The path to the LLI executable
Brian Gaeked11577b2004-05-04 21:09:01 +0000196 std::vector<std::string> ToolArgs; // Args to pass to LLI
Chris Lattner2cdd21c2003-12-14 21:35:53 +0000197 public:
Brian Gaeked11577b2004-05-04 21:09:01 +0000198 JIT(const std::string &Path, const std::vector<std::string> *Args)
199 : LLIPath(Path) {
200 ToolArgs.clear ();
Brian Gaeke48b008d2004-05-04 22:02:41 +0000201 if (Args) { ToolArgs = *Args; }
Brian Gaeked11577b2004-05-04 21:09:01 +0000202 }
Chris Lattner2cdd21c2003-12-14 21:35:53 +0000203
204 virtual int ExecuteProgram(const std::string &Bytecode,
205 const std::vector<std::string> &Args,
206 const std::string &InputFile,
207 const std::string &OutputFile,
208 const std::vector<std::string> &SharedLibs =
Chris Lattnereeed9832003-10-14 21:52:52 +0000209 std::vector<std::string>());
Chris Lattner2cdd21c2003-12-14 21:35:53 +0000210 };
211}
Misha Brukman9558c6a2003-09-29 22:39:25 +0000212
213int JIT::ExecuteProgram(const std::string &Bytecode,
Chris Lattner7915a1e2003-10-14 21:34:11 +0000214 const std::vector<std::string> &Args,
Misha Brukman9558c6a2003-09-29 22:39:25 +0000215 const std::string &InputFile,
216 const std::string &OutputFile,
Chris Lattnereeed9832003-10-14 21:52:52 +0000217 const std::vector<std::string> &SharedLibs) {
Misha Brukman9558c6a2003-09-29 22:39:25 +0000218 // Construct a vector of parameters, incorporating those from the command-line
219 std::vector<const char*> JITArgs;
220 JITArgs.push_back(LLIPath.c_str());
Misha Brukman9558c6a2003-09-29 22:39:25 +0000221 JITArgs.push_back("-force-interpreter=false");
Chris Lattnereeed9832003-10-14 21:52:52 +0000222
Brian Gaeked11577b2004-05-04 21:09:01 +0000223 // Add any extra LLI args.
224 for (unsigned i = 0, e = ToolArgs.size(); i != e; ++i)
225 JITArgs.push_back(ToolArgs[i].c_str());
226
Chris Lattnereeed9832003-10-14 21:52:52 +0000227 for (unsigned i = 0, e = SharedLibs.size(); i != e; ++i) {
Misha Brukman9558c6a2003-09-29 22:39:25 +0000228 JITArgs.push_back("-load");
Chris Lattnereeed9832003-10-14 21:52:52 +0000229 JITArgs.push_back(SharedLibs[i].c_str());
Misha Brukman9558c6a2003-09-29 22:39:25 +0000230 }
231 JITArgs.push_back(Bytecode.c_str());
232 // Add optional parameters to the running program from Argv
233 for (unsigned i=0, e = Args.size(); i != e; ++i)
234 JITArgs.push_back(Args[i].c_str());
235 JITArgs.push_back(0);
236
237 std::cout << "<jit>" << std::flush;
Chris Lattner0b1fe842003-10-19 02:27:40 +0000238 DEBUG(std::cerr << "\nAbout to run:\t";
Chris Lattner7b2ccff2003-10-19 02:14:58 +0000239 for (unsigned i=0, e = JITArgs.size()-1; i != e; ++i)
Misha Brukman9558c6a2003-09-29 22:39:25 +0000240 std::cerr << " " << JITArgs[i];
241 std::cerr << "\n";
242 );
243 DEBUG(std::cerr << "\nSending output to " << OutputFile << "\n");
244 return RunProgramWithTimeout(LLIPath, &JITArgs[0],
245 InputFile, OutputFile, OutputFile);
246}
247
Chris Lattner7915a1e2003-10-14 21:34:11 +0000248/// createJIT - Try to find the LLI executable
Misha Brukman9558c6a2003-09-29 22:39:25 +0000249///
Chris Lattner7915a1e2003-10-14 21:34:11 +0000250AbstractInterpreter *AbstractInterpreter::createJIT(const std::string &ProgPath,
Brian Gaeked11577b2004-05-04 21:09:01 +0000251 std::string &Message, const std::vector<std::string> *Args) {
Chris Lattner7915a1e2003-10-14 21:34:11 +0000252 std::string LLIPath = FindExecutable("lli", ProgPath);
Misha Brukman9558c6a2003-09-29 22:39:25 +0000253 if (!LLIPath.empty()) {
254 Message = "Found lli: " + LLIPath + "\n";
Brian Gaeked11577b2004-05-04 21:09:01 +0000255 return new JIT(LLIPath, Args);
Misha Brukman9558c6a2003-09-29 22:39:25 +0000256 }
257
258 Message = "Cannot find `lli' in executable directory or PATH!\n";
259 return 0;
260}
261
Chris Lattner8c56be52004-02-18 20:21:57 +0000262void CBE::OutputC(const std::string &Bytecode,
Misha Brukman9558c6a2003-09-29 22:39:25 +0000263 std::string &OutputCFile) {
264 OutputCFile = getUniqueFilename(Bytecode+".cbe.c");
Brian Gaeked11577b2004-05-04 21:09:01 +0000265 std::vector<const char *> LLCArgs;
266 LLCArgs.push_back (LLCPath.c_str());
267
268 // Add any extra LLC args.
269 for (unsigned i = 0, e = ToolArgs.size(); i != e; ++i)
270 LLCArgs.push_back(ToolArgs[i].c_str());
271
272 LLCArgs.push_back ("-o");
273 LLCArgs.push_back (OutputCFile.c_str()); // Output to the C file
274 LLCArgs.push_back ("-march=c"); // Output C language
275 LLCArgs.push_back ("-f"); // Overwrite as necessary...
276 LLCArgs.push_back (Bytecode.c_str()); // This is the input bytecode
277 LLCArgs.push_back (0);
Misha Brukman9558c6a2003-09-29 22:39:25 +0000278
279 std::cout << "<cbe>" << std::flush;
Brian Gaeked11577b2004-05-04 21:09:01 +0000280 DEBUG(std::cerr << "\nAbout to run:\t";
281 for (unsigned i=0, e = LLCArgs.size()-1; i != e; ++i)
282 std::cerr << " " << LLCArgs[i];
283 std::cerr << "\n";
284 );
285 if (RunProgramWithTimeout(LLCPath, &LLCArgs[0], "/dev/null", "/dev/null",
Chris Lattner8c56be52004-02-18 20:21:57 +0000286 "/dev/null"))
Brian Gaeked11577b2004-05-04 21:09:01 +0000287 ProcessFailure(LLCPath, &LLCArgs[0]);
Misha Brukman9558c6a2003-09-29 22:39:25 +0000288}
289
Chris Lattner9cbbee32004-02-18 23:24:41 +0000290void CBE::compileProgram(const std::string &Bytecode) {
291 std::string OutputCFile;
292 OutputC(Bytecode, OutputCFile);
293 removeFile(OutputCFile);
294}
295
Misha Brukman9558c6a2003-09-29 22:39:25 +0000296int CBE::ExecuteProgram(const std::string &Bytecode,
Chris Lattner7915a1e2003-10-14 21:34:11 +0000297 const std::vector<std::string> &Args,
Misha Brukman9558c6a2003-09-29 22:39:25 +0000298 const std::string &InputFile,
299 const std::string &OutputFile,
Chris Lattnereeed9832003-10-14 21:52:52 +0000300 const std::vector<std::string> &SharedLibs) {
Misha Brukman9558c6a2003-09-29 22:39:25 +0000301 std::string OutputCFile;
Chris Lattner8c56be52004-02-18 20:21:57 +0000302 OutputC(Bytecode, OutputCFile);
Misha Brukman9558c6a2003-09-29 22:39:25 +0000303
Chris Lattner8c56be52004-02-18 20:21:57 +0000304 FileRemover CFileRemove(OutputCFile);
Misha Brukman9558c6a2003-09-29 22:39:25 +0000305
Chris Lattner8c56be52004-02-18 20:21:57 +0000306 return gcc->ExecuteProgram(OutputCFile, Args, GCC::CFile,
307 InputFile, OutputFile, SharedLibs);
Misha Brukman9558c6a2003-09-29 22:39:25 +0000308}
309
Chris Lattner9915cd92004-02-17 06:40:06 +0000310/// createCBE - Try to find the 'llc' executable
Misha Brukman9558c6a2003-09-29 22:39:25 +0000311///
Chris Lattner7915a1e2003-10-14 21:34:11 +0000312CBE *AbstractInterpreter::createCBE(const std::string &ProgramPath,
Brian Gaeked11577b2004-05-04 21:09:01 +0000313 std::string &Message,
314 const std::vector<std::string> *Args) {
Chris Lattner9915cd92004-02-17 06:40:06 +0000315 std::string LLCPath = FindExecutable("llc", ProgramPath);
316 if (LLCPath.empty()) {
Misha Brukman9558c6a2003-09-29 22:39:25 +0000317 Message =
Chris Lattner9915cd92004-02-17 06:40:06 +0000318 "Cannot find `llc' in executable directory or PATH!\n";
Misha Brukman9558c6a2003-09-29 22:39:25 +0000319 return 0;
320 }
321
Chris Lattner9915cd92004-02-17 06:40:06 +0000322 Message = "Found llc: " + LLCPath + "\n";
Chris Lattner7915a1e2003-10-14 21:34:11 +0000323 GCC *gcc = GCC::create(ProgramPath, Message);
Misha Brukman9558c6a2003-09-29 22:39:25 +0000324 if (!gcc) {
325 std::cerr << Message << "\n";
326 exit(1);
327 }
Brian Gaeked11577b2004-05-04 21:09:01 +0000328 return new CBE(LLCPath, gcc, Args);
Misha Brukman9558c6a2003-09-29 22:39:25 +0000329}
330
331//===---------------------------------------------------------------------===//
332// GCC abstraction
333//
Misha Brukman9558c6a2003-09-29 22:39:25 +0000334int GCC::ExecuteProgram(const std::string &ProgramFile,
Chris Lattner7915a1e2003-10-14 21:34:11 +0000335 const std::vector<std::string> &Args,
Misha Brukman9558c6a2003-09-29 22:39:25 +0000336 FileType fileType,
337 const std::string &InputFile,
338 const std::string &OutputFile,
Chris Lattnereeed9832003-10-14 21:52:52 +0000339 const std::vector<std::string> &SharedLibs) {
Misha Brukman9558c6a2003-09-29 22:39:25 +0000340 std::vector<const char*> GCCArgs;
341
342 GCCArgs.push_back(GCCPath.c_str());
Chris Lattnereeed9832003-10-14 21:52:52 +0000343
344 // Specify the shared libraries to link in...
345 for (unsigned i = 0, e = SharedLibs.size(); i != e; ++i)
346 GCCArgs.push_back(SharedLibs[i].c_str());
347
348 // Specify -x explicitly in case the extension is wonky
Misha Brukman9558c6a2003-09-29 22:39:25 +0000349 GCCArgs.push_back("-x");
350 if (fileType == CFile) {
351 GCCArgs.push_back("c");
352 GCCArgs.push_back("-fno-strict-aliasing");
353 } else {
354 GCCArgs.push_back("assembler");
355 }
356 GCCArgs.push_back(ProgramFile.c_str()); // Specify the input filename...
357 GCCArgs.push_back("-o");
Chris Lattnereeed9832003-10-14 21:52:52 +0000358 std::string OutputBinary = getUniqueFilename(ProgramFile+".gcc.exe");
Misha Brukman9558c6a2003-09-29 22:39:25 +0000359 GCCArgs.push_back(OutputBinary.c_str()); // Output to the right file...
360 GCCArgs.push_back("-lm"); // Hard-code the math library...
361 GCCArgs.push_back("-O2"); // Optimize the program a bit...
Brian Gaekec8db76c2003-11-18 06:31:17 +0000362#if defined (HAVE_LINK_R)
Chris Lattner1f0f1622003-10-18 21:54:47 +0000363 GCCArgs.push_back("-Wl,-R."); // Search this dir for .so files
Brian Gaekec8db76c2003-11-18 06:31:17 +0000364#endif
Misha Brukman9558c6a2003-09-29 22:39:25 +0000365 GCCArgs.push_back(0); // NULL terminator
366
367 std::cout << "<gcc>" << std::flush;
368 if (RunProgramWithTimeout(GCCPath, &GCCArgs[0], "/dev/null", "/dev/null",
369 "/dev/null")) {
Chris Lattner89bf9ea2004-02-18 20:38:00 +0000370 ProcessFailure(GCCPath, &GCCArgs[0]);
Misha Brukman9558c6a2003-09-29 22:39:25 +0000371 exit(1);
372 }
373
374 std::vector<const char*> ProgramArgs;
375 ProgramArgs.push_back(OutputBinary.c_str());
376 // Add optional parameters to the running program from Argv
377 for (unsigned i=0, e = Args.size(); i != e; ++i)
378 ProgramArgs.push_back(Args[i].c_str());
379 ProgramArgs.push_back(0); // NULL terminator
380
381 // Now that we have a binary, run it!
382 std::cout << "<program>" << std::flush;
Chris Lattner0b1fe842003-10-19 02:27:40 +0000383 DEBUG(std::cerr << "\nAbout to run:\t";
Chris Lattner7b2ccff2003-10-19 02:14:58 +0000384 for (unsigned i=0, e = ProgramArgs.size()-1; i != e; ++i)
Misha Brukman9558c6a2003-09-29 22:39:25 +0000385 std::cerr << " " << ProgramArgs[i];
386 std::cerr << "\n";
387 );
Chris Lattner8c56be52004-02-18 20:21:57 +0000388
389 FileRemover OutputBinaryRemover(OutputBinary);
390 return RunProgramWithTimeout(OutputBinary, &ProgramArgs[0],
391 InputFile, OutputFile, OutputFile);
Misha Brukman9558c6a2003-09-29 22:39:25 +0000392}
393
Chris Lattner1798e4a2003-10-14 21:07:25 +0000394int GCC::MakeSharedObject(const std::string &InputFile, FileType fileType,
Misha Brukman9558c6a2003-09-29 22:39:25 +0000395 std::string &OutputFile) {
John Criswell7f7d16b2004-01-26 20:59:41 +0000396 OutputFile = getUniqueFilename(InputFile+SHLIBEXT);
Misha Brukman9558c6a2003-09-29 22:39:25 +0000397 // Compile the C/asm file into a shared object
398 const char* GCCArgs[] = {
399 GCCPath.c_str(),
400 "-x", (fileType == AsmFile) ? "assembler" : "c",
401 "-fno-strict-aliasing",
402 InputFile.c_str(), // Specify the input filename...
403#if defined(sparc) || defined(__sparc__) || defined(__sparcv9)
404 "-G", // Compile a shared library, `-G' for Sparc
Misha Brukmanb3998ec2004-07-16 19:45:45 +0000405#elif (defined(__POWERPC__) || defined(__ppc__)) && defined(__APPLE__)
406 "-dynamiclib", // `-dynamiclib' for MacOS X/PowerPC
407 "-fno-common", // allow global vars w/o initializers to live
Chris Lattnere312e152004-07-19 06:03:51 +0000408 "-undefined", // in data segment, rather than generating
Chris Lattner5fbf29c2004-07-19 06:00:17 +0000409 "dynamic_lookup", // blocks. dynamic_lookup requires that you set
410 // MACOSX_DEPLOYMENT_TARGET=10.3 in your env.
Misha Brukmanb3998ec2004-07-16 19:45:45 +0000411#else
Misha Brukman9558c6a2003-09-29 22:39:25 +0000412 "-shared", // `-shared' for Linux/X86, maybe others
413#endif
414 "-o", OutputFile.c_str(), // Output to the right filename...
415 "-O2", // Optimize the program a bit...
416 0
417 };
418
419 std::cout << "<gcc>" << std::flush;
Chris Lattner1798e4a2003-10-14 21:07:25 +0000420 if (RunProgramWithTimeout(GCCPath, GCCArgs, "/dev/null", "/dev/null",
421 "/dev/null")) {
Chris Lattner89bf9ea2004-02-18 20:38:00 +0000422 ProcessFailure(GCCPath, GCCArgs);
Chris Lattner1798e4a2003-10-14 21:07:25 +0000423 return 1;
Misha Brukman9558c6a2003-09-29 22:39:25 +0000424 }
425 return 0;
426}
427
Chris Lattner7915a1e2003-10-14 21:34:11 +0000428/// create - Try to find the `gcc' executable
Misha Brukman9558c6a2003-09-29 22:39:25 +0000429///
Chris Lattner7915a1e2003-10-14 21:34:11 +0000430GCC *GCC::create(const std::string &ProgramPath, std::string &Message) {
Misha Brukman9558c6a2003-09-29 22:39:25 +0000431 std::string GCCPath = FindExecutable("gcc", ProgramPath);
432 if (GCCPath.empty()) {
433 Message = "Cannot find `gcc' in executable directory or PATH!\n";
434 return 0;
435 }
436
437 Message = "Found gcc: " + GCCPath + "\n";
438 return new GCC(GCCPath);
439}