blob: fd9c2b303170e62fa3e1d340b5a377ae21be17c5 [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"
Reid Spencer551ccae2004-09-01 22:55:40 +000016#include "llvm/Config/config.h" // for HAVE_LINK_R
17#include "llvm/Support/Debug.h"
18#include "llvm/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.
Reid Spencercda985e2004-12-15 01:51:56 +000033 sys::Path ErrorFilename("error_messages");
34 ErrorFilename.makeUnique();
Chris Lattner89bf9ea2004-02-18 20:38:00 +000035 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
Reid Spencer9ac14182004-12-16 23:01:34 +000047 ErrorFilename.destroyFile();
Chris Lattner89bf9ea2004-02-18 20:38:00 +000048 throw ToolExecutionError(OS.str());
49}
50
Misha Brukman9558c6a2003-09-29 22:39:25 +000051//===---------------------------------------------------------------------===//
52// LLI Implementation of AbstractIntepreter interface
53//
Chris Lattner2cdd21c2003-12-14 21:35:53 +000054namespace {
55 class LLI : public AbstractInterpreter {
56 std::string LLIPath; // The path to the LLI executable
Brian Gaeked11577b2004-05-04 21:09:01 +000057 std::vector<std::string> ToolArgs; // Args to pass to LLI
Chris Lattner2cdd21c2003-12-14 21:35:53 +000058 public:
Brian Gaeked11577b2004-05-04 21:09:01 +000059 LLI(const std::string &Path, const std::vector<std::string> *Args)
60 : LLIPath(Path) {
61 ToolArgs.clear ();
Brian Gaeke48b008d2004-05-04 22:02:41 +000062 if (Args) { ToolArgs = *Args; }
Brian Gaeked11577b2004-05-04 21:09:01 +000063 }
Chris Lattner2cdd21c2003-12-14 21:35:53 +000064
65 virtual int ExecuteProgram(const std::string &Bytecode,
66 const std::vector<std::string> &Args,
67 const std::string &InputFile,
68 const std::string &OutputFile,
69 const std::vector<std::string> &SharedLibs =
Chris Lattnere96b2ed2004-07-24 07:49:11 +000070 std::vector<std::string>(),
71 unsigned Timeout = 0);
Chris Lattner2cdd21c2003-12-14 21:35:53 +000072 };
73}
Misha Brukman9558c6a2003-09-29 22:39:25 +000074
75int LLI::ExecuteProgram(const std::string &Bytecode,
Chris Lattner7915a1e2003-10-14 21:34:11 +000076 const std::vector<std::string> &Args,
Misha Brukman9558c6a2003-09-29 22:39:25 +000077 const std::string &InputFile,
78 const std::string &OutputFile,
Chris Lattnere96b2ed2004-07-24 07:49:11 +000079 const std::vector<std::string> &SharedLibs,
80 unsigned Timeout) {
Chris Lattner8c56be52004-02-18 20:21:57 +000081 if (!SharedLibs.empty())
82 throw ToolExecutionError("LLI currently does not support "
83 "loading shared libraries.");
Misha Brukman9558c6a2003-09-29 22:39:25 +000084
85 std::vector<const char*> LLIArgs;
86 LLIArgs.push_back(LLIPath.c_str());
Misha Brukman9558c6a2003-09-29 22:39:25 +000087 LLIArgs.push_back("-force-interpreter=true");
Brian Gaeked11577b2004-05-04 21:09:01 +000088
89 // Add any extra LLI args.
90 for (unsigned i = 0, e = ToolArgs.size(); i != e; ++i)
91 LLIArgs.push_back(ToolArgs[i].c_str());
92
Misha Brukman9558c6a2003-09-29 22:39:25 +000093 LLIArgs.push_back(Bytecode.c_str());
94 // Add optional parameters to the running program from Argv
95 for (unsigned i=0, e = Args.size(); i != e; ++i)
96 LLIArgs.push_back(Args[i].c_str());
97 LLIArgs.push_back(0);
98
99 std::cout << "<lli>" << std::flush;
Chris Lattner0b1fe842003-10-19 02:27:40 +0000100 DEBUG(std::cerr << "\nAbout to run:\t";
Chris Lattner7b2ccff2003-10-19 02:14:58 +0000101 for (unsigned i=0, e = LLIArgs.size()-1; i != e; ++i)
Misha Brukman9558c6a2003-09-29 22:39:25 +0000102 std::cerr << " " << LLIArgs[i];
103 std::cerr << "\n";
104 );
105 return RunProgramWithTimeout(LLIPath, &LLIArgs[0],
Chris Lattnere96b2ed2004-07-24 07:49:11 +0000106 InputFile, OutputFile, OutputFile, Timeout);
Misha Brukman9558c6a2003-09-29 22:39:25 +0000107}
108
109// LLI create method - Try to find the LLI executable
Chris Lattner7915a1e2003-10-14 21:34:11 +0000110AbstractInterpreter *AbstractInterpreter::createLLI(const std::string &ProgPath,
Brian Gaeked11577b2004-05-04 21:09:01 +0000111 std::string &Message,
112 const std::vector<std::string> *ToolArgs) {
Reid Spencer51ab8ec2004-12-13 23:43:44 +0000113 std::string LLIPath = FindExecutable("lli", ProgPath).toString();
Misha Brukman9558c6a2003-09-29 22:39:25 +0000114 if (!LLIPath.empty()) {
115 Message = "Found lli: " + LLIPath + "\n";
Brian Gaeked11577b2004-05-04 21:09:01 +0000116 return new LLI(LLIPath, ToolArgs);
Misha Brukman9558c6a2003-09-29 22:39:25 +0000117 }
118
119 Message = "Cannot find `lli' in executable directory or PATH!\n";
120 return 0;
121}
122
123//===----------------------------------------------------------------------===//
124// LLC Implementation of AbstractIntepreter interface
125//
Reid Spencer9ac14182004-12-16 23:01:34 +0000126void LLC::OutputAsm(const std::string &Bytecode, sys::Path &OutputAsmFile) {
Reid Spencercda985e2004-12-15 01:51:56 +0000127 sys::Path uniqueFile(Bytecode+".llc.s");
128 uniqueFile.makeUnique();
Reid Spencer9ac14182004-12-16 23:01:34 +0000129 OutputAsmFile = uniqueFile;
Brian Gaeked11577b2004-05-04 21:09:01 +0000130 std::vector<const char *> LLCArgs;
131 LLCArgs.push_back (LLCPath.c_str());
132
133 // Add any extra LLC args.
134 for (unsigned i = 0, e = ToolArgs.size(); i != e; ++i)
135 LLCArgs.push_back(ToolArgs[i].c_str());
136
137 LLCArgs.push_back ("-o");
138 LLCArgs.push_back (OutputAsmFile.c_str()); // Output to the Asm file
139 LLCArgs.push_back ("-f"); // Overwrite as necessary...
140 LLCArgs.push_back (Bytecode.c_str()); // This is the input bytecode
141 LLCArgs.push_back (0);
Misha Brukman9558c6a2003-09-29 22:39:25 +0000142
143 std::cout << "<llc>" << std::flush;
Brian Gaeked11577b2004-05-04 21:09:01 +0000144 DEBUG(std::cerr << "\nAbout to run:\t";
145 for (unsigned i=0, e = LLCArgs.size()-1; i != e; ++i)
146 std::cerr << " " << LLCArgs[i];
147 std::cerr << "\n";
148 );
149 if (RunProgramWithTimeout(LLCPath, &LLCArgs[0], "/dev/null", "/dev/null",
Chris Lattner8c56be52004-02-18 20:21:57 +0000150 "/dev/null"))
Brian Gaeked11577b2004-05-04 21:09:01 +0000151 ProcessFailure(LLCPath, &LLCArgs[0]);
Misha Brukman9558c6a2003-09-29 22:39:25 +0000152}
153
Chris Lattner9cbbee32004-02-18 23:24:41 +0000154void LLC::compileProgram(const std::string &Bytecode) {
Reid Spencer9ac14182004-12-16 23:01:34 +0000155 sys::Path OutputAsmFile;
Chris Lattner9cbbee32004-02-18 23:24:41 +0000156 OutputAsm(Bytecode, OutputAsmFile);
Reid Spencer9ac14182004-12-16 23:01:34 +0000157 OutputAsmFile.destroyFile();
Chris Lattner9cbbee32004-02-18 23:24:41 +0000158}
159
Misha Brukman9558c6a2003-09-29 22:39:25 +0000160int LLC::ExecuteProgram(const std::string &Bytecode,
Chris Lattner7915a1e2003-10-14 21:34:11 +0000161 const std::vector<std::string> &Args,
Misha Brukman9558c6a2003-09-29 22:39:25 +0000162 const std::string &InputFile,
163 const std::string &OutputFile,
Chris Lattnere96b2ed2004-07-24 07:49:11 +0000164 const std::vector<std::string> &SharedLibs,
165 unsigned Timeout) {
Chris Lattnereeed9832003-10-14 21:52:52 +0000166
Reid Spencer9ac14182004-12-16 23:01:34 +0000167 sys::Path OutputAsmFile;
Chris Lattner8c56be52004-02-18 20:21:57 +0000168 OutputAsm(Bytecode, OutputAsmFile);
169 FileRemover OutFileRemover(OutputAsmFile);
Misha Brukman9558c6a2003-09-29 22:39:25 +0000170
171 // Assuming LLC worked, compile the result with GCC and run it.
Reid Spencer9ac14182004-12-16 23:01:34 +0000172 return gcc->ExecuteProgram(OutputAsmFile.toString(), Args, GCC::AsmFile,
Chris Lattnere96b2ed2004-07-24 07:49:11 +0000173 InputFile, OutputFile, SharedLibs, Timeout);
Misha Brukman9558c6a2003-09-29 22:39:25 +0000174}
175
Chris Lattner7915a1e2003-10-14 21:34:11 +0000176/// createLLC - Try to find the LLC executable
Misha Brukman9558c6a2003-09-29 22:39:25 +0000177///
Chris Lattner7915a1e2003-10-14 21:34:11 +0000178LLC *AbstractInterpreter::createLLC(const std::string &ProgramPath,
Brian Gaeked11577b2004-05-04 21:09:01 +0000179 std::string &Message,
180 const std::vector<std::string> *Args) {
Reid Spencer51ab8ec2004-12-13 23:43:44 +0000181 std::string LLCPath = FindExecutable("llc", ProgramPath).toString();
Misha Brukman9558c6a2003-09-29 22:39:25 +0000182 if (LLCPath.empty()) {
183 Message = "Cannot find `llc' in executable directory or PATH!\n";
184 return 0;
185 }
186
187 Message = "Found llc: " + LLCPath + "\n";
Chris Lattner7915a1e2003-10-14 21:34:11 +0000188 GCC *gcc = GCC::create(ProgramPath, Message);
Misha Brukman9558c6a2003-09-29 22:39:25 +0000189 if (!gcc) {
190 std::cerr << Message << "\n";
191 exit(1);
192 }
Brian Gaeked11577b2004-05-04 21:09:01 +0000193 return new LLC(LLCPath, gcc, Args);
Misha Brukman9558c6a2003-09-29 22:39:25 +0000194}
195
196//===---------------------------------------------------------------------===//
197// JIT Implementation of AbstractIntepreter interface
198//
Chris Lattner2cdd21c2003-12-14 21:35:53 +0000199namespace {
200 class JIT : public AbstractInterpreter {
201 std::string LLIPath; // The path to the LLI executable
Brian Gaeked11577b2004-05-04 21:09:01 +0000202 std::vector<std::string> ToolArgs; // Args to pass to LLI
Chris Lattner2cdd21c2003-12-14 21:35:53 +0000203 public:
Brian Gaeked11577b2004-05-04 21:09:01 +0000204 JIT(const std::string &Path, const std::vector<std::string> *Args)
205 : LLIPath(Path) {
206 ToolArgs.clear ();
Brian Gaeke48b008d2004-05-04 22:02:41 +0000207 if (Args) { ToolArgs = *Args; }
Brian Gaeked11577b2004-05-04 21:09:01 +0000208 }
Chris Lattner2cdd21c2003-12-14 21:35:53 +0000209
210 virtual int ExecuteProgram(const std::string &Bytecode,
211 const std::vector<std::string> &Args,
212 const std::string &InputFile,
213 const std::string &OutputFile,
214 const std::vector<std::string> &SharedLibs =
Chris Lattnere96b2ed2004-07-24 07:49:11 +0000215 std::vector<std::string>(), unsigned Timeout =0);
Chris Lattner2cdd21c2003-12-14 21:35:53 +0000216 };
217}
Misha Brukman9558c6a2003-09-29 22:39:25 +0000218
219int JIT::ExecuteProgram(const std::string &Bytecode,
Chris Lattner7915a1e2003-10-14 21:34:11 +0000220 const std::vector<std::string> &Args,
Misha Brukman9558c6a2003-09-29 22:39:25 +0000221 const std::string &InputFile,
222 const std::string &OutputFile,
Chris Lattnere96b2ed2004-07-24 07:49:11 +0000223 const std::vector<std::string> &SharedLibs,
224 unsigned Timeout) {
Misha Brukman9558c6a2003-09-29 22:39:25 +0000225 // Construct a vector of parameters, incorporating those from the command-line
226 std::vector<const char*> JITArgs;
227 JITArgs.push_back(LLIPath.c_str());
Misha Brukman9558c6a2003-09-29 22:39:25 +0000228 JITArgs.push_back("-force-interpreter=false");
Chris Lattnereeed9832003-10-14 21:52:52 +0000229
Brian Gaeked11577b2004-05-04 21:09:01 +0000230 // Add any extra LLI args.
231 for (unsigned i = 0, e = ToolArgs.size(); i != e; ++i)
232 JITArgs.push_back(ToolArgs[i].c_str());
233
Chris Lattnereeed9832003-10-14 21:52:52 +0000234 for (unsigned i = 0, e = SharedLibs.size(); i != e; ++i) {
Misha Brukman9558c6a2003-09-29 22:39:25 +0000235 JITArgs.push_back("-load");
Chris Lattnereeed9832003-10-14 21:52:52 +0000236 JITArgs.push_back(SharedLibs[i].c_str());
Misha Brukman9558c6a2003-09-29 22:39:25 +0000237 }
238 JITArgs.push_back(Bytecode.c_str());
239 // Add optional parameters to the running program from Argv
240 for (unsigned i=0, e = Args.size(); i != e; ++i)
241 JITArgs.push_back(Args[i].c_str());
242 JITArgs.push_back(0);
243
244 std::cout << "<jit>" << std::flush;
Chris Lattner0b1fe842003-10-19 02:27:40 +0000245 DEBUG(std::cerr << "\nAbout to run:\t";
Chris Lattner7b2ccff2003-10-19 02:14:58 +0000246 for (unsigned i=0, e = JITArgs.size()-1; i != e; ++i)
Misha Brukman9558c6a2003-09-29 22:39:25 +0000247 std::cerr << " " << JITArgs[i];
248 std::cerr << "\n";
249 );
250 DEBUG(std::cerr << "\nSending output to " << OutputFile << "\n");
251 return RunProgramWithTimeout(LLIPath, &JITArgs[0],
Chris Lattnere96b2ed2004-07-24 07:49:11 +0000252 InputFile, OutputFile, OutputFile, Timeout);
Misha Brukman9558c6a2003-09-29 22:39:25 +0000253}
254
Chris Lattner7915a1e2003-10-14 21:34:11 +0000255/// createJIT - Try to find the LLI executable
Misha Brukman9558c6a2003-09-29 22:39:25 +0000256///
Chris Lattner7915a1e2003-10-14 21:34:11 +0000257AbstractInterpreter *AbstractInterpreter::createJIT(const std::string &ProgPath,
Brian Gaeked11577b2004-05-04 21:09:01 +0000258 std::string &Message, const std::vector<std::string> *Args) {
Reid Spencer51ab8ec2004-12-13 23:43:44 +0000259 std::string LLIPath = FindExecutable("lli", ProgPath).toString();
Misha Brukman9558c6a2003-09-29 22:39:25 +0000260 if (!LLIPath.empty()) {
261 Message = "Found lli: " + LLIPath + "\n";
Brian Gaeked11577b2004-05-04 21:09:01 +0000262 return new JIT(LLIPath, Args);
Misha Brukman9558c6a2003-09-29 22:39:25 +0000263 }
264
265 Message = "Cannot find `lli' in executable directory or PATH!\n";
266 return 0;
267}
268
Reid Spencer9ac14182004-12-16 23:01:34 +0000269void CBE::OutputC(const std::string &Bytecode, sys::Path& OutputCFile) {
Reid Spencercda985e2004-12-15 01:51:56 +0000270 sys::Path uniqueFile(Bytecode+".cbe.c");
271 uniqueFile.makeUnique();
Reid Spencer9ac14182004-12-16 23:01:34 +0000272 OutputCFile = uniqueFile;
Brian Gaeked11577b2004-05-04 21:09:01 +0000273 std::vector<const char *> LLCArgs;
274 LLCArgs.push_back (LLCPath.c_str());
275
276 // Add any extra LLC args.
277 for (unsigned i = 0, e = ToolArgs.size(); i != e; ++i)
278 LLCArgs.push_back(ToolArgs[i].c_str());
279
280 LLCArgs.push_back ("-o");
281 LLCArgs.push_back (OutputCFile.c_str()); // Output to the C file
282 LLCArgs.push_back ("-march=c"); // Output C language
283 LLCArgs.push_back ("-f"); // Overwrite as necessary...
284 LLCArgs.push_back (Bytecode.c_str()); // This is the input bytecode
285 LLCArgs.push_back (0);
Misha Brukman9558c6a2003-09-29 22:39:25 +0000286
287 std::cout << "<cbe>" << std::flush;
Brian Gaeked11577b2004-05-04 21:09:01 +0000288 DEBUG(std::cerr << "\nAbout to run:\t";
289 for (unsigned i=0, e = LLCArgs.size()-1; i != e; ++i)
290 std::cerr << " " << LLCArgs[i];
291 std::cerr << "\n";
292 );
293 if (RunProgramWithTimeout(LLCPath, &LLCArgs[0], "/dev/null", "/dev/null",
Chris Lattner8c56be52004-02-18 20:21:57 +0000294 "/dev/null"))
Brian Gaeked11577b2004-05-04 21:09:01 +0000295 ProcessFailure(LLCPath, &LLCArgs[0]);
Misha Brukman9558c6a2003-09-29 22:39:25 +0000296}
297
Chris Lattner9cbbee32004-02-18 23:24:41 +0000298void CBE::compileProgram(const std::string &Bytecode) {
Reid Spencer9ac14182004-12-16 23:01:34 +0000299 sys::Path OutputCFile;
Chris Lattner9cbbee32004-02-18 23:24:41 +0000300 OutputC(Bytecode, OutputCFile);
Reid Spencer9ac14182004-12-16 23:01:34 +0000301 OutputCFile.destroyFile();
Chris Lattner9cbbee32004-02-18 23:24:41 +0000302}
303
Misha Brukman9558c6a2003-09-29 22:39:25 +0000304int CBE::ExecuteProgram(const std::string &Bytecode,
Chris Lattner7915a1e2003-10-14 21:34:11 +0000305 const std::vector<std::string> &Args,
Misha Brukman9558c6a2003-09-29 22:39:25 +0000306 const std::string &InputFile,
307 const std::string &OutputFile,
Chris Lattnere96b2ed2004-07-24 07:49:11 +0000308 const std::vector<std::string> &SharedLibs,
309 unsigned Timeout) {
Reid Spencer9ac14182004-12-16 23:01:34 +0000310 sys::Path OutputCFile;
Chris Lattner8c56be52004-02-18 20:21:57 +0000311 OutputC(Bytecode, OutputCFile);
Misha Brukman9558c6a2003-09-29 22:39:25 +0000312
Chris Lattner8c56be52004-02-18 20:21:57 +0000313 FileRemover CFileRemove(OutputCFile);
Misha Brukman9558c6a2003-09-29 22:39:25 +0000314
Reid Spencer9ac14182004-12-16 23:01:34 +0000315 return gcc->ExecuteProgram(OutputCFile.toString(), Args, GCC::CFile,
Chris Lattnere96b2ed2004-07-24 07:49:11 +0000316 InputFile, OutputFile, SharedLibs, Timeout);
Misha Brukman9558c6a2003-09-29 22:39:25 +0000317}
318
Chris Lattner9915cd92004-02-17 06:40:06 +0000319/// createCBE - Try to find the 'llc' executable
Misha Brukman9558c6a2003-09-29 22:39:25 +0000320///
Chris Lattner7915a1e2003-10-14 21:34:11 +0000321CBE *AbstractInterpreter::createCBE(const std::string &ProgramPath,
Brian Gaeked11577b2004-05-04 21:09:01 +0000322 std::string &Message,
323 const std::vector<std::string> *Args) {
Reid Spencer51ab8ec2004-12-13 23:43:44 +0000324 std::string LLCPath = FindExecutable("llc", ProgramPath).toString();
Chris Lattner9915cd92004-02-17 06:40:06 +0000325 if (LLCPath.empty()) {
Misha Brukman9558c6a2003-09-29 22:39:25 +0000326 Message =
Chris Lattner9915cd92004-02-17 06:40:06 +0000327 "Cannot find `llc' in executable directory or PATH!\n";
Misha Brukman9558c6a2003-09-29 22:39:25 +0000328 return 0;
329 }
330
Chris Lattner9915cd92004-02-17 06:40:06 +0000331 Message = "Found llc: " + LLCPath + "\n";
Chris Lattner7915a1e2003-10-14 21:34:11 +0000332 GCC *gcc = GCC::create(ProgramPath, Message);
Misha Brukman9558c6a2003-09-29 22:39:25 +0000333 if (!gcc) {
334 std::cerr << Message << "\n";
335 exit(1);
336 }
Brian Gaeked11577b2004-05-04 21:09:01 +0000337 return new CBE(LLCPath, gcc, Args);
Misha Brukman9558c6a2003-09-29 22:39:25 +0000338}
339
340//===---------------------------------------------------------------------===//
341// GCC abstraction
342//
Misha Brukman9558c6a2003-09-29 22:39:25 +0000343int GCC::ExecuteProgram(const std::string &ProgramFile,
Chris Lattner7915a1e2003-10-14 21:34:11 +0000344 const std::vector<std::string> &Args,
Misha Brukman9558c6a2003-09-29 22:39:25 +0000345 FileType fileType,
346 const std::string &InputFile,
347 const std::string &OutputFile,
Chris Lattnere96b2ed2004-07-24 07:49:11 +0000348 const std::vector<std::string> &SharedLibs,
349 unsigned Timeout) {
Misha Brukman9558c6a2003-09-29 22:39:25 +0000350 std::vector<const char*> GCCArgs;
351
352 GCCArgs.push_back(GCCPath.c_str());
Chris Lattnereeed9832003-10-14 21:52:52 +0000353
354 // Specify the shared libraries to link in...
355 for (unsigned i = 0, e = SharedLibs.size(); i != e; ++i)
356 GCCArgs.push_back(SharedLibs[i].c_str());
357
358 // Specify -x explicitly in case the extension is wonky
Misha Brukman9558c6a2003-09-29 22:39:25 +0000359 GCCArgs.push_back("-x");
360 if (fileType == CFile) {
361 GCCArgs.push_back("c");
362 GCCArgs.push_back("-fno-strict-aliasing");
363 } else {
364 GCCArgs.push_back("assembler");
365 }
366 GCCArgs.push_back(ProgramFile.c_str()); // Specify the input filename...
367 GCCArgs.push_back("-o");
Reid Spencercda985e2004-12-15 01:51:56 +0000368 sys::Path OutputBinary (ProgramFile+".gcc.exe");
369 OutputBinary.makeUnique();
Misha Brukman9558c6a2003-09-29 22:39:25 +0000370 GCCArgs.push_back(OutputBinary.c_str()); // Output to the right file...
371 GCCArgs.push_back("-lm"); // Hard-code the math library...
372 GCCArgs.push_back("-O2"); // Optimize the program a bit...
Brian Gaekec8db76c2003-11-18 06:31:17 +0000373#if defined (HAVE_LINK_R)
Chris Lattner1f0f1622003-10-18 21:54:47 +0000374 GCCArgs.push_back("-Wl,-R."); // Search this dir for .so files
Brian Gaekec8db76c2003-11-18 06:31:17 +0000375#endif
Misha Brukman9558c6a2003-09-29 22:39:25 +0000376 GCCArgs.push_back(0); // NULL terminator
377
378 std::cout << "<gcc>" << std::flush;
379 if (RunProgramWithTimeout(GCCPath, &GCCArgs[0], "/dev/null", "/dev/null",
380 "/dev/null")) {
Chris Lattner89bf9ea2004-02-18 20:38:00 +0000381 ProcessFailure(GCCPath, &GCCArgs[0]);
Misha Brukman9558c6a2003-09-29 22:39:25 +0000382 exit(1);
383 }
384
385 std::vector<const char*> ProgramArgs;
386 ProgramArgs.push_back(OutputBinary.c_str());
387 // Add optional parameters to the running program from Argv
388 for (unsigned i=0, e = Args.size(); i != e; ++i)
389 ProgramArgs.push_back(Args[i].c_str());
390 ProgramArgs.push_back(0); // NULL terminator
391
392 // Now that we have a binary, run it!
393 std::cout << "<program>" << std::flush;
Chris Lattner0b1fe842003-10-19 02:27:40 +0000394 DEBUG(std::cerr << "\nAbout to run:\t";
Chris Lattner7b2ccff2003-10-19 02:14:58 +0000395 for (unsigned i=0, e = ProgramArgs.size()-1; i != e; ++i)
Misha Brukman9558c6a2003-09-29 22:39:25 +0000396 std::cerr << " " << ProgramArgs[i];
397 std::cerr << "\n";
398 );
Chris Lattner8c56be52004-02-18 20:21:57 +0000399
Reid Spencer9ac14182004-12-16 23:01:34 +0000400 FileRemover OutputBinaryRemover(OutputBinary);
Reid Spencercda985e2004-12-15 01:51:56 +0000401 return RunProgramWithTimeout(OutputBinary.toString(), &ProgramArgs[0],
Chris Lattnere96b2ed2004-07-24 07:49:11 +0000402 InputFile, OutputFile, OutputFile, Timeout);
Misha Brukman9558c6a2003-09-29 22:39:25 +0000403}
404
Chris Lattner1798e4a2003-10-14 21:07:25 +0000405int GCC::MakeSharedObject(const std::string &InputFile, FileType fileType,
Misha Brukman9558c6a2003-09-29 22:39:25 +0000406 std::string &OutputFile) {
Reid Spencercda985e2004-12-15 01:51:56 +0000407 sys::Path uniqueFilename(InputFile+LTDL_SHLIB_EXT);
408 uniqueFilename.makeUnique();
409 OutputFile = uniqueFilename.toString();
410
Misha Brukman9558c6a2003-09-29 22:39:25 +0000411 // Compile the C/asm file into a shared object
412 const char* GCCArgs[] = {
413 GCCPath.c_str(),
414 "-x", (fileType == AsmFile) ? "assembler" : "c",
415 "-fno-strict-aliasing",
416 InputFile.c_str(), // Specify the input filename...
417#if defined(sparc) || defined(__sparc__) || defined(__sparcv9)
418 "-G", // Compile a shared library, `-G' for Sparc
Misha Brukmanb3998ec2004-07-16 19:45:45 +0000419#elif (defined(__POWERPC__) || defined(__ppc__)) && defined(__APPLE__)
Nate Begeman79255882004-10-17 23:03:32 +0000420 "-single_module", // link all source files into a single module
Misha Brukmanb3998ec2004-07-16 19:45:45 +0000421 "-dynamiclib", // `-dynamiclib' for MacOS X/PowerPC
Chris Lattnere312e152004-07-19 06:03:51 +0000422 "-undefined", // in data segment, rather than generating
Chris Lattner5fbf29c2004-07-19 06:00:17 +0000423 "dynamic_lookup", // blocks. dynamic_lookup requires that you set
424 // MACOSX_DEPLOYMENT_TARGET=10.3 in your env.
Misha Brukmanb3998ec2004-07-16 19:45:45 +0000425#else
Misha Brukman9558c6a2003-09-29 22:39:25 +0000426 "-shared", // `-shared' for Linux/X86, maybe others
427#endif
428 "-o", OutputFile.c_str(), // Output to the right filename...
429 "-O2", // Optimize the program a bit...
430 0
431 };
432
433 std::cout << "<gcc>" << std::flush;
Chris Lattner1798e4a2003-10-14 21:07:25 +0000434 if (RunProgramWithTimeout(GCCPath, GCCArgs, "/dev/null", "/dev/null",
435 "/dev/null")) {
Chris Lattner89bf9ea2004-02-18 20:38:00 +0000436 ProcessFailure(GCCPath, GCCArgs);
Chris Lattner1798e4a2003-10-14 21:07:25 +0000437 return 1;
Misha Brukman9558c6a2003-09-29 22:39:25 +0000438 }
439 return 0;
440}
441
Chris Lattner7915a1e2003-10-14 21:34:11 +0000442/// create - Try to find the `gcc' executable
Misha Brukman9558c6a2003-09-29 22:39:25 +0000443///
Chris Lattner7915a1e2003-10-14 21:34:11 +0000444GCC *GCC::create(const std::string &ProgramPath, std::string &Message) {
Reid Spencer51ab8ec2004-12-13 23:43:44 +0000445 std::string GCCPath = FindExecutable("gcc", ProgramPath).toString();
Misha Brukman9558c6a2003-09-29 22:39:25 +0000446 if (GCCPath.empty()) {
447 Message = "Cannot find `gcc' in executable directory or PATH!\n";
448 return 0;
449 }
450
451 Message = "Found gcc: " + GCCPath + "\n";
452 return new GCC(GCCPath);
453}