blob: 46ffac1122996fa26bf684a2c138d859566f10d5 [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"
Chris Lattner9c3b55e2003-04-24 19:13:02 +000028#include "llvm/Target/TargetData.h"
Chris Lattnerd9d8c072002-07-23 22:04:43 +000029#include "llvm/Transforms/IPO.h"
Chris Lattnerd9d8c072002-07-23 22:04:43 +000030#include "llvm/Transforms/Scalar.h"
Reid Spencer551ccae2004-09-01 22:55:40 +000031#include "llvm/Support/CommandLine.h"
32#include "llvm/Support/FileUtilities.h"
Chris Lattnerc30598b2006-12-06 01:18:01 +000033#include "llvm/Support/ManagedStatic.h"
Bill Wendling68fe61d2006-11-29 00:19:40 +000034#include "llvm/Support/Streams.h"
Chris Lattnerbed85ff2004-05-27 05:41:36 +000035#include "llvm/System/Signals.h"
Reid Spencer551ccae2004-09-01 22:55:40 +000036#include "llvm/Support/SystemUtils.h"
Chris Lattnere7fca512002-01-24 19:12:12 +000037#include <fstream>
38#include <memory>
Brian Gaeked0fde302003-11-11 22:41:34 +000039using namespace llvm;
40
Chris Lattnerf3d4f172003-04-18 23:01:25 +000041namespace {
Misha Brukman3da94ae2005-04-22 00:00:37 +000042 cl::list<std::string>
Chris Lattnerf3d4f172003-04-18 23:01:25 +000043 InputFilenames(cl::Positional, cl::desc("<input bytecode files>"),
44 cl::OneOrMore);
Chris Lattner5ff62e92002-07-22 02:10:13 +000045
Misha Brukman3da94ae2005-04-22 00:00:37 +000046 cl::opt<std::string>
Chris Lattnerf3d4f172003-04-18 23:01:25 +000047 OutputFilename("o", cl::desc("Override output filename"), cl::init("a.out"),
48 cl::value_desc("filename"));
Chris Lattner5ff62e92002-07-22 02:10:13 +000049
Misha Brukman704448f2005-04-20 04:08:35 +000050 cl::opt<bool>
Chris Lattnerf3d4f172003-04-18 23:01:25 +000051 Verbose("v", cl::desc("Print information about actions taken"));
Misha Brukman704448f2005-04-20 04:08:35 +000052
Misha Brukman3da94ae2005-04-22 00:00:37 +000053 cl::list<std::string>
Chris Lattnerf3d4f172003-04-18 23:01:25 +000054 LibPaths("L", cl::desc("Specify a library search path"), cl::Prefix,
55 cl::value_desc("directory"));
Chris Lattner5ff62e92002-07-22 02:10:13 +000056
Misha Brukman3da94ae2005-04-22 00:00:37 +000057 cl::list<std::string>
Chris Lattnerf3d4f172003-04-18 23:01:25 +000058 Libraries("l", cl::desc("Specify libraries to link to"), cl::Prefix,
59 cl::value_desc("library prefix"));
Chris Lattner5ff62e92002-07-22 02:10:13 +000060
Chris Lattnerf3d4f172003-04-18 23:01:25 +000061 cl::opt<bool>
Chris Lattner3f14fb12004-12-02 21:26:10 +000062 Strip("strip-all", cl::desc("Strip all symbol info from executable"));
63 cl::opt<bool>
64 StripDebug("strip-debug",
65 cl::desc("Strip debugger symbol info from executable"));
Chris Lattner5ff62e92002-07-22 02:10:13 +000066
Chris Lattnerf3d4f172003-04-18 23:01:25 +000067 cl::opt<bool>
68 NoInternalize("disable-internalize",
69 cl::desc("Do not mark all symbols as internal"));
Chris Lattnerfe32bf52003-11-05 06:05:21 +000070 cl::alias
Chris Lattnera2b2dc92003-08-22 19:18:45 +000071 ExportDynamic("export-dynamic", cl::desc("Alias for -disable-internalize"),
72 cl::aliasopt(NoInternalize));
Chris Lattnera856db22003-04-18 23:38:22 +000073
Chris Lattner10970eb2003-04-19 22:44:38 +000074 cl::opt<bool>
75 LinkAsLibrary("link-as-library", cl::desc("Link the .bc files together as a"
76 " library, not an executable"));
Chris Lattnerfe32bf52003-11-05 06:05:21 +000077 cl::alias
78 Relink("r", cl::desc("Alias for -link-as-library"),
79 cl::aliasopt(LinkAsLibrary));
Chris Lattner10970eb2003-04-19 22:44:38 +000080
Misha Brukman704448f2005-04-20 04:08:35 +000081 cl::opt<bool>
Chris Lattnercb834652005-11-17 16:08:04 +000082 Native("native", cl::ZeroOrMore,
Chris Lattner6e271232003-09-22 20:21:34 +000083 cl::desc("Generate a native binary instead of a shell script"));
Misha Brukman704448f2005-04-20 04:08:35 +000084 cl::opt<bool>
Chris Lattnercb834652005-11-17 16:08:04 +000085 NativeCBE("native-cbe", cl::ZeroOrMore,
Chris Lattner69e8d282004-04-06 16:43:13 +000086 cl::desc("Generate a native binary with the C backend and GCC"));
Misha Brukmanb0bafc52005-04-20 03:22:18 +000087
Misha Brukman704448f2005-04-20 04:08:35 +000088 cl::opt<bool>
Misha Brukmanb0bafc52005-04-20 03:22:18 +000089 SaveTemps("save-temps",
90 cl::desc("Do not delete temporary files"));
Misha Brukman704448f2005-04-20 04:08:35 +000091
Reid Spencer156aa352005-12-22 01:50:56 +000092 cl::list<std::string>
Reid Spencer837149c2005-02-28 08:45:35 +000093 RPath("rpath",
94 cl::desc("Set runtime shared library search path (requires -native or"
Misha Brukman3da94ae2005-04-22 00:00:37 +000095 " -native-cbe)"),
Reid Spencer837149c2005-02-28 08:45:35 +000096 cl::Prefix, cl::value_desc("directory"));
Misha Brukman704448f2005-04-20 04:08:35 +000097
Reid Spencer837149c2005-02-28 08:45:35 +000098 cl::opt<std::string>
99 SOName("soname",
100 cl::desc("Set internal name of shared library (requires -native or"
Misha Brukman3da94ae2005-04-22 00:00:37 +0000101 " -native-cbe)"),
Reid Spencer837149c2005-02-28 08:45:35 +0000102 cl::Prefix, cl::value_desc("name"));
Misha Brukman704448f2005-04-20 04:08:35 +0000103
John Criswelldc0de4f2003-09-18 16:22:26 +0000104 // Compatibility options that are ignored but supported by LD
Chris Lattnera856db22003-04-18 23:38:22 +0000105 cl::opt<std::string>
Chris Lattnera856db22003-04-18 23:38:22 +0000106 CO4("version-script", cl::Hidden, cl::desc("Compatibility option: ignored"));
107 cl::opt<bool>
108 CO5("eh-frame-hdr", cl::Hidden, cl::desc("Compatibility option: ignored"));
John Criswellaa2a47d2003-12-09 15:39:11 +0000109 cl::opt<std::string>
110 CO6("h", cl::Hidden, cl::desc("Compatibility option: ignored"));
Reid Spencer156aa352005-12-22 01:50:56 +0000111 cl::opt<bool>
112 CO7("start-group", cl::Hidden, cl::desc("Compatibility option: ignored"));
113 cl::opt<bool>
114 CO8("end-group", cl::Hidden, cl::desc("Compatibility option: ignored"));
115
Chris Lattner3f14fb12004-12-02 21:26:10 +0000116 cl::alias A0("s", cl::desc("Alias for --strip-all"),
117 cl::aliasopt(Strip));
118 cl::alias A1("S", cl::desc("Alias for --strip-debug"),
119 cl::aliasopt(StripDebug));
Reid Spencerc4064132004-12-13 03:01:14 +0000120
Chris Lattnerf3d4f172003-04-18 23:01:25 +0000121}
Chris Lattnere7fca512002-01-24 19:12:12 +0000122
Chris Lattner0ebee742004-06-02 00:22:24 +0000123/// PrintAndReturn - Prints a message to standard error and returns true.
Misha Brukman98399692003-11-20 06:21:54 +0000124///
125/// Inputs:
126/// progname - The name of the program (i.e. argv[0]).
127/// Message - The message to print to standard error.
Misha Brukman98399692003-11-20 06:21:54 +0000128///
Chris Lattner0ebee742004-06-02 00:22:24 +0000129static int PrintAndReturn(const char *progname, const std::string &Message) {
Bill Wendling68fe61d2006-11-29 00:19:40 +0000130 llvm_cerr << progname << ": " << Message << "\n";
John Criswell71478b72003-09-19 20:24:23 +0000131 return 1;
Chris Lattner10970eb2003-04-19 22:44:38 +0000132}
133
Chris Lattner7e88d412004-06-02 00:10:19 +0000134/// EmitShellScript - Output the wrapper file that invokes the JIT on the LLVM
135/// bytecode file for the program.
136static void EmitShellScript(char **argv) {
Anton Korobeynikov7d515442006-09-01 20:35:17 +0000137#if defined(_WIN32) || defined(__CYGWIN__)
Chris Lattner681692d2004-06-02 00:53:57 +0000138 // Windows doesn't support #!/bin/sh style shell scripts in .exe files. To
139 // support windows systems, we copy the llvm-stub.exe executable from the
140 // build tree to the destination file.
Anton Korobeynikov7d515442006-09-01 20:35:17 +0000141 std::string ErrMsg;
142 sys::Path llvmstub = FindExecutable("llvm-stub.exe", argv[0]);
143 if (llvmstub.isEmpty()) {
Bill Wendling68fe61d2006-11-29 00:19:40 +0000144 llvm_cerr << "Could not find llvm-stub.exe executable!\n";
Chris Lattner681692d2004-06-02 00:53:57 +0000145 exit(1);
146 }
Anton Korobeynikov7d515442006-09-01 20:35:17 +0000147 if (0 != sys::CopyFile(sys::Path(OutputFilename), llvmstub, &ErrMsg)) {
Bill Wendling68fe61d2006-11-29 00:19:40 +0000148 llvm_cerr << argv[0] << ": " << ErrMsg << "\n";
Anton Korobeynikov7d515442006-09-01 20:35:17 +0000149 exit(1);
150 }
151
152 return;
Chris Lattner681692d2004-06-02 00:53:57 +0000153#endif
154
Chris Lattner7e88d412004-06-02 00:10:19 +0000155 // Output the script to start the program...
156 std::ofstream Out2(OutputFilename.c_str());
157 if (!Out2.good())
158 exit(PrintAndReturn(argv[0], "error opening '" + OutputFilename +
159 "' for writing!"));
160
161 Out2 << "#!/bin/sh\n";
162 // Allow user to setenv LLVMINTERP if lli is not in their PATH.
163 Out2 << "lli=${LLVMINTERP-lli}\n";
164 Out2 << "exec $lli \\\n";
Reid Spencerc4064132004-12-13 03:01:14 +0000165
Chris Lattner7e88d412004-06-02 00:10:19 +0000166 // We don't need to link in libc! In fact, /usr/lib/libc.so may not be a
167 // shared object at all! See RH 8: plain text.
Misha Brukman3da94ae2005-04-22 00:00:37 +0000168 std::vector<std::string>::iterator libc =
Chris Lattner7e88d412004-06-02 00:10:19 +0000169 std::find(Libraries.begin(), Libraries.end(), "c");
170 if (libc != Libraries.end()) Libraries.erase(libc);
171 // List all the shared object (native) libraries this executable will need
172 // on the command line, so that we don't have to do this manually!
Misha Brukman3da94ae2005-04-22 00:00:37 +0000173 for (std::vector<std::string>::iterator i = Libraries.begin(),
Chris Lattner7e88d412004-06-02 00:10:19 +0000174 e = Libraries.end(); i != e; ++i) {
Reid Spencerc4064132004-12-13 03:01:14 +0000175 sys::Path FullLibraryPath = sys::Path::FindLibrary(*i);
176 if (!FullLibraryPath.isEmpty() && FullLibraryPath.isDynamicLibrary())
177 Out2 << " -load=" << FullLibraryPath.toString() << " \\\n";
Chris Lattner7e88d412004-06-02 00:10:19 +0000178 }
179 Out2 << " $0.bc ${1+\"$@\"}\n";
180 Out2.close();
181}
Chris Lattner10970eb2003-04-19 22:44:38 +0000182
Reid Spencerc4064132004-12-13 03:01:14 +0000183// BuildLinkItems -- This function generates a LinkItemList for the LinkItems
184// linker function by combining the Files and Libraries in the order they were
185// declared on the command line.
186static void BuildLinkItems(
187 Linker::ItemList& Items,
188 const cl::list<std::string>& Files,
189 const cl::list<std::string>& Libraries) {
190
Misha Brukman3da94ae2005-04-22 00:00:37 +0000191 // Build the list of linkage items for LinkItems.
Reid Spencerc4064132004-12-13 03:01:14 +0000192
193 cl::list<std::string>::const_iterator fileIt = Files.begin();
194 cl::list<std::string>::const_iterator libIt = Libraries.begin();
195
196 int libPos = -1, filePos = -1;
Reid Spencer05f7e792004-12-13 17:18:19 +0000197 while ( libIt != Libraries.end() || fileIt != Files.end() ) {
Reid Spencerc4064132004-12-13 03:01:14 +0000198 if (libIt != Libraries.end())
199 libPos = Libraries.getPosition(libIt - Libraries.begin());
200 else
201 libPos = -1;
202 if (fileIt != Files.end())
203 filePos = Files.getPosition(fileIt - Files.begin());
204 else
205 filePos = -1;
206
207 if (filePos != -1 && (libPos == -1 || filePos < libPos)) {
208 // Add a source file
209 Items.push_back(std::make_pair(*fileIt++, false));
210 } else if (libPos != -1 && (filePos == -1 || libPos < filePos)) {
211 // Add a library
212 Items.push_back(std::make_pair(*libIt++, true));
Reid Spencerc4064132004-12-13 03:01:14 +0000213 }
214 }
215}
Reid Spencer05f7e792004-12-13 17:18:19 +0000216
Reid Spencer93f8f552004-12-13 23:44:23 +0000217int main(int argc, char **argv, char **envp ) {
Chris Lattnerc30598b2006-12-06 01:18:01 +0000218 llvm_shutdown_obj X; // Call llvm_shutdown() on exit.
Chris Lattner5ff62e92002-07-22 02:10:13 +0000219 cl::ParseCommandLineOptions(argc, argv, " llvm linker for GCC\n");
Reid Spencer9de7b332004-08-29 19:28:55 +0000220 sys::PrintStackTraceOnErrorSignal();
Chris Lattnere7fca512002-01-24 19:12:12 +0000221
Reid Spencer4c1af902004-11-14 22:17:03 +0000222 int exitCode = 0;
Chris Lattnere7fca512002-01-24 19:12:12 +0000223
Reid Spencerc4064132004-12-13 03:01:14 +0000224 std::string ProgName = sys::Path(argv[0]).getBasename();
Reid Spencer328ead92005-12-13 20:00:37 +0000225 Linker TheLinker(ProgName, OutputFilename, Verbose);
Reid Spencerc4064132004-12-13 03:01:14 +0000226
Reid Spencer4c1af902004-11-14 22:17:03 +0000227 try {
Reid Spencer4c1af902004-11-14 22:17:03 +0000228 // Remove any consecutive duplicates of the same library...
229 Libraries.erase(std::unique(Libraries.begin(), Libraries.end()),
230 Libraries.end());
Chris Lattnerfd35c642003-11-03 17:27:17 +0000231
Reid Spencerc4064132004-12-13 03:01:14 +0000232 TheLinker.addPaths(LibPaths);
233 TheLinker.addSystemPaths();
John Criswell71478b72003-09-19 20:24:23 +0000234
Reid Spencerc191d492004-12-05 19:15:29 +0000235 if (LinkAsLibrary) {
Reid Spencerc4064132004-12-13 03:01:14 +0000236 std::vector<sys::Path> Files;
237 for (unsigned i = 0; i < InputFilenames.size(); ++i )
238 Files.push_back(sys::Path(InputFilenames[i]));
239
240 if (TheLinker.LinkInFiles(Files))
241 return 1; // Error already printed by linker
242
Reid Spencer6b463b22004-12-08 05:17:40 +0000243 // The libraries aren't linked in but are noted as "dependent" in the
244 // module.
Misha Brukman3da94ae2005-04-22 00:00:37 +0000245 for (cl::list<std::string>::const_iterator I = Libraries.begin(),
Reid Spencer6b463b22004-12-08 05:17:40 +0000246 E = Libraries.end(); I != E ; ++I) {
Reid Spencerc4064132004-12-13 03:01:14 +0000247 TheLinker.getModule()->addLibrary(*I);
Reid Spencer6b463b22004-12-08 05:17:40 +0000248 }
249
Reid Spencerc191d492004-12-05 19:15:29 +0000250 } else {
251 // Build a list of the items from our command line
Reid Spencerc4064132004-12-13 03:01:14 +0000252 Linker::ItemList Items;
Reid Spencerf4484f32006-01-10 03:14:40 +0000253 Linker::ItemList NativeItems;
Reid Spencerc191d492004-12-05 19:15:29 +0000254 BuildLinkItems(Items, InputFilenames, Libraries);
Chris Lattnere7fca512002-01-24 19:12:12 +0000255
Reid Spencerc191d492004-12-05 19:15:29 +0000256 // Link all the items together
Reid Spencerf4484f32006-01-10 03:14:40 +0000257 if (TheLinker.LinkInItems(Items,NativeItems))
Reid Spencerc191d492004-12-05 19:15:29 +0000258 return 1; // Error already printed
Reid Spencerf4484f32006-01-10 03:14:40 +0000259
260 // Revise the Libraries based on the remaining (native) libraries that
261 // were not linked in to the bytecode. This ensures that we don't attempt
262 // to pass a bytecode library to the native linker
263 Libraries.clear(); // we've consumed the libraries except for native
264 if ((Native || NativeCBE) && !NativeItems.empty()) {
265 for (Linker::ItemList::const_iterator I = NativeItems.begin(),
266 E = NativeItems.end(); I != E; ++I) {
267 Libraries.push_back(I->first);
268 }
269 }
Reid Spencerc191d492004-12-05 19:15:29 +0000270 }
Chris Lattnere7fca512002-01-24 19:12:12 +0000271
Reid Spencerc4064132004-12-13 03:01:14 +0000272 // We're done with the Linker, so tell it to release its module
273 std::auto_ptr<Module> Composite(TheLinker.releaseModule());
274
Reid Spencer4c1af902004-11-14 22:17:03 +0000275 // Create the output file.
276 std::string RealBytecodeOutput = OutputFilename;
Reid Spencer837149c2005-02-28 08:45:35 +0000277 if (!LinkAsLibrary || Native || NativeCBE) RealBytecodeOutput += ".bc";
Jeff Cohen5fb6ed42005-01-22 17:36:17 +0000278 std::ios::openmode io_mode = std::ios::out | std::ios::trunc |
279 std::ios::binary;
280 std::ofstream Out(RealBytecodeOutput.c_str(), io_mode);
Reid Spencer4c1af902004-11-14 22:17:03 +0000281 if (!Out.good())
282 return PrintAndReturn(argv[0], "error opening '" + RealBytecodeOutput +
283 "' for writing!");
Chris Lattner76d12292002-04-18 19:55:25 +0000284
Reid Spencer4c1af902004-11-14 22:17:03 +0000285 // Ensure that the bytecode file gets removed from the disk if we get a
286 // SIGINT signal.
287 sys::RemoveFileOnSignal(sys::Path(RealBytecodeOutput));
John Criswelldc0de4f2003-09-18 16:22:26 +0000288
Chris Lattnerbcfe1e22004-12-12 07:53:51 +0000289 // Strip everything if Strip is set, otherwise if stripdebug is set, just
290 // strip debug info.
Chris Lattner3f14fb12004-12-02 21:26:10 +0000291 int StripLevel = Strip ? 2 : (StripDebug ? 1 : 0);
Chris Lattnerbcfe1e22004-12-12 07:53:51 +0000292
Misha Brukman3da94ae2005-04-22 00:00:37 +0000293 // Internalize the module if neither -disable-internalize nor
Chris Lattnerbcfe1e22004-12-12 07:53:51 +0000294 // -link-as-library are passed in.
295 bool ShouldInternalize = !NoInternalize & !LinkAsLibrary;
296
297 // Generate the bytecode file.
298 if (GenerateBytecode(Composite.get(), StripLevel, ShouldInternalize, &Out)){
Reid Spencer4c1af902004-11-14 22:17:03 +0000299 Out.close();
300 return PrintAndReturn(argv[0], "error generating bytecode");
John Criswelldabaf7d2003-09-16 21:27:35 +0000301 }
Misha Brukmanc1fdca82003-08-20 20:38:15 +0000302
Reid Spencer4c1af902004-11-14 22:17:03 +0000303 // Close the bytecode file.
304 Out.close();
305
Reid Spencer837149c2005-02-28 08:45:35 +0000306 // Generate either a native file or a JIT shell script. If the user wants
Misha Brukman3da94ae2005-04-22 00:00:37 +0000307 // to generate a native file, compile it from the bytecode file. Otherwise,
308 // if the target is not a library, create a script that will run the
Reid Spencer837149c2005-02-28 08:45:35 +0000309 // bytecode through the JIT.
310 if (Native) {
311 // Name of the Assembly Language output file
312 sys::Path AssemblyFile (OutputFilename);
313 AssemblyFile.appendSuffix("s");
Reid Spencer4c1af902004-11-14 22:17:03 +0000314
Reid Spencer837149c2005-02-28 08:45:35 +0000315 // Mark the output files for removal if we get an interrupt.
316 sys::RemoveFileOnSignal(AssemblyFile);
317 sys::RemoveFileOnSignal(sys::Path(OutputFilename));
Reid Spencer4c1af902004-11-14 22:17:03 +0000318
Reid Spencer837149c2005-02-28 08:45:35 +0000319 // Determine the locations of the llc and gcc programs.
320 sys::Path llc = FindExecutable("llc", argv[0]);
321 if (llc.isEmpty())
322 return PrintAndReturn(argv[0], "Failed to find llc");
Reid Spencer4c1af902004-11-14 22:17:03 +0000323
Reid Spencer837149c2005-02-28 08:45:35 +0000324 sys::Path gcc = FindExecutable("gcc", argv[0]);
325 if (gcc.isEmpty())
326 return PrintAndReturn(argv[0], "Failed to find gcc");
Reid Spencer4c1af902004-11-14 22:17:03 +0000327
Reid Spencer837149c2005-02-28 08:45:35 +0000328 // Generate an assembly language file for the bytecode.
Bill Wendling68fe61d2006-11-29 00:19:40 +0000329 if (Verbose) llvm_cout << "Generating Assembly Code\n";
Reid Spencer8ea5ecb2006-08-21 06:04:45 +0000330 std::string ErrMsg;
331 if (0 != GenerateAssembly(
332 AssemblyFile.toString(), RealBytecodeOutput, llc, ErrMsg, Verbose)) {
Bill Wendling68fe61d2006-11-29 00:19:40 +0000333 llvm_cerr << argv[0] << ": " << ErrMsg << "\n";
Reid Spencer8ea5ecb2006-08-21 06:04:45 +0000334 return 2;
335 }
Bill Wendling68fe61d2006-11-29 00:19:40 +0000336 if (Verbose) llvm_cout << "Generating Native Code\n";
Reid Spencer8ea5ecb2006-08-21 06:04:45 +0000337 if (0 != GenerateNative(OutputFilename, AssemblyFile.toString(),
Chris Lattner1d924f62005-08-02 22:07:38 +0000338 LibPaths, Libraries, gcc, envp, LinkAsLibrary,
Reid Spencer8ea5ecb2006-08-21 06:04:45 +0000339 NoInternalize, RPath, SOName, ErrMsg, Verbose) ) {
Bill Wendling68fe61d2006-11-29 00:19:40 +0000340 llvm_cerr << argv[0] << ": " << ErrMsg << "\n";
Reid Spencer8ea5ecb2006-08-21 06:04:45 +0000341 return 2;
342 }
Reid Spencer4c1af902004-11-14 22:17:03 +0000343
Misha Brukmanb0bafc52005-04-20 03:22:18 +0000344 if (!SaveTemps) {
345 // Remove the assembly language file.
Reid Spencera229c5c2005-07-08 03:08:58 +0000346 AssemblyFile.eraseFromDisk();
Misha Brukmanb0bafc52005-04-20 03:22:18 +0000347 // Remove the bytecode language file.
Reid Spencera229c5c2005-07-08 03:08:58 +0000348 sys::Path(RealBytecodeOutput).eraseFromDisk();
Misha Brukmanb0bafc52005-04-20 03:22:18 +0000349 }
Misha Brukman704448f2005-04-20 04:08:35 +0000350
Reid Spencer837149c2005-02-28 08:45:35 +0000351 } else if (NativeCBE) {
352 sys::Path CFile (OutputFilename);
353 CFile.appendSuffix("cbe.c");
Reid Spencer4c1af902004-11-14 22:17:03 +0000354
Reid Spencer837149c2005-02-28 08:45:35 +0000355 // Mark the output files for removal if we get an interrupt.
356 sys::RemoveFileOnSignal(CFile);
357 sys::RemoveFileOnSignal(sys::Path(OutputFilename));
358
359 // Determine the locations of the llc and gcc programs.
360 sys::Path llc = FindExecutable("llc", argv[0]);
361 if (llc.isEmpty())
362 return PrintAndReturn(argv[0], "Failed to find llc");
363
364 sys::Path gcc = FindExecutable("gcc", argv[0]);
365 if (gcc.isEmpty())
366 return PrintAndReturn(argv[0], "Failed to find gcc");
367
368 // Generate an assembly language file for the bytecode.
Bill Wendling68fe61d2006-11-29 00:19:40 +0000369 if (Verbose) llvm_cout << "Generating C Source Code\n";
Reid Spencer8ea5ecb2006-08-21 06:04:45 +0000370 std::string ErrMsg;
371 if (0 != GenerateCFile(
372 CFile.toString(), RealBytecodeOutput, llc, ErrMsg, Verbose)) {
Bill Wendling68fe61d2006-11-29 00:19:40 +0000373 llvm_cerr << argv[0] << ": " << ErrMsg << "\n";
Reid Spencer8ea5ecb2006-08-21 06:04:45 +0000374 return 2;
375 }
Bill Wendling68fe61d2006-11-29 00:19:40 +0000376 if (Verbose) llvm_cout << "Generating Native Code\n";
Reid Spencer8ea5ecb2006-08-21 06:04:45 +0000377 if (0 != GenerateNative(OutputFilename, CFile.toString(),
Chris Lattner1d924f62005-08-02 22:07:38 +0000378 LibPaths, Libraries, gcc, envp, LinkAsLibrary,
Reid Spencer8ea5ecb2006-08-21 06:04:45 +0000379 NoInternalize, RPath, SOName, ErrMsg, Verbose)) {
Bill Wendling68fe61d2006-11-29 00:19:40 +0000380 llvm_cerr << argv[0] << ": " << ErrMsg << "\n";
Reid Spencer8ea5ecb2006-08-21 06:04:45 +0000381 return 2;
382 }
Reid Spencer837149c2005-02-28 08:45:35 +0000383
Misha Brukmanb0bafc52005-04-20 03:22:18 +0000384 if (!SaveTemps) {
385 // Remove the assembly language file.
Reid Spencera229c5c2005-07-08 03:08:58 +0000386 CFile.eraseFromDisk();
Misha Brukmanb0bafc52005-04-20 03:22:18 +0000387 // Remove the bytecode language file.
Reid Spencera229c5c2005-07-08 03:08:58 +0000388 sys::Path(RealBytecodeOutput).eraseFromDisk();
Misha Brukmanb0bafc52005-04-20 03:22:18 +0000389 }
Reid Spencer837149c2005-02-28 08:45:35 +0000390
391 } else if (!LinkAsLibrary) {
392 EmitShellScript(argv);
Misha Brukman704448f2005-04-20 04:08:35 +0000393
Reid Spencer837149c2005-02-28 08:45:35 +0000394 // Make the bytecode file readable and directly executable in LLEE
Reid Spencere1647f42006-08-22 23:27:23 +0000395 std::string ErrMsg;
396 if (sys::Path(RealBytecodeOutput).makeExecutableOnDisk(&ErrMsg)) {
Bill Wendling68fe61d2006-11-29 00:19:40 +0000397 llvm_cerr << argv[0] << ": " << ErrMsg << "\n";
Reid Spencere1647f42006-08-22 23:27:23 +0000398 return 1;
399 }
400 if (sys::Path(RealBytecodeOutput).makeReadableOnDisk(&ErrMsg)) {
Bill Wendling68fe61d2006-11-29 00:19:40 +0000401 llvm_cerr << argv[0] << ": " << ErrMsg << "\n";
Reid Spencere1647f42006-08-22 23:27:23 +0000402 return 1;
403 }
Reid Spencer4c1af902004-11-14 22:17:03 +0000404 }
Reid Spencer837149c2005-02-28 08:45:35 +0000405
406 // Make the output, whether native or script, executable as well...
Reid Spencere1647f42006-08-22 23:27:23 +0000407 std::string ErrMsg;
408 if (sys::Path(OutputFilename).makeExecutableOnDisk(&ErrMsg)) {
Bill Wendling68fe61d2006-11-29 00:19:40 +0000409 llvm_cerr << argv[0] << ": " << ErrMsg << "\n";
Reid Spencere1647f42006-08-22 23:27:23 +0000410 return 1;
411 }
Reid Spencer4c1af902004-11-14 22:17:03 +0000412 } catch (const char*msg) {
Bill Wendling68fe61d2006-11-29 00:19:40 +0000413 llvm_cerr << argv[0] << ": " << msg << "\n";
Reid Spencer4c1af902004-11-14 22:17:03 +0000414 exitCode = 1;
415 } catch (const std::string& msg) {
Bill Wendling68fe61d2006-11-29 00:19:40 +0000416 llvm_cerr << argv[0] << ": " << msg << "\n";
Reid Spencer4c1af902004-11-14 22:17:03 +0000417 exitCode = 2;
418 } catch (...) {
419 // This really shouldn't happen, but just in case ....
Bill Wendling68fe61d2006-11-29 00:19:40 +0000420 llvm_cerr << argv[0] << ": An unexpected unknown exception occurred.\n";
Reid Spencer4c1af902004-11-14 22:17:03 +0000421 exitCode = 3;
Chris Lattner10970eb2003-04-19 22:44:38 +0000422 }
Chris Lattnere7fca512002-01-24 19:12:12 +0000423
Reid Spencer4c1af902004-11-14 22:17:03 +0000424 return exitCode;
Chris Lattnere7fca512002-01-24 19:12:12 +0000425}