blob: ae4510008849dc67f367c442315f0477d7aeb901 [file] [log] [blame]
Chris Lattner10970eb2003-04-19 22:44:38 +00001//===- gccld.cpp - LLVM 'ld' compatible linker ----------------------------===//
Misha Brukman3da94ae2005-04-22 00:00:37 +00002//
John Criswell7c0e0222003-10-20 17:47:21 +00003// 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.
Misha Brukman3da94ae2005-04-22 00:00:37 +00007//
John Criswell7c0e0222003-10-20 17:47:21 +00008//===----------------------------------------------------------------------===//
Chris Lattnere7fca512002-01-24 19:12:12 +00009//
10// This utility is intended to be compatible with GCC, and follows standard
Chris Lattner10970eb2003-04-19 22:44:38 +000011// system 'ld' conventions. As such, the default output file is ./a.out.
Chris Lattnere7fca512002-01-24 19:12:12 +000012// 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,
Brian Gaeke69a79602003-05-23 20:27:07 +000019// I'm not too worried about this.
Chris Lattnere7fca512002-01-24 19:12:12 +000020//
21//===----------------------------------------------------------------------===//
22
Chris Lattner6e271232003-09-22 20:21:34 +000023#include "gccld.h"
Reid Spencer605b9e22004-11-14 23:00:08 +000024#include "llvm/Linker.h"
Chris Lattnere7fca512002-01-24 19:12:12 +000025#include "llvm/Module.h"
Chris Lattnerad202a02002-04-08 00:14:58 +000026#include "llvm/PassManager.h"
27#include "llvm/Bytecode/Reader.h"
28#include "llvm/Bytecode/WriteBytecodePass.h"
Chris Lattner9c3b55e2003-04-24 19:13:02 +000029#include "llvm/Target/TargetData.h"
Chris Lattnerd9d8c072002-07-23 22:04:43 +000030#include "llvm/Transforms/IPO.h"
Chris Lattnerd9d8c072002-07-23 22:04:43 +000031#include "llvm/Transforms/Scalar.h"
Reid Spencer551ccae2004-09-01 22:55:40 +000032#include "llvm/Support/CommandLine.h"
33#include "llvm/Support/FileUtilities.h"
Chris Lattnerbed85ff2004-05-27 05:41:36 +000034#include "llvm/System/Signals.h"
Reid Spencer551ccae2004-09-01 22:55:40 +000035#include "llvm/Support/SystemUtils.h"
Chris Lattnere7fca512002-01-24 19:12:12 +000036#include <fstream>
37#include <memory>
Brian Gaeked0fde302003-11-11 22:41:34 +000038using namespace llvm;
39
Chris Lattnerf3d4f172003-04-18 23:01:25 +000040namespace {
Misha Brukman3da94ae2005-04-22 00:00:37 +000041 cl::list<std::string>
Chris Lattnerf3d4f172003-04-18 23:01:25 +000042 InputFilenames(cl::Positional, cl::desc("<input bytecode files>"),
43 cl::OneOrMore);
Chris Lattner5ff62e92002-07-22 02:10:13 +000044
Misha Brukman3da94ae2005-04-22 00:00:37 +000045 cl::opt<std::string>
Chris Lattnerf3d4f172003-04-18 23:01:25 +000046 OutputFilename("o", cl::desc("Override output filename"), cl::init("a.out"),
47 cl::value_desc("filename"));
Chris Lattner5ff62e92002-07-22 02:10:13 +000048
Misha Brukman704448f2005-04-20 04:08:35 +000049 cl::opt<bool>
Chris Lattnerf3d4f172003-04-18 23:01:25 +000050 Verbose("v", cl::desc("Print information about actions taken"));
Misha Brukman704448f2005-04-20 04:08:35 +000051
Misha Brukman3da94ae2005-04-22 00:00:37 +000052 cl::list<std::string>
Chris Lattnerf3d4f172003-04-18 23:01:25 +000053 LibPaths("L", cl::desc("Specify a library search path"), cl::Prefix,
54 cl::value_desc("directory"));
Chris Lattner5ff62e92002-07-22 02:10:13 +000055
Misha Brukman3da94ae2005-04-22 00:00:37 +000056 cl::list<std::string>
Chris Lattnerf3d4f172003-04-18 23:01:25 +000057 Libraries("l", cl::desc("Specify libraries to link to"), cl::Prefix,
58 cl::value_desc("library prefix"));
Chris Lattner5ff62e92002-07-22 02:10:13 +000059
Chris Lattnerf3d4f172003-04-18 23:01:25 +000060 cl::opt<bool>
Chris Lattner3f14fb12004-12-02 21:26:10 +000061 Strip("strip-all", cl::desc("Strip all symbol info from executable"));
62 cl::opt<bool>
63 StripDebug("strip-debug",
64 cl::desc("Strip debugger symbol info from executable"));
Chris Lattner5ff62e92002-07-22 02:10:13 +000065
Chris Lattnerf3d4f172003-04-18 23:01:25 +000066 cl::opt<bool>
67 NoInternalize("disable-internalize",
68 cl::desc("Do not mark all symbols as internal"));
Chris Lattnerfe32bf52003-11-05 06:05:21 +000069 cl::alias
Chris Lattnera2b2dc92003-08-22 19:18:45 +000070 ExportDynamic("export-dynamic", cl::desc("Alias for -disable-internalize"),
71 cl::aliasopt(NoInternalize));
Chris Lattnera856db22003-04-18 23:38:22 +000072
Chris Lattner10970eb2003-04-19 22:44:38 +000073 cl::opt<bool>
74 LinkAsLibrary("link-as-library", cl::desc("Link the .bc files together as a"
75 " library, not an executable"));
Chris Lattnerfe32bf52003-11-05 06:05:21 +000076 cl::alias
77 Relink("r", cl::desc("Alias for -link-as-library"),
78 cl::aliasopt(LinkAsLibrary));
Chris Lattner10970eb2003-04-19 22:44:38 +000079
Misha Brukman704448f2005-04-20 04:08:35 +000080 cl::opt<bool>
Chris Lattnercb834652005-11-17 16:08:04 +000081 Native("native", cl::ZeroOrMore,
Chris Lattner6e271232003-09-22 20:21:34 +000082 cl::desc("Generate a native binary instead of a shell script"));
Misha Brukman704448f2005-04-20 04:08:35 +000083 cl::opt<bool>
Chris Lattnercb834652005-11-17 16:08:04 +000084 NativeCBE("native-cbe", cl::ZeroOrMore,
Chris Lattner69e8d282004-04-06 16:43:13 +000085 cl::desc("Generate a native binary with the C backend and GCC"));
Misha Brukmanb0bafc52005-04-20 03:22:18 +000086
Misha Brukman704448f2005-04-20 04:08:35 +000087 cl::opt<bool>
Misha Brukmanb0bafc52005-04-20 03:22:18 +000088 SaveTemps("save-temps",
89 cl::desc("Do not delete temporary files"));
Misha Brukman704448f2005-04-20 04:08:35 +000090
Reid Spencer156aa352005-12-22 01:50:56 +000091 cl::list<std::string>
Reid Spencer837149c2005-02-28 08:45:35 +000092 RPath("rpath",
93 cl::desc("Set runtime shared library search path (requires -native or"
Misha Brukman3da94ae2005-04-22 00:00:37 +000094 " -native-cbe)"),
Reid Spencer837149c2005-02-28 08:45:35 +000095 cl::Prefix, cl::value_desc("directory"));
Misha Brukman704448f2005-04-20 04:08:35 +000096
Reid Spencer837149c2005-02-28 08:45:35 +000097 cl::opt<std::string>
98 SOName("soname",
99 cl::desc("Set internal name of shared library (requires -native or"
Misha Brukman3da94ae2005-04-22 00:00:37 +0000100 " -native-cbe)"),
Reid Spencer837149c2005-02-28 08:45:35 +0000101 cl::Prefix, cl::value_desc("name"));
Misha Brukman704448f2005-04-20 04:08:35 +0000102
John Criswelldc0de4f2003-09-18 16:22:26 +0000103 // Compatibility options that are ignored but supported by LD
Chris Lattnera856db22003-04-18 23:38:22 +0000104 cl::opt<std::string>
Chris Lattnera856db22003-04-18 23:38:22 +0000105 CO4("version-script", cl::Hidden, cl::desc("Compatibility option: ignored"));
106 cl::opt<bool>
107 CO5("eh-frame-hdr", cl::Hidden, cl::desc("Compatibility option: ignored"));
John Criswellaa2a47d2003-12-09 15:39:11 +0000108 cl::opt<std::string>
109 CO6("h", cl::Hidden, cl::desc("Compatibility option: ignored"));
Reid Spencer156aa352005-12-22 01:50:56 +0000110 cl::opt<bool>
111 CO7("start-group", cl::Hidden, cl::desc("Compatibility option: ignored"));
112 cl::opt<bool>
113 CO8("end-group", cl::Hidden, cl::desc("Compatibility option: ignored"));
114
Chris Lattner3f14fb12004-12-02 21:26:10 +0000115 cl::alias A0("s", cl::desc("Alias for --strip-all"),
116 cl::aliasopt(Strip));
117 cl::alias A1("S", cl::desc("Alias for --strip-debug"),
118 cl::aliasopt(StripDebug));
Reid Spencerc4064132004-12-13 03:01:14 +0000119
Chris Lattnerf3d4f172003-04-18 23:01:25 +0000120}
Chris Lattnere7fca512002-01-24 19:12:12 +0000121
Chris Lattner0ebee742004-06-02 00:22:24 +0000122/// PrintAndReturn - Prints a message to standard error and returns true.
Misha Brukman98399692003-11-20 06:21:54 +0000123///
124/// Inputs:
125/// progname - The name of the program (i.e. argv[0]).
126/// Message - The message to print to standard error.
Misha Brukman98399692003-11-20 06:21:54 +0000127///
Chris Lattner0ebee742004-06-02 00:22:24 +0000128static int PrintAndReturn(const char *progname, const std::string &Message) {
129 std::cerr << progname << ": " << Message << "\n";
John Criswell71478b72003-09-19 20:24:23 +0000130 return 1;
Chris Lattner10970eb2003-04-19 22:44:38 +0000131}
132
Chris Lattner7e88d412004-06-02 00:10:19 +0000133/// EmitShellScript - Output the wrapper file that invokes the JIT on the LLVM
134/// bytecode file for the program.
135static void EmitShellScript(char **argv) {
Chris Lattner681692d2004-06-02 00:53:57 +0000136#if defined(_WIN32) || defined(__CYGWIN__)
137 // Windows doesn't support #!/bin/sh style shell scripts in .exe files. To
138 // support windows systems, we copy the llvm-stub.exe executable from the
139 // build tree to the destination file.
Reid Spencer93f8f552004-12-13 23:44:23 +0000140 std::string llvmstub = FindExecutable("llvm-stub.exe", argv[0]).toString();
Chris Lattner681692d2004-06-02 00:53:57 +0000141 if (llvmstub.empty()) {
142 std::cerr << "Could not find llvm-stub.exe executable!\n";
143 exit(1);
144 }
Reid Spencer682084a2004-12-21 03:24:02 +0000145 sys::CopyFile(sys::Path(OutputFilename), sys::Path(llvmstub));
Chris Lattner681692d2004-06-02 00:53:57 +0000146 return;
147#endif
148
Chris Lattner7e88d412004-06-02 00:10:19 +0000149 // Output the script to start the program...
150 std::ofstream Out2(OutputFilename.c_str());
151 if (!Out2.good())
152 exit(PrintAndReturn(argv[0], "error opening '" + OutputFilename +
153 "' for writing!"));
154
155 Out2 << "#!/bin/sh\n";
156 // Allow user to setenv LLVMINTERP if lli is not in their PATH.
157 Out2 << "lli=${LLVMINTERP-lli}\n";
158 Out2 << "exec $lli \\\n";
Reid Spencerc4064132004-12-13 03:01:14 +0000159
Chris Lattner7e88d412004-06-02 00:10:19 +0000160 // We don't need to link in libc! In fact, /usr/lib/libc.so may not be a
161 // shared object at all! See RH 8: plain text.
Misha Brukman3da94ae2005-04-22 00:00:37 +0000162 std::vector<std::string>::iterator libc =
Chris Lattner7e88d412004-06-02 00:10:19 +0000163 std::find(Libraries.begin(), Libraries.end(), "c");
164 if (libc != Libraries.end()) Libraries.erase(libc);
165 // List all the shared object (native) libraries this executable will need
166 // on the command line, so that we don't have to do this manually!
Misha Brukman3da94ae2005-04-22 00:00:37 +0000167 for (std::vector<std::string>::iterator i = Libraries.begin(),
Chris Lattner7e88d412004-06-02 00:10:19 +0000168 e = Libraries.end(); i != e; ++i) {
Reid Spencerc4064132004-12-13 03:01:14 +0000169 sys::Path FullLibraryPath = sys::Path::FindLibrary(*i);
170 if (!FullLibraryPath.isEmpty() && FullLibraryPath.isDynamicLibrary())
171 Out2 << " -load=" << FullLibraryPath.toString() << " \\\n";
Chris Lattner7e88d412004-06-02 00:10:19 +0000172 }
173 Out2 << " $0.bc ${1+\"$@\"}\n";
174 Out2.close();
175}
Chris Lattner10970eb2003-04-19 22:44:38 +0000176
Reid Spencerc4064132004-12-13 03:01:14 +0000177// BuildLinkItems -- This function generates a LinkItemList for the LinkItems
178// linker function by combining the Files and Libraries in the order they were
179// declared on the command line.
180static void BuildLinkItems(
181 Linker::ItemList& Items,
182 const cl::list<std::string>& Files,
183 const cl::list<std::string>& Libraries) {
184
Misha Brukman3da94ae2005-04-22 00:00:37 +0000185 // Build the list of linkage items for LinkItems.
Reid Spencerc4064132004-12-13 03:01:14 +0000186
187 cl::list<std::string>::const_iterator fileIt = Files.begin();
188 cl::list<std::string>::const_iterator libIt = Libraries.begin();
189
190 int libPos = -1, filePos = -1;
Reid Spencer05f7e792004-12-13 17:18:19 +0000191 while ( libIt != Libraries.end() || fileIt != Files.end() ) {
Reid Spencerc4064132004-12-13 03:01:14 +0000192 if (libIt != Libraries.end())
193 libPos = Libraries.getPosition(libIt - Libraries.begin());
194 else
195 libPos = -1;
196 if (fileIt != Files.end())
197 filePos = Files.getPosition(fileIt - Files.begin());
198 else
199 filePos = -1;
200
201 if (filePos != -1 && (libPos == -1 || filePos < libPos)) {
202 // Add a source file
203 Items.push_back(std::make_pair(*fileIt++, false));
204 } else if (libPos != -1 && (filePos == -1 || libPos < filePos)) {
205 // Add a library
206 Items.push_back(std::make_pair(*libIt++, true));
Reid Spencerc4064132004-12-13 03:01:14 +0000207 }
208 }
209}
Reid Spencer05f7e792004-12-13 17:18:19 +0000210
Reid Spencer93f8f552004-12-13 23:44:23 +0000211int main(int argc, char **argv, char **envp ) {
Chris Lattner5ff62e92002-07-22 02:10:13 +0000212 cl::ParseCommandLineOptions(argc, argv, " llvm linker for GCC\n");
Reid Spencer9de7b332004-08-29 19:28:55 +0000213 sys::PrintStackTraceOnErrorSignal();
Chris Lattnere7fca512002-01-24 19:12:12 +0000214
Reid Spencer4c1af902004-11-14 22:17:03 +0000215 int exitCode = 0;
Chris Lattnere7fca512002-01-24 19:12:12 +0000216
Reid Spencerc4064132004-12-13 03:01:14 +0000217 std::string ProgName = sys::Path(argv[0]).getBasename();
Reid Spencer328ead92005-12-13 20:00:37 +0000218 Linker TheLinker(ProgName, OutputFilename, Verbose);
Reid Spencerc4064132004-12-13 03:01:14 +0000219
Reid Spencer4c1af902004-11-14 22:17:03 +0000220 try {
Reid Spencer4c1af902004-11-14 22:17:03 +0000221 // Remove any consecutive duplicates of the same library...
222 Libraries.erase(std::unique(Libraries.begin(), Libraries.end()),
223 Libraries.end());
Chris Lattnerfd35c642003-11-03 17:27:17 +0000224
Reid Spencerc4064132004-12-13 03:01:14 +0000225 TheLinker.addPaths(LibPaths);
226 TheLinker.addSystemPaths();
John Criswell71478b72003-09-19 20:24:23 +0000227
Reid Spencerc191d492004-12-05 19:15:29 +0000228 if (LinkAsLibrary) {
Reid Spencerc4064132004-12-13 03:01:14 +0000229 std::vector<sys::Path> Files;
230 for (unsigned i = 0; i < InputFilenames.size(); ++i )
231 Files.push_back(sys::Path(InputFilenames[i]));
232
233 if (TheLinker.LinkInFiles(Files))
234 return 1; // Error already printed by linker
235
Reid Spencer6b463b22004-12-08 05:17:40 +0000236 // The libraries aren't linked in but are noted as "dependent" in the
237 // module.
Misha Brukman3da94ae2005-04-22 00:00:37 +0000238 for (cl::list<std::string>::const_iterator I = Libraries.begin(),
Reid Spencer6b463b22004-12-08 05:17:40 +0000239 E = Libraries.end(); I != E ; ++I) {
Reid Spencerc4064132004-12-13 03:01:14 +0000240 TheLinker.getModule()->addLibrary(*I);
Reid Spencer6b463b22004-12-08 05:17:40 +0000241 }
242
Reid Spencerc191d492004-12-05 19:15:29 +0000243 } else {
244 // Build a list of the items from our command line
Reid Spencerc4064132004-12-13 03:01:14 +0000245 Linker::ItemList Items;
Reid Spencerc191d492004-12-05 19:15:29 +0000246 BuildLinkItems(Items, InputFilenames, Libraries);
Chris Lattnere7fca512002-01-24 19:12:12 +0000247
Reid Spencerc191d492004-12-05 19:15:29 +0000248 // Link all the items together
Reid Spencerc4064132004-12-13 03:01:14 +0000249 if (TheLinker.LinkInItems(Items))
Reid Spencerc191d492004-12-05 19:15:29 +0000250 return 1; // Error already printed
251 }
Chris Lattnere7fca512002-01-24 19:12:12 +0000252
Reid Spencerc4064132004-12-13 03:01:14 +0000253 // We're done with the Linker, so tell it to release its module
254 std::auto_ptr<Module> Composite(TheLinker.releaseModule());
255
Reid Spencer4c1af902004-11-14 22:17:03 +0000256 // Create the output file.
257 std::string RealBytecodeOutput = OutputFilename;
Reid Spencer837149c2005-02-28 08:45:35 +0000258 if (!LinkAsLibrary || Native || NativeCBE) RealBytecodeOutput += ".bc";
Jeff Cohen5fb6ed42005-01-22 17:36:17 +0000259 std::ios::openmode io_mode = std::ios::out | std::ios::trunc |
260 std::ios::binary;
261 std::ofstream Out(RealBytecodeOutput.c_str(), io_mode);
Reid Spencer4c1af902004-11-14 22:17:03 +0000262 if (!Out.good())
263 return PrintAndReturn(argv[0], "error opening '" + RealBytecodeOutput +
264 "' for writing!");
Chris Lattner76d12292002-04-18 19:55:25 +0000265
Reid Spencer4c1af902004-11-14 22:17:03 +0000266 // Ensure that the bytecode file gets removed from the disk if we get a
267 // SIGINT signal.
268 sys::RemoveFileOnSignal(sys::Path(RealBytecodeOutput));
John Criswelldc0de4f2003-09-18 16:22:26 +0000269
Chris Lattnerbcfe1e22004-12-12 07:53:51 +0000270 // Strip everything if Strip is set, otherwise if stripdebug is set, just
271 // strip debug info.
Chris Lattner3f14fb12004-12-02 21:26:10 +0000272 int StripLevel = Strip ? 2 : (StripDebug ? 1 : 0);
Chris Lattnerbcfe1e22004-12-12 07:53:51 +0000273
Misha Brukman3da94ae2005-04-22 00:00:37 +0000274 // Internalize the module if neither -disable-internalize nor
Chris Lattnerbcfe1e22004-12-12 07:53:51 +0000275 // -link-as-library are passed in.
276 bool ShouldInternalize = !NoInternalize & !LinkAsLibrary;
277
278 // Generate the bytecode file.
279 if (GenerateBytecode(Composite.get(), StripLevel, ShouldInternalize, &Out)){
Reid Spencer4c1af902004-11-14 22:17:03 +0000280 Out.close();
281 return PrintAndReturn(argv[0], "error generating bytecode");
John Criswelldabaf7d2003-09-16 21:27:35 +0000282 }
Misha Brukmanc1fdca82003-08-20 20:38:15 +0000283
Reid Spencer4c1af902004-11-14 22:17:03 +0000284 // Close the bytecode file.
285 Out.close();
286
Reid Spencer837149c2005-02-28 08:45:35 +0000287 // Generate either a native file or a JIT shell script. If the user wants
Misha Brukman3da94ae2005-04-22 00:00:37 +0000288 // to generate a native file, compile it from the bytecode file. Otherwise,
289 // if the target is not a library, create a script that will run the
Reid Spencer837149c2005-02-28 08:45:35 +0000290 // bytecode through the JIT.
291 if (Native) {
292 // Name of the Assembly Language output file
293 sys::Path AssemblyFile (OutputFilename);
294 AssemblyFile.appendSuffix("s");
Reid Spencer4c1af902004-11-14 22:17:03 +0000295
Reid Spencer837149c2005-02-28 08:45:35 +0000296 // Mark the output files for removal if we get an interrupt.
297 sys::RemoveFileOnSignal(AssemblyFile);
298 sys::RemoveFileOnSignal(sys::Path(OutputFilename));
Reid Spencer4c1af902004-11-14 22:17:03 +0000299
Reid Spencer837149c2005-02-28 08:45:35 +0000300 // Determine the locations of the llc and gcc programs.
301 sys::Path llc = FindExecutable("llc", argv[0]);
302 if (llc.isEmpty())
303 return PrintAndReturn(argv[0], "Failed to find llc");
Reid Spencer4c1af902004-11-14 22:17:03 +0000304
Reid Spencer837149c2005-02-28 08:45:35 +0000305 sys::Path gcc = FindExecutable("gcc", argv[0]);
306 if (gcc.isEmpty())
307 return PrintAndReturn(argv[0], "Failed to find gcc");
Reid Spencer4c1af902004-11-14 22:17:03 +0000308
Reid Spencer837149c2005-02-28 08:45:35 +0000309 // Generate an assembly language file for the bytecode.
310 if (Verbose) std::cout << "Generating Assembly Code\n";
Misha Brukman3da94ae2005-04-22 00:00:37 +0000311 GenerateAssembly(AssemblyFile.toString(), RealBytecodeOutput, llc,
Misha Brukmanb0bafc52005-04-20 03:22:18 +0000312 Verbose);
Reid Spencer837149c2005-02-28 08:45:35 +0000313 if (Verbose) std::cout << "Generating Native Code\n";
Misha Brukman3da94ae2005-04-22 00:00:37 +0000314 GenerateNative(OutputFilename, AssemblyFile.toString(),
Chris Lattner1d924f62005-08-02 22:07:38 +0000315 LibPaths, Libraries, gcc, envp, LinkAsLibrary,
316 NoInternalize, RPath, SOName, Verbose);
Reid Spencer4c1af902004-11-14 22:17:03 +0000317
Misha Brukmanb0bafc52005-04-20 03:22:18 +0000318 if (!SaveTemps) {
319 // Remove the assembly language file.
Reid Spencera229c5c2005-07-08 03:08:58 +0000320 AssemblyFile.eraseFromDisk();
Misha Brukmanb0bafc52005-04-20 03:22:18 +0000321 // Remove the bytecode language file.
Reid Spencera229c5c2005-07-08 03:08:58 +0000322 sys::Path(RealBytecodeOutput).eraseFromDisk();
Misha Brukmanb0bafc52005-04-20 03:22:18 +0000323 }
Misha Brukman704448f2005-04-20 04:08:35 +0000324
Reid Spencer837149c2005-02-28 08:45:35 +0000325 } else if (NativeCBE) {
326 sys::Path CFile (OutputFilename);
327 CFile.appendSuffix("cbe.c");
Reid Spencer4c1af902004-11-14 22:17:03 +0000328
Reid Spencer837149c2005-02-28 08:45:35 +0000329 // Mark the output files for removal if we get an interrupt.
330 sys::RemoveFileOnSignal(CFile);
331 sys::RemoveFileOnSignal(sys::Path(OutputFilename));
332
333 // Determine the locations of the llc and gcc programs.
334 sys::Path llc = FindExecutable("llc", argv[0]);
335 if (llc.isEmpty())
336 return PrintAndReturn(argv[0], "Failed to find llc");
337
338 sys::Path gcc = FindExecutable("gcc", argv[0]);
339 if (gcc.isEmpty())
340 return PrintAndReturn(argv[0], "Failed to find gcc");
341
342 // Generate an assembly language file for the bytecode.
Misha Brukmanb0bafc52005-04-20 03:22:18 +0000343 if (Verbose) std::cout << "Generating C Source Code\n";
344 GenerateCFile(CFile.toString(), RealBytecodeOutput, llc, Verbose);
Reid Spencer837149c2005-02-28 08:45:35 +0000345 if (Verbose) std::cout << "Generating Native Code\n";
346 GenerateNative(OutputFilename, CFile.toString(),
Chris Lattner1d924f62005-08-02 22:07:38 +0000347 LibPaths, Libraries, gcc, envp, LinkAsLibrary,
348 NoInternalize, RPath, SOName, Verbose);
Reid Spencer837149c2005-02-28 08:45:35 +0000349
Misha Brukmanb0bafc52005-04-20 03:22:18 +0000350 if (!SaveTemps) {
351 // Remove the assembly language file.
Reid Spencera229c5c2005-07-08 03:08:58 +0000352 CFile.eraseFromDisk();
Misha Brukmanb0bafc52005-04-20 03:22:18 +0000353 // Remove the bytecode language file.
Reid Spencera229c5c2005-07-08 03:08:58 +0000354 sys::Path(RealBytecodeOutput).eraseFromDisk();
Misha Brukmanb0bafc52005-04-20 03:22:18 +0000355 }
Reid Spencer837149c2005-02-28 08:45:35 +0000356
357 } else if (!LinkAsLibrary) {
358 EmitShellScript(argv);
Misha Brukman704448f2005-04-20 04:08:35 +0000359
Reid Spencer837149c2005-02-28 08:45:35 +0000360 // Make the bytecode file readable and directly executable in LLEE
Reid Spencera229c5c2005-07-08 03:08:58 +0000361 sys::Path(RealBytecodeOutput).makeExecutableOnDisk();
362 sys::Path(RealBytecodeOutput).makeReadableOnDisk();
Reid Spencer4c1af902004-11-14 22:17:03 +0000363 }
Reid Spencer837149c2005-02-28 08:45:35 +0000364
365 // Make the output, whether native or script, executable as well...
Reid Spencera229c5c2005-07-08 03:08:58 +0000366 sys::Path(OutputFilename).makeExecutableOnDisk();
Misha Brukman704448f2005-04-20 04:08:35 +0000367
Reid Spencer4c1af902004-11-14 22:17:03 +0000368 } catch (const char*msg) {
369 std::cerr << argv[0] << ": " << msg << "\n";
370 exitCode = 1;
371 } catch (const std::string& msg) {
372 std::cerr << argv[0] << ": " << msg << "\n";
373 exitCode = 2;
374 } catch (...) {
375 // This really shouldn't happen, but just in case ....
Reid Spencerc4064132004-12-13 03:01:14 +0000376 std::cerr << argv[0] << ": An unexpected unknown exception occurred.\n";
Reid Spencer4c1af902004-11-14 22:17:03 +0000377 exitCode = 3;
Chris Lattner10970eb2003-04-19 22:44:38 +0000378 }
Chris Lattnere7fca512002-01-24 19:12:12 +0000379
Reid Spencer4c1af902004-11-14 22:17:03 +0000380 return exitCode;
Chris Lattnere7fca512002-01-24 19:12:12 +0000381}