blob: 0c97cf33f411dbe3d81592ea594b188618971317 [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 }
Douglas Gregor97eec242011-09-15 22:00:41 +0000393
394 Args.AddAllArgs(CmdArgs, options::OPT_fauto_module_import);
Daniel Dunbard067f7f2009-04-08 23:54:23 +0000395}
396
Chris Lattner57540c52011-04-15 05:22:18 +0000397/// getARMTargetCPU - Get the (LLVM) name of the ARM cpu we are targeting.
Daniel Dunbarfb58b0a2009-09-10 06:49:20 +0000398//
399// FIXME: tblgen this.
Rafael Espindola0e1fb4f2010-06-28 17:18:09 +0000400static const char *getARMTargetCPU(const ArgList &Args,
401 const llvm::Triple &Triple) {
Daniel Dunbarfb58b0a2009-09-10 06:49:20 +0000402 // FIXME: Warn on inconsistent use of -mcpu and -march.
Daniel Dunbar0f5c5422009-09-10 04:57:17 +0000403
404 // If we have -mcpu=, use that.
405 if (Arg *A = Args.getLastArg(options::OPT_mcpu_EQ))
406 return A->getValue(Args);
407
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000408 StringRef MArch;
Daniel Dunbar0f5c5422009-09-10 04:57:17 +0000409 if (Arg *A = Args.getLastArg(options::OPT_march_EQ)) {
Rafael Espindola0e1fb4f2010-06-28 17:18:09 +0000410 // Otherwise, if we have -march= choose the base CPU for that arch.
411 MArch = A->getValue(Args);
412 } else {
413 // Otherwise, use the Arch from the triple.
414 MArch = Triple.getArchName();
Daniel Dunbar0f5c5422009-09-10 04:57:17 +0000415 }
416
Rafael Espindola0e1fb4f2010-06-28 17:18:09 +0000417 if (MArch == "armv2" || MArch == "armv2a")
418 return "arm2";
419 if (MArch == "armv3")
420 return "arm6";
421 if (MArch == "armv3m")
422 return "arm7m";
423 if (MArch == "armv4" || MArch == "armv4t")
424 return "arm7tdmi";
425 if (MArch == "armv5" || MArch == "armv5t")
426 return "arm10tdmi";
427 if (MArch == "armv5e" || MArch == "armv5te")
428 return "arm1026ejs";
429 if (MArch == "armv5tej")
430 return "arm926ej-s";
431 if (MArch == "armv6" || MArch == "armv6k")
432 return "arm1136jf-s";
433 if (MArch == "armv6j")
434 return "arm1136j-s";
435 if (MArch == "armv6z" || MArch == "armv6zk")
436 return "arm1176jzf-s";
437 if (MArch == "armv6t2")
438 return "arm1156t2-s";
439 if (MArch == "armv7" || MArch == "armv7a" || MArch == "armv7-a")
440 return "cortex-a8";
441 if (MArch == "armv7r" || MArch == "armv7-r")
442 return "cortex-r4";
443 if (MArch == "armv7m" || MArch == "armv7-m")
444 return "cortex-m3";
445 if (MArch == "ep9312")
446 return "ep9312";
447 if (MArch == "iwmmxt")
448 return "iwmmxt";
449 if (MArch == "xscale")
450 return "xscale";
Bob Wilsond9249412011-03-21 20:40:05 +0000451 if (MArch == "armv6m" || MArch == "armv6-m")
452 return "cortex-m0";
Rafael Espindola0e1fb4f2010-06-28 17:18:09 +0000453
454 // If all else failed, return the most base CPU LLVM supports.
Daniel Dunbar0f5c5422009-09-10 04:57:17 +0000455 return "arm7tdmi";
456}
457
Daniel Dunbarf492c922009-09-10 22:59:51 +0000458/// getLLVMArchSuffixForARM - Get the LLVM arch name to use for a particular
Daniel Dunbarfb58b0a2009-09-10 06:49:20 +0000459/// CPU.
460//
461// FIXME: This is redundant with -mcpu, why does LLVM use this.
462// FIXME: tblgen this, or kill it!
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000463static const char *getLLVMArchSuffixForARM(StringRef CPU) {
Daniel Dunbarfb58b0a2009-09-10 06:49:20 +0000464 if (CPU == "arm7tdmi" || CPU == "arm7tdmi-s" || CPU == "arm710t" ||
465 CPU == "arm720t" || CPU == "arm9" || CPU == "arm9tdmi" ||
466 CPU == "arm920" || CPU == "arm920t" || CPU == "arm922t" ||
467 CPU == "arm940t" || CPU == "ep9312")
Daniel Dunbarf492c922009-09-10 22:59:51 +0000468 return "v4t";
Daniel Dunbarfb58b0a2009-09-10 06:49:20 +0000469
470 if (CPU == "arm10tdmi" || CPU == "arm1020t")
Daniel Dunbarf492c922009-09-10 22:59:51 +0000471 return "v5";
Daniel Dunbarfb58b0a2009-09-10 06:49:20 +0000472
473 if (CPU == "arm9e" || CPU == "arm926ej-s" || CPU == "arm946e-s" ||
474 CPU == "arm966e-s" || CPU == "arm968e-s" || CPU == "arm10e" ||
475 CPU == "arm1020e" || CPU == "arm1022e" || CPU == "xscale" ||
476 CPU == "iwmmxt")
Daniel Dunbarf492c922009-09-10 22:59:51 +0000477 return "v5e";
Daniel Dunbarfb58b0a2009-09-10 06:49:20 +0000478
479 if (CPU == "arm1136j-s" || CPU == "arm1136jf-s" || CPU == "arm1176jz-s" ||
480 CPU == "arm1176jzf-s" || CPU == "mpcorenovfp" || CPU == "mpcore")
Daniel Dunbarf492c922009-09-10 22:59:51 +0000481 return "v6";
Daniel Dunbarfb58b0a2009-09-10 06:49:20 +0000482
483 if (CPU == "arm1156t2-s" || CPU == "arm1156t2f-s")
Daniel Dunbarf492c922009-09-10 22:59:51 +0000484 return "v6t2";
Daniel Dunbarfb58b0a2009-09-10 06:49:20 +0000485
486 if (CPU == "cortex-a8" || CPU == "cortex-a9")
Daniel Dunbarf492c922009-09-10 22:59:51 +0000487 return "v7";
Daniel Dunbarfb58b0a2009-09-10 06:49:20 +0000488
Daniel Dunbarf492c922009-09-10 22:59:51 +0000489 return "";
Daniel Dunbarfb58b0a2009-09-10 06:49:20 +0000490}
491
Daniel Dunbard609b7b2009-11-17 06:37:03 +0000492// FIXME: Move to target hook.
493static bool isSignedCharDefault(const llvm::Triple &Triple) {
494 switch (Triple.getArch()) {
495 default:
496 return true;
497
Jim Grosbach7c2c6642011-05-24 15:40:46 +0000498 case llvm::Triple::arm:
Daniel Dunbard609b7b2009-11-17 06:37:03 +0000499 case llvm::Triple::ppc:
500 case llvm::Triple::ppc64:
501 if (Triple.getOS() == llvm::Triple::Darwin)
502 return true;
503 return false;
504
505 case llvm::Triple::systemz:
506 return false;
507 }
508}
509
Daniel Dunbar0f5c5422009-09-10 04:57:17 +0000510void Clang::AddARMTargetArgs(const ArgList &Args,
Daniel Dunbarc9388c12011-03-17 17:10:06 +0000511 ArgStringList &CmdArgs,
512 bool KernelOrKext) const {
Daniel Dunbar083edf72009-12-21 18:54:17 +0000513 const Driver &D = getToolChain().getDriver();
Rafael Espindola0e1fb4f2010-06-28 17:18:09 +0000514 llvm::Triple Triple = getToolChain().getTriple();
Daniel Dunbar78485922009-09-10 23:00:09 +0000515
Daniel Dunbar908b4852011-03-02 00:55:57 +0000516 // Disable movt generation, if requested.
517#ifdef DISABLE_ARM_DARWIN_USE_MOVT
Daniel Dunbar12100e22011-03-22 16:48:17 +0000518 CmdArgs.push_back("-backend-option");
Daniel Dunbar908b4852011-03-02 00:55:57 +0000519 CmdArgs.push_back("-arm-darwin-use-movt=0");
520#endif
521
Daniel Dunbar4ed78982009-09-14 00:34:46 +0000522 // Select the ABI to use.
523 //
524 // FIXME: Support -meabi.
525 const char *ABIName = 0;
526 if (Arg *A = Args.getLastArg(options::OPT_mabi_EQ)) {
527 ABIName = A->getValue(Args);
528 } else {
529 // Select the default based on the platform.
Bob Wilsond1447c42011-02-04 17:59:28 +0000530 switch(Triple.getEnvironment()) {
531 case llvm::Triple::GNUEABI:
Daniel Dunbar4ed78982009-09-14 00:34:46 +0000532 ABIName = "aapcs-linux";
Bob Wilsond1447c42011-02-04 17:59:28 +0000533 break;
534 case llvm::Triple::EABI:
Rafael Espindola23a8a062010-06-16 19:01:17 +0000535 ABIName = "aapcs";
Bob Wilsond1447c42011-02-04 17:59:28 +0000536 break;
537 default:
Rafael Espindola23a8a062010-06-16 19:01:17 +0000538 ABIName = "apcs-gnu";
Bob Wilsond1447c42011-02-04 17:59:28 +0000539 }
Daniel Dunbar4ed78982009-09-14 00:34:46 +0000540 }
541 CmdArgs.push_back("-target-abi");
542 CmdArgs.push_back(ABIName);
543
Daniel Dunbar0f5c5422009-09-10 04:57:17 +0000544 // Set the CPU based on -march= and -mcpu=.
Daniel Dunbara7d02312009-12-18 06:30:12 +0000545 CmdArgs.push_back("-target-cpu");
Rafael Espindola0e1fb4f2010-06-28 17:18:09 +0000546 CmdArgs.push_back(getARMTargetCPU(Args, Triple));
Daniel Dunbar0f5c5422009-09-10 04:57:17 +0000547
Daniel Dunbar78485922009-09-10 23:00:09 +0000548 // Select the float ABI as determined by -msoft-float, -mhard-float, and
549 // -mfloat-abi=.
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000550 StringRef FloatABI;
Daniel Dunbar78485922009-09-10 23:00:09 +0000551 if (Arg *A = Args.getLastArg(options::OPT_msoft_float,
552 options::OPT_mhard_float,
553 options::OPT_mfloat_abi_EQ)) {
554 if (A->getOption().matches(options::OPT_msoft_float))
555 FloatABI = "soft";
556 else if (A->getOption().matches(options::OPT_mhard_float))
557 FloatABI = "hard";
558 else {
559 FloatABI = A->getValue(Args);
560 if (FloatABI != "soft" && FloatABI != "softfp" && FloatABI != "hard") {
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000561 D.Diag(diag::err_drv_invalid_mfloat_abi)
Daniel Dunbar78485922009-09-10 23:00:09 +0000562 << A->getAsString(Args);
563 FloatABI = "soft";
564 }
565 }
566 }
567
568 // If unspecified, choose the default based on the platform.
569 if (FloatABI.empty()) {
Rafael Espindola0e1fb4f2010-06-28 17:18:09 +0000570 const llvm::Triple &Triple = getToolChain().getTriple();
571 switch (Triple.getOS()) {
Daniel Dunbar78485922009-09-10 23:00:09 +0000572 case llvm::Triple::Darwin: {
573 // Darwin defaults to "softfp" for v6 and v7.
574 //
575 // FIXME: Factor out an ARM class so we can cache the arch somewhere.
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000576 StringRef ArchName =
Rafael Espindola0e1fb4f2010-06-28 17:18:09 +0000577 getLLVMArchSuffixForARM(getARMTargetCPU(Args, Triple));
Daniel Dunbar78485922009-09-10 23:00:09 +0000578 if (ArchName.startswith("v6") || ArchName.startswith("v7"))
579 FloatABI = "softfp";
580 else
581 FloatABI = "soft";
582 break;
583 }
584
Rafael Espindolab1ef8ff2010-06-27 18:29:21 +0000585 case llvm::Triple::Linux: {
Bob Wilsond1447c42011-02-04 17:59:28 +0000586 if (getToolChain().getTriple().getEnvironment() == llvm::Triple::GNUEABI) {
Rafael Espindolab1ef8ff2010-06-27 18:29:21 +0000587 FloatABI = "softfp";
588 break;
589 }
590 }
591 // fall through
592
Daniel Dunbar78485922009-09-10 23:00:09 +0000593 default:
Bob Wilsond1447c42011-02-04 17:59:28 +0000594 switch(Triple.getEnvironment()) {
595 case llvm::Triple::GNUEABI:
596 FloatABI = "softfp";
597 break;
598 case llvm::Triple::EABI:
599 // EABI is always AAPCS, and if it was not marked 'hard', it's softfp
600 FloatABI = "softfp";
601 break;
602 default:
603 // Assume "soft", but warn the user we are guessing.
604 FloatABI = "soft";
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000605 D.Diag(diag::warn_drv_assuming_mfloat_abi_is) << "soft";
Bob Wilsond1447c42011-02-04 17:59:28 +0000606 break;
607 }
Daniel Dunbar78485922009-09-10 23:00:09 +0000608 }
609 }
610
611 if (FloatABI == "soft") {
612 // Floating point operations and argument passing are soft.
613 //
614 // FIXME: This changes CPP defines, we need -target-soft-float.
Daniel Dunbara74f8ff2009-11-30 08:42:00 +0000615 CmdArgs.push_back("-msoft-float");
Daniel Dunbar6cc525b2009-12-08 19:49:51 +0000616 CmdArgs.push_back("-mfloat-abi");
617 CmdArgs.push_back("soft");
Daniel Dunbar78485922009-09-10 23:00:09 +0000618 } else if (FloatABI == "softfp") {
619 // Floating point operations are hard, but argument passing is soft.
Daniel Dunbar6cc525b2009-12-08 19:49:51 +0000620 CmdArgs.push_back("-mfloat-abi");
621 CmdArgs.push_back("soft");
Daniel Dunbar78485922009-09-10 23:00:09 +0000622 } else {
623 // Floating point operations and argument passing are hard.
624 assert(FloatABI == "hard" && "Invalid float abi!");
Daniel Dunbar6cc525b2009-12-08 19:49:51 +0000625 CmdArgs.push_back("-mfloat-abi");
626 CmdArgs.push_back("hard");
Daniel Dunbar78485922009-09-10 23:00:09 +0000627 }
Daniel Dunbar893d4752009-12-19 04:15:38 +0000628
629 // Set appropriate target features for floating point mode.
630 //
631 // FIXME: Note, this is a hack, the LLVM backend doesn't actually use these
632 // yet (it uses the -mfloat-abi and -msoft-float options above), and it is
633 // stripped out by the ARM target.
634
635 // Use software floating point operations?
636 if (FloatABI == "soft") {
637 CmdArgs.push_back("-target-feature");
638 CmdArgs.push_back("+soft-float");
639 }
640
641 // Use software floating point argument passing?
642 if (FloatABI != "hard") {
643 CmdArgs.push_back("-target-feature");
644 CmdArgs.push_back("+soft-float-abi");
645 }
Daniel Dunbar0def3d12009-12-21 23:28:17 +0000646
647 // Honor -mfpu=.
648 //
649 // FIXME: Centralize feature selection, defaulting shouldn't be also in the
650 // frontend target.
651 if (const Arg *A = Args.getLastArg(options::OPT_mfpu_EQ)) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000652 StringRef FPU = A->getValue(Args);
Daniel Dunbar0def3d12009-12-21 23:28:17 +0000653
654 // Set the target features based on the FPU.
655 if (FPU == "fpa" || FPU == "fpe2" || FPU == "fpe3" || FPU == "maverick") {
656 // Disable any default FPU support.
657 CmdArgs.push_back("-target-feature");
658 CmdArgs.push_back("-vfp2");
659 CmdArgs.push_back("-target-feature");
660 CmdArgs.push_back("-vfp3");
661 CmdArgs.push_back("-target-feature");
662 CmdArgs.push_back("-neon");
663 } else if (FPU == "vfp") {
664 CmdArgs.push_back("-target-feature");
665 CmdArgs.push_back("+vfp2");
666 } else if (FPU == "vfp3") {
667 CmdArgs.push_back("-target-feature");
668 CmdArgs.push_back("+vfp3");
669 } else if (FPU == "neon") {
670 CmdArgs.push_back("-target-feature");
671 CmdArgs.push_back("+neon");
672 } else
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000673 D.Diag(diag::err_drv_clang_unsupported) << A->getAsString(Args);
Daniel Dunbar0def3d12009-12-21 23:28:17 +0000674 }
Daniel Dunbarb1db4b62011-03-17 00:07:34 +0000675
676 // Setting -msoft-float effectively disables NEON because of the GCC
677 // implementation, although the same isn't true of VFP or VFP3.
678 if (FloatABI == "soft") {
Daniel Dunbarc9388c12011-03-17 17:10:06 +0000679 CmdArgs.push_back("-target-feature");
680 CmdArgs.push_back("-neon");
681 }
682
683 // Kernel code has more strict alignment requirements.
684 if (KernelOrKext) {
Daniel Dunbar12100e22011-03-22 16:48:17 +0000685 CmdArgs.push_back("-backend-option");
Daniel Dunbarc9388c12011-03-17 17:10:06 +0000686 CmdArgs.push_back("-arm-long-calls");
687
Daniel Dunbar12100e22011-03-22 16:48:17 +0000688 CmdArgs.push_back("-backend-option");
Daniel Dunbarc9388c12011-03-17 17:10:06 +0000689 CmdArgs.push_back("-arm-strict-align");
Daniel Dunbared904c82011-04-18 21:26:42 +0000690
691 // The kext linker doesn't know how to deal with movw/movt.
692#ifndef DISABLE_ARM_DARWIN_USE_MOVT
693 CmdArgs.push_back("-backend-option");
694 CmdArgs.push_back("-arm-darwin-use-movt=0");
695#endif
Daniel Dunbarb1db4b62011-03-17 00:07:34 +0000696 }
Chad Rosierba3df1d2011-08-26 00:26:29 +0000697
698 // Setting -mno-global-merge disables the codegen global merge pass. Setting
699 // -mglobal-merge has no effect as the pass is enabled by default.
700 if (Arg *A = Args.getLastArg(options::OPT_mglobal_merge,
701 options::OPT_mno_global_merge)) {
702 if (A->getOption().matches(options::OPT_mno_global_merge))
703 CmdArgs.push_back("-mno-global-merge");
704 }
Daniel Dunbar0f5c5422009-09-10 04:57:17 +0000705}
706
Eric Christopher0b26a612010-03-02 02:41:08 +0000707void Clang::AddMIPSTargetArgs(const ArgList &Args,
708 ArgStringList &CmdArgs) const {
709 const Driver &D = getToolChain().getDriver();
710
711 // Select the ABI to use.
712 const char *ABIName = 0;
713 if (Arg *A = Args.getLastArg(options::OPT_mabi_EQ)) {
714 ABIName = A->getValue(Args);
715 } else {
716 ABIName = "o32";
717 }
718
719 CmdArgs.push_back("-target-abi");
720 CmdArgs.push_back(ABIName);
721
722 if (const Arg *A = Args.getLastArg(options::OPT_march_EQ)) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000723 StringRef MArch = A->getValue(Args);
Eric Christopher0b26a612010-03-02 02:41:08 +0000724 CmdArgs.push_back("-target-cpu");
725
726 if ((MArch == "r2000") || (MArch == "r3000"))
727 CmdArgs.push_back("mips1");
728 else if (MArch == "r6000")
729 CmdArgs.push_back("mips2");
730 else
Nick Lewycky36d8f052011-05-04 03:44:01 +0000731 CmdArgs.push_back(Args.MakeArgString(MArch));
Eric Christopher0b26a612010-03-02 02:41:08 +0000732 }
733
734 // Select the float ABI as determined by -msoft-float, -mhard-float, and
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000735 StringRef FloatABI;
Eric Christopher0b26a612010-03-02 02:41:08 +0000736 if (Arg *A = Args.getLastArg(options::OPT_msoft_float,
737 options::OPT_mhard_float)) {
738 if (A->getOption().matches(options::OPT_msoft_float))
739 FloatABI = "soft";
740 else if (A->getOption().matches(options::OPT_mhard_float))
741 FloatABI = "hard";
742 }
743
744 // If unspecified, choose the default based on the platform.
745 if (FloatABI.empty()) {
Benjamin Kramerf41ccef2010-04-08 15:44:22 +0000746 // Assume "soft", but warn the user we are guessing.
747 FloatABI = "soft";
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000748 D.Diag(diag::warn_drv_assuming_mfloat_abi_is) << "soft";
Eric Christopher0b26a612010-03-02 02:41:08 +0000749 }
750
751 if (FloatABI == "soft") {
752 // Floating point operations and argument passing are soft.
753 //
754 // FIXME: This changes CPP defines, we need -target-soft-float.
755 CmdArgs.push_back("-msoft-float");
756 } else {
757 assert(FloatABI == "hard" && "Invalid float abi!");
758 CmdArgs.push_back("-mhard-float");
759 }
760}
761
Bruno Cardoso Lopese7f211c2010-11-09 17:21:19 +0000762void Clang::AddSparcTargetArgs(const ArgList &Args,
763 ArgStringList &CmdArgs) const {
764 const Driver &D = getToolChain().getDriver();
765
766 if (const Arg *A = Args.getLastArg(options::OPT_march_EQ)) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000767 StringRef MArch = A->getValue(Args);
Bruno Cardoso Lopese7f211c2010-11-09 17:21:19 +0000768 CmdArgs.push_back("-target-cpu");
769 CmdArgs.push_back(MArch.str().c_str());
770 }
771
772 // Select the float ABI as determined by -msoft-float, -mhard-float, and
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000773 StringRef FloatABI;
Bruno Cardoso Lopese7f211c2010-11-09 17:21:19 +0000774 if (Arg *A = Args.getLastArg(options::OPT_msoft_float,
775 options::OPT_mhard_float)) {
776 if (A->getOption().matches(options::OPT_msoft_float))
777 FloatABI = "soft";
778 else if (A->getOption().matches(options::OPT_mhard_float))
779 FloatABI = "hard";
780 }
781
782 // If unspecified, choose the default based on the platform.
783 if (FloatABI.empty()) {
784 switch (getToolChain().getTriple().getOS()) {
785 default:
786 // Assume "soft", but warn the user we are guessing.
787 FloatABI = "soft";
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000788 D.Diag(diag::warn_drv_assuming_mfloat_abi_is) << "soft";
Bruno Cardoso Lopese7f211c2010-11-09 17:21:19 +0000789 break;
790 }
791 }
792
793 if (FloatABI == "soft") {
794 // Floating point operations and argument passing are soft.
795 //
796 // FIXME: This changes CPP defines, we need -target-soft-float.
797 CmdArgs.push_back("-msoft-float");
Bruno Cardoso Lopese7f211c2010-11-09 17:21:19 +0000798 CmdArgs.push_back("-target-feature");
799 CmdArgs.push_back("+soft-float");
800 } else {
801 assert(FloatABI == "hard" && "Invalid float abi!");
802 CmdArgs.push_back("-mhard-float");
803 }
804}
805
Daniel Dunbar3b3191f2009-09-09 22:33:08 +0000806void Clang::AddX86TargetArgs(const ArgList &Args,
807 ArgStringList &CmdArgs) const {
Daniel Dunbare2cf8f72009-09-10 22:59:57 +0000808 if (!Args.hasFlag(options::OPT_mred_zone,
809 options::OPT_mno_red_zone,
810 true) ||
811 Args.hasArg(options::OPT_mkernel) ||
812 Args.hasArg(options::OPT_fapple_kext))
Daniel Dunbar8bed86c2009-11-20 22:21:36 +0000813 CmdArgs.push_back("-disable-red-zone");
Daniel Dunbare2cf8f72009-09-10 22:59:57 +0000814
Daniel Dunbare2cf8f72009-09-10 22:59:57 +0000815 if (Args.hasFlag(options::OPT_msoft_float,
816 options::OPT_mno_soft_float,
817 false))
Daniel Dunbar8bed86c2009-11-20 22:21:36 +0000818 CmdArgs.push_back("-no-implicit-float");
Daniel Dunbare2cf8f72009-09-10 22:59:57 +0000819
Daniel Dunbare13ada62009-11-14 22:04:54 +0000820 const char *CPUName = 0;
Daniel Dunbar3b3191f2009-09-09 22:33:08 +0000821 if (const Arg *A = Args.getLastArg(options::OPT_march_EQ)) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000822 if (StringRef(A->getValue(Args)) == "native") {
Daniel Dunbare13ada62009-11-14 22:04:54 +0000823 // FIXME: Reject attempts to use -march=native unless the target matches
824 // the host.
825 //
826 // FIXME: We should also incorporate the detected target features for use
827 // with -native.
828 std::string CPU = llvm::sys::getHostCPUName();
829 if (!CPU.empty())
830 CPUName = Args.MakeArgString(CPU);
831 } else
832 CPUName = A->getValue(Args);
833 }
Daniel Dunbar3b3191f2009-09-09 22:33:08 +0000834
Daniel Dunbare13ada62009-11-14 22:04:54 +0000835 // Select the default CPU if none was given (or detection failed).
836 if (!CPUName) {
Daniel Dunbar3b3191f2009-09-09 22:33:08 +0000837 // FIXME: Need target hooks.
Benjamin Kramer842bf172010-01-30 15:01:47 +0000838 if (getToolChain().getOS().startswith("darwin")) {
Daniel Dunbar116b3052011-05-31 15:58:55 +0000839 if (getToolChain().getArch() == llvm::Triple::x86_64)
Daniel Dunbare13ada62009-11-14 22:04:54 +0000840 CPUName = "core2";
Daniel Dunbar116b3052011-05-31 15:58:55 +0000841 else if (getToolChain().getArch() == llvm::Triple::x86)
Daniel Dunbare13ada62009-11-14 22:04:54 +0000842 CPUName = "yonah";
Chris Lattnerb986aba2010-04-11 19:29:39 +0000843 } else if (getToolChain().getOS().startswith("haiku")) {
Daniel Dunbar116b3052011-05-31 15:58:55 +0000844 if (getToolChain().getArch() == llvm::Triple::x86_64)
Chris Lattnerb986aba2010-04-11 19:29:39 +0000845 CPUName = "x86-64";
Daniel Dunbar116b3052011-05-31 15:58:55 +0000846 else if (getToolChain().getArch() == llvm::Triple::x86)
Chris Lattnerb986aba2010-04-11 19:29:39 +0000847 CPUName = "i586";
Daniel Dunbarea3813f2010-08-01 23:13:54 +0000848 } else if (getToolChain().getOS().startswith("openbsd")) {
Daniel Dunbar116b3052011-05-31 15:58:55 +0000849 if (getToolChain().getArch() == llvm::Triple::x86_64)
Daniel Dunbarea3813f2010-08-01 23:13:54 +0000850 CPUName = "x86-64";
Daniel Dunbar116b3052011-05-31 15:58:55 +0000851 else if (getToolChain().getArch() == llvm::Triple::x86)
Daniel Dunbarea3813f2010-08-01 23:13:54 +0000852 CPUName = "i486";
Roman Divacky432f10d2011-03-01 18:11:37 +0000853 } else if (getToolChain().getOS().startswith("freebsd")) {
Daniel Dunbar116b3052011-05-31 15:58:55 +0000854 if (getToolChain().getArch() == llvm::Triple::x86_64)
Roman Divacky432f10d2011-03-01 18:11:37 +0000855 CPUName = "x86-64";
Daniel Dunbar116b3052011-05-31 15:58:55 +0000856 else if (getToolChain().getArch() == llvm::Triple::x86)
Roman Divacky432f10d2011-03-01 18:11:37 +0000857 CPUName = "i486";
Benjamin Kramer24f1d3e2011-02-02 18:59:27 +0000858 } else if (getToolChain().getOS().startswith("netbsd")) {
Daniel Dunbar116b3052011-05-31 15:58:55 +0000859 if (getToolChain().getArch() == llvm::Triple::x86_64)
Benjamin Kramer24f1d3e2011-02-02 18:59:27 +0000860 CPUName = "x86-64";
Daniel Dunbar116b3052011-05-31 15:58:55 +0000861 else if (getToolChain().getArch() == llvm::Triple::x86)
Benjamin Kramer24f1d3e2011-02-02 18:59:27 +0000862 CPUName = "i486";
Daniel Dunbar3b3191f2009-09-09 22:33:08 +0000863 } else {
Daniel Dunbar116b3052011-05-31 15:58:55 +0000864 if (getToolChain().getArch() == llvm::Triple::x86_64)
Daniel Dunbare13ada62009-11-14 22:04:54 +0000865 CPUName = "x86-64";
Daniel Dunbar116b3052011-05-31 15:58:55 +0000866 else if (getToolChain().getArch() == llvm::Triple::x86)
Daniel Dunbare13ada62009-11-14 22:04:54 +0000867 CPUName = "pentium4";
Daniel Dunbar3b3191f2009-09-09 22:33:08 +0000868 }
869 }
870
Daniel Dunbare13ada62009-11-14 22:04:54 +0000871 if (CPUName) {
Daniel Dunbara7d02312009-12-18 06:30:12 +0000872 CmdArgs.push_back("-target-cpu");
Daniel Dunbare13ada62009-11-14 22:04:54 +0000873 CmdArgs.push_back(CPUName);
874 }
875
Eli Friedmanad811f02011-07-02 00:34:19 +0000876 // The required algorithm here is slightly strange: the options are applied
877 // in order (so -mno-sse -msse2 disables SSE3), but any option that gets
878 // directly overridden later is ignored (so "-mno-sse -msse2 -mno-sse2 -msse"
879 // is equivalent to "-mno-sse2 -msse"). The -cc1 handling deals with the
880 // former correctly, but not the latter; handle directly-overridden
881 // attributes here.
882 llvm::StringMap<unsigned> PrevFeature;
883 std::vector<const char*> Features;
Daniel Dunbar44b36ee2009-11-25 11:53:23 +0000884 for (arg_iterator it = Args.filtered_begin(options::OPT_m_x86_Features_Group),
885 ie = Args.filtered_end(); it != ie; ++it) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000886 StringRef Name = (*it)->getOption().getName();
Daniel Dunbara442fd52010-06-11 22:00:13 +0000887 (*it)->claim();
Daniel Dunbar3b3191f2009-09-09 22:33:08 +0000888
Daniel Dunbar44b36ee2009-11-25 11:53:23 +0000889 // Skip over "-m".
890 assert(Name.startswith("-m") && "Invalid feature name.");
891 Name = Name.substr(2);
Daniel Dunbar3b3191f2009-09-09 22:33:08 +0000892
Daniel Dunbar44b36ee2009-11-25 11:53:23 +0000893 bool IsNegative = Name.startswith("no-");
894 if (IsNegative)
895 Name = Name.substr(3);
Daniel Dunbar3b3191f2009-09-09 22:33:08 +0000896
Eli Friedmanad811f02011-07-02 00:34:19 +0000897 unsigned& Prev = PrevFeature[Name];
898 if (Prev)
899 Features[Prev - 1] = 0;
900 Prev = Features.size() + 1;
901 Features.push_back(Args.MakeArgString((IsNegative ? "-" : "+") + Name));
902 }
903 for (unsigned i = 0; i < Features.size(); i++) {
904 if (Features[i]) {
905 CmdArgs.push_back("-target-feature");
906 CmdArgs.push_back(Features[i]);
907 }
Daniel Dunbar3b3191f2009-09-09 22:33:08 +0000908 }
909}
910
Eric Christopher84fbdb42011-08-19 00:30:14 +0000911static bool
John McCallb5f652e2011-06-22 00:53:57 +0000912shouldUseExceptionTablesForObjCExceptions(unsigned objcABIVersion,
Anders Carlsson246ff3f2011-02-28 00:44:51 +0000913 const llvm::Triple &Triple) {
914 // We use the zero-cost exception tables for Objective-C if the non-fragile
915 // ABI is enabled or when compiling for x86_64 and ARM on Snow Leopard and
916 // later.
917
John McCallb5f652e2011-06-22 00:53:57 +0000918 if (objcABIVersion >= 2)
Anders Carlsson246ff3f2011-02-28 00:44:51 +0000919 return true;
920
921 if (Triple.getOS() != llvm::Triple::Darwin)
922 return false;
923
Eric Christopherbf15d2b2011-07-02 00:20:22 +0000924 return (!Triple.isMacOSXVersionLT(10,5) &&
Anders Carlsson246ff3f2011-02-28 00:44:51 +0000925 (Triple.getArch() == llvm::Triple::x86_64 ||
Eric Christopher84fbdb42011-08-19 00:30:14 +0000926 Triple.getArch() == llvm::Triple::arm));
Anders Carlsson246ff3f2011-02-28 00:44:51 +0000927}
928
Anders Carlssone96ab552011-02-28 02:27:16 +0000929/// addExceptionArgs - Adds exception related arguments to the driver command
930/// arguments. There's a master flag, -fexceptions and also language specific
931/// flags to enable/disable C++ and Objective-C exceptions.
932/// This makes it possible to for example disable C++ exceptions but enable
933/// Objective-C exceptions.
934static void addExceptionArgs(const ArgList &Args, types::ID InputType,
935 const llvm::Triple &Triple,
936 bool KernelOrKext, bool IsRewriter,
John McCallb5f652e2011-06-22 00:53:57 +0000937 unsigned objcABIVersion,
Anders Carlssone96ab552011-02-28 02:27:16 +0000938 ArgStringList &CmdArgs) {
939 if (KernelOrKext)
940 return;
941
942 // Exceptions are enabled by default.
943 bool ExceptionsEnabled = true;
944
945 // This keeps track of whether exceptions were explicitly turned on or off.
946 bool DidHaveExplicitExceptionFlag = false;
947
Rafael Espindola00a66572009-10-01 13:33:33 +0000948 if (Arg *A = Args.getLastArg(options::OPT_fexceptions,
949 options::OPT_fno_exceptions)) {
950 if (A->getOption().matches(options::OPT_fexceptions))
Anders Carlssone96ab552011-02-28 02:27:16 +0000951 ExceptionsEnabled = true;
Eric Christopher84fbdb42011-08-19 00:30:14 +0000952 else
Anders Carlssone96ab552011-02-28 02:27:16 +0000953 ExceptionsEnabled = false;
954
955 DidHaveExplicitExceptionFlag = true;
Rafael Espindola00a66572009-10-01 13:33:33 +0000956 }
Daniel Dunbar30a12b82010-09-14 23:12:31 +0000957
Anders Carlssone96ab552011-02-28 02:27:16 +0000958 bool ShouldUseExceptionTables = false;
Fariborz Jahaniane4b21ab2009-10-01 20:30:46 +0000959
Anders Carlssone96ab552011-02-28 02:27:16 +0000960 // Exception tables and cleanups can be enabled with -fexceptions even if the
961 // language itself doesn't support exceptions.
962 if (ExceptionsEnabled && DidHaveExplicitExceptionFlag)
963 ShouldUseExceptionTables = true;
Daniel Dunbar30a12b82010-09-14 23:12:31 +0000964
Daniel Dunbarc44f8cf2011-03-17 23:28:31 +0000965 // Obj-C exceptions are enabled by default, regardless of -fexceptions. This
966 // is not necessarily sensible, but follows GCC.
967 if (types::isObjC(InputType) &&
Eric Christopher84fbdb42011-08-19 00:30:14 +0000968 Args.hasFlag(options::OPT_fobjc_exceptions,
Daniel Dunbarc44f8cf2011-03-17 23:28:31 +0000969 options::OPT_fno_objc_exceptions,
970 true)) {
971 CmdArgs.push_back("-fobjc-exceptions");
Anders Carlssone96ab552011-02-28 02:27:16 +0000972
Eric Christopher84fbdb42011-08-19 00:30:14 +0000973 ShouldUseExceptionTables |=
John McCallb5f652e2011-06-22 00:53:57 +0000974 shouldUseExceptionTablesForObjCExceptions(objcABIVersion, Triple);
Anders Carlssone96ab552011-02-28 02:27:16 +0000975 }
976
977 if (types::isCXX(InputType)) {
978 bool CXXExceptionsEnabled = ExceptionsEnabled;
979
Eric Christopher84fbdb42011-08-19 00:30:14 +0000980 if (Arg *A = Args.getLastArg(options::OPT_fcxx_exceptions,
981 options::OPT_fno_cxx_exceptions,
Anders Carlssone96ab552011-02-28 02:27:16 +0000982 options::OPT_fexceptions,
983 options::OPT_fno_exceptions)) {
984 if (A->getOption().matches(options::OPT_fcxx_exceptions))
985 CXXExceptionsEnabled = true;
Chandler Carruth74f87112011-02-28 07:25:18 +0000986 else if (A->getOption().matches(options::OPT_fno_cxx_exceptions))
Anders Carlssone96ab552011-02-28 02:27:16 +0000987 CXXExceptionsEnabled = false;
988 }
989
990 if (CXXExceptionsEnabled) {
991 CmdArgs.push_back("-fcxx-exceptions");
992
993 ShouldUseExceptionTables = true;
994 }
995 }
996
997 if (ShouldUseExceptionTables)
998 CmdArgs.push_back("-fexceptions");
Rafael Espindola00a66572009-10-01 13:33:33 +0000999}
1000
Rafael Espindola4cfa7972011-05-02 17:43:32 +00001001static bool ShouldDisableCFI(const ArgList &Args,
1002 const ToolChain &TC) {
Rafael Espindolaf934f982011-05-17 16:26:17 +00001003 if (TC.getTriple().getOS() == llvm::Triple::Darwin) {
1004 // The native darwin assembler doesn't support cfi directives, so
Rafael Espindola35ab91c2011-05-17 19:06:58 +00001005 // we disable them if we think the .s file will be passed to it.
Rafael Espindola4cfa7972011-05-02 17:43:32 +00001006
Rafael Espindolaf934f982011-05-17 16:26:17 +00001007 // FIXME: Duplicated code with ToolChains.cpp
1008 // FIXME: This doesn't belong here, but ideally we will support static soon
1009 // anyway.
1010 bool HasStatic = (Args.hasArg(options::OPT_mkernel) ||
1011 Args.hasArg(options::OPT_static) ||
1012 Args.hasArg(options::OPT_fapple_kext));
1013 bool IsIADefault = TC.IsIntegratedAssemblerDefault() && !HasStatic;
1014 bool UseIntegratedAs = Args.hasFlag(options::OPT_integrated_as,
1015 options::OPT_no_integrated_as,
1016 IsIADefault);
1017 bool UseCFI = Args.hasFlag(options::OPT_fdwarf2_cfi_asm,
1018 options::OPT_fno_dwarf2_cfi_asm,
1019 UseIntegratedAs);
1020 return !UseCFI;
1021 }
1022
1023 // For now we assume that every other assembler support CFI.
1024 return false;
Rafael Espindola4cfa7972011-05-02 17:43:32 +00001025}
1026
Joerg Sonnenbergeref317a22011-05-06 14:35:16 +00001027/// \brief Check whether the given input tree contains any compilation actions.
1028static bool ContainsCompileAction(const Action *A) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001029 if (isa<CompileJobAction>(A))
Joerg Sonnenbergeref317a22011-05-06 14:35:16 +00001030 return true;
1031
1032 for (Action::const_iterator it = A->begin(), ie = A->end(); it != ie; ++it)
1033 if (ContainsCompileAction(*it))
1034 return true;
1035
1036 return false;
1037}
1038
1039/// \brief Check if -relax-all should be passed to the internal assembler.
1040/// This is done by default when compiling non-assembler source with -O0.
1041static bool UseRelaxAll(Compilation &C, const ArgList &Args) {
1042 bool RelaxDefault = true;
1043
1044 if (Arg *A = Args.getLastArg(options::OPT_O_Group))
1045 RelaxDefault = A->getOption().matches(options::OPT_O0);
1046
1047 if (RelaxDefault) {
1048 RelaxDefault = false;
1049 for (ActionList::const_iterator it = C.getActions().begin(),
1050 ie = C.getActions().end(); it != ie; ++it) {
1051 if (ContainsCompileAction(*it)) {
1052 RelaxDefault = true;
1053 break;
1054 }
1055 }
1056 }
1057
1058 return Args.hasFlag(options::OPT_mrelax_all, options::OPT_mno_relax_all,
1059 RelaxDefault);
1060}
1061
Daniel Dunbar1a093d22009-03-18 06:00:36 +00001062void Clang::ConstructJob(Compilation &C, const JobAction &JA,
Daniel Dunbar04c4c2c2009-03-18 07:06:02 +00001063 const InputInfo &Output,
Daniel Dunbar0450e6d2009-03-18 06:07:59 +00001064 const InputInfoList &Inputs,
Daniel Dunbara2aedc62009-03-18 10:01:51 +00001065 const ArgList &Args,
Daniel Dunbar1a093d22009-03-18 06:00:36 +00001066 const char *LinkingOutput) const {
Daniel Dunbare46b52a2010-03-20 04:52:14 +00001067 bool KernelOrKext = Args.hasArg(options::OPT_mkernel,
1068 options::OPT_fapple_kext);
Daniel Dunbar083edf72009-12-21 18:54:17 +00001069 const Driver &D = getToolChain().getDriver();
Daniel Dunbara3246a02009-03-18 08:07:30 +00001070 ArgStringList CmdArgs;
1071
Daniel Dunbare521a892009-03-31 20:53:55 +00001072 assert(Inputs.size() == 1 && "Unable to handle multiple inputs.");
1073
Daniel Dunbar6c536aa2009-12-11 23:00:49 +00001074 // Invoke ourselves in -cc1 mode.
1075 //
1076 // FIXME: Implement custom jobs for internal actions.
1077 CmdArgs.push_back("-cc1");
1078
Daniel Dunbar624c21b2009-10-30 18:12:20 +00001079 // Add the "effective" target triple.
Daniel Dunbard640be22009-03-31 17:35:15 +00001080 CmdArgs.push_back("-triple");
Daniel Dunbar82eb4ce2010-08-23 22:35:37 +00001081 std::string TripleStr = getToolChain().ComputeEffectiveClangTriple(Args);
Daniel Dunbar624c21b2009-10-30 18:12:20 +00001082 CmdArgs.push_back(Args.MakeArgString(TripleStr));
Daniel Dunbarfb58b0a2009-09-10 06:49:20 +00001083
Daniel Dunbar624c21b2009-10-30 18:12:20 +00001084 // Select the appropriate action.
Daniel Dunbar99b55242010-07-19 19:44:22 +00001085 bool IsRewriter = false;
Daniel Dunbara2aedc62009-03-18 10:01:51 +00001086 if (isa<AnalyzeJobAction>(JA)) {
1087 assert(JA.getType() == types::TY_Plist && "Invalid output type.");
1088 CmdArgs.push_back("-analyze");
1089 } else if (isa<PreprocessJobAction>(JA)) {
Daniel Dunbard67a3222009-03-30 06:36:42 +00001090 if (Output.getType() == types::TY_Dependencies)
1091 CmdArgs.push_back("-Eonly");
1092 else
1093 CmdArgs.push_back("-E");
Daniel Dunbarc4343942010-02-03 03:07:56 +00001094 } else if (isa<AssembleJobAction>(JA)) {
1095 CmdArgs.push_back("-emit-obj");
Daniel Dunbar06e2cc32010-05-27 06:18:05 +00001096
Joerg Sonnenbergeref317a22011-05-06 14:35:16 +00001097 if (UseRelaxAll(C, Args))
Daniel Dunbar06e2cc32010-05-27 06:18:05 +00001098 CmdArgs.push_back("-mrelax-all");
Daniel Dunbarfe6c97b2010-08-24 16:47:49 +00001099
Daniel Dunbar2b4de142010-10-18 22:36:15 +00001100 // When using an integrated assembler, translate -Wa, and -Xassembler
1101 // options.
1102 for (arg_iterator it = Args.filtered_begin(options::OPT_Wa_COMMA,
1103 options::OPT_Xassembler),
1104 ie = Args.filtered_end(); it != ie; ++it) {
1105 const Arg *A = *it;
1106 A->claim();
1107
1108 for (unsigned i = 0, e = A->getNumValues(); i != e; ++i) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001109 StringRef Value = A->getValue(Args, i);
Daniel Dunbar2b4de142010-10-18 22:36:15 +00001110
1111 if (Value == "-force_cpusubtype_ALL") {
1112 // Do nothing, this is the default and we don't support anything else.
Daniel Dunbara78e5892010-10-28 20:36:23 +00001113 } else if (Value == "-L") {
Daniel Dunbar67919b22011-03-28 22:49:28 +00001114 CmdArgs.push_back("-msave-temp-labels");
Joerg Sonnenberger3028e462011-05-19 20:46:39 +00001115 } else if (Value == "--fatal-warnings") {
Joerg Sonnenbergerb487d2d2011-05-19 18:42:29 +00001116 CmdArgs.push_back("-mllvm");
1117 CmdArgs.push_back("-fatal-assembler-warnings");
Nick Lewyckyca6b90d2011-06-21 00:14:18 +00001118 } else if (Value == "--noexecstack") {
1119 CmdArgs.push_back("-mnoexecstack");
Daniel Dunbar2b4de142010-10-18 22:36:15 +00001120 } else {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001121 D.Diag(diag::err_drv_unsupported_option_argument)
Daniel Dunbar2b4de142010-10-18 22:36:15 +00001122 << A->getOption().getName() << Value;
1123 }
1124 }
1125 }
Daniel Dunbar7c874332010-11-19 16:23:35 +00001126
1127 // Also ignore explicit -force_cpusubtype_ALL option.
1128 (void) Args.hasArg(options::OPT_force__cpusubtype__ALL);
Daniel Dunbara2aedc62009-03-18 10:01:51 +00001129 } else if (isa<PrecompileJobAction>(JA)) {
Argyrios Kyrtzidis90bdfbb2010-08-11 23:27:58 +00001130 // Use PCH if the user requested it.
Daniel Dunbarcbc34b72009-10-15 20:02:44 +00001131 bool UsePCH = D.CCCUsePCH;
Daniel Dunbarcbc34b72009-10-15 20:02:44 +00001132
1133 if (UsePCH)
Douglas Gregor111af7d2009-04-18 00:34:01 +00001134 CmdArgs.push_back("-emit-pch");
1135 else
1136 CmdArgs.push_back("-emit-pth");
Daniel Dunbara2aedc62009-03-18 10:01:51 +00001137 } else {
1138 assert(isa<CompileJobAction>(JA) && "Invalid action for clang tool.");
Daniel Dunbard067f7f2009-04-08 23:54:23 +00001139
Daniel Dunbara2aedc62009-03-18 10:01:51 +00001140 if (JA.getType() == types::TY_Nothing) {
1141 CmdArgs.push_back("-fsyntax-only");
Daniel Dunbar24e52992010-06-07 23:28:45 +00001142 } else if (JA.getType() == types::TY_LLVM_IR ||
1143 JA.getType() == types::TY_LTO_IR) {
Daniel Dunbara2aedc62009-03-18 10:01:51 +00001144 CmdArgs.push_back("-emit-llvm");
Daniel Dunbar24e52992010-06-07 23:28:45 +00001145 } else if (JA.getType() == types::TY_LLVM_BC ||
1146 JA.getType() == types::TY_LTO_BC) {
Daniel Dunbara2aedc62009-03-18 10:01:51 +00001147 CmdArgs.push_back("-emit-llvm-bc");
1148 } else if (JA.getType() == types::TY_PP_Asm) {
Daniel Dunbard112f102009-09-17 00:47:53 +00001149 CmdArgs.push_back("-S");
Daniel Dunbar6cdf83c2009-09-01 16:57:46 +00001150 } else if (JA.getType() == types::TY_AST) {
1151 CmdArgs.push_back("-emit-pch");
Daniel Dunbaraeea8ac2010-02-11 03:16:21 +00001152 } else if (JA.getType() == types::TY_RewrittenObjC) {
1153 CmdArgs.push_back("-rewrite-objc");
Daniel Dunbar99b55242010-07-19 19:44:22 +00001154 IsRewriter = true;
Daniel Dunbaraeea8ac2010-02-11 03:16:21 +00001155 } else {
1156 assert(JA.getType() == types::TY_PP_Asm &&
1157 "Unexpected output type!");
Daniel Dunbara2aedc62009-03-18 10:01:51 +00001158 }
Daniel Dunbara3246a02009-03-18 08:07:30 +00001159 }
1160
Daniel Dunbara2aedc62009-03-18 10:01:51 +00001161 // The make clang go fast button.
1162 CmdArgs.push_back("-disable-free");
1163
John McCallbb79b5f2010-02-13 03:50:24 +00001164 // Disable the verification pass in -asserts builds.
1165#ifdef NDEBUG
1166 CmdArgs.push_back("-disable-llvm-verifier");
1167#endif
1168
Daniel Dunbar3b358a32009-04-08 05:11:16 +00001169 // Set the main file name, so that debug info works even with
1170 // -save-temps.
1171 CmdArgs.push_back("-main-file-name");
1172 CmdArgs.push_back(darwin::CC1::getBaseInputName(Args, Inputs));
1173
Daniel Dunbar17ddaa62009-04-08 18:03:55 +00001174 // Some flags which affect the language (via preprocessor
1175 // defines). See darwin::CC1::AddCPPArgs.
1176 if (Args.hasArg(options::OPT_static))
1177 CmdArgs.push_back("-static-define");
1178
Daniel Dunbara2aedc62009-03-18 10:01:51 +00001179 if (isa<AnalyzeJobAction>(JA)) {
Ted Kremenek05e6f5b2009-09-25 05:55:59 +00001180 // Enable region store model by default.
1181 CmdArgs.push_back("-analyzer-store=region");
1182
Ted Kremenek7bea9a12009-12-07 22:26:14 +00001183 // Treat blocks as analysis entry points.
1184 CmdArgs.push_back("-analyzer-opt-analyze-nested-blocks");
1185
Ted Kremenek49c79792011-03-24 00:28:47 +00001186 CmdArgs.push_back("-analyzer-eagerly-assume");
1187
Daniel Dunbara2aedc62009-03-18 10:01:51 +00001188 // Add default argument set.
Daniel Dunbar58f345ce2009-05-22 00:38:15 +00001189 if (!Args.hasArg(options::OPT__analyzer_no_default_checks)) {
Argyrios Kyrtzidisa6d04d52011-02-15 07:42:33 +00001190 CmdArgs.push_back("-analyzer-checker=core");
Ted Kremenek49c79792011-03-24 00:28:47 +00001191 CmdArgs.push_back("-analyzer-checker=deadcode");
1192 CmdArgs.push_back("-analyzer-checker=security");
1193
Argyrios Kyrtzidisa6d04d52011-02-15 07:42:33 +00001194 if (getToolChain().getTriple().getOS() != llvm::Triple::Win32)
1195 CmdArgs.push_back("-analyzer-checker=unix");
Ted Kremenek49c79792011-03-24 00:28:47 +00001196
Argyrios Kyrtzidisa6d04d52011-02-15 07:42:33 +00001197 if (getToolChain().getTriple().getVendor() == llvm::Triple::Apple)
Ted Kremenek49c79792011-03-24 00:28:47 +00001198 CmdArgs.push_back("-analyzer-checker=osx");
Daniel Dunbar58f345ce2009-05-22 00:38:15 +00001199 }
Daniel Dunbard067f7f2009-04-08 23:54:23 +00001200
Daniel Dunbar58f345ce2009-05-22 00:38:15 +00001201 // Set the output format. The default is plist, for (lame) historical
1202 // reasons.
1203 CmdArgs.push_back("-analyzer-output");
1204 if (Arg *A = Args.getLastArg(options::OPT__analyzer_output))
1205 CmdArgs.push_back(A->getValue(Args));
1206 else
1207 CmdArgs.push_back("plist");
Daniel Dunbara2aedc62009-03-18 10:01:51 +00001208
Ted Kremenekfe449a22010-03-22 22:32:05 +00001209 // Disable the presentation of standard compiler warnings when
1210 // using --analyze. We only want to show static analyzer diagnostics
1211 // or frontend errors.
1212 CmdArgs.push_back("-w");
1213
Daniel Dunbara2aedc62009-03-18 10:01:51 +00001214 // Add -Xanalyzer arguments when running as analyzer.
1215 Args.AddAllArgValues(CmdArgs, options::OPT_Xanalyzer);
Mike Stump11289f42009-09-09 15:08:12 +00001216 }
1217
Daniel Dunbar4eadb602009-09-10 01:21:12 +00001218 CheckCodeGenerationOptions(D, Args);
1219
Daniel Dunbar44e71222009-04-29 18:32:25 +00001220 // Perform argument translation for LLVM backend. This
1221 // takes some care in reconciling with llvm-gcc. The
1222 // issue is that llvm-gcc translates these options based on
1223 // the values in cc1, whereas we are processing based on
1224 // the driver arguments.
Daniel Dunbard067f7f2009-04-08 23:54:23 +00001225
Daniel Dunbar44e71222009-04-29 18:32:25 +00001226 // This comes from the default translation the driver + cc1
1227 // would do to enable flag_pic.
1228 //
1229 // FIXME: Centralize this code.
1230 bool PICEnabled = (Args.hasArg(options::OPT_fPIC) ||
1231 Args.hasArg(options::OPT_fpic) ||
1232 Args.hasArg(options::OPT_fPIE) ||
1233 Args.hasArg(options::OPT_fpie));
1234 bool PICDisabled = (Args.hasArg(options::OPT_mkernel) ||
1235 Args.hasArg(options::OPT_static));
1236 const char *Model = getToolChain().GetForcedPicModel();
1237 if (!Model) {
1238 if (Args.hasArg(options::OPT_mdynamic_no_pic))
1239 Model = "dynamic-no-pic";
1240 else if (PICDisabled)
1241 Model = "static";
1242 else if (PICEnabled)
1243 Model = "pic";
Daniel Dunbara2aedc62009-03-18 10:01:51 +00001244 else
Daniel Dunbar44e71222009-04-29 18:32:25 +00001245 Model = getToolChain().GetDefaultRelocationModel();
Daniel Dunbara2aedc62009-03-18 10:01:51 +00001246 }
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001247 if (StringRef(Model) != "pic") {
Daniel Dunbara1b02a22009-11-29 07:18:39 +00001248 CmdArgs.push_back("-mrelocation-model");
1249 CmdArgs.push_back(Model);
1250 }
Daniel Dunbar44e71222009-04-29 18:32:25 +00001251
1252 // Infer the __PIC__ value.
1253 //
1254 // FIXME: This isn't quite right on Darwin, which always sets
1255 // __PIC__=2.
1256 if (strcmp(Model, "pic") == 0 || strcmp(Model, "dynamic-no-pic") == 0) {
Daniel Dunbar5fe08662009-11-29 02:39:08 +00001257 CmdArgs.push_back("-pic-level");
1258 CmdArgs.push_back(Args.hasArg(options::OPT_fPIC) ? "2" : "1");
Daniel Dunbar44e71222009-04-29 18:32:25 +00001259 }
Tanya Lattnerf9d41df2009-11-04 01:18:09 +00001260 if (!Args.hasFlag(options::OPT_fmerge_all_constants,
1261 options::OPT_fno_merge_all_constants))
Chris Lattner9242b332011-04-08 18:06:54 +00001262 CmdArgs.push_back("-fno-merge-all-constants");
Daniel Dunbar306945d2009-09-16 06:17:29 +00001263
Daniel Dunbara1b02a22009-11-29 07:18:39 +00001264 // LLVM Code Generator Options.
1265
Daniel Dunbar0bb03312011-02-09 17:54:19 +00001266 if (Arg *A = Args.getLastArg(options::OPT_mregparm_EQ)) {
1267 CmdArgs.push_back("-mregparm");
1268 CmdArgs.push_back(A->getValue(Args));
1269 }
1270
Roman Divacky65b88cd2011-03-01 17:40:53 +00001271 if (Args.hasFlag(options::OPT_mrtd, options::OPT_mno_rtd, false))
1272 CmdArgs.push_back("-mrtd");
1273
Daniel Dunbara1b02a22009-11-29 07:18:39 +00001274 // FIXME: Set --enable-unsafe-fp-math.
1275 if (Args.hasFlag(options::OPT_fno_omit_frame_pointer,
1276 options::OPT_fomit_frame_pointer))
1277 CmdArgs.push_back("-mdisable-fp-elim");
1278 if (!Args.hasFlag(options::OPT_fzero_initialized_in_bss,
1279 options::OPT_fno_zero_initialized_in_bss))
1280 CmdArgs.push_back("-mno-zero-initialized-in-bss");
Daniel Dunbar7aa71f92011-02-04 02:20:39 +00001281 if (!Args.hasFlag(options::OPT_fstrict_aliasing,
1282 options::OPT_fno_strict_aliasing,
1283 getToolChain().IsStrictAliasingDefault()))
Dan Gohman10169b92010-10-14 22:36:56 +00001284 CmdArgs.push_back("-relaxed-aliasing");
Daniel Dunbar0d8ca9e2010-05-14 22:00:22 +00001285
1286 // Decide whether to use verbose asm. Verbose assembly is the default on
1287 // toolchains which have the integrated assembler on by default.
1288 bool IsVerboseAsmDefault = getToolChain().IsIntegratedAssemblerDefault();
1289 if (Args.hasFlag(options::OPT_fverbose_asm, options::OPT_fno_verbose_asm,
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001290 IsVerboseAsmDefault) ||
Daniel Dunbar0d8ca9e2010-05-14 22:00:22 +00001291 Args.hasArg(options::OPT_dA))
Daniel Dunbara1b02a22009-11-29 07:18:39 +00001292 CmdArgs.push_back("-masm-verbose");
Daniel Dunbar0d8ca9e2010-05-14 22:00:22 +00001293
Daniel Dunbara1b02a22009-11-29 07:18:39 +00001294 if (Args.hasArg(options::OPT_fdebug_pass_structure)) {
1295 CmdArgs.push_back("-mdebug-pass");
1296 CmdArgs.push_back("Structure");
1297 }
1298 if (Args.hasArg(options::OPT_fdebug_pass_arguments)) {
1299 CmdArgs.push_back("-mdebug-pass");
1300 CmdArgs.push_back("Arguments");
1301 }
1302
John McCall8517abc2010-02-19 02:45:38 +00001303 // Enable -mconstructor-aliases except on darwin, where we have to
1304 // work around a linker bug; see <rdar://problem/7651567>.
1305 if (getToolChain().getTriple().getOS() != llvm::Triple::Darwin)
1306 CmdArgs.push_back("-mconstructor-aliases");
NAKAMURA Takumi029d74b2011-02-17 08:50:50 +00001307
John McCall7ef5cb32011-03-18 02:56:14 +00001308 // Darwin's kernel doesn't support guard variables; just die if we
1309 // try to use them.
1310 if (KernelOrKext &&
1311 getToolChain().getTriple().getOS() == llvm::Triple::Darwin)
1312 CmdArgs.push_back("-fforbid-guard-variables");
1313
Douglas Gregordbe39272011-02-01 15:15:22 +00001314 if (Args.hasArg(options::OPT_mms_bitfields)) {
1315 CmdArgs.push_back("-mms-bitfields");
1316 }
John McCall8517abc2010-02-19 02:45:38 +00001317
Daniel Dunbar306945d2009-09-16 06:17:29 +00001318 // This is a coarse approximation of what llvm-gcc actually does, both
1319 // -fasynchronous-unwind-tables and -fnon-call-exceptions interact in more
1320 // complicated ways.
1321 bool AsynchronousUnwindTables =
1322 Args.hasFlag(options::OPT_fasynchronous_unwind_tables,
1323 options::OPT_fno_asynchronous_unwind_tables,
1324 getToolChain().IsUnwindTablesDefault() &&
Daniel Dunbare46b52a2010-03-20 04:52:14 +00001325 !KernelOrKext);
Daniel Dunbar306945d2009-09-16 06:17:29 +00001326 if (Args.hasFlag(options::OPT_funwind_tables, options::OPT_fno_unwind_tables,
1327 AsynchronousUnwindTables))
Daniel Dunbara1b02a22009-11-29 07:18:39 +00001328 CmdArgs.push_back("-munwind-tables");
1329
1330 if (Arg *A = Args.getLastArg(options::OPT_flimited_precision_EQ)) {
1331 CmdArgs.push_back("-mlimit-float-precision");
1332 CmdArgs.push_back(A->getValue(Args));
1333 }
Daniel Dunbar44e71222009-04-29 18:32:25 +00001334
Daniel Dunbar4dbaaa62009-05-06 03:16:41 +00001335 // FIXME: Handle -mtune=.
1336 (void) Args.hasArg(options::OPT_mtune_EQ);
Daniel Dunbar44e71222009-04-29 18:32:25 +00001337
Benjamin Kramercf4371a2009-08-05 14:30:52 +00001338 if (Arg *A = Args.getLastArg(options::OPT_mcmodel_EQ)) {
Daniel Dunbara1b02a22009-11-29 07:18:39 +00001339 CmdArgs.push_back("-mcode-model");
Benjamin Kramercf4371a2009-08-05 14:30:52 +00001340 CmdArgs.push_back(A->getValue(Args));
1341 }
1342
Daniel Dunbar3b3191f2009-09-09 22:33:08 +00001343 // Add target specific cpu and features flags.
1344 switch(getToolChain().getTriple().getArch()) {
1345 default:
1346 break;
Daniel Dunbar4dbaaa62009-05-06 03:16:41 +00001347
Daniel Dunbar0f5c5422009-09-10 04:57:17 +00001348 case llvm::Triple::arm:
1349 case llvm::Triple::thumb:
Daniel Dunbarc9388c12011-03-17 17:10:06 +00001350 AddARMTargetArgs(Args, CmdArgs, KernelOrKext);
Daniel Dunbar0f5c5422009-09-10 04:57:17 +00001351 break;
1352
Eric Christopher0b26a612010-03-02 02:41:08 +00001353 case llvm::Triple::mips:
1354 case llvm::Triple::mipsel:
1355 AddMIPSTargetArgs(Args, CmdArgs);
1356 break;
1357
Bruno Cardoso Lopese7f211c2010-11-09 17:21:19 +00001358 case llvm::Triple::sparc:
1359 AddSparcTargetArgs(Args, CmdArgs);
1360 break;
1361
Daniel Dunbar3b3191f2009-09-09 22:33:08 +00001362 case llvm::Triple::x86:
1363 case llvm::Triple::x86_64:
1364 AddX86TargetArgs(Args, CmdArgs);
1365 break;
Daniel Dunbar44e71222009-04-29 18:32:25 +00001366 }
1367
Daniel Dunbar976a2f52010-08-11 23:07:47 +00001368 // Pass the linker version in use.
1369 if (Arg *A = Args.getLastArg(options::OPT_mlinker_version_EQ)) {
1370 CmdArgs.push_back("-target-linker-version");
1371 CmdArgs.push_back(A->getValue(Args));
1372 }
1373
Nick Lewycky75033772011-02-02 06:43:03 +00001374 // -mno-omit-leaf-frame-pointer is the default on Darwin.
Daniel Dunbarbb7ac522010-07-01 01:31:45 +00001375 if (Args.hasFlag(options::OPT_momit_leaf_frame_pointer,
Nick Lewycky75033772011-02-02 06:43:03 +00001376 options::OPT_mno_omit_leaf_frame_pointer,
1377 getToolChain().getTriple().getOS() != llvm::Triple::Darwin))
Daniel Dunbarbb7ac522010-07-01 01:31:45 +00001378 CmdArgs.push_back("-momit-leaf-frame-pointer");
1379
Dan Gohmand1e76b92010-01-08 02:20:44 +00001380 // -fno-math-errno is default.
1381 if (Args.hasFlag(options::OPT_fmath_errno,
Daniel Dunbar44e71222009-04-29 18:32:25 +00001382 options::OPT_fno_math_errno,
Dan Gohmand1e76b92010-01-08 02:20:44 +00001383 false))
1384 CmdArgs.push_back("-fmath-errno");
Daniel Dunbar44e71222009-04-29 18:32:25 +00001385
Daniel Dunbarfcc49a82010-05-12 18:19:58 +00001386 // Explicitly error on some things we know we don't support and can't just
1387 // ignore.
1388 types::ID InputType = Inputs[0].getType();
Daniel Dunbar4ed214a2010-09-24 19:39:37 +00001389 if (!Args.hasArg(options::OPT_fallow_unsupported)) {
1390 Arg *Unsupported;
Peter Collingbourne77b0e7f22011-07-12 19:35:15 +00001391 if ((Unsupported = Args.getLastArg(options::OPT_iframework)))
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001392 D.Diag(diag::err_drv_clang_unsupported)
Daniel Dunbarfcc49a82010-05-12 18:19:58 +00001393 << Unsupported->getOption().getName();
Daniel Dunbar4ed214a2010-09-24 19:39:37 +00001394
1395 if (types::isCXX(InputType) &&
1396 getToolChain().getTriple().getOS() == llvm::Triple::Darwin &&
1397 getToolChain().getTriple().getArch() == llvm::Triple::x86) {
Bob Wilson0d45f582011-08-13 23:48:55 +00001398 if ((Unsupported = Args.getLastArg(options::OPT_fapple_kext)) ||
1399 (Unsupported = Args.getLastArg(options::OPT_mkernel)))
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001400 D.Diag(diag::err_drv_clang_unsupported_opt_cxx_darwin_i386)
Daniel Dunbar4ed214a2010-09-24 19:39:37 +00001401 << Unsupported->getOption().getName();
1402 }
Daniel Dunbarfcc49a82010-05-12 18:19:58 +00001403 }
1404
Daniel Dunbara2aedc62009-03-18 10:01:51 +00001405 Args.AddAllArgs(CmdArgs, options::OPT_v);
Daniel Dunbard4352752010-08-24 22:44:13 +00001406 Args.AddLastArg(CmdArgs, options::OPT_H);
Chad Rosierbe10f982011-08-02 17:58:04 +00001407 if (D.CCPrintHeaders && !D.CCGenDiagnostics) {
Daniel Dunbarac540b32011-02-02 21:11:35 +00001408 CmdArgs.push_back("-header-include-file");
1409 CmdArgs.push_back(D.CCPrintHeadersFilename ?
1410 D.CCPrintHeadersFilename : "-");
1411 }
Daniel Dunbara2aedc62009-03-18 10:01:51 +00001412 Args.AddLastArg(CmdArgs, options::OPT_P);
Mike Stump11289f42009-09-09 15:08:12 +00001413 Args.AddLastArg(CmdArgs, options::OPT_print_ivar_layout);
Daniel Dunbara2aedc62009-03-18 10:01:51 +00001414
Chad Rosierbe10f982011-08-02 17:58:04 +00001415 if (D.CCLogDiagnostics && !D.CCGenDiagnostics) {
Daniel Dunbar529c03b2011-04-07 18:01:20 +00001416 CmdArgs.push_back("-diagnostic-log-file");
1417 CmdArgs.push_back(D.CCLogDiagnosticsFilename ?
1418 D.CCLogDiagnosticsFilename : "-");
1419 }
1420
Daniel Dunbara2aedc62009-03-18 10:01:51 +00001421 // Special case debug options to only pass -g to clang. This is
1422 // wrong.
Rafael Espindola08a692a2010-03-07 04:46:18 +00001423 Args.ClaimAllArgs(options::OPT_g_Group);
Daniel Dunbar4083d042010-05-12 18:19:55 +00001424 if (Arg *A = Args.getLastArg(options::OPT_g_Group))
1425 if (!A->getOption().matches(options::OPT_g0))
1426 CmdArgs.push_back("-g");
Daniel Dunbara2aedc62009-03-18 10:01:51 +00001427
Rafael Espindola66bfb2752010-05-06 21:06:04 +00001428 Args.AddAllArgs(CmdArgs, options::OPT_ffunction_sections);
1429 Args.AddAllArgs(CmdArgs, options::OPT_fdata_sections);
1430
Chris Lattner3c77a352010-06-22 00:03:40 +00001431 Args.AddAllArgs(CmdArgs, options::OPT_finstrument_functions);
1432
Nick Lewycky207bce32011-04-21 23:44:07 +00001433 if (Args.hasArg(options::OPT_ftest_coverage) ||
1434 Args.hasArg(options::OPT_coverage))
1435 CmdArgs.push_back("-femit-coverage-notes");
1436 if (Args.hasArg(options::OPT_fprofile_arcs) ||
1437 Args.hasArg(options::OPT_coverage))
1438 CmdArgs.push_back("-femit-coverage-data");
1439
Nick Lewycky480cb992011-05-04 20:46:58 +00001440 if (C.getArgs().hasArg(options::OPT_c) ||
1441 C.getArgs().hasArg(options::OPT_S)) {
1442 if (Output.isFilename()) {
Nick Lewycky85c011d2011-05-05 00:08:20 +00001443 CmdArgs.push_back("-coverage-file");
1444 CmdArgs.push_back(Args.MakeArgString(Output.getFilename()));
Nick Lewycky480cb992011-05-04 20:46:58 +00001445 }
1446 }
1447
Daniel Dunbara2aedc62009-03-18 10:01:51 +00001448 Args.AddLastArg(CmdArgs, options::OPT_nostdinc);
Douglas Gregor64b046f2010-03-24 20:13:48 +00001449 Args.AddLastArg(CmdArgs, options::OPT_nostdincxx);
Rafael Espindolab3549d72009-10-26 13:36:57 +00001450 Args.AddLastArg(CmdArgs, options::OPT_nobuiltininc);
Daniel Dunbara2aedc62009-03-18 10:01:51 +00001451
Daniel Dunbar34e0b8c2009-12-15 01:02:52 +00001452 // Pass the path to compiler resource files.
Daniel Dunbar34e0b8c2009-12-15 01:02:52 +00001453 CmdArgs.push_back("-resource-dir");
Daniel Dunbar3f3e2cd2010-01-20 02:35:16 +00001454 CmdArgs.push_back(D.ResourceDir.c_str());
Daniel Dunbar9dc82a22009-04-07 21:42:00 +00001455
Argyrios Kyrtzidis71731d62010-11-03 22:45:23 +00001456 Args.AddLastArg(CmdArgs, options::OPT_working_directory);
1457
John McCalld70fb982011-06-15 23:25:17 +00001458 if (!Args.hasArg(options::OPT_fno_objc_arc)) {
Argyrios Kyrtzidisc44b93d2011-07-07 04:00:39 +00001459 if (const Arg *A = Args.getLastArg(options::OPT_ccc_arcmt_check,
Argyrios Kyrtzidis7fbd97f2011-07-09 20:00:58 +00001460 options::OPT_ccc_arcmt_modify,
1461 options::OPT_ccc_arcmt_migrate)) {
John McCalld70fb982011-06-15 23:25:17 +00001462 switch (A->getOption().getID()) {
1463 default:
1464 llvm_unreachable("missed a case");
Argyrios Kyrtzidisc44b93d2011-07-07 04:00:39 +00001465 case options::OPT_ccc_arcmt_check:
John McCalld70fb982011-06-15 23:25:17 +00001466 CmdArgs.push_back("-arcmt-check");
1467 break;
Argyrios Kyrtzidisc44b93d2011-07-07 04:00:39 +00001468 case options::OPT_ccc_arcmt_modify:
John McCalld70fb982011-06-15 23:25:17 +00001469 CmdArgs.push_back("-arcmt-modify");
1470 break;
Argyrios Kyrtzidis7fbd97f2011-07-09 20:00:58 +00001471 case options::OPT_ccc_arcmt_migrate:
1472 CmdArgs.push_back("-arcmt-migrate");
1473 CmdArgs.push_back("-arcmt-migrate-directory");
1474 CmdArgs.push_back(A->getValue(Args));
Argyrios Kyrtzidisd5713632011-07-19 17:20:03 +00001475
1476 Args.AddLastArg(CmdArgs, options::OPT_arcmt_migrate_report_output);
1477 Args.AddLastArg(CmdArgs, options::OPT_arcmt_migrate_emit_arc_errors);
Argyrios Kyrtzidis7fbd97f2011-07-09 20:00:58 +00001478 break;
John McCalld70fb982011-06-15 23:25:17 +00001479 }
1480 }
1481 }
Eric Christopher84fbdb42011-08-19 00:30:14 +00001482
Daniel Dunbard067f7f2009-04-08 23:54:23 +00001483 // Add preprocessing options like -I, -D, etc. if we are using the
1484 // preprocessor.
1485 //
1486 // FIXME: Support -fpreprocessed
Daniel Dunbard067f7f2009-04-08 23:54:23 +00001487 if (types::getPreprocessedType(InputType) != types::TY_INVALID)
Douglas Gregor111af7d2009-04-18 00:34:01 +00001488 AddPreprocessingOptions(D, Args, CmdArgs, Output, Inputs);
Daniel Dunbara2aedc62009-03-18 10:01:51 +00001489
Rafael Espindolaa7431922011-07-21 23:40:37 +00001490 // Don't warn about "clang -c -DPIC -fPIC test.i" because libtool.m4 assumes
1491 // that "The compiler can only warn and ignore the option if not recognized".
1492 // When building with ccache, it will pass -D options to clang even on
1493 // preprocessed inputs and configure concludes that -fPIC is not supported.
1494 Args.ClaimAllArgs(options::OPT_D);
1495
Daniel Dunbar58f78332009-09-17 06:53:36 +00001496 // Manually translate -O to -O2 and -O4 to -O3; let clang reject
Daniel Dunbar13864952009-03-24 20:17:30 +00001497 // others.
1498 if (Arg *A = Args.getLastArg(options::OPT_O_Group)) {
Daniel Dunbar0bfb21e2009-11-19 03:26:40 +00001499 if (A->getOption().matches(options::OPT_O4))
Daniel Dunbar13864952009-03-24 20:17:30 +00001500 CmdArgs.push_back("-O3");
Daniel Dunbar9296f632010-05-27 06:51:08 +00001501 else if (A->getOption().matches(options::OPT_O) &&
1502 A->getValue(Args)[0] == '\0')
Daniel Dunbar58f78332009-09-17 06:53:36 +00001503 CmdArgs.push_back("-O2");
Daniel Dunbara2aedc62009-03-18 10:01:51 +00001504 else
Daniel Dunbar7ef5ed62009-03-18 23:39:35 +00001505 A->render(Args, CmdArgs);
Daniel Dunbara2aedc62009-03-18 10:01:51 +00001506 }
1507
Daniel Dunbar945577c2009-10-29 02:24:45 +00001508 Args.AddAllArgs(CmdArgs, options::OPT_W_Group);
1509 Args.AddLastArg(CmdArgs, options::OPT_pedantic);
1510 Args.AddLastArg(CmdArgs, options::OPT_pedantic_errors);
Daniel Dunbara2aedc62009-03-18 10:01:51 +00001511 Args.AddLastArg(CmdArgs, options::OPT_w);
Daniel Dunbarc44b4cc2009-04-07 22:13:21 +00001512
1513 // Handle -{std, ansi, trigraphs} -- take the last of -{std, ansi}
1514 // (-ansi is equivalent to -std=c89).
1515 //
1516 // If a std is supplied, only add -trigraphs if it follows the
1517 // option.
1518 if (Arg *Std = Args.getLastArg(options::OPT_std_EQ, options::OPT_ansi)) {
1519 if (Std->getOption().matches(options::OPT_ansi))
Nuno Lopes275225d2009-10-16 14:28:06 +00001520 if (types::isCXX(InputType))
Daniel Dunbar1c7577c2009-11-04 06:24:38 +00001521 CmdArgs.push_back("-std=c++98");
Nuno Lopes275225d2009-10-16 14:28:06 +00001522 else
Daniel Dunbar1c7577c2009-11-04 06:24:38 +00001523 CmdArgs.push_back("-std=c89");
Daniel Dunbarc44b4cc2009-04-07 22:13:21 +00001524 else
1525 Std->render(Args, CmdArgs);
1526
Daniel Dunbar3f1a1ff2010-06-14 21:23:08 +00001527 if (Arg *A = Args.getLastArg(options::OPT_std_EQ, options::OPT_ansi,
1528 options::OPT_trigraphs))
1529 if (A != Std)
Daniel Dunbarc44b4cc2009-04-07 22:13:21 +00001530 A->render(Args, CmdArgs);
Daniel Dunbar72a60902009-04-26 01:10:38 +00001531 } else {
1532 // Honor -std-default.
Daniel Dunbar12998192010-01-29 21:03:02 +00001533 //
1534 // FIXME: Clang doesn't correctly handle -std= when the input language
1535 // doesn't match. For the time being just ignore this for C++ inputs;
1536 // eventually we want to do all the standard defaulting here instead of
1537 // splitting it between the driver and clang -cc1.
1538 if (!types::isCXX(InputType))
1539 Args.AddAllArgsTranslated(CmdArgs, options::OPT_std_default_EQ,
1540 "-std=", /*Joined=*/true);
Daniel Dunbarc44b4cc2009-04-07 22:13:21 +00001541 Args.AddLastArg(CmdArgs, options::OPT_trigraphs);
Daniel Dunbar72a60902009-04-26 01:10:38 +00001542 }
Daniel Dunbard067f7f2009-04-08 23:54:23 +00001543
Chandler Carruthb009b142011-04-23 06:30:43 +00001544 // Map the bizarre '-Wwrite-strings' flag to a more sensible
1545 // '-fconst-strings'; this better indicates its actual behavior.
1546 if (Args.hasFlag(options::OPT_Wwrite_strings, options::OPT_Wno_write_strings,
1547 false)) {
1548 // For perfect compatibility with GCC, we do this even in the presence of
1549 // '-w'. This flag names something other than a warning for GCC.
1550 CmdArgs.push_back("-fconst-strings");
1551 }
1552
Chandler Carruth61fbf622011-04-23 09:27:53 +00001553 // GCC provides a macro definition '__DEPRECATED' when -Wdeprecated is active
Chandler Carruth30483fb2011-04-23 19:48:40 +00001554 // during C++ compilation, which it is by default. GCC keeps this define even
1555 // in the presence of '-w', match this behavior bug-for-bug.
1556 if (types::isCXX(InputType) &&
1557 Args.hasFlag(options::OPT_Wdeprecated, options::OPT_Wno_deprecated,
1558 true)) {
1559 CmdArgs.push_back("-fdeprecated-macro");
Chandler Carruth61fbf622011-04-23 09:27:53 +00001560 }
1561
Chandler Carruthe0391482010-05-22 02:21:53 +00001562 // Translate GCC's misnamer '-fasm' arguments to '-fgnu-keywords'.
1563 if (Arg *Asm = Args.getLastArg(options::OPT_fasm, options::OPT_fno_asm)) {
1564 if (Asm->getOption().matches(options::OPT_fasm))
1565 CmdArgs.push_back("-fgnu-keywords");
1566 else
1567 CmdArgs.push_back("-fno-gnu-keywords");
1568 }
1569
Rafael Espindola4cfa7972011-05-02 17:43:32 +00001570 if (ShouldDisableCFI(Args, getToolChain()))
1571 CmdArgs.push_back("-fno-dwarf2-cfi-asm");
Rafael Espindolae2641872011-04-30 18:35:43 +00001572
Daniel Dunbara2aedc62009-03-18 10:01:51 +00001573 if (Arg *A = Args.getLastArg(options::OPT_ftemplate_depth_)) {
1574 CmdArgs.push_back("-ftemplate-depth");
1575 CmdArgs.push_back(A->getValue(Args));
1576 }
1577
Argyrios Kyrtzidisef6c8da2010-11-18 00:20:36 +00001578 if (Arg *A = Args.getLastArg(options::OPT_Wlarge_by_value_copy_EQ,
1579 options::OPT_Wlarge_by_value_copy_def)) {
1580 CmdArgs.push_back("-Wlarge-by-value-copy");
Argyrios Kyrtzidisaf84ec02010-11-17 23:11:54 +00001581 if (A->getNumValues())
1582 CmdArgs.push_back(A->getValue(Args));
1583 else
Argyrios Kyrtzidisef6c8da2010-11-18 00:20:36 +00001584 CmdArgs.push_back("64"); // default value for -Wlarge-by-value-copy.
Argyrios Kyrtzidisaf84ec02010-11-17 23:11:54 +00001585 }
1586
Daniel Dunbarfffd1812009-11-19 04:00:53 +00001587 if (Args.hasArg(options::OPT__relocatable_pch))
Daniel Dunbar8bed86c2009-11-20 22:21:36 +00001588 CmdArgs.push_back("-relocatable-pch");
Mike Stump11289f42009-09-09 15:08:12 +00001589
Daniel Dunbar1c7577c2009-11-04 06:24:38 +00001590 if (Arg *A = Args.getLastArg(options::OPT_fconstant_string_class_EQ)) {
1591 CmdArgs.push_back("-fconstant-string-class");
1592 CmdArgs.push_back(A->getValue(Args));
1593 }
David Chisnall5778fce2009-08-31 16:41:57 +00001594
Chris Lattnere23003d2010-01-09 21:54:33 +00001595 if (Arg *A = Args.getLastArg(options::OPT_ftabstop_EQ)) {
1596 CmdArgs.push_back("-ftabstop");
1597 CmdArgs.push_back(A->getValue(Args));
1598 }
1599
Chris Lattnerb35583d2010-04-07 20:49:23 +00001600 CmdArgs.push_back("-ferror-limit");
1601 if (Arg *A = Args.getLastArg(options::OPT_ferror_limit_EQ))
1602 CmdArgs.push_back(A->getValue(Args));
1603 else
1604 CmdArgs.push_back("19");
Douglas Gregorffed1cb2010-04-20 07:18:24 +00001605
Chandler Carrutha77a7272010-05-06 04:55:18 +00001606 if (Arg *A = Args.getLastArg(options::OPT_fmacro_backtrace_limit_EQ)) {
1607 CmdArgs.push_back("-fmacro-backtrace-limit");
Douglas Gregorcd121fb2010-05-04 17:13:42 +00001608 CmdArgs.push_back(A->getValue(Args));
Chandler Carrutha77a7272010-05-06 04:55:18 +00001609 }
1610
1611 if (Arg *A = Args.getLastArg(options::OPT_ftemplate_backtrace_limit_EQ)) {
1612 CmdArgs.push_back("-ftemplate-backtrace-limit");
Douglas Gregorffed1cb2010-04-20 07:18:24 +00001613 CmdArgs.push_back(A->getValue(Args));
Chandler Carrutha77a7272010-05-06 04:55:18 +00001614 }
1615
Daniel Dunbar2c978472009-11-04 06:24:47 +00001616 // Pass -fmessage-length=.
Daniel Dunbar84bb7932009-11-30 08:40:54 +00001617 CmdArgs.push_back("-fmessage-length");
Daniel Dunbar2c978472009-11-04 06:24:47 +00001618 if (Arg *A = Args.getLastArg(options::OPT_fmessage_length_EQ)) {
Daniel Dunbar84bb7932009-11-30 08:40:54 +00001619 CmdArgs.push_back(A->getValue(Args));
Daniel Dunbar2c978472009-11-04 06:24:47 +00001620 } else {
1621 // If -fmessage-length=N was not specified, determine whether this is a
1622 // terminal and, if so, implicitly define -fmessage-length appropriately.
1623 unsigned N = llvm::sys::Process::StandardErrColumns();
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001624 CmdArgs.push_back(Args.MakeArgString(Twine(N)));
Daniel Dunbar2c978472009-11-04 06:24:47 +00001625 }
1626
Daniel Dunbare357d562009-12-03 18:42:11 +00001627 if (const Arg *A = Args.getLastArg(options::OPT_fvisibility_EQ)) {
1628 CmdArgs.push_back("-fvisibility");
1629 CmdArgs.push_back(A->getValue(Args));
1630 }
1631
Douglas Gregor08329632010-06-15 17:05:35 +00001632 Args.AddLastArg(CmdArgs, options::OPT_fvisibility_inlines_hidden);
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001633
Daniel Dunbare46b52a2010-03-20 04:52:14 +00001634 // -fhosted is default.
1635 if (KernelOrKext || Args.hasFlag(options::OPT_ffreestanding,
1636 options::OPT_fhosted,
1637 false))
1638 CmdArgs.push_back("-ffreestanding");
1639
Daniel Dunbare357d562009-12-03 18:42:11 +00001640 // Forward -f (flag) options which we can pass directly.
Mike Stumpd9546382009-12-12 01:27:46 +00001641 Args.AddLastArg(CmdArgs, options::OPT_fcatch_undefined_behavior);
Daniel Dunbar3a148f22009-04-07 21:51:40 +00001642 Args.AddLastArg(CmdArgs, options::OPT_femit_all_decls);
Daniel Dunbar3a148f22009-04-07 21:51:40 +00001643 Args.AddLastArg(CmdArgs, options::OPT_fheinous_gnu_extensions);
Devang Patel91bbb552010-09-30 19:05:55 +00001644 Args.AddLastArg(CmdArgs, options::OPT_flimit_debug_info);
Daniel Dunbar733b0f82011-03-01 18:49:30 +00001645 if (getToolChain().SupportsProfiling())
1646 Args.AddLastArg(CmdArgs, options::OPT_pg);
Daniel Dunbar35621a92010-03-16 16:57:46 +00001647
1648 // -flax-vector-conversions is default.
1649 if (!Args.hasFlag(options::OPT_flax_vector_conversions,
1650 options::OPT_fno_lax_vector_conversions))
1651 CmdArgs.push_back("-fno-lax-vector-conversions");
1652
Fariborz Jahaniana4cfff82011-01-07 01:05:02 +00001653 if (Args.getLastArg(options::OPT_fapple_kext))
1654 CmdArgs.push_back("-fapple-kext");
1655
Fariborz Jahaniana4404f22009-05-22 20:17:16 +00001656 Args.AddLastArg(CmdArgs, options::OPT_fobjc_sender_dependent_dispatch);
Chris Lattner69686412009-04-21 05:34:31 +00001657 Args.AddLastArg(CmdArgs, options::OPT_fdiagnostics_print_source_range_info);
Douglas Gregoreec975c2010-08-19 20:24:43 +00001658 Args.AddLastArg(CmdArgs, options::OPT_fdiagnostics_parseable_fixits);
Daniel Dunbar3a148f22009-04-07 21:51:40 +00001659 Args.AddLastArg(CmdArgs, options::OPT_ftime_report);
1660 Args.AddLastArg(CmdArgs, options::OPT_ftrapv);
David Chisnalldd84ef12010-09-17 18:29:54 +00001661
1662 if (Arg *A = Args.getLastArg(options::OPT_ftrapv_handler_EQ)) {
1663 CmdArgs.push_back("-ftrapv-handler");
1664 CmdArgs.push_back(A->getValue(Args));
1665 }
1666
Evan Cheng04c94292011-04-08 21:37:45 +00001667 // Forward -ftrap_function= options to the backend.
1668 if (Arg *A = Args.getLastArg(options::OPT_ftrap_function_EQ)) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001669 StringRef FuncName = A->getValue(Args);
Evan Cheng04c94292011-04-08 21:37:45 +00001670 CmdArgs.push_back("-backend-option");
1671 CmdArgs.push_back(Args.MakeArgString("-trap-func=" + FuncName));
1672 }
1673
Chandler Carruth6e501032011-03-27 00:04:55 +00001674 // -fno-strict-overflow implies -fwrapv if it isn't disabled, but
1675 // -fstrict-overflow won't turn off an explicitly enabled -fwrapv.
1676 if (Arg *A = Args.getLastArg(options::OPT_fwrapv,
1677 options::OPT_fno_wrapv)) {
1678 if (A->getOption().matches(options::OPT_fwrapv))
1679 CmdArgs.push_back("-fwrapv");
1680 } else if (Arg *A = Args.getLastArg(options::OPT_fstrict_overflow,
1681 options::OPT_fno_strict_overflow)) {
1682 if (A->getOption().matches(options::OPT_fno_strict_overflow))
1683 CmdArgs.push_back("-fwrapv");
1684 }
Daniel Dunbar3a148f22009-04-07 21:51:40 +00001685 Args.AddLastArg(CmdArgs, options::OPT_fwritable_strings);
Eric Christopherf387dbd2010-08-07 23:08:14 +00001686 Args.AddLastArg(CmdArgs, options::OPT_funroll_loops);
Daniel Dunbara2aedc62009-03-18 10:01:51 +00001687
Daniel Dunbara77eaeb2009-09-03 04:54:28 +00001688 Args.AddLastArg(CmdArgs, options::OPT_pthread);
1689
Daniel Dunbar4930e332009-11-17 08:07:36 +00001690 // -stack-protector=0 is default.
1691 unsigned StackProtectorLevel = 0;
Bill Wendlingd63bbad2009-06-28 07:36:13 +00001692 if (Arg *A = Args.getLastArg(options::OPT_fno_stack_protector,
1693 options::OPT_fstack_protector_all,
1694 options::OPT_fstack_protector)) {
Daniel Dunbar4930e332009-11-17 08:07:36 +00001695 if (A->getOption().matches(options::OPT_fstack_protector))
1696 StackProtectorLevel = 1;
1697 else if (A->getOption().matches(options::OPT_fstack_protector_all))
1698 StackProtectorLevel = 2;
Nico Weberdd473632011-08-23 07:38:27 +00001699 } else {
1700 StackProtectorLevel =
1701 getToolChain().GetDefaultStackProtectorLevel(KernelOrKext);
1702 }
Daniel Dunbar4930e332009-11-17 08:07:36 +00001703 if (StackProtectorLevel) {
1704 CmdArgs.push_back("-stack-protector");
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001705 CmdArgs.push_back(Args.MakeArgString(Twine(StackProtectorLevel)));
Bill Wendlingd63bbad2009-06-28 07:36:13 +00001706 }
1707
Eric Christopherd5c45f62011-05-02 21:18:22 +00001708 // Translate -mstackrealign
1709 if (Args.hasArg(options::OPT_mstackrealign)) {
1710 CmdArgs.push_back("-backend-option");
1711 CmdArgs.push_back("-force-align-stack");
1712 }
Eric Christopher84fbdb42011-08-19 00:30:14 +00001713
Daniel Dunbard18049a2009-04-07 21:16:11 +00001714 // Forward -f options with positive and negative forms; we translate
1715 // these by hand.
1716
Fariborz Jahaniana4cfff82011-01-07 01:05:02 +00001717 if (Args.hasArg(options::OPT_mkernel)) {
Daniel Dunbar80f787c2011-02-04 17:24:47 +00001718 if (!Args.hasArg(options::OPT_fapple_kext) && types::isCXX(InputType))
Fariborz Jahaniana4cfff82011-01-07 01:05:02 +00001719 CmdArgs.push_back("-fapple-kext");
1720 if (!Args.hasArg(options::OPT_fbuiltin))
1721 CmdArgs.push_back("-fno-builtin");
1722 }
Daniel Dunbar4930e332009-11-17 08:07:36 +00001723 // -fbuiltin is default.
Fariborz Jahaniana4cfff82011-01-07 01:05:02 +00001724 else if (!Args.hasFlag(options::OPT_fbuiltin, options::OPT_fno_builtin))
Daniel Dunbar484afa22009-11-19 04:55:23 +00001725 CmdArgs.push_back("-fno-builtin");
Daniel Dunbard18049a2009-04-07 21:16:11 +00001726
Nuno Lopes13c88c72009-12-16 16:59:22 +00001727 if (!Args.hasFlag(options::OPT_fassume_sane_operator_new,
1728 options::OPT_fno_assume_sane_operator_new))
1729 CmdArgs.push_back("-fno-assume-sane-operator-new");
1730
Daniel Dunbar4930e332009-11-17 08:07:36 +00001731 // -fblocks=0 is default.
1732 if (Args.hasFlag(options::OPT_fblocks, options::OPT_fno_blocks,
David Chisnallda209912011-02-28 17:11:43 +00001733 getToolChain().IsBlocksDefault()) ||
1734 (Args.hasArg(options::OPT_fgnu_runtime) &&
1735 Args.hasArg(options::OPT_fobjc_nonfragile_abi) &&
1736 !Args.hasArg(options::OPT_fno_blocks))) {
Daniel Dunbar4930e332009-11-17 08:07:36 +00001737 CmdArgs.push_back("-fblocks");
John McCall7959fee2011-09-09 20:41:01 +00001738
1739 if (!Args.hasArg(options::OPT_fgnu_runtime) &&
1740 !getToolChain().hasBlocksRuntime())
1741 CmdArgs.push_back("-fblocks-runtime-optional");
David Chisnall950a9512009-11-17 19:33:30 +00001742 }
Daniel Dunbard18049a2009-04-07 21:16:11 +00001743
John McCalldfea9982010-04-09 19:12:06 +00001744 // -faccess-control is default.
John McCall3155f572010-04-09 19:03:51 +00001745 if (Args.hasFlag(options::OPT_fno_access_control,
1746 options::OPT_faccess_control,
John McCalldfea9982010-04-09 19:12:06 +00001747 false))
John McCall3155f572010-04-09 19:03:51 +00001748 CmdArgs.push_back("-fno-access-control");
John McCall59bb1d42010-03-17 01:32:13 +00001749
Anders Carlssond470fef2010-11-21 00:09:52 +00001750 // -felide-constructors is the default.
1751 if (Args.hasFlag(options::OPT_fno_elide_constructors,
1752 options::OPT_felide_constructors,
1753 false))
1754 CmdArgs.push_back("-fno-elide-constructors");
1755
Daniel Dunbar0730e4f2009-11-17 07:06:20 +00001756 // -frtti is default.
Daniel Dunbare46b52a2010-03-20 04:52:14 +00001757 if (KernelOrKext ||
1758 !Args.hasFlag(options::OPT_frtti, options::OPT_fno_rtti))
Daniel Dunbar484afa22009-11-19 04:55:23 +00001759 CmdArgs.push_back("-fno-rtti");
Mike Stump183c3d22009-07-31 23:15:31 +00001760
Argyrios Kyrtzidis74825bc2010-10-08 00:25:19 +00001761 // -fshort-enums=0 is default.
1762 // FIXME: Are there targers where -fshort-enums is on by default ?
1763 if (Args.hasFlag(options::OPT_fshort_enums,
1764 options::OPT_fno_short_enums, false))
1765 CmdArgs.push_back("-fshort-enums");
1766
Daniel Dunbard609b7b2009-11-17 06:37:03 +00001767 // -fsigned-char is default.
Daniel Dunbar5bdd2992009-11-25 10:14:30 +00001768 if (!Args.hasFlag(options::OPT_fsigned_char, options::OPT_funsigned_char,
Daniel Dunbard609b7b2009-11-17 06:37:03 +00001769 isSignedCharDefault(getToolChain().getTriple())))
Daniel Dunbar5fe08662009-11-29 02:39:08 +00001770 CmdArgs.push_back("-fno-signed-char");
Eli Friedman327f0b52009-06-05 07:21:14 +00001771
Anders Carlssonfcd764a2010-02-06 23:23:06 +00001772 // -fthreadsafe-static is default.
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001773 if (!Args.hasFlag(options::OPT_fthreadsafe_statics,
Anders Carlssonfcd764a2010-02-06 23:23:06 +00001774 options::OPT_fno_threadsafe_statics))
1775 CmdArgs.push_back("-fno-threadsafe-statics");
1776
Daniel Dunbarfe06df42010-03-20 04:15:41 +00001777 // -fuse-cxa-atexit is default.
Anton Korobeynikov82b33332010-09-11 11:17:06 +00001778 if (KernelOrKext ||
1779 !Args.hasFlag(options::OPT_fuse_cxa_atexit, options::OPT_fno_use_cxa_atexit,
NAKAMURA Takumi6bdc8a22010-10-10 01:53:03 +00001780 getToolChain().getTriple().getOS() != llvm::Triple::Cygwin &&
NAKAMURA Takumi31ea2f12011-02-17 08:51:38 +00001781 getToolChain().getTriple().getOS() != llvm::Triple::MinGW32))
Daniel Dunbarfe06df42010-03-20 04:15:41 +00001782 CmdArgs.push_back("-fno-use-cxa-atexit");
1783
Daniel Dunbar0730e4f2009-11-17 07:06:20 +00001784 // -fms-extensions=0 is default.
Daniel Dunbar5bdd2992009-11-25 10:14:30 +00001785 if (Args.hasFlag(options::OPT_fms_extensions, options::OPT_fno_ms_extensions,
Daniel Dunbar0730e4f2009-11-17 07:06:20 +00001786 getToolChain().getTriple().getOS() == llvm::Triple::Win32))
1787 CmdArgs.push_back("-fms-extensions");
1788
Michael J. Spencer4992ca4b2010-10-21 05:21:48 +00001789 // -fmsc-version=1300 is default.
1790 if (Args.hasFlag(options::OPT_fms_extensions, options::OPT_fno_ms_extensions,
1791 getToolChain().getTriple().getOS() == llvm::Triple::Win32) ||
1792 Args.hasArg(options::OPT_fmsc_version)) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001793 StringRef msc_ver = Args.getLastArgValue(options::OPT_fmsc_version);
Michael J. Spencer4992ca4b2010-10-21 05:21:48 +00001794 if (msc_ver.empty())
1795 CmdArgs.push_back("-fmsc-version=1300");
1796 else
1797 CmdArgs.push_back(Args.MakeArgString("-fmsc-version=" + msc_ver));
1798 }
1799
1800
Dawn Perchik68bb1b42010-09-02 23:59:25 +00001801 // -fborland-extensions=0 is default.
1802 if (Args.hasFlag(options::OPT_fborland_extensions,
1803 options::OPT_fno_borland_extensions, false))
1804 CmdArgs.push_back("-fborland-extensions");
1805
Francois Pichet02744872011-09-01 16:38:08 +00001806 // -fno-delayed-template-parsing is default, except for Windows where MSVC STL
1807 // needs it.
Francois Pichet1c229c02011-04-22 22:18:13 +00001808 if (Args.hasFlag(options::OPT_fdelayed_template_parsing,
1809 options::OPT_fno_delayed_template_parsing,
Francois Pichet02744872011-09-01 16:38:08 +00001810 getToolChain().getTriple().getOS() == llvm::Triple::Win32))
Francois Pichet35bc5de2011-08-26 00:22:34 +00001811 CmdArgs.push_back("-fdelayed-template-parsing");
Francois Pichet1c229c02011-04-22 22:18:13 +00001812
Chandler Carruthe03aa552010-04-17 20:17:31 +00001813 // -fgnu-keywords default varies depending on language; only pass if
1814 // specified.
1815 if (Arg *A = Args.getLastArg(options::OPT_fgnu_keywords,
Daniel Dunbardb059592010-04-24 17:56:39 +00001816 options::OPT_fno_gnu_keywords))
1817 A->render(Args, CmdArgs);
Chandler Carruthe03aa552010-04-17 20:17:31 +00001818
Rafael Espindola922a6242011-06-02 17:30:53 +00001819 if (Args.hasFlag(options::OPT_fgnu89_inline,
1820 options::OPT_fno_gnu89_inline,
1821 false))
Rafael Espindolafb2af642011-06-02 16:13:27 +00001822 CmdArgs.push_back("-fgnu89-inline");
1823
Daniel Dunbar4930e332009-11-17 08:07:36 +00001824 // -fobjc-nonfragile-abi=0 is default.
John McCall24fc0de2011-07-06 00:26:06 +00001825 ObjCRuntime objCRuntime;
John McCallb5f652e2011-06-22 00:53:57 +00001826 unsigned objcABIVersion = 0;
Daniel Dunbar4930e332009-11-17 08:07:36 +00001827 if (types::isObjC(InputType)) {
John McCall24fc0de2011-07-06 00:26:06 +00001828 bool NeXTRuntimeIsDefault
1829 = (IsRewriter || getToolChain().getTriple().isOSDarwin());
1830 if (Args.hasFlag(options::OPT_fnext_runtime, options::OPT_fgnu_runtime,
John McCall75bc7732011-07-06 02:36:30 +00001831 NeXTRuntimeIsDefault)) {
John McCall24fc0de2011-07-06 00:26:06 +00001832 objCRuntime.setKind(ObjCRuntime::NeXT);
John McCall75bc7732011-07-06 02:36:30 +00001833 } else {
1834 CmdArgs.push_back("-fgnu-runtime");
John McCall24fc0de2011-07-06 00:26:06 +00001835 objCRuntime.setKind(ObjCRuntime::GNU);
John McCall75bc7732011-07-06 02:36:30 +00001836 }
John McCall24fc0de2011-07-06 00:26:06 +00001837 getToolChain().configureObjCRuntime(objCRuntime);
1838 if (objCRuntime.HasARC)
1839 CmdArgs.push_back("-fobjc-runtime-has-arc");
1840 if (objCRuntime.HasWeak)
1841 CmdArgs.push_back("-fobjc-runtime-has-weak");
John McCall9de19782011-07-06 01:22:26 +00001842 if (objCRuntime.HasTerminate)
1843 CmdArgs.push_back("-fobjc-runtime-has-terminate");
John McCall24fc0de2011-07-06 00:26:06 +00001844
Daniel Dunbarc1dd0e92010-09-20 18:19:55 +00001845 // Compute the Objective-C ABI "version" to use. Version numbers are
1846 // slightly confusing for historical reasons:
1847 // 1 - Traditional "fragile" ABI
1848 // 2 - Non-fragile ABI, version 1
1849 // 3 - Non-fragile ABI, version 2
John McCallb5f652e2011-06-22 00:53:57 +00001850 objcABIVersion = 1;
Daniel Dunbarc1dd0e92010-09-20 18:19:55 +00001851 // If -fobjc-abi-version= is present, use that to set the version.
Daniel Dunbar12c82082010-04-28 23:25:24 +00001852 if (Arg *A = Args.getLastArg(options::OPT_fobjc_abi_version_EQ)) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001853 if (StringRef(A->getValue(Args)) == "1")
John McCallb5f652e2011-06-22 00:53:57 +00001854 objcABIVersion = 1;
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001855 else if (StringRef(A->getValue(Args)) == "2")
John McCallb5f652e2011-06-22 00:53:57 +00001856 objcABIVersion = 2;
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001857 else if (StringRef(A->getValue(Args)) == "3")
John McCallb5f652e2011-06-22 00:53:57 +00001858 objcABIVersion = 3;
Daniel Dunbar12c82082010-04-28 23:25:24 +00001859 else
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001860 D.Diag(diag::err_drv_clang_unsupported) << A->getAsString(Args);
Daniel Dunbarc1dd0e92010-09-20 18:19:55 +00001861 } else {
1862 // Otherwise, determine if we are using the non-fragile ABI.
1863 if (Args.hasFlag(options::OPT_fobjc_nonfragile_abi,
1864 options::OPT_fno_objc_nonfragile_abi,
1865 getToolChain().IsObjCNonFragileABIDefault())) {
1866 // Determine the non-fragile ABI version to use.
Daniel Dunbaraeed5fe2010-11-11 16:08:59 +00001867#ifdef DISABLE_DEFAULT_NONFRAGILEABI_TWO
1868 unsigned NonFragileABIVersion = 1;
1869#else
Daniel Dunbarc1dd0e92010-09-20 18:19:55 +00001870 unsigned NonFragileABIVersion = 2;
Daniel Dunbaraeed5fe2010-11-11 16:08:59 +00001871#endif
Daniel Dunbarc1dd0e92010-09-20 18:19:55 +00001872
1873 if (Arg *A = Args.getLastArg(
1874 options::OPT_fobjc_nonfragile_abi_version_EQ)) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001875 if (StringRef(A->getValue(Args)) == "1")
Daniel Dunbarc1dd0e92010-09-20 18:19:55 +00001876 NonFragileABIVersion = 1;
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001877 else if (StringRef(A->getValue(Args)) == "2")
Daniel Dunbarc1dd0e92010-09-20 18:19:55 +00001878 NonFragileABIVersion = 2;
1879 else
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001880 D.Diag(diag::err_drv_clang_unsupported)
Daniel Dunbarc1dd0e92010-09-20 18:19:55 +00001881 << A->getAsString(Args);
1882 }
1883
John McCallb5f652e2011-06-22 00:53:57 +00001884 objcABIVersion = 1 + NonFragileABIVersion;
Daniel Dunbarc1dd0e92010-09-20 18:19:55 +00001885 } else {
John McCallb5f652e2011-06-22 00:53:57 +00001886 objcABIVersion = 1;
Daniel Dunbarc1dd0e92010-09-20 18:19:55 +00001887 }
Daniel Dunbar12c82082010-04-28 23:25:24 +00001888 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001889
John McCallb5f652e2011-06-22 00:53:57 +00001890 if (objcABIVersion == 2 || objcABIVersion == 3) {
Fariborz Jahanian3aa19e9a2011-01-04 20:05:20 +00001891 CmdArgs.push_back("-fobjc-nonfragile-abi");
Daniel Dunbarfca18c1b42010-04-24 17:56:46 +00001892
1893 // -fobjc-dispatch-method is only relevant with the nonfragile-abi, and
1894 // legacy is the default.
1895 if (!Args.hasFlag(options::OPT_fobjc_legacy_dispatch,
1896 options::OPT_fno_objc_legacy_dispatch,
1897 getToolChain().IsObjCLegacyDispatchDefault())) {
1898 if (getToolChain().UseObjCMixedDispatch())
1899 CmdArgs.push_back("-fobjc-dispatch-method=mixed");
1900 else
1901 CmdArgs.push_back("-fobjc-dispatch-method=non-legacy");
1902 }
Daniel Dunbar2e3f2c82010-02-01 21:07:25 +00001903 }
NAKAMURA Takumi029d74b2011-02-17 08:50:50 +00001904
Ted Kremenek1d56c9e2010-12-23 21:35:43 +00001905 // -fobjc-default-synthesize-properties=0 is default.
1906 if (Args.hasFlag(options::OPT_fobjc_default_synthesize_properties,
1907 options::OPT_fno_objc_default_synthesize_properties,
1908 getToolChain().IsObjCDefaultSynthPropertiesDefault())) {
1909 CmdArgs.push_back("-fobjc-default-synthesize-properties");
1910 }
Daniel Dunbar4930e332009-11-17 08:07:36 +00001911 }
1912
John McCall24fc0de2011-07-06 00:26:06 +00001913 // Allow -fno-objc-arr to trump -fobjc-arr/-fobjc-arc.
1914 // NOTE: This logic is duplicated in ToolChains.cpp.
1915 bool ARC = isObjCAutoRefCount(Args);
1916 if (ARC) {
1917 CmdArgs.push_back("-fobjc-arc");
1918
1919 // Allow the user to enable full exceptions code emission.
1920 // We define off for Objective-CC, on for Objective-C++.
1921 if (Args.hasFlag(options::OPT_fobjc_arc_exceptions,
1922 options::OPT_fno_objc_arc_exceptions,
1923 /*default*/ types::isCXX(InputType)))
1924 CmdArgs.push_back("-fobjc-arc-exceptions");
1925 }
1926
1927 // -fobjc-infer-related-result-type is the default, except in the Objective-C
1928 // rewriter.
1929 if (IsRewriter)
1930 CmdArgs.push_back("-fno-objc-infer-related-result-type");
Eric Christopher84fbdb42011-08-19 00:30:14 +00001931
John McCall24fc0de2011-07-06 00:26:06 +00001932 // Handle -fobjc-gc and -fobjc-gc-only. They are exclusive, and -fobjc-gc-only
1933 // takes precedence.
1934 const Arg *GCArg = Args.getLastArg(options::OPT_fobjc_gc_only);
1935 if (!GCArg)
1936 GCArg = Args.getLastArg(options::OPT_fobjc_gc);
1937 if (GCArg) {
1938 if (ARC) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001939 D.Diag(diag::err_drv_objc_gc_arr)
John McCall24fc0de2011-07-06 00:26:06 +00001940 << GCArg->getAsString(Args);
1941 } else if (getToolChain().SupportsObjCGC()) {
1942 GCArg->render(Args, CmdArgs);
1943 } else {
1944 // FIXME: We should move this to a hard error.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001945 D.Diag(diag::warn_drv_objc_gc_unsupported)
John McCall24fc0de2011-07-06 00:26:06 +00001946 << GCArg->getAsString(Args);
1947 }
1948 }
1949
John McCallb5f652e2011-06-22 00:53:57 +00001950 // Add exception args.
1951 addExceptionArgs(Args, InputType, getToolChain().getTriple(),
1952 KernelOrKext, IsRewriter, objcABIVersion, CmdArgs);
1953
1954 if (getToolChain().UseSjLjExceptions())
1955 CmdArgs.push_back("-fsjlj-exceptions");
1956
1957 // C++ "sane" operator new.
Daniel Dunbar2e3f2c82010-02-01 21:07:25 +00001958 if (!Args.hasFlag(options::OPT_fassume_sane_operator_new,
1959 options::OPT_fno_assume_sane_operator_new))
1960 CmdArgs.push_back("-fno-assume-sane-operator-new");
1961
Daniel Dunbar34d7a992010-04-27 15:34:57 +00001962 // -fconstant-cfstrings is default, and may be subject to argument translation
1963 // on Darwin.
1964 if (!Args.hasFlag(options::OPT_fconstant_cfstrings,
1965 options::OPT_fno_constant_cfstrings) ||
1966 !Args.hasFlag(options::OPT_mconstant_cfstrings,
1967 options::OPT_mno_constant_cfstrings))
1968 CmdArgs.push_back("-fno-constant-cfstrings");
1969
John Thompsoned4e2952009-11-05 20:14:16 +00001970 // -fshort-wchar default varies depending on platform; only
1971 // pass if specified.
Daniel Dunbar2cb4e7a2010-04-27 15:35:03 +00001972 if (Arg *A = Args.getLastArg(options::OPT_fshort_wchar))
1973 A->render(Args, CmdArgs);
John Thompsoned4e2952009-11-05 20:14:16 +00001974
Daniel Dunbar1a8a2e82009-10-29 02:39:57 +00001975 // -fno-pascal-strings is default, only pass non-default. If the tool chain
1976 // happened to translate to -mpascal-strings, we want to back translate here.
Daniel Dunbard4510f22009-04-07 23:51:44 +00001977 //
1978 // FIXME: This is gross; that translation should be pulled from the
1979 // tool chain.
Daniel Dunbard067f7f2009-04-08 23:54:23 +00001980 if (Args.hasFlag(options::OPT_fpascal_strings,
Daniel Dunbard4510f22009-04-07 23:51:44 +00001981 options::OPT_fno_pascal_strings,
1982 false) ||
1983 Args.hasFlag(options::OPT_mpascal_strings,
1984 options::OPT_mno_pascal_strings,
1985 false))
Daniel Dunbard18049a2009-04-07 21:16:11 +00001986 CmdArgs.push_back("-fpascal-strings");
NAKAMURA Takumi029d74b2011-02-17 08:50:50 +00001987
Fariborz Jahaniana4cfff82011-01-07 01:05:02 +00001988 if (Args.hasArg(options::OPT_mkernel) ||
1989 Args.hasArg(options::OPT_fapple_kext)) {
1990 if (!Args.hasArg(options::OPT_fcommon))
1991 CmdArgs.push_back("-fno-common");
1992 }
Daniel Dunbard18049a2009-04-07 21:16:11 +00001993 // -fcommon is default, only pass non-default.
Fariborz Jahaniana4cfff82011-01-07 01:05:02 +00001994 else if (!Args.hasFlag(options::OPT_fcommon, options::OPT_fno_common))
Daniel Dunbard18049a2009-04-07 21:16:11 +00001995 CmdArgs.push_back("-fno-common");
1996
Daniel Dunbar2edd9232009-04-15 02:37:43 +00001997 // -fsigned-bitfields is default, and clang doesn't yet support
Daniel Dunbar6358d682010-10-15 22:30:42 +00001998 // -funsigned-bitfields.
Mike Stump11289f42009-09-09 15:08:12 +00001999 if (!Args.hasFlag(options::OPT_fsigned_bitfields,
Daniel Dunbar2edd9232009-04-15 02:37:43 +00002000 options::OPT_funsigned_bitfields))
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002001 D.Diag(diag::warn_drv_clang_unsupported)
Daniel Dunbar2edd9232009-04-15 02:37:43 +00002002 << Args.getLastArg(options::OPT_funsigned_bitfields)->getAsString(Args);
2003
Daniel Dunbar6358d682010-10-15 22:30:42 +00002004 // -fsigned-bitfields is default, and clang doesn't support -fno-for-scope.
2005 if (!Args.hasFlag(options::OPT_ffor_scope,
2006 options::OPT_fno_for_scope))
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002007 D.Diag(diag::err_drv_clang_unsupported)
Daniel Dunbar6358d682010-10-15 22:30:42 +00002008 << Args.getLastArg(options::OPT_fno_for_scope)->getAsString(Args);
2009
Jeffrey Yasskin460aa542010-06-08 04:56:20 +00002010 // -fcaret-diagnostics is default.
2011 if (!Args.hasFlag(options::OPT_fcaret_diagnostics,
2012 options::OPT_fno_caret_diagnostics, true))
2013 CmdArgs.push_back("-fno-caret-diagnostics");
2014
Daniel Dunbar8281bde2009-04-19 21:09:34 +00002015 // -fdiagnostics-fixit-info is default, only pass non-default.
Mike Stump11289f42009-09-09 15:08:12 +00002016 if (!Args.hasFlag(options::OPT_fdiagnostics_fixit_info,
Daniel Dunbar8281bde2009-04-19 21:09:34 +00002017 options::OPT_fno_diagnostics_fixit_info))
2018 CmdArgs.push_back("-fno-diagnostics-fixit-info");
Eric Christopher84fbdb42011-08-19 00:30:14 +00002019
Douglas Gregor46ce91a2011-04-15 22:04:17 +00002020 // Enable -fdiagnostics-show-name by default.
2021 if (Args.hasFlag(options::OPT_fdiagnostics_show_name,
2022 options::OPT_fno_diagnostics_show_name, false))
2023 CmdArgs.push_back("-fdiagnostics-show-name");
Daniel Dunbar8281bde2009-04-19 21:09:34 +00002024
Daniel Dunbar092f0cc2009-04-16 06:32:38 +00002025 // Enable -fdiagnostics-show-option by default.
Mike Stump11289f42009-09-09 15:08:12 +00002026 if (Args.hasFlag(options::OPT_fdiagnostics_show_option,
Daniel Dunbar092f0cc2009-04-16 06:32:38 +00002027 options::OPT_fno_diagnostics_show_option))
2028 CmdArgs.push_back("-fdiagnostics-show-option");
Daniel Dunbar5ec95022009-11-04 06:24:57 +00002029
Chris Lattnerbf6fac82010-05-04 21:55:25 +00002030 if (const Arg *A =
2031 Args.getLastArg(options::OPT_fdiagnostics_show_category_EQ)) {
2032 CmdArgs.push_back("-fdiagnostics-show-category");
2033 CmdArgs.push_back(A->getValue(Args));
2034 }
Daniel Dunbarfe6c97b2010-08-24 16:47:49 +00002035
Douglas Gregor643c9222011-05-21 17:07:29 +00002036 if (const Arg *A =
2037 Args.getLastArg(options::OPT_fdiagnostics_format_EQ)) {
2038 CmdArgs.push_back("-fdiagnostics-format");
2039 CmdArgs.push_back(A->getValue(Args));
2040 }
2041
Chandler Carruthb6766f02011-03-27 01:50:55 +00002042 if (Arg *A = Args.getLastArg(
2043 options::OPT_fdiagnostics_show_note_include_stack,
2044 options::OPT_fno_diagnostics_show_note_include_stack)) {
2045 if (A->getOption().matches(
2046 options::OPT_fdiagnostics_show_note_include_stack))
2047 CmdArgs.push_back("-fdiagnostics-show-note-include-stack");
2048 else
2049 CmdArgs.push_back("-fno-diagnostics-show-note-include-stack");
2050 }
2051
Daniel Dunbar5ec95022009-11-04 06:24:57 +00002052 // Color diagnostics are the default, unless the terminal doesn't support
2053 // them.
2054 if (Args.hasFlag(options::OPT_fcolor_diagnostics,
Argyrios Kyrtzidis4f920162010-09-23 12:56:06 +00002055 options::OPT_fno_color_diagnostics,
2056 llvm::sys::Process::StandardErrHasColors()))
Daniel Dunbar5ec95022009-11-04 06:24:57 +00002057 CmdArgs.push_back("-fcolor-diagnostics");
2058
Daniel Dunbardb097022009-06-08 21:13:54 +00002059 if (!Args.hasFlag(options::OPT_fshow_source_location,
2060 options::OPT_fno_show_source_location))
2061 CmdArgs.push_back("-fno-show-source-location");
Daniel Dunbar092f0cc2009-04-16 06:32:38 +00002062
Douglas Gregor643c9222011-05-21 17:07:29 +00002063 if (!Args.hasFlag(options::OPT_fshow_column,
2064 options::OPT_fno_show_column,
2065 true))
2066 CmdArgs.push_back("-fno-show-column");
2067
Douglas Gregor8ed0c0b2010-07-09 17:35:33 +00002068 if (!Args.hasFlag(options::OPT_fspell_checking,
2069 options::OPT_fno_spell_checking))
2070 CmdArgs.push_back("-fno-spell-checking");
Daniel Dunbarfe6c97b2010-08-24 16:47:49 +00002071
Daniel Dunbar473f8a62010-10-18 22:49:46 +00002072
Daniel Dunbar3ada2b72010-11-02 19:42:04 +00002073 // Silently ignore -fasm-blocks for now.
2074 (void) Args.hasFlag(options::OPT_fasm_blocks, options::OPT_fno_asm_blocks,
2075 false);
Daniel Dunbar473f8a62010-10-18 22:49:46 +00002076
Jeffrey Yasskin2b99c6f2010-06-11 05:57:47 +00002077 if (Arg *A = Args.getLastArg(options::OPT_fshow_overloads_EQ))
2078 A->render(Args, CmdArgs);
2079
Daniel Dunbarf5e9b1f2009-04-19 21:20:32 +00002080 // -fdollars-in-identifiers default varies depending on platform and
2081 // language; only pass if specified.
Mike Stump11289f42009-09-09 15:08:12 +00002082 if (Arg *A = Args.getLastArg(options::OPT_fdollars_in_identifiers,
Daniel Dunbarf5e9b1f2009-04-19 21:20:32 +00002083 options::OPT_fno_dollars_in_identifiers)) {
2084 if (A->getOption().matches(options::OPT_fdollars_in_identifiers))
Daniel Dunbar15cef0e2009-12-16 20:10:18 +00002085 CmdArgs.push_back("-fdollars-in-identifiers");
Daniel Dunbarf5e9b1f2009-04-19 21:20:32 +00002086 else
Daniel Dunbar15cef0e2009-12-16 20:10:18 +00002087 CmdArgs.push_back("-fno-dollars-in-identifiers");
Daniel Dunbarf5e9b1f2009-04-19 21:20:32 +00002088 }
2089
Daniel Dunbaradeeb052009-05-22 19:02:20 +00002090 // -funit-at-a-time is default, and we don't support -fno-unit-at-a-time for
2091 // practical purposes.
Mike Stump11289f42009-09-09 15:08:12 +00002092 if (Arg *A = Args.getLastArg(options::OPT_funit_at_a_time,
Daniel Dunbaradeeb052009-05-22 19:02:20 +00002093 options::OPT_fno_unit_at_a_time)) {
2094 if (A->getOption().matches(options::OPT_fno_unit_at_a_time))
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002095 D.Diag(diag::warn_drv_clang_unsupported) << A->getAsString(Args);
Daniel Dunbaradeeb052009-05-22 19:02:20 +00002096 }
Eli Friedmanbb0d9a52009-07-14 21:58:17 +00002097
Daniel Dunbar2ffe0292009-09-10 03:37:02 +00002098 // Default to -fno-builtin-str{cat,cpy} on Darwin for ARM.
Daniel Dunbar4fa08112009-09-10 04:57:27 +00002099 //
Daniel Dunbar6c536aa2009-12-11 23:00:49 +00002100 // FIXME: This is disabled until clang -cc1 supports -fno-builtin-foo. PR4941.
Daniel Dunbar4fa08112009-09-10 04:57:27 +00002101#if 0
Daniel Dunbar2ffe0292009-09-10 03:37:02 +00002102 if (getToolChain().getTriple().getOS() == llvm::Triple::Darwin &&
2103 (getToolChain().getTriple().getArch() == llvm::Triple::arm ||
2104 getToolChain().getTriple().getArch() == llvm::Triple::thumb)) {
2105 if (!Args.hasArg(options::OPT_fbuiltin_strcat))
2106 CmdArgs.push_back("-fno-builtin-strcat");
2107 if (!Args.hasArg(options::OPT_fbuiltin_strcpy))
2108 CmdArgs.push_back("-fno-builtin-strcpy");
2109 }
Daniel Dunbar4fa08112009-09-10 04:57:27 +00002110#endif
Daniel Dunbar2ffe0292009-09-10 03:37:02 +00002111
Daniel Dunbar8c3d7352011-03-18 21:23:40 +00002112 // Only allow -traditional or -traditional-cpp outside in preprocessing modes.
Mike Stump11289f42009-09-09 15:08:12 +00002113 if (Arg *A = Args.getLastArg(options::OPT_traditional,
Daniel Dunbar8c3d7352011-03-18 21:23:40 +00002114 options::OPT_traditional_cpp)) {
2115 if (isa<PreprocessJobAction>(JA))
2116 CmdArgs.push_back("-traditional-cpp");
Eric Christopher84fbdb42011-08-19 00:30:14 +00002117 else
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002118 D.Diag(diag::err_drv_clang_unsupported) << A->getAsString(Args);
Daniel Dunbar8c3d7352011-03-18 21:23:40 +00002119 }
Eli Friedmanbb0d9a52009-07-14 21:58:17 +00002120
Daniel Dunbara2aedc62009-03-18 10:01:51 +00002121 Args.AddLastArg(CmdArgs, options::OPT_dM);
Chris Lattnercac63f32009-04-12 01:56:53 +00002122 Args.AddLastArg(CmdArgs, options::OPT_dD);
Daniel Dunbara2aedc62009-03-18 10:01:51 +00002123
Daniel Dunbar76fa8402010-04-15 06:09:03 +00002124 // Forward -Xclang arguments to -cc1, and -mllvm arguments to the LLVM option
2125 // parser.
Daniel Dunbara2aedc62009-03-18 10:01:51 +00002126 Args.AddAllArgValues(CmdArgs, options::OPT_Xclang);
Daniel Dunbar76fa8402010-04-15 06:09:03 +00002127 for (arg_iterator it = Args.filtered_begin(options::OPT_mllvm),
2128 ie = Args.filtered_end(); it != ie; ++it) {
Daniel Dunbara442fd52010-06-11 22:00:13 +00002129 (*it)->claim();
Daniel Dunbar88534f42010-04-17 06:10:00 +00002130
Daniel Dunbar76fa8402010-04-15 06:09:03 +00002131 // We translate this by hand to the -cc1 argument, since nightly test uses
2132 // it and developers have been trained to spell it with -mllvm.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002133 if (StringRef((*it)->getValue(Args, 0)) == "-disable-llvm-optzns")
Daniel Dunbar76fa8402010-04-15 06:09:03 +00002134 CmdArgs.push_back("-disable-llvm-optzns");
2135 else
Daniel Dunbara442fd52010-06-11 22:00:13 +00002136 (*it)->render(Args, CmdArgs);
Daniel Dunbar76fa8402010-04-15 06:09:03 +00002137 }
Daniel Dunbara2aedc62009-03-18 10:01:51 +00002138
Daniel Dunbard67a3222009-03-30 06:36:42 +00002139 if (Output.getType() == types::TY_Dependencies) {
2140 // Handled with other dependency code.
Daniel Dunbar5cdf3e02009-03-19 07:29:38 +00002141 } else if (Output.isFilename()) {
Daniel Dunbara3246a02009-03-18 08:07:30 +00002142 CmdArgs.push_back("-o");
Daniel Dunbar5cdf3e02009-03-19 07:29:38 +00002143 CmdArgs.push_back(Output.getFilename());
2144 } else {
2145 assert(Output.isNothing() && "Invalid output.");
Daniel Dunbara3246a02009-03-18 08:07:30 +00002146 }
2147
Daniel Dunbara2aedc62009-03-18 10:01:51 +00002148 for (InputInfoList::const_iterator
2149 it = Inputs.begin(), ie = Inputs.end(); it != ie; ++it) {
2150 const InputInfo &II = *it;
2151 CmdArgs.push_back("-x");
2152 CmdArgs.push_back(types::getTypeName(II.getType()));
Daniel Dunbarb440f562010-08-02 02:38:21 +00002153 if (II.isFilename())
Daniel Dunbar5cdf3e02009-03-19 07:29:38 +00002154 CmdArgs.push_back(II.getFilename());
Daniel Dunbara2aedc62009-03-18 10:01:51 +00002155 else
Daniel Dunbar5cdf3e02009-03-19 07:29:38 +00002156 II.getInputArg().renderAsInput(Args, CmdArgs);
Daniel Dunbara2aedc62009-03-18 10:01:51 +00002157 }
Daniel Dunbard067f7f2009-04-08 23:54:23 +00002158
Chris Lattnere9d7d782009-11-03 19:50:27 +00002159 Args.AddAllArgs(CmdArgs, options::OPT_undef);
2160
Daniel Dunbarb31b76f2010-07-18 21:16:15 +00002161 const char *Exec = getToolChain().getDriver().getClangProgramPath();
Daniel Dunbar24c7f5e2009-12-18 02:43:17 +00002162
2163 // Optionally embed the -cc1 level arguments into the debug info, for build
2164 // analysis.
2165 if (getToolChain().UseDwarfDebugFlags()) {
Daniel Dunbar7f3d9502010-06-04 18:47:06 +00002166 ArgStringList OriginalArgs;
2167 for (ArgList::const_iterator it = Args.begin(),
2168 ie = Args.end(); it != ie; ++it)
2169 (*it)->render(Args, OriginalArgs);
Daniel Dunbarfe6c97b2010-08-24 16:47:49 +00002170
Daniel Dunbar24c7f5e2009-12-18 02:43:17 +00002171 llvm::SmallString<256> Flags;
2172 Flags += Exec;
Daniel Dunbar7f3d9502010-06-04 18:47:06 +00002173 for (unsigned i = 0, e = OriginalArgs.size(); i != e; ++i) {
Daniel Dunbar24c7f5e2009-12-18 02:43:17 +00002174 Flags += " ";
Daniel Dunbar7f3d9502010-06-04 18:47:06 +00002175 Flags += OriginalArgs[i];
Daniel Dunbar24c7f5e2009-12-18 02:43:17 +00002176 }
2177 CmdArgs.push_back("-dwarf-debug-flags");
2178 CmdArgs.push_back(Args.MakeArgString(Flags.str()));
2179 }
2180
Daniel Dunbar7fbaf532010-08-02 02:38:28 +00002181 C.addCommand(new Command(JA, *this, Exec, CmdArgs));
Daniel Dunbar17731772009-03-23 19:03:36 +00002182
Roman Divacky178e01602011-02-10 16:52:03 +00002183 if (Arg *A = Args.getLastArg(options::OPT_pg))
2184 if (Args.hasArg(options::OPT_fomit_frame_pointer))
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002185 D.Diag(diag::err_drv_argument_not_allowed_with)
Roman Divacky178e01602011-02-10 16:52:03 +00002186 << "-fomit-frame-pointer" << A->getAsString(Args);
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00002187
Daniel Dunbarc2a71892009-04-03 20:51:31 +00002188 // Claim some arguments which clang supports automatically.
2189
Daniel Dunbar3e0cac62010-04-15 06:18:42 +00002190 // -fpch-preprocess is used with gcc to add a special marker in the output to
2191 // include the PCH file. Clang's PTH solution is completely transparent, so we
2192 // do not need to deal with it at all.
Daniel Dunbarc2a71892009-04-03 20:51:31 +00002193 Args.ClaimAllArgs(options::OPT_fpch_preprocess);
Daniel Dunbard067f7f2009-04-08 23:54:23 +00002194
Daniel Dunbar17731772009-03-23 19:03:36 +00002195 // Claim some arguments which clang doesn't support, but we don't
2196 // care to warn the user about.
Daniel Dunbar44b36ee2009-11-25 11:53:23 +00002197 Args.ClaimAllArgs(options::OPT_clang_ignored_f_Group);
2198 Args.ClaimAllArgs(options::OPT_clang_ignored_m_Group);
Rafael Espindola22f603032011-02-28 23:29:45 +00002199
Rafael Espindolad95a8122011-03-01 05:25:27 +00002200 // Disable warnings for clang -E -use-gold-plugin -emit-llvm foo.c
Rafael Espindola22f603032011-02-28 23:29:45 +00002201 Args.ClaimAllArgs(options::OPT_use_gold_plugin);
Rafael Espindolad95a8122011-03-01 05:25:27 +00002202 Args.ClaimAllArgs(options::OPT_emit_llvm);
Daniel Dunbar1a093d22009-03-18 06:00:36 +00002203}
2204
Daniel Dunbar4f5e79c2010-05-20 21:30:13 +00002205void ClangAs::ConstructJob(Compilation &C, const JobAction &JA,
Daniel Dunbar4f5e79c2010-05-20 21:30:13 +00002206 const InputInfo &Output,
2207 const InputInfoList &Inputs,
2208 const ArgList &Args,
2209 const char *LinkingOutput) const {
Daniel Dunbar4f5e79c2010-05-20 21:30:13 +00002210 ArgStringList CmdArgs;
2211
2212 assert(Inputs.size() == 1 && "Unexpected number of inputs.");
2213 const InputInfo &Input = Inputs[0];
2214
Rafael Espindolacfaadda2010-11-17 22:13:25 +00002215 // Don't warn about "clang -w -c foo.s"
2216 Args.ClaimAllArgs(options::OPT_w);
Rafael Espindolad95a8122011-03-01 05:25:27 +00002217 // and "clang -emit-llvm -c foo.s"
2218 Args.ClaimAllArgs(options::OPT_emit_llvm);
2219 // and "clang -use-gold-plugin -c foo.s"
2220 Args.ClaimAllArgs(options::OPT_use_gold_plugin);
Rafael Espindolacfaadda2010-11-17 22:13:25 +00002221
Daniel Dunbar4f5e79c2010-05-20 21:30:13 +00002222 // Invoke ourselves in -cc1as mode.
2223 //
2224 // FIXME: Implement custom jobs for internal actions.
2225 CmdArgs.push_back("-cc1as");
2226
2227 // Add the "effective" target triple.
2228 CmdArgs.push_back("-triple");
Daniel Dunbar82eb4ce2010-08-23 22:35:37 +00002229 std::string TripleStr = getToolChain().ComputeEffectiveClangTriple(Args);
Daniel Dunbar4f5e79c2010-05-20 21:30:13 +00002230 CmdArgs.push_back(Args.MakeArgString(TripleStr));
2231
2232 // Set the output mode, we currently only expect to be used as a real
2233 // assembler.
2234 CmdArgs.push_back("-filetype");
2235 CmdArgs.push_back("obj");
2236
Joerg Sonnenbergeref317a22011-05-06 14:35:16 +00002237 if (UseRelaxAll(C, Args))
Daniel Dunbar99ca8b72010-05-28 16:43:21 +00002238 CmdArgs.push_back("-relax-all");
Daniel Dunbar06e2cc32010-05-27 06:18:05 +00002239
Daniel Dunbar1d733e22011-03-17 17:37:29 +00002240 // Ignore explicit -force_cpusubtype_ALL option.
2241 (void) Args.hasArg(options::OPT_force__cpusubtype__ALL);
Daniel Dunbar4f5e79c2010-05-20 21:30:13 +00002242
2243 // FIXME: Add -g support, once we have it.
2244
2245 // FIXME: Add -static support, once we have it.
2246
2247 Args.AddAllArgValues(CmdArgs, options::OPT_Wa_COMMA,
2248 options::OPT_Xassembler);
Daniel Dunbar252e8f92011-04-29 17:53:18 +00002249 Args.AddAllArgs(CmdArgs, options::OPT_mllvm);
Daniel Dunbar4f5e79c2010-05-20 21:30:13 +00002250
2251 assert(Output.isFilename() && "Unexpected lipo output.");
2252 CmdArgs.push_back("-o");
2253 CmdArgs.push_back(Output.getFilename());
2254
Daniel Dunbarb440f562010-08-02 02:38:21 +00002255 assert(Input.isFilename() && "Invalid input.");
2256 CmdArgs.push_back(Input.getFilename());
Daniel Dunbar4f5e79c2010-05-20 21:30:13 +00002257
Daniel Dunbarb31b76f2010-07-18 21:16:15 +00002258 const char *Exec = getToolChain().getDriver().getClangProgramPath();
Daniel Dunbar7fbaf532010-08-02 02:38:28 +00002259 C.addCommand(new Command(JA, *this, Exec, CmdArgs));
Daniel Dunbar4f5e79c2010-05-20 21:30:13 +00002260}
2261
Daniel Dunbara3246a02009-03-18 08:07:30 +00002262void gcc::Common::ConstructJob(Compilation &C, const JobAction &JA,
Daniel Dunbara3246a02009-03-18 08:07:30 +00002263 const InputInfo &Output,
2264 const InputInfoList &Inputs,
Daniel Dunbara2aedc62009-03-18 10:01:51 +00002265 const ArgList &Args,
Daniel Dunbara3246a02009-03-18 08:07:30 +00002266 const char *LinkingOutput) const {
Daniel Dunbar083edf72009-12-21 18:54:17 +00002267 const Driver &D = getToolChain().getDriver();
Daniel Dunbara3246a02009-03-18 08:07:30 +00002268 ArgStringList CmdArgs;
Daniel Dunbar1a093d22009-03-18 06:00:36 +00002269
Daniel Dunbard067f7f2009-04-08 23:54:23 +00002270 for (ArgList::const_iterator
Daniel Dunbara2aedc62009-03-18 10:01:51 +00002271 it = Args.begin(), ie = Args.end(); it != ie; ++it) {
Daniel Dunbara3246a02009-03-18 08:07:30 +00002272 Arg *A = *it;
Daniel Dunbar2da02722009-03-19 07:55:12 +00002273 if (A->getOption().hasForwardToGCC()) {
Daniel Dunbar939c1212010-08-03 16:14:14 +00002274 // Don't forward any -g arguments to assembly steps.
2275 if (isa<AssembleJobAction>(JA) &&
2276 A->getOption().matches(options::OPT_g_Group))
2277 continue;
2278
Daniel Dunbar2da02722009-03-19 07:55:12 +00002279 // It is unfortunate that we have to claim here, as this means
2280 // we will basically never report anything interesting for
Daniel Dunbar5716d872009-05-02 21:41:52 +00002281 // platforms using a generic gcc, even if we are just using gcc
2282 // to get to the assembler.
Daniel Dunbar2da02722009-03-19 07:55:12 +00002283 A->claim();
Daniel Dunbara2aedc62009-03-18 10:01:51 +00002284 A->render(Args, CmdArgs);
Daniel Dunbar2da02722009-03-19 07:55:12 +00002285 }
Daniel Dunbara3246a02009-03-18 08:07:30 +00002286 }
Daniel Dunbard067f7f2009-04-08 23:54:23 +00002287
Daniel Dunbar4e295052010-01-25 22:35:08 +00002288 RenderExtraToolArgs(JA, CmdArgs);
Daniel Dunbara3246a02009-03-18 08:07:30 +00002289
2290 // If using a driver driver, force the arch.
Daniel Dunbar5bbebfe2009-05-22 02:21:04 +00002291 const std::string &Arch = getToolChain().getArchName();
Daniel Dunbarf4894fe2009-12-23 00:46:38 +00002292 if (getToolChain().getTriple().getOS() == llvm::Triple::Darwin) {
Daniel Dunbara3246a02009-03-18 08:07:30 +00002293 CmdArgs.push_back("-arch");
Daniel Dunbar0a05d932009-04-01 20:33:11 +00002294
2295 // FIXME: Remove these special cases.
Daniel Dunbar5bbebfe2009-05-22 02:21:04 +00002296 if (Arch == "powerpc")
2297 CmdArgs.push_back("ppc");
2298 else if (Arch == "powerpc64")
2299 CmdArgs.push_back("ppc64");
2300 else
Daniel Dunbar1c28f1e2009-09-09 22:32:48 +00002301 CmdArgs.push_back(Args.MakeArgString(Arch));
Daniel Dunbara3246a02009-03-18 08:07:30 +00002302 }
2303
Daniel Dunbar5716d872009-05-02 21:41:52 +00002304 // Try to force gcc to match the tool chain we want, if we recognize
2305 // the arch.
Daniel Dunbar5bbebfe2009-05-22 02:21:04 +00002306 //
2307 // FIXME: The triple class should directly provide the information we want
2308 // here.
2309 if (Arch == "i386" || Arch == "powerpc")
Daniel Dunbar5716d872009-05-02 21:41:52 +00002310 CmdArgs.push_back("-m32");
Daniel Dunbar5bbebfe2009-05-22 02:21:04 +00002311 else if (Arch == "x86_64" || Arch == "powerpc64")
Daniel Dunbar5716d872009-05-02 21:41:52 +00002312 CmdArgs.push_back("-m64");
2313
Daniel Dunbarb440f562010-08-02 02:38:21 +00002314 if (Output.isFilename()) {
Daniel Dunbara3246a02009-03-18 08:07:30 +00002315 CmdArgs.push_back("-o");
Daniel Dunbar5cdf3e02009-03-19 07:29:38 +00002316 CmdArgs.push_back(Output.getFilename());
2317 } else {
2318 assert(Output.isNothing() && "Unexpected output");
Daniel Dunbara3246a02009-03-18 08:07:30 +00002319 CmdArgs.push_back("-fsyntax-only");
Daniel Dunbar5cdf3e02009-03-19 07:29:38 +00002320 }
Daniel Dunbara3246a02009-03-18 08:07:30 +00002321
2322
2323 // Only pass -x if gcc will understand it; otherwise hope gcc
2324 // understands the suffix correctly. The main use case this would go
2325 // wrong in is for linker inputs if they happened to have an odd
2326 // suffix; really the only way to get this to happen is a command
2327 // like '-x foobar a.c' which will treat a.c like a linker input.
2328 //
2329 // FIXME: For the linker case specifically, can we safely convert
2330 // inputs into '-Wl,' options?
2331 for (InputInfoList::const_iterator
2332 it = Inputs.begin(), ie = Inputs.end(); it != ie; ++it) {
2333 const InputInfo &II = *it;
Daniel Dunbare3e263f2009-05-02 20:14:53 +00002334
Daniel Dunbar6cdf83c2009-09-01 16:57:46 +00002335 // Don't try to pass LLVM or AST inputs to a generic gcc.
Daniel Dunbar24e52992010-06-07 23:28:45 +00002336 if (II.getType() == types::TY_LLVM_IR || II.getType() == types::TY_LTO_IR ||
2337 II.getType() == types::TY_LLVM_BC || II.getType() == types::TY_LTO_BC)
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002338 D.Diag(diag::err_drv_no_linker_llvm_support)
Daniel Dunbar1c28f1e2009-09-09 22:32:48 +00002339 << getToolChain().getTripleString();
Daniel Dunbar6cdf83c2009-09-01 16:57:46 +00002340 else if (II.getType() == types::TY_AST)
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002341 D.Diag(diag::err_drv_no_ast_support)
Daniel Dunbar1c28f1e2009-09-09 22:32:48 +00002342 << getToolChain().getTripleString();
Daniel Dunbare3e263f2009-05-02 20:14:53 +00002343
Daniel Dunbara3246a02009-03-18 08:07:30 +00002344 if (types::canTypeBeUserSpecified(II.getType())) {
2345 CmdArgs.push_back("-x");
2346 CmdArgs.push_back(types::getTypeName(II.getType()));
2347 }
2348
Daniel Dunbarb440f562010-08-02 02:38:21 +00002349 if (II.isFilename())
Daniel Dunbar5cdf3e02009-03-19 07:29:38 +00002350 CmdArgs.push_back(II.getFilename());
Daniel Dunbarf2476752010-09-25 18:10:05 +00002351 else {
2352 const Arg &A = II.getInputArg();
2353
2354 // Reverse translate some rewritten options.
2355 if (A.getOption().matches(options::OPT_Z_reserved_lib_stdcxx)) {
2356 CmdArgs.push_back("-lstdc++");
2357 continue;
2358 }
2359
Daniel Dunbar5cdf3e02009-03-19 07:29:38 +00002360 // Don't render as input, we need gcc to do the translations.
Daniel Dunbarf2476752010-09-25 18:10:05 +00002361 A.render(Args, CmdArgs);
2362 }
Daniel Dunbara3246a02009-03-18 08:07:30 +00002363 }
2364
Dylan Noblesmith70e73a32011-04-09 13:31:59 +00002365 const std::string customGCCName = D.getCCCGenericGCCName();
2366 const char *GCCName;
2367 if (!customGCCName.empty())
2368 GCCName = customGCCName.c_str();
2369 else if (D.CCCIsCXX) {
2370#ifdef IS_CYGWIN15
2371 // FIXME: Detect the version of Cygwin at runtime?
2372 GCCName = "g++-4";
2373#else
2374 GCCName = "g++";
2375#endif
2376 } else
2377 GCCName = "gcc";
2378
Daniel Dunbard067f7f2009-04-08 23:54:23 +00002379 const char *Exec =
Daniel Dunbar9c3ed5f2010-07-14 18:46:23 +00002380 Args.MakeArgString(getToolChain().GetProgramPath(GCCName));
Daniel Dunbar7fbaf532010-08-02 02:38:28 +00002381 C.addCommand(new Command(JA, *this, Exec, CmdArgs));
Daniel Dunbar1a093d22009-03-18 06:00:36 +00002382}
2383
Daniel Dunbar4e295052010-01-25 22:35:08 +00002384void gcc::Preprocess::RenderExtraToolArgs(const JobAction &JA,
2385 ArgStringList &CmdArgs) const {
Daniel Dunbara3246a02009-03-18 08:07:30 +00002386 CmdArgs.push_back("-E");
Daniel Dunbar1a093d22009-03-18 06:00:36 +00002387}
2388
Daniel Dunbar4e295052010-01-25 22:35:08 +00002389void gcc::Precompile::RenderExtraToolArgs(const JobAction &JA,
2390 ArgStringList &CmdArgs) const {
Daniel Dunbara3246a02009-03-18 08:07:30 +00002391 // The type is good enough.
Daniel Dunbar1a093d22009-03-18 06:00:36 +00002392}
2393
Daniel Dunbar4e295052010-01-25 22:35:08 +00002394void gcc::Compile::RenderExtraToolArgs(const JobAction &JA,
2395 ArgStringList &CmdArgs) const {
Daniel Dunbaraeea8ac2010-02-11 03:16:21 +00002396 const Driver &D = getToolChain().getDriver();
2397
Daniel Dunbar4e295052010-01-25 22:35:08 +00002398 // If -flto, etc. are present then make sure not to force assembly output.
Daniel Dunbar24e52992010-06-07 23:28:45 +00002399 if (JA.getType() == types::TY_LLVM_IR || JA.getType() == types::TY_LTO_IR ||
2400 JA.getType() == types::TY_LLVM_BC || JA.getType() == types::TY_LTO_BC)
Daniel Dunbar4e295052010-01-25 22:35:08 +00002401 CmdArgs.push_back("-c");
Daniel Dunbaraeea8ac2010-02-11 03:16:21 +00002402 else {
2403 if (JA.getType() != types::TY_PP_Asm)
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002404 D.Diag(diag::err_drv_invalid_gcc_output_type)
Daniel Dunbaraeea8ac2010-02-11 03:16:21 +00002405 << getTypeName(JA.getType());
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00002406
Daniel Dunbar4e295052010-01-25 22:35:08 +00002407 CmdArgs.push_back("-S");
Daniel Dunbaraeea8ac2010-02-11 03:16:21 +00002408 }
Daniel Dunbar1a093d22009-03-18 06:00:36 +00002409}
2410
Daniel Dunbar4e295052010-01-25 22:35:08 +00002411void gcc::Assemble::RenderExtraToolArgs(const JobAction &JA,
2412 ArgStringList &CmdArgs) const {
Daniel Dunbara3246a02009-03-18 08:07:30 +00002413 CmdArgs.push_back("-c");
Daniel Dunbar1a093d22009-03-18 06:00:36 +00002414}
Daniel Dunbara3246a02009-03-18 08:07:30 +00002415
Daniel Dunbar4e295052010-01-25 22:35:08 +00002416void gcc::Link::RenderExtraToolArgs(const JobAction &JA,
2417 ArgStringList &CmdArgs) const {
Daniel Dunbara3246a02009-03-18 08:07:30 +00002418 // The types are (hopefully) good enough.
2419}
2420
Daniel Dunbare6adeee2009-03-29 17:08:39 +00002421const char *darwin::CC1::getCC1Name(types::ID Type) const {
2422 switch (Type) {
2423 default:
2424 assert(0 && "Unexpected type for Darwin CC1 tool.");
2425 case types::TY_Asm:
2426 case types::TY_C: case types::TY_CHeader:
2427 case types::TY_PP_C: case types::TY_PP_CHeader:
2428 return "cc1";
2429 case types::TY_ObjC: case types::TY_ObjCHeader:
Nico Webered8080c2011-08-13 23:13:37 +00002430 case types::TY_PP_ObjC: case types::TY_PP_ObjC_Alias:
2431 case types::TY_PP_ObjCHeader:
Daniel Dunbare6adeee2009-03-29 17:08:39 +00002432 return "cc1obj";
2433 case types::TY_CXX: case types::TY_CXXHeader:
2434 case types::TY_PP_CXX: case types::TY_PP_CXXHeader:
2435 return "cc1plus";
2436 case types::TY_ObjCXX: case types::TY_ObjCXXHeader:
Nico Webered8080c2011-08-13 23:13:37 +00002437 case types::TY_PP_ObjCXX: case types::TY_PP_ObjCXX_Alias:
2438 case types::TY_PP_ObjCXXHeader:
Daniel Dunbare6adeee2009-03-29 17:08:39 +00002439 return "cc1objplus";
2440 }
2441}
2442
Daniel Dunbard067f7f2009-04-08 23:54:23 +00002443const char *darwin::CC1::getBaseInputName(const ArgList &Args,
Daniel Dunbar52e96cc2009-03-30 00:34:04 +00002444 const InputInfoList &Inputs) {
Michael J. Spencere1696752010-12-18 00:19:12 +00002445 return Args.MakeArgString(
2446 llvm::sys::path::filename(Inputs[0].getBaseInput()));
Daniel Dunbarafec1f52009-03-29 18:40:18 +00002447}
2448
Daniel Dunbard067f7f2009-04-08 23:54:23 +00002449const char *darwin::CC1::getBaseInputStem(const ArgList &Args,
Daniel Dunbar52e96cc2009-03-30 00:34:04 +00002450 const InputInfoList &Inputs) {
Daniel Dunbarafec1f52009-03-29 18:40:18 +00002451 const char *Str = getBaseInputName(Args, Inputs);
2452
Chris Lattner906bb902011-01-16 08:14:11 +00002453 if (const char *End = strrchr(Str, '.'))
Daniel Dunbar1c28f1e2009-09-09 22:32:48 +00002454 return Args.MakeArgString(std::string(Str, End));
Daniel Dunbarafec1f52009-03-29 18:40:18 +00002455
2456 return Str;
2457}
2458
2459const char *
Daniel Dunbard067f7f2009-04-08 23:54:23 +00002460darwin::CC1::getDependencyFileName(const ArgList &Args,
Daniel Dunbar52e96cc2009-03-30 00:34:04 +00002461 const InputInfoList &Inputs) {
Daniel Dunbarafec1f52009-03-29 18:40:18 +00002462 // FIXME: Think about this more.
2463 std::string Res;
2464
2465 if (Arg *OutputOpt = Args.getLastArg(options::OPT_o)) {
2466 std::string Str(OutputOpt->getValue(Args));
Daniel Dunbarafec1f52009-03-29 18:40:18 +00002467 Res = Str.substr(0, Str.rfind('.'));
Chad Rosier6fdf38b2011-08-17 23:08:45 +00002468 } else {
Daniel Dunbar52e96cc2009-03-30 00:34:04 +00002469 Res = darwin::CC1::getBaseInputStem(Args, Inputs);
Chad Rosier6fdf38b2011-08-17 23:08:45 +00002470 }
Daniel Dunbar1c28f1e2009-09-09 22:32:48 +00002471 return Args.MakeArgString(Res + ".d");
Daniel Dunbarafec1f52009-03-29 18:40:18 +00002472}
2473
Chad Rosierbc5ea3d2011-08-17 18:24:55 +00002474void darwin::CC1::RemoveCC1UnsupportedArgs(ArgStringList &CmdArgs) const {
Eric Christopher84fbdb42011-08-19 00:30:14 +00002475 for (ArgStringList::iterator it = CmdArgs.begin(), ie = CmdArgs.end();
Chad Rosierbc5ea3d2011-08-17 18:24:55 +00002476 it != ie;) {
Chad Rosier2f818792011-08-18 17:56:32 +00002477
2478 StringRef Option = *it;
Chad Rosierf29d9aa2011-08-26 18:30:43 +00002479 bool RemoveOption = false;
Chad Rosier30453862011-08-18 01:18:28 +00002480
Chad Rosierf29d9aa2011-08-26 18:30:43 +00002481 // Remove -faltivec
2482 if (Option.equals("-faltivec")) {
2483 it = CmdArgs.erase(it);
2484 ie = CmdArgs.end();
Chad Rosier30453862011-08-18 01:18:28 +00002485 continue;
2486 }
2487
Chad Rosierf29d9aa2011-08-26 18:30:43 +00002488 // Handle machine specific options.
2489 if (Option.startswith("-m")) {
2490 RemoveOption = llvm::StringSwitch<bool>(Option)
2491 .Case("-mthumb", true)
2492 .Case("-mno-thumb", true)
2493 .Case("-mno-fused-madd", true)
2494 .Case("-mlong-branch", true)
2495 .Case("-mlongcall", true)
2496 .Case("-mcpu=G4", true)
2497 .Case("-mcpu=G5", true)
2498 .Default(false);
2499 }
2500
2501 // Handle warning options.
2502 if (Option.startswith("-W")) {
2503 // Remove -W/-Wno- to reduce the number of cases.
2504 if (Option.startswith("-Wno-"))
2505 Option = Option.substr(5);
2506 else
2507 Option = Option.substr(2);
2508
2509 RemoveOption = llvm::StringSwitch<bool>(Option)
2510 .Case("address-of-temporary", true)
2511 .Case("ambiguous-member-template", true)
2512 .Case("analyzer-incompatible-plugin", true)
2513 .Case("array-bounds", true)
2514 .Case("array-bounds-pointer-arithmetic", true)
2515 .Case("bind-to-temporary-copy", true)
2516 .Case("bitwise-op-parentheses", true)
2517 .Case("bool-conversions", true)
2518 .Case("builtin-macro-redefined", true)
2519 .Case("c++-hex-floats", true)
2520 .Case("c++0x-compat", true)
2521 .Case("c++0x-extensions", true)
2522 .Case("c++0x-narrowing", true)
2523 .Case("c++0x-static-nonintegral-init", true)
2524 .Case("conditional-uninitialized", true)
2525 .Case("constant-conversion", true)
2526 .Case("CFString-literal", true)
2527 .Case("constant-logical-operand", true)
2528 .Case("custom-atomic-properties", true)
2529 .Case("default-arg-special-member", true)
2530 .Case("delegating-ctor-cycles", true)
2531 .Case("delete-non-virtual-dtor", true)
2532 .Case("deprecated-implementations", true)
2533 .Case("deprecated-writable-strings", true)
2534 .Case("distributed-object-modifiers", true)
2535 .Case("duplicate-method-arg", true)
2536 .Case("dynamic-class-memaccess", true)
2537 .Case("enum-compare", true)
2538 .Case("exit-time-destructors", true)
2539 .Case("gnu", true)
2540 .Case("gnu-designator", true)
2541 .Case("header-hygiene", true)
2542 .Case("idiomatic-parentheses", true)
2543 .Case("ignored-qualifiers", true)
2544 .Case("implicit-atomic-properties", true)
2545 .Case("incompatible-pointer-types", true)
2546 .Case("incomplete-implementation", true)
2547 .Case("initializer-overrides", true)
2548 .Case("invalid-noreturn", true)
2549 .Case("invalid-token-paste", true)
2550 .Case("literal-conversion", true)
2551 .Case("literal-range", true)
2552 .Case("local-type-template-args", true)
2553 .Case("logical-op-parentheses", true)
2554 .Case("method-signatures", true)
2555 .Case("microsoft", true)
2556 .Case("mismatched-tags", true)
2557 .Case("missing-method-return-type", true)
2558 .Case("non-pod-varargs", true)
2559 .Case("nonfragile-abi2", true)
2560 .Case("null-arithmetic", true)
2561 .Case("null-dereference", true)
2562 .Case("out-of-line-declaration", true)
2563 .Case("overriding-method-mismatch", true)
2564 .Case("readonly-setter-attrs", true)
2565 .Case("return-stack-address", true)
2566 .Case("self-assign", true)
2567 .Case("semicolon-before-method-body", true)
2568 .Case("sentinel", true)
2569 .Case("shift-overflow", true)
2570 .Case("shift-sign-overflow", true)
2571 .Case("sign-conversion", true)
2572 .Case("sizeof-array-argument", true)
2573 .Case("sizeof-pointer-memaccess", true)
2574 .Case("string-compare", true)
2575 .Case("super-class-method-mismatch", true)
2576 .Case("tautological-compare", true)
2577 .Case("typedef-redefinition", true)
2578 .Case("typename-missing", true)
2579 .Case("undefined-reinterpret-cast", true)
2580 .Case("unknown-warning-option", true)
2581 .Case("unnamed-type-template-args", true)
2582 .Case("unneeded-internal-declaration", true)
2583 .Case("unneeded-member-function", true)
2584 .Case("unused-comparison", true)
2585 .Case("unused-exception-parameter", true)
2586 .Case("unused-member-function", true)
2587 .Case("unused-result", true)
2588 .Case("vector-conversions", true)
2589 .Case("vla", true)
2590 .Case("used-but-marked-unused", true)
2591 .Case("weak-vtables", true)
2592 .Default(false);
2593 } // if (Option.startswith("-W"))
Chad Rosier30453862011-08-18 01:18:28 +00002594 if (RemoveOption) {
Chad Rosierbc5ea3d2011-08-17 18:24:55 +00002595 it = CmdArgs.erase(it);
Chad Rosier23594f62011-08-17 18:51:56 +00002596 ie = CmdArgs.end();
Chad Rosier6fdf38b2011-08-17 23:08:45 +00002597 } else {
Chad Rosierbc5ea3d2011-08-17 18:24:55 +00002598 ++it;
Chad Rosier6fdf38b2011-08-17 23:08:45 +00002599 }
Chad Rosierbc5ea3d2011-08-17 18:24:55 +00002600 }
2601}
2602
Daniel Dunbard067f7f2009-04-08 23:54:23 +00002603void darwin::CC1::AddCC1Args(const ArgList &Args,
Daniel Dunbarafec1f52009-03-29 18:40:18 +00002604 ArgStringList &CmdArgs) const {
Daniel Dunbar083edf72009-12-21 18:54:17 +00002605 const Driver &D = getToolChain().getDriver();
Daniel Dunbar4eadb602009-09-10 01:21:12 +00002606
2607 CheckCodeGenerationOptions(D, Args);
2608
Daniel Dunbarafec1f52009-03-29 18:40:18 +00002609 // Derived from cc1 spec.
Daniel Dunbarafec1f52009-03-29 18:40:18 +00002610 if (!Args.hasArg(options::OPT_mkernel) && !Args.hasArg(options::OPT_static) &&
2611 !Args.hasArg(options::OPT_mdynamic_no_pic))
2612 CmdArgs.push_back("-fPIC");
2613
Daniel Dunbar2ffe0292009-09-10 03:37:02 +00002614 if (getToolChain().getTriple().getArch() == llvm::Triple::arm ||
2615 getToolChain().getTriple().getArch() == llvm::Triple::thumb) {
2616 if (!Args.hasArg(options::OPT_fbuiltin_strcat))
2617 CmdArgs.push_back("-fno-builtin-strcat");
2618 if (!Args.hasArg(options::OPT_fbuiltin_strcpy))
2619 CmdArgs.push_back("-fno-builtin-strcpy");
2620 }
2621
Daniel Dunbarafec1f52009-03-29 18:40:18 +00002622 if (Args.hasArg(options::OPT_g_Flag) &&
2623 !Args.hasArg(options::OPT_fno_eliminate_unused_debug_symbols))
2624 CmdArgs.push_back("-feliminate-unused-debug-symbols");
2625}
2626
2627void darwin::CC1::AddCC1OptionsArgs(const ArgList &Args, ArgStringList &CmdArgs,
2628 const InputInfoList &Inputs,
2629 const ArgStringList &OutputArgs) const {
Daniel Dunbar083edf72009-12-21 18:54:17 +00002630 const Driver &D = getToolChain().getDriver();
Daniel Dunbarafec1f52009-03-29 18:40:18 +00002631
2632 // Derived from cc1_options spec.
2633 if (Args.hasArg(options::OPT_fast) ||
2634 Args.hasArg(options::OPT_fastf) ||
2635 Args.hasArg(options::OPT_fastcp))
2636 CmdArgs.push_back("-O3");
Daniel Dunbard067f7f2009-04-08 23:54:23 +00002637
Daniel Dunbarafec1f52009-03-29 18:40:18 +00002638 if (Arg *A = Args.getLastArg(options::OPT_pg))
2639 if (Args.hasArg(options::OPT_fomit_frame_pointer))
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002640 D.Diag(diag::err_drv_argument_not_allowed_with)
Daniel Dunbarafec1f52009-03-29 18:40:18 +00002641 << A->getAsString(Args) << "-fomit-frame-pointer";
2642
2643 AddCC1Args(Args, CmdArgs);
2644
2645 if (!Args.hasArg(options::OPT_Q))
2646 CmdArgs.push_back("-quiet");
2647
2648 CmdArgs.push_back("-dumpbase");
Daniel Dunbar52e96cc2009-03-30 00:34:04 +00002649 CmdArgs.push_back(darwin::CC1::getBaseInputName(Args, Inputs));
Daniel Dunbarafec1f52009-03-29 18:40:18 +00002650
2651 Args.AddAllArgs(CmdArgs, options::OPT_d_Group);
2652
2653 Args.AddAllArgs(CmdArgs, options::OPT_m_Group);
2654 Args.AddAllArgs(CmdArgs, options::OPT_a_Group);
2655
2656 // FIXME: The goal is to use the user provided -o if that is our
2657 // final output, otherwise to drive from the original input
2658 // name. Find a clean way to go about this.
2659 if ((Args.hasArg(options::OPT_c) || Args.hasArg(options::OPT_S)) &&
2660 Args.hasArg(options::OPT_o)) {
2661 Arg *OutputOpt = Args.getLastArg(options::OPT_o);
2662 CmdArgs.push_back("-auxbase-strip");
2663 CmdArgs.push_back(OutputOpt->getValue(Args));
2664 } else {
2665 CmdArgs.push_back("-auxbase");
Daniel Dunbar52e96cc2009-03-30 00:34:04 +00002666 CmdArgs.push_back(darwin::CC1::getBaseInputStem(Args, Inputs));
Daniel Dunbarafec1f52009-03-29 18:40:18 +00002667 }
2668
2669 Args.AddAllArgs(CmdArgs, options::OPT_g_Group);
2670
2671 Args.AddAllArgs(CmdArgs, options::OPT_O);
2672 // FIXME: -Wall is getting some special treatment. Investigate.
2673 Args.AddAllArgs(CmdArgs, options::OPT_W_Group, options::OPT_pedantic_Group);
2674 Args.AddLastArg(CmdArgs, options::OPT_w);
Daniel Dunbard067f7f2009-04-08 23:54:23 +00002675 Args.AddAllArgs(CmdArgs, options::OPT_std_EQ, options::OPT_ansi,
Daniel Dunbarafec1f52009-03-29 18:40:18 +00002676 options::OPT_trigraphs);
Daniel Dunbar72a60902009-04-26 01:10:38 +00002677 if (!Args.getLastArg(options::OPT_std_EQ, options::OPT_ansi)) {
2678 // Honor -std-default.
2679 Args.AddAllArgsTranslated(CmdArgs, options::OPT_std_default_EQ,
2680 "-std=", /*Joined=*/true);
2681 }
2682
Daniel Dunbarafec1f52009-03-29 18:40:18 +00002683 if (Args.hasArg(options::OPT_v))
2684 CmdArgs.push_back("-version");
Daniel Dunbar733b0f82011-03-01 18:49:30 +00002685 if (Args.hasArg(options::OPT_pg) &&
2686 getToolChain().SupportsProfiling())
Daniel Dunbarafec1f52009-03-29 18:40:18 +00002687 CmdArgs.push_back("-p");
2688 Args.AddLastArg(CmdArgs, options::OPT_p);
Daniel Dunbard067f7f2009-04-08 23:54:23 +00002689
Daniel Dunbarafec1f52009-03-29 18:40:18 +00002690 // The driver treats -fsyntax-only specially.
Daniel Dunbar2ffe0292009-09-10 03:37:02 +00002691 if (getToolChain().getTriple().getArch() == llvm::Triple::arm ||
2692 getToolChain().getTriple().getArch() == llvm::Triple::thumb) {
2693 // Removes -fbuiltin-str{cat,cpy}; these aren't recognized by cc1 but are
2694 // used to inhibit the default -fno-builtin-str{cat,cpy}.
2695 //
2696 // FIXME: Should we grow a better way to deal with "removing" args?
Daniel Dunbar44b36ee2009-11-25 11:53:23 +00002697 for (arg_iterator it = Args.filtered_begin(options::OPT_f_Group,
2698 options::OPT_fsyntax_only),
2699 ie = Args.filtered_end(); it != ie; ++it) {
Daniel Dunbara442fd52010-06-11 22:00:13 +00002700 if (!(*it)->getOption().matches(options::OPT_fbuiltin_strcat) &&
2701 !(*it)->getOption().matches(options::OPT_fbuiltin_strcpy)) {
2702 (*it)->claim();
2703 (*it)->render(Args, CmdArgs);
Daniel Dunbar2ffe0292009-09-10 03:37:02 +00002704 }
2705 }
2706 } else
2707 Args.AddAllArgs(CmdArgs, options::OPT_f_Group, options::OPT_fsyntax_only);
Daniel Dunbard067f7f2009-04-08 23:54:23 +00002708
Daniel Dunbarf28c2ff2011-04-07 20:41:03 +00002709 // Claim Clang only -f options, they aren't worth warning about.
2710 Args.ClaimAllArgs(options::OPT_f_clang_Group);
2711
Daniel Dunbarafec1f52009-03-29 18:40:18 +00002712 Args.AddAllArgs(CmdArgs, options::OPT_undef);
2713 if (Args.hasArg(options::OPT_Qn))
2714 CmdArgs.push_back("-fno-ident");
Daniel Dunbard067f7f2009-04-08 23:54:23 +00002715
Daniel Dunbarafec1f52009-03-29 18:40:18 +00002716 // FIXME: This isn't correct.
2717 //Args.AddLastArg(CmdArgs, options::OPT__help)
2718 //Args.AddLastArg(CmdArgs, options::OPT__targetHelp)
2719
2720 CmdArgs.append(OutputArgs.begin(), OutputArgs.end());
2721
2722 // FIXME: Still don't get what is happening here. Investigate.
2723 Args.AddAllArgs(CmdArgs, options::OPT__param);
2724
2725 if (Args.hasArg(options::OPT_fmudflap) ||
2726 Args.hasArg(options::OPT_fmudflapth)) {
2727 CmdArgs.push_back("-fno-builtin");
2728 CmdArgs.push_back("-fno-merge-constants");
2729 }
Daniel Dunbard067f7f2009-04-08 23:54:23 +00002730
Daniel Dunbarafec1f52009-03-29 18:40:18 +00002731 if (Args.hasArg(options::OPT_coverage)) {
2732 CmdArgs.push_back("-fprofile-arcs");
2733 CmdArgs.push_back("-ftest-coverage");
2734 }
2735
2736 if (types::isCXX(Inputs[0].getType()))
2737 CmdArgs.push_back("-D__private_extern__=extern");
2738}
2739
2740void darwin::CC1::AddCPPOptionsArgs(const ArgList &Args, ArgStringList &CmdArgs,
2741 const InputInfoList &Inputs,
2742 const ArgStringList &OutputArgs) const {
2743 // Derived from cpp_options
2744 AddCPPUniqueOptionsArgs(Args, CmdArgs, Inputs);
Daniel Dunbard067f7f2009-04-08 23:54:23 +00002745
Daniel Dunbarafec1f52009-03-29 18:40:18 +00002746 CmdArgs.append(OutputArgs.begin(), OutputArgs.end());
2747
2748 AddCC1Args(Args, CmdArgs);
2749
2750 // NOTE: The code below has some commonality with cpp_options, but
2751 // in classic gcc style ends up sending things in different
2752 // orders. This may be a good merge candidate once we drop pedantic
2753 // compatibility.
2754
2755 Args.AddAllArgs(CmdArgs, options::OPT_m_Group);
Daniel Dunbard067f7f2009-04-08 23:54:23 +00002756 Args.AddAllArgs(CmdArgs, options::OPT_std_EQ, options::OPT_ansi,
Daniel Dunbarafec1f52009-03-29 18:40:18 +00002757 options::OPT_trigraphs);
Daniel Dunbar72a60902009-04-26 01:10:38 +00002758 if (!Args.getLastArg(options::OPT_std_EQ, options::OPT_ansi)) {
2759 // Honor -std-default.
2760 Args.AddAllArgsTranslated(CmdArgs, options::OPT_std_default_EQ,
2761 "-std=", /*Joined=*/true);
2762 }
Daniel Dunbarafec1f52009-03-29 18:40:18 +00002763 Args.AddAllArgs(CmdArgs, options::OPT_W_Group, options::OPT_pedantic_Group);
2764 Args.AddLastArg(CmdArgs, options::OPT_w);
Daniel Dunbard067f7f2009-04-08 23:54:23 +00002765
Daniel Dunbarafec1f52009-03-29 18:40:18 +00002766 // The driver treats -fsyntax-only specially.
2767 Args.AddAllArgs(CmdArgs, options::OPT_f_Group, options::OPT_fsyntax_only);
2768
Daniel Dunbarf28c2ff2011-04-07 20:41:03 +00002769 // Claim Clang only -f options, they aren't worth warning about.
2770 Args.ClaimAllArgs(options::OPT_f_clang_Group);
2771
Daniel Dunbarafec1f52009-03-29 18:40:18 +00002772 if (Args.hasArg(options::OPT_g_Group) && !Args.hasArg(options::OPT_g0) &&
2773 !Args.hasArg(options::OPT_fno_working_directory))
2774 CmdArgs.push_back("-fworking-directory");
2775
2776 Args.AddAllArgs(CmdArgs, options::OPT_O);
2777 Args.AddAllArgs(CmdArgs, options::OPT_undef);
2778 if (Args.hasArg(options::OPT_save_temps))
2779 CmdArgs.push_back("-fpch-preprocess");
2780}
2781
Daniel Dunbard067f7f2009-04-08 23:54:23 +00002782void darwin::CC1::AddCPPUniqueOptionsArgs(const ArgList &Args,
Daniel Dunbarafec1f52009-03-29 18:40:18 +00002783 ArgStringList &CmdArgs,
Mike Stump11289f42009-09-09 15:08:12 +00002784 const InputInfoList &Inputs) const {
Daniel Dunbar083edf72009-12-21 18:54:17 +00002785 const Driver &D = getToolChain().getDriver();
Daniel Dunbarafec1f52009-03-29 18:40:18 +00002786
Daniel Dunbar64198ef2009-09-10 01:21:05 +00002787 CheckPreprocessingOptions(D, Args);
2788
Daniel Dunbarafec1f52009-03-29 18:40:18 +00002789 // Derived from cpp_unique_options.
Daniel Dunbar64198ef2009-09-10 01:21:05 +00002790 // -{C,CC} only with -E is checked in CheckPreprocessingOptions().
2791 Args.AddLastArg(CmdArgs, options::OPT_C);
2792 Args.AddLastArg(CmdArgs, options::OPT_CC);
Daniel Dunbarafec1f52009-03-29 18:40:18 +00002793 if (!Args.hasArg(options::OPT_Q))
2794 CmdArgs.push_back("-quiet");
2795 Args.AddAllArgs(CmdArgs, options::OPT_nostdinc);
Douglas Gregor64b046f2010-03-24 20:13:48 +00002796 Args.AddAllArgs(CmdArgs, options::OPT_nostdincxx);
Daniel Dunbarafec1f52009-03-29 18:40:18 +00002797 Args.AddLastArg(CmdArgs, options::OPT_v);
2798 Args.AddAllArgs(CmdArgs, options::OPT_I_Group, options::OPT_F);
2799 Args.AddLastArg(CmdArgs, options::OPT_P);
2800
2801 // FIXME: Handle %I properly.
2802 if (getToolChain().getArchName() == "x86_64") {
2803 CmdArgs.push_back("-imultilib");
2804 CmdArgs.push_back("x86_64");
2805 }
2806
2807 if (Args.hasArg(options::OPT_MD)) {
2808 CmdArgs.push_back("-MD");
Daniel Dunbar52e96cc2009-03-30 00:34:04 +00002809 CmdArgs.push_back(darwin::CC1::getDependencyFileName(Args, Inputs));
Daniel Dunbarafec1f52009-03-29 18:40:18 +00002810 }
2811
2812 if (Args.hasArg(options::OPT_MMD)) {
2813 CmdArgs.push_back("-MMD");
Daniel Dunbar52e96cc2009-03-30 00:34:04 +00002814 CmdArgs.push_back(darwin::CC1::getDependencyFileName(Args, Inputs));
Daniel Dunbarafec1f52009-03-29 18:40:18 +00002815 }
2816
2817 Args.AddLastArg(CmdArgs, options::OPT_M);
2818 Args.AddLastArg(CmdArgs, options::OPT_MM);
2819 Args.AddAllArgs(CmdArgs, options::OPT_MF);
2820 Args.AddLastArg(CmdArgs, options::OPT_MG);
2821 Args.AddLastArg(CmdArgs, options::OPT_MP);
2822 Args.AddAllArgs(CmdArgs, options::OPT_MQ);
2823 Args.AddAllArgs(CmdArgs, options::OPT_MT);
2824 if (!Args.hasArg(options::OPT_M) && !Args.hasArg(options::OPT_MM) &&
2825 (Args.hasArg(options::OPT_MD) || Args.hasArg(options::OPT_MMD))) {
2826 if (Arg *OutputOpt = Args.getLastArg(options::OPT_o)) {
2827 CmdArgs.push_back("-MQ");
2828 CmdArgs.push_back(OutputOpt->getValue(Args));
2829 }
2830 }
2831
2832 Args.AddLastArg(CmdArgs, options::OPT_remap);
2833 if (Args.hasArg(options::OPT_g3))
2834 CmdArgs.push_back("-dD");
2835 Args.AddLastArg(CmdArgs, options::OPT_H);
2836
2837 AddCPPArgs(Args, CmdArgs);
2838
2839 Args.AddAllArgs(CmdArgs, options::OPT_D, options::OPT_U, options::OPT_A);
2840 Args.AddAllArgs(CmdArgs, options::OPT_i_Group);
2841
2842 for (InputInfoList::const_iterator
2843 it = Inputs.begin(), ie = Inputs.end(); it != ie; ++it) {
2844 const InputInfo &II = *it;
Daniel Dunbard067f7f2009-04-08 23:54:23 +00002845
Daniel Dunbarb440f562010-08-02 02:38:21 +00002846 CmdArgs.push_back(II.getFilename());
Daniel Dunbarafec1f52009-03-29 18:40:18 +00002847 }
2848
2849 Args.AddAllArgValues(CmdArgs, options::OPT_Wp_COMMA,
2850 options::OPT_Xpreprocessor);
2851
2852 if (Args.hasArg(options::OPT_fmudflap)) {
2853 CmdArgs.push_back("-D_MUDFLAP");
2854 CmdArgs.push_back("-include");
2855 CmdArgs.push_back("mf-runtime.h");
2856 }
2857
2858 if (Args.hasArg(options::OPT_fmudflapth)) {
2859 CmdArgs.push_back("-D_MUDFLAP");
2860 CmdArgs.push_back("-D_MUDFLAPTH");
2861 CmdArgs.push_back("-include");
2862 CmdArgs.push_back("mf-runtime.h");
2863 }
2864}
2865
Daniel Dunbard067f7f2009-04-08 23:54:23 +00002866void darwin::CC1::AddCPPArgs(const ArgList &Args,
Daniel Dunbarafec1f52009-03-29 18:40:18 +00002867 ArgStringList &CmdArgs) const {
2868 // Derived from cpp spec.
2869
2870 if (Args.hasArg(options::OPT_static)) {
2871 // The gcc spec is broken here, it refers to dynamic but
2872 // that has been translated. Start by being bug compatible.
Daniel Dunbard067f7f2009-04-08 23:54:23 +00002873
Daniel Dunbarafec1f52009-03-29 18:40:18 +00002874 // if (!Args.hasArg(arglist.parser.dynamicOption))
2875 CmdArgs.push_back("-D__STATIC__");
2876 } else
2877 CmdArgs.push_back("-D__DYNAMIC__");
2878
2879 if (Args.hasArg(options::OPT_pthread))
2880 CmdArgs.push_back("-D_REENTRANT");
2881}
2882
Daniel Dunbare6adeee2009-03-29 17:08:39 +00002883void darwin::Preprocess::ConstructJob(Compilation &C, const JobAction &JA,
Daniel Dunbar7fbaf532010-08-02 02:38:28 +00002884 const InputInfo &Output,
Daniel Dunbard067f7f2009-04-08 23:54:23 +00002885 const InputInfoList &Inputs,
2886 const ArgList &Args,
Daniel Dunbare6adeee2009-03-29 17:08:39 +00002887 const char *LinkingOutput) const {
2888 ArgStringList CmdArgs;
2889
2890 assert(Inputs.size() == 1 && "Unexpected number of inputs!");
2891
2892 CmdArgs.push_back("-E");
2893
2894 if (Args.hasArg(options::OPT_traditional) ||
Daniel Dunbare6adeee2009-03-29 17:08:39 +00002895 Args.hasArg(options::OPT_traditional_cpp))
2896 CmdArgs.push_back("-traditional-cpp");
Daniel Dunbard067f7f2009-04-08 23:54:23 +00002897
Daniel Dunbare6adeee2009-03-29 17:08:39 +00002898 ArgStringList OutputArgs;
Daniel Dunbarb440f562010-08-02 02:38:21 +00002899 assert(Output.isFilename() && "Unexpected CC1 output.");
2900 OutputArgs.push_back("-o");
2901 OutputArgs.push_back(Output.getFilename());
Daniel Dunbare6adeee2009-03-29 17:08:39 +00002902
Joerg Sonnenbergerb86f5f42011-03-06 23:31:01 +00002903 if (Args.hasArg(options::OPT_E) || getToolChain().getDriver().CCCIsCPP) {
Daniel Dunbarf64f5302009-03-29 22:27:40 +00002904 AddCPPOptionsArgs(Args, CmdArgs, Inputs, OutputArgs);
2905 } else {
2906 AddCPPOptionsArgs(Args, CmdArgs, Inputs, ArgStringList());
2907 CmdArgs.append(OutputArgs.begin(), OutputArgs.end());
2908 }
Daniel Dunbard067f7f2009-04-08 23:54:23 +00002909
Daniel Dunbar6a8803a2009-04-03 01:27:06 +00002910 Args.AddAllArgs(CmdArgs, options::OPT_d_Group);
2911
Chad Rosierc31e48d2011-09-08 00:38:00 +00002912 RemoveCC1UnsupportedArgs(CmdArgs);
2913
Daniel Dunbare6adeee2009-03-29 17:08:39 +00002914 const char *CC1Name = getCC1Name(Inputs[0].getType());
Daniel Dunbard067f7f2009-04-08 23:54:23 +00002915 const char *Exec =
Daniel Dunbar9c3ed5f2010-07-14 18:46:23 +00002916 Args.MakeArgString(getToolChain().GetProgramPath(CC1Name));
Daniel Dunbar7fbaf532010-08-02 02:38:28 +00002917 C.addCommand(new Command(JA, *this, Exec, CmdArgs));
Daniel Dunbare6adeee2009-03-29 17:08:39 +00002918}
2919
2920void darwin::Compile::ConstructJob(Compilation &C, const JobAction &JA,
Daniel Dunbar7fbaf532010-08-02 02:38:28 +00002921 const InputInfo &Output,
Daniel Dunbard067f7f2009-04-08 23:54:23 +00002922 const InputInfoList &Inputs,
2923 const ArgList &Args,
Daniel Dunbare6adeee2009-03-29 17:08:39 +00002924 const char *LinkingOutput) const {
Daniel Dunbar083edf72009-12-21 18:54:17 +00002925 const Driver &D = getToolChain().getDriver();
Daniel Dunbare6adeee2009-03-29 17:08:39 +00002926 ArgStringList CmdArgs;
2927
2928 assert(Inputs.size() == 1 && "Unexpected number of inputs!");
2929
2930 types::ID InputType = Inputs[0].getType();
2931 const Arg *A;
Eli Friedmanbb0d9a52009-07-14 21:58:17 +00002932 if ((A = Args.getLastArg(options::OPT_traditional)))
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002933 D.Diag(diag::err_drv_argument_only_allowed_with)
Daniel Dunbare6adeee2009-03-29 17:08:39 +00002934 << A->getAsString(Args) << "-E";
2935
Daniel Dunbar24e52992010-06-07 23:28:45 +00002936 if (JA.getType() == types::TY_LLVM_IR ||
2937 JA.getType() == types::TY_LTO_IR)
Daniel Dunbare6adeee2009-03-29 17:08:39 +00002938 CmdArgs.push_back("-emit-llvm");
Daniel Dunbar24e52992010-06-07 23:28:45 +00002939 else if (JA.getType() == types::TY_LLVM_BC ||
2940 JA.getType() == types::TY_LTO_BC)
Daniel Dunbare6adeee2009-03-29 17:08:39 +00002941 CmdArgs.push_back("-emit-llvm-bc");
Daniel Dunbar6cdf83c2009-09-01 16:57:46 +00002942 else if (Output.getType() == types::TY_AST)
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002943 D.Diag(diag::err_drv_no_ast_support)
Daniel Dunbar1c28f1e2009-09-09 22:32:48 +00002944 << getToolChain().getTripleString();
Daniel Dunbarbcd554f2010-02-11 17:33:45 +00002945 else if (JA.getType() != types::TY_PP_Asm &&
2946 JA.getType() != types::TY_PCH)
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002947 D.Diag(diag::err_drv_invalid_gcc_output_type)
Daniel Dunbaraeea8ac2010-02-11 03:16:21 +00002948 << getTypeName(JA.getType());
Daniel Dunbare6adeee2009-03-29 17:08:39 +00002949
2950 ArgStringList OutputArgs;
2951 if (Output.getType() != types::TY_PCH) {
2952 OutputArgs.push_back("-o");
Daniel Dunbarb440f562010-08-02 02:38:21 +00002953 if (Output.isNothing())
Daniel Dunbare6adeee2009-03-29 17:08:39 +00002954 OutputArgs.push_back("/dev/null");
2955 else
2956 OutputArgs.push_back(Output.getFilename());
2957 }
2958
2959 // There is no need for this level of compatibility, but it makes
2960 // diffing easier.
2961 bool OutputArgsEarly = (Args.hasArg(options::OPT_fsyntax_only) ||
2962 Args.hasArg(options::OPT_S));
2963
2964 if (types::getPreprocessedType(InputType) != types::TY_INVALID) {
Daniel Dunbarafec1f52009-03-29 18:40:18 +00002965 AddCPPUniqueOptionsArgs(Args, CmdArgs, Inputs);
Daniel Dunbare6adeee2009-03-29 17:08:39 +00002966 if (OutputArgsEarly) {
2967 AddCC1OptionsArgs(Args, CmdArgs, Inputs, OutputArgs);
2968 } else {
2969 AddCC1OptionsArgs(Args, CmdArgs, Inputs, ArgStringList());
2970 CmdArgs.append(OutputArgs.begin(), OutputArgs.end());
2971 }
2972 } else {
2973 CmdArgs.push_back("-fpreprocessed");
Daniel Dunbard067f7f2009-04-08 23:54:23 +00002974
Daniel Dunbare6adeee2009-03-29 17:08:39 +00002975 for (InputInfoList::const_iterator
2976 it = Inputs.begin(), ie = Inputs.end(); it != ie; ++it) {
2977 const InputInfo &II = *it;
2978
Daniel Dunbar6cdf83c2009-09-01 16:57:46 +00002979 // Reject AST inputs.
2980 if (II.getType() == types::TY_AST) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002981 D.Diag(diag::err_drv_no_ast_support)
Daniel Dunbar1c28f1e2009-09-09 22:32:48 +00002982 << getToolChain().getTripleString();
Daniel Dunbar6cdf83c2009-09-01 16:57:46 +00002983 return;
2984 }
2985
Daniel Dunbarb440f562010-08-02 02:38:21 +00002986 CmdArgs.push_back(II.getFilename());
Daniel Dunbare6adeee2009-03-29 17:08:39 +00002987 }
2988
2989 if (OutputArgsEarly) {
2990 AddCC1OptionsArgs(Args, CmdArgs, Inputs, OutputArgs);
2991 } else {
2992 AddCC1OptionsArgs(Args, CmdArgs, Inputs, ArgStringList());
2993 CmdArgs.append(OutputArgs.begin(), OutputArgs.end());
2994 }
2995 }
Daniel Dunbard067f7f2009-04-08 23:54:23 +00002996
Daniel Dunbare6adeee2009-03-29 17:08:39 +00002997 if (Output.getType() == types::TY_PCH) {
2998 assert(Output.isFilename() && "Invalid PCH output.");
2999
3000 CmdArgs.push_back("-o");
3001 // NOTE: gcc uses a temp .s file for this, but there doesn't seem
3002 // to be a good reason.
Chad Rosier96d690c2011-08-01 19:58:48 +00003003 const char *TmpPath = C.getArgs().MakeArgString(
Chad Rosier39ab7432011-08-26 21:28:44 +00003004 D.GetTemporaryPath("cc", "s"));
Chad Rosier96d690c2011-08-01 19:58:48 +00003005 C.addTempFile(TmpPath);
3006 CmdArgs.push_back(TmpPath);
Daniel Dunbard067f7f2009-04-08 23:54:23 +00003007
Eric Christopher84fbdb42011-08-19 00:30:14 +00003008 // If we're emitting a pch file with the last 4 characters of ".pth"
3009 // and falling back to llvm-gcc we want to use ".gch" instead.
3010 std::string OutputFile(Output.getFilename());
3011 size_t loc = OutputFile.rfind(".pth");
3012 if (loc != std::string::npos)
3013 OutputFile.replace(loc, 4, ".gch");
3014 const char *Tmp = C.getArgs().MakeArgString("--output-pch="+OutputFile);
3015 CmdArgs.push_back(Tmp);
Daniel Dunbard067f7f2009-04-08 23:54:23 +00003016 }
Daniel Dunbare6adeee2009-03-29 17:08:39 +00003017
Chad Rosierbc5ea3d2011-08-17 18:24:55 +00003018 RemoveCC1UnsupportedArgs(CmdArgs);
3019
Daniel Dunbare6adeee2009-03-29 17:08:39 +00003020 const char *CC1Name = getCC1Name(Inputs[0].getType());
Daniel Dunbard067f7f2009-04-08 23:54:23 +00003021 const char *Exec =
Daniel Dunbar9c3ed5f2010-07-14 18:46:23 +00003022 Args.MakeArgString(getToolChain().GetProgramPath(CC1Name));
Daniel Dunbar7fbaf532010-08-02 02:38:28 +00003023 C.addCommand(new Command(JA, *this, Exec, CmdArgs));
Daniel Dunbare6adeee2009-03-29 17:08:39 +00003024}
3025
Daniel Dunbarbe220842009-03-20 16:06:39 +00003026void darwin::Assemble::ConstructJob(Compilation &C, const JobAction &JA,
Daniel Dunbar7fbaf532010-08-02 02:38:28 +00003027 const InputInfo &Output,
Daniel Dunbard067f7f2009-04-08 23:54:23 +00003028 const InputInfoList &Inputs,
3029 const ArgList &Args,
Daniel Dunbarbe220842009-03-20 16:06:39 +00003030 const char *LinkingOutput) const {
3031 ArgStringList CmdArgs;
3032
3033 assert(Inputs.size() == 1 && "Unexpected number of inputs.");
3034 const InputInfo &Input = Inputs[0];
3035
Daniel Dunbardc8355e2011-04-12 23:59:20 +00003036 // Determine the original source input.
3037 const Action *SourceAction = &JA;
3038 while (SourceAction->getKind() != Action::InputClass) {
3039 assert(!SourceAction->getInputs().empty() && "unexpected root action!");
3040 SourceAction = SourceAction->getInputs()[0];
3041 }
3042
3043 // Forward -g, assuming we are dealing with an actual assembly file.
Eric Christopher84fbdb42011-08-19 00:30:14 +00003044 if (SourceAction->getType() == types::TY_Asm ||
Daniel Dunbardc8355e2011-04-12 23:59:20 +00003045 SourceAction->getType() == types::TY_PP_Asm) {
Daniel Dunbar5c9c1182009-04-01 00:27:44 +00003046 if (Args.hasArg(options::OPT_gstabs))
3047 CmdArgs.push_back("--gstabs");
3048 else if (Args.hasArg(options::OPT_g_Group))
3049 CmdArgs.push_back("--gdwarf2");
3050 }
Daniel Dunbard067f7f2009-04-08 23:54:23 +00003051
Daniel Dunbarbe220842009-03-20 16:06:39 +00003052 // Derived from asm spec.
Daniel Dunbar3571dd92009-09-09 18:36:27 +00003053 AddDarwinArch(Args, CmdArgs);
Daniel Dunbarbe220842009-03-20 16:06:39 +00003054
Daniel Dunbar6d484762010-07-22 01:47:22 +00003055 // Use -force_cpusubtype_ALL on x86 by default.
3056 if (getToolChain().getTriple().getArch() == llvm::Triple::x86 ||
3057 getToolChain().getTriple().getArch() == llvm::Triple::x86_64 ||
Daniel Dunbar3571dd92009-09-09 18:36:27 +00003058 Args.hasArg(options::OPT_force__cpusubtype__ALL))
3059 CmdArgs.push_back("-force_cpusubtype_ALL");
3060
Daniel Dunbara6b4a3d2009-08-24 22:26:16 +00003061 if (getToolChain().getTriple().getArch() != llvm::Triple::x86_64 &&
3062 (Args.hasArg(options::OPT_mkernel) ||
Daniel Dunbarbe220842009-03-20 16:06:39 +00003063 Args.hasArg(options::OPT_static) ||
Daniel Dunbara6b4a3d2009-08-24 22:26:16 +00003064 Args.hasArg(options::OPT_fapple_kext)))
Daniel Dunbard067f7f2009-04-08 23:54:23 +00003065 CmdArgs.push_back("-static");
3066
Daniel Dunbarbe220842009-03-20 16:06:39 +00003067 Args.AddAllArgValues(CmdArgs, options::OPT_Wa_COMMA,
3068 options::OPT_Xassembler);
3069
3070 assert(Output.isFilename() && "Unexpected lipo output.");
3071 CmdArgs.push_back("-o");
3072 CmdArgs.push_back(Output.getFilename());
3073
Daniel Dunbarb440f562010-08-02 02:38:21 +00003074 assert(Input.isFilename() && "Invalid input.");
3075 CmdArgs.push_back(Input.getFilename());
Daniel Dunbarbe220842009-03-20 16:06:39 +00003076
3077 // asm_final spec is empty.
3078
Daniel Dunbard067f7f2009-04-08 23:54:23 +00003079 const char *Exec =
Daniel Dunbar9c3ed5f2010-07-14 18:46:23 +00003080 Args.MakeArgString(getToolChain().GetProgramPath("as"));
Daniel Dunbar7fbaf532010-08-02 02:38:28 +00003081 C.addCommand(new Command(JA, *this, Exec, CmdArgs));
Daniel Dunbarbe220842009-03-20 16:06:39 +00003082}
Daniel Dunbar64ed5e32009-03-20 00:52:38 +00003083
Daniel Dunbare9ded432009-09-09 18:36:20 +00003084void darwin::DarwinTool::AddDarwinArch(const ArgList &Args,
3085 ArgStringList &CmdArgs) const {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003086 StringRef ArchName = getDarwinToolChain().getDarwinArchName(Args);
Daniel Dunbardcc3b652010-01-22 02:04:58 +00003087
Daniel Dunbarc1964212009-03-26 16:23:12 +00003088 // Derived from darwin_arch spec.
3089 CmdArgs.push_back("-arch");
Daniel Dunbardcc3b652010-01-22 02:04:58 +00003090 CmdArgs.push_back(Args.MakeArgString(ArchName));
Daniel Dunbar91dbfd62009-09-04 18:35:31 +00003091
Daniel Dunbardcc3b652010-01-22 02:04:58 +00003092 // FIXME: Is this needed anymore?
3093 if (ArchName == "arm")
Daniel Dunbar91dbfd62009-09-04 18:35:31 +00003094 CmdArgs.push_back("-force_cpusubtype_ALL");
Daniel Dunbarc1964212009-03-26 16:23:12 +00003095}
3096
Daniel Dunbarccbc4522010-09-09 21:51:05 +00003097void darwin::Link::AddLinkArgs(Compilation &C,
3098 const ArgList &Args,
Daniel Dunbarc1964212009-03-26 16:23:12 +00003099 ArgStringList &CmdArgs) const {
Daniel Dunbar083edf72009-12-21 18:54:17 +00003100 const Driver &D = getToolChain().getDriver();
Daniel Dunbarc44d3132011-04-28 21:23:41 +00003101 const toolchains::Darwin &DarwinTC = getDarwinToolChain();
Daniel Dunbarc1964212009-03-26 16:23:12 +00003102
Daniel Dunbarcacb0e22010-08-11 23:07:50 +00003103 unsigned Version[3] = { 0, 0, 0 };
3104 if (Arg *A = Args.getLastArg(options::OPT_mlinker_version_EQ)) {
3105 bool HadExtra;
3106 if (!Driver::GetReleaseVersion(A->getValue(Args), Version[0],
3107 Version[1], Version[2], HadExtra) ||
3108 HadExtra)
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003109 D.Diag(diag::err_drv_invalid_version_number)
Daniel Dunbarcacb0e22010-08-11 23:07:50 +00003110 << A->getAsString(Args);
3111 }
3112
3113 // Newer linkers support -demangle, pass it if supported and not disabled by
3114 // the user.
Daniel Dunbar6d776eb2010-11-19 17:51:40 +00003115 //
3116 // FIXME: We temporarily avoid passing -demangle to any iOS linker, because
3117 // unfortunately we can't be guaranteed that the linker version used there
3118 // will match the linker version detected at configure time. We need the
3119 // universal driver.
3120 if (Version[0] >= 100 && !Args.hasArg(options::OPT_Z_Xlinker__no_demangle) &&
Daniel Dunbarc44d3132011-04-28 21:23:41 +00003121 !DarwinTC.isTargetIPhoneOS()) {
Daniel Dunbar1eaf5c62010-09-07 17:07:49 +00003122 // Don't pass -demangle to ld_classic.
3123 //
3124 // FIXME: This is a temporary workaround, ld should be handling this.
3125 bool UsesLdClassic = (getToolChain().getArch() == llvm::Triple::x86 &&
3126 Args.hasArg(options::OPT_static));
Daniel Dunbar3d7e0e22010-09-07 17:50:41 +00003127 if (getToolChain().getArch() == llvm::Triple::x86) {
3128 for (arg_iterator it = Args.filtered_begin(options::OPT_Xlinker,
3129 options::OPT_Wl_COMMA),
3130 ie = Args.filtered_end(); it != ie; ++it) {
3131 const Arg *A = *it;
3132 for (unsigned i = 0, e = A->getNumValues(); i != e; ++i)
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003133 if (StringRef(A->getValue(Args, i)) == "-kext")
Daniel Dunbar3d7e0e22010-09-07 17:50:41 +00003134 UsesLdClassic = true;
3135 }
3136 }
Daniel Dunbar1eaf5c62010-09-07 17:07:49 +00003137 if (!UsesLdClassic)
3138 CmdArgs.push_back("-demangle");
Daniel Dunbarcacb0e22010-08-11 23:07:50 +00003139 }
3140
Daniel Dunbaref889c72011-06-21 20:55:11 +00003141 // If we are using LTO, then automatically create a temporary file path for
3142 // the linker to use, so that it's lifetime will extend past a possible
3143 // dsymutil step.
Daniel Dunbar3d125d32011-06-21 21:18:32 +00003144 if (Version[0] >= 116 && D.IsUsingLTO(Args)) {
Daniel Dunbaref889c72011-06-21 20:55:11 +00003145 const char *TmpPath = C.getArgs().MakeArgString(
Chad Rosier39ab7432011-08-26 21:28:44 +00003146 D.GetTemporaryPath("cc", types::getTypeTempSuffix(types::TY_Object)));
Daniel Dunbaref889c72011-06-21 20:55:11 +00003147 C.addTempFile(TmpPath);
3148 CmdArgs.push_back("-object_path_lto");
3149 CmdArgs.push_back(TmpPath);
3150 }
3151
Daniel Dunbarc1964212009-03-26 16:23:12 +00003152 // Derived from the "link" spec.
3153 Args.AddAllArgs(CmdArgs, options::OPT_static);
3154 if (!Args.hasArg(options::OPT_static))
3155 CmdArgs.push_back("-dynamic");
3156 if (Args.hasArg(options::OPT_fgnu_runtime)) {
3157 // FIXME: gcc replaces -lobjc in forward args with -lobjc-gnu
3158 // here. How do we wish to handle such things?
3159 }
Daniel Dunbard067f7f2009-04-08 23:54:23 +00003160
Daniel Dunbarc1964212009-03-26 16:23:12 +00003161 if (!Args.hasArg(options::OPT_dynamiclib)) {
Daniel Dunbara48823f2010-01-22 02:04:52 +00003162 AddDarwinArch(Args, CmdArgs);
Daniel Dunbara48823f2010-01-22 02:04:52 +00003163 // FIXME: Why do this only on this path?
Daniel Dunbar93d7acf2010-01-22 03:37:33 +00003164 Args.AddLastArg(CmdArgs, options::OPT_force__cpusubtype__ALL);
Daniel Dunbarc1964212009-03-26 16:23:12 +00003165
3166 Args.AddLastArg(CmdArgs, options::OPT_bundle);
3167 Args.AddAllArgs(CmdArgs, options::OPT_bundle__loader);
3168 Args.AddAllArgs(CmdArgs, options::OPT_client__name);
3169
3170 Arg *A;
3171 if ((A = Args.getLastArg(options::OPT_compatibility__version)) ||
3172 (A = Args.getLastArg(options::OPT_current__version)) ||
3173 (A = Args.getLastArg(options::OPT_install__name)))
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003174 D.Diag(diag::err_drv_argument_only_allowed_with)
Daniel Dunbarc1964212009-03-26 16:23:12 +00003175 << A->getAsString(Args) << "-dynamiclib";
3176
3177 Args.AddLastArg(CmdArgs, options::OPT_force__flat__namespace);
3178 Args.AddLastArg(CmdArgs, options::OPT_keep__private__externs);
3179 Args.AddLastArg(CmdArgs, options::OPT_private__bundle);
3180 } else {
3181 CmdArgs.push_back("-dylib");
3182
3183 Arg *A;
3184 if ((A = Args.getLastArg(options::OPT_bundle)) ||
3185 (A = Args.getLastArg(options::OPT_bundle__loader)) ||
3186 (A = Args.getLastArg(options::OPT_client__name)) ||
3187 (A = Args.getLastArg(options::OPT_force__flat__namespace)) ||
3188 (A = Args.getLastArg(options::OPT_keep__private__externs)) ||
3189 (A = Args.getLastArg(options::OPT_private__bundle)))
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003190 D.Diag(diag::err_drv_argument_not_allowed_with)
Daniel Dunbarc1964212009-03-26 16:23:12 +00003191 << A->getAsString(Args) << "-dynamiclib";
Daniel Dunbard067f7f2009-04-08 23:54:23 +00003192
Daniel Dunbarc1964212009-03-26 16:23:12 +00003193 Args.AddAllArgsTranslated(CmdArgs, options::OPT_compatibility__version,
3194 "-dylib_compatibility_version");
3195 Args.AddAllArgsTranslated(CmdArgs, options::OPT_current__version,
3196 "-dylib_current_version");
3197
Daniel Dunbara48823f2010-01-22 02:04:52 +00003198 AddDarwinArch(Args, CmdArgs);
Daniel Dunbarc1964212009-03-26 16:23:12 +00003199
3200 Args.AddAllArgsTranslated(CmdArgs, options::OPT_install__name,
3201 "-dylib_install_name");
3202 }
3203
3204 Args.AddLastArg(CmdArgs, options::OPT_all__load);
3205 Args.AddAllArgs(CmdArgs, options::OPT_allowable__client);
3206 Args.AddLastArg(CmdArgs, options::OPT_bind__at__load);
Daniel Dunbarc44d3132011-04-28 21:23:41 +00003207 if (DarwinTC.isTargetIPhoneOS())
Daniel Dunbar2b5f6812009-09-04 18:35:41 +00003208 Args.AddLastArg(CmdArgs, options::OPT_arch__errors__fatal);
Daniel Dunbarc1964212009-03-26 16:23:12 +00003209 Args.AddLastArg(CmdArgs, options::OPT_dead__strip);
3210 Args.AddLastArg(CmdArgs, options::OPT_no__dead__strip__inits__and__terms);
3211 Args.AddAllArgs(CmdArgs, options::OPT_dylib__file);
3212 Args.AddLastArg(CmdArgs, options::OPT_dynamic);
3213 Args.AddAllArgs(CmdArgs, options::OPT_exported__symbols__list);
3214 Args.AddLastArg(CmdArgs, options::OPT_flat__namespace);
Daniel Dunbar044a3902011-06-28 20:16:02 +00003215 Args.AddAllArgs(CmdArgs, options::OPT_force__load);
Daniel Dunbarc1964212009-03-26 16:23:12 +00003216 Args.AddAllArgs(CmdArgs, options::OPT_headerpad__max__install__names);
3217 Args.AddAllArgs(CmdArgs, options::OPT_image__base);
3218 Args.AddAllArgs(CmdArgs, options::OPT_init);
3219
Daniel Dunbarc44d3132011-04-28 21:23:41 +00003220 // Add the deployment target.
3221 unsigned TargetVersion[3];
3222 DarwinTC.getTargetVersion(TargetVersion);
Daniel Dunbar72ceb922011-04-30 04:22:58 +00003223
3224 // If we had an explicit -mios-simulator-version-min argument, honor that,
3225 // otherwise use the traditional deployment targets. We can't just check the
3226 // is-sim attribute because existing code follows this path, and the linker
3227 // may not handle the argument.
3228 //
3229 // FIXME: We may be able to remove this, once we can verify no one depends on
3230 // it.
3231 if (Args.hasArg(options::OPT_mios_simulator_version_min_EQ))
3232 CmdArgs.push_back("-ios_simulator_version_min");
3233 else if (DarwinTC.isTargetIPhoneOS())
3234 CmdArgs.push_back("-iphoneos_version_min");
3235 else
3236 CmdArgs.push_back("-macosx_version_min");
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003237 CmdArgs.push_back(Args.MakeArgString(Twine(TargetVersion[0]) + "." +
3238 Twine(TargetVersion[1]) + "." +
3239 Twine(TargetVersion[2])));
Daniel Dunbarc44d3132011-04-28 21:23:41 +00003240
Daniel Dunbarc1964212009-03-26 16:23:12 +00003241 Args.AddLastArg(CmdArgs, options::OPT_nomultidefs);
3242 Args.AddLastArg(CmdArgs, options::OPT_multi__module);
3243 Args.AddLastArg(CmdArgs, options::OPT_single__module);
3244 Args.AddAllArgs(CmdArgs, options::OPT_multiply__defined);
3245 Args.AddAllArgs(CmdArgs, options::OPT_multiply__defined__unused);
Daniel Dunbard067f7f2009-04-08 23:54:23 +00003246
Daniel Dunbaraf68a882010-07-13 23:31:40 +00003247 if (const Arg *A = Args.getLastArg(options::OPT_fpie, options::OPT_fPIE,
3248 options::OPT_fno_pie,
3249 options::OPT_fno_PIE)) {
3250 if (A->getOption().matches(options::OPT_fpie) ||
3251 A->getOption().matches(options::OPT_fPIE))
3252 CmdArgs.push_back("-pie");
3253 else
3254 CmdArgs.push_back("-no_pie");
3255 }
Daniel Dunbarc1964212009-03-26 16:23:12 +00003256
3257 Args.AddLastArg(CmdArgs, options::OPT_prebind);
3258 Args.AddLastArg(CmdArgs, options::OPT_noprebind);
3259 Args.AddLastArg(CmdArgs, options::OPT_nofixprebinding);
3260 Args.AddLastArg(CmdArgs, options::OPT_prebind__all__twolevel__modules);
3261 Args.AddLastArg(CmdArgs, options::OPT_read__only__relocs);
3262 Args.AddAllArgs(CmdArgs, options::OPT_sectcreate);
3263 Args.AddAllArgs(CmdArgs, options::OPT_sectorder);
3264 Args.AddAllArgs(CmdArgs, options::OPT_seg1addr);
3265 Args.AddAllArgs(CmdArgs, options::OPT_segprot);
3266 Args.AddAllArgs(CmdArgs, options::OPT_segaddr);
3267 Args.AddAllArgs(CmdArgs, options::OPT_segs__read__only__addr);
3268 Args.AddAllArgs(CmdArgs, options::OPT_segs__read__write__addr);
3269 Args.AddAllArgs(CmdArgs, options::OPT_seg__addr__table);
3270 Args.AddAllArgs(CmdArgs, options::OPT_seg__addr__table__filename);
3271 Args.AddAllArgs(CmdArgs, options::OPT_sub__library);
3272 Args.AddAllArgs(CmdArgs, options::OPT_sub__umbrella);
Daniel Dunbar2b5f6812009-09-04 18:35:41 +00003273
Daniel Dunbar84384642011-05-02 21:03:47 +00003274 // Give --sysroot= preference, over the Apple specific behavior to also use
3275 // --isysroot as the syslibroot.
3276 if (const Arg *A = Args.getLastArg(options::OPT__sysroot_EQ)) {
3277 CmdArgs.push_back("-syslibroot");
3278 CmdArgs.push_back(A->getValue(Args));
3279 } else if (const Arg *A = Args.getLastArg(options::OPT_isysroot)) {
3280 CmdArgs.push_back("-syslibroot");
3281 CmdArgs.push_back(A->getValue(Args));
3282 } else if (getDarwinToolChain().isTargetIPhoneOS()) {
3283 CmdArgs.push_back("-syslibroot");
3284 CmdArgs.push_back("/Developer/SDKs/Extra");
Daniel Dunbar2b5f6812009-09-04 18:35:41 +00003285 }
3286
Daniel Dunbarc1964212009-03-26 16:23:12 +00003287 Args.AddLastArg(CmdArgs, options::OPT_twolevel__namespace);
3288 Args.AddLastArg(CmdArgs, options::OPT_twolevel__namespace__hints);
3289 Args.AddAllArgs(CmdArgs, options::OPT_umbrella);
3290 Args.AddAllArgs(CmdArgs, options::OPT_undefined);
3291 Args.AddAllArgs(CmdArgs, options::OPT_unexported__symbols__list);
Daniel Dunbar2b5f6812009-09-04 18:35:41 +00003292 Args.AddAllArgs(CmdArgs, options::OPT_weak__reference__mismatches);
Daniel Dunbarc1964212009-03-26 16:23:12 +00003293 Args.AddLastArg(CmdArgs, options::OPT_X_Flag);
3294 Args.AddAllArgs(CmdArgs, options::OPT_y);
3295 Args.AddLastArg(CmdArgs, options::OPT_w);
3296 Args.AddAllArgs(CmdArgs, options::OPT_pagezero__size);
3297 Args.AddAllArgs(CmdArgs, options::OPT_segs__read__);
3298 Args.AddLastArg(CmdArgs, options::OPT_seglinkedit);
3299 Args.AddLastArg(CmdArgs, options::OPT_noseglinkedit);
3300 Args.AddAllArgs(CmdArgs, options::OPT_sectalign);
3301 Args.AddAllArgs(CmdArgs, options::OPT_sectobjectsymbols);
3302 Args.AddAllArgs(CmdArgs, options::OPT_segcreate);
3303 Args.AddLastArg(CmdArgs, options::OPT_whyload);
3304 Args.AddLastArg(CmdArgs, options::OPT_whatsloaded);
3305 Args.AddAllArgs(CmdArgs, options::OPT_dylinker__install__name);
3306 Args.AddLastArg(CmdArgs, options::OPT_dylinker);
3307 Args.AddLastArg(CmdArgs, options::OPT_Mach);
3308}
3309
3310void darwin::Link::ConstructJob(Compilation &C, const JobAction &JA,
Daniel Dunbar7fbaf532010-08-02 02:38:28 +00003311 const InputInfo &Output,
Daniel Dunbard067f7f2009-04-08 23:54:23 +00003312 const InputInfoList &Inputs,
3313 const ArgList &Args,
Daniel Dunbarc1964212009-03-26 16:23:12 +00003314 const char *LinkingOutput) const {
3315 assert(Output.getType() == types::TY_Image && "Invalid linker output type.");
Daniel Dunbarc09988d2009-09-08 16:39:16 +00003316
Daniel Dunbarc1964212009-03-26 16:23:12 +00003317 // The logic here is derived from gcc's behavior; most of which
3318 // comes from specs (starting with link_command). Consult gcc for
3319 // more information.
Daniel Dunbarc1964212009-03-26 16:23:12 +00003320 ArgStringList CmdArgs;
3321
3322 // I'm not sure why this particular decomposition exists in gcc, but
3323 // we follow suite for ease of comparison.
Daniel Dunbarccbc4522010-09-09 21:51:05 +00003324 AddLinkArgs(C, Args, CmdArgs);
Daniel Dunbarc1964212009-03-26 16:23:12 +00003325
Daniel Dunbarc1964212009-03-26 16:23:12 +00003326 Args.AddAllArgs(CmdArgs, options::OPT_d_Flag);
3327 Args.AddAllArgs(CmdArgs, options::OPT_s);
3328 Args.AddAllArgs(CmdArgs, options::OPT_t);
3329 Args.AddAllArgs(CmdArgs, options::OPT_Z_Flag);
3330 Args.AddAllArgs(CmdArgs, options::OPT_u_Group);
3331 Args.AddAllArgs(CmdArgs, options::OPT_A);
3332 Args.AddLastArg(CmdArgs, options::OPT_e);
3333 Args.AddAllArgs(CmdArgs, options::OPT_m_Separate);
3334 Args.AddAllArgs(CmdArgs, options::OPT_r);
3335
Daniel Dunbar767bbab2010-10-18 22:08:36 +00003336 // Forward -ObjC when either -ObjC or -ObjC++ is used, to force loading
3337 // members of static archive libraries which implement Objective-C classes or
3338 // categories.
3339 if (Args.hasArg(options::OPT_ObjC) || Args.hasArg(options::OPT_ObjCXX))
3340 CmdArgs.push_back("-ObjC");
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00003341
Daniel Dunbarc1964212009-03-26 16:23:12 +00003342 CmdArgs.push_back("-o");
3343 CmdArgs.push_back(Output.getFilename());
3344
Daniel Dunbarc1964212009-03-26 16:23:12 +00003345 if (!Args.hasArg(options::OPT_A) &&
3346 !Args.hasArg(options::OPT_nostdlib) &&
3347 !Args.hasArg(options::OPT_nostartfiles)) {
3348 // Derived from startfile spec.
3349 if (Args.hasArg(options::OPT_dynamiclib)) {
Daniel Dunbard067f7f2009-04-08 23:54:23 +00003350 // Derived from darwin_dylib1 spec.
Daniel Dunbar16d97092011-04-01 21:02:42 +00003351 if (getDarwinToolChain().isTargetIOSSimulator()) {
3352 // The simulator doesn't have a versioned crt1 file.
3353 CmdArgs.push_back("-ldylib1.o");
3354 } else if (getDarwinToolChain().isTargetIPhoneOS()) {
Daniel Dunbar83608032010-01-27 00:56:56 +00003355 if (getDarwinToolChain().isIPhoneOSVersionLT(3, 1))
3356 CmdArgs.push_back("-ldylib1.o");
3357 } else {
Daniel Dunbar6d23b2f2010-01-27 00:57:03 +00003358 if (getDarwinToolChain().isMacosxVersionLT(10, 5))
Daniel Dunbar83608032010-01-27 00:56:56 +00003359 CmdArgs.push_back("-ldylib1.o");
Daniel Dunbar6d23b2f2010-01-27 00:57:03 +00003360 else if (getDarwinToolChain().isMacosxVersionLT(10, 6))
Daniel Dunbar83608032010-01-27 00:56:56 +00003361 CmdArgs.push_back("-ldylib1.10.5.o");
3362 }
Daniel Dunbarc1964212009-03-26 16:23:12 +00003363 } else {
3364 if (Args.hasArg(options::OPT_bundle)) {
Daniel Dunbarae8bca02009-04-01 03:17:40 +00003365 if (!Args.hasArg(options::OPT_static)) {
3366 // Derived from darwin_bundle1 spec.
Daniel Dunbar16d97092011-04-01 21:02:42 +00003367 if (getDarwinToolChain().isTargetIOSSimulator()) {
3368 // The simulator doesn't have a versioned crt1 file.
3369 CmdArgs.push_back("-lbundle1.o");
3370 } else if (getDarwinToolChain().isTargetIPhoneOS()) {
Daniel Dunbar83608032010-01-27 00:56:56 +00003371 if (getDarwinToolChain().isIPhoneOSVersionLT(3, 1))
3372 CmdArgs.push_back("-lbundle1.o");
3373 } else {
Daniel Dunbar6d23b2f2010-01-27 00:57:03 +00003374 if (getDarwinToolChain().isMacosxVersionLT(10, 6))
Daniel Dunbar83608032010-01-27 00:56:56 +00003375 CmdArgs.push_back("-lbundle1.o");
3376 }
Daniel Dunbarae8bca02009-04-01 03:17:40 +00003377 }
Daniel Dunbarc1964212009-03-26 16:23:12 +00003378 } else {
Daniel Dunbar733b0f82011-03-01 18:49:30 +00003379 if (Args.hasArg(options::OPT_pg) &&
3380 getToolChain().SupportsProfiling()) {
Daniel Dunbarc1964212009-03-26 16:23:12 +00003381 if (Args.hasArg(options::OPT_static) ||
3382 Args.hasArg(options::OPT_object) ||
3383 Args.hasArg(options::OPT_preload)) {
3384 CmdArgs.push_back("-lgcrt0.o");
3385 } else {
3386 CmdArgs.push_back("-lgcrt1.o");
Daniel Dunbard067f7f2009-04-08 23:54:23 +00003387
Daniel Dunbarc1964212009-03-26 16:23:12 +00003388 // darwin_crt2 spec is empty.
Daniel Dunbard067f7f2009-04-08 23:54:23 +00003389 }
Daniel Dunbarc1964212009-03-26 16:23:12 +00003390 } else {
3391 if (Args.hasArg(options::OPT_static) ||
3392 Args.hasArg(options::OPT_object) ||
3393 Args.hasArg(options::OPT_preload)) {
3394 CmdArgs.push_back("-lcrt0.o");
3395 } else {
3396 // Derived from darwin_crt1 spec.
Daniel Dunbarebc34df2011-03-31 17:12:33 +00003397 if (getDarwinToolChain().isTargetIOSSimulator()) {
3398 // The simulator doesn't have a versioned crt1 file.
3399 CmdArgs.push_back("-lcrt1.o");
3400 } else if (getDarwinToolChain().isTargetIPhoneOS()) {
Daniel Dunbar83608032010-01-27 00:56:56 +00003401 if (getDarwinToolChain().isIPhoneOSVersionLT(3, 1))
3402 CmdArgs.push_back("-lcrt1.o");
3403 else
3404 CmdArgs.push_back("-lcrt1.3.1.o");
Daniel Dunbar6d23b2f2010-01-27 00:57:03 +00003405 } else {
3406 if (getDarwinToolChain().isMacosxVersionLT(10, 5))
3407 CmdArgs.push_back("-lcrt1.o");
3408 else if (getDarwinToolChain().isMacosxVersionLT(10, 6))
3409 CmdArgs.push_back("-lcrt1.10.5.o");
3410 else
3411 CmdArgs.push_back("-lcrt1.10.6.o");
Daniel Dunbard067f7f2009-04-08 23:54:23 +00003412
Daniel Dunbar6d23b2f2010-01-27 00:57:03 +00003413 // darwin_crt2 spec is empty.
3414 }
Daniel Dunbarc1964212009-03-26 16:23:12 +00003415 }
3416 }
3417 }
3418 }
3419
Daniel Dunbar6d23b2f2010-01-27 00:57:03 +00003420 if (!getDarwinToolChain().isTargetIPhoneOS() &&
3421 Args.hasArg(options::OPT_shared_libgcc) &&
3422 getDarwinToolChain().isMacosxVersionLT(10, 5)) {
Daniel Dunbar1c28f1e2009-09-09 22:32:48 +00003423 const char *Str =
Daniel Dunbar9c3ed5f2010-07-14 18:46:23 +00003424 Args.MakeArgString(getToolChain().GetFilePath("crt3.o"));
Daniel Dunbar1c28f1e2009-09-09 22:32:48 +00003425 CmdArgs.push_back(Str);
Daniel Dunbarc1964212009-03-26 16:23:12 +00003426 }
3427 }
3428
3429 Args.AddAllArgs(CmdArgs, options::OPT_L);
Daniel Dunbard067f7f2009-04-08 23:54:23 +00003430
Daniel Dunbarc1964212009-03-26 16:23:12 +00003431 if (Args.hasArg(options::OPT_fopenmp))
3432 // This is more complicated in gcc...
3433 CmdArgs.push_back("-lgomp");
3434
Daniel Dunbar4c30b892009-09-18 08:14:36 +00003435 getDarwinToolChain().AddLinkSearchPathArgs(Args, CmdArgs);
Daniel Dunbard067f7f2009-04-08 23:54:23 +00003436
John McCall31168b02011-06-15 23:02:42 +00003437 // In ARC, if we don't have runtime support, link in the runtime
3438 // stubs. We have to do this *before* adding any of the normal
3439 // linker inputs so that its initializer gets run first.
John McCall24fc0de2011-07-06 00:26:06 +00003440 if (isObjCAutoRefCount(Args)) {
3441 ObjCRuntime runtime;
3442 getDarwinToolChain().configureObjCRuntime(runtime);
3443 if (!runtime.HasARC)
3444 getDarwinToolChain().AddLinkARCArgs(Args, CmdArgs);
3445 }
John McCall31168b02011-06-15 23:02:42 +00003446
Daniel Dunbar54423b22010-09-17 00:24:54 +00003447 AddLinkerInputs(getToolChain(), Inputs, Args, CmdArgs);
Daniel Dunbarc1964212009-03-26 16:23:12 +00003448
3449 if (LinkingOutput) {
3450 CmdArgs.push_back("-arch_multiple");
3451 CmdArgs.push_back("-final_output");
3452 CmdArgs.push_back(LinkingOutput);
3453 }
3454
Daniel Dunbarc1964212009-03-26 16:23:12 +00003455 if (Args.hasArg(options::OPT_fnested_functions))
3456 CmdArgs.push_back("-allow_stack_execute");
Daniel Dunbard067f7f2009-04-08 23:54:23 +00003457
Daniel Dunbarc1964212009-03-26 16:23:12 +00003458 if (!Args.hasArg(options::OPT_nostdlib) &&
3459 !Args.hasArg(options::OPT_nodefaultlibs)) {
Daniel Dunbar083edf72009-12-21 18:54:17 +00003460 if (getToolChain().getDriver().CCCIsCXX)
Daniel Dunbar3f7796f2010-09-17 01:20:05 +00003461 getToolChain().AddCXXStdlibLibArgs(Args, CmdArgs);
Daniel Dunbarad0f62b2009-04-08 06:06:21 +00003462
Daniel Dunbarc1964212009-03-26 16:23:12 +00003463 // link_ssp spec is empty.
3464
Daniel Dunbar26d482a2009-09-18 08:15:03 +00003465 // Let the tool chain choose which runtime library to link.
3466 getDarwinToolChain().AddLinkRuntimeLibArgs(Args, CmdArgs);
Daniel Dunbarc1964212009-03-26 16:23:12 +00003467 }
3468
3469 if (!Args.hasArg(options::OPT_A) &&
3470 !Args.hasArg(options::OPT_nostdlib) &&
3471 !Args.hasArg(options::OPT_nostartfiles)) {
3472 // endfile_spec is empty.
3473 }
3474
Bill Wendling08760582011-06-27 19:15:03 +00003475 addProfileRT(getToolChain(), Args, CmdArgs, getToolChain().getTriple());
Nick Lewycky82fe5f42011-05-24 21:54:59 +00003476
Daniel Dunbarc1964212009-03-26 16:23:12 +00003477 Args.AddAllArgs(CmdArgs, options::OPT_T_Group);
3478 Args.AddAllArgs(CmdArgs, options::OPT_F);
3479
Daniel Dunbard067f7f2009-04-08 23:54:23 +00003480 const char *Exec =
Daniel Dunbar9c3ed5f2010-07-14 18:46:23 +00003481 Args.MakeArgString(getToolChain().GetProgramPath("ld"));
Daniel Dunbar7fbaf532010-08-02 02:38:28 +00003482 C.addCommand(new Command(JA, *this, Exec, CmdArgs));
Daniel Dunbarc1964212009-03-26 16:23:12 +00003483}
3484
Daniel Dunbar64ed5e32009-03-20 00:52:38 +00003485void darwin::Lipo::ConstructJob(Compilation &C, const JobAction &JA,
Daniel Dunbar7fbaf532010-08-02 02:38:28 +00003486 const InputInfo &Output,
Daniel Dunbard067f7f2009-04-08 23:54:23 +00003487 const InputInfoList &Inputs,
3488 const ArgList &Args,
Daniel Dunbar64ed5e32009-03-20 00:52:38 +00003489 const char *LinkingOutput) const {
3490 ArgStringList CmdArgs;
3491
3492 CmdArgs.push_back("-create");
3493 assert(Output.isFilename() && "Unexpected lipo output.");
Daniel Dunbar06686ab2009-03-24 00:24:37 +00003494
3495 CmdArgs.push_back("-output");
Daniel Dunbar64ed5e32009-03-20 00:52:38 +00003496 CmdArgs.push_back(Output.getFilename());
Daniel Dunbar06686ab2009-03-24 00:24:37 +00003497
Daniel Dunbar64ed5e32009-03-20 00:52:38 +00003498 for (InputInfoList::const_iterator
3499 it = Inputs.begin(), ie = Inputs.end(); it != ie; ++it) {
3500 const InputInfo &II = *it;
3501 assert(II.isFilename() && "Unexpected lipo input.");
3502 CmdArgs.push_back(II.getFilename());
3503 }
Daniel Dunbard067f7f2009-04-08 23:54:23 +00003504 const char *Exec =
Daniel Dunbar9c3ed5f2010-07-14 18:46:23 +00003505 Args.MakeArgString(getToolChain().GetProgramPath("lipo"));
Daniel Dunbar7fbaf532010-08-02 02:38:28 +00003506 C.addCommand(new Command(JA, *this, Exec, CmdArgs));
Daniel Dunbar64ed5e32009-03-20 00:52:38 +00003507}
Daniel Dunbar8eb473c2009-03-31 17:45:15 +00003508
Daniel Dunbar88299622010-06-04 18:28:36 +00003509void darwin::Dsymutil::ConstructJob(Compilation &C, const JobAction &JA,
Daniel Dunbar7fbaf532010-08-02 02:38:28 +00003510 const InputInfo &Output,
Daniel Dunbar88299622010-06-04 18:28:36 +00003511 const InputInfoList &Inputs,
3512 const ArgList &Args,
3513 const char *LinkingOutput) const {
3514 ArgStringList CmdArgs;
3515
Daniel Dunbareb86b042011-05-09 17:23:16 +00003516 CmdArgs.push_back("-o");
3517 CmdArgs.push_back(Output.getFilename());
3518
Daniel Dunbar88299622010-06-04 18:28:36 +00003519 assert(Inputs.size() == 1 && "Unable to handle multiple inputs.");
3520 const InputInfo &Input = Inputs[0];
3521 assert(Input.isFilename() && "Unexpected dsymutil input.");
3522 CmdArgs.push_back(Input.getFilename());
3523
Daniel Dunbar88299622010-06-04 18:28:36 +00003524 const char *Exec =
Daniel Dunbar9c3ed5f2010-07-14 18:46:23 +00003525 Args.MakeArgString(getToolChain().GetProgramPath("dsymutil"));
Daniel Dunbar7fbaf532010-08-02 02:38:28 +00003526 C.addCommand(new Command(JA, *this, Exec, CmdArgs));
Daniel Dunbar88299622010-06-04 18:28:36 +00003527}
3528
Eric Christopher551ef452011-08-23 17:56:55 +00003529void darwin::VerifyDebug::ConstructJob(Compilation &C, const JobAction &JA,
3530 const InputInfo &Output,
3531 const InputInfoList &Inputs,
3532 const ArgList &Args,
3533 const char *LinkingOutput) const {
3534 ArgStringList CmdArgs;
3535 CmdArgs.push_back("--verify");
3536
3537 assert(Inputs.size() == 1 && "Unable to handle multiple inputs.");
3538 const InputInfo &Input = Inputs[0];
3539 assert(Input.isFilename() && "Unexpected verify input");
3540
3541 // Grabbing the output of the earlier dsymutil run.
3542 CmdArgs.push_back(Input.getFilename());
3543
3544 const char *Exec =
3545 Args.MakeArgString(getToolChain().GetProgramPath("dwarfdump"));
3546 C.addCommand(new Command(JA, *this, Exec, CmdArgs));
3547}
3548
Edward O'Callaghan856e4ff2009-08-22 01:06:46 +00003549void auroraux::Assemble::ConstructJob(Compilation &C, const JobAction &JA,
Daniel Dunbar7fbaf532010-08-02 02:38:28 +00003550 const InputInfo &Output,
Daniel Dunbar1c7577c2009-11-04 06:24:38 +00003551 const InputInfoList &Inputs,
3552 const ArgList &Args,
3553 const char *LinkingOutput) const {
Edward O'Callaghan856e4ff2009-08-22 01:06:46 +00003554 ArgStringList CmdArgs;
3555
3556 Args.AddAllArgValues(CmdArgs, options::OPT_Wa_COMMA,
3557 options::OPT_Xassembler);
3558
3559 CmdArgs.push_back("-o");
Daniel Dunbarb440f562010-08-02 02:38:21 +00003560 CmdArgs.push_back(Output.getFilename());
Edward O'Callaghan856e4ff2009-08-22 01:06:46 +00003561
3562 for (InputInfoList::const_iterator
3563 it = Inputs.begin(), ie = Inputs.end(); it != ie; ++it) {
3564 const InputInfo &II = *it;
Daniel Dunbarb440f562010-08-02 02:38:21 +00003565 CmdArgs.push_back(II.getFilename());
Edward O'Callaghan856e4ff2009-08-22 01:06:46 +00003566 }
3567
3568 const char *Exec =
Daniel Dunbar9c3ed5f2010-07-14 18:46:23 +00003569 Args.MakeArgString(getToolChain().GetProgramPath("gas"));
Daniel Dunbar7fbaf532010-08-02 02:38:28 +00003570 C.addCommand(new Command(JA, *this, Exec, CmdArgs));
Edward O'Callaghan856e4ff2009-08-22 01:06:46 +00003571}
3572
3573void auroraux::Link::ConstructJob(Compilation &C, const JobAction &JA,
Daniel Dunbar7fbaf532010-08-02 02:38:28 +00003574 const InputInfo &Output,
Daniel Dunbar1c7577c2009-11-04 06:24:38 +00003575 const InputInfoList &Inputs,
3576 const ArgList &Args,
3577 const char *LinkingOutput) const {
Edward O'Callaghan856e4ff2009-08-22 01:06:46 +00003578 ArgStringList CmdArgs;
3579
3580 if ((!Args.hasArg(options::OPT_nostdlib)) &&
Daniel Dunbar1c7577c2009-11-04 06:24:38 +00003581 (!Args.hasArg(options::OPT_shared))) {
Edward O'Callaghan856e4ff2009-08-22 01:06:46 +00003582 CmdArgs.push_back("-e");
Edward O'Callaghand8712d92009-10-15 07:44:07 +00003583 CmdArgs.push_back("_start");
Edward O'Callaghan856e4ff2009-08-22 01:06:46 +00003584 }
3585
3586 if (Args.hasArg(options::OPT_static)) {
3587 CmdArgs.push_back("-Bstatic");
Edward O'Callaghand8712d92009-10-15 07:44:07 +00003588 CmdArgs.push_back("-dn");
Edward O'Callaghan856e4ff2009-08-22 01:06:46 +00003589 } else {
Edward O'Callaghand8712d92009-10-15 07:44:07 +00003590// CmdArgs.push_back("--eh-frame-hdr");
Edward O'Callaghan856e4ff2009-08-22 01:06:46 +00003591 CmdArgs.push_back("-Bdynamic");
3592 if (Args.hasArg(options::OPT_shared)) {
3593 CmdArgs.push_back("-shared");
3594 } else {
Edward O'Callaghan7d3c2752009-10-16 19:44:18 +00003595 CmdArgs.push_back("--dynamic-linker");
Edward O'Callaghan856e4ff2009-08-22 01:06:46 +00003596 CmdArgs.push_back("/lib/ld.so.1"); // 64Bit Path /lib/amd64/ld.so.1
3597 }
3598 }
3599
Daniel Dunbarb440f562010-08-02 02:38:21 +00003600 if (Output.isFilename()) {
Edward O'Callaghan856e4ff2009-08-22 01:06:46 +00003601 CmdArgs.push_back("-o");
3602 CmdArgs.push_back(Output.getFilename());
3603 } else {
3604 assert(Output.isNothing() && "Invalid output.");
3605 }
3606
3607 if (!Args.hasArg(options::OPT_nostdlib) &&
3608 !Args.hasArg(options::OPT_nostartfiles)) {
3609 if (!Args.hasArg(options::OPT_shared)) {
Chris Lattner3e2ee142010-07-07 16:01:42 +00003610 CmdArgs.push_back(Args.MakeArgString(
Daniel Dunbar9c3ed5f2010-07-14 18:46:23 +00003611 getToolChain().GetFilePath("crt1.o")));
Chris Lattner3e2ee142010-07-07 16:01:42 +00003612 CmdArgs.push_back(Args.MakeArgString(
Daniel Dunbar9c3ed5f2010-07-14 18:46:23 +00003613 getToolChain().GetFilePath("crti.o")));
Chris Lattner3e2ee142010-07-07 16:01:42 +00003614 CmdArgs.push_back(Args.MakeArgString(
Daniel Dunbar9c3ed5f2010-07-14 18:46:23 +00003615 getToolChain().GetFilePath("crtbegin.o")));
Edward O'Callaghan856e4ff2009-08-22 01:06:46 +00003616 } else {
Chris Lattner3e2ee142010-07-07 16:01:42 +00003617 CmdArgs.push_back(Args.MakeArgString(
Daniel Dunbar9c3ed5f2010-07-14 18:46:23 +00003618 getToolChain().GetFilePath("crti.o")));
Edward O'Callaghan856e4ff2009-08-22 01:06:46 +00003619 }
Chris Lattner3e2ee142010-07-07 16:01:42 +00003620 CmdArgs.push_back(Args.MakeArgString(
Daniel Dunbar9c3ed5f2010-07-14 18:46:23 +00003621 getToolChain().GetFilePath("crtn.o")));
Edward O'Callaghan856e4ff2009-08-22 01:06:46 +00003622 }
3623
Daniel Dunbar1c7577c2009-11-04 06:24:38 +00003624 CmdArgs.push_back(Args.MakeArgString("-L/opt/gcc4/lib/gcc/"
3625 + getToolChain().getTripleString()
Daniel Dunbarb0b18612009-10-29 02:24:37 +00003626 + "/4.2.4"));
Edward O'Callaghan856e4ff2009-08-22 01:06:46 +00003627
3628 Args.AddAllArgs(CmdArgs, options::OPT_L);
3629 Args.AddAllArgs(CmdArgs, options::OPT_T_Group);
3630 Args.AddAllArgs(CmdArgs, options::OPT_e);
3631
Daniel Dunbar54423b22010-09-17 00:24:54 +00003632 AddLinkerInputs(getToolChain(), Inputs, Args, CmdArgs);
Edward O'Callaghan856e4ff2009-08-22 01:06:46 +00003633
3634 if (!Args.hasArg(options::OPT_nostdlib) &&
3635 !Args.hasArg(options::OPT_nodefaultlibs)) {
3636 // FIXME: For some reason GCC passes -lgcc before adding
3637 // the default system libraries. Just mimic this for now.
3638 CmdArgs.push_back("-lgcc");
3639
3640 if (Args.hasArg(options::OPT_pthread))
3641 CmdArgs.push_back("-pthread");
3642 if (!Args.hasArg(options::OPT_shared))
3643 CmdArgs.push_back("-lc");
3644 CmdArgs.push_back("-lgcc");
3645 }
3646
3647 if (!Args.hasArg(options::OPT_nostdlib) &&
3648 !Args.hasArg(options::OPT_nostartfiles)) {
3649 if (!Args.hasArg(options::OPT_shared))
Chris Lattner3e2ee142010-07-07 16:01:42 +00003650 CmdArgs.push_back(Args.MakeArgString(
Daniel Dunbar9c3ed5f2010-07-14 18:46:23 +00003651 getToolChain().GetFilePath("crtend.o")));
Edward O'Callaghan856e4ff2009-08-22 01:06:46 +00003652 }
3653
Bill Wendling08760582011-06-27 19:15:03 +00003654 addProfileRT(getToolChain(), Args, CmdArgs, getToolChain().getTriple());
Nick Lewycky82fe5f42011-05-24 21:54:59 +00003655
Edward O'Callaghan856e4ff2009-08-22 01:06:46 +00003656 const char *Exec =
Daniel Dunbar9c3ed5f2010-07-14 18:46:23 +00003657 Args.MakeArgString(getToolChain().GetProgramPath("ld"));
Daniel Dunbar7fbaf532010-08-02 02:38:28 +00003658 C.addCommand(new Command(JA, *this, Exec, CmdArgs));
Edward O'Callaghan856e4ff2009-08-22 01:06:46 +00003659}
3660
Daniel Dunbar10de9e62009-06-29 20:52:51 +00003661void openbsd::Assemble::ConstructJob(Compilation &C, const JobAction &JA,
Daniel Dunbar7fbaf532010-08-02 02:38:28 +00003662 const InputInfo &Output,
Daniel Dunbar10de9e62009-06-29 20:52:51 +00003663 const InputInfoList &Inputs,
3664 const ArgList &Args,
Mike Stump11289f42009-09-09 15:08:12 +00003665 const char *LinkingOutput) const {
Daniel Dunbar10de9e62009-06-29 20:52:51 +00003666 ArgStringList CmdArgs;
3667
3668 Args.AddAllArgValues(CmdArgs, options::OPT_Wa_COMMA,
3669 options::OPT_Xassembler);
3670
3671 CmdArgs.push_back("-o");
Daniel Dunbarb440f562010-08-02 02:38:21 +00003672 CmdArgs.push_back(Output.getFilename());
Daniel Dunbar10de9e62009-06-29 20:52:51 +00003673
3674 for (InputInfoList::const_iterator
3675 it = Inputs.begin(), ie = Inputs.end(); it != ie; ++it) {
3676 const InputInfo &II = *it;
Daniel Dunbarb440f562010-08-02 02:38:21 +00003677 CmdArgs.push_back(II.getFilename());
Daniel Dunbar10de9e62009-06-29 20:52:51 +00003678 }
3679
3680 const char *Exec =
Daniel Dunbar9c3ed5f2010-07-14 18:46:23 +00003681 Args.MakeArgString(getToolChain().GetProgramPath("as"));
Daniel Dunbar7fbaf532010-08-02 02:38:28 +00003682 C.addCommand(new Command(JA, *this, Exec, CmdArgs));
Daniel Dunbar10de9e62009-06-29 20:52:51 +00003683}
3684
3685void openbsd::Link::ConstructJob(Compilation &C, const JobAction &JA,
Daniel Dunbar7fbaf532010-08-02 02:38:28 +00003686 const InputInfo &Output,
Daniel Dunbar10de9e62009-06-29 20:52:51 +00003687 const InputInfoList &Inputs,
3688 const ArgList &Args,
3689 const char *LinkingOutput) const {
Daniel Dunbar083edf72009-12-21 18:54:17 +00003690 const Driver &D = getToolChain().getDriver();
Daniel Dunbar10de9e62009-06-29 20:52:51 +00003691 ArgStringList CmdArgs;
3692
Daniel Dunbara8888ac2009-08-03 01:28:59 +00003693 if ((!Args.hasArg(options::OPT_nostdlib)) &&
Daniel Dunbar1c7577c2009-11-04 06:24:38 +00003694 (!Args.hasArg(options::OPT_shared))) {
Daniel Dunbara8888ac2009-08-03 01:28:59 +00003695 CmdArgs.push_back("-e");
3696 CmdArgs.push_back("__start");
3697 }
3698
Daniel Dunbar10de9e62009-06-29 20:52:51 +00003699 if (Args.hasArg(options::OPT_static)) {
3700 CmdArgs.push_back("-Bstatic");
3701 } else {
Rafael Espindola7ba97af2010-11-11 02:17:51 +00003702 if (Args.hasArg(options::OPT_rdynamic))
3703 CmdArgs.push_back("-export-dynamic");
Daniel Dunbar10de9e62009-06-29 20:52:51 +00003704 CmdArgs.push_back("--eh-frame-hdr");
Daniel Dunbara8888ac2009-08-03 01:28:59 +00003705 CmdArgs.push_back("-Bdynamic");
Daniel Dunbar10de9e62009-06-29 20:52:51 +00003706 if (Args.hasArg(options::OPT_shared)) {
Daniel Dunbara8888ac2009-08-03 01:28:59 +00003707 CmdArgs.push_back("-shared");
Daniel Dunbar10de9e62009-06-29 20:52:51 +00003708 } else {
3709 CmdArgs.push_back("-dynamic-linker");
3710 CmdArgs.push_back("/usr/libexec/ld.so");
3711 }
3712 }
3713
Daniel Dunbarb440f562010-08-02 02:38:21 +00003714 if (Output.isFilename()) {
Daniel Dunbar10de9e62009-06-29 20:52:51 +00003715 CmdArgs.push_back("-o");
3716 CmdArgs.push_back(Output.getFilename());
3717 } else {
3718 assert(Output.isNothing() && "Invalid output.");
3719 }
3720
3721 if (!Args.hasArg(options::OPT_nostdlib) &&
3722 !Args.hasArg(options::OPT_nostartfiles)) {
3723 if (!Args.hasArg(options::OPT_shared)) {
Chris Lattner3e2ee142010-07-07 16:01:42 +00003724 CmdArgs.push_back(Args.MakeArgString(
Daniel Dunbar9c3ed5f2010-07-14 18:46:23 +00003725 getToolChain().GetFilePath("crt0.o")));
Chris Lattner3e2ee142010-07-07 16:01:42 +00003726 CmdArgs.push_back(Args.MakeArgString(
Daniel Dunbar9c3ed5f2010-07-14 18:46:23 +00003727 getToolChain().GetFilePath("crtbegin.o")));
Daniel Dunbar10de9e62009-06-29 20:52:51 +00003728 } else {
Chris Lattner3e2ee142010-07-07 16:01:42 +00003729 CmdArgs.push_back(Args.MakeArgString(
Daniel Dunbar9c3ed5f2010-07-14 18:46:23 +00003730 getToolChain().GetFilePath("crtbeginS.o")));
Daniel Dunbar10de9e62009-06-29 20:52:51 +00003731 }
3732 }
3733
Edward O'Callaghan5c521462009-10-28 15:13:08 +00003734 std::string Triple = getToolChain().getTripleString();
3735 if (Triple.substr(0, 6) == "x86_64")
Daniel Dunbar1c7577c2009-11-04 06:24:38 +00003736 Triple.replace(0, 6, "amd64");
Daniel Dunbarb0b18612009-10-29 02:24:37 +00003737 CmdArgs.push_back(Args.MakeArgString("-L/usr/lib/gcc-lib/" + Triple +
Daniel Dunbarea3813f2010-08-01 23:13:54 +00003738 "/4.2.1"));
Daniel Dunbara8888ac2009-08-03 01:28:59 +00003739
Daniel Dunbar10de9e62009-06-29 20:52:51 +00003740 Args.AddAllArgs(CmdArgs, options::OPT_L);
3741 Args.AddAllArgs(CmdArgs, options::OPT_T_Group);
3742 Args.AddAllArgs(CmdArgs, options::OPT_e);
3743
Daniel Dunbar54423b22010-09-17 00:24:54 +00003744 AddLinkerInputs(getToolChain(), Inputs, Args, CmdArgs);
Daniel Dunbar10de9e62009-06-29 20:52:51 +00003745
3746 if (!Args.hasArg(options::OPT_nostdlib) &&
3747 !Args.hasArg(options::OPT_nodefaultlibs)) {
Daniel Dunbarea3813f2010-08-01 23:13:54 +00003748 if (D.CCCIsCXX) {
Daniel Dunbar3f7796f2010-09-17 01:20:05 +00003749 getToolChain().AddCXXStdlibLibArgs(Args, CmdArgs);
Daniel Dunbarea3813f2010-08-01 23:13:54 +00003750 CmdArgs.push_back("-lm");
3751 }
3752
Daniel Dunbara8888ac2009-08-03 01:28:59 +00003753 // FIXME: For some reason GCC passes -lgcc before adding
3754 // the default system libraries. Just mimic this for now.
3755 CmdArgs.push_back("-lgcc");
Daniel Dunbar10de9e62009-06-29 20:52:51 +00003756
3757 if (Args.hasArg(options::OPT_pthread))
Chris Lattnerd0257f72011-02-21 18:36:51 +00003758 CmdArgs.push_back("-lpthread");
Daniel Dunbara8888ac2009-08-03 01:28:59 +00003759 if (!Args.hasArg(options::OPT_shared))
3760 CmdArgs.push_back("-lc");
3761 CmdArgs.push_back("-lgcc");
Daniel Dunbar10de9e62009-06-29 20:52:51 +00003762 }
3763
3764 if (!Args.hasArg(options::OPT_nostdlib) &&
3765 !Args.hasArg(options::OPT_nostartfiles)) {
3766 if (!Args.hasArg(options::OPT_shared))
Chris Lattner3e2ee142010-07-07 16:01:42 +00003767 CmdArgs.push_back(Args.MakeArgString(
Daniel Dunbar9c3ed5f2010-07-14 18:46:23 +00003768 getToolChain().GetFilePath("crtend.o")));
Daniel Dunbar10de9e62009-06-29 20:52:51 +00003769 else
Chris Lattner3e2ee142010-07-07 16:01:42 +00003770 CmdArgs.push_back(Args.MakeArgString(
Daniel Dunbar9c3ed5f2010-07-14 18:46:23 +00003771 getToolChain().GetFilePath("crtendS.o")));
Daniel Dunbar10de9e62009-06-29 20:52:51 +00003772 }
3773
3774 const char *Exec =
Daniel Dunbar9c3ed5f2010-07-14 18:46:23 +00003775 Args.MakeArgString(getToolChain().GetProgramPath("ld"));
Daniel Dunbar7fbaf532010-08-02 02:38:28 +00003776 C.addCommand(new Command(JA, *this, Exec, CmdArgs));
Daniel Dunbar10de9e62009-06-29 20:52:51 +00003777}
Ed Schoutene33194b2009-04-02 19:13:12 +00003778
Daniel Dunbar8eb473c2009-03-31 17:45:15 +00003779void freebsd::Assemble::ConstructJob(Compilation &C, const JobAction &JA,
Daniel Dunbar7fbaf532010-08-02 02:38:28 +00003780 const InputInfo &Output,
Daniel Dunbard067f7f2009-04-08 23:54:23 +00003781 const InputInfoList &Inputs,
3782 const ArgList &Args,
Mike Stump11289f42009-09-09 15:08:12 +00003783 const char *LinkingOutput) const {
Daniel Dunbar8eb473c2009-03-31 17:45:15 +00003784 ArgStringList CmdArgs;
3785
Daniel Dunbard854c8d2009-04-01 19:36:32 +00003786 // When building 32-bit code on FreeBSD/amd64, we have to explicitly
3787 // instruct as in the base system to assemble 32-bit code.
3788 if (getToolChain().getArchName() == "i386")
Daniel Dunbar8eb473c2009-03-31 17:45:15 +00003789 CmdArgs.push_back("--32");
3790
Roman Divacky00859c22011-06-04 07:37:31 +00003791 if (getToolChain().getArchName() == "powerpc")
3792 CmdArgs.push_back("-a32");
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00003793
Eric Christopher0b26a612010-03-02 02:41:08 +00003794 // Set byte order explicitly
3795 if (getToolChain().getArchName() == "mips")
3796 CmdArgs.push_back("-EB");
3797 else if (getToolChain().getArchName() == "mipsel")
3798 CmdArgs.push_back("-EL");
3799
Daniel Dunbar8eb473c2009-03-31 17:45:15 +00003800 Args.AddAllArgValues(CmdArgs, options::OPT_Wa_COMMA,
3801 options::OPT_Xassembler);
3802
3803 CmdArgs.push_back("-o");
Daniel Dunbarb440f562010-08-02 02:38:21 +00003804 CmdArgs.push_back(Output.getFilename());
Daniel Dunbar8eb473c2009-03-31 17:45:15 +00003805
3806 for (InputInfoList::const_iterator
3807 it = Inputs.begin(), ie = Inputs.end(); it != ie; ++it) {
3808 const InputInfo &II = *it;
Daniel Dunbarb440f562010-08-02 02:38:21 +00003809 CmdArgs.push_back(II.getFilename());
Daniel Dunbar8eb473c2009-03-31 17:45:15 +00003810 }
3811
Daniel Dunbard067f7f2009-04-08 23:54:23 +00003812 const char *Exec =
Daniel Dunbar9c3ed5f2010-07-14 18:46:23 +00003813 Args.MakeArgString(getToolChain().GetProgramPath("as"));
Daniel Dunbar7fbaf532010-08-02 02:38:28 +00003814 C.addCommand(new Command(JA, *this, Exec, CmdArgs));
Daniel Dunbar8eb473c2009-03-31 17:45:15 +00003815}
Daniel Dunbard854c8d2009-04-01 19:36:32 +00003816
3817void freebsd::Link::ConstructJob(Compilation &C, const JobAction &JA,
Daniel Dunbar7fbaf532010-08-02 02:38:28 +00003818 const InputInfo &Output,
Daniel Dunbard067f7f2009-04-08 23:54:23 +00003819 const InputInfoList &Inputs,
3820 const ArgList &Args,
Daniel Dunbare3e263f2009-05-02 20:14:53 +00003821 const char *LinkingOutput) const {
Daniel Dunbar083edf72009-12-21 18:54:17 +00003822 const Driver &D = getToolChain().getDriver();
Daniel Dunbard854c8d2009-04-01 19:36:32 +00003823 ArgStringList CmdArgs;
3824
Joerg Sonnenberger6165ab12011-03-21 13:51:29 +00003825 if (!D.SysRoot.empty())
3826 CmdArgs.push_back(Args.MakeArgString("--sysroot=" + D.SysRoot));
3827
Daniel Dunbard854c8d2009-04-01 19:36:32 +00003828 if (Args.hasArg(options::OPT_static)) {
3829 CmdArgs.push_back("-Bstatic");
3830 } else {
Rafael Espindola7ba97af2010-11-11 02:17:51 +00003831 if (Args.hasArg(options::OPT_rdynamic))
3832 CmdArgs.push_back("-export-dynamic");
Daniel Dunbard854c8d2009-04-01 19:36:32 +00003833 CmdArgs.push_back("--eh-frame-hdr");
3834 if (Args.hasArg(options::OPT_shared)) {
3835 CmdArgs.push_back("-Bshareable");
3836 } else {
3837 CmdArgs.push_back("-dynamic-linker");
3838 CmdArgs.push_back("/libexec/ld-elf.so.1");
3839 }
3840 }
3841
3842 // When building 32-bit code on FreeBSD/amd64, we have to explicitly
3843 // instruct ld in the base system to link 32-bit code.
3844 if (getToolChain().getArchName() == "i386") {
3845 CmdArgs.push_back("-m");
3846 CmdArgs.push_back("elf_i386_fbsd");
3847 }
3848
Roman Divacky5e300b82011-06-04 07:40:24 +00003849 if (getToolChain().getArchName() == "powerpc") {
3850 CmdArgs.push_back("-m");
3851 CmdArgs.push_back("elf32ppc");
3852 }
3853
Daniel Dunbarb440f562010-08-02 02:38:21 +00003854 if (Output.isFilename()) {
Daniel Dunbard854c8d2009-04-01 19:36:32 +00003855 CmdArgs.push_back("-o");
3856 CmdArgs.push_back(Output.getFilename());
3857 } else {
3858 assert(Output.isNothing() && "Invalid output.");
3859 }
3860
3861 if (!Args.hasArg(options::OPT_nostdlib) &&
3862 !Args.hasArg(options::OPT_nostartfiles)) {
3863 if (!Args.hasArg(options::OPT_shared)) {
Roman Divacky66f22762011-02-10 16:59:40 +00003864 if (Args.hasArg(options::OPT_pg))
3865 CmdArgs.push_back(Args.MakeArgString(
3866 getToolChain().GetFilePath("gcrt1.o")));
3867 else
3868 CmdArgs.push_back(Args.MakeArgString(
3869 getToolChain().GetFilePath("crt1.o")));
Chris Lattner3e2ee142010-07-07 16:01:42 +00003870 CmdArgs.push_back(Args.MakeArgString(
Daniel Dunbar9c3ed5f2010-07-14 18:46:23 +00003871 getToolChain().GetFilePath("crti.o")));
Chris Lattner3e2ee142010-07-07 16:01:42 +00003872 CmdArgs.push_back(Args.MakeArgString(
Daniel Dunbar9c3ed5f2010-07-14 18:46:23 +00003873 getToolChain().GetFilePath("crtbegin.o")));
Daniel Dunbard854c8d2009-04-01 19:36:32 +00003874 } else {
Chris Lattner3e2ee142010-07-07 16:01:42 +00003875 CmdArgs.push_back(Args.MakeArgString(
Daniel Dunbar9c3ed5f2010-07-14 18:46:23 +00003876 getToolChain().GetFilePath("crti.o")));
Chris Lattner3e2ee142010-07-07 16:01:42 +00003877 CmdArgs.push_back(Args.MakeArgString(
Daniel Dunbar9c3ed5f2010-07-14 18:46:23 +00003878 getToolChain().GetFilePath("crtbeginS.o")));
Daniel Dunbard854c8d2009-04-01 19:36:32 +00003879 }
3880 }
3881
3882 Args.AddAllArgs(CmdArgs, options::OPT_L);
Roman Divackyee8188a2011-03-01 17:53:14 +00003883 const ToolChain::path_list Paths = getToolChain().getFilePaths();
3884 for (ToolChain::path_list::const_iterator i = Paths.begin(), e = Paths.end();
3885 i != e; ++i)
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003886 CmdArgs.push_back(Args.MakeArgString(StringRef("-L") + *i));
Daniel Dunbard854c8d2009-04-01 19:36:32 +00003887 Args.AddAllArgs(CmdArgs, options::OPT_T_Group);
3888 Args.AddAllArgs(CmdArgs, options::OPT_e);
David Chisnall589a4942010-08-15 22:58:12 +00003889 Args.AddAllArgs(CmdArgs, options::OPT_s);
3890 Args.AddAllArgs(CmdArgs, options::OPT_t);
3891 Args.AddAllArgs(CmdArgs, options::OPT_Z_Flag);
3892 Args.AddAllArgs(CmdArgs, options::OPT_r);
Daniel Dunbard854c8d2009-04-01 19:36:32 +00003893
Daniel Dunbar54423b22010-09-17 00:24:54 +00003894 AddLinkerInputs(getToolChain(), Inputs, Args, CmdArgs);
Daniel Dunbard854c8d2009-04-01 19:36:32 +00003895
3896 if (!Args.hasArg(options::OPT_nostdlib) &&
3897 !Args.hasArg(options::OPT_nodefaultlibs)) {
Daniel Dunbar4b8ef282010-02-17 08:07:51 +00003898 if (D.CCCIsCXX) {
Daniel Dunbar3f7796f2010-09-17 01:20:05 +00003899 getToolChain().AddCXXStdlibLibArgs(Args, CmdArgs);
Roman Divacky66f22762011-02-10 16:59:40 +00003900 if (Args.hasArg(options::OPT_pg))
3901 CmdArgs.push_back("-lm_p");
3902 else
3903 CmdArgs.push_back("-lm");
Daniel Dunbar4b8ef282010-02-17 08:07:51 +00003904 }
Daniel Dunbard854c8d2009-04-01 19:36:32 +00003905 // FIXME: For some reason GCC passes -lgcc and -lgcc_s before adding
3906 // the default system libraries. Just mimic this for now.
Roman Divacky66f22762011-02-10 16:59:40 +00003907 if (Args.hasArg(options::OPT_pg))
3908 CmdArgs.push_back("-lgcc_p");
3909 else
3910 CmdArgs.push_back("-lgcc");
Daniel Dunbard854c8d2009-04-01 19:36:32 +00003911 if (Args.hasArg(options::OPT_static)) {
3912 CmdArgs.push_back("-lgcc_eh");
Roman Divacky66f22762011-02-10 16:59:40 +00003913 } else if (Args.hasArg(options::OPT_pg)) {
3914 CmdArgs.push_back("-lgcc_eh_p");
Daniel Dunbard854c8d2009-04-01 19:36:32 +00003915 } else {
3916 CmdArgs.push_back("--as-needed");
3917 CmdArgs.push_back("-lgcc_s");
3918 CmdArgs.push_back("--no-as-needed");
3919 }
3920
Matt Beaumont-Gay1fe49152011-02-10 20:35:01 +00003921 if (Args.hasArg(options::OPT_pthread)) {
Roman Divacky66f22762011-02-10 16:59:40 +00003922 if (Args.hasArg(options::OPT_pg))
3923 CmdArgs.push_back("-lpthread_p");
3924 else
3925 CmdArgs.push_back("-lpthread");
Matt Beaumont-Gay1fe49152011-02-10 20:35:01 +00003926 }
Daniel Dunbard854c8d2009-04-01 19:36:32 +00003927
Roman Divacky66f22762011-02-10 16:59:40 +00003928 if (Args.hasArg(options::OPT_pg)) {
3929 if (Args.hasArg(options::OPT_shared))
3930 CmdArgs.push_back("-lc");
3931 else
3932 CmdArgs.push_back("-lc_p");
3933 CmdArgs.push_back("-lgcc_p");
3934 } else {
3935 CmdArgs.push_back("-lc");
3936 CmdArgs.push_back("-lgcc");
3937 }
3938
Daniel Dunbard854c8d2009-04-01 19:36:32 +00003939 if (Args.hasArg(options::OPT_static)) {
3940 CmdArgs.push_back("-lgcc_eh");
Roman Divacky66f22762011-02-10 16:59:40 +00003941 } else if (Args.hasArg(options::OPT_pg)) {
3942 CmdArgs.push_back("-lgcc_eh_p");
Daniel Dunbard854c8d2009-04-01 19:36:32 +00003943 } else {
3944 CmdArgs.push_back("--as-needed");
3945 CmdArgs.push_back("-lgcc_s");
3946 CmdArgs.push_back("--no-as-needed");
3947 }
3948 }
3949
3950 if (!Args.hasArg(options::OPT_nostdlib) &&
3951 !Args.hasArg(options::OPT_nostartfiles)) {
3952 if (!Args.hasArg(options::OPT_shared))
Daniel Dunbar9c3ed5f2010-07-14 18:46:23 +00003953 CmdArgs.push_back(Args.MakeArgString(getToolChain().GetFilePath(
Chris Lattner3e2ee142010-07-07 16:01:42 +00003954 "crtend.o")));
Daniel Dunbard854c8d2009-04-01 19:36:32 +00003955 else
Daniel Dunbar9c3ed5f2010-07-14 18:46:23 +00003956 CmdArgs.push_back(Args.MakeArgString(getToolChain().GetFilePath(
Chris Lattner3e2ee142010-07-07 16:01:42 +00003957 "crtendS.o")));
Daniel Dunbar9c3ed5f2010-07-14 18:46:23 +00003958 CmdArgs.push_back(Args.MakeArgString(getToolChain().GetFilePath(
Chris Lattner3e2ee142010-07-07 16:01:42 +00003959 "crtn.o")));
Daniel Dunbard854c8d2009-04-01 19:36:32 +00003960 }
3961
Bill Wendling08760582011-06-27 19:15:03 +00003962 addProfileRT(getToolChain(), Args, CmdArgs, getToolChain().getTriple());
Nick Lewycky82fe5f42011-05-24 21:54:59 +00003963
Daniel Dunbard067f7f2009-04-08 23:54:23 +00003964 const char *Exec =
Daniel Dunbar9c3ed5f2010-07-14 18:46:23 +00003965 Args.MakeArgString(getToolChain().GetProgramPath("ld"));
Daniel Dunbar7fbaf532010-08-02 02:38:28 +00003966 C.addCommand(new Command(JA, *this, Exec, CmdArgs));
Daniel Dunbard854c8d2009-04-01 19:36:32 +00003967}
Daniel Dunbarcc912342009-05-02 18:28:39 +00003968
Benjamin Kramer24f1d3e2011-02-02 18:59:27 +00003969void netbsd::Assemble::ConstructJob(Compilation &C, const JobAction &JA,
3970 const InputInfo &Output,
3971 const InputInfoList &Inputs,
3972 const ArgList &Args,
3973 const char *LinkingOutput) const {
3974 ArgStringList CmdArgs;
3975
3976 // When building 32-bit code on NetBSD/amd64, we have to explicitly
3977 // instruct as in the base system to assemble 32-bit code.
Joerg Sonnenberger637603a2011-05-16 13:35:02 +00003978 if (ToolTriple.getArch() == llvm::Triple::x86_64 &&
3979 getToolChain().getArch() == llvm::Triple::x86)
Benjamin Kramer24f1d3e2011-02-02 18:59:27 +00003980 CmdArgs.push_back("--32");
3981
3982
3983 // Set byte order explicitly
3984 if (getToolChain().getArchName() == "mips")
3985 CmdArgs.push_back("-EB");
3986 else if (getToolChain().getArchName() == "mipsel")
3987 CmdArgs.push_back("-EL");
3988
3989 Args.AddAllArgValues(CmdArgs, options::OPT_Wa_COMMA,
3990 options::OPT_Xassembler);
3991
3992 CmdArgs.push_back("-o");
3993 CmdArgs.push_back(Output.getFilename());
3994
3995 for (InputInfoList::const_iterator
3996 it = Inputs.begin(), ie = Inputs.end(); it != ie; ++it) {
3997 const InputInfo &II = *it;
3998 CmdArgs.push_back(II.getFilename());
3999 }
4000
Joerg Sonnenbergerfcc3ec92011-03-21 14:01:40 +00004001 const char *Exec = Args.MakeArgString(FindTargetProgramPath(getToolChain(),
Joerg Sonnenberger637603a2011-05-16 13:35:02 +00004002 ToolTriple.getTriple(),
4003 "as"));
Benjamin Kramer24f1d3e2011-02-02 18:59:27 +00004004 C.addCommand(new Command(JA, *this, Exec, CmdArgs));
4005}
4006
4007void netbsd::Link::ConstructJob(Compilation &C, const JobAction &JA,
4008 const InputInfo &Output,
4009 const InputInfoList &Inputs,
4010 const ArgList &Args,
4011 const char *LinkingOutput) const {
4012 const Driver &D = getToolChain().getDriver();
4013 ArgStringList CmdArgs;
4014
Joerg Sonnenberger6165ab12011-03-21 13:51:29 +00004015 if (!D.SysRoot.empty())
4016 CmdArgs.push_back(Args.MakeArgString("--sysroot=" + D.SysRoot));
4017
Benjamin Kramer24f1d3e2011-02-02 18:59:27 +00004018 if (Args.hasArg(options::OPT_static)) {
4019 CmdArgs.push_back("-Bstatic");
4020 } else {
4021 if (Args.hasArg(options::OPT_rdynamic))
4022 CmdArgs.push_back("-export-dynamic");
4023 CmdArgs.push_back("--eh-frame-hdr");
4024 if (Args.hasArg(options::OPT_shared)) {
4025 CmdArgs.push_back("-Bshareable");
4026 } else {
4027 CmdArgs.push_back("-dynamic-linker");
4028 CmdArgs.push_back("/libexec/ld.elf_so");
4029 }
4030 }
4031
4032 // When building 32-bit code on NetBSD/amd64, we have to explicitly
4033 // instruct ld in the base system to link 32-bit code.
Joerg Sonnenberger637603a2011-05-16 13:35:02 +00004034 if (ToolTriple.getArch() == llvm::Triple::x86_64 &&
4035 getToolChain().getArch() == llvm::Triple::x86) {
Benjamin Kramer24f1d3e2011-02-02 18:59:27 +00004036 CmdArgs.push_back("-m");
4037 CmdArgs.push_back("elf_i386");
4038 }
4039
4040 if (Output.isFilename()) {
4041 CmdArgs.push_back("-o");
4042 CmdArgs.push_back(Output.getFilename());
4043 } else {
4044 assert(Output.isNothing() && "Invalid output.");
4045 }
4046
4047 if (!Args.hasArg(options::OPT_nostdlib) &&
4048 !Args.hasArg(options::OPT_nostartfiles)) {
4049 if (!Args.hasArg(options::OPT_shared)) {
4050 CmdArgs.push_back(Args.MakeArgString(
4051 getToolChain().GetFilePath("crt0.o")));
4052 CmdArgs.push_back(Args.MakeArgString(
4053 getToolChain().GetFilePath("crti.o")));
4054 CmdArgs.push_back(Args.MakeArgString(
4055 getToolChain().GetFilePath("crtbegin.o")));
4056 } else {
4057 CmdArgs.push_back(Args.MakeArgString(
4058 getToolChain().GetFilePath("crti.o")));
4059 CmdArgs.push_back(Args.MakeArgString(
4060 getToolChain().GetFilePath("crtbeginS.o")));
4061 }
4062 }
4063
4064 Args.AddAllArgs(CmdArgs, options::OPT_L);
4065 Args.AddAllArgs(CmdArgs, options::OPT_T_Group);
4066 Args.AddAllArgs(CmdArgs, options::OPT_e);
4067 Args.AddAllArgs(CmdArgs, options::OPT_s);
4068 Args.AddAllArgs(CmdArgs, options::OPT_t);
4069 Args.AddAllArgs(CmdArgs, options::OPT_Z_Flag);
4070 Args.AddAllArgs(CmdArgs, options::OPT_r);
4071
4072 AddLinkerInputs(getToolChain(), Inputs, Args, CmdArgs);
4073
4074 if (!Args.hasArg(options::OPT_nostdlib) &&
4075 !Args.hasArg(options::OPT_nodefaultlibs)) {
4076 if (D.CCCIsCXX) {
4077 getToolChain().AddCXXStdlibLibArgs(Args, CmdArgs);
4078 CmdArgs.push_back("-lm");
4079 }
4080 // FIXME: For some reason GCC passes -lgcc and -lgcc_s before adding
4081 // the default system libraries. Just mimic this for now.
Benjamin Kramer24f1d3e2011-02-02 18:59:27 +00004082 if (Args.hasArg(options::OPT_static)) {
4083 CmdArgs.push_back("-lgcc_eh");
4084 } else {
4085 CmdArgs.push_back("--as-needed");
4086 CmdArgs.push_back("-lgcc_s");
4087 CmdArgs.push_back("--no-as-needed");
4088 }
Joerg Sonnenberger87717772011-06-07 23:39:17 +00004089 CmdArgs.push_back("-lgcc");
Benjamin Kramer24f1d3e2011-02-02 18:59:27 +00004090
4091 if (Args.hasArg(options::OPT_pthread))
4092 CmdArgs.push_back("-lpthread");
4093 CmdArgs.push_back("-lc");
4094
4095 CmdArgs.push_back("-lgcc");
4096 if (Args.hasArg(options::OPT_static)) {
4097 CmdArgs.push_back("-lgcc_eh");
4098 } else {
4099 CmdArgs.push_back("--as-needed");
4100 CmdArgs.push_back("-lgcc_s");
4101 CmdArgs.push_back("--no-as-needed");
4102 }
4103 }
4104
4105 if (!Args.hasArg(options::OPT_nostdlib) &&
4106 !Args.hasArg(options::OPT_nostartfiles)) {
4107 if (!Args.hasArg(options::OPT_shared))
4108 CmdArgs.push_back(Args.MakeArgString(getToolChain().GetFilePath(
4109 "crtend.o")));
4110 else
4111 CmdArgs.push_back(Args.MakeArgString(getToolChain().GetFilePath(
4112 "crtendS.o")));
4113 CmdArgs.push_back(Args.MakeArgString(getToolChain().GetFilePath(
4114 "crtn.o")));
4115 }
4116
Bill Wendling08760582011-06-27 19:15:03 +00004117 addProfileRT(getToolChain(), Args, CmdArgs, getToolChain().getTriple());
Nick Lewycky82fe5f42011-05-24 21:54:59 +00004118
Joerg Sonnenbergerfcc3ec92011-03-21 14:01:40 +00004119 const char *Exec = Args.MakeArgString(FindTargetProgramPath(getToolChain(),
Joerg Sonnenberger637603a2011-05-16 13:35:02 +00004120 ToolTriple.getTriple(),
4121 "ld"));
Benjamin Kramer24f1d3e2011-02-02 18:59:27 +00004122 C.addCommand(new Command(JA, *this, Exec, CmdArgs));
4123}
4124
Rafael Espindola92b00932010-08-10 00:25:48 +00004125void linuxtools::Assemble::ConstructJob(Compilation &C, const JobAction &JA,
4126 const InputInfo &Output,
4127 const InputInfoList &Inputs,
4128 const ArgList &Args,
4129 const char *LinkingOutput) const {
4130 ArgStringList CmdArgs;
4131
4132 // Add --32/--64 to make sure we get the format we want.
4133 // This is incomplete
4134 if (getToolChain().getArch() == llvm::Triple::x86) {
4135 CmdArgs.push_back("--32");
4136 } else if (getToolChain().getArch() == llvm::Triple::x86_64) {
4137 CmdArgs.push_back("--64");
4138 } else if (getToolChain().getArch() == llvm::Triple::arm) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00004139 StringRef MArch = getToolChain().getArchName();
Rafael Espindola92b00932010-08-10 00:25:48 +00004140 if (MArch == "armv7" || MArch == "armv7a" || MArch == "armv7-a")
4141 CmdArgs.push_back("-mfpu=neon");
4142 }
4143
4144 Args.AddAllArgValues(CmdArgs, options::OPT_Wa_COMMA,
4145 options::OPT_Xassembler);
4146
4147 CmdArgs.push_back("-o");
4148 CmdArgs.push_back(Output.getFilename());
4149
4150 for (InputInfoList::const_iterator
4151 it = Inputs.begin(), ie = Inputs.end(); it != ie; ++it) {
4152 const InputInfo &II = *it;
4153 CmdArgs.push_back(II.getFilename());
4154 }
4155
4156 const char *Exec =
4157 Args.MakeArgString(getToolChain().GetProgramPath("as"));
4158 C.addCommand(new Command(JA, *this, Exec, CmdArgs));
4159}
4160
Rafael Espindolac8f008f2010-11-07 20:14:31 +00004161void linuxtools::Link::ConstructJob(Compilation &C, const JobAction &JA,
4162 const InputInfo &Output,
4163 const InputInfoList &Inputs,
4164 const ArgList &Args,
4165 const char *LinkingOutput) const {
4166 const toolchains::Linux& ToolChain =
4167 static_cast<const toolchains::Linux&>(getToolChain());
4168 const Driver &D = ToolChain.getDriver();
4169 ArgStringList CmdArgs;
4170
Rafael Espindolad1002f62010-11-15 18:28:16 +00004171 // Silence warning for "clang -g foo.o -o foo"
4172 Args.ClaimAllArgs(options::OPT_g_Group);
Rafael Espindolad95a8122011-03-01 05:25:27 +00004173 // and "clang -emit-llvm foo.o -o foo"
4174 Args.ClaimAllArgs(options::OPT_emit_llvm);
Rafael Espindolaf92614c2010-11-17 20:37:10 +00004175 // and for "clang -g foo.o -o foo". Other warning options are already
4176 // handled somewhere else.
4177 Args.ClaimAllArgs(options::OPT_w);
Rafael Espindolad1002f62010-11-15 18:28:16 +00004178
Joerg Sonnenberger6165ab12011-03-21 13:51:29 +00004179 if (!D.SysRoot.empty())
4180 CmdArgs.push_back(Args.MakeArgString("--sysroot=" + D.SysRoot));
Rafael Espindolac8f008f2010-11-07 20:14:31 +00004181
Rafael Espindolad47ac232010-11-17 22:26:15 +00004182 if (Args.hasArg(options::OPT_pie))
4183 CmdArgs.push_back("-pie");
4184
Rafael Espindola1c76c592010-11-07 22:57:16 +00004185 if (Args.hasArg(options::OPT_rdynamic))
4186 CmdArgs.push_back("-export-dynamic");
4187
Rafael Espindola34d77dc2010-11-11 19:34:42 +00004188 if (Args.hasArg(options::OPT_s))
4189 CmdArgs.push_back("-s");
4190
Rafael Espindolac8f008f2010-11-07 20:14:31 +00004191 for (std::vector<std::string>::const_iterator i = ToolChain.ExtraOpts.begin(),
4192 e = ToolChain.ExtraOpts.end();
4193 i != e; ++i)
4194 CmdArgs.push_back(i->c_str());
4195
4196 if (!Args.hasArg(options::OPT_static)) {
4197 CmdArgs.push_back("--eh-frame-hdr");
4198 }
4199
4200 CmdArgs.push_back("-m");
4201 if (ToolChain.getArch() == llvm::Triple::x86)
4202 CmdArgs.push_back("elf_i386");
Eric Christopher84fbdb42011-08-19 00:30:14 +00004203 else if (ToolChain.getArch() == llvm::Triple::arm
Douglas Gregord9bb1522011-03-06 19:11:49 +00004204 || ToolChain.getArch() == llvm::Triple::thumb)
Rafael Espindolac8f008f2010-11-07 20:14:31 +00004205 CmdArgs.push_back("armelf_linux_eabi");
Ted Kremenek43d47cc2011-04-05 22:04:27 +00004206 else if (ToolChain.getArch() == llvm::Triple::ppc)
4207 CmdArgs.push_back("elf32ppclinux");
4208 else if (ToolChain.getArch() == llvm::Triple::ppc64)
4209 CmdArgs.push_back("elf64ppc");
Rafael Espindolac8f008f2010-11-07 20:14:31 +00004210 else
4211 CmdArgs.push_back("elf_x86_64");
4212
4213 if (Args.hasArg(options::OPT_static)) {
Douglas Gregord9bb1522011-03-06 19:11:49 +00004214 if (ToolChain.getArch() == llvm::Triple::arm
4215 || ToolChain.getArch() == llvm::Triple::thumb)
Rafael Espindolac8f008f2010-11-07 20:14:31 +00004216 CmdArgs.push_back("-Bstatic");
4217 else
4218 CmdArgs.push_back("-static");
4219 } else if (Args.hasArg(options::OPT_shared)) {
4220 CmdArgs.push_back("-shared");
4221 }
4222
4223 if (ToolChain.getArch() == llvm::Triple::arm ||
Douglas Gregord9bb1522011-03-06 19:11:49 +00004224 ToolChain.getArch() == llvm::Triple::thumb ||
Rafael Espindolac8f008f2010-11-07 20:14:31 +00004225 (!Args.hasArg(options::OPT_static) &&
4226 !Args.hasArg(options::OPT_shared))) {
4227 CmdArgs.push_back("-dynamic-linker");
4228 if (ToolChain.getArch() == llvm::Triple::x86)
4229 CmdArgs.push_back("/lib/ld-linux.so.2");
Douglas Gregord9bb1522011-03-06 19:11:49 +00004230 else if (ToolChain.getArch() == llvm::Triple::arm ||
4231 ToolChain.getArch() == llvm::Triple::thumb)
Rafael Espindolac8f008f2010-11-07 20:14:31 +00004232 CmdArgs.push_back("/lib/ld-linux.so.3");
Ted Kremenek43d47cc2011-04-05 22:04:27 +00004233 else if (ToolChain.getArch() == llvm::Triple::ppc)
Chris Lattner20b90d02011-04-11 21:15:37 +00004234 CmdArgs.push_back("/lib/ld.so.1");
Ted Kremenek43d47cc2011-04-05 22:04:27 +00004235 else if (ToolChain.getArch() == llvm::Triple::ppc64)
Chris Lattner20b90d02011-04-11 21:15:37 +00004236 CmdArgs.push_back("/lib64/ld64.so.1");
Rafael Espindolac8f008f2010-11-07 20:14:31 +00004237 else
4238 CmdArgs.push_back("/lib64/ld-linux-x86-64.so.2");
4239 }
4240
4241 CmdArgs.push_back("-o");
4242 CmdArgs.push_back(Output.getFilename());
4243
Rafael Espindola81937ec2010-12-01 01:52:43 +00004244 if (!Args.hasArg(options::OPT_nostdlib) &&
4245 !Args.hasArg(options::OPT_nostartfiles)) {
Rafael Espindolad47ac232010-11-17 22:26:15 +00004246 const char *crt1 = NULL;
4247 if (!Args.hasArg(options::OPT_shared)){
4248 if (Args.hasArg(options::OPT_pie))
4249 crt1 = "Scrt1.o";
4250 else
4251 crt1 = "crt1.o";
4252 }
4253 if (crt1)
4254 CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath(crt1)));
Rafael Espindolac8f008f2010-11-07 20:14:31 +00004255
Rafael Espindola9aa60e92010-11-12 03:00:39 +00004256 CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath("crti.o")));
Rafael Espindolac8f008f2010-11-07 20:14:31 +00004257
Rafael Espindola9aa60e92010-11-12 03:00:39 +00004258 const char *crtbegin;
4259 if (Args.hasArg(options::OPT_static))
4260 crtbegin = "crtbeginT.o";
Rafael Espindolad47ac232010-11-17 22:26:15 +00004261 else if (Args.hasArg(options::OPT_shared) || Args.hasArg(options::OPT_pie))
Rafael Espindola9aa60e92010-11-12 03:00:39 +00004262 crtbegin = "crtbeginS.o";
4263 else
4264 crtbegin = "crtbegin.o";
4265 CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath(crtbegin)));
4266 }
Rafael Espindolac8f008f2010-11-07 20:14:31 +00004267
4268 Args.AddAllArgs(CmdArgs, options::OPT_L);
4269
4270 const ToolChain::path_list Paths = ToolChain.getFilePaths();
4271
Roman Divackyee8188a2011-03-01 17:53:14 +00004272 for (ToolChain::path_list::const_iterator i = Paths.begin(), e = Paths.end();
4273 i != e; ++i)
Chris Lattner0e62c1c2011-07-23 10:55:15 +00004274 CmdArgs.push_back(Args.MakeArgString(StringRef("-L") + *i));
Rafael Espindolac8f008f2010-11-07 20:14:31 +00004275
4276 AddLinkerInputs(ToolChain, Inputs, Args, CmdArgs);
4277
Rafael Espindola9aa60e92010-11-12 03:00:39 +00004278 if (D.CCCIsCXX && !Args.hasArg(options::OPT_nostdlib)) {
Rafael Espindolac8f008f2010-11-07 20:14:31 +00004279 ToolChain.AddCXXStdlibLibArgs(Args, CmdArgs);
4280 CmdArgs.push_back("-lm");
4281 }
4282
Rafael Espindola9aa60e92010-11-12 03:00:39 +00004283 if (!Args.hasArg(options::OPT_nostdlib)) {
Nick Lewycky97864da2011-06-04 06:27:06 +00004284 if (Args.hasArg(options::OPT_static))
4285 CmdArgs.push_back("--start-group");
4286
Rafael Espindola9aa60e92010-11-12 03:00:39 +00004287 if (!D.CCCIsCXX)
Rafael Espindolac8f008f2010-11-07 20:14:31 +00004288 CmdArgs.push_back("-lgcc");
Rafael Espindola9aa60e92010-11-12 03:00:39 +00004289
4290 if (Args.hasArg(options::OPT_static)) {
4291 if (D.CCCIsCXX)
4292 CmdArgs.push_back("-lgcc");
4293 } else {
4294 if (!D.CCCIsCXX)
4295 CmdArgs.push_back("--as-needed");
4296 CmdArgs.push_back("-lgcc_s");
4297 if (!D.CCCIsCXX)
4298 CmdArgs.push_back("--no-as-needed");
4299 }
4300
4301 if (Args.hasArg(options::OPT_static))
4302 CmdArgs.push_back("-lgcc_eh");
4303 else if (!Args.hasArg(options::OPT_shared) && D.CCCIsCXX)
4304 CmdArgs.push_back("-lgcc");
4305
4306 if (Args.hasArg(options::OPT_pthread) ||
4307 Args.hasArg(options::OPT_pthreads))
4308 CmdArgs.push_back("-lpthread");
4309
4310 CmdArgs.push_back("-lc");
4311
4312 if (Args.hasArg(options::OPT_static))
4313 CmdArgs.push_back("--end-group");
4314 else {
4315 if (!D.CCCIsCXX)
4316 CmdArgs.push_back("-lgcc");
4317
4318 if (!D.CCCIsCXX)
4319 CmdArgs.push_back("--as-needed");
4320 CmdArgs.push_back("-lgcc_s");
4321 if (!D.CCCIsCXX)
4322 CmdArgs.push_back("--no-as-needed");
4323
4324 if (!Args.hasArg(options::OPT_shared) && D.CCCIsCXX)
4325 CmdArgs.push_back("-lgcc");
4326 }
4327
Rafael Espindolad47ac232010-11-17 22:26:15 +00004328
Rafael Espindola81937ec2010-12-01 01:52:43 +00004329 if (!Args.hasArg(options::OPT_nostartfiles)) {
4330 const char *crtend;
4331 if (Args.hasArg(options::OPT_shared) || Args.hasArg(options::OPT_pie))
4332 crtend = "crtendS.o";
4333 else
4334 crtend = "crtend.o";
Rafael Espindola9aa60e92010-11-12 03:00:39 +00004335
Rafael Espindola81937ec2010-12-01 01:52:43 +00004336 CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath(crtend)));
4337 CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath("crtn.o")));
4338 }
Rafael Espindolac8f008f2010-11-07 20:14:31 +00004339 }
4340
Bill Wendling08760582011-06-27 19:15:03 +00004341 addProfileRT(getToolChain(), Args, CmdArgs, getToolChain().getTriple());
Nick Lewycky82fe5f42011-05-24 21:54:59 +00004342
Rafael Espindolac8f008f2010-11-07 20:14:31 +00004343 if (Args.hasArg(options::OPT_use_gold_plugin)) {
4344 CmdArgs.push_back("-plugin");
4345 std::string Plugin = ToolChain.getDriver().Dir + "/../lib/LLVMgold.so";
4346 CmdArgs.push_back(Args.MakeArgString(Plugin));
4347 }
4348
4349 C.addCommand(new Command(JA, *this, ToolChain.Linker.c_str(), CmdArgs));
4350}
Rafael Espindola92b00932010-08-10 00:25:48 +00004351
Chris Lattner3e2ee142010-07-07 16:01:42 +00004352void minix::Assemble::ConstructJob(Compilation &C, const JobAction &JA,
Daniel Dunbar7fbaf532010-08-02 02:38:28 +00004353 const InputInfo &Output,
4354 const InputInfoList &Inputs,
4355 const ArgList &Args,
4356 const char *LinkingOutput) const {
Chris Lattner3e2ee142010-07-07 16:01:42 +00004357 ArgStringList CmdArgs;
4358
4359 Args.AddAllArgValues(CmdArgs, options::OPT_Wa_COMMA,
4360 options::OPT_Xassembler);
4361
4362 CmdArgs.push_back("-o");
Daniel Dunbarb440f562010-08-02 02:38:21 +00004363 CmdArgs.push_back(Output.getFilename());
Chris Lattner3e2ee142010-07-07 16:01:42 +00004364
4365 for (InputInfoList::const_iterator
4366 it = Inputs.begin(), ie = Inputs.end(); it != ie; ++it) {
4367 const InputInfo &II = *it;
Daniel Dunbarb440f562010-08-02 02:38:21 +00004368 CmdArgs.push_back(II.getFilename());
Chris Lattner3e2ee142010-07-07 16:01:42 +00004369 }
4370
4371 const char *Exec =
Daniel Dunbar9c3ed5f2010-07-14 18:46:23 +00004372 Args.MakeArgString(getToolChain().GetProgramPath("gas"));
Daniel Dunbar7fbaf532010-08-02 02:38:28 +00004373 C.addCommand(new Command(JA, *this, Exec, CmdArgs));
Chris Lattner3e2ee142010-07-07 16:01:42 +00004374}
4375
4376void minix::Link::ConstructJob(Compilation &C, const JobAction &JA,
Daniel Dunbar7fbaf532010-08-02 02:38:28 +00004377 const InputInfo &Output,
4378 const InputInfoList &Inputs,
4379 const ArgList &Args,
4380 const char *LinkingOutput) const {
Chris Lattner3e2ee142010-07-07 16:01:42 +00004381 const Driver &D = getToolChain().getDriver();
4382 ArgStringList CmdArgs;
4383
Daniel Dunbarb440f562010-08-02 02:38:21 +00004384 if (Output.isFilename()) {
Chris Lattner3e2ee142010-07-07 16:01:42 +00004385 CmdArgs.push_back("-o");
4386 CmdArgs.push_back(Output.getFilename());
4387 } else {
4388 assert(Output.isNothing() && "Invalid output.");
4389 }
4390
4391 if (!Args.hasArg(options::OPT_nostdlib) &&
4392 !Args.hasArg(options::OPT_nostartfiles))
Daniel Dunbar9c3ed5f2010-07-14 18:46:23 +00004393 CmdArgs.push_back(Args.MakeArgString(getToolChain().GetFilePath(
Chris Lattner3e2ee142010-07-07 16:01:42 +00004394 "/usr/gnu/lib/crtso.o")));
4395
4396 Args.AddAllArgs(CmdArgs, options::OPT_L);
4397 Args.AddAllArgs(CmdArgs, options::OPT_T_Group);
4398 Args.AddAllArgs(CmdArgs, options::OPT_e);
4399
Daniel Dunbar54423b22010-09-17 00:24:54 +00004400 AddLinkerInputs(getToolChain(), Inputs, Args, CmdArgs);
Chris Lattner3e2ee142010-07-07 16:01:42 +00004401
4402 if (!Args.hasArg(options::OPT_nostdlib) &&
4403 !Args.hasArg(options::OPT_nodefaultlibs)) {
4404 if (D.CCCIsCXX) {
Daniel Dunbar3f7796f2010-09-17 01:20:05 +00004405 getToolChain().AddCXXStdlibLibArgs(Args, CmdArgs);
Chris Lattner3e2ee142010-07-07 16:01:42 +00004406 CmdArgs.push_back("-lm");
4407 }
4408
4409 if (Args.hasArg(options::OPT_pthread))
4410 CmdArgs.push_back("-lpthread");
4411 CmdArgs.push_back("-lc");
4412 CmdArgs.push_back("-lgcc");
4413 CmdArgs.push_back("-L/usr/gnu/lib");
4414 // FIXME: fill in the correct search path for the final
4415 // support libraries.
4416 CmdArgs.push_back("-L/usr/gnu/lib/gcc/i686-pc-minix/4.4.3");
4417 }
4418
4419 if (!Args.hasArg(options::OPT_nostdlib) &&
4420 !Args.hasArg(options::OPT_nostartfiles)) {
Daniel Dunbar9c3ed5f2010-07-14 18:46:23 +00004421 CmdArgs.push_back(Args.MakeArgString(getToolChain().GetFilePath(
Chris Lattner3e2ee142010-07-07 16:01:42 +00004422 "/usr/gnu/lib/libend.a")));
4423 }
4424
Bill Wendling08760582011-06-27 19:15:03 +00004425 addProfileRT(getToolChain(), Args, CmdArgs, getToolChain().getTriple());
Nick Lewycky82fe5f42011-05-24 21:54:59 +00004426
Chris Lattner3e2ee142010-07-07 16:01:42 +00004427 const char *Exec =
Daniel Dunbar9c3ed5f2010-07-14 18:46:23 +00004428 Args.MakeArgString(getToolChain().GetProgramPath("/usr/gnu/bin/gld"));
Daniel Dunbar7fbaf532010-08-02 02:38:28 +00004429 C.addCommand(new Command(JA, *this, Exec, CmdArgs));
Chris Lattner3e2ee142010-07-07 16:01:42 +00004430}
4431
Daniel Dunbarcc912342009-05-02 18:28:39 +00004432/// DragonFly Tools
4433
4434// For now, DragonFly Assemble does just about the same as for
4435// FreeBSD, but this may change soon.
4436void dragonfly::Assemble::ConstructJob(Compilation &C, const JobAction &JA,
Daniel Dunbar7fbaf532010-08-02 02:38:28 +00004437 const InputInfo &Output,
Daniel Dunbar1c7577c2009-11-04 06:24:38 +00004438 const InputInfoList &Inputs,
4439 const ArgList &Args,
4440 const char *LinkingOutput) const {
Daniel Dunbarcc912342009-05-02 18:28:39 +00004441 ArgStringList CmdArgs;
4442
4443 // When building 32-bit code on DragonFly/pc64, we have to explicitly
4444 // instruct as in the base system to assemble 32-bit code.
4445 if (getToolChain().getArchName() == "i386")
4446 CmdArgs.push_back("--32");
4447
4448 Args.AddAllArgValues(CmdArgs, options::OPT_Wa_COMMA,
4449 options::OPT_Xassembler);
4450
4451 CmdArgs.push_back("-o");
Daniel Dunbarb440f562010-08-02 02:38:21 +00004452 CmdArgs.push_back(Output.getFilename());
Daniel Dunbarcc912342009-05-02 18:28:39 +00004453
4454 for (InputInfoList::const_iterator
4455 it = Inputs.begin(), ie = Inputs.end(); it != ie; ++it) {
4456 const InputInfo &II = *it;
Daniel Dunbarb440f562010-08-02 02:38:21 +00004457 CmdArgs.push_back(II.getFilename());
Daniel Dunbarcc912342009-05-02 18:28:39 +00004458 }
4459
4460 const char *Exec =
Daniel Dunbar9c3ed5f2010-07-14 18:46:23 +00004461 Args.MakeArgString(getToolChain().GetProgramPath("as"));
Daniel Dunbar7fbaf532010-08-02 02:38:28 +00004462 C.addCommand(new Command(JA, *this, Exec, CmdArgs));
Daniel Dunbarcc912342009-05-02 18:28:39 +00004463}
4464
4465void dragonfly::Link::ConstructJob(Compilation &C, const JobAction &JA,
Daniel Dunbar7fbaf532010-08-02 02:38:28 +00004466 const InputInfo &Output,
4467 const InputInfoList &Inputs,
4468 const ArgList &Args,
4469 const char *LinkingOutput) const {
Daniel Dunbar083edf72009-12-21 18:54:17 +00004470 const Driver &D = getToolChain().getDriver();
Daniel Dunbarcc912342009-05-02 18:28:39 +00004471 ArgStringList CmdArgs;
4472
Joerg Sonnenberger6165ab12011-03-21 13:51:29 +00004473 if (!D.SysRoot.empty())
4474 CmdArgs.push_back(Args.MakeArgString("--sysroot=" + D.SysRoot));
4475
Daniel Dunbarcc912342009-05-02 18:28:39 +00004476 if (Args.hasArg(options::OPT_static)) {
4477 CmdArgs.push_back("-Bstatic");
4478 } else {
4479 if (Args.hasArg(options::OPT_shared))
4480 CmdArgs.push_back("-Bshareable");
4481 else {
4482 CmdArgs.push_back("-dynamic-linker");
4483 CmdArgs.push_back("/usr/libexec/ld-elf.so.2");
4484 }
4485 }
4486
4487 // When building 32-bit code on DragonFly/pc64, we have to explicitly
4488 // instruct ld in the base system to link 32-bit code.
4489 if (getToolChain().getArchName() == "i386") {
4490 CmdArgs.push_back("-m");
4491 CmdArgs.push_back("elf_i386");
4492 }
4493
Daniel Dunbarb440f562010-08-02 02:38:21 +00004494 if (Output.isFilename()) {
Daniel Dunbarcc912342009-05-02 18:28:39 +00004495 CmdArgs.push_back("-o");
4496 CmdArgs.push_back(Output.getFilename());
4497 } else {
4498 assert(Output.isNothing() && "Invalid output.");
4499 }
4500
4501 if (!Args.hasArg(options::OPT_nostdlib) &&
4502 !Args.hasArg(options::OPT_nostartfiles)) {
4503 if (!Args.hasArg(options::OPT_shared)) {
Chris Lattner3e2ee142010-07-07 16:01:42 +00004504 CmdArgs.push_back(
Daniel Dunbar9c3ed5f2010-07-14 18:46:23 +00004505 Args.MakeArgString(getToolChain().GetFilePath("crt1.o")));
Chris Lattner3e2ee142010-07-07 16:01:42 +00004506 CmdArgs.push_back(
Daniel Dunbar9c3ed5f2010-07-14 18:46:23 +00004507 Args.MakeArgString(getToolChain().GetFilePath("crti.o")));
Chris Lattner3e2ee142010-07-07 16:01:42 +00004508 CmdArgs.push_back(
Daniel Dunbar9c3ed5f2010-07-14 18:46:23 +00004509 Args.MakeArgString(getToolChain().GetFilePath("crtbegin.o")));
Daniel Dunbarcc912342009-05-02 18:28:39 +00004510 } else {
Chris Lattner3e2ee142010-07-07 16:01:42 +00004511 CmdArgs.push_back(
Daniel Dunbar9c3ed5f2010-07-14 18:46:23 +00004512 Args.MakeArgString(getToolChain().GetFilePath("crti.o")));
Chris Lattner3e2ee142010-07-07 16:01:42 +00004513 CmdArgs.push_back(
Daniel Dunbar9c3ed5f2010-07-14 18:46:23 +00004514 Args.MakeArgString(getToolChain().GetFilePath("crtbeginS.o")));
Daniel Dunbarcc912342009-05-02 18:28:39 +00004515 }
4516 }
4517
4518 Args.AddAllArgs(CmdArgs, options::OPT_L);
4519 Args.AddAllArgs(CmdArgs, options::OPT_T_Group);
4520 Args.AddAllArgs(CmdArgs, options::OPT_e);
4521
Daniel Dunbar54423b22010-09-17 00:24:54 +00004522 AddLinkerInputs(getToolChain(), Inputs, Args, CmdArgs);
Daniel Dunbarcc912342009-05-02 18:28:39 +00004523
4524 if (!Args.hasArg(options::OPT_nostdlib) &&
4525 !Args.hasArg(options::OPT_nodefaultlibs)) {
4526 // FIXME: GCC passes on -lgcc, -lgcc_pic and a whole lot of
4527 // rpaths
4528 CmdArgs.push_back("-L/usr/lib/gcc41");
4529
4530 if (!Args.hasArg(options::OPT_static)) {
4531 CmdArgs.push_back("-rpath");
4532 CmdArgs.push_back("/usr/lib/gcc41");
4533
4534 CmdArgs.push_back("-rpath-link");
4535 CmdArgs.push_back("/usr/lib/gcc41");
4536
4537 CmdArgs.push_back("-rpath");
4538 CmdArgs.push_back("/usr/lib");
4539
4540 CmdArgs.push_back("-rpath-link");
4541 CmdArgs.push_back("/usr/lib");
4542 }
4543
Rafael Espindola38360b32010-07-20 12:59:03 +00004544 if (D.CCCIsCXX) {
Daniel Dunbar3f7796f2010-09-17 01:20:05 +00004545 getToolChain().AddCXXStdlibLibArgs(Args, CmdArgs);
Rafael Espindola38360b32010-07-20 12:59:03 +00004546 CmdArgs.push_back("-lm");
4547 }
4548
Daniel Dunbarcc912342009-05-02 18:28:39 +00004549 if (Args.hasArg(options::OPT_shared)) {
4550 CmdArgs.push_back("-lgcc_pic");
4551 } else {
4552 CmdArgs.push_back("-lgcc");
4553 }
4554
4555
4556 if (Args.hasArg(options::OPT_pthread))
Mike Stump0a65b632009-10-31 20:11:46 +00004557 CmdArgs.push_back("-lpthread");
Daniel Dunbarcc912342009-05-02 18:28:39 +00004558
4559 if (!Args.hasArg(options::OPT_nolibc)) {
4560 CmdArgs.push_back("-lc");
4561 }
4562
4563 if (Args.hasArg(options::OPT_shared)) {
4564 CmdArgs.push_back("-lgcc_pic");
4565 } else {
4566 CmdArgs.push_back("-lgcc");
4567 }
4568 }
4569
4570 if (!Args.hasArg(options::OPT_nostdlib) &&
4571 !Args.hasArg(options::OPT_nostartfiles)) {
4572 if (!Args.hasArg(options::OPT_shared))
Chris Lattner3e2ee142010-07-07 16:01:42 +00004573 CmdArgs.push_back(Args.MakeArgString(
Daniel Dunbar9c3ed5f2010-07-14 18:46:23 +00004574 getToolChain().GetFilePath("crtend.o")));
Daniel Dunbarcc912342009-05-02 18:28:39 +00004575 else
Chris Lattner3e2ee142010-07-07 16:01:42 +00004576 CmdArgs.push_back(Args.MakeArgString(
Daniel Dunbar9c3ed5f2010-07-14 18:46:23 +00004577 getToolChain().GetFilePath("crtendS.o")));
Chris Lattner3e2ee142010-07-07 16:01:42 +00004578 CmdArgs.push_back(Args.MakeArgString(
Daniel Dunbar9c3ed5f2010-07-14 18:46:23 +00004579 getToolChain().GetFilePath("crtn.o")));
Daniel Dunbarcc912342009-05-02 18:28:39 +00004580 }
4581
Bill Wendling08760582011-06-27 19:15:03 +00004582 addProfileRT(getToolChain(), Args, CmdArgs, getToolChain().getTriple());
Nick Lewycky82fe5f42011-05-24 21:54:59 +00004583
Daniel Dunbarcc912342009-05-02 18:28:39 +00004584 const char *Exec =
Daniel Dunbar9c3ed5f2010-07-14 18:46:23 +00004585 Args.MakeArgString(getToolChain().GetProgramPath("ld"));
Daniel Dunbar7fbaf532010-08-02 02:38:28 +00004586 C.addCommand(new Command(JA, *this, Exec, CmdArgs));
Daniel Dunbarcc912342009-05-02 18:28:39 +00004587}
Michael J. Spencerb186bc32010-08-21 21:55:07 +00004588
4589void visualstudio::Link::ConstructJob(Compilation &C, const JobAction &JA,
4590 const InputInfo &Output,
4591 const InputInfoList &Inputs,
4592 const ArgList &Args,
4593 const char *LinkingOutput) const {
Michael J. Spencerb186bc32010-08-21 21:55:07 +00004594 ArgStringList CmdArgs;
4595
4596 if (Output.isFilename()) {
Daniel Dunbar2cc3f172010-09-17 00:45:02 +00004597 CmdArgs.push_back(Args.MakeArgString(std::string("-out:") +
4598 Output.getFilename()));
Michael J. Spencerb186bc32010-08-21 21:55:07 +00004599 } else {
4600 assert(Output.isNothing() && "Invalid output.");
4601 }
4602
4603 if (!Args.hasArg(options::OPT_nostdlib) &&
4604 !Args.hasArg(options::OPT_nostartfiles)) {
4605 CmdArgs.push_back("-defaultlib:libcmt");
4606 }
4607
4608 CmdArgs.push_back("-nologo");
4609
Daniel Dunbar54423b22010-09-17 00:24:54 +00004610 AddLinkerInputs(getToolChain(), Inputs, Args, CmdArgs);
Michael J. Spencerb186bc32010-08-21 21:55:07 +00004611
4612 const char *Exec =
Daniel Dunbar54423b22010-09-17 00:24:54 +00004613 Args.MakeArgString(getToolChain().GetProgramPath("link.exe"));
Michael J. Spencerb186bc32010-08-21 21:55:07 +00004614 C.addCommand(new Command(JA, *this, Exec, CmdArgs));
4615}