blob: c3c07f412f803a2ca88440cd6d3c270201dc92ae [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"
John McCall24fc0de2011-07-06 00:26:06 +000020#include "clang/Driver/ObjCRuntime.h"
Daniel Dunbara3246a02009-03-18 08:07:30 +000021#include "clang/Driver/Option.h"
Daniel Dunbarda13faf2009-11-19 04:25:22 +000022#include "clang/Driver/Options.h"
Daniel Dunbara3246a02009-03-18 08:07:30 +000023#include "clang/Driver/ToolChain.h"
Daniel Dunbar04c4c2c2009-03-18 07:06:02 +000024#include "clang/Driver/Util.h"
25
Daniel Dunbar1c28f1e2009-09-09 22:32:48 +000026#include "llvm/ADT/SmallString.h"
Douglas Gregorf7b87cb2009-10-29 00:41:01 +000027#include "llvm/ADT/StringSwitch.h"
Daniel Dunbarb4a3e432009-09-09 22:32:34 +000028#include "llvm/ADT/Twine.h"
Michael J. Spencerf6efe582011-01-10 02:34:13 +000029#include "llvm/Support/FileSystem.h"
Daniel Dunbarc1964212009-03-26 16:23:12 +000030#include "llvm/Support/Format.h"
31#include "llvm/Support/raw_ostream.h"
Michael J. Spencer8aaf4992010-11-29 18:12:39 +000032#include "llvm/Support/Host.h"
33#include "llvm/Support/Process.h"
John McCall31168b02011-06-15 23:02:42 +000034#include "llvm/Support/ErrorHandling.h"
Daniel Dunbar04c4c2c2009-03-18 07:06:02 +000035
36#include "InputInfo.h"
Daniel Dunbarc1964212009-03-26 16:23:12 +000037#include "ToolChains.h"
Daniel Dunbar04c4c2c2009-03-18 07:06:02 +000038
Dylan Noblesmith70e73a32011-04-09 13:31:59 +000039#ifdef __CYGWIN__
40#include <cygwin/version.h>
41#if defined(CYGWIN_VERSION_DLL_MAJOR) && CYGWIN_VERSION_DLL_MAJOR<1007
42#define IS_CYGWIN15 1
43#endif
44#endif
45
Daniel Dunbar1a093d22009-03-18 06:00:36 +000046using namespace clang::driver;
47using namespace clang::driver::tools;
Chris Lattner0e62c1c2011-07-23 10:55:15 +000048using namespace clang;
Daniel Dunbar1a093d22009-03-18 06:00:36 +000049
Joerg Sonnenbergerfcc3ec92011-03-21 14:01:40 +000050/// FindTargetProgramPath - Return path of the target specific version of
51/// ProgName. If it doesn't exist, return path of ProgName itself.
52static std::string FindTargetProgramPath(const ToolChain &TheToolChain,
Joerg Sonnenberger637603a2011-05-16 13:35:02 +000053 const std::string TripleString,
Joerg Sonnenbergerfcc3ec92011-03-21 14:01:40 +000054 const char *ProgName) {
Joerg Sonnenberger637603a2011-05-16 13:35:02 +000055 std::string Executable(TripleString + "-" + ProgName);
Joerg Sonnenbergerfcc3ec92011-03-21 14:01:40 +000056 std::string Path(TheToolChain.GetProgramPath(Executable.c_str()));
57 if (Path != Executable)
58 return Path;
59 return TheToolChain.GetProgramPath(ProgName);
60}
61
Daniel Dunbar64198ef2009-09-10 01:21:05 +000062/// CheckPreprocessingOptions - Perform some validation of preprocessing
63/// arguments that is shared with gcc.
64static void CheckPreprocessingOptions(const Driver &D, const ArgList &Args) {
65 if (Arg *A = Args.getLastArg(options::OPT_C, options::OPT_CC))
Joerg Sonnenbergerb86f5f42011-03-06 23:31:01 +000066 if (!Args.hasArg(options::OPT_E) && !D.CCCIsCPP)
Chris Lattner0e62c1c2011-07-23 10:55:15 +000067 D.Diag(diag::err_drv_argument_only_allowed_with)
Daniel Dunbar64198ef2009-09-10 01:21:05 +000068 << A->getAsString(Args) << "-E";
69}
70
Daniel Dunbar4eadb602009-09-10 01:21:12 +000071/// CheckCodeGenerationOptions - Perform some validation of code generation
72/// arguments that is shared with gcc.
73static void CheckCodeGenerationOptions(const Driver &D, const ArgList &Args) {
74 // In gcc, only ARM checks this, but it seems reasonable to check universally.
75 if (Args.hasArg(options::OPT_static))
76 if (const Arg *A = Args.getLastArg(options::OPT_dynamic,
77 options::OPT_mdynamic_no_pic))
Chris Lattner0e62c1c2011-07-23 10:55:15 +000078 D.Diag(diag::err_drv_argument_not_allowed_with)
Daniel Dunbar4eadb602009-09-10 01:21:12 +000079 << A->getAsString(Args) << "-static";
80}
81
Chris Lattnerbf2803f2010-03-29 17:55:58 +000082// Quote target names for inclusion in GNU Make dependency files.
83// Only the characters '$', '#', ' ', '\t' are quoted.
Chris Lattner0e62c1c2011-07-23 10:55:15 +000084static void QuoteTarget(StringRef Target,
85 SmallVectorImpl<char> &Res) {
Chris Lattnerbf2803f2010-03-29 17:55:58 +000086 for (unsigned i = 0, e = Target.size(); i != e; ++i) {
87 switch (Target[i]) {
88 case ' ':
89 case '\t':
90 // Escape the preceding backslashes
91 for (int j = i - 1; j >= 0 && Target[j] == '\\'; --j)
92 Res.push_back('\\');
93
94 // Escape the space/tab
95 Res.push_back('\\');
96 break;
97 case '$':
98 Res.push_back('$');
99 break;
100 case '#':
101 Res.push_back('\\');
102 break;
103 default:
104 break;
105 }
106
107 Res.push_back(Target[i]);
108 }
109}
110
Daniel Dunbar54423b22010-09-17 00:24:54 +0000111static void AddLinkerInputs(const ToolChain &TC,
112 const InputInfoList &Inputs, const ArgList &Args,
113 ArgStringList &CmdArgs) {
114 const Driver &D = TC.getDriver();
115
Daniel Dunbar1094bb12011-02-19 05:33:51 +0000116 // Add extra linker input arguments which are not treated as inputs
117 // (constructed via -Xarch_).
118 Args.AddAllArgValues(CmdArgs, options::OPT_Zlinker_input);
119
Daniel Dunbar54423b22010-09-17 00:24:54 +0000120 for (InputInfoList::const_iterator
121 it = Inputs.begin(), ie = Inputs.end(); it != ie; ++it) {
122 const InputInfo &II = *it;
123
124 if (!TC.HasNativeLLVMSupport()) {
125 // Don't try to pass LLVM inputs unless we have native support.
126 if (II.getType() == types::TY_LLVM_IR ||
127 II.getType() == types::TY_LTO_IR ||
128 II.getType() == types::TY_LLVM_BC ||
129 II.getType() == types::TY_LTO_BC)
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000130 D.Diag(diag::err_drv_no_linker_llvm_support)
Daniel Dunbar54423b22010-09-17 00:24:54 +0000131 << TC.getTripleString();
132 }
133
Daniel Dunbar2cc3f172010-09-17 00:45:02 +0000134 // Add filenames immediately.
135 if (II.isFilename()) {
Daniel Dunbar54423b22010-09-17 00:24:54 +0000136 CmdArgs.push_back(II.getFilename());
Daniel Dunbar2cc3f172010-09-17 00:45:02 +0000137 continue;
138 }
139
140 // Otherwise, this is a linker input argument.
141 const Arg &A = II.getInputArg();
142
143 // Handle reserved library options.
144 if (A.getOption().matches(options::OPT_Z_reserved_lib_stdcxx)) {
Daniel Dunbar3f7796f2010-09-17 01:20:05 +0000145 TC.AddCXXStdlibLibArgs(Args, CmdArgs);
Shantonu Senafeb03b2010-09-17 18:39:08 +0000146 } else if (A.getOption().matches(options::OPT_Z_reserved_lib_cckext)) {
147 TC.AddCCKextLibArgs(Args, CmdArgs);
Daniel Dunbar2cc3f172010-09-17 00:45:02 +0000148 } else
149 A.renderAsInput(Args, CmdArgs);
Daniel Dunbar54423b22010-09-17 00:24:54 +0000150 }
151}
152
John McCall31168b02011-06-15 23:02:42 +0000153/// \brief Determine whether Objective-C automated reference counting is
154/// enabled.
155static bool isObjCAutoRefCount(const ArgList &Args) {
156 return Args.hasFlag(options::OPT_fobjc_arc, options::OPT_fno_objc_arc, false);
157}
158
Rafael Espindola9d4a8cf2011-06-02 18:58:46 +0000159static void addProfileRT(const ToolChain &TC, const ArgList &Args,
Bill Wendling08760582011-06-27 19:15:03 +0000160 ArgStringList &CmdArgs,
161 llvm::Triple Triple) {
162 if (!(Args.hasArg(options::OPT_fprofile_arcs) ||
163 Args.hasArg(options::OPT_fprofile_generate) ||
164 Args.hasArg(options::OPT_fcreate_profile) ||
165 Args.hasArg(options::OPT_coverage)))
166 return;
167
168 // GCC links libgcov.a by adding -L<inst>/gcc/lib/gcc/<triple>/<ver> -lgcov to
169 // the link line. We cannot do the same thing because unlike gcov there is a
170 // libprofile_rt.so. We used to use the -l:libprofile_rt.a syntax, but that is
171 // not supported by old linkers.
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000172 Twine ProfileRT =
173 Twine(TC.getDriver().Dir) + "/../lib/" + "libprofile_rt.a";
Bill Wendling08760582011-06-27 19:15:03 +0000174
175 if (Triple.getOS() == llvm::Triple::Darwin) {
176 // On Darwin, if the static library doesn't exist try the dylib.
177 bool Exists;
178 if (llvm::sys::fs::exists(ProfileRT.str(), Exists) || !Exists)
179 ProfileRT =
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000180 Twine(TC.getDriver().Dir) + "/../lib/" + "libprofile_rt.dylib";
Rafael Espindola9d4a8cf2011-06-02 18:58:46 +0000181 }
Bill Wendling08760582011-06-27 19:15:03 +0000182
183 CmdArgs.push_back(Args.MakeArgString(ProfileRT));
Rafael Espindola9d4a8cf2011-06-02 18:58:46 +0000184}
185
Benjamin Kramer8404eb02011-09-22 21:41:16 +0000186static void AddIncludeDirectoryList(const ArgList &Args,
187 ArgStringList &CmdArgs,
188 const char *ArgName,
189 const char *DirList) {
190 if (!DirList)
191 return; // Nothing to do.
192
193 StringRef Dirs(DirList);
194 if (Dirs.empty()) // Empty string should not add '.'.
195 return;
196
197 StringRef::size_type Delim;
198 while ((Delim = Dirs.find(llvm::sys::PathSeparator)) != StringRef::npos) {
199 if (Delim == 0) { // Leading colon.
200 CmdArgs.push_back(ArgName);
201 CmdArgs.push_back(".");
202 } else {
203 CmdArgs.push_back(ArgName);
204 CmdArgs.push_back(Args.MakeArgString(Dirs.substr(0, Delim)));
205 }
206 Dirs = Dirs.substr(Delim + 1);
207 }
208
209 if (Dirs.empty()) { // Trailing colon.
210 CmdArgs.push_back(ArgName);
211 CmdArgs.push_back(".");
212 } else { // Add the last path.
213 CmdArgs.push_back(ArgName);
214 CmdArgs.push_back(Args.MakeArgString(Dirs));
215 }
216}
217
Mike Stump11289f42009-09-09 15:08:12 +0000218void Clang::AddPreprocessingOptions(const Driver &D,
Douglas Gregor111af7d2009-04-18 00:34:01 +0000219 const ArgList &Args,
Daniel Dunbard067f7f2009-04-08 23:54:23 +0000220 ArgStringList &CmdArgs,
221 const InputInfo &Output,
222 const InputInfoList &Inputs) const {
Daniel Dunbard067f7f2009-04-08 23:54:23 +0000223 Arg *A;
Daniel Dunbar367dbb92009-06-08 21:48:20 +0000224
Daniel Dunbar64198ef2009-09-10 01:21:05 +0000225 CheckPreprocessingOptions(D, Args);
226
227 Args.AddLastArg(CmdArgs, options::OPT_C);
228 Args.AddLastArg(CmdArgs, options::OPT_CC);
Daniel Dunbar367dbb92009-06-08 21:48:20 +0000229
230 // Handle dependency file generation.
Daniel Dunbar86aed7d2010-12-08 21:33:40 +0000231 if ((A = Args.getLastArg(options::OPT_M, options::OPT_MM)) ||
Daniel Dunbard067f7f2009-04-08 23:54:23 +0000232 (A = Args.getLastArg(options::OPT_MD)) ||
233 (A = Args.getLastArg(options::OPT_MMD))) {
234 // Determine the output location.
235 const char *DepFile;
236 if (Output.getType() == types::TY_Dependencies) {
Daniel Dunbarb440f562010-08-02 02:38:21 +0000237 DepFile = Output.getFilename();
Daniel Dunbard067f7f2009-04-08 23:54:23 +0000238 } else if (Arg *MF = Args.getLastArg(options::OPT_MF)) {
239 DepFile = MF->getValue(Args);
Daniel Dunbar0bfb21e2009-11-19 03:26:40 +0000240 } else if (A->getOption().matches(options::OPT_M) ||
241 A->getOption().matches(options::OPT_MM)) {
Daniel Dunbard067f7f2009-04-08 23:54:23 +0000242 DepFile = "-";
243 } else {
244 DepFile = darwin::CC1::getDependencyFileName(Args, Inputs);
245 }
246 CmdArgs.push_back("-dependency-file");
247 CmdArgs.push_back(DepFile);
248
Chris Lattnerbf2803f2010-03-29 17:55:58 +0000249 // Add a default target if one wasn't specified.
Daniel Dunbard067f7f2009-04-08 23:54:23 +0000250 if (!Args.hasArg(options::OPT_MT) && !Args.hasArg(options::OPT_MQ)) {
251 const char *DepTarget;
252
253 // If user provided -o, that is the dependency target, except
254 // when we are only generating a dependency file.
255 Arg *OutputOpt = Args.getLastArg(options::OPT_o);
256 if (OutputOpt && Output.getType() != types::TY_Dependencies) {
257 DepTarget = OutputOpt->getValue(Args);
258 } else {
259 // Otherwise derive from the base input.
260 //
261 // FIXME: This should use the computed output file location.
Michael J. Spencere1696752010-12-18 00:19:12 +0000262 llvm::SmallString<128> P(Inputs[0].getBaseInput());
263 llvm::sys::path::replace_extension(P, "o");
264 DepTarget = Args.MakeArgString(llvm::sys::path::filename(P));
Daniel Dunbard067f7f2009-04-08 23:54:23 +0000265 }
266
267 CmdArgs.push_back("-MT");
Chris Lattnerbf2803f2010-03-29 17:55:58 +0000268 llvm::SmallString<128> Quoted;
269 QuoteTarget(DepTarget, Quoted);
270 CmdArgs.push_back(Args.MakeArgString(Quoted));
Daniel Dunbard067f7f2009-04-08 23:54:23 +0000271 }
272
Daniel Dunbar0bfb21e2009-11-19 03:26:40 +0000273 if (A->getOption().matches(options::OPT_M) ||
274 A->getOption().matches(options::OPT_MD))
Daniel Dunbard067f7f2009-04-08 23:54:23 +0000275 CmdArgs.push_back("-sys-header-deps");
276 }
277
Peter Collingbourne77b0e7f22011-07-12 19:35:15 +0000278 if (Args.hasArg(options::OPT_MG)) {
279 if (!A || A->getOption().matches(options::OPT_MD) ||
280 A->getOption().matches(options::OPT_MMD))
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000281 D.Diag(diag::err_drv_mg_requires_m_or_mm);
Peter Collingbourne77b0e7f22011-07-12 19:35:15 +0000282 CmdArgs.push_back("-MG");
283 }
284
Daniel Dunbard067f7f2009-04-08 23:54:23 +0000285 Args.AddLastArg(CmdArgs, options::OPT_MP);
Chris Lattnerbf2803f2010-03-29 17:55:58 +0000286
287 // Convert all -MQ <target> args to -MT <quoted target>
288 for (arg_iterator it = Args.filtered_begin(options::OPT_MT,
289 options::OPT_MQ),
290 ie = Args.filtered_end(); it != ie; ++it) {
Daniel Dunbara442fd52010-06-11 22:00:13 +0000291 const Arg *A = *it;
292 A->claim();
Chris Lattnerbf2803f2010-03-29 17:55:58 +0000293
Daniel Dunbara442fd52010-06-11 22:00:13 +0000294 if (A->getOption().matches(options::OPT_MQ)) {
Chris Lattnerbf2803f2010-03-29 17:55:58 +0000295 CmdArgs.push_back("-MT");
296 llvm::SmallString<128> Quoted;
Daniel Dunbara442fd52010-06-11 22:00:13 +0000297 QuoteTarget(A->getValue(Args), Quoted);
Chris Lattnerbf2803f2010-03-29 17:55:58 +0000298 CmdArgs.push_back(Args.MakeArgString(Quoted));
299
300 // -MT flag - no change
301 } else {
Daniel Dunbara442fd52010-06-11 22:00:13 +0000302 A->render(Args, CmdArgs);
Chris Lattnerbf2803f2010-03-29 17:55:58 +0000303 }
304 }
Daniel Dunbard067f7f2009-04-08 23:54:23 +0000305
Douglas Gregor111af7d2009-04-18 00:34:01 +0000306 // Add -i* options, and automatically translate to
307 // -include-pch/-include-pth for transparent PCH support. It's
308 // wonky, but we include looking for .gch so we can support seamless
309 // replacement into a build system already set up to be generating
310 // .gch files.
Argyrios Kyrtzidis2f23b412010-09-30 16:53:47 +0000311 bool RenderedImplicitInclude = false;
Daniel Dunbar44b36ee2009-11-25 11:53:23 +0000312 for (arg_iterator it = Args.filtered_begin(options::OPT_clang_i_Group),
313 ie = Args.filtered_end(); it != ie; ++it) {
314 const Arg *A = it;
Daniel Dunbard067f7f2009-04-08 23:54:23 +0000315
316 if (A->getOption().matches(options::OPT_include)) {
Argyrios Kyrtzidis2f23b412010-09-30 16:53:47 +0000317 bool IsFirstImplicitInclude = !RenderedImplicitInclude;
318 RenderedImplicitInclude = true;
319
Argyrios Kyrtzidis90bdfbb2010-08-11 23:27:58 +0000320 // Use PCH if the user requested it.
Daniel Dunbarcbc34b72009-10-15 20:02:44 +0000321 bool UsePCH = D.CCCUsePCH;
Daniel Dunbarcbc34b72009-10-15 20:02:44 +0000322
Daniel Dunbard067f7f2009-04-08 23:54:23 +0000323 bool FoundPTH = false;
Douglas Gregor111af7d2009-04-18 00:34:01 +0000324 bool FoundPCH = false;
Daniel Dunbard067f7f2009-04-08 23:54:23 +0000325 llvm::sys::Path P(A->getValue(Args));
Michael J. Spencerf6efe582011-01-10 02:34:13 +0000326 bool Exists;
Daniel Dunbarcbc34b72009-10-15 20:02:44 +0000327 if (UsePCH) {
Douglas Gregor111af7d2009-04-18 00:34:01 +0000328 P.appendSuffix("pch");
Michael J. Spencerf6efe582011-01-10 02:34:13 +0000329 if (!llvm::sys::fs::exists(P.str(), Exists) && Exists)
Douglas Gregor111af7d2009-04-18 00:34:01 +0000330 FoundPCH = true;
Mike Stump11289f42009-09-09 15:08:12 +0000331 else
Douglas Gregor111af7d2009-04-18 00:34:01 +0000332 P.eraseSuffix();
Daniel Dunbard067f7f2009-04-08 23:54:23 +0000333 }
334
Douglas Gregor111af7d2009-04-18 00:34:01 +0000335 if (!FoundPCH) {
336 P.appendSuffix("pth");
Michael J. Spencerf6efe582011-01-10 02:34:13 +0000337 if (!llvm::sys::fs::exists(P.str(), Exists) && Exists)
Douglas Gregor111af7d2009-04-18 00:34:01 +0000338 FoundPTH = true;
339 else
340 P.eraseSuffix();
Mike Stump11289f42009-09-09 15:08:12 +0000341 }
342
Douglas Gregor111af7d2009-04-18 00:34:01 +0000343 if (!FoundPCH && !FoundPTH) {
344 P.appendSuffix("gch");
Michael J. Spencerf6efe582011-01-10 02:34:13 +0000345 if (!llvm::sys::fs::exists(P.str(), Exists) && Exists) {
Daniel Dunbarcbc34b72009-10-15 20:02:44 +0000346 FoundPCH = UsePCH;
347 FoundPTH = !UsePCH;
Douglas Gregor111af7d2009-04-18 00:34:01 +0000348 }
Mike Stump11289f42009-09-09 15:08:12 +0000349 else
Douglas Gregor111af7d2009-04-18 00:34:01 +0000350 P.eraseSuffix();
351 }
352
353 if (FoundPCH || FoundPTH) {
Argyrios Kyrtzidis2f23b412010-09-30 16:53:47 +0000354 if (IsFirstImplicitInclude) {
355 A->claim();
356 if (UsePCH)
357 CmdArgs.push_back("-include-pch");
358 else
359 CmdArgs.push_back("-include-pth");
360 CmdArgs.push_back(Args.MakeArgString(P.str()));
361 continue;
362 } else {
363 // Ignore the PCH if not first on command line and emit warning.
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000364 D.Diag(diag::warn_drv_pch_not_first_include)
Argyrios Kyrtzidis2f23b412010-09-30 16:53:47 +0000365 << P.str() << A->getAsString(Args);
366 }
Daniel Dunbard067f7f2009-04-08 23:54:23 +0000367 }
368 }
369
370 // Not translated, render as usual.
371 A->claim();
372 A->render(Args, CmdArgs);
373 }
374
375 Args.AddAllArgs(CmdArgs, options::OPT_D, options::OPT_U);
Douglas Gregor9f93e382011-07-28 04:45:53 +0000376 Args.AddAllArgs(CmdArgs, options::OPT_I_Group, options::OPT_F,
377 options::OPT_index_header_map);
Daniel Dunbard067f7f2009-04-08 23:54:23 +0000378
Daniel Dunbarbf11f792010-09-14 23:12:35 +0000379 // Add C++ include arguments, if needed.
380 types::ID InputType = Inputs[0].getType();
John McCall31168b02011-06-15 23:02:42 +0000381 if (types::isCXX(InputType)) {
382 bool ObjCXXAutoRefCount
383 = types::isObjC(InputType) && isObjCAutoRefCount(Args);
Eric Christopher84fbdb42011-08-19 00:30:14 +0000384 getToolChain().AddClangCXXStdlibIncludeArgs(Args, CmdArgs,
John McCall31168b02011-06-15 23:02:42 +0000385 ObjCXXAutoRefCount);
Bob Wilsonb02ea3d2011-06-21 21:12:29 +0000386 Args.AddAllArgs(CmdArgs, options::OPT_stdlib_EQ);
John McCall31168b02011-06-15 23:02:42 +0000387 }
Daniel Dunbarbf11f792010-09-14 23:12:35 +0000388
Daniel Dunbard067f7f2009-04-08 23:54:23 +0000389 // Add -Wp, and -Xassembler if using the preprocessor.
390
391 // FIXME: There is a very unfortunate problem here, some troubled
392 // souls abuse -Wp, to pass preprocessor options in gcc syntax. To
393 // really support that we would have to parse and then translate
394 // those options. :(
395 Args.AddAllArgValues(CmdArgs, options::OPT_Wp_COMMA,
396 options::OPT_Xpreprocessor);
Daniel Dunbar38b62792009-10-29 01:53:44 +0000397
398 // -I- is a deprecated GCC feature, reject it.
399 if (Arg *A = Args.getLastArg(options::OPT_I_))
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000400 D.Diag(diag::err_drv_I_dash_not_supported) << A->getAsString(Args);
Chandler Carruth24e17e12010-10-20 07:00:47 +0000401
402 // If we have a --sysroot, and don't have an explicit -isysroot flag, add an
403 // -isysroot to the CC1 invocation.
404 if (Arg *A = Args.getLastArg(options::OPT__sysroot_EQ)) {
405 if (!Args.hasArg(options::OPT_isysroot)) {
406 CmdArgs.push_back("-isysroot");
407 CmdArgs.push_back(A->getValue(Args));
408 }
409 }
Douglas Gregorf936f782011-09-14 20:28:46 +0000410
411 // If a module path was provided, pass it along. Otherwise, use a temporary
412 // directory.
413 if (Arg *A = Args.getLastArg(options::OPT_fmodule_cache_path)) {
Douglas Gregorf936f782011-09-14 20:28:46 +0000414 A->claim();
415 A->render(Args, CmdArgs);
416 } else {
417 llvm::SmallString<128> DefaultModuleCache;
418 llvm::sys::path::system_temp_directory(/*erasedOnReboot=*/false,
419 DefaultModuleCache);
420 llvm::sys::path::append(DefaultModuleCache, "clang-module-cache");
421 CmdArgs.push_back("-fmodule-cache-path");
422 CmdArgs.push_back(Args.MakeArgString(DefaultModuleCache));
423 }
Douglas Gregor97eec242011-09-15 22:00:41 +0000424
425 Args.AddAllArgs(CmdArgs, options::OPT_fauto_module_import);
Benjamin Kramer8404eb02011-09-22 21:41:16 +0000426
427 // Parse additional include paths from environment variables.
428 // CPATH - included following the user specified includes (but prior to
429 // builtin and standard includes).
430 AddIncludeDirectoryList(Args, CmdArgs, "-I", ::getenv("CPATH"));
431 // C_INCLUDE_PATH - system includes enabled when compiling C.
432 AddIncludeDirectoryList(Args, CmdArgs, "-c-isystem",
433 ::getenv("C_INCLUDE_PATH"));
434 // CPLUS_INCLUDE_PATH - system includes enabled when compiling C++.
435 AddIncludeDirectoryList(Args, CmdArgs, "-cxx-isystem",
436 ::getenv("CPLUS_INCLUDE_PATH"));
437 // OBJC_INCLUDE_PATH - system includes enabled when compiling ObjC.
438 AddIncludeDirectoryList(Args, CmdArgs, "-objc-isystem",
439 ::getenv("OBJC_INCLUDE_PATH"));
440 // OBJCPLUS_INCLUDE_PATH - system includes enabled when compiling ObjC++.
441 AddIncludeDirectoryList(Args, CmdArgs, "-objcxx-isystem",
442 ::getenv("OBJCPLUS_INCLUDE_PATH"));
Daniel Dunbard067f7f2009-04-08 23:54:23 +0000443}
444
Chris Lattner57540c52011-04-15 05:22:18 +0000445/// getARMTargetCPU - Get the (LLVM) name of the ARM cpu we are targeting.
Daniel Dunbarfb58b0a2009-09-10 06:49:20 +0000446//
447// FIXME: tblgen this.
Rafael Espindola0e1fb4f2010-06-28 17:18:09 +0000448static const char *getARMTargetCPU(const ArgList &Args,
449 const llvm::Triple &Triple) {
Daniel Dunbarfb58b0a2009-09-10 06:49:20 +0000450 // FIXME: Warn on inconsistent use of -mcpu and -march.
Daniel Dunbar0f5c5422009-09-10 04:57:17 +0000451
452 // If we have -mcpu=, use that.
453 if (Arg *A = Args.getLastArg(options::OPT_mcpu_EQ))
454 return A->getValue(Args);
455
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000456 StringRef MArch;
Daniel Dunbar0f5c5422009-09-10 04:57:17 +0000457 if (Arg *A = Args.getLastArg(options::OPT_march_EQ)) {
Rafael Espindola0e1fb4f2010-06-28 17:18:09 +0000458 // Otherwise, if we have -march= choose the base CPU for that arch.
459 MArch = A->getValue(Args);
460 } else {
461 // Otherwise, use the Arch from the triple.
462 MArch = Triple.getArchName();
Daniel Dunbar0f5c5422009-09-10 04:57:17 +0000463 }
464
Rafael Espindola0e1fb4f2010-06-28 17:18:09 +0000465 if (MArch == "armv2" || MArch == "armv2a")
466 return "arm2";
467 if (MArch == "armv3")
468 return "arm6";
469 if (MArch == "armv3m")
470 return "arm7m";
471 if (MArch == "armv4" || MArch == "armv4t")
472 return "arm7tdmi";
473 if (MArch == "armv5" || MArch == "armv5t")
474 return "arm10tdmi";
475 if (MArch == "armv5e" || MArch == "armv5te")
476 return "arm1026ejs";
477 if (MArch == "armv5tej")
478 return "arm926ej-s";
479 if (MArch == "armv6" || MArch == "armv6k")
480 return "arm1136jf-s";
481 if (MArch == "armv6j")
482 return "arm1136j-s";
483 if (MArch == "armv6z" || MArch == "armv6zk")
484 return "arm1176jzf-s";
485 if (MArch == "armv6t2")
486 return "arm1156t2-s";
487 if (MArch == "armv7" || MArch == "armv7a" || MArch == "armv7-a")
488 return "cortex-a8";
489 if (MArch == "armv7r" || MArch == "armv7-r")
490 return "cortex-r4";
491 if (MArch == "armv7m" || MArch == "armv7-m")
492 return "cortex-m3";
493 if (MArch == "ep9312")
494 return "ep9312";
495 if (MArch == "iwmmxt")
496 return "iwmmxt";
497 if (MArch == "xscale")
498 return "xscale";
Bob Wilsond9249412011-03-21 20:40:05 +0000499 if (MArch == "armv6m" || MArch == "armv6-m")
500 return "cortex-m0";
Rafael Espindola0e1fb4f2010-06-28 17:18:09 +0000501
502 // If all else failed, return the most base CPU LLVM supports.
Daniel Dunbar0f5c5422009-09-10 04:57:17 +0000503 return "arm7tdmi";
504}
505
Daniel Dunbarf492c922009-09-10 22:59:51 +0000506/// getLLVMArchSuffixForARM - Get the LLVM arch name to use for a particular
Daniel Dunbarfb58b0a2009-09-10 06:49:20 +0000507/// CPU.
508//
509// FIXME: This is redundant with -mcpu, why does LLVM use this.
510// FIXME: tblgen this, or kill it!
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000511static const char *getLLVMArchSuffixForARM(StringRef CPU) {
Daniel Dunbarfb58b0a2009-09-10 06:49:20 +0000512 if (CPU == "arm7tdmi" || CPU == "arm7tdmi-s" || CPU == "arm710t" ||
513 CPU == "arm720t" || CPU == "arm9" || CPU == "arm9tdmi" ||
514 CPU == "arm920" || CPU == "arm920t" || CPU == "arm922t" ||
515 CPU == "arm940t" || CPU == "ep9312")
Daniel Dunbarf492c922009-09-10 22:59:51 +0000516 return "v4t";
Daniel Dunbarfb58b0a2009-09-10 06:49:20 +0000517
518 if (CPU == "arm10tdmi" || CPU == "arm1020t")
Daniel Dunbarf492c922009-09-10 22:59:51 +0000519 return "v5";
Daniel Dunbarfb58b0a2009-09-10 06:49:20 +0000520
521 if (CPU == "arm9e" || CPU == "arm926ej-s" || CPU == "arm946e-s" ||
522 CPU == "arm966e-s" || CPU == "arm968e-s" || CPU == "arm10e" ||
523 CPU == "arm1020e" || CPU == "arm1022e" || CPU == "xscale" ||
524 CPU == "iwmmxt")
Daniel Dunbarf492c922009-09-10 22:59:51 +0000525 return "v5e";
Daniel Dunbarfb58b0a2009-09-10 06:49:20 +0000526
527 if (CPU == "arm1136j-s" || CPU == "arm1136jf-s" || CPU == "arm1176jz-s" ||
528 CPU == "arm1176jzf-s" || CPU == "mpcorenovfp" || CPU == "mpcore")
Daniel Dunbarf492c922009-09-10 22:59:51 +0000529 return "v6";
Daniel Dunbarfb58b0a2009-09-10 06:49:20 +0000530
531 if (CPU == "arm1156t2-s" || CPU == "arm1156t2f-s")
Daniel Dunbarf492c922009-09-10 22:59:51 +0000532 return "v6t2";
Daniel Dunbarfb58b0a2009-09-10 06:49:20 +0000533
534 if (CPU == "cortex-a8" || CPU == "cortex-a9")
Daniel Dunbarf492c922009-09-10 22:59:51 +0000535 return "v7";
Daniel Dunbarfb58b0a2009-09-10 06:49:20 +0000536
Daniel Dunbarf492c922009-09-10 22:59:51 +0000537 return "";
Daniel Dunbarfb58b0a2009-09-10 06:49:20 +0000538}
539
Daniel Dunbard609b7b2009-11-17 06:37:03 +0000540// FIXME: Move to target hook.
541static bool isSignedCharDefault(const llvm::Triple &Triple) {
542 switch (Triple.getArch()) {
543 default:
544 return true;
545
Jim Grosbach7c2c6642011-05-24 15:40:46 +0000546 case llvm::Triple::arm:
Daniel Dunbard609b7b2009-11-17 06:37:03 +0000547 case llvm::Triple::ppc:
548 case llvm::Triple::ppc64:
549 if (Triple.getOS() == llvm::Triple::Darwin)
550 return true;
551 return false;
552
553 case llvm::Triple::systemz:
554 return false;
555 }
556}
557
Daniel Dunbar0f5c5422009-09-10 04:57:17 +0000558void Clang::AddARMTargetArgs(const ArgList &Args,
Daniel Dunbarc9388c12011-03-17 17:10:06 +0000559 ArgStringList &CmdArgs,
560 bool KernelOrKext) const {
Daniel Dunbar083edf72009-12-21 18:54:17 +0000561 const Driver &D = getToolChain().getDriver();
Rafael Espindola0e1fb4f2010-06-28 17:18:09 +0000562 llvm::Triple Triple = getToolChain().getTriple();
Daniel Dunbar78485922009-09-10 23:00:09 +0000563
Daniel Dunbar908b4852011-03-02 00:55:57 +0000564 // Disable movt generation, if requested.
565#ifdef DISABLE_ARM_DARWIN_USE_MOVT
Daniel Dunbar12100e22011-03-22 16:48:17 +0000566 CmdArgs.push_back("-backend-option");
Daniel Dunbar908b4852011-03-02 00:55:57 +0000567 CmdArgs.push_back("-arm-darwin-use-movt=0");
568#endif
569
Daniel Dunbar4ed78982009-09-14 00:34:46 +0000570 // Select the ABI to use.
571 //
572 // FIXME: Support -meabi.
573 const char *ABIName = 0;
574 if (Arg *A = Args.getLastArg(options::OPT_mabi_EQ)) {
575 ABIName = A->getValue(Args);
576 } else {
577 // Select the default based on the platform.
Bob Wilsond1447c42011-02-04 17:59:28 +0000578 switch(Triple.getEnvironment()) {
579 case llvm::Triple::GNUEABI:
Daniel Dunbar4ed78982009-09-14 00:34:46 +0000580 ABIName = "aapcs-linux";
Bob Wilsond1447c42011-02-04 17:59:28 +0000581 break;
582 case llvm::Triple::EABI:
Rafael Espindola23a8a062010-06-16 19:01:17 +0000583 ABIName = "aapcs";
Bob Wilsond1447c42011-02-04 17:59:28 +0000584 break;
585 default:
Rafael Espindola23a8a062010-06-16 19:01:17 +0000586 ABIName = "apcs-gnu";
Bob Wilsond1447c42011-02-04 17:59:28 +0000587 }
Daniel Dunbar4ed78982009-09-14 00:34:46 +0000588 }
589 CmdArgs.push_back("-target-abi");
590 CmdArgs.push_back(ABIName);
591
Daniel Dunbar0f5c5422009-09-10 04:57:17 +0000592 // Set the CPU based on -march= and -mcpu=.
Daniel Dunbara7d02312009-12-18 06:30:12 +0000593 CmdArgs.push_back("-target-cpu");
Rafael Espindola0e1fb4f2010-06-28 17:18:09 +0000594 CmdArgs.push_back(getARMTargetCPU(Args, Triple));
Daniel Dunbar0f5c5422009-09-10 04:57:17 +0000595
Daniel Dunbar78485922009-09-10 23:00:09 +0000596 // Select the float ABI as determined by -msoft-float, -mhard-float, and
597 // -mfloat-abi=.
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000598 StringRef FloatABI;
Daniel Dunbar78485922009-09-10 23:00:09 +0000599 if (Arg *A = Args.getLastArg(options::OPT_msoft_float,
600 options::OPT_mhard_float,
601 options::OPT_mfloat_abi_EQ)) {
602 if (A->getOption().matches(options::OPT_msoft_float))
603 FloatABI = "soft";
604 else if (A->getOption().matches(options::OPT_mhard_float))
605 FloatABI = "hard";
606 else {
607 FloatABI = A->getValue(Args);
608 if (FloatABI != "soft" && FloatABI != "softfp" && FloatABI != "hard") {
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000609 D.Diag(diag::err_drv_invalid_mfloat_abi)
Daniel Dunbar78485922009-09-10 23:00:09 +0000610 << A->getAsString(Args);
611 FloatABI = "soft";
612 }
613 }
614 }
615
616 // If unspecified, choose the default based on the platform.
617 if (FloatABI.empty()) {
Rafael Espindola0e1fb4f2010-06-28 17:18:09 +0000618 const llvm::Triple &Triple = getToolChain().getTriple();
619 switch (Triple.getOS()) {
Daniel Dunbar78485922009-09-10 23:00:09 +0000620 case llvm::Triple::Darwin: {
621 // Darwin defaults to "softfp" for v6 and v7.
622 //
623 // FIXME: Factor out an ARM class so we can cache the arch somewhere.
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000624 StringRef ArchName =
Rafael Espindola0e1fb4f2010-06-28 17:18:09 +0000625 getLLVMArchSuffixForARM(getARMTargetCPU(Args, Triple));
Daniel Dunbar78485922009-09-10 23:00:09 +0000626 if (ArchName.startswith("v6") || ArchName.startswith("v7"))
627 FloatABI = "softfp";
628 else
629 FloatABI = "soft";
630 break;
631 }
632
Rafael Espindolab1ef8ff2010-06-27 18:29:21 +0000633 case llvm::Triple::Linux: {
Bob Wilsond1447c42011-02-04 17:59:28 +0000634 if (getToolChain().getTriple().getEnvironment() == llvm::Triple::GNUEABI) {
Rafael Espindolab1ef8ff2010-06-27 18:29:21 +0000635 FloatABI = "softfp";
636 break;
637 }
638 }
639 // fall through
640
Daniel Dunbar78485922009-09-10 23:00:09 +0000641 default:
Bob Wilsond1447c42011-02-04 17:59:28 +0000642 switch(Triple.getEnvironment()) {
643 case llvm::Triple::GNUEABI:
644 FloatABI = "softfp";
645 break;
646 case llvm::Triple::EABI:
647 // EABI is always AAPCS, and if it was not marked 'hard', it's softfp
648 FloatABI = "softfp";
649 break;
650 default:
651 // Assume "soft", but warn the user we are guessing.
652 FloatABI = "soft";
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000653 D.Diag(diag::warn_drv_assuming_mfloat_abi_is) << "soft";
Bob Wilsond1447c42011-02-04 17:59:28 +0000654 break;
655 }
Daniel Dunbar78485922009-09-10 23:00:09 +0000656 }
657 }
658
659 if (FloatABI == "soft") {
660 // Floating point operations and argument passing are soft.
661 //
662 // FIXME: This changes CPP defines, we need -target-soft-float.
Daniel Dunbara74f8ff2009-11-30 08:42:00 +0000663 CmdArgs.push_back("-msoft-float");
Daniel Dunbar6cc525b2009-12-08 19:49:51 +0000664 CmdArgs.push_back("-mfloat-abi");
665 CmdArgs.push_back("soft");
Daniel Dunbar78485922009-09-10 23:00:09 +0000666 } else if (FloatABI == "softfp") {
667 // Floating point operations are hard, but argument passing is soft.
Daniel Dunbar6cc525b2009-12-08 19:49:51 +0000668 CmdArgs.push_back("-mfloat-abi");
669 CmdArgs.push_back("soft");
Daniel Dunbar78485922009-09-10 23:00:09 +0000670 } else {
671 // Floating point operations and argument passing are hard.
672 assert(FloatABI == "hard" && "Invalid float abi!");
Daniel Dunbar6cc525b2009-12-08 19:49:51 +0000673 CmdArgs.push_back("-mfloat-abi");
674 CmdArgs.push_back("hard");
Daniel Dunbar78485922009-09-10 23:00:09 +0000675 }
Daniel Dunbar893d4752009-12-19 04:15:38 +0000676
677 // Set appropriate target features for floating point mode.
678 //
679 // FIXME: Note, this is a hack, the LLVM backend doesn't actually use these
680 // yet (it uses the -mfloat-abi and -msoft-float options above), and it is
681 // stripped out by the ARM target.
682
683 // Use software floating point operations?
684 if (FloatABI == "soft") {
685 CmdArgs.push_back("-target-feature");
686 CmdArgs.push_back("+soft-float");
687 }
688
689 // Use software floating point argument passing?
690 if (FloatABI != "hard") {
691 CmdArgs.push_back("-target-feature");
692 CmdArgs.push_back("+soft-float-abi");
693 }
Daniel Dunbar0def3d12009-12-21 23:28:17 +0000694
695 // Honor -mfpu=.
696 //
697 // FIXME: Centralize feature selection, defaulting shouldn't be also in the
698 // frontend target.
699 if (const Arg *A = Args.getLastArg(options::OPT_mfpu_EQ)) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000700 StringRef FPU = A->getValue(Args);
Daniel Dunbar0def3d12009-12-21 23:28:17 +0000701
702 // Set the target features based on the FPU.
703 if (FPU == "fpa" || FPU == "fpe2" || FPU == "fpe3" || FPU == "maverick") {
704 // Disable any default FPU support.
705 CmdArgs.push_back("-target-feature");
706 CmdArgs.push_back("-vfp2");
707 CmdArgs.push_back("-target-feature");
708 CmdArgs.push_back("-vfp3");
709 CmdArgs.push_back("-target-feature");
710 CmdArgs.push_back("-neon");
711 } else if (FPU == "vfp") {
712 CmdArgs.push_back("-target-feature");
713 CmdArgs.push_back("+vfp2");
714 } else if (FPU == "vfp3") {
715 CmdArgs.push_back("-target-feature");
716 CmdArgs.push_back("+vfp3");
717 } else if (FPU == "neon") {
718 CmdArgs.push_back("-target-feature");
719 CmdArgs.push_back("+neon");
720 } else
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000721 D.Diag(diag::err_drv_clang_unsupported) << A->getAsString(Args);
Daniel Dunbar0def3d12009-12-21 23:28:17 +0000722 }
Daniel Dunbarb1db4b62011-03-17 00:07:34 +0000723
724 // Setting -msoft-float effectively disables NEON because of the GCC
725 // implementation, although the same isn't true of VFP or VFP3.
726 if (FloatABI == "soft") {
Daniel Dunbarc9388c12011-03-17 17:10:06 +0000727 CmdArgs.push_back("-target-feature");
728 CmdArgs.push_back("-neon");
729 }
730
731 // Kernel code has more strict alignment requirements.
732 if (KernelOrKext) {
Daniel Dunbar12100e22011-03-22 16:48:17 +0000733 CmdArgs.push_back("-backend-option");
Daniel Dunbarc9388c12011-03-17 17:10:06 +0000734 CmdArgs.push_back("-arm-long-calls");
735
Daniel Dunbar12100e22011-03-22 16:48:17 +0000736 CmdArgs.push_back("-backend-option");
Daniel Dunbarc9388c12011-03-17 17:10:06 +0000737 CmdArgs.push_back("-arm-strict-align");
Daniel Dunbared904c82011-04-18 21:26:42 +0000738
739 // The kext linker doesn't know how to deal with movw/movt.
740#ifndef DISABLE_ARM_DARWIN_USE_MOVT
741 CmdArgs.push_back("-backend-option");
742 CmdArgs.push_back("-arm-darwin-use-movt=0");
743#endif
Daniel Dunbarb1db4b62011-03-17 00:07:34 +0000744 }
Chad Rosierba3df1d2011-08-26 00:26:29 +0000745
746 // Setting -mno-global-merge disables the codegen global merge pass. Setting
747 // -mglobal-merge has no effect as the pass is enabled by default.
748 if (Arg *A = Args.getLastArg(options::OPT_mglobal_merge,
749 options::OPT_mno_global_merge)) {
750 if (A->getOption().matches(options::OPT_mno_global_merge))
751 CmdArgs.push_back("-mno-global-merge");
752 }
Daniel Dunbar0f5c5422009-09-10 04:57:17 +0000753}
754
Eric Christopher0b26a612010-03-02 02:41:08 +0000755void Clang::AddMIPSTargetArgs(const ArgList &Args,
756 ArgStringList &CmdArgs) const {
757 const Driver &D = getToolChain().getDriver();
758
759 // Select the ABI to use.
760 const char *ABIName = 0;
761 if (Arg *A = Args.getLastArg(options::OPT_mabi_EQ)) {
762 ABIName = A->getValue(Args);
763 } else {
764 ABIName = "o32";
765 }
766
767 CmdArgs.push_back("-target-abi");
768 CmdArgs.push_back(ABIName);
769
770 if (const Arg *A = Args.getLastArg(options::OPT_march_EQ)) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000771 StringRef MArch = A->getValue(Args);
Eric Christopher0b26a612010-03-02 02:41:08 +0000772 CmdArgs.push_back("-target-cpu");
773
774 if ((MArch == "r2000") || (MArch == "r3000"))
775 CmdArgs.push_back("mips1");
776 else if (MArch == "r6000")
777 CmdArgs.push_back("mips2");
778 else
Nick Lewycky36d8f052011-05-04 03:44:01 +0000779 CmdArgs.push_back(Args.MakeArgString(MArch));
Eric Christopher0b26a612010-03-02 02:41:08 +0000780 }
781
782 // Select the float ABI as determined by -msoft-float, -mhard-float, and
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000783 StringRef FloatABI;
Eric Christopher0b26a612010-03-02 02:41:08 +0000784 if (Arg *A = Args.getLastArg(options::OPT_msoft_float,
785 options::OPT_mhard_float)) {
786 if (A->getOption().matches(options::OPT_msoft_float))
787 FloatABI = "soft";
788 else if (A->getOption().matches(options::OPT_mhard_float))
789 FloatABI = "hard";
790 }
791
792 // If unspecified, choose the default based on the platform.
793 if (FloatABI.empty()) {
Benjamin Kramerf41ccef2010-04-08 15:44:22 +0000794 // Assume "soft", but warn the user we are guessing.
795 FloatABI = "soft";
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000796 D.Diag(diag::warn_drv_assuming_mfloat_abi_is) << "soft";
Eric Christopher0b26a612010-03-02 02:41:08 +0000797 }
798
799 if (FloatABI == "soft") {
800 // Floating point operations and argument passing are soft.
801 //
802 // FIXME: This changes CPP defines, we need -target-soft-float.
803 CmdArgs.push_back("-msoft-float");
804 } else {
805 assert(FloatABI == "hard" && "Invalid float abi!");
806 CmdArgs.push_back("-mhard-float");
807 }
808}
809
Bruno Cardoso Lopese7f211c2010-11-09 17:21:19 +0000810void Clang::AddSparcTargetArgs(const ArgList &Args,
811 ArgStringList &CmdArgs) const {
812 const Driver &D = getToolChain().getDriver();
813
814 if (const Arg *A = Args.getLastArg(options::OPT_march_EQ)) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000815 StringRef MArch = A->getValue(Args);
Bruno Cardoso Lopese7f211c2010-11-09 17:21:19 +0000816 CmdArgs.push_back("-target-cpu");
817 CmdArgs.push_back(MArch.str().c_str());
818 }
819
820 // Select the float ABI as determined by -msoft-float, -mhard-float, and
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000821 StringRef FloatABI;
Bruno Cardoso Lopese7f211c2010-11-09 17:21:19 +0000822 if (Arg *A = Args.getLastArg(options::OPT_msoft_float,
823 options::OPT_mhard_float)) {
824 if (A->getOption().matches(options::OPT_msoft_float))
825 FloatABI = "soft";
826 else if (A->getOption().matches(options::OPT_mhard_float))
827 FloatABI = "hard";
828 }
829
830 // If unspecified, choose the default based on the platform.
831 if (FloatABI.empty()) {
832 switch (getToolChain().getTriple().getOS()) {
833 default:
834 // Assume "soft", but warn the user we are guessing.
835 FloatABI = "soft";
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000836 D.Diag(diag::warn_drv_assuming_mfloat_abi_is) << "soft";
Bruno Cardoso Lopese7f211c2010-11-09 17:21:19 +0000837 break;
838 }
839 }
840
841 if (FloatABI == "soft") {
842 // Floating point operations and argument passing are soft.
843 //
844 // FIXME: This changes CPP defines, we need -target-soft-float.
845 CmdArgs.push_back("-msoft-float");
Bruno Cardoso Lopese7f211c2010-11-09 17:21:19 +0000846 CmdArgs.push_back("-target-feature");
847 CmdArgs.push_back("+soft-float");
848 } else {
849 assert(FloatABI == "hard" && "Invalid float abi!");
850 CmdArgs.push_back("-mhard-float");
851 }
852}
853
Daniel Dunbar3b3191f2009-09-09 22:33:08 +0000854void Clang::AddX86TargetArgs(const ArgList &Args,
855 ArgStringList &CmdArgs) const {
Daniel Dunbare2cf8f72009-09-10 22:59:57 +0000856 if (!Args.hasFlag(options::OPT_mred_zone,
857 options::OPT_mno_red_zone,
858 true) ||
859 Args.hasArg(options::OPT_mkernel) ||
860 Args.hasArg(options::OPT_fapple_kext))
Daniel Dunbar8bed86c2009-11-20 22:21:36 +0000861 CmdArgs.push_back("-disable-red-zone");
Daniel Dunbare2cf8f72009-09-10 22:59:57 +0000862
Daniel Dunbare2cf8f72009-09-10 22:59:57 +0000863 if (Args.hasFlag(options::OPT_msoft_float,
864 options::OPT_mno_soft_float,
865 false))
Daniel Dunbar8bed86c2009-11-20 22:21:36 +0000866 CmdArgs.push_back("-no-implicit-float");
Daniel Dunbare2cf8f72009-09-10 22:59:57 +0000867
Daniel Dunbare13ada62009-11-14 22:04:54 +0000868 const char *CPUName = 0;
Daniel Dunbar3b3191f2009-09-09 22:33:08 +0000869 if (const Arg *A = Args.getLastArg(options::OPT_march_EQ)) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000870 if (StringRef(A->getValue(Args)) == "native") {
Daniel Dunbare13ada62009-11-14 22:04:54 +0000871 // FIXME: Reject attempts to use -march=native unless the target matches
872 // the host.
873 //
874 // FIXME: We should also incorporate the detected target features for use
875 // with -native.
876 std::string CPU = llvm::sys::getHostCPUName();
877 if (!CPU.empty())
878 CPUName = Args.MakeArgString(CPU);
879 } else
880 CPUName = A->getValue(Args);
881 }
Daniel Dunbar3b3191f2009-09-09 22:33:08 +0000882
Daniel Dunbare13ada62009-11-14 22:04:54 +0000883 // Select the default CPU if none was given (or detection failed).
884 if (!CPUName) {
Daniel Dunbar3b3191f2009-09-09 22:33:08 +0000885 // FIXME: Need target hooks.
Benjamin Kramer842bf172010-01-30 15:01:47 +0000886 if (getToolChain().getOS().startswith("darwin")) {
Daniel Dunbar116b3052011-05-31 15:58:55 +0000887 if (getToolChain().getArch() == llvm::Triple::x86_64)
Daniel Dunbare13ada62009-11-14 22:04:54 +0000888 CPUName = "core2";
Daniel Dunbar116b3052011-05-31 15:58:55 +0000889 else if (getToolChain().getArch() == llvm::Triple::x86)
Daniel Dunbare13ada62009-11-14 22:04:54 +0000890 CPUName = "yonah";
Chris Lattnerb986aba2010-04-11 19:29:39 +0000891 } else if (getToolChain().getOS().startswith("haiku")) {
Daniel Dunbar116b3052011-05-31 15:58:55 +0000892 if (getToolChain().getArch() == llvm::Triple::x86_64)
Chris Lattnerb986aba2010-04-11 19:29:39 +0000893 CPUName = "x86-64";
Daniel Dunbar116b3052011-05-31 15:58:55 +0000894 else if (getToolChain().getArch() == llvm::Triple::x86)
Chris Lattnerb986aba2010-04-11 19:29:39 +0000895 CPUName = "i586";
Daniel Dunbarea3813f2010-08-01 23:13:54 +0000896 } else if (getToolChain().getOS().startswith("openbsd")) {
Daniel Dunbar116b3052011-05-31 15:58:55 +0000897 if (getToolChain().getArch() == llvm::Triple::x86_64)
Daniel Dunbarea3813f2010-08-01 23:13:54 +0000898 CPUName = "x86-64";
Daniel Dunbar116b3052011-05-31 15:58:55 +0000899 else if (getToolChain().getArch() == llvm::Triple::x86)
Daniel Dunbarea3813f2010-08-01 23:13:54 +0000900 CPUName = "i486";
Roman Divacky432f10d2011-03-01 18:11:37 +0000901 } else if (getToolChain().getOS().startswith("freebsd")) {
Daniel Dunbar116b3052011-05-31 15:58:55 +0000902 if (getToolChain().getArch() == llvm::Triple::x86_64)
Roman Divacky432f10d2011-03-01 18:11:37 +0000903 CPUName = "x86-64";
Daniel Dunbar116b3052011-05-31 15:58:55 +0000904 else if (getToolChain().getArch() == llvm::Triple::x86)
Roman Divacky432f10d2011-03-01 18:11:37 +0000905 CPUName = "i486";
Benjamin Kramer24f1d3e2011-02-02 18:59:27 +0000906 } else if (getToolChain().getOS().startswith("netbsd")) {
Daniel Dunbar116b3052011-05-31 15:58:55 +0000907 if (getToolChain().getArch() == llvm::Triple::x86_64)
Benjamin Kramer24f1d3e2011-02-02 18:59:27 +0000908 CPUName = "x86-64";
Daniel Dunbar116b3052011-05-31 15:58:55 +0000909 else if (getToolChain().getArch() == llvm::Triple::x86)
Benjamin Kramer24f1d3e2011-02-02 18:59:27 +0000910 CPUName = "i486";
Daniel Dunbar3b3191f2009-09-09 22:33:08 +0000911 } else {
Daniel Dunbar116b3052011-05-31 15:58:55 +0000912 if (getToolChain().getArch() == llvm::Triple::x86_64)
Daniel Dunbare13ada62009-11-14 22:04:54 +0000913 CPUName = "x86-64";
Daniel Dunbar116b3052011-05-31 15:58:55 +0000914 else if (getToolChain().getArch() == llvm::Triple::x86)
Daniel Dunbare13ada62009-11-14 22:04:54 +0000915 CPUName = "pentium4";
Daniel Dunbar3b3191f2009-09-09 22:33:08 +0000916 }
917 }
918
Daniel Dunbare13ada62009-11-14 22:04:54 +0000919 if (CPUName) {
Daniel Dunbara7d02312009-12-18 06:30:12 +0000920 CmdArgs.push_back("-target-cpu");
Daniel Dunbare13ada62009-11-14 22:04:54 +0000921 CmdArgs.push_back(CPUName);
922 }
923
Eli Friedmanad811f02011-07-02 00:34:19 +0000924 // The required algorithm here is slightly strange: the options are applied
925 // in order (so -mno-sse -msse2 disables SSE3), but any option that gets
926 // directly overridden later is ignored (so "-mno-sse -msse2 -mno-sse2 -msse"
927 // is equivalent to "-mno-sse2 -msse"). The -cc1 handling deals with the
928 // former correctly, but not the latter; handle directly-overridden
929 // attributes here.
930 llvm::StringMap<unsigned> PrevFeature;
931 std::vector<const char*> Features;
Daniel Dunbar44b36ee2009-11-25 11:53:23 +0000932 for (arg_iterator it = Args.filtered_begin(options::OPT_m_x86_Features_Group),
933 ie = Args.filtered_end(); it != ie; ++it) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000934 StringRef Name = (*it)->getOption().getName();
Daniel Dunbara442fd52010-06-11 22:00:13 +0000935 (*it)->claim();
Daniel Dunbar3b3191f2009-09-09 22:33:08 +0000936
Daniel Dunbar44b36ee2009-11-25 11:53:23 +0000937 // Skip over "-m".
938 assert(Name.startswith("-m") && "Invalid feature name.");
939 Name = Name.substr(2);
Daniel Dunbar3b3191f2009-09-09 22:33:08 +0000940
Daniel Dunbar44b36ee2009-11-25 11:53:23 +0000941 bool IsNegative = Name.startswith("no-");
942 if (IsNegative)
943 Name = Name.substr(3);
Daniel Dunbar3b3191f2009-09-09 22:33:08 +0000944
Eli Friedmanad811f02011-07-02 00:34:19 +0000945 unsigned& Prev = PrevFeature[Name];
946 if (Prev)
947 Features[Prev - 1] = 0;
948 Prev = Features.size() + 1;
949 Features.push_back(Args.MakeArgString((IsNegative ? "-" : "+") + Name));
950 }
951 for (unsigned i = 0; i < Features.size(); i++) {
952 if (Features[i]) {
953 CmdArgs.push_back("-target-feature");
954 CmdArgs.push_back(Features[i]);
955 }
Daniel Dunbar3b3191f2009-09-09 22:33:08 +0000956 }
957}
958
Eric Christopher84fbdb42011-08-19 00:30:14 +0000959static bool
John McCallb5f652e2011-06-22 00:53:57 +0000960shouldUseExceptionTablesForObjCExceptions(unsigned objcABIVersion,
Anders Carlsson246ff3f2011-02-28 00:44:51 +0000961 const llvm::Triple &Triple) {
962 // We use the zero-cost exception tables for Objective-C if the non-fragile
963 // ABI is enabled or when compiling for x86_64 and ARM on Snow Leopard and
964 // later.
965
John McCallb5f652e2011-06-22 00:53:57 +0000966 if (objcABIVersion >= 2)
Anders Carlsson246ff3f2011-02-28 00:44:51 +0000967 return true;
968
969 if (Triple.getOS() != llvm::Triple::Darwin)
970 return false;
971
Eric Christopherbf15d2b2011-07-02 00:20:22 +0000972 return (!Triple.isMacOSXVersionLT(10,5) &&
Anders Carlsson246ff3f2011-02-28 00:44:51 +0000973 (Triple.getArch() == llvm::Triple::x86_64 ||
Eric Christopher84fbdb42011-08-19 00:30:14 +0000974 Triple.getArch() == llvm::Triple::arm));
Anders Carlsson246ff3f2011-02-28 00:44:51 +0000975}
976
Anders Carlssone96ab552011-02-28 02:27:16 +0000977/// addExceptionArgs - Adds exception related arguments to the driver command
978/// arguments. There's a master flag, -fexceptions and also language specific
979/// flags to enable/disable C++ and Objective-C exceptions.
980/// This makes it possible to for example disable C++ exceptions but enable
981/// Objective-C exceptions.
982static void addExceptionArgs(const ArgList &Args, types::ID InputType,
983 const llvm::Triple &Triple,
984 bool KernelOrKext, bool IsRewriter,
John McCallb5f652e2011-06-22 00:53:57 +0000985 unsigned objcABIVersion,
Anders Carlssone96ab552011-02-28 02:27:16 +0000986 ArgStringList &CmdArgs) {
987 if (KernelOrKext)
988 return;
989
990 // Exceptions are enabled by default.
991 bool ExceptionsEnabled = true;
992
993 // This keeps track of whether exceptions were explicitly turned on or off.
994 bool DidHaveExplicitExceptionFlag = false;
995
Rafael Espindola00a66572009-10-01 13:33:33 +0000996 if (Arg *A = Args.getLastArg(options::OPT_fexceptions,
997 options::OPT_fno_exceptions)) {
998 if (A->getOption().matches(options::OPT_fexceptions))
Anders Carlssone96ab552011-02-28 02:27:16 +0000999 ExceptionsEnabled = true;
Eric Christopher84fbdb42011-08-19 00:30:14 +00001000 else
Anders Carlssone96ab552011-02-28 02:27:16 +00001001 ExceptionsEnabled = false;
1002
1003 DidHaveExplicitExceptionFlag = true;
Rafael Espindola00a66572009-10-01 13:33:33 +00001004 }
Daniel Dunbar30a12b82010-09-14 23:12:31 +00001005
Anders Carlssone96ab552011-02-28 02:27:16 +00001006 bool ShouldUseExceptionTables = false;
Fariborz Jahaniane4b21ab2009-10-01 20:30:46 +00001007
Anders Carlssone96ab552011-02-28 02:27:16 +00001008 // Exception tables and cleanups can be enabled with -fexceptions even if the
1009 // language itself doesn't support exceptions.
1010 if (ExceptionsEnabled && DidHaveExplicitExceptionFlag)
1011 ShouldUseExceptionTables = true;
Daniel Dunbar30a12b82010-09-14 23:12:31 +00001012
Daniel Dunbarc44f8cf2011-03-17 23:28:31 +00001013 // Obj-C exceptions are enabled by default, regardless of -fexceptions. This
1014 // is not necessarily sensible, but follows GCC.
1015 if (types::isObjC(InputType) &&
Eric Christopher84fbdb42011-08-19 00:30:14 +00001016 Args.hasFlag(options::OPT_fobjc_exceptions,
Daniel Dunbarc44f8cf2011-03-17 23:28:31 +00001017 options::OPT_fno_objc_exceptions,
1018 true)) {
1019 CmdArgs.push_back("-fobjc-exceptions");
Anders Carlssone96ab552011-02-28 02:27:16 +00001020
Eric Christopher84fbdb42011-08-19 00:30:14 +00001021 ShouldUseExceptionTables |=
John McCallb5f652e2011-06-22 00:53:57 +00001022 shouldUseExceptionTablesForObjCExceptions(objcABIVersion, Triple);
Anders Carlssone96ab552011-02-28 02:27:16 +00001023 }
1024
1025 if (types::isCXX(InputType)) {
1026 bool CXXExceptionsEnabled = ExceptionsEnabled;
1027
Eric Christopher84fbdb42011-08-19 00:30:14 +00001028 if (Arg *A = Args.getLastArg(options::OPT_fcxx_exceptions,
1029 options::OPT_fno_cxx_exceptions,
Anders Carlssone96ab552011-02-28 02:27:16 +00001030 options::OPT_fexceptions,
1031 options::OPT_fno_exceptions)) {
1032 if (A->getOption().matches(options::OPT_fcxx_exceptions))
1033 CXXExceptionsEnabled = true;
Chandler Carruth74f87112011-02-28 07:25:18 +00001034 else if (A->getOption().matches(options::OPT_fno_cxx_exceptions))
Anders Carlssone96ab552011-02-28 02:27:16 +00001035 CXXExceptionsEnabled = false;
1036 }
1037
1038 if (CXXExceptionsEnabled) {
1039 CmdArgs.push_back("-fcxx-exceptions");
1040
1041 ShouldUseExceptionTables = true;
1042 }
1043 }
1044
1045 if (ShouldUseExceptionTables)
1046 CmdArgs.push_back("-fexceptions");
Rafael Espindola00a66572009-10-01 13:33:33 +00001047}
1048
Rafael Espindola4cfa7972011-05-02 17:43:32 +00001049static bool ShouldDisableCFI(const ArgList &Args,
1050 const ToolChain &TC) {
Rafael Espindolaf934f982011-05-17 16:26:17 +00001051 if (TC.getTriple().getOS() == llvm::Triple::Darwin) {
1052 // The native darwin assembler doesn't support cfi directives, so
Rafael Espindola35ab91c2011-05-17 19:06:58 +00001053 // we disable them if we think the .s file will be passed to it.
Rafael Espindola4cfa7972011-05-02 17:43:32 +00001054
Rafael Espindolaf934f982011-05-17 16:26:17 +00001055 // FIXME: Duplicated code with ToolChains.cpp
1056 // FIXME: This doesn't belong here, but ideally we will support static soon
1057 // anyway.
1058 bool HasStatic = (Args.hasArg(options::OPT_mkernel) ||
1059 Args.hasArg(options::OPT_static) ||
1060 Args.hasArg(options::OPT_fapple_kext));
1061 bool IsIADefault = TC.IsIntegratedAssemblerDefault() && !HasStatic;
1062 bool UseIntegratedAs = Args.hasFlag(options::OPT_integrated_as,
1063 options::OPT_no_integrated_as,
1064 IsIADefault);
1065 bool UseCFI = Args.hasFlag(options::OPT_fdwarf2_cfi_asm,
1066 options::OPT_fno_dwarf2_cfi_asm,
1067 UseIntegratedAs);
1068 return !UseCFI;
1069 }
1070
1071 // For now we assume that every other assembler support CFI.
1072 return false;
Rafael Espindola4cfa7972011-05-02 17:43:32 +00001073}
1074
Joerg Sonnenbergeref317a22011-05-06 14:35:16 +00001075/// \brief Check whether the given input tree contains any compilation actions.
1076static bool ContainsCompileAction(const Action *A) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001077 if (isa<CompileJobAction>(A))
Joerg Sonnenbergeref317a22011-05-06 14:35:16 +00001078 return true;
1079
1080 for (Action::const_iterator it = A->begin(), ie = A->end(); it != ie; ++it)
1081 if (ContainsCompileAction(*it))
1082 return true;
1083
1084 return false;
1085}
1086
1087/// \brief Check if -relax-all should be passed to the internal assembler.
1088/// This is done by default when compiling non-assembler source with -O0.
1089static bool UseRelaxAll(Compilation &C, const ArgList &Args) {
1090 bool RelaxDefault = true;
1091
1092 if (Arg *A = Args.getLastArg(options::OPT_O_Group))
1093 RelaxDefault = A->getOption().matches(options::OPT_O0);
1094
1095 if (RelaxDefault) {
1096 RelaxDefault = false;
1097 for (ActionList::const_iterator it = C.getActions().begin(),
1098 ie = C.getActions().end(); it != ie; ++it) {
1099 if (ContainsCompileAction(*it)) {
1100 RelaxDefault = true;
1101 break;
1102 }
1103 }
1104 }
1105
1106 return Args.hasFlag(options::OPT_mrelax_all, options::OPT_mno_relax_all,
1107 RelaxDefault);
1108}
1109
Daniel Dunbar1a093d22009-03-18 06:00:36 +00001110void Clang::ConstructJob(Compilation &C, const JobAction &JA,
Daniel Dunbar04c4c2c2009-03-18 07:06:02 +00001111 const InputInfo &Output,
Daniel Dunbar0450e6d2009-03-18 06:07:59 +00001112 const InputInfoList &Inputs,
Daniel Dunbara2aedc62009-03-18 10:01:51 +00001113 const ArgList &Args,
Daniel Dunbar1a093d22009-03-18 06:00:36 +00001114 const char *LinkingOutput) const {
Daniel Dunbare46b52a2010-03-20 04:52:14 +00001115 bool KernelOrKext = Args.hasArg(options::OPT_mkernel,
1116 options::OPT_fapple_kext);
Daniel Dunbar083edf72009-12-21 18:54:17 +00001117 const Driver &D = getToolChain().getDriver();
Daniel Dunbara3246a02009-03-18 08:07:30 +00001118 ArgStringList CmdArgs;
1119
Daniel Dunbare521a892009-03-31 20:53:55 +00001120 assert(Inputs.size() == 1 && "Unable to handle multiple inputs.");
1121
Daniel Dunbar6c536aa2009-12-11 23:00:49 +00001122 // Invoke ourselves in -cc1 mode.
1123 //
1124 // FIXME: Implement custom jobs for internal actions.
1125 CmdArgs.push_back("-cc1");
1126
Daniel Dunbar624c21b2009-10-30 18:12:20 +00001127 // Add the "effective" target triple.
Daniel Dunbard640be22009-03-31 17:35:15 +00001128 CmdArgs.push_back("-triple");
Daniel Dunbar82eb4ce2010-08-23 22:35:37 +00001129 std::string TripleStr = getToolChain().ComputeEffectiveClangTriple(Args);
Daniel Dunbar624c21b2009-10-30 18:12:20 +00001130 CmdArgs.push_back(Args.MakeArgString(TripleStr));
Daniel Dunbarfb58b0a2009-09-10 06:49:20 +00001131
Daniel Dunbar624c21b2009-10-30 18:12:20 +00001132 // Select the appropriate action.
Daniel Dunbar99b55242010-07-19 19:44:22 +00001133 bool IsRewriter = false;
Daniel Dunbara2aedc62009-03-18 10:01:51 +00001134 if (isa<AnalyzeJobAction>(JA)) {
1135 assert(JA.getType() == types::TY_Plist && "Invalid output type.");
1136 CmdArgs.push_back("-analyze");
1137 } else if (isa<PreprocessJobAction>(JA)) {
Daniel Dunbard67a3222009-03-30 06:36:42 +00001138 if (Output.getType() == types::TY_Dependencies)
1139 CmdArgs.push_back("-Eonly");
1140 else
1141 CmdArgs.push_back("-E");
Daniel Dunbarc4343942010-02-03 03:07:56 +00001142 } else if (isa<AssembleJobAction>(JA)) {
1143 CmdArgs.push_back("-emit-obj");
Daniel Dunbar06e2cc32010-05-27 06:18:05 +00001144
Joerg Sonnenbergeref317a22011-05-06 14:35:16 +00001145 if (UseRelaxAll(C, Args))
Daniel Dunbar06e2cc32010-05-27 06:18:05 +00001146 CmdArgs.push_back("-mrelax-all");
Daniel Dunbarfe6c97b2010-08-24 16:47:49 +00001147
Daniel Dunbar2b4de142010-10-18 22:36:15 +00001148 // When using an integrated assembler, translate -Wa, and -Xassembler
1149 // options.
1150 for (arg_iterator it = Args.filtered_begin(options::OPT_Wa_COMMA,
1151 options::OPT_Xassembler),
1152 ie = Args.filtered_end(); it != ie; ++it) {
1153 const Arg *A = *it;
1154 A->claim();
1155
1156 for (unsigned i = 0, e = A->getNumValues(); i != e; ++i) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001157 StringRef Value = A->getValue(Args, i);
Daniel Dunbar2b4de142010-10-18 22:36:15 +00001158
1159 if (Value == "-force_cpusubtype_ALL") {
1160 // Do nothing, this is the default and we don't support anything else.
Daniel Dunbara78e5892010-10-28 20:36:23 +00001161 } else if (Value == "-L") {
Daniel Dunbar67919b22011-03-28 22:49:28 +00001162 CmdArgs.push_back("-msave-temp-labels");
Joerg Sonnenberger3028e462011-05-19 20:46:39 +00001163 } else if (Value == "--fatal-warnings") {
Joerg Sonnenbergerb487d2d2011-05-19 18:42:29 +00001164 CmdArgs.push_back("-mllvm");
1165 CmdArgs.push_back("-fatal-assembler-warnings");
Nick Lewyckyca6b90d2011-06-21 00:14:18 +00001166 } else if (Value == "--noexecstack") {
1167 CmdArgs.push_back("-mnoexecstack");
Daniel Dunbar2b4de142010-10-18 22:36:15 +00001168 } else {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001169 D.Diag(diag::err_drv_unsupported_option_argument)
Daniel Dunbar2b4de142010-10-18 22:36:15 +00001170 << A->getOption().getName() << Value;
1171 }
1172 }
1173 }
Daniel Dunbar7c874332010-11-19 16:23:35 +00001174
1175 // Also ignore explicit -force_cpusubtype_ALL option.
1176 (void) Args.hasArg(options::OPT_force__cpusubtype__ALL);
Daniel Dunbara2aedc62009-03-18 10:01:51 +00001177 } else if (isa<PrecompileJobAction>(JA)) {
Argyrios Kyrtzidis90bdfbb2010-08-11 23:27:58 +00001178 // Use PCH if the user requested it.
Daniel Dunbarcbc34b72009-10-15 20:02:44 +00001179 bool UsePCH = D.CCCUsePCH;
Daniel Dunbarcbc34b72009-10-15 20:02:44 +00001180
1181 if (UsePCH)
Douglas Gregor111af7d2009-04-18 00:34:01 +00001182 CmdArgs.push_back("-emit-pch");
1183 else
1184 CmdArgs.push_back("-emit-pth");
Daniel Dunbara2aedc62009-03-18 10:01:51 +00001185 } else {
1186 assert(isa<CompileJobAction>(JA) && "Invalid action for clang tool.");
Daniel Dunbard067f7f2009-04-08 23:54:23 +00001187
Daniel Dunbara2aedc62009-03-18 10:01:51 +00001188 if (JA.getType() == types::TY_Nothing) {
1189 CmdArgs.push_back("-fsyntax-only");
Daniel Dunbar24e52992010-06-07 23:28:45 +00001190 } else if (JA.getType() == types::TY_LLVM_IR ||
1191 JA.getType() == types::TY_LTO_IR) {
Daniel Dunbara2aedc62009-03-18 10:01:51 +00001192 CmdArgs.push_back("-emit-llvm");
Daniel Dunbar24e52992010-06-07 23:28:45 +00001193 } else if (JA.getType() == types::TY_LLVM_BC ||
1194 JA.getType() == types::TY_LTO_BC) {
Daniel Dunbara2aedc62009-03-18 10:01:51 +00001195 CmdArgs.push_back("-emit-llvm-bc");
1196 } else if (JA.getType() == types::TY_PP_Asm) {
Daniel Dunbard112f102009-09-17 00:47:53 +00001197 CmdArgs.push_back("-S");
Daniel Dunbar6cdf83c2009-09-01 16:57:46 +00001198 } else if (JA.getType() == types::TY_AST) {
1199 CmdArgs.push_back("-emit-pch");
Daniel Dunbaraeea8ac2010-02-11 03:16:21 +00001200 } else if (JA.getType() == types::TY_RewrittenObjC) {
1201 CmdArgs.push_back("-rewrite-objc");
Daniel Dunbar99b55242010-07-19 19:44:22 +00001202 IsRewriter = true;
Daniel Dunbaraeea8ac2010-02-11 03:16:21 +00001203 } else {
1204 assert(JA.getType() == types::TY_PP_Asm &&
1205 "Unexpected output type!");
Daniel Dunbara2aedc62009-03-18 10:01:51 +00001206 }
Daniel Dunbara3246a02009-03-18 08:07:30 +00001207 }
1208
Daniel Dunbara2aedc62009-03-18 10:01:51 +00001209 // The make clang go fast button.
1210 CmdArgs.push_back("-disable-free");
1211
John McCallbb79b5f2010-02-13 03:50:24 +00001212 // Disable the verification pass in -asserts builds.
1213#ifdef NDEBUG
1214 CmdArgs.push_back("-disable-llvm-verifier");
1215#endif
1216
Daniel Dunbar3b358a32009-04-08 05:11:16 +00001217 // Set the main file name, so that debug info works even with
1218 // -save-temps.
1219 CmdArgs.push_back("-main-file-name");
1220 CmdArgs.push_back(darwin::CC1::getBaseInputName(Args, Inputs));
1221
Daniel Dunbar17ddaa62009-04-08 18:03:55 +00001222 // Some flags which affect the language (via preprocessor
1223 // defines). See darwin::CC1::AddCPPArgs.
1224 if (Args.hasArg(options::OPT_static))
1225 CmdArgs.push_back("-static-define");
1226
Daniel Dunbara2aedc62009-03-18 10:01:51 +00001227 if (isa<AnalyzeJobAction>(JA)) {
Ted Kremenek05e6f5b2009-09-25 05:55:59 +00001228 // Enable region store model by default.
1229 CmdArgs.push_back("-analyzer-store=region");
1230
Ted Kremenek7bea9a12009-12-07 22:26:14 +00001231 // Treat blocks as analysis entry points.
1232 CmdArgs.push_back("-analyzer-opt-analyze-nested-blocks");
1233
Ted Kremenek49c79792011-03-24 00:28:47 +00001234 CmdArgs.push_back("-analyzer-eagerly-assume");
1235
Daniel Dunbara2aedc62009-03-18 10:01:51 +00001236 // Add default argument set.
Daniel Dunbar58f345ce2009-05-22 00:38:15 +00001237 if (!Args.hasArg(options::OPT__analyzer_no_default_checks)) {
Argyrios Kyrtzidisa6d04d52011-02-15 07:42:33 +00001238 CmdArgs.push_back("-analyzer-checker=core");
Ted Kremenek49c79792011-03-24 00:28:47 +00001239 CmdArgs.push_back("-analyzer-checker=deadcode");
1240 CmdArgs.push_back("-analyzer-checker=security");
1241
Argyrios Kyrtzidisa6d04d52011-02-15 07:42:33 +00001242 if (getToolChain().getTriple().getOS() != llvm::Triple::Win32)
1243 CmdArgs.push_back("-analyzer-checker=unix");
Ted Kremenek49c79792011-03-24 00:28:47 +00001244
Argyrios Kyrtzidisa6d04d52011-02-15 07:42:33 +00001245 if (getToolChain().getTriple().getVendor() == llvm::Triple::Apple)
Ted Kremenek49c79792011-03-24 00:28:47 +00001246 CmdArgs.push_back("-analyzer-checker=osx");
Daniel Dunbar58f345ce2009-05-22 00:38:15 +00001247 }
Daniel Dunbard067f7f2009-04-08 23:54:23 +00001248
Daniel Dunbar58f345ce2009-05-22 00:38:15 +00001249 // Set the output format. The default is plist, for (lame) historical
1250 // reasons.
1251 CmdArgs.push_back("-analyzer-output");
1252 if (Arg *A = Args.getLastArg(options::OPT__analyzer_output))
1253 CmdArgs.push_back(A->getValue(Args));
1254 else
1255 CmdArgs.push_back("plist");
Daniel Dunbara2aedc62009-03-18 10:01:51 +00001256
Ted Kremenekfe449a22010-03-22 22:32:05 +00001257 // Disable the presentation of standard compiler warnings when
1258 // using --analyze. We only want to show static analyzer diagnostics
1259 // or frontend errors.
1260 CmdArgs.push_back("-w");
1261
Daniel Dunbara2aedc62009-03-18 10:01:51 +00001262 // Add -Xanalyzer arguments when running as analyzer.
1263 Args.AddAllArgValues(CmdArgs, options::OPT_Xanalyzer);
Mike Stump11289f42009-09-09 15:08:12 +00001264 }
1265
Daniel Dunbar4eadb602009-09-10 01:21:12 +00001266 CheckCodeGenerationOptions(D, Args);
1267
Daniel Dunbar44e71222009-04-29 18:32:25 +00001268 // Perform argument translation for LLVM backend. This
1269 // takes some care in reconciling with llvm-gcc. The
1270 // issue is that llvm-gcc translates these options based on
1271 // the values in cc1, whereas we are processing based on
1272 // the driver arguments.
Daniel Dunbard067f7f2009-04-08 23:54:23 +00001273
Daniel Dunbar44e71222009-04-29 18:32:25 +00001274 // This comes from the default translation the driver + cc1
1275 // would do to enable flag_pic.
1276 //
1277 // FIXME: Centralize this code.
1278 bool PICEnabled = (Args.hasArg(options::OPT_fPIC) ||
1279 Args.hasArg(options::OPT_fpic) ||
1280 Args.hasArg(options::OPT_fPIE) ||
1281 Args.hasArg(options::OPT_fpie));
1282 bool PICDisabled = (Args.hasArg(options::OPT_mkernel) ||
1283 Args.hasArg(options::OPT_static));
1284 const char *Model = getToolChain().GetForcedPicModel();
1285 if (!Model) {
1286 if (Args.hasArg(options::OPT_mdynamic_no_pic))
1287 Model = "dynamic-no-pic";
1288 else if (PICDisabled)
1289 Model = "static";
1290 else if (PICEnabled)
1291 Model = "pic";
Daniel Dunbara2aedc62009-03-18 10:01:51 +00001292 else
Daniel Dunbar44e71222009-04-29 18:32:25 +00001293 Model = getToolChain().GetDefaultRelocationModel();
Daniel Dunbara2aedc62009-03-18 10:01:51 +00001294 }
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001295 if (StringRef(Model) != "pic") {
Daniel Dunbara1b02a22009-11-29 07:18:39 +00001296 CmdArgs.push_back("-mrelocation-model");
1297 CmdArgs.push_back(Model);
1298 }
Daniel Dunbar44e71222009-04-29 18:32:25 +00001299
1300 // Infer the __PIC__ value.
1301 //
1302 // FIXME: This isn't quite right on Darwin, which always sets
1303 // __PIC__=2.
1304 if (strcmp(Model, "pic") == 0 || strcmp(Model, "dynamic-no-pic") == 0) {
Daniel Dunbar5fe08662009-11-29 02:39:08 +00001305 CmdArgs.push_back("-pic-level");
1306 CmdArgs.push_back(Args.hasArg(options::OPT_fPIC) ? "2" : "1");
Daniel Dunbar44e71222009-04-29 18:32:25 +00001307 }
Tanya Lattnerf9d41df2009-11-04 01:18:09 +00001308 if (!Args.hasFlag(options::OPT_fmerge_all_constants,
1309 options::OPT_fno_merge_all_constants))
Chris Lattner9242b332011-04-08 18:06:54 +00001310 CmdArgs.push_back("-fno-merge-all-constants");
Daniel Dunbar306945d2009-09-16 06:17:29 +00001311
Daniel Dunbara1b02a22009-11-29 07:18:39 +00001312 // LLVM Code Generator Options.
1313
Daniel Dunbar0bb03312011-02-09 17:54:19 +00001314 if (Arg *A = Args.getLastArg(options::OPT_mregparm_EQ)) {
1315 CmdArgs.push_back("-mregparm");
1316 CmdArgs.push_back(A->getValue(Args));
1317 }
1318
Roman Divacky65b88cd2011-03-01 17:40:53 +00001319 if (Args.hasFlag(options::OPT_mrtd, options::OPT_mno_rtd, false))
1320 CmdArgs.push_back("-mrtd");
1321
Daniel Dunbara1b02a22009-11-29 07:18:39 +00001322 // FIXME: Set --enable-unsafe-fp-math.
1323 if (Args.hasFlag(options::OPT_fno_omit_frame_pointer,
1324 options::OPT_fomit_frame_pointer))
1325 CmdArgs.push_back("-mdisable-fp-elim");
1326 if (!Args.hasFlag(options::OPT_fzero_initialized_in_bss,
1327 options::OPT_fno_zero_initialized_in_bss))
1328 CmdArgs.push_back("-mno-zero-initialized-in-bss");
Daniel Dunbar7aa71f92011-02-04 02:20:39 +00001329 if (!Args.hasFlag(options::OPT_fstrict_aliasing,
1330 options::OPT_fno_strict_aliasing,
1331 getToolChain().IsStrictAliasingDefault()))
Dan Gohman10169b92010-10-14 22:36:56 +00001332 CmdArgs.push_back("-relaxed-aliasing");
Daniel Dunbar0d8ca9e2010-05-14 22:00:22 +00001333
1334 // Decide whether to use verbose asm. Verbose assembly is the default on
1335 // toolchains which have the integrated assembler on by default.
1336 bool IsVerboseAsmDefault = getToolChain().IsIntegratedAssemblerDefault();
1337 if (Args.hasFlag(options::OPT_fverbose_asm, options::OPT_fno_verbose_asm,
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001338 IsVerboseAsmDefault) ||
Daniel Dunbar0d8ca9e2010-05-14 22:00:22 +00001339 Args.hasArg(options::OPT_dA))
Daniel Dunbara1b02a22009-11-29 07:18:39 +00001340 CmdArgs.push_back("-masm-verbose");
Daniel Dunbar0d8ca9e2010-05-14 22:00:22 +00001341
Daniel Dunbara1b02a22009-11-29 07:18:39 +00001342 if (Args.hasArg(options::OPT_fdebug_pass_structure)) {
1343 CmdArgs.push_back("-mdebug-pass");
1344 CmdArgs.push_back("Structure");
1345 }
1346 if (Args.hasArg(options::OPT_fdebug_pass_arguments)) {
1347 CmdArgs.push_back("-mdebug-pass");
1348 CmdArgs.push_back("Arguments");
1349 }
1350
John McCall8517abc2010-02-19 02:45:38 +00001351 // Enable -mconstructor-aliases except on darwin, where we have to
1352 // work around a linker bug; see <rdar://problem/7651567>.
1353 if (getToolChain().getTriple().getOS() != llvm::Triple::Darwin)
1354 CmdArgs.push_back("-mconstructor-aliases");
NAKAMURA Takumi029d74b2011-02-17 08:50:50 +00001355
John McCall7ef5cb32011-03-18 02:56:14 +00001356 // Darwin's kernel doesn't support guard variables; just die if we
1357 // try to use them.
1358 if (KernelOrKext &&
1359 getToolChain().getTriple().getOS() == llvm::Triple::Darwin)
1360 CmdArgs.push_back("-fforbid-guard-variables");
1361
Douglas Gregordbe39272011-02-01 15:15:22 +00001362 if (Args.hasArg(options::OPT_mms_bitfields)) {
1363 CmdArgs.push_back("-mms-bitfields");
1364 }
John McCall8517abc2010-02-19 02:45:38 +00001365
Daniel Dunbar306945d2009-09-16 06:17:29 +00001366 // This is a coarse approximation of what llvm-gcc actually does, both
1367 // -fasynchronous-unwind-tables and -fnon-call-exceptions interact in more
1368 // complicated ways.
1369 bool AsynchronousUnwindTables =
1370 Args.hasFlag(options::OPT_fasynchronous_unwind_tables,
1371 options::OPT_fno_asynchronous_unwind_tables,
1372 getToolChain().IsUnwindTablesDefault() &&
Daniel Dunbare46b52a2010-03-20 04:52:14 +00001373 !KernelOrKext);
Daniel Dunbar306945d2009-09-16 06:17:29 +00001374 if (Args.hasFlag(options::OPT_funwind_tables, options::OPT_fno_unwind_tables,
1375 AsynchronousUnwindTables))
Daniel Dunbara1b02a22009-11-29 07:18:39 +00001376 CmdArgs.push_back("-munwind-tables");
1377
1378 if (Arg *A = Args.getLastArg(options::OPT_flimited_precision_EQ)) {
1379 CmdArgs.push_back("-mlimit-float-precision");
1380 CmdArgs.push_back(A->getValue(Args));
1381 }
Daniel Dunbar44e71222009-04-29 18:32:25 +00001382
Daniel Dunbar4dbaaa62009-05-06 03:16:41 +00001383 // FIXME: Handle -mtune=.
1384 (void) Args.hasArg(options::OPT_mtune_EQ);
Daniel Dunbar44e71222009-04-29 18:32:25 +00001385
Benjamin Kramercf4371a2009-08-05 14:30:52 +00001386 if (Arg *A = Args.getLastArg(options::OPT_mcmodel_EQ)) {
Daniel Dunbara1b02a22009-11-29 07:18:39 +00001387 CmdArgs.push_back("-mcode-model");
Benjamin Kramercf4371a2009-08-05 14:30:52 +00001388 CmdArgs.push_back(A->getValue(Args));
1389 }
1390
Daniel Dunbar3b3191f2009-09-09 22:33:08 +00001391 // Add target specific cpu and features flags.
1392 switch(getToolChain().getTriple().getArch()) {
1393 default:
1394 break;
Daniel Dunbar4dbaaa62009-05-06 03:16:41 +00001395
Daniel Dunbar0f5c5422009-09-10 04:57:17 +00001396 case llvm::Triple::arm:
1397 case llvm::Triple::thumb:
Daniel Dunbarc9388c12011-03-17 17:10:06 +00001398 AddARMTargetArgs(Args, CmdArgs, KernelOrKext);
Daniel Dunbar0f5c5422009-09-10 04:57:17 +00001399 break;
1400
Eric Christopher0b26a612010-03-02 02:41:08 +00001401 case llvm::Triple::mips:
1402 case llvm::Triple::mipsel:
Akira Hatanaka94ab5542011-09-21 02:13:07 +00001403 case llvm::Triple::mips64:
1404 case llvm::Triple::mips64el:
Eric Christopher0b26a612010-03-02 02:41:08 +00001405 AddMIPSTargetArgs(Args, CmdArgs);
1406 break;
1407
Bruno Cardoso Lopese7f211c2010-11-09 17:21:19 +00001408 case llvm::Triple::sparc:
1409 AddSparcTargetArgs(Args, CmdArgs);
1410 break;
1411
Daniel Dunbar3b3191f2009-09-09 22:33:08 +00001412 case llvm::Triple::x86:
1413 case llvm::Triple::x86_64:
1414 AddX86TargetArgs(Args, CmdArgs);
1415 break;
Daniel Dunbar44e71222009-04-29 18:32:25 +00001416 }
1417
Daniel Dunbar976a2f52010-08-11 23:07:47 +00001418 // Pass the linker version in use.
1419 if (Arg *A = Args.getLastArg(options::OPT_mlinker_version_EQ)) {
1420 CmdArgs.push_back("-target-linker-version");
1421 CmdArgs.push_back(A->getValue(Args));
1422 }
1423
Nick Lewycky75033772011-02-02 06:43:03 +00001424 // -mno-omit-leaf-frame-pointer is the default on Darwin.
Daniel Dunbarbb7ac522010-07-01 01:31:45 +00001425 if (Args.hasFlag(options::OPT_momit_leaf_frame_pointer,
Nick Lewycky75033772011-02-02 06:43:03 +00001426 options::OPT_mno_omit_leaf_frame_pointer,
1427 getToolChain().getTriple().getOS() != llvm::Triple::Darwin))
Daniel Dunbarbb7ac522010-07-01 01:31:45 +00001428 CmdArgs.push_back("-momit-leaf-frame-pointer");
1429
Dan Gohmand1e76b92010-01-08 02:20:44 +00001430 // -fno-math-errno is default.
1431 if (Args.hasFlag(options::OPT_fmath_errno,
Daniel Dunbar44e71222009-04-29 18:32:25 +00001432 options::OPT_fno_math_errno,
Dan Gohmand1e76b92010-01-08 02:20:44 +00001433 false))
1434 CmdArgs.push_back("-fmath-errno");
Daniel Dunbar44e71222009-04-29 18:32:25 +00001435
Daniel Dunbarfcc49a82010-05-12 18:19:58 +00001436 // Explicitly error on some things we know we don't support and can't just
1437 // ignore.
1438 types::ID InputType = Inputs[0].getType();
Daniel Dunbar4ed214a2010-09-24 19:39:37 +00001439 if (!Args.hasArg(options::OPT_fallow_unsupported)) {
1440 Arg *Unsupported;
Peter Collingbourne77b0e7f22011-07-12 19:35:15 +00001441 if ((Unsupported = Args.getLastArg(options::OPT_iframework)))
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001442 D.Diag(diag::err_drv_clang_unsupported)
Daniel Dunbarfcc49a82010-05-12 18:19:58 +00001443 << Unsupported->getOption().getName();
Daniel Dunbar4ed214a2010-09-24 19:39:37 +00001444
1445 if (types::isCXX(InputType) &&
1446 getToolChain().getTriple().getOS() == llvm::Triple::Darwin &&
1447 getToolChain().getTriple().getArch() == llvm::Triple::x86) {
Bob Wilson0d45f582011-08-13 23:48:55 +00001448 if ((Unsupported = Args.getLastArg(options::OPT_fapple_kext)) ||
1449 (Unsupported = Args.getLastArg(options::OPT_mkernel)))
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001450 D.Diag(diag::err_drv_clang_unsupported_opt_cxx_darwin_i386)
Daniel Dunbar4ed214a2010-09-24 19:39:37 +00001451 << Unsupported->getOption().getName();
1452 }
Daniel Dunbarfcc49a82010-05-12 18:19:58 +00001453 }
1454
Daniel Dunbara2aedc62009-03-18 10:01:51 +00001455 Args.AddAllArgs(CmdArgs, options::OPT_v);
Daniel Dunbard4352752010-08-24 22:44:13 +00001456 Args.AddLastArg(CmdArgs, options::OPT_H);
Chad Rosierbe10f982011-08-02 17:58:04 +00001457 if (D.CCPrintHeaders && !D.CCGenDiagnostics) {
Daniel Dunbarac540b32011-02-02 21:11:35 +00001458 CmdArgs.push_back("-header-include-file");
1459 CmdArgs.push_back(D.CCPrintHeadersFilename ?
1460 D.CCPrintHeadersFilename : "-");
1461 }
Daniel Dunbara2aedc62009-03-18 10:01:51 +00001462 Args.AddLastArg(CmdArgs, options::OPT_P);
Mike Stump11289f42009-09-09 15:08:12 +00001463 Args.AddLastArg(CmdArgs, options::OPT_print_ivar_layout);
Daniel Dunbara2aedc62009-03-18 10:01:51 +00001464
Chad Rosierbe10f982011-08-02 17:58:04 +00001465 if (D.CCLogDiagnostics && !D.CCGenDiagnostics) {
Daniel Dunbar529c03b2011-04-07 18:01:20 +00001466 CmdArgs.push_back("-diagnostic-log-file");
1467 CmdArgs.push_back(D.CCLogDiagnosticsFilename ?
1468 D.CCLogDiagnosticsFilename : "-");
1469 }
1470
Daniel Dunbara2aedc62009-03-18 10:01:51 +00001471 // Special case debug options to only pass -g to clang. This is
1472 // wrong.
Rafael Espindola08a692a2010-03-07 04:46:18 +00001473 Args.ClaimAllArgs(options::OPT_g_Group);
Daniel Dunbar4083d042010-05-12 18:19:55 +00001474 if (Arg *A = Args.getLastArg(options::OPT_g_Group))
1475 if (!A->getOption().matches(options::OPT_g0))
1476 CmdArgs.push_back("-g");
Daniel Dunbara2aedc62009-03-18 10:01:51 +00001477
Rafael Espindola66bfb2752010-05-06 21:06:04 +00001478 Args.AddAllArgs(CmdArgs, options::OPT_ffunction_sections);
1479 Args.AddAllArgs(CmdArgs, options::OPT_fdata_sections);
1480
Chris Lattner3c77a352010-06-22 00:03:40 +00001481 Args.AddAllArgs(CmdArgs, options::OPT_finstrument_functions);
1482
Nick Lewycky207bce32011-04-21 23:44:07 +00001483 if (Args.hasArg(options::OPT_ftest_coverage) ||
1484 Args.hasArg(options::OPT_coverage))
1485 CmdArgs.push_back("-femit-coverage-notes");
1486 if (Args.hasArg(options::OPT_fprofile_arcs) ||
1487 Args.hasArg(options::OPT_coverage))
1488 CmdArgs.push_back("-femit-coverage-data");
1489
Nick Lewycky480cb992011-05-04 20:46:58 +00001490 if (C.getArgs().hasArg(options::OPT_c) ||
1491 C.getArgs().hasArg(options::OPT_S)) {
1492 if (Output.isFilename()) {
Nick Lewycky85c011d2011-05-05 00:08:20 +00001493 CmdArgs.push_back("-coverage-file");
1494 CmdArgs.push_back(Args.MakeArgString(Output.getFilename()));
Nick Lewycky480cb992011-05-04 20:46:58 +00001495 }
1496 }
1497
Daniel Dunbara2aedc62009-03-18 10:01:51 +00001498 Args.AddLastArg(CmdArgs, options::OPT_nostdinc);
Douglas Gregor64b046f2010-03-24 20:13:48 +00001499 Args.AddLastArg(CmdArgs, options::OPT_nostdincxx);
Rafael Espindolab3549d72009-10-26 13:36:57 +00001500 Args.AddLastArg(CmdArgs, options::OPT_nobuiltininc);
Daniel Dunbara2aedc62009-03-18 10:01:51 +00001501
Daniel Dunbar34e0b8c2009-12-15 01:02:52 +00001502 // Pass the path to compiler resource files.
Daniel Dunbar34e0b8c2009-12-15 01:02:52 +00001503 CmdArgs.push_back("-resource-dir");
Daniel Dunbar3f3e2cd2010-01-20 02:35:16 +00001504 CmdArgs.push_back(D.ResourceDir.c_str());
Daniel Dunbar9dc82a22009-04-07 21:42:00 +00001505
Argyrios Kyrtzidis71731d62010-11-03 22:45:23 +00001506 Args.AddLastArg(CmdArgs, options::OPT_working_directory);
1507
John McCalld70fb982011-06-15 23:25:17 +00001508 if (!Args.hasArg(options::OPT_fno_objc_arc)) {
Argyrios Kyrtzidisc44b93d2011-07-07 04:00:39 +00001509 if (const Arg *A = Args.getLastArg(options::OPT_ccc_arcmt_check,
Argyrios Kyrtzidis7fbd97f2011-07-09 20:00:58 +00001510 options::OPT_ccc_arcmt_modify,
1511 options::OPT_ccc_arcmt_migrate)) {
John McCalld70fb982011-06-15 23:25:17 +00001512 switch (A->getOption().getID()) {
1513 default:
1514 llvm_unreachable("missed a case");
Argyrios Kyrtzidisc44b93d2011-07-07 04:00:39 +00001515 case options::OPT_ccc_arcmt_check:
John McCalld70fb982011-06-15 23:25:17 +00001516 CmdArgs.push_back("-arcmt-check");
1517 break;
Argyrios Kyrtzidisc44b93d2011-07-07 04:00:39 +00001518 case options::OPT_ccc_arcmt_modify:
John McCalld70fb982011-06-15 23:25:17 +00001519 CmdArgs.push_back("-arcmt-modify");
1520 break;
Argyrios Kyrtzidis7fbd97f2011-07-09 20:00:58 +00001521 case options::OPT_ccc_arcmt_migrate:
1522 CmdArgs.push_back("-arcmt-migrate");
1523 CmdArgs.push_back("-arcmt-migrate-directory");
1524 CmdArgs.push_back(A->getValue(Args));
Argyrios Kyrtzidisd5713632011-07-19 17:20:03 +00001525
1526 Args.AddLastArg(CmdArgs, options::OPT_arcmt_migrate_report_output);
1527 Args.AddLastArg(CmdArgs, options::OPT_arcmt_migrate_emit_arc_errors);
Argyrios Kyrtzidis7fbd97f2011-07-09 20:00:58 +00001528 break;
John McCalld70fb982011-06-15 23:25:17 +00001529 }
1530 }
1531 }
Eric Christopher84fbdb42011-08-19 00:30:14 +00001532
Daniel Dunbard067f7f2009-04-08 23:54:23 +00001533 // Add preprocessing options like -I, -D, etc. if we are using the
1534 // preprocessor.
1535 //
1536 // FIXME: Support -fpreprocessed
Daniel Dunbard067f7f2009-04-08 23:54:23 +00001537 if (types::getPreprocessedType(InputType) != types::TY_INVALID)
Douglas Gregor111af7d2009-04-18 00:34:01 +00001538 AddPreprocessingOptions(D, Args, CmdArgs, Output, Inputs);
Daniel Dunbara2aedc62009-03-18 10:01:51 +00001539
Rafael Espindolaa7431922011-07-21 23:40:37 +00001540 // Don't warn about "clang -c -DPIC -fPIC test.i" because libtool.m4 assumes
1541 // that "The compiler can only warn and ignore the option if not recognized".
1542 // When building with ccache, it will pass -D options to clang even on
1543 // preprocessed inputs and configure concludes that -fPIC is not supported.
1544 Args.ClaimAllArgs(options::OPT_D);
1545
Daniel Dunbar58f78332009-09-17 06:53:36 +00001546 // Manually translate -O to -O2 and -O4 to -O3; let clang reject
Daniel Dunbar13864952009-03-24 20:17:30 +00001547 // others.
1548 if (Arg *A = Args.getLastArg(options::OPT_O_Group)) {
Daniel Dunbar0bfb21e2009-11-19 03:26:40 +00001549 if (A->getOption().matches(options::OPT_O4))
Daniel Dunbar13864952009-03-24 20:17:30 +00001550 CmdArgs.push_back("-O3");
Daniel Dunbar9296f632010-05-27 06:51:08 +00001551 else if (A->getOption().matches(options::OPT_O) &&
1552 A->getValue(Args)[0] == '\0')
Daniel Dunbar58f78332009-09-17 06:53:36 +00001553 CmdArgs.push_back("-O2");
Daniel Dunbara2aedc62009-03-18 10:01:51 +00001554 else
Daniel Dunbar7ef5ed62009-03-18 23:39:35 +00001555 A->render(Args, CmdArgs);
Daniel Dunbara2aedc62009-03-18 10:01:51 +00001556 }
1557
Daniel Dunbar945577c2009-10-29 02:24:45 +00001558 Args.AddAllArgs(CmdArgs, options::OPT_W_Group);
1559 Args.AddLastArg(CmdArgs, options::OPT_pedantic);
1560 Args.AddLastArg(CmdArgs, options::OPT_pedantic_errors);
Daniel Dunbara2aedc62009-03-18 10:01:51 +00001561 Args.AddLastArg(CmdArgs, options::OPT_w);
Daniel Dunbarc44b4cc2009-04-07 22:13:21 +00001562
1563 // Handle -{std, ansi, trigraphs} -- take the last of -{std, ansi}
1564 // (-ansi is equivalent to -std=c89).
1565 //
1566 // If a std is supplied, only add -trigraphs if it follows the
1567 // option.
1568 if (Arg *Std = Args.getLastArg(options::OPT_std_EQ, options::OPT_ansi)) {
1569 if (Std->getOption().matches(options::OPT_ansi))
Nuno Lopes275225d2009-10-16 14:28:06 +00001570 if (types::isCXX(InputType))
Daniel Dunbar1c7577c2009-11-04 06:24:38 +00001571 CmdArgs.push_back("-std=c++98");
Nuno Lopes275225d2009-10-16 14:28:06 +00001572 else
Daniel Dunbar1c7577c2009-11-04 06:24:38 +00001573 CmdArgs.push_back("-std=c89");
Daniel Dunbarc44b4cc2009-04-07 22:13:21 +00001574 else
1575 Std->render(Args, CmdArgs);
1576
Daniel Dunbar3f1a1ff2010-06-14 21:23:08 +00001577 if (Arg *A = Args.getLastArg(options::OPT_std_EQ, options::OPT_ansi,
1578 options::OPT_trigraphs))
1579 if (A != Std)
Daniel Dunbarc44b4cc2009-04-07 22:13:21 +00001580 A->render(Args, CmdArgs);
Daniel Dunbar72a60902009-04-26 01:10:38 +00001581 } else {
1582 // Honor -std-default.
Daniel Dunbar12998192010-01-29 21:03:02 +00001583 //
1584 // FIXME: Clang doesn't correctly handle -std= when the input language
1585 // doesn't match. For the time being just ignore this for C++ inputs;
1586 // eventually we want to do all the standard defaulting here instead of
1587 // splitting it between the driver and clang -cc1.
1588 if (!types::isCXX(InputType))
1589 Args.AddAllArgsTranslated(CmdArgs, options::OPT_std_default_EQ,
1590 "-std=", /*Joined=*/true);
Daniel Dunbarc44b4cc2009-04-07 22:13:21 +00001591 Args.AddLastArg(CmdArgs, options::OPT_trigraphs);
Daniel Dunbar72a60902009-04-26 01:10:38 +00001592 }
Daniel Dunbard067f7f2009-04-08 23:54:23 +00001593
Chandler Carruthb009b142011-04-23 06:30:43 +00001594 // Map the bizarre '-Wwrite-strings' flag to a more sensible
1595 // '-fconst-strings'; this better indicates its actual behavior.
1596 if (Args.hasFlag(options::OPT_Wwrite_strings, options::OPT_Wno_write_strings,
1597 false)) {
1598 // For perfect compatibility with GCC, we do this even in the presence of
1599 // '-w'. This flag names something other than a warning for GCC.
1600 CmdArgs.push_back("-fconst-strings");
1601 }
1602
Chandler Carruth61fbf622011-04-23 09:27:53 +00001603 // GCC provides a macro definition '__DEPRECATED' when -Wdeprecated is active
Chandler Carruth30483fb2011-04-23 19:48:40 +00001604 // during C++ compilation, which it is by default. GCC keeps this define even
1605 // in the presence of '-w', match this behavior bug-for-bug.
1606 if (types::isCXX(InputType) &&
1607 Args.hasFlag(options::OPT_Wdeprecated, options::OPT_Wno_deprecated,
1608 true)) {
1609 CmdArgs.push_back("-fdeprecated-macro");
Chandler Carruth61fbf622011-04-23 09:27:53 +00001610 }
1611
Chandler Carruthe0391482010-05-22 02:21:53 +00001612 // Translate GCC's misnamer '-fasm' arguments to '-fgnu-keywords'.
1613 if (Arg *Asm = Args.getLastArg(options::OPT_fasm, options::OPT_fno_asm)) {
1614 if (Asm->getOption().matches(options::OPT_fasm))
1615 CmdArgs.push_back("-fgnu-keywords");
1616 else
1617 CmdArgs.push_back("-fno-gnu-keywords");
1618 }
1619
Rafael Espindola4cfa7972011-05-02 17:43:32 +00001620 if (ShouldDisableCFI(Args, getToolChain()))
1621 CmdArgs.push_back("-fno-dwarf2-cfi-asm");
Rafael Espindolae2641872011-04-30 18:35:43 +00001622
Daniel Dunbara2aedc62009-03-18 10:01:51 +00001623 if (Arg *A = Args.getLastArg(options::OPT_ftemplate_depth_)) {
1624 CmdArgs.push_back("-ftemplate-depth");
1625 CmdArgs.push_back(A->getValue(Args));
1626 }
1627
Argyrios Kyrtzidisef6c8da2010-11-18 00:20:36 +00001628 if (Arg *A = Args.getLastArg(options::OPT_Wlarge_by_value_copy_EQ,
1629 options::OPT_Wlarge_by_value_copy_def)) {
1630 CmdArgs.push_back("-Wlarge-by-value-copy");
Argyrios Kyrtzidisaf84ec02010-11-17 23:11:54 +00001631 if (A->getNumValues())
1632 CmdArgs.push_back(A->getValue(Args));
1633 else
Argyrios Kyrtzidisef6c8da2010-11-18 00:20:36 +00001634 CmdArgs.push_back("64"); // default value for -Wlarge-by-value-copy.
Argyrios Kyrtzidisaf84ec02010-11-17 23:11:54 +00001635 }
1636
Daniel Dunbarfffd1812009-11-19 04:00:53 +00001637 if (Args.hasArg(options::OPT__relocatable_pch))
Daniel Dunbar8bed86c2009-11-20 22:21:36 +00001638 CmdArgs.push_back("-relocatable-pch");
Mike Stump11289f42009-09-09 15:08:12 +00001639
Daniel Dunbar1c7577c2009-11-04 06:24:38 +00001640 if (Arg *A = Args.getLastArg(options::OPT_fconstant_string_class_EQ)) {
1641 CmdArgs.push_back("-fconstant-string-class");
1642 CmdArgs.push_back(A->getValue(Args));
1643 }
David Chisnall5778fce2009-08-31 16:41:57 +00001644
Chris Lattnere23003d2010-01-09 21:54:33 +00001645 if (Arg *A = Args.getLastArg(options::OPT_ftabstop_EQ)) {
1646 CmdArgs.push_back("-ftabstop");
1647 CmdArgs.push_back(A->getValue(Args));
1648 }
1649
Chris Lattnerb35583d2010-04-07 20:49:23 +00001650 CmdArgs.push_back("-ferror-limit");
1651 if (Arg *A = Args.getLastArg(options::OPT_ferror_limit_EQ))
1652 CmdArgs.push_back(A->getValue(Args));
1653 else
1654 CmdArgs.push_back("19");
Douglas Gregorffed1cb2010-04-20 07:18:24 +00001655
Chandler Carrutha77a7272010-05-06 04:55:18 +00001656 if (Arg *A = Args.getLastArg(options::OPT_fmacro_backtrace_limit_EQ)) {
1657 CmdArgs.push_back("-fmacro-backtrace-limit");
Douglas Gregorcd121fb2010-05-04 17:13:42 +00001658 CmdArgs.push_back(A->getValue(Args));
Chandler Carrutha77a7272010-05-06 04:55:18 +00001659 }
1660
1661 if (Arg *A = Args.getLastArg(options::OPT_ftemplate_backtrace_limit_EQ)) {
1662 CmdArgs.push_back("-ftemplate-backtrace-limit");
Douglas Gregorffed1cb2010-04-20 07:18:24 +00001663 CmdArgs.push_back(A->getValue(Args));
Chandler Carrutha77a7272010-05-06 04:55:18 +00001664 }
1665
Daniel Dunbar2c978472009-11-04 06:24:47 +00001666 // Pass -fmessage-length=.
Daniel Dunbar84bb7932009-11-30 08:40:54 +00001667 CmdArgs.push_back("-fmessage-length");
Daniel Dunbar2c978472009-11-04 06:24:47 +00001668 if (Arg *A = Args.getLastArg(options::OPT_fmessage_length_EQ)) {
Daniel Dunbar84bb7932009-11-30 08:40:54 +00001669 CmdArgs.push_back(A->getValue(Args));
Daniel Dunbar2c978472009-11-04 06:24:47 +00001670 } else {
1671 // If -fmessage-length=N was not specified, determine whether this is a
1672 // terminal and, if so, implicitly define -fmessage-length appropriately.
1673 unsigned N = llvm::sys::Process::StandardErrColumns();
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001674 CmdArgs.push_back(Args.MakeArgString(Twine(N)));
Daniel Dunbar2c978472009-11-04 06:24:47 +00001675 }
1676
Daniel Dunbare357d562009-12-03 18:42:11 +00001677 if (const Arg *A = Args.getLastArg(options::OPT_fvisibility_EQ)) {
1678 CmdArgs.push_back("-fvisibility");
1679 CmdArgs.push_back(A->getValue(Args));
1680 }
1681
Douglas Gregor08329632010-06-15 17:05:35 +00001682 Args.AddLastArg(CmdArgs, options::OPT_fvisibility_inlines_hidden);
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001683
Daniel Dunbare46b52a2010-03-20 04:52:14 +00001684 // -fhosted is default.
1685 if (KernelOrKext || Args.hasFlag(options::OPT_ffreestanding,
1686 options::OPT_fhosted,
1687 false))
1688 CmdArgs.push_back("-ffreestanding");
1689
Daniel Dunbare357d562009-12-03 18:42:11 +00001690 // Forward -f (flag) options which we can pass directly.
Mike Stumpd9546382009-12-12 01:27:46 +00001691 Args.AddLastArg(CmdArgs, options::OPT_fcatch_undefined_behavior);
Daniel Dunbar3a148f22009-04-07 21:51:40 +00001692 Args.AddLastArg(CmdArgs, options::OPT_femit_all_decls);
Daniel Dunbar3a148f22009-04-07 21:51:40 +00001693 Args.AddLastArg(CmdArgs, options::OPT_fheinous_gnu_extensions);
Devang Patel91bbb552010-09-30 19:05:55 +00001694 Args.AddLastArg(CmdArgs, options::OPT_flimit_debug_info);
Daniel Dunbar733b0f82011-03-01 18:49:30 +00001695 if (getToolChain().SupportsProfiling())
1696 Args.AddLastArg(CmdArgs, options::OPT_pg);
Daniel Dunbar35621a92010-03-16 16:57:46 +00001697
1698 // -flax-vector-conversions is default.
1699 if (!Args.hasFlag(options::OPT_flax_vector_conversions,
1700 options::OPT_fno_lax_vector_conversions))
1701 CmdArgs.push_back("-fno-lax-vector-conversions");
1702
Fariborz Jahaniana4cfff82011-01-07 01:05:02 +00001703 if (Args.getLastArg(options::OPT_fapple_kext))
1704 CmdArgs.push_back("-fapple-kext");
1705
Fariborz Jahaniana4404f22009-05-22 20:17:16 +00001706 Args.AddLastArg(CmdArgs, options::OPT_fobjc_sender_dependent_dispatch);
Chris Lattner69686412009-04-21 05:34:31 +00001707 Args.AddLastArg(CmdArgs, options::OPT_fdiagnostics_print_source_range_info);
Douglas Gregoreec975c2010-08-19 20:24:43 +00001708 Args.AddLastArg(CmdArgs, options::OPT_fdiagnostics_parseable_fixits);
Daniel Dunbar3a148f22009-04-07 21:51:40 +00001709 Args.AddLastArg(CmdArgs, options::OPT_ftime_report);
1710 Args.AddLastArg(CmdArgs, options::OPT_ftrapv);
David Chisnalldd84ef12010-09-17 18:29:54 +00001711
1712 if (Arg *A = Args.getLastArg(options::OPT_ftrapv_handler_EQ)) {
1713 CmdArgs.push_back("-ftrapv-handler");
1714 CmdArgs.push_back(A->getValue(Args));
1715 }
1716
Evan Cheng04c94292011-04-08 21:37:45 +00001717 // Forward -ftrap_function= options to the backend.
1718 if (Arg *A = Args.getLastArg(options::OPT_ftrap_function_EQ)) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001719 StringRef FuncName = A->getValue(Args);
Evan Cheng04c94292011-04-08 21:37:45 +00001720 CmdArgs.push_back("-backend-option");
1721 CmdArgs.push_back(Args.MakeArgString("-trap-func=" + FuncName));
1722 }
1723
Chandler Carruth6e501032011-03-27 00:04:55 +00001724 // -fno-strict-overflow implies -fwrapv if it isn't disabled, but
1725 // -fstrict-overflow won't turn off an explicitly enabled -fwrapv.
1726 if (Arg *A = Args.getLastArg(options::OPT_fwrapv,
1727 options::OPT_fno_wrapv)) {
1728 if (A->getOption().matches(options::OPT_fwrapv))
1729 CmdArgs.push_back("-fwrapv");
1730 } else if (Arg *A = Args.getLastArg(options::OPT_fstrict_overflow,
1731 options::OPT_fno_strict_overflow)) {
1732 if (A->getOption().matches(options::OPT_fno_strict_overflow))
1733 CmdArgs.push_back("-fwrapv");
1734 }
Daniel Dunbar3a148f22009-04-07 21:51:40 +00001735 Args.AddLastArg(CmdArgs, options::OPT_fwritable_strings);
Eric Christopherf387dbd2010-08-07 23:08:14 +00001736 Args.AddLastArg(CmdArgs, options::OPT_funroll_loops);
Daniel Dunbara2aedc62009-03-18 10:01:51 +00001737
Daniel Dunbara77eaeb2009-09-03 04:54:28 +00001738 Args.AddLastArg(CmdArgs, options::OPT_pthread);
1739
Daniel Dunbar4930e332009-11-17 08:07:36 +00001740 // -stack-protector=0 is default.
1741 unsigned StackProtectorLevel = 0;
Bill Wendlingd63bbad2009-06-28 07:36:13 +00001742 if (Arg *A = Args.getLastArg(options::OPT_fno_stack_protector,
1743 options::OPT_fstack_protector_all,
1744 options::OPT_fstack_protector)) {
Daniel Dunbar4930e332009-11-17 08:07:36 +00001745 if (A->getOption().matches(options::OPT_fstack_protector))
1746 StackProtectorLevel = 1;
1747 else if (A->getOption().matches(options::OPT_fstack_protector_all))
1748 StackProtectorLevel = 2;
Nico Weberdd473632011-08-23 07:38:27 +00001749 } else {
1750 StackProtectorLevel =
1751 getToolChain().GetDefaultStackProtectorLevel(KernelOrKext);
1752 }
Daniel Dunbar4930e332009-11-17 08:07:36 +00001753 if (StackProtectorLevel) {
1754 CmdArgs.push_back("-stack-protector");
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001755 CmdArgs.push_back(Args.MakeArgString(Twine(StackProtectorLevel)));
Bill Wendlingd63bbad2009-06-28 07:36:13 +00001756 }
1757
Eric Christopherd5c45f62011-05-02 21:18:22 +00001758 // Translate -mstackrealign
1759 if (Args.hasArg(options::OPT_mstackrealign)) {
1760 CmdArgs.push_back("-backend-option");
1761 CmdArgs.push_back("-force-align-stack");
1762 }
Eric Christopher84fbdb42011-08-19 00:30:14 +00001763
Daniel Dunbard18049a2009-04-07 21:16:11 +00001764 // Forward -f options with positive and negative forms; we translate
1765 // these by hand.
1766
Fariborz Jahaniana4cfff82011-01-07 01:05:02 +00001767 if (Args.hasArg(options::OPT_mkernel)) {
Daniel Dunbar80f787c2011-02-04 17:24:47 +00001768 if (!Args.hasArg(options::OPT_fapple_kext) && types::isCXX(InputType))
Fariborz Jahaniana4cfff82011-01-07 01:05:02 +00001769 CmdArgs.push_back("-fapple-kext");
1770 if (!Args.hasArg(options::OPT_fbuiltin))
1771 CmdArgs.push_back("-fno-builtin");
1772 }
Daniel Dunbar4930e332009-11-17 08:07:36 +00001773 // -fbuiltin is default.
Fariborz Jahaniana4cfff82011-01-07 01:05:02 +00001774 else if (!Args.hasFlag(options::OPT_fbuiltin, options::OPT_fno_builtin))
Daniel Dunbar484afa22009-11-19 04:55:23 +00001775 CmdArgs.push_back("-fno-builtin");
Daniel Dunbard18049a2009-04-07 21:16:11 +00001776
Nuno Lopes13c88c72009-12-16 16:59:22 +00001777 if (!Args.hasFlag(options::OPT_fassume_sane_operator_new,
1778 options::OPT_fno_assume_sane_operator_new))
1779 CmdArgs.push_back("-fno-assume-sane-operator-new");
1780
Daniel Dunbar4930e332009-11-17 08:07:36 +00001781 // -fblocks=0 is default.
1782 if (Args.hasFlag(options::OPT_fblocks, options::OPT_fno_blocks,
David Chisnallda209912011-02-28 17:11:43 +00001783 getToolChain().IsBlocksDefault()) ||
1784 (Args.hasArg(options::OPT_fgnu_runtime) &&
1785 Args.hasArg(options::OPT_fobjc_nonfragile_abi) &&
1786 !Args.hasArg(options::OPT_fno_blocks))) {
Daniel Dunbar4930e332009-11-17 08:07:36 +00001787 CmdArgs.push_back("-fblocks");
John McCall7959fee2011-09-09 20:41:01 +00001788
1789 if (!Args.hasArg(options::OPT_fgnu_runtime) &&
1790 !getToolChain().hasBlocksRuntime())
1791 CmdArgs.push_back("-fblocks-runtime-optional");
David Chisnall950a9512009-11-17 19:33:30 +00001792 }
Daniel Dunbard18049a2009-04-07 21:16:11 +00001793
John McCalldfea9982010-04-09 19:12:06 +00001794 // -faccess-control is default.
John McCall3155f572010-04-09 19:03:51 +00001795 if (Args.hasFlag(options::OPT_fno_access_control,
1796 options::OPT_faccess_control,
John McCalldfea9982010-04-09 19:12:06 +00001797 false))
John McCall3155f572010-04-09 19:03:51 +00001798 CmdArgs.push_back("-fno-access-control");
John McCall59bb1d42010-03-17 01:32:13 +00001799
Anders Carlssond470fef2010-11-21 00:09:52 +00001800 // -felide-constructors is the default.
1801 if (Args.hasFlag(options::OPT_fno_elide_constructors,
1802 options::OPT_felide_constructors,
1803 false))
1804 CmdArgs.push_back("-fno-elide-constructors");
1805
Daniel Dunbar0730e4f2009-11-17 07:06:20 +00001806 // -frtti is default.
Daniel Dunbare46b52a2010-03-20 04:52:14 +00001807 if (KernelOrKext ||
1808 !Args.hasFlag(options::OPT_frtti, options::OPT_fno_rtti))
Daniel Dunbar484afa22009-11-19 04:55:23 +00001809 CmdArgs.push_back("-fno-rtti");
Mike Stump183c3d22009-07-31 23:15:31 +00001810
Argyrios Kyrtzidis74825bc2010-10-08 00:25:19 +00001811 // -fshort-enums=0 is default.
1812 // FIXME: Are there targers where -fshort-enums is on by default ?
1813 if (Args.hasFlag(options::OPT_fshort_enums,
1814 options::OPT_fno_short_enums, false))
1815 CmdArgs.push_back("-fshort-enums");
1816
Daniel Dunbard609b7b2009-11-17 06:37:03 +00001817 // -fsigned-char is default.
Daniel Dunbar5bdd2992009-11-25 10:14:30 +00001818 if (!Args.hasFlag(options::OPT_fsigned_char, options::OPT_funsigned_char,
Daniel Dunbard609b7b2009-11-17 06:37:03 +00001819 isSignedCharDefault(getToolChain().getTriple())))
Daniel Dunbar5fe08662009-11-29 02:39:08 +00001820 CmdArgs.push_back("-fno-signed-char");
Eli Friedman327f0b52009-06-05 07:21:14 +00001821
Anders Carlssonfcd764a2010-02-06 23:23:06 +00001822 // -fthreadsafe-static is default.
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001823 if (!Args.hasFlag(options::OPT_fthreadsafe_statics,
Anders Carlssonfcd764a2010-02-06 23:23:06 +00001824 options::OPT_fno_threadsafe_statics))
1825 CmdArgs.push_back("-fno-threadsafe-statics");
1826
Daniel Dunbarfe06df42010-03-20 04:15:41 +00001827 // -fuse-cxa-atexit is default.
Anton Korobeynikov82b33332010-09-11 11:17:06 +00001828 if (KernelOrKext ||
1829 !Args.hasFlag(options::OPT_fuse_cxa_atexit, options::OPT_fno_use_cxa_atexit,
NAKAMURA Takumi6bdc8a22010-10-10 01:53:03 +00001830 getToolChain().getTriple().getOS() != llvm::Triple::Cygwin &&
NAKAMURA Takumi31ea2f12011-02-17 08:51:38 +00001831 getToolChain().getTriple().getOS() != llvm::Triple::MinGW32))
Daniel Dunbarfe06df42010-03-20 04:15:41 +00001832 CmdArgs.push_back("-fno-use-cxa-atexit");
1833
Daniel Dunbar0730e4f2009-11-17 07:06:20 +00001834 // -fms-extensions=0 is default.
Daniel Dunbar5bdd2992009-11-25 10:14:30 +00001835 if (Args.hasFlag(options::OPT_fms_extensions, options::OPT_fno_ms_extensions,
Daniel Dunbar0730e4f2009-11-17 07:06:20 +00001836 getToolChain().getTriple().getOS() == llvm::Triple::Win32))
1837 CmdArgs.push_back("-fms-extensions");
1838
Francois Pichet1b4f1632011-09-17 04:32:15 +00001839 // -fms-compatibility=0 is default.
1840 if (Args.hasFlag(options::OPT_fms_compatibility, options::OPT_fno_ms_compatibility,
1841 getToolChain().getTriple().getOS() == llvm::Triple::Win32))
1842 CmdArgs.push_back("-fms-compatibility");
1843
Michael J. Spencer4992ca4b2010-10-21 05:21:48 +00001844 // -fmsc-version=1300 is default.
1845 if (Args.hasFlag(options::OPT_fms_extensions, options::OPT_fno_ms_extensions,
1846 getToolChain().getTriple().getOS() == llvm::Triple::Win32) ||
1847 Args.hasArg(options::OPT_fmsc_version)) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001848 StringRef msc_ver = Args.getLastArgValue(options::OPT_fmsc_version);
Michael J. Spencer4992ca4b2010-10-21 05:21:48 +00001849 if (msc_ver.empty())
1850 CmdArgs.push_back("-fmsc-version=1300");
1851 else
1852 CmdArgs.push_back(Args.MakeArgString("-fmsc-version=" + msc_ver));
1853 }
1854
1855
Dawn Perchik68bb1b42010-09-02 23:59:25 +00001856 // -fborland-extensions=0 is default.
1857 if (Args.hasFlag(options::OPT_fborland_extensions,
1858 options::OPT_fno_borland_extensions, false))
1859 CmdArgs.push_back("-fborland-extensions");
1860
Francois Pichet02744872011-09-01 16:38:08 +00001861 // -fno-delayed-template-parsing is default, except for Windows where MSVC STL
1862 // needs it.
Francois Pichet1c229c02011-04-22 22:18:13 +00001863 if (Args.hasFlag(options::OPT_fdelayed_template_parsing,
1864 options::OPT_fno_delayed_template_parsing,
Francois Pichet02744872011-09-01 16:38:08 +00001865 getToolChain().getTriple().getOS() == llvm::Triple::Win32))
Francois Pichet35bc5de2011-08-26 00:22:34 +00001866 CmdArgs.push_back("-fdelayed-template-parsing");
Francois Pichet1c229c02011-04-22 22:18:13 +00001867
Chandler Carruthe03aa552010-04-17 20:17:31 +00001868 // -fgnu-keywords default varies depending on language; only pass if
1869 // specified.
1870 if (Arg *A = Args.getLastArg(options::OPT_fgnu_keywords,
Daniel Dunbardb059592010-04-24 17:56:39 +00001871 options::OPT_fno_gnu_keywords))
1872 A->render(Args, CmdArgs);
Chandler Carruthe03aa552010-04-17 20:17:31 +00001873
Rafael Espindola922a6242011-06-02 17:30:53 +00001874 if (Args.hasFlag(options::OPT_fgnu89_inline,
1875 options::OPT_fno_gnu89_inline,
1876 false))
Rafael Espindolafb2af642011-06-02 16:13:27 +00001877 CmdArgs.push_back("-fgnu89-inline");
1878
Daniel Dunbar4930e332009-11-17 08:07:36 +00001879 // -fobjc-nonfragile-abi=0 is default.
John McCall24fc0de2011-07-06 00:26:06 +00001880 ObjCRuntime objCRuntime;
John McCallb5f652e2011-06-22 00:53:57 +00001881 unsigned objcABIVersion = 0;
Daniel Dunbar4930e332009-11-17 08:07:36 +00001882 if (types::isObjC(InputType)) {
John McCall24fc0de2011-07-06 00:26:06 +00001883 bool NeXTRuntimeIsDefault
1884 = (IsRewriter || getToolChain().getTriple().isOSDarwin());
1885 if (Args.hasFlag(options::OPT_fnext_runtime, options::OPT_fgnu_runtime,
John McCall75bc7732011-07-06 02:36:30 +00001886 NeXTRuntimeIsDefault)) {
John McCall24fc0de2011-07-06 00:26:06 +00001887 objCRuntime.setKind(ObjCRuntime::NeXT);
John McCall75bc7732011-07-06 02:36:30 +00001888 } else {
1889 CmdArgs.push_back("-fgnu-runtime");
John McCall24fc0de2011-07-06 00:26:06 +00001890 objCRuntime.setKind(ObjCRuntime::GNU);
John McCall75bc7732011-07-06 02:36:30 +00001891 }
John McCall24fc0de2011-07-06 00:26:06 +00001892 getToolChain().configureObjCRuntime(objCRuntime);
1893 if (objCRuntime.HasARC)
1894 CmdArgs.push_back("-fobjc-runtime-has-arc");
1895 if (objCRuntime.HasWeak)
1896 CmdArgs.push_back("-fobjc-runtime-has-weak");
John McCall9de19782011-07-06 01:22:26 +00001897 if (objCRuntime.HasTerminate)
1898 CmdArgs.push_back("-fobjc-runtime-has-terminate");
John McCall24fc0de2011-07-06 00:26:06 +00001899
Daniel Dunbarc1dd0e92010-09-20 18:19:55 +00001900 // Compute the Objective-C ABI "version" to use. Version numbers are
1901 // slightly confusing for historical reasons:
1902 // 1 - Traditional "fragile" ABI
1903 // 2 - Non-fragile ABI, version 1
1904 // 3 - Non-fragile ABI, version 2
John McCallb5f652e2011-06-22 00:53:57 +00001905 objcABIVersion = 1;
Daniel Dunbarc1dd0e92010-09-20 18:19:55 +00001906 // If -fobjc-abi-version= is present, use that to set the version.
Daniel Dunbar12c82082010-04-28 23:25:24 +00001907 if (Arg *A = Args.getLastArg(options::OPT_fobjc_abi_version_EQ)) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001908 if (StringRef(A->getValue(Args)) == "1")
John McCallb5f652e2011-06-22 00:53:57 +00001909 objcABIVersion = 1;
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001910 else if (StringRef(A->getValue(Args)) == "2")
John McCallb5f652e2011-06-22 00:53:57 +00001911 objcABIVersion = 2;
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001912 else if (StringRef(A->getValue(Args)) == "3")
John McCallb5f652e2011-06-22 00:53:57 +00001913 objcABIVersion = 3;
Daniel Dunbar12c82082010-04-28 23:25:24 +00001914 else
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001915 D.Diag(diag::err_drv_clang_unsupported) << A->getAsString(Args);
Daniel Dunbarc1dd0e92010-09-20 18:19:55 +00001916 } else {
1917 // Otherwise, determine if we are using the non-fragile ABI.
1918 if (Args.hasFlag(options::OPT_fobjc_nonfragile_abi,
1919 options::OPT_fno_objc_nonfragile_abi,
1920 getToolChain().IsObjCNonFragileABIDefault())) {
1921 // Determine the non-fragile ABI version to use.
Daniel Dunbaraeed5fe2010-11-11 16:08:59 +00001922#ifdef DISABLE_DEFAULT_NONFRAGILEABI_TWO
1923 unsigned NonFragileABIVersion = 1;
1924#else
Daniel Dunbarc1dd0e92010-09-20 18:19:55 +00001925 unsigned NonFragileABIVersion = 2;
Daniel Dunbaraeed5fe2010-11-11 16:08:59 +00001926#endif
Daniel Dunbarc1dd0e92010-09-20 18:19:55 +00001927
1928 if (Arg *A = Args.getLastArg(
1929 options::OPT_fobjc_nonfragile_abi_version_EQ)) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001930 if (StringRef(A->getValue(Args)) == "1")
Daniel Dunbarc1dd0e92010-09-20 18:19:55 +00001931 NonFragileABIVersion = 1;
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001932 else if (StringRef(A->getValue(Args)) == "2")
Daniel Dunbarc1dd0e92010-09-20 18:19:55 +00001933 NonFragileABIVersion = 2;
1934 else
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001935 D.Diag(diag::err_drv_clang_unsupported)
Daniel Dunbarc1dd0e92010-09-20 18:19:55 +00001936 << A->getAsString(Args);
1937 }
1938
John McCallb5f652e2011-06-22 00:53:57 +00001939 objcABIVersion = 1 + NonFragileABIVersion;
Daniel Dunbarc1dd0e92010-09-20 18:19:55 +00001940 } else {
John McCallb5f652e2011-06-22 00:53:57 +00001941 objcABIVersion = 1;
Daniel Dunbarc1dd0e92010-09-20 18:19:55 +00001942 }
Daniel Dunbar12c82082010-04-28 23:25:24 +00001943 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001944
John McCallb5f652e2011-06-22 00:53:57 +00001945 if (objcABIVersion == 2 || objcABIVersion == 3) {
Fariborz Jahanian3aa19e9a2011-01-04 20:05:20 +00001946 CmdArgs.push_back("-fobjc-nonfragile-abi");
Daniel Dunbarfca18c1b42010-04-24 17:56:46 +00001947
1948 // -fobjc-dispatch-method is only relevant with the nonfragile-abi, and
1949 // legacy is the default.
1950 if (!Args.hasFlag(options::OPT_fobjc_legacy_dispatch,
1951 options::OPT_fno_objc_legacy_dispatch,
1952 getToolChain().IsObjCLegacyDispatchDefault())) {
1953 if (getToolChain().UseObjCMixedDispatch())
1954 CmdArgs.push_back("-fobjc-dispatch-method=mixed");
1955 else
1956 CmdArgs.push_back("-fobjc-dispatch-method=non-legacy");
1957 }
Daniel Dunbar2e3f2c82010-02-01 21:07:25 +00001958 }
NAKAMURA Takumi029d74b2011-02-17 08:50:50 +00001959
Ted Kremenek1d56c9e2010-12-23 21:35:43 +00001960 // -fobjc-default-synthesize-properties=0 is default.
1961 if (Args.hasFlag(options::OPT_fobjc_default_synthesize_properties,
1962 options::OPT_fno_objc_default_synthesize_properties,
1963 getToolChain().IsObjCDefaultSynthPropertiesDefault())) {
1964 CmdArgs.push_back("-fobjc-default-synthesize-properties");
1965 }
Daniel Dunbar4930e332009-11-17 08:07:36 +00001966 }
1967
John McCall24fc0de2011-07-06 00:26:06 +00001968 // Allow -fno-objc-arr to trump -fobjc-arr/-fobjc-arc.
1969 // NOTE: This logic is duplicated in ToolChains.cpp.
1970 bool ARC = isObjCAutoRefCount(Args);
1971 if (ARC) {
1972 CmdArgs.push_back("-fobjc-arc");
1973
1974 // Allow the user to enable full exceptions code emission.
1975 // We define off for Objective-CC, on for Objective-C++.
1976 if (Args.hasFlag(options::OPT_fobjc_arc_exceptions,
1977 options::OPT_fno_objc_arc_exceptions,
1978 /*default*/ types::isCXX(InputType)))
1979 CmdArgs.push_back("-fobjc-arc-exceptions");
1980 }
1981
1982 // -fobjc-infer-related-result-type is the default, except in the Objective-C
1983 // rewriter.
1984 if (IsRewriter)
1985 CmdArgs.push_back("-fno-objc-infer-related-result-type");
Eric Christopher84fbdb42011-08-19 00:30:14 +00001986
John McCall24fc0de2011-07-06 00:26:06 +00001987 // Handle -fobjc-gc and -fobjc-gc-only. They are exclusive, and -fobjc-gc-only
1988 // takes precedence.
1989 const Arg *GCArg = Args.getLastArg(options::OPT_fobjc_gc_only);
1990 if (!GCArg)
1991 GCArg = Args.getLastArg(options::OPT_fobjc_gc);
1992 if (GCArg) {
1993 if (ARC) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001994 D.Diag(diag::err_drv_objc_gc_arr)
John McCall24fc0de2011-07-06 00:26:06 +00001995 << GCArg->getAsString(Args);
1996 } else if (getToolChain().SupportsObjCGC()) {
1997 GCArg->render(Args, CmdArgs);
1998 } else {
1999 // FIXME: We should move this to a hard error.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002000 D.Diag(diag::warn_drv_objc_gc_unsupported)
John McCall24fc0de2011-07-06 00:26:06 +00002001 << GCArg->getAsString(Args);
2002 }
2003 }
2004
John McCallb5f652e2011-06-22 00:53:57 +00002005 // Add exception args.
2006 addExceptionArgs(Args, InputType, getToolChain().getTriple(),
2007 KernelOrKext, IsRewriter, objcABIVersion, CmdArgs);
2008
2009 if (getToolChain().UseSjLjExceptions())
2010 CmdArgs.push_back("-fsjlj-exceptions");
2011
2012 // C++ "sane" operator new.
Daniel Dunbar2e3f2c82010-02-01 21:07:25 +00002013 if (!Args.hasFlag(options::OPT_fassume_sane_operator_new,
2014 options::OPT_fno_assume_sane_operator_new))
2015 CmdArgs.push_back("-fno-assume-sane-operator-new");
2016
Daniel Dunbar34d7a992010-04-27 15:34:57 +00002017 // -fconstant-cfstrings is default, and may be subject to argument translation
2018 // on Darwin.
2019 if (!Args.hasFlag(options::OPT_fconstant_cfstrings,
2020 options::OPT_fno_constant_cfstrings) ||
2021 !Args.hasFlag(options::OPT_mconstant_cfstrings,
2022 options::OPT_mno_constant_cfstrings))
2023 CmdArgs.push_back("-fno-constant-cfstrings");
2024
John Thompsoned4e2952009-11-05 20:14:16 +00002025 // -fshort-wchar default varies depending on platform; only
2026 // pass if specified.
Daniel Dunbar2cb4e7a2010-04-27 15:35:03 +00002027 if (Arg *A = Args.getLastArg(options::OPT_fshort_wchar))
2028 A->render(Args, CmdArgs);
John Thompsoned4e2952009-11-05 20:14:16 +00002029
Daniel Dunbar1a8a2e82009-10-29 02:39:57 +00002030 // -fno-pascal-strings is default, only pass non-default. If the tool chain
2031 // happened to translate to -mpascal-strings, we want to back translate here.
Daniel Dunbard4510f22009-04-07 23:51:44 +00002032 //
2033 // FIXME: This is gross; that translation should be pulled from the
2034 // tool chain.
Daniel Dunbard067f7f2009-04-08 23:54:23 +00002035 if (Args.hasFlag(options::OPT_fpascal_strings,
Daniel Dunbard4510f22009-04-07 23:51:44 +00002036 options::OPT_fno_pascal_strings,
2037 false) ||
2038 Args.hasFlag(options::OPT_mpascal_strings,
2039 options::OPT_mno_pascal_strings,
2040 false))
Daniel Dunbard18049a2009-04-07 21:16:11 +00002041 CmdArgs.push_back("-fpascal-strings");
NAKAMURA Takumi029d74b2011-02-17 08:50:50 +00002042
Fariborz Jahaniana4cfff82011-01-07 01:05:02 +00002043 if (Args.hasArg(options::OPT_mkernel) ||
2044 Args.hasArg(options::OPT_fapple_kext)) {
2045 if (!Args.hasArg(options::OPT_fcommon))
2046 CmdArgs.push_back("-fno-common");
2047 }
Daniel Dunbard18049a2009-04-07 21:16:11 +00002048 // -fcommon is default, only pass non-default.
Fariborz Jahaniana4cfff82011-01-07 01:05:02 +00002049 else if (!Args.hasFlag(options::OPT_fcommon, options::OPT_fno_common))
Daniel Dunbard18049a2009-04-07 21:16:11 +00002050 CmdArgs.push_back("-fno-common");
2051
Daniel Dunbar2edd9232009-04-15 02:37:43 +00002052 // -fsigned-bitfields is default, and clang doesn't yet support
Daniel Dunbar6358d682010-10-15 22:30:42 +00002053 // -funsigned-bitfields.
Mike Stump11289f42009-09-09 15:08:12 +00002054 if (!Args.hasFlag(options::OPT_fsigned_bitfields,
Daniel Dunbar2edd9232009-04-15 02:37:43 +00002055 options::OPT_funsigned_bitfields))
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002056 D.Diag(diag::warn_drv_clang_unsupported)
Daniel Dunbar2edd9232009-04-15 02:37:43 +00002057 << Args.getLastArg(options::OPT_funsigned_bitfields)->getAsString(Args);
2058
Daniel Dunbar6358d682010-10-15 22:30:42 +00002059 // -fsigned-bitfields is default, and clang doesn't support -fno-for-scope.
2060 if (!Args.hasFlag(options::OPT_ffor_scope,
2061 options::OPT_fno_for_scope))
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002062 D.Diag(diag::err_drv_clang_unsupported)
Daniel Dunbar6358d682010-10-15 22:30:42 +00002063 << Args.getLastArg(options::OPT_fno_for_scope)->getAsString(Args);
2064
Jeffrey Yasskin460aa542010-06-08 04:56:20 +00002065 // -fcaret-diagnostics is default.
2066 if (!Args.hasFlag(options::OPT_fcaret_diagnostics,
2067 options::OPT_fno_caret_diagnostics, true))
2068 CmdArgs.push_back("-fno-caret-diagnostics");
2069
Daniel Dunbar8281bde2009-04-19 21:09:34 +00002070 // -fdiagnostics-fixit-info is default, only pass non-default.
Mike Stump11289f42009-09-09 15:08:12 +00002071 if (!Args.hasFlag(options::OPT_fdiagnostics_fixit_info,
Daniel Dunbar8281bde2009-04-19 21:09:34 +00002072 options::OPT_fno_diagnostics_fixit_info))
2073 CmdArgs.push_back("-fno-diagnostics-fixit-info");
Eric Christopher84fbdb42011-08-19 00:30:14 +00002074
Douglas Gregor46ce91a2011-04-15 22:04:17 +00002075 // Enable -fdiagnostics-show-name by default.
2076 if (Args.hasFlag(options::OPT_fdiagnostics_show_name,
2077 options::OPT_fno_diagnostics_show_name, false))
2078 CmdArgs.push_back("-fdiagnostics-show-name");
Daniel Dunbar8281bde2009-04-19 21:09:34 +00002079
Daniel Dunbar092f0cc2009-04-16 06:32:38 +00002080 // Enable -fdiagnostics-show-option by default.
Mike Stump11289f42009-09-09 15:08:12 +00002081 if (Args.hasFlag(options::OPT_fdiagnostics_show_option,
Daniel Dunbar092f0cc2009-04-16 06:32:38 +00002082 options::OPT_fno_diagnostics_show_option))
2083 CmdArgs.push_back("-fdiagnostics-show-option");
Daniel Dunbar5ec95022009-11-04 06:24:57 +00002084
Chris Lattnerbf6fac82010-05-04 21:55:25 +00002085 if (const Arg *A =
2086 Args.getLastArg(options::OPT_fdiagnostics_show_category_EQ)) {
2087 CmdArgs.push_back("-fdiagnostics-show-category");
2088 CmdArgs.push_back(A->getValue(Args));
2089 }
Daniel Dunbarfe6c97b2010-08-24 16:47:49 +00002090
Douglas Gregor643c9222011-05-21 17:07:29 +00002091 if (const Arg *A =
2092 Args.getLastArg(options::OPT_fdiagnostics_format_EQ)) {
2093 CmdArgs.push_back("-fdiagnostics-format");
2094 CmdArgs.push_back(A->getValue(Args));
2095 }
2096
Chandler Carruthb6766f02011-03-27 01:50:55 +00002097 if (Arg *A = Args.getLastArg(
2098 options::OPT_fdiagnostics_show_note_include_stack,
2099 options::OPT_fno_diagnostics_show_note_include_stack)) {
2100 if (A->getOption().matches(
2101 options::OPT_fdiagnostics_show_note_include_stack))
2102 CmdArgs.push_back("-fdiagnostics-show-note-include-stack");
2103 else
2104 CmdArgs.push_back("-fno-diagnostics-show-note-include-stack");
2105 }
2106
Daniel Dunbar5ec95022009-11-04 06:24:57 +00002107 // Color diagnostics are the default, unless the terminal doesn't support
2108 // them.
2109 if (Args.hasFlag(options::OPT_fcolor_diagnostics,
Argyrios Kyrtzidis4f920162010-09-23 12:56:06 +00002110 options::OPT_fno_color_diagnostics,
2111 llvm::sys::Process::StandardErrHasColors()))
Daniel Dunbar5ec95022009-11-04 06:24:57 +00002112 CmdArgs.push_back("-fcolor-diagnostics");
2113
Daniel Dunbardb097022009-06-08 21:13:54 +00002114 if (!Args.hasFlag(options::OPT_fshow_source_location,
2115 options::OPT_fno_show_source_location))
2116 CmdArgs.push_back("-fno-show-source-location");
Daniel Dunbar092f0cc2009-04-16 06:32:38 +00002117
Douglas Gregor643c9222011-05-21 17:07:29 +00002118 if (!Args.hasFlag(options::OPT_fshow_column,
2119 options::OPT_fno_show_column,
2120 true))
2121 CmdArgs.push_back("-fno-show-column");
2122
Douglas Gregor8ed0c0b2010-07-09 17:35:33 +00002123 if (!Args.hasFlag(options::OPT_fspell_checking,
2124 options::OPT_fno_spell_checking))
2125 CmdArgs.push_back("-fno-spell-checking");
Daniel Dunbarfe6c97b2010-08-24 16:47:49 +00002126
Daniel Dunbar473f8a62010-10-18 22:49:46 +00002127
Daniel Dunbar3ada2b72010-11-02 19:42:04 +00002128 // Silently ignore -fasm-blocks for now.
2129 (void) Args.hasFlag(options::OPT_fasm_blocks, options::OPT_fno_asm_blocks,
2130 false);
Daniel Dunbar473f8a62010-10-18 22:49:46 +00002131
Jeffrey Yasskin2b99c6f2010-06-11 05:57:47 +00002132 if (Arg *A = Args.getLastArg(options::OPT_fshow_overloads_EQ))
2133 A->render(Args, CmdArgs);
2134
Daniel Dunbarf5e9b1f2009-04-19 21:20:32 +00002135 // -fdollars-in-identifiers default varies depending on platform and
2136 // language; only pass if specified.
Mike Stump11289f42009-09-09 15:08:12 +00002137 if (Arg *A = Args.getLastArg(options::OPT_fdollars_in_identifiers,
Daniel Dunbarf5e9b1f2009-04-19 21:20:32 +00002138 options::OPT_fno_dollars_in_identifiers)) {
2139 if (A->getOption().matches(options::OPT_fdollars_in_identifiers))
Daniel Dunbar15cef0e2009-12-16 20:10:18 +00002140 CmdArgs.push_back("-fdollars-in-identifiers");
Daniel Dunbarf5e9b1f2009-04-19 21:20:32 +00002141 else
Daniel Dunbar15cef0e2009-12-16 20:10:18 +00002142 CmdArgs.push_back("-fno-dollars-in-identifiers");
Daniel Dunbarf5e9b1f2009-04-19 21:20:32 +00002143 }
2144
Daniel Dunbaradeeb052009-05-22 19:02:20 +00002145 // -funit-at-a-time is default, and we don't support -fno-unit-at-a-time for
2146 // practical purposes.
Mike Stump11289f42009-09-09 15:08:12 +00002147 if (Arg *A = Args.getLastArg(options::OPT_funit_at_a_time,
Daniel Dunbaradeeb052009-05-22 19:02:20 +00002148 options::OPT_fno_unit_at_a_time)) {
2149 if (A->getOption().matches(options::OPT_fno_unit_at_a_time))
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002150 D.Diag(diag::warn_drv_clang_unsupported) << A->getAsString(Args);
Daniel Dunbaradeeb052009-05-22 19:02:20 +00002151 }
Eli Friedmanbb0d9a52009-07-14 21:58:17 +00002152
Daniel Dunbar2ffe0292009-09-10 03:37:02 +00002153 // Default to -fno-builtin-str{cat,cpy} on Darwin for ARM.
Daniel Dunbar4fa08112009-09-10 04:57:27 +00002154 //
Daniel Dunbar6c536aa2009-12-11 23:00:49 +00002155 // FIXME: This is disabled until clang -cc1 supports -fno-builtin-foo. PR4941.
Daniel Dunbar4fa08112009-09-10 04:57:27 +00002156#if 0
Daniel Dunbar2ffe0292009-09-10 03:37:02 +00002157 if (getToolChain().getTriple().getOS() == llvm::Triple::Darwin &&
2158 (getToolChain().getTriple().getArch() == llvm::Triple::arm ||
2159 getToolChain().getTriple().getArch() == llvm::Triple::thumb)) {
2160 if (!Args.hasArg(options::OPT_fbuiltin_strcat))
2161 CmdArgs.push_back("-fno-builtin-strcat");
2162 if (!Args.hasArg(options::OPT_fbuiltin_strcpy))
2163 CmdArgs.push_back("-fno-builtin-strcpy");
2164 }
Daniel Dunbar4fa08112009-09-10 04:57:27 +00002165#endif
Daniel Dunbar2ffe0292009-09-10 03:37:02 +00002166
Daniel Dunbar8c3d7352011-03-18 21:23:40 +00002167 // Only allow -traditional or -traditional-cpp outside in preprocessing modes.
Mike Stump11289f42009-09-09 15:08:12 +00002168 if (Arg *A = Args.getLastArg(options::OPT_traditional,
Daniel Dunbar8c3d7352011-03-18 21:23:40 +00002169 options::OPT_traditional_cpp)) {
2170 if (isa<PreprocessJobAction>(JA))
2171 CmdArgs.push_back("-traditional-cpp");
Eric Christopher84fbdb42011-08-19 00:30:14 +00002172 else
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002173 D.Diag(diag::err_drv_clang_unsupported) << A->getAsString(Args);
Daniel Dunbar8c3d7352011-03-18 21:23:40 +00002174 }
Eli Friedmanbb0d9a52009-07-14 21:58:17 +00002175
Daniel Dunbara2aedc62009-03-18 10:01:51 +00002176 Args.AddLastArg(CmdArgs, options::OPT_dM);
Chris Lattnercac63f32009-04-12 01:56:53 +00002177 Args.AddLastArg(CmdArgs, options::OPT_dD);
Daniel Dunbara2aedc62009-03-18 10:01:51 +00002178
Daniel Dunbar76fa8402010-04-15 06:09:03 +00002179 // Forward -Xclang arguments to -cc1, and -mllvm arguments to the LLVM option
2180 // parser.
Daniel Dunbara2aedc62009-03-18 10:01:51 +00002181 Args.AddAllArgValues(CmdArgs, options::OPT_Xclang);
Daniel Dunbar76fa8402010-04-15 06:09:03 +00002182 for (arg_iterator it = Args.filtered_begin(options::OPT_mllvm),
2183 ie = Args.filtered_end(); it != ie; ++it) {
Daniel Dunbara442fd52010-06-11 22:00:13 +00002184 (*it)->claim();
Daniel Dunbar88534f42010-04-17 06:10:00 +00002185
Daniel Dunbar76fa8402010-04-15 06:09:03 +00002186 // We translate this by hand to the -cc1 argument, since nightly test uses
2187 // it and developers have been trained to spell it with -mllvm.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002188 if (StringRef((*it)->getValue(Args, 0)) == "-disable-llvm-optzns")
Daniel Dunbar76fa8402010-04-15 06:09:03 +00002189 CmdArgs.push_back("-disable-llvm-optzns");
2190 else
Daniel Dunbara442fd52010-06-11 22:00:13 +00002191 (*it)->render(Args, CmdArgs);
Daniel Dunbar76fa8402010-04-15 06:09:03 +00002192 }
Daniel Dunbara2aedc62009-03-18 10:01:51 +00002193
Daniel Dunbard67a3222009-03-30 06:36:42 +00002194 if (Output.getType() == types::TY_Dependencies) {
2195 // Handled with other dependency code.
Daniel Dunbar5cdf3e02009-03-19 07:29:38 +00002196 } else if (Output.isFilename()) {
Daniel Dunbara3246a02009-03-18 08:07:30 +00002197 CmdArgs.push_back("-o");
Daniel Dunbar5cdf3e02009-03-19 07:29:38 +00002198 CmdArgs.push_back(Output.getFilename());
2199 } else {
2200 assert(Output.isNothing() && "Invalid output.");
Daniel Dunbara3246a02009-03-18 08:07:30 +00002201 }
2202
Daniel Dunbara2aedc62009-03-18 10:01:51 +00002203 for (InputInfoList::const_iterator
2204 it = Inputs.begin(), ie = Inputs.end(); it != ie; ++it) {
2205 const InputInfo &II = *it;
2206 CmdArgs.push_back("-x");
2207 CmdArgs.push_back(types::getTypeName(II.getType()));
Daniel Dunbarb440f562010-08-02 02:38:21 +00002208 if (II.isFilename())
Daniel Dunbar5cdf3e02009-03-19 07:29:38 +00002209 CmdArgs.push_back(II.getFilename());
Daniel Dunbara2aedc62009-03-18 10:01:51 +00002210 else
Daniel Dunbar5cdf3e02009-03-19 07:29:38 +00002211 II.getInputArg().renderAsInput(Args, CmdArgs);
Daniel Dunbara2aedc62009-03-18 10:01:51 +00002212 }
Daniel Dunbard067f7f2009-04-08 23:54:23 +00002213
Chris Lattnere9d7d782009-11-03 19:50:27 +00002214 Args.AddAllArgs(CmdArgs, options::OPT_undef);
2215
Daniel Dunbarb31b76f2010-07-18 21:16:15 +00002216 const char *Exec = getToolChain().getDriver().getClangProgramPath();
Daniel Dunbar24c7f5e2009-12-18 02:43:17 +00002217
2218 // Optionally embed the -cc1 level arguments into the debug info, for build
2219 // analysis.
2220 if (getToolChain().UseDwarfDebugFlags()) {
Daniel Dunbar7f3d9502010-06-04 18:47:06 +00002221 ArgStringList OriginalArgs;
2222 for (ArgList::const_iterator it = Args.begin(),
2223 ie = Args.end(); it != ie; ++it)
2224 (*it)->render(Args, OriginalArgs);
Daniel Dunbarfe6c97b2010-08-24 16:47:49 +00002225
Daniel Dunbar24c7f5e2009-12-18 02:43:17 +00002226 llvm::SmallString<256> Flags;
2227 Flags += Exec;
Daniel Dunbar7f3d9502010-06-04 18:47:06 +00002228 for (unsigned i = 0, e = OriginalArgs.size(); i != e; ++i) {
Daniel Dunbar24c7f5e2009-12-18 02:43:17 +00002229 Flags += " ";
Daniel Dunbar7f3d9502010-06-04 18:47:06 +00002230 Flags += OriginalArgs[i];
Daniel Dunbar24c7f5e2009-12-18 02:43:17 +00002231 }
2232 CmdArgs.push_back("-dwarf-debug-flags");
2233 CmdArgs.push_back(Args.MakeArgString(Flags.str()));
2234 }
2235
Daniel Dunbar7fbaf532010-08-02 02:38:28 +00002236 C.addCommand(new Command(JA, *this, Exec, CmdArgs));
Daniel Dunbar17731772009-03-23 19:03:36 +00002237
Roman Divacky178e01602011-02-10 16:52:03 +00002238 if (Arg *A = Args.getLastArg(options::OPT_pg))
2239 if (Args.hasArg(options::OPT_fomit_frame_pointer))
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002240 D.Diag(diag::err_drv_argument_not_allowed_with)
Roman Divacky178e01602011-02-10 16:52:03 +00002241 << "-fomit-frame-pointer" << A->getAsString(Args);
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00002242
Daniel Dunbarc2a71892009-04-03 20:51:31 +00002243 // Claim some arguments which clang supports automatically.
2244
Daniel Dunbar3e0cac62010-04-15 06:18:42 +00002245 // -fpch-preprocess is used with gcc to add a special marker in the output to
2246 // include the PCH file. Clang's PTH solution is completely transparent, so we
2247 // do not need to deal with it at all.
Daniel Dunbarc2a71892009-04-03 20:51:31 +00002248 Args.ClaimAllArgs(options::OPT_fpch_preprocess);
Daniel Dunbard067f7f2009-04-08 23:54:23 +00002249
Daniel Dunbar17731772009-03-23 19:03:36 +00002250 // Claim some arguments which clang doesn't support, but we don't
2251 // care to warn the user about.
Daniel Dunbar44b36ee2009-11-25 11:53:23 +00002252 Args.ClaimAllArgs(options::OPT_clang_ignored_f_Group);
2253 Args.ClaimAllArgs(options::OPT_clang_ignored_m_Group);
Rafael Espindola22f603032011-02-28 23:29:45 +00002254
Rafael Espindolad95a8122011-03-01 05:25:27 +00002255 // Disable warnings for clang -E -use-gold-plugin -emit-llvm foo.c
Rafael Espindola22f603032011-02-28 23:29:45 +00002256 Args.ClaimAllArgs(options::OPT_use_gold_plugin);
Rafael Espindolad95a8122011-03-01 05:25:27 +00002257 Args.ClaimAllArgs(options::OPT_emit_llvm);
Daniel Dunbar1a093d22009-03-18 06:00:36 +00002258}
2259
Daniel Dunbar4f5e79c2010-05-20 21:30:13 +00002260void ClangAs::ConstructJob(Compilation &C, const JobAction &JA,
Daniel Dunbar4f5e79c2010-05-20 21:30:13 +00002261 const InputInfo &Output,
2262 const InputInfoList &Inputs,
2263 const ArgList &Args,
2264 const char *LinkingOutput) const {
Daniel Dunbar4f5e79c2010-05-20 21:30:13 +00002265 ArgStringList CmdArgs;
2266
2267 assert(Inputs.size() == 1 && "Unexpected number of inputs.");
2268 const InputInfo &Input = Inputs[0];
2269
Rafael Espindolacfaadda2010-11-17 22:13:25 +00002270 // Don't warn about "clang -w -c foo.s"
2271 Args.ClaimAllArgs(options::OPT_w);
Rafael Espindolad95a8122011-03-01 05:25:27 +00002272 // and "clang -emit-llvm -c foo.s"
2273 Args.ClaimAllArgs(options::OPT_emit_llvm);
2274 // and "clang -use-gold-plugin -c foo.s"
2275 Args.ClaimAllArgs(options::OPT_use_gold_plugin);
Rafael Espindolacfaadda2010-11-17 22:13:25 +00002276
Daniel Dunbar4f5e79c2010-05-20 21:30:13 +00002277 // Invoke ourselves in -cc1as mode.
2278 //
2279 // FIXME: Implement custom jobs for internal actions.
2280 CmdArgs.push_back("-cc1as");
2281
2282 // Add the "effective" target triple.
2283 CmdArgs.push_back("-triple");
Chad Rosierd3a0f952011-09-20 20:44:06 +00002284 std::string TripleStr =
2285 getToolChain().ComputeEffectiveClangTriple(Args, Input.getType());
Daniel Dunbar4f5e79c2010-05-20 21:30:13 +00002286 CmdArgs.push_back(Args.MakeArgString(TripleStr));
2287
2288 // Set the output mode, we currently only expect to be used as a real
2289 // assembler.
2290 CmdArgs.push_back("-filetype");
2291 CmdArgs.push_back("obj");
2292
Joerg Sonnenbergeref317a22011-05-06 14:35:16 +00002293 if (UseRelaxAll(C, Args))
Daniel Dunbar99ca8b72010-05-28 16:43:21 +00002294 CmdArgs.push_back("-relax-all");
Daniel Dunbar06e2cc32010-05-27 06:18:05 +00002295
Daniel Dunbar1d733e22011-03-17 17:37:29 +00002296 // Ignore explicit -force_cpusubtype_ALL option.
2297 (void) Args.hasArg(options::OPT_force__cpusubtype__ALL);
Daniel Dunbar4f5e79c2010-05-20 21:30:13 +00002298
2299 // FIXME: Add -g support, once we have it.
2300
2301 // FIXME: Add -static support, once we have it.
2302
2303 Args.AddAllArgValues(CmdArgs, options::OPT_Wa_COMMA,
2304 options::OPT_Xassembler);
Daniel Dunbar252e8f92011-04-29 17:53:18 +00002305 Args.AddAllArgs(CmdArgs, options::OPT_mllvm);
Daniel Dunbar4f5e79c2010-05-20 21:30:13 +00002306
2307 assert(Output.isFilename() && "Unexpected lipo output.");
2308 CmdArgs.push_back("-o");
2309 CmdArgs.push_back(Output.getFilename());
2310
Daniel Dunbarb440f562010-08-02 02:38:21 +00002311 assert(Input.isFilename() && "Invalid input.");
2312 CmdArgs.push_back(Input.getFilename());
Daniel Dunbar4f5e79c2010-05-20 21:30:13 +00002313
Daniel Dunbarb31b76f2010-07-18 21:16:15 +00002314 const char *Exec = getToolChain().getDriver().getClangProgramPath();
Daniel Dunbar7fbaf532010-08-02 02:38:28 +00002315 C.addCommand(new Command(JA, *this, Exec, CmdArgs));
Daniel Dunbar4f5e79c2010-05-20 21:30:13 +00002316}
2317
Daniel Dunbara3246a02009-03-18 08:07:30 +00002318void gcc::Common::ConstructJob(Compilation &C, const JobAction &JA,
Daniel Dunbara3246a02009-03-18 08:07:30 +00002319 const InputInfo &Output,
2320 const InputInfoList &Inputs,
Daniel Dunbara2aedc62009-03-18 10:01:51 +00002321 const ArgList &Args,
Daniel Dunbara3246a02009-03-18 08:07:30 +00002322 const char *LinkingOutput) const {
Daniel Dunbar083edf72009-12-21 18:54:17 +00002323 const Driver &D = getToolChain().getDriver();
Daniel Dunbara3246a02009-03-18 08:07:30 +00002324 ArgStringList CmdArgs;
Daniel Dunbar1a093d22009-03-18 06:00:36 +00002325
Daniel Dunbard067f7f2009-04-08 23:54:23 +00002326 for (ArgList::const_iterator
Daniel Dunbara2aedc62009-03-18 10:01:51 +00002327 it = Args.begin(), ie = Args.end(); it != ie; ++it) {
Daniel Dunbara3246a02009-03-18 08:07:30 +00002328 Arg *A = *it;
Daniel Dunbar2da02722009-03-19 07:55:12 +00002329 if (A->getOption().hasForwardToGCC()) {
Daniel Dunbar939c1212010-08-03 16:14:14 +00002330 // Don't forward any -g arguments to assembly steps.
2331 if (isa<AssembleJobAction>(JA) &&
2332 A->getOption().matches(options::OPT_g_Group))
2333 continue;
2334
Daniel Dunbar2da02722009-03-19 07:55:12 +00002335 // It is unfortunate that we have to claim here, as this means
2336 // we will basically never report anything interesting for
Daniel Dunbar5716d872009-05-02 21:41:52 +00002337 // platforms using a generic gcc, even if we are just using gcc
2338 // to get to the assembler.
Daniel Dunbar2da02722009-03-19 07:55:12 +00002339 A->claim();
Daniel Dunbara2aedc62009-03-18 10:01:51 +00002340 A->render(Args, CmdArgs);
Daniel Dunbar2da02722009-03-19 07:55:12 +00002341 }
Daniel Dunbara3246a02009-03-18 08:07:30 +00002342 }
Daniel Dunbard067f7f2009-04-08 23:54:23 +00002343
Daniel Dunbar4e295052010-01-25 22:35:08 +00002344 RenderExtraToolArgs(JA, CmdArgs);
Daniel Dunbara3246a02009-03-18 08:07:30 +00002345
2346 // If using a driver driver, force the arch.
Daniel Dunbar5bbebfe2009-05-22 02:21:04 +00002347 const std::string &Arch = getToolChain().getArchName();
Daniel Dunbarf4894fe2009-12-23 00:46:38 +00002348 if (getToolChain().getTriple().getOS() == llvm::Triple::Darwin) {
Daniel Dunbara3246a02009-03-18 08:07:30 +00002349 CmdArgs.push_back("-arch");
Daniel Dunbar0a05d932009-04-01 20:33:11 +00002350
2351 // FIXME: Remove these special cases.
Daniel Dunbar5bbebfe2009-05-22 02:21:04 +00002352 if (Arch == "powerpc")
2353 CmdArgs.push_back("ppc");
2354 else if (Arch == "powerpc64")
2355 CmdArgs.push_back("ppc64");
2356 else
Daniel Dunbar1c28f1e2009-09-09 22:32:48 +00002357 CmdArgs.push_back(Args.MakeArgString(Arch));
Daniel Dunbara3246a02009-03-18 08:07:30 +00002358 }
2359
Daniel Dunbar5716d872009-05-02 21:41:52 +00002360 // Try to force gcc to match the tool chain we want, if we recognize
2361 // the arch.
Daniel Dunbar5bbebfe2009-05-22 02:21:04 +00002362 //
2363 // FIXME: The triple class should directly provide the information we want
2364 // here.
2365 if (Arch == "i386" || Arch == "powerpc")
Daniel Dunbar5716d872009-05-02 21:41:52 +00002366 CmdArgs.push_back("-m32");
Daniel Dunbar5bbebfe2009-05-22 02:21:04 +00002367 else if (Arch == "x86_64" || Arch == "powerpc64")
Daniel Dunbar5716d872009-05-02 21:41:52 +00002368 CmdArgs.push_back("-m64");
2369
Daniel Dunbarb440f562010-08-02 02:38:21 +00002370 if (Output.isFilename()) {
Daniel Dunbara3246a02009-03-18 08:07:30 +00002371 CmdArgs.push_back("-o");
Daniel Dunbar5cdf3e02009-03-19 07:29:38 +00002372 CmdArgs.push_back(Output.getFilename());
2373 } else {
2374 assert(Output.isNothing() && "Unexpected output");
Daniel Dunbara3246a02009-03-18 08:07:30 +00002375 CmdArgs.push_back("-fsyntax-only");
Daniel Dunbar5cdf3e02009-03-19 07:29:38 +00002376 }
Daniel Dunbara3246a02009-03-18 08:07:30 +00002377
2378
2379 // Only pass -x if gcc will understand it; otherwise hope gcc
2380 // understands the suffix correctly. The main use case this would go
2381 // wrong in is for linker inputs if they happened to have an odd
2382 // suffix; really the only way to get this to happen is a command
2383 // like '-x foobar a.c' which will treat a.c like a linker input.
2384 //
2385 // FIXME: For the linker case specifically, can we safely convert
2386 // inputs into '-Wl,' options?
2387 for (InputInfoList::const_iterator
2388 it = Inputs.begin(), ie = Inputs.end(); it != ie; ++it) {
2389 const InputInfo &II = *it;
Daniel Dunbare3e263f2009-05-02 20:14:53 +00002390
Daniel Dunbar6cdf83c2009-09-01 16:57:46 +00002391 // Don't try to pass LLVM or AST inputs to a generic gcc.
Daniel Dunbar24e52992010-06-07 23:28:45 +00002392 if (II.getType() == types::TY_LLVM_IR || II.getType() == types::TY_LTO_IR ||
2393 II.getType() == types::TY_LLVM_BC || II.getType() == types::TY_LTO_BC)
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002394 D.Diag(diag::err_drv_no_linker_llvm_support)
Daniel Dunbar1c28f1e2009-09-09 22:32:48 +00002395 << getToolChain().getTripleString();
Daniel Dunbar6cdf83c2009-09-01 16:57:46 +00002396 else if (II.getType() == types::TY_AST)
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002397 D.Diag(diag::err_drv_no_ast_support)
Daniel Dunbar1c28f1e2009-09-09 22:32:48 +00002398 << getToolChain().getTripleString();
Daniel Dunbare3e263f2009-05-02 20:14:53 +00002399
Daniel Dunbara3246a02009-03-18 08:07:30 +00002400 if (types::canTypeBeUserSpecified(II.getType())) {
2401 CmdArgs.push_back("-x");
2402 CmdArgs.push_back(types::getTypeName(II.getType()));
2403 }
2404
Daniel Dunbarb440f562010-08-02 02:38:21 +00002405 if (II.isFilename())
Daniel Dunbar5cdf3e02009-03-19 07:29:38 +00002406 CmdArgs.push_back(II.getFilename());
Daniel Dunbarf2476752010-09-25 18:10:05 +00002407 else {
2408 const Arg &A = II.getInputArg();
2409
2410 // Reverse translate some rewritten options.
2411 if (A.getOption().matches(options::OPT_Z_reserved_lib_stdcxx)) {
2412 CmdArgs.push_back("-lstdc++");
2413 continue;
2414 }
2415
Daniel Dunbar5cdf3e02009-03-19 07:29:38 +00002416 // Don't render as input, we need gcc to do the translations.
Daniel Dunbarf2476752010-09-25 18:10:05 +00002417 A.render(Args, CmdArgs);
2418 }
Daniel Dunbara3246a02009-03-18 08:07:30 +00002419 }
2420
Dylan Noblesmith70e73a32011-04-09 13:31:59 +00002421 const std::string customGCCName = D.getCCCGenericGCCName();
2422 const char *GCCName;
2423 if (!customGCCName.empty())
2424 GCCName = customGCCName.c_str();
2425 else if (D.CCCIsCXX) {
2426#ifdef IS_CYGWIN15
2427 // FIXME: Detect the version of Cygwin at runtime?
2428 GCCName = "g++-4";
2429#else
2430 GCCName = "g++";
2431#endif
2432 } else
2433 GCCName = "gcc";
2434
Daniel Dunbard067f7f2009-04-08 23:54:23 +00002435 const char *Exec =
Daniel Dunbar9c3ed5f2010-07-14 18:46:23 +00002436 Args.MakeArgString(getToolChain().GetProgramPath(GCCName));
Daniel Dunbar7fbaf532010-08-02 02:38:28 +00002437 C.addCommand(new Command(JA, *this, Exec, CmdArgs));
Daniel Dunbar1a093d22009-03-18 06:00:36 +00002438}
2439
Daniel Dunbar4e295052010-01-25 22:35:08 +00002440void gcc::Preprocess::RenderExtraToolArgs(const JobAction &JA,
2441 ArgStringList &CmdArgs) const {
Daniel Dunbara3246a02009-03-18 08:07:30 +00002442 CmdArgs.push_back("-E");
Daniel Dunbar1a093d22009-03-18 06:00:36 +00002443}
2444
Daniel Dunbar4e295052010-01-25 22:35:08 +00002445void gcc::Precompile::RenderExtraToolArgs(const JobAction &JA,
2446 ArgStringList &CmdArgs) const {
Daniel Dunbara3246a02009-03-18 08:07:30 +00002447 // The type is good enough.
Daniel Dunbar1a093d22009-03-18 06:00:36 +00002448}
2449
Daniel Dunbar4e295052010-01-25 22:35:08 +00002450void gcc::Compile::RenderExtraToolArgs(const JobAction &JA,
2451 ArgStringList &CmdArgs) const {
Daniel Dunbaraeea8ac2010-02-11 03:16:21 +00002452 const Driver &D = getToolChain().getDriver();
2453
Daniel Dunbar4e295052010-01-25 22:35:08 +00002454 // If -flto, etc. are present then make sure not to force assembly output.
Daniel Dunbar24e52992010-06-07 23:28:45 +00002455 if (JA.getType() == types::TY_LLVM_IR || JA.getType() == types::TY_LTO_IR ||
2456 JA.getType() == types::TY_LLVM_BC || JA.getType() == types::TY_LTO_BC)
Daniel Dunbar4e295052010-01-25 22:35:08 +00002457 CmdArgs.push_back("-c");
Daniel Dunbaraeea8ac2010-02-11 03:16:21 +00002458 else {
2459 if (JA.getType() != types::TY_PP_Asm)
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002460 D.Diag(diag::err_drv_invalid_gcc_output_type)
Daniel Dunbaraeea8ac2010-02-11 03:16:21 +00002461 << getTypeName(JA.getType());
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00002462
Daniel Dunbar4e295052010-01-25 22:35:08 +00002463 CmdArgs.push_back("-S");
Daniel Dunbaraeea8ac2010-02-11 03:16:21 +00002464 }
Daniel Dunbar1a093d22009-03-18 06:00:36 +00002465}
2466
Daniel Dunbar4e295052010-01-25 22:35:08 +00002467void gcc::Assemble::RenderExtraToolArgs(const JobAction &JA,
2468 ArgStringList &CmdArgs) const {
Daniel Dunbara3246a02009-03-18 08:07:30 +00002469 CmdArgs.push_back("-c");
Daniel Dunbar1a093d22009-03-18 06:00:36 +00002470}
Daniel Dunbara3246a02009-03-18 08:07:30 +00002471
Daniel Dunbar4e295052010-01-25 22:35:08 +00002472void gcc::Link::RenderExtraToolArgs(const JobAction &JA,
2473 ArgStringList &CmdArgs) const {
Daniel Dunbara3246a02009-03-18 08:07:30 +00002474 // The types are (hopefully) good enough.
2475}
2476
Daniel Dunbare6adeee2009-03-29 17:08:39 +00002477const char *darwin::CC1::getCC1Name(types::ID Type) const {
2478 switch (Type) {
2479 default:
2480 assert(0 && "Unexpected type for Darwin CC1 tool.");
2481 case types::TY_Asm:
2482 case types::TY_C: case types::TY_CHeader:
2483 case types::TY_PP_C: case types::TY_PP_CHeader:
2484 return "cc1";
2485 case types::TY_ObjC: case types::TY_ObjCHeader:
Nico Webered8080c2011-08-13 23:13:37 +00002486 case types::TY_PP_ObjC: case types::TY_PP_ObjC_Alias:
2487 case types::TY_PP_ObjCHeader:
Daniel Dunbare6adeee2009-03-29 17:08:39 +00002488 return "cc1obj";
2489 case types::TY_CXX: case types::TY_CXXHeader:
2490 case types::TY_PP_CXX: case types::TY_PP_CXXHeader:
2491 return "cc1plus";
2492 case types::TY_ObjCXX: case types::TY_ObjCXXHeader:
Nico Webered8080c2011-08-13 23:13:37 +00002493 case types::TY_PP_ObjCXX: case types::TY_PP_ObjCXX_Alias:
2494 case types::TY_PP_ObjCXXHeader:
Daniel Dunbare6adeee2009-03-29 17:08:39 +00002495 return "cc1objplus";
2496 }
2497}
2498
Daniel Dunbard067f7f2009-04-08 23:54:23 +00002499const char *darwin::CC1::getBaseInputName(const ArgList &Args,
Daniel Dunbar52e96cc2009-03-30 00:34:04 +00002500 const InputInfoList &Inputs) {
Michael J. Spencere1696752010-12-18 00:19:12 +00002501 return Args.MakeArgString(
2502 llvm::sys::path::filename(Inputs[0].getBaseInput()));
Daniel Dunbarafec1f52009-03-29 18:40:18 +00002503}
2504
Daniel Dunbard067f7f2009-04-08 23:54:23 +00002505const char *darwin::CC1::getBaseInputStem(const ArgList &Args,
Daniel Dunbar52e96cc2009-03-30 00:34:04 +00002506 const InputInfoList &Inputs) {
Daniel Dunbarafec1f52009-03-29 18:40:18 +00002507 const char *Str = getBaseInputName(Args, Inputs);
2508
Chris Lattner906bb902011-01-16 08:14:11 +00002509 if (const char *End = strrchr(Str, '.'))
Daniel Dunbar1c28f1e2009-09-09 22:32:48 +00002510 return Args.MakeArgString(std::string(Str, End));
Daniel Dunbarafec1f52009-03-29 18:40:18 +00002511
2512 return Str;
2513}
2514
2515const char *
Daniel Dunbard067f7f2009-04-08 23:54:23 +00002516darwin::CC1::getDependencyFileName(const ArgList &Args,
Daniel Dunbar52e96cc2009-03-30 00:34:04 +00002517 const InputInfoList &Inputs) {
Daniel Dunbarafec1f52009-03-29 18:40:18 +00002518 // FIXME: Think about this more.
2519 std::string Res;
2520
2521 if (Arg *OutputOpt = Args.getLastArg(options::OPT_o)) {
2522 std::string Str(OutputOpt->getValue(Args));
Daniel Dunbarafec1f52009-03-29 18:40:18 +00002523 Res = Str.substr(0, Str.rfind('.'));
Chad Rosier6fdf38b2011-08-17 23:08:45 +00002524 } else {
Daniel Dunbar52e96cc2009-03-30 00:34:04 +00002525 Res = darwin::CC1::getBaseInputStem(Args, Inputs);
Chad Rosier6fdf38b2011-08-17 23:08:45 +00002526 }
Daniel Dunbar1c28f1e2009-09-09 22:32:48 +00002527 return Args.MakeArgString(Res + ".d");
Daniel Dunbarafec1f52009-03-29 18:40:18 +00002528}
2529
Chad Rosierbc5ea3d2011-08-17 18:24:55 +00002530void darwin::CC1::RemoveCC1UnsupportedArgs(ArgStringList &CmdArgs) const {
Eric Christopher84fbdb42011-08-19 00:30:14 +00002531 for (ArgStringList::iterator it = CmdArgs.begin(), ie = CmdArgs.end();
Chad Rosierbc5ea3d2011-08-17 18:24:55 +00002532 it != ie;) {
Chad Rosier2f818792011-08-18 17:56:32 +00002533
2534 StringRef Option = *it;
Chad Rosierf29d9aa2011-08-26 18:30:43 +00002535 bool RemoveOption = false;
Chad Rosier30453862011-08-18 01:18:28 +00002536
Chad Rosierf29d9aa2011-08-26 18:30:43 +00002537 // Remove -faltivec
2538 if (Option.equals("-faltivec")) {
2539 it = CmdArgs.erase(it);
2540 ie = CmdArgs.end();
Chad Rosier30453862011-08-18 01:18:28 +00002541 continue;
2542 }
2543
Chad Rosierf29d9aa2011-08-26 18:30:43 +00002544 // Handle machine specific options.
2545 if (Option.startswith("-m")) {
2546 RemoveOption = llvm::StringSwitch<bool>(Option)
2547 .Case("-mthumb", true)
2548 .Case("-mno-thumb", true)
2549 .Case("-mno-fused-madd", true)
2550 .Case("-mlong-branch", true)
2551 .Case("-mlongcall", true)
2552 .Case("-mcpu=G4", true)
2553 .Case("-mcpu=G5", true)
2554 .Default(false);
2555 }
2556
2557 // Handle warning options.
2558 if (Option.startswith("-W")) {
2559 // Remove -W/-Wno- to reduce the number of cases.
2560 if (Option.startswith("-Wno-"))
2561 Option = Option.substr(5);
2562 else
2563 Option = Option.substr(2);
2564
2565 RemoveOption = llvm::StringSwitch<bool>(Option)
2566 .Case("address-of-temporary", true)
2567 .Case("ambiguous-member-template", true)
2568 .Case("analyzer-incompatible-plugin", true)
2569 .Case("array-bounds", true)
2570 .Case("array-bounds-pointer-arithmetic", true)
2571 .Case("bind-to-temporary-copy", true)
2572 .Case("bitwise-op-parentheses", true)
2573 .Case("bool-conversions", true)
2574 .Case("builtin-macro-redefined", true)
2575 .Case("c++-hex-floats", true)
2576 .Case("c++0x-compat", true)
2577 .Case("c++0x-extensions", true)
2578 .Case("c++0x-narrowing", true)
2579 .Case("c++0x-static-nonintegral-init", true)
2580 .Case("conditional-uninitialized", true)
2581 .Case("constant-conversion", true)
2582 .Case("CFString-literal", true)
2583 .Case("constant-logical-operand", true)
2584 .Case("custom-atomic-properties", true)
2585 .Case("default-arg-special-member", true)
2586 .Case("delegating-ctor-cycles", true)
2587 .Case("delete-non-virtual-dtor", true)
2588 .Case("deprecated-implementations", true)
2589 .Case("deprecated-writable-strings", true)
2590 .Case("distributed-object-modifiers", true)
2591 .Case("duplicate-method-arg", true)
2592 .Case("dynamic-class-memaccess", true)
2593 .Case("enum-compare", true)
2594 .Case("exit-time-destructors", true)
2595 .Case("gnu", true)
2596 .Case("gnu-designator", true)
2597 .Case("header-hygiene", true)
2598 .Case("idiomatic-parentheses", true)
2599 .Case("ignored-qualifiers", true)
2600 .Case("implicit-atomic-properties", true)
2601 .Case("incompatible-pointer-types", true)
2602 .Case("incomplete-implementation", true)
2603 .Case("initializer-overrides", true)
2604 .Case("invalid-noreturn", true)
2605 .Case("invalid-token-paste", true)
2606 .Case("literal-conversion", true)
2607 .Case("literal-range", true)
2608 .Case("local-type-template-args", true)
2609 .Case("logical-op-parentheses", true)
2610 .Case("method-signatures", true)
2611 .Case("microsoft", true)
2612 .Case("mismatched-tags", true)
2613 .Case("missing-method-return-type", true)
2614 .Case("non-pod-varargs", true)
2615 .Case("nonfragile-abi2", true)
2616 .Case("null-arithmetic", true)
2617 .Case("null-dereference", true)
2618 .Case("out-of-line-declaration", true)
2619 .Case("overriding-method-mismatch", true)
2620 .Case("readonly-setter-attrs", true)
2621 .Case("return-stack-address", true)
2622 .Case("self-assign", true)
2623 .Case("semicolon-before-method-body", true)
2624 .Case("sentinel", true)
2625 .Case("shift-overflow", true)
2626 .Case("shift-sign-overflow", true)
2627 .Case("sign-conversion", true)
2628 .Case("sizeof-array-argument", true)
2629 .Case("sizeof-pointer-memaccess", true)
2630 .Case("string-compare", true)
2631 .Case("super-class-method-mismatch", true)
2632 .Case("tautological-compare", true)
2633 .Case("typedef-redefinition", true)
2634 .Case("typename-missing", true)
2635 .Case("undefined-reinterpret-cast", true)
2636 .Case("unknown-warning-option", true)
2637 .Case("unnamed-type-template-args", true)
2638 .Case("unneeded-internal-declaration", true)
2639 .Case("unneeded-member-function", true)
2640 .Case("unused-comparison", true)
2641 .Case("unused-exception-parameter", true)
2642 .Case("unused-member-function", true)
2643 .Case("unused-result", true)
2644 .Case("vector-conversions", true)
2645 .Case("vla", true)
2646 .Case("used-but-marked-unused", true)
2647 .Case("weak-vtables", true)
2648 .Default(false);
2649 } // if (Option.startswith("-W"))
Chad Rosier30453862011-08-18 01:18:28 +00002650 if (RemoveOption) {
Chad Rosierbc5ea3d2011-08-17 18:24:55 +00002651 it = CmdArgs.erase(it);
Chad Rosier23594f62011-08-17 18:51:56 +00002652 ie = CmdArgs.end();
Chad Rosier6fdf38b2011-08-17 23:08:45 +00002653 } else {
Chad Rosierbc5ea3d2011-08-17 18:24:55 +00002654 ++it;
Chad Rosier6fdf38b2011-08-17 23:08:45 +00002655 }
Chad Rosierbc5ea3d2011-08-17 18:24:55 +00002656 }
2657}
2658
Daniel Dunbard067f7f2009-04-08 23:54:23 +00002659void darwin::CC1::AddCC1Args(const ArgList &Args,
Daniel Dunbarafec1f52009-03-29 18:40:18 +00002660 ArgStringList &CmdArgs) const {
Daniel Dunbar083edf72009-12-21 18:54:17 +00002661 const Driver &D = getToolChain().getDriver();
Daniel Dunbar4eadb602009-09-10 01:21:12 +00002662
2663 CheckCodeGenerationOptions(D, Args);
2664
Daniel Dunbarafec1f52009-03-29 18:40:18 +00002665 // Derived from cc1 spec.
Daniel Dunbarafec1f52009-03-29 18:40:18 +00002666 if (!Args.hasArg(options::OPT_mkernel) && !Args.hasArg(options::OPT_static) &&
2667 !Args.hasArg(options::OPT_mdynamic_no_pic))
2668 CmdArgs.push_back("-fPIC");
2669
Daniel Dunbar2ffe0292009-09-10 03:37:02 +00002670 if (getToolChain().getTriple().getArch() == llvm::Triple::arm ||
2671 getToolChain().getTriple().getArch() == llvm::Triple::thumb) {
2672 if (!Args.hasArg(options::OPT_fbuiltin_strcat))
2673 CmdArgs.push_back("-fno-builtin-strcat");
2674 if (!Args.hasArg(options::OPT_fbuiltin_strcpy))
2675 CmdArgs.push_back("-fno-builtin-strcpy");
2676 }
2677
Daniel Dunbarafec1f52009-03-29 18:40:18 +00002678 if (Args.hasArg(options::OPT_g_Flag) &&
2679 !Args.hasArg(options::OPT_fno_eliminate_unused_debug_symbols))
2680 CmdArgs.push_back("-feliminate-unused-debug-symbols");
2681}
2682
2683void darwin::CC1::AddCC1OptionsArgs(const ArgList &Args, ArgStringList &CmdArgs,
2684 const InputInfoList &Inputs,
2685 const ArgStringList &OutputArgs) const {
Daniel Dunbar083edf72009-12-21 18:54:17 +00002686 const Driver &D = getToolChain().getDriver();
Daniel Dunbarafec1f52009-03-29 18:40:18 +00002687
2688 // Derived from cc1_options spec.
2689 if (Args.hasArg(options::OPT_fast) ||
2690 Args.hasArg(options::OPT_fastf) ||
2691 Args.hasArg(options::OPT_fastcp))
2692 CmdArgs.push_back("-O3");
Daniel Dunbard067f7f2009-04-08 23:54:23 +00002693
Daniel Dunbarafec1f52009-03-29 18:40:18 +00002694 if (Arg *A = Args.getLastArg(options::OPT_pg))
2695 if (Args.hasArg(options::OPT_fomit_frame_pointer))
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002696 D.Diag(diag::err_drv_argument_not_allowed_with)
Daniel Dunbarafec1f52009-03-29 18:40:18 +00002697 << A->getAsString(Args) << "-fomit-frame-pointer";
2698
2699 AddCC1Args(Args, CmdArgs);
2700
2701 if (!Args.hasArg(options::OPT_Q))
2702 CmdArgs.push_back("-quiet");
2703
2704 CmdArgs.push_back("-dumpbase");
Daniel Dunbar52e96cc2009-03-30 00:34:04 +00002705 CmdArgs.push_back(darwin::CC1::getBaseInputName(Args, Inputs));
Daniel Dunbarafec1f52009-03-29 18:40:18 +00002706
2707 Args.AddAllArgs(CmdArgs, options::OPT_d_Group);
2708
2709 Args.AddAllArgs(CmdArgs, options::OPT_m_Group);
2710 Args.AddAllArgs(CmdArgs, options::OPT_a_Group);
2711
2712 // FIXME: The goal is to use the user provided -o if that is our
2713 // final output, otherwise to drive from the original input
2714 // name. Find a clean way to go about this.
2715 if ((Args.hasArg(options::OPT_c) || Args.hasArg(options::OPT_S)) &&
2716 Args.hasArg(options::OPT_o)) {
2717 Arg *OutputOpt = Args.getLastArg(options::OPT_o);
2718 CmdArgs.push_back("-auxbase-strip");
2719 CmdArgs.push_back(OutputOpt->getValue(Args));
2720 } else {
2721 CmdArgs.push_back("-auxbase");
Daniel Dunbar52e96cc2009-03-30 00:34:04 +00002722 CmdArgs.push_back(darwin::CC1::getBaseInputStem(Args, Inputs));
Daniel Dunbarafec1f52009-03-29 18:40:18 +00002723 }
2724
2725 Args.AddAllArgs(CmdArgs, options::OPT_g_Group);
2726
2727 Args.AddAllArgs(CmdArgs, options::OPT_O);
2728 // FIXME: -Wall is getting some special treatment. Investigate.
2729 Args.AddAllArgs(CmdArgs, options::OPT_W_Group, options::OPT_pedantic_Group);
2730 Args.AddLastArg(CmdArgs, options::OPT_w);
Daniel Dunbard067f7f2009-04-08 23:54:23 +00002731 Args.AddAllArgs(CmdArgs, options::OPT_std_EQ, options::OPT_ansi,
Daniel Dunbarafec1f52009-03-29 18:40:18 +00002732 options::OPT_trigraphs);
Daniel Dunbar72a60902009-04-26 01:10:38 +00002733 if (!Args.getLastArg(options::OPT_std_EQ, options::OPT_ansi)) {
2734 // Honor -std-default.
2735 Args.AddAllArgsTranslated(CmdArgs, options::OPT_std_default_EQ,
2736 "-std=", /*Joined=*/true);
2737 }
2738
Daniel Dunbarafec1f52009-03-29 18:40:18 +00002739 if (Args.hasArg(options::OPT_v))
2740 CmdArgs.push_back("-version");
Daniel Dunbar733b0f82011-03-01 18:49:30 +00002741 if (Args.hasArg(options::OPT_pg) &&
2742 getToolChain().SupportsProfiling())
Daniel Dunbarafec1f52009-03-29 18:40:18 +00002743 CmdArgs.push_back("-p");
2744 Args.AddLastArg(CmdArgs, options::OPT_p);
Daniel Dunbard067f7f2009-04-08 23:54:23 +00002745
Daniel Dunbarafec1f52009-03-29 18:40:18 +00002746 // The driver treats -fsyntax-only specially.
Daniel Dunbar2ffe0292009-09-10 03:37:02 +00002747 if (getToolChain().getTriple().getArch() == llvm::Triple::arm ||
2748 getToolChain().getTriple().getArch() == llvm::Triple::thumb) {
2749 // Removes -fbuiltin-str{cat,cpy}; these aren't recognized by cc1 but are
2750 // used to inhibit the default -fno-builtin-str{cat,cpy}.
2751 //
2752 // FIXME: Should we grow a better way to deal with "removing" args?
Daniel Dunbar44b36ee2009-11-25 11:53:23 +00002753 for (arg_iterator it = Args.filtered_begin(options::OPT_f_Group,
2754 options::OPT_fsyntax_only),
2755 ie = Args.filtered_end(); it != ie; ++it) {
Daniel Dunbara442fd52010-06-11 22:00:13 +00002756 if (!(*it)->getOption().matches(options::OPT_fbuiltin_strcat) &&
2757 !(*it)->getOption().matches(options::OPT_fbuiltin_strcpy)) {
2758 (*it)->claim();
2759 (*it)->render(Args, CmdArgs);
Daniel Dunbar2ffe0292009-09-10 03:37:02 +00002760 }
2761 }
2762 } else
2763 Args.AddAllArgs(CmdArgs, options::OPT_f_Group, options::OPT_fsyntax_only);
Daniel Dunbard067f7f2009-04-08 23:54:23 +00002764
Daniel Dunbarf28c2ff2011-04-07 20:41:03 +00002765 // Claim Clang only -f options, they aren't worth warning about.
2766 Args.ClaimAllArgs(options::OPT_f_clang_Group);
2767
Daniel Dunbarafec1f52009-03-29 18:40:18 +00002768 Args.AddAllArgs(CmdArgs, options::OPT_undef);
2769 if (Args.hasArg(options::OPT_Qn))
2770 CmdArgs.push_back("-fno-ident");
Daniel Dunbard067f7f2009-04-08 23:54:23 +00002771
Daniel Dunbarafec1f52009-03-29 18:40:18 +00002772 // FIXME: This isn't correct.
2773 //Args.AddLastArg(CmdArgs, options::OPT__help)
2774 //Args.AddLastArg(CmdArgs, options::OPT__targetHelp)
2775
2776 CmdArgs.append(OutputArgs.begin(), OutputArgs.end());
2777
2778 // FIXME: Still don't get what is happening here. Investigate.
2779 Args.AddAllArgs(CmdArgs, options::OPT__param);
2780
2781 if (Args.hasArg(options::OPT_fmudflap) ||
2782 Args.hasArg(options::OPT_fmudflapth)) {
2783 CmdArgs.push_back("-fno-builtin");
2784 CmdArgs.push_back("-fno-merge-constants");
2785 }
Daniel Dunbard067f7f2009-04-08 23:54:23 +00002786
Daniel Dunbarafec1f52009-03-29 18:40:18 +00002787 if (Args.hasArg(options::OPT_coverage)) {
2788 CmdArgs.push_back("-fprofile-arcs");
2789 CmdArgs.push_back("-ftest-coverage");
2790 }
2791
2792 if (types::isCXX(Inputs[0].getType()))
2793 CmdArgs.push_back("-D__private_extern__=extern");
2794}
2795
2796void darwin::CC1::AddCPPOptionsArgs(const ArgList &Args, ArgStringList &CmdArgs,
2797 const InputInfoList &Inputs,
2798 const ArgStringList &OutputArgs) const {
2799 // Derived from cpp_options
2800 AddCPPUniqueOptionsArgs(Args, CmdArgs, Inputs);
Daniel Dunbard067f7f2009-04-08 23:54:23 +00002801
Daniel Dunbarafec1f52009-03-29 18:40:18 +00002802 CmdArgs.append(OutputArgs.begin(), OutputArgs.end());
2803
2804 AddCC1Args(Args, CmdArgs);
2805
2806 // NOTE: The code below has some commonality with cpp_options, but
2807 // in classic gcc style ends up sending things in different
2808 // orders. This may be a good merge candidate once we drop pedantic
2809 // compatibility.
2810
2811 Args.AddAllArgs(CmdArgs, options::OPT_m_Group);
Daniel Dunbard067f7f2009-04-08 23:54:23 +00002812 Args.AddAllArgs(CmdArgs, options::OPT_std_EQ, options::OPT_ansi,
Daniel Dunbarafec1f52009-03-29 18:40:18 +00002813 options::OPT_trigraphs);
Daniel Dunbar72a60902009-04-26 01:10:38 +00002814 if (!Args.getLastArg(options::OPT_std_EQ, options::OPT_ansi)) {
2815 // Honor -std-default.
2816 Args.AddAllArgsTranslated(CmdArgs, options::OPT_std_default_EQ,
2817 "-std=", /*Joined=*/true);
2818 }
Daniel Dunbarafec1f52009-03-29 18:40:18 +00002819 Args.AddAllArgs(CmdArgs, options::OPT_W_Group, options::OPT_pedantic_Group);
2820 Args.AddLastArg(CmdArgs, options::OPT_w);
Daniel Dunbard067f7f2009-04-08 23:54:23 +00002821
Daniel Dunbarafec1f52009-03-29 18:40:18 +00002822 // The driver treats -fsyntax-only specially.
2823 Args.AddAllArgs(CmdArgs, options::OPT_f_Group, options::OPT_fsyntax_only);
2824
Daniel Dunbarf28c2ff2011-04-07 20:41:03 +00002825 // Claim Clang only -f options, they aren't worth warning about.
2826 Args.ClaimAllArgs(options::OPT_f_clang_Group);
2827
Daniel Dunbarafec1f52009-03-29 18:40:18 +00002828 if (Args.hasArg(options::OPT_g_Group) && !Args.hasArg(options::OPT_g0) &&
2829 !Args.hasArg(options::OPT_fno_working_directory))
2830 CmdArgs.push_back("-fworking-directory");
2831
2832 Args.AddAllArgs(CmdArgs, options::OPT_O);
2833 Args.AddAllArgs(CmdArgs, options::OPT_undef);
2834 if (Args.hasArg(options::OPT_save_temps))
2835 CmdArgs.push_back("-fpch-preprocess");
2836}
2837
Daniel Dunbard067f7f2009-04-08 23:54:23 +00002838void darwin::CC1::AddCPPUniqueOptionsArgs(const ArgList &Args,
Daniel Dunbarafec1f52009-03-29 18:40:18 +00002839 ArgStringList &CmdArgs,
Mike Stump11289f42009-09-09 15:08:12 +00002840 const InputInfoList &Inputs) const {
Daniel Dunbar083edf72009-12-21 18:54:17 +00002841 const Driver &D = getToolChain().getDriver();
Daniel Dunbarafec1f52009-03-29 18:40:18 +00002842
Daniel Dunbar64198ef2009-09-10 01:21:05 +00002843 CheckPreprocessingOptions(D, Args);
2844
Daniel Dunbarafec1f52009-03-29 18:40:18 +00002845 // Derived from cpp_unique_options.
Daniel Dunbar64198ef2009-09-10 01:21:05 +00002846 // -{C,CC} only with -E is checked in CheckPreprocessingOptions().
2847 Args.AddLastArg(CmdArgs, options::OPT_C);
2848 Args.AddLastArg(CmdArgs, options::OPT_CC);
Daniel Dunbarafec1f52009-03-29 18:40:18 +00002849 if (!Args.hasArg(options::OPT_Q))
2850 CmdArgs.push_back("-quiet");
2851 Args.AddAllArgs(CmdArgs, options::OPT_nostdinc);
Douglas Gregor64b046f2010-03-24 20:13:48 +00002852 Args.AddAllArgs(CmdArgs, options::OPT_nostdincxx);
Daniel Dunbarafec1f52009-03-29 18:40:18 +00002853 Args.AddLastArg(CmdArgs, options::OPT_v);
2854 Args.AddAllArgs(CmdArgs, options::OPT_I_Group, options::OPT_F);
2855 Args.AddLastArg(CmdArgs, options::OPT_P);
2856
2857 // FIXME: Handle %I properly.
2858 if (getToolChain().getArchName() == "x86_64") {
2859 CmdArgs.push_back("-imultilib");
2860 CmdArgs.push_back("x86_64");
2861 }
2862
2863 if (Args.hasArg(options::OPT_MD)) {
2864 CmdArgs.push_back("-MD");
Daniel Dunbar52e96cc2009-03-30 00:34:04 +00002865 CmdArgs.push_back(darwin::CC1::getDependencyFileName(Args, Inputs));
Daniel Dunbarafec1f52009-03-29 18:40:18 +00002866 }
2867
2868 if (Args.hasArg(options::OPT_MMD)) {
2869 CmdArgs.push_back("-MMD");
Daniel Dunbar52e96cc2009-03-30 00:34:04 +00002870 CmdArgs.push_back(darwin::CC1::getDependencyFileName(Args, Inputs));
Daniel Dunbarafec1f52009-03-29 18:40:18 +00002871 }
2872
2873 Args.AddLastArg(CmdArgs, options::OPT_M);
2874 Args.AddLastArg(CmdArgs, options::OPT_MM);
2875 Args.AddAllArgs(CmdArgs, options::OPT_MF);
2876 Args.AddLastArg(CmdArgs, options::OPT_MG);
2877 Args.AddLastArg(CmdArgs, options::OPT_MP);
2878 Args.AddAllArgs(CmdArgs, options::OPT_MQ);
2879 Args.AddAllArgs(CmdArgs, options::OPT_MT);
2880 if (!Args.hasArg(options::OPT_M) && !Args.hasArg(options::OPT_MM) &&
2881 (Args.hasArg(options::OPT_MD) || Args.hasArg(options::OPT_MMD))) {
2882 if (Arg *OutputOpt = Args.getLastArg(options::OPT_o)) {
2883 CmdArgs.push_back("-MQ");
2884 CmdArgs.push_back(OutputOpt->getValue(Args));
2885 }
2886 }
2887
2888 Args.AddLastArg(CmdArgs, options::OPT_remap);
2889 if (Args.hasArg(options::OPT_g3))
2890 CmdArgs.push_back("-dD");
2891 Args.AddLastArg(CmdArgs, options::OPT_H);
2892
2893 AddCPPArgs(Args, CmdArgs);
2894
2895 Args.AddAllArgs(CmdArgs, options::OPT_D, options::OPT_U, options::OPT_A);
2896 Args.AddAllArgs(CmdArgs, options::OPT_i_Group);
2897
2898 for (InputInfoList::const_iterator
2899 it = Inputs.begin(), ie = Inputs.end(); it != ie; ++it) {
2900 const InputInfo &II = *it;
Daniel Dunbard067f7f2009-04-08 23:54:23 +00002901
Daniel Dunbarb440f562010-08-02 02:38:21 +00002902 CmdArgs.push_back(II.getFilename());
Daniel Dunbarafec1f52009-03-29 18:40:18 +00002903 }
2904
2905 Args.AddAllArgValues(CmdArgs, options::OPT_Wp_COMMA,
2906 options::OPT_Xpreprocessor);
2907
2908 if (Args.hasArg(options::OPT_fmudflap)) {
2909 CmdArgs.push_back("-D_MUDFLAP");
2910 CmdArgs.push_back("-include");
2911 CmdArgs.push_back("mf-runtime.h");
2912 }
2913
2914 if (Args.hasArg(options::OPT_fmudflapth)) {
2915 CmdArgs.push_back("-D_MUDFLAP");
2916 CmdArgs.push_back("-D_MUDFLAPTH");
2917 CmdArgs.push_back("-include");
2918 CmdArgs.push_back("mf-runtime.h");
2919 }
2920}
2921
Daniel Dunbard067f7f2009-04-08 23:54:23 +00002922void darwin::CC1::AddCPPArgs(const ArgList &Args,
Daniel Dunbarafec1f52009-03-29 18:40:18 +00002923 ArgStringList &CmdArgs) const {
2924 // Derived from cpp spec.
2925
2926 if (Args.hasArg(options::OPT_static)) {
2927 // The gcc spec is broken here, it refers to dynamic but
2928 // that has been translated. Start by being bug compatible.
Daniel Dunbard067f7f2009-04-08 23:54:23 +00002929
Daniel Dunbarafec1f52009-03-29 18:40:18 +00002930 // if (!Args.hasArg(arglist.parser.dynamicOption))
2931 CmdArgs.push_back("-D__STATIC__");
2932 } else
2933 CmdArgs.push_back("-D__DYNAMIC__");
2934
2935 if (Args.hasArg(options::OPT_pthread))
2936 CmdArgs.push_back("-D_REENTRANT");
2937}
2938
Daniel Dunbare6adeee2009-03-29 17:08:39 +00002939void darwin::Preprocess::ConstructJob(Compilation &C, const JobAction &JA,
Daniel Dunbar7fbaf532010-08-02 02:38:28 +00002940 const InputInfo &Output,
Daniel Dunbard067f7f2009-04-08 23:54:23 +00002941 const InputInfoList &Inputs,
2942 const ArgList &Args,
Daniel Dunbare6adeee2009-03-29 17:08:39 +00002943 const char *LinkingOutput) const {
2944 ArgStringList CmdArgs;
2945
2946 assert(Inputs.size() == 1 && "Unexpected number of inputs!");
2947
2948 CmdArgs.push_back("-E");
2949
2950 if (Args.hasArg(options::OPT_traditional) ||
Daniel Dunbare6adeee2009-03-29 17:08:39 +00002951 Args.hasArg(options::OPT_traditional_cpp))
2952 CmdArgs.push_back("-traditional-cpp");
Daniel Dunbard067f7f2009-04-08 23:54:23 +00002953
Daniel Dunbare6adeee2009-03-29 17:08:39 +00002954 ArgStringList OutputArgs;
Daniel Dunbarb440f562010-08-02 02:38:21 +00002955 assert(Output.isFilename() && "Unexpected CC1 output.");
2956 OutputArgs.push_back("-o");
2957 OutputArgs.push_back(Output.getFilename());
Daniel Dunbare6adeee2009-03-29 17:08:39 +00002958
Joerg Sonnenbergerb86f5f42011-03-06 23:31:01 +00002959 if (Args.hasArg(options::OPT_E) || getToolChain().getDriver().CCCIsCPP) {
Daniel Dunbarf64f5302009-03-29 22:27:40 +00002960 AddCPPOptionsArgs(Args, CmdArgs, Inputs, OutputArgs);
2961 } else {
2962 AddCPPOptionsArgs(Args, CmdArgs, Inputs, ArgStringList());
2963 CmdArgs.append(OutputArgs.begin(), OutputArgs.end());
2964 }
Daniel Dunbard067f7f2009-04-08 23:54:23 +00002965
Daniel Dunbar6a8803a2009-04-03 01:27:06 +00002966 Args.AddAllArgs(CmdArgs, options::OPT_d_Group);
2967
Chad Rosierc31e48d2011-09-08 00:38:00 +00002968 RemoveCC1UnsupportedArgs(CmdArgs);
2969
Daniel Dunbare6adeee2009-03-29 17:08:39 +00002970 const char *CC1Name = getCC1Name(Inputs[0].getType());
Daniel Dunbard067f7f2009-04-08 23:54:23 +00002971 const char *Exec =
Daniel Dunbar9c3ed5f2010-07-14 18:46:23 +00002972 Args.MakeArgString(getToolChain().GetProgramPath(CC1Name));
Daniel Dunbar7fbaf532010-08-02 02:38:28 +00002973 C.addCommand(new Command(JA, *this, Exec, CmdArgs));
Daniel Dunbare6adeee2009-03-29 17:08:39 +00002974}
2975
2976void darwin::Compile::ConstructJob(Compilation &C, const JobAction &JA,
Daniel Dunbar7fbaf532010-08-02 02:38:28 +00002977 const InputInfo &Output,
Daniel Dunbard067f7f2009-04-08 23:54:23 +00002978 const InputInfoList &Inputs,
2979 const ArgList &Args,
Daniel Dunbare6adeee2009-03-29 17:08:39 +00002980 const char *LinkingOutput) const {
Daniel Dunbar083edf72009-12-21 18:54:17 +00002981 const Driver &D = getToolChain().getDriver();
Daniel Dunbare6adeee2009-03-29 17:08:39 +00002982 ArgStringList CmdArgs;
2983
2984 assert(Inputs.size() == 1 && "Unexpected number of inputs!");
2985
2986 types::ID InputType = Inputs[0].getType();
2987 const Arg *A;
Eli Friedmanbb0d9a52009-07-14 21:58:17 +00002988 if ((A = Args.getLastArg(options::OPT_traditional)))
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002989 D.Diag(diag::err_drv_argument_only_allowed_with)
Daniel Dunbare6adeee2009-03-29 17:08:39 +00002990 << A->getAsString(Args) << "-E";
2991
Daniel Dunbar24e52992010-06-07 23:28:45 +00002992 if (JA.getType() == types::TY_LLVM_IR ||
2993 JA.getType() == types::TY_LTO_IR)
Daniel Dunbare6adeee2009-03-29 17:08:39 +00002994 CmdArgs.push_back("-emit-llvm");
Daniel Dunbar24e52992010-06-07 23:28:45 +00002995 else if (JA.getType() == types::TY_LLVM_BC ||
2996 JA.getType() == types::TY_LTO_BC)
Daniel Dunbare6adeee2009-03-29 17:08:39 +00002997 CmdArgs.push_back("-emit-llvm-bc");
Daniel Dunbar6cdf83c2009-09-01 16:57:46 +00002998 else if (Output.getType() == types::TY_AST)
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002999 D.Diag(diag::err_drv_no_ast_support)
Daniel Dunbar1c28f1e2009-09-09 22:32:48 +00003000 << getToolChain().getTripleString();
Daniel Dunbarbcd554f2010-02-11 17:33:45 +00003001 else if (JA.getType() != types::TY_PP_Asm &&
3002 JA.getType() != types::TY_PCH)
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003003 D.Diag(diag::err_drv_invalid_gcc_output_type)
Daniel Dunbaraeea8ac2010-02-11 03:16:21 +00003004 << getTypeName(JA.getType());
Daniel Dunbare6adeee2009-03-29 17:08:39 +00003005
3006 ArgStringList OutputArgs;
3007 if (Output.getType() != types::TY_PCH) {
3008 OutputArgs.push_back("-o");
Daniel Dunbarb440f562010-08-02 02:38:21 +00003009 if (Output.isNothing())
Daniel Dunbare6adeee2009-03-29 17:08:39 +00003010 OutputArgs.push_back("/dev/null");
3011 else
3012 OutputArgs.push_back(Output.getFilename());
3013 }
3014
3015 // There is no need for this level of compatibility, but it makes
3016 // diffing easier.
3017 bool OutputArgsEarly = (Args.hasArg(options::OPT_fsyntax_only) ||
3018 Args.hasArg(options::OPT_S));
3019
3020 if (types::getPreprocessedType(InputType) != types::TY_INVALID) {
Daniel Dunbarafec1f52009-03-29 18:40:18 +00003021 AddCPPUniqueOptionsArgs(Args, CmdArgs, Inputs);
Daniel Dunbare6adeee2009-03-29 17:08:39 +00003022 if (OutputArgsEarly) {
3023 AddCC1OptionsArgs(Args, CmdArgs, Inputs, OutputArgs);
3024 } else {
3025 AddCC1OptionsArgs(Args, CmdArgs, Inputs, ArgStringList());
3026 CmdArgs.append(OutputArgs.begin(), OutputArgs.end());
3027 }
3028 } else {
3029 CmdArgs.push_back("-fpreprocessed");
Daniel Dunbard067f7f2009-04-08 23:54:23 +00003030
Daniel Dunbare6adeee2009-03-29 17:08:39 +00003031 for (InputInfoList::const_iterator
3032 it = Inputs.begin(), ie = Inputs.end(); it != ie; ++it) {
3033 const InputInfo &II = *it;
3034
Daniel Dunbar6cdf83c2009-09-01 16:57:46 +00003035 // Reject AST inputs.
3036 if (II.getType() == types::TY_AST) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003037 D.Diag(diag::err_drv_no_ast_support)
Daniel Dunbar1c28f1e2009-09-09 22:32:48 +00003038 << getToolChain().getTripleString();
Daniel Dunbar6cdf83c2009-09-01 16:57:46 +00003039 return;
3040 }
3041
Daniel Dunbarb440f562010-08-02 02:38:21 +00003042 CmdArgs.push_back(II.getFilename());
Daniel Dunbare6adeee2009-03-29 17:08:39 +00003043 }
3044
3045 if (OutputArgsEarly) {
3046 AddCC1OptionsArgs(Args, CmdArgs, Inputs, OutputArgs);
3047 } else {
3048 AddCC1OptionsArgs(Args, CmdArgs, Inputs, ArgStringList());
3049 CmdArgs.append(OutputArgs.begin(), OutputArgs.end());
3050 }
3051 }
Daniel Dunbard067f7f2009-04-08 23:54:23 +00003052
Daniel Dunbare6adeee2009-03-29 17:08:39 +00003053 if (Output.getType() == types::TY_PCH) {
3054 assert(Output.isFilename() && "Invalid PCH output.");
3055
3056 CmdArgs.push_back("-o");
3057 // NOTE: gcc uses a temp .s file for this, but there doesn't seem
3058 // to be a good reason.
Chad Rosier96d690c2011-08-01 19:58:48 +00003059 const char *TmpPath = C.getArgs().MakeArgString(
Chad Rosier39ab7432011-08-26 21:28:44 +00003060 D.GetTemporaryPath("cc", "s"));
Chad Rosier96d690c2011-08-01 19:58:48 +00003061 C.addTempFile(TmpPath);
3062 CmdArgs.push_back(TmpPath);
Daniel Dunbard067f7f2009-04-08 23:54:23 +00003063
Eric Christopher84fbdb42011-08-19 00:30:14 +00003064 // If we're emitting a pch file with the last 4 characters of ".pth"
3065 // and falling back to llvm-gcc we want to use ".gch" instead.
3066 std::string OutputFile(Output.getFilename());
3067 size_t loc = OutputFile.rfind(".pth");
3068 if (loc != std::string::npos)
3069 OutputFile.replace(loc, 4, ".gch");
3070 const char *Tmp = C.getArgs().MakeArgString("--output-pch="+OutputFile);
3071 CmdArgs.push_back(Tmp);
Daniel Dunbard067f7f2009-04-08 23:54:23 +00003072 }
Daniel Dunbare6adeee2009-03-29 17:08:39 +00003073
Chad Rosierbc5ea3d2011-08-17 18:24:55 +00003074 RemoveCC1UnsupportedArgs(CmdArgs);
3075
Daniel Dunbare6adeee2009-03-29 17:08:39 +00003076 const char *CC1Name = getCC1Name(Inputs[0].getType());
Daniel Dunbard067f7f2009-04-08 23:54:23 +00003077 const char *Exec =
Daniel Dunbar9c3ed5f2010-07-14 18:46:23 +00003078 Args.MakeArgString(getToolChain().GetProgramPath(CC1Name));
Daniel Dunbar7fbaf532010-08-02 02:38:28 +00003079 C.addCommand(new Command(JA, *this, Exec, CmdArgs));
Daniel Dunbare6adeee2009-03-29 17:08:39 +00003080}
3081
Daniel Dunbarbe220842009-03-20 16:06:39 +00003082void darwin::Assemble::ConstructJob(Compilation &C, const JobAction &JA,
Daniel Dunbar7fbaf532010-08-02 02:38:28 +00003083 const InputInfo &Output,
Daniel Dunbard067f7f2009-04-08 23:54:23 +00003084 const InputInfoList &Inputs,
3085 const ArgList &Args,
Daniel Dunbarbe220842009-03-20 16:06:39 +00003086 const char *LinkingOutput) const {
3087 ArgStringList CmdArgs;
3088
3089 assert(Inputs.size() == 1 && "Unexpected number of inputs.");
3090 const InputInfo &Input = Inputs[0];
3091
Daniel Dunbardc8355e2011-04-12 23:59:20 +00003092 // Determine the original source input.
3093 const Action *SourceAction = &JA;
3094 while (SourceAction->getKind() != Action::InputClass) {
3095 assert(!SourceAction->getInputs().empty() && "unexpected root action!");
3096 SourceAction = SourceAction->getInputs()[0];
3097 }
3098
3099 // Forward -g, assuming we are dealing with an actual assembly file.
Eric Christopher84fbdb42011-08-19 00:30:14 +00003100 if (SourceAction->getType() == types::TY_Asm ||
Daniel Dunbardc8355e2011-04-12 23:59:20 +00003101 SourceAction->getType() == types::TY_PP_Asm) {
Daniel Dunbar5c9c1182009-04-01 00:27:44 +00003102 if (Args.hasArg(options::OPT_gstabs))
3103 CmdArgs.push_back("--gstabs");
3104 else if (Args.hasArg(options::OPT_g_Group))
3105 CmdArgs.push_back("--gdwarf2");
3106 }
Daniel Dunbard067f7f2009-04-08 23:54:23 +00003107
Daniel Dunbarbe220842009-03-20 16:06:39 +00003108 // Derived from asm spec.
Daniel Dunbar3571dd92009-09-09 18:36:27 +00003109 AddDarwinArch(Args, CmdArgs);
Daniel Dunbarbe220842009-03-20 16:06:39 +00003110
Daniel Dunbar6d484762010-07-22 01:47:22 +00003111 // Use -force_cpusubtype_ALL on x86 by default.
3112 if (getToolChain().getTriple().getArch() == llvm::Triple::x86 ||
3113 getToolChain().getTriple().getArch() == llvm::Triple::x86_64 ||
Daniel Dunbar3571dd92009-09-09 18:36:27 +00003114 Args.hasArg(options::OPT_force__cpusubtype__ALL))
3115 CmdArgs.push_back("-force_cpusubtype_ALL");
3116
Daniel Dunbara6b4a3d2009-08-24 22:26:16 +00003117 if (getToolChain().getTriple().getArch() != llvm::Triple::x86_64 &&
3118 (Args.hasArg(options::OPT_mkernel) ||
Daniel Dunbarbe220842009-03-20 16:06:39 +00003119 Args.hasArg(options::OPT_static) ||
Daniel Dunbara6b4a3d2009-08-24 22:26:16 +00003120 Args.hasArg(options::OPT_fapple_kext)))
Daniel Dunbard067f7f2009-04-08 23:54:23 +00003121 CmdArgs.push_back("-static");
3122
Daniel Dunbarbe220842009-03-20 16:06:39 +00003123 Args.AddAllArgValues(CmdArgs, options::OPT_Wa_COMMA,
3124 options::OPT_Xassembler);
3125
3126 assert(Output.isFilename() && "Unexpected lipo output.");
3127 CmdArgs.push_back("-o");
3128 CmdArgs.push_back(Output.getFilename());
3129
Daniel Dunbarb440f562010-08-02 02:38:21 +00003130 assert(Input.isFilename() && "Invalid input.");
3131 CmdArgs.push_back(Input.getFilename());
Daniel Dunbarbe220842009-03-20 16:06:39 +00003132
3133 // asm_final spec is empty.
3134
Daniel Dunbard067f7f2009-04-08 23:54:23 +00003135 const char *Exec =
Daniel Dunbar9c3ed5f2010-07-14 18:46:23 +00003136 Args.MakeArgString(getToolChain().GetProgramPath("as"));
Daniel Dunbar7fbaf532010-08-02 02:38:28 +00003137 C.addCommand(new Command(JA, *this, Exec, CmdArgs));
Daniel Dunbarbe220842009-03-20 16:06:39 +00003138}
Daniel Dunbar64ed5e32009-03-20 00:52:38 +00003139
Daniel Dunbare9ded432009-09-09 18:36:20 +00003140void darwin::DarwinTool::AddDarwinArch(const ArgList &Args,
3141 ArgStringList &CmdArgs) const {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003142 StringRef ArchName = getDarwinToolChain().getDarwinArchName(Args);
Daniel Dunbardcc3b652010-01-22 02:04:58 +00003143
Daniel Dunbarc1964212009-03-26 16:23:12 +00003144 // Derived from darwin_arch spec.
3145 CmdArgs.push_back("-arch");
Daniel Dunbardcc3b652010-01-22 02:04:58 +00003146 CmdArgs.push_back(Args.MakeArgString(ArchName));
Daniel Dunbar91dbfd62009-09-04 18:35:31 +00003147
Daniel Dunbardcc3b652010-01-22 02:04:58 +00003148 // FIXME: Is this needed anymore?
3149 if (ArchName == "arm")
Daniel Dunbar91dbfd62009-09-04 18:35:31 +00003150 CmdArgs.push_back("-force_cpusubtype_ALL");
Daniel Dunbarc1964212009-03-26 16:23:12 +00003151}
3152
Daniel Dunbarccbc4522010-09-09 21:51:05 +00003153void darwin::Link::AddLinkArgs(Compilation &C,
3154 const ArgList &Args,
Daniel Dunbarc1964212009-03-26 16:23:12 +00003155 ArgStringList &CmdArgs) const {
Daniel Dunbar083edf72009-12-21 18:54:17 +00003156 const Driver &D = getToolChain().getDriver();
Daniel Dunbarc44d3132011-04-28 21:23:41 +00003157 const toolchains::Darwin &DarwinTC = getDarwinToolChain();
Daniel Dunbarc1964212009-03-26 16:23:12 +00003158
Daniel Dunbarcacb0e22010-08-11 23:07:50 +00003159 unsigned Version[3] = { 0, 0, 0 };
3160 if (Arg *A = Args.getLastArg(options::OPT_mlinker_version_EQ)) {
3161 bool HadExtra;
3162 if (!Driver::GetReleaseVersion(A->getValue(Args), Version[0],
3163 Version[1], Version[2], HadExtra) ||
3164 HadExtra)
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003165 D.Diag(diag::err_drv_invalid_version_number)
Daniel Dunbarcacb0e22010-08-11 23:07:50 +00003166 << A->getAsString(Args);
3167 }
3168
3169 // Newer linkers support -demangle, pass it if supported and not disabled by
3170 // the user.
Daniel Dunbar6d776eb2010-11-19 17:51:40 +00003171 //
3172 // FIXME: We temporarily avoid passing -demangle to any iOS linker, because
3173 // unfortunately we can't be guaranteed that the linker version used there
3174 // will match the linker version detected at configure time. We need the
3175 // universal driver.
3176 if (Version[0] >= 100 && !Args.hasArg(options::OPT_Z_Xlinker__no_demangle) &&
Daniel Dunbarc44d3132011-04-28 21:23:41 +00003177 !DarwinTC.isTargetIPhoneOS()) {
Daniel Dunbar1eaf5c62010-09-07 17:07:49 +00003178 // Don't pass -demangle to ld_classic.
3179 //
3180 // FIXME: This is a temporary workaround, ld should be handling this.
3181 bool UsesLdClassic = (getToolChain().getArch() == llvm::Triple::x86 &&
3182 Args.hasArg(options::OPT_static));
Daniel Dunbar3d7e0e22010-09-07 17:50:41 +00003183 if (getToolChain().getArch() == llvm::Triple::x86) {
3184 for (arg_iterator it = Args.filtered_begin(options::OPT_Xlinker,
3185 options::OPT_Wl_COMMA),
3186 ie = Args.filtered_end(); it != ie; ++it) {
3187 const Arg *A = *it;
3188 for (unsigned i = 0, e = A->getNumValues(); i != e; ++i)
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003189 if (StringRef(A->getValue(Args, i)) == "-kext")
Daniel Dunbar3d7e0e22010-09-07 17:50:41 +00003190 UsesLdClassic = true;
3191 }
3192 }
Daniel Dunbar1eaf5c62010-09-07 17:07:49 +00003193 if (!UsesLdClassic)
3194 CmdArgs.push_back("-demangle");
Daniel Dunbarcacb0e22010-08-11 23:07:50 +00003195 }
3196
Daniel Dunbaref889c72011-06-21 20:55:11 +00003197 // If we are using LTO, then automatically create a temporary file path for
3198 // the linker to use, so that it's lifetime will extend past a possible
3199 // dsymutil step.
Daniel Dunbar3d125d32011-06-21 21:18:32 +00003200 if (Version[0] >= 116 && D.IsUsingLTO(Args)) {
Daniel Dunbaref889c72011-06-21 20:55:11 +00003201 const char *TmpPath = C.getArgs().MakeArgString(
Chad Rosier39ab7432011-08-26 21:28:44 +00003202 D.GetTemporaryPath("cc", types::getTypeTempSuffix(types::TY_Object)));
Daniel Dunbaref889c72011-06-21 20:55:11 +00003203 C.addTempFile(TmpPath);
3204 CmdArgs.push_back("-object_path_lto");
3205 CmdArgs.push_back(TmpPath);
3206 }
3207
Daniel Dunbarc1964212009-03-26 16:23:12 +00003208 // Derived from the "link" spec.
3209 Args.AddAllArgs(CmdArgs, options::OPT_static);
3210 if (!Args.hasArg(options::OPT_static))
3211 CmdArgs.push_back("-dynamic");
3212 if (Args.hasArg(options::OPT_fgnu_runtime)) {
3213 // FIXME: gcc replaces -lobjc in forward args with -lobjc-gnu
3214 // here. How do we wish to handle such things?
3215 }
Daniel Dunbard067f7f2009-04-08 23:54:23 +00003216
Daniel Dunbarc1964212009-03-26 16:23:12 +00003217 if (!Args.hasArg(options::OPT_dynamiclib)) {
Daniel Dunbara48823f2010-01-22 02:04:52 +00003218 AddDarwinArch(Args, CmdArgs);
Daniel Dunbara48823f2010-01-22 02:04:52 +00003219 // FIXME: Why do this only on this path?
Daniel Dunbar93d7acf2010-01-22 03:37:33 +00003220 Args.AddLastArg(CmdArgs, options::OPT_force__cpusubtype__ALL);
Daniel Dunbarc1964212009-03-26 16:23:12 +00003221
3222 Args.AddLastArg(CmdArgs, options::OPT_bundle);
3223 Args.AddAllArgs(CmdArgs, options::OPT_bundle__loader);
3224 Args.AddAllArgs(CmdArgs, options::OPT_client__name);
3225
3226 Arg *A;
3227 if ((A = Args.getLastArg(options::OPT_compatibility__version)) ||
3228 (A = Args.getLastArg(options::OPT_current__version)) ||
3229 (A = Args.getLastArg(options::OPT_install__name)))
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003230 D.Diag(diag::err_drv_argument_only_allowed_with)
Daniel Dunbarc1964212009-03-26 16:23:12 +00003231 << A->getAsString(Args) << "-dynamiclib";
3232
3233 Args.AddLastArg(CmdArgs, options::OPT_force__flat__namespace);
3234 Args.AddLastArg(CmdArgs, options::OPT_keep__private__externs);
3235 Args.AddLastArg(CmdArgs, options::OPT_private__bundle);
3236 } else {
3237 CmdArgs.push_back("-dylib");
3238
3239 Arg *A;
3240 if ((A = Args.getLastArg(options::OPT_bundle)) ||
3241 (A = Args.getLastArg(options::OPT_bundle__loader)) ||
3242 (A = Args.getLastArg(options::OPT_client__name)) ||
3243 (A = Args.getLastArg(options::OPT_force__flat__namespace)) ||
3244 (A = Args.getLastArg(options::OPT_keep__private__externs)) ||
3245 (A = Args.getLastArg(options::OPT_private__bundle)))
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003246 D.Diag(diag::err_drv_argument_not_allowed_with)
Daniel Dunbarc1964212009-03-26 16:23:12 +00003247 << A->getAsString(Args) << "-dynamiclib";
Daniel Dunbard067f7f2009-04-08 23:54:23 +00003248
Daniel Dunbarc1964212009-03-26 16:23:12 +00003249 Args.AddAllArgsTranslated(CmdArgs, options::OPT_compatibility__version,
3250 "-dylib_compatibility_version");
3251 Args.AddAllArgsTranslated(CmdArgs, options::OPT_current__version,
3252 "-dylib_current_version");
3253
Daniel Dunbara48823f2010-01-22 02:04:52 +00003254 AddDarwinArch(Args, CmdArgs);
Daniel Dunbarc1964212009-03-26 16:23:12 +00003255
3256 Args.AddAllArgsTranslated(CmdArgs, options::OPT_install__name,
3257 "-dylib_install_name");
3258 }
3259
3260 Args.AddLastArg(CmdArgs, options::OPT_all__load);
3261 Args.AddAllArgs(CmdArgs, options::OPT_allowable__client);
3262 Args.AddLastArg(CmdArgs, options::OPT_bind__at__load);
Daniel Dunbarc44d3132011-04-28 21:23:41 +00003263 if (DarwinTC.isTargetIPhoneOS())
Daniel Dunbar2b5f6812009-09-04 18:35:41 +00003264 Args.AddLastArg(CmdArgs, options::OPT_arch__errors__fatal);
Daniel Dunbarc1964212009-03-26 16:23:12 +00003265 Args.AddLastArg(CmdArgs, options::OPT_dead__strip);
3266 Args.AddLastArg(CmdArgs, options::OPT_no__dead__strip__inits__and__terms);
3267 Args.AddAllArgs(CmdArgs, options::OPT_dylib__file);
3268 Args.AddLastArg(CmdArgs, options::OPT_dynamic);
3269 Args.AddAllArgs(CmdArgs, options::OPT_exported__symbols__list);
3270 Args.AddLastArg(CmdArgs, options::OPT_flat__namespace);
Daniel Dunbar044a3902011-06-28 20:16:02 +00003271 Args.AddAllArgs(CmdArgs, options::OPT_force__load);
Daniel Dunbarc1964212009-03-26 16:23:12 +00003272 Args.AddAllArgs(CmdArgs, options::OPT_headerpad__max__install__names);
3273 Args.AddAllArgs(CmdArgs, options::OPT_image__base);
3274 Args.AddAllArgs(CmdArgs, options::OPT_init);
3275
Daniel Dunbarc44d3132011-04-28 21:23:41 +00003276 // Add the deployment target.
3277 unsigned TargetVersion[3];
3278 DarwinTC.getTargetVersion(TargetVersion);
Daniel Dunbar72ceb922011-04-30 04:22:58 +00003279
3280 // If we had an explicit -mios-simulator-version-min argument, honor that,
3281 // otherwise use the traditional deployment targets. We can't just check the
3282 // is-sim attribute because existing code follows this path, and the linker
3283 // may not handle the argument.
3284 //
3285 // FIXME: We may be able to remove this, once we can verify no one depends on
3286 // it.
3287 if (Args.hasArg(options::OPT_mios_simulator_version_min_EQ))
3288 CmdArgs.push_back("-ios_simulator_version_min");
3289 else if (DarwinTC.isTargetIPhoneOS())
3290 CmdArgs.push_back("-iphoneos_version_min");
3291 else
3292 CmdArgs.push_back("-macosx_version_min");
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003293 CmdArgs.push_back(Args.MakeArgString(Twine(TargetVersion[0]) + "." +
3294 Twine(TargetVersion[1]) + "." +
3295 Twine(TargetVersion[2])));
Daniel Dunbarc44d3132011-04-28 21:23:41 +00003296
Daniel Dunbarc1964212009-03-26 16:23:12 +00003297 Args.AddLastArg(CmdArgs, options::OPT_nomultidefs);
3298 Args.AddLastArg(CmdArgs, options::OPT_multi__module);
3299 Args.AddLastArg(CmdArgs, options::OPT_single__module);
3300 Args.AddAllArgs(CmdArgs, options::OPT_multiply__defined);
3301 Args.AddAllArgs(CmdArgs, options::OPT_multiply__defined__unused);
Daniel Dunbard067f7f2009-04-08 23:54:23 +00003302
Daniel Dunbaraf68a882010-07-13 23:31:40 +00003303 if (const Arg *A = Args.getLastArg(options::OPT_fpie, options::OPT_fPIE,
3304 options::OPT_fno_pie,
3305 options::OPT_fno_PIE)) {
3306 if (A->getOption().matches(options::OPT_fpie) ||
3307 A->getOption().matches(options::OPT_fPIE))
3308 CmdArgs.push_back("-pie");
3309 else
3310 CmdArgs.push_back("-no_pie");
3311 }
Daniel Dunbarc1964212009-03-26 16:23:12 +00003312
3313 Args.AddLastArg(CmdArgs, options::OPT_prebind);
3314 Args.AddLastArg(CmdArgs, options::OPT_noprebind);
3315 Args.AddLastArg(CmdArgs, options::OPT_nofixprebinding);
3316 Args.AddLastArg(CmdArgs, options::OPT_prebind__all__twolevel__modules);
3317 Args.AddLastArg(CmdArgs, options::OPT_read__only__relocs);
3318 Args.AddAllArgs(CmdArgs, options::OPT_sectcreate);
3319 Args.AddAllArgs(CmdArgs, options::OPT_sectorder);
3320 Args.AddAllArgs(CmdArgs, options::OPT_seg1addr);
3321 Args.AddAllArgs(CmdArgs, options::OPT_segprot);
3322 Args.AddAllArgs(CmdArgs, options::OPT_segaddr);
3323 Args.AddAllArgs(CmdArgs, options::OPT_segs__read__only__addr);
3324 Args.AddAllArgs(CmdArgs, options::OPT_segs__read__write__addr);
3325 Args.AddAllArgs(CmdArgs, options::OPT_seg__addr__table);
3326 Args.AddAllArgs(CmdArgs, options::OPT_seg__addr__table__filename);
3327 Args.AddAllArgs(CmdArgs, options::OPT_sub__library);
3328 Args.AddAllArgs(CmdArgs, options::OPT_sub__umbrella);
Daniel Dunbar2b5f6812009-09-04 18:35:41 +00003329
Daniel Dunbar84384642011-05-02 21:03:47 +00003330 // Give --sysroot= preference, over the Apple specific behavior to also use
3331 // --isysroot as the syslibroot.
3332 if (const Arg *A = Args.getLastArg(options::OPT__sysroot_EQ)) {
3333 CmdArgs.push_back("-syslibroot");
3334 CmdArgs.push_back(A->getValue(Args));
3335 } else if (const Arg *A = Args.getLastArg(options::OPT_isysroot)) {
3336 CmdArgs.push_back("-syslibroot");
3337 CmdArgs.push_back(A->getValue(Args));
3338 } else if (getDarwinToolChain().isTargetIPhoneOS()) {
3339 CmdArgs.push_back("-syslibroot");
3340 CmdArgs.push_back("/Developer/SDKs/Extra");
Daniel Dunbar2b5f6812009-09-04 18:35:41 +00003341 }
3342
Daniel Dunbarc1964212009-03-26 16:23:12 +00003343 Args.AddLastArg(CmdArgs, options::OPT_twolevel__namespace);
3344 Args.AddLastArg(CmdArgs, options::OPT_twolevel__namespace__hints);
3345 Args.AddAllArgs(CmdArgs, options::OPT_umbrella);
3346 Args.AddAllArgs(CmdArgs, options::OPT_undefined);
3347 Args.AddAllArgs(CmdArgs, options::OPT_unexported__symbols__list);
Daniel Dunbar2b5f6812009-09-04 18:35:41 +00003348 Args.AddAllArgs(CmdArgs, options::OPT_weak__reference__mismatches);
Daniel Dunbarc1964212009-03-26 16:23:12 +00003349 Args.AddLastArg(CmdArgs, options::OPT_X_Flag);
3350 Args.AddAllArgs(CmdArgs, options::OPT_y);
3351 Args.AddLastArg(CmdArgs, options::OPT_w);
3352 Args.AddAllArgs(CmdArgs, options::OPT_pagezero__size);
3353 Args.AddAllArgs(CmdArgs, options::OPT_segs__read__);
3354 Args.AddLastArg(CmdArgs, options::OPT_seglinkedit);
3355 Args.AddLastArg(CmdArgs, options::OPT_noseglinkedit);
3356 Args.AddAllArgs(CmdArgs, options::OPT_sectalign);
3357 Args.AddAllArgs(CmdArgs, options::OPT_sectobjectsymbols);
3358 Args.AddAllArgs(CmdArgs, options::OPT_segcreate);
3359 Args.AddLastArg(CmdArgs, options::OPT_whyload);
3360 Args.AddLastArg(CmdArgs, options::OPT_whatsloaded);
3361 Args.AddAllArgs(CmdArgs, options::OPT_dylinker__install__name);
3362 Args.AddLastArg(CmdArgs, options::OPT_dylinker);
3363 Args.AddLastArg(CmdArgs, options::OPT_Mach);
3364}
3365
3366void darwin::Link::ConstructJob(Compilation &C, const JobAction &JA,
Daniel Dunbar7fbaf532010-08-02 02:38:28 +00003367 const InputInfo &Output,
Daniel Dunbard067f7f2009-04-08 23:54:23 +00003368 const InputInfoList &Inputs,
3369 const ArgList &Args,
Daniel Dunbarc1964212009-03-26 16:23:12 +00003370 const char *LinkingOutput) const {
3371 assert(Output.getType() == types::TY_Image && "Invalid linker output type.");
Daniel Dunbarc09988d2009-09-08 16:39:16 +00003372
Daniel Dunbarc1964212009-03-26 16:23:12 +00003373 // The logic here is derived from gcc's behavior; most of which
3374 // comes from specs (starting with link_command). Consult gcc for
3375 // more information.
Daniel Dunbarc1964212009-03-26 16:23:12 +00003376 ArgStringList CmdArgs;
3377
3378 // I'm not sure why this particular decomposition exists in gcc, but
3379 // we follow suite for ease of comparison.
Daniel Dunbarccbc4522010-09-09 21:51:05 +00003380 AddLinkArgs(C, Args, CmdArgs);
Daniel Dunbarc1964212009-03-26 16:23:12 +00003381
Daniel Dunbarc1964212009-03-26 16:23:12 +00003382 Args.AddAllArgs(CmdArgs, options::OPT_d_Flag);
3383 Args.AddAllArgs(CmdArgs, options::OPT_s);
3384 Args.AddAllArgs(CmdArgs, options::OPT_t);
3385 Args.AddAllArgs(CmdArgs, options::OPT_Z_Flag);
3386 Args.AddAllArgs(CmdArgs, options::OPT_u_Group);
3387 Args.AddAllArgs(CmdArgs, options::OPT_A);
3388 Args.AddLastArg(CmdArgs, options::OPT_e);
3389 Args.AddAllArgs(CmdArgs, options::OPT_m_Separate);
3390 Args.AddAllArgs(CmdArgs, options::OPT_r);
3391
Daniel Dunbar767bbab2010-10-18 22:08:36 +00003392 // Forward -ObjC when either -ObjC or -ObjC++ is used, to force loading
3393 // members of static archive libraries which implement Objective-C classes or
3394 // categories.
3395 if (Args.hasArg(options::OPT_ObjC) || Args.hasArg(options::OPT_ObjCXX))
3396 CmdArgs.push_back("-ObjC");
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00003397
Daniel Dunbarc1964212009-03-26 16:23:12 +00003398 CmdArgs.push_back("-o");
3399 CmdArgs.push_back(Output.getFilename());
3400
Daniel Dunbarc1964212009-03-26 16:23:12 +00003401 if (!Args.hasArg(options::OPT_A) &&
3402 !Args.hasArg(options::OPT_nostdlib) &&
3403 !Args.hasArg(options::OPT_nostartfiles)) {
3404 // Derived from startfile spec.
3405 if (Args.hasArg(options::OPT_dynamiclib)) {
Daniel Dunbard067f7f2009-04-08 23:54:23 +00003406 // Derived from darwin_dylib1 spec.
Daniel Dunbar16d97092011-04-01 21:02:42 +00003407 if (getDarwinToolChain().isTargetIOSSimulator()) {
3408 // The simulator doesn't have a versioned crt1 file.
3409 CmdArgs.push_back("-ldylib1.o");
3410 } else if (getDarwinToolChain().isTargetIPhoneOS()) {
Daniel Dunbar83608032010-01-27 00:56:56 +00003411 if (getDarwinToolChain().isIPhoneOSVersionLT(3, 1))
3412 CmdArgs.push_back("-ldylib1.o");
3413 } else {
Daniel Dunbar6d23b2f2010-01-27 00:57:03 +00003414 if (getDarwinToolChain().isMacosxVersionLT(10, 5))
Daniel Dunbar83608032010-01-27 00:56:56 +00003415 CmdArgs.push_back("-ldylib1.o");
Daniel Dunbar6d23b2f2010-01-27 00:57:03 +00003416 else if (getDarwinToolChain().isMacosxVersionLT(10, 6))
Daniel Dunbar83608032010-01-27 00:56:56 +00003417 CmdArgs.push_back("-ldylib1.10.5.o");
3418 }
Daniel Dunbarc1964212009-03-26 16:23:12 +00003419 } else {
3420 if (Args.hasArg(options::OPT_bundle)) {
Daniel Dunbarae8bca02009-04-01 03:17:40 +00003421 if (!Args.hasArg(options::OPT_static)) {
3422 // Derived from darwin_bundle1 spec.
Daniel Dunbar16d97092011-04-01 21:02:42 +00003423 if (getDarwinToolChain().isTargetIOSSimulator()) {
3424 // The simulator doesn't have a versioned crt1 file.
3425 CmdArgs.push_back("-lbundle1.o");
3426 } else if (getDarwinToolChain().isTargetIPhoneOS()) {
Daniel Dunbar83608032010-01-27 00:56:56 +00003427 if (getDarwinToolChain().isIPhoneOSVersionLT(3, 1))
3428 CmdArgs.push_back("-lbundle1.o");
3429 } else {
Daniel Dunbar6d23b2f2010-01-27 00:57:03 +00003430 if (getDarwinToolChain().isMacosxVersionLT(10, 6))
Daniel Dunbar83608032010-01-27 00:56:56 +00003431 CmdArgs.push_back("-lbundle1.o");
3432 }
Daniel Dunbarae8bca02009-04-01 03:17:40 +00003433 }
Daniel Dunbarc1964212009-03-26 16:23:12 +00003434 } else {
Daniel Dunbar733b0f82011-03-01 18:49:30 +00003435 if (Args.hasArg(options::OPT_pg) &&
3436 getToolChain().SupportsProfiling()) {
Daniel Dunbarc1964212009-03-26 16:23:12 +00003437 if (Args.hasArg(options::OPT_static) ||
3438 Args.hasArg(options::OPT_object) ||
3439 Args.hasArg(options::OPT_preload)) {
3440 CmdArgs.push_back("-lgcrt0.o");
3441 } else {
3442 CmdArgs.push_back("-lgcrt1.o");
Daniel Dunbard067f7f2009-04-08 23:54:23 +00003443
Daniel Dunbarc1964212009-03-26 16:23:12 +00003444 // darwin_crt2 spec is empty.
Daniel Dunbard067f7f2009-04-08 23:54:23 +00003445 }
Daniel Dunbarc1964212009-03-26 16:23:12 +00003446 } else {
3447 if (Args.hasArg(options::OPT_static) ||
3448 Args.hasArg(options::OPT_object) ||
3449 Args.hasArg(options::OPT_preload)) {
3450 CmdArgs.push_back("-lcrt0.o");
3451 } else {
3452 // Derived from darwin_crt1 spec.
Daniel Dunbarebc34df2011-03-31 17:12:33 +00003453 if (getDarwinToolChain().isTargetIOSSimulator()) {
3454 // The simulator doesn't have a versioned crt1 file.
3455 CmdArgs.push_back("-lcrt1.o");
3456 } else if (getDarwinToolChain().isTargetIPhoneOS()) {
Daniel Dunbar83608032010-01-27 00:56:56 +00003457 if (getDarwinToolChain().isIPhoneOSVersionLT(3, 1))
3458 CmdArgs.push_back("-lcrt1.o");
3459 else
3460 CmdArgs.push_back("-lcrt1.3.1.o");
Daniel Dunbar6d23b2f2010-01-27 00:57:03 +00003461 } else {
3462 if (getDarwinToolChain().isMacosxVersionLT(10, 5))
3463 CmdArgs.push_back("-lcrt1.o");
3464 else if (getDarwinToolChain().isMacosxVersionLT(10, 6))
3465 CmdArgs.push_back("-lcrt1.10.5.o");
3466 else
3467 CmdArgs.push_back("-lcrt1.10.6.o");
Daniel Dunbard067f7f2009-04-08 23:54:23 +00003468
Daniel Dunbar6d23b2f2010-01-27 00:57:03 +00003469 // darwin_crt2 spec is empty.
3470 }
Daniel Dunbarc1964212009-03-26 16:23:12 +00003471 }
3472 }
3473 }
3474 }
3475
Daniel Dunbar6d23b2f2010-01-27 00:57:03 +00003476 if (!getDarwinToolChain().isTargetIPhoneOS() &&
3477 Args.hasArg(options::OPT_shared_libgcc) &&
3478 getDarwinToolChain().isMacosxVersionLT(10, 5)) {
Daniel Dunbar1c28f1e2009-09-09 22:32:48 +00003479 const char *Str =
Daniel Dunbar9c3ed5f2010-07-14 18:46:23 +00003480 Args.MakeArgString(getToolChain().GetFilePath("crt3.o"));
Daniel Dunbar1c28f1e2009-09-09 22:32:48 +00003481 CmdArgs.push_back(Str);
Daniel Dunbarc1964212009-03-26 16:23:12 +00003482 }
3483 }
3484
3485 Args.AddAllArgs(CmdArgs, options::OPT_L);
Daniel Dunbard067f7f2009-04-08 23:54:23 +00003486
Daniel Dunbarc1964212009-03-26 16:23:12 +00003487 if (Args.hasArg(options::OPT_fopenmp))
3488 // This is more complicated in gcc...
3489 CmdArgs.push_back("-lgomp");
3490
Daniel Dunbar4c30b892009-09-18 08:14:36 +00003491 getDarwinToolChain().AddLinkSearchPathArgs(Args, CmdArgs);
Daniel Dunbard067f7f2009-04-08 23:54:23 +00003492
John McCall31168b02011-06-15 23:02:42 +00003493 // In ARC, if we don't have runtime support, link in the runtime
3494 // stubs. We have to do this *before* adding any of the normal
3495 // linker inputs so that its initializer gets run first.
John McCall24fc0de2011-07-06 00:26:06 +00003496 if (isObjCAutoRefCount(Args)) {
3497 ObjCRuntime runtime;
3498 getDarwinToolChain().configureObjCRuntime(runtime);
3499 if (!runtime.HasARC)
3500 getDarwinToolChain().AddLinkARCArgs(Args, CmdArgs);
3501 }
John McCall31168b02011-06-15 23:02:42 +00003502
Daniel Dunbar54423b22010-09-17 00:24:54 +00003503 AddLinkerInputs(getToolChain(), Inputs, Args, CmdArgs);
Daniel Dunbarc1964212009-03-26 16:23:12 +00003504
3505 if (LinkingOutput) {
3506 CmdArgs.push_back("-arch_multiple");
3507 CmdArgs.push_back("-final_output");
3508 CmdArgs.push_back(LinkingOutput);
3509 }
3510
Daniel Dunbarc1964212009-03-26 16:23:12 +00003511 if (Args.hasArg(options::OPT_fnested_functions))
3512 CmdArgs.push_back("-allow_stack_execute");
Daniel Dunbard067f7f2009-04-08 23:54:23 +00003513
Daniel Dunbarc1964212009-03-26 16:23:12 +00003514 if (!Args.hasArg(options::OPT_nostdlib) &&
3515 !Args.hasArg(options::OPT_nodefaultlibs)) {
Daniel Dunbar083edf72009-12-21 18:54:17 +00003516 if (getToolChain().getDriver().CCCIsCXX)
Daniel Dunbar3f7796f2010-09-17 01:20:05 +00003517 getToolChain().AddCXXStdlibLibArgs(Args, CmdArgs);
Daniel Dunbarad0f62b2009-04-08 06:06:21 +00003518
Daniel Dunbarc1964212009-03-26 16:23:12 +00003519 // link_ssp spec is empty.
3520
Daniel Dunbar26d482a2009-09-18 08:15:03 +00003521 // Let the tool chain choose which runtime library to link.
3522 getDarwinToolChain().AddLinkRuntimeLibArgs(Args, CmdArgs);
Daniel Dunbarc1964212009-03-26 16:23:12 +00003523 }
3524
3525 if (!Args.hasArg(options::OPT_A) &&
3526 !Args.hasArg(options::OPT_nostdlib) &&
3527 !Args.hasArg(options::OPT_nostartfiles)) {
3528 // endfile_spec is empty.
3529 }
3530
Bill Wendling08760582011-06-27 19:15:03 +00003531 addProfileRT(getToolChain(), Args, CmdArgs, getToolChain().getTriple());
Nick Lewycky82fe5f42011-05-24 21:54:59 +00003532
Daniel Dunbarc1964212009-03-26 16:23:12 +00003533 Args.AddAllArgs(CmdArgs, options::OPT_T_Group);
3534 Args.AddAllArgs(CmdArgs, options::OPT_F);
3535
Daniel Dunbard067f7f2009-04-08 23:54:23 +00003536 const char *Exec =
Daniel Dunbar9c3ed5f2010-07-14 18:46:23 +00003537 Args.MakeArgString(getToolChain().GetProgramPath("ld"));
Daniel Dunbar7fbaf532010-08-02 02:38:28 +00003538 C.addCommand(new Command(JA, *this, Exec, CmdArgs));
Daniel Dunbarc1964212009-03-26 16:23:12 +00003539}
3540
Daniel Dunbar64ed5e32009-03-20 00:52:38 +00003541void darwin::Lipo::ConstructJob(Compilation &C, const JobAction &JA,
Daniel Dunbar7fbaf532010-08-02 02:38:28 +00003542 const InputInfo &Output,
Daniel Dunbard067f7f2009-04-08 23:54:23 +00003543 const InputInfoList &Inputs,
3544 const ArgList &Args,
Daniel Dunbar64ed5e32009-03-20 00:52:38 +00003545 const char *LinkingOutput) const {
3546 ArgStringList CmdArgs;
3547
3548 CmdArgs.push_back("-create");
3549 assert(Output.isFilename() && "Unexpected lipo output.");
Daniel Dunbar06686ab2009-03-24 00:24:37 +00003550
3551 CmdArgs.push_back("-output");
Daniel Dunbar64ed5e32009-03-20 00:52:38 +00003552 CmdArgs.push_back(Output.getFilename());
Daniel Dunbar06686ab2009-03-24 00:24:37 +00003553
Daniel Dunbar64ed5e32009-03-20 00:52:38 +00003554 for (InputInfoList::const_iterator
3555 it = Inputs.begin(), ie = Inputs.end(); it != ie; ++it) {
3556 const InputInfo &II = *it;
3557 assert(II.isFilename() && "Unexpected lipo input.");
3558 CmdArgs.push_back(II.getFilename());
3559 }
Daniel Dunbard067f7f2009-04-08 23:54:23 +00003560 const char *Exec =
Daniel Dunbar9c3ed5f2010-07-14 18:46:23 +00003561 Args.MakeArgString(getToolChain().GetProgramPath("lipo"));
Daniel Dunbar7fbaf532010-08-02 02:38:28 +00003562 C.addCommand(new Command(JA, *this, Exec, CmdArgs));
Daniel Dunbar64ed5e32009-03-20 00:52:38 +00003563}
Daniel Dunbar8eb473c2009-03-31 17:45:15 +00003564
Daniel Dunbar88299622010-06-04 18:28:36 +00003565void darwin::Dsymutil::ConstructJob(Compilation &C, const JobAction &JA,
Daniel Dunbar7fbaf532010-08-02 02:38:28 +00003566 const InputInfo &Output,
Daniel Dunbar88299622010-06-04 18:28:36 +00003567 const InputInfoList &Inputs,
3568 const ArgList &Args,
3569 const char *LinkingOutput) const {
3570 ArgStringList CmdArgs;
3571
Daniel Dunbareb86b042011-05-09 17:23:16 +00003572 CmdArgs.push_back("-o");
3573 CmdArgs.push_back(Output.getFilename());
3574
Daniel Dunbar88299622010-06-04 18:28:36 +00003575 assert(Inputs.size() == 1 && "Unable to handle multiple inputs.");
3576 const InputInfo &Input = Inputs[0];
3577 assert(Input.isFilename() && "Unexpected dsymutil input.");
3578 CmdArgs.push_back(Input.getFilename());
3579
Daniel Dunbar88299622010-06-04 18:28:36 +00003580 const char *Exec =
Daniel Dunbar9c3ed5f2010-07-14 18:46:23 +00003581 Args.MakeArgString(getToolChain().GetProgramPath("dsymutil"));
Daniel Dunbar7fbaf532010-08-02 02:38:28 +00003582 C.addCommand(new Command(JA, *this, Exec, CmdArgs));
Daniel Dunbar88299622010-06-04 18:28:36 +00003583}
3584
Eric Christopher551ef452011-08-23 17:56:55 +00003585void darwin::VerifyDebug::ConstructJob(Compilation &C, const JobAction &JA,
3586 const InputInfo &Output,
3587 const InputInfoList &Inputs,
3588 const ArgList &Args,
3589 const char *LinkingOutput) const {
3590 ArgStringList CmdArgs;
3591 CmdArgs.push_back("--verify");
3592
3593 assert(Inputs.size() == 1 && "Unable to handle multiple inputs.");
3594 const InputInfo &Input = Inputs[0];
3595 assert(Input.isFilename() && "Unexpected verify input");
3596
3597 // Grabbing the output of the earlier dsymutil run.
3598 CmdArgs.push_back(Input.getFilename());
3599
3600 const char *Exec =
3601 Args.MakeArgString(getToolChain().GetProgramPath("dwarfdump"));
3602 C.addCommand(new Command(JA, *this, Exec, CmdArgs));
3603}
3604
Edward O'Callaghan856e4ff2009-08-22 01:06:46 +00003605void auroraux::Assemble::ConstructJob(Compilation &C, const JobAction &JA,
Daniel Dunbar7fbaf532010-08-02 02:38:28 +00003606 const InputInfo &Output,
Daniel Dunbar1c7577c2009-11-04 06:24:38 +00003607 const InputInfoList &Inputs,
3608 const ArgList &Args,
3609 const char *LinkingOutput) const {
Edward O'Callaghan856e4ff2009-08-22 01:06:46 +00003610 ArgStringList CmdArgs;
3611
3612 Args.AddAllArgValues(CmdArgs, options::OPT_Wa_COMMA,
3613 options::OPT_Xassembler);
3614
3615 CmdArgs.push_back("-o");
Daniel Dunbarb440f562010-08-02 02:38:21 +00003616 CmdArgs.push_back(Output.getFilename());
Edward O'Callaghan856e4ff2009-08-22 01:06:46 +00003617
3618 for (InputInfoList::const_iterator
3619 it = Inputs.begin(), ie = Inputs.end(); it != ie; ++it) {
3620 const InputInfo &II = *it;
Daniel Dunbarb440f562010-08-02 02:38:21 +00003621 CmdArgs.push_back(II.getFilename());
Edward O'Callaghan856e4ff2009-08-22 01:06:46 +00003622 }
3623
3624 const char *Exec =
Daniel Dunbar9c3ed5f2010-07-14 18:46:23 +00003625 Args.MakeArgString(getToolChain().GetProgramPath("gas"));
Daniel Dunbar7fbaf532010-08-02 02:38:28 +00003626 C.addCommand(new Command(JA, *this, Exec, CmdArgs));
Edward O'Callaghan856e4ff2009-08-22 01:06:46 +00003627}
3628
3629void auroraux::Link::ConstructJob(Compilation &C, const JobAction &JA,
Daniel Dunbar7fbaf532010-08-02 02:38:28 +00003630 const InputInfo &Output,
Daniel Dunbar1c7577c2009-11-04 06:24:38 +00003631 const InputInfoList &Inputs,
3632 const ArgList &Args,
3633 const char *LinkingOutput) const {
Edward O'Callaghan856e4ff2009-08-22 01:06:46 +00003634 ArgStringList CmdArgs;
3635
3636 if ((!Args.hasArg(options::OPT_nostdlib)) &&
Daniel Dunbar1c7577c2009-11-04 06:24:38 +00003637 (!Args.hasArg(options::OPT_shared))) {
Edward O'Callaghan856e4ff2009-08-22 01:06:46 +00003638 CmdArgs.push_back("-e");
Edward O'Callaghand8712d92009-10-15 07:44:07 +00003639 CmdArgs.push_back("_start");
Edward O'Callaghan856e4ff2009-08-22 01:06:46 +00003640 }
3641
3642 if (Args.hasArg(options::OPT_static)) {
3643 CmdArgs.push_back("-Bstatic");
Edward O'Callaghand8712d92009-10-15 07:44:07 +00003644 CmdArgs.push_back("-dn");
Edward O'Callaghan856e4ff2009-08-22 01:06:46 +00003645 } else {
Edward O'Callaghand8712d92009-10-15 07:44:07 +00003646// CmdArgs.push_back("--eh-frame-hdr");
Edward O'Callaghan856e4ff2009-08-22 01:06:46 +00003647 CmdArgs.push_back("-Bdynamic");
3648 if (Args.hasArg(options::OPT_shared)) {
3649 CmdArgs.push_back("-shared");
3650 } else {
Edward O'Callaghan7d3c2752009-10-16 19:44:18 +00003651 CmdArgs.push_back("--dynamic-linker");
Edward O'Callaghan856e4ff2009-08-22 01:06:46 +00003652 CmdArgs.push_back("/lib/ld.so.1"); // 64Bit Path /lib/amd64/ld.so.1
3653 }
3654 }
3655
Daniel Dunbarb440f562010-08-02 02:38:21 +00003656 if (Output.isFilename()) {
Edward O'Callaghan856e4ff2009-08-22 01:06:46 +00003657 CmdArgs.push_back("-o");
3658 CmdArgs.push_back(Output.getFilename());
3659 } else {
3660 assert(Output.isNothing() && "Invalid output.");
3661 }
3662
3663 if (!Args.hasArg(options::OPT_nostdlib) &&
3664 !Args.hasArg(options::OPT_nostartfiles)) {
3665 if (!Args.hasArg(options::OPT_shared)) {
Chris Lattner3e2ee142010-07-07 16:01:42 +00003666 CmdArgs.push_back(Args.MakeArgString(
Daniel Dunbar9c3ed5f2010-07-14 18:46:23 +00003667 getToolChain().GetFilePath("crt1.o")));
Chris Lattner3e2ee142010-07-07 16:01:42 +00003668 CmdArgs.push_back(Args.MakeArgString(
Daniel Dunbar9c3ed5f2010-07-14 18:46:23 +00003669 getToolChain().GetFilePath("crti.o")));
Chris Lattner3e2ee142010-07-07 16:01:42 +00003670 CmdArgs.push_back(Args.MakeArgString(
Daniel Dunbar9c3ed5f2010-07-14 18:46:23 +00003671 getToolChain().GetFilePath("crtbegin.o")));
Edward O'Callaghan856e4ff2009-08-22 01:06:46 +00003672 } else {
Chris Lattner3e2ee142010-07-07 16:01:42 +00003673 CmdArgs.push_back(Args.MakeArgString(
Daniel Dunbar9c3ed5f2010-07-14 18:46:23 +00003674 getToolChain().GetFilePath("crti.o")));
Edward O'Callaghan856e4ff2009-08-22 01:06:46 +00003675 }
Chris Lattner3e2ee142010-07-07 16:01:42 +00003676 CmdArgs.push_back(Args.MakeArgString(
Daniel Dunbar9c3ed5f2010-07-14 18:46:23 +00003677 getToolChain().GetFilePath("crtn.o")));
Edward O'Callaghan856e4ff2009-08-22 01:06:46 +00003678 }
3679
Daniel Dunbar1c7577c2009-11-04 06:24:38 +00003680 CmdArgs.push_back(Args.MakeArgString("-L/opt/gcc4/lib/gcc/"
3681 + getToolChain().getTripleString()
Daniel Dunbarb0b18612009-10-29 02:24:37 +00003682 + "/4.2.4"));
Edward O'Callaghan856e4ff2009-08-22 01:06:46 +00003683
3684 Args.AddAllArgs(CmdArgs, options::OPT_L);
3685 Args.AddAllArgs(CmdArgs, options::OPT_T_Group);
3686 Args.AddAllArgs(CmdArgs, options::OPT_e);
3687
Daniel Dunbar54423b22010-09-17 00:24:54 +00003688 AddLinkerInputs(getToolChain(), Inputs, Args, CmdArgs);
Edward O'Callaghan856e4ff2009-08-22 01:06:46 +00003689
3690 if (!Args.hasArg(options::OPT_nostdlib) &&
3691 !Args.hasArg(options::OPT_nodefaultlibs)) {
3692 // FIXME: For some reason GCC passes -lgcc before adding
3693 // the default system libraries. Just mimic this for now.
3694 CmdArgs.push_back("-lgcc");
3695
3696 if (Args.hasArg(options::OPT_pthread))
3697 CmdArgs.push_back("-pthread");
3698 if (!Args.hasArg(options::OPT_shared))
3699 CmdArgs.push_back("-lc");
3700 CmdArgs.push_back("-lgcc");
3701 }
3702
3703 if (!Args.hasArg(options::OPT_nostdlib) &&
3704 !Args.hasArg(options::OPT_nostartfiles)) {
3705 if (!Args.hasArg(options::OPT_shared))
Chris Lattner3e2ee142010-07-07 16:01:42 +00003706 CmdArgs.push_back(Args.MakeArgString(
Daniel Dunbar9c3ed5f2010-07-14 18:46:23 +00003707 getToolChain().GetFilePath("crtend.o")));
Edward O'Callaghan856e4ff2009-08-22 01:06:46 +00003708 }
3709
Bill Wendling08760582011-06-27 19:15:03 +00003710 addProfileRT(getToolChain(), Args, CmdArgs, getToolChain().getTriple());
Nick Lewycky82fe5f42011-05-24 21:54:59 +00003711
Edward O'Callaghan856e4ff2009-08-22 01:06:46 +00003712 const char *Exec =
Daniel Dunbar9c3ed5f2010-07-14 18:46:23 +00003713 Args.MakeArgString(getToolChain().GetProgramPath("ld"));
Daniel Dunbar7fbaf532010-08-02 02:38:28 +00003714 C.addCommand(new Command(JA, *this, Exec, CmdArgs));
Edward O'Callaghan856e4ff2009-08-22 01:06:46 +00003715}
3716
Daniel Dunbar10de9e62009-06-29 20:52:51 +00003717void openbsd::Assemble::ConstructJob(Compilation &C, const JobAction &JA,
Daniel Dunbar7fbaf532010-08-02 02:38:28 +00003718 const InputInfo &Output,
Daniel Dunbar10de9e62009-06-29 20:52:51 +00003719 const InputInfoList &Inputs,
3720 const ArgList &Args,
Mike Stump11289f42009-09-09 15:08:12 +00003721 const char *LinkingOutput) const {
Daniel Dunbar10de9e62009-06-29 20:52:51 +00003722 ArgStringList CmdArgs;
3723
3724 Args.AddAllArgValues(CmdArgs, options::OPT_Wa_COMMA,
3725 options::OPT_Xassembler);
3726
3727 CmdArgs.push_back("-o");
Daniel Dunbarb440f562010-08-02 02:38:21 +00003728 CmdArgs.push_back(Output.getFilename());
Daniel Dunbar10de9e62009-06-29 20:52:51 +00003729
3730 for (InputInfoList::const_iterator
3731 it = Inputs.begin(), ie = Inputs.end(); it != ie; ++it) {
3732 const InputInfo &II = *it;
Daniel Dunbarb440f562010-08-02 02:38:21 +00003733 CmdArgs.push_back(II.getFilename());
Daniel Dunbar10de9e62009-06-29 20:52:51 +00003734 }
3735
3736 const char *Exec =
Daniel Dunbar9c3ed5f2010-07-14 18:46:23 +00003737 Args.MakeArgString(getToolChain().GetProgramPath("as"));
Daniel Dunbar7fbaf532010-08-02 02:38:28 +00003738 C.addCommand(new Command(JA, *this, Exec, CmdArgs));
Daniel Dunbar10de9e62009-06-29 20:52:51 +00003739}
3740
3741void openbsd::Link::ConstructJob(Compilation &C, const JobAction &JA,
Daniel Dunbar7fbaf532010-08-02 02:38:28 +00003742 const InputInfo &Output,
Daniel Dunbar10de9e62009-06-29 20:52:51 +00003743 const InputInfoList &Inputs,
3744 const ArgList &Args,
3745 const char *LinkingOutput) const {
Daniel Dunbar083edf72009-12-21 18:54:17 +00003746 const Driver &D = getToolChain().getDriver();
Daniel Dunbar10de9e62009-06-29 20:52:51 +00003747 ArgStringList CmdArgs;
3748
Daniel Dunbara8888ac2009-08-03 01:28:59 +00003749 if ((!Args.hasArg(options::OPT_nostdlib)) &&
Daniel Dunbar1c7577c2009-11-04 06:24:38 +00003750 (!Args.hasArg(options::OPT_shared))) {
Daniel Dunbara8888ac2009-08-03 01:28:59 +00003751 CmdArgs.push_back("-e");
3752 CmdArgs.push_back("__start");
3753 }
3754
Daniel Dunbar10de9e62009-06-29 20:52:51 +00003755 if (Args.hasArg(options::OPT_static)) {
3756 CmdArgs.push_back("-Bstatic");
3757 } else {
Rafael Espindola7ba97af2010-11-11 02:17:51 +00003758 if (Args.hasArg(options::OPT_rdynamic))
3759 CmdArgs.push_back("-export-dynamic");
Daniel Dunbar10de9e62009-06-29 20:52:51 +00003760 CmdArgs.push_back("--eh-frame-hdr");
Daniel Dunbara8888ac2009-08-03 01:28:59 +00003761 CmdArgs.push_back("-Bdynamic");
Daniel Dunbar10de9e62009-06-29 20:52:51 +00003762 if (Args.hasArg(options::OPT_shared)) {
Daniel Dunbara8888ac2009-08-03 01:28:59 +00003763 CmdArgs.push_back("-shared");
Daniel Dunbar10de9e62009-06-29 20:52:51 +00003764 } else {
3765 CmdArgs.push_back("-dynamic-linker");
3766 CmdArgs.push_back("/usr/libexec/ld.so");
3767 }
3768 }
3769
Daniel Dunbarb440f562010-08-02 02:38:21 +00003770 if (Output.isFilename()) {
Daniel Dunbar10de9e62009-06-29 20:52:51 +00003771 CmdArgs.push_back("-o");
3772 CmdArgs.push_back(Output.getFilename());
3773 } else {
3774 assert(Output.isNothing() && "Invalid output.");
3775 }
3776
3777 if (!Args.hasArg(options::OPT_nostdlib) &&
3778 !Args.hasArg(options::OPT_nostartfiles)) {
3779 if (!Args.hasArg(options::OPT_shared)) {
Chris Lattner3e2ee142010-07-07 16:01:42 +00003780 CmdArgs.push_back(Args.MakeArgString(
Daniel Dunbar9c3ed5f2010-07-14 18:46:23 +00003781 getToolChain().GetFilePath("crt0.o")));
Chris Lattner3e2ee142010-07-07 16:01:42 +00003782 CmdArgs.push_back(Args.MakeArgString(
Daniel Dunbar9c3ed5f2010-07-14 18:46:23 +00003783 getToolChain().GetFilePath("crtbegin.o")));
Daniel Dunbar10de9e62009-06-29 20:52:51 +00003784 } else {
Chris Lattner3e2ee142010-07-07 16:01:42 +00003785 CmdArgs.push_back(Args.MakeArgString(
Daniel Dunbar9c3ed5f2010-07-14 18:46:23 +00003786 getToolChain().GetFilePath("crtbeginS.o")));
Daniel Dunbar10de9e62009-06-29 20:52:51 +00003787 }
3788 }
3789
Edward O'Callaghan5c521462009-10-28 15:13:08 +00003790 std::string Triple = getToolChain().getTripleString();
3791 if (Triple.substr(0, 6) == "x86_64")
Daniel Dunbar1c7577c2009-11-04 06:24:38 +00003792 Triple.replace(0, 6, "amd64");
Daniel Dunbarb0b18612009-10-29 02:24:37 +00003793 CmdArgs.push_back(Args.MakeArgString("-L/usr/lib/gcc-lib/" + Triple +
Daniel Dunbarea3813f2010-08-01 23:13:54 +00003794 "/4.2.1"));
Daniel Dunbara8888ac2009-08-03 01:28:59 +00003795
Daniel Dunbar10de9e62009-06-29 20:52:51 +00003796 Args.AddAllArgs(CmdArgs, options::OPT_L);
3797 Args.AddAllArgs(CmdArgs, options::OPT_T_Group);
3798 Args.AddAllArgs(CmdArgs, options::OPT_e);
3799
Daniel Dunbar54423b22010-09-17 00:24:54 +00003800 AddLinkerInputs(getToolChain(), Inputs, Args, CmdArgs);
Daniel Dunbar10de9e62009-06-29 20:52:51 +00003801
3802 if (!Args.hasArg(options::OPT_nostdlib) &&
3803 !Args.hasArg(options::OPT_nodefaultlibs)) {
Daniel Dunbarea3813f2010-08-01 23:13:54 +00003804 if (D.CCCIsCXX) {
Daniel Dunbar3f7796f2010-09-17 01:20:05 +00003805 getToolChain().AddCXXStdlibLibArgs(Args, CmdArgs);
Daniel Dunbarea3813f2010-08-01 23:13:54 +00003806 CmdArgs.push_back("-lm");
3807 }
3808
Daniel Dunbara8888ac2009-08-03 01:28:59 +00003809 // FIXME: For some reason GCC passes -lgcc before adding
3810 // the default system libraries. Just mimic this for now.
3811 CmdArgs.push_back("-lgcc");
Daniel Dunbar10de9e62009-06-29 20:52:51 +00003812
3813 if (Args.hasArg(options::OPT_pthread))
Chris Lattnerd0257f72011-02-21 18:36:51 +00003814 CmdArgs.push_back("-lpthread");
Daniel Dunbara8888ac2009-08-03 01:28:59 +00003815 if (!Args.hasArg(options::OPT_shared))
3816 CmdArgs.push_back("-lc");
3817 CmdArgs.push_back("-lgcc");
Daniel Dunbar10de9e62009-06-29 20:52:51 +00003818 }
3819
3820 if (!Args.hasArg(options::OPT_nostdlib) &&
3821 !Args.hasArg(options::OPT_nostartfiles)) {
3822 if (!Args.hasArg(options::OPT_shared))
Chris Lattner3e2ee142010-07-07 16:01:42 +00003823 CmdArgs.push_back(Args.MakeArgString(
Daniel Dunbar9c3ed5f2010-07-14 18:46:23 +00003824 getToolChain().GetFilePath("crtend.o")));
Daniel Dunbar10de9e62009-06-29 20:52:51 +00003825 else
Chris Lattner3e2ee142010-07-07 16:01:42 +00003826 CmdArgs.push_back(Args.MakeArgString(
Daniel Dunbar9c3ed5f2010-07-14 18:46:23 +00003827 getToolChain().GetFilePath("crtendS.o")));
Daniel Dunbar10de9e62009-06-29 20:52:51 +00003828 }
3829
3830 const char *Exec =
Daniel Dunbar9c3ed5f2010-07-14 18:46:23 +00003831 Args.MakeArgString(getToolChain().GetProgramPath("ld"));
Daniel Dunbar7fbaf532010-08-02 02:38:28 +00003832 C.addCommand(new Command(JA, *this, Exec, CmdArgs));
Daniel Dunbar10de9e62009-06-29 20:52:51 +00003833}
Ed Schoutene33194b2009-04-02 19:13:12 +00003834
Daniel Dunbar8eb473c2009-03-31 17:45:15 +00003835void freebsd::Assemble::ConstructJob(Compilation &C, const JobAction &JA,
Daniel Dunbar7fbaf532010-08-02 02:38:28 +00003836 const InputInfo &Output,
Daniel Dunbard067f7f2009-04-08 23:54:23 +00003837 const InputInfoList &Inputs,
3838 const ArgList &Args,
Mike Stump11289f42009-09-09 15:08:12 +00003839 const char *LinkingOutput) const {
Daniel Dunbar8eb473c2009-03-31 17:45:15 +00003840 ArgStringList CmdArgs;
3841
Daniel Dunbard854c8d2009-04-01 19:36:32 +00003842 // When building 32-bit code on FreeBSD/amd64, we have to explicitly
3843 // instruct as in the base system to assemble 32-bit code.
3844 if (getToolChain().getArchName() == "i386")
Daniel Dunbar8eb473c2009-03-31 17:45:15 +00003845 CmdArgs.push_back("--32");
3846
Roman Divacky00859c22011-06-04 07:37:31 +00003847 if (getToolChain().getArchName() == "powerpc")
3848 CmdArgs.push_back("-a32");
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00003849
Eric Christopher0b26a612010-03-02 02:41:08 +00003850 // Set byte order explicitly
3851 if (getToolChain().getArchName() == "mips")
3852 CmdArgs.push_back("-EB");
3853 else if (getToolChain().getArchName() == "mipsel")
3854 CmdArgs.push_back("-EL");
3855
Daniel Dunbar8eb473c2009-03-31 17:45:15 +00003856 Args.AddAllArgValues(CmdArgs, options::OPT_Wa_COMMA,
3857 options::OPT_Xassembler);
3858
3859 CmdArgs.push_back("-o");
Daniel Dunbarb440f562010-08-02 02:38:21 +00003860 CmdArgs.push_back(Output.getFilename());
Daniel Dunbar8eb473c2009-03-31 17:45:15 +00003861
3862 for (InputInfoList::const_iterator
3863 it = Inputs.begin(), ie = Inputs.end(); it != ie; ++it) {
3864 const InputInfo &II = *it;
Daniel Dunbarb440f562010-08-02 02:38:21 +00003865 CmdArgs.push_back(II.getFilename());
Daniel Dunbar8eb473c2009-03-31 17:45:15 +00003866 }
3867
Daniel Dunbard067f7f2009-04-08 23:54:23 +00003868 const char *Exec =
Daniel Dunbar9c3ed5f2010-07-14 18:46:23 +00003869 Args.MakeArgString(getToolChain().GetProgramPath("as"));
Daniel Dunbar7fbaf532010-08-02 02:38:28 +00003870 C.addCommand(new Command(JA, *this, Exec, CmdArgs));
Daniel Dunbar8eb473c2009-03-31 17:45:15 +00003871}
Daniel Dunbard854c8d2009-04-01 19:36:32 +00003872
3873void freebsd::Link::ConstructJob(Compilation &C, const JobAction &JA,
Daniel Dunbar7fbaf532010-08-02 02:38:28 +00003874 const InputInfo &Output,
Daniel Dunbard067f7f2009-04-08 23:54:23 +00003875 const InputInfoList &Inputs,
3876 const ArgList &Args,
Daniel Dunbare3e263f2009-05-02 20:14:53 +00003877 const char *LinkingOutput) const {
Daniel Dunbar083edf72009-12-21 18:54:17 +00003878 const Driver &D = getToolChain().getDriver();
Daniel Dunbard854c8d2009-04-01 19:36:32 +00003879 ArgStringList CmdArgs;
3880
Joerg Sonnenberger6165ab12011-03-21 13:51:29 +00003881 if (!D.SysRoot.empty())
3882 CmdArgs.push_back(Args.MakeArgString("--sysroot=" + D.SysRoot));
3883
Daniel Dunbard854c8d2009-04-01 19:36:32 +00003884 if (Args.hasArg(options::OPT_static)) {
3885 CmdArgs.push_back("-Bstatic");
3886 } else {
Rafael Espindola7ba97af2010-11-11 02:17:51 +00003887 if (Args.hasArg(options::OPT_rdynamic))
3888 CmdArgs.push_back("-export-dynamic");
Daniel Dunbard854c8d2009-04-01 19:36:32 +00003889 CmdArgs.push_back("--eh-frame-hdr");
3890 if (Args.hasArg(options::OPT_shared)) {
3891 CmdArgs.push_back("-Bshareable");
3892 } else {
3893 CmdArgs.push_back("-dynamic-linker");
3894 CmdArgs.push_back("/libexec/ld-elf.so.1");
3895 }
3896 }
3897
3898 // When building 32-bit code on FreeBSD/amd64, we have to explicitly
3899 // instruct ld in the base system to link 32-bit code.
3900 if (getToolChain().getArchName() == "i386") {
3901 CmdArgs.push_back("-m");
3902 CmdArgs.push_back("elf_i386_fbsd");
3903 }
3904
Roman Divacky5e300b82011-06-04 07:40:24 +00003905 if (getToolChain().getArchName() == "powerpc") {
3906 CmdArgs.push_back("-m");
3907 CmdArgs.push_back("elf32ppc");
3908 }
3909
Daniel Dunbarb440f562010-08-02 02:38:21 +00003910 if (Output.isFilename()) {
Daniel Dunbard854c8d2009-04-01 19:36:32 +00003911 CmdArgs.push_back("-o");
3912 CmdArgs.push_back(Output.getFilename());
3913 } else {
3914 assert(Output.isNothing() && "Invalid output.");
3915 }
3916
3917 if (!Args.hasArg(options::OPT_nostdlib) &&
3918 !Args.hasArg(options::OPT_nostartfiles)) {
3919 if (!Args.hasArg(options::OPT_shared)) {
Roman Divacky66f22762011-02-10 16:59:40 +00003920 if (Args.hasArg(options::OPT_pg))
3921 CmdArgs.push_back(Args.MakeArgString(
3922 getToolChain().GetFilePath("gcrt1.o")));
3923 else
3924 CmdArgs.push_back(Args.MakeArgString(
3925 getToolChain().GetFilePath("crt1.o")));
Chris Lattner3e2ee142010-07-07 16:01:42 +00003926 CmdArgs.push_back(Args.MakeArgString(
Daniel Dunbar9c3ed5f2010-07-14 18:46:23 +00003927 getToolChain().GetFilePath("crti.o")));
Chris Lattner3e2ee142010-07-07 16:01:42 +00003928 CmdArgs.push_back(Args.MakeArgString(
Daniel Dunbar9c3ed5f2010-07-14 18:46:23 +00003929 getToolChain().GetFilePath("crtbegin.o")));
Daniel Dunbard854c8d2009-04-01 19:36:32 +00003930 } else {
Chris Lattner3e2ee142010-07-07 16:01:42 +00003931 CmdArgs.push_back(Args.MakeArgString(
Daniel Dunbar9c3ed5f2010-07-14 18:46:23 +00003932 getToolChain().GetFilePath("crti.o")));
Chris Lattner3e2ee142010-07-07 16:01:42 +00003933 CmdArgs.push_back(Args.MakeArgString(
Daniel Dunbar9c3ed5f2010-07-14 18:46:23 +00003934 getToolChain().GetFilePath("crtbeginS.o")));
Daniel Dunbard854c8d2009-04-01 19:36:32 +00003935 }
3936 }
3937
3938 Args.AddAllArgs(CmdArgs, options::OPT_L);
Roman Divackyee8188a2011-03-01 17:53:14 +00003939 const ToolChain::path_list Paths = getToolChain().getFilePaths();
3940 for (ToolChain::path_list::const_iterator i = Paths.begin(), e = Paths.end();
3941 i != e; ++i)
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003942 CmdArgs.push_back(Args.MakeArgString(StringRef("-L") + *i));
Daniel Dunbard854c8d2009-04-01 19:36:32 +00003943 Args.AddAllArgs(CmdArgs, options::OPT_T_Group);
3944 Args.AddAllArgs(CmdArgs, options::OPT_e);
David Chisnall589a4942010-08-15 22:58:12 +00003945 Args.AddAllArgs(CmdArgs, options::OPT_s);
3946 Args.AddAllArgs(CmdArgs, options::OPT_t);
3947 Args.AddAllArgs(CmdArgs, options::OPT_Z_Flag);
3948 Args.AddAllArgs(CmdArgs, options::OPT_r);
Daniel Dunbard854c8d2009-04-01 19:36:32 +00003949
Daniel Dunbar54423b22010-09-17 00:24:54 +00003950 AddLinkerInputs(getToolChain(), Inputs, Args, CmdArgs);
Daniel Dunbard854c8d2009-04-01 19:36:32 +00003951
3952 if (!Args.hasArg(options::OPT_nostdlib) &&
3953 !Args.hasArg(options::OPT_nodefaultlibs)) {
Daniel Dunbar4b8ef282010-02-17 08:07:51 +00003954 if (D.CCCIsCXX) {
Daniel Dunbar3f7796f2010-09-17 01:20:05 +00003955 getToolChain().AddCXXStdlibLibArgs(Args, CmdArgs);
Roman Divacky66f22762011-02-10 16:59:40 +00003956 if (Args.hasArg(options::OPT_pg))
3957 CmdArgs.push_back("-lm_p");
3958 else
3959 CmdArgs.push_back("-lm");
Daniel Dunbar4b8ef282010-02-17 08:07:51 +00003960 }
Daniel Dunbard854c8d2009-04-01 19:36:32 +00003961 // FIXME: For some reason GCC passes -lgcc and -lgcc_s before adding
3962 // the default system libraries. Just mimic this for now.
Roman Divacky66f22762011-02-10 16:59:40 +00003963 if (Args.hasArg(options::OPT_pg))
3964 CmdArgs.push_back("-lgcc_p");
3965 else
3966 CmdArgs.push_back("-lgcc");
Daniel Dunbard854c8d2009-04-01 19:36:32 +00003967 if (Args.hasArg(options::OPT_static)) {
3968 CmdArgs.push_back("-lgcc_eh");
Roman Divacky66f22762011-02-10 16:59:40 +00003969 } else if (Args.hasArg(options::OPT_pg)) {
3970 CmdArgs.push_back("-lgcc_eh_p");
Daniel Dunbard854c8d2009-04-01 19:36:32 +00003971 } else {
3972 CmdArgs.push_back("--as-needed");
3973 CmdArgs.push_back("-lgcc_s");
3974 CmdArgs.push_back("--no-as-needed");
3975 }
3976
Matt Beaumont-Gay1fe49152011-02-10 20:35:01 +00003977 if (Args.hasArg(options::OPT_pthread)) {
Roman Divacky66f22762011-02-10 16:59:40 +00003978 if (Args.hasArg(options::OPT_pg))
3979 CmdArgs.push_back("-lpthread_p");
3980 else
3981 CmdArgs.push_back("-lpthread");
Matt Beaumont-Gay1fe49152011-02-10 20:35:01 +00003982 }
Daniel Dunbard854c8d2009-04-01 19:36:32 +00003983
Roman Divacky66f22762011-02-10 16:59:40 +00003984 if (Args.hasArg(options::OPT_pg)) {
3985 if (Args.hasArg(options::OPT_shared))
3986 CmdArgs.push_back("-lc");
3987 else
3988 CmdArgs.push_back("-lc_p");
3989 CmdArgs.push_back("-lgcc_p");
3990 } else {
3991 CmdArgs.push_back("-lc");
3992 CmdArgs.push_back("-lgcc");
3993 }
3994
Daniel Dunbard854c8d2009-04-01 19:36:32 +00003995 if (Args.hasArg(options::OPT_static)) {
3996 CmdArgs.push_back("-lgcc_eh");
Roman Divacky66f22762011-02-10 16:59:40 +00003997 } else if (Args.hasArg(options::OPT_pg)) {
3998 CmdArgs.push_back("-lgcc_eh_p");
Daniel Dunbard854c8d2009-04-01 19:36:32 +00003999 } else {
4000 CmdArgs.push_back("--as-needed");
4001 CmdArgs.push_back("-lgcc_s");
4002 CmdArgs.push_back("--no-as-needed");
4003 }
4004 }
4005
4006 if (!Args.hasArg(options::OPT_nostdlib) &&
4007 !Args.hasArg(options::OPT_nostartfiles)) {
4008 if (!Args.hasArg(options::OPT_shared))
Daniel Dunbar9c3ed5f2010-07-14 18:46:23 +00004009 CmdArgs.push_back(Args.MakeArgString(getToolChain().GetFilePath(
Chris Lattner3e2ee142010-07-07 16:01:42 +00004010 "crtend.o")));
Daniel Dunbard854c8d2009-04-01 19:36:32 +00004011 else
Daniel Dunbar9c3ed5f2010-07-14 18:46:23 +00004012 CmdArgs.push_back(Args.MakeArgString(getToolChain().GetFilePath(
Chris Lattner3e2ee142010-07-07 16:01:42 +00004013 "crtendS.o")));
Daniel Dunbar9c3ed5f2010-07-14 18:46:23 +00004014 CmdArgs.push_back(Args.MakeArgString(getToolChain().GetFilePath(
Chris Lattner3e2ee142010-07-07 16:01:42 +00004015 "crtn.o")));
Daniel Dunbard854c8d2009-04-01 19:36:32 +00004016 }
4017
Bill Wendling08760582011-06-27 19:15:03 +00004018 addProfileRT(getToolChain(), Args, CmdArgs, getToolChain().getTriple());
Nick Lewycky82fe5f42011-05-24 21:54:59 +00004019
Daniel Dunbard067f7f2009-04-08 23:54:23 +00004020 const char *Exec =
Daniel Dunbar9c3ed5f2010-07-14 18:46:23 +00004021 Args.MakeArgString(getToolChain().GetProgramPath("ld"));
Daniel Dunbar7fbaf532010-08-02 02:38:28 +00004022 C.addCommand(new Command(JA, *this, Exec, CmdArgs));
Daniel Dunbard854c8d2009-04-01 19:36:32 +00004023}
Daniel Dunbarcc912342009-05-02 18:28:39 +00004024
Benjamin Kramer24f1d3e2011-02-02 18:59:27 +00004025void netbsd::Assemble::ConstructJob(Compilation &C, const JobAction &JA,
4026 const InputInfo &Output,
4027 const InputInfoList &Inputs,
4028 const ArgList &Args,
4029 const char *LinkingOutput) const {
4030 ArgStringList CmdArgs;
4031
4032 // When building 32-bit code on NetBSD/amd64, we have to explicitly
4033 // instruct as in the base system to assemble 32-bit code.
Joerg Sonnenberger637603a2011-05-16 13:35:02 +00004034 if (ToolTriple.getArch() == llvm::Triple::x86_64 &&
4035 getToolChain().getArch() == llvm::Triple::x86)
Benjamin Kramer24f1d3e2011-02-02 18:59:27 +00004036 CmdArgs.push_back("--32");
4037
4038
4039 // Set byte order explicitly
4040 if (getToolChain().getArchName() == "mips")
4041 CmdArgs.push_back("-EB");
4042 else if (getToolChain().getArchName() == "mipsel")
4043 CmdArgs.push_back("-EL");
4044
4045 Args.AddAllArgValues(CmdArgs, options::OPT_Wa_COMMA,
4046 options::OPT_Xassembler);
4047
4048 CmdArgs.push_back("-o");
4049 CmdArgs.push_back(Output.getFilename());
4050
4051 for (InputInfoList::const_iterator
4052 it = Inputs.begin(), ie = Inputs.end(); it != ie; ++it) {
4053 const InputInfo &II = *it;
4054 CmdArgs.push_back(II.getFilename());
4055 }
4056
Joerg Sonnenbergerfcc3ec92011-03-21 14:01:40 +00004057 const char *Exec = Args.MakeArgString(FindTargetProgramPath(getToolChain(),
Joerg Sonnenberger637603a2011-05-16 13:35:02 +00004058 ToolTriple.getTriple(),
4059 "as"));
Benjamin Kramer24f1d3e2011-02-02 18:59:27 +00004060 C.addCommand(new Command(JA, *this, Exec, CmdArgs));
4061}
4062
4063void netbsd::Link::ConstructJob(Compilation &C, const JobAction &JA,
4064 const InputInfo &Output,
4065 const InputInfoList &Inputs,
4066 const ArgList &Args,
4067 const char *LinkingOutput) const {
4068 const Driver &D = getToolChain().getDriver();
4069 ArgStringList CmdArgs;
4070
Joerg Sonnenberger6165ab12011-03-21 13:51:29 +00004071 if (!D.SysRoot.empty())
4072 CmdArgs.push_back(Args.MakeArgString("--sysroot=" + D.SysRoot));
4073
Benjamin Kramer24f1d3e2011-02-02 18:59:27 +00004074 if (Args.hasArg(options::OPT_static)) {
4075 CmdArgs.push_back("-Bstatic");
4076 } else {
4077 if (Args.hasArg(options::OPT_rdynamic))
4078 CmdArgs.push_back("-export-dynamic");
4079 CmdArgs.push_back("--eh-frame-hdr");
4080 if (Args.hasArg(options::OPT_shared)) {
4081 CmdArgs.push_back("-Bshareable");
4082 } else {
4083 CmdArgs.push_back("-dynamic-linker");
4084 CmdArgs.push_back("/libexec/ld.elf_so");
4085 }
4086 }
4087
4088 // When building 32-bit code on NetBSD/amd64, we have to explicitly
4089 // instruct ld in the base system to link 32-bit code.
Joerg Sonnenberger637603a2011-05-16 13:35:02 +00004090 if (ToolTriple.getArch() == llvm::Triple::x86_64 &&
4091 getToolChain().getArch() == llvm::Triple::x86) {
Benjamin Kramer24f1d3e2011-02-02 18:59:27 +00004092 CmdArgs.push_back("-m");
4093 CmdArgs.push_back("elf_i386");
4094 }
4095
4096 if (Output.isFilename()) {
4097 CmdArgs.push_back("-o");
4098 CmdArgs.push_back(Output.getFilename());
4099 } else {
4100 assert(Output.isNothing() && "Invalid output.");
4101 }
4102
4103 if (!Args.hasArg(options::OPT_nostdlib) &&
4104 !Args.hasArg(options::OPT_nostartfiles)) {
4105 if (!Args.hasArg(options::OPT_shared)) {
4106 CmdArgs.push_back(Args.MakeArgString(
4107 getToolChain().GetFilePath("crt0.o")));
4108 CmdArgs.push_back(Args.MakeArgString(
4109 getToolChain().GetFilePath("crti.o")));
4110 CmdArgs.push_back(Args.MakeArgString(
4111 getToolChain().GetFilePath("crtbegin.o")));
4112 } else {
4113 CmdArgs.push_back(Args.MakeArgString(
4114 getToolChain().GetFilePath("crti.o")));
4115 CmdArgs.push_back(Args.MakeArgString(
4116 getToolChain().GetFilePath("crtbeginS.o")));
4117 }
4118 }
4119
4120 Args.AddAllArgs(CmdArgs, options::OPT_L);
4121 Args.AddAllArgs(CmdArgs, options::OPT_T_Group);
4122 Args.AddAllArgs(CmdArgs, options::OPT_e);
4123 Args.AddAllArgs(CmdArgs, options::OPT_s);
4124 Args.AddAllArgs(CmdArgs, options::OPT_t);
4125 Args.AddAllArgs(CmdArgs, options::OPT_Z_Flag);
4126 Args.AddAllArgs(CmdArgs, options::OPT_r);
4127
4128 AddLinkerInputs(getToolChain(), Inputs, Args, CmdArgs);
4129
4130 if (!Args.hasArg(options::OPT_nostdlib) &&
4131 !Args.hasArg(options::OPT_nodefaultlibs)) {
4132 if (D.CCCIsCXX) {
4133 getToolChain().AddCXXStdlibLibArgs(Args, CmdArgs);
4134 CmdArgs.push_back("-lm");
4135 }
4136 // FIXME: For some reason GCC passes -lgcc and -lgcc_s before adding
4137 // the default system libraries. Just mimic this for now.
Benjamin Kramer24f1d3e2011-02-02 18:59:27 +00004138 if (Args.hasArg(options::OPT_static)) {
4139 CmdArgs.push_back("-lgcc_eh");
4140 } else {
4141 CmdArgs.push_back("--as-needed");
4142 CmdArgs.push_back("-lgcc_s");
4143 CmdArgs.push_back("--no-as-needed");
4144 }
Joerg Sonnenberger87717772011-06-07 23:39:17 +00004145 CmdArgs.push_back("-lgcc");
Benjamin Kramer24f1d3e2011-02-02 18:59:27 +00004146
4147 if (Args.hasArg(options::OPT_pthread))
4148 CmdArgs.push_back("-lpthread");
4149 CmdArgs.push_back("-lc");
4150
4151 CmdArgs.push_back("-lgcc");
4152 if (Args.hasArg(options::OPT_static)) {
4153 CmdArgs.push_back("-lgcc_eh");
4154 } else {
4155 CmdArgs.push_back("--as-needed");
4156 CmdArgs.push_back("-lgcc_s");
4157 CmdArgs.push_back("--no-as-needed");
4158 }
4159 }
4160
4161 if (!Args.hasArg(options::OPT_nostdlib) &&
4162 !Args.hasArg(options::OPT_nostartfiles)) {
4163 if (!Args.hasArg(options::OPT_shared))
4164 CmdArgs.push_back(Args.MakeArgString(getToolChain().GetFilePath(
4165 "crtend.o")));
4166 else
4167 CmdArgs.push_back(Args.MakeArgString(getToolChain().GetFilePath(
4168 "crtendS.o")));
4169 CmdArgs.push_back(Args.MakeArgString(getToolChain().GetFilePath(
4170 "crtn.o")));
4171 }
4172
Bill Wendling08760582011-06-27 19:15:03 +00004173 addProfileRT(getToolChain(), Args, CmdArgs, getToolChain().getTriple());
Nick Lewycky82fe5f42011-05-24 21:54:59 +00004174
Joerg Sonnenbergerfcc3ec92011-03-21 14:01:40 +00004175 const char *Exec = Args.MakeArgString(FindTargetProgramPath(getToolChain(),
Joerg Sonnenberger637603a2011-05-16 13:35:02 +00004176 ToolTriple.getTriple(),
4177 "ld"));
Benjamin Kramer24f1d3e2011-02-02 18:59:27 +00004178 C.addCommand(new Command(JA, *this, Exec, CmdArgs));
4179}
4180
Rafael Espindola92b00932010-08-10 00:25:48 +00004181void linuxtools::Assemble::ConstructJob(Compilation &C, const JobAction &JA,
4182 const InputInfo &Output,
4183 const InputInfoList &Inputs,
4184 const ArgList &Args,
4185 const char *LinkingOutput) const {
4186 ArgStringList CmdArgs;
4187
4188 // Add --32/--64 to make sure we get the format we want.
4189 // This is incomplete
4190 if (getToolChain().getArch() == llvm::Triple::x86) {
4191 CmdArgs.push_back("--32");
4192 } else if (getToolChain().getArch() == llvm::Triple::x86_64) {
4193 CmdArgs.push_back("--64");
4194 } else if (getToolChain().getArch() == llvm::Triple::arm) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00004195 StringRef MArch = getToolChain().getArchName();
Rafael Espindola92b00932010-08-10 00:25:48 +00004196 if (MArch == "armv7" || MArch == "armv7a" || MArch == "armv7-a")
4197 CmdArgs.push_back("-mfpu=neon");
4198 }
4199
4200 Args.AddAllArgValues(CmdArgs, options::OPT_Wa_COMMA,
4201 options::OPT_Xassembler);
4202
4203 CmdArgs.push_back("-o");
4204 CmdArgs.push_back(Output.getFilename());
4205
4206 for (InputInfoList::const_iterator
4207 it = Inputs.begin(), ie = Inputs.end(); it != ie; ++it) {
4208 const InputInfo &II = *it;
4209 CmdArgs.push_back(II.getFilename());
4210 }
4211
4212 const char *Exec =
4213 Args.MakeArgString(getToolChain().GetProgramPath("as"));
4214 C.addCommand(new Command(JA, *this, Exec, CmdArgs));
4215}
4216
Rafael Espindolac8f008f2010-11-07 20:14:31 +00004217void linuxtools::Link::ConstructJob(Compilation &C, const JobAction &JA,
4218 const InputInfo &Output,
4219 const InputInfoList &Inputs,
4220 const ArgList &Args,
4221 const char *LinkingOutput) const {
4222 const toolchains::Linux& ToolChain =
4223 static_cast<const toolchains::Linux&>(getToolChain());
4224 const Driver &D = ToolChain.getDriver();
4225 ArgStringList CmdArgs;
4226
Rafael Espindolad1002f62010-11-15 18:28:16 +00004227 // Silence warning for "clang -g foo.o -o foo"
4228 Args.ClaimAllArgs(options::OPT_g_Group);
Rafael Espindolad95a8122011-03-01 05:25:27 +00004229 // and "clang -emit-llvm foo.o -o foo"
4230 Args.ClaimAllArgs(options::OPT_emit_llvm);
Rafael Espindolaf92614c2010-11-17 20:37:10 +00004231 // and for "clang -g foo.o -o foo". Other warning options are already
4232 // handled somewhere else.
4233 Args.ClaimAllArgs(options::OPT_w);
Rafael Espindolad1002f62010-11-15 18:28:16 +00004234
Joerg Sonnenberger6165ab12011-03-21 13:51:29 +00004235 if (!D.SysRoot.empty())
4236 CmdArgs.push_back(Args.MakeArgString("--sysroot=" + D.SysRoot));
Rafael Espindolac8f008f2010-11-07 20:14:31 +00004237
Rafael Espindolad47ac232010-11-17 22:26:15 +00004238 if (Args.hasArg(options::OPT_pie))
4239 CmdArgs.push_back("-pie");
4240
Rafael Espindola1c76c592010-11-07 22:57:16 +00004241 if (Args.hasArg(options::OPT_rdynamic))
4242 CmdArgs.push_back("-export-dynamic");
4243
Rafael Espindola34d77dc2010-11-11 19:34:42 +00004244 if (Args.hasArg(options::OPT_s))
4245 CmdArgs.push_back("-s");
4246
Rafael Espindolac8f008f2010-11-07 20:14:31 +00004247 for (std::vector<std::string>::const_iterator i = ToolChain.ExtraOpts.begin(),
4248 e = ToolChain.ExtraOpts.end();
4249 i != e; ++i)
4250 CmdArgs.push_back(i->c_str());
4251
4252 if (!Args.hasArg(options::OPT_static)) {
4253 CmdArgs.push_back("--eh-frame-hdr");
4254 }
4255
4256 CmdArgs.push_back("-m");
4257 if (ToolChain.getArch() == llvm::Triple::x86)
4258 CmdArgs.push_back("elf_i386");
Eric Christopher84fbdb42011-08-19 00:30:14 +00004259 else if (ToolChain.getArch() == llvm::Triple::arm
Douglas Gregord9bb1522011-03-06 19:11:49 +00004260 || ToolChain.getArch() == llvm::Triple::thumb)
Rafael Espindolac8f008f2010-11-07 20:14:31 +00004261 CmdArgs.push_back("armelf_linux_eabi");
Ted Kremenek43d47cc2011-04-05 22:04:27 +00004262 else if (ToolChain.getArch() == llvm::Triple::ppc)
4263 CmdArgs.push_back("elf32ppclinux");
4264 else if (ToolChain.getArch() == llvm::Triple::ppc64)
4265 CmdArgs.push_back("elf64ppc");
Rafael Espindolac8f008f2010-11-07 20:14:31 +00004266 else
4267 CmdArgs.push_back("elf_x86_64");
4268
4269 if (Args.hasArg(options::OPT_static)) {
Douglas Gregord9bb1522011-03-06 19:11:49 +00004270 if (ToolChain.getArch() == llvm::Triple::arm
4271 || ToolChain.getArch() == llvm::Triple::thumb)
Rafael Espindolac8f008f2010-11-07 20:14:31 +00004272 CmdArgs.push_back("-Bstatic");
4273 else
4274 CmdArgs.push_back("-static");
4275 } else if (Args.hasArg(options::OPT_shared)) {
4276 CmdArgs.push_back("-shared");
4277 }
4278
4279 if (ToolChain.getArch() == llvm::Triple::arm ||
Douglas Gregord9bb1522011-03-06 19:11:49 +00004280 ToolChain.getArch() == llvm::Triple::thumb ||
Rafael Espindolac8f008f2010-11-07 20:14:31 +00004281 (!Args.hasArg(options::OPT_static) &&
4282 !Args.hasArg(options::OPT_shared))) {
4283 CmdArgs.push_back("-dynamic-linker");
4284 if (ToolChain.getArch() == llvm::Triple::x86)
4285 CmdArgs.push_back("/lib/ld-linux.so.2");
Douglas Gregord9bb1522011-03-06 19:11:49 +00004286 else if (ToolChain.getArch() == llvm::Triple::arm ||
4287 ToolChain.getArch() == llvm::Triple::thumb)
Rafael Espindolac8f008f2010-11-07 20:14:31 +00004288 CmdArgs.push_back("/lib/ld-linux.so.3");
Ted Kremenek43d47cc2011-04-05 22:04:27 +00004289 else if (ToolChain.getArch() == llvm::Triple::ppc)
Chris Lattner20b90d02011-04-11 21:15:37 +00004290 CmdArgs.push_back("/lib/ld.so.1");
Ted Kremenek43d47cc2011-04-05 22:04:27 +00004291 else if (ToolChain.getArch() == llvm::Triple::ppc64)
Chris Lattner20b90d02011-04-11 21:15:37 +00004292 CmdArgs.push_back("/lib64/ld64.so.1");
Rafael Espindolac8f008f2010-11-07 20:14:31 +00004293 else
4294 CmdArgs.push_back("/lib64/ld-linux-x86-64.so.2");
4295 }
4296
4297 CmdArgs.push_back("-o");
4298 CmdArgs.push_back(Output.getFilename());
4299
Rafael Espindola81937ec2010-12-01 01:52:43 +00004300 if (!Args.hasArg(options::OPT_nostdlib) &&
4301 !Args.hasArg(options::OPT_nostartfiles)) {
Rafael Espindolad47ac232010-11-17 22:26:15 +00004302 const char *crt1 = NULL;
4303 if (!Args.hasArg(options::OPT_shared)){
4304 if (Args.hasArg(options::OPT_pie))
4305 crt1 = "Scrt1.o";
4306 else
4307 crt1 = "crt1.o";
4308 }
4309 if (crt1)
4310 CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath(crt1)));
Rafael Espindolac8f008f2010-11-07 20:14:31 +00004311
Rafael Espindola9aa60e92010-11-12 03:00:39 +00004312 CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath("crti.o")));
Rafael Espindolac8f008f2010-11-07 20:14:31 +00004313
Rafael Espindola9aa60e92010-11-12 03:00:39 +00004314 const char *crtbegin;
4315 if (Args.hasArg(options::OPT_static))
4316 crtbegin = "crtbeginT.o";
Rafael Espindolad47ac232010-11-17 22:26:15 +00004317 else if (Args.hasArg(options::OPT_shared) || Args.hasArg(options::OPT_pie))
Rafael Espindola9aa60e92010-11-12 03:00:39 +00004318 crtbegin = "crtbeginS.o";
4319 else
4320 crtbegin = "crtbegin.o";
4321 CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath(crtbegin)));
4322 }
Rafael Espindolac8f008f2010-11-07 20:14:31 +00004323
4324 Args.AddAllArgs(CmdArgs, options::OPT_L);
4325
4326 const ToolChain::path_list Paths = ToolChain.getFilePaths();
4327
Roman Divackyee8188a2011-03-01 17:53:14 +00004328 for (ToolChain::path_list::const_iterator i = Paths.begin(), e = Paths.end();
4329 i != e; ++i)
Chris Lattner0e62c1c2011-07-23 10:55:15 +00004330 CmdArgs.push_back(Args.MakeArgString(StringRef("-L") + *i));
Rafael Espindolac8f008f2010-11-07 20:14:31 +00004331
4332 AddLinkerInputs(ToolChain, Inputs, Args, CmdArgs);
4333
Rafael Espindola9aa60e92010-11-12 03:00:39 +00004334 if (D.CCCIsCXX && !Args.hasArg(options::OPT_nostdlib)) {
Rafael Espindolac8f008f2010-11-07 20:14:31 +00004335 ToolChain.AddCXXStdlibLibArgs(Args, CmdArgs);
4336 CmdArgs.push_back("-lm");
4337 }
4338
Rafael Espindola9aa60e92010-11-12 03:00:39 +00004339 if (!Args.hasArg(options::OPT_nostdlib)) {
Nick Lewycky97864da2011-06-04 06:27:06 +00004340 if (Args.hasArg(options::OPT_static))
4341 CmdArgs.push_back("--start-group");
4342
Rafael Espindola9aa60e92010-11-12 03:00:39 +00004343 if (!D.CCCIsCXX)
Rafael Espindolac8f008f2010-11-07 20:14:31 +00004344 CmdArgs.push_back("-lgcc");
Rafael Espindola9aa60e92010-11-12 03:00:39 +00004345
4346 if (Args.hasArg(options::OPT_static)) {
4347 if (D.CCCIsCXX)
4348 CmdArgs.push_back("-lgcc");
4349 } else {
4350 if (!D.CCCIsCXX)
4351 CmdArgs.push_back("--as-needed");
4352 CmdArgs.push_back("-lgcc_s");
4353 if (!D.CCCIsCXX)
4354 CmdArgs.push_back("--no-as-needed");
4355 }
4356
4357 if (Args.hasArg(options::OPT_static))
4358 CmdArgs.push_back("-lgcc_eh");
4359 else if (!Args.hasArg(options::OPT_shared) && D.CCCIsCXX)
4360 CmdArgs.push_back("-lgcc");
4361
4362 if (Args.hasArg(options::OPT_pthread) ||
4363 Args.hasArg(options::OPT_pthreads))
4364 CmdArgs.push_back("-lpthread");
4365
4366 CmdArgs.push_back("-lc");
4367
4368 if (Args.hasArg(options::OPT_static))
4369 CmdArgs.push_back("--end-group");
4370 else {
4371 if (!D.CCCIsCXX)
4372 CmdArgs.push_back("-lgcc");
4373
4374 if (!D.CCCIsCXX)
4375 CmdArgs.push_back("--as-needed");
4376 CmdArgs.push_back("-lgcc_s");
4377 if (!D.CCCIsCXX)
4378 CmdArgs.push_back("--no-as-needed");
4379
4380 if (!Args.hasArg(options::OPT_shared) && D.CCCIsCXX)
4381 CmdArgs.push_back("-lgcc");
4382 }
4383
Rafael Espindolad47ac232010-11-17 22:26:15 +00004384
Rafael Espindola81937ec2010-12-01 01:52:43 +00004385 if (!Args.hasArg(options::OPT_nostartfiles)) {
4386 const char *crtend;
4387 if (Args.hasArg(options::OPT_shared) || Args.hasArg(options::OPT_pie))
4388 crtend = "crtendS.o";
4389 else
4390 crtend = "crtend.o";
Rafael Espindola9aa60e92010-11-12 03:00:39 +00004391
Rafael Espindola81937ec2010-12-01 01:52:43 +00004392 CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath(crtend)));
4393 CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath("crtn.o")));
4394 }
Rafael Espindolac8f008f2010-11-07 20:14:31 +00004395 }
4396
Bill Wendling08760582011-06-27 19:15:03 +00004397 addProfileRT(getToolChain(), Args, CmdArgs, getToolChain().getTriple());
Nick Lewycky82fe5f42011-05-24 21:54:59 +00004398
Rafael Espindolac8f008f2010-11-07 20:14:31 +00004399 if (Args.hasArg(options::OPT_use_gold_plugin)) {
4400 CmdArgs.push_back("-plugin");
4401 std::string Plugin = ToolChain.getDriver().Dir + "/../lib/LLVMgold.so";
4402 CmdArgs.push_back(Args.MakeArgString(Plugin));
4403 }
4404
4405 C.addCommand(new Command(JA, *this, ToolChain.Linker.c_str(), CmdArgs));
4406}
Rafael Espindola92b00932010-08-10 00:25:48 +00004407
Chris Lattner3e2ee142010-07-07 16:01:42 +00004408void minix::Assemble::ConstructJob(Compilation &C, const JobAction &JA,
Daniel Dunbar7fbaf532010-08-02 02:38:28 +00004409 const InputInfo &Output,
4410 const InputInfoList &Inputs,
4411 const ArgList &Args,
4412 const char *LinkingOutput) const {
Chris Lattner3e2ee142010-07-07 16:01:42 +00004413 ArgStringList CmdArgs;
4414
4415 Args.AddAllArgValues(CmdArgs, options::OPT_Wa_COMMA,
4416 options::OPT_Xassembler);
4417
4418 CmdArgs.push_back("-o");
Daniel Dunbarb440f562010-08-02 02:38:21 +00004419 CmdArgs.push_back(Output.getFilename());
Chris Lattner3e2ee142010-07-07 16:01:42 +00004420
4421 for (InputInfoList::const_iterator
4422 it = Inputs.begin(), ie = Inputs.end(); it != ie; ++it) {
4423 const InputInfo &II = *it;
Daniel Dunbarb440f562010-08-02 02:38:21 +00004424 CmdArgs.push_back(II.getFilename());
Chris Lattner3e2ee142010-07-07 16:01:42 +00004425 }
4426
4427 const char *Exec =
Daniel Dunbar9c3ed5f2010-07-14 18:46:23 +00004428 Args.MakeArgString(getToolChain().GetProgramPath("gas"));
Daniel Dunbar7fbaf532010-08-02 02:38:28 +00004429 C.addCommand(new Command(JA, *this, Exec, CmdArgs));
Chris Lattner3e2ee142010-07-07 16:01:42 +00004430}
4431
4432void minix::Link::ConstructJob(Compilation &C, const JobAction &JA,
Daniel Dunbar7fbaf532010-08-02 02:38:28 +00004433 const InputInfo &Output,
4434 const InputInfoList &Inputs,
4435 const ArgList &Args,
4436 const char *LinkingOutput) const {
Chris Lattner3e2ee142010-07-07 16:01:42 +00004437 const Driver &D = getToolChain().getDriver();
4438 ArgStringList CmdArgs;
4439
Daniel Dunbarb440f562010-08-02 02:38:21 +00004440 if (Output.isFilename()) {
Chris Lattner3e2ee142010-07-07 16:01:42 +00004441 CmdArgs.push_back("-o");
4442 CmdArgs.push_back(Output.getFilename());
4443 } else {
4444 assert(Output.isNothing() && "Invalid output.");
4445 }
4446
4447 if (!Args.hasArg(options::OPT_nostdlib) &&
4448 !Args.hasArg(options::OPT_nostartfiles))
Daniel Dunbar9c3ed5f2010-07-14 18:46:23 +00004449 CmdArgs.push_back(Args.MakeArgString(getToolChain().GetFilePath(
Chris Lattner3e2ee142010-07-07 16:01:42 +00004450 "/usr/gnu/lib/crtso.o")));
4451
4452 Args.AddAllArgs(CmdArgs, options::OPT_L);
4453 Args.AddAllArgs(CmdArgs, options::OPT_T_Group);
4454 Args.AddAllArgs(CmdArgs, options::OPT_e);
4455
Daniel Dunbar54423b22010-09-17 00:24:54 +00004456 AddLinkerInputs(getToolChain(), Inputs, Args, CmdArgs);
Chris Lattner3e2ee142010-07-07 16:01:42 +00004457
4458 if (!Args.hasArg(options::OPT_nostdlib) &&
4459 !Args.hasArg(options::OPT_nodefaultlibs)) {
4460 if (D.CCCIsCXX) {
Daniel Dunbar3f7796f2010-09-17 01:20:05 +00004461 getToolChain().AddCXXStdlibLibArgs(Args, CmdArgs);
Chris Lattner3e2ee142010-07-07 16:01:42 +00004462 CmdArgs.push_back("-lm");
4463 }
4464
4465 if (Args.hasArg(options::OPT_pthread))
4466 CmdArgs.push_back("-lpthread");
4467 CmdArgs.push_back("-lc");
4468 CmdArgs.push_back("-lgcc");
4469 CmdArgs.push_back("-L/usr/gnu/lib");
4470 // FIXME: fill in the correct search path for the final
4471 // support libraries.
4472 CmdArgs.push_back("-L/usr/gnu/lib/gcc/i686-pc-minix/4.4.3");
4473 }
4474
4475 if (!Args.hasArg(options::OPT_nostdlib) &&
4476 !Args.hasArg(options::OPT_nostartfiles)) {
Daniel Dunbar9c3ed5f2010-07-14 18:46:23 +00004477 CmdArgs.push_back(Args.MakeArgString(getToolChain().GetFilePath(
Chris Lattner3e2ee142010-07-07 16:01:42 +00004478 "/usr/gnu/lib/libend.a")));
4479 }
4480
Bill Wendling08760582011-06-27 19:15:03 +00004481 addProfileRT(getToolChain(), Args, CmdArgs, getToolChain().getTriple());
Nick Lewycky82fe5f42011-05-24 21:54:59 +00004482
Chris Lattner3e2ee142010-07-07 16:01:42 +00004483 const char *Exec =
Daniel Dunbar9c3ed5f2010-07-14 18:46:23 +00004484 Args.MakeArgString(getToolChain().GetProgramPath("/usr/gnu/bin/gld"));
Daniel Dunbar7fbaf532010-08-02 02:38:28 +00004485 C.addCommand(new Command(JA, *this, Exec, CmdArgs));
Chris Lattner3e2ee142010-07-07 16:01:42 +00004486}
4487
Daniel Dunbarcc912342009-05-02 18:28:39 +00004488/// DragonFly Tools
4489
4490// For now, DragonFly Assemble does just about the same as for
4491// FreeBSD, but this may change soon.
4492void dragonfly::Assemble::ConstructJob(Compilation &C, const JobAction &JA,
Daniel Dunbar7fbaf532010-08-02 02:38:28 +00004493 const InputInfo &Output,
Daniel Dunbar1c7577c2009-11-04 06:24:38 +00004494 const InputInfoList &Inputs,
4495 const ArgList &Args,
4496 const char *LinkingOutput) const {
Daniel Dunbarcc912342009-05-02 18:28:39 +00004497 ArgStringList CmdArgs;
4498
4499 // When building 32-bit code on DragonFly/pc64, we have to explicitly
4500 // instruct as in the base system to assemble 32-bit code.
4501 if (getToolChain().getArchName() == "i386")
4502 CmdArgs.push_back("--32");
4503
4504 Args.AddAllArgValues(CmdArgs, options::OPT_Wa_COMMA,
4505 options::OPT_Xassembler);
4506
4507 CmdArgs.push_back("-o");
Daniel Dunbarb440f562010-08-02 02:38:21 +00004508 CmdArgs.push_back(Output.getFilename());
Daniel Dunbarcc912342009-05-02 18:28:39 +00004509
4510 for (InputInfoList::const_iterator
4511 it = Inputs.begin(), ie = Inputs.end(); it != ie; ++it) {
4512 const InputInfo &II = *it;
Daniel Dunbarb440f562010-08-02 02:38:21 +00004513 CmdArgs.push_back(II.getFilename());
Daniel Dunbarcc912342009-05-02 18:28:39 +00004514 }
4515
4516 const char *Exec =
Daniel Dunbar9c3ed5f2010-07-14 18:46:23 +00004517 Args.MakeArgString(getToolChain().GetProgramPath("as"));
Daniel Dunbar7fbaf532010-08-02 02:38:28 +00004518 C.addCommand(new Command(JA, *this, Exec, CmdArgs));
Daniel Dunbarcc912342009-05-02 18:28:39 +00004519}
4520
4521void dragonfly::Link::ConstructJob(Compilation &C, const JobAction &JA,
Daniel Dunbar7fbaf532010-08-02 02:38:28 +00004522 const InputInfo &Output,
4523 const InputInfoList &Inputs,
4524 const ArgList &Args,
4525 const char *LinkingOutput) const {
Daniel Dunbar083edf72009-12-21 18:54:17 +00004526 const Driver &D = getToolChain().getDriver();
Daniel Dunbarcc912342009-05-02 18:28:39 +00004527 ArgStringList CmdArgs;
4528
Joerg Sonnenberger6165ab12011-03-21 13:51:29 +00004529 if (!D.SysRoot.empty())
4530 CmdArgs.push_back(Args.MakeArgString("--sysroot=" + D.SysRoot));
4531
Daniel Dunbarcc912342009-05-02 18:28:39 +00004532 if (Args.hasArg(options::OPT_static)) {
4533 CmdArgs.push_back("-Bstatic");
4534 } else {
4535 if (Args.hasArg(options::OPT_shared))
4536 CmdArgs.push_back("-Bshareable");
4537 else {
4538 CmdArgs.push_back("-dynamic-linker");
4539 CmdArgs.push_back("/usr/libexec/ld-elf.so.2");
4540 }
4541 }
4542
4543 // When building 32-bit code on DragonFly/pc64, we have to explicitly
4544 // instruct ld in the base system to link 32-bit code.
4545 if (getToolChain().getArchName() == "i386") {
4546 CmdArgs.push_back("-m");
4547 CmdArgs.push_back("elf_i386");
4548 }
4549
Daniel Dunbarb440f562010-08-02 02:38:21 +00004550 if (Output.isFilename()) {
Daniel Dunbarcc912342009-05-02 18:28:39 +00004551 CmdArgs.push_back("-o");
4552 CmdArgs.push_back(Output.getFilename());
4553 } else {
4554 assert(Output.isNothing() && "Invalid output.");
4555 }
4556
4557 if (!Args.hasArg(options::OPT_nostdlib) &&
4558 !Args.hasArg(options::OPT_nostartfiles)) {
4559 if (!Args.hasArg(options::OPT_shared)) {
Chris Lattner3e2ee142010-07-07 16:01:42 +00004560 CmdArgs.push_back(
Daniel Dunbar9c3ed5f2010-07-14 18:46:23 +00004561 Args.MakeArgString(getToolChain().GetFilePath("crt1.o")));
Chris Lattner3e2ee142010-07-07 16:01:42 +00004562 CmdArgs.push_back(
Daniel Dunbar9c3ed5f2010-07-14 18:46:23 +00004563 Args.MakeArgString(getToolChain().GetFilePath("crti.o")));
Chris Lattner3e2ee142010-07-07 16:01:42 +00004564 CmdArgs.push_back(
Daniel Dunbar9c3ed5f2010-07-14 18:46:23 +00004565 Args.MakeArgString(getToolChain().GetFilePath("crtbegin.o")));
Daniel Dunbarcc912342009-05-02 18:28:39 +00004566 } else {
Chris Lattner3e2ee142010-07-07 16:01:42 +00004567 CmdArgs.push_back(
Daniel Dunbar9c3ed5f2010-07-14 18:46:23 +00004568 Args.MakeArgString(getToolChain().GetFilePath("crti.o")));
Chris Lattner3e2ee142010-07-07 16:01:42 +00004569 CmdArgs.push_back(
Daniel Dunbar9c3ed5f2010-07-14 18:46:23 +00004570 Args.MakeArgString(getToolChain().GetFilePath("crtbeginS.o")));
Daniel Dunbarcc912342009-05-02 18:28:39 +00004571 }
4572 }
4573
4574 Args.AddAllArgs(CmdArgs, options::OPT_L);
4575 Args.AddAllArgs(CmdArgs, options::OPT_T_Group);
4576 Args.AddAllArgs(CmdArgs, options::OPT_e);
4577
Daniel Dunbar54423b22010-09-17 00:24:54 +00004578 AddLinkerInputs(getToolChain(), Inputs, Args, CmdArgs);
Daniel Dunbarcc912342009-05-02 18:28:39 +00004579
4580 if (!Args.hasArg(options::OPT_nostdlib) &&
4581 !Args.hasArg(options::OPT_nodefaultlibs)) {
4582 // FIXME: GCC passes on -lgcc, -lgcc_pic and a whole lot of
4583 // rpaths
4584 CmdArgs.push_back("-L/usr/lib/gcc41");
4585
4586 if (!Args.hasArg(options::OPT_static)) {
4587 CmdArgs.push_back("-rpath");
4588 CmdArgs.push_back("/usr/lib/gcc41");
4589
4590 CmdArgs.push_back("-rpath-link");
4591 CmdArgs.push_back("/usr/lib/gcc41");
4592
4593 CmdArgs.push_back("-rpath");
4594 CmdArgs.push_back("/usr/lib");
4595
4596 CmdArgs.push_back("-rpath-link");
4597 CmdArgs.push_back("/usr/lib");
4598 }
4599
Rafael Espindola38360b32010-07-20 12:59:03 +00004600 if (D.CCCIsCXX) {
Daniel Dunbar3f7796f2010-09-17 01:20:05 +00004601 getToolChain().AddCXXStdlibLibArgs(Args, CmdArgs);
Rafael Espindola38360b32010-07-20 12:59:03 +00004602 CmdArgs.push_back("-lm");
4603 }
4604
Daniel Dunbarcc912342009-05-02 18:28:39 +00004605 if (Args.hasArg(options::OPT_shared)) {
4606 CmdArgs.push_back("-lgcc_pic");
4607 } else {
4608 CmdArgs.push_back("-lgcc");
4609 }
4610
4611
4612 if (Args.hasArg(options::OPT_pthread))
Mike Stump0a65b632009-10-31 20:11:46 +00004613 CmdArgs.push_back("-lpthread");
Daniel Dunbarcc912342009-05-02 18:28:39 +00004614
4615 if (!Args.hasArg(options::OPT_nolibc)) {
4616 CmdArgs.push_back("-lc");
4617 }
4618
4619 if (Args.hasArg(options::OPT_shared)) {
4620 CmdArgs.push_back("-lgcc_pic");
4621 } else {
4622 CmdArgs.push_back("-lgcc");
4623 }
4624 }
4625
4626 if (!Args.hasArg(options::OPT_nostdlib) &&
4627 !Args.hasArg(options::OPT_nostartfiles)) {
4628 if (!Args.hasArg(options::OPT_shared))
Chris Lattner3e2ee142010-07-07 16:01:42 +00004629 CmdArgs.push_back(Args.MakeArgString(
Daniel Dunbar9c3ed5f2010-07-14 18:46:23 +00004630 getToolChain().GetFilePath("crtend.o")));
Daniel Dunbarcc912342009-05-02 18:28:39 +00004631 else
Chris Lattner3e2ee142010-07-07 16:01:42 +00004632 CmdArgs.push_back(Args.MakeArgString(
Daniel Dunbar9c3ed5f2010-07-14 18:46:23 +00004633 getToolChain().GetFilePath("crtendS.o")));
Chris Lattner3e2ee142010-07-07 16:01:42 +00004634 CmdArgs.push_back(Args.MakeArgString(
Daniel Dunbar9c3ed5f2010-07-14 18:46:23 +00004635 getToolChain().GetFilePath("crtn.o")));
Daniel Dunbarcc912342009-05-02 18:28:39 +00004636 }
4637
Bill Wendling08760582011-06-27 19:15:03 +00004638 addProfileRT(getToolChain(), Args, CmdArgs, getToolChain().getTriple());
Nick Lewycky82fe5f42011-05-24 21:54:59 +00004639
Daniel Dunbarcc912342009-05-02 18:28:39 +00004640 const char *Exec =
Daniel Dunbar9c3ed5f2010-07-14 18:46:23 +00004641 Args.MakeArgString(getToolChain().GetProgramPath("ld"));
Daniel Dunbar7fbaf532010-08-02 02:38:28 +00004642 C.addCommand(new Command(JA, *this, Exec, CmdArgs));
Daniel Dunbarcc912342009-05-02 18:28:39 +00004643}
Michael J. Spencerb186bc32010-08-21 21:55:07 +00004644
4645void visualstudio::Link::ConstructJob(Compilation &C, const JobAction &JA,
4646 const InputInfo &Output,
4647 const InputInfoList &Inputs,
4648 const ArgList &Args,
4649 const char *LinkingOutput) const {
Michael J. Spencerb186bc32010-08-21 21:55:07 +00004650 ArgStringList CmdArgs;
4651
4652 if (Output.isFilename()) {
Daniel Dunbar2cc3f172010-09-17 00:45:02 +00004653 CmdArgs.push_back(Args.MakeArgString(std::string("-out:") +
4654 Output.getFilename()));
Michael J. Spencerb186bc32010-08-21 21:55:07 +00004655 } else {
4656 assert(Output.isNothing() && "Invalid output.");
4657 }
4658
4659 if (!Args.hasArg(options::OPT_nostdlib) &&
4660 !Args.hasArg(options::OPT_nostartfiles)) {
4661 CmdArgs.push_back("-defaultlib:libcmt");
4662 }
4663
4664 CmdArgs.push_back("-nologo");
4665
Daniel Dunbar54423b22010-09-17 00:24:54 +00004666 AddLinkerInputs(getToolChain(), Inputs, Args, CmdArgs);
Michael J. Spencerb186bc32010-08-21 21:55:07 +00004667
4668 const char *Exec =
Daniel Dunbar54423b22010-09-17 00:24:54 +00004669 Args.MakeArgString(getToolChain().GetProgramPath("link.exe"));
Michael J. Spencerb186bc32010-08-21 21:55:07 +00004670 C.addCommand(new Command(JA, *this, Exec, CmdArgs));
4671}