blob: 28a14a29703072040ca03a629f45a07f0664f446 [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
Mike Stump11289f42009-09-09 15:08:12 +0000186void Clang::AddPreprocessingOptions(const Driver &D,
Douglas Gregor111af7d2009-04-18 00:34:01 +0000187 const ArgList &Args,
Daniel Dunbard067f7f2009-04-08 23:54:23 +0000188 ArgStringList &CmdArgs,
189 const InputInfo &Output,
190 const InputInfoList &Inputs) const {
Daniel Dunbard067f7f2009-04-08 23:54:23 +0000191 Arg *A;
Daniel Dunbar367dbb92009-06-08 21:48:20 +0000192
Daniel Dunbar64198ef2009-09-10 01:21:05 +0000193 CheckPreprocessingOptions(D, Args);
194
195 Args.AddLastArg(CmdArgs, options::OPT_C);
196 Args.AddLastArg(CmdArgs, options::OPT_CC);
Daniel Dunbar367dbb92009-06-08 21:48:20 +0000197
198 // Handle dependency file generation.
Daniel Dunbar86aed7d2010-12-08 21:33:40 +0000199 if ((A = Args.getLastArg(options::OPT_M, options::OPT_MM)) ||
Daniel Dunbard067f7f2009-04-08 23:54:23 +0000200 (A = Args.getLastArg(options::OPT_MD)) ||
201 (A = Args.getLastArg(options::OPT_MMD))) {
202 // Determine the output location.
203 const char *DepFile;
204 if (Output.getType() == types::TY_Dependencies) {
Daniel Dunbarb440f562010-08-02 02:38:21 +0000205 DepFile = Output.getFilename();
Daniel Dunbard067f7f2009-04-08 23:54:23 +0000206 } else if (Arg *MF = Args.getLastArg(options::OPT_MF)) {
207 DepFile = MF->getValue(Args);
Daniel Dunbar0bfb21e2009-11-19 03:26:40 +0000208 } else if (A->getOption().matches(options::OPT_M) ||
209 A->getOption().matches(options::OPT_MM)) {
Daniel Dunbard067f7f2009-04-08 23:54:23 +0000210 DepFile = "-";
211 } else {
212 DepFile = darwin::CC1::getDependencyFileName(Args, Inputs);
213 }
214 CmdArgs.push_back("-dependency-file");
215 CmdArgs.push_back(DepFile);
216
Chris Lattnerbf2803f2010-03-29 17:55:58 +0000217 // Add a default target if one wasn't specified.
Daniel Dunbard067f7f2009-04-08 23:54:23 +0000218 if (!Args.hasArg(options::OPT_MT) && !Args.hasArg(options::OPT_MQ)) {
219 const char *DepTarget;
220
221 // If user provided -o, that is the dependency target, except
222 // when we are only generating a dependency file.
223 Arg *OutputOpt = Args.getLastArg(options::OPT_o);
224 if (OutputOpt && Output.getType() != types::TY_Dependencies) {
225 DepTarget = OutputOpt->getValue(Args);
226 } else {
227 // Otherwise derive from the base input.
228 //
229 // FIXME: This should use the computed output file location.
Michael J. Spencere1696752010-12-18 00:19:12 +0000230 llvm::SmallString<128> P(Inputs[0].getBaseInput());
231 llvm::sys::path::replace_extension(P, "o");
232 DepTarget = Args.MakeArgString(llvm::sys::path::filename(P));
Daniel Dunbard067f7f2009-04-08 23:54:23 +0000233 }
234
235 CmdArgs.push_back("-MT");
Chris Lattnerbf2803f2010-03-29 17:55:58 +0000236 llvm::SmallString<128> Quoted;
237 QuoteTarget(DepTarget, Quoted);
238 CmdArgs.push_back(Args.MakeArgString(Quoted));
Daniel Dunbard067f7f2009-04-08 23:54:23 +0000239 }
240
Daniel Dunbar0bfb21e2009-11-19 03:26:40 +0000241 if (A->getOption().matches(options::OPT_M) ||
242 A->getOption().matches(options::OPT_MD))
Daniel Dunbard067f7f2009-04-08 23:54:23 +0000243 CmdArgs.push_back("-sys-header-deps");
244 }
245
Peter Collingbourne77b0e7f22011-07-12 19:35:15 +0000246 if (Args.hasArg(options::OPT_MG)) {
247 if (!A || A->getOption().matches(options::OPT_MD) ||
248 A->getOption().matches(options::OPT_MMD))
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000249 D.Diag(diag::err_drv_mg_requires_m_or_mm);
Peter Collingbourne77b0e7f22011-07-12 19:35:15 +0000250 CmdArgs.push_back("-MG");
251 }
252
Daniel Dunbard067f7f2009-04-08 23:54:23 +0000253 Args.AddLastArg(CmdArgs, options::OPT_MP);
Chris Lattnerbf2803f2010-03-29 17:55:58 +0000254
255 // Convert all -MQ <target> args to -MT <quoted target>
256 for (arg_iterator it = Args.filtered_begin(options::OPT_MT,
257 options::OPT_MQ),
258 ie = Args.filtered_end(); it != ie; ++it) {
Daniel Dunbara442fd52010-06-11 22:00:13 +0000259 const Arg *A = *it;
260 A->claim();
Chris Lattnerbf2803f2010-03-29 17:55:58 +0000261
Daniel Dunbara442fd52010-06-11 22:00:13 +0000262 if (A->getOption().matches(options::OPT_MQ)) {
Chris Lattnerbf2803f2010-03-29 17:55:58 +0000263 CmdArgs.push_back("-MT");
264 llvm::SmallString<128> Quoted;
Daniel Dunbara442fd52010-06-11 22:00:13 +0000265 QuoteTarget(A->getValue(Args), Quoted);
Chris Lattnerbf2803f2010-03-29 17:55:58 +0000266 CmdArgs.push_back(Args.MakeArgString(Quoted));
267
268 // -MT flag - no change
269 } else {
Daniel Dunbara442fd52010-06-11 22:00:13 +0000270 A->render(Args, CmdArgs);
Chris Lattnerbf2803f2010-03-29 17:55:58 +0000271 }
272 }
Daniel Dunbard067f7f2009-04-08 23:54:23 +0000273
Douglas Gregor111af7d2009-04-18 00:34:01 +0000274 // Add -i* options, and automatically translate to
275 // -include-pch/-include-pth for transparent PCH support. It's
276 // wonky, but we include looking for .gch so we can support seamless
277 // replacement into a build system already set up to be generating
278 // .gch files.
Argyrios Kyrtzidis2f23b412010-09-30 16:53:47 +0000279 bool RenderedImplicitInclude = false;
Daniel Dunbar44b36ee2009-11-25 11:53:23 +0000280 for (arg_iterator it = Args.filtered_begin(options::OPT_clang_i_Group),
281 ie = Args.filtered_end(); it != ie; ++it) {
282 const Arg *A = it;
Daniel Dunbard067f7f2009-04-08 23:54:23 +0000283
284 if (A->getOption().matches(options::OPT_include)) {
Argyrios Kyrtzidis2f23b412010-09-30 16:53:47 +0000285 bool IsFirstImplicitInclude = !RenderedImplicitInclude;
286 RenderedImplicitInclude = true;
287
Argyrios Kyrtzidis90bdfbb2010-08-11 23:27:58 +0000288 // Use PCH if the user requested it.
Daniel Dunbarcbc34b72009-10-15 20:02:44 +0000289 bool UsePCH = D.CCCUsePCH;
Daniel Dunbarcbc34b72009-10-15 20:02:44 +0000290
Daniel Dunbard067f7f2009-04-08 23:54:23 +0000291 bool FoundPTH = false;
Douglas Gregor111af7d2009-04-18 00:34:01 +0000292 bool FoundPCH = false;
Daniel Dunbard067f7f2009-04-08 23:54:23 +0000293 llvm::sys::Path P(A->getValue(Args));
Michael J. Spencerf6efe582011-01-10 02:34:13 +0000294 bool Exists;
Daniel Dunbarcbc34b72009-10-15 20:02:44 +0000295 if (UsePCH) {
Douglas Gregor111af7d2009-04-18 00:34:01 +0000296 P.appendSuffix("pch");
Michael J. Spencerf6efe582011-01-10 02:34:13 +0000297 if (!llvm::sys::fs::exists(P.str(), Exists) && Exists)
Douglas Gregor111af7d2009-04-18 00:34:01 +0000298 FoundPCH = true;
Mike Stump11289f42009-09-09 15:08:12 +0000299 else
Douglas Gregor111af7d2009-04-18 00:34:01 +0000300 P.eraseSuffix();
Daniel Dunbard067f7f2009-04-08 23:54:23 +0000301 }
302
Douglas Gregor111af7d2009-04-18 00:34:01 +0000303 if (!FoundPCH) {
304 P.appendSuffix("pth");
Michael J. Spencerf6efe582011-01-10 02:34:13 +0000305 if (!llvm::sys::fs::exists(P.str(), Exists) && Exists)
Douglas Gregor111af7d2009-04-18 00:34:01 +0000306 FoundPTH = true;
307 else
308 P.eraseSuffix();
Mike Stump11289f42009-09-09 15:08:12 +0000309 }
310
Douglas Gregor111af7d2009-04-18 00:34:01 +0000311 if (!FoundPCH && !FoundPTH) {
312 P.appendSuffix("gch");
Michael J. Spencerf6efe582011-01-10 02:34:13 +0000313 if (!llvm::sys::fs::exists(P.str(), Exists) && Exists) {
Daniel Dunbarcbc34b72009-10-15 20:02:44 +0000314 FoundPCH = UsePCH;
315 FoundPTH = !UsePCH;
Douglas Gregor111af7d2009-04-18 00:34:01 +0000316 }
Mike Stump11289f42009-09-09 15:08:12 +0000317 else
Douglas Gregor111af7d2009-04-18 00:34:01 +0000318 P.eraseSuffix();
319 }
320
321 if (FoundPCH || FoundPTH) {
Argyrios Kyrtzidis2f23b412010-09-30 16:53:47 +0000322 if (IsFirstImplicitInclude) {
323 A->claim();
324 if (UsePCH)
325 CmdArgs.push_back("-include-pch");
326 else
327 CmdArgs.push_back("-include-pth");
328 CmdArgs.push_back(Args.MakeArgString(P.str()));
329 continue;
330 } else {
331 // Ignore the PCH if not first on command line and emit warning.
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000332 D.Diag(diag::warn_drv_pch_not_first_include)
Argyrios Kyrtzidis2f23b412010-09-30 16:53:47 +0000333 << P.str() << A->getAsString(Args);
334 }
Daniel Dunbard067f7f2009-04-08 23:54:23 +0000335 }
336 }
337
338 // Not translated, render as usual.
339 A->claim();
340 A->render(Args, CmdArgs);
341 }
342
343 Args.AddAllArgs(CmdArgs, options::OPT_D, options::OPT_U);
Douglas Gregor9f93e382011-07-28 04:45:53 +0000344 Args.AddAllArgs(CmdArgs, options::OPT_I_Group, options::OPT_F,
345 options::OPT_index_header_map);
Daniel Dunbard067f7f2009-04-08 23:54:23 +0000346
Daniel Dunbarbf11f792010-09-14 23:12:35 +0000347 // Add C++ include arguments, if needed.
348 types::ID InputType = Inputs[0].getType();
John McCall31168b02011-06-15 23:02:42 +0000349 if (types::isCXX(InputType)) {
350 bool ObjCXXAutoRefCount
351 = types::isObjC(InputType) && isObjCAutoRefCount(Args);
Eric Christopher84fbdb42011-08-19 00:30:14 +0000352 getToolChain().AddClangCXXStdlibIncludeArgs(Args, CmdArgs,
John McCall31168b02011-06-15 23:02:42 +0000353 ObjCXXAutoRefCount);
Bob Wilsonb02ea3d2011-06-21 21:12:29 +0000354 Args.AddAllArgs(CmdArgs, options::OPT_stdlib_EQ);
John McCall31168b02011-06-15 23:02:42 +0000355 }
Daniel Dunbarbf11f792010-09-14 23:12:35 +0000356
Daniel Dunbard067f7f2009-04-08 23:54:23 +0000357 // Add -Wp, and -Xassembler if using the preprocessor.
358
359 // FIXME: There is a very unfortunate problem here, some troubled
360 // souls abuse -Wp, to pass preprocessor options in gcc syntax. To
361 // really support that we would have to parse and then translate
362 // those options. :(
363 Args.AddAllArgValues(CmdArgs, options::OPT_Wp_COMMA,
364 options::OPT_Xpreprocessor);
Daniel Dunbar38b62792009-10-29 01:53:44 +0000365
366 // -I- is a deprecated GCC feature, reject it.
367 if (Arg *A = Args.getLastArg(options::OPT_I_))
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000368 D.Diag(diag::err_drv_I_dash_not_supported) << A->getAsString(Args);
Chandler Carruth24e17e12010-10-20 07:00:47 +0000369
370 // If we have a --sysroot, and don't have an explicit -isysroot flag, add an
371 // -isysroot to the CC1 invocation.
372 if (Arg *A = Args.getLastArg(options::OPT__sysroot_EQ)) {
373 if (!Args.hasArg(options::OPT_isysroot)) {
374 CmdArgs.push_back("-isysroot");
375 CmdArgs.push_back(A->getValue(Args));
376 }
377 }
Douglas Gregorf936f782011-09-14 20:28:46 +0000378
379 // If a module path was provided, pass it along. Otherwise, use a temporary
380 // directory.
381 if (Arg *A = Args.getLastArg(options::OPT_fmodule_cache_path)) {
382 CmdArgs.push_back(A->getValue(Args));
383 A->claim();
384 A->render(Args, CmdArgs);
385 } else {
386 llvm::SmallString<128> DefaultModuleCache;
387 llvm::sys::path::system_temp_directory(/*erasedOnReboot=*/false,
388 DefaultModuleCache);
389 llvm::sys::path::append(DefaultModuleCache, "clang-module-cache");
390 CmdArgs.push_back("-fmodule-cache-path");
391 CmdArgs.push_back(Args.MakeArgString(DefaultModuleCache));
392 }
Daniel Dunbard067f7f2009-04-08 23:54:23 +0000393}
394
Chris Lattner57540c52011-04-15 05:22:18 +0000395/// getARMTargetCPU - Get the (LLVM) name of the ARM cpu we are targeting.
Daniel Dunbarfb58b0a2009-09-10 06:49:20 +0000396//
397// FIXME: tblgen this.
Rafael Espindola0e1fb4f2010-06-28 17:18:09 +0000398static const char *getARMTargetCPU(const ArgList &Args,
399 const llvm::Triple &Triple) {
Daniel Dunbarfb58b0a2009-09-10 06:49:20 +0000400 // FIXME: Warn on inconsistent use of -mcpu and -march.
Daniel Dunbar0f5c5422009-09-10 04:57:17 +0000401
402 // If we have -mcpu=, use that.
403 if (Arg *A = Args.getLastArg(options::OPT_mcpu_EQ))
404 return A->getValue(Args);
405
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000406 StringRef MArch;
Daniel Dunbar0f5c5422009-09-10 04:57:17 +0000407 if (Arg *A = Args.getLastArg(options::OPT_march_EQ)) {
Rafael Espindola0e1fb4f2010-06-28 17:18:09 +0000408 // Otherwise, if we have -march= choose the base CPU for that arch.
409 MArch = A->getValue(Args);
410 } else {
411 // Otherwise, use the Arch from the triple.
412 MArch = Triple.getArchName();
Daniel Dunbar0f5c5422009-09-10 04:57:17 +0000413 }
414
Rafael Espindola0e1fb4f2010-06-28 17:18:09 +0000415 if (MArch == "armv2" || MArch == "armv2a")
416 return "arm2";
417 if (MArch == "armv3")
418 return "arm6";
419 if (MArch == "armv3m")
420 return "arm7m";
421 if (MArch == "armv4" || MArch == "armv4t")
422 return "arm7tdmi";
423 if (MArch == "armv5" || MArch == "armv5t")
424 return "arm10tdmi";
425 if (MArch == "armv5e" || MArch == "armv5te")
426 return "arm1026ejs";
427 if (MArch == "armv5tej")
428 return "arm926ej-s";
429 if (MArch == "armv6" || MArch == "armv6k")
430 return "arm1136jf-s";
431 if (MArch == "armv6j")
432 return "arm1136j-s";
433 if (MArch == "armv6z" || MArch == "armv6zk")
434 return "arm1176jzf-s";
435 if (MArch == "armv6t2")
436 return "arm1156t2-s";
437 if (MArch == "armv7" || MArch == "armv7a" || MArch == "armv7-a")
438 return "cortex-a8";
439 if (MArch == "armv7r" || MArch == "armv7-r")
440 return "cortex-r4";
441 if (MArch == "armv7m" || MArch == "armv7-m")
442 return "cortex-m3";
443 if (MArch == "ep9312")
444 return "ep9312";
445 if (MArch == "iwmmxt")
446 return "iwmmxt";
447 if (MArch == "xscale")
448 return "xscale";
Bob Wilsond9249412011-03-21 20:40:05 +0000449 if (MArch == "armv6m" || MArch == "armv6-m")
450 return "cortex-m0";
Rafael Espindola0e1fb4f2010-06-28 17:18:09 +0000451
452 // If all else failed, return the most base CPU LLVM supports.
Daniel Dunbar0f5c5422009-09-10 04:57:17 +0000453 return "arm7tdmi";
454}
455
Daniel Dunbarf492c922009-09-10 22:59:51 +0000456/// getLLVMArchSuffixForARM - Get the LLVM arch name to use for a particular
Daniel Dunbarfb58b0a2009-09-10 06:49:20 +0000457/// CPU.
458//
459// FIXME: This is redundant with -mcpu, why does LLVM use this.
460// FIXME: tblgen this, or kill it!
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000461static const char *getLLVMArchSuffixForARM(StringRef CPU) {
Daniel Dunbarfb58b0a2009-09-10 06:49:20 +0000462 if (CPU == "arm7tdmi" || CPU == "arm7tdmi-s" || CPU == "arm710t" ||
463 CPU == "arm720t" || CPU == "arm9" || CPU == "arm9tdmi" ||
464 CPU == "arm920" || CPU == "arm920t" || CPU == "arm922t" ||
465 CPU == "arm940t" || CPU == "ep9312")
Daniel Dunbarf492c922009-09-10 22:59:51 +0000466 return "v4t";
Daniel Dunbarfb58b0a2009-09-10 06:49:20 +0000467
468 if (CPU == "arm10tdmi" || CPU == "arm1020t")
Daniel Dunbarf492c922009-09-10 22:59:51 +0000469 return "v5";
Daniel Dunbarfb58b0a2009-09-10 06:49:20 +0000470
471 if (CPU == "arm9e" || CPU == "arm926ej-s" || CPU == "arm946e-s" ||
472 CPU == "arm966e-s" || CPU == "arm968e-s" || CPU == "arm10e" ||
473 CPU == "arm1020e" || CPU == "arm1022e" || CPU == "xscale" ||
474 CPU == "iwmmxt")
Daniel Dunbarf492c922009-09-10 22:59:51 +0000475 return "v5e";
Daniel Dunbarfb58b0a2009-09-10 06:49:20 +0000476
477 if (CPU == "arm1136j-s" || CPU == "arm1136jf-s" || CPU == "arm1176jz-s" ||
478 CPU == "arm1176jzf-s" || CPU == "mpcorenovfp" || CPU == "mpcore")
Daniel Dunbarf492c922009-09-10 22:59:51 +0000479 return "v6";
Daniel Dunbarfb58b0a2009-09-10 06:49:20 +0000480
481 if (CPU == "arm1156t2-s" || CPU == "arm1156t2f-s")
Daniel Dunbarf492c922009-09-10 22:59:51 +0000482 return "v6t2";
Daniel Dunbarfb58b0a2009-09-10 06:49:20 +0000483
484 if (CPU == "cortex-a8" || CPU == "cortex-a9")
Daniel Dunbarf492c922009-09-10 22:59:51 +0000485 return "v7";
Daniel Dunbarfb58b0a2009-09-10 06:49:20 +0000486
Daniel Dunbarf492c922009-09-10 22:59:51 +0000487 return "";
Daniel Dunbarfb58b0a2009-09-10 06:49:20 +0000488}
489
Daniel Dunbard609b7b2009-11-17 06:37:03 +0000490// FIXME: Move to target hook.
491static bool isSignedCharDefault(const llvm::Triple &Triple) {
492 switch (Triple.getArch()) {
493 default:
494 return true;
495
Jim Grosbach7c2c6642011-05-24 15:40:46 +0000496 case llvm::Triple::arm:
Daniel Dunbard609b7b2009-11-17 06:37:03 +0000497 case llvm::Triple::ppc:
498 case llvm::Triple::ppc64:
499 if (Triple.getOS() == llvm::Triple::Darwin)
500 return true;
501 return false;
502
503 case llvm::Triple::systemz:
504 return false;
505 }
506}
507
Daniel Dunbar0f5c5422009-09-10 04:57:17 +0000508void Clang::AddARMTargetArgs(const ArgList &Args,
Daniel Dunbarc9388c12011-03-17 17:10:06 +0000509 ArgStringList &CmdArgs,
510 bool KernelOrKext) const {
Daniel Dunbar083edf72009-12-21 18:54:17 +0000511 const Driver &D = getToolChain().getDriver();
Rafael Espindola0e1fb4f2010-06-28 17:18:09 +0000512 llvm::Triple Triple = getToolChain().getTriple();
Daniel Dunbar78485922009-09-10 23:00:09 +0000513
Daniel Dunbar908b4852011-03-02 00:55:57 +0000514 // Disable movt generation, if requested.
515#ifdef DISABLE_ARM_DARWIN_USE_MOVT
Daniel Dunbar12100e22011-03-22 16:48:17 +0000516 CmdArgs.push_back("-backend-option");
Daniel Dunbar908b4852011-03-02 00:55:57 +0000517 CmdArgs.push_back("-arm-darwin-use-movt=0");
518#endif
519
Daniel Dunbar4ed78982009-09-14 00:34:46 +0000520 // Select the ABI to use.
521 //
522 // FIXME: Support -meabi.
523 const char *ABIName = 0;
524 if (Arg *A = Args.getLastArg(options::OPT_mabi_EQ)) {
525 ABIName = A->getValue(Args);
526 } else {
527 // Select the default based on the platform.
Bob Wilsond1447c42011-02-04 17:59:28 +0000528 switch(Triple.getEnvironment()) {
529 case llvm::Triple::GNUEABI:
Daniel Dunbar4ed78982009-09-14 00:34:46 +0000530 ABIName = "aapcs-linux";
Bob Wilsond1447c42011-02-04 17:59:28 +0000531 break;
532 case llvm::Triple::EABI:
Rafael Espindola23a8a062010-06-16 19:01:17 +0000533 ABIName = "aapcs";
Bob Wilsond1447c42011-02-04 17:59:28 +0000534 break;
535 default:
Rafael Espindola23a8a062010-06-16 19:01:17 +0000536 ABIName = "apcs-gnu";
Bob Wilsond1447c42011-02-04 17:59:28 +0000537 }
Daniel Dunbar4ed78982009-09-14 00:34:46 +0000538 }
539 CmdArgs.push_back("-target-abi");
540 CmdArgs.push_back(ABIName);
541
Daniel Dunbar0f5c5422009-09-10 04:57:17 +0000542 // Set the CPU based on -march= and -mcpu=.
Daniel Dunbara7d02312009-12-18 06:30:12 +0000543 CmdArgs.push_back("-target-cpu");
Rafael Espindola0e1fb4f2010-06-28 17:18:09 +0000544 CmdArgs.push_back(getARMTargetCPU(Args, Triple));
Daniel Dunbar0f5c5422009-09-10 04:57:17 +0000545
Daniel Dunbar78485922009-09-10 23:00:09 +0000546 // Select the float ABI as determined by -msoft-float, -mhard-float, and
547 // -mfloat-abi=.
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000548 StringRef FloatABI;
Daniel Dunbar78485922009-09-10 23:00:09 +0000549 if (Arg *A = Args.getLastArg(options::OPT_msoft_float,
550 options::OPT_mhard_float,
551 options::OPT_mfloat_abi_EQ)) {
552 if (A->getOption().matches(options::OPT_msoft_float))
553 FloatABI = "soft";
554 else if (A->getOption().matches(options::OPT_mhard_float))
555 FloatABI = "hard";
556 else {
557 FloatABI = A->getValue(Args);
558 if (FloatABI != "soft" && FloatABI != "softfp" && FloatABI != "hard") {
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000559 D.Diag(diag::err_drv_invalid_mfloat_abi)
Daniel Dunbar78485922009-09-10 23:00:09 +0000560 << A->getAsString(Args);
561 FloatABI = "soft";
562 }
563 }
564 }
565
566 // If unspecified, choose the default based on the platform.
567 if (FloatABI.empty()) {
Rafael Espindola0e1fb4f2010-06-28 17:18:09 +0000568 const llvm::Triple &Triple = getToolChain().getTriple();
569 switch (Triple.getOS()) {
Daniel Dunbar78485922009-09-10 23:00:09 +0000570 case llvm::Triple::Darwin: {
571 // Darwin defaults to "softfp" for v6 and v7.
572 //
573 // FIXME: Factor out an ARM class so we can cache the arch somewhere.
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000574 StringRef ArchName =
Rafael Espindola0e1fb4f2010-06-28 17:18:09 +0000575 getLLVMArchSuffixForARM(getARMTargetCPU(Args, Triple));
Daniel Dunbar78485922009-09-10 23:00:09 +0000576 if (ArchName.startswith("v6") || ArchName.startswith("v7"))
577 FloatABI = "softfp";
578 else
579 FloatABI = "soft";
580 break;
581 }
582
Rafael Espindolab1ef8ff2010-06-27 18:29:21 +0000583 case llvm::Triple::Linux: {
Bob Wilsond1447c42011-02-04 17:59:28 +0000584 if (getToolChain().getTriple().getEnvironment() == llvm::Triple::GNUEABI) {
Rafael Espindolab1ef8ff2010-06-27 18:29:21 +0000585 FloatABI = "softfp";
586 break;
587 }
588 }
589 // fall through
590
Daniel Dunbar78485922009-09-10 23:00:09 +0000591 default:
Bob Wilsond1447c42011-02-04 17:59:28 +0000592 switch(Triple.getEnvironment()) {
593 case llvm::Triple::GNUEABI:
594 FloatABI = "softfp";
595 break;
596 case llvm::Triple::EABI:
597 // EABI is always AAPCS, and if it was not marked 'hard', it's softfp
598 FloatABI = "softfp";
599 break;
600 default:
601 // Assume "soft", but warn the user we are guessing.
602 FloatABI = "soft";
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000603 D.Diag(diag::warn_drv_assuming_mfloat_abi_is) << "soft";
Bob Wilsond1447c42011-02-04 17:59:28 +0000604 break;
605 }
Daniel Dunbar78485922009-09-10 23:00:09 +0000606 }
607 }
608
609 if (FloatABI == "soft") {
610 // Floating point operations and argument passing are soft.
611 //
612 // FIXME: This changes CPP defines, we need -target-soft-float.
Daniel Dunbara74f8ff2009-11-30 08:42:00 +0000613 CmdArgs.push_back("-msoft-float");
Daniel Dunbar6cc525b2009-12-08 19:49:51 +0000614 CmdArgs.push_back("-mfloat-abi");
615 CmdArgs.push_back("soft");
Daniel Dunbar78485922009-09-10 23:00:09 +0000616 } else if (FloatABI == "softfp") {
617 // Floating point operations are hard, but argument passing is soft.
Daniel Dunbar6cc525b2009-12-08 19:49:51 +0000618 CmdArgs.push_back("-mfloat-abi");
619 CmdArgs.push_back("soft");
Daniel Dunbar78485922009-09-10 23:00:09 +0000620 } else {
621 // Floating point operations and argument passing are hard.
622 assert(FloatABI == "hard" && "Invalid float abi!");
Daniel Dunbar6cc525b2009-12-08 19:49:51 +0000623 CmdArgs.push_back("-mfloat-abi");
624 CmdArgs.push_back("hard");
Daniel Dunbar78485922009-09-10 23:00:09 +0000625 }
Daniel Dunbar893d4752009-12-19 04:15:38 +0000626
627 // Set appropriate target features for floating point mode.
628 //
629 // FIXME: Note, this is a hack, the LLVM backend doesn't actually use these
630 // yet (it uses the -mfloat-abi and -msoft-float options above), and it is
631 // stripped out by the ARM target.
632
633 // Use software floating point operations?
634 if (FloatABI == "soft") {
635 CmdArgs.push_back("-target-feature");
636 CmdArgs.push_back("+soft-float");
637 }
638
639 // Use software floating point argument passing?
640 if (FloatABI != "hard") {
641 CmdArgs.push_back("-target-feature");
642 CmdArgs.push_back("+soft-float-abi");
643 }
Daniel Dunbar0def3d12009-12-21 23:28:17 +0000644
645 // Honor -mfpu=.
646 //
647 // FIXME: Centralize feature selection, defaulting shouldn't be also in the
648 // frontend target.
649 if (const Arg *A = Args.getLastArg(options::OPT_mfpu_EQ)) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000650 StringRef FPU = A->getValue(Args);
Daniel Dunbar0def3d12009-12-21 23:28:17 +0000651
652 // Set the target features based on the FPU.
653 if (FPU == "fpa" || FPU == "fpe2" || FPU == "fpe3" || FPU == "maverick") {
654 // Disable any default FPU support.
655 CmdArgs.push_back("-target-feature");
656 CmdArgs.push_back("-vfp2");
657 CmdArgs.push_back("-target-feature");
658 CmdArgs.push_back("-vfp3");
659 CmdArgs.push_back("-target-feature");
660 CmdArgs.push_back("-neon");
661 } else if (FPU == "vfp") {
662 CmdArgs.push_back("-target-feature");
663 CmdArgs.push_back("+vfp2");
664 } else if (FPU == "vfp3") {
665 CmdArgs.push_back("-target-feature");
666 CmdArgs.push_back("+vfp3");
667 } else if (FPU == "neon") {
668 CmdArgs.push_back("-target-feature");
669 CmdArgs.push_back("+neon");
670 } else
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000671 D.Diag(diag::err_drv_clang_unsupported) << A->getAsString(Args);
Daniel Dunbar0def3d12009-12-21 23:28:17 +0000672 }
Daniel Dunbarb1db4b62011-03-17 00:07:34 +0000673
674 // Setting -msoft-float effectively disables NEON because of the GCC
675 // implementation, although the same isn't true of VFP or VFP3.
676 if (FloatABI == "soft") {
Daniel Dunbarc9388c12011-03-17 17:10:06 +0000677 CmdArgs.push_back("-target-feature");
678 CmdArgs.push_back("-neon");
679 }
680
681 // Kernel code has more strict alignment requirements.
682 if (KernelOrKext) {
Daniel Dunbar12100e22011-03-22 16:48:17 +0000683 CmdArgs.push_back("-backend-option");
Daniel Dunbarc9388c12011-03-17 17:10:06 +0000684 CmdArgs.push_back("-arm-long-calls");
685
Daniel Dunbar12100e22011-03-22 16:48:17 +0000686 CmdArgs.push_back("-backend-option");
Daniel Dunbarc9388c12011-03-17 17:10:06 +0000687 CmdArgs.push_back("-arm-strict-align");
Daniel Dunbared904c82011-04-18 21:26:42 +0000688
689 // The kext linker doesn't know how to deal with movw/movt.
690#ifndef DISABLE_ARM_DARWIN_USE_MOVT
691 CmdArgs.push_back("-backend-option");
692 CmdArgs.push_back("-arm-darwin-use-movt=0");
693#endif
Daniel Dunbarb1db4b62011-03-17 00:07:34 +0000694 }
Chad Rosierba3df1d2011-08-26 00:26:29 +0000695
696 // Setting -mno-global-merge disables the codegen global merge pass. Setting
697 // -mglobal-merge has no effect as the pass is enabled by default.
698 if (Arg *A = Args.getLastArg(options::OPT_mglobal_merge,
699 options::OPT_mno_global_merge)) {
700 if (A->getOption().matches(options::OPT_mno_global_merge))
701 CmdArgs.push_back("-mno-global-merge");
702 }
Daniel Dunbar0f5c5422009-09-10 04:57:17 +0000703}
704
Eric Christopher0b26a612010-03-02 02:41:08 +0000705void Clang::AddMIPSTargetArgs(const ArgList &Args,
706 ArgStringList &CmdArgs) const {
707 const Driver &D = getToolChain().getDriver();
708
709 // Select the ABI to use.
710 const char *ABIName = 0;
711 if (Arg *A = Args.getLastArg(options::OPT_mabi_EQ)) {
712 ABIName = A->getValue(Args);
713 } else {
714 ABIName = "o32";
715 }
716
717 CmdArgs.push_back("-target-abi");
718 CmdArgs.push_back(ABIName);
719
720 if (const Arg *A = Args.getLastArg(options::OPT_march_EQ)) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000721 StringRef MArch = A->getValue(Args);
Eric Christopher0b26a612010-03-02 02:41:08 +0000722 CmdArgs.push_back("-target-cpu");
723
724 if ((MArch == "r2000") || (MArch == "r3000"))
725 CmdArgs.push_back("mips1");
726 else if (MArch == "r6000")
727 CmdArgs.push_back("mips2");
728 else
Nick Lewycky36d8f052011-05-04 03:44:01 +0000729 CmdArgs.push_back(Args.MakeArgString(MArch));
Eric Christopher0b26a612010-03-02 02:41:08 +0000730 }
731
732 // Select the float ABI as determined by -msoft-float, -mhard-float, and
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000733 StringRef FloatABI;
Eric Christopher0b26a612010-03-02 02:41:08 +0000734 if (Arg *A = Args.getLastArg(options::OPT_msoft_float,
735 options::OPT_mhard_float)) {
736 if (A->getOption().matches(options::OPT_msoft_float))
737 FloatABI = "soft";
738 else if (A->getOption().matches(options::OPT_mhard_float))
739 FloatABI = "hard";
740 }
741
742 // If unspecified, choose the default based on the platform.
743 if (FloatABI.empty()) {
Benjamin Kramerf41ccef2010-04-08 15:44:22 +0000744 // Assume "soft", but warn the user we are guessing.
745 FloatABI = "soft";
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000746 D.Diag(diag::warn_drv_assuming_mfloat_abi_is) << "soft";
Eric Christopher0b26a612010-03-02 02:41:08 +0000747 }
748
749 if (FloatABI == "soft") {
750 // Floating point operations and argument passing are soft.
751 //
752 // FIXME: This changes CPP defines, we need -target-soft-float.
753 CmdArgs.push_back("-msoft-float");
754 } else {
755 assert(FloatABI == "hard" && "Invalid float abi!");
756 CmdArgs.push_back("-mhard-float");
757 }
758}
759
Bruno Cardoso Lopese7f211c2010-11-09 17:21:19 +0000760void Clang::AddSparcTargetArgs(const ArgList &Args,
761 ArgStringList &CmdArgs) const {
762 const Driver &D = getToolChain().getDriver();
763
764 if (const Arg *A = Args.getLastArg(options::OPT_march_EQ)) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000765 StringRef MArch = A->getValue(Args);
Bruno Cardoso Lopese7f211c2010-11-09 17:21:19 +0000766 CmdArgs.push_back("-target-cpu");
767 CmdArgs.push_back(MArch.str().c_str());
768 }
769
770 // Select the float ABI as determined by -msoft-float, -mhard-float, and
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000771 StringRef FloatABI;
Bruno Cardoso Lopese7f211c2010-11-09 17:21:19 +0000772 if (Arg *A = Args.getLastArg(options::OPT_msoft_float,
773 options::OPT_mhard_float)) {
774 if (A->getOption().matches(options::OPT_msoft_float))
775 FloatABI = "soft";
776 else if (A->getOption().matches(options::OPT_mhard_float))
777 FloatABI = "hard";
778 }
779
780 // If unspecified, choose the default based on the platform.
781 if (FloatABI.empty()) {
782 switch (getToolChain().getTriple().getOS()) {
783 default:
784 // Assume "soft", but warn the user we are guessing.
785 FloatABI = "soft";
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000786 D.Diag(diag::warn_drv_assuming_mfloat_abi_is) << "soft";
Bruno Cardoso Lopese7f211c2010-11-09 17:21:19 +0000787 break;
788 }
789 }
790
791 if (FloatABI == "soft") {
792 // Floating point operations and argument passing are soft.
793 //
794 // FIXME: This changes CPP defines, we need -target-soft-float.
795 CmdArgs.push_back("-msoft-float");
Bruno Cardoso Lopese7f211c2010-11-09 17:21:19 +0000796 CmdArgs.push_back("-target-feature");
797 CmdArgs.push_back("+soft-float");
798 } else {
799 assert(FloatABI == "hard" && "Invalid float abi!");
800 CmdArgs.push_back("-mhard-float");
801 }
802}
803
Daniel Dunbar3b3191f2009-09-09 22:33:08 +0000804void Clang::AddX86TargetArgs(const ArgList &Args,
805 ArgStringList &CmdArgs) const {
Daniel Dunbare2cf8f72009-09-10 22:59:57 +0000806 if (!Args.hasFlag(options::OPT_mred_zone,
807 options::OPT_mno_red_zone,
808 true) ||
809 Args.hasArg(options::OPT_mkernel) ||
810 Args.hasArg(options::OPT_fapple_kext))
Daniel Dunbar8bed86c2009-11-20 22:21:36 +0000811 CmdArgs.push_back("-disable-red-zone");
Daniel Dunbare2cf8f72009-09-10 22:59:57 +0000812
Daniel Dunbare2cf8f72009-09-10 22:59:57 +0000813 if (Args.hasFlag(options::OPT_msoft_float,
814 options::OPT_mno_soft_float,
815 false))
Daniel Dunbar8bed86c2009-11-20 22:21:36 +0000816 CmdArgs.push_back("-no-implicit-float");
Daniel Dunbare2cf8f72009-09-10 22:59:57 +0000817
Daniel Dunbare13ada62009-11-14 22:04:54 +0000818 const char *CPUName = 0;
Daniel Dunbar3b3191f2009-09-09 22:33:08 +0000819 if (const Arg *A = Args.getLastArg(options::OPT_march_EQ)) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000820 if (StringRef(A->getValue(Args)) == "native") {
Daniel Dunbare13ada62009-11-14 22:04:54 +0000821 // FIXME: Reject attempts to use -march=native unless the target matches
822 // the host.
823 //
824 // FIXME: We should also incorporate the detected target features for use
825 // with -native.
826 std::string CPU = llvm::sys::getHostCPUName();
827 if (!CPU.empty())
828 CPUName = Args.MakeArgString(CPU);
829 } else
830 CPUName = A->getValue(Args);
831 }
Daniel Dunbar3b3191f2009-09-09 22:33:08 +0000832
Daniel Dunbare13ada62009-11-14 22:04:54 +0000833 // Select the default CPU if none was given (or detection failed).
834 if (!CPUName) {
Daniel Dunbar3b3191f2009-09-09 22:33:08 +0000835 // FIXME: Need target hooks.
Benjamin Kramer842bf172010-01-30 15:01:47 +0000836 if (getToolChain().getOS().startswith("darwin")) {
Daniel Dunbar116b3052011-05-31 15:58:55 +0000837 if (getToolChain().getArch() == llvm::Triple::x86_64)
Daniel Dunbare13ada62009-11-14 22:04:54 +0000838 CPUName = "core2";
Daniel Dunbar116b3052011-05-31 15:58:55 +0000839 else if (getToolChain().getArch() == llvm::Triple::x86)
Daniel Dunbare13ada62009-11-14 22:04:54 +0000840 CPUName = "yonah";
Chris Lattnerb986aba2010-04-11 19:29:39 +0000841 } else if (getToolChain().getOS().startswith("haiku")) {
Daniel Dunbar116b3052011-05-31 15:58:55 +0000842 if (getToolChain().getArch() == llvm::Triple::x86_64)
Chris Lattnerb986aba2010-04-11 19:29:39 +0000843 CPUName = "x86-64";
Daniel Dunbar116b3052011-05-31 15:58:55 +0000844 else if (getToolChain().getArch() == llvm::Triple::x86)
Chris Lattnerb986aba2010-04-11 19:29:39 +0000845 CPUName = "i586";
Daniel Dunbarea3813f2010-08-01 23:13:54 +0000846 } else if (getToolChain().getOS().startswith("openbsd")) {
Daniel Dunbar116b3052011-05-31 15:58:55 +0000847 if (getToolChain().getArch() == llvm::Triple::x86_64)
Daniel Dunbarea3813f2010-08-01 23:13:54 +0000848 CPUName = "x86-64";
Daniel Dunbar116b3052011-05-31 15:58:55 +0000849 else if (getToolChain().getArch() == llvm::Triple::x86)
Daniel Dunbarea3813f2010-08-01 23:13:54 +0000850 CPUName = "i486";
Roman Divacky432f10d2011-03-01 18:11:37 +0000851 } else if (getToolChain().getOS().startswith("freebsd")) {
Daniel Dunbar116b3052011-05-31 15:58:55 +0000852 if (getToolChain().getArch() == llvm::Triple::x86_64)
Roman Divacky432f10d2011-03-01 18:11:37 +0000853 CPUName = "x86-64";
Daniel Dunbar116b3052011-05-31 15:58:55 +0000854 else if (getToolChain().getArch() == llvm::Triple::x86)
Roman Divacky432f10d2011-03-01 18:11:37 +0000855 CPUName = "i486";
Benjamin Kramer24f1d3e2011-02-02 18:59:27 +0000856 } else if (getToolChain().getOS().startswith("netbsd")) {
Daniel Dunbar116b3052011-05-31 15:58:55 +0000857 if (getToolChain().getArch() == llvm::Triple::x86_64)
Benjamin Kramer24f1d3e2011-02-02 18:59:27 +0000858 CPUName = "x86-64";
Daniel Dunbar116b3052011-05-31 15:58:55 +0000859 else if (getToolChain().getArch() == llvm::Triple::x86)
Benjamin Kramer24f1d3e2011-02-02 18:59:27 +0000860 CPUName = "i486";
Daniel Dunbar3b3191f2009-09-09 22:33:08 +0000861 } else {
Daniel Dunbar116b3052011-05-31 15:58:55 +0000862 if (getToolChain().getArch() == llvm::Triple::x86_64)
Daniel Dunbare13ada62009-11-14 22:04:54 +0000863 CPUName = "x86-64";
Daniel Dunbar116b3052011-05-31 15:58:55 +0000864 else if (getToolChain().getArch() == llvm::Triple::x86)
Daniel Dunbare13ada62009-11-14 22:04:54 +0000865 CPUName = "pentium4";
Daniel Dunbar3b3191f2009-09-09 22:33:08 +0000866 }
867 }
868
Daniel Dunbare13ada62009-11-14 22:04:54 +0000869 if (CPUName) {
Daniel Dunbara7d02312009-12-18 06:30:12 +0000870 CmdArgs.push_back("-target-cpu");
Daniel Dunbare13ada62009-11-14 22:04:54 +0000871 CmdArgs.push_back(CPUName);
872 }
873
Eli Friedmanad811f02011-07-02 00:34:19 +0000874 // The required algorithm here is slightly strange: the options are applied
875 // in order (so -mno-sse -msse2 disables SSE3), but any option that gets
876 // directly overridden later is ignored (so "-mno-sse -msse2 -mno-sse2 -msse"
877 // is equivalent to "-mno-sse2 -msse"). The -cc1 handling deals with the
878 // former correctly, but not the latter; handle directly-overridden
879 // attributes here.
880 llvm::StringMap<unsigned> PrevFeature;
881 std::vector<const char*> Features;
Daniel Dunbar44b36ee2009-11-25 11:53:23 +0000882 for (arg_iterator it = Args.filtered_begin(options::OPT_m_x86_Features_Group),
883 ie = Args.filtered_end(); it != ie; ++it) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000884 StringRef Name = (*it)->getOption().getName();
Daniel Dunbara442fd52010-06-11 22:00:13 +0000885 (*it)->claim();
Daniel Dunbar3b3191f2009-09-09 22:33:08 +0000886
Daniel Dunbar44b36ee2009-11-25 11:53:23 +0000887 // Skip over "-m".
888 assert(Name.startswith("-m") && "Invalid feature name.");
889 Name = Name.substr(2);
Daniel Dunbar3b3191f2009-09-09 22:33:08 +0000890
Daniel Dunbar44b36ee2009-11-25 11:53:23 +0000891 bool IsNegative = Name.startswith("no-");
892 if (IsNegative)
893 Name = Name.substr(3);
Daniel Dunbar3b3191f2009-09-09 22:33:08 +0000894
Eli Friedmanad811f02011-07-02 00:34:19 +0000895 unsigned& Prev = PrevFeature[Name];
896 if (Prev)
897 Features[Prev - 1] = 0;
898 Prev = Features.size() + 1;
899 Features.push_back(Args.MakeArgString((IsNegative ? "-" : "+") + Name));
900 }
901 for (unsigned i = 0; i < Features.size(); i++) {
902 if (Features[i]) {
903 CmdArgs.push_back("-target-feature");
904 CmdArgs.push_back(Features[i]);
905 }
Daniel Dunbar3b3191f2009-09-09 22:33:08 +0000906 }
907}
908
Eric Christopher84fbdb42011-08-19 00:30:14 +0000909static bool
John McCallb5f652e2011-06-22 00:53:57 +0000910shouldUseExceptionTablesForObjCExceptions(unsigned objcABIVersion,
Anders Carlsson246ff3f2011-02-28 00:44:51 +0000911 const llvm::Triple &Triple) {
912 // We use the zero-cost exception tables for Objective-C if the non-fragile
913 // ABI is enabled or when compiling for x86_64 and ARM on Snow Leopard and
914 // later.
915
John McCallb5f652e2011-06-22 00:53:57 +0000916 if (objcABIVersion >= 2)
Anders Carlsson246ff3f2011-02-28 00:44:51 +0000917 return true;
918
919 if (Triple.getOS() != llvm::Triple::Darwin)
920 return false;
921
Eric Christopherbf15d2b2011-07-02 00:20:22 +0000922 return (!Triple.isMacOSXVersionLT(10,5) &&
Anders Carlsson246ff3f2011-02-28 00:44:51 +0000923 (Triple.getArch() == llvm::Triple::x86_64 ||
Eric Christopher84fbdb42011-08-19 00:30:14 +0000924 Triple.getArch() == llvm::Triple::arm));
Anders Carlsson246ff3f2011-02-28 00:44:51 +0000925}
926
Anders Carlssone96ab552011-02-28 02:27:16 +0000927/// addExceptionArgs - Adds exception related arguments to the driver command
928/// arguments. There's a master flag, -fexceptions and also language specific
929/// flags to enable/disable C++ and Objective-C exceptions.
930/// This makes it possible to for example disable C++ exceptions but enable
931/// Objective-C exceptions.
932static void addExceptionArgs(const ArgList &Args, types::ID InputType,
933 const llvm::Triple &Triple,
934 bool KernelOrKext, bool IsRewriter,
John McCallb5f652e2011-06-22 00:53:57 +0000935 unsigned objcABIVersion,
Anders Carlssone96ab552011-02-28 02:27:16 +0000936 ArgStringList &CmdArgs) {
937 if (KernelOrKext)
938 return;
939
940 // Exceptions are enabled by default.
941 bool ExceptionsEnabled = true;
942
943 // This keeps track of whether exceptions were explicitly turned on or off.
944 bool DidHaveExplicitExceptionFlag = false;
945
Rafael Espindola00a66572009-10-01 13:33:33 +0000946 if (Arg *A = Args.getLastArg(options::OPT_fexceptions,
947 options::OPT_fno_exceptions)) {
948 if (A->getOption().matches(options::OPT_fexceptions))
Anders Carlssone96ab552011-02-28 02:27:16 +0000949 ExceptionsEnabled = true;
Eric Christopher84fbdb42011-08-19 00:30:14 +0000950 else
Anders Carlssone96ab552011-02-28 02:27:16 +0000951 ExceptionsEnabled = false;
952
953 DidHaveExplicitExceptionFlag = true;
Rafael Espindola00a66572009-10-01 13:33:33 +0000954 }
Daniel Dunbar30a12b82010-09-14 23:12:31 +0000955
Anders Carlssone96ab552011-02-28 02:27:16 +0000956 bool ShouldUseExceptionTables = false;
Fariborz Jahaniane4b21ab2009-10-01 20:30:46 +0000957
Anders Carlssone96ab552011-02-28 02:27:16 +0000958 // Exception tables and cleanups can be enabled with -fexceptions even if the
959 // language itself doesn't support exceptions.
960 if (ExceptionsEnabled && DidHaveExplicitExceptionFlag)
961 ShouldUseExceptionTables = true;
Daniel Dunbar30a12b82010-09-14 23:12:31 +0000962
Daniel Dunbarc44f8cf2011-03-17 23:28:31 +0000963 // Obj-C exceptions are enabled by default, regardless of -fexceptions. This
964 // is not necessarily sensible, but follows GCC.
965 if (types::isObjC(InputType) &&
Eric Christopher84fbdb42011-08-19 00:30:14 +0000966 Args.hasFlag(options::OPT_fobjc_exceptions,
Daniel Dunbarc44f8cf2011-03-17 23:28:31 +0000967 options::OPT_fno_objc_exceptions,
968 true)) {
969 CmdArgs.push_back("-fobjc-exceptions");
Anders Carlssone96ab552011-02-28 02:27:16 +0000970
Eric Christopher84fbdb42011-08-19 00:30:14 +0000971 ShouldUseExceptionTables |=
John McCallb5f652e2011-06-22 00:53:57 +0000972 shouldUseExceptionTablesForObjCExceptions(objcABIVersion, Triple);
Anders Carlssone96ab552011-02-28 02:27:16 +0000973 }
974
975 if (types::isCXX(InputType)) {
976 bool CXXExceptionsEnabled = ExceptionsEnabled;
977
Eric Christopher84fbdb42011-08-19 00:30:14 +0000978 if (Arg *A = Args.getLastArg(options::OPT_fcxx_exceptions,
979 options::OPT_fno_cxx_exceptions,
Anders Carlssone96ab552011-02-28 02:27:16 +0000980 options::OPT_fexceptions,
981 options::OPT_fno_exceptions)) {
982 if (A->getOption().matches(options::OPT_fcxx_exceptions))
983 CXXExceptionsEnabled = true;
Chandler Carruth74f87112011-02-28 07:25:18 +0000984 else if (A->getOption().matches(options::OPT_fno_cxx_exceptions))
Anders Carlssone96ab552011-02-28 02:27:16 +0000985 CXXExceptionsEnabled = false;
986 }
987
988 if (CXXExceptionsEnabled) {
989 CmdArgs.push_back("-fcxx-exceptions");
990
991 ShouldUseExceptionTables = true;
992 }
993 }
994
995 if (ShouldUseExceptionTables)
996 CmdArgs.push_back("-fexceptions");
Rafael Espindola00a66572009-10-01 13:33:33 +0000997}
998
Rafael Espindola4cfa7972011-05-02 17:43:32 +0000999static bool ShouldDisableCFI(const ArgList &Args,
1000 const ToolChain &TC) {
Rafael Espindolaf934f982011-05-17 16:26:17 +00001001 if (TC.getTriple().getOS() == llvm::Triple::Darwin) {
1002 // The native darwin assembler doesn't support cfi directives, so
Rafael Espindola35ab91c2011-05-17 19:06:58 +00001003 // we disable them if we think the .s file will be passed to it.
Rafael Espindola4cfa7972011-05-02 17:43:32 +00001004
Rafael Espindolaf934f982011-05-17 16:26:17 +00001005 // FIXME: Duplicated code with ToolChains.cpp
1006 // FIXME: This doesn't belong here, but ideally we will support static soon
1007 // anyway.
1008 bool HasStatic = (Args.hasArg(options::OPT_mkernel) ||
1009 Args.hasArg(options::OPT_static) ||
1010 Args.hasArg(options::OPT_fapple_kext));
1011 bool IsIADefault = TC.IsIntegratedAssemblerDefault() && !HasStatic;
1012 bool UseIntegratedAs = Args.hasFlag(options::OPT_integrated_as,
1013 options::OPT_no_integrated_as,
1014 IsIADefault);
1015 bool UseCFI = Args.hasFlag(options::OPT_fdwarf2_cfi_asm,
1016 options::OPT_fno_dwarf2_cfi_asm,
1017 UseIntegratedAs);
1018 return !UseCFI;
1019 }
1020
1021 // For now we assume that every other assembler support CFI.
1022 return false;
Rafael Espindola4cfa7972011-05-02 17:43:32 +00001023}
1024
Joerg Sonnenbergeref317a22011-05-06 14:35:16 +00001025/// \brief Check whether the given input tree contains any compilation actions.
1026static bool ContainsCompileAction(const Action *A) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001027 if (isa<CompileJobAction>(A))
Joerg Sonnenbergeref317a22011-05-06 14:35:16 +00001028 return true;
1029
1030 for (Action::const_iterator it = A->begin(), ie = A->end(); it != ie; ++it)
1031 if (ContainsCompileAction(*it))
1032 return true;
1033
1034 return false;
1035}
1036
1037/// \brief Check if -relax-all should be passed to the internal assembler.
1038/// This is done by default when compiling non-assembler source with -O0.
1039static bool UseRelaxAll(Compilation &C, const ArgList &Args) {
1040 bool RelaxDefault = true;
1041
1042 if (Arg *A = Args.getLastArg(options::OPT_O_Group))
1043 RelaxDefault = A->getOption().matches(options::OPT_O0);
1044
1045 if (RelaxDefault) {
1046 RelaxDefault = false;
1047 for (ActionList::const_iterator it = C.getActions().begin(),
1048 ie = C.getActions().end(); it != ie; ++it) {
1049 if (ContainsCompileAction(*it)) {
1050 RelaxDefault = true;
1051 break;
1052 }
1053 }
1054 }
1055
1056 return Args.hasFlag(options::OPT_mrelax_all, options::OPT_mno_relax_all,
1057 RelaxDefault);
1058}
1059
Daniel Dunbar1a093d22009-03-18 06:00:36 +00001060void Clang::ConstructJob(Compilation &C, const JobAction &JA,
Daniel Dunbar04c4c2c2009-03-18 07:06:02 +00001061 const InputInfo &Output,
Daniel Dunbar0450e6d2009-03-18 06:07:59 +00001062 const InputInfoList &Inputs,
Daniel Dunbara2aedc62009-03-18 10:01:51 +00001063 const ArgList &Args,
Daniel Dunbar1a093d22009-03-18 06:00:36 +00001064 const char *LinkingOutput) const {
Daniel Dunbare46b52a2010-03-20 04:52:14 +00001065 bool KernelOrKext = Args.hasArg(options::OPT_mkernel,
1066 options::OPT_fapple_kext);
Daniel Dunbar083edf72009-12-21 18:54:17 +00001067 const Driver &D = getToolChain().getDriver();
Daniel Dunbara3246a02009-03-18 08:07:30 +00001068 ArgStringList CmdArgs;
1069
Daniel Dunbare521a892009-03-31 20:53:55 +00001070 assert(Inputs.size() == 1 && "Unable to handle multiple inputs.");
1071
Daniel Dunbar6c536aa2009-12-11 23:00:49 +00001072 // Invoke ourselves in -cc1 mode.
1073 //
1074 // FIXME: Implement custom jobs for internal actions.
1075 CmdArgs.push_back("-cc1");
1076
Daniel Dunbar624c21b2009-10-30 18:12:20 +00001077 // Add the "effective" target triple.
Daniel Dunbard640be22009-03-31 17:35:15 +00001078 CmdArgs.push_back("-triple");
Daniel Dunbar82eb4ce2010-08-23 22:35:37 +00001079 std::string TripleStr = getToolChain().ComputeEffectiveClangTriple(Args);
Daniel Dunbar624c21b2009-10-30 18:12:20 +00001080 CmdArgs.push_back(Args.MakeArgString(TripleStr));
Daniel Dunbarfb58b0a2009-09-10 06:49:20 +00001081
Daniel Dunbar624c21b2009-10-30 18:12:20 +00001082 // Select the appropriate action.
Daniel Dunbar99b55242010-07-19 19:44:22 +00001083 bool IsRewriter = false;
Daniel Dunbara2aedc62009-03-18 10:01:51 +00001084 if (isa<AnalyzeJobAction>(JA)) {
1085 assert(JA.getType() == types::TY_Plist && "Invalid output type.");
1086 CmdArgs.push_back("-analyze");
1087 } else if (isa<PreprocessJobAction>(JA)) {
Daniel Dunbard67a3222009-03-30 06:36:42 +00001088 if (Output.getType() == types::TY_Dependencies)
1089 CmdArgs.push_back("-Eonly");
1090 else
1091 CmdArgs.push_back("-E");
Daniel Dunbarc4343942010-02-03 03:07:56 +00001092 } else if (isa<AssembleJobAction>(JA)) {
1093 CmdArgs.push_back("-emit-obj");
Daniel Dunbar06e2cc32010-05-27 06:18:05 +00001094
Joerg Sonnenbergeref317a22011-05-06 14:35:16 +00001095 if (UseRelaxAll(C, Args))
Daniel Dunbar06e2cc32010-05-27 06:18:05 +00001096 CmdArgs.push_back("-mrelax-all");
Daniel Dunbarfe6c97b2010-08-24 16:47:49 +00001097
Daniel Dunbar2b4de142010-10-18 22:36:15 +00001098 // When using an integrated assembler, translate -Wa, and -Xassembler
1099 // options.
1100 for (arg_iterator it = Args.filtered_begin(options::OPT_Wa_COMMA,
1101 options::OPT_Xassembler),
1102 ie = Args.filtered_end(); it != ie; ++it) {
1103 const Arg *A = *it;
1104 A->claim();
1105
1106 for (unsigned i = 0, e = A->getNumValues(); i != e; ++i) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001107 StringRef Value = A->getValue(Args, i);
Daniel Dunbar2b4de142010-10-18 22:36:15 +00001108
1109 if (Value == "-force_cpusubtype_ALL") {
1110 // Do nothing, this is the default and we don't support anything else.
Daniel Dunbara78e5892010-10-28 20:36:23 +00001111 } else if (Value == "-L") {
Daniel Dunbar67919b22011-03-28 22:49:28 +00001112 CmdArgs.push_back("-msave-temp-labels");
Joerg Sonnenberger3028e462011-05-19 20:46:39 +00001113 } else if (Value == "--fatal-warnings") {
Joerg Sonnenbergerb487d2d2011-05-19 18:42:29 +00001114 CmdArgs.push_back("-mllvm");
1115 CmdArgs.push_back("-fatal-assembler-warnings");
Nick Lewyckyca6b90d2011-06-21 00:14:18 +00001116 } else if (Value == "--noexecstack") {
1117 CmdArgs.push_back("-mnoexecstack");
Daniel Dunbar2b4de142010-10-18 22:36:15 +00001118 } else {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001119 D.Diag(diag::err_drv_unsupported_option_argument)
Daniel Dunbar2b4de142010-10-18 22:36:15 +00001120 << A->getOption().getName() << Value;
1121 }
1122 }
1123 }
Daniel Dunbar7c874332010-11-19 16:23:35 +00001124
1125 // Also ignore explicit -force_cpusubtype_ALL option.
1126 (void) Args.hasArg(options::OPT_force__cpusubtype__ALL);
Daniel Dunbara2aedc62009-03-18 10:01:51 +00001127 } else if (isa<PrecompileJobAction>(JA)) {
Argyrios Kyrtzidis90bdfbb2010-08-11 23:27:58 +00001128 // Use PCH if the user requested it.
Daniel Dunbarcbc34b72009-10-15 20:02:44 +00001129 bool UsePCH = D.CCCUsePCH;
Daniel Dunbarcbc34b72009-10-15 20:02:44 +00001130
1131 if (UsePCH)
Douglas Gregor111af7d2009-04-18 00:34:01 +00001132 CmdArgs.push_back("-emit-pch");
1133 else
1134 CmdArgs.push_back("-emit-pth");
Daniel Dunbara2aedc62009-03-18 10:01:51 +00001135 } else {
1136 assert(isa<CompileJobAction>(JA) && "Invalid action for clang tool.");
Daniel Dunbard067f7f2009-04-08 23:54:23 +00001137
Daniel Dunbara2aedc62009-03-18 10:01:51 +00001138 if (JA.getType() == types::TY_Nothing) {
1139 CmdArgs.push_back("-fsyntax-only");
Daniel Dunbar24e52992010-06-07 23:28:45 +00001140 } else if (JA.getType() == types::TY_LLVM_IR ||
1141 JA.getType() == types::TY_LTO_IR) {
Daniel Dunbara2aedc62009-03-18 10:01:51 +00001142 CmdArgs.push_back("-emit-llvm");
Daniel Dunbar24e52992010-06-07 23:28:45 +00001143 } else if (JA.getType() == types::TY_LLVM_BC ||
1144 JA.getType() == types::TY_LTO_BC) {
Daniel Dunbara2aedc62009-03-18 10:01:51 +00001145 CmdArgs.push_back("-emit-llvm-bc");
1146 } else if (JA.getType() == types::TY_PP_Asm) {
Daniel Dunbard112f102009-09-17 00:47:53 +00001147 CmdArgs.push_back("-S");
Daniel Dunbar6cdf83c2009-09-01 16:57:46 +00001148 } else if (JA.getType() == types::TY_AST) {
1149 CmdArgs.push_back("-emit-pch");
Daniel Dunbaraeea8ac2010-02-11 03:16:21 +00001150 } else if (JA.getType() == types::TY_RewrittenObjC) {
1151 CmdArgs.push_back("-rewrite-objc");
Daniel Dunbar99b55242010-07-19 19:44:22 +00001152 IsRewriter = true;
Daniel Dunbaraeea8ac2010-02-11 03:16:21 +00001153 } else {
1154 assert(JA.getType() == types::TY_PP_Asm &&
1155 "Unexpected output type!");
Daniel Dunbara2aedc62009-03-18 10:01:51 +00001156 }
Daniel Dunbara3246a02009-03-18 08:07:30 +00001157 }
1158
Daniel Dunbara2aedc62009-03-18 10:01:51 +00001159 // The make clang go fast button.
1160 CmdArgs.push_back("-disable-free");
1161
John McCallbb79b5f2010-02-13 03:50:24 +00001162 // Disable the verification pass in -asserts builds.
1163#ifdef NDEBUG
1164 CmdArgs.push_back("-disable-llvm-verifier");
1165#endif
1166
Daniel Dunbar3b358a32009-04-08 05:11:16 +00001167 // Set the main file name, so that debug info works even with
1168 // -save-temps.
1169 CmdArgs.push_back("-main-file-name");
1170 CmdArgs.push_back(darwin::CC1::getBaseInputName(Args, Inputs));
1171
Daniel Dunbar17ddaa62009-04-08 18:03:55 +00001172 // Some flags which affect the language (via preprocessor
1173 // defines). See darwin::CC1::AddCPPArgs.
1174 if (Args.hasArg(options::OPT_static))
1175 CmdArgs.push_back("-static-define");
1176
Daniel Dunbara2aedc62009-03-18 10:01:51 +00001177 if (isa<AnalyzeJobAction>(JA)) {
Ted Kremenek05e6f5b2009-09-25 05:55:59 +00001178 // Enable region store model by default.
1179 CmdArgs.push_back("-analyzer-store=region");
1180
Ted Kremenek7bea9a12009-12-07 22:26:14 +00001181 // Treat blocks as analysis entry points.
1182 CmdArgs.push_back("-analyzer-opt-analyze-nested-blocks");
1183
Ted Kremenek49c79792011-03-24 00:28:47 +00001184 CmdArgs.push_back("-analyzer-eagerly-assume");
1185
Daniel Dunbara2aedc62009-03-18 10:01:51 +00001186 // Add default argument set.
Daniel Dunbar58f345ce2009-05-22 00:38:15 +00001187 if (!Args.hasArg(options::OPT__analyzer_no_default_checks)) {
Argyrios Kyrtzidisa6d04d52011-02-15 07:42:33 +00001188 CmdArgs.push_back("-analyzer-checker=core");
Ted Kremenek49c79792011-03-24 00:28:47 +00001189 CmdArgs.push_back("-analyzer-checker=deadcode");
1190 CmdArgs.push_back("-analyzer-checker=security");
1191
Argyrios Kyrtzidisa6d04d52011-02-15 07:42:33 +00001192 if (getToolChain().getTriple().getOS() != llvm::Triple::Win32)
1193 CmdArgs.push_back("-analyzer-checker=unix");
Ted Kremenek49c79792011-03-24 00:28:47 +00001194
Argyrios Kyrtzidisa6d04d52011-02-15 07:42:33 +00001195 if (getToolChain().getTriple().getVendor() == llvm::Triple::Apple)
Ted Kremenek49c79792011-03-24 00:28:47 +00001196 CmdArgs.push_back("-analyzer-checker=osx");
Daniel Dunbar58f345ce2009-05-22 00:38:15 +00001197 }
Daniel Dunbard067f7f2009-04-08 23:54:23 +00001198
Daniel Dunbar58f345ce2009-05-22 00:38:15 +00001199 // Set the output format. The default is plist, for (lame) historical
1200 // reasons.
1201 CmdArgs.push_back("-analyzer-output");
1202 if (Arg *A = Args.getLastArg(options::OPT__analyzer_output))
1203 CmdArgs.push_back(A->getValue(Args));
1204 else
1205 CmdArgs.push_back("plist");
Daniel Dunbara2aedc62009-03-18 10:01:51 +00001206
Ted Kremenekfe449a22010-03-22 22:32:05 +00001207 // Disable the presentation of standard compiler warnings when
1208 // using --analyze. We only want to show static analyzer diagnostics
1209 // or frontend errors.
1210 CmdArgs.push_back("-w");
1211
Daniel Dunbara2aedc62009-03-18 10:01:51 +00001212 // Add -Xanalyzer arguments when running as analyzer.
1213 Args.AddAllArgValues(CmdArgs, options::OPT_Xanalyzer);
Mike Stump11289f42009-09-09 15:08:12 +00001214 }
1215
Daniel Dunbar4eadb602009-09-10 01:21:12 +00001216 CheckCodeGenerationOptions(D, Args);
1217
Daniel Dunbar44e71222009-04-29 18:32:25 +00001218 // Perform argument translation for LLVM backend. This
1219 // takes some care in reconciling with llvm-gcc. The
1220 // issue is that llvm-gcc translates these options based on
1221 // the values in cc1, whereas we are processing based on
1222 // the driver arguments.
Daniel Dunbard067f7f2009-04-08 23:54:23 +00001223
Daniel Dunbar44e71222009-04-29 18:32:25 +00001224 // This comes from the default translation the driver + cc1
1225 // would do to enable flag_pic.
1226 //
1227 // FIXME: Centralize this code.
1228 bool PICEnabled = (Args.hasArg(options::OPT_fPIC) ||
1229 Args.hasArg(options::OPT_fpic) ||
1230 Args.hasArg(options::OPT_fPIE) ||
1231 Args.hasArg(options::OPT_fpie));
1232 bool PICDisabled = (Args.hasArg(options::OPT_mkernel) ||
1233 Args.hasArg(options::OPT_static));
1234 const char *Model = getToolChain().GetForcedPicModel();
1235 if (!Model) {
1236 if (Args.hasArg(options::OPT_mdynamic_no_pic))
1237 Model = "dynamic-no-pic";
1238 else if (PICDisabled)
1239 Model = "static";
1240 else if (PICEnabled)
1241 Model = "pic";
Daniel Dunbara2aedc62009-03-18 10:01:51 +00001242 else
Daniel Dunbar44e71222009-04-29 18:32:25 +00001243 Model = getToolChain().GetDefaultRelocationModel();
Daniel Dunbara2aedc62009-03-18 10:01:51 +00001244 }
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001245 if (StringRef(Model) != "pic") {
Daniel Dunbara1b02a22009-11-29 07:18:39 +00001246 CmdArgs.push_back("-mrelocation-model");
1247 CmdArgs.push_back(Model);
1248 }
Daniel Dunbar44e71222009-04-29 18:32:25 +00001249
1250 // Infer the __PIC__ value.
1251 //
1252 // FIXME: This isn't quite right on Darwin, which always sets
1253 // __PIC__=2.
1254 if (strcmp(Model, "pic") == 0 || strcmp(Model, "dynamic-no-pic") == 0) {
Daniel Dunbar5fe08662009-11-29 02:39:08 +00001255 CmdArgs.push_back("-pic-level");
1256 CmdArgs.push_back(Args.hasArg(options::OPT_fPIC) ? "2" : "1");
Daniel Dunbar44e71222009-04-29 18:32:25 +00001257 }
Tanya Lattnerf9d41df2009-11-04 01:18:09 +00001258 if (!Args.hasFlag(options::OPT_fmerge_all_constants,
1259 options::OPT_fno_merge_all_constants))
Chris Lattner9242b332011-04-08 18:06:54 +00001260 CmdArgs.push_back("-fno-merge-all-constants");
Daniel Dunbar306945d2009-09-16 06:17:29 +00001261
Daniel Dunbara1b02a22009-11-29 07:18:39 +00001262 // LLVM Code Generator Options.
1263
Daniel Dunbar0bb03312011-02-09 17:54:19 +00001264 if (Arg *A = Args.getLastArg(options::OPT_mregparm_EQ)) {
1265 CmdArgs.push_back("-mregparm");
1266 CmdArgs.push_back(A->getValue(Args));
1267 }
1268
Roman Divacky65b88cd2011-03-01 17:40:53 +00001269 if (Args.hasFlag(options::OPT_mrtd, options::OPT_mno_rtd, false))
1270 CmdArgs.push_back("-mrtd");
1271
Daniel Dunbara1b02a22009-11-29 07:18:39 +00001272 // FIXME: Set --enable-unsafe-fp-math.
1273 if (Args.hasFlag(options::OPT_fno_omit_frame_pointer,
1274 options::OPT_fomit_frame_pointer))
1275 CmdArgs.push_back("-mdisable-fp-elim");
1276 if (!Args.hasFlag(options::OPT_fzero_initialized_in_bss,
1277 options::OPT_fno_zero_initialized_in_bss))
1278 CmdArgs.push_back("-mno-zero-initialized-in-bss");
Daniel Dunbar7aa71f92011-02-04 02:20:39 +00001279 if (!Args.hasFlag(options::OPT_fstrict_aliasing,
1280 options::OPT_fno_strict_aliasing,
1281 getToolChain().IsStrictAliasingDefault()))
Dan Gohman10169b92010-10-14 22:36:56 +00001282 CmdArgs.push_back("-relaxed-aliasing");
Daniel Dunbar0d8ca9e2010-05-14 22:00:22 +00001283
1284 // Decide whether to use verbose asm. Verbose assembly is the default on
1285 // toolchains which have the integrated assembler on by default.
1286 bool IsVerboseAsmDefault = getToolChain().IsIntegratedAssemblerDefault();
1287 if (Args.hasFlag(options::OPT_fverbose_asm, options::OPT_fno_verbose_asm,
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001288 IsVerboseAsmDefault) ||
Daniel Dunbar0d8ca9e2010-05-14 22:00:22 +00001289 Args.hasArg(options::OPT_dA))
Daniel Dunbara1b02a22009-11-29 07:18:39 +00001290 CmdArgs.push_back("-masm-verbose");
Daniel Dunbar0d8ca9e2010-05-14 22:00:22 +00001291
Daniel Dunbara1b02a22009-11-29 07:18:39 +00001292 if (Args.hasArg(options::OPT_fdebug_pass_structure)) {
1293 CmdArgs.push_back("-mdebug-pass");
1294 CmdArgs.push_back("Structure");
1295 }
1296 if (Args.hasArg(options::OPT_fdebug_pass_arguments)) {
1297 CmdArgs.push_back("-mdebug-pass");
1298 CmdArgs.push_back("Arguments");
1299 }
1300
John McCall8517abc2010-02-19 02:45:38 +00001301 // Enable -mconstructor-aliases except on darwin, where we have to
1302 // work around a linker bug; see <rdar://problem/7651567>.
1303 if (getToolChain().getTriple().getOS() != llvm::Triple::Darwin)
1304 CmdArgs.push_back("-mconstructor-aliases");
NAKAMURA Takumi029d74b2011-02-17 08:50:50 +00001305
John McCall7ef5cb32011-03-18 02:56:14 +00001306 // Darwin's kernel doesn't support guard variables; just die if we
1307 // try to use them.
1308 if (KernelOrKext &&
1309 getToolChain().getTriple().getOS() == llvm::Triple::Darwin)
1310 CmdArgs.push_back("-fforbid-guard-variables");
1311
Douglas Gregordbe39272011-02-01 15:15:22 +00001312 if (Args.hasArg(options::OPT_mms_bitfields)) {
1313 CmdArgs.push_back("-mms-bitfields");
1314 }
John McCall8517abc2010-02-19 02:45:38 +00001315
Daniel Dunbar306945d2009-09-16 06:17:29 +00001316 // This is a coarse approximation of what llvm-gcc actually does, both
1317 // -fasynchronous-unwind-tables and -fnon-call-exceptions interact in more
1318 // complicated ways.
1319 bool AsynchronousUnwindTables =
1320 Args.hasFlag(options::OPT_fasynchronous_unwind_tables,
1321 options::OPT_fno_asynchronous_unwind_tables,
1322 getToolChain().IsUnwindTablesDefault() &&
Daniel Dunbare46b52a2010-03-20 04:52:14 +00001323 !KernelOrKext);
Daniel Dunbar306945d2009-09-16 06:17:29 +00001324 if (Args.hasFlag(options::OPT_funwind_tables, options::OPT_fno_unwind_tables,
1325 AsynchronousUnwindTables))
Daniel Dunbara1b02a22009-11-29 07:18:39 +00001326 CmdArgs.push_back("-munwind-tables");
1327
1328 if (Arg *A = Args.getLastArg(options::OPT_flimited_precision_EQ)) {
1329 CmdArgs.push_back("-mlimit-float-precision");
1330 CmdArgs.push_back(A->getValue(Args));
1331 }
Daniel Dunbar44e71222009-04-29 18:32:25 +00001332
Daniel Dunbar4dbaaa62009-05-06 03:16:41 +00001333 // FIXME: Handle -mtune=.
1334 (void) Args.hasArg(options::OPT_mtune_EQ);
Daniel Dunbar44e71222009-04-29 18:32:25 +00001335
Benjamin Kramercf4371a2009-08-05 14:30:52 +00001336 if (Arg *A = Args.getLastArg(options::OPT_mcmodel_EQ)) {
Daniel Dunbara1b02a22009-11-29 07:18:39 +00001337 CmdArgs.push_back("-mcode-model");
Benjamin Kramercf4371a2009-08-05 14:30:52 +00001338 CmdArgs.push_back(A->getValue(Args));
1339 }
1340
Daniel Dunbar3b3191f2009-09-09 22:33:08 +00001341 // Add target specific cpu and features flags.
1342 switch(getToolChain().getTriple().getArch()) {
1343 default:
1344 break;
Daniel Dunbar4dbaaa62009-05-06 03:16:41 +00001345
Daniel Dunbar0f5c5422009-09-10 04:57:17 +00001346 case llvm::Triple::arm:
1347 case llvm::Triple::thumb:
Daniel Dunbarc9388c12011-03-17 17:10:06 +00001348 AddARMTargetArgs(Args, CmdArgs, KernelOrKext);
Daniel Dunbar0f5c5422009-09-10 04:57:17 +00001349 break;
1350
Eric Christopher0b26a612010-03-02 02:41:08 +00001351 case llvm::Triple::mips:
1352 case llvm::Triple::mipsel:
1353 AddMIPSTargetArgs(Args, CmdArgs);
1354 break;
1355
Bruno Cardoso Lopese7f211c2010-11-09 17:21:19 +00001356 case llvm::Triple::sparc:
1357 AddSparcTargetArgs(Args, CmdArgs);
1358 break;
1359
Daniel Dunbar3b3191f2009-09-09 22:33:08 +00001360 case llvm::Triple::x86:
1361 case llvm::Triple::x86_64:
1362 AddX86TargetArgs(Args, CmdArgs);
1363 break;
Daniel Dunbar44e71222009-04-29 18:32:25 +00001364 }
1365
Daniel Dunbar976a2f52010-08-11 23:07:47 +00001366 // Pass the linker version in use.
1367 if (Arg *A = Args.getLastArg(options::OPT_mlinker_version_EQ)) {
1368 CmdArgs.push_back("-target-linker-version");
1369 CmdArgs.push_back(A->getValue(Args));
1370 }
1371
Nick Lewycky75033772011-02-02 06:43:03 +00001372 // -mno-omit-leaf-frame-pointer is the default on Darwin.
Daniel Dunbarbb7ac522010-07-01 01:31:45 +00001373 if (Args.hasFlag(options::OPT_momit_leaf_frame_pointer,
Nick Lewycky75033772011-02-02 06:43:03 +00001374 options::OPT_mno_omit_leaf_frame_pointer,
1375 getToolChain().getTriple().getOS() != llvm::Triple::Darwin))
Daniel Dunbarbb7ac522010-07-01 01:31:45 +00001376 CmdArgs.push_back("-momit-leaf-frame-pointer");
1377
Dan Gohmand1e76b92010-01-08 02:20:44 +00001378 // -fno-math-errno is default.
1379 if (Args.hasFlag(options::OPT_fmath_errno,
Daniel Dunbar44e71222009-04-29 18:32:25 +00001380 options::OPT_fno_math_errno,
Dan Gohmand1e76b92010-01-08 02:20:44 +00001381 false))
1382 CmdArgs.push_back("-fmath-errno");
Daniel Dunbar44e71222009-04-29 18:32:25 +00001383
Daniel Dunbarfcc49a82010-05-12 18:19:58 +00001384 // Explicitly error on some things we know we don't support and can't just
1385 // ignore.
1386 types::ID InputType = Inputs[0].getType();
Daniel Dunbar4ed214a2010-09-24 19:39:37 +00001387 if (!Args.hasArg(options::OPT_fallow_unsupported)) {
1388 Arg *Unsupported;
Peter Collingbourne77b0e7f22011-07-12 19:35:15 +00001389 if ((Unsupported = Args.getLastArg(options::OPT_iframework)))
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001390 D.Diag(diag::err_drv_clang_unsupported)
Daniel Dunbarfcc49a82010-05-12 18:19:58 +00001391 << Unsupported->getOption().getName();
Daniel Dunbar4ed214a2010-09-24 19:39:37 +00001392
1393 if (types::isCXX(InputType) &&
1394 getToolChain().getTriple().getOS() == llvm::Triple::Darwin &&
1395 getToolChain().getTriple().getArch() == llvm::Triple::x86) {
Bob Wilson0d45f582011-08-13 23:48:55 +00001396 if ((Unsupported = Args.getLastArg(options::OPT_fapple_kext)) ||
1397 (Unsupported = Args.getLastArg(options::OPT_mkernel)))
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001398 D.Diag(diag::err_drv_clang_unsupported_opt_cxx_darwin_i386)
Daniel Dunbar4ed214a2010-09-24 19:39:37 +00001399 << Unsupported->getOption().getName();
1400 }
Daniel Dunbarfcc49a82010-05-12 18:19:58 +00001401 }
1402
Daniel Dunbara2aedc62009-03-18 10:01:51 +00001403 Args.AddAllArgs(CmdArgs, options::OPT_v);
Daniel Dunbard4352752010-08-24 22:44:13 +00001404 Args.AddLastArg(CmdArgs, options::OPT_H);
Chad Rosierbe10f982011-08-02 17:58:04 +00001405 if (D.CCPrintHeaders && !D.CCGenDiagnostics) {
Daniel Dunbarac540b32011-02-02 21:11:35 +00001406 CmdArgs.push_back("-header-include-file");
1407 CmdArgs.push_back(D.CCPrintHeadersFilename ?
1408 D.CCPrintHeadersFilename : "-");
1409 }
Daniel Dunbara2aedc62009-03-18 10:01:51 +00001410 Args.AddLastArg(CmdArgs, options::OPT_P);
Mike Stump11289f42009-09-09 15:08:12 +00001411 Args.AddLastArg(CmdArgs, options::OPT_print_ivar_layout);
Daniel Dunbara2aedc62009-03-18 10:01:51 +00001412
Chad Rosierbe10f982011-08-02 17:58:04 +00001413 if (D.CCLogDiagnostics && !D.CCGenDiagnostics) {
Daniel Dunbar529c03b2011-04-07 18:01:20 +00001414 CmdArgs.push_back("-diagnostic-log-file");
1415 CmdArgs.push_back(D.CCLogDiagnosticsFilename ?
1416 D.CCLogDiagnosticsFilename : "-");
1417 }
1418
Daniel Dunbara2aedc62009-03-18 10:01:51 +00001419 // Special case debug options to only pass -g to clang. This is
1420 // wrong.
Rafael Espindola08a692a2010-03-07 04:46:18 +00001421 Args.ClaimAllArgs(options::OPT_g_Group);
Daniel Dunbar4083d042010-05-12 18:19:55 +00001422 if (Arg *A = Args.getLastArg(options::OPT_g_Group))
1423 if (!A->getOption().matches(options::OPT_g0))
1424 CmdArgs.push_back("-g");
Daniel Dunbara2aedc62009-03-18 10:01:51 +00001425
Rafael Espindola66bfb2752010-05-06 21:06:04 +00001426 Args.AddAllArgs(CmdArgs, options::OPT_ffunction_sections);
1427 Args.AddAllArgs(CmdArgs, options::OPT_fdata_sections);
1428
Chris Lattner3c77a352010-06-22 00:03:40 +00001429 Args.AddAllArgs(CmdArgs, options::OPT_finstrument_functions);
1430
Nick Lewycky207bce32011-04-21 23:44:07 +00001431 if (Args.hasArg(options::OPT_ftest_coverage) ||
1432 Args.hasArg(options::OPT_coverage))
1433 CmdArgs.push_back("-femit-coverage-notes");
1434 if (Args.hasArg(options::OPT_fprofile_arcs) ||
1435 Args.hasArg(options::OPT_coverage))
1436 CmdArgs.push_back("-femit-coverage-data");
1437
Nick Lewycky480cb992011-05-04 20:46:58 +00001438 if (C.getArgs().hasArg(options::OPT_c) ||
1439 C.getArgs().hasArg(options::OPT_S)) {
1440 if (Output.isFilename()) {
Nick Lewycky85c011d2011-05-05 00:08:20 +00001441 CmdArgs.push_back("-coverage-file");
1442 CmdArgs.push_back(Args.MakeArgString(Output.getFilename()));
Nick Lewycky480cb992011-05-04 20:46:58 +00001443 }
1444 }
1445
Daniel Dunbara2aedc62009-03-18 10:01:51 +00001446 Args.AddLastArg(CmdArgs, options::OPT_nostdinc);
Douglas Gregor64b046f2010-03-24 20:13:48 +00001447 Args.AddLastArg(CmdArgs, options::OPT_nostdincxx);
Rafael Espindolab3549d72009-10-26 13:36:57 +00001448 Args.AddLastArg(CmdArgs, options::OPT_nobuiltininc);
Daniel Dunbara2aedc62009-03-18 10:01:51 +00001449
Daniel Dunbar34e0b8c2009-12-15 01:02:52 +00001450 // Pass the path to compiler resource files.
Daniel Dunbar34e0b8c2009-12-15 01:02:52 +00001451 CmdArgs.push_back("-resource-dir");
Daniel Dunbar3f3e2cd2010-01-20 02:35:16 +00001452 CmdArgs.push_back(D.ResourceDir.c_str());
Daniel Dunbar9dc82a22009-04-07 21:42:00 +00001453
Argyrios Kyrtzidis71731d62010-11-03 22:45:23 +00001454 Args.AddLastArg(CmdArgs, options::OPT_working_directory);
1455
John McCalld70fb982011-06-15 23:25:17 +00001456 if (!Args.hasArg(options::OPT_fno_objc_arc)) {
Argyrios Kyrtzidisc44b93d2011-07-07 04:00:39 +00001457 if (const Arg *A = Args.getLastArg(options::OPT_ccc_arcmt_check,
Argyrios Kyrtzidis7fbd97f2011-07-09 20:00:58 +00001458 options::OPT_ccc_arcmt_modify,
1459 options::OPT_ccc_arcmt_migrate)) {
John McCalld70fb982011-06-15 23:25:17 +00001460 switch (A->getOption().getID()) {
1461 default:
1462 llvm_unreachable("missed a case");
Argyrios Kyrtzidisc44b93d2011-07-07 04:00:39 +00001463 case options::OPT_ccc_arcmt_check:
John McCalld70fb982011-06-15 23:25:17 +00001464 CmdArgs.push_back("-arcmt-check");
1465 break;
Argyrios Kyrtzidisc44b93d2011-07-07 04:00:39 +00001466 case options::OPT_ccc_arcmt_modify:
John McCalld70fb982011-06-15 23:25:17 +00001467 CmdArgs.push_back("-arcmt-modify");
1468 break;
Argyrios Kyrtzidis7fbd97f2011-07-09 20:00:58 +00001469 case options::OPT_ccc_arcmt_migrate:
1470 CmdArgs.push_back("-arcmt-migrate");
1471 CmdArgs.push_back("-arcmt-migrate-directory");
1472 CmdArgs.push_back(A->getValue(Args));
Argyrios Kyrtzidisd5713632011-07-19 17:20:03 +00001473
1474 Args.AddLastArg(CmdArgs, options::OPT_arcmt_migrate_report_output);
1475 Args.AddLastArg(CmdArgs, options::OPT_arcmt_migrate_emit_arc_errors);
Argyrios Kyrtzidis7fbd97f2011-07-09 20:00:58 +00001476 break;
John McCalld70fb982011-06-15 23:25:17 +00001477 }
1478 }
1479 }
Eric Christopher84fbdb42011-08-19 00:30:14 +00001480
Daniel Dunbard067f7f2009-04-08 23:54:23 +00001481 // Add preprocessing options like -I, -D, etc. if we are using the
1482 // preprocessor.
1483 //
1484 // FIXME: Support -fpreprocessed
Daniel Dunbard067f7f2009-04-08 23:54:23 +00001485 if (types::getPreprocessedType(InputType) != types::TY_INVALID)
Douglas Gregor111af7d2009-04-18 00:34:01 +00001486 AddPreprocessingOptions(D, Args, CmdArgs, Output, Inputs);
Daniel Dunbara2aedc62009-03-18 10:01:51 +00001487
Rafael Espindolaa7431922011-07-21 23:40:37 +00001488 // Don't warn about "clang -c -DPIC -fPIC test.i" because libtool.m4 assumes
1489 // that "The compiler can only warn and ignore the option if not recognized".
1490 // When building with ccache, it will pass -D options to clang even on
1491 // preprocessed inputs and configure concludes that -fPIC is not supported.
1492 Args.ClaimAllArgs(options::OPT_D);
1493
Daniel Dunbar58f78332009-09-17 06:53:36 +00001494 // Manually translate -O to -O2 and -O4 to -O3; let clang reject
Daniel Dunbar13864952009-03-24 20:17:30 +00001495 // others.
1496 if (Arg *A = Args.getLastArg(options::OPT_O_Group)) {
Daniel Dunbar0bfb21e2009-11-19 03:26:40 +00001497 if (A->getOption().matches(options::OPT_O4))
Daniel Dunbar13864952009-03-24 20:17:30 +00001498 CmdArgs.push_back("-O3");
Daniel Dunbar9296f632010-05-27 06:51:08 +00001499 else if (A->getOption().matches(options::OPT_O) &&
1500 A->getValue(Args)[0] == '\0')
Daniel Dunbar58f78332009-09-17 06:53:36 +00001501 CmdArgs.push_back("-O2");
Daniel Dunbara2aedc62009-03-18 10:01:51 +00001502 else
Daniel Dunbar7ef5ed62009-03-18 23:39:35 +00001503 A->render(Args, CmdArgs);
Daniel Dunbara2aedc62009-03-18 10:01:51 +00001504 }
1505
Daniel Dunbar945577c2009-10-29 02:24:45 +00001506 Args.AddAllArgs(CmdArgs, options::OPT_W_Group);
1507 Args.AddLastArg(CmdArgs, options::OPT_pedantic);
1508 Args.AddLastArg(CmdArgs, options::OPT_pedantic_errors);
Daniel Dunbara2aedc62009-03-18 10:01:51 +00001509 Args.AddLastArg(CmdArgs, options::OPT_w);
Daniel Dunbarc44b4cc2009-04-07 22:13:21 +00001510
1511 // Handle -{std, ansi, trigraphs} -- take the last of -{std, ansi}
1512 // (-ansi is equivalent to -std=c89).
1513 //
1514 // If a std is supplied, only add -trigraphs if it follows the
1515 // option.
1516 if (Arg *Std = Args.getLastArg(options::OPT_std_EQ, options::OPT_ansi)) {
1517 if (Std->getOption().matches(options::OPT_ansi))
Nuno Lopes275225d2009-10-16 14:28:06 +00001518 if (types::isCXX(InputType))
Daniel Dunbar1c7577c2009-11-04 06:24:38 +00001519 CmdArgs.push_back("-std=c++98");
Nuno Lopes275225d2009-10-16 14:28:06 +00001520 else
Daniel Dunbar1c7577c2009-11-04 06:24:38 +00001521 CmdArgs.push_back("-std=c89");
Daniel Dunbarc44b4cc2009-04-07 22:13:21 +00001522 else
1523 Std->render(Args, CmdArgs);
1524
Daniel Dunbar3f1a1ff2010-06-14 21:23:08 +00001525 if (Arg *A = Args.getLastArg(options::OPT_std_EQ, options::OPT_ansi,
1526 options::OPT_trigraphs))
1527 if (A != Std)
Daniel Dunbarc44b4cc2009-04-07 22:13:21 +00001528 A->render(Args, CmdArgs);
Daniel Dunbar72a60902009-04-26 01:10:38 +00001529 } else {
1530 // Honor -std-default.
Daniel Dunbar12998192010-01-29 21:03:02 +00001531 //
1532 // FIXME: Clang doesn't correctly handle -std= when the input language
1533 // doesn't match. For the time being just ignore this for C++ inputs;
1534 // eventually we want to do all the standard defaulting here instead of
1535 // splitting it between the driver and clang -cc1.
1536 if (!types::isCXX(InputType))
1537 Args.AddAllArgsTranslated(CmdArgs, options::OPT_std_default_EQ,
1538 "-std=", /*Joined=*/true);
Daniel Dunbarc44b4cc2009-04-07 22:13:21 +00001539 Args.AddLastArg(CmdArgs, options::OPT_trigraphs);
Daniel Dunbar72a60902009-04-26 01:10:38 +00001540 }
Daniel Dunbard067f7f2009-04-08 23:54:23 +00001541
Chandler Carruthb009b142011-04-23 06:30:43 +00001542 // Map the bizarre '-Wwrite-strings' flag to a more sensible
1543 // '-fconst-strings'; this better indicates its actual behavior.
1544 if (Args.hasFlag(options::OPT_Wwrite_strings, options::OPT_Wno_write_strings,
1545 false)) {
1546 // For perfect compatibility with GCC, we do this even in the presence of
1547 // '-w'. This flag names something other than a warning for GCC.
1548 CmdArgs.push_back("-fconst-strings");
1549 }
1550
Chandler Carruth61fbf622011-04-23 09:27:53 +00001551 // GCC provides a macro definition '__DEPRECATED' when -Wdeprecated is active
Chandler Carruth30483fb2011-04-23 19:48:40 +00001552 // during C++ compilation, which it is by default. GCC keeps this define even
1553 // in the presence of '-w', match this behavior bug-for-bug.
1554 if (types::isCXX(InputType) &&
1555 Args.hasFlag(options::OPT_Wdeprecated, options::OPT_Wno_deprecated,
1556 true)) {
1557 CmdArgs.push_back("-fdeprecated-macro");
Chandler Carruth61fbf622011-04-23 09:27:53 +00001558 }
1559
Chandler Carruthe0391482010-05-22 02:21:53 +00001560 // Translate GCC's misnamer '-fasm' arguments to '-fgnu-keywords'.
1561 if (Arg *Asm = Args.getLastArg(options::OPT_fasm, options::OPT_fno_asm)) {
1562 if (Asm->getOption().matches(options::OPT_fasm))
1563 CmdArgs.push_back("-fgnu-keywords");
1564 else
1565 CmdArgs.push_back("-fno-gnu-keywords");
1566 }
1567
Rafael Espindola4cfa7972011-05-02 17:43:32 +00001568 if (ShouldDisableCFI(Args, getToolChain()))
1569 CmdArgs.push_back("-fno-dwarf2-cfi-asm");
Rafael Espindolae2641872011-04-30 18:35:43 +00001570
Daniel Dunbara2aedc62009-03-18 10:01:51 +00001571 if (Arg *A = Args.getLastArg(options::OPT_ftemplate_depth_)) {
1572 CmdArgs.push_back("-ftemplate-depth");
1573 CmdArgs.push_back(A->getValue(Args));
1574 }
1575
Argyrios Kyrtzidisef6c8da2010-11-18 00:20:36 +00001576 if (Arg *A = Args.getLastArg(options::OPT_Wlarge_by_value_copy_EQ,
1577 options::OPT_Wlarge_by_value_copy_def)) {
1578 CmdArgs.push_back("-Wlarge-by-value-copy");
Argyrios Kyrtzidisaf84ec02010-11-17 23:11:54 +00001579 if (A->getNumValues())
1580 CmdArgs.push_back(A->getValue(Args));
1581 else
Argyrios Kyrtzidisef6c8da2010-11-18 00:20:36 +00001582 CmdArgs.push_back("64"); // default value for -Wlarge-by-value-copy.
Argyrios Kyrtzidisaf84ec02010-11-17 23:11:54 +00001583 }
1584
Daniel Dunbarfffd1812009-11-19 04:00:53 +00001585 if (Args.hasArg(options::OPT__relocatable_pch))
Daniel Dunbar8bed86c2009-11-20 22:21:36 +00001586 CmdArgs.push_back("-relocatable-pch");
Mike Stump11289f42009-09-09 15:08:12 +00001587
Daniel Dunbar1c7577c2009-11-04 06:24:38 +00001588 if (Arg *A = Args.getLastArg(options::OPT_fconstant_string_class_EQ)) {
1589 CmdArgs.push_back("-fconstant-string-class");
1590 CmdArgs.push_back(A->getValue(Args));
1591 }
David Chisnall5778fce2009-08-31 16:41:57 +00001592
Chris Lattnere23003d2010-01-09 21:54:33 +00001593 if (Arg *A = Args.getLastArg(options::OPT_ftabstop_EQ)) {
1594 CmdArgs.push_back("-ftabstop");
1595 CmdArgs.push_back(A->getValue(Args));
1596 }
1597
Chris Lattnerb35583d2010-04-07 20:49:23 +00001598 CmdArgs.push_back("-ferror-limit");
1599 if (Arg *A = Args.getLastArg(options::OPT_ferror_limit_EQ))
1600 CmdArgs.push_back(A->getValue(Args));
1601 else
1602 CmdArgs.push_back("19");
Douglas Gregorffed1cb2010-04-20 07:18:24 +00001603
Chandler Carrutha77a7272010-05-06 04:55:18 +00001604 if (Arg *A = Args.getLastArg(options::OPT_fmacro_backtrace_limit_EQ)) {
1605 CmdArgs.push_back("-fmacro-backtrace-limit");
Douglas Gregorcd121fb2010-05-04 17:13:42 +00001606 CmdArgs.push_back(A->getValue(Args));
Chandler Carrutha77a7272010-05-06 04:55:18 +00001607 }
1608
1609 if (Arg *A = Args.getLastArg(options::OPT_ftemplate_backtrace_limit_EQ)) {
1610 CmdArgs.push_back("-ftemplate-backtrace-limit");
Douglas Gregorffed1cb2010-04-20 07:18:24 +00001611 CmdArgs.push_back(A->getValue(Args));
Chandler Carrutha77a7272010-05-06 04:55:18 +00001612 }
1613
Daniel Dunbar2c978472009-11-04 06:24:47 +00001614 // Pass -fmessage-length=.
Daniel Dunbar84bb7932009-11-30 08:40:54 +00001615 CmdArgs.push_back("-fmessage-length");
Daniel Dunbar2c978472009-11-04 06:24:47 +00001616 if (Arg *A = Args.getLastArg(options::OPT_fmessage_length_EQ)) {
Daniel Dunbar84bb7932009-11-30 08:40:54 +00001617 CmdArgs.push_back(A->getValue(Args));
Daniel Dunbar2c978472009-11-04 06:24:47 +00001618 } else {
1619 // If -fmessage-length=N was not specified, determine whether this is a
1620 // terminal and, if so, implicitly define -fmessage-length appropriately.
1621 unsigned N = llvm::sys::Process::StandardErrColumns();
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001622 CmdArgs.push_back(Args.MakeArgString(Twine(N)));
Daniel Dunbar2c978472009-11-04 06:24:47 +00001623 }
1624
Daniel Dunbare357d562009-12-03 18:42:11 +00001625 if (const Arg *A = Args.getLastArg(options::OPT_fvisibility_EQ)) {
1626 CmdArgs.push_back("-fvisibility");
1627 CmdArgs.push_back(A->getValue(Args));
1628 }
1629
Douglas Gregor08329632010-06-15 17:05:35 +00001630 Args.AddLastArg(CmdArgs, options::OPT_fvisibility_inlines_hidden);
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001631
Daniel Dunbare46b52a2010-03-20 04:52:14 +00001632 // -fhosted is default.
1633 if (KernelOrKext || Args.hasFlag(options::OPT_ffreestanding,
1634 options::OPT_fhosted,
1635 false))
1636 CmdArgs.push_back("-ffreestanding");
1637
Daniel Dunbare357d562009-12-03 18:42:11 +00001638 // Forward -f (flag) options which we can pass directly.
Mike Stumpd9546382009-12-12 01:27:46 +00001639 Args.AddLastArg(CmdArgs, options::OPT_fcatch_undefined_behavior);
Daniel Dunbar3a148f22009-04-07 21:51:40 +00001640 Args.AddLastArg(CmdArgs, options::OPT_femit_all_decls);
Daniel Dunbar3a148f22009-04-07 21:51:40 +00001641 Args.AddLastArg(CmdArgs, options::OPT_fheinous_gnu_extensions);
Devang Patel91bbb552010-09-30 19:05:55 +00001642 Args.AddLastArg(CmdArgs, options::OPT_flimit_debug_info);
Daniel Dunbar733b0f82011-03-01 18:49:30 +00001643 if (getToolChain().SupportsProfiling())
1644 Args.AddLastArg(CmdArgs, options::OPT_pg);
Daniel Dunbar35621a92010-03-16 16:57:46 +00001645
1646 // -flax-vector-conversions is default.
1647 if (!Args.hasFlag(options::OPT_flax_vector_conversions,
1648 options::OPT_fno_lax_vector_conversions))
1649 CmdArgs.push_back("-fno-lax-vector-conversions");
1650
Fariborz Jahaniana4cfff82011-01-07 01:05:02 +00001651 if (Args.getLastArg(options::OPT_fapple_kext))
1652 CmdArgs.push_back("-fapple-kext");
1653
Fariborz Jahaniana4404f22009-05-22 20:17:16 +00001654 Args.AddLastArg(CmdArgs, options::OPT_fobjc_sender_dependent_dispatch);
Chris Lattner69686412009-04-21 05:34:31 +00001655 Args.AddLastArg(CmdArgs, options::OPT_fdiagnostics_print_source_range_info);
Douglas Gregoreec975c2010-08-19 20:24:43 +00001656 Args.AddLastArg(CmdArgs, options::OPT_fdiagnostics_parseable_fixits);
Daniel Dunbar3a148f22009-04-07 21:51:40 +00001657 Args.AddLastArg(CmdArgs, options::OPT_ftime_report);
1658 Args.AddLastArg(CmdArgs, options::OPT_ftrapv);
David Chisnalldd84ef12010-09-17 18:29:54 +00001659
1660 if (Arg *A = Args.getLastArg(options::OPT_ftrapv_handler_EQ)) {
1661 CmdArgs.push_back("-ftrapv-handler");
1662 CmdArgs.push_back(A->getValue(Args));
1663 }
1664
Evan Cheng04c94292011-04-08 21:37:45 +00001665 // Forward -ftrap_function= options to the backend.
1666 if (Arg *A = Args.getLastArg(options::OPT_ftrap_function_EQ)) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001667 StringRef FuncName = A->getValue(Args);
Evan Cheng04c94292011-04-08 21:37:45 +00001668 CmdArgs.push_back("-backend-option");
1669 CmdArgs.push_back(Args.MakeArgString("-trap-func=" + FuncName));
1670 }
1671
Chandler Carruth6e501032011-03-27 00:04:55 +00001672 // -fno-strict-overflow implies -fwrapv if it isn't disabled, but
1673 // -fstrict-overflow won't turn off an explicitly enabled -fwrapv.
1674 if (Arg *A = Args.getLastArg(options::OPT_fwrapv,
1675 options::OPT_fno_wrapv)) {
1676 if (A->getOption().matches(options::OPT_fwrapv))
1677 CmdArgs.push_back("-fwrapv");
1678 } else if (Arg *A = Args.getLastArg(options::OPT_fstrict_overflow,
1679 options::OPT_fno_strict_overflow)) {
1680 if (A->getOption().matches(options::OPT_fno_strict_overflow))
1681 CmdArgs.push_back("-fwrapv");
1682 }
Daniel Dunbar3a148f22009-04-07 21:51:40 +00001683 Args.AddLastArg(CmdArgs, options::OPT_fwritable_strings);
Eric Christopherf387dbd2010-08-07 23:08:14 +00001684 Args.AddLastArg(CmdArgs, options::OPT_funroll_loops);
Daniel Dunbara2aedc62009-03-18 10:01:51 +00001685
Daniel Dunbara77eaeb2009-09-03 04:54:28 +00001686 Args.AddLastArg(CmdArgs, options::OPT_pthread);
1687
Daniel Dunbar4930e332009-11-17 08:07:36 +00001688 // -stack-protector=0 is default.
1689 unsigned StackProtectorLevel = 0;
Bill Wendlingd63bbad2009-06-28 07:36:13 +00001690 if (Arg *A = Args.getLastArg(options::OPT_fno_stack_protector,
1691 options::OPT_fstack_protector_all,
1692 options::OPT_fstack_protector)) {
Daniel Dunbar4930e332009-11-17 08:07:36 +00001693 if (A->getOption().matches(options::OPT_fstack_protector))
1694 StackProtectorLevel = 1;
1695 else if (A->getOption().matches(options::OPT_fstack_protector_all))
1696 StackProtectorLevel = 2;
Nico Weberdd473632011-08-23 07:38:27 +00001697 } else {
1698 StackProtectorLevel =
1699 getToolChain().GetDefaultStackProtectorLevel(KernelOrKext);
1700 }
Daniel Dunbar4930e332009-11-17 08:07:36 +00001701 if (StackProtectorLevel) {
1702 CmdArgs.push_back("-stack-protector");
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001703 CmdArgs.push_back(Args.MakeArgString(Twine(StackProtectorLevel)));
Bill Wendlingd63bbad2009-06-28 07:36:13 +00001704 }
1705
Eric Christopherd5c45f62011-05-02 21:18:22 +00001706 // Translate -mstackrealign
1707 if (Args.hasArg(options::OPT_mstackrealign)) {
1708 CmdArgs.push_back("-backend-option");
1709 CmdArgs.push_back("-force-align-stack");
1710 }
Eric Christopher84fbdb42011-08-19 00:30:14 +00001711
Daniel Dunbard18049a2009-04-07 21:16:11 +00001712 // Forward -f options with positive and negative forms; we translate
1713 // these by hand.
1714
Fariborz Jahaniana4cfff82011-01-07 01:05:02 +00001715 if (Args.hasArg(options::OPT_mkernel)) {
Daniel Dunbar80f787c2011-02-04 17:24:47 +00001716 if (!Args.hasArg(options::OPT_fapple_kext) && types::isCXX(InputType))
Fariborz Jahaniana4cfff82011-01-07 01:05:02 +00001717 CmdArgs.push_back("-fapple-kext");
1718 if (!Args.hasArg(options::OPT_fbuiltin))
1719 CmdArgs.push_back("-fno-builtin");
1720 }
Daniel Dunbar4930e332009-11-17 08:07:36 +00001721 // -fbuiltin is default.
Fariborz Jahaniana4cfff82011-01-07 01:05:02 +00001722 else if (!Args.hasFlag(options::OPT_fbuiltin, options::OPT_fno_builtin))
Daniel Dunbar484afa22009-11-19 04:55:23 +00001723 CmdArgs.push_back("-fno-builtin");
Daniel Dunbard18049a2009-04-07 21:16:11 +00001724
Nuno Lopes13c88c72009-12-16 16:59:22 +00001725 if (!Args.hasFlag(options::OPT_fassume_sane_operator_new,
1726 options::OPT_fno_assume_sane_operator_new))
1727 CmdArgs.push_back("-fno-assume-sane-operator-new");
1728
Daniel Dunbar4930e332009-11-17 08:07:36 +00001729 // -fblocks=0 is default.
1730 if (Args.hasFlag(options::OPT_fblocks, options::OPT_fno_blocks,
David Chisnallda209912011-02-28 17:11:43 +00001731 getToolChain().IsBlocksDefault()) ||
1732 (Args.hasArg(options::OPT_fgnu_runtime) &&
1733 Args.hasArg(options::OPT_fobjc_nonfragile_abi) &&
1734 !Args.hasArg(options::OPT_fno_blocks))) {
Daniel Dunbar4930e332009-11-17 08:07:36 +00001735 CmdArgs.push_back("-fblocks");
John McCall7959fee2011-09-09 20:41:01 +00001736
1737 if (!Args.hasArg(options::OPT_fgnu_runtime) &&
1738 !getToolChain().hasBlocksRuntime())
1739 CmdArgs.push_back("-fblocks-runtime-optional");
David Chisnall950a9512009-11-17 19:33:30 +00001740 }
Daniel Dunbard18049a2009-04-07 21:16:11 +00001741
John McCalldfea9982010-04-09 19:12:06 +00001742 // -faccess-control is default.
John McCall3155f572010-04-09 19:03:51 +00001743 if (Args.hasFlag(options::OPT_fno_access_control,
1744 options::OPT_faccess_control,
John McCalldfea9982010-04-09 19:12:06 +00001745 false))
John McCall3155f572010-04-09 19:03:51 +00001746 CmdArgs.push_back("-fno-access-control");
John McCall59bb1d42010-03-17 01:32:13 +00001747
Anders Carlssond470fef2010-11-21 00:09:52 +00001748 // -felide-constructors is the default.
1749 if (Args.hasFlag(options::OPT_fno_elide_constructors,
1750 options::OPT_felide_constructors,
1751 false))
1752 CmdArgs.push_back("-fno-elide-constructors");
1753
Daniel Dunbar0730e4f2009-11-17 07:06:20 +00001754 // -frtti is default.
Daniel Dunbare46b52a2010-03-20 04:52:14 +00001755 if (KernelOrKext ||
1756 !Args.hasFlag(options::OPT_frtti, options::OPT_fno_rtti))
Daniel Dunbar484afa22009-11-19 04:55:23 +00001757 CmdArgs.push_back("-fno-rtti");
Mike Stump183c3d22009-07-31 23:15:31 +00001758
Argyrios Kyrtzidis74825bc2010-10-08 00:25:19 +00001759 // -fshort-enums=0 is default.
1760 // FIXME: Are there targers where -fshort-enums is on by default ?
1761 if (Args.hasFlag(options::OPT_fshort_enums,
1762 options::OPT_fno_short_enums, false))
1763 CmdArgs.push_back("-fshort-enums");
1764
Daniel Dunbard609b7b2009-11-17 06:37:03 +00001765 // -fsigned-char is default.
Daniel Dunbar5bdd2992009-11-25 10:14:30 +00001766 if (!Args.hasFlag(options::OPT_fsigned_char, options::OPT_funsigned_char,
Daniel Dunbard609b7b2009-11-17 06:37:03 +00001767 isSignedCharDefault(getToolChain().getTriple())))
Daniel Dunbar5fe08662009-11-29 02:39:08 +00001768 CmdArgs.push_back("-fno-signed-char");
Eli Friedman327f0b52009-06-05 07:21:14 +00001769
Anders Carlssonfcd764a2010-02-06 23:23:06 +00001770 // -fthreadsafe-static is default.
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001771 if (!Args.hasFlag(options::OPT_fthreadsafe_statics,
Anders Carlssonfcd764a2010-02-06 23:23:06 +00001772 options::OPT_fno_threadsafe_statics))
1773 CmdArgs.push_back("-fno-threadsafe-statics");
1774
Daniel Dunbarfe06df42010-03-20 04:15:41 +00001775 // -fuse-cxa-atexit is default.
Anton Korobeynikov82b33332010-09-11 11:17:06 +00001776 if (KernelOrKext ||
1777 !Args.hasFlag(options::OPT_fuse_cxa_atexit, options::OPT_fno_use_cxa_atexit,
NAKAMURA Takumi6bdc8a22010-10-10 01:53:03 +00001778 getToolChain().getTriple().getOS() != llvm::Triple::Cygwin &&
NAKAMURA Takumi31ea2f12011-02-17 08:51:38 +00001779 getToolChain().getTriple().getOS() != llvm::Triple::MinGW32))
Daniel Dunbarfe06df42010-03-20 04:15:41 +00001780 CmdArgs.push_back("-fno-use-cxa-atexit");
1781
Daniel Dunbar0730e4f2009-11-17 07:06:20 +00001782 // -fms-extensions=0 is default.
Daniel Dunbar5bdd2992009-11-25 10:14:30 +00001783 if (Args.hasFlag(options::OPT_fms_extensions, options::OPT_fno_ms_extensions,
Daniel Dunbar0730e4f2009-11-17 07:06:20 +00001784 getToolChain().getTriple().getOS() == llvm::Triple::Win32))
1785 CmdArgs.push_back("-fms-extensions");
1786
Michael J. Spencer4992ca4b2010-10-21 05:21:48 +00001787 // -fmsc-version=1300 is default.
1788 if (Args.hasFlag(options::OPT_fms_extensions, options::OPT_fno_ms_extensions,
1789 getToolChain().getTriple().getOS() == llvm::Triple::Win32) ||
1790 Args.hasArg(options::OPT_fmsc_version)) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001791 StringRef msc_ver = Args.getLastArgValue(options::OPT_fmsc_version);
Michael J. Spencer4992ca4b2010-10-21 05:21:48 +00001792 if (msc_ver.empty())
1793 CmdArgs.push_back("-fmsc-version=1300");
1794 else
1795 CmdArgs.push_back(Args.MakeArgString("-fmsc-version=" + msc_ver));
1796 }
1797
1798
Dawn Perchik68bb1b42010-09-02 23:59:25 +00001799 // -fborland-extensions=0 is default.
1800 if (Args.hasFlag(options::OPT_fborland_extensions,
1801 options::OPT_fno_borland_extensions, false))
1802 CmdArgs.push_back("-fborland-extensions");
1803
Francois Pichet02744872011-09-01 16:38:08 +00001804 // -fno-delayed-template-parsing is default, except for Windows where MSVC STL
1805 // needs it.
Francois Pichet1c229c02011-04-22 22:18:13 +00001806 if (Args.hasFlag(options::OPT_fdelayed_template_parsing,
1807 options::OPT_fno_delayed_template_parsing,
Francois Pichet02744872011-09-01 16:38:08 +00001808 getToolChain().getTriple().getOS() == llvm::Triple::Win32))
Francois Pichet35bc5de2011-08-26 00:22:34 +00001809 CmdArgs.push_back("-fdelayed-template-parsing");
Francois Pichet1c229c02011-04-22 22:18:13 +00001810
Chandler Carruthe03aa552010-04-17 20:17:31 +00001811 // -fgnu-keywords default varies depending on language; only pass if
1812 // specified.
1813 if (Arg *A = Args.getLastArg(options::OPT_fgnu_keywords,
Daniel Dunbardb059592010-04-24 17:56:39 +00001814 options::OPT_fno_gnu_keywords))
1815 A->render(Args, CmdArgs);
Chandler Carruthe03aa552010-04-17 20:17:31 +00001816
Rafael Espindola922a6242011-06-02 17:30:53 +00001817 if (Args.hasFlag(options::OPT_fgnu89_inline,
1818 options::OPT_fno_gnu89_inline,
1819 false))
Rafael Espindolafb2af642011-06-02 16:13:27 +00001820 CmdArgs.push_back("-fgnu89-inline");
1821
Daniel Dunbar4930e332009-11-17 08:07:36 +00001822 // -fobjc-nonfragile-abi=0 is default.
John McCall24fc0de2011-07-06 00:26:06 +00001823 ObjCRuntime objCRuntime;
John McCallb5f652e2011-06-22 00:53:57 +00001824 unsigned objcABIVersion = 0;
Daniel Dunbar4930e332009-11-17 08:07:36 +00001825 if (types::isObjC(InputType)) {
John McCall24fc0de2011-07-06 00:26:06 +00001826 bool NeXTRuntimeIsDefault
1827 = (IsRewriter || getToolChain().getTriple().isOSDarwin());
1828 if (Args.hasFlag(options::OPT_fnext_runtime, options::OPT_fgnu_runtime,
John McCall75bc7732011-07-06 02:36:30 +00001829 NeXTRuntimeIsDefault)) {
John McCall24fc0de2011-07-06 00:26:06 +00001830 objCRuntime.setKind(ObjCRuntime::NeXT);
John McCall75bc7732011-07-06 02:36:30 +00001831 } else {
1832 CmdArgs.push_back("-fgnu-runtime");
John McCall24fc0de2011-07-06 00:26:06 +00001833 objCRuntime.setKind(ObjCRuntime::GNU);
John McCall75bc7732011-07-06 02:36:30 +00001834 }
John McCall24fc0de2011-07-06 00:26:06 +00001835 getToolChain().configureObjCRuntime(objCRuntime);
1836 if (objCRuntime.HasARC)
1837 CmdArgs.push_back("-fobjc-runtime-has-arc");
1838 if (objCRuntime.HasWeak)
1839 CmdArgs.push_back("-fobjc-runtime-has-weak");
John McCall9de19782011-07-06 01:22:26 +00001840 if (objCRuntime.HasTerminate)
1841 CmdArgs.push_back("-fobjc-runtime-has-terminate");
John McCall24fc0de2011-07-06 00:26:06 +00001842
Daniel Dunbarc1dd0e92010-09-20 18:19:55 +00001843 // Compute the Objective-C ABI "version" to use. Version numbers are
1844 // slightly confusing for historical reasons:
1845 // 1 - Traditional "fragile" ABI
1846 // 2 - Non-fragile ABI, version 1
1847 // 3 - Non-fragile ABI, version 2
John McCallb5f652e2011-06-22 00:53:57 +00001848 objcABIVersion = 1;
Daniel Dunbarc1dd0e92010-09-20 18:19:55 +00001849 // If -fobjc-abi-version= is present, use that to set the version.
Daniel Dunbar12c82082010-04-28 23:25:24 +00001850 if (Arg *A = Args.getLastArg(options::OPT_fobjc_abi_version_EQ)) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001851 if (StringRef(A->getValue(Args)) == "1")
John McCallb5f652e2011-06-22 00:53:57 +00001852 objcABIVersion = 1;
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001853 else if (StringRef(A->getValue(Args)) == "2")
John McCallb5f652e2011-06-22 00:53:57 +00001854 objcABIVersion = 2;
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001855 else if (StringRef(A->getValue(Args)) == "3")
John McCallb5f652e2011-06-22 00:53:57 +00001856 objcABIVersion = 3;
Daniel Dunbar12c82082010-04-28 23:25:24 +00001857 else
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001858 D.Diag(diag::err_drv_clang_unsupported) << A->getAsString(Args);
Daniel Dunbarc1dd0e92010-09-20 18:19:55 +00001859 } else {
1860 // Otherwise, determine if we are using the non-fragile ABI.
1861 if (Args.hasFlag(options::OPT_fobjc_nonfragile_abi,
1862 options::OPT_fno_objc_nonfragile_abi,
1863 getToolChain().IsObjCNonFragileABIDefault())) {
1864 // Determine the non-fragile ABI version to use.
Daniel Dunbaraeed5fe2010-11-11 16:08:59 +00001865#ifdef DISABLE_DEFAULT_NONFRAGILEABI_TWO
1866 unsigned NonFragileABIVersion = 1;
1867#else
Daniel Dunbarc1dd0e92010-09-20 18:19:55 +00001868 unsigned NonFragileABIVersion = 2;
Daniel Dunbaraeed5fe2010-11-11 16:08:59 +00001869#endif
Daniel Dunbarc1dd0e92010-09-20 18:19:55 +00001870
1871 if (Arg *A = Args.getLastArg(
1872 options::OPT_fobjc_nonfragile_abi_version_EQ)) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001873 if (StringRef(A->getValue(Args)) == "1")
Daniel Dunbarc1dd0e92010-09-20 18:19:55 +00001874 NonFragileABIVersion = 1;
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001875 else if (StringRef(A->getValue(Args)) == "2")
Daniel Dunbarc1dd0e92010-09-20 18:19:55 +00001876 NonFragileABIVersion = 2;
1877 else
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001878 D.Diag(diag::err_drv_clang_unsupported)
Daniel Dunbarc1dd0e92010-09-20 18:19:55 +00001879 << A->getAsString(Args);
1880 }
1881
John McCallb5f652e2011-06-22 00:53:57 +00001882 objcABIVersion = 1 + NonFragileABIVersion;
Daniel Dunbarc1dd0e92010-09-20 18:19:55 +00001883 } else {
John McCallb5f652e2011-06-22 00:53:57 +00001884 objcABIVersion = 1;
Daniel Dunbarc1dd0e92010-09-20 18:19:55 +00001885 }
Daniel Dunbar12c82082010-04-28 23:25:24 +00001886 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001887
John McCallb5f652e2011-06-22 00:53:57 +00001888 if (objcABIVersion == 2 || objcABIVersion == 3) {
Fariborz Jahanian3aa19e9a2011-01-04 20:05:20 +00001889 CmdArgs.push_back("-fobjc-nonfragile-abi");
Daniel Dunbarfca18c1b42010-04-24 17:56:46 +00001890
1891 // -fobjc-dispatch-method is only relevant with the nonfragile-abi, and
1892 // legacy is the default.
1893 if (!Args.hasFlag(options::OPT_fobjc_legacy_dispatch,
1894 options::OPT_fno_objc_legacy_dispatch,
1895 getToolChain().IsObjCLegacyDispatchDefault())) {
1896 if (getToolChain().UseObjCMixedDispatch())
1897 CmdArgs.push_back("-fobjc-dispatch-method=mixed");
1898 else
1899 CmdArgs.push_back("-fobjc-dispatch-method=non-legacy");
1900 }
Daniel Dunbar2e3f2c82010-02-01 21:07:25 +00001901 }
NAKAMURA Takumi029d74b2011-02-17 08:50:50 +00001902
Ted Kremenek1d56c9e2010-12-23 21:35:43 +00001903 // -fobjc-default-synthesize-properties=0 is default.
1904 if (Args.hasFlag(options::OPT_fobjc_default_synthesize_properties,
1905 options::OPT_fno_objc_default_synthesize_properties,
1906 getToolChain().IsObjCDefaultSynthPropertiesDefault())) {
1907 CmdArgs.push_back("-fobjc-default-synthesize-properties");
1908 }
Daniel Dunbar4930e332009-11-17 08:07:36 +00001909 }
1910
John McCall24fc0de2011-07-06 00:26:06 +00001911 // Allow -fno-objc-arr to trump -fobjc-arr/-fobjc-arc.
1912 // NOTE: This logic is duplicated in ToolChains.cpp.
1913 bool ARC = isObjCAutoRefCount(Args);
1914 if (ARC) {
1915 CmdArgs.push_back("-fobjc-arc");
1916
1917 // Allow the user to enable full exceptions code emission.
1918 // We define off for Objective-CC, on for Objective-C++.
1919 if (Args.hasFlag(options::OPT_fobjc_arc_exceptions,
1920 options::OPT_fno_objc_arc_exceptions,
1921 /*default*/ types::isCXX(InputType)))
1922 CmdArgs.push_back("-fobjc-arc-exceptions");
1923 }
1924
1925 // -fobjc-infer-related-result-type is the default, except in the Objective-C
1926 // rewriter.
1927 if (IsRewriter)
1928 CmdArgs.push_back("-fno-objc-infer-related-result-type");
Eric Christopher84fbdb42011-08-19 00:30:14 +00001929
John McCall24fc0de2011-07-06 00:26:06 +00001930 // Handle -fobjc-gc and -fobjc-gc-only. They are exclusive, and -fobjc-gc-only
1931 // takes precedence.
1932 const Arg *GCArg = Args.getLastArg(options::OPT_fobjc_gc_only);
1933 if (!GCArg)
1934 GCArg = Args.getLastArg(options::OPT_fobjc_gc);
1935 if (GCArg) {
1936 if (ARC) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001937 D.Diag(diag::err_drv_objc_gc_arr)
John McCall24fc0de2011-07-06 00:26:06 +00001938 << GCArg->getAsString(Args);
1939 } else if (getToolChain().SupportsObjCGC()) {
1940 GCArg->render(Args, CmdArgs);
1941 } else {
1942 // FIXME: We should move this to a hard error.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001943 D.Diag(diag::warn_drv_objc_gc_unsupported)
John McCall24fc0de2011-07-06 00:26:06 +00001944 << GCArg->getAsString(Args);
1945 }
1946 }
1947
John McCallb5f652e2011-06-22 00:53:57 +00001948 // Add exception args.
1949 addExceptionArgs(Args, InputType, getToolChain().getTriple(),
1950 KernelOrKext, IsRewriter, objcABIVersion, CmdArgs);
1951
1952 if (getToolChain().UseSjLjExceptions())
1953 CmdArgs.push_back("-fsjlj-exceptions");
1954
1955 // C++ "sane" operator new.
Daniel Dunbar2e3f2c82010-02-01 21:07:25 +00001956 if (!Args.hasFlag(options::OPT_fassume_sane_operator_new,
1957 options::OPT_fno_assume_sane_operator_new))
1958 CmdArgs.push_back("-fno-assume-sane-operator-new");
1959
Daniel Dunbar34d7a992010-04-27 15:34:57 +00001960 // -fconstant-cfstrings is default, and may be subject to argument translation
1961 // on Darwin.
1962 if (!Args.hasFlag(options::OPT_fconstant_cfstrings,
1963 options::OPT_fno_constant_cfstrings) ||
1964 !Args.hasFlag(options::OPT_mconstant_cfstrings,
1965 options::OPT_mno_constant_cfstrings))
1966 CmdArgs.push_back("-fno-constant-cfstrings");
1967
John Thompsoned4e2952009-11-05 20:14:16 +00001968 // -fshort-wchar default varies depending on platform; only
1969 // pass if specified.
Daniel Dunbar2cb4e7a2010-04-27 15:35:03 +00001970 if (Arg *A = Args.getLastArg(options::OPT_fshort_wchar))
1971 A->render(Args, CmdArgs);
John Thompsoned4e2952009-11-05 20:14:16 +00001972
Daniel Dunbar1a8a2e82009-10-29 02:39:57 +00001973 // -fno-pascal-strings is default, only pass non-default. If the tool chain
1974 // happened to translate to -mpascal-strings, we want to back translate here.
Daniel Dunbard4510f22009-04-07 23:51:44 +00001975 //
1976 // FIXME: This is gross; that translation should be pulled from the
1977 // tool chain.
Daniel Dunbard067f7f2009-04-08 23:54:23 +00001978 if (Args.hasFlag(options::OPT_fpascal_strings,
Daniel Dunbard4510f22009-04-07 23:51:44 +00001979 options::OPT_fno_pascal_strings,
1980 false) ||
1981 Args.hasFlag(options::OPT_mpascal_strings,
1982 options::OPT_mno_pascal_strings,
1983 false))
Daniel Dunbard18049a2009-04-07 21:16:11 +00001984 CmdArgs.push_back("-fpascal-strings");
NAKAMURA Takumi029d74b2011-02-17 08:50:50 +00001985
Fariborz Jahaniana4cfff82011-01-07 01:05:02 +00001986 if (Args.hasArg(options::OPT_mkernel) ||
1987 Args.hasArg(options::OPT_fapple_kext)) {
1988 if (!Args.hasArg(options::OPT_fcommon))
1989 CmdArgs.push_back("-fno-common");
1990 }
Daniel Dunbard18049a2009-04-07 21:16:11 +00001991 // -fcommon is default, only pass non-default.
Fariborz Jahaniana4cfff82011-01-07 01:05:02 +00001992 else if (!Args.hasFlag(options::OPT_fcommon, options::OPT_fno_common))
Daniel Dunbard18049a2009-04-07 21:16:11 +00001993 CmdArgs.push_back("-fno-common");
1994
Daniel Dunbar2edd9232009-04-15 02:37:43 +00001995 // -fsigned-bitfields is default, and clang doesn't yet support
Daniel Dunbar6358d682010-10-15 22:30:42 +00001996 // -funsigned-bitfields.
Mike Stump11289f42009-09-09 15:08:12 +00001997 if (!Args.hasFlag(options::OPT_fsigned_bitfields,
Daniel Dunbar2edd9232009-04-15 02:37:43 +00001998 options::OPT_funsigned_bitfields))
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001999 D.Diag(diag::warn_drv_clang_unsupported)
Daniel Dunbar2edd9232009-04-15 02:37:43 +00002000 << Args.getLastArg(options::OPT_funsigned_bitfields)->getAsString(Args);
2001
Daniel Dunbar6358d682010-10-15 22:30:42 +00002002 // -fsigned-bitfields is default, and clang doesn't support -fno-for-scope.
2003 if (!Args.hasFlag(options::OPT_ffor_scope,
2004 options::OPT_fno_for_scope))
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002005 D.Diag(diag::err_drv_clang_unsupported)
Daniel Dunbar6358d682010-10-15 22:30:42 +00002006 << Args.getLastArg(options::OPT_fno_for_scope)->getAsString(Args);
2007
Jeffrey Yasskin460aa542010-06-08 04:56:20 +00002008 // -fcaret-diagnostics is default.
2009 if (!Args.hasFlag(options::OPT_fcaret_diagnostics,
2010 options::OPT_fno_caret_diagnostics, true))
2011 CmdArgs.push_back("-fno-caret-diagnostics");
2012
Daniel Dunbar8281bde2009-04-19 21:09:34 +00002013 // -fdiagnostics-fixit-info is default, only pass non-default.
Mike Stump11289f42009-09-09 15:08:12 +00002014 if (!Args.hasFlag(options::OPT_fdiagnostics_fixit_info,
Daniel Dunbar8281bde2009-04-19 21:09:34 +00002015 options::OPT_fno_diagnostics_fixit_info))
2016 CmdArgs.push_back("-fno-diagnostics-fixit-info");
Eric Christopher84fbdb42011-08-19 00:30:14 +00002017
Douglas Gregor46ce91a2011-04-15 22:04:17 +00002018 // Enable -fdiagnostics-show-name by default.
2019 if (Args.hasFlag(options::OPT_fdiagnostics_show_name,
2020 options::OPT_fno_diagnostics_show_name, false))
2021 CmdArgs.push_back("-fdiagnostics-show-name");
Daniel Dunbar8281bde2009-04-19 21:09:34 +00002022
Daniel Dunbar092f0cc2009-04-16 06:32:38 +00002023 // Enable -fdiagnostics-show-option by default.
Mike Stump11289f42009-09-09 15:08:12 +00002024 if (Args.hasFlag(options::OPT_fdiagnostics_show_option,
Daniel Dunbar092f0cc2009-04-16 06:32:38 +00002025 options::OPT_fno_diagnostics_show_option))
2026 CmdArgs.push_back("-fdiagnostics-show-option");
Daniel Dunbar5ec95022009-11-04 06:24:57 +00002027
Chris Lattnerbf6fac82010-05-04 21:55:25 +00002028 if (const Arg *A =
2029 Args.getLastArg(options::OPT_fdiagnostics_show_category_EQ)) {
2030 CmdArgs.push_back("-fdiagnostics-show-category");
2031 CmdArgs.push_back(A->getValue(Args));
2032 }
Daniel Dunbarfe6c97b2010-08-24 16:47:49 +00002033
Douglas Gregor643c9222011-05-21 17:07:29 +00002034 if (const Arg *A =
2035 Args.getLastArg(options::OPT_fdiagnostics_format_EQ)) {
2036 CmdArgs.push_back("-fdiagnostics-format");
2037 CmdArgs.push_back(A->getValue(Args));
2038 }
2039
Chandler Carruthb6766f02011-03-27 01:50:55 +00002040 if (Arg *A = Args.getLastArg(
2041 options::OPT_fdiagnostics_show_note_include_stack,
2042 options::OPT_fno_diagnostics_show_note_include_stack)) {
2043 if (A->getOption().matches(
2044 options::OPT_fdiagnostics_show_note_include_stack))
2045 CmdArgs.push_back("-fdiagnostics-show-note-include-stack");
2046 else
2047 CmdArgs.push_back("-fno-diagnostics-show-note-include-stack");
2048 }
2049
Daniel Dunbar5ec95022009-11-04 06:24:57 +00002050 // Color diagnostics are the default, unless the terminal doesn't support
2051 // them.
2052 if (Args.hasFlag(options::OPT_fcolor_diagnostics,
Argyrios Kyrtzidis4f920162010-09-23 12:56:06 +00002053 options::OPT_fno_color_diagnostics,
2054 llvm::sys::Process::StandardErrHasColors()))
Daniel Dunbar5ec95022009-11-04 06:24:57 +00002055 CmdArgs.push_back("-fcolor-diagnostics");
2056
Daniel Dunbardb097022009-06-08 21:13:54 +00002057 if (!Args.hasFlag(options::OPT_fshow_source_location,
2058 options::OPT_fno_show_source_location))
2059 CmdArgs.push_back("-fno-show-source-location");
Daniel Dunbar092f0cc2009-04-16 06:32:38 +00002060
Douglas Gregor643c9222011-05-21 17:07:29 +00002061 if (!Args.hasFlag(options::OPT_fshow_column,
2062 options::OPT_fno_show_column,
2063 true))
2064 CmdArgs.push_back("-fno-show-column");
2065
Douglas Gregor8ed0c0b2010-07-09 17:35:33 +00002066 if (!Args.hasFlag(options::OPT_fspell_checking,
2067 options::OPT_fno_spell_checking))
2068 CmdArgs.push_back("-fno-spell-checking");
Daniel Dunbarfe6c97b2010-08-24 16:47:49 +00002069
Daniel Dunbar473f8a62010-10-18 22:49:46 +00002070
Daniel Dunbar3ada2b72010-11-02 19:42:04 +00002071 // Silently ignore -fasm-blocks for now.
2072 (void) Args.hasFlag(options::OPT_fasm_blocks, options::OPT_fno_asm_blocks,
2073 false);
Daniel Dunbar473f8a62010-10-18 22:49:46 +00002074
Jeffrey Yasskin2b99c6f2010-06-11 05:57:47 +00002075 if (Arg *A = Args.getLastArg(options::OPT_fshow_overloads_EQ))
2076 A->render(Args, CmdArgs);
2077
Daniel Dunbarf5e9b1f2009-04-19 21:20:32 +00002078 // -fdollars-in-identifiers default varies depending on platform and
2079 // language; only pass if specified.
Mike Stump11289f42009-09-09 15:08:12 +00002080 if (Arg *A = Args.getLastArg(options::OPT_fdollars_in_identifiers,
Daniel Dunbarf5e9b1f2009-04-19 21:20:32 +00002081 options::OPT_fno_dollars_in_identifiers)) {
2082 if (A->getOption().matches(options::OPT_fdollars_in_identifiers))
Daniel Dunbar15cef0e2009-12-16 20:10:18 +00002083 CmdArgs.push_back("-fdollars-in-identifiers");
Daniel Dunbarf5e9b1f2009-04-19 21:20:32 +00002084 else
Daniel Dunbar15cef0e2009-12-16 20:10:18 +00002085 CmdArgs.push_back("-fno-dollars-in-identifiers");
Daniel Dunbarf5e9b1f2009-04-19 21:20:32 +00002086 }
2087
Daniel Dunbaradeeb052009-05-22 19:02:20 +00002088 // -funit-at-a-time is default, and we don't support -fno-unit-at-a-time for
2089 // practical purposes.
Mike Stump11289f42009-09-09 15:08:12 +00002090 if (Arg *A = Args.getLastArg(options::OPT_funit_at_a_time,
Daniel Dunbaradeeb052009-05-22 19:02:20 +00002091 options::OPT_fno_unit_at_a_time)) {
2092 if (A->getOption().matches(options::OPT_fno_unit_at_a_time))
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002093 D.Diag(diag::warn_drv_clang_unsupported) << A->getAsString(Args);
Daniel Dunbaradeeb052009-05-22 19:02:20 +00002094 }
Eli Friedmanbb0d9a52009-07-14 21:58:17 +00002095
Daniel Dunbar2ffe0292009-09-10 03:37:02 +00002096 // Default to -fno-builtin-str{cat,cpy} on Darwin for ARM.
Daniel Dunbar4fa08112009-09-10 04:57:27 +00002097 //
Daniel Dunbar6c536aa2009-12-11 23:00:49 +00002098 // FIXME: This is disabled until clang -cc1 supports -fno-builtin-foo. PR4941.
Daniel Dunbar4fa08112009-09-10 04:57:27 +00002099#if 0
Daniel Dunbar2ffe0292009-09-10 03:37:02 +00002100 if (getToolChain().getTriple().getOS() == llvm::Triple::Darwin &&
2101 (getToolChain().getTriple().getArch() == llvm::Triple::arm ||
2102 getToolChain().getTriple().getArch() == llvm::Triple::thumb)) {
2103 if (!Args.hasArg(options::OPT_fbuiltin_strcat))
2104 CmdArgs.push_back("-fno-builtin-strcat");
2105 if (!Args.hasArg(options::OPT_fbuiltin_strcpy))
2106 CmdArgs.push_back("-fno-builtin-strcpy");
2107 }
Daniel Dunbar4fa08112009-09-10 04:57:27 +00002108#endif
Daniel Dunbar2ffe0292009-09-10 03:37:02 +00002109
Daniel Dunbar8c3d7352011-03-18 21:23:40 +00002110 // Only allow -traditional or -traditional-cpp outside in preprocessing modes.
Mike Stump11289f42009-09-09 15:08:12 +00002111 if (Arg *A = Args.getLastArg(options::OPT_traditional,
Daniel Dunbar8c3d7352011-03-18 21:23:40 +00002112 options::OPT_traditional_cpp)) {
2113 if (isa<PreprocessJobAction>(JA))
2114 CmdArgs.push_back("-traditional-cpp");
Eric Christopher84fbdb42011-08-19 00:30:14 +00002115 else
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002116 D.Diag(diag::err_drv_clang_unsupported) << A->getAsString(Args);
Daniel Dunbar8c3d7352011-03-18 21:23:40 +00002117 }
Eli Friedmanbb0d9a52009-07-14 21:58:17 +00002118
Daniel Dunbara2aedc62009-03-18 10:01:51 +00002119 Args.AddLastArg(CmdArgs, options::OPT_dM);
Chris Lattnercac63f32009-04-12 01:56:53 +00002120 Args.AddLastArg(CmdArgs, options::OPT_dD);
Daniel Dunbara2aedc62009-03-18 10:01:51 +00002121
Daniel Dunbar76fa8402010-04-15 06:09:03 +00002122 // Forward -Xclang arguments to -cc1, and -mllvm arguments to the LLVM option
2123 // parser.
Daniel Dunbara2aedc62009-03-18 10:01:51 +00002124 Args.AddAllArgValues(CmdArgs, options::OPT_Xclang);
Daniel Dunbar76fa8402010-04-15 06:09:03 +00002125 for (arg_iterator it = Args.filtered_begin(options::OPT_mllvm),
2126 ie = Args.filtered_end(); it != ie; ++it) {
Daniel Dunbara442fd52010-06-11 22:00:13 +00002127 (*it)->claim();
Daniel Dunbar88534f42010-04-17 06:10:00 +00002128
Daniel Dunbar76fa8402010-04-15 06:09:03 +00002129 // We translate this by hand to the -cc1 argument, since nightly test uses
2130 // it and developers have been trained to spell it with -mllvm.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002131 if (StringRef((*it)->getValue(Args, 0)) == "-disable-llvm-optzns")
Daniel Dunbar76fa8402010-04-15 06:09:03 +00002132 CmdArgs.push_back("-disable-llvm-optzns");
2133 else
Daniel Dunbara442fd52010-06-11 22:00:13 +00002134 (*it)->render(Args, CmdArgs);
Daniel Dunbar76fa8402010-04-15 06:09:03 +00002135 }
Daniel Dunbara2aedc62009-03-18 10:01:51 +00002136
Daniel Dunbard67a3222009-03-30 06:36:42 +00002137 if (Output.getType() == types::TY_Dependencies) {
2138 // Handled with other dependency code.
Daniel Dunbar5cdf3e02009-03-19 07:29:38 +00002139 } else if (Output.isFilename()) {
Daniel Dunbara3246a02009-03-18 08:07:30 +00002140 CmdArgs.push_back("-o");
Daniel Dunbar5cdf3e02009-03-19 07:29:38 +00002141 CmdArgs.push_back(Output.getFilename());
2142 } else {
2143 assert(Output.isNothing() && "Invalid output.");
Daniel Dunbara3246a02009-03-18 08:07:30 +00002144 }
2145
Daniel Dunbara2aedc62009-03-18 10:01:51 +00002146 for (InputInfoList::const_iterator
2147 it = Inputs.begin(), ie = Inputs.end(); it != ie; ++it) {
2148 const InputInfo &II = *it;
2149 CmdArgs.push_back("-x");
2150 CmdArgs.push_back(types::getTypeName(II.getType()));
Daniel Dunbarb440f562010-08-02 02:38:21 +00002151 if (II.isFilename())
Daniel Dunbar5cdf3e02009-03-19 07:29:38 +00002152 CmdArgs.push_back(II.getFilename());
Daniel Dunbara2aedc62009-03-18 10:01:51 +00002153 else
Daniel Dunbar5cdf3e02009-03-19 07:29:38 +00002154 II.getInputArg().renderAsInput(Args, CmdArgs);
Daniel Dunbara2aedc62009-03-18 10:01:51 +00002155 }
Daniel Dunbard067f7f2009-04-08 23:54:23 +00002156
Chris Lattnere9d7d782009-11-03 19:50:27 +00002157 Args.AddAllArgs(CmdArgs, options::OPT_undef);
2158
Daniel Dunbarb31b76f2010-07-18 21:16:15 +00002159 const char *Exec = getToolChain().getDriver().getClangProgramPath();
Daniel Dunbar24c7f5e2009-12-18 02:43:17 +00002160
2161 // Optionally embed the -cc1 level arguments into the debug info, for build
2162 // analysis.
2163 if (getToolChain().UseDwarfDebugFlags()) {
Daniel Dunbar7f3d9502010-06-04 18:47:06 +00002164 ArgStringList OriginalArgs;
2165 for (ArgList::const_iterator it = Args.begin(),
2166 ie = Args.end(); it != ie; ++it)
2167 (*it)->render(Args, OriginalArgs);
Daniel Dunbarfe6c97b2010-08-24 16:47:49 +00002168
Daniel Dunbar24c7f5e2009-12-18 02:43:17 +00002169 llvm::SmallString<256> Flags;
2170 Flags += Exec;
Daniel Dunbar7f3d9502010-06-04 18:47:06 +00002171 for (unsigned i = 0, e = OriginalArgs.size(); i != e; ++i) {
Daniel Dunbar24c7f5e2009-12-18 02:43:17 +00002172 Flags += " ";
Daniel Dunbar7f3d9502010-06-04 18:47:06 +00002173 Flags += OriginalArgs[i];
Daniel Dunbar24c7f5e2009-12-18 02:43:17 +00002174 }
2175 CmdArgs.push_back("-dwarf-debug-flags");
2176 CmdArgs.push_back(Args.MakeArgString(Flags.str()));
2177 }
2178
Daniel Dunbar7fbaf532010-08-02 02:38:28 +00002179 C.addCommand(new Command(JA, *this, Exec, CmdArgs));
Daniel Dunbar17731772009-03-23 19:03:36 +00002180
Roman Divacky178e01602011-02-10 16:52:03 +00002181 if (Arg *A = Args.getLastArg(options::OPT_pg))
2182 if (Args.hasArg(options::OPT_fomit_frame_pointer))
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002183 D.Diag(diag::err_drv_argument_not_allowed_with)
Roman Divacky178e01602011-02-10 16:52:03 +00002184 << "-fomit-frame-pointer" << A->getAsString(Args);
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00002185
Daniel Dunbarc2a71892009-04-03 20:51:31 +00002186 // Claim some arguments which clang supports automatically.
2187
Daniel Dunbar3e0cac62010-04-15 06:18:42 +00002188 // -fpch-preprocess is used with gcc to add a special marker in the output to
2189 // include the PCH file. Clang's PTH solution is completely transparent, so we
2190 // do not need to deal with it at all.
Daniel Dunbarc2a71892009-04-03 20:51:31 +00002191 Args.ClaimAllArgs(options::OPT_fpch_preprocess);
Daniel Dunbard067f7f2009-04-08 23:54:23 +00002192
Daniel Dunbar17731772009-03-23 19:03:36 +00002193 // Claim some arguments which clang doesn't support, but we don't
2194 // care to warn the user about.
Daniel Dunbar44b36ee2009-11-25 11:53:23 +00002195 Args.ClaimAllArgs(options::OPT_clang_ignored_f_Group);
2196 Args.ClaimAllArgs(options::OPT_clang_ignored_m_Group);
Rafael Espindola22f603032011-02-28 23:29:45 +00002197
Rafael Espindolad95a8122011-03-01 05:25:27 +00002198 // Disable warnings for clang -E -use-gold-plugin -emit-llvm foo.c
Rafael Espindola22f603032011-02-28 23:29:45 +00002199 Args.ClaimAllArgs(options::OPT_use_gold_plugin);
Rafael Espindolad95a8122011-03-01 05:25:27 +00002200 Args.ClaimAllArgs(options::OPT_emit_llvm);
Daniel Dunbar1a093d22009-03-18 06:00:36 +00002201}
2202
Daniel Dunbar4f5e79c2010-05-20 21:30:13 +00002203void ClangAs::ConstructJob(Compilation &C, const JobAction &JA,
Daniel Dunbar4f5e79c2010-05-20 21:30:13 +00002204 const InputInfo &Output,
2205 const InputInfoList &Inputs,
2206 const ArgList &Args,
2207 const char *LinkingOutput) const {
Daniel Dunbar4f5e79c2010-05-20 21:30:13 +00002208 ArgStringList CmdArgs;
2209
2210 assert(Inputs.size() == 1 && "Unexpected number of inputs.");
2211 const InputInfo &Input = Inputs[0];
2212
Rafael Espindolacfaadda2010-11-17 22:13:25 +00002213 // Don't warn about "clang -w -c foo.s"
2214 Args.ClaimAllArgs(options::OPT_w);
Rafael Espindolad95a8122011-03-01 05:25:27 +00002215 // and "clang -emit-llvm -c foo.s"
2216 Args.ClaimAllArgs(options::OPT_emit_llvm);
2217 // and "clang -use-gold-plugin -c foo.s"
2218 Args.ClaimAllArgs(options::OPT_use_gold_plugin);
Rafael Espindolacfaadda2010-11-17 22:13:25 +00002219
Daniel Dunbar4f5e79c2010-05-20 21:30:13 +00002220 // Invoke ourselves in -cc1as mode.
2221 //
2222 // FIXME: Implement custom jobs for internal actions.
2223 CmdArgs.push_back("-cc1as");
2224
2225 // Add the "effective" target triple.
2226 CmdArgs.push_back("-triple");
Daniel Dunbar82eb4ce2010-08-23 22:35:37 +00002227 std::string TripleStr = getToolChain().ComputeEffectiveClangTriple(Args);
Daniel Dunbar4f5e79c2010-05-20 21:30:13 +00002228 CmdArgs.push_back(Args.MakeArgString(TripleStr));
2229
2230 // Set the output mode, we currently only expect to be used as a real
2231 // assembler.
2232 CmdArgs.push_back("-filetype");
2233 CmdArgs.push_back("obj");
2234
Joerg Sonnenbergeref317a22011-05-06 14:35:16 +00002235 if (UseRelaxAll(C, Args))
Daniel Dunbar99ca8b72010-05-28 16:43:21 +00002236 CmdArgs.push_back("-relax-all");
Daniel Dunbar06e2cc32010-05-27 06:18:05 +00002237
Daniel Dunbar1d733e22011-03-17 17:37:29 +00002238 // Ignore explicit -force_cpusubtype_ALL option.
2239 (void) Args.hasArg(options::OPT_force__cpusubtype__ALL);
Daniel Dunbar4f5e79c2010-05-20 21:30:13 +00002240
2241 // FIXME: Add -g support, once we have it.
2242
2243 // FIXME: Add -static support, once we have it.
2244
2245 Args.AddAllArgValues(CmdArgs, options::OPT_Wa_COMMA,
2246 options::OPT_Xassembler);
Daniel Dunbar252e8f92011-04-29 17:53:18 +00002247 Args.AddAllArgs(CmdArgs, options::OPT_mllvm);
Daniel Dunbar4f5e79c2010-05-20 21:30:13 +00002248
2249 assert(Output.isFilename() && "Unexpected lipo output.");
2250 CmdArgs.push_back("-o");
2251 CmdArgs.push_back(Output.getFilename());
2252
Daniel Dunbarb440f562010-08-02 02:38:21 +00002253 assert(Input.isFilename() && "Invalid input.");
2254 CmdArgs.push_back(Input.getFilename());
Daniel Dunbar4f5e79c2010-05-20 21:30:13 +00002255
Daniel Dunbarb31b76f2010-07-18 21:16:15 +00002256 const char *Exec = getToolChain().getDriver().getClangProgramPath();
Daniel Dunbar7fbaf532010-08-02 02:38:28 +00002257 C.addCommand(new Command(JA, *this, Exec, CmdArgs));
Daniel Dunbar4f5e79c2010-05-20 21:30:13 +00002258}
2259
Daniel Dunbara3246a02009-03-18 08:07:30 +00002260void gcc::Common::ConstructJob(Compilation &C, const JobAction &JA,
Daniel Dunbara3246a02009-03-18 08:07:30 +00002261 const InputInfo &Output,
2262 const InputInfoList &Inputs,
Daniel Dunbara2aedc62009-03-18 10:01:51 +00002263 const ArgList &Args,
Daniel Dunbara3246a02009-03-18 08:07:30 +00002264 const char *LinkingOutput) const {
Daniel Dunbar083edf72009-12-21 18:54:17 +00002265 const Driver &D = getToolChain().getDriver();
Daniel Dunbara3246a02009-03-18 08:07:30 +00002266 ArgStringList CmdArgs;
Daniel Dunbar1a093d22009-03-18 06:00:36 +00002267
Daniel Dunbard067f7f2009-04-08 23:54:23 +00002268 for (ArgList::const_iterator
Daniel Dunbara2aedc62009-03-18 10:01:51 +00002269 it = Args.begin(), ie = Args.end(); it != ie; ++it) {
Daniel Dunbara3246a02009-03-18 08:07:30 +00002270 Arg *A = *it;
Daniel Dunbar2da02722009-03-19 07:55:12 +00002271 if (A->getOption().hasForwardToGCC()) {
Daniel Dunbar939c1212010-08-03 16:14:14 +00002272 // Don't forward any -g arguments to assembly steps.
2273 if (isa<AssembleJobAction>(JA) &&
2274 A->getOption().matches(options::OPT_g_Group))
2275 continue;
2276
Daniel Dunbar2da02722009-03-19 07:55:12 +00002277 // It is unfortunate that we have to claim here, as this means
2278 // we will basically never report anything interesting for
Daniel Dunbar5716d872009-05-02 21:41:52 +00002279 // platforms using a generic gcc, even if we are just using gcc
2280 // to get to the assembler.
Daniel Dunbar2da02722009-03-19 07:55:12 +00002281 A->claim();
Daniel Dunbara2aedc62009-03-18 10:01:51 +00002282 A->render(Args, CmdArgs);
Daniel Dunbar2da02722009-03-19 07:55:12 +00002283 }
Daniel Dunbara3246a02009-03-18 08:07:30 +00002284 }
Daniel Dunbard067f7f2009-04-08 23:54:23 +00002285
Daniel Dunbar4e295052010-01-25 22:35:08 +00002286 RenderExtraToolArgs(JA, CmdArgs);
Daniel Dunbara3246a02009-03-18 08:07:30 +00002287
2288 // If using a driver driver, force the arch.
Daniel Dunbar5bbebfe2009-05-22 02:21:04 +00002289 const std::string &Arch = getToolChain().getArchName();
Daniel Dunbarf4894fe2009-12-23 00:46:38 +00002290 if (getToolChain().getTriple().getOS() == llvm::Triple::Darwin) {
Daniel Dunbara3246a02009-03-18 08:07:30 +00002291 CmdArgs.push_back("-arch");
Daniel Dunbar0a05d932009-04-01 20:33:11 +00002292
2293 // FIXME: Remove these special cases.
Daniel Dunbar5bbebfe2009-05-22 02:21:04 +00002294 if (Arch == "powerpc")
2295 CmdArgs.push_back("ppc");
2296 else if (Arch == "powerpc64")
2297 CmdArgs.push_back("ppc64");
2298 else
Daniel Dunbar1c28f1e2009-09-09 22:32:48 +00002299 CmdArgs.push_back(Args.MakeArgString(Arch));
Daniel Dunbara3246a02009-03-18 08:07:30 +00002300 }
2301
Daniel Dunbar5716d872009-05-02 21:41:52 +00002302 // Try to force gcc to match the tool chain we want, if we recognize
2303 // the arch.
Daniel Dunbar5bbebfe2009-05-22 02:21:04 +00002304 //
2305 // FIXME: The triple class should directly provide the information we want
2306 // here.
2307 if (Arch == "i386" || Arch == "powerpc")
Daniel Dunbar5716d872009-05-02 21:41:52 +00002308 CmdArgs.push_back("-m32");
Daniel Dunbar5bbebfe2009-05-22 02:21:04 +00002309 else if (Arch == "x86_64" || Arch == "powerpc64")
Daniel Dunbar5716d872009-05-02 21:41:52 +00002310 CmdArgs.push_back("-m64");
2311
Daniel Dunbarb440f562010-08-02 02:38:21 +00002312 if (Output.isFilename()) {
Daniel Dunbara3246a02009-03-18 08:07:30 +00002313 CmdArgs.push_back("-o");
Daniel Dunbar5cdf3e02009-03-19 07:29:38 +00002314 CmdArgs.push_back(Output.getFilename());
2315 } else {
2316 assert(Output.isNothing() && "Unexpected output");
Daniel Dunbara3246a02009-03-18 08:07:30 +00002317 CmdArgs.push_back("-fsyntax-only");
Daniel Dunbar5cdf3e02009-03-19 07:29:38 +00002318 }
Daniel Dunbara3246a02009-03-18 08:07:30 +00002319
2320
2321 // Only pass -x if gcc will understand it; otherwise hope gcc
2322 // understands the suffix correctly. The main use case this would go
2323 // wrong in is for linker inputs if they happened to have an odd
2324 // suffix; really the only way to get this to happen is a command
2325 // like '-x foobar a.c' which will treat a.c like a linker input.
2326 //
2327 // FIXME: For the linker case specifically, can we safely convert
2328 // inputs into '-Wl,' options?
2329 for (InputInfoList::const_iterator
2330 it = Inputs.begin(), ie = Inputs.end(); it != ie; ++it) {
2331 const InputInfo &II = *it;
Daniel Dunbare3e263f2009-05-02 20:14:53 +00002332
Daniel Dunbar6cdf83c2009-09-01 16:57:46 +00002333 // Don't try to pass LLVM or AST inputs to a generic gcc.
Daniel Dunbar24e52992010-06-07 23:28:45 +00002334 if (II.getType() == types::TY_LLVM_IR || II.getType() == types::TY_LTO_IR ||
2335 II.getType() == types::TY_LLVM_BC || II.getType() == types::TY_LTO_BC)
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002336 D.Diag(diag::err_drv_no_linker_llvm_support)
Daniel Dunbar1c28f1e2009-09-09 22:32:48 +00002337 << getToolChain().getTripleString();
Daniel Dunbar6cdf83c2009-09-01 16:57:46 +00002338 else if (II.getType() == types::TY_AST)
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002339 D.Diag(diag::err_drv_no_ast_support)
Daniel Dunbar1c28f1e2009-09-09 22:32:48 +00002340 << getToolChain().getTripleString();
Daniel Dunbare3e263f2009-05-02 20:14:53 +00002341
Daniel Dunbara3246a02009-03-18 08:07:30 +00002342 if (types::canTypeBeUserSpecified(II.getType())) {
2343 CmdArgs.push_back("-x");
2344 CmdArgs.push_back(types::getTypeName(II.getType()));
2345 }
2346
Daniel Dunbarb440f562010-08-02 02:38:21 +00002347 if (II.isFilename())
Daniel Dunbar5cdf3e02009-03-19 07:29:38 +00002348 CmdArgs.push_back(II.getFilename());
Daniel Dunbarf2476752010-09-25 18:10:05 +00002349 else {
2350 const Arg &A = II.getInputArg();
2351
2352 // Reverse translate some rewritten options.
2353 if (A.getOption().matches(options::OPT_Z_reserved_lib_stdcxx)) {
2354 CmdArgs.push_back("-lstdc++");
2355 continue;
2356 }
2357
Daniel Dunbar5cdf3e02009-03-19 07:29:38 +00002358 // Don't render as input, we need gcc to do the translations.
Daniel Dunbarf2476752010-09-25 18:10:05 +00002359 A.render(Args, CmdArgs);
2360 }
Daniel Dunbara3246a02009-03-18 08:07:30 +00002361 }
2362
Dylan Noblesmith70e73a32011-04-09 13:31:59 +00002363 const std::string customGCCName = D.getCCCGenericGCCName();
2364 const char *GCCName;
2365 if (!customGCCName.empty())
2366 GCCName = customGCCName.c_str();
2367 else if (D.CCCIsCXX) {
2368#ifdef IS_CYGWIN15
2369 // FIXME: Detect the version of Cygwin at runtime?
2370 GCCName = "g++-4";
2371#else
2372 GCCName = "g++";
2373#endif
2374 } else
2375 GCCName = "gcc";
2376
Daniel Dunbard067f7f2009-04-08 23:54:23 +00002377 const char *Exec =
Daniel Dunbar9c3ed5f2010-07-14 18:46:23 +00002378 Args.MakeArgString(getToolChain().GetProgramPath(GCCName));
Daniel Dunbar7fbaf532010-08-02 02:38:28 +00002379 C.addCommand(new Command(JA, *this, Exec, CmdArgs));
Daniel Dunbar1a093d22009-03-18 06:00:36 +00002380}
2381
Daniel Dunbar4e295052010-01-25 22:35:08 +00002382void gcc::Preprocess::RenderExtraToolArgs(const JobAction &JA,
2383 ArgStringList &CmdArgs) const {
Daniel Dunbara3246a02009-03-18 08:07:30 +00002384 CmdArgs.push_back("-E");
Daniel Dunbar1a093d22009-03-18 06:00:36 +00002385}
2386
Daniel Dunbar4e295052010-01-25 22:35:08 +00002387void gcc::Precompile::RenderExtraToolArgs(const JobAction &JA,
2388 ArgStringList &CmdArgs) const {
Daniel Dunbara3246a02009-03-18 08:07:30 +00002389 // The type is good enough.
Daniel Dunbar1a093d22009-03-18 06:00:36 +00002390}
2391
Daniel Dunbar4e295052010-01-25 22:35:08 +00002392void gcc::Compile::RenderExtraToolArgs(const JobAction &JA,
2393 ArgStringList &CmdArgs) const {
Daniel Dunbaraeea8ac2010-02-11 03:16:21 +00002394 const Driver &D = getToolChain().getDriver();
2395
Daniel Dunbar4e295052010-01-25 22:35:08 +00002396 // If -flto, etc. are present then make sure not to force assembly output.
Daniel Dunbar24e52992010-06-07 23:28:45 +00002397 if (JA.getType() == types::TY_LLVM_IR || JA.getType() == types::TY_LTO_IR ||
2398 JA.getType() == types::TY_LLVM_BC || JA.getType() == types::TY_LTO_BC)
Daniel Dunbar4e295052010-01-25 22:35:08 +00002399 CmdArgs.push_back("-c");
Daniel Dunbaraeea8ac2010-02-11 03:16:21 +00002400 else {
2401 if (JA.getType() != types::TY_PP_Asm)
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002402 D.Diag(diag::err_drv_invalid_gcc_output_type)
Daniel Dunbaraeea8ac2010-02-11 03:16:21 +00002403 << getTypeName(JA.getType());
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00002404
Daniel Dunbar4e295052010-01-25 22:35:08 +00002405 CmdArgs.push_back("-S");
Daniel Dunbaraeea8ac2010-02-11 03:16:21 +00002406 }
Daniel Dunbar1a093d22009-03-18 06:00:36 +00002407}
2408
Daniel Dunbar4e295052010-01-25 22:35:08 +00002409void gcc::Assemble::RenderExtraToolArgs(const JobAction &JA,
2410 ArgStringList &CmdArgs) const {
Daniel Dunbara3246a02009-03-18 08:07:30 +00002411 CmdArgs.push_back("-c");
Daniel Dunbar1a093d22009-03-18 06:00:36 +00002412}
Daniel Dunbara3246a02009-03-18 08:07:30 +00002413
Daniel Dunbar4e295052010-01-25 22:35:08 +00002414void gcc::Link::RenderExtraToolArgs(const JobAction &JA,
2415 ArgStringList &CmdArgs) const {
Daniel Dunbara3246a02009-03-18 08:07:30 +00002416 // The types are (hopefully) good enough.
2417}
2418
Daniel Dunbare6adeee2009-03-29 17:08:39 +00002419const char *darwin::CC1::getCC1Name(types::ID Type) const {
2420 switch (Type) {
2421 default:
2422 assert(0 && "Unexpected type for Darwin CC1 tool.");
2423 case types::TY_Asm:
2424 case types::TY_C: case types::TY_CHeader:
2425 case types::TY_PP_C: case types::TY_PP_CHeader:
2426 return "cc1";
2427 case types::TY_ObjC: case types::TY_ObjCHeader:
Nico Webered8080c2011-08-13 23:13:37 +00002428 case types::TY_PP_ObjC: case types::TY_PP_ObjC_Alias:
2429 case types::TY_PP_ObjCHeader:
Daniel Dunbare6adeee2009-03-29 17:08:39 +00002430 return "cc1obj";
2431 case types::TY_CXX: case types::TY_CXXHeader:
2432 case types::TY_PP_CXX: case types::TY_PP_CXXHeader:
2433 return "cc1plus";
2434 case types::TY_ObjCXX: case types::TY_ObjCXXHeader:
Nico Webered8080c2011-08-13 23:13:37 +00002435 case types::TY_PP_ObjCXX: case types::TY_PP_ObjCXX_Alias:
2436 case types::TY_PP_ObjCXXHeader:
Daniel Dunbare6adeee2009-03-29 17:08:39 +00002437 return "cc1objplus";
2438 }
2439}
2440
Daniel Dunbard067f7f2009-04-08 23:54:23 +00002441const char *darwin::CC1::getBaseInputName(const ArgList &Args,
Daniel Dunbar52e96cc2009-03-30 00:34:04 +00002442 const InputInfoList &Inputs) {
Michael J. Spencere1696752010-12-18 00:19:12 +00002443 return Args.MakeArgString(
2444 llvm::sys::path::filename(Inputs[0].getBaseInput()));
Daniel Dunbarafec1f52009-03-29 18:40:18 +00002445}
2446
Daniel Dunbard067f7f2009-04-08 23:54:23 +00002447const char *darwin::CC1::getBaseInputStem(const ArgList &Args,
Daniel Dunbar52e96cc2009-03-30 00:34:04 +00002448 const InputInfoList &Inputs) {
Daniel Dunbarafec1f52009-03-29 18:40:18 +00002449 const char *Str = getBaseInputName(Args, Inputs);
2450
Chris Lattner906bb902011-01-16 08:14:11 +00002451 if (const char *End = strrchr(Str, '.'))
Daniel Dunbar1c28f1e2009-09-09 22:32:48 +00002452 return Args.MakeArgString(std::string(Str, End));
Daniel Dunbarafec1f52009-03-29 18:40:18 +00002453
2454 return Str;
2455}
2456
2457const char *
Daniel Dunbard067f7f2009-04-08 23:54:23 +00002458darwin::CC1::getDependencyFileName(const ArgList &Args,
Daniel Dunbar52e96cc2009-03-30 00:34:04 +00002459 const InputInfoList &Inputs) {
Daniel Dunbarafec1f52009-03-29 18:40:18 +00002460 // FIXME: Think about this more.
2461 std::string Res;
2462
2463 if (Arg *OutputOpt = Args.getLastArg(options::OPT_o)) {
2464 std::string Str(OutputOpt->getValue(Args));
Daniel Dunbarafec1f52009-03-29 18:40:18 +00002465 Res = Str.substr(0, Str.rfind('.'));
Chad Rosier6fdf38b2011-08-17 23:08:45 +00002466 } else {
Daniel Dunbar52e96cc2009-03-30 00:34:04 +00002467 Res = darwin::CC1::getBaseInputStem(Args, Inputs);
Chad Rosier6fdf38b2011-08-17 23:08:45 +00002468 }
Daniel Dunbar1c28f1e2009-09-09 22:32:48 +00002469 return Args.MakeArgString(Res + ".d");
Daniel Dunbarafec1f52009-03-29 18:40:18 +00002470}
2471
Chad Rosierbc5ea3d2011-08-17 18:24:55 +00002472void darwin::CC1::RemoveCC1UnsupportedArgs(ArgStringList &CmdArgs) const {
Eric Christopher84fbdb42011-08-19 00:30:14 +00002473 for (ArgStringList::iterator it = CmdArgs.begin(), ie = CmdArgs.end();
Chad Rosierbc5ea3d2011-08-17 18:24:55 +00002474 it != ie;) {
Chad Rosier2f818792011-08-18 17:56:32 +00002475
2476 StringRef Option = *it;
Chad Rosierf29d9aa2011-08-26 18:30:43 +00002477 bool RemoveOption = false;
Chad Rosier30453862011-08-18 01:18:28 +00002478
Chad Rosierf29d9aa2011-08-26 18:30:43 +00002479 // Remove -faltivec
2480 if (Option.equals("-faltivec")) {
2481 it = CmdArgs.erase(it);
2482 ie = CmdArgs.end();
Chad Rosier30453862011-08-18 01:18:28 +00002483 continue;
2484 }
2485
Chad Rosierf29d9aa2011-08-26 18:30:43 +00002486 // Handle machine specific options.
2487 if (Option.startswith("-m")) {
2488 RemoveOption = llvm::StringSwitch<bool>(Option)
2489 .Case("-mthumb", true)
2490 .Case("-mno-thumb", true)
2491 .Case("-mno-fused-madd", true)
2492 .Case("-mlong-branch", true)
2493 .Case("-mlongcall", true)
2494 .Case("-mcpu=G4", true)
2495 .Case("-mcpu=G5", true)
2496 .Default(false);
2497 }
2498
2499 // Handle warning options.
2500 if (Option.startswith("-W")) {
2501 // Remove -W/-Wno- to reduce the number of cases.
2502 if (Option.startswith("-Wno-"))
2503 Option = Option.substr(5);
2504 else
2505 Option = Option.substr(2);
2506
2507 RemoveOption = llvm::StringSwitch<bool>(Option)
2508 .Case("address-of-temporary", true)
2509 .Case("ambiguous-member-template", true)
2510 .Case("analyzer-incompatible-plugin", true)
2511 .Case("array-bounds", true)
2512 .Case("array-bounds-pointer-arithmetic", true)
2513 .Case("bind-to-temporary-copy", true)
2514 .Case("bitwise-op-parentheses", true)
2515 .Case("bool-conversions", true)
2516 .Case("builtin-macro-redefined", true)
2517 .Case("c++-hex-floats", true)
2518 .Case("c++0x-compat", true)
2519 .Case("c++0x-extensions", true)
2520 .Case("c++0x-narrowing", true)
2521 .Case("c++0x-static-nonintegral-init", true)
2522 .Case("conditional-uninitialized", true)
2523 .Case("constant-conversion", true)
2524 .Case("CFString-literal", true)
2525 .Case("constant-logical-operand", true)
2526 .Case("custom-atomic-properties", true)
2527 .Case("default-arg-special-member", true)
2528 .Case("delegating-ctor-cycles", true)
2529 .Case("delete-non-virtual-dtor", true)
2530 .Case("deprecated-implementations", true)
2531 .Case("deprecated-writable-strings", true)
2532 .Case("distributed-object-modifiers", true)
2533 .Case("duplicate-method-arg", true)
2534 .Case("dynamic-class-memaccess", true)
2535 .Case("enum-compare", true)
2536 .Case("exit-time-destructors", true)
2537 .Case("gnu", true)
2538 .Case("gnu-designator", true)
2539 .Case("header-hygiene", true)
2540 .Case("idiomatic-parentheses", true)
2541 .Case("ignored-qualifiers", true)
2542 .Case("implicit-atomic-properties", true)
2543 .Case("incompatible-pointer-types", true)
2544 .Case("incomplete-implementation", true)
2545 .Case("initializer-overrides", true)
2546 .Case("invalid-noreturn", true)
2547 .Case("invalid-token-paste", true)
2548 .Case("literal-conversion", true)
2549 .Case("literal-range", true)
2550 .Case("local-type-template-args", true)
2551 .Case("logical-op-parentheses", true)
2552 .Case("method-signatures", true)
2553 .Case("microsoft", true)
2554 .Case("mismatched-tags", true)
2555 .Case("missing-method-return-type", true)
2556 .Case("non-pod-varargs", true)
2557 .Case("nonfragile-abi2", true)
2558 .Case("null-arithmetic", true)
2559 .Case("null-dereference", true)
2560 .Case("out-of-line-declaration", true)
2561 .Case("overriding-method-mismatch", true)
2562 .Case("readonly-setter-attrs", true)
2563 .Case("return-stack-address", true)
2564 .Case("self-assign", true)
2565 .Case("semicolon-before-method-body", true)
2566 .Case("sentinel", true)
2567 .Case("shift-overflow", true)
2568 .Case("shift-sign-overflow", true)
2569 .Case("sign-conversion", true)
2570 .Case("sizeof-array-argument", true)
2571 .Case("sizeof-pointer-memaccess", true)
2572 .Case("string-compare", true)
2573 .Case("super-class-method-mismatch", true)
2574 .Case("tautological-compare", true)
2575 .Case("typedef-redefinition", true)
2576 .Case("typename-missing", true)
2577 .Case("undefined-reinterpret-cast", true)
2578 .Case("unknown-warning-option", true)
2579 .Case("unnamed-type-template-args", true)
2580 .Case("unneeded-internal-declaration", true)
2581 .Case("unneeded-member-function", true)
2582 .Case("unused-comparison", true)
2583 .Case("unused-exception-parameter", true)
2584 .Case("unused-member-function", true)
2585 .Case("unused-result", true)
2586 .Case("vector-conversions", true)
2587 .Case("vla", true)
2588 .Case("used-but-marked-unused", true)
2589 .Case("weak-vtables", true)
2590 .Default(false);
2591 } // if (Option.startswith("-W"))
Chad Rosier30453862011-08-18 01:18:28 +00002592 if (RemoveOption) {
Chad Rosierbc5ea3d2011-08-17 18:24:55 +00002593 it = CmdArgs.erase(it);
Chad Rosier23594f62011-08-17 18:51:56 +00002594 ie = CmdArgs.end();
Chad Rosier6fdf38b2011-08-17 23:08:45 +00002595 } else {
Chad Rosierbc5ea3d2011-08-17 18:24:55 +00002596 ++it;
Chad Rosier6fdf38b2011-08-17 23:08:45 +00002597 }
Chad Rosierbc5ea3d2011-08-17 18:24:55 +00002598 }
2599}
2600
Daniel Dunbard067f7f2009-04-08 23:54:23 +00002601void darwin::CC1::AddCC1Args(const ArgList &Args,
Daniel Dunbarafec1f52009-03-29 18:40:18 +00002602 ArgStringList &CmdArgs) const {
Daniel Dunbar083edf72009-12-21 18:54:17 +00002603 const Driver &D = getToolChain().getDriver();
Daniel Dunbar4eadb602009-09-10 01:21:12 +00002604
2605 CheckCodeGenerationOptions(D, Args);
2606
Daniel Dunbarafec1f52009-03-29 18:40:18 +00002607 // Derived from cc1 spec.
Daniel Dunbarafec1f52009-03-29 18:40:18 +00002608 if (!Args.hasArg(options::OPT_mkernel) && !Args.hasArg(options::OPT_static) &&
2609 !Args.hasArg(options::OPT_mdynamic_no_pic))
2610 CmdArgs.push_back("-fPIC");
2611
Daniel Dunbar2ffe0292009-09-10 03:37:02 +00002612 if (getToolChain().getTriple().getArch() == llvm::Triple::arm ||
2613 getToolChain().getTriple().getArch() == llvm::Triple::thumb) {
2614 if (!Args.hasArg(options::OPT_fbuiltin_strcat))
2615 CmdArgs.push_back("-fno-builtin-strcat");
2616 if (!Args.hasArg(options::OPT_fbuiltin_strcpy))
2617 CmdArgs.push_back("-fno-builtin-strcpy");
2618 }
2619
Daniel Dunbarafec1f52009-03-29 18:40:18 +00002620 if (Args.hasArg(options::OPT_g_Flag) &&
2621 !Args.hasArg(options::OPT_fno_eliminate_unused_debug_symbols))
2622 CmdArgs.push_back("-feliminate-unused-debug-symbols");
2623}
2624
2625void darwin::CC1::AddCC1OptionsArgs(const ArgList &Args, ArgStringList &CmdArgs,
2626 const InputInfoList &Inputs,
2627 const ArgStringList &OutputArgs) const {
Daniel Dunbar083edf72009-12-21 18:54:17 +00002628 const Driver &D = getToolChain().getDriver();
Daniel Dunbarafec1f52009-03-29 18:40:18 +00002629
2630 // Derived from cc1_options spec.
2631 if (Args.hasArg(options::OPT_fast) ||
2632 Args.hasArg(options::OPT_fastf) ||
2633 Args.hasArg(options::OPT_fastcp))
2634 CmdArgs.push_back("-O3");
Daniel Dunbard067f7f2009-04-08 23:54:23 +00002635
Daniel Dunbarafec1f52009-03-29 18:40:18 +00002636 if (Arg *A = Args.getLastArg(options::OPT_pg))
2637 if (Args.hasArg(options::OPT_fomit_frame_pointer))
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002638 D.Diag(diag::err_drv_argument_not_allowed_with)
Daniel Dunbarafec1f52009-03-29 18:40:18 +00002639 << A->getAsString(Args) << "-fomit-frame-pointer";
2640
2641 AddCC1Args(Args, CmdArgs);
2642
2643 if (!Args.hasArg(options::OPT_Q))
2644 CmdArgs.push_back("-quiet");
2645
2646 CmdArgs.push_back("-dumpbase");
Daniel Dunbar52e96cc2009-03-30 00:34:04 +00002647 CmdArgs.push_back(darwin::CC1::getBaseInputName(Args, Inputs));
Daniel Dunbarafec1f52009-03-29 18:40:18 +00002648
2649 Args.AddAllArgs(CmdArgs, options::OPT_d_Group);
2650
2651 Args.AddAllArgs(CmdArgs, options::OPT_m_Group);
2652 Args.AddAllArgs(CmdArgs, options::OPT_a_Group);
2653
2654 // FIXME: The goal is to use the user provided -o if that is our
2655 // final output, otherwise to drive from the original input
2656 // name. Find a clean way to go about this.
2657 if ((Args.hasArg(options::OPT_c) || Args.hasArg(options::OPT_S)) &&
2658 Args.hasArg(options::OPT_o)) {
2659 Arg *OutputOpt = Args.getLastArg(options::OPT_o);
2660 CmdArgs.push_back("-auxbase-strip");
2661 CmdArgs.push_back(OutputOpt->getValue(Args));
2662 } else {
2663 CmdArgs.push_back("-auxbase");
Daniel Dunbar52e96cc2009-03-30 00:34:04 +00002664 CmdArgs.push_back(darwin::CC1::getBaseInputStem(Args, Inputs));
Daniel Dunbarafec1f52009-03-29 18:40:18 +00002665 }
2666
2667 Args.AddAllArgs(CmdArgs, options::OPT_g_Group);
2668
2669 Args.AddAllArgs(CmdArgs, options::OPT_O);
2670 // FIXME: -Wall is getting some special treatment. Investigate.
2671 Args.AddAllArgs(CmdArgs, options::OPT_W_Group, options::OPT_pedantic_Group);
2672 Args.AddLastArg(CmdArgs, options::OPT_w);
Daniel Dunbard067f7f2009-04-08 23:54:23 +00002673 Args.AddAllArgs(CmdArgs, options::OPT_std_EQ, options::OPT_ansi,
Daniel Dunbarafec1f52009-03-29 18:40:18 +00002674 options::OPT_trigraphs);
Daniel Dunbar72a60902009-04-26 01:10:38 +00002675 if (!Args.getLastArg(options::OPT_std_EQ, options::OPT_ansi)) {
2676 // Honor -std-default.
2677 Args.AddAllArgsTranslated(CmdArgs, options::OPT_std_default_EQ,
2678 "-std=", /*Joined=*/true);
2679 }
2680
Daniel Dunbarafec1f52009-03-29 18:40:18 +00002681 if (Args.hasArg(options::OPT_v))
2682 CmdArgs.push_back("-version");
Daniel Dunbar733b0f82011-03-01 18:49:30 +00002683 if (Args.hasArg(options::OPT_pg) &&
2684 getToolChain().SupportsProfiling())
Daniel Dunbarafec1f52009-03-29 18:40:18 +00002685 CmdArgs.push_back("-p");
2686 Args.AddLastArg(CmdArgs, options::OPT_p);
Daniel Dunbard067f7f2009-04-08 23:54:23 +00002687
Daniel Dunbarafec1f52009-03-29 18:40:18 +00002688 // The driver treats -fsyntax-only specially.
Daniel Dunbar2ffe0292009-09-10 03:37:02 +00002689 if (getToolChain().getTriple().getArch() == llvm::Triple::arm ||
2690 getToolChain().getTriple().getArch() == llvm::Triple::thumb) {
2691 // Removes -fbuiltin-str{cat,cpy}; these aren't recognized by cc1 but are
2692 // used to inhibit the default -fno-builtin-str{cat,cpy}.
2693 //
2694 // FIXME: Should we grow a better way to deal with "removing" args?
Daniel Dunbar44b36ee2009-11-25 11:53:23 +00002695 for (arg_iterator it = Args.filtered_begin(options::OPT_f_Group,
2696 options::OPT_fsyntax_only),
2697 ie = Args.filtered_end(); it != ie; ++it) {
Daniel Dunbara442fd52010-06-11 22:00:13 +00002698 if (!(*it)->getOption().matches(options::OPT_fbuiltin_strcat) &&
2699 !(*it)->getOption().matches(options::OPT_fbuiltin_strcpy)) {
2700 (*it)->claim();
2701 (*it)->render(Args, CmdArgs);
Daniel Dunbar2ffe0292009-09-10 03:37:02 +00002702 }
2703 }
2704 } else
2705 Args.AddAllArgs(CmdArgs, options::OPT_f_Group, options::OPT_fsyntax_only);
Daniel Dunbard067f7f2009-04-08 23:54:23 +00002706
Daniel Dunbarf28c2ff2011-04-07 20:41:03 +00002707 // Claim Clang only -f options, they aren't worth warning about.
2708 Args.ClaimAllArgs(options::OPT_f_clang_Group);
2709
Daniel Dunbarafec1f52009-03-29 18:40:18 +00002710 Args.AddAllArgs(CmdArgs, options::OPT_undef);
2711 if (Args.hasArg(options::OPT_Qn))
2712 CmdArgs.push_back("-fno-ident");
Daniel Dunbard067f7f2009-04-08 23:54:23 +00002713
Daniel Dunbarafec1f52009-03-29 18:40:18 +00002714 // FIXME: This isn't correct.
2715 //Args.AddLastArg(CmdArgs, options::OPT__help)
2716 //Args.AddLastArg(CmdArgs, options::OPT__targetHelp)
2717
2718 CmdArgs.append(OutputArgs.begin(), OutputArgs.end());
2719
2720 // FIXME: Still don't get what is happening here. Investigate.
2721 Args.AddAllArgs(CmdArgs, options::OPT__param);
2722
2723 if (Args.hasArg(options::OPT_fmudflap) ||
2724 Args.hasArg(options::OPT_fmudflapth)) {
2725 CmdArgs.push_back("-fno-builtin");
2726 CmdArgs.push_back("-fno-merge-constants");
2727 }
Daniel Dunbard067f7f2009-04-08 23:54:23 +00002728
Daniel Dunbarafec1f52009-03-29 18:40:18 +00002729 if (Args.hasArg(options::OPT_coverage)) {
2730 CmdArgs.push_back("-fprofile-arcs");
2731 CmdArgs.push_back("-ftest-coverage");
2732 }
2733
2734 if (types::isCXX(Inputs[0].getType()))
2735 CmdArgs.push_back("-D__private_extern__=extern");
2736}
2737
2738void darwin::CC1::AddCPPOptionsArgs(const ArgList &Args, ArgStringList &CmdArgs,
2739 const InputInfoList &Inputs,
2740 const ArgStringList &OutputArgs) const {
2741 // Derived from cpp_options
2742 AddCPPUniqueOptionsArgs(Args, CmdArgs, Inputs);
Daniel Dunbard067f7f2009-04-08 23:54:23 +00002743
Daniel Dunbarafec1f52009-03-29 18:40:18 +00002744 CmdArgs.append(OutputArgs.begin(), OutputArgs.end());
2745
2746 AddCC1Args(Args, CmdArgs);
2747
2748 // NOTE: The code below has some commonality with cpp_options, but
2749 // in classic gcc style ends up sending things in different
2750 // orders. This may be a good merge candidate once we drop pedantic
2751 // compatibility.
2752
2753 Args.AddAllArgs(CmdArgs, options::OPT_m_Group);
Daniel Dunbard067f7f2009-04-08 23:54:23 +00002754 Args.AddAllArgs(CmdArgs, options::OPT_std_EQ, options::OPT_ansi,
Daniel Dunbarafec1f52009-03-29 18:40:18 +00002755 options::OPT_trigraphs);
Daniel Dunbar72a60902009-04-26 01:10:38 +00002756 if (!Args.getLastArg(options::OPT_std_EQ, options::OPT_ansi)) {
2757 // Honor -std-default.
2758 Args.AddAllArgsTranslated(CmdArgs, options::OPT_std_default_EQ,
2759 "-std=", /*Joined=*/true);
2760 }
Daniel Dunbarafec1f52009-03-29 18:40:18 +00002761 Args.AddAllArgs(CmdArgs, options::OPT_W_Group, options::OPT_pedantic_Group);
2762 Args.AddLastArg(CmdArgs, options::OPT_w);
Daniel Dunbard067f7f2009-04-08 23:54:23 +00002763
Daniel Dunbarafec1f52009-03-29 18:40:18 +00002764 // The driver treats -fsyntax-only specially.
2765 Args.AddAllArgs(CmdArgs, options::OPT_f_Group, options::OPT_fsyntax_only);
2766
Daniel Dunbarf28c2ff2011-04-07 20:41:03 +00002767 // Claim Clang only -f options, they aren't worth warning about.
2768 Args.ClaimAllArgs(options::OPT_f_clang_Group);
2769
Daniel Dunbarafec1f52009-03-29 18:40:18 +00002770 if (Args.hasArg(options::OPT_g_Group) && !Args.hasArg(options::OPT_g0) &&
2771 !Args.hasArg(options::OPT_fno_working_directory))
2772 CmdArgs.push_back("-fworking-directory");
2773
2774 Args.AddAllArgs(CmdArgs, options::OPT_O);
2775 Args.AddAllArgs(CmdArgs, options::OPT_undef);
2776 if (Args.hasArg(options::OPT_save_temps))
2777 CmdArgs.push_back("-fpch-preprocess");
2778}
2779
Daniel Dunbard067f7f2009-04-08 23:54:23 +00002780void darwin::CC1::AddCPPUniqueOptionsArgs(const ArgList &Args,
Daniel Dunbarafec1f52009-03-29 18:40:18 +00002781 ArgStringList &CmdArgs,
Mike Stump11289f42009-09-09 15:08:12 +00002782 const InputInfoList &Inputs) const {
Daniel Dunbar083edf72009-12-21 18:54:17 +00002783 const Driver &D = getToolChain().getDriver();
Daniel Dunbarafec1f52009-03-29 18:40:18 +00002784
Daniel Dunbar64198ef2009-09-10 01:21:05 +00002785 CheckPreprocessingOptions(D, Args);
2786
Daniel Dunbarafec1f52009-03-29 18:40:18 +00002787 // Derived from cpp_unique_options.
Daniel Dunbar64198ef2009-09-10 01:21:05 +00002788 // -{C,CC} only with -E is checked in CheckPreprocessingOptions().
2789 Args.AddLastArg(CmdArgs, options::OPT_C);
2790 Args.AddLastArg(CmdArgs, options::OPT_CC);
Daniel Dunbarafec1f52009-03-29 18:40:18 +00002791 if (!Args.hasArg(options::OPT_Q))
2792 CmdArgs.push_back("-quiet");
2793 Args.AddAllArgs(CmdArgs, options::OPT_nostdinc);
Douglas Gregor64b046f2010-03-24 20:13:48 +00002794 Args.AddAllArgs(CmdArgs, options::OPT_nostdincxx);
Daniel Dunbarafec1f52009-03-29 18:40:18 +00002795 Args.AddLastArg(CmdArgs, options::OPT_v);
2796 Args.AddAllArgs(CmdArgs, options::OPT_I_Group, options::OPT_F);
2797 Args.AddLastArg(CmdArgs, options::OPT_P);
2798
2799 // FIXME: Handle %I properly.
2800 if (getToolChain().getArchName() == "x86_64") {
2801 CmdArgs.push_back("-imultilib");
2802 CmdArgs.push_back("x86_64");
2803 }
2804
2805 if (Args.hasArg(options::OPT_MD)) {
2806 CmdArgs.push_back("-MD");
Daniel Dunbar52e96cc2009-03-30 00:34:04 +00002807 CmdArgs.push_back(darwin::CC1::getDependencyFileName(Args, Inputs));
Daniel Dunbarafec1f52009-03-29 18:40:18 +00002808 }
2809
2810 if (Args.hasArg(options::OPT_MMD)) {
2811 CmdArgs.push_back("-MMD");
Daniel Dunbar52e96cc2009-03-30 00:34:04 +00002812 CmdArgs.push_back(darwin::CC1::getDependencyFileName(Args, Inputs));
Daniel Dunbarafec1f52009-03-29 18:40:18 +00002813 }
2814
2815 Args.AddLastArg(CmdArgs, options::OPT_M);
2816 Args.AddLastArg(CmdArgs, options::OPT_MM);
2817 Args.AddAllArgs(CmdArgs, options::OPT_MF);
2818 Args.AddLastArg(CmdArgs, options::OPT_MG);
2819 Args.AddLastArg(CmdArgs, options::OPT_MP);
2820 Args.AddAllArgs(CmdArgs, options::OPT_MQ);
2821 Args.AddAllArgs(CmdArgs, options::OPT_MT);
2822 if (!Args.hasArg(options::OPT_M) && !Args.hasArg(options::OPT_MM) &&
2823 (Args.hasArg(options::OPT_MD) || Args.hasArg(options::OPT_MMD))) {
2824 if (Arg *OutputOpt = Args.getLastArg(options::OPT_o)) {
2825 CmdArgs.push_back("-MQ");
2826 CmdArgs.push_back(OutputOpt->getValue(Args));
2827 }
2828 }
2829
2830 Args.AddLastArg(CmdArgs, options::OPT_remap);
2831 if (Args.hasArg(options::OPT_g3))
2832 CmdArgs.push_back("-dD");
2833 Args.AddLastArg(CmdArgs, options::OPT_H);
2834
2835 AddCPPArgs(Args, CmdArgs);
2836
2837 Args.AddAllArgs(CmdArgs, options::OPT_D, options::OPT_U, options::OPT_A);
2838 Args.AddAllArgs(CmdArgs, options::OPT_i_Group);
2839
2840 for (InputInfoList::const_iterator
2841 it = Inputs.begin(), ie = Inputs.end(); it != ie; ++it) {
2842 const InputInfo &II = *it;
Daniel Dunbard067f7f2009-04-08 23:54:23 +00002843
Daniel Dunbarb440f562010-08-02 02:38:21 +00002844 CmdArgs.push_back(II.getFilename());
Daniel Dunbarafec1f52009-03-29 18:40:18 +00002845 }
2846
2847 Args.AddAllArgValues(CmdArgs, options::OPT_Wp_COMMA,
2848 options::OPT_Xpreprocessor);
2849
2850 if (Args.hasArg(options::OPT_fmudflap)) {
2851 CmdArgs.push_back("-D_MUDFLAP");
2852 CmdArgs.push_back("-include");
2853 CmdArgs.push_back("mf-runtime.h");
2854 }
2855
2856 if (Args.hasArg(options::OPT_fmudflapth)) {
2857 CmdArgs.push_back("-D_MUDFLAP");
2858 CmdArgs.push_back("-D_MUDFLAPTH");
2859 CmdArgs.push_back("-include");
2860 CmdArgs.push_back("mf-runtime.h");
2861 }
2862}
2863
Daniel Dunbard067f7f2009-04-08 23:54:23 +00002864void darwin::CC1::AddCPPArgs(const ArgList &Args,
Daniel Dunbarafec1f52009-03-29 18:40:18 +00002865 ArgStringList &CmdArgs) const {
2866 // Derived from cpp spec.
2867
2868 if (Args.hasArg(options::OPT_static)) {
2869 // The gcc spec is broken here, it refers to dynamic but
2870 // that has been translated. Start by being bug compatible.
Daniel Dunbard067f7f2009-04-08 23:54:23 +00002871
Daniel Dunbarafec1f52009-03-29 18:40:18 +00002872 // if (!Args.hasArg(arglist.parser.dynamicOption))
2873 CmdArgs.push_back("-D__STATIC__");
2874 } else
2875 CmdArgs.push_back("-D__DYNAMIC__");
2876
2877 if (Args.hasArg(options::OPT_pthread))
2878 CmdArgs.push_back("-D_REENTRANT");
2879}
2880
Daniel Dunbare6adeee2009-03-29 17:08:39 +00002881void darwin::Preprocess::ConstructJob(Compilation &C, const JobAction &JA,
Daniel Dunbar7fbaf532010-08-02 02:38:28 +00002882 const InputInfo &Output,
Daniel Dunbard067f7f2009-04-08 23:54:23 +00002883 const InputInfoList &Inputs,
2884 const ArgList &Args,
Daniel Dunbare6adeee2009-03-29 17:08:39 +00002885 const char *LinkingOutput) const {
2886 ArgStringList CmdArgs;
2887
2888 assert(Inputs.size() == 1 && "Unexpected number of inputs!");
2889
2890 CmdArgs.push_back("-E");
2891
2892 if (Args.hasArg(options::OPT_traditional) ||
Daniel Dunbare6adeee2009-03-29 17:08:39 +00002893 Args.hasArg(options::OPT_traditional_cpp))
2894 CmdArgs.push_back("-traditional-cpp");
Daniel Dunbard067f7f2009-04-08 23:54:23 +00002895
Daniel Dunbare6adeee2009-03-29 17:08:39 +00002896 ArgStringList OutputArgs;
Daniel Dunbarb440f562010-08-02 02:38:21 +00002897 assert(Output.isFilename() && "Unexpected CC1 output.");
2898 OutputArgs.push_back("-o");
2899 OutputArgs.push_back(Output.getFilename());
Daniel Dunbare6adeee2009-03-29 17:08:39 +00002900
Joerg Sonnenbergerb86f5f42011-03-06 23:31:01 +00002901 if (Args.hasArg(options::OPT_E) || getToolChain().getDriver().CCCIsCPP) {
Daniel Dunbarf64f5302009-03-29 22:27:40 +00002902 AddCPPOptionsArgs(Args, CmdArgs, Inputs, OutputArgs);
2903 } else {
2904 AddCPPOptionsArgs(Args, CmdArgs, Inputs, ArgStringList());
2905 CmdArgs.append(OutputArgs.begin(), OutputArgs.end());
2906 }
Daniel Dunbard067f7f2009-04-08 23:54:23 +00002907
Daniel Dunbar6a8803a2009-04-03 01:27:06 +00002908 Args.AddAllArgs(CmdArgs, options::OPT_d_Group);
2909
Chad Rosierc31e48d2011-09-08 00:38:00 +00002910 RemoveCC1UnsupportedArgs(CmdArgs);
2911
Daniel Dunbare6adeee2009-03-29 17:08:39 +00002912 const char *CC1Name = getCC1Name(Inputs[0].getType());
Daniel Dunbard067f7f2009-04-08 23:54:23 +00002913 const char *Exec =
Daniel Dunbar9c3ed5f2010-07-14 18:46:23 +00002914 Args.MakeArgString(getToolChain().GetProgramPath(CC1Name));
Daniel Dunbar7fbaf532010-08-02 02:38:28 +00002915 C.addCommand(new Command(JA, *this, Exec, CmdArgs));
Daniel Dunbare6adeee2009-03-29 17:08:39 +00002916}
2917
2918void darwin::Compile::ConstructJob(Compilation &C, const JobAction &JA,
Daniel Dunbar7fbaf532010-08-02 02:38:28 +00002919 const InputInfo &Output,
Daniel Dunbard067f7f2009-04-08 23:54:23 +00002920 const InputInfoList &Inputs,
2921 const ArgList &Args,
Daniel Dunbare6adeee2009-03-29 17:08:39 +00002922 const char *LinkingOutput) const {
Daniel Dunbar083edf72009-12-21 18:54:17 +00002923 const Driver &D = getToolChain().getDriver();
Daniel Dunbare6adeee2009-03-29 17:08:39 +00002924 ArgStringList CmdArgs;
2925
2926 assert(Inputs.size() == 1 && "Unexpected number of inputs!");
2927
2928 types::ID InputType = Inputs[0].getType();
2929 const Arg *A;
Eli Friedmanbb0d9a52009-07-14 21:58:17 +00002930 if ((A = Args.getLastArg(options::OPT_traditional)))
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002931 D.Diag(diag::err_drv_argument_only_allowed_with)
Daniel Dunbare6adeee2009-03-29 17:08:39 +00002932 << A->getAsString(Args) << "-E";
2933
Daniel Dunbar24e52992010-06-07 23:28:45 +00002934 if (JA.getType() == types::TY_LLVM_IR ||
2935 JA.getType() == types::TY_LTO_IR)
Daniel Dunbare6adeee2009-03-29 17:08:39 +00002936 CmdArgs.push_back("-emit-llvm");
Daniel Dunbar24e52992010-06-07 23:28:45 +00002937 else if (JA.getType() == types::TY_LLVM_BC ||
2938 JA.getType() == types::TY_LTO_BC)
Daniel Dunbare6adeee2009-03-29 17:08:39 +00002939 CmdArgs.push_back("-emit-llvm-bc");
Daniel Dunbar6cdf83c2009-09-01 16:57:46 +00002940 else if (Output.getType() == types::TY_AST)
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002941 D.Diag(diag::err_drv_no_ast_support)
Daniel Dunbar1c28f1e2009-09-09 22:32:48 +00002942 << getToolChain().getTripleString();
Daniel Dunbarbcd554f2010-02-11 17:33:45 +00002943 else if (JA.getType() != types::TY_PP_Asm &&
2944 JA.getType() != types::TY_PCH)
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002945 D.Diag(diag::err_drv_invalid_gcc_output_type)
Daniel Dunbaraeea8ac2010-02-11 03:16:21 +00002946 << getTypeName(JA.getType());
Daniel Dunbare6adeee2009-03-29 17:08:39 +00002947
2948 ArgStringList OutputArgs;
2949 if (Output.getType() != types::TY_PCH) {
2950 OutputArgs.push_back("-o");
Daniel Dunbarb440f562010-08-02 02:38:21 +00002951 if (Output.isNothing())
Daniel Dunbare6adeee2009-03-29 17:08:39 +00002952 OutputArgs.push_back("/dev/null");
2953 else
2954 OutputArgs.push_back(Output.getFilename());
2955 }
2956
2957 // There is no need for this level of compatibility, but it makes
2958 // diffing easier.
2959 bool OutputArgsEarly = (Args.hasArg(options::OPT_fsyntax_only) ||
2960 Args.hasArg(options::OPT_S));
2961
2962 if (types::getPreprocessedType(InputType) != types::TY_INVALID) {
Daniel Dunbarafec1f52009-03-29 18:40:18 +00002963 AddCPPUniqueOptionsArgs(Args, CmdArgs, Inputs);
Daniel Dunbare6adeee2009-03-29 17:08:39 +00002964 if (OutputArgsEarly) {
2965 AddCC1OptionsArgs(Args, CmdArgs, Inputs, OutputArgs);
2966 } else {
2967 AddCC1OptionsArgs(Args, CmdArgs, Inputs, ArgStringList());
2968 CmdArgs.append(OutputArgs.begin(), OutputArgs.end());
2969 }
2970 } else {
2971 CmdArgs.push_back("-fpreprocessed");
Daniel Dunbard067f7f2009-04-08 23:54:23 +00002972
Daniel Dunbare6adeee2009-03-29 17:08:39 +00002973 for (InputInfoList::const_iterator
2974 it = Inputs.begin(), ie = Inputs.end(); it != ie; ++it) {
2975 const InputInfo &II = *it;
2976
Daniel Dunbar6cdf83c2009-09-01 16:57:46 +00002977 // Reject AST inputs.
2978 if (II.getType() == types::TY_AST) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002979 D.Diag(diag::err_drv_no_ast_support)
Daniel Dunbar1c28f1e2009-09-09 22:32:48 +00002980 << getToolChain().getTripleString();
Daniel Dunbar6cdf83c2009-09-01 16:57:46 +00002981 return;
2982 }
2983
Daniel Dunbarb440f562010-08-02 02:38:21 +00002984 CmdArgs.push_back(II.getFilename());
Daniel Dunbare6adeee2009-03-29 17:08:39 +00002985 }
2986
2987 if (OutputArgsEarly) {
2988 AddCC1OptionsArgs(Args, CmdArgs, Inputs, OutputArgs);
2989 } else {
2990 AddCC1OptionsArgs(Args, CmdArgs, Inputs, ArgStringList());
2991 CmdArgs.append(OutputArgs.begin(), OutputArgs.end());
2992 }
2993 }
Daniel Dunbard067f7f2009-04-08 23:54:23 +00002994
Daniel Dunbare6adeee2009-03-29 17:08:39 +00002995 if (Output.getType() == types::TY_PCH) {
2996 assert(Output.isFilename() && "Invalid PCH output.");
2997
2998 CmdArgs.push_back("-o");
2999 // NOTE: gcc uses a temp .s file for this, but there doesn't seem
3000 // to be a good reason.
Chad Rosier96d690c2011-08-01 19:58:48 +00003001 const char *TmpPath = C.getArgs().MakeArgString(
Chad Rosier39ab7432011-08-26 21:28:44 +00003002 D.GetTemporaryPath("cc", "s"));
Chad Rosier96d690c2011-08-01 19:58:48 +00003003 C.addTempFile(TmpPath);
3004 CmdArgs.push_back(TmpPath);
Daniel Dunbard067f7f2009-04-08 23:54:23 +00003005
Eric Christopher84fbdb42011-08-19 00:30:14 +00003006 // If we're emitting a pch file with the last 4 characters of ".pth"
3007 // and falling back to llvm-gcc we want to use ".gch" instead.
3008 std::string OutputFile(Output.getFilename());
3009 size_t loc = OutputFile.rfind(".pth");
3010 if (loc != std::string::npos)
3011 OutputFile.replace(loc, 4, ".gch");
3012 const char *Tmp = C.getArgs().MakeArgString("--output-pch="+OutputFile);
3013 CmdArgs.push_back(Tmp);
Daniel Dunbard067f7f2009-04-08 23:54:23 +00003014 }
Daniel Dunbare6adeee2009-03-29 17:08:39 +00003015
Chad Rosierbc5ea3d2011-08-17 18:24:55 +00003016 RemoveCC1UnsupportedArgs(CmdArgs);
3017
Daniel Dunbare6adeee2009-03-29 17:08:39 +00003018 const char *CC1Name = getCC1Name(Inputs[0].getType());
Daniel Dunbard067f7f2009-04-08 23:54:23 +00003019 const char *Exec =
Daniel Dunbar9c3ed5f2010-07-14 18:46:23 +00003020 Args.MakeArgString(getToolChain().GetProgramPath(CC1Name));
Daniel Dunbar7fbaf532010-08-02 02:38:28 +00003021 C.addCommand(new Command(JA, *this, Exec, CmdArgs));
Daniel Dunbare6adeee2009-03-29 17:08:39 +00003022}
3023
Daniel Dunbarbe220842009-03-20 16:06:39 +00003024void darwin::Assemble::ConstructJob(Compilation &C, const JobAction &JA,
Daniel Dunbar7fbaf532010-08-02 02:38:28 +00003025 const InputInfo &Output,
Daniel Dunbard067f7f2009-04-08 23:54:23 +00003026 const InputInfoList &Inputs,
3027 const ArgList &Args,
Daniel Dunbarbe220842009-03-20 16:06:39 +00003028 const char *LinkingOutput) const {
3029 ArgStringList CmdArgs;
3030
3031 assert(Inputs.size() == 1 && "Unexpected number of inputs.");
3032 const InputInfo &Input = Inputs[0];
3033
Daniel Dunbardc8355e2011-04-12 23:59:20 +00003034 // Determine the original source input.
3035 const Action *SourceAction = &JA;
3036 while (SourceAction->getKind() != Action::InputClass) {
3037 assert(!SourceAction->getInputs().empty() && "unexpected root action!");
3038 SourceAction = SourceAction->getInputs()[0];
3039 }
3040
3041 // Forward -g, assuming we are dealing with an actual assembly file.
Eric Christopher84fbdb42011-08-19 00:30:14 +00003042 if (SourceAction->getType() == types::TY_Asm ||
Daniel Dunbardc8355e2011-04-12 23:59:20 +00003043 SourceAction->getType() == types::TY_PP_Asm) {
Daniel Dunbar5c9c1182009-04-01 00:27:44 +00003044 if (Args.hasArg(options::OPT_gstabs))
3045 CmdArgs.push_back("--gstabs");
3046 else if (Args.hasArg(options::OPT_g_Group))
3047 CmdArgs.push_back("--gdwarf2");
3048 }
Daniel Dunbard067f7f2009-04-08 23:54:23 +00003049
Daniel Dunbarbe220842009-03-20 16:06:39 +00003050 // Derived from asm spec.
Daniel Dunbar3571dd92009-09-09 18:36:27 +00003051 AddDarwinArch(Args, CmdArgs);
Daniel Dunbarbe220842009-03-20 16:06:39 +00003052
Daniel Dunbar6d484762010-07-22 01:47:22 +00003053 // Use -force_cpusubtype_ALL on x86 by default.
3054 if (getToolChain().getTriple().getArch() == llvm::Triple::x86 ||
3055 getToolChain().getTriple().getArch() == llvm::Triple::x86_64 ||
Daniel Dunbar3571dd92009-09-09 18:36:27 +00003056 Args.hasArg(options::OPT_force__cpusubtype__ALL))
3057 CmdArgs.push_back("-force_cpusubtype_ALL");
3058
Daniel Dunbara6b4a3d2009-08-24 22:26:16 +00003059 if (getToolChain().getTriple().getArch() != llvm::Triple::x86_64 &&
3060 (Args.hasArg(options::OPT_mkernel) ||
Daniel Dunbarbe220842009-03-20 16:06:39 +00003061 Args.hasArg(options::OPT_static) ||
Daniel Dunbara6b4a3d2009-08-24 22:26:16 +00003062 Args.hasArg(options::OPT_fapple_kext)))
Daniel Dunbard067f7f2009-04-08 23:54:23 +00003063 CmdArgs.push_back("-static");
3064
Daniel Dunbarbe220842009-03-20 16:06:39 +00003065 Args.AddAllArgValues(CmdArgs, options::OPT_Wa_COMMA,
3066 options::OPT_Xassembler);
3067
3068 assert(Output.isFilename() && "Unexpected lipo output.");
3069 CmdArgs.push_back("-o");
3070 CmdArgs.push_back(Output.getFilename());
3071
Daniel Dunbarb440f562010-08-02 02:38:21 +00003072 assert(Input.isFilename() && "Invalid input.");
3073 CmdArgs.push_back(Input.getFilename());
Daniel Dunbarbe220842009-03-20 16:06:39 +00003074
3075 // asm_final spec is empty.
3076
Daniel Dunbard067f7f2009-04-08 23:54:23 +00003077 const char *Exec =
Daniel Dunbar9c3ed5f2010-07-14 18:46:23 +00003078 Args.MakeArgString(getToolChain().GetProgramPath("as"));
Daniel Dunbar7fbaf532010-08-02 02:38:28 +00003079 C.addCommand(new Command(JA, *this, Exec, CmdArgs));
Daniel Dunbarbe220842009-03-20 16:06:39 +00003080}
Daniel Dunbar64ed5e32009-03-20 00:52:38 +00003081
Daniel Dunbare9ded432009-09-09 18:36:20 +00003082void darwin::DarwinTool::AddDarwinArch(const ArgList &Args,
3083 ArgStringList &CmdArgs) const {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003084 StringRef ArchName = getDarwinToolChain().getDarwinArchName(Args);
Daniel Dunbardcc3b652010-01-22 02:04:58 +00003085
Daniel Dunbarc1964212009-03-26 16:23:12 +00003086 // Derived from darwin_arch spec.
3087 CmdArgs.push_back("-arch");
Daniel Dunbardcc3b652010-01-22 02:04:58 +00003088 CmdArgs.push_back(Args.MakeArgString(ArchName));
Daniel Dunbar91dbfd62009-09-04 18:35:31 +00003089
Daniel Dunbardcc3b652010-01-22 02:04:58 +00003090 // FIXME: Is this needed anymore?
3091 if (ArchName == "arm")
Daniel Dunbar91dbfd62009-09-04 18:35:31 +00003092 CmdArgs.push_back("-force_cpusubtype_ALL");
Daniel Dunbarc1964212009-03-26 16:23:12 +00003093}
3094
Daniel Dunbarccbc4522010-09-09 21:51:05 +00003095void darwin::Link::AddLinkArgs(Compilation &C,
3096 const ArgList &Args,
Daniel Dunbarc1964212009-03-26 16:23:12 +00003097 ArgStringList &CmdArgs) const {
Daniel Dunbar083edf72009-12-21 18:54:17 +00003098 const Driver &D = getToolChain().getDriver();
Daniel Dunbarc44d3132011-04-28 21:23:41 +00003099 const toolchains::Darwin &DarwinTC = getDarwinToolChain();
Daniel Dunbarc1964212009-03-26 16:23:12 +00003100
Daniel Dunbarcacb0e22010-08-11 23:07:50 +00003101 unsigned Version[3] = { 0, 0, 0 };
3102 if (Arg *A = Args.getLastArg(options::OPT_mlinker_version_EQ)) {
3103 bool HadExtra;
3104 if (!Driver::GetReleaseVersion(A->getValue(Args), Version[0],
3105 Version[1], Version[2], HadExtra) ||
3106 HadExtra)
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003107 D.Diag(diag::err_drv_invalid_version_number)
Daniel Dunbarcacb0e22010-08-11 23:07:50 +00003108 << A->getAsString(Args);
3109 }
3110
3111 // Newer linkers support -demangle, pass it if supported and not disabled by
3112 // the user.
Daniel Dunbar6d776eb2010-11-19 17:51:40 +00003113 //
3114 // FIXME: We temporarily avoid passing -demangle to any iOS linker, because
3115 // unfortunately we can't be guaranteed that the linker version used there
3116 // will match the linker version detected at configure time. We need the
3117 // universal driver.
3118 if (Version[0] >= 100 && !Args.hasArg(options::OPT_Z_Xlinker__no_demangle) &&
Daniel Dunbarc44d3132011-04-28 21:23:41 +00003119 !DarwinTC.isTargetIPhoneOS()) {
Daniel Dunbar1eaf5c62010-09-07 17:07:49 +00003120 // Don't pass -demangle to ld_classic.
3121 //
3122 // FIXME: This is a temporary workaround, ld should be handling this.
3123 bool UsesLdClassic = (getToolChain().getArch() == llvm::Triple::x86 &&
3124 Args.hasArg(options::OPT_static));
Daniel Dunbar3d7e0e22010-09-07 17:50:41 +00003125 if (getToolChain().getArch() == llvm::Triple::x86) {
3126 for (arg_iterator it = Args.filtered_begin(options::OPT_Xlinker,
3127 options::OPT_Wl_COMMA),
3128 ie = Args.filtered_end(); it != ie; ++it) {
3129 const Arg *A = *it;
3130 for (unsigned i = 0, e = A->getNumValues(); i != e; ++i)
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003131 if (StringRef(A->getValue(Args, i)) == "-kext")
Daniel Dunbar3d7e0e22010-09-07 17:50:41 +00003132 UsesLdClassic = true;
3133 }
3134 }
Daniel Dunbar1eaf5c62010-09-07 17:07:49 +00003135 if (!UsesLdClassic)
3136 CmdArgs.push_back("-demangle");
Daniel Dunbarcacb0e22010-08-11 23:07:50 +00003137 }
3138
Daniel Dunbaref889c72011-06-21 20:55:11 +00003139 // If we are using LTO, then automatically create a temporary file path for
3140 // the linker to use, so that it's lifetime will extend past a possible
3141 // dsymutil step.
Daniel Dunbar3d125d32011-06-21 21:18:32 +00003142 if (Version[0] >= 116 && D.IsUsingLTO(Args)) {
Daniel Dunbaref889c72011-06-21 20:55:11 +00003143 const char *TmpPath = C.getArgs().MakeArgString(
Chad Rosier39ab7432011-08-26 21:28:44 +00003144 D.GetTemporaryPath("cc", types::getTypeTempSuffix(types::TY_Object)));
Daniel Dunbaref889c72011-06-21 20:55:11 +00003145 C.addTempFile(TmpPath);
3146 CmdArgs.push_back("-object_path_lto");
3147 CmdArgs.push_back(TmpPath);
3148 }
3149
Daniel Dunbarc1964212009-03-26 16:23:12 +00003150 // Derived from the "link" spec.
3151 Args.AddAllArgs(CmdArgs, options::OPT_static);
3152 if (!Args.hasArg(options::OPT_static))
3153 CmdArgs.push_back("-dynamic");
3154 if (Args.hasArg(options::OPT_fgnu_runtime)) {
3155 // FIXME: gcc replaces -lobjc in forward args with -lobjc-gnu
3156 // here. How do we wish to handle such things?
3157 }
Daniel Dunbard067f7f2009-04-08 23:54:23 +00003158
Daniel Dunbarc1964212009-03-26 16:23:12 +00003159 if (!Args.hasArg(options::OPT_dynamiclib)) {
Daniel Dunbara48823f2010-01-22 02:04:52 +00003160 AddDarwinArch(Args, CmdArgs);
Daniel Dunbara48823f2010-01-22 02:04:52 +00003161 // FIXME: Why do this only on this path?
Daniel Dunbar93d7acf2010-01-22 03:37:33 +00003162 Args.AddLastArg(CmdArgs, options::OPT_force__cpusubtype__ALL);
Daniel Dunbarc1964212009-03-26 16:23:12 +00003163
3164 Args.AddLastArg(CmdArgs, options::OPT_bundle);
3165 Args.AddAllArgs(CmdArgs, options::OPT_bundle__loader);
3166 Args.AddAllArgs(CmdArgs, options::OPT_client__name);
3167
3168 Arg *A;
3169 if ((A = Args.getLastArg(options::OPT_compatibility__version)) ||
3170 (A = Args.getLastArg(options::OPT_current__version)) ||
3171 (A = Args.getLastArg(options::OPT_install__name)))
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003172 D.Diag(diag::err_drv_argument_only_allowed_with)
Daniel Dunbarc1964212009-03-26 16:23:12 +00003173 << A->getAsString(Args) << "-dynamiclib";
3174
3175 Args.AddLastArg(CmdArgs, options::OPT_force__flat__namespace);
3176 Args.AddLastArg(CmdArgs, options::OPT_keep__private__externs);
3177 Args.AddLastArg(CmdArgs, options::OPT_private__bundle);
3178 } else {
3179 CmdArgs.push_back("-dylib");
3180
3181 Arg *A;
3182 if ((A = Args.getLastArg(options::OPT_bundle)) ||
3183 (A = Args.getLastArg(options::OPT_bundle__loader)) ||
3184 (A = Args.getLastArg(options::OPT_client__name)) ||
3185 (A = Args.getLastArg(options::OPT_force__flat__namespace)) ||
3186 (A = Args.getLastArg(options::OPT_keep__private__externs)) ||
3187 (A = Args.getLastArg(options::OPT_private__bundle)))
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003188 D.Diag(diag::err_drv_argument_not_allowed_with)
Daniel Dunbarc1964212009-03-26 16:23:12 +00003189 << A->getAsString(Args) << "-dynamiclib";
Daniel Dunbard067f7f2009-04-08 23:54:23 +00003190
Daniel Dunbarc1964212009-03-26 16:23:12 +00003191 Args.AddAllArgsTranslated(CmdArgs, options::OPT_compatibility__version,
3192 "-dylib_compatibility_version");
3193 Args.AddAllArgsTranslated(CmdArgs, options::OPT_current__version,
3194 "-dylib_current_version");
3195
Daniel Dunbara48823f2010-01-22 02:04:52 +00003196 AddDarwinArch(Args, CmdArgs);
Daniel Dunbarc1964212009-03-26 16:23:12 +00003197
3198 Args.AddAllArgsTranslated(CmdArgs, options::OPT_install__name,
3199 "-dylib_install_name");
3200 }
3201
3202 Args.AddLastArg(CmdArgs, options::OPT_all__load);
3203 Args.AddAllArgs(CmdArgs, options::OPT_allowable__client);
3204 Args.AddLastArg(CmdArgs, options::OPT_bind__at__load);
Daniel Dunbarc44d3132011-04-28 21:23:41 +00003205 if (DarwinTC.isTargetIPhoneOS())
Daniel Dunbar2b5f6812009-09-04 18:35:41 +00003206 Args.AddLastArg(CmdArgs, options::OPT_arch__errors__fatal);
Daniel Dunbarc1964212009-03-26 16:23:12 +00003207 Args.AddLastArg(CmdArgs, options::OPT_dead__strip);
3208 Args.AddLastArg(CmdArgs, options::OPT_no__dead__strip__inits__and__terms);
3209 Args.AddAllArgs(CmdArgs, options::OPT_dylib__file);
3210 Args.AddLastArg(CmdArgs, options::OPT_dynamic);
3211 Args.AddAllArgs(CmdArgs, options::OPT_exported__symbols__list);
3212 Args.AddLastArg(CmdArgs, options::OPT_flat__namespace);
Daniel Dunbar044a3902011-06-28 20:16:02 +00003213 Args.AddAllArgs(CmdArgs, options::OPT_force__load);
Daniel Dunbarc1964212009-03-26 16:23:12 +00003214 Args.AddAllArgs(CmdArgs, options::OPT_headerpad__max__install__names);
3215 Args.AddAllArgs(CmdArgs, options::OPT_image__base);
3216 Args.AddAllArgs(CmdArgs, options::OPT_init);
3217
Daniel Dunbarc44d3132011-04-28 21:23:41 +00003218 // Add the deployment target.
3219 unsigned TargetVersion[3];
3220 DarwinTC.getTargetVersion(TargetVersion);
Daniel Dunbar72ceb922011-04-30 04:22:58 +00003221
3222 // If we had an explicit -mios-simulator-version-min argument, honor that,
3223 // otherwise use the traditional deployment targets. We can't just check the
3224 // is-sim attribute because existing code follows this path, and the linker
3225 // may not handle the argument.
3226 //
3227 // FIXME: We may be able to remove this, once we can verify no one depends on
3228 // it.
3229 if (Args.hasArg(options::OPT_mios_simulator_version_min_EQ))
3230 CmdArgs.push_back("-ios_simulator_version_min");
3231 else if (DarwinTC.isTargetIPhoneOS())
3232 CmdArgs.push_back("-iphoneos_version_min");
3233 else
3234 CmdArgs.push_back("-macosx_version_min");
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003235 CmdArgs.push_back(Args.MakeArgString(Twine(TargetVersion[0]) + "." +
3236 Twine(TargetVersion[1]) + "." +
3237 Twine(TargetVersion[2])));
Daniel Dunbarc44d3132011-04-28 21:23:41 +00003238
Daniel Dunbarc1964212009-03-26 16:23:12 +00003239 Args.AddLastArg(CmdArgs, options::OPT_nomultidefs);
3240 Args.AddLastArg(CmdArgs, options::OPT_multi__module);
3241 Args.AddLastArg(CmdArgs, options::OPT_single__module);
3242 Args.AddAllArgs(CmdArgs, options::OPT_multiply__defined);
3243 Args.AddAllArgs(CmdArgs, options::OPT_multiply__defined__unused);
Daniel Dunbard067f7f2009-04-08 23:54:23 +00003244
Daniel Dunbaraf68a882010-07-13 23:31:40 +00003245 if (const Arg *A = Args.getLastArg(options::OPT_fpie, options::OPT_fPIE,
3246 options::OPT_fno_pie,
3247 options::OPT_fno_PIE)) {
3248 if (A->getOption().matches(options::OPT_fpie) ||
3249 A->getOption().matches(options::OPT_fPIE))
3250 CmdArgs.push_back("-pie");
3251 else
3252 CmdArgs.push_back("-no_pie");
3253 }
Daniel Dunbarc1964212009-03-26 16:23:12 +00003254
3255 Args.AddLastArg(CmdArgs, options::OPT_prebind);
3256 Args.AddLastArg(CmdArgs, options::OPT_noprebind);
3257 Args.AddLastArg(CmdArgs, options::OPT_nofixprebinding);
3258 Args.AddLastArg(CmdArgs, options::OPT_prebind__all__twolevel__modules);
3259 Args.AddLastArg(CmdArgs, options::OPT_read__only__relocs);
3260 Args.AddAllArgs(CmdArgs, options::OPT_sectcreate);
3261 Args.AddAllArgs(CmdArgs, options::OPT_sectorder);
3262 Args.AddAllArgs(CmdArgs, options::OPT_seg1addr);
3263 Args.AddAllArgs(CmdArgs, options::OPT_segprot);
3264 Args.AddAllArgs(CmdArgs, options::OPT_segaddr);
3265 Args.AddAllArgs(CmdArgs, options::OPT_segs__read__only__addr);
3266 Args.AddAllArgs(CmdArgs, options::OPT_segs__read__write__addr);
3267 Args.AddAllArgs(CmdArgs, options::OPT_seg__addr__table);
3268 Args.AddAllArgs(CmdArgs, options::OPT_seg__addr__table__filename);
3269 Args.AddAllArgs(CmdArgs, options::OPT_sub__library);
3270 Args.AddAllArgs(CmdArgs, options::OPT_sub__umbrella);
Daniel Dunbar2b5f6812009-09-04 18:35:41 +00003271
Daniel Dunbar84384642011-05-02 21:03:47 +00003272 // Give --sysroot= preference, over the Apple specific behavior to also use
3273 // --isysroot as the syslibroot.
3274 if (const Arg *A = Args.getLastArg(options::OPT__sysroot_EQ)) {
3275 CmdArgs.push_back("-syslibroot");
3276 CmdArgs.push_back(A->getValue(Args));
3277 } else if (const Arg *A = Args.getLastArg(options::OPT_isysroot)) {
3278 CmdArgs.push_back("-syslibroot");
3279 CmdArgs.push_back(A->getValue(Args));
3280 } else if (getDarwinToolChain().isTargetIPhoneOS()) {
3281 CmdArgs.push_back("-syslibroot");
3282 CmdArgs.push_back("/Developer/SDKs/Extra");
Daniel Dunbar2b5f6812009-09-04 18:35:41 +00003283 }
3284
Daniel Dunbarc1964212009-03-26 16:23:12 +00003285 Args.AddLastArg(CmdArgs, options::OPT_twolevel__namespace);
3286 Args.AddLastArg(CmdArgs, options::OPT_twolevel__namespace__hints);
3287 Args.AddAllArgs(CmdArgs, options::OPT_umbrella);
3288 Args.AddAllArgs(CmdArgs, options::OPT_undefined);
3289 Args.AddAllArgs(CmdArgs, options::OPT_unexported__symbols__list);
Daniel Dunbar2b5f6812009-09-04 18:35:41 +00003290 Args.AddAllArgs(CmdArgs, options::OPT_weak__reference__mismatches);
Daniel Dunbarc1964212009-03-26 16:23:12 +00003291 Args.AddLastArg(CmdArgs, options::OPT_X_Flag);
3292 Args.AddAllArgs(CmdArgs, options::OPT_y);
3293 Args.AddLastArg(CmdArgs, options::OPT_w);
3294 Args.AddAllArgs(CmdArgs, options::OPT_pagezero__size);
3295 Args.AddAllArgs(CmdArgs, options::OPT_segs__read__);
3296 Args.AddLastArg(CmdArgs, options::OPT_seglinkedit);
3297 Args.AddLastArg(CmdArgs, options::OPT_noseglinkedit);
3298 Args.AddAllArgs(CmdArgs, options::OPT_sectalign);
3299 Args.AddAllArgs(CmdArgs, options::OPT_sectobjectsymbols);
3300 Args.AddAllArgs(CmdArgs, options::OPT_segcreate);
3301 Args.AddLastArg(CmdArgs, options::OPT_whyload);
3302 Args.AddLastArg(CmdArgs, options::OPT_whatsloaded);
3303 Args.AddAllArgs(CmdArgs, options::OPT_dylinker__install__name);
3304 Args.AddLastArg(CmdArgs, options::OPT_dylinker);
3305 Args.AddLastArg(CmdArgs, options::OPT_Mach);
3306}
3307
3308void darwin::Link::ConstructJob(Compilation &C, const JobAction &JA,
Daniel Dunbar7fbaf532010-08-02 02:38:28 +00003309 const InputInfo &Output,
Daniel Dunbard067f7f2009-04-08 23:54:23 +00003310 const InputInfoList &Inputs,
3311 const ArgList &Args,
Daniel Dunbarc1964212009-03-26 16:23:12 +00003312 const char *LinkingOutput) const {
3313 assert(Output.getType() == types::TY_Image && "Invalid linker output type.");
Daniel Dunbarc09988d2009-09-08 16:39:16 +00003314
Daniel Dunbarc1964212009-03-26 16:23:12 +00003315 // The logic here is derived from gcc's behavior; most of which
3316 // comes from specs (starting with link_command). Consult gcc for
3317 // more information.
Daniel Dunbarc1964212009-03-26 16:23:12 +00003318 ArgStringList CmdArgs;
3319
3320 // I'm not sure why this particular decomposition exists in gcc, but
3321 // we follow suite for ease of comparison.
Daniel Dunbarccbc4522010-09-09 21:51:05 +00003322 AddLinkArgs(C, Args, CmdArgs);
Daniel Dunbarc1964212009-03-26 16:23:12 +00003323
Daniel Dunbarc1964212009-03-26 16:23:12 +00003324 Args.AddAllArgs(CmdArgs, options::OPT_d_Flag);
3325 Args.AddAllArgs(CmdArgs, options::OPT_s);
3326 Args.AddAllArgs(CmdArgs, options::OPT_t);
3327 Args.AddAllArgs(CmdArgs, options::OPT_Z_Flag);
3328 Args.AddAllArgs(CmdArgs, options::OPT_u_Group);
3329 Args.AddAllArgs(CmdArgs, options::OPT_A);
3330 Args.AddLastArg(CmdArgs, options::OPT_e);
3331 Args.AddAllArgs(CmdArgs, options::OPT_m_Separate);
3332 Args.AddAllArgs(CmdArgs, options::OPT_r);
3333
Daniel Dunbar767bbab2010-10-18 22:08:36 +00003334 // Forward -ObjC when either -ObjC or -ObjC++ is used, to force loading
3335 // members of static archive libraries which implement Objective-C classes or
3336 // categories.
3337 if (Args.hasArg(options::OPT_ObjC) || Args.hasArg(options::OPT_ObjCXX))
3338 CmdArgs.push_back("-ObjC");
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00003339
Daniel Dunbarc1964212009-03-26 16:23:12 +00003340 CmdArgs.push_back("-o");
3341 CmdArgs.push_back(Output.getFilename());
3342
Daniel Dunbarc1964212009-03-26 16:23:12 +00003343 if (!Args.hasArg(options::OPT_A) &&
3344 !Args.hasArg(options::OPT_nostdlib) &&
3345 !Args.hasArg(options::OPT_nostartfiles)) {
3346 // Derived from startfile spec.
3347 if (Args.hasArg(options::OPT_dynamiclib)) {
Daniel Dunbard067f7f2009-04-08 23:54:23 +00003348 // Derived from darwin_dylib1 spec.
Daniel Dunbar16d97092011-04-01 21:02:42 +00003349 if (getDarwinToolChain().isTargetIOSSimulator()) {
3350 // The simulator doesn't have a versioned crt1 file.
3351 CmdArgs.push_back("-ldylib1.o");
3352 } else if (getDarwinToolChain().isTargetIPhoneOS()) {
Daniel Dunbar83608032010-01-27 00:56:56 +00003353 if (getDarwinToolChain().isIPhoneOSVersionLT(3, 1))
3354 CmdArgs.push_back("-ldylib1.o");
3355 } else {
Daniel Dunbar6d23b2f2010-01-27 00:57:03 +00003356 if (getDarwinToolChain().isMacosxVersionLT(10, 5))
Daniel Dunbar83608032010-01-27 00:56:56 +00003357 CmdArgs.push_back("-ldylib1.o");
Daniel Dunbar6d23b2f2010-01-27 00:57:03 +00003358 else if (getDarwinToolChain().isMacosxVersionLT(10, 6))
Daniel Dunbar83608032010-01-27 00:56:56 +00003359 CmdArgs.push_back("-ldylib1.10.5.o");
3360 }
Daniel Dunbarc1964212009-03-26 16:23:12 +00003361 } else {
3362 if (Args.hasArg(options::OPT_bundle)) {
Daniel Dunbarae8bca02009-04-01 03:17:40 +00003363 if (!Args.hasArg(options::OPT_static)) {
3364 // Derived from darwin_bundle1 spec.
Daniel Dunbar16d97092011-04-01 21:02:42 +00003365 if (getDarwinToolChain().isTargetIOSSimulator()) {
3366 // The simulator doesn't have a versioned crt1 file.
3367 CmdArgs.push_back("-lbundle1.o");
3368 } else if (getDarwinToolChain().isTargetIPhoneOS()) {
Daniel Dunbar83608032010-01-27 00:56:56 +00003369 if (getDarwinToolChain().isIPhoneOSVersionLT(3, 1))
3370 CmdArgs.push_back("-lbundle1.o");
3371 } else {
Daniel Dunbar6d23b2f2010-01-27 00:57:03 +00003372 if (getDarwinToolChain().isMacosxVersionLT(10, 6))
Daniel Dunbar83608032010-01-27 00:56:56 +00003373 CmdArgs.push_back("-lbundle1.o");
3374 }
Daniel Dunbarae8bca02009-04-01 03:17:40 +00003375 }
Daniel Dunbarc1964212009-03-26 16:23:12 +00003376 } else {
Daniel Dunbar733b0f82011-03-01 18:49:30 +00003377 if (Args.hasArg(options::OPT_pg) &&
3378 getToolChain().SupportsProfiling()) {
Daniel Dunbarc1964212009-03-26 16:23:12 +00003379 if (Args.hasArg(options::OPT_static) ||
3380 Args.hasArg(options::OPT_object) ||
3381 Args.hasArg(options::OPT_preload)) {
3382 CmdArgs.push_back("-lgcrt0.o");
3383 } else {
3384 CmdArgs.push_back("-lgcrt1.o");
Daniel Dunbard067f7f2009-04-08 23:54:23 +00003385
Daniel Dunbarc1964212009-03-26 16:23:12 +00003386 // darwin_crt2 spec is empty.
Daniel Dunbard067f7f2009-04-08 23:54:23 +00003387 }
Daniel Dunbarc1964212009-03-26 16:23:12 +00003388 } else {
3389 if (Args.hasArg(options::OPT_static) ||
3390 Args.hasArg(options::OPT_object) ||
3391 Args.hasArg(options::OPT_preload)) {
3392 CmdArgs.push_back("-lcrt0.o");
3393 } else {
3394 // Derived from darwin_crt1 spec.
Daniel Dunbarebc34df2011-03-31 17:12:33 +00003395 if (getDarwinToolChain().isTargetIOSSimulator()) {
3396 // The simulator doesn't have a versioned crt1 file.
3397 CmdArgs.push_back("-lcrt1.o");
3398 } else if (getDarwinToolChain().isTargetIPhoneOS()) {
Daniel Dunbar83608032010-01-27 00:56:56 +00003399 if (getDarwinToolChain().isIPhoneOSVersionLT(3, 1))
3400 CmdArgs.push_back("-lcrt1.o");
3401 else
3402 CmdArgs.push_back("-lcrt1.3.1.o");
Daniel Dunbar6d23b2f2010-01-27 00:57:03 +00003403 } else {
3404 if (getDarwinToolChain().isMacosxVersionLT(10, 5))
3405 CmdArgs.push_back("-lcrt1.o");
3406 else if (getDarwinToolChain().isMacosxVersionLT(10, 6))
3407 CmdArgs.push_back("-lcrt1.10.5.o");
3408 else
3409 CmdArgs.push_back("-lcrt1.10.6.o");
Daniel Dunbard067f7f2009-04-08 23:54:23 +00003410
Daniel Dunbar6d23b2f2010-01-27 00:57:03 +00003411 // darwin_crt2 spec is empty.
3412 }
Daniel Dunbarc1964212009-03-26 16:23:12 +00003413 }
3414 }
3415 }
3416 }
3417
Daniel Dunbar6d23b2f2010-01-27 00:57:03 +00003418 if (!getDarwinToolChain().isTargetIPhoneOS() &&
3419 Args.hasArg(options::OPT_shared_libgcc) &&
3420 getDarwinToolChain().isMacosxVersionLT(10, 5)) {
Daniel Dunbar1c28f1e2009-09-09 22:32:48 +00003421 const char *Str =
Daniel Dunbar9c3ed5f2010-07-14 18:46:23 +00003422 Args.MakeArgString(getToolChain().GetFilePath("crt3.o"));
Daniel Dunbar1c28f1e2009-09-09 22:32:48 +00003423 CmdArgs.push_back(Str);
Daniel Dunbarc1964212009-03-26 16:23:12 +00003424 }
3425 }
3426
3427 Args.AddAllArgs(CmdArgs, options::OPT_L);
Daniel Dunbard067f7f2009-04-08 23:54:23 +00003428
Daniel Dunbarc1964212009-03-26 16:23:12 +00003429 if (Args.hasArg(options::OPT_fopenmp))
3430 // This is more complicated in gcc...
3431 CmdArgs.push_back("-lgomp");
3432
Daniel Dunbar4c30b892009-09-18 08:14:36 +00003433 getDarwinToolChain().AddLinkSearchPathArgs(Args, CmdArgs);
Daniel Dunbard067f7f2009-04-08 23:54:23 +00003434
John McCall31168b02011-06-15 23:02:42 +00003435 // In ARC, if we don't have runtime support, link in the runtime
3436 // stubs. We have to do this *before* adding any of the normal
3437 // linker inputs so that its initializer gets run first.
John McCall24fc0de2011-07-06 00:26:06 +00003438 if (isObjCAutoRefCount(Args)) {
3439 ObjCRuntime runtime;
3440 getDarwinToolChain().configureObjCRuntime(runtime);
3441 if (!runtime.HasARC)
3442 getDarwinToolChain().AddLinkARCArgs(Args, CmdArgs);
3443 }
John McCall31168b02011-06-15 23:02:42 +00003444
Daniel Dunbar54423b22010-09-17 00:24:54 +00003445 AddLinkerInputs(getToolChain(), Inputs, Args, CmdArgs);
Daniel Dunbarc1964212009-03-26 16:23:12 +00003446
3447 if (LinkingOutput) {
3448 CmdArgs.push_back("-arch_multiple");
3449 CmdArgs.push_back("-final_output");
3450 CmdArgs.push_back(LinkingOutput);
3451 }
3452
Daniel Dunbarc1964212009-03-26 16:23:12 +00003453 if (Args.hasArg(options::OPT_fnested_functions))
3454 CmdArgs.push_back("-allow_stack_execute");
Daniel Dunbard067f7f2009-04-08 23:54:23 +00003455
Daniel Dunbarc1964212009-03-26 16:23:12 +00003456 if (!Args.hasArg(options::OPT_nostdlib) &&
3457 !Args.hasArg(options::OPT_nodefaultlibs)) {
Daniel Dunbar083edf72009-12-21 18:54:17 +00003458 if (getToolChain().getDriver().CCCIsCXX)
Daniel Dunbar3f7796f2010-09-17 01:20:05 +00003459 getToolChain().AddCXXStdlibLibArgs(Args, CmdArgs);
Daniel Dunbarad0f62b2009-04-08 06:06:21 +00003460
Daniel Dunbarc1964212009-03-26 16:23:12 +00003461 // link_ssp spec is empty.
3462
Daniel Dunbar26d482a2009-09-18 08:15:03 +00003463 // Let the tool chain choose which runtime library to link.
3464 getDarwinToolChain().AddLinkRuntimeLibArgs(Args, CmdArgs);
Daniel Dunbarc1964212009-03-26 16:23:12 +00003465 }
3466
3467 if (!Args.hasArg(options::OPT_A) &&
3468 !Args.hasArg(options::OPT_nostdlib) &&
3469 !Args.hasArg(options::OPT_nostartfiles)) {
3470 // endfile_spec is empty.
3471 }
3472
Bill Wendling08760582011-06-27 19:15:03 +00003473 addProfileRT(getToolChain(), Args, CmdArgs, getToolChain().getTriple());
Nick Lewycky82fe5f42011-05-24 21:54:59 +00003474
Daniel Dunbarc1964212009-03-26 16:23:12 +00003475 Args.AddAllArgs(CmdArgs, options::OPT_T_Group);
3476 Args.AddAllArgs(CmdArgs, options::OPT_F);
3477
Daniel Dunbard067f7f2009-04-08 23:54:23 +00003478 const char *Exec =
Daniel Dunbar9c3ed5f2010-07-14 18:46:23 +00003479 Args.MakeArgString(getToolChain().GetProgramPath("ld"));
Daniel Dunbar7fbaf532010-08-02 02:38:28 +00003480 C.addCommand(new Command(JA, *this, Exec, CmdArgs));
Daniel Dunbarc1964212009-03-26 16:23:12 +00003481}
3482
Daniel Dunbar64ed5e32009-03-20 00:52:38 +00003483void darwin::Lipo::ConstructJob(Compilation &C, const JobAction &JA,
Daniel Dunbar7fbaf532010-08-02 02:38:28 +00003484 const InputInfo &Output,
Daniel Dunbard067f7f2009-04-08 23:54:23 +00003485 const InputInfoList &Inputs,
3486 const ArgList &Args,
Daniel Dunbar64ed5e32009-03-20 00:52:38 +00003487 const char *LinkingOutput) const {
3488 ArgStringList CmdArgs;
3489
3490 CmdArgs.push_back("-create");
3491 assert(Output.isFilename() && "Unexpected lipo output.");
Daniel Dunbar06686ab2009-03-24 00:24:37 +00003492
3493 CmdArgs.push_back("-output");
Daniel Dunbar64ed5e32009-03-20 00:52:38 +00003494 CmdArgs.push_back(Output.getFilename());
Daniel Dunbar06686ab2009-03-24 00:24:37 +00003495
Daniel Dunbar64ed5e32009-03-20 00:52:38 +00003496 for (InputInfoList::const_iterator
3497 it = Inputs.begin(), ie = Inputs.end(); it != ie; ++it) {
3498 const InputInfo &II = *it;
3499 assert(II.isFilename() && "Unexpected lipo input.");
3500 CmdArgs.push_back(II.getFilename());
3501 }
Daniel Dunbard067f7f2009-04-08 23:54:23 +00003502 const char *Exec =
Daniel Dunbar9c3ed5f2010-07-14 18:46:23 +00003503 Args.MakeArgString(getToolChain().GetProgramPath("lipo"));
Daniel Dunbar7fbaf532010-08-02 02:38:28 +00003504 C.addCommand(new Command(JA, *this, Exec, CmdArgs));
Daniel Dunbar64ed5e32009-03-20 00:52:38 +00003505}
Daniel Dunbar8eb473c2009-03-31 17:45:15 +00003506
Daniel Dunbar88299622010-06-04 18:28:36 +00003507void darwin::Dsymutil::ConstructJob(Compilation &C, const JobAction &JA,
Daniel Dunbar7fbaf532010-08-02 02:38:28 +00003508 const InputInfo &Output,
Daniel Dunbar88299622010-06-04 18:28:36 +00003509 const InputInfoList &Inputs,
3510 const ArgList &Args,
3511 const char *LinkingOutput) const {
3512 ArgStringList CmdArgs;
3513
Daniel Dunbareb86b042011-05-09 17:23:16 +00003514 CmdArgs.push_back("-o");
3515 CmdArgs.push_back(Output.getFilename());
3516
Daniel Dunbar88299622010-06-04 18:28:36 +00003517 assert(Inputs.size() == 1 && "Unable to handle multiple inputs.");
3518 const InputInfo &Input = Inputs[0];
3519 assert(Input.isFilename() && "Unexpected dsymutil input.");
3520 CmdArgs.push_back(Input.getFilename());
3521
Daniel Dunbar88299622010-06-04 18:28:36 +00003522 const char *Exec =
Daniel Dunbar9c3ed5f2010-07-14 18:46:23 +00003523 Args.MakeArgString(getToolChain().GetProgramPath("dsymutil"));
Daniel Dunbar7fbaf532010-08-02 02:38:28 +00003524 C.addCommand(new Command(JA, *this, Exec, CmdArgs));
Daniel Dunbar88299622010-06-04 18:28:36 +00003525}
3526
Eric Christopher551ef452011-08-23 17:56:55 +00003527void darwin::VerifyDebug::ConstructJob(Compilation &C, const JobAction &JA,
3528 const InputInfo &Output,
3529 const InputInfoList &Inputs,
3530 const ArgList &Args,
3531 const char *LinkingOutput) const {
3532 ArgStringList CmdArgs;
3533 CmdArgs.push_back("--verify");
3534
3535 assert(Inputs.size() == 1 && "Unable to handle multiple inputs.");
3536 const InputInfo &Input = Inputs[0];
3537 assert(Input.isFilename() && "Unexpected verify input");
3538
3539 // Grabbing the output of the earlier dsymutil run.
3540 CmdArgs.push_back(Input.getFilename());
3541
3542 const char *Exec =
3543 Args.MakeArgString(getToolChain().GetProgramPath("dwarfdump"));
3544 C.addCommand(new Command(JA, *this, Exec, CmdArgs));
3545}
3546
Edward O'Callaghan856e4ff2009-08-22 01:06:46 +00003547void auroraux::Assemble::ConstructJob(Compilation &C, const JobAction &JA,
Daniel Dunbar7fbaf532010-08-02 02:38:28 +00003548 const InputInfo &Output,
Daniel Dunbar1c7577c2009-11-04 06:24:38 +00003549 const InputInfoList &Inputs,
3550 const ArgList &Args,
3551 const char *LinkingOutput) const {
Edward O'Callaghan856e4ff2009-08-22 01:06:46 +00003552 ArgStringList CmdArgs;
3553
3554 Args.AddAllArgValues(CmdArgs, options::OPT_Wa_COMMA,
3555 options::OPT_Xassembler);
3556
3557 CmdArgs.push_back("-o");
Daniel Dunbarb440f562010-08-02 02:38:21 +00003558 CmdArgs.push_back(Output.getFilename());
Edward O'Callaghan856e4ff2009-08-22 01:06:46 +00003559
3560 for (InputInfoList::const_iterator
3561 it = Inputs.begin(), ie = Inputs.end(); it != ie; ++it) {
3562 const InputInfo &II = *it;
Daniel Dunbarb440f562010-08-02 02:38:21 +00003563 CmdArgs.push_back(II.getFilename());
Edward O'Callaghan856e4ff2009-08-22 01:06:46 +00003564 }
3565
3566 const char *Exec =
Daniel Dunbar9c3ed5f2010-07-14 18:46:23 +00003567 Args.MakeArgString(getToolChain().GetProgramPath("gas"));
Daniel Dunbar7fbaf532010-08-02 02:38:28 +00003568 C.addCommand(new Command(JA, *this, Exec, CmdArgs));
Edward O'Callaghan856e4ff2009-08-22 01:06:46 +00003569}
3570
3571void auroraux::Link::ConstructJob(Compilation &C, const JobAction &JA,
Daniel Dunbar7fbaf532010-08-02 02:38:28 +00003572 const InputInfo &Output,
Daniel Dunbar1c7577c2009-11-04 06:24:38 +00003573 const InputInfoList &Inputs,
3574 const ArgList &Args,
3575 const char *LinkingOutput) const {
Edward O'Callaghan856e4ff2009-08-22 01:06:46 +00003576 ArgStringList CmdArgs;
3577
3578 if ((!Args.hasArg(options::OPT_nostdlib)) &&
Daniel Dunbar1c7577c2009-11-04 06:24:38 +00003579 (!Args.hasArg(options::OPT_shared))) {
Edward O'Callaghan856e4ff2009-08-22 01:06:46 +00003580 CmdArgs.push_back("-e");
Edward O'Callaghand8712d92009-10-15 07:44:07 +00003581 CmdArgs.push_back("_start");
Edward O'Callaghan856e4ff2009-08-22 01:06:46 +00003582 }
3583
3584 if (Args.hasArg(options::OPT_static)) {
3585 CmdArgs.push_back("-Bstatic");
Edward O'Callaghand8712d92009-10-15 07:44:07 +00003586 CmdArgs.push_back("-dn");
Edward O'Callaghan856e4ff2009-08-22 01:06:46 +00003587 } else {
Edward O'Callaghand8712d92009-10-15 07:44:07 +00003588// CmdArgs.push_back("--eh-frame-hdr");
Edward O'Callaghan856e4ff2009-08-22 01:06:46 +00003589 CmdArgs.push_back("-Bdynamic");
3590 if (Args.hasArg(options::OPT_shared)) {
3591 CmdArgs.push_back("-shared");
3592 } else {
Edward O'Callaghan7d3c2752009-10-16 19:44:18 +00003593 CmdArgs.push_back("--dynamic-linker");
Edward O'Callaghan856e4ff2009-08-22 01:06:46 +00003594 CmdArgs.push_back("/lib/ld.so.1"); // 64Bit Path /lib/amd64/ld.so.1
3595 }
3596 }
3597
Daniel Dunbarb440f562010-08-02 02:38:21 +00003598 if (Output.isFilename()) {
Edward O'Callaghan856e4ff2009-08-22 01:06:46 +00003599 CmdArgs.push_back("-o");
3600 CmdArgs.push_back(Output.getFilename());
3601 } else {
3602 assert(Output.isNothing() && "Invalid output.");
3603 }
3604
3605 if (!Args.hasArg(options::OPT_nostdlib) &&
3606 !Args.hasArg(options::OPT_nostartfiles)) {
3607 if (!Args.hasArg(options::OPT_shared)) {
Chris Lattner3e2ee142010-07-07 16:01:42 +00003608 CmdArgs.push_back(Args.MakeArgString(
Daniel Dunbar9c3ed5f2010-07-14 18:46:23 +00003609 getToolChain().GetFilePath("crt1.o")));
Chris Lattner3e2ee142010-07-07 16:01:42 +00003610 CmdArgs.push_back(Args.MakeArgString(
Daniel Dunbar9c3ed5f2010-07-14 18:46:23 +00003611 getToolChain().GetFilePath("crti.o")));
Chris Lattner3e2ee142010-07-07 16:01:42 +00003612 CmdArgs.push_back(Args.MakeArgString(
Daniel Dunbar9c3ed5f2010-07-14 18:46:23 +00003613 getToolChain().GetFilePath("crtbegin.o")));
Edward O'Callaghan856e4ff2009-08-22 01:06:46 +00003614 } else {
Chris Lattner3e2ee142010-07-07 16:01:42 +00003615 CmdArgs.push_back(Args.MakeArgString(
Daniel Dunbar9c3ed5f2010-07-14 18:46:23 +00003616 getToolChain().GetFilePath("crti.o")));
Edward O'Callaghan856e4ff2009-08-22 01:06:46 +00003617 }
Chris Lattner3e2ee142010-07-07 16:01:42 +00003618 CmdArgs.push_back(Args.MakeArgString(
Daniel Dunbar9c3ed5f2010-07-14 18:46:23 +00003619 getToolChain().GetFilePath("crtn.o")));
Edward O'Callaghan856e4ff2009-08-22 01:06:46 +00003620 }
3621
Daniel Dunbar1c7577c2009-11-04 06:24:38 +00003622 CmdArgs.push_back(Args.MakeArgString("-L/opt/gcc4/lib/gcc/"
3623 + getToolChain().getTripleString()
Daniel Dunbarb0b18612009-10-29 02:24:37 +00003624 + "/4.2.4"));
Edward O'Callaghan856e4ff2009-08-22 01:06:46 +00003625
3626 Args.AddAllArgs(CmdArgs, options::OPT_L);
3627 Args.AddAllArgs(CmdArgs, options::OPT_T_Group);
3628 Args.AddAllArgs(CmdArgs, options::OPT_e);
3629
Daniel Dunbar54423b22010-09-17 00:24:54 +00003630 AddLinkerInputs(getToolChain(), Inputs, Args, CmdArgs);
Edward O'Callaghan856e4ff2009-08-22 01:06:46 +00003631
3632 if (!Args.hasArg(options::OPT_nostdlib) &&
3633 !Args.hasArg(options::OPT_nodefaultlibs)) {
3634 // FIXME: For some reason GCC passes -lgcc before adding
3635 // the default system libraries. Just mimic this for now.
3636 CmdArgs.push_back("-lgcc");
3637
3638 if (Args.hasArg(options::OPT_pthread))
3639 CmdArgs.push_back("-pthread");
3640 if (!Args.hasArg(options::OPT_shared))
3641 CmdArgs.push_back("-lc");
3642 CmdArgs.push_back("-lgcc");
3643 }
3644
3645 if (!Args.hasArg(options::OPT_nostdlib) &&
3646 !Args.hasArg(options::OPT_nostartfiles)) {
3647 if (!Args.hasArg(options::OPT_shared))
Chris Lattner3e2ee142010-07-07 16:01:42 +00003648 CmdArgs.push_back(Args.MakeArgString(
Daniel Dunbar9c3ed5f2010-07-14 18:46:23 +00003649 getToolChain().GetFilePath("crtend.o")));
Edward O'Callaghan856e4ff2009-08-22 01:06:46 +00003650 }
3651
Bill Wendling08760582011-06-27 19:15:03 +00003652 addProfileRT(getToolChain(), Args, CmdArgs, getToolChain().getTriple());
Nick Lewycky82fe5f42011-05-24 21:54:59 +00003653
Edward O'Callaghan856e4ff2009-08-22 01:06:46 +00003654 const char *Exec =
Daniel Dunbar9c3ed5f2010-07-14 18:46:23 +00003655 Args.MakeArgString(getToolChain().GetProgramPath("ld"));
Daniel Dunbar7fbaf532010-08-02 02:38:28 +00003656 C.addCommand(new Command(JA, *this, Exec, CmdArgs));
Edward O'Callaghan856e4ff2009-08-22 01:06:46 +00003657}
3658
Daniel Dunbar10de9e62009-06-29 20:52:51 +00003659void openbsd::Assemble::ConstructJob(Compilation &C, const JobAction &JA,
Daniel Dunbar7fbaf532010-08-02 02:38:28 +00003660 const InputInfo &Output,
Daniel Dunbar10de9e62009-06-29 20:52:51 +00003661 const InputInfoList &Inputs,
3662 const ArgList &Args,
Mike Stump11289f42009-09-09 15:08:12 +00003663 const char *LinkingOutput) const {
Daniel Dunbar10de9e62009-06-29 20:52:51 +00003664 ArgStringList CmdArgs;
3665
3666 Args.AddAllArgValues(CmdArgs, options::OPT_Wa_COMMA,
3667 options::OPT_Xassembler);
3668
3669 CmdArgs.push_back("-o");
Daniel Dunbarb440f562010-08-02 02:38:21 +00003670 CmdArgs.push_back(Output.getFilename());
Daniel Dunbar10de9e62009-06-29 20:52:51 +00003671
3672 for (InputInfoList::const_iterator
3673 it = Inputs.begin(), ie = Inputs.end(); it != ie; ++it) {
3674 const InputInfo &II = *it;
Daniel Dunbarb440f562010-08-02 02:38:21 +00003675 CmdArgs.push_back(II.getFilename());
Daniel Dunbar10de9e62009-06-29 20:52:51 +00003676 }
3677
3678 const char *Exec =
Daniel Dunbar9c3ed5f2010-07-14 18:46:23 +00003679 Args.MakeArgString(getToolChain().GetProgramPath("as"));
Daniel Dunbar7fbaf532010-08-02 02:38:28 +00003680 C.addCommand(new Command(JA, *this, Exec, CmdArgs));
Daniel Dunbar10de9e62009-06-29 20:52:51 +00003681}
3682
3683void openbsd::Link::ConstructJob(Compilation &C, const JobAction &JA,
Daniel Dunbar7fbaf532010-08-02 02:38:28 +00003684 const InputInfo &Output,
Daniel Dunbar10de9e62009-06-29 20:52:51 +00003685 const InputInfoList &Inputs,
3686 const ArgList &Args,
3687 const char *LinkingOutput) const {
Daniel Dunbar083edf72009-12-21 18:54:17 +00003688 const Driver &D = getToolChain().getDriver();
Daniel Dunbar10de9e62009-06-29 20:52:51 +00003689 ArgStringList CmdArgs;
3690
Daniel Dunbara8888ac2009-08-03 01:28:59 +00003691 if ((!Args.hasArg(options::OPT_nostdlib)) &&
Daniel Dunbar1c7577c2009-11-04 06:24:38 +00003692 (!Args.hasArg(options::OPT_shared))) {
Daniel Dunbara8888ac2009-08-03 01:28:59 +00003693 CmdArgs.push_back("-e");
3694 CmdArgs.push_back("__start");
3695 }
3696
Daniel Dunbar10de9e62009-06-29 20:52:51 +00003697 if (Args.hasArg(options::OPT_static)) {
3698 CmdArgs.push_back("-Bstatic");
3699 } else {
Rafael Espindola7ba97af2010-11-11 02:17:51 +00003700 if (Args.hasArg(options::OPT_rdynamic))
3701 CmdArgs.push_back("-export-dynamic");
Daniel Dunbar10de9e62009-06-29 20:52:51 +00003702 CmdArgs.push_back("--eh-frame-hdr");
Daniel Dunbara8888ac2009-08-03 01:28:59 +00003703 CmdArgs.push_back("-Bdynamic");
Daniel Dunbar10de9e62009-06-29 20:52:51 +00003704 if (Args.hasArg(options::OPT_shared)) {
Daniel Dunbara8888ac2009-08-03 01:28:59 +00003705 CmdArgs.push_back("-shared");
Daniel Dunbar10de9e62009-06-29 20:52:51 +00003706 } else {
3707 CmdArgs.push_back("-dynamic-linker");
3708 CmdArgs.push_back("/usr/libexec/ld.so");
3709 }
3710 }
3711
Daniel Dunbarb440f562010-08-02 02:38:21 +00003712 if (Output.isFilename()) {
Daniel Dunbar10de9e62009-06-29 20:52:51 +00003713 CmdArgs.push_back("-o");
3714 CmdArgs.push_back(Output.getFilename());
3715 } else {
3716 assert(Output.isNothing() && "Invalid output.");
3717 }
3718
3719 if (!Args.hasArg(options::OPT_nostdlib) &&
3720 !Args.hasArg(options::OPT_nostartfiles)) {
3721 if (!Args.hasArg(options::OPT_shared)) {
Chris Lattner3e2ee142010-07-07 16:01:42 +00003722 CmdArgs.push_back(Args.MakeArgString(
Daniel Dunbar9c3ed5f2010-07-14 18:46:23 +00003723 getToolChain().GetFilePath("crt0.o")));
Chris Lattner3e2ee142010-07-07 16:01:42 +00003724 CmdArgs.push_back(Args.MakeArgString(
Daniel Dunbar9c3ed5f2010-07-14 18:46:23 +00003725 getToolChain().GetFilePath("crtbegin.o")));
Daniel Dunbar10de9e62009-06-29 20:52:51 +00003726 } else {
Chris Lattner3e2ee142010-07-07 16:01:42 +00003727 CmdArgs.push_back(Args.MakeArgString(
Daniel Dunbar9c3ed5f2010-07-14 18:46:23 +00003728 getToolChain().GetFilePath("crtbeginS.o")));
Daniel Dunbar10de9e62009-06-29 20:52:51 +00003729 }
3730 }
3731
Edward O'Callaghan5c521462009-10-28 15:13:08 +00003732 std::string Triple = getToolChain().getTripleString();
3733 if (Triple.substr(0, 6) == "x86_64")
Daniel Dunbar1c7577c2009-11-04 06:24:38 +00003734 Triple.replace(0, 6, "amd64");
Daniel Dunbarb0b18612009-10-29 02:24:37 +00003735 CmdArgs.push_back(Args.MakeArgString("-L/usr/lib/gcc-lib/" + Triple +
Daniel Dunbarea3813f2010-08-01 23:13:54 +00003736 "/4.2.1"));
Daniel Dunbara8888ac2009-08-03 01:28:59 +00003737
Daniel Dunbar10de9e62009-06-29 20:52:51 +00003738 Args.AddAllArgs(CmdArgs, options::OPT_L);
3739 Args.AddAllArgs(CmdArgs, options::OPT_T_Group);
3740 Args.AddAllArgs(CmdArgs, options::OPT_e);
3741
Daniel Dunbar54423b22010-09-17 00:24:54 +00003742 AddLinkerInputs(getToolChain(), Inputs, Args, CmdArgs);
Daniel Dunbar10de9e62009-06-29 20:52:51 +00003743
3744 if (!Args.hasArg(options::OPT_nostdlib) &&
3745 !Args.hasArg(options::OPT_nodefaultlibs)) {
Daniel Dunbarea3813f2010-08-01 23:13:54 +00003746 if (D.CCCIsCXX) {
Daniel Dunbar3f7796f2010-09-17 01:20:05 +00003747 getToolChain().AddCXXStdlibLibArgs(Args, CmdArgs);
Daniel Dunbarea3813f2010-08-01 23:13:54 +00003748 CmdArgs.push_back("-lm");
3749 }
3750
Daniel Dunbara8888ac2009-08-03 01:28:59 +00003751 // FIXME: For some reason GCC passes -lgcc before adding
3752 // the default system libraries. Just mimic this for now.
3753 CmdArgs.push_back("-lgcc");
Daniel Dunbar10de9e62009-06-29 20:52:51 +00003754
3755 if (Args.hasArg(options::OPT_pthread))
Chris Lattnerd0257f72011-02-21 18:36:51 +00003756 CmdArgs.push_back("-lpthread");
Daniel Dunbara8888ac2009-08-03 01:28:59 +00003757 if (!Args.hasArg(options::OPT_shared))
3758 CmdArgs.push_back("-lc");
3759 CmdArgs.push_back("-lgcc");
Daniel Dunbar10de9e62009-06-29 20:52:51 +00003760 }
3761
3762 if (!Args.hasArg(options::OPT_nostdlib) &&
3763 !Args.hasArg(options::OPT_nostartfiles)) {
3764 if (!Args.hasArg(options::OPT_shared))
Chris Lattner3e2ee142010-07-07 16:01:42 +00003765 CmdArgs.push_back(Args.MakeArgString(
Daniel Dunbar9c3ed5f2010-07-14 18:46:23 +00003766 getToolChain().GetFilePath("crtend.o")));
Daniel Dunbar10de9e62009-06-29 20:52:51 +00003767 else
Chris Lattner3e2ee142010-07-07 16:01:42 +00003768 CmdArgs.push_back(Args.MakeArgString(
Daniel Dunbar9c3ed5f2010-07-14 18:46:23 +00003769 getToolChain().GetFilePath("crtendS.o")));
Daniel Dunbar10de9e62009-06-29 20:52:51 +00003770 }
3771
3772 const char *Exec =
Daniel Dunbar9c3ed5f2010-07-14 18:46:23 +00003773 Args.MakeArgString(getToolChain().GetProgramPath("ld"));
Daniel Dunbar7fbaf532010-08-02 02:38:28 +00003774 C.addCommand(new Command(JA, *this, Exec, CmdArgs));
Daniel Dunbar10de9e62009-06-29 20:52:51 +00003775}
Ed Schoutene33194b2009-04-02 19:13:12 +00003776
Daniel Dunbar8eb473c2009-03-31 17:45:15 +00003777void freebsd::Assemble::ConstructJob(Compilation &C, const JobAction &JA,
Daniel Dunbar7fbaf532010-08-02 02:38:28 +00003778 const InputInfo &Output,
Daniel Dunbard067f7f2009-04-08 23:54:23 +00003779 const InputInfoList &Inputs,
3780 const ArgList &Args,
Mike Stump11289f42009-09-09 15:08:12 +00003781 const char *LinkingOutput) const {
Daniel Dunbar8eb473c2009-03-31 17:45:15 +00003782 ArgStringList CmdArgs;
3783
Daniel Dunbard854c8d2009-04-01 19:36:32 +00003784 // When building 32-bit code on FreeBSD/amd64, we have to explicitly
3785 // instruct as in the base system to assemble 32-bit code.
3786 if (getToolChain().getArchName() == "i386")
Daniel Dunbar8eb473c2009-03-31 17:45:15 +00003787 CmdArgs.push_back("--32");
3788
Roman Divacky00859c22011-06-04 07:37:31 +00003789 if (getToolChain().getArchName() == "powerpc")
3790 CmdArgs.push_back("-a32");
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00003791
Eric Christopher0b26a612010-03-02 02:41:08 +00003792 // Set byte order explicitly
3793 if (getToolChain().getArchName() == "mips")
3794 CmdArgs.push_back("-EB");
3795 else if (getToolChain().getArchName() == "mipsel")
3796 CmdArgs.push_back("-EL");
3797
Daniel Dunbar8eb473c2009-03-31 17:45:15 +00003798 Args.AddAllArgValues(CmdArgs, options::OPT_Wa_COMMA,
3799 options::OPT_Xassembler);
3800
3801 CmdArgs.push_back("-o");
Daniel Dunbarb440f562010-08-02 02:38:21 +00003802 CmdArgs.push_back(Output.getFilename());
Daniel Dunbar8eb473c2009-03-31 17:45:15 +00003803
3804 for (InputInfoList::const_iterator
3805 it = Inputs.begin(), ie = Inputs.end(); it != ie; ++it) {
3806 const InputInfo &II = *it;
Daniel Dunbarb440f562010-08-02 02:38:21 +00003807 CmdArgs.push_back(II.getFilename());
Daniel Dunbar8eb473c2009-03-31 17:45:15 +00003808 }
3809
Daniel Dunbard067f7f2009-04-08 23:54:23 +00003810 const char *Exec =
Daniel Dunbar9c3ed5f2010-07-14 18:46:23 +00003811 Args.MakeArgString(getToolChain().GetProgramPath("as"));
Daniel Dunbar7fbaf532010-08-02 02:38:28 +00003812 C.addCommand(new Command(JA, *this, Exec, CmdArgs));
Daniel Dunbar8eb473c2009-03-31 17:45:15 +00003813}
Daniel Dunbard854c8d2009-04-01 19:36:32 +00003814
3815void freebsd::Link::ConstructJob(Compilation &C, const JobAction &JA,
Daniel Dunbar7fbaf532010-08-02 02:38:28 +00003816 const InputInfo &Output,
Daniel Dunbard067f7f2009-04-08 23:54:23 +00003817 const InputInfoList &Inputs,
3818 const ArgList &Args,
Daniel Dunbare3e263f2009-05-02 20:14:53 +00003819 const char *LinkingOutput) const {
Daniel Dunbar083edf72009-12-21 18:54:17 +00003820 const Driver &D = getToolChain().getDriver();
Daniel Dunbard854c8d2009-04-01 19:36:32 +00003821 ArgStringList CmdArgs;
3822
Joerg Sonnenberger6165ab12011-03-21 13:51:29 +00003823 if (!D.SysRoot.empty())
3824 CmdArgs.push_back(Args.MakeArgString("--sysroot=" + D.SysRoot));
3825
Daniel Dunbard854c8d2009-04-01 19:36:32 +00003826 if (Args.hasArg(options::OPT_static)) {
3827 CmdArgs.push_back("-Bstatic");
3828 } else {
Rafael Espindola7ba97af2010-11-11 02:17:51 +00003829 if (Args.hasArg(options::OPT_rdynamic))
3830 CmdArgs.push_back("-export-dynamic");
Daniel Dunbard854c8d2009-04-01 19:36:32 +00003831 CmdArgs.push_back("--eh-frame-hdr");
3832 if (Args.hasArg(options::OPT_shared)) {
3833 CmdArgs.push_back("-Bshareable");
3834 } else {
3835 CmdArgs.push_back("-dynamic-linker");
3836 CmdArgs.push_back("/libexec/ld-elf.so.1");
3837 }
3838 }
3839
3840 // When building 32-bit code on FreeBSD/amd64, we have to explicitly
3841 // instruct ld in the base system to link 32-bit code.
3842 if (getToolChain().getArchName() == "i386") {
3843 CmdArgs.push_back("-m");
3844 CmdArgs.push_back("elf_i386_fbsd");
3845 }
3846
Roman Divacky5e300b82011-06-04 07:40:24 +00003847 if (getToolChain().getArchName() == "powerpc") {
3848 CmdArgs.push_back("-m");
3849 CmdArgs.push_back("elf32ppc");
3850 }
3851
Daniel Dunbarb440f562010-08-02 02:38:21 +00003852 if (Output.isFilename()) {
Daniel Dunbard854c8d2009-04-01 19:36:32 +00003853 CmdArgs.push_back("-o");
3854 CmdArgs.push_back(Output.getFilename());
3855 } else {
3856 assert(Output.isNothing() && "Invalid output.");
3857 }
3858
3859 if (!Args.hasArg(options::OPT_nostdlib) &&
3860 !Args.hasArg(options::OPT_nostartfiles)) {
3861 if (!Args.hasArg(options::OPT_shared)) {
Roman Divacky66f22762011-02-10 16:59:40 +00003862 if (Args.hasArg(options::OPT_pg))
3863 CmdArgs.push_back(Args.MakeArgString(
3864 getToolChain().GetFilePath("gcrt1.o")));
3865 else
3866 CmdArgs.push_back(Args.MakeArgString(
3867 getToolChain().GetFilePath("crt1.o")));
Chris Lattner3e2ee142010-07-07 16:01:42 +00003868 CmdArgs.push_back(Args.MakeArgString(
Daniel Dunbar9c3ed5f2010-07-14 18:46:23 +00003869 getToolChain().GetFilePath("crti.o")));
Chris Lattner3e2ee142010-07-07 16:01:42 +00003870 CmdArgs.push_back(Args.MakeArgString(
Daniel Dunbar9c3ed5f2010-07-14 18:46:23 +00003871 getToolChain().GetFilePath("crtbegin.o")));
Daniel Dunbard854c8d2009-04-01 19:36:32 +00003872 } else {
Chris Lattner3e2ee142010-07-07 16:01:42 +00003873 CmdArgs.push_back(Args.MakeArgString(
Daniel Dunbar9c3ed5f2010-07-14 18:46:23 +00003874 getToolChain().GetFilePath("crti.o")));
Chris Lattner3e2ee142010-07-07 16:01:42 +00003875 CmdArgs.push_back(Args.MakeArgString(
Daniel Dunbar9c3ed5f2010-07-14 18:46:23 +00003876 getToolChain().GetFilePath("crtbeginS.o")));
Daniel Dunbard854c8d2009-04-01 19:36:32 +00003877 }
3878 }
3879
3880 Args.AddAllArgs(CmdArgs, options::OPT_L);
Roman Divackyee8188a2011-03-01 17:53:14 +00003881 const ToolChain::path_list Paths = getToolChain().getFilePaths();
3882 for (ToolChain::path_list::const_iterator i = Paths.begin(), e = Paths.end();
3883 i != e; ++i)
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003884 CmdArgs.push_back(Args.MakeArgString(StringRef("-L") + *i));
Daniel Dunbard854c8d2009-04-01 19:36:32 +00003885 Args.AddAllArgs(CmdArgs, options::OPT_T_Group);
3886 Args.AddAllArgs(CmdArgs, options::OPT_e);
David Chisnall589a4942010-08-15 22:58:12 +00003887 Args.AddAllArgs(CmdArgs, options::OPT_s);
3888 Args.AddAllArgs(CmdArgs, options::OPT_t);
3889 Args.AddAllArgs(CmdArgs, options::OPT_Z_Flag);
3890 Args.AddAllArgs(CmdArgs, options::OPT_r);
Daniel Dunbard854c8d2009-04-01 19:36:32 +00003891
Daniel Dunbar54423b22010-09-17 00:24:54 +00003892 AddLinkerInputs(getToolChain(), Inputs, Args, CmdArgs);
Daniel Dunbard854c8d2009-04-01 19:36:32 +00003893
3894 if (!Args.hasArg(options::OPT_nostdlib) &&
3895 !Args.hasArg(options::OPT_nodefaultlibs)) {
Daniel Dunbar4b8ef282010-02-17 08:07:51 +00003896 if (D.CCCIsCXX) {
Daniel Dunbar3f7796f2010-09-17 01:20:05 +00003897 getToolChain().AddCXXStdlibLibArgs(Args, CmdArgs);
Roman Divacky66f22762011-02-10 16:59:40 +00003898 if (Args.hasArg(options::OPT_pg))
3899 CmdArgs.push_back("-lm_p");
3900 else
3901 CmdArgs.push_back("-lm");
Daniel Dunbar4b8ef282010-02-17 08:07:51 +00003902 }
Daniel Dunbard854c8d2009-04-01 19:36:32 +00003903 // FIXME: For some reason GCC passes -lgcc and -lgcc_s before adding
3904 // the default system libraries. Just mimic this for now.
Roman Divacky66f22762011-02-10 16:59:40 +00003905 if (Args.hasArg(options::OPT_pg))
3906 CmdArgs.push_back("-lgcc_p");
3907 else
3908 CmdArgs.push_back("-lgcc");
Daniel Dunbard854c8d2009-04-01 19:36:32 +00003909 if (Args.hasArg(options::OPT_static)) {
3910 CmdArgs.push_back("-lgcc_eh");
Roman Divacky66f22762011-02-10 16:59:40 +00003911 } else if (Args.hasArg(options::OPT_pg)) {
3912 CmdArgs.push_back("-lgcc_eh_p");
Daniel Dunbard854c8d2009-04-01 19:36:32 +00003913 } else {
3914 CmdArgs.push_back("--as-needed");
3915 CmdArgs.push_back("-lgcc_s");
3916 CmdArgs.push_back("--no-as-needed");
3917 }
3918
Matt Beaumont-Gay1fe49152011-02-10 20:35:01 +00003919 if (Args.hasArg(options::OPT_pthread)) {
Roman Divacky66f22762011-02-10 16:59:40 +00003920 if (Args.hasArg(options::OPT_pg))
3921 CmdArgs.push_back("-lpthread_p");
3922 else
3923 CmdArgs.push_back("-lpthread");
Matt Beaumont-Gay1fe49152011-02-10 20:35:01 +00003924 }
Daniel Dunbard854c8d2009-04-01 19:36:32 +00003925
Roman Divacky66f22762011-02-10 16:59:40 +00003926 if (Args.hasArg(options::OPT_pg)) {
3927 if (Args.hasArg(options::OPT_shared))
3928 CmdArgs.push_back("-lc");
3929 else
3930 CmdArgs.push_back("-lc_p");
3931 CmdArgs.push_back("-lgcc_p");
3932 } else {
3933 CmdArgs.push_back("-lc");
3934 CmdArgs.push_back("-lgcc");
3935 }
3936
Daniel Dunbard854c8d2009-04-01 19:36:32 +00003937 if (Args.hasArg(options::OPT_static)) {
3938 CmdArgs.push_back("-lgcc_eh");
Roman Divacky66f22762011-02-10 16:59:40 +00003939 } else if (Args.hasArg(options::OPT_pg)) {
3940 CmdArgs.push_back("-lgcc_eh_p");
Daniel Dunbard854c8d2009-04-01 19:36:32 +00003941 } else {
3942 CmdArgs.push_back("--as-needed");
3943 CmdArgs.push_back("-lgcc_s");
3944 CmdArgs.push_back("--no-as-needed");
3945 }
3946 }
3947
3948 if (!Args.hasArg(options::OPT_nostdlib) &&
3949 !Args.hasArg(options::OPT_nostartfiles)) {
3950 if (!Args.hasArg(options::OPT_shared))
Daniel Dunbar9c3ed5f2010-07-14 18:46:23 +00003951 CmdArgs.push_back(Args.MakeArgString(getToolChain().GetFilePath(
Chris Lattner3e2ee142010-07-07 16:01:42 +00003952 "crtend.o")));
Daniel Dunbard854c8d2009-04-01 19:36:32 +00003953 else
Daniel Dunbar9c3ed5f2010-07-14 18:46:23 +00003954 CmdArgs.push_back(Args.MakeArgString(getToolChain().GetFilePath(
Chris Lattner3e2ee142010-07-07 16:01:42 +00003955 "crtendS.o")));
Daniel Dunbar9c3ed5f2010-07-14 18:46:23 +00003956 CmdArgs.push_back(Args.MakeArgString(getToolChain().GetFilePath(
Chris Lattner3e2ee142010-07-07 16:01:42 +00003957 "crtn.o")));
Daniel Dunbard854c8d2009-04-01 19:36:32 +00003958 }
3959
Bill Wendling08760582011-06-27 19:15:03 +00003960 addProfileRT(getToolChain(), Args, CmdArgs, getToolChain().getTriple());
Nick Lewycky82fe5f42011-05-24 21:54:59 +00003961
Daniel Dunbard067f7f2009-04-08 23:54:23 +00003962 const char *Exec =
Daniel Dunbar9c3ed5f2010-07-14 18:46:23 +00003963 Args.MakeArgString(getToolChain().GetProgramPath("ld"));
Daniel Dunbar7fbaf532010-08-02 02:38:28 +00003964 C.addCommand(new Command(JA, *this, Exec, CmdArgs));
Daniel Dunbard854c8d2009-04-01 19:36:32 +00003965}
Daniel Dunbarcc912342009-05-02 18:28:39 +00003966
Benjamin Kramer24f1d3e2011-02-02 18:59:27 +00003967void netbsd::Assemble::ConstructJob(Compilation &C, const JobAction &JA,
3968 const InputInfo &Output,
3969 const InputInfoList &Inputs,
3970 const ArgList &Args,
3971 const char *LinkingOutput) const {
3972 ArgStringList CmdArgs;
3973
3974 // When building 32-bit code on NetBSD/amd64, we have to explicitly
3975 // instruct as in the base system to assemble 32-bit code.
Joerg Sonnenberger637603a2011-05-16 13:35:02 +00003976 if (ToolTriple.getArch() == llvm::Triple::x86_64 &&
3977 getToolChain().getArch() == llvm::Triple::x86)
Benjamin Kramer24f1d3e2011-02-02 18:59:27 +00003978 CmdArgs.push_back("--32");
3979
3980
3981 // Set byte order explicitly
3982 if (getToolChain().getArchName() == "mips")
3983 CmdArgs.push_back("-EB");
3984 else if (getToolChain().getArchName() == "mipsel")
3985 CmdArgs.push_back("-EL");
3986
3987 Args.AddAllArgValues(CmdArgs, options::OPT_Wa_COMMA,
3988 options::OPT_Xassembler);
3989
3990 CmdArgs.push_back("-o");
3991 CmdArgs.push_back(Output.getFilename());
3992
3993 for (InputInfoList::const_iterator
3994 it = Inputs.begin(), ie = Inputs.end(); it != ie; ++it) {
3995 const InputInfo &II = *it;
3996 CmdArgs.push_back(II.getFilename());
3997 }
3998
Joerg Sonnenbergerfcc3ec92011-03-21 14:01:40 +00003999 const char *Exec = Args.MakeArgString(FindTargetProgramPath(getToolChain(),
Joerg Sonnenberger637603a2011-05-16 13:35:02 +00004000 ToolTriple.getTriple(),
4001 "as"));
Benjamin Kramer24f1d3e2011-02-02 18:59:27 +00004002 C.addCommand(new Command(JA, *this, Exec, CmdArgs));
4003}
4004
4005void netbsd::Link::ConstructJob(Compilation &C, const JobAction &JA,
4006 const InputInfo &Output,
4007 const InputInfoList &Inputs,
4008 const ArgList &Args,
4009 const char *LinkingOutput) const {
4010 const Driver &D = getToolChain().getDriver();
4011 ArgStringList CmdArgs;
4012
Joerg Sonnenberger6165ab12011-03-21 13:51:29 +00004013 if (!D.SysRoot.empty())
4014 CmdArgs.push_back(Args.MakeArgString("--sysroot=" + D.SysRoot));
4015
Benjamin Kramer24f1d3e2011-02-02 18:59:27 +00004016 if (Args.hasArg(options::OPT_static)) {
4017 CmdArgs.push_back("-Bstatic");
4018 } else {
4019 if (Args.hasArg(options::OPT_rdynamic))
4020 CmdArgs.push_back("-export-dynamic");
4021 CmdArgs.push_back("--eh-frame-hdr");
4022 if (Args.hasArg(options::OPT_shared)) {
4023 CmdArgs.push_back("-Bshareable");
4024 } else {
4025 CmdArgs.push_back("-dynamic-linker");
4026 CmdArgs.push_back("/libexec/ld.elf_so");
4027 }
4028 }
4029
4030 // When building 32-bit code on NetBSD/amd64, we have to explicitly
4031 // instruct ld in the base system to link 32-bit code.
Joerg Sonnenberger637603a2011-05-16 13:35:02 +00004032 if (ToolTriple.getArch() == llvm::Triple::x86_64 &&
4033 getToolChain().getArch() == llvm::Triple::x86) {
Benjamin Kramer24f1d3e2011-02-02 18:59:27 +00004034 CmdArgs.push_back("-m");
4035 CmdArgs.push_back("elf_i386");
4036 }
4037
4038 if (Output.isFilename()) {
4039 CmdArgs.push_back("-o");
4040 CmdArgs.push_back(Output.getFilename());
4041 } else {
4042 assert(Output.isNothing() && "Invalid output.");
4043 }
4044
4045 if (!Args.hasArg(options::OPT_nostdlib) &&
4046 !Args.hasArg(options::OPT_nostartfiles)) {
4047 if (!Args.hasArg(options::OPT_shared)) {
4048 CmdArgs.push_back(Args.MakeArgString(
4049 getToolChain().GetFilePath("crt0.o")));
4050 CmdArgs.push_back(Args.MakeArgString(
4051 getToolChain().GetFilePath("crti.o")));
4052 CmdArgs.push_back(Args.MakeArgString(
4053 getToolChain().GetFilePath("crtbegin.o")));
4054 } else {
4055 CmdArgs.push_back(Args.MakeArgString(
4056 getToolChain().GetFilePath("crti.o")));
4057 CmdArgs.push_back(Args.MakeArgString(
4058 getToolChain().GetFilePath("crtbeginS.o")));
4059 }
4060 }
4061
4062 Args.AddAllArgs(CmdArgs, options::OPT_L);
4063 Args.AddAllArgs(CmdArgs, options::OPT_T_Group);
4064 Args.AddAllArgs(CmdArgs, options::OPT_e);
4065 Args.AddAllArgs(CmdArgs, options::OPT_s);
4066 Args.AddAllArgs(CmdArgs, options::OPT_t);
4067 Args.AddAllArgs(CmdArgs, options::OPT_Z_Flag);
4068 Args.AddAllArgs(CmdArgs, options::OPT_r);
4069
4070 AddLinkerInputs(getToolChain(), Inputs, Args, CmdArgs);
4071
4072 if (!Args.hasArg(options::OPT_nostdlib) &&
4073 !Args.hasArg(options::OPT_nodefaultlibs)) {
4074 if (D.CCCIsCXX) {
4075 getToolChain().AddCXXStdlibLibArgs(Args, CmdArgs);
4076 CmdArgs.push_back("-lm");
4077 }
4078 // FIXME: For some reason GCC passes -lgcc and -lgcc_s before adding
4079 // the default system libraries. Just mimic this for now.
Benjamin Kramer24f1d3e2011-02-02 18:59:27 +00004080 if (Args.hasArg(options::OPT_static)) {
4081 CmdArgs.push_back("-lgcc_eh");
4082 } else {
4083 CmdArgs.push_back("--as-needed");
4084 CmdArgs.push_back("-lgcc_s");
4085 CmdArgs.push_back("--no-as-needed");
4086 }
Joerg Sonnenberger87717772011-06-07 23:39:17 +00004087 CmdArgs.push_back("-lgcc");
Benjamin Kramer24f1d3e2011-02-02 18:59:27 +00004088
4089 if (Args.hasArg(options::OPT_pthread))
4090 CmdArgs.push_back("-lpthread");
4091 CmdArgs.push_back("-lc");
4092
4093 CmdArgs.push_back("-lgcc");
4094 if (Args.hasArg(options::OPT_static)) {
4095 CmdArgs.push_back("-lgcc_eh");
4096 } else {
4097 CmdArgs.push_back("--as-needed");
4098 CmdArgs.push_back("-lgcc_s");
4099 CmdArgs.push_back("--no-as-needed");
4100 }
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(getToolChain().GetFilePath(
4107 "crtend.o")));
4108 else
4109 CmdArgs.push_back(Args.MakeArgString(getToolChain().GetFilePath(
4110 "crtendS.o")));
4111 CmdArgs.push_back(Args.MakeArgString(getToolChain().GetFilePath(
4112 "crtn.o")));
4113 }
4114
Bill Wendling08760582011-06-27 19:15:03 +00004115 addProfileRT(getToolChain(), Args, CmdArgs, getToolChain().getTriple());
Nick Lewycky82fe5f42011-05-24 21:54:59 +00004116
Joerg Sonnenbergerfcc3ec92011-03-21 14:01:40 +00004117 const char *Exec = Args.MakeArgString(FindTargetProgramPath(getToolChain(),
Joerg Sonnenberger637603a2011-05-16 13:35:02 +00004118 ToolTriple.getTriple(),
4119 "ld"));
Benjamin Kramer24f1d3e2011-02-02 18:59:27 +00004120 C.addCommand(new Command(JA, *this, Exec, CmdArgs));
4121}
4122
Rafael Espindola92b00932010-08-10 00:25:48 +00004123void linuxtools::Assemble::ConstructJob(Compilation &C, const JobAction &JA,
4124 const InputInfo &Output,
4125 const InputInfoList &Inputs,
4126 const ArgList &Args,
4127 const char *LinkingOutput) const {
4128 ArgStringList CmdArgs;
4129
4130 // Add --32/--64 to make sure we get the format we want.
4131 // This is incomplete
4132 if (getToolChain().getArch() == llvm::Triple::x86) {
4133 CmdArgs.push_back("--32");
4134 } else if (getToolChain().getArch() == llvm::Triple::x86_64) {
4135 CmdArgs.push_back("--64");
4136 } else if (getToolChain().getArch() == llvm::Triple::arm) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00004137 StringRef MArch = getToolChain().getArchName();
Rafael Espindola92b00932010-08-10 00:25:48 +00004138 if (MArch == "armv7" || MArch == "armv7a" || MArch == "armv7-a")
4139 CmdArgs.push_back("-mfpu=neon");
4140 }
4141
4142 Args.AddAllArgValues(CmdArgs, options::OPT_Wa_COMMA,
4143 options::OPT_Xassembler);
4144
4145 CmdArgs.push_back("-o");
4146 CmdArgs.push_back(Output.getFilename());
4147
4148 for (InputInfoList::const_iterator
4149 it = Inputs.begin(), ie = Inputs.end(); it != ie; ++it) {
4150 const InputInfo &II = *it;
4151 CmdArgs.push_back(II.getFilename());
4152 }
4153
4154 const char *Exec =
4155 Args.MakeArgString(getToolChain().GetProgramPath("as"));
4156 C.addCommand(new Command(JA, *this, Exec, CmdArgs));
4157}
4158
Rafael Espindolac8f008f2010-11-07 20:14:31 +00004159void linuxtools::Link::ConstructJob(Compilation &C, const JobAction &JA,
4160 const InputInfo &Output,
4161 const InputInfoList &Inputs,
4162 const ArgList &Args,
4163 const char *LinkingOutput) const {
4164 const toolchains::Linux& ToolChain =
4165 static_cast<const toolchains::Linux&>(getToolChain());
4166 const Driver &D = ToolChain.getDriver();
4167 ArgStringList CmdArgs;
4168
Rafael Espindolad1002f62010-11-15 18:28:16 +00004169 // Silence warning for "clang -g foo.o -o foo"
4170 Args.ClaimAllArgs(options::OPT_g_Group);
Rafael Espindolad95a8122011-03-01 05:25:27 +00004171 // and "clang -emit-llvm foo.o -o foo"
4172 Args.ClaimAllArgs(options::OPT_emit_llvm);
Rafael Espindolaf92614c2010-11-17 20:37:10 +00004173 // and for "clang -g foo.o -o foo". Other warning options are already
4174 // handled somewhere else.
4175 Args.ClaimAllArgs(options::OPT_w);
Rafael Espindolad1002f62010-11-15 18:28:16 +00004176
Joerg Sonnenberger6165ab12011-03-21 13:51:29 +00004177 if (!D.SysRoot.empty())
4178 CmdArgs.push_back(Args.MakeArgString("--sysroot=" + D.SysRoot));
Rafael Espindolac8f008f2010-11-07 20:14:31 +00004179
Rafael Espindolad47ac232010-11-17 22:26:15 +00004180 if (Args.hasArg(options::OPT_pie))
4181 CmdArgs.push_back("-pie");
4182
Rafael Espindola1c76c592010-11-07 22:57:16 +00004183 if (Args.hasArg(options::OPT_rdynamic))
4184 CmdArgs.push_back("-export-dynamic");
4185
Rafael Espindola34d77dc2010-11-11 19:34:42 +00004186 if (Args.hasArg(options::OPT_s))
4187 CmdArgs.push_back("-s");
4188
Rafael Espindolac8f008f2010-11-07 20:14:31 +00004189 for (std::vector<std::string>::const_iterator i = ToolChain.ExtraOpts.begin(),
4190 e = ToolChain.ExtraOpts.end();
4191 i != e; ++i)
4192 CmdArgs.push_back(i->c_str());
4193
4194 if (!Args.hasArg(options::OPT_static)) {
4195 CmdArgs.push_back("--eh-frame-hdr");
4196 }
4197
4198 CmdArgs.push_back("-m");
4199 if (ToolChain.getArch() == llvm::Triple::x86)
4200 CmdArgs.push_back("elf_i386");
Eric Christopher84fbdb42011-08-19 00:30:14 +00004201 else if (ToolChain.getArch() == llvm::Triple::arm
Douglas Gregord9bb1522011-03-06 19:11:49 +00004202 || ToolChain.getArch() == llvm::Triple::thumb)
Rafael Espindolac8f008f2010-11-07 20:14:31 +00004203 CmdArgs.push_back("armelf_linux_eabi");
Ted Kremenek43d47cc2011-04-05 22:04:27 +00004204 else if (ToolChain.getArch() == llvm::Triple::ppc)
4205 CmdArgs.push_back("elf32ppclinux");
4206 else if (ToolChain.getArch() == llvm::Triple::ppc64)
4207 CmdArgs.push_back("elf64ppc");
Rafael Espindolac8f008f2010-11-07 20:14:31 +00004208 else
4209 CmdArgs.push_back("elf_x86_64");
4210
4211 if (Args.hasArg(options::OPT_static)) {
Douglas Gregord9bb1522011-03-06 19:11:49 +00004212 if (ToolChain.getArch() == llvm::Triple::arm
4213 || ToolChain.getArch() == llvm::Triple::thumb)
Rafael Espindolac8f008f2010-11-07 20:14:31 +00004214 CmdArgs.push_back("-Bstatic");
4215 else
4216 CmdArgs.push_back("-static");
4217 } else if (Args.hasArg(options::OPT_shared)) {
4218 CmdArgs.push_back("-shared");
4219 }
4220
4221 if (ToolChain.getArch() == llvm::Triple::arm ||
Douglas Gregord9bb1522011-03-06 19:11:49 +00004222 ToolChain.getArch() == llvm::Triple::thumb ||
Rafael Espindolac8f008f2010-11-07 20:14:31 +00004223 (!Args.hasArg(options::OPT_static) &&
4224 !Args.hasArg(options::OPT_shared))) {
4225 CmdArgs.push_back("-dynamic-linker");
4226 if (ToolChain.getArch() == llvm::Triple::x86)
4227 CmdArgs.push_back("/lib/ld-linux.so.2");
Douglas Gregord9bb1522011-03-06 19:11:49 +00004228 else if (ToolChain.getArch() == llvm::Triple::arm ||
4229 ToolChain.getArch() == llvm::Triple::thumb)
Rafael Espindolac8f008f2010-11-07 20:14:31 +00004230 CmdArgs.push_back("/lib/ld-linux.so.3");
Ted Kremenek43d47cc2011-04-05 22:04:27 +00004231 else if (ToolChain.getArch() == llvm::Triple::ppc)
Chris Lattner20b90d02011-04-11 21:15:37 +00004232 CmdArgs.push_back("/lib/ld.so.1");
Ted Kremenek43d47cc2011-04-05 22:04:27 +00004233 else if (ToolChain.getArch() == llvm::Triple::ppc64)
Chris Lattner20b90d02011-04-11 21:15:37 +00004234 CmdArgs.push_back("/lib64/ld64.so.1");
Rafael Espindolac8f008f2010-11-07 20:14:31 +00004235 else
4236 CmdArgs.push_back("/lib64/ld-linux-x86-64.so.2");
4237 }
4238
4239 CmdArgs.push_back("-o");
4240 CmdArgs.push_back(Output.getFilename());
4241
Rafael Espindola81937ec2010-12-01 01:52:43 +00004242 if (!Args.hasArg(options::OPT_nostdlib) &&
4243 !Args.hasArg(options::OPT_nostartfiles)) {
Rafael Espindolad47ac232010-11-17 22:26:15 +00004244 const char *crt1 = NULL;
4245 if (!Args.hasArg(options::OPT_shared)){
4246 if (Args.hasArg(options::OPT_pie))
4247 crt1 = "Scrt1.o";
4248 else
4249 crt1 = "crt1.o";
4250 }
4251 if (crt1)
4252 CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath(crt1)));
Rafael Espindolac8f008f2010-11-07 20:14:31 +00004253
Rafael Espindola9aa60e92010-11-12 03:00:39 +00004254 CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath("crti.o")));
Rafael Espindolac8f008f2010-11-07 20:14:31 +00004255
Rafael Espindola9aa60e92010-11-12 03:00:39 +00004256 const char *crtbegin;
4257 if (Args.hasArg(options::OPT_static))
4258 crtbegin = "crtbeginT.o";
Rafael Espindolad47ac232010-11-17 22:26:15 +00004259 else if (Args.hasArg(options::OPT_shared) || Args.hasArg(options::OPT_pie))
Rafael Espindola9aa60e92010-11-12 03:00:39 +00004260 crtbegin = "crtbeginS.o";
4261 else
4262 crtbegin = "crtbegin.o";
4263 CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath(crtbegin)));
4264 }
Rafael Espindolac8f008f2010-11-07 20:14:31 +00004265
4266 Args.AddAllArgs(CmdArgs, options::OPT_L);
4267
4268 const ToolChain::path_list Paths = ToolChain.getFilePaths();
4269
Roman Divackyee8188a2011-03-01 17:53:14 +00004270 for (ToolChain::path_list::const_iterator i = Paths.begin(), e = Paths.end();
4271 i != e; ++i)
Chris Lattner0e62c1c2011-07-23 10:55:15 +00004272 CmdArgs.push_back(Args.MakeArgString(StringRef("-L") + *i));
Rafael Espindolac8f008f2010-11-07 20:14:31 +00004273
4274 AddLinkerInputs(ToolChain, Inputs, Args, CmdArgs);
4275
Rafael Espindola9aa60e92010-11-12 03:00:39 +00004276 if (D.CCCIsCXX && !Args.hasArg(options::OPT_nostdlib)) {
Rafael Espindolac8f008f2010-11-07 20:14:31 +00004277 ToolChain.AddCXXStdlibLibArgs(Args, CmdArgs);
4278 CmdArgs.push_back("-lm");
4279 }
4280
Rafael Espindola9aa60e92010-11-12 03:00:39 +00004281 if (!Args.hasArg(options::OPT_nostdlib)) {
Nick Lewycky97864da2011-06-04 06:27:06 +00004282 if (Args.hasArg(options::OPT_static))
4283 CmdArgs.push_back("--start-group");
4284
Rafael Espindola9aa60e92010-11-12 03:00:39 +00004285 if (!D.CCCIsCXX)
Rafael Espindolac8f008f2010-11-07 20:14:31 +00004286 CmdArgs.push_back("-lgcc");
Rafael Espindola9aa60e92010-11-12 03:00:39 +00004287
4288 if (Args.hasArg(options::OPT_static)) {
4289 if (D.CCCIsCXX)
4290 CmdArgs.push_back("-lgcc");
4291 } else {
4292 if (!D.CCCIsCXX)
4293 CmdArgs.push_back("--as-needed");
4294 CmdArgs.push_back("-lgcc_s");
4295 if (!D.CCCIsCXX)
4296 CmdArgs.push_back("--no-as-needed");
4297 }
4298
4299 if (Args.hasArg(options::OPT_static))
4300 CmdArgs.push_back("-lgcc_eh");
4301 else if (!Args.hasArg(options::OPT_shared) && D.CCCIsCXX)
4302 CmdArgs.push_back("-lgcc");
4303
4304 if (Args.hasArg(options::OPT_pthread) ||
4305 Args.hasArg(options::OPT_pthreads))
4306 CmdArgs.push_back("-lpthread");
4307
4308 CmdArgs.push_back("-lc");
4309
4310 if (Args.hasArg(options::OPT_static))
4311 CmdArgs.push_back("--end-group");
4312 else {
4313 if (!D.CCCIsCXX)
4314 CmdArgs.push_back("-lgcc");
4315
4316 if (!D.CCCIsCXX)
4317 CmdArgs.push_back("--as-needed");
4318 CmdArgs.push_back("-lgcc_s");
4319 if (!D.CCCIsCXX)
4320 CmdArgs.push_back("--no-as-needed");
4321
4322 if (!Args.hasArg(options::OPT_shared) && D.CCCIsCXX)
4323 CmdArgs.push_back("-lgcc");
4324 }
4325
Rafael Espindolad47ac232010-11-17 22:26:15 +00004326
Rafael Espindola81937ec2010-12-01 01:52:43 +00004327 if (!Args.hasArg(options::OPT_nostartfiles)) {
4328 const char *crtend;
4329 if (Args.hasArg(options::OPT_shared) || Args.hasArg(options::OPT_pie))
4330 crtend = "crtendS.o";
4331 else
4332 crtend = "crtend.o";
Rafael Espindola9aa60e92010-11-12 03:00:39 +00004333
Rafael Espindola81937ec2010-12-01 01:52:43 +00004334 CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath(crtend)));
4335 CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath("crtn.o")));
4336 }
Rafael Espindolac8f008f2010-11-07 20:14:31 +00004337 }
4338
Bill Wendling08760582011-06-27 19:15:03 +00004339 addProfileRT(getToolChain(), Args, CmdArgs, getToolChain().getTriple());
Nick Lewycky82fe5f42011-05-24 21:54:59 +00004340
Rafael Espindolac8f008f2010-11-07 20:14:31 +00004341 if (Args.hasArg(options::OPT_use_gold_plugin)) {
4342 CmdArgs.push_back("-plugin");
4343 std::string Plugin = ToolChain.getDriver().Dir + "/../lib/LLVMgold.so";
4344 CmdArgs.push_back(Args.MakeArgString(Plugin));
4345 }
4346
4347 C.addCommand(new Command(JA, *this, ToolChain.Linker.c_str(), CmdArgs));
4348}
Rafael Espindola92b00932010-08-10 00:25:48 +00004349
Chris Lattner3e2ee142010-07-07 16:01:42 +00004350void minix::Assemble::ConstructJob(Compilation &C, const JobAction &JA,
Daniel Dunbar7fbaf532010-08-02 02:38:28 +00004351 const InputInfo &Output,
4352 const InputInfoList &Inputs,
4353 const ArgList &Args,
4354 const char *LinkingOutput) const {
Chris Lattner3e2ee142010-07-07 16:01:42 +00004355 ArgStringList CmdArgs;
4356
4357 Args.AddAllArgValues(CmdArgs, options::OPT_Wa_COMMA,
4358 options::OPT_Xassembler);
4359
4360 CmdArgs.push_back("-o");
Daniel Dunbarb440f562010-08-02 02:38:21 +00004361 CmdArgs.push_back(Output.getFilename());
Chris Lattner3e2ee142010-07-07 16:01:42 +00004362
4363 for (InputInfoList::const_iterator
4364 it = Inputs.begin(), ie = Inputs.end(); it != ie; ++it) {
4365 const InputInfo &II = *it;
Daniel Dunbarb440f562010-08-02 02:38:21 +00004366 CmdArgs.push_back(II.getFilename());
Chris Lattner3e2ee142010-07-07 16:01:42 +00004367 }
4368
4369 const char *Exec =
Daniel Dunbar9c3ed5f2010-07-14 18:46:23 +00004370 Args.MakeArgString(getToolChain().GetProgramPath("gas"));
Daniel Dunbar7fbaf532010-08-02 02:38:28 +00004371 C.addCommand(new Command(JA, *this, Exec, CmdArgs));
Chris Lattner3e2ee142010-07-07 16:01:42 +00004372}
4373
4374void minix::Link::ConstructJob(Compilation &C, const JobAction &JA,
Daniel Dunbar7fbaf532010-08-02 02:38:28 +00004375 const InputInfo &Output,
4376 const InputInfoList &Inputs,
4377 const ArgList &Args,
4378 const char *LinkingOutput) const {
Chris Lattner3e2ee142010-07-07 16:01:42 +00004379 const Driver &D = getToolChain().getDriver();
4380 ArgStringList CmdArgs;
4381
Daniel Dunbarb440f562010-08-02 02:38:21 +00004382 if (Output.isFilename()) {
Chris Lattner3e2ee142010-07-07 16:01:42 +00004383 CmdArgs.push_back("-o");
4384 CmdArgs.push_back(Output.getFilename());
4385 } else {
4386 assert(Output.isNothing() && "Invalid output.");
4387 }
4388
4389 if (!Args.hasArg(options::OPT_nostdlib) &&
4390 !Args.hasArg(options::OPT_nostartfiles))
Daniel Dunbar9c3ed5f2010-07-14 18:46:23 +00004391 CmdArgs.push_back(Args.MakeArgString(getToolChain().GetFilePath(
Chris Lattner3e2ee142010-07-07 16:01:42 +00004392 "/usr/gnu/lib/crtso.o")));
4393
4394 Args.AddAllArgs(CmdArgs, options::OPT_L);
4395 Args.AddAllArgs(CmdArgs, options::OPT_T_Group);
4396 Args.AddAllArgs(CmdArgs, options::OPT_e);
4397
Daniel Dunbar54423b22010-09-17 00:24:54 +00004398 AddLinkerInputs(getToolChain(), Inputs, Args, CmdArgs);
Chris Lattner3e2ee142010-07-07 16:01:42 +00004399
4400 if (!Args.hasArg(options::OPT_nostdlib) &&
4401 !Args.hasArg(options::OPT_nodefaultlibs)) {
4402 if (D.CCCIsCXX) {
Daniel Dunbar3f7796f2010-09-17 01:20:05 +00004403 getToolChain().AddCXXStdlibLibArgs(Args, CmdArgs);
Chris Lattner3e2ee142010-07-07 16:01:42 +00004404 CmdArgs.push_back("-lm");
4405 }
4406
4407 if (Args.hasArg(options::OPT_pthread))
4408 CmdArgs.push_back("-lpthread");
4409 CmdArgs.push_back("-lc");
4410 CmdArgs.push_back("-lgcc");
4411 CmdArgs.push_back("-L/usr/gnu/lib");
4412 // FIXME: fill in the correct search path for the final
4413 // support libraries.
4414 CmdArgs.push_back("-L/usr/gnu/lib/gcc/i686-pc-minix/4.4.3");
4415 }
4416
4417 if (!Args.hasArg(options::OPT_nostdlib) &&
4418 !Args.hasArg(options::OPT_nostartfiles)) {
Daniel Dunbar9c3ed5f2010-07-14 18:46:23 +00004419 CmdArgs.push_back(Args.MakeArgString(getToolChain().GetFilePath(
Chris Lattner3e2ee142010-07-07 16:01:42 +00004420 "/usr/gnu/lib/libend.a")));
4421 }
4422
Bill Wendling08760582011-06-27 19:15:03 +00004423 addProfileRT(getToolChain(), Args, CmdArgs, getToolChain().getTriple());
Nick Lewycky82fe5f42011-05-24 21:54:59 +00004424
Chris Lattner3e2ee142010-07-07 16:01:42 +00004425 const char *Exec =
Daniel Dunbar9c3ed5f2010-07-14 18:46:23 +00004426 Args.MakeArgString(getToolChain().GetProgramPath("/usr/gnu/bin/gld"));
Daniel Dunbar7fbaf532010-08-02 02:38:28 +00004427 C.addCommand(new Command(JA, *this, Exec, CmdArgs));
Chris Lattner3e2ee142010-07-07 16:01:42 +00004428}
4429
Daniel Dunbarcc912342009-05-02 18:28:39 +00004430/// DragonFly Tools
4431
4432// For now, DragonFly Assemble does just about the same as for
4433// FreeBSD, but this may change soon.
4434void dragonfly::Assemble::ConstructJob(Compilation &C, const JobAction &JA,
Daniel Dunbar7fbaf532010-08-02 02:38:28 +00004435 const InputInfo &Output,
Daniel Dunbar1c7577c2009-11-04 06:24:38 +00004436 const InputInfoList &Inputs,
4437 const ArgList &Args,
4438 const char *LinkingOutput) const {
Daniel Dunbarcc912342009-05-02 18:28:39 +00004439 ArgStringList CmdArgs;
4440
4441 // When building 32-bit code on DragonFly/pc64, we have to explicitly
4442 // instruct as in the base system to assemble 32-bit code.
4443 if (getToolChain().getArchName() == "i386")
4444 CmdArgs.push_back("--32");
4445
4446 Args.AddAllArgValues(CmdArgs, options::OPT_Wa_COMMA,
4447 options::OPT_Xassembler);
4448
4449 CmdArgs.push_back("-o");
Daniel Dunbarb440f562010-08-02 02:38:21 +00004450 CmdArgs.push_back(Output.getFilename());
Daniel Dunbarcc912342009-05-02 18:28:39 +00004451
4452 for (InputInfoList::const_iterator
4453 it = Inputs.begin(), ie = Inputs.end(); it != ie; ++it) {
4454 const InputInfo &II = *it;
Daniel Dunbarb440f562010-08-02 02:38:21 +00004455 CmdArgs.push_back(II.getFilename());
Daniel Dunbarcc912342009-05-02 18:28:39 +00004456 }
4457
4458 const char *Exec =
Daniel Dunbar9c3ed5f2010-07-14 18:46:23 +00004459 Args.MakeArgString(getToolChain().GetProgramPath("as"));
Daniel Dunbar7fbaf532010-08-02 02:38:28 +00004460 C.addCommand(new Command(JA, *this, Exec, CmdArgs));
Daniel Dunbarcc912342009-05-02 18:28:39 +00004461}
4462
4463void dragonfly::Link::ConstructJob(Compilation &C, const JobAction &JA,
Daniel Dunbar7fbaf532010-08-02 02:38:28 +00004464 const InputInfo &Output,
4465 const InputInfoList &Inputs,
4466 const ArgList &Args,
4467 const char *LinkingOutput) const {
Daniel Dunbar083edf72009-12-21 18:54:17 +00004468 const Driver &D = getToolChain().getDriver();
Daniel Dunbarcc912342009-05-02 18:28:39 +00004469 ArgStringList CmdArgs;
4470
Joerg Sonnenberger6165ab12011-03-21 13:51:29 +00004471 if (!D.SysRoot.empty())
4472 CmdArgs.push_back(Args.MakeArgString("--sysroot=" + D.SysRoot));
4473
Daniel Dunbarcc912342009-05-02 18:28:39 +00004474 if (Args.hasArg(options::OPT_static)) {
4475 CmdArgs.push_back("-Bstatic");
4476 } else {
4477 if (Args.hasArg(options::OPT_shared))
4478 CmdArgs.push_back("-Bshareable");
4479 else {
4480 CmdArgs.push_back("-dynamic-linker");
4481 CmdArgs.push_back("/usr/libexec/ld-elf.so.2");
4482 }
4483 }
4484
4485 // When building 32-bit code on DragonFly/pc64, we have to explicitly
4486 // instruct ld in the base system to link 32-bit code.
4487 if (getToolChain().getArchName() == "i386") {
4488 CmdArgs.push_back("-m");
4489 CmdArgs.push_back("elf_i386");
4490 }
4491
Daniel Dunbarb440f562010-08-02 02:38:21 +00004492 if (Output.isFilename()) {
Daniel Dunbarcc912342009-05-02 18:28:39 +00004493 CmdArgs.push_back("-o");
4494 CmdArgs.push_back(Output.getFilename());
4495 } else {
4496 assert(Output.isNothing() && "Invalid output.");
4497 }
4498
4499 if (!Args.hasArg(options::OPT_nostdlib) &&
4500 !Args.hasArg(options::OPT_nostartfiles)) {
4501 if (!Args.hasArg(options::OPT_shared)) {
Chris Lattner3e2ee142010-07-07 16:01:42 +00004502 CmdArgs.push_back(
Daniel Dunbar9c3ed5f2010-07-14 18:46:23 +00004503 Args.MakeArgString(getToolChain().GetFilePath("crt1.o")));
Chris Lattner3e2ee142010-07-07 16:01:42 +00004504 CmdArgs.push_back(
Daniel Dunbar9c3ed5f2010-07-14 18:46:23 +00004505 Args.MakeArgString(getToolChain().GetFilePath("crti.o")));
Chris Lattner3e2ee142010-07-07 16:01:42 +00004506 CmdArgs.push_back(
Daniel Dunbar9c3ed5f2010-07-14 18:46:23 +00004507 Args.MakeArgString(getToolChain().GetFilePath("crtbegin.o")));
Daniel Dunbarcc912342009-05-02 18:28:39 +00004508 } else {
Chris Lattner3e2ee142010-07-07 16:01:42 +00004509 CmdArgs.push_back(
Daniel Dunbar9c3ed5f2010-07-14 18:46:23 +00004510 Args.MakeArgString(getToolChain().GetFilePath("crti.o")));
Chris Lattner3e2ee142010-07-07 16:01:42 +00004511 CmdArgs.push_back(
Daniel Dunbar9c3ed5f2010-07-14 18:46:23 +00004512 Args.MakeArgString(getToolChain().GetFilePath("crtbeginS.o")));
Daniel Dunbarcc912342009-05-02 18:28:39 +00004513 }
4514 }
4515
4516 Args.AddAllArgs(CmdArgs, options::OPT_L);
4517 Args.AddAllArgs(CmdArgs, options::OPT_T_Group);
4518 Args.AddAllArgs(CmdArgs, options::OPT_e);
4519
Daniel Dunbar54423b22010-09-17 00:24:54 +00004520 AddLinkerInputs(getToolChain(), Inputs, Args, CmdArgs);
Daniel Dunbarcc912342009-05-02 18:28:39 +00004521
4522 if (!Args.hasArg(options::OPT_nostdlib) &&
4523 !Args.hasArg(options::OPT_nodefaultlibs)) {
4524 // FIXME: GCC passes on -lgcc, -lgcc_pic and a whole lot of
4525 // rpaths
4526 CmdArgs.push_back("-L/usr/lib/gcc41");
4527
4528 if (!Args.hasArg(options::OPT_static)) {
4529 CmdArgs.push_back("-rpath");
4530 CmdArgs.push_back("/usr/lib/gcc41");
4531
4532 CmdArgs.push_back("-rpath-link");
4533 CmdArgs.push_back("/usr/lib/gcc41");
4534
4535 CmdArgs.push_back("-rpath");
4536 CmdArgs.push_back("/usr/lib");
4537
4538 CmdArgs.push_back("-rpath-link");
4539 CmdArgs.push_back("/usr/lib");
4540 }
4541
Rafael Espindola38360b32010-07-20 12:59:03 +00004542 if (D.CCCIsCXX) {
Daniel Dunbar3f7796f2010-09-17 01:20:05 +00004543 getToolChain().AddCXXStdlibLibArgs(Args, CmdArgs);
Rafael Espindola38360b32010-07-20 12:59:03 +00004544 CmdArgs.push_back("-lm");
4545 }
4546
Daniel Dunbarcc912342009-05-02 18:28:39 +00004547 if (Args.hasArg(options::OPT_shared)) {
4548 CmdArgs.push_back("-lgcc_pic");
4549 } else {
4550 CmdArgs.push_back("-lgcc");
4551 }
4552
4553
4554 if (Args.hasArg(options::OPT_pthread))
Mike Stump0a65b632009-10-31 20:11:46 +00004555 CmdArgs.push_back("-lpthread");
Daniel Dunbarcc912342009-05-02 18:28:39 +00004556
4557 if (!Args.hasArg(options::OPT_nolibc)) {
4558 CmdArgs.push_back("-lc");
4559 }
4560
4561 if (Args.hasArg(options::OPT_shared)) {
4562 CmdArgs.push_back("-lgcc_pic");
4563 } else {
4564 CmdArgs.push_back("-lgcc");
4565 }
4566 }
4567
4568 if (!Args.hasArg(options::OPT_nostdlib) &&
4569 !Args.hasArg(options::OPT_nostartfiles)) {
4570 if (!Args.hasArg(options::OPT_shared))
Chris Lattner3e2ee142010-07-07 16:01:42 +00004571 CmdArgs.push_back(Args.MakeArgString(
Daniel Dunbar9c3ed5f2010-07-14 18:46:23 +00004572 getToolChain().GetFilePath("crtend.o")));
Daniel Dunbarcc912342009-05-02 18:28:39 +00004573 else
Chris Lattner3e2ee142010-07-07 16:01:42 +00004574 CmdArgs.push_back(Args.MakeArgString(
Daniel Dunbar9c3ed5f2010-07-14 18:46:23 +00004575 getToolChain().GetFilePath("crtendS.o")));
Chris Lattner3e2ee142010-07-07 16:01:42 +00004576 CmdArgs.push_back(Args.MakeArgString(
Daniel Dunbar9c3ed5f2010-07-14 18:46:23 +00004577 getToolChain().GetFilePath("crtn.o")));
Daniel Dunbarcc912342009-05-02 18:28:39 +00004578 }
4579
Bill Wendling08760582011-06-27 19:15:03 +00004580 addProfileRT(getToolChain(), Args, CmdArgs, getToolChain().getTriple());
Nick Lewycky82fe5f42011-05-24 21:54:59 +00004581
Daniel Dunbarcc912342009-05-02 18:28:39 +00004582 const char *Exec =
Daniel Dunbar9c3ed5f2010-07-14 18:46:23 +00004583 Args.MakeArgString(getToolChain().GetProgramPath("ld"));
Daniel Dunbar7fbaf532010-08-02 02:38:28 +00004584 C.addCommand(new Command(JA, *this, Exec, CmdArgs));
Daniel Dunbarcc912342009-05-02 18:28:39 +00004585}
Michael J. Spencerb186bc32010-08-21 21:55:07 +00004586
4587void visualstudio::Link::ConstructJob(Compilation &C, const JobAction &JA,
4588 const InputInfo &Output,
4589 const InputInfoList &Inputs,
4590 const ArgList &Args,
4591 const char *LinkingOutput) const {
Michael J. Spencerb186bc32010-08-21 21:55:07 +00004592 ArgStringList CmdArgs;
4593
4594 if (Output.isFilename()) {
Daniel Dunbar2cc3f172010-09-17 00:45:02 +00004595 CmdArgs.push_back(Args.MakeArgString(std::string("-out:") +
4596 Output.getFilename()));
Michael J. Spencerb186bc32010-08-21 21:55:07 +00004597 } else {
4598 assert(Output.isNothing() && "Invalid output.");
4599 }
4600
4601 if (!Args.hasArg(options::OPT_nostdlib) &&
4602 !Args.hasArg(options::OPT_nostartfiles)) {
4603 CmdArgs.push_back("-defaultlib:libcmt");
4604 }
4605
4606 CmdArgs.push_back("-nologo");
4607
Daniel Dunbar54423b22010-09-17 00:24:54 +00004608 AddLinkerInputs(getToolChain(), Inputs, Args, CmdArgs);
Michael J. Spencerb186bc32010-08-21 21:55:07 +00004609
4610 const char *Exec =
Daniel Dunbar54423b22010-09-17 00:24:54 +00004611 Args.MakeArgString(getToolChain().GetProgramPath("link.exe"));
Michael J. Spencerb186bc32010-08-21 21:55:07 +00004612 C.addCommand(new Command(JA, *this, Exec, CmdArgs));
4613}