blob: 1726769ec2e9602d73d415eb0579374d8874901b [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
Daniel Dunbar8ac38d72011-02-19 05:33:51 +000094 // Add extra linker input arguments which are not treated as inputs
95 // (constructed via -Xarch_).
96 Args.AddAllArgValues(CmdArgs, options::OPT_Zlinker_input);
97
Daniel Dunbar2008fee2010-09-17 00:24:54 +000098 for (InputInfoList::const_iterator
99 it = Inputs.begin(), ie = Inputs.end(); it != ie; ++it) {
100 const InputInfo &II = *it;
101
102 if (!TC.HasNativeLLVMSupport()) {
103 // Don't try to pass LLVM inputs unless we have native support.
104 if (II.getType() == types::TY_LLVM_IR ||
105 II.getType() == types::TY_LTO_IR ||
106 II.getType() == types::TY_LLVM_BC ||
107 II.getType() == types::TY_LTO_BC)
108 D.Diag(clang::diag::err_drv_no_linker_llvm_support)
109 << TC.getTripleString();
110 }
111
Daniel Dunbare5a37f42010-09-17 00:45:02 +0000112 // Add filenames immediately.
113 if (II.isFilename()) {
Daniel Dunbar2008fee2010-09-17 00:24:54 +0000114 CmdArgs.push_back(II.getFilename());
Daniel Dunbare5a37f42010-09-17 00:45:02 +0000115 continue;
116 }
117
118 // Otherwise, this is a linker input argument.
119 const Arg &A = II.getInputArg();
120
121 // Handle reserved library options.
122 if (A.getOption().matches(options::OPT_Z_reserved_lib_stdcxx)) {
Daniel Dunbar132e35d2010-09-17 01:20:05 +0000123 TC.AddCXXStdlibLibArgs(Args, CmdArgs);
Shantonu Sen7433fed2010-09-17 18:39:08 +0000124 } else if (A.getOption().matches(options::OPT_Z_reserved_lib_cckext)) {
125 TC.AddCCKextLibArgs(Args, CmdArgs);
Daniel Dunbare5a37f42010-09-17 00:45:02 +0000126 } else
127 A.renderAsInput(Args, CmdArgs);
Daniel Dunbar2008fee2010-09-17 00:24:54 +0000128 }
129}
130
Mike Stump1eb44332009-09-09 15:08:12 +0000131void Clang::AddPreprocessingOptions(const Driver &D,
Douglas Gregordf91ef32009-04-18 00:34:01 +0000132 const ArgList &Args,
Daniel Dunbarc21c4852009-04-08 23:54:23 +0000133 ArgStringList &CmdArgs,
134 const InputInfo &Output,
135 const InputInfoList &Inputs) const {
Daniel Dunbarc21c4852009-04-08 23:54:23 +0000136 Arg *A;
Daniel Dunbar3a183d32009-06-08 21:48:20 +0000137
Daniel Dunbar88a3d6c2009-09-10 01:21:05 +0000138 CheckPreprocessingOptions(D, Args);
139
140 Args.AddLastArg(CmdArgs, options::OPT_C);
141 Args.AddLastArg(CmdArgs, options::OPT_CC);
Daniel Dunbar3a183d32009-06-08 21:48:20 +0000142
143 // Handle dependency file generation.
Daniel Dunbar9eb93b02010-12-08 21:33:40 +0000144 if ((A = Args.getLastArg(options::OPT_M, options::OPT_MM)) ||
Daniel Dunbarc21c4852009-04-08 23:54:23 +0000145 (A = Args.getLastArg(options::OPT_MD)) ||
146 (A = Args.getLastArg(options::OPT_MMD))) {
147 // Determine the output location.
148 const char *DepFile;
149 if (Output.getType() == types::TY_Dependencies) {
Daniel Dunbar7c1e4652010-08-02 02:38:21 +0000150 DepFile = Output.getFilename();
Daniel Dunbarc21c4852009-04-08 23:54:23 +0000151 } else if (Arg *MF = Args.getLastArg(options::OPT_MF)) {
152 DepFile = MF->getValue(Args);
Daniel Dunbarb827a052009-11-19 03:26:40 +0000153 } else if (A->getOption().matches(options::OPT_M) ||
154 A->getOption().matches(options::OPT_MM)) {
Daniel Dunbarc21c4852009-04-08 23:54:23 +0000155 DepFile = "-";
156 } else {
157 DepFile = darwin::CC1::getDependencyFileName(Args, Inputs);
158 }
159 CmdArgs.push_back("-dependency-file");
160 CmdArgs.push_back(DepFile);
161
Chris Lattner3edbeb72010-03-29 17:55:58 +0000162 // Add a default target if one wasn't specified.
Daniel Dunbarc21c4852009-04-08 23:54:23 +0000163 if (!Args.hasArg(options::OPT_MT) && !Args.hasArg(options::OPT_MQ)) {
164 const char *DepTarget;
165
166 // If user provided -o, that is the dependency target, except
167 // when we are only generating a dependency file.
168 Arg *OutputOpt = Args.getLastArg(options::OPT_o);
169 if (OutputOpt && Output.getType() != types::TY_Dependencies) {
170 DepTarget = OutputOpt->getValue(Args);
171 } else {
172 // Otherwise derive from the base input.
173 //
174 // FIXME: This should use the computed output file location.
Michael J. Spencer472ccff2010-12-18 00:19:12 +0000175 llvm::SmallString<128> P(Inputs[0].getBaseInput());
176 llvm::sys::path::replace_extension(P, "o");
177 DepTarget = Args.MakeArgString(llvm::sys::path::filename(P));
Daniel Dunbarc21c4852009-04-08 23:54:23 +0000178 }
179
180 CmdArgs.push_back("-MT");
Chris Lattner3edbeb72010-03-29 17:55:58 +0000181 llvm::SmallString<128> Quoted;
182 QuoteTarget(DepTarget, Quoted);
183 CmdArgs.push_back(Args.MakeArgString(Quoted));
Daniel Dunbarc21c4852009-04-08 23:54:23 +0000184 }
185
Daniel Dunbarb827a052009-11-19 03:26:40 +0000186 if (A->getOption().matches(options::OPT_M) ||
187 A->getOption().matches(options::OPT_MD))
Daniel Dunbarc21c4852009-04-08 23:54:23 +0000188 CmdArgs.push_back("-sys-header-deps");
189 }
190
191 Args.AddLastArg(CmdArgs, options::OPT_MP);
Chris Lattner3edbeb72010-03-29 17:55:58 +0000192
193 // Convert all -MQ <target> args to -MT <quoted target>
194 for (arg_iterator it = Args.filtered_begin(options::OPT_MT,
195 options::OPT_MQ),
196 ie = Args.filtered_end(); it != ie; ++it) {
Daniel Dunbar7e4953e2010-06-11 22:00:13 +0000197 const Arg *A = *it;
198 A->claim();
Chris Lattner3edbeb72010-03-29 17:55:58 +0000199
Daniel Dunbar7e4953e2010-06-11 22:00:13 +0000200 if (A->getOption().matches(options::OPT_MQ)) {
Chris Lattner3edbeb72010-03-29 17:55:58 +0000201 CmdArgs.push_back("-MT");
202 llvm::SmallString<128> Quoted;
Daniel Dunbar7e4953e2010-06-11 22:00:13 +0000203 QuoteTarget(A->getValue(Args), Quoted);
Chris Lattner3edbeb72010-03-29 17:55:58 +0000204 CmdArgs.push_back(Args.MakeArgString(Quoted));
205
206 // -MT flag - no change
207 } else {
Daniel Dunbar7e4953e2010-06-11 22:00:13 +0000208 A->render(Args, CmdArgs);
Chris Lattner3edbeb72010-03-29 17:55:58 +0000209 }
210 }
Daniel Dunbarc21c4852009-04-08 23:54:23 +0000211
Douglas Gregordf91ef32009-04-18 00:34:01 +0000212 // Add -i* options, and automatically translate to
213 // -include-pch/-include-pth for transparent PCH support. It's
214 // wonky, but we include looking for .gch so we can support seamless
215 // replacement into a build system already set up to be generating
216 // .gch files.
Argyrios Kyrtzidis990142a2010-09-30 16:53:47 +0000217 bool RenderedImplicitInclude = false;
Daniel Dunbarcdd96862009-11-25 11:53:23 +0000218 for (arg_iterator it = Args.filtered_begin(options::OPT_clang_i_Group),
219 ie = Args.filtered_end(); it != ie; ++it) {
220 const Arg *A = it;
Daniel Dunbarc21c4852009-04-08 23:54:23 +0000221
222 if (A->getOption().matches(options::OPT_include)) {
Argyrios Kyrtzidis990142a2010-09-30 16:53:47 +0000223 bool IsFirstImplicitInclude = !RenderedImplicitInclude;
224 RenderedImplicitInclude = true;
225
Argyrios Kyrtzidise5c35372010-08-11 23:27:58 +0000226 // Use PCH if the user requested it.
Daniel Dunbar0ebd9322009-10-15 20:02:44 +0000227 bool UsePCH = D.CCCUsePCH;
Daniel Dunbar0ebd9322009-10-15 20:02:44 +0000228
Daniel Dunbarc21c4852009-04-08 23:54:23 +0000229 bool FoundPTH = false;
Douglas Gregordf91ef32009-04-18 00:34:01 +0000230 bool FoundPCH = false;
Daniel Dunbarc21c4852009-04-08 23:54:23 +0000231 llvm::sys::Path P(A->getValue(Args));
Michael J. Spencer32bef4e2011-01-10 02:34:13 +0000232 bool Exists;
Daniel Dunbar0ebd9322009-10-15 20:02:44 +0000233 if (UsePCH) {
Douglas Gregordf91ef32009-04-18 00:34:01 +0000234 P.appendSuffix("pch");
Michael J. Spencer32bef4e2011-01-10 02:34:13 +0000235 if (!llvm::sys::fs::exists(P.str(), Exists) && Exists)
Douglas Gregordf91ef32009-04-18 00:34:01 +0000236 FoundPCH = true;
Mike Stump1eb44332009-09-09 15:08:12 +0000237 else
Douglas Gregordf91ef32009-04-18 00:34:01 +0000238 P.eraseSuffix();
Daniel Dunbarc21c4852009-04-08 23:54:23 +0000239 }
240
Douglas Gregordf91ef32009-04-18 00:34:01 +0000241 if (!FoundPCH) {
242 P.appendSuffix("pth");
Michael J. Spencer32bef4e2011-01-10 02:34:13 +0000243 if (!llvm::sys::fs::exists(P.str(), Exists) && Exists)
Douglas Gregordf91ef32009-04-18 00:34:01 +0000244 FoundPTH = true;
245 else
246 P.eraseSuffix();
Mike Stump1eb44332009-09-09 15:08:12 +0000247 }
248
Douglas Gregordf91ef32009-04-18 00:34:01 +0000249 if (!FoundPCH && !FoundPTH) {
250 P.appendSuffix("gch");
Michael J. Spencer32bef4e2011-01-10 02:34:13 +0000251 if (!llvm::sys::fs::exists(P.str(), Exists) && Exists) {
Daniel Dunbar0ebd9322009-10-15 20:02:44 +0000252 FoundPCH = UsePCH;
253 FoundPTH = !UsePCH;
Douglas Gregordf91ef32009-04-18 00:34:01 +0000254 }
Mike Stump1eb44332009-09-09 15:08:12 +0000255 else
Douglas Gregordf91ef32009-04-18 00:34:01 +0000256 P.eraseSuffix();
257 }
258
259 if (FoundPCH || FoundPTH) {
Argyrios Kyrtzidis990142a2010-09-30 16:53:47 +0000260 if (IsFirstImplicitInclude) {
261 A->claim();
262 if (UsePCH)
263 CmdArgs.push_back("-include-pch");
264 else
265 CmdArgs.push_back("-include-pth");
266 CmdArgs.push_back(Args.MakeArgString(P.str()));
267 continue;
268 } else {
269 // Ignore the PCH if not first on command line and emit warning.
270 D.Diag(clang::diag::warn_drv_pch_not_first_include)
271 << P.str() << A->getAsString(Args);
272 }
Daniel Dunbarc21c4852009-04-08 23:54:23 +0000273 }
274 }
275
276 // Not translated, render as usual.
277 A->claim();
278 A->render(Args, CmdArgs);
279 }
280
281 Args.AddAllArgs(CmdArgs, options::OPT_D, options::OPT_U);
282 Args.AddAllArgs(CmdArgs, options::OPT_I_Group, options::OPT_F);
283
Daniel Dunbar641b98b2010-09-14 23:12:35 +0000284 // Add C++ include arguments, if needed.
285 types::ID InputType = Inputs[0].getType();
286 if (types::isCXX(InputType))
287 getToolChain().AddClangCXXStdlibIncludeArgs(Args, CmdArgs);
288
Daniel Dunbarc21c4852009-04-08 23:54:23 +0000289 // Add -Wp, and -Xassembler if using the preprocessor.
290
291 // FIXME: There is a very unfortunate problem here, some troubled
292 // souls abuse -Wp, to pass preprocessor options in gcc syntax. To
293 // really support that we would have to parse and then translate
294 // those options. :(
295 Args.AddAllArgValues(CmdArgs, options::OPT_Wp_COMMA,
296 options::OPT_Xpreprocessor);
Daniel Dunbar607d7f62009-10-29 01:53:44 +0000297
298 // -I- is a deprecated GCC feature, reject it.
299 if (Arg *A = Args.getLastArg(options::OPT_I_))
300 D.Diag(clang::diag::err_drv_I_dash_not_supported) << A->getAsString(Args);
Chandler Carruthfeee58c2010-10-20 07:00:47 +0000301
302 // If we have a --sysroot, and don't have an explicit -isysroot flag, add an
303 // -isysroot to the CC1 invocation.
304 if (Arg *A = Args.getLastArg(options::OPT__sysroot_EQ)) {
305 if (!Args.hasArg(options::OPT_isysroot)) {
306 CmdArgs.push_back("-isysroot");
307 CmdArgs.push_back(A->getValue(Args));
308 }
309 }
Daniel Dunbarc21c4852009-04-08 23:54:23 +0000310}
311
Daniel Dunbar728a5122009-09-10 06:49:20 +0000312/// getARMTargetCPU - Get the (LLVM) name of the ARM cpu we are targetting.
313//
314// FIXME: tblgen this.
Rafael Espindolabcd6df62010-06-28 17:18:09 +0000315static const char *getARMTargetCPU(const ArgList &Args,
316 const llvm::Triple &Triple) {
Daniel Dunbar728a5122009-09-10 06:49:20 +0000317 // FIXME: Warn on inconsistent use of -mcpu and -march.
Daniel Dunbarb163ef72009-09-10 04:57:17 +0000318
319 // If we have -mcpu=, use that.
320 if (Arg *A = Args.getLastArg(options::OPT_mcpu_EQ))
321 return A->getValue(Args);
322
Rafael Espindolabcd6df62010-06-28 17:18:09 +0000323 llvm::StringRef MArch;
Daniel Dunbarb163ef72009-09-10 04:57:17 +0000324 if (Arg *A = Args.getLastArg(options::OPT_march_EQ)) {
Rafael Espindolabcd6df62010-06-28 17:18:09 +0000325 // Otherwise, if we have -march= choose the base CPU for that arch.
326 MArch = A->getValue(Args);
327 } else {
328 // Otherwise, use the Arch from the triple.
329 MArch = Triple.getArchName();
Daniel Dunbarb163ef72009-09-10 04:57:17 +0000330 }
331
Rafael Espindolabcd6df62010-06-28 17:18:09 +0000332 if (MArch == "armv2" || MArch == "armv2a")
333 return "arm2";
334 if (MArch == "armv3")
335 return "arm6";
336 if (MArch == "armv3m")
337 return "arm7m";
338 if (MArch == "armv4" || MArch == "armv4t")
339 return "arm7tdmi";
340 if (MArch == "armv5" || MArch == "armv5t")
341 return "arm10tdmi";
342 if (MArch == "armv5e" || MArch == "armv5te")
343 return "arm1026ejs";
344 if (MArch == "armv5tej")
345 return "arm926ej-s";
346 if (MArch == "armv6" || MArch == "armv6k")
347 return "arm1136jf-s";
348 if (MArch == "armv6j")
349 return "arm1136j-s";
350 if (MArch == "armv6z" || MArch == "armv6zk")
351 return "arm1176jzf-s";
352 if (MArch == "armv6t2")
353 return "arm1156t2-s";
354 if (MArch == "armv7" || MArch == "armv7a" || MArch == "armv7-a")
355 return "cortex-a8";
356 if (MArch == "armv7r" || MArch == "armv7-r")
357 return "cortex-r4";
358 if (MArch == "armv7m" || MArch == "armv7-m")
359 return "cortex-m3";
360 if (MArch == "ep9312")
361 return "ep9312";
362 if (MArch == "iwmmxt")
363 return "iwmmxt";
364 if (MArch == "xscale")
365 return "xscale";
366
367 // If all else failed, return the most base CPU LLVM supports.
Daniel Dunbarb163ef72009-09-10 04:57:17 +0000368 return "arm7tdmi";
369}
370
Daniel Dunbar1d65e4b2009-09-10 22:59:51 +0000371/// getLLVMArchSuffixForARM - Get the LLVM arch name to use for a particular
Daniel Dunbar728a5122009-09-10 06:49:20 +0000372/// CPU.
373//
374// FIXME: This is redundant with -mcpu, why does LLVM use this.
375// FIXME: tblgen this, or kill it!
Daniel Dunbar1d65e4b2009-09-10 22:59:51 +0000376static const char *getLLVMArchSuffixForARM(llvm::StringRef CPU) {
Daniel Dunbar728a5122009-09-10 06:49:20 +0000377 if (CPU == "arm7tdmi" || CPU == "arm7tdmi-s" || CPU == "arm710t" ||
378 CPU == "arm720t" || CPU == "arm9" || CPU == "arm9tdmi" ||
379 CPU == "arm920" || CPU == "arm920t" || CPU == "arm922t" ||
380 CPU == "arm940t" || CPU == "ep9312")
Daniel Dunbar1d65e4b2009-09-10 22:59:51 +0000381 return "v4t";
Daniel Dunbar728a5122009-09-10 06:49:20 +0000382
383 if (CPU == "arm10tdmi" || CPU == "arm1020t")
Daniel Dunbar1d65e4b2009-09-10 22:59:51 +0000384 return "v5";
Daniel Dunbar728a5122009-09-10 06:49:20 +0000385
386 if (CPU == "arm9e" || CPU == "arm926ej-s" || CPU == "arm946e-s" ||
387 CPU == "arm966e-s" || CPU == "arm968e-s" || CPU == "arm10e" ||
388 CPU == "arm1020e" || CPU == "arm1022e" || CPU == "xscale" ||
389 CPU == "iwmmxt")
Daniel Dunbar1d65e4b2009-09-10 22:59:51 +0000390 return "v5e";
Daniel Dunbar728a5122009-09-10 06:49:20 +0000391
392 if (CPU == "arm1136j-s" || CPU == "arm1136jf-s" || CPU == "arm1176jz-s" ||
393 CPU == "arm1176jzf-s" || CPU == "mpcorenovfp" || CPU == "mpcore")
Daniel Dunbar1d65e4b2009-09-10 22:59:51 +0000394 return "v6";
Daniel Dunbar728a5122009-09-10 06:49:20 +0000395
396 if (CPU == "arm1156t2-s" || CPU == "arm1156t2f-s")
Daniel Dunbar1d65e4b2009-09-10 22:59:51 +0000397 return "v6t2";
Daniel Dunbar728a5122009-09-10 06:49:20 +0000398
399 if (CPU == "cortex-a8" || CPU == "cortex-a9")
Daniel Dunbar1d65e4b2009-09-10 22:59:51 +0000400 return "v7";
Daniel Dunbar728a5122009-09-10 06:49:20 +0000401
Daniel Dunbar1d65e4b2009-09-10 22:59:51 +0000402 return "";
Daniel Dunbar728a5122009-09-10 06:49:20 +0000403}
404
Daniel Dunbar1f95e652009-11-17 06:37:03 +0000405// FIXME: Move to target hook.
406static bool isSignedCharDefault(const llvm::Triple &Triple) {
407 switch (Triple.getArch()) {
408 default:
409 return true;
410
411 case llvm::Triple::ppc:
412 case llvm::Triple::ppc64:
413 if (Triple.getOS() == llvm::Triple::Darwin)
414 return true;
415 return false;
416
417 case llvm::Triple::systemz:
418 return false;
419 }
420}
421
Daniel Dunbarb163ef72009-09-10 04:57:17 +0000422void Clang::AddARMTargetArgs(const ArgList &Args,
423 ArgStringList &CmdArgs) const {
Daniel Dunbaree788e72009-12-21 18:54:17 +0000424 const Driver &D = getToolChain().getDriver();
Rafael Espindolabcd6df62010-06-28 17:18:09 +0000425 llvm::Triple Triple = getToolChain().getTriple();
Daniel Dunbarcbd19332009-09-10 23:00:09 +0000426
Daniel Dunbar2030d8f2009-09-14 00:34:46 +0000427 // Select the ABI to use.
428 //
429 // FIXME: Support -meabi.
430 const char *ABIName = 0;
431 if (Arg *A = Args.getLastArg(options::OPT_mabi_EQ)) {
432 ABIName = A->getValue(Args);
433 } else {
434 // Select the default based on the platform.
Bob Wilsonfc2bd7c2011-02-04 17:59:28 +0000435 switch(Triple.getEnvironment()) {
436 case llvm::Triple::GNUEABI:
Daniel Dunbar2030d8f2009-09-14 00:34:46 +0000437 ABIName = "aapcs-linux";
Bob Wilsonfc2bd7c2011-02-04 17:59:28 +0000438 break;
439 case llvm::Triple::EABI:
Rafael Espindola1ed1a592010-06-16 19:01:17 +0000440 ABIName = "aapcs";
Bob Wilsonfc2bd7c2011-02-04 17:59:28 +0000441 break;
442 default:
Rafael Espindola1ed1a592010-06-16 19:01:17 +0000443 ABIName = "apcs-gnu";
Bob Wilsonfc2bd7c2011-02-04 17:59:28 +0000444 }
Daniel Dunbar2030d8f2009-09-14 00:34:46 +0000445 }
446 CmdArgs.push_back("-target-abi");
447 CmdArgs.push_back(ABIName);
448
Daniel Dunbarb163ef72009-09-10 04:57:17 +0000449 // Set the CPU based on -march= and -mcpu=.
Daniel Dunbar38b48af2009-12-18 06:30:12 +0000450 CmdArgs.push_back("-target-cpu");
Rafael Espindolabcd6df62010-06-28 17:18:09 +0000451 CmdArgs.push_back(getARMTargetCPU(Args, Triple));
Daniel Dunbarb163ef72009-09-10 04:57:17 +0000452
Daniel Dunbarcbd19332009-09-10 23:00:09 +0000453 // Select the float ABI as determined by -msoft-float, -mhard-float, and
454 // -mfloat-abi=.
455 llvm::StringRef FloatABI;
456 if (Arg *A = Args.getLastArg(options::OPT_msoft_float,
457 options::OPT_mhard_float,
458 options::OPT_mfloat_abi_EQ)) {
459 if (A->getOption().matches(options::OPT_msoft_float))
460 FloatABI = "soft";
461 else if (A->getOption().matches(options::OPT_mhard_float))
462 FloatABI = "hard";
463 else {
464 FloatABI = A->getValue(Args);
465 if (FloatABI != "soft" && FloatABI != "softfp" && FloatABI != "hard") {
466 D.Diag(clang::diag::err_drv_invalid_mfloat_abi)
467 << A->getAsString(Args);
468 FloatABI = "soft";
469 }
470 }
471 }
472
473 // If unspecified, choose the default based on the platform.
474 if (FloatABI.empty()) {
Rafael Espindolabcd6df62010-06-28 17:18:09 +0000475 const llvm::Triple &Triple = getToolChain().getTriple();
476 switch (Triple.getOS()) {
Daniel Dunbarcbd19332009-09-10 23:00:09 +0000477 case llvm::Triple::Darwin: {
478 // Darwin defaults to "softfp" for v6 and v7.
479 //
480 // FIXME: Factor out an ARM class so we can cache the arch somewhere.
Rafael Espindolabcd6df62010-06-28 17:18:09 +0000481 llvm::StringRef ArchName =
482 getLLVMArchSuffixForARM(getARMTargetCPU(Args, Triple));
Daniel Dunbarcbd19332009-09-10 23:00:09 +0000483 if (ArchName.startswith("v6") || ArchName.startswith("v7"))
484 FloatABI = "softfp";
485 else
486 FloatABI = "soft";
487 break;
488 }
489
Rafael Espindola7e9cebf2010-06-27 18:29:21 +0000490 case llvm::Triple::Linux: {
Bob Wilsonfc2bd7c2011-02-04 17:59:28 +0000491 if (getToolChain().getTriple().getEnvironment() == llvm::Triple::GNUEABI) {
Rafael Espindola7e9cebf2010-06-27 18:29:21 +0000492 FloatABI = "softfp";
493 break;
494 }
495 }
496 // fall through
497
Daniel Dunbarcbd19332009-09-10 23:00:09 +0000498 default:
Bob Wilsonfc2bd7c2011-02-04 17:59:28 +0000499 switch(Triple.getEnvironment()) {
500 case llvm::Triple::GNUEABI:
501 FloatABI = "softfp";
502 break;
503 case llvm::Triple::EABI:
504 // EABI is always AAPCS, and if it was not marked 'hard', it's softfp
505 FloatABI = "softfp";
506 break;
507 default:
508 // Assume "soft", but warn the user we are guessing.
509 FloatABI = "soft";
510 D.Diag(clang::diag::warn_drv_assuming_mfloat_abi_is) << "soft";
511 break;
512 }
Daniel Dunbarcbd19332009-09-10 23:00:09 +0000513 }
514 }
515
516 if (FloatABI == "soft") {
517 // Floating point operations and argument passing are soft.
518 //
519 // FIXME: This changes CPP defines, we need -target-soft-float.
Daniel Dunbar3b315262009-11-30 08:42:00 +0000520 CmdArgs.push_back("-msoft-float");
Daniel Dunbar87667aa2009-12-08 19:49:51 +0000521 CmdArgs.push_back("-mfloat-abi");
522 CmdArgs.push_back("soft");
Daniel Dunbarcbd19332009-09-10 23:00:09 +0000523 } else if (FloatABI == "softfp") {
524 // Floating point operations are hard, but argument passing is soft.
Daniel Dunbar87667aa2009-12-08 19:49:51 +0000525 CmdArgs.push_back("-mfloat-abi");
526 CmdArgs.push_back("soft");
Daniel Dunbarcbd19332009-09-10 23:00:09 +0000527 } else {
528 // Floating point operations and argument passing are hard.
529 assert(FloatABI == "hard" && "Invalid float abi!");
Daniel Dunbar87667aa2009-12-08 19:49:51 +0000530 CmdArgs.push_back("-mfloat-abi");
531 CmdArgs.push_back("hard");
Daniel Dunbarcbd19332009-09-10 23:00:09 +0000532 }
Daniel Dunbar97f52ac2009-12-19 04:15:38 +0000533
534 // Set appropriate target features for floating point mode.
535 //
536 // FIXME: Note, this is a hack, the LLVM backend doesn't actually use these
537 // yet (it uses the -mfloat-abi and -msoft-float options above), and it is
538 // stripped out by the ARM target.
539
540 // Use software floating point operations?
541 if (FloatABI == "soft") {
542 CmdArgs.push_back("-target-feature");
543 CmdArgs.push_back("+soft-float");
544 }
545
546 // Use software floating point argument passing?
547 if (FloatABI != "hard") {
548 CmdArgs.push_back("-target-feature");
549 CmdArgs.push_back("+soft-float-abi");
550 }
Daniel Dunbara91320b2009-12-21 23:28:17 +0000551
552 // Honor -mfpu=.
553 //
554 // FIXME: Centralize feature selection, defaulting shouldn't be also in the
555 // frontend target.
556 if (const Arg *A = Args.getLastArg(options::OPT_mfpu_EQ)) {
557 llvm::StringRef FPU = A->getValue(Args);
558
559 // Set the target features based on the FPU.
560 if (FPU == "fpa" || FPU == "fpe2" || FPU == "fpe3" || FPU == "maverick") {
561 // Disable any default FPU support.
562 CmdArgs.push_back("-target-feature");
563 CmdArgs.push_back("-vfp2");
564 CmdArgs.push_back("-target-feature");
565 CmdArgs.push_back("-vfp3");
566 CmdArgs.push_back("-target-feature");
567 CmdArgs.push_back("-neon");
568 } else if (FPU == "vfp") {
569 CmdArgs.push_back("-target-feature");
570 CmdArgs.push_back("+vfp2");
571 } else if (FPU == "vfp3") {
572 CmdArgs.push_back("-target-feature");
573 CmdArgs.push_back("+vfp3");
574 } else if (FPU == "neon") {
575 CmdArgs.push_back("-target-feature");
576 CmdArgs.push_back("+neon");
577 } else
578 D.Diag(clang::diag::err_drv_clang_unsupported) << A->getAsString(Args);
579 }
Daniel Dunbarb163ef72009-09-10 04:57:17 +0000580}
581
Eric Christophered734732010-03-02 02:41:08 +0000582void Clang::AddMIPSTargetArgs(const ArgList &Args,
583 ArgStringList &CmdArgs) const {
584 const Driver &D = getToolChain().getDriver();
585
586 // Select the ABI to use.
587 const char *ABIName = 0;
588 if (Arg *A = Args.getLastArg(options::OPT_mabi_EQ)) {
589 ABIName = A->getValue(Args);
590 } else {
591 ABIName = "o32";
592 }
593
594 CmdArgs.push_back("-target-abi");
595 CmdArgs.push_back(ABIName);
596
597 if (const Arg *A = Args.getLastArg(options::OPT_march_EQ)) {
598 llvm::StringRef MArch = A->getValue(Args);
599 CmdArgs.push_back("-target-cpu");
600
601 if ((MArch == "r2000") || (MArch == "r3000"))
602 CmdArgs.push_back("mips1");
603 else if (MArch == "r6000")
604 CmdArgs.push_back("mips2");
605 else
606 CmdArgs.push_back(MArch.str().c_str());
607 }
608
609 // Select the float ABI as determined by -msoft-float, -mhard-float, and
610 llvm::StringRef FloatABI;
611 if (Arg *A = Args.getLastArg(options::OPT_msoft_float,
612 options::OPT_mhard_float)) {
613 if (A->getOption().matches(options::OPT_msoft_float))
614 FloatABI = "soft";
615 else if (A->getOption().matches(options::OPT_mhard_float))
616 FloatABI = "hard";
617 }
618
619 // If unspecified, choose the default based on the platform.
620 if (FloatABI.empty()) {
Benjamin Kramer71c972a2010-04-08 15:44:22 +0000621 // Assume "soft", but warn the user we are guessing.
622 FloatABI = "soft";
623 D.Diag(clang::diag::warn_drv_assuming_mfloat_abi_is) << "soft";
Eric Christophered734732010-03-02 02:41:08 +0000624 }
625
626 if (FloatABI == "soft") {
627 // Floating point operations and argument passing are soft.
628 //
629 // FIXME: This changes CPP defines, we need -target-soft-float.
630 CmdArgs.push_back("-msoft-float");
631 } else {
632 assert(FloatABI == "hard" && "Invalid float abi!");
633 CmdArgs.push_back("-mhard-float");
634 }
635}
636
Bruno Cardoso Lopes9284d212010-11-09 17:21:19 +0000637void Clang::AddSparcTargetArgs(const ArgList &Args,
638 ArgStringList &CmdArgs) const {
639 const Driver &D = getToolChain().getDriver();
640
641 if (const Arg *A = Args.getLastArg(options::OPT_march_EQ)) {
642 llvm::StringRef MArch = A->getValue(Args);
643 CmdArgs.push_back("-target-cpu");
644 CmdArgs.push_back(MArch.str().c_str());
645 }
646
647 // Select the float ABI as determined by -msoft-float, -mhard-float, and
648 llvm::StringRef FloatABI;
649 if (Arg *A = Args.getLastArg(options::OPT_msoft_float,
650 options::OPT_mhard_float)) {
651 if (A->getOption().matches(options::OPT_msoft_float))
652 FloatABI = "soft";
653 else if (A->getOption().matches(options::OPT_mhard_float))
654 FloatABI = "hard";
655 }
656
657 // If unspecified, choose the default based on the platform.
658 if (FloatABI.empty()) {
659 switch (getToolChain().getTriple().getOS()) {
660 default:
661 // Assume "soft", but warn the user we are guessing.
662 FloatABI = "soft";
663 D.Diag(clang::diag::warn_drv_assuming_mfloat_abi_is) << "soft";
664 break;
665 }
666 }
667
668 if (FloatABI == "soft") {
669 // Floating point operations and argument passing are soft.
670 //
671 // FIXME: This changes CPP defines, we need -target-soft-float.
672 CmdArgs.push_back("-msoft-float");
673 CmdArgs.push_back("soft");
674 CmdArgs.push_back("-target-feature");
675 CmdArgs.push_back("+soft-float");
676 } else {
677 assert(FloatABI == "hard" && "Invalid float abi!");
678 CmdArgs.push_back("-mhard-float");
679 }
680}
681
Daniel Dunbar6acda162009-09-09 22:33:08 +0000682void Clang::AddX86TargetArgs(const ArgList &Args,
683 ArgStringList &CmdArgs) const {
Daniel Dunbare6ad3f92009-09-10 22:59:57 +0000684 if (!Args.hasFlag(options::OPT_mred_zone,
685 options::OPT_mno_red_zone,
686 true) ||
687 Args.hasArg(options::OPT_mkernel) ||
688 Args.hasArg(options::OPT_fapple_kext))
Daniel Dunbar66861e02009-11-20 22:21:36 +0000689 CmdArgs.push_back("-disable-red-zone");
Daniel Dunbare6ad3f92009-09-10 22:59:57 +0000690
Daniel Dunbare6ad3f92009-09-10 22:59:57 +0000691 if (Args.hasFlag(options::OPT_msoft_float,
692 options::OPT_mno_soft_float,
693 false))
Daniel Dunbar66861e02009-11-20 22:21:36 +0000694 CmdArgs.push_back("-no-implicit-float");
Daniel Dunbare6ad3f92009-09-10 22:59:57 +0000695
Daniel Dunbarf86fedd2009-11-14 22:04:54 +0000696 const char *CPUName = 0;
Daniel Dunbar6acda162009-09-09 22:33:08 +0000697 if (const Arg *A = Args.getLastArg(options::OPT_march_EQ)) {
Daniel Dunbarf86fedd2009-11-14 22:04:54 +0000698 if (llvm::StringRef(A->getValue(Args)) == "native") {
699 // FIXME: Reject attempts to use -march=native unless the target matches
700 // the host.
701 //
702 // FIXME: We should also incorporate the detected target features for use
703 // with -native.
704 std::string CPU = llvm::sys::getHostCPUName();
705 if (!CPU.empty())
706 CPUName = Args.MakeArgString(CPU);
707 } else
708 CPUName = A->getValue(Args);
709 }
Daniel Dunbar6acda162009-09-09 22:33:08 +0000710
Daniel Dunbarf86fedd2009-11-14 22:04:54 +0000711 // Select the default CPU if none was given (or detection failed).
712 if (!CPUName) {
Daniel Dunbar6acda162009-09-09 22:33:08 +0000713 // FIXME: Need target hooks.
Benjamin Kramerf22d1fd2010-01-30 15:01:47 +0000714 if (getToolChain().getOS().startswith("darwin")) {
Daniel Dunbar6acda162009-09-09 22:33:08 +0000715 if (getToolChain().getArchName() == "x86_64")
Daniel Dunbarf86fedd2009-11-14 22:04:54 +0000716 CPUName = "core2";
Daniel Dunbar6acda162009-09-09 22:33:08 +0000717 else if (getToolChain().getArchName() == "i386")
Daniel Dunbarf86fedd2009-11-14 22:04:54 +0000718 CPUName = "yonah";
Chris Lattner86ed3a32010-04-11 19:29:39 +0000719 } else if (getToolChain().getOS().startswith("haiku")) {
720 if (getToolChain().getArchName() == "x86_64")
721 CPUName = "x86-64";
722 else if (getToolChain().getArchName() == "i386")
723 CPUName = "i586";
Daniel Dunbar95c04572010-08-01 23:13:54 +0000724 } else if (getToolChain().getOS().startswith("openbsd")) {
725 if (getToolChain().getArchName() == "x86_64")
726 CPUName = "x86-64";
727 else if (getToolChain().getArchName() == "i386")
728 CPUName = "i486";
Benjamin Kramer8e50a962011-02-02 18:59:27 +0000729 } else if (getToolChain().getOS().startswith("netbsd")) {
730 if (getToolChain().getArchName() == "x86_64")
731 CPUName = "x86-64";
732 else if (getToolChain().getArchName() == "i386")
733 CPUName = "i486";
Daniel Dunbar6acda162009-09-09 22:33:08 +0000734 } else {
735 if (getToolChain().getArchName() == "x86_64")
Daniel Dunbarf86fedd2009-11-14 22:04:54 +0000736 CPUName = "x86-64";
Daniel Dunbar6acda162009-09-09 22:33:08 +0000737 else if (getToolChain().getArchName() == "i386")
Daniel Dunbarf86fedd2009-11-14 22:04:54 +0000738 CPUName = "pentium4";
Daniel Dunbar6acda162009-09-09 22:33:08 +0000739 }
740 }
741
Daniel Dunbarf86fedd2009-11-14 22:04:54 +0000742 if (CPUName) {
Daniel Dunbar38b48af2009-12-18 06:30:12 +0000743 CmdArgs.push_back("-target-cpu");
Daniel Dunbarf86fedd2009-11-14 22:04:54 +0000744 CmdArgs.push_back(CPUName);
745 }
746
Daniel Dunbarcdd96862009-11-25 11:53:23 +0000747 for (arg_iterator it = Args.filtered_begin(options::OPT_m_x86_Features_Group),
748 ie = Args.filtered_end(); it != ie; ++it) {
Daniel Dunbar7e4953e2010-06-11 22:00:13 +0000749 llvm::StringRef Name = (*it)->getOption().getName();
750 (*it)->claim();
Daniel Dunbar6acda162009-09-09 22:33:08 +0000751
Daniel Dunbarcdd96862009-11-25 11:53:23 +0000752 // Skip over "-m".
753 assert(Name.startswith("-m") && "Invalid feature name.");
754 Name = Name.substr(2);
Daniel Dunbar6acda162009-09-09 22:33:08 +0000755
Daniel Dunbarcdd96862009-11-25 11:53:23 +0000756 bool IsNegative = Name.startswith("no-");
757 if (IsNegative)
758 Name = Name.substr(3);
Daniel Dunbar6acda162009-09-09 22:33:08 +0000759
Daniel Dunbarcdd96862009-11-25 11:53:23 +0000760 CmdArgs.push_back("-target-feature");
761 CmdArgs.push_back(Args.MakeArgString((IsNegative ? "-" : "+") + Name));
Daniel Dunbar6acda162009-09-09 22:33:08 +0000762 }
763}
764
Anders Carlsson525544d2011-02-28 00:44:51 +0000765static bool
766shouldUseExceptionTablesForObjCExceptions(const ArgList &Args,
767 const llvm::Triple &Triple) {
768 // We use the zero-cost exception tables for Objective-C if the non-fragile
769 // ABI is enabled or when compiling for x86_64 and ARM on Snow Leopard and
770 // later.
771
772 if (Args.hasArg(options::OPT_fobjc_nonfragile_abi))
773 return true;
774
775 if (Triple.getOS() != llvm::Triple::Darwin)
776 return false;
777
778 return (Triple.getDarwinMajorNumber() >= 9 &&
779 (Triple.getArch() == llvm::Triple::x86_64 ||
780 Triple.getArch() == llvm::Triple::arm));
781}
782
Anders Carlsson15348ae2011-02-28 02:27:16 +0000783/// addExceptionArgs - Adds exception related arguments to the driver command
784/// arguments. There's a master flag, -fexceptions and also language specific
785/// flags to enable/disable C++ and Objective-C exceptions.
786/// This makes it possible to for example disable C++ exceptions but enable
787/// Objective-C exceptions.
788static void addExceptionArgs(const ArgList &Args, types::ID InputType,
789 const llvm::Triple &Triple,
790 bool KernelOrKext, bool IsRewriter,
791 ArgStringList &CmdArgs) {
792 if (KernelOrKext)
793 return;
794
795 // Exceptions are enabled by default.
796 bool ExceptionsEnabled = true;
797
798 // This keeps track of whether exceptions were explicitly turned on or off.
799 bool DidHaveExplicitExceptionFlag = false;
800
Rafael Espindolaf759df02009-10-01 13:33:33 +0000801 if (Arg *A = Args.getLastArg(options::OPT_fexceptions,
802 options::OPT_fno_exceptions)) {
803 if (A->getOption().matches(options::OPT_fexceptions))
Anders Carlsson15348ae2011-02-28 02:27:16 +0000804 ExceptionsEnabled = true;
805 else
806 ExceptionsEnabled = false;
807
808 DidHaveExplicitExceptionFlag = true;
Rafael Espindolaf759df02009-10-01 13:33:33 +0000809 }
Daniel Dunbar1a2cd4f2010-09-14 23:12:31 +0000810
Anders Carlsson15348ae2011-02-28 02:27:16 +0000811 bool ShouldUseExceptionTables = false;
Fariborz Jahanian85caf032009-10-01 20:30:46 +0000812
Anders Carlsson15348ae2011-02-28 02:27:16 +0000813 // Exception tables and cleanups can be enabled with -fexceptions even if the
814 // language itself doesn't support exceptions.
815 if (ExceptionsEnabled && DidHaveExplicitExceptionFlag)
816 ShouldUseExceptionTables = true;
Daniel Dunbar1a2cd4f2010-09-14 23:12:31 +0000817
Anders Carlsson15348ae2011-02-28 02:27:16 +0000818 if (types::isObjC(InputType)) {
819 bool ObjCExceptionsEnabled = ExceptionsEnabled;
820
821 if (Arg *A = Args.getLastArg(options::OPT_fobjc_exceptions,
822 options::OPT_fno_objc_exceptions,
823 options::OPT_fexceptions,
824 options::OPT_fno_exceptions)) {
825 if (A->getOption().matches(options::OPT_fobjc_exceptions))
826 ObjCExceptionsEnabled = true;
827 else if (A->getOption().matches(options::OPT_fno_objc_exceptions))
828 ObjCExceptionsEnabled = false;
829 }
830
831 if (ObjCExceptionsEnabled) {
832 CmdArgs.push_back("-fobjc-exceptions");
833
834 ShouldUseExceptionTables |=
835 shouldUseExceptionTablesForObjCExceptions(Args, Triple);
836 }
837 }
838
839 if (types::isCXX(InputType)) {
840 bool CXXExceptionsEnabled = ExceptionsEnabled;
841
842 if (Arg *A = Args.getLastArg(options::OPT_fcxx_exceptions,
843 options::OPT_fno_cxx_exceptions,
844 options::OPT_fexceptions,
845 options::OPT_fno_exceptions)) {
846 if (A->getOption().matches(options::OPT_fcxx_exceptions))
847 CXXExceptionsEnabled = true;
Chandler Carruth43f220f2011-02-28 07:25:18 +0000848 else if (A->getOption().matches(options::OPT_fno_cxx_exceptions))
Anders Carlsson15348ae2011-02-28 02:27:16 +0000849 CXXExceptionsEnabled = false;
850 }
851
852 if (CXXExceptionsEnabled) {
853 CmdArgs.push_back("-fcxx-exceptions");
854
855 ShouldUseExceptionTables = true;
856 }
857 }
858
859 if (ShouldUseExceptionTables)
860 CmdArgs.push_back("-fexceptions");
Rafael Espindolaf759df02009-10-01 13:33:33 +0000861}
862
Daniel Dunbar47ac7d22009-03-18 06:00:36 +0000863void Clang::ConstructJob(Compilation &C, const JobAction &JA,
Daniel Dunbar871adcf2009-03-18 07:06:02 +0000864 const InputInfo &Output,
Daniel Dunbar62cf6012009-03-18 06:07:59 +0000865 const InputInfoList &Inputs,
Daniel Dunbar1d460332009-03-18 10:01:51 +0000866 const ArgList &Args,
Daniel Dunbar47ac7d22009-03-18 06:00:36 +0000867 const char *LinkingOutput) const {
Daniel Dunbar0a80ba72010-03-20 04:52:14 +0000868 bool KernelOrKext = Args.hasArg(options::OPT_mkernel,
869 options::OPT_fapple_kext);
Daniel Dunbaree788e72009-12-21 18:54:17 +0000870 const Driver &D = getToolChain().getDriver();
Daniel Dunbarb488c1d2009-03-18 08:07:30 +0000871 ArgStringList CmdArgs;
872
Daniel Dunbar077ba6a2009-03-31 20:53:55 +0000873 assert(Inputs.size() == 1 && "Unable to handle multiple inputs.");
874
Daniel Dunbar8ff5b282009-12-11 23:00:49 +0000875 // Invoke ourselves in -cc1 mode.
876 //
877 // FIXME: Implement custom jobs for internal actions.
878 CmdArgs.push_back("-cc1");
879
Daniel Dunbardd4fe002009-10-30 18:12:20 +0000880 // Add the "effective" target triple.
Daniel Dunbaraf07f932009-03-31 17:35:15 +0000881 CmdArgs.push_back("-triple");
Daniel Dunbar00577ad2010-08-23 22:35:37 +0000882 std::string TripleStr = getToolChain().ComputeEffectiveClangTriple(Args);
Daniel Dunbardd4fe002009-10-30 18:12:20 +0000883 CmdArgs.push_back(Args.MakeArgString(TripleStr));
Daniel Dunbar728a5122009-09-10 06:49:20 +0000884
Daniel Dunbardd4fe002009-10-30 18:12:20 +0000885 // Select the appropriate action.
Daniel Dunbar5314e442010-07-19 19:44:22 +0000886 bool IsRewriter = false;
Daniel Dunbar1d460332009-03-18 10:01:51 +0000887 if (isa<AnalyzeJobAction>(JA)) {
888 assert(JA.getType() == types::TY_Plist && "Invalid output type.");
889 CmdArgs.push_back("-analyze");
890 } else if (isa<PreprocessJobAction>(JA)) {
Daniel Dunbarcd8e4c42009-03-30 06:36:42 +0000891 if (Output.getType() == types::TY_Dependencies)
892 CmdArgs.push_back("-Eonly");
893 else
894 CmdArgs.push_back("-E");
Daniel Dunbar8767cbc2010-02-03 03:07:56 +0000895 } else if (isa<AssembleJobAction>(JA)) {
896 CmdArgs.push_back("-emit-obj");
Daniel Dunbar99298002010-05-27 06:18:05 +0000897
898 // At -O0, we use -mrelax-all by default.
899 bool IsOpt = false;
900 if (Arg *A = Args.getLastArg(options::OPT_O_Group))
901 IsOpt = !A->getOption().matches(options::OPT_O0);
902 if (Args.hasFlag(options::OPT_mrelax_all,
903 options::OPT_mno_relax_all,
904 !IsOpt))
905 CmdArgs.push_back("-mrelax-all");
Daniel Dunbarca0e0542010-08-24 16:47:49 +0000906
Daniel Dunbarfcec10b2010-10-18 22:36:15 +0000907 // When using an integrated assembler, translate -Wa, and -Xassembler
908 // options.
909 for (arg_iterator it = Args.filtered_begin(options::OPT_Wa_COMMA,
910 options::OPT_Xassembler),
911 ie = Args.filtered_end(); it != ie; ++it) {
912 const Arg *A = *it;
913 A->claim();
914
915 for (unsigned i = 0, e = A->getNumValues(); i != e; ++i) {
916 llvm::StringRef Value = A->getValue(Args, i);
917
918 if (Value == "-force_cpusubtype_ALL") {
919 // Do nothing, this is the default and we don't support anything else.
Daniel Dunbarb14eed02010-10-28 20:36:23 +0000920 } else if (Value == "-L") {
921 // We don't support -L yet, but it isn't important enough to error
922 // on. No one should really be using it for a semantic change.
923 D.Diag(clang::diag::warn_drv_unsupported_option_argument)
924 << A->getOption().getName() << Value;
Daniel Dunbarfcec10b2010-10-18 22:36:15 +0000925 } else {
926 D.Diag(clang::diag::err_drv_unsupported_option_argument)
927 << A->getOption().getName() << Value;
928 }
929 }
930 }
Daniel Dunbard02bba82010-11-19 16:23:35 +0000931
932 // Also ignore explicit -force_cpusubtype_ALL option.
933 (void) Args.hasArg(options::OPT_force__cpusubtype__ALL);
Daniel Dunbar1d460332009-03-18 10:01:51 +0000934 } else if (isa<PrecompileJobAction>(JA)) {
Argyrios Kyrtzidise5c35372010-08-11 23:27:58 +0000935 // Use PCH if the user requested it.
Daniel Dunbar0ebd9322009-10-15 20:02:44 +0000936 bool UsePCH = D.CCCUsePCH;
Daniel Dunbar0ebd9322009-10-15 20:02:44 +0000937
938 if (UsePCH)
Douglas Gregordf91ef32009-04-18 00:34:01 +0000939 CmdArgs.push_back("-emit-pch");
940 else
941 CmdArgs.push_back("-emit-pth");
Daniel Dunbar1d460332009-03-18 10:01:51 +0000942 } else {
943 assert(isa<CompileJobAction>(JA) && "Invalid action for clang tool.");
Daniel Dunbarc21c4852009-04-08 23:54:23 +0000944
Daniel Dunbar1d460332009-03-18 10:01:51 +0000945 if (JA.getType() == types::TY_Nothing) {
946 CmdArgs.push_back("-fsyntax-only");
Daniel Dunbar6c6424b2010-06-07 23:28:45 +0000947 } else if (JA.getType() == types::TY_LLVM_IR ||
948 JA.getType() == types::TY_LTO_IR) {
Daniel Dunbar1d460332009-03-18 10:01:51 +0000949 CmdArgs.push_back("-emit-llvm");
Daniel Dunbar6c6424b2010-06-07 23:28:45 +0000950 } else if (JA.getType() == types::TY_LLVM_BC ||
951 JA.getType() == types::TY_LTO_BC) {
Daniel Dunbar1d460332009-03-18 10:01:51 +0000952 CmdArgs.push_back("-emit-llvm-bc");
953 } else if (JA.getType() == types::TY_PP_Asm) {
Daniel Dunbare3b8d072009-09-17 00:47:53 +0000954 CmdArgs.push_back("-S");
Daniel Dunbar5915fbf2009-09-01 16:57:46 +0000955 } else if (JA.getType() == types::TY_AST) {
956 CmdArgs.push_back("-emit-pch");
Daniel Dunbar64952502010-02-11 03:16:21 +0000957 } else if (JA.getType() == types::TY_RewrittenObjC) {
958 CmdArgs.push_back("-rewrite-objc");
Daniel Dunbar5314e442010-07-19 19:44:22 +0000959 IsRewriter = true;
Daniel Dunbar64952502010-02-11 03:16:21 +0000960 } else {
961 assert(JA.getType() == types::TY_PP_Asm &&
962 "Unexpected output type!");
Daniel Dunbar1d460332009-03-18 10:01:51 +0000963 }
Daniel Dunbarb488c1d2009-03-18 08:07:30 +0000964 }
965
Daniel Dunbar1d460332009-03-18 10:01:51 +0000966 // The make clang go fast button.
967 CmdArgs.push_back("-disable-free");
968
John McCallb689afb2010-02-13 03:50:24 +0000969 // Disable the verification pass in -asserts builds.
970#ifdef NDEBUG
971 CmdArgs.push_back("-disable-llvm-verifier");
972#endif
973
Daniel Dunbarc9abc042009-04-08 05:11:16 +0000974 // Set the main file name, so that debug info works even with
975 // -save-temps.
976 CmdArgs.push_back("-main-file-name");
977 CmdArgs.push_back(darwin::CC1::getBaseInputName(Args, Inputs));
978
Daniel Dunbar3bbc7532009-04-08 18:03:55 +0000979 // Some flags which affect the language (via preprocessor
980 // defines). See darwin::CC1::AddCPPArgs.
981 if (Args.hasArg(options::OPT_static))
982 CmdArgs.push_back("-static-define");
983
Daniel Dunbar1d460332009-03-18 10:01:51 +0000984 if (isa<AnalyzeJobAction>(JA)) {
Ted Kremenekb8bb3e72009-09-25 05:55:59 +0000985 // Enable region store model by default.
986 CmdArgs.push_back("-analyzer-store=region");
987
Ted Kremenekb40d06d2009-12-07 22:26:14 +0000988 // Treat blocks as analysis entry points.
989 CmdArgs.push_back("-analyzer-opt-analyze-nested-blocks");
990
Daniel Dunbar1d460332009-03-18 10:01:51 +0000991 // Add default argument set.
Daniel Dunbard8fc0f22009-05-22 00:38:15 +0000992 if (!Args.hasArg(options::OPT__analyzer_no_default_checks)) {
Ted Kremenekf0335172011-01-14 22:31:31 +0000993 types::ID InputType = Inputs[0].getType();
994
995 // Checks to perform for all language types.
Argyrios Kyrtzidis027a6ab2011-02-15 07:42:33 +0000996
997 CmdArgs.push_back("-analyzer-checker=core");
998 if (getToolChain().getTriple().getOS() != llvm::Triple::Win32)
999 CmdArgs.push_back("-analyzer-checker=unix");
1000 if (getToolChain().getTriple().getVendor() == llvm::Triple::Apple)
1001 CmdArgs.push_back("-analyzer-checker=macosx");
1002
Argyrios Kyrtzidisa676d502011-02-28 01:27:12 +00001003 CmdArgs.push_back("-analyzer-checker=DeadStores");
1004
Ted Kremenekf0335172011-01-14 22:31:31 +00001005 // Checks to perform for Objective-C/Objective-C++.
1006 if (types::isObjC(InputType)) {
Argyrios Kyrtzidis43dee222011-02-14 18:13:31 +00001007 // Enable all checkers in 'cocoa' package.
1008 CmdArgs.push_back("-analyzer-checker=cocoa");
Ted Kremenekf0335172011-01-14 22:31:31 +00001009 }
1010
Argyrios Kyrtzidis8899ab02011-02-19 08:03:21 +00001011 CmdArgs.push_back("-analyzer-eagerly-assume");
Daniel Dunbard8fc0f22009-05-22 00:38:15 +00001012 }
Daniel Dunbarc21c4852009-04-08 23:54:23 +00001013
Daniel Dunbard8fc0f22009-05-22 00:38:15 +00001014 // Set the output format. The default is plist, for (lame) historical
1015 // reasons.
1016 CmdArgs.push_back("-analyzer-output");
1017 if (Arg *A = Args.getLastArg(options::OPT__analyzer_output))
1018 CmdArgs.push_back(A->getValue(Args));
1019 else
1020 CmdArgs.push_back("plist");
Daniel Dunbar1d460332009-03-18 10:01:51 +00001021
Ted Kremenek0647a7b2010-03-22 22:32:05 +00001022 // Disable the presentation of standard compiler warnings when
1023 // using --analyze. We only want to show static analyzer diagnostics
1024 // or frontend errors.
1025 CmdArgs.push_back("-w");
1026
Daniel Dunbar1d460332009-03-18 10:01:51 +00001027 // Add -Xanalyzer arguments when running as analyzer.
1028 Args.AddAllArgValues(CmdArgs, options::OPT_Xanalyzer);
Mike Stump1eb44332009-09-09 15:08:12 +00001029 }
1030
Daniel Dunbare2fd6642009-09-10 01:21:12 +00001031 CheckCodeGenerationOptions(D, Args);
1032
Daniel Dunbarbc85be82009-04-29 18:32:25 +00001033 // Perform argument translation for LLVM backend. This
1034 // takes some care in reconciling with llvm-gcc. The
1035 // issue is that llvm-gcc translates these options based on
1036 // the values in cc1, whereas we are processing based on
1037 // the driver arguments.
Daniel Dunbarc21c4852009-04-08 23:54:23 +00001038
Daniel Dunbarbc85be82009-04-29 18:32:25 +00001039 // This comes from the default translation the driver + cc1
1040 // would do to enable flag_pic.
1041 //
1042 // FIXME: Centralize this code.
1043 bool PICEnabled = (Args.hasArg(options::OPT_fPIC) ||
1044 Args.hasArg(options::OPT_fpic) ||
1045 Args.hasArg(options::OPT_fPIE) ||
1046 Args.hasArg(options::OPT_fpie));
1047 bool PICDisabled = (Args.hasArg(options::OPT_mkernel) ||
1048 Args.hasArg(options::OPT_static));
1049 const char *Model = getToolChain().GetForcedPicModel();
1050 if (!Model) {
1051 if (Args.hasArg(options::OPT_mdynamic_no_pic))
1052 Model = "dynamic-no-pic";
1053 else if (PICDisabled)
1054 Model = "static";
1055 else if (PICEnabled)
1056 Model = "pic";
Daniel Dunbar1d460332009-03-18 10:01:51 +00001057 else
Daniel Dunbarbc85be82009-04-29 18:32:25 +00001058 Model = getToolChain().GetDefaultRelocationModel();
Daniel Dunbar1d460332009-03-18 10:01:51 +00001059 }
Daniel Dunbarf219e7c2009-11-29 07:18:39 +00001060 if (llvm::StringRef(Model) != "pic") {
1061 CmdArgs.push_back("-mrelocation-model");
1062 CmdArgs.push_back(Model);
1063 }
Daniel Dunbarbc85be82009-04-29 18:32:25 +00001064
1065 // Infer the __PIC__ value.
1066 //
1067 // FIXME: This isn't quite right on Darwin, which always sets
1068 // __PIC__=2.
1069 if (strcmp(Model, "pic") == 0 || strcmp(Model, "dynamic-no-pic") == 0) {
Daniel Dunbar76743522009-11-29 02:39:08 +00001070 CmdArgs.push_back("-pic-level");
1071 CmdArgs.push_back(Args.hasArg(options::OPT_fPIC) ? "2" : "1");
Daniel Dunbarbc85be82009-04-29 18:32:25 +00001072 }
Tanya Lattner59876c22009-11-04 01:18:09 +00001073 if (!Args.hasFlag(options::OPT_fmerge_all_constants,
1074 options::OPT_fno_merge_all_constants))
Daniel Dunbar66861e02009-11-20 22:21:36 +00001075 CmdArgs.push_back("-no-merge-all-constants");
Daniel Dunbar6bea73b2009-09-16 06:17:29 +00001076
Daniel Dunbarf219e7c2009-11-29 07:18:39 +00001077 // LLVM Code Generator Options.
1078
Daniel Dunbar17d3fea2011-02-09 17:54:19 +00001079 if (Arg *A = Args.getLastArg(options::OPT_mregparm_EQ)) {
1080 CmdArgs.push_back("-mregparm");
1081 CmdArgs.push_back(A->getValue(Args));
1082 }
1083
Daniel Dunbarf219e7c2009-11-29 07:18:39 +00001084 // FIXME: Set --enable-unsafe-fp-math.
1085 if (Args.hasFlag(options::OPT_fno_omit_frame_pointer,
1086 options::OPT_fomit_frame_pointer))
1087 CmdArgs.push_back("-mdisable-fp-elim");
1088 if (!Args.hasFlag(options::OPT_fzero_initialized_in_bss,
1089 options::OPT_fno_zero_initialized_in_bss))
1090 CmdArgs.push_back("-mno-zero-initialized-in-bss");
Daniel Dunbar398c6102011-02-04 02:20:39 +00001091 if (!Args.hasFlag(options::OPT_fstrict_aliasing,
1092 options::OPT_fno_strict_aliasing,
1093 getToolChain().IsStrictAliasingDefault()))
Dan Gohman4d5625e2010-10-14 22:36:56 +00001094 CmdArgs.push_back("-relaxed-aliasing");
Daniel Dunbar1b718482010-05-14 22:00:22 +00001095
1096 // Decide whether to use verbose asm. Verbose assembly is the default on
1097 // toolchains which have the integrated assembler on by default.
1098 bool IsVerboseAsmDefault = getToolChain().IsIntegratedAssemblerDefault();
1099 if (Args.hasFlag(options::OPT_fverbose_asm, options::OPT_fno_verbose_asm,
Michael J. Spencer20249a12010-10-21 03:16:25 +00001100 IsVerboseAsmDefault) ||
Daniel Dunbar1b718482010-05-14 22:00:22 +00001101 Args.hasArg(options::OPT_dA))
Daniel Dunbarf219e7c2009-11-29 07:18:39 +00001102 CmdArgs.push_back("-masm-verbose");
Daniel Dunbar1b718482010-05-14 22:00:22 +00001103
Daniel Dunbarf219e7c2009-11-29 07:18:39 +00001104 if (Args.hasArg(options::OPT_fdebug_pass_structure)) {
1105 CmdArgs.push_back("-mdebug-pass");
1106 CmdArgs.push_back("Structure");
1107 }
1108 if (Args.hasArg(options::OPT_fdebug_pass_arguments)) {
1109 CmdArgs.push_back("-mdebug-pass");
1110 CmdArgs.push_back("Arguments");
1111 }
1112
John McCalld0c2ec42010-02-19 02:45:38 +00001113 // Enable -mconstructor-aliases except on darwin, where we have to
1114 // work around a linker bug; see <rdar://problem/7651567>.
1115 if (getToolChain().getTriple().getOS() != llvm::Triple::Darwin)
1116 CmdArgs.push_back("-mconstructor-aliases");
NAKAMURA Takumi125b4cb2011-02-17 08:50:50 +00001117
Douglas Gregor6f755502011-02-01 15:15:22 +00001118 if (Args.hasArg(options::OPT_mms_bitfields)) {
1119 CmdArgs.push_back("-mms-bitfields");
1120 }
John McCalld0c2ec42010-02-19 02:45:38 +00001121
Daniel Dunbar6bea73b2009-09-16 06:17:29 +00001122 // This is a coarse approximation of what llvm-gcc actually does, both
1123 // -fasynchronous-unwind-tables and -fnon-call-exceptions interact in more
1124 // complicated ways.
1125 bool AsynchronousUnwindTables =
1126 Args.hasFlag(options::OPT_fasynchronous_unwind_tables,
1127 options::OPT_fno_asynchronous_unwind_tables,
1128 getToolChain().IsUnwindTablesDefault() &&
Daniel Dunbar0a80ba72010-03-20 04:52:14 +00001129 !KernelOrKext);
Daniel Dunbar6bea73b2009-09-16 06:17:29 +00001130 if (Args.hasFlag(options::OPT_funwind_tables, options::OPT_fno_unwind_tables,
1131 AsynchronousUnwindTables))
Daniel Dunbarf219e7c2009-11-29 07:18:39 +00001132 CmdArgs.push_back("-munwind-tables");
1133
1134 if (Arg *A = Args.getLastArg(options::OPT_flimited_precision_EQ)) {
1135 CmdArgs.push_back("-mlimit-float-precision");
1136 CmdArgs.push_back(A->getValue(Args));
1137 }
Daniel Dunbarbc85be82009-04-29 18:32:25 +00001138
Daniel Dunbar868bd0a2009-05-06 03:16:41 +00001139 // FIXME: Handle -mtune=.
1140 (void) Args.hasArg(options::OPT_mtune_EQ);
Daniel Dunbarbc85be82009-04-29 18:32:25 +00001141
Benjamin Kramer8e9ef0d2009-08-05 14:30:52 +00001142 if (Arg *A = Args.getLastArg(options::OPT_mcmodel_EQ)) {
Daniel Dunbarf219e7c2009-11-29 07:18:39 +00001143 CmdArgs.push_back("-mcode-model");
Benjamin Kramer8e9ef0d2009-08-05 14:30:52 +00001144 CmdArgs.push_back(A->getValue(Args));
1145 }
1146
Daniel Dunbar6acda162009-09-09 22:33:08 +00001147 // Add target specific cpu and features flags.
1148 switch(getToolChain().getTriple().getArch()) {
1149 default:
1150 break;
Daniel Dunbar868bd0a2009-05-06 03:16:41 +00001151
Daniel Dunbarb163ef72009-09-10 04:57:17 +00001152 case llvm::Triple::arm:
1153 case llvm::Triple::thumb:
1154 AddARMTargetArgs(Args, CmdArgs);
1155 break;
1156
Eric Christophered734732010-03-02 02:41:08 +00001157 case llvm::Triple::mips:
1158 case llvm::Triple::mipsel:
1159 AddMIPSTargetArgs(Args, CmdArgs);
1160 break;
1161
Bruno Cardoso Lopes9284d212010-11-09 17:21:19 +00001162 case llvm::Triple::sparc:
1163 AddSparcTargetArgs(Args, CmdArgs);
1164 break;
1165
Daniel Dunbar6acda162009-09-09 22:33:08 +00001166 case llvm::Triple::x86:
1167 case llvm::Triple::x86_64:
1168 AddX86TargetArgs(Args, CmdArgs);
1169 break;
Daniel Dunbarbc85be82009-04-29 18:32:25 +00001170 }
1171
Daniel Dunbarc176bc62010-08-11 23:07:47 +00001172 // Pass the linker version in use.
1173 if (Arg *A = Args.getLastArg(options::OPT_mlinker_version_EQ)) {
1174 CmdArgs.push_back("-target-linker-version");
1175 CmdArgs.push_back(A->getValue(Args));
1176 }
1177
Nick Lewyckyb2d11cc2011-02-02 06:43:03 +00001178 // -mno-omit-leaf-frame-pointer is the default on Darwin.
Daniel Dunbar1ad66482010-07-01 01:31:45 +00001179 if (Args.hasFlag(options::OPT_momit_leaf_frame_pointer,
Nick Lewyckyb2d11cc2011-02-02 06:43:03 +00001180 options::OPT_mno_omit_leaf_frame_pointer,
1181 getToolChain().getTriple().getOS() != llvm::Triple::Darwin))
Daniel Dunbar1ad66482010-07-01 01:31:45 +00001182 CmdArgs.push_back("-momit-leaf-frame-pointer");
1183
Dan Gohmanc31176d2010-01-08 02:20:44 +00001184 // -fno-math-errno is default.
1185 if (Args.hasFlag(options::OPT_fmath_errno,
Daniel Dunbarbc85be82009-04-29 18:32:25 +00001186 options::OPT_fno_math_errno,
Dan Gohmanc31176d2010-01-08 02:20:44 +00001187 false))
1188 CmdArgs.push_back("-fmath-errno");
Daniel Dunbarbc85be82009-04-29 18:32:25 +00001189
Daniel Dunbarb30575c2010-05-12 18:19:58 +00001190 // Explicitly error on some things we know we don't support and can't just
1191 // ignore.
1192 types::ID InputType = Inputs[0].getType();
Daniel Dunbare94db472010-09-24 19:39:37 +00001193 if (!Args.hasArg(options::OPT_fallow_unsupported)) {
1194 Arg *Unsupported;
1195 if ((Unsupported = Args.getLastArg(options::OPT_MG)) ||
Argyrios Kyrtzidis9a2b9d72010-10-08 00:25:19 +00001196 (Unsupported = Args.getLastArg(options::OPT_iframework)))
Daniel Dunbare94db472010-09-24 19:39:37 +00001197 D.Diag(clang::diag::err_drv_clang_unsupported)
Daniel Dunbarb30575c2010-05-12 18:19:58 +00001198 << Unsupported->getOption().getName();
Daniel Dunbare94db472010-09-24 19:39:37 +00001199
1200 if (types::isCXX(InputType) &&
1201 getToolChain().getTriple().getOS() == llvm::Triple::Darwin &&
1202 getToolChain().getTriple().getArch() == llvm::Triple::x86) {
1203 if ((Unsupported = Args.getLastArg(options::OPT_fapple_kext)))
1204 D.Diag(clang::diag::err_drv_clang_unsupported_opt_cxx_darwin_i386)
1205 << Unsupported->getOption().getName();
1206 }
Daniel Dunbarb30575c2010-05-12 18:19:58 +00001207 }
1208
Daniel Dunbar1d460332009-03-18 10:01:51 +00001209 Args.AddAllArgs(CmdArgs, options::OPT_v);
Daniel Dunbarf7c16d92010-08-24 22:44:13 +00001210 Args.AddLastArg(CmdArgs, options::OPT_H);
Daniel Dunbar322c29f2011-02-02 21:11:35 +00001211 if (D.CCPrintHeaders) {
1212 CmdArgs.push_back("-header-include-file");
1213 CmdArgs.push_back(D.CCPrintHeadersFilename ?
1214 D.CCPrintHeadersFilename : "-");
1215 }
Daniel Dunbar1d460332009-03-18 10:01:51 +00001216 Args.AddLastArg(CmdArgs, options::OPT_P);
Mike Stump1eb44332009-09-09 15:08:12 +00001217 Args.AddLastArg(CmdArgs, options::OPT_print_ivar_layout);
Daniel Dunbar1d460332009-03-18 10:01:51 +00001218
1219 // Special case debug options to only pass -g to clang. This is
1220 // wrong.
Rafael Espindola18f36d92010-03-07 04:46:18 +00001221 Args.ClaimAllArgs(options::OPT_g_Group);
Daniel Dunbar6b3d5a62010-05-12 18:19:55 +00001222 if (Arg *A = Args.getLastArg(options::OPT_g_Group))
1223 if (!A->getOption().matches(options::OPT_g0))
1224 CmdArgs.push_back("-g");
Daniel Dunbar1d460332009-03-18 10:01:51 +00001225
Rafael Espindola9cf933a2010-05-06 21:06:04 +00001226 Args.AddAllArgs(CmdArgs, options::OPT_ffunction_sections);
1227 Args.AddAllArgs(CmdArgs, options::OPT_fdata_sections);
1228
Chris Lattner7255a2d2010-06-22 00:03:40 +00001229 Args.AddAllArgs(CmdArgs, options::OPT_finstrument_functions);
1230
Daniel Dunbar1d460332009-03-18 10:01:51 +00001231 Args.AddLastArg(CmdArgs, options::OPT_nostdinc);
Douglas Gregor4c2bcad2010-03-24 20:13:48 +00001232 Args.AddLastArg(CmdArgs, options::OPT_nostdincxx);
Rafael Espindola8d737cc2009-10-26 13:36:57 +00001233 Args.AddLastArg(CmdArgs, options::OPT_nobuiltininc);
Daniel Dunbar1d460332009-03-18 10:01:51 +00001234
Daniel Dunbar5f122322009-12-15 01:02:52 +00001235 // Pass the path to compiler resource files.
Daniel Dunbar5f122322009-12-15 01:02:52 +00001236 CmdArgs.push_back("-resource-dir");
Daniel Dunbar225c4172010-01-20 02:35:16 +00001237 CmdArgs.push_back(D.ResourceDir.c_str());
Daniel Dunbar2ac9fc22009-04-07 21:42:00 +00001238
Argyrios Kyrtzidis389db162010-11-03 22:45:23 +00001239 Args.AddLastArg(CmdArgs, options::OPT_working_directory);
1240
Daniel Dunbarc21c4852009-04-08 23:54:23 +00001241 // Add preprocessing options like -I, -D, etc. if we are using the
1242 // preprocessor.
1243 //
1244 // FIXME: Support -fpreprocessed
Daniel Dunbarc21c4852009-04-08 23:54:23 +00001245 if (types::getPreprocessedType(InputType) != types::TY_INVALID)
Douglas Gregordf91ef32009-04-18 00:34:01 +00001246 AddPreprocessingOptions(D, Args, CmdArgs, Output, Inputs);
Daniel Dunbar1d460332009-03-18 10:01:51 +00001247
Daniel Dunbar20f0eac2009-09-17 06:53:36 +00001248 // Manually translate -O to -O2 and -O4 to -O3; let clang reject
Daniel Dunbar337a6272009-03-24 20:17:30 +00001249 // others.
1250 if (Arg *A = Args.getLastArg(options::OPT_O_Group)) {
Daniel Dunbarb827a052009-11-19 03:26:40 +00001251 if (A->getOption().matches(options::OPT_O4))
Daniel Dunbar337a6272009-03-24 20:17:30 +00001252 CmdArgs.push_back("-O3");
Daniel Dunbar473916c2010-05-27 06:51:08 +00001253 else if (A->getOption().matches(options::OPT_O) &&
1254 A->getValue(Args)[0] == '\0')
Daniel Dunbar20f0eac2009-09-17 06:53:36 +00001255 CmdArgs.push_back("-O2");
Daniel Dunbar5e3bc942010-11-13 18:17:11 +00001256 else if (A->getOption().matches(options::OPT_O) &&
1257 A->getValue(Args)[0] == 'z' &&
1258 A->getValue(Args)[1] == '\0')
1259 CmdArgs.push_back("-Os");
Daniel Dunbar1d460332009-03-18 10:01:51 +00001260 else
Daniel Dunbar5697aa02009-03-18 23:39:35 +00001261 A->render(Args, CmdArgs);
Daniel Dunbar1d460332009-03-18 10:01:51 +00001262 }
1263
Daniel Dunbar6e8371e2009-10-29 02:24:45 +00001264 Args.AddAllArgs(CmdArgs, options::OPT_W_Group);
1265 Args.AddLastArg(CmdArgs, options::OPT_pedantic);
1266 Args.AddLastArg(CmdArgs, options::OPT_pedantic_errors);
Daniel Dunbar1d460332009-03-18 10:01:51 +00001267 Args.AddLastArg(CmdArgs, options::OPT_w);
Daniel Dunbard573d262009-04-07 22:13:21 +00001268
1269 // Handle -{std, ansi, trigraphs} -- take the last of -{std, ansi}
1270 // (-ansi is equivalent to -std=c89).
1271 //
1272 // If a std is supplied, only add -trigraphs if it follows the
1273 // option.
1274 if (Arg *Std = Args.getLastArg(options::OPT_std_EQ, options::OPT_ansi)) {
1275 if (Std->getOption().matches(options::OPT_ansi))
Nuno Lopes528365d2009-10-16 14:28:06 +00001276 if (types::isCXX(InputType))
Daniel Dunbar294691e2009-11-04 06:24:38 +00001277 CmdArgs.push_back("-std=c++98");
Nuno Lopes528365d2009-10-16 14:28:06 +00001278 else
Daniel Dunbar294691e2009-11-04 06:24:38 +00001279 CmdArgs.push_back("-std=c89");
Daniel Dunbard573d262009-04-07 22:13:21 +00001280 else
1281 Std->render(Args, CmdArgs);
1282
Daniel Dunbar0e100312010-06-14 21:23:08 +00001283 if (Arg *A = Args.getLastArg(options::OPT_std_EQ, options::OPT_ansi,
1284 options::OPT_trigraphs))
1285 if (A != Std)
Daniel Dunbard573d262009-04-07 22:13:21 +00001286 A->render(Args, CmdArgs);
Daniel Dunbara3ff2022009-04-26 01:10:38 +00001287 } else {
1288 // Honor -std-default.
Daniel Dunbar4a5290e2010-01-29 21:03:02 +00001289 //
1290 // FIXME: Clang doesn't correctly handle -std= when the input language
1291 // doesn't match. For the time being just ignore this for C++ inputs;
1292 // eventually we want to do all the standard defaulting here instead of
1293 // splitting it between the driver and clang -cc1.
1294 if (!types::isCXX(InputType))
1295 Args.AddAllArgsTranslated(CmdArgs, options::OPT_std_default_EQ,
1296 "-std=", /*Joined=*/true);
Daniel Dunbard573d262009-04-07 22:13:21 +00001297 Args.AddLastArg(CmdArgs, options::OPT_trigraphs);
Daniel Dunbara3ff2022009-04-26 01:10:38 +00001298 }
Daniel Dunbarc21c4852009-04-08 23:54:23 +00001299
Chandler Carruthc304ba32010-05-22 02:21:53 +00001300 // Translate GCC's misnamer '-fasm' arguments to '-fgnu-keywords'.
1301 if (Arg *Asm = Args.getLastArg(options::OPT_fasm, options::OPT_fno_asm)) {
1302 if (Asm->getOption().matches(options::OPT_fasm))
1303 CmdArgs.push_back("-fgnu-keywords");
1304 else
1305 CmdArgs.push_back("-fno-gnu-keywords");
1306 }
1307
Daniel Dunbar1d460332009-03-18 10:01:51 +00001308 if (Arg *A = Args.getLastArg(options::OPT_ftemplate_depth_)) {
1309 CmdArgs.push_back("-ftemplate-depth");
1310 CmdArgs.push_back(A->getValue(Args));
1311 }
1312
Argyrios Kyrtzidis1380a142010-11-18 00:20:36 +00001313 if (Arg *A = Args.getLastArg(options::OPT_Wlarge_by_value_copy_EQ,
1314 options::OPT_Wlarge_by_value_copy_def)) {
1315 CmdArgs.push_back("-Wlarge-by-value-copy");
Argyrios Kyrtzidis3532fdd2010-11-17 23:11:54 +00001316 if (A->getNumValues())
1317 CmdArgs.push_back(A->getValue(Args));
1318 else
Argyrios Kyrtzidis1380a142010-11-18 00:20:36 +00001319 CmdArgs.push_back("64"); // default value for -Wlarge-by-value-copy.
Argyrios Kyrtzidis3532fdd2010-11-17 23:11:54 +00001320 }
1321
Daniel Dunbare4bdae72009-11-19 04:00:53 +00001322 if (Args.hasArg(options::OPT__relocatable_pch))
Daniel Dunbar66861e02009-11-20 22:21:36 +00001323 CmdArgs.push_back("-relocatable-pch");
Mike Stump1eb44332009-09-09 15:08:12 +00001324
Daniel Dunbar294691e2009-11-04 06:24:38 +00001325 if (Arg *A = Args.getLastArg(options::OPT_fconstant_string_class_EQ)) {
1326 CmdArgs.push_back("-fconstant-string-class");
1327 CmdArgs.push_back(A->getValue(Args));
1328 }
David Chisnall8a5a9aa2009-08-31 16:41:57 +00001329
Chris Lattner124fca52010-01-09 21:54:33 +00001330 if (Arg *A = Args.getLastArg(options::OPT_ftabstop_EQ)) {
1331 CmdArgs.push_back("-ftabstop");
1332 CmdArgs.push_back(A->getValue(Args));
1333 }
1334
Chris Lattner0f0c9632010-04-07 20:49:23 +00001335 CmdArgs.push_back("-ferror-limit");
1336 if (Arg *A = Args.getLastArg(options::OPT_ferror_limit_EQ))
1337 CmdArgs.push_back(A->getValue(Args));
1338 else
1339 CmdArgs.push_back("19");
Douglas Gregor575cf372010-04-20 07:18:24 +00001340
Chandler Carruthc40f73c2010-05-06 04:55:18 +00001341 if (Arg *A = Args.getLastArg(options::OPT_fmacro_backtrace_limit_EQ)) {
1342 CmdArgs.push_back("-fmacro-backtrace-limit");
Douglas Gregor6c1cb992010-05-04 17:13:42 +00001343 CmdArgs.push_back(A->getValue(Args));
Chandler Carruthc40f73c2010-05-06 04:55:18 +00001344 }
1345
1346 if (Arg *A = Args.getLastArg(options::OPT_ftemplate_backtrace_limit_EQ)) {
1347 CmdArgs.push_back("-ftemplate-backtrace-limit");
Douglas Gregor575cf372010-04-20 07:18:24 +00001348 CmdArgs.push_back(A->getValue(Args));
Chandler Carruthc40f73c2010-05-06 04:55:18 +00001349 }
1350
Daniel Dunbar55efe142009-11-04 06:24:47 +00001351 // Pass -fmessage-length=.
Daniel Dunbara28690e2009-11-30 08:40:54 +00001352 CmdArgs.push_back("-fmessage-length");
Daniel Dunbar55efe142009-11-04 06:24:47 +00001353 if (Arg *A = Args.getLastArg(options::OPT_fmessage_length_EQ)) {
Daniel Dunbara28690e2009-11-30 08:40:54 +00001354 CmdArgs.push_back(A->getValue(Args));
Daniel Dunbar55efe142009-11-04 06:24:47 +00001355 } else {
1356 // If -fmessage-length=N was not specified, determine whether this is a
1357 // terminal and, if so, implicitly define -fmessage-length appropriately.
1358 unsigned N = llvm::sys::Process::StandardErrColumns();
Daniel Dunbar55efe142009-11-04 06:24:47 +00001359 CmdArgs.push_back(Args.MakeArgString(llvm::Twine(N)));
1360 }
1361
Daniel Dunbarba8d8612009-12-03 18:42:11 +00001362 if (const Arg *A = Args.getLastArg(options::OPT_fvisibility_EQ)) {
1363 CmdArgs.push_back("-fvisibility");
1364 CmdArgs.push_back(A->getValue(Args));
1365 }
1366
Douglas Gregor7cf84d62010-06-15 17:05:35 +00001367 Args.AddLastArg(CmdArgs, options::OPT_fvisibility_inlines_hidden);
Michael J. Spencer20249a12010-10-21 03:16:25 +00001368
Daniel Dunbar0a80ba72010-03-20 04:52:14 +00001369 // -fhosted is default.
1370 if (KernelOrKext || Args.hasFlag(options::OPT_ffreestanding,
1371 options::OPT_fhosted,
1372 false))
1373 CmdArgs.push_back("-ffreestanding");
1374
Daniel Dunbarba8d8612009-12-03 18:42:11 +00001375 // Forward -f (flag) options which we can pass directly.
Mike Stump9c276ae2009-12-12 01:27:46 +00001376 Args.AddLastArg(CmdArgs, options::OPT_fcatch_undefined_behavior);
Daniel Dunbar3aaf0822009-04-07 21:51:40 +00001377 Args.AddLastArg(CmdArgs, options::OPT_femit_all_decls);
Daniel Dunbar3aaf0822009-04-07 21:51:40 +00001378 Args.AddLastArg(CmdArgs, options::OPT_fheinous_gnu_extensions);
Devang Patelc69e1cf2010-09-30 19:05:55 +00001379 Args.AddLastArg(CmdArgs, options::OPT_flimit_debug_info);
Roman Divackybe4c8702011-02-10 16:52:03 +00001380 Args.AddLastArg(CmdArgs, options::OPT_pg);
Daniel Dunbar8c6fa842010-03-16 16:57:46 +00001381
1382 // -flax-vector-conversions is default.
1383 if (!Args.hasFlag(options::OPT_flax_vector_conversions,
1384 options::OPT_fno_lax_vector_conversions))
1385 CmdArgs.push_back("-fno-lax-vector-conversions");
1386
Daniel Dunbar43a9b322010-04-10 16:20:23 +00001387 // Handle -fobjc-gc and -fobjc-gc-only. They are exclusive, and -fobjc-gc-only
1388 // takes precedence.
1389 const Arg *GCArg = Args.getLastArg(options::OPT_fobjc_gc_only);
1390 if (!GCArg)
1391 GCArg = Args.getLastArg(options::OPT_fobjc_gc);
1392 if (GCArg) {
1393 if (getToolChain().SupportsObjCGC()) {
1394 GCArg->render(Args, CmdArgs);
1395 } else {
1396 // FIXME: We should move this to a hard error.
1397 D.Diag(clang::diag::warn_drv_objc_gc_unsupported)
1398 << GCArg->getAsString(Args);
1399 }
1400 }
1401
Fariborz Jahanianb466d012011-01-07 01:05:02 +00001402 if (Args.getLastArg(options::OPT_fapple_kext))
1403 CmdArgs.push_back("-fapple-kext");
1404
Daniel Dunbar3aaf0822009-04-07 21:51:40 +00001405 Args.AddLastArg(CmdArgs, options::OPT_fno_show_column);
Fariborz Jahanian34e65772009-05-22 20:17:16 +00001406 Args.AddLastArg(CmdArgs, options::OPT_fobjc_sender_dependent_dispatch);
Chris Lattner182e0922009-04-21 05:34:31 +00001407 Args.AddLastArg(CmdArgs, options::OPT_fdiagnostics_print_source_range_info);
Douglas Gregor4786c152010-08-19 20:24:43 +00001408 Args.AddLastArg(CmdArgs, options::OPT_fdiagnostics_parseable_fixits);
Daniel Dunbar3aaf0822009-04-07 21:51:40 +00001409 Args.AddLastArg(CmdArgs, options::OPT_ftime_report);
1410 Args.AddLastArg(CmdArgs, options::OPT_ftrapv);
David Chisnall7f18e672010-09-17 18:29:54 +00001411
1412 if (Arg *A = Args.getLastArg(options::OPT_ftrapv_handler_EQ)) {
1413 CmdArgs.push_back("-ftrapv-handler");
1414 CmdArgs.push_back(A->getValue(Args));
1415 }
1416
Chris Lattnera4d71452010-06-26 21:25:03 +00001417 Args.AddLastArg(CmdArgs, options::OPT_fwrapv);
Daniel Dunbar3aaf0822009-04-07 21:51:40 +00001418 Args.AddLastArg(CmdArgs, options::OPT_fwritable_strings);
Eric Christopherf84d4092010-08-07 23:08:14 +00001419 Args.AddLastArg(CmdArgs, options::OPT_funroll_loops);
Daniel Dunbar1d460332009-03-18 10:01:51 +00001420
Daniel Dunbar5345c392009-09-03 04:54:28 +00001421 Args.AddLastArg(CmdArgs, options::OPT_pthread);
1422
Daniel Dunbar9e5cc6b2009-11-17 08:07:36 +00001423 // -stack-protector=0 is default.
1424 unsigned StackProtectorLevel = 0;
Bill Wendling45483f72009-06-28 07:36:13 +00001425 if (Arg *A = Args.getLastArg(options::OPT_fno_stack_protector,
1426 options::OPT_fstack_protector_all,
1427 options::OPT_fstack_protector)) {
Daniel Dunbar9e5cc6b2009-11-17 08:07:36 +00001428 if (A->getOption().matches(options::OPT_fstack_protector))
1429 StackProtectorLevel = 1;
1430 else if (A->getOption().matches(options::OPT_fstack_protector_all))
1431 StackProtectorLevel = 2;
1432 } else
1433 StackProtectorLevel = getToolChain().GetDefaultStackProtectorLevel();
1434 if (StackProtectorLevel) {
1435 CmdArgs.push_back("-stack-protector");
1436 CmdArgs.push_back(Args.MakeArgString(llvm::Twine(StackProtectorLevel)));
Bill Wendling45483f72009-06-28 07:36:13 +00001437 }
1438
Daniel Dunbar48d1ef72009-04-07 21:16:11 +00001439 // Forward -f options with positive and negative forms; we translate
1440 // these by hand.
1441
Fariborz Jahanianb466d012011-01-07 01:05:02 +00001442 if (Args.hasArg(options::OPT_mkernel)) {
Daniel Dunbar2843c192011-02-04 17:24:47 +00001443 if (!Args.hasArg(options::OPT_fapple_kext) && types::isCXX(InputType))
Fariborz Jahanianb466d012011-01-07 01:05:02 +00001444 CmdArgs.push_back("-fapple-kext");
1445 if (!Args.hasArg(options::OPT_fbuiltin))
1446 CmdArgs.push_back("-fno-builtin");
1447 }
Daniel Dunbar9e5cc6b2009-11-17 08:07:36 +00001448 // -fbuiltin is default.
Fariborz Jahanianb466d012011-01-07 01:05:02 +00001449 else if (!Args.hasFlag(options::OPT_fbuiltin, options::OPT_fno_builtin))
Daniel Dunbar53e84842009-11-19 04:55:23 +00001450 CmdArgs.push_back("-fno-builtin");
Daniel Dunbar48d1ef72009-04-07 21:16:11 +00001451
Nuno Lopesfc284482009-12-16 16:59:22 +00001452 if (!Args.hasFlag(options::OPT_fassume_sane_operator_new,
1453 options::OPT_fno_assume_sane_operator_new))
1454 CmdArgs.push_back("-fno-assume-sane-operator-new");
1455
Daniel Dunbar9e5cc6b2009-11-17 08:07:36 +00001456 // -fblocks=0 is default.
1457 if (Args.hasFlag(options::OPT_fblocks, options::OPT_fno_blocks,
David Chisnalle6533ff2011-02-28 17:11:43 +00001458 getToolChain().IsBlocksDefault()) ||
1459 (Args.hasArg(options::OPT_fgnu_runtime) &&
1460 Args.hasArg(options::OPT_fobjc_nonfragile_abi) &&
1461 !Args.hasArg(options::OPT_fno_blocks))) {
Daniel Dunbar9e5cc6b2009-11-17 08:07:36 +00001462 CmdArgs.push_back("-fblocks");
David Chisnall5e530af2009-11-17 19:33:30 +00001463 }
Daniel Dunbar48d1ef72009-04-07 21:16:11 +00001464
John McCall32579cf2010-04-09 19:12:06 +00001465 // -faccess-control is default.
John McCall7002f4c2010-04-09 19:03:51 +00001466 if (Args.hasFlag(options::OPT_fno_access_control,
1467 options::OPT_faccess_control,
John McCall32579cf2010-04-09 19:12:06 +00001468 false))
John McCall7002f4c2010-04-09 19:03:51 +00001469 CmdArgs.push_back("-fno-access-control");
John McCall3ddd6e02010-03-17 01:32:13 +00001470
Anders Carlssona4c24752010-11-21 00:09:52 +00001471 // -felide-constructors is the default.
1472 if (Args.hasFlag(options::OPT_fno_elide_constructors,
1473 options::OPT_felide_constructors,
1474 false))
1475 CmdArgs.push_back("-fno-elide-constructors");
1476
Anders Carlsson15348ae2011-02-28 02:27:16 +00001477 // Add exception args.
1478 addExceptionArgs(Args, InputType, getToolChain().getTriple(),
1479 KernelOrKext, IsRewriter, CmdArgs);
Mike Stump738f8c22009-07-31 23:15:31 +00001480
Daniel Dunbarb2987d12010-02-10 18:49:11 +00001481 if (getToolChain().UseSjLjExceptions())
1482 CmdArgs.push_back("-fsjlj-exceptions");
1483
Daniel Dunbar0be42c42009-11-17 07:06:20 +00001484 // -frtti is default.
Daniel Dunbar0a80ba72010-03-20 04:52:14 +00001485 if (KernelOrKext ||
1486 !Args.hasFlag(options::OPT_frtti, options::OPT_fno_rtti))
Daniel Dunbar53e84842009-11-19 04:55:23 +00001487 CmdArgs.push_back("-fno-rtti");
Mike Stump738f8c22009-07-31 23:15:31 +00001488
Argyrios Kyrtzidis9a2b9d72010-10-08 00:25:19 +00001489 // -fshort-enums=0 is default.
1490 // FIXME: Are there targers where -fshort-enums is on by default ?
1491 if (Args.hasFlag(options::OPT_fshort_enums,
1492 options::OPT_fno_short_enums, false))
1493 CmdArgs.push_back("-fshort-enums");
1494
Daniel Dunbar1f95e652009-11-17 06:37:03 +00001495 // -fsigned-char is default.
Daniel Dunbar6d2eb4d2009-11-25 10:14:30 +00001496 if (!Args.hasFlag(options::OPT_fsigned_char, options::OPT_funsigned_char,
Daniel Dunbar1f95e652009-11-17 06:37:03 +00001497 isSignedCharDefault(getToolChain().getTriple())))
Daniel Dunbar76743522009-11-29 02:39:08 +00001498 CmdArgs.push_back("-fno-signed-char");
Eli Friedman5a779732009-06-05 07:21:14 +00001499
Anders Carlssona508b7d2010-02-06 23:23:06 +00001500 // -fthreadsafe-static is default.
Michael J. Spencer20249a12010-10-21 03:16:25 +00001501 if (!Args.hasFlag(options::OPT_fthreadsafe_statics,
Anders Carlssona508b7d2010-02-06 23:23:06 +00001502 options::OPT_fno_threadsafe_statics))
1503 CmdArgs.push_back("-fno-threadsafe-statics");
1504
Daniel Dunbarefb0fa92010-03-20 04:15:41 +00001505 // -fuse-cxa-atexit is default.
Anton Korobeynikovd0a57d52010-09-11 11:17:06 +00001506 if (KernelOrKext ||
1507 !Args.hasFlag(options::OPT_fuse_cxa_atexit, options::OPT_fno_use_cxa_atexit,
NAKAMURA Takumicd7a30b2010-10-10 01:53:03 +00001508 getToolChain().getTriple().getOS() != llvm::Triple::Cygwin &&
NAKAMURA Takumi0aa20572011-02-17 08:51:38 +00001509 getToolChain().getTriple().getOS() != llvm::Triple::MinGW32))
Daniel Dunbarefb0fa92010-03-20 04:15:41 +00001510 CmdArgs.push_back("-fno-use-cxa-atexit");
1511
Daniel Dunbar0be42c42009-11-17 07:06:20 +00001512 // -fms-extensions=0 is default.
Daniel Dunbar6d2eb4d2009-11-25 10:14:30 +00001513 if (Args.hasFlag(options::OPT_fms_extensions, options::OPT_fno_ms_extensions,
Daniel Dunbar0be42c42009-11-17 07:06:20 +00001514 getToolChain().getTriple().getOS() == llvm::Triple::Win32))
1515 CmdArgs.push_back("-fms-extensions");
1516
Michael J. Spencerdae4ac42010-10-21 05:21:48 +00001517 // -fmsc-version=1300 is default.
1518 if (Args.hasFlag(options::OPT_fms_extensions, options::OPT_fno_ms_extensions,
1519 getToolChain().getTriple().getOS() == llvm::Triple::Win32) ||
1520 Args.hasArg(options::OPT_fmsc_version)) {
1521 llvm::StringRef msc_ver = Args.getLastArgValue(options::OPT_fmsc_version);
1522 if (msc_ver.empty())
1523 CmdArgs.push_back("-fmsc-version=1300");
1524 else
1525 CmdArgs.push_back(Args.MakeArgString("-fmsc-version=" + msc_ver));
1526 }
1527
1528
Dawn Perchik400b6072010-09-02 23:59:25 +00001529 // -fborland-extensions=0 is default.
1530 if (Args.hasFlag(options::OPT_fborland_extensions,
1531 options::OPT_fno_borland_extensions, false))
1532 CmdArgs.push_back("-fborland-extensions");
1533
Chandler Carrutheb5d7b72010-04-17 20:17:31 +00001534 // -fgnu-keywords default varies depending on language; only pass if
1535 // specified.
1536 if (Arg *A = Args.getLastArg(options::OPT_fgnu_keywords,
Daniel Dunbar40788d92010-04-24 17:56:39 +00001537 options::OPT_fno_gnu_keywords))
1538 A->render(Args, CmdArgs);
Chandler Carrutheb5d7b72010-04-17 20:17:31 +00001539
Daniel Dunbar5314e442010-07-19 19:44:22 +00001540 // -fnext-runtime defaults to on Darwin and when rewriting Objective-C, and is
1541 // -the -cc1 default.
Michael J. Spencer20249a12010-10-21 03:16:25 +00001542 bool NeXTRuntimeIsDefault =
Daniel Dunbar5314e442010-07-19 19:44:22 +00001543 IsRewriter || getToolChain().getTriple().getOS() == llvm::Triple::Darwin;
Daniel Dunbar6d2eb4d2009-11-25 10:14:30 +00001544 if (!Args.hasFlag(options::OPT_fnext_runtime, options::OPT_fgnu_runtime,
Daniel Dunbar5314e442010-07-19 19:44:22 +00001545 NeXTRuntimeIsDefault))
Daniel Dunbarc5a97ec2009-11-17 07:07:28 +00001546 CmdArgs.push_back("-fgnu-runtime");
1547
Daniel Dunbar9e5cc6b2009-11-17 08:07:36 +00001548 // -fobjc-nonfragile-abi=0 is default.
1549 if (types::isObjC(InputType)) {
Daniel Dunbardea63132010-09-20 18:19:55 +00001550 // Compute the Objective-C ABI "version" to use. Version numbers are
1551 // slightly confusing for historical reasons:
1552 // 1 - Traditional "fragile" ABI
1553 // 2 - Non-fragile ABI, version 1
1554 // 3 - Non-fragile ABI, version 2
Daniel Dunbar5e2a8ac2010-04-28 23:25:24 +00001555 unsigned Version = 1;
Daniel Dunbardea63132010-09-20 18:19:55 +00001556 // If -fobjc-abi-version= is present, use that to set the version.
Daniel Dunbar5e2a8ac2010-04-28 23:25:24 +00001557 if (Arg *A = Args.getLastArg(options::OPT_fobjc_abi_version_EQ)) {
1558 if (llvm::StringRef(A->getValue(Args)) == "1")
1559 Version = 1;
1560 else if (llvm::StringRef(A->getValue(Args)) == "2")
1561 Version = 2;
Fariborz Jahanian5f81d8a2010-09-15 16:00:51 +00001562 else if (llvm::StringRef(A->getValue(Args)) == "3")
1563 Version = 3;
Daniel Dunbar5e2a8ac2010-04-28 23:25:24 +00001564 else
1565 D.Diag(clang::diag::err_drv_clang_unsupported) << A->getAsString(Args);
Daniel Dunbardea63132010-09-20 18:19:55 +00001566 } else {
1567 // Otherwise, determine if we are using the non-fragile ABI.
1568 if (Args.hasFlag(options::OPT_fobjc_nonfragile_abi,
1569 options::OPT_fno_objc_nonfragile_abi,
1570 getToolChain().IsObjCNonFragileABIDefault())) {
1571 // Determine the non-fragile ABI version to use.
Daniel Dunbarde17ba72010-11-11 16:08:59 +00001572#ifdef DISABLE_DEFAULT_NONFRAGILEABI_TWO
1573 unsigned NonFragileABIVersion = 1;
1574#else
Daniel Dunbardea63132010-09-20 18:19:55 +00001575 unsigned NonFragileABIVersion = 2;
Daniel Dunbarde17ba72010-11-11 16:08:59 +00001576#endif
Daniel Dunbardea63132010-09-20 18:19:55 +00001577
1578 if (Arg *A = Args.getLastArg(
1579 options::OPT_fobjc_nonfragile_abi_version_EQ)) {
1580 if (llvm::StringRef(A->getValue(Args)) == "1")
1581 NonFragileABIVersion = 1;
1582 else if (llvm::StringRef(A->getValue(Args)) == "2")
1583 NonFragileABIVersion = 2;
1584 else
1585 D.Diag(clang::diag::err_drv_clang_unsupported)
1586 << A->getAsString(Args);
1587 }
1588
1589 Version = 1 + NonFragileABIVersion;
1590 } else {
1591 Version = 1;
1592 }
Daniel Dunbar5e2a8ac2010-04-28 23:25:24 +00001593 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00001594
Fariborz Jahanian5f81d8a2010-09-15 16:00:51 +00001595 if (Version == 2 || Version == 3) {
Fariborz Jahanian112c3302011-01-04 20:05:20 +00001596 CmdArgs.push_back("-fobjc-nonfragile-abi");
Daniel Dunbarf643b9b2010-04-24 17:56:46 +00001597
1598 // -fobjc-dispatch-method is only relevant with the nonfragile-abi, and
1599 // legacy is the default.
1600 if (!Args.hasFlag(options::OPT_fobjc_legacy_dispatch,
1601 options::OPT_fno_objc_legacy_dispatch,
1602 getToolChain().IsObjCLegacyDispatchDefault())) {
1603 if (getToolChain().UseObjCMixedDispatch())
1604 CmdArgs.push_back("-fobjc-dispatch-method=mixed");
1605 else
1606 CmdArgs.push_back("-fobjc-dispatch-method=non-legacy");
1607 }
Daniel Dunbar984eb862010-02-01 21:07:25 +00001608 }
NAKAMURA Takumi125b4cb2011-02-17 08:50:50 +00001609
Ted Kremenekc32647d2010-12-23 21:35:43 +00001610 // -fobjc-default-synthesize-properties=0 is default.
1611 if (Args.hasFlag(options::OPT_fobjc_default_synthesize_properties,
1612 options::OPT_fno_objc_default_synthesize_properties,
1613 getToolChain().IsObjCDefaultSynthPropertiesDefault())) {
1614 CmdArgs.push_back("-fobjc-default-synthesize-properties");
1615 }
Daniel Dunbar9e5cc6b2009-11-17 08:07:36 +00001616 }
1617
Daniel Dunbar984eb862010-02-01 21:07:25 +00001618 if (!Args.hasFlag(options::OPT_fassume_sane_operator_new,
1619 options::OPT_fno_assume_sane_operator_new))
1620 CmdArgs.push_back("-fno-assume-sane-operator-new");
1621
Daniel Dunbarf35f14d2010-04-27 15:34:57 +00001622 // -fconstant-cfstrings is default, and may be subject to argument translation
1623 // on Darwin.
1624 if (!Args.hasFlag(options::OPT_fconstant_cfstrings,
1625 options::OPT_fno_constant_cfstrings) ||
1626 !Args.hasFlag(options::OPT_mconstant_cfstrings,
1627 options::OPT_mno_constant_cfstrings))
1628 CmdArgs.push_back("-fno-constant-cfstrings");
1629
John Thompsona6fda122009-11-05 20:14:16 +00001630 // -fshort-wchar default varies depending on platform; only
1631 // pass if specified.
Daniel Dunbar1744a352010-04-27 15:35:03 +00001632 if (Arg *A = Args.getLastArg(options::OPT_fshort_wchar))
1633 A->render(Args, CmdArgs);
John Thompsona6fda122009-11-05 20:14:16 +00001634
Daniel Dunbaree848a72009-10-29 02:39:57 +00001635 // -fno-pascal-strings is default, only pass non-default. If the tool chain
1636 // happened to translate to -mpascal-strings, we want to back translate here.
Daniel Dunbar82d00682009-04-07 23:51:44 +00001637 //
1638 // FIXME: This is gross; that translation should be pulled from the
1639 // tool chain.
Daniel Dunbarc21c4852009-04-08 23:54:23 +00001640 if (Args.hasFlag(options::OPT_fpascal_strings,
Daniel Dunbar82d00682009-04-07 23:51:44 +00001641 options::OPT_fno_pascal_strings,
1642 false) ||
1643 Args.hasFlag(options::OPT_mpascal_strings,
1644 options::OPT_mno_pascal_strings,
1645 false))
Daniel Dunbar48d1ef72009-04-07 21:16:11 +00001646 CmdArgs.push_back("-fpascal-strings");
NAKAMURA Takumi125b4cb2011-02-17 08:50:50 +00001647
Fariborz Jahanianb466d012011-01-07 01:05:02 +00001648 if (Args.hasArg(options::OPT_mkernel) ||
1649 Args.hasArg(options::OPT_fapple_kext)) {
1650 if (!Args.hasArg(options::OPT_fcommon))
1651 CmdArgs.push_back("-fno-common");
1652 }
Daniel Dunbar48d1ef72009-04-07 21:16:11 +00001653 // -fcommon is default, only pass non-default.
Fariborz Jahanianb466d012011-01-07 01:05:02 +00001654 else if (!Args.hasFlag(options::OPT_fcommon, options::OPT_fno_common))
Daniel Dunbar48d1ef72009-04-07 21:16:11 +00001655 CmdArgs.push_back("-fno-common");
1656
Daniel Dunbar70d3c922009-04-15 02:37:43 +00001657 // -fsigned-bitfields is default, and clang doesn't yet support
Daniel Dunbar06205ca2010-10-15 22:30:42 +00001658 // -funsigned-bitfields.
Mike Stump1eb44332009-09-09 15:08:12 +00001659 if (!Args.hasFlag(options::OPT_fsigned_bitfields,
Daniel Dunbar70d3c922009-04-15 02:37:43 +00001660 options::OPT_funsigned_bitfields))
1661 D.Diag(clang::diag::warn_drv_clang_unsupported)
1662 << Args.getLastArg(options::OPT_funsigned_bitfields)->getAsString(Args);
1663
Daniel Dunbar06205ca2010-10-15 22:30:42 +00001664 // -fsigned-bitfields is default, and clang doesn't support -fno-for-scope.
1665 if (!Args.hasFlag(options::OPT_ffor_scope,
1666 options::OPT_fno_for_scope))
1667 D.Diag(clang::diag::err_drv_clang_unsupported)
1668 << Args.getLastArg(options::OPT_fno_for_scope)->getAsString(Args);
1669
Jeffrey Yasskin0ea22fd2010-06-08 04:56:20 +00001670 // -fcaret-diagnostics is default.
1671 if (!Args.hasFlag(options::OPT_fcaret_diagnostics,
1672 options::OPT_fno_caret_diagnostics, true))
1673 CmdArgs.push_back("-fno-caret-diagnostics");
1674
Daniel Dunbar49138fc2009-04-19 21:09:34 +00001675 // -fdiagnostics-fixit-info is default, only pass non-default.
Mike Stump1eb44332009-09-09 15:08:12 +00001676 if (!Args.hasFlag(options::OPT_fdiagnostics_fixit_info,
Daniel Dunbar49138fc2009-04-19 21:09:34 +00001677 options::OPT_fno_diagnostics_fixit_info))
1678 CmdArgs.push_back("-fno-diagnostics-fixit-info");
1679
Daniel Dunbar9e820ee2009-04-16 06:32:38 +00001680 // Enable -fdiagnostics-show-option by default.
Mike Stump1eb44332009-09-09 15:08:12 +00001681 if (Args.hasFlag(options::OPT_fdiagnostics_show_option,
Daniel Dunbar9e820ee2009-04-16 06:32:38 +00001682 options::OPT_fno_diagnostics_show_option))
1683 CmdArgs.push_back("-fdiagnostics-show-option");
Daniel Dunbar838be482009-11-04 06:24:57 +00001684
Chris Lattner6fbe8392010-05-04 21:55:25 +00001685 if (const Arg *A =
1686 Args.getLastArg(options::OPT_fdiagnostics_show_category_EQ)) {
1687 CmdArgs.push_back("-fdiagnostics-show-category");
1688 CmdArgs.push_back(A->getValue(Args));
1689 }
Daniel Dunbarca0e0542010-08-24 16:47:49 +00001690
Daniel Dunbar838be482009-11-04 06:24:57 +00001691 // Color diagnostics are the default, unless the terminal doesn't support
1692 // them.
1693 if (Args.hasFlag(options::OPT_fcolor_diagnostics,
Argyrios Kyrtzidisf765d762010-09-23 12:56:06 +00001694 options::OPT_fno_color_diagnostics,
1695 llvm::sys::Process::StandardErrHasColors()))
Daniel Dunbar838be482009-11-04 06:24:57 +00001696 CmdArgs.push_back("-fcolor-diagnostics");
1697
Daniel Dunbar75eb1d62009-06-08 21:13:54 +00001698 if (!Args.hasFlag(options::OPT_fshow_source_location,
1699 options::OPT_fno_show_source_location))
1700 CmdArgs.push_back("-fno-show-source-location");
Daniel Dunbar9e820ee2009-04-16 06:32:38 +00001701
Douglas Gregora0068fc2010-07-09 17:35:33 +00001702 if (!Args.hasFlag(options::OPT_fspell_checking,
1703 options::OPT_fno_spell_checking))
1704 CmdArgs.push_back("-fno-spell-checking");
Daniel Dunbarca0e0542010-08-24 16:47:49 +00001705
Daniel Dunbar25b26eb2010-10-18 22:49:46 +00001706
Daniel Dunbar16894392010-11-02 19:42:04 +00001707 // Silently ignore -fasm-blocks for now.
1708 (void) Args.hasFlag(options::OPT_fasm_blocks, options::OPT_fno_asm_blocks,
1709 false);
Daniel Dunbar25b26eb2010-10-18 22:49:46 +00001710
Jeffrey Yasskin5edbdcc2010-06-11 05:57:47 +00001711 if (Arg *A = Args.getLastArg(options::OPT_fshow_overloads_EQ))
1712 A->render(Args, CmdArgs);
1713
Daniel Dunbar7695fba2009-04-19 21:20:32 +00001714 // -fdollars-in-identifiers default varies depending on platform and
1715 // language; only pass if specified.
Mike Stump1eb44332009-09-09 15:08:12 +00001716 if (Arg *A = Args.getLastArg(options::OPT_fdollars_in_identifiers,
Daniel Dunbar7695fba2009-04-19 21:20:32 +00001717 options::OPT_fno_dollars_in_identifiers)) {
1718 if (A->getOption().matches(options::OPT_fdollars_in_identifiers))
Daniel Dunbar8663b182009-12-16 20:10:18 +00001719 CmdArgs.push_back("-fdollars-in-identifiers");
Daniel Dunbar7695fba2009-04-19 21:20:32 +00001720 else
Daniel Dunbar8663b182009-12-16 20:10:18 +00001721 CmdArgs.push_back("-fno-dollars-in-identifiers");
Daniel Dunbar7695fba2009-04-19 21:20:32 +00001722 }
1723
Daniel Dunbare027a4b2009-05-22 19:02:20 +00001724 // -funit-at-a-time is default, and we don't support -fno-unit-at-a-time for
1725 // practical purposes.
Mike Stump1eb44332009-09-09 15:08:12 +00001726 if (Arg *A = Args.getLastArg(options::OPT_funit_at_a_time,
Daniel Dunbare027a4b2009-05-22 19:02:20 +00001727 options::OPT_fno_unit_at_a_time)) {
1728 if (A->getOption().matches(options::OPT_fno_unit_at_a_time))
Edward O'Callaghanc3769102009-10-13 16:41:34 +00001729 D.Diag(clang::diag::warn_drv_clang_unsupported) << A->getAsString(Args);
Daniel Dunbare027a4b2009-05-22 19:02:20 +00001730 }
Eli Friedmanceb5c5b2009-07-14 21:58:17 +00001731
Daniel Dunbar2ba91572009-09-10 03:37:02 +00001732 // Default to -fno-builtin-str{cat,cpy} on Darwin for ARM.
Daniel Dunbarf84a4a42009-09-10 04:57:27 +00001733 //
Daniel Dunbar8ff5b282009-12-11 23:00:49 +00001734 // FIXME: This is disabled until clang -cc1 supports -fno-builtin-foo. PR4941.
Daniel Dunbarf84a4a42009-09-10 04:57:27 +00001735#if 0
Daniel Dunbar2ba91572009-09-10 03:37:02 +00001736 if (getToolChain().getTriple().getOS() == llvm::Triple::Darwin &&
1737 (getToolChain().getTriple().getArch() == llvm::Triple::arm ||
1738 getToolChain().getTriple().getArch() == llvm::Triple::thumb)) {
1739 if (!Args.hasArg(options::OPT_fbuiltin_strcat))
1740 CmdArgs.push_back("-fno-builtin-strcat");
1741 if (!Args.hasArg(options::OPT_fbuiltin_strcpy))
1742 CmdArgs.push_back("-fno-builtin-strcpy");
1743 }
Daniel Dunbarf84a4a42009-09-10 04:57:27 +00001744#endif
Daniel Dunbar2ba91572009-09-10 03:37:02 +00001745
Mike Stump1eb44332009-09-09 15:08:12 +00001746 if (Arg *A = Args.getLastArg(options::OPT_traditional,
Eli Friedmanceb5c5b2009-07-14 21:58:17 +00001747 options::OPT_traditional_cpp))
1748 D.Diag(clang::diag::err_drv_clang_unsupported) << A->getAsString(Args);
1749
Daniel Dunbar1d460332009-03-18 10:01:51 +00001750 Args.AddLastArg(CmdArgs, options::OPT_dM);
Chris Lattnerd82df3a2009-04-12 01:56:53 +00001751 Args.AddLastArg(CmdArgs, options::OPT_dD);
Daniel Dunbar1d460332009-03-18 10:01:51 +00001752
Daniel Dunbar3f87fb02010-04-15 06:09:03 +00001753 // Forward -Xclang arguments to -cc1, and -mllvm arguments to the LLVM option
1754 // parser.
Daniel Dunbar1d460332009-03-18 10:01:51 +00001755 Args.AddAllArgValues(CmdArgs, options::OPT_Xclang);
Daniel Dunbar3f87fb02010-04-15 06:09:03 +00001756 for (arg_iterator it = Args.filtered_begin(options::OPT_mllvm),
1757 ie = Args.filtered_end(); it != ie; ++it) {
Daniel Dunbar7e4953e2010-06-11 22:00:13 +00001758 (*it)->claim();
Daniel Dunbarfb36d212010-04-17 06:10:00 +00001759
Daniel Dunbar3f87fb02010-04-15 06:09:03 +00001760 // We translate this by hand to the -cc1 argument, since nightly test uses
1761 // it and developers have been trained to spell it with -mllvm.
Daniel Dunbar7e4953e2010-06-11 22:00:13 +00001762 if (llvm::StringRef((*it)->getValue(Args, 0)) == "-disable-llvm-optzns")
Daniel Dunbar3f87fb02010-04-15 06:09:03 +00001763 CmdArgs.push_back("-disable-llvm-optzns");
1764 else
Daniel Dunbar7e4953e2010-06-11 22:00:13 +00001765 (*it)->render(Args, CmdArgs);
Daniel Dunbar3f87fb02010-04-15 06:09:03 +00001766 }
Daniel Dunbar1d460332009-03-18 10:01:51 +00001767
Daniel Dunbarcd8e4c42009-03-30 06:36:42 +00001768 if (Output.getType() == types::TY_Dependencies) {
1769 // Handled with other dependency code.
Daniel Dunbar115a7922009-03-19 07:29:38 +00001770 } else if (Output.isFilename()) {
Daniel Dunbarb488c1d2009-03-18 08:07:30 +00001771 CmdArgs.push_back("-o");
Daniel Dunbar115a7922009-03-19 07:29:38 +00001772 CmdArgs.push_back(Output.getFilename());
1773 } else {
1774 assert(Output.isNothing() && "Invalid output.");
Daniel Dunbarb488c1d2009-03-18 08:07:30 +00001775 }
1776
Daniel Dunbar1d460332009-03-18 10:01:51 +00001777 for (InputInfoList::const_iterator
1778 it = Inputs.begin(), ie = Inputs.end(); it != ie; ++it) {
1779 const InputInfo &II = *it;
1780 CmdArgs.push_back("-x");
1781 CmdArgs.push_back(types::getTypeName(II.getType()));
Daniel Dunbar7c1e4652010-08-02 02:38:21 +00001782 if (II.isFilename())
Daniel Dunbar115a7922009-03-19 07:29:38 +00001783 CmdArgs.push_back(II.getFilename());
Daniel Dunbar1d460332009-03-18 10:01:51 +00001784 else
Daniel Dunbar115a7922009-03-19 07:29:38 +00001785 II.getInputArg().renderAsInput(Args, CmdArgs);
Daniel Dunbar1d460332009-03-18 10:01:51 +00001786 }
Daniel Dunbarc21c4852009-04-08 23:54:23 +00001787
Chris Lattnere6113de2009-11-03 19:50:27 +00001788 Args.AddAllArgs(CmdArgs, options::OPT_undef);
1789
Daniel Dunbara001c1c2010-07-18 21:16:15 +00001790 const char *Exec = getToolChain().getDriver().getClangProgramPath();
Daniel Dunbarf2d8b9f2009-12-18 02:43:17 +00001791
1792 // Optionally embed the -cc1 level arguments into the debug info, for build
1793 // analysis.
1794 if (getToolChain().UseDwarfDebugFlags()) {
Daniel Dunbar6e900472010-06-04 18:47:06 +00001795 ArgStringList OriginalArgs;
1796 for (ArgList::const_iterator it = Args.begin(),
1797 ie = Args.end(); it != ie; ++it)
1798 (*it)->render(Args, OriginalArgs);
Daniel Dunbarca0e0542010-08-24 16:47:49 +00001799
Daniel Dunbarf2d8b9f2009-12-18 02:43:17 +00001800 llvm::SmallString<256> Flags;
1801 Flags += Exec;
Daniel Dunbar6e900472010-06-04 18:47:06 +00001802 for (unsigned i = 0, e = OriginalArgs.size(); i != e; ++i) {
Daniel Dunbarf2d8b9f2009-12-18 02:43:17 +00001803 Flags += " ";
Daniel Dunbar6e900472010-06-04 18:47:06 +00001804 Flags += OriginalArgs[i];
Daniel Dunbarf2d8b9f2009-12-18 02:43:17 +00001805 }
1806 CmdArgs.push_back("-dwarf-debug-flags");
1807 CmdArgs.push_back(Args.MakeArgString(Flags.str()));
1808 }
1809
Daniel Dunbar2fe238e2010-08-02 02:38:28 +00001810 C.addCommand(new Command(JA, *this, Exec, CmdArgs));
Daniel Dunbara880db02009-03-23 19:03:36 +00001811
Roman Divackybe4c8702011-02-10 16:52:03 +00001812 if (Arg *A = Args.getLastArg(options::OPT_pg))
1813 if (Args.hasArg(options::OPT_fomit_frame_pointer))
1814 D.Diag(clang::diag::err_drv_argument_not_allowed_with)
1815 << "-fomit-frame-pointer" << A->getAsString(Args);
Michael J. Spencer20249a12010-10-21 03:16:25 +00001816
Daniel Dunbar68fb4692009-04-03 20:51:31 +00001817 // Claim some arguments which clang supports automatically.
1818
Daniel Dunbarf4046862010-04-15 06:18:42 +00001819 // -fpch-preprocess is used with gcc to add a special marker in the output to
1820 // include the PCH file. Clang's PTH solution is completely transparent, so we
1821 // do not need to deal with it at all.
Daniel Dunbar68fb4692009-04-03 20:51:31 +00001822 Args.ClaimAllArgs(options::OPT_fpch_preprocess);
Daniel Dunbarc21c4852009-04-08 23:54:23 +00001823
Daniel Dunbara880db02009-03-23 19:03:36 +00001824 // Claim some arguments which clang doesn't support, but we don't
1825 // care to warn the user about.
Daniel Dunbarcdd96862009-11-25 11:53:23 +00001826 Args.ClaimAllArgs(options::OPT_clang_ignored_f_Group);
1827 Args.ClaimAllArgs(options::OPT_clang_ignored_m_Group);
Rafael Espindola035ff0c2011-02-28 23:29:45 +00001828
1829 Args.ClaimAllArgs(options::OPT_use_gold_plugin);
Daniel Dunbar47ac7d22009-03-18 06:00:36 +00001830}
1831
Daniel Dunbar20a9aa52010-05-20 21:30:13 +00001832void ClangAs::ConstructJob(Compilation &C, const JobAction &JA,
Daniel Dunbar20a9aa52010-05-20 21:30:13 +00001833 const InputInfo &Output,
1834 const InputInfoList &Inputs,
1835 const ArgList &Args,
1836 const char *LinkingOutput) const {
Daniel Dunbar20a9aa52010-05-20 21:30:13 +00001837 ArgStringList CmdArgs;
1838
1839 assert(Inputs.size() == 1 && "Unexpected number of inputs.");
1840 const InputInfo &Input = Inputs[0];
1841
Rafael Espindoladbe80d92010-11-17 22:13:25 +00001842 // Don't warn about "clang -w -c foo.s"
1843 Args.ClaimAllArgs(options::OPT_w);
1844
Daniel Dunbar20a9aa52010-05-20 21:30:13 +00001845 // Invoke ourselves in -cc1as mode.
1846 //
1847 // FIXME: Implement custom jobs for internal actions.
1848 CmdArgs.push_back("-cc1as");
1849
1850 // Add the "effective" target triple.
1851 CmdArgs.push_back("-triple");
Daniel Dunbar00577ad2010-08-23 22:35:37 +00001852 std::string TripleStr = getToolChain().ComputeEffectiveClangTriple(Args);
Daniel Dunbar20a9aa52010-05-20 21:30:13 +00001853 CmdArgs.push_back(Args.MakeArgString(TripleStr));
1854
1855 // Set the output mode, we currently only expect to be used as a real
1856 // assembler.
1857 CmdArgs.push_back("-filetype");
1858 CmdArgs.push_back("obj");
1859
Daniel Dunbar99298002010-05-27 06:18:05 +00001860 // At -O0, we use -mrelax-all by default.
1861 bool IsOpt = false;
1862 if (Arg *A = Args.getLastArg(options::OPT_O_Group))
1863 IsOpt = !A->getOption().matches(options::OPT_O0);
1864 if (Args.hasFlag(options::OPT_mrelax_all,
1865 options::OPT_mno_relax_all,
1866 !IsOpt))
Daniel Dunbar469d40e2010-05-28 16:43:21 +00001867 CmdArgs.push_back("-relax-all");
Daniel Dunbar99298002010-05-27 06:18:05 +00001868
Daniel Dunbar20a9aa52010-05-20 21:30:13 +00001869 // FIXME: Add -force_cpusubtype_ALL support, once we have it.
1870
1871 // FIXME: Add -g support, once we have it.
1872
1873 // FIXME: Add -static support, once we have it.
1874
1875 Args.AddAllArgValues(CmdArgs, options::OPT_Wa_COMMA,
1876 options::OPT_Xassembler);
1877
1878 assert(Output.isFilename() && "Unexpected lipo output.");
1879 CmdArgs.push_back("-o");
1880 CmdArgs.push_back(Output.getFilename());
1881
Daniel Dunbar7c1e4652010-08-02 02:38:21 +00001882 assert(Input.isFilename() && "Invalid input.");
1883 CmdArgs.push_back(Input.getFilename());
Daniel Dunbar20a9aa52010-05-20 21:30:13 +00001884
Daniel Dunbara001c1c2010-07-18 21:16:15 +00001885 const char *Exec = getToolChain().getDriver().getClangProgramPath();
Daniel Dunbar2fe238e2010-08-02 02:38:28 +00001886 C.addCommand(new Command(JA, *this, Exec, CmdArgs));
Daniel Dunbar20a9aa52010-05-20 21:30:13 +00001887}
1888
Daniel Dunbarb488c1d2009-03-18 08:07:30 +00001889void gcc::Common::ConstructJob(Compilation &C, const JobAction &JA,
Daniel Dunbarb488c1d2009-03-18 08:07:30 +00001890 const InputInfo &Output,
1891 const InputInfoList &Inputs,
Daniel Dunbar1d460332009-03-18 10:01:51 +00001892 const ArgList &Args,
Daniel Dunbarb488c1d2009-03-18 08:07:30 +00001893 const char *LinkingOutput) const {
Daniel Dunbaree788e72009-12-21 18:54:17 +00001894 const Driver &D = getToolChain().getDriver();
Daniel Dunbarb488c1d2009-03-18 08:07:30 +00001895 ArgStringList CmdArgs;
Daniel Dunbar47ac7d22009-03-18 06:00:36 +00001896
Daniel Dunbarc21c4852009-04-08 23:54:23 +00001897 for (ArgList::const_iterator
Daniel Dunbar1d460332009-03-18 10:01:51 +00001898 it = Args.begin(), ie = Args.end(); it != ie; ++it) {
Daniel Dunbarb488c1d2009-03-18 08:07:30 +00001899 Arg *A = *it;
Daniel Dunbar75877192009-03-19 07:55:12 +00001900 if (A->getOption().hasForwardToGCC()) {
Daniel Dunbar2dffe2d2010-08-03 16:14:14 +00001901 // Don't forward any -g arguments to assembly steps.
1902 if (isa<AssembleJobAction>(JA) &&
1903 A->getOption().matches(options::OPT_g_Group))
1904 continue;
1905
Daniel Dunbar75877192009-03-19 07:55:12 +00001906 // It is unfortunate that we have to claim here, as this means
1907 // we will basically never report anything interesting for
Daniel Dunbar6ecc7a92009-05-02 21:41:52 +00001908 // platforms using a generic gcc, even if we are just using gcc
1909 // to get to the assembler.
Daniel Dunbar75877192009-03-19 07:55:12 +00001910 A->claim();
Daniel Dunbar1d460332009-03-18 10:01:51 +00001911 A->render(Args, CmdArgs);
Daniel Dunbar75877192009-03-19 07:55:12 +00001912 }
Daniel Dunbarb488c1d2009-03-18 08:07:30 +00001913 }
Daniel Dunbarc21c4852009-04-08 23:54:23 +00001914
Daniel Dunbar82b51cc2010-01-25 22:35:08 +00001915 RenderExtraToolArgs(JA, CmdArgs);
Daniel Dunbarb488c1d2009-03-18 08:07:30 +00001916
1917 // If using a driver driver, force the arch.
Daniel Dunbar7cfe31a2009-05-22 02:21:04 +00001918 const std::string &Arch = getToolChain().getArchName();
Daniel Dunbar1bda4342009-12-23 00:46:38 +00001919 if (getToolChain().getTriple().getOS() == llvm::Triple::Darwin) {
Daniel Dunbarb488c1d2009-03-18 08:07:30 +00001920 CmdArgs.push_back("-arch");
Daniel Dunbarbf54a062009-04-01 20:33:11 +00001921
1922 // FIXME: Remove these special cases.
Daniel Dunbar7cfe31a2009-05-22 02:21:04 +00001923 if (Arch == "powerpc")
1924 CmdArgs.push_back("ppc");
1925 else if (Arch == "powerpc64")
1926 CmdArgs.push_back("ppc64");
1927 else
Daniel Dunbar88137642009-09-09 22:32:48 +00001928 CmdArgs.push_back(Args.MakeArgString(Arch));
Daniel Dunbarb488c1d2009-03-18 08:07:30 +00001929 }
1930
Daniel Dunbar6ecc7a92009-05-02 21:41:52 +00001931 // Try to force gcc to match the tool chain we want, if we recognize
1932 // the arch.
Daniel Dunbar7cfe31a2009-05-22 02:21:04 +00001933 //
1934 // FIXME: The triple class should directly provide the information we want
1935 // here.
1936 if (Arch == "i386" || Arch == "powerpc")
Daniel Dunbar6ecc7a92009-05-02 21:41:52 +00001937 CmdArgs.push_back("-m32");
Daniel Dunbar7cfe31a2009-05-22 02:21:04 +00001938 else if (Arch == "x86_64" || Arch == "powerpc64")
Daniel Dunbar6ecc7a92009-05-02 21:41:52 +00001939 CmdArgs.push_back("-m64");
1940
Daniel Dunbar7c1e4652010-08-02 02:38:21 +00001941 if (Output.isFilename()) {
Daniel Dunbarb488c1d2009-03-18 08:07:30 +00001942 CmdArgs.push_back("-o");
Daniel Dunbar115a7922009-03-19 07:29:38 +00001943 CmdArgs.push_back(Output.getFilename());
1944 } else {
1945 assert(Output.isNothing() && "Unexpected output");
Daniel Dunbarb488c1d2009-03-18 08:07:30 +00001946 CmdArgs.push_back("-fsyntax-only");
Daniel Dunbar115a7922009-03-19 07:29:38 +00001947 }
Daniel Dunbarb488c1d2009-03-18 08:07:30 +00001948
1949
1950 // Only pass -x if gcc will understand it; otherwise hope gcc
1951 // understands the suffix correctly. The main use case this would go
1952 // wrong in is for linker inputs if they happened to have an odd
1953 // suffix; really the only way to get this to happen is a command
1954 // like '-x foobar a.c' which will treat a.c like a linker input.
1955 //
1956 // FIXME: For the linker case specifically, can we safely convert
1957 // inputs into '-Wl,' options?
1958 for (InputInfoList::const_iterator
1959 it = Inputs.begin(), ie = Inputs.end(); it != ie; ++it) {
1960 const InputInfo &II = *it;
Daniel Dunbara8304f62009-05-02 20:14:53 +00001961
Daniel Dunbar5915fbf2009-09-01 16:57:46 +00001962 // Don't try to pass LLVM or AST inputs to a generic gcc.
Daniel Dunbar6c6424b2010-06-07 23:28:45 +00001963 if (II.getType() == types::TY_LLVM_IR || II.getType() == types::TY_LTO_IR ||
1964 II.getType() == types::TY_LLVM_BC || II.getType() == types::TY_LTO_BC)
Daniel Dunbara8304f62009-05-02 20:14:53 +00001965 D.Diag(clang::diag::err_drv_no_linker_llvm_support)
Daniel Dunbar88137642009-09-09 22:32:48 +00001966 << getToolChain().getTripleString();
Daniel Dunbar5915fbf2009-09-01 16:57:46 +00001967 else if (II.getType() == types::TY_AST)
1968 D.Diag(clang::diag::err_drv_no_ast_support)
Daniel Dunbar88137642009-09-09 22:32:48 +00001969 << getToolChain().getTripleString();
Daniel Dunbara8304f62009-05-02 20:14:53 +00001970
Daniel Dunbarb488c1d2009-03-18 08:07:30 +00001971 if (types::canTypeBeUserSpecified(II.getType())) {
1972 CmdArgs.push_back("-x");
1973 CmdArgs.push_back(types::getTypeName(II.getType()));
1974 }
1975
Daniel Dunbar7c1e4652010-08-02 02:38:21 +00001976 if (II.isFilename())
Daniel Dunbar115a7922009-03-19 07:29:38 +00001977 CmdArgs.push_back(II.getFilename());
Daniel Dunbar48f99942010-09-25 18:10:05 +00001978 else {
1979 const Arg &A = II.getInputArg();
1980
1981 // Reverse translate some rewritten options.
1982 if (A.getOption().matches(options::OPT_Z_reserved_lib_stdcxx)) {
1983 CmdArgs.push_back("-lstdc++");
1984 continue;
1985 }
1986
Daniel Dunbar115a7922009-03-19 07:29:38 +00001987 // Don't render as input, we need gcc to do the translations.
Daniel Dunbar48f99942010-09-25 18:10:05 +00001988 A.render(Args, CmdArgs);
1989 }
Daniel Dunbarb488c1d2009-03-18 08:07:30 +00001990 }
1991
Rafael Espindola79e9e9d2010-09-06 02:36:23 +00001992 const char *GCCName = getToolChain().getDriver().getCCCGenericGCCName().c_str();
Daniel Dunbarc21c4852009-04-08 23:54:23 +00001993 const char *Exec =
Daniel Dunbar4a7e8892010-07-14 18:46:23 +00001994 Args.MakeArgString(getToolChain().GetProgramPath(GCCName));
Daniel Dunbar2fe238e2010-08-02 02:38:28 +00001995 C.addCommand(new Command(JA, *this, Exec, CmdArgs));
Daniel Dunbar47ac7d22009-03-18 06:00:36 +00001996}
1997
Daniel Dunbar82b51cc2010-01-25 22:35:08 +00001998void gcc::Preprocess::RenderExtraToolArgs(const JobAction &JA,
1999 ArgStringList &CmdArgs) const {
Daniel Dunbarb488c1d2009-03-18 08:07:30 +00002000 CmdArgs.push_back("-E");
Daniel Dunbar47ac7d22009-03-18 06:00:36 +00002001}
2002
Daniel Dunbar82b51cc2010-01-25 22:35:08 +00002003void gcc::Precompile::RenderExtraToolArgs(const JobAction &JA,
2004 ArgStringList &CmdArgs) const {
Daniel Dunbarb488c1d2009-03-18 08:07:30 +00002005 // The type is good enough.
Daniel Dunbar47ac7d22009-03-18 06:00:36 +00002006}
2007
Daniel Dunbar82b51cc2010-01-25 22:35:08 +00002008void gcc::Compile::RenderExtraToolArgs(const JobAction &JA,
2009 ArgStringList &CmdArgs) const {
Daniel Dunbar64952502010-02-11 03:16:21 +00002010 const Driver &D = getToolChain().getDriver();
2011
Daniel Dunbar82b51cc2010-01-25 22:35:08 +00002012 // If -flto, etc. are present then make sure not to force assembly output.
Daniel Dunbar6c6424b2010-06-07 23:28:45 +00002013 if (JA.getType() == types::TY_LLVM_IR || JA.getType() == types::TY_LTO_IR ||
2014 JA.getType() == types::TY_LLVM_BC || JA.getType() == types::TY_LTO_BC)
Daniel Dunbar82b51cc2010-01-25 22:35:08 +00002015 CmdArgs.push_back("-c");
Daniel Dunbar64952502010-02-11 03:16:21 +00002016 else {
2017 if (JA.getType() != types::TY_PP_Asm)
2018 D.Diag(clang::diag::err_drv_invalid_gcc_output_type)
2019 << getTypeName(JA.getType());
Michael J. Spencer20249a12010-10-21 03:16:25 +00002020
Daniel Dunbar82b51cc2010-01-25 22:35:08 +00002021 CmdArgs.push_back("-S");
Daniel Dunbar64952502010-02-11 03:16:21 +00002022 }
Daniel Dunbar47ac7d22009-03-18 06:00:36 +00002023}
2024
Daniel Dunbar82b51cc2010-01-25 22:35:08 +00002025void gcc::Assemble::RenderExtraToolArgs(const JobAction &JA,
2026 ArgStringList &CmdArgs) const {
Daniel Dunbarb488c1d2009-03-18 08:07:30 +00002027 CmdArgs.push_back("-c");
Daniel Dunbar47ac7d22009-03-18 06:00:36 +00002028}
Daniel Dunbarb488c1d2009-03-18 08:07:30 +00002029
Daniel Dunbar82b51cc2010-01-25 22:35:08 +00002030void gcc::Link::RenderExtraToolArgs(const JobAction &JA,
2031 ArgStringList &CmdArgs) const {
Daniel Dunbarb488c1d2009-03-18 08:07:30 +00002032 // The types are (hopefully) good enough.
2033}
2034
Daniel Dunbar40f12652009-03-29 17:08:39 +00002035const char *darwin::CC1::getCC1Name(types::ID Type) const {
2036 switch (Type) {
2037 default:
2038 assert(0 && "Unexpected type for Darwin CC1 tool.");
2039 case types::TY_Asm:
2040 case types::TY_C: case types::TY_CHeader:
2041 case types::TY_PP_C: case types::TY_PP_CHeader:
2042 return "cc1";
2043 case types::TY_ObjC: case types::TY_ObjCHeader:
2044 case types::TY_PP_ObjC: case types::TY_PP_ObjCHeader:
2045 return "cc1obj";
2046 case types::TY_CXX: case types::TY_CXXHeader:
2047 case types::TY_PP_CXX: case types::TY_PP_CXXHeader:
2048 return "cc1plus";
2049 case types::TY_ObjCXX: case types::TY_ObjCXXHeader:
2050 case types::TY_PP_ObjCXX: case types::TY_PP_ObjCXXHeader:
2051 return "cc1objplus";
2052 }
2053}
2054
Daniel Dunbarc21c4852009-04-08 23:54:23 +00002055const char *darwin::CC1::getBaseInputName(const ArgList &Args,
Daniel Dunbara5a7bd02009-03-30 00:34:04 +00002056 const InputInfoList &Inputs) {
Michael J. Spencer472ccff2010-12-18 00:19:12 +00002057 return Args.MakeArgString(
2058 llvm::sys::path::filename(Inputs[0].getBaseInput()));
Daniel Dunbara3ec60e2009-03-29 18:40:18 +00002059}
2060
Daniel Dunbarc21c4852009-04-08 23:54:23 +00002061const char *darwin::CC1::getBaseInputStem(const ArgList &Args,
Daniel Dunbara5a7bd02009-03-30 00:34:04 +00002062 const InputInfoList &Inputs) {
Daniel Dunbara3ec60e2009-03-29 18:40:18 +00002063 const char *Str = getBaseInputName(Args, Inputs);
2064
Chris Lattner657ca662011-01-16 08:14:11 +00002065 if (const char *End = strrchr(Str, '.'))
Daniel Dunbar88137642009-09-09 22:32:48 +00002066 return Args.MakeArgString(std::string(Str, End));
Daniel Dunbara3ec60e2009-03-29 18:40:18 +00002067
2068 return Str;
2069}
2070
2071const char *
Daniel Dunbarc21c4852009-04-08 23:54:23 +00002072darwin::CC1::getDependencyFileName(const ArgList &Args,
Daniel Dunbara5a7bd02009-03-30 00:34:04 +00002073 const InputInfoList &Inputs) {
Daniel Dunbara3ec60e2009-03-29 18:40:18 +00002074 // FIXME: Think about this more.
2075 std::string Res;
2076
2077 if (Arg *OutputOpt = Args.getLastArg(options::OPT_o)) {
2078 std::string Str(OutputOpt->getValue(Args));
Daniel Dunbarc21c4852009-04-08 23:54:23 +00002079
Daniel Dunbara3ec60e2009-03-29 18:40:18 +00002080 Res = Str.substr(0, Str.rfind('.'));
2081 } else
Daniel Dunbara5a7bd02009-03-30 00:34:04 +00002082 Res = darwin::CC1::getBaseInputStem(Args, Inputs);
Daniel Dunbara3ec60e2009-03-29 18:40:18 +00002083
Daniel Dunbar88137642009-09-09 22:32:48 +00002084 return Args.MakeArgString(Res + ".d");
Daniel Dunbara3ec60e2009-03-29 18:40:18 +00002085}
2086
Daniel Dunbarc21c4852009-04-08 23:54:23 +00002087void darwin::CC1::AddCC1Args(const ArgList &Args,
Daniel Dunbara3ec60e2009-03-29 18:40:18 +00002088 ArgStringList &CmdArgs) const {
Daniel Dunbaree788e72009-12-21 18:54:17 +00002089 const Driver &D = getToolChain().getDriver();
Daniel Dunbare2fd6642009-09-10 01:21:12 +00002090
2091 CheckCodeGenerationOptions(D, Args);
2092
Daniel Dunbara3ec60e2009-03-29 18:40:18 +00002093 // Derived from cc1 spec.
Daniel Dunbara3ec60e2009-03-29 18:40:18 +00002094 if (!Args.hasArg(options::OPT_mkernel) && !Args.hasArg(options::OPT_static) &&
2095 !Args.hasArg(options::OPT_mdynamic_no_pic))
2096 CmdArgs.push_back("-fPIC");
2097
Daniel Dunbar2ba91572009-09-10 03:37:02 +00002098 if (getToolChain().getTriple().getArch() == llvm::Triple::arm ||
2099 getToolChain().getTriple().getArch() == llvm::Triple::thumb) {
2100 if (!Args.hasArg(options::OPT_fbuiltin_strcat))
2101 CmdArgs.push_back("-fno-builtin-strcat");
2102 if (!Args.hasArg(options::OPT_fbuiltin_strcpy))
2103 CmdArgs.push_back("-fno-builtin-strcpy");
2104 }
2105
Daniel Dunbara3ec60e2009-03-29 18:40:18 +00002106 // gcc has some code here to deal with when no -mmacosx-version-min
2107 // and no -miphoneos-version-min is present, but this never happens
2108 // due to tool chain specific argument translation.
2109
Daniel Dunbara3ec60e2009-03-29 18:40:18 +00002110 if (Args.hasArg(options::OPT_g_Flag) &&
2111 !Args.hasArg(options::OPT_fno_eliminate_unused_debug_symbols))
2112 CmdArgs.push_back("-feliminate-unused-debug-symbols");
2113}
2114
2115void darwin::CC1::AddCC1OptionsArgs(const ArgList &Args, ArgStringList &CmdArgs,
2116 const InputInfoList &Inputs,
2117 const ArgStringList &OutputArgs) const {
Daniel Dunbaree788e72009-12-21 18:54:17 +00002118 const Driver &D = getToolChain().getDriver();
Daniel Dunbara3ec60e2009-03-29 18:40:18 +00002119
2120 // Derived from cc1_options spec.
2121 if (Args.hasArg(options::OPT_fast) ||
2122 Args.hasArg(options::OPT_fastf) ||
2123 Args.hasArg(options::OPT_fastcp))
2124 CmdArgs.push_back("-O3");
Daniel Dunbarc21c4852009-04-08 23:54:23 +00002125
Daniel Dunbara3ec60e2009-03-29 18:40:18 +00002126 if (Arg *A = Args.getLastArg(options::OPT_pg))
2127 if (Args.hasArg(options::OPT_fomit_frame_pointer))
2128 D.Diag(clang::diag::err_drv_argument_not_allowed_with)
2129 << A->getAsString(Args) << "-fomit-frame-pointer";
2130
2131 AddCC1Args(Args, CmdArgs);
2132
2133 if (!Args.hasArg(options::OPT_Q))
2134 CmdArgs.push_back("-quiet");
2135
2136 CmdArgs.push_back("-dumpbase");
Daniel Dunbara5a7bd02009-03-30 00:34:04 +00002137 CmdArgs.push_back(darwin::CC1::getBaseInputName(Args, Inputs));
Daniel Dunbara3ec60e2009-03-29 18:40:18 +00002138
2139 Args.AddAllArgs(CmdArgs, options::OPT_d_Group);
2140
2141 Args.AddAllArgs(CmdArgs, options::OPT_m_Group);
2142 Args.AddAllArgs(CmdArgs, options::OPT_a_Group);
2143
2144 // FIXME: The goal is to use the user provided -o if that is our
2145 // final output, otherwise to drive from the original input
2146 // name. Find a clean way to go about this.
2147 if ((Args.hasArg(options::OPT_c) || Args.hasArg(options::OPT_S)) &&
2148 Args.hasArg(options::OPT_o)) {
2149 Arg *OutputOpt = Args.getLastArg(options::OPT_o);
2150 CmdArgs.push_back("-auxbase-strip");
2151 CmdArgs.push_back(OutputOpt->getValue(Args));
2152 } else {
2153 CmdArgs.push_back("-auxbase");
Daniel Dunbara5a7bd02009-03-30 00:34:04 +00002154 CmdArgs.push_back(darwin::CC1::getBaseInputStem(Args, Inputs));
Daniel Dunbara3ec60e2009-03-29 18:40:18 +00002155 }
2156
2157 Args.AddAllArgs(CmdArgs, options::OPT_g_Group);
2158
2159 Args.AddAllArgs(CmdArgs, options::OPT_O);
2160 // FIXME: -Wall is getting some special treatment. Investigate.
2161 Args.AddAllArgs(CmdArgs, options::OPT_W_Group, options::OPT_pedantic_Group);
2162 Args.AddLastArg(CmdArgs, options::OPT_w);
Daniel Dunbarc21c4852009-04-08 23:54:23 +00002163 Args.AddAllArgs(CmdArgs, options::OPT_std_EQ, options::OPT_ansi,
Daniel Dunbara3ec60e2009-03-29 18:40:18 +00002164 options::OPT_trigraphs);
Daniel Dunbara3ff2022009-04-26 01:10:38 +00002165 if (!Args.getLastArg(options::OPT_std_EQ, options::OPT_ansi)) {
2166 // Honor -std-default.
2167 Args.AddAllArgsTranslated(CmdArgs, options::OPT_std_default_EQ,
2168 "-std=", /*Joined=*/true);
2169 }
2170
Daniel Dunbara3ec60e2009-03-29 18:40:18 +00002171 if (Args.hasArg(options::OPT_v))
2172 CmdArgs.push_back("-version");
2173 if (Args.hasArg(options::OPT_pg))
2174 CmdArgs.push_back("-p");
2175 Args.AddLastArg(CmdArgs, options::OPT_p);
Daniel Dunbarc21c4852009-04-08 23:54:23 +00002176
Daniel Dunbara3ec60e2009-03-29 18:40:18 +00002177 // The driver treats -fsyntax-only specially.
Daniel Dunbar2ba91572009-09-10 03:37:02 +00002178 if (getToolChain().getTriple().getArch() == llvm::Triple::arm ||
2179 getToolChain().getTriple().getArch() == llvm::Triple::thumb) {
2180 // Removes -fbuiltin-str{cat,cpy}; these aren't recognized by cc1 but are
2181 // used to inhibit the default -fno-builtin-str{cat,cpy}.
2182 //
2183 // FIXME: Should we grow a better way to deal with "removing" args?
Daniel Dunbarcdd96862009-11-25 11:53:23 +00002184 for (arg_iterator it = Args.filtered_begin(options::OPT_f_Group,
2185 options::OPT_fsyntax_only),
2186 ie = Args.filtered_end(); it != ie; ++it) {
Daniel Dunbar7e4953e2010-06-11 22:00:13 +00002187 if (!(*it)->getOption().matches(options::OPT_fbuiltin_strcat) &&
2188 !(*it)->getOption().matches(options::OPT_fbuiltin_strcpy)) {
2189 (*it)->claim();
2190 (*it)->render(Args, CmdArgs);
Daniel Dunbar2ba91572009-09-10 03:37:02 +00002191 }
2192 }
2193 } else
2194 Args.AddAllArgs(CmdArgs, options::OPT_f_Group, options::OPT_fsyntax_only);
Daniel Dunbarc21c4852009-04-08 23:54:23 +00002195
Daniel Dunbara3ec60e2009-03-29 18:40:18 +00002196 Args.AddAllArgs(CmdArgs, options::OPT_undef);
2197 if (Args.hasArg(options::OPT_Qn))
2198 CmdArgs.push_back("-fno-ident");
Daniel Dunbarc21c4852009-04-08 23:54:23 +00002199
Daniel Dunbara3ec60e2009-03-29 18:40:18 +00002200 // FIXME: This isn't correct.
2201 //Args.AddLastArg(CmdArgs, options::OPT__help)
2202 //Args.AddLastArg(CmdArgs, options::OPT__targetHelp)
2203
2204 CmdArgs.append(OutputArgs.begin(), OutputArgs.end());
2205
2206 // FIXME: Still don't get what is happening here. Investigate.
2207 Args.AddAllArgs(CmdArgs, options::OPT__param);
2208
2209 if (Args.hasArg(options::OPT_fmudflap) ||
2210 Args.hasArg(options::OPT_fmudflapth)) {
2211 CmdArgs.push_back("-fno-builtin");
2212 CmdArgs.push_back("-fno-merge-constants");
2213 }
Daniel Dunbarc21c4852009-04-08 23:54:23 +00002214
Daniel Dunbara3ec60e2009-03-29 18:40:18 +00002215 if (Args.hasArg(options::OPT_coverage)) {
2216 CmdArgs.push_back("-fprofile-arcs");
2217 CmdArgs.push_back("-ftest-coverage");
2218 }
2219
2220 if (types::isCXX(Inputs[0].getType()))
2221 CmdArgs.push_back("-D__private_extern__=extern");
2222}
2223
2224void darwin::CC1::AddCPPOptionsArgs(const ArgList &Args, ArgStringList &CmdArgs,
2225 const InputInfoList &Inputs,
2226 const ArgStringList &OutputArgs) const {
2227 // Derived from cpp_options
2228 AddCPPUniqueOptionsArgs(Args, CmdArgs, Inputs);
Daniel Dunbarc21c4852009-04-08 23:54:23 +00002229
Daniel Dunbara3ec60e2009-03-29 18:40:18 +00002230 CmdArgs.append(OutputArgs.begin(), OutputArgs.end());
2231
2232 AddCC1Args(Args, CmdArgs);
2233
2234 // NOTE: The code below has some commonality with cpp_options, but
2235 // in classic gcc style ends up sending things in different
2236 // orders. This may be a good merge candidate once we drop pedantic
2237 // compatibility.
2238
2239 Args.AddAllArgs(CmdArgs, options::OPT_m_Group);
Daniel Dunbarc21c4852009-04-08 23:54:23 +00002240 Args.AddAllArgs(CmdArgs, options::OPT_std_EQ, options::OPT_ansi,
Daniel Dunbara3ec60e2009-03-29 18:40:18 +00002241 options::OPT_trigraphs);
Daniel Dunbara3ff2022009-04-26 01:10:38 +00002242 if (!Args.getLastArg(options::OPT_std_EQ, options::OPT_ansi)) {
2243 // Honor -std-default.
2244 Args.AddAllArgsTranslated(CmdArgs, options::OPT_std_default_EQ,
2245 "-std=", /*Joined=*/true);
2246 }
Daniel Dunbara3ec60e2009-03-29 18:40:18 +00002247 Args.AddAllArgs(CmdArgs, options::OPT_W_Group, options::OPT_pedantic_Group);
2248 Args.AddLastArg(CmdArgs, options::OPT_w);
Daniel Dunbarc21c4852009-04-08 23:54:23 +00002249
Daniel Dunbara3ec60e2009-03-29 18:40:18 +00002250 // The driver treats -fsyntax-only specially.
2251 Args.AddAllArgs(CmdArgs, options::OPT_f_Group, options::OPT_fsyntax_only);
2252
2253 if (Args.hasArg(options::OPT_g_Group) && !Args.hasArg(options::OPT_g0) &&
2254 !Args.hasArg(options::OPT_fno_working_directory))
2255 CmdArgs.push_back("-fworking-directory");
2256
2257 Args.AddAllArgs(CmdArgs, options::OPT_O);
2258 Args.AddAllArgs(CmdArgs, options::OPT_undef);
2259 if (Args.hasArg(options::OPT_save_temps))
2260 CmdArgs.push_back("-fpch-preprocess");
2261}
2262
Daniel Dunbarc21c4852009-04-08 23:54:23 +00002263void darwin::CC1::AddCPPUniqueOptionsArgs(const ArgList &Args,
Daniel Dunbara3ec60e2009-03-29 18:40:18 +00002264 ArgStringList &CmdArgs,
Mike Stump1eb44332009-09-09 15:08:12 +00002265 const InputInfoList &Inputs) const {
Daniel Dunbaree788e72009-12-21 18:54:17 +00002266 const Driver &D = getToolChain().getDriver();
Daniel Dunbara3ec60e2009-03-29 18:40:18 +00002267
Daniel Dunbar88a3d6c2009-09-10 01:21:05 +00002268 CheckPreprocessingOptions(D, Args);
2269
Daniel Dunbara3ec60e2009-03-29 18:40:18 +00002270 // Derived from cpp_unique_options.
Daniel Dunbar88a3d6c2009-09-10 01:21:05 +00002271 // -{C,CC} only with -E is checked in CheckPreprocessingOptions().
2272 Args.AddLastArg(CmdArgs, options::OPT_C);
2273 Args.AddLastArg(CmdArgs, options::OPT_CC);
Daniel Dunbara3ec60e2009-03-29 18:40:18 +00002274 if (!Args.hasArg(options::OPT_Q))
2275 CmdArgs.push_back("-quiet");
2276 Args.AddAllArgs(CmdArgs, options::OPT_nostdinc);
Douglas Gregor4c2bcad2010-03-24 20:13:48 +00002277 Args.AddAllArgs(CmdArgs, options::OPT_nostdincxx);
Daniel Dunbara3ec60e2009-03-29 18:40:18 +00002278 Args.AddLastArg(CmdArgs, options::OPT_v);
2279 Args.AddAllArgs(CmdArgs, options::OPT_I_Group, options::OPT_F);
2280 Args.AddLastArg(CmdArgs, options::OPT_P);
2281
2282 // FIXME: Handle %I properly.
2283 if (getToolChain().getArchName() == "x86_64") {
2284 CmdArgs.push_back("-imultilib");
2285 CmdArgs.push_back("x86_64");
2286 }
2287
2288 if (Args.hasArg(options::OPT_MD)) {
2289 CmdArgs.push_back("-MD");
Daniel Dunbara5a7bd02009-03-30 00:34:04 +00002290 CmdArgs.push_back(darwin::CC1::getDependencyFileName(Args, Inputs));
Daniel Dunbara3ec60e2009-03-29 18:40:18 +00002291 }
2292
2293 if (Args.hasArg(options::OPT_MMD)) {
2294 CmdArgs.push_back("-MMD");
Daniel Dunbara5a7bd02009-03-30 00:34:04 +00002295 CmdArgs.push_back(darwin::CC1::getDependencyFileName(Args, Inputs));
Daniel Dunbara3ec60e2009-03-29 18:40:18 +00002296 }
2297
2298 Args.AddLastArg(CmdArgs, options::OPT_M);
2299 Args.AddLastArg(CmdArgs, options::OPT_MM);
2300 Args.AddAllArgs(CmdArgs, options::OPT_MF);
2301 Args.AddLastArg(CmdArgs, options::OPT_MG);
2302 Args.AddLastArg(CmdArgs, options::OPT_MP);
2303 Args.AddAllArgs(CmdArgs, options::OPT_MQ);
2304 Args.AddAllArgs(CmdArgs, options::OPT_MT);
2305 if (!Args.hasArg(options::OPT_M) && !Args.hasArg(options::OPT_MM) &&
2306 (Args.hasArg(options::OPT_MD) || Args.hasArg(options::OPT_MMD))) {
2307 if (Arg *OutputOpt = Args.getLastArg(options::OPT_o)) {
2308 CmdArgs.push_back("-MQ");
2309 CmdArgs.push_back(OutputOpt->getValue(Args));
2310 }
2311 }
2312
2313 Args.AddLastArg(CmdArgs, options::OPT_remap);
2314 if (Args.hasArg(options::OPT_g3))
2315 CmdArgs.push_back("-dD");
2316 Args.AddLastArg(CmdArgs, options::OPT_H);
2317
2318 AddCPPArgs(Args, CmdArgs);
2319
2320 Args.AddAllArgs(CmdArgs, options::OPT_D, options::OPT_U, options::OPT_A);
2321 Args.AddAllArgs(CmdArgs, options::OPT_i_Group);
2322
2323 for (InputInfoList::const_iterator
2324 it = Inputs.begin(), ie = Inputs.end(); it != ie; ++it) {
2325 const InputInfo &II = *it;
Daniel Dunbarc21c4852009-04-08 23:54:23 +00002326
Daniel Dunbar7c1e4652010-08-02 02:38:21 +00002327 CmdArgs.push_back(II.getFilename());
Daniel Dunbara3ec60e2009-03-29 18:40:18 +00002328 }
2329
2330 Args.AddAllArgValues(CmdArgs, options::OPT_Wp_COMMA,
2331 options::OPT_Xpreprocessor);
2332
2333 if (Args.hasArg(options::OPT_fmudflap)) {
2334 CmdArgs.push_back("-D_MUDFLAP");
2335 CmdArgs.push_back("-include");
2336 CmdArgs.push_back("mf-runtime.h");
2337 }
2338
2339 if (Args.hasArg(options::OPT_fmudflapth)) {
2340 CmdArgs.push_back("-D_MUDFLAP");
2341 CmdArgs.push_back("-D_MUDFLAPTH");
2342 CmdArgs.push_back("-include");
2343 CmdArgs.push_back("mf-runtime.h");
2344 }
2345}
2346
Daniel Dunbarc21c4852009-04-08 23:54:23 +00002347void darwin::CC1::AddCPPArgs(const ArgList &Args,
Daniel Dunbara3ec60e2009-03-29 18:40:18 +00002348 ArgStringList &CmdArgs) const {
2349 // Derived from cpp spec.
2350
2351 if (Args.hasArg(options::OPT_static)) {
2352 // The gcc spec is broken here, it refers to dynamic but
2353 // that has been translated. Start by being bug compatible.
Daniel Dunbarc21c4852009-04-08 23:54:23 +00002354
Daniel Dunbara3ec60e2009-03-29 18:40:18 +00002355 // if (!Args.hasArg(arglist.parser.dynamicOption))
2356 CmdArgs.push_back("-D__STATIC__");
2357 } else
2358 CmdArgs.push_back("-D__DYNAMIC__");
2359
2360 if (Args.hasArg(options::OPT_pthread))
2361 CmdArgs.push_back("-D_REENTRANT");
2362}
2363
Daniel Dunbar40f12652009-03-29 17:08:39 +00002364void darwin::Preprocess::ConstructJob(Compilation &C, const JobAction &JA,
Daniel Dunbar2fe238e2010-08-02 02:38:28 +00002365 const InputInfo &Output,
Daniel Dunbarc21c4852009-04-08 23:54:23 +00002366 const InputInfoList &Inputs,
2367 const ArgList &Args,
Daniel Dunbar40f12652009-03-29 17:08:39 +00002368 const char *LinkingOutput) const {
2369 ArgStringList CmdArgs;
2370
2371 assert(Inputs.size() == 1 && "Unexpected number of inputs!");
2372
2373 CmdArgs.push_back("-E");
2374
2375 if (Args.hasArg(options::OPT_traditional) ||
Daniel Dunbar40f12652009-03-29 17:08:39 +00002376 Args.hasArg(options::OPT_traditional_cpp))
2377 CmdArgs.push_back("-traditional-cpp");
Daniel Dunbarc21c4852009-04-08 23:54:23 +00002378
Daniel Dunbar40f12652009-03-29 17:08:39 +00002379 ArgStringList OutputArgs;
Daniel Dunbar7c1e4652010-08-02 02:38:21 +00002380 assert(Output.isFilename() && "Unexpected CC1 output.");
2381 OutputArgs.push_back("-o");
2382 OutputArgs.push_back(Output.getFilename());
Daniel Dunbar40f12652009-03-29 17:08:39 +00002383
Daniel Dunbar9120f172009-03-29 22:27:40 +00002384 if (Args.hasArg(options::OPT_E)) {
2385 AddCPPOptionsArgs(Args, CmdArgs, Inputs, OutputArgs);
2386 } else {
2387 AddCPPOptionsArgs(Args, CmdArgs, Inputs, ArgStringList());
2388 CmdArgs.append(OutputArgs.begin(), OutputArgs.end());
2389 }
Daniel Dunbarc21c4852009-04-08 23:54:23 +00002390
Daniel Dunbar8a2073a2009-04-03 01:27:06 +00002391 Args.AddAllArgs(CmdArgs, options::OPT_d_Group);
2392
Daniel Dunbar40f12652009-03-29 17:08:39 +00002393 const char *CC1Name = getCC1Name(Inputs[0].getType());
Daniel Dunbarc21c4852009-04-08 23:54:23 +00002394 const char *Exec =
Daniel Dunbar4a7e8892010-07-14 18:46:23 +00002395 Args.MakeArgString(getToolChain().GetProgramPath(CC1Name));
Daniel Dunbar2fe238e2010-08-02 02:38:28 +00002396 C.addCommand(new Command(JA, *this, Exec, CmdArgs));
Daniel Dunbar40f12652009-03-29 17:08:39 +00002397}
2398
2399void darwin::Compile::ConstructJob(Compilation &C, const JobAction &JA,
Daniel Dunbar2fe238e2010-08-02 02:38:28 +00002400 const InputInfo &Output,
Daniel Dunbarc21c4852009-04-08 23:54:23 +00002401 const InputInfoList &Inputs,
2402 const ArgList &Args,
Daniel Dunbar40f12652009-03-29 17:08:39 +00002403 const char *LinkingOutput) const {
Daniel Dunbaree788e72009-12-21 18:54:17 +00002404 const Driver &D = getToolChain().getDriver();
Daniel Dunbar40f12652009-03-29 17:08:39 +00002405 ArgStringList CmdArgs;
2406
2407 assert(Inputs.size() == 1 && "Unexpected number of inputs!");
2408
2409 types::ID InputType = Inputs[0].getType();
2410 const Arg *A;
Eli Friedmanceb5c5b2009-07-14 21:58:17 +00002411 if ((A = Args.getLastArg(options::OPT_traditional)))
Daniel Dunbar40f12652009-03-29 17:08:39 +00002412 D.Diag(clang::diag::err_drv_argument_only_allowed_with)
2413 << A->getAsString(Args) << "-E";
2414
Daniel Dunbar6c6424b2010-06-07 23:28:45 +00002415 if (JA.getType() == types::TY_LLVM_IR ||
2416 JA.getType() == types::TY_LTO_IR)
Daniel Dunbar40f12652009-03-29 17:08:39 +00002417 CmdArgs.push_back("-emit-llvm");
Daniel Dunbar6c6424b2010-06-07 23:28:45 +00002418 else if (JA.getType() == types::TY_LLVM_BC ||
2419 JA.getType() == types::TY_LTO_BC)
Daniel Dunbar40f12652009-03-29 17:08:39 +00002420 CmdArgs.push_back("-emit-llvm-bc");
Daniel Dunbar5915fbf2009-09-01 16:57:46 +00002421 else if (Output.getType() == types::TY_AST)
2422 D.Diag(clang::diag::err_drv_no_ast_support)
Daniel Dunbar88137642009-09-09 22:32:48 +00002423 << getToolChain().getTripleString();
Daniel Dunbarae24a882010-02-11 17:33:45 +00002424 else if (JA.getType() != types::TY_PP_Asm &&
2425 JA.getType() != types::TY_PCH)
Daniel Dunbar64952502010-02-11 03:16:21 +00002426 D.Diag(clang::diag::err_drv_invalid_gcc_output_type)
2427 << getTypeName(JA.getType());
Daniel Dunbar40f12652009-03-29 17:08:39 +00002428
2429 ArgStringList OutputArgs;
2430 if (Output.getType() != types::TY_PCH) {
2431 OutputArgs.push_back("-o");
Daniel Dunbar7c1e4652010-08-02 02:38:21 +00002432 if (Output.isNothing())
Daniel Dunbar40f12652009-03-29 17:08:39 +00002433 OutputArgs.push_back("/dev/null");
2434 else
2435 OutputArgs.push_back(Output.getFilename());
2436 }
2437
2438 // There is no need for this level of compatibility, but it makes
2439 // diffing easier.
2440 bool OutputArgsEarly = (Args.hasArg(options::OPT_fsyntax_only) ||
2441 Args.hasArg(options::OPT_S));
2442
2443 if (types::getPreprocessedType(InputType) != types::TY_INVALID) {
Daniel Dunbara3ec60e2009-03-29 18:40:18 +00002444 AddCPPUniqueOptionsArgs(Args, CmdArgs, Inputs);
Daniel Dunbar40f12652009-03-29 17:08:39 +00002445 if (OutputArgsEarly) {
2446 AddCC1OptionsArgs(Args, CmdArgs, Inputs, OutputArgs);
2447 } else {
2448 AddCC1OptionsArgs(Args, CmdArgs, Inputs, ArgStringList());
2449 CmdArgs.append(OutputArgs.begin(), OutputArgs.end());
2450 }
2451 } else {
2452 CmdArgs.push_back("-fpreprocessed");
Daniel Dunbarc21c4852009-04-08 23:54:23 +00002453
Daniel Dunbar40f12652009-03-29 17:08:39 +00002454 for (InputInfoList::const_iterator
2455 it = Inputs.begin(), ie = Inputs.end(); it != ie; ++it) {
2456 const InputInfo &II = *it;
2457
Daniel Dunbar5915fbf2009-09-01 16:57:46 +00002458 // Reject AST inputs.
2459 if (II.getType() == types::TY_AST) {
2460 D.Diag(clang::diag::err_drv_no_ast_support)
Daniel Dunbar88137642009-09-09 22:32:48 +00002461 << getToolChain().getTripleString();
Daniel Dunbar5915fbf2009-09-01 16:57:46 +00002462 return;
2463 }
2464
Daniel Dunbar7c1e4652010-08-02 02:38:21 +00002465 CmdArgs.push_back(II.getFilename());
Daniel Dunbar40f12652009-03-29 17:08:39 +00002466 }
2467
2468 if (OutputArgsEarly) {
2469 AddCC1OptionsArgs(Args, CmdArgs, Inputs, OutputArgs);
2470 } else {
2471 AddCC1OptionsArgs(Args, CmdArgs, Inputs, ArgStringList());
2472 CmdArgs.append(OutputArgs.begin(), OutputArgs.end());
2473 }
2474 }
Daniel Dunbarc21c4852009-04-08 23:54:23 +00002475
Daniel Dunbar40f12652009-03-29 17:08:39 +00002476 if (Output.getType() == types::TY_PCH) {
2477 assert(Output.isFilename() && "Invalid PCH output.");
2478
2479 CmdArgs.push_back("-o");
2480 // NOTE: gcc uses a temp .s file for this, but there doesn't seem
2481 // to be a good reason.
2482 CmdArgs.push_back("/dev/null");
Daniel Dunbarc21c4852009-04-08 23:54:23 +00002483
Daniel Dunbare6c9ae12009-11-21 02:31:29 +00002484 CmdArgs.push_back("--output-pch=");
Daniel Dunbar40f12652009-03-29 17:08:39 +00002485 CmdArgs.push_back(Output.getFilename());
Daniel Dunbarc21c4852009-04-08 23:54:23 +00002486 }
Daniel Dunbar40f12652009-03-29 17:08:39 +00002487
2488 const char *CC1Name = getCC1Name(Inputs[0].getType());
Daniel Dunbarc21c4852009-04-08 23:54:23 +00002489 const char *Exec =
Daniel Dunbar4a7e8892010-07-14 18:46:23 +00002490 Args.MakeArgString(getToolChain().GetProgramPath(CC1Name));
Daniel Dunbar2fe238e2010-08-02 02:38:28 +00002491 C.addCommand(new Command(JA, *this, Exec, CmdArgs));
Daniel Dunbar40f12652009-03-29 17:08:39 +00002492}
2493
Daniel Dunbar8cac5f72009-03-20 16:06:39 +00002494void darwin::Assemble::ConstructJob(Compilation &C, const JobAction &JA,
Daniel Dunbar2fe238e2010-08-02 02:38:28 +00002495 const InputInfo &Output,
Daniel Dunbarc21c4852009-04-08 23:54:23 +00002496 const InputInfoList &Inputs,
2497 const ArgList &Args,
Daniel Dunbar8cac5f72009-03-20 16:06:39 +00002498 const char *LinkingOutput) const {
2499 ArgStringList CmdArgs;
2500
2501 assert(Inputs.size() == 1 && "Unexpected number of inputs.");
2502 const InputInfo &Input = Inputs[0];
2503
2504 // Bit of a hack, this is only used for original inputs.
Daniel Dunbarc21c4852009-04-08 23:54:23 +00002505 //
Daniel Dunbar8e4fea62009-04-01 00:27:44 +00002506 // FIXME: This is broken for preprocessed .s inputs.
Daniel Dunbar8cac5f72009-03-20 16:06:39 +00002507 if (Input.isFilename() &&
Daniel Dunbar8e4fea62009-04-01 00:27:44 +00002508 strcmp(Input.getFilename(), Input.getBaseInput()) == 0) {
2509 if (Args.hasArg(options::OPT_gstabs))
2510 CmdArgs.push_back("--gstabs");
2511 else if (Args.hasArg(options::OPT_g_Group))
2512 CmdArgs.push_back("--gdwarf2");
2513 }
Daniel Dunbarc21c4852009-04-08 23:54:23 +00002514
Daniel Dunbar8cac5f72009-03-20 16:06:39 +00002515 // Derived from asm spec.
Daniel Dunbarcc6f8032009-09-09 18:36:27 +00002516 AddDarwinArch(Args, CmdArgs);
Daniel Dunbar8cac5f72009-03-20 16:06:39 +00002517
Daniel Dunbarf5438e32010-07-22 01:47:22 +00002518 // Use -force_cpusubtype_ALL on x86 by default.
2519 if (getToolChain().getTriple().getArch() == llvm::Triple::x86 ||
2520 getToolChain().getTriple().getArch() == llvm::Triple::x86_64 ||
Daniel Dunbarcc6f8032009-09-09 18:36:27 +00002521 Args.hasArg(options::OPT_force__cpusubtype__ALL))
2522 CmdArgs.push_back("-force_cpusubtype_ALL");
2523
Daniel Dunbar0e2679d2009-08-24 22:26:16 +00002524 if (getToolChain().getTriple().getArch() != llvm::Triple::x86_64 &&
2525 (Args.hasArg(options::OPT_mkernel) ||
Daniel Dunbar8cac5f72009-03-20 16:06:39 +00002526 Args.hasArg(options::OPT_static) ||
Daniel Dunbar0e2679d2009-08-24 22:26:16 +00002527 Args.hasArg(options::OPT_fapple_kext)))
Daniel Dunbarc21c4852009-04-08 23:54:23 +00002528 CmdArgs.push_back("-static");
2529
Daniel Dunbar8cac5f72009-03-20 16:06:39 +00002530 Args.AddAllArgValues(CmdArgs, options::OPT_Wa_COMMA,
2531 options::OPT_Xassembler);
2532
2533 assert(Output.isFilename() && "Unexpected lipo output.");
2534 CmdArgs.push_back("-o");
2535 CmdArgs.push_back(Output.getFilename());
2536
Daniel Dunbar7c1e4652010-08-02 02:38:21 +00002537 assert(Input.isFilename() && "Invalid input.");
2538 CmdArgs.push_back(Input.getFilename());
Daniel Dunbar8cac5f72009-03-20 16:06:39 +00002539
2540 // asm_final spec is empty.
2541
Daniel Dunbarc21c4852009-04-08 23:54:23 +00002542 const char *Exec =
Daniel Dunbar4a7e8892010-07-14 18:46:23 +00002543 Args.MakeArgString(getToolChain().GetProgramPath("as"));
Daniel Dunbar2fe238e2010-08-02 02:38:28 +00002544 C.addCommand(new Command(JA, *this, Exec, CmdArgs));
Daniel Dunbar8cac5f72009-03-20 16:06:39 +00002545}
Daniel Dunbarff7488d2009-03-20 00:52:38 +00002546
Daniel Dunbarfbefe6b2009-09-09 18:36:20 +00002547void darwin::DarwinTool::AddDarwinArch(const ArgList &Args,
2548 ArgStringList &CmdArgs) const {
Daniel Dunbareeff4062010-01-22 02:04:58 +00002549 llvm::StringRef ArchName = getDarwinToolChain().getDarwinArchName(Args);
2550
Daniel Dunbar02633b52009-03-26 16:23:12 +00002551 // Derived from darwin_arch spec.
2552 CmdArgs.push_back("-arch");
Daniel Dunbareeff4062010-01-22 02:04:58 +00002553 CmdArgs.push_back(Args.MakeArgString(ArchName));
Daniel Dunbar78dbd582009-09-04 18:35:31 +00002554
Daniel Dunbareeff4062010-01-22 02:04:58 +00002555 // FIXME: Is this needed anymore?
2556 if (ArchName == "arm")
Daniel Dunbar78dbd582009-09-04 18:35:31 +00002557 CmdArgs.push_back("-force_cpusubtype_ALL");
Daniel Dunbar02633b52009-03-26 16:23:12 +00002558}
2559
Daniel Dunbar748de8e2010-09-09 21:51:05 +00002560void darwin::Link::AddLinkArgs(Compilation &C,
2561 const ArgList &Args,
Daniel Dunbar02633b52009-03-26 16:23:12 +00002562 ArgStringList &CmdArgs) const {
Daniel Dunbaree788e72009-12-21 18:54:17 +00002563 const Driver &D = getToolChain().getDriver();
Daniel Dunbar02633b52009-03-26 16:23:12 +00002564
Daniel Dunbarb18dc5b2010-08-11 23:07:50 +00002565 unsigned Version[3] = { 0, 0, 0 };
2566 if (Arg *A = Args.getLastArg(options::OPT_mlinker_version_EQ)) {
2567 bool HadExtra;
2568 if (!Driver::GetReleaseVersion(A->getValue(Args), Version[0],
2569 Version[1], Version[2], HadExtra) ||
2570 HadExtra)
2571 D.Diag(clang::diag::err_drv_invalid_version_number)
2572 << A->getAsString(Args);
2573 }
2574
2575 // Newer linkers support -demangle, pass it if supported and not disabled by
2576 // the user.
Daniel Dunbara30d6cd2010-11-19 17:51:40 +00002577 //
2578 // FIXME: We temporarily avoid passing -demangle to any iOS linker, because
2579 // unfortunately we can't be guaranteed that the linker version used there
2580 // will match the linker version detected at configure time. We need the
2581 // universal driver.
2582 if (Version[0] >= 100 && !Args.hasArg(options::OPT_Z_Xlinker__no_demangle) &&
2583 !getDarwinToolChain().isTargetIPhoneOS()) {
Daniel Dunbarbcf1da82010-09-07 17:07:49 +00002584 // Don't pass -demangle to ld_classic.
2585 //
2586 // FIXME: This is a temporary workaround, ld should be handling this.
2587 bool UsesLdClassic = (getToolChain().getArch() == llvm::Triple::x86 &&
2588 Args.hasArg(options::OPT_static));
Daniel Dunbar9ced7042010-09-07 17:50:41 +00002589 if (getToolChain().getArch() == llvm::Triple::x86) {
2590 for (arg_iterator it = Args.filtered_begin(options::OPT_Xlinker,
2591 options::OPT_Wl_COMMA),
2592 ie = Args.filtered_end(); it != ie; ++it) {
2593 const Arg *A = *it;
2594 for (unsigned i = 0, e = A->getNumValues(); i != e; ++i)
2595 if (llvm::StringRef(A->getValue(Args, i)) == "-kext")
2596 UsesLdClassic = true;
2597 }
2598 }
Daniel Dunbarbcf1da82010-09-07 17:07:49 +00002599 if (!UsesLdClassic)
2600 CmdArgs.push_back("-demangle");
Daniel Dunbarb18dc5b2010-08-11 23:07:50 +00002601 }
2602
Daniel Dunbar02633b52009-03-26 16:23:12 +00002603 // Derived from the "link" spec.
2604 Args.AddAllArgs(CmdArgs, options::OPT_static);
2605 if (!Args.hasArg(options::OPT_static))
2606 CmdArgs.push_back("-dynamic");
2607 if (Args.hasArg(options::OPT_fgnu_runtime)) {
2608 // FIXME: gcc replaces -lobjc in forward args with -lobjc-gnu
2609 // here. How do we wish to handle such things?
2610 }
Daniel Dunbarc21c4852009-04-08 23:54:23 +00002611
Daniel Dunbar02633b52009-03-26 16:23:12 +00002612 if (!Args.hasArg(options::OPT_dynamiclib)) {
Daniel Dunbara6d38492010-01-22 02:04:52 +00002613 AddDarwinArch(Args, CmdArgs);
Daniel Dunbara6d38492010-01-22 02:04:52 +00002614 // FIXME: Why do this only on this path?
Daniel Dunbar8917dd42010-01-22 03:37:33 +00002615 Args.AddLastArg(CmdArgs, options::OPT_force__cpusubtype__ALL);
Daniel Dunbar02633b52009-03-26 16:23:12 +00002616
2617 Args.AddLastArg(CmdArgs, options::OPT_bundle);
2618 Args.AddAllArgs(CmdArgs, options::OPT_bundle__loader);
2619 Args.AddAllArgs(CmdArgs, options::OPT_client__name);
2620
2621 Arg *A;
2622 if ((A = Args.getLastArg(options::OPT_compatibility__version)) ||
2623 (A = Args.getLastArg(options::OPT_current__version)) ||
2624 (A = Args.getLastArg(options::OPT_install__name)))
2625 D.Diag(clang::diag::err_drv_argument_only_allowed_with)
2626 << A->getAsString(Args) << "-dynamiclib";
2627
2628 Args.AddLastArg(CmdArgs, options::OPT_force__flat__namespace);
2629 Args.AddLastArg(CmdArgs, options::OPT_keep__private__externs);
2630 Args.AddLastArg(CmdArgs, options::OPT_private__bundle);
2631 } else {
2632 CmdArgs.push_back("-dylib");
2633
2634 Arg *A;
2635 if ((A = Args.getLastArg(options::OPT_bundle)) ||
2636 (A = Args.getLastArg(options::OPT_bundle__loader)) ||
2637 (A = Args.getLastArg(options::OPT_client__name)) ||
2638 (A = Args.getLastArg(options::OPT_force__flat__namespace)) ||
2639 (A = Args.getLastArg(options::OPT_keep__private__externs)) ||
2640 (A = Args.getLastArg(options::OPT_private__bundle)))
2641 D.Diag(clang::diag::err_drv_argument_not_allowed_with)
2642 << A->getAsString(Args) << "-dynamiclib";
Daniel Dunbarc21c4852009-04-08 23:54:23 +00002643
Daniel Dunbar02633b52009-03-26 16:23:12 +00002644 Args.AddAllArgsTranslated(CmdArgs, options::OPT_compatibility__version,
2645 "-dylib_compatibility_version");
2646 Args.AddAllArgsTranslated(CmdArgs, options::OPT_current__version,
2647 "-dylib_current_version");
2648
Daniel Dunbara6d38492010-01-22 02:04:52 +00002649 AddDarwinArch(Args, CmdArgs);
Daniel Dunbar02633b52009-03-26 16:23:12 +00002650
2651 Args.AddAllArgsTranslated(CmdArgs, options::OPT_install__name,
2652 "-dylib_install_name");
2653 }
2654
2655 Args.AddLastArg(CmdArgs, options::OPT_all__load);
2656 Args.AddAllArgs(CmdArgs, options::OPT_allowable__client);
2657 Args.AddLastArg(CmdArgs, options::OPT_bind__at__load);
Daniel Dunbar251ca6c2010-01-27 00:56:37 +00002658 if (getDarwinToolChain().isTargetIPhoneOS())
Daniel Dunbard82f8fa2009-09-04 18:35:41 +00002659 Args.AddLastArg(CmdArgs, options::OPT_arch__errors__fatal);
Daniel Dunbar02633b52009-03-26 16:23:12 +00002660 Args.AddLastArg(CmdArgs, options::OPT_dead__strip);
2661 Args.AddLastArg(CmdArgs, options::OPT_no__dead__strip__inits__and__terms);
2662 Args.AddAllArgs(CmdArgs, options::OPT_dylib__file);
2663 Args.AddLastArg(CmdArgs, options::OPT_dynamic);
2664 Args.AddAllArgs(CmdArgs, options::OPT_exported__symbols__list);
2665 Args.AddLastArg(CmdArgs, options::OPT_flat__namespace);
2666 Args.AddAllArgs(CmdArgs, options::OPT_headerpad__max__install__names);
2667 Args.AddAllArgs(CmdArgs, options::OPT_image__base);
2668 Args.AddAllArgs(CmdArgs, options::OPT_init);
2669
Daniel Dunbar314cba02010-01-26 03:56:40 +00002670 // Adding all arguments doesn't make sense here but this is what gcc does. One
2671 // of this should always be present thanks to argument translation.
2672 assert((Args.hasArg(options::OPT_mmacosx_version_min_EQ) ||
2673 Args.hasArg(options::OPT_miphoneos_version_min_EQ)) &&
2674 "Missing version argument (lost in translation)?");
Daniel Dunbar30392de2009-09-04 18:35:21 +00002675 Args.AddAllArgsTranslated(CmdArgs, options::OPT_mmacosx_version_min_EQ,
Daniel Dunbar294691e2009-11-04 06:24:38 +00002676 "-macosx_version_min");
Daniel Dunbar02633b52009-03-26 16:23:12 +00002677 Args.AddAllArgsTranslated(CmdArgs, options::OPT_miphoneos_version_min_EQ,
2678 "-iphoneos_version_min");
2679 Args.AddLastArg(CmdArgs, options::OPT_nomultidefs);
2680 Args.AddLastArg(CmdArgs, options::OPT_multi__module);
2681 Args.AddLastArg(CmdArgs, options::OPT_single__module);
2682 Args.AddAllArgs(CmdArgs, options::OPT_multiply__defined);
2683 Args.AddAllArgs(CmdArgs, options::OPT_multiply__defined__unused);
Daniel Dunbarc21c4852009-04-08 23:54:23 +00002684
Daniel Dunbar47e879d2010-07-13 23:31:40 +00002685 if (const Arg *A = Args.getLastArg(options::OPT_fpie, options::OPT_fPIE,
2686 options::OPT_fno_pie,
2687 options::OPT_fno_PIE)) {
2688 if (A->getOption().matches(options::OPT_fpie) ||
2689 A->getOption().matches(options::OPT_fPIE))
2690 CmdArgs.push_back("-pie");
2691 else
2692 CmdArgs.push_back("-no_pie");
2693 }
Daniel Dunbar02633b52009-03-26 16:23:12 +00002694
2695 Args.AddLastArg(CmdArgs, options::OPT_prebind);
2696 Args.AddLastArg(CmdArgs, options::OPT_noprebind);
2697 Args.AddLastArg(CmdArgs, options::OPT_nofixprebinding);
2698 Args.AddLastArg(CmdArgs, options::OPT_prebind__all__twolevel__modules);
2699 Args.AddLastArg(CmdArgs, options::OPT_read__only__relocs);
2700 Args.AddAllArgs(CmdArgs, options::OPT_sectcreate);
2701 Args.AddAllArgs(CmdArgs, options::OPT_sectorder);
2702 Args.AddAllArgs(CmdArgs, options::OPT_seg1addr);
2703 Args.AddAllArgs(CmdArgs, options::OPT_segprot);
2704 Args.AddAllArgs(CmdArgs, options::OPT_segaddr);
2705 Args.AddAllArgs(CmdArgs, options::OPT_segs__read__only__addr);
2706 Args.AddAllArgs(CmdArgs, options::OPT_segs__read__write__addr);
2707 Args.AddAllArgs(CmdArgs, options::OPT_seg__addr__table);
2708 Args.AddAllArgs(CmdArgs, options::OPT_seg__addr__table__filename);
2709 Args.AddAllArgs(CmdArgs, options::OPT_sub__library);
2710 Args.AddAllArgs(CmdArgs, options::OPT_sub__umbrella);
Daniel Dunbard82f8fa2009-09-04 18:35:41 +00002711
Daniel Dunbar02633b52009-03-26 16:23:12 +00002712 Args.AddAllArgsTranslated(CmdArgs, options::OPT_isysroot, "-syslibroot");
Daniel Dunbar251ca6c2010-01-27 00:56:37 +00002713 if (getDarwinToolChain().isTargetIPhoneOS()) {
Daniel Dunbard82f8fa2009-09-04 18:35:41 +00002714 if (!Args.hasArg(options::OPT_isysroot)) {
2715 CmdArgs.push_back("-syslibroot");
2716 CmdArgs.push_back("/Developer/SDKs/Extra");
2717 }
2718 }
2719
Daniel Dunbar02633b52009-03-26 16:23:12 +00002720 Args.AddLastArg(CmdArgs, options::OPT_twolevel__namespace);
2721 Args.AddLastArg(CmdArgs, options::OPT_twolevel__namespace__hints);
2722 Args.AddAllArgs(CmdArgs, options::OPT_umbrella);
2723 Args.AddAllArgs(CmdArgs, options::OPT_undefined);
2724 Args.AddAllArgs(CmdArgs, options::OPT_unexported__symbols__list);
Daniel Dunbard82f8fa2009-09-04 18:35:41 +00002725 Args.AddAllArgs(CmdArgs, options::OPT_weak__reference__mismatches);
Daniel Dunbar02633b52009-03-26 16:23:12 +00002726 Args.AddLastArg(CmdArgs, options::OPT_X_Flag);
2727 Args.AddAllArgs(CmdArgs, options::OPT_y);
2728 Args.AddLastArg(CmdArgs, options::OPT_w);
2729 Args.AddAllArgs(CmdArgs, options::OPT_pagezero__size);
2730 Args.AddAllArgs(CmdArgs, options::OPT_segs__read__);
2731 Args.AddLastArg(CmdArgs, options::OPT_seglinkedit);
2732 Args.AddLastArg(CmdArgs, options::OPT_noseglinkedit);
2733 Args.AddAllArgs(CmdArgs, options::OPT_sectalign);
2734 Args.AddAllArgs(CmdArgs, options::OPT_sectobjectsymbols);
2735 Args.AddAllArgs(CmdArgs, options::OPT_segcreate);
2736 Args.AddLastArg(CmdArgs, options::OPT_whyload);
2737 Args.AddLastArg(CmdArgs, options::OPT_whatsloaded);
2738 Args.AddAllArgs(CmdArgs, options::OPT_dylinker__install__name);
2739 Args.AddLastArg(CmdArgs, options::OPT_dylinker);
2740 Args.AddLastArg(CmdArgs, options::OPT_Mach);
2741}
2742
2743void darwin::Link::ConstructJob(Compilation &C, const JobAction &JA,
Daniel Dunbar2fe238e2010-08-02 02:38:28 +00002744 const InputInfo &Output,
Daniel Dunbarc21c4852009-04-08 23:54:23 +00002745 const InputInfoList &Inputs,
2746 const ArgList &Args,
Daniel Dunbar02633b52009-03-26 16:23:12 +00002747 const char *LinkingOutput) const {
2748 assert(Output.getType() == types::TY_Image && "Invalid linker output type.");
Daniel Dunbare0be8b12009-09-08 16:39:16 +00002749
Daniel Dunbar02633b52009-03-26 16:23:12 +00002750 // The logic here is derived from gcc's behavior; most of which
2751 // comes from specs (starting with link_command). Consult gcc for
2752 // more information.
Daniel Dunbar02633b52009-03-26 16:23:12 +00002753 ArgStringList CmdArgs;
2754
2755 // I'm not sure why this particular decomposition exists in gcc, but
2756 // we follow suite for ease of comparison.
Daniel Dunbar748de8e2010-09-09 21:51:05 +00002757 AddLinkArgs(C, Args, CmdArgs);
Daniel Dunbar02633b52009-03-26 16:23:12 +00002758
Daniel Dunbar02633b52009-03-26 16:23:12 +00002759 Args.AddAllArgs(CmdArgs, options::OPT_d_Flag);
2760 Args.AddAllArgs(CmdArgs, options::OPT_s);
2761 Args.AddAllArgs(CmdArgs, options::OPT_t);
2762 Args.AddAllArgs(CmdArgs, options::OPT_Z_Flag);
2763 Args.AddAllArgs(CmdArgs, options::OPT_u_Group);
2764 Args.AddAllArgs(CmdArgs, options::OPT_A);
2765 Args.AddLastArg(CmdArgs, options::OPT_e);
2766 Args.AddAllArgs(CmdArgs, options::OPT_m_Separate);
2767 Args.AddAllArgs(CmdArgs, options::OPT_r);
2768
Daniel Dunbar270073c2010-10-18 22:08:36 +00002769 // Forward -ObjC when either -ObjC or -ObjC++ is used, to force loading
2770 // members of static archive libraries which implement Objective-C classes or
2771 // categories.
2772 if (Args.hasArg(options::OPT_ObjC) || Args.hasArg(options::OPT_ObjCXX))
2773 CmdArgs.push_back("-ObjC");
Michael J. Spencer20249a12010-10-21 03:16:25 +00002774
Daniel Dunbar02633b52009-03-26 16:23:12 +00002775 CmdArgs.push_back("-o");
2776 CmdArgs.push_back(Output.getFilename());
2777
Daniel Dunbar02633b52009-03-26 16:23:12 +00002778 if (!Args.hasArg(options::OPT_A) &&
2779 !Args.hasArg(options::OPT_nostdlib) &&
2780 !Args.hasArg(options::OPT_nostartfiles)) {
2781 // Derived from startfile spec.
2782 if (Args.hasArg(options::OPT_dynamiclib)) {
Daniel Dunbarc21c4852009-04-08 23:54:23 +00002783 // Derived from darwin_dylib1 spec.
Daniel Dunbarcacb0f02010-01-27 00:56:56 +00002784 if (getDarwinToolChain().isTargetIPhoneOS()) {
2785 if (getDarwinToolChain().isIPhoneOSVersionLT(3, 1))
2786 CmdArgs.push_back("-ldylib1.o");
2787 } else {
Daniel Dunbarce3fdf22010-01-27 00:57:03 +00002788 if (getDarwinToolChain().isMacosxVersionLT(10, 5))
Daniel Dunbarcacb0f02010-01-27 00:56:56 +00002789 CmdArgs.push_back("-ldylib1.o");
Daniel Dunbarce3fdf22010-01-27 00:57:03 +00002790 else if (getDarwinToolChain().isMacosxVersionLT(10, 6))
Daniel Dunbarcacb0f02010-01-27 00:56:56 +00002791 CmdArgs.push_back("-ldylib1.10.5.o");
2792 }
Daniel Dunbar02633b52009-03-26 16:23:12 +00002793 } else {
2794 if (Args.hasArg(options::OPT_bundle)) {
Daniel Dunbar8a8d8af2009-04-01 03:17:40 +00002795 if (!Args.hasArg(options::OPT_static)) {
2796 // Derived from darwin_bundle1 spec.
Daniel Dunbarcacb0f02010-01-27 00:56:56 +00002797 if (getDarwinToolChain().isTargetIPhoneOS()) {
2798 if (getDarwinToolChain().isIPhoneOSVersionLT(3, 1))
2799 CmdArgs.push_back("-lbundle1.o");
2800 } else {
Daniel Dunbarce3fdf22010-01-27 00:57:03 +00002801 if (getDarwinToolChain().isMacosxVersionLT(10, 6))
Daniel Dunbarcacb0f02010-01-27 00:56:56 +00002802 CmdArgs.push_back("-lbundle1.o");
2803 }
Daniel Dunbar8a8d8af2009-04-01 03:17:40 +00002804 }
Daniel Dunbar02633b52009-03-26 16:23:12 +00002805 } else {
2806 if (Args.hasArg(options::OPT_pg)) {
2807 if (Args.hasArg(options::OPT_static) ||
2808 Args.hasArg(options::OPT_object) ||
2809 Args.hasArg(options::OPT_preload)) {
2810 CmdArgs.push_back("-lgcrt0.o");
2811 } else {
2812 CmdArgs.push_back("-lgcrt1.o");
Daniel Dunbarc21c4852009-04-08 23:54:23 +00002813
Daniel Dunbar02633b52009-03-26 16:23:12 +00002814 // darwin_crt2 spec is empty.
Daniel Dunbarc21c4852009-04-08 23:54:23 +00002815 }
Daniel Dunbar02633b52009-03-26 16:23:12 +00002816 } else {
2817 if (Args.hasArg(options::OPT_static) ||
2818 Args.hasArg(options::OPT_object) ||
2819 Args.hasArg(options::OPT_preload)) {
2820 CmdArgs.push_back("-lcrt0.o");
2821 } else {
2822 // Derived from darwin_crt1 spec.
Daniel Dunbar251ca6c2010-01-27 00:56:37 +00002823 if (getDarwinToolChain().isTargetIPhoneOS()) {
Daniel Dunbarcacb0f02010-01-27 00:56:56 +00002824 if (getDarwinToolChain().isIPhoneOSVersionLT(3, 1))
2825 CmdArgs.push_back("-lcrt1.o");
2826 else
2827 CmdArgs.push_back("-lcrt1.3.1.o");
Daniel Dunbarce3fdf22010-01-27 00:57:03 +00002828 } else {
2829 if (getDarwinToolChain().isMacosxVersionLT(10, 5))
2830 CmdArgs.push_back("-lcrt1.o");
2831 else if (getDarwinToolChain().isMacosxVersionLT(10, 6))
2832 CmdArgs.push_back("-lcrt1.10.5.o");
2833 else
2834 CmdArgs.push_back("-lcrt1.10.6.o");
Daniel Dunbarc21c4852009-04-08 23:54:23 +00002835
Daniel Dunbarce3fdf22010-01-27 00:57:03 +00002836 // darwin_crt2 spec is empty.
2837 }
Daniel Dunbar02633b52009-03-26 16:23:12 +00002838 }
2839 }
2840 }
2841 }
2842
Daniel Dunbarce3fdf22010-01-27 00:57:03 +00002843 if (!getDarwinToolChain().isTargetIPhoneOS() &&
2844 Args.hasArg(options::OPT_shared_libgcc) &&
2845 getDarwinToolChain().isMacosxVersionLT(10, 5)) {
Daniel Dunbar88137642009-09-09 22:32:48 +00002846 const char *Str =
Daniel Dunbar4a7e8892010-07-14 18:46:23 +00002847 Args.MakeArgString(getToolChain().GetFilePath("crt3.o"));
Daniel Dunbar88137642009-09-09 22:32:48 +00002848 CmdArgs.push_back(Str);
Daniel Dunbar02633b52009-03-26 16:23:12 +00002849 }
2850 }
2851
2852 Args.AddAllArgs(CmdArgs, options::OPT_L);
Daniel Dunbarc21c4852009-04-08 23:54:23 +00002853
Daniel Dunbar02633b52009-03-26 16:23:12 +00002854 if (Args.hasArg(options::OPT_fopenmp))
2855 // This is more complicated in gcc...
2856 CmdArgs.push_back("-lgomp");
2857
Daniel Dunbar6b200b22009-09-18 08:14:36 +00002858 getDarwinToolChain().AddLinkSearchPathArgs(Args, CmdArgs);
Daniel Dunbarc21c4852009-04-08 23:54:23 +00002859
Daniel Dunbar2008fee2010-09-17 00:24:54 +00002860 AddLinkerInputs(getToolChain(), Inputs, Args, CmdArgs);
Daniel Dunbar02633b52009-03-26 16:23:12 +00002861
2862 if (LinkingOutput) {
2863 CmdArgs.push_back("-arch_multiple");
2864 CmdArgs.push_back("-final_output");
2865 CmdArgs.push_back(LinkingOutput);
2866 }
2867
2868 if (Args.hasArg(options::OPT_fprofile_arcs) ||
2869 Args.hasArg(options::OPT_fprofile_generate) ||
2870 Args.hasArg(options::OPT_fcreate_profile) ||
2871 Args.hasArg(options::OPT_coverage))
2872 CmdArgs.push_back("-lgcov");
Daniel Dunbarc21c4852009-04-08 23:54:23 +00002873
Daniel Dunbar02633b52009-03-26 16:23:12 +00002874 if (Args.hasArg(options::OPT_fnested_functions))
2875 CmdArgs.push_back("-allow_stack_execute");
Daniel Dunbarc21c4852009-04-08 23:54:23 +00002876
Daniel Dunbar02633b52009-03-26 16:23:12 +00002877 if (!Args.hasArg(options::OPT_nostdlib) &&
2878 !Args.hasArg(options::OPT_nodefaultlibs)) {
Daniel Dunbaree788e72009-12-21 18:54:17 +00002879 if (getToolChain().getDriver().CCCIsCXX)
Daniel Dunbar132e35d2010-09-17 01:20:05 +00002880 getToolChain().AddCXXStdlibLibArgs(Args, CmdArgs);
Daniel Dunbaredfa02b2009-04-08 06:06:21 +00002881
Daniel Dunbar02633b52009-03-26 16:23:12 +00002882 // link_ssp spec is empty.
2883
Daniel Dunbar6cd41542009-09-18 08:15:03 +00002884 // Let the tool chain choose which runtime library to link.
2885 getDarwinToolChain().AddLinkRuntimeLibArgs(Args, CmdArgs);
Daniel Dunbar02633b52009-03-26 16:23:12 +00002886 }
2887
2888 if (!Args.hasArg(options::OPT_A) &&
2889 !Args.hasArg(options::OPT_nostdlib) &&
2890 !Args.hasArg(options::OPT_nostartfiles)) {
2891 // endfile_spec is empty.
2892 }
2893
2894 Args.AddAllArgs(CmdArgs, options::OPT_T_Group);
2895 Args.AddAllArgs(CmdArgs, options::OPT_F);
2896
Daniel Dunbarc21c4852009-04-08 23:54:23 +00002897 const char *Exec =
Daniel Dunbar4a7e8892010-07-14 18:46:23 +00002898 Args.MakeArgString(getToolChain().GetProgramPath("ld"));
Daniel Dunbar2fe238e2010-08-02 02:38:28 +00002899 C.addCommand(new Command(JA, *this, Exec, CmdArgs));
Daniel Dunbar02633b52009-03-26 16:23:12 +00002900}
2901
Daniel Dunbarff7488d2009-03-20 00:52:38 +00002902void darwin::Lipo::ConstructJob(Compilation &C, const JobAction &JA,
Daniel Dunbar2fe238e2010-08-02 02:38:28 +00002903 const InputInfo &Output,
Daniel Dunbarc21c4852009-04-08 23:54:23 +00002904 const InputInfoList &Inputs,
2905 const ArgList &Args,
Daniel Dunbarff7488d2009-03-20 00:52:38 +00002906 const char *LinkingOutput) const {
2907 ArgStringList CmdArgs;
2908
2909 CmdArgs.push_back("-create");
2910 assert(Output.isFilename() && "Unexpected lipo output.");
Daniel Dunbara428df82009-03-24 00:24:37 +00002911
2912 CmdArgs.push_back("-output");
Daniel Dunbarff7488d2009-03-20 00:52:38 +00002913 CmdArgs.push_back(Output.getFilename());
Daniel Dunbara428df82009-03-24 00:24:37 +00002914
Daniel Dunbarff7488d2009-03-20 00:52:38 +00002915 for (InputInfoList::const_iterator
2916 it = Inputs.begin(), ie = Inputs.end(); it != ie; ++it) {
2917 const InputInfo &II = *it;
2918 assert(II.isFilename() && "Unexpected lipo input.");
2919 CmdArgs.push_back(II.getFilename());
2920 }
Daniel Dunbarc21c4852009-04-08 23:54:23 +00002921 const char *Exec =
Daniel Dunbar4a7e8892010-07-14 18:46:23 +00002922 Args.MakeArgString(getToolChain().GetProgramPath("lipo"));
Daniel Dunbar2fe238e2010-08-02 02:38:28 +00002923 C.addCommand(new Command(JA, *this, Exec, CmdArgs));
Daniel Dunbarff7488d2009-03-20 00:52:38 +00002924}
Daniel Dunbar68a31d42009-03-31 17:45:15 +00002925
Daniel Dunbar6e0f2542010-06-04 18:28:36 +00002926void darwin::Dsymutil::ConstructJob(Compilation &C, const JobAction &JA,
Daniel Dunbar2fe238e2010-08-02 02:38:28 +00002927 const InputInfo &Output,
Daniel Dunbar6e0f2542010-06-04 18:28:36 +00002928 const InputInfoList &Inputs,
2929 const ArgList &Args,
2930 const char *LinkingOutput) const {
2931 ArgStringList CmdArgs;
2932
2933 assert(Inputs.size() == 1 && "Unable to handle multiple inputs.");
2934 const InputInfo &Input = Inputs[0];
2935 assert(Input.isFilename() && "Unexpected dsymutil input.");
2936 CmdArgs.push_back(Input.getFilename());
2937
2938 CmdArgs.push_back("-o");
2939 CmdArgs.push_back(Output.getFilename());
2940
2941 const char *Exec =
Daniel Dunbar4a7e8892010-07-14 18:46:23 +00002942 Args.MakeArgString(getToolChain().GetProgramPath("dsymutil"));
Daniel Dunbar2fe238e2010-08-02 02:38:28 +00002943 C.addCommand(new Command(JA, *this, Exec, CmdArgs));
Daniel Dunbar6e0f2542010-06-04 18:28:36 +00002944}
2945
Edward O'Callaghane7925a02009-08-22 01:06:46 +00002946void auroraux::Assemble::ConstructJob(Compilation &C, const JobAction &JA,
Daniel Dunbar2fe238e2010-08-02 02:38:28 +00002947 const InputInfo &Output,
Daniel Dunbar294691e2009-11-04 06:24:38 +00002948 const InputInfoList &Inputs,
2949 const ArgList &Args,
2950 const char *LinkingOutput) const {
Edward O'Callaghane7925a02009-08-22 01:06:46 +00002951 ArgStringList CmdArgs;
2952
2953 Args.AddAllArgValues(CmdArgs, options::OPT_Wa_COMMA,
2954 options::OPT_Xassembler);
2955
2956 CmdArgs.push_back("-o");
Daniel Dunbar7c1e4652010-08-02 02:38:21 +00002957 CmdArgs.push_back(Output.getFilename());
Edward O'Callaghane7925a02009-08-22 01:06:46 +00002958
2959 for (InputInfoList::const_iterator
2960 it = Inputs.begin(), ie = Inputs.end(); it != ie; ++it) {
2961 const InputInfo &II = *it;
Daniel Dunbar7c1e4652010-08-02 02:38:21 +00002962 CmdArgs.push_back(II.getFilename());
Edward O'Callaghane7925a02009-08-22 01:06:46 +00002963 }
2964
2965 const char *Exec =
Daniel Dunbar4a7e8892010-07-14 18:46:23 +00002966 Args.MakeArgString(getToolChain().GetProgramPath("gas"));
Daniel Dunbar2fe238e2010-08-02 02:38:28 +00002967 C.addCommand(new Command(JA, *this, Exec, CmdArgs));
Edward O'Callaghane7925a02009-08-22 01:06:46 +00002968}
2969
2970void auroraux::Link::ConstructJob(Compilation &C, const JobAction &JA,
Daniel Dunbar2fe238e2010-08-02 02:38:28 +00002971 const InputInfo &Output,
Daniel Dunbar294691e2009-11-04 06:24:38 +00002972 const InputInfoList &Inputs,
2973 const ArgList &Args,
2974 const char *LinkingOutput) const {
Edward O'Callaghane7925a02009-08-22 01:06:46 +00002975 ArgStringList CmdArgs;
2976
2977 if ((!Args.hasArg(options::OPT_nostdlib)) &&
Daniel Dunbar294691e2009-11-04 06:24:38 +00002978 (!Args.hasArg(options::OPT_shared))) {
Edward O'Callaghane7925a02009-08-22 01:06:46 +00002979 CmdArgs.push_back("-e");
Edward O'Callaghan7adf9492009-10-15 07:44:07 +00002980 CmdArgs.push_back("_start");
Edward O'Callaghane7925a02009-08-22 01:06:46 +00002981 }
2982
2983 if (Args.hasArg(options::OPT_static)) {
2984 CmdArgs.push_back("-Bstatic");
Edward O'Callaghan7adf9492009-10-15 07:44:07 +00002985 CmdArgs.push_back("-dn");
Edward O'Callaghane7925a02009-08-22 01:06:46 +00002986 } else {
Edward O'Callaghan7adf9492009-10-15 07:44:07 +00002987// CmdArgs.push_back("--eh-frame-hdr");
Edward O'Callaghane7925a02009-08-22 01:06:46 +00002988 CmdArgs.push_back("-Bdynamic");
2989 if (Args.hasArg(options::OPT_shared)) {
2990 CmdArgs.push_back("-shared");
2991 } else {
Edward O'Callaghan3cecc192009-10-16 19:44:18 +00002992 CmdArgs.push_back("--dynamic-linker");
Edward O'Callaghane7925a02009-08-22 01:06:46 +00002993 CmdArgs.push_back("/lib/ld.so.1"); // 64Bit Path /lib/amd64/ld.so.1
2994 }
2995 }
2996
Daniel Dunbar7c1e4652010-08-02 02:38:21 +00002997 if (Output.isFilename()) {
Edward O'Callaghane7925a02009-08-22 01:06:46 +00002998 CmdArgs.push_back("-o");
2999 CmdArgs.push_back(Output.getFilename());
3000 } else {
3001 assert(Output.isNothing() && "Invalid output.");
3002 }
3003
3004 if (!Args.hasArg(options::OPT_nostdlib) &&
3005 !Args.hasArg(options::OPT_nostartfiles)) {
3006 if (!Args.hasArg(options::OPT_shared)) {
Chris Lattner38e317d2010-07-07 16:01:42 +00003007 CmdArgs.push_back(Args.MakeArgString(
Daniel Dunbar4a7e8892010-07-14 18:46:23 +00003008 getToolChain().GetFilePath("crt1.o")));
Chris Lattner38e317d2010-07-07 16:01:42 +00003009 CmdArgs.push_back(Args.MakeArgString(
Daniel Dunbar4a7e8892010-07-14 18:46:23 +00003010 getToolChain().GetFilePath("crti.o")));
Chris Lattner38e317d2010-07-07 16:01:42 +00003011 CmdArgs.push_back(Args.MakeArgString(
Daniel Dunbar4a7e8892010-07-14 18:46:23 +00003012 getToolChain().GetFilePath("crtbegin.o")));
Edward O'Callaghane7925a02009-08-22 01:06:46 +00003013 } else {
Chris Lattner38e317d2010-07-07 16:01:42 +00003014 CmdArgs.push_back(Args.MakeArgString(
Daniel Dunbar4a7e8892010-07-14 18:46:23 +00003015 getToolChain().GetFilePath("crti.o")));
Edward O'Callaghane7925a02009-08-22 01:06:46 +00003016 }
Chris Lattner38e317d2010-07-07 16:01:42 +00003017 CmdArgs.push_back(Args.MakeArgString(
Daniel Dunbar4a7e8892010-07-14 18:46:23 +00003018 getToolChain().GetFilePath("crtn.o")));
Edward O'Callaghane7925a02009-08-22 01:06:46 +00003019 }
3020
Daniel Dunbar294691e2009-11-04 06:24:38 +00003021 CmdArgs.push_back(Args.MakeArgString("-L/opt/gcc4/lib/gcc/"
3022 + getToolChain().getTripleString()
Daniel Dunbarf7fb31f2009-10-29 02:24:37 +00003023 + "/4.2.4"));
Edward O'Callaghane7925a02009-08-22 01:06:46 +00003024
3025 Args.AddAllArgs(CmdArgs, options::OPT_L);
3026 Args.AddAllArgs(CmdArgs, options::OPT_T_Group);
3027 Args.AddAllArgs(CmdArgs, options::OPT_e);
3028
Daniel Dunbar2008fee2010-09-17 00:24:54 +00003029 AddLinkerInputs(getToolChain(), Inputs, Args, CmdArgs);
Edward O'Callaghane7925a02009-08-22 01:06:46 +00003030
3031 if (!Args.hasArg(options::OPT_nostdlib) &&
3032 !Args.hasArg(options::OPT_nodefaultlibs)) {
3033 // FIXME: For some reason GCC passes -lgcc before adding
3034 // the default system libraries. Just mimic this for now.
3035 CmdArgs.push_back("-lgcc");
3036
3037 if (Args.hasArg(options::OPT_pthread))
3038 CmdArgs.push_back("-pthread");
3039 if (!Args.hasArg(options::OPT_shared))
3040 CmdArgs.push_back("-lc");
3041 CmdArgs.push_back("-lgcc");
3042 }
3043
3044 if (!Args.hasArg(options::OPT_nostdlib) &&
3045 !Args.hasArg(options::OPT_nostartfiles)) {
3046 if (!Args.hasArg(options::OPT_shared))
Chris Lattner38e317d2010-07-07 16:01:42 +00003047 CmdArgs.push_back(Args.MakeArgString(
Daniel Dunbar4a7e8892010-07-14 18:46:23 +00003048 getToolChain().GetFilePath("crtend.o")));
Edward O'Callaghane7925a02009-08-22 01:06:46 +00003049 }
3050
3051 const char *Exec =
Daniel Dunbar4a7e8892010-07-14 18:46:23 +00003052 Args.MakeArgString(getToolChain().GetProgramPath("ld"));
Daniel Dunbar2fe238e2010-08-02 02:38:28 +00003053 C.addCommand(new Command(JA, *this, Exec, CmdArgs));
Edward O'Callaghane7925a02009-08-22 01:06:46 +00003054}
3055
Daniel Dunbarf7b8eec2009-06-29 20:52:51 +00003056void openbsd::Assemble::ConstructJob(Compilation &C, const JobAction &JA,
Daniel Dunbar2fe238e2010-08-02 02:38:28 +00003057 const InputInfo &Output,
Daniel Dunbarf7b8eec2009-06-29 20:52:51 +00003058 const InputInfoList &Inputs,
3059 const ArgList &Args,
Mike Stump1eb44332009-09-09 15:08:12 +00003060 const char *LinkingOutput) const {
Daniel Dunbarf7b8eec2009-06-29 20:52:51 +00003061 ArgStringList CmdArgs;
3062
3063 Args.AddAllArgValues(CmdArgs, options::OPT_Wa_COMMA,
3064 options::OPT_Xassembler);
3065
3066 CmdArgs.push_back("-o");
Daniel Dunbar7c1e4652010-08-02 02:38:21 +00003067 CmdArgs.push_back(Output.getFilename());
Daniel Dunbarf7b8eec2009-06-29 20:52:51 +00003068
3069 for (InputInfoList::const_iterator
3070 it = Inputs.begin(), ie = Inputs.end(); it != ie; ++it) {
3071 const InputInfo &II = *it;
Daniel Dunbar7c1e4652010-08-02 02:38:21 +00003072 CmdArgs.push_back(II.getFilename());
Daniel Dunbarf7b8eec2009-06-29 20:52:51 +00003073 }
3074
3075 const char *Exec =
Daniel Dunbar4a7e8892010-07-14 18:46:23 +00003076 Args.MakeArgString(getToolChain().GetProgramPath("as"));
Daniel Dunbar2fe238e2010-08-02 02:38:28 +00003077 C.addCommand(new Command(JA, *this, Exec, CmdArgs));
Daniel Dunbarf7b8eec2009-06-29 20:52:51 +00003078}
3079
3080void openbsd::Link::ConstructJob(Compilation &C, const JobAction &JA,
Daniel Dunbar2fe238e2010-08-02 02:38:28 +00003081 const InputInfo &Output,
Daniel Dunbarf7b8eec2009-06-29 20:52:51 +00003082 const InputInfoList &Inputs,
3083 const ArgList &Args,
3084 const char *LinkingOutput) const {
Daniel Dunbaree788e72009-12-21 18:54:17 +00003085 const Driver &D = getToolChain().getDriver();
Daniel Dunbarf7b8eec2009-06-29 20:52:51 +00003086 ArgStringList CmdArgs;
3087
Daniel Dunbar2bbcf662009-08-03 01:28:59 +00003088 if ((!Args.hasArg(options::OPT_nostdlib)) &&
Daniel Dunbar294691e2009-11-04 06:24:38 +00003089 (!Args.hasArg(options::OPT_shared))) {
Daniel Dunbar2bbcf662009-08-03 01:28:59 +00003090 CmdArgs.push_back("-e");
3091 CmdArgs.push_back("__start");
3092 }
3093
Daniel Dunbarf7b8eec2009-06-29 20:52:51 +00003094 if (Args.hasArg(options::OPT_static)) {
3095 CmdArgs.push_back("-Bstatic");
3096 } else {
Rafael Espindola65ba55d2010-11-11 02:17:51 +00003097 if (Args.hasArg(options::OPT_rdynamic))
3098 CmdArgs.push_back("-export-dynamic");
Daniel Dunbarf7b8eec2009-06-29 20:52:51 +00003099 CmdArgs.push_back("--eh-frame-hdr");
Daniel Dunbar2bbcf662009-08-03 01:28:59 +00003100 CmdArgs.push_back("-Bdynamic");
Daniel Dunbarf7b8eec2009-06-29 20:52:51 +00003101 if (Args.hasArg(options::OPT_shared)) {
Daniel Dunbar2bbcf662009-08-03 01:28:59 +00003102 CmdArgs.push_back("-shared");
Daniel Dunbarf7b8eec2009-06-29 20:52:51 +00003103 } else {
3104 CmdArgs.push_back("-dynamic-linker");
3105 CmdArgs.push_back("/usr/libexec/ld.so");
3106 }
3107 }
3108
Daniel Dunbar7c1e4652010-08-02 02:38:21 +00003109 if (Output.isFilename()) {
Daniel Dunbarf7b8eec2009-06-29 20:52:51 +00003110 CmdArgs.push_back("-o");
3111 CmdArgs.push_back(Output.getFilename());
3112 } else {
3113 assert(Output.isNothing() && "Invalid output.");
3114 }
3115
3116 if (!Args.hasArg(options::OPT_nostdlib) &&
3117 !Args.hasArg(options::OPT_nostartfiles)) {
3118 if (!Args.hasArg(options::OPT_shared)) {
Chris Lattner38e317d2010-07-07 16:01:42 +00003119 CmdArgs.push_back(Args.MakeArgString(
Daniel Dunbar4a7e8892010-07-14 18:46:23 +00003120 getToolChain().GetFilePath("crt0.o")));
Chris Lattner38e317d2010-07-07 16:01:42 +00003121 CmdArgs.push_back(Args.MakeArgString(
Daniel Dunbar4a7e8892010-07-14 18:46:23 +00003122 getToolChain().GetFilePath("crtbegin.o")));
Daniel Dunbarf7b8eec2009-06-29 20:52:51 +00003123 } else {
Chris Lattner38e317d2010-07-07 16:01:42 +00003124 CmdArgs.push_back(Args.MakeArgString(
Daniel Dunbar4a7e8892010-07-14 18:46:23 +00003125 getToolChain().GetFilePath("crtbeginS.o")));
Daniel Dunbarf7b8eec2009-06-29 20:52:51 +00003126 }
3127 }
3128
Edward O'Callaghane7e18202009-10-28 15:13:08 +00003129 std::string Triple = getToolChain().getTripleString();
3130 if (Triple.substr(0, 6) == "x86_64")
Daniel Dunbar294691e2009-11-04 06:24:38 +00003131 Triple.replace(0, 6, "amd64");
Daniel Dunbarf7fb31f2009-10-29 02:24:37 +00003132 CmdArgs.push_back(Args.MakeArgString("-L/usr/lib/gcc-lib/" + Triple +
Daniel Dunbar95c04572010-08-01 23:13:54 +00003133 "/4.2.1"));
Daniel Dunbar2bbcf662009-08-03 01:28:59 +00003134
Daniel Dunbarf7b8eec2009-06-29 20:52:51 +00003135 Args.AddAllArgs(CmdArgs, options::OPT_L);
3136 Args.AddAllArgs(CmdArgs, options::OPT_T_Group);
3137 Args.AddAllArgs(CmdArgs, options::OPT_e);
3138
Daniel Dunbar2008fee2010-09-17 00:24:54 +00003139 AddLinkerInputs(getToolChain(), Inputs, Args, CmdArgs);
Daniel Dunbarf7b8eec2009-06-29 20:52:51 +00003140
3141 if (!Args.hasArg(options::OPT_nostdlib) &&
3142 !Args.hasArg(options::OPT_nodefaultlibs)) {
Daniel Dunbar95c04572010-08-01 23:13:54 +00003143 if (D.CCCIsCXX) {
Daniel Dunbar132e35d2010-09-17 01:20:05 +00003144 getToolChain().AddCXXStdlibLibArgs(Args, CmdArgs);
Daniel Dunbar95c04572010-08-01 23:13:54 +00003145 CmdArgs.push_back("-lm");
3146 }
3147
Daniel Dunbar2bbcf662009-08-03 01:28:59 +00003148 // FIXME: For some reason GCC passes -lgcc before adding
3149 // the default system libraries. Just mimic this for now.
3150 CmdArgs.push_back("-lgcc");
Daniel Dunbarf7b8eec2009-06-29 20:52:51 +00003151
3152 if (Args.hasArg(options::OPT_pthread))
Chris Lattnera2e40212011-02-21 18:36:51 +00003153 CmdArgs.push_back("-lpthread");
Daniel Dunbar2bbcf662009-08-03 01:28:59 +00003154 if (!Args.hasArg(options::OPT_shared))
3155 CmdArgs.push_back("-lc");
3156 CmdArgs.push_back("-lgcc");
Daniel Dunbarf7b8eec2009-06-29 20:52:51 +00003157 }
3158
3159 if (!Args.hasArg(options::OPT_nostdlib) &&
3160 !Args.hasArg(options::OPT_nostartfiles)) {
3161 if (!Args.hasArg(options::OPT_shared))
Chris Lattner38e317d2010-07-07 16:01:42 +00003162 CmdArgs.push_back(Args.MakeArgString(
Daniel Dunbar4a7e8892010-07-14 18:46:23 +00003163 getToolChain().GetFilePath("crtend.o")));
Daniel Dunbarf7b8eec2009-06-29 20:52:51 +00003164 else
Chris Lattner38e317d2010-07-07 16:01:42 +00003165 CmdArgs.push_back(Args.MakeArgString(
Daniel Dunbar4a7e8892010-07-14 18:46:23 +00003166 getToolChain().GetFilePath("crtendS.o")));
Daniel Dunbarf7b8eec2009-06-29 20:52:51 +00003167 }
3168
3169 const char *Exec =
Daniel Dunbar4a7e8892010-07-14 18:46:23 +00003170 Args.MakeArgString(getToolChain().GetProgramPath("ld"));
Daniel Dunbar2fe238e2010-08-02 02:38:28 +00003171 C.addCommand(new Command(JA, *this, Exec, CmdArgs));
Daniel Dunbarf7b8eec2009-06-29 20:52:51 +00003172}
Ed Schoutenc66a5a32009-04-02 19:13:12 +00003173
Daniel Dunbar68a31d42009-03-31 17:45:15 +00003174void freebsd::Assemble::ConstructJob(Compilation &C, const JobAction &JA,
Daniel Dunbar2fe238e2010-08-02 02:38:28 +00003175 const InputInfo &Output,
Daniel Dunbarc21c4852009-04-08 23:54:23 +00003176 const InputInfoList &Inputs,
3177 const ArgList &Args,
Mike Stump1eb44332009-09-09 15:08:12 +00003178 const char *LinkingOutput) const {
Daniel Dunbar68a31d42009-03-31 17:45:15 +00003179 ArgStringList CmdArgs;
3180
Daniel Dunbar008f54a2009-04-01 19:36:32 +00003181 // When building 32-bit code on FreeBSD/amd64, we have to explicitly
3182 // instruct as in the base system to assemble 32-bit code.
3183 if (getToolChain().getArchName() == "i386")
Daniel Dunbar68a31d42009-03-31 17:45:15 +00003184 CmdArgs.push_back("--32");
3185
Michael J. Spencer20249a12010-10-21 03:16:25 +00003186
Eric Christophered734732010-03-02 02:41:08 +00003187 // Set byte order explicitly
3188 if (getToolChain().getArchName() == "mips")
3189 CmdArgs.push_back("-EB");
3190 else if (getToolChain().getArchName() == "mipsel")
3191 CmdArgs.push_back("-EL");
3192
Daniel Dunbar68a31d42009-03-31 17:45:15 +00003193 Args.AddAllArgValues(CmdArgs, options::OPT_Wa_COMMA,
3194 options::OPT_Xassembler);
3195
3196 CmdArgs.push_back("-o");
Daniel Dunbar7c1e4652010-08-02 02:38:21 +00003197 CmdArgs.push_back(Output.getFilename());
Daniel Dunbar68a31d42009-03-31 17:45:15 +00003198
3199 for (InputInfoList::const_iterator
3200 it = Inputs.begin(), ie = Inputs.end(); it != ie; ++it) {
3201 const InputInfo &II = *it;
Daniel Dunbar7c1e4652010-08-02 02:38:21 +00003202 CmdArgs.push_back(II.getFilename());
Daniel Dunbar68a31d42009-03-31 17:45:15 +00003203 }
3204
Daniel Dunbarc21c4852009-04-08 23:54:23 +00003205 const char *Exec =
Daniel Dunbar4a7e8892010-07-14 18:46:23 +00003206 Args.MakeArgString(getToolChain().GetProgramPath("as"));
Daniel Dunbar2fe238e2010-08-02 02:38:28 +00003207 C.addCommand(new Command(JA, *this, Exec, CmdArgs));
Daniel Dunbar68a31d42009-03-31 17:45:15 +00003208}
Daniel Dunbar008f54a2009-04-01 19:36:32 +00003209
3210void freebsd::Link::ConstructJob(Compilation &C, const JobAction &JA,
Daniel Dunbar2fe238e2010-08-02 02:38:28 +00003211 const InputInfo &Output,
Daniel Dunbarc21c4852009-04-08 23:54:23 +00003212 const InputInfoList &Inputs,
3213 const ArgList &Args,
Daniel Dunbara8304f62009-05-02 20:14:53 +00003214 const char *LinkingOutput) const {
Daniel Dunbaree788e72009-12-21 18:54:17 +00003215 const Driver &D = getToolChain().getDriver();
Daniel Dunbar008f54a2009-04-01 19:36:32 +00003216 ArgStringList CmdArgs;
3217
3218 if (Args.hasArg(options::OPT_static)) {
3219 CmdArgs.push_back("-Bstatic");
3220 } else {
Rafael Espindola65ba55d2010-11-11 02:17:51 +00003221 if (Args.hasArg(options::OPT_rdynamic))
3222 CmdArgs.push_back("-export-dynamic");
Daniel Dunbar008f54a2009-04-01 19:36:32 +00003223 CmdArgs.push_back("--eh-frame-hdr");
3224 if (Args.hasArg(options::OPT_shared)) {
3225 CmdArgs.push_back("-Bshareable");
3226 } else {
3227 CmdArgs.push_back("-dynamic-linker");
3228 CmdArgs.push_back("/libexec/ld-elf.so.1");
3229 }
3230 }
3231
3232 // When building 32-bit code on FreeBSD/amd64, we have to explicitly
3233 // instruct ld in the base system to link 32-bit code.
3234 if (getToolChain().getArchName() == "i386") {
3235 CmdArgs.push_back("-m");
3236 CmdArgs.push_back("elf_i386_fbsd");
3237 }
3238
Daniel Dunbar7c1e4652010-08-02 02:38:21 +00003239 if (Output.isFilename()) {
Daniel Dunbar008f54a2009-04-01 19:36:32 +00003240 CmdArgs.push_back("-o");
3241 CmdArgs.push_back(Output.getFilename());
3242 } else {
3243 assert(Output.isNothing() && "Invalid output.");
3244 }
3245
3246 if (!Args.hasArg(options::OPT_nostdlib) &&
3247 !Args.hasArg(options::OPT_nostartfiles)) {
3248 if (!Args.hasArg(options::OPT_shared)) {
Roman Divackyc16bb762011-02-10 16:59:40 +00003249 if (Args.hasArg(options::OPT_pg))
3250 CmdArgs.push_back(Args.MakeArgString(
3251 getToolChain().GetFilePath("gcrt1.o")));
3252 else
3253 CmdArgs.push_back(Args.MakeArgString(
3254 getToolChain().GetFilePath("crt1.o")));
Chris Lattner38e317d2010-07-07 16:01:42 +00003255 CmdArgs.push_back(Args.MakeArgString(
Daniel Dunbar4a7e8892010-07-14 18:46:23 +00003256 getToolChain().GetFilePath("crti.o")));
Chris Lattner38e317d2010-07-07 16:01:42 +00003257 CmdArgs.push_back(Args.MakeArgString(
Daniel Dunbar4a7e8892010-07-14 18:46:23 +00003258 getToolChain().GetFilePath("crtbegin.o")));
Daniel Dunbar008f54a2009-04-01 19:36:32 +00003259 } else {
Chris Lattner38e317d2010-07-07 16:01:42 +00003260 CmdArgs.push_back(Args.MakeArgString(
Daniel Dunbar4a7e8892010-07-14 18:46:23 +00003261 getToolChain().GetFilePath("crti.o")));
Chris Lattner38e317d2010-07-07 16:01:42 +00003262 CmdArgs.push_back(Args.MakeArgString(
Daniel Dunbar4a7e8892010-07-14 18:46:23 +00003263 getToolChain().GetFilePath("crtbeginS.o")));
Daniel Dunbar008f54a2009-04-01 19:36:32 +00003264 }
3265 }
3266
3267 Args.AddAllArgs(CmdArgs, options::OPT_L);
Roman Divacky80f0ab72011-01-30 08:12:24 +00003268 CmdArgs.push_back("-L/usr/lib");
Daniel Dunbar008f54a2009-04-01 19:36:32 +00003269 Args.AddAllArgs(CmdArgs, options::OPT_T_Group);
3270 Args.AddAllArgs(CmdArgs, options::OPT_e);
David Chisnallc7363772010-08-15 22:58:12 +00003271 Args.AddAllArgs(CmdArgs, options::OPT_s);
3272 Args.AddAllArgs(CmdArgs, options::OPT_t);
3273 Args.AddAllArgs(CmdArgs, options::OPT_Z_Flag);
3274 Args.AddAllArgs(CmdArgs, options::OPT_r);
Daniel Dunbar008f54a2009-04-01 19:36:32 +00003275
Daniel Dunbar2008fee2010-09-17 00:24:54 +00003276 AddLinkerInputs(getToolChain(), Inputs, Args, CmdArgs);
Daniel Dunbar008f54a2009-04-01 19:36:32 +00003277
3278 if (!Args.hasArg(options::OPT_nostdlib) &&
3279 !Args.hasArg(options::OPT_nodefaultlibs)) {
Daniel Dunbar20022632010-02-17 08:07:51 +00003280 if (D.CCCIsCXX) {
Daniel Dunbar132e35d2010-09-17 01:20:05 +00003281 getToolChain().AddCXXStdlibLibArgs(Args, CmdArgs);
Roman Divackyc16bb762011-02-10 16:59:40 +00003282 if (Args.hasArg(options::OPT_pg))
3283 CmdArgs.push_back("-lm_p");
3284 else
3285 CmdArgs.push_back("-lm");
Daniel Dunbar20022632010-02-17 08:07:51 +00003286 }
Daniel Dunbar008f54a2009-04-01 19:36:32 +00003287 // FIXME: For some reason GCC passes -lgcc and -lgcc_s before adding
3288 // the default system libraries. Just mimic this for now.
Roman Divackyc16bb762011-02-10 16:59:40 +00003289 if (Args.hasArg(options::OPT_pg))
3290 CmdArgs.push_back("-lgcc_p");
3291 else
3292 CmdArgs.push_back("-lgcc");
Daniel Dunbar008f54a2009-04-01 19:36:32 +00003293 if (Args.hasArg(options::OPT_static)) {
3294 CmdArgs.push_back("-lgcc_eh");
Roman Divackyc16bb762011-02-10 16:59:40 +00003295 } else if (Args.hasArg(options::OPT_pg)) {
3296 CmdArgs.push_back("-lgcc_eh_p");
Daniel Dunbar008f54a2009-04-01 19:36:32 +00003297 } else {
3298 CmdArgs.push_back("--as-needed");
3299 CmdArgs.push_back("-lgcc_s");
3300 CmdArgs.push_back("--no-as-needed");
3301 }
3302
Matt Beaumont-Gay24230262011-02-10 20:35:01 +00003303 if (Args.hasArg(options::OPT_pthread)) {
Roman Divackyc16bb762011-02-10 16:59:40 +00003304 if (Args.hasArg(options::OPT_pg))
3305 CmdArgs.push_back("-lpthread_p");
3306 else
3307 CmdArgs.push_back("-lpthread");
Matt Beaumont-Gay24230262011-02-10 20:35:01 +00003308 }
Daniel Dunbar008f54a2009-04-01 19:36:32 +00003309
Roman Divackyc16bb762011-02-10 16:59:40 +00003310 if (Args.hasArg(options::OPT_pg)) {
3311 if (Args.hasArg(options::OPT_shared))
3312 CmdArgs.push_back("-lc");
3313 else
3314 CmdArgs.push_back("-lc_p");
3315 CmdArgs.push_back("-lgcc_p");
3316 } else {
3317 CmdArgs.push_back("-lc");
3318 CmdArgs.push_back("-lgcc");
3319 }
3320
Daniel Dunbar008f54a2009-04-01 19:36:32 +00003321 if (Args.hasArg(options::OPT_static)) {
3322 CmdArgs.push_back("-lgcc_eh");
Roman Divackyc16bb762011-02-10 16:59:40 +00003323 } else if (Args.hasArg(options::OPT_pg)) {
3324 CmdArgs.push_back("-lgcc_eh_p");
Daniel Dunbar008f54a2009-04-01 19:36:32 +00003325 } else {
3326 CmdArgs.push_back("--as-needed");
3327 CmdArgs.push_back("-lgcc_s");
3328 CmdArgs.push_back("--no-as-needed");
3329 }
3330 }
3331
3332 if (!Args.hasArg(options::OPT_nostdlib) &&
3333 !Args.hasArg(options::OPT_nostartfiles)) {
3334 if (!Args.hasArg(options::OPT_shared))
Daniel Dunbar4a7e8892010-07-14 18:46:23 +00003335 CmdArgs.push_back(Args.MakeArgString(getToolChain().GetFilePath(
Chris Lattner38e317d2010-07-07 16:01:42 +00003336 "crtend.o")));
Daniel Dunbar008f54a2009-04-01 19:36:32 +00003337 else
Daniel Dunbar4a7e8892010-07-14 18:46:23 +00003338 CmdArgs.push_back(Args.MakeArgString(getToolChain().GetFilePath(
Chris Lattner38e317d2010-07-07 16:01:42 +00003339 "crtendS.o")));
Daniel Dunbar4a7e8892010-07-14 18:46:23 +00003340 CmdArgs.push_back(Args.MakeArgString(getToolChain().GetFilePath(
Chris Lattner38e317d2010-07-07 16:01:42 +00003341 "crtn.o")));
Daniel Dunbar008f54a2009-04-01 19:36:32 +00003342 }
3343
Daniel Dunbarc21c4852009-04-08 23:54:23 +00003344 const char *Exec =
Daniel Dunbar4a7e8892010-07-14 18:46:23 +00003345 Args.MakeArgString(getToolChain().GetProgramPath("ld"));
Daniel Dunbar2fe238e2010-08-02 02:38:28 +00003346 C.addCommand(new Command(JA, *this, Exec, CmdArgs));
Daniel Dunbar008f54a2009-04-01 19:36:32 +00003347}
Daniel Dunbar11e1b402009-05-02 18:28:39 +00003348
Benjamin Kramer8e50a962011-02-02 18:59:27 +00003349void netbsd::Assemble::ConstructJob(Compilation &C, const JobAction &JA,
3350 const InputInfo &Output,
3351 const InputInfoList &Inputs,
3352 const ArgList &Args,
3353 const char *LinkingOutput) const {
3354 ArgStringList CmdArgs;
3355
3356 // When building 32-bit code on NetBSD/amd64, we have to explicitly
3357 // instruct as in the base system to assemble 32-bit code.
3358 if (getToolChain().getArchName() == "i386")
3359 CmdArgs.push_back("--32");
3360
3361
3362 // Set byte order explicitly
3363 if (getToolChain().getArchName() == "mips")
3364 CmdArgs.push_back("-EB");
3365 else if (getToolChain().getArchName() == "mipsel")
3366 CmdArgs.push_back("-EL");
3367
3368 Args.AddAllArgValues(CmdArgs, options::OPT_Wa_COMMA,
3369 options::OPT_Xassembler);
3370
3371 CmdArgs.push_back("-o");
3372 CmdArgs.push_back(Output.getFilename());
3373
3374 for (InputInfoList::const_iterator
3375 it = Inputs.begin(), ie = Inputs.end(); it != ie; ++it) {
3376 const InputInfo &II = *it;
3377 CmdArgs.push_back(II.getFilename());
3378 }
3379
3380 const char *Exec =
3381 Args.MakeArgString(getToolChain().GetProgramPath("as"));
3382 C.addCommand(new Command(JA, *this, Exec, CmdArgs));
3383}
3384
3385void netbsd::Link::ConstructJob(Compilation &C, const JobAction &JA,
3386 const InputInfo &Output,
3387 const InputInfoList &Inputs,
3388 const ArgList &Args,
3389 const char *LinkingOutput) const {
3390 const Driver &D = getToolChain().getDriver();
3391 ArgStringList CmdArgs;
3392
3393 if (Args.hasArg(options::OPT_static)) {
3394 CmdArgs.push_back("-Bstatic");
3395 } else {
3396 if (Args.hasArg(options::OPT_rdynamic))
3397 CmdArgs.push_back("-export-dynamic");
3398 CmdArgs.push_back("--eh-frame-hdr");
3399 if (Args.hasArg(options::OPT_shared)) {
3400 CmdArgs.push_back("-Bshareable");
3401 } else {
3402 CmdArgs.push_back("-dynamic-linker");
3403 CmdArgs.push_back("/libexec/ld.elf_so");
3404 }
3405 }
3406
3407 // When building 32-bit code on NetBSD/amd64, we have to explicitly
3408 // instruct ld in the base system to link 32-bit code.
3409 if (getToolChain().getArchName() == "i386") {
3410 CmdArgs.push_back("-m");
3411 CmdArgs.push_back("elf_i386");
3412 }
3413
3414 if (Output.isFilename()) {
3415 CmdArgs.push_back("-o");
3416 CmdArgs.push_back(Output.getFilename());
3417 } else {
3418 assert(Output.isNothing() && "Invalid output.");
3419 }
3420
3421 if (!Args.hasArg(options::OPT_nostdlib) &&
3422 !Args.hasArg(options::OPT_nostartfiles)) {
3423 if (!Args.hasArg(options::OPT_shared)) {
3424 CmdArgs.push_back(Args.MakeArgString(
3425 getToolChain().GetFilePath("crt0.o")));
3426 CmdArgs.push_back(Args.MakeArgString(
3427 getToolChain().GetFilePath("crti.o")));
3428 CmdArgs.push_back(Args.MakeArgString(
3429 getToolChain().GetFilePath("crtbegin.o")));
3430 } else {
3431 CmdArgs.push_back(Args.MakeArgString(
3432 getToolChain().GetFilePath("crti.o")));
3433 CmdArgs.push_back(Args.MakeArgString(
3434 getToolChain().GetFilePath("crtbeginS.o")));
3435 }
3436 }
3437
3438 Args.AddAllArgs(CmdArgs, options::OPT_L);
3439 Args.AddAllArgs(CmdArgs, options::OPT_T_Group);
3440 Args.AddAllArgs(CmdArgs, options::OPT_e);
3441 Args.AddAllArgs(CmdArgs, options::OPT_s);
3442 Args.AddAllArgs(CmdArgs, options::OPT_t);
3443 Args.AddAllArgs(CmdArgs, options::OPT_Z_Flag);
3444 Args.AddAllArgs(CmdArgs, options::OPT_r);
3445
3446 AddLinkerInputs(getToolChain(), Inputs, Args, CmdArgs);
3447
3448 if (!Args.hasArg(options::OPT_nostdlib) &&
3449 !Args.hasArg(options::OPT_nodefaultlibs)) {
3450 if (D.CCCIsCXX) {
3451 getToolChain().AddCXXStdlibLibArgs(Args, CmdArgs);
3452 CmdArgs.push_back("-lm");
3453 }
3454 // FIXME: For some reason GCC passes -lgcc and -lgcc_s before adding
3455 // the default system libraries. Just mimic this for now.
3456 CmdArgs.push_back("-lgcc");
3457 if (Args.hasArg(options::OPT_static)) {
3458 CmdArgs.push_back("-lgcc_eh");
3459 } else {
3460 CmdArgs.push_back("--as-needed");
3461 CmdArgs.push_back("-lgcc_s");
3462 CmdArgs.push_back("--no-as-needed");
3463 }
3464
3465 if (Args.hasArg(options::OPT_pthread))
3466 CmdArgs.push_back("-lpthread");
3467 CmdArgs.push_back("-lc");
3468
3469 CmdArgs.push_back("-lgcc");
3470 if (Args.hasArg(options::OPT_static)) {
3471 CmdArgs.push_back("-lgcc_eh");
3472 } else {
3473 CmdArgs.push_back("--as-needed");
3474 CmdArgs.push_back("-lgcc_s");
3475 CmdArgs.push_back("--no-as-needed");
3476 }
3477 }
3478
3479 if (!Args.hasArg(options::OPT_nostdlib) &&
3480 !Args.hasArg(options::OPT_nostartfiles)) {
3481 if (!Args.hasArg(options::OPT_shared))
3482 CmdArgs.push_back(Args.MakeArgString(getToolChain().GetFilePath(
3483 "crtend.o")));
3484 else
3485 CmdArgs.push_back(Args.MakeArgString(getToolChain().GetFilePath(
3486 "crtendS.o")));
3487 CmdArgs.push_back(Args.MakeArgString(getToolChain().GetFilePath(
3488 "crtn.o")));
3489 }
3490
3491 const char *Exec =
3492 Args.MakeArgString(getToolChain().GetProgramPath("ld"));
3493 C.addCommand(new Command(JA, *this, Exec, CmdArgs));
3494}
3495
Rafael Espindolaba30bbe2010-08-10 00:25:48 +00003496void linuxtools::Assemble::ConstructJob(Compilation &C, const JobAction &JA,
3497 const InputInfo &Output,
3498 const InputInfoList &Inputs,
3499 const ArgList &Args,
3500 const char *LinkingOutput) const {
3501 ArgStringList CmdArgs;
3502
3503 // Add --32/--64 to make sure we get the format we want.
3504 // This is incomplete
3505 if (getToolChain().getArch() == llvm::Triple::x86) {
3506 CmdArgs.push_back("--32");
3507 } else if (getToolChain().getArch() == llvm::Triple::x86_64) {
3508 CmdArgs.push_back("--64");
3509 } else if (getToolChain().getArch() == llvm::Triple::arm) {
3510 llvm::StringRef MArch = getToolChain().getArchName();
3511 if (MArch == "armv7" || MArch == "armv7a" || MArch == "armv7-a")
3512 CmdArgs.push_back("-mfpu=neon");
3513 }
3514
3515 Args.AddAllArgValues(CmdArgs, options::OPT_Wa_COMMA,
3516 options::OPT_Xassembler);
3517
3518 CmdArgs.push_back("-o");
3519 CmdArgs.push_back(Output.getFilename());
3520
3521 for (InputInfoList::const_iterator
3522 it = Inputs.begin(), ie = Inputs.end(); it != ie; ++it) {
3523 const InputInfo &II = *it;
3524 CmdArgs.push_back(II.getFilename());
3525 }
3526
3527 const char *Exec =
3528 Args.MakeArgString(getToolChain().GetProgramPath("as"));
3529 C.addCommand(new Command(JA, *this, Exec, CmdArgs));
3530}
3531
Rafael Espindolac1da9812010-11-07 20:14:31 +00003532void linuxtools::Link::ConstructJob(Compilation &C, const JobAction &JA,
3533 const InputInfo &Output,
3534 const InputInfoList &Inputs,
3535 const ArgList &Args,
3536 const char *LinkingOutput) const {
3537 const toolchains::Linux& ToolChain =
3538 static_cast<const toolchains::Linux&>(getToolChain());
3539 const Driver &D = ToolChain.getDriver();
3540 ArgStringList CmdArgs;
3541
Rafael Espindola26f14c32010-11-15 18:28:16 +00003542 // Silence warning for "clang -g foo.o -o foo"
3543 Args.ClaimAllArgs(options::OPT_g_Group);
Rafael Espindola7f6458b2010-11-17 20:37:10 +00003544 // and for "clang -g foo.o -o foo". Other warning options are already
3545 // handled somewhere else.
3546 Args.ClaimAllArgs(options::OPT_w);
Rafael Espindola26f14c32010-11-15 18:28:16 +00003547
Rafael Espindolac1da9812010-11-07 20:14:31 +00003548 if (Arg *A = Args.getLastArg(options::OPT__sysroot_EQ)) {
3549 CmdArgs.push_back("--sysroot");
3550 CmdArgs.push_back(A->getValue(Args));
3551 }
3552
Rafael Espindolafdda1712010-11-17 22:26:15 +00003553 if (Args.hasArg(options::OPT_pie))
3554 CmdArgs.push_back("-pie");
3555
Rafael Espindoladc1b76d2010-11-07 22:57:16 +00003556 if (Args.hasArg(options::OPT_rdynamic))
3557 CmdArgs.push_back("-export-dynamic");
3558
Rafael Espindolae0e6d3b2010-11-11 19:34:42 +00003559 if (Args.hasArg(options::OPT_s))
3560 CmdArgs.push_back("-s");
3561
Rafael Espindolac1da9812010-11-07 20:14:31 +00003562 for (std::vector<std::string>::const_iterator i = ToolChain.ExtraOpts.begin(),
3563 e = ToolChain.ExtraOpts.end();
3564 i != e; ++i)
3565 CmdArgs.push_back(i->c_str());
3566
3567 if (!Args.hasArg(options::OPT_static)) {
3568 CmdArgs.push_back("--eh-frame-hdr");
3569 }
3570
3571 CmdArgs.push_back("-m");
3572 if (ToolChain.getArch() == llvm::Triple::x86)
3573 CmdArgs.push_back("elf_i386");
3574 else if (ToolChain.getArch() == llvm::Triple::arm)
3575 CmdArgs.push_back("armelf_linux_eabi");
3576 else
3577 CmdArgs.push_back("elf_x86_64");
3578
3579 if (Args.hasArg(options::OPT_static)) {
3580 if (ToolChain.getArch() == llvm::Triple::arm)
3581 CmdArgs.push_back("-Bstatic");
3582 else
3583 CmdArgs.push_back("-static");
3584 } else if (Args.hasArg(options::OPT_shared)) {
3585 CmdArgs.push_back("-shared");
3586 }
3587
3588 if (ToolChain.getArch() == llvm::Triple::arm ||
3589 (!Args.hasArg(options::OPT_static) &&
3590 !Args.hasArg(options::OPT_shared))) {
3591 CmdArgs.push_back("-dynamic-linker");
3592 if (ToolChain.getArch() == llvm::Triple::x86)
3593 CmdArgs.push_back("/lib/ld-linux.so.2");
3594 else if (ToolChain.getArch() == llvm::Triple::arm)
3595 CmdArgs.push_back("/lib/ld-linux.so.3");
3596 else
3597 CmdArgs.push_back("/lib64/ld-linux-x86-64.so.2");
3598 }
3599
3600 CmdArgs.push_back("-o");
3601 CmdArgs.push_back(Output.getFilename());
3602
Rafael Espindola49c64fd2010-12-01 01:52:43 +00003603 if (!Args.hasArg(options::OPT_nostdlib) &&
3604 !Args.hasArg(options::OPT_nostartfiles)) {
Rafael Espindolafdda1712010-11-17 22:26:15 +00003605 const char *crt1 = NULL;
3606 if (!Args.hasArg(options::OPT_shared)){
3607 if (Args.hasArg(options::OPT_pie))
3608 crt1 = "Scrt1.o";
3609 else
3610 crt1 = "crt1.o";
3611 }
3612 if (crt1)
3613 CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath(crt1)));
Rafael Espindolac1da9812010-11-07 20:14:31 +00003614
Rafael Espindola89414b32010-11-12 03:00:39 +00003615 CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath("crti.o")));
Rafael Espindolac1da9812010-11-07 20:14:31 +00003616
Rafael Espindola89414b32010-11-12 03:00:39 +00003617 const char *crtbegin;
3618 if (Args.hasArg(options::OPT_static))
3619 crtbegin = "crtbeginT.o";
Rafael Espindolafdda1712010-11-17 22:26:15 +00003620 else if (Args.hasArg(options::OPT_shared) || Args.hasArg(options::OPT_pie))
Rafael Espindola89414b32010-11-12 03:00:39 +00003621 crtbegin = "crtbeginS.o";
3622 else
3623 crtbegin = "crtbegin.o";
3624 CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath(crtbegin)));
3625 }
Rafael Espindolac1da9812010-11-07 20:14:31 +00003626
3627 Args.AddAllArgs(CmdArgs, options::OPT_L);
3628
3629 const ToolChain::path_list Paths = ToolChain.getFilePaths();
3630
3631 for (ToolChain::path_list::const_iterator i = Paths.begin(),
3632 e = Paths.end();
3633 i != e; ++i) {
3634 const std::string &s = *i;
3635 CmdArgs.push_back(Args.MakeArgString(std::string("-L") + s));
3636 }
3637
3638 AddLinkerInputs(ToolChain, Inputs, Args, CmdArgs);
3639
Rafael Espindola89414b32010-11-12 03:00:39 +00003640 if (D.CCCIsCXX && !Args.hasArg(options::OPT_nostdlib)) {
Rafael Espindolac1da9812010-11-07 20:14:31 +00003641 ToolChain.AddCXXStdlibLibArgs(Args, CmdArgs);
3642 CmdArgs.push_back("-lm");
3643 }
3644
3645 if (Args.hasArg(options::OPT_static))
3646 CmdArgs.push_back("--start-group");
3647
Rafael Espindola89414b32010-11-12 03:00:39 +00003648 if (!Args.hasArg(options::OPT_nostdlib)) {
3649 if (!D.CCCIsCXX)
Rafael Espindolac1da9812010-11-07 20:14:31 +00003650 CmdArgs.push_back("-lgcc");
Rafael Espindola89414b32010-11-12 03:00:39 +00003651
3652 if (Args.hasArg(options::OPT_static)) {
3653 if (D.CCCIsCXX)
3654 CmdArgs.push_back("-lgcc");
3655 } else {
3656 if (!D.CCCIsCXX)
3657 CmdArgs.push_back("--as-needed");
3658 CmdArgs.push_back("-lgcc_s");
3659 if (!D.CCCIsCXX)
3660 CmdArgs.push_back("--no-as-needed");
3661 }
3662
3663 if (Args.hasArg(options::OPT_static))
3664 CmdArgs.push_back("-lgcc_eh");
3665 else if (!Args.hasArg(options::OPT_shared) && D.CCCIsCXX)
3666 CmdArgs.push_back("-lgcc");
3667
3668 if (Args.hasArg(options::OPT_pthread) ||
3669 Args.hasArg(options::OPT_pthreads))
3670 CmdArgs.push_back("-lpthread");
3671
3672 CmdArgs.push_back("-lc");
3673
3674 if (Args.hasArg(options::OPT_static))
3675 CmdArgs.push_back("--end-group");
3676 else {
3677 if (!D.CCCIsCXX)
3678 CmdArgs.push_back("-lgcc");
3679
3680 if (!D.CCCIsCXX)
3681 CmdArgs.push_back("--as-needed");
3682 CmdArgs.push_back("-lgcc_s");
3683 if (!D.CCCIsCXX)
3684 CmdArgs.push_back("--no-as-needed");
3685
3686 if (!Args.hasArg(options::OPT_shared) && D.CCCIsCXX)
3687 CmdArgs.push_back("-lgcc");
3688 }
3689
Rafael Espindolafdda1712010-11-17 22:26:15 +00003690
Rafael Espindola49c64fd2010-12-01 01:52:43 +00003691 if (!Args.hasArg(options::OPT_nostartfiles)) {
3692 const char *crtend;
3693 if (Args.hasArg(options::OPT_shared) || Args.hasArg(options::OPT_pie))
3694 crtend = "crtendS.o";
3695 else
3696 crtend = "crtend.o";
Rafael Espindola89414b32010-11-12 03:00:39 +00003697
Rafael Espindola49c64fd2010-12-01 01:52:43 +00003698 CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath(crtend)));
3699 CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath("crtn.o")));
3700 }
Rafael Espindolac1da9812010-11-07 20:14:31 +00003701 }
3702
Rafael Espindolac1da9812010-11-07 20:14:31 +00003703 if (Args.hasArg(options::OPT_use_gold_plugin)) {
3704 CmdArgs.push_back("-plugin");
3705 std::string Plugin = ToolChain.getDriver().Dir + "/../lib/LLVMgold.so";
3706 CmdArgs.push_back(Args.MakeArgString(Plugin));
3707 }
3708
3709 C.addCommand(new Command(JA, *this, ToolChain.Linker.c_str(), CmdArgs));
3710}
Rafael Espindolaba30bbe2010-08-10 00:25:48 +00003711
Chris Lattner38e317d2010-07-07 16:01:42 +00003712void minix::Assemble::ConstructJob(Compilation &C, const JobAction &JA,
Daniel Dunbar2fe238e2010-08-02 02:38:28 +00003713 const InputInfo &Output,
3714 const InputInfoList &Inputs,
3715 const ArgList &Args,
3716 const char *LinkingOutput) const {
Chris Lattner38e317d2010-07-07 16:01:42 +00003717 ArgStringList CmdArgs;
3718
3719 Args.AddAllArgValues(CmdArgs, options::OPT_Wa_COMMA,
3720 options::OPT_Xassembler);
3721
3722 CmdArgs.push_back("-o");
Daniel Dunbar7c1e4652010-08-02 02:38:21 +00003723 CmdArgs.push_back(Output.getFilename());
Chris Lattner38e317d2010-07-07 16:01:42 +00003724
3725 for (InputInfoList::const_iterator
3726 it = Inputs.begin(), ie = Inputs.end(); it != ie; ++it) {
3727 const InputInfo &II = *it;
Daniel Dunbar7c1e4652010-08-02 02:38:21 +00003728 CmdArgs.push_back(II.getFilename());
Chris Lattner38e317d2010-07-07 16:01:42 +00003729 }
3730
3731 const char *Exec =
Daniel Dunbar4a7e8892010-07-14 18:46:23 +00003732 Args.MakeArgString(getToolChain().GetProgramPath("gas"));
Daniel Dunbar2fe238e2010-08-02 02:38:28 +00003733 C.addCommand(new Command(JA, *this, Exec, CmdArgs));
Chris Lattner38e317d2010-07-07 16:01:42 +00003734}
3735
3736void minix::Link::ConstructJob(Compilation &C, const JobAction &JA,
Daniel Dunbar2fe238e2010-08-02 02:38:28 +00003737 const InputInfo &Output,
3738 const InputInfoList &Inputs,
3739 const ArgList &Args,
3740 const char *LinkingOutput) const {
Chris Lattner38e317d2010-07-07 16:01:42 +00003741 const Driver &D = getToolChain().getDriver();
3742 ArgStringList CmdArgs;
3743
Daniel Dunbar7c1e4652010-08-02 02:38:21 +00003744 if (Output.isFilename()) {
Chris Lattner38e317d2010-07-07 16:01:42 +00003745 CmdArgs.push_back("-o");
3746 CmdArgs.push_back(Output.getFilename());
3747 } else {
3748 assert(Output.isNothing() && "Invalid output.");
3749 }
3750
3751 if (!Args.hasArg(options::OPT_nostdlib) &&
3752 !Args.hasArg(options::OPT_nostartfiles))
Daniel Dunbar4a7e8892010-07-14 18:46:23 +00003753 CmdArgs.push_back(Args.MakeArgString(getToolChain().GetFilePath(
Chris Lattner38e317d2010-07-07 16:01:42 +00003754 "/usr/gnu/lib/crtso.o")));
3755
3756 Args.AddAllArgs(CmdArgs, options::OPT_L);
3757 Args.AddAllArgs(CmdArgs, options::OPT_T_Group);
3758 Args.AddAllArgs(CmdArgs, options::OPT_e);
3759
Daniel Dunbar2008fee2010-09-17 00:24:54 +00003760 AddLinkerInputs(getToolChain(), Inputs, Args, CmdArgs);
Chris Lattner38e317d2010-07-07 16:01:42 +00003761
3762 if (!Args.hasArg(options::OPT_nostdlib) &&
3763 !Args.hasArg(options::OPT_nodefaultlibs)) {
3764 if (D.CCCIsCXX) {
Daniel Dunbar132e35d2010-09-17 01:20:05 +00003765 getToolChain().AddCXXStdlibLibArgs(Args, CmdArgs);
Chris Lattner38e317d2010-07-07 16:01:42 +00003766 CmdArgs.push_back("-lm");
3767 }
3768
3769 if (Args.hasArg(options::OPT_pthread))
3770 CmdArgs.push_back("-lpthread");
3771 CmdArgs.push_back("-lc");
3772 CmdArgs.push_back("-lgcc");
3773 CmdArgs.push_back("-L/usr/gnu/lib");
3774 // FIXME: fill in the correct search path for the final
3775 // support libraries.
3776 CmdArgs.push_back("-L/usr/gnu/lib/gcc/i686-pc-minix/4.4.3");
3777 }
3778
3779 if (!Args.hasArg(options::OPT_nostdlib) &&
3780 !Args.hasArg(options::OPT_nostartfiles)) {
Daniel Dunbar4a7e8892010-07-14 18:46:23 +00003781 CmdArgs.push_back(Args.MakeArgString(getToolChain().GetFilePath(
Chris Lattner38e317d2010-07-07 16:01:42 +00003782 "/usr/gnu/lib/libend.a")));
3783 }
3784
3785 const char *Exec =
Daniel Dunbar4a7e8892010-07-14 18:46:23 +00003786 Args.MakeArgString(getToolChain().GetProgramPath("/usr/gnu/bin/gld"));
Daniel Dunbar2fe238e2010-08-02 02:38:28 +00003787 C.addCommand(new Command(JA, *this, Exec, CmdArgs));
Chris Lattner38e317d2010-07-07 16:01:42 +00003788}
3789
Daniel Dunbar11e1b402009-05-02 18:28:39 +00003790/// DragonFly Tools
3791
3792// For now, DragonFly Assemble does just about the same as for
3793// FreeBSD, but this may change soon.
3794void dragonfly::Assemble::ConstructJob(Compilation &C, const JobAction &JA,
Daniel Dunbar2fe238e2010-08-02 02:38:28 +00003795 const InputInfo &Output,
Daniel Dunbar294691e2009-11-04 06:24:38 +00003796 const InputInfoList &Inputs,
3797 const ArgList &Args,
3798 const char *LinkingOutput) const {
Daniel Dunbar11e1b402009-05-02 18:28:39 +00003799 ArgStringList CmdArgs;
3800
3801 // When building 32-bit code on DragonFly/pc64, we have to explicitly
3802 // instruct as in the base system to assemble 32-bit code.
3803 if (getToolChain().getArchName() == "i386")
3804 CmdArgs.push_back("--32");
3805
3806 Args.AddAllArgValues(CmdArgs, options::OPT_Wa_COMMA,
3807 options::OPT_Xassembler);
3808
3809 CmdArgs.push_back("-o");
Daniel Dunbar7c1e4652010-08-02 02:38:21 +00003810 CmdArgs.push_back(Output.getFilename());
Daniel Dunbar11e1b402009-05-02 18:28:39 +00003811
3812 for (InputInfoList::const_iterator
3813 it = Inputs.begin(), ie = Inputs.end(); it != ie; ++it) {
3814 const InputInfo &II = *it;
Daniel Dunbar7c1e4652010-08-02 02:38:21 +00003815 CmdArgs.push_back(II.getFilename());
Daniel Dunbar11e1b402009-05-02 18:28:39 +00003816 }
3817
3818 const char *Exec =
Daniel Dunbar4a7e8892010-07-14 18:46:23 +00003819 Args.MakeArgString(getToolChain().GetProgramPath("as"));
Daniel Dunbar2fe238e2010-08-02 02:38:28 +00003820 C.addCommand(new Command(JA, *this, Exec, CmdArgs));
Daniel Dunbar11e1b402009-05-02 18:28:39 +00003821}
3822
3823void dragonfly::Link::ConstructJob(Compilation &C, const JobAction &JA,
Daniel Dunbar2fe238e2010-08-02 02:38:28 +00003824 const InputInfo &Output,
3825 const InputInfoList &Inputs,
3826 const ArgList &Args,
3827 const char *LinkingOutput) const {
Daniel Dunbaree788e72009-12-21 18:54:17 +00003828 const Driver &D = getToolChain().getDriver();
Daniel Dunbar11e1b402009-05-02 18:28:39 +00003829 ArgStringList CmdArgs;
3830
3831 if (Args.hasArg(options::OPT_static)) {
3832 CmdArgs.push_back("-Bstatic");
3833 } else {
3834 if (Args.hasArg(options::OPT_shared))
3835 CmdArgs.push_back("-Bshareable");
3836 else {
3837 CmdArgs.push_back("-dynamic-linker");
3838 CmdArgs.push_back("/usr/libexec/ld-elf.so.2");
3839 }
3840 }
3841
3842 // When building 32-bit code on DragonFly/pc64, we have to explicitly
3843 // instruct ld in the base system to link 32-bit code.
3844 if (getToolChain().getArchName() == "i386") {
3845 CmdArgs.push_back("-m");
3846 CmdArgs.push_back("elf_i386");
3847 }
3848
Daniel Dunbar7c1e4652010-08-02 02:38:21 +00003849 if (Output.isFilename()) {
Daniel Dunbar11e1b402009-05-02 18:28:39 +00003850 CmdArgs.push_back("-o");
3851 CmdArgs.push_back(Output.getFilename());
3852 } else {
3853 assert(Output.isNothing() && "Invalid output.");
3854 }
3855
3856 if (!Args.hasArg(options::OPT_nostdlib) &&
3857 !Args.hasArg(options::OPT_nostartfiles)) {
3858 if (!Args.hasArg(options::OPT_shared)) {
Chris Lattner38e317d2010-07-07 16:01:42 +00003859 CmdArgs.push_back(
Daniel Dunbar4a7e8892010-07-14 18:46:23 +00003860 Args.MakeArgString(getToolChain().GetFilePath("crt1.o")));
Chris Lattner38e317d2010-07-07 16:01:42 +00003861 CmdArgs.push_back(
Daniel Dunbar4a7e8892010-07-14 18:46:23 +00003862 Args.MakeArgString(getToolChain().GetFilePath("crti.o")));
Chris Lattner38e317d2010-07-07 16:01:42 +00003863 CmdArgs.push_back(
Daniel Dunbar4a7e8892010-07-14 18:46:23 +00003864 Args.MakeArgString(getToolChain().GetFilePath("crtbegin.o")));
Daniel Dunbar11e1b402009-05-02 18:28:39 +00003865 } else {
Chris Lattner38e317d2010-07-07 16:01:42 +00003866 CmdArgs.push_back(
Daniel Dunbar4a7e8892010-07-14 18:46:23 +00003867 Args.MakeArgString(getToolChain().GetFilePath("crti.o")));
Chris Lattner38e317d2010-07-07 16:01:42 +00003868 CmdArgs.push_back(
Daniel Dunbar4a7e8892010-07-14 18:46:23 +00003869 Args.MakeArgString(getToolChain().GetFilePath("crtbeginS.o")));
Daniel Dunbar11e1b402009-05-02 18:28:39 +00003870 }
3871 }
3872
3873 Args.AddAllArgs(CmdArgs, options::OPT_L);
3874 Args.AddAllArgs(CmdArgs, options::OPT_T_Group);
3875 Args.AddAllArgs(CmdArgs, options::OPT_e);
3876
Daniel Dunbar2008fee2010-09-17 00:24:54 +00003877 AddLinkerInputs(getToolChain(), Inputs, Args, CmdArgs);
Daniel Dunbar11e1b402009-05-02 18:28:39 +00003878
3879 if (!Args.hasArg(options::OPT_nostdlib) &&
3880 !Args.hasArg(options::OPT_nodefaultlibs)) {
3881 // FIXME: GCC passes on -lgcc, -lgcc_pic and a whole lot of
3882 // rpaths
3883 CmdArgs.push_back("-L/usr/lib/gcc41");
3884
3885 if (!Args.hasArg(options::OPT_static)) {
3886 CmdArgs.push_back("-rpath");
3887 CmdArgs.push_back("/usr/lib/gcc41");
3888
3889 CmdArgs.push_back("-rpath-link");
3890 CmdArgs.push_back("/usr/lib/gcc41");
3891
3892 CmdArgs.push_back("-rpath");
3893 CmdArgs.push_back("/usr/lib");
3894
3895 CmdArgs.push_back("-rpath-link");
3896 CmdArgs.push_back("/usr/lib");
3897 }
3898
Rafael Espindola405861d2010-07-20 12:59:03 +00003899 if (D.CCCIsCXX) {
Daniel Dunbar132e35d2010-09-17 01:20:05 +00003900 getToolChain().AddCXXStdlibLibArgs(Args, CmdArgs);
Rafael Espindola405861d2010-07-20 12:59:03 +00003901 CmdArgs.push_back("-lm");
3902 }
3903
Daniel Dunbar11e1b402009-05-02 18:28:39 +00003904 if (Args.hasArg(options::OPT_shared)) {
3905 CmdArgs.push_back("-lgcc_pic");
3906 } else {
3907 CmdArgs.push_back("-lgcc");
3908 }
3909
3910
3911 if (Args.hasArg(options::OPT_pthread))
Mike Stump4d63f8b2009-10-31 20:11:46 +00003912 CmdArgs.push_back("-lpthread");
Daniel Dunbar11e1b402009-05-02 18:28:39 +00003913
3914 if (!Args.hasArg(options::OPT_nolibc)) {
3915 CmdArgs.push_back("-lc");
3916 }
3917
3918 if (Args.hasArg(options::OPT_shared)) {
3919 CmdArgs.push_back("-lgcc_pic");
3920 } else {
3921 CmdArgs.push_back("-lgcc");
3922 }
3923 }
3924
3925 if (!Args.hasArg(options::OPT_nostdlib) &&
3926 !Args.hasArg(options::OPT_nostartfiles)) {
3927 if (!Args.hasArg(options::OPT_shared))
Chris Lattner38e317d2010-07-07 16:01:42 +00003928 CmdArgs.push_back(Args.MakeArgString(
Daniel Dunbar4a7e8892010-07-14 18:46:23 +00003929 getToolChain().GetFilePath("crtend.o")));
Daniel Dunbar11e1b402009-05-02 18:28:39 +00003930 else
Chris Lattner38e317d2010-07-07 16:01:42 +00003931 CmdArgs.push_back(Args.MakeArgString(
Daniel Dunbar4a7e8892010-07-14 18:46:23 +00003932 getToolChain().GetFilePath("crtendS.o")));
Chris Lattner38e317d2010-07-07 16:01:42 +00003933 CmdArgs.push_back(Args.MakeArgString(
Daniel Dunbar4a7e8892010-07-14 18:46:23 +00003934 getToolChain().GetFilePath("crtn.o")));
Daniel Dunbar11e1b402009-05-02 18:28:39 +00003935 }
3936
3937 const char *Exec =
Daniel Dunbar4a7e8892010-07-14 18:46:23 +00003938 Args.MakeArgString(getToolChain().GetProgramPath("ld"));
Daniel Dunbar2fe238e2010-08-02 02:38:28 +00003939 C.addCommand(new Command(JA, *this, Exec, CmdArgs));
Daniel Dunbar11e1b402009-05-02 18:28:39 +00003940}
Michael J. Spencerff58e362010-08-21 21:55:07 +00003941
3942void visualstudio::Link::ConstructJob(Compilation &C, const JobAction &JA,
3943 const InputInfo &Output,
3944 const InputInfoList &Inputs,
3945 const ArgList &Args,
3946 const char *LinkingOutput) const {
Michael J. Spencerff58e362010-08-21 21:55:07 +00003947 ArgStringList CmdArgs;
3948
3949 if (Output.isFilename()) {
Daniel Dunbare5a37f42010-09-17 00:45:02 +00003950 CmdArgs.push_back(Args.MakeArgString(std::string("-out:") +
3951 Output.getFilename()));
Michael J. Spencerff58e362010-08-21 21:55:07 +00003952 } else {
3953 assert(Output.isNothing() && "Invalid output.");
3954 }
3955
3956 if (!Args.hasArg(options::OPT_nostdlib) &&
3957 !Args.hasArg(options::OPT_nostartfiles)) {
3958 CmdArgs.push_back("-defaultlib:libcmt");
3959 }
3960
3961 CmdArgs.push_back("-nologo");
3962
Daniel Dunbar2008fee2010-09-17 00:24:54 +00003963 AddLinkerInputs(getToolChain(), Inputs, Args, CmdArgs);
Michael J. Spencerff58e362010-08-21 21:55:07 +00003964
3965 const char *Exec =
Daniel Dunbar2008fee2010-09-17 00:24:54 +00003966 Args.MakeArgString(getToolChain().GetProgramPath("link.exe"));
Michael J. Spencerff58e362010-08-21 21:55:07 +00003967 C.addCommand(new Command(JA, *this, Exec, CmdArgs));
3968}