blob: e147c05ffe6f269fa63340ceb1ae8237aeeb951f [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 }
Daniel Dunbard067f7f2009-04-08 23:54:23 +0000378}
379
Chris Lattner57540c52011-04-15 05:22:18 +0000380/// getARMTargetCPU - Get the (LLVM) name of the ARM cpu we are targeting.
Daniel Dunbarfb58b0a2009-09-10 06:49:20 +0000381//
382// FIXME: tblgen this.
Rafael Espindola0e1fb4f2010-06-28 17:18:09 +0000383static const char *getARMTargetCPU(const ArgList &Args,
384 const llvm::Triple &Triple) {
Daniel Dunbarfb58b0a2009-09-10 06:49:20 +0000385 // FIXME: Warn on inconsistent use of -mcpu and -march.
Daniel Dunbar0f5c5422009-09-10 04:57:17 +0000386
387 // If we have -mcpu=, use that.
388 if (Arg *A = Args.getLastArg(options::OPT_mcpu_EQ))
389 return A->getValue(Args);
390
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000391 StringRef MArch;
Daniel Dunbar0f5c5422009-09-10 04:57:17 +0000392 if (Arg *A = Args.getLastArg(options::OPT_march_EQ)) {
Rafael Espindola0e1fb4f2010-06-28 17:18:09 +0000393 // Otherwise, if we have -march= choose the base CPU for that arch.
394 MArch = A->getValue(Args);
395 } else {
396 // Otherwise, use the Arch from the triple.
397 MArch = Triple.getArchName();
Daniel Dunbar0f5c5422009-09-10 04:57:17 +0000398 }
399
Rafael Espindola0e1fb4f2010-06-28 17:18:09 +0000400 if (MArch == "armv2" || MArch == "armv2a")
401 return "arm2";
402 if (MArch == "armv3")
403 return "arm6";
404 if (MArch == "armv3m")
405 return "arm7m";
406 if (MArch == "armv4" || MArch == "armv4t")
407 return "arm7tdmi";
408 if (MArch == "armv5" || MArch == "armv5t")
409 return "arm10tdmi";
410 if (MArch == "armv5e" || MArch == "armv5te")
411 return "arm1026ejs";
412 if (MArch == "armv5tej")
413 return "arm926ej-s";
414 if (MArch == "armv6" || MArch == "armv6k")
415 return "arm1136jf-s";
416 if (MArch == "armv6j")
417 return "arm1136j-s";
418 if (MArch == "armv6z" || MArch == "armv6zk")
419 return "arm1176jzf-s";
420 if (MArch == "armv6t2")
421 return "arm1156t2-s";
422 if (MArch == "armv7" || MArch == "armv7a" || MArch == "armv7-a")
423 return "cortex-a8";
424 if (MArch == "armv7r" || MArch == "armv7-r")
425 return "cortex-r4";
426 if (MArch == "armv7m" || MArch == "armv7-m")
427 return "cortex-m3";
428 if (MArch == "ep9312")
429 return "ep9312";
430 if (MArch == "iwmmxt")
431 return "iwmmxt";
432 if (MArch == "xscale")
433 return "xscale";
Bob Wilsond9249412011-03-21 20:40:05 +0000434 if (MArch == "armv6m" || MArch == "armv6-m")
435 return "cortex-m0";
Rafael Espindola0e1fb4f2010-06-28 17:18:09 +0000436
437 // If all else failed, return the most base CPU LLVM supports.
Daniel Dunbar0f5c5422009-09-10 04:57:17 +0000438 return "arm7tdmi";
439}
440
Daniel Dunbarf492c922009-09-10 22:59:51 +0000441/// getLLVMArchSuffixForARM - Get the LLVM arch name to use for a particular
Daniel Dunbarfb58b0a2009-09-10 06:49:20 +0000442/// CPU.
443//
444// FIXME: This is redundant with -mcpu, why does LLVM use this.
445// FIXME: tblgen this, or kill it!
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000446static const char *getLLVMArchSuffixForARM(StringRef CPU) {
Daniel Dunbarfb58b0a2009-09-10 06:49:20 +0000447 if (CPU == "arm7tdmi" || CPU == "arm7tdmi-s" || CPU == "arm710t" ||
448 CPU == "arm720t" || CPU == "arm9" || CPU == "arm9tdmi" ||
449 CPU == "arm920" || CPU == "arm920t" || CPU == "arm922t" ||
450 CPU == "arm940t" || CPU == "ep9312")
Daniel Dunbarf492c922009-09-10 22:59:51 +0000451 return "v4t";
Daniel Dunbarfb58b0a2009-09-10 06:49:20 +0000452
453 if (CPU == "arm10tdmi" || CPU == "arm1020t")
Daniel Dunbarf492c922009-09-10 22:59:51 +0000454 return "v5";
Daniel Dunbarfb58b0a2009-09-10 06:49:20 +0000455
456 if (CPU == "arm9e" || CPU == "arm926ej-s" || CPU == "arm946e-s" ||
457 CPU == "arm966e-s" || CPU == "arm968e-s" || CPU == "arm10e" ||
458 CPU == "arm1020e" || CPU == "arm1022e" || CPU == "xscale" ||
459 CPU == "iwmmxt")
Daniel Dunbarf492c922009-09-10 22:59:51 +0000460 return "v5e";
Daniel Dunbarfb58b0a2009-09-10 06:49:20 +0000461
462 if (CPU == "arm1136j-s" || CPU == "arm1136jf-s" || CPU == "arm1176jz-s" ||
463 CPU == "arm1176jzf-s" || CPU == "mpcorenovfp" || CPU == "mpcore")
Daniel Dunbarf492c922009-09-10 22:59:51 +0000464 return "v6";
Daniel Dunbarfb58b0a2009-09-10 06:49:20 +0000465
466 if (CPU == "arm1156t2-s" || CPU == "arm1156t2f-s")
Daniel Dunbarf492c922009-09-10 22:59:51 +0000467 return "v6t2";
Daniel Dunbarfb58b0a2009-09-10 06:49:20 +0000468
469 if (CPU == "cortex-a8" || CPU == "cortex-a9")
Daniel Dunbarf492c922009-09-10 22:59:51 +0000470 return "v7";
Daniel Dunbarfb58b0a2009-09-10 06:49:20 +0000471
Daniel Dunbarf492c922009-09-10 22:59:51 +0000472 return "";
Daniel Dunbarfb58b0a2009-09-10 06:49:20 +0000473}
474
Daniel Dunbard609b7b2009-11-17 06:37:03 +0000475// FIXME: Move to target hook.
476static bool isSignedCharDefault(const llvm::Triple &Triple) {
477 switch (Triple.getArch()) {
478 default:
479 return true;
480
Jim Grosbach7c2c6642011-05-24 15:40:46 +0000481 case llvm::Triple::arm:
Daniel Dunbard609b7b2009-11-17 06:37:03 +0000482 case llvm::Triple::ppc:
483 case llvm::Triple::ppc64:
484 if (Triple.getOS() == llvm::Triple::Darwin)
485 return true;
486 return false;
487
488 case llvm::Triple::systemz:
489 return false;
490 }
491}
492
Daniel Dunbar0f5c5422009-09-10 04:57:17 +0000493void Clang::AddARMTargetArgs(const ArgList &Args,
Daniel Dunbarc9388c12011-03-17 17:10:06 +0000494 ArgStringList &CmdArgs,
495 bool KernelOrKext) const {
Daniel Dunbar083edf72009-12-21 18:54:17 +0000496 const Driver &D = getToolChain().getDriver();
Rafael Espindola0e1fb4f2010-06-28 17:18:09 +0000497 llvm::Triple Triple = getToolChain().getTriple();
Daniel Dunbar78485922009-09-10 23:00:09 +0000498
Daniel Dunbar908b4852011-03-02 00:55:57 +0000499 // Disable movt generation, if requested.
500#ifdef DISABLE_ARM_DARWIN_USE_MOVT
Daniel Dunbar12100e22011-03-22 16:48:17 +0000501 CmdArgs.push_back("-backend-option");
Daniel Dunbar908b4852011-03-02 00:55:57 +0000502 CmdArgs.push_back("-arm-darwin-use-movt=0");
503#endif
504
Daniel Dunbar4ed78982009-09-14 00:34:46 +0000505 // Select the ABI to use.
506 //
507 // FIXME: Support -meabi.
508 const char *ABIName = 0;
509 if (Arg *A = Args.getLastArg(options::OPT_mabi_EQ)) {
510 ABIName = A->getValue(Args);
511 } else {
512 // Select the default based on the platform.
Bob Wilsond1447c42011-02-04 17:59:28 +0000513 switch(Triple.getEnvironment()) {
514 case llvm::Triple::GNUEABI:
Daniel Dunbar4ed78982009-09-14 00:34:46 +0000515 ABIName = "aapcs-linux";
Bob Wilsond1447c42011-02-04 17:59:28 +0000516 break;
517 case llvm::Triple::EABI:
Rafael Espindola23a8a062010-06-16 19:01:17 +0000518 ABIName = "aapcs";
Bob Wilsond1447c42011-02-04 17:59:28 +0000519 break;
520 default:
Rafael Espindola23a8a062010-06-16 19:01:17 +0000521 ABIName = "apcs-gnu";
Bob Wilsond1447c42011-02-04 17:59:28 +0000522 }
Daniel Dunbar4ed78982009-09-14 00:34:46 +0000523 }
524 CmdArgs.push_back("-target-abi");
525 CmdArgs.push_back(ABIName);
526
Daniel Dunbar0f5c5422009-09-10 04:57:17 +0000527 // Set the CPU based on -march= and -mcpu=.
Daniel Dunbara7d02312009-12-18 06:30:12 +0000528 CmdArgs.push_back("-target-cpu");
Rafael Espindola0e1fb4f2010-06-28 17:18:09 +0000529 CmdArgs.push_back(getARMTargetCPU(Args, Triple));
Daniel Dunbar0f5c5422009-09-10 04:57:17 +0000530
Daniel Dunbar78485922009-09-10 23:00:09 +0000531 // Select the float ABI as determined by -msoft-float, -mhard-float, and
532 // -mfloat-abi=.
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000533 StringRef FloatABI;
Daniel Dunbar78485922009-09-10 23:00:09 +0000534 if (Arg *A = Args.getLastArg(options::OPT_msoft_float,
535 options::OPT_mhard_float,
536 options::OPT_mfloat_abi_EQ)) {
537 if (A->getOption().matches(options::OPT_msoft_float))
538 FloatABI = "soft";
539 else if (A->getOption().matches(options::OPT_mhard_float))
540 FloatABI = "hard";
541 else {
542 FloatABI = A->getValue(Args);
543 if (FloatABI != "soft" && FloatABI != "softfp" && FloatABI != "hard") {
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000544 D.Diag(diag::err_drv_invalid_mfloat_abi)
Daniel Dunbar78485922009-09-10 23:00:09 +0000545 << A->getAsString(Args);
546 FloatABI = "soft";
547 }
548 }
549 }
550
551 // If unspecified, choose the default based on the platform.
552 if (FloatABI.empty()) {
Rafael Espindola0e1fb4f2010-06-28 17:18:09 +0000553 const llvm::Triple &Triple = getToolChain().getTriple();
554 switch (Triple.getOS()) {
Daniel Dunbar78485922009-09-10 23:00:09 +0000555 case llvm::Triple::Darwin: {
556 // Darwin defaults to "softfp" for v6 and v7.
557 //
558 // FIXME: Factor out an ARM class so we can cache the arch somewhere.
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000559 StringRef ArchName =
Rafael Espindola0e1fb4f2010-06-28 17:18:09 +0000560 getLLVMArchSuffixForARM(getARMTargetCPU(Args, Triple));
Daniel Dunbar78485922009-09-10 23:00:09 +0000561 if (ArchName.startswith("v6") || ArchName.startswith("v7"))
562 FloatABI = "softfp";
563 else
564 FloatABI = "soft";
565 break;
566 }
567
Rafael Espindolab1ef8ff2010-06-27 18:29:21 +0000568 case llvm::Triple::Linux: {
Bob Wilsond1447c42011-02-04 17:59:28 +0000569 if (getToolChain().getTriple().getEnvironment() == llvm::Triple::GNUEABI) {
Rafael Espindolab1ef8ff2010-06-27 18:29:21 +0000570 FloatABI = "softfp";
571 break;
572 }
573 }
574 // fall through
575
Daniel Dunbar78485922009-09-10 23:00:09 +0000576 default:
Bob Wilsond1447c42011-02-04 17:59:28 +0000577 switch(Triple.getEnvironment()) {
578 case llvm::Triple::GNUEABI:
579 FloatABI = "softfp";
580 break;
581 case llvm::Triple::EABI:
582 // EABI is always AAPCS, and if it was not marked 'hard', it's softfp
583 FloatABI = "softfp";
584 break;
585 default:
586 // Assume "soft", but warn the user we are guessing.
587 FloatABI = "soft";
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000588 D.Diag(diag::warn_drv_assuming_mfloat_abi_is) << "soft";
Bob Wilsond1447c42011-02-04 17:59:28 +0000589 break;
590 }
Daniel Dunbar78485922009-09-10 23:00:09 +0000591 }
592 }
593
594 if (FloatABI == "soft") {
595 // Floating point operations and argument passing are soft.
596 //
597 // FIXME: This changes CPP defines, we need -target-soft-float.
Daniel Dunbara74f8ff2009-11-30 08:42:00 +0000598 CmdArgs.push_back("-msoft-float");
Daniel Dunbar6cc525b2009-12-08 19:49:51 +0000599 CmdArgs.push_back("-mfloat-abi");
600 CmdArgs.push_back("soft");
Daniel Dunbar78485922009-09-10 23:00:09 +0000601 } else if (FloatABI == "softfp") {
602 // Floating point operations are hard, but argument passing is soft.
Daniel Dunbar6cc525b2009-12-08 19:49:51 +0000603 CmdArgs.push_back("-mfloat-abi");
604 CmdArgs.push_back("soft");
Daniel Dunbar78485922009-09-10 23:00:09 +0000605 } else {
606 // Floating point operations and argument passing are hard.
607 assert(FloatABI == "hard" && "Invalid float abi!");
Daniel Dunbar6cc525b2009-12-08 19:49:51 +0000608 CmdArgs.push_back("-mfloat-abi");
609 CmdArgs.push_back("hard");
Daniel Dunbar78485922009-09-10 23:00:09 +0000610 }
Daniel Dunbar893d4752009-12-19 04:15:38 +0000611
612 // Set appropriate target features for floating point mode.
613 //
614 // FIXME: Note, this is a hack, the LLVM backend doesn't actually use these
615 // yet (it uses the -mfloat-abi and -msoft-float options above), and it is
616 // stripped out by the ARM target.
617
618 // Use software floating point operations?
619 if (FloatABI == "soft") {
620 CmdArgs.push_back("-target-feature");
621 CmdArgs.push_back("+soft-float");
622 }
623
624 // Use software floating point argument passing?
625 if (FloatABI != "hard") {
626 CmdArgs.push_back("-target-feature");
627 CmdArgs.push_back("+soft-float-abi");
628 }
Daniel Dunbar0def3d12009-12-21 23:28:17 +0000629
630 // Honor -mfpu=.
631 //
632 // FIXME: Centralize feature selection, defaulting shouldn't be also in the
633 // frontend target.
634 if (const Arg *A = Args.getLastArg(options::OPT_mfpu_EQ)) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000635 StringRef FPU = A->getValue(Args);
Daniel Dunbar0def3d12009-12-21 23:28:17 +0000636
637 // Set the target features based on the FPU.
638 if (FPU == "fpa" || FPU == "fpe2" || FPU == "fpe3" || FPU == "maverick") {
639 // Disable any default FPU support.
640 CmdArgs.push_back("-target-feature");
641 CmdArgs.push_back("-vfp2");
642 CmdArgs.push_back("-target-feature");
643 CmdArgs.push_back("-vfp3");
644 CmdArgs.push_back("-target-feature");
645 CmdArgs.push_back("-neon");
646 } else if (FPU == "vfp") {
647 CmdArgs.push_back("-target-feature");
648 CmdArgs.push_back("+vfp2");
649 } else if (FPU == "vfp3") {
650 CmdArgs.push_back("-target-feature");
651 CmdArgs.push_back("+vfp3");
652 } else if (FPU == "neon") {
653 CmdArgs.push_back("-target-feature");
654 CmdArgs.push_back("+neon");
655 } else
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000656 D.Diag(diag::err_drv_clang_unsupported) << A->getAsString(Args);
Daniel Dunbar0def3d12009-12-21 23:28:17 +0000657 }
Daniel Dunbarb1db4b62011-03-17 00:07:34 +0000658
659 // Setting -msoft-float effectively disables NEON because of the GCC
660 // implementation, although the same isn't true of VFP or VFP3.
661 if (FloatABI == "soft") {
Daniel Dunbarc9388c12011-03-17 17:10:06 +0000662 CmdArgs.push_back("-target-feature");
663 CmdArgs.push_back("-neon");
664 }
665
666 // Kernel code has more strict alignment requirements.
667 if (KernelOrKext) {
Daniel Dunbar12100e22011-03-22 16:48:17 +0000668 CmdArgs.push_back("-backend-option");
Daniel Dunbarc9388c12011-03-17 17:10:06 +0000669 CmdArgs.push_back("-arm-long-calls");
670
Daniel Dunbar12100e22011-03-22 16:48:17 +0000671 CmdArgs.push_back("-backend-option");
Daniel Dunbarc9388c12011-03-17 17:10:06 +0000672 CmdArgs.push_back("-arm-strict-align");
Daniel Dunbared904c82011-04-18 21:26:42 +0000673
674 // The kext linker doesn't know how to deal with movw/movt.
675#ifndef DISABLE_ARM_DARWIN_USE_MOVT
676 CmdArgs.push_back("-backend-option");
677 CmdArgs.push_back("-arm-darwin-use-movt=0");
678#endif
Daniel Dunbarb1db4b62011-03-17 00:07:34 +0000679 }
Daniel Dunbar0f5c5422009-09-10 04:57:17 +0000680}
681
Eric Christopher0b26a612010-03-02 02:41:08 +0000682void Clang::AddMIPSTargetArgs(const ArgList &Args,
683 ArgStringList &CmdArgs) const {
684 const Driver &D = getToolChain().getDriver();
685
686 // Select the ABI to use.
687 const char *ABIName = 0;
688 if (Arg *A = Args.getLastArg(options::OPT_mabi_EQ)) {
689 ABIName = A->getValue(Args);
690 } else {
691 ABIName = "o32";
692 }
693
694 CmdArgs.push_back("-target-abi");
695 CmdArgs.push_back(ABIName);
696
697 if (const Arg *A = Args.getLastArg(options::OPT_march_EQ)) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000698 StringRef MArch = A->getValue(Args);
Eric Christopher0b26a612010-03-02 02:41:08 +0000699 CmdArgs.push_back("-target-cpu");
700
701 if ((MArch == "r2000") || (MArch == "r3000"))
702 CmdArgs.push_back("mips1");
703 else if (MArch == "r6000")
704 CmdArgs.push_back("mips2");
705 else
Nick Lewycky36d8f052011-05-04 03:44:01 +0000706 CmdArgs.push_back(Args.MakeArgString(MArch));
Eric Christopher0b26a612010-03-02 02:41:08 +0000707 }
708
709 // Select the float ABI as determined by -msoft-float, -mhard-float, and
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000710 StringRef FloatABI;
Eric Christopher0b26a612010-03-02 02:41:08 +0000711 if (Arg *A = Args.getLastArg(options::OPT_msoft_float,
712 options::OPT_mhard_float)) {
713 if (A->getOption().matches(options::OPT_msoft_float))
714 FloatABI = "soft";
715 else if (A->getOption().matches(options::OPT_mhard_float))
716 FloatABI = "hard";
717 }
718
719 // If unspecified, choose the default based on the platform.
720 if (FloatABI.empty()) {
Benjamin Kramerf41ccef2010-04-08 15:44:22 +0000721 // Assume "soft", but warn the user we are guessing.
722 FloatABI = "soft";
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000723 D.Diag(diag::warn_drv_assuming_mfloat_abi_is) << "soft";
Eric Christopher0b26a612010-03-02 02:41:08 +0000724 }
725
726 if (FloatABI == "soft") {
727 // Floating point operations and argument passing are soft.
728 //
729 // FIXME: This changes CPP defines, we need -target-soft-float.
730 CmdArgs.push_back("-msoft-float");
731 } else {
732 assert(FloatABI == "hard" && "Invalid float abi!");
733 CmdArgs.push_back("-mhard-float");
734 }
735}
736
Bruno Cardoso Lopese7f211c2010-11-09 17:21:19 +0000737void Clang::AddSparcTargetArgs(const ArgList &Args,
738 ArgStringList &CmdArgs) const {
739 const Driver &D = getToolChain().getDriver();
740
741 if (const Arg *A = Args.getLastArg(options::OPT_march_EQ)) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000742 StringRef MArch = A->getValue(Args);
Bruno Cardoso Lopese7f211c2010-11-09 17:21:19 +0000743 CmdArgs.push_back("-target-cpu");
744 CmdArgs.push_back(MArch.str().c_str());
745 }
746
747 // Select the float ABI as determined by -msoft-float, -mhard-float, and
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000748 StringRef FloatABI;
Bruno Cardoso Lopese7f211c2010-11-09 17:21:19 +0000749 if (Arg *A = Args.getLastArg(options::OPT_msoft_float,
750 options::OPT_mhard_float)) {
751 if (A->getOption().matches(options::OPT_msoft_float))
752 FloatABI = "soft";
753 else if (A->getOption().matches(options::OPT_mhard_float))
754 FloatABI = "hard";
755 }
756
757 // If unspecified, choose the default based on the platform.
758 if (FloatABI.empty()) {
759 switch (getToolChain().getTriple().getOS()) {
760 default:
761 // Assume "soft", but warn the user we are guessing.
762 FloatABI = "soft";
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000763 D.Diag(diag::warn_drv_assuming_mfloat_abi_is) << "soft";
Bruno Cardoso Lopese7f211c2010-11-09 17:21:19 +0000764 break;
765 }
766 }
767
768 if (FloatABI == "soft") {
769 // Floating point operations and argument passing are soft.
770 //
771 // FIXME: This changes CPP defines, we need -target-soft-float.
772 CmdArgs.push_back("-msoft-float");
Bruno Cardoso Lopese7f211c2010-11-09 17:21:19 +0000773 CmdArgs.push_back("-target-feature");
774 CmdArgs.push_back("+soft-float");
775 } else {
776 assert(FloatABI == "hard" && "Invalid float abi!");
777 CmdArgs.push_back("-mhard-float");
778 }
779}
780
Daniel Dunbar3b3191f2009-09-09 22:33:08 +0000781void Clang::AddX86TargetArgs(const ArgList &Args,
782 ArgStringList &CmdArgs) const {
Daniel Dunbare2cf8f72009-09-10 22:59:57 +0000783 if (!Args.hasFlag(options::OPT_mred_zone,
784 options::OPT_mno_red_zone,
785 true) ||
786 Args.hasArg(options::OPT_mkernel) ||
787 Args.hasArg(options::OPT_fapple_kext))
Daniel Dunbar8bed86c2009-11-20 22:21:36 +0000788 CmdArgs.push_back("-disable-red-zone");
Daniel Dunbare2cf8f72009-09-10 22:59:57 +0000789
Daniel Dunbare2cf8f72009-09-10 22:59:57 +0000790 if (Args.hasFlag(options::OPT_msoft_float,
791 options::OPT_mno_soft_float,
792 false))
Daniel Dunbar8bed86c2009-11-20 22:21:36 +0000793 CmdArgs.push_back("-no-implicit-float");
Daniel Dunbare2cf8f72009-09-10 22:59:57 +0000794
Daniel Dunbare13ada62009-11-14 22:04:54 +0000795 const char *CPUName = 0;
Daniel Dunbar3b3191f2009-09-09 22:33:08 +0000796 if (const Arg *A = Args.getLastArg(options::OPT_march_EQ)) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000797 if (StringRef(A->getValue(Args)) == "native") {
Daniel Dunbare13ada62009-11-14 22:04:54 +0000798 // FIXME: Reject attempts to use -march=native unless the target matches
799 // the host.
800 //
801 // FIXME: We should also incorporate the detected target features for use
802 // with -native.
803 std::string CPU = llvm::sys::getHostCPUName();
804 if (!CPU.empty())
805 CPUName = Args.MakeArgString(CPU);
806 } else
807 CPUName = A->getValue(Args);
808 }
Daniel Dunbar3b3191f2009-09-09 22:33:08 +0000809
Daniel Dunbare13ada62009-11-14 22:04:54 +0000810 // Select the default CPU if none was given (or detection failed).
811 if (!CPUName) {
Daniel Dunbar3b3191f2009-09-09 22:33:08 +0000812 // FIXME: Need target hooks.
Benjamin Kramer842bf172010-01-30 15:01:47 +0000813 if (getToolChain().getOS().startswith("darwin")) {
Daniel Dunbar116b3052011-05-31 15:58:55 +0000814 if (getToolChain().getArch() == llvm::Triple::x86_64)
Daniel Dunbare13ada62009-11-14 22:04:54 +0000815 CPUName = "core2";
Daniel Dunbar116b3052011-05-31 15:58:55 +0000816 else if (getToolChain().getArch() == llvm::Triple::x86)
Daniel Dunbare13ada62009-11-14 22:04:54 +0000817 CPUName = "yonah";
Chris Lattnerb986aba2010-04-11 19:29:39 +0000818 } else if (getToolChain().getOS().startswith("haiku")) {
Daniel Dunbar116b3052011-05-31 15:58:55 +0000819 if (getToolChain().getArch() == llvm::Triple::x86_64)
Chris Lattnerb986aba2010-04-11 19:29:39 +0000820 CPUName = "x86-64";
Daniel Dunbar116b3052011-05-31 15:58:55 +0000821 else if (getToolChain().getArch() == llvm::Triple::x86)
Chris Lattnerb986aba2010-04-11 19:29:39 +0000822 CPUName = "i586";
Daniel Dunbarea3813f2010-08-01 23:13:54 +0000823 } else if (getToolChain().getOS().startswith("openbsd")) {
Daniel Dunbar116b3052011-05-31 15:58:55 +0000824 if (getToolChain().getArch() == llvm::Triple::x86_64)
Daniel Dunbarea3813f2010-08-01 23:13:54 +0000825 CPUName = "x86-64";
Daniel Dunbar116b3052011-05-31 15:58:55 +0000826 else if (getToolChain().getArch() == llvm::Triple::x86)
Daniel Dunbarea3813f2010-08-01 23:13:54 +0000827 CPUName = "i486";
Roman Divacky432f10d2011-03-01 18:11:37 +0000828 } else if (getToolChain().getOS().startswith("freebsd")) {
Daniel Dunbar116b3052011-05-31 15:58:55 +0000829 if (getToolChain().getArch() == llvm::Triple::x86_64)
Roman Divacky432f10d2011-03-01 18:11:37 +0000830 CPUName = "x86-64";
Daniel Dunbar116b3052011-05-31 15:58:55 +0000831 else if (getToolChain().getArch() == llvm::Triple::x86)
Roman Divacky432f10d2011-03-01 18:11:37 +0000832 CPUName = "i486";
Benjamin Kramer24f1d3e2011-02-02 18:59:27 +0000833 } else if (getToolChain().getOS().startswith("netbsd")) {
Daniel Dunbar116b3052011-05-31 15:58:55 +0000834 if (getToolChain().getArch() == llvm::Triple::x86_64)
Benjamin Kramer24f1d3e2011-02-02 18:59:27 +0000835 CPUName = "x86-64";
Daniel Dunbar116b3052011-05-31 15:58:55 +0000836 else if (getToolChain().getArch() == llvm::Triple::x86)
Benjamin Kramer24f1d3e2011-02-02 18:59:27 +0000837 CPUName = "i486";
Daniel Dunbar3b3191f2009-09-09 22:33:08 +0000838 } else {
Daniel Dunbar116b3052011-05-31 15:58:55 +0000839 if (getToolChain().getArch() == llvm::Triple::x86_64)
Daniel Dunbare13ada62009-11-14 22:04:54 +0000840 CPUName = "x86-64";
Daniel Dunbar116b3052011-05-31 15:58:55 +0000841 else if (getToolChain().getArch() == llvm::Triple::x86)
Daniel Dunbare13ada62009-11-14 22:04:54 +0000842 CPUName = "pentium4";
Daniel Dunbar3b3191f2009-09-09 22:33:08 +0000843 }
844 }
845
Daniel Dunbare13ada62009-11-14 22:04:54 +0000846 if (CPUName) {
Daniel Dunbara7d02312009-12-18 06:30:12 +0000847 CmdArgs.push_back("-target-cpu");
Daniel Dunbare13ada62009-11-14 22:04:54 +0000848 CmdArgs.push_back(CPUName);
849 }
850
Eli Friedmanad811f02011-07-02 00:34:19 +0000851 // The required algorithm here is slightly strange: the options are applied
852 // in order (so -mno-sse -msse2 disables SSE3), but any option that gets
853 // directly overridden later is ignored (so "-mno-sse -msse2 -mno-sse2 -msse"
854 // is equivalent to "-mno-sse2 -msse"). The -cc1 handling deals with the
855 // former correctly, but not the latter; handle directly-overridden
856 // attributes here.
857 llvm::StringMap<unsigned> PrevFeature;
858 std::vector<const char*> Features;
Daniel Dunbar44b36ee2009-11-25 11:53:23 +0000859 for (arg_iterator it = Args.filtered_begin(options::OPT_m_x86_Features_Group),
860 ie = Args.filtered_end(); it != ie; ++it) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000861 StringRef Name = (*it)->getOption().getName();
Daniel Dunbara442fd52010-06-11 22:00:13 +0000862 (*it)->claim();
Daniel Dunbar3b3191f2009-09-09 22:33:08 +0000863
Daniel Dunbar44b36ee2009-11-25 11:53:23 +0000864 // Skip over "-m".
865 assert(Name.startswith("-m") && "Invalid feature name.");
866 Name = Name.substr(2);
Daniel Dunbar3b3191f2009-09-09 22:33:08 +0000867
Daniel Dunbar44b36ee2009-11-25 11:53:23 +0000868 bool IsNegative = Name.startswith("no-");
869 if (IsNegative)
870 Name = Name.substr(3);
Daniel Dunbar3b3191f2009-09-09 22:33:08 +0000871
Eli Friedmanad811f02011-07-02 00:34:19 +0000872 unsigned& Prev = PrevFeature[Name];
873 if (Prev)
874 Features[Prev - 1] = 0;
875 Prev = Features.size() + 1;
876 Features.push_back(Args.MakeArgString((IsNegative ? "-" : "+") + Name));
877 }
878 for (unsigned i = 0; i < Features.size(); i++) {
879 if (Features[i]) {
880 CmdArgs.push_back("-target-feature");
881 CmdArgs.push_back(Features[i]);
882 }
Daniel Dunbar3b3191f2009-09-09 22:33:08 +0000883 }
884}
885
Eric Christopher84fbdb42011-08-19 00:30:14 +0000886static bool
John McCallb5f652e2011-06-22 00:53:57 +0000887shouldUseExceptionTablesForObjCExceptions(unsigned objcABIVersion,
Anders Carlsson246ff3f2011-02-28 00:44:51 +0000888 const llvm::Triple &Triple) {
889 // We use the zero-cost exception tables for Objective-C if the non-fragile
890 // ABI is enabled or when compiling for x86_64 and ARM on Snow Leopard and
891 // later.
892
John McCallb5f652e2011-06-22 00:53:57 +0000893 if (objcABIVersion >= 2)
Anders Carlsson246ff3f2011-02-28 00:44:51 +0000894 return true;
895
896 if (Triple.getOS() != llvm::Triple::Darwin)
897 return false;
898
Eric Christopherbf15d2b2011-07-02 00:20:22 +0000899 return (!Triple.isMacOSXVersionLT(10,5) &&
Anders Carlsson246ff3f2011-02-28 00:44:51 +0000900 (Triple.getArch() == llvm::Triple::x86_64 ||
Eric Christopher84fbdb42011-08-19 00:30:14 +0000901 Triple.getArch() == llvm::Triple::arm));
Anders Carlsson246ff3f2011-02-28 00:44:51 +0000902}
903
Anders Carlssone96ab552011-02-28 02:27:16 +0000904/// addExceptionArgs - Adds exception related arguments to the driver command
905/// arguments. There's a master flag, -fexceptions and also language specific
906/// flags to enable/disable C++ and Objective-C exceptions.
907/// This makes it possible to for example disable C++ exceptions but enable
908/// Objective-C exceptions.
909static void addExceptionArgs(const ArgList &Args, types::ID InputType,
910 const llvm::Triple &Triple,
911 bool KernelOrKext, bool IsRewriter,
John McCallb5f652e2011-06-22 00:53:57 +0000912 unsigned objcABIVersion,
Anders Carlssone96ab552011-02-28 02:27:16 +0000913 ArgStringList &CmdArgs) {
914 if (KernelOrKext)
915 return;
916
917 // Exceptions are enabled by default.
918 bool ExceptionsEnabled = true;
919
920 // This keeps track of whether exceptions were explicitly turned on or off.
921 bool DidHaveExplicitExceptionFlag = false;
922
Rafael Espindola00a66572009-10-01 13:33:33 +0000923 if (Arg *A = Args.getLastArg(options::OPT_fexceptions,
924 options::OPT_fno_exceptions)) {
925 if (A->getOption().matches(options::OPT_fexceptions))
Anders Carlssone96ab552011-02-28 02:27:16 +0000926 ExceptionsEnabled = true;
Eric Christopher84fbdb42011-08-19 00:30:14 +0000927 else
Anders Carlssone96ab552011-02-28 02:27:16 +0000928 ExceptionsEnabled = false;
929
930 DidHaveExplicitExceptionFlag = true;
Rafael Espindola00a66572009-10-01 13:33:33 +0000931 }
Daniel Dunbar30a12b82010-09-14 23:12:31 +0000932
Anders Carlssone96ab552011-02-28 02:27:16 +0000933 bool ShouldUseExceptionTables = false;
Fariborz Jahaniane4b21ab2009-10-01 20:30:46 +0000934
Anders Carlssone96ab552011-02-28 02:27:16 +0000935 // Exception tables and cleanups can be enabled with -fexceptions even if the
936 // language itself doesn't support exceptions.
937 if (ExceptionsEnabled && DidHaveExplicitExceptionFlag)
938 ShouldUseExceptionTables = true;
Daniel Dunbar30a12b82010-09-14 23:12:31 +0000939
Daniel Dunbarc44f8cf2011-03-17 23:28:31 +0000940 // Obj-C exceptions are enabled by default, regardless of -fexceptions. This
941 // is not necessarily sensible, but follows GCC.
942 if (types::isObjC(InputType) &&
Eric Christopher84fbdb42011-08-19 00:30:14 +0000943 Args.hasFlag(options::OPT_fobjc_exceptions,
Daniel Dunbarc44f8cf2011-03-17 23:28:31 +0000944 options::OPT_fno_objc_exceptions,
945 true)) {
946 CmdArgs.push_back("-fobjc-exceptions");
Anders Carlssone96ab552011-02-28 02:27:16 +0000947
Eric Christopher84fbdb42011-08-19 00:30:14 +0000948 ShouldUseExceptionTables |=
John McCallb5f652e2011-06-22 00:53:57 +0000949 shouldUseExceptionTablesForObjCExceptions(objcABIVersion, Triple);
Anders Carlssone96ab552011-02-28 02:27:16 +0000950 }
951
952 if (types::isCXX(InputType)) {
953 bool CXXExceptionsEnabled = ExceptionsEnabled;
954
Eric Christopher84fbdb42011-08-19 00:30:14 +0000955 if (Arg *A = Args.getLastArg(options::OPT_fcxx_exceptions,
956 options::OPT_fno_cxx_exceptions,
Anders Carlssone96ab552011-02-28 02:27:16 +0000957 options::OPT_fexceptions,
958 options::OPT_fno_exceptions)) {
959 if (A->getOption().matches(options::OPT_fcxx_exceptions))
960 CXXExceptionsEnabled = true;
Chandler Carruth74f87112011-02-28 07:25:18 +0000961 else if (A->getOption().matches(options::OPT_fno_cxx_exceptions))
Anders Carlssone96ab552011-02-28 02:27:16 +0000962 CXXExceptionsEnabled = false;
963 }
964
965 if (CXXExceptionsEnabled) {
966 CmdArgs.push_back("-fcxx-exceptions");
967
968 ShouldUseExceptionTables = true;
969 }
970 }
971
972 if (ShouldUseExceptionTables)
973 CmdArgs.push_back("-fexceptions");
Rafael Espindola00a66572009-10-01 13:33:33 +0000974}
975
Rafael Espindola4cfa7972011-05-02 17:43:32 +0000976static bool ShouldDisableCFI(const ArgList &Args,
977 const ToolChain &TC) {
Rafael Espindolaf934f982011-05-17 16:26:17 +0000978 if (TC.getTriple().getOS() == llvm::Triple::Darwin) {
979 // The native darwin assembler doesn't support cfi directives, so
Rafael Espindola35ab91c2011-05-17 19:06:58 +0000980 // we disable them if we think the .s file will be passed to it.
Rafael Espindola4cfa7972011-05-02 17:43:32 +0000981
Rafael Espindolaf934f982011-05-17 16:26:17 +0000982 // FIXME: Duplicated code with ToolChains.cpp
983 // FIXME: This doesn't belong here, but ideally we will support static soon
984 // anyway.
985 bool HasStatic = (Args.hasArg(options::OPT_mkernel) ||
986 Args.hasArg(options::OPT_static) ||
987 Args.hasArg(options::OPT_fapple_kext));
988 bool IsIADefault = TC.IsIntegratedAssemblerDefault() && !HasStatic;
989 bool UseIntegratedAs = Args.hasFlag(options::OPT_integrated_as,
990 options::OPT_no_integrated_as,
991 IsIADefault);
992 bool UseCFI = Args.hasFlag(options::OPT_fdwarf2_cfi_asm,
993 options::OPT_fno_dwarf2_cfi_asm,
994 UseIntegratedAs);
995 return !UseCFI;
996 }
997
998 // For now we assume that every other assembler support CFI.
999 return false;
Rafael Espindola4cfa7972011-05-02 17:43:32 +00001000}
1001
Joerg Sonnenbergeref317a22011-05-06 14:35:16 +00001002/// \brief Check whether the given input tree contains any compilation actions.
1003static bool ContainsCompileAction(const Action *A) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001004 if (isa<CompileJobAction>(A))
Joerg Sonnenbergeref317a22011-05-06 14:35:16 +00001005 return true;
1006
1007 for (Action::const_iterator it = A->begin(), ie = A->end(); it != ie; ++it)
1008 if (ContainsCompileAction(*it))
1009 return true;
1010
1011 return false;
1012}
1013
1014/// \brief Check if -relax-all should be passed to the internal assembler.
1015/// This is done by default when compiling non-assembler source with -O0.
1016static bool UseRelaxAll(Compilation &C, const ArgList &Args) {
1017 bool RelaxDefault = true;
1018
1019 if (Arg *A = Args.getLastArg(options::OPT_O_Group))
1020 RelaxDefault = A->getOption().matches(options::OPT_O0);
1021
1022 if (RelaxDefault) {
1023 RelaxDefault = false;
1024 for (ActionList::const_iterator it = C.getActions().begin(),
1025 ie = C.getActions().end(); it != ie; ++it) {
1026 if (ContainsCompileAction(*it)) {
1027 RelaxDefault = true;
1028 break;
1029 }
1030 }
1031 }
1032
1033 return Args.hasFlag(options::OPT_mrelax_all, options::OPT_mno_relax_all,
1034 RelaxDefault);
1035}
1036
Daniel Dunbar1a093d22009-03-18 06:00:36 +00001037void Clang::ConstructJob(Compilation &C, const JobAction &JA,
Daniel Dunbar04c4c2c2009-03-18 07:06:02 +00001038 const InputInfo &Output,
Daniel Dunbar0450e6d2009-03-18 06:07:59 +00001039 const InputInfoList &Inputs,
Daniel Dunbara2aedc62009-03-18 10:01:51 +00001040 const ArgList &Args,
Daniel Dunbar1a093d22009-03-18 06:00:36 +00001041 const char *LinkingOutput) const {
Daniel Dunbare46b52a2010-03-20 04:52:14 +00001042 bool KernelOrKext = Args.hasArg(options::OPT_mkernel,
1043 options::OPT_fapple_kext);
Daniel Dunbar083edf72009-12-21 18:54:17 +00001044 const Driver &D = getToolChain().getDriver();
Daniel Dunbara3246a02009-03-18 08:07:30 +00001045 ArgStringList CmdArgs;
1046
Daniel Dunbare521a892009-03-31 20:53:55 +00001047 assert(Inputs.size() == 1 && "Unable to handle multiple inputs.");
1048
Daniel Dunbar6c536aa2009-12-11 23:00:49 +00001049 // Invoke ourselves in -cc1 mode.
1050 //
1051 // FIXME: Implement custom jobs for internal actions.
1052 CmdArgs.push_back("-cc1");
1053
Daniel Dunbar624c21b2009-10-30 18:12:20 +00001054 // Add the "effective" target triple.
Daniel Dunbard640be22009-03-31 17:35:15 +00001055 CmdArgs.push_back("-triple");
Daniel Dunbar82eb4ce2010-08-23 22:35:37 +00001056 std::string TripleStr = getToolChain().ComputeEffectiveClangTriple(Args);
Daniel Dunbar624c21b2009-10-30 18:12:20 +00001057 CmdArgs.push_back(Args.MakeArgString(TripleStr));
Daniel Dunbarfb58b0a2009-09-10 06:49:20 +00001058
Daniel Dunbar624c21b2009-10-30 18:12:20 +00001059 // Select the appropriate action.
Daniel Dunbar99b55242010-07-19 19:44:22 +00001060 bool IsRewriter = false;
Daniel Dunbara2aedc62009-03-18 10:01:51 +00001061 if (isa<AnalyzeJobAction>(JA)) {
1062 assert(JA.getType() == types::TY_Plist && "Invalid output type.");
1063 CmdArgs.push_back("-analyze");
1064 } else if (isa<PreprocessJobAction>(JA)) {
Daniel Dunbard67a3222009-03-30 06:36:42 +00001065 if (Output.getType() == types::TY_Dependencies)
1066 CmdArgs.push_back("-Eonly");
1067 else
1068 CmdArgs.push_back("-E");
Daniel Dunbarc4343942010-02-03 03:07:56 +00001069 } else if (isa<AssembleJobAction>(JA)) {
1070 CmdArgs.push_back("-emit-obj");
Daniel Dunbar06e2cc32010-05-27 06:18:05 +00001071
Joerg Sonnenbergeref317a22011-05-06 14:35:16 +00001072 if (UseRelaxAll(C, Args))
Daniel Dunbar06e2cc32010-05-27 06:18:05 +00001073 CmdArgs.push_back("-mrelax-all");
Daniel Dunbarfe6c97b2010-08-24 16:47:49 +00001074
Daniel Dunbar2b4de142010-10-18 22:36:15 +00001075 // When using an integrated assembler, translate -Wa, and -Xassembler
1076 // options.
1077 for (arg_iterator it = Args.filtered_begin(options::OPT_Wa_COMMA,
1078 options::OPT_Xassembler),
1079 ie = Args.filtered_end(); it != ie; ++it) {
1080 const Arg *A = *it;
1081 A->claim();
1082
1083 for (unsigned i = 0, e = A->getNumValues(); i != e; ++i) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001084 StringRef Value = A->getValue(Args, i);
Daniel Dunbar2b4de142010-10-18 22:36:15 +00001085
1086 if (Value == "-force_cpusubtype_ALL") {
1087 // Do nothing, this is the default and we don't support anything else.
Daniel Dunbara78e5892010-10-28 20:36:23 +00001088 } else if (Value == "-L") {
Daniel Dunbar67919b22011-03-28 22:49:28 +00001089 CmdArgs.push_back("-msave-temp-labels");
Joerg Sonnenberger3028e462011-05-19 20:46:39 +00001090 } else if (Value == "--fatal-warnings") {
Joerg Sonnenbergerb487d2d2011-05-19 18:42:29 +00001091 CmdArgs.push_back("-mllvm");
1092 CmdArgs.push_back("-fatal-assembler-warnings");
Nick Lewyckyca6b90d2011-06-21 00:14:18 +00001093 } else if (Value == "--noexecstack") {
1094 CmdArgs.push_back("-mnoexecstack");
Daniel Dunbar2b4de142010-10-18 22:36:15 +00001095 } else {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001096 D.Diag(diag::err_drv_unsupported_option_argument)
Daniel Dunbar2b4de142010-10-18 22:36:15 +00001097 << A->getOption().getName() << Value;
1098 }
1099 }
1100 }
Daniel Dunbar7c874332010-11-19 16:23:35 +00001101
1102 // Also ignore explicit -force_cpusubtype_ALL option.
1103 (void) Args.hasArg(options::OPT_force__cpusubtype__ALL);
Daniel Dunbara2aedc62009-03-18 10:01:51 +00001104 } else if (isa<PrecompileJobAction>(JA)) {
Argyrios Kyrtzidis90bdfbb2010-08-11 23:27:58 +00001105 // Use PCH if the user requested it.
Daniel Dunbarcbc34b72009-10-15 20:02:44 +00001106 bool UsePCH = D.CCCUsePCH;
Daniel Dunbarcbc34b72009-10-15 20:02:44 +00001107
1108 if (UsePCH)
Douglas Gregor111af7d2009-04-18 00:34:01 +00001109 CmdArgs.push_back("-emit-pch");
1110 else
1111 CmdArgs.push_back("-emit-pth");
Daniel Dunbara2aedc62009-03-18 10:01:51 +00001112 } else {
1113 assert(isa<CompileJobAction>(JA) && "Invalid action for clang tool.");
Daniel Dunbard067f7f2009-04-08 23:54:23 +00001114
Daniel Dunbara2aedc62009-03-18 10:01:51 +00001115 if (JA.getType() == types::TY_Nothing) {
1116 CmdArgs.push_back("-fsyntax-only");
Daniel Dunbar24e52992010-06-07 23:28:45 +00001117 } else if (JA.getType() == types::TY_LLVM_IR ||
1118 JA.getType() == types::TY_LTO_IR) {
Daniel Dunbara2aedc62009-03-18 10:01:51 +00001119 CmdArgs.push_back("-emit-llvm");
Daniel Dunbar24e52992010-06-07 23:28:45 +00001120 } else if (JA.getType() == types::TY_LLVM_BC ||
1121 JA.getType() == types::TY_LTO_BC) {
Daniel Dunbara2aedc62009-03-18 10:01:51 +00001122 CmdArgs.push_back("-emit-llvm-bc");
1123 } else if (JA.getType() == types::TY_PP_Asm) {
Daniel Dunbard112f102009-09-17 00:47:53 +00001124 CmdArgs.push_back("-S");
Daniel Dunbar6cdf83c2009-09-01 16:57:46 +00001125 } else if (JA.getType() == types::TY_AST) {
1126 CmdArgs.push_back("-emit-pch");
Daniel Dunbaraeea8ac2010-02-11 03:16:21 +00001127 } else if (JA.getType() == types::TY_RewrittenObjC) {
1128 CmdArgs.push_back("-rewrite-objc");
Daniel Dunbar99b55242010-07-19 19:44:22 +00001129 IsRewriter = true;
Daniel Dunbaraeea8ac2010-02-11 03:16:21 +00001130 } else {
1131 assert(JA.getType() == types::TY_PP_Asm &&
1132 "Unexpected output type!");
Daniel Dunbara2aedc62009-03-18 10:01:51 +00001133 }
Daniel Dunbara3246a02009-03-18 08:07:30 +00001134 }
1135
Daniel Dunbara2aedc62009-03-18 10:01:51 +00001136 // The make clang go fast button.
1137 CmdArgs.push_back("-disable-free");
1138
John McCallbb79b5f2010-02-13 03:50:24 +00001139 // Disable the verification pass in -asserts builds.
1140#ifdef NDEBUG
1141 CmdArgs.push_back("-disable-llvm-verifier");
1142#endif
1143
Daniel Dunbar3b358a32009-04-08 05:11:16 +00001144 // Set the main file name, so that debug info works even with
1145 // -save-temps.
1146 CmdArgs.push_back("-main-file-name");
1147 CmdArgs.push_back(darwin::CC1::getBaseInputName(Args, Inputs));
1148
Daniel Dunbar17ddaa62009-04-08 18:03:55 +00001149 // Some flags which affect the language (via preprocessor
1150 // defines). See darwin::CC1::AddCPPArgs.
1151 if (Args.hasArg(options::OPT_static))
1152 CmdArgs.push_back("-static-define");
1153
Daniel Dunbara2aedc62009-03-18 10:01:51 +00001154 if (isa<AnalyzeJobAction>(JA)) {
Ted Kremenek05e6f5b2009-09-25 05:55:59 +00001155 // Enable region store model by default.
1156 CmdArgs.push_back("-analyzer-store=region");
1157
Ted Kremenek7bea9a12009-12-07 22:26:14 +00001158 // Treat blocks as analysis entry points.
1159 CmdArgs.push_back("-analyzer-opt-analyze-nested-blocks");
1160
Ted Kremenek49c79792011-03-24 00:28:47 +00001161 CmdArgs.push_back("-analyzer-eagerly-assume");
1162
Daniel Dunbara2aedc62009-03-18 10:01:51 +00001163 // Add default argument set.
Daniel Dunbar58f345ce2009-05-22 00:38:15 +00001164 if (!Args.hasArg(options::OPT__analyzer_no_default_checks)) {
Argyrios Kyrtzidisa6d04d52011-02-15 07:42:33 +00001165 CmdArgs.push_back("-analyzer-checker=core");
Ted Kremenek49c79792011-03-24 00:28:47 +00001166 CmdArgs.push_back("-analyzer-checker=deadcode");
1167 CmdArgs.push_back("-analyzer-checker=security");
1168
Argyrios Kyrtzidisa6d04d52011-02-15 07:42:33 +00001169 if (getToolChain().getTriple().getOS() != llvm::Triple::Win32)
1170 CmdArgs.push_back("-analyzer-checker=unix");
Ted Kremenek49c79792011-03-24 00:28:47 +00001171
Argyrios Kyrtzidisa6d04d52011-02-15 07:42:33 +00001172 if (getToolChain().getTriple().getVendor() == llvm::Triple::Apple)
Ted Kremenek49c79792011-03-24 00:28:47 +00001173 CmdArgs.push_back("-analyzer-checker=osx");
Daniel Dunbar58f345ce2009-05-22 00:38:15 +00001174 }
Daniel Dunbard067f7f2009-04-08 23:54:23 +00001175
Daniel Dunbar58f345ce2009-05-22 00:38:15 +00001176 // Set the output format. The default is plist, for (lame) historical
1177 // reasons.
1178 CmdArgs.push_back("-analyzer-output");
1179 if (Arg *A = Args.getLastArg(options::OPT__analyzer_output))
1180 CmdArgs.push_back(A->getValue(Args));
1181 else
1182 CmdArgs.push_back("plist");
Daniel Dunbara2aedc62009-03-18 10:01:51 +00001183
Ted Kremenekfe449a22010-03-22 22:32:05 +00001184 // Disable the presentation of standard compiler warnings when
1185 // using --analyze. We only want to show static analyzer diagnostics
1186 // or frontend errors.
1187 CmdArgs.push_back("-w");
1188
Daniel Dunbara2aedc62009-03-18 10:01:51 +00001189 // Add -Xanalyzer arguments when running as analyzer.
1190 Args.AddAllArgValues(CmdArgs, options::OPT_Xanalyzer);
Mike Stump11289f42009-09-09 15:08:12 +00001191 }
1192
Daniel Dunbar4eadb602009-09-10 01:21:12 +00001193 CheckCodeGenerationOptions(D, Args);
1194
Daniel Dunbar44e71222009-04-29 18:32:25 +00001195 // Perform argument translation for LLVM backend. This
1196 // takes some care in reconciling with llvm-gcc. The
1197 // issue is that llvm-gcc translates these options based on
1198 // the values in cc1, whereas we are processing based on
1199 // the driver arguments.
Daniel Dunbard067f7f2009-04-08 23:54:23 +00001200
Daniel Dunbar44e71222009-04-29 18:32:25 +00001201 // This comes from the default translation the driver + cc1
1202 // would do to enable flag_pic.
1203 //
1204 // FIXME: Centralize this code.
1205 bool PICEnabled = (Args.hasArg(options::OPT_fPIC) ||
1206 Args.hasArg(options::OPT_fpic) ||
1207 Args.hasArg(options::OPT_fPIE) ||
1208 Args.hasArg(options::OPT_fpie));
1209 bool PICDisabled = (Args.hasArg(options::OPT_mkernel) ||
1210 Args.hasArg(options::OPT_static));
1211 const char *Model = getToolChain().GetForcedPicModel();
1212 if (!Model) {
1213 if (Args.hasArg(options::OPT_mdynamic_no_pic))
1214 Model = "dynamic-no-pic";
1215 else if (PICDisabled)
1216 Model = "static";
1217 else if (PICEnabled)
1218 Model = "pic";
Daniel Dunbara2aedc62009-03-18 10:01:51 +00001219 else
Daniel Dunbar44e71222009-04-29 18:32:25 +00001220 Model = getToolChain().GetDefaultRelocationModel();
Daniel Dunbara2aedc62009-03-18 10:01:51 +00001221 }
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001222 if (StringRef(Model) != "pic") {
Daniel Dunbara1b02a22009-11-29 07:18:39 +00001223 CmdArgs.push_back("-mrelocation-model");
1224 CmdArgs.push_back(Model);
1225 }
Daniel Dunbar44e71222009-04-29 18:32:25 +00001226
1227 // Infer the __PIC__ value.
1228 //
1229 // FIXME: This isn't quite right on Darwin, which always sets
1230 // __PIC__=2.
1231 if (strcmp(Model, "pic") == 0 || strcmp(Model, "dynamic-no-pic") == 0) {
Daniel Dunbar5fe08662009-11-29 02:39:08 +00001232 CmdArgs.push_back("-pic-level");
1233 CmdArgs.push_back(Args.hasArg(options::OPT_fPIC) ? "2" : "1");
Daniel Dunbar44e71222009-04-29 18:32:25 +00001234 }
Tanya Lattnerf9d41df2009-11-04 01:18:09 +00001235 if (!Args.hasFlag(options::OPT_fmerge_all_constants,
1236 options::OPT_fno_merge_all_constants))
Chris Lattner9242b332011-04-08 18:06:54 +00001237 CmdArgs.push_back("-fno-merge-all-constants");
Daniel Dunbar306945d2009-09-16 06:17:29 +00001238
Daniel Dunbara1b02a22009-11-29 07:18:39 +00001239 // LLVM Code Generator Options.
1240
Daniel Dunbar0bb03312011-02-09 17:54:19 +00001241 if (Arg *A = Args.getLastArg(options::OPT_mregparm_EQ)) {
1242 CmdArgs.push_back("-mregparm");
1243 CmdArgs.push_back(A->getValue(Args));
1244 }
1245
Roman Divacky65b88cd2011-03-01 17:40:53 +00001246 if (Args.hasFlag(options::OPT_mrtd, options::OPT_mno_rtd, false))
1247 CmdArgs.push_back("-mrtd");
1248
Daniel Dunbara1b02a22009-11-29 07:18:39 +00001249 // FIXME: Set --enable-unsafe-fp-math.
1250 if (Args.hasFlag(options::OPT_fno_omit_frame_pointer,
1251 options::OPT_fomit_frame_pointer))
1252 CmdArgs.push_back("-mdisable-fp-elim");
1253 if (!Args.hasFlag(options::OPT_fzero_initialized_in_bss,
1254 options::OPT_fno_zero_initialized_in_bss))
1255 CmdArgs.push_back("-mno-zero-initialized-in-bss");
Daniel Dunbar7aa71f92011-02-04 02:20:39 +00001256 if (!Args.hasFlag(options::OPT_fstrict_aliasing,
1257 options::OPT_fno_strict_aliasing,
1258 getToolChain().IsStrictAliasingDefault()))
Dan Gohman10169b92010-10-14 22:36:56 +00001259 CmdArgs.push_back("-relaxed-aliasing");
Daniel Dunbar0d8ca9e2010-05-14 22:00:22 +00001260
1261 // Decide whether to use verbose asm. Verbose assembly is the default on
1262 // toolchains which have the integrated assembler on by default.
1263 bool IsVerboseAsmDefault = getToolChain().IsIntegratedAssemblerDefault();
1264 if (Args.hasFlag(options::OPT_fverbose_asm, options::OPT_fno_verbose_asm,
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001265 IsVerboseAsmDefault) ||
Daniel Dunbar0d8ca9e2010-05-14 22:00:22 +00001266 Args.hasArg(options::OPT_dA))
Daniel Dunbara1b02a22009-11-29 07:18:39 +00001267 CmdArgs.push_back("-masm-verbose");
Daniel Dunbar0d8ca9e2010-05-14 22:00:22 +00001268
Daniel Dunbara1b02a22009-11-29 07:18:39 +00001269 if (Args.hasArg(options::OPT_fdebug_pass_structure)) {
1270 CmdArgs.push_back("-mdebug-pass");
1271 CmdArgs.push_back("Structure");
1272 }
1273 if (Args.hasArg(options::OPT_fdebug_pass_arguments)) {
1274 CmdArgs.push_back("-mdebug-pass");
1275 CmdArgs.push_back("Arguments");
1276 }
1277
John McCall8517abc2010-02-19 02:45:38 +00001278 // Enable -mconstructor-aliases except on darwin, where we have to
1279 // work around a linker bug; see <rdar://problem/7651567>.
1280 if (getToolChain().getTriple().getOS() != llvm::Triple::Darwin)
1281 CmdArgs.push_back("-mconstructor-aliases");
NAKAMURA Takumi029d74b2011-02-17 08:50:50 +00001282
John McCall7ef5cb32011-03-18 02:56:14 +00001283 // Darwin's kernel doesn't support guard variables; just die if we
1284 // try to use them.
1285 if (KernelOrKext &&
1286 getToolChain().getTriple().getOS() == llvm::Triple::Darwin)
1287 CmdArgs.push_back("-fforbid-guard-variables");
1288
Douglas Gregordbe39272011-02-01 15:15:22 +00001289 if (Args.hasArg(options::OPT_mms_bitfields)) {
1290 CmdArgs.push_back("-mms-bitfields");
1291 }
John McCall8517abc2010-02-19 02:45:38 +00001292
Daniel Dunbar306945d2009-09-16 06:17:29 +00001293 // This is a coarse approximation of what llvm-gcc actually does, both
1294 // -fasynchronous-unwind-tables and -fnon-call-exceptions interact in more
1295 // complicated ways.
1296 bool AsynchronousUnwindTables =
1297 Args.hasFlag(options::OPT_fasynchronous_unwind_tables,
1298 options::OPT_fno_asynchronous_unwind_tables,
1299 getToolChain().IsUnwindTablesDefault() &&
Daniel Dunbare46b52a2010-03-20 04:52:14 +00001300 !KernelOrKext);
Daniel Dunbar306945d2009-09-16 06:17:29 +00001301 if (Args.hasFlag(options::OPT_funwind_tables, options::OPT_fno_unwind_tables,
1302 AsynchronousUnwindTables))
Daniel Dunbara1b02a22009-11-29 07:18:39 +00001303 CmdArgs.push_back("-munwind-tables");
1304
1305 if (Arg *A = Args.getLastArg(options::OPT_flimited_precision_EQ)) {
1306 CmdArgs.push_back("-mlimit-float-precision");
1307 CmdArgs.push_back(A->getValue(Args));
1308 }
Daniel Dunbar44e71222009-04-29 18:32:25 +00001309
Daniel Dunbar4dbaaa62009-05-06 03:16:41 +00001310 // FIXME: Handle -mtune=.
1311 (void) Args.hasArg(options::OPT_mtune_EQ);
Daniel Dunbar44e71222009-04-29 18:32:25 +00001312
Benjamin Kramercf4371a2009-08-05 14:30:52 +00001313 if (Arg *A = Args.getLastArg(options::OPT_mcmodel_EQ)) {
Daniel Dunbara1b02a22009-11-29 07:18:39 +00001314 CmdArgs.push_back("-mcode-model");
Benjamin Kramercf4371a2009-08-05 14:30:52 +00001315 CmdArgs.push_back(A->getValue(Args));
1316 }
1317
Daniel Dunbar3b3191f2009-09-09 22:33:08 +00001318 // Add target specific cpu and features flags.
1319 switch(getToolChain().getTriple().getArch()) {
1320 default:
1321 break;
Daniel Dunbar4dbaaa62009-05-06 03:16:41 +00001322
Daniel Dunbar0f5c5422009-09-10 04:57:17 +00001323 case llvm::Triple::arm:
1324 case llvm::Triple::thumb:
Daniel Dunbarc9388c12011-03-17 17:10:06 +00001325 AddARMTargetArgs(Args, CmdArgs, KernelOrKext);
Daniel Dunbar0f5c5422009-09-10 04:57:17 +00001326 break;
1327
Eric Christopher0b26a612010-03-02 02:41:08 +00001328 case llvm::Triple::mips:
1329 case llvm::Triple::mipsel:
1330 AddMIPSTargetArgs(Args, CmdArgs);
1331 break;
1332
Bruno Cardoso Lopese7f211c2010-11-09 17:21:19 +00001333 case llvm::Triple::sparc:
1334 AddSparcTargetArgs(Args, CmdArgs);
1335 break;
1336
Daniel Dunbar3b3191f2009-09-09 22:33:08 +00001337 case llvm::Triple::x86:
1338 case llvm::Triple::x86_64:
1339 AddX86TargetArgs(Args, CmdArgs);
1340 break;
Daniel Dunbar44e71222009-04-29 18:32:25 +00001341 }
1342
Daniel Dunbar976a2f52010-08-11 23:07:47 +00001343 // Pass the linker version in use.
1344 if (Arg *A = Args.getLastArg(options::OPT_mlinker_version_EQ)) {
1345 CmdArgs.push_back("-target-linker-version");
1346 CmdArgs.push_back(A->getValue(Args));
1347 }
1348
Nick Lewycky75033772011-02-02 06:43:03 +00001349 // -mno-omit-leaf-frame-pointer is the default on Darwin.
Daniel Dunbarbb7ac522010-07-01 01:31:45 +00001350 if (Args.hasFlag(options::OPT_momit_leaf_frame_pointer,
Nick Lewycky75033772011-02-02 06:43:03 +00001351 options::OPT_mno_omit_leaf_frame_pointer,
1352 getToolChain().getTriple().getOS() != llvm::Triple::Darwin))
Daniel Dunbarbb7ac522010-07-01 01:31:45 +00001353 CmdArgs.push_back("-momit-leaf-frame-pointer");
1354
Dan Gohmand1e76b92010-01-08 02:20:44 +00001355 // -fno-math-errno is default.
1356 if (Args.hasFlag(options::OPT_fmath_errno,
Daniel Dunbar44e71222009-04-29 18:32:25 +00001357 options::OPT_fno_math_errno,
Dan Gohmand1e76b92010-01-08 02:20:44 +00001358 false))
1359 CmdArgs.push_back("-fmath-errno");
Daniel Dunbar44e71222009-04-29 18:32:25 +00001360
Daniel Dunbarfcc49a82010-05-12 18:19:58 +00001361 // Explicitly error on some things we know we don't support and can't just
1362 // ignore.
1363 types::ID InputType = Inputs[0].getType();
Daniel Dunbar4ed214a2010-09-24 19:39:37 +00001364 if (!Args.hasArg(options::OPT_fallow_unsupported)) {
1365 Arg *Unsupported;
Peter Collingbourne77b0e7f22011-07-12 19:35:15 +00001366 if ((Unsupported = Args.getLastArg(options::OPT_iframework)))
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001367 D.Diag(diag::err_drv_clang_unsupported)
Daniel Dunbarfcc49a82010-05-12 18:19:58 +00001368 << Unsupported->getOption().getName();
Daniel Dunbar4ed214a2010-09-24 19:39:37 +00001369
1370 if (types::isCXX(InputType) &&
1371 getToolChain().getTriple().getOS() == llvm::Triple::Darwin &&
1372 getToolChain().getTriple().getArch() == llvm::Triple::x86) {
Bob Wilson0d45f582011-08-13 23:48:55 +00001373 if ((Unsupported = Args.getLastArg(options::OPT_fapple_kext)) ||
1374 (Unsupported = Args.getLastArg(options::OPT_mkernel)))
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001375 D.Diag(diag::err_drv_clang_unsupported_opt_cxx_darwin_i386)
Daniel Dunbar4ed214a2010-09-24 19:39:37 +00001376 << Unsupported->getOption().getName();
1377 }
Daniel Dunbarfcc49a82010-05-12 18:19:58 +00001378 }
1379
Daniel Dunbara2aedc62009-03-18 10:01:51 +00001380 Args.AddAllArgs(CmdArgs, options::OPT_v);
Daniel Dunbard4352752010-08-24 22:44:13 +00001381 Args.AddLastArg(CmdArgs, options::OPT_H);
Chad Rosierbe10f982011-08-02 17:58:04 +00001382 if (D.CCPrintHeaders && !D.CCGenDiagnostics) {
Daniel Dunbarac540b32011-02-02 21:11:35 +00001383 CmdArgs.push_back("-header-include-file");
1384 CmdArgs.push_back(D.CCPrintHeadersFilename ?
1385 D.CCPrintHeadersFilename : "-");
1386 }
Daniel Dunbara2aedc62009-03-18 10:01:51 +00001387 Args.AddLastArg(CmdArgs, options::OPT_P);
Mike Stump11289f42009-09-09 15:08:12 +00001388 Args.AddLastArg(CmdArgs, options::OPT_print_ivar_layout);
Daniel Dunbara2aedc62009-03-18 10:01:51 +00001389
Chad Rosierbe10f982011-08-02 17:58:04 +00001390 if (D.CCLogDiagnostics && !D.CCGenDiagnostics) {
Daniel Dunbar529c03b2011-04-07 18:01:20 +00001391 CmdArgs.push_back("-diagnostic-log-file");
1392 CmdArgs.push_back(D.CCLogDiagnosticsFilename ?
1393 D.CCLogDiagnosticsFilename : "-");
1394 }
1395
Daniel Dunbara2aedc62009-03-18 10:01:51 +00001396 // Special case debug options to only pass -g to clang. This is
1397 // wrong.
Rafael Espindola08a692a2010-03-07 04:46:18 +00001398 Args.ClaimAllArgs(options::OPT_g_Group);
Daniel Dunbar4083d042010-05-12 18:19:55 +00001399 if (Arg *A = Args.getLastArg(options::OPT_g_Group))
1400 if (!A->getOption().matches(options::OPT_g0))
1401 CmdArgs.push_back("-g");
Daniel Dunbara2aedc62009-03-18 10:01:51 +00001402
Rafael Espindola66bfb2752010-05-06 21:06:04 +00001403 Args.AddAllArgs(CmdArgs, options::OPT_ffunction_sections);
1404 Args.AddAllArgs(CmdArgs, options::OPT_fdata_sections);
1405
Chris Lattner3c77a352010-06-22 00:03:40 +00001406 Args.AddAllArgs(CmdArgs, options::OPT_finstrument_functions);
1407
Nick Lewycky207bce32011-04-21 23:44:07 +00001408 if (Args.hasArg(options::OPT_ftest_coverage) ||
1409 Args.hasArg(options::OPT_coverage))
1410 CmdArgs.push_back("-femit-coverage-notes");
1411 if (Args.hasArg(options::OPT_fprofile_arcs) ||
1412 Args.hasArg(options::OPT_coverage))
1413 CmdArgs.push_back("-femit-coverage-data");
1414
Nick Lewycky480cb992011-05-04 20:46:58 +00001415 if (C.getArgs().hasArg(options::OPT_c) ||
1416 C.getArgs().hasArg(options::OPT_S)) {
1417 if (Output.isFilename()) {
Nick Lewycky85c011d2011-05-05 00:08:20 +00001418 CmdArgs.push_back("-coverage-file");
1419 CmdArgs.push_back(Args.MakeArgString(Output.getFilename()));
Nick Lewycky480cb992011-05-04 20:46:58 +00001420 }
1421 }
1422
Daniel Dunbara2aedc62009-03-18 10:01:51 +00001423 Args.AddLastArg(CmdArgs, options::OPT_nostdinc);
Douglas Gregor64b046f2010-03-24 20:13:48 +00001424 Args.AddLastArg(CmdArgs, options::OPT_nostdincxx);
Rafael Espindolab3549d72009-10-26 13:36:57 +00001425 Args.AddLastArg(CmdArgs, options::OPT_nobuiltininc);
Daniel Dunbara2aedc62009-03-18 10:01:51 +00001426
Daniel Dunbar34e0b8c2009-12-15 01:02:52 +00001427 // Pass the path to compiler resource files.
Daniel Dunbar34e0b8c2009-12-15 01:02:52 +00001428 CmdArgs.push_back("-resource-dir");
Daniel Dunbar3f3e2cd2010-01-20 02:35:16 +00001429 CmdArgs.push_back(D.ResourceDir.c_str());
Daniel Dunbar9dc82a22009-04-07 21:42:00 +00001430
Argyrios Kyrtzidis71731d62010-11-03 22:45:23 +00001431 Args.AddLastArg(CmdArgs, options::OPT_working_directory);
1432
John McCalld70fb982011-06-15 23:25:17 +00001433 if (!Args.hasArg(options::OPT_fno_objc_arc)) {
Argyrios Kyrtzidisc44b93d2011-07-07 04:00:39 +00001434 if (const Arg *A = Args.getLastArg(options::OPT_ccc_arcmt_check,
Argyrios Kyrtzidis7fbd97f2011-07-09 20:00:58 +00001435 options::OPT_ccc_arcmt_modify,
1436 options::OPT_ccc_arcmt_migrate)) {
John McCalld70fb982011-06-15 23:25:17 +00001437 switch (A->getOption().getID()) {
1438 default:
1439 llvm_unreachable("missed a case");
Argyrios Kyrtzidisc44b93d2011-07-07 04:00:39 +00001440 case options::OPT_ccc_arcmt_check:
John McCalld70fb982011-06-15 23:25:17 +00001441 CmdArgs.push_back("-arcmt-check");
1442 break;
Argyrios Kyrtzidisc44b93d2011-07-07 04:00:39 +00001443 case options::OPT_ccc_arcmt_modify:
John McCalld70fb982011-06-15 23:25:17 +00001444 CmdArgs.push_back("-arcmt-modify");
1445 break;
Argyrios Kyrtzidis7fbd97f2011-07-09 20:00:58 +00001446 case options::OPT_ccc_arcmt_migrate:
1447 CmdArgs.push_back("-arcmt-migrate");
1448 CmdArgs.push_back("-arcmt-migrate-directory");
1449 CmdArgs.push_back(A->getValue(Args));
Argyrios Kyrtzidisd5713632011-07-19 17:20:03 +00001450
1451 Args.AddLastArg(CmdArgs, options::OPT_arcmt_migrate_report_output);
1452 Args.AddLastArg(CmdArgs, options::OPT_arcmt_migrate_emit_arc_errors);
Argyrios Kyrtzidis7fbd97f2011-07-09 20:00:58 +00001453 break;
John McCalld70fb982011-06-15 23:25:17 +00001454 }
1455 }
1456 }
Eric Christopher84fbdb42011-08-19 00:30:14 +00001457
Daniel Dunbard067f7f2009-04-08 23:54:23 +00001458 // Add preprocessing options like -I, -D, etc. if we are using the
1459 // preprocessor.
1460 //
1461 // FIXME: Support -fpreprocessed
Daniel Dunbard067f7f2009-04-08 23:54:23 +00001462 if (types::getPreprocessedType(InputType) != types::TY_INVALID)
Douglas Gregor111af7d2009-04-18 00:34:01 +00001463 AddPreprocessingOptions(D, Args, CmdArgs, Output, Inputs);
Daniel Dunbara2aedc62009-03-18 10:01:51 +00001464
Rafael Espindolaa7431922011-07-21 23:40:37 +00001465 // Don't warn about "clang -c -DPIC -fPIC test.i" because libtool.m4 assumes
1466 // that "The compiler can only warn and ignore the option if not recognized".
1467 // When building with ccache, it will pass -D options to clang even on
1468 // preprocessed inputs and configure concludes that -fPIC is not supported.
1469 Args.ClaimAllArgs(options::OPT_D);
1470
Daniel Dunbar58f78332009-09-17 06:53:36 +00001471 // Manually translate -O to -O2 and -O4 to -O3; let clang reject
Daniel Dunbar13864952009-03-24 20:17:30 +00001472 // others.
1473 if (Arg *A = Args.getLastArg(options::OPT_O_Group)) {
Daniel Dunbar0bfb21e2009-11-19 03:26:40 +00001474 if (A->getOption().matches(options::OPT_O4))
Daniel Dunbar13864952009-03-24 20:17:30 +00001475 CmdArgs.push_back("-O3");
Daniel Dunbar9296f632010-05-27 06:51:08 +00001476 else if (A->getOption().matches(options::OPT_O) &&
1477 A->getValue(Args)[0] == '\0')
Daniel Dunbar58f78332009-09-17 06:53:36 +00001478 CmdArgs.push_back("-O2");
Daniel Dunbara2aedc62009-03-18 10:01:51 +00001479 else
Daniel Dunbar7ef5ed62009-03-18 23:39:35 +00001480 A->render(Args, CmdArgs);
Daniel Dunbara2aedc62009-03-18 10:01:51 +00001481 }
1482
Daniel Dunbar945577c2009-10-29 02:24:45 +00001483 Args.AddAllArgs(CmdArgs, options::OPT_W_Group);
1484 Args.AddLastArg(CmdArgs, options::OPT_pedantic);
1485 Args.AddLastArg(CmdArgs, options::OPT_pedantic_errors);
Daniel Dunbara2aedc62009-03-18 10:01:51 +00001486 Args.AddLastArg(CmdArgs, options::OPT_w);
Daniel Dunbarc44b4cc2009-04-07 22:13:21 +00001487
1488 // Handle -{std, ansi, trigraphs} -- take the last of -{std, ansi}
1489 // (-ansi is equivalent to -std=c89).
1490 //
1491 // If a std is supplied, only add -trigraphs if it follows the
1492 // option.
1493 if (Arg *Std = Args.getLastArg(options::OPT_std_EQ, options::OPT_ansi)) {
1494 if (Std->getOption().matches(options::OPT_ansi))
Nuno Lopes275225d2009-10-16 14:28:06 +00001495 if (types::isCXX(InputType))
Daniel Dunbar1c7577c2009-11-04 06:24:38 +00001496 CmdArgs.push_back("-std=c++98");
Nuno Lopes275225d2009-10-16 14:28:06 +00001497 else
Daniel Dunbar1c7577c2009-11-04 06:24:38 +00001498 CmdArgs.push_back("-std=c89");
Daniel Dunbarc44b4cc2009-04-07 22:13:21 +00001499 else
1500 Std->render(Args, CmdArgs);
1501
Daniel Dunbar3f1a1ff2010-06-14 21:23:08 +00001502 if (Arg *A = Args.getLastArg(options::OPT_std_EQ, options::OPT_ansi,
1503 options::OPT_trigraphs))
1504 if (A != Std)
Daniel Dunbarc44b4cc2009-04-07 22:13:21 +00001505 A->render(Args, CmdArgs);
Daniel Dunbar72a60902009-04-26 01:10:38 +00001506 } else {
1507 // Honor -std-default.
Daniel Dunbar12998192010-01-29 21:03:02 +00001508 //
1509 // FIXME: Clang doesn't correctly handle -std= when the input language
1510 // doesn't match. For the time being just ignore this for C++ inputs;
1511 // eventually we want to do all the standard defaulting here instead of
1512 // splitting it between the driver and clang -cc1.
1513 if (!types::isCXX(InputType))
1514 Args.AddAllArgsTranslated(CmdArgs, options::OPT_std_default_EQ,
1515 "-std=", /*Joined=*/true);
Daniel Dunbarc44b4cc2009-04-07 22:13:21 +00001516 Args.AddLastArg(CmdArgs, options::OPT_trigraphs);
Daniel Dunbar72a60902009-04-26 01:10:38 +00001517 }
Daniel Dunbard067f7f2009-04-08 23:54:23 +00001518
Chandler Carruthb009b142011-04-23 06:30:43 +00001519 // Map the bizarre '-Wwrite-strings' flag to a more sensible
1520 // '-fconst-strings'; this better indicates its actual behavior.
1521 if (Args.hasFlag(options::OPT_Wwrite_strings, options::OPT_Wno_write_strings,
1522 false)) {
1523 // For perfect compatibility with GCC, we do this even in the presence of
1524 // '-w'. This flag names something other than a warning for GCC.
1525 CmdArgs.push_back("-fconst-strings");
1526 }
1527
Chandler Carruth61fbf622011-04-23 09:27:53 +00001528 // GCC provides a macro definition '__DEPRECATED' when -Wdeprecated is active
Chandler Carruth30483fb2011-04-23 19:48:40 +00001529 // during C++ compilation, which it is by default. GCC keeps this define even
1530 // in the presence of '-w', match this behavior bug-for-bug.
1531 if (types::isCXX(InputType) &&
1532 Args.hasFlag(options::OPT_Wdeprecated, options::OPT_Wno_deprecated,
1533 true)) {
1534 CmdArgs.push_back("-fdeprecated-macro");
Chandler Carruth61fbf622011-04-23 09:27:53 +00001535 }
1536
Chandler Carruthe0391482010-05-22 02:21:53 +00001537 // Translate GCC's misnamer '-fasm' arguments to '-fgnu-keywords'.
1538 if (Arg *Asm = Args.getLastArg(options::OPT_fasm, options::OPT_fno_asm)) {
1539 if (Asm->getOption().matches(options::OPT_fasm))
1540 CmdArgs.push_back("-fgnu-keywords");
1541 else
1542 CmdArgs.push_back("-fno-gnu-keywords");
1543 }
1544
Rafael Espindola4cfa7972011-05-02 17:43:32 +00001545 if (ShouldDisableCFI(Args, getToolChain()))
1546 CmdArgs.push_back("-fno-dwarf2-cfi-asm");
Rafael Espindolae2641872011-04-30 18:35:43 +00001547
Daniel Dunbara2aedc62009-03-18 10:01:51 +00001548 if (Arg *A = Args.getLastArg(options::OPT_ftemplate_depth_)) {
1549 CmdArgs.push_back("-ftemplate-depth");
1550 CmdArgs.push_back(A->getValue(Args));
1551 }
1552
Argyrios Kyrtzidisef6c8da2010-11-18 00:20:36 +00001553 if (Arg *A = Args.getLastArg(options::OPT_Wlarge_by_value_copy_EQ,
1554 options::OPT_Wlarge_by_value_copy_def)) {
1555 CmdArgs.push_back("-Wlarge-by-value-copy");
Argyrios Kyrtzidisaf84ec02010-11-17 23:11:54 +00001556 if (A->getNumValues())
1557 CmdArgs.push_back(A->getValue(Args));
1558 else
Argyrios Kyrtzidisef6c8da2010-11-18 00:20:36 +00001559 CmdArgs.push_back("64"); // default value for -Wlarge-by-value-copy.
Argyrios Kyrtzidisaf84ec02010-11-17 23:11:54 +00001560 }
1561
Daniel Dunbarfffd1812009-11-19 04:00:53 +00001562 if (Args.hasArg(options::OPT__relocatable_pch))
Daniel Dunbar8bed86c2009-11-20 22:21:36 +00001563 CmdArgs.push_back("-relocatable-pch");
Mike Stump11289f42009-09-09 15:08:12 +00001564
Daniel Dunbar1c7577c2009-11-04 06:24:38 +00001565 if (Arg *A = Args.getLastArg(options::OPT_fconstant_string_class_EQ)) {
1566 CmdArgs.push_back("-fconstant-string-class");
1567 CmdArgs.push_back(A->getValue(Args));
1568 }
David Chisnall5778fce2009-08-31 16:41:57 +00001569
Chris Lattnere23003d2010-01-09 21:54:33 +00001570 if (Arg *A = Args.getLastArg(options::OPT_ftabstop_EQ)) {
1571 CmdArgs.push_back("-ftabstop");
1572 CmdArgs.push_back(A->getValue(Args));
1573 }
1574
Chris Lattnerb35583d2010-04-07 20:49:23 +00001575 CmdArgs.push_back("-ferror-limit");
1576 if (Arg *A = Args.getLastArg(options::OPT_ferror_limit_EQ))
1577 CmdArgs.push_back(A->getValue(Args));
1578 else
1579 CmdArgs.push_back("19");
Douglas Gregorffed1cb2010-04-20 07:18:24 +00001580
Chandler Carrutha77a7272010-05-06 04:55:18 +00001581 if (Arg *A = Args.getLastArg(options::OPT_fmacro_backtrace_limit_EQ)) {
1582 CmdArgs.push_back("-fmacro-backtrace-limit");
Douglas Gregorcd121fb2010-05-04 17:13:42 +00001583 CmdArgs.push_back(A->getValue(Args));
Chandler Carrutha77a7272010-05-06 04:55:18 +00001584 }
1585
1586 if (Arg *A = Args.getLastArg(options::OPT_ftemplate_backtrace_limit_EQ)) {
1587 CmdArgs.push_back("-ftemplate-backtrace-limit");
Douglas Gregorffed1cb2010-04-20 07:18:24 +00001588 CmdArgs.push_back(A->getValue(Args));
Chandler Carrutha77a7272010-05-06 04:55:18 +00001589 }
1590
Daniel Dunbar2c978472009-11-04 06:24:47 +00001591 // Pass -fmessage-length=.
Daniel Dunbar84bb7932009-11-30 08:40:54 +00001592 CmdArgs.push_back("-fmessage-length");
Daniel Dunbar2c978472009-11-04 06:24:47 +00001593 if (Arg *A = Args.getLastArg(options::OPT_fmessage_length_EQ)) {
Daniel Dunbar84bb7932009-11-30 08:40:54 +00001594 CmdArgs.push_back(A->getValue(Args));
Daniel Dunbar2c978472009-11-04 06:24:47 +00001595 } else {
1596 // If -fmessage-length=N was not specified, determine whether this is a
1597 // terminal and, if so, implicitly define -fmessage-length appropriately.
1598 unsigned N = llvm::sys::Process::StandardErrColumns();
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001599 CmdArgs.push_back(Args.MakeArgString(Twine(N)));
Daniel Dunbar2c978472009-11-04 06:24:47 +00001600 }
1601
Daniel Dunbare357d562009-12-03 18:42:11 +00001602 if (const Arg *A = Args.getLastArg(options::OPT_fvisibility_EQ)) {
1603 CmdArgs.push_back("-fvisibility");
1604 CmdArgs.push_back(A->getValue(Args));
1605 }
1606
Douglas Gregor08329632010-06-15 17:05:35 +00001607 Args.AddLastArg(CmdArgs, options::OPT_fvisibility_inlines_hidden);
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001608
Daniel Dunbare46b52a2010-03-20 04:52:14 +00001609 // -fhosted is default.
1610 if (KernelOrKext || Args.hasFlag(options::OPT_ffreestanding,
1611 options::OPT_fhosted,
1612 false))
1613 CmdArgs.push_back("-ffreestanding");
1614
Daniel Dunbare357d562009-12-03 18:42:11 +00001615 // Forward -f (flag) options which we can pass directly.
Mike Stumpd9546382009-12-12 01:27:46 +00001616 Args.AddLastArg(CmdArgs, options::OPT_fcatch_undefined_behavior);
Daniel Dunbar3a148f22009-04-07 21:51:40 +00001617 Args.AddLastArg(CmdArgs, options::OPT_femit_all_decls);
Daniel Dunbar3a148f22009-04-07 21:51:40 +00001618 Args.AddLastArg(CmdArgs, options::OPT_fheinous_gnu_extensions);
Devang Patel91bbb552010-09-30 19:05:55 +00001619 Args.AddLastArg(CmdArgs, options::OPT_flimit_debug_info);
Daniel Dunbar733b0f82011-03-01 18:49:30 +00001620 if (getToolChain().SupportsProfiling())
1621 Args.AddLastArg(CmdArgs, options::OPT_pg);
Daniel Dunbar35621a92010-03-16 16:57:46 +00001622
1623 // -flax-vector-conversions is default.
1624 if (!Args.hasFlag(options::OPT_flax_vector_conversions,
1625 options::OPT_fno_lax_vector_conversions))
1626 CmdArgs.push_back("-fno-lax-vector-conversions");
1627
Fariborz Jahaniana4cfff82011-01-07 01:05:02 +00001628 if (Args.getLastArg(options::OPT_fapple_kext))
1629 CmdArgs.push_back("-fapple-kext");
1630
Fariborz Jahaniana4404f22009-05-22 20:17:16 +00001631 Args.AddLastArg(CmdArgs, options::OPT_fobjc_sender_dependent_dispatch);
Chris Lattner69686412009-04-21 05:34:31 +00001632 Args.AddLastArg(CmdArgs, options::OPT_fdiagnostics_print_source_range_info);
Douglas Gregoreec975c2010-08-19 20:24:43 +00001633 Args.AddLastArg(CmdArgs, options::OPT_fdiagnostics_parseable_fixits);
Daniel Dunbar3a148f22009-04-07 21:51:40 +00001634 Args.AddLastArg(CmdArgs, options::OPT_ftime_report);
1635 Args.AddLastArg(CmdArgs, options::OPT_ftrapv);
David Chisnalldd84ef12010-09-17 18:29:54 +00001636
1637 if (Arg *A = Args.getLastArg(options::OPT_ftrapv_handler_EQ)) {
1638 CmdArgs.push_back("-ftrapv-handler");
1639 CmdArgs.push_back(A->getValue(Args));
1640 }
1641
Evan Cheng04c94292011-04-08 21:37:45 +00001642 // Forward -ftrap_function= options to the backend.
1643 if (Arg *A = Args.getLastArg(options::OPT_ftrap_function_EQ)) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001644 StringRef FuncName = A->getValue(Args);
Evan Cheng04c94292011-04-08 21:37:45 +00001645 CmdArgs.push_back("-backend-option");
1646 CmdArgs.push_back(Args.MakeArgString("-trap-func=" + FuncName));
1647 }
1648
Chandler Carruth6e501032011-03-27 00:04:55 +00001649 // -fno-strict-overflow implies -fwrapv if it isn't disabled, but
1650 // -fstrict-overflow won't turn off an explicitly enabled -fwrapv.
1651 if (Arg *A = Args.getLastArg(options::OPT_fwrapv,
1652 options::OPT_fno_wrapv)) {
1653 if (A->getOption().matches(options::OPT_fwrapv))
1654 CmdArgs.push_back("-fwrapv");
1655 } else if (Arg *A = Args.getLastArg(options::OPT_fstrict_overflow,
1656 options::OPT_fno_strict_overflow)) {
1657 if (A->getOption().matches(options::OPT_fno_strict_overflow))
1658 CmdArgs.push_back("-fwrapv");
1659 }
Daniel Dunbar3a148f22009-04-07 21:51:40 +00001660 Args.AddLastArg(CmdArgs, options::OPT_fwritable_strings);
Eric Christopherf387dbd2010-08-07 23:08:14 +00001661 Args.AddLastArg(CmdArgs, options::OPT_funroll_loops);
Daniel Dunbara2aedc62009-03-18 10:01:51 +00001662
Daniel Dunbara77eaeb2009-09-03 04:54:28 +00001663 Args.AddLastArg(CmdArgs, options::OPT_pthread);
1664
Daniel Dunbar4930e332009-11-17 08:07:36 +00001665 // -stack-protector=0 is default.
1666 unsigned StackProtectorLevel = 0;
Bill Wendlingd63bbad2009-06-28 07:36:13 +00001667 if (Arg *A = Args.getLastArg(options::OPT_fno_stack_protector,
1668 options::OPT_fstack_protector_all,
1669 options::OPT_fstack_protector)) {
Daniel Dunbar4930e332009-11-17 08:07:36 +00001670 if (A->getOption().matches(options::OPT_fstack_protector))
1671 StackProtectorLevel = 1;
1672 else if (A->getOption().matches(options::OPT_fstack_protector_all))
1673 StackProtectorLevel = 2;
Nico Weberdd473632011-08-23 07:38:27 +00001674 } else {
1675 StackProtectorLevel =
1676 getToolChain().GetDefaultStackProtectorLevel(KernelOrKext);
1677 }
Daniel Dunbar4930e332009-11-17 08:07:36 +00001678 if (StackProtectorLevel) {
1679 CmdArgs.push_back("-stack-protector");
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001680 CmdArgs.push_back(Args.MakeArgString(Twine(StackProtectorLevel)));
Bill Wendlingd63bbad2009-06-28 07:36:13 +00001681 }
1682
Eric Christopherd5c45f62011-05-02 21:18:22 +00001683 // Translate -mstackrealign
1684 if (Args.hasArg(options::OPT_mstackrealign)) {
1685 CmdArgs.push_back("-backend-option");
1686 CmdArgs.push_back("-force-align-stack");
1687 }
Eric Christopher84fbdb42011-08-19 00:30:14 +00001688
Daniel Dunbard18049a2009-04-07 21:16:11 +00001689 // Forward -f options with positive and negative forms; we translate
1690 // these by hand.
1691
Fariborz Jahaniana4cfff82011-01-07 01:05:02 +00001692 if (Args.hasArg(options::OPT_mkernel)) {
Daniel Dunbar80f787c2011-02-04 17:24:47 +00001693 if (!Args.hasArg(options::OPT_fapple_kext) && types::isCXX(InputType))
Fariborz Jahaniana4cfff82011-01-07 01:05:02 +00001694 CmdArgs.push_back("-fapple-kext");
1695 if (!Args.hasArg(options::OPT_fbuiltin))
1696 CmdArgs.push_back("-fno-builtin");
1697 }
Daniel Dunbar4930e332009-11-17 08:07:36 +00001698 // -fbuiltin is default.
Fariborz Jahaniana4cfff82011-01-07 01:05:02 +00001699 else if (!Args.hasFlag(options::OPT_fbuiltin, options::OPT_fno_builtin))
Daniel Dunbar484afa22009-11-19 04:55:23 +00001700 CmdArgs.push_back("-fno-builtin");
Daniel Dunbard18049a2009-04-07 21:16:11 +00001701
Nuno Lopes13c88c72009-12-16 16:59:22 +00001702 if (!Args.hasFlag(options::OPT_fassume_sane_operator_new,
1703 options::OPT_fno_assume_sane_operator_new))
1704 CmdArgs.push_back("-fno-assume-sane-operator-new");
1705
Daniel Dunbar4930e332009-11-17 08:07:36 +00001706 // -fblocks=0 is default.
1707 if (Args.hasFlag(options::OPT_fblocks, options::OPT_fno_blocks,
David Chisnallda209912011-02-28 17:11:43 +00001708 getToolChain().IsBlocksDefault()) ||
1709 (Args.hasArg(options::OPT_fgnu_runtime) &&
1710 Args.hasArg(options::OPT_fobjc_nonfragile_abi) &&
1711 !Args.hasArg(options::OPT_fno_blocks))) {
Daniel Dunbar4930e332009-11-17 08:07:36 +00001712 CmdArgs.push_back("-fblocks");
David Chisnall950a9512009-11-17 19:33:30 +00001713 }
Daniel Dunbard18049a2009-04-07 21:16:11 +00001714
John McCalldfea9982010-04-09 19:12:06 +00001715 // -faccess-control is default.
John McCall3155f572010-04-09 19:03:51 +00001716 if (Args.hasFlag(options::OPT_fno_access_control,
1717 options::OPT_faccess_control,
John McCalldfea9982010-04-09 19:12:06 +00001718 false))
John McCall3155f572010-04-09 19:03:51 +00001719 CmdArgs.push_back("-fno-access-control");
John McCall59bb1d42010-03-17 01:32:13 +00001720
Anders Carlssond470fef2010-11-21 00:09:52 +00001721 // -felide-constructors is the default.
1722 if (Args.hasFlag(options::OPT_fno_elide_constructors,
1723 options::OPT_felide_constructors,
1724 false))
1725 CmdArgs.push_back("-fno-elide-constructors");
1726
Daniel Dunbar0730e4f2009-11-17 07:06:20 +00001727 // -frtti is default.
Daniel Dunbare46b52a2010-03-20 04:52:14 +00001728 if (KernelOrKext ||
1729 !Args.hasFlag(options::OPT_frtti, options::OPT_fno_rtti))
Daniel Dunbar484afa22009-11-19 04:55:23 +00001730 CmdArgs.push_back("-fno-rtti");
Mike Stump183c3d22009-07-31 23:15:31 +00001731
Argyrios Kyrtzidis74825bc2010-10-08 00:25:19 +00001732 // -fshort-enums=0 is default.
1733 // FIXME: Are there targers where -fshort-enums is on by default ?
1734 if (Args.hasFlag(options::OPT_fshort_enums,
1735 options::OPT_fno_short_enums, false))
1736 CmdArgs.push_back("-fshort-enums");
1737
Daniel Dunbard609b7b2009-11-17 06:37:03 +00001738 // -fsigned-char is default.
Daniel Dunbar5bdd2992009-11-25 10:14:30 +00001739 if (!Args.hasFlag(options::OPT_fsigned_char, options::OPT_funsigned_char,
Daniel Dunbard609b7b2009-11-17 06:37:03 +00001740 isSignedCharDefault(getToolChain().getTriple())))
Daniel Dunbar5fe08662009-11-29 02:39:08 +00001741 CmdArgs.push_back("-fno-signed-char");
Eli Friedman327f0b52009-06-05 07:21:14 +00001742
Anders Carlssonfcd764a2010-02-06 23:23:06 +00001743 // -fthreadsafe-static is default.
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001744 if (!Args.hasFlag(options::OPT_fthreadsafe_statics,
Anders Carlssonfcd764a2010-02-06 23:23:06 +00001745 options::OPT_fno_threadsafe_statics))
1746 CmdArgs.push_back("-fno-threadsafe-statics");
1747
Daniel Dunbarfe06df42010-03-20 04:15:41 +00001748 // -fuse-cxa-atexit is default.
Anton Korobeynikov82b33332010-09-11 11:17:06 +00001749 if (KernelOrKext ||
1750 !Args.hasFlag(options::OPT_fuse_cxa_atexit, options::OPT_fno_use_cxa_atexit,
NAKAMURA Takumi6bdc8a22010-10-10 01:53:03 +00001751 getToolChain().getTriple().getOS() != llvm::Triple::Cygwin &&
NAKAMURA Takumi31ea2f12011-02-17 08:51:38 +00001752 getToolChain().getTriple().getOS() != llvm::Triple::MinGW32))
Daniel Dunbarfe06df42010-03-20 04:15:41 +00001753 CmdArgs.push_back("-fno-use-cxa-atexit");
1754
Daniel Dunbar0730e4f2009-11-17 07:06:20 +00001755 // -fms-extensions=0 is default.
Daniel Dunbar5bdd2992009-11-25 10:14:30 +00001756 if (Args.hasFlag(options::OPT_fms_extensions, options::OPT_fno_ms_extensions,
Daniel Dunbar0730e4f2009-11-17 07:06:20 +00001757 getToolChain().getTriple().getOS() == llvm::Triple::Win32))
1758 CmdArgs.push_back("-fms-extensions");
1759
Michael J. Spencer4992ca4b2010-10-21 05:21:48 +00001760 // -fmsc-version=1300 is default.
1761 if (Args.hasFlag(options::OPT_fms_extensions, options::OPT_fno_ms_extensions,
1762 getToolChain().getTriple().getOS() == llvm::Triple::Win32) ||
1763 Args.hasArg(options::OPT_fmsc_version)) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001764 StringRef msc_ver = Args.getLastArgValue(options::OPT_fmsc_version);
Michael J. Spencer4992ca4b2010-10-21 05:21:48 +00001765 if (msc_ver.empty())
1766 CmdArgs.push_back("-fmsc-version=1300");
1767 else
1768 CmdArgs.push_back(Args.MakeArgString("-fmsc-version=" + msc_ver));
1769 }
1770
1771
Dawn Perchik68bb1b42010-09-02 23:59:25 +00001772 // -fborland-extensions=0 is default.
1773 if (Args.hasFlag(options::OPT_fborland_extensions,
1774 options::OPT_fno_borland_extensions, false))
1775 CmdArgs.push_back("-fborland-extensions");
1776
Francois Pichet1c229c02011-04-22 22:18:13 +00001777 // -fno-delayed-template-parsing is default.
1778 if (Args.hasFlag(options::OPT_fdelayed_template_parsing,
1779 options::OPT_fno_delayed_template_parsing,
1780 false))
1781 CmdArgs.push_back("-fdelayed-template-parsing");
1782
Chandler Carruthe03aa552010-04-17 20:17:31 +00001783 // -fgnu-keywords default varies depending on language; only pass if
1784 // specified.
1785 if (Arg *A = Args.getLastArg(options::OPT_fgnu_keywords,
Daniel Dunbardb059592010-04-24 17:56:39 +00001786 options::OPT_fno_gnu_keywords))
1787 A->render(Args, CmdArgs);
Chandler Carruthe03aa552010-04-17 20:17:31 +00001788
Rafael Espindola922a6242011-06-02 17:30:53 +00001789 if (Args.hasFlag(options::OPT_fgnu89_inline,
1790 options::OPT_fno_gnu89_inline,
1791 false))
Rafael Espindolafb2af642011-06-02 16:13:27 +00001792 CmdArgs.push_back("-fgnu89-inline");
1793
Daniel Dunbar4930e332009-11-17 08:07:36 +00001794 // -fobjc-nonfragile-abi=0 is default.
John McCall24fc0de2011-07-06 00:26:06 +00001795 ObjCRuntime objCRuntime;
John McCallb5f652e2011-06-22 00:53:57 +00001796 unsigned objcABIVersion = 0;
Daniel Dunbar4930e332009-11-17 08:07:36 +00001797 if (types::isObjC(InputType)) {
John McCall24fc0de2011-07-06 00:26:06 +00001798 bool NeXTRuntimeIsDefault
1799 = (IsRewriter || getToolChain().getTriple().isOSDarwin());
1800 if (Args.hasFlag(options::OPT_fnext_runtime, options::OPT_fgnu_runtime,
John McCall75bc7732011-07-06 02:36:30 +00001801 NeXTRuntimeIsDefault)) {
John McCall24fc0de2011-07-06 00:26:06 +00001802 objCRuntime.setKind(ObjCRuntime::NeXT);
John McCall75bc7732011-07-06 02:36:30 +00001803 } else {
1804 CmdArgs.push_back("-fgnu-runtime");
John McCall24fc0de2011-07-06 00:26:06 +00001805 objCRuntime.setKind(ObjCRuntime::GNU);
John McCall75bc7732011-07-06 02:36:30 +00001806 }
John McCall24fc0de2011-07-06 00:26:06 +00001807 getToolChain().configureObjCRuntime(objCRuntime);
1808 if (objCRuntime.HasARC)
1809 CmdArgs.push_back("-fobjc-runtime-has-arc");
1810 if (objCRuntime.HasWeak)
1811 CmdArgs.push_back("-fobjc-runtime-has-weak");
John McCall9de19782011-07-06 01:22:26 +00001812 if (objCRuntime.HasTerminate)
1813 CmdArgs.push_back("-fobjc-runtime-has-terminate");
John McCall24fc0de2011-07-06 00:26:06 +00001814
Daniel Dunbarc1dd0e92010-09-20 18:19:55 +00001815 // Compute the Objective-C ABI "version" to use. Version numbers are
1816 // slightly confusing for historical reasons:
1817 // 1 - Traditional "fragile" ABI
1818 // 2 - Non-fragile ABI, version 1
1819 // 3 - Non-fragile ABI, version 2
John McCallb5f652e2011-06-22 00:53:57 +00001820 objcABIVersion = 1;
Daniel Dunbarc1dd0e92010-09-20 18:19:55 +00001821 // If -fobjc-abi-version= is present, use that to set the version.
Daniel Dunbar12c82082010-04-28 23:25:24 +00001822 if (Arg *A = Args.getLastArg(options::OPT_fobjc_abi_version_EQ)) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001823 if (StringRef(A->getValue(Args)) == "1")
John McCallb5f652e2011-06-22 00:53:57 +00001824 objcABIVersion = 1;
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001825 else if (StringRef(A->getValue(Args)) == "2")
John McCallb5f652e2011-06-22 00:53:57 +00001826 objcABIVersion = 2;
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001827 else if (StringRef(A->getValue(Args)) == "3")
John McCallb5f652e2011-06-22 00:53:57 +00001828 objcABIVersion = 3;
Daniel Dunbar12c82082010-04-28 23:25:24 +00001829 else
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001830 D.Diag(diag::err_drv_clang_unsupported) << A->getAsString(Args);
Daniel Dunbarc1dd0e92010-09-20 18:19:55 +00001831 } else {
1832 // Otherwise, determine if we are using the non-fragile ABI.
1833 if (Args.hasFlag(options::OPT_fobjc_nonfragile_abi,
1834 options::OPT_fno_objc_nonfragile_abi,
1835 getToolChain().IsObjCNonFragileABIDefault())) {
1836 // Determine the non-fragile ABI version to use.
Daniel Dunbaraeed5fe2010-11-11 16:08:59 +00001837#ifdef DISABLE_DEFAULT_NONFRAGILEABI_TWO
1838 unsigned NonFragileABIVersion = 1;
1839#else
Daniel Dunbarc1dd0e92010-09-20 18:19:55 +00001840 unsigned NonFragileABIVersion = 2;
Daniel Dunbaraeed5fe2010-11-11 16:08:59 +00001841#endif
Daniel Dunbarc1dd0e92010-09-20 18:19:55 +00001842
1843 if (Arg *A = Args.getLastArg(
1844 options::OPT_fobjc_nonfragile_abi_version_EQ)) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001845 if (StringRef(A->getValue(Args)) == "1")
Daniel Dunbarc1dd0e92010-09-20 18:19:55 +00001846 NonFragileABIVersion = 1;
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001847 else if (StringRef(A->getValue(Args)) == "2")
Daniel Dunbarc1dd0e92010-09-20 18:19:55 +00001848 NonFragileABIVersion = 2;
1849 else
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001850 D.Diag(diag::err_drv_clang_unsupported)
Daniel Dunbarc1dd0e92010-09-20 18:19:55 +00001851 << A->getAsString(Args);
1852 }
1853
John McCallb5f652e2011-06-22 00:53:57 +00001854 objcABIVersion = 1 + NonFragileABIVersion;
Daniel Dunbarc1dd0e92010-09-20 18:19:55 +00001855 } else {
John McCallb5f652e2011-06-22 00:53:57 +00001856 objcABIVersion = 1;
Daniel Dunbarc1dd0e92010-09-20 18:19:55 +00001857 }
Daniel Dunbar12c82082010-04-28 23:25:24 +00001858 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001859
John McCallb5f652e2011-06-22 00:53:57 +00001860 if (objcABIVersion == 2 || objcABIVersion == 3) {
Fariborz Jahanian3aa19e9a2011-01-04 20:05:20 +00001861 CmdArgs.push_back("-fobjc-nonfragile-abi");
Daniel Dunbarfca18c1b42010-04-24 17:56:46 +00001862
1863 // -fobjc-dispatch-method is only relevant with the nonfragile-abi, and
1864 // legacy is the default.
1865 if (!Args.hasFlag(options::OPT_fobjc_legacy_dispatch,
1866 options::OPT_fno_objc_legacy_dispatch,
1867 getToolChain().IsObjCLegacyDispatchDefault())) {
1868 if (getToolChain().UseObjCMixedDispatch())
1869 CmdArgs.push_back("-fobjc-dispatch-method=mixed");
1870 else
1871 CmdArgs.push_back("-fobjc-dispatch-method=non-legacy");
1872 }
Daniel Dunbar2e3f2c82010-02-01 21:07:25 +00001873 }
NAKAMURA Takumi029d74b2011-02-17 08:50:50 +00001874
Ted Kremenek414a2c02011-04-28 19:26:03 +00001875 // FIXME: Don't expose -fobjc-default-synthesize-properties as a top-level
1876 // driver flag yet. This feature is still under active development
1877 // and shouldn't be exposed as a user visible feature (which may change).
1878 // Clang still supports this as a -cc1 option for development and testing.
1879#if 0
Ted Kremenek1d56c9e2010-12-23 21:35:43 +00001880 // -fobjc-default-synthesize-properties=0 is default.
1881 if (Args.hasFlag(options::OPT_fobjc_default_synthesize_properties,
1882 options::OPT_fno_objc_default_synthesize_properties,
1883 getToolChain().IsObjCDefaultSynthPropertiesDefault())) {
1884 CmdArgs.push_back("-fobjc-default-synthesize-properties");
1885 }
Ted Kremenek414a2c02011-04-28 19:26:03 +00001886#endif
Daniel Dunbar4930e332009-11-17 08:07:36 +00001887 }
1888
John McCall24fc0de2011-07-06 00:26:06 +00001889 // Allow -fno-objc-arr to trump -fobjc-arr/-fobjc-arc.
1890 // NOTE: This logic is duplicated in ToolChains.cpp.
1891 bool ARC = isObjCAutoRefCount(Args);
1892 if (ARC) {
1893 CmdArgs.push_back("-fobjc-arc");
1894
1895 // Allow the user to enable full exceptions code emission.
1896 // We define off for Objective-CC, on for Objective-C++.
1897 if (Args.hasFlag(options::OPT_fobjc_arc_exceptions,
1898 options::OPT_fno_objc_arc_exceptions,
1899 /*default*/ types::isCXX(InputType)))
1900 CmdArgs.push_back("-fobjc-arc-exceptions");
1901 }
1902
1903 // -fobjc-infer-related-result-type is the default, except in the Objective-C
1904 // rewriter.
1905 if (IsRewriter)
1906 CmdArgs.push_back("-fno-objc-infer-related-result-type");
Eric Christopher84fbdb42011-08-19 00:30:14 +00001907
John McCall24fc0de2011-07-06 00:26:06 +00001908 // Handle -fobjc-gc and -fobjc-gc-only. They are exclusive, and -fobjc-gc-only
1909 // takes precedence.
1910 const Arg *GCArg = Args.getLastArg(options::OPT_fobjc_gc_only);
1911 if (!GCArg)
1912 GCArg = Args.getLastArg(options::OPT_fobjc_gc);
1913 if (GCArg) {
1914 if (ARC) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001915 D.Diag(diag::err_drv_objc_gc_arr)
John McCall24fc0de2011-07-06 00:26:06 +00001916 << GCArg->getAsString(Args);
1917 } else if (getToolChain().SupportsObjCGC()) {
1918 GCArg->render(Args, CmdArgs);
1919 } else {
1920 // FIXME: We should move this to a hard error.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001921 D.Diag(diag::warn_drv_objc_gc_unsupported)
John McCall24fc0de2011-07-06 00:26:06 +00001922 << GCArg->getAsString(Args);
1923 }
1924 }
1925
John McCallb5f652e2011-06-22 00:53:57 +00001926 // Add exception args.
1927 addExceptionArgs(Args, InputType, getToolChain().getTriple(),
1928 KernelOrKext, IsRewriter, objcABIVersion, CmdArgs);
1929
1930 if (getToolChain().UseSjLjExceptions())
1931 CmdArgs.push_back("-fsjlj-exceptions");
1932
1933 // C++ "sane" operator new.
Daniel Dunbar2e3f2c82010-02-01 21:07:25 +00001934 if (!Args.hasFlag(options::OPT_fassume_sane_operator_new,
1935 options::OPT_fno_assume_sane_operator_new))
1936 CmdArgs.push_back("-fno-assume-sane-operator-new");
1937
Daniel Dunbar34d7a992010-04-27 15:34:57 +00001938 // -fconstant-cfstrings is default, and may be subject to argument translation
1939 // on Darwin.
1940 if (!Args.hasFlag(options::OPT_fconstant_cfstrings,
1941 options::OPT_fno_constant_cfstrings) ||
1942 !Args.hasFlag(options::OPT_mconstant_cfstrings,
1943 options::OPT_mno_constant_cfstrings))
1944 CmdArgs.push_back("-fno-constant-cfstrings");
1945
John Thompsoned4e2952009-11-05 20:14:16 +00001946 // -fshort-wchar default varies depending on platform; only
1947 // pass if specified.
Daniel Dunbar2cb4e7a2010-04-27 15:35:03 +00001948 if (Arg *A = Args.getLastArg(options::OPT_fshort_wchar))
1949 A->render(Args, CmdArgs);
John Thompsoned4e2952009-11-05 20:14:16 +00001950
Daniel Dunbar1a8a2e82009-10-29 02:39:57 +00001951 // -fno-pascal-strings is default, only pass non-default. If the tool chain
1952 // happened to translate to -mpascal-strings, we want to back translate here.
Daniel Dunbard4510f22009-04-07 23:51:44 +00001953 //
1954 // FIXME: This is gross; that translation should be pulled from the
1955 // tool chain.
Daniel Dunbard067f7f2009-04-08 23:54:23 +00001956 if (Args.hasFlag(options::OPT_fpascal_strings,
Daniel Dunbard4510f22009-04-07 23:51:44 +00001957 options::OPT_fno_pascal_strings,
1958 false) ||
1959 Args.hasFlag(options::OPT_mpascal_strings,
1960 options::OPT_mno_pascal_strings,
1961 false))
Daniel Dunbard18049a2009-04-07 21:16:11 +00001962 CmdArgs.push_back("-fpascal-strings");
NAKAMURA Takumi029d74b2011-02-17 08:50:50 +00001963
Fariborz Jahaniana4cfff82011-01-07 01:05:02 +00001964 if (Args.hasArg(options::OPT_mkernel) ||
1965 Args.hasArg(options::OPT_fapple_kext)) {
1966 if (!Args.hasArg(options::OPT_fcommon))
1967 CmdArgs.push_back("-fno-common");
1968 }
Daniel Dunbard18049a2009-04-07 21:16:11 +00001969 // -fcommon is default, only pass non-default.
Fariborz Jahaniana4cfff82011-01-07 01:05:02 +00001970 else if (!Args.hasFlag(options::OPT_fcommon, options::OPT_fno_common))
Daniel Dunbard18049a2009-04-07 21:16:11 +00001971 CmdArgs.push_back("-fno-common");
1972
Daniel Dunbar2edd9232009-04-15 02:37:43 +00001973 // -fsigned-bitfields is default, and clang doesn't yet support
Daniel Dunbar6358d682010-10-15 22:30:42 +00001974 // -funsigned-bitfields.
Mike Stump11289f42009-09-09 15:08:12 +00001975 if (!Args.hasFlag(options::OPT_fsigned_bitfields,
Daniel Dunbar2edd9232009-04-15 02:37:43 +00001976 options::OPT_funsigned_bitfields))
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001977 D.Diag(diag::warn_drv_clang_unsupported)
Daniel Dunbar2edd9232009-04-15 02:37:43 +00001978 << Args.getLastArg(options::OPT_funsigned_bitfields)->getAsString(Args);
1979
Daniel Dunbar6358d682010-10-15 22:30:42 +00001980 // -fsigned-bitfields is default, and clang doesn't support -fno-for-scope.
1981 if (!Args.hasFlag(options::OPT_ffor_scope,
1982 options::OPT_fno_for_scope))
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001983 D.Diag(diag::err_drv_clang_unsupported)
Daniel Dunbar6358d682010-10-15 22:30:42 +00001984 << Args.getLastArg(options::OPT_fno_for_scope)->getAsString(Args);
1985
Jeffrey Yasskin460aa542010-06-08 04:56:20 +00001986 // -fcaret-diagnostics is default.
1987 if (!Args.hasFlag(options::OPT_fcaret_diagnostics,
1988 options::OPT_fno_caret_diagnostics, true))
1989 CmdArgs.push_back("-fno-caret-diagnostics");
1990
Daniel Dunbar8281bde2009-04-19 21:09:34 +00001991 // -fdiagnostics-fixit-info is default, only pass non-default.
Mike Stump11289f42009-09-09 15:08:12 +00001992 if (!Args.hasFlag(options::OPT_fdiagnostics_fixit_info,
Daniel Dunbar8281bde2009-04-19 21:09:34 +00001993 options::OPT_fno_diagnostics_fixit_info))
1994 CmdArgs.push_back("-fno-diagnostics-fixit-info");
Eric Christopher84fbdb42011-08-19 00:30:14 +00001995
Douglas Gregor46ce91a2011-04-15 22:04:17 +00001996 // Enable -fdiagnostics-show-name by default.
1997 if (Args.hasFlag(options::OPT_fdiagnostics_show_name,
1998 options::OPT_fno_diagnostics_show_name, false))
1999 CmdArgs.push_back("-fdiagnostics-show-name");
Daniel Dunbar8281bde2009-04-19 21:09:34 +00002000
Daniel Dunbar092f0cc2009-04-16 06:32:38 +00002001 // Enable -fdiagnostics-show-option by default.
Mike Stump11289f42009-09-09 15:08:12 +00002002 if (Args.hasFlag(options::OPT_fdiagnostics_show_option,
Daniel Dunbar092f0cc2009-04-16 06:32:38 +00002003 options::OPT_fno_diagnostics_show_option))
2004 CmdArgs.push_back("-fdiagnostics-show-option");
Daniel Dunbar5ec95022009-11-04 06:24:57 +00002005
Chris Lattnerbf6fac82010-05-04 21:55:25 +00002006 if (const Arg *A =
2007 Args.getLastArg(options::OPT_fdiagnostics_show_category_EQ)) {
2008 CmdArgs.push_back("-fdiagnostics-show-category");
2009 CmdArgs.push_back(A->getValue(Args));
2010 }
Daniel Dunbarfe6c97b2010-08-24 16:47:49 +00002011
Douglas Gregor643c9222011-05-21 17:07:29 +00002012 if (const Arg *A =
2013 Args.getLastArg(options::OPT_fdiagnostics_format_EQ)) {
2014 CmdArgs.push_back("-fdiagnostics-format");
2015 CmdArgs.push_back(A->getValue(Args));
2016 }
2017
Chandler Carruthb6766f02011-03-27 01:50:55 +00002018 if (Arg *A = Args.getLastArg(
2019 options::OPT_fdiagnostics_show_note_include_stack,
2020 options::OPT_fno_diagnostics_show_note_include_stack)) {
2021 if (A->getOption().matches(
2022 options::OPT_fdiagnostics_show_note_include_stack))
2023 CmdArgs.push_back("-fdiagnostics-show-note-include-stack");
2024 else
2025 CmdArgs.push_back("-fno-diagnostics-show-note-include-stack");
2026 }
2027
Daniel Dunbar5ec95022009-11-04 06:24:57 +00002028 // Color diagnostics are the default, unless the terminal doesn't support
2029 // them.
2030 if (Args.hasFlag(options::OPT_fcolor_diagnostics,
Argyrios Kyrtzidis4f920162010-09-23 12:56:06 +00002031 options::OPT_fno_color_diagnostics,
2032 llvm::sys::Process::StandardErrHasColors()))
Daniel Dunbar5ec95022009-11-04 06:24:57 +00002033 CmdArgs.push_back("-fcolor-diagnostics");
2034
Daniel Dunbardb097022009-06-08 21:13:54 +00002035 if (!Args.hasFlag(options::OPT_fshow_source_location,
2036 options::OPT_fno_show_source_location))
2037 CmdArgs.push_back("-fno-show-source-location");
Daniel Dunbar092f0cc2009-04-16 06:32:38 +00002038
Douglas Gregor643c9222011-05-21 17:07:29 +00002039 if (!Args.hasFlag(options::OPT_fshow_column,
2040 options::OPT_fno_show_column,
2041 true))
2042 CmdArgs.push_back("-fno-show-column");
2043
Douglas Gregor8ed0c0b2010-07-09 17:35:33 +00002044 if (!Args.hasFlag(options::OPT_fspell_checking,
2045 options::OPT_fno_spell_checking))
2046 CmdArgs.push_back("-fno-spell-checking");
Daniel Dunbarfe6c97b2010-08-24 16:47:49 +00002047
Daniel Dunbar473f8a62010-10-18 22:49:46 +00002048
Daniel Dunbar3ada2b72010-11-02 19:42:04 +00002049 // Silently ignore -fasm-blocks for now.
2050 (void) Args.hasFlag(options::OPT_fasm_blocks, options::OPT_fno_asm_blocks,
2051 false);
Daniel Dunbar473f8a62010-10-18 22:49:46 +00002052
Jeffrey Yasskin2b99c6f2010-06-11 05:57:47 +00002053 if (Arg *A = Args.getLastArg(options::OPT_fshow_overloads_EQ))
2054 A->render(Args, CmdArgs);
2055
Daniel Dunbarf5e9b1f2009-04-19 21:20:32 +00002056 // -fdollars-in-identifiers default varies depending on platform and
2057 // language; only pass if specified.
Mike Stump11289f42009-09-09 15:08:12 +00002058 if (Arg *A = Args.getLastArg(options::OPT_fdollars_in_identifiers,
Daniel Dunbarf5e9b1f2009-04-19 21:20:32 +00002059 options::OPT_fno_dollars_in_identifiers)) {
2060 if (A->getOption().matches(options::OPT_fdollars_in_identifiers))
Daniel Dunbar15cef0e2009-12-16 20:10:18 +00002061 CmdArgs.push_back("-fdollars-in-identifiers");
Daniel Dunbarf5e9b1f2009-04-19 21:20:32 +00002062 else
Daniel Dunbar15cef0e2009-12-16 20:10:18 +00002063 CmdArgs.push_back("-fno-dollars-in-identifiers");
Daniel Dunbarf5e9b1f2009-04-19 21:20:32 +00002064 }
2065
Daniel Dunbaradeeb052009-05-22 19:02:20 +00002066 // -funit-at-a-time is default, and we don't support -fno-unit-at-a-time for
2067 // practical purposes.
Mike Stump11289f42009-09-09 15:08:12 +00002068 if (Arg *A = Args.getLastArg(options::OPT_funit_at_a_time,
Daniel Dunbaradeeb052009-05-22 19:02:20 +00002069 options::OPT_fno_unit_at_a_time)) {
2070 if (A->getOption().matches(options::OPT_fno_unit_at_a_time))
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002071 D.Diag(diag::warn_drv_clang_unsupported) << A->getAsString(Args);
Daniel Dunbaradeeb052009-05-22 19:02:20 +00002072 }
Eli Friedmanbb0d9a52009-07-14 21:58:17 +00002073
Daniel Dunbar2ffe0292009-09-10 03:37:02 +00002074 // Default to -fno-builtin-str{cat,cpy} on Darwin for ARM.
Daniel Dunbar4fa08112009-09-10 04:57:27 +00002075 //
Daniel Dunbar6c536aa2009-12-11 23:00:49 +00002076 // FIXME: This is disabled until clang -cc1 supports -fno-builtin-foo. PR4941.
Daniel Dunbar4fa08112009-09-10 04:57:27 +00002077#if 0
Daniel Dunbar2ffe0292009-09-10 03:37:02 +00002078 if (getToolChain().getTriple().getOS() == llvm::Triple::Darwin &&
2079 (getToolChain().getTriple().getArch() == llvm::Triple::arm ||
2080 getToolChain().getTriple().getArch() == llvm::Triple::thumb)) {
2081 if (!Args.hasArg(options::OPT_fbuiltin_strcat))
2082 CmdArgs.push_back("-fno-builtin-strcat");
2083 if (!Args.hasArg(options::OPT_fbuiltin_strcpy))
2084 CmdArgs.push_back("-fno-builtin-strcpy");
2085 }
Daniel Dunbar4fa08112009-09-10 04:57:27 +00002086#endif
Daniel Dunbar2ffe0292009-09-10 03:37:02 +00002087
Daniel Dunbar8c3d7352011-03-18 21:23:40 +00002088 // Only allow -traditional or -traditional-cpp outside in preprocessing modes.
Mike Stump11289f42009-09-09 15:08:12 +00002089 if (Arg *A = Args.getLastArg(options::OPT_traditional,
Daniel Dunbar8c3d7352011-03-18 21:23:40 +00002090 options::OPT_traditional_cpp)) {
2091 if (isa<PreprocessJobAction>(JA))
2092 CmdArgs.push_back("-traditional-cpp");
Eric Christopher84fbdb42011-08-19 00:30:14 +00002093 else
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002094 D.Diag(diag::err_drv_clang_unsupported) << A->getAsString(Args);
Daniel Dunbar8c3d7352011-03-18 21:23:40 +00002095 }
Eli Friedmanbb0d9a52009-07-14 21:58:17 +00002096
Daniel Dunbara2aedc62009-03-18 10:01:51 +00002097 Args.AddLastArg(CmdArgs, options::OPT_dM);
Chris Lattnercac63f32009-04-12 01:56:53 +00002098 Args.AddLastArg(CmdArgs, options::OPT_dD);
Daniel Dunbara2aedc62009-03-18 10:01:51 +00002099
Daniel Dunbar76fa8402010-04-15 06:09:03 +00002100 // Forward -Xclang arguments to -cc1, and -mllvm arguments to the LLVM option
2101 // parser.
Daniel Dunbara2aedc62009-03-18 10:01:51 +00002102 Args.AddAllArgValues(CmdArgs, options::OPT_Xclang);
Daniel Dunbar76fa8402010-04-15 06:09:03 +00002103 for (arg_iterator it = Args.filtered_begin(options::OPT_mllvm),
2104 ie = Args.filtered_end(); it != ie; ++it) {
Daniel Dunbara442fd52010-06-11 22:00:13 +00002105 (*it)->claim();
Daniel Dunbar88534f42010-04-17 06:10:00 +00002106
Daniel Dunbar76fa8402010-04-15 06:09:03 +00002107 // We translate this by hand to the -cc1 argument, since nightly test uses
2108 // it and developers have been trained to spell it with -mllvm.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002109 if (StringRef((*it)->getValue(Args, 0)) == "-disable-llvm-optzns")
Daniel Dunbar76fa8402010-04-15 06:09:03 +00002110 CmdArgs.push_back("-disable-llvm-optzns");
2111 else
Daniel Dunbara442fd52010-06-11 22:00:13 +00002112 (*it)->render(Args, CmdArgs);
Daniel Dunbar76fa8402010-04-15 06:09:03 +00002113 }
Daniel Dunbara2aedc62009-03-18 10:01:51 +00002114
Daniel Dunbard67a3222009-03-30 06:36:42 +00002115 if (Output.getType() == types::TY_Dependencies) {
2116 // Handled with other dependency code.
Daniel Dunbar5cdf3e02009-03-19 07:29:38 +00002117 } else if (Output.isFilename()) {
Daniel Dunbara3246a02009-03-18 08:07:30 +00002118 CmdArgs.push_back("-o");
Daniel Dunbar5cdf3e02009-03-19 07:29:38 +00002119 CmdArgs.push_back(Output.getFilename());
2120 } else {
2121 assert(Output.isNothing() && "Invalid output.");
Daniel Dunbara3246a02009-03-18 08:07:30 +00002122 }
2123
Daniel Dunbara2aedc62009-03-18 10:01:51 +00002124 for (InputInfoList::const_iterator
2125 it = Inputs.begin(), ie = Inputs.end(); it != ie; ++it) {
2126 const InputInfo &II = *it;
2127 CmdArgs.push_back("-x");
2128 CmdArgs.push_back(types::getTypeName(II.getType()));
Daniel Dunbarb440f562010-08-02 02:38:21 +00002129 if (II.isFilename())
Daniel Dunbar5cdf3e02009-03-19 07:29:38 +00002130 CmdArgs.push_back(II.getFilename());
Daniel Dunbara2aedc62009-03-18 10:01:51 +00002131 else
Daniel Dunbar5cdf3e02009-03-19 07:29:38 +00002132 II.getInputArg().renderAsInput(Args, CmdArgs);
Daniel Dunbara2aedc62009-03-18 10:01:51 +00002133 }
Daniel Dunbard067f7f2009-04-08 23:54:23 +00002134
Chris Lattnere9d7d782009-11-03 19:50:27 +00002135 Args.AddAllArgs(CmdArgs, options::OPT_undef);
2136
Daniel Dunbarb31b76f2010-07-18 21:16:15 +00002137 const char *Exec = getToolChain().getDriver().getClangProgramPath();
Daniel Dunbar24c7f5e2009-12-18 02:43:17 +00002138
2139 // Optionally embed the -cc1 level arguments into the debug info, for build
2140 // analysis.
2141 if (getToolChain().UseDwarfDebugFlags()) {
Daniel Dunbar7f3d9502010-06-04 18:47:06 +00002142 ArgStringList OriginalArgs;
2143 for (ArgList::const_iterator it = Args.begin(),
2144 ie = Args.end(); it != ie; ++it)
2145 (*it)->render(Args, OriginalArgs);
Daniel Dunbarfe6c97b2010-08-24 16:47:49 +00002146
Daniel Dunbar24c7f5e2009-12-18 02:43:17 +00002147 llvm::SmallString<256> Flags;
2148 Flags += Exec;
Daniel Dunbar7f3d9502010-06-04 18:47:06 +00002149 for (unsigned i = 0, e = OriginalArgs.size(); i != e; ++i) {
Daniel Dunbar24c7f5e2009-12-18 02:43:17 +00002150 Flags += " ";
Daniel Dunbar7f3d9502010-06-04 18:47:06 +00002151 Flags += OriginalArgs[i];
Daniel Dunbar24c7f5e2009-12-18 02:43:17 +00002152 }
2153 CmdArgs.push_back("-dwarf-debug-flags");
2154 CmdArgs.push_back(Args.MakeArgString(Flags.str()));
2155 }
2156
Daniel Dunbar7fbaf532010-08-02 02:38:28 +00002157 C.addCommand(new Command(JA, *this, Exec, CmdArgs));
Daniel Dunbar17731772009-03-23 19:03:36 +00002158
Roman Divacky178e01602011-02-10 16:52:03 +00002159 if (Arg *A = Args.getLastArg(options::OPT_pg))
2160 if (Args.hasArg(options::OPT_fomit_frame_pointer))
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002161 D.Diag(diag::err_drv_argument_not_allowed_with)
Roman Divacky178e01602011-02-10 16:52:03 +00002162 << "-fomit-frame-pointer" << A->getAsString(Args);
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00002163
Daniel Dunbarc2a71892009-04-03 20:51:31 +00002164 // Claim some arguments which clang supports automatically.
2165
Daniel Dunbar3e0cac62010-04-15 06:18:42 +00002166 // -fpch-preprocess is used with gcc to add a special marker in the output to
2167 // include the PCH file. Clang's PTH solution is completely transparent, so we
2168 // do not need to deal with it at all.
Daniel Dunbarc2a71892009-04-03 20:51:31 +00002169 Args.ClaimAllArgs(options::OPT_fpch_preprocess);
Daniel Dunbard067f7f2009-04-08 23:54:23 +00002170
Daniel Dunbar17731772009-03-23 19:03:36 +00002171 // Claim some arguments which clang doesn't support, but we don't
2172 // care to warn the user about.
Daniel Dunbar44b36ee2009-11-25 11:53:23 +00002173 Args.ClaimAllArgs(options::OPT_clang_ignored_f_Group);
2174 Args.ClaimAllArgs(options::OPT_clang_ignored_m_Group);
Rafael Espindola22f603032011-02-28 23:29:45 +00002175
Rafael Espindolad95a8122011-03-01 05:25:27 +00002176 // Disable warnings for clang -E -use-gold-plugin -emit-llvm foo.c
Rafael Espindola22f603032011-02-28 23:29:45 +00002177 Args.ClaimAllArgs(options::OPT_use_gold_plugin);
Rafael Espindolad95a8122011-03-01 05:25:27 +00002178 Args.ClaimAllArgs(options::OPT_emit_llvm);
Daniel Dunbar1a093d22009-03-18 06:00:36 +00002179}
2180
Daniel Dunbar4f5e79c2010-05-20 21:30:13 +00002181void ClangAs::ConstructJob(Compilation &C, const JobAction &JA,
Daniel Dunbar4f5e79c2010-05-20 21:30:13 +00002182 const InputInfo &Output,
2183 const InputInfoList &Inputs,
2184 const ArgList &Args,
2185 const char *LinkingOutput) const {
Daniel Dunbar4f5e79c2010-05-20 21:30:13 +00002186 ArgStringList CmdArgs;
2187
2188 assert(Inputs.size() == 1 && "Unexpected number of inputs.");
2189 const InputInfo &Input = Inputs[0];
2190
Rafael Espindolacfaadda2010-11-17 22:13:25 +00002191 // Don't warn about "clang -w -c foo.s"
2192 Args.ClaimAllArgs(options::OPT_w);
Rafael Espindolad95a8122011-03-01 05:25:27 +00002193 // and "clang -emit-llvm -c foo.s"
2194 Args.ClaimAllArgs(options::OPT_emit_llvm);
2195 // and "clang -use-gold-plugin -c foo.s"
2196 Args.ClaimAllArgs(options::OPT_use_gold_plugin);
Rafael Espindolacfaadda2010-11-17 22:13:25 +00002197
Daniel Dunbar4f5e79c2010-05-20 21:30:13 +00002198 // Invoke ourselves in -cc1as mode.
2199 //
2200 // FIXME: Implement custom jobs for internal actions.
2201 CmdArgs.push_back("-cc1as");
2202
2203 // Add the "effective" target triple.
2204 CmdArgs.push_back("-triple");
Daniel Dunbar82eb4ce2010-08-23 22:35:37 +00002205 std::string TripleStr = getToolChain().ComputeEffectiveClangTriple(Args);
Daniel Dunbar4f5e79c2010-05-20 21:30:13 +00002206 CmdArgs.push_back(Args.MakeArgString(TripleStr));
2207
2208 // Set the output mode, we currently only expect to be used as a real
2209 // assembler.
2210 CmdArgs.push_back("-filetype");
2211 CmdArgs.push_back("obj");
2212
Joerg Sonnenbergeref317a22011-05-06 14:35:16 +00002213 if (UseRelaxAll(C, Args))
Daniel Dunbar99ca8b72010-05-28 16:43:21 +00002214 CmdArgs.push_back("-relax-all");
Daniel Dunbar06e2cc32010-05-27 06:18:05 +00002215
Daniel Dunbar1d733e22011-03-17 17:37:29 +00002216 // Ignore explicit -force_cpusubtype_ALL option.
2217 (void) Args.hasArg(options::OPT_force__cpusubtype__ALL);
Daniel Dunbar4f5e79c2010-05-20 21:30:13 +00002218
2219 // FIXME: Add -g support, once we have it.
2220
2221 // FIXME: Add -static support, once we have it.
2222
2223 Args.AddAllArgValues(CmdArgs, options::OPT_Wa_COMMA,
2224 options::OPT_Xassembler);
Daniel Dunbar252e8f92011-04-29 17:53:18 +00002225 Args.AddAllArgs(CmdArgs, options::OPT_mllvm);
Daniel Dunbar4f5e79c2010-05-20 21:30:13 +00002226
2227 assert(Output.isFilename() && "Unexpected lipo output.");
2228 CmdArgs.push_back("-o");
2229 CmdArgs.push_back(Output.getFilename());
2230
Daniel Dunbarb440f562010-08-02 02:38:21 +00002231 assert(Input.isFilename() && "Invalid input.");
2232 CmdArgs.push_back(Input.getFilename());
Daniel Dunbar4f5e79c2010-05-20 21:30:13 +00002233
Daniel Dunbarb31b76f2010-07-18 21:16:15 +00002234 const char *Exec = getToolChain().getDriver().getClangProgramPath();
Daniel Dunbar7fbaf532010-08-02 02:38:28 +00002235 C.addCommand(new Command(JA, *this, Exec, CmdArgs));
Daniel Dunbar4f5e79c2010-05-20 21:30:13 +00002236}
2237
Daniel Dunbara3246a02009-03-18 08:07:30 +00002238void gcc::Common::ConstructJob(Compilation &C, const JobAction &JA,
Daniel Dunbara3246a02009-03-18 08:07:30 +00002239 const InputInfo &Output,
2240 const InputInfoList &Inputs,
Daniel Dunbara2aedc62009-03-18 10:01:51 +00002241 const ArgList &Args,
Daniel Dunbara3246a02009-03-18 08:07:30 +00002242 const char *LinkingOutput) const {
Daniel Dunbar083edf72009-12-21 18:54:17 +00002243 const Driver &D = getToolChain().getDriver();
Daniel Dunbara3246a02009-03-18 08:07:30 +00002244 ArgStringList CmdArgs;
Daniel Dunbar1a093d22009-03-18 06:00:36 +00002245
Daniel Dunbard067f7f2009-04-08 23:54:23 +00002246 for (ArgList::const_iterator
Daniel Dunbara2aedc62009-03-18 10:01:51 +00002247 it = Args.begin(), ie = Args.end(); it != ie; ++it) {
Daniel Dunbara3246a02009-03-18 08:07:30 +00002248 Arg *A = *it;
Daniel Dunbar2da02722009-03-19 07:55:12 +00002249 if (A->getOption().hasForwardToGCC()) {
Daniel Dunbar939c1212010-08-03 16:14:14 +00002250 // Don't forward any -g arguments to assembly steps.
2251 if (isa<AssembleJobAction>(JA) &&
2252 A->getOption().matches(options::OPT_g_Group))
2253 continue;
2254
Daniel Dunbar2da02722009-03-19 07:55:12 +00002255 // It is unfortunate that we have to claim here, as this means
2256 // we will basically never report anything interesting for
Daniel Dunbar5716d872009-05-02 21:41:52 +00002257 // platforms using a generic gcc, even if we are just using gcc
2258 // to get to the assembler.
Daniel Dunbar2da02722009-03-19 07:55:12 +00002259 A->claim();
Daniel Dunbara2aedc62009-03-18 10:01:51 +00002260 A->render(Args, CmdArgs);
Daniel Dunbar2da02722009-03-19 07:55:12 +00002261 }
Daniel Dunbara3246a02009-03-18 08:07:30 +00002262 }
Daniel Dunbard067f7f2009-04-08 23:54:23 +00002263
Daniel Dunbar4e295052010-01-25 22:35:08 +00002264 RenderExtraToolArgs(JA, CmdArgs);
Daniel Dunbara3246a02009-03-18 08:07:30 +00002265
2266 // If using a driver driver, force the arch.
Daniel Dunbar5bbebfe2009-05-22 02:21:04 +00002267 const std::string &Arch = getToolChain().getArchName();
Daniel Dunbarf4894fe2009-12-23 00:46:38 +00002268 if (getToolChain().getTriple().getOS() == llvm::Triple::Darwin) {
Daniel Dunbara3246a02009-03-18 08:07:30 +00002269 CmdArgs.push_back("-arch");
Daniel Dunbar0a05d932009-04-01 20:33:11 +00002270
2271 // FIXME: Remove these special cases.
Daniel Dunbar5bbebfe2009-05-22 02:21:04 +00002272 if (Arch == "powerpc")
2273 CmdArgs.push_back("ppc");
2274 else if (Arch == "powerpc64")
2275 CmdArgs.push_back("ppc64");
2276 else
Daniel Dunbar1c28f1e2009-09-09 22:32:48 +00002277 CmdArgs.push_back(Args.MakeArgString(Arch));
Daniel Dunbara3246a02009-03-18 08:07:30 +00002278 }
2279
Daniel Dunbar5716d872009-05-02 21:41:52 +00002280 // Try to force gcc to match the tool chain we want, if we recognize
2281 // the arch.
Daniel Dunbar5bbebfe2009-05-22 02:21:04 +00002282 //
2283 // FIXME: The triple class should directly provide the information we want
2284 // here.
2285 if (Arch == "i386" || Arch == "powerpc")
Daniel Dunbar5716d872009-05-02 21:41:52 +00002286 CmdArgs.push_back("-m32");
Daniel Dunbar5bbebfe2009-05-22 02:21:04 +00002287 else if (Arch == "x86_64" || Arch == "powerpc64")
Daniel Dunbar5716d872009-05-02 21:41:52 +00002288 CmdArgs.push_back("-m64");
2289
Daniel Dunbarb440f562010-08-02 02:38:21 +00002290 if (Output.isFilename()) {
Daniel Dunbara3246a02009-03-18 08:07:30 +00002291 CmdArgs.push_back("-o");
Daniel Dunbar5cdf3e02009-03-19 07:29:38 +00002292 CmdArgs.push_back(Output.getFilename());
2293 } else {
2294 assert(Output.isNothing() && "Unexpected output");
Daniel Dunbara3246a02009-03-18 08:07:30 +00002295 CmdArgs.push_back("-fsyntax-only");
Daniel Dunbar5cdf3e02009-03-19 07:29:38 +00002296 }
Daniel Dunbara3246a02009-03-18 08:07:30 +00002297
2298
2299 // Only pass -x if gcc will understand it; otherwise hope gcc
2300 // understands the suffix correctly. The main use case this would go
2301 // wrong in is for linker inputs if they happened to have an odd
2302 // suffix; really the only way to get this to happen is a command
2303 // like '-x foobar a.c' which will treat a.c like a linker input.
2304 //
2305 // FIXME: For the linker case specifically, can we safely convert
2306 // inputs into '-Wl,' options?
2307 for (InputInfoList::const_iterator
2308 it = Inputs.begin(), ie = Inputs.end(); it != ie; ++it) {
2309 const InputInfo &II = *it;
Daniel Dunbare3e263f2009-05-02 20:14:53 +00002310
Daniel Dunbar6cdf83c2009-09-01 16:57:46 +00002311 // Don't try to pass LLVM or AST inputs to a generic gcc.
Daniel Dunbar24e52992010-06-07 23:28:45 +00002312 if (II.getType() == types::TY_LLVM_IR || II.getType() == types::TY_LTO_IR ||
2313 II.getType() == types::TY_LLVM_BC || II.getType() == types::TY_LTO_BC)
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002314 D.Diag(diag::err_drv_no_linker_llvm_support)
Daniel Dunbar1c28f1e2009-09-09 22:32:48 +00002315 << getToolChain().getTripleString();
Daniel Dunbar6cdf83c2009-09-01 16:57:46 +00002316 else if (II.getType() == types::TY_AST)
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002317 D.Diag(diag::err_drv_no_ast_support)
Daniel Dunbar1c28f1e2009-09-09 22:32:48 +00002318 << getToolChain().getTripleString();
Daniel Dunbare3e263f2009-05-02 20:14:53 +00002319
Daniel Dunbara3246a02009-03-18 08:07:30 +00002320 if (types::canTypeBeUserSpecified(II.getType())) {
2321 CmdArgs.push_back("-x");
2322 CmdArgs.push_back(types::getTypeName(II.getType()));
2323 }
2324
Daniel Dunbarb440f562010-08-02 02:38:21 +00002325 if (II.isFilename())
Daniel Dunbar5cdf3e02009-03-19 07:29:38 +00002326 CmdArgs.push_back(II.getFilename());
Daniel Dunbarf2476752010-09-25 18:10:05 +00002327 else {
2328 const Arg &A = II.getInputArg();
2329
2330 // Reverse translate some rewritten options.
2331 if (A.getOption().matches(options::OPT_Z_reserved_lib_stdcxx)) {
2332 CmdArgs.push_back("-lstdc++");
2333 continue;
2334 }
2335
Daniel Dunbar5cdf3e02009-03-19 07:29:38 +00002336 // Don't render as input, we need gcc to do the translations.
Daniel Dunbarf2476752010-09-25 18:10:05 +00002337 A.render(Args, CmdArgs);
2338 }
Daniel Dunbara3246a02009-03-18 08:07:30 +00002339 }
2340
Dylan Noblesmith70e73a32011-04-09 13:31:59 +00002341 const std::string customGCCName = D.getCCCGenericGCCName();
2342 const char *GCCName;
2343 if (!customGCCName.empty())
2344 GCCName = customGCCName.c_str();
2345 else if (D.CCCIsCXX) {
2346#ifdef IS_CYGWIN15
2347 // FIXME: Detect the version of Cygwin at runtime?
2348 GCCName = "g++-4";
2349#else
2350 GCCName = "g++";
2351#endif
2352 } else
2353 GCCName = "gcc";
2354
Daniel Dunbard067f7f2009-04-08 23:54:23 +00002355 const char *Exec =
Daniel Dunbar9c3ed5f2010-07-14 18:46:23 +00002356 Args.MakeArgString(getToolChain().GetProgramPath(GCCName));
Daniel Dunbar7fbaf532010-08-02 02:38:28 +00002357 C.addCommand(new Command(JA, *this, Exec, CmdArgs));
Daniel Dunbar1a093d22009-03-18 06:00:36 +00002358}
2359
Daniel Dunbar4e295052010-01-25 22:35:08 +00002360void gcc::Preprocess::RenderExtraToolArgs(const JobAction &JA,
2361 ArgStringList &CmdArgs) const {
Daniel Dunbara3246a02009-03-18 08:07:30 +00002362 CmdArgs.push_back("-E");
Daniel Dunbar1a093d22009-03-18 06:00:36 +00002363}
2364
Daniel Dunbar4e295052010-01-25 22:35:08 +00002365void gcc::Precompile::RenderExtraToolArgs(const JobAction &JA,
2366 ArgStringList &CmdArgs) const {
Daniel Dunbara3246a02009-03-18 08:07:30 +00002367 // The type is good enough.
Daniel Dunbar1a093d22009-03-18 06:00:36 +00002368}
2369
Daniel Dunbar4e295052010-01-25 22:35:08 +00002370void gcc::Compile::RenderExtraToolArgs(const JobAction &JA,
2371 ArgStringList &CmdArgs) const {
Daniel Dunbaraeea8ac2010-02-11 03:16:21 +00002372 const Driver &D = getToolChain().getDriver();
2373
Daniel Dunbar4e295052010-01-25 22:35:08 +00002374 // If -flto, etc. are present then make sure not to force assembly output.
Daniel Dunbar24e52992010-06-07 23:28:45 +00002375 if (JA.getType() == types::TY_LLVM_IR || JA.getType() == types::TY_LTO_IR ||
2376 JA.getType() == types::TY_LLVM_BC || JA.getType() == types::TY_LTO_BC)
Daniel Dunbar4e295052010-01-25 22:35:08 +00002377 CmdArgs.push_back("-c");
Daniel Dunbaraeea8ac2010-02-11 03:16:21 +00002378 else {
2379 if (JA.getType() != types::TY_PP_Asm)
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002380 D.Diag(diag::err_drv_invalid_gcc_output_type)
Daniel Dunbaraeea8ac2010-02-11 03:16:21 +00002381 << getTypeName(JA.getType());
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00002382
Daniel Dunbar4e295052010-01-25 22:35:08 +00002383 CmdArgs.push_back("-S");
Daniel Dunbaraeea8ac2010-02-11 03:16:21 +00002384 }
Daniel Dunbar1a093d22009-03-18 06:00:36 +00002385}
2386
Daniel Dunbar4e295052010-01-25 22:35:08 +00002387void gcc::Assemble::RenderExtraToolArgs(const JobAction &JA,
2388 ArgStringList &CmdArgs) const {
Daniel Dunbara3246a02009-03-18 08:07:30 +00002389 CmdArgs.push_back("-c");
Daniel Dunbar1a093d22009-03-18 06:00:36 +00002390}
Daniel Dunbara3246a02009-03-18 08:07:30 +00002391
Daniel Dunbar4e295052010-01-25 22:35:08 +00002392void gcc::Link::RenderExtraToolArgs(const JobAction &JA,
2393 ArgStringList &CmdArgs) const {
Daniel Dunbara3246a02009-03-18 08:07:30 +00002394 // The types are (hopefully) good enough.
2395}
2396
Daniel Dunbare6adeee2009-03-29 17:08:39 +00002397const char *darwin::CC1::getCC1Name(types::ID Type) const {
2398 switch (Type) {
2399 default:
2400 assert(0 && "Unexpected type for Darwin CC1 tool.");
2401 case types::TY_Asm:
2402 case types::TY_C: case types::TY_CHeader:
2403 case types::TY_PP_C: case types::TY_PP_CHeader:
2404 return "cc1";
2405 case types::TY_ObjC: case types::TY_ObjCHeader:
Nico Webered8080c2011-08-13 23:13:37 +00002406 case types::TY_PP_ObjC: case types::TY_PP_ObjC_Alias:
2407 case types::TY_PP_ObjCHeader:
Daniel Dunbare6adeee2009-03-29 17:08:39 +00002408 return "cc1obj";
2409 case types::TY_CXX: case types::TY_CXXHeader:
2410 case types::TY_PP_CXX: case types::TY_PP_CXXHeader:
2411 return "cc1plus";
2412 case types::TY_ObjCXX: case types::TY_ObjCXXHeader:
Nico Webered8080c2011-08-13 23:13:37 +00002413 case types::TY_PP_ObjCXX: case types::TY_PP_ObjCXX_Alias:
2414 case types::TY_PP_ObjCXXHeader:
Daniel Dunbare6adeee2009-03-29 17:08:39 +00002415 return "cc1objplus";
2416 }
2417}
2418
Daniel Dunbard067f7f2009-04-08 23:54:23 +00002419const char *darwin::CC1::getBaseInputName(const ArgList &Args,
Daniel Dunbar52e96cc2009-03-30 00:34:04 +00002420 const InputInfoList &Inputs) {
Michael J. Spencere1696752010-12-18 00:19:12 +00002421 return Args.MakeArgString(
2422 llvm::sys::path::filename(Inputs[0].getBaseInput()));
Daniel Dunbarafec1f52009-03-29 18:40:18 +00002423}
2424
Daniel Dunbard067f7f2009-04-08 23:54:23 +00002425const char *darwin::CC1::getBaseInputStem(const ArgList &Args,
Daniel Dunbar52e96cc2009-03-30 00:34:04 +00002426 const InputInfoList &Inputs) {
Daniel Dunbarafec1f52009-03-29 18:40:18 +00002427 const char *Str = getBaseInputName(Args, Inputs);
2428
Chris Lattner906bb902011-01-16 08:14:11 +00002429 if (const char *End = strrchr(Str, '.'))
Daniel Dunbar1c28f1e2009-09-09 22:32:48 +00002430 return Args.MakeArgString(std::string(Str, End));
Daniel Dunbarafec1f52009-03-29 18:40:18 +00002431
2432 return Str;
2433}
2434
2435const char *
Daniel Dunbard067f7f2009-04-08 23:54:23 +00002436darwin::CC1::getDependencyFileName(const ArgList &Args,
Daniel Dunbar52e96cc2009-03-30 00:34:04 +00002437 const InputInfoList &Inputs) {
Daniel Dunbarafec1f52009-03-29 18:40:18 +00002438 // FIXME: Think about this more.
2439 std::string Res;
2440
2441 if (Arg *OutputOpt = Args.getLastArg(options::OPT_o)) {
2442 std::string Str(OutputOpt->getValue(Args));
Daniel Dunbarafec1f52009-03-29 18:40:18 +00002443 Res = Str.substr(0, Str.rfind('.'));
Chad Rosier6fdf38b2011-08-17 23:08:45 +00002444 } else {
Daniel Dunbar52e96cc2009-03-30 00:34:04 +00002445 Res = darwin::CC1::getBaseInputStem(Args, Inputs);
Chad Rosier6fdf38b2011-08-17 23:08:45 +00002446 }
Daniel Dunbar1c28f1e2009-09-09 22:32:48 +00002447 return Args.MakeArgString(Res + ".d");
Daniel Dunbarafec1f52009-03-29 18:40:18 +00002448}
2449
Chad Rosierbc5ea3d2011-08-17 18:24:55 +00002450void darwin::CC1::RemoveCC1UnsupportedArgs(ArgStringList &CmdArgs) const {
Eric Christopher84fbdb42011-08-19 00:30:14 +00002451 for (ArgStringList::iterator it = CmdArgs.begin(), ie = CmdArgs.end();
Chad Rosierbc5ea3d2011-08-17 18:24:55 +00002452 it != ie;) {
Chad Rosier2f818792011-08-18 17:56:32 +00002453
2454 StringRef Option = *it;
Chad Rosier30453862011-08-18 01:18:28 +00002455
2456 // We only remove warning options.
Chad Rosier2f818792011-08-18 17:56:32 +00002457 if (!Option.startswith("-W")) {
Chad Rosier30453862011-08-18 01:18:28 +00002458 ++it;
2459 continue;
2460 }
2461
Chad Rosier2f818792011-08-18 17:56:32 +00002462 if (Option.startswith("-Wno-"))
2463 Option = Option.substr(5);
Chad Rosier30453862011-08-18 01:18:28 +00002464 else
Chad Rosier2f818792011-08-18 17:56:32 +00002465 Option = Option.substr(2);
Chad Rosier30453862011-08-18 01:18:28 +00002466
2467 bool RemoveOption = llvm::StringSwitch<bool>(Option)
2468 .Case("address-of-temporary", true)
2469 .Case("ambiguous-member-template", true)
2470 .Case("analyzer-incompatible-plugin", true)
2471 .Case("array-bounds", true)
2472 .Case("array-bounds-pointer-arithmetic", true)
2473 .Case("bind-to-temporary-copy", true)
2474 .Case("bitwise-op-parentheses", true)
2475 .Case("bool-conversions", true)
2476 .Case("builtin-macro-redefined", true)
2477 .Case("c++-hex-floats", true)
2478 .Case("c++0x-compat", true)
2479 .Case("c++0x-extensions", true)
2480 .Case("c++0x-narrowing", true)
2481 .Case("c++0x-static-nonintegral-init", true)
2482 .Case("conditional-uninitialized", true)
2483 .Case("constant-conversion", true)
2484 .Case("CFString-literal", true)
2485 .Case("constant-logical-operand", true)
2486 .Case("custom-atomic-properties", true)
2487 .Case("default-arg-special-member", true)
2488 .Case("delegating-ctor-cycles", true)
2489 .Case("delete-non-virtual-dtor", true)
2490 .Case("deprecated-implementations", true)
2491 .Case("deprecated-writable-strings", true)
2492 .Case("distributed-object-modifiers", true)
2493 .Case("duplicate-method-arg", true)
2494 .Case("dynamic-class-memaccess", true)
2495 .Case("enum-compare", true)
2496 .Case("exit-time-destructors", true)
2497 .Case("gnu", true)
2498 .Case("gnu-designator", true)
2499 .Case("header-hygiene", true)
2500 .Case("idiomatic-parentheses", true)
2501 .Case("ignored-qualifiers", true)
2502 .Case("implicit-atomic-properties", true)
2503 .Case("incompatible-pointer-types", true)
2504 .Case("incomplete-implementation", true)
2505 .Case("initializer-overrides", true)
2506 .Case("invalid-noreturn", true)
2507 .Case("invalid-token-paste", true)
2508 .Case("literal-conversion", true)
2509 .Case("literal-range", true)
2510 .Case("local-type-template-args", true)
2511 .Case("logical-op-parentheses", true)
2512 .Case("method-signatures", true)
2513 .Case("microsoft", true)
2514 .Case("mismatched-tags", true)
2515 .Case("missing-method-return-type", true)
2516 .Case("non-pod-varargs", true)
2517 .Case("nonfragile-abi2", true)
2518 .Case("null-arithmetic", true)
2519 .Case("null-dereference", true)
2520 .Case("out-of-line-declaration", true)
2521 .Case("overriding-method-mismatch", true)
2522 .Case("readonly-setter-attrs", true)
2523 .Case("return-stack-address", true)
2524 .Case("self-assign", true)
2525 .Case("semicolon-before-method-body", true)
2526 .Case("sentinel", true)
2527 .Case("shift-overflow", true)
2528 .Case("shift-sign-overflow", true)
2529 .Case("sign-conversion", true)
2530 .Case("sizeof-array-argument", true)
2531 .Case("sizeof-pointer-memaccess", true)
2532 .Case("string-compare", true)
2533 .Case("super-class-method-mismatch", true)
2534 .Case("tautological-compare", true)
2535 .Case("typedef-redefinition", true)
2536 .Case("typename-missing", true)
2537 .Case("undefined-reinterpret-cast", true)
2538 .Case("unknown-warning-option", true)
2539 .Case("unnamed-type-template-args", true)
2540 .Case("unneeded-internal-declaration", true)
2541 .Case("unneeded-member-function", true)
2542 .Case("unused-comparison", true)
2543 .Case("unused-exception-parameter", true)
2544 .Case("unused-member-function", true)
2545 .Case("unused-result", true)
2546 .Case("vector-conversions", true)
2547 .Case("vla", true)
2548 .Case("used-but-marked-unused", true)
2549 .Case("weak-vtables", true)
2550 .Default(false);
2551
2552 if (RemoveOption) {
Chad Rosierbc5ea3d2011-08-17 18:24:55 +00002553 it = CmdArgs.erase(it);
Chad Rosier23594f62011-08-17 18:51:56 +00002554 ie = CmdArgs.end();
Chad Rosier6fdf38b2011-08-17 23:08:45 +00002555 } else {
Chad Rosierbc5ea3d2011-08-17 18:24:55 +00002556 ++it;
Chad Rosier6fdf38b2011-08-17 23:08:45 +00002557 }
Chad Rosierbc5ea3d2011-08-17 18:24:55 +00002558 }
2559}
2560
Daniel Dunbard067f7f2009-04-08 23:54:23 +00002561void darwin::CC1::AddCC1Args(const ArgList &Args,
Daniel Dunbarafec1f52009-03-29 18:40:18 +00002562 ArgStringList &CmdArgs) const {
Daniel Dunbar083edf72009-12-21 18:54:17 +00002563 const Driver &D = getToolChain().getDriver();
Daniel Dunbar4eadb602009-09-10 01:21:12 +00002564
2565 CheckCodeGenerationOptions(D, Args);
2566
Daniel Dunbarafec1f52009-03-29 18:40:18 +00002567 // Derived from cc1 spec.
Daniel Dunbarafec1f52009-03-29 18:40:18 +00002568 if (!Args.hasArg(options::OPT_mkernel) && !Args.hasArg(options::OPT_static) &&
2569 !Args.hasArg(options::OPT_mdynamic_no_pic))
2570 CmdArgs.push_back("-fPIC");
2571
Daniel Dunbar2ffe0292009-09-10 03:37:02 +00002572 if (getToolChain().getTriple().getArch() == llvm::Triple::arm ||
2573 getToolChain().getTriple().getArch() == llvm::Triple::thumb) {
2574 if (!Args.hasArg(options::OPT_fbuiltin_strcat))
2575 CmdArgs.push_back("-fno-builtin-strcat");
2576 if (!Args.hasArg(options::OPT_fbuiltin_strcpy))
2577 CmdArgs.push_back("-fno-builtin-strcpy");
2578 }
2579
Daniel Dunbarafec1f52009-03-29 18:40:18 +00002580 if (Args.hasArg(options::OPT_g_Flag) &&
2581 !Args.hasArg(options::OPT_fno_eliminate_unused_debug_symbols))
2582 CmdArgs.push_back("-feliminate-unused-debug-symbols");
2583}
2584
2585void darwin::CC1::AddCC1OptionsArgs(const ArgList &Args, ArgStringList &CmdArgs,
2586 const InputInfoList &Inputs,
2587 const ArgStringList &OutputArgs) const {
Daniel Dunbar083edf72009-12-21 18:54:17 +00002588 const Driver &D = getToolChain().getDriver();
Daniel Dunbarafec1f52009-03-29 18:40:18 +00002589
2590 // Derived from cc1_options spec.
2591 if (Args.hasArg(options::OPT_fast) ||
2592 Args.hasArg(options::OPT_fastf) ||
2593 Args.hasArg(options::OPT_fastcp))
2594 CmdArgs.push_back("-O3");
Daniel Dunbard067f7f2009-04-08 23:54:23 +00002595
Daniel Dunbarafec1f52009-03-29 18:40:18 +00002596 if (Arg *A = Args.getLastArg(options::OPT_pg))
2597 if (Args.hasArg(options::OPT_fomit_frame_pointer))
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002598 D.Diag(diag::err_drv_argument_not_allowed_with)
Daniel Dunbarafec1f52009-03-29 18:40:18 +00002599 << A->getAsString(Args) << "-fomit-frame-pointer";
2600
2601 AddCC1Args(Args, CmdArgs);
2602
2603 if (!Args.hasArg(options::OPT_Q))
2604 CmdArgs.push_back("-quiet");
2605
2606 CmdArgs.push_back("-dumpbase");
Daniel Dunbar52e96cc2009-03-30 00:34:04 +00002607 CmdArgs.push_back(darwin::CC1::getBaseInputName(Args, Inputs));
Daniel Dunbarafec1f52009-03-29 18:40:18 +00002608
2609 Args.AddAllArgs(CmdArgs, options::OPT_d_Group);
2610
2611 Args.AddAllArgs(CmdArgs, options::OPT_m_Group);
2612 Args.AddAllArgs(CmdArgs, options::OPT_a_Group);
2613
2614 // FIXME: The goal is to use the user provided -o if that is our
2615 // final output, otherwise to drive from the original input
2616 // name. Find a clean way to go about this.
2617 if ((Args.hasArg(options::OPT_c) || Args.hasArg(options::OPT_S)) &&
2618 Args.hasArg(options::OPT_o)) {
2619 Arg *OutputOpt = Args.getLastArg(options::OPT_o);
2620 CmdArgs.push_back("-auxbase-strip");
2621 CmdArgs.push_back(OutputOpt->getValue(Args));
2622 } else {
2623 CmdArgs.push_back("-auxbase");
Daniel Dunbar52e96cc2009-03-30 00:34:04 +00002624 CmdArgs.push_back(darwin::CC1::getBaseInputStem(Args, Inputs));
Daniel Dunbarafec1f52009-03-29 18:40:18 +00002625 }
2626
2627 Args.AddAllArgs(CmdArgs, options::OPT_g_Group);
2628
2629 Args.AddAllArgs(CmdArgs, options::OPT_O);
2630 // FIXME: -Wall is getting some special treatment. Investigate.
2631 Args.AddAllArgs(CmdArgs, options::OPT_W_Group, options::OPT_pedantic_Group);
2632 Args.AddLastArg(CmdArgs, options::OPT_w);
Daniel Dunbard067f7f2009-04-08 23:54:23 +00002633 Args.AddAllArgs(CmdArgs, options::OPT_std_EQ, options::OPT_ansi,
Daniel Dunbarafec1f52009-03-29 18:40:18 +00002634 options::OPT_trigraphs);
Daniel Dunbar72a60902009-04-26 01:10:38 +00002635 if (!Args.getLastArg(options::OPT_std_EQ, options::OPT_ansi)) {
2636 // Honor -std-default.
2637 Args.AddAllArgsTranslated(CmdArgs, options::OPT_std_default_EQ,
2638 "-std=", /*Joined=*/true);
2639 }
2640
Daniel Dunbarafec1f52009-03-29 18:40:18 +00002641 if (Args.hasArg(options::OPT_v))
2642 CmdArgs.push_back("-version");
Daniel Dunbar733b0f82011-03-01 18:49:30 +00002643 if (Args.hasArg(options::OPT_pg) &&
2644 getToolChain().SupportsProfiling())
Daniel Dunbarafec1f52009-03-29 18:40:18 +00002645 CmdArgs.push_back("-p");
2646 Args.AddLastArg(CmdArgs, options::OPT_p);
Daniel Dunbard067f7f2009-04-08 23:54:23 +00002647
Daniel Dunbarafec1f52009-03-29 18:40:18 +00002648 // The driver treats -fsyntax-only specially.
Daniel Dunbar2ffe0292009-09-10 03:37:02 +00002649 if (getToolChain().getTriple().getArch() == llvm::Triple::arm ||
2650 getToolChain().getTriple().getArch() == llvm::Triple::thumb) {
2651 // Removes -fbuiltin-str{cat,cpy}; these aren't recognized by cc1 but are
2652 // used to inhibit the default -fno-builtin-str{cat,cpy}.
2653 //
2654 // FIXME: Should we grow a better way to deal with "removing" args?
Daniel Dunbar44b36ee2009-11-25 11:53:23 +00002655 for (arg_iterator it = Args.filtered_begin(options::OPT_f_Group,
2656 options::OPT_fsyntax_only),
2657 ie = Args.filtered_end(); it != ie; ++it) {
Daniel Dunbara442fd52010-06-11 22:00:13 +00002658 if (!(*it)->getOption().matches(options::OPT_fbuiltin_strcat) &&
2659 !(*it)->getOption().matches(options::OPT_fbuiltin_strcpy)) {
2660 (*it)->claim();
2661 (*it)->render(Args, CmdArgs);
Daniel Dunbar2ffe0292009-09-10 03:37:02 +00002662 }
2663 }
2664 } else
2665 Args.AddAllArgs(CmdArgs, options::OPT_f_Group, options::OPT_fsyntax_only);
Daniel Dunbard067f7f2009-04-08 23:54:23 +00002666
Daniel Dunbarf28c2ff2011-04-07 20:41:03 +00002667 // Claim Clang only -f options, they aren't worth warning about.
2668 Args.ClaimAllArgs(options::OPT_f_clang_Group);
2669
Daniel Dunbarafec1f52009-03-29 18:40:18 +00002670 Args.AddAllArgs(CmdArgs, options::OPT_undef);
2671 if (Args.hasArg(options::OPT_Qn))
2672 CmdArgs.push_back("-fno-ident");
Daniel Dunbard067f7f2009-04-08 23:54:23 +00002673
Daniel Dunbarafec1f52009-03-29 18:40:18 +00002674 // FIXME: This isn't correct.
2675 //Args.AddLastArg(CmdArgs, options::OPT__help)
2676 //Args.AddLastArg(CmdArgs, options::OPT__targetHelp)
2677
2678 CmdArgs.append(OutputArgs.begin(), OutputArgs.end());
2679
2680 // FIXME: Still don't get what is happening here. Investigate.
2681 Args.AddAllArgs(CmdArgs, options::OPT__param);
2682
2683 if (Args.hasArg(options::OPT_fmudflap) ||
2684 Args.hasArg(options::OPT_fmudflapth)) {
2685 CmdArgs.push_back("-fno-builtin");
2686 CmdArgs.push_back("-fno-merge-constants");
2687 }
Daniel Dunbard067f7f2009-04-08 23:54:23 +00002688
Daniel Dunbarafec1f52009-03-29 18:40:18 +00002689 if (Args.hasArg(options::OPT_coverage)) {
2690 CmdArgs.push_back("-fprofile-arcs");
2691 CmdArgs.push_back("-ftest-coverage");
2692 }
2693
2694 if (types::isCXX(Inputs[0].getType()))
2695 CmdArgs.push_back("-D__private_extern__=extern");
2696}
2697
2698void darwin::CC1::AddCPPOptionsArgs(const ArgList &Args, ArgStringList &CmdArgs,
2699 const InputInfoList &Inputs,
2700 const ArgStringList &OutputArgs) const {
2701 // Derived from cpp_options
2702 AddCPPUniqueOptionsArgs(Args, CmdArgs, Inputs);
Daniel Dunbard067f7f2009-04-08 23:54:23 +00002703
Daniel Dunbarafec1f52009-03-29 18:40:18 +00002704 CmdArgs.append(OutputArgs.begin(), OutputArgs.end());
2705
2706 AddCC1Args(Args, CmdArgs);
2707
2708 // NOTE: The code below has some commonality with cpp_options, but
2709 // in classic gcc style ends up sending things in different
2710 // orders. This may be a good merge candidate once we drop pedantic
2711 // compatibility.
2712
2713 Args.AddAllArgs(CmdArgs, options::OPT_m_Group);
Daniel Dunbard067f7f2009-04-08 23:54:23 +00002714 Args.AddAllArgs(CmdArgs, options::OPT_std_EQ, options::OPT_ansi,
Daniel Dunbarafec1f52009-03-29 18:40:18 +00002715 options::OPT_trigraphs);
Daniel Dunbar72a60902009-04-26 01:10:38 +00002716 if (!Args.getLastArg(options::OPT_std_EQ, options::OPT_ansi)) {
2717 // Honor -std-default.
2718 Args.AddAllArgsTranslated(CmdArgs, options::OPT_std_default_EQ,
2719 "-std=", /*Joined=*/true);
2720 }
Daniel Dunbarafec1f52009-03-29 18:40:18 +00002721 Args.AddAllArgs(CmdArgs, options::OPT_W_Group, options::OPT_pedantic_Group);
2722 Args.AddLastArg(CmdArgs, options::OPT_w);
Daniel Dunbard067f7f2009-04-08 23:54:23 +00002723
Daniel Dunbarafec1f52009-03-29 18:40:18 +00002724 // The driver treats -fsyntax-only specially.
2725 Args.AddAllArgs(CmdArgs, options::OPT_f_Group, options::OPT_fsyntax_only);
2726
Daniel Dunbarf28c2ff2011-04-07 20:41:03 +00002727 // Claim Clang only -f options, they aren't worth warning about.
2728 Args.ClaimAllArgs(options::OPT_f_clang_Group);
2729
Daniel Dunbarafec1f52009-03-29 18:40:18 +00002730 if (Args.hasArg(options::OPT_g_Group) && !Args.hasArg(options::OPT_g0) &&
2731 !Args.hasArg(options::OPT_fno_working_directory))
2732 CmdArgs.push_back("-fworking-directory");
2733
2734 Args.AddAllArgs(CmdArgs, options::OPT_O);
2735 Args.AddAllArgs(CmdArgs, options::OPT_undef);
2736 if (Args.hasArg(options::OPT_save_temps))
2737 CmdArgs.push_back("-fpch-preprocess");
2738}
2739
Daniel Dunbard067f7f2009-04-08 23:54:23 +00002740void darwin::CC1::AddCPPUniqueOptionsArgs(const ArgList &Args,
Daniel Dunbarafec1f52009-03-29 18:40:18 +00002741 ArgStringList &CmdArgs,
Mike Stump11289f42009-09-09 15:08:12 +00002742 const InputInfoList &Inputs) const {
Daniel Dunbar083edf72009-12-21 18:54:17 +00002743 const Driver &D = getToolChain().getDriver();
Daniel Dunbarafec1f52009-03-29 18:40:18 +00002744
Daniel Dunbar64198ef2009-09-10 01:21:05 +00002745 CheckPreprocessingOptions(D, Args);
2746
Daniel Dunbarafec1f52009-03-29 18:40:18 +00002747 // Derived from cpp_unique_options.
Daniel Dunbar64198ef2009-09-10 01:21:05 +00002748 // -{C,CC} only with -E is checked in CheckPreprocessingOptions().
2749 Args.AddLastArg(CmdArgs, options::OPT_C);
2750 Args.AddLastArg(CmdArgs, options::OPT_CC);
Daniel Dunbarafec1f52009-03-29 18:40:18 +00002751 if (!Args.hasArg(options::OPT_Q))
2752 CmdArgs.push_back("-quiet");
2753 Args.AddAllArgs(CmdArgs, options::OPT_nostdinc);
Douglas Gregor64b046f2010-03-24 20:13:48 +00002754 Args.AddAllArgs(CmdArgs, options::OPT_nostdincxx);
Daniel Dunbarafec1f52009-03-29 18:40:18 +00002755 Args.AddLastArg(CmdArgs, options::OPT_v);
2756 Args.AddAllArgs(CmdArgs, options::OPT_I_Group, options::OPT_F);
2757 Args.AddLastArg(CmdArgs, options::OPT_P);
2758
2759 // FIXME: Handle %I properly.
2760 if (getToolChain().getArchName() == "x86_64") {
2761 CmdArgs.push_back("-imultilib");
2762 CmdArgs.push_back("x86_64");
2763 }
2764
2765 if (Args.hasArg(options::OPT_MD)) {
2766 CmdArgs.push_back("-MD");
Daniel Dunbar52e96cc2009-03-30 00:34:04 +00002767 CmdArgs.push_back(darwin::CC1::getDependencyFileName(Args, Inputs));
Daniel Dunbarafec1f52009-03-29 18:40:18 +00002768 }
2769
2770 if (Args.hasArg(options::OPT_MMD)) {
2771 CmdArgs.push_back("-MMD");
Daniel Dunbar52e96cc2009-03-30 00:34:04 +00002772 CmdArgs.push_back(darwin::CC1::getDependencyFileName(Args, Inputs));
Daniel Dunbarafec1f52009-03-29 18:40:18 +00002773 }
2774
2775 Args.AddLastArg(CmdArgs, options::OPT_M);
2776 Args.AddLastArg(CmdArgs, options::OPT_MM);
2777 Args.AddAllArgs(CmdArgs, options::OPT_MF);
2778 Args.AddLastArg(CmdArgs, options::OPT_MG);
2779 Args.AddLastArg(CmdArgs, options::OPT_MP);
2780 Args.AddAllArgs(CmdArgs, options::OPT_MQ);
2781 Args.AddAllArgs(CmdArgs, options::OPT_MT);
2782 if (!Args.hasArg(options::OPT_M) && !Args.hasArg(options::OPT_MM) &&
2783 (Args.hasArg(options::OPT_MD) || Args.hasArg(options::OPT_MMD))) {
2784 if (Arg *OutputOpt = Args.getLastArg(options::OPT_o)) {
2785 CmdArgs.push_back("-MQ");
2786 CmdArgs.push_back(OutputOpt->getValue(Args));
2787 }
2788 }
2789
2790 Args.AddLastArg(CmdArgs, options::OPT_remap);
2791 if (Args.hasArg(options::OPT_g3))
2792 CmdArgs.push_back("-dD");
2793 Args.AddLastArg(CmdArgs, options::OPT_H);
2794
2795 AddCPPArgs(Args, CmdArgs);
2796
2797 Args.AddAllArgs(CmdArgs, options::OPT_D, options::OPT_U, options::OPT_A);
2798 Args.AddAllArgs(CmdArgs, options::OPT_i_Group);
2799
2800 for (InputInfoList::const_iterator
2801 it = Inputs.begin(), ie = Inputs.end(); it != ie; ++it) {
2802 const InputInfo &II = *it;
Daniel Dunbard067f7f2009-04-08 23:54:23 +00002803
Daniel Dunbarb440f562010-08-02 02:38:21 +00002804 CmdArgs.push_back(II.getFilename());
Daniel Dunbarafec1f52009-03-29 18:40:18 +00002805 }
2806
2807 Args.AddAllArgValues(CmdArgs, options::OPT_Wp_COMMA,
2808 options::OPT_Xpreprocessor);
2809
2810 if (Args.hasArg(options::OPT_fmudflap)) {
2811 CmdArgs.push_back("-D_MUDFLAP");
2812 CmdArgs.push_back("-include");
2813 CmdArgs.push_back("mf-runtime.h");
2814 }
2815
2816 if (Args.hasArg(options::OPT_fmudflapth)) {
2817 CmdArgs.push_back("-D_MUDFLAP");
2818 CmdArgs.push_back("-D_MUDFLAPTH");
2819 CmdArgs.push_back("-include");
2820 CmdArgs.push_back("mf-runtime.h");
2821 }
2822}
2823
Daniel Dunbard067f7f2009-04-08 23:54:23 +00002824void darwin::CC1::AddCPPArgs(const ArgList &Args,
Daniel Dunbarafec1f52009-03-29 18:40:18 +00002825 ArgStringList &CmdArgs) const {
2826 // Derived from cpp spec.
2827
2828 if (Args.hasArg(options::OPT_static)) {
2829 // The gcc spec is broken here, it refers to dynamic but
2830 // that has been translated. Start by being bug compatible.
Daniel Dunbard067f7f2009-04-08 23:54:23 +00002831
Daniel Dunbarafec1f52009-03-29 18:40:18 +00002832 // if (!Args.hasArg(arglist.parser.dynamicOption))
2833 CmdArgs.push_back("-D__STATIC__");
2834 } else
2835 CmdArgs.push_back("-D__DYNAMIC__");
2836
2837 if (Args.hasArg(options::OPT_pthread))
2838 CmdArgs.push_back("-D_REENTRANT");
2839}
2840
Daniel Dunbare6adeee2009-03-29 17:08:39 +00002841void darwin::Preprocess::ConstructJob(Compilation &C, const JobAction &JA,
Daniel Dunbar7fbaf532010-08-02 02:38:28 +00002842 const InputInfo &Output,
Daniel Dunbard067f7f2009-04-08 23:54:23 +00002843 const InputInfoList &Inputs,
2844 const ArgList &Args,
Daniel Dunbare6adeee2009-03-29 17:08:39 +00002845 const char *LinkingOutput) const {
2846 ArgStringList CmdArgs;
2847
2848 assert(Inputs.size() == 1 && "Unexpected number of inputs!");
2849
2850 CmdArgs.push_back("-E");
2851
2852 if (Args.hasArg(options::OPT_traditional) ||
Daniel Dunbare6adeee2009-03-29 17:08:39 +00002853 Args.hasArg(options::OPT_traditional_cpp))
2854 CmdArgs.push_back("-traditional-cpp");
Daniel Dunbard067f7f2009-04-08 23:54:23 +00002855
Daniel Dunbare6adeee2009-03-29 17:08:39 +00002856 ArgStringList OutputArgs;
Daniel Dunbarb440f562010-08-02 02:38:21 +00002857 assert(Output.isFilename() && "Unexpected CC1 output.");
2858 OutputArgs.push_back("-o");
2859 OutputArgs.push_back(Output.getFilename());
Daniel Dunbare6adeee2009-03-29 17:08:39 +00002860
Joerg Sonnenbergerb86f5f42011-03-06 23:31:01 +00002861 if (Args.hasArg(options::OPT_E) || getToolChain().getDriver().CCCIsCPP) {
Daniel Dunbarf64f5302009-03-29 22:27:40 +00002862 AddCPPOptionsArgs(Args, CmdArgs, Inputs, OutputArgs);
2863 } else {
2864 AddCPPOptionsArgs(Args, CmdArgs, Inputs, ArgStringList());
2865 CmdArgs.append(OutputArgs.begin(), OutputArgs.end());
2866 }
Daniel Dunbard067f7f2009-04-08 23:54:23 +00002867
Daniel Dunbar6a8803a2009-04-03 01:27:06 +00002868 Args.AddAllArgs(CmdArgs, options::OPT_d_Group);
2869
Daniel Dunbare6adeee2009-03-29 17:08:39 +00002870 const char *CC1Name = getCC1Name(Inputs[0].getType());
Daniel Dunbard067f7f2009-04-08 23:54:23 +00002871 const char *Exec =
Daniel Dunbar9c3ed5f2010-07-14 18:46:23 +00002872 Args.MakeArgString(getToolChain().GetProgramPath(CC1Name));
Daniel Dunbar7fbaf532010-08-02 02:38:28 +00002873 C.addCommand(new Command(JA, *this, Exec, CmdArgs));
Daniel Dunbare6adeee2009-03-29 17:08:39 +00002874}
2875
2876void darwin::Compile::ConstructJob(Compilation &C, const JobAction &JA,
Daniel Dunbar7fbaf532010-08-02 02:38:28 +00002877 const InputInfo &Output,
Daniel Dunbard067f7f2009-04-08 23:54:23 +00002878 const InputInfoList &Inputs,
2879 const ArgList &Args,
Daniel Dunbare6adeee2009-03-29 17:08:39 +00002880 const char *LinkingOutput) const {
Daniel Dunbar083edf72009-12-21 18:54:17 +00002881 const Driver &D = getToolChain().getDriver();
Daniel Dunbare6adeee2009-03-29 17:08:39 +00002882 ArgStringList CmdArgs;
2883
2884 assert(Inputs.size() == 1 && "Unexpected number of inputs!");
2885
2886 types::ID InputType = Inputs[0].getType();
2887 const Arg *A;
Eli Friedmanbb0d9a52009-07-14 21:58:17 +00002888 if ((A = Args.getLastArg(options::OPT_traditional)))
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002889 D.Diag(diag::err_drv_argument_only_allowed_with)
Daniel Dunbare6adeee2009-03-29 17:08:39 +00002890 << A->getAsString(Args) << "-E";
2891
Daniel Dunbar24e52992010-06-07 23:28:45 +00002892 if (JA.getType() == types::TY_LLVM_IR ||
2893 JA.getType() == types::TY_LTO_IR)
Daniel Dunbare6adeee2009-03-29 17:08:39 +00002894 CmdArgs.push_back("-emit-llvm");
Daniel Dunbar24e52992010-06-07 23:28:45 +00002895 else if (JA.getType() == types::TY_LLVM_BC ||
2896 JA.getType() == types::TY_LTO_BC)
Daniel Dunbare6adeee2009-03-29 17:08:39 +00002897 CmdArgs.push_back("-emit-llvm-bc");
Daniel Dunbar6cdf83c2009-09-01 16:57:46 +00002898 else if (Output.getType() == types::TY_AST)
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002899 D.Diag(diag::err_drv_no_ast_support)
Daniel Dunbar1c28f1e2009-09-09 22:32:48 +00002900 << getToolChain().getTripleString();
Daniel Dunbarbcd554f2010-02-11 17:33:45 +00002901 else if (JA.getType() != types::TY_PP_Asm &&
2902 JA.getType() != types::TY_PCH)
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002903 D.Diag(diag::err_drv_invalid_gcc_output_type)
Daniel Dunbaraeea8ac2010-02-11 03:16:21 +00002904 << getTypeName(JA.getType());
Daniel Dunbare6adeee2009-03-29 17:08:39 +00002905
2906 ArgStringList OutputArgs;
2907 if (Output.getType() != types::TY_PCH) {
2908 OutputArgs.push_back("-o");
Daniel Dunbarb440f562010-08-02 02:38:21 +00002909 if (Output.isNothing())
Daniel Dunbare6adeee2009-03-29 17:08:39 +00002910 OutputArgs.push_back("/dev/null");
2911 else
2912 OutputArgs.push_back(Output.getFilename());
2913 }
2914
2915 // There is no need for this level of compatibility, but it makes
2916 // diffing easier.
2917 bool OutputArgsEarly = (Args.hasArg(options::OPT_fsyntax_only) ||
2918 Args.hasArg(options::OPT_S));
2919
2920 if (types::getPreprocessedType(InputType) != types::TY_INVALID) {
Daniel Dunbarafec1f52009-03-29 18:40:18 +00002921 AddCPPUniqueOptionsArgs(Args, CmdArgs, Inputs);
Daniel Dunbare6adeee2009-03-29 17:08:39 +00002922 if (OutputArgsEarly) {
2923 AddCC1OptionsArgs(Args, CmdArgs, Inputs, OutputArgs);
2924 } else {
2925 AddCC1OptionsArgs(Args, CmdArgs, Inputs, ArgStringList());
2926 CmdArgs.append(OutputArgs.begin(), OutputArgs.end());
2927 }
2928 } else {
2929 CmdArgs.push_back("-fpreprocessed");
Daniel Dunbard067f7f2009-04-08 23:54:23 +00002930
Daniel Dunbare6adeee2009-03-29 17:08:39 +00002931 for (InputInfoList::const_iterator
2932 it = Inputs.begin(), ie = Inputs.end(); it != ie; ++it) {
2933 const InputInfo &II = *it;
2934
Daniel Dunbar6cdf83c2009-09-01 16:57:46 +00002935 // Reject AST inputs.
2936 if (II.getType() == types::TY_AST) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002937 D.Diag(diag::err_drv_no_ast_support)
Daniel Dunbar1c28f1e2009-09-09 22:32:48 +00002938 << getToolChain().getTripleString();
Daniel Dunbar6cdf83c2009-09-01 16:57:46 +00002939 return;
2940 }
2941
Daniel Dunbarb440f562010-08-02 02:38:21 +00002942 CmdArgs.push_back(II.getFilename());
Daniel Dunbare6adeee2009-03-29 17:08:39 +00002943 }
2944
2945 if (OutputArgsEarly) {
2946 AddCC1OptionsArgs(Args, CmdArgs, Inputs, OutputArgs);
2947 } else {
2948 AddCC1OptionsArgs(Args, CmdArgs, Inputs, ArgStringList());
2949 CmdArgs.append(OutputArgs.begin(), OutputArgs.end());
2950 }
2951 }
Daniel Dunbard067f7f2009-04-08 23:54:23 +00002952
Daniel Dunbare6adeee2009-03-29 17:08:39 +00002953 if (Output.getType() == types::TY_PCH) {
2954 assert(Output.isFilename() && "Invalid PCH output.");
2955
2956 CmdArgs.push_back("-o");
2957 // NOTE: gcc uses a temp .s file for this, but there doesn't seem
2958 // to be a good reason.
Chad Rosier96d690c2011-08-01 19:58:48 +00002959 const char *TmpPath = C.getArgs().MakeArgString(
2960 D.GetTemporaryPath("s"));
2961 C.addTempFile(TmpPath);
2962 CmdArgs.push_back(TmpPath);
Daniel Dunbard067f7f2009-04-08 23:54:23 +00002963
Eric Christopher84fbdb42011-08-19 00:30:14 +00002964 // If we're emitting a pch file with the last 4 characters of ".pth"
2965 // and falling back to llvm-gcc we want to use ".gch" instead.
2966 std::string OutputFile(Output.getFilename());
2967 size_t loc = OutputFile.rfind(".pth");
2968 if (loc != std::string::npos)
2969 OutputFile.replace(loc, 4, ".gch");
2970 const char *Tmp = C.getArgs().MakeArgString("--output-pch="+OutputFile);
2971 CmdArgs.push_back(Tmp);
Daniel Dunbard067f7f2009-04-08 23:54:23 +00002972 }
Daniel Dunbare6adeee2009-03-29 17:08:39 +00002973
Chad Rosierbc5ea3d2011-08-17 18:24:55 +00002974 RemoveCC1UnsupportedArgs(CmdArgs);
2975
Daniel Dunbare6adeee2009-03-29 17:08:39 +00002976 const char *CC1Name = getCC1Name(Inputs[0].getType());
Daniel Dunbard067f7f2009-04-08 23:54:23 +00002977 const char *Exec =
Daniel Dunbar9c3ed5f2010-07-14 18:46:23 +00002978 Args.MakeArgString(getToolChain().GetProgramPath(CC1Name));
Daniel Dunbar7fbaf532010-08-02 02:38:28 +00002979 C.addCommand(new Command(JA, *this, Exec, CmdArgs));
Daniel Dunbare6adeee2009-03-29 17:08:39 +00002980}
2981
Daniel Dunbarbe220842009-03-20 16:06:39 +00002982void darwin::Assemble::ConstructJob(Compilation &C, const JobAction &JA,
Daniel Dunbar7fbaf532010-08-02 02:38:28 +00002983 const InputInfo &Output,
Daniel Dunbard067f7f2009-04-08 23:54:23 +00002984 const InputInfoList &Inputs,
2985 const ArgList &Args,
Daniel Dunbarbe220842009-03-20 16:06:39 +00002986 const char *LinkingOutput) const {
2987 ArgStringList CmdArgs;
2988
2989 assert(Inputs.size() == 1 && "Unexpected number of inputs.");
2990 const InputInfo &Input = Inputs[0];
2991
Daniel Dunbardc8355e2011-04-12 23:59:20 +00002992 // Determine the original source input.
2993 const Action *SourceAction = &JA;
2994 while (SourceAction->getKind() != Action::InputClass) {
2995 assert(!SourceAction->getInputs().empty() && "unexpected root action!");
2996 SourceAction = SourceAction->getInputs()[0];
2997 }
2998
2999 // Forward -g, assuming we are dealing with an actual assembly file.
Eric Christopher84fbdb42011-08-19 00:30:14 +00003000 if (SourceAction->getType() == types::TY_Asm ||
Daniel Dunbardc8355e2011-04-12 23:59:20 +00003001 SourceAction->getType() == types::TY_PP_Asm) {
Daniel Dunbar5c9c1182009-04-01 00:27:44 +00003002 if (Args.hasArg(options::OPT_gstabs))
3003 CmdArgs.push_back("--gstabs");
3004 else if (Args.hasArg(options::OPT_g_Group))
3005 CmdArgs.push_back("--gdwarf2");
3006 }
Daniel Dunbard067f7f2009-04-08 23:54:23 +00003007
Daniel Dunbarbe220842009-03-20 16:06:39 +00003008 // Derived from asm spec.
Daniel Dunbar3571dd92009-09-09 18:36:27 +00003009 AddDarwinArch(Args, CmdArgs);
Daniel Dunbarbe220842009-03-20 16:06:39 +00003010
Daniel Dunbar6d484762010-07-22 01:47:22 +00003011 // Use -force_cpusubtype_ALL on x86 by default.
3012 if (getToolChain().getTriple().getArch() == llvm::Triple::x86 ||
3013 getToolChain().getTriple().getArch() == llvm::Triple::x86_64 ||
Daniel Dunbar3571dd92009-09-09 18:36:27 +00003014 Args.hasArg(options::OPT_force__cpusubtype__ALL))
3015 CmdArgs.push_back("-force_cpusubtype_ALL");
3016
Daniel Dunbara6b4a3d2009-08-24 22:26:16 +00003017 if (getToolChain().getTriple().getArch() != llvm::Triple::x86_64 &&
3018 (Args.hasArg(options::OPT_mkernel) ||
Daniel Dunbarbe220842009-03-20 16:06:39 +00003019 Args.hasArg(options::OPT_static) ||
Daniel Dunbara6b4a3d2009-08-24 22:26:16 +00003020 Args.hasArg(options::OPT_fapple_kext)))
Daniel Dunbard067f7f2009-04-08 23:54:23 +00003021 CmdArgs.push_back("-static");
3022
Daniel Dunbarbe220842009-03-20 16:06:39 +00003023 Args.AddAllArgValues(CmdArgs, options::OPT_Wa_COMMA,
3024 options::OPT_Xassembler);
3025
3026 assert(Output.isFilename() && "Unexpected lipo output.");
3027 CmdArgs.push_back("-o");
3028 CmdArgs.push_back(Output.getFilename());
3029
Daniel Dunbarb440f562010-08-02 02:38:21 +00003030 assert(Input.isFilename() && "Invalid input.");
3031 CmdArgs.push_back(Input.getFilename());
Daniel Dunbarbe220842009-03-20 16:06:39 +00003032
3033 // asm_final spec is empty.
3034
Daniel Dunbard067f7f2009-04-08 23:54:23 +00003035 const char *Exec =
Daniel Dunbar9c3ed5f2010-07-14 18:46:23 +00003036 Args.MakeArgString(getToolChain().GetProgramPath("as"));
Daniel Dunbar7fbaf532010-08-02 02:38:28 +00003037 C.addCommand(new Command(JA, *this, Exec, CmdArgs));
Daniel Dunbarbe220842009-03-20 16:06:39 +00003038}
Daniel Dunbar64ed5e32009-03-20 00:52:38 +00003039
Daniel Dunbare9ded432009-09-09 18:36:20 +00003040void darwin::DarwinTool::AddDarwinArch(const ArgList &Args,
3041 ArgStringList &CmdArgs) const {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003042 StringRef ArchName = getDarwinToolChain().getDarwinArchName(Args);
Daniel Dunbardcc3b652010-01-22 02:04:58 +00003043
Daniel Dunbarc1964212009-03-26 16:23:12 +00003044 // Derived from darwin_arch spec.
3045 CmdArgs.push_back("-arch");
Daniel Dunbardcc3b652010-01-22 02:04:58 +00003046 CmdArgs.push_back(Args.MakeArgString(ArchName));
Daniel Dunbar91dbfd62009-09-04 18:35:31 +00003047
Daniel Dunbardcc3b652010-01-22 02:04:58 +00003048 // FIXME: Is this needed anymore?
3049 if (ArchName == "arm")
Daniel Dunbar91dbfd62009-09-04 18:35:31 +00003050 CmdArgs.push_back("-force_cpusubtype_ALL");
Daniel Dunbarc1964212009-03-26 16:23:12 +00003051}
3052
Daniel Dunbarccbc4522010-09-09 21:51:05 +00003053void darwin::Link::AddLinkArgs(Compilation &C,
3054 const ArgList &Args,
Daniel Dunbarc1964212009-03-26 16:23:12 +00003055 ArgStringList &CmdArgs) const {
Daniel Dunbar083edf72009-12-21 18:54:17 +00003056 const Driver &D = getToolChain().getDriver();
Daniel Dunbarc44d3132011-04-28 21:23:41 +00003057 const toolchains::Darwin &DarwinTC = getDarwinToolChain();
Daniel Dunbarc1964212009-03-26 16:23:12 +00003058
Daniel Dunbarcacb0e22010-08-11 23:07:50 +00003059 unsigned Version[3] = { 0, 0, 0 };
3060 if (Arg *A = Args.getLastArg(options::OPT_mlinker_version_EQ)) {
3061 bool HadExtra;
3062 if (!Driver::GetReleaseVersion(A->getValue(Args), Version[0],
3063 Version[1], Version[2], HadExtra) ||
3064 HadExtra)
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003065 D.Diag(diag::err_drv_invalid_version_number)
Daniel Dunbarcacb0e22010-08-11 23:07:50 +00003066 << A->getAsString(Args);
3067 }
3068
3069 // Newer linkers support -demangle, pass it if supported and not disabled by
3070 // the user.
Daniel Dunbar6d776eb2010-11-19 17:51:40 +00003071 //
3072 // FIXME: We temporarily avoid passing -demangle to any iOS linker, because
3073 // unfortunately we can't be guaranteed that the linker version used there
3074 // will match the linker version detected at configure time. We need the
3075 // universal driver.
3076 if (Version[0] >= 100 && !Args.hasArg(options::OPT_Z_Xlinker__no_demangle) &&
Daniel Dunbarc44d3132011-04-28 21:23:41 +00003077 !DarwinTC.isTargetIPhoneOS()) {
Daniel Dunbar1eaf5c62010-09-07 17:07:49 +00003078 // Don't pass -demangle to ld_classic.
3079 //
3080 // FIXME: This is a temporary workaround, ld should be handling this.
3081 bool UsesLdClassic = (getToolChain().getArch() == llvm::Triple::x86 &&
3082 Args.hasArg(options::OPT_static));
Daniel Dunbar3d7e0e22010-09-07 17:50:41 +00003083 if (getToolChain().getArch() == llvm::Triple::x86) {
3084 for (arg_iterator it = Args.filtered_begin(options::OPT_Xlinker,
3085 options::OPT_Wl_COMMA),
3086 ie = Args.filtered_end(); it != ie; ++it) {
3087 const Arg *A = *it;
3088 for (unsigned i = 0, e = A->getNumValues(); i != e; ++i)
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003089 if (StringRef(A->getValue(Args, i)) == "-kext")
Daniel Dunbar3d7e0e22010-09-07 17:50:41 +00003090 UsesLdClassic = true;
3091 }
3092 }
Daniel Dunbar1eaf5c62010-09-07 17:07:49 +00003093 if (!UsesLdClassic)
3094 CmdArgs.push_back("-demangle");
Daniel Dunbarcacb0e22010-08-11 23:07:50 +00003095 }
3096
Daniel Dunbaref889c72011-06-21 20:55:11 +00003097 // If we are using LTO, then automatically create a temporary file path for
3098 // the linker to use, so that it's lifetime will extend past a possible
3099 // dsymutil step.
Daniel Dunbar3d125d32011-06-21 21:18:32 +00003100 if (Version[0] >= 116 && D.IsUsingLTO(Args)) {
Daniel Dunbaref889c72011-06-21 20:55:11 +00003101 const char *TmpPath = C.getArgs().MakeArgString(
3102 D.GetTemporaryPath(types::getTypeTempSuffix(types::TY_Object)));
3103 C.addTempFile(TmpPath);
3104 CmdArgs.push_back("-object_path_lto");
3105 CmdArgs.push_back(TmpPath);
3106 }
3107
Daniel Dunbarc1964212009-03-26 16:23:12 +00003108 // Derived from the "link" spec.
3109 Args.AddAllArgs(CmdArgs, options::OPT_static);
3110 if (!Args.hasArg(options::OPT_static))
3111 CmdArgs.push_back("-dynamic");
3112 if (Args.hasArg(options::OPT_fgnu_runtime)) {
3113 // FIXME: gcc replaces -lobjc in forward args with -lobjc-gnu
3114 // here. How do we wish to handle such things?
3115 }
Daniel Dunbard067f7f2009-04-08 23:54:23 +00003116
Daniel Dunbarc1964212009-03-26 16:23:12 +00003117 if (!Args.hasArg(options::OPT_dynamiclib)) {
Daniel Dunbara48823f2010-01-22 02:04:52 +00003118 AddDarwinArch(Args, CmdArgs);
Daniel Dunbara48823f2010-01-22 02:04:52 +00003119 // FIXME: Why do this only on this path?
Daniel Dunbar93d7acf2010-01-22 03:37:33 +00003120 Args.AddLastArg(CmdArgs, options::OPT_force__cpusubtype__ALL);
Daniel Dunbarc1964212009-03-26 16:23:12 +00003121
3122 Args.AddLastArg(CmdArgs, options::OPT_bundle);
3123 Args.AddAllArgs(CmdArgs, options::OPT_bundle__loader);
3124 Args.AddAllArgs(CmdArgs, options::OPT_client__name);
3125
3126 Arg *A;
3127 if ((A = Args.getLastArg(options::OPT_compatibility__version)) ||
3128 (A = Args.getLastArg(options::OPT_current__version)) ||
3129 (A = Args.getLastArg(options::OPT_install__name)))
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003130 D.Diag(diag::err_drv_argument_only_allowed_with)
Daniel Dunbarc1964212009-03-26 16:23:12 +00003131 << A->getAsString(Args) << "-dynamiclib";
3132
3133 Args.AddLastArg(CmdArgs, options::OPT_force__flat__namespace);
3134 Args.AddLastArg(CmdArgs, options::OPT_keep__private__externs);
3135 Args.AddLastArg(CmdArgs, options::OPT_private__bundle);
3136 } else {
3137 CmdArgs.push_back("-dylib");
3138
3139 Arg *A;
3140 if ((A = Args.getLastArg(options::OPT_bundle)) ||
3141 (A = Args.getLastArg(options::OPT_bundle__loader)) ||
3142 (A = Args.getLastArg(options::OPT_client__name)) ||
3143 (A = Args.getLastArg(options::OPT_force__flat__namespace)) ||
3144 (A = Args.getLastArg(options::OPT_keep__private__externs)) ||
3145 (A = Args.getLastArg(options::OPT_private__bundle)))
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003146 D.Diag(diag::err_drv_argument_not_allowed_with)
Daniel Dunbarc1964212009-03-26 16:23:12 +00003147 << A->getAsString(Args) << "-dynamiclib";
Daniel Dunbard067f7f2009-04-08 23:54:23 +00003148
Daniel Dunbarc1964212009-03-26 16:23:12 +00003149 Args.AddAllArgsTranslated(CmdArgs, options::OPT_compatibility__version,
3150 "-dylib_compatibility_version");
3151 Args.AddAllArgsTranslated(CmdArgs, options::OPT_current__version,
3152 "-dylib_current_version");
3153
Daniel Dunbara48823f2010-01-22 02:04:52 +00003154 AddDarwinArch(Args, CmdArgs);
Daniel Dunbarc1964212009-03-26 16:23:12 +00003155
3156 Args.AddAllArgsTranslated(CmdArgs, options::OPT_install__name,
3157 "-dylib_install_name");
3158 }
3159
3160 Args.AddLastArg(CmdArgs, options::OPT_all__load);
3161 Args.AddAllArgs(CmdArgs, options::OPT_allowable__client);
3162 Args.AddLastArg(CmdArgs, options::OPT_bind__at__load);
Daniel Dunbarc44d3132011-04-28 21:23:41 +00003163 if (DarwinTC.isTargetIPhoneOS())
Daniel Dunbar2b5f6812009-09-04 18:35:41 +00003164 Args.AddLastArg(CmdArgs, options::OPT_arch__errors__fatal);
Daniel Dunbarc1964212009-03-26 16:23:12 +00003165 Args.AddLastArg(CmdArgs, options::OPT_dead__strip);
3166 Args.AddLastArg(CmdArgs, options::OPT_no__dead__strip__inits__and__terms);
3167 Args.AddAllArgs(CmdArgs, options::OPT_dylib__file);
3168 Args.AddLastArg(CmdArgs, options::OPT_dynamic);
3169 Args.AddAllArgs(CmdArgs, options::OPT_exported__symbols__list);
3170 Args.AddLastArg(CmdArgs, options::OPT_flat__namespace);
Daniel Dunbar044a3902011-06-28 20:16:02 +00003171 Args.AddAllArgs(CmdArgs, options::OPT_force__load);
Daniel Dunbarc1964212009-03-26 16:23:12 +00003172 Args.AddAllArgs(CmdArgs, options::OPT_headerpad__max__install__names);
3173 Args.AddAllArgs(CmdArgs, options::OPT_image__base);
3174 Args.AddAllArgs(CmdArgs, options::OPT_init);
3175
Daniel Dunbarc44d3132011-04-28 21:23:41 +00003176 // Add the deployment target.
3177 unsigned TargetVersion[3];
3178 DarwinTC.getTargetVersion(TargetVersion);
Daniel Dunbar72ceb922011-04-30 04:22:58 +00003179
3180 // If we had an explicit -mios-simulator-version-min argument, honor that,
3181 // otherwise use the traditional deployment targets. We can't just check the
3182 // is-sim attribute because existing code follows this path, and the linker
3183 // may not handle the argument.
3184 //
3185 // FIXME: We may be able to remove this, once we can verify no one depends on
3186 // it.
3187 if (Args.hasArg(options::OPT_mios_simulator_version_min_EQ))
3188 CmdArgs.push_back("-ios_simulator_version_min");
3189 else if (DarwinTC.isTargetIPhoneOS())
3190 CmdArgs.push_back("-iphoneos_version_min");
3191 else
3192 CmdArgs.push_back("-macosx_version_min");
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003193 CmdArgs.push_back(Args.MakeArgString(Twine(TargetVersion[0]) + "." +
3194 Twine(TargetVersion[1]) + "." +
3195 Twine(TargetVersion[2])));
Daniel Dunbarc44d3132011-04-28 21:23:41 +00003196
Daniel Dunbarc1964212009-03-26 16:23:12 +00003197 Args.AddLastArg(CmdArgs, options::OPT_nomultidefs);
3198 Args.AddLastArg(CmdArgs, options::OPT_multi__module);
3199 Args.AddLastArg(CmdArgs, options::OPT_single__module);
3200 Args.AddAllArgs(CmdArgs, options::OPT_multiply__defined);
3201 Args.AddAllArgs(CmdArgs, options::OPT_multiply__defined__unused);
Daniel Dunbard067f7f2009-04-08 23:54:23 +00003202
Daniel Dunbaraf68a882010-07-13 23:31:40 +00003203 if (const Arg *A = Args.getLastArg(options::OPT_fpie, options::OPT_fPIE,
3204 options::OPT_fno_pie,
3205 options::OPT_fno_PIE)) {
3206 if (A->getOption().matches(options::OPT_fpie) ||
3207 A->getOption().matches(options::OPT_fPIE))
3208 CmdArgs.push_back("-pie");
3209 else
3210 CmdArgs.push_back("-no_pie");
3211 }
Daniel Dunbarc1964212009-03-26 16:23:12 +00003212
3213 Args.AddLastArg(CmdArgs, options::OPT_prebind);
3214 Args.AddLastArg(CmdArgs, options::OPT_noprebind);
3215 Args.AddLastArg(CmdArgs, options::OPT_nofixprebinding);
3216 Args.AddLastArg(CmdArgs, options::OPT_prebind__all__twolevel__modules);
3217 Args.AddLastArg(CmdArgs, options::OPT_read__only__relocs);
3218 Args.AddAllArgs(CmdArgs, options::OPT_sectcreate);
3219 Args.AddAllArgs(CmdArgs, options::OPT_sectorder);
3220 Args.AddAllArgs(CmdArgs, options::OPT_seg1addr);
3221 Args.AddAllArgs(CmdArgs, options::OPT_segprot);
3222 Args.AddAllArgs(CmdArgs, options::OPT_segaddr);
3223 Args.AddAllArgs(CmdArgs, options::OPT_segs__read__only__addr);
3224 Args.AddAllArgs(CmdArgs, options::OPT_segs__read__write__addr);
3225 Args.AddAllArgs(CmdArgs, options::OPT_seg__addr__table);
3226 Args.AddAllArgs(CmdArgs, options::OPT_seg__addr__table__filename);
3227 Args.AddAllArgs(CmdArgs, options::OPT_sub__library);
3228 Args.AddAllArgs(CmdArgs, options::OPT_sub__umbrella);
Daniel Dunbar2b5f6812009-09-04 18:35:41 +00003229
Daniel Dunbar84384642011-05-02 21:03:47 +00003230 // Give --sysroot= preference, over the Apple specific behavior to also use
3231 // --isysroot as the syslibroot.
3232 if (const Arg *A = Args.getLastArg(options::OPT__sysroot_EQ)) {
3233 CmdArgs.push_back("-syslibroot");
3234 CmdArgs.push_back(A->getValue(Args));
3235 } else if (const Arg *A = Args.getLastArg(options::OPT_isysroot)) {
3236 CmdArgs.push_back("-syslibroot");
3237 CmdArgs.push_back(A->getValue(Args));
3238 } else if (getDarwinToolChain().isTargetIPhoneOS()) {
3239 CmdArgs.push_back("-syslibroot");
3240 CmdArgs.push_back("/Developer/SDKs/Extra");
Daniel Dunbar2b5f6812009-09-04 18:35:41 +00003241 }
3242
Daniel Dunbarc1964212009-03-26 16:23:12 +00003243 Args.AddLastArg(CmdArgs, options::OPT_twolevel__namespace);
3244 Args.AddLastArg(CmdArgs, options::OPT_twolevel__namespace__hints);
3245 Args.AddAllArgs(CmdArgs, options::OPT_umbrella);
3246 Args.AddAllArgs(CmdArgs, options::OPT_undefined);
3247 Args.AddAllArgs(CmdArgs, options::OPT_unexported__symbols__list);
Daniel Dunbar2b5f6812009-09-04 18:35:41 +00003248 Args.AddAllArgs(CmdArgs, options::OPT_weak__reference__mismatches);
Daniel Dunbarc1964212009-03-26 16:23:12 +00003249 Args.AddLastArg(CmdArgs, options::OPT_X_Flag);
3250 Args.AddAllArgs(CmdArgs, options::OPT_y);
3251 Args.AddLastArg(CmdArgs, options::OPT_w);
3252 Args.AddAllArgs(CmdArgs, options::OPT_pagezero__size);
3253 Args.AddAllArgs(CmdArgs, options::OPT_segs__read__);
3254 Args.AddLastArg(CmdArgs, options::OPT_seglinkedit);
3255 Args.AddLastArg(CmdArgs, options::OPT_noseglinkedit);
3256 Args.AddAllArgs(CmdArgs, options::OPT_sectalign);
3257 Args.AddAllArgs(CmdArgs, options::OPT_sectobjectsymbols);
3258 Args.AddAllArgs(CmdArgs, options::OPT_segcreate);
3259 Args.AddLastArg(CmdArgs, options::OPT_whyload);
3260 Args.AddLastArg(CmdArgs, options::OPT_whatsloaded);
3261 Args.AddAllArgs(CmdArgs, options::OPT_dylinker__install__name);
3262 Args.AddLastArg(CmdArgs, options::OPT_dylinker);
3263 Args.AddLastArg(CmdArgs, options::OPT_Mach);
3264}
3265
3266void darwin::Link::ConstructJob(Compilation &C, const JobAction &JA,
Daniel Dunbar7fbaf532010-08-02 02:38:28 +00003267 const InputInfo &Output,
Daniel Dunbard067f7f2009-04-08 23:54:23 +00003268 const InputInfoList &Inputs,
3269 const ArgList &Args,
Daniel Dunbarc1964212009-03-26 16:23:12 +00003270 const char *LinkingOutput) const {
3271 assert(Output.getType() == types::TY_Image && "Invalid linker output type.");
Daniel Dunbarc09988d2009-09-08 16:39:16 +00003272
Daniel Dunbarc1964212009-03-26 16:23:12 +00003273 // The logic here is derived from gcc's behavior; most of which
3274 // comes from specs (starting with link_command). Consult gcc for
3275 // more information.
Daniel Dunbarc1964212009-03-26 16:23:12 +00003276 ArgStringList CmdArgs;
3277
3278 // I'm not sure why this particular decomposition exists in gcc, but
3279 // we follow suite for ease of comparison.
Daniel Dunbarccbc4522010-09-09 21:51:05 +00003280 AddLinkArgs(C, Args, CmdArgs);
Daniel Dunbarc1964212009-03-26 16:23:12 +00003281
Daniel Dunbarc1964212009-03-26 16:23:12 +00003282 Args.AddAllArgs(CmdArgs, options::OPT_d_Flag);
3283 Args.AddAllArgs(CmdArgs, options::OPT_s);
3284 Args.AddAllArgs(CmdArgs, options::OPT_t);
3285 Args.AddAllArgs(CmdArgs, options::OPT_Z_Flag);
3286 Args.AddAllArgs(CmdArgs, options::OPT_u_Group);
3287 Args.AddAllArgs(CmdArgs, options::OPT_A);
3288 Args.AddLastArg(CmdArgs, options::OPT_e);
3289 Args.AddAllArgs(CmdArgs, options::OPT_m_Separate);
3290 Args.AddAllArgs(CmdArgs, options::OPT_r);
3291
Daniel Dunbar767bbab2010-10-18 22:08:36 +00003292 // Forward -ObjC when either -ObjC or -ObjC++ is used, to force loading
3293 // members of static archive libraries which implement Objective-C classes or
3294 // categories.
3295 if (Args.hasArg(options::OPT_ObjC) || Args.hasArg(options::OPT_ObjCXX))
3296 CmdArgs.push_back("-ObjC");
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00003297
Daniel Dunbarc1964212009-03-26 16:23:12 +00003298 CmdArgs.push_back("-o");
3299 CmdArgs.push_back(Output.getFilename());
3300
Daniel Dunbarc1964212009-03-26 16:23:12 +00003301 if (!Args.hasArg(options::OPT_A) &&
3302 !Args.hasArg(options::OPT_nostdlib) &&
3303 !Args.hasArg(options::OPT_nostartfiles)) {
3304 // Derived from startfile spec.
3305 if (Args.hasArg(options::OPT_dynamiclib)) {
Daniel Dunbard067f7f2009-04-08 23:54:23 +00003306 // Derived from darwin_dylib1 spec.
Daniel Dunbar16d97092011-04-01 21:02:42 +00003307 if (getDarwinToolChain().isTargetIOSSimulator()) {
3308 // The simulator doesn't have a versioned crt1 file.
3309 CmdArgs.push_back("-ldylib1.o");
3310 } else if (getDarwinToolChain().isTargetIPhoneOS()) {
Daniel Dunbar83608032010-01-27 00:56:56 +00003311 if (getDarwinToolChain().isIPhoneOSVersionLT(3, 1))
3312 CmdArgs.push_back("-ldylib1.o");
3313 } else {
Daniel Dunbar6d23b2f2010-01-27 00:57:03 +00003314 if (getDarwinToolChain().isMacosxVersionLT(10, 5))
Daniel Dunbar83608032010-01-27 00:56:56 +00003315 CmdArgs.push_back("-ldylib1.o");
Daniel Dunbar6d23b2f2010-01-27 00:57:03 +00003316 else if (getDarwinToolChain().isMacosxVersionLT(10, 6))
Daniel Dunbar83608032010-01-27 00:56:56 +00003317 CmdArgs.push_back("-ldylib1.10.5.o");
3318 }
Daniel Dunbarc1964212009-03-26 16:23:12 +00003319 } else {
3320 if (Args.hasArg(options::OPT_bundle)) {
Daniel Dunbarae8bca02009-04-01 03:17:40 +00003321 if (!Args.hasArg(options::OPT_static)) {
3322 // Derived from darwin_bundle1 spec.
Daniel Dunbar16d97092011-04-01 21:02:42 +00003323 if (getDarwinToolChain().isTargetIOSSimulator()) {
3324 // The simulator doesn't have a versioned crt1 file.
3325 CmdArgs.push_back("-lbundle1.o");
3326 } else if (getDarwinToolChain().isTargetIPhoneOS()) {
Daniel Dunbar83608032010-01-27 00:56:56 +00003327 if (getDarwinToolChain().isIPhoneOSVersionLT(3, 1))
3328 CmdArgs.push_back("-lbundle1.o");
3329 } else {
Daniel Dunbar6d23b2f2010-01-27 00:57:03 +00003330 if (getDarwinToolChain().isMacosxVersionLT(10, 6))
Daniel Dunbar83608032010-01-27 00:56:56 +00003331 CmdArgs.push_back("-lbundle1.o");
3332 }
Daniel Dunbarae8bca02009-04-01 03:17:40 +00003333 }
Daniel Dunbarc1964212009-03-26 16:23:12 +00003334 } else {
Daniel Dunbar733b0f82011-03-01 18:49:30 +00003335 if (Args.hasArg(options::OPT_pg) &&
3336 getToolChain().SupportsProfiling()) {
Daniel Dunbarc1964212009-03-26 16:23:12 +00003337 if (Args.hasArg(options::OPT_static) ||
3338 Args.hasArg(options::OPT_object) ||
3339 Args.hasArg(options::OPT_preload)) {
3340 CmdArgs.push_back("-lgcrt0.o");
3341 } else {
3342 CmdArgs.push_back("-lgcrt1.o");
Daniel Dunbard067f7f2009-04-08 23:54:23 +00003343
Daniel Dunbarc1964212009-03-26 16:23:12 +00003344 // darwin_crt2 spec is empty.
Daniel Dunbard067f7f2009-04-08 23:54:23 +00003345 }
Daniel Dunbarc1964212009-03-26 16:23:12 +00003346 } else {
3347 if (Args.hasArg(options::OPT_static) ||
3348 Args.hasArg(options::OPT_object) ||
3349 Args.hasArg(options::OPT_preload)) {
3350 CmdArgs.push_back("-lcrt0.o");
3351 } else {
3352 // Derived from darwin_crt1 spec.
Daniel Dunbarebc34df2011-03-31 17:12:33 +00003353 if (getDarwinToolChain().isTargetIOSSimulator()) {
3354 // The simulator doesn't have a versioned crt1 file.
3355 CmdArgs.push_back("-lcrt1.o");
3356 } else if (getDarwinToolChain().isTargetIPhoneOS()) {
Daniel Dunbar83608032010-01-27 00:56:56 +00003357 if (getDarwinToolChain().isIPhoneOSVersionLT(3, 1))
3358 CmdArgs.push_back("-lcrt1.o");
3359 else
3360 CmdArgs.push_back("-lcrt1.3.1.o");
Daniel Dunbar6d23b2f2010-01-27 00:57:03 +00003361 } else {
3362 if (getDarwinToolChain().isMacosxVersionLT(10, 5))
3363 CmdArgs.push_back("-lcrt1.o");
3364 else if (getDarwinToolChain().isMacosxVersionLT(10, 6))
3365 CmdArgs.push_back("-lcrt1.10.5.o");
3366 else
3367 CmdArgs.push_back("-lcrt1.10.6.o");
Daniel Dunbard067f7f2009-04-08 23:54:23 +00003368
Daniel Dunbar6d23b2f2010-01-27 00:57:03 +00003369 // darwin_crt2 spec is empty.
3370 }
Daniel Dunbarc1964212009-03-26 16:23:12 +00003371 }
3372 }
3373 }
3374 }
3375
Daniel Dunbar6d23b2f2010-01-27 00:57:03 +00003376 if (!getDarwinToolChain().isTargetIPhoneOS() &&
3377 Args.hasArg(options::OPT_shared_libgcc) &&
3378 getDarwinToolChain().isMacosxVersionLT(10, 5)) {
Daniel Dunbar1c28f1e2009-09-09 22:32:48 +00003379 const char *Str =
Daniel Dunbar9c3ed5f2010-07-14 18:46:23 +00003380 Args.MakeArgString(getToolChain().GetFilePath("crt3.o"));
Daniel Dunbar1c28f1e2009-09-09 22:32:48 +00003381 CmdArgs.push_back(Str);
Daniel Dunbarc1964212009-03-26 16:23:12 +00003382 }
3383 }
3384
3385 Args.AddAllArgs(CmdArgs, options::OPT_L);
Daniel Dunbard067f7f2009-04-08 23:54:23 +00003386
Daniel Dunbarc1964212009-03-26 16:23:12 +00003387 if (Args.hasArg(options::OPT_fopenmp))
3388 // This is more complicated in gcc...
3389 CmdArgs.push_back("-lgomp");
3390
Daniel Dunbar4c30b892009-09-18 08:14:36 +00003391 getDarwinToolChain().AddLinkSearchPathArgs(Args, CmdArgs);
Daniel Dunbard067f7f2009-04-08 23:54:23 +00003392
John McCall31168b02011-06-15 23:02:42 +00003393 // In ARC, if we don't have runtime support, link in the runtime
3394 // stubs. We have to do this *before* adding any of the normal
3395 // linker inputs so that its initializer gets run first.
John McCall24fc0de2011-07-06 00:26:06 +00003396 if (isObjCAutoRefCount(Args)) {
3397 ObjCRuntime runtime;
3398 getDarwinToolChain().configureObjCRuntime(runtime);
3399 if (!runtime.HasARC)
3400 getDarwinToolChain().AddLinkARCArgs(Args, CmdArgs);
3401 }
John McCall31168b02011-06-15 23:02:42 +00003402
Daniel Dunbar54423b22010-09-17 00:24:54 +00003403 AddLinkerInputs(getToolChain(), Inputs, Args, CmdArgs);
Daniel Dunbarc1964212009-03-26 16:23:12 +00003404
3405 if (LinkingOutput) {
3406 CmdArgs.push_back("-arch_multiple");
3407 CmdArgs.push_back("-final_output");
3408 CmdArgs.push_back(LinkingOutput);
3409 }
3410
Daniel Dunbarc1964212009-03-26 16:23:12 +00003411 if (Args.hasArg(options::OPT_fnested_functions))
3412 CmdArgs.push_back("-allow_stack_execute");
Daniel Dunbard067f7f2009-04-08 23:54:23 +00003413
Daniel Dunbarc1964212009-03-26 16:23:12 +00003414 if (!Args.hasArg(options::OPT_nostdlib) &&
3415 !Args.hasArg(options::OPT_nodefaultlibs)) {
Daniel Dunbar083edf72009-12-21 18:54:17 +00003416 if (getToolChain().getDriver().CCCIsCXX)
Daniel Dunbar3f7796f2010-09-17 01:20:05 +00003417 getToolChain().AddCXXStdlibLibArgs(Args, CmdArgs);
Daniel Dunbarad0f62b2009-04-08 06:06:21 +00003418
Daniel Dunbarc1964212009-03-26 16:23:12 +00003419 // link_ssp spec is empty.
3420
Daniel Dunbar26d482a2009-09-18 08:15:03 +00003421 // Let the tool chain choose which runtime library to link.
3422 getDarwinToolChain().AddLinkRuntimeLibArgs(Args, CmdArgs);
Daniel Dunbarc1964212009-03-26 16:23:12 +00003423 }
3424
3425 if (!Args.hasArg(options::OPT_A) &&
3426 !Args.hasArg(options::OPT_nostdlib) &&
3427 !Args.hasArg(options::OPT_nostartfiles)) {
3428 // endfile_spec is empty.
3429 }
3430
Bill Wendling08760582011-06-27 19:15:03 +00003431 addProfileRT(getToolChain(), Args, CmdArgs, getToolChain().getTriple());
Nick Lewycky82fe5f42011-05-24 21:54:59 +00003432
Daniel Dunbarc1964212009-03-26 16:23:12 +00003433 Args.AddAllArgs(CmdArgs, options::OPT_T_Group);
3434 Args.AddAllArgs(CmdArgs, options::OPT_F);
3435
Daniel Dunbard067f7f2009-04-08 23:54:23 +00003436 const char *Exec =
Daniel Dunbar9c3ed5f2010-07-14 18:46:23 +00003437 Args.MakeArgString(getToolChain().GetProgramPath("ld"));
Daniel Dunbar7fbaf532010-08-02 02:38:28 +00003438 C.addCommand(new Command(JA, *this, Exec, CmdArgs));
Daniel Dunbarc1964212009-03-26 16:23:12 +00003439}
3440
Daniel Dunbar64ed5e32009-03-20 00:52:38 +00003441void darwin::Lipo::ConstructJob(Compilation &C, const JobAction &JA,
Daniel Dunbar7fbaf532010-08-02 02:38:28 +00003442 const InputInfo &Output,
Daniel Dunbard067f7f2009-04-08 23:54:23 +00003443 const InputInfoList &Inputs,
3444 const ArgList &Args,
Daniel Dunbar64ed5e32009-03-20 00:52:38 +00003445 const char *LinkingOutput) const {
3446 ArgStringList CmdArgs;
3447
3448 CmdArgs.push_back("-create");
3449 assert(Output.isFilename() && "Unexpected lipo output.");
Daniel Dunbar06686ab2009-03-24 00:24:37 +00003450
3451 CmdArgs.push_back("-output");
Daniel Dunbar64ed5e32009-03-20 00:52:38 +00003452 CmdArgs.push_back(Output.getFilename());
Daniel Dunbar06686ab2009-03-24 00:24:37 +00003453
Daniel Dunbar64ed5e32009-03-20 00:52:38 +00003454 for (InputInfoList::const_iterator
3455 it = Inputs.begin(), ie = Inputs.end(); it != ie; ++it) {
3456 const InputInfo &II = *it;
3457 assert(II.isFilename() && "Unexpected lipo input.");
3458 CmdArgs.push_back(II.getFilename());
3459 }
Daniel Dunbard067f7f2009-04-08 23:54:23 +00003460 const char *Exec =
Daniel Dunbar9c3ed5f2010-07-14 18:46:23 +00003461 Args.MakeArgString(getToolChain().GetProgramPath("lipo"));
Daniel Dunbar7fbaf532010-08-02 02:38:28 +00003462 C.addCommand(new Command(JA, *this, Exec, CmdArgs));
Daniel Dunbar64ed5e32009-03-20 00:52:38 +00003463}
Daniel Dunbar8eb473c2009-03-31 17:45:15 +00003464
Daniel Dunbar88299622010-06-04 18:28:36 +00003465void darwin::Dsymutil::ConstructJob(Compilation &C, const JobAction &JA,
Daniel Dunbar7fbaf532010-08-02 02:38:28 +00003466 const InputInfo &Output,
Daniel Dunbar88299622010-06-04 18:28:36 +00003467 const InputInfoList &Inputs,
3468 const ArgList &Args,
3469 const char *LinkingOutput) const {
3470 ArgStringList CmdArgs;
3471
Daniel Dunbareb86b042011-05-09 17:23:16 +00003472 CmdArgs.push_back("-o");
3473 CmdArgs.push_back(Output.getFilename());
3474
Daniel Dunbar88299622010-06-04 18:28:36 +00003475 assert(Inputs.size() == 1 && "Unable to handle multiple inputs.");
3476 const InputInfo &Input = Inputs[0];
3477 assert(Input.isFilename() && "Unexpected dsymutil input.");
3478 CmdArgs.push_back(Input.getFilename());
3479
Daniel Dunbar88299622010-06-04 18:28:36 +00003480 const char *Exec =
Daniel Dunbar9c3ed5f2010-07-14 18:46:23 +00003481 Args.MakeArgString(getToolChain().GetProgramPath("dsymutil"));
Daniel Dunbar7fbaf532010-08-02 02:38:28 +00003482 C.addCommand(new Command(JA, *this, Exec, CmdArgs));
Daniel Dunbar88299622010-06-04 18:28:36 +00003483}
3484
Edward O'Callaghan856e4ff2009-08-22 01:06:46 +00003485void auroraux::Assemble::ConstructJob(Compilation &C, const JobAction &JA,
Daniel Dunbar7fbaf532010-08-02 02:38:28 +00003486 const InputInfo &Output,
Daniel Dunbar1c7577c2009-11-04 06:24:38 +00003487 const InputInfoList &Inputs,
3488 const ArgList &Args,
3489 const char *LinkingOutput) const {
Edward O'Callaghan856e4ff2009-08-22 01:06:46 +00003490 ArgStringList CmdArgs;
3491
3492 Args.AddAllArgValues(CmdArgs, options::OPT_Wa_COMMA,
3493 options::OPT_Xassembler);
3494
3495 CmdArgs.push_back("-o");
Daniel Dunbarb440f562010-08-02 02:38:21 +00003496 CmdArgs.push_back(Output.getFilename());
Edward O'Callaghan856e4ff2009-08-22 01:06:46 +00003497
3498 for (InputInfoList::const_iterator
3499 it = Inputs.begin(), ie = Inputs.end(); it != ie; ++it) {
3500 const InputInfo &II = *it;
Daniel Dunbarb440f562010-08-02 02:38:21 +00003501 CmdArgs.push_back(II.getFilename());
Edward O'Callaghan856e4ff2009-08-22 01:06:46 +00003502 }
3503
3504 const char *Exec =
Daniel Dunbar9c3ed5f2010-07-14 18:46:23 +00003505 Args.MakeArgString(getToolChain().GetProgramPath("gas"));
Daniel Dunbar7fbaf532010-08-02 02:38:28 +00003506 C.addCommand(new Command(JA, *this, Exec, CmdArgs));
Edward O'Callaghan856e4ff2009-08-22 01:06:46 +00003507}
3508
3509void auroraux::Link::ConstructJob(Compilation &C, const JobAction &JA,
Daniel Dunbar7fbaf532010-08-02 02:38:28 +00003510 const InputInfo &Output,
Daniel Dunbar1c7577c2009-11-04 06:24:38 +00003511 const InputInfoList &Inputs,
3512 const ArgList &Args,
3513 const char *LinkingOutput) const {
Edward O'Callaghan856e4ff2009-08-22 01:06:46 +00003514 ArgStringList CmdArgs;
3515
3516 if ((!Args.hasArg(options::OPT_nostdlib)) &&
Daniel Dunbar1c7577c2009-11-04 06:24:38 +00003517 (!Args.hasArg(options::OPT_shared))) {
Edward O'Callaghan856e4ff2009-08-22 01:06:46 +00003518 CmdArgs.push_back("-e");
Edward O'Callaghand8712d92009-10-15 07:44:07 +00003519 CmdArgs.push_back("_start");
Edward O'Callaghan856e4ff2009-08-22 01:06:46 +00003520 }
3521
3522 if (Args.hasArg(options::OPT_static)) {
3523 CmdArgs.push_back("-Bstatic");
Edward O'Callaghand8712d92009-10-15 07:44:07 +00003524 CmdArgs.push_back("-dn");
Edward O'Callaghan856e4ff2009-08-22 01:06:46 +00003525 } else {
Edward O'Callaghand8712d92009-10-15 07:44:07 +00003526// CmdArgs.push_back("--eh-frame-hdr");
Edward O'Callaghan856e4ff2009-08-22 01:06:46 +00003527 CmdArgs.push_back("-Bdynamic");
3528 if (Args.hasArg(options::OPT_shared)) {
3529 CmdArgs.push_back("-shared");
3530 } else {
Edward O'Callaghan7d3c2752009-10-16 19:44:18 +00003531 CmdArgs.push_back("--dynamic-linker");
Edward O'Callaghan856e4ff2009-08-22 01:06:46 +00003532 CmdArgs.push_back("/lib/ld.so.1"); // 64Bit Path /lib/amd64/ld.so.1
3533 }
3534 }
3535
Daniel Dunbarb440f562010-08-02 02:38:21 +00003536 if (Output.isFilename()) {
Edward O'Callaghan856e4ff2009-08-22 01:06:46 +00003537 CmdArgs.push_back("-o");
3538 CmdArgs.push_back(Output.getFilename());
3539 } else {
3540 assert(Output.isNothing() && "Invalid output.");
3541 }
3542
3543 if (!Args.hasArg(options::OPT_nostdlib) &&
3544 !Args.hasArg(options::OPT_nostartfiles)) {
3545 if (!Args.hasArg(options::OPT_shared)) {
Chris Lattner3e2ee142010-07-07 16:01:42 +00003546 CmdArgs.push_back(Args.MakeArgString(
Daniel Dunbar9c3ed5f2010-07-14 18:46:23 +00003547 getToolChain().GetFilePath("crt1.o")));
Chris Lattner3e2ee142010-07-07 16:01:42 +00003548 CmdArgs.push_back(Args.MakeArgString(
Daniel Dunbar9c3ed5f2010-07-14 18:46:23 +00003549 getToolChain().GetFilePath("crti.o")));
Chris Lattner3e2ee142010-07-07 16:01:42 +00003550 CmdArgs.push_back(Args.MakeArgString(
Daniel Dunbar9c3ed5f2010-07-14 18:46:23 +00003551 getToolChain().GetFilePath("crtbegin.o")));
Edward O'Callaghan856e4ff2009-08-22 01:06:46 +00003552 } else {
Chris Lattner3e2ee142010-07-07 16:01:42 +00003553 CmdArgs.push_back(Args.MakeArgString(
Daniel Dunbar9c3ed5f2010-07-14 18:46:23 +00003554 getToolChain().GetFilePath("crti.o")));
Edward O'Callaghan856e4ff2009-08-22 01:06:46 +00003555 }
Chris Lattner3e2ee142010-07-07 16:01:42 +00003556 CmdArgs.push_back(Args.MakeArgString(
Daniel Dunbar9c3ed5f2010-07-14 18:46:23 +00003557 getToolChain().GetFilePath("crtn.o")));
Edward O'Callaghan856e4ff2009-08-22 01:06:46 +00003558 }
3559
Daniel Dunbar1c7577c2009-11-04 06:24:38 +00003560 CmdArgs.push_back(Args.MakeArgString("-L/opt/gcc4/lib/gcc/"
3561 + getToolChain().getTripleString()
Daniel Dunbarb0b18612009-10-29 02:24:37 +00003562 + "/4.2.4"));
Edward O'Callaghan856e4ff2009-08-22 01:06:46 +00003563
3564 Args.AddAllArgs(CmdArgs, options::OPT_L);
3565 Args.AddAllArgs(CmdArgs, options::OPT_T_Group);
3566 Args.AddAllArgs(CmdArgs, options::OPT_e);
3567
Daniel Dunbar54423b22010-09-17 00:24:54 +00003568 AddLinkerInputs(getToolChain(), Inputs, Args, CmdArgs);
Edward O'Callaghan856e4ff2009-08-22 01:06:46 +00003569
3570 if (!Args.hasArg(options::OPT_nostdlib) &&
3571 !Args.hasArg(options::OPT_nodefaultlibs)) {
3572 // FIXME: For some reason GCC passes -lgcc before adding
3573 // the default system libraries. Just mimic this for now.
3574 CmdArgs.push_back("-lgcc");
3575
3576 if (Args.hasArg(options::OPT_pthread))
3577 CmdArgs.push_back("-pthread");
3578 if (!Args.hasArg(options::OPT_shared))
3579 CmdArgs.push_back("-lc");
3580 CmdArgs.push_back("-lgcc");
3581 }
3582
3583 if (!Args.hasArg(options::OPT_nostdlib) &&
3584 !Args.hasArg(options::OPT_nostartfiles)) {
3585 if (!Args.hasArg(options::OPT_shared))
Chris Lattner3e2ee142010-07-07 16:01:42 +00003586 CmdArgs.push_back(Args.MakeArgString(
Daniel Dunbar9c3ed5f2010-07-14 18:46:23 +00003587 getToolChain().GetFilePath("crtend.o")));
Edward O'Callaghan856e4ff2009-08-22 01:06:46 +00003588 }
3589
Bill Wendling08760582011-06-27 19:15:03 +00003590 addProfileRT(getToolChain(), Args, CmdArgs, getToolChain().getTriple());
Nick Lewycky82fe5f42011-05-24 21:54:59 +00003591
Edward O'Callaghan856e4ff2009-08-22 01:06:46 +00003592 const char *Exec =
Daniel Dunbar9c3ed5f2010-07-14 18:46:23 +00003593 Args.MakeArgString(getToolChain().GetProgramPath("ld"));
Daniel Dunbar7fbaf532010-08-02 02:38:28 +00003594 C.addCommand(new Command(JA, *this, Exec, CmdArgs));
Edward O'Callaghan856e4ff2009-08-22 01:06:46 +00003595}
3596
Daniel Dunbar10de9e62009-06-29 20:52:51 +00003597void openbsd::Assemble::ConstructJob(Compilation &C, const JobAction &JA,
Daniel Dunbar7fbaf532010-08-02 02:38:28 +00003598 const InputInfo &Output,
Daniel Dunbar10de9e62009-06-29 20:52:51 +00003599 const InputInfoList &Inputs,
3600 const ArgList &Args,
Mike Stump11289f42009-09-09 15:08:12 +00003601 const char *LinkingOutput) const {
Daniel Dunbar10de9e62009-06-29 20:52:51 +00003602 ArgStringList CmdArgs;
3603
3604 Args.AddAllArgValues(CmdArgs, options::OPT_Wa_COMMA,
3605 options::OPT_Xassembler);
3606
3607 CmdArgs.push_back("-o");
Daniel Dunbarb440f562010-08-02 02:38:21 +00003608 CmdArgs.push_back(Output.getFilename());
Daniel Dunbar10de9e62009-06-29 20:52:51 +00003609
3610 for (InputInfoList::const_iterator
3611 it = Inputs.begin(), ie = Inputs.end(); it != ie; ++it) {
3612 const InputInfo &II = *it;
Daniel Dunbarb440f562010-08-02 02:38:21 +00003613 CmdArgs.push_back(II.getFilename());
Daniel Dunbar10de9e62009-06-29 20:52:51 +00003614 }
3615
3616 const char *Exec =
Daniel Dunbar9c3ed5f2010-07-14 18:46:23 +00003617 Args.MakeArgString(getToolChain().GetProgramPath("as"));
Daniel Dunbar7fbaf532010-08-02 02:38:28 +00003618 C.addCommand(new Command(JA, *this, Exec, CmdArgs));
Daniel Dunbar10de9e62009-06-29 20:52:51 +00003619}
3620
3621void openbsd::Link::ConstructJob(Compilation &C, const JobAction &JA,
Daniel Dunbar7fbaf532010-08-02 02:38:28 +00003622 const InputInfo &Output,
Daniel Dunbar10de9e62009-06-29 20:52:51 +00003623 const InputInfoList &Inputs,
3624 const ArgList &Args,
3625 const char *LinkingOutput) const {
Daniel Dunbar083edf72009-12-21 18:54:17 +00003626 const Driver &D = getToolChain().getDriver();
Daniel Dunbar10de9e62009-06-29 20:52:51 +00003627 ArgStringList CmdArgs;
3628
Daniel Dunbara8888ac2009-08-03 01:28:59 +00003629 if ((!Args.hasArg(options::OPT_nostdlib)) &&
Daniel Dunbar1c7577c2009-11-04 06:24:38 +00003630 (!Args.hasArg(options::OPT_shared))) {
Daniel Dunbara8888ac2009-08-03 01:28:59 +00003631 CmdArgs.push_back("-e");
3632 CmdArgs.push_back("__start");
3633 }
3634
Daniel Dunbar10de9e62009-06-29 20:52:51 +00003635 if (Args.hasArg(options::OPT_static)) {
3636 CmdArgs.push_back("-Bstatic");
3637 } else {
Rafael Espindola7ba97af2010-11-11 02:17:51 +00003638 if (Args.hasArg(options::OPT_rdynamic))
3639 CmdArgs.push_back("-export-dynamic");
Daniel Dunbar10de9e62009-06-29 20:52:51 +00003640 CmdArgs.push_back("--eh-frame-hdr");
Daniel Dunbara8888ac2009-08-03 01:28:59 +00003641 CmdArgs.push_back("-Bdynamic");
Daniel Dunbar10de9e62009-06-29 20:52:51 +00003642 if (Args.hasArg(options::OPT_shared)) {
Daniel Dunbara8888ac2009-08-03 01:28:59 +00003643 CmdArgs.push_back("-shared");
Daniel Dunbar10de9e62009-06-29 20:52:51 +00003644 } else {
3645 CmdArgs.push_back("-dynamic-linker");
3646 CmdArgs.push_back("/usr/libexec/ld.so");
3647 }
3648 }
3649
Daniel Dunbarb440f562010-08-02 02:38:21 +00003650 if (Output.isFilename()) {
Daniel Dunbar10de9e62009-06-29 20:52:51 +00003651 CmdArgs.push_back("-o");
3652 CmdArgs.push_back(Output.getFilename());
3653 } else {
3654 assert(Output.isNothing() && "Invalid output.");
3655 }
3656
3657 if (!Args.hasArg(options::OPT_nostdlib) &&
3658 !Args.hasArg(options::OPT_nostartfiles)) {
3659 if (!Args.hasArg(options::OPT_shared)) {
Chris Lattner3e2ee142010-07-07 16:01:42 +00003660 CmdArgs.push_back(Args.MakeArgString(
Daniel Dunbar9c3ed5f2010-07-14 18:46:23 +00003661 getToolChain().GetFilePath("crt0.o")));
Chris Lattner3e2ee142010-07-07 16:01:42 +00003662 CmdArgs.push_back(Args.MakeArgString(
Daniel Dunbar9c3ed5f2010-07-14 18:46:23 +00003663 getToolChain().GetFilePath("crtbegin.o")));
Daniel Dunbar10de9e62009-06-29 20:52:51 +00003664 } else {
Chris Lattner3e2ee142010-07-07 16:01:42 +00003665 CmdArgs.push_back(Args.MakeArgString(
Daniel Dunbar9c3ed5f2010-07-14 18:46:23 +00003666 getToolChain().GetFilePath("crtbeginS.o")));
Daniel Dunbar10de9e62009-06-29 20:52:51 +00003667 }
3668 }
3669
Edward O'Callaghan5c521462009-10-28 15:13:08 +00003670 std::string Triple = getToolChain().getTripleString();
3671 if (Triple.substr(0, 6) == "x86_64")
Daniel Dunbar1c7577c2009-11-04 06:24:38 +00003672 Triple.replace(0, 6, "amd64");
Daniel Dunbarb0b18612009-10-29 02:24:37 +00003673 CmdArgs.push_back(Args.MakeArgString("-L/usr/lib/gcc-lib/" + Triple +
Daniel Dunbarea3813f2010-08-01 23:13:54 +00003674 "/4.2.1"));
Daniel Dunbara8888ac2009-08-03 01:28:59 +00003675
Daniel Dunbar10de9e62009-06-29 20:52:51 +00003676 Args.AddAllArgs(CmdArgs, options::OPT_L);
3677 Args.AddAllArgs(CmdArgs, options::OPT_T_Group);
3678 Args.AddAllArgs(CmdArgs, options::OPT_e);
3679
Daniel Dunbar54423b22010-09-17 00:24:54 +00003680 AddLinkerInputs(getToolChain(), Inputs, Args, CmdArgs);
Daniel Dunbar10de9e62009-06-29 20:52:51 +00003681
3682 if (!Args.hasArg(options::OPT_nostdlib) &&
3683 !Args.hasArg(options::OPT_nodefaultlibs)) {
Daniel Dunbarea3813f2010-08-01 23:13:54 +00003684 if (D.CCCIsCXX) {
Daniel Dunbar3f7796f2010-09-17 01:20:05 +00003685 getToolChain().AddCXXStdlibLibArgs(Args, CmdArgs);
Daniel Dunbarea3813f2010-08-01 23:13:54 +00003686 CmdArgs.push_back("-lm");
3687 }
3688
Daniel Dunbara8888ac2009-08-03 01:28:59 +00003689 // FIXME: For some reason GCC passes -lgcc before adding
3690 // the default system libraries. Just mimic this for now.
3691 CmdArgs.push_back("-lgcc");
Daniel Dunbar10de9e62009-06-29 20:52:51 +00003692
3693 if (Args.hasArg(options::OPT_pthread))
Chris Lattnerd0257f72011-02-21 18:36:51 +00003694 CmdArgs.push_back("-lpthread");
Daniel Dunbara8888ac2009-08-03 01:28:59 +00003695 if (!Args.hasArg(options::OPT_shared))
3696 CmdArgs.push_back("-lc");
3697 CmdArgs.push_back("-lgcc");
Daniel Dunbar10de9e62009-06-29 20:52:51 +00003698 }
3699
3700 if (!Args.hasArg(options::OPT_nostdlib) &&
3701 !Args.hasArg(options::OPT_nostartfiles)) {
3702 if (!Args.hasArg(options::OPT_shared))
Chris Lattner3e2ee142010-07-07 16:01:42 +00003703 CmdArgs.push_back(Args.MakeArgString(
Daniel Dunbar9c3ed5f2010-07-14 18:46:23 +00003704 getToolChain().GetFilePath("crtend.o")));
Daniel Dunbar10de9e62009-06-29 20:52:51 +00003705 else
Chris Lattner3e2ee142010-07-07 16:01:42 +00003706 CmdArgs.push_back(Args.MakeArgString(
Daniel Dunbar9c3ed5f2010-07-14 18:46:23 +00003707 getToolChain().GetFilePath("crtendS.o")));
Daniel Dunbar10de9e62009-06-29 20:52:51 +00003708 }
3709
3710 const char *Exec =
Daniel Dunbar9c3ed5f2010-07-14 18:46:23 +00003711 Args.MakeArgString(getToolChain().GetProgramPath("ld"));
Daniel Dunbar7fbaf532010-08-02 02:38:28 +00003712 C.addCommand(new Command(JA, *this, Exec, CmdArgs));
Daniel Dunbar10de9e62009-06-29 20:52:51 +00003713}
Ed Schoutene33194b2009-04-02 19:13:12 +00003714
Daniel Dunbar8eb473c2009-03-31 17:45:15 +00003715void freebsd::Assemble::ConstructJob(Compilation &C, const JobAction &JA,
Daniel Dunbar7fbaf532010-08-02 02:38:28 +00003716 const InputInfo &Output,
Daniel Dunbard067f7f2009-04-08 23:54:23 +00003717 const InputInfoList &Inputs,
3718 const ArgList &Args,
Mike Stump11289f42009-09-09 15:08:12 +00003719 const char *LinkingOutput) const {
Daniel Dunbar8eb473c2009-03-31 17:45:15 +00003720 ArgStringList CmdArgs;
3721
Daniel Dunbard854c8d2009-04-01 19:36:32 +00003722 // When building 32-bit code on FreeBSD/amd64, we have to explicitly
3723 // instruct as in the base system to assemble 32-bit code.
3724 if (getToolChain().getArchName() == "i386")
Daniel Dunbar8eb473c2009-03-31 17:45:15 +00003725 CmdArgs.push_back("--32");
3726
Roman Divacky00859c22011-06-04 07:37:31 +00003727 if (getToolChain().getArchName() == "powerpc")
3728 CmdArgs.push_back("-a32");
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00003729
Eric Christopher0b26a612010-03-02 02:41:08 +00003730 // Set byte order explicitly
3731 if (getToolChain().getArchName() == "mips")
3732 CmdArgs.push_back("-EB");
3733 else if (getToolChain().getArchName() == "mipsel")
3734 CmdArgs.push_back("-EL");
3735
Daniel Dunbar8eb473c2009-03-31 17:45:15 +00003736 Args.AddAllArgValues(CmdArgs, options::OPT_Wa_COMMA,
3737 options::OPT_Xassembler);
3738
3739 CmdArgs.push_back("-o");
Daniel Dunbarb440f562010-08-02 02:38:21 +00003740 CmdArgs.push_back(Output.getFilename());
Daniel Dunbar8eb473c2009-03-31 17:45:15 +00003741
3742 for (InputInfoList::const_iterator
3743 it = Inputs.begin(), ie = Inputs.end(); it != ie; ++it) {
3744 const InputInfo &II = *it;
Daniel Dunbarb440f562010-08-02 02:38:21 +00003745 CmdArgs.push_back(II.getFilename());
Daniel Dunbar8eb473c2009-03-31 17:45:15 +00003746 }
3747
Daniel Dunbard067f7f2009-04-08 23:54:23 +00003748 const char *Exec =
Daniel Dunbar9c3ed5f2010-07-14 18:46:23 +00003749 Args.MakeArgString(getToolChain().GetProgramPath("as"));
Daniel Dunbar7fbaf532010-08-02 02:38:28 +00003750 C.addCommand(new Command(JA, *this, Exec, CmdArgs));
Daniel Dunbar8eb473c2009-03-31 17:45:15 +00003751}
Daniel Dunbard854c8d2009-04-01 19:36:32 +00003752
3753void freebsd::Link::ConstructJob(Compilation &C, const JobAction &JA,
Daniel Dunbar7fbaf532010-08-02 02:38:28 +00003754 const InputInfo &Output,
Daniel Dunbard067f7f2009-04-08 23:54:23 +00003755 const InputInfoList &Inputs,
3756 const ArgList &Args,
Daniel Dunbare3e263f2009-05-02 20:14:53 +00003757 const char *LinkingOutput) const {
Daniel Dunbar083edf72009-12-21 18:54:17 +00003758 const Driver &D = getToolChain().getDriver();
Daniel Dunbard854c8d2009-04-01 19:36:32 +00003759 ArgStringList CmdArgs;
3760
Joerg Sonnenberger6165ab12011-03-21 13:51:29 +00003761 if (!D.SysRoot.empty())
3762 CmdArgs.push_back(Args.MakeArgString("--sysroot=" + D.SysRoot));
3763
Daniel Dunbard854c8d2009-04-01 19:36:32 +00003764 if (Args.hasArg(options::OPT_static)) {
3765 CmdArgs.push_back("-Bstatic");
3766 } else {
Rafael Espindola7ba97af2010-11-11 02:17:51 +00003767 if (Args.hasArg(options::OPT_rdynamic))
3768 CmdArgs.push_back("-export-dynamic");
Daniel Dunbard854c8d2009-04-01 19:36:32 +00003769 CmdArgs.push_back("--eh-frame-hdr");
3770 if (Args.hasArg(options::OPT_shared)) {
3771 CmdArgs.push_back("-Bshareable");
3772 } else {
3773 CmdArgs.push_back("-dynamic-linker");
3774 CmdArgs.push_back("/libexec/ld-elf.so.1");
3775 }
3776 }
3777
3778 // When building 32-bit code on FreeBSD/amd64, we have to explicitly
3779 // instruct ld in the base system to link 32-bit code.
3780 if (getToolChain().getArchName() == "i386") {
3781 CmdArgs.push_back("-m");
3782 CmdArgs.push_back("elf_i386_fbsd");
3783 }
3784
Roman Divacky5e300b82011-06-04 07:40:24 +00003785 if (getToolChain().getArchName() == "powerpc") {
3786 CmdArgs.push_back("-m");
3787 CmdArgs.push_back("elf32ppc");
3788 }
3789
Daniel Dunbarb440f562010-08-02 02:38:21 +00003790 if (Output.isFilename()) {
Daniel Dunbard854c8d2009-04-01 19:36:32 +00003791 CmdArgs.push_back("-o");
3792 CmdArgs.push_back(Output.getFilename());
3793 } else {
3794 assert(Output.isNothing() && "Invalid output.");
3795 }
3796
3797 if (!Args.hasArg(options::OPT_nostdlib) &&
3798 !Args.hasArg(options::OPT_nostartfiles)) {
3799 if (!Args.hasArg(options::OPT_shared)) {
Roman Divacky66f22762011-02-10 16:59:40 +00003800 if (Args.hasArg(options::OPT_pg))
3801 CmdArgs.push_back(Args.MakeArgString(
3802 getToolChain().GetFilePath("gcrt1.o")));
3803 else
3804 CmdArgs.push_back(Args.MakeArgString(
3805 getToolChain().GetFilePath("crt1.o")));
Chris Lattner3e2ee142010-07-07 16:01:42 +00003806 CmdArgs.push_back(Args.MakeArgString(
Daniel Dunbar9c3ed5f2010-07-14 18:46:23 +00003807 getToolChain().GetFilePath("crti.o")));
Chris Lattner3e2ee142010-07-07 16:01:42 +00003808 CmdArgs.push_back(Args.MakeArgString(
Daniel Dunbar9c3ed5f2010-07-14 18:46:23 +00003809 getToolChain().GetFilePath("crtbegin.o")));
Daniel Dunbard854c8d2009-04-01 19:36:32 +00003810 } else {
Chris Lattner3e2ee142010-07-07 16:01:42 +00003811 CmdArgs.push_back(Args.MakeArgString(
Daniel Dunbar9c3ed5f2010-07-14 18:46:23 +00003812 getToolChain().GetFilePath("crti.o")));
Chris Lattner3e2ee142010-07-07 16:01:42 +00003813 CmdArgs.push_back(Args.MakeArgString(
Daniel Dunbar9c3ed5f2010-07-14 18:46:23 +00003814 getToolChain().GetFilePath("crtbeginS.o")));
Daniel Dunbard854c8d2009-04-01 19:36:32 +00003815 }
3816 }
3817
3818 Args.AddAllArgs(CmdArgs, options::OPT_L);
Roman Divackyee8188a2011-03-01 17:53:14 +00003819 const ToolChain::path_list Paths = getToolChain().getFilePaths();
3820 for (ToolChain::path_list::const_iterator i = Paths.begin(), e = Paths.end();
3821 i != e; ++i)
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003822 CmdArgs.push_back(Args.MakeArgString(StringRef("-L") + *i));
Daniel Dunbard854c8d2009-04-01 19:36:32 +00003823 Args.AddAllArgs(CmdArgs, options::OPT_T_Group);
3824 Args.AddAllArgs(CmdArgs, options::OPT_e);
David Chisnall589a4942010-08-15 22:58:12 +00003825 Args.AddAllArgs(CmdArgs, options::OPT_s);
3826 Args.AddAllArgs(CmdArgs, options::OPT_t);
3827 Args.AddAllArgs(CmdArgs, options::OPT_Z_Flag);
3828 Args.AddAllArgs(CmdArgs, options::OPT_r);
Daniel Dunbard854c8d2009-04-01 19:36:32 +00003829
Daniel Dunbar54423b22010-09-17 00:24:54 +00003830 AddLinkerInputs(getToolChain(), Inputs, Args, CmdArgs);
Daniel Dunbard854c8d2009-04-01 19:36:32 +00003831
3832 if (!Args.hasArg(options::OPT_nostdlib) &&
3833 !Args.hasArg(options::OPT_nodefaultlibs)) {
Daniel Dunbar4b8ef282010-02-17 08:07:51 +00003834 if (D.CCCIsCXX) {
Daniel Dunbar3f7796f2010-09-17 01:20:05 +00003835 getToolChain().AddCXXStdlibLibArgs(Args, CmdArgs);
Roman Divacky66f22762011-02-10 16:59:40 +00003836 if (Args.hasArg(options::OPT_pg))
3837 CmdArgs.push_back("-lm_p");
3838 else
3839 CmdArgs.push_back("-lm");
Daniel Dunbar4b8ef282010-02-17 08:07:51 +00003840 }
Daniel Dunbard854c8d2009-04-01 19:36:32 +00003841 // FIXME: For some reason GCC passes -lgcc and -lgcc_s before adding
3842 // the default system libraries. Just mimic this for now.
Roman Divacky66f22762011-02-10 16:59:40 +00003843 if (Args.hasArg(options::OPT_pg))
3844 CmdArgs.push_back("-lgcc_p");
3845 else
3846 CmdArgs.push_back("-lgcc");
Daniel Dunbard854c8d2009-04-01 19:36:32 +00003847 if (Args.hasArg(options::OPT_static)) {
3848 CmdArgs.push_back("-lgcc_eh");
Roman Divacky66f22762011-02-10 16:59:40 +00003849 } else if (Args.hasArg(options::OPT_pg)) {
3850 CmdArgs.push_back("-lgcc_eh_p");
Daniel Dunbard854c8d2009-04-01 19:36:32 +00003851 } else {
3852 CmdArgs.push_back("--as-needed");
3853 CmdArgs.push_back("-lgcc_s");
3854 CmdArgs.push_back("--no-as-needed");
3855 }
3856
Matt Beaumont-Gay1fe49152011-02-10 20:35:01 +00003857 if (Args.hasArg(options::OPT_pthread)) {
Roman Divacky66f22762011-02-10 16:59:40 +00003858 if (Args.hasArg(options::OPT_pg))
3859 CmdArgs.push_back("-lpthread_p");
3860 else
3861 CmdArgs.push_back("-lpthread");
Matt Beaumont-Gay1fe49152011-02-10 20:35:01 +00003862 }
Daniel Dunbard854c8d2009-04-01 19:36:32 +00003863
Roman Divacky66f22762011-02-10 16:59:40 +00003864 if (Args.hasArg(options::OPT_pg)) {
3865 if (Args.hasArg(options::OPT_shared))
3866 CmdArgs.push_back("-lc");
3867 else
3868 CmdArgs.push_back("-lc_p");
3869 CmdArgs.push_back("-lgcc_p");
3870 } else {
3871 CmdArgs.push_back("-lc");
3872 CmdArgs.push_back("-lgcc");
3873 }
3874
Daniel Dunbard854c8d2009-04-01 19:36:32 +00003875 if (Args.hasArg(options::OPT_static)) {
3876 CmdArgs.push_back("-lgcc_eh");
Roman Divacky66f22762011-02-10 16:59:40 +00003877 } else if (Args.hasArg(options::OPT_pg)) {
3878 CmdArgs.push_back("-lgcc_eh_p");
Daniel Dunbard854c8d2009-04-01 19:36:32 +00003879 } else {
3880 CmdArgs.push_back("--as-needed");
3881 CmdArgs.push_back("-lgcc_s");
3882 CmdArgs.push_back("--no-as-needed");
3883 }
3884 }
3885
3886 if (!Args.hasArg(options::OPT_nostdlib) &&
3887 !Args.hasArg(options::OPT_nostartfiles)) {
3888 if (!Args.hasArg(options::OPT_shared))
Daniel Dunbar9c3ed5f2010-07-14 18:46:23 +00003889 CmdArgs.push_back(Args.MakeArgString(getToolChain().GetFilePath(
Chris Lattner3e2ee142010-07-07 16:01:42 +00003890 "crtend.o")));
Daniel Dunbard854c8d2009-04-01 19:36:32 +00003891 else
Daniel Dunbar9c3ed5f2010-07-14 18:46:23 +00003892 CmdArgs.push_back(Args.MakeArgString(getToolChain().GetFilePath(
Chris Lattner3e2ee142010-07-07 16:01:42 +00003893 "crtendS.o")));
Daniel Dunbar9c3ed5f2010-07-14 18:46:23 +00003894 CmdArgs.push_back(Args.MakeArgString(getToolChain().GetFilePath(
Chris Lattner3e2ee142010-07-07 16:01:42 +00003895 "crtn.o")));
Daniel Dunbard854c8d2009-04-01 19:36:32 +00003896 }
3897
Bill Wendling08760582011-06-27 19:15:03 +00003898 addProfileRT(getToolChain(), Args, CmdArgs, getToolChain().getTriple());
Nick Lewycky82fe5f42011-05-24 21:54:59 +00003899
Daniel Dunbard067f7f2009-04-08 23:54:23 +00003900 const char *Exec =
Daniel Dunbar9c3ed5f2010-07-14 18:46:23 +00003901 Args.MakeArgString(getToolChain().GetProgramPath("ld"));
Daniel Dunbar7fbaf532010-08-02 02:38:28 +00003902 C.addCommand(new Command(JA, *this, Exec, CmdArgs));
Daniel Dunbard854c8d2009-04-01 19:36:32 +00003903}
Daniel Dunbarcc912342009-05-02 18:28:39 +00003904
Benjamin Kramer24f1d3e2011-02-02 18:59:27 +00003905void netbsd::Assemble::ConstructJob(Compilation &C, const JobAction &JA,
3906 const InputInfo &Output,
3907 const InputInfoList &Inputs,
3908 const ArgList &Args,
3909 const char *LinkingOutput) const {
3910 ArgStringList CmdArgs;
3911
3912 // When building 32-bit code on NetBSD/amd64, we have to explicitly
3913 // instruct as in the base system to assemble 32-bit code.
Joerg Sonnenberger637603a2011-05-16 13:35:02 +00003914 if (ToolTriple.getArch() == llvm::Triple::x86_64 &&
3915 getToolChain().getArch() == llvm::Triple::x86)
Benjamin Kramer24f1d3e2011-02-02 18:59:27 +00003916 CmdArgs.push_back("--32");
3917
3918
3919 // Set byte order explicitly
3920 if (getToolChain().getArchName() == "mips")
3921 CmdArgs.push_back("-EB");
3922 else if (getToolChain().getArchName() == "mipsel")
3923 CmdArgs.push_back("-EL");
3924
3925 Args.AddAllArgValues(CmdArgs, options::OPT_Wa_COMMA,
3926 options::OPT_Xassembler);
3927
3928 CmdArgs.push_back("-o");
3929 CmdArgs.push_back(Output.getFilename());
3930
3931 for (InputInfoList::const_iterator
3932 it = Inputs.begin(), ie = Inputs.end(); it != ie; ++it) {
3933 const InputInfo &II = *it;
3934 CmdArgs.push_back(II.getFilename());
3935 }
3936
Joerg Sonnenbergerfcc3ec92011-03-21 14:01:40 +00003937 const char *Exec = Args.MakeArgString(FindTargetProgramPath(getToolChain(),
Joerg Sonnenberger637603a2011-05-16 13:35:02 +00003938 ToolTriple.getTriple(),
3939 "as"));
Benjamin Kramer24f1d3e2011-02-02 18:59:27 +00003940 C.addCommand(new Command(JA, *this, Exec, CmdArgs));
3941}
3942
3943void netbsd::Link::ConstructJob(Compilation &C, const JobAction &JA,
3944 const InputInfo &Output,
3945 const InputInfoList &Inputs,
3946 const ArgList &Args,
3947 const char *LinkingOutput) const {
3948 const Driver &D = getToolChain().getDriver();
3949 ArgStringList CmdArgs;
3950
Joerg Sonnenberger6165ab12011-03-21 13:51:29 +00003951 if (!D.SysRoot.empty())
3952 CmdArgs.push_back(Args.MakeArgString("--sysroot=" + D.SysRoot));
3953
Benjamin Kramer24f1d3e2011-02-02 18:59:27 +00003954 if (Args.hasArg(options::OPT_static)) {
3955 CmdArgs.push_back("-Bstatic");
3956 } else {
3957 if (Args.hasArg(options::OPT_rdynamic))
3958 CmdArgs.push_back("-export-dynamic");
3959 CmdArgs.push_back("--eh-frame-hdr");
3960 if (Args.hasArg(options::OPT_shared)) {
3961 CmdArgs.push_back("-Bshareable");
3962 } else {
3963 CmdArgs.push_back("-dynamic-linker");
3964 CmdArgs.push_back("/libexec/ld.elf_so");
3965 }
3966 }
3967
3968 // When building 32-bit code on NetBSD/amd64, we have to explicitly
3969 // instruct ld in the base system to link 32-bit code.
Joerg Sonnenberger637603a2011-05-16 13:35:02 +00003970 if (ToolTriple.getArch() == llvm::Triple::x86_64 &&
3971 getToolChain().getArch() == llvm::Triple::x86) {
Benjamin Kramer24f1d3e2011-02-02 18:59:27 +00003972 CmdArgs.push_back("-m");
3973 CmdArgs.push_back("elf_i386");
3974 }
3975
3976 if (Output.isFilename()) {
3977 CmdArgs.push_back("-o");
3978 CmdArgs.push_back(Output.getFilename());
3979 } else {
3980 assert(Output.isNothing() && "Invalid output.");
3981 }
3982
3983 if (!Args.hasArg(options::OPT_nostdlib) &&
3984 !Args.hasArg(options::OPT_nostartfiles)) {
3985 if (!Args.hasArg(options::OPT_shared)) {
3986 CmdArgs.push_back(Args.MakeArgString(
3987 getToolChain().GetFilePath("crt0.o")));
3988 CmdArgs.push_back(Args.MakeArgString(
3989 getToolChain().GetFilePath("crti.o")));
3990 CmdArgs.push_back(Args.MakeArgString(
3991 getToolChain().GetFilePath("crtbegin.o")));
3992 } else {
3993 CmdArgs.push_back(Args.MakeArgString(
3994 getToolChain().GetFilePath("crti.o")));
3995 CmdArgs.push_back(Args.MakeArgString(
3996 getToolChain().GetFilePath("crtbeginS.o")));
3997 }
3998 }
3999
4000 Args.AddAllArgs(CmdArgs, options::OPT_L);
4001 Args.AddAllArgs(CmdArgs, options::OPT_T_Group);
4002 Args.AddAllArgs(CmdArgs, options::OPT_e);
4003 Args.AddAllArgs(CmdArgs, options::OPT_s);
4004 Args.AddAllArgs(CmdArgs, options::OPT_t);
4005 Args.AddAllArgs(CmdArgs, options::OPT_Z_Flag);
4006 Args.AddAllArgs(CmdArgs, options::OPT_r);
4007
4008 AddLinkerInputs(getToolChain(), Inputs, Args, CmdArgs);
4009
4010 if (!Args.hasArg(options::OPT_nostdlib) &&
4011 !Args.hasArg(options::OPT_nodefaultlibs)) {
4012 if (D.CCCIsCXX) {
4013 getToolChain().AddCXXStdlibLibArgs(Args, CmdArgs);
4014 CmdArgs.push_back("-lm");
4015 }
4016 // FIXME: For some reason GCC passes -lgcc and -lgcc_s before adding
4017 // the default system libraries. Just mimic this for now.
Benjamin Kramer24f1d3e2011-02-02 18:59:27 +00004018 if (Args.hasArg(options::OPT_static)) {
4019 CmdArgs.push_back("-lgcc_eh");
4020 } else {
4021 CmdArgs.push_back("--as-needed");
4022 CmdArgs.push_back("-lgcc_s");
4023 CmdArgs.push_back("--no-as-needed");
4024 }
Joerg Sonnenberger87717772011-06-07 23:39:17 +00004025 CmdArgs.push_back("-lgcc");
Benjamin Kramer24f1d3e2011-02-02 18:59:27 +00004026
4027 if (Args.hasArg(options::OPT_pthread))
4028 CmdArgs.push_back("-lpthread");
4029 CmdArgs.push_back("-lc");
4030
4031 CmdArgs.push_back("-lgcc");
4032 if (Args.hasArg(options::OPT_static)) {
4033 CmdArgs.push_back("-lgcc_eh");
4034 } else {
4035 CmdArgs.push_back("--as-needed");
4036 CmdArgs.push_back("-lgcc_s");
4037 CmdArgs.push_back("--no-as-needed");
4038 }
4039 }
4040
4041 if (!Args.hasArg(options::OPT_nostdlib) &&
4042 !Args.hasArg(options::OPT_nostartfiles)) {
4043 if (!Args.hasArg(options::OPT_shared))
4044 CmdArgs.push_back(Args.MakeArgString(getToolChain().GetFilePath(
4045 "crtend.o")));
4046 else
4047 CmdArgs.push_back(Args.MakeArgString(getToolChain().GetFilePath(
4048 "crtendS.o")));
4049 CmdArgs.push_back(Args.MakeArgString(getToolChain().GetFilePath(
4050 "crtn.o")));
4051 }
4052
Bill Wendling08760582011-06-27 19:15:03 +00004053 addProfileRT(getToolChain(), Args, CmdArgs, getToolChain().getTriple());
Nick Lewycky82fe5f42011-05-24 21:54:59 +00004054
Joerg Sonnenbergerfcc3ec92011-03-21 14:01:40 +00004055 const char *Exec = Args.MakeArgString(FindTargetProgramPath(getToolChain(),
Joerg Sonnenberger637603a2011-05-16 13:35:02 +00004056 ToolTriple.getTriple(),
4057 "ld"));
Benjamin Kramer24f1d3e2011-02-02 18:59:27 +00004058 C.addCommand(new Command(JA, *this, Exec, CmdArgs));
4059}
4060
Rafael Espindola92b00932010-08-10 00:25:48 +00004061void linuxtools::Assemble::ConstructJob(Compilation &C, const JobAction &JA,
4062 const InputInfo &Output,
4063 const InputInfoList &Inputs,
4064 const ArgList &Args,
4065 const char *LinkingOutput) const {
4066 ArgStringList CmdArgs;
4067
4068 // Add --32/--64 to make sure we get the format we want.
4069 // This is incomplete
4070 if (getToolChain().getArch() == llvm::Triple::x86) {
4071 CmdArgs.push_back("--32");
4072 } else if (getToolChain().getArch() == llvm::Triple::x86_64) {
4073 CmdArgs.push_back("--64");
4074 } else if (getToolChain().getArch() == llvm::Triple::arm) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00004075 StringRef MArch = getToolChain().getArchName();
Rafael Espindola92b00932010-08-10 00:25:48 +00004076 if (MArch == "armv7" || MArch == "armv7a" || MArch == "armv7-a")
4077 CmdArgs.push_back("-mfpu=neon");
4078 }
4079
4080 Args.AddAllArgValues(CmdArgs, options::OPT_Wa_COMMA,
4081 options::OPT_Xassembler);
4082
4083 CmdArgs.push_back("-o");
4084 CmdArgs.push_back(Output.getFilename());
4085
4086 for (InputInfoList::const_iterator
4087 it = Inputs.begin(), ie = Inputs.end(); it != ie; ++it) {
4088 const InputInfo &II = *it;
4089 CmdArgs.push_back(II.getFilename());
4090 }
4091
4092 const char *Exec =
4093 Args.MakeArgString(getToolChain().GetProgramPath("as"));
4094 C.addCommand(new Command(JA, *this, Exec, CmdArgs));
4095}
4096
Rafael Espindolac8f008f2010-11-07 20:14:31 +00004097void linuxtools::Link::ConstructJob(Compilation &C, const JobAction &JA,
4098 const InputInfo &Output,
4099 const InputInfoList &Inputs,
4100 const ArgList &Args,
4101 const char *LinkingOutput) const {
4102 const toolchains::Linux& ToolChain =
4103 static_cast<const toolchains::Linux&>(getToolChain());
4104 const Driver &D = ToolChain.getDriver();
4105 ArgStringList CmdArgs;
4106
Rafael Espindolad1002f62010-11-15 18:28:16 +00004107 // Silence warning for "clang -g foo.o -o foo"
4108 Args.ClaimAllArgs(options::OPT_g_Group);
Rafael Espindolad95a8122011-03-01 05:25:27 +00004109 // and "clang -emit-llvm foo.o -o foo"
4110 Args.ClaimAllArgs(options::OPT_emit_llvm);
Rafael Espindolaf92614c2010-11-17 20:37:10 +00004111 // and for "clang -g foo.o -o foo". Other warning options are already
4112 // handled somewhere else.
4113 Args.ClaimAllArgs(options::OPT_w);
Rafael Espindolad1002f62010-11-15 18:28:16 +00004114
Joerg Sonnenberger6165ab12011-03-21 13:51:29 +00004115 if (!D.SysRoot.empty())
4116 CmdArgs.push_back(Args.MakeArgString("--sysroot=" + D.SysRoot));
Rafael Espindolac8f008f2010-11-07 20:14:31 +00004117
Rafael Espindolad47ac232010-11-17 22:26:15 +00004118 if (Args.hasArg(options::OPT_pie))
4119 CmdArgs.push_back("-pie");
4120
Rafael Espindola1c76c592010-11-07 22:57:16 +00004121 if (Args.hasArg(options::OPT_rdynamic))
4122 CmdArgs.push_back("-export-dynamic");
4123
Rafael Espindola34d77dc2010-11-11 19:34:42 +00004124 if (Args.hasArg(options::OPT_s))
4125 CmdArgs.push_back("-s");
4126
Rafael Espindolac8f008f2010-11-07 20:14:31 +00004127 for (std::vector<std::string>::const_iterator i = ToolChain.ExtraOpts.begin(),
4128 e = ToolChain.ExtraOpts.end();
4129 i != e; ++i)
4130 CmdArgs.push_back(i->c_str());
4131
4132 if (!Args.hasArg(options::OPT_static)) {
4133 CmdArgs.push_back("--eh-frame-hdr");
4134 }
4135
4136 CmdArgs.push_back("-m");
4137 if (ToolChain.getArch() == llvm::Triple::x86)
4138 CmdArgs.push_back("elf_i386");
Eric Christopher84fbdb42011-08-19 00:30:14 +00004139 else if (ToolChain.getArch() == llvm::Triple::arm
Douglas Gregord9bb1522011-03-06 19:11:49 +00004140 || ToolChain.getArch() == llvm::Triple::thumb)
Rafael Espindolac8f008f2010-11-07 20:14:31 +00004141 CmdArgs.push_back("armelf_linux_eabi");
Ted Kremenek43d47cc2011-04-05 22:04:27 +00004142 else if (ToolChain.getArch() == llvm::Triple::ppc)
4143 CmdArgs.push_back("elf32ppclinux");
4144 else if (ToolChain.getArch() == llvm::Triple::ppc64)
4145 CmdArgs.push_back("elf64ppc");
Rafael Espindolac8f008f2010-11-07 20:14:31 +00004146 else
4147 CmdArgs.push_back("elf_x86_64");
4148
4149 if (Args.hasArg(options::OPT_static)) {
Douglas Gregord9bb1522011-03-06 19:11:49 +00004150 if (ToolChain.getArch() == llvm::Triple::arm
4151 || ToolChain.getArch() == llvm::Triple::thumb)
Rafael Espindolac8f008f2010-11-07 20:14:31 +00004152 CmdArgs.push_back("-Bstatic");
4153 else
4154 CmdArgs.push_back("-static");
4155 } else if (Args.hasArg(options::OPT_shared)) {
4156 CmdArgs.push_back("-shared");
4157 }
4158
4159 if (ToolChain.getArch() == llvm::Triple::arm ||
Douglas Gregord9bb1522011-03-06 19:11:49 +00004160 ToolChain.getArch() == llvm::Triple::thumb ||
Rafael Espindolac8f008f2010-11-07 20:14:31 +00004161 (!Args.hasArg(options::OPT_static) &&
4162 !Args.hasArg(options::OPT_shared))) {
4163 CmdArgs.push_back("-dynamic-linker");
4164 if (ToolChain.getArch() == llvm::Triple::x86)
4165 CmdArgs.push_back("/lib/ld-linux.so.2");
Douglas Gregord9bb1522011-03-06 19:11:49 +00004166 else if (ToolChain.getArch() == llvm::Triple::arm ||
4167 ToolChain.getArch() == llvm::Triple::thumb)
Rafael Espindolac8f008f2010-11-07 20:14:31 +00004168 CmdArgs.push_back("/lib/ld-linux.so.3");
Ted Kremenek43d47cc2011-04-05 22:04:27 +00004169 else if (ToolChain.getArch() == llvm::Triple::ppc)
Chris Lattner20b90d02011-04-11 21:15:37 +00004170 CmdArgs.push_back("/lib/ld.so.1");
Ted Kremenek43d47cc2011-04-05 22:04:27 +00004171 else if (ToolChain.getArch() == llvm::Triple::ppc64)
Chris Lattner20b90d02011-04-11 21:15:37 +00004172 CmdArgs.push_back("/lib64/ld64.so.1");
Rafael Espindolac8f008f2010-11-07 20:14:31 +00004173 else
4174 CmdArgs.push_back("/lib64/ld-linux-x86-64.so.2");
4175 }
4176
4177 CmdArgs.push_back("-o");
4178 CmdArgs.push_back(Output.getFilename());
4179
Rafael Espindola81937ec2010-12-01 01:52:43 +00004180 if (!Args.hasArg(options::OPT_nostdlib) &&
4181 !Args.hasArg(options::OPT_nostartfiles)) {
Rafael Espindolad47ac232010-11-17 22:26:15 +00004182 const char *crt1 = NULL;
4183 if (!Args.hasArg(options::OPT_shared)){
4184 if (Args.hasArg(options::OPT_pie))
4185 crt1 = "Scrt1.o";
4186 else
4187 crt1 = "crt1.o";
4188 }
4189 if (crt1)
4190 CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath(crt1)));
Rafael Espindolac8f008f2010-11-07 20:14:31 +00004191
Rafael Espindola9aa60e92010-11-12 03:00:39 +00004192 CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath("crti.o")));
Rafael Espindolac8f008f2010-11-07 20:14:31 +00004193
Rafael Espindola9aa60e92010-11-12 03:00:39 +00004194 const char *crtbegin;
4195 if (Args.hasArg(options::OPT_static))
4196 crtbegin = "crtbeginT.o";
Rafael Espindolad47ac232010-11-17 22:26:15 +00004197 else if (Args.hasArg(options::OPT_shared) || Args.hasArg(options::OPT_pie))
Rafael Espindola9aa60e92010-11-12 03:00:39 +00004198 crtbegin = "crtbeginS.o";
4199 else
4200 crtbegin = "crtbegin.o";
4201 CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath(crtbegin)));
4202 }
Rafael Espindolac8f008f2010-11-07 20:14:31 +00004203
4204 Args.AddAllArgs(CmdArgs, options::OPT_L);
4205
4206 const ToolChain::path_list Paths = ToolChain.getFilePaths();
4207
Roman Divackyee8188a2011-03-01 17:53:14 +00004208 for (ToolChain::path_list::const_iterator i = Paths.begin(), e = Paths.end();
4209 i != e; ++i)
Chris Lattner0e62c1c2011-07-23 10:55:15 +00004210 CmdArgs.push_back(Args.MakeArgString(StringRef("-L") + *i));
Rafael Espindolac8f008f2010-11-07 20:14:31 +00004211
4212 AddLinkerInputs(ToolChain, Inputs, Args, CmdArgs);
4213
Rafael Espindola9aa60e92010-11-12 03:00:39 +00004214 if (D.CCCIsCXX && !Args.hasArg(options::OPT_nostdlib)) {
Rafael Espindolac8f008f2010-11-07 20:14:31 +00004215 ToolChain.AddCXXStdlibLibArgs(Args, CmdArgs);
4216 CmdArgs.push_back("-lm");
4217 }
4218
Rafael Espindola9aa60e92010-11-12 03:00:39 +00004219 if (!Args.hasArg(options::OPT_nostdlib)) {
Nick Lewycky97864da2011-06-04 06:27:06 +00004220 if (Args.hasArg(options::OPT_static))
4221 CmdArgs.push_back("--start-group");
4222
Rafael Espindola9aa60e92010-11-12 03:00:39 +00004223 if (!D.CCCIsCXX)
Rafael Espindolac8f008f2010-11-07 20:14:31 +00004224 CmdArgs.push_back("-lgcc");
Rafael Espindola9aa60e92010-11-12 03:00:39 +00004225
4226 if (Args.hasArg(options::OPT_static)) {
4227 if (D.CCCIsCXX)
4228 CmdArgs.push_back("-lgcc");
4229 } else {
4230 if (!D.CCCIsCXX)
4231 CmdArgs.push_back("--as-needed");
4232 CmdArgs.push_back("-lgcc_s");
4233 if (!D.CCCIsCXX)
4234 CmdArgs.push_back("--no-as-needed");
4235 }
4236
4237 if (Args.hasArg(options::OPT_static))
4238 CmdArgs.push_back("-lgcc_eh");
4239 else if (!Args.hasArg(options::OPT_shared) && D.CCCIsCXX)
4240 CmdArgs.push_back("-lgcc");
4241
4242 if (Args.hasArg(options::OPT_pthread) ||
4243 Args.hasArg(options::OPT_pthreads))
4244 CmdArgs.push_back("-lpthread");
4245
4246 CmdArgs.push_back("-lc");
4247
4248 if (Args.hasArg(options::OPT_static))
4249 CmdArgs.push_back("--end-group");
4250 else {
4251 if (!D.CCCIsCXX)
4252 CmdArgs.push_back("-lgcc");
4253
4254 if (!D.CCCIsCXX)
4255 CmdArgs.push_back("--as-needed");
4256 CmdArgs.push_back("-lgcc_s");
4257 if (!D.CCCIsCXX)
4258 CmdArgs.push_back("--no-as-needed");
4259
4260 if (!Args.hasArg(options::OPT_shared) && D.CCCIsCXX)
4261 CmdArgs.push_back("-lgcc");
4262 }
4263
Rafael Espindolad47ac232010-11-17 22:26:15 +00004264
Rafael Espindola81937ec2010-12-01 01:52:43 +00004265 if (!Args.hasArg(options::OPT_nostartfiles)) {
4266 const char *crtend;
4267 if (Args.hasArg(options::OPT_shared) || Args.hasArg(options::OPT_pie))
4268 crtend = "crtendS.o";
4269 else
4270 crtend = "crtend.o";
Rafael Espindola9aa60e92010-11-12 03:00:39 +00004271
Rafael Espindola81937ec2010-12-01 01:52:43 +00004272 CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath(crtend)));
4273 CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath("crtn.o")));
4274 }
Rafael Espindolac8f008f2010-11-07 20:14:31 +00004275 }
4276
Bill Wendling08760582011-06-27 19:15:03 +00004277 addProfileRT(getToolChain(), Args, CmdArgs, getToolChain().getTriple());
Nick Lewycky82fe5f42011-05-24 21:54:59 +00004278
Rafael Espindolac8f008f2010-11-07 20:14:31 +00004279 if (Args.hasArg(options::OPT_use_gold_plugin)) {
4280 CmdArgs.push_back("-plugin");
4281 std::string Plugin = ToolChain.getDriver().Dir + "/../lib/LLVMgold.so";
4282 CmdArgs.push_back(Args.MakeArgString(Plugin));
4283 }
4284
4285 C.addCommand(new Command(JA, *this, ToolChain.Linker.c_str(), CmdArgs));
4286}
Rafael Espindola92b00932010-08-10 00:25:48 +00004287
Chris Lattner3e2ee142010-07-07 16:01:42 +00004288void minix::Assemble::ConstructJob(Compilation &C, const JobAction &JA,
Daniel Dunbar7fbaf532010-08-02 02:38:28 +00004289 const InputInfo &Output,
4290 const InputInfoList &Inputs,
4291 const ArgList &Args,
4292 const char *LinkingOutput) const {
Chris Lattner3e2ee142010-07-07 16:01:42 +00004293 ArgStringList CmdArgs;
4294
4295 Args.AddAllArgValues(CmdArgs, options::OPT_Wa_COMMA,
4296 options::OPT_Xassembler);
4297
4298 CmdArgs.push_back("-o");
Daniel Dunbarb440f562010-08-02 02:38:21 +00004299 CmdArgs.push_back(Output.getFilename());
Chris Lattner3e2ee142010-07-07 16:01:42 +00004300
4301 for (InputInfoList::const_iterator
4302 it = Inputs.begin(), ie = Inputs.end(); it != ie; ++it) {
4303 const InputInfo &II = *it;
Daniel Dunbarb440f562010-08-02 02:38:21 +00004304 CmdArgs.push_back(II.getFilename());
Chris Lattner3e2ee142010-07-07 16:01:42 +00004305 }
4306
4307 const char *Exec =
Daniel Dunbar9c3ed5f2010-07-14 18:46:23 +00004308 Args.MakeArgString(getToolChain().GetProgramPath("gas"));
Daniel Dunbar7fbaf532010-08-02 02:38:28 +00004309 C.addCommand(new Command(JA, *this, Exec, CmdArgs));
Chris Lattner3e2ee142010-07-07 16:01:42 +00004310}
4311
4312void minix::Link::ConstructJob(Compilation &C, const JobAction &JA,
Daniel Dunbar7fbaf532010-08-02 02:38:28 +00004313 const InputInfo &Output,
4314 const InputInfoList &Inputs,
4315 const ArgList &Args,
4316 const char *LinkingOutput) const {
Chris Lattner3e2ee142010-07-07 16:01:42 +00004317 const Driver &D = getToolChain().getDriver();
4318 ArgStringList CmdArgs;
4319
Daniel Dunbarb440f562010-08-02 02:38:21 +00004320 if (Output.isFilename()) {
Chris Lattner3e2ee142010-07-07 16:01:42 +00004321 CmdArgs.push_back("-o");
4322 CmdArgs.push_back(Output.getFilename());
4323 } else {
4324 assert(Output.isNothing() && "Invalid output.");
4325 }
4326
4327 if (!Args.hasArg(options::OPT_nostdlib) &&
4328 !Args.hasArg(options::OPT_nostartfiles))
Daniel Dunbar9c3ed5f2010-07-14 18:46:23 +00004329 CmdArgs.push_back(Args.MakeArgString(getToolChain().GetFilePath(
Chris Lattner3e2ee142010-07-07 16:01:42 +00004330 "/usr/gnu/lib/crtso.o")));
4331
4332 Args.AddAllArgs(CmdArgs, options::OPT_L);
4333 Args.AddAllArgs(CmdArgs, options::OPT_T_Group);
4334 Args.AddAllArgs(CmdArgs, options::OPT_e);
4335
Daniel Dunbar54423b22010-09-17 00:24:54 +00004336 AddLinkerInputs(getToolChain(), Inputs, Args, CmdArgs);
Chris Lattner3e2ee142010-07-07 16:01:42 +00004337
4338 if (!Args.hasArg(options::OPT_nostdlib) &&
4339 !Args.hasArg(options::OPT_nodefaultlibs)) {
4340 if (D.CCCIsCXX) {
Daniel Dunbar3f7796f2010-09-17 01:20:05 +00004341 getToolChain().AddCXXStdlibLibArgs(Args, CmdArgs);
Chris Lattner3e2ee142010-07-07 16:01:42 +00004342 CmdArgs.push_back("-lm");
4343 }
4344
4345 if (Args.hasArg(options::OPT_pthread))
4346 CmdArgs.push_back("-lpthread");
4347 CmdArgs.push_back("-lc");
4348 CmdArgs.push_back("-lgcc");
4349 CmdArgs.push_back("-L/usr/gnu/lib");
4350 // FIXME: fill in the correct search path for the final
4351 // support libraries.
4352 CmdArgs.push_back("-L/usr/gnu/lib/gcc/i686-pc-minix/4.4.3");
4353 }
4354
4355 if (!Args.hasArg(options::OPT_nostdlib) &&
4356 !Args.hasArg(options::OPT_nostartfiles)) {
Daniel Dunbar9c3ed5f2010-07-14 18:46:23 +00004357 CmdArgs.push_back(Args.MakeArgString(getToolChain().GetFilePath(
Chris Lattner3e2ee142010-07-07 16:01:42 +00004358 "/usr/gnu/lib/libend.a")));
4359 }
4360
Bill Wendling08760582011-06-27 19:15:03 +00004361 addProfileRT(getToolChain(), Args, CmdArgs, getToolChain().getTriple());
Nick Lewycky82fe5f42011-05-24 21:54:59 +00004362
Chris Lattner3e2ee142010-07-07 16:01:42 +00004363 const char *Exec =
Daniel Dunbar9c3ed5f2010-07-14 18:46:23 +00004364 Args.MakeArgString(getToolChain().GetProgramPath("/usr/gnu/bin/gld"));
Daniel Dunbar7fbaf532010-08-02 02:38:28 +00004365 C.addCommand(new Command(JA, *this, Exec, CmdArgs));
Chris Lattner3e2ee142010-07-07 16:01:42 +00004366}
4367
Daniel Dunbarcc912342009-05-02 18:28:39 +00004368/// DragonFly Tools
4369
4370// For now, DragonFly Assemble does just about the same as for
4371// FreeBSD, but this may change soon.
4372void dragonfly::Assemble::ConstructJob(Compilation &C, const JobAction &JA,
Daniel Dunbar7fbaf532010-08-02 02:38:28 +00004373 const InputInfo &Output,
Daniel Dunbar1c7577c2009-11-04 06:24:38 +00004374 const InputInfoList &Inputs,
4375 const ArgList &Args,
4376 const char *LinkingOutput) const {
Daniel Dunbarcc912342009-05-02 18:28:39 +00004377 ArgStringList CmdArgs;
4378
4379 // When building 32-bit code on DragonFly/pc64, we have to explicitly
4380 // instruct as in the base system to assemble 32-bit code.
4381 if (getToolChain().getArchName() == "i386")
4382 CmdArgs.push_back("--32");
4383
4384 Args.AddAllArgValues(CmdArgs, options::OPT_Wa_COMMA,
4385 options::OPT_Xassembler);
4386
4387 CmdArgs.push_back("-o");
Daniel Dunbarb440f562010-08-02 02:38:21 +00004388 CmdArgs.push_back(Output.getFilename());
Daniel Dunbarcc912342009-05-02 18:28:39 +00004389
4390 for (InputInfoList::const_iterator
4391 it = Inputs.begin(), ie = Inputs.end(); it != ie; ++it) {
4392 const InputInfo &II = *it;
Daniel Dunbarb440f562010-08-02 02:38:21 +00004393 CmdArgs.push_back(II.getFilename());
Daniel Dunbarcc912342009-05-02 18:28:39 +00004394 }
4395
4396 const char *Exec =
Daniel Dunbar9c3ed5f2010-07-14 18:46:23 +00004397 Args.MakeArgString(getToolChain().GetProgramPath("as"));
Daniel Dunbar7fbaf532010-08-02 02:38:28 +00004398 C.addCommand(new Command(JA, *this, Exec, CmdArgs));
Daniel Dunbarcc912342009-05-02 18:28:39 +00004399}
4400
4401void dragonfly::Link::ConstructJob(Compilation &C, const JobAction &JA,
Daniel Dunbar7fbaf532010-08-02 02:38:28 +00004402 const InputInfo &Output,
4403 const InputInfoList &Inputs,
4404 const ArgList &Args,
4405 const char *LinkingOutput) const {
Daniel Dunbar083edf72009-12-21 18:54:17 +00004406 const Driver &D = getToolChain().getDriver();
Daniel Dunbarcc912342009-05-02 18:28:39 +00004407 ArgStringList CmdArgs;
4408
Joerg Sonnenberger6165ab12011-03-21 13:51:29 +00004409 if (!D.SysRoot.empty())
4410 CmdArgs.push_back(Args.MakeArgString("--sysroot=" + D.SysRoot));
4411
Daniel Dunbarcc912342009-05-02 18:28:39 +00004412 if (Args.hasArg(options::OPT_static)) {
4413 CmdArgs.push_back("-Bstatic");
4414 } else {
4415 if (Args.hasArg(options::OPT_shared))
4416 CmdArgs.push_back("-Bshareable");
4417 else {
4418 CmdArgs.push_back("-dynamic-linker");
4419 CmdArgs.push_back("/usr/libexec/ld-elf.so.2");
4420 }
4421 }
4422
4423 // When building 32-bit code on DragonFly/pc64, we have to explicitly
4424 // instruct ld in the base system to link 32-bit code.
4425 if (getToolChain().getArchName() == "i386") {
4426 CmdArgs.push_back("-m");
4427 CmdArgs.push_back("elf_i386");
4428 }
4429
Daniel Dunbarb440f562010-08-02 02:38:21 +00004430 if (Output.isFilename()) {
Daniel Dunbarcc912342009-05-02 18:28:39 +00004431 CmdArgs.push_back("-o");
4432 CmdArgs.push_back(Output.getFilename());
4433 } else {
4434 assert(Output.isNothing() && "Invalid output.");
4435 }
4436
4437 if (!Args.hasArg(options::OPT_nostdlib) &&
4438 !Args.hasArg(options::OPT_nostartfiles)) {
4439 if (!Args.hasArg(options::OPT_shared)) {
Chris Lattner3e2ee142010-07-07 16:01:42 +00004440 CmdArgs.push_back(
Daniel Dunbar9c3ed5f2010-07-14 18:46:23 +00004441 Args.MakeArgString(getToolChain().GetFilePath("crt1.o")));
Chris Lattner3e2ee142010-07-07 16:01:42 +00004442 CmdArgs.push_back(
Daniel Dunbar9c3ed5f2010-07-14 18:46:23 +00004443 Args.MakeArgString(getToolChain().GetFilePath("crti.o")));
Chris Lattner3e2ee142010-07-07 16:01:42 +00004444 CmdArgs.push_back(
Daniel Dunbar9c3ed5f2010-07-14 18:46:23 +00004445 Args.MakeArgString(getToolChain().GetFilePath("crtbegin.o")));
Daniel Dunbarcc912342009-05-02 18:28:39 +00004446 } else {
Chris Lattner3e2ee142010-07-07 16:01:42 +00004447 CmdArgs.push_back(
Daniel Dunbar9c3ed5f2010-07-14 18:46:23 +00004448 Args.MakeArgString(getToolChain().GetFilePath("crti.o")));
Chris Lattner3e2ee142010-07-07 16:01:42 +00004449 CmdArgs.push_back(
Daniel Dunbar9c3ed5f2010-07-14 18:46:23 +00004450 Args.MakeArgString(getToolChain().GetFilePath("crtbeginS.o")));
Daniel Dunbarcc912342009-05-02 18:28:39 +00004451 }
4452 }
4453
4454 Args.AddAllArgs(CmdArgs, options::OPT_L);
4455 Args.AddAllArgs(CmdArgs, options::OPT_T_Group);
4456 Args.AddAllArgs(CmdArgs, options::OPT_e);
4457
Daniel Dunbar54423b22010-09-17 00:24:54 +00004458 AddLinkerInputs(getToolChain(), Inputs, Args, CmdArgs);
Daniel Dunbarcc912342009-05-02 18:28:39 +00004459
4460 if (!Args.hasArg(options::OPT_nostdlib) &&
4461 !Args.hasArg(options::OPT_nodefaultlibs)) {
4462 // FIXME: GCC passes on -lgcc, -lgcc_pic and a whole lot of
4463 // rpaths
4464 CmdArgs.push_back("-L/usr/lib/gcc41");
4465
4466 if (!Args.hasArg(options::OPT_static)) {
4467 CmdArgs.push_back("-rpath");
4468 CmdArgs.push_back("/usr/lib/gcc41");
4469
4470 CmdArgs.push_back("-rpath-link");
4471 CmdArgs.push_back("/usr/lib/gcc41");
4472
4473 CmdArgs.push_back("-rpath");
4474 CmdArgs.push_back("/usr/lib");
4475
4476 CmdArgs.push_back("-rpath-link");
4477 CmdArgs.push_back("/usr/lib");
4478 }
4479
Rafael Espindola38360b32010-07-20 12:59:03 +00004480 if (D.CCCIsCXX) {
Daniel Dunbar3f7796f2010-09-17 01:20:05 +00004481 getToolChain().AddCXXStdlibLibArgs(Args, CmdArgs);
Rafael Espindola38360b32010-07-20 12:59:03 +00004482 CmdArgs.push_back("-lm");
4483 }
4484
Daniel Dunbarcc912342009-05-02 18:28:39 +00004485 if (Args.hasArg(options::OPT_shared)) {
4486 CmdArgs.push_back("-lgcc_pic");
4487 } else {
4488 CmdArgs.push_back("-lgcc");
4489 }
4490
4491
4492 if (Args.hasArg(options::OPT_pthread))
Mike Stump0a65b632009-10-31 20:11:46 +00004493 CmdArgs.push_back("-lpthread");
Daniel Dunbarcc912342009-05-02 18:28:39 +00004494
4495 if (!Args.hasArg(options::OPT_nolibc)) {
4496 CmdArgs.push_back("-lc");
4497 }
4498
4499 if (Args.hasArg(options::OPT_shared)) {
4500 CmdArgs.push_back("-lgcc_pic");
4501 } else {
4502 CmdArgs.push_back("-lgcc");
4503 }
4504 }
4505
4506 if (!Args.hasArg(options::OPT_nostdlib) &&
4507 !Args.hasArg(options::OPT_nostartfiles)) {
4508 if (!Args.hasArg(options::OPT_shared))
Chris Lattner3e2ee142010-07-07 16:01:42 +00004509 CmdArgs.push_back(Args.MakeArgString(
Daniel Dunbar9c3ed5f2010-07-14 18:46:23 +00004510 getToolChain().GetFilePath("crtend.o")));
Daniel Dunbarcc912342009-05-02 18:28:39 +00004511 else
Chris Lattner3e2ee142010-07-07 16:01:42 +00004512 CmdArgs.push_back(Args.MakeArgString(
Daniel Dunbar9c3ed5f2010-07-14 18:46:23 +00004513 getToolChain().GetFilePath("crtendS.o")));
Chris Lattner3e2ee142010-07-07 16:01:42 +00004514 CmdArgs.push_back(Args.MakeArgString(
Daniel Dunbar9c3ed5f2010-07-14 18:46:23 +00004515 getToolChain().GetFilePath("crtn.o")));
Daniel Dunbarcc912342009-05-02 18:28:39 +00004516 }
4517
Bill Wendling08760582011-06-27 19:15:03 +00004518 addProfileRT(getToolChain(), Args, CmdArgs, getToolChain().getTriple());
Nick Lewycky82fe5f42011-05-24 21:54:59 +00004519
Daniel Dunbarcc912342009-05-02 18:28:39 +00004520 const char *Exec =
Daniel Dunbar9c3ed5f2010-07-14 18:46:23 +00004521 Args.MakeArgString(getToolChain().GetProgramPath("ld"));
Daniel Dunbar7fbaf532010-08-02 02:38:28 +00004522 C.addCommand(new Command(JA, *this, Exec, CmdArgs));
Daniel Dunbarcc912342009-05-02 18:28:39 +00004523}
Michael J. Spencerb186bc32010-08-21 21:55:07 +00004524
4525void visualstudio::Link::ConstructJob(Compilation &C, const JobAction &JA,
4526 const InputInfo &Output,
4527 const InputInfoList &Inputs,
4528 const ArgList &Args,
4529 const char *LinkingOutput) const {
Michael J. Spencerb186bc32010-08-21 21:55:07 +00004530 ArgStringList CmdArgs;
4531
4532 if (Output.isFilename()) {
Daniel Dunbar2cc3f172010-09-17 00:45:02 +00004533 CmdArgs.push_back(Args.MakeArgString(std::string("-out:") +
4534 Output.getFilename()));
Michael J. Spencerb186bc32010-08-21 21:55:07 +00004535 } else {
4536 assert(Output.isNothing() && "Invalid output.");
4537 }
4538
4539 if (!Args.hasArg(options::OPT_nostdlib) &&
4540 !Args.hasArg(options::OPT_nostartfiles)) {
4541 CmdArgs.push_back("-defaultlib:libcmt");
4542 }
4543
4544 CmdArgs.push_back("-nologo");
4545
Daniel Dunbar54423b22010-09-17 00:24:54 +00004546 AddLinkerInputs(getToolChain(), Inputs, Args, CmdArgs);
Michael J. Spencerb186bc32010-08-21 21:55:07 +00004547
4548 const char *Exec =
Daniel Dunbar54423b22010-09-17 00:24:54 +00004549 Args.MakeArgString(getToolChain().GetProgramPath("link.exe"));
Michael J. Spencerb186bc32010-08-21 21:55:07 +00004550 C.addCommand(new Command(JA, *this, Exec, CmdArgs));
4551}