blob: 50f32958d5843f9492b96780198ad90c2f64bba0 [file] [log] [blame]
Nick Lewyckye3365aa2010-09-23 23:48:20 +00001//===--- Tools.cpp - Tools Implementations --------------------------------===//
Daniel Dunbar47ac7d22009-03-18 06:00:36 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10#include "Tools.h"
11
Daniel Dunbar1d460332009-03-18 10:01:51 +000012#include "clang/Driver/Action.h"
Daniel Dunbar871adcf2009-03-18 07:06:02 +000013#include "clang/Driver/Arg.h"
Daniel Dunbarb488c1d2009-03-18 08:07:30 +000014#include "clang/Driver/ArgList.h"
Daniel Dunbaree848a72009-10-29 02:39:57 +000015#include "clang/Driver/Driver.h"
16#include "clang/Driver/DriverDiagnostic.h"
Daniel Dunbar871adcf2009-03-18 07:06:02 +000017#include "clang/Driver/Compilation.h"
18#include "clang/Driver/Job.h"
Daniel Dunbarb488c1d2009-03-18 08:07:30 +000019#include "clang/Driver/HostInfo.h"
20#include "clang/Driver/Option.h"
Daniel Dunbar265e9ef2009-11-19 04:25:22 +000021#include "clang/Driver/Options.h"
Daniel Dunbarb488c1d2009-03-18 08:07:30 +000022#include "clang/Driver/ToolChain.h"
Daniel Dunbar871adcf2009-03-18 07:06:02 +000023#include "clang/Driver/Util.h"
24
Daniel Dunbar88137642009-09-09 22:32:48 +000025#include "llvm/ADT/SmallString.h"
Douglas Gregor55d3f7a2009-10-29 00:41:01 +000026#include "llvm/ADT/StringSwitch.h"
Daniel Dunbar5b750fe2009-09-09 22:32:34 +000027#include "llvm/ADT/Twine.h"
Michael J. Spencer32bef4e2011-01-10 02:34:13 +000028#include "llvm/Support/FileSystem.h"
Daniel Dunbar02633b52009-03-26 16:23:12 +000029#include "llvm/Support/Format.h"
30#include "llvm/Support/raw_ostream.h"
Michael J. Spencer03013fa2010-11-29 18:12:39 +000031#include "llvm/Support/Host.h"
32#include "llvm/Support/Process.h"
Daniel Dunbar871adcf2009-03-18 07:06:02 +000033
34#include "InputInfo.h"
Daniel Dunbar02633b52009-03-26 16:23:12 +000035#include "ToolChains.h"
Daniel Dunbar871adcf2009-03-18 07:06:02 +000036
Daniel Dunbar47ac7d22009-03-18 06:00:36 +000037using namespace clang::driver;
38using namespace clang::driver::tools;
39
Daniel Dunbar88a3d6c2009-09-10 01:21:05 +000040/// CheckPreprocessingOptions - Perform some validation of preprocessing
41/// arguments that is shared with gcc.
42static void CheckPreprocessingOptions(const Driver &D, const ArgList &Args) {
43 if (Arg *A = Args.getLastArg(options::OPT_C, options::OPT_CC))
44 if (!Args.hasArg(options::OPT_E))
45 D.Diag(clang::diag::err_drv_argument_only_allowed_with)
46 << A->getAsString(Args) << "-E";
47}
48
Daniel Dunbare2fd6642009-09-10 01:21:12 +000049/// CheckCodeGenerationOptions - Perform some validation of code generation
50/// arguments that is shared with gcc.
51static void CheckCodeGenerationOptions(const Driver &D, const ArgList &Args) {
52 // In gcc, only ARM checks this, but it seems reasonable to check universally.
53 if (Args.hasArg(options::OPT_static))
54 if (const Arg *A = Args.getLastArg(options::OPT_dynamic,
55 options::OPT_mdynamic_no_pic))
56 D.Diag(clang::diag::err_drv_argument_not_allowed_with)
57 << A->getAsString(Args) << "-static";
58}
59
Chris Lattner3edbeb72010-03-29 17:55:58 +000060// Quote target names for inclusion in GNU Make dependency files.
61// Only the characters '$', '#', ' ', '\t' are quoted.
62static void QuoteTarget(llvm::StringRef Target,
63 llvm::SmallVectorImpl<char> &Res) {
64 for (unsigned i = 0, e = Target.size(); i != e; ++i) {
65 switch (Target[i]) {
66 case ' ':
67 case '\t':
68 // Escape the preceding backslashes
69 for (int j = i - 1; j >= 0 && Target[j] == '\\'; --j)
70 Res.push_back('\\');
71
72 // Escape the space/tab
73 Res.push_back('\\');
74 break;
75 case '$':
76 Res.push_back('$');
77 break;
78 case '#':
79 Res.push_back('\\');
80 break;
81 default:
82 break;
83 }
84
85 Res.push_back(Target[i]);
86 }
87}
88
Daniel Dunbar2008fee2010-09-17 00:24:54 +000089static void AddLinkerInputs(const ToolChain &TC,
90 const InputInfoList &Inputs, const ArgList &Args,
91 ArgStringList &CmdArgs) {
92 const Driver &D = TC.getDriver();
93
94 for (InputInfoList::const_iterator
95 it = Inputs.begin(), ie = Inputs.end(); it != ie; ++it) {
96 const InputInfo &II = *it;
97
98 if (!TC.HasNativeLLVMSupport()) {
99 // Don't try to pass LLVM inputs unless we have native support.
100 if (II.getType() == types::TY_LLVM_IR ||
101 II.getType() == types::TY_LTO_IR ||
102 II.getType() == types::TY_LLVM_BC ||
103 II.getType() == types::TY_LTO_BC)
104 D.Diag(clang::diag::err_drv_no_linker_llvm_support)
105 << TC.getTripleString();
106 }
107
Daniel Dunbare5a37f42010-09-17 00:45:02 +0000108 // Add filenames immediately.
109 if (II.isFilename()) {
Daniel Dunbar2008fee2010-09-17 00:24:54 +0000110 CmdArgs.push_back(II.getFilename());
Daniel Dunbare5a37f42010-09-17 00:45:02 +0000111 continue;
112 }
113
114 // Otherwise, this is a linker input argument.
115 const Arg &A = II.getInputArg();
116
117 // Handle reserved library options.
118 if (A.getOption().matches(options::OPT_Z_reserved_lib_stdcxx)) {
Daniel Dunbar132e35d2010-09-17 01:20:05 +0000119 TC.AddCXXStdlibLibArgs(Args, CmdArgs);
Shantonu Sen7433fed2010-09-17 18:39:08 +0000120 } else if (A.getOption().matches(options::OPT_Z_reserved_lib_cckext)) {
121 TC.AddCCKextLibArgs(Args, CmdArgs);
Daniel Dunbare5a37f42010-09-17 00:45:02 +0000122 } else
123 A.renderAsInput(Args, CmdArgs);
Daniel Dunbar2008fee2010-09-17 00:24:54 +0000124 }
125}
126
Mike Stump1eb44332009-09-09 15:08:12 +0000127void Clang::AddPreprocessingOptions(const Driver &D,
Douglas Gregordf91ef32009-04-18 00:34:01 +0000128 const ArgList &Args,
Daniel Dunbarc21c4852009-04-08 23:54:23 +0000129 ArgStringList &CmdArgs,
130 const InputInfo &Output,
131 const InputInfoList &Inputs) const {
Daniel Dunbarc21c4852009-04-08 23:54:23 +0000132 Arg *A;
Daniel Dunbar3a183d32009-06-08 21:48:20 +0000133
Daniel Dunbar88a3d6c2009-09-10 01:21:05 +0000134 CheckPreprocessingOptions(D, Args);
135
136 Args.AddLastArg(CmdArgs, options::OPT_C);
137 Args.AddLastArg(CmdArgs, options::OPT_CC);
Daniel Dunbar3a183d32009-06-08 21:48:20 +0000138
139 // Handle dependency file generation.
Daniel Dunbar9eb93b02010-12-08 21:33:40 +0000140 if ((A = Args.getLastArg(options::OPT_M, options::OPT_MM)) ||
Daniel Dunbarc21c4852009-04-08 23:54:23 +0000141 (A = Args.getLastArg(options::OPT_MD)) ||
142 (A = Args.getLastArg(options::OPT_MMD))) {
143 // Determine the output location.
144 const char *DepFile;
145 if (Output.getType() == types::TY_Dependencies) {
Daniel Dunbar7c1e4652010-08-02 02:38:21 +0000146 DepFile = Output.getFilename();
Daniel Dunbarc21c4852009-04-08 23:54:23 +0000147 } else if (Arg *MF = Args.getLastArg(options::OPT_MF)) {
148 DepFile = MF->getValue(Args);
Daniel Dunbarb827a052009-11-19 03:26:40 +0000149 } else if (A->getOption().matches(options::OPT_M) ||
150 A->getOption().matches(options::OPT_MM)) {
Daniel Dunbarc21c4852009-04-08 23:54:23 +0000151 DepFile = "-";
152 } else {
153 DepFile = darwin::CC1::getDependencyFileName(Args, Inputs);
154 }
155 CmdArgs.push_back("-dependency-file");
156 CmdArgs.push_back(DepFile);
157
Chris Lattner3edbeb72010-03-29 17:55:58 +0000158 // Add a default target if one wasn't specified.
Daniel Dunbarc21c4852009-04-08 23:54:23 +0000159 if (!Args.hasArg(options::OPT_MT) && !Args.hasArg(options::OPT_MQ)) {
160 const char *DepTarget;
161
162 // If user provided -o, that is the dependency target, except
163 // when we are only generating a dependency file.
164 Arg *OutputOpt = Args.getLastArg(options::OPT_o);
165 if (OutputOpt && Output.getType() != types::TY_Dependencies) {
166 DepTarget = OutputOpt->getValue(Args);
167 } else {
168 // Otherwise derive from the base input.
169 //
170 // FIXME: This should use the computed output file location.
Michael J. Spencer472ccff2010-12-18 00:19:12 +0000171 llvm::SmallString<128> P(Inputs[0].getBaseInput());
172 llvm::sys::path::replace_extension(P, "o");
173 DepTarget = Args.MakeArgString(llvm::sys::path::filename(P));
Daniel Dunbarc21c4852009-04-08 23:54:23 +0000174 }
175
176 CmdArgs.push_back("-MT");
Chris Lattner3edbeb72010-03-29 17:55:58 +0000177 llvm::SmallString<128> Quoted;
178 QuoteTarget(DepTarget, Quoted);
179 CmdArgs.push_back(Args.MakeArgString(Quoted));
Daniel Dunbarc21c4852009-04-08 23:54:23 +0000180 }
181
Daniel Dunbarb827a052009-11-19 03:26:40 +0000182 if (A->getOption().matches(options::OPT_M) ||
183 A->getOption().matches(options::OPT_MD))
Daniel Dunbarc21c4852009-04-08 23:54:23 +0000184 CmdArgs.push_back("-sys-header-deps");
185 }
186
187 Args.AddLastArg(CmdArgs, options::OPT_MP);
Chris Lattner3edbeb72010-03-29 17:55:58 +0000188
189 // Convert all -MQ <target> args to -MT <quoted target>
190 for (arg_iterator it = Args.filtered_begin(options::OPT_MT,
191 options::OPT_MQ),
192 ie = Args.filtered_end(); it != ie; ++it) {
Daniel Dunbar7e4953e2010-06-11 22:00:13 +0000193 const Arg *A = *it;
194 A->claim();
Chris Lattner3edbeb72010-03-29 17:55:58 +0000195
Daniel Dunbar7e4953e2010-06-11 22:00:13 +0000196 if (A->getOption().matches(options::OPT_MQ)) {
Chris Lattner3edbeb72010-03-29 17:55:58 +0000197 CmdArgs.push_back("-MT");
198 llvm::SmallString<128> Quoted;
Daniel Dunbar7e4953e2010-06-11 22:00:13 +0000199 QuoteTarget(A->getValue(Args), Quoted);
Chris Lattner3edbeb72010-03-29 17:55:58 +0000200 CmdArgs.push_back(Args.MakeArgString(Quoted));
201
202 // -MT flag - no change
203 } else {
Daniel Dunbar7e4953e2010-06-11 22:00:13 +0000204 A->render(Args, CmdArgs);
Chris Lattner3edbeb72010-03-29 17:55:58 +0000205 }
206 }
Daniel Dunbarc21c4852009-04-08 23:54:23 +0000207
Douglas Gregordf91ef32009-04-18 00:34:01 +0000208 // Add -i* options, and automatically translate to
209 // -include-pch/-include-pth for transparent PCH support. It's
210 // wonky, but we include looking for .gch so we can support seamless
211 // replacement into a build system already set up to be generating
212 // .gch files.
Argyrios Kyrtzidis990142a2010-09-30 16:53:47 +0000213 bool RenderedImplicitInclude = false;
Daniel Dunbarcdd96862009-11-25 11:53:23 +0000214 for (arg_iterator it = Args.filtered_begin(options::OPT_clang_i_Group),
215 ie = Args.filtered_end(); it != ie; ++it) {
216 const Arg *A = it;
Daniel Dunbarc21c4852009-04-08 23:54:23 +0000217
218 if (A->getOption().matches(options::OPT_include)) {
Argyrios Kyrtzidis990142a2010-09-30 16:53:47 +0000219 bool IsFirstImplicitInclude = !RenderedImplicitInclude;
220 RenderedImplicitInclude = true;
221
Argyrios Kyrtzidise5c35372010-08-11 23:27:58 +0000222 // Use PCH if the user requested it.
Daniel Dunbar0ebd9322009-10-15 20:02:44 +0000223 bool UsePCH = D.CCCUsePCH;
Daniel Dunbar0ebd9322009-10-15 20:02:44 +0000224
Daniel Dunbarc21c4852009-04-08 23:54:23 +0000225 bool FoundPTH = false;
Douglas Gregordf91ef32009-04-18 00:34:01 +0000226 bool FoundPCH = false;
Daniel Dunbarc21c4852009-04-08 23:54:23 +0000227 llvm::sys::Path P(A->getValue(Args));
Michael J. Spencer32bef4e2011-01-10 02:34:13 +0000228 bool Exists;
Daniel Dunbar0ebd9322009-10-15 20:02:44 +0000229 if (UsePCH) {
Douglas Gregordf91ef32009-04-18 00:34:01 +0000230 P.appendSuffix("pch");
Michael J. Spencer32bef4e2011-01-10 02:34:13 +0000231 if (!llvm::sys::fs::exists(P.str(), Exists) && Exists)
Douglas Gregordf91ef32009-04-18 00:34:01 +0000232 FoundPCH = true;
Mike Stump1eb44332009-09-09 15:08:12 +0000233 else
Douglas Gregordf91ef32009-04-18 00:34:01 +0000234 P.eraseSuffix();
Daniel Dunbarc21c4852009-04-08 23:54:23 +0000235 }
236
Douglas Gregordf91ef32009-04-18 00:34:01 +0000237 if (!FoundPCH) {
238 P.appendSuffix("pth");
Michael J. Spencer32bef4e2011-01-10 02:34:13 +0000239 if (!llvm::sys::fs::exists(P.str(), Exists) && Exists)
Douglas Gregordf91ef32009-04-18 00:34:01 +0000240 FoundPTH = true;
241 else
242 P.eraseSuffix();
Mike Stump1eb44332009-09-09 15:08:12 +0000243 }
244
Douglas Gregordf91ef32009-04-18 00:34:01 +0000245 if (!FoundPCH && !FoundPTH) {
246 P.appendSuffix("gch");
Michael J. Spencer32bef4e2011-01-10 02:34:13 +0000247 if (!llvm::sys::fs::exists(P.str(), Exists) && Exists) {
Daniel Dunbar0ebd9322009-10-15 20:02:44 +0000248 FoundPCH = UsePCH;
249 FoundPTH = !UsePCH;
Douglas Gregordf91ef32009-04-18 00:34:01 +0000250 }
Mike Stump1eb44332009-09-09 15:08:12 +0000251 else
Douglas Gregordf91ef32009-04-18 00:34:01 +0000252 P.eraseSuffix();
253 }
254
255 if (FoundPCH || FoundPTH) {
Argyrios Kyrtzidis990142a2010-09-30 16:53:47 +0000256 if (IsFirstImplicitInclude) {
257 A->claim();
258 if (UsePCH)
259 CmdArgs.push_back("-include-pch");
260 else
261 CmdArgs.push_back("-include-pth");
262 CmdArgs.push_back(Args.MakeArgString(P.str()));
263 continue;
264 } else {
265 // Ignore the PCH if not first on command line and emit warning.
266 D.Diag(clang::diag::warn_drv_pch_not_first_include)
267 << P.str() << A->getAsString(Args);
268 }
Daniel Dunbarc21c4852009-04-08 23:54:23 +0000269 }
270 }
271
272 // Not translated, render as usual.
273 A->claim();
274 A->render(Args, CmdArgs);
275 }
276
277 Args.AddAllArgs(CmdArgs, options::OPT_D, options::OPT_U);
278 Args.AddAllArgs(CmdArgs, options::OPT_I_Group, options::OPT_F);
279
Daniel Dunbar641b98b2010-09-14 23:12:35 +0000280 // Add C++ include arguments, if needed.
281 types::ID InputType = Inputs[0].getType();
282 if (types::isCXX(InputType))
283 getToolChain().AddClangCXXStdlibIncludeArgs(Args, CmdArgs);
284
Daniel Dunbarc21c4852009-04-08 23:54:23 +0000285 // Add -Wp, and -Xassembler if using the preprocessor.
286
287 // FIXME: There is a very unfortunate problem here, some troubled
288 // souls abuse -Wp, to pass preprocessor options in gcc syntax. To
289 // really support that we would have to parse and then translate
290 // those options. :(
291 Args.AddAllArgValues(CmdArgs, options::OPT_Wp_COMMA,
292 options::OPT_Xpreprocessor);
Daniel Dunbar607d7f62009-10-29 01:53:44 +0000293
294 // -I- is a deprecated GCC feature, reject it.
295 if (Arg *A = Args.getLastArg(options::OPT_I_))
296 D.Diag(clang::diag::err_drv_I_dash_not_supported) << A->getAsString(Args);
Chandler Carruthfeee58c2010-10-20 07:00:47 +0000297
298 // If we have a --sysroot, and don't have an explicit -isysroot flag, add an
299 // -isysroot to the CC1 invocation.
300 if (Arg *A = Args.getLastArg(options::OPT__sysroot_EQ)) {
301 if (!Args.hasArg(options::OPT_isysroot)) {
302 CmdArgs.push_back("-isysroot");
303 CmdArgs.push_back(A->getValue(Args));
304 }
305 }
Daniel Dunbarc21c4852009-04-08 23:54:23 +0000306}
307
Daniel Dunbar728a5122009-09-10 06:49:20 +0000308/// getARMTargetCPU - Get the (LLVM) name of the ARM cpu we are targetting.
309//
310// FIXME: tblgen this.
Rafael Espindolabcd6df62010-06-28 17:18:09 +0000311static const char *getARMTargetCPU(const ArgList &Args,
312 const llvm::Triple &Triple) {
Daniel Dunbar728a5122009-09-10 06:49:20 +0000313 // FIXME: Warn on inconsistent use of -mcpu and -march.
Daniel Dunbarb163ef72009-09-10 04:57:17 +0000314
315 // If we have -mcpu=, use that.
316 if (Arg *A = Args.getLastArg(options::OPT_mcpu_EQ))
317 return A->getValue(Args);
318
Rafael Espindolabcd6df62010-06-28 17:18:09 +0000319 llvm::StringRef MArch;
Daniel Dunbarb163ef72009-09-10 04:57:17 +0000320 if (Arg *A = Args.getLastArg(options::OPT_march_EQ)) {
Rafael Espindolabcd6df62010-06-28 17:18:09 +0000321 // Otherwise, if we have -march= choose the base CPU for that arch.
322 MArch = A->getValue(Args);
323 } else {
324 // Otherwise, use the Arch from the triple.
325 MArch = Triple.getArchName();
Daniel Dunbarb163ef72009-09-10 04:57:17 +0000326 }
327
Rafael Espindolabcd6df62010-06-28 17:18:09 +0000328 if (MArch == "armv2" || MArch == "armv2a")
329 return "arm2";
330 if (MArch == "armv3")
331 return "arm6";
332 if (MArch == "armv3m")
333 return "arm7m";
334 if (MArch == "armv4" || MArch == "armv4t")
335 return "arm7tdmi";
336 if (MArch == "armv5" || MArch == "armv5t")
337 return "arm10tdmi";
338 if (MArch == "armv5e" || MArch == "armv5te")
339 return "arm1026ejs";
340 if (MArch == "armv5tej")
341 return "arm926ej-s";
342 if (MArch == "armv6" || MArch == "armv6k")
343 return "arm1136jf-s";
344 if (MArch == "armv6j")
345 return "arm1136j-s";
346 if (MArch == "armv6z" || MArch == "armv6zk")
347 return "arm1176jzf-s";
348 if (MArch == "armv6t2")
349 return "arm1156t2-s";
350 if (MArch == "armv7" || MArch == "armv7a" || MArch == "armv7-a")
351 return "cortex-a8";
352 if (MArch == "armv7r" || MArch == "armv7-r")
353 return "cortex-r4";
354 if (MArch == "armv7m" || MArch == "armv7-m")
355 return "cortex-m3";
356 if (MArch == "ep9312")
357 return "ep9312";
358 if (MArch == "iwmmxt")
359 return "iwmmxt";
360 if (MArch == "xscale")
361 return "xscale";
362
363 // If all else failed, return the most base CPU LLVM supports.
Daniel Dunbarb163ef72009-09-10 04:57:17 +0000364 return "arm7tdmi";
365}
366
Daniel Dunbar1d65e4b2009-09-10 22:59:51 +0000367/// getLLVMArchSuffixForARM - Get the LLVM arch name to use for a particular
Daniel Dunbar728a5122009-09-10 06:49:20 +0000368/// CPU.
369//
370// FIXME: This is redundant with -mcpu, why does LLVM use this.
371// FIXME: tblgen this, or kill it!
Daniel Dunbar1d65e4b2009-09-10 22:59:51 +0000372static const char *getLLVMArchSuffixForARM(llvm::StringRef CPU) {
Daniel Dunbar728a5122009-09-10 06:49:20 +0000373 if (CPU == "arm7tdmi" || CPU == "arm7tdmi-s" || CPU == "arm710t" ||
374 CPU == "arm720t" || CPU == "arm9" || CPU == "arm9tdmi" ||
375 CPU == "arm920" || CPU == "arm920t" || CPU == "arm922t" ||
376 CPU == "arm940t" || CPU == "ep9312")
Daniel Dunbar1d65e4b2009-09-10 22:59:51 +0000377 return "v4t";
Daniel Dunbar728a5122009-09-10 06:49:20 +0000378
379 if (CPU == "arm10tdmi" || CPU == "arm1020t")
Daniel Dunbar1d65e4b2009-09-10 22:59:51 +0000380 return "v5";
Daniel Dunbar728a5122009-09-10 06:49:20 +0000381
382 if (CPU == "arm9e" || CPU == "arm926ej-s" || CPU == "arm946e-s" ||
383 CPU == "arm966e-s" || CPU == "arm968e-s" || CPU == "arm10e" ||
384 CPU == "arm1020e" || CPU == "arm1022e" || CPU == "xscale" ||
385 CPU == "iwmmxt")
Daniel Dunbar1d65e4b2009-09-10 22:59:51 +0000386 return "v5e";
Daniel Dunbar728a5122009-09-10 06:49:20 +0000387
388 if (CPU == "arm1136j-s" || CPU == "arm1136jf-s" || CPU == "arm1176jz-s" ||
389 CPU == "arm1176jzf-s" || CPU == "mpcorenovfp" || CPU == "mpcore")
Daniel Dunbar1d65e4b2009-09-10 22:59:51 +0000390 return "v6";
Daniel Dunbar728a5122009-09-10 06:49:20 +0000391
392 if (CPU == "arm1156t2-s" || CPU == "arm1156t2f-s")
Daniel Dunbar1d65e4b2009-09-10 22:59:51 +0000393 return "v6t2";
Daniel Dunbar728a5122009-09-10 06:49:20 +0000394
395 if (CPU == "cortex-a8" || CPU == "cortex-a9")
Daniel Dunbar1d65e4b2009-09-10 22:59:51 +0000396 return "v7";
Daniel Dunbar728a5122009-09-10 06:49:20 +0000397
Daniel Dunbar1d65e4b2009-09-10 22:59:51 +0000398 return "";
Daniel Dunbar728a5122009-09-10 06:49:20 +0000399}
400
Daniel Dunbar1f95e652009-11-17 06:37:03 +0000401// FIXME: Move to target hook.
402static bool isSignedCharDefault(const llvm::Triple &Triple) {
403 switch (Triple.getArch()) {
404 default:
405 return true;
406
407 case llvm::Triple::ppc:
408 case llvm::Triple::ppc64:
409 if (Triple.getOS() == llvm::Triple::Darwin)
410 return true;
411 return false;
412
413 case llvm::Triple::systemz:
414 return false;
415 }
416}
417
Daniel Dunbarb163ef72009-09-10 04:57:17 +0000418void Clang::AddARMTargetArgs(const ArgList &Args,
419 ArgStringList &CmdArgs) const {
Daniel Dunbaree788e72009-12-21 18:54:17 +0000420 const Driver &D = getToolChain().getDriver();
Rafael Espindolabcd6df62010-06-28 17:18:09 +0000421 llvm::Triple Triple = getToolChain().getTriple();
Daniel Dunbarcbd19332009-09-10 23:00:09 +0000422
Daniel Dunbar2030d8f2009-09-14 00:34:46 +0000423 // Select the ABI to use.
424 //
425 // FIXME: Support -meabi.
426 const char *ABIName = 0;
427 if (Arg *A = Args.getLastArg(options::OPT_mabi_EQ)) {
428 ABIName = A->getValue(Args);
429 } else {
430 // Select the default based on the platform.
Rafael Espindolabcd6df62010-06-28 17:18:09 +0000431 llvm::StringRef env = Triple.getEnvironmentName();
Rafael Espindola1ed1a592010-06-16 19:01:17 +0000432 if (env == "gnueabi")
Daniel Dunbar2030d8f2009-09-14 00:34:46 +0000433 ABIName = "aapcs-linux";
Rafael Espindola1ed1a592010-06-16 19:01:17 +0000434 else if (env == "eabi")
435 ABIName = "aapcs";
436 else
437 ABIName = "apcs-gnu";
Daniel Dunbar2030d8f2009-09-14 00:34:46 +0000438 }
439 CmdArgs.push_back("-target-abi");
440 CmdArgs.push_back(ABIName);
441
Daniel Dunbarb163ef72009-09-10 04:57:17 +0000442 // Set the CPU based on -march= and -mcpu=.
Daniel Dunbar38b48af2009-12-18 06:30:12 +0000443 CmdArgs.push_back("-target-cpu");
Rafael Espindolabcd6df62010-06-28 17:18:09 +0000444 CmdArgs.push_back(getARMTargetCPU(Args, Triple));
Daniel Dunbarb163ef72009-09-10 04:57:17 +0000445
Daniel Dunbarcbd19332009-09-10 23:00:09 +0000446 // Select the float ABI as determined by -msoft-float, -mhard-float, and
447 // -mfloat-abi=.
448 llvm::StringRef FloatABI;
449 if (Arg *A = Args.getLastArg(options::OPT_msoft_float,
450 options::OPT_mhard_float,
451 options::OPT_mfloat_abi_EQ)) {
452 if (A->getOption().matches(options::OPT_msoft_float))
453 FloatABI = "soft";
454 else if (A->getOption().matches(options::OPT_mhard_float))
455 FloatABI = "hard";
456 else {
457 FloatABI = A->getValue(Args);
458 if (FloatABI != "soft" && FloatABI != "softfp" && FloatABI != "hard") {
459 D.Diag(clang::diag::err_drv_invalid_mfloat_abi)
460 << A->getAsString(Args);
461 FloatABI = "soft";
462 }
463 }
464 }
465
466 // If unspecified, choose the default based on the platform.
467 if (FloatABI.empty()) {
Rafael Espindolabcd6df62010-06-28 17:18:09 +0000468 const llvm::Triple &Triple = getToolChain().getTriple();
469 switch (Triple.getOS()) {
Daniel Dunbarcbd19332009-09-10 23:00:09 +0000470 case llvm::Triple::Darwin: {
471 // Darwin defaults to "softfp" for v6 and v7.
472 //
473 // FIXME: Factor out an ARM class so we can cache the arch somewhere.
Rafael Espindolabcd6df62010-06-28 17:18:09 +0000474 llvm::StringRef ArchName =
475 getLLVMArchSuffixForARM(getARMTargetCPU(Args, Triple));
Daniel Dunbarcbd19332009-09-10 23:00:09 +0000476 if (ArchName.startswith("v6") || ArchName.startswith("v7"))
477 FloatABI = "softfp";
478 else
479 FloatABI = "soft";
480 break;
481 }
482
Rafael Espindola7e9cebf2010-06-27 18:29:21 +0000483 case llvm::Triple::Linux: {
484 llvm::StringRef Env = getToolChain().getTriple().getEnvironmentName();
485 if (Env == "gnueabi") {
486 FloatABI = "softfp";
487 break;
488 }
489 }
490 // fall through
491
Daniel Dunbarcbd19332009-09-10 23:00:09 +0000492 default:
493 // Assume "soft", but warn the user we are guessing.
494 FloatABI = "soft";
495 D.Diag(clang::diag::warn_drv_assuming_mfloat_abi_is) << "soft";
496 break;
497 }
498 }
499
500 if (FloatABI == "soft") {
501 // Floating point operations and argument passing are soft.
502 //
503 // FIXME: This changes CPP defines, we need -target-soft-float.
Daniel Dunbar3b315262009-11-30 08:42:00 +0000504 CmdArgs.push_back("-msoft-float");
Daniel Dunbar87667aa2009-12-08 19:49:51 +0000505 CmdArgs.push_back("-mfloat-abi");
506 CmdArgs.push_back("soft");
Daniel Dunbarcbd19332009-09-10 23:00:09 +0000507 } else if (FloatABI == "softfp") {
508 // Floating point operations are hard, but argument passing is soft.
Daniel Dunbar87667aa2009-12-08 19:49:51 +0000509 CmdArgs.push_back("-mfloat-abi");
510 CmdArgs.push_back("soft");
Daniel Dunbarcbd19332009-09-10 23:00:09 +0000511 } else {
512 // Floating point operations and argument passing are hard.
513 assert(FloatABI == "hard" && "Invalid float abi!");
Daniel Dunbar87667aa2009-12-08 19:49:51 +0000514 CmdArgs.push_back("-mfloat-abi");
515 CmdArgs.push_back("hard");
Daniel Dunbarcbd19332009-09-10 23:00:09 +0000516 }
Daniel Dunbar97f52ac2009-12-19 04:15:38 +0000517
518 // Set appropriate target features for floating point mode.
519 //
520 // FIXME: Note, this is a hack, the LLVM backend doesn't actually use these
521 // yet (it uses the -mfloat-abi and -msoft-float options above), and it is
522 // stripped out by the ARM target.
523
524 // Use software floating point operations?
525 if (FloatABI == "soft") {
526 CmdArgs.push_back("-target-feature");
527 CmdArgs.push_back("+soft-float");
528 }
529
530 // Use software floating point argument passing?
531 if (FloatABI != "hard") {
532 CmdArgs.push_back("-target-feature");
533 CmdArgs.push_back("+soft-float-abi");
534 }
Daniel Dunbara91320b2009-12-21 23:28:17 +0000535
536 // Honor -mfpu=.
537 //
538 // FIXME: Centralize feature selection, defaulting shouldn't be also in the
539 // frontend target.
540 if (const Arg *A = Args.getLastArg(options::OPT_mfpu_EQ)) {
541 llvm::StringRef FPU = A->getValue(Args);
542
543 // Set the target features based on the FPU.
544 if (FPU == "fpa" || FPU == "fpe2" || FPU == "fpe3" || FPU == "maverick") {
545 // Disable any default FPU support.
546 CmdArgs.push_back("-target-feature");
547 CmdArgs.push_back("-vfp2");
548 CmdArgs.push_back("-target-feature");
549 CmdArgs.push_back("-vfp3");
550 CmdArgs.push_back("-target-feature");
551 CmdArgs.push_back("-neon");
552 } else if (FPU == "vfp") {
553 CmdArgs.push_back("-target-feature");
554 CmdArgs.push_back("+vfp2");
555 } else if (FPU == "vfp3") {
556 CmdArgs.push_back("-target-feature");
557 CmdArgs.push_back("+vfp3");
558 } else if (FPU == "neon") {
559 CmdArgs.push_back("-target-feature");
560 CmdArgs.push_back("+neon");
561 } else
562 D.Diag(clang::diag::err_drv_clang_unsupported) << A->getAsString(Args);
563 }
Daniel Dunbarb163ef72009-09-10 04:57:17 +0000564}
565
Eric Christophered734732010-03-02 02:41:08 +0000566void Clang::AddMIPSTargetArgs(const ArgList &Args,
567 ArgStringList &CmdArgs) const {
568 const Driver &D = getToolChain().getDriver();
569
570 // Select the ABI to use.
571 const char *ABIName = 0;
572 if (Arg *A = Args.getLastArg(options::OPT_mabi_EQ)) {
573 ABIName = A->getValue(Args);
574 } else {
575 ABIName = "o32";
576 }
577
578 CmdArgs.push_back("-target-abi");
579 CmdArgs.push_back(ABIName);
580
581 if (const Arg *A = Args.getLastArg(options::OPT_march_EQ)) {
582 llvm::StringRef MArch = A->getValue(Args);
583 CmdArgs.push_back("-target-cpu");
584
585 if ((MArch == "r2000") || (MArch == "r3000"))
586 CmdArgs.push_back("mips1");
587 else if (MArch == "r6000")
588 CmdArgs.push_back("mips2");
589 else
590 CmdArgs.push_back(MArch.str().c_str());
591 }
592
593 // Select the float ABI as determined by -msoft-float, -mhard-float, and
594 llvm::StringRef FloatABI;
595 if (Arg *A = Args.getLastArg(options::OPT_msoft_float,
596 options::OPT_mhard_float)) {
597 if (A->getOption().matches(options::OPT_msoft_float))
598 FloatABI = "soft";
599 else if (A->getOption().matches(options::OPT_mhard_float))
600 FloatABI = "hard";
601 }
602
603 // If unspecified, choose the default based on the platform.
604 if (FloatABI.empty()) {
Benjamin Kramer71c972a2010-04-08 15:44:22 +0000605 // Assume "soft", but warn the user we are guessing.
606 FloatABI = "soft";
607 D.Diag(clang::diag::warn_drv_assuming_mfloat_abi_is) << "soft";
Eric Christophered734732010-03-02 02:41:08 +0000608 }
609
610 if (FloatABI == "soft") {
611 // Floating point operations and argument passing are soft.
612 //
613 // FIXME: This changes CPP defines, we need -target-soft-float.
614 CmdArgs.push_back("-msoft-float");
615 } else {
616 assert(FloatABI == "hard" && "Invalid float abi!");
617 CmdArgs.push_back("-mhard-float");
618 }
619}
620
Bruno Cardoso Lopes9284d212010-11-09 17:21:19 +0000621void Clang::AddSparcTargetArgs(const ArgList &Args,
622 ArgStringList &CmdArgs) const {
623 const Driver &D = getToolChain().getDriver();
624
625 if (const Arg *A = Args.getLastArg(options::OPT_march_EQ)) {
626 llvm::StringRef MArch = A->getValue(Args);
627 CmdArgs.push_back("-target-cpu");
628 CmdArgs.push_back(MArch.str().c_str());
629 }
630
631 // Select the float ABI as determined by -msoft-float, -mhard-float, and
632 llvm::StringRef FloatABI;
633 if (Arg *A = Args.getLastArg(options::OPT_msoft_float,
634 options::OPT_mhard_float)) {
635 if (A->getOption().matches(options::OPT_msoft_float))
636 FloatABI = "soft";
637 else if (A->getOption().matches(options::OPT_mhard_float))
638 FloatABI = "hard";
639 }
640
641 // If unspecified, choose the default based on the platform.
642 if (FloatABI.empty()) {
643 switch (getToolChain().getTriple().getOS()) {
644 default:
645 // Assume "soft", but warn the user we are guessing.
646 FloatABI = "soft";
647 D.Diag(clang::diag::warn_drv_assuming_mfloat_abi_is) << "soft";
648 break;
649 }
650 }
651
652 if (FloatABI == "soft") {
653 // Floating point operations and argument passing are soft.
654 //
655 // FIXME: This changes CPP defines, we need -target-soft-float.
656 CmdArgs.push_back("-msoft-float");
657 CmdArgs.push_back("soft");
658 CmdArgs.push_back("-target-feature");
659 CmdArgs.push_back("+soft-float");
660 } else {
661 assert(FloatABI == "hard" && "Invalid float abi!");
662 CmdArgs.push_back("-mhard-float");
663 }
664}
665
Daniel Dunbar6acda162009-09-09 22:33:08 +0000666void Clang::AddX86TargetArgs(const ArgList &Args,
667 ArgStringList &CmdArgs) const {
Daniel Dunbare6ad3f92009-09-10 22:59:57 +0000668 if (!Args.hasFlag(options::OPT_mred_zone,
669 options::OPT_mno_red_zone,
670 true) ||
671 Args.hasArg(options::OPT_mkernel) ||
672 Args.hasArg(options::OPT_fapple_kext))
Daniel Dunbar66861e02009-11-20 22:21:36 +0000673 CmdArgs.push_back("-disable-red-zone");
Daniel Dunbare6ad3f92009-09-10 22:59:57 +0000674
Daniel Dunbare6ad3f92009-09-10 22:59:57 +0000675 if (Args.hasFlag(options::OPT_msoft_float,
676 options::OPT_mno_soft_float,
677 false))
Daniel Dunbar66861e02009-11-20 22:21:36 +0000678 CmdArgs.push_back("-no-implicit-float");
Daniel Dunbare6ad3f92009-09-10 22:59:57 +0000679
Daniel Dunbarf86fedd2009-11-14 22:04:54 +0000680 const char *CPUName = 0;
Daniel Dunbar6acda162009-09-09 22:33:08 +0000681 if (const Arg *A = Args.getLastArg(options::OPT_march_EQ)) {
Daniel Dunbarf86fedd2009-11-14 22:04:54 +0000682 if (llvm::StringRef(A->getValue(Args)) == "native") {
683 // FIXME: Reject attempts to use -march=native unless the target matches
684 // the host.
685 //
686 // FIXME: We should also incorporate the detected target features for use
687 // with -native.
688 std::string CPU = llvm::sys::getHostCPUName();
689 if (!CPU.empty())
690 CPUName = Args.MakeArgString(CPU);
691 } else
692 CPUName = A->getValue(Args);
693 }
Daniel Dunbar6acda162009-09-09 22:33:08 +0000694
Daniel Dunbarf86fedd2009-11-14 22:04:54 +0000695 // Select the default CPU if none was given (or detection failed).
696 if (!CPUName) {
Daniel Dunbar6acda162009-09-09 22:33:08 +0000697 // FIXME: Need target hooks.
Benjamin Kramerf22d1fd2010-01-30 15:01:47 +0000698 if (getToolChain().getOS().startswith("darwin")) {
Daniel Dunbar6acda162009-09-09 22:33:08 +0000699 if (getToolChain().getArchName() == "x86_64")
Daniel Dunbarf86fedd2009-11-14 22:04:54 +0000700 CPUName = "core2";
Daniel Dunbar6acda162009-09-09 22:33:08 +0000701 else if (getToolChain().getArchName() == "i386")
Daniel Dunbarf86fedd2009-11-14 22:04:54 +0000702 CPUName = "yonah";
Chris Lattner86ed3a32010-04-11 19:29:39 +0000703 } else if (getToolChain().getOS().startswith("haiku")) {
704 if (getToolChain().getArchName() == "x86_64")
705 CPUName = "x86-64";
706 else if (getToolChain().getArchName() == "i386")
707 CPUName = "i586";
Daniel Dunbar95c04572010-08-01 23:13:54 +0000708 } else if (getToolChain().getOS().startswith("openbsd")) {
709 if (getToolChain().getArchName() == "x86_64")
710 CPUName = "x86-64";
711 else if (getToolChain().getArchName() == "i386")
712 CPUName = "i486";
Daniel Dunbar6acda162009-09-09 22:33:08 +0000713 } else {
714 if (getToolChain().getArchName() == "x86_64")
Daniel Dunbarf86fedd2009-11-14 22:04:54 +0000715 CPUName = "x86-64";
Daniel Dunbar6acda162009-09-09 22:33:08 +0000716 else if (getToolChain().getArchName() == "i386")
Daniel Dunbarf86fedd2009-11-14 22:04:54 +0000717 CPUName = "pentium4";
Daniel Dunbar6acda162009-09-09 22:33:08 +0000718 }
719 }
720
Daniel Dunbarf86fedd2009-11-14 22:04:54 +0000721 if (CPUName) {
Daniel Dunbar38b48af2009-12-18 06:30:12 +0000722 CmdArgs.push_back("-target-cpu");
Daniel Dunbarf86fedd2009-11-14 22:04:54 +0000723 CmdArgs.push_back(CPUName);
724 }
725
Daniel Dunbarcdd96862009-11-25 11:53:23 +0000726 for (arg_iterator it = Args.filtered_begin(options::OPT_m_x86_Features_Group),
727 ie = Args.filtered_end(); it != ie; ++it) {
Daniel Dunbar7e4953e2010-06-11 22:00:13 +0000728 llvm::StringRef Name = (*it)->getOption().getName();
729 (*it)->claim();
Daniel Dunbar6acda162009-09-09 22:33:08 +0000730
Daniel Dunbarcdd96862009-11-25 11:53:23 +0000731 // Skip over "-m".
732 assert(Name.startswith("-m") && "Invalid feature name.");
733 Name = Name.substr(2);
Daniel Dunbar6acda162009-09-09 22:33:08 +0000734
Daniel Dunbarcdd96862009-11-25 11:53:23 +0000735 bool IsNegative = Name.startswith("no-");
736 if (IsNegative)
737 Name = Name.substr(3);
Daniel Dunbar6acda162009-09-09 22:33:08 +0000738
Daniel Dunbarcdd96862009-11-25 11:53:23 +0000739 CmdArgs.push_back("-target-feature");
740 CmdArgs.push_back(Args.MakeArgString((IsNegative ? "-" : "+") + Name));
Daniel Dunbar6acda162009-09-09 22:33:08 +0000741 }
742}
743
Fariborz Jahanian85caf032009-10-01 20:30:46 +0000744static bool needsExceptions(const ArgList &Args, types::ID InputType,
745 const llvm::Triple &Triple) {
Daniel Dunbar1a2cd4f2010-09-14 23:12:31 +0000746 // Handle -fno-exceptions.
Rafael Espindolaf759df02009-10-01 13:33:33 +0000747 if (Arg *A = Args.getLastArg(options::OPT_fexceptions,
748 options::OPT_fno_exceptions)) {
749 if (A->getOption().matches(options::OPT_fexceptions))
750 return true;
751 else
752 return false;
753 }
Daniel Dunbar1a2cd4f2010-09-14 23:12:31 +0000754
755 // Otherwise, C++ inputs use exceptions.
756 if (types::isCXX(InputType))
Rafael Espindolaf759df02009-10-01 13:33:33 +0000757 return true;
Fariborz Jahanian85caf032009-10-01 20:30:46 +0000758
Daniel Dunbar1a2cd4f2010-09-14 23:12:31 +0000759 // As do Objective-C non-fragile ABI inputs and all Objective-C inputs on
John McCall4a1cdb52010-10-06 01:40:51 +0000760 // x86_64 and ARM after SnowLeopard.
Daniel Dunbar1a2cd4f2010-09-14 23:12:31 +0000761 if (types::isObjC(InputType)) {
Rafael Espindolaf759df02009-10-01 13:33:33 +0000762 if (Args.hasArg(options::OPT_fobjc_nonfragile_abi))
763 return true;
Fariborz Jahanian85caf032009-10-01 20:30:46 +0000764 if (Triple.getOS() != llvm::Triple::Darwin)
Rafael Espindolaf759df02009-10-01 13:33:33 +0000765 return false;
Fariborz Jahanian85caf032009-10-01 20:30:46 +0000766 return (Triple.getDarwinMajorNumber() >= 9 &&
John McCall4a1cdb52010-10-06 01:40:51 +0000767 (Triple.getArch() == llvm::Triple::x86_64 ||
768 Triple.getArch() == llvm::Triple::arm));
Rafael Espindolaf759df02009-10-01 13:33:33 +0000769 }
Daniel Dunbar1a2cd4f2010-09-14 23:12:31 +0000770
771 return false;
Rafael Espindolaf759df02009-10-01 13:33:33 +0000772}
773
Daniel Dunbar47ac7d22009-03-18 06:00:36 +0000774void Clang::ConstructJob(Compilation &C, const JobAction &JA,
Daniel Dunbar871adcf2009-03-18 07:06:02 +0000775 const InputInfo &Output,
Daniel Dunbar62cf6012009-03-18 06:07:59 +0000776 const InputInfoList &Inputs,
Daniel Dunbar1d460332009-03-18 10:01:51 +0000777 const ArgList &Args,
Daniel Dunbar47ac7d22009-03-18 06:00:36 +0000778 const char *LinkingOutput) const {
Daniel Dunbar0a80ba72010-03-20 04:52:14 +0000779 bool KernelOrKext = Args.hasArg(options::OPT_mkernel,
780 options::OPT_fapple_kext);
Daniel Dunbaree788e72009-12-21 18:54:17 +0000781 const Driver &D = getToolChain().getDriver();
Daniel Dunbarb488c1d2009-03-18 08:07:30 +0000782 ArgStringList CmdArgs;
783
Daniel Dunbar077ba6a2009-03-31 20:53:55 +0000784 assert(Inputs.size() == 1 && "Unable to handle multiple inputs.");
785
Daniel Dunbar8ff5b282009-12-11 23:00:49 +0000786 // Invoke ourselves in -cc1 mode.
787 //
788 // FIXME: Implement custom jobs for internal actions.
789 CmdArgs.push_back("-cc1");
790
Daniel Dunbardd4fe002009-10-30 18:12:20 +0000791 // Add the "effective" target triple.
Daniel Dunbaraf07f932009-03-31 17:35:15 +0000792 CmdArgs.push_back("-triple");
Daniel Dunbar00577ad2010-08-23 22:35:37 +0000793 std::string TripleStr = getToolChain().ComputeEffectiveClangTriple(Args);
Daniel Dunbardd4fe002009-10-30 18:12:20 +0000794 CmdArgs.push_back(Args.MakeArgString(TripleStr));
Daniel Dunbar728a5122009-09-10 06:49:20 +0000795
Daniel Dunbardd4fe002009-10-30 18:12:20 +0000796 // Select the appropriate action.
Daniel Dunbar5314e442010-07-19 19:44:22 +0000797 bool IsRewriter = false;
Daniel Dunbar1d460332009-03-18 10:01:51 +0000798 if (isa<AnalyzeJobAction>(JA)) {
799 assert(JA.getType() == types::TY_Plist && "Invalid output type.");
800 CmdArgs.push_back("-analyze");
801 } else if (isa<PreprocessJobAction>(JA)) {
Daniel Dunbarcd8e4c42009-03-30 06:36:42 +0000802 if (Output.getType() == types::TY_Dependencies)
803 CmdArgs.push_back("-Eonly");
804 else
805 CmdArgs.push_back("-E");
Daniel Dunbar8767cbc2010-02-03 03:07:56 +0000806 } else if (isa<AssembleJobAction>(JA)) {
807 CmdArgs.push_back("-emit-obj");
Daniel Dunbar99298002010-05-27 06:18:05 +0000808
809 // At -O0, we use -mrelax-all by default.
810 bool IsOpt = false;
811 if (Arg *A = Args.getLastArg(options::OPT_O_Group))
812 IsOpt = !A->getOption().matches(options::OPT_O0);
813 if (Args.hasFlag(options::OPT_mrelax_all,
814 options::OPT_mno_relax_all,
815 !IsOpt))
816 CmdArgs.push_back("-mrelax-all");
Daniel Dunbarca0e0542010-08-24 16:47:49 +0000817
Daniel Dunbarfcec10b2010-10-18 22:36:15 +0000818 // When using an integrated assembler, translate -Wa, and -Xassembler
819 // options.
820 for (arg_iterator it = Args.filtered_begin(options::OPT_Wa_COMMA,
821 options::OPT_Xassembler),
822 ie = Args.filtered_end(); it != ie; ++it) {
823 const Arg *A = *it;
824 A->claim();
825
826 for (unsigned i = 0, e = A->getNumValues(); i != e; ++i) {
827 llvm::StringRef Value = A->getValue(Args, i);
828
829 if (Value == "-force_cpusubtype_ALL") {
830 // Do nothing, this is the default and we don't support anything else.
Daniel Dunbarb14eed02010-10-28 20:36:23 +0000831 } else if (Value == "-L") {
832 // We don't support -L yet, but it isn't important enough to error
833 // on. No one should really be using it for a semantic change.
834 D.Diag(clang::diag::warn_drv_unsupported_option_argument)
835 << A->getOption().getName() << Value;
Daniel Dunbarfcec10b2010-10-18 22:36:15 +0000836 } else {
837 D.Diag(clang::diag::err_drv_unsupported_option_argument)
838 << A->getOption().getName() << Value;
839 }
840 }
841 }
Daniel Dunbard02bba82010-11-19 16:23:35 +0000842
843 // Also ignore explicit -force_cpusubtype_ALL option.
844 (void) Args.hasArg(options::OPT_force__cpusubtype__ALL);
Daniel Dunbar1d460332009-03-18 10:01:51 +0000845 } else if (isa<PrecompileJobAction>(JA)) {
Argyrios Kyrtzidise5c35372010-08-11 23:27:58 +0000846 // Use PCH if the user requested it.
Daniel Dunbar0ebd9322009-10-15 20:02:44 +0000847 bool UsePCH = D.CCCUsePCH;
Daniel Dunbar0ebd9322009-10-15 20:02:44 +0000848
849 if (UsePCH)
Douglas Gregordf91ef32009-04-18 00:34:01 +0000850 CmdArgs.push_back("-emit-pch");
851 else
852 CmdArgs.push_back("-emit-pth");
Daniel Dunbar1d460332009-03-18 10:01:51 +0000853 } else {
854 assert(isa<CompileJobAction>(JA) && "Invalid action for clang tool.");
Daniel Dunbarc21c4852009-04-08 23:54:23 +0000855
Daniel Dunbar1d460332009-03-18 10:01:51 +0000856 if (JA.getType() == types::TY_Nothing) {
857 CmdArgs.push_back("-fsyntax-only");
Daniel Dunbar6c6424b2010-06-07 23:28:45 +0000858 } else if (JA.getType() == types::TY_LLVM_IR ||
859 JA.getType() == types::TY_LTO_IR) {
Daniel Dunbar1d460332009-03-18 10:01:51 +0000860 CmdArgs.push_back("-emit-llvm");
Daniel Dunbar6c6424b2010-06-07 23:28:45 +0000861 } else if (JA.getType() == types::TY_LLVM_BC ||
862 JA.getType() == types::TY_LTO_BC) {
Daniel Dunbar1d460332009-03-18 10:01:51 +0000863 CmdArgs.push_back("-emit-llvm-bc");
864 } else if (JA.getType() == types::TY_PP_Asm) {
Daniel Dunbare3b8d072009-09-17 00:47:53 +0000865 CmdArgs.push_back("-S");
Daniel Dunbar5915fbf2009-09-01 16:57:46 +0000866 } else if (JA.getType() == types::TY_AST) {
867 CmdArgs.push_back("-emit-pch");
Daniel Dunbar64952502010-02-11 03:16:21 +0000868 } else if (JA.getType() == types::TY_RewrittenObjC) {
869 CmdArgs.push_back("-rewrite-objc");
Daniel Dunbar5314e442010-07-19 19:44:22 +0000870 IsRewriter = true;
Daniel Dunbar64952502010-02-11 03:16:21 +0000871 } else {
872 assert(JA.getType() == types::TY_PP_Asm &&
873 "Unexpected output type!");
Daniel Dunbar1d460332009-03-18 10:01:51 +0000874 }
Daniel Dunbarb488c1d2009-03-18 08:07:30 +0000875 }
876
Daniel Dunbar1d460332009-03-18 10:01:51 +0000877 // The make clang go fast button.
878 CmdArgs.push_back("-disable-free");
879
John McCallb689afb2010-02-13 03:50:24 +0000880 // Disable the verification pass in -asserts builds.
881#ifdef NDEBUG
882 CmdArgs.push_back("-disable-llvm-verifier");
883#endif
884
Daniel Dunbarc9abc042009-04-08 05:11:16 +0000885 // Set the main file name, so that debug info works even with
886 // -save-temps.
887 CmdArgs.push_back("-main-file-name");
888 CmdArgs.push_back(darwin::CC1::getBaseInputName(Args, Inputs));
889
Daniel Dunbar3bbc7532009-04-08 18:03:55 +0000890 // Some flags which affect the language (via preprocessor
891 // defines). See darwin::CC1::AddCPPArgs.
892 if (Args.hasArg(options::OPT_static))
893 CmdArgs.push_back("-static-define");
894
Daniel Dunbar1d460332009-03-18 10:01:51 +0000895 if (isa<AnalyzeJobAction>(JA)) {
Ted Kremenekb8bb3e72009-09-25 05:55:59 +0000896 // Enable region store model by default.
897 CmdArgs.push_back("-analyzer-store=region");
898
Ted Kremenekb40d06d2009-12-07 22:26:14 +0000899 // Treat blocks as analysis entry points.
900 CmdArgs.push_back("-analyzer-opt-analyze-nested-blocks");
901
Daniel Dunbar1d460332009-03-18 10:01:51 +0000902 // Add default argument set.
Daniel Dunbard8fc0f22009-05-22 00:38:15 +0000903 if (!Args.hasArg(options::OPT__analyzer_no_default_checks)) {
Ted Kremenekcd9902b2010-02-05 01:52:40 +0000904 CmdArgs.push_back("-analyzer-check-dead-stores");
Ted Kremenek647aa462010-04-29 22:50:22 +0000905 // Do not enable the security-syntatic check since it
906 // it needs to be refined (known issues).
907 // CmdArgs.push_back("-analyzer-check-security-syntactic");
Ted Kremenek565e4652010-02-05 02:06:54 +0000908 CmdArgs.push_back("-analyzer-check-objc-mem");
Daniel Dunbard8fc0f22009-05-22 00:38:15 +0000909 CmdArgs.push_back("-analyzer-eagerly-assume");
Ted Kremenekfa15be42010-02-05 01:57:44 +0000910 CmdArgs.push_back("-analyzer-check-objc-methodsigs");
Daniel Dunbard8fc0f22009-05-22 00:38:15 +0000911 // Do not enable the missing -dealloc check.
Ted Kremenek7909fc82010-02-05 01:59:21 +0000912 // '-analyzer-check-objc-missing-dealloc',
Ted Kremenek2ade5362010-02-05 01:55:01 +0000913 CmdArgs.push_back("-analyzer-check-objc-unused-ivars");
Ted Kremenek79a79f52010-08-10 18:03:13 +0000914 CmdArgs.push_back("-analyzer-check-idempotent-operations");
Daniel Dunbard8fc0f22009-05-22 00:38:15 +0000915 }
Daniel Dunbarc21c4852009-04-08 23:54:23 +0000916
Daniel Dunbard8fc0f22009-05-22 00:38:15 +0000917 // Set the output format. The default is plist, for (lame) historical
918 // reasons.
919 CmdArgs.push_back("-analyzer-output");
920 if (Arg *A = Args.getLastArg(options::OPT__analyzer_output))
921 CmdArgs.push_back(A->getValue(Args));
922 else
923 CmdArgs.push_back("plist");
Daniel Dunbar1d460332009-03-18 10:01:51 +0000924
Ted Kremenek0647a7b2010-03-22 22:32:05 +0000925 // Disable the presentation of standard compiler warnings when
926 // using --analyze. We only want to show static analyzer diagnostics
927 // or frontend errors.
928 CmdArgs.push_back("-w");
929
Daniel Dunbar1d460332009-03-18 10:01:51 +0000930 // Add -Xanalyzer arguments when running as analyzer.
931 Args.AddAllArgValues(CmdArgs, options::OPT_Xanalyzer);
Mike Stump1eb44332009-09-09 15:08:12 +0000932 }
933
Daniel Dunbare2fd6642009-09-10 01:21:12 +0000934 CheckCodeGenerationOptions(D, Args);
935
Daniel Dunbarbc85be82009-04-29 18:32:25 +0000936 // Perform argument translation for LLVM backend. This
937 // takes some care in reconciling with llvm-gcc. The
938 // issue is that llvm-gcc translates these options based on
939 // the values in cc1, whereas we are processing based on
940 // the driver arguments.
Daniel Dunbarc21c4852009-04-08 23:54:23 +0000941
Daniel Dunbarbc85be82009-04-29 18:32:25 +0000942 // This comes from the default translation the driver + cc1
943 // would do to enable flag_pic.
944 //
945 // FIXME: Centralize this code.
946 bool PICEnabled = (Args.hasArg(options::OPT_fPIC) ||
947 Args.hasArg(options::OPT_fpic) ||
948 Args.hasArg(options::OPT_fPIE) ||
949 Args.hasArg(options::OPT_fpie));
950 bool PICDisabled = (Args.hasArg(options::OPT_mkernel) ||
951 Args.hasArg(options::OPT_static));
952 const char *Model = getToolChain().GetForcedPicModel();
953 if (!Model) {
954 if (Args.hasArg(options::OPT_mdynamic_no_pic))
955 Model = "dynamic-no-pic";
956 else if (PICDisabled)
957 Model = "static";
958 else if (PICEnabled)
959 Model = "pic";
Daniel Dunbar1d460332009-03-18 10:01:51 +0000960 else
Daniel Dunbarbc85be82009-04-29 18:32:25 +0000961 Model = getToolChain().GetDefaultRelocationModel();
Daniel Dunbar1d460332009-03-18 10:01:51 +0000962 }
Daniel Dunbarf219e7c2009-11-29 07:18:39 +0000963 if (llvm::StringRef(Model) != "pic") {
964 CmdArgs.push_back("-mrelocation-model");
965 CmdArgs.push_back(Model);
966 }
Daniel Dunbarbc85be82009-04-29 18:32:25 +0000967
968 // Infer the __PIC__ value.
969 //
970 // FIXME: This isn't quite right on Darwin, which always sets
971 // __PIC__=2.
972 if (strcmp(Model, "pic") == 0 || strcmp(Model, "dynamic-no-pic") == 0) {
Daniel Dunbar76743522009-11-29 02:39:08 +0000973 CmdArgs.push_back("-pic-level");
974 CmdArgs.push_back(Args.hasArg(options::OPT_fPIC) ? "2" : "1");
Daniel Dunbarbc85be82009-04-29 18:32:25 +0000975 }
Tanya Lattner59876c22009-11-04 01:18:09 +0000976 if (!Args.hasFlag(options::OPT_fmerge_all_constants,
977 options::OPT_fno_merge_all_constants))
Daniel Dunbar66861e02009-11-20 22:21:36 +0000978 CmdArgs.push_back("-no-merge-all-constants");
Daniel Dunbar6bea73b2009-09-16 06:17:29 +0000979
Daniel Dunbarf219e7c2009-11-29 07:18:39 +0000980 // LLVM Code Generator Options.
981
982 // FIXME: Set --enable-unsafe-fp-math.
983 if (Args.hasFlag(options::OPT_fno_omit_frame_pointer,
984 options::OPT_fomit_frame_pointer))
985 CmdArgs.push_back("-mdisable-fp-elim");
986 if (!Args.hasFlag(options::OPT_fzero_initialized_in_bss,
987 options::OPT_fno_zero_initialized_in_bss))
988 CmdArgs.push_back("-mno-zero-initialized-in-bss");
Dan Gohman4d5625e2010-10-14 22:36:56 +0000989 if (Args.hasFlag(options::OPT_fno_strict_aliasing,
990 options::OPT_fstrict_aliasing,
991 false))
992 CmdArgs.push_back("-relaxed-aliasing");
Daniel Dunbar1b718482010-05-14 22:00:22 +0000993
994 // Decide whether to use verbose asm. Verbose assembly is the default on
995 // toolchains which have the integrated assembler on by default.
996 bool IsVerboseAsmDefault = getToolChain().IsIntegratedAssemblerDefault();
997 if (Args.hasFlag(options::OPT_fverbose_asm, options::OPT_fno_verbose_asm,
Michael J. Spencer20249a12010-10-21 03:16:25 +0000998 IsVerboseAsmDefault) ||
Daniel Dunbar1b718482010-05-14 22:00:22 +0000999 Args.hasArg(options::OPT_dA))
Daniel Dunbarf219e7c2009-11-29 07:18:39 +00001000 CmdArgs.push_back("-masm-verbose");
Daniel Dunbar1b718482010-05-14 22:00:22 +00001001
Daniel Dunbarf219e7c2009-11-29 07:18:39 +00001002 if (Args.hasArg(options::OPT_fdebug_pass_structure)) {
1003 CmdArgs.push_back("-mdebug-pass");
1004 CmdArgs.push_back("Structure");
1005 }
1006 if (Args.hasArg(options::OPT_fdebug_pass_arguments)) {
1007 CmdArgs.push_back("-mdebug-pass");
1008 CmdArgs.push_back("Arguments");
1009 }
1010
John McCalld0c2ec42010-02-19 02:45:38 +00001011 // Enable -mconstructor-aliases except on darwin, where we have to
1012 // work around a linker bug; see <rdar://problem/7651567>.
1013 if (getToolChain().getTriple().getOS() != llvm::Triple::Darwin)
1014 CmdArgs.push_back("-mconstructor-aliases");
1015
Daniel Dunbar6bea73b2009-09-16 06:17:29 +00001016 // This is a coarse approximation of what llvm-gcc actually does, both
1017 // -fasynchronous-unwind-tables and -fnon-call-exceptions interact in more
1018 // complicated ways.
1019 bool AsynchronousUnwindTables =
1020 Args.hasFlag(options::OPT_fasynchronous_unwind_tables,
1021 options::OPT_fno_asynchronous_unwind_tables,
1022 getToolChain().IsUnwindTablesDefault() &&
Daniel Dunbar0a80ba72010-03-20 04:52:14 +00001023 !KernelOrKext);
Daniel Dunbar6bea73b2009-09-16 06:17:29 +00001024 if (Args.hasFlag(options::OPT_funwind_tables, options::OPT_fno_unwind_tables,
1025 AsynchronousUnwindTables))
Daniel Dunbarf219e7c2009-11-29 07:18:39 +00001026 CmdArgs.push_back("-munwind-tables");
1027
1028 if (Arg *A = Args.getLastArg(options::OPT_flimited_precision_EQ)) {
1029 CmdArgs.push_back("-mlimit-float-precision");
1030 CmdArgs.push_back(A->getValue(Args));
1031 }
Daniel Dunbarbc85be82009-04-29 18:32:25 +00001032
Daniel Dunbar868bd0a2009-05-06 03:16:41 +00001033 // FIXME: Handle -mtune=.
1034 (void) Args.hasArg(options::OPT_mtune_EQ);
Daniel Dunbarbc85be82009-04-29 18:32:25 +00001035
Benjamin Kramer8e9ef0d2009-08-05 14:30:52 +00001036 if (Arg *A = Args.getLastArg(options::OPT_mcmodel_EQ)) {
Daniel Dunbarf219e7c2009-11-29 07:18:39 +00001037 CmdArgs.push_back("-mcode-model");
Benjamin Kramer8e9ef0d2009-08-05 14:30:52 +00001038 CmdArgs.push_back(A->getValue(Args));
1039 }
1040
Daniel Dunbar6acda162009-09-09 22:33:08 +00001041 // Add target specific cpu and features flags.
1042 switch(getToolChain().getTriple().getArch()) {
1043 default:
1044 break;
Daniel Dunbar868bd0a2009-05-06 03:16:41 +00001045
Daniel Dunbarb163ef72009-09-10 04:57:17 +00001046 case llvm::Triple::arm:
1047 case llvm::Triple::thumb:
1048 AddARMTargetArgs(Args, CmdArgs);
1049 break;
1050
Eric Christophered734732010-03-02 02:41:08 +00001051 case llvm::Triple::mips:
1052 case llvm::Triple::mipsel:
1053 AddMIPSTargetArgs(Args, CmdArgs);
1054 break;
1055
Bruno Cardoso Lopes9284d212010-11-09 17:21:19 +00001056 case llvm::Triple::sparc:
1057 AddSparcTargetArgs(Args, CmdArgs);
1058 break;
1059
Daniel Dunbar6acda162009-09-09 22:33:08 +00001060 case llvm::Triple::x86:
1061 case llvm::Triple::x86_64:
1062 AddX86TargetArgs(Args, CmdArgs);
1063 break;
Daniel Dunbarbc85be82009-04-29 18:32:25 +00001064 }
1065
Daniel Dunbarc176bc62010-08-11 23:07:47 +00001066 // Pass the linker version in use.
1067 if (Arg *A = Args.getLastArg(options::OPT_mlinker_version_EQ)) {
1068 CmdArgs.push_back("-target-linker-version");
1069 CmdArgs.push_back(A->getValue(Args));
1070 }
1071
Daniel Dunbar1ad66482010-07-01 01:31:45 +00001072 // -mno-omit-leaf-frame-pointer is default.
1073 if (Args.hasFlag(options::OPT_momit_leaf_frame_pointer,
1074 options::OPT_mno_omit_leaf_frame_pointer, false))
1075 CmdArgs.push_back("-momit-leaf-frame-pointer");
1076
Dan Gohmanc31176d2010-01-08 02:20:44 +00001077 // -fno-math-errno is default.
1078 if (Args.hasFlag(options::OPT_fmath_errno,
Daniel Dunbarbc85be82009-04-29 18:32:25 +00001079 options::OPT_fno_math_errno,
Dan Gohmanc31176d2010-01-08 02:20:44 +00001080 false))
1081 CmdArgs.push_back("-fmath-errno");
Daniel Dunbarbc85be82009-04-29 18:32:25 +00001082
Daniel Dunbarb30575c2010-05-12 18:19:58 +00001083 // Explicitly error on some things we know we don't support and can't just
1084 // ignore.
1085 types::ID InputType = Inputs[0].getType();
Daniel Dunbare94db472010-09-24 19:39:37 +00001086 if (!Args.hasArg(options::OPT_fallow_unsupported)) {
1087 Arg *Unsupported;
1088 if ((Unsupported = Args.getLastArg(options::OPT_MG)) ||
Argyrios Kyrtzidis9a2b9d72010-10-08 00:25:19 +00001089 (Unsupported = Args.getLastArg(options::OPT_iframework)))
Daniel Dunbare94db472010-09-24 19:39:37 +00001090 D.Diag(clang::diag::err_drv_clang_unsupported)
Daniel Dunbarb30575c2010-05-12 18:19:58 +00001091 << Unsupported->getOption().getName();
Daniel Dunbare94db472010-09-24 19:39:37 +00001092
1093 if (types::isCXX(InputType) &&
1094 getToolChain().getTriple().getOS() == llvm::Triple::Darwin &&
1095 getToolChain().getTriple().getArch() == llvm::Triple::x86) {
1096 if ((Unsupported = Args.getLastArg(options::OPT_fapple_kext)))
1097 D.Diag(clang::diag::err_drv_clang_unsupported_opt_cxx_darwin_i386)
1098 << Unsupported->getOption().getName();
1099 }
Daniel Dunbarb30575c2010-05-12 18:19:58 +00001100 }
1101
Daniel Dunbar1d460332009-03-18 10:01:51 +00001102 Args.AddAllArgs(CmdArgs, options::OPT_v);
Daniel Dunbarf7c16d92010-08-24 22:44:13 +00001103 Args.AddLastArg(CmdArgs, options::OPT_H);
Daniel Dunbar1d460332009-03-18 10:01:51 +00001104 Args.AddLastArg(CmdArgs, options::OPT_P);
Mike Stump1eb44332009-09-09 15:08:12 +00001105 Args.AddLastArg(CmdArgs, options::OPT_print_ivar_layout);
Daniel Dunbar1d460332009-03-18 10:01:51 +00001106
1107 // Special case debug options to only pass -g to clang. This is
1108 // wrong.
Rafael Espindola18f36d92010-03-07 04:46:18 +00001109 Args.ClaimAllArgs(options::OPT_g_Group);
Daniel Dunbar6b3d5a62010-05-12 18:19:55 +00001110 if (Arg *A = Args.getLastArg(options::OPT_g_Group))
1111 if (!A->getOption().matches(options::OPT_g0))
1112 CmdArgs.push_back("-g");
Daniel Dunbar1d460332009-03-18 10:01:51 +00001113
Rafael Espindola9cf933a2010-05-06 21:06:04 +00001114 Args.AddAllArgs(CmdArgs, options::OPT_ffunction_sections);
1115 Args.AddAllArgs(CmdArgs, options::OPT_fdata_sections);
1116
Chris Lattner7255a2d2010-06-22 00:03:40 +00001117 Args.AddAllArgs(CmdArgs, options::OPT_finstrument_functions);
1118
Daniel Dunbar1d460332009-03-18 10:01:51 +00001119 Args.AddLastArg(CmdArgs, options::OPT_nostdinc);
Douglas Gregor4c2bcad2010-03-24 20:13:48 +00001120 Args.AddLastArg(CmdArgs, options::OPT_nostdincxx);
Rafael Espindola8d737cc2009-10-26 13:36:57 +00001121 Args.AddLastArg(CmdArgs, options::OPT_nobuiltininc);
Daniel Dunbar1d460332009-03-18 10:01:51 +00001122
Daniel Dunbar5f122322009-12-15 01:02:52 +00001123 // Pass the path to compiler resource files.
Daniel Dunbar5f122322009-12-15 01:02:52 +00001124 CmdArgs.push_back("-resource-dir");
Daniel Dunbar225c4172010-01-20 02:35:16 +00001125 CmdArgs.push_back(D.ResourceDir.c_str());
Daniel Dunbar2ac9fc22009-04-07 21:42:00 +00001126
Argyrios Kyrtzidis389db162010-11-03 22:45:23 +00001127 Args.AddLastArg(CmdArgs, options::OPT_working_directory);
1128
Daniel Dunbarc21c4852009-04-08 23:54:23 +00001129 // Add preprocessing options like -I, -D, etc. if we are using the
1130 // preprocessor.
1131 //
1132 // FIXME: Support -fpreprocessed
Daniel Dunbarc21c4852009-04-08 23:54:23 +00001133 if (types::getPreprocessedType(InputType) != types::TY_INVALID)
Douglas Gregordf91ef32009-04-18 00:34:01 +00001134 AddPreprocessingOptions(D, Args, CmdArgs, Output, Inputs);
Daniel Dunbar1d460332009-03-18 10:01:51 +00001135
Daniel Dunbar20f0eac2009-09-17 06:53:36 +00001136 // Manually translate -O to -O2 and -O4 to -O3; let clang reject
Daniel Dunbar337a6272009-03-24 20:17:30 +00001137 // others.
1138 if (Arg *A = Args.getLastArg(options::OPT_O_Group)) {
Daniel Dunbarb827a052009-11-19 03:26:40 +00001139 if (A->getOption().matches(options::OPT_O4))
Daniel Dunbar337a6272009-03-24 20:17:30 +00001140 CmdArgs.push_back("-O3");
Daniel Dunbar473916c2010-05-27 06:51:08 +00001141 else if (A->getOption().matches(options::OPT_O) &&
1142 A->getValue(Args)[0] == '\0')
Daniel Dunbar20f0eac2009-09-17 06:53:36 +00001143 CmdArgs.push_back("-O2");
Daniel Dunbar5e3bc942010-11-13 18:17:11 +00001144 else if (A->getOption().matches(options::OPT_O) &&
1145 A->getValue(Args)[0] == 'z' &&
1146 A->getValue(Args)[1] == '\0')
1147 CmdArgs.push_back("-Os");
Daniel Dunbar1d460332009-03-18 10:01:51 +00001148 else
Daniel Dunbar5697aa02009-03-18 23:39:35 +00001149 A->render(Args, CmdArgs);
Daniel Dunbar1d460332009-03-18 10:01:51 +00001150 }
1151
Daniel Dunbar6e8371e2009-10-29 02:24:45 +00001152 Args.AddAllArgs(CmdArgs, options::OPT_W_Group);
1153 Args.AddLastArg(CmdArgs, options::OPT_pedantic);
1154 Args.AddLastArg(CmdArgs, options::OPT_pedantic_errors);
Daniel Dunbar1d460332009-03-18 10:01:51 +00001155 Args.AddLastArg(CmdArgs, options::OPT_w);
Daniel Dunbard573d262009-04-07 22:13:21 +00001156
1157 // Handle -{std, ansi, trigraphs} -- take the last of -{std, ansi}
1158 // (-ansi is equivalent to -std=c89).
1159 //
1160 // If a std is supplied, only add -trigraphs if it follows the
1161 // option.
1162 if (Arg *Std = Args.getLastArg(options::OPT_std_EQ, options::OPT_ansi)) {
1163 if (Std->getOption().matches(options::OPT_ansi))
Nuno Lopes528365d2009-10-16 14:28:06 +00001164 if (types::isCXX(InputType))
Daniel Dunbar294691e2009-11-04 06:24:38 +00001165 CmdArgs.push_back("-std=c++98");
Nuno Lopes528365d2009-10-16 14:28:06 +00001166 else
Daniel Dunbar294691e2009-11-04 06:24:38 +00001167 CmdArgs.push_back("-std=c89");
Daniel Dunbard573d262009-04-07 22:13:21 +00001168 else
1169 Std->render(Args, CmdArgs);
1170
Daniel Dunbar0e100312010-06-14 21:23:08 +00001171 if (Arg *A = Args.getLastArg(options::OPT_std_EQ, options::OPT_ansi,
1172 options::OPT_trigraphs))
1173 if (A != Std)
Daniel Dunbard573d262009-04-07 22:13:21 +00001174 A->render(Args, CmdArgs);
Daniel Dunbara3ff2022009-04-26 01:10:38 +00001175 } else {
1176 // Honor -std-default.
Daniel Dunbar4a5290e2010-01-29 21:03:02 +00001177 //
1178 // FIXME: Clang doesn't correctly handle -std= when the input language
1179 // doesn't match. For the time being just ignore this for C++ inputs;
1180 // eventually we want to do all the standard defaulting here instead of
1181 // splitting it between the driver and clang -cc1.
1182 if (!types::isCXX(InputType))
1183 Args.AddAllArgsTranslated(CmdArgs, options::OPT_std_default_EQ,
1184 "-std=", /*Joined=*/true);
Daniel Dunbard573d262009-04-07 22:13:21 +00001185 Args.AddLastArg(CmdArgs, options::OPT_trigraphs);
Daniel Dunbara3ff2022009-04-26 01:10:38 +00001186 }
Daniel Dunbarc21c4852009-04-08 23:54:23 +00001187
Chandler Carruthc304ba32010-05-22 02:21:53 +00001188 // Translate GCC's misnamer '-fasm' arguments to '-fgnu-keywords'.
1189 if (Arg *Asm = Args.getLastArg(options::OPT_fasm, options::OPT_fno_asm)) {
1190 if (Asm->getOption().matches(options::OPT_fasm))
1191 CmdArgs.push_back("-fgnu-keywords");
1192 else
1193 CmdArgs.push_back("-fno-gnu-keywords");
1194 }
1195
Daniel Dunbar1d460332009-03-18 10:01:51 +00001196 if (Arg *A = Args.getLastArg(options::OPT_ftemplate_depth_)) {
1197 CmdArgs.push_back("-ftemplate-depth");
1198 CmdArgs.push_back(A->getValue(Args));
1199 }
1200
Argyrios Kyrtzidis1380a142010-11-18 00:20:36 +00001201 if (Arg *A = Args.getLastArg(options::OPT_Wlarge_by_value_copy_EQ,
1202 options::OPT_Wlarge_by_value_copy_def)) {
1203 CmdArgs.push_back("-Wlarge-by-value-copy");
Argyrios Kyrtzidis3532fdd2010-11-17 23:11:54 +00001204 if (A->getNumValues())
1205 CmdArgs.push_back(A->getValue(Args));
1206 else
Argyrios Kyrtzidis1380a142010-11-18 00:20:36 +00001207 CmdArgs.push_back("64"); // default value for -Wlarge-by-value-copy.
Argyrios Kyrtzidis3532fdd2010-11-17 23:11:54 +00001208 }
1209
Daniel Dunbare4bdae72009-11-19 04:00:53 +00001210 if (Args.hasArg(options::OPT__relocatable_pch))
Daniel Dunbar66861e02009-11-20 22:21:36 +00001211 CmdArgs.push_back("-relocatable-pch");
Mike Stump1eb44332009-09-09 15:08:12 +00001212
Daniel Dunbar294691e2009-11-04 06:24:38 +00001213 if (Arg *A = Args.getLastArg(options::OPT_fconstant_string_class_EQ)) {
1214 CmdArgs.push_back("-fconstant-string-class");
1215 CmdArgs.push_back(A->getValue(Args));
1216 }
David Chisnall8a5a9aa2009-08-31 16:41:57 +00001217
Chris Lattner124fca52010-01-09 21:54:33 +00001218 if (Arg *A = Args.getLastArg(options::OPT_ftabstop_EQ)) {
1219 CmdArgs.push_back("-ftabstop");
1220 CmdArgs.push_back(A->getValue(Args));
1221 }
1222
Chris Lattner0f0c9632010-04-07 20:49:23 +00001223 CmdArgs.push_back("-ferror-limit");
1224 if (Arg *A = Args.getLastArg(options::OPT_ferror_limit_EQ))
1225 CmdArgs.push_back(A->getValue(Args));
1226 else
1227 CmdArgs.push_back("19");
Douglas Gregor575cf372010-04-20 07:18:24 +00001228
Chandler Carruthc40f73c2010-05-06 04:55:18 +00001229 if (Arg *A = Args.getLastArg(options::OPT_fmacro_backtrace_limit_EQ)) {
1230 CmdArgs.push_back("-fmacro-backtrace-limit");
Douglas Gregor6c1cb992010-05-04 17:13:42 +00001231 CmdArgs.push_back(A->getValue(Args));
Chandler Carruthc40f73c2010-05-06 04:55:18 +00001232 }
1233
1234 if (Arg *A = Args.getLastArg(options::OPT_ftemplate_backtrace_limit_EQ)) {
1235 CmdArgs.push_back("-ftemplate-backtrace-limit");
Douglas Gregor575cf372010-04-20 07:18:24 +00001236 CmdArgs.push_back(A->getValue(Args));
Chandler Carruthc40f73c2010-05-06 04:55:18 +00001237 }
1238
Daniel Dunbar55efe142009-11-04 06:24:47 +00001239 // Pass -fmessage-length=.
Daniel Dunbara28690e2009-11-30 08:40:54 +00001240 CmdArgs.push_back("-fmessage-length");
Daniel Dunbar55efe142009-11-04 06:24:47 +00001241 if (Arg *A = Args.getLastArg(options::OPT_fmessage_length_EQ)) {
Daniel Dunbara28690e2009-11-30 08:40:54 +00001242 CmdArgs.push_back(A->getValue(Args));
Daniel Dunbar55efe142009-11-04 06:24:47 +00001243 } else {
1244 // If -fmessage-length=N was not specified, determine whether this is a
1245 // terminal and, if so, implicitly define -fmessage-length appropriately.
1246 unsigned N = llvm::sys::Process::StandardErrColumns();
Daniel Dunbar55efe142009-11-04 06:24:47 +00001247 CmdArgs.push_back(Args.MakeArgString(llvm::Twine(N)));
1248 }
1249
Daniel Dunbarba8d8612009-12-03 18:42:11 +00001250 if (const Arg *A = Args.getLastArg(options::OPT_fvisibility_EQ)) {
1251 CmdArgs.push_back("-fvisibility");
1252 CmdArgs.push_back(A->getValue(Args));
1253 }
1254
Douglas Gregor7cf84d62010-06-15 17:05:35 +00001255 Args.AddLastArg(CmdArgs, options::OPT_fvisibility_inlines_hidden);
Michael J. Spencer20249a12010-10-21 03:16:25 +00001256
Daniel Dunbar0a80ba72010-03-20 04:52:14 +00001257 // -fhosted is default.
1258 if (KernelOrKext || Args.hasFlag(options::OPT_ffreestanding,
1259 options::OPT_fhosted,
1260 false))
1261 CmdArgs.push_back("-ffreestanding");
1262
Daniel Dunbarba8d8612009-12-03 18:42:11 +00001263 // Forward -f (flag) options which we can pass directly.
Mike Stump9c276ae2009-12-12 01:27:46 +00001264 Args.AddLastArg(CmdArgs, options::OPT_fcatch_undefined_behavior);
Daniel Dunbar3aaf0822009-04-07 21:51:40 +00001265 Args.AddLastArg(CmdArgs, options::OPT_femit_all_decls);
Daniel Dunbar3aaf0822009-04-07 21:51:40 +00001266 Args.AddLastArg(CmdArgs, options::OPT_fheinous_gnu_extensions);
Devang Patelc69e1cf2010-09-30 19:05:55 +00001267 Args.AddLastArg(CmdArgs, options::OPT_flimit_debug_info);
Daniel Dunbar8c6fa842010-03-16 16:57:46 +00001268
1269 // -flax-vector-conversions is default.
1270 if (!Args.hasFlag(options::OPT_flax_vector_conversions,
1271 options::OPT_fno_lax_vector_conversions))
1272 CmdArgs.push_back("-fno-lax-vector-conversions");
1273
Daniel Dunbar43a9b322010-04-10 16:20:23 +00001274 // Handle -fobjc-gc and -fobjc-gc-only. They are exclusive, and -fobjc-gc-only
1275 // takes precedence.
1276 const Arg *GCArg = Args.getLastArg(options::OPT_fobjc_gc_only);
1277 if (!GCArg)
1278 GCArg = Args.getLastArg(options::OPT_fobjc_gc);
1279 if (GCArg) {
1280 if (getToolChain().SupportsObjCGC()) {
1281 GCArg->render(Args, CmdArgs);
1282 } else {
1283 // FIXME: We should move this to a hard error.
1284 D.Diag(clang::diag::warn_drv_objc_gc_unsupported)
1285 << GCArg->getAsString(Args);
1286 }
1287 }
1288
Fariborz Jahanianb466d012011-01-07 01:05:02 +00001289 if (Args.getLastArg(options::OPT_fapple_kext))
1290 CmdArgs.push_back("-fapple-kext");
1291
Daniel Dunbar3aaf0822009-04-07 21:51:40 +00001292 Args.AddLastArg(CmdArgs, options::OPT_fno_show_column);
Fariborz Jahanian34e65772009-05-22 20:17:16 +00001293 Args.AddLastArg(CmdArgs, options::OPT_fobjc_sender_dependent_dispatch);
Chris Lattner182e0922009-04-21 05:34:31 +00001294 Args.AddLastArg(CmdArgs, options::OPT_fdiagnostics_print_source_range_info);
Douglas Gregor4786c152010-08-19 20:24:43 +00001295 Args.AddLastArg(CmdArgs, options::OPT_fdiagnostics_parseable_fixits);
Daniel Dunbar3aaf0822009-04-07 21:51:40 +00001296 Args.AddLastArg(CmdArgs, options::OPT_ftime_report);
1297 Args.AddLastArg(CmdArgs, options::OPT_ftrapv);
David Chisnall7f18e672010-09-17 18:29:54 +00001298
1299 if (Arg *A = Args.getLastArg(options::OPT_ftrapv_handler_EQ)) {
1300 CmdArgs.push_back("-ftrapv-handler");
1301 CmdArgs.push_back(A->getValue(Args));
1302 }
1303
Chris Lattnera4d71452010-06-26 21:25:03 +00001304 Args.AddLastArg(CmdArgs, options::OPT_fwrapv);
Daniel Dunbar3aaf0822009-04-07 21:51:40 +00001305 Args.AddLastArg(CmdArgs, options::OPT_fwritable_strings);
Eric Christopherf84d4092010-08-07 23:08:14 +00001306 Args.AddLastArg(CmdArgs, options::OPT_funroll_loops);
Daniel Dunbar1d460332009-03-18 10:01:51 +00001307
Daniel Dunbar5345c392009-09-03 04:54:28 +00001308 Args.AddLastArg(CmdArgs, options::OPT_pthread);
1309
Daniel Dunbar9e5cc6b2009-11-17 08:07:36 +00001310 // -stack-protector=0 is default.
1311 unsigned StackProtectorLevel = 0;
Bill Wendling45483f72009-06-28 07:36:13 +00001312 if (Arg *A = Args.getLastArg(options::OPT_fno_stack_protector,
1313 options::OPT_fstack_protector_all,
1314 options::OPT_fstack_protector)) {
Daniel Dunbar9e5cc6b2009-11-17 08:07:36 +00001315 if (A->getOption().matches(options::OPT_fstack_protector))
1316 StackProtectorLevel = 1;
1317 else if (A->getOption().matches(options::OPT_fstack_protector_all))
1318 StackProtectorLevel = 2;
1319 } else
1320 StackProtectorLevel = getToolChain().GetDefaultStackProtectorLevel();
1321 if (StackProtectorLevel) {
1322 CmdArgs.push_back("-stack-protector");
1323 CmdArgs.push_back(Args.MakeArgString(llvm::Twine(StackProtectorLevel)));
Bill Wendling45483f72009-06-28 07:36:13 +00001324 }
1325
Daniel Dunbar48d1ef72009-04-07 21:16:11 +00001326 // Forward -f options with positive and negative forms; we translate
1327 // these by hand.
1328
Fariborz Jahanianb466d012011-01-07 01:05:02 +00001329 if (Args.hasArg(options::OPT_mkernel)) {
1330 if (!Args.hasArg(options::OPT_fapple_kext))
1331 CmdArgs.push_back("-fapple-kext");
1332 if (!Args.hasArg(options::OPT_fbuiltin))
1333 CmdArgs.push_back("-fno-builtin");
1334 }
Daniel Dunbar9e5cc6b2009-11-17 08:07:36 +00001335 // -fbuiltin is default.
Fariborz Jahanianb466d012011-01-07 01:05:02 +00001336 else if (!Args.hasFlag(options::OPT_fbuiltin, options::OPT_fno_builtin))
Daniel Dunbar53e84842009-11-19 04:55:23 +00001337 CmdArgs.push_back("-fno-builtin");
Daniel Dunbar48d1ef72009-04-07 21:16:11 +00001338
Nuno Lopesfc284482009-12-16 16:59:22 +00001339 if (!Args.hasFlag(options::OPT_fassume_sane_operator_new,
1340 options::OPT_fno_assume_sane_operator_new))
1341 CmdArgs.push_back("-fno-assume-sane-operator-new");
1342
Daniel Dunbar9e5cc6b2009-11-17 08:07:36 +00001343 // -fblocks=0 is default.
1344 if (Args.hasFlag(options::OPT_fblocks, options::OPT_fno_blocks,
David Chisnall5e530af2009-11-17 19:33:30 +00001345 getToolChain().IsBlocksDefault())) {
Daniel Dunbar9e5cc6b2009-11-17 08:07:36 +00001346 CmdArgs.push_back("-fblocks");
David Chisnall5e530af2009-11-17 19:33:30 +00001347 }
Daniel Dunbar48d1ef72009-04-07 21:16:11 +00001348
John McCall32579cf2010-04-09 19:12:06 +00001349 // -faccess-control is default.
John McCall7002f4c2010-04-09 19:03:51 +00001350 if (Args.hasFlag(options::OPT_fno_access_control,
1351 options::OPT_faccess_control,
John McCall32579cf2010-04-09 19:12:06 +00001352 false))
John McCall7002f4c2010-04-09 19:03:51 +00001353 CmdArgs.push_back("-fno-access-control");
John McCall3ddd6e02010-03-17 01:32:13 +00001354
Anders Carlssona4c24752010-11-21 00:09:52 +00001355 // -felide-constructors is the default.
1356 if (Args.hasFlag(options::OPT_fno_elide_constructors,
1357 options::OPT_felide_constructors,
1358 false))
1359 CmdArgs.push_back("-fno-elide-constructors");
1360
Daniel Dunbar6d2eb4d2009-11-25 10:14:30 +00001361 // -fexceptions=0 is default.
Daniel Dunbarfee1add2010-09-09 21:17:44 +00001362 if (!KernelOrKext &&
1363 needsExceptions(Args, InputType, getToolChain().getTriple()))
Rafael Espindolaf759df02009-10-01 13:33:33 +00001364 CmdArgs.push_back("-fexceptions");
Mike Stump738f8c22009-07-31 23:15:31 +00001365
Daniel Dunbarb2987d12010-02-10 18:49:11 +00001366 if (getToolChain().UseSjLjExceptions())
1367 CmdArgs.push_back("-fsjlj-exceptions");
1368
Daniel Dunbar0be42c42009-11-17 07:06:20 +00001369 // -frtti is default.
Daniel Dunbar0a80ba72010-03-20 04:52:14 +00001370 if (KernelOrKext ||
1371 !Args.hasFlag(options::OPT_frtti, options::OPT_fno_rtti))
Daniel Dunbar53e84842009-11-19 04:55:23 +00001372 CmdArgs.push_back("-fno-rtti");
Mike Stump738f8c22009-07-31 23:15:31 +00001373
Argyrios Kyrtzidis9a2b9d72010-10-08 00:25:19 +00001374 // -fshort-enums=0 is default.
1375 // FIXME: Are there targers where -fshort-enums is on by default ?
1376 if (Args.hasFlag(options::OPT_fshort_enums,
1377 options::OPT_fno_short_enums, false))
1378 CmdArgs.push_back("-fshort-enums");
1379
Daniel Dunbar1f95e652009-11-17 06:37:03 +00001380 // -fsigned-char is default.
Daniel Dunbar6d2eb4d2009-11-25 10:14:30 +00001381 if (!Args.hasFlag(options::OPT_fsigned_char, options::OPT_funsigned_char,
Daniel Dunbar1f95e652009-11-17 06:37:03 +00001382 isSignedCharDefault(getToolChain().getTriple())))
Daniel Dunbar76743522009-11-29 02:39:08 +00001383 CmdArgs.push_back("-fno-signed-char");
Eli Friedman5a779732009-06-05 07:21:14 +00001384
Anders Carlssona508b7d2010-02-06 23:23:06 +00001385 // -fthreadsafe-static is default.
Michael J. Spencer20249a12010-10-21 03:16:25 +00001386 if (!Args.hasFlag(options::OPT_fthreadsafe_statics,
Anders Carlssona508b7d2010-02-06 23:23:06 +00001387 options::OPT_fno_threadsafe_statics))
1388 CmdArgs.push_back("-fno-threadsafe-statics");
1389
Daniel Dunbarefb0fa92010-03-20 04:15:41 +00001390 // -fuse-cxa-atexit is default.
Anton Korobeynikovd0a57d52010-09-11 11:17:06 +00001391 if (KernelOrKext ||
1392 !Args.hasFlag(options::OPT_fuse_cxa_atexit, options::OPT_fno_use_cxa_atexit,
NAKAMURA Takumicd7a30b2010-10-10 01:53:03 +00001393 getToolChain().getTriple().getOS() != llvm::Triple::Cygwin &&
Anton Korobeynikovd0a57d52010-09-11 11:17:06 +00001394 getToolChain().getTriple().getOS() != llvm::Triple::MinGW32 &&
1395 getToolChain().getTriple().getOS() != llvm::Triple::MinGW64))
Daniel Dunbarefb0fa92010-03-20 04:15:41 +00001396 CmdArgs.push_back("-fno-use-cxa-atexit");
1397
Daniel Dunbar0be42c42009-11-17 07:06:20 +00001398 // -fms-extensions=0 is default.
Daniel Dunbar6d2eb4d2009-11-25 10:14:30 +00001399 if (Args.hasFlag(options::OPT_fms_extensions, options::OPT_fno_ms_extensions,
Daniel Dunbar0be42c42009-11-17 07:06:20 +00001400 getToolChain().getTriple().getOS() == llvm::Triple::Win32))
1401 CmdArgs.push_back("-fms-extensions");
1402
Michael J. Spencerdae4ac42010-10-21 05:21:48 +00001403 // -fmsc-version=1300 is default.
1404 if (Args.hasFlag(options::OPT_fms_extensions, options::OPT_fno_ms_extensions,
1405 getToolChain().getTriple().getOS() == llvm::Triple::Win32) ||
1406 Args.hasArg(options::OPT_fmsc_version)) {
1407 llvm::StringRef msc_ver = Args.getLastArgValue(options::OPT_fmsc_version);
1408 if (msc_ver.empty())
1409 CmdArgs.push_back("-fmsc-version=1300");
1410 else
1411 CmdArgs.push_back(Args.MakeArgString("-fmsc-version=" + msc_ver));
1412 }
1413
1414
Dawn Perchik400b6072010-09-02 23:59:25 +00001415 // -fborland-extensions=0 is default.
1416 if (Args.hasFlag(options::OPT_fborland_extensions,
1417 options::OPT_fno_borland_extensions, false))
1418 CmdArgs.push_back("-fborland-extensions");
1419
Chandler Carrutheb5d7b72010-04-17 20:17:31 +00001420 // -fgnu-keywords default varies depending on language; only pass if
1421 // specified.
1422 if (Arg *A = Args.getLastArg(options::OPT_fgnu_keywords,
Daniel Dunbar40788d92010-04-24 17:56:39 +00001423 options::OPT_fno_gnu_keywords))
1424 A->render(Args, CmdArgs);
Chandler Carrutheb5d7b72010-04-17 20:17:31 +00001425
Daniel Dunbar5314e442010-07-19 19:44:22 +00001426 // -fnext-runtime defaults to on Darwin and when rewriting Objective-C, and is
1427 // -the -cc1 default.
Michael J. Spencer20249a12010-10-21 03:16:25 +00001428 bool NeXTRuntimeIsDefault =
Daniel Dunbar5314e442010-07-19 19:44:22 +00001429 IsRewriter || getToolChain().getTriple().getOS() == llvm::Triple::Darwin;
Daniel Dunbar6d2eb4d2009-11-25 10:14:30 +00001430 if (!Args.hasFlag(options::OPT_fnext_runtime, options::OPT_fgnu_runtime,
Daniel Dunbar5314e442010-07-19 19:44:22 +00001431 NeXTRuntimeIsDefault))
Daniel Dunbarc5a97ec2009-11-17 07:07:28 +00001432 CmdArgs.push_back("-fgnu-runtime");
1433
Daniel Dunbar9e5cc6b2009-11-17 08:07:36 +00001434 // -fobjc-nonfragile-abi=0 is default.
1435 if (types::isObjC(InputType)) {
Daniel Dunbardea63132010-09-20 18:19:55 +00001436 // Compute the Objective-C ABI "version" to use. Version numbers are
1437 // slightly confusing for historical reasons:
1438 // 1 - Traditional "fragile" ABI
1439 // 2 - Non-fragile ABI, version 1
1440 // 3 - Non-fragile ABI, version 2
Daniel Dunbar5e2a8ac2010-04-28 23:25:24 +00001441 unsigned Version = 1;
Daniel Dunbardea63132010-09-20 18:19:55 +00001442 // If -fobjc-abi-version= is present, use that to set the version.
Daniel Dunbar5e2a8ac2010-04-28 23:25:24 +00001443 if (Arg *A = Args.getLastArg(options::OPT_fobjc_abi_version_EQ)) {
1444 if (llvm::StringRef(A->getValue(Args)) == "1")
1445 Version = 1;
1446 else if (llvm::StringRef(A->getValue(Args)) == "2")
1447 Version = 2;
Fariborz Jahanian5f81d8a2010-09-15 16:00:51 +00001448 else if (llvm::StringRef(A->getValue(Args)) == "3")
1449 Version = 3;
Daniel Dunbar5e2a8ac2010-04-28 23:25:24 +00001450 else
1451 D.Diag(clang::diag::err_drv_clang_unsupported) << A->getAsString(Args);
Daniel Dunbardea63132010-09-20 18:19:55 +00001452 } else {
1453 // Otherwise, determine if we are using the non-fragile ABI.
1454 if (Args.hasFlag(options::OPT_fobjc_nonfragile_abi,
1455 options::OPT_fno_objc_nonfragile_abi,
1456 getToolChain().IsObjCNonFragileABIDefault())) {
1457 // Determine the non-fragile ABI version to use.
Daniel Dunbarde17ba72010-11-11 16:08:59 +00001458#ifdef DISABLE_DEFAULT_NONFRAGILEABI_TWO
1459 unsigned NonFragileABIVersion = 1;
1460#else
Daniel Dunbardea63132010-09-20 18:19:55 +00001461 unsigned NonFragileABIVersion = 2;
Daniel Dunbarde17ba72010-11-11 16:08:59 +00001462#endif
Daniel Dunbardea63132010-09-20 18:19:55 +00001463
1464 if (Arg *A = Args.getLastArg(
1465 options::OPT_fobjc_nonfragile_abi_version_EQ)) {
1466 if (llvm::StringRef(A->getValue(Args)) == "1")
1467 NonFragileABIVersion = 1;
1468 else if (llvm::StringRef(A->getValue(Args)) == "2")
1469 NonFragileABIVersion = 2;
1470 else
1471 D.Diag(clang::diag::err_drv_clang_unsupported)
1472 << A->getAsString(Args);
1473 }
1474
1475 Version = 1 + NonFragileABIVersion;
1476 } else {
1477 Version = 1;
1478 }
Daniel Dunbar5e2a8ac2010-04-28 23:25:24 +00001479 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00001480
Fariborz Jahanian5f81d8a2010-09-15 16:00:51 +00001481 if (Version == 2 || Version == 3) {
Fariborz Jahanian112c3302011-01-04 20:05:20 +00001482 CmdArgs.push_back("-fobjc-nonfragile-abi");
Daniel Dunbarf643b9b2010-04-24 17:56:46 +00001483
1484 // -fobjc-dispatch-method is only relevant with the nonfragile-abi, and
1485 // legacy is the default.
1486 if (!Args.hasFlag(options::OPT_fobjc_legacy_dispatch,
1487 options::OPT_fno_objc_legacy_dispatch,
1488 getToolChain().IsObjCLegacyDispatchDefault())) {
1489 if (getToolChain().UseObjCMixedDispatch())
1490 CmdArgs.push_back("-fobjc-dispatch-method=mixed");
1491 else
1492 CmdArgs.push_back("-fobjc-dispatch-method=non-legacy");
1493 }
Daniel Dunbar984eb862010-02-01 21:07:25 +00001494 }
Ted Kremenekc32647d2010-12-23 21:35:43 +00001495
1496 // -fobjc-default-synthesize-properties=0 is default.
1497 if (Args.hasFlag(options::OPT_fobjc_default_synthesize_properties,
1498 options::OPT_fno_objc_default_synthesize_properties,
1499 getToolChain().IsObjCDefaultSynthPropertiesDefault())) {
1500 CmdArgs.push_back("-fobjc-default-synthesize-properties");
1501 }
Daniel Dunbar9e5cc6b2009-11-17 08:07:36 +00001502 }
1503
Daniel Dunbar984eb862010-02-01 21:07:25 +00001504 if (!Args.hasFlag(options::OPT_fassume_sane_operator_new,
1505 options::OPT_fno_assume_sane_operator_new))
1506 CmdArgs.push_back("-fno-assume-sane-operator-new");
1507
Daniel Dunbarf35f14d2010-04-27 15:34:57 +00001508 // -fconstant-cfstrings is default, and may be subject to argument translation
1509 // on Darwin.
1510 if (!Args.hasFlag(options::OPT_fconstant_cfstrings,
1511 options::OPT_fno_constant_cfstrings) ||
1512 !Args.hasFlag(options::OPT_mconstant_cfstrings,
1513 options::OPT_mno_constant_cfstrings))
1514 CmdArgs.push_back("-fno-constant-cfstrings");
1515
John Thompsona6fda122009-11-05 20:14:16 +00001516 // -fshort-wchar default varies depending on platform; only
1517 // pass if specified.
Daniel Dunbar1744a352010-04-27 15:35:03 +00001518 if (Arg *A = Args.getLastArg(options::OPT_fshort_wchar))
1519 A->render(Args, CmdArgs);
John Thompsona6fda122009-11-05 20:14:16 +00001520
Daniel Dunbaree848a72009-10-29 02:39:57 +00001521 // -fno-pascal-strings is default, only pass non-default. If the tool chain
1522 // happened to translate to -mpascal-strings, we want to back translate here.
Daniel Dunbar82d00682009-04-07 23:51:44 +00001523 //
1524 // FIXME: This is gross; that translation should be pulled from the
1525 // tool chain.
Daniel Dunbarc21c4852009-04-08 23:54:23 +00001526 if (Args.hasFlag(options::OPT_fpascal_strings,
Daniel Dunbar82d00682009-04-07 23:51:44 +00001527 options::OPT_fno_pascal_strings,
1528 false) ||
1529 Args.hasFlag(options::OPT_mpascal_strings,
1530 options::OPT_mno_pascal_strings,
1531 false))
Daniel Dunbar48d1ef72009-04-07 21:16:11 +00001532 CmdArgs.push_back("-fpascal-strings");
Fariborz Jahanianb466d012011-01-07 01:05:02 +00001533
1534 if (Args.hasArg(options::OPT_mkernel) ||
1535 Args.hasArg(options::OPT_fapple_kext)) {
1536 if (!Args.hasArg(options::OPT_fcommon))
1537 CmdArgs.push_back("-fno-common");
1538 }
Daniel Dunbar48d1ef72009-04-07 21:16:11 +00001539 // -fcommon is default, only pass non-default.
Fariborz Jahanianb466d012011-01-07 01:05:02 +00001540 else if (!Args.hasFlag(options::OPT_fcommon, options::OPT_fno_common))
Daniel Dunbar48d1ef72009-04-07 21:16:11 +00001541 CmdArgs.push_back("-fno-common");
1542
Daniel Dunbar70d3c922009-04-15 02:37:43 +00001543 // -fsigned-bitfields is default, and clang doesn't yet support
Daniel Dunbar06205ca2010-10-15 22:30:42 +00001544 // -funsigned-bitfields.
Mike Stump1eb44332009-09-09 15:08:12 +00001545 if (!Args.hasFlag(options::OPT_fsigned_bitfields,
Daniel Dunbar70d3c922009-04-15 02:37:43 +00001546 options::OPT_funsigned_bitfields))
1547 D.Diag(clang::diag::warn_drv_clang_unsupported)
1548 << Args.getLastArg(options::OPT_funsigned_bitfields)->getAsString(Args);
1549
Daniel Dunbar06205ca2010-10-15 22:30:42 +00001550 // -fsigned-bitfields is default, and clang doesn't support -fno-for-scope.
1551 if (!Args.hasFlag(options::OPT_ffor_scope,
1552 options::OPT_fno_for_scope))
1553 D.Diag(clang::diag::err_drv_clang_unsupported)
1554 << Args.getLastArg(options::OPT_fno_for_scope)->getAsString(Args);
1555
Jeffrey Yasskin0ea22fd2010-06-08 04:56:20 +00001556 // -fcaret-diagnostics is default.
1557 if (!Args.hasFlag(options::OPT_fcaret_diagnostics,
1558 options::OPT_fno_caret_diagnostics, true))
1559 CmdArgs.push_back("-fno-caret-diagnostics");
1560
Daniel Dunbar49138fc2009-04-19 21:09:34 +00001561 // -fdiagnostics-fixit-info is default, only pass non-default.
Mike Stump1eb44332009-09-09 15:08:12 +00001562 if (!Args.hasFlag(options::OPT_fdiagnostics_fixit_info,
Daniel Dunbar49138fc2009-04-19 21:09:34 +00001563 options::OPT_fno_diagnostics_fixit_info))
1564 CmdArgs.push_back("-fno-diagnostics-fixit-info");
1565
Daniel Dunbar9e820ee2009-04-16 06:32:38 +00001566 // Enable -fdiagnostics-show-option by default.
Mike Stump1eb44332009-09-09 15:08:12 +00001567 if (Args.hasFlag(options::OPT_fdiagnostics_show_option,
Daniel Dunbar9e820ee2009-04-16 06:32:38 +00001568 options::OPT_fno_diagnostics_show_option))
1569 CmdArgs.push_back("-fdiagnostics-show-option");
Daniel Dunbar838be482009-11-04 06:24:57 +00001570
Chris Lattner6fbe8392010-05-04 21:55:25 +00001571 if (const Arg *A =
1572 Args.getLastArg(options::OPT_fdiagnostics_show_category_EQ)) {
1573 CmdArgs.push_back("-fdiagnostics-show-category");
1574 CmdArgs.push_back(A->getValue(Args));
1575 }
Daniel Dunbarca0e0542010-08-24 16:47:49 +00001576
Daniel Dunbar838be482009-11-04 06:24:57 +00001577 // Color diagnostics are the default, unless the terminal doesn't support
1578 // them.
1579 if (Args.hasFlag(options::OPT_fcolor_diagnostics,
Argyrios Kyrtzidisf765d762010-09-23 12:56:06 +00001580 options::OPT_fno_color_diagnostics,
1581 llvm::sys::Process::StandardErrHasColors()))
Daniel Dunbar838be482009-11-04 06:24:57 +00001582 CmdArgs.push_back("-fcolor-diagnostics");
1583
Daniel Dunbar75eb1d62009-06-08 21:13:54 +00001584 if (!Args.hasFlag(options::OPT_fshow_source_location,
1585 options::OPT_fno_show_source_location))
1586 CmdArgs.push_back("-fno-show-source-location");
Daniel Dunbar9e820ee2009-04-16 06:32:38 +00001587
Douglas Gregora0068fc2010-07-09 17:35:33 +00001588 if (!Args.hasFlag(options::OPT_fspell_checking,
1589 options::OPT_fno_spell_checking))
1590 CmdArgs.push_back("-fno-spell-checking");
Daniel Dunbarca0e0542010-08-24 16:47:49 +00001591
Daniel Dunbar25b26eb2010-10-18 22:49:46 +00001592
Daniel Dunbar16894392010-11-02 19:42:04 +00001593 // Silently ignore -fasm-blocks for now.
1594 (void) Args.hasFlag(options::OPT_fasm_blocks, options::OPT_fno_asm_blocks,
1595 false);
Daniel Dunbar25b26eb2010-10-18 22:49:46 +00001596
Jeffrey Yasskin5edbdcc2010-06-11 05:57:47 +00001597 if (Arg *A = Args.getLastArg(options::OPT_fshow_overloads_EQ))
1598 A->render(Args, CmdArgs);
1599
Daniel Dunbar7695fba2009-04-19 21:20:32 +00001600 // -fdollars-in-identifiers default varies depending on platform and
1601 // language; only pass if specified.
Mike Stump1eb44332009-09-09 15:08:12 +00001602 if (Arg *A = Args.getLastArg(options::OPT_fdollars_in_identifiers,
Daniel Dunbar7695fba2009-04-19 21:20:32 +00001603 options::OPT_fno_dollars_in_identifiers)) {
1604 if (A->getOption().matches(options::OPT_fdollars_in_identifiers))
Daniel Dunbar8663b182009-12-16 20:10:18 +00001605 CmdArgs.push_back("-fdollars-in-identifiers");
Daniel Dunbar7695fba2009-04-19 21:20:32 +00001606 else
Daniel Dunbar8663b182009-12-16 20:10:18 +00001607 CmdArgs.push_back("-fno-dollars-in-identifiers");
Daniel Dunbar7695fba2009-04-19 21:20:32 +00001608 }
1609
Daniel Dunbare027a4b2009-05-22 19:02:20 +00001610 // -funit-at-a-time is default, and we don't support -fno-unit-at-a-time for
1611 // practical purposes.
Mike Stump1eb44332009-09-09 15:08:12 +00001612 if (Arg *A = Args.getLastArg(options::OPT_funit_at_a_time,
Daniel Dunbare027a4b2009-05-22 19:02:20 +00001613 options::OPT_fno_unit_at_a_time)) {
1614 if (A->getOption().matches(options::OPT_fno_unit_at_a_time))
Edward O'Callaghanc3769102009-10-13 16:41:34 +00001615 D.Diag(clang::diag::warn_drv_clang_unsupported) << A->getAsString(Args);
Daniel Dunbare027a4b2009-05-22 19:02:20 +00001616 }
Eli Friedmanceb5c5b2009-07-14 21:58:17 +00001617
Daniel Dunbar2ba91572009-09-10 03:37:02 +00001618 // Default to -fno-builtin-str{cat,cpy} on Darwin for ARM.
Daniel Dunbarf84a4a42009-09-10 04:57:27 +00001619 //
Daniel Dunbar8ff5b282009-12-11 23:00:49 +00001620 // FIXME: This is disabled until clang -cc1 supports -fno-builtin-foo. PR4941.
Daniel Dunbarf84a4a42009-09-10 04:57:27 +00001621#if 0
Daniel Dunbar2ba91572009-09-10 03:37:02 +00001622 if (getToolChain().getTriple().getOS() == llvm::Triple::Darwin &&
1623 (getToolChain().getTriple().getArch() == llvm::Triple::arm ||
1624 getToolChain().getTriple().getArch() == llvm::Triple::thumb)) {
1625 if (!Args.hasArg(options::OPT_fbuiltin_strcat))
1626 CmdArgs.push_back("-fno-builtin-strcat");
1627 if (!Args.hasArg(options::OPT_fbuiltin_strcpy))
1628 CmdArgs.push_back("-fno-builtin-strcpy");
1629 }
Daniel Dunbarf84a4a42009-09-10 04:57:27 +00001630#endif
Daniel Dunbar2ba91572009-09-10 03:37:02 +00001631
Mike Stump1eb44332009-09-09 15:08:12 +00001632 if (Arg *A = Args.getLastArg(options::OPT_traditional,
Eli Friedmanceb5c5b2009-07-14 21:58:17 +00001633 options::OPT_traditional_cpp))
1634 D.Diag(clang::diag::err_drv_clang_unsupported) << A->getAsString(Args);
1635
Daniel Dunbar1d460332009-03-18 10:01:51 +00001636 Args.AddLastArg(CmdArgs, options::OPT_dM);
Chris Lattnerd82df3a2009-04-12 01:56:53 +00001637 Args.AddLastArg(CmdArgs, options::OPT_dD);
Daniel Dunbar1d460332009-03-18 10:01:51 +00001638
Daniel Dunbar3f87fb02010-04-15 06:09:03 +00001639 // Forward -Xclang arguments to -cc1, and -mllvm arguments to the LLVM option
1640 // parser.
Daniel Dunbar1d460332009-03-18 10:01:51 +00001641 Args.AddAllArgValues(CmdArgs, options::OPT_Xclang);
Daniel Dunbar3f87fb02010-04-15 06:09:03 +00001642 for (arg_iterator it = Args.filtered_begin(options::OPT_mllvm),
1643 ie = Args.filtered_end(); it != ie; ++it) {
Daniel Dunbar7e4953e2010-06-11 22:00:13 +00001644 (*it)->claim();
Daniel Dunbarfb36d212010-04-17 06:10:00 +00001645
Daniel Dunbar3f87fb02010-04-15 06:09:03 +00001646 // We translate this by hand to the -cc1 argument, since nightly test uses
1647 // it and developers have been trained to spell it with -mllvm.
Daniel Dunbar7e4953e2010-06-11 22:00:13 +00001648 if (llvm::StringRef((*it)->getValue(Args, 0)) == "-disable-llvm-optzns")
Daniel Dunbar3f87fb02010-04-15 06:09:03 +00001649 CmdArgs.push_back("-disable-llvm-optzns");
1650 else
Daniel Dunbar7e4953e2010-06-11 22:00:13 +00001651 (*it)->render(Args, CmdArgs);
Daniel Dunbar3f87fb02010-04-15 06:09:03 +00001652 }
Daniel Dunbar1d460332009-03-18 10:01:51 +00001653
Daniel Dunbarcd8e4c42009-03-30 06:36:42 +00001654 if (Output.getType() == types::TY_Dependencies) {
1655 // Handled with other dependency code.
Daniel Dunbar115a7922009-03-19 07:29:38 +00001656 } else if (Output.isFilename()) {
Daniel Dunbarb488c1d2009-03-18 08:07:30 +00001657 CmdArgs.push_back("-o");
Daniel Dunbar115a7922009-03-19 07:29:38 +00001658 CmdArgs.push_back(Output.getFilename());
1659 } else {
1660 assert(Output.isNothing() && "Invalid output.");
Daniel Dunbarb488c1d2009-03-18 08:07:30 +00001661 }
1662
Daniel Dunbar1d460332009-03-18 10:01:51 +00001663 for (InputInfoList::const_iterator
1664 it = Inputs.begin(), ie = Inputs.end(); it != ie; ++it) {
1665 const InputInfo &II = *it;
1666 CmdArgs.push_back("-x");
1667 CmdArgs.push_back(types::getTypeName(II.getType()));
Daniel Dunbar7c1e4652010-08-02 02:38:21 +00001668 if (II.isFilename())
Daniel Dunbar115a7922009-03-19 07:29:38 +00001669 CmdArgs.push_back(II.getFilename());
Daniel Dunbar1d460332009-03-18 10:01:51 +00001670 else
Daniel Dunbar115a7922009-03-19 07:29:38 +00001671 II.getInputArg().renderAsInput(Args, CmdArgs);
Daniel Dunbar1d460332009-03-18 10:01:51 +00001672 }
Daniel Dunbarc21c4852009-04-08 23:54:23 +00001673
Chris Lattnere6113de2009-11-03 19:50:27 +00001674 Args.AddAllArgs(CmdArgs, options::OPT_undef);
1675
Daniel Dunbara001c1c2010-07-18 21:16:15 +00001676 const char *Exec = getToolChain().getDriver().getClangProgramPath();
Daniel Dunbarf2d8b9f2009-12-18 02:43:17 +00001677
1678 // Optionally embed the -cc1 level arguments into the debug info, for build
1679 // analysis.
1680 if (getToolChain().UseDwarfDebugFlags()) {
Daniel Dunbar6e900472010-06-04 18:47:06 +00001681 ArgStringList OriginalArgs;
1682 for (ArgList::const_iterator it = Args.begin(),
1683 ie = Args.end(); it != ie; ++it)
1684 (*it)->render(Args, OriginalArgs);
Daniel Dunbarca0e0542010-08-24 16:47:49 +00001685
Daniel Dunbarf2d8b9f2009-12-18 02:43:17 +00001686 llvm::SmallString<256> Flags;
1687 Flags += Exec;
Daniel Dunbar6e900472010-06-04 18:47:06 +00001688 for (unsigned i = 0, e = OriginalArgs.size(); i != e; ++i) {
Daniel Dunbarf2d8b9f2009-12-18 02:43:17 +00001689 Flags += " ";
Daniel Dunbar6e900472010-06-04 18:47:06 +00001690 Flags += OriginalArgs[i];
Daniel Dunbarf2d8b9f2009-12-18 02:43:17 +00001691 }
1692 CmdArgs.push_back("-dwarf-debug-flags");
1693 CmdArgs.push_back(Args.MakeArgString(Flags.str()));
1694 }
1695
Daniel Dunbar2fe238e2010-08-02 02:38:28 +00001696 C.addCommand(new Command(JA, *this, Exec, CmdArgs));
Daniel Dunbara880db02009-03-23 19:03:36 +00001697
Daniel Dunbar5c1aaaf2009-04-07 19:18:24 +00001698 // Explicitly warn that these options are unsupported, even though
1699 // we are allowing compilation to continue.
Daniel Dunbarcdd96862009-11-25 11:53:23 +00001700 for (arg_iterator it = Args.filtered_begin(options::OPT_pg),
1701 ie = Args.filtered_end(); it != ie; ++it) {
Daniel Dunbar7e4953e2010-06-11 22:00:13 +00001702 (*it)->claim();
1703 D.Diag(clang::diag::warn_drv_clang_unsupported) << (*it)->getAsString(Args);
Daniel Dunbar5c1aaaf2009-04-07 19:18:24 +00001704 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00001705
Daniel Dunbar68fb4692009-04-03 20:51:31 +00001706 // Claim some arguments which clang supports automatically.
1707
Daniel Dunbarf4046862010-04-15 06:18:42 +00001708 // -fpch-preprocess is used with gcc to add a special marker in the output to
1709 // include the PCH file. Clang's PTH solution is completely transparent, so we
1710 // do not need to deal with it at all.
Daniel Dunbar68fb4692009-04-03 20:51:31 +00001711 Args.ClaimAllArgs(options::OPT_fpch_preprocess);
Daniel Dunbarc21c4852009-04-08 23:54:23 +00001712
Daniel Dunbara880db02009-03-23 19:03:36 +00001713 // Claim some arguments which clang doesn't support, but we don't
1714 // care to warn the user about.
Daniel Dunbarcdd96862009-11-25 11:53:23 +00001715 Args.ClaimAllArgs(options::OPT_clang_ignored_f_Group);
1716 Args.ClaimAllArgs(options::OPT_clang_ignored_m_Group);
Daniel Dunbar47ac7d22009-03-18 06:00:36 +00001717}
1718
Daniel Dunbar20a9aa52010-05-20 21:30:13 +00001719void ClangAs::ConstructJob(Compilation &C, const JobAction &JA,
Daniel Dunbar20a9aa52010-05-20 21:30:13 +00001720 const InputInfo &Output,
1721 const InputInfoList &Inputs,
1722 const ArgList &Args,
1723 const char *LinkingOutput) const {
Daniel Dunbar20a9aa52010-05-20 21:30:13 +00001724 ArgStringList CmdArgs;
1725
1726 assert(Inputs.size() == 1 && "Unexpected number of inputs.");
1727 const InputInfo &Input = Inputs[0];
1728
Rafael Espindoladbe80d92010-11-17 22:13:25 +00001729 // Don't warn about "clang -w -c foo.s"
1730 Args.ClaimAllArgs(options::OPT_w);
1731
Daniel Dunbar20a9aa52010-05-20 21:30:13 +00001732 // Invoke ourselves in -cc1as mode.
1733 //
1734 // FIXME: Implement custom jobs for internal actions.
1735 CmdArgs.push_back("-cc1as");
1736
1737 // Add the "effective" target triple.
1738 CmdArgs.push_back("-triple");
Daniel Dunbar00577ad2010-08-23 22:35:37 +00001739 std::string TripleStr = getToolChain().ComputeEffectiveClangTriple(Args);
Daniel Dunbar20a9aa52010-05-20 21:30:13 +00001740 CmdArgs.push_back(Args.MakeArgString(TripleStr));
1741
1742 // Set the output mode, we currently only expect to be used as a real
1743 // assembler.
1744 CmdArgs.push_back("-filetype");
1745 CmdArgs.push_back("obj");
1746
Daniel Dunbar99298002010-05-27 06:18:05 +00001747 // At -O0, we use -mrelax-all by default.
1748 bool IsOpt = false;
1749 if (Arg *A = Args.getLastArg(options::OPT_O_Group))
1750 IsOpt = !A->getOption().matches(options::OPT_O0);
1751 if (Args.hasFlag(options::OPT_mrelax_all,
1752 options::OPT_mno_relax_all,
1753 !IsOpt))
Daniel Dunbar469d40e2010-05-28 16:43:21 +00001754 CmdArgs.push_back("-relax-all");
Daniel Dunbar99298002010-05-27 06:18:05 +00001755
Daniel Dunbar20a9aa52010-05-20 21:30:13 +00001756 // FIXME: Add -force_cpusubtype_ALL support, once we have it.
1757
1758 // FIXME: Add -g support, once we have it.
1759
1760 // FIXME: Add -static support, once we have it.
1761
1762 Args.AddAllArgValues(CmdArgs, options::OPT_Wa_COMMA,
1763 options::OPT_Xassembler);
1764
1765 assert(Output.isFilename() && "Unexpected lipo output.");
1766 CmdArgs.push_back("-o");
1767 CmdArgs.push_back(Output.getFilename());
1768
Daniel Dunbar7c1e4652010-08-02 02:38:21 +00001769 assert(Input.isFilename() && "Invalid input.");
1770 CmdArgs.push_back(Input.getFilename());
Daniel Dunbar20a9aa52010-05-20 21:30:13 +00001771
Daniel Dunbara001c1c2010-07-18 21:16:15 +00001772 const char *Exec = getToolChain().getDriver().getClangProgramPath();
Daniel Dunbar2fe238e2010-08-02 02:38:28 +00001773 C.addCommand(new Command(JA, *this, Exec, CmdArgs));
Daniel Dunbar20a9aa52010-05-20 21:30:13 +00001774}
1775
Daniel Dunbarb488c1d2009-03-18 08:07:30 +00001776void gcc::Common::ConstructJob(Compilation &C, const JobAction &JA,
Daniel Dunbarb488c1d2009-03-18 08:07:30 +00001777 const InputInfo &Output,
1778 const InputInfoList &Inputs,
Daniel Dunbar1d460332009-03-18 10:01:51 +00001779 const ArgList &Args,
Daniel Dunbarb488c1d2009-03-18 08:07:30 +00001780 const char *LinkingOutput) const {
Daniel Dunbaree788e72009-12-21 18:54:17 +00001781 const Driver &D = getToolChain().getDriver();
Daniel Dunbarb488c1d2009-03-18 08:07:30 +00001782 ArgStringList CmdArgs;
Daniel Dunbar47ac7d22009-03-18 06:00:36 +00001783
Daniel Dunbarc21c4852009-04-08 23:54:23 +00001784 for (ArgList::const_iterator
Daniel Dunbar1d460332009-03-18 10:01:51 +00001785 it = Args.begin(), ie = Args.end(); it != ie; ++it) {
Daniel Dunbarb488c1d2009-03-18 08:07:30 +00001786 Arg *A = *it;
Daniel Dunbar75877192009-03-19 07:55:12 +00001787 if (A->getOption().hasForwardToGCC()) {
Daniel Dunbar2dffe2d2010-08-03 16:14:14 +00001788 // Don't forward any -g arguments to assembly steps.
1789 if (isa<AssembleJobAction>(JA) &&
1790 A->getOption().matches(options::OPT_g_Group))
1791 continue;
1792
Daniel Dunbar75877192009-03-19 07:55:12 +00001793 // It is unfortunate that we have to claim here, as this means
1794 // we will basically never report anything interesting for
Daniel Dunbar6ecc7a92009-05-02 21:41:52 +00001795 // platforms using a generic gcc, even if we are just using gcc
1796 // to get to the assembler.
Daniel Dunbar75877192009-03-19 07:55:12 +00001797 A->claim();
Daniel Dunbar1d460332009-03-18 10:01:51 +00001798 A->render(Args, CmdArgs);
Daniel Dunbar75877192009-03-19 07:55:12 +00001799 }
Daniel Dunbarb488c1d2009-03-18 08:07:30 +00001800 }
Daniel Dunbarc21c4852009-04-08 23:54:23 +00001801
Daniel Dunbar82b51cc2010-01-25 22:35:08 +00001802 RenderExtraToolArgs(JA, CmdArgs);
Daniel Dunbarb488c1d2009-03-18 08:07:30 +00001803
1804 // If using a driver driver, force the arch.
Daniel Dunbar7cfe31a2009-05-22 02:21:04 +00001805 const std::string &Arch = getToolChain().getArchName();
Daniel Dunbar1bda4342009-12-23 00:46:38 +00001806 if (getToolChain().getTriple().getOS() == llvm::Triple::Darwin) {
Daniel Dunbarb488c1d2009-03-18 08:07:30 +00001807 CmdArgs.push_back("-arch");
Daniel Dunbarbf54a062009-04-01 20:33:11 +00001808
1809 // FIXME: Remove these special cases.
Daniel Dunbar7cfe31a2009-05-22 02:21:04 +00001810 if (Arch == "powerpc")
1811 CmdArgs.push_back("ppc");
1812 else if (Arch == "powerpc64")
1813 CmdArgs.push_back("ppc64");
1814 else
Daniel Dunbar88137642009-09-09 22:32:48 +00001815 CmdArgs.push_back(Args.MakeArgString(Arch));
Daniel Dunbarb488c1d2009-03-18 08:07:30 +00001816 }
1817
Daniel Dunbar6ecc7a92009-05-02 21:41:52 +00001818 // Try to force gcc to match the tool chain we want, if we recognize
1819 // the arch.
Daniel Dunbar7cfe31a2009-05-22 02:21:04 +00001820 //
1821 // FIXME: The triple class should directly provide the information we want
1822 // here.
1823 if (Arch == "i386" || Arch == "powerpc")
Daniel Dunbar6ecc7a92009-05-02 21:41:52 +00001824 CmdArgs.push_back("-m32");
Daniel Dunbar7cfe31a2009-05-22 02:21:04 +00001825 else if (Arch == "x86_64" || Arch == "powerpc64")
Daniel Dunbar6ecc7a92009-05-02 21:41:52 +00001826 CmdArgs.push_back("-m64");
1827
Daniel Dunbar7c1e4652010-08-02 02:38:21 +00001828 if (Output.isFilename()) {
Daniel Dunbarb488c1d2009-03-18 08:07:30 +00001829 CmdArgs.push_back("-o");
Daniel Dunbar115a7922009-03-19 07:29:38 +00001830 CmdArgs.push_back(Output.getFilename());
1831 } else {
1832 assert(Output.isNothing() && "Unexpected output");
Daniel Dunbarb488c1d2009-03-18 08:07:30 +00001833 CmdArgs.push_back("-fsyntax-only");
Daniel Dunbar115a7922009-03-19 07:29:38 +00001834 }
Daniel Dunbarb488c1d2009-03-18 08:07:30 +00001835
1836
1837 // Only pass -x if gcc will understand it; otherwise hope gcc
1838 // understands the suffix correctly. The main use case this would go
1839 // wrong in is for linker inputs if they happened to have an odd
1840 // suffix; really the only way to get this to happen is a command
1841 // like '-x foobar a.c' which will treat a.c like a linker input.
1842 //
1843 // FIXME: For the linker case specifically, can we safely convert
1844 // inputs into '-Wl,' options?
1845 for (InputInfoList::const_iterator
1846 it = Inputs.begin(), ie = Inputs.end(); it != ie; ++it) {
1847 const InputInfo &II = *it;
Daniel Dunbara8304f62009-05-02 20:14:53 +00001848
Daniel Dunbar5915fbf2009-09-01 16:57:46 +00001849 // Don't try to pass LLVM or AST inputs to a generic gcc.
Daniel Dunbar6c6424b2010-06-07 23:28:45 +00001850 if (II.getType() == types::TY_LLVM_IR || II.getType() == types::TY_LTO_IR ||
1851 II.getType() == types::TY_LLVM_BC || II.getType() == types::TY_LTO_BC)
Daniel Dunbara8304f62009-05-02 20:14:53 +00001852 D.Diag(clang::diag::err_drv_no_linker_llvm_support)
Daniel Dunbar88137642009-09-09 22:32:48 +00001853 << getToolChain().getTripleString();
Daniel Dunbar5915fbf2009-09-01 16:57:46 +00001854 else if (II.getType() == types::TY_AST)
1855 D.Diag(clang::diag::err_drv_no_ast_support)
Daniel Dunbar88137642009-09-09 22:32:48 +00001856 << getToolChain().getTripleString();
Daniel Dunbara8304f62009-05-02 20:14:53 +00001857
Daniel Dunbarb488c1d2009-03-18 08:07:30 +00001858 if (types::canTypeBeUserSpecified(II.getType())) {
1859 CmdArgs.push_back("-x");
1860 CmdArgs.push_back(types::getTypeName(II.getType()));
1861 }
1862
Daniel Dunbar7c1e4652010-08-02 02:38:21 +00001863 if (II.isFilename())
Daniel Dunbar115a7922009-03-19 07:29:38 +00001864 CmdArgs.push_back(II.getFilename());
Daniel Dunbar48f99942010-09-25 18:10:05 +00001865 else {
1866 const Arg &A = II.getInputArg();
1867
1868 // Reverse translate some rewritten options.
1869 if (A.getOption().matches(options::OPT_Z_reserved_lib_stdcxx)) {
1870 CmdArgs.push_back("-lstdc++");
1871 continue;
1872 }
1873
Daniel Dunbar115a7922009-03-19 07:29:38 +00001874 // Don't render as input, we need gcc to do the translations.
Daniel Dunbar48f99942010-09-25 18:10:05 +00001875 A.render(Args, CmdArgs);
1876 }
Daniel Dunbarb488c1d2009-03-18 08:07:30 +00001877 }
1878
Rafael Espindola79e9e9d2010-09-06 02:36:23 +00001879 const char *GCCName = getToolChain().getDriver().getCCCGenericGCCName().c_str();
Daniel Dunbarc21c4852009-04-08 23:54:23 +00001880 const char *Exec =
Daniel Dunbar4a7e8892010-07-14 18:46:23 +00001881 Args.MakeArgString(getToolChain().GetProgramPath(GCCName));
Daniel Dunbar2fe238e2010-08-02 02:38:28 +00001882 C.addCommand(new Command(JA, *this, Exec, CmdArgs));
Daniel Dunbar47ac7d22009-03-18 06:00:36 +00001883}
1884
Daniel Dunbar82b51cc2010-01-25 22:35:08 +00001885void gcc::Preprocess::RenderExtraToolArgs(const JobAction &JA,
1886 ArgStringList &CmdArgs) const {
Daniel Dunbarb488c1d2009-03-18 08:07:30 +00001887 CmdArgs.push_back("-E");
Daniel Dunbar47ac7d22009-03-18 06:00:36 +00001888}
1889
Daniel Dunbar82b51cc2010-01-25 22:35:08 +00001890void gcc::Precompile::RenderExtraToolArgs(const JobAction &JA,
1891 ArgStringList &CmdArgs) const {
Daniel Dunbarb488c1d2009-03-18 08:07:30 +00001892 // The type is good enough.
Daniel Dunbar47ac7d22009-03-18 06:00:36 +00001893}
1894
Daniel Dunbar82b51cc2010-01-25 22:35:08 +00001895void gcc::Compile::RenderExtraToolArgs(const JobAction &JA,
1896 ArgStringList &CmdArgs) const {
Daniel Dunbar64952502010-02-11 03:16:21 +00001897 const Driver &D = getToolChain().getDriver();
1898
Daniel Dunbar82b51cc2010-01-25 22:35:08 +00001899 // If -flto, etc. are present then make sure not to force assembly output.
Daniel Dunbar6c6424b2010-06-07 23:28:45 +00001900 if (JA.getType() == types::TY_LLVM_IR || JA.getType() == types::TY_LTO_IR ||
1901 JA.getType() == types::TY_LLVM_BC || JA.getType() == types::TY_LTO_BC)
Daniel Dunbar82b51cc2010-01-25 22:35:08 +00001902 CmdArgs.push_back("-c");
Daniel Dunbar64952502010-02-11 03:16:21 +00001903 else {
1904 if (JA.getType() != types::TY_PP_Asm)
1905 D.Diag(clang::diag::err_drv_invalid_gcc_output_type)
1906 << getTypeName(JA.getType());
Michael J. Spencer20249a12010-10-21 03:16:25 +00001907
Daniel Dunbar82b51cc2010-01-25 22:35:08 +00001908 CmdArgs.push_back("-S");
Daniel Dunbar64952502010-02-11 03:16:21 +00001909 }
Daniel Dunbar47ac7d22009-03-18 06:00:36 +00001910}
1911
Daniel Dunbar82b51cc2010-01-25 22:35:08 +00001912void gcc::Assemble::RenderExtraToolArgs(const JobAction &JA,
1913 ArgStringList &CmdArgs) const {
Daniel Dunbarb488c1d2009-03-18 08:07:30 +00001914 CmdArgs.push_back("-c");
Daniel Dunbar47ac7d22009-03-18 06:00:36 +00001915}
Daniel Dunbarb488c1d2009-03-18 08:07:30 +00001916
Daniel Dunbar82b51cc2010-01-25 22:35:08 +00001917void gcc::Link::RenderExtraToolArgs(const JobAction &JA,
1918 ArgStringList &CmdArgs) const {
Daniel Dunbarb488c1d2009-03-18 08:07:30 +00001919 // The types are (hopefully) good enough.
1920}
1921
Daniel Dunbar40f12652009-03-29 17:08:39 +00001922const char *darwin::CC1::getCC1Name(types::ID Type) const {
1923 switch (Type) {
1924 default:
1925 assert(0 && "Unexpected type for Darwin CC1 tool.");
1926 case types::TY_Asm:
1927 case types::TY_C: case types::TY_CHeader:
1928 case types::TY_PP_C: case types::TY_PP_CHeader:
1929 return "cc1";
1930 case types::TY_ObjC: case types::TY_ObjCHeader:
1931 case types::TY_PP_ObjC: case types::TY_PP_ObjCHeader:
1932 return "cc1obj";
1933 case types::TY_CXX: case types::TY_CXXHeader:
1934 case types::TY_PP_CXX: case types::TY_PP_CXXHeader:
1935 return "cc1plus";
1936 case types::TY_ObjCXX: case types::TY_ObjCXXHeader:
1937 case types::TY_PP_ObjCXX: case types::TY_PP_ObjCXXHeader:
1938 return "cc1objplus";
1939 }
1940}
1941
Daniel Dunbarc21c4852009-04-08 23:54:23 +00001942const char *darwin::CC1::getBaseInputName(const ArgList &Args,
Daniel Dunbara5a7bd02009-03-30 00:34:04 +00001943 const InputInfoList &Inputs) {
Michael J. Spencer472ccff2010-12-18 00:19:12 +00001944 return Args.MakeArgString(
1945 llvm::sys::path::filename(Inputs[0].getBaseInput()));
Daniel Dunbara3ec60e2009-03-29 18:40:18 +00001946}
1947
Daniel Dunbarc21c4852009-04-08 23:54:23 +00001948const char *darwin::CC1::getBaseInputStem(const ArgList &Args,
Daniel Dunbara5a7bd02009-03-30 00:34:04 +00001949 const InputInfoList &Inputs) {
Daniel Dunbara3ec60e2009-03-29 18:40:18 +00001950 const char *Str = getBaseInputName(Args, Inputs);
1951
1952 if (const char *End = strchr(Str, '.'))
Daniel Dunbar88137642009-09-09 22:32:48 +00001953 return Args.MakeArgString(std::string(Str, End));
Daniel Dunbara3ec60e2009-03-29 18:40:18 +00001954
1955 return Str;
1956}
1957
1958const char *
Daniel Dunbarc21c4852009-04-08 23:54:23 +00001959darwin::CC1::getDependencyFileName(const ArgList &Args,
Daniel Dunbara5a7bd02009-03-30 00:34:04 +00001960 const InputInfoList &Inputs) {
Daniel Dunbara3ec60e2009-03-29 18:40:18 +00001961 // FIXME: Think about this more.
1962 std::string Res;
1963
1964 if (Arg *OutputOpt = Args.getLastArg(options::OPT_o)) {
1965 std::string Str(OutputOpt->getValue(Args));
Daniel Dunbarc21c4852009-04-08 23:54:23 +00001966
Daniel Dunbara3ec60e2009-03-29 18:40:18 +00001967 Res = Str.substr(0, Str.rfind('.'));
1968 } else
Daniel Dunbara5a7bd02009-03-30 00:34:04 +00001969 Res = darwin::CC1::getBaseInputStem(Args, Inputs);
Daniel Dunbara3ec60e2009-03-29 18:40:18 +00001970
Daniel Dunbar88137642009-09-09 22:32:48 +00001971 return Args.MakeArgString(Res + ".d");
Daniel Dunbara3ec60e2009-03-29 18:40:18 +00001972}
1973
Daniel Dunbarc21c4852009-04-08 23:54:23 +00001974void darwin::CC1::AddCC1Args(const ArgList &Args,
Daniel Dunbara3ec60e2009-03-29 18:40:18 +00001975 ArgStringList &CmdArgs) const {
Daniel Dunbaree788e72009-12-21 18:54:17 +00001976 const Driver &D = getToolChain().getDriver();
Daniel Dunbare2fd6642009-09-10 01:21:12 +00001977
1978 CheckCodeGenerationOptions(D, Args);
1979
Daniel Dunbara3ec60e2009-03-29 18:40:18 +00001980 // Derived from cc1 spec.
Daniel Dunbara3ec60e2009-03-29 18:40:18 +00001981 if (!Args.hasArg(options::OPT_mkernel) && !Args.hasArg(options::OPT_static) &&
1982 !Args.hasArg(options::OPT_mdynamic_no_pic))
1983 CmdArgs.push_back("-fPIC");
1984
Daniel Dunbar2ba91572009-09-10 03:37:02 +00001985 if (getToolChain().getTriple().getArch() == llvm::Triple::arm ||
1986 getToolChain().getTriple().getArch() == llvm::Triple::thumb) {
1987 if (!Args.hasArg(options::OPT_fbuiltin_strcat))
1988 CmdArgs.push_back("-fno-builtin-strcat");
1989 if (!Args.hasArg(options::OPT_fbuiltin_strcpy))
1990 CmdArgs.push_back("-fno-builtin-strcpy");
1991 }
1992
Daniel Dunbara3ec60e2009-03-29 18:40:18 +00001993 // gcc has some code here to deal with when no -mmacosx-version-min
1994 // and no -miphoneos-version-min is present, but this never happens
1995 // due to tool chain specific argument translation.
1996
Daniel Dunbara3ec60e2009-03-29 18:40:18 +00001997 if (Args.hasArg(options::OPT_g_Flag) &&
1998 !Args.hasArg(options::OPT_fno_eliminate_unused_debug_symbols))
1999 CmdArgs.push_back("-feliminate-unused-debug-symbols");
2000}
2001
2002void darwin::CC1::AddCC1OptionsArgs(const ArgList &Args, ArgStringList &CmdArgs,
2003 const InputInfoList &Inputs,
2004 const ArgStringList &OutputArgs) const {
Daniel Dunbaree788e72009-12-21 18:54:17 +00002005 const Driver &D = getToolChain().getDriver();
Daniel Dunbara3ec60e2009-03-29 18:40:18 +00002006
2007 // Derived from cc1_options spec.
2008 if (Args.hasArg(options::OPT_fast) ||
2009 Args.hasArg(options::OPT_fastf) ||
2010 Args.hasArg(options::OPT_fastcp))
2011 CmdArgs.push_back("-O3");
Daniel Dunbarc21c4852009-04-08 23:54:23 +00002012
Daniel Dunbara3ec60e2009-03-29 18:40:18 +00002013 if (Arg *A = Args.getLastArg(options::OPT_pg))
2014 if (Args.hasArg(options::OPT_fomit_frame_pointer))
2015 D.Diag(clang::diag::err_drv_argument_not_allowed_with)
2016 << A->getAsString(Args) << "-fomit-frame-pointer";
2017
2018 AddCC1Args(Args, CmdArgs);
2019
2020 if (!Args.hasArg(options::OPT_Q))
2021 CmdArgs.push_back("-quiet");
2022
2023 CmdArgs.push_back("-dumpbase");
Daniel Dunbara5a7bd02009-03-30 00:34:04 +00002024 CmdArgs.push_back(darwin::CC1::getBaseInputName(Args, Inputs));
Daniel Dunbara3ec60e2009-03-29 18:40:18 +00002025
2026 Args.AddAllArgs(CmdArgs, options::OPT_d_Group);
2027
2028 Args.AddAllArgs(CmdArgs, options::OPT_m_Group);
2029 Args.AddAllArgs(CmdArgs, options::OPT_a_Group);
2030
2031 // FIXME: The goal is to use the user provided -o if that is our
2032 // final output, otherwise to drive from the original input
2033 // name. Find a clean way to go about this.
2034 if ((Args.hasArg(options::OPT_c) || Args.hasArg(options::OPT_S)) &&
2035 Args.hasArg(options::OPT_o)) {
2036 Arg *OutputOpt = Args.getLastArg(options::OPT_o);
2037 CmdArgs.push_back("-auxbase-strip");
2038 CmdArgs.push_back(OutputOpt->getValue(Args));
2039 } else {
2040 CmdArgs.push_back("-auxbase");
Daniel Dunbara5a7bd02009-03-30 00:34:04 +00002041 CmdArgs.push_back(darwin::CC1::getBaseInputStem(Args, Inputs));
Daniel Dunbara3ec60e2009-03-29 18:40:18 +00002042 }
2043
2044 Args.AddAllArgs(CmdArgs, options::OPT_g_Group);
2045
2046 Args.AddAllArgs(CmdArgs, options::OPT_O);
2047 // FIXME: -Wall is getting some special treatment. Investigate.
2048 Args.AddAllArgs(CmdArgs, options::OPT_W_Group, options::OPT_pedantic_Group);
2049 Args.AddLastArg(CmdArgs, options::OPT_w);
Daniel Dunbarc21c4852009-04-08 23:54:23 +00002050 Args.AddAllArgs(CmdArgs, options::OPT_std_EQ, options::OPT_ansi,
Daniel Dunbara3ec60e2009-03-29 18:40:18 +00002051 options::OPT_trigraphs);
Daniel Dunbara3ff2022009-04-26 01:10:38 +00002052 if (!Args.getLastArg(options::OPT_std_EQ, options::OPT_ansi)) {
2053 // Honor -std-default.
2054 Args.AddAllArgsTranslated(CmdArgs, options::OPT_std_default_EQ,
2055 "-std=", /*Joined=*/true);
2056 }
2057
Daniel Dunbara3ec60e2009-03-29 18:40:18 +00002058 if (Args.hasArg(options::OPT_v))
2059 CmdArgs.push_back("-version");
2060 if (Args.hasArg(options::OPT_pg))
2061 CmdArgs.push_back("-p");
2062 Args.AddLastArg(CmdArgs, options::OPT_p);
Daniel Dunbarc21c4852009-04-08 23:54:23 +00002063
Daniel Dunbara3ec60e2009-03-29 18:40:18 +00002064 // The driver treats -fsyntax-only specially.
Daniel Dunbar2ba91572009-09-10 03:37:02 +00002065 if (getToolChain().getTriple().getArch() == llvm::Triple::arm ||
2066 getToolChain().getTriple().getArch() == llvm::Triple::thumb) {
2067 // Removes -fbuiltin-str{cat,cpy}; these aren't recognized by cc1 but are
2068 // used to inhibit the default -fno-builtin-str{cat,cpy}.
2069 //
2070 // FIXME: Should we grow a better way to deal with "removing" args?
Daniel Dunbarcdd96862009-11-25 11:53:23 +00002071 for (arg_iterator it = Args.filtered_begin(options::OPT_f_Group,
2072 options::OPT_fsyntax_only),
2073 ie = Args.filtered_end(); it != ie; ++it) {
Daniel Dunbar7e4953e2010-06-11 22:00:13 +00002074 if (!(*it)->getOption().matches(options::OPT_fbuiltin_strcat) &&
2075 !(*it)->getOption().matches(options::OPT_fbuiltin_strcpy)) {
2076 (*it)->claim();
2077 (*it)->render(Args, CmdArgs);
Daniel Dunbar2ba91572009-09-10 03:37:02 +00002078 }
2079 }
2080 } else
2081 Args.AddAllArgs(CmdArgs, options::OPT_f_Group, options::OPT_fsyntax_only);
Daniel Dunbarc21c4852009-04-08 23:54:23 +00002082
Daniel Dunbara3ec60e2009-03-29 18:40:18 +00002083 Args.AddAllArgs(CmdArgs, options::OPT_undef);
2084 if (Args.hasArg(options::OPT_Qn))
2085 CmdArgs.push_back("-fno-ident");
Daniel Dunbarc21c4852009-04-08 23:54:23 +00002086
Daniel Dunbara3ec60e2009-03-29 18:40:18 +00002087 // FIXME: This isn't correct.
2088 //Args.AddLastArg(CmdArgs, options::OPT__help)
2089 //Args.AddLastArg(CmdArgs, options::OPT__targetHelp)
2090
2091 CmdArgs.append(OutputArgs.begin(), OutputArgs.end());
2092
2093 // FIXME: Still don't get what is happening here. Investigate.
2094 Args.AddAllArgs(CmdArgs, options::OPT__param);
2095
2096 if (Args.hasArg(options::OPT_fmudflap) ||
2097 Args.hasArg(options::OPT_fmudflapth)) {
2098 CmdArgs.push_back("-fno-builtin");
2099 CmdArgs.push_back("-fno-merge-constants");
2100 }
Daniel Dunbarc21c4852009-04-08 23:54:23 +00002101
Daniel Dunbara3ec60e2009-03-29 18:40:18 +00002102 if (Args.hasArg(options::OPT_coverage)) {
2103 CmdArgs.push_back("-fprofile-arcs");
2104 CmdArgs.push_back("-ftest-coverage");
2105 }
2106
2107 if (types::isCXX(Inputs[0].getType()))
2108 CmdArgs.push_back("-D__private_extern__=extern");
2109}
2110
2111void darwin::CC1::AddCPPOptionsArgs(const ArgList &Args, ArgStringList &CmdArgs,
2112 const InputInfoList &Inputs,
2113 const ArgStringList &OutputArgs) const {
2114 // Derived from cpp_options
2115 AddCPPUniqueOptionsArgs(Args, CmdArgs, Inputs);
Daniel Dunbarc21c4852009-04-08 23:54:23 +00002116
Daniel Dunbara3ec60e2009-03-29 18:40:18 +00002117 CmdArgs.append(OutputArgs.begin(), OutputArgs.end());
2118
2119 AddCC1Args(Args, CmdArgs);
2120
2121 // NOTE: The code below has some commonality with cpp_options, but
2122 // in classic gcc style ends up sending things in different
2123 // orders. This may be a good merge candidate once we drop pedantic
2124 // compatibility.
2125
2126 Args.AddAllArgs(CmdArgs, options::OPT_m_Group);
Daniel Dunbarc21c4852009-04-08 23:54:23 +00002127 Args.AddAllArgs(CmdArgs, options::OPT_std_EQ, options::OPT_ansi,
Daniel Dunbara3ec60e2009-03-29 18:40:18 +00002128 options::OPT_trigraphs);
Daniel Dunbara3ff2022009-04-26 01:10:38 +00002129 if (!Args.getLastArg(options::OPT_std_EQ, options::OPT_ansi)) {
2130 // Honor -std-default.
2131 Args.AddAllArgsTranslated(CmdArgs, options::OPT_std_default_EQ,
2132 "-std=", /*Joined=*/true);
2133 }
Daniel Dunbara3ec60e2009-03-29 18:40:18 +00002134 Args.AddAllArgs(CmdArgs, options::OPT_W_Group, options::OPT_pedantic_Group);
2135 Args.AddLastArg(CmdArgs, options::OPT_w);
Daniel Dunbarc21c4852009-04-08 23:54:23 +00002136
Daniel Dunbara3ec60e2009-03-29 18:40:18 +00002137 // The driver treats -fsyntax-only specially.
2138 Args.AddAllArgs(CmdArgs, options::OPT_f_Group, options::OPT_fsyntax_only);
2139
2140 if (Args.hasArg(options::OPT_g_Group) && !Args.hasArg(options::OPT_g0) &&
2141 !Args.hasArg(options::OPT_fno_working_directory))
2142 CmdArgs.push_back("-fworking-directory");
2143
2144 Args.AddAllArgs(CmdArgs, options::OPT_O);
2145 Args.AddAllArgs(CmdArgs, options::OPT_undef);
2146 if (Args.hasArg(options::OPT_save_temps))
2147 CmdArgs.push_back("-fpch-preprocess");
2148}
2149
Daniel Dunbarc21c4852009-04-08 23:54:23 +00002150void darwin::CC1::AddCPPUniqueOptionsArgs(const ArgList &Args,
Daniel Dunbara3ec60e2009-03-29 18:40:18 +00002151 ArgStringList &CmdArgs,
Mike Stump1eb44332009-09-09 15:08:12 +00002152 const InputInfoList &Inputs) const {
Daniel Dunbaree788e72009-12-21 18:54:17 +00002153 const Driver &D = getToolChain().getDriver();
Daniel Dunbara3ec60e2009-03-29 18:40:18 +00002154
Daniel Dunbar88a3d6c2009-09-10 01:21:05 +00002155 CheckPreprocessingOptions(D, Args);
2156
Daniel Dunbara3ec60e2009-03-29 18:40:18 +00002157 // Derived from cpp_unique_options.
Daniel Dunbar88a3d6c2009-09-10 01:21:05 +00002158 // -{C,CC} only with -E is checked in CheckPreprocessingOptions().
2159 Args.AddLastArg(CmdArgs, options::OPT_C);
2160 Args.AddLastArg(CmdArgs, options::OPT_CC);
Daniel Dunbara3ec60e2009-03-29 18:40:18 +00002161 if (!Args.hasArg(options::OPT_Q))
2162 CmdArgs.push_back("-quiet");
2163 Args.AddAllArgs(CmdArgs, options::OPT_nostdinc);
Douglas Gregor4c2bcad2010-03-24 20:13:48 +00002164 Args.AddAllArgs(CmdArgs, options::OPT_nostdincxx);
Daniel Dunbara3ec60e2009-03-29 18:40:18 +00002165 Args.AddLastArg(CmdArgs, options::OPT_v);
2166 Args.AddAllArgs(CmdArgs, options::OPT_I_Group, options::OPT_F);
2167 Args.AddLastArg(CmdArgs, options::OPT_P);
2168
2169 // FIXME: Handle %I properly.
2170 if (getToolChain().getArchName() == "x86_64") {
2171 CmdArgs.push_back("-imultilib");
2172 CmdArgs.push_back("x86_64");
2173 }
2174
2175 if (Args.hasArg(options::OPT_MD)) {
2176 CmdArgs.push_back("-MD");
Daniel Dunbara5a7bd02009-03-30 00:34:04 +00002177 CmdArgs.push_back(darwin::CC1::getDependencyFileName(Args, Inputs));
Daniel Dunbara3ec60e2009-03-29 18:40:18 +00002178 }
2179
2180 if (Args.hasArg(options::OPT_MMD)) {
2181 CmdArgs.push_back("-MMD");
Daniel Dunbara5a7bd02009-03-30 00:34:04 +00002182 CmdArgs.push_back(darwin::CC1::getDependencyFileName(Args, Inputs));
Daniel Dunbara3ec60e2009-03-29 18:40:18 +00002183 }
2184
2185 Args.AddLastArg(CmdArgs, options::OPT_M);
2186 Args.AddLastArg(CmdArgs, options::OPT_MM);
2187 Args.AddAllArgs(CmdArgs, options::OPT_MF);
2188 Args.AddLastArg(CmdArgs, options::OPT_MG);
2189 Args.AddLastArg(CmdArgs, options::OPT_MP);
2190 Args.AddAllArgs(CmdArgs, options::OPT_MQ);
2191 Args.AddAllArgs(CmdArgs, options::OPT_MT);
2192 if (!Args.hasArg(options::OPT_M) && !Args.hasArg(options::OPT_MM) &&
2193 (Args.hasArg(options::OPT_MD) || Args.hasArg(options::OPT_MMD))) {
2194 if (Arg *OutputOpt = Args.getLastArg(options::OPT_o)) {
2195 CmdArgs.push_back("-MQ");
2196 CmdArgs.push_back(OutputOpt->getValue(Args));
2197 }
2198 }
2199
2200 Args.AddLastArg(CmdArgs, options::OPT_remap);
2201 if (Args.hasArg(options::OPT_g3))
2202 CmdArgs.push_back("-dD");
2203 Args.AddLastArg(CmdArgs, options::OPT_H);
2204
2205 AddCPPArgs(Args, CmdArgs);
2206
2207 Args.AddAllArgs(CmdArgs, options::OPT_D, options::OPT_U, options::OPT_A);
2208 Args.AddAllArgs(CmdArgs, options::OPT_i_Group);
2209
2210 for (InputInfoList::const_iterator
2211 it = Inputs.begin(), ie = Inputs.end(); it != ie; ++it) {
2212 const InputInfo &II = *it;
Daniel Dunbarc21c4852009-04-08 23:54:23 +00002213
Daniel Dunbar7c1e4652010-08-02 02:38:21 +00002214 CmdArgs.push_back(II.getFilename());
Daniel Dunbara3ec60e2009-03-29 18:40:18 +00002215 }
2216
2217 Args.AddAllArgValues(CmdArgs, options::OPT_Wp_COMMA,
2218 options::OPT_Xpreprocessor);
2219
2220 if (Args.hasArg(options::OPT_fmudflap)) {
2221 CmdArgs.push_back("-D_MUDFLAP");
2222 CmdArgs.push_back("-include");
2223 CmdArgs.push_back("mf-runtime.h");
2224 }
2225
2226 if (Args.hasArg(options::OPT_fmudflapth)) {
2227 CmdArgs.push_back("-D_MUDFLAP");
2228 CmdArgs.push_back("-D_MUDFLAPTH");
2229 CmdArgs.push_back("-include");
2230 CmdArgs.push_back("mf-runtime.h");
2231 }
2232}
2233
Daniel Dunbarc21c4852009-04-08 23:54:23 +00002234void darwin::CC1::AddCPPArgs(const ArgList &Args,
Daniel Dunbara3ec60e2009-03-29 18:40:18 +00002235 ArgStringList &CmdArgs) const {
2236 // Derived from cpp spec.
2237
2238 if (Args.hasArg(options::OPT_static)) {
2239 // The gcc spec is broken here, it refers to dynamic but
2240 // that has been translated. Start by being bug compatible.
Daniel Dunbarc21c4852009-04-08 23:54:23 +00002241
Daniel Dunbara3ec60e2009-03-29 18:40:18 +00002242 // if (!Args.hasArg(arglist.parser.dynamicOption))
2243 CmdArgs.push_back("-D__STATIC__");
2244 } else
2245 CmdArgs.push_back("-D__DYNAMIC__");
2246
2247 if (Args.hasArg(options::OPT_pthread))
2248 CmdArgs.push_back("-D_REENTRANT");
2249}
2250
Daniel Dunbar40f12652009-03-29 17:08:39 +00002251void darwin::Preprocess::ConstructJob(Compilation &C, const JobAction &JA,
Daniel Dunbar2fe238e2010-08-02 02:38:28 +00002252 const InputInfo &Output,
Daniel Dunbarc21c4852009-04-08 23:54:23 +00002253 const InputInfoList &Inputs,
2254 const ArgList &Args,
Daniel Dunbar40f12652009-03-29 17:08:39 +00002255 const char *LinkingOutput) const {
2256 ArgStringList CmdArgs;
2257
2258 assert(Inputs.size() == 1 && "Unexpected number of inputs!");
2259
2260 CmdArgs.push_back("-E");
2261
2262 if (Args.hasArg(options::OPT_traditional) ||
Daniel Dunbar40f12652009-03-29 17:08:39 +00002263 Args.hasArg(options::OPT_traditional_cpp))
2264 CmdArgs.push_back("-traditional-cpp");
Daniel Dunbarc21c4852009-04-08 23:54:23 +00002265
Daniel Dunbar40f12652009-03-29 17:08:39 +00002266 ArgStringList OutputArgs;
Daniel Dunbar7c1e4652010-08-02 02:38:21 +00002267 assert(Output.isFilename() && "Unexpected CC1 output.");
2268 OutputArgs.push_back("-o");
2269 OutputArgs.push_back(Output.getFilename());
Daniel Dunbar40f12652009-03-29 17:08:39 +00002270
Daniel Dunbar9120f172009-03-29 22:27:40 +00002271 if (Args.hasArg(options::OPT_E)) {
2272 AddCPPOptionsArgs(Args, CmdArgs, Inputs, OutputArgs);
2273 } else {
2274 AddCPPOptionsArgs(Args, CmdArgs, Inputs, ArgStringList());
2275 CmdArgs.append(OutputArgs.begin(), OutputArgs.end());
2276 }
Daniel Dunbarc21c4852009-04-08 23:54:23 +00002277
Daniel Dunbar8a2073a2009-04-03 01:27:06 +00002278 Args.AddAllArgs(CmdArgs, options::OPT_d_Group);
2279
Daniel Dunbar40f12652009-03-29 17:08:39 +00002280 const char *CC1Name = getCC1Name(Inputs[0].getType());
Daniel Dunbarc21c4852009-04-08 23:54:23 +00002281 const char *Exec =
Daniel Dunbar4a7e8892010-07-14 18:46:23 +00002282 Args.MakeArgString(getToolChain().GetProgramPath(CC1Name));
Daniel Dunbar2fe238e2010-08-02 02:38:28 +00002283 C.addCommand(new Command(JA, *this, Exec, CmdArgs));
Daniel Dunbar40f12652009-03-29 17:08:39 +00002284}
2285
2286void darwin::Compile::ConstructJob(Compilation &C, const JobAction &JA,
Daniel Dunbar2fe238e2010-08-02 02:38:28 +00002287 const InputInfo &Output,
Daniel Dunbarc21c4852009-04-08 23:54:23 +00002288 const InputInfoList &Inputs,
2289 const ArgList &Args,
Daniel Dunbar40f12652009-03-29 17:08:39 +00002290 const char *LinkingOutput) const {
Daniel Dunbaree788e72009-12-21 18:54:17 +00002291 const Driver &D = getToolChain().getDriver();
Daniel Dunbar40f12652009-03-29 17:08:39 +00002292 ArgStringList CmdArgs;
2293
2294 assert(Inputs.size() == 1 && "Unexpected number of inputs!");
2295
2296 types::ID InputType = Inputs[0].getType();
2297 const Arg *A;
Eli Friedmanceb5c5b2009-07-14 21:58:17 +00002298 if ((A = Args.getLastArg(options::OPT_traditional)))
Daniel Dunbar40f12652009-03-29 17:08:39 +00002299 D.Diag(clang::diag::err_drv_argument_only_allowed_with)
2300 << A->getAsString(Args) << "-E";
2301
Daniel Dunbar6c6424b2010-06-07 23:28:45 +00002302 if (JA.getType() == types::TY_LLVM_IR ||
2303 JA.getType() == types::TY_LTO_IR)
Daniel Dunbar40f12652009-03-29 17:08:39 +00002304 CmdArgs.push_back("-emit-llvm");
Daniel Dunbar6c6424b2010-06-07 23:28:45 +00002305 else if (JA.getType() == types::TY_LLVM_BC ||
2306 JA.getType() == types::TY_LTO_BC)
Daniel Dunbar40f12652009-03-29 17:08:39 +00002307 CmdArgs.push_back("-emit-llvm-bc");
Daniel Dunbar5915fbf2009-09-01 16:57:46 +00002308 else if (Output.getType() == types::TY_AST)
2309 D.Diag(clang::diag::err_drv_no_ast_support)
Daniel Dunbar88137642009-09-09 22:32:48 +00002310 << getToolChain().getTripleString();
Daniel Dunbarae24a882010-02-11 17:33:45 +00002311 else if (JA.getType() != types::TY_PP_Asm &&
2312 JA.getType() != types::TY_PCH)
Daniel Dunbar64952502010-02-11 03:16:21 +00002313 D.Diag(clang::diag::err_drv_invalid_gcc_output_type)
2314 << getTypeName(JA.getType());
Daniel Dunbar40f12652009-03-29 17:08:39 +00002315
2316 ArgStringList OutputArgs;
2317 if (Output.getType() != types::TY_PCH) {
2318 OutputArgs.push_back("-o");
Daniel Dunbar7c1e4652010-08-02 02:38:21 +00002319 if (Output.isNothing())
Daniel Dunbar40f12652009-03-29 17:08:39 +00002320 OutputArgs.push_back("/dev/null");
2321 else
2322 OutputArgs.push_back(Output.getFilename());
2323 }
2324
2325 // There is no need for this level of compatibility, but it makes
2326 // diffing easier.
2327 bool OutputArgsEarly = (Args.hasArg(options::OPT_fsyntax_only) ||
2328 Args.hasArg(options::OPT_S));
2329
2330 if (types::getPreprocessedType(InputType) != types::TY_INVALID) {
Daniel Dunbara3ec60e2009-03-29 18:40:18 +00002331 AddCPPUniqueOptionsArgs(Args, CmdArgs, Inputs);
Daniel Dunbar40f12652009-03-29 17:08:39 +00002332 if (OutputArgsEarly) {
2333 AddCC1OptionsArgs(Args, CmdArgs, Inputs, OutputArgs);
2334 } else {
2335 AddCC1OptionsArgs(Args, CmdArgs, Inputs, ArgStringList());
2336 CmdArgs.append(OutputArgs.begin(), OutputArgs.end());
2337 }
2338 } else {
2339 CmdArgs.push_back("-fpreprocessed");
Daniel Dunbarc21c4852009-04-08 23:54:23 +00002340
Daniel Dunbar40f12652009-03-29 17:08:39 +00002341 for (InputInfoList::const_iterator
2342 it = Inputs.begin(), ie = Inputs.end(); it != ie; ++it) {
2343 const InputInfo &II = *it;
2344
Daniel Dunbar5915fbf2009-09-01 16:57:46 +00002345 // Reject AST inputs.
2346 if (II.getType() == types::TY_AST) {
2347 D.Diag(clang::diag::err_drv_no_ast_support)
Daniel Dunbar88137642009-09-09 22:32:48 +00002348 << getToolChain().getTripleString();
Daniel Dunbar5915fbf2009-09-01 16:57:46 +00002349 return;
2350 }
2351
Daniel Dunbar7c1e4652010-08-02 02:38:21 +00002352 CmdArgs.push_back(II.getFilename());
Daniel Dunbar40f12652009-03-29 17:08:39 +00002353 }
2354
2355 if (OutputArgsEarly) {
2356 AddCC1OptionsArgs(Args, CmdArgs, Inputs, OutputArgs);
2357 } else {
2358 AddCC1OptionsArgs(Args, CmdArgs, Inputs, ArgStringList());
2359 CmdArgs.append(OutputArgs.begin(), OutputArgs.end());
2360 }
2361 }
Daniel Dunbarc21c4852009-04-08 23:54:23 +00002362
Daniel Dunbar40f12652009-03-29 17:08:39 +00002363 if (Output.getType() == types::TY_PCH) {
2364 assert(Output.isFilename() && "Invalid PCH output.");
2365
2366 CmdArgs.push_back("-o");
2367 // NOTE: gcc uses a temp .s file for this, but there doesn't seem
2368 // to be a good reason.
2369 CmdArgs.push_back("/dev/null");
Daniel Dunbarc21c4852009-04-08 23:54:23 +00002370
Daniel Dunbare6c9ae12009-11-21 02:31:29 +00002371 CmdArgs.push_back("--output-pch=");
Daniel Dunbar40f12652009-03-29 17:08:39 +00002372 CmdArgs.push_back(Output.getFilename());
Daniel Dunbarc21c4852009-04-08 23:54:23 +00002373 }
Daniel Dunbar40f12652009-03-29 17:08:39 +00002374
2375 const char *CC1Name = getCC1Name(Inputs[0].getType());
Daniel Dunbarc21c4852009-04-08 23:54:23 +00002376 const char *Exec =
Daniel Dunbar4a7e8892010-07-14 18:46:23 +00002377 Args.MakeArgString(getToolChain().GetProgramPath(CC1Name));
Daniel Dunbar2fe238e2010-08-02 02:38:28 +00002378 C.addCommand(new Command(JA, *this, Exec, CmdArgs));
Daniel Dunbar40f12652009-03-29 17:08:39 +00002379}
2380
Daniel Dunbar8cac5f72009-03-20 16:06:39 +00002381void darwin::Assemble::ConstructJob(Compilation &C, const JobAction &JA,
Daniel Dunbar2fe238e2010-08-02 02:38:28 +00002382 const InputInfo &Output,
Daniel Dunbarc21c4852009-04-08 23:54:23 +00002383 const InputInfoList &Inputs,
2384 const ArgList &Args,
Daniel Dunbar8cac5f72009-03-20 16:06:39 +00002385 const char *LinkingOutput) const {
2386 ArgStringList CmdArgs;
2387
2388 assert(Inputs.size() == 1 && "Unexpected number of inputs.");
2389 const InputInfo &Input = Inputs[0];
2390
2391 // Bit of a hack, this is only used for original inputs.
Daniel Dunbarc21c4852009-04-08 23:54:23 +00002392 //
Daniel Dunbar8e4fea62009-04-01 00:27:44 +00002393 // FIXME: This is broken for preprocessed .s inputs.
Daniel Dunbar8cac5f72009-03-20 16:06:39 +00002394 if (Input.isFilename() &&
Daniel Dunbar8e4fea62009-04-01 00:27:44 +00002395 strcmp(Input.getFilename(), Input.getBaseInput()) == 0) {
2396 if (Args.hasArg(options::OPT_gstabs))
2397 CmdArgs.push_back("--gstabs");
2398 else if (Args.hasArg(options::OPT_g_Group))
2399 CmdArgs.push_back("--gdwarf2");
2400 }
Daniel Dunbarc21c4852009-04-08 23:54:23 +00002401
Daniel Dunbar8cac5f72009-03-20 16:06:39 +00002402 // Derived from asm spec.
Daniel Dunbarcc6f8032009-09-09 18:36:27 +00002403 AddDarwinArch(Args, CmdArgs);
Daniel Dunbar8cac5f72009-03-20 16:06:39 +00002404
Daniel Dunbarf5438e32010-07-22 01:47:22 +00002405 // Use -force_cpusubtype_ALL on x86 by default.
2406 if (getToolChain().getTriple().getArch() == llvm::Triple::x86 ||
2407 getToolChain().getTriple().getArch() == llvm::Triple::x86_64 ||
Daniel Dunbarcc6f8032009-09-09 18:36:27 +00002408 Args.hasArg(options::OPT_force__cpusubtype__ALL))
2409 CmdArgs.push_back("-force_cpusubtype_ALL");
2410
Daniel Dunbar0e2679d2009-08-24 22:26:16 +00002411 if (getToolChain().getTriple().getArch() != llvm::Triple::x86_64 &&
2412 (Args.hasArg(options::OPT_mkernel) ||
Daniel Dunbar8cac5f72009-03-20 16:06:39 +00002413 Args.hasArg(options::OPT_static) ||
Daniel Dunbar0e2679d2009-08-24 22:26:16 +00002414 Args.hasArg(options::OPT_fapple_kext)))
Daniel Dunbarc21c4852009-04-08 23:54:23 +00002415 CmdArgs.push_back("-static");
2416
Daniel Dunbar8cac5f72009-03-20 16:06:39 +00002417 Args.AddAllArgValues(CmdArgs, options::OPT_Wa_COMMA,
2418 options::OPT_Xassembler);
2419
2420 assert(Output.isFilename() && "Unexpected lipo output.");
2421 CmdArgs.push_back("-o");
2422 CmdArgs.push_back(Output.getFilename());
2423
Daniel Dunbar7c1e4652010-08-02 02:38:21 +00002424 assert(Input.isFilename() && "Invalid input.");
2425 CmdArgs.push_back(Input.getFilename());
Daniel Dunbar8cac5f72009-03-20 16:06:39 +00002426
2427 // asm_final spec is empty.
2428
Daniel Dunbarc21c4852009-04-08 23:54:23 +00002429 const char *Exec =
Daniel Dunbar4a7e8892010-07-14 18:46:23 +00002430 Args.MakeArgString(getToolChain().GetProgramPath("as"));
Daniel Dunbar2fe238e2010-08-02 02:38:28 +00002431 C.addCommand(new Command(JA, *this, Exec, CmdArgs));
Daniel Dunbar8cac5f72009-03-20 16:06:39 +00002432}
Daniel Dunbarff7488d2009-03-20 00:52:38 +00002433
Daniel Dunbarfbefe6b2009-09-09 18:36:20 +00002434void darwin::DarwinTool::AddDarwinArch(const ArgList &Args,
2435 ArgStringList &CmdArgs) const {
Daniel Dunbareeff4062010-01-22 02:04:58 +00002436 llvm::StringRef ArchName = getDarwinToolChain().getDarwinArchName(Args);
2437
Daniel Dunbar02633b52009-03-26 16:23:12 +00002438 // Derived from darwin_arch spec.
2439 CmdArgs.push_back("-arch");
Daniel Dunbareeff4062010-01-22 02:04:58 +00002440 CmdArgs.push_back(Args.MakeArgString(ArchName));
Daniel Dunbar78dbd582009-09-04 18:35:31 +00002441
Daniel Dunbareeff4062010-01-22 02:04:58 +00002442 // FIXME: Is this needed anymore?
2443 if (ArchName == "arm")
Daniel Dunbar78dbd582009-09-04 18:35:31 +00002444 CmdArgs.push_back("-force_cpusubtype_ALL");
Daniel Dunbar02633b52009-03-26 16:23:12 +00002445}
2446
Daniel Dunbar748de8e2010-09-09 21:51:05 +00002447void darwin::Link::AddLinkArgs(Compilation &C,
2448 const ArgList &Args,
Daniel Dunbar02633b52009-03-26 16:23:12 +00002449 ArgStringList &CmdArgs) const {
Daniel Dunbaree788e72009-12-21 18:54:17 +00002450 const Driver &D = getToolChain().getDriver();
Daniel Dunbar02633b52009-03-26 16:23:12 +00002451
Daniel Dunbarb18dc5b2010-08-11 23:07:50 +00002452 unsigned Version[3] = { 0, 0, 0 };
2453 if (Arg *A = Args.getLastArg(options::OPT_mlinker_version_EQ)) {
2454 bool HadExtra;
2455 if (!Driver::GetReleaseVersion(A->getValue(Args), Version[0],
2456 Version[1], Version[2], HadExtra) ||
2457 HadExtra)
2458 D.Diag(clang::diag::err_drv_invalid_version_number)
2459 << A->getAsString(Args);
2460 }
2461
2462 // Newer linkers support -demangle, pass it if supported and not disabled by
2463 // the user.
Daniel Dunbara30d6cd2010-11-19 17:51:40 +00002464 //
2465 // FIXME: We temporarily avoid passing -demangle to any iOS linker, because
2466 // unfortunately we can't be guaranteed that the linker version used there
2467 // will match the linker version detected at configure time. We need the
2468 // universal driver.
2469 if (Version[0] >= 100 && !Args.hasArg(options::OPT_Z_Xlinker__no_demangle) &&
2470 !getDarwinToolChain().isTargetIPhoneOS()) {
Daniel Dunbarbcf1da82010-09-07 17:07:49 +00002471 // Don't pass -demangle to ld_classic.
2472 //
2473 // FIXME: This is a temporary workaround, ld should be handling this.
2474 bool UsesLdClassic = (getToolChain().getArch() == llvm::Triple::x86 &&
2475 Args.hasArg(options::OPT_static));
Daniel Dunbar9ced7042010-09-07 17:50:41 +00002476 if (getToolChain().getArch() == llvm::Triple::x86) {
2477 for (arg_iterator it = Args.filtered_begin(options::OPT_Xlinker,
2478 options::OPT_Wl_COMMA),
2479 ie = Args.filtered_end(); it != ie; ++it) {
2480 const Arg *A = *it;
2481 for (unsigned i = 0, e = A->getNumValues(); i != e; ++i)
2482 if (llvm::StringRef(A->getValue(Args, i)) == "-kext")
2483 UsesLdClassic = true;
2484 }
2485 }
Daniel Dunbarbcf1da82010-09-07 17:07:49 +00002486 if (!UsesLdClassic)
2487 CmdArgs.push_back("-demangle");
Daniel Dunbarb18dc5b2010-08-11 23:07:50 +00002488 }
2489
Daniel Dunbar02633b52009-03-26 16:23:12 +00002490 // Derived from the "link" spec.
2491 Args.AddAllArgs(CmdArgs, options::OPT_static);
2492 if (!Args.hasArg(options::OPT_static))
2493 CmdArgs.push_back("-dynamic");
2494 if (Args.hasArg(options::OPT_fgnu_runtime)) {
2495 // FIXME: gcc replaces -lobjc in forward args with -lobjc-gnu
2496 // here. How do we wish to handle such things?
2497 }
Daniel Dunbarc21c4852009-04-08 23:54:23 +00002498
Daniel Dunbar02633b52009-03-26 16:23:12 +00002499 if (!Args.hasArg(options::OPT_dynamiclib)) {
Daniel Dunbara6d38492010-01-22 02:04:52 +00002500 AddDarwinArch(Args, CmdArgs);
Daniel Dunbara6d38492010-01-22 02:04:52 +00002501 // FIXME: Why do this only on this path?
Daniel Dunbar8917dd42010-01-22 03:37:33 +00002502 Args.AddLastArg(CmdArgs, options::OPT_force__cpusubtype__ALL);
Daniel Dunbar02633b52009-03-26 16:23:12 +00002503
2504 Args.AddLastArg(CmdArgs, options::OPT_bundle);
2505 Args.AddAllArgs(CmdArgs, options::OPT_bundle__loader);
2506 Args.AddAllArgs(CmdArgs, options::OPT_client__name);
2507
2508 Arg *A;
2509 if ((A = Args.getLastArg(options::OPT_compatibility__version)) ||
2510 (A = Args.getLastArg(options::OPT_current__version)) ||
2511 (A = Args.getLastArg(options::OPT_install__name)))
2512 D.Diag(clang::diag::err_drv_argument_only_allowed_with)
2513 << A->getAsString(Args) << "-dynamiclib";
2514
2515 Args.AddLastArg(CmdArgs, options::OPT_force__flat__namespace);
2516 Args.AddLastArg(CmdArgs, options::OPT_keep__private__externs);
2517 Args.AddLastArg(CmdArgs, options::OPT_private__bundle);
2518 } else {
2519 CmdArgs.push_back("-dylib");
2520
2521 Arg *A;
2522 if ((A = Args.getLastArg(options::OPT_bundle)) ||
2523 (A = Args.getLastArg(options::OPT_bundle__loader)) ||
2524 (A = Args.getLastArg(options::OPT_client__name)) ||
2525 (A = Args.getLastArg(options::OPT_force__flat__namespace)) ||
2526 (A = Args.getLastArg(options::OPT_keep__private__externs)) ||
2527 (A = Args.getLastArg(options::OPT_private__bundle)))
2528 D.Diag(clang::diag::err_drv_argument_not_allowed_with)
2529 << A->getAsString(Args) << "-dynamiclib";
Daniel Dunbarc21c4852009-04-08 23:54:23 +00002530
Daniel Dunbar02633b52009-03-26 16:23:12 +00002531 Args.AddAllArgsTranslated(CmdArgs, options::OPT_compatibility__version,
2532 "-dylib_compatibility_version");
2533 Args.AddAllArgsTranslated(CmdArgs, options::OPT_current__version,
2534 "-dylib_current_version");
2535
Daniel Dunbara6d38492010-01-22 02:04:52 +00002536 AddDarwinArch(Args, CmdArgs);
Daniel Dunbar02633b52009-03-26 16:23:12 +00002537
2538 Args.AddAllArgsTranslated(CmdArgs, options::OPT_install__name,
2539 "-dylib_install_name");
2540 }
2541
2542 Args.AddLastArg(CmdArgs, options::OPT_all__load);
2543 Args.AddAllArgs(CmdArgs, options::OPT_allowable__client);
2544 Args.AddLastArg(CmdArgs, options::OPT_bind__at__load);
Daniel Dunbar251ca6c2010-01-27 00:56:37 +00002545 if (getDarwinToolChain().isTargetIPhoneOS())
Daniel Dunbard82f8fa2009-09-04 18:35:41 +00002546 Args.AddLastArg(CmdArgs, options::OPT_arch__errors__fatal);
Daniel Dunbar02633b52009-03-26 16:23:12 +00002547 Args.AddLastArg(CmdArgs, options::OPT_dead__strip);
2548 Args.AddLastArg(CmdArgs, options::OPT_no__dead__strip__inits__and__terms);
2549 Args.AddAllArgs(CmdArgs, options::OPT_dylib__file);
2550 Args.AddLastArg(CmdArgs, options::OPT_dynamic);
2551 Args.AddAllArgs(CmdArgs, options::OPT_exported__symbols__list);
2552 Args.AddLastArg(CmdArgs, options::OPT_flat__namespace);
2553 Args.AddAllArgs(CmdArgs, options::OPT_headerpad__max__install__names);
2554 Args.AddAllArgs(CmdArgs, options::OPT_image__base);
2555 Args.AddAllArgs(CmdArgs, options::OPT_init);
2556
Daniel Dunbar314cba02010-01-26 03:56:40 +00002557 // Adding all arguments doesn't make sense here but this is what gcc does. One
2558 // of this should always be present thanks to argument translation.
2559 assert((Args.hasArg(options::OPT_mmacosx_version_min_EQ) ||
2560 Args.hasArg(options::OPT_miphoneos_version_min_EQ)) &&
2561 "Missing version argument (lost in translation)?");
Daniel Dunbar30392de2009-09-04 18:35:21 +00002562 Args.AddAllArgsTranslated(CmdArgs, options::OPT_mmacosx_version_min_EQ,
Daniel Dunbar294691e2009-11-04 06:24:38 +00002563 "-macosx_version_min");
Daniel Dunbar02633b52009-03-26 16:23:12 +00002564 Args.AddAllArgsTranslated(CmdArgs, options::OPT_miphoneos_version_min_EQ,
2565 "-iphoneos_version_min");
2566 Args.AddLastArg(CmdArgs, options::OPT_nomultidefs);
2567 Args.AddLastArg(CmdArgs, options::OPT_multi__module);
2568 Args.AddLastArg(CmdArgs, options::OPT_single__module);
2569 Args.AddAllArgs(CmdArgs, options::OPT_multiply__defined);
2570 Args.AddAllArgs(CmdArgs, options::OPT_multiply__defined__unused);
Daniel Dunbarc21c4852009-04-08 23:54:23 +00002571
Daniel Dunbar47e879d2010-07-13 23:31:40 +00002572 if (const Arg *A = Args.getLastArg(options::OPT_fpie, options::OPT_fPIE,
2573 options::OPT_fno_pie,
2574 options::OPT_fno_PIE)) {
2575 if (A->getOption().matches(options::OPT_fpie) ||
2576 A->getOption().matches(options::OPT_fPIE))
2577 CmdArgs.push_back("-pie");
2578 else
2579 CmdArgs.push_back("-no_pie");
2580 }
Daniel Dunbar02633b52009-03-26 16:23:12 +00002581
2582 Args.AddLastArg(CmdArgs, options::OPT_prebind);
2583 Args.AddLastArg(CmdArgs, options::OPT_noprebind);
2584 Args.AddLastArg(CmdArgs, options::OPT_nofixprebinding);
2585 Args.AddLastArg(CmdArgs, options::OPT_prebind__all__twolevel__modules);
2586 Args.AddLastArg(CmdArgs, options::OPT_read__only__relocs);
2587 Args.AddAllArgs(CmdArgs, options::OPT_sectcreate);
2588 Args.AddAllArgs(CmdArgs, options::OPT_sectorder);
2589 Args.AddAllArgs(CmdArgs, options::OPT_seg1addr);
2590 Args.AddAllArgs(CmdArgs, options::OPT_segprot);
2591 Args.AddAllArgs(CmdArgs, options::OPT_segaddr);
2592 Args.AddAllArgs(CmdArgs, options::OPT_segs__read__only__addr);
2593 Args.AddAllArgs(CmdArgs, options::OPT_segs__read__write__addr);
2594 Args.AddAllArgs(CmdArgs, options::OPT_seg__addr__table);
2595 Args.AddAllArgs(CmdArgs, options::OPT_seg__addr__table__filename);
2596 Args.AddAllArgs(CmdArgs, options::OPT_sub__library);
2597 Args.AddAllArgs(CmdArgs, options::OPT_sub__umbrella);
Daniel Dunbard82f8fa2009-09-04 18:35:41 +00002598
Daniel Dunbar02633b52009-03-26 16:23:12 +00002599 Args.AddAllArgsTranslated(CmdArgs, options::OPT_isysroot, "-syslibroot");
Daniel Dunbar251ca6c2010-01-27 00:56:37 +00002600 if (getDarwinToolChain().isTargetIPhoneOS()) {
Daniel Dunbard82f8fa2009-09-04 18:35:41 +00002601 if (!Args.hasArg(options::OPT_isysroot)) {
2602 CmdArgs.push_back("-syslibroot");
2603 CmdArgs.push_back("/Developer/SDKs/Extra");
2604 }
2605 }
2606
Daniel Dunbar02633b52009-03-26 16:23:12 +00002607 Args.AddLastArg(CmdArgs, options::OPT_twolevel__namespace);
2608 Args.AddLastArg(CmdArgs, options::OPT_twolevel__namespace__hints);
2609 Args.AddAllArgs(CmdArgs, options::OPT_umbrella);
2610 Args.AddAllArgs(CmdArgs, options::OPT_undefined);
2611 Args.AddAllArgs(CmdArgs, options::OPT_unexported__symbols__list);
Daniel Dunbard82f8fa2009-09-04 18:35:41 +00002612 Args.AddAllArgs(CmdArgs, options::OPT_weak__reference__mismatches);
Daniel Dunbar02633b52009-03-26 16:23:12 +00002613 Args.AddLastArg(CmdArgs, options::OPT_X_Flag);
2614 Args.AddAllArgs(CmdArgs, options::OPT_y);
2615 Args.AddLastArg(CmdArgs, options::OPT_w);
2616 Args.AddAllArgs(CmdArgs, options::OPT_pagezero__size);
2617 Args.AddAllArgs(CmdArgs, options::OPT_segs__read__);
2618 Args.AddLastArg(CmdArgs, options::OPT_seglinkedit);
2619 Args.AddLastArg(CmdArgs, options::OPT_noseglinkedit);
2620 Args.AddAllArgs(CmdArgs, options::OPT_sectalign);
2621 Args.AddAllArgs(CmdArgs, options::OPT_sectobjectsymbols);
2622 Args.AddAllArgs(CmdArgs, options::OPT_segcreate);
2623 Args.AddLastArg(CmdArgs, options::OPT_whyload);
2624 Args.AddLastArg(CmdArgs, options::OPT_whatsloaded);
2625 Args.AddAllArgs(CmdArgs, options::OPT_dylinker__install__name);
2626 Args.AddLastArg(CmdArgs, options::OPT_dylinker);
2627 Args.AddLastArg(CmdArgs, options::OPT_Mach);
2628}
2629
2630void darwin::Link::ConstructJob(Compilation &C, const JobAction &JA,
Daniel Dunbar2fe238e2010-08-02 02:38:28 +00002631 const InputInfo &Output,
Daniel Dunbarc21c4852009-04-08 23:54:23 +00002632 const InputInfoList &Inputs,
2633 const ArgList &Args,
Daniel Dunbar02633b52009-03-26 16:23:12 +00002634 const char *LinkingOutput) const {
2635 assert(Output.getType() == types::TY_Image && "Invalid linker output type.");
Daniel Dunbare0be8b12009-09-08 16:39:16 +00002636
Daniel Dunbar02633b52009-03-26 16:23:12 +00002637 // The logic here is derived from gcc's behavior; most of which
2638 // comes from specs (starting with link_command). Consult gcc for
2639 // more information.
Daniel Dunbar02633b52009-03-26 16:23:12 +00002640 ArgStringList CmdArgs;
2641
2642 // I'm not sure why this particular decomposition exists in gcc, but
2643 // we follow suite for ease of comparison.
Daniel Dunbar748de8e2010-09-09 21:51:05 +00002644 AddLinkArgs(C, Args, CmdArgs);
Daniel Dunbar02633b52009-03-26 16:23:12 +00002645
Daniel Dunbar02633b52009-03-26 16:23:12 +00002646 Args.AddAllArgs(CmdArgs, options::OPT_d_Flag);
2647 Args.AddAllArgs(CmdArgs, options::OPT_s);
2648 Args.AddAllArgs(CmdArgs, options::OPT_t);
2649 Args.AddAllArgs(CmdArgs, options::OPT_Z_Flag);
2650 Args.AddAllArgs(CmdArgs, options::OPT_u_Group);
2651 Args.AddAllArgs(CmdArgs, options::OPT_A);
2652 Args.AddLastArg(CmdArgs, options::OPT_e);
2653 Args.AddAllArgs(CmdArgs, options::OPT_m_Separate);
2654 Args.AddAllArgs(CmdArgs, options::OPT_r);
2655
Daniel Dunbar270073c2010-10-18 22:08:36 +00002656 // Forward -ObjC when either -ObjC or -ObjC++ is used, to force loading
2657 // members of static archive libraries which implement Objective-C classes or
2658 // categories.
2659 if (Args.hasArg(options::OPT_ObjC) || Args.hasArg(options::OPT_ObjCXX))
2660 CmdArgs.push_back("-ObjC");
Michael J. Spencer20249a12010-10-21 03:16:25 +00002661
Daniel Dunbar02633b52009-03-26 16:23:12 +00002662 CmdArgs.push_back("-o");
2663 CmdArgs.push_back(Output.getFilename());
2664
Daniel Dunbar02633b52009-03-26 16:23:12 +00002665 if (!Args.hasArg(options::OPT_A) &&
2666 !Args.hasArg(options::OPT_nostdlib) &&
2667 !Args.hasArg(options::OPT_nostartfiles)) {
2668 // Derived from startfile spec.
2669 if (Args.hasArg(options::OPT_dynamiclib)) {
Daniel Dunbarc21c4852009-04-08 23:54:23 +00002670 // Derived from darwin_dylib1 spec.
Daniel Dunbarcacb0f02010-01-27 00:56:56 +00002671 if (getDarwinToolChain().isTargetIPhoneOS()) {
2672 if (getDarwinToolChain().isIPhoneOSVersionLT(3, 1))
2673 CmdArgs.push_back("-ldylib1.o");
2674 } else {
Daniel Dunbarce3fdf22010-01-27 00:57:03 +00002675 if (getDarwinToolChain().isMacosxVersionLT(10, 5))
Daniel Dunbarcacb0f02010-01-27 00:56:56 +00002676 CmdArgs.push_back("-ldylib1.o");
Daniel Dunbarce3fdf22010-01-27 00:57:03 +00002677 else if (getDarwinToolChain().isMacosxVersionLT(10, 6))
Daniel Dunbarcacb0f02010-01-27 00:56:56 +00002678 CmdArgs.push_back("-ldylib1.10.5.o");
2679 }
Daniel Dunbar02633b52009-03-26 16:23:12 +00002680 } else {
2681 if (Args.hasArg(options::OPT_bundle)) {
Daniel Dunbar8a8d8af2009-04-01 03:17:40 +00002682 if (!Args.hasArg(options::OPT_static)) {
2683 // Derived from darwin_bundle1 spec.
Daniel Dunbarcacb0f02010-01-27 00:56:56 +00002684 if (getDarwinToolChain().isTargetIPhoneOS()) {
2685 if (getDarwinToolChain().isIPhoneOSVersionLT(3, 1))
2686 CmdArgs.push_back("-lbundle1.o");
2687 } else {
Daniel Dunbarce3fdf22010-01-27 00:57:03 +00002688 if (getDarwinToolChain().isMacosxVersionLT(10, 6))
Daniel Dunbarcacb0f02010-01-27 00:56:56 +00002689 CmdArgs.push_back("-lbundle1.o");
2690 }
Daniel Dunbar8a8d8af2009-04-01 03:17:40 +00002691 }
Daniel Dunbar02633b52009-03-26 16:23:12 +00002692 } else {
2693 if (Args.hasArg(options::OPT_pg)) {
2694 if (Args.hasArg(options::OPT_static) ||
2695 Args.hasArg(options::OPT_object) ||
2696 Args.hasArg(options::OPT_preload)) {
2697 CmdArgs.push_back("-lgcrt0.o");
2698 } else {
2699 CmdArgs.push_back("-lgcrt1.o");
Daniel Dunbarc21c4852009-04-08 23:54:23 +00002700
Daniel Dunbar02633b52009-03-26 16:23:12 +00002701 // darwin_crt2 spec is empty.
Daniel Dunbarc21c4852009-04-08 23:54:23 +00002702 }
Daniel Dunbar02633b52009-03-26 16:23:12 +00002703 } else {
2704 if (Args.hasArg(options::OPT_static) ||
2705 Args.hasArg(options::OPT_object) ||
2706 Args.hasArg(options::OPT_preload)) {
2707 CmdArgs.push_back("-lcrt0.o");
2708 } else {
2709 // Derived from darwin_crt1 spec.
Daniel Dunbar251ca6c2010-01-27 00:56:37 +00002710 if (getDarwinToolChain().isTargetIPhoneOS()) {
Daniel Dunbarcacb0f02010-01-27 00:56:56 +00002711 if (getDarwinToolChain().isIPhoneOSVersionLT(3, 1))
2712 CmdArgs.push_back("-lcrt1.o");
2713 else
2714 CmdArgs.push_back("-lcrt1.3.1.o");
Daniel Dunbarce3fdf22010-01-27 00:57:03 +00002715 } else {
2716 if (getDarwinToolChain().isMacosxVersionLT(10, 5))
2717 CmdArgs.push_back("-lcrt1.o");
2718 else if (getDarwinToolChain().isMacosxVersionLT(10, 6))
2719 CmdArgs.push_back("-lcrt1.10.5.o");
2720 else
2721 CmdArgs.push_back("-lcrt1.10.6.o");
Daniel Dunbarc21c4852009-04-08 23:54:23 +00002722
Daniel Dunbarce3fdf22010-01-27 00:57:03 +00002723 // darwin_crt2 spec is empty.
2724 }
Daniel Dunbar02633b52009-03-26 16:23:12 +00002725 }
2726 }
2727 }
2728 }
2729
Daniel Dunbarce3fdf22010-01-27 00:57:03 +00002730 if (!getDarwinToolChain().isTargetIPhoneOS() &&
2731 Args.hasArg(options::OPT_shared_libgcc) &&
2732 getDarwinToolChain().isMacosxVersionLT(10, 5)) {
Daniel Dunbar88137642009-09-09 22:32:48 +00002733 const char *Str =
Daniel Dunbar4a7e8892010-07-14 18:46:23 +00002734 Args.MakeArgString(getToolChain().GetFilePath("crt3.o"));
Daniel Dunbar88137642009-09-09 22:32:48 +00002735 CmdArgs.push_back(Str);
Daniel Dunbar02633b52009-03-26 16:23:12 +00002736 }
2737 }
2738
2739 Args.AddAllArgs(CmdArgs, options::OPT_L);
Daniel Dunbarc21c4852009-04-08 23:54:23 +00002740
Daniel Dunbar02633b52009-03-26 16:23:12 +00002741 if (Args.hasArg(options::OPT_fopenmp))
2742 // This is more complicated in gcc...
2743 CmdArgs.push_back("-lgomp");
2744
Daniel Dunbar6b200b22009-09-18 08:14:36 +00002745 getDarwinToolChain().AddLinkSearchPathArgs(Args, CmdArgs);
Daniel Dunbarc21c4852009-04-08 23:54:23 +00002746
Daniel Dunbar2008fee2010-09-17 00:24:54 +00002747 AddLinkerInputs(getToolChain(), Inputs, Args, CmdArgs);
Daniel Dunbar02633b52009-03-26 16:23:12 +00002748
2749 if (LinkingOutput) {
2750 CmdArgs.push_back("-arch_multiple");
2751 CmdArgs.push_back("-final_output");
2752 CmdArgs.push_back(LinkingOutput);
2753 }
2754
2755 if (Args.hasArg(options::OPT_fprofile_arcs) ||
2756 Args.hasArg(options::OPT_fprofile_generate) ||
2757 Args.hasArg(options::OPT_fcreate_profile) ||
2758 Args.hasArg(options::OPT_coverage))
2759 CmdArgs.push_back("-lgcov");
Daniel Dunbarc21c4852009-04-08 23:54:23 +00002760
Daniel Dunbar02633b52009-03-26 16:23:12 +00002761 if (Args.hasArg(options::OPT_fnested_functions))
2762 CmdArgs.push_back("-allow_stack_execute");
Daniel Dunbarc21c4852009-04-08 23:54:23 +00002763
Daniel Dunbar02633b52009-03-26 16:23:12 +00002764 if (!Args.hasArg(options::OPT_nostdlib) &&
2765 !Args.hasArg(options::OPT_nodefaultlibs)) {
Daniel Dunbaree788e72009-12-21 18:54:17 +00002766 if (getToolChain().getDriver().CCCIsCXX)
Daniel Dunbar132e35d2010-09-17 01:20:05 +00002767 getToolChain().AddCXXStdlibLibArgs(Args, CmdArgs);
Daniel Dunbaredfa02b2009-04-08 06:06:21 +00002768
Daniel Dunbar02633b52009-03-26 16:23:12 +00002769 // link_ssp spec is empty.
2770
Daniel Dunbar6cd41542009-09-18 08:15:03 +00002771 // Let the tool chain choose which runtime library to link.
2772 getDarwinToolChain().AddLinkRuntimeLibArgs(Args, CmdArgs);
Daniel Dunbar02633b52009-03-26 16:23:12 +00002773 }
2774
2775 if (!Args.hasArg(options::OPT_A) &&
2776 !Args.hasArg(options::OPT_nostdlib) &&
2777 !Args.hasArg(options::OPT_nostartfiles)) {
2778 // endfile_spec is empty.
2779 }
2780
2781 Args.AddAllArgs(CmdArgs, options::OPT_T_Group);
2782 Args.AddAllArgs(CmdArgs, options::OPT_F);
2783
Daniel Dunbarc21c4852009-04-08 23:54:23 +00002784 const char *Exec =
Daniel Dunbar4a7e8892010-07-14 18:46:23 +00002785 Args.MakeArgString(getToolChain().GetProgramPath("ld"));
Daniel Dunbar2fe238e2010-08-02 02:38:28 +00002786 C.addCommand(new Command(JA, *this, Exec, CmdArgs));
Daniel Dunbar02633b52009-03-26 16:23:12 +00002787}
2788
Daniel Dunbarff7488d2009-03-20 00:52:38 +00002789void darwin::Lipo::ConstructJob(Compilation &C, const JobAction &JA,
Daniel Dunbar2fe238e2010-08-02 02:38:28 +00002790 const InputInfo &Output,
Daniel Dunbarc21c4852009-04-08 23:54:23 +00002791 const InputInfoList &Inputs,
2792 const ArgList &Args,
Daniel Dunbarff7488d2009-03-20 00:52:38 +00002793 const char *LinkingOutput) const {
2794 ArgStringList CmdArgs;
2795
2796 CmdArgs.push_back("-create");
2797 assert(Output.isFilename() && "Unexpected lipo output.");
Daniel Dunbara428df82009-03-24 00:24:37 +00002798
2799 CmdArgs.push_back("-output");
Daniel Dunbarff7488d2009-03-20 00:52:38 +00002800 CmdArgs.push_back(Output.getFilename());
Daniel Dunbara428df82009-03-24 00:24:37 +00002801
Daniel Dunbarff7488d2009-03-20 00:52:38 +00002802 for (InputInfoList::const_iterator
2803 it = Inputs.begin(), ie = Inputs.end(); it != ie; ++it) {
2804 const InputInfo &II = *it;
2805 assert(II.isFilename() && "Unexpected lipo input.");
2806 CmdArgs.push_back(II.getFilename());
2807 }
Daniel Dunbarc21c4852009-04-08 23:54:23 +00002808 const char *Exec =
Daniel Dunbar4a7e8892010-07-14 18:46:23 +00002809 Args.MakeArgString(getToolChain().GetProgramPath("lipo"));
Daniel Dunbar2fe238e2010-08-02 02:38:28 +00002810 C.addCommand(new Command(JA, *this, Exec, CmdArgs));
Daniel Dunbarff7488d2009-03-20 00:52:38 +00002811}
Daniel Dunbar68a31d42009-03-31 17:45:15 +00002812
Daniel Dunbar6e0f2542010-06-04 18:28:36 +00002813void darwin::Dsymutil::ConstructJob(Compilation &C, const JobAction &JA,
Daniel Dunbar2fe238e2010-08-02 02:38:28 +00002814 const InputInfo &Output,
Daniel Dunbar6e0f2542010-06-04 18:28:36 +00002815 const InputInfoList &Inputs,
2816 const ArgList &Args,
2817 const char *LinkingOutput) const {
2818 ArgStringList CmdArgs;
2819
2820 assert(Inputs.size() == 1 && "Unable to handle multiple inputs.");
2821 const InputInfo &Input = Inputs[0];
2822 assert(Input.isFilename() && "Unexpected dsymutil input.");
2823 CmdArgs.push_back(Input.getFilename());
2824
2825 CmdArgs.push_back("-o");
2826 CmdArgs.push_back(Output.getFilename());
2827
2828 const char *Exec =
Daniel Dunbar4a7e8892010-07-14 18:46:23 +00002829 Args.MakeArgString(getToolChain().GetProgramPath("dsymutil"));
Daniel Dunbar2fe238e2010-08-02 02:38:28 +00002830 C.addCommand(new Command(JA, *this, Exec, CmdArgs));
Daniel Dunbar6e0f2542010-06-04 18:28:36 +00002831}
2832
Edward O'Callaghane7925a02009-08-22 01:06:46 +00002833void auroraux::Assemble::ConstructJob(Compilation &C, const JobAction &JA,
Daniel Dunbar2fe238e2010-08-02 02:38:28 +00002834 const InputInfo &Output,
Daniel Dunbar294691e2009-11-04 06:24:38 +00002835 const InputInfoList &Inputs,
2836 const ArgList &Args,
2837 const char *LinkingOutput) const {
Edward O'Callaghane7925a02009-08-22 01:06:46 +00002838 ArgStringList CmdArgs;
2839
2840 Args.AddAllArgValues(CmdArgs, options::OPT_Wa_COMMA,
2841 options::OPT_Xassembler);
2842
2843 CmdArgs.push_back("-o");
Daniel Dunbar7c1e4652010-08-02 02:38:21 +00002844 CmdArgs.push_back(Output.getFilename());
Edward O'Callaghane7925a02009-08-22 01:06:46 +00002845
2846 for (InputInfoList::const_iterator
2847 it = Inputs.begin(), ie = Inputs.end(); it != ie; ++it) {
2848 const InputInfo &II = *it;
Daniel Dunbar7c1e4652010-08-02 02:38:21 +00002849 CmdArgs.push_back(II.getFilename());
Edward O'Callaghane7925a02009-08-22 01:06:46 +00002850 }
2851
2852 const char *Exec =
Daniel Dunbar4a7e8892010-07-14 18:46:23 +00002853 Args.MakeArgString(getToolChain().GetProgramPath("gas"));
Daniel Dunbar2fe238e2010-08-02 02:38:28 +00002854 C.addCommand(new Command(JA, *this, Exec, CmdArgs));
Edward O'Callaghane7925a02009-08-22 01:06:46 +00002855}
2856
2857void auroraux::Link::ConstructJob(Compilation &C, const JobAction &JA,
Daniel Dunbar2fe238e2010-08-02 02:38:28 +00002858 const InputInfo &Output,
Daniel Dunbar294691e2009-11-04 06:24:38 +00002859 const InputInfoList &Inputs,
2860 const ArgList &Args,
2861 const char *LinkingOutput) const {
Edward O'Callaghane7925a02009-08-22 01:06:46 +00002862 ArgStringList CmdArgs;
2863
2864 if ((!Args.hasArg(options::OPT_nostdlib)) &&
Daniel Dunbar294691e2009-11-04 06:24:38 +00002865 (!Args.hasArg(options::OPT_shared))) {
Edward O'Callaghane7925a02009-08-22 01:06:46 +00002866 CmdArgs.push_back("-e");
Edward O'Callaghan7adf9492009-10-15 07:44:07 +00002867 CmdArgs.push_back("_start");
Edward O'Callaghane7925a02009-08-22 01:06:46 +00002868 }
2869
2870 if (Args.hasArg(options::OPT_static)) {
2871 CmdArgs.push_back("-Bstatic");
Edward O'Callaghan7adf9492009-10-15 07:44:07 +00002872 CmdArgs.push_back("-dn");
Edward O'Callaghane7925a02009-08-22 01:06:46 +00002873 } else {
Edward O'Callaghan7adf9492009-10-15 07:44:07 +00002874// CmdArgs.push_back("--eh-frame-hdr");
Edward O'Callaghane7925a02009-08-22 01:06:46 +00002875 CmdArgs.push_back("-Bdynamic");
2876 if (Args.hasArg(options::OPT_shared)) {
2877 CmdArgs.push_back("-shared");
2878 } else {
Edward O'Callaghan3cecc192009-10-16 19:44:18 +00002879 CmdArgs.push_back("--dynamic-linker");
Edward O'Callaghane7925a02009-08-22 01:06:46 +00002880 CmdArgs.push_back("/lib/ld.so.1"); // 64Bit Path /lib/amd64/ld.so.1
2881 }
2882 }
2883
Daniel Dunbar7c1e4652010-08-02 02:38:21 +00002884 if (Output.isFilename()) {
Edward O'Callaghane7925a02009-08-22 01:06:46 +00002885 CmdArgs.push_back("-o");
2886 CmdArgs.push_back(Output.getFilename());
2887 } else {
2888 assert(Output.isNothing() && "Invalid output.");
2889 }
2890
2891 if (!Args.hasArg(options::OPT_nostdlib) &&
2892 !Args.hasArg(options::OPT_nostartfiles)) {
2893 if (!Args.hasArg(options::OPT_shared)) {
Chris Lattner38e317d2010-07-07 16:01:42 +00002894 CmdArgs.push_back(Args.MakeArgString(
Daniel Dunbar4a7e8892010-07-14 18:46:23 +00002895 getToolChain().GetFilePath("crt1.o")));
Chris Lattner38e317d2010-07-07 16:01:42 +00002896 CmdArgs.push_back(Args.MakeArgString(
Daniel Dunbar4a7e8892010-07-14 18:46:23 +00002897 getToolChain().GetFilePath("crti.o")));
Chris Lattner38e317d2010-07-07 16:01:42 +00002898 CmdArgs.push_back(Args.MakeArgString(
Daniel Dunbar4a7e8892010-07-14 18:46:23 +00002899 getToolChain().GetFilePath("crtbegin.o")));
Edward O'Callaghane7925a02009-08-22 01:06:46 +00002900 } else {
Chris Lattner38e317d2010-07-07 16:01:42 +00002901 CmdArgs.push_back(Args.MakeArgString(
Daniel Dunbar4a7e8892010-07-14 18:46:23 +00002902 getToolChain().GetFilePath("crti.o")));
Edward O'Callaghane7925a02009-08-22 01:06:46 +00002903 }
Chris Lattner38e317d2010-07-07 16:01:42 +00002904 CmdArgs.push_back(Args.MakeArgString(
Daniel Dunbar4a7e8892010-07-14 18:46:23 +00002905 getToolChain().GetFilePath("crtn.o")));
Edward O'Callaghane7925a02009-08-22 01:06:46 +00002906 }
2907
Daniel Dunbar294691e2009-11-04 06:24:38 +00002908 CmdArgs.push_back(Args.MakeArgString("-L/opt/gcc4/lib/gcc/"
2909 + getToolChain().getTripleString()
Daniel Dunbarf7fb31f2009-10-29 02:24:37 +00002910 + "/4.2.4"));
Edward O'Callaghane7925a02009-08-22 01:06:46 +00002911
2912 Args.AddAllArgs(CmdArgs, options::OPT_L);
2913 Args.AddAllArgs(CmdArgs, options::OPT_T_Group);
2914 Args.AddAllArgs(CmdArgs, options::OPT_e);
2915
Daniel Dunbar2008fee2010-09-17 00:24:54 +00002916 AddLinkerInputs(getToolChain(), Inputs, Args, CmdArgs);
Edward O'Callaghane7925a02009-08-22 01:06:46 +00002917
2918 if (!Args.hasArg(options::OPT_nostdlib) &&
2919 !Args.hasArg(options::OPT_nodefaultlibs)) {
2920 // FIXME: For some reason GCC passes -lgcc before adding
2921 // the default system libraries. Just mimic this for now.
2922 CmdArgs.push_back("-lgcc");
2923
2924 if (Args.hasArg(options::OPT_pthread))
2925 CmdArgs.push_back("-pthread");
2926 if (!Args.hasArg(options::OPT_shared))
2927 CmdArgs.push_back("-lc");
2928 CmdArgs.push_back("-lgcc");
2929 }
2930
2931 if (!Args.hasArg(options::OPT_nostdlib) &&
2932 !Args.hasArg(options::OPT_nostartfiles)) {
2933 if (!Args.hasArg(options::OPT_shared))
Chris Lattner38e317d2010-07-07 16:01:42 +00002934 CmdArgs.push_back(Args.MakeArgString(
Daniel Dunbar4a7e8892010-07-14 18:46:23 +00002935 getToolChain().GetFilePath("crtend.o")));
Edward O'Callaghane7925a02009-08-22 01:06:46 +00002936 }
2937
2938 const char *Exec =
Daniel Dunbar4a7e8892010-07-14 18:46:23 +00002939 Args.MakeArgString(getToolChain().GetProgramPath("ld"));
Daniel Dunbar2fe238e2010-08-02 02:38:28 +00002940 C.addCommand(new Command(JA, *this, Exec, CmdArgs));
Edward O'Callaghane7925a02009-08-22 01:06:46 +00002941}
2942
Daniel Dunbarf7b8eec2009-06-29 20:52:51 +00002943void openbsd::Assemble::ConstructJob(Compilation &C, const JobAction &JA,
Daniel Dunbar2fe238e2010-08-02 02:38:28 +00002944 const InputInfo &Output,
Daniel Dunbarf7b8eec2009-06-29 20:52:51 +00002945 const InputInfoList &Inputs,
2946 const ArgList &Args,
Mike Stump1eb44332009-09-09 15:08:12 +00002947 const char *LinkingOutput) const {
Daniel Dunbarf7b8eec2009-06-29 20:52:51 +00002948 ArgStringList CmdArgs;
2949
2950 Args.AddAllArgValues(CmdArgs, options::OPT_Wa_COMMA,
2951 options::OPT_Xassembler);
2952
2953 CmdArgs.push_back("-o");
Daniel Dunbar7c1e4652010-08-02 02:38:21 +00002954 CmdArgs.push_back(Output.getFilename());
Daniel Dunbarf7b8eec2009-06-29 20:52:51 +00002955
2956 for (InputInfoList::const_iterator
2957 it = Inputs.begin(), ie = Inputs.end(); it != ie; ++it) {
2958 const InputInfo &II = *it;
Daniel Dunbar7c1e4652010-08-02 02:38:21 +00002959 CmdArgs.push_back(II.getFilename());
Daniel Dunbarf7b8eec2009-06-29 20:52:51 +00002960 }
2961
2962 const char *Exec =
Daniel Dunbar4a7e8892010-07-14 18:46:23 +00002963 Args.MakeArgString(getToolChain().GetProgramPath("as"));
Daniel Dunbar2fe238e2010-08-02 02:38:28 +00002964 C.addCommand(new Command(JA, *this, Exec, CmdArgs));
Daniel Dunbarf7b8eec2009-06-29 20:52:51 +00002965}
2966
2967void openbsd::Link::ConstructJob(Compilation &C, const JobAction &JA,
Daniel Dunbar2fe238e2010-08-02 02:38:28 +00002968 const InputInfo &Output,
Daniel Dunbarf7b8eec2009-06-29 20:52:51 +00002969 const InputInfoList &Inputs,
2970 const ArgList &Args,
2971 const char *LinkingOutput) const {
Daniel Dunbaree788e72009-12-21 18:54:17 +00002972 const Driver &D = getToolChain().getDriver();
Daniel Dunbarf7b8eec2009-06-29 20:52:51 +00002973 ArgStringList CmdArgs;
2974
Daniel Dunbar2bbcf662009-08-03 01:28:59 +00002975 if ((!Args.hasArg(options::OPT_nostdlib)) &&
Daniel Dunbar294691e2009-11-04 06:24:38 +00002976 (!Args.hasArg(options::OPT_shared))) {
Daniel Dunbar2bbcf662009-08-03 01:28:59 +00002977 CmdArgs.push_back("-e");
2978 CmdArgs.push_back("__start");
2979 }
2980
Daniel Dunbarf7b8eec2009-06-29 20:52:51 +00002981 if (Args.hasArg(options::OPT_static)) {
2982 CmdArgs.push_back("-Bstatic");
2983 } else {
Rafael Espindola65ba55d2010-11-11 02:17:51 +00002984 if (Args.hasArg(options::OPT_rdynamic))
2985 CmdArgs.push_back("-export-dynamic");
Daniel Dunbarf7b8eec2009-06-29 20:52:51 +00002986 CmdArgs.push_back("--eh-frame-hdr");
Daniel Dunbar2bbcf662009-08-03 01:28:59 +00002987 CmdArgs.push_back("-Bdynamic");
Daniel Dunbarf7b8eec2009-06-29 20:52:51 +00002988 if (Args.hasArg(options::OPT_shared)) {
Daniel Dunbar2bbcf662009-08-03 01:28:59 +00002989 CmdArgs.push_back("-shared");
Daniel Dunbarf7b8eec2009-06-29 20:52:51 +00002990 } else {
2991 CmdArgs.push_back("-dynamic-linker");
2992 CmdArgs.push_back("/usr/libexec/ld.so");
2993 }
2994 }
2995
Daniel Dunbar7c1e4652010-08-02 02:38:21 +00002996 if (Output.isFilename()) {
Daniel Dunbarf7b8eec2009-06-29 20:52:51 +00002997 CmdArgs.push_back("-o");
2998 CmdArgs.push_back(Output.getFilename());
2999 } else {
3000 assert(Output.isNothing() && "Invalid output.");
3001 }
3002
3003 if (!Args.hasArg(options::OPT_nostdlib) &&
3004 !Args.hasArg(options::OPT_nostartfiles)) {
3005 if (!Args.hasArg(options::OPT_shared)) {
Chris Lattner38e317d2010-07-07 16:01:42 +00003006 CmdArgs.push_back(Args.MakeArgString(
Daniel Dunbar4a7e8892010-07-14 18:46:23 +00003007 getToolChain().GetFilePath("crt0.o")));
Chris Lattner38e317d2010-07-07 16:01:42 +00003008 CmdArgs.push_back(Args.MakeArgString(
Daniel Dunbar4a7e8892010-07-14 18:46:23 +00003009 getToolChain().GetFilePath("crtbegin.o")));
Daniel Dunbarf7b8eec2009-06-29 20:52:51 +00003010 } else {
Chris Lattner38e317d2010-07-07 16:01:42 +00003011 CmdArgs.push_back(Args.MakeArgString(
Daniel Dunbar4a7e8892010-07-14 18:46:23 +00003012 getToolChain().GetFilePath("crtbeginS.o")));
Daniel Dunbarf7b8eec2009-06-29 20:52:51 +00003013 }
3014 }
3015
Edward O'Callaghane7e18202009-10-28 15:13:08 +00003016 std::string Triple = getToolChain().getTripleString();
3017 if (Triple.substr(0, 6) == "x86_64")
Daniel Dunbar294691e2009-11-04 06:24:38 +00003018 Triple.replace(0, 6, "amd64");
Daniel Dunbarf7fb31f2009-10-29 02:24:37 +00003019 CmdArgs.push_back(Args.MakeArgString("-L/usr/lib/gcc-lib/" + Triple +
Daniel Dunbar95c04572010-08-01 23:13:54 +00003020 "/4.2.1"));
Daniel Dunbar2bbcf662009-08-03 01:28:59 +00003021
Daniel Dunbarf7b8eec2009-06-29 20:52:51 +00003022 Args.AddAllArgs(CmdArgs, options::OPT_L);
3023 Args.AddAllArgs(CmdArgs, options::OPT_T_Group);
3024 Args.AddAllArgs(CmdArgs, options::OPT_e);
3025
Daniel Dunbar2008fee2010-09-17 00:24:54 +00003026 AddLinkerInputs(getToolChain(), Inputs, Args, CmdArgs);
Daniel Dunbarf7b8eec2009-06-29 20:52:51 +00003027
3028 if (!Args.hasArg(options::OPT_nostdlib) &&
3029 !Args.hasArg(options::OPT_nodefaultlibs)) {
Daniel Dunbar95c04572010-08-01 23:13:54 +00003030 if (D.CCCIsCXX) {
Daniel Dunbar132e35d2010-09-17 01:20:05 +00003031 getToolChain().AddCXXStdlibLibArgs(Args, CmdArgs);
Daniel Dunbar95c04572010-08-01 23:13:54 +00003032 CmdArgs.push_back("-lm");
3033 }
3034
Daniel Dunbar2bbcf662009-08-03 01:28:59 +00003035 // FIXME: For some reason GCC passes -lgcc before adding
3036 // the default system libraries. Just mimic this for now.
3037 CmdArgs.push_back("-lgcc");
Daniel Dunbarf7b8eec2009-06-29 20:52:51 +00003038
3039 if (Args.hasArg(options::OPT_pthread))
3040 CmdArgs.push_back("-pthread");
Daniel Dunbar2bbcf662009-08-03 01:28:59 +00003041 if (!Args.hasArg(options::OPT_shared))
3042 CmdArgs.push_back("-lc");
3043 CmdArgs.push_back("-lgcc");
Daniel Dunbarf7b8eec2009-06-29 20:52:51 +00003044 }
3045
3046 if (!Args.hasArg(options::OPT_nostdlib) &&
3047 !Args.hasArg(options::OPT_nostartfiles)) {
3048 if (!Args.hasArg(options::OPT_shared))
Chris Lattner38e317d2010-07-07 16:01:42 +00003049 CmdArgs.push_back(Args.MakeArgString(
Daniel Dunbar4a7e8892010-07-14 18:46:23 +00003050 getToolChain().GetFilePath("crtend.o")));
Daniel Dunbarf7b8eec2009-06-29 20:52:51 +00003051 else
Chris Lattner38e317d2010-07-07 16:01:42 +00003052 CmdArgs.push_back(Args.MakeArgString(
Daniel Dunbar4a7e8892010-07-14 18:46:23 +00003053 getToolChain().GetFilePath("crtendS.o")));
Daniel Dunbarf7b8eec2009-06-29 20:52:51 +00003054 }
3055
3056 const char *Exec =
Daniel Dunbar4a7e8892010-07-14 18:46:23 +00003057 Args.MakeArgString(getToolChain().GetProgramPath("ld"));
Daniel Dunbar2fe238e2010-08-02 02:38:28 +00003058 C.addCommand(new Command(JA, *this, Exec, CmdArgs));
Daniel Dunbarf7b8eec2009-06-29 20:52:51 +00003059}
Ed Schoutenc66a5a32009-04-02 19:13:12 +00003060
Daniel Dunbar68a31d42009-03-31 17:45:15 +00003061void freebsd::Assemble::ConstructJob(Compilation &C, const JobAction &JA,
Daniel Dunbar2fe238e2010-08-02 02:38:28 +00003062 const InputInfo &Output,
Daniel Dunbarc21c4852009-04-08 23:54:23 +00003063 const InputInfoList &Inputs,
3064 const ArgList &Args,
Mike Stump1eb44332009-09-09 15:08:12 +00003065 const char *LinkingOutput) const {
Daniel Dunbar68a31d42009-03-31 17:45:15 +00003066 ArgStringList CmdArgs;
3067
Daniel Dunbar008f54a2009-04-01 19:36:32 +00003068 // When building 32-bit code on FreeBSD/amd64, we have to explicitly
3069 // instruct as in the base system to assemble 32-bit code.
3070 if (getToolChain().getArchName() == "i386")
Daniel Dunbar68a31d42009-03-31 17:45:15 +00003071 CmdArgs.push_back("--32");
3072
Michael J. Spencer20249a12010-10-21 03:16:25 +00003073
Eric Christophered734732010-03-02 02:41:08 +00003074 // Set byte order explicitly
3075 if (getToolChain().getArchName() == "mips")
3076 CmdArgs.push_back("-EB");
3077 else if (getToolChain().getArchName() == "mipsel")
3078 CmdArgs.push_back("-EL");
3079
Daniel Dunbar68a31d42009-03-31 17:45:15 +00003080 Args.AddAllArgValues(CmdArgs, options::OPT_Wa_COMMA,
3081 options::OPT_Xassembler);
3082
3083 CmdArgs.push_back("-o");
Daniel Dunbar7c1e4652010-08-02 02:38:21 +00003084 CmdArgs.push_back(Output.getFilename());
Daniel Dunbar68a31d42009-03-31 17:45:15 +00003085
3086 for (InputInfoList::const_iterator
3087 it = Inputs.begin(), ie = Inputs.end(); it != ie; ++it) {
3088 const InputInfo &II = *it;
Daniel Dunbar7c1e4652010-08-02 02:38:21 +00003089 CmdArgs.push_back(II.getFilename());
Daniel Dunbar68a31d42009-03-31 17:45:15 +00003090 }
3091
Daniel Dunbarc21c4852009-04-08 23:54:23 +00003092 const char *Exec =
Daniel Dunbar4a7e8892010-07-14 18:46:23 +00003093 Args.MakeArgString(getToolChain().GetProgramPath("as"));
Daniel Dunbar2fe238e2010-08-02 02:38:28 +00003094 C.addCommand(new Command(JA, *this, Exec, CmdArgs));
Daniel Dunbar68a31d42009-03-31 17:45:15 +00003095}
Daniel Dunbar008f54a2009-04-01 19:36:32 +00003096
3097void freebsd::Link::ConstructJob(Compilation &C, const JobAction &JA,
Daniel Dunbar2fe238e2010-08-02 02:38:28 +00003098 const InputInfo &Output,
Daniel Dunbarc21c4852009-04-08 23:54:23 +00003099 const InputInfoList &Inputs,
3100 const ArgList &Args,
Daniel Dunbara8304f62009-05-02 20:14:53 +00003101 const char *LinkingOutput) const {
Daniel Dunbaree788e72009-12-21 18:54:17 +00003102 const Driver &D = getToolChain().getDriver();
Daniel Dunbar008f54a2009-04-01 19:36:32 +00003103 ArgStringList CmdArgs;
3104
3105 if (Args.hasArg(options::OPT_static)) {
3106 CmdArgs.push_back("-Bstatic");
3107 } else {
Rafael Espindola65ba55d2010-11-11 02:17:51 +00003108 if (Args.hasArg(options::OPT_rdynamic))
3109 CmdArgs.push_back("-export-dynamic");
Daniel Dunbar008f54a2009-04-01 19:36:32 +00003110 CmdArgs.push_back("--eh-frame-hdr");
3111 if (Args.hasArg(options::OPT_shared)) {
3112 CmdArgs.push_back("-Bshareable");
3113 } else {
3114 CmdArgs.push_back("-dynamic-linker");
3115 CmdArgs.push_back("/libexec/ld-elf.so.1");
3116 }
3117 }
3118
3119 // When building 32-bit code on FreeBSD/amd64, we have to explicitly
3120 // instruct ld in the base system to link 32-bit code.
3121 if (getToolChain().getArchName() == "i386") {
3122 CmdArgs.push_back("-m");
3123 CmdArgs.push_back("elf_i386_fbsd");
3124 }
3125
Daniel Dunbar7c1e4652010-08-02 02:38:21 +00003126 if (Output.isFilename()) {
Daniel Dunbar008f54a2009-04-01 19:36:32 +00003127 CmdArgs.push_back("-o");
3128 CmdArgs.push_back(Output.getFilename());
3129 } else {
3130 assert(Output.isNothing() && "Invalid output.");
3131 }
3132
3133 if (!Args.hasArg(options::OPT_nostdlib) &&
3134 !Args.hasArg(options::OPT_nostartfiles)) {
3135 if (!Args.hasArg(options::OPT_shared)) {
Chris Lattner38e317d2010-07-07 16:01:42 +00003136 CmdArgs.push_back(Args.MakeArgString(
Daniel Dunbar4a7e8892010-07-14 18:46:23 +00003137 getToolChain().GetFilePath("crt1.o")));
Chris Lattner38e317d2010-07-07 16:01:42 +00003138 CmdArgs.push_back(Args.MakeArgString(
Daniel Dunbar4a7e8892010-07-14 18:46:23 +00003139 getToolChain().GetFilePath("crti.o")));
Chris Lattner38e317d2010-07-07 16:01:42 +00003140 CmdArgs.push_back(Args.MakeArgString(
Daniel Dunbar4a7e8892010-07-14 18:46:23 +00003141 getToolChain().GetFilePath("crtbegin.o")));
Daniel Dunbar008f54a2009-04-01 19:36:32 +00003142 } else {
Chris Lattner38e317d2010-07-07 16:01:42 +00003143 CmdArgs.push_back(Args.MakeArgString(
Daniel Dunbar4a7e8892010-07-14 18:46:23 +00003144 getToolChain().GetFilePath("crti.o")));
Chris Lattner38e317d2010-07-07 16:01:42 +00003145 CmdArgs.push_back(Args.MakeArgString(
Daniel Dunbar4a7e8892010-07-14 18:46:23 +00003146 getToolChain().GetFilePath("crtbeginS.o")));
Daniel Dunbar008f54a2009-04-01 19:36:32 +00003147 }
3148 }
3149
3150 Args.AddAllArgs(CmdArgs, options::OPT_L);
3151 Args.AddAllArgs(CmdArgs, options::OPT_T_Group);
3152 Args.AddAllArgs(CmdArgs, options::OPT_e);
David Chisnallc7363772010-08-15 22:58:12 +00003153 Args.AddAllArgs(CmdArgs, options::OPT_s);
3154 Args.AddAllArgs(CmdArgs, options::OPT_t);
3155 Args.AddAllArgs(CmdArgs, options::OPT_Z_Flag);
3156 Args.AddAllArgs(CmdArgs, options::OPT_r);
Daniel Dunbar008f54a2009-04-01 19:36:32 +00003157
Daniel Dunbar2008fee2010-09-17 00:24:54 +00003158 AddLinkerInputs(getToolChain(), Inputs, Args, CmdArgs);
Daniel Dunbar008f54a2009-04-01 19:36:32 +00003159
3160 if (!Args.hasArg(options::OPT_nostdlib) &&
3161 !Args.hasArg(options::OPT_nodefaultlibs)) {
Daniel Dunbar20022632010-02-17 08:07:51 +00003162 if (D.CCCIsCXX) {
Daniel Dunbar132e35d2010-09-17 01:20:05 +00003163 getToolChain().AddCXXStdlibLibArgs(Args, CmdArgs);
Daniel Dunbar20022632010-02-17 08:07:51 +00003164 CmdArgs.push_back("-lm");
3165 }
Daniel Dunbar008f54a2009-04-01 19:36:32 +00003166 // FIXME: For some reason GCC passes -lgcc and -lgcc_s before adding
3167 // the default system libraries. Just mimic this for now.
3168 CmdArgs.push_back("-lgcc");
3169 if (Args.hasArg(options::OPT_static)) {
3170 CmdArgs.push_back("-lgcc_eh");
3171 } else {
3172 CmdArgs.push_back("--as-needed");
3173 CmdArgs.push_back("-lgcc_s");
3174 CmdArgs.push_back("--no-as-needed");
3175 }
3176
3177 if (Args.hasArg(options::OPT_pthread))
3178 CmdArgs.push_back("-lpthread");
3179 CmdArgs.push_back("-lc");
3180
3181 CmdArgs.push_back("-lgcc");
3182 if (Args.hasArg(options::OPT_static)) {
3183 CmdArgs.push_back("-lgcc_eh");
3184 } else {
3185 CmdArgs.push_back("--as-needed");
3186 CmdArgs.push_back("-lgcc_s");
3187 CmdArgs.push_back("--no-as-needed");
3188 }
3189 }
3190
3191 if (!Args.hasArg(options::OPT_nostdlib) &&
3192 !Args.hasArg(options::OPT_nostartfiles)) {
3193 if (!Args.hasArg(options::OPT_shared))
Daniel Dunbar4a7e8892010-07-14 18:46:23 +00003194 CmdArgs.push_back(Args.MakeArgString(getToolChain().GetFilePath(
Chris Lattner38e317d2010-07-07 16:01:42 +00003195 "crtend.o")));
Daniel Dunbar008f54a2009-04-01 19:36:32 +00003196 else
Daniel Dunbar4a7e8892010-07-14 18:46:23 +00003197 CmdArgs.push_back(Args.MakeArgString(getToolChain().GetFilePath(
Chris Lattner38e317d2010-07-07 16:01:42 +00003198 "crtendS.o")));
Daniel Dunbar4a7e8892010-07-14 18:46:23 +00003199 CmdArgs.push_back(Args.MakeArgString(getToolChain().GetFilePath(
Chris Lattner38e317d2010-07-07 16:01:42 +00003200 "crtn.o")));
Daniel Dunbar008f54a2009-04-01 19:36:32 +00003201 }
3202
Daniel Dunbarc21c4852009-04-08 23:54:23 +00003203 const char *Exec =
Daniel Dunbar4a7e8892010-07-14 18:46:23 +00003204 Args.MakeArgString(getToolChain().GetProgramPath("ld"));
Daniel Dunbar2fe238e2010-08-02 02:38:28 +00003205 C.addCommand(new Command(JA, *this, Exec, CmdArgs));
Daniel Dunbar008f54a2009-04-01 19:36:32 +00003206}
Daniel Dunbar11e1b402009-05-02 18:28:39 +00003207
Rafael Espindolaba30bbe2010-08-10 00:25:48 +00003208void linuxtools::Assemble::ConstructJob(Compilation &C, const JobAction &JA,
3209 const InputInfo &Output,
3210 const InputInfoList &Inputs,
3211 const ArgList &Args,
3212 const char *LinkingOutput) const {
3213 ArgStringList CmdArgs;
3214
3215 // Add --32/--64 to make sure we get the format we want.
3216 // This is incomplete
3217 if (getToolChain().getArch() == llvm::Triple::x86) {
3218 CmdArgs.push_back("--32");
3219 } else if (getToolChain().getArch() == llvm::Triple::x86_64) {
3220 CmdArgs.push_back("--64");
3221 } else if (getToolChain().getArch() == llvm::Triple::arm) {
3222 llvm::StringRef MArch = getToolChain().getArchName();
3223 if (MArch == "armv7" || MArch == "armv7a" || MArch == "armv7-a")
3224 CmdArgs.push_back("-mfpu=neon");
3225 }
3226
3227 Args.AddAllArgValues(CmdArgs, options::OPT_Wa_COMMA,
3228 options::OPT_Xassembler);
3229
3230 CmdArgs.push_back("-o");
3231 CmdArgs.push_back(Output.getFilename());
3232
3233 for (InputInfoList::const_iterator
3234 it = Inputs.begin(), ie = Inputs.end(); it != ie; ++it) {
3235 const InputInfo &II = *it;
3236 CmdArgs.push_back(II.getFilename());
3237 }
3238
3239 const char *Exec =
3240 Args.MakeArgString(getToolChain().GetProgramPath("as"));
3241 C.addCommand(new Command(JA, *this, Exec, CmdArgs));
3242}
3243
Rafael Espindolac1da9812010-11-07 20:14:31 +00003244void linuxtools::Link::ConstructJob(Compilation &C, const JobAction &JA,
3245 const InputInfo &Output,
3246 const InputInfoList &Inputs,
3247 const ArgList &Args,
3248 const char *LinkingOutput) const {
3249 const toolchains::Linux& ToolChain =
3250 static_cast<const toolchains::Linux&>(getToolChain());
3251 const Driver &D = ToolChain.getDriver();
3252 ArgStringList CmdArgs;
3253
Rafael Espindola26f14c32010-11-15 18:28:16 +00003254 // Silence warning for "clang -g foo.o -o foo"
3255 Args.ClaimAllArgs(options::OPT_g_Group);
Rafael Espindola7f6458b2010-11-17 20:37:10 +00003256 // and for "clang -g foo.o -o foo". Other warning options are already
3257 // handled somewhere else.
3258 Args.ClaimAllArgs(options::OPT_w);
Rafael Espindola26f14c32010-11-15 18:28:16 +00003259
Rafael Espindolac1da9812010-11-07 20:14:31 +00003260 if (Arg *A = Args.getLastArg(options::OPT__sysroot_EQ)) {
3261 CmdArgs.push_back("--sysroot");
3262 CmdArgs.push_back(A->getValue(Args));
3263 }
3264
Rafael Espindolafdda1712010-11-17 22:26:15 +00003265 if (Args.hasArg(options::OPT_pie))
3266 CmdArgs.push_back("-pie");
3267
Rafael Espindoladc1b76d2010-11-07 22:57:16 +00003268 if (Args.hasArg(options::OPT_rdynamic))
3269 CmdArgs.push_back("-export-dynamic");
3270
Rafael Espindolae0e6d3b2010-11-11 19:34:42 +00003271 if (Args.hasArg(options::OPT_s))
3272 CmdArgs.push_back("-s");
3273
Rafael Espindolac1da9812010-11-07 20:14:31 +00003274 for (std::vector<std::string>::const_iterator i = ToolChain.ExtraOpts.begin(),
3275 e = ToolChain.ExtraOpts.end();
3276 i != e; ++i)
3277 CmdArgs.push_back(i->c_str());
3278
3279 if (!Args.hasArg(options::OPT_static)) {
3280 CmdArgs.push_back("--eh-frame-hdr");
3281 }
3282
3283 CmdArgs.push_back("-m");
3284 if (ToolChain.getArch() == llvm::Triple::x86)
3285 CmdArgs.push_back("elf_i386");
3286 else if (ToolChain.getArch() == llvm::Triple::arm)
3287 CmdArgs.push_back("armelf_linux_eabi");
3288 else
3289 CmdArgs.push_back("elf_x86_64");
3290
3291 if (Args.hasArg(options::OPT_static)) {
3292 if (ToolChain.getArch() == llvm::Triple::arm)
3293 CmdArgs.push_back("-Bstatic");
3294 else
3295 CmdArgs.push_back("-static");
3296 } else if (Args.hasArg(options::OPT_shared)) {
3297 CmdArgs.push_back("-shared");
3298 }
3299
3300 if (ToolChain.getArch() == llvm::Triple::arm ||
3301 (!Args.hasArg(options::OPT_static) &&
3302 !Args.hasArg(options::OPT_shared))) {
3303 CmdArgs.push_back("-dynamic-linker");
3304 if (ToolChain.getArch() == llvm::Triple::x86)
3305 CmdArgs.push_back("/lib/ld-linux.so.2");
3306 else if (ToolChain.getArch() == llvm::Triple::arm)
3307 CmdArgs.push_back("/lib/ld-linux.so.3");
3308 else
3309 CmdArgs.push_back("/lib64/ld-linux-x86-64.so.2");
3310 }
3311
3312 CmdArgs.push_back("-o");
3313 CmdArgs.push_back(Output.getFilename());
3314
Rafael Espindola49c64fd2010-12-01 01:52:43 +00003315 if (!Args.hasArg(options::OPT_nostdlib) &&
3316 !Args.hasArg(options::OPT_nostartfiles)) {
Rafael Espindolafdda1712010-11-17 22:26:15 +00003317 const char *crt1 = NULL;
3318 if (!Args.hasArg(options::OPT_shared)){
3319 if (Args.hasArg(options::OPT_pie))
3320 crt1 = "Scrt1.o";
3321 else
3322 crt1 = "crt1.o";
3323 }
3324 if (crt1)
3325 CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath(crt1)));
Rafael Espindolac1da9812010-11-07 20:14:31 +00003326
Rafael Espindola89414b32010-11-12 03:00:39 +00003327 CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath("crti.o")));
Rafael Espindolac1da9812010-11-07 20:14:31 +00003328
Rafael Espindola89414b32010-11-12 03:00:39 +00003329 const char *crtbegin;
3330 if (Args.hasArg(options::OPT_static))
3331 crtbegin = "crtbeginT.o";
Rafael Espindolafdda1712010-11-17 22:26:15 +00003332 else if (Args.hasArg(options::OPT_shared) || Args.hasArg(options::OPT_pie))
Rafael Espindola89414b32010-11-12 03:00:39 +00003333 crtbegin = "crtbeginS.o";
3334 else
3335 crtbegin = "crtbegin.o";
3336 CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath(crtbegin)));
3337 }
Rafael Espindolac1da9812010-11-07 20:14:31 +00003338
3339 Args.AddAllArgs(CmdArgs, options::OPT_L);
3340
3341 const ToolChain::path_list Paths = ToolChain.getFilePaths();
3342
3343 for (ToolChain::path_list::const_iterator i = Paths.begin(),
3344 e = Paths.end();
3345 i != e; ++i) {
3346 const std::string &s = *i;
3347 CmdArgs.push_back(Args.MakeArgString(std::string("-L") + s));
3348 }
3349
3350 AddLinkerInputs(ToolChain, Inputs, Args, CmdArgs);
3351
Rafael Espindola89414b32010-11-12 03:00:39 +00003352 if (D.CCCIsCXX && !Args.hasArg(options::OPT_nostdlib)) {
Rafael Espindolac1da9812010-11-07 20:14:31 +00003353 ToolChain.AddCXXStdlibLibArgs(Args, CmdArgs);
3354 CmdArgs.push_back("-lm");
3355 }
3356
3357 if (Args.hasArg(options::OPT_static))
3358 CmdArgs.push_back("--start-group");
3359
Rafael Espindola89414b32010-11-12 03:00:39 +00003360 if (!Args.hasArg(options::OPT_nostdlib)) {
3361 if (!D.CCCIsCXX)
Rafael Espindolac1da9812010-11-07 20:14:31 +00003362 CmdArgs.push_back("-lgcc");
Rafael Espindola89414b32010-11-12 03:00:39 +00003363
3364 if (Args.hasArg(options::OPT_static)) {
3365 if (D.CCCIsCXX)
3366 CmdArgs.push_back("-lgcc");
3367 } else {
3368 if (!D.CCCIsCXX)
3369 CmdArgs.push_back("--as-needed");
3370 CmdArgs.push_back("-lgcc_s");
3371 if (!D.CCCIsCXX)
3372 CmdArgs.push_back("--no-as-needed");
3373 }
3374
3375 if (Args.hasArg(options::OPT_static))
3376 CmdArgs.push_back("-lgcc_eh");
3377 else if (!Args.hasArg(options::OPT_shared) && D.CCCIsCXX)
3378 CmdArgs.push_back("-lgcc");
3379
3380 if (Args.hasArg(options::OPT_pthread) ||
3381 Args.hasArg(options::OPT_pthreads))
3382 CmdArgs.push_back("-lpthread");
3383
3384 CmdArgs.push_back("-lc");
3385
3386 if (Args.hasArg(options::OPT_static))
3387 CmdArgs.push_back("--end-group");
3388 else {
3389 if (!D.CCCIsCXX)
3390 CmdArgs.push_back("-lgcc");
3391
3392 if (!D.CCCIsCXX)
3393 CmdArgs.push_back("--as-needed");
3394 CmdArgs.push_back("-lgcc_s");
3395 if (!D.CCCIsCXX)
3396 CmdArgs.push_back("--no-as-needed");
3397
3398 if (!Args.hasArg(options::OPT_shared) && D.CCCIsCXX)
3399 CmdArgs.push_back("-lgcc");
3400 }
3401
Rafael Espindolafdda1712010-11-17 22:26:15 +00003402
Rafael Espindola49c64fd2010-12-01 01:52:43 +00003403 if (!Args.hasArg(options::OPT_nostartfiles)) {
3404 const char *crtend;
3405 if (Args.hasArg(options::OPT_shared) || Args.hasArg(options::OPT_pie))
3406 crtend = "crtendS.o";
3407 else
3408 crtend = "crtend.o";
Rafael Espindola89414b32010-11-12 03:00:39 +00003409
Rafael Espindola49c64fd2010-12-01 01:52:43 +00003410 CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath(crtend)));
3411 CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath("crtn.o")));
3412 }
Rafael Espindolac1da9812010-11-07 20:14:31 +00003413 }
3414
Rafael Espindolac1da9812010-11-07 20:14:31 +00003415 if (Args.hasArg(options::OPT_use_gold_plugin)) {
3416 CmdArgs.push_back("-plugin");
3417 std::string Plugin = ToolChain.getDriver().Dir + "/../lib/LLVMgold.so";
3418 CmdArgs.push_back(Args.MakeArgString(Plugin));
3419 }
3420
3421 C.addCommand(new Command(JA, *this, ToolChain.Linker.c_str(), CmdArgs));
3422}
Rafael Espindolaba30bbe2010-08-10 00:25:48 +00003423
Chris Lattner38e317d2010-07-07 16:01:42 +00003424void minix::Assemble::ConstructJob(Compilation &C, const JobAction &JA,
Daniel Dunbar2fe238e2010-08-02 02:38:28 +00003425 const InputInfo &Output,
3426 const InputInfoList &Inputs,
3427 const ArgList &Args,
3428 const char *LinkingOutput) const {
Chris Lattner38e317d2010-07-07 16:01:42 +00003429 ArgStringList CmdArgs;
3430
3431 Args.AddAllArgValues(CmdArgs, options::OPT_Wa_COMMA,
3432 options::OPT_Xassembler);
3433
3434 CmdArgs.push_back("-o");
Daniel Dunbar7c1e4652010-08-02 02:38:21 +00003435 CmdArgs.push_back(Output.getFilename());
Chris Lattner38e317d2010-07-07 16:01:42 +00003436
3437 for (InputInfoList::const_iterator
3438 it = Inputs.begin(), ie = Inputs.end(); it != ie; ++it) {
3439 const InputInfo &II = *it;
Daniel Dunbar7c1e4652010-08-02 02:38:21 +00003440 CmdArgs.push_back(II.getFilename());
Chris Lattner38e317d2010-07-07 16:01:42 +00003441 }
3442
3443 const char *Exec =
Daniel Dunbar4a7e8892010-07-14 18:46:23 +00003444 Args.MakeArgString(getToolChain().GetProgramPath("gas"));
Daniel Dunbar2fe238e2010-08-02 02:38:28 +00003445 C.addCommand(new Command(JA, *this, Exec, CmdArgs));
Chris Lattner38e317d2010-07-07 16:01:42 +00003446}
3447
3448void minix::Link::ConstructJob(Compilation &C, const JobAction &JA,
Daniel Dunbar2fe238e2010-08-02 02:38:28 +00003449 const InputInfo &Output,
3450 const InputInfoList &Inputs,
3451 const ArgList &Args,
3452 const char *LinkingOutput) const {
Chris Lattner38e317d2010-07-07 16:01:42 +00003453 const Driver &D = getToolChain().getDriver();
3454 ArgStringList CmdArgs;
3455
Daniel Dunbar7c1e4652010-08-02 02:38:21 +00003456 if (Output.isFilename()) {
Chris Lattner38e317d2010-07-07 16:01:42 +00003457 CmdArgs.push_back("-o");
3458 CmdArgs.push_back(Output.getFilename());
3459 } else {
3460 assert(Output.isNothing() && "Invalid output.");
3461 }
3462
3463 if (!Args.hasArg(options::OPT_nostdlib) &&
3464 !Args.hasArg(options::OPT_nostartfiles))
Daniel Dunbar4a7e8892010-07-14 18:46:23 +00003465 CmdArgs.push_back(Args.MakeArgString(getToolChain().GetFilePath(
Chris Lattner38e317d2010-07-07 16:01:42 +00003466 "/usr/gnu/lib/crtso.o")));
3467
3468 Args.AddAllArgs(CmdArgs, options::OPT_L);
3469 Args.AddAllArgs(CmdArgs, options::OPT_T_Group);
3470 Args.AddAllArgs(CmdArgs, options::OPT_e);
3471
Daniel Dunbar2008fee2010-09-17 00:24:54 +00003472 AddLinkerInputs(getToolChain(), Inputs, Args, CmdArgs);
Chris Lattner38e317d2010-07-07 16:01:42 +00003473
3474 if (!Args.hasArg(options::OPT_nostdlib) &&
3475 !Args.hasArg(options::OPT_nodefaultlibs)) {
3476 if (D.CCCIsCXX) {
Daniel Dunbar132e35d2010-09-17 01:20:05 +00003477 getToolChain().AddCXXStdlibLibArgs(Args, CmdArgs);
Chris Lattner38e317d2010-07-07 16:01:42 +00003478 CmdArgs.push_back("-lm");
3479 }
3480
3481 if (Args.hasArg(options::OPT_pthread))
3482 CmdArgs.push_back("-lpthread");
3483 CmdArgs.push_back("-lc");
3484 CmdArgs.push_back("-lgcc");
3485 CmdArgs.push_back("-L/usr/gnu/lib");
3486 // FIXME: fill in the correct search path for the final
3487 // support libraries.
3488 CmdArgs.push_back("-L/usr/gnu/lib/gcc/i686-pc-minix/4.4.3");
3489 }
3490
3491 if (!Args.hasArg(options::OPT_nostdlib) &&
3492 !Args.hasArg(options::OPT_nostartfiles)) {
Daniel Dunbar4a7e8892010-07-14 18:46:23 +00003493 CmdArgs.push_back(Args.MakeArgString(getToolChain().GetFilePath(
Chris Lattner38e317d2010-07-07 16:01:42 +00003494 "/usr/gnu/lib/libend.a")));
3495 }
3496
3497 const char *Exec =
Daniel Dunbar4a7e8892010-07-14 18:46:23 +00003498 Args.MakeArgString(getToolChain().GetProgramPath("/usr/gnu/bin/gld"));
Daniel Dunbar2fe238e2010-08-02 02:38:28 +00003499 C.addCommand(new Command(JA, *this, Exec, CmdArgs));
Chris Lattner38e317d2010-07-07 16:01:42 +00003500}
3501
Daniel Dunbar11e1b402009-05-02 18:28:39 +00003502/// DragonFly Tools
3503
3504// For now, DragonFly Assemble does just about the same as for
3505// FreeBSD, but this may change soon.
3506void dragonfly::Assemble::ConstructJob(Compilation &C, const JobAction &JA,
Daniel Dunbar2fe238e2010-08-02 02:38:28 +00003507 const InputInfo &Output,
Daniel Dunbar294691e2009-11-04 06:24:38 +00003508 const InputInfoList &Inputs,
3509 const ArgList &Args,
3510 const char *LinkingOutput) const {
Daniel Dunbar11e1b402009-05-02 18:28:39 +00003511 ArgStringList CmdArgs;
3512
3513 // When building 32-bit code on DragonFly/pc64, we have to explicitly
3514 // instruct as in the base system to assemble 32-bit code.
3515 if (getToolChain().getArchName() == "i386")
3516 CmdArgs.push_back("--32");
3517
3518 Args.AddAllArgValues(CmdArgs, options::OPT_Wa_COMMA,
3519 options::OPT_Xassembler);
3520
3521 CmdArgs.push_back("-o");
Daniel Dunbar7c1e4652010-08-02 02:38:21 +00003522 CmdArgs.push_back(Output.getFilename());
Daniel Dunbar11e1b402009-05-02 18:28:39 +00003523
3524 for (InputInfoList::const_iterator
3525 it = Inputs.begin(), ie = Inputs.end(); it != ie; ++it) {
3526 const InputInfo &II = *it;
Daniel Dunbar7c1e4652010-08-02 02:38:21 +00003527 CmdArgs.push_back(II.getFilename());
Daniel Dunbar11e1b402009-05-02 18:28:39 +00003528 }
3529
3530 const char *Exec =
Daniel Dunbar4a7e8892010-07-14 18:46:23 +00003531 Args.MakeArgString(getToolChain().GetProgramPath("as"));
Daniel Dunbar2fe238e2010-08-02 02:38:28 +00003532 C.addCommand(new Command(JA, *this, Exec, CmdArgs));
Daniel Dunbar11e1b402009-05-02 18:28:39 +00003533}
3534
3535void dragonfly::Link::ConstructJob(Compilation &C, const JobAction &JA,
Daniel Dunbar2fe238e2010-08-02 02:38:28 +00003536 const InputInfo &Output,
3537 const InputInfoList &Inputs,
3538 const ArgList &Args,
3539 const char *LinkingOutput) const {
Daniel Dunbaree788e72009-12-21 18:54:17 +00003540 const Driver &D = getToolChain().getDriver();
Daniel Dunbar11e1b402009-05-02 18:28:39 +00003541 ArgStringList CmdArgs;
3542
3543 if (Args.hasArg(options::OPT_static)) {
3544 CmdArgs.push_back("-Bstatic");
3545 } else {
3546 if (Args.hasArg(options::OPT_shared))
3547 CmdArgs.push_back("-Bshareable");
3548 else {
3549 CmdArgs.push_back("-dynamic-linker");
3550 CmdArgs.push_back("/usr/libexec/ld-elf.so.2");
3551 }
3552 }
3553
3554 // When building 32-bit code on DragonFly/pc64, we have to explicitly
3555 // instruct ld in the base system to link 32-bit code.
3556 if (getToolChain().getArchName() == "i386") {
3557 CmdArgs.push_back("-m");
3558 CmdArgs.push_back("elf_i386");
3559 }
3560
Daniel Dunbar7c1e4652010-08-02 02:38:21 +00003561 if (Output.isFilename()) {
Daniel Dunbar11e1b402009-05-02 18:28:39 +00003562 CmdArgs.push_back("-o");
3563 CmdArgs.push_back(Output.getFilename());
3564 } else {
3565 assert(Output.isNothing() && "Invalid output.");
3566 }
3567
3568 if (!Args.hasArg(options::OPT_nostdlib) &&
3569 !Args.hasArg(options::OPT_nostartfiles)) {
3570 if (!Args.hasArg(options::OPT_shared)) {
Chris Lattner38e317d2010-07-07 16:01:42 +00003571 CmdArgs.push_back(
Daniel Dunbar4a7e8892010-07-14 18:46:23 +00003572 Args.MakeArgString(getToolChain().GetFilePath("crt1.o")));
Chris Lattner38e317d2010-07-07 16:01:42 +00003573 CmdArgs.push_back(
Daniel Dunbar4a7e8892010-07-14 18:46:23 +00003574 Args.MakeArgString(getToolChain().GetFilePath("crti.o")));
Chris Lattner38e317d2010-07-07 16:01:42 +00003575 CmdArgs.push_back(
Daniel Dunbar4a7e8892010-07-14 18:46:23 +00003576 Args.MakeArgString(getToolChain().GetFilePath("crtbegin.o")));
Daniel Dunbar11e1b402009-05-02 18:28:39 +00003577 } else {
Chris Lattner38e317d2010-07-07 16:01:42 +00003578 CmdArgs.push_back(
Daniel Dunbar4a7e8892010-07-14 18:46:23 +00003579 Args.MakeArgString(getToolChain().GetFilePath("crti.o")));
Chris Lattner38e317d2010-07-07 16:01:42 +00003580 CmdArgs.push_back(
Daniel Dunbar4a7e8892010-07-14 18:46:23 +00003581 Args.MakeArgString(getToolChain().GetFilePath("crtbeginS.o")));
Daniel Dunbar11e1b402009-05-02 18:28:39 +00003582 }
3583 }
3584
3585 Args.AddAllArgs(CmdArgs, options::OPT_L);
3586 Args.AddAllArgs(CmdArgs, options::OPT_T_Group);
3587 Args.AddAllArgs(CmdArgs, options::OPT_e);
3588
Daniel Dunbar2008fee2010-09-17 00:24:54 +00003589 AddLinkerInputs(getToolChain(), Inputs, Args, CmdArgs);
Daniel Dunbar11e1b402009-05-02 18:28:39 +00003590
3591 if (!Args.hasArg(options::OPT_nostdlib) &&
3592 !Args.hasArg(options::OPT_nodefaultlibs)) {
3593 // FIXME: GCC passes on -lgcc, -lgcc_pic and a whole lot of
3594 // rpaths
3595 CmdArgs.push_back("-L/usr/lib/gcc41");
3596
3597 if (!Args.hasArg(options::OPT_static)) {
3598 CmdArgs.push_back("-rpath");
3599 CmdArgs.push_back("/usr/lib/gcc41");
3600
3601 CmdArgs.push_back("-rpath-link");
3602 CmdArgs.push_back("/usr/lib/gcc41");
3603
3604 CmdArgs.push_back("-rpath");
3605 CmdArgs.push_back("/usr/lib");
3606
3607 CmdArgs.push_back("-rpath-link");
3608 CmdArgs.push_back("/usr/lib");
3609 }
3610
Rafael Espindola405861d2010-07-20 12:59:03 +00003611 if (D.CCCIsCXX) {
Daniel Dunbar132e35d2010-09-17 01:20:05 +00003612 getToolChain().AddCXXStdlibLibArgs(Args, CmdArgs);
Rafael Espindola405861d2010-07-20 12:59:03 +00003613 CmdArgs.push_back("-lm");
3614 }
3615
Daniel Dunbar11e1b402009-05-02 18:28:39 +00003616 if (Args.hasArg(options::OPT_shared)) {
3617 CmdArgs.push_back("-lgcc_pic");
3618 } else {
3619 CmdArgs.push_back("-lgcc");
3620 }
3621
3622
3623 if (Args.hasArg(options::OPT_pthread))
Mike Stump4d63f8b2009-10-31 20:11:46 +00003624 CmdArgs.push_back("-lpthread");
Daniel Dunbar11e1b402009-05-02 18:28:39 +00003625
3626 if (!Args.hasArg(options::OPT_nolibc)) {
3627 CmdArgs.push_back("-lc");
3628 }
3629
3630 if (Args.hasArg(options::OPT_shared)) {
3631 CmdArgs.push_back("-lgcc_pic");
3632 } else {
3633 CmdArgs.push_back("-lgcc");
3634 }
3635 }
3636
3637 if (!Args.hasArg(options::OPT_nostdlib) &&
3638 !Args.hasArg(options::OPT_nostartfiles)) {
3639 if (!Args.hasArg(options::OPT_shared))
Chris Lattner38e317d2010-07-07 16:01:42 +00003640 CmdArgs.push_back(Args.MakeArgString(
Daniel Dunbar4a7e8892010-07-14 18:46:23 +00003641 getToolChain().GetFilePath("crtend.o")));
Daniel Dunbar11e1b402009-05-02 18:28:39 +00003642 else
Chris Lattner38e317d2010-07-07 16:01:42 +00003643 CmdArgs.push_back(Args.MakeArgString(
Daniel Dunbar4a7e8892010-07-14 18:46:23 +00003644 getToolChain().GetFilePath("crtendS.o")));
Chris Lattner38e317d2010-07-07 16:01:42 +00003645 CmdArgs.push_back(Args.MakeArgString(
Daniel Dunbar4a7e8892010-07-14 18:46:23 +00003646 getToolChain().GetFilePath("crtn.o")));
Daniel Dunbar11e1b402009-05-02 18:28:39 +00003647 }
3648
3649 const char *Exec =
Daniel Dunbar4a7e8892010-07-14 18:46:23 +00003650 Args.MakeArgString(getToolChain().GetProgramPath("ld"));
Daniel Dunbar2fe238e2010-08-02 02:38:28 +00003651 C.addCommand(new Command(JA, *this, Exec, CmdArgs));
Daniel Dunbar11e1b402009-05-02 18:28:39 +00003652}
Michael J. Spencerff58e362010-08-21 21:55:07 +00003653
3654void visualstudio::Link::ConstructJob(Compilation &C, const JobAction &JA,
3655 const InputInfo &Output,
3656 const InputInfoList &Inputs,
3657 const ArgList &Args,
3658 const char *LinkingOutput) const {
Michael J. Spencerff58e362010-08-21 21:55:07 +00003659 ArgStringList CmdArgs;
3660
3661 if (Output.isFilename()) {
Daniel Dunbare5a37f42010-09-17 00:45:02 +00003662 CmdArgs.push_back(Args.MakeArgString(std::string("-out:") +
3663 Output.getFilename()));
Michael J. Spencerff58e362010-08-21 21:55:07 +00003664 } else {
3665 assert(Output.isNothing() && "Invalid output.");
3666 }
3667
3668 if (!Args.hasArg(options::OPT_nostdlib) &&
3669 !Args.hasArg(options::OPT_nostartfiles)) {
3670 CmdArgs.push_back("-defaultlib:libcmt");
3671 }
3672
3673 CmdArgs.push_back("-nologo");
3674
Daniel Dunbar2008fee2010-09-17 00:24:54 +00003675 AddLinkerInputs(getToolChain(), Inputs, Args, CmdArgs);
Michael J. Spencerff58e362010-08-21 21:55:07 +00003676
3677 const char *Exec =
Daniel Dunbar2008fee2010-09-17 00:24:54 +00003678 Args.MakeArgString(getToolChain().GetProgramPath("link.exe"));
Michael J. Spencerff58e362010-08-21 21:55:07 +00003679 C.addCommand(new Command(JA, *this, Exec, CmdArgs));
3680}