blob: a07046a0d06126f2126b346a0ab620e96bd457b1 [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
Reid Spencerb31baa82004-12-19 18:00:21 +000025static void ProcessFailure(sys::Path ProgPath, const char** Args) {
Chris Lattner89bf9ea2004-02-18 20:38:00 +000026 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();
Reid Spencerb31baa82004-12-19 18:00:21 +000035 RunProgramWithTimeout(ProgPath, Args, sys::Path(""), ErrorFilename,
36 ErrorFilename);
Chris Lattner89bf9ea2004-02-18 20:38:00 +000037
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 );
Reid Spencerb31baa82004-12-19 18:00:21 +0000105 return RunProgramWithTimeout(sys::Path(LLIPath), &LLIArgs[0],
106 sys::Path(InputFile), sys::Path(OutputFile), sys::Path(OutputFile),
107 Timeout);
Misha Brukman9558c6a2003-09-29 22:39:25 +0000108}
109
110// LLI create method - Try to find the LLI executable
Chris Lattner7915a1e2003-10-14 21:34:11 +0000111AbstractInterpreter *AbstractInterpreter::createLLI(const std::string &ProgPath,
Brian Gaeked11577b2004-05-04 21:09:01 +0000112 std::string &Message,
113 const std::vector<std::string> *ToolArgs) {
Reid Spencer51ab8ec2004-12-13 23:43:44 +0000114 std::string LLIPath = FindExecutable("lli", ProgPath).toString();
Misha Brukman9558c6a2003-09-29 22:39:25 +0000115 if (!LLIPath.empty()) {
116 Message = "Found lli: " + LLIPath + "\n";
Brian Gaeked11577b2004-05-04 21:09:01 +0000117 return new LLI(LLIPath, ToolArgs);
Misha Brukman9558c6a2003-09-29 22:39:25 +0000118 }
119
120 Message = "Cannot find `lli' in executable directory or PATH!\n";
121 return 0;
122}
123
124//===----------------------------------------------------------------------===//
125// LLC Implementation of AbstractIntepreter interface
126//
Reid Spencer9ac14182004-12-16 23:01:34 +0000127void LLC::OutputAsm(const std::string &Bytecode, sys::Path &OutputAsmFile) {
Reid Spencercda985e2004-12-15 01:51:56 +0000128 sys::Path uniqueFile(Bytecode+".llc.s");
129 uniqueFile.makeUnique();
Reid Spencer9ac14182004-12-16 23:01:34 +0000130 OutputAsmFile = uniqueFile;
Brian Gaeked11577b2004-05-04 21:09:01 +0000131 std::vector<const char *> LLCArgs;
132 LLCArgs.push_back (LLCPath.c_str());
133
134 // Add any extra LLC args.
135 for (unsigned i = 0, e = ToolArgs.size(); i != e; ++i)
136 LLCArgs.push_back(ToolArgs[i].c_str());
137
138 LLCArgs.push_back ("-o");
139 LLCArgs.push_back (OutputAsmFile.c_str()); // Output to the Asm file
140 LLCArgs.push_back ("-f"); // Overwrite as necessary...
141 LLCArgs.push_back (Bytecode.c_str()); // This is the input bytecode
142 LLCArgs.push_back (0);
Misha Brukman9558c6a2003-09-29 22:39:25 +0000143
144 std::cout << "<llc>" << std::flush;
Brian Gaeked11577b2004-05-04 21:09:01 +0000145 DEBUG(std::cerr << "\nAbout to run:\t";
146 for (unsigned i=0, e = LLCArgs.size()-1; i != e; ++i)
147 std::cerr << " " << LLCArgs[i];
148 std::cerr << "\n";
149 );
Reid Spencerb31baa82004-12-19 18:00:21 +0000150 if (RunProgramWithTimeout(sys::Path(LLCPath), &LLCArgs[0],
151 sys::Path(), sys::Path(), sys::Path()))
152 ProcessFailure(sys::Path(LLCPath), &LLCArgs[0]);
Misha Brukman9558c6a2003-09-29 22:39:25 +0000153}
154
Chris Lattner9cbbee32004-02-18 23:24:41 +0000155void LLC::compileProgram(const std::string &Bytecode) {
Reid Spencer9ac14182004-12-16 23:01:34 +0000156 sys::Path OutputAsmFile;
Chris Lattner9cbbee32004-02-18 23:24:41 +0000157 OutputAsm(Bytecode, OutputAsmFile);
Reid Spencer9ac14182004-12-16 23:01:34 +0000158 OutputAsmFile.destroyFile();
Chris Lattner9cbbee32004-02-18 23:24:41 +0000159}
160
Misha Brukman9558c6a2003-09-29 22:39:25 +0000161int LLC::ExecuteProgram(const std::string &Bytecode,
Chris Lattner7915a1e2003-10-14 21:34:11 +0000162 const std::vector<std::string> &Args,
Misha Brukman9558c6a2003-09-29 22:39:25 +0000163 const std::string &InputFile,
164 const std::string &OutputFile,
Chris Lattnere96b2ed2004-07-24 07:49:11 +0000165 const std::vector<std::string> &SharedLibs,
166 unsigned Timeout) {
Chris Lattnereeed9832003-10-14 21:52:52 +0000167
Reid Spencer9ac14182004-12-16 23:01:34 +0000168 sys::Path OutputAsmFile;
Chris Lattner8c56be52004-02-18 20:21:57 +0000169 OutputAsm(Bytecode, OutputAsmFile);
170 FileRemover OutFileRemover(OutputAsmFile);
Misha Brukman9558c6a2003-09-29 22:39:25 +0000171
172 // Assuming LLC worked, compile the result with GCC and run it.
Reid Spencer9ac14182004-12-16 23:01:34 +0000173 return gcc->ExecuteProgram(OutputAsmFile.toString(), Args, GCC::AsmFile,
Chris Lattnere96b2ed2004-07-24 07:49:11 +0000174 InputFile, OutputFile, SharedLibs, Timeout);
Misha Brukman9558c6a2003-09-29 22:39:25 +0000175}
176
Chris Lattner7915a1e2003-10-14 21:34:11 +0000177/// createLLC - Try to find the LLC executable
Misha Brukman9558c6a2003-09-29 22:39:25 +0000178///
Chris Lattner7915a1e2003-10-14 21:34:11 +0000179LLC *AbstractInterpreter::createLLC(const std::string &ProgramPath,
Brian Gaeked11577b2004-05-04 21:09:01 +0000180 std::string &Message,
181 const std::vector<std::string> *Args) {
Reid Spencer51ab8ec2004-12-13 23:43:44 +0000182 std::string LLCPath = FindExecutable("llc", ProgramPath).toString();
Misha Brukman9558c6a2003-09-29 22:39:25 +0000183 if (LLCPath.empty()) {
184 Message = "Cannot find `llc' in executable directory or PATH!\n";
185 return 0;
186 }
187
188 Message = "Found llc: " + LLCPath + "\n";
Chris Lattner7915a1e2003-10-14 21:34:11 +0000189 GCC *gcc = GCC::create(ProgramPath, Message);
Misha Brukman9558c6a2003-09-29 22:39:25 +0000190 if (!gcc) {
191 std::cerr << Message << "\n";
192 exit(1);
193 }
Brian Gaeked11577b2004-05-04 21:09:01 +0000194 return new LLC(LLCPath, gcc, Args);
Misha Brukman9558c6a2003-09-29 22:39:25 +0000195}
196
197//===---------------------------------------------------------------------===//
198// JIT Implementation of AbstractIntepreter interface
199//
Chris Lattner2cdd21c2003-12-14 21:35:53 +0000200namespace {
201 class JIT : public AbstractInterpreter {
202 std::string LLIPath; // The path to the LLI executable
Brian Gaeked11577b2004-05-04 21:09:01 +0000203 std::vector<std::string> ToolArgs; // Args to pass to LLI
Chris Lattner2cdd21c2003-12-14 21:35:53 +0000204 public:
Brian Gaeked11577b2004-05-04 21:09:01 +0000205 JIT(const std::string &Path, const std::vector<std::string> *Args)
206 : LLIPath(Path) {
207 ToolArgs.clear ();
Brian Gaeke48b008d2004-05-04 22:02:41 +0000208 if (Args) { ToolArgs = *Args; }
Brian Gaeked11577b2004-05-04 21:09:01 +0000209 }
Chris Lattner2cdd21c2003-12-14 21:35:53 +0000210
211 virtual int ExecuteProgram(const std::string &Bytecode,
212 const std::vector<std::string> &Args,
213 const std::string &InputFile,
214 const std::string &OutputFile,
215 const std::vector<std::string> &SharedLibs =
Chris Lattnere96b2ed2004-07-24 07:49:11 +0000216 std::vector<std::string>(), unsigned Timeout =0);
Chris Lattner2cdd21c2003-12-14 21:35:53 +0000217 };
218}
Misha Brukman9558c6a2003-09-29 22:39:25 +0000219
220int JIT::ExecuteProgram(const std::string &Bytecode,
Chris Lattner7915a1e2003-10-14 21:34:11 +0000221 const std::vector<std::string> &Args,
Misha Brukman9558c6a2003-09-29 22:39:25 +0000222 const std::string &InputFile,
223 const std::string &OutputFile,
Chris Lattnere96b2ed2004-07-24 07:49:11 +0000224 const std::vector<std::string> &SharedLibs,
225 unsigned Timeout) {
Misha Brukman9558c6a2003-09-29 22:39:25 +0000226 // Construct a vector of parameters, incorporating those from the command-line
227 std::vector<const char*> JITArgs;
228 JITArgs.push_back(LLIPath.c_str());
Misha Brukman9558c6a2003-09-29 22:39:25 +0000229 JITArgs.push_back("-force-interpreter=false");
Chris Lattnereeed9832003-10-14 21:52:52 +0000230
Brian Gaeked11577b2004-05-04 21:09:01 +0000231 // Add any extra LLI args.
232 for (unsigned i = 0, e = ToolArgs.size(); i != e; ++i)
233 JITArgs.push_back(ToolArgs[i].c_str());
234
Chris Lattnereeed9832003-10-14 21:52:52 +0000235 for (unsigned i = 0, e = SharedLibs.size(); i != e; ++i) {
Misha Brukman9558c6a2003-09-29 22:39:25 +0000236 JITArgs.push_back("-load");
Chris Lattnereeed9832003-10-14 21:52:52 +0000237 JITArgs.push_back(SharedLibs[i].c_str());
Misha Brukman9558c6a2003-09-29 22:39:25 +0000238 }
239 JITArgs.push_back(Bytecode.c_str());
240 // Add optional parameters to the running program from Argv
241 for (unsigned i=0, e = Args.size(); i != e; ++i)
242 JITArgs.push_back(Args[i].c_str());
243 JITArgs.push_back(0);
244
245 std::cout << "<jit>" << std::flush;
Chris Lattner0b1fe842003-10-19 02:27:40 +0000246 DEBUG(std::cerr << "\nAbout to run:\t";
Chris Lattner7b2ccff2003-10-19 02:14:58 +0000247 for (unsigned i=0, e = JITArgs.size()-1; i != e; ++i)
Misha Brukman9558c6a2003-09-29 22:39:25 +0000248 std::cerr << " " << JITArgs[i];
249 std::cerr << "\n";
250 );
251 DEBUG(std::cerr << "\nSending output to " << OutputFile << "\n");
Reid Spencerb31baa82004-12-19 18:00:21 +0000252 return RunProgramWithTimeout(sys::Path(LLIPath), &JITArgs[0],
253 sys::Path(InputFile), sys::Path(OutputFile), sys::Path(OutputFile),
254 Timeout);
Misha Brukman9558c6a2003-09-29 22:39:25 +0000255}
256
Chris Lattner7915a1e2003-10-14 21:34:11 +0000257/// createJIT - Try to find the LLI executable
Misha Brukman9558c6a2003-09-29 22:39:25 +0000258///
Chris Lattner7915a1e2003-10-14 21:34:11 +0000259AbstractInterpreter *AbstractInterpreter::createJIT(const std::string &ProgPath,
Brian Gaeked11577b2004-05-04 21:09:01 +0000260 std::string &Message, const std::vector<std::string> *Args) {
Reid Spencer51ab8ec2004-12-13 23:43:44 +0000261 std::string LLIPath = FindExecutable("lli", ProgPath).toString();
Misha Brukman9558c6a2003-09-29 22:39:25 +0000262 if (!LLIPath.empty()) {
263 Message = "Found lli: " + LLIPath + "\n";
Brian Gaeked11577b2004-05-04 21:09:01 +0000264 return new JIT(LLIPath, Args);
Misha Brukman9558c6a2003-09-29 22:39:25 +0000265 }
266
267 Message = "Cannot find `lli' in executable directory or PATH!\n";
268 return 0;
269}
270
Reid Spencer9ac14182004-12-16 23:01:34 +0000271void CBE::OutputC(const std::string &Bytecode, sys::Path& OutputCFile) {
Reid Spencercda985e2004-12-15 01:51:56 +0000272 sys::Path uniqueFile(Bytecode+".cbe.c");
273 uniqueFile.makeUnique();
Reid Spencer9ac14182004-12-16 23:01:34 +0000274 OutputCFile = uniqueFile;
Brian Gaeked11577b2004-05-04 21:09:01 +0000275 std::vector<const char *> LLCArgs;
276 LLCArgs.push_back (LLCPath.c_str());
277
278 // Add any extra LLC args.
279 for (unsigned i = 0, e = ToolArgs.size(); i != e; ++i)
280 LLCArgs.push_back(ToolArgs[i].c_str());
281
282 LLCArgs.push_back ("-o");
283 LLCArgs.push_back (OutputCFile.c_str()); // Output to the C file
284 LLCArgs.push_back ("-march=c"); // Output C language
285 LLCArgs.push_back ("-f"); // Overwrite as necessary...
286 LLCArgs.push_back (Bytecode.c_str()); // This is the input bytecode
287 LLCArgs.push_back (0);
Misha Brukman9558c6a2003-09-29 22:39:25 +0000288
289 std::cout << "<cbe>" << std::flush;
Brian Gaeked11577b2004-05-04 21:09:01 +0000290 DEBUG(std::cerr << "\nAbout to run:\t";
291 for (unsigned i=0, e = LLCArgs.size()-1; i != e; ++i)
292 std::cerr << " " << LLCArgs[i];
293 std::cerr << "\n";
294 );
Reid Spencerb31baa82004-12-19 18:00:21 +0000295 if (RunProgramWithTimeout(LLCPath, &LLCArgs[0], sys::Path(), sys::Path(),
296 sys::Path()))
Brian Gaeked11577b2004-05-04 21:09:01 +0000297 ProcessFailure(LLCPath, &LLCArgs[0]);
Misha Brukman9558c6a2003-09-29 22:39:25 +0000298}
299
Chris Lattner9cbbee32004-02-18 23:24:41 +0000300void CBE::compileProgram(const std::string &Bytecode) {
Reid Spencer9ac14182004-12-16 23:01:34 +0000301 sys::Path OutputCFile;
Chris Lattner9cbbee32004-02-18 23:24:41 +0000302 OutputC(Bytecode, OutputCFile);
Reid Spencer9ac14182004-12-16 23:01:34 +0000303 OutputCFile.destroyFile();
Chris Lattner9cbbee32004-02-18 23:24:41 +0000304}
305
Misha Brukman9558c6a2003-09-29 22:39:25 +0000306int CBE::ExecuteProgram(const std::string &Bytecode,
Chris Lattner7915a1e2003-10-14 21:34:11 +0000307 const std::vector<std::string> &Args,
Misha Brukman9558c6a2003-09-29 22:39:25 +0000308 const std::string &InputFile,
309 const std::string &OutputFile,
Chris Lattnere96b2ed2004-07-24 07:49:11 +0000310 const std::vector<std::string> &SharedLibs,
311 unsigned Timeout) {
Reid Spencer9ac14182004-12-16 23:01:34 +0000312 sys::Path OutputCFile;
Chris Lattner8c56be52004-02-18 20:21:57 +0000313 OutputC(Bytecode, OutputCFile);
Misha Brukman9558c6a2003-09-29 22:39:25 +0000314
Chris Lattner8c56be52004-02-18 20:21:57 +0000315 FileRemover CFileRemove(OutputCFile);
Misha Brukman9558c6a2003-09-29 22:39:25 +0000316
Reid Spencer9ac14182004-12-16 23:01:34 +0000317 return gcc->ExecuteProgram(OutputCFile.toString(), Args, GCC::CFile,
Chris Lattnere96b2ed2004-07-24 07:49:11 +0000318 InputFile, OutputFile, SharedLibs, Timeout);
Misha Brukman9558c6a2003-09-29 22:39:25 +0000319}
320
Chris Lattner9915cd92004-02-17 06:40:06 +0000321/// createCBE - Try to find the 'llc' executable
Misha Brukman9558c6a2003-09-29 22:39:25 +0000322///
Chris Lattner7915a1e2003-10-14 21:34:11 +0000323CBE *AbstractInterpreter::createCBE(const std::string &ProgramPath,
Brian Gaeked11577b2004-05-04 21:09:01 +0000324 std::string &Message,
325 const std::vector<std::string> *Args) {
Reid Spencerb31baa82004-12-19 18:00:21 +0000326 sys::Path LLCPath = FindExecutable("llc", ProgramPath);
327 if (LLCPath.isEmpty()) {
Misha Brukman9558c6a2003-09-29 22:39:25 +0000328 Message =
Chris Lattner9915cd92004-02-17 06:40:06 +0000329 "Cannot find `llc' in executable directory or PATH!\n";
Misha Brukman9558c6a2003-09-29 22:39:25 +0000330 return 0;
331 }
332
Reid Spencerb31baa82004-12-19 18:00:21 +0000333 Message = "Found llc: " + LLCPath.toString() + "\n";
Chris Lattner7915a1e2003-10-14 21:34:11 +0000334 GCC *gcc = GCC::create(ProgramPath, Message);
Misha Brukman9558c6a2003-09-29 22:39:25 +0000335 if (!gcc) {
336 std::cerr << Message << "\n";
337 exit(1);
338 }
Brian Gaeked11577b2004-05-04 21:09:01 +0000339 return new CBE(LLCPath, gcc, Args);
Misha Brukman9558c6a2003-09-29 22:39:25 +0000340}
341
342//===---------------------------------------------------------------------===//
343// GCC abstraction
344//
Misha Brukman9558c6a2003-09-29 22:39:25 +0000345int GCC::ExecuteProgram(const std::string &ProgramFile,
Chris Lattner7915a1e2003-10-14 21:34:11 +0000346 const std::vector<std::string> &Args,
Misha Brukman9558c6a2003-09-29 22:39:25 +0000347 FileType fileType,
348 const std::string &InputFile,
349 const std::string &OutputFile,
Chris Lattnere96b2ed2004-07-24 07:49:11 +0000350 const std::vector<std::string> &SharedLibs,
351 unsigned Timeout) {
Misha Brukman9558c6a2003-09-29 22:39:25 +0000352 std::vector<const char*> GCCArgs;
353
354 GCCArgs.push_back(GCCPath.c_str());
Chris Lattnereeed9832003-10-14 21:52:52 +0000355
356 // Specify the shared libraries to link in...
357 for (unsigned i = 0, e = SharedLibs.size(); i != e; ++i)
358 GCCArgs.push_back(SharedLibs[i].c_str());
359
360 // Specify -x explicitly in case the extension is wonky
Misha Brukman9558c6a2003-09-29 22:39:25 +0000361 GCCArgs.push_back("-x");
362 if (fileType == CFile) {
363 GCCArgs.push_back("c");
364 GCCArgs.push_back("-fno-strict-aliasing");
365 } else {
366 GCCArgs.push_back("assembler");
367 }
368 GCCArgs.push_back(ProgramFile.c_str()); // Specify the input filename...
369 GCCArgs.push_back("-o");
Reid Spencercda985e2004-12-15 01:51:56 +0000370 sys::Path OutputBinary (ProgramFile+".gcc.exe");
371 OutputBinary.makeUnique();
Misha Brukman9558c6a2003-09-29 22:39:25 +0000372 GCCArgs.push_back(OutputBinary.c_str()); // Output to the right file...
373 GCCArgs.push_back("-lm"); // Hard-code the math library...
374 GCCArgs.push_back("-O2"); // Optimize the program a bit...
Brian Gaekec8db76c2003-11-18 06:31:17 +0000375#if defined (HAVE_LINK_R)
Chris Lattner1f0f1622003-10-18 21:54:47 +0000376 GCCArgs.push_back("-Wl,-R."); // Search this dir for .so files
Brian Gaekec8db76c2003-11-18 06:31:17 +0000377#endif
Misha Brukman9558c6a2003-09-29 22:39:25 +0000378 GCCArgs.push_back(0); // NULL terminator
379
380 std::cout << "<gcc>" << std::flush;
Reid Spencerb31baa82004-12-19 18:00:21 +0000381 if (RunProgramWithTimeout(GCCPath, &GCCArgs[0], sys::Path(), sys::Path(),
382 sys::Path())) {
Chris Lattner89bf9ea2004-02-18 20:38:00 +0000383 ProcessFailure(GCCPath, &GCCArgs[0]);
Misha Brukman9558c6a2003-09-29 22:39:25 +0000384 exit(1);
385 }
386
387 std::vector<const char*> ProgramArgs;
388 ProgramArgs.push_back(OutputBinary.c_str());
389 // Add optional parameters to the running program from Argv
390 for (unsigned i=0, e = Args.size(); i != e; ++i)
391 ProgramArgs.push_back(Args[i].c_str());
392 ProgramArgs.push_back(0); // NULL terminator
393
394 // Now that we have a binary, run it!
395 std::cout << "<program>" << std::flush;
Chris Lattner0b1fe842003-10-19 02:27:40 +0000396 DEBUG(std::cerr << "\nAbout to run:\t";
Chris Lattner7b2ccff2003-10-19 02:14:58 +0000397 for (unsigned i=0, e = ProgramArgs.size()-1; i != e; ++i)
Misha Brukman9558c6a2003-09-29 22:39:25 +0000398 std::cerr << " " << ProgramArgs[i];
399 std::cerr << "\n";
400 );
Chris Lattner8c56be52004-02-18 20:21:57 +0000401
Reid Spencer9ac14182004-12-16 23:01:34 +0000402 FileRemover OutputBinaryRemover(OutputBinary);
Reid Spencerb31baa82004-12-19 18:00:21 +0000403 return RunProgramWithTimeout(OutputBinary, &ProgramArgs[0],
404 sys::Path(InputFile), sys::Path(OutputFile), sys::Path(OutputFile),
405 Timeout);
Misha Brukman9558c6a2003-09-29 22:39:25 +0000406}
407
Chris Lattner1798e4a2003-10-14 21:07:25 +0000408int GCC::MakeSharedObject(const std::string &InputFile, FileType fileType,
Misha Brukman9558c6a2003-09-29 22:39:25 +0000409 std::string &OutputFile) {
Reid Spencercda985e2004-12-15 01:51:56 +0000410 sys::Path uniqueFilename(InputFile+LTDL_SHLIB_EXT);
411 uniqueFilename.makeUnique();
412 OutputFile = uniqueFilename.toString();
413
Misha Brukman9558c6a2003-09-29 22:39:25 +0000414 // Compile the C/asm file into a shared object
415 const char* GCCArgs[] = {
416 GCCPath.c_str(),
417 "-x", (fileType == AsmFile) ? "assembler" : "c",
418 "-fno-strict-aliasing",
419 InputFile.c_str(), // Specify the input filename...
420#if defined(sparc) || defined(__sparc__) || defined(__sparcv9)
421 "-G", // Compile a shared library, `-G' for Sparc
Misha Brukmanb3998ec2004-07-16 19:45:45 +0000422#elif (defined(__POWERPC__) || defined(__ppc__)) && defined(__APPLE__)
Nate Begeman79255882004-10-17 23:03:32 +0000423 "-single_module", // link all source files into a single module
Misha Brukmanb3998ec2004-07-16 19:45:45 +0000424 "-dynamiclib", // `-dynamiclib' for MacOS X/PowerPC
Chris Lattnere312e152004-07-19 06:03:51 +0000425 "-undefined", // in data segment, rather than generating
Chris Lattner5fbf29c2004-07-19 06:00:17 +0000426 "dynamic_lookup", // blocks. dynamic_lookup requires that you set
427 // MACOSX_DEPLOYMENT_TARGET=10.3 in your env.
Misha Brukmanb3998ec2004-07-16 19:45:45 +0000428#else
Misha Brukman9558c6a2003-09-29 22:39:25 +0000429 "-shared", // `-shared' for Linux/X86, maybe others
430#endif
431 "-o", OutputFile.c_str(), // Output to the right filename...
432 "-O2", // Optimize the program a bit...
433 0
434 };
435
436 std::cout << "<gcc>" << std::flush;
Reid Spencerb31baa82004-12-19 18:00:21 +0000437 if (RunProgramWithTimeout(GCCPath, GCCArgs, sys::Path(), sys::Path(),
438 sys::Path())) {
Chris Lattner89bf9ea2004-02-18 20:38:00 +0000439 ProcessFailure(GCCPath, GCCArgs);
Chris Lattner1798e4a2003-10-14 21:07:25 +0000440 return 1;
Misha Brukman9558c6a2003-09-29 22:39:25 +0000441 }
442 return 0;
443}
444
Chris Lattner7915a1e2003-10-14 21:34:11 +0000445/// create - Try to find the `gcc' executable
Misha Brukman9558c6a2003-09-29 22:39:25 +0000446///
Chris Lattner7915a1e2003-10-14 21:34:11 +0000447GCC *GCC::create(const std::string &ProgramPath, std::string &Message) {
Reid Spencerb31baa82004-12-19 18:00:21 +0000448 sys::Path GCCPath = FindExecutable("gcc", ProgramPath);
449 if (GCCPath.isEmpty()) {
Misha Brukman9558c6a2003-09-29 22:39:25 +0000450 Message = "Cannot find `gcc' in executable directory or PATH!\n";
451 return 0;
452 }
453
Reid Spencerb31baa82004-12-19 18:00:21 +0000454 Message = "Found gcc: " + GCCPath.toString() + "\n";
Misha Brukman9558c6a2003-09-29 22:39:25 +0000455 return new GCC(GCCPath);
456}