blob: beaeade836cec340a79b335e1265d2d3ebb84043 [file] [log] [blame]
Nick Lewyckye47c2452010-09-23 23:48:20 +00001//===--- Tools.cpp - Tools Implementations --------------------------------===//
Daniel Dunbar1a093d22009-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 Dunbara2aedc62009-03-18 10:01:51 +000012#include "clang/Driver/Action.h"
Daniel Dunbar04c4c2c2009-03-18 07:06:02 +000013#include "clang/Driver/Arg.h"
Daniel Dunbara3246a02009-03-18 08:07:30 +000014#include "clang/Driver/ArgList.h"
Daniel Dunbar1a8a2e82009-10-29 02:39:57 +000015#include "clang/Driver/Driver.h"
16#include "clang/Driver/DriverDiagnostic.h"
Daniel Dunbar04c4c2c2009-03-18 07:06:02 +000017#include "clang/Driver/Compilation.h"
18#include "clang/Driver/Job.h"
Daniel Dunbara3246a02009-03-18 08:07:30 +000019#include "clang/Driver/HostInfo.h"
20#include "clang/Driver/Option.h"
Daniel Dunbarda13faf2009-11-19 04:25:22 +000021#include "clang/Driver/Options.h"
Daniel Dunbara3246a02009-03-18 08:07:30 +000022#include "clang/Driver/ToolChain.h"
Daniel Dunbar04c4c2c2009-03-18 07:06:02 +000023#include "clang/Driver/Util.h"
24
Daniel Dunbar1c28f1e2009-09-09 22:32:48 +000025#include "llvm/ADT/SmallString.h"
Douglas Gregorf7b87cb2009-10-29 00:41:01 +000026#include "llvm/ADT/StringSwitch.h"
Daniel Dunbarb4a3e432009-09-09 22:32:34 +000027#include "llvm/ADT/Twine.h"
Michael J. Spencerf6efe582011-01-10 02:34:13 +000028#include "llvm/Support/FileSystem.h"
Daniel Dunbarc1964212009-03-26 16:23:12 +000029#include "llvm/Support/Format.h"
30#include "llvm/Support/raw_ostream.h"
Michael J. Spencer8aaf4992010-11-29 18:12:39 +000031#include "llvm/Support/Host.h"
32#include "llvm/Support/Process.h"
Daniel Dunbar04c4c2c2009-03-18 07:06:02 +000033
34#include "InputInfo.h"
Daniel Dunbarc1964212009-03-26 16:23:12 +000035#include "ToolChains.h"
Daniel Dunbar04c4c2c2009-03-18 07:06:02 +000036
Daniel Dunbar1a093d22009-03-18 06:00:36 +000037using namespace clang::driver;
38using namespace clang::driver::tools;
39
Joerg Sonnenbergerfcc3ec92011-03-21 14:01:40 +000040/// FindTargetProgramPath - Return path of the target specific version of
41/// ProgName. If it doesn't exist, return path of ProgName itself.
42static std::string FindTargetProgramPath(const ToolChain &TheToolChain,
43 const char *ProgName) {
44 std::string Executable(TheToolChain.getTripleString() + "-" + ProgName);
45 std::string Path(TheToolChain.GetProgramPath(Executable.c_str()));
46 if (Path != Executable)
47 return Path;
48 return TheToolChain.GetProgramPath(ProgName);
49}
50
Daniel Dunbar64198ef2009-09-10 01:21:05 +000051/// CheckPreprocessingOptions - Perform some validation of preprocessing
52/// arguments that is shared with gcc.
53static void CheckPreprocessingOptions(const Driver &D, const ArgList &Args) {
54 if (Arg *A = Args.getLastArg(options::OPT_C, options::OPT_CC))
Joerg Sonnenbergerb86f5f42011-03-06 23:31:01 +000055 if (!Args.hasArg(options::OPT_E) && !D.CCCIsCPP)
Daniel Dunbar64198ef2009-09-10 01:21:05 +000056 D.Diag(clang::diag::err_drv_argument_only_allowed_with)
57 << A->getAsString(Args) << "-E";
58}
59
Daniel Dunbar4eadb602009-09-10 01:21:12 +000060/// CheckCodeGenerationOptions - Perform some validation of code generation
61/// arguments that is shared with gcc.
62static void CheckCodeGenerationOptions(const Driver &D, const ArgList &Args) {
63 // In gcc, only ARM checks this, but it seems reasonable to check universally.
64 if (Args.hasArg(options::OPT_static))
65 if (const Arg *A = Args.getLastArg(options::OPT_dynamic,
66 options::OPT_mdynamic_no_pic))
67 D.Diag(clang::diag::err_drv_argument_not_allowed_with)
68 << A->getAsString(Args) << "-static";
69}
70
Chris Lattnerbf2803f2010-03-29 17:55:58 +000071// Quote target names for inclusion in GNU Make dependency files.
72// Only the characters '$', '#', ' ', '\t' are quoted.
73static void QuoteTarget(llvm::StringRef Target,
74 llvm::SmallVectorImpl<char> &Res) {
75 for (unsigned i = 0, e = Target.size(); i != e; ++i) {
76 switch (Target[i]) {
77 case ' ':
78 case '\t':
79 // Escape the preceding backslashes
80 for (int j = i - 1; j >= 0 && Target[j] == '\\'; --j)
81 Res.push_back('\\');
82
83 // Escape the space/tab
84 Res.push_back('\\');
85 break;
86 case '$':
87 Res.push_back('$');
88 break;
89 case '#':
90 Res.push_back('\\');
91 break;
92 default:
93 break;
94 }
95
96 Res.push_back(Target[i]);
97 }
98}
99
Daniel Dunbar54423b22010-09-17 00:24:54 +0000100static void AddLinkerInputs(const ToolChain &TC,
101 const InputInfoList &Inputs, const ArgList &Args,
102 ArgStringList &CmdArgs) {
103 const Driver &D = TC.getDriver();
104
Daniel Dunbar1094bb12011-02-19 05:33:51 +0000105 // Add extra linker input arguments which are not treated as inputs
106 // (constructed via -Xarch_).
107 Args.AddAllArgValues(CmdArgs, options::OPT_Zlinker_input);
108
Daniel Dunbar54423b22010-09-17 00:24:54 +0000109 for (InputInfoList::const_iterator
110 it = Inputs.begin(), ie = Inputs.end(); it != ie; ++it) {
111 const InputInfo &II = *it;
112
113 if (!TC.HasNativeLLVMSupport()) {
114 // Don't try to pass LLVM inputs unless we have native support.
115 if (II.getType() == types::TY_LLVM_IR ||
116 II.getType() == types::TY_LTO_IR ||
117 II.getType() == types::TY_LLVM_BC ||
118 II.getType() == types::TY_LTO_BC)
119 D.Diag(clang::diag::err_drv_no_linker_llvm_support)
120 << TC.getTripleString();
121 }
122
Daniel Dunbar2cc3f172010-09-17 00:45:02 +0000123 // Add filenames immediately.
124 if (II.isFilename()) {
Daniel Dunbar54423b22010-09-17 00:24:54 +0000125 CmdArgs.push_back(II.getFilename());
Daniel Dunbar2cc3f172010-09-17 00:45:02 +0000126 continue;
127 }
128
129 // Otherwise, this is a linker input argument.
130 const Arg &A = II.getInputArg();
131
132 // Handle reserved library options.
133 if (A.getOption().matches(options::OPT_Z_reserved_lib_stdcxx)) {
Daniel Dunbar3f7796f2010-09-17 01:20:05 +0000134 TC.AddCXXStdlibLibArgs(Args, CmdArgs);
Shantonu Senafeb03b2010-09-17 18:39:08 +0000135 } else if (A.getOption().matches(options::OPT_Z_reserved_lib_cckext)) {
136 TC.AddCCKextLibArgs(Args, CmdArgs);
Daniel Dunbar2cc3f172010-09-17 00:45:02 +0000137 } else
138 A.renderAsInput(Args, CmdArgs);
Daniel Dunbar54423b22010-09-17 00:24:54 +0000139 }
140}
141
Mike Stump11289f42009-09-09 15:08:12 +0000142void Clang::AddPreprocessingOptions(const Driver &D,
Douglas Gregor111af7d2009-04-18 00:34:01 +0000143 const ArgList &Args,
Daniel Dunbard067f7f2009-04-08 23:54:23 +0000144 ArgStringList &CmdArgs,
145 const InputInfo &Output,
146 const InputInfoList &Inputs) const {
Daniel Dunbard067f7f2009-04-08 23:54:23 +0000147 Arg *A;
Daniel Dunbar367dbb92009-06-08 21:48:20 +0000148
Daniel Dunbar64198ef2009-09-10 01:21:05 +0000149 CheckPreprocessingOptions(D, Args);
150
151 Args.AddLastArg(CmdArgs, options::OPT_C);
152 Args.AddLastArg(CmdArgs, options::OPT_CC);
Daniel Dunbar367dbb92009-06-08 21:48:20 +0000153
154 // Handle dependency file generation.
Daniel Dunbar86aed7d2010-12-08 21:33:40 +0000155 if ((A = Args.getLastArg(options::OPT_M, options::OPT_MM)) ||
Daniel Dunbard067f7f2009-04-08 23:54:23 +0000156 (A = Args.getLastArg(options::OPT_MD)) ||
157 (A = Args.getLastArg(options::OPT_MMD))) {
158 // Determine the output location.
159 const char *DepFile;
160 if (Output.getType() == types::TY_Dependencies) {
Daniel Dunbarb440f562010-08-02 02:38:21 +0000161 DepFile = Output.getFilename();
Daniel Dunbard067f7f2009-04-08 23:54:23 +0000162 } else if (Arg *MF = Args.getLastArg(options::OPT_MF)) {
163 DepFile = MF->getValue(Args);
Daniel Dunbar0bfb21e2009-11-19 03:26:40 +0000164 } else if (A->getOption().matches(options::OPT_M) ||
165 A->getOption().matches(options::OPT_MM)) {
Daniel Dunbard067f7f2009-04-08 23:54:23 +0000166 DepFile = "-";
167 } else {
168 DepFile = darwin::CC1::getDependencyFileName(Args, Inputs);
169 }
170 CmdArgs.push_back("-dependency-file");
171 CmdArgs.push_back(DepFile);
172
Chris Lattnerbf2803f2010-03-29 17:55:58 +0000173 // Add a default target if one wasn't specified.
Daniel Dunbard067f7f2009-04-08 23:54:23 +0000174 if (!Args.hasArg(options::OPT_MT) && !Args.hasArg(options::OPT_MQ)) {
175 const char *DepTarget;
176
177 // If user provided -o, that is the dependency target, except
178 // when we are only generating a dependency file.
179 Arg *OutputOpt = Args.getLastArg(options::OPT_o);
180 if (OutputOpt && Output.getType() != types::TY_Dependencies) {
181 DepTarget = OutputOpt->getValue(Args);
182 } else {
183 // Otherwise derive from the base input.
184 //
185 // FIXME: This should use the computed output file location.
Michael J. Spencere1696752010-12-18 00:19:12 +0000186 llvm::SmallString<128> P(Inputs[0].getBaseInput());
187 llvm::sys::path::replace_extension(P, "o");
188 DepTarget = Args.MakeArgString(llvm::sys::path::filename(P));
Daniel Dunbard067f7f2009-04-08 23:54:23 +0000189 }
190
191 CmdArgs.push_back("-MT");
Chris Lattnerbf2803f2010-03-29 17:55:58 +0000192 llvm::SmallString<128> Quoted;
193 QuoteTarget(DepTarget, Quoted);
194 CmdArgs.push_back(Args.MakeArgString(Quoted));
Daniel Dunbard067f7f2009-04-08 23:54:23 +0000195 }
196
Daniel Dunbar0bfb21e2009-11-19 03:26:40 +0000197 if (A->getOption().matches(options::OPT_M) ||
198 A->getOption().matches(options::OPT_MD))
Daniel Dunbard067f7f2009-04-08 23:54:23 +0000199 CmdArgs.push_back("-sys-header-deps");
200 }
201
202 Args.AddLastArg(CmdArgs, options::OPT_MP);
Chris Lattnerbf2803f2010-03-29 17:55:58 +0000203
204 // Convert all -MQ <target> args to -MT <quoted target>
205 for (arg_iterator it = Args.filtered_begin(options::OPT_MT,
206 options::OPT_MQ),
207 ie = Args.filtered_end(); it != ie; ++it) {
Daniel Dunbara442fd52010-06-11 22:00:13 +0000208 const Arg *A = *it;
209 A->claim();
Chris Lattnerbf2803f2010-03-29 17:55:58 +0000210
Daniel Dunbara442fd52010-06-11 22:00:13 +0000211 if (A->getOption().matches(options::OPT_MQ)) {
Chris Lattnerbf2803f2010-03-29 17:55:58 +0000212 CmdArgs.push_back("-MT");
213 llvm::SmallString<128> Quoted;
Daniel Dunbara442fd52010-06-11 22:00:13 +0000214 QuoteTarget(A->getValue(Args), Quoted);
Chris Lattnerbf2803f2010-03-29 17:55:58 +0000215 CmdArgs.push_back(Args.MakeArgString(Quoted));
216
217 // -MT flag - no change
218 } else {
Daniel Dunbara442fd52010-06-11 22:00:13 +0000219 A->render(Args, CmdArgs);
Chris Lattnerbf2803f2010-03-29 17:55:58 +0000220 }
221 }
Daniel Dunbard067f7f2009-04-08 23:54:23 +0000222
Douglas Gregor111af7d2009-04-18 00:34:01 +0000223 // Add -i* options, and automatically translate to
224 // -include-pch/-include-pth for transparent PCH support. It's
225 // wonky, but we include looking for .gch so we can support seamless
226 // replacement into a build system already set up to be generating
227 // .gch files.
Argyrios Kyrtzidis2f23b412010-09-30 16:53:47 +0000228 bool RenderedImplicitInclude = false;
Daniel Dunbar44b36ee2009-11-25 11:53:23 +0000229 for (arg_iterator it = Args.filtered_begin(options::OPT_clang_i_Group),
230 ie = Args.filtered_end(); it != ie; ++it) {
231 const Arg *A = it;
Daniel Dunbard067f7f2009-04-08 23:54:23 +0000232
233 if (A->getOption().matches(options::OPT_include)) {
Argyrios Kyrtzidis2f23b412010-09-30 16:53:47 +0000234 bool IsFirstImplicitInclude = !RenderedImplicitInclude;
235 RenderedImplicitInclude = true;
236
Argyrios Kyrtzidis90bdfbb2010-08-11 23:27:58 +0000237 // Use PCH if the user requested it.
Daniel Dunbarcbc34b72009-10-15 20:02:44 +0000238 bool UsePCH = D.CCCUsePCH;
Daniel Dunbarcbc34b72009-10-15 20:02:44 +0000239
Daniel Dunbard067f7f2009-04-08 23:54:23 +0000240 bool FoundPTH = false;
Douglas Gregor111af7d2009-04-18 00:34:01 +0000241 bool FoundPCH = false;
Daniel Dunbard067f7f2009-04-08 23:54:23 +0000242 llvm::sys::Path P(A->getValue(Args));
Michael J. Spencerf6efe582011-01-10 02:34:13 +0000243 bool Exists;
Daniel Dunbarcbc34b72009-10-15 20:02:44 +0000244 if (UsePCH) {
Douglas Gregor111af7d2009-04-18 00:34:01 +0000245 P.appendSuffix("pch");
Michael J. Spencerf6efe582011-01-10 02:34:13 +0000246 if (!llvm::sys::fs::exists(P.str(), Exists) && Exists)
Douglas Gregor111af7d2009-04-18 00:34:01 +0000247 FoundPCH = true;
Mike Stump11289f42009-09-09 15:08:12 +0000248 else
Douglas Gregor111af7d2009-04-18 00:34:01 +0000249 P.eraseSuffix();
Daniel Dunbard067f7f2009-04-08 23:54:23 +0000250 }
251
Douglas Gregor111af7d2009-04-18 00:34:01 +0000252 if (!FoundPCH) {
253 P.appendSuffix("pth");
Michael J. Spencerf6efe582011-01-10 02:34:13 +0000254 if (!llvm::sys::fs::exists(P.str(), Exists) && Exists)
Douglas Gregor111af7d2009-04-18 00:34:01 +0000255 FoundPTH = true;
256 else
257 P.eraseSuffix();
Mike Stump11289f42009-09-09 15:08:12 +0000258 }
259
Douglas Gregor111af7d2009-04-18 00:34:01 +0000260 if (!FoundPCH && !FoundPTH) {
261 P.appendSuffix("gch");
Michael J. Spencerf6efe582011-01-10 02:34:13 +0000262 if (!llvm::sys::fs::exists(P.str(), Exists) && Exists) {
Daniel Dunbarcbc34b72009-10-15 20:02:44 +0000263 FoundPCH = UsePCH;
264 FoundPTH = !UsePCH;
Douglas Gregor111af7d2009-04-18 00:34:01 +0000265 }
Mike Stump11289f42009-09-09 15:08:12 +0000266 else
Douglas Gregor111af7d2009-04-18 00:34:01 +0000267 P.eraseSuffix();
268 }
269
270 if (FoundPCH || FoundPTH) {
Argyrios Kyrtzidis2f23b412010-09-30 16:53:47 +0000271 if (IsFirstImplicitInclude) {
272 A->claim();
273 if (UsePCH)
274 CmdArgs.push_back("-include-pch");
275 else
276 CmdArgs.push_back("-include-pth");
277 CmdArgs.push_back(Args.MakeArgString(P.str()));
278 continue;
279 } else {
280 // Ignore the PCH if not first on command line and emit warning.
281 D.Diag(clang::diag::warn_drv_pch_not_first_include)
282 << P.str() << A->getAsString(Args);
283 }
Daniel Dunbard067f7f2009-04-08 23:54:23 +0000284 }
285 }
286
287 // Not translated, render as usual.
288 A->claim();
289 A->render(Args, CmdArgs);
290 }
291
292 Args.AddAllArgs(CmdArgs, options::OPT_D, options::OPT_U);
293 Args.AddAllArgs(CmdArgs, options::OPT_I_Group, options::OPT_F);
294
Daniel Dunbarbf11f792010-09-14 23:12:35 +0000295 // Add C++ include arguments, if needed.
296 types::ID InputType = Inputs[0].getType();
297 if (types::isCXX(InputType))
298 getToolChain().AddClangCXXStdlibIncludeArgs(Args, CmdArgs);
299
Daniel Dunbard067f7f2009-04-08 23:54:23 +0000300 // Add -Wp, and -Xassembler if using the preprocessor.
301
302 // FIXME: There is a very unfortunate problem here, some troubled
303 // souls abuse -Wp, to pass preprocessor options in gcc syntax. To
304 // really support that we would have to parse and then translate
305 // those options. :(
306 Args.AddAllArgValues(CmdArgs, options::OPT_Wp_COMMA,
307 options::OPT_Xpreprocessor);
Daniel Dunbar38b62792009-10-29 01:53:44 +0000308
309 // -I- is a deprecated GCC feature, reject it.
310 if (Arg *A = Args.getLastArg(options::OPT_I_))
311 D.Diag(clang::diag::err_drv_I_dash_not_supported) << A->getAsString(Args);
Chandler Carruth24e17e12010-10-20 07:00:47 +0000312
313 // If we have a --sysroot, and don't have an explicit -isysroot flag, add an
314 // -isysroot to the CC1 invocation.
315 if (Arg *A = Args.getLastArg(options::OPT__sysroot_EQ)) {
316 if (!Args.hasArg(options::OPT_isysroot)) {
317 CmdArgs.push_back("-isysroot");
318 CmdArgs.push_back(A->getValue(Args));
319 }
320 }
Daniel Dunbard067f7f2009-04-08 23:54:23 +0000321}
322
Daniel Dunbarfb58b0a2009-09-10 06:49:20 +0000323/// getARMTargetCPU - Get the (LLVM) name of the ARM cpu we are targetting.
324//
325// FIXME: tblgen this.
Rafael Espindola0e1fb4f2010-06-28 17:18:09 +0000326static const char *getARMTargetCPU(const ArgList &Args,
327 const llvm::Triple &Triple) {
Daniel Dunbarfb58b0a2009-09-10 06:49:20 +0000328 // FIXME: Warn on inconsistent use of -mcpu and -march.
Daniel Dunbar0f5c5422009-09-10 04:57:17 +0000329
330 // If we have -mcpu=, use that.
331 if (Arg *A = Args.getLastArg(options::OPT_mcpu_EQ))
332 return A->getValue(Args);
333
Rafael Espindola0e1fb4f2010-06-28 17:18:09 +0000334 llvm::StringRef MArch;
Daniel Dunbar0f5c5422009-09-10 04:57:17 +0000335 if (Arg *A = Args.getLastArg(options::OPT_march_EQ)) {
Rafael Espindola0e1fb4f2010-06-28 17:18:09 +0000336 // Otherwise, if we have -march= choose the base CPU for that arch.
337 MArch = A->getValue(Args);
338 } else {
339 // Otherwise, use the Arch from the triple.
340 MArch = Triple.getArchName();
Daniel Dunbar0f5c5422009-09-10 04:57:17 +0000341 }
342
Rafael Espindola0e1fb4f2010-06-28 17:18:09 +0000343 if (MArch == "armv2" || MArch == "armv2a")
344 return "arm2";
345 if (MArch == "armv3")
346 return "arm6";
347 if (MArch == "armv3m")
348 return "arm7m";
349 if (MArch == "armv4" || MArch == "armv4t")
350 return "arm7tdmi";
351 if (MArch == "armv5" || MArch == "armv5t")
352 return "arm10tdmi";
353 if (MArch == "armv5e" || MArch == "armv5te")
354 return "arm1026ejs";
355 if (MArch == "armv5tej")
356 return "arm926ej-s";
357 if (MArch == "armv6" || MArch == "armv6k")
358 return "arm1136jf-s";
359 if (MArch == "armv6j")
360 return "arm1136j-s";
361 if (MArch == "armv6z" || MArch == "armv6zk")
362 return "arm1176jzf-s";
363 if (MArch == "armv6t2")
364 return "arm1156t2-s";
365 if (MArch == "armv7" || MArch == "armv7a" || MArch == "armv7-a")
366 return "cortex-a8";
367 if (MArch == "armv7r" || MArch == "armv7-r")
368 return "cortex-r4";
369 if (MArch == "armv7m" || MArch == "armv7-m")
370 return "cortex-m3";
371 if (MArch == "ep9312")
372 return "ep9312";
373 if (MArch == "iwmmxt")
374 return "iwmmxt";
375 if (MArch == "xscale")
376 return "xscale";
Bob Wilsond9249412011-03-21 20:40:05 +0000377 if (MArch == "armv6m" || MArch == "armv6-m")
378 return "cortex-m0";
Rafael Espindola0e1fb4f2010-06-28 17:18:09 +0000379
380 // If all else failed, return the most base CPU LLVM supports.
Daniel Dunbar0f5c5422009-09-10 04:57:17 +0000381 return "arm7tdmi";
382}
383
Daniel Dunbarf492c922009-09-10 22:59:51 +0000384/// getLLVMArchSuffixForARM - Get the LLVM arch name to use for a particular
Daniel Dunbarfb58b0a2009-09-10 06:49:20 +0000385/// CPU.
386//
387// FIXME: This is redundant with -mcpu, why does LLVM use this.
388// FIXME: tblgen this, or kill it!
Daniel Dunbarf492c922009-09-10 22:59:51 +0000389static const char *getLLVMArchSuffixForARM(llvm::StringRef CPU) {
Daniel Dunbarfb58b0a2009-09-10 06:49:20 +0000390 if (CPU == "arm7tdmi" || CPU == "arm7tdmi-s" || CPU == "arm710t" ||
391 CPU == "arm720t" || CPU == "arm9" || CPU == "arm9tdmi" ||
392 CPU == "arm920" || CPU == "arm920t" || CPU == "arm922t" ||
393 CPU == "arm940t" || CPU == "ep9312")
Daniel Dunbarf492c922009-09-10 22:59:51 +0000394 return "v4t";
Daniel Dunbarfb58b0a2009-09-10 06:49:20 +0000395
396 if (CPU == "arm10tdmi" || CPU == "arm1020t")
Daniel Dunbarf492c922009-09-10 22:59:51 +0000397 return "v5";
Daniel Dunbarfb58b0a2009-09-10 06:49:20 +0000398
399 if (CPU == "arm9e" || CPU == "arm926ej-s" || CPU == "arm946e-s" ||
400 CPU == "arm966e-s" || CPU == "arm968e-s" || CPU == "arm10e" ||
401 CPU == "arm1020e" || CPU == "arm1022e" || CPU == "xscale" ||
402 CPU == "iwmmxt")
Daniel Dunbarf492c922009-09-10 22:59:51 +0000403 return "v5e";
Daniel Dunbarfb58b0a2009-09-10 06:49:20 +0000404
405 if (CPU == "arm1136j-s" || CPU == "arm1136jf-s" || CPU == "arm1176jz-s" ||
406 CPU == "arm1176jzf-s" || CPU == "mpcorenovfp" || CPU == "mpcore")
Daniel Dunbarf492c922009-09-10 22:59:51 +0000407 return "v6";
Daniel Dunbarfb58b0a2009-09-10 06:49:20 +0000408
409 if (CPU == "arm1156t2-s" || CPU == "arm1156t2f-s")
Daniel Dunbarf492c922009-09-10 22:59:51 +0000410 return "v6t2";
Daniel Dunbarfb58b0a2009-09-10 06:49:20 +0000411
412 if (CPU == "cortex-a8" || CPU == "cortex-a9")
Daniel Dunbarf492c922009-09-10 22:59:51 +0000413 return "v7";
Daniel Dunbarfb58b0a2009-09-10 06:49:20 +0000414
Daniel Dunbarf492c922009-09-10 22:59:51 +0000415 return "";
Daniel Dunbarfb58b0a2009-09-10 06:49:20 +0000416}
417
Daniel Dunbard609b7b2009-11-17 06:37:03 +0000418// FIXME: Move to target hook.
419static bool isSignedCharDefault(const llvm::Triple &Triple) {
420 switch (Triple.getArch()) {
421 default:
422 return true;
423
424 case llvm::Triple::ppc:
425 case llvm::Triple::ppc64:
426 if (Triple.getOS() == llvm::Triple::Darwin)
427 return true;
428 return false;
429
430 case llvm::Triple::systemz:
431 return false;
432 }
433}
434
Daniel Dunbar0f5c5422009-09-10 04:57:17 +0000435void Clang::AddARMTargetArgs(const ArgList &Args,
Daniel Dunbarc9388c12011-03-17 17:10:06 +0000436 ArgStringList &CmdArgs,
437 bool KernelOrKext) const {
Daniel Dunbar083edf72009-12-21 18:54:17 +0000438 const Driver &D = getToolChain().getDriver();
Rafael Espindola0e1fb4f2010-06-28 17:18:09 +0000439 llvm::Triple Triple = getToolChain().getTriple();
Daniel Dunbar78485922009-09-10 23:00:09 +0000440
Daniel Dunbar908b4852011-03-02 00:55:57 +0000441 // Disable movt generation, if requested.
442#ifdef DISABLE_ARM_DARWIN_USE_MOVT
Daniel Dunbar12100e22011-03-22 16:48:17 +0000443 CmdArgs.push_back("-backend-option");
Daniel Dunbar908b4852011-03-02 00:55:57 +0000444 CmdArgs.push_back("-arm-darwin-use-movt=0");
445#endif
446
Daniel Dunbar4ed78982009-09-14 00:34:46 +0000447 // Select the ABI to use.
448 //
449 // FIXME: Support -meabi.
450 const char *ABIName = 0;
451 if (Arg *A = Args.getLastArg(options::OPT_mabi_EQ)) {
452 ABIName = A->getValue(Args);
453 } else {
454 // Select the default based on the platform.
Bob Wilsond1447c42011-02-04 17:59:28 +0000455 switch(Triple.getEnvironment()) {
456 case llvm::Triple::GNUEABI:
Daniel Dunbar4ed78982009-09-14 00:34:46 +0000457 ABIName = "aapcs-linux";
Bob Wilsond1447c42011-02-04 17:59:28 +0000458 break;
459 case llvm::Triple::EABI:
Rafael Espindola23a8a062010-06-16 19:01:17 +0000460 ABIName = "aapcs";
Bob Wilsond1447c42011-02-04 17:59:28 +0000461 break;
462 default:
Rafael Espindola23a8a062010-06-16 19:01:17 +0000463 ABIName = "apcs-gnu";
Bob Wilsond1447c42011-02-04 17:59:28 +0000464 }
Daniel Dunbar4ed78982009-09-14 00:34:46 +0000465 }
466 CmdArgs.push_back("-target-abi");
467 CmdArgs.push_back(ABIName);
468
Daniel Dunbar0f5c5422009-09-10 04:57:17 +0000469 // Set the CPU based on -march= and -mcpu=.
Daniel Dunbara7d02312009-12-18 06:30:12 +0000470 CmdArgs.push_back("-target-cpu");
Rafael Espindola0e1fb4f2010-06-28 17:18:09 +0000471 CmdArgs.push_back(getARMTargetCPU(Args, Triple));
Daniel Dunbar0f5c5422009-09-10 04:57:17 +0000472
Daniel Dunbar78485922009-09-10 23:00:09 +0000473 // Select the float ABI as determined by -msoft-float, -mhard-float, and
474 // -mfloat-abi=.
475 llvm::StringRef FloatABI;
476 if (Arg *A = Args.getLastArg(options::OPT_msoft_float,
477 options::OPT_mhard_float,
478 options::OPT_mfloat_abi_EQ)) {
479 if (A->getOption().matches(options::OPT_msoft_float))
480 FloatABI = "soft";
481 else if (A->getOption().matches(options::OPT_mhard_float))
482 FloatABI = "hard";
483 else {
484 FloatABI = A->getValue(Args);
485 if (FloatABI != "soft" && FloatABI != "softfp" && FloatABI != "hard") {
486 D.Diag(clang::diag::err_drv_invalid_mfloat_abi)
487 << A->getAsString(Args);
488 FloatABI = "soft";
489 }
490 }
491 }
492
493 // If unspecified, choose the default based on the platform.
494 if (FloatABI.empty()) {
Rafael Espindola0e1fb4f2010-06-28 17:18:09 +0000495 const llvm::Triple &Triple = getToolChain().getTriple();
496 switch (Triple.getOS()) {
Daniel Dunbar78485922009-09-10 23:00:09 +0000497 case llvm::Triple::Darwin: {
498 // Darwin defaults to "softfp" for v6 and v7.
499 //
500 // FIXME: Factor out an ARM class so we can cache the arch somewhere.
Rafael Espindola0e1fb4f2010-06-28 17:18:09 +0000501 llvm::StringRef ArchName =
502 getLLVMArchSuffixForARM(getARMTargetCPU(Args, Triple));
Daniel Dunbar78485922009-09-10 23:00:09 +0000503 if (ArchName.startswith("v6") || ArchName.startswith("v7"))
504 FloatABI = "softfp";
505 else
506 FloatABI = "soft";
507 break;
508 }
509
Rafael Espindolab1ef8ff2010-06-27 18:29:21 +0000510 case llvm::Triple::Linux: {
Bob Wilsond1447c42011-02-04 17:59:28 +0000511 if (getToolChain().getTriple().getEnvironment() == llvm::Triple::GNUEABI) {
Rafael Espindolab1ef8ff2010-06-27 18:29:21 +0000512 FloatABI = "softfp";
513 break;
514 }
515 }
516 // fall through
517
Daniel Dunbar78485922009-09-10 23:00:09 +0000518 default:
Bob Wilsond1447c42011-02-04 17:59:28 +0000519 switch(Triple.getEnvironment()) {
520 case llvm::Triple::GNUEABI:
521 FloatABI = "softfp";
522 break;
523 case llvm::Triple::EABI:
524 // EABI is always AAPCS, and if it was not marked 'hard', it's softfp
525 FloatABI = "softfp";
526 break;
527 default:
528 // Assume "soft", but warn the user we are guessing.
529 FloatABI = "soft";
530 D.Diag(clang::diag::warn_drv_assuming_mfloat_abi_is) << "soft";
531 break;
532 }
Daniel Dunbar78485922009-09-10 23:00:09 +0000533 }
534 }
535
536 if (FloatABI == "soft") {
537 // Floating point operations and argument passing are soft.
538 //
539 // FIXME: This changes CPP defines, we need -target-soft-float.
Daniel Dunbara74f8ff2009-11-30 08:42:00 +0000540 CmdArgs.push_back("-msoft-float");
Daniel Dunbar6cc525b2009-12-08 19:49:51 +0000541 CmdArgs.push_back("-mfloat-abi");
542 CmdArgs.push_back("soft");
Daniel Dunbar78485922009-09-10 23:00:09 +0000543 } else if (FloatABI == "softfp") {
544 // Floating point operations are hard, but argument passing is soft.
Daniel Dunbar6cc525b2009-12-08 19:49:51 +0000545 CmdArgs.push_back("-mfloat-abi");
546 CmdArgs.push_back("soft");
Daniel Dunbar78485922009-09-10 23:00:09 +0000547 } else {
548 // Floating point operations and argument passing are hard.
549 assert(FloatABI == "hard" && "Invalid float abi!");
Daniel Dunbar6cc525b2009-12-08 19:49:51 +0000550 CmdArgs.push_back("-mfloat-abi");
551 CmdArgs.push_back("hard");
Daniel Dunbar78485922009-09-10 23:00:09 +0000552 }
Daniel Dunbar893d4752009-12-19 04:15:38 +0000553
554 // Set appropriate target features for floating point mode.
555 //
556 // FIXME: Note, this is a hack, the LLVM backend doesn't actually use these
557 // yet (it uses the -mfloat-abi and -msoft-float options above), and it is
558 // stripped out by the ARM target.
559
560 // Use software floating point operations?
561 if (FloatABI == "soft") {
562 CmdArgs.push_back("-target-feature");
563 CmdArgs.push_back("+soft-float");
564 }
565
566 // Use software floating point argument passing?
567 if (FloatABI != "hard") {
568 CmdArgs.push_back("-target-feature");
569 CmdArgs.push_back("+soft-float-abi");
570 }
Daniel Dunbar0def3d12009-12-21 23:28:17 +0000571
572 // Honor -mfpu=.
573 //
574 // FIXME: Centralize feature selection, defaulting shouldn't be also in the
575 // frontend target.
576 if (const Arg *A = Args.getLastArg(options::OPT_mfpu_EQ)) {
577 llvm::StringRef FPU = A->getValue(Args);
578
579 // Set the target features based on the FPU.
580 if (FPU == "fpa" || FPU == "fpe2" || FPU == "fpe3" || FPU == "maverick") {
581 // Disable any default FPU support.
582 CmdArgs.push_back("-target-feature");
583 CmdArgs.push_back("-vfp2");
584 CmdArgs.push_back("-target-feature");
585 CmdArgs.push_back("-vfp3");
586 CmdArgs.push_back("-target-feature");
587 CmdArgs.push_back("-neon");
588 } else if (FPU == "vfp") {
589 CmdArgs.push_back("-target-feature");
590 CmdArgs.push_back("+vfp2");
591 } else if (FPU == "vfp3") {
592 CmdArgs.push_back("-target-feature");
593 CmdArgs.push_back("+vfp3");
594 } else if (FPU == "neon") {
595 CmdArgs.push_back("-target-feature");
596 CmdArgs.push_back("+neon");
597 } else
598 D.Diag(clang::diag::err_drv_clang_unsupported) << A->getAsString(Args);
599 }
Daniel Dunbarb1db4b62011-03-17 00:07:34 +0000600
601 // Setting -msoft-float effectively disables NEON because of the GCC
602 // implementation, although the same isn't true of VFP or VFP3.
603 if (FloatABI == "soft") {
Daniel Dunbarc9388c12011-03-17 17:10:06 +0000604 CmdArgs.push_back("-target-feature");
605 CmdArgs.push_back("-neon");
606 }
607
608 // Kernel code has more strict alignment requirements.
609 if (KernelOrKext) {
Daniel Dunbar12100e22011-03-22 16:48:17 +0000610 CmdArgs.push_back("-backend-option");
Daniel Dunbarc9388c12011-03-17 17:10:06 +0000611 CmdArgs.push_back("-arm-long-calls");
612
Daniel Dunbar12100e22011-03-22 16:48:17 +0000613 CmdArgs.push_back("-backend-option");
Daniel Dunbarc9388c12011-03-17 17:10:06 +0000614 CmdArgs.push_back("-arm-strict-align");
Daniel Dunbarb1db4b62011-03-17 00:07:34 +0000615 }
Daniel Dunbar0f5c5422009-09-10 04:57:17 +0000616}
617
Eric Christopher0b26a612010-03-02 02:41:08 +0000618void Clang::AddMIPSTargetArgs(const ArgList &Args,
619 ArgStringList &CmdArgs) const {
620 const Driver &D = getToolChain().getDriver();
621
622 // Select the ABI to use.
623 const char *ABIName = 0;
624 if (Arg *A = Args.getLastArg(options::OPT_mabi_EQ)) {
625 ABIName = A->getValue(Args);
626 } else {
627 ABIName = "o32";
628 }
629
630 CmdArgs.push_back("-target-abi");
631 CmdArgs.push_back(ABIName);
632
633 if (const Arg *A = Args.getLastArg(options::OPT_march_EQ)) {
634 llvm::StringRef MArch = A->getValue(Args);
635 CmdArgs.push_back("-target-cpu");
636
637 if ((MArch == "r2000") || (MArch == "r3000"))
638 CmdArgs.push_back("mips1");
639 else if (MArch == "r6000")
640 CmdArgs.push_back("mips2");
641 else
642 CmdArgs.push_back(MArch.str().c_str());
643 }
644
645 // Select the float ABI as determined by -msoft-float, -mhard-float, and
646 llvm::StringRef FloatABI;
647 if (Arg *A = Args.getLastArg(options::OPT_msoft_float,
648 options::OPT_mhard_float)) {
649 if (A->getOption().matches(options::OPT_msoft_float))
650 FloatABI = "soft";
651 else if (A->getOption().matches(options::OPT_mhard_float))
652 FloatABI = "hard";
653 }
654
655 // If unspecified, choose the default based on the platform.
656 if (FloatABI.empty()) {
Benjamin Kramerf41ccef2010-04-08 15:44:22 +0000657 // Assume "soft", but warn the user we are guessing.
658 FloatABI = "soft";
659 D.Diag(clang::diag::warn_drv_assuming_mfloat_abi_is) << "soft";
Eric Christopher0b26a612010-03-02 02:41:08 +0000660 }
661
662 if (FloatABI == "soft") {
663 // Floating point operations and argument passing are soft.
664 //
665 // FIXME: This changes CPP defines, we need -target-soft-float.
666 CmdArgs.push_back("-msoft-float");
667 } else {
668 assert(FloatABI == "hard" && "Invalid float abi!");
669 CmdArgs.push_back("-mhard-float");
670 }
671}
672
Bruno Cardoso Lopese7f211c2010-11-09 17:21:19 +0000673void Clang::AddSparcTargetArgs(const ArgList &Args,
674 ArgStringList &CmdArgs) const {
675 const Driver &D = getToolChain().getDriver();
676
677 if (const Arg *A = Args.getLastArg(options::OPT_march_EQ)) {
678 llvm::StringRef MArch = A->getValue(Args);
679 CmdArgs.push_back("-target-cpu");
680 CmdArgs.push_back(MArch.str().c_str());
681 }
682
683 // Select the float ABI as determined by -msoft-float, -mhard-float, and
684 llvm::StringRef FloatABI;
685 if (Arg *A = Args.getLastArg(options::OPT_msoft_float,
686 options::OPT_mhard_float)) {
687 if (A->getOption().matches(options::OPT_msoft_float))
688 FloatABI = "soft";
689 else if (A->getOption().matches(options::OPT_mhard_float))
690 FloatABI = "hard";
691 }
692
693 // If unspecified, choose the default based on the platform.
694 if (FloatABI.empty()) {
695 switch (getToolChain().getTriple().getOS()) {
696 default:
697 // Assume "soft", but warn the user we are guessing.
698 FloatABI = "soft";
699 D.Diag(clang::diag::warn_drv_assuming_mfloat_abi_is) << "soft";
700 break;
701 }
702 }
703
704 if (FloatABI == "soft") {
705 // Floating point operations and argument passing are soft.
706 //
707 // FIXME: This changes CPP defines, we need -target-soft-float.
708 CmdArgs.push_back("-msoft-float");
709 CmdArgs.push_back("soft");
710 CmdArgs.push_back("-target-feature");
711 CmdArgs.push_back("+soft-float");
712 } else {
713 assert(FloatABI == "hard" && "Invalid float abi!");
714 CmdArgs.push_back("-mhard-float");
715 }
716}
717
Daniel Dunbar3b3191f2009-09-09 22:33:08 +0000718void Clang::AddX86TargetArgs(const ArgList &Args,
719 ArgStringList &CmdArgs) const {
Daniel Dunbare2cf8f72009-09-10 22:59:57 +0000720 if (!Args.hasFlag(options::OPT_mred_zone,
721 options::OPT_mno_red_zone,
722 true) ||
723 Args.hasArg(options::OPT_mkernel) ||
724 Args.hasArg(options::OPT_fapple_kext))
Daniel Dunbar8bed86c2009-11-20 22:21:36 +0000725 CmdArgs.push_back("-disable-red-zone");
Daniel Dunbare2cf8f72009-09-10 22:59:57 +0000726
Daniel Dunbare2cf8f72009-09-10 22:59:57 +0000727 if (Args.hasFlag(options::OPT_msoft_float,
728 options::OPT_mno_soft_float,
729 false))
Daniel Dunbar8bed86c2009-11-20 22:21:36 +0000730 CmdArgs.push_back("-no-implicit-float");
Daniel Dunbare2cf8f72009-09-10 22:59:57 +0000731
Daniel Dunbare13ada62009-11-14 22:04:54 +0000732 const char *CPUName = 0;
Daniel Dunbar3b3191f2009-09-09 22:33:08 +0000733 if (const Arg *A = Args.getLastArg(options::OPT_march_EQ)) {
Daniel Dunbare13ada62009-11-14 22:04:54 +0000734 if (llvm::StringRef(A->getValue(Args)) == "native") {
735 // FIXME: Reject attempts to use -march=native unless the target matches
736 // the host.
737 //
738 // FIXME: We should also incorporate the detected target features for use
739 // with -native.
740 std::string CPU = llvm::sys::getHostCPUName();
741 if (!CPU.empty())
742 CPUName = Args.MakeArgString(CPU);
743 } else
744 CPUName = A->getValue(Args);
745 }
Daniel Dunbar3b3191f2009-09-09 22:33:08 +0000746
Daniel Dunbare13ada62009-11-14 22:04:54 +0000747 // Select the default CPU if none was given (or detection failed).
748 if (!CPUName) {
Daniel Dunbar3b3191f2009-09-09 22:33:08 +0000749 // FIXME: Need target hooks.
Benjamin Kramer842bf172010-01-30 15:01:47 +0000750 if (getToolChain().getOS().startswith("darwin")) {
Daniel Dunbar3b3191f2009-09-09 22:33:08 +0000751 if (getToolChain().getArchName() == "x86_64")
Daniel Dunbare13ada62009-11-14 22:04:54 +0000752 CPUName = "core2";
Daniel Dunbar3b3191f2009-09-09 22:33:08 +0000753 else if (getToolChain().getArchName() == "i386")
Daniel Dunbare13ada62009-11-14 22:04:54 +0000754 CPUName = "yonah";
Chris Lattnerb986aba2010-04-11 19:29:39 +0000755 } else if (getToolChain().getOS().startswith("haiku")) {
756 if (getToolChain().getArchName() == "x86_64")
757 CPUName = "x86-64";
758 else if (getToolChain().getArchName() == "i386")
759 CPUName = "i586";
Daniel Dunbarea3813f2010-08-01 23:13:54 +0000760 } else if (getToolChain().getOS().startswith("openbsd")) {
761 if (getToolChain().getArchName() == "x86_64")
762 CPUName = "x86-64";
763 else if (getToolChain().getArchName() == "i386")
764 CPUName = "i486";
Roman Divacky432f10d2011-03-01 18:11:37 +0000765 } else if (getToolChain().getOS().startswith("freebsd")) {
766 if (getToolChain().getArchName() == "x86_64")
767 CPUName = "x86-64";
768 else if (getToolChain().getArchName() == "i386")
769 CPUName = "i486";
Benjamin Kramer24f1d3e2011-02-02 18:59:27 +0000770 } else if (getToolChain().getOS().startswith("netbsd")) {
771 if (getToolChain().getArchName() == "x86_64")
772 CPUName = "x86-64";
773 else if (getToolChain().getArchName() == "i386")
774 CPUName = "i486";
Daniel Dunbar3b3191f2009-09-09 22:33:08 +0000775 } else {
776 if (getToolChain().getArchName() == "x86_64")
Daniel Dunbare13ada62009-11-14 22:04:54 +0000777 CPUName = "x86-64";
Daniel Dunbar3b3191f2009-09-09 22:33:08 +0000778 else if (getToolChain().getArchName() == "i386")
Daniel Dunbare13ada62009-11-14 22:04:54 +0000779 CPUName = "pentium4";
Daniel Dunbar3b3191f2009-09-09 22:33:08 +0000780 }
781 }
782
Daniel Dunbare13ada62009-11-14 22:04:54 +0000783 if (CPUName) {
Daniel Dunbara7d02312009-12-18 06:30:12 +0000784 CmdArgs.push_back("-target-cpu");
Daniel Dunbare13ada62009-11-14 22:04:54 +0000785 CmdArgs.push_back(CPUName);
786 }
787
Daniel Dunbar44b36ee2009-11-25 11:53:23 +0000788 for (arg_iterator it = Args.filtered_begin(options::OPT_m_x86_Features_Group),
789 ie = Args.filtered_end(); it != ie; ++it) {
Daniel Dunbara442fd52010-06-11 22:00:13 +0000790 llvm::StringRef Name = (*it)->getOption().getName();
791 (*it)->claim();
Daniel Dunbar3b3191f2009-09-09 22:33:08 +0000792
Daniel Dunbar44b36ee2009-11-25 11:53:23 +0000793 // Skip over "-m".
794 assert(Name.startswith("-m") && "Invalid feature name.");
795 Name = Name.substr(2);
Daniel Dunbar3b3191f2009-09-09 22:33:08 +0000796
Daniel Dunbar44b36ee2009-11-25 11:53:23 +0000797 bool IsNegative = Name.startswith("no-");
798 if (IsNegative)
799 Name = Name.substr(3);
Daniel Dunbar3b3191f2009-09-09 22:33:08 +0000800
Daniel Dunbar44b36ee2009-11-25 11:53:23 +0000801 CmdArgs.push_back("-target-feature");
802 CmdArgs.push_back(Args.MakeArgString((IsNegative ? "-" : "+") + Name));
Daniel Dunbar3b3191f2009-09-09 22:33:08 +0000803 }
804}
805
Anders Carlsson246ff3f2011-02-28 00:44:51 +0000806static bool
807shouldUseExceptionTablesForObjCExceptions(const ArgList &Args,
808 const llvm::Triple &Triple) {
809 // We use the zero-cost exception tables for Objective-C if the non-fragile
810 // ABI is enabled or when compiling for x86_64 and ARM on Snow Leopard and
811 // later.
812
813 if (Args.hasArg(options::OPT_fobjc_nonfragile_abi))
814 return true;
815
816 if (Triple.getOS() != llvm::Triple::Darwin)
817 return false;
818
819 return (Triple.getDarwinMajorNumber() >= 9 &&
820 (Triple.getArch() == llvm::Triple::x86_64 ||
821 Triple.getArch() == llvm::Triple::arm));
822}
823
Anders Carlssone96ab552011-02-28 02:27:16 +0000824/// addExceptionArgs - Adds exception related arguments to the driver command
825/// arguments. There's a master flag, -fexceptions and also language specific
826/// flags to enable/disable C++ and Objective-C exceptions.
827/// This makes it possible to for example disable C++ exceptions but enable
828/// Objective-C exceptions.
829static void addExceptionArgs(const ArgList &Args, types::ID InputType,
830 const llvm::Triple &Triple,
831 bool KernelOrKext, bool IsRewriter,
832 ArgStringList &CmdArgs) {
833 if (KernelOrKext)
834 return;
835
836 // Exceptions are enabled by default.
837 bool ExceptionsEnabled = true;
838
839 // This keeps track of whether exceptions were explicitly turned on or off.
840 bool DidHaveExplicitExceptionFlag = false;
841
Rafael Espindola00a66572009-10-01 13:33:33 +0000842 if (Arg *A = Args.getLastArg(options::OPT_fexceptions,
843 options::OPT_fno_exceptions)) {
844 if (A->getOption().matches(options::OPT_fexceptions))
Anders Carlssone96ab552011-02-28 02:27:16 +0000845 ExceptionsEnabled = true;
846 else
847 ExceptionsEnabled = false;
848
849 DidHaveExplicitExceptionFlag = true;
Rafael Espindola00a66572009-10-01 13:33:33 +0000850 }
Daniel Dunbar30a12b82010-09-14 23:12:31 +0000851
Anders Carlssone96ab552011-02-28 02:27:16 +0000852 bool ShouldUseExceptionTables = false;
Fariborz Jahaniane4b21ab2009-10-01 20:30:46 +0000853
Anders Carlssone96ab552011-02-28 02:27:16 +0000854 // Exception tables and cleanups can be enabled with -fexceptions even if the
855 // language itself doesn't support exceptions.
856 if (ExceptionsEnabled && DidHaveExplicitExceptionFlag)
857 ShouldUseExceptionTables = true;
Daniel Dunbar30a12b82010-09-14 23:12:31 +0000858
Daniel Dunbarc44f8cf2011-03-17 23:28:31 +0000859 // Obj-C exceptions are enabled by default, regardless of -fexceptions. This
860 // is not necessarily sensible, but follows GCC.
861 if (types::isObjC(InputType) &&
862 Args.hasFlag(options::OPT_fobjc_exceptions,
863 options::OPT_fno_objc_exceptions,
864 true)) {
865 CmdArgs.push_back("-fobjc-exceptions");
Anders Carlssone96ab552011-02-28 02:27:16 +0000866
Daniel Dunbarc44f8cf2011-03-17 23:28:31 +0000867 ShouldUseExceptionTables |=
868 shouldUseExceptionTablesForObjCExceptions(Args, Triple);
Anders Carlssone96ab552011-02-28 02:27:16 +0000869 }
870
871 if (types::isCXX(InputType)) {
872 bool CXXExceptionsEnabled = ExceptionsEnabled;
873
874 if (Arg *A = Args.getLastArg(options::OPT_fcxx_exceptions,
875 options::OPT_fno_cxx_exceptions,
876 options::OPT_fexceptions,
877 options::OPT_fno_exceptions)) {
878 if (A->getOption().matches(options::OPT_fcxx_exceptions))
879 CXXExceptionsEnabled = true;
Chandler Carruth74f87112011-02-28 07:25:18 +0000880 else if (A->getOption().matches(options::OPT_fno_cxx_exceptions))
Anders Carlssone96ab552011-02-28 02:27:16 +0000881 CXXExceptionsEnabled = false;
882 }
883
884 if (CXXExceptionsEnabled) {
885 CmdArgs.push_back("-fcxx-exceptions");
886
887 ShouldUseExceptionTables = true;
888 }
889 }
890
891 if (ShouldUseExceptionTables)
892 CmdArgs.push_back("-fexceptions");
Rafael Espindola00a66572009-10-01 13:33:33 +0000893}
894
Daniel Dunbar1a093d22009-03-18 06:00:36 +0000895void Clang::ConstructJob(Compilation &C, const JobAction &JA,
Daniel Dunbar04c4c2c2009-03-18 07:06:02 +0000896 const InputInfo &Output,
Daniel Dunbar0450e6d2009-03-18 06:07:59 +0000897 const InputInfoList &Inputs,
Daniel Dunbara2aedc62009-03-18 10:01:51 +0000898 const ArgList &Args,
Daniel Dunbar1a093d22009-03-18 06:00:36 +0000899 const char *LinkingOutput) const {
Daniel Dunbare46b52a2010-03-20 04:52:14 +0000900 bool KernelOrKext = Args.hasArg(options::OPT_mkernel,
901 options::OPT_fapple_kext);
Daniel Dunbar083edf72009-12-21 18:54:17 +0000902 const Driver &D = getToolChain().getDriver();
Daniel Dunbara3246a02009-03-18 08:07:30 +0000903 ArgStringList CmdArgs;
904
Daniel Dunbare521a892009-03-31 20:53:55 +0000905 assert(Inputs.size() == 1 && "Unable to handle multiple inputs.");
906
Daniel Dunbar6c536aa2009-12-11 23:00:49 +0000907 // Invoke ourselves in -cc1 mode.
908 //
909 // FIXME: Implement custom jobs for internal actions.
910 CmdArgs.push_back("-cc1");
911
Daniel Dunbar624c21b2009-10-30 18:12:20 +0000912 // Add the "effective" target triple.
Daniel Dunbard640be22009-03-31 17:35:15 +0000913 CmdArgs.push_back("-triple");
Daniel Dunbar82eb4ce2010-08-23 22:35:37 +0000914 std::string TripleStr = getToolChain().ComputeEffectiveClangTriple(Args);
Daniel Dunbar624c21b2009-10-30 18:12:20 +0000915 CmdArgs.push_back(Args.MakeArgString(TripleStr));
Daniel Dunbarfb58b0a2009-09-10 06:49:20 +0000916
Daniel Dunbar624c21b2009-10-30 18:12:20 +0000917 // Select the appropriate action.
Daniel Dunbar99b55242010-07-19 19:44:22 +0000918 bool IsRewriter = false;
Daniel Dunbara2aedc62009-03-18 10:01:51 +0000919 if (isa<AnalyzeJobAction>(JA)) {
920 assert(JA.getType() == types::TY_Plist && "Invalid output type.");
921 CmdArgs.push_back("-analyze");
922 } else if (isa<PreprocessJobAction>(JA)) {
Daniel Dunbard67a3222009-03-30 06:36:42 +0000923 if (Output.getType() == types::TY_Dependencies)
924 CmdArgs.push_back("-Eonly");
925 else
926 CmdArgs.push_back("-E");
Daniel Dunbarc4343942010-02-03 03:07:56 +0000927 } else if (isa<AssembleJobAction>(JA)) {
928 CmdArgs.push_back("-emit-obj");
Daniel Dunbar06e2cc32010-05-27 06:18:05 +0000929
930 // At -O0, we use -mrelax-all by default.
931 bool IsOpt = false;
932 if (Arg *A = Args.getLastArg(options::OPT_O_Group))
933 IsOpt = !A->getOption().matches(options::OPT_O0);
934 if (Args.hasFlag(options::OPT_mrelax_all,
935 options::OPT_mno_relax_all,
936 !IsOpt))
937 CmdArgs.push_back("-mrelax-all");
Daniel Dunbarfe6c97b2010-08-24 16:47:49 +0000938
Daniel Dunbar2b4de142010-10-18 22:36:15 +0000939 // When using an integrated assembler, translate -Wa, and -Xassembler
940 // options.
941 for (arg_iterator it = Args.filtered_begin(options::OPT_Wa_COMMA,
942 options::OPT_Xassembler),
943 ie = Args.filtered_end(); it != ie; ++it) {
944 const Arg *A = *it;
945 A->claim();
946
947 for (unsigned i = 0, e = A->getNumValues(); i != e; ++i) {
948 llvm::StringRef Value = A->getValue(Args, i);
949
950 if (Value == "-force_cpusubtype_ALL") {
951 // Do nothing, this is the default and we don't support anything else.
Daniel Dunbara78e5892010-10-28 20:36:23 +0000952 } else if (Value == "-L") {
953 // We don't support -L yet, but it isn't important enough to error
954 // on. No one should really be using it for a semantic change.
955 D.Diag(clang::diag::warn_drv_unsupported_option_argument)
956 << A->getOption().getName() << Value;
Daniel Dunbar2b4de142010-10-18 22:36:15 +0000957 } else {
958 D.Diag(clang::diag::err_drv_unsupported_option_argument)
959 << A->getOption().getName() << Value;
960 }
961 }
962 }
Daniel Dunbar7c874332010-11-19 16:23:35 +0000963
964 // Also ignore explicit -force_cpusubtype_ALL option.
965 (void) Args.hasArg(options::OPT_force__cpusubtype__ALL);
Daniel Dunbara2aedc62009-03-18 10:01:51 +0000966 } else if (isa<PrecompileJobAction>(JA)) {
Argyrios Kyrtzidis90bdfbb2010-08-11 23:27:58 +0000967 // Use PCH if the user requested it.
Daniel Dunbarcbc34b72009-10-15 20:02:44 +0000968 bool UsePCH = D.CCCUsePCH;
Daniel Dunbarcbc34b72009-10-15 20:02:44 +0000969
970 if (UsePCH)
Douglas Gregor111af7d2009-04-18 00:34:01 +0000971 CmdArgs.push_back("-emit-pch");
972 else
973 CmdArgs.push_back("-emit-pth");
Daniel Dunbara2aedc62009-03-18 10:01:51 +0000974 } else {
975 assert(isa<CompileJobAction>(JA) && "Invalid action for clang tool.");
Daniel Dunbard067f7f2009-04-08 23:54:23 +0000976
Daniel Dunbara2aedc62009-03-18 10:01:51 +0000977 if (JA.getType() == types::TY_Nothing) {
978 CmdArgs.push_back("-fsyntax-only");
Daniel Dunbar24e52992010-06-07 23:28:45 +0000979 } else if (JA.getType() == types::TY_LLVM_IR ||
980 JA.getType() == types::TY_LTO_IR) {
Daniel Dunbara2aedc62009-03-18 10:01:51 +0000981 CmdArgs.push_back("-emit-llvm");
Daniel Dunbar24e52992010-06-07 23:28:45 +0000982 } else if (JA.getType() == types::TY_LLVM_BC ||
983 JA.getType() == types::TY_LTO_BC) {
Daniel Dunbara2aedc62009-03-18 10:01:51 +0000984 CmdArgs.push_back("-emit-llvm-bc");
985 } else if (JA.getType() == types::TY_PP_Asm) {
Daniel Dunbard112f102009-09-17 00:47:53 +0000986 CmdArgs.push_back("-S");
Daniel Dunbar6cdf83c2009-09-01 16:57:46 +0000987 } else if (JA.getType() == types::TY_AST) {
988 CmdArgs.push_back("-emit-pch");
Daniel Dunbaraeea8ac2010-02-11 03:16:21 +0000989 } else if (JA.getType() == types::TY_RewrittenObjC) {
990 CmdArgs.push_back("-rewrite-objc");
Daniel Dunbar99b55242010-07-19 19:44:22 +0000991 IsRewriter = true;
Daniel Dunbaraeea8ac2010-02-11 03:16:21 +0000992 } else {
993 assert(JA.getType() == types::TY_PP_Asm &&
994 "Unexpected output type!");
Daniel Dunbara2aedc62009-03-18 10:01:51 +0000995 }
Daniel Dunbara3246a02009-03-18 08:07:30 +0000996 }
997
Daniel Dunbara2aedc62009-03-18 10:01:51 +0000998 // The make clang go fast button.
999 CmdArgs.push_back("-disable-free");
1000
John McCallbb79b5f2010-02-13 03:50:24 +00001001 // Disable the verification pass in -asserts builds.
1002#ifdef NDEBUG
1003 CmdArgs.push_back("-disable-llvm-verifier");
1004#endif
1005
Daniel Dunbar3b358a32009-04-08 05:11:16 +00001006 // Set the main file name, so that debug info works even with
1007 // -save-temps.
1008 CmdArgs.push_back("-main-file-name");
1009 CmdArgs.push_back(darwin::CC1::getBaseInputName(Args, Inputs));
1010
Daniel Dunbar17ddaa62009-04-08 18:03:55 +00001011 // Some flags which affect the language (via preprocessor
1012 // defines). See darwin::CC1::AddCPPArgs.
1013 if (Args.hasArg(options::OPT_static))
1014 CmdArgs.push_back("-static-define");
1015
Daniel Dunbara2aedc62009-03-18 10:01:51 +00001016 if (isa<AnalyzeJobAction>(JA)) {
Ted Kremenek05e6f5b2009-09-25 05:55:59 +00001017 // Enable region store model by default.
1018 CmdArgs.push_back("-analyzer-store=region");
1019
Ted Kremenek7bea9a12009-12-07 22:26:14 +00001020 // Treat blocks as analysis entry points.
1021 CmdArgs.push_back("-analyzer-opt-analyze-nested-blocks");
1022
Ted Kremenek49c79792011-03-24 00:28:47 +00001023 CmdArgs.push_back("-analyzer-eagerly-assume");
1024
Daniel Dunbara2aedc62009-03-18 10:01:51 +00001025 // Add default argument set.
Daniel Dunbar58f345ce2009-05-22 00:38:15 +00001026 if (!Args.hasArg(options::OPT__analyzer_no_default_checks)) {
Argyrios Kyrtzidisa6d04d52011-02-15 07:42:33 +00001027 CmdArgs.push_back("-analyzer-checker=core");
Ted Kremenek49c79792011-03-24 00:28:47 +00001028 CmdArgs.push_back("-analyzer-checker=deadcode");
1029 CmdArgs.push_back("-analyzer-checker=security");
1030
Argyrios Kyrtzidisa6d04d52011-02-15 07:42:33 +00001031 if (getToolChain().getTriple().getOS() != llvm::Triple::Win32)
1032 CmdArgs.push_back("-analyzer-checker=unix");
Ted Kremenek49c79792011-03-24 00:28:47 +00001033
Argyrios Kyrtzidisa6d04d52011-02-15 07:42:33 +00001034 if (getToolChain().getTriple().getVendor() == llvm::Triple::Apple)
Ted Kremenek49c79792011-03-24 00:28:47 +00001035 CmdArgs.push_back("-analyzer-checker=osx");
Daniel Dunbar58f345ce2009-05-22 00:38:15 +00001036 }
Daniel Dunbard067f7f2009-04-08 23:54:23 +00001037
Daniel Dunbar58f345ce2009-05-22 00:38:15 +00001038 // Set the output format. The default is plist, for (lame) historical
1039 // reasons.
1040 CmdArgs.push_back("-analyzer-output");
1041 if (Arg *A = Args.getLastArg(options::OPT__analyzer_output))
1042 CmdArgs.push_back(A->getValue(Args));
1043 else
1044 CmdArgs.push_back("plist");
Daniel Dunbara2aedc62009-03-18 10:01:51 +00001045
Ted Kremenekfe449a22010-03-22 22:32:05 +00001046 // Disable the presentation of standard compiler warnings when
1047 // using --analyze. We only want to show static analyzer diagnostics
1048 // or frontend errors.
1049 CmdArgs.push_back("-w");
1050
Daniel Dunbara2aedc62009-03-18 10:01:51 +00001051 // Add -Xanalyzer arguments when running as analyzer.
1052 Args.AddAllArgValues(CmdArgs, options::OPT_Xanalyzer);
Mike Stump11289f42009-09-09 15:08:12 +00001053 }
1054
Daniel Dunbar4eadb602009-09-10 01:21:12 +00001055 CheckCodeGenerationOptions(D, Args);
1056
Daniel Dunbar44e71222009-04-29 18:32:25 +00001057 // Perform argument translation for LLVM backend. This
1058 // takes some care in reconciling with llvm-gcc. The
1059 // issue is that llvm-gcc translates these options based on
1060 // the values in cc1, whereas we are processing based on
1061 // the driver arguments.
Daniel Dunbard067f7f2009-04-08 23:54:23 +00001062
Daniel Dunbar44e71222009-04-29 18:32:25 +00001063 // This comes from the default translation the driver + cc1
1064 // would do to enable flag_pic.
1065 //
1066 // FIXME: Centralize this code.
1067 bool PICEnabled = (Args.hasArg(options::OPT_fPIC) ||
1068 Args.hasArg(options::OPT_fpic) ||
1069 Args.hasArg(options::OPT_fPIE) ||
1070 Args.hasArg(options::OPT_fpie));
1071 bool PICDisabled = (Args.hasArg(options::OPT_mkernel) ||
1072 Args.hasArg(options::OPT_static));
1073 const char *Model = getToolChain().GetForcedPicModel();
1074 if (!Model) {
1075 if (Args.hasArg(options::OPT_mdynamic_no_pic))
1076 Model = "dynamic-no-pic";
1077 else if (PICDisabled)
1078 Model = "static";
1079 else if (PICEnabled)
1080 Model = "pic";
Daniel Dunbara2aedc62009-03-18 10:01:51 +00001081 else
Daniel Dunbar44e71222009-04-29 18:32:25 +00001082 Model = getToolChain().GetDefaultRelocationModel();
Daniel Dunbara2aedc62009-03-18 10:01:51 +00001083 }
Daniel Dunbara1b02a22009-11-29 07:18:39 +00001084 if (llvm::StringRef(Model) != "pic") {
1085 CmdArgs.push_back("-mrelocation-model");
1086 CmdArgs.push_back(Model);
1087 }
Daniel Dunbar44e71222009-04-29 18:32:25 +00001088
1089 // Infer the __PIC__ value.
1090 //
1091 // FIXME: This isn't quite right on Darwin, which always sets
1092 // __PIC__=2.
1093 if (strcmp(Model, "pic") == 0 || strcmp(Model, "dynamic-no-pic") == 0) {
Daniel Dunbar5fe08662009-11-29 02:39:08 +00001094 CmdArgs.push_back("-pic-level");
1095 CmdArgs.push_back(Args.hasArg(options::OPT_fPIC) ? "2" : "1");
Daniel Dunbar44e71222009-04-29 18:32:25 +00001096 }
Tanya Lattnerf9d41df2009-11-04 01:18:09 +00001097 if (!Args.hasFlag(options::OPT_fmerge_all_constants,
1098 options::OPT_fno_merge_all_constants))
Daniel Dunbar8bed86c2009-11-20 22:21:36 +00001099 CmdArgs.push_back("-no-merge-all-constants");
Daniel Dunbar306945d2009-09-16 06:17:29 +00001100
Daniel Dunbara1b02a22009-11-29 07:18:39 +00001101 // LLVM Code Generator Options.
1102
Daniel Dunbar0bb03312011-02-09 17:54:19 +00001103 if (Arg *A = Args.getLastArg(options::OPT_mregparm_EQ)) {
1104 CmdArgs.push_back("-mregparm");
1105 CmdArgs.push_back(A->getValue(Args));
1106 }
1107
Roman Divacky65b88cd2011-03-01 17:40:53 +00001108 if (Args.hasFlag(options::OPT_mrtd, options::OPT_mno_rtd, false))
1109 CmdArgs.push_back("-mrtd");
1110
Daniel Dunbara1b02a22009-11-29 07:18:39 +00001111 // FIXME: Set --enable-unsafe-fp-math.
1112 if (Args.hasFlag(options::OPT_fno_omit_frame_pointer,
1113 options::OPT_fomit_frame_pointer))
1114 CmdArgs.push_back("-mdisable-fp-elim");
1115 if (!Args.hasFlag(options::OPT_fzero_initialized_in_bss,
1116 options::OPT_fno_zero_initialized_in_bss))
1117 CmdArgs.push_back("-mno-zero-initialized-in-bss");
Daniel Dunbar7aa71f92011-02-04 02:20:39 +00001118 if (!Args.hasFlag(options::OPT_fstrict_aliasing,
1119 options::OPT_fno_strict_aliasing,
1120 getToolChain().IsStrictAliasingDefault()))
Dan Gohman10169b92010-10-14 22:36:56 +00001121 CmdArgs.push_back("-relaxed-aliasing");
Daniel Dunbar0d8ca9e2010-05-14 22:00:22 +00001122
1123 // Decide whether to use verbose asm. Verbose assembly is the default on
1124 // toolchains which have the integrated assembler on by default.
1125 bool IsVerboseAsmDefault = getToolChain().IsIntegratedAssemblerDefault();
1126 if (Args.hasFlag(options::OPT_fverbose_asm, options::OPT_fno_verbose_asm,
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001127 IsVerboseAsmDefault) ||
Daniel Dunbar0d8ca9e2010-05-14 22:00:22 +00001128 Args.hasArg(options::OPT_dA))
Daniel Dunbara1b02a22009-11-29 07:18:39 +00001129 CmdArgs.push_back("-masm-verbose");
Daniel Dunbar0d8ca9e2010-05-14 22:00:22 +00001130
Daniel Dunbara1b02a22009-11-29 07:18:39 +00001131 if (Args.hasArg(options::OPT_fdebug_pass_structure)) {
1132 CmdArgs.push_back("-mdebug-pass");
1133 CmdArgs.push_back("Structure");
1134 }
1135 if (Args.hasArg(options::OPT_fdebug_pass_arguments)) {
1136 CmdArgs.push_back("-mdebug-pass");
1137 CmdArgs.push_back("Arguments");
1138 }
1139
John McCall8517abc2010-02-19 02:45:38 +00001140 // Enable -mconstructor-aliases except on darwin, where we have to
1141 // work around a linker bug; see <rdar://problem/7651567>.
1142 if (getToolChain().getTriple().getOS() != llvm::Triple::Darwin)
1143 CmdArgs.push_back("-mconstructor-aliases");
NAKAMURA Takumi029d74b2011-02-17 08:50:50 +00001144
John McCall7ef5cb32011-03-18 02:56:14 +00001145 // Darwin's kernel doesn't support guard variables; just die if we
1146 // try to use them.
1147 if (KernelOrKext &&
1148 getToolChain().getTriple().getOS() == llvm::Triple::Darwin)
1149 CmdArgs.push_back("-fforbid-guard-variables");
1150
Douglas Gregordbe39272011-02-01 15:15:22 +00001151 if (Args.hasArg(options::OPT_mms_bitfields)) {
1152 CmdArgs.push_back("-mms-bitfields");
1153 }
John McCall8517abc2010-02-19 02:45:38 +00001154
Daniel Dunbar306945d2009-09-16 06:17:29 +00001155 // This is a coarse approximation of what llvm-gcc actually does, both
1156 // -fasynchronous-unwind-tables and -fnon-call-exceptions interact in more
1157 // complicated ways.
1158 bool AsynchronousUnwindTables =
1159 Args.hasFlag(options::OPT_fasynchronous_unwind_tables,
1160 options::OPT_fno_asynchronous_unwind_tables,
1161 getToolChain().IsUnwindTablesDefault() &&
Daniel Dunbare46b52a2010-03-20 04:52:14 +00001162 !KernelOrKext);
Daniel Dunbar306945d2009-09-16 06:17:29 +00001163 if (Args.hasFlag(options::OPT_funwind_tables, options::OPT_fno_unwind_tables,
1164 AsynchronousUnwindTables))
Daniel Dunbara1b02a22009-11-29 07:18:39 +00001165 CmdArgs.push_back("-munwind-tables");
1166
1167 if (Arg *A = Args.getLastArg(options::OPT_flimited_precision_EQ)) {
1168 CmdArgs.push_back("-mlimit-float-precision");
1169 CmdArgs.push_back(A->getValue(Args));
1170 }
Daniel Dunbar44e71222009-04-29 18:32:25 +00001171
Daniel Dunbar4dbaaa62009-05-06 03:16:41 +00001172 // FIXME: Handle -mtune=.
1173 (void) Args.hasArg(options::OPT_mtune_EQ);
Daniel Dunbar44e71222009-04-29 18:32:25 +00001174
Benjamin Kramercf4371a2009-08-05 14:30:52 +00001175 if (Arg *A = Args.getLastArg(options::OPT_mcmodel_EQ)) {
Daniel Dunbara1b02a22009-11-29 07:18:39 +00001176 CmdArgs.push_back("-mcode-model");
Benjamin Kramercf4371a2009-08-05 14:30:52 +00001177 CmdArgs.push_back(A->getValue(Args));
1178 }
1179
Daniel Dunbar3b3191f2009-09-09 22:33:08 +00001180 // Add target specific cpu and features flags.
1181 switch(getToolChain().getTriple().getArch()) {
1182 default:
1183 break;
Daniel Dunbar4dbaaa62009-05-06 03:16:41 +00001184
Daniel Dunbar0f5c5422009-09-10 04:57:17 +00001185 case llvm::Triple::arm:
1186 case llvm::Triple::thumb:
Daniel Dunbarc9388c12011-03-17 17:10:06 +00001187 AddARMTargetArgs(Args, CmdArgs, KernelOrKext);
Daniel Dunbar0f5c5422009-09-10 04:57:17 +00001188 break;
1189
Eric Christopher0b26a612010-03-02 02:41:08 +00001190 case llvm::Triple::mips:
1191 case llvm::Triple::mipsel:
1192 AddMIPSTargetArgs(Args, CmdArgs);
1193 break;
1194
Bruno Cardoso Lopese7f211c2010-11-09 17:21:19 +00001195 case llvm::Triple::sparc:
1196 AddSparcTargetArgs(Args, CmdArgs);
1197 break;
1198
Daniel Dunbar3b3191f2009-09-09 22:33:08 +00001199 case llvm::Triple::x86:
1200 case llvm::Triple::x86_64:
1201 AddX86TargetArgs(Args, CmdArgs);
1202 break;
Daniel Dunbar44e71222009-04-29 18:32:25 +00001203 }
1204
Daniel Dunbar976a2f52010-08-11 23:07:47 +00001205 // Pass the linker version in use.
1206 if (Arg *A = Args.getLastArg(options::OPT_mlinker_version_EQ)) {
1207 CmdArgs.push_back("-target-linker-version");
1208 CmdArgs.push_back(A->getValue(Args));
1209 }
1210
Nick Lewycky75033772011-02-02 06:43:03 +00001211 // -mno-omit-leaf-frame-pointer is the default on Darwin.
Daniel Dunbarbb7ac522010-07-01 01:31:45 +00001212 if (Args.hasFlag(options::OPT_momit_leaf_frame_pointer,
Nick Lewycky75033772011-02-02 06:43:03 +00001213 options::OPT_mno_omit_leaf_frame_pointer,
1214 getToolChain().getTriple().getOS() != llvm::Triple::Darwin))
Daniel Dunbarbb7ac522010-07-01 01:31:45 +00001215 CmdArgs.push_back("-momit-leaf-frame-pointer");
1216
Dan Gohmand1e76b92010-01-08 02:20:44 +00001217 // -fno-math-errno is default.
1218 if (Args.hasFlag(options::OPT_fmath_errno,
Daniel Dunbar44e71222009-04-29 18:32:25 +00001219 options::OPT_fno_math_errno,
Dan Gohmand1e76b92010-01-08 02:20:44 +00001220 false))
1221 CmdArgs.push_back("-fmath-errno");
Daniel Dunbar44e71222009-04-29 18:32:25 +00001222
Daniel Dunbarfcc49a82010-05-12 18:19:58 +00001223 // Explicitly error on some things we know we don't support and can't just
1224 // ignore.
1225 types::ID InputType = Inputs[0].getType();
Daniel Dunbar4ed214a2010-09-24 19:39:37 +00001226 if (!Args.hasArg(options::OPT_fallow_unsupported)) {
1227 Arg *Unsupported;
1228 if ((Unsupported = Args.getLastArg(options::OPT_MG)) ||
Argyrios Kyrtzidis74825bc2010-10-08 00:25:19 +00001229 (Unsupported = Args.getLastArg(options::OPT_iframework)))
Daniel Dunbar4ed214a2010-09-24 19:39:37 +00001230 D.Diag(clang::diag::err_drv_clang_unsupported)
Daniel Dunbarfcc49a82010-05-12 18:19:58 +00001231 << Unsupported->getOption().getName();
Daniel Dunbar4ed214a2010-09-24 19:39:37 +00001232
1233 if (types::isCXX(InputType) &&
1234 getToolChain().getTriple().getOS() == llvm::Triple::Darwin &&
1235 getToolChain().getTriple().getArch() == llvm::Triple::x86) {
1236 if ((Unsupported = Args.getLastArg(options::OPT_fapple_kext)))
1237 D.Diag(clang::diag::err_drv_clang_unsupported_opt_cxx_darwin_i386)
1238 << Unsupported->getOption().getName();
1239 }
Daniel Dunbarfcc49a82010-05-12 18:19:58 +00001240 }
1241
Daniel Dunbara2aedc62009-03-18 10:01:51 +00001242 Args.AddAllArgs(CmdArgs, options::OPT_v);
Daniel Dunbard4352752010-08-24 22:44:13 +00001243 Args.AddLastArg(CmdArgs, options::OPT_H);
Daniel Dunbarac540b32011-02-02 21:11:35 +00001244 if (D.CCPrintHeaders) {
1245 CmdArgs.push_back("-header-include-file");
1246 CmdArgs.push_back(D.CCPrintHeadersFilename ?
1247 D.CCPrintHeadersFilename : "-");
1248 }
Daniel Dunbara2aedc62009-03-18 10:01:51 +00001249 Args.AddLastArg(CmdArgs, options::OPT_P);
Mike Stump11289f42009-09-09 15:08:12 +00001250 Args.AddLastArg(CmdArgs, options::OPT_print_ivar_layout);
Daniel Dunbara2aedc62009-03-18 10:01:51 +00001251
1252 // Special case debug options to only pass -g to clang. This is
1253 // wrong.
Rafael Espindola08a692a2010-03-07 04:46:18 +00001254 Args.ClaimAllArgs(options::OPT_g_Group);
Daniel Dunbar4083d042010-05-12 18:19:55 +00001255 if (Arg *A = Args.getLastArg(options::OPT_g_Group))
1256 if (!A->getOption().matches(options::OPT_g0))
1257 CmdArgs.push_back("-g");
Daniel Dunbara2aedc62009-03-18 10:01:51 +00001258
Rafael Espindola66bfb2752010-05-06 21:06:04 +00001259 Args.AddAllArgs(CmdArgs, options::OPT_ffunction_sections);
1260 Args.AddAllArgs(CmdArgs, options::OPT_fdata_sections);
1261
Chris Lattner3c77a352010-06-22 00:03:40 +00001262 Args.AddAllArgs(CmdArgs, options::OPT_finstrument_functions);
1263
Daniel Dunbara2aedc62009-03-18 10:01:51 +00001264 Args.AddLastArg(CmdArgs, options::OPT_nostdinc);
Douglas Gregor64b046f2010-03-24 20:13:48 +00001265 Args.AddLastArg(CmdArgs, options::OPT_nostdincxx);
Rafael Espindolab3549d72009-10-26 13:36:57 +00001266 Args.AddLastArg(CmdArgs, options::OPT_nobuiltininc);
Daniel Dunbara2aedc62009-03-18 10:01:51 +00001267
Daniel Dunbar34e0b8c2009-12-15 01:02:52 +00001268 // Pass the path to compiler resource files.
Daniel Dunbar34e0b8c2009-12-15 01:02:52 +00001269 CmdArgs.push_back("-resource-dir");
Daniel Dunbar3f3e2cd2010-01-20 02:35:16 +00001270 CmdArgs.push_back(D.ResourceDir.c_str());
Daniel Dunbar9dc82a22009-04-07 21:42:00 +00001271
Argyrios Kyrtzidis71731d62010-11-03 22:45:23 +00001272 Args.AddLastArg(CmdArgs, options::OPT_working_directory);
1273
Daniel Dunbard067f7f2009-04-08 23:54:23 +00001274 // Add preprocessing options like -I, -D, etc. if we are using the
1275 // preprocessor.
1276 //
1277 // FIXME: Support -fpreprocessed
Daniel Dunbard067f7f2009-04-08 23:54:23 +00001278 if (types::getPreprocessedType(InputType) != types::TY_INVALID)
Douglas Gregor111af7d2009-04-18 00:34:01 +00001279 AddPreprocessingOptions(D, Args, CmdArgs, Output, Inputs);
Daniel Dunbara2aedc62009-03-18 10:01:51 +00001280
Daniel Dunbar58f78332009-09-17 06:53:36 +00001281 // Manually translate -O to -O2 and -O4 to -O3; let clang reject
Daniel Dunbar13864952009-03-24 20:17:30 +00001282 // others.
1283 if (Arg *A = Args.getLastArg(options::OPT_O_Group)) {
Daniel Dunbar0bfb21e2009-11-19 03:26:40 +00001284 if (A->getOption().matches(options::OPT_O4))
Daniel Dunbar13864952009-03-24 20:17:30 +00001285 CmdArgs.push_back("-O3");
Daniel Dunbar9296f632010-05-27 06:51:08 +00001286 else if (A->getOption().matches(options::OPT_O) &&
1287 A->getValue(Args)[0] == '\0')
Daniel Dunbar58f78332009-09-17 06:53:36 +00001288 CmdArgs.push_back("-O2");
Daniel Dunbarfc8aefb2010-11-13 18:17:11 +00001289 else if (A->getOption().matches(options::OPT_O) &&
1290 A->getValue(Args)[0] == 'z' &&
1291 A->getValue(Args)[1] == '\0')
1292 CmdArgs.push_back("-Os");
Daniel Dunbara2aedc62009-03-18 10:01:51 +00001293 else
Daniel Dunbar7ef5ed62009-03-18 23:39:35 +00001294 A->render(Args, CmdArgs);
Daniel Dunbara2aedc62009-03-18 10:01:51 +00001295 }
1296
Daniel Dunbar945577c2009-10-29 02:24:45 +00001297 Args.AddAllArgs(CmdArgs, options::OPT_W_Group);
1298 Args.AddLastArg(CmdArgs, options::OPT_pedantic);
1299 Args.AddLastArg(CmdArgs, options::OPT_pedantic_errors);
Daniel Dunbara2aedc62009-03-18 10:01:51 +00001300 Args.AddLastArg(CmdArgs, options::OPT_w);
Daniel Dunbarc44b4cc2009-04-07 22:13:21 +00001301
1302 // Handle -{std, ansi, trigraphs} -- take the last of -{std, ansi}
1303 // (-ansi is equivalent to -std=c89).
1304 //
1305 // If a std is supplied, only add -trigraphs if it follows the
1306 // option.
1307 if (Arg *Std = Args.getLastArg(options::OPT_std_EQ, options::OPT_ansi)) {
1308 if (Std->getOption().matches(options::OPT_ansi))
Nuno Lopes275225d2009-10-16 14:28:06 +00001309 if (types::isCXX(InputType))
Daniel Dunbar1c7577c2009-11-04 06:24:38 +00001310 CmdArgs.push_back("-std=c++98");
Nuno Lopes275225d2009-10-16 14:28:06 +00001311 else
Daniel Dunbar1c7577c2009-11-04 06:24:38 +00001312 CmdArgs.push_back("-std=c89");
Daniel Dunbarc44b4cc2009-04-07 22:13:21 +00001313 else
1314 Std->render(Args, CmdArgs);
1315
Daniel Dunbar3f1a1ff2010-06-14 21:23:08 +00001316 if (Arg *A = Args.getLastArg(options::OPT_std_EQ, options::OPT_ansi,
1317 options::OPT_trigraphs))
1318 if (A != Std)
Daniel Dunbarc44b4cc2009-04-07 22:13:21 +00001319 A->render(Args, CmdArgs);
Daniel Dunbar72a60902009-04-26 01:10:38 +00001320 } else {
1321 // Honor -std-default.
Daniel Dunbar12998192010-01-29 21:03:02 +00001322 //
1323 // FIXME: Clang doesn't correctly handle -std= when the input language
1324 // doesn't match. For the time being just ignore this for C++ inputs;
1325 // eventually we want to do all the standard defaulting here instead of
1326 // splitting it between the driver and clang -cc1.
1327 if (!types::isCXX(InputType))
1328 Args.AddAllArgsTranslated(CmdArgs, options::OPT_std_default_EQ,
1329 "-std=", /*Joined=*/true);
Daniel Dunbarc44b4cc2009-04-07 22:13:21 +00001330 Args.AddLastArg(CmdArgs, options::OPT_trigraphs);
Daniel Dunbar72a60902009-04-26 01:10:38 +00001331 }
Daniel Dunbard067f7f2009-04-08 23:54:23 +00001332
Chandler Carruthe0391482010-05-22 02:21:53 +00001333 // Translate GCC's misnamer '-fasm' arguments to '-fgnu-keywords'.
1334 if (Arg *Asm = Args.getLastArg(options::OPT_fasm, options::OPT_fno_asm)) {
1335 if (Asm->getOption().matches(options::OPT_fasm))
1336 CmdArgs.push_back("-fgnu-keywords");
1337 else
1338 CmdArgs.push_back("-fno-gnu-keywords");
1339 }
1340
Daniel Dunbara2aedc62009-03-18 10:01:51 +00001341 if (Arg *A = Args.getLastArg(options::OPT_ftemplate_depth_)) {
1342 CmdArgs.push_back("-ftemplate-depth");
1343 CmdArgs.push_back(A->getValue(Args));
1344 }
1345
Argyrios Kyrtzidisef6c8da2010-11-18 00:20:36 +00001346 if (Arg *A = Args.getLastArg(options::OPT_Wlarge_by_value_copy_EQ,
1347 options::OPT_Wlarge_by_value_copy_def)) {
1348 CmdArgs.push_back("-Wlarge-by-value-copy");
Argyrios Kyrtzidisaf84ec02010-11-17 23:11:54 +00001349 if (A->getNumValues())
1350 CmdArgs.push_back(A->getValue(Args));
1351 else
Argyrios Kyrtzidisef6c8da2010-11-18 00:20:36 +00001352 CmdArgs.push_back("64"); // default value for -Wlarge-by-value-copy.
Argyrios Kyrtzidisaf84ec02010-11-17 23:11:54 +00001353 }
1354
Daniel Dunbarfffd1812009-11-19 04:00:53 +00001355 if (Args.hasArg(options::OPT__relocatable_pch))
Daniel Dunbar8bed86c2009-11-20 22:21:36 +00001356 CmdArgs.push_back("-relocatable-pch");
Mike Stump11289f42009-09-09 15:08:12 +00001357
Daniel Dunbar1c7577c2009-11-04 06:24:38 +00001358 if (Arg *A = Args.getLastArg(options::OPT_fconstant_string_class_EQ)) {
1359 CmdArgs.push_back("-fconstant-string-class");
1360 CmdArgs.push_back(A->getValue(Args));
1361 }
David Chisnall5778fce2009-08-31 16:41:57 +00001362
Chris Lattnere23003d2010-01-09 21:54:33 +00001363 if (Arg *A = Args.getLastArg(options::OPT_ftabstop_EQ)) {
1364 CmdArgs.push_back("-ftabstop");
1365 CmdArgs.push_back(A->getValue(Args));
1366 }
1367
Chris Lattnerb35583d2010-04-07 20:49:23 +00001368 CmdArgs.push_back("-ferror-limit");
1369 if (Arg *A = Args.getLastArg(options::OPT_ferror_limit_EQ))
1370 CmdArgs.push_back(A->getValue(Args));
1371 else
1372 CmdArgs.push_back("19");
Douglas Gregorffed1cb2010-04-20 07:18:24 +00001373
Chandler Carrutha77a7272010-05-06 04:55:18 +00001374 if (Arg *A = Args.getLastArg(options::OPT_fmacro_backtrace_limit_EQ)) {
1375 CmdArgs.push_back("-fmacro-backtrace-limit");
Douglas Gregorcd121fb2010-05-04 17:13:42 +00001376 CmdArgs.push_back(A->getValue(Args));
Chandler Carrutha77a7272010-05-06 04:55:18 +00001377 }
1378
1379 if (Arg *A = Args.getLastArg(options::OPT_ftemplate_backtrace_limit_EQ)) {
1380 CmdArgs.push_back("-ftemplate-backtrace-limit");
Douglas Gregorffed1cb2010-04-20 07:18:24 +00001381 CmdArgs.push_back(A->getValue(Args));
Chandler Carrutha77a7272010-05-06 04:55:18 +00001382 }
1383
Daniel Dunbar2c978472009-11-04 06:24:47 +00001384 // Pass -fmessage-length=.
Daniel Dunbar84bb7932009-11-30 08:40:54 +00001385 CmdArgs.push_back("-fmessage-length");
Daniel Dunbar2c978472009-11-04 06:24:47 +00001386 if (Arg *A = Args.getLastArg(options::OPT_fmessage_length_EQ)) {
Daniel Dunbar84bb7932009-11-30 08:40:54 +00001387 CmdArgs.push_back(A->getValue(Args));
Daniel Dunbar2c978472009-11-04 06:24:47 +00001388 } else {
1389 // If -fmessage-length=N was not specified, determine whether this is a
1390 // terminal and, if so, implicitly define -fmessage-length appropriately.
1391 unsigned N = llvm::sys::Process::StandardErrColumns();
Daniel Dunbar2c978472009-11-04 06:24:47 +00001392 CmdArgs.push_back(Args.MakeArgString(llvm::Twine(N)));
1393 }
1394
Daniel Dunbare357d562009-12-03 18:42:11 +00001395 if (const Arg *A = Args.getLastArg(options::OPT_fvisibility_EQ)) {
1396 CmdArgs.push_back("-fvisibility");
1397 CmdArgs.push_back(A->getValue(Args));
1398 }
1399
Douglas Gregor08329632010-06-15 17:05:35 +00001400 Args.AddLastArg(CmdArgs, options::OPT_fvisibility_inlines_hidden);
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001401
Daniel Dunbare46b52a2010-03-20 04:52:14 +00001402 // -fhosted is default.
1403 if (KernelOrKext || Args.hasFlag(options::OPT_ffreestanding,
1404 options::OPT_fhosted,
1405 false))
1406 CmdArgs.push_back("-ffreestanding");
1407
Daniel Dunbare357d562009-12-03 18:42:11 +00001408 // Forward -f (flag) options which we can pass directly.
Mike Stumpd9546382009-12-12 01:27:46 +00001409 Args.AddLastArg(CmdArgs, options::OPT_fcatch_undefined_behavior);
Daniel Dunbar3a148f22009-04-07 21:51:40 +00001410 Args.AddLastArg(CmdArgs, options::OPT_femit_all_decls);
Daniel Dunbar3a148f22009-04-07 21:51:40 +00001411 Args.AddLastArg(CmdArgs, options::OPT_fheinous_gnu_extensions);
Devang Patel91bbb552010-09-30 19:05:55 +00001412 Args.AddLastArg(CmdArgs, options::OPT_flimit_debug_info);
Daniel Dunbar733b0f82011-03-01 18:49:30 +00001413 if (getToolChain().SupportsProfiling())
1414 Args.AddLastArg(CmdArgs, options::OPT_pg);
Daniel Dunbar35621a92010-03-16 16:57:46 +00001415
1416 // -flax-vector-conversions is default.
1417 if (!Args.hasFlag(options::OPT_flax_vector_conversions,
1418 options::OPT_fno_lax_vector_conversions))
1419 CmdArgs.push_back("-fno-lax-vector-conversions");
1420
Daniel Dunbar16334e12010-04-10 16:20:23 +00001421 // Handle -fobjc-gc and -fobjc-gc-only. They are exclusive, and -fobjc-gc-only
1422 // takes precedence.
1423 const Arg *GCArg = Args.getLastArg(options::OPT_fobjc_gc_only);
1424 if (!GCArg)
1425 GCArg = Args.getLastArg(options::OPT_fobjc_gc);
1426 if (GCArg) {
1427 if (getToolChain().SupportsObjCGC()) {
1428 GCArg->render(Args, CmdArgs);
1429 } else {
1430 // FIXME: We should move this to a hard error.
1431 D.Diag(clang::diag::warn_drv_objc_gc_unsupported)
1432 << GCArg->getAsString(Args);
1433 }
1434 }
1435
Fariborz Jahaniana4cfff82011-01-07 01:05:02 +00001436 if (Args.getLastArg(options::OPT_fapple_kext))
1437 CmdArgs.push_back("-fapple-kext");
1438
Daniel Dunbar3a148f22009-04-07 21:51:40 +00001439 Args.AddLastArg(CmdArgs, options::OPT_fno_show_column);
Fariborz Jahaniana4404f22009-05-22 20:17:16 +00001440 Args.AddLastArg(CmdArgs, options::OPT_fobjc_sender_dependent_dispatch);
Chris Lattner69686412009-04-21 05:34:31 +00001441 Args.AddLastArg(CmdArgs, options::OPT_fdiagnostics_print_source_range_info);
Douglas Gregoreec975c2010-08-19 20:24:43 +00001442 Args.AddLastArg(CmdArgs, options::OPT_fdiagnostics_parseable_fixits);
Daniel Dunbar3a148f22009-04-07 21:51:40 +00001443 Args.AddLastArg(CmdArgs, options::OPT_ftime_report);
1444 Args.AddLastArg(CmdArgs, options::OPT_ftrapv);
David Chisnalldd84ef12010-09-17 18:29:54 +00001445
1446 if (Arg *A = Args.getLastArg(options::OPT_ftrapv_handler_EQ)) {
1447 CmdArgs.push_back("-ftrapv-handler");
1448 CmdArgs.push_back(A->getValue(Args));
1449 }
1450
Chris Lattner51924e512010-06-26 21:25:03 +00001451 Args.AddLastArg(CmdArgs, options::OPT_fwrapv);
Daniel Dunbar3a148f22009-04-07 21:51:40 +00001452 Args.AddLastArg(CmdArgs, options::OPT_fwritable_strings);
Eric Christopherf387dbd2010-08-07 23:08:14 +00001453 Args.AddLastArg(CmdArgs, options::OPT_funroll_loops);
Daniel Dunbara2aedc62009-03-18 10:01:51 +00001454
Daniel Dunbara77eaeb2009-09-03 04:54:28 +00001455 Args.AddLastArg(CmdArgs, options::OPT_pthread);
1456
Daniel Dunbar4930e332009-11-17 08:07:36 +00001457 // -stack-protector=0 is default.
1458 unsigned StackProtectorLevel = 0;
Bill Wendlingd63bbad2009-06-28 07:36:13 +00001459 if (Arg *A = Args.getLastArg(options::OPT_fno_stack_protector,
1460 options::OPT_fstack_protector_all,
1461 options::OPT_fstack_protector)) {
Daniel Dunbar4930e332009-11-17 08:07:36 +00001462 if (A->getOption().matches(options::OPT_fstack_protector))
1463 StackProtectorLevel = 1;
1464 else if (A->getOption().matches(options::OPT_fstack_protector_all))
1465 StackProtectorLevel = 2;
1466 } else
1467 StackProtectorLevel = getToolChain().GetDefaultStackProtectorLevel();
1468 if (StackProtectorLevel) {
1469 CmdArgs.push_back("-stack-protector");
1470 CmdArgs.push_back(Args.MakeArgString(llvm::Twine(StackProtectorLevel)));
Bill Wendlingd63bbad2009-06-28 07:36:13 +00001471 }
1472
Daniel Dunbard18049a2009-04-07 21:16:11 +00001473 // Forward -f options with positive and negative forms; we translate
1474 // these by hand.
1475
Fariborz Jahaniana4cfff82011-01-07 01:05:02 +00001476 if (Args.hasArg(options::OPT_mkernel)) {
Daniel Dunbar80f787c2011-02-04 17:24:47 +00001477 if (!Args.hasArg(options::OPT_fapple_kext) && types::isCXX(InputType))
Fariborz Jahaniana4cfff82011-01-07 01:05:02 +00001478 CmdArgs.push_back("-fapple-kext");
1479 if (!Args.hasArg(options::OPT_fbuiltin))
1480 CmdArgs.push_back("-fno-builtin");
1481 }
Daniel Dunbar4930e332009-11-17 08:07:36 +00001482 // -fbuiltin is default.
Fariborz Jahaniana4cfff82011-01-07 01:05:02 +00001483 else if (!Args.hasFlag(options::OPT_fbuiltin, options::OPT_fno_builtin))
Daniel Dunbar484afa22009-11-19 04:55:23 +00001484 CmdArgs.push_back("-fno-builtin");
Daniel Dunbard18049a2009-04-07 21:16:11 +00001485
Nuno Lopes13c88c72009-12-16 16:59:22 +00001486 if (!Args.hasFlag(options::OPT_fassume_sane_operator_new,
1487 options::OPT_fno_assume_sane_operator_new))
1488 CmdArgs.push_back("-fno-assume-sane-operator-new");
1489
Daniel Dunbar4930e332009-11-17 08:07:36 +00001490 // -fblocks=0 is default.
1491 if (Args.hasFlag(options::OPT_fblocks, options::OPT_fno_blocks,
David Chisnallda209912011-02-28 17:11:43 +00001492 getToolChain().IsBlocksDefault()) ||
1493 (Args.hasArg(options::OPT_fgnu_runtime) &&
1494 Args.hasArg(options::OPT_fobjc_nonfragile_abi) &&
1495 !Args.hasArg(options::OPT_fno_blocks))) {
Daniel Dunbar4930e332009-11-17 08:07:36 +00001496 CmdArgs.push_back("-fblocks");
David Chisnall950a9512009-11-17 19:33:30 +00001497 }
Daniel Dunbard18049a2009-04-07 21:16:11 +00001498
John McCalldfea9982010-04-09 19:12:06 +00001499 // -faccess-control is default.
John McCall3155f572010-04-09 19:03:51 +00001500 if (Args.hasFlag(options::OPT_fno_access_control,
1501 options::OPT_faccess_control,
John McCalldfea9982010-04-09 19:12:06 +00001502 false))
John McCall3155f572010-04-09 19:03:51 +00001503 CmdArgs.push_back("-fno-access-control");
John McCall59bb1d42010-03-17 01:32:13 +00001504
Anders Carlssond470fef2010-11-21 00:09:52 +00001505 // -felide-constructors is the default.
1506 if (Args.hasFlag(options::OPT_fno_elide_constructors,
1507 options::OPT_felide_constructors,
1508 false))
1509 CmdArgs.push_back("-fno-elide-constructors");
1510
Anders Carlssone96ab552011-02-28 02:27:16 +00001511 // Add exception args.
1512 addExceptionArgs(Args, InputType, getToolChain().getTriple(),
1513 KernelOrKext, IsRewriter, CmdArgs);
Mike Stump183c3d22009-07-31 23:15:31 +00001514
Daniel Dunbar3241d402010-02-10 18:49:11 +00001515 if (getToolChain().UseSjLjExceptions())
1516 CmdArgs.push_back("-fsjlj-exceptions");
1517
Daniel Dunbar0730e4f2009-11-17 07:06:20 +00001518 // -frtti is default.
Daniel Dunbare46b52a2010-03-20 04:52:14 +00001519 if (KernelOrKext ||
1520 !Args.hasFlag(options::OPT_frtti, options::OPT_fno_rtti))
Daniel Dunbar484afa22009-11-19 04:55:23 +00001521 CmdArgs.push_back("-fno-rtti");
Mike Stump183c3d22009-07-31 23:15:31 +00001522
Argyrios Kyrtzidis74825bc2010-10-08 00:25:19 +00001523 // -fshort-enums=0 is default.
1524 // FIXME: Are there targers where -fshort-enums is on by default ?
1525 if (Args.hasFlag(options::OPT_fshort_enums,
1526 options::OPT_fno_short_enums, false))
1527 CmdArgs.push_back("-fshort-enums");
1528
Daniel Dunbard609b7b2009-11-17 06:37:03 +00001529 // -fsigned-char is default.
Daniel Dunbar5bdd2992009-11-25 10:14:30 +00001530 if (!Args.hasFlag(options::OPT_fsigned_char, options::OPT_funsigned_char,
Daniel Dunbard609b7b2009-11-17 06:37:03 +00001531 isSignedCharDefault(getToolChain().getTriple())))
Daniel Dunbar5fe08662009-11-29 02:39:08 +00001532 CmdArgs.push_back("-fno-signed-char");
Eli Friedman327f0b52009-06-05 07:21:14 +00001533
Anders Carlssonfcd764a2010-02-06 23:23:06 +00001534 // -fthreadsafe-static is default.
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001535 if (!Args.hasFlag(options::OPT_fthreadsafe_statics,
Anders Carlssonfcd764a2010-02-06 23:23:06 +00001536 options::OPT_fno_threadsafe_statics))
1537 CmdArgs.push_back("-fno-threadsafe-statics");
1538
Daniel Dunbarfe06df42010-03-20 04:15:41 +00001539 // -fuse-cxa-atexit is default.
Anton Korobeynikov82b33332010-09-11 11:17:06 +00001540 if (KernelOrKext ||
1541 !Args.hasFlag(options::OPT_fuse_cxa_atexit, options::OPT_fno_use_cxa_atexit,
NAKAMURA Takumi6bdc8a22010-10-10 01:53:03 +00001542 getToolChain().getTriple().getOS() != llvm::Triple::Cygwin &&
NAKAMURA Takumi31ea2f12011-02-17 08:51:38 +00001543 getToolChain().getTriple().getOS() != llvm::Triple::MinGW32))
Daniel Dunbarfe06df42010-03-20 04:15:41 +00001544 CmdArgs.push_back("-fno-use-cxa-atexit");
1545
Daniel Dunbar0730e4f2009-11-17 07:06:20 +00001546 // -fms-extensions=0 is default.
Daniel Dunbar5bdd2992009-11-25 10:14:30 +00001547 if (Args.hasFlag(options::OPT_fms_extensions, options::OPT_fno_ms_extensions,
Daniel Dunbar0730e4f2009-11-17 07:06:20 +00001548 getToolChain().getTriple().getOS() == llvm::Triple::Win32))
1549 CmdArgs.push_back("-fms-extensions");
1550
Michael J. Spencer4992ca4b2010-10-21 05:21:48 +00001551 // -fmsc-version=1300 is default.
1552 if (Args.hasFlag(options::OPT_fms_extensions, options::OPT_fno_ms_extensions,
1553 getToolChain().getTriple().getOS() == llvm::Triple::Win32) ||
1554 Args.hasArg(options::OPT_fmsc_version)) {
1555 llvm::StringRef msc_ver = Args.getLastArgValue(options::OPT_fmsc_version);
1556 if (msc_ver.empty())
1557 CmdArgs.push_back("-fmsc-version=1300");
1558 else
1559 CmdArgs.push_back(Args.MakeArgString("-fmsc-version=" + msc_ver));
1560 }
1561
1562
Dawn Perchik68bb1b42010-09-02 23:59:25 +00001563 // -fborland-extensions=0 is default.
1564 if (Args.hasFlag(options::OPT_fborland_extensions,
1565 options::OPT_fno_borland_extensions, false))
1566 CmdArgs.push_back("-fborland-extensions");
1567
Chandler Carruthe03aa552010-04-17 20:17:31 +00001568 // -fgnu-keywords default varies depending on language; only pass if
1569 // specified.
1570 if (Arg *A = Args.getLastArg(options::OPT_fgnu_keywords,
Daniel Dunbardb059592010-04-24 17:56:39 +00001571 options::OPT_fno_gnu_keywords))
1572 A->render(Args, CmdArgs);
Chandler Carruthe03aa552010-04-17 20:17:31 +00001573
Daniel Dunbar99b55242010-07-19 19:44:22 +00001574 // -fnext-runtime defaults to on Darwin and when rewriting Objective-C, and is
1575 // -the -cc1 default.
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001576 bool NeXTRuntimeIsDefault =
Daniel Dunbar99b55242010-07-19 19:44:22 +00001577 IsRewriter || getToolChain().getTriple().getOS() == llvm::Triple::Darwin;
Daniel Dunbar5bdd2992009-11-25 10:14:30 +00001578 if (!Args.hasFlag(options::OPT_fnext_runtime, options::OPT_fgnu_runtime,
Daniel Dunbar99b55242010-07-19 19:44:22 +00001579 NeXTRuntimeIsDefault))
Daniel Dunbar4656c532009-11-17 07:07:28 +00001580 CmdArgs.push_back("-fgnu-runtime");
1581
Daniel Dunbar4930e332009-11-17 08:07:36 +00001582 // -fobjc-nonfragile-abi=0 is default.
1583 if (types::isObjC(InputType)) {
Daniel Dunbarc1dd0e92010-09-20 18:19:55 +00001584 // Compute the Objective-C ABI "version" to use. Version numbers are
1585 // slightly confusing for historical reasons:
1586 // 1 - Traditional "fragile" ABI
1587 // 2 - Non-fragile ABI, version 1
1588 // 3 - Non-fragile ABI, version 2
Daniel Dunbar12c82082010-04-28 23:25:24 +00001589 unsigned Version = 1;
Daniel Dunbarc1dd0e92010-09-20 18:19:55 +00001590 // If -fobjc-abi-version= is present, use that to set the version.
Daniel Dunbar12c82082010-04-28 23:25:24 +00001591 if (Arg *A = Args.getLastArg(options::OPT_fobjc_abi_version_EQ)) {
1592 if (llvm::StringRef(A->getValue(Args)) == "1")
1593 Version = 1;
1594 else if (llvm::StringRef(A->getValue(Args)) == "2")
1595 Version = 2;
Fariborz Jahanianf51a3872010-09-15 16:00:51 +00001596 else if (llvm::StringRef(A->getValue(Args)) == "3")
1597 Version = 3;
Daniel Dunbar12c82082010-04-28 23:25:24 +00001598 else
1599 D.Diag(clang::diag::err_drv_clang_unsupported) << A->getAsString(Args);
Daniel Dunbarc1dd0e92010-09-20 18:19:55 +00001600 } else {
1601 // Otherwise, determine if we are using the non-fragile ABI.
1602 if (Args.hasFlag(options::OPT_fobjc_nonfragile_abi,
1603 options::OPT_fno_objc_nonfragile_abi,
1604 getToolChain().IsObjCNonFragileABIDefault())) {
1605 // Determine the non-fragile ABI version to use.
Daniel Dunbaraeed5fe2010-11-11 16:08:59 +00001606#ifdef DISABLE_DEFAULT_NONFRAGILEABI_TWO
1607 unsigned NonFragileABIVersion = 1;
1608#else
Daniel Dunbarc1dd0e92010-09-20 18:19:55 +00001609 unsigned NonFragileABIVersion = 2;
Daniel Dunbaraeed5fe2010-11-11 16:08:59 +00001610#endif
Daniel Dunbarc1dd0e92010-09-20 18:19:55 +00001611
1612 if (Arg *A = Args.getLastArg(
1613 options::OPT_fobjc_nonfragile_abi_version_EQ)) {
1614 if (llvm::StringRef(A->getValue(Args)) == "1")
1615 NonFragileABIVersion = 1;
1616 else if (llvm::StringRef(A->getValue(Args)) == "2")
1617 NonFragileABIVersion = 2;
1618 else
1619 D.Diag(clang::diag::err_drv_clang_unsupported)
1620 << A->getAsString(Args);
1621 }
1622
1623 Version = 1 + NonFragileABIVersion;
1624 } else {
1625 Version = 1;
1626 }
Daniel Dunbar12c82082010-04-28 23:25:24 +00001627 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001628
Fariborz Jahanianf51a3872010-09-15 16:00:51 +00001629 if (Version == 2 || Version == 3) {
Fariborz Jahanian3aa19e9a2011-01-04 20:05:20 +00001630 CmdArgs.push_back("-fobjc-nonfragile-abi");
Daniel Dunbarfca18c1b42010-04-24 17:56:46 +00001631
1632 // -fobjc-dispatch-method is only relevant with the nonfragile-abi, and
1633 // legacy is the default.
1634 if (!Args.hasFlag(options::OPT_fobjc_legacy_dispatch,
1635 options::OPT_fno_objc_legacy_dispatch,
1636 getToolChain().IsObjCLegacyDispatchDefault())) {
1637 if (getToolChain().UseObjCMixedDispatch())
1638 CmdArgs.push_back("-fobjc-dispatch-method=mixed");
1639 else
1640 CmdArgs.push_back("-fobjc-dispatch-method=non-legacy");
1641 }
Daniel Dunbar2e3f2c82010-02-01 21:07:25 +00001642 }
NAKAMURA Takumi029d74b2011-02-17 08:50:50 +00001643
Ted Kremenek1d56c9e2010-12-23 21:35:43 +00001644 // -fobjc-default-synthesize-properties=0 is default.
1645 if (Args.hasFlag(options::OPT_fobjc_default_synthesize_properties,
1646 options::OPT_fno_objc_default_synthesize_properties,
1647 getToolChain().IsObjCDefaultSynthPropertiesDefault())) {
1648 CmdArgs.push_back("-fobjc-default-synthesize-properties");
1649 }
Daniel Dunbar4930e332009-11-17 08:07:36 +00001650 }
1651
Daniel Dunbar2e3f2c82010-02-01 21:07:25 +00001652 if (!Args.hasFlag(options::OPT_fassume_sane_operator_new,
1653 options::OPT_fno_assume_sane_operator_new))
1654 CmdArgs.push_back("-fno-assume-sane-operator-new");
1655
Daniel Dunbar34d7a992010-04-27 15:34:57 +00001656 // -fconstant-cfstrings is default, and may be subject to argument translation
1657 // on Darwin.
1658 if (!Args.hasFlag(options::OPT_fconstant_cfstrings,
1659 options::OPT_fno_constant_cfstrings) ||
1660 !Args.hasFlag(options::OPT_mconstant_cfstrings,
1661 options::OPT_mno_constant_cfstrings))
1662 CmdArgs.push_back("-fno-constant-cfstrings");
1663
John Thompsoned4e2952009-11-05 20:14:16 +00001664 // -fshort-wchar default varies depending on platform; only
1665 // pass if specified.
Daniel Dunbar2cb4e7a2010-04-27 15:35:03 +00001666 if (Arg *A = Args.getLastArg(options::OPT_fshort_wchar))
1667 A->render(Args, CmdArgs);
John Thompsoned4e2952009-11-05 20:14:16 +00001668
Daniel Dunbar1a8a2e82009-10-29 02:39:57 +00001669 // -fno-pascal-strings is default, only pass non-default. If the tool chain
1670 // happened to translate to -mpascal-strings, we want to back translate here.
Daniel Dunbard4510f22009-04-07 23:51:44 +00001671 //
1672 // FIXME: This is gross; that translation should be pulled from the
1673 // tool chain.
Daniel Dunbard067f7f2009-04-08 23:54:23 +00001674 if (Args.hasFlag(options::OPT_fpascal_strings,
Daniel Dunbard4510f22009-04-07 23:51:44 +00001675 options::OPT_fno_pascal_strings,
1676 false) ||
1677 Args.hasFlag(options::OPT_mpascal_strings,
1678 options::OPT_mno_pascal_strings,
1679 false))
Daniel Dunbard18049a2009-04-07 21:16:11 +00001680 CmdArgs.push_back("-fpascal-strings");
NAKAMURA Takumi029d74b2011-02-17 08:50:50 +00001681
Fariborz Jahaniana4cfff82011-01-07 01:05:02 +00001682 if (Args.hasArg(options::OPT_mkernel) ||
1683 Args.hasArg(options::OPT_fapple_kext)) {
1684 if (!Args.hasArg(options::OPT_fcommon))
1685 CmdArgs.push_back("-fno-common");
1686 }
Daniel Dunbard18049a2009-04-07 21:16:11 +00001687 // -fcommon is default, only pass non-default.
Fariborz Jahaniana4cfff82011-01-07 01:05:02 +00001688 else if (!Args.hasFlag(options::OPT_fcommon, options::OPT_fno_common))
Daniel Dunbard18049a2009-04-07 21:16:11 +00001689 CmdArgs.push_back("-fno-common");
1690
Daniel Dunbar2edd9232009-04-15 02:37:43 +00001691 // -fsigned-bitfields is default, and clang doesn't yet support
Daniel Dunbar6358d682010-10-15 22:30:42 +00001692 // -funsigned-bitfields.
Mike Stump11289f42009-09-09 15:08:12 +00001693 if (!Args.hasFlag(options::OPT_fsigned_bitfields,
Daniel Dunbar2edd9232009-04-15 02:37:43 +00001694 options::OPT_funsigned_bitfields))
1695 D.Diag(clang::diag::warn_drv_clang_unsupported)
1696 << Args.getLastArg(options::OPT_funsigned_bitfields)->getAsString(Args);
1697
Daniel Dunbar6358d682010-10-15 22:30:42 +00001698 // -fsigned-bitfields is default, and clang doesn't support -fno-for-scope.
1699 if (!Args.hasFlag(options::OPT_ffor_scope,
1700 options::OPT_fno_for_scope))
1701 D.Diag(clang::diag::err_drv_clang_unsupported)
1702 << Args.getLastArg(options::OPT_fno_for_scope)->getAsString(Args);
1703
Jeffrey Yasskin460aa542010-06-08 04:56:20 +00001704 // -fcaret-diagnostics is default.
1705 if (!Args.hasFlag(options::OPT_fcaret_diagnostics,
1706 options::OPT_fno_caret_diagnostics, true))
1707 CmdArgs.push_back("-fno-caret-diagnostics");
1708
Daniel Dunbar8281bde2009-04-19 21:09:34 +00001709 // -fdiagnostics-fixit-info is default, only pass non-default.
Mike Stump11289f42009-09-09 15:08:12 +00001710 if (!Args.hasFlag(options::OPT_fdiagnostics_fixit_info,
Daniel Dunbar8281bde2009-04-19 21:09:34 +00001711 options::OPT_fno_diagnostics_fixit_info))
1712 CmdArgs.push_back("-fno-diagnostics-fixit-info");
1713
Daniel Dunbar092f0cc2009-04-16 06:32:38 +00001714 // Enable -fdiagnostics-show-option by default.
Mike Stump11289f42009-09-09 15:08:12 +00001715 if (Args.hasFlag(options::OPT_fdiagnostics_show_option,
Daniel Dunbar092f0cc2009-04-16 06:32:38 +00001716 options::OPT_fno_diagnostics_show_option))
1717 CmdArgs.push_back("-fdiagnostics-show-option");
Daniel Dunbar5ec95022009-11-04 06:24:57 +00001718
Chris Lattnerbf6fac82010-05-04 21:55:25 +00001719 if (const Arg *A =
1720 Args.getLastArg(options::OPT_fdiagnostics_show_category_EQ)) {
1721 CmdArgs.push_back("-fdiagnostics-show-category");
1722 CmdArgs.push_back(A->getValue(Args));
1723 }
Daniel Dunbarfe6c97b2010-08-24 16:47:49 +00001724
Daniel Dunbar5ec95022009-11-04 06:24:57 +00001725 // Color diagnostics are the default, unless the terminal doesn't support
1726 // them.
1727 if (Args.hasFlag(options::OPT_fcolor_diagnostics,
Argyrios Kyrtzidis4f920162010-09-23 12:56:06 +00001728 options::OPT_fno_color_diagnostics,
1729 llvm::sys::Process::StandardErrHasColors()))
Daniel Dunbar5ec95022009-11-04 06:24:57 +00001730 CmdArgs.push_back("-fcolor-diagnostics");
1731
Daniel Dunbardb097022009-06-08 21:13:54 +00001732 if (!Args.hasFlag(options::OPT_fshow_source_location,
1733 options::OPT_fno_show_source_location))
1734 CmdArgs.push_back("-fno-show-source-location");
Daniel Dunbar092f0cc2009-04-16 06:32:38 +00001735
Douglas Gregor8ed0c0b2010-07-09 17:35:33 +00001736 if (!Args.hasFlag(options::OPT_fspell_checking,
1737 options::OPT_fno_spell_checking))
1738 CmdArgs.push_back("-fno-spell-checking");
Daniel Dunbarfe6c97b2010-08-24 16:47:49 +00001739
Daniel Dunbar473f8a62010-10-18 22:49:46 +00001740
Daniel Dunbar3ada2b72010-11-02 19:42:04 +00001741 // Silently ignore -fasm-blocks for now.
1742 (void) Args.hasFlag(options::OPT_fasm_blocks, options::OPT_fno_asm_blocks,
1743 false);
Daniel Dunbar473f8a62010-10-18 22:49:46 +00001744
Jeffrey Yasskin2b99c6f2010-06-11 05:57:47 +00001745 if (Arg *A = Args.getLastArg(options::OPT_fshow_overloads_EQ))
1746 A->render(Args, CmdArgs);
1747
Daniel Dunbarf5e9b1f2009-04-19 21:20:32 +00001748 // -fdollars-in-identifiers default varies depending on platform and
1749 // language; only pass if specified.
Mike Stump11289f42009-09-09 15:08:12 +00001750 if (Arg *A = Args.getLastArg(options::OPT_fdollars_in_identifiers,
Daniel Dunbarf5e9b1f2009-04-19 21:20:32 +00001751 options::OPT_fno_dollars_in_identifiers)) {
1752 if (A->getOption().matches(options::OPT_fdollars_in_identifiers))
Daniel Dunbar15cef0e2009-12-16 20:10:18 +00001753 CmdArgs.push_back("-fdollars-in-identifiers");
Daniel Dunbarf5e9b1f2009-04-19 21:20:32 +00001754 else
Daniel Dunbar15cef0e2009-12-16 20:10:18 +00001755 CmdArgs.push_back("-fno-dollars-in-identifiers");
Daniel Dunbarf5e9b1f2009-04-19 21:20:32 +00001756 }
1757
Daniel Dunbaradeeb052009-05-22 19:02:20 +00001758 // -funit-at-a-time is default, and we don't support -fno-unit-at-a-time for
1759 // practical purposes.
Mike Stump11289f42009-09-09 15:08:12 +00001760 if (Arg *A = Args.getLastArg(options::OPT_funit_at_a_time,
Daniel Dunbaradeeb052009-05-22 19:02:20 +00001761 options::OPT_fno_unit_at_a_time)) {
1762 if (A->getOption().matches(options::OPT_fno_unit_at_a_time))
Edward O'Callaghanfc460552009-10-13 16:41:34 +00001763 D.Diag(clang::diag::warn_drv_clang_unsupported) << A->getAsString(Args);
Daniel Dunbaradeeb052009-05-22 19:02:20 +00001764 }
Eli Friedmanbb0d9a52009-07-14 21:58:17 +00001765
Daniel Dunbar2ffe0292009-09-10 03:37:02 +00001766 // Default to -fno-builtin-str{cat,cpy} on Darwin for ARM.
Daniel Dunbar4fa08112009-09-10 04:57:27 +00001767 //
Daniel Dunbar6c536aa2009-12-11 23:00:49 +00001768 // FIXME: This is disabled until clang -cc1 supports -fno-builtin-foo. PR4941.
Daniel Dunbar4fa08112009-09-10 04:57:27 +00001769#if 0
Daniel Dunbar2ffe0292009-09-10 03:37:02 +00001770 if (getToolChain().getTriple().getOS() == llvm::Triple::Darwin &&
1771 (getToolChain().getTriple().getArch() == llvm::Triple::arm ||
1772 getToolChain().getTriple().getArch() == llvm::Triple::thumb)) {
1773 if (!Args.hasArg(options::OPT_fbuiltin_strcat))
1774 CmdArgs.push_back("-fno-builtin-strcat");
1775 if (!Args.hasArg(options::OPT_fbuiltin_strcpy))
1776 CmdArgs.push_back("-fno-builtin-strcpy");
1777 }
Daniel Dunbar4fa08112009-09-10 04:57:27 +00001778#endif
Daniel Dunbar2ffe0292009-09-10 03:37:02 +00001779
Daniel Dunbar8c3d7352011-03-18 21:23:40 +00001780 // Only allow -traditional or -traditional-cpp outside in preprocessing modes.
Mike Stump11289f42009-09-09 15:08:12 +00001781 if (Arg *A = Args.getLastArg(options::OPT_traditional,
Daniel Dunbar8c3d7352011-03-18 21:23:40 +00001782 options::OPT_traditional_cpp)) {
1783 if (isa<PreprocessJobAction>(JA))
1784 CmdArgs.push_back("-traditional-cpp");
1785 else
1786 D.Diag(clang::diag::err_drv_clang_unsupported) << A->getAsString(Args);
1787 }
Eli Friedmanbb0d9a52009-07-14 21:58:17 +00001788
Daniel Dunbara2aedc62009-03-18 10:01:51 +00001789 Args.AddLastArg(CmdArgs, options::OPT_dM);
Chris Lattnercac63f32009-04-12 01:56:53 +00001790 Args.AddLastArg(CmdArgs, options::OPT_dD);
Daniel Dunbara2aedc62009-03-18 10:01:51 +00001791
Daniel Dunbar76fa8402010-04-15 06:09:03 +00001792 // Forward -Xclang arguments to -cc1, and -mllvm arguments to the LLVM option
1793 // parser.
Daniel Dunbara2aedc62009-03-18 10:01:51 +00001794 Args.AddAllArgValues(CmdArgs, options::OPT_Xclang);
Daniel Dunbar76fa8402010-04-15 06:09:03 +00001795 for (arg_iterator it = Args.filtered_begin(options::OPT_mllvm),
1796 ie = Args.filtered_end(); it != ie; ++it) {
Daniel Dunbara442fd52010-06-11 22:00:13 +00001797 (*it)->claim();
Daniel Dunbar88534f42010-04-17 06:10:00 +00001798
Daniel Dunbar76fa8402010-04-15 06:09:03 +00001799 // We translate this by hand to the -cc1 argument, since nightly test uses
1800 // it and developers have been trained to spell it with -mllvm.
Daniel Dunbara442fd52010-06-11 22:00:13 +00001801 if (llvm::StringRef((*it)->getValue(Args, 0)) == "-disable-llvm-optzns")
Daniel Dunbar76fa8402010-04-15 06:09:03 +00001802 CmdArgs.push_back("-disable-llvm-optzns");
1803 else
Daniel Dunbara442fd52010-06-11 22:00:13 +00001804 (*it)->render(Args, CmdArgs);
Daniel Dunbar76fa8402010-04-15 06:09:03 +00001805 }
Daniel Dunbara2aedc62009-03-18 10:01:51 +00001806
Daniel Dunbard67a3222009-03-30 06:36:42 +00001807 if (Output.getType() == types::TY_Dependencies) {
1808 // Handled with other dependency code.
Daniel Dunbar5cdf3e02009-03-19 07:29:38 +00001809 } else if (Output.isFilename()) {
Daniel Dunbara3246a02009-03-18 08:07:30 +00001810 CmdArgs.push_back("-o");
Daniel Dunbar5cdf3e02009-03-19 07:29:38 +00001811 CmdArgs.push_back(Output.getFilename());
1812 } else {
1813 assert(Output.isNothing() && "Invalid output.");
Daniel Dunbara3246a02009-03-18 08:07:30 +00001814 }
1815
Daniel Dunbara2aedc62009-03-18 10:01:51 +00001816 for (InputInfoList::const_iterator
1817 it = Inputs.begin(), ie = Inputs.end(); it != ie; ++it) {
1818 const InputInfo &II = *it;
1819 CmdArgs.push_back("-x");
1820 CmdArgs.push_back(types::getTypeName(II.getType()));
Daniel Dunbarb440f562010-08-02 02:38:21 +00001821 if (II.isFilename())
Daniel Dunbar5cdf3e02009-03-19 07:29:38 +00001822 CmdArgs.push_back(II.getFilename());
Daniel Dunbara2aedc62009-03-18 10:01:51 +00001823 else
Daniel Dunbar5cdf3e02009-03-19 07:29:38 +00001824 II.getInputArg().renderAsInput(Args, CmdArgs);
Daniel Dunbara2aedc62009-03-18 10:01:51 +00001825 }
Daniel Dunbard067f7f2009-04-08 23:54:23 +00001826
Chris Lattnere9d7d782009-11-03 19:50:27 +00001827 Args.AddAllArgs(CmdArgs, options::OPT_undef);
1828
Daniel Dunbarb31b76f2010-07-18 21:16:15 +00001829 const char *Exec = getToolChain().getDriver().getClangProgramPath();
Daniel Dunbar24c7f5e2009-12-18 02:43:17 +00001830
1831 // Optionally embed the -cc1 level arguments into the debug info, for build
1832 // analysis.
1833 if (getToolChain().UseDwarfDebugFlags()) {
Daniel Dunbar7f3d9502010-06-04 18:47:06 +00001834 ArgStringList OriginalArgs;
1835 for (ArgList::const_iterator it = Args.begin(),
1836 ie = Args.end(); it != ie; ++it)
1837 (*it)->render(Args, OriginalArgs);
Daniel Dunbarfe6c97b2010-08-24 16:47:49 +00001838
Daniel Dunbar24c7f5e2009-12-18 02:43:17 +00001839 llvm::SmallString<256> Flags;
1840 Flags += Exec;
Daniel Dunbar7f3d9502010-06-04 18:47:06 +00001841 for (unsigned i = 0, e = OriginalArgs.size(); i != e; ++i) {
Daniel Dunbar24c7f5e2009-12-18 02:43:17 +00001842 Flags += " ";
Daniel Dunbar7f3d9502010-06-04 18:47:06 +00001843 Flags += OriginalArgs[i];
Daniel Dunbar24c7f5e2009-12-18 02:43:17 +00001844 }
1845 CmdArgs.push_back("-dwarf-debug-flags");
1846 CmdArgs.push_back(Args.MakeArgString(Flags.str()));
1847 }
1848
Daniel Dunbar7fbaf532010-08-02 02:38:28 +00001849 C.addCommand(new Command(JA, *this, Exec, CmdArgs));
Daniel Dunbar17731772009-03-23 19:03:36 +00001850
Roman Divacky178e01602011-02-10 16:52:03 +00001851 if (Arg *A = Args.getLastArg(options::OPT_pg))
1852 if (Args.hasArg(options::OPT_fomit_frame_pointer))
1853 D.Diag(clang::diag::err_drv_argument_not_allowed_with)
1854 << "-fomit-frame-pointer" << A->getAsString(Args);
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001855
Daniel Dunbarc2a71892009-04-03 20:51:31 +00001856 // Claim some arguments which clang supports automatically.
1857
Daniel Dunbar3e0cac62010-04-15 06:18:42 +00001858 // -fpch-preprocess is used with gcc to add a special marker in the output to
1859 // include the PCH file. Clang's PTH solution is completely transparent, so we
1860 // do not need to deal with it at all.
Daniel Dunbarc2a71892009-04-03 20:51:31 +00001861 Args.ClaimAllArgs(options::OPT_fpch_preprocess);
Daniel Dunbard067f7f2009-04-08 23:54:23 +00001862
Daniel Dunbar17731772009-03-23 19:03:36 +00001863 // Claim some arguments which clang doesn't support, but we don't
1864 // care to warn the user about.
Daniel Dunbar44b36ee2009-11-25 11:53:23 +00001865 Args.ClaimAllArgs(options::OPT_clang_ignored_f_Group);
1866 Args.ClaimAllArgs(options::OPT_clang_ignored_m_Group);
Rafael Espindola22f603032011-02-28 23:29:45 +00001867
Rafael Espindolad95a8122011-03-01 05:25:27 +00001868 // Disable warnings for clang -E -use-gold-plugin -emit-llvm foo.c
Rafael Espindola22f603032011-02-28 23:29:45 +00001869 Args.ClaimAllArgs(options::OPT_use_gold_plugin);
Rafael Espindolad95a8122011-03-01 05:25:27 +00001870 Args.ClaimAllArgs(options::OPT_emit_llvm);
Daniel Dunbar1a093d22009-03-18 06:00:36 +00001871}
1872
Daniel Dunbar4f5e79c2010-05-20 21:30:13 +00001873void ClangAs::ConstructJob(Compilation &C, const JobAction &JA,
Daniel Dunbar4f5e79c2010-05-20 21:30:13 +00001874 const InputInfo &Output,
1875 const InputInfoList &Inputs,
1876 const ArgList &Args,
1877 const char *LinkingOutput) const {
Daniel Dunbar4f5e79c2010-05-20 21:30:13 +00001878 ArgStringList CmdArgs;
1879
1880 assert(Inputs.size() == 1 && "Unexpected number of inputs.");
1881 const InputInfo &Input = Inputs[0];
1882
Rafael Espindolacfaadda2010-11-17 22:13:25 +00001883 // Don't warn about "clang -w -c foo.s"
1884 Args.ClaimAllArgs(options::OPT_w);
Rafael Espindolad95a8122011-03-01 05:25:27 +00001885 // and "clang -emit-llvm -c foo.s"
1886 Args.ClaimAllArgs(options::OPT_emit_llvm);
1887 // and "clang -use-gold-plugin -c foo.s"
1888 Args.ClaimAllArgs(options::OPT_use_gold_plugin);
Rafael Espindolacfaadda2010-11-17 22:13:25 +00001889
Daniel Dunbar4f5e79c2010-05-20 21:30:13 +00001890 // Invoke ourselves in -cc1as mode.
1891 //
1892 // FIXME: Implement custom jobs for internal actions.
1893 CmdArgs.push_back("-cc1as");
1894
1895 // Add the "effective" target triple.
1896 CmdArgs.push_back("-triple");
Daniel Dunbar82eb4ce2010-08-23 22:35:37 +00001897 std::string TripleStr = getToolChain().ComputeEffectiveClangTriple(Args);
Daniel Dunbar4f5e79c2010-05-20 21:30:13 +00001898 CmdArgs.push_back(Args.MakeArgString(TripleStr));
1899
1900 // Set the output mode, we currently only expect to be used as a real
1901 // assembler.
1902 CmdArgs.push_back("-filetype");
1903 CmdArgs.push_back("obj");
1904
Daniel Dunbar06e2cc32010-05-27 06:18:05 +00001905 // At -O0, we use -mrelax-all by default.
1906 bool IsOpt = false;
1907 if (Arg *A = Args.getLastArg(options::OPT_O_Group))
1908 IsOpt = !A->getOption().matches(options::OPT_O0);
1909 if (Args.hasFlag(options::OPT_mrelax_all,
1910 options::OPT_mno_relax_all,
1911 !IsOpt))
Daniel Dunbar99ca8b72010-05-28 16:43:21 +00001912 CmdArgs.push_back("-relax-all");
Daniel Dunbar06e2cc32010-05-27 06:18:05 +00001913
Daniel Dunbar1d733e22011-03-17 17:37:29 +00001914 // Ignore explicit -force_cpusubtype_ALL option.
1915 (void) Args.hasArg(options::OPT_force__cpusubtype__ALL);
Daniel Dunbar4f5e79c2010-05-20 21:30:13 +00001916
1917 // FIXME: Add -g support, once we have it.
1918
1919 // FIXME: Add -static support, once we have it.
1920
1921 Args.AddAllArgValues(CmdArgs, options::OPT_Wa_COMMA,
1922 options::OPT_Xassembler);
1923
1924 assert(Output.isFilename() && "Unexpected lipo output.");
1925 CmdArgs.push_back("-o");
1926 CmdArgs.push_back(Output.getFilename());
1927
Daniel Dunbarb440f562010-08-02 02:38:21 +00001928 assert(Input.isFilename() && "Invalid input.");
1929 CmdArgs.push_back(Input.getFilename());
Daniel Dunbar4f5e79c2010-05-20 21:30:13 +00001930
Daniel Dunbarb31b76f2010-07-18 21:16:15 +00001931 const char *Exec = getToolChain().getDriver().getClangProgramPath();
Daniel Dunbar7fbaf532010-08-02 02:38:28 +00001932 C.addCommand(new Command(JA, *this, Exec, CmdArgs));
Daniel Dunbar4f5e79c2010-05-20 21:30:13 +00001933}
1934
Daniel Dunbara3246a02009-03-18 08:07:30 +00001935void gcc::Common::ConstructJob(Compilation &C, const JobAction &JA,
Daniel Dunbara3246a02009-03-18 08:07:30 +00001936 const InputInfo &Output,
1937 const InputInfoList &Inputs,
Daniel Dunbara2aedc62009-03-18 10:01:51 +00001938 const ArgList &Args,
Daniel Dunbara3246a02009-03-18 08:07:30 +00001939 const char *LinkingOutput) const {
Daniel Dunbar083edf72009-12-21 18:54:17 +00001940 const Driver &D = getToolChain().getDriver();
Daniel Dunbara3246a02009-03-18 08:07:30 +00001941 ArgStringList CmdArgs;
Daniel Dunbar1a093d22009-03-18 06:00:36 +00001942
Daniel Dunbard067f7f2009-04-08 23:54:23 +00001943 for (ArgList::const_iterator
Daniel Dunbara2aedc62009-03-18 10:01:51 +00001944 it = Args.begin(), ie = Args.end(); it != ie; ++it) {
Daniel Dunbara3246a02009-03-18 08:07:30 +00001945 Arg *A = *it;
Daniel Dunbar2da02722009-03-19 07:55:12 +00001946 if (A->getOption().hasForwardToGCC()) {
Daniel Dunbar939c1212010-08-03 16:14:14 +00001947 // Don't forward any -g arguments to assembly steps.
1948 if (isa<AssembleJobAction>(JA) &&
1949 A->getOption().matches(options::OPT_g_Group))
1950 continue;
1951
Daniel Dunbar2da02722009-03-19 07:55:12 +00001952 // It is unfortunate that we have to claim here, as this means
1953 // we will basically never report anything interesting for
Daniel Dunbar5716d872009-05-02 21:41:52 +00001954 // platforms using a generic gcc, even if we are just using gcc
1955 // to get to the assembler.
Daniel Dunbar2da02722009-03-19 07:55:12 +00001956 A->claim();
Daniel Dunbara2aedc62009-03-18 10:01:51 +00001957 A->render(Args, CmdArgs);
Daniel Dunbar2da02722009-03-19 07:55:12 +00001958 }
Daniel Dunbara3246a02009-03-18 08:07:30 +00001959 }
Daniel Dunbard067f7f2009-04-08 23:54:23 +00001960
Daniel Dunbar4e295052010-01-25 22:35:08 +00001961 RenderExtraToolArgs(JA, CmdArgs);
Daniel Dunbara3246a02009-03-18 08:07:30 +00001962
1963 // If using a driver driver, force the arch.
Daniel Dunbar5bbebfe2009-05-22 02:21:04 +00001964 const std::string &Arch = getToolChain().getArchName();
Daniel Dunbarf4894fe2009-12-23 00:46:38 +00001965 if (getToolChain().getTriple().getOS() == llvm::Triple::Darwin) {
Daniel Dunbara3246a02009-03-18 08:07:30 +00001966 CmdArgs.push_back("-arch");
Daniel Dunbar0a05d932009-04-01 20:33:11 +00001967
1968 // FIXME: Remove these special cases.
Daniel Dunbar5bbebfe2009-05-22 02:21:04 +00001969 if (Arch == "powerpc")
1970 CmdArgs.push_back("ppc");
1971 else if (Arch == "powerpc64")
1972 CmdArgs.push_back("ppc64");
1973 else
Daniel Dunbar1c28f1e2009-09-09 22:32:48 +00001974 CmdArgs.push_back(Args.MakeArgString(Arch));
Daniel Dunbara3246a02009-03-18 08:07:30 +00001975 }
1976
Daniel Dunbar5716d872009-05-02 21:41:52 +00001977 // Try to force gcc to match the tool chain we want, if we recognize
1978 // the arch.
Daniel Dunbar5bbebfe2009-05-22 02:21:04 +00001979 //
1980 // FIXME: The triple class should directly provide the information we want
1981 // here.
1982 if (Arch == "i386" || Arch == "powerpc")
Daniel Dunbar5716d872009-05-02 21:41:52 +00001983 CmdArgs.push_back("-m32");
Daniel Dunbar5bbebfe2009-05-22 02:21:04 +00001984 else if (Arch == "x86_64" || Arch == "powerpc64")
Daniel Dunbar5716d872009-05-02 21:41:52 +00001985 CmdArgs.push_back("-m64");
1986
Daniel Dunbarb440f562010-08-02 02:38:21 +00001987 if (Output.isFilename()) {
Daniel Dunbara3246a02009-03-18 08:07:30 +00001988 CmdArgs.push_back("-o");
Daniel Dunbar5cdf3e02009-03-19 07:29:38 +00001989 CmdArgs.push_back(Output.getFilename());
1990 } else {
1991 assert(Output.isNothing() && "Unexpected output");
Daniel Dunbara3246a02009-03-18 08:07:30 +00001992 CmdArgs.push_back("-fsyntax-only");
Daniel Dunbar5cdf3e02009-03-19 07:29:38 +00001993 }
Daniel Dunbara3246a02009-03-18 08:07:30 +00001994
1995
1996 // Only pass -x if gcc will understand it; otherwise hope gcc
1997 // understands the suffix correctly. The main use case this would go
1998 // wrong in is for linker inputs if they happened to have an odd
1999 // suffix; really the only way to get this to happen is a command
2000 // like '-x foobar a.c' which will treat a.c like a linker input.
2001 //
2002 // FIXME: For the linker case specifically, can we safely convert
2003 // inputs into '-Wl,' options?
2004 for (InputInfoList::const_iterator
2005 it = Inputs.begin(), ie = Inputs.end(); it != ie; ++it) {
2006 const InputInfo &II = *it;
Daniel Dunbare3e263f2009-05-02 20:14:53 +00002007
Daniel Dunbar6cdf83c2009-09-01 16:57:46 +00002008 // Don't try to pass LLVM or AST inputs to a generic gcc.
Daniel Dunbar24e52992010-06-07 23:28:45 +00002009 if (II.getType() == types::TY_LLVM_IR || II.getType() == types::TY_LTO_IR ||
2010 II.getType() == types::TY_LLVM_BC || II.getType() == types::TY_LTO_BC)
Daniel Dunbare3e263f2009-05-02 20:14:53 +00002011 D.Diag(clang::diag::err_drv_no_linker_llvm_support)
Daniel Dunbar1c28f1e2009-09-09 22:32:48 +00002012 << getToolChain().getTripleString();
Daniel Dunbar6cdf83c2009-09-01 16:57:46 +00002013 else if (II.getType() == types::TY_AST)
2014 D.Diag(clang::diag::err_drv_no_ast_support)
Daniel Dunbar1c28f1e2009-09-09 22:32:48 +00002015 << getToolChain().getTripleString();
Daniel Dunbare3e263f2009-05-02 20:14:53 +00002016
Daniel Dunbara3246a02009-03-18 08:07:30 +00002017 if (types::canTypeBeUserSpecified(II.getType())) {
2018 CmdArgs.push_back("-x");
2019 CmdArgs.push_back(types::getTypeName(II.getType()));
2020 }
2021
Daniel Dunbarb440f562010-08-02 02:38:21 +00002022 if (II.isFilename())
Daniel Dunbar5cdf3e02009-03-19 07:29:38 +00002023 CmdArgs.push_back(II.getFilename());
Daniel Dunbarf2476752010-09-25 18:10:05 +00002024 else {
2025 const Arg &A = II.getInputArg();
2026
2027 // Reverse translate some rewritten options.
2028 if (A.getOption().matches(options::OPT_Z_reserved_lib_stdcxx)) {
2029 CmdArgs.push_back("-lstdc++");
2030 continue;
2031 }
2032
Daniel Dunbar5cdf3e02009-03-19 07:29:38 +00002033 // Don't render as input, we need gcc to do the translations.
Daniel Dunbarf2476752010-09-25 18:10:05 +00002034 A.render(Args, CmdArgs);
2035 }
Daniel Dunbara3246a02009-03-18 08:07:30 +00002036 }
2037
Rafael Espindolab2d0d402010-09-06 02:36:23 +00002038 const char *GCCName = getToolChain().getDriver().getCCCGenericGCCName().c_str();
Daniel Dunbard067f7f2009-04-08 23:54:23 +00002039 const char *Exec =
Daniel Dunbar9c3ed5f2010-07-14 18:46:23 +00002040 Args.MakeArgString(getToolChain().GetProgramPath(GCCName));
Daniel Dunbar7fbaf532010-08-02 02:38:28 +00002041 C.addCommand(new Command(JA, *this, Exec, CmdArgs));
Daniel Dunbar1a093d22009-03-18 06:00:36 +00002042}
2043
Daniel Dunbar4e295052010-01-25 22:35:08 +00002044void gcc::Preprocess::RenderExtraToolArgs(const JobAction &JA,
2045 ArgStringList &CmdArgs) const {
Daniel Dunbara3246a02009-03-18 08:07:30 +00002046 CmdArgs.push_back("-E");
Daniel Dunbar1a093d22009-03-18 06:00:36 +00002047}
2048
Daniel Dunbar4e295052010-01-25 22:35:08 +00002049void gcc::Precompile::RenderExtraToolArgs(const JobAction &JA,
2050 ArgStringList &CmdArgs) const {
Daniel Dunbara3246a02009-03-18 08:07:30 +00002051 // The type is good enough.
Daniel Dunbar1a093d22009-03-18 06:00:36 +00002052}
2053
Daniel Dunbar4e295052010-01-25 22:35:08 +00002054void gcc::Compile::RenderExtraToolArgs(const JobAction &JA,
2055 ArgStringList &CmdArgs) const {
Daniel Dunbaraeea8ac2010-02-11 03:16:21 +00002056 const Driver &D = getToolChain().getDriver();
2057
Daniel Dunbar4e295052010-01-25 22:35:08 +00002058 // If -flto, etc. are present then make sure not to force assembly output.
Daniel Dunbar24e52992010-06-07 23:28:45 +00002059 if (JA.getType() == types::TY_LLVM_IR || JA.getType() == types::TY_LTO_IR ||
2060 JA.getType() == types::TY_LLVM_BC || JA.getType() == types::TY_LTO_BC)
Daniel Dunbar4e295052010-01-25 22:35:08 +00002061 CmdArgs.push_back("-c");
Daniel Dunbaraeea8ac2010-02-11 03:16:21 +00002062 else {
2063 if (JA.getType() != types::TY_PP_Asm)
2064 D.Diag(clang::diag::err_drv_invalid_gcc_output_type)
2065 << getTypeName(JA.getType());
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00002066
Daniel Dunbar4e295052010-01-25 22:35:08 +00002067 CmdArgs.push_back("-S");
Daniel Dunbaraeea8ac2010-02-11 03:16:21 +00002068 }
Daniel Dunbar1a093d22009-03-18 06:00:36 +00002069}
2070
Daniel Dunbar4e295052010-01-25 22:35:08 +00002071void gcc::Assemble::RenderExtraToolArgs(const JobAction &JA,
2072 ArgStringList &CmdArgs) const {
Daniel Dunbara3246a02009-03-18 08:07:30 +00002073 CmdArgs.push_back("-c");
Daniel Dunbar1a093d22009-03-18 06:00:36 +00002074}
Daniel Dunbara3246a02009-03-18 08:07:30 +00002075
Daniel Dunbar4e295052010-01-25 22:35:08 +00002076void gcc::Link::RenderExtraToolArgs(const JobAction &JA,
2077 ArgStringList &CmdArgs) const {
Daniel Dunbara3246a02009-03-18 08:07:30 +00002078 // The types are (hopefully) good enough.
2079}
2080
Daniel Dunbare6adeee2009-03-29 17:08:39 +00002081const char *darwin::CC1::getCC1Name(types::ID Type) const {
2082 switch (Type) {
2083 default:
2084 assert(0 && "Unexpected type for Darwin CC1 tool.");
2085 case types::TY_Asm:
2086 case types::TY_C: case types::TY_CHeader:
2087 case types::TY_PP_C: case types::TY_PP_CHeader:
2088 return "cc1";
2089 case types::TY_ObjC: case types::TY_ObjCHeader:
2090 case types::TY_PP_ObjC: case types::TY_PP_ObjCHeader:
2091 return "cc1obj";
2092 case types::TY_CXX: case types::TY_CXXHeader:
2093 case types::TY_PP_CXX: case types::TY_PP_CXXHeader:
2094 return "cc1plus";
2095 case types::TY_ObjCXX: case types::TY_ObjCXXHeader:
2096 case types::TY_PP_ObjCXX: case types::TY_PP_ObjCXXHeader:
2097 return "cc1objplus";
2098 }
2099}
2100
Daniel Dunbard067f7f2009-04-08 23:54:23 +00002101const char *darwin::CC1::getBaseInputName(const ArgList &Args,
Daniel Dunbar52e96cc2009-03-30 00:34:04 +00002102 const InputInfoList &Inputs) {
Michael J. Spencere1696752010-12-18 00:19:12 +00002103 return Args.MakeArgString(
2104 llvm::sys::path::filename(Inputs[0].getBaseInput()));
Daniel Dunbarafec1f52009-03-29 18:40:18 +00002105}
2106
Daniel Dunbard067f7f2009-04-08 23:54:23 +00002107const char *darwin::CC1::getBaseInputStem(const ArgList &Args,
Daniel Dunbar52e96cc2009-03-30 00:34:04 +00002108 const InputInfoList &Inputs) {
Daniel Dunbarafec1f52009-03-29 18:40:18 +00002109 const char *Str = getBaseInputName(Args, Inputs);
2110
Chris Lattner906bb902011-01-16 08:14:11 +00002111 if (const char *End = strrchr(Str, '.'))
Daniel Dunbar1c28f1e2009-09-09 22:32:48 +00002112 return Args.MakeArgString(std::string(Str, End));
Daniel Dunbarafec1f52009-03-29 18:40:18 +00002113
2114 return Str;
2115}
2116
2117const char *
Daniel Dunbard067f7f2009-04-08 23:54:23 +00002118darwin::CC1::getDependencyFileName(const ArgList &Args,
Daniel Dunbar52e96cc2009-03-30 00:34:04 +00002119 const InputInfoList &Inputs) {
Daniel Dunbarafec1f52009-03-29 18:40:18 +00002120 // FIXME: Think about this more.
2121 std::string Res;
2122
2123 if (Arg *OutputOpt = Args.getLastArg(options::OPT_o)) {
2124 std::string Str(OutputOpt->getValue(Args));
Daniel Dunbard067f7f2009-04-08 23:54:23 +00002125
Daniel Dunbarafec1f52009-03-29 18:40:18 +00002126 Res = Str.substr(0, Str.rfind('.'));
2127 } else
Daniel Dunbar52e96cc2009-03-30 00:34:04 +00002128 Res = darwin::CC1::getBaseInputStem(Args, Inputs);
Daniel Dunbarafec1f52009-03-29 18:40:18 +00002129
Daniel Dunbar1c28f1e2009-09-09 22:32:48 +00002130 return Args.MakeArgString(Res + ".d");
Daniel Dunbarafec1f52009-03-29 18:40:18 +00002131}
2132
Daniel Dunbard067f7f2009-04-08 23:54:23 +00002133void darwin::CC1::AddCC1Args(const ArgList &Args,
Daniel Dunbarafec1f52009-03-29 18:40:18 +00002134 ArgStringList &CmdArgs) const {
Daniel Dunbar083edf72009-12-21 18:54:17 +00002135 const Driver &D = getToolChain().getDriver();
Daniel Dunbar4eadb602009-09-10 01:21:12 +00002136
2137 CheckCodeGenerationOptions(D, Args);
2138
Daniel Dunbarafec1f52009-03-29 18:40:18 +00002139 // Derived from cc1 spec.
Daniel Dunbarafec1f52009-03-29 18:40:18 +00002140 if (!Args.hasArg(options::OPT_mkernel) && !Args.hasArg(options::OPT_static) &&
2141 !Args.hasArg(options::OPT_mdynamic_no_pic))
2142 CmdArgs.push_back("-fPIC");
2143
Daniel Dunbar2ffe0292009-09-10 03:37:02 +00002144 if (getToolChain().getTriple().getArch() == llvm::Triple::arm ||
2145 getToolChain().getTriple().getArch() == llvm::Triple::thumb) {
2146 if (!Args.hasArg(options::OPT_fbuiltin_strcat))
2147 CmdArgs.push_back("-fno-builtin-strcat");
2148 if (!Args.hasArg(options::OPT_fbuiltin_strcpy))
2149 CmdArgs.push_back("-fno-builtin-strcpy");
2150 }
2151
Daniel Dunbarafec1f52009-03-29 18:40:18 +00002152 // gcc has some code here to deal with when no -mmacosx-version-min
2153 // and no -miphoneos-version-min is present, but this never happens
2154 // due to tool chain specific argument translation.
2155
Daniel Dunbarafec1f52009-03-29 18:40:18 +00002156 if (Args.hasArg(options::OPT_g_Flag) &&
2157 !Args.hasArg(options::OPT_fno_eliminate_unused_debug_symbols))
2158 CmdArgs.push_back("-feliminate-unused-debug-symbols");
2159}
2160
2161void darwin::CC1::AddCC1OptionsArgs(const ArgList &Args, ArgStringList &CmdArgs,
2162 const InputInfoList &Inputs,
2163 const ArgStringList &OutputArgs) const {
Daniel Dunbar083edf72009-12-21 18:54:17 +00002164 const Driver &D = getToolChain().getDriver();
Daniel Dunbarafec1f52009-03-29 18:40:18 +00002165
2166 // Derived from cc1_options spec.
2167 if (Args.hasArg(options::OPT_fast) ||
2168 Args.hasArg(options::OPT_fastf) ||
2169 Args.hasArg(options::OPT_fastcp))
2170 CmdArgs.push_back("-O3");
Daniel Dunbard067f7f2009-04-08 23:54:23 +00002171
Daniel Dunbarafec1f52009-03-29 18:40:18 +00002172 if (Arg *A = Args.getLastArg(options::OPT_pg))
2173 if (Args.hasArg(options::OPT_fomit_frame_pointer))
2174 D.Diag(clang::diag::err_drv_argument_not_allowed_with)
2175 << A->getAsString(Args) << "-fomit-frame-pointer";
2176
2177 AddCC1Args(Args, CmdArgs);
2178
2179 if (!Args.hasArg(options::OPT_Q))
2180 CmdArgs.push_back("-quiet");
2181
2182 CmdArgs.push_back("-dumpbase");
Daniel Dunbar52e96cc2009-03-30 00:34:04 +00002183 CmdArgs.push_back(darwin::CC1::getBaseInputName(Args, Inputs));
Daniel Dunbarafec1f52009-03-29 18:40:18 +00002184
2185 Args.AddAllArgs(CmdArgs, options::OPT_d_Group);
2186
2187 Args.AddAllArgs(CmdArgs, options::OPT_m_Group);
2188 Args.AddAllArgs(CmdArgs, options::OPT_a_Group);
2189
2190 // FIXME: The goal is to use the user provided -o if that is our
2191 // final output, otherwise to drive from the original input
2192 // name. Find a clean way to go about this.
2193 if ((Args.hasArg(options::OPT_c) || Args.hasArg(options::OPT_S)) &&
2194 Args.hasArg(options::OPT_o)) {
2195 Arg *OutputOpt = Args.getLastArg(options::OPT_o);
2196 CmdArgs.push_back("-auxbase-strip");
2197 CmdArgs.push_back(OutputOpt->getValue(Args));
2198 } else {
2199 CmdArgs.push_back("-auxbase");
Daniel Dunbar52e96cc2009-03-30 00:34:04 +00002200 CmdArgs.push_back(darwin::CC1::getBaseInputStem(Args, Inputs));
Daniel Dunbarafec1f52009-03-29 18:40:18 +00002201 }
2202
2203 Args.AddAllArgs(CmdArgs, options::OPT_g_Group);
2204
2205 Args.AddAllArgs(CmdArgs, options::OPT_O);
2206 // FIXME: -Wall is getting some special treatment. Investigate.
2207 Args.AddAllArgs(CmdArgs, options::OPT_W_Group, options::OPT_pedantic_Group);
2208 Args.AddLastArg(CmdArgs, options::OPT_w);
Daniel Dunbard067f7f2009-04-08 23:54:23 +00002209 Args.AddAllArgs(CmdArgs, options::OPT_std_EQ, options::OPT_ansi,
Daniel Dunbarafec1f52009-03-29 18:40:18 +00002210 options::OPT_trigraphs);
Daniel Dunbar72a60902009-04-26 01:10:38 +00002211 if (!Args.getLastArg(options::OPT_std_EQ, options::OPT_ansi)) {
2212 // Honor -std-default.
2213 Args.AddAllArgsTranslated(CmdArgs, options::OPT_std_default_EQ,
2214 "-std=", /*Joined=*/true);
2215 }
2216
Daniel Dunbarafec1f52009-03-29 18:40:18 +00002217 if (Args.hasArg(options::OPT_v))
2218 CmdArgs.push_back("-version");
Daniel Dunbar733b0f82011-03-01 18:49:30 +00002219 if (Args.hasArg(options::OPT_pg) &&
2220 getToolChain().SupportsProfiling())
Daniel Dunbarafec1f52009-03-29 18:40:18 +00002221 CmdArgs.push_back("-p");
2222 Args.AddLastArg(CmdArgs, options::OPT_p);
Daniel Dunbard067f7f2009-04-08 23:54:23 +00002223
Daniel Dunbarafec1f52009-03-29 18:40:18 +00002224 // The driver treats -fsyntax-only specially.
Daniel Dunbar2ffe0292009-09-10 03:37:02 +00002225 if (getToolChain().getTriple().getArch() == llvm::Triple::arm ||
2226 getToolChain().getTriple().getArch() == llvm::Triple::thumb) {
2227 // Removes -fbuiltin-str{cat,cpy}; these aren't recognized by cc1 but are
2228 // used to inhibit the default -fno-builtin-str{cat,cpy}.
2229 //
2230 // FIXME: Should we grow a better way to deal with "removing" args?
Daniel Dunbar44b36ee2009-11-25 11:53:23 +00002231 for (arg_iterator it = Args.filtered_begin(options::OPT_f_Group,
2232 options::OPT_fsyntax_only),
2233 ie = Args.filtered_end(); it != ie; ++it) {
Daniel Dunbara442fd52010-06-11 22:00:13 +00002234 if (!(*it)->getOption().matches(options::OPT_fbuiltin_strcat) &&
2235 !(*it)->getOption().matches(options::OPT_fbuiltin_strcpy)) {
2236 (*it)->claim();
2237 (*it)->render(Args, CmdArgs);
Daniel Dunbar2ffe0292009-09-10 03:37:02 +00002238 }
2239 }
2240 } else
2241 Args.AddAllArgs(CmdArgs, options::OPT_f_Group, options::OPT_fsyntax_only);
Daniel Dunbard067f7f2009-04-08 23:54:23 +00002242
Daniel Dunbarafec1f52009-03-29 18:40:18 +00002243 Args.AddAllArgs(CmdArgs, options::OPT_undef);
2244 if (Args.hasArg(options::OPT_Qn))
2245 CmdArgs.push_back("-fno-ident");
Daniel Dunbard067f7f2009-04-08 23:54:23 +00002246
Daniel Dunbarafec1f52009-03-29 18:40:18 +00002247 // FIXME: This isn't correct.
2248 //Args.AddLastArg(CmdArgs, options::OPT__help)
2249 //Args.AddLastArg(CmdArgs, options::OPT__targetHelp)
2250
2251 CmdArgs.append(OutputArgs.begin(), OutputArgs.end());
2252
2253 // FIXME: Still don't get what is happening here. Investigate.
2254 Args.AddAllArgs(CmdArgs, options::OPT__param);
2255
2256 if (Args.hasArg(options::OPT_fmudflap) ||
2257 Args.hasArg(options::OPT_fmudflapth)) {
2258 CmdArgs.push_back("-fno-builtin");
2259 CmdArgs.push_back("-fno-merge-constants");
2260 }
Daniel Dunbard067f7f2009-04-08 23:54:23 +00002261
Daniel Dunbarafec1f52009-03-29 18:40:18 +00002262 if (Args.hasArg(options::OPT_coverage)) {
2263 CmdArgs.push_back("-fprofile-arcs");
2264 CmdArgs.push_back("-ftest-coverage");
2265 }
2266
2267 if (types::isCXX(Inputs[0].getType()))
2268 CmdArgs.push_back("-D__private_extern__=extern");
2269}
2270
2271void darwin::CC1::AddCPPOptionsArgs(const ArgList &Args, ArgStringList &CmdArgs,
2272 const InputInfoList &Inputs,
2273 const ArgStringList &OutputArgs) const {
2274 // Derived from cpp_options
2275 AddCPPUniqueOptionsArgs(Args, CmdArgs, Inputs);
Daniel Dunbard067f7f2009-04-08 23:54:23 +00002276
Daniel Dunbarafec1f52009-03-29 18:40:18 +00002277 CmdArgs.append(OutputArgs.begin(), OutputArgs.end());
2278
2279 AddCC1Args(Args, CmdArgs);
2280
2281 // NOTE: The code below has some commonality with cpp_options, but
2282 // in classic gcc style ends up sending things in different
2283 // orders. This may be a good merge candidate once we drop pedantic
2284 // compatibility.
2285
2286 Args.AddAllArgs(CmdArgs, options::OPT_m_Group);
Daniel Dunbard067f7f2009-04-08 23:54:23 +00002287 Args.AddAllArgs(CmdArgs, options::OPT_std_EQ, options::OPT_ansi,
Daniel Dunbarafec1f52009-03-29 18:40:18 +00002288 options::OPT_trigraphs);
Daniel Dunbar72a60902009-04-26 01:10:38 +00002289 if (!Args.getLastArg(options::OPT_std_EQ, options::OPT_ansi)) {
2290 // Honor -std-default.
2291 Args.AddAllArgsTranslated(CmdArgs, options::OPT_std_default_EQ,
2292 "-std=", /*Joined=*/true);
2293 }
Daniel Dunbarafec1f52009-03-29 18:40:18 +00002294 Args.AddAllArgs(CmdArgs, options::OPT_W_Group, options::OPT_pedantic_Group);
2295 Args.AddLastArg(CmdArgs, options::OPT_w);
Daniel Dunbard067f7f2009-04-08 23:54:23 +00002296
Daniel Dunbarafec1f52009-03-29 18:40:18 +00002297 // The driver treats -fsyntax-only specially.
2298 Args.AddAllArgs(CmdArgs, options::OPT_f_Group, options::OPT_fsyntax_only);
2299
2300 if (Args.hasArg(options::OPT_g_Group) && !Args.hasArg(options::OPT_g0) &&
2301 !Args.hasArg(options::OPT_fno_working_directory))
2302 CmdArgs.push_back("-fworking-directory");
2303
2304 Args.AddAllArgs(CmdArgs, options::OPT_O);
2305 Args.AddAllArgs(CmdArgs, options::OPT_undef);
2306 if (Args.hasArg(options::OPT_save_temps))
2307 CmdArgs.push_back("-fpch-preprocess");
2308}
2309
Daniel Dunbard067f7f2009-04-08 23:54:23 +00002310void darwin::CC1::AddCPPUniqueOptionsArgs(const ArgList &Args,
Daniel Dunbarafec1f52009-03-29 18:40:18 +00002311 ArgStringList &CmdArgs,
Mike Stump11289f42009-09-09 15:08:12 +00002312 const InputInfoList &Inputs) const {
Daniel Dunbar083edf72009-12-21 18:54:17 +00002313 const Driver &D = getToolChain().getDriver();
Daniel Dunbarafec1f52009-03-29 18:40:18 +00002314
Daniel Dunbar64198ef2009-09-10 01:21:05 +00002315 CheckPreprocessingOptions(D, Args);
2316
Daniel Dunbarafec1f52009-03-29 18:40:18 +00002317 // Derived from cpp_unique_options.
Daniel Dunbar64198ef2009-09-10 01:21:05 +00002318 // -{C,CC} only with -E is checked in CheckPreprocessingOptions().
2319 Args.AddLastArg(CmdArgs, options::OPT_C);
2320 Args.AddLastArg(CmdArgs, options::OPT_CC);
Daniel Dunbarafec1f52009-03-29 18:40:18 +00002321 if (!Args.hasArg(options::OPT_Q))
2322 CmdArgs.push_back("-quiet");
2323 Args.AddAllArgs(CmdArgs, options::OPT_nostdinc);
Douglas Gregor64b046f2010-03-24 20:13:48 +00002324 Args.AddAllArgs(CmdArgs, options::OPT_nostdincxx);
Daniel Dunbarafec1f52009-03-29 18:40:18 +00002325 Args.AddLastArg(CmdArgs, options::OPT_v);
2326 Args.AddAllArgs(CmdArgs, options::OPT_I_Group, options::OPT_F);
2327 Args.AddLastArg(CmdArgs, options::OPT_P);
2328
2329 // FIXME: Handle %I properly.
2330 if (getToolChain().getArchName() == "x86_64") {
2331 CmdArgs.push_back("-imultilib");
2332 CmdArgs.push_back("x86_64");
2333 }
2334
2335 if (Args.hasArg(options::OPT_MD)) {
2336 CmdArgs.push_back("-MD");
Daniel Dunbar52e96cc2009-03-30 00:34:04 +00002337 CmdArgs.push_back(darwin::CC1::getDependencyFileName(Args, Inputs));
Daniel Dunbarafec1f52009-03-29 18:40:18 +00002338 }
2339
2340 if (Args.hasArg(options::OPT_MMD)) {
2341 CmdArgs.push_back("-MMD");
Daniel Dunbar52e96cc2009-03-30 00:34:04 +00002342 CmdArgs.push_back(darwin::CC1::getDependencyFileName(Args, Inputs));
Daniel Dunbarafec1f52009-03-29 18:40:18 +00002343 }
2344
2345 Args.AddLastArg(CmdArgs, options::OPT_M);
2346 Args.AddLastArg(CmdArgs, options::OPT_MM);
2347 Args.AddAllArgs(CmdArgs, options::OPT_MF);
2348 Args.AddLastArg(CmdArgs, options::OPT_MG);
2349 Args.AddLastArg(CmdArgs, options::OPT_MP);
2350 Args.AddAllArgs(CmdArgs, options::OPT_MQ);
2351 Args.AddAllArgs(CmdArgs, options::OPT_MT);
2352 if (!Args.hasArg(options::OPT_M) && !Args.hasArg(options::OPT_MM) &&
2353 (Args.hasArg(options::OPT_MD) || Args.hasArg(options::OPT_MMD))) {
2354 if (Arg *OutputOpt = Args.getLastArg(options::OPT_o)) {
2355 CmdArgs.push_back("-MQ");
2356 CmdArgs.push_back(OutputOpt->getValue(Args));
2357 }
2358 }
2359
2360 Args.AddLastArg(CmdArgs, options::OPT_remap);
2361 if (Args.hasArg(options::OPT_g3))
2362 CmdArgs.push_back("-dD");
2363 Args.AddLastArg(CmdArgs, options::OPT_H);
2364
2365 AddCPPArgs(Args, CmdArgs);
2366
2367 Args.AddAllArgs(CmdArgs, options::OPT_D, options::OPT_U, options::OPT_A);
2368 Args.AddAllArgs(CmdArgs, options::OPT_i_Group);
2369
2370 for (InputInfoList::const_iterator
2371 it = Inputs.begin(), ie = Inputs.end(); it != ie; ++it) {
2372 const InputInfo &II = *it;
Daniel Dunbard067f7f2009-04-08 23:54:23 +00002373
Daniel Dunbarb440f562010-08-02 02:38:21 +00002374 CmdArgs.push_back(II.getFilename());
Daniel Dunbarafec1f52009-03-29 18:40:18 +00002375 }
2376
2377 Args.AddAllArgValues(CmdArgs, options::OPT_Wp_COMMA,
2378 options::OPT_Xpreprocessor);
2379
2380 if (Args.hasArg(options::OPT_fmudflap)) {
2381 CmdArgs.push_back("-D_MUDFLAP");
2382 CmdArgs.push_back("-include");
2383 CmdArgs.push_back("mf-runtime.h");
2384 }
2385
2386 if (Args.hasArg(options::OPT_fmudflapth)) {
2387 CmdArgs.push_back("-D_MUDFLAP");
2388 CmdArgs.push_back("-D_MUDFLAPTH");
2389 CmdArgs.push_back("-include");
2390 CmdArgs.push_back("mf-runtime.h");
2391 }
2392}
2393
Daniel Dunbard067f7f2009-04-08 23:54:23 +00002394void darwin::CC1::AddCPPArgs(const ArgList &Args,
Daniel Dunbarafec1f52009-03-29 18:40:18 +00002395 ArgStringList &CmdArgs) const {
2396 // Derived from cpp spec.
2397
2398 if (Args.hasArg(options::OPT_static)) {
2399 // The gcc spec is broken here, it refers to dynamic but
2400 // that has been translated. Start by being bug compatible.
Daniel Dunbard067f7f2009-04-08 23:54:23 +00002401
Daniel Dunbarafec1f52009-03-29 18:40:18 +00002402 // if (!Args.hasArg(arglist.parser.dynamicOption))
2403 CmdArgs.push_back("-D__STATIC__");
2404 } else
2405 CmdArgs.push_back("-D__DYNAMIC__");
2406
2407 if (Args.hasArg(options::OPT_pthread))
2408 CmdArgs.push_back("-D_REENTRANT");
2409}
2410
Daniel Dunbare6adeee2009-03-29 17:08:39 +00002411void darwin::Preprocess::ConstructJob(Compilation &C, const JobAction &JA,
Daniel Dunbar7fbaf532010-08-02 02:38:28 +00002412 const InputInfo &Output,
Daniel Dunbard067f7f2009-04-08 23:54:23 +00002413 const InputInfoList &Inputs,
2414 const ArgList &Args,
Daniel Dunbare6adeee2009-03-29 17:08:39 +00002415 const char *LinkingOutput) const {
2416 ArgStringList CmdArgs;
2417
2418 assert(Inputs.size() == 1 && "Unexpected number of inputs!");
2419
2420 CmdArgs.push_back("-E");
2421
2422 if (Args.hasArg(options::OPT_traditional) ||
Daniel Dunbare6adeee2009-03-29 17:08:39 +00002423 Args.hasArg(options::OPT_traditional_cpp))
2424 CmdArgs.push_back("-traditional-cpp");
Daniel Dunbard067f7f2009-04-08 23:54:23 +00002425
Daniel Dunbare6adeee2009-03-29 17:08:39 +00002426 ArgStringList OutputArgs;
Daniel Dunbarb440f562010-08-02 02:38:21 +00002427 assert(Output.isFilename() && "Unexpected CC1 output.");
2428 OutputArgs.push_back("-o");
2429 OutputArgs.push_back(Output.getFilename());
Daniel Dunbare6adeee2009-03-29 17:08:39 +00002430
Joerg Sonnenbergerb86f5f42011-03-06 23:31:01 +00002431 if (Args.hasArg(options::OPT_E) || getToolChain().getDriver().CCCIsCPP) {
Daniel Dunbarf64f5302009-03-29 22:27:40 +00002432 AddCPPOptionsArgs(Args, CmdArgs, Inputs, OutputArgs);
2433 } else {
2434 AddCPPOptionsArgs(Args, CmdArgs, Inputs, ArgStringList());
2435 CmdArgs.append(OutputArgs.begin(), OutputArgs.end());
2436 }
Daniel Dunbard067f7f2009-04-08 23:54:23 +00002437
Daniel Dunbar6a8803a2009-04-03 01:27:06 +00002438 Args.AddAllArgs(CmdArgs, options::OPT_d_Group);
2439
Daniel Dunbare6adeee2009-03-29 17:08:39 +00002440 const char *CC1Name = getCC1Name(Inputs[0].getType());
Daniel Dunbard067f7f2009-04-08 23:54:23 +00002441 const char *Exec =
Daniel Dunbar9c3ed5f2010-07-14 18:46:23 +00002442 Args.MakeArgString(getToolChain().GetProgramPath(CC1Name));
Daniel Dunbar7fbaf532010-08-02 02:38:28 +00002443 C.addCommand(new Command(JA, *this, Exec, CmdArgs));
Daniel Dunbare6adeee2009-03-29 17:08:39 +00002444}
2445
2446void darwin::Compile::ConstructJob(Compilation &C, const JobAction &JA,
Daniel Dunbar7fbaf532010-08-02 02:38:28 +00002447 const InputInfo &Output,
Daniel Dunbard067f7f2009-04-08 23:54:23 +00002448 const InputInfoList &Inputs,
2449 const ArgList &Args,
Daniel Dunbare6adeee2009-03-29 17:08:39 +00002450 const char *LinkingOutput) const {
Daniel Dunbar083edf72009-12-21 18:54:17 +00002451 const Driver &D = getToolChain().getDriver();
Daniel Dunbare6adeee2009-03-29 17:08:39 +00002452 ArgStringList CmdArgs;
2453
2454 assert(Inputs.size() == 1 && "Unexpected number of inputs!");
2455
2456 types::ID InputType = Inputs[0].getType();
2457 const Arg *A;
Eli Friedmanbb0d9a52009-07-14 21:58:17 +00002458 if ((A = Args.getLastArg(options::OPT_traditional)))
Daniel Dunbare6adeee2009-03-29 17:08:39 +00002459 D.Diag(clang::diag::err_drv_argument_only_allowed_with)
2460 << A->getAsString(Args) << "-E";
2461
Daniel Dunbar24e52992010-06-07 23:28:45 +00002462 if (JA.getType() == types::TY_LLVM_IR ||
2463 JA.getType() == types::TY_LTO_IR)
Daniel Dunbare6adeee2009-03-29 17:08:39 +00002464 CmdArgs.push_back("-emit-llvm");
Daniel Dunbar24e52992010-06-07 23:28:45 +00002465 else if (JA.getType() == types::TY_LLVM_BC ||
2466 JA.getType() == types::TY_LTO_BC)
Daniel Dunbare6adeee2009-03-29 17:08:39 +00002467 CmdArgs.push_back("-emit-llvm-bc");
Daniel Dunbar6cdf83c2009-09-01 16:57:46 +00002468 else if (Output.getType() == types::TY_AST)
2469 D.Diag(clang::diag::err_drv_no_ast_support)
Daniel Dunbar1c28f1e2009-09-09 22:32:48 +00002470 << getToolChain().getTripleString();
Daniel Dunbarbcd554f2010-02-11 17:33:45 +00002471 else if (JA.getType() != types::TY_PP_Asm &&
2472 JA.getType() != types::TY_PCH)
Daniel Dunbaraeea8ac2010-02-11 03:16:21 +00002473 D.Diag(clang::diag::err_drv_invalid_gcc_output_type)
2474 << getTypeName(JA.getType());
Daniel Dunbare6adeee2009-03-29 17:08:39 +00002475
2476 ArgStringList OutputArgs;
2477 if (Output.getType() != types::TY_PCH) {
2478 OutputArgs.push_back("-o");
Daniel Dunbarb440f562010-08-02 02:38:21 +00002479 if (Output.isNothing())
Daniel Dunbare6adeee2009-03-29 17:08:39 +00002480 OutputArgs.push_back("/dev/null");
2481 else
2482 OutputArgs.push_back(Output.getFilename());
2483 }
2484
2485 // There is no need for this level of compatibility, but it makes
2486 // diffing easier.
2487 bool OutputArgsEarly = (Args.hasArg(options::OPT_fsyntax_only) ||
2488 Args.hasArg(options::OPT_S));
2489
2490 if (types::getPreprocessedType(InputType) != types::TY_INVALID) {
Daniel Dunbarafec1f52009-03-29 18:40:18 +00002491 AddCPPUniqueOptionsArgs(Args, CmdArgs, Inputs);
Daniel Dunbare6adeee2009-03-29 17:08:39 +00002492 if (OutputArgsEarly) {
2493 AddCC1OptionsArgs(Args, CmdArgs, Inputs, OutputArgs);
2494 } else {
2495 AddCC1OptionsArgs(Args, CmdArgs, Inputs, ArgStringList());
2496 CmdArgs.append(OutputArgs.begin(), OutputArgs.end());
2497 }
2498 } else {
2499 CmdArgs.push_back("-fpreprocessed");
Daniel Dunbard067f7f2009-04-08 23:54:23 +00002500
Daniel Dunbare6adeee2009-03-29 17:08:39 +00002501 for (InputInfoList::const_iterator
2502 it = Inputs.begin(), ie = Inputs.end(); it != ie; ++it) {
2503 const InputInfo &II = *it;
2504
Daniel Dunbar6cdf83c2009-09-01 16:57:46 +00002505 // Reject AST inputs.
2506 if (II.getType() == types::TY_AST) {
2507 D.Diag(clang::diag::err_drv_no_ast_support)
Daniel Dunbar1c28f1e2009-09-09 22:32:48 +00002508 << getToolChain().getTripleString();
Daniel Dunbar6cdf83c2009-09-01 16:57:46 +00002509 return;
2510 }
2511
Daniel Dunbarb440f562010-08-02 02:38:21 +00002512 CmdArgs.push_back(II.getFilename());
Daniel Dunbare6adeee2009-03-29 17:08:39 +00002513 }
2514
2515 if (OutputArgsEarly) {
2516 AddCC1OptionsArgs(Args, CmdArgs, Inputs, OutputArgs);
2517 } else {
2518 AddCC1OptionsArgs(Args, CmdArgs, Inputs, ArgStringList());
2519 CmdArgs.append(OutputArgs.begin(), OutputArgs.end());
2520 }
2521 }
Daniel Dunbard067f7f2009-04-08 23:54:23 +00002522
Daniel Dunbare6adeee2009-03-29 17:08:39 +00002523 if (Output.getType() == types::TY_PCH) {
2524 assert(Output.isFilename() && "Invalid PCH output.");
2525
2526 CmdArgs.push_back("-o");
2527 // NOTE: gcc uses a temp .s file for this, but there doesn't seem
2528 // to be a good reason.
2529 CmdArgs.push_back("/dev/null");
Daniel Dunbard067f7f2009-04-08 23:54:23 +00002530
Daniel Dunbarf4b39e92009-11-21 02:31:29 +00002531 CmdArgs.push_back("--output-pch=");
Daniel Dunbare6adeee2009-03-29 17:08:39 +00002532 CmdArgs.push_back(Output.getFilename());
Daniel Dunbard067f7f2009-04-08 23:54:23 +00002533 }
Daniel Dunbare6adeee2009-03-29 17:08:39 +00002534
2535 const char *CC1Name = getCC1Name(Inputs[0].getType());
Daniel Dunbard067f7f2009-04-08 23:54:23 +00002536 const char *Exec =
Daniel Dunbar9c3ed5f2010-07-14 18:46:23 +00002537 Args.MakeArgString(getToolChain().GetProgramPath(CC1Name));
Daniel Dunbar7fbaf532010-08-02 02:38:28 +00002538 C.addCommand(new Command(JA, *this, Exec, CmdArgs));
Daniel Dunbare6adeee2009-03-29 17:08:39 +00002539}
2540
Daniel Dunbarbe220842009-03-20 16:06:39 +00002541void darwin::Assemble::ConstructJob(Compilation &C, const JobAction &JA,
Daniel Dunbar7fbaf532010-08-02 02:38:28 +00002542 const InputInfo &Output,
Daniel Dunbard067f7f2009-04-08 23:54:23 +00002543 const InputInfoList &Inputs,
2544 const ArgList &Args,
Daniel Dunbarbe220842009-03-20 16:06:39 +00002545 const char *LinkingOutput) const {
2546 ArgStringList CmdArgs;
2547
2548 assert(Inputs.size() == 1 && "Unexpected number of inputs.");
2549 const InputInfo &Input = Inputs[0];
2550
2551 // Bit of a hack, this is only used for original inputs.
Daniel Dunbard067f7f2009-04-08 23:54:23 +00002552 //
Daniel Dunbar5c9c1182009-04-01 00:27:44 +00002553 // FIXME: This is broken for preprocessed .s inputs.
Daniel Dunbarbe220842009-03-20 16:06:39 +00002554 if (Input.isFilename() &&
Daniel Dunbar5c9c1182009-04-01 00:27:44 +00002555 strcmp(Input.getFilename(), Input.getBaseInput()) == 0) {
2556 if (Args.hasArg(options::OPT_gstabs))
2557 CmdArgs.push_back("--gstabs");
2558 else if (Args.hasArg(options::OPT_g_Group))
2559 CmdArgs.push_back("--gdwarf2");
2560 }
Daniel Dunbard067f7f2009-04-08 23:54:23 +00002561
Daniel Dunbarbe220842009-03-20 16:06:39 +00002562 // Derived from asm spec.
Daniel Dunbar3571dd92009-09-09 18:36:27 +00002563 AddDarwinArch(Args, CmdArgs);
Daniel Dunbarbe220842009-03-20 16:06:39 +00002564
Daniel Dunbar6d484762010-07-22 01:47:22 +00002565 // Use -force_cpusubtype_ALL on x86 by default.
2566 if (getToolChain().getTriple().getArch() == llvm::Triple::x86 ||
2567 getToolChain().getTriple().getArch() == llvm::Triple::x86_64 ||
Daniel Dunbar3571dd92009-09-09 18:36:27 +00002568 Args.hasArg(options::OPT_force__cpusubtype__ALL))
2569 CmdArgs.push_back("-force_cpusubtype_ALL");
2570
Daniel Dunbara6b4a3d2009-08-24 22:26:16 +00002571 if (getToolChain().getTriple().getArch() != llvm::Triple::x86_64 &&
2572 (Args.hasArg(options::OPT_mkernel) ||
Daniel Dunbarbe220842009-03-20 16:06:39 +00002573 Args.hasArg(options::OPT_static) ||
Daniel Dunbara6b4a3d2009-08-24 22:26:16 +00002574 Args.hasArg(options::OPT_fapple_kext)))
Daniel Dunbard067f7f2009-04-08 23:54:23 +00002575 CmdArgs.push_back("-static");
2576
Daniel Dunbarbe220842009-03-20 16:06:39 +00002577 Args.AddAllArgValues(CmdArgs, options::OPT_Wa_COMMA,
2578 options::OPT_Xassembler);
2579
2580 assert(Output.isFilename() && "Unexpected lipo output.");
2581 CmdArgs.push_back("-o");
2582 CmdArgs.push_back(Output.getFilename());
2583
Daniel Dunbarb440f562010-08-02 02:38:21 +00002584 assert(Input.isFilename() && "Invalid input.");
2585 CmdArgs.push_back(Input.getFilename());
Daniel Dunbarbe220842009-03-20 16:06:39 +00002586
2587 // asm_final spec is empty.
2588
Daniel Dunbard067f7f2009-04-08 23:54:23 +00002589 const char *Exec =
Daniel Dunbar9c3ed5f2010-07-14 18:46:23 +00002590 Args.MakeArgString(getToolChain().GetProgramPath("as"));
Daniel Dunbar7fbaf532010-08-02 02:38:28 +00002591 C.addCommand(new Command(JA, *this, Exec, CmdArgs));
Daniel Dunbarbe220842009-03-20 16:06:39 +00002592}
Daniel Dunbar64ed5e32009-03-20 00:52:38 +00002593
Daniel Dunbare9ded432009-09-09 18:36:20 +00002594void darwin::DarwinTool::AddDarwinArch(const ArgList &Args,
2595 ArgStringList &CmdArgs) const {
Daniel Dunbardcc3b652010-01-22 02:04:58 +00002596 llvm::StringRef ArchName = getDarwinToolChain().getDarwinArchName(Args);
2597
Daniel Dunbarc1964212009-03-26 16:23:12 +00002598 // Derived from darwin_arch spec.
2599 CmdArgs.push_back("-arch");
Daniel Dunbardcc3b652010-01-22 02:04:58 +00002600 CmdArgs.push_back(Args.MakeArgString(ArchName));
Daniel Dunbar91dbfd62009-09-04 18:35:31 +00002601
Daniel Dunbardcc3b652010-01-22 02:04:58 +00002602 // FIXME: Is this needed anymore?
2603 if (ArchName == "arm")
Daniel Dunbar91dbfd62009-09-04 18:35:31 +00002604 CmdArgs.push_back("-force_cpusubtype_ALL");
Daniel Dunbarc1964212009-03-26 16:23:12 +00002605}
2606
Daniel Dunbarccbc4522010-09-09 21:51:05 +00002607void darwin::Link::AddLinkArgs(Compilation &C,
2608 const ArgList &Args,
Daniel Dunbarc1964212009-03-26 16:23:12 +00002609 ArgStringList &CmdArgs) const {
Daniel Dunbar083edf72009-12-21 18:54:17 +00002610 const Driver &D = getToolChain().getDriver();
Daniel Dunbarc1964212009-03-26 16:23:12 +00002611
Daniel Dunbarcacb0e22010-08-11 23:07:50 +00002612 unsigned Version[3] = { 0, 0, 0 };
2613 if (Arg *A = Args.getLastArg(options::OPT_mlinker_version_EQ)) {
2614 bool HadExtra;
2615 if (!Driver::GetReleaseVersion(A->getValue(Args), Version[0],
2616 Version[1], Version[2], HadExtra) ||
2617 HadExtra)
2618 D.Diag(clang::diag::err_drv_invalid_version_number)
2619 << A->getAsString(Args);
2620 }
2621
2622 // Newer linkers support -demangle, pass it if supported and not disabled by
2623 // the user.
Daniel Dunbar6d776eb2010-11-19 17:51:40 +00002624 //
2625 // FIXME: We temporarily avoid passing -demangle to any iOS linker, because
2626 // unfortunately we can't be guaranteed that the linker version used there
2627 // will match the linker version detected at configure time. We need the
2628 // universal driver.
2629 if (Version[0] >= 100 && !Args.hasArg(options::OPT_Z_Xlinker__no_demangle) &&
2630 !getDarwinToolChain().isTargetIPhoneOS()) {
Daniel Dunbar1eaf5c62010-09-07 17:07:49 +00002631 // Don't pass -demangle to ld_classic.
2632 //
2633 // FIXME: This is a temporary workaround, ld should be handling this.
2634 bool UsesLdClassic = (getToolChain().getArch() == llvm::Triple::x86 &&
2635 Args.hasArg(options::OPT_static));
Daniel Dunbar3d7e0e22010-09-07 17:50:41 +00002636 if (getToolChain().getArch() == llvm::Triple::x86) {
2637 for (arg_iterator it = Args.filtered_begin(options::OPT_Xlinker,
2638 options::OPT_Wl_COMMA),
2639 ie = Args.filtered_end(); it != ie; ++it) {
2640 const Arg *A = *it;
2641 for (unsigned i = 0, e = A->getNumValues(); i != e; ++i)
2642 if (llvm::StringRef(A->getValue(Args, i)) == "-kext")
2643 UsesLdClassic = true;
2644 }
2645 }
Daniel Dunbar1eaf5c62010-09-07 17:07:49 +00002646 if (!UsesLdClassic)
2647 CmdArgs.push_back("-demangle");
Daniel Dunbarcacb0e22010-08-11 23:07:50 +00002648 }
2649
Daniel Dunbarc1964212009-03-26 16:23:12 +00002650 // Derived from the "link" spec.
2651 Args.AddAllArgs(CmdArgs, options::OPT_static);
2652 if (!Args.hasArg(options::OPT_static))
2653 CmdArgs.push_back("-dynamic");
2654 if (Args.hasArg(options::OPT_fgnu_runtime)) {
2655 // FIXME: gcc replaces -lobjc in forward args with -lobjc-gnu
2656 // here. How do we wish to handle such things?
2657 }
Daniel Dunbard067f7f2009-04-08 23:54:23 +00002658
Daniel Dunbarc1964212009-03-26 16:23:12 +00002659 if (!Args.hasArg(options::OPT_dynamiclib)) {
Daniel Dunbara48823f2010-01-22 02:04:52 +00002660 AddDarwinArch(Args, CmdArgs);
Daniel Dunbara48823f2010-01-22 02:04:52 +00002661 // FIXME: Why do this only on this path?
Daniel Dunbar93d7acf2010-01-22 03:37:33 +00002662 Args.AddLastArg(CmdArgs, options::OPT_force__cpusubtype__ALL);
Daniel Dunbarc1964212009-03-26 16:23:12 +00002663
2664 Args.AddLastArg(CmdArgs, options::OPT_bundle);
2665 Args.AddAllArgs(CmdArgs, options::OPT_bundle__loader);
2666 Args.AddAllArgs(CmdArgs, options::OPT_client__name);
2667
2668 Arg *A;
2669 if ((A = Args.getLastArg(options::OPT_compatibility__version)) ||
2670 (A = Args.getLastArg(options::OPT_current__version)) ||
2671 (A = Args.getLastArg(options::OPT_install__name)))
2672 D.Diag(clang::diag::err_drv_argument_only_allowed_with)
2673 << A->getAsString(Args) << "-dynamiclib";
2674
2675 Args.AddLastArg(CmdArgs, options::OPT_force__flat__namespace);
2676 Args.AddLastArg(CmdArgs, options::OPT_keep__private__externs);
2677 Args.AddLastArg(CmdArgs, options::OPT_private__bundle);
2678 } else {
2679 CmdArgs.push_back("-dylib");
2680
2681 Arg *A;
2682 if ((A = Args.getLastArg(options::OPT_bundle)) ||
2683 (A = Args.getLastArg(options::OPT_bundle__loader)) ||
2684 (A = Args.getLastArg(options::OPT_client__name)) ||
2685 (A = Args.getLastArg(options::OPT_force__flat__namespace)) ||
2686 (A = Args.getLastArg(options::OPT_keep__private__externs)) ||
2687 (A = Args.getLastArg(options::OPT_private__bundle)))
2688 D.Diag(clang::diag::err_drv_argument_not_allowed_with)
2689 << A->getAsString(Args) << "-dynamiclib";
Daniel Dunbard067f7f2009-04-08 23:54:23 +00002690
Daniel Dunbarc1964212009-03-26 16:23:12 +00002691 Args.AddAllArgsTranslated(CmdArgs, options::OPT_compatibility__version,
2692 "-dylib_compatibility_version");
2693 Args.AddAllArgsTranslated(CmdArgs, options::OPT_current__version,
2694 "-dylib_current_version");
2695
Daniel Dunbara48823f2010-01-22 02:04:52 +00002696 AddDarwinArch(Args, CmdArgs);
Daniel Dunbarc1964212009-03-26 16:23:12 +00002697
2698 Args.AddAllArgsTranslated(CmdArgs, options::OPT_install__name,
2699 "-dylib_install_name");
2700 }
2701
2702 Args.AddLastArg(CmdArgs, options::OPT_all__load);
2703 Args.AddAllArgs(CmdArgs, options::OPT_allowable__client);
2704 Args.AddLastArg(CmdArgs, options::OPT_bind__at__load);
Daniel Dunbar15c89422010-01-27 00:56:37 +00002705 if (getDarwinToolChain().isTargetIPhoneOS())
Daniel Dunbar2b5f6812009-09-04 18:35:41 +00002706 Args.AddLastArg(CmdArgs, options::OPT_arch__errors__fatal);
Daniel Dunbarc1964212009-03-26 16:23:12 +00002707 Args.AddLastArg(CmdArgs, options::OPT_dead__strip);
2708 Args.AddLastArg(CmdArgs, options::OPT_no__dead__strip__inits__and__terms);
2709 Args.AddAllArgs(CmdArgs, options::OPT_dylib__file);
2710 Args.AddLastArg(CmdArgs, options::OPT_dynamic);
2711 Args.AddAllArgs(CmdArgs, options::OPT_exported__symbols__list);
2712 Args.AddLastArg(CmdArgs, options::OPT_flat__namespace);
2713 Args.AddAllArgs(CmdArgs, options::OPT_headerpad__max__install__names);
2714 Args.AddAllArgs(CmdArgs, options::OPT_image__base);
2715 Args.AddAllArgs(CmdArgs, options::OPT_init);
2716
Daniel Dunbar19afd612010-01-26 03:56:40 +00002717 // Adding all arguments doesn't make sense here but this is what gcc does. One
2718 // of this should always be present thanks to argument translation.
2719 assert((Args.hasArg(options::OPT_mmacosx_version_min_EQ) ||
2720 Args.hasArg(options::OPT_miphoneos_version_min_EQ)) &&
2721 "Missing version argument (lost in translation)?");
Daniel Dunbar84e727f2009-09-04 18:35:21 +00002722 Args.AddAllArgsTranslated(CmdArgs, options::OPT_mmacosx_version_min_EQ,
Daniel Dunbar1c7577c2009-11-04 06:24:38 +00002723 "-macosx_version_min");
Daniel Dunbarc1964212009-03-26 16:23:12 +00002724 Args.AddAllArgsTranslated(CmdArgs, options::OPT_miphoneos_version_min_EQ,
2725 "-iphoneos_version_min");
2726 Args.AddLastArg(CmdArgs, options::OPT_nomultidefs);
2727 Args.AddLastArg(CmdArgs, options::OPT_multi__module);
2728 Args.AddLastArg(CmdArgs, options::OPT_single__module);
2729 Args.AddAllArgs(CmdArgs, options::OPT_multiply__defined);
2730 Args.AddAllArgs(CmdArgs, options::OPT_multiply__defined__unused);
Daniel Dunbard067f7f2009-04-08 23:54:23 +00002731
Daniel Dunbaraf68a882010-07-13 23:31:40 +00002732 if (const Arg *A = Args.getLastArg(options::OPT_fpie, options::OPT_fPIE,
2733 options::OPT_fno_pie,
2734 options::OPT_fno_PIE)) {
2735 if (A->getOption().matches(options::OPT_fpie) ||
2736 A->getOption().matches(options::OPT_fPIE))
2737 CmdArgs.push_back("-pie");
2738 else
2739 CmdArgs.push_back("-no_pie");
2740 }
Daniel Dunbarc1964212009-03-26 16:23:12 +00002741
2742 Args.AddLastArg(CmdArgs, options::OPT_prebind);
2743 Args.AddLastArg(CmdArgs, options::OPT_noprebind);
2744 Args.AddLastArg(CmdArgs, options::OPT_nofixprebinding);
2745 Args.AddLastArg(CmdArgs, options::OPT_prebind__all__twolevel__modules);
2746 Args.AddLastArg(CmdArgs, options::OPT_read__only__relocs);
2747 Args.AddAllArgs(CmdArgs, options::OPT_sectcreate);
2748 Args.AddAllArgs(CmdArgs, options::OPT_sectorder);
2749 Args.AddAllArgs(CmdArgs, options::OPT_seg1addr);
2750 Args.AddAllArgs(CmdArgs, options::OPT_segprot);
2751 Args.AddAllArgs(CmdArgs, options::OPT_segaddr);
2752 Args.AddAllArgs(CmdArgs, options::OPT_segs__read__only__addr);
2753 Args.AddAllArgs(CmdArgs, options::OPT_segs__read__write__addr);
2754 Args.AddAllArgs(CmdArgs, options::OPT_seg__addr__table);
2755 Args.AddAllArgs(CmdArgs, options::OPT_seg__addr__table__filename);
2756 Args.AddAllArgs(CmdArgs, options::OPT_sub__library);
2757 Args.AddAllArgs(CmdArgs, options::OPT_sub__umbrella);
Daniel Dunbar2b5f6812009-09-04 18:35:41 +00002758
Daniel Dunbarc1964212009-03-26 16:23:12 +00002759 Args.AddAllArgsTranslated(CmdArgs, options::OPT_isysroot, "-syslibroot");
Daniel Dunbar15c89422010-01-27 00:56:37 +00002760 if (getDarwinToolChain().isTargetIPhoneOS()) {
Daniel Dunbar2b5f6812009-09-04 18:35:41 +00002761 if (!Args.hasArg(options::OPT_isysroot)) {
2762 CmdArgs.push_back("-syslibroot");
2763 CmdArgs.push_back("/Developer/SDKs/Extra");
2764 }
2765 }
2766
Daniel Dunbarc1964212009-03-26 16:23:12 +00002767 Args.AddLastArg(CmdArgs, options::OPT_twolevel__namespace);
2768 Args.AddLastArg(CmdArgs, options::OPT_twolevel__namespace__hints);
2769 Args.AddAllArgs(CmdArgs, options::OPT_umbrella);
2770 Args.AddAllArgs(CmdArgs, options::OPT_undefined);
2771 Args.AddAllArgs(CmdArgs, options::OPT_unexported__symbols__list);
Daniel Dunbar2b5f6812009-09-04 18:35:41 +00002772 Args.AddAllArgs(CmdArgs, options::OPT_weak__reference__mismatches);
Daniel Dunbarc1964212009-03-26 16:23:12 +00002773 Args.AddLastArg(CmdArgs, options::OPT_X_Flag);
2774 Args.AddAllArgs(CmdArgs, options::OPT_y);
2775 Args.AddLastArg(CmdArgs, options::OPT_w);
2776 Args.AddAllArgs(CmdArgs, options::OPT_pagezero__size);
2777 Args.AddAllArgs(CmdArgs, options::OPT_segs__read__);
2778 Args.AddLastArg(CmdArgs, options::OPT_seglinkedit);
2779 Args.AddLastArg(CmdArgs, options::OPT_noseglinkedit);
2780 Args.AddAllArgs(CmdArgs, options::OPT_sectalign);
2781 Args.AddAllArgs(CmdArgs, options::OPT_sectobjectsymbols);
2782 Args.AddAllArgs(CmdArgs, options::OPT_segcreate);
2783 Args.AddLastArg(CmdArgs, options::OPT_whyload);
2784 Args.AddLastArg(CmdArgs, options::OPT_whatsloaded);
2785 Args.AddAllArgs(CmdArgs, options::OPT_dylinker__install__name);
2786 Args.AddLastArg(CmdArgs, options::OPT_dylinker);
2787 Args.AddLastArg(CmdArgs, options::OPT_Mach);
2788}
2789
2790void darwin::Link::ConstructJob(Compilation &C, const JobAction &JA,
Daniel Dunbar7fbaf532010-08-02 02:38:28 +00002791 const InputInfo &Output,
Daniel Dunbard067f7f2009-04-08 23:54:23 +00002792 const InputInfoList &Inputs,
2793 const ArgList &Args,
Daniel Dunbarc1964212009-03-26 16:23:12 +00002794 const char *LinkingOutput) const {
2795 assert(Output.getType() == types::TY_Image && "Invalid linker output type.");
Daniel Dunbarc09988d2009-09-08 16:39:16 +00002796
Daniel Dunbarc1964212009-03-26 16:23:12 +00002797 // The logic here is derived from gcc's behavior; most of which
2798 // comes from specs (starting with link_command). Consult gcc for
2799 // more information.
Daniel Dunbarc1964212009-03-26 16:23:12 +00002800 ArgStringList CmdArgs;
2801
2802 // I'm not sure why this particular decomposition exists in gcc, but
2803 // we follow suite for ease of comparison.
Daniel Dunbarccbc4522010-09-09 21:51:05 +00002804 AddLinkArgs(C, Args, CmdArgs);
Daniel Dunbarc1964212009-03-26 16:23:12 +00002805
Daniel Dunbarc1964212009-03-26 16:23:12 +00002806 Args.AddAllArgs(CmdArgs, options::OPT_d_Flag);
2807 Args.AddAllArgs(CmdArgs, options::OPT_s);
2808 Args.AddAllArgs(CmdArgs, options::OPT_t);
2809 Args.AddAllArgs(CmdArgs, options::OPT_Z_Flag);
2810 Args.AddAllArgs(CmdArgs, options::OPT_u_Group);
2811 Args.AddAllArgs(CmdArgs, options::OPT_A);
2812 Args.AddLastArg(CmdArgs, options::OPT_e);
2813 Args.AddAllArgs(CmdArgs, options::OPT_m_Separate);
2814 Args.AddAllArgs(CmdArgs, options::OPT_r);
2815
Daniel Dunbar767bbab2010-10-18 22:08:36 +00002816 // Forward -ObjC when either -ObjC or -ObjC++ is used, to force loading
2817 // members of static archive libraries which implement Objective-C classes or
2818 // categories.
2819 if (Args.hasArg(options::OPT_ObjC) || Args.hasArg(options::OPT_ObjCXX))
2820 CmdArgs.push_back("-ObjC");
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00002821
Daniel Dunbarc1964212009-03-26 16:23:12 +00002822 CmdArgs.push_back("-o");
2823 CmdArgs.push_back(Output.getFilename());
2824
Daniel Dunbarc1964212009-03-26 16:23:12 +00002825 if (!Args.hasArg(options::OPT_A) &&
2826 !Args.hasArg(options::OPT_nostdlib) &&
2827 !Args.hasArg(options::OPT_nostartfiles)) {
2828 // Derived from startfile spec.
2829 if (Args.hasArg(options::OPT_dynamiclib)) {
Daniel Dunbard067f7f2009-04-08 23:54:23 +00002830 // Derived from darwin_dylib1 spec.
Daniel Dunbar83608032010-01-27 00:56:56 +00002831 if (getDarwinToolChain().isTargetIPhoneOS()) {
2832 if (getDarwinToolChain().isIPhoneOSVersionLT(3, 1))
2833 CmdArgs.push_back("-ldylib1.o");
2834 } else {
Daniel Dunbar6d23b2f2010-01-27 00:57:03 +00002835 if (getDarwinToolChain().isMacosxVersionLT(10, 5))
Daniel Dunbar83608032010-01-27 00:56:56 +00002836 CmdArgs.push_back("-ldylib1.o");
Daniel Dunbar6d23b2f2010-01-27 00:57:03 +00002837 else if (getDarwinToolChain().isMacosxVersionLT(10, 6))
Daniel Dunbar83608032010-01-27 00:56:56 +00002838 CmdArgs.push_back("-ldylib1.10.5.o");
2839 }
Daniel Dunbarc1964212009-03-26 16:23:12 +00002840 } else {
2841 if (Args.hasArg(options::OPT_bundle)) {
Daniel Dunbarae8bca02009-04-01 03:17:40 +00002842 if (!Args.hasArg(options::OPT_static)) {
2843 // Derived from darwin_bundle1 spec.
Daniel Dunbar83608032010-01-27 00:56:56 +00002844 if (getDarwinToolChain().isTargetIPhoneOS()) {
2845 if (getDarwinToolChain().isIPhoneOSVersionLT(3, 1))
2846 CmdArgs.push_back("-lbundle1.o");
2847 } else {
Daniel Dunbar6d23b2f2010-01-27 00:57:03 +00002848 if (getDarwinToolChain().isMacosxVersionLT(10, 6))
Daniel Dunbar83608032010-01-27 00:56:56 +00002849 CmdArgs.push_back("-lbundle1.o");
2850 }
Daniel Dunbarae8bca02009-04-01 03:17:40 +00002851 }
Daniel Dunbarc1964212009-03-26 16:23:12 +00002852 } else {
Daniel Dunbar733b0f82011-03-01 18:49:30 +00002853 if (Args.hasArg(options::OPT_pg) &&
2854 getToolChain().SupportsProfiling()) {
Daniel Dunbarc1964212009-03-26 16:23:12 +00002855 if (Args.hasArg(options::OPT_static) ||
2856 Args.hasArg(options::OPT_object) ||
2857 Args.hasArg(options::OPT_preload)) {
2858 CmdArgs.push_back("-lgcrt0.o");
2859 } else {
2860 CmdArgs.push_back("-lgcrt1.o");
Daniel Dunbard067f7f2009-04-08 23:54:23 +00002861
Daniel Dunbarc1964212009-03-26 16:23:12 +00002862 // darwin_crt2 spec is empty.
Daniel Dunbard067f7f2009-04-08 23:54:23 +00002863 }
Daniel Dunbarc1964212009-03-26 16:23:12 +00002864 } else {
2865 if (Args.hasArg(options::OPT_static) ||
2866 Args.hasArg(options::OPT_object) ||
2867 Args.hasArg(options::OPT_preload)) {
2868 CmdArgs.push_back("-lcrt0.o");
2869 } else {
2870 // Derived from darwin_crt1 spec.
Daniel Dunbar15c89422010-01-27 00:56:37 +00002871 if (getDarwinToolChain().isTargetIPhoneOS()) {
Daniel Dunbar83608032010-01-27 00:56:56 +00002872 if (getDarwinToolChain().isIPhoneOSVersionLT(3, 1))
2873 CmdArgs.push_back("-lcrt1.o");
2874 else
2875 CmdArgs.push_back("-lcrt1.3.1.o");
Daniel Dunbar6d23b2f2010-01-27 00:57:03 +00002876 } else {
2877 if (getDarwinToolChain().isMacosxVersionLT(10, 5))
2878 CmdArgs.push_back("-lcrt1.o");
2879 else if (getDarwinToolChain().isMacosxVersionLT(10, 6))
2880 CmdArgs.push_back("-lcrt1.10.5.o");
2881 else
2882 CmdArgs.push_back("-lcrt1.10.6.o");
Daniel Dunbard067f7f2009-04-08 23:54:23 +00002883
Daniel Dunbar6d23b2f2010-01-27 00:57:03 +00002884 // darwin_crt2 spec is empty.
2885 }
Daniel Dunbarc1964212009-03-26 16:23:12 +00002886 }
2887 }
2888 }
2889 }
2890
Daniel Dunbar6d23b2f2010-01-27 00:57:03 +00002891 if (!getDarwinToolChain().isTargetIPhoneOS() &&
2892 Args.hasArg(options::OPT_shared_libgcc) &&
2893 getDarwinToolChain().isMacosxVersionLT(10, 5)) {
Daniel Dunbar1c28f1e2009-09-09 22:32:48 +00002894 const char *Str =
Daniel Dunbar9c3ed5f2010-07-14 18:46:23 +00002895 Args.MakeArgString(getToolChain().GetFilePath("crt3.o"));
Daniel Dunbar1c28f1e2009-09-09 22:32:48 +00002896 CmdArgs.push_back(Str);
Daniel Dunbarc1964212009-03-26 16:23:12 +00002897 }
2898 }
2899
2900 Args.AddAllArgs(CmdArgs, options::OPT_L);
Daniel Dunbard067f7f2009-04-08 23:54:23 +00002901
Daniel Dunbarc1964212009-03-26 16:23:12 +00002902 if (Args.hasArg(options::OPT_fopenmp))
2903 // This is more complicated in gcc...
2904 CmdArgs.push_back("-lgomp");
2905
Daniel Dunbar4c30b892009-09-18 08:14:36 +00002906 getDarwinToolChain().AddLinkSearchPathArgs(Args, CmdArgs);
Daniel Dunbard067f7f2009-04-08 23:54:23 +00002907
Daniel Dunbar54423b22010-09-17 00:24:54 +00002908 AddLinkerInputs(getToolChain(), Inputs, Args, CmdArgs);
Daniel Dunbarc1964212009-03-26 16:23:12 +00002909
2910 if (LinkingOutput) {
2911 CmdArgs.push_back("-arch_multiple");
2912 CmdArgs.push_back("-final_output");
2913 CmdArgs.push_back(LinkingOutput);
2914 }
2915
2916 if (Args.hasArg(options::OPT_fprofile_arcs) ||
2917 Args.hasArg(options::OPT_fprofile_generate) ||
2918 Args.hasArg(options::OPT_fcreate_profile) ||
2919 Args.hasArg(options::OPT_coverage))
2920 CmdArgs.push_back("-lgcov");
Daniel Dunbard067f7f2009-04-08 23:54:23 +00002921
Daniel Dunbarc1964212009-03-26 16:23:12 +00002922 if (Args.hasArg(options::OPT_fnested_functions))
2923 CmdArgs.push_back("-allow_stack_execute");
Daniel Dunbard067f7f2009-04-08 23:54:23 +00002924
Daniel Dunbarc1964212009-03-26 16:23:12 +00002925 if (!Args.hasArg(options::OPT_nostdlib) &&
2926 !Args.hasArg(options::OPT_nodefaultlibs)) {
Daniel Dunbar083edf72009-12-21 18:54:17 +00002927 if (getToolChain().getDriver().CCCIsCXX)
Daniel Dunbar3f7796f2010-09-17 01:20:05 +00002928 getToolChain().AddCXXStdlibLibArgs(Args, CmdArgs);
Daniel Dunbarad0f62b2009-04-08 06:06:21 +00002929
Daniel Dunbarc1964212009-03-26 16:23:12 +00002930 // link_ssp spec is empty.
2931
Daniel Dunbar26d482a2009-09-18 08:15:03 +00002932 // Let the tool chain choose which runtime library to link.
2933 getDarwinToolChain().AddLinkRuntimeLibArgs(Args, CmdArgs);
Daniel Dunbarc1964212009-03-26 16:23:12 +00002934 }
2935
2936 if (!Args.hasArg(options::OPT_A) &&
2937 !Args.hasArg(options::OPT_nostdlib) &&
2938 !Args.hasArg(options::OPT_nostartfiles)) {
2939 // endfile_spec is empty.
2940 }
2941
2942 Args.AddAllArgs(CmdArgs, options::OPT_T_Group);
2943 Args.AddAllArgs(CmdArgs, options::OPT_F);
2944
Daniel Dunbard067f7f2009-04-08 23:54:23 +00002945 const char *Exec =
Daniel Dunbar9c3ed5f2010-07-14 18:46:23 +00002946 Args.MakeArgString(getToolChain().GetProgramPath("ld"));
Daniel Dunbar7fbaf532010-08-02 02:38:28 +00002947 C.addCommand(new Command(JA, *this, Exec, CmdArgs));
Daniel Dunbarc1964212009-03-26 16:23:12 +00002948}
2949
Daniel Dunbar64ed5e32009-03-20 00:52:38 +00002950void darwin::Lipo::ConstructJob(Compilation &C, const JobAction &JA,
Daniel Dunbar7fbaf532010-08-02 02:38:28 +00002951 const InputInfo &Output,
Daniel Dunbard067f7f2009-04-08 23:54:23 +00002952 const InputInfoList &Inputs,
2953 const ArgList &Args,
Daniel Dunbar64ed5e32009-03-20 00:52:38 +00002954 const char *LinkingOutput) const {
2955 ArgStringList CmdArgs;
2956
2957 CmdArgs.push_back("-create");
2958 assert(Output.isFilename() && "Unexpected lipo output.");
Daniel Dunbar06686ab2009-03-24 00:24:37 +00002959
2960 CmdArgs.push_back("-output");
Daniel Dunbar64ed5e32009-03-20 00:52:38 +00002961 CmdArgs.push_back(Output.getFilename());
Daniel Dunbar06686ab2009-03-24 00:24:37 +00002962
Daniel Dunbar64ed5e32009-03-20 00:52:38 +00002963 for (InputInfoList::const_iterator
2964 it = Inputs.begin(), ie = Inputs.end(); it != ie; ++it) {
2965 const InputInfo &II = *it;
2966 assert(II.isFilename() && "Unexpected lipo input.");
2967 CmdArgs.push_back(II.getFilename());
2968 }
Daniel Dunbard067f7f2009-04-08 23:54:23 +00002969 const char *Exec =
Daniel Dunbar9c3ed5f2010-07-14 18:46:23 +00002970 Args.MakeArgString(getToolChain().GetProgramPath("lipo"));
Daniel Dunbar7fbaf532010-08-02 02:38:28 +00002971 C.addCommand(new Command(JA, *this, Exec, CmdArgs));
Daniel Dunbar64ed5e32009-03-20 00:52:38 +00002972}
Daniel Dunbar8eb473c2009-03-31 17:45:15 +00002973
Daniel Dunbar88299622010-06-04 18:28:36 +00002974void darwin::Dsymutil::ConstructJob(Compilation &C, const JobAction &JA,
Daniel Dunbar7fbaf532010-08-02 02:38:28 +00002975 const InputInfo &Output,
Daniel Dunbar88299622010-06-04 18:28:36 +00002976 const InputInfoList &Inputs,
2977 const ArgList &Args,
2978 const char *LinkingOutput) const {
2979 ArgStringList CmdArgs;
2980
2981 assert(Inputs.size() == 1 && "Unable to handle multiple inputs.");
2982 const InputInfo &Input = Inputs[0];
2983 assert(Input.isFilename() && "Unexpected dsymutil input.");
2984 CmdArgs.push_back(Input.getFilename());
2985
2986 CmdArgs.push_back("-o");
2987 CmdArgs.push_back(Output.getFilename());
2988
2989 const char *Exec =
Daniel Dunbar9c3ed5f2010-07-14 18:46:23 +00002990 Args.MakeArgString(getToolChain().GetProgramPath("dsymutil"));
Daniel Dunbar7fbaf532010-08-02 02:38:28 +00002991 C.addCommand(new Command(JA, *this, Exec, CmdArgs));
Daniel Dunbar88299622010-06-04 18:28:36 +00002992}
2993
Edward O'Callaghan856e4ff2009-08-22 01:06:46 +00002994void auroraux::Assemble::ConstructJob(Compilation &C, const JobAction &JA,
Daniel Dunbar7fbaf532010-08-02 02:38:28 +00002995 const InputInfo &Output,
Daniel Dunbar1c7577c2009-11-04 06:24:38 +00002996 const InputInfoList &Inputs,
2997 const ArgList &Args,
2998 const char *LinkingOutput) const {
Edward O'Callaghan856e4ff2009-08-22 01:06:46 +00002999 ArgStringList CmdArgs;
3000
3001 Args.AddAllArgValues(CmdArgs, options::OPT_Wa_COMMA,
3002 options::OPT_Xassembler);
3003
3004 CmdArgs.push_back("-o");
Daniel Dunbarb440f562010-08-02 02:38:21 +00003005 CmdArgs.push_back(Output.getFilename());
Edward O'Callaghan856e4ff2009-08-22 01:06:46 +00003006
3007 for (InputInfoList::const_iterator
3008 it = Inputs.begin(), ie = Inputs.end(); it != ie; ++it) {
3009 const InputInfo &II = *it;
Daniel Dunbarb440f562010-08-02 02:38:21 +00003010 CmdArgs.push_back(II.getFilename());
Edward O'Callaghan856e4ff2009-08-22 01:06:46 +00003011 }
3012
3013 const char *Exec =
Daniel Dunbar9c3ed5f2010-07-14 18:46:23 +00003014 Args.MakeArgString(getToolChain().GetProgramPath("gas"));
Daniel Dunbar7fbaf532010-08-02 02:38:28 +00003015 C.addCommand(new Command(JA, *this, Exec, CmdArgs));
Edward O'Callaghan856e4ff2009-08-22 01:06:46 +00003016}
3017
3018void auroraux::Link::ConstructJob(Compilation &C, const JobAction &JA,
Daniel Dunbar7fbaf532010-08-02 02:38:28 +00003019 const InputInfo &Output,
Daniel Dunbar1c7577c2009-11-04 06:24:38 +00003020 const InputInfoList &Inputs,
3021 const ArgList &Args,
3022 const char *LinkingOutput) const {
Edward O'Callaghan856e4ff2009-08-22 01:06:46 +00003023 ArgStringList CmdArgs;
3024
3025 if ((!Args.hasArg(options::OPT_nostdlib)) &&
Daniel Dunbar1c7577c2009-11-04 06:24:38 +00003026 (!Args.hasArg(options::OPT_shared))) {
Edward O'Callaghan856e4ff2009-08-22 01:06:46 +00003027 CmdArgs.push_back("-e");
Edward O'Callaghand8712d92009-10-15 07:44:07 +00003028 CmdArgs.push_back("_start");
Edward O'Callaghan856e4ff2009-08-22 01:06:46 +00003029 }
3030
3031 if (Args.hasArg(options::OPT_static)) {
3032 CmdArgs.push_back("-Bstatic");
Edward O'Callaghand8712d92009-10-15 07:44:07 +00003033 CmdArgs.push_back("-dn");
Edward O'Callaghan856e4ff2009-08-22 01:06:46 +00003034 } else {
Edward O'Callaghand8712d92009-10-15 07:44:07 +00003035// CmdArgs.push_back("--eh-frame-hdr");
Edward O'Callaghan856e4ff2009-08-22 01:06:46 +00003036 CmdArgs.push_back("-Bdynamic");
3037 if (Args.hasArg(options::OPT_shared)) {
3038 CmdArgs.push_back("-shared");
3039 } else {
Edward O'Callaghan7d3c2752009-10-16 19:44:18 +00003040 CmdArgs.push_back("--dynamic-linker");
Edward O'Callaghan856e4ff2009-08-22 01:06:46 +00003041 CmdArgs.push_back("/lib/ld.so.1"); // 64Bit Path /lib/amd64/ld.so.1
3042 }
3043 }
3044
Daniel Dunbarb440f562010-08-02 02:38:21 +00003045 if (Output.isFilename()) {
Edward O'Callaghan856e4ff2009-08-22 01:06:46 +00003046 CmdArgs.push_back("-o");
3047 CmdArgs.push_back(Output.getFilename());
3048 } else {
3049 assert(Output.isNothing() && "Invalid output.");
3050 }
3051
3052 if (!Args.hasArg(options::OPT_nostdlib) &&
3053 !Args.hasArg(options::OPT_nostartfiles)) {
3054 if (!Args.hasArg(options::OPT_shared)) {
Chris Lattner3e2ee142010-07-07 16:01:42 +00003055 CmdArgs.push_back(Args.MakeArgString(
Daniel Dunbar9c3ed5f2010-07-14 18:46:23 +00003056 getToolChain().GetFilePath("crt1.o")));
Chris Lattner3e2ee142010-07-07 16:01:42 +00003057 CmdArgs.push_back(Args.MakeArgString(
Daniel Dunbar9c3ed5f2010-07-14 18:46:23 +00003058 getToolChain().GetFilePath("crti.o")));
Chris Lattner3e2ee142010-07-07 16:01:42 +00003059 CmdArgs.push_back(Args.MakeArgString(
Daniel Dunbar9c3ed5f2010-07-14 18:46:23 +00003060 getToolChain().GetFilePath("crtbegin.o")));
Edward O'Callaghan856e4ff2009-08-22 01:06:46 +00003061 } else {
Chris Lattner3e2ee142010-07-07 16:01:42 +00003062 CmdArgs.push_back(Args.MakeArgString(
Daniel Dunbar9c3ed5f2010-07-14 18:46:23 +00003063 getToolChain().GetFilePath("crti.o")));
Edward O'Callaghan856e4ff2009-08-22 01:06:46 +00003064 }
Chris Lattner3e2ee142010-07-07 16:01:42 +00003065 CmdArgs.push_back(Args.MakeArgString(
Daniel Dunbar9c3ed5f2010-07-14 18:46:23 +00003066 getToolChain().GetFilePath("crtn.o")));
Edward O'Callaghan856e4ff2009-08-22 01:06:46 +00003067 }
3068
Daniel Dunbar1c7577c2009-11-04 06:24:38 +00003069 CmdArgs.push_back(Args.MakeArgString("-L/opt/gcc4/lib/gcc/"
3070 + getToolChain().getTripleString()
Daniel Dunbarb0b18612009-10-29 02:24:37 +00003071 + "/4.2.4"));
Edward O'Callaghan856e4ff2009-08-22 01:06:46 +00003072
3073 Args.AddAllArgs(CmdArgs, options::OPT_L);
3074 Args.AddAllArgs(CmdArgs, options::OPT_T_Group);
3075 Args.AddAllArgs(CmdArgs, options::OPT_e);
3076
Daniel Dunbar54423b22010-09-17 00:24:54 +00003077 AddLinkerInputs(getToolChain(), Inputs, Args, CmdArgs);
Edward O'Callaghan856e4ff2009-08-22 01:06:46 +00003078
3079 if (!Args.hasArg(options::OPT_nostdlib) &&
3080 !Args.hasArg(options::OPT_nodefaultlibs)) {
3081 // FIXME: For some reason GCC passes -lgcc before adding
3082 // the default system libraries. Just mimic this for now.
3083 CmdArgs.push_back("-lgcc");
3084
3085 if (Args.hasArg(options::OPT_pthread))
3086 CmdArgs.push_back("-pthread");
3087 if (!Args.hasArg(options::OPT_shared))
3088 CmdArgs.push_back("-lc");
3089 CmdArgs.push_back("-lgcc");
3090 }
3091
3092 if (!Args.hasArg(options::OPT_nostdlib) &&
3093 !Args.hasArg(options::OPT_nostartfiles)) {
3094 if (!Args.hasArg(options::OPT_shared))
Chris Lattner3e2ee142010-07-07 16:01:42 +00003095 CmdArgs.push_back(Args.MakeArgString(
Daniel Dunbar9c3ed5f2010-07-14 18:46:23 +00003096 getToolChain().GetFilePath("crtend.o")));
Edward O'Callaghan856e4ff2009-08-22 01:06:46 +00003097 }
3098
3099 const char *Exec =
Daniel Dunbar9c3ed5f2010-07-14 18:46:23 +00003100 Args.MakeArgString(getToolChain().GetProgramPath("ld"));
Daniel Dunbar7fbaf532010-08-02 02:38:28 +00003101 C.addCommand(new Command(JA, *this, Exec, CmdArgs));
Edward O'Callaghan856e4ff2009-08-22 01:06:46 +00003102}
3103
Daniel Dunbar10de9e62009-06-29 20:52:51 +00003104void openbsd::Assemble::ConstructJob(Compilation &C, const JobAction &JA,
Daniel Dunbar7fbaf532010-08-02 02:38:28 +00003105 const InputInfo &Output,
Daniel Dunbar10de9e62009-06-29 20:52:51 +00003106 const InputInfoList &Inputs,
3107 const ArgList &Args,
Mike Stump11289f42009-09-09 15:08:12 +00003108 const char *LinkingOutput) const {
Daniel Dunbar10de9e62009-06-29 20:52:51 +00003109 ArgStringList CmdArgs;
3110
3111 Args.AddAllArgValues(CmdArgs, options::OPT_Wa_COMMA,
3112 options::OPT_Xassembler);
3113
3114 CmdArgs.push_back("-o");
Daniel Dunbarb440f562010-08-02 02:38:21 +00003115 CmdArgs.push_back(Output.getFilename());
Daniel Dunbar10de9e62009-06-29 20:52:51 +00003116
3117 for (InputInfoList::const_iterator
3118 it = Inputs.begin(), ie = Inputs.end(); it != ie; ++it) {
3119 const InputInfo &II = *it;
Daniel Dunbarb440f562010-08-02 02:38:21 +00003120 CmdArgs.push_back(II.getFilename());
Daniel Dunbar10de9e62009-06-29 20:52:51 +00003121 }
3122
3123 const char *Exec =
Daniel Dunbar9c3ed5f2010-07-14 18:46:23 +00003124 Args.MakeArgString(getToolChain().GetProgramPath("as"));
Daniel Dunbar7fbaf532010-08-02 02:38:28 +00003125 C.addCommand(new Command(JA, *this, Exec, CmdArgs));
Daniel Dunbar10de9e62009-06-29 20:52:51 +00003126}
3127
3128void openbsd::Link::ConstructJob(Compilation &C, const JobAction &JA,
Daniel Dunbar7fbaf532010-08-02 02:38:28 +00003129 const InputInfo &Output,
Daniel Dunbar10de9e62009-06-29 20:52:51 +00003130 const InputInfoList &Inputs,
3131 const ArgList &Args,
3132 const char *LinkingOutput) const {
Daniel Dunbar083edf72009-12-21 18:54:17 +00003133 const Driver &D = getToolChain().getDriver();
Daniel Dunbar10de9e62009-06-29 20:52:51 +00003134 ArgStringList CmdArgs;
3135
Daniel Dunbara8888ac2009-08-03 01:28:59 +00003136 if ((!Args.hasArg(options::OPT_nostdlib)) &&
Daniel Dunbar1c7577c2009-11-04 06:24:38 +00003137 (!Args.hasArg(options::OPT_shared))) {
Daniel Dunbara8888ac2009-08-03 01:28:59 +00003138 CmdArgs.push_back("-e");
3139 CmdArgs.push_back("__start");
3140 }
3141
Daniel Dunbar10de9e62009-06-29 20:52:51 +00003142 if (Args.hasArg(options::OPT_static)) {
3143 CmdArgs.push_back("-Bstatic");
3144 } else {
Rafael Espindola7ba97af2010-11-11 02:17:51 +00003145 if (Args.hasArg(options::OPT_rdynamic))
3146 CmdArgs.push_back("-export-dynamic");
Daniel Dunbar10de9e62009-06-29 20:52:51 +00003147 CmdArgs.push_back("--eh-frame-hdr");
Daniel Dunbara8888ac2009-08-03 01:28:59 +00003148 CmdArgs.push_back("-Bdynamic");
Daniel Dunbar10de9e62009-06-29 20:52:51 +00003149 if (Args.hasArg(options::OPT_shared)) {
Daniel Dunbara8888ac2009-08-03 01:28:59 +00003150 CmdArgs.push_back("-shared");
Daniel Dunbar10de9e62009-06-29 20:52:51 +00003151 } else {
3152 CmdArgs.push_back("-dynamic-linker");
3153 CmdArgs.push_back("/usr/libexec/ld.so");
3154 }
3155 }
3156
Daniel Dunbarb440f562010-08-02 02:38:21 +00003157 if (Output.isFilename()) {
Daniel Dunbar10de9e62009-06-29 20:52:51 +00003158 CmdArgs.push_back("-o");
3159 CmdArgs.push_back(Output.getFilename());
3160 } else {
3161 assert(Output.isNothing() && "Invalid output.");
3162 }
3163
3164 if (!Args.hasArg(options::OPT_nostdlib) &&
3165 !Args.hasArg(options::OPT_nostartfiles)) {
3166 if (!Args.hasArg(options::OPT_shared)) {
Chris Lattner3e2ee142010-07-07 16:01:42 +00003167 CmdArgs.push_back(Args.MakeArgString(
Daniel Dunbar9c3ed5f2010-07-14 18:46:23 +00003168 getToolChain().GetFilePath("crt0.o")));
Chris Lattner3e2ee142010-07-07 16:01:42 +00003169 CmdArgs.push_back(Args.MakeArgString(
Daniel Dunbar9c3ed5f2010-07-14 18:46:23 +00003170 getToolChain().GetFilePath("crtbegin.o")));
Daniel Dunbar10de9e62009-06-29 20:52:51 +00003171 } else {
Chris Lattner3e2ee142010-07-07 16:01:42 +00003172 CmdArgs.push_back(Args.MakeArgString(
Daniel Dunbar9c3ed5f2010-07-14 18:46:23 +00003173 getToolChain().GetFilePath("crtbeginS.o")));
Daniel Dunbar10de9e62009-06-29 20:52:51 +00003174 }
3175 }
3176
Edward O'Callaghan5c521462009-10-28 15:13:08 +00003177 std::string Triple = getToolChain().getTripleString();
3178 if (Triple.substr(0, 6) == "x86_64")
Daniel Dunbar1c7577c2009-11-04 06:24:38 +00003179 Triple.replace(0, 6, "amd64");
Daniel Dunbarb0b18612009-10-29 02:24:37 +00003180 CmdArgs.push_back(Args.MakeArgString("-L/usr/lib/gcc-lib/" + Triple +
Daniel Dunbarea3813f2010-08-01 23:13:54 +00003181 "/4.2.1"));
Daniel Dunbara8888ac2009-08-03 01:28:59 +00003182
Daniel Dunbar10de9e62009-06-29 20:52:51 +00003183 Args.AddAllArgs(CmdArgs, options::OPT_L);
3184 Args.AddAllArgs(CmdArgs, options::OPT_T_Group);
3185 Args.AddAllArgs(CmdArgs, options::OPT_e);
3186
Daniel Dunbar54423b22010-09-17 00:24:54 +00003187 AddLinkerInputs(getToolChain(), Inputs, Args, CmdArgs);
Daniel Dunbar10de9e62009-06-29 20:52:51 +00003188
3189 if (!Args.hasArg(options::OPT_nostdlib) &&
3190 !Args.hasArg(options::OPT_nodefaultlibs)) {
Daniel Dunbarea3813f2010-08-01 23:13:54 +00003191 if (D.CCCIsCXX) {
Daniel Dunbar3f7796f2010-09-17 01:20:05 +00003192 getToolChain().AddCXXStdlibLibArgs(Args, CmdArgs);
Daniel Dunbarea3813f2010-08-01 23:13:54 +00003193 CmdArgs.push_back("-lm");
3194 }
3195
Daniel Dunbara8888ac2009-08-03 01:28:59 +00003196 // FIXME: For some reason GCC passes -lgcc before adding
3197 // the default system libraries. Just mimic this for now.
3198 CmdArgs.push_back("-lgcc");
Daniel Dunbar10de9e62009-06-29 20:52:51 +00003199
3200 if (Args.hasArg(options::OPT_pthread))
Chris Lattnerd0257f72011-02-21 18:36:51 +00003201 CmdArgs.push_back("-lpthread");
Daniel Dunbara8888ac2009-08-03 01:28:59 +00003202 if (!Args.hasArg(options::OPT_shared))
3203 CmdArgs.push_back("-lc");
3204 CmdArgs.push_back("-lgcc");
Daniel Dunbar10de9e62009-06-29 20:52:51 +00003205 }
3206
3207 if (!Args.hasArg(options::OPT_nostdlib) &&
3208 !Args.hasArg(options::OPT_nostartfiles)) {
3209 if (!Args.hasArg(options::OPT_shared))
Chris Lattner3e2ee142010-07-07 16:01:42 +00003210 CmdArgs.push_back(Args.MakeArgString(
Daniel Dunbar9c3ed5f2010-07-14 18:46:23 +00003211 getToolChain().GetFilePath("crtend.o")));
Daniel Dunbar10de9e62009-06-29 20:52:51 +00003212 else
Chris Lattner3e2ee142010-07-07 16:01:42 +00003213 CmdArgs.push_back(Args.MakeArgString(
Daniel Dunbar9c3ed5f2010-07-14 18:46:23 +00003214 getToolChain().GetFilePath("crtendS.o")));
Daniel Dunbar10de9e62009-06-29 20:52:51 +00003215 }
3216
3217 const char *Exec =
Daniel Dunbar9c3ed5f2010-07-14 18:46:23 +00003218 Args.MakeArgString(getToolChain().GetProgramPath("ld"));
Daniel Dunbar7fbaf532010-08-02 02:38:28 +00003219 C.addCommand(new Command(JA, *this, Exec, CmdArgs));
Daniel Dunbar10de9e62009-06-29 20:52:51 +00003220}
Ed Schoutene33194b2009-04-02 19:13:12 +00003221
Daniel Dunbar8eb473c2009-03-31 17:45:15 +00003222void freebsd::Assemble::ConstructJob(Compilation &C, const JobAction &JA,
Daniel Dunbar7fbaf532010-08-02 02:38:28 +00003223 const InputInfo &Output,
Daniel Dunbard067f7f2009-04-08 23:54:23 +00003224 const InputInfoList &Inputs,
3225 const ArgList &Args,
Mike Stump11289f42009-09-09 15:08:12 +00003226 const char *LinkingOutput) const {
Daniel Dunbar8eb473c2009-03-31 17:45:15 +00003227 ArgStringList CmdArgs;
3228
Daniel Dunbard854c8d2009-04-01 19:36:32 +00003229 // When building 32-bit code on FreeBSD/amd64, we have to explicitly
3230 // instruct as in the base system to assemble 32-bit code.
3231 if (getToolChain().getArchName() == "i386")
Daniel Dunbar8eb473c2009-03-31 17:45:15 +00003232 CmdArgs.push_back("--32");
3233
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00003234
Eric Christopher0b26a612010-03-02 02:41:08 +00003235 // Set byte order explicitly
3236 if (getToolChain().getArchName() == "mips")
3237 CmdArgs.push_back("-EB");
3238 else if (getToolChain().getArchName() == "mipsel")
3239 CmdArgs.push_back("-EL");
3240
Daniel Dunbar8eb473c2009-03-31 17:45:15 +00003241 Args.AddAllArgValues(CmdArgs, options::OPT_Wa_COMMA,
3242 options::OPT_Xassembler);
3243
3244 CmdArgs.push_back("-o");
Daniel Dunbarb440f562010-08-02 02:38:21 +00003245 CmdArgs.push_back(Output.getFilename());
Daniel Dunbar8eb473c2009-03-31 17:45:15 +00003246
3247 for (InputInfoList::const_iterator
3248 it = Inputs.begin(), ie = Inputs.end(); it != ie; ++it) {
3249 const InputInfo &II = *it;
Daniel Dunbarb440f562010-08-02 02:38:21 +00003250 CmdArgs.push_back(II.getFilename());
Daniel Dunbar8eb473c2009-03-31 17:45:15 +00003251 }
3252
Daniel Dunbard067f7f2009-04-08 23:54:23 +00003253 const char *Exec =
Daniel Dunbar9c3ed5f2010-07-14 18:46:23 +00003254 Args.MakeArgString(getToolChain().GetProgramPath("as"));
Daniel Dunbar7fbaf532010-08-02 02:38:28 +00003255 C.addCommand(new Command(JA, *this, Exec, CmdArgs));
Daniel Dunbar8eb473c2009-03-31 17:45:15 +00003256}
Daniel Dunbard854c8d2009-04-01 19:36:32 +00003257
3258void freebsd::Link::ConstructJob(Compilation &C, const JobAction &JA,
Daniel Dunbar7fbaf532010-08-02 02:38:28 +00003259 const InputInfo &Output,
Daniel Dunbard067f7f2009-04-08 23:54:23 +00003260 const InputInfoList &Inputs,
3261 const ArgList &Args,
Daniel Dunbare3e263f2009-05-02 20:14:53 +00003262 const char *LinkingOutput) const {
Daniel Dunbar083edf72009-12-21 18:54:17 +00003263 const Driver &D = getToolChain().getDriver();
Daniel Dunbard854c8d2009-04-01 19:36:32 +00003264 ArgStringList CmdArgs;
3265
Joerg Sonnenberger6165ab12011-03-21 13:51:29 +00003266 if (!D.SysRoot.empty())
3267 CmdArgs.push_back(Args.MakeArgString("--sysroot=" + D.SysRoot));
3268
Daniel Dunbard854c8d2009-04-01 19:36:32 +00003269 if (Args.hasArg(options::OPT_static)) {
3270 CmdArgs.push_back("-Bstatic");
3271 } else {
Rafael Espindola7ba97af2010-11-11 02:17:51 +00003272 if (Args.hasArg(options::OPT_rdynamic))
3273 CmdArgs.push_back("-export-dynamic");
Daniel Dunbard854c8d2009-04-01 19:36:32 +00003274 CmdArgs.push_back("--eh-frame-hdr");
3275 if (Args.hasArg(options::OPT_shared)) {
3276 CmdArgs.push_back("-Bshareable");
3277 } else {
3278 CmdArgs.push_back("-dynamic-linker");
3279 CmdArgs.push_back("/libexec/ld-elf.so.1");
3280 }
3281 }
3282
3283 // When building 32-bit code on FreeBSD/amd64, we have to explicitly
3284 // instruct ld in the base system to link 32-bit code.
3285 if (getToolChain().getArchName() == "i386") {
3286 CmdArgs.push_back("-m");
3287 CmdArgs.push_back("elf_i386_fbsd");
3288 }
3289
Daniel Dunbarb440f562010-08-02 02:38:21 +00003290 if (Output.isFilename()) {
Daniel Dunbard854c8d2009-04-01 19:36:32 +00003291 CmdArgs.push_back("-o");
3292 CmdArgs.push_back(Output.getFilename());
3293 } else {
3294 assert(Output.isNothing() && "Invalid output.");
3295 }
3296
3297 if (!Args.hasArg(options::OPT_nostdlib) &&
3298 !Args.hasArg(options::OPT_nostartfiles)) {
3299 if (!Args.hasArg(options::OPT_shared)) {
Roman Divacky66f22762011-02-10 16:59:40 +00003300 if (Args.hasArg(options::OPT_pg))
3301 CmdArgs.push_back(Args.MakeArgString(
3302 getToolChain().GetFilePath("gcrt1.o")));
3303 else
3304 CmdArgs.push_back(Args.MakeArgString(
3305 getToolChain().GetFilePath("crt1.o")));
Chris Lattner3e2ee142010-07-07 16:01:42 +00003306 CmdArgs.push_back(Args.MakeArgString(
Daniel Dunbar9c3ed5f2010-07-14 18:46:23 +00003307 getToolChain().GetFilePath("crti.o")));
Chris Lattner3e2ee142010-07-07 16:01:42 +00003308 CmdArgs.push_back(Args.MakeArgString(
Daniel Dunbar9c3ed5f2010-07-14 18:46:23 +00003309 getToolChain().GetFilePath("crtbegin.o")));
Daniel Dunbard854c8d2009-04-01 19:36:32 +00003310 } else {
Chris Lattner3e2ee142010-07-07 16:01:42 +00003311 CmdArgs.push_back(Args.MakeArgString(
Daniel Dunbar9c3ed5f2010-07-14 18:46:23 +00003312 getToolChain().GetFilePath("crti.o")));
Chris Lattner3e2ee142010-07-07 16:01:42 +00003313 CmdArgs.push_back(Args.MakeArgString(
Daniel Dunbar9c3ed5f2010-07-14 18:46:23 +00003314 getToolChain().GetFilePath("crtbeginS.o")));
Daniel Dunbard854c8d2009-04-01 19:36:32 +00003315 }
3316 }
3317
3318 Args.AddAllArgs(CmdArgs, options::OPT_L);
Roman Divackyee8188a2011-03-01 17:53:14 +00003319 const ToolChain::path_list Paths = getToolChain().getFilePaths();
3320 for (ToolChain::path_list::const_iterator i = Paths.begin(), e = Paths.end();
3321 i != e; ++i)
3322 CmdArgs.push_back(Args.MakeArgString(llvm::StringRef("-L") + *i));
Daniel Dunbard854c8d2009-04-01 19:36:32 +00003323 Args.AddAllArgs(CmdArgs, options::OPT_T_Group);
3324 Args.AddAllArgs(CmdArgs, options::OPT_e);
David Chisnall589a4942010-08-15 22:58:12 +00003325 Args.AddAllArgs(CmdArgs, options::OPT_s);
3326 Args.AddAllArgs(CmdArgs, options::OPT_t);
3327 Args.AddAllArgs(CmdArgs, options::OPT_Z_Flag);
3328 Args.AddAllArgs(CmdArgs, options::OPT_r);
Daniel Dunbard854c8d2009-04-01 19:36:32 +00003329
Daniel Dunbar54423b22010-09-17 00:24:54 +00003330 AddLinkerInputs(getToolChain(), Inputs, Args, CmdArgs);
Daniel Dunbard854c8d2009-04-01 19:36:32 +00003331
3332 if (!Args.hasArg(options::OPT_nostdlib) &&
3333 !Args.hasArg(options::OPT_nodefaultlibs)) {
Daniel Dunbar4b8ef282010-02-17 08:07:51 +00003334 if (D.CCCIsCXX) {
Daniel Dunbar3f7796f2010-09-17 01:20:05 +00003335 getToolChain().AddCXXStdlibLibArgs(Args, CmdArgs);
Roman Divacky66f22762011-02-10 16:59:40 +00003336 if (Args.hasArg(options::OPT_pg))
3337 CmdArgs.push_back("-lm_p");
3338 else
3339 CmdArgs.push_back("-lm");
Daniel Dunbar4b8ef282010-02-17 08:07:51 +00003340 }
Daniel Dunbard854c8d2009-04-01 19:36:32 +00003341 // FIXME: For some reason GCC passes -lgcc and -lgcc_s before adding
3342 // the default system libraries. Just mimic this for now.
Roman Divacky66f22762011-02-10 16:59:40 +00003343 if (Args.hasArg(options::OPT_pg))
3344 CmdArgs.push_back("-lgcc_p");
3345 else
3346 CmdArgs.push_back("-lgcc");
Daniel Dunbard854c8d2009-04-01 19:36:32 +00003347 if (Args.hasArg(options::OPT_static)) {
3348 CmdArgs.push_back("-lgcc_eh");
Roman Divacky66f22762011-02-10 16:59:40 +00003349 } else if (Args.hasArg(options::OPT_pg)) {
3350 CmdArgs.push_back("-lgcc_eh_p");
Daniel Dunbard854c8d2009-04-01 19:36:32 +00003351 } else {
3352 CmdArgs.push_back("--as-needed");
3353 CmdArgs.push_back("-lgcc_s");
3354 CmdArgs.push_back("--no-as-needed");
3355 }
3356
Matt Beaumont-Gay1fe49152011-02-10 20:35:01 +00003357 if (Args.hasArg(options::OPT_pthread)) {
Roman Divacky66f22762011-02-10 16:59:40 +00003358 if (Args.hasArg(options::OPT_pg))
3359 CmdArgs.push_back("-lpthread_p");
3360 else
3361 CmdArgs.push_back("-lpthread");
Matt Beaumont-Gay1fe49152011-02-10 20:35:01 +00003362 }
Daniel Dunbard854c8d2009-04-01 19:36:32 +00003363
Roman Divacky66f22762011-02-10 16:59:40 +00003364 if (Args.hasArg(options::OPT_pg)) {
3365 if (Args.hasArg(options::OPT_shared))
3366 CmdArgs.push_back("-lc");
3367 else
3368 CmdArgs.push_back("-lc_p");
3369 CmdArgs.push_back("-lgcc_p");
3370 } else {
3371 CmdArgs.push_back("-lc");
3372 CmdArgs.push_back("-lgcc");
3373 }
3374
Daniel Dunbard854c8d2009-04-01 19:36:32 +00003375 if (Args.hasArg(options::OPT_static)) {
3376 CmdArgs.push_back("-lgcc_eh");
Roman Divacky66f22762011-02-10 16:59:40 +00003377 } else if (Args.hasArg(options::OPT_pg)) {
3378 CmdArgs.push_back("-lgcc_eh_p");
Daniel Dunbard854c8d2009-04-01 19:36:32 +00003379 } else {
3380 CmdArgs.push_back("--as-needed");
3381 CmdArgs.push_back("-lgcc_s");
3382 CmdArgs.push_back("--no-as-needed");
3383 }
3384 }
3385
3386 if (!Args.hasArg(options::OPT_nostdlib) &&
3387 !Args.hasArg(options::OPT_nostartfiles)) {
3388 if (!Args.hasArg(options::OPT_shared))
Daniel Dunbar9c3ed5f2010-07-14 18:46:23 +00003389 CmdArgs.push_back(Args.MakeArgString(getToolChain().GetFilePath(
Chris Lattner3e2ee142010-07-07 16:01:42 +00003390 "crtend.o")));
Daniel Dunbard854c8d2009-04-01 19:36:32 +00003391 else
Daniel Dunbar9c3ed5f2010-07-14 18:46:23 +00003392 CmdArgs.push_back(Args.MakeArgString(getToolChain().GetFilePath(
Chris Lattner3e2ee142010-07-07 16:01:42 +00003393 "crtendS.o")));
Daniel Dunbar9c3ed5f2010-07-14 18:46:23 +00003394 CmdArgs.push_back(Args.MakeArgString(getToolChain().GetFilePath(
Chris Lattner3e2ee142010-07-07 16:01:42 +00003395 "crtn.o")));
Daniel Dunbard854c8d2009-04-01 19:36:32 +00003396 }
3397
Daniel Dunbard067f7f2009-04-08 23:54:23 +00003398 const char *Exec =
Daniel Dunbar9c3ed5f2010-07-14 18:46:23 +00003399 Args.MakeArgString(getToolChain().GetProgramPath("ld"));
Daniel Dunbar7fbaf532010-08-02 02:38:28 +00003400 C.addCommand(new Command(JA, *this, Exec, CmdArgs));
Daniel Dunbard854c8d2009-04-01 19:36:32 +00003401}
Daniel Dunbarcc912342009-05-02 18:28:39 +00003402
Benjamin Kramer24f1d3e2011-02-02 18:59:27 +00003403void netbsd::Assemble::ConstructJob(Compilation &C, const JobAction &JA,
3404 const InputInfo &Output,
3405 const InputInfoList &Inputs,
3406 const ArgList &Args,
3407 const char *LinkingOutput) const {
3408 ArgStringList CmdArgs;
3409
3410 // When building 32-bit code on NetBSD/amd64, we have to explicitly
3411 // instruct as in the base system to assemble 32-bit code.
3412 if (getToolChain().getArchName() == "i386")
3413 CmdArgs.push_back("--32");
3414
3415
3416 // Set byte order explicitly
3417 if (getToolChain().getArchName() == "mips")
3418 CmdArgs.push_back("-EB");
3419 else if (getToolChain().getArchName() == "mipsel")
3420 CmdArgs.push_back("-EL");
3421
3422 Args.AddAllArgValues(CmdArgs, options::OPT_Wa_COMMA,
3423 options::OPT_Xassembler);
3424
3425 CmdArgs.push_back("-o");
3426 CmdArgs.push_back(Output.getFilename());
3427
3428 for (InputInfoList::const_iterator
3429 it = Inputs.begin(), ie = Inputs.end(); it != ie; ++it) {
3430 const InputInfo &II = *it;
3431 CmdArgs.push_back(II.getFilename());
3432 }
3433
Joerg Sonnenbergerfcc3ec92011-03-21 14:01:40 +00003434 const char *Exec = Args.MakeArgString(FindTargetProgramPath(getToolChain(),
3435 "as"));
Benjamin Kramer24f1d3e2011-02-02 18:59:27 +00003436 C.addCommand(new Command(JA, *this, Exec, CmdArgs));
3437}
3438
3439void netbsd::Link::ConstructJob(Compilation &C, const JobAction &JA,
3440 const InputInfo &Output,
3441 const InputInfoList &Inputs,
3442 const ArgList &Args,
3443 const char *LinkingOutput) const {
3444 const Driver &D = getToolChain().getDriver();
3445 ArgStringList CmdArgs;
3446
Joerg Sonnenberger6165ab12011-03-21 13:51:29 +00003447 if (!D.SysRoot.empty())
3448 CmdArgs.push_back(Args.MakeArgString("--sysroot=" + D.SysRoot));
3449
Benjamin Kramer24f1d3e2011-02-02 18:59:27 +00003450 if (Args.hasArg(options::OPT_static)) {
3451 CmdArgs.push_back("-Bstatic");
3452 } else {
3453 if (Args.hasArg(options::OPT_rdynamic))
3454 CmdArgs.push_back("-export-dynamic");
3455 CmdArgs.push_back("--eh-frame-hdr");
3456 if (Args.hasArg(options::OPT_shared)) {
3457 CmdArgs.push_back("-Bshareable");
3458 } else {
3459 CmdArgs.push_back("-dynamic-linker");
3460 CmdArgs.push_back("/libexec/ld.elf_so");
3461 }
3462 }
3463
3464 // When building 32-bit code on NetBSD/amd64, we have to explicitly
3465 // instruct ld in the base system to link 32-bit code.
3466 if (getToolChain().getArchName() == "i386") {
3467 CmdArgs.push_back("-m");
3468 CmdArgs.push_back("elf_i386");
3469 }
3470
3471 if (Output.isFilename()) {
3472 CmdArgs.push_back("-o");
3473 CmdArgs.push_back(Output.getFilename());
3474 } else {
3475 assert(Output.isNothing() && "Invalid output.");
3476 }
3477
3478 if (!Args.hasArg(options::OPT_nostdlib) &&
3479 !Args.hasArg(options::OPT_nostartfiles)) {
3480 if (!Args.hasArg(options::OPT_shared)) {
3481 CmdArgs.push_back(Args.MakeArgString(
3482 getToolChain().GetFilePath("crt0.o")));
3483 CmdArgs.push_back(Args.MakeArgString(
3484 getToolChain().GetFilePath("crti.o")));
3485 CmdArgs.push_back(Args.MakeArgString(
3486 getToolChain().GetFilePath("crtbegin.o")));
3487 } else {
3488 CmdArgs.push_back(Args.MakeArgString(
3489 getToolChain().GetFilePath("crti.o")));
3490 CmdArgs.push_back(Args.MakeArgString(
3491 getToolChain().GetFilePath("crtbeginS.o")));
3492 }
3493 }
3494
3495 Args.AddAllArgs(CmdArgs, options::OPT_L);
3496 Args.AddAllArgs(CmdArgs, options::OPT_T_Group);
3497 Args.AddAllArgs(CmdArgs, options::OPT_e);
3498 Args.AddAllArgs(CmdArgs, options::OPT_s);
3499 Args.AddAllArgs(CmdArgs, options::OPT_t);
3500 Args.AddAllArgs(CmdArgs, options::OPT_Z_Flag);
3501 Args.AddAllArgs(CmdArgs, options::OPT_r);
3502
3503 AddLinkerInputs(getToolChain(), Inputs, Args, CmdArgs);
3504
3505 if (!Args.hasArg(options::OPT_nostdlib) &&
3506 !Args.hasArg(options::OPT_nodefaultlibs)) {
3507 if (D.CCCIsCXX) {
3508 getToolChain().AddCXXStdlibLibArgs(Args, CmdArgs);
3509 CmdArgs.push_back("-lm");
3510 }
3511 // FIXME: For some reason GCC passes -lgcc and -lgcc_s before adding
3512 // the default system libraries. Just mimic this for now.
3513 CmdArgs.push_back("-lgcc");
3514 if (Args.hasArg(options::OPT_static)) {
3515 CmdArgs.push_back("-lgcc_eh");
3516 } else {
3517 CmdArgs.push_back("--as-needed");
3518 CmdArgs.push_back("-lgcc_s");
3519 CmdArgs.push_back("--no-as-needed");
3520 }
3521
3522 if (Args.hasArg(options::OPT_pthread))
3523 CmdArgs.push_back("-lpthread");
3524 CmdArgs.push_back("-lc");
3525
3526 CmdArgs.push_back("-lgcc");
3527 if (Args.hasArg(options::OPT_static)) {
3528 CmdArgs.push_back("-lgcc_eh");
3529 } else {
3530 CmdArgs.push_back("--as-needed");
3531 CmdArgs.push_back("-lgcc_s");
3532 CmdArgs.push_back("--no-as-needed");
3533 }
3534 }
3535
3536 if (!Args.hasArg(options::OPT_nostdlib) &&
3537 !Args.hasArg(options::OPT_nostartfiles)) {
3538 if (!Args.hasArg(options::OPT_shared))
3539 CmdArgs.push_back(Args.MakeArgString(getToolChain().GetFilePath(
3540 "crtend.o")));
3541 else
3542 CmdArgs.push_back(Args.MakeArgString(getToolChain().GetFilePath(
3543 "crtendS.o")));
3544 CmdArgs.push_back(Args.MakeArgString(getToolChain().GetFilePath(
3545 "crtn.o")));
3546 }
3547
Joerg Sonnenbergerfcc3ec92011-03-21 14:01:40 +00003548 const char *Exec = Args.MakeArgString(FindTargetProgramPath(getToolChain(),
3549 "ld"));
Benjamin Kramer24f1d3e2011-02-02 18:59:27 +00003550 C.addCommand(new Command(JA, *this, Exec, CmdArgs));
3551}
3552
Rafael Espindola92b00932010-08-10 00:25:48 +00003553void linuxtools::Assemble::ConstructJob(Compilation &C, const JobAction &JA,
3554 const InputInfo &Output,
3555 const InputInfoList &Inputs,
3556 const ArgList &Args,
3557 const char *LinkingOutput) const {
3558 ArgStringList CmdArgs;
3559
3560 // Add --32/--64 to make sure we get the format we want.
3561 // This is incomplete
3562 if (getToolChain().getArch() == llvm::Triple::x86) {
3563 CmdArgs.push_back("--32");
3564 } else if (getToolChain().getArch() == llvm::Triple::x86_64) {
3565 CmdArgs.push_back("--64");
3566 } else if (getToolChain().getArch() == llvm::Triple::arm) {
3567 llvm::StringRef MArch = getToolChain().getArchName();
3568 if (MArch == "armv7" || MArch == "armv7a" || MArch == "armv7-a")
3569 CmdArgs.push_back("-mfpu=neon");
3570 }
3571
3572 Args.AddAllArgValues(CmdArgs, options::OPT_Wa_COMMA,
3573 options::OPT_Xassembler);
3574
3575 CmdArgs.push_back("-o");
3576 CmdArgs.push_back(Output.getFilename());
3577
3578 for (InputInfoList::const_iterator
3579 it = Inputs.begin(), ie = Inputs.end(); it != ie; ++it) {
3580 const InputInfo &II = *it;
3581 CmdArgs.push_back(II.getFilename());
3582 }
3583
3584 const char *Exec =
3585 Args.MakeArgString(getToolChain().GetProgramPath("as"));
3586 C.addCommand(new Command(JA, *this, Exec, CmdArgs));
3587}
3588
Rafael Espindolac8f008f2010-11-07 20:14:31 +00003589void linuxtools::Link::ConstructJob(Compilation &C, const JobAction &JA,
3590 const InputInfo &Output,
3591 const InputInfoList &Inputs,
3592 const ArgList &Args,
3593 const char *LinkingOutput) const {
3594 const toolchains::Linux& ToolChain =
3595 static_cast<const toolchains::Linux&>(getToolChain());
3596 const Driver &D = ToolChain.getDriver();
3597 ArgStringList CmdArgs;
3598
Rafael Espindolad1002f62010-11-15 18:28:16 +00003599 // Silence warning for "clang -g foo.o -o foo"
3600 Args.ClaimAllArgs(options::OPT_g_Group);
Rafael Espindolad95a8122011-03-01 05:25:27 +00003601 // and "clang -emit-llvm foo.o -o foo"
3602 Args.ClaimAllArgs(options::OPT_emit_llvm);
Rafael Espindolaf92614c2010-11-17 20:37:10 +00003603 // and for "clang -g foo.o -o foo". Other warning options are already
3604 // handled somewhere else.
3605 Args.ClaimAllArgs(options::OPT_w);
Rafael Espindolad1002f62010-11-15 18:28:16 +00003606
Joerg Sonnenberger6165ab12011-03-21 13:51:29 +00003607 if (!D.SysRoot.empty())
3608 CmdArgs.push_back(Args.MakeArgString("--sysroot=" + D.SysRoot));
Rafael Espindolac8f008f2010-11-07 20:14:31 +00003609
Rafael Espindolad47ac232010-11-17 22:26:15 +00003610 if (Args.hasArg(options::OPT_pie))
3611 CmdArgs.push_back("-pie");
3612
Rafael Espindola1c76c592010-11-07 22:57:16 +00003613 if (Args.hasArg(options::OPT_rdynamic))
3614 CmdArgs.push_back("-export-dynamic");
3615
Rafael Espindola34d77dc2010-11-11 19:34:42 +00003616 if (Args.hasArg(options::OPT_s))
3617 CmdArgs.push_back("-s");
3618
Rafael Espindolac8f008f2010-11-07 20:14:31 +00003619 for (std::vector<std::string>::const_iterator i = ToolChain.ExtraOpts.begin(),
3620 e = ToolChain.ExtraOpts.end();
3621 i != e; ++i)
3622 CmdArgs.push_back(i->c_str());
3623
3624 if (!Args.hasArg(options::OPT_static)) {
3625 CmdArgs.push_back("--eh-frame-hdr");
3626 }
3627
3628 CmdArgs.push_back("-m");
3629 if (ToolChain.getArch() == llvm::Triple::x86)
3630 CmdArgs.push_back("elf_i386");
Douglas Gregord9bb1522011-03-06 19:11:49 +00003631 else if (ToolChain.getArch() == llvm::Triple::arm
3632 || ToolChain.getArch() == llvm::Triple::thumb)
Rafael Espindolac8f008f2010-11-07 20:14:31 +00003633 CmdArgs.push_back("armelf_linux_eabi");
3634 else
3635 CmdArgs.push_back("elf_x86_64");
3636
3637 if (Args.hasArg(options::OPT_static)) {
Douglas Gregord9bb1522011-03-06 19:11:49 +00003638 if (ToolChain.getArch() == llvm::Triple::arm
3639 || ToolChain.getArch() == llvm::Triple::thumb)
Rafael Espindolac8f008f2010-11-07 20:14:31 +00003640 CmdArgs.push_back("-Bstatic");
3641 else
3642 CmdArgs.push_back("-static");
3643 } else if (Args.hasArg(options::OPT_shared)) {
3644 CmdArgs.push_back("-shared");
3645 }
3646
3647 if (ToolChain.getArch() == llvm::Triple::arm ||
Douglas Gregord9bb1522011-03-06 19:11:49 +00003648 ToolChain.getArch() == llvm::Triple::thumb ||
Rafael Espindolac8f008f2010-11-07 20:14:31 +00003649 (!Args.hasArg(options::OPT_static) &&
3650 !Args.hasArg(options::OPT_shared))) {
3651 CmdArgs.push_back("-dynamic-linker");
3652 if (ToolChain.getArch() == llvm::Triple::x86)
3653 CmdArgs.push_back("/lib/ld-linux.so.2");
Douglas Gregord9bb1522011-03-06 19:11:49 +00003654 else if (ToolChain.getArch() == llvm::Triple::arm ||
3655 ToolChain.getArch() == llvm::Triple::thumb)
Rafael Espindolac8f008f2010-11-07 20:14:31 +00003656 CmdArgs.push_back("/lib/ld-linux.so.3");
3657 else
3658 CmdArgs.push_back("/lib64/ld-linux-x86-64.so.2");
3659 }
3660
3661 CmdArgs.push_back("-o");
3662 CmdArgs.push_back(Output.getFilename());
3663
Rafael Espindola81937ec2010-12-01 01:52:43 +00003664 if (!Args.hasArg(options::OPT_nostdlib) &&
3665 !Args.hasArg(options::OPT_nostartfiles)) {
Rafael Espindolad47ac232010-11-17 22:26:15 +00003666 const char *crt1 = NULL;
3667 if (!Args.hasArg(options::OPT_shared)){
3668 if (Args.hasArg(options::OPT_pie))
3669 crt1 = "Scrt1.o";
3670 else
3671 crt1 = "crt1.o";
3672 }
3673 if (crt1)
3674 CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath(crt1)));
Rafael Espindolac8f008f2010-11-07 20:14:31 +00003675
Rafael Espindola9aa60e92010-11-12 03:00:39 +00003676 CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath("crti.o")));
Rafael Espindolac8f008f2010-11-07 20:14:31 +00003677
Rafael Espindola9aa60e92010-11-12 03:00:39 +00003678 const char *crtbegin;
3679 if (Args.hasArg(options::OPT_static))
3680 crtbegin = "crtbeginT.o";
Rafael Espindolad47ac232010-11-17 22:26:15 +00003681 else if (Args.hasArg(options::OPT_shared) || Args.hasArg(options::OPT_pie))
Rafael Espindola9aa60e92010-11-12 03:00:39 +00003682 crtbegin = "crtbeginS.o";
3683 else
3684 crtbegin = "crtbegin.o";
3685 CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath(crtbegin)));
3686 }
Rafael Espindolac8f008f2010-11-07 20:14:31 +00003687
3688 Args.AddAllArgs(CmdArgs, options::OPT_L);
3689
3690 const ToolChain::path_list Paths = ToolChain.getFilePaths();
3691
Roman Divackyee8188a2011-03-01 17:53:14 +00003692 for (ToolChain::path_list::const_iterator i = Paths.begin(), e = Paths.end();
3693 i != e; ++i)
3694 CmdArgs.push_back(Args.MakeArgString(llvm::StringRef("-L") + *i));
Rafael Espindolac8f008f2010-11-07 20:14:31 +00003695
3696 AddLinkerInputs(ToolChain, Inputs, Args, CmdArgs);
3697
Rafael Espindola9aa60e92010-11-12 03:00:39 +00003698 if (D.CCCIsCXX && !Args.hasArg(options::OPT_nostdlib)) {
Rafael Espindolac8f008f2010-11-07 20:14:31 +00003699 ToolChain.AddCXXStdlibLibArgs(Args, CmdArgs);
3700 CmdArgs.push_back("-lm");
3701 }
3702
3703 if (Args.hasArg(options::OPT_static))
3704 CmdArgs.push_back("--start-group");
3705
Rafael Espindola9aa60e92010-11-12 03:00:39 +00003706 if (!Args.hasArg(options::OPT_nostdlib)) {
3707 if (!D.CCCIsCXX)
Rafael Espindolac8f008f2010-11-07 20:14:31 +00003708 CmdArgs.push_back("-lgcc");
Rafael Espindola9aa60e92010-11-12 03:00:39 +00003709
3710 if (Args.hasArg(options::OPT_static)) {
3711 if (D.CCCIsCXX)
3712 CmdArgs.push_back("-lgcc");
3713 } else {
3714 if (!D.CCCIsCXX)
3715 CmdArgs.push_back("--as-needed");
3716 CmdArgs.push_back("-lgcc_s");
3717 if (!D.CCCIsCXX)
3718 CmdArgs.push_back("--no-as-needed");
3719 }
3720
3721 if (Args.hasArg(options::OPT_static))
3722 CmdArgs.push_back("-lgcc_eh");
3723 else if (!Args.hasArg(options::OPT_shared) && D.CCCIsCXX)
3724 CmdArgs.push_back("-lgcc");
3725
3726 if (Args.hasArg(options::OPT_pthread) ||
3727 Args.hasArg(options::OPT_pthreads))
3728 CmdArgs.push_back("-lpthread");
3729
3730 CmdArgs.push_back("-lc");
3731
3732 if (Args.hasArg(options::OPT_static))
3733 CmdArgs.push_back("--end-group");
3734 else {
3735 if (!D.CCCIsCXX)
3736 CmdArgs.push_back("-lgcc");
3737
3738 if (!D.CCCIsCXX)
3739 CmdArgs.push_back("--as-needed");
3740 CmdArgs.push_back("-lgcc_s");
3741 if (!D.CCCIsCXX)
3742 CmdArgs.push_back("--no-as-needed");
3743
3744 if (!Args.hasArg(options::OPT_shared) && D.CCCIsCXX)
3745 CmdArgs.push_back("-lgcc");
3746 }
3747
Rafael Espindolad47ac232010-11-17 22:26:15 +00003748
Rafael Espindola81937ec2010-12-01 01:52:43 +00003749 if (!Args.hasArg(options::OPT_nostartfiles)) {
3750 const char *crtend;
3751 if (Args.hasArg(options::OPT_shared) || Args.hasArg(options::OPT_pie))
3752 crtend = "crtendS.o";
3753 else
3754 crtend = "crtend.o";
Rafael Espindola9aa60e92010-11-12 03:00:39 +00003755
Rafael Espindola81937ec2010-12-01 01:52:43 +00003756 CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath(crtend)));
3757 CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath("crtn.o")));
3758 }
Rafael Espindolac8f008f2010-11-07 20:14:31 +00003759 }
3760
Rafael Espindolac8f008f2010-11-07 20:14:31 +00003761 if (Args.hasArg(options::OPT_use_gold_plugin)) {
3762 CmdArgs.push_back("-plugin");
3763 std::string Plugin = ToolChain.getDriver().Dir + "/../lib/LLVMgold.so";
3764 CmdArgs.push_back(Args.MakeArgString(Plugin));
3765 }
3766
3767 C.addCommand(new Command(JA, *this, ToolChain.Linker.c_str(), CmdArgs));
3768}
Rafael Espindola92b00932010-08-10 00:25:48 +00003769
Chris Lattner3e2ee142010-07-07 16:01:42 +00003770void minix::Assemble::ConstructJob(Compilation &C, const JobAction &JA,
Daniel Dunbar7fbaf532010-08-02 02:38:28 +00003771 const InputInfo &Output,
3772 const InputInfoList &Inputs,
3773 const ArgList &Args,
3774 const char *LinkingOutput) const {
Chris Lattner3e2ee142010-07-07 16:01:42 +00003775 ArgStringList CmdArgs;
3776
3777 Args.AddAllArgValues(CmdArgs, options::OPT_Wa_COMMA,
3778 options::OPT_Xassembler);
3779
3780 CmdArgs.push_back("-o");
Daniel Dunbarb440f562010-08-02 02:38:21 +00003781 CmdArgs.push_back(Output.getFilename());
Chris Lattner3e2ee142010-07-07 16:01:42 +00003782
3783 for (InputInfoList::const_iterator
3784 it = Inputs.begin(), ie = Inputs.end(); it != ie; ++it) {
3785 const InputInfo &II = *it;
Daniel Dunbarb440f562010-08-02 02:38:21 +00003786 CmdArgs.push_back(II.getFilename());
Chris Lattner3e2ee142010-07-07 16:01:42 +00003787 }
3788
3789 const char *Exec =
Daniel Dunbar9c3ed5f2010-07-14 18:46:23 +00003790 Args.MakeArgString(getToolChain().GetProgramPath("gas"));
Daniel Dunbar7fbaf532010-08-02 02:38:28 +00003791 C.addCommand(new Command(JA, *this, Exec, CmdArgs));
Chris Lattner3e2ee142010-07-07 16:01:42 +00003792}
3793
3794void minix::Link::ConstructJob(Compilation &C, const JobAction &JA,
Daniel Dunbar7fbaf532010-08-02 02:38:28 +00003795 const InputInfo &Output,
3796 const InputInfoList &Inputs,
3797 const ArgList &Args,
3798 const char *LinkingOutput) const {
Chris Lattner3e2ee142010-07-07 16:01:42 +00003799 const Driver &D = getToolChain().getDriver();
3800 ArgStringList CmdArgs;
3801
Daniel Dunbarb440f562010-08-02 02:38:21 +00003802 if (Output.isFilename()) {
Chris Lattner3e2ee142010-07-07 16:01:42 +00003803 CmdArgs.push_back("-o");
3804 CmdArgs.push_back(Output.getFilename());
3805 } else {
3806 assert(Output.isNothing() && "Invalid output.");
3807 }
3808
3809 if (!Args.hasArg(options::OPT_nostdlib) &&
3810 !Args.hasArg(options::OPT_nostartfiles))
Daniel Dunbar9c3ed5f2010-07-14 18:46:23 +00003811 CmdArgs.push_back(Args.MakeArgString(getToolChain().GetFilePath(
Chris Lattner3e2ee142010-07-07 16:01:42 +00003812 "/usr/gnu/lib/crtso.o")));
3813
3814 Args.AddAllArgs(CmdArgs, options::OPT_L);
3815 Args.AddAllArgs(CmdArgs, options::OPT_T_Group);
3816 Args.AddAllArgs(CmdArgs, options::OPT_e);
3817
Daniel Dunbar54423b22010-09-17 00:24:54 +00003818 AddLinkerInputs(getToolChain(), Inputs, Args, CmdArgs);
Chris Lattner3e2ee142010-07-07 16:01:42 +00003819
3820 if (!Args.hasArg(options::OPT_nostdlib) &&
3821 !Args.hasArg(options::OPT_nodefaultlibs)) {
3822 if (D.CCCIsCXX) {
Daniel Dunbar3f7796f2010-09-17 01:20:05 +00003823 getToolChain().AddCXXStdlibLibArgs(Args, CmdArgs);
Chris Lattner3e2ee142010-07-07 16:01:42 +00003824 CmdArgs.push_back("-lm");
3825 }
3826
3827 if (Args.hasArg(options::OPT_pthread))
3828 CmdArgs.push_back("-lpthread");
3829 CmdArgs.push_back("-lc");
3830 CmdArgs.push_back("-lgcc");
3831 CmdArgs.push_back("-L/usr/gnu/lib");
3832 // FIXME: fill in the correct search path for the final
3833 // support libraries.
3834 CmdArgs.push_back("-L/usr/gnu/lib/gcc/i686-pc-minix/4.4.3");
3835 }
3836
3837 if (!Args.hasArg(options::OPT_nostdlib) &&
3838 !Args.hasArg(options::OPT_nostartfiles)) {
Daniel Dunbar9c3ed5f2010-07-14 18:46:23 +00003839 CmdArgs.push_back(Args.MakeArgString(getToolChain().GetFilePath(
Chris Lattner3e2ee142010-07-07 16:01:42 +00003840 "/usr/gnu/lib/libend.a")));
3841 }
3842
3843 const char *Exec =
Daniel Dunbar9c3ed5f2010-07-14 18:46:23 +00003844 Args.MakeArgString(getToolChain().GetProgramPath("/usr/gnu/bin/gld"));
Daniel Dunbar7fbaf532010-08-02 02:38:28 +00003845 C.addCommand(new Command(JA, *this, Exec, CmdArgs));
Chris Lattner3e2ee142010-07-07 16:01:42 +00003846}
3847
Daniel Dunbarcc912342009-05-02 18:28:39 +00003848/// DragonFly Tools
3849
3850// For now, DragonFly Assemble does just about the same as for
3851// FreeBSD, but this may change soon.
3852void dragonfly::Assemble::ConstructJob(Compilation &C, const JobAction &JA,
Daniel Dunbar7fbaf532010-08-02 02:38:28 +00003853 const InputInfo &Output,
Daniel Dunbar1c7577c2009-11-04 06:24:38 +00003854 const InputInfoList &Inputs,
3855 const ArgList &Args,
3856 const char *LinkingOutput) const {
Daniel Dunbarcc912342009-05-02 18:28:39 +00003857 ArgStringList CmdArgs;
3858
3859 // When building 32-bit code on DragonFly/pc64, we have to explicitly
3860 // instruct as in the base system to assemble 32-bit code.
3861 if (getToolChain().getArchName() == "i386")
3862 CmdArgs.push_back("--32");
3863
3864 Args.AddAllArgValues(CmdArgs, options::OPT_Wa_COMMA,
3865 options::OPT_Xassembler);
3866
3867 CmdArgs.push_back("-o");
Daniel Dunbarb440f562010-08-02 02:38:21 +00003868 CmdArgs.push_back(Output.getFilename());
Daniel Dunbarcc912342009-05-02 18:28:39 +00003869
3870 for (InputInfoList::const_iterator
3871 it = Inputs.begin(), ie = Inputs.end(); it != ie; ++it) {
3872 const InputInfo &II = *it;
Daniel Dunbarb440f562010-08-02 02:38:21 +00003873 CmdArgs.push_back(II.getFilename());
Daniel Dunbarcc912342009-05-02 18:28:39 +00003874 }
3875
3876 const char *Exec =
Daniel Dunbar9c3ed5f2010-07-14 18:46:23 +00003877 Args.MakeArgString(getToolChain().GetProgramPath("as"));
Daniel Dunbar7fbaf532010-08-02 02:38:28 +00003878 C.addCommand(new Command(JA, *this, Exec, CmdArgs));
Daniel Dunbarcc912342009-05-02 18:28:39 +00003879}
3880
3881void dragonfly::Link::ConstructJob(Compilation &C, const JobAction &JA,
Daniel Dunbar7fbaf532010-08-02 02:38:28 +00003882 const InputInfo &Output,
3883 const InputInfoList &Inputs,
3884 const ArgList &Args,
3885 const char *LinkingOutput) const {
Daniel Dunbar083edf72009-12-21 18:54:17 +00003886 const Driver &D = getToolChain().getDriver();
Daniel Dunbarcc912342009-05-02 18:28:39 +00003887 ArgStringList CmdArgs;
3888
Joerg Sonnenberger6165ab12011-03-21 13:51:29 +00003889 if (!D.SysRoot.empty())
3890 CmdArgs.push_back(Args.MakeArgString("--sysroot=" + D.SysRoot));
3891
Daniel Dunbarcc912342009-05-02 18:28:39 +00003892 if (Args.hasArg(options::OPT_static)) {
3893 CmdArgs.push_back("-Bstatic");
3894 } else {
3895 if (Args.hasArg(options::OPT_shared))
3896 CmdArgs.push_back("-Bshareable");
3897 else {
3898 CmdArgs.push_back("-dynamic-linker");
3899 CmdArgs.push_back("/usr/libexec/ld-elf.so.2");
3900 }
3901 }
3902
3903 // When building 32-bit code on DragonFly/pc64, we have to explicitly
3904 // instruct ld in the base system to link 32-bit code.
3905 if (getToolChain().getArchName() == "i386") {
3906 CmdArgs.push_back("-m");
3907 CmdArgs.push_back("elf_i386");
3908 }
3909
Daniel Dunbarb440f562010-08-02 02:38:21 +00003910 if (Output.isFilename()) {
Daniel Dunbarcc912342009-05-02 18:28:39 +00003911 CmdArgs.push_back("-o");
3912 CmdArgs.push_back(Output.getFilename());
3913 } else {
3914 assert(Output.isNothing() && "Invalid output.");
3915 }
3916
3917 if (!Args.hasArg(options::OPT_nostdlib) &&
3918 !Args.hasArg(options::OPT_nostartfiles)) {
3919 if (!Args.hasArg(options::OPT_shared)) {
Chris Lattner3e2ee142010-07-07 16:01:42 +00003920 CmdArgs.push_back(
Daniel Dunbar9c3ed5f2010-07-14 18:46:23 +00003921 Args.MakeArgString(getToolChain().GetFilePath("crt1.o")));
Chris Lattner3e2ee142010-07-07 16:01:42 +00003922 CmdArgs.push_back(
Daniel Dunbar9c3ed5f2010-07-14 18:46:23 +00003923 Args.MakeArgString(getToolChain().GetFilePath("crti.o")));
Chris Lattner3e2ee142010-07-07 16:01:42 +00003924 CmdArgs.push_back(
Daniel Dunbar9c3ed5f2010-07-14 18:46:23 +00003925 Args.MakeArgString(getToolChain().GetFilePath("crtbegin.o")));
Daniel Dunbarcc912342009-05-02 18:28:39 +00003926 } else {
Chris Lattner3e2ee142010-07-07 16:01:42 +00003927 CmdArgs.push_back(
Daniel Dunbar9c3ed5f2010-07-14 18:46:23 +00003928 Args.MakeArgString(getToolChain().GetFilePath("crti.o")));
Chris Lattner3e2ee142010-07-07 16:01:42 +00003929 CmdArgs.push_back(
Daniel Dunbar9c3ed5f2010-07-14 18:46:23 +00003930 Args.MakeArgString(getToolChain().GetFilePath("crtbeginS.o")));
Daniel Dunbarcc912342009-05-02 18:28:39 +00003931 }
3932 }
3933
3934 Args.AddAllArgs(CmdArgs, options::OPT_L);
3935 Args.AddAllArgs(CmdArgs, options::OPT_T_Group);
3936 Args.AddAllArgs(CmdArgs, options::OPT_e);
3937
Daniel Dunbar54423b22010-09-17 00:24:54 +00003938 AddLinkerInputs(getToolChain(), Inputs, Args, CmdArgs);
Daniel Dunbarcc912342009-05-02 18:28:39 +00003939
3940 if (!Args.hasArg(options::OPT_nostdlib) &&
3941 !Args.hasArg(options::OPT_nodefaultlibs)) {
3942 // FIXME: GCC passes on -lgcc, -lgcc_pic and a whole lot of
3943 // rpaths
3944 CmdArgs.push_back("-L/usr/lib/gcc41");
3945
3946 if (!Args.hasArg(options::OPT_static)) {
3947 CmdArgs.push_back("-rpath");
3948 CmdArgs.push_back("/usr/lib/gcc41");
3949
3950 CmdArgs.push_back("-rpath-link");
3951 CmdArgs.push_back("/usr/lib/gcc41");
3952
3953 CmdArgs.push_back("-rpath");
3954 CmdArgs.push_back("/usr/lib");
3955
3956 CmdArgs.push_back("-rpath-link");
3957 CmdArgs.push_back("/usr/lib");
3958 }
3959
Rafael Espindola38360b32010-07-20 12:59:03 +00003960 if (D.CCCIsCXX) {
Daniel Dunbar3f7796f2010-09-17 01:20:05 +00003961 getToolChain().AddCXXStdlibLibArgs(Args, CmdArgs);
Rafael Espindola38360b32010-07-20 12:59:03 +00003962 CmdArgs.push_back("-lm");
3963 }
3964
Daniel Dunbarcc912342009-05-02 18:28:39 +00003965 if (Args.hasArg(options::OPT_shared)) {
3966 CmdArgs.push_back("-lgcc_pic");
3967 } else {
3968 CmdArgs.push_back("-lgcc");
3969 }
3970
3971
3972 if (Args.hasArg(options::OPT_pthread))
Mike Stump0a65b632009-10-31 20:11:46 +00003973 CmdArgs.push_back("-lpthread");
Daniel Dunbarcc912342009-05-02 18:28:39 +00003974
3975 if (!Args.hasArg(options::OPT_nolibc)) {
3976 CmdArgs.push_back("-lc");
3977 }
3978
3979 if (Args.hasArg(options::OPT_shared)) {
3980 CmdArgs.push_back("-lgcc_pic");
3981 } else {
3982 CmdArgs.push_back("-lgcc");
3983 }
3984 }
3985
3986 if (!Args.hasArg(options::OPT_nostdlib) &&
3987 !Args.hasArg(options::OPT_nostartfiles)) {
3988 if (!Args.hasArg(options::OPT_shared))
Chris Lattner3e2ee142010-07-07 16:01:42 +00003989 CmdArgs.push_back(Args.MakeArgString(
Daniel Dunbar9c3ed5f2010-07-14 18:46:23 +00003990 getToolChain().GetFilePath("crtend.o")));
Daniel Dunbarcc912342009-05-02 18:28:39 +00003991 else
Chris Lattner3e2ee142010-07-07 16:01:42 +00003992 CmdArgs.push_back(Args.MakeArgString(
Daniel Dunbar9c3ed5f2010-07-14 18:46:23 +00003993 getToolChain().GetFilePath("crtendS.o")));
Chris Lattner3e2ee142010-07-07 16:01:42 +00003994 CmdArgs.push_back(Args.MakeArgString(
Daniel Dunbar9c3ed5f2010-07-14 18:46:23 +00003995 getToolChain().GetFilePath("crtn.o")));
Daniel Dunbarcc912342009-05-02 18:28:39 +00003996 }
3997
3998 const char *Exec =
Daniel Dunbar9c3ed5f2010-07-14 18:46:23 +00003999 Args.MakeArgString(getToolChain().GetProgramPath("ld"));
Daniel Dunbar7fbaf532010-08-02 02:38:28 +00004000 C.addCommand(new Command(JA, *this, Exec, CmdArgs));
Daniel Dunbarcc912342009-05-02 18:28:39 +00004001}
Michael J. Spencerb186bc32010-08-21 21:55:07 +00004002
4003void visualstudio::Link::ConstructJob(Compilation &C, const JobAction &JA,
4004 const InputInfo &Output,
4005 const InputInfoList &Inputs,
4006 const ArgList &Args,
4007 const char *LinkingOutput) const {
Michael J. Spencerb186bc32010-08-21 21:55:07 +00004008 ArgStringList CmdArgs;
4009
4010 if (Output.isFilename()) {
Daniel Dunbar2cc3f172010-09-17 00:45:02 +00004011 CmdArgs.push_back(Args.MakeArgString(std::string("-out:") +
4012 Output.getFilename()));
Michael J. Spencerb186bc32010-08-21 21:55:07 +00004013 } else {
4014 assert(Output.isNothing() && "Invalid output.");
4015 }
4016
4017 if (!Args.hasArg(options::OPT_nostdlib) &&
4018 !Args.hasArg(options::OPT_nostartfiles)) {
4019 CmdArgs.push_back("-defaultlib:libcmt");
4020 }
4021
4022 CmdArgs.push_back("-nologo");
4023
Daniel Dunbar54423b22010-09-17 00:24:54 +00004024 AddLinkerInputs(getToolChain(), Inputs, Args, CmdArgs);
Michael J. Spencerb186bc32010-08-21 21:55:07 +00004025
4026 const char *Exec =
Daniel Dunbar54423b22010-09-17 00:24:54 +00004027 Args.MakeArgString(getToolChain().GetProgramPath("link.exe"));
Michael J. Spencerb186bc32010-08-21 21:55:07 +00004028 C.addCommand(new Command(JA, *this, Exec, CmdArgs));
4029}