blob: 432fa9d7cf464cf6873b5734d8229cc7a7c4f4bb [file] [log] [blame]
Chris Lattnera58d2be2003-09-30 03:24:28 +00001//===- GenerateCode.cpp - Functions for generating executable files ------===//
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//===----------------------------------------------------------------------===//
John Criswelldc0de4f2003-09-18 16:22:26 +00009//
10// This file contains functions for generating executable files once linking
11// has finished. This includes generating a shell script to run the JIT or
12// a native executable derived from the bytecode.
13//
14//===----------------------------------------------------------------------===//
15
Misha Brukmanbb5a4d02003-09-30 17:33:12 +000016#include "gccld.h"
Reid Spencer6da1e0d2004-12-14 04:20:08 +000017#include "llvm/System/Program.h"
John Criswelldc0de4f2003-09-18 16:22:26 +000018#include "llvm/Module.h"
19#include "llvm/PassManager.h"
Chris Lattnercc650b62003-11-09 19:55:09 +000020#include "llvm/Analysis/LoadValueNumbering.h"
Chris Lattner2d26ffb2004-07-27 08:13:15 +000021#include "llvm/Analysis/Passes.h"
Brian Gaeke1ab90d42003-11-16 23:07:28 +000022#include "llvm/Analysis/Verifier.h"
Reid Spencer837149c2005-02-28 08:45:35 +000023#include "llvm/Bytecode/Archive.h"
John Criswelldc0de4f2003-09-18 16:22:26 +000024#include "llvm/Bytecode/WriteBytecodePass.h"
Misha Brukmanbb5a4d02003-09-30 17:33:12 +000025#include "llvm/Target/TargetData.h"
26#include "llvm/Transforms/IPO.h"
27#include "llvm/Transforms/Scalar.h"
Reid Spencer551ccae2004-09-01 22:55:40 +000028#include "llvm/Support/SystemUtils.h"
29#include "llvm/Support/CommandLine.h"
Reid Spencer6da1e0d2004-12-14 04:20:08 +000030
Brian Gaeked0fde302003-11-11 22:41:34 +000031using namespace llvm;
32
Chris Lattner246ce3c2003-10-24 18:09:23 +000033namespace {
34 cl::opt<bool>
35 DisableInline("disable-inlining", cl::desc("Do not run the inliner pass"));
Brian Gaeke1ab90d42003-11-16 23:07:28 +000036
37 cl::opt<bool>
38 Verify("verify", cl::desc("Verify intermediate results of all passes"));
39
40 cl::opt<bool>
41 DisableOptimizations("disable-opt",
42 cl::desc("Do not run any optimization passes"));
Chris Lattner246ce3c2003-10-24 18:09:23 +000043}
44
Chris Lattner0ebee742004-06-02 00:22:24 +000045/// CopyEnv - This function takes an array of environment variables and makes a
46/// copy of it. This copy can then be manipulated any way the caller likes
47/// without affecting the process's real environment.
48///
49/// Inputs:
50/// envp - An array of C strings containing an environment.
51///
52/// Return value:
53/// NULL - An error occurred.
54///
55/// Otherwise, a pointer to a new array of C strings is returned. Every string
56/// in the array is a duplicate of the one in the original array (i.e. we do
57/// not copy the char *'s from one array to another).
58///
59static char ** CopyEnv(char ** const envp) {
60 // Count the number of entries in the old list;
61 unsigned entries; // The number of entries in the old environment list
62 for (entries = 0; envp[entries] != NULL; entries++)
63 /*empty*/;
64
65 // Add one more entry for the NULL pointer that ends the list.
66 ++entries;
67
68 // If there are no entries at all, just return NULL.
69 if (entries == 0)
70 return NULL;
71
72 // Allocate a new environment list.
73 char **newenv = new char* [entries];
74 if ((newenv = new char* [entries]) == NULL)
75 return NULL;
76
77 // Make a copy of the list. Don't forget the NULL that ends the list.
78 entries = 0;
79 while (envp[entries] != NULL) {
80 newenv[entries] = new char[strlen (envp[entries]) + 1];
81 strcpy (newenv[entries], envp[entries]);
82 ++entries;
83 }
84 newenv[entries] = NULL;
85
86 return newenv;
87}
88
89
90/// RemoveEnv - Remove the specified environment variable from the environment
91/// array.
92///
93/// Inputs:
94/// name - The name of the variable to remove. It cannot be NULL.
95/// envp - The array of environment variables. It cannot be NULL.
96///
97/// Notes:
98/// This is mainly done because functions to remove items from the environment
99/// are not available across all platforms. In particular, Solaris does not
100/// seem to have an unsetenv() function or a setenv() function (or they are
101/// undocumented if they do exist).
102///
103static void RemoveEnv(const char * name, char ** const envp) {
104 for (unsigned index=0; envp[index] != NULL; index++) {
105 // Find the first equals sign in the array and make it an EOS character.
106 char *p = strchr (envp[index], '=');
107 if (p == NULL)
108 continue;
109 else
110 *p = '\0';
111
112 // Compare the two strings. If they are equal, zap this string.
113 // Otherwise, restore it.
114 if (!strcmp(name, envp[index]))
115 *envp[index] = '\0';
116 else
117 *p = '=';
118 }
Chris Lattner0ebee742004-06-02 00:22:24 +0000119}
120
Misha Brukmanb0bafc52005-04-20 03:22:18 +0000121static void dumpArgs(const char **args) {
Chris Lattnerf3942132005-09-23 06:05:46 +0000122 std::cerr << *args++;
Misha Brukmanb0bafc52005-04-20 03:22:18 +0000123 while (*args)
Chris Lattnerf3942132005-09-23 06:05:46 +0000124 std::cerr << ' ' << *args++;
125 std::cerr << '\n' << std::flush;
Misha Brukmanb0bafc52005-04-20 03:22:18 +0000126}
127
Brian Gaeke1ab90d42003-11-16 23:07:28 +0000128static inline void addPass(PassManager &PM, Pass *P) {
129 // Add the pass to the pass manager...
130 PM.add(P);
Misha Brukman704448f2005-04-20 04:08:35 +0000131
Brian Gaeke1ab90d42003-11-16 23:07:28 +0000132 // If we are verifying all of the intermediate steps, add the verifier...
133 if (Verify) PM.add(createVerifierPass());
134}
135
Reid Spencer837149c2005-02-28 08:45:35 +0000136static bool isBytecodeLibrary(const sys::Path &FullPath) {
137 // Check for a bytecode file
138 if (FullPath.isBytecodeFile()) return true;
139 // Check for a dynamic library file
140 if (FullPath.isDynamicLibrary()) return false;
141 // Check for a true bytecode archive file
142 if (FullPath.isArchive() ) {
143 std::string ErrorMessage;
144 Archive* ar = Archive::OpenAndLoadSymbols( FullPath, &ErrorMessage );
Misha Brukman704448f2005-04-20 04:08:35 +0000145 return ar->isBytecodeArchive();
Reid Spencer837149c2005-02-28 08:45:35 +0000146 }
147 return false;
148}
149
Misha Brukman704448f2005-04-20 04:08:35 +0000150static bool isBytecodeLPath(const std::string &LibPath) {
Reid Spencer837149c2005-02-28 08:45:35 +0000151 bool isBytecodeLPath = false;
152
Reid Spencerdd04df02005-07-07 23:21:43 +0000153 sys::Path LPath(LibPath);
Reid Spencer837149c2005-02-28 08:45:35 +0000154
Reid Spencer8f1ac1c2005-07-08 16:48:52 +0000155 // Make sure it exists
156 if (!LPath.exists())
Reid Spencerdd04df02005-07-07 23:21:43 +0000157 return isBytecodeLPath;
Reid Spencer837149c2005-02-28 08:45:35 +0000158
Reid Spencer8f1ac1c2005-07-08 16:48:52 +0000159 // Make sure its a directory
Chris Lattnerf3942132005-09-23 06:05:46 +0000160 try {
Reid Spencer8f1ac1c2005-07-08 16:48:52 +0000161 if (!LPath.isDirectory())
162 return isBytecodeLPath;
Chris Lattnerf3942132005-09-23 06:05:46 +0000163 } catch (std::string& xcptn) {
Reid Spencer8f1ac1c2005-07-08 16:48:52 +0000164 return isBytecodeLPath;
165 }
166
Reid Spencer837149c2005-02-28 08:45:35 +0000167 // Grab the contents of the -L path
168 std::set<sys::Path> Files;
169 LPath.getDirectoryContents(Files);
170
171 // Iterate over the contents one by one to determine
172 // if this -L path has any bytecode shared libraries
173 // or archives
174 std::set<sys::Path>::iterator File = Files.begin();
175 for (; File != Files.end(); ++File) {
176
177 if ( File->isDirectory() )
178 continue;
Misha Brukman704448f2005-04-20 04:08:35 +0000179
Reid Spencer837149c2005-02-28 08:45:35 +0000180 std::string path = File->toString();
181 std::string dllsuffix = sys::Path::GetDLLSuffix();
182
183 // Check for an ending '.dll,.so' or '.a' suffix as all
184 // other files are not of interest to us here
185 if ( path.find(dllsuffix, path.size()-dllsuffix.size()) == std::string::npos
186 && path.find(".a", path.size()-2) == std::string::npos )
187 continue;
Misha Brukman704448f2005-04-20 04:08:35 +0000188
Reid Spencer837149c2005-02-28 08:45:35 +0000189 // Finally, check to see if the file is a true bytecode file
190 if (isBytecodeLibrary(*File))
191 isBytecodeLPath = true;
192 }
193 return isBytecodeLPath;
194}
195
Misha Brukman1c534052003-09-30 17:42:57 +0000196/// GenerateBytecode - generates a bytecode file from the specified module.
197///
198/// Inputs:
199/// M - The module for which bytecode should be generated.
Chris Lattner3f14fb12004-12-02 21:26:10 +0000200/// StripLevel - 2 if we should strip all symbols, 1 if we should strip
201/// debug info.
Misha Brukman1c534052003-09-30 17:42:57 +0000202/// Internalize - Flags whether all symbols should be marked internal.
203/// Out - Pointer to file stream to which to write the output.
204///
Misha Brukman1c534052003-09-30 17:42:57 +0000205/// Returns non-zero value on error.
206///
Chris Lattner3f14fb12004-12-02 21:26:10 +0000207int llvm::GenerateBytecode(Module *M, int StripLevel, bool Internalize,
Chris Lattner27a9b272004-04-06 16:54:04 +0000208 std::ostream *Out) {
John Criswelldc0de4f2003-09-18 16:22:26 +0000209 // In addition to just linking the input from GCC, we also want to spiff it up
210 // a little bit. Do this now.
211 PassManager Passes;
212
Brian Gaeke1ab90d42003-11-16 23:07:28 +0000213 if (Verify) Passes.add(createVerifierPass());
214
John Criswelldc0de4f2003-09-18 16:22:26 +0000215 // Add an appropriate TargetData instance for this module...
Misha Brukman438e3642003-11-20 06:26:15 +0000216 addPass(Passes, new TargetData("gccld", M));
John Criswelldc0de4f2003-09-18 16:22:26 +0000217
Chris Lattner548e8132003-11-28 09:44:03 +0000218 // Often if the programmer does not specify proper prototypes for the
219 // functions they are calling, they end up calling a vararg version of the
220 // function that does not get a body filled in (the real function has typed
221 // arguments). This pass merges the two functions.
222 addPass(Passes, createFunctionResolvingPass());
223
Chris Lattner429a9cb2004-11-16 18:59:20 +0000224 if (!DisableOptimizations) {
Chris Lattner86f42db2004-11-17 16:41:19 +0000225 if (Internalize) {
226 // Now that composite has been compiled, scan through the module, looking
227 // for a main function. If main is defined, mark all other functions
228 // internal.
229 addPass(Passes, createInternalizePass());
230 }
231
Chris Lattner93a00e42004-10-07 04:12:02 +0000232 // Now that we internalized some globals, see if we can hack on them!
233 addPass(Passes, createGlobalOptimizerPass());
Chris Lattnerdd429c62004-02-25 21:35:13 +0000234
Brian Gaeke1ab90d42003-11-16 23:07:28 +0000235 // Linking modules together can lead to duplicated global constants, only
236 // keep one copy of each constant...
Misha Brukman438e3642003-11-20 06:26:15 +0000237 addPass(Passes, createConstantMergePass());
John Criswelldc0de4f2003-09-18 16:22:26 +0000238
Brian Gaeke1ab90d42003-11-16 23:07:28 +0000239 // Propagate constants at call sites into the functions they call.
Chris Lattnerb4a400a2004-12-10 08:03:43 +0000240 addPass(Passes, createIPSCCPPass());
Brian Gaeke1ab90d42003-11-16 23:07:28 +0000241
242 // Remove unused arguments from functions...
Misha Brukman438e3642003-11-20 06:26:15 +0000243 addPass(Passes, createDeadArgEliminationPass());
Brian Gaeke1ab90d42003-11-16 23:07:28 +0000244
245 if (!DisableInline)
Misha Brukman438e3642003-11-20 06:26:15 +0000246 addPass(Passes, createFunctionInliningPass()); // Inline small functions
Brian Gaeke1ab90d42003-11-16 23:07:28 +0000247
Chris Lattnerf9c455d2004-04-12 05:38:15 +0000248 addPass(Passes, createPruneEHPass()); // Remove dead EH info
Chris Lattnere3ef9b52004-10-11 04:47:18 +0000249 addPass(Passes, createGlobalOptimizerPass()); // Optimize globals again.
Chris Lattnerf9c455d2004-04-12 05:38:15 +0000250 addPass(Passes, createGlobalDCEPass()); // Remove dead functions
251
Chris Lattnerf8338c42004-03-07 22:12:40 +0000252 // If we didn't decide to inline a function, check to see if we can
253 // transform it to pass arguments by value instead of by reference.
254 addPass(Passes, createArgumentPromotionPass());
255
Chris Lattnerf74a4012004-02-26 03:34:30 +0000256 // The IPO passes may leave cruft around. Clean up after them.
257 addPass(Passes, createInstructionCombiningPass());
258
259 addPass(Passes, createScalarReplAggregatesPass()); // Break up allocas
260
Brian Gaeke1ab90d42003-11-16 23:07:28 +0000261 // Run a few AA driven optimizations here and now, to cleanup the code.
Chris Lattner93127fb2004-08-02 10:10:08 +0000262 addPass(Passes, createGlobalsModRefPass()); // IP alias analysis
Brian Gaeke1ab90d42003-11-16 23:07:28 +0000263
Misha Brukman438e3642003-11-20 06:26:15 +0000264 addPass(Passes, createLICMPass()); // Hoist loop invariants
265 addPass(Passes, createLoadValueNumberingPass()); // GVN for load instrs
266 addPass(Passes, createGCSEPass()); // Remove common subexprs
Chris Lattner2d26ffb2004-07-27 08:13:15 +0000267 addPass(Passes, createDeadStoreEliminationPass()); // Nuke dead stores
Brian Gaeke1ab90d42003-11-16 23:07:28 +0000268
Chris Lattnerf74a4012004-02-26 03:34:30 +0000269 // Cleanup and simplify the code after the scalar optimizations.
Misha Brukman438e3642003-11-20 06:26:15 +0000270 addPass(Passes, createInstructionCombiningPass());
Brian Gaeke1ab90d42003-11-16 23:07:28 +0000271
272 // Delete basic blocks, which optimization passes may have killed...
Misha Brukman438e3642003-11-20 06:26:15 +0000273 addPass(Passes, createCFGSimplificationPass());
Brian Gaeke1ab90d42003-11-16 23:07:28 +0000274
275 // Now that we have optimized the program, discard unreachable functions...
Misha Brukman438e3642003-11-20 06:26:15 +0000276 addPass(Passes, createGlobalDCEPass());
John Criswelldc0de4f2003-09-18 16:22:26 +0000277 }
278
Chris Lattner3f14fb12004-12-02 21:26:10 +0000279 // If the -s or -S command line options were specified, strip the symbols out
280 // of the resulting program to make it smaller. -s and -S are GLD options
281 // that we are supporting.
282 if (StripLevel)
283 addPass(Passes, createStripSymbolsPass(StripLevel == 1));
Chris Lattner429a9cb2004-11-16 18:59:20 +0000284
Chris Lattner0cccb182004-01-14 03:39:46 +0000285 // Make sure everything is still good.
286 Passes.add(createVerifierPass());
287
John Criswelldc0de4f2003-09-18 16:22:26 +0000288 // Add the pass that writes bytecode to the output file...
Misha Brukman438e3642003-11-20 06:26:15 +0000289 addPass(Passes, new WriteBytecodePass(Out));
John Criswelldc0de4f2003-09-18 16:22:26 +0000290
291 // Run our queue of passes all at once now, efficiently.
292 Passes.run(*M);
293
294 return 0;
295}
296
Misha Brukman1c534052003-09-30 17:42:57 +0000297/// GenerateAssembly - generates a native assembly language source file from the
298/// specified bytecode file.
299///
300/// Inputs:
301/// InputFilename - The name of the output bytecode file.
302/// OutputFilename - The name of the file to generate.
303/// llc - The pathname to use for LLC.
Misha Brukman1c534052003-09-30 17:42:57 +0000304///
Misha Brukman1c534052003-09-30 17:42:57 +0000305/// Return non-zero value on error.
306///
Chris Lattner27a9b272004-04-06 16:54:04 +0000307int llvm::GenerateAssembly(const std::string &OutputFilename,
308 const std::string &InputFilename,
Misha Brukmanb0bafc52005-04-20 03:22:18 +0000309 const sys::Path &llc,
310 bool Verbose) {
John Criswelldc0de4f2003-09-18 16:22:26 +0000311 // Run LLC to convert the bytecode file into assembly code.
Reid Spencerf6358c72004-12-19 18:00:56 +0000312 std::vector<const char*> args;
Chris Lattnerbf9add42005-04-10 20:59:38 +0000313 args.push_back(llc.c_str());
Reid Spencer6da1e0d2004-12-14 04:20:08 +0000314 args.push_back("-f");
315 args.push_back("-o");
Reid Spencerf6358c72004-12-19 18:00:56 +0000316 args.push_back(OutputFilename.c_str());
317 args.push_back(InputFilename.c_str());
Chris Lattnered5fa582005-02-13 23:02:34 +0000318 args.push_back(0);
Misha Brukmanb0bafc52005-04-20 03:22:18 +0000319 if (Verbose) dumpArgs(&args[0]);
Reid Spencerf6358c72004-12-19 18:00:56 +0000320 return sys::Program::ExecuteAndWait(llc, &args[0]);
John Criswelldc0de4f2003-09-18 16:22:26 +0000321}
322
Chris Lattner1d924f62005-08-02 22:07:38 +0000323/// GenerateCFile - generates a C source file from the specified bytecode file.
Chris Lattner27a9b272004-04-06 16:54:04 +0000324int llvm::GenerateCFile(const std::string &OutputFile,
325 const std::string &InputFile,
Misha Brukmanb0bafc52005-04-20 03:22:18 +0000326 const sys::Path &llc,
327 bool Verbose) {
Chris Lattner69e8d282004-04-06 16:43:13 +0000328 // Run LLC to convert the bytecode file into C.
Reid Spencerf6358c72004-12-19 18:00:56 +0000329 std::vector<const char*> args;
Chris Lattnerbf9add42005-04-10 20:59:38 +0000330 args.push_back(llc.c_str());
Reid Spencer6da1e0d2004-12-14 04:20:08 +0000331 args.push_back("-march=c");
332 args.push_back("-f");
333 args.push_back("-o");
Reid Spencerf6358c72004-12-19 18:00:56 +0000334 args.push_back(OutputFile.c_str());
335 args.push_back(InputFile.c_str());
Chris Lattnered5fa582005-02-13 23:02:34 +0000336 args.push_back(0);
Misha Brukmanb0bafc52005-04-20 03:22:18 +0000337 if (Verbose) dumpArgs(&args[0]);
Reid Spencerf6358c72004-12-19 18:00:56 +0000338 return sys::Program::ExecuteAndWait(llc, &args[0]);
Chris Lattner69e8d282004-04-06 16:43:13 +0000339}
340
Chris Lattner1d924f62005-08-02 22:07:38 +0000341/// GenerateNative - generates a native executable file from the specified
342/// assembly source file.
Misha Brukman1c534052003-09-30 17:42:57 +0000343///
344/// Inputs:
345/// InputFilename - The name of the output bytecode file.
346/// OutputFilename - The name of the file to generate.
347/// Libraries - The list of libraries with which to link.
Misha Brukman1c534052003-09-30 17:42:57 +0000348/// gcc - The pathname to use for GGC.
349/// envp - A copy of the process's current environment.
350///
351/// Outputs:
352/// None.
353///
354/// Returns non-zero value on error.
355///
Chris Lattner27a9b272004-04-06 16:54:04 +0000356int llvm::GenerateNative(const std::string &OutputFilename,
357 const std::string &InputFilename,
Reid Spencer837149c2005-02-28 08:45:35 +0000358 const std::vector<std::string> &LibPaths,
Chris Lattner27a9b272004-04-06 16:54:04 +0000359 const std::vector<std::string> &Libraries,
Reid Spencer837149c2005-02-28 08:45:35 +0000360 const sys::Path &gcc, char ** const envp,
361 bool Shared,
Chris Lattner1d924f62005-08-02 22:07:38 +0000362 bool ExportAllAsDynamic,
Reid Spencer837149c2005-02-28 08:45:35 +0000363 const std::string &RPath,
Misha Brukmanb0bafc52005-04-20 03:22:18 +0000364 const std::string &SOName,
365 bool Verbose) {
John Criswelldc0de4f2003-09-18 16:22:26 +0000366 // Remove these environment variables from the environment of the
367 // programs that we will execute. It appears that GCC sets these
368 // environment variables so that the programs it uses can configure
369 // themselves identically.
370 //
Misha Brukmanbb5a4d02003-09-30 17:33:12 +0000371 // However, when we invoke GCC below, we want it to use its normal
372 // configuration. Hence, we must sanitize its environment.
373 char ** clean_env = CopyEnv(envp);
John Criswelldc0de4f2003-09-18 16:22:26 +0000374 if (clean_env == NULL)
John Criswelldc0de4f2003-09-18 16:22:26 +0000375 return 1;
Misha Brukmanbb5a4d02003-09-30 17:33:12 +0000376 RemoveEnv("LIBRARY_PATH", clean_env);
377 RemoveEnv("COLLECT_GCC_OPTIONS", clean_env);
378 RemoveEnv("GCC_EXEC_PREFIX", clean_env);
379 RemoveEnv("COMPILER_PATH", clean_env);
380 RemoveEnv("COLLECT_GCC", clean_env);
John Criswelldc0de4f2003-09-18 16:22:26 +0000381
John Criswelldc0de4f2003-09-18 16:22:26 +0000382
John Criswelldc0de4f2003-09-18 16:22:26 +0000383 // Run GCC to assemble and link the program into native code.
384 //
385 // Note:
386 // We can't just assemble and link the file with the system assembler
387 // and linker because we don't know where to put the _start symbol.
388 // GCC mysteriously knows how to do it.
Reid Spencerf6358c72004-12-19 18:00:56 +0000389 std::vector<const char*> args;
Chris Lattnerbf9add42005-04-10 20:59:38 +0000390 args.push_back(gcc.c_str());
Reid Spencer6da1e0d2004-12-14 04:20:08 +0000391 args.push_back("-fno-strict-aliasing");
392 args.push_back("-O3");
393 args.push_back("-o");
Reid Spencerf6358c72004-12-19 18:00:56 +0000394 args.push_back(OutputFilename.c_str());
395 args.push_back(InputFilename.c_str());
Misha Brukman704448f2005-04-20 04:08:35 +0000396
Chris Lattnerf3942132005-09-23 06:05:46 +0000397 // StringsToDelete - We don't want to call c_str() on temporary strings.
398 // If we need a temporary string, copy it here so that the memory is not
399 // reclaimed until after the exec call. All of these strings are allocated
400 // with strdup.
401 std::vector<char*> StringsToDelete;
402
Reid Spencer837149c2005-02-28 08:45:35 +0000403 if (Shared) args.push_back("-shared");
Chris Lattner1d924f62005-08-02 22:07:38 +0000404 if (ExportAllAsDynamic) args.push_back("-export-dynamic");
Reid Spencer837149c2005-02-28 08:45:35 +0000405 if (!RPath.empty()) {
406 std::string rp = "-Wl,-rpath," + RPath;
Chris Lattnerf3942132005-09-23 06:05:46 +0000407 StringsToDelete.push_back(strdup(rp.c_str()));
408 args.push_back(StringsToDelete.back());
Reid Spencer837149c2005-02-28 08:45:35 +0000409 }
410 if (!SOName.empty()) {
411 std::string so = "-Wl,-soname," + SOName;
Chris Lattnerf3942132005-09-23 06:05:46 +0000412 StringsToDelete.push_back(strdup(so.c_str()));
413 args.push_back(StringsToDelete.back());
Reid Spencer837149c2005-02-28 08:45:35 +0000414 }
Misha Brukman704448f2005-04-20 04:08:35 +0000415
Reid Spencer837149c2005-02-28 08:45:35 +0000416 // Add in the libpaths to find the libraries.
417 //
418 // Note:
419 // When gccld is called from the llvm-gxx frontends, the -L paths for
Misha Brukman3da94ae2005-04-22 00:00:37 +0000420 // the LLVM cfrontend install paths are appended. We don't want the
Reid Spencer837149c2005-02-28 08:45:35 +0000421 // native linker to use these -L paths as they contain bytecode files.
422 // Further, we don't want any -L paths that contain bytecode shared
423 // libraries or true bytecode archive files. We omit them in all such
424 // cases.
Chris Lattnerf3942132005-09-23 06:05:46 +0000425 for (unsigned index = 0; index < LibPaths.size(); index++)
426 if (!isBytecodeLPath(LibPaths[index])) {
427 std::string Tmp = "-L"+LibPaths[index];
428 StringsToDelete.push_back(strdup(Tmp.c_str()));
429 args.push_back(StringsToDelete.back());
Reid Spencer837149c2005-02-28 08:45:35 +0000430 }
Misha Brukman3da94ae2005-04-22 00:00:37 +0000431
John Criswell71478b72003-09-19 20:24:23 +0000432 // Add in the libraries to link.
Chris Lattnerf3942132005-09-23 06:05:46 +0000433 for (unsigned index = 0; index < Libraries.size(); index++)
Reid Spencerf6358c72004-12-19 18:00:56 +0000434 if (Libraries[index] != "crtend") {
Chris Lattnerf3942132005-09-23 06:05:46 +0000435 std::string Tmp = "-l"+Libraries[index];
436 StringsToDelete.push_back(strdup(Tmp.c_str()));
437 args.push_back(StringsToDelete.back());
Reid Spencerf6358c72004-12-19 18:00:56 +0000438 }
Chris Lattnerf3942132005-09-23 06:05:46 +0000439 args.push_back(0); // Null terminate.
John Criswell71478b72003-09-19 20:24:23 +0000440
John Criswell71478b72003-09-19 20:24:23 +0000441 // Run the compiler to assembly and link together the program.
Misha Brukmanb0bafc52005-04-20 03:22:18 +0000442 if (Verbose) dumpArgs(&args[0]);
Chris Lattnerf3942132005-09-23 06:05:46 +0000443 int Res = sys::Program::ExecuteAndWait(gcc, &args[0], (const char**)clean_env);
444
445 while (!StringsToDelete.empty()) {
446 free(StringsToDelete.back());
447 StringsToDelete.pop_back();
448 }
449 return Res;
John Criswelldc0de4f2003-09-18 16:22:26 +0000450}
Chris Lattner0ebee742004-06-02 00:22:24 +0000451