blob: c85a6be04987e3f05a2170a3b712862a3f9111ff [file] [log] [blame]
Reid Spencerc0af3f02004-09-13 01:27:53 +00001//===- llvm-ld.cpp - LLVM 'ld' compatible linker --------------------------===//
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//===----------------------------------------------------------------------===//
9//
10// This utility is intended to be compatible with GCC, and follows standard
11// system 'ld' conventions. As such, the default output file is ./a.out.
12// Additionally, this program outputs a shell script that is used to invoke LLI
13// to execute the program. In this manner, the generated executable (a.out for
14// example), is directly executable, whereas the bytecode file actually lives in
15// the a.out.bc file generated by this program. Also, Force is on by default.
16//
17// Note that if someone (or a script) deletes the executable program generated,
18// the .bc file will be left around. Considering that this is a temporary hack,
19// I'm not too worried about this.
20//
21//===----------------------------------------------------------------------===//
22
Reid Spencer605b9e22004-11-14 23:00:08 +000023#include "llvm/Linker.h"
Reid Spencer6da1e0d2004-12-14 04:20:08 +000024#include "llvm/System/Program.h"
Reid Spencerc0af3f02004-09-13 01:27:53 +000025#include "llvm/Module.h"
26#include "llvm/PassManager.h"
27#include "llvm/Bytecode/Reader.h"
Reid Spencer445564a2004-11-20 20:02:56 +000028#include "llvm/Bytecode/Writer.h"
Reid Spencerc0af3f02004-09-13 01:27:53 +000029#include "llvm/Target/TargetData.h"
Reid Spenceraefd04b2004-09-25 16:00:07 +000030#include "llvm/Target/TargetMachine.h"
31#include "llvm/Target/TargetMachineRegistry.h"
Reid Spencerc0af3f02004-09-13 01:27:53 +000032#include "llvm/Support/CommandLine.h"
33#include "llvm/Support/FileUtilities.h"
Reid Spencerc0af3f02004-09-13 01:27:53 +000034#include "llvm/Support/SystemUtils.h"
Reid Spencer445564a2004-11-20 20:02:56 +000035#include "llvm/System/Signals.h"
Reid Spencerc0af3f02004-09-13 01:27:53 +000036#include <fstream>
Reid Spencer445564a2004-11-20 20:02:56 +000037#include <iostream>
Reid Spencerc0af3f02004-09-13 01:27:53 +000038#include <memory>
Reid Spencer445564a2004-11-20 20:02:56 +000039
Reid Spencerc0af3f02004-09-13 01:27:53 +000040using namespace llvm;
41
Reid Spencer445564a2004-11-20 20:02:56 +000042// Input/Output Options
43static cl::list<std::string> InputFilenames(cl::Positional, cl::OneOrMore,
44 cl::desc("<input bytecode files>"));
Reid Spencerc0af3f02004-09-13 01:27:53 +000045
Reid Spencer445564a2004-11-20 20:02:56 +000046static cl::opt<std::string> OutputFilename("o", cl::init("a.out"),
47 cl::desc("Override output filename"),
48 cl::value_desc("filename"));
Reid Spencerc0af3f02004-09-13 01:27:53 +000049
Reid Spencer445564a2004-11-20 20:02:56 +000050static cl::opt<bool> Verbose("v",
51 cl::desc("Print information about actions taken"));
Reid Spencerc0af3f02004-09-13 01:27:53 +000052
Reid Spencer445564a2004-11-20 20:02:56 +000053static cl::list<std::string> LibPaths("L", cl::Prefix,
54 cl::desc("Specify a library search path"),
55 cl::value_desc("directory"));
Reid Spencerc0af3f02004-09-13 01:27:53 +000056
Reid Spencer445564a2004-11-20 20:02:56 +000057static cl::list<std::string> Libraries("l", cl::Prefix,
58 cl::desc("Specify libraries to link to"),
59 cl::value_desc("library prefix"));
Reid Spencerc0af3f02004-09-13 01:27:53 +000060
Reid Spencer445564a2004-11-20 20:02:56 +000061static cl::opt<bool> LinkAsLibrary("link-as-library",
62 cl::desc("Link the .bc files together as a library, not an executable"));
Reid Spencerc0af3f02004-09-13 01:27:53 +000063
Reid Spencer445564a2004-11-20 20:02:56 +000064static cl::alias Relink("r", cl::aliasopt(LinkAsLibrary),
65 cl::desc("Alias for -link-as-library"));
Reid Spencerc0af3f02004-09-13 01:27:53 +000066
Reid Spencer445564a2004-11-20 20:02:56 +000067static cl::opt<const TargetMachineRegistry::Entry*, false, TargetNameParser>
Reid Spenceraefd04b2004-09-25 16:00:07 +000068 MachineArch("march", cl::desc("Architecture to generate assembly for:"));
69
Reid Spencer445564a2004-11-20 20:02:56 +000070static cl::opt<bool> Native("native",
71 cl::desc("Generate a native binary instead of a shell script"));
Reid Spencerc0af3f02004-09-13 01:27:53 +000072
Reid Spencer445564a2004-11-20 20:02:56 +000073static cl::opt<bool>NativeCBE("native-cbe",
74 cl::desc("Generate a native binary with the C backend and GCC"));
75
76static cl::opt<bool>DisableCompression("disable-compression",cl::init(false),
77 cl::desc("Disable writing of compressed bytecode files"));
78
79// Compatibility options that are ignored but supported by LD
80static cl::opt<std::string> CO3("soname", cl::Hidden,
81 cl::desc("Compatibility option: ignored"));
82
83static cl::opt<std::string> CO4("version-script", cl::Hidden,
84 cl::desc("Compatibility option: ignored"));
85
86static cl::opt<bool> CO5("eh-frame-hdr", cl::Hidden,
87 cl::desc("Compatibility option: ignored"));
88
89static cl::opt<std::string> CO6("h", cl::Hidden,
90 cl::desc("Compatibility option: ignored"));
91
92/// This is just for convenience so it doesn't have to be passed around
93/// everywhere.
Reid Spencerc4064132004-12-13 03:01:14 +000094static std::string progname;
Reid Spencerc0af3f02004-09-13 01:27:53 +000095
96/// PrintAndReturn - Prints a message to standard error and returns true.
97///
98/// Inputs:
99/// progname - The name of the program (i.e. argv[0]).
100/// Message - The message to print to standard error.
101///
Reid Spencer445564a2004-11-20 20:02:56 +0000102static int PrintAndReturn(const std::string &Message) {
Reid Spencerc0af3f02004-09-13 01:27:53 +0000103 std::cerr << progname << ": " << Message << "\n";
104 return 1;
105}
106
Reid Spencer445564a2004-11-20 20:02:56 +0000107/// CopyEnv - This function takes an array of environment variables and makes a
108/// copy of it. This copy can then be manipulated any way the caller likes
109/// without affecting the process's real environment.
110///
111/// Inputs:
112/// envp - An array of C strings containing an environment.
113///
114/// Return value:
115/// NULL - An error occurred.
116///
117/// Otherwise, a pointer to a new array of C strings is returned. Every string
118/// in the array is a duplicate of the one in the original array (i.e. we do
119/// not copy the char *'s from one array to another).
120///
121static char ** CopyEnv(char ** const envp) {
122 // Count the number of entries in the old list;
123 unsigned entries; // The number of entries in the old environment list
124 for (entries = 0; envp[entries] != NULL; entries++)
125 /*empty*/;
126
127 // Add one more entry for the NULL pointer that ends the list.
128 ++entries;
129
130 // If there are no entries at all, just return NULL.
131 if (entries == 0)
132 return NULL;
133
134 // Allocate a new environment list.
135 char **newenv = new char* [entries];
136 if ((newenv = new char* [entries]) == NULL)
137 return NULL;
138
139 // Make a copy of the list. Don't forget the NULL that ends the list.
140 entries = 0;
141 while (envp[entries] != NULL) {
142 newenv[entries] = new char[strlen (envp[entries]) + 1];
143 strcpy (newenv[entries], envp[entries]);
144 ++entries;
145 }
146 newenv[entries] = NULL;
147
148 return newenv;
149}
150
151
152/// RemoveEnv - Remove the specified environment variable from the environment
153/// array.
154///
155/// Inputs:
156/// name - The name of the variable to remove. It cannot be NULL.
157/// envp - The array of environment variables. It cannot be NULL.
158///
159/// Notes:
160/// This is mainly done because functions to remove items from the environment
161/// are not available across all platforms. In particular, Solaris does not
162/// seem to have an unsetenv() function or a setenv() function (or they are
163/// undocumented if they do exist).
164///
165static void RemoveEnv(const char * name, char ** const envp) {
166 for (unsigned index=0; envp[index] != NULL; index++) {
167 // Find the first equals sign in the array and make it an EOS character.
168 char *p = strchr (envp[index], '=');
169 if (p == NULL)
170 continue;
171 else
172 *p = '\0';
173
174 // Compare the two strings. If they are equal, zap this string.
175 // Otherwise, restore it.
176 if (!strcmp(name, envp[index]))
177 *envp[index] = '\0';
178 else
179 *p = '=';
180 }
181
182 return;
183}
184
185/// GenerateBytecode - generates a bytecode file from the module provided
186void GenerateBytecode(Module* M, const std::string& FileName) {
187
188 // Create the output file.
189 std::ofstream Out(FileName.c_str());
190 if (!Out.good()) {
191 PrintAndReturn("error opening '" + FileName + "' for writing!");
192 return;
193 }
194
195 // Ensure that the bytecode file gets removed from the disk if we get a
196 // terminating signal.
197 sys::RemoveFileOnSignal(sys::Path(FileName));
198
199 // Write it out
200 WriteBytecodeToFile(M, Out, !DisableCompression);
201
202 // Close the bytecode file.
203 Out.close();
204}
205
206/// GenerateAssembly - generates a native assembly language source file from the
207/// specified bytecode file.
208///
209/// Inputs:
210/// InputFilename - The name of the output bytecode file.
211/// OutputFilename - The name of the file to generate.
212/// llc - The pathname to use for LLC.
213/// envp - The environment to use when running LLC.
214///
215/// Return non-zero value on error.
216///
217static int GenerateAssembly(const std::string &OutputFilename,
218 const std::string &InputFilename,
Reid Spencer6da1e0d2004-12-14 04:20:08 +0000219 const sys::Path &llc) {
Reid Spencer445564a2004-11-20 20:02:56 +0000220 // Run LLC to convert the bytecode file into assembly code.
Reid Spencerf6358c72004-12-19 18:00:56 +0000221 std::vector<const char*> args;
Reid Spencer6da1e0d2004-12-14 04:20:08 +0000222 args.push_back( "-f");
223 args.push_back( "-o");
Reid Spencerf6358c72004-12-19 18:00:56 +0000224 args.push_back( OutputFilename.c_str() );
225 args.push_back( InputFilename.c_str() );
Reid Spencer445564a2004-11-20 20:02:56 +0000226
Reid Spencerf6358c72004-12-19 18:00:56 +0000227 return sys::Program::ExecuteAndWait(llc,&args[0]);
Reid Spencer445564a2004-11-20 20:02:56 +0000228}
229
230/// GenerateAssembly - generates a native assembly language source file from the
231/// specified bytecode file.
232static int GenerateCFile(const std::string &OutputFile,
233 const std::string &InputFile,
Reid Spencer6da1e0d2004-12-14 04:20:08 +0000234 const sys::Path &llc) {
Reid Spencer445564a2004-11-20 20:02:56 +0000235 // Run LLC to convert the bytecode file into C.
Reid Spencerf6358c72004-12-19 18:00:56 +0000236 std::vector<const char*> args;
Reid Spencer6da1e0d2004-12-14 04:20:08 +0000237 args.push_back( "-march=c");
238 args.push_back( "-f");
239 args.push_back( "-o");
Reid Spencerf6358c72004-12-19 18:00:56 +0000240 args.push_back( OutputFile.c_str() );
241 args.push_back( InputFile.c_str() );
242 return sys::Program::ExecuteAndWait(llc, &args[0]);
Reid Spencer445564a2004-11-20 20:02:56 +0000243}
244
245/// GenerateNative - generates a native assembly language source file from the
246/// specified assembly source file.
247///
248/// Inputs:
249/// InputFilename - The name of the output bytecode file.
250/// OutputFilename - The name of the file to generate.
251/// Libraries - The list of libraries with which to link.
252/// LibPaths - The list of directories in which to find libraries.
253/// gcc - The pathname to use for GGC.
254/// envp - A copy of the process's current environment.
255///
256/// Outputs:
257/// None.
258///
259/// Returns non-zero value on error.
260///
261static int GenerateNative(const std::string &OutputFilename,
262 const std::string &InputFilename,
263 const std::vector<std::string> &Libraries,
Reid Spencer6da1e0d2004-12-14 04:20:08 +0000264 const sys::Path &gcc, char ** const envp) {
Reid Spencer445564a2004-11-20 20:02:56 +0000265 // Remove these environment variables from the environment of the
266 // programs that we will execute. It appears that GCC sets these
267 // environment variables so that the programs it uses can configure
268 // themselves identically.
269 //
270 // However, when we invoke GCC below, we want it to use its normal
271 // configuration. Hence, we must sanitize its environment.
272 char ** clean_env = CopyEnv(envp);
273 if (clean_env == NULL)
274 return 1;
275 RemoveEnv("LIBRARY_PATH", clean_env);
276 RemoveEnv("COLLECT_GCC_OPTIONS", clean_env);
277 RemoveEnv("GCC_EXEC_PREFIX", clean_env);
278 RemoveEnv("COMPILER_PATH", clean_env);
279 RemoveEnv("COLLECT_GCC", clean_env);
280
Reid Spencer445564a2004-11-20 20:02:56 +0000281
282 // Run GCC to assemble and link the program into native code.
283 //
284 // Note:
285 // We can't just assemble and link the file with the system assembler
286 // and linker because we don't know where to put the _start symbol.
287 // GCC mysteriously knows how to do it.
Reid Spencerf6358c72004-12-19 18:00:56 +0000288 std::vector<const char*> args;
Reid Spencer6da1e0d2004-12-14 04:20:08 +0000289 args.push_back("-fno-strict-aliasing");
290 args.push_back("-O3");
291 args.push_back("-o");
Reid Spencerf6358c72004-12-19 18:00:56 +0000292 args.push_back(OutputFilename.c_str());
293 args.push_back(InputFilename.c_str());
Reid Spencer445564a2004-11-20 20:02:56 +0000294
295 // Add in the libraries to link.
Reid Spencer6da1e0d2004-12-14 04:20:08 +0000296 for (unsigned index = 0; index < Libraries.size(); index++)
Reid Spencerf6358c72004-12-19 18:00:56 +0000297 if (Libraries[index] != "crtend") {
298 args.push_back("-l");
299 args.push_back(Libraries[index].c_str());
300 }
Reid Spencer445564a2004-11-20 20:02:56 +0000301
302 // Run the compiler to assembly and link together the program.
Reid Spencerf6358c72004-12-19 18:00:56 +0000303 return sys::Program::ExecuteAndWait(gcc, &args[0], (const char**)clean_env);
Reid Spencer445564a2004-11-20 20:02:56 +0000304}
305
Reid Spencerc0af3f02004-09-13 01:27:53 +0000306/// EmitShellScript - Output the wrapper file that invokes the JIT on the LLVM
307/// bytecode file for the program.
308static void EmitShellScript(char **argv) {
309#if defined(_WIN32) || defined(__CYGWIN__)
310 // Windows doesn't support #!/bin/sh style shell scripts in .exe files. To
311 // support windows systems, we copy the llvm-stub.exe executable from the
312 // build tree to the destination file.
313 std::string llvmstub = FindExecutable("llvm-stub.exe", argv[0]);
314 if (llvmstub.empty()) {
315 std::cerr << "Could not find llvm-stub.exe executable!\n";
316 exit(1);
317 }
Reid Spencer06c06db2004-12-18 06:54:21 +0000318 sys::CopyFile(OutputFilename, llvmstub);
Reid Spencerc0af3f02004-09-13 01:27:53 +0000319 return;
320#endif
321
322 // Output the script to start the program...
323 std::ofstream Out2(OutputFilename.c_str());
324 if (!Out2.good())
Reid Spencer445564a2004-11-20 20:02:56 +0000325 exit(PrintAndReturn("error opening '" + OutputFilename + "' for writing!"));
Reid Spencerc0af3f02004-09-13 01:27:53 +0000326
327 Out2 << "#!/bin/sh\n";
328 // Allow user to setenv LLVMINTERP if lli is not in their PATH.
329 Out2 << "lli=${LLVMINTERP-lli}\n";
330 Out2 << "exec $lli \\\n";
331 // gcc accepts -l<lib> and implicitly searches /lib and /usr/lib.
332 LibPaths.push_back("/lib");
333 LibPaths.push_back("/usr/lib");
334 LibPaths.push_back("/usr/X11R6/lib");
335 // We don't need to link in libc! In fact, /usr/lib/libc.so may not be a
336 // shared object at all! See RH 8: plain text.
337 std::vector<std::string>::iterator libc =
338 std::find(Libraries.begin(), Libraries.end(), "c");
339 if (libc != Libraries.end()) Libraries.erase(libc);
340 // List all the shared object (native) libraries this executable will need
341 // on the command line, so that we don't have to do this manually!
342 for (std::vector<std::string>::iterator i = Libraries.begin(),
343 e = Libraries.end(); i != e; ++i) {
Reid Spencerc4064132004-12-13 03:01:14 +0000344 sys::Path FullLibraryPath = sys::Path::FindLibrary(*i);
345 if (!FullLibraryPath.isEmpty() && FullLibraryPath.isDynamicLibrary())
346 Out2 << " -load=" << FullLibraryPath.toString() << " \\\n";
Reid Spencerc0af3f02004-09-13 01:27:53 +0000347 }
348 Out2 << " $0.bc ${1+\"$@\"}\n";
349 Out2.close();
350}
351
Reid Spencerc4064132004-12-13 03:01:14 +0000352// BuildLinkItems -- This function generates a LinkItemList for the LinkItems
353// linker function by combining the Files and Libraries in the order they were
354// declared on the command line.
355static void BuildLinkItems(
356 Linker::ItemList& Items,
357 const cl::list<std::string>& Files,
358 const cl::list<std::string>& Libraries) {
359
360 // Build the list of linkage items for LinkItems.
361
362 cl::list<std::string>::const_iterator fileIt = Files.begin();
363 cl::list<std::string>::const_iterator libIt = Libraries.begin();
364
365 int libPos = -1, filePos = -1;
Reid Spencer05f7e792004-12-13 17:18:19 +0000366 while ( libIt != Libraries.end() || fileIt != Files.end() ) {
Reid Spencerc4064132004-12-13 03:01:14 +0000367 if (libIt != Libraries.end())
368 libPos = Libraries.getPosition(libIt - Libraries.begin());
369 else
370 libPos = -1;
371 if (fileIt != Files.end())
372 filePos = Files.getPosition(fileIt - Files.begin());
373 else
374 filePos = -1;
375
376 if (filePos != -1 && (libPos == -1 || filePos < libPos)) {
377 // Add a source file
378 Items.push_back(std::make_pair(*fileIt++, false));
379 } else if (libPos != -1 && (filePos == -1 || libPos < filePos)) {
380 // Add a library
381 Items.push_back(std::make_pair(*libIt++, true));
Reid Spencerc4064132004-12-13 03:01:14 +0000382 }
383 }
384}
385
Reid Spencer445564a2004-11-20 20:02:56 +0000386// Rightly this should go in a header file but it just seems such a waste.
387namespace llvm {
388extern void Optimize(Module*);
389}
390
Reid Spencerc0af3f02004-09-13 01:27:53 +0000391int main(int argc, char **argv, char **envp) {
Reid Spencer445564a2004-11-20 20:02:56 +0000392 // Initial global variable above for convenience printing of program name.
Reid Spencerc4064132004-12-13 03:01:14 +0000393 progname = sys::Path(argv[0]).getBasename();
394 Linker TheLinker(progname, Verbose);
395
396 // Set up the library paths for the Linker
397 TheLinker.addPaths(LibPaths);
398 TheLinker.addSystemPaths();
Reid Spencer445564a2004-11-20 20:02:56 +0000399
400 // Parse the command line options
Reid Spencerc4064132004-12-13 03:01:14 +0000401 cl::ParseCommandLineOptions(argc, argv, " llvm linker\n");
Reid Spencerc0af3f02004-09-13 01:27:53 +0000402 sys::PrintStackTraceOnErrorSignal();
403
Reid Spencerc0af3f02004-09-13 01:27:53 +0000404 // Remove any consecutive duplicates of the same library...
405 Libraries.erase(std::unique(Libraries.begin(), Libraries.end()),
406 Libraries.end());
407
Reid Spencerc191d492004-12-05 19:15:29 +0000408 if (LinkAsLibrary) {
Reid Spencerc4064132004-12-13 03:01:14 +0000409 std::vector<sys::Path> Files;
410 for (unsigned i = 0; i < InputFilenames.size(); ++i )
411 Files.push_back(sys::Path(InputFilenames[i]));
412 if (TheLinker.LinkInFiles(Files))
Reid Spencerc191d492004-12-05 19:15:29 +0000413 return 1; // Error already printed
Reid Spencer6b463b22004-12-08 05:17:40 +0000414
415 // The libraries aren't linked in but are noted as "dependent" in the
416 // module.
417 for (cl::list<std::string>::const_iterator I = Libraries.begin(),
418 E = Libraries.end(); I != E ; ++I) {
Reid Spencerc4064132004-12-13 03:01:14 +0000419 TheLinker.getModule()->addLibrary(*I);
Reid Spencer6b463b22004-12-08 05:17:40 +0000420 }
Reid Spencerc191d492004-12-05 19:15:29 +0000421 } else {
422 // Build a list of the items from our command line
Reid Spencerc4064132004-12-13 03:01:14 +0000423 Linker::ItemList Items;
Reid Spencerc191d492004-12-05 19:15:29 +0000424 BuildLinkItems(Items, InputFilenames, Libraries);
425
426 // Link all the items together
Reid Spencerc4064132004-12-13 03:01:14 +0000427 if (TheLinker.LinkInItems(Items) )
428 return 1;
Reid Spencerc191d492004-12-05 19:15:29 +0000429 }
Reid Spencerc0af3f02004-09-13 01:27:53 +0000430
Reid Spencerc4064132004-12-13 03:01:14 +0000431 std::auto_ptr<Module> Composite(TheLinker.releaseModule());
432
Reid Spencer445564a2004-11-20 20:02:56 +0000433 // Optimize the module
434 Optimize(Composite.get());
435
436 // Generate the bytecode for the optimized module.
Reid Spencerc0af3f02004-09-13 01:27:53 +0000437 std::string RealBytecodeOutput = OutputFilename;
438 if (!LinkAsLibrary) RealBytecodeOutput += ".bc";
Reid Spencer445564a2004-11-20 20:02:56 +0000439 GenerateBytecode(Composite.get(), RealBytecodeOutput);
Reid Spencerc0af3f02004-09-13 01:27:53 +0000440
441 // If we are not linking a library, generate either a native executable
442 // or a JIT shell script, depending upon what the user wants.
443 if (!LinkAsLibrary) {
444 // If the user wants to generate a native executable, compile it from the
445 // bytecode file.
446 //
447 // Otherwise, create a script that will run the bytecode through the JIT.
448 if (Native) {
449 // Name of the Assembly Language output file
Reid Spencer5f767602004-12-16 23:04:20 +0000450 sys::Path AssemblyFile ( OutputFilename);
451 AssemblyFile.appendSuffix("s");
Reid Spencerc0af3f02004-09-13 01:27:53 +0000452
453 // Mark the output files for removal if we get an interrupt.
Reid Spencer5f767602004-12-16 23:04:20 +0000454 sys::RemoveFileOnSignal(AssemblyFile);
Reid Spencer227b6d02004-11-14 22:30:54 +0000455 sys::RemoveFileOnSignal(sys::Path(OutputFilename));
Reid Spencerc0af3f02004-09-13 01:27:53 +0000456
457 // Determine the locations of the llc and gcc programs.
Reid Spencer6da1e0d2004-12-14 04:20:08 +0000458 sys::Path llc = FindExecutable("llc", argv[0]);
459 if (llc.isEmpty())
Reid Spencer445564a2004-11-20 20:02:56 +0000460 return PrintAndReturn("Failed to find llc");
Reid Spencerc0af3f02004-09-13 01:27:53 +0000461
Reid Spencer6da1e0d2004-12-14 04:20:08 +0000462 sys::Path gcc = FindExecutable("gcc", argv[0]);
463 if (gcc.isEmpty())
Reid Spencer445564a2004-11-20 20:02:56 +0000464 return PrintAndReturn("Failed to find gcc");
Reid Spencerc0af3f02004-09-13 01:27:53 +0000465
466 // Generate an assembly language file for the bytecode.
467 if (Verbose) std::cout << "Generating Assembly Code\n";
Reid Spencer5f767602004-12-16 23:04:20 +0000468 GenerateAssembly(AssemblyFile.toString(), RealBytecodeOutput, llc);
Reid Spencerc0af3f02004-09-13 01:27:53 +0000469 if (Verbose) std::cout << "Generating Native Code\n";
Reid Spencer5f767602004-12-16 23:04:20 +0000470 GenerateNative(OutputFilename, AssemblyFile.toString(), Libraries,
471 gcc, envp);
Reid Spencerc0af3f02004-09-13 01:27:53 +0000472
473 // Remove the assembly language file.
Reid Spencer5f767602004-12-16 23:04:20 +0000474 AssemblyFile.destroyFile();
Reid Spencerc0af3f02004-09-13 01:27:53 +0000475 } else if (NativeCBE) {
Reid Spencer5f767602004-12-16 23:04:20 +0000476 sys::Path CFile (OutputFilename);
477 CFile.appendSuffix("cbe.c");
Reid Spencerc0af3f02004-09-13 01:27:53 +0000478
479 // Mark the output files for removal if we get an interrupt.
Reid Spencer5f767602004-12-16 23:04:20 +0000480 sys::RemoveFileOnSignal(CFile);
Reid Spencer227b6d02004-11-14 22:30:54 +0000481 sys::RemoveFileOnSignal(sys::Path(OutputFilename));
Reid Spencerc0af3f02004-09-13 01:27:53 +0000482
483 // Determine the locations of the llc and gcc programs.
Reid Spencer6da1e0d2004-12-14 04:20:08 +0000484 sys::Path llc = FindExecutable("llc", argv[0]);
485 if (llc.isEmpty())
Reid Spencer445564a2004-11-20 20:02:56 +0000486 return PrintAndReturn("Failed to find llc");
Reid Spencer93f8f552004-12-13 23:44:23 +0000487
Reid Spencer6da1e0d2004-12-14 04:20:08 +0000488 sys::Path gcc = FindExecutable("gcc", argv[0]);
489 if (gcc.isEmpty())
Reid Spencer445564a2004-11-20 20:02:56 +0000490 return PrintAndReturn("Failed to find gcc");
Reid Spencerc0af3f02004-09-13 01:27:53 +0000491
492 // Generate an assembly language file for the bytecode.
493 if (Verbose) std::cout << "Generating Assembly Code\n";
Reid Spencer5f767602004-12-16 23:04:20 +0000494 GenerateCFile(CFile.toString(), RealBytecodeOutput, llc);
Reid Spencerc0af3f02004-09-13 01:27:53 +0000495 if (Verbose) std::cout << "Generating Native Code\n";
Reid Spencer5f767602004-12-16 23:04:20 +0000496 GenerateNative(OutputFilename, CFile.toString(), Libraries, gcc, envp);
Reid Spencerc0af3f02004-09-13 01:27:53 +0000497
498 // Remove the assembly language file.
Reid Spencer5f767602004-12-16 23:04:20 +0000499 CFile.destroyFile();
Reid Spencerc0af3f02004-09-13 01:27:53 +0000500
501 } else {
502 EmitShellScript(argv);
503 }
504
505 // Make the script executable...
Reid Spencer319cdec2004-12-13 20:03:02 +0000506 sys::Path(OutputFilename).makeExecutable();
Reid Spencerc0af3f02004-09-13 01:27:53 +0000507
508 // Make the bytecode file readable and directly executable in LLEE as well
Reid Spencer319cdec2004-12-13 20:03:02 +0000509 sys::Path(RealBytecodeOutput).makeExecutable();
510 sys::Path(RealBytecodeOutput).makeReadable();
Reid Spencerc0af3f02004-09-13 01:27:53 +0000511 }
512
513 return 0;
514}