blob: 81585c85674daf1f70780bf496b1d0e50acde99a [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
Chandler Carruth6e501032011-03-27 00:04:55 +00001451 // -fno-strict-overflow implies -fwrapv if it isn't disabled, but
1452 // -fstrict-overflow won't turn off an explicitly enabled -fwrapv.
1453 if (Arg *A = Args.getLastArg(options::OPT_fwrapv,
1454 options::OPT_fno_wrapv)) {
1455 if (A->getOption().matches(options::OPT_fwrapv))
1456 CmdArgs.push_back("-fwrapv");
1457 } else if (Arg *A = Args.getLastArg(options::OPT_fstrict_overflow,
1458 options::OPT_fno_strict_overflow)) {
1459 if (A->getOption().matches(options::OPT_fno_strict_overflow))
1460 CmdArgs.push_back("-fwrapv");
1461 }
Daniel Dunbar3a148f22009-04-07 21:51:40 +00001462 Args.AddLastArg(CmdArgs, options::OPT_fwritable_strings);
Eric Christopherf387dbd2010-08-07 23:08:14 +00001463 Args.AddLastArg(CmdArgs, options::OPT_funroll_loops);
Daniel Dunbara2aedc62009-03-18 10:01:51 +00001464
Daniel Dunbara77eaeb2009-09-03 04:54:28 +00001465 Args.AddLastArg(CmdArgs, options::OPT_pthread);
1466
Daniel Dunbar4930e332009-11-17 08:07:36 +00001467 // -stack-protector=0 is default.
1468 unsigned StackProtectorLevel = 0;
Bill Wendlingd63bbad2009-06-28 07:36:13 +00001469 if (Arg *A = Args.getLastArg(options::OPT_fno_stack_protector,
1470 options::OPT_fstack_protector_all,
1471 options::OPT_fstack_protector)) {
Daniel Dunbar4930e332009-11-17 08:07:36 +00001472 if (A->getOption().matches(options::OPT_fstack_protector))
1473 StackProtectorLevel = 1;
1474 else if (A->getOption().matches(options::OPT_fstack_protector_all))
1475 StackProtectorLevel = 2;
1476 } else
1477 StackProtectorLevel = getToolChain().GetDefaultStackProtectorLevel();
1478 if (StackProtectorLevel) {
1479 CmdArgs.push_back("-stack-protector");
1480 CmdArgs.push_back(Args.MakeArgString(llvm::Twine(StackProtectorLevel)));
Bill Wendlingd63bbad2009-06-28 07:36:13 +00001481 }
1482
Daniel Dunbard18049a2009-04-07 21:16:11 +00001483 // Forward -f options with positive and negative forms; we translate
1484 // these by hand.
1485
Fariborz Jahaniana4cfff82011-01-07 01:05:02 +00001486 if (Args.hasArg(options::OPT_mkernel)) {
Daniel Dunbar80f787c2011-02-04 17:24:47 +00001487 if (!Args.hasArg(options::OPT_fapple_kext) && types::isCXX(InputType))
Fariborz Jahaniana4cfff82011-01-07 01:05:02 +00001488 CmdArgs.push_back("-fapple-kext");
1489 if (!Args.hasArg(options::OPT_fbuiltin))
1490 CmdArgs.push_back("-fno-builtin");
1491 }
Daniel Dunbar4930e332009-11-17 08:07:36 +00001492 // -fbuiltin is default.
Fariborz Jahaniana4cfff82011-01-07 01:05:02 +00001493 else if (!Args.hasFlag(options::OPT_fbuiltin, options::OPT_fno_builtin))
Daniel Dunbar484afa22009-11-19 04:55:23 +00001494 CmdArgs.push_back("-fno-builtin");
Daniel Dunbard18049a2009-04-07 21:16:11 +00001495
Nuno Lopes13c88c72009-12-16 16:59:22 +00001496 if (!Args.hasFlag(options::OPT_fassume_sane_operator_new,
1497 options::OPT_fno_assume_sane_operator_new))
1498 CmdArgs.push_back("-fno-assume-sane-operator-new");
1499
Daniel Dunbar4930e332009-11-17 08:07:36 +00001500 // -fblocks=0 is default.
1501 if (Args.hasFlag(options::OPT_fblocks, options::OPT_fno_blocks,
David Chisnallda209912011-02-28 17:11:43 +00001502 getToolChain().IsBlocksDefault()) ||
1503 (Args.hasArg(options::OPT_fgnu_runtime) &&
1504 Args.hasArg(options::OPT_fobjc_nonfragile_abi) &&
1505 !Args.hasArg(options::OPT_fno_blocks))) {
Daniel Dunbar4930e332009-11-17 08:07:36 +00001506 CmdArgs.push_back("-fblocks");
David Chisnall950a9512009-11-17 19:33:30 +00001507 }
Daniel Dunbard18049a2009-04-07 21:16:11 +00001508
John McCalldfea9982010-04-09 19:12:06 +00001509 // -faccess-control is default.
John McCall3155f572010-04-09 19:03:51 +00001510 if (Args.hasFlag(options::OPT_fno_access_control,
1511 options::OPT_faccess_control,
John McCalldfea9982010-04-09 19:12:06 +00001512 false))
John McCall3155f572010-04-09 19:03:51 +00001513 CmdArgs.push_back("-fno-access-control");
John McCall59bb1d42010-03-17 01:32:13 +00001514
Anders Carlssond470fef2010-11-21 00:09:52 +00001515 // -felide-constructors is the default.
1516 if (Args.hasFlag(options::OPT_fno_elide_constructors,
1517 options::OPT_felide_constructors,
1518 false))
1519 CmdArgs.push_back("-fno-elide-constructors");
1520
Anders Carlssone96ab552011-02-28 02:27:16 +00001521 // Add exception args.
1522 addExceptionArgs(Args, InputType, getToolChain().getTriple(),
1523 KernelOrKext, IsRewriter, CmdArgs);
Mike Stump183c3d22009-07-31 23:15:31 +00001524
Daniel Dunbar3241d402010-02-10 18:49:11 +00001525 if (getToolChain().UseSjLjExceptions())
1526 CmdArgs.push_back("-fsjlj-exceptions");
1527
Daniel Dunbar0730e4f2009-11-17 07:06:20 +00001528 // -frtti is default.
Daniel Dunbare46b52a2010-03-20 04:52:14 +00001529 if (KernelOrKext ||
1530 !Args.hasFlag(options::OPT_frtti, options::OPT_fno_rtti))
Daniel Dunbar484afa22009-11-19 04:55:23 +00001531 CmdArgs.push_back("-fno-rtti");
Mike Stump183c3d22009-07-31 23:15:31 +00001532
Argyrios Kyrtzidis74825bc2010-10-08 00:25:19 +00001533 // -fshort-enums=0 is default.
1534 // FIXME: Are there targers where -fshort-enums is on by default ?
1535 if (Args.hasFlag(options::OPT_fshort_enums,
1536 options::OPT_fno_short_enums, false))
1537 CmdArgs.push_back("-fshort-enums");
1538
Daniel Dunbard609b7b2009-11-17 06:37:03 +00001539 // -fsigned-char is default.
Daniel Dunbar5bdd2992009-11-25 10:14:30 +00001540 if (!Args.hasFlag(options::OPT_fsigned_char, options::OPT_funsigned_char,
Daniel Dunbard609b7b2009-11-17 06:37:03 +00001541 isSignedCharDefault(getToolChain().getTriple())))
Daniel Dunbar5fe08662009-11-29 02:39:08 +00001542 CmdArgs.push_back("-fno-signed-char");
Eli Friedman327f0b52009-06-05 07:21:14 +00001543
Anders Carlssonfcd764a2010-02-06 23:23:06 +00001544 // -fthreadsafe-static is default.
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001545 if (!Args.hasFlag(options::OPT_fthreadsafe_statics,
Anders Carlssonfcd764a2010-02-06 23:23:06 +00001546 options::OPT_fno_threadsafe_statics))
1547 CmdArgs.push_back("-fno-threadsafe-statics");
1548
Daniel Dunbarfe06df42010-03-20 04:15:41 +00001549 // -fuse-cxa-atexit is default.
Anton Korobeynikov82b33332010-09-11 11:17:06 +00001550 if (KernelOrKext ||
1551 !Args.hasFlag(options::OPT_fuse_cxa_atexit, options::OPT_fno_use_cxa_atexit,
NAKAMURA Takumi6bdc8a22010-10-10 01:53:03 +00001552 getToolChain().getTriple().getOS() != llvm::Triple::Cygwin &&
NAKAMURA Takumi31ea2f12011-02-17 08:51:38 +00001553 getToolChain().getTriple().getOS() != llvm::Triple::MinGW32))
Daniel Dunbarfe06df42010-03-20 04:15:41 +00001554 CmdArgs.push_back("-fno-use-cxa-atexit");
1555
Daniel Dunbar0730e4f2009-11-17 07:06:20 +00001556 // -fms-extensions=0 is default.
Daniel Dunbar5bdd2992009-11-25 10:14:30 +00001557 if (Args.hasFlag(options::OPT_fms_extensions, options::OPT_fno_ms_extensions,
Daniel Dunbar0730e4f2009-11-17 07:06:20 +00001558 getToolChain().getTriple().getOS() == llvm::Triple::Win32))
1559 CmdArgs.push_back("-fms-extensions");
1560
Michael J. Spencer4992ca4b2010-10-21 05:21:48 +00001561 // -fmsc-version=1300 is default.
1562 if (Args.hasFlag(options::OPT_fms_extensions, options::OPT_fno_ms_extensions,
1563 getToolChain().getTriple().getOS() == llvm::Triple::Win32) ||
1564 Args.hasArg(options::OPT_fmsc_version)) {
1565 llvm::StringRef msc_ver = Args.getLastArgValue(options::OPT_fmsc_version);
1566 if (msc_ver.empty())
1567 CmdArgs.push_back("-fmsc-version=1300");
1568 else
1569 CmdArgs.push_back(Args.MakeArgString("-fmsc-version=" + msc_ver));
1570 }
1571
1572
Dawn Perchik68bb1b42010-09-02 23:59:25 +00001573 // -fborland-extensions=0 is default.
1574 if (Args.hasFlag(options::OPT_fborland_extensions,
1575 options::OPT_fno_borland_extensions, false))
1576 CmdArgs.push_back("-fborland-extensions");
1577
Chandler Carruthe03aa552010-04-17 20:17:31 +00001578 // -fgnu-keywords default varies depending on language; only pass if
1579 // specified.
1580 if (Arg *A = Args.getLastArg(options::OPT_fgnu_keywords,
Daniel Dunbardb059592010-04-24 17:56:39 +00001581 options::OPT_fno_gnu_keywords))
1582 A->render(Args, CmdArgs);
Chandler Carruthe03aa552010-04-17 20:17:31 +00001583
Daniel Dunbar99b55242010-07-19 19:44:22 +00001584 // -fnext-runtime defaults to on Darwin and when rewriting Objective-C, and is
1585 // -the -cc1 default.
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001586 bool NeXTRuntimeIsDefault =
Daniel Dunbar99b55242010-07-19 19:44:22 +00001587 IsRewriter || getToolChain().getTriple().getOS() == llvm::Triple::Darwin;
Daniel Dunbar5bdd2992009-11-25 10:14:30 +00001588 if (!Args.hasFlag(options::OPT_fnext_runtime, options::OPT_fgnu_runtime,
Daniel Dunbar99b55242010-07-19 19:44:22 +00001589 NeXTRuntimeIsDefault))
Daniel Dunbar4656c532009-11-17 07:07:28 +00001590 CmdArgs.push_back("-fgnu-runtime");
1591
Daniel Dunbar4930e332009-11-17 08:07:36 +00001592 // -fobjc-nonfragile-abi=0 is default.
1593 if (types::isObjC(InputType)) {
Daniel Dunbarc1dd0e92010-09-20 18:19:55 +00001594 // Compute the Objective-C ABI "version" to use. Version numbers are
1595 // slightly confusing for historical reasons:
1596 // 1 - Traditional "fragile" ABI
1597 // 2 - Non-fragile ABI, version 1
1598 // 3 - Non-fragile ABI, version 2
Daniel Dunbar12c82082010-04-28 23:25:24 +00001599 unsigned Version = 1;
Daniel Dunbarc1dd0e92010-09-20 18:19:55 +00001600 // If -fobjc-abi-version= is present, use that to set the version.
Daniel Dunbar12c82082010-04-28 23:25:24 +00001601 if (Arg *A = Args.getLastArg(options::OPT_fobjc_abi_version_EQ)) {
1602 if (llvm::StringRef(A->getValue(Args)) == "1")
1603 Version = 1;
1604 else if (llvm::StringRef(A->getValue(Args)) == "2")
1605 Version = 2;
Fariborz Jahanianf51a3872010-09-15 16:00:51 +00001606 else if (llvm::StringRef(A->getValue(Args)) == "3")
1607 Version = 3;
Daniel Dunbar12c82082010-04-28 23:25:24 +00001608 else
1609 D.Diag(clang::diag::err_drv_clang_unsupported) << A->getAsString(Args);
Daniel Dunbarc1dd0e92010-09-20 18:19:55 +00001610 } else {
1611 // Otherwise, determine if we are using the non-fragile ABI.
1612 if (Args.hasFlag(options::OPT_fobjc_nonfragile_abi,
1613 options::OPT_fno_objc_nonfragile_abi,
1614 getToolChain().IsObjCNonFragileABIDefault())) {
1615 // Determine the non-fragile ABI version to use.
Daniel Dunbaraeed5fe2010-11-11 16:08:59 +00001616#ifdef DISABLE_DEFAULT_NONFRAGILEABI_TWO
1617 unsigned NonFragileABIVersion = 1;
1618#else
Daniel Dunbarc1dd0e92010-09-20 18:19:55 +00001619 unsigned NonFragileABIVersion = 2;
Daniel Dunbaraeed5fe2010-11-11 16:08:59 +00001620#endif
Daniel Dunbarc1dd0e92010-09-20 18:19:55 +00001621
1622 if (Arg *A = Args.getLastArg(
1623 options::OPT_fobjc_nonfragile_abi_version_EQ)) {
1624 if (llvm::StringRef(A->getValue(Args)) == "1")
1625 NonFragileABIVersion = 1;
1626 else if (llvm::StringRef(A->getValue(Args)) == "2")
1627 NonFragileABIVersion = 2;
1628 else
1629 D.Diag(clang::diag::err_drv_clang_unsupported)
1630 << A->getAsString(Args);
1631 }
1632
1633 Version = 1 + NonFragileABIVersion;
1634 } else {
1635 Version = 1;
1636 }
Daniel Dunbar12c82082010-04-28 23:25:24 +00001637 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001638
Fariborz Jahanianf51a3872010-09-15 16:00:51 +00001639 if (Version == 2 || Version == 3) {
Fariborz Jahanian3aa19e9a2011-01-04 20:05:20 +00001640 CmdArgs.push_back("-fobjc-nonfragile-abi");
Daniel Dunbarfca18c1b42010-04-24 17:56:46 +00001641
1642 // -fobjc-dispatch-method is only relevant with the nonfragile-abi, and
1643 // legacy is the default.
1644 if (!Args.hasFlag(options::OPT_fobjc_legacy_dispatch,
1645 options::OPT_fno_objc_legacy_dispatch,
1646 getToolChain().IsObjCLegacyDispatchDefault())) {
1647 if (getToolChain().UseObjCMixedDispatch())
1648 CmdArgs.push_back("-fobjc-dispatch-method=mixed");
1649 else
1650 CmdArgs.push_back("-fobjc-dispatch-method=non-legacy");
1651 }
Daniel Dunbar2e3f2c82010-02-01 21:07:25 +00001652 }
NAKAMURA Takumi029d74b2011-02-17 08:50:50 +00001653
Ted Kremenek1d56c9e2010-12-23 21:35:43 +00001654 // -fobjc-default-synthesize-properties=0 is default.
1655 if (Args.hasFlag(options::OPT_fobjc_default_synthesize_properties,
1656 options::OPT_fno_objc_default_synthesize_properties,
1657 getToolChain().IsObjCDefaultSynthPropertiesDefault())) {
1658 CmdArgs.push_back("-fobjc-default-synthesize-properties");
1659 }
Daniel Dunbar4930e332009-11-17 08:07:36 +00001660 }
1661
Daniel Dunbar2e3f2c82010-02-01 21:07:25 +00001662 if (!Args.hasFlag(options::OPT_fassume_sane_operator_new,
1663 options::OPT_fno_assume_sane_operator_new))
1664 CmdArgs.push_back("-fno-assume-sane-operator-new");
1665
Daniel Dunbar34d7a992010-04-27 15:34:57 +00001666 // -fconstant-cfstrings is default, and may be subject to argument translation
1667 // on Darwin.
1668 if (!Args.hasFlag(options::OPT_fconstant_cfstrings,
1669 options::OPT_fno_constant_cfstrings) ||
1670 !Args.hasFlag(options::OPT_mconstant_cfstrings,
1671 options::OPT_mno_constant_cfstrings))
1672 CmdArgs.push_back("-fno-constant-cfstrings");
1673
John Thompsoned4e2952009-11-05 20:14:16 +00001674 // -fshort-wchar default varies depending on platform; only
1675 // pass if specified.
Daniel Dunbar2cb4e7a2010-04-27 15:35:03 +00001676 if (Arg *A = Args.getLastArg(options::OPT_fshort_wchar))
1677 A->render(Args, CmdArgs);
John Thompsoned4e2952009-11-05 20:14:16 +00001678
Daniel Dunbar1a8a2e82009-10-29 02:39:57 +00001679 // -fno-pascal-strings is default, only pass non-default. If the tool chain
1680 // happened to translate to -mpascal-strings, we want to back translate here.
Daniel Dunbard4510f22009-04-07 23:51:44 +00001681 //
1682 // FIXME: This is gross; that translation should be pulled from the
1683 // tool chain.
Daniel Dunbard067f7f2009-04-08 23:54:23 +00001684 if (Args.hasFlag(options::OPT_fpascal_strings,
Daniel Dunbard4510f22009-04-07 23:51:44 +00001685 options::OPT_fno_pascal_strings,
1686 false) ||
1687 Args.hasFlag(options::OPT_mpascal_strings,
1688 options::OPT_mno_pascal_strings,
1689 false))
Daniel Dunbard18049a2009-04-07 21:16:11 +00001690 CmdArgs.push_back("-fpascal-strings");
NAKAMURA Takumi029d74b2011-02-17 08:50:50 +00001691
Fariborz Jahaniana4cfff82011-01-07 01:05:02 +00001692 if (Args.hasArg(options::OPT_mkernel) ||
1693 Args.hasArg(options::OPT_fapple_kext)) {
1694 if (!Args.hasArg(options::OPT_fcommon))
1695 CmdArgs.push_back("-fno-common");
1696 }
Daniel Dunbard18049a2009-04-07 21:16:11 +00001697 // -fcommon is default, only pass non-default.
Fariborz Jahaniana4cfff82011-01-07 01:05:02 +00001698 else if (!Args.hasFlag(options::OPT_fcommon, options::OPT_fno_common))
Daniel Dunbard18049a2009-04-07 21:16:11 +00001699 CmdArgs.push_back("-fno-common");
1700
Daniel Dunbar2edd9232009-04-15 02:37:43 +00001701 // -fsigned-bitfields is default, and clang doesn't yet support
Daniel Dunbar6358d682010-10-15 22:30:42 +00001702 // -funsigned-bitfields.
Mike Stump11289f42009-09-09 15:08:12 +00001703 if (!Args.hasFlag(options::OPT_fsigned_bitfields,
Daniel Dunbar2edd9232009-04-15 02:37:43 +00001704 options::OPT_funsigned_bitfields))
1705 D.Diag(clang::diag::warn_drv_clang_unsupported)
1706 << Args.getLastArg(options::OPT_funsigned_bitfields)->getAsString(Args);
1707
Daniel Dunbar6358d682010-10-15 22:30:42 +00001708 // -fsigned-bitfields is default, and clang doesn't support -fno-for-scope.
1709 if (!Args.hasFlag(options::OPT_ffor_scope,
1710 options::OPT_fno_for_scope))
1711 D.Diag(clang::diag::err_drv_clang_unsupported)
1712 << Args.getLastArg(options::OPT_fno_for_scope)->getAsString(Args);
1713
Jeffrey Yasskin460aa542010-06-08 04:56:20 +00001714 // -fcaret-diagnostics is default.
1715 if (!Args.hasFlag(options::OPT_fcaret_diagnostics,
1716 options::OPT_fno_caret_diagnostics, true))
1717 CmdArgs.push_back("-fno-caret-diagnostics");
1718
Daniel Dunbar8281bde2009-04-19 21:09:34 +00001719 // -fdiagnostics-fixit-info is default, only pass non-default.
Mike Stump11289f42009-09-09 15:08:12 +00001720 if (!Args.hasFlag(options::OPT_fdiagnostics_fixit_info,
Daniel Dunbar8281bde2009-04-19 21:09:34 +00001721 options::OPT_fno_diagnostics_fixit_info))
1722 CmdArgs.push_back("-fno-diagnostics-fixit-info");
1723
Daniel Dunbar092f0cc2009-04-16 06:32:38 +00001724 // Enable -fdiagnostics-show-option by default.
Mike Stump11289f42009-09-09 15:08:12 +00001725 if (Args.hasFlag(options::OPT_fdiagnostics_show_option,
Daniel Dunbar092f0cc2009-04-16 06:32:38 +00001726 options::OPT_fno_diagnostics_show_option))
1727 CmdArgs.push_back("-fdiagnostics-show-option");
Daniel Dunbar5ec95022009-11-04 06:24:57 +00001728
Chris Lattnerbf6fac82010-05-04 21:55:25 +00001729 if (const Arg *A =
1730 Args.getLastArg(options::OPT_fdiagnostics_show_category_EQ)) {
1731 CmdArgs.push_back("-fdiagnostics-show-category");
1732 CmdArgs.push_back(A->getValue(Args));
1733 }
Daniel Dunbarfe6c97b2010-08-24 16:47:49 +00001734
Daniel Dunbar5ec95022009-11-04 06:24:57 +00001735 // Color diagnostics are the default, unless the terminal doesn't support
1736 // them.
1737 if (Args.hasFlag(options::OPT_fcolor_diagnostics,
Argyrios Kyrtzidis4f920162010-09-23 12:56:06 +00001738 options::OPT_fno_color_diagnostics,
1739 llvm::sys::Process::StandardErrHasColors()))
Daniel Dunbar5ec95022009-11-04 06:24:57 +00001740 CmdArgs.push_back("-fcolor-diagnostics");
1741
Daniel Dunbardb097022009-06-08 21:13:54 +00001742 if (!Args.hasFlag(options::OPT_fshow_source_location,
1743 options::OPT_fno_show_source_location))
1744 CmdArgs.push_back("-fno-show-source-location");
Daniel Dunbar092f0cc2009-04-16 06:32:38 +00001745
Douglas Gregor8ed0c0b2010-07-09 17:35:33 +00001746 if (!Args.hasFlag(options::OPT_fspell_checking,
1747 options::OPT_fno_spell_checking))
1748 CmdArgs.push_back("-fno-spell-checking");
Daniel Dunbarfe6c97b2010-08-24 16:47:49 +00001749
Daniel Dunbar473f8a62010-10-18 22:49:46 +00001750
Daniel Dunbar3ada2b72010-11-02 19:42:04 +00001751 // Silently ignore -fasm-blocks for now.
1752 (void) Args.hasFlag(options::OPT_fasm_blocks, options::OPT_fno_asm_blocks,
1753 false);
Daniel Dunbar473f8a62010-10-18 22:49:46 +00001754
Jeffrey Yasskin2b99c6f2010-06-11 05:57:47 +00001755 if (Arg *A = Args.getLastArg(options::OPT_fshow_overloads_EQ))
1756 A->render(Args, CmdArgs);
1757
Daniel Dunbarf5e9b1f2009-04-19 21:20:32 +00001758 // -fdollars-in-identifiers default varies depending on platform and
1759 // language; only pass if specified.
Mike Stump11289f42009-09-09 15:08:12 +00001760 if (Arg *A = Args.getLastArg(options::OPT_fdollars_in_identifiers,
Daniel Dunbarf5e9b1f2009-04-19 21:20:32 +00001761 options::OPT_fno_dollars_in_identifiers)) {
1762 if (A->getOption().matches(options::OPT_fdollars_in_identifiers))
Daniel Dunbar15cef0e2009-12-16 20:10:18 +00001763 CmdArgs.push_back("-fdollars-in-identifiers");
Daniel Dunbarf5e9b1f2009-04-19 21:20:32 +00001764 else
Daniel Dunbar15cef0e2009-12-16 20:10:18 +00001765 CmdArgs.push_back("-fno-dollars-in-identifiers");
Daniel Dunbarf5e9b1f2009-04-19 21:20:32 +00001766 }
1767
Daniel Dunbaradeeb052009-05-22 19:02:20 +00001768 // -funit-at-a-time is default, and we don't support -fno-unit-at-a-time for
1769 // practical purposes.
Mike Stump11289f42009-09-09 15:08:12 +00001770 if (Arg *A = Args.getLastArg(options::OPT_funit_at_a_time,
Daniel Dunbaradeeb052009-05-22 19:02:20 +00001771 options::OPT_fno_unit_at_a_time)) {
1772 if (A->getOption().matches(options::OPT_fno_unit_at_a_time))
Edward O'Callaghanfc460552009-10-13 16:41:34 +00001773 D.Diag(clang::diag::warn_drv_clang_unsupported) << A->getAsString(Args);
Daniel Dunbaradeeb052009-05-22 19:02:20 +00001774 }
Eli Friedmanbb0d9a52009-07-14 21:58:17 +00001775
Daniel Dunbar2ffe0292009-09-10 03:37:02 +00001776 // Default to -fno-builtin-str{cat,cpy} on Darwin for ARM.
Daniel Dunbar4fa08112009-09-10 04:57:27 +00001777 //
Daniel Dunbar6c536aa2009-12-11 23:00:49 +00001778 // FIXME: This is disabled until clang -cc1 supports -fno-builtin-foo. PR4941.
Daniel Dunbar4fa08112009-09-10 04:57:27 +00001779#if 0
Daniel Dunbar2ffe0292009-09-10 03:37:02 +00001780 if (getToolChain().getTriple().getOS() == llvm::Triple::Darwin &&
1781 (getToolChain().getTriple().getArch() == llvm::Triple::arm ||
1782 getToolChain().getTriple().getArch() == llvm::Triple::thumb)) {
1783 if (!Args.hasArg(options::OPT_fbuiltin_strcat))
1784 CmdArgs.push_back("-fno-builtin-strcat");
1785 if (!Args.hasArg(options::OPT_fbuiltin_strcpy))
1786 CmdArgs.push_back("-fno-builtin-strcpy");
1787 }
Daniel Dunbar4fa08112009-09-10 04:57:27 +00001788#endif
Daniel Dunbar2ffe0292009-09-10 03:37:02 +00001789
Daniel Dunbar8c3d7352011-03-18 21:23:40 +00001790 // Only allow -traditional or -traditional-cpp outside in preprocessing modes.
Mike Stump11289f42009-09-09 15:08:12 +00001791 if (Arg *A = Args.getLastArg(options::OPT_traditional,
Daniel Dunbar8c3d7352011-03-18 21:23:40 +00001792 options::OPT_traditional_cpp)) {
1793 if (isa<PreprocessJobAction>(JA))
1794 CmdArgs.push_back("-traditional-cpp");
1795 else
1796 D.Diag(clang::diag::err_drv_clang_unsupported) << A->getAsString(Args);
1797 }
Eli Friedmanbb0d9a52009-07-14 21:58:17 +00001798
Daniel Dunbara2aedc62009-03-18 10:01:51 +00001799 Args.AddLastArg(CmdArgs, options::OPT_dM);
Chris Lattnercac63f32009-04-12 01:56:53 +00001800 Args.AddLastArg(CmdArgs, options::OPT_dD);
Daniel Dunbara2aedc62009-03-18 10:01:51 +00001801
Daniel Dunbar76fa8402010-04-15 06:09:03 +00001802 // Forward -Xclang arguments to -cc1, and -mllvm arguments to the LLVM option
1803 // parser.
Daniel Dunbara2aedc62009-03-18 10:01:51 +00001804 Args.AddAllArgValues(CmdArgs, options::OPT_Xclang);
Daniel Dunbar76fa8402010-04-15 06:09:03 +00001805 for (arg_iterator it = Args.filtered_begin(options::OPT_mllvm),
1806 ie = Args.filtered_end(); it != ie; ++it) {
Daniel Dunbara442fd52010-06-11 22:00:13 +00001807 (*it)->claim();
Daniel Dunbar88534f42010-04-17 06:10:00 +00001808
Daniel Dunbar76fa8402010-04-15 06:09:03 +00001809 // We translate this by hand to the -cc1 argument, since nightly test uses
1810 // it and developers have been trained to spell it with -mllvm.
Daniel Dunbara442fd52010-06-11 22:00:13 +00001811 if (llvm::StringRef((*it)->getValue(Args, 0)) == "-disable-llvm-optzns")
Daniel Dunbar76fa8402010-04-15 06:09:03 +00001812 CmdArgs.push_back("-disable-llvm-optzns");
1813 else
Daniel Dunbara442fd52010-06-11 22:00:13 +00001814 (*it)->render(Args, CmdArgs);
Daniel Dunbar76fa8402010-04-15 06:09:03 +00001815 }
Daniel Dunbara2aedc62009-03-18 10:01:51 +00001816
Daniel Dunbard67a3222009-03-30 06:36:42 +00001817 if (Output.getType() == types::TY_Dependencies) {
1818 // Handled with other dependency code.
Daniel Dunbar5cdf3e02009-03-19 07:29:38 +00001819 } else if (Output.isFilename()) {
Daniel Dunbara3246a02009-03-18 08:07:30 +00001820 CmdArgs.push_back("-o");
Daniel Dunbar5cdf3e02009-03-19 07:29:38 +00001821 CmdArgs.push_back(Output.getFilename());
1822 } else {
1823 assert(Output.isNothing() && "Invalid output.");
Daniel Dunbara3246a02009-03-18 08:07:30 +00001824 }
1825
Daniel Dunbara2aedc62009-03-18 10:01:51 +00001826 for (InputInfoList::const_iterator
1827 it = Inputs.begin(), ie = Inputs.end(); it != ie; ++it) {
1828 const InputInfo &II = *it;
1829 CmdArgs.push_back("-x");
1830 CmdArgs.push_back(types::getTypeName(II.getType()));
Daniel Dunbarb440f562010-08-02 02:38:21 +00001831 if (II.isFilename())
Daniel Dunbar5cdf3e02009-03-19 07:29:38 +00001832 CmdArgs.push_back(II.getFilename());
Daniel Dunbara2aedc62009-03-18 10:01:51 +00001833 else
Daniel Dunbar5cdf3e02009-03-19 07:29:38 +00001834 II.getInputArg().renderAsInput(Args, CmdArgs);
Daniel Dunbara2aedc62009-03-18 10:01:51 +00001835 }
Daniel Dunbard067f7f2009-04-08 23:54:23 +00001836
Chris Lattnere9d7d782009-11-03 19:50:27 +00001837 Args.AddAllArgs(CmdArgs, options::OPT_undef);
1838
Daniel Dunbarb31b76f2010-07-18 21:16:15 +00001839 const char *Exec = getToolChain().getDriver().getClangProgramPath();
Daniel Dunbar24c7f5e2009-12-18 02:43:17 +00001840
1841 // Optionally embed the -cc1 level arguments into the debug info, for build
1842 // analysis.
1843 if (getToolChain().UseDwarfDebugFlags()) {
Daniel Dunbar7f3d9502010-06-04 18:47:06 +00001844 ArgStringList OriginalArgs;
1845 for (ArgList::const_iterator it = Args.begin(),
1846 ie = Args.end(); it != ie; ++it)
1847 (*it)->render(Args, OriginalArgs);
Daniel Dunbarfe6c97b2010-08-24 16:47:49 +00001848
Daniel Dunbar24c7f5e2009-12-18 02:43:17 +00001849 llvm::SmallString<256> Flags;
1850 Flags += Exec;
Daniel Dunbar7f3d9502010-06-04 18:47:06 +00001851 for (unsigned i = 0, e = OriginalArgs.size(); i != e; ++i) {
Daniel Dunbar24c7f5e2009-12-18 02:43:17 +00001852 Flags += " ";
Daniel Dunbar7f3d9502010-06-04 18:47:06 +00001853 Flags += OriginalArgs[i];
Daniel Dunbar24c7f5e2009-12-18 02:43:17 +00001854 }
1855 CmdArgs.push_back("-dwarf-debug-flags");
1856 CmdArgs.push_back(Args.MakeArgString(Flags.str()));
1857 }
1858
Daniel Dunbar7fbaf532010-08-02 02:38:28 +00001859 C.addCommand(new Command(JA, *this, Exec, CmdArgs));
Daniel Dunbar17731772009-03-23 19:03:36 +00001860
Roman Divacky178e01602011-02-10 16:52:03 +00001861 if (Arg *A = Args.getLastArg(options::OPT_pg))
1862 if (Args.hasArg(options::OPT_fomit_frame_pointer))
1863 D.Diag(clang::diag::err_drv_argument_not_allowed_with)
1864 << "-fomit-frame-pointer" << A->getAsString(Args);
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001865
Daniel Dunbarc2a71892009-04-03 20:51:31 +00001866 // Claim some arguments which clang supports automatically.
1867
Daniel Dunbar3e0cac62010-04-15 06:18:42 +00001868 // -fpch-preprocess is used with gcc to add a special marker in the output to
1869 // include the PCH file. Clang's PTH solution is completely transparent, so we
1870 // do not need to deal with it at all.
Daniel Dunbarc2a71892009-04-03 20:51:31 +00001871 Args.ClaimAllArgs(options::OPT_fpch_preprocess);
Daniel Dunbard067f7f2009-04-08 23:54:23 +00001872
Daniel Dunbar17731772009-03-23 19:03:36 +00001873 // Claim some arguments which clang doesn't support, but we don't
1874 // care to warn the user about.
Daniel Dunbar44b36ee2009-11-25 11:53:23 +00001875 Args.ClaimAllArgs(options::OPT_clang_ignored_f_Group);
1876 Args.ClaimAllArgs(options::OPT_clang_ignored_m_Group);
Rafael Espindola22f603032011-02-28 23:29:45 +00001877
Rafael Espindolad95a8122011-03-01 05:25:27 +00001878 // Disable warnings for clang -E -use-gold-plugin -emit-llvm foo.c
Rafael Espindola22f603032011-02-28 23:29:45 +00001879 Args.ClaimAllArgs(options::OPT_use_gold_plugin);
Rafael Espindolad95a8122011-03-01 05:25:27 +00001880 Args.ClaimAllArgs(options::OPT_emit_llvm);
Daniel Dunbar1a093d22009-03-18 06:00:36 +00001881}
1882
Daniel Dunbar4f5e79c2010-05-20 21:30:13 +00001883void ClangAs::ConstructJob(Compilation &C, const JobAction &JA,
Daniel Dunbar4f5e79c2010-05-20 21:30:13 +00001884 const InputInfo &Output,
1885 const InputInfoList &Inputs,
1886 const ArgList &Args,
1887 const char *LinkingOutput) const {
Daniel Dunbar4f5e79c2010-05-20 21:30:13 +00001888 ArgStringList CmdArgs;
1889
1890 assert(Inputs.size() == 1 && "Unexpected number of inputs.");
1891 const InputInfo &Input = Inputs[0];
1892
Rafael Espindolacfaadda2010-11-17 22:13:25 +00001893 // Don't warn about "clang -w -c foo.s"
1894 Args.ClaimAllArgs(options::OPT_w);
Rafael Espindolad95a8122011-03-01 05:25:27 +00001895 // and "clang -emit-llvm -c foo.s"
1896 Args.ClaimAllArgs(options::OPT_emit_llvm);
1897 // and "clang -use-gold-plugin -c foo.s"
1898 Args.ClaimAllArgs(options::OPT_use_gold_plugin);
Rafael Espindolacfaadda2010-11-17 22:13:25 +00001899
Daniel Dunbar4f5e79c2010-05-20 21:30:13 +00001900 // Invoke ourselves in -cc1as mode.
1901 //
1902 // FIXME: Implement custom jobs for internal actions.
1903 CmdArgs.push_back("-cc1as");
1904
1905 // Add the "effective" target triple.
1906 CmdArgs.push_back("-triple");
Daniel Dunbar82eb4ce2010-08-23 22:35:37 +00001907 std::string TripleStr = getToolChain().ComputeEffectiveClangTriple(Args);
Daniel Dunbar4f5e79c2010-05-20 21:30:13 +00001908 CmdArgs.push_back(Args.MakeArgString(TripleStr));
1909
1910 // Set the output mode, we currently only expect to be used as a real
1911 // assembler.
1912 CmdArgs.push_back("-filetype");
1913 CmdArgs.push_back("obj");
1914
Daniel Dunbar06e2cc32010-05-27 06:18:05 +00001915 // At -O0, we use -mrelax-all by default.
1916 bool IsOpt = false;
1917 if (Arg *A = Args.getLastArg(options::OPT_O_Group))
1918 IsOpt = !A->getOption().matches(options::OPT_O0);
1919 if (Args.hasFlag(options::OPT_mrelax_all,
1920 options::OPT_mno_relax_all,
1921 !IsOpt))
Daniel Dunbar99ca8b72010-05-28 16:43:21 +00001922 CmdArgs.push_back("-relax-all");
Daniel Dunbar06e2cc32010-05-27 06:18:05 +00001923
Daniel Dunbar1d733e22011-03-17 17:37:29 +00001924 // Ignore explicit -force_cpusubtype_ALL option.
1925 (void) Args.hasArg(options::OPT_force__cpusubtype__ALL);
Daniel Dunbar4f5e79c2010-05-20 21:30:13 +00001926
1927 // FIXME: Add -g support, once we have it.
1928
1929 // FIXME: Add -static support, once we have it.
1930
1931 Args.AddAllArgValues(CmdArgs, options::OPT_Wa_COMMA,
1932 options::OPT_Xassembler);
1933
1934 assert(Output.isFilename() && "Unexpected lipo output.");
1935 CmdArgs.push_back("-o");
1936 CmdArgs.push_back(Output.getFilename());
1937
Daniel Dunbarb440f562010-08-02 02:38:21 +00001938 assert(Input.isFilename() && "Invalid input.");
1939 CmdArgs.push_back(Input.getFilename());
Daniel Dunbar4f5e79c2010-05-20 21:30:13 +00001940
Daniel Dunbarb31b76f2010-07-18 21:16:15 +00001941 const char *Exec = getToolChain().getDriver().getClangProgramPath();
Daniel Dunbar7fbaf532010-08-02 02:38:28 +00001942 C.addCommand(new Command(JA, *this, Exec, CmdArgs));
Daniel Dunbar4f5e79c2010-05-20 21:30:13 +00001943}
1944
Daniel Dunbara3246a02009-03-18 08:07:30 +00001945void gcc::Common::ConstructJob(Compilation &C, const JobAction &JA,
Daniel Dunbara3246a02009-03-18 08:07:30 +00001946 const InputInfo &Output,
1947 const InputInfoList &Inputs,
Daniel Dunbara2aedc62009-03-18 10:01:51 +00001948 const ArgList &Args,
Daniel Dunbara3246a02009-03-18 08:07:30 +00001949 const char *LinkingOutput) const {
Daniel Dunbar083edf72009-12-21 18:54:17 +00001950 const Driver &D = getToolChain().getDriver();
Daniel Dunbara3246a02009-03-18 08:07:30 +00001951 ArgStringList CmdArgs;
Daniel Dunbar1a093d22009-03-18 06:00:36 +00001952
Daniel Dunbard067f7f2009-04-08 23:54:23 +00001953 for (ArgList::const_iterator
Daniel Dunbara2aedc62009-03-18 10:01:51 +00001954 it = Args.begin(), ie = Args.end(); it != ie; ++it) {
Daniel Dunbara3246a02009-03-18 08:07:30 +00001955 Arg *A = *it;
Daniel Dunbar2da02722009-03-19 07:55:12 +00001956 if (A->getOption().hasForwardToGCC()) {
Daniel Dunbar939c1212010-08-03 16:14:14 +00001957 // Don't forward any -g arguments to assembly steps.
1958 if (isa<AssembleJobAction>(JA) &&
1959 A->getOption().matches(options::OPT_g_Group))
1960 continue;
1961
Daniel Dunbar2da02722009-03-19 07:55:12 +00001962 // It is unfortunate that we have to claim here, as this means
1963 // we will basically never report anything interesting for
Daniel Dunbar5716d872009-05-02 21:41:52 +00001964 // platforms using a generic gcc, even if we are just using gcc
1965 // to get to the assembler.
Daniel Dunbar2da02722009-03-19 07:55:12 +00001966 A->claim();
Daniel Dunbara2aedc62009-03-18 10:01:51 +00001967 A->render(Args, CmdArgs);
Daniel Dunbar2da02722009-03-19 07:55:12 +00001968 }
Daniel Dunbara3246a02009-03-18 08:07:30 +00001969 }
Daniel Dunbard067f7f2009-04-08 23:54:23 +00001970
Daniel Dunbar4e295052010-01-25 22:35:08 +00001971 RenderExtraToolArgs(JA, CmdArgs);
Daniel Dunbara3246a02009-03-18 08:07:30 +00001972
1973 // If using a driver driver, force the arch.
Daniel Dunbar5bbebfe2009-05-22 02:21:04 +00001974 const std::string &Arch = getToolChain().getArchName();
Daniel Dunbarf4894fe2009-12-23 00:46:38 +00001975 if (getToolChain().getTriple().getOS() == llvm::Triple::Darwin) {
Daniel Dunbara3246a02009-03-18 08:07:30 +00001976 CmdArgs.push_back("-arch");
Daniel Dunbar0a05d932009-04-01 20:33:11 +00001977
1978 // FIXME: Remove these special cases.
Daniel Dunbar5bbebfe2009-05-22 02:21:04 +00001979 if (Arch == "powerpc")
1980 CmdArgs.push_back("ppc");
1981 else if (Arch == "powerpc64")
1982 CmdArgs.push_back("ppc64");
1983 else
Daniel Dunbar1c28f1e2009-09-09 22:32:48 +00001984 CmdArgs.push_back(Args.MakeArgString(Arch));
Daniel Dunbara3246a02009-03-18 08:07:30 +00001985 }
1986
Daniel Dunbar5716d872009-05-02 21:41:52 +00001987 // Try to force gcc to match the tool chain we want, if we recognize
1988 // the arch.
Daniel Dunbar5bbebfe2009-05-22 02:21:04 +00001989 //
1990 // FIXME: The triple class should directly provide the information we want
1991 // here.
1992 if (Arch == "i386" || Arch == "powerpc")
Daniel Dunbar5716d872009-05-02 21:41:52 +00001993 CmdArgs.push_back("-m32");
Daniel Dunbar5bbebfe2009-05-22 02:21:04 +00001994 else if (Arch == "x86_64" || Arch == "powerpc64")
Daniel Dunbar5716d872009-05-02 21:41:52 +00001995 CmdArgs.push_back("-m64");
1996
Daniel Dunbarb440f562010-08-02 02:38:21 +00001997 if (Output.isFilename()) {
Daniel Dunbara3246a02009-03-18 08:07:30 +00001998 CmdArgs.push_back("-o");
Daniel Dunbar5cdf3e02009-03-19 07:29:38 +00001999 CmdArgs.push_back(Output.getFilename());
2000 } else {
2001 assert(Output.isNothing() && "Unexpected output");
Daniel Dunbara3246a02009-03-18 08:07:30 +00002002 CmdArgs.push_back("-fsyntax-only");
Daniel Dunbar5cdf3e02009-03-19 07:29:38 +00002003 }
Daniel Dunbara3246a02009-03-18 08:07:30 +00002004
2005
2006 // Only pass -x if gcc will understand it; otherwise hope gcc
2007 // understands the suffix correctly. The main use case this would go
2008 // wrong in is for linker inputs if they happened to have an odd
2009 // suffix; really the only way to get this to happen is a command
2010 // like '-x foobar a.c' which will treat a.c like a linker input.
2011 //
2012 // FIXME: For the linker case specifically, can we safely convert
2013 // inputs into '-Wl,' options?
2014 for (InputInfoList::const_iterator
2015 it = Inputs.begin(), ie = Inputs.end(); it != ie; ++it) {
2016 const InputInfo &II = *it;
Daniel Dunbare3e263f2009-05-02 20:14:53 +00002017
Daniel Dunbar6cdf83c2009-09-01 16:57:46 +00002018 // Don't try to pass LLVM or AST inputs to a generic gcc.
Daniel Dunbar24e52992010-06-07 23:28:45 +00002019 if (II.getType() == types::TY_LLVM_IR || II.getType() == types::TY_LTO_IR ||
2020 II.getType() == types::TY_LLVM_BC || II.getType() == types::TY_LTO_BC)
Daniel Dunbare3e263f2009-05-02 20:14:53 +00002021 D.Diag(clang::diag::err_drv_no_linker_llvm_support)
Daniel Dunbar1c28f1e2009-09-09 22:32:48 +00002022 << getToolChain().getTripleString();
Daniel Dunbar6cdf83c2009-09-01 16:57:46 +00002023 else if (II.getType() == types::TY_AST)
2024 D.Diag(clang::diag::err_drv_no_ast_support)
Daniel Dunbar1c28f1e2009-09-09 22:32:48 +00002025 << getToolChain().getTripleString();
Daniel Dunbare3e263f2009-05-02 20:14:53 +00002026
Daniel Dunbara3246a02009-03-18 08:07:30 +00002027 if (types::canTypeBeUserSpecified(II.getType())) {
2028 CmdArgs.push_back("-x");
2029 CmdArgs.push_back(types::getTypeName(II.getType()));
2030 }
2031
Daniel Dunbarb440f562010-08-02 02:38:21 +00002032 if (II.isFilename())
Daniel Dunbar5cdf3e02009-03-19 07:29:38 +00002033 CmdArgs.push_back(II.getFilename());
Daniel Dunbarf2476752010-09-25 18:10:05 +00002034 else {
2035 const Arg &A = II.getInputArg();
2036
2037 // Reverse translate some rewritten options.
2038 if (A.getOption().matches(options::OPT_Z_reserved_lib_stdcxx)) {
2039 CmdArgs.push_back("-lstdc++");
2040 continue;
2041 }
2042
Daniel Dunbar5cdf3e02009-03-19 07:29:38 +00002043 // Don't render as input, we need gcc to do the translations.
Daniel Dunbarf2476752010-09-25 18:10:05 +00002044 A.render(Args, CmdArgs);
2045 }
Daniel Dunbara3246a02009-03-18 08:07:30 +00002046 }
2047
Rafael Espindolab2d0d402010-09-06 02:36:23 +00002048 const char *GCCName = getToolChain().getDriver().getCCCGenericGCCName().c_str();
Daniel Dunbard067f7f2009-04-08 23:54:23 +00002049 const char *Exec =
Daniel Dunbar9c3ed5f2010-07-14 18:46:23 +00002050 Args.MakeArgString(getToolChain().GetProgramPath(GCCName));
Daniel Dunbar7fbaf532010-08-02 02:38:28 +00002051 C.addCommand(new Command(JA, *this, Exec, CmdArgs));
Daniel Dunbar1a093d22009-03-18 06:00:36 +00002052}
2053
Daniel Dunbar4e295052010-01-25 22:35:08 +00002054void gcc::Preprocess::RenderExtraToolArgs(const JobAction &JA,
2055 ArgStringList &CmdArgs) const {
Daniel Dunbara3246a02009-03-18 08:07:30 +00002056 CmdArgs.push_back("-E");
Daniel Dunbar1a093d22009-03-18 06:00:36 +00002057}
2058
Daniel Dunbar4e295052010-01-25 22:35:08 +00002059void gcc::Precompile::RenderExtraToolArgs(const JobAction &JA,
2060 ArgStringList &CmdArgs) const {
Daniel Dunbara3246a02009-03-18 08:07:30 +00002061 // The type is good enough.
Daniel Dunbar1a093d22009-03-18 06:00:36 +00002062}
2063
Daniel Dunbar4e295052010-01-25 22:35:08 +00002064void gcc::Compile::RenderExtraToolArgs(const JobAction &JA,
2065 ArgStringList &CmdArgs) const {
Daniel Dunbaraeea8ac2010-02-11 03:16:21 +00002066 const Driver &D = getToolChain().getDriver();
2067
Daniel Dunbar4e295052010-01-25 22:35:08 +00002068 // If -flto, etc. are present then make sure not to force assembly output.
Daniel Dunbar24e52992010-06-07 23:28:45 +00002069 if (JA.getType() == types::TY_LLVM_IR || JA.getType() == types::TY_LTO_IR ||
2070 JA.getType() == types::TY_LLVM_BC || JA.getType() == types::TY_LTO_BC)
Daniel Dunbar4e295052010-01-25 22:35:08 +00002071 CmdArgs.push_back("-c");
Daniel Dunbaraeea8ac2010-02-11 03:16:21 +00002072 else {
2073 if (JA.getType() != types::TY_PP_Asm)
2074 D.Diag(clang::diag::err_drv_invalid_gcc_output_type)
2075 << getTypeName(JA.getType());
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00002076
Daniel Dunbar4e295052010-01-25 22:35:08 +00002077 CmdArgs.push_back("-S");
Daniel Dunbaraeea8ac2010-02-11 03:16:21 +00002078 }
Daniel Dunbar1a093d22009-03-18 06:00:36 +00002079}
2080
Daniel Dunbar4e295052010-01-25 22:35:08 +00002081void gcc::Assemble::RenderExtraToolArgs(const JobAction &JA,
2082 ArgStringList &CmdArgs) const {
Daniel Dunbara3246a02009-03-18 08:07:30 +00002083 CmdArgs.push_back("-c");
Daniel Dunbar1a093d22009-03-18 06:00:36 +00002084}
Daniel Dunbara3246a02009-03-18 08:07:30 +00002085
Daniel Dunbar4e295052010-01-25 22:35:08 +00002086void gcc::Link::RenderExtraToolArgs(const JobAction &JA,
2087 ArgStringList &CmdArgs) const {
Daniel Dunbara3246a02009-03-18 08:07:30 +00002088 // The types are (hopefully) good enough.
2089}
2090
Daniel Dunbare6adeee2009-03-29 17:08:39 +00002091const char *darwin::CC1::getCC1Name(types::ID Type) const {
2092 switch (Type) {
2093 default:
2094 assert(0 && "Unexpected type for Darwin CC1 tool.");
2095 case types::TY_Asm:
2096 case types::TY_C: case types::TY_CHeader:
2097 case types::TY_PP_C: case types::TY_PP_CHeader:
2098 return "cc1";
2099 case types::TY_ObjC: case types::TY_ObjCHeader:
2100 case types::TY_PP_ObjC: case types::TY_PP_ObjCHeader:
2101 return "cc1obj";
2102 case types::TY_CXX: case types::TY_CXXHeader:
2103 case types::TY_PP_CXX: case types::TY_PP_CXXHeader:
2104 return "cc1plus";
2105 case types::TY_ObjCXX: case types::TY_ObjCXXHeader:
2106 case types::TY_PP_ObjCXX: case types::TY_PP_ObjCXXHeader:
2107 return "cc1objplus";
2108 }
2109}
2110
Daniel Dunbard067f7f2009-04-08 23:54:23 +00002111const char *darwin::CC1::getBaseInputName(const ArgList &Args,
Daniel Dunbar52e96cc2009-03-30 00:34:04 +00002112 const InputInfoList &Inputs) {
Michael J. Spencere1696752010-12-18 00:19:12 +00002113 return Args.MakeArgString(
2114 llvm::sys::path::filename(Inputs[0].getBaseInput()));
Daniel Dunbarafec1f52009-03-29 18:40:18 +00002115}
2116
Daniel Dunbard067f7f2009-04-08 23:54:23 +00002117const char *darwin::CC1::getBaseInputStem(const ArgList &Args,
Daniel Dunbar52e96cc2009-03-30 00:34:04 +00002118 const InputInfoList &Inputs) {
Daniel Dunbarafec1f52009-03-29 18:40:18 +00002119 const char *Str = getBaseInputName(Args, Inputs);
2120
Chris Lattner906bb902011-01-16 08:14:11 +00002121 if (const char *End = strrchr(Str, '.'))
Daniel Dunbar1c28f1e2009-09-09 22:32:48 +00002122 return Args.MakeArgString(std::string(Str, End));
Daniel Dunbarafec1f52009-03-29 18:40:18 +00002123
2124 return Str;
2125}
2126
2127const char *
Daniel Dunbard067f7f2009-04-08 23:54:23 +00002128darwin::CC1::getDependencyFileName(const ArgList &Args,
Daniel Dunbar52e96cc2009-03-30 00:34:04 +00002129 const InputInfoList &Inputs) {
Daniel Dunbarafec1f52009-03-29 18:40:18 +00002130 // FIXME: Think about this more.
2131 std::string Res;
2132
2133 if (Arg *OutputOpt = Args.getLastArg(options::OPT_o)) {
2134 std::string Str(OutputOpt->getValue(Args));
Daniel Dunbard067f7f2009-04-08 23:54:23 +00002135
Daniel Dunbarafec1f52009-03-29 18:40:18 +00002136 Res = Str.substr(0, Str.rfind('.'));
2137 } else
Daniel Dunbar52e96cc2009-03-30 00:34:04 +00002138 Res = darwin::CC1::getBaseInputStem(Args, Inputs);
Daniel Dunbarafec1f52009-03-29 18:40:18 +00002139
Daniel Dunbar1c28f1e2009-09-09 22:32:48 +00002140 return Args.MakeArgString(Res + ".d");
Daniel Dunbarafec1f52009-03-29 18:40:18 +00002141}
2142
Daniel Dunbard067f7f2009-04-08 23:54:23 +00002143void darwin::CC1::AddCC1Args(const ArgList &Args,
Daniel Dunbarafec1f52009-03-29 18:40:18 +00002144 ArgStringList &CmdArgs) const {
Daniel Dunbar083edf72009-12-21 18:54:17 +00002145 const Driver &D = getToolChain().getDriver();
Daniel Dunbar4eadb602009-09-10 01:21:12 +00002146
2147 CheckCodeGenerationOptions(D, Args);
2148
Daniel Dunbarafec1f52009-03-29 18:40:18 +00002149 // Derived from cc1 spec.
Daniel Dunbarafec1f52009-03-29 18:40:18 +00002150 if (!Args.hasArg(options::OPT_mkernel) && !Args.hasArg(options::OPT_static) &&
2151 !Args.hasArg(options::OPT_mdynamic_no_pic))
2152 CmdArgs.push_back("-fPIC");
2153
Daniel Dunbar2ffe0292009-09-10 03:37:02 +00002154 if (getToolChain().getTriple().getArch() == llvm::Triple::arm ||
2155 getToolChain().getTriple().getArch() == llvm::Triple::thumb) {
2156 if (!Args.hasArg(options::OPT_fbuiltin_strcat))
2157 CmdArgs.push_back("-fno-builtin-strcat");
2158 if (!Args.hasArg(options::OPT_fbuiltin_strcpy))
2159 CmdArgs.push_back("-fno-builtin-strcpy");
2160 }
2161
Daniel Dunbarafec1f52009-03-29 18:40:18 +00002162 // gcc has some code here to deal with when no -mmacosx-version-min
2163 // and no -miphoneos-version-min is present, but this never happens
2164 // due to tool chain specific argument translation.
2165
Daniel Dunbarafec1f52009-03-29 18:40:18 +00002166 if (Args.hasArg(options::OPT_g_Flag) &&
2167 !Args.hasArg(options::OPT_fno_eliminate_unused_debug_symbols))
2168 CmdArgs.push_back("-feliminate-unused-debug-symbols");
2169}
2170
2171void darwin::CC1::AddCC1OptionsArgs(const ArgList &Args, ArgStringList &CmdArgs,
2172 const InputInfoList &Inputs,
2173 const ArgStringList &OutputArgs) const {
Daniel Dunbar083edf72009-12-21 18:54:17 +00002174 const Driver &D = getToolChain().getDriver();
Daniel Dunbarafec1f52009-03-29 18:40:18 +00002175
2176 // Derived from cc1_options spec.
2177 if (Args.hasArg(options::OPT_fast) ||
2178 Args.hasArg(options::OPT_fastf) ||
2179 Args.hasArg(options::OPT_fastcp))
2180 CmdArgs.push_back("-O3");
Daniel Dunbard067f7f2009-04-08 23:54:23 +00002181
Daniel Dunbarafec1f52009-03-29 18:40:18 +00002182 if (Arg *A = Args.getLastArg(options::OPT_pg))
2183 if (Args.hasArg(options::OPT_fomit_frame_pointer))
2184 D.Diag(clang::diag::err_drv_argument_not_allowed_with)
2185 << A->getAsString(Args) << "-fomit-frame-pointer";
2186
2187 AddCC1Args(Args, CmdArgs);
2188
2189 if (!Args.hasArg(options::OPT_Q))
2190 CmdArgs.push_back("-quiet");
2191
2192 CmdArgs.push_back("-dumpbase");
Daniel Dunbar52e96cc2009-03-30 00:34:04 +00002193 CmdArgs.push_back(darwin::CC1::getBaseInputName(Args, Inputs));
Daniel Dunbarafec1f52009-03-29 18:40:18 +00002194
2195 Args.AddAllArgs(CmdArgs, options::OPT_d_Group);
2196
2197 Args.AddAllArgs(CmdArgs, options::OPT_m_Group);
2198 Args.AddAllArgs(CmdArgs, options::OPT_a_Group);
2199
2200 // FIXME: The goal is to use the user provided -o if that is our
2201 // final output, otherwise to drive from the original input
2202 // name. Find a clean way to go about this.
2203 if ((Args.hasArg(options::OPT_c) || Args.hasArg(options::OPT_S)) &&
2204 Args.hasArg(options::OPT_o)) {
2205 Arg *OutputOpt = Args.getLastArg(options::OPT_o);
2206 CmdArgs.push_back("-auxbase-strip");
2207 CmdArgs.push_back(OutputOpt->getValue(Args));
2208 } else {
2209 CmdArgs.push_back("-auxbase");
Daniel Dunbar52e96cc2009-03-30 00:34:04 +00002210 CmdArgs.push_back(darwin::CC1::getBaseInputStem(Args, Inputs));
Daniel Dunbarafec1f52009-03-29 18:40:18 +00002211 }
2212
2213 Args.AddAllArgs(CmdArgs, options::OPT_g_Group);
2214
2215 Args.AddAllArgs(CmdArgs, options::OPT_O);
2216 // FIXME: -Wall is getting some special treatment. Investigate.
2217 Args.AddAllArgs(CmdArgs, options::OPT_W_Group, options::OPT_pedantic_Group);
2218 Args.AddLastArg(CmdArgs, options::OPT_w);
Daniel Dunbard067f7f2009-04-08 23:54:23 +00002219 Args.AddAllArgs(CmdArgs, options::OPT_std_EQ, options::OPT_ansi,
Daniel Dunbarafec1f52009-03-29 18:40:18 +00002220 options::OPT_trigraphs);
Daniel Dunbar72a60902009-04-26 01:10:38 +00002221 if (!Args.getLastArg(options::OPT_std_EQ, options::OPT_ansi)) {
2222 // Honor -std-default.
2223 Args.AddAllArgsTranslated(CmdArgs, options::OPT_std_default_EQ,
2224 "-std=", /*Joined=*/true);
2225 }
2226
Daniel Dunbarafec1f52009-03-29 18:40:18 +00002227 if (Args.hasArg(options::OPT_v))
2228 CmdArgs.push_back("-version");
Daniel Dunbar733b0f82011-03-01 18:49:30 +00002229 if (Args.hasArg(options::OPT_pg) &&
2230 getToolChain().SupportsProfiling())
Daniel Dunbarafec1f52009-03-29 18:40:18 +00002231 CmdArgs.push_back("-p");
2232 Args.AddLastArg(CmdArgs, options::OPT_p);
Daniel Dunbard067f7f2009-04-08 23:54:23 +00002233
Daniel Dunbarafec1f52009-03-29 18:40:18 +00002234 // The driver treats -fsyntax-only specially.
Daniel Dunbar2ffe0292009-09-10 03:37:02 +00002235 if (getToolChain().getTriple().getArch() == llvm::Triple::arm ||
2236 getToolChain().getTriple().getArch() == llvm::Triple::thumb) {
2237 // Removes -fbuiltin-str{cat,cpy}; these aren't recognized by cc1 but are
2238 // used to inhibit the default -fno-builtin-str{cat,cpy}.
2239 //
2240 // FIXME: Should we grow a better way to deal with "removing" args?
Daniel Dunbar44b36ee2009-11-25 11:53:23 +00002241 for (arg_iterator it = Args.filtered_begin(options::OPT_f_Group,
2242 options::OPT_fsyntax_only),
2243 ie = Args.filtered_end(); it != ie; ++it) {
Daniel Dunbara442fd52010-06-11 22:00:13 +00002244 if (!(*it)->getOption().matches(options::OPT_fbuiltin_strcat) &&
2245 !(*it)->getOption().matches(options::OPT_fbuiltin_strcpy)) {
2246 (*it)->claim();
2247 (*it)->render(Args, CmdArgs);
Daniel Dunbar2ffe0292009-09-10 03:37:02 +00002248 }
2249 }
2250 } else
2251 Args.AddAllArgs(CmdArgs, options::OPT_f_Group, options::OPT_fsyntax_only);
Daniel Dunbard067f7f2009-04-08 23:54:23 +00002252
Daniel Dunbarafec1f52009-03-29 18:40:18 +00002253 Args.AddAllArgs(CmdArgs, options::OPT_undef);
2254 if (Args.hasArg(options::OPT_Qn))
2255 CmdArgs.push_back("-fno-ident");
Daniel Dunbard067f7f2009-04-08 23:54:23 +00002256
Daniel Dunbarafec1f52009-03-29 18:40:18 +00002257 // FIXME: This isn't correct.
2258 //Args.AddLastArg(CmdArgs, options::OPT__help)
2259 //Args.AddLastArg(CmdArgs, options::OPT__targetHelp)
2260
2261 CmdArgs.append(OutputArgs.begin(), OutputArgs.end());
2262
2263 // FIXME: Still don't get what is happening here. Investigate.
2264 Args.AddAllArgs(CmdArgs, options::OPT__param);
2265
2266 if (Args.hasArg(options::OPT_fmudflap) ||
2267 Args.hasArg(options::OPT_fmudflapth)) {
2268 CmdArgs.push_back("-fno-builtin");
2269 CmdArgs.push_back("-fno-merge-constants");
2270 }
Daniel Dunbard067f7f2009-04-08 23:54:23 +00002271
Daniel Dunbarafec1f52009-03-29 18:40:18 +00002272 if (Args.hasArg(options::OPT_coverage)) {
2273 CmdArgs.push_back("-fprofile-arcs");
2274 CmdArgs.push_back("-ftest-coverage");
2275 }
2276
2277 if (types::isCXX(Inputs[0].getType()))
2278 CmdArgs.push_back("-D__private_extern__=extern");
2279}
2280
2281void darwin::CC1::AddCPPOptionsArgs(const ArgList &Args, ArgStringList &CmdArgs,
2282 const InputInfoList &Inputs,
2283 const ArgStringList &OutputArgs) const {
2284 // Derived from cpp_options
2285 AddCPPUniqueOptionsArgs(Args, CmdArgs, Inputs);
Daniel Dunbard067f7f2009-04-08 23:54:23 +00002286
Daniel Dunbarafec1f52009-03-29 18:40:18 +00002287 CmdArgs.append(OutputArgs.begin(), OutputArgs.end());
2288
2289 AddCC1Args(Args, CmdArgs);
2290
2291 // NOTE: The code below has some commonality with cpp_options, but
2292 // in classic gcc style ends up sending things in different
2293 // orders. This may be a good merge candidate once we drop pedantic
2294 // compatibility.
2295
2296 Args.AddAllArgs(CmdArgs, options::OPT_m_Group);
Daniel Dunbard067f7f2009-04-08 23:54:23 +00002297 Args.AddAllArgs(CmdArgs, options::OPT_std_EQ, options::OPT_ansi,
Daniel Dunbarafec1f52009-03-29 18:40:18 +00002298 options::OPT_trigraphs);
Daniel Dunbar72a60902009-04-26 01:10:38 +00002299 if (!Args.getLastArg(options::OPT_std_EQ, options::OPT_ansi)) {
2300 // Honor -std-default.
2301 Args.AddAllArgsTranslated(CmdArgs, options::OPT_std_default_EQ,
2302 "-std=", /*Joined=*/true);
2303 }
Daniel Dunbarafec1f52009-03-29 18:40:18 +00002304 Args.AddAllArgs(CmdArgs, options::OPT_W_Group, options::OPT_pedantic_Group);
2305 Args.AddLastArg(CmdArgs, options::OPT_w);
Daniel Dunbard067f7f2009-04-08 23:54:23 +00002306
Daniel Dunbarafec1f52009-03-29 18:40:18 +00002307 // The driver treats -fsyntax-only specially.
2308 Args.AddAllArgs(CmdArgs, options::OPT_f_Group, options::OPT_fsyntax_only);
2309
2310 if (Args.hasArg(options::OPT_g_Group) && !Args.hasArg(options::OPT_g0) &&
2311 !Args.hasArg(options::OPT_fno_working_directory))
2312 CmdArgs.push_back("-fworking-directory");
2313
2314 Args.AddAllArgs(CmdArgs, options::OPT_O);
2315 Args.AddAllArgs(CmdArgs, options::OPT_undef);
2316 if (Args.hasArg(options::OPT_save_temps))
2317 CmdArgs.push_back("-fpch-preprocess");
2318}
2319
Daniel Dunbard067f7f2009-04-08 23:54:23 +00002320void darwin::CC1::AddCPPUniqueOptionsArgs(const ArgList &Args,
Daniel Dunbarafec1f52009-03-29 18:40:18 +00002321 ArgStringList &CmdArgs,
Mike Stump11289f42009-09-09 15:08:12 +00002322 const InputInfoList &Inputs) const {
Daniel Dunbar083edf72009-12-21 18:54:17 +00002323 const Driver &D = getToolChain().getDriver();
Daniel Dunbarafec1f52009-03-29 18:40:18 +00002324
Daniel Dunbar64198ef2009-09-10 01:21:05 +00002325 CheckPreprocessingOptions(D, Args);
2326
Daniel Dunbarafec1f52009-03-29 18:40:18 +00002327 // Derived from cpp_unique_options.
Daniel Dunbar64198ef2009-09-10 01:21:05 +00002328 // -{C,CC} only with -E is checked in CheckPreprocessingOptions().
2329 Args.AddLastArg(CmdArgs, options::OPT_C);
2330 Args.AddLastArg(CmdArgs, options::OPT_CC);
Daniel Dunbarafec1f52009-03-29 18:40:18 +00002331 if (!Args.hasArg(options::OPT_Q))
2332 CmdArgs.push_back("-quiet");
2333 Args.AddAllArgs(CmdArgs, options::OPT_nostdinc);
Douglas Gregor64b046f2010-03-24 20:13:48 +00002334 Args.AddAllArgs(CmdArgs, options::OPT_nostdincxx);
Daniel Dunbarafec1f52009-03-29 18:40:18 +00002335 Args.AddLastArg(CmdArgs, options::OPT_v);
2336 Args.AddAllArgs(CmdArgs, options::OPT_I_Group, options::OPT_F);
2337 Args.AddLastArg(CmdArgs, options::OPT_P);
2338
2339 // FIXME: Handle %I properly.
2340 if (getToolChain().getArchName() == "x86_64") {
2341 CmdArgs.push_back("-imultilib");
2342 CmdArgs.push_back("x86_64");
2343 }
2344
2345 if (Args.hasArg(options::OPT_MD)) {
2346 CmdArgs.push_back("-MD");
Daniel Dunbar52e96cc2009-03-30 00:34:04 +00002347 CmdArgs.push_back(darwin::CC1::getDependencyFileName(Args, Inputs));
Daniel Dunbarafec1f52009-03-29 18:40:18 +00002348 }
2349
2350 if (Args.hasArg(options::OPT_MMD)) {
2351 CmdArgs.push_back("-MMD");
Daniel Dunbar52e96cc2009-03-30 00:34:04 +00002352 CmdArgs.push_back(darwin::CC1::getDependencyFileName(Args, Inputs));
Daniel Dunbarafec1f52009-03-29 18:40:18 +00002353 }
2354
2355 Args.AddLastArg(CmdArgs, options::OPT_M);
2356 Args.AddLastArg(CmdArgs, options::OPT_MM);
2357 Args.AddAllArgs(CmdArgs, options::OPT_MF);
2358 Args.AddLastArg(CmdArgs, options::OPT_MG);
2359 Args.AddLastArg(CmdArgs, options::OPT_MP);
2360 Args.AddAllArgs(CmdArgs, options::OPT_MQ);
2361 Args.AddAllArgs(CmdArgs, options::OPT_MT);
2362 if (!Args.hasArg(options::OPT_M) && !Args.hasArg(options::OPT_MM) &&
2363 (Args.hasArg(options::OPT_MD) || Args.hasArg(options::OPT_MMD))) {
2364 if (Arg *OutputOpt = Args.getLastArg(options::OPT_o)) {
2365 CmdArgs.push_back("-MQ");
2366 CmdArgs.push_back(OutputOpt->getValue(Args));
2367 }
2368 }
2369
2370 Args.AddLastArg(CmdArgs, options::OPT_remap);
2371 if (Args.hasArg(options::OPT_g3))
2372 CmdArgs.push_back("-dD");
2373 Args.AddLastArg(CmdArgs, options::OPT_H);
2374
2375 AddCPPArgs(Args, CmdArgs);
2376
2377 Args.AddAllArgs(CmdArgs, options::OPT_D, options::OPT_U, options::OPT_A);
2378 Args.AddAllArgs(CmdArgs, options::OPT_i_Group);
2379
2380 for (InputInfoList::const_iterator
2381 it = Inputs.begin(), ie = Inputs.end(); it != ie; ++it) {
2382 const InputInfo &II = *it;
Daniel Dunbard067f7f2009-04-08 23:54:23 +00002383
Daniel Dunbarb440f562010-08-02 02:38:21 +00002384 CmdArgs.push_back(II.getFilename());
Daniel Dunbarafec1f52009-03-29 18:40:18 +00002385 }
2386
2387 Args.AddAllArgValues(CmdArgs, options::OPT_Wp_COMMA,
2388 options::OPT_Xpreprocessor);
2389
2390 if (Args.hasArg(options::OPT_fmudflap)) {
2391 CmdArgs.push_back("-D_MUDFLAP");
2392 CmdArgs.push_back("-include");
2393 CmdArgs.push_back("mf-runtime.h");
2394 }
2395
2396 if (Args.hasArg(options::OPT_fmudflapth)) {
2397 CmdArgs.push_back("-D_MUDFLAP");
2398 CmdArgs.push_back("-D_MUDFLAPTH");
2399 CmdArgs.push_back("-include");
2400 CmdArgs.push_back("mf-runtime.h");
2401 }
2402}
2403
Daniel Dunbard067f7f2009-04-08 23:54:23 +00002404void darwin::CC1::AddCPPArgs(const ArgList &Args,
Daniel Dunbarafec1f52009-03-29 18:40:18 +00002405 ArgStringList &CmdArgs) const {
2406 // Derived from cpp spec.
2407
2408 if (Args.hasArg(options::OPT_static)) {
2409 // The gcc spec is broken here, it refers to dynamic but
2410 // that has been translated. Start by being bug compatible.
Daniel Dunbard067f7f2009-04-08 23:54:23 +00002411
Daniel Dunbarafec1f52009-03-29 18:40:18 +00002412 // if (!Args.hasArg(arglist.parser.dynamicOption))
2413 CmdArgs.push_back("-D__STATIC__");
2414 } else
2415 CmdArgs.push_back("-D__DYNAMIC__");
2416
2417 if (Args.hasArg(options::OPT_pthread))
2418 CmdArgs.push_back("-D_REENTRANT");
2419}
2420
Daniel Dunbare6adeee2009-03-29 17:08:39 +00002421void darwin::Preprocess::ConstructJob(Compilation &C, const JobAction &JA,
Daniel Dunbar7fbaf532010-08-02 02:38:28 +00002422 const InputInfo &Output,
Daniel Dunbard067f7f2009-04-08 23:54:23 +00002423 const InputInfoList &Inputs,
2424 const ArgList &Args,
Daniel Dunbare6adeee2009-03-29 17:08:39 +00002425 const char *LinkingOutput) const {
2426 ArgStringList CmdArgs;
2427
2428 assert(Inputs.size() == 1 && "Unexpected number of inputs!");
2429
2430 CmdArgs.push_back("-E");
2431
2432 if (Args.hasArg(options::OPT_traditional) ||
Daniel Dunbare6adeee2009-03-29 17:08:39 +00002433 Args.hasArg(options::OPT_traditional_cpp))
2434 CmdArgs.push_back("-traditional-cpp");
Daniel Dunbard067f7f2009-04-08 23:54:23 +00002435
Daniel Dunbare6adeee2009-03-29 17:08:39 +00002436 ArgStringList OutputArgs;
Daniel Dunbarb440f562010-08-02 02:38:21 +00002437 assert(Output.isFilename() && "Unexpected CC1 output.");
2438 OutputArgs.push_back("-o");
2439 OutputArgs.push_back(Output.getFilename());
Daniel Dunbare6adeee2009-03-29 17:08:39 +00002440
Joerg Sonnenbergerb86f5f42011-03-06 23:31:01 +00002441 if (Args.hasArg(options::OPT_E) || getToolChain().getDriver().CCCIsCPP) {
Daniel Dunbarf64f5302009-03-29 22:27:40 +00002442 AddCPPOptionsArgs(Args, CmdArgs, Inputs, OutputArgs);
2443 } else {
2444 AddCPPOptionsArgs(Args, CmdArgs, Inputs, ArgStringList());
2445 CmdArgs.append(OutputArgs.begin(), OutputArgs.end());
2446 }
Daniel Dunbard067f7f2009-04-08 23:54:23 +00002447
Daniel Dunbar6a8803a2009-04-03 01:27:06 +00002448 Args.AddAllArgs(CmdArgs, options::OPT_d_Group);
2449
Daniel Dunbare6adeee2009-03-29 17:08:39 +00002450 const char *CC1Name = getCC1Name(Inputs[0].getType());
Daniel Dunbard067f7f2009-04-08 23:54:23 +00002451 const char *Exec =
Daniel Dunbar9c3ed5f2010-07-14 18:46:23 +00002452 Args.MakeArgString(getToolChain().GetProgramPath(CC1Name));
Daniel Dunbar7fbaf532010-08-02 02:38:28 +00002453 C.addCommand(new Command(JA, *this, Exec, CmdArgs));
Daniel Dunbare6adeee2009-03-29 17:08:39 +00002454}
2455
2456void darwin::Compile::ConstructJob(Compilation &C, const JobAction &JA,
Daniel Dunbar7fbaf532010-08-02 02:38:28 +00002457 const InputInfo &Output,
Daniel Dunbard067f7f2009-04-08 23:54:23 +00002458 const InputInfoList &Inputs,
2459 const ArgList &Args,
Daniel Dunbare6adeee2009-03-29 17:08:39 +00002460 const char *LinkingOutput) const {
Daniel Dunbar083edf72009-12-21 18:54:17 +00002461 const Driver &D = getToolChain().getDriver();
Daniel Dunbare6adeee2009-03-29 17:08:39 +00002462 ArgStringList CmdArgs;
2463
2464 assert(Inputs.size() == 1 && "Unexpected number of inputs!");
2465
2466 types::ID InputType = Inputs[0].getType();
2467 const Arg *A;
Eli Friedmanbb0d9a52009-07-14 21:58:17 +00002468 if ((A = Args.getLastArg(options::OPT_traditional)))
Daniel Dunbare6adeee2009-03-29 17:08:39 +00002469 D.Diag(clang::diag::err_drv_argument_only_allowed_with)
2470 << A->getAsString(Args) << "-E";
2471
Daniel Dunbar24e52992010-06-07 23:28:45 +00002472 if (JA.getType() == types::TY_LLVM_IR ||
2473 JA.getType() == types::TY_LTO_IR)
Daniel Dunbare6adeee2009-03-29 17:08:39 +00002474 CmdArgs.push_back("-emit-llvm");
Daniel Dunbar24e52992010-06-07 23:28:45 +00002475 else if (JA.getType() == types::TY_LLVM_BC ||
2476 JA.getType() == types::TY_LTO_BC)
Daniel Dunbare6adeee2009-03-29 17:08:39 +00002477 CmdArgs.push_back("-emit-llvm-bc");
Daniel Dunbar6cdf83c2009-09-01 16:57:46 +00002478 else if (Output.getType() == types::TY_AST)
2479 D.Diag(clang::diag::err_drv_no_ast_support)
Daniel Dunbar1c28f1e2009-09-09 22:32:48 +00002480 << getToolChain().getTripleString();
Daniel Dunbarbcd554f2010-02-11 17:33:45 +00002481 else if (JA.getType() != types::TY_PP_Asm &&
2482 JA.getType() != types::TY_PCH)
Daniel Dunbaraeea8ac2010-02-11 03:16:21 +00002483 D.Diag(clang::diag::err_drv_invalid_gcc_output_type)
2484 << getTypeName(JA.getType());
Daniel Dunbare6adeee2009-03-29 17:08:39 +00002485
2486 ArgStringList OutputArgs;
2487 if (Output.getType() != types::TY_PCH) {
2488 OutputArgs.push_back("-o");
Daniel Dunbarb440f562010-08-02 02:38:21 +00002489 if (Output.isNothing())
Daniel Dunbare6adeee2009-03-29 17:08:39 +00002490 OutputArgs.push_back("/dev/null");
2491 else
2492 OutputArgs.push_back(Output.getFilename());
2493 }
2494
2495 // There is no need for this level of compatibility, but it makes
2496 // diffing easier.
2497 bool OutputArgsEarly = (Args.hasArg(options::OPT_fsyntax_only) ||
2498 Args.hasArg(options::OPT_S));
2499
2500 if (types::getPreprocessedType(InputType) != types::TY_INVALID) {
Daniel Dunbarafec1f52009-03-29 18:40:18 +00002501 AddCPPUniqueOptionsArgs(Args, CmdArgs, Inputs);
Daniel Dunbare6adeee2009-03-29 17:08:39 +00002502 if (OutputArgsEarly) {
2503 AddCC1OptionsArgs(Args, CmdArgs, Inputs, OutputArgs);
2504 } else {
2505 AddCC1OptionsArgs(Args, CmdArgs, Inputs, ArgStringList());
2506 CmdArgs.append(OutputArgs.begin(), OutputArgs.end());
2507 }
2508 } else {
2509 CmdArgs.push_back("-fpreprocessed");
Daniel Dunbard067f7f2009-04-08 23:54:23 +00002510
Daniel Dunbare6adeee2009-03-29 17:08:39 +00002511 for (InputInfoList::const_iterator
2512 it = Inputs.begin(), ie = Inputs.end(); it != ie; ++it) {
2513 const InputInfo &II = *it;
2514
Daniel Dunbar6cdf83c2009-09-01 16:57:46 +00002515 // Reject AST inputs.
2516 if (II.getType() == types::TY_AST) {
2517 D.Diag(clang::diag::err_drv_no_ast_support)
Daniel Dunbar1c28f1e2009-09-09 22:32:48 +00002518 << getToolChain().getTripleString();
Daniel Dunbar6cdf83c2009-09-01 16:57:46 +00002519 return;
2520 }
2521
Daniel Dunbarb440f562010-08-02 02:38:21 +00002522 CmdArgs.push_back(II.getFilename());
Daniel Dunbare6adeee2009-03-29 17:08:39 +00002523 }
2524
2525 if (OutputArgsEarly) {
2526 AddCC1OptionsArgs(Args, CmdArgs, Inputs, OutputArgs);
2527 } else {
2528 AddCC1OptionsArgs(Args, CmdArgs, Inputs, ArgStringList());
2529 CmdArgs.append(OutputArgs.begin(), OutputArgs.end());
2530 }
2531 }
Daniel Dunbard067f7f2009-04-08 23:54:23 +00002532
Daniel Dunbare6adeee2009-03-29 17:08:39 +00002533 if (Output.getType() == types::TY_PCH) {
2534 assert(Output.isFilename() && "Invalid PCH output.");
2535
2536 CmdArgs.push_back("-o");
2537 // NOTE: gcc uses a temp .s file for this, but there doesn't seem
2538 // to be a good reason.
2539 CmdArgs.push_back("/dev/null");
Daniel Dunbard067f7f2009-04-08 23:54:23 +00002540
Daniel Dunbarf4b39e92009-11-21 02:31:29 +00002541 CmdArgs.push_back("--output-pch=");
Daniel Dunbare6adeee2009-03-29 17:08:39 +00002542 CmdArgs.push_back(Output.getFilename());
Daniel Dunbard067f7f2009-04-08 23:54:23 +00002543 }
Daniel Dunbare6adeee2009-03-29 17:08:39 +00002544
2545 const char *CC1Name = getCC1Name(Inputs[0].getType());
Daniel Dunbard067f7f2009-04-08 23:54:23 +00002546 const char *Exec =
Daniel Dunbar9c3ed5f2010-07-14 18:46:23 +00002547 Args.MakeArgString(getToolChain().GetProgramPath(CC1Name));
Daniel Dunbar7fbaf532010-08-02 02:38:28 +00002548 C.addCommand(new Command(JA, *this, Exec, CmdArgs));
Daniel Dunbare6adeee2009-03-29 17:08:39 +00002549}
2550
Daniel Dunbarbe220842009-03-20 16:06:39 +00002551void darwin::Assemble::ConstructJob(Compilation &C, const JobAction &JA,
Daniel Dunbar7fbaf532010-08-02 02:38:28 +00002552 const InputInfo &Output,
Daniel Dunbard067f7f2009-04-08 23:54:23 +00002553 const InputInfoList &Inputs,
2554 const ArgList &Args,
Daniel Dunbarbe220842009-03-20 16:06:39 +00002555 const char *LinkingOutput) const {
2556 ArgStringList CmdArgs;
2557
2558 assert(Inputs.size() == 1 && "Unexpected number of inputs.");
2559 const InputInfo &Input = Inputs[0];
2560
2561 // Bit of a hack, this is only used for original inputs.
Daniel Dunbard067f7f2009-04-08 23:54:23 +00002562 //
Daniel Dunbar5c9c1182009-04-01 00:27:44 +00002563 // FIXME: This is broken for preprocessed .s inputs.
Daniel Dunbarbe220842009-03-20 16:06:39 +00002564 if (Input.isFilename() &&
Daniel Dunbar5c9c1182009-04-01 00:27:44 +00002565 strcmp(Input.getFilename(), Input.getBaseInput()) == 0) {
2566 if (Args.hasArg(options::OPT_gstabs))
2567 CmdArgs.push_back("--gstabs");
2568 else if (Args.hasArg(options::OPT_g_Group))
2569 CmdArgs.push_back("--gdwarf2");
2570 }
Daniel Dunbard067f7f2009-04-08 23:54:23 +00002571
Daniel Dunbarbe220842009-03-20 16:06:39 +00002572 // Derived from asm spec.
Daniel Dunbar3571dd92009-09-09 18:36:27 +00002573 AddDarwinArch(Args, CmdArgs);
Daniel Dunbarbe220842009-03-20 16:06:39 +00002574
Daniel Dunbar6d484762010-07-22 01:47:22 +00002575 // Use -force_cpusubtype_ALL on x86 by default.
2576 if (getToolChain().getTriple().getArch() == llvm::Triple::x86 ||
2577 getToolChain().getTriple().getArch() == llvm::Triple::x86_64 ||
Daniel Dunbar3571dd92009-09-09 18:36:27 +00002578 Args.hasArg(options::OPT_force__cpusubtype__ALL))
2579 CmdArgs.push_back("-force_cpusubtype_ALL");
2580
Daniel Dunbara6b4a3d2009-08-24 22:26:16 +00002581 if (getToolChain().getTriple().getArch() != llvm::Triple::x86_64 &&
2582 (Args.hasArg(options::OPT_mkernel) ||
Daniel Dunbarbe220842009-03-20 16:06:39 +00002583 Args.hasArg(options::OPT_static) ||
Daniel Dunbara6b4a3d2009-08-24 22:26:16 +00002584 Args.hasArg(options::OPT_fapple_kext)))
Daniel Dunbard067f7f2009-04-08 23:54:23 +00002585 CmdArgs.push_back("-static");
2586
Daniel Dunbarbe220842009-03-20 16:06:39 +00002587 Args.AddAllArgValues(CmdArgs, options::OPT_Wa_COMMA,
2588 options::OPT_Xassembler);
2589
2590 assert(Output.isFilename() && "Unexpected lipo output.");
2591 CmdArgs.push_back("-o");
2592 CmdArgs.push_back(Output.getFilename());
2593
Daniel Dunbarb440f562010-08-02 02:38:21 +00002594 assert(Input.isFilename() && "Invalid input.");
2595 CmdArgs.push_back(Input.getFilename());
Daniel Dunbarbe220842009-03-20 16:06:39 +00002596
2597 // asm_final spec is empty.
2598
Daniel Dunbard067f7f2009-04-08 23:54:23 +00002599 const char *Exec =
Daniel Dunbar9c3ed5f2010-07-14 18:46:23 +00002600 Args.MakeArgString(getToolChain().GetProgramPath("as"));
Daniel Dunbar7fbaf532010-08-02 02:38:28 +00002601 C.addCommand(new Command(JA, *this, Exec, CmdArgs));
Daniel Dunbarbe220842009-03-20 16:06:39 +00002602}
Daniel Dunbar64ed5e32009-03-20 00:52:38 +00002603
Daniel Dunbare9ded432009-09-09 18:36:20 +00002604void darwin::DarwinTool::AddDarwinArch(const ArgList &Args,
2605 ArgStringList &CmdArgs) const {
Daniel Dunbardcc3b652010-01-22 02:04:58 +00002606 llvm::StringRef ArchName = getDarwinToolChain().getDarwinArchName(Args);
2607
Daniel Dunbarc1964212009-03-26 16:23:12 +00002608 // Derived from darwin_arch spec.
2609 CmdArgs.push_back("-arch");
Daniel Dunbardcc3b652010-01-22 02:04:58 +00002610 CmdArgs.push_back(Args.MakeArgString(ArchName));
Daniel Dunbar91dbfd62009-09-04 18:35:31 +00002611
Daniel Dunbardcc3b652010-01-22 02:04:58 +00002612 // FIXME: Is this needed anymore?
2613 if (ArchName == "arm")
Daniel Dunbar91dbfd62009-09-04 18:35:31 +00002614 CmdArgs.push_back("-force_cpusubtype_ALL");
Daniel Dunbarc1964212009-03-26 16:23:12 +00002615}
2616
Daniel Dunbarccbc4522010-09-09 21:51:05 +00002617void darwin::Link::AddLinkArgs(Compilation &C,
2618 const ArgList &Args,
Daniel Dunbarc1964212009-03-26 16:23:12 +00002619 ArgStringList &CmdArgs) const {
Daniel Dunbar083edf72009-12-21 18:54:17 +00002620 const Driver &D = getToolChain().getDriver();
Daniel Dunbarc1964212009-03-26 16:23:12 +00002621
Daniel Dunbarcacb0e22010-08-11 23:07:50 +00002622 unsigned Version[3] = { 0, 0, 0 };
2623 if (Arg *A = Args.getLastArg(options::OPT_mlinker_version_EQ)) {
2624 bool HadExtra;
2625 if (!Driver::GetReleaseVersion(A->getValue(Args), Version[0],
2626 Version[1], Version[2], HadExtra) ||
2627 HadExtra)
2628 D.Diag(clang::diag::err_drv_invalid_version_number)
2629 << A->getAsString(Args);
2630 }
2631
2632 // Newer linkers support -demangle, pass it if supported and not disabled by
2633 // the user.
Daniel Dunbar6d776eb2010-11-19 17:51:40 +00002634 //
2635 // FIXME: We temporarily avoid passing -demangle to any iOS linker, because
2636 // unfortunately we can't be guaranteed that the linker version used there
2637 // will match the linker version detected at configure time. We need the
2638 // universal driver.
2639 if (Version[0] >= 100 && !Args.hasArg(options::OPT_Z_Xlinker__no_demangle) &&
2640 !getDarwinToolChain().isTargetIPhoneOS()) {
Daniel Dunbar1eaf5c62010-09-07 17:07:49 +00002641 // Don't pass -demangle to ld_classic.
2642 //
2643 // FIXME: This is a temporary workaround, ld should be handling this.
2644 bool UsesLdClassic = (getToolChain().getArch() == llvm::Triple::x86 &&
2645 Args.hasArg(options::OPT_static));
Daniel Dunbar3d7e0e22010-09-07 17:50:41 +00002646 if (getToolChain().getArch() == llvm::Triple::x86) {
2647 for (arg_iterator it = Args.filtered_begin(options::OPT_Xlinker,
2648 options::OPT_Wl_COMMA),
2649 ie = Args.filtered_end(); it != ie; ++it) {
2650 const Arg *A = *it;
2651 for (unsigned i = 0, e = A->getNumValues(); i != e; ++i)
2652 if (llvm::StringRef(A->getValue(Args, i)) == "-kext")
2653 UsesLdClassic = true;
2654 }
2655 }
Daniel Dunbar1eaf5c62010-09-07 17:07:49 +00002656 if (!UsesLdClassic)
2657 CmdArgs.push_back("-demangle");
Daniel Dunbarcacb0e22010-08-11 23:07:50 +00002658 }
2659
Daniel Dunbarc1964212009-03-26 16:23:12 +00002660 // Derived from the "link" spec.
2661 Args.AddAllArgs(CmdArgs, options::OPT_static);
2662 if (!Args.hasArg(options::OPT_static))
2663 CmdArgs.push_back("-dynamic");
2664 if (Args.hasArg(options::OPT_fgnu_runtime)) {
2665 // FIXME: gcc replaces -lobjc in forward args with -lobjc-gnu
2666 // here. How do we wish to handle such things?
2667 }
Daniel Dunbard067f7f2009-04-08 23:54:23 +00002668
Daniel Dunbarc1964212009-03-26 16:23:12 +00002669 if (!Args.hasArg(options::OPT_dynamiclib)) {
Daniel Dunbara48823f2010-01-22 02:04:52 +00002670 AddDarwinArch(Args, CmdArgs);
Daniel Dunbara48823f2010-01-22 02:04:52 +00002671 // FIXME: Why do this only on this path?
Daniel Dunbar93d7acf2010-01-22 03:37:33 +00002672 Args.AddLastArg(CmdArgs, options::OPT_force__cpusubtype__ALL);
Daniel Dunbarc1964212009-03-26 16:23:12 +00002673
2674 Args.AddLastArg(CmdArgs, options::OPT_bundle);
2675 Args.AddAllArgs(CmdArgs, options::OPT_bundle__loader);
2676 Args.AddAllArgs(CmdArgs, options::OPT_client__name);
2677
2678 Arg *A;
2679 if ((A = Args.getLastArg(options::OPT_compatibility__version)) ||
2680 (A = Args.getLastArg(options::OPT_current__version)) ||
2681 (A = Args.getLastArg(options::OPT_install__name)))
2682 D.Diag(clang::diag::err_drv_argument_only_allowed_with)
2683 << A->getAsString(Args) << "-dynamiclib";
2684
2685 Args.AddLastArg(CmdArgs, options::OPT_force__flat__namespace);
2686 Args.AddLastArg(CmdArgs, options::OPT_keep__private__externs);
2687 Args.AddLastArg(CmdArgs, options::OPT_private__bundle);
2688 } else {
2689 CmdArgs.push_back("-dylib");
2690
2691 Arg *A;
2692 if ((A = Args.getLastArg(options::OPT_bundle)) ||
2693 (A = Args.getLastArg(options::OPT_bundle__loader)) ||
2694 (A = Args.getLastArg(options::OPT_client__name)) ||
2695 (A = Args.getLastArg(options::OPT_force__flat__namespace)) ||
2696 (A = Args.getLastArg(options::OPT_keep__private__externs)) ||
2697 (A = Args.getLastArg(options::OPT_private__bundle)))
2698 D.Diag(clang::diag::err_drv_argument_not_allowed_with)
2699 << A->getAsString(Args) << "-dynamiclib";
Daniel Dunbard067f7f2009-04-08 23:54:23 +00002700
Daniel Dunbarc1964212009-03-26 16:23:12 +00002701 Args.AddAllArgsTranslated(CmdArgs, options::OPT_compatibility__version,
2702 "-dylib_compatibility_version");
2703 Args.AddAllArgsTranslated(CmdArgs, options::OPT_current__version,
2704 "-dylib_current_version");
2705
Daniel Dunbara48823f2010-01-22 02:04:52 +00002706 AddDarwinArch(Args, CmdArgs);
Daniel Dunbarc1964212009-03-26 16:23:12 +00002707
2708 Args.AddAllArgsTranslated(CmdArgs, options::OPT_install__name,
2709 "-dylib_install_name");
2710 }
2711
2712 Args.AddLastArg(CmdArgs, options::OPT_all__load);
2713 Args.AddAllArgs(CmdArgs, options::OPT_allowable__client);
2714 Args.AddLastArg(CmdArgs, options::OPT_bind__at__load);
Daniel Dunbar15c89422010-01-27 00:56:37 +00002715 if (getDarwinToolChain().isTargetIPhoneOS())
Daniel Dunbar2b5f6812009-09-04 18:35:41 +00002716 Args.AddLastArg(CmdArgs, options::OPT_arch__errors__fatal);
Daniel Dunbarc1964212009-03-26 16:23:12 +00002717 Args.AddLastArg(CmdArgs, options::OPT_dead__strip);
2718 Args.AddLastArg(CmdArgs, options::OPT_no__dead__strip__inits__and__terms);
2719 Args.AddAllArgs(CmdArgs, options::OPT_dylib__file);
2720 Args.AddLastArg(CmdArgs, options::OPT_dynamic);
2721 Args.AddAllArgs(CmdArgs, options::OPT_exported__symbols__list);
2722 Args.AddLastArg(CmdArgs, options::OPT_flat__namespace);
2723 Args.AddAllArgs(CmdArgs, options::OPT_headerpad__max__install__names);
2724 Args.AddAllArgs(CmdArgs, options::OPT_image__base);
2725 Args.AddAllArgs(CmdArgs, options::OPT_init);
2726
Daniel Dunbar19afd612010-01-26 03:56:40 +00002727 // Adding all arguments doesn't make sense here but this is what gcc does. One
2728 // of this should always be present thanks to argument translation.
2729 assert((Args.hasArg(options::OPT_mmacosx_version_min_EQ) ||
2730 Args.hasArg(options::OPT_miphoneos_version_min_EQ)) &&
2731 "Missing version argument (lost in translation)?");
Daniel Dunbar84e727f2009-09-04 18:35:21 +00002732 Args.AddAllArgsTranslated(CmdArgs, options::OPT_mmacosx_version_min_EQ,
Daniel Dunbar1c7577c2009-11-04 06:24:38 +00002733 "-macosx_version_min");
Daniel Dunbarc1964212009-03-26 16:23:12 +00002734 Args.AddAllArgsTranslated(CmdArgs, options::OPT_miphoneos_version_min_EQ,
2735 "-iphoneos_version_min");
2736 Args.AddLastArg(CmdArgs, options::OPT_nomultidefs);
2737 Args.AddLastArg(CmdArgs, options::OPT_multi__module);
2738 Args.AddLastArg(CmdArgs, options::OPT_single__module);
2739 Args.AddAllArgs(CmdArgs, options::OPT_multiply__defined);
2740 Args.AddAllArgs(CmdArgs, options::OPT_multiply__defined__unused);
Daniel Dunbard067f7f2009-04-08 23:54:23 +00002741
Daniel Dunbaraf68a882010-07-13 23:31:40 +00002742 if (const Arg *A = Args.getLastArg(options::OPT_fpie, options::OPT_fPIE,
2743 options::OPT_fno_pie,
2744 options::OPT_fno_PIE)) {
2745 if (A->getOption().matches(options::OPT_fpie) ||
2746 A->getOption().matches(options::OPT_fPIE))
2747 CmdArgs.push_back("-pie");
2748 else
2749 CmdArgs.push_back("-no_pie");
2750 }
Daniel Dunbarc1964212009-03-26 16:23:12 +00002751
2752 Args.AddLastArg(CmdArgs, options::OPT_prebind);
2753 Args.AddLastArg(CmdArgs, options::OPT_noprebind);
2754 Args.AddLastArg(CmdArgs, options::OPT_nofixprebinding);
2755 Args.AddLastArg(CmdArgs, options::OPT_prebind__all__twolevel__modules);
2756 Args.AddLastArg(CmdArgs, options::OPT_read__only__relocs);
2757 Args.AddAllArgs(CmdArgs, options::OPT_sectcreate);
2758 Args.AddAllArgs(CmdArgs, options::OPT_sectorder);
2759 Args.AddAllArgs(CmdArgs, options::OPT_seg1addr);
2760 Args.AddAllArgs(CmdArgs, options::OPT_segprot);
2761 Args.AddAllArgs(CmdArgs, options::OPT_segaddr);
2762 Args.AddAllArgs(CmdArgs, options::OPT_segs__read__only__addr);
2763 Args.AddAllArgs(CmdArgs, options::OPT_segs__read__write__addr);
2764 Args.AddAllArgs(CmdArgs, options::OPT_seg__addr__table);
2765 Args.AddAllArgs(CmdArgs, options::OPT_seg__addr__table__filename);
2766 Args.AddAllArgs(CmdArgs, options::OPT_sub__library);
2767 Args.AddAllArgs(CmdArgs, options::OPT_sub__umbrella);
Daniel Dunbar2b5f6812009-09-04 18:35:41 +00002768
Daniel Dunbarc1964212009-03-26 16:23:12 +00002769 Args.AddAllArgsTranslated(CmdArgs, options::OPT_isysroot, "-syslibroot");
Daniel Dunbar15c89422010-01-27 00:56:37 +00002770 if (getDarwinToolChain().isTargetIPhoneOS()) {
Daniel Dunbar2b5f6812009-09-04 18:35:41 +00002771 if (!Args.hasArg(options::OPT_isysroot)) {
2772 CmdArgs.push_back("-syslibroot");
2773 CmdArgs.push_back("/Developer/SDKs/Extra");
2774 }
2775 }
2776
Daniel Dunbarc1964212009-03-26 16:23:12 +00002777 Args.AddLastArg(CmdArgs, options::OPT_twolevel__namespace);
2778 Args.AddLastArg(CmdArgs, options::OPT_twolevel__namespace__hints);
2779 Args.AddAllArgs(CmdArgs, options::OPT_umbrella);
2780 Args.AddAllArgs(CmdArgs, options::OPT_undefined);
2781 Args.AddAllArgs(CmdArgs, options::OPT_unexported__symbols__list);
Daniel Dunbar2b5f6812009-09-04 18:35:41 +00002782 Args.AddAllArgs(CmdArgs, options::OPT_weak__reference__mismatches);
Daniel Dunbarc1964212009-03-26 16:23:12 +00002783 Args.AddLastArg(CmdArgs, options::OPT_X_Flag);
2784 Args.AddAllArgs(CmdArgs, options::OPT_y);
2785 Args.AddLastArg(CmdArgs, options::OPT_w);
2786 Args.AddAllArgs(CmdArgs, options::OPT_pagezero__size);
2787 Args.AddAllArgs(CmdArgs, options::OPT_segs__read__);
2788 Args.AddLastArg(CmdArgs, options::OPT_seglinkedit);
2789 Args.AddLastArg(CmdArgs, options::OPT_noseglinkedit);
2790 Args.AddAllArgs(CmdArgs, options::OPT_sectalign);
2791 Args.AddAllArgs(CmdArgs, options::OPT_sectobjectsymbols);
2792 Args.AddAllArgs(CmdArgs, options::OPT_segcreate);
2793 Args.AddLastArg(CmdArgs, options::OPT_whyload);
2794 Args.AddLastArg(CmdArgs, options::OPT_whatsloaded);
2795 Args.AddAllArgs(CmdArgs, options::OPT_dylinker__install__name);
2796 Args.AddLastArg(CmdArgs, options::OPT_dylinker);
2797 Args.AddLastArg(CmdArgs, options::OPT_Mach);
2798}
2799
2800void darwin::Link::ConstructJob(Compilation &C, const JobAction &JA,
Daniel Dunbar7fbaf532010-08-02 02:38:28 +00002801 const InputInfo &Output,
Daniel Dunbard067f7f2009-04-08 23:54:23 +00002802 const InputInfoList &Inputs,
2803 const ArgList &Args,
Daniel Dunbarc1964212009-03-26 16:23:12 +00002804 const char *LinkingOutput) const {
2805 assert(Output.getType() == types::TY_Image && "Invalid linker output type.");
Daniel Dunbarc09988d2009-09-08 16:39:16 +00002806
Daniel Dunbarc1964212009-03-26 16:23:12 +00002807 // The logic here is derived from gcc's behavior; most of which
2808 // comes from specs (starting with link_command). Consult gcc for
2809 // more information.
Daniel Dunbarc1964212009-03-26 16:23:12 +00002810 ArgStringList CmdArgs;
2811
2812 // I'm not sure why this particular decomposition exists in gcc, but
2813 // we follow suite for ease of comparison.
Daniel Dunbarccbc4522010-09-09 21:51:05 +00002814 AddLinkArgs(C, Args, CmdArgs);
Daniel Dunbarc1964212009-03-26 16:23:12 +00002815
Daniel Dunbarc1964212009-03-26 16:23:12 +00002816 Args.AddAllArgs(CmdArgs, options::OPT_d_Flag);
2817 Args.AddAllArgs(CmdArgs, options::OPT_s);
2818 Args.AddAllArgs(CmdArgs, options::OPT_t);
2819 Args.AddAllArgs(CmdArgs, options::OPT_Z_Flag);
2820 Args.AddAllArgs(CmdArgs, options::OPT_u_Group);
2821 Args.AddAllArgs(CmdArgs, options::OPT_A);
2822 Args.AddLastArg(CmdArgs, options::OPT_e);
2823 Args.AddAllArgs(CmdArgs, options::OPT_m_Separate);
2824 Args.AddAllArgs(CmdArgs, options::OPT_r);
2825
Daniel Dunbar767bbab2010-10-18 22:08:36 +00002826 // Forward -ObjC when either -ObjC or -ObjC++ is used, to force loading
2827 // members of static archive libraries which implement Objective-C classes or
2828 // categories.
2829 if (Args.hasArg(options::OPT_ObjC) || Args.hasArg(options::OPT_ObjCXX))
2830 CmdArgs.push_back("-ObjC");
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00002831
Daniel Dunbarc1964212009-03-26 16:23:12 +00002832 CmdArgs.push_back("-o");
2833 CmdArgs.push_back(Output.getFilename());
2834
Daniel Dunbarc1964212009-03-26 16:23:12 +00002835 if (!Args.hasArg(options::OPT_A) &&
2836 !Args.hasArg(options::OPT_nostdlib) &&
2837 !Args.hasArg(options::OPT_nostartfiles)) {
2838 // Derived from startfile spec.
2839 if (Args.hasArg(options::OPT_dynamiclib)) {
Daniel Dunbard067f7f2009-04-08 23:54:23 +00002840 // Derived from darwin_dylib1 spec.
Daniel Dunbar83608032010-01-27 00:56:56 +00002841 if (getDarwinToolChain().isTargetIPhoneOS()) {
2842 if (getDarwinToolChain().isIPhoneOSVersionLT(3, 1))
2843 CmdArgs.push_back("-ldylib1.o");
2844 } else {
Daniel Dunbar6d23b2f2010-01-27 00:57:03 +00002845 if (getDarwinToolChain().isMacosxVersionLT(10, 5))
Daniel Dunbar83608032010-01-27 00:56:56 +00002846 CmdArgs.push_back("-ldylib1.o");
Daniel Dunbar6d23b2f2010-01-27 00:57:03 +00002847 else if (getDarwinToolChain().isMacosxVersionLT(10, 6))
Daniel Dunbar83608032010-01-27 00:56:56 +00002848 CmdArgs.push_back("-ldylib1.10.5.o");
2849 }
Daniel Dunbarc1964212009-03-26 16:23:12 +00002850 } else {
2851 if (Args.hasArg(options::OPT_bundle)) {
Daniel Dunbarae8bca02009-04-01 03:17:40 +00002852 if (!Args.hasArg(options::OPT_static)) {
2853 // Derived from darwin_bundle1 spec.
Daniel Dunbar83608032010-01-27 00:56:56 +00002854 if (getDarwinToolChain().isTargetIPhoneOS()) {
2855 if (getDarwinToolChain().isIPhoneOSVersionLT(3, 1))
2856 CmdArgs.push_back("-lbundle1.o");
2857 } else {
Daniel Dunbar6d23b2f2010-01-27 00:57:03 +00002858 if (getDarwinToolChain().isMacosxVersionLT(10, 6))
Daniel Dunbar83608032010-01-27 00:56:56 +00002859 CmdArgs.push_back("-lbundle1.o");
2860 }
Daniel Dunbarae8bca02009-04-01 03:17:40 +00002861 }
Daniel Dunbarc1964212009-03-26 16:23:12 +00002862 } else {
Daniel Dunbar733b0f82011-03-01 18:49:30 +00002863 if (Args.hasArg(options::OPT_pg) &&
2864 getToolChain().SupportsProfiling()) {
Daniel Dunbarc1964212009-03-26 16:23:12 +00002865 if (Args.hasArg(options::OPT_static) ||
2866 Args.hasArg(options::OPT_object) ||
2867 Args.hasArg(options::OPT_preload)) {
2868 CmdArgs.push_back("-lgcrt0.o");
2869 } else {
2870 CmdArgs.push_back("-lgcrt1.o");
Daniel Dunbard067f7f2009-04-08 23:54:23 +00002871
Daniel Dunbarc1964212009-03-26 16:23:12 +00002872 // darwin_crt2 spec is empty.
Daniel Dunbard067f7f2009-04-08 23:54:23 +00002873 }
Daniel Dunbarc1964212009-03-26 16:23:12 +00002874 } else {
2875 if (Args.hasArg(options::OPT_static) ||
2876 Args.hasArg(options::OPT_object) ||
2877 Args.hasArg(options::OPT_preload)) {
2878 CmdArgs.push_back("-lcrt0.o");
2879 } else {
2880 // Derived from darwin_crt1 spec.
Daniel Dunbar15c89422010-01-27 00:56:37 +00002881 if (getDarwinToolChain().isTargetIPhoneOS()) {
Daniel Dunbar83608032010-01-27 00:56:56 +00002882 if (getDarwinToolChain().isIPhoneOSVersionLT(3, 1))
2883 CmdArgs.push_back("-lcrt1.o");
2884 else
2885 CmdArgs.push_back("-lcrt1.3.1.o");
Daniel Dunbar6d23b2f2010-01-27 00:57:03 +00002886 } else {
2887 if (getDarwinToolChain().isMacosxVersionLT(10, 5))
2888 CmdArgs.push_back("-lcrt1.o");
2889 else if (getDarwinToolChain().isMacosxVersionLT(10, 6))
2890 CmdArgs.push_back("-lcrt1.10.5.o");
2891 else
2892 CmdArgs.push_back("-lcrt1.10.6.o");
Daniel Dunbard067f7f2009-04-08 23:54:23 +00002893
Daniel Dunbar6d23b2f2010-01-27 00:57:03 +00002894 // darwin_crt2 spec is empty.
2895 }
Daniel Dunbarc1964212009-03-26 16:23:12 +00002896 }
2897 }
2898 }
2899 }
2900
Daniel Dunbar6d23b2f2010-01-27 00:57:03 +00002901 if (!getDarwinToolChain().isTargetIPhoneOS() &&
2902 Args.hasArg(options::OPT_shared_libgcc) &&
2903 getDarwinToolChain().isMacosxVersionLT(10, 5)) {
Daniel Dunbar1c28f1e2009-09-09 22:32:48 +00002904 const char *Str =
Daniel Dunbar9c3ed5f2010-07-14 18:46:23 +00002905 Args.MakeArgString(getToolChain().GetFilePath("crt3.o"));
Daniel Dunbar1c28f1e2009-09-09 22:32:48 +00002906 CmdArgs.push_back(Str);
Daniel Dunbarc1964212009-03-26 16:23:12 +00002907 }
2908 }
2909
2910 Args.AddAllArgs(CmdArgs, options::OPT_L);
Daniel Dunbard067f7f2009-04-08 23:54:23 +00002911
Daniel Dunbarc1964212009-03-26 16:23:12 +00002912 if (Args.hasArg(options::OPT_fopenmp))
2913 // This is more complicated in gcc...
2914 CmdArgs.push_back("-lgomp");
2915
Daniel Dunbar4c30b892009-09-18 08:14:36 +00002916 getDarwinToolChain().AddLinkSearchPathArgs(Args, CmdArgs);
Daniel Dunbard067f7f2009-04-08 23:54:23 +00002917
Daniel Dunbar54423b22010-09-17 00:24:54 +00002918 AddLinkerInputs(getToolChain(), Inputs, Args, CmdArgs);
Daniel Dunbarc1964212009-03-26 16:23:12 +00002919
2920 if (LinkingOutput) {
2921 CmdArgs.push_back("-arch_multiple");
2922 CmdArgs.push_back("-final_output");
2923 CmdArgs.push_back(LinkingOutput);
2924 }
2925
2926 if (Args.hasArg(options::OPT_fprofile_arcs) ||
2927 Args.hasArg(options::OPT_fprofile_generate) ||
2928 Args.hasArg(options::OPT_fcreate_profile) ||
2929 Args.hasArg(options::OPT_coverage))
2930 CmdArgs.push_back("-lgcov");
Daniel Dunbard067f7f2009-04-08 23:54:23 +00002931
Daniel Dunbarc1964212009-03-26 16:23:12 +00002932 if (Args.hasArg(options::OPT_fnested_functions))
2933 CmdArgs.push_back("-allow_stack_execute");
Daniel Dunbard067f7f2009-04-08 23:54:23 +00002934
Daniel Dunbarc1964212009-03-26 16:23:12 +00002935 if (!Args.hasArg(options::OPT_nostdlib) &&
2936 !Args.hasArg(options::OPT_nodefaultlibs)) {
Daniel Dunbar083edf72009-12-21 18:54:17 +00002937 if (getToolChain().getDriver().CCCIsCXX)
Daniel Dunbar3f7796f2010-09-17 01:20:05 +00002938 getToolChain().AddCXXStdlibLibArgs(Args, CmdArgs);
Daniel Dunbarad0f62b2009-04-08 06:06:21 +00002939
Daniel Dunbarc1964212009-03-26 16:23:12 +00002940 // link_ssp spec is empty.
2941
Daniel Dunbar26d482a2009-09-18 08:15:03 +00002942 // Let the tool chain choose which runtime library to link.
2943 getDarwinToolChain().AddLinkRuntimeLibArgs(Args, CmdArgs);
Daniel Dunbarc1964212009-03-26 16:23:12 +00002944 }
2945
2946 if (!Args.hasArg(options::OPT_A) &&
2947 !Args.hasArg(options::OPT_nostdlib) &&
2948 !Args.hasArg(options::OPT_nostartfiles)) {
2949 // endfile_spec is empty.
2950 }
2951
2952 Args.AddAllArgs(CmdArgs, options::OPT_T_Group);
2953 Args.AddAllArgs(CmdArgs, options::OPT_F);
2954
Daniel Dunbard067f7f2009-04-08 23:54:23 +00002955 const char *Exec =
Daniel Dunbar9c3ed5f2010-07-14 18:46:23 +00002956 Args.MakeArgString(getToolChain().GetProgramPath("ld"));
Daniel Dunbar7fbaf532010-08-02 02:38:28 +00002957 C.addCommand(new Command(JA, *this, Exec, CmdArgs));
Daniel Dunbarc1964212009-03-26 16:23:12 +00002958}
2959
Daniel Dunbar64ed5e32009-03-20 00:52:38 +00002960void darwin::Lipo::ConstructJob(Compilation &C, const JobAction &JA,
Daniel Dunbar7fbaf532010-08-02 02:38:28 +00002961 const InputInfo &Output,
Daniel Dunbard067f7f2009-04-08 23:54:23 +00002962 const InputInfoList &Inputs,
2963 const ArgList &Args,
Daniel Dunbar64ed5e32009-03-20 00:52:38 +00002964 const char *LinkingOutput) const {
2965 ArgStringList CmdArgs;
2966
2967 CmdArgs.push_back("-create");
2968 assert(Output.isFilename() && "Unexpected lipo output.");
Daniel Dunbar06686ab2009-03-24 00:24:37 +00002969
2970 CmdArgs.push_back("-output");
Daniel Dunbar64ed5e32009-03-20 00:52:38 +00002971 CmdArgs.push_back(Output.getFilename());
Daniel Dunbar06686ab2009-03-24 00:24:37 +00002972
Daniel Dunbar64ed5e32009-03-20 00:52:38 +00002973 for (InputInfoList::const_iterator
2974 it = Inputs.begin(), ie = Inputs.end(); it != ie; ++it) {
2975 const InputInfo &II = *it;
2976 assert(II.isFilename() && "Unexpected lipo input.");
2977 CmdArgs.push_back(II.getFilename());
2978 }
Daniel Dunbard067f7f2009-04-08 23:54:23 +00002979 const char *Exec =
Daniel Dunbar9c3ed5f2010-07-14 18:46:23 +00002980 Args.MakeArgString(getToolChain().GetProgramPath("lipo"));
Daniel Dunbar7fbaf532010-08-02 02:38:28 +00002981 C.addCommand(new Command(JA, *this, Exec, CmdArgs));
Daniel Dunbar64ed5e32009-03-20 00:52:38 +00002982}
Daniel Dunbar8eb473c2009-03-31 17:45:15 +00002983
Daniel Dunbar88299622010-06-04 18:28:36 +00002984void darwin::Dsymutil::ConstructJob(Compilation &C, const JobAction &JA,
Daniel Dunbar7fbaf532010-08-02 02:38:28 +00002985 const InputInfo &Output,
Daniel Dunbar88299622010-06-04 18:28:36 +00002986 const InputInfoList &Inputs,
2987 const ArgList &Args,
2988 const char *LinkingOutput) const {
2989 ArgStringList CmdArgs;
2990
2991 assert(Inputs.size() == 1 && "Unable to handle multiple inputs.");
2992 const InputInfo &Input = Inputs[0];
2993 assert(Input.isFilename() && "Unexpected dsymutil input.");
2994 CmdArgs.push_back(Input.getFilename());
2995
2996 CmdArgs.push_back("-o");
2997 CmdArgs.push_back(Output.getFilename());
2998
2999 const char *Exec =
Daniel Dunbar9c3ed5f2010-07-14 18:46:23 +00003000 Args.MakeArgString(getToolChain().GetProgramPath("dsymutil"));
Daniel Dunbar7fbaf532010-08-02 02:38:28 +00003001 C.addCommand(new Command(JA, *this, Exec, CmdArgs));
Daniel Dunbar88299622010-06-04 18:28:36 +00003002}
3003
Edward O'Callaghan856e4ff2009-08-22 01:06:46 +00003004void auroraux::Assemble::ConstructJob(Compilation &C, const JobAction &JA,
Daniel Dunbar7fbaf532010-08-02 02:38:28 +00003005 const InputInfo &Output,
Daniel Dunbar1c7577c2009-11-04 06:24:38 +00003006 const InputInfoList &Inputs,
3007 const ArgList &Args,
3008 const char *LinkingOutput) const {
Edward O'Callaghan856e4ff2009-08-22 01:06:46 +00003009 ArgStringList CmdArgs;
3010
3011 Args.AddAllArgValues(CmdArgs, options::OPT_Wa_COMMA,
3012 options::OPT_Xassembler);
3013
3014 CmdArgs.push_back("-o");
Daniel Dunbarb440f562010-08-02 02:38:21 +00003015 CmdArgs.push_back(Output.getFilename());
Edward O'Callaghan856e4ff2009-08-22 01:06:46 +00003016
3017 for (InputInfoList::const_iterator
3018 it = Inputs.begin(), ie = Inputs.end(); it != ie; ++it) {
3019 const InputInfo &II = *it;
Daniel Dunbarb440f562010-08-02 02:38:21 +00003020 CmdArgs.push_back(II.getFilename());
Edward O'Callaghan856e4ff2009-08-22 01:06:46 +00003021 }
3022
3023 const char *Exec =
Daniel Dunbar9c3ed5f2010-07-14 18:46:23 +00003024 Args.MakeArgString(getToolChain().GetProgramPath("gas"));
Daniel Dunbar7fbaf532010-08-02 02:38:28 +00003025 C.addCommand(new Command(JA, *this, Exec, CmdArgs));
Edward O'Callaghan856e4ff2009-08-22 01:06:46 +00003026}
3027
3028void auroraux::Link::ConstructJob(Compilation &C, const JobAction &JA,
Daniel Dunbar7fbaf532010-08-02 02:38:28 +00003029 const InputInfo &Output,
Daniel Dunbar1c7577c2009-11-04 06:24:38 +00003030 const InputInfoList &Inputs,
3031 const ArgList &Args,
3032 const char *LinkingOutput) const {
Edward O'Callaghan856e4ff2009-08-22 01:06:46 +00003033 ArgStringList CmdArgs;
3034
3035 if ((!Args.hasArg(options::OPT_nostdlib)) &&
Daniel Dunbar1c7577c2009-11-04 06:24:38 +00003036 (!Args.hasArg(options::OPT_shared))) {
Edward O'Callaghan856e4ff2009-08-22 01:06:46 +00003037 CmdArgs.push_back("-e");
Edward O'Callaghand8712d92009-10-15 07:44:07 +00003038 CmdArgs.push_back("_start");
Edward O'Callaghan856e4ff2009-08-22 01:06:46 +00003039 }
3040
3041 if (Args.hasArg(options::OPT_static)) {
3042 CmdArgs.push_back("-Bstatic");
Edward O'Callaghand8712d92009-10-15 07:44:07 +00003043 CmdArgs.push_back("-dn");
Edward O'Callaghan856e4ff2009-08-22 01:06:46 +00003044 } else {
Edward O'Callaghand8712d92009-10-15 07:44:07 +00003045// CmdArgs.push_back("--eh-frame-hdr");
Edward O'Callaghan856e4ff2009-08-22 01:06:46 +00003046 CmdArgs.push_back("-Bdynamic");
3047 if (Args.hasArg(options::OPT_shared)) {
3048 CmdArgs.push_back("-shared");
3049 } else {
Edward O'Callaghan7d3c2752009-10-16 19:44:18 +00003050 CmdArgs.push_back("--dynamic-linker");
Edward O'Callaghan856e4ff2009-08-22 01:06:46 +00003051 CmdArgs.push_back("/lib/ld.so.1"); // 64Bit Path /lib/amd64/ld.so.1
3052 }
3053 }
3054
Daniel Dunbarb440f562010-08-02 02:38:21 +00003055 if (Output.isFilename()) {
Edward O'Callaghan856e4ff2009-08-22 01:06:46 +00003056 CmdArgs.push_back("-o");
3057 CmdArgs.push_back(Output.getFilename());
3058 } else {
3059 assert(Output.isNothing() && "Invalid output.");
3060 }
3061
3062 if (!Args.hasArg(options::OPT_nostdlib) &&
3063 !Args.hasArg(options::OPT_nostartfiles)) {
3064 if (!Args.hasArg(options::OPT_shared)) {
Chris Lattner3e2ee142010-07-07 16:01:42 +00003065 CmdArgs.push_back(Args.MakeArgString(
Daniel Dunbar9c3ed5f2010-07-14 18:46:23 +00003066 getToolChain().GetFilePath("crt1.o")));
Chris Lattner3e2ee142010-07-07 16:01:42 +00003067 CmdArgs.push_back(Args.MakeArgString(
Daniel Dunbar9c3ed5f2010-07-14 18:46:23 +00003068 getToolChain().GetFilePath("crti.o")));
Chris Lattner3e2ee142010-07-07 16:01:42 +00003069 CmdArgs.push_back(Args.MakeArgString(
Daniel Dunbar9c3ed5f2010-07-14 18:46:23 +00003070 getToolChain().GetFilePath("crtbegin.o")));
Edward O'Callaghan856e4ff2009-08-22 01:06:46 +00003071 } else {
Chris Lattner3e2ee142010-07-07 16:01:42 +00003072 CmdArgs.push_back(Args.MakeArgString(
Daniel Dunbar9c3ed5f2010-07-14 18:46:23 +00003073 getToolChain().GetFilePath("crti.o")));
Edward O'Callaghan856e4ff2009-08-22 01:06:46 +00003074 }
Chris Lattner3e2ee142010-07-07 16:01:42 +00003075 CmdArgs.push_back(Args.MakeArgString(
Daniel Dunbar9c3ed5f2010-07-14 18:46:23 +00003076 getToolChain().GetFilePath("crtn.o")));
Edward O'Callaghan856e4ff2009-08-22 01:06:46 +00003077 }
3078
Daniel Dunbar1c7577c2009-11-04 06:24:38 +00003079 CmdArgs.push_back(Args.MakeArgString("-L/opt/gcc4/lib/gcc/"
3080 + getToolChain().getTripleString()
Daniel Dunbarb0b18612009-10-29 02:24:37 +00003081 + "/4.2.4"));
Edward O'Callaghan856e4ff2009-08-22 01:06:46 +00003082
3083 Args.AddAllArgs(CmdArgs, options::OPT_L);
3084 Args.AddAllArgs(CmdArgs, options::OPT_T_Group);
3085 Args.AddAllArgs(CmdArgs, options::OPT_e);
3086
Daniel Dunbar54423b22010-09-17 00:24:54 +00003087 AddLinkerInputs(getToolChain(), Inputs, Args, CmdArgs);
Edward O'Callaghan856e4ff2009-08-22 01:06:46 +00003088
3089 if (!Args.hasArg(options::OPT_nostdlib) &&
3090 !Args.hasArg(options::OPT_nodefaultlibs)) {
3091 // FIXME: For some reason GCC passes -lgcc before adding
3092 // the default system libraries. Just mimic this for now.
3093 CmdArgs.push_back("-lgcc");
3094
3095 if (Args.hasArg(options::OPT_pthread))
3096 CmdArgs.push_back("-pthread");
3097 if (!Args.hasArg(options::OPT_shared))
3098 CmdArgs.push_back("-lc");
3099 CmdArgs.push_back("-lgcc");
3100 }
3101
3102 if (!Args.hasArg(options::OPT_nostdlib) &&
3103 !Args.hasArg(options::OPT_nostartfiles)) {
3104 if (!Args.hasArg(options::OPT_shared))
Chris Lattner3e2ee142010-07-07 16:01:42 +00003105 CmdArgs.push_back(Args.MakeArgString(
Daniel Dunbar9c3ed5f2010-07-14 18:46:23 +00003106 getToolChain().GetFilePath("crtend.o")));
Edward O'Callaghan856e4ff2009-08-22 01:06:46 +00003107 }
3108
3109 const char *Exec =
Daniel Dunbar9c3ed5f2010-07-14 18:46:23 +00003110 Args.MakeArgString(getToolChain().GetProgramPath("ld"));
Daniel Dunbar7fbaf532010-08-02 02:38:28 +00003111 C.addCommand(new Command(JA, *this, Exec, CmdArgs));
Edward O'Callaghan856e4ff2009-08-22 01:06:46 +00003112}
3113
Daniel Dunbar10de9e62009-06-29 20:52:51 +00003114void openbsd::Assemble::ConstructJob(Compilation &C, const JobAction &JA,
Daniel Dunbar7fbaf532010-08-02 02:38:28 +00003115 const InputInfo &Output,
Daniel Dunbar10de9e62009-06-29 20:52:51 +00003116 const InputInfoList &Inputs,
3117 const ArgList &Args,
Mike Stump11289f42009-09-09 15:08:12 +00003118 const char *LinkingOutput) const {
Daniel Dunbar10de9e62009-06-29 20:52:51 +00003119 ArgStringList CmdArgs;
3120
3121 Args.AddAllArgValues(CmdArgs, options::OPT_Wa_COMMA,
3122 options::OPT_Xassembler);
3123
3124 CmdArgs.push_back("-o");
Daniel Dunbarb440f562010-08-02 02:38:21 +00003125 CmdArgs.push_back(Output.getFilename());
Daniel Dunbar10de9e62009-06-29 20:52:51 +00003126
3127 for (InputInfoList::const_iterator
3128 it = Inputs.begin(), ie = Inputs.end(); it != ie; ++it) {
3129 const InputInfo &II = *it;
Daniel Dunbarb440f562010-08-02 02:38:21 +00003130 CmdArgs.push_back(II.getFilename());
Daniel Dunbar10de9e62009-06-29 20:52:51 +00003131 }
3132
3133 const char *Exec =
Daniel Dunbar9c3ed5f2010-07-14 18:46:23 +00003134 Args.MakeArgString(getToolChain().GetProgramPath("as"));
Daniel Dunbar7fbaf532010-08-02 02:38:28 +00003135 C.addCommand(new Command(JA, *this, Exec, CmdArgs));
Daniel Dunbar10de9e62009-06-29 20:52:51 +00003136}
3137
3138void openbsd::Link::ConstructJob(Compilation &C, const JobAction &JA,
Daniel Dunbar7fbaf532010-08-02 02:38:28 +00003139 const InputInfo &Output,
Daniel Dunbar10de9e62009-06-29 20:52:51 +00003140 const InputInfoList &Inputs,
3141 const ArgList &Args,
3142 const char *LinkingOutput) const {
Daniel Dunbar083edf72009-12-21 18:54:17 +00003143 const Driver &D = getToolChain().getDriver();
Daniel Dunbar10de9e62009-06-29 20:52:51 +00003144 ArgStringList CmdArgs;
3145
Daniel Dunbara8888ac2009-08-03 01:28:59 +00003146 if ((!Args.hasArg(options::OPT_nostdlib)) &&
Daniel Dunbar1c7577c2009-11-04 06:24:38 +00003147 (!Args.hasArg(options::OPT_shared))) {
Daniel Dunbara8888ac2009-08-03 01:28:59 +00003148 CmdArgs.push_back("-e");
3149 CmdArgs.push_back("__start");
3150 }
3151
Daniel Dunbar10de9e62009-06-29 20:52:51 +00003152 if (Args.hasArg(options::OPT_static)) {
3153 CmdArgs.push_back("-Bstatic");
3154 } else {
Rafael Espindola7ba97af2010-11-11 02:17:51 +00003155 if (Args.hasArg(options::OPT_rdynamic))
3156 CmdArgs.push_back("-export-dynamic");
Daniel Dunbar10de9e62009-06-29 20:52:51 +00003157 CmdArgs.push_back("--eh-frame-hdr");
Daniel Dunbara8888ac2009-08-03 01:28:59 +00003158 CmdArgs.push_back("-Bdynamic");
Daniel Dunbar10de9e62009-06-29 20:52:51 +00003159 if (Args.hasArg(options::OPT_shared)) {
Daniel Dunbara8888ac2009-08-03 01:28:59 +00003160 CmdArgs.push_back("-shared");
Daniel Dunbar10de9e62009-06-29 20:52:51 +00003161 } else {
3162 CmdArgs.push_back("-dynamic-linker");
3163 CmdArgs.push_back("/usr/libexec/ld.so");
3164 }
3165 }
3166
Daniel Dunbarb440f562010-08-02 02:38:21 +00003167 if (Output.isFilename()) {
Daniel Dunbar10de9e62009-06-29 20:52:51 +00003168 CmdArgs.push_back("-o");
3169 CmdArgs.push_back(Output.getFilename());
3170 } else {
3171 assert(Output.isNothing() && "Invalid output.");
3172 }
3173
3174 if (!Args.hasArg(options::OPT_nostdlib) &&
3175 !Args.hasArg(options::OPT_nostartfiles)) {
3176 if (!Args.hasArg(options::OPT_shared)) {
Chris Lattner3e2ee142010-07-07 16:01:42 +00003177 CmdArgs.push_back(Args.MakeArgString(
Daniel Dunbar9c3ed5f2010-07-14 18:46:23 +00003178 getToolChain().GetFilePath("crt0.o")));
Chris Lattner3e2ee142010-07-07 16:01:42 +00003179 CmdArgs.push_back(Args.MakeArgString(
Daniel Dunbar9c3ed5f2010-07-14 18:46:23 +00003180 getToolChain().GetFilePath("crtbegin.o")));
Daniel Dunbar10de9e62009-06-29 20:52:51 +00003181 } else {
Chris Lattner3e2ee142010-07-07 16:01:42 +00003182 CmdArgs.push_back(Args.MakeArgString(
Daniel Dunbar9c3ed5f2010-07-14 18:46:23 +00003183 getToolChain().GetFilePath("crtbeginS.o")));
Daniel Dunbar10de9e62009-06-29 20:52:51 +00003184 }
3185 }
3186
Edward O'Callaghan5c521462009-10-28 15:13:08 +00003187 std::string Triple = getToolChain().getTripleString();
3188 if (Triple.substr(0, 6) == "x86_64")
Daniel Dunbar1c7577c2009-11-04 06:24:38 +00003189 Triple.replace(0, 6, "amd64");
Daniel Dunbarb0b18612009-10-29 02:24:37 +00003190 CmdArgs.push_back(Args.MakeArgString("-L/usr/lib/gcc-lib/" + Triple +
Daniel Dunbarea3813f2010-08-01 23:13:54 +00003191 "/4.2.1"));
Daniel Dunbara8888ac2009-08-03 01:28:59 +00003192
Daniel Dunbar10de9e62009-06-29 20:52:51 +00003193 Args.AddAllArgs(CmdArgs, options::OPT_L);
3194 Args.AddAllArgs(CmdArgs, options::OPT_T_Group);
3195 Args.AddAllArgs(CmdArgs, options::OPT_e);
3196
Daniel Dunbar54423b22010-09-17 00:24:54 +00003197 AddLinkerInputs(getToolChain(), Inputs, Args, CmdArgs);
Daniel Dunbar10de9e62009-06-29 20:52:51 +00003198
3199 if (!Args.hasArg(options::OPT_nostdlib) &&
3200 !Args.hasArg(options::OPT_nodefaultlibs)) {
Daniel Dunbarea3813f2010-08-01 23:13:54 +00003201 if (D.CCCIsCXX) {
Daniel Dunbar3f7796f2010-09-17 01:20:05 +00003202 getToolChain().AddCXXStdlibLibArgs(Args, CmdArgs);
Daniel Dunbarea3813f2010-08-01 23:13:54 +00003203 CmdArgs.push_back("-lm");
3204 }
3205
Daniel Dunbara8888ac2009-08-03 01:28:59 +00003206 // FIXME: For some reason GCC passes -lgcc before adding
3207 // the default system libraries. Just mimic this for now.
3208 CmdArgs.push_back("-lgcc");
Daniel Dunbar10de9e62009-06-29 20:52:51 +00003209
3210 if (Args.hasArg(options::OPT_pthread))
Chris Lattnerd0257f72011-02-21 18:36:51 +00003211 CmdArgs.push_back("-lpthread");
Daniel Dunbara8888ac2009-08-03 01:28:59 +00003212 if (!Args.hasArg(options::OPT_shared))
3213 CmdArgs.push_back("-lc");
3214 CmdArgs.push_back("-lgcc");
Daniel Dunbar10de9e62009-06-29 20:52:51 +00003215 }
3216
3217 if (!Args.hasArg(options::OPT_nostdlib) &&
3218 !Args.hasArg(options::OPT_nostartfiles)) {
3219 if (!Args.hasArg(options::OPT_shared))
Chris Lattner3e2ee142010-07-07 16:01:42 +00003220 CmdArgs.push_back(Args.MakeArgString(
Daniel Dunbar9c3ed5f2010-07-14 18:46:23 +00003221 getToolChain().GetFilePath("crtend.o")));
Daniel Dunbar10de9e62009-06-29 20:52:51 +00003222 else
Chris Lattner3e2ee142010-07-07 16:01:42 +00003223 CmdArgs.push_back(Args.MakeArgString(
Daniel Dunbar9c3ed5f2010-07-14 18:46:23 +00003224 getToolChain().GetFilePath("crtendS.o")));
Daniel Dunbar10de9e62009-06-29 20:52:51 +00003225 }
3226
3227 const char *Exec =
Daniel Dunbar9c3ed5f2010-07-14 18:46:23 +00003228 Args.MakeArgString(getToolChain().GetProgramPath("ld"));
Daniel Dunbar7fbaf532010-08-02 02:38:28 +00003229 C.addCommand(new Command(JA, *this, Exec, CmdArgs));
Daniel Dunbar10de9e62009-06-29 20:52:51 +00003230}
Ed Schoutene33194b2009-04-02 19:13:12 +00003231
Daniel Dunbar8eb473c2009-03-31 17:45:15 +00003232void freebsd::Assemble::ConstructJob(Compilation &C, const JobAction &JA,
Daniel Dunbar7fbaf532010-08-02 02:38:28 +00003233 const InputInfo &Output,
Daniel Dunbard067f7f2009-04-08 23:54:23 +00003234 const InputInfoList &Inputs,
3235 const ArgList &Args,
Mike Stump11289f42009-09-09 15:08:12 +00003236 const char *LinkingOutput) const {
Daniel Dunbar8eb473c2009-03-31 17:45:15 +00003237 ArgStringList CmdArgs;
3238
Daniel Dunbard854c8d2009-04-01 19:36:32 +00003239 // When building 32-bit code on FreeBSD/amd64, we have to explicitly
3240 // instruct as in the base system to assemble 32-bit code.
3241 if (getToolChain().getArchName() == "i386")
Daniel Dunbar8eb473c2009-03-31 17:45:15 +00003242 CmdArgs.push_back("--32");
3243
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00003244
Eric Christopher0b26a612010-03-02 02:41:08 +00003245 // Set byte order explicitly
3246 if (getToolChain().getArchName() == "mips")
3247 CmdArgs.push_back("-EB");
3248 else if (getToolChain().getArchName() == "mipsel")
3249 CmdArgs.push_back("-EL");
3250
Daniel Dunbar8eb473c2009-03-31 17:45:15 +00003251 Args.AddAllArgValues(CmdArgs, options::OPT_Wa_COMMA,
3252 options::OPT_Xassembler);
3253
3254 CmdArgs.push_back("-o");
Daniel Dunbarb440f562010-08-02 02:38:21 +00003255 CmdArgs.push_back(Output.getFilename());
Daniel Dunbar8eb473c2009-03-31 17:45:15 +00003256
3257 for (InputInfoList::const_iterator
3258 it = Inputs.begin(), ie = Inputs.end(); it != ie; ++it) {
3259 const InputInfo &II = *it;
Daniel Dunbarb440f562010-08-02 02:38:21 +00003260 CmdArgs.push_back(II.getFilename());
Daniel Dunbar8eb473c2009-03-31 17:45:15 +00003261 }
3262
Daniel Dunbard067f7f2009-04-08 23:54:23 +00003263 const char *Exec =
Daniel Dunbar9c3ed5f2010-07-14 18:46:23 +00003264 Args.MakeArgString(getToolChain().GetProgramPath("as"));
Daniel Dunbar7fbaf532010-08-02 02:38:28 +00003265 C.addCommand(new Command(JA, *this, Exec, CmdArgs));
Daniel Dunbar8eb473c2009-03-31 17:45:15 +00003266}
Daniel Dunbard854c8d2009-04-01 19:36:32 +00003267
3268void freebsd::Link::ConstructJob(Compilation &C, const JobAction &JA,
Daniel Dunbar7fbaf532010-08-02 02:38:28 +00003269 const InputInfo &Output,
Daniel Dunbard067f7f2009-04-08 23:54:23 +00003270 const InputInfoList &Inputs,
3271 const ArgList &Args,
Daniel Dunbare3e263f2009-05-02 20:14:53 +00003272 const char *LinkingOutput) const {
Daniel Dunbar083edf72009-12-21 18:54:17 +00003273 const Driver &D = getToolChain().getDriver();
Daniel Dunbard854c8d2009-04-01 19:36:32 +00003274 ArgStringList CmdArgs;
3275
Joerg Sonnenberger6165ab12011-03-21 13:51:29 +00003276 if (!D.SysRoot.empty())
3277 CmdArgs.push_back(Args.MakeArgString("--sysroot=" + D.SysRoot));
3278
Daniel Dunbard854c8d2009-04-01 19:36:32 +00003279 if (Args.hasArg(options::OPT_static)) {
3280 CmdArgs.push_back("-Bstatic");
3281 } else {
Rafael Espindola7ba97af2010-11-11 02:17:51 +00003282 if (Args.hasArg(options::OPT_rdynamic))
3283 CmdArgs.push_back("-export-dynamic");
Daniel Dunbard854c8d2009-04-01 19:36:32 +00003284 CmdArgs.push_back("--eh-frame-hdr");
3285 if (Args.hasArg(options::OPT_shared)) {
3286 CmdArgs.push_back("-Bshareable");
3287 } else {
3288 CmdArgs.push_back("-dynamic-linker");
3289 CmdArgs.push_back("/libexec/ld-elf.so.1");
3290 }
3291 }
3292
3293 // When building 32-bit code on FreeBSD/amd64, we have to explicitly
3294 // instruct ld in the base system to link 32-bit code.
3295 if (getToolChain().getArchName() == "i386") {
3296 CmdArgs.push_back("-m");
3297 CmdArgs.push_back("elf_i386_fbsd");
3298 }
3299
Daniel Dunbarb440f562010-08-02 02:38:21 +00003300 if (Output.isFilename()) {
Daniel Dunbard854c8d2009-04-01 19:36:32 +00003301 CmdArgs.push_back("-o");
3302 CmdArgs.push_back(Output.getFilename());
3303 } else {
3304 assert(Output.isNothing() && "Invalid output.");
3305 }
3306
3307 if (!Args.hasArg(options::OPT_nostdlib) &&
3308 !Args.hasArg(options::OPT_nostartfiles)) {
3309 if (!Args.hasArg(options::OPT_shared)) {
Roman Divacky66f22762011-02-10 16:59:40 +00003310 if (Args.hasArg(options::OPT_pg))
3311 CmdArgs.push_back(Args.MakeArgString(
3312 getToolChain().GetFilePath("gcrt1.o")));
3313 else
3314 CmdArgs.push_back(Args.MakeArgString(
3315 getToolChain().GetFilePath("crt1.o")));
Chris Lattner3e2ee142010-07-07 16:01:42 +00003316 CmdArgs.push_back(Args.MakeArgString(
Daniel Dunbar9c3ed5f2010-07-14 18:46:23 +00003317 getToolChain().GetFilePath("crti.o")));
Chris Lattner3e2ee142010-07-07 16:01:42 +00003318 CmdArgs.push_back(Args.MakeArgString(
Daniel Dunbar9c3ed5f2010-07-14 18:46:23 +00003319 getToolChain().GetFilePath("crtbegin.o")));
Daniel Dunbard854c8d2009-04-01 19:36:32 +00003320 } else {
Chris Lattner3e2ee142010-07-07 16:01:42 +00003321 CmdArgs.push_back(Args.MakeArgString(
Daniel Dunbar9c3ed5f2010-07-14 18:46:23 +00003322 getToolChain().GetFilePath("crti.o")));
Chris Lattner3e2ee142010-07-07 16:01:42 +00003323 CmdArgs.push_back(Args.MakeArgString(
Daniel Dunbar9c3ed5f2010-07-14 18:46:23 +00003324 getToolChain().GetFilePath("crtbeginS.o")));
Daniel Dunbard854c8d2009-04-01 19:36:32 +00003325 }
3326 }
3327
3328 Args.AddAllArgs(CmdArgs, options::OPT_L);
Roman Divackyee8188a2011-03-01 17:53:14 +00003329 const ToolChain::path_list Paths = getToolChain().getFilePaths();
3330 for (ToolChain::path_list::const_iterator i = Paths.begin(), e = Paths.end();
3331 i != e; ++i)
3332 CmdArgs.push_back(Args.MakeArgString(llvm::StringRef("-L") + *i));
Daniel Dunbard854c8d2009-04-01 19:36:32 +00003333 Args.AddAllArgs(CmdArgs, options::OPT_T_Group);
3334 Args.AddAllArgs(CmdArgs, options::OPT_e);
David Chisnall589a4942010-08-15 22:58:12 +00003335 Args.AddAllArgs(CmdArgs, options::OPT_s);
3336 Args.AddAllArgs(CmdArgs, options::OPT_t);
3337 Args.AddAllArgs(CmdArgs, options::OPT_Z_Flag);
3338 Args.AddAllArgs(CmdArgs, options::OPT_r);
Daniel Dunbard854c8d2009-04-01 19:36:32 +00003339
Daniel Dunbar54423b22010-09-17 00:24:54 +00003340 AddLinkerInputs(getToolChain(), Inputs, Args, CmdArgs);
Daniel Dunbard854c8d2009-04-01 19:36:32 +00003341
3342 if (!Args.hasArg(options::OPT_nostdlib) &&
3343 !Args.hasArg(options::OPT_nodefaultlibs)) {
Daniel Dunbar4b8ef282010-02-17 08:07:51 +00003344 if (D.CCCIsCXX) {
Daniel Dunbar3f7796f2010-09-17 01:20:05 +00003345 getToolChain().AddCXXStdlibLibArgs(Args, CmdArgs);
Roman Divacky66f22762011-02-10 16:59:40 +00003346 if (Args.hasArg(options::OPT_pg))
3347 CmdArgs.push_back("-lm_p");
3348 else
3349 CmdArgs.push_back("-lm");
Daniel Dunbar4b8ef282010-02-17 08:07:51 +00003350 }
Daniel Dunbard854c8d2009-04-01 19:36:32 +00003351 // FIXME: For some reason GCC passes -lgcc and -lgcc_s before adding
3352 // the default system libraries. Just mimic this for now.
Roman Divacky66f22762011-02-10 16:59:40 +00003353 if (Args.hasArg(options::OPT_pg))
3354 CmdArgs.push_back("-lgcc_p");
3355 else
3356 CmdArgs.push_back("-lgcc");
Daniel Dunbard854c8d2009-04-01 19:36:32 +00003357 if (Args.hasArg(options::OPT_static)) {
3358 CmdArgs.push_back("-lgcc_eh");
Roman Divacky66f22762011-02-10 16:59:40 +00003359 } else if (Args.hasArg(options::OPT_pg)) {
3360 CmdArgs.push_back("-lgcc_eh_p");
Daniel Dunbard854c8d2009-04-01 19:36:32 +00003361 } else {
3362 CmdArgs.push_back("--as-needed");
3363 CmdArgs.push_back("-lgcc_s");
3364 CmdArgs.push_back("--no-as-needed");
3365 }
3366
Matt Beaumont-Gay1fe49152011-02-10 20:35:01 +00003367 if (Args.hasArg(options::OPT_pthread)) {
Roman Divacky66f22762011-02-10 16:59:40 +00003368 if (Args.hasArg(options::OPT_pg))
3369 CmdArgs.push_back("-lpthread_p");
3370 else
3371 CmdArgs.push_back("-lpthread");
Matt Beaumont-Gay1fe49152011-02-10 20:35:01 +00003372 }
Daniel Dunbard854c8d2009-04-01 19:36:32 +00003373
Roman Divacky66f22762011-02-10 16:59:40 +00003374 if (Args.hasArg(options::OPT_pg)) {
3375 if (Args.hasArg(options::OPT_shared))
3376 CmdArgs.push_back("-lc");
3377 else
3378 CmdArgs.push_back("-lc_p");
3379 CmdArgs.push_back("-lgcc_p");
3380 } else {
3381 CmdArgs.push_back("-lc");
3382 CmdArgs.push_back("-lgcc");
3383 }
3384
Daniel Dunbard854c8d2009-04-01 19:36:32 +00003385 if (Args.hasArg(options::OPT_static)) {
3386 CmdArgs.push_back("-lgcc_eh");
Roman Divacky66f22762011-02-10 16:59:40 +00003387 } else if (Args.hasArg(options::OPT_pg)) {
3388 CmdArgs.push_back("-lgcc_eh_p");
Daniel Dunbard854c8d2009-04-01 19:36:32 +00003389 } else {
3390 CmdArgs.push_back("--as-needed");
3391 CmdArgs.push_back("-lgcc_s");
3392 CmdArgs.push_back("--no-as-needed");
3393 }
3394 }
3395
3396 if (!Args.hasArg(options::OPT_nostdlib) &&
3397 !Args.hasArg(options::OPT_nostartfiles)) {
3398 if (!Args.hasArg(options::OPT_shared))
Daniel Dunbar9c3ed5f2010-07-14 18:46:23 +00003399 CmdArgs.push_back(Args.MakeArgString(getToolChain().GetFilePath(
Chris Lattner3e2ee142010-07-07 16:01:42 +00003400 "crtend.o")));
Daniel Dunbard854c8d2009-04-01 19:36:32 +00003401 else
Daniel Dunbar9c3ed5f2010-07-14 18:46:23 +00003402 CmdArgs.push_back(Args.MakeArgString(getToolChain().GetFilePath(
Chris Lattner3e2ee142010-07-07 16:01:42 +00003403 "crtendS.o")));
Daniel Dunbar9c3ed5f2010-07-14 18:46:23 +00003404 CmdArgs.push_back(Args.MakeArgString(getToolChain().GetFilePath(
Chris Lattner3e2ee142010-07-07 16:01:42 +00003405 "crtn.o")));
Daniel Dunbard854c8d2009-04-01 19:36:32 +00003406 }
3407
Daniel Dunbard067f7f2009-04-08 23:54:23 +00003408 const char *Exec =
Daniel Dunbar9c3ed5f2010-07-14 18:46:23 +00003409 Args.MakeArgString(getToolChain().GetProgramPath("ld"));
Daniel Dunbar7fbaf532010-08-02 02:38:28 +00003410 C.addCommand(new Command(JA, *this, Exec, CmdArgs));
Daniel Dunbard854c8d2009-04-01 19:36:32 +00003411}
Daniel Dunbarcc912342009-05-02 18:28:39 +00003412
Benjamin Kramer24f1d3e2011-02-02 18:59:27 +00003413void netbsd::Assemble::ConstructJob(Compilation &C, const JobAction &JA,
3414 const InputInfo &Output,
3415 const InputInfoList &Inputs,
3416 const ArgList &Args,
3417 const char *LinkingOutput) const {
3418 ArgStringList CmdArgs;
3419
3420 // When building 32-bit code on NetBSD/amd64, we have to explicitly
3421 // instruct as in the base system to assemble 32-bit code.
3422 if (getToolChain().getArchName() == "i386")
3423 CmdArgs.push_back("--32");
3424
3425
3426 // Set byte order explicitly
3427 if (getToolChain().getArchName() == "mips")
3428 CmdArgs.push_back("-EB");
3429 else if (getToolChain().getArchName() == "mipsel")
3430 CmdArgs.push_back("-EL");
3431
3432 Args.AddAllArgValues(CmdArgs, options::OPT_Wa_COMMA,
3433 options::OPT_Xassembler);
3434
3435 CmdArgs.push_back("-o");
3436 CmdArgs.push_back(Output.getFilename());
3437
3438 for (InputInfoList::const_iterator
3439 it = Inputs.begin(), ie = Inputs.end(); it != ie; ++it) {
3440 const InputInfo &II = *it;
3441 CmdArgs.push_back(II.getFilename());
3442 }
3443
Joerg Sonnenbergerfcc3ec92011-03-21 14:01:40 +00003444 const char *Exec = Args.MakeArgString(FindTargetProgramPath(getToolChain(),
3445 "as"));
Benjamin Kramer24f1d3e2011-02-02 18:59:27 +00003446 C.addCommand(new Command(JA, *this, Exec, CmdArgs));
3447}
3448
3449void netbsd::Link::ConstructJob(Compilation &C, const JobAction &JA,
3450 const InputInfo &Output,
3451 const InputInfoList &Inputs,
3452 const ArgList &Args,
3453 const char *LinkingOutput) const {
3454 const Driver &D = getToolChain().getDriver();
3455 ArgStringList CmdArgs;
3456
Joerg Sonnenberger6165ab12011-03-21 13:51:29 +00003457 if (!D.SysRoot.empty())
3458 CmdArgs.push_back(Args.MakeArgString("--sysroot=" + D.SysRoot));
3459
Benjamin Kramer24f1d3e2011-02-02 18:59:27 +00003460 if (Args.hasArg(options::OPT_static)) {
3461 CmdArgs.push_back("-Bstatic");
3462 } else {
3463 if (Args.hasArg(options::OPT_rdynamic))
3464 CmdArgs.push_back("-export-dynamic");
3465 CmdArgs.push_back("--eh-frame-hdr");
3466 if (Args.hasArg(options::OPT_shared)) {
3467 CmdArgs.push_back("-Bshareable");
3468 } else {
3469 CmdArgs.push_back("-dynamic-linker");
3470 CmdArgs.push_back("/libexec/ld.elf_so");
3471 }
3472 }
3473
3474 // When building 32-bit code on NetBSD/amd64, we have to explicitly
3475 // instruct ld in the base system to link 32-bit code.
3476 if (getToolChain().getArchName() == "i386") {
3477 CmdArgs.push_back("-m");
3478 CmdArgs.push_back("elf_i386");
3479 }
3480
3481 if (Output.isFilename()) {
3482 CmdArgs.push_back("-o");
3483 CmdArgs.push_back(Output.getFilename());
3484 } else {
3485 assert(Output.isNothing() && "Invalid output.");
3486 }
3487
3488 if (!Args.hasArg(options::OPT_nostdlib) &&
3489 !Args.hasArg(options::OPT_nostartfiles)) {
3490 if (!Args.hasArg(options::OPT_shared)) {
3491 CmdArgs.push_back(Args.MakeArgString(
3492 getToolChain().GetFilePath("crt0.o")));
3493 CmdArgs.push_back(Args.MakeArgString(
3494 getToolChain().GetFilePath("crti.o")));
3495 CmdArgs.push_back(Args.MakeArgString(
3496 getToolChain().GetFilePath("crtbegin.o")));
3497 } else {
3498 CmdArgs.push_back(Args.MakeArgString(
3499 getToolChain().GetFilePath("crti.o")));
3500 CmdArgs.push_back(Args.MakeArgString(
3501 getToolChain().GetFilePath("crtbeginS.o")));
3502 }
3503 }
3504
3505 Args.AddAllArgs(CmdArgs, options::OPT_L);
3506 Args.AddAllArgs(CmdArgs, options::OPT_T_Group);
3507 Args.AddAllArgs(CmdArgs, options::OPT_e);
3508 Args.AddAllArgs(CmdArgs, options::OPT_s);
3509 Args.AddAllArgs(CmdArgs, options::OPT_t);
3510 Args.AddAllArgs(CmdArgs, options::OPT_Z_Flag);
3511 Args.AddAllArgs(CmdArgs, options::OPT_r);
3512
3513 AddLinkerInputs(getToolChain(), Inputs, Args, CmdArgs);
3514
3515 if (!Args.hasArg(options::OPT_nostdlib) &&
3516 !Args.hasArg(options::OPT_nodefaultlibs)) {
3517 if (D.CCCIsCXX) {
3518 getToolChain().AddCXXStdlibLibArgs(Args, CmdArgs);
3519 CmdArgs.push_back("-lm");
3520 }
3521 // FIXME: For some reason GCC passes -lgcc and -lgcc_s before adding
3522 // the default system libraries. Just mimic this for now.
3523 CmdArgs.push_back("-lgcc");
3524 if (Args.hasArg(options::OPT_static)) {
3525 CmdArgs.push_back("-lgcc_eh");
3526 } else {
3527 CmdArgs.push_back("--as-needed");
3528 CmdArgs.push_back("-lgcc_s");
3529 CmdArgs.push_back("--no-as-needed");
3530 }
3531
3532 if (Args.hasArg(options::OPT_pthread))
3533 CmdArgs.push_back("-lpthread");
3534 CmdArgs.push_back("-lc");
3535
3536 CmdArgs.push_back("-lgcc");
3537 if (Args.hasArg(options::OPT_static)) {
3538 CmdArgs.push_back("-lgcc_eh");
3539 } else {
3540 CmdArgs.push_back("--as-needed");
3541 CmdArgs.push_back("-lgcc_s");
3542 CmdArgs.push_back("--no-as-needed");
3543 }
3544 }
3545
3546 if (!Args.hasArg(options::OPT_nostdlib) &&
3547 !Args.hasArg(options::OPT_nostartfiles)) {
3548 if (!Args.hasArg(options::OPT_shared))
3549 CmdArgs.push_back(Args.MakeArgString(getToolChain().GetFilePath(
3550 "crtend.o")));
3551 else
3552 CmdArgs.push_back(Args.MakeArgString(getToolChain().GetFilePath(
3553 "crtendS.o")));
3554 CmdArgs.push_back(Args.MakeArgString(getToolChain().GetFilePath(
3555 "crtn.o")));
3556 }
3557
Joerg Sonnenbergerfcc3ec92011-03-21 14:01:40 +00003558 const char *Exec = Args.MakeArgString(FindTargetProgramPath(getToolChain(),
3559 "ld"));
Benjamin Kramer24f1d3e2011-02-02 18:59:27 +00003560 C.addCommand(new Command(JA, *this, Exec, CmdArgs));
3561}
3562
Rafael Espindola92b00932010-08-10 00:25:48 +00003563void linuxtools::Assemble::ConstructJob(Compilation &C, const JobAction &JA,
3564 const InputInfo &Output,
3565 const InputInfoList &Inputs,
3566 const ArgList &Args,
3567 const char *LinkingOutput) const {
3568 ArgStringList CmdArgs;
3569
3570 // Add --32/--64 to make sure we get the format we want.
3571 // This is incomplete
3572 if (getToolChain().getArch() == llvm::Triple::x86) {
3573 CmdArgs.push_back("--32");
3574 } else if (getToolChain().getArch() == llvm::Triple::x86_64) {
3575 CmdArgs.push_back("--64");
3576 } else if (getToolChain().getArch() == llvm::Triple::arm) {
3577 llvm::StringRef MArch = getToolChain().getArchName();
3578 if (MArch == "armv7" || MArch == "armv7a" || MArch == "armv7-a")
3579 CmdArgs.push_back("-mfpu=neon");
3580 }
3581
3582 Args.AddAllArgValues(CmdArgs, options::OPT_Wa_COMMA,
3583 options::OPT_Xassembler);
3584
3585 CmdArgs.push_back("-o");
3586 CmdArgs.push_back(Output.getFilename());
3587
3588 for (InputInfoList::const_iterator
3589 it = Inputs.begin(), ie = Inputs.end(); it != ie; ++it) {
3590 const InputInfo &II = *it;
3591 CmdArgs.push_back(II.getFilename());
3592 }
3593
3594 const char *Exec =
3595 Args.MakeArgString(getToolChain().GetProgramPath("as"));
3596 C.addCommand(new Command(JA, *this, Exec, CmdArgs));
3597}
3598
Rafael Espindolac8f008f2010-11-07 20:14:31 +00003599void linuxtools::Link::ConstructJob(Compilation &C, const JobAction &JA,
3600 const InputInfo &Output,
3601 const InputInfoList &Inputs,
3602 const ArgList &Args,
3603 const char *LinkingOutput) const {
3604 const toolchains::Linux& ToolChain =
3605 static_cast<const toolchains::Linux&>(getToolChain());
3606 const Driver &D = ToolChain.getDriver();
3607 ArgStringList CmdArgs;
3608
Rafael Espindolad1002f62010-11-15 18:28:16 +00003609 // Silence warning for "clang -g foo.o -o foo"
3610 Args.ClaimAllArgs(options::OPT_g_Group);
Rafael Espindolad95a8122011-03-01 05:25:27 +00003611 // and "clang -emit-llvm foo.o -o foo"
3612 Args.ClaimAllArgs(options::OPT_emit_llvm);
Rafael Espindolaf92614c2010-11-17 20:37:10 +00003613 // and for "clang -g foo.o -o foo". Other warning options are already
3614 // handled somewhere else.
3615 Args.ClaimAllArgs(options::OPT_w);
Rafael Espindolad1002f62010-11-15 18:28:16 +00003616
Joerg Sonnenberger6165ab12011-03-21 13:51:29 +00003617 if (!D.SysRoot.empty())
3618 CmdArgs.push_back(Args.MakeArgString("--sysroot=" + D.SysRoot));
Rafael Espindolac8f008f2010-11-07 20:14:31 +00003619
Rafael Espindolad47ac232010-11-17 22:26:15 +00003620 if (Args.hasArg(options::OPT_pie))
3621 CmdArgs.push_back("-pie");
3622
Rafael Espindola1c76c592010-11-07 22:57:16 +00003623 if (Args.hasArg(options::OPT_rdynamic))
3624 CmdArgs.push_back("-export-dynamic");
3625
Rafael Espindola34d77dc2010-11-11 19:34:42 +00003626 if (Args.hasArg(options::OPT_s))
3627 CmdArgs.push_back("-s");
3628
Rafael Espindolac8f008f2010-11-07 20:14:31 +00003629 for (std::vector<std::string>::const_iterator i = ToolChain.ExtraOpts.begin(),
3630 e = ToolChain.ExtraOpts.end();
3631 i != e; ++i)
3632 CmdArgs.push_back(i->c_str());
3633
3634 if (!Args.hasArg(options::OPT_static)) {
3635 CmdArgs.push_back("--eh-frame-hdr");
3636 }
3637
3638 CmdArgs.push_back("-m");
3639 if (ToolChain.getArch() == llvm::Triple::x86)
3640 CmdArgs.push_back("elf_i386");
Douglas Gregord9bb1522011-03-06 19:11:49 +00003641 else if (ToolChain.getArch() == llvm::Triple::arm
3642 || ToolChain.getArch() == llvm::Triple::thumb)
Rafael Espindolac8f008f2010-11-07 20:14:31 +00003643 CmdArgs.push_back("armelf_linux_eabi");
3644 else
3645 CmdArgs.push_back("elf_x86_64");
3646
3647 if (Args.hasArg(options::OPT_static)) {
Douglas Gregord9bb1522011-03-06 19:11:49 +00003648 if (ToolChain.getArch() == llvm::Triple::arm
3649 || ToolChain.getArch() == llvm::Triple::thumb)
Rafael Espindolac8f008f2010-11-07 20:14:31 +00003650 CmdArgs.push_back("-Bstatic");
3651 else
3652 CmdArgs.push_back("-static");
3653 } else if (Args.hasArg(options::OPT_shared)) {
3654 CmdArgs.push_back("-shared");
3655 }
3656
3657 if (ToolChain.getArch() == llvm::Triple::arm ||
Douglas Gregord9bb1522011-03-06 19:11:49 +00003658 ToolChain.getArch() == llvm::Triple::thumb ||
Rafael Espindolac8f008f2010-11-07 20:14:31 +00003659 (!Args.hasArg(options::OPT_static) &&
3660 !Args.hasArg(options::OPT_shared))) {
3661 CmdArgs.push_back("-dynamic-linker");
3662 if (ToolChain.getArch() == llvm::Triple::x86)
3663 CmdArgs.push_back("/lib/ld-linux.so.2");
Douglas Gregord9bb1522011-03-06 19:11:49 +00003664 else if (ToolChain.getArch() == llvm::Triple::arm ||
3665 ToolChain.getArch() == llvm::Triple::thumb)
Rafael Espindolac8f008f2010-11-07 20:14:31 +00003666 CmdArgs.push_back("/lib/ld-linux.so.3");
3667 else
3668 CmdArgs.push_back("/lib64/ld-linux-x86-64.so.2");
3669 }
3670
3671 CmdArgs.push_back("-o");
3672 CmdArgs.push_back(Output.getFilename());
3673
Rafael Espindola81937ec2010-12-01 01:52:43 +00003674 if (!Args.hasArg(options::OPT_nostdlib) &&
3675 !Args.hasArg(options::OPT_nostartfiles)) {
Rafael Espindolad47ac232010-11-17 22:26:15 +00003676 const char *crt1 = NULL;
3677 if (!Args.hasArg(options::OPT_shared)){
3678 if (Args.hasArg(options::OPT_pie))
3679 crt1 = "Scrt1.o";
3680 else
3681 crt1 = "crt1.o";
3682 }
3683 if (crt1)
3684 CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath(crt1)));
Rafael Espindolac8f008f2010-11-07 20:14:31 +00003685
Rafael Espindola9aa60e92010-11-12 03:00:39 +00003686 CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath("crti.o")));
Rafael Espindolac8f008f2010-11-07 20:14:31 +00003687
Rafael Espindola9aa60e92010-11-12 03:00:39 +00003688 const char *crtbegin;
3689 if (Args.hasArg(options::OPT_static))
3690 crtbegin = "crtbeginT.o";
Rafael Espindolad47ac232010-11-17 22:26:15 +00003691 else if (Args.hasArg(options::OPT_shared) || Args.hasArg(options::OPT_pie))
Rafael Espindola9aa60e92010-11-12 03:00:39 +00003692 crtbegin = "crtbeginS.o";
3693 else
3694 crtbegin = "crtbegin.o";
3695 CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath(crtbegin)));
3696 }
Rafael Espindolac8f008f2010-11-07 20:14:31 +00003697
3698 Args.AddAllArgs(CmdArgs, options::OPT_L);
3699
3700 const ToolChain::path_list Paths = ToolChain.getFilePaths();
3701
Roman Divackyee8188a2011-03-01 17:53:14 +00003702 for (ToolChain::path_list::const_iterator i = Paths.begin(), e = Paths.end();
3703 i != e; ++i)
3704 CmdArgs.push_back(Args.MakeArgString(llvm::StringRef("-L") + *i));
Rafael Espindolac8f008f2010-11-07 20:14:31 +00003705
3706 AddLinkerInputs(ToolChain, Inputs, Args, CmdArgs);
3707
Rafael Espindola9aa60e92010-11-12 03:00:39 +00003708 if (D.CCCIsCXX && !Args.hasArg(options::OPT_nostdlib)) {
Rafael Espindolac8f008f2010-11-07 20:14:31 +00003709 ToolChain.AddCXXStdlibLibArgs(Args, CmdArgs);
3710 CmdArgs.push_back("-lm");
3711 }
3712
3713 if (Args.hasArg(options::OPT_static))
3714 CmdArgs.push_back("--start-group");
3715
Rafael Espindola9aa60e92010-11-12 03:00:39 +00003716 if (!Args.hasArg(options::OPT_nostdlib)) {
3717 if (!D.CCCIsCXX)
Rafael Espindolac8f008f2010-11-07 20:14:31 +00003718 CmdArgs.push_back("-lgcc");
Rafael Espindola9aa60e92010-11-12 03:00:39 +00003719
3720 if (Args.hasArg(options::OPT_static)) {
3721 if (D.CCCIsCXX)
3722 CmdArgs.push_back("-lgcc");
3723 } else {
3724 if (!D.CCCIsCXX)
3725 CmdArgs.push_back("--as-needed");
3726 CmdArgs.push_back("-lgcc_s");
3727 if (!D.CCCIsCXX)
3728 CmdArgs.push_back("--no-as-needed");
3729 }
3730
3731 if (Args.hasArg(options::OPT_static))
3732 CmdArgs.push_back("-lgcc_eh");
3733 else if (!Args.hasArg(options::OPT_shared) && D.CCCIsCXX)
3734 CmdArgs.push_back("-lgcc");
3735
3736 if (Args.hasArg(options::OPT_pthread) ||
3737 Args.hasArg(options::OPT_pthreads))
3738 CmdArgs.push_back("-lpthread");
3739
3740 CmdArgs.push_back("-lc");
3741
3742 if (Args.hasArg(options::OPT_static))
3743 CmdArgs.push_back("--end-group");
3744 else {
3745 if (!D.CCCIsCXX)
3746 CmdArgs.push_back("-lgcc");
3747
3748 if (!D.CCCIsCXX)
3749 CmdArgs.push_back("--as-needed");
3750 CmdArgs.push_back("-lgcc_s");
3751 if (!D.CCCIsCXX)
3752 CmdArgs.push_back("--no-as-needed");
3753
3754 if (!Args.hasArg(options::OPT_shared) && D.CCCIsCXX)
3755 CmdArgs.push_back("-lgcc");
3756 }
3757
Rafael Espindolad47ac232010-11-17 22:26:15 +00003758
Rafael Espindola81937ec2010-12-01 01:52:43 +00003759 if (!Args.hasArg(options::OPT_nostartfiles)) {
3760 const char *crtend;
3761 if (Args.hasArg(options::OPT_shared) || Args.hasArg(options::OPT_pie))
3762 crtend = "crtendS.o";
3763 else
3764 crtend = "crtend.o";
Rafael Espindola9aa60e92010-11-12 03:00:39 +00003765
Rafael Espindola81937ec2010-12-01 01:52:43 +00003766 CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath(crtend)));
3767 CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath("crtn.o")));
3768 }
Rafael Espindolac8f008f2010-11-07 20:14:31 +00003769 }
3770
Rafael Espindolac8f008f2010-11-07 20:14:31 +00003771 if (Args.hasArg(options::OPT_use_gold_plugin)) {
3772 CmdArgs.push_back("-plugin");
3773 std::string Plugin = ToolChain.getDriver().Dir + "/../lib/LLVMgold.so";
3774 CmdArgs.push_back(Args.MakeArgString(Plugin));
3775 }
3776
3777 C.addCommand(new Command(JA, *this, ToolChain.Linker.c_str(), CmdArgs));
3778}
Rafael Espindola92b00932010-08-10 00:25:48 +00003779
Chris Lattner3e2ee142010-07-07 16:01:42 +00003780void minix::Assemble::ConstructJob(Compilation &C, const JobAction &JA,
Daniel Dunbar7fbaf532010-08-02 02:38:28 +00003781 const InputInfo &Output,
3782 const InputInfoList &Inputs,
3783 const ArgList &Args,
3784 const char *LinkingOutput) const {
Chris Lattner3e2ee142010-07-07 16:01:42 +00003785 ArgStringList CmdArgs;
3786
3787 Args.AddAllArgValues(CmdArgs, options::OPT_Wa_COMMA,
3788 options::OPT_Xassembler);
3789
3790 CmdArgs.push_back("-o");
Daniel Dunbarb440f562010-08-02 02:38:21 +00003791 CmdArgs.push_back(Output.getFilename());
Chris Lattner3e2ee142010-07-07 16:01:42 +00003792
3793 for (InputInfoList::const_iterator
3794 it = Inputs.begin(), ie = Inputs.end(); it != ie; ++it) {
3795 const InputInfo &II = *it;
Daniel Dunbarb440f562010-08-02 02:38:21 +00003796 CmdArgs.push_back(II.getFilename());
Chris Lattner3e2ee142010-07-07 16:01:42 +00003797 }
3798
3799 const char *Exec =
Daniel Dunbar9c3ed5f2010-07-14 18:46:23 +00003800 Args.MakeArgString(getToolChain().GetProgramPath("gas"));
Daniel Dunbar7fbaf532010-08-02 02:38:28 +00003801 C.addCommand(new Command(JA, *this, Exec, CmdArgs));
Chris Lattner3e2ee142010-07-07 16:01:42 +00003802}
3803
3804void minix::Link::ConstructJob(Compilation &C, const JobAction &JA,
Daniel Dunbar7fbaf532010-08-02 02:38:28 +00003805 const InputInfo &Output,
3806 const InputInfoList &Inputs,
3807 const ArgList &Args,
3808 const char *LinkingOutput) const {
Chris Lattner3e2ee142010-07-07 16:01:42 +00003809 const Driver &D = getToolChain().getDriver();
3810 ArgStringList CmdArgs;
3811
Daniel Dunbarb440f562010-08-02 02:38:21 +00003812 if (Output.isFilename()) {
Chris Lattner3e2ee142010-07-07 16:01:42 +00003813 CmdArgs.push_back("-o");
3814 CmdArgs.push_back(Output.getFilename());
3815 } else {
3816 assert(Output.isNothing() && "Invalid output.");
3817 }
3818
3819 if (!Args.hasArg(options::OPT_nostdlib) &&
3820 !Args.hasArg(options::OPT_nostartfiles))
Daniel Dunbar9c3ed5f2010-07-14 18:46:23 +00003821 CmdArgs.push_back(Args.MakeArgString(getToolChain().GetFilePath(
Chris Lattner3e2ee142010-07-07 16:01:42 +00003822 "/usr/gnu/lib/crtso.o")));
3823
3824 Args.AddAllArgs(CmdArgs, options::OPT_L);
3825 Args.AddAllArgs(CmdArgs, options::OPT_T_Group);
3826 Args.AddAllArgs(CmdArgs, options::OPT_e);
3827
Daniel Dunbar54423b22010-09-17 00:24:54 +00003828 AddLinkerInputs(getToolChain(), Inputs, Args, CmdArgs);
Chris Lattner3e2ee142010-07-07 16:01:42 +00003829
3830 if (!Args.hasArg(options::OPT_nostdlib) &&
3831 !Args.hasArg(options::OPT_nodefaultlibs)) {
3832 if (D.CCCIsCXX) {
Daniel Dunbar3f7796f2010-09-17 01:20:05 +00003833 getToolChain().AddCXXStdlibLibArgs(Args, CmdArgs);
Chris Lattner3e2ee142010-07-07 16:01:42 +00003834 CmdArgs.push_back("-lm");
3835 }
3836
3837 if (Args.hasArg(options::OPT_pthread))
3838 CmdArgs.push_back("-lpthread");
3839 CmdArgs.push_back("-lc");
3840 CmdArgs.push_back("-lgcc");
3841 CmdArgs.push_back("-L/usr/gnu/lib");
3842 // FIXME: fill in the correct search path for the final
3843 // support libraries.
3844 CmdArgs.push_back("-L/usr/gnu/lib/gcc/i686-pc-minix/4.4.3");
3845 }
3846
3847 if (!Args.hasArg(options::OPT_nostdlib) &&
3848 !Args.hasArg(options::OPT_nostartfiles)) {
Daniel Dunbar9c3ed5f2010-07-14 18:46:23 +00003849 CmdArgs.push_back(Args.MakeArgString(getToolChain().GetFilePath(
Chris Lattner3e2ee142010-07-07 16:01:42 +00003850 "/usr/gnu/lib/libend.a")));
3851 }
3852
3853 const char *Exec =
Daniel Dunbar9c3ed5f2010-07-14 18:46:23 +00003854 Args.MakeArgString(getToolChain().GetProgramPath("/usr/gnu/bin/gld"));
Daniel Dunbar7fbaf532010-08-02 02:38:28 +00003855 C.addCommand(new Command(JA, *this, Exec, CmdArgs));
Chris Lattner3e2ee142010-07-07 16:01:42 +00003856}
3857
Daniel Dunbarcc912342009-05-02 18:28:39 +00003858/// DragonFly Tools
3859
3860// For now, DragonFly Assemble does just about the same as for
3861// FreeBSD, but this may change soon.
3862void dragonfly::Assemble::ConstructJob(Compilation &C, const JobAction &JA,
Daniel Dunbar7fbaf532010-08-02 02:38:28 +00003863 const InputInfo &Output,
Daniel Dunbar1c7577c2009-11-04 06:24:38 +00003864 const InputInfoList &Inputs,
3865 const ArgList &Args,
3866 const char *LinkingOutput) const {
Daniel Dunbarcc912342009-05-02 18:28:39 +00003867 ArgStringList CmdArgs;
3868
3869 // When building 32-bit code on DragonFly/pc64, we have to explicitly
3870 // instruct as in the base system to assemble 32-bit code.
3871 if (getToolChain().getArchName() == "i386")
3872 CmdArgs.push_back("--32");
3873
3874 Args.AddAllArgValues(CmdArgs, options::OPT_Wa_COMMA,
3875 options::OPT_Xassembler);
3876
3877 CmdArgs.push_back("-o");
Daniel Dunbarb440f562010-08-02 02:38:21 +00003878 CmdArgs.push_back(Output.getFilename());
Daniel Dunbarcc912342009-05-02 18:28:39 +00003879
3880 for (InputInfoList::const_iterator
3881 it = Inputs.begin(), ie = Inputs.end(); it != ie; ++it) {
3882 const InputInfo &II = *it;
Daniel Dunbarb440f562010-08-02 02:38:21 +00003883 CmdArgs.push_back(II.getFilename());
Daniel Dunbarcc912342009-05-02 18:28:39 +00003884 }
3885
3886 const char *Exec =
Daniel Dunbar9c3ed5f2010-07-14 18:46:23 +00003887 Args.MakeArgString(getToolChain().GetProgramPath("as"));
Daniel Dunbar7fbaf532010-08-02 02:38:28 +00003888 C.addCommand(new Command(JA, *this, Exec, CmdArgs));
Daniel Dunbarcc912342009-05-02 18:28:39 +00003889}
3890
3891void dragonfly::Link::ConstructJob(Compilation &C, const JobAction &JA,
Daniel Dunbar7fbaf532010-08-02 02:38:28 +00003892 const InputInfo &Output,
3893 const InputInfoList &Inputs,
3894 const ArgList &Args,
3895 const char *LinkingOutput) const {
Daniel Dunbar083edf72009-12-21 18:54:17 +00003896 const Driver &D = getToolChain().getDriver();
Daniel Dunbarcc912342009-05-02 18:28:39 +00003897 ArgStringList CmdArgs;
3898
Joerg Sonnenberger6165ab12011-03-21 13:51:29 +00003899 if (!D.SysRoot.empty())
3900 CmdArgs.push_back(Args.MakeArgString("--sysroot=" + D.SysRoot));
3901
Daniel Dunbarcc912342009-05-02 18:28:39 +00003902 if (Args.hasArg(options::OPT_static)) {
3903 CmdArgs.push_back("-Bstatic");
3904 } else {
3905 if (Args.hasArg(options::OPT_shared))
3906 CmdArgs.push_back("-Bshareable");
3907 else {
3908 CmdArgs.push_back("-dynamic-linker");
3909 CmdArgs.push_back("/usr/libexec/ld-elf.so.2");
3910 }
3911 }
3912
3913 // When building 32-bit code on DragonFly/pc64, we have to explicitly
3914 // instruct ld in the base system to link 32-bit code.
3915 if (getToolChain().getArchName() == "i386") {
3916 CmdArgs.push_back("-m");
3917 CmdArgs.push_back("elf_i386");
3918 }
3919
Daniel Dunbarb440f562010-08-02 02:38:21 +00003920 if (Output.isFilename()) {
Daniel Dunbarcc912342009-05-02 18:28:39 +00003921 CmdArgs.push_back("-o");
3922 CmdArgs.push_back(Output.getFilename());
3923 } else {
3924 assert(Output.isNothing() && "Invalid output.");
3925 }
3926
3927 if (!Args.hasArg(options::OPT_nostdlib) &&
3928 !Args.hasArg(options::OPT_nostartfiles)) {
3929 if (!Args.hasArg(options::OPT_shared)) {
Chris Lattner3e2ee142010-07-07 16:01:42 +00003930 CmdArgs.push_back(
Daniel Dunbar9c3ed5f2010-07-14 18:46:23 +00003931 Args.MakeArgString(getToolChain().GetFilePath("crt1.o")));
Chris Lattner3e2ee142010-07-07 16:01:42 +00003932 CmdArgs.push_back(
Daniel Dunbar9c3ed5f2010-07-14 18:46:23 +00003933 Args.MakeArgString(getToolChain().GetFilePath("crti.o")));
Chris Lattner3e2ee142010-07-07 16:01:42 +00003934 CmdArgs.push_back(
Daniel Dunbar9c3ed5f2010-07-14 18:46:23 +00003935 Args.MakeArgString(getToolChain().GetFilePath("crtbegin.o")));
Daniel Dunbarcc912342009-05-02 18:28:39 +00003936 } else {
Chris Lattner3e2ee142010-07-07 16:01:42 +00003937 CmdArgs.push_back(
Daniel Dunbar9c3ed5f2010-07-14 18:46:23 +00003938 Args.MakeArgString(getToolChain().GetFilePath("crti.o")));
Chris Lattner3e2ee142010-07-07 16:01:42 +00003939 CmdArgs.push_back(
Daniel Dunbar9c3ed5f2010-07-14 18:46:23 +00003940 Args.MakeArgString(getToolChain().GetFilePath("crtbeginS.o")));
Daniel Dunbarcc912342009-05-02 18:28:39 +00003941 }
3942 }
3943
3944 Args.AddAllArgs(CmdArgs, options::OPT_L);
3945 Args.AddAllArgs(CmdArgs, options::OPT_T_Group);
3946 Args.AddAllArgs(CmdArgs, options::OPT_e);
3947
Daniel Dunbar54423b22010-09-17 00:24:54 +00003948 AddLinkerInputs(getToolChain(), Inputs, Args, CmdArgs);
Daniel Dunbarcc912342009-05-02 18:28:39 +00003949
3950 if (!Args.hasArg(options::OPT_nostdlib) &&
3951 !Args.hasArg(options::OPT_nodefaultlibs)) {
3952 // FIXME: GCC passes on -lgcc, -lgcc_pic and a whole lot of
3953 // rpaths
3954 CmdArgs.push_back("-L/usr/lib/gcc41");
3955
3956 if (!Args.hasArg(options::OPT_static)) {
3957 CmdArgs.push_back("-rpath");
3958 CmdArgs.push_back("/usr/lib/gcc41");
3959
3960 CmdArgs.push_back("-rpath-link");
3961 CmdArgs.push_back("/usr/lib/gcc41");
3962
3963 CmdArgs.push_back("-rpath");
3964 CmdArgs.push_back("/usr/lib");
3965
3966 CmdArgs.push_back("-rpath-link");
3967 CmdArgs.push_back("/usr/lib");
3968 }
3969
Rafael Espindola38360b32010-07-20 12:59:03 +00003970 if (D.CCCIsCXX) {
Daniel Dunbar3f7796f2010-09-17 01:20:05 +00003971 getToolChain().AddCXXStdlibLibArgs(Args, CmdArgs);
Rafael Espindola38360b32010-07-20 12:59:03 +00003972 CmdArgs.push_back("-lm");
3973 }
3974
Daniel Dunbarcc912342009-05-02 18:28:39 +00003975 if (Args.hasArg(options::OPT_shared)) {
3976 CmdArgs.push_back("-lgcc_pic");
3977 } else {
3978 CmdArgs.push_back("-lgcc");
3979 }
3980
3981
3982 if (Args.hasArg(options::OPT_pthread))
Mike Stump0a65b632009-10-31 20:11:46 +00003983 CmdArgs.push_back("-lpthread");
Daniel Dunbarcc912342009-05-02 18:28:39 +00003984
3985 if (!Args.hasArg(options::OPT_nolibc)) {
3986 CmdArgs.push_back("-lc");
3987 }
3988
3989 if (Args.hasArg(options::OPT_shared)) {
3990 CmdArgs.push_back("-lgcc_pic");
3991 } else {
3992 CmdArgs.push_back("-lgcc");
3993 }
3994 }
3995
3996 if (!Args.hasArg(options::OPT_nostdlib) &&
3997 !Args.hasArg(options::OPT_nostartfiles)) {
3998 if (!Args.hasArg(options::OPT_shared))
Chris Lattner3e2ee142010-07-07 16:01:42 +00003999 CmdArgs.push_back(Args.MakeArgString(
Daniel Dunbar9c3ed5f2010-07-14 18:46:23 +00004000 getToolChain().GetFilePath("crtend.o")));
Daniel Dunbarcc912342009-05-02 18:28:39 +00004001 else
Chris Lattner3e2ee142010-07-07 16:01:42 +00004002 CmdArgs.push_back(Args.MakeArgString(
Daniel Dunbar9c3ed5f2010-07-14 18:46:23 +00004003 getToolChain().GetFilePath("crtendS.o")));
Chris Lattner3e2ee142010-07-07 16:01:42 +00004004 CmdArgs.push_back(Args.MakeArgString(
Daniel Dunbar9c3ed5f2010-07-14 18:46:23 +00004005 getToolChain().GetFilePath("crtn.o")));
Daniel Dunbarcc912342009-05-02 18:28:39 +00004006 }
4007
4008 const char *Exec =
Daniel Dunbar9c3ed5f2010-07-14 18:46:23 +00004009 Args.MakeArgString(getToolChain().GetProgramPath("ld"));
Daniel Dunbar7fbaf532010-08-02 02:38:28 +00004010 C.addCommand(new Command(JA, *this, Exec, CmdArgs));
Daniel Dunbarcc912342009-05-02 18:28:39 +00004011}
Michael J. Spencerb186bc32010-08-21 21:55:07 +00004012
4013void visualstudio::Link::ConstructJob(Compilation &C, const JobAction &JA,
4014 const InputInfo &Output,
4015 const InputInfoList &Inputs,
4016 const ArgList &Args,
4017 const char *LinkingOutput) const {
Michael J. Spencerb186bc32010-08-21 21:55:07 +00004018 ArgStringList CmdArgs;
4019
4020 if (Output.isFilename()) {
Daniel Dunbar2cc3f172010-09-17 00:45:02 +00004021 CmdArgs.push_back(Args.MakeArgString(std::string("-out:") +
4022 Output.getFilename()));
Michael J. Spencerb186bc32010-08-21 21:55:07 +00004023 } else {
4024 assert(Output.isNothing() && "Invalid output.");
4025 }
4026
4027 if (!Args.hasArg(options::OPT_nostdlib) &&
4028 !Args.hasArg(options::OPT_nostartfiles)) {
4029 CmdArgs.push_back("-defaultlib:libcmt");
4030 }
4031
4032 CmdArgs.push_back("-nologo");
4033
Daniel Dunbar54423b22010-09-17 00:24:54 +00004034 AddLinkerInputs(getToolChain(), Inputs, Args, CmdArgs);
Michael J. Spencerb186bc32010-08-21 21:55:07 +00004035
4036 const char *Exec =
Daniel Dunbar54423b22010-09-17 00:24:54 +00004037 Args.MakeArgString(getToolChain().GetProgramPath("link.exe"));
Michael J. Spencerb186bc32010-08-21 21:55:07 +00004038 C.addCommand(new Command(JA, *this, Exec, CmdArgs));
4039}