blob: f925f762e568162a3b1f8524a77145a2594c2019 [file] [log] [blame]
Nick Lewyckye3365aa2010-09-23 23:48:20 +00001//===--- Tools.cpp - Tools Implementations --------------------------------===//
Daniel Dunbar47ac7d22009-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"
Chandler Carruth55fc8732012-12-04 09:13:33 +000011#include "InputInfo.h"
12#include "SanitizerArgs.h"
13#include "ToolChains.h"
14#include "clang/Basic/ObjCRuntime.h"
Kevin Enderby02341792013-01-17 21:38:06 +000015#include "clang/Basic/Version.h"
Daniel Dunbar1d460332009-03-18 10:01:51 +000016#include "clang/Driver/Action.h"
Daniel Dunbar871adcf2009-03-18 07:06:02 +000017#include "clang/Driver/Arg.h"
Daniel Dunbarb488c1d2009-03-18 08:07:30 +000018#include "clang/Driver/ArgList.h"
Chandler Carruth55fc8732012-12-04 09:13:33 +000019#include "clang/Driver/Compilation.h"
Daniel Dunbaree848a72009-10-29 02:39:57 +000020#include "clang/Driver/Driver.h"
21#include "clang/Driver/DriverDiagnostic.h"
Daniel Dunbar871adcf2009-03-18 07:06:02 +000022#include "clang/Driver/Job.h"
Daniel Dunbarb488c1d2009-03-18 08:07:30 +000023#include "clang/Driver/Option.h"
Daniel Dunbar265e9ef2009-11-19 04:25:22 +000024#include "clang/Driver/Options.h"
Daniel Dunbarb488c1d2009-03-18 08:07:30 +000025#include "clang/Driver/ToolChain.h"
Daniel Dunbar871adcf2009-03-18 07:06:02 +000026#include "clang/Driver/Util.h"
Daniel Dunbar88137642009-09-09 22:32:48 +000027#include "llvm/ADT/SmallString.h"
Douglas Gregor55d3f7a2009-10-29 00:41:01 +000028#include "llvm/ADT/StringSwitch.h"
Daniel Dunbar5b750fe2009-09-09 22:32:34 +000029#include "llvm/ADT/Twine.h"
Chandler Carruth55fc8732012-12-04 09:13:33 +000030#include "llvm/Support/ErrorHandling.h"
Michael J. Spencer32bef4e2011-01-10 02:34:13 +000031#include "llvm/Support/FileSystem.h"
Daniel Dunbar02633b52009-03-26 16:23:12 +000032#include "llvm/Support/Format.h"
Michael J. Spencer03013fa2010-11-29 18:12:39 +000033#include "llvm/Support/Host.h"
34#include "llvm/Support/Process.h"
Chandler Carruth55fc8732012-12-04 09:13:33 +000035#include "llvm/Support/raw_ostream.h"
Daniel Dunbar871adcf2009-03-18 07:06:02 +000036
Daniel Dunbar47ac7d22009-03-18 06:00:36 +000037using namespace clang::driver;
38using namespace clang::driver::tools;
Chris Lattner5f9e2722011-07-23 10:55:15 +000039using namespace clang;
Daniel Dunbar47ac7d22009-03-18 06:00:36 +000040
Daniel Dunbar88a3d6c2009-09-10 01:21:05 +000041/// CheckPreprocessingOptions - Perform some validation of preprocessing
42/// arguments that is shared with gcc.
43static void CheckPreprocessingOptions(const Driver &D, const ArgList &Args) {
44 if (Arg *A = Args.getLastArg(options::OPT_C, options::OPT_CC))
Joerg Sonnenberger9ade4ae2011-03-06 23:31:01 +000045 if (!Args.hasArg(options::OPT_E) && !D.CCCIsCPP)
Chris Lattner5f9e2722011-07-23 10:55:15 +000046 D.Diag(diag::err_drv_argument_only_allowed_with)
Daniel Dunbar88a3d6c2009-09-10 01:21:05 +000047 << A->getAsString(Args) << "-E";
48}
49
Daniel Dunbare2fd6642009-09-10 01:21:12 +000050/// CheckCodeGenerationOptions - Perform some validation of code generation
51/// arguments that is shared with gcc.
52static void CheckCodeGenerationOptions(const Driver &D, const ArgList &Args) {
53 // In gcc, only ARM checks this, but it seems reasonable to check universally.
54 if (Args.hasArg(options::OPT_static))
55 if (const Arg *A = Args.getLastArg(options::OPT_dynamic,
56 options::OPT_mdynamic_no_pic))
Chris Lattner5f9e2722011-07-23 10:55:15 +000057 D.Diag(diag::err_drv_argument_not_allowed_with)
Daniel Dunbare2fd6642009-09-10 01:21:12 +000058 << A->getAsString(Args) << "-static";
59}
60
Chris Lattner3edbeb72010-03-29 17:55:58 +000061// Quote target names for inclusion in GNU Make dependency files.
62// Only the characters '$', '#', ' ', '\t' are quoted.
Chris Lattner5f9e2722011-07-23 10:55:15 +000063static void QuoteTarget(StringRef Target,
64 SmallVectorImpl<char> &Res) {
Chris Lattner3edbeb72010-03-29 17:55:58 +000065 for (unsigned i = 0, e = Target.size(); i != e; ++i) {
66 switch (Target[i]) {
67 case ' ':
68 case '\t':
69 // Escape the preceding backslashes
70 for (int j = i - 1; j >= 0 && Target[j] == '\\'; --j)
71 Res.push_back('\\');
72
73 // Escape the space/tab
74 Res.push_back('\\');
75 break;
76 case '$':
77 Res.push_back('$');
78 break;
79 case '#':
80 Res.push_back('\\');
81 break;
82 default:
83 break;
84 }
85
86 Res.push_back(Target[i]);
87 }
88}
89
Bill Wendling3d717152012-03-12 22:10:06 +000090static void addDirectoryList(const ArgList &Args,
Bill Wendlingbdb8f3c2012-03-12 21:22:35 +000091 ArgStringList &CmdArgs,
92 const char *ArgName,
Bill Wendling3d717152012-03-12 22:10:06 +000093 const char *EnvVar) {
94 const char *DirList = ::getenv(EnvVar);
Chad Rosier89aa2ce2012-10-30 21:42:09 +000095 bool CombinedArg = false;
96
Bill Wendlingbdb8f3c2012-03-12 21:22:35 +000097 if (!DirList)
98 return; // Nothing to do.
99
Chad Rosier89aa2ce2012-10-30 21:42:09 +0000100 StringRef Name(ArgName);
101 if (Name.equals("-I") || Name.equals("-L"))
102 CombinedArg = true;
103
Bill Wendlingbdb8f3c2012-03-12 21:22:35 +0000104 StringRef Dirs(DirList);
105 if (Dirs.empty()) // Empty string should not add '.'.
106 return;
107
108 StringRef::size_type Delim;
109 while ((Delim = Dirs.find(llvm::sys::PathSeparator)) != StringRef::npos) {
110 if (Delim == 0) { // Leading colon.
Chad Rosier89aa2ce2012-10-30 21:42:09 +0000111 if (CombinedArg) {
112 CmdArgs.push_back(Args.MakeArgString(std::string(ArgName) + "."));
113 } else {
114 CmdArgs.push_back(ArgName);
115 CmdArgs.push_back(".");
116 }
Bill Wendlingbdb8f3c2012-03-12 21:22:35 +0000117 } else {
Chad Rosier89aa2ce2012-10-30 21:42:09 +0000118 if (CombinedArg) {
119 CmdArgs.push_back(Args.MakeArgString(std::string(ArgName) + Dirs.substr(0, Delim)));
120 } else {
121 CmdArgs.push_back(ArgName);
122 CmdArgs.push_back(Args.MakeArgString(Dirs.substr(0, Delim)));
123 }
Bill Wendlingbdb8f3c2012-03-12 21:22:35 +0000124 }
Nico Weber09c5c392012-03-19 15:00:03 +0000125 Dirs = Dirs.substr(Delim + 1);
Bill Wendlingbdb8f3c2012-03-12 21:22:35 +0000126 }
127
128 if (Dirs.empty()) { // Trailing colon.
Chad Rosier89aa2ce2012-10-30 21:42:09 +0000129 if (CombinedArg) {
130 CmdArgs.push_back(Args.MakeArgString(std::string(ArgName) + "."));
131 } else {
132 CmdArgs.push_back(ArgName);
133 CmdArgs.push_back(".");
134 }
Bill Wendlingbdb8f3c2012-03-12 21:22:35 +0000135 } else { // Add the last path.
Chad Rosier89aa2ce2012-10-30 21:42:09 +0000136 if (CombinedArg) {
137 CmdArgs.push_back(Args.MakeArgString(std::string(ArgName) + Dirs));
138 } else {
139 CmdArgs.push_back(ArgName);
140 CmdArgs.push_back(Args.MakeArgString(Dirs));
141 }
Bill Wendlingbdb8f3c2012-03-12 21:22:35 +0000142 }
143}
144
Daniel Dunbar2008fee2010-09-17 00:24:54 +0000145static void AddLinkerInputs(const ToolChain &TC,
146 const InputInfoList &Inputs, const ArgList &Args,
147 ArgStringList &CmdArgs) {
148 const Driver &D = TC.getDriver();
149
Daniel Dunbar8ac38d72011-02-19 05:33:51 +0000150 // Add extra linker input arguments which are not treated as inputs
151 // (constructed via -Xarch_).
152 Args.AddAllArgValues(CmdArgs, options::OPT_Zlinker_input);
153
Daniel Dunbar2008fee2010-09-17 00:24:54 +0000154 for (InputInfoList::const_iterator
155 it = Inputs.begin(), ie = Inputs.end(); it != ie; ++it) {
156 const InputInfo &II = *it;
157
158 if (!TC.HasNativeLLVMSupport()) {
159 // Don't try to pass LLVM inputs unless we have native support.
160 if (II.getType() == types::TY_LLVM_IR ||
161 II.getType() == types::TY_LTO_IR ||
162 II.getType() == types::TY_LLVM_BC ||
163 II.getType() == types::TY_LTO_BC)
Chris Lattner5f9e2722011-07-23 10:55:15 +0000164 D.Diag(diag::err_drv_no_linker_llvm_support)
Daniel Dunbar2008fee2010-09-17 00:24:54 +0000165 << TC.getTripleString();
166 }
167
Daniel Dunbare5a37f42010-09-17 00:45:02 +0000168 // Add filenames immediately.
169 if (II.isFilename()) {
Daniel Dunbar2008fee2010-09-17 00:24:54 +0000170 CmdArgs.push_back(II.getFilename());
Daniel Dunbare5a37f42010-09-17 00:45:02 +0000171 continue;
172 }
173
174 // Otherwise, this is a linker input argument.
175 const Arg &A = II.getInputArg();
176
177 // Handle reserved library options.
178 if (A.getOption().matches(options::OPT_Z_reserved_lib_stdcxx)) {
Daniel Dunbar132e35d2010-09-17 01:20:05 +0000179 TC.AddCXXStdlibLibArgs(Args, CmdArgs);
Shantonu Sen7433fed2010-09-17 18:39:08 +0000180 } else if (A.getOption().matches(options::OPT_Z_reserved_lib_cckext)) {
181 TC.AddCCKextLibArgs(Args, CmdArgs);
Daniel Dunbare5a37f42010-09-17 00:45:02 +0000182 } else
183 A.renderAsInput(Args, CmdArgs);
Daniel Dunbar2008fee2010-09-17 00:24:54 +0000184 }
Bill Wendlingbdb8f3c2012-03-12 21:22:35 +0000185
186 // LIBRARY_PATH - included following the user specified library paths.
Bill Wendling3d717152012-03-12 22:10:06 +0000187 addDirectoryList(Args, CmdArgs, "-L", "LIBRARY_PATH");
Daniel Dunbar2008fee2010-09-17 00:24:54 +0000188}
189
John McCallf85e1932011-06-15 23:02:42 +0000190/// \brief Determine whether Objective-C automated reference counting is
191/// enabled.
192static bool isObjCAutoRefCount(const ArgList &Args) {
193 return Args.hasFlag(options::OPT_fobjc_arc, options::OPT_fno_objc_arc, false);
194}
195
Ted Kremenekebcb57a2012-03-06 20:05:56 +0000196/// \brief Determine whether we are linking the ObjC runtime.
197static bool isObjCRuntimeLinked(const ArgList &Args) {
Bob Wilsona7635f12012-08-07 19:58:00 +0000198 if (isObjCAutoRefCount(Args)) {
199 Args.ClaimAllArgs(options::OPT_fobjc_link_runtime);
Ted Kremenekebcb57a2012-03-06 20:05:56 +0000200 return true;
Bob Wilsona7635f12012-08-07 19:58:00 +0000201 }
Ted Kremenekebcb57a2012-03-06 20:05:56 +0000202 return Args.hasArg(options::OPT_fobjc_link_runtime);
203}
204
Rafael Espindoladb3f24a2011-06-02 18:58:46 +0000205static void addProfileRT(const ToolChain &TC, const ArgList &Args,
Bill Wendling3f4be6f2011-06-27 19:15:03 +0000206 ArgStringList &CmdArgs,
207 llvm::Triple Triple) {
208 if (!(Args.hasArg(options::OPT_fprofile_arcs) ||
209 Args.hasArg(options::OPT_fprofile_generate) ||
210 Args.hasArg(options::OPT_fcreate_profile) ||
211 Args.hasArg(options::OPT_coverage)))
212 return;
213
214 // GCC links libgcov.a by adding -L<inst>/gcc/lib/gcc/<triple>/<ver> -lgcov to
215 // the link line. We cannot do the same thing because unlike gcov there is a
216 // libprofile_rt.so. We used to use the -l:libprofile_rt.a syntax, but that is
217 // not supported by old linkers.
Benjamin Kramerf2db04c2011-11-07 16:02:25 +0000218 std::string ProfileRT =
219 std::string(TC.getDriver().Dir) + "/../lib/libprofile_rt.a";
Bill Wendling3f4be6f2011-06-27 19:15:03 +0000220
Bill Wendling3f4be6f2011-06-27 19:15:03 +0000221 CmdArgs.push_back(Args.MakeArgString(ProfileRT));
Rafael Espindoladb3f24a2011-06-02 18:58:46 +0000222}
223
Michael J. Spencer91e06da2012-10-19 22:37:06 +0000224static bool forwardToGCC(const Option &O) {
225 return !O.hasFlag(options::NoForward) &&
226 !O.hasFlag(options::DriverOption) &&
227 !O.hasFlag(options::LinkerInput);
228}
229
Peter Collingbourne54db68b2011-11-06 00:40:05 +0000230void Clang::AddPreprocessingOptions(Compilation &C,
Chad Rosier9d718632013-01-24 19:14:47 +0000231 const JobAction &JA,
Peter Collingbourne54db68b2011-11-06 00:40:05 +0000232 const Driver &D,
Douglas Gregordf91ef32009-04-18 00:34:01 +0000233 const ArgList &Args,
Daniel Dunbarc21c4852009-04-08 23:54:23 +0000234 ArgStringList &CmdArgs,
235 const InputInfo &Output,
236 const InputInfoList &Inputs) const {
Daniel Dunbarc21c4852009-04-08 23:54:23 +0000237 Arg *A;
Daniel Dunbar3a183d32009-06-08 21:48:20 +0000238
Daniel Dunbar88a3d6c2009-09-10 01:21:05 +0000239 CheckPreprocessingOptions(D, Args);
240
241 Args.AddLastArg(CmdArgs, options::OPT_C);
242 Args.AddLastArg(CmdArgs, options::OPT_CC);
Daniel Dunbar3a183d32009-06-08 21:48:20 +0000243
244 // Handle dependency file generation.
Daniel Dunbar9eb93b02010-12-08 21:33:40 +0000245 if ((A = Args.getLastArg(options::OPT_M, options::OPT_MM)) ||
Daniel Dunbarc21c4852009-04-08 23:54:23 +0000246 (A = Args.getLastArg(options::OPT_MD)) ||
247 (A = Args.getLastArg(options::OPT_MMD))) {
248 // Determine the output location.
249 const char *DepFile;
Benjamin Kramer99c72082012-09-26 19:01:49 +0000250 if (Arg *MF = Args.getLastArg(options::OPT_MF)) {
Richard Smith1d489cf2012-11-01 04:30:05 +0000251 DepFile = MF->getValue();
Chad Rosier9d718632013-01-24 19:14:47 +0000252 C.addFailureResultFile(DepFile, &JA);
Benjamin Kramer99c72082012-09-26 19:01:49 +0000253 } else if (Output.getType() == types::TY_Dependencies) {
254 DepFile = Output.getFilename();
Daniel Dunbarb827a052009-11-19 03:26:40 +0000255 } else if (A->getOption().matches(options::OPT_M) ||
256 A->getOption().matches(options::OPT_MM)) {
Daniel Dunbarc21c4852009-04-08 23:54:23 +0000257 DepFile = "-";
258 } else {
Bob Wilson66b8a662012-11-23 06:14:39 +0000259 DepFile = getDependencyFileName(Args, Inputs);
Chad Rosier9d718632013-01-24 19:14:47 +0000260 C.addFailureResultFile(DepFile, &JA);
Daniel Dunbarc21c4852009-04-08 23:54:23 +0000261 }
262 CmdArgs.push_back("-dependency-file");
263 CmdArgs.push_back(DepFile);
264
Chris Lattner3edbeb72010-03-29 17:55:58 +0000265 // Add a default target if one wasn't specified.
Daniel Dunbarc21c4852009-04-08 23:54:23 +0000266 if (!Args.hasArg(options::OPT_MT) && !Args.hasArg(options::OPT_MQ)) {
267 const char *DepTarget;
268
269 // If user provided -o, that is the dependency target, except
270 // when we are only generating a dependency file.
271 Arg *OutputOpt = Args.getLastArg(options::OPT_o);
272 if (OutputOpt && Output.getType() != types::TY_Dependencies) {
Richard Smith1d489cf2012-11-01 04:30:05 +0000273 DepTarget = OutputOpt->getValue();
Daniel Dunbarc21c4852009-04-08 23:54:23 +0000274 } else {
275 // Otherwise derive from the base input.
276 //
277 // FIXME: This should use the computed output file location.
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +0000278 SmallString<128> P(Inputs[0].getBaseInput());
Michael J. Spencer472ccff2010-12-18 00:19:12 +0000279 llvm::sys::path::replace_extension(P, "o");
280 DepTarget = Args.MakeArgString(llvm::sys::path::filename(P));
Daniel Dunbarc21c4852009-04-08 23:54:23 +0000281 }
282
283 CmdArgs.push_back("-MT");
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +0000284 SmallString<128> Quoted;
Chris Lattner3edbeb72010-03-29 17:55:58 +0000285 QuoteTarget(DepTarget, Quoted);
286 CmdArgs.push_back(Args.MakeArgString(Quoted));
Daniel Dunbarc21c4852009-04-08 23:54:23 +0000287 }
288
Daniel Dunbarb827a052009-11-19 03:26:40 +0000289 if (A->getOption().matches(options::OPT_M) ||
290 A->getOption().matches(options::OPT_MD))
Daniel Dunbarc21c4852009-04-08 23:54:23 +0000291 CmdArgs.push_back("-sys-header-deps");
292 }
293
Peter Collingbournebb527862011-07-12 19:35:15 +0000294 if (Args.hasArg(options::OPT_MG)) {
295 if (!A || A->getOption().matches(options::OPT_MD) ||
296 A->getOption().matches(options::OPT_MMD))
Chris Lattner5f9e2722011-07-23 10:55:15 +0000297 D.Diag(diag::err_drv_mg_requires_m_or_mm);
Peter Collingbournebb527862011-07-12 19:35:15 +0000298 CmdArgs.push_back("-MG");
299 }
300
Daniel Dunbarc21c4852009-04-08 23:54:23 +0000301 Args.AddLastArg(CmdArgs, options::OPT_MP);
Chris Lattner3edbeb72010-03-29 17:55:58 +0000302
303 // Convert all -MQ <target> args to -MT <quoted target>
304 for (arg_iterator it = Args.filtered_begin(options::OPT_MT,
305 options::OPT_MQ),
306 ie = Args.filtered_end(); it != ie; ++it) {
Daniel Dunbar7e4953e2010-06-11 22:00:13 +0000307 const Arg *A = *it;
308 A->claim();
Chris Lattner3edbeb72010-03-29 17:55:58 +0000309
Daniel Dunbar7e4953e2010-06-11 22:00:13 +0000310 if (A->getOption().matches(options::OPT_MQ)) {
Chris Lattner3edbeb72010-03-29 17:55:58 +0000311 CmdArgs.push_back("-MT");
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +0000312 SmallString<128> Quoted;
Richard Smith1d489cf2012-11-01 04:30:05 +0000313 QuoteTarget(A->getValue(), Quoted);
Chris Lattner3edbeb72010-03-29 17:55:58 +0000314 CmdArgs.push_back(Args.MakeArgString(Quoted));
315
316 // -MT flag - no change
317 } else {
Daniel Dunbar7e4953e2010-06-11 22:00:13 +0000318 A->render(Args, CmdArgs);
Chris Lattner3edbeb72010-03-29 17:55:58 +0000319 }
320 }
Daniel Dunbarc21c4852009-04-08 23:54:23 +0000321
Douglas Gregordf91ef32009-04-18 00:34:01 +0000322 // Add -i* options, and automatically translate to
323 // -include-pch/-include-pth for transparent PCH support. It's
324 // wonky, but we include looking for .gch so we can support seamless
325 // replacement into a build system already set up to be generating
326 // .gch files.
Argyrios Kyrtzidis990142a2010-09-30 16:53:47 +0000327 bool RenderedImplicitInclude = false;
Daniel Dunbarcdd96862009-11-25 11:53:23 +0000328 for (arg_iterator it = Args.filtered_begin(options::OPT_clang_i_Group),
329 ie = Args.filtered_end(); it != ie; ++it) {
330 const Arg *A = it;
Daniel Dunbarc21c4852009-04-08 23:54:23 +0000331
332 if (A->getOption().matches(options::OPT_include)) {
Argyrios Kyrtzidis990142a2010-09-30 16:53:47 +0000333 bool IsFirstImplicitInclude = !RenderedImplicitInclude;
334 RenderedImplicitInclude = true;
335
Argyrios Kyrtzidise5c35372010-08-11 23:27:58 +0000336 // Use PCH if the user requested it.
Daniel Dunbar0ebd9322009-10-15 20:02:44 +0000337 bool UsePCH = D.CCCUsePCH;
Daniel Dunbar0ebd9322009-10-15 20:02:44 +0000338
Daniel Dunbarc21c4852009-04-08 23:54:23 +0000339 bool FoundPTH = false;
Douglas Gregordf91ef32009-04-18 00:34:01 +0000340 bool FoundPCH = false;
Richard Smith1d489cf2012-11-01 04:30:05 +0000341 llvm::sys::Path P(A->getValue());
Michael J. Spencer32bef4e2011-01-10 02:34:13 +0000342 bool Exists;
Daniel Dunbar0ebd9322009-10-15 20:02:44 +0000343 if (UsePCH) {
Douglas Gregordf91ef32009-04-18 00:34:01 +0000344 P.appendSuffix("pch");
Michael J. Spencer32bef4e2011-01-10 02:34:13 +0000345 if (!llvm::sys::fs::exists(P.str(), Exists) && Exists)
Douglas Gregordf91ef32009-04-18 00:34:01 +0000346 FoundPCH = true;
Mike Stump1eb44332009-09-09 15:08:12 +0000347 else
Douglas Gregordf91ef32009-04-18 00:34:01 +0000348 P.eraseSuffix();
Daniel Dunbarc21c4852009-04-08 23:54:23 +0000349 }
350
Douglas Gregordf91ef32009-04-18 00:34:01 +0000351 if (!FoundPCH) {
352 P.appendSuffix("pth");
Michael J. Spencer32bef4e2011-01-10 02:34:13 +0000353 if (!llvm::sys::fs::exists(P.str(), Exists) && Exists)
Douglas Gregordf91ef32009-04-18 00:34:01 +0000354 FoundPTH = true;
355 else
356 P.eraseSuffix();
Mike Stump1eb44332009-09-09 15:08:12 +0000357 }
358
Douglas Gregordf91ef32009-04-18 00:34:01 +0000359 if (!FoundPCH && !FoundPTH) {
360 P.appendSuffix("gch");
Michael J. Spencer32bef4e2011-01-10 02:34:13 +0000361 if (!llvm::sys::fs::exists(P.str(), Exists) && Exists) {
Daniel Dunbar0ebd9322009-10-15 20:02:44 +0000362 FoundPCH = UsePCH;
363 FoundPTH = !UsePCH;
Douglas Gregordf91ef32009-04-18 00:34:01 +0000364 }
Mike Stump1eb44332009-09-09 15:08:12 +0000365 else
Douglas Gregordf91ef32009-04-18 00:34:01 +0000366 P.eraseSuffix();
367 }
368
369 if (FoundPCH || FoundPTH) {
Argyrios Kyrtzidis990142a2010-09-30 16:53:47 +0000370 if (IsFirstImplicitInclude) {
371 A->claim();
372 if (UsePCH)
373 CmdArgs.push_back("-include-pch");
374 else
375 CmdArgs.push_back("-include-pth");
376 CmdArgs.push_back(Args.MakeArgString(P.str()));
377 continue;
378 } else {
379 // Ignore the PCH if not first on command line and emit warning.
Chris Lattner5f9e2722011-07-23 10:55:15 +0000380 D.Diag(diag::warn_drv_pch_not_first_include)
Argyrios Kyrtzidis990142a2010-09-30 16:53:47 +0000381 << P.str() << A->getAsString(Args);
382 }
Daniel Dunbarc21c4852009-04-08 23:54:23 +0000383 }
384 }
385
386 // Not translated, render as usual.
387 A->claim();
388 A->render(Args, CmdArgs);
389 }
390
391 Args.AddAllArgs(CmdArgs, options::OPT_D, options::OPT_U);
Douglas Gregor65e02fa2011-07-28 04:45:53 +0000392 Args.AddAllArgs(CmdArgs, options::OPT_I_Group, options::OPT_F,
393 options::OPT_index_header_map);
Daniel Dunbarc21c4852009-04-08 23:54:23 +0000394
395 // Add -Wp, and -Xassembler if using the preprocessor.
396
397 // FIXME: There is a very unfortunate problem here, some troubled
398 // souls abuse -Wp, to pass preprocessor options in gcc syntax. To
399 // really support that we would have to parse and then translate
400 // those options. :(
401 Args.AddAllArgValues(CmdArgs, options::OPT_Wp_COMMA,
402 options::OPT_Xpreprocessor);
Daniel Dunbar607d7f62009-10-29 01:53:44 +0000403
404 // -I- is a deprecated GCC feature, reject it.
405 if (Arg *A = Args.getLastArg(options::OPT_I_))
Chris Lattner5f9e2722011-07-23 10:55:15 +0000406 D.Diag(diag::err_drv_I_dash_not_supported) << A->getAsString(Args);
Chandler Carruthfeee58c2010-10-20 07:00:47 +0000407
408 // If we have a --sysroot, and don't have an explicit -isysroot flag, add an
409 // -isysroot to the CC1 invocation.
Sebastian Pop4762a2d2012-04-16 04:16:43 +0000410 StringRef sysroot = C.getSysRoot();
411 if (sysroot != "") {
Chandler Carruthfeee58c2010-10-20 07:00:47 +0000412 if (!Args.hasArg(options::OPT_isysroot)) {
413 CmdArgs.push_back("-isysroot");
Sebastian Pop4762a2d2012-04-16 04:16:43 +0000414 CmdArgs.push_back(C.getArgs().MakeArgString(sysroot));
Chandler Carruthfeee58c2010-10-20 07:00:47 +0000415 }
416 }
Douglas Gregor8ee51ef2011-09-14 20:28:46 +0000417
418 // If a module path was provided, pass it along. Otherwise, use a temporary
419 // directory.
420 if (Arg *A = Args.getLastArg(options::OPT_fmodule_cache_path)) {
Douglas Gregor8ee51ef2011-09-14 20:28:46 +0000421 A->claim();
422 A->render(Args, CmdArgs);
423 } else {
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +0000424 SmallString<128> DefaultModuleCache;
Douglas Gregor8ee51ef2011-09-14 20:28:46 +0000425 llvm::sys::path::system_temp_directory(/*erasedOnReboot=*/false,
426 DefaultModuleCache);
427 llvm::sys::path::append(DefaultModuleCache, "clang-module-cache");
428 CmdArgs.push_back("-fmodule-cache-path");
429 CmdArgs.push_back(Args.MakeArgString(DefaultModuleCache));
430 }
Douglas Gregorfba18aa2011-09-15 22:00:41 +0000431
Benjamin Kramer47adebe2011-09-22 21:41:16 +0000432 // Parse additional include paths from environment variables.
Chandler Carruthb5870e72011-11-04 07:12:58 +0000433 // FIXME: We should probably sink the logic for handling these from the
434 // frontend into the driver. It will allow deleting 4 otherwise unused flags.
Benjamin Kramer47adebe2011-09-22 21:41:16 +0000435 // CPATH - included following the user specified includes (but prior to
436 // builtin and standard includes).
Bill Wendling3d717152012-03-12 22:10:06 +0000437 addDirectoryList(Args, CmdArgs, "-I", "CPATH");
Benjamin Kramer47adebe2011-09-22 21:41:16 +0000438 // C_INCLUDE_PATH - system includes enabled when compiling C.
Bill Wendling3d717152012-03-12 22:10:06 +0000439 addDirectoryList(Args, CmdArgs, "-c-isystem", "C_INCLUDE_PATH");
Benjamin Kramer47adebe2011-09-22 21:41:16 +0000440 // CPLUS_INCLUDE_PATH - system includes enabled when compiling C++.
Bill Wendling3d717152012-03-12 22:10:06 +0000441 addDirectoryList(Args, CmdArgs, "-cxx-isystem", "CPLUS_INCLUDE_PATH");
Benjamin Kramer47adebe2011-09-22 21:41:16 +0000442 // OBJC_INCLUDE_PATH - system includes enabled when compiling ObjC.
Bill Wendling3d717152012-03-12 22:10:06 +0000443 addDirectoryList(Args, CmdArgs, "-objc-isystem", "OBJC_INCLUDE_PATH");
Benjamin Kramer47adebe2011-09-22 21:41:16 +0000444 // OBJCPLUS_INCLUDE_PATH - system includes enabled when compiling ObjC++.
Bill Wendling3d717152012-03-12 22:10:06 +0000445 addDirectoryList(Args, CmdArgs, "-objcxx-isystem", "OBJCPLUS_INCLUDE_PATH");
Chandler Carruth88491fc2011-11-04 07:12:53 +0000446
Chandler Carruth88491fc2011-11-04 07:12:53 +0000447 // Add C++ include arguments, if needed.
Chandler Carrutha4614422011-11-04 07:43:33 +0000448 if (types::isCXX(Inputs[0].getType()))
Chandler Carruth7ffa0322011-11-04 07:34:47 +0000449 getToolChain().AddClangCXXStdlibIncludeArgs(Args, CmdArgs);
Chandler Carruth7d7e9f92011-11-05 20:17:13 +0000450
451 // Add system include arguments.
452 getToolChain().AddClangSystemIncludeArgs(Args, CmdArgs);
Daniel Dunbarc21c4852009-04-08 23:54:23 +0000453}
454
Daniel Dunbar1d65e4b2009-09-10 22:59:51 +0000455/// getLLVMArchSuffixForARM - Get the LLVM arch name to use for a particular
Daniel Dunbar728a5122009-09-10 06:49:20 +0000456/// CPU.
457//
458// FIXME: This is redundant with -mcpu, why does LLVM use this.
459// FIXME: tblgen this, or kill it!
Chris Lattner5f9e2722011-07-23 10:55:15 +0000460static const char *getLLVMArchSuffixForARM(StringRef CPU) {
Chad Rosierae1aee62011-10-07 17:48:56 +0000461 return llvm::StringSwitch<const char *>(CPU)
462 .Cases("arm7tdmi", "arm7tdmi-s", "arm710t", "v4t")
463 .Cases("arm720t", "arm9", "arm9tdmi", "v4t")
464 .Cases("arm920", "arm920t", "arm922t", "v4t")
465 .Cases("arm940t", "ep9312","v4t")
466 .Cases("arm10tdmi", "arm1020t", "v5")
467 .Cases("arm9e", "arm926ej-s", "arm946e-s", "v5e")
468 .Cases("arm966e-s", "arm968e-s", "arm10e", "v5e")
469 .Cases("arm1020e", "arm1022e", "xscale", "iwmmxt", "v5e")
470 .Cases("arm1136j-s", "arm1136jf-s", "arm1176jz-s", "v6")
471 .Cases("arm1176jzf-s", "mpcorenovfp", "mpcore", "v6")
472 .Cases("arm1156t2-s", "arm1156t2f-s", "v6t2")
Quentin Colombet74632aa2012-11-29 23:15:27 +0000473 .Cases("cortex-a5", "cortex-a8", "cortex-a9", "cortex-a15", "v7")
Quentin Colombetab137512012-12-21 17:57:47 +0000474 .Case("cortex-r5", "v7r")
Bob Wilson57f6d192012-03-21 17:19:12 +0000475 .Case("cortex-m3", "v7m")
Jim Grosbach69033132012-03-29 19:53:34 +0000476 .Case("cortex-m4", "v7m")
Bob Wilson57f6d192012-03-21 17:19:12 +0000477 .Case("cortex-m0", "v6m")
Bob Wilson336bfa32012-09-29 23:52:50 +0000478 .Case("cortex-a9-mp", "v7f")
479 .Case("swift", "v7s")
Chad Rosierae1aee62011-10-07 17:48:56 +0000480 .Default("");
Daniel Dunbar728a5122009-09-10 06:49:20 +0000481}
482
Benjamin Kramer92c4fd52012-06-26 22:20:06 +0000483/// getARMTargetCPU - Get the (LLVM) name of the ARM cpu we are targeting.
484//
485// FIXME: tblgen this.
486static std::string getARMTargetCPU(const ArgList &Args,
487 const llvm::Triple &Triple) {
488 // FIXME: Warn on inconsistent use of -mcpu and -march.
489
490 // If we have -mcpu=, use that.
491 if (Arg *A = Args.getLastArg(options::OPT_mcpu_EQ)) {
Richard Smith1d489cf2012-11-01 04:30:05 +0000492 StringRef MCPU = A->getValue();
Benjamin Kramer92c4fd52012-06-26 22:20:06 +0000493 // Handle -mcpu=native.
494 if (MCPU == "native")
495 return llvm::sys::getHostCPUName();
496 else
497 return MCPU;
498 }
499
500 StringRef MArch;
501 if (Arg *A = Args.getLastArg(options::OPT_march_EQ)) {
502 // Otherwise, if we have -march= choose the base CPU for that arch.
Richard Smith1d489cf2012-11-01 04:30:05 +0000503 MArch = A->getValue();
Benjamin Kramer92c4fd52012-06-26 22:20:06 +0000504 } else {
505 // Otherwise, use the Arch from the triple.
506 MArch = Triple.getArchName();
507 }
508
509 // Handle -march=native.
510 std::string NativeMArch;
511 if (MArch == "native") {
512 std::string CPU = llvm::sys::getHostCPUName();
513 if (CPU != "generic") {
514 // Translate the native cpu into the architecture. The switch below will
515 // then chose the minimum cpu for that arch.
516 NativeMArch = std::string("arm") + getLLVMArchSuffixForARM(CPU);
517 MArch = NativeMArch;
518 }
519 }
520
521 return llvm::StringSwitch<const char *>(MArch)
522 .Cases("armv2", "armv2a","arm2")
523 .Case("armv3", "arm6")
524 .Case("armv3m", "arm7m")
525 .Cases("armv4", "armv4t", "arm7tdmi")
526 .Cases("armv5", "armv5t", "arm10tdmi")
527 .Cases("armv5e", "armv5te", "arm1022e")
528 .Case("armv5tej", "arm926ej-s")
529 .Cases("armv6", "armv6k", "arm1136jf-s")
530 .Case("armv6j", "arm1136j-s")
531 .Cases("armv6z", "armv6zk", "arm1176jzf-s")
532 .Case("armv6t2", "arm1156t2-s")
533 .Cases("armv7", "armv7a", "armv7-a", "cortex-a8")
Bob Wilson336bfa32012-09-29 23:52:50 +0000534 .Cases("armv7f", "armv7-f", "cortex-a9-mp")
535 .Cases("armv7s", "armv7-s", "swift")
Benjamin Kramer92c4fd52012-06-26 22:20:06 +0000536 .Cases("armv7r", "armv7-r", "cortex-r4")
537 .Cases("armv7m", "armv7-m", "cortex-m3")
538 .Case("ep9312", "ep9312")
539 .Case("iwmmxt", "iwmmxt")
540 .Case("xscale", "xscale")
541 .Cases("armv6m", "armv6-m", "cortex-m0")
542 // If all else failed, return the most base CPU LLVM supports.
543 .Default("arm7tdmi");
544}
545
Daniel Dunbar1f95e652009-11-17 06:37:03 +0000546// FIXME: Move to target hook.
547static bool isSignedCharDefault(const llvm::Triple &Triple) {
548 switch (Triple.getArch()) {
549 default:
550 return true;
551
Jim Grosbach5b4e7b12011-05-24 15:40:46 +0000552 case llvm::Triple::arm:
Daniel Dunbar1f95e652009-11-17 06:37:03 +0000553 case llvm::Triple::ppc:
554 case llvm::Triple::ppc64:
Bob Wilson905c45f2011-10-14 05:03:44 +0000555 if (Triple.isOSDarwin())
Daniel Dunbar1f95e652009-11-17 06:37:03 +0000556 return true;
557 return false;
Daniel Dunbar1f95e652009-11-17 06:37:03 +0000558 }
559}
560
Chad Rosier99317272012-04-04 20:51:35 +0000561// Handle -mfpu=.
562//
563// FIXME: Centralize feature selection, defaulting shouldn't be also in the
564// frontend target.
565static void addFPUArgs(const Driver &D, const Arg *A, const ArgList &Args,
566 ArgStringList &CmdArgs) {
Richard Smith1d489cf2012-11-01 04:30:05 +0000567 StringRef FPU = A->getValue();
Chad Rosier99317272012-04-04 20:51:35 +0000568
569 // Set the target features based on the FPU.
570 if (FPU == "fpa" || FPU == "fpe2" || FPU == "fpe3" || FPU == "maverick") {
571 // Disable any default FPU support.
572 CmdArgs.push_back("-target-feature");
573 CmdArgs.push_back("-vfp2");
574 CmdArgs.push_back("-target-feature");
575 CmdArgs.push_back("-vfp3");
576 CmdArgs.push_back("-target-feature");
577 CmdArgs.push_back("-neon");
578 } else if (FPU == "vfp3-d16" || FPU == "vfpv3-d16") {
579 CmdArgs.push_back("-target-feature");
580 CmdArgs.push_back("+vfp3");
581 CmdArgs.push_back("-target-feature");
582 CmdArgs.push_back("+d16");
583 CmdArgs.push_back("-target-feature");
584 CmdArgs.push_back("-neon");
585 } else if (FPU == "vfp") {
586 CmdArgs.push_back("-target-feature");
587 CmdArgs.push_back("+vfp2");
588 CmdArgs.push_back("-target-feature");
589 CmdArgs.push_back("-neon");
590 } else if (FPU == "vfp3" || FPU == "vfpv3") {
591 CmdArgs.push_back("-target-feature");
592 CmdArgs.push_back("+vfp3");
593 CmdArgs.push_back("-target-feature");
594 CmdArgs.push_back("-neon");
595 } else if (FPU == "neon") {
596 CmdArgs.push_back("-target-feature");
597 CmdArgs.push_back("+neon");
598 } else
599 D.Diag(diag::err_drv_clang_unsupported) << A->getAsString(Args);
600}
601
Chad Rosier7a938fa2012-04-04 20:39:32 +0000602// Handle -mfpmath=.
603static void addFPMathArgs(const Driver &D, const Arg *A, const ArgList &Args,
Chad Rosier30fe6ba2012-04-04 22:13:40 +0000604 ArgStringList &CmdArgs, StringRef CPU) {
Richard Smith1d489cf2012-11-01 04:30:05 +0000605 StringRef FPMath = A->getValue();
Chad Rosier7a938fa2012-04-04 20:39:32 +0000606
607 // Set the target features based on the FPMath.
608 if (FPMath == "neon") {
609 CmdArgs.push_back("-target-feature");
610 CmdArgs.push_back("+neonfp");
Chad Rosier30fe6ba2012-04-04 22:13:40 +0000611
Silviu Baranga2df67ea2012-09-13 15:06:00 +0000612 if (CPU != "cortex-a8" && CPU != "cortex-a9" && CPU != "cortex-a9-mp" &&
Quentin Colombet74632aa2012-11-29 23:15:27 +0000613 CPU != "cortex-a15" && CPU != "cortex-a5")
Chad Rosier30fe6ba2012-04-04 22:13:40 +0000614 D.Diag(diag::err_drv_invalid_feature) << "-mfpmath=neon" << CPU;
615
Chad Rosier7a938fa2012-04-04 20:39:32 +0000616 } else if (FPMath == "vfp" || FPMath == "vfp2" || FPMath == "vfp3" ||
617 FPMath == "vfp4") {
618 CmdArgs.push_back("-target-feature");
619 CmdArgs.push_back("-neonfp");
Chad Rosier30fe6ba2012-04-04 22:13:40 +0000620
621 // FIXME: Add warnings when disabling a feature not present for a given CPU.
Chad Rosier7a938fa2012-04-04 20:39:32 +0000622 } else
623 D.Diag(diag::err_drv_clang_unsupported) << A->getAsString(Args);
624}
625
Anton Korobeynikove2571792012-04-09 13:38:30 +0000626// Select the float ABI as determined by -msoft-float, -mhard-float, and
627// -mfloat-abi=.
628static StringRef getARMFloatABI(const Driver &D,
629 const ArgList &Args,
630 const llvm::Triple &Triple) {
Chris Lattner5f9e2722011-07-23 10:55:15 +0000631 StringRef FloatABI;
Daniel Dunbarcbd19332009-09-10 23:00:09 +0000632 if (Arg *A = Args.getLastArg(options::OPT_msoft_float,
633 options::OPT_mhard_float,
634 options::OPT_mfloat_abi_EQ)) {
635 if (A->getOption().matches(options::OPT_msoft_float))
636 FloatABI = "soft";
637 else if (A->getOption().matches(options::OPT_mhard_float))
638 FloatABI = "hard";
639 else {
Richard Smith1d489cf2012-11-01 04:30:05 +0000640 FloatABI = A->getValue();
Daniel Dunbarcbd19332009-09-10 23:00:09 +0000641 if (FloatABI != "soft" && FloatABI != "softfp" && FloatABI != "hard") {
Chris Lattner5f9e2722011-07-23 10:55:15 +0000642 D.Diag(diag::err_drv_invalid_mfloat_abi)
Daniel Dunbarcbd19332009-09-10 23:00:09 +0000643 << A->getAsString(Args);
644 FloatABI = "soft";
645 }
646 }
647 }
648
649 // If unspecified, choose the default based on the platform.
650 if (FloatABI.empty()) {
Rafael Espindolabcd6df62010-06-28 17:18:09 +0000651 switch (Triple.getOS()) {
Bob Wilson905c45f2011-10-14 05:03:44 +0000652 case llvm::Triple::Darwin:
653 case llvm::Triple::MacOSX:
654 case llvm::Triple::IOS: {
Daniel Dunbarcbd19332009-09-10 23:00:09 +0000655 // Darwin defaults to "softfp" for v6 and v7.
656 //
657 // FIXME: Factor out an ARM class so we can cache the arch somewhere.
Benjamin Kramer92c4fd52012-06-26 22:20:06 +0000658 std::string ArchName =
Rafael Espindolabcd6df62010-06-28 17:18:09 +0000659 getLLVMArchSuffixForARM(getARMTargetCPU(Args, Triple));
Benjamin Kramer92c4fd52012-06-26 22:20:06 +0000660 if (StringRef(ArchName).startswith("v6") ||
661 StringRef(ArchName).startswith("v7"))
Daniel Dunbarcbd19332009-09-10 23:00:09 +0000662 FloatABI = "softfp";
663 else
664 FloatABI = "soft";
665 break;
666 }
667
Rafael Espindola27fa2362012-12-13 04:17:14 +0000668 case llvm::Triple::FreeBSD:
669 // FreeBSD defaults to soft float
670 FloatABI = "soft";
671 break;
672
Daniel Dunbarcbd19332009-09-10 23:00:09 +0000673 default:
Bob Wilsonfc2bd7c2011-02-04 17:59:28 +0000674 switch(Triple.getEnvironment()) {
Jiangning Liuff104a12012-07-31 08:06:29 +0000675 case llvm::Triple::GNUEABIHF:
676 FloatABI = "hard";
677 break;
Bob Wilsonfc2bd7c2011-02-04 17:59:28 +0000678 case llvm::Triple::GNUEABI:
679 FloatABI = "softfp";
680 break;
681 case llvm::Triple::EABI:
682 // EABI is always AAPCS, and if it was not marked 'hard', it's softfp
683 FloatABI = "softfp";
684 break;
Logan Chien94a71422012-09-02 09:30:11 +0000685 case llvm::Triple::Android: {
Benjamin Kramer92c4fd52012-06-26 22:20:06 +0000686 std::string ArchName =
Chandler Carruthb43550b2012-01-10 19:47:42 +0000687 getLLVMArchSuffixForARM(getARMTargetCPU(Args, Triple));
Benjamin Kramer92c4fd52012-06-26 22:20:06 +0000688 if (StringRef(ArchName).startswith("v7"))
Chandler Carruthb43550b2012-01-10 19:47:42 +0000689 FloatABI = "softfp";
690 else
691 FloatABI = "soft";
692 break;
693 }
Bob Wilsonfc2bd7c2011-02-04 17:59:28 +0000694 default:
695 // Assume "soft", but warn the user we are guessing.
696 FloatABI = "soft";
Chris Lattner5f9e2722011-07-23 10:55:15 +0000697 D.Diag(diag::warn_drv_assuming_mfloat_abi_is) << "soft";
Bob Wilsonfc2bd7c2011-02-04 17:59:28 +0000698 break;
699 }
Daniel Dunbarcbd19332009-09-10 23:00:09 +0000700 }
701 }
702
Anton Korobeynikove2571792012-04-09 13:38:30 +0000703 return FloatABI;
704}
705
706
707void Clang::AddARMTargetArgs(const ArgList &Args,
708 ArgStringList &CmdArgs,
709 bool KernelOrKext) const {
710 const Driver &D = getToolChain().getDriver();
Daniel Dunbar7a0c0642012-10-15 22:23:53 +0000711 // Get the effective triple, which takes into account the deployment target.
712 std::string TripleStr = getToolChain().ComputeEffectiveClangTriple(Args);
713 llvm::Triple Triple(TripleStr);
Daniel Dunbar2e4e1102012-10-22 18:30:51 +0000714 std::string CPUName = getARMTargetCPU(Args, Triple);
Anton Korobeynikove2571792012-04-09 13:38:30 +0000715
716 // Select the ABI to use.
717 //
718 // FIXME: Support -meabi.
719 const char *ABIName = 0;
720 if (Arg *A = Args.getLastArg(options::OPT_mabi_EQ)) {
Richard Smith1d489cf2012-11-01 04:30:05 +0000721 ABIName = A->getValue();
Daniel Dunbar2e4e1102012-10-22 18:30:51 +0000722 } else if (Triple.isOSDarwin()) {
723 // The backend is hardwired to assume AAPCS for M-class processors, ensure
724 // the frontend matches that.
725 if (StringRef(CPUName).startswith("cortex-m")) {
726 ABIName = "aapcs";
727 } else {
728 ABIName = "apcs-gnu";
729 }
Anton Korobeynikove2571792012-04-09 13:38:30 +0000730 } else {
731 // Select the default based on the platform.
732 switch(Triple.getEnvironment()) {
Logan Chien94a71422012-09-02 09:30:11 +0000733 case llvm::Triple::Android:
Anton Korobeynikove2571792012-04-09 13:38:30 +0000734 case llvm::Triple::GNUEABI:
Jiangning Liuff104a12012-07-31 08:06:29 +0000735 case llvm::Triple::GNUEABIHF:
Anton Korobeynikove2571792012-04-09 13:38:30 +0000736 ABIName = "aapcs-linux";
737 break;
738 case llvm::Triple::EABI:
739 ABIName = "aapcs";
740 break;
741 default:
742 ABIName = "apcs-gnu";
743 }
744 }
745 CmdArgs.push_back("-target-abi");
746 CmdArgs.push_back(ABIName);
747
748 // Set the CPU based on -march= and -mcpu=.
749 CmdArgs.push_back("-target-cpu");
Daniel Dunbar2e4e1102012-10-22 18:30:51 +0000750 CmdArgs.push_back(Args.MakeArgString(CPUName));
Anton Korobeynikove2571792012-04-09 13:38:30 +0000751
752 // Determine floating point ABI from the options & target defaults.
753 StringRef FloatABI = getARMFloatABI(D, Args, Triple);
Daniel Dunbarcbd19332009-09-10 23:00:09 +0000754 if (FloatABI == "soft") {
755 // Floating point operations and argument passing are soft.
756 //
757 // FIXME: This changes CPP defines, we need -target-soft-float.
Daniel Dunbar3b315262009-11-30 08:42:00 +0000758 CmdArgs.push_back("-msoft-float");
Daniel Dunbar87667aa2009-12-08 19:49:51 +0000759 CmdArgs.push_back("-mfloat-abi");
760 CmdArgs.push_back("soft");
Daniel Dunbarcbd19332009-09-10 23:00:09 +0000761 } else if (FloatABI == "softfp") {
762 // Floating point operations are hard, but argument passing is soft.
Daniel Dunbar87667aa2009-12-08 19:49:51 +0000763 CmdArgs.push_back("-mfloat-abi");
764 CmdArgs.push_back("soft");
Daniel Dunbarcbd19332009-09-10 23:00:09 +0000765 } else {
766 // Floating point operations and argument passing are hard.
767 assert(FloatABI == "hard" && "Invalid float abi!");
Daniel Dunbar87667aa2009-12-08 19:49:51 +0000768 CmdArgs.push_back("-mfloat-abi");
769 CmdArgs.push_back("hard");
Daniel Dunbarcbd19332009-09-10 23:00:09 +0000770 }
Daniel Dunbar97f52ac2009-12-19 04:15:38 +0000771
772 // Set appropriate target features for floating point mode.
773 //
774 // FIXME: Note, this is a hack, the LLVM backend doesn't actually use these
775 // yet (it uses the -mfloat-abi and -msoft-float options above), and it is
776 // stripped out by the ARM target.
777
778 // Use software floating point operations?
779 if (FloatABI == "soft") {
780 CmdArgs.push_back("-target-feature");
781 CmdArgs.push_back("+soft-float");
782 }
783
784 // Use software floating point argument passing?
785 if (FloatABI != "hard") {
786 CmdArgs.push_back("-target-feature");
787 CmdArgs.push_back("+soft-float-abi");
788 }
Daniel Dunbara91320b2009-12-21 23:28:17 +0000789
790 // Honor -mfpu=.
Chad Rosier99317272012-04-04 20:51:35 +0000791 if (const Arg *A = Args.getLastArg(options::OPT_mfpu_EQ))
Chad Rosierf80f2a52012-04-04 20:56:36 +0000792 addFPUArgs(D, A, Args, CmdArgs);
Daniel Dunbar7187fac2011-03-17 00:07:34 +0000793
Chad Rosier7a938fa2012-04-04 20:39:32 +0000794 // Honor -mfpmath=.
795 if (const Arg *A = Args.getLastArg(options::OPT_mfpmath_EQ))
Chad Rosier30fe6ba2012-04-04 22:13:40 +0000796 addFPMathArgs(D, A, Args, CmdArgs, getARMTargetCPU(Args, Triple));
Chad Rosier7a938fa2012-04-04 20:39:32 +0000797
Daniel Dunbar7187fac2011-03-17 00:07:34 +0000798 // Setting -msoft-float effectively disables NEON because of the GCC
799 // implementation, although the same isn't true of VFP or VFP3.
800 if (FloatABI == "soft") {
Daniel Dunbarfa41d692011-03-17 17:10:06 +0000801 CmdArgs.push_back("-target-feature");
802 CmdArgs.push_back("-neon");
803 }
804
805 // Kernel code has more strict alignment requirements.
806 if (KernelOrKext) {
Daniel Dunbar7a0c0642012-10-15 22:23:53 +0000807 if (Triple.getOS() != llvm::Triple::IOS || Triple.isOSVersionLT(6)) {
808 CmdArgs.push_back("-backend-option");
809 CmdArgs.push_back("-arm-long-calls");
810 }
Daniel Dunbarfa41d692011-03-17 17:10:06 +0000811
Daniel Dunbar3c66d302011-03-22 16:48:17 +0000812 CmdArgs.push_back("-backend-option");
Daniel Dunbarfa41d692011-03-17 17:10:06 +0000813 CmdArgs.push_back("-arm-strict-align");
Daniel Dunbarb5fbb892011-04-18 21:26:42 +0000814
815 // The kext linker doesn't know how to deal with movw/movt.
Daniel Dunbarb5fbb892011-04-18 21:26:42 +0000816 CmdArgs.push_back("-backend-option");
817 CmdArgs.push_back("-arm-darwin-use-movt=0");
Daniel Dunbar7187fac2011-03-17 00:07:34 +0000818 }
Chad Rosier1b906052011-08-26 00:26:29 +0000819
820 // Setting -mno-global-merge disables the codegen global merge pass. Setting
821 // -mglobal-merge has no effect as the pass is enabled by default.
822 if (Arg *A = Args.getLastArg(options::OPT_mglobal_merge,
823 options::OPT_mno_global_merge)) {
824 if (A->getOption().matches(options::OPT_mno_global_merge))
825 CmdArgs.push_back("-mno-global-merge");
826 }
Chad Rosieree9ad5c2012-05-16 20:40:09 +0000827
Chad Rosier005af272012-05-16 21:19:55 +0000828 if (Args.hasArg(options::OPT_mno_implicit_float))
Chad Rosieree9ad5c2012-05-16 20:40:09 +0000829 CmdArgs.push_back("-no-implicit-float");
Daniel Dunbarb163ef72009-09-10 04:57:17 +0000830}
831
Simon Atanasyan8e1c5982012-09-21 20:19:32 +0000832// Translate MIPS CPU name alias option to CPU name.
833static StringRef getMipsCPUFromAlias(const Arg &A) {
834 if (A.getOption().matches(options::OPT_mips32))
835 return "mips32";
836 if (A.getOption().matches(options::OPT_mips32r2))
837 return "mips32r2";
838 if (A.getOption().matches(options::OPT_mips64))
839 return "mips64";
840 if (A.getOption().matches(options::OPT_mips64r2))
841 return "mips64r2";
842 llvm_unreachable("Unexpected option");
843 return "";
844}
845
Simon Atanasyana2768be2012-04-07 22:09:23 +0000846// Get CPU and ABI names. They are not independent
847// so we have to calculate them together.
848static void getMipsCPUAndABI(const ArgList &Args,
849 const ToolChain &TC,
850 StringRef &CPUName,
851 StringRef &ABIName) {
Simon Atanasyan89d83ff2012-09-10 08:32:41 +0000852 const char *DefMips32CPU = "mips32";
853 const char *DefMips64CPU = "mips64";
Akira Hatanaka9f360622011-09-26 21:07:52 +0000854
Simon Atanasyan89d83ff2012-09-10 08:32:41 +0000855 if (Arg *A = Args.getLastArg(options::OPT_march_EQ,
Simon Atanasyan8e1c5982012-09-21 20:19:32 +0000856 options::OPT_mcpu_EQ,
857 options::OPT_mips_CPUs_Group)) {
858 if (A->getOption().matches(options::OPT_mips_CPUs_Group))
859 CPUName = getMipsCPUFromAlias(*A);
860 else
Richard Smith1d489cf2012-11-01 04:30:05 +0000861 CPUName = A->getValue();
Simon Atanasyan8e1c5982012-09-21 20:19:32 +0000862 }
Simon Atanasyan89d83ff2012-09-10 08:32:41 +0000863
Akira Hatanaka9f360622011-09-26 21:07:52 +0000864 if (Arg *A = Args.getLastArg(options::OPT_mabi_EQ))
Richard Smith1d489cf2012-11-01 04:30:05 +0000865 ABIName = A->getValue();
Simon Atanasyan89d83ff2012-09-10 08:32:41 +0000866
867 // Setup default CPU and ABI names.
868 if (CPUName.empty() && ABIName.empty()) {
869 switch (TC.getTriple().getArch()) {
870 default:
871 llvm_unreachable("Unexpected triple arch name");
872 case llvm::Triple::mips:
873 case llvm::Triple::mipsel:
874 CPUName = DefMips32CPU;
875 break;
876 case llvm::Triple::mips64:
877 case llvm::Triple::mips64el:
878 CPUName = DefMips64CPU;
879 break;
880 }
881 }
882
883 if (!ABIName.empty()) {
884 // Deduce CPU name from ABI name.
885 CPUName = llvm::StringSwitch<const char *>(ABIName)
886 .Cases("o32", "eabi", DefMips32CPU)
887 .Cases("n32", "n64", DefMips64CPU)
888 .Default("");
889 }
890 else if (!CPUName.empty()) {
891 // Deduce ABI name from CPU name.
892 ABIName = llvm::StringSwitch<const char *>(CPUName)
893 .Cases("mips32", "mips32r2", "o32")
894 .Cases("mips64", "mips64r2", "n64")
895 .Default("");
896 }
897
898 // FIXME: Warn on inconsistent cpu and abi usage.
Simon Atanasyana2768be2012-04-07 22:09:23 +0000899}
900
Simon Atanasyan5e627792012-06-02 15:06:29 +0000901// Select the MIPS float ABI as determined by -msoft-float, -mhard-float,
902// and -mfloat-abi=.
903static StringRef getMipsFloatABI(const Driver &D, const ArgList &Args) {
Akira Hatanakaad8d8a32012-03-23 23:07:09 +0000904 // Select the float ABI as determined by -msoft-float, -mhard-float,
905 // and -mfloat-abi=.
Chris Lattner5f9e2722011-07-23 10:55:15 +0000906 StringRef FloatABI;
Eric Christophered734732010-03-02 02:41:08 +0000907 if (Arg *A = Args.getLastArg(options::OPT_msoft_float,
Akira Hatanakaad8d8a32012-03-23 23:07:09 +0000908 options::OPT_mhard_float,
909 options::OPT_mfloat_abi_EQ)) {
Eric Christophered734732010-03-02 02:41:08 +0000910 if (A->getOption().matches(options::OPT_msoft_float))
911 FloatABI = "soft";
912 else if (A->getOption().matches(options::OPT_mhard_float))
913 FloatABI = "hard";
Akira Hatanakaad8d8a32012-03-23 23:07:09 +0000914 else {
Richard Smith1d489cf2012-11-01 04:30:05 +0000915 FloatABI = A->getValue();
Akira Hatanakaad8d8a32012-03-23 23:07:09 +0000916 if (FloatABI != "soft" && FloatABI != "single" && FloatABI != "hard") {
Simon Atanasyan5e627792012-06-02 15:06:29 +0000917 D.Diag(diag::err_drv_invalid_mfloat_abi) << A->getAsString(Args);
Akira Hatanakaad8d8a32012-03-23 23:07:09 +0000918 FloatABI = "hard";
919 }
920 }
Eric Christophered734732010-03-02 02:41:08 +0000921 }
922
923 // If unspecified, choose the default based on the platform.
924 if (FloatABI.empty()) {
Akira Hatanakaad8d8a32012-03-23 23:07:09 +0000925 // Assume "hard", because it's a default value used by gcc.
926 // When we start to recognize specific target MIPS processors,
927 // we will be able to select the default more correctly.
928 FloatABI = "hard";
Eric Christophered734732010-03-02 02:41:08 +0000929 }
930
Simon Atanasyan5e627792012-06-02 15:06:29 +0000931 return FloatABI;
932}
933
Simon Atanasyandc536f52012-07-05 18:51:43 +0000934static void AddTargetFeature(const ArgList &Args,
935 ArgStringList &CmdArgs,
936 OptSpecifier OnOpt,
937 OptSpecifier OffOpt,
938 StringRef FeatureName) {
939 if (Arg *A = Args.getLastArg(OnOpt, OffOpt)) {
940 CmdArgs.push_back("-target-feature");
941 if (A->getOption().matches(OnOpt))
942 CmdArgs.push_back(Args.MakeArgString("+" + FeatureName));
943 else
944 CmdArgs.push_back(Args.MakeArgString("-" + FeatureName));
945 }
946}
947
Simon Atanasyan5e627792012-06-02 15:06:29 +0000948void Clang::AddMIPSTargetArgs(const ArgList &Args,
949 ArgStringList &CmdArgs) const {
950 const Driver &D = getToolChain().getDriver();
951 StringRef CPUName;
952 StringRef ABIName;
953 getMipsCPUAndABI(Args, getToolChain(), CPUName, ABIName);
954
955 CmdArgs.push_back("-target-cpu");
956 CmdArgs.push_back(CPUName.data());
957
958 CmdArgs.push_back("-target-abi");
959 CmdArgs.push_back(ABIName.data());
960
961 StringRef FloatABI = getMipsFloatABI(D, Args);
962
Simon Atanasyane1d792f2013-01-10 12:36:19 +0000963 bool IsMips16 = Args.getLastArg(options::OPT_mips16) != NULL;
964
965 if (FloatABI == "soft" || (FloatABI == "hard" && IsMips16)) {
Eric Christophered734732010-03-02 02:41:08 +0000966 // Floating point operations and argument passing are soft.
Eric Christophered734732010-03-02 02:41:08 +0000967 CmdArgs.push_back("-msoft-float");
Akira Hatanakaad8d8a32012-03-23 23:07:09 +0000968 CmdArgs.push_back("-mfloat-abi");
969 CmdArgs.push_back("soft");
970
971 // FIXME: Note, this is a hack. We need to pass the selected float
972 // mode to the MipsTargetInfoBase to define appropriate macros there.
973 // Now it is the only method.
974 CmdArgs.push_back("-target-feature");
975 CmdArgs.push_back("+soft-float");
Simon Atanasyane1d792f2013-01-10 12:36:19 +0000976
977 if (FloatABI == "hard" && IsMips16) {
978 CmdArgs.push_back("-mllvm");
979 CmdArgs.push_back("-mips16-hard-float");
980 }
Akira Hatanakaad8d8a32012-03-23 23:07:09 +0000981 }
982 else if (FloatABI == "single") {
983 // Restrict the use of hardware floating-point
984 // instructions to 32-bit operations.
985 CmdArgs.push_back("-target-feature");
986 CmdArgs.push_back("+single-float");
987 }
988 else {
989 // Floating point operations and argument passing are hard.
Eric Christophered734732010-03-02 02:41:08 +0000990 assert(FloatABI == "hard" && "Invalid float abi!");
Akira Hatanakaad8d8a32012-03-23 23:07:09 +0000991 CmdArgs.push_back("-mfloat-abi");
992 CmdArgs.push_back("hard");
Eric Christophered734732010-03-02 02:41:08 +0000993 }
Simon Atanasyan0b273ef2012-07-05 14:19:39 +0000994
Simon Atanasyandc536f52012-07-05 18:51:43 +0000995 AddTargetFeature(Args, CmdArgs,
996 options::OPT_mips16, options::OPT_mno_mips16,
997 "mips16");
Simon Atanasyand797a852012-07-05 19:23:00 +0000998 AddTargetFeature(Args, CmdArgs,
999 options::OPT_mdsp, options::OPT_mno_dsp,
1000 "dsp");
1001 AddTargetFeature(Args, CmdArgs,
1002 options::OPT_mdspr2, options::OPT_mno_dspr2,
1003 "dspr2");
Simon Atanasyan9804b762012-08-27 20:55:56 +00001004
Simon Atanasyanbda07ac2012-12-01 18:27:21 +00001005 if (Arg *A = Args.getLastArg(options::OPT_mxgot, options::OPT_mno_xgot)) {
1006 if (A->getOption().matches(options::OPT_mxgot)) {
1007 CmdArgs.push_back("-mllvm");
1008 CmdArgs.push_back("-mxgot");
1009 }
1010 }
1011
Simon Atanasyan9804b762012-08-27 20:55:56 +00001012 if (Arg *A = Args.getLastArg(options::OPT_G)) {
Richard Smith1d489cf2012-11-01 04:30:05 +00001013 StringRef v = A->getValue();
Simon Atanasyan9804b762012-08-27 20:55:56 +00001014 CmdArgs.push_back("-mllvm");
1015 CmdArgs.push_back(Args.MakeArgString("-mips-ssection-threshold=" + v));
1016 A->claim();
1017 }
Eric Christophered734732010-03-02 02:41:08 +00001018}
1019
Hal Finkel02a84272012-06-11 22:35:19 +00001020/// getPPCTargetCPU - Get the (LLVM) name of the PowerPC cpu we are targeting.
1021static std::string getPPCTargetCPU(const ArgList &Args) {
1022 if (Arg *A = Args.getLastArg(options::OPT_mcpu_EQ)) {
Richard Smith1d489cf2012-11-01 04:30:05 +00001023 StringRef CPUName = A->getValue();
Hal Finkel02a84272012-06-11 22:35:19 +00001024
1025 if (CPUName == "native") {
1026 std::string CPU = llvm::sys::getHostCPUName();
1027 if (!CPU.empty() && CPU != "generic")
1028 return CPU;
1029 else
1030 return "";
1031 }
1032
1033 return llvm::StringSwitch<const char *>(CPUName)
1034 .Case("common", "generic")
1035 .Case("440", "440")
1036 .Case("440fp", "440")
1037 .Case("450", "450")
1038 .Case("601", "601")
1039 .Case("602", "602")
1040 .Case("603", "603")
1041 .Case("603e", "603e")
1042 .Case("603ev", "603ev")
1043 .Case("604", "604")
1044 .Case("604e", "604e")
1045 .Case("620", "620")
1046 .Case("G3", "g3")
1047 .Case("7400", "7400")
1048 .Case("G4", "g4")
1049 .Case("7450", "7450")
1050 .Case("G4+", "g4+")
1051 .Case("750", "750")
1052 .Case("970", "970")
1053 .Case("G5", "g5")
1054 .Case("a2", "a2")
Hal Finkel7de32962012-09-18 22:25:03 +00001055 .Case("e500mc", "e500mc")
1056 .Case("e5500", "e5500")
Hal Finkel02a84272012-06-11 22:35:19 +00001057 .Case("power6", "pwr6")
1058 .Case("power7", "pwr7")
1059 .Case("powerpc", "ppc")
1060 .Case("powerpc64", "ppc64")
1061 .Default("");
1062 }
1063
1064 return "";
1065}
1066
1067void Clang::AddPPCTargetArgs(const ArgList &Args,
1068 ArgStringList &CmdArgs) const {
1069 std::string TargetCPUName = getPPCTargetCPU(Args);
1070
1071 // LLVM may default to generating code for the native CPU,
1072 // but, like gcc, we default to a more generic option for
1073 // each architecture. (except on Darwin)
1074 llvm::Triple Triple = getToolChain().getTriple();
1075 if (TargetCPUName.empty() && !Triple.isOSDarwin()) {
1076 if (Triple.getArch() == llvm::Triple::ppc64)
1077 TargetCPUName = "ppc64";
1078 else
1079 TargetCPUName = "ppc";
1080 }
1081
1082 if (!TargetCPUName.empty()) {
1083 CmdArgs.push_back("-target-cpu");
1084 CmdArgs.push_back(Args.MakeArgString(TargetCPUName.c_str()));
1085 }
1086}
1087
Bruno Cardoso Lopes9284d212010-11-09 17:21:19 +00001088void Clang::AddSparcTargetArgs(const ArgList &Args,
1089 ArgStringList &CmdArgs) const {
1090 const Driver &D = getToolChain().getDriver();
1091
1092 if (const Arg *A = Args.getLastArg(options::OPT_march_EQ)) {
Bruno Cardoso Lopes9284d212010-11-09 17:21:19 +00001093 CmdArgs.push_back("-target-cpu");
Richard Smith1d489cf2012-11-01 04:30:05 +00001094 CmdArgs.push_back(A->getValue());
Bruno Cardoso Lopes9284d212010-11-09 17:21:19 +00001095 }
1096
1097 // Select the float ABI as determined by -msoft-float, -mhard-float, and
Chris Lattner5f9e2722011-07-23 10:55:15 +00001098 StringRef FloatABI;
Bruno Cardoso Lopes9284d212010-11-09 17:21:19 +00001099 if (Arg *A = Args.getLastArg(options::OPT_msoft_float,
1100 options::OPT_mhard_float)) {
1101 if (A->getOption().matches(options::OPT_msoft_float))
1102 FloatABI = "soft";
1103 else if (A->getOption().matches(options::OPT_mhard_float))
1104 FloatABI = "hard";
1105 }
1106
1107 // If unspecified, choose the default based on the platform.
1108 if (FloatABI.empty()) {
1109 switch (getToolChain().getTriple().getOS()) {
1110 default:
1111 // Assume "soft", but warn the user we are guessing.
1112 FloatABI = "soft";
Chris Lattner5f9e2722011-07-23 10:55:15 +00001113 D.Diag(diag::warn_drv_assuming_mfloat_abi_is) << "soft";
Bruno Cardoso Lopes9284d212010-11-09 17:21:19 +00001114 break;
1115 }
1116 }
1117
1118 if (FloatABI == "soft") {
1119 // Floating point operations and argument passing are soft.
1120 //
1121 // FIXME: This changes CPP defines, we need -target-soft-float.
1122 CmdArgs.push_back("-msoft-float");
Bruno Cardoso Lopes9284d212010-11-09 17:21:19 +00001123 CmdArgs.push_back("-target-feature");
1124 CmdArgs.push_back("+soft-float");
1125 } else {
1126 assert(FloatABI == "hard" && "Invalid float abi!");
1127 CmdArgs.push_back("-mhard-float");
1128 }
1129}
1130
Chandler Carruth700d4e42013-01-13 11:46:33 +00001131static const char *getX86TargetCPU(const ArgList &Args,
1132 const llvm::Triple &Triple) {
1133 if (const Arg *A = Args.getLastArg(options::OPT_march_EQ)) {
1134 if (StringRef(A->getValue()) != "native")
1135 return A->getValue();
1136
1137 // FIXME: Reject attempts to use -march=native unless the target matches
1138 // the host.
1139 //
1140 // FIXME: We should also incorporate the detected target features for use
1141 // with -native.
1142 std::string CPU = llvm::sys::getHostCPUName();
1143 if (!CPU.empty() && CPU != "generic")
1144 return Args.MakeArgString(CPU);
1145 }
1146
1147 // Select the default CPU if none was given (or detection failed).
1148
1149 if (Triple.getArch() != llvm::Triple::x86_64 &&
1150 Triple.getArch() != llvm::Triple::x86)
1151 return 0; // This routine is only handling x86 targets.
1152
1153 bool Is64Bit = Triple.getArch() == llvm::Triple::x86_64;
1154
1155 // FIXME: Need target hooks.
1156 if (Triple.isOSDarwin())
1157 return Is64Bit ? "core2" : "yonah";
1158
1159 // Everything else goes to x86-64 in 64-bit mode.
1160 if (Is64Bit)
1161 return "x86-64";
1162
1163 if (Triple.getOSName().startswith("haiku"))
1164 return "i586";
1165 if (Triple.getOSName().startswith("openbsd"))
1166 return "i486";
1167 if (Triple.getOSName().startswith("bitrig"))
1168 return "i686";
1169 if (Triple.getOSName().startswith("freebsd"))
1170 return "i486";
1171 if (Triple.getOSName().startswith("netbsd"))
1172 return "i486";
1173 // All x86 devices running Android have core2 as their common
1174 // denominator. This makes a better choice than pentium4.
1175 if (Triple.getEnvironment() == llvm::Triple::Android)
1176 return "core2";
1177
1178 // Fallback to p4.
1179 return "pentium4";
1180}
1181
Daniel Dunbar6acda162009-09-09 22:33:08 +00001182void Clang::AddX86TargetArgs(const ArgList &Args,
1183 ArgStringList &CmdArgs) const {
Daniel Dunbare6ad3f92009-09-10 22:59:57 +00001184 if (!Args.hasFlag(options::OPT_mred_zone,
1185 options::OPT_mno_red_zone,
1186 true) ||
1187 Args.hasArg(options::OPT_mkernel) ||
1188 Args.hasArg(options::OPT_fapple_kext))
Daniel Dunbar66861e02009-11-20 22:21:36 +00001189 CmdArgs.push_back("-disable-red-zone");
Daniel Dunbare6ad3f92009-09-10 22:59:57 +00001190
Daniel Dunbare6ad3f92009-09-10 22:59:57 +00001191 if (Args.hasFlag(options::OPT_msoft_float,
1192 options::OPT_mno_soft_float,
1193 false))
Daniel Dunbar66861e02009-11-20 22:21:36 +00001194 CmdArgs.push_back("-no-implicit-float");
Daniel Dunbare6ad3f92009-09-10 22:59:57 +00001195
Chandler Carruth700d4e42013-01-13 11:46:33 +00001196 if (const char *CPUName = getX86TargetCPU(Args, getToolChain().getTriple())) {
Daniel Dunbar38b48af2009-12-18 06:30:12 +00001197 CmdArgs.push_back("-target-cpu");
Daniel Dunbarf86fedd2009-11-14 22:04:54 +00001198 CmdArgs.push_back(CPUName);
1199 }
1200
Eli Friedmand18eeca2011-07-02 00:34:19 +00001201 // The required algorithm here is slightly strange: the options are applied
1202 // in order (so -mno-sse -msse2 disables SSE3), but any option that gets
1203 // directly overridden later is ignored (so "-mno-sse -msse2 -mno-sse2 -msse"
1204 // is equivalent to "-mno-sse2 -msse"). The -cc1 handling deals with the
1205 // former correctly, but not the latter; handle directly-overridden
1206 // attributes here.
1207 llvm::StringMap<unsigned> PrevFeature;
1208 std::vector<const char*> Features;
Daniel Dunbarcdd96862009-11-25 11:53:23 +00001209 for (arg_iterator it = Args.filtered_begin(options::OPT_m_x86_Features_Group),
1210 ie = Args.filtered_end(); it != ie; ++it) {
Chris Lattner5f9e2722011-07-23 10:55:15 +00001211 StringRef Name = (*it)->getOption().getName();
Daniel Dunbar7e4953e2010-06-11 22:00:13 +00001212 (*it)->claim();
Daniel Dunbar6acda162009-09-09 22:33:08 +00001213
Daniel Dunbarcdd96862009-11-25 11:53:23 +00001214 // Skip over "-m".
Michael J. Spencerc6357102012-10-22 22:13:48 +00001215 assert(Name.startswith("m") && "Invalid feature name.");
1216 Name = Name.substr(1);
Daniel Dunbar6acda162009-09-09 22:33:08 +00001217
Daniel Dunbarcdd96862009-11-25 11:53:23 +00001218 bool IsNegative = Name.startswith("no-");
1219 if (IsNegative)
1220 Name = Name.substr(3);
Daniel Dunbar6acda162009-09-09 22:33:08 +00001221
Eli Friedmand18eeca2011-07-02 00:34:19 +00001222 unsigned& Prev = PrevFeature[Name];
1223 if (Prev)
1224 Features[Prev - 1] = 0;
1225 Prev = Features.size() + 1;
1226 Features.push_back(Args.MakeArgString((IsNegative ? "-" : "+") + Name));
1227 }
1228 for (unsigned i = 0; i < Features.size(); i++) {
1229 if (Features[i]) {
1230 CmdArgs.push_back("-target-feature");
1231 CmdArgs.push_back(Features[i]);
1232 }
Daniel Dunbar6acda162009-09-09 22:33:08 +00001233 }
1234}
1235
Matthew Curtis33c95f12012-12-06 17:49:03 +00001236static inline bool HasPICArg(const ArgList &Args) {
1237 return Args.hasArg(options::OPT_fPIC)
1238 || Args.hasArg(options::OPT_fpic);
1239}
1240
1241static Arg *GetLastSmallDataThresholdArg(const ArgList &Args) {
1242 return Args.getLastArg(options::OPT_G,
1243 options::OPT_G_EQ,
1244 options::OPT_msmall_data_threshold_EQ);
1245}
1246
1247static std::string GetHexagonSmallDataThresholdValue(const ArgList &Args) {
1248 std::string value;
1249 if (HasPICArg(Args))
1250 value = "0";
1251 else if (Arg *A = GetLastSmallDataThresholdArg(Args)) {
1252 value = A->getValue();
1253 A->claim();
1254 }
1255 return value;
1256}
1257
Tony Linthicum96319392011-12-12 21:14:55 +00001258void Clang::AddHexagonTargetArgs(const ArgList &Args,
1259 ArgStringList &CmdArgs) const {
1260 llvm::Triple Triple = getToolChain().getTriple();
1261
1262 CmdArgs.push_back("-target-cpu");
Matthew Curtis67814152012-12-06 14:16:43 +00001263 CmdArgs.push_back(Args.MakeArgString(
1264 "hexagon"
1265 + toolchains::Hexagon_TC::GetTargetCPU(Args)));
Tony Linthicum96319392011-12-12 21:14:55 +00001266 CmdArgs.push_back("-fno-signed-char");
Matthew Curtis1dbaef52012-12-07 13:52:44 +00001267 CmdArgs.push_back("-mqdsp6-compat");
1268 CmdArgs.push_back("-Wreturn-type");
Tony Linthicum96319392011-12-12 21:14:55 +00001269
Matthew Curtis33c95f12012-12-06 17:49:03 +00001270 std::string SmallDataThreshold = GetHexagonSmallDataThresholdValue(Args);
1271 if (!SmallDataThreshold.empty()) {
Tony Linthicum96319392011-12-12 21:14:55 +00001272 CmdArgs.push_back ("-mllvm");
Matthew Curtis33c95f12012-12-06 17:49:03 +00001273 CmdArgs.push_back(Args.MakeArgString(
1274 "-hexagon-small-data-threshold=" + SmallDataThreshold));
Tony Linthicum96319392011-12-12 21:14:55 +00001275 }
1276
Sirish Pande5f9688b2012-05-10 20:19:54 +00001277 if (!Args.hasArg(options::OPT_fno_short_enums))
1278 CmdArgs.push_back("-fshort-enums");
1279 if (Args.getLastArg(options::OPT_mieee_rnd_near)) {
1280 CmdArgs.push_back ("-mllvm");
1281 CmdArgs.push_back ("-enable-hexagon-ieee-rnd-near");
1282 }
Tony Linthicum96319392011-12-12 21:14:55 +00001283 CmdArgs.push_back ("-mllvm");
1284 CmdArgs.push_back ("-machine-sink-split=0");
1285}
1286
Eric Christopher88b7cf02011-08-19 00:30:14 +00001287static bool
John McCall260611a2012-06-20 06:18:46 +00001288shouldUseExceptionTablesForObjCExceptions(const ObjCRuntime &runtime,
Anders Carlsson525544d2011-02-28 00:44:51 +00001289 const llvm::Triple &Triple) {
1290 // We use the zero-cost exception tables for Objective-C if the non-fragile
1291 // ABI is enabled or when compiling for x86_64 and ARM on Snow Leopard and
1292 // later.
John McCall260611a2012-06-20 06:18:46 +00001293 if (runtime.isNonFragile())
Anders Carlsson525544d2011-02-28 00:44:51 +00001294 return true;
1295
Bob Wilson905c45f2011-10-14 05:03:44 +00001296 if (!Triple.isOSDarwin())
Anders Carlsson525544d2011-02-28 00:44:51 +00001297 return false;
1298
Eric Christopheraa7333c2011-07-02 00:20:22 +00001299 return (!Triple.isMacOSXVersionLT(10,5) &&
Anders Carlsson525544d2011-02-28 00:44:51 +00001300 (Triple.getArch() == llvm::Triple::x86_64 ||
Eric Christopher88b7cf02011-08-19 00:30:14 +00001301 Triple.getArch() == llvm::Triple::arm));
Anders Carlsson525544d2011-02-28 00:44:51 +00001302}
1303
Anders Carlsson15348ae2011-02-28 02:27:16 +00001304/// addExceptionArgs - Adds exception related arguments to the driver command
1305/// arguments. There's a master flag, -fexceptions and also language specific
1306/// flags to enable/disable C++ and Objective-C exceptions.
1307/// This makes it possible to for example disable C++ exceptions but enable
1308/// Objective-C exceptions.
1309static void addExceptionArgs(const ArgList &Args, types::ID InputType,
1310 const llvm::Triple &Triple,
Fariborz Jahanian15b77312012-04-04 18:28:00 +00001311 bool KernelOrKext,
John McCall260611a2012-06-20 06:18:46 +00001312 const ObjCRuntime &objcRuntime,
Anders Carlsson15348ae2011-02-28 02:27:16 +00001313 ArgStringList &CmdArgs) {
Chad Rosierafc4baa2012-03-26 22:04:46 +00001314 if (KernelOrKext) {
1315 // -mkernel and -fapple-kext imply no exceptions, so claim exception related
1316 // arguments now to avoid warnings about unused arguments.
1317 Args.ClaimAllArgs(options::OPT_fexceptions);
1318 Args.ClaimAllArgs(options::OPT_fno_exceptions);
1319 Args.ClaimAllArgs(options::OPT_fobjc_exceptions);
1320 Args.ClaimAllArgs(options::OPT_fno_objc_exceptions);
1321 Args.ClaimAllArgs(options::OPT_fcxx_exceptions);
1322 Args.ClaimAllArgs(options::OPT_fno_cxx_exceptions);
Anders Carlsson15348ae2011-02-28 02:27:16 +00001323 return;
Chad Rosierafc4baa2012-03-26 22:04:46 +00001324 }
Anders Carlsson15348ae2011-02-28 02:27:16 +00001325
1326 // Exceptions are enabled by default.
1327 bool ExceptionsEnabled = true;
1328
1329 // This keeps track of whether exceptions were explicitly turned on or off.
1330 bool DidHaveExplicitExceptionFlag = false;
1331
Rafael Espindolaf759df02009-10-01 13:33:33 +00001332 if (Arg *A = Args.getLastArg(options::OPT_fexceptions,
1333 options::OPT_fno_exceptions)) {
1334 if (A->getOption().matches(options::OPT_fexceptions))
Anders Carlsson15348ae2011-02-28 02:27:16 +00001335 ExceptionsEnabled = true;
Eric Christopher88b7cf02011-08-19 00:30:14 +00001336 else
Anders Carlsson15348ae2011-02-28 02:27:16 +00001337 ExceptionsEnabled = false;
1338
1339 DidHaveExplicitExceptionFlag = true;
Rafael Espindolaf759df02009-10-01 13:33:33 +00001340 }
Daniel Dunbar1a2cd4f2010-09-14 23:12:31 +00001341
Anders Carlsson15348ae2011-02-28 02:27:16 +00001342 bool ShouldUseExceptionTables = false;
Fariborz Jahanian85caf032009-10-01 20:30:46 +00001343
Anders Carlsson15348ae2011-02-28 02:27:16 +00001344 // Exception tables and cleanups can be enabled with -fexceptions even if the
1345 // language itself doesn't support exceptions.
1346 if (ExceptionsEnabled && DidHaveExplicitExceptionFlag)
1347 ShouldUseExceptionTables = true;
Daniel Dunbar1a2cd4f2010-09-14 23:12:31 +00001348
Daniel Dunbard47ea692011-03-17 23:28:31 +00001349 // Obj-C exceptions are enabled by default, regardless of -fexceptions. This
1350 // is not necessarily sensible, but follows GCC.
1351 if (types::isObjC(InputType) &&
Eric Christopher88b7cf02011-08-19 00:30:14 +00001352 Args.hasFlag(options::OPT_fobjc_exceptions,
Daniel Dunbard47ea692011-03-17 23:28:31 +00001353 options::OPT_fno_objc_exceptions,
1354 true)) {
1355 CmdArgs.push_back("-fobjc-exceptions");
Anders Carlsson15348ae2011-02-28 02:27:16 +00001356
Eric Christopher88b7cf02011-08-19 00:30:14 +00001357 ShouldUseExceptionTables |=
John McCall260611a2012-06-20 06:18:46 +00001358 shouldUseExceptionTablesForObjCExceptions(objcRuntime, Triple);
Anders Carlsson15348ae2011-02-28 02:27:16 +00001359 }
1360
1361 if (types::isCXX(InputType)) {
1362 bool CXXExceptionsEnabled = ExceptionsEnabled;
1363
Eric Christopher88b7cf02011-08-19 00:30:14 +00001364 if (Arg *A = Args.getLastArg(options::OPT_fcxx_exceptions,
1365 options::OPT_fno_cxx_exceptions,
Anders Carlsson15348ae2011-02-28 02:27:16 +00001366 options::OPT_fexceptions,
1367 options::OPT_fno_exceptions)) {
1368 if (A->getOption().matches(options::OPT_fcxx_exceptions))
1369 CXXExceptionsEnabled = true;
Chandler Carruth43f220f2011-02-28 07:25:18 +00001370 else if (A->getOption().matches(options::OPT_fno_cxx_exceptions))
Anders Carlsson15348ae2011-02-28 02:27:16 +00001371 CXXExceptionsEnabled = false;
1372 }
1373
1374 if (CXXExceptionsEnabled) {
1375 CmdArgs.push_back("-fcxx-exceptions");
1376
1377 ShouldUseExceptionTables = true;
1378 }
1379 }
1380
1381 if (ShouldUseExceptionTables)
1382 CmdArgs.push_back("-fexceptions");
Rafael Espindolaf759df02009-10-01 13:33:33 +00001383}
1384
Rafael Espindola61b1efe2011-05-02 17:43:32 +00001385static bool ShouldDisableCFI(const ArgList &Args,
1386 const ToolChain &TC) {
Rafael Espindola701ec8d2012-03-08 14:39:55 +00001387 bool Default = true;
Bob Wilson905c45f2011-10-14 05:03:44 +00001388 if (TC.getTriple().isOSDarwin()) {
Rafael Espindola97f6abb2011-05-17 16:26:17 +00001389 // The native darwin assembler doesn't support cfi directives, so
Rafael Espindolacb773922011-05-17 19:06:58 +00001390 // we disable them if we think the .s file will be passed to it.
Rafael Espindola701ec8d2012-03-08 14:39:55 +00001391 Default = Args.hasFlag(options::OPT_integrated_as,
Eric Christopher27e2b982012-12-18 00:31:10 +00001392 options::OPT_no_integrated_as,
1393 TC.IsIntegratedAssemblerDefault());
Rafael Espindola97f6abb2011-05-17 16:26:17 +00001394 }
Rafael Espindola701ec8d2012-03-08 14:39:55 +00001395 return !Args.hasFlag(options::OPT_fdwarf2_cfi_asm,
Eric Christopher27e2b982012-12-18 00:31:10 +00001396 options::OPT_fno_dwarf2_cfi_asm,
1397 Default);
Rafael Espindola61b1efe2011-05-02 17:43:32 +00001398}
1399
Nick Lewyckyea523d72011-10-17 23:05:52 +00001400static bool ShouldDisableDwarfDirectory(const ArgList &Args,
1401 const ToolChain &TC) {
1402 bool IsIADefault = TC.IsIntegratedAssemblerDefault();
1403 bool UseIntegratedAs = Args.hasFlag(options::OPT_integrated_as,
1404 options::OPT_no_integrated_as,
1405 IsIADefault);
1406 bool UseDwarfDirectory = Args.hasFlag(options::OPT_fdwarf_directory_asm,
1407 options::OPT_fno_dwarf_directory_asm,
1408 UseIntegratedAs);
1409 return !UseDwarfDirectory;
1410}
1411
Joerg Sonnenberger359cf922011-05-06 14:35:16 +00001412/// \brief Check whether the given input tree contains any compilation actions.
1413static bool ContainsCompileAction(const Action *A) {
Chris Lattner5f9e2722011-07-23 10:55:15 +00001414 if (isa<CompileJobAction>(A))
Joerg Sonnenberger359cf922011-05-06 14:35:16 +00001415 return true;
1416
1417 for (Action::const_iterator it = A->begin(), ie = A->end(); it != ie; ++it)
1418 if (ContainsCompileAction(*it))
1419 return true;
1420
1421 return false;
1422}
1423
1424/// \brief Check if -relax-all should be passed to the internal assembler.
1425/// This is done by default when compiling non-assembler source with -O0.
1426static bool UseRelaxAll(Compilation &C, const ArgList &Args) {
1427 bool RelaxDefault = true;
1428
1429 if (Arg *A = Args.getLastArg(options::OPT_O_Group))
1430 RelaxDefault = A->getOption().matches(options::OPT_O0);
1431
1432 if (RelaxDefault) {
1433 RelaxDefault = false;
1434 for (ActionList::const_iterator it = C.getActions().begin(),
1435 ie = C.getActions().end(); it != ie; ++it) {
1436 if (ContainsCompileAction(*it)) {
1437 RelaxDefault = true;
1438 break;
1439 }
1440 }
1441 }
1442
1443 return Args.hasFlag(options::OPT_mrelax_all, options::OPT_mno_relax_all,
1444 RelaxDefault);
1445}
1446
Alexey Samsonova40548c2013-01-16 11:34:36 +00001447SanitizerArgs::SanitizerArgs(const Driver &D, const ArgList &Args)
Alexey Samsonov4bdc6042013-01-20 13:12:12 +00001448 : Kind(0), BlacklistFile(""), MsanTrackOrigins(false),
1449 AsanZeroBaseShadow(false) {
Alexey Samsonov3e335c12013-01-28 07:20:44 +00001450 unsigned AllKinds = 0; // All kinds of sanitizers that were turned on
1451 // at least once (possibly, disabled further).
Richard Smithc4dabad2012-11-05 22:04:41 +00001452 for (ArgList::const_iterator I = Args.begin(), E = Args.end(); I != E; ++I) {
Alexey Samsonov3325b162012-11-28 17:34:24 +00001453 unsigned Add, Remove;
1454 if (!parse(D, Args, *I, Add, Remove, true))
Richard Smithc4dabad2012-11-05 22:04:41 +00001455 continue;
Richard Smithc4dabad2012-11-05 22:04:41 +00001456 (*I)->claim();
Alexey Samsonovbb1071c2012-11-06 15:09:03 +00001457 Kind |= Add;
1458 Kind &= ~Remove;
Alexey Samsonov3e335c12013-01-28 07:20:44 +00001459 AllKinds |= Add;
Richard Smithc4dabad2012-11-05 22:04:41 +00001460 }
1461
Chad Rosier78d85b12013-01-29 23:31:22 +00001462 UbsanTrapOnError =
1463 Args.hasArg(options::OPT_fcatch_undefined_behavior) ||
1464 Args.hasFlag(options::OPT_fsanitize_undefined_trap_on_error,
1465 options::OPT_fno_sanitize_undefined_trap_on_error, false);
1466
1467 if (Args.hasArg(options::OPT_fcatch_undefined_behavior) &&
1468 !Args.hasFlag(options::OPT_fsanitize_undefined_trap_on_error,
1469 options::OPT_fno_sanitize_undefined_trap_on_error, true)) {
1470 D.Diag(diag::err_drv_argument_not_allowed_with)
1471 << "-fcatch-undefined-behavior"
1472 << "-fno-sanitize-undefined-trap-on-error";
1473 }
1474
1475 // Warn about undefined sanitizer options that require runtime support.
1476 if (UbsanTrapOnError && notAllowedWithTrap()) {
1477 if (Args.hasArg(options::OPT_fcatch_undefined_behavior))
1478 D.Diag(diag::err_drv_argument_not_allowed_with)
1479 << lastArgumentForKind(D, Args, NotAllowedWithTrap)
1480 << "-fcatch-undefined-behavior";
1481 else if (Args.hasFlag(options::OPT_fsanitize_undefined_trap_on_error,
1482 options::OPT_fno_sanitize_undefined_trap_on_error,
1483 false))
1484 D.Diag(diag::err_drv_argument_not_allowed_with)
1485 << lastArgumentForKind(D, Args, NotAllowedWithTrap)
1486 << "-fsanitize-undefined-trap-on-error";
1487 }
1488
Richard Smithc4dabad2012-11-05 22:04:41 +00001489 // Only one runtime library can be used at once.
Alexey Samsonovbb1071c2012-11-06 15:09:03 +00001490 bool NeedsAsan = needsAsanRt();
1491 bool NeedsTsan = needsTsanRt();
Evgeniy Stepanov99469f72012-12-05 13:37:12 +00001492 bool NeedsMsan = needsMsanRt();
Richard Smith05650372012-12-01 01:02:45 +00001493 if (NeedsAsan && NeedsTsan)
Richard Smithc4dabad2012-11-05 22:04:41 +00001494 D.Diag(diag::err_drv_argument_not_allowed_with)
Richard Smith05650372012-12-01 01:02:45 +00001495 << lastArgumentForKind(D, Args, NeedsAsanRt)
1496 << lastArgumentForKind(D, Args, NeedsTsanRt);
Evgeniy Stepanov99469f72012-12-05 13:37:12 +00001497 if (NeedsAsan && NeedsMsan)
1498 D.Diag(diag::err_drv_argument_not_allowed_with)
1499 << lastArgumentForKind(D, Args, NeedsAsanRt)
1500 << lastArgumentForKind(D, Args, NeedsMsanRt);
1501 if (NeedsTsan && NeedsMsan)
1502 D.Diag(diag::err_drv_argument_not_allowed_with)
1503 << lastArgumentForKind(D, Args, NeedsTsanRt)
1504 << lastArgumentForKind(D, Args, NeedsMsanRt);
Alexey Samsonov4d1a6e42012-11-29 22:36:21 +00001505
1506 // If -fsanitize contains extra features of ASan, it should also
Alexey Samsonov3e335c12013-01-28 07:20:44 +00001507 // explicitly contain -fsanitize=address (probably, turned off later in the
1508 // command line).
1509 if ((Kind & AddressFull) != 0 && (AllKinds & Address) == 0)
1510 D.Diag(diag::warn_drv_unused_sanitizer)
1511 << lastArgumentForKind(D, Args, AddressFull)
1512 << "-fsanitize=address";
Alexey Samsonov91ecfa62012-12-03 19:12:58 +00001513
1514 // Parse -f(no-)sanitize-blacklist options.
1515 if (Arg *BLArg = Args.getLastArg(options::OPT_fsanitize_blacklist,
1516 options::OPT_fno_sanitize_blacklist)) {
1517 if (BLArg->getOption().matches(options::OPT_fsanitize_blacklist)) {
1518 std::string BLPath = BLArg->getValue();
1519 bool BLExists = false;
1520 if (!llvm::sys::fs::exists(BLPath, BLExists) && BLExists)
1521 BlacklistFile = BLPath;
1522 else
1523 D.Diag(diag::err_drv_no_such_file) << BLPath;
1524 }
1525 }
Evgeniy Stepanov34ef11b2012-12-24 08:42:34 +00001526
1527 // Parse -f(no-)sanitize-memory-track-origins options.
Alexey Samsonov4bdc6042013-01-20 13:12:12 +00001528 if (NeedsMsan)
Evgeniy Stepanov34ef11b2012-12-24 08:42:34 +00001529 MsanTrackOrigins =
1530 Args.hasFlag(options::OPT_fsanitize_memory_track_origins,
1531 options::OPT_fno_sanitize_memory_track_origins,
1532 /* Default */false);
Alexey Samsonov4bdc6042013-01-20 13:12:12 +00001533
1534 // Parse -f(no-)sanitize-address-zero-base-shadow options.
1535 if (NeedsAsan)
1536 AsanZeroBaseShadow =
1537 Args.hasFlag(options::OPT_fsanitize_address_zero_base_shadow,
1538 options::OPT_fno_sanitize_address_zero_base_shadow,
1539 /* Default */false);
Richard Smithc4dabad2012-11-05 22:04:41 +00001540}
1541
Kostya Serebryanydff466c2011-11-30 01:39:16 +00001542/// If AddressSanitizer is enabled, add appropriate linker flags (Linux).
1543/// This needs to be called before we add the C run-time (malloc, etc).
1544static void addAsanRTLinux(const ToolChain &TC, const ArgList &Args,
Kostya Serebryany7b5f1012011-12-06 19:18:44 +00001545 ArgStringList &CmdArgs) {
Logan Chien94a71422012-09-02 09:30:11 +00001546 if(TC.getTriple().getEnvironment() == llvm::Triple::Android) {
Evgeniy Stepanova6ddc022012-04-25 08:59:22 +00001547 if (!Args.hasArg(options::OPT_shared)) {
Evgeniy Stepanov83738622012-06-04 11:15:05 +00001548 if (!Args.hasArg(options::OPT_pie))
1549 TC.getDriver().Diag(diag::err_drv_asan_android_requires_pie);
Evgeniy Stepanova6ddc022012-04-25 08:59:22 +00001550 }
Daniel Dunbar8cd0d252011-12-07 23:22:17 +00001551
Evgeniy Stepanov8ba75412012-09-12 09:09:08 +00001552 SmallString<128> LibAsan(TC.getDriver().ResourceDir);
1553 llvm::sys::path::append(LibAsan, "lib", "linux",
1554 (Twine("libclang_rt.asan-") +
1555 TC.getArchName() + "-android.so"));
Matt Beaumont-Gay45b27382012-12-04 21:18:26 +00001556 CmdArgs.insert(CmdArgs.begin(), Args.MakeArgString(LibAsan));
Evgeniy Stepanova6ddc022012-04-25 08:59:22 +00001557 } else {
1558 if (!Args.hasArg(options::OPT_shared)) {
Alexey Samsonov4bdc6042013-01-20 13:12:12 +00001559 bool ZeroBaseShadow = Args.hasFlag(
1560 options::OPT_fsanitize_address_zero_base_shadow,
1561 options::OPT_fno_sanitize_address_zero_base_shadow, false);
1562 if (ZeroBaseShadow && !Args.hasArg(options::OPT_pie)) {
1563 TC.getDriver().Diag(diag::err_drv_argument_only_allowed_with) <<
1564 "-fsanitize-address-zero-base-shadow" << "-pie";
1565 }
Evgeniy Stepanova6ddc022012-04-25 08:59:22 +00001566 // LibAsan is "libclang_rt.asan-<ArchName>.a" in the Linux library
1567 // resource directory.
1568 SmallString<128> LibAsan(TC.getDriver().ResourceDir);
1569 llvm::sys::path::append(LibAsan, "lib", "linux",
1570 (Twine("libclang_rt.asan-") +
1571 TC.getArchName() + ".a"));
Matt Beaumont-Gay45b27382012-12-04 21:18:26 +00001572 // The ASan runtime needs to come before -lstdc++ (or -lc++, libstdc++.a,
1573 // etc.) so that the linker picks ASan's versions of the global 'operator
1574 // new' and 'operator delete' symbols. We take the extreme (but simple)
Chandler Carruth1d153982012-12-04 22:54:37 +00001575 // strategy of inserting it at the front of the link command. It also
1576 // needs to be forced to end up in the executable, so wrap it in
1577 // whole-archive.
1578 SmallVector<const char*, 3> PrefixArgs;
1579 PrefixArgs.push_back("-whole-archive");
1580 PrefixArgs.push_back(Args.MakeArgString(LibAsan));
1581 PrefixArgs.push_back("-no-whole-archive");
1582 CmdArgs.insert(CmdArgs.begin(), PrefixArgs.begin(), PrefixArgs.end());
Evgeniy Stepanova6ddc022012-04-25 08:59:22 +00001583 CmdArgs.push_back("-lpthread");
1584 CmdArgs.push_back("-ldl");
1585 CmdArgs.push_back("-export-dynamic");
1586 }
1587 }
Kostya Serebryanydff466c2011-11-30 01:39:16 +00001588}
1589
Kostya Serebryanyf7efb0e2012-05-16 06:36:00 +00001590/// If ThreadSanitizer is enabled, add appropriate linker flags (Linux).
1591/// This needs to be called before we add the C run-time (malloc, etc).
1592static void addTsanRTLinux(const ToolChain &TC, const ArgList &Args,
1593 ArgStringList &CmdArgs) {
Kostya Serebryanyf7efb0e2012-05-16 06:36:00 +00001594 if (!Args.hasArg(options::OPT_shared)) {
Evgeniy Stepanov09ccf392012-12-03 13:20:43 +00001595 if (!Args.hasArg(options::OPT_pie))
Evgeniy Stepanov99469f72012-12-05 13:37:12 +00001596 TC.getDriver().Diag(diag::err_drv_argument_only_allowed_with) <<
1597 "-fsanitize=thread" << "-pie";
Kostya Serebryanyf7efb0e2012-05-16 06:36:00 +00001598 // LibTsan is "libclang_rt.tsan-<ArchName>.a" in the Linux library
1599 // resource directory.
1600 SmallString<128> LibTsan(TC.getDriver().ResourceDir);
1601 llvm::sys::path::append(LibTsan, "lib", "linux",
1602 (Twine("libclang_rt.tsan-") +
1603 TC.getArchName() + ".a"));
1604 CmdArgs.push_back(Args.MakeArgString(LibTsan));
1605 CmdArgs.push_back("-lpthread");
1606 CmdArgs.push_back("-ldl");
1607 CmdArgs.push_back("-export-dynamic");
1608 }
1609}
1610
Evgeniy Stepanov09ccf392012-12-03 13:20:43 +00001611/// If MemorySanitizer is enabled, add appropriate linker flags (Linux).
1612/// This needs to be called before we add the C run-time (malloc, etc).
1613static void addMsanRTLinux(const ToolChain &TC, const ArgList &Args,
1614 ArgStringList &CmdArgs) {
1615 if (!Args.hasArg(options::OPT_shared)) {
1616 if (!Args.hasArg(options::OPT_pie))
Evgeniy Stepanov99469f72012-12-05 13:37:12 +00001617 TC.getDriver().Diag(diag::err_drv_argument_only_allowed_with) <<
1618 "-fsanitize=memory" << "-pie";
Evgeniy Stepanov09ccf392012-12-03 13:20:43 +00001619 // LibMsan is "libclang_rt.msan-<ArchName>.a" in the Linux library
1620 // resource directory.
1621 SmallString<128> LibMsan(TC.getDriver().ResourceDir);
1622 llvm::sys::path::append(LibMsan, "lib", "linux",
1623 (Twine("libclang_rt.msan-") +
1624 TC.getArchName() + ".a"));
1625 CmdArgs.push_back(Args.MakeArgString(LibMsan));
1626 CmdArgs.push_back("-lpthread");
1627 CmdArgs.push_back("-ldl");
1628 CmdArgs.push_back("-export-dynamic");
1629 }
1630}
1631
Richard Smith4def70d2012-10-09 19:52:38 +00001632/// If UndefinedBehaviorSanitizer is enabled, add appropriate linker flags
1633/// (Linux).
1634static void addUbsanRTLinux(const ToolChain &TC, const ArgList &Args,
1635 ArgStringList &CmdArgs) {
Richard Smith79188ae2013-01-18 22:09:26 +00001636 // LibUbsan is "libclang_rt.ubsan-<ArchName>.a" in the Linux library
1637 // resource directory.
1638 SmallString<128> LibUbsan(TC.getDriver().ResourceDir);
1639 llvm::sys::path::append(LibUbsan, "lib", "linux",
1640 (Twine("libclang_rt.ubsan-") +
1641 TC.getArchName() + ".a"));
1642 CmdArgs.push_back(Args.MakeArgString(LibUbsan));
1643 CmdArgs.push_back("-lpthread");
1644 CmdArgs.push_back("-export-dynamic");
Richard Smith4def70d2012-10-09 19:52:38 +00001645}
1646
Rafael Espindola6af27ec2011-12-14 21:02:23 +00001647static bool shouldUseFramePointer(const ArgList &Args,
1648 const llvm::Triple &Triple) {
1649 if (Arg *A = Args.getLastArg(options::OPT_fno_omit_frame_pointer,
1650 options::OPT_fomit_frame_pointer))
1651 return A->getOption().matches(options::OPT_fno_omit_frame_pointer);
1652
Rafael Espindolaa2a17892011-12-14 21:50:24 +00001653 // Don't use a frame pointer on linux x86 and x86_64 if optimizing.
Rafael Espindola6af27ec2011-12-14 21:02:23 +00001654 if ((Triple.getArch() == llvm::Triple::x86_64 ||
1655 Triple.getArch() == llvm::Triple::x86) &&
1656 Triple.getOS() == llvm::Triple::Linux) {
1657 if (Arg *A = Args.getLastArg(options::OPT_O_Group))
1658 if (!A->getOption().matches(options::OPT_O0))
1659 return false;
1660 }
1661
1662 return true;
1663}
1664
Chandler Carruthd566df62012-12-17 21:40:04 +00001665/// If the PWD environment variable is set, add a CC1 option to specify the
1666/// debug compilation directory.
1667static void addDebugCompDirArg(const ArgList &Args, ArgStringList &CmdArgs) {
1668 if (const char *pwd = ::getenv("PWD")) {
1669 // GCC also verifies that stat(pwd) and stat(".") have the same inode
1670 // number. Not doing those because stats are slow, but we could.
1671 if (llvm::sys::path::is_absolute(pwd)) {
1672 std::string CompDir = pwd;
1673 CmdArgs.push_back("-fdebug-compilation-dir");
1674 CmdArgs.push_back(Args.MakeArgString(CompDir));
1675 }
1676 }
1677}
1678
Daniel Dunbar47ac7d22009-03-18 06:00:36 +00001679void Clang::ConstructJob(Compilation &C, const JobAction &JA,
Daniel Dunbar871adcf2009-03-18 07:06:02 +00001680 const InputInfo &Output,
Daniel Dunbar62cf6012009-03-18 06:07:59 +00001681 const InputInfoList &Inputs,
Daniel Dunbar1d460332009-03-18 10:01:51 +00001682 const ArgList &Args,
Daniel Dunbar47ac7d22009-03-18 06:00:36 +00001683 const char *LinkingOutput) const {
Daniel Dunbar0a80ba72010-03-20 04:52:14 +00001684 bool KernelOrKext = Args.hasArg(options::OPT_mkernel,
1685 options::OPT_fapple_kext);
Daniel Dunbaree788e72009-12-21 18:54:17 +00001686 const Driver &D = getToolChain().getDriver();
Daniel Dunbarb488c1d2009-03-18 08:07:30 +00001687 ArgStringList CmdArgs;
1688
Daniel Dunbar077ba6a2009-03-31 20:53:55 +00001689 assert(Inputs.size() == 1 && "Unable to handle multiple inputs.");
1690
Daniel Dunbar8ff5b282009-12-11 23:00:49 +00001691 // Invoke ourselves in -cc1 mode.
1692 //
1693 // FIXME: Implement custom jobs for internal actions.
1694 CmdArgs.push_back("-cc1");
1695
Daniel Dunbardd4fe002009-10-30 18:12:20 +00001696 // Add the "effective" target triple.
Daniel Dunbaraf07f932009-03-31 17:35:15 +00001697 CmdArgs.push_back("-triple");
Daniel Dunbar00577ad2010-08-23 22:35:37 +00001698 std::string TripleStr = getToolChain().ComputeEffectiveClangTriple(Args);
Daniel Dunbardd4fe002009-10-30 18:12:20 +00001699 CmdArgs.push_back(Args.MakeArgString(TripleStr));
Daniel Dunbar728a5122009-09-10 06:49:20 +00001700
Daniel Dunbardd4fe002009-10-30 18:12:20 +00001701 // Select the appropriate action.
John McCall260611a2012-06-20 06:18:46 +00001702 RewriteKind rewriteKind = RK_None;
Fariborz Jahaniane982cc02012-04-04 18:50:28 +00001703
Daniel Dunbar1d460332009-03-18 10:01:51 +00001704 if (isa<AnalyzeJobAction>(JA)) {
1705 assert(JA.getType() == types::TY_Plist && "Invalid output type.");
1706 CmdArgs.push_back("-analyze");
Ted Kremenek30660a82012-03-06 20:06:33 +00001707 } else if (isa<MigrateJobAction>(JA)) {
1708 CmdArgs.push_back("-migrate");
Daniel Dunbar1d460332009-03-18 10:01:51 +00001709 } else if (isa<PreprocessJobAction>(JA)) {
Daniel Dunbarcd8e4c42009-03-30 06:36:42 +00001710 if (Output.getType() == types::TY_Dependencies)
1711 CmdArgs.push_back("-Eonly");
1712 else
1713 CmdArgs.push_back("-E");
Daniel Dunbar8767cbc2010-02-03 03:07:56 +00001714 } else if (isa<AssembleJobAction>(JA)) {
1715 CmdArgs.push_back("-emit-obj");
Daniel Dunbar99298002010-05-27 06:18:05 +00001716
Joerg Sonnenberger359cf922011-05-06 14:35:16 +00001717 if (UseRelaxAll(C, Args))
Daniel Dunbar99298002010-05-27 06:18:05 +00001718 CmdArgs.push_back("-mrelax-all");
Daniel Dunbarca0e0542010-08-24 16:47:49 +00001719
Daniel Dunbarfcec10b2010-10-18 22:36:15 +00001720 // When using an integrated assembler, translate -Wa, and -Xassembler
1721 // options.
1722 for (arg_iterator it = Args.filtered_begin(options::OPT_Wa_COMMA,
1723 options::OPT_Xassembler),
1724 ie = Args.filtered_end(); it != ie; ++it) {
1725 const Arg *A = *it;
1726 A->claim();
1727
1728 for (unsigned i = 0, e = A->getNumValues(); i != e; ++i) {
Richard Smith1d489cf2012-11-01 04:30:05 +00001729 StringRef Value = A->getValue(i);
Daniel Dunbarfcec10b2010-10-18 22:36:15 +00001730
1731 if (Value == "-force_cpusubtype_ALL") {
1732 // Do nothing, this is the default and we don't support anything else.
Daniel Dunbarb14eed02010-10-28 20:36:23 +00001733 } else if (Value == "-L") {
Daniel Dunbar96932322011-03-28 22:49:28 +00001734 CmdArgs.push_back("-msave-temp-labels");
Joerg Sonnenberger46a49392011-05-19 20:46:39 +00001735 } else if (Value == "--fatal-warnings") {
Joerg Sonnenbergerd7933502011-05-19 18:42:29 +00001736 CmdArgs.push_back("-mllvm");
1737 CmdArgs.push_back("-fatal-assembler-warnings");
Nick Lewyckyc3b90142011-06-21 00:14:18 +00001738 } else if (Value == "--noexecstack") {
1739 CmdArgs.push_back("-mnoexecstack");
Daniel Dunbarfcec10b2010-10-18 22:36:15 +00001740 } else {
Chris Lattner5f9e2722011-07-23 10:55:15 +00001741 D.Diag(diag::err_drv_unsupported_option_argument)
Daniel Dunbarfcec10b2010-10-18 22:36:15 +00001742 << A->getOption().getName() << Value;
1743 }
1744 }
1745 }
Daniel Dunbard02bba82010-11-19 16:23:35 +00001746
1747 // Also ignore explicit -force_cpusubtype_ALL option.
1748 (void) Args.hasArg(options::OPT_force__cpusubtype__ALL);
Daniel Dunbar1d460332009-03-18 10:01:51 +00001749 } else if (isa<PrecompileJobAction>(JA)) {
Argyrios Kyrtzidise5c35372010-08-11 23:27:58 +00001750 // Use PCH if the user requested it.
Daniel Dunbar0ebd9322009-10-15 20:02:44 +00001751 bool UsePCH = D.CCCUsePCH;
Daniel Dunbar0ebd9322009-10-15 20:02:44 +00001752
Aaron Ballman761322b2012-07-31 01:21:00 +00001753 if (JA.getType() == types::TY_Nothing)
1754 CmdArgs.push_back("-fsyntax-only");
1755 else if (UsePCH)
Douglas Gregordf91ef32009-04-18 00:34:01 +00001756 CmdArgs.push_back("-emit-pch");
1757 else
1758 CmdArgs.push_back("-emit-pth");
Daniel Dunbar1d460332009-03-18 10:01:51 +00001759 } else {
1760 assert(isa<CompileJobAction>(JA) && "Invalid action for clang tool.");
Daniel Dunbarc21c4852009-04-08 23:54:23 +00001761
Daniel Dunbar1d460332009-03-18 10:01:51 +00001762 if (JA.getType() == types::TY_Nothing) {
1763 CmdArgs.push_back("-fsyntax-only");
Daniel Dunbar6c6424b2010-06-07 23:28:45 +00001764 } else if (JA.getType() == types::TY_LLVM_IR ||
1765 JA.getType() == types::TY_LTO_IR) {
Daniel Dunbar1d460332009-03-18 10:01:51 +00001766 CmdArgs.push_back("-emit-llvm");
Daniel Dunbar6c6424b2010-06-07 23:28:45 +00001767 } else if (JA.getType() == types::TY_LLVM_BC ||
1768 JA.getType() == types::TY_LTO_BC) {
Daniel Dunbar1d460332009-03-18 10:01:51 +00001769 CmdArgs.push_back("-emit-llvm-bc");
1770 } else if (JA.getType() == types::TY_PP_Asm) {
Daniel Dunbare3b8d072009-09-17 00:47:53 +00001771 CmdArgs.push_back("-S");
Daniel Dunbar5915fbf2009-09-01 16:57:46 +00001772 } else if (JA.getType() == types::TY_AST) {
1773 CmdArgs.push_back("-emit-pch");
Daniel Dunbar64952502010-02-11 03:16:21 +00001774 } else if (JA.getType() == types::TY_RewrittenObjC) {
1775 CmdArgs.push_back("-rewrite-objc");
John McCall260611a2012-06-20 06:18:46 +00001776 rewriteKind = RK_NonFragile;
Fariborz Jahanian582b3952012-04-02 15:59:19 +00001777 } else if (JA.getType() == types::TY_RewrittenLegacyObjC) {
1778 CmdArgs.push_back("-rewrite-objc");
John McCall260611a2012-06-20 06:18:46 +00001779 rewriteKind = RK_Fragile;
Daniel Dunbar64952502010-02-11 03:16:21 +00001780 } else {
1781 assert(JA.getType() == types::TY_PP_Asm &&
1782 "Unexpected output type!");
Daniel Dunbar1d460332009-03-18 10:01:51 +00001783 }
Daniel Dunbarb488c1d2009-03-18 08:07:30 +00001784 }
1785
Daniel Dunbar1d460332009-03-18 10:01:51 +00001786 // The make clang go fast button.
1787 CmdArgs.push_back("-disable-free");
1788
John McCallb689afb2010-02-13 03:50:24 +00001789 // Disable the verification pass in -asserts builds.
1790#ifdef NDEBUG
1791 CmdArgs.push_back("-disable-llvm-verifier");
1792#endif
1793
Daniel Dunbarc9abc042009-04-08 05:11:16 +00001794 // Set the main file name, so that debug info works even with
1795 // -save-temps.
1796 CmdArgs.push_back("-main-file-name");
Bob Wilson66b8a662012-11-23 06:14:39 +00001797 CmdArgs.push_back(getBaseInputName(Args, Inputs));
Daniel Dunbarc9abc042009-04-08 05:11:16 +00001798
Daniel Dunbar3bbc7532009-04-08 18:03:55 +00001799 // Some flags which affect the language (via preprocessor
Bob Wilson66b8a662012-11-23 06:14:39 +00001800 // defines).
Daniel Dunbar3bbc7532009-04-08 18:03:55 +00001801 if (Args.hasArg(options::OPT_static))
1802 CmdArgs.push_back("-static-define");
1803
Daniel Dunbar1d460332009-03-18 10:01:51 +00001804 if (isa<AnalyzeJobAction>(JA)) {
Ted Kremenekb8bb3e72009-09-25 05:55:59 +00001805 // Enable region store model by default.
1806 CmdArgs.push_back("-analyzer-store=region");
1807
Ted Kremenekb40d06d2009-12-07 22:26:14 +00001808 // Treat blocks as analysis entry points.
1809 CmdArgs.push_back("-analyzer-opt-analyze-nested-blocks");
1810
Ted Kremenek51885072011-03-24 00:28:47 +00001811 CmdArgs.push_back("-analyzer-eagerly-assume");
1812
Daniel Dunbar1d460332009-03-18 10:01:51 +00001813 // Add default argument set.
Daniel Dunbard8fc0f22009-05-22 00:38:15 +00001814 if (!Args.hasArg(options::OPT__analyzer_no_default_checks)) {
Argyrios Kyrtzidis027a6ab2011-02-15 07:42:33 +00001815 CmdArgs.push_back("-analyzer-checker=core");
Ted Kremenek51885072011-03-24 00:28:47 +00001816
Argyrios Kyrtzidis027a6ab2011-02-15 07:42:33 +00001817 if (getToolChain().getTriple().getOS() != llvm::Triple::Win32)
1818 CmdArgs.push_back("-analyzer-checker=unix");
Ted Kremenek51885072011-03-24 00:28:47 +00001819
Argyrios Kyrtzidis027a6ab2011-02-15 07:42:33 +00001820 if (getToolChain().getTriple().getVendor() == llvm::Triple::Apple)
Ted Kremenek51885072011-03-24 00:28:47 +00001821 CmdArgs.push_back("-analyzer-checker=osx");
Ted Kremeneka8180e52012-01-20 06:00:17 +00001822
1823 CmdArgs.push_back("-analyzer-checker=deadcode");
Ted Kremenek8dc05062012-01-26 02:27:38 +00001824
1825 // Enable the following experimental checkers for testing.
Ted Kremenek8dc05062012-01-26 02:27:38 +00001826 CmdArgs.push_back("-analyzer-checker=security.insecureAPI.UncheckedReturn");
1827 CmdArgs.push_back("-analyzer-checker=security.insecureAPI.getpw");
1828 CmdArgs.push_back("-analyzer-checker=security.insecureAPI.gets");
1829 CmdArgs.push_back("-analyzer-checker=security.insecureAPI.mktemp");
1830 CmdArgs.push_back("-analyzer-checker=security.insecureAPI.mkstemp");
1831 CmdArgs.push_back("-analyzer-checker=security.insecureAPI.vfork");
Daniel Dunbard8fc0f22009-05-22 00:38:15 +00001832 }
Daniel Dunbarc21c4852009-04-08 23:54:23 +00001833
Daniel Dunbard8fc0f22009-05-22 00:38:15 +00001834 // Set the output format. The default is plist, for (lame) historical
1835 // reasons.
1836 CmdArgs.push_back("-analyzer-output");
1837 if (Arg *A = Args.getLastArg(options::OPT__analyzer_output))
Richard Smith1d489cf2012-11-01 04:30:05 +00001838 CmdArgs.push_back(A->getValue());
Daniel Dunbard8fc0f22009-05-22 00:38:15 +00001839 else
1840 CmdArgs.push_back("plist");
Daniel Dunbar1d460332009-03-18 10:01:51 +00001841
Ted Kremenek0647a7b2010-03-22 22:32:05 +00001842 // Disable the presentation of standard compiler warnings when
1843 // using --analyze. We only want to show static analyzer diagnostics
1844 // or frontend errors.
1845 CmdArgs.push_back("-w");
1846
Daniel Dunbar1d460332009-03-18 10:01:51 +00001847 // Add -Xanalyzer arguments when running as analyzer.
1848 Args.AddAllArgValues(CmdArgs, options::OPT_Xanalyzer);
Mike Stump1eb44332009-09-09 15:08:12 +00001849 }
1850
Daniel Dunbare2fd6642009-09-10 01:21:12 +00001851 CheckCodeGenerationOptions(D, Args);
1852
Chandler Carruth7ce816a2012-11-19 03:52:03 +00001853 // For the PIC and PIE flag options, this logic is different from the legacy
1854 // logic in very old versions of GCC, as that logic was just a bug no one had
1855 // ever fixed. This logic is both more rational and consistent with GCC's new
1856 // logic now that the bugs are fixed. The last argument relating to either
1857 // PIC or PIE wins, and no other argument is used. If the last argument is
1858 // any flavor of the '-fno-...' arguments, both PIC and PIE are disabled. Any
1859 // PIE option implicitly enables PIC at the same level.
1860 bool PIE = false;
1861 bool PIC = getToolChain().isPICDefault();
1862 bool IsPICLevelTwo = PIC;
1863 if (Arg *A = Args.getLastArg(options::OPT_fPIC, options::OPT_fno_PIC,
1864 options::OPT_fpic, options::OPT_fno_pic,
1865 options::OPT_fPIE, options::OPT_fno_PIE,
1866 options::OPT_fpie, options::OPT_fno_pie)) {
1867 Option O = A->getOption();
1868 if (O.matches(options::OPT_fPIC) || O.matches(options::OPT_fpic) ||
1869 O.matches(options::OPT_fPIE) || O.matches(options::OPT_fpie)) {
1870 PIE = O.matches(options::OPT_fPIE) || O.matches(options::OPT_fpie);
1871 PIC = PIE || O.matches(options::OPT_fPIC) || O.matches(options::OPT_fpic);
1872 IsPICLevelTwo = O.matches(options::OPT_fPIE) ||
1873 O.matches(options::OPT_fPIC);
1874 } else {
1875 PIE = PIC = false;
1876 }
Benjamin Kramerb12ecd32012-11-13 15:32:35 +00001877 }
Chandler Carruth7ce816a2012-11-19 03:52:03 +00001878 // Check whether the tool chain trumps the PIC-ness decision. If the PIC-ness
1879 // is forced, then neither PIC nor PIE flags will have no effect.
1880 if (getToolChain().isPICDefaultForced()) {
1881 PIE = false;
1882 PIC = getToolChain().isPICDefault();
1883 IsPICLevelTwo = PIC;
Chandler Carruth5e219cf2012-04-08 16:40:35 +00001884 }
Chandler Carruth7ce816a2012-11-19 03:52:03 +00001885
1886 // Inroduce a Darwin-specific hack. If the default is PIC but the flags
1887 // specified while enabling PIC enabled level 1 PIC, just force it back to
1888 // level 2 PIC instead. This matches the behavior of Darwin GCC (based on my
1889 // informal testing).
1890 if (PIC && getToolChain().getTriple().isOSDarwin())
1891 IsPICLevelTwo |= getToolChain().isPICDefault();
1892
Chandler Carruth5e219cf2012-04-08 16:40:35 +00001893 // Note that these flags are trump-cards. Regardless of the order w.r.t. the
1894 // PIC or PIE options above, if these show up, PIC is disabled.
Daniel Dunbar7a0c0642012-10-15 22:23:53 +00001895 llvm::Triple Triple(TripleStr);
1896 if ((Args.hasArg(options::OPT_mkernel) ||
1897 Args.hasArg(options::OPT_fapple_kext)) &&
1898 (Triple.getOS() != llvm::Triple::IOS ||
1899 Triple.isOSVersionLT(6)))
Chandler Carruth7ce816a2012-11-19 03:52:03 +00001900 PIC = PIE = false;
Chandler Carruth5e219cf2012-04-08 16:40:35 +00001901 if (Args.hasArg(options::OPT_static))
Chandler Carruth7ce816a2012-11-19 03:52:03 +00001902 PIC = PIE = false;
Chandler Carruth5e219cf2012-04-08 16:40:35 +00001903
Chandler Carruth7ce816a2012-11-19 03:52:03 +00001904 if (Arg *A = Args.getLastArg(options::OPT_mdynamic_no_pic)) {
1905 // This is a very special mode. It trumps the other modes, almost no one
1906 // uses it, and it isn't even valid on any OS but Darwin.
1907 if (!getToolChain().getTriple().isOSDarwin())
1908 D.Diag(diag::err_drv_unsupported_opt_for_target)
1909 << A->getSpelling() << getToolChain().getTriple().str();
1910
1911 // FIXME: Warn when this flag trumps some other PIC or PIE flag.
1912
Daniel Dunbarf219e7c2009-11-29 07:18:39 +00001913 CmdArgs.push_back("-mrelocation-model");
Chandler Carruth7ce816a2012-11-19 03:52:03 +00001914 CmdArgs.push_back("dynamic-no-pic");
Daniel Dunbarbc85be82009-04-29 18:32:25 +00001915
Chandler Carruth7ce816a2012-11-19 03:52:03 +00001916 // Only a forced PIC mode can cause the actual compile to have PIC defines
1917 // etc., no flags are sufficient. This behavior was selected to closely
1918 // match that of llvm-gcc and Apple GCC before that.
1919 if (getToolChain().isPICDefault() && getToolChain().isPICDefaultForced()) {
1920 CmdArgs.push_back("-pic-level");
1921 CmdArgs.push_back("2");
1922 }
1923 } else {
1924 // Currently, LLVM only knows about PIC vs. static; the PIE differences are
1925 // handled in Clang's IRGen by the -pie-level flag.
1926 CmdArgs.push_back("-mrelocation-model");
1927 CmdArgs.push_back(PIC ? "pic" : "static");
1928
1929 if (PIC) {
1930 CmdArgs.push_back("-pic-level");
1931 CmdArgs.push_back(IsPICLevelTwo ? "2" : "1");
1932 if (PIE) {
1933 CmdArgs.push_back("-pie-level");
1934 CmdArgs.push_back(IsPICLevelTwo ? "2" : "1");
1935 }
1936 }
Daniel Dunbarbc85be82009-04-29 18:32:25 +00001937 }
Chandler Carruth5e219cf2012-04-08 16:40:35 +00001938
Tanya Lattner59876c22009-11-04 01:18:09 +00001939 if (!Args.hasFlag(options::OPT_fmerge_all_constants,
1940 options::OPT_fno_merge_all_constants))
Chris Lattnerf44a1a02011-04-08 18:06:54 +00001941 CmdArgs.push_back("-fno-merge-all-constants");
Daniel Dunbar6bea73b2009-09-16 06:17:29 +00001942
Daniel Dunbarf219e7c2009-11-29 07:18:39 +00001943 // LLVM Code Generator Options.
1944
Daniel Dunbar17d3fea2011-02-09 17:54:19 +00001945 if (Arg *A = Args.getLastArg(options::OPT_mregparm_EQ)) {
1946 CmdArgs.push_back("-mregparm");
Richard Smith1d489cf2012-11-01 04:30:05 +00001947 CmdArgs.push_back(A->getValue());
Daniel Dunbar17d3fea2011-02-09 17:54:19 +00001948 }
1949
Roman Divackycfe9af22011-03-01 17:40:53 +00001950 if (Args.hasFlag(options::OPT_mrtd, options::OPT_mno_rtd, false))
1951 CmdArgs.push_back("-mrtd");
1952
Rafael Espindola6af27ec2011-12-14 21:02:23 +00001953 if (shouldUseFramePointer(Args, getToolChain().getTriple()))
Daniel Dunbarf219e7c2009-11-29 07:18:39 +00001954 CmdArgs.push_back("-mdisable-fp-elim");
1955 if (!Args.hasFlag(options::OPT_fzero_initialized_in_bss,
1956 options::OPT_fno_zero_initialized_in_bss))
1957 CmdArgs.push_back("-mno-zero-initialized-in-bss");
Daniel Dunbar398c6102011-02-04 02:20:39 +00001958 if (!Args.hasFlag(options::OPT_fstrict_aliasing,
1959 options::OPT_fno_strict_aliasing,
1960 getToolChain().IsStrictAliasingDefault()))
Dan Gohman4d5625e2010-10-14 22:36:56 +00001961 CmdArgs.push_back("-relaxed-aliasing");
Chandler Carruth82fe6ae2012-03-27 23:58:37 +00001962 if (Args.hasFlag(options::OPT_fstrict_enums, options::OPT_fno_strict_enums,
1963 false))
1964 CmdArgs.push_back("-fstrict-enums");
Nick Lewycky1db772b2012-01-23 08:29:12 +00001965 if (!Args.hasFlag(options::OPT_foptimize_sibling_calls,
1966 options::OPT_fno_optimize_sibling_calls))
1967 CmdArgs.push_back("-mdisable-tail-calls");
Daniel Dunbar1b718482010-05-14 22:00:22 +00001968
Chandler Carruthabf07a72012-01-02 14:19:45 +00001969 // Handle various floating point optimization flags, mapping them to the
1970 // appropriate LLVM code generation flags. The pattern for all of these is to
1971 // default off the codegen optimizations, and if any flag enables them and no
1972 // flag disables them after the flag enabling them, enable the codegen
1973 // optimization. This is complicated by several "umbrella" flags.
1974 if (Arg *A = Args.getLastArg(options::OPT_ffast_math,
Chad Rosier80ecf5e2012-09-25 22:03:25 +00001975 options::OPT_fno_fast_math,
Chandler Carruthabf07a72012-01-02 14:19:45 +00001976 options::OPT_ffinite_math_only,
1977 options::OPT_fno_finite_math_only,
1978 options::OPT_fhonor_infinities,
1979 options::OPT_fno_honor_infinities))
Chad Rosier80ecf5e2012-09-25 22:03:25 +00001980 if (A->getOption().getID() != options::OPT_fno_fast_math &&
1981 A->getOption().getID() != options::OPT_fno_finite_math_only &&
Chandler Carruthabf07a72012-01-02 14:19:45 +00001982 A->getOption().getID() != options::OPT_fhonor_infinities)
1983 CmdArgs.push_back("-menable-no-infs");
1984 if (Arg *A = Args.getLastArg(options::OPT_ffast_math,
Chad Rosier80ecf5e2012-09-25 22:03:25 +00001985 options::OPT_fno_fast_math,
Chandler Carruthabf07a72012-01-02 14:19:45 +00001986 options::OPT_ffinite_math_only,
1987 options::OPT_fno_finite_math_only,
1988 options::OPT_fhonor_nans,
1989 options::OPT_fno_honor_nans))
Chad Rosier80ecf5e2012-09-25 22:03:25 +00001990 if (A->getOption().getID() != options::OPT_fno_fast_math &&
1991 A->getOption().getID() != options::OPT_fno_finite_math_only &&
Chandler Carruthabf07a72012-01-02 14:19:45 +00001992 A->getOption().getID() != options::OPT_fhonor_nans)
1993 CmdArgs.push_back("-menable-no-nans");
1994
Benjamin Kramer769aa2d2012-05-02 14:55:48 +00001995 // -fmath-errno is the default on some platforms, e.g. BSD-derived OSes.
1996 bool MathErrno = getToolChain().IsMathErrnoDefault();
Chandler Carruthabf07a72012-01-02 14:19:45 +00001997 if (Arg *A = Args.getLastArg(options::OPT_ffast_math,
Chad Rosier80ecf5e2012-09-25 22:03:25 +00001998 options::OPT_fno_fast_math,
Chandler Carruthabf07a72012-01-02 14:19:45 +00001999 options::OPT_fmath_errno,
Chandler Carruth4f50c502012-04-26 02:10:51 +00002000 options::OPT_fno_math_errno))
2001 MathErrno = A->getOption().getID() == options::OPT_fmath_errno;
2002 if (MathErrno)
2003 CmdArgs.push_back("-fmath-errno");
Chandler Carruthabf07a72012-01-02 14:19:45 +00002004
2005 // There are several flags which require disabling very specific
2006 // optimizations. Any of these being disabled forces us to turn off the
2007 // entire set of LLVM optimizations, so collect them through all the flag
2008 // madness.
2009 bool AssociativeMath = false;
2010 if (Arg *A = Args.getLastArg(options::OPT_ffast_math,
Chad Rosier80ecf5e2012-09-25 22:03:25 +00002011 options::OPT_fno_fast_math,
Chandler Carruthabf07a72012-01-02 14:19:45 +00002012 options::OPT_funsafe_math_optimizations,
2013 options::OPT_fno_unsafe_math_optimizations,
2014 options::OPT_fassociative_math,
2015 options::OPT_fno_associative_math))
Chad Rosier80ecf5e2012-09-25 22:03:25 +00002016 if (A->getOption().getID() != options::OPT_fno_fast_math &&
2017 A->getOption().getID() != options::OPT_fno_unsafe_math_optimizations &&
Chandler Carruthabf07a72012-01-02 14:19:45 +00002018 A->getOption().getID() != options::OPT_fno_associative_math)
2019 AssociativeMath = true;
2020 bool ReciprocalMath = false;
2021 if (Arg *A = Args.getLastArg(options::OPT_ffast_math,
Chad Rosier80ecf5e2012-09-25 22:03:25 +00002022 options::OPT_fno_fast_math,
Chandler Carruthabf07a72012-01-02 14:19:45 +00002023 options::OPT_funsafe_math_optimizations,
2024 options::OPT_fno_unsafe_math_optimizations,
2025 options::OPT_freciprocal_math,
2026 options::OPT_fno_reciprocal_math))
Chad Rosier80ecf5e2012-09-25 22:03:25 +00002027 if (A->getOption().getID() != options::OPT_fno_fast_math &&
2028 A->getOption().getID() != options::OPT_fno_unsafe_math_optimizations &&
Chandler Carruthabf07a72012-01-02 14:19:45 +00002029 A->getOption().getID() != options::OPT_fno_reciprocal_math)
2030 ReciprocalMath = true;
2031 bool SignedZeros = true;
2032 if (Arg *A = Args.getLastArg(options::OPT_ffast_math,
Chad Rosier80ecf5e2012-09-25 22:03:25 +00002033 options::OPT_fno_fast_math,
Chandler Carruthabf07a72012-01-02 14:19:45 +00002034 options::OPT_funsafe_math_optimizations,
2035 options::OPT_fno_unsafe_math_optimizations,
2036 options::OPT_fsigned_zeros,
2037 options::OPT_fno_signed_zeros))
Chad Rosier80ecf5e2012-09-25 22:03:25 +00002038 if (A->getOption().getID() != options::OPT_fno_fast_math &&
2039 A->getOption().getID() != options::OPT_fno_unsafe_math_optimizations &&
Chandler Carruthabf07a72012-01-02 14:19:45 +00002040 A->getOption().getID() != options::OPT_fsigned_zeros)
2041 SignedZeros = false;
2042 bool TrappingMath = true;
2043 if (Arg *A = Args.getLastArg(options::OPT_ffast_math,
Chad Rosier80ecf5e2012-09-25 22:03:25 +00002044 options::OPT_fno_fast_math,
Chandler Carruthabf07a72012-01-02 14:19:45 +00002045 options::OPT_funsafe_math_optimizations,
2046 options::OPT_fno_unsafe_math_optimizations,
2047 options::OPT_ftrapping_math,
2048 options::OPT_fno_trapping_math))
Chad Rosier80ecf5e2012-09-25 22:03:25 +00002049 if (A->getOption().getID() != options::OPT_fno_fast_math &&
2050 A->getOption().getID() != options::OPT_fno_unsafe_math_optimizations &&
Chandler Carruthabf07a72012-01-02 14:19:45 +00002051 A->getOption().getID() != options::OPT_ftrapping_math)
2052 TrappingMath = false;
2053 if (!MathErrno && AssociativeMath && ReciprocalMath && !SignedZeros &&
2054 !TrappingMath)
2055 CmdArgs.push_back("-menable-unsafe-fp-math");
2056
Lang Hamesc9686712012-07-06 00:59:19 +00002057
2058 // Validate and pass through -fp-contract option.
2059 if (Arg *A = Args.getLastArg(options::OPT_ffast_math,
Chad Rosier80ecf5e2012-09-25 22:03:25 +00002060 options::OPT_fno_fast_math,
Lang Hamesc9686712012-07-06 00:59:19 +00002061 options::OPT_ffp_contract)) {
2062 if (A->getOption().getID() == options::OPT_ffp_contract) {
Richard Smith1d489cf2012-11-01 04:30:05 +00002063 StringRef Val = A->getValue();
Lang Hamesc9686712012-07-06 00:59:19 +00002064 if (Val == "fast" || Val == "on" || Val == "off") {
2065 CmdArgs.push_back(Args.MakeArgString("-ffp-contract=" + Val));
2066 } else {
2067 D.Diag(diag::err_drv_unsupported_option_argument)
2068 << A->getOption().getName() << Val;
2069 }
Chad Rosier80ecf5e2012-09-25 22:03:25 +00002070 } else if (A->getOption().getID() == options::OPT_ffast_math) {
Lang Hamesc9686712012-07-06 00:59:19 +00002071 // If fast-math is set then set the fp-contract mode to fast.
2072 CmdArgs.push_back(Args.MakeArgString("-ffp-contract=fast"));
2073 }
2074 }
2075
Bob Wilson455e72e2012-07-19 03:52:53 +00002076 // We separately look for the '-ffast-math' and '-ffinite-math-only' flags,
2077 // and if we find them, tell the frontend to provide the appropriate
2078 // preprocessor macros. This is distinct from enabling any optimizations as
2079 // these options induce language changes which must survive serialization
2080 // and deserialization, etc.
Chad Rosier80ecf5e2012-09-25 22:03:25 +00002081 if (Arg *A = Args.getLastArg(options::OPT_ffast_math, options::OPT_fno_fast_math))
2082 if (A->getOption().matches(options::OPT_ffast_math))
2083 CmdArgs.push_back("-ffast-math");
2084 if (Arg *A = Args.getLastArg(options::OPT_ffinite_math_only, options::OPT_fno_fast_math))
2085 if (A->getOption().matches(options::OPT_ffinite_math_only))
2086 CmdArgs.push_back("-ffinite-math-only");
Chandler Carruthabf07a72012-01-02 14:19:45 +00002087
Daniel Dunbar1b718482010-05-14 22:00:22 +00002088 // Decide whether to use verbose asm. Verbose assembly is the default on
2089 // toolchains which have the integrated assembler on by default.
2090 bool IsVerboseAsmDefault = getToolChain().IsIntegratedAssemblerDefault();
2091 if (Args.hasFlag(options::OPT_fverbose_asm, options::OPT_fno_verbose_asm,
Michael J. Spencer20249a12010-10-21 03:16:25 +00002092 IsVerboseAsmDefault) ||
Daniel Dunbar1b718482010-05-14 22:00:22 +00002093 Args.hasArg(options::OPT_dA))
Daniel Dunbarf219e7c2009-11-29 07:18:39 +00002094 CmdArgs.push_back("-masm-verbose");
Daniel Dunbar1b718482010-05-14 22:00:22 +00002095
Daniel Dunbarf219e7c2009-11-29 07:18:39 +00002096 if (Args.hasArg(options::OPT_fdebug_pass_structure)) {
2097 CmdArgs.push_back("-mdebug-pass");
2098 CmdArgs.push_back("Structure");
2099 }
2100 if (Args.hasArg(options::OPT_fdebug_pass_arguments)) {
2101 CmdArgs.push_back("-mdebug-pass");
2102 CmdArgs.push_back("Arguments");
2103 }
2104
John McCalld0c2ec42010-02-19 02:45:38 +00002105 // Enable -mconstructor-aliases except on darwin, where we have to
2106 // work around a linker bug; see <rdar://problem/7651567>.
Bob Wilson905c45f2011-10-14 05:03:44 +00002107 if (!getToolChain().getTriple().isOSDarwin())
John McCalld0c2ec42010-02-19 02:45:38 +00002108 CmdArgs.push_back("-mconstructor-aliases");
NAKAMURA Takumi125b4cb2011-02-17 08:50:50 +00002109
John McCall32096692011-03-18 02:56:14 +00002110 // Darwin's kernel doesn't support guard variables; just die if we
2111 // try to use them.
Bob Wilson905c45f2011-10-14 05:03:44 +00002112 if (KernelOrKext && getToolChain().getTriple().isOSDarwin())
John McCall32096692011-03-18 02:56:14 +00002113 CmdArgs.push_back("-fforbid-guard-variables");
2114
Douglas Gregor6f755502011-02-01 15:15:22 +00002115 if (Args.hasArg(options::OPT_mms_bitfields)) {
2116 CmdArgs.push_back("-mms-bitfields");
2117 }
John McCalld0c2ec42010-02-19 02:45:38 +00002118
Daniel Dunbar6bea73b2009-09-16 06:17:29 +00002119 // This is a coarse approximation of what llvm-gcc actually does, both
2120 // -fasynchronous-unwind-tables and -fnon-call-exceptions interact in more
2121 // complicated ways.
2122 bool AsynchronousUnwindTables =
2123 Args.hasFlag(options::OPT_fasynchronous_unwind_tables,
2124 options::OPT_fno_asynchronous_unwind_tables,
2125 getToolChain().IsUnwindTablesDefault() &&
Daniel Dunbar0a80ba72010-03-20 04:52:14 +00002126 !KernelOrKext);
Daniel Dunbar6bea73b2009-09-16 06:17:29 +00002127 if (Args.hasFlag(options::OPT_funwind_tables, options::OPT_fno_unwind_tables,
2128 AsynchronousUnwindTables))
Daniel Dunbarf219e7c2009-11-29 07:18:39 +00002129 CmdArgs.push_back("-munwind-tables");
2130
Chandler Carrutha6b25812012-11-21 23:40:23 +00002131 getToolChain().addClangTargetOptions(Args, CmdArgs);
Rafael Espindola8af669f2012-06-19 01:26:10 +00002132
Daniel Dunbarf219e7c2009-11-29 07:18:39 +00002133 if (Arg *A = Args.getLastArg(options::OPT_flimited_precision_EQ)) {
2134 CmdArgs.push_back("-mlimit-float-precision");
Richard Smith1d489cf2012-11-01 04:30:05 +00002135 CmdArgs.push_back(A->getValue());
Daniel Dunbarf219e7c2009-11-29 07:18:39 +00002136 }
Daniel Dunbarbc85be82009-04-29 18:32:25 +00002137
Daniel Dunbar868bd0a2009-05-06 03:16:41 +00002138 // FIXME: Handle -mtune=.
2139 (void) Args.hasArg(options::OPT_mtune_EQ);
Daniel Dunbarbc85be82009-04-29 18:32:25 +00002140
Benjamin Kramer8e9ef0d2009-08-05 14:30:52 +00002141 if (Arg *A = Args.getLastArg(options::OPT_mcmodel_EQ)) {
Daniel Dunbarf219e7c2009-11-29 07:18:39 +00002142 CmdArgs.push_back("-mcode-model");
Richard Smith1d489cf2012-11-01 04:30:05 +00002143 CmdArgs.push_back(A->getValue());
Benjamin Kramer8e9ef0d2009-08-05 14:30:52 +00002144 }
2145
Daniel Dunbar6acda162009-09-09 22:33:08 +00002146 // Add target specific cpu and features flags.
2147 switch(getToolChain().getTriple().getArch()) {
2148 default:
2149 break;
Daniel Dunbar868bd0a2009-05-06 03:16:41 +00002150
Daniel Dunbarb163ef72009-09-10 04:57:17 +00002151 case llvm::Triple::arm:
2152 case llvm::Triple::thumb:
Daniel Dunbarfa41d692011-03-17 17:10:06 +00002153 AddARMTargetArgs(Args, CmdArgs, KernelOrKext);
Daniel Dunbarb163ef72009-09-10 04:57:17 +00002154 break;
2155
Eric Christophered734732010-03-02 02:41:08 +00002156 case llvm::Triple::mips:
2157 case llvm::Triple::mipsel:
Akira Hatanaka7ec02582011-09-21 02:13:07 +00002158 case llvm::Triple::mips64:
2159 case llvm::Triple::mips64el:
Eric Christophered734732010-03-02 02:41:08 +00002160 AddMIPSTargetArgs(Args, CmdArgs);
2161 break;
2162
Hal Finkel02a84272012-06-11 22:35:19 +00002163 case llvm::Triple::ppc:
2164 case llvm::Triple::ppc64:
2165 AddPPCTargetArgs(Args, CmdArgs);
2166 break;
2167
Bruno Cardoso Lopes9284d212010-11-09 17:21:19 +00002168 case llvm::Triple::sparc:
2169 AddSparcTargetArgs(Args, CmdArgs);
2170 break;
2171
Daniel Dunbar6acda162009-09-09 22:33:08 +00002172 case llvm::Triple::x86:
2173 case llvm::Triple::x86_64:
2174 AddX86TargetArgs(Args, CmdArgs);
2175 break;
Tony Linthicum96319392011-12-12 21:14:55 +00002176
2177 case llvm::Triple::hexagon:
2178 AddHexagonTargetArgs(Args, CmdArgs);
2179 break;
Daniel Dunbarbc85be82009-04-29 18:32:25 +00002180 }
2181
Tony Linthicum96319392011-12-12 21:14:55 +00002182
2183
Daniel Dunbarc176bc62010-08-11 23:07:47 +00002184 // Pass the linker version in use.
2185 if (Arg *A = Args.getLastArg(options::OPT_mlinker_version_EQ)) {
2186 CmdArgs.push_back("-target-linker-version");
Richard Smith1d489cf2012-11-01 04:30:05 +00002187 CmdArgs.push_back(A->getValue());
Daniel Dunbarc176bc62010-08-11 23:07:47 +00002188 }
2189
Nick Lewyckyb2d11cc2011-02-02 06:43:03 +00002190 // -mno-omit-leaf-frame-pointer is the default on Darwin.
Daniel Dunbar1ad66482010-07-01 01:31:45 +00002191 if (Args.hasFlag(options::OPT_momit_leaf_frame_pointer,
Nick Lewyckyb2d11cc2011-02-02 06:43:03 +00002192 options::OPT_mno_omit_leaf_frame_pointer,
Bob Wilson905c45f2011-10-14 05:03:44 +00002193 !getToolChain().getTriple().isOSDarwin()))
Daniel Dunbar1ad66482010-07-01 01:31:45 +00002194 CmdArgs.push_back("-momit-leaf-frame-pointer");
2195
Daniel Dunbarb30575c2010-05-12 18:19:58 +00002196 // Explicitly error on some things we know we don't support and can't just
2197 // ignore.
2198 types::ID InputType = Inputs[0].getType();
Daniel Dunbare94db472010-09-24 19:39:37 +00002199 if (!Args.hasArg(options::OPT_fallow_unsupported)) {
2200 Arg *Unsupported;
Daniel Dunbare94db472010-09-24 19:39:37 +00002201 if (types::isCXX(InputType) &&
Bob Wilson905c45f2011-10-14 05:03:44 +00002202 getToolChain().getTriple().isOSDarwin() &&
Daniel Dunbare94db472010-09-24 19:39:37 +00002203 getToolChain().getTriple().getArch() == llvm::Triple::x86) {
Bob Wilsona544aee2011-08-13 23:48:55 +00002204 if ((Unsupported = Args.getLastArg(options::OPT_fapple_kext)) ||
2205 (Unsupported = Args.getLastArg(options::OPT_mkernel)))
Chris Lattner5f9e2722011-07-23 10:55:15 +00002206 D.Diag(diag::err_drv_clang_unsupported_opt_cxx_darwin_i386)
Daniel Dunbare94db472010-09-24 19:39:37 +00002207 << Unsupported->getOption().getName();
2208 }
Daniel Dunbarb30575c2010-05-12 18:19:58 +00002209 }
2210
Daniel Dunbar1d460332009-03-18 10:01:51 +00002211 Args.AddAllArgs(CmdArgs, options::OPT_v);
Daniel Dunbarf7c16d92010-08-24 22:44:13 +00002212 Args.AddLastArg(CmdArgs, options::OPT_H);
Chad Rosier2b819102011-08-02 17:58:04 +00002213 if (D.CCPrintHeaders && !D.CCGenDiagnostics) {
Daniel Dunbar322c29f2011-02-02 21:11:35 +00002214 CmdArgs.push_back("-header-include-file");
2215 CmdArgs.push_back(D.CCPrintHeadersFilename ?
2216 D.CCPrintHeadersFilename : "-");
2217 }
Daniel Dunbar1d460332009-03-18 10:01:51 +00002218 Args.AddLastArg(CmdArgs, options::OPT_P);
Mike Stump1eb44332009-09-09 15:08:12 +00002219 Args.AddLastArg(CmdArgs, options::OPT_print_ivar_layout);
Daniel Dunbar1d460332009-03-18 10:01:51 +00002220
Chad Rosier2b819102011-08-02 17:58:04 +00002221 if (D.CCLogDiagnostics && !D.CCGenDiagnostics) {
Daniel Dunbarc8a22b02011-04-07 18:01:20 +00002222 CmdArgs.push_back("-diagnostic-log-file");
2223 CmdArgs.push_back(D.CCLogDiagnosticsFilename ?
2224 D.CCLogDiagnosticsFilename : "-");
2225 }
2226
Alexey Samsonova9cd83b2012-05-29 08:10:34 +00002227 // Use the last option from "-g" group. "-gline-tables-only" is
2228 // preserved, all other debug options are substituted with "-g".
Rafael Espindola18f36d92010-03-07 04:46:18 +00002229 Args.ClaimAllArgs(options::OPT_g_Group);
Alexey Samsonova9cd83b2012-05-29 08:10:34 +00002230 if (Arg *A = Args.getLastArg(options::OPT_g_Group)) {
2231 if (A->getOption().matches(options::OPT_gline_tables_only)) {
2232 CmdArgs.push_back("-gline-tables-only");
Alexey Samsonov7f326072012-06-21 08:22:39 +00002233 } else if (!A->getOption().matches(options::OPT_g0) &&
2234 !A->getOption().matches(options::OPT_ggdb0)) {
Chad Rosiercf6ba2e2011-11-07 19:52:29 +00002235 CmdArgs.push_back("-g");
Chad Rosier2875bda2011-11-04 19:28:44 +00002236 }
Alexey Samsonova9cd83b2012-05-29 08:10:34 +00002237 }
Daniel Dunbar1d460332009-03-18 10:01:51 +00002238
Alexey Samsonov7f326072012-06-21 08:22:39 +00002239 // We ignore flags -gstrict-dwarf and -grecord-gcc-switches for now.
2240 Args.ClaimAllArgs(options::OPT_g_flags_Group);
Eric Christopherda3301e2012-10-18 21:52:18 +00002241 if (Args.hasArg(options::OPT_gcolumn_info))
2242 CmdArgs.push_back("-dwarf-column-info");
Alexey Samsonov7f326072012-06-21 08:22:39 +00002243
Rafael Espindola9cf933a2010-05-06 21:06:04 +00002244 Args.AddAllArgs(CmdArgs, options::OPT_ffunction_sections);
2245 Args.AddAllArgs(CmdArgs, options::OPT_fdata_sections);
2246
Chris Lattner7255a2d2010-06-22 00:03:40 +00002247 Args.AddAllArgs(CmdArgs, options::OPT_finstrument_functions);
2248
Nick Lewyckye8ba8d72011-04-21 23:44:07 +00002249 if (Args.hasArg(options::OPT_ftest_coverage) ||
2250 Args.hasArg(options::OPT_coverage))
2251 CmdArgs.push_back("-femit-coverage-notes");
2252 if (Args.hasArg(options::OPT_fprofile_arcs) ||
2253 Args.hasArg(options::OPT_coverage))
2254 CmdArgs.push_back("-femit-coverage-data");
2255
Nick Lewycky5ea4f442011-05-04 20:46:58 +00002256 if (C.getArgs().hasArg(options::OPT_c) ||
2257 C.getArgs().hasArg(options::OPT_S)) {
2258 if (Output.isFilename()) {
Nick Lewycky3dc05412011-05-05 00:08:20 +00002259 CmdArgs.push_back("-coverage-file");
Bill Wendlingecbbea42012-08-30 00:43:41 +00002260 SmallString<128> absFilename(Output.getFilename());
2261 llvm::sys::fs::make_absolute(absFilename);
2262 CmdArgs.push_back(Args.MakeArgString(absFilename));
Nick Lewycky5ea4f442011-05-04 20:46:58 +00002263 }
2264 }
2265
Daniel Dunbara268fc02011-10-11 18:20:10 +00002266 // Pass options for controlling the default header search paths.
2267 if (Args.hasArg(options::OPT_nostdinc)) {
2268 CmdArgs.push_back("-nostdsysteminc");
2269 CmdArgs.push_back("-nobuiltininc");
2270 } else {
Daniel Dunbar92d6d402011-10-11 18:20:16 +00002271 if (Args.hasArg(options::OPT_nostdlibinc))
2272 CmdArgs.push_back("-nostdsysteminc");
Daniel Dunbara268fc02011-10-11 18:20:10 +00002273 Args.AddLastArg(CmdArgs, options::OPT_nostdincxx);
2274 Args.AddLastArg(CmdArgs, options::OPT_nobuiltininc);
2275 }
Daniel Dunbar1d460332009-03-18 10:01:51 +00002276
Daniel Dunbar5f122322009-12-15 01:02:52 +00002277 // Pass the path to compiler resource files.
Daniel Dunbar5f122322009-12-15 01:02:52 +00002278 CmdArgs.push_back("-resource-dir");
Daniel Dunbar225c4172010-01-20 02:35:16 +00002279 CmdArgs.push_back(D.ResourceDir.c_str());
Daniel Dunbar2ac9fc22009-04-07 21:42:00 +00002280
Argyrios Kyrtzidis389db162010-11-03 22:45:23 +00002281 Args.AddLastArg(CmdArgs, options::OPT_working_directory);
2282
Ted Kremenek30660a82012-03-06 20:06:33 +00002283 bool ARCMTEnabled = false;
John McCall8f0e8d22011-06-15 23:25:17 +00002284 if (!Args.hasArg(options::OPT_fno_objc_arc)) {
Argyrios Kyrtzidis72ac1202011-07-07 04:00:39 +00002285 if (const Arg *A = Args.getLastArg(options::OPT_ccc_arcmt_check,
Argyrios Kyrtzidis69325d52011-07-09 20:00:58 +00002286 options::OPT_ccc_arcmt_modify,
2287 options::OPT_ccc_arcmt_migrate)) {
Ted Kremenek30660a82012-03-06 20:06:33 +00002288 ARCMTEnabled = true;
John McCall8f0e8d22011-06-15 23:25:17 +00002289 switch (A->getOption().getID()) {
2290 default:
2291 llvm_unreachable("missed a case");
Argyrios Kyrtzidis72ac1202011-07-07 04:00:39 +00002292 case options::OPT_ccc_arcmt_check:
John McCall8f0e8d22011-06-15 23:25:17 +00002293 CmdArgs.push_back("-arcmt-check");
2294 break;
Argyrios Kyrtzidis72ac1202011-07-07 04:00:39 +00002295 case options::OPT_ccc_arcmt_modify:
John McCall8f0e8d22011-06-15 23:25:17 +00002296 CmdArgs.push_back("-arcmt-modify");
2297 break;
Argyrios Kyrtzidis69325d52011-07-09 20:00:58 +00002298 case options::OPT_ccc_arcmt_migrate:
2299 CmdArgs.push_back("-arcmt-migrate");
Ted Kremenek30660a82012-03-06 20:06:33 +00002300 CmdArgs.push_back("-mt-migrate-directory");
Richard Smith1d489cf2012-11-01 04:30:05 +00002301 CmdArgs.push_back(A->getValue());
Argyrios Kyrtzidis7ee20492011-07-19 17:20:03 +00002302
2303 Args.AddLastArg(CmdArgs, options::OPT_arcmt_migrate_report_output);
2304 Args.AddLastArg(CmdArgs, options::OPT_arcmt_migrate_emit_arc_errors);
Argyrios Kyrtzidis69325d52011-07-09 20:00:58 +00002305 break;
John McCall8f0e8d22011-06-15 23:25:17 +00002306 }
2307 }
2308 }
Eric Christopher88b7cf02011-08-19 00:30:14 +00002309
Ted Kremenek30660a82012-03-06 20:06:33 +00002310 if (const Arg *A = Args.getLastArg(options::OPT_ccc_objcmt_migrate)) {
2311 if (ARCMTEnabled) {
2312 D.Diag(diag::err_drv_argument_not_allowed_with)
2313 << A->getAsString(Args) << "-ccc-arcmt-migrate";
2314 }
2315 CmdArgs.push_back("-mt-migrate-directory");
Richard Smith1d489cf2012-11-01 04:30:05 +00002316 CmdArgs.push_back(A->getValue());
Ted Kremenek30660a82012-03-06 20:06:33 +00002317
2318 if (!Args.hasArg(options::OPT_objcmt_migrate_literals,
2319 options::OPT_objcmt_migrate_subscripting)) {
2320 // None specified, means enable them all.
2321 CmdArgs.push_back("-objcmt-migrate-literals");
2322 CmdArgs.push_back("-objcmt-migrate-subscripting");
2323 } else {
2324 Args.AddLastArg(CmdArgs, options::OPT_objcmt_migrate_literals);
2325 Args.AddLastArg(CmdArgs, options::OPT_objcmt_migrate_subscripting);
2326 }
2327 }
2328
Daniel Dunbarc21c4852009-04-08 23:54:23 +00002329 // Add preprocessing options like -I, -D, etc. if we are using the
2330 // preprocessor.
2331 //
2332 // FIXME: Support -fpreprocessed
Daniel Dunbarc21c4852009-04-08 23:54:23 +00002333 if (types::getPreprocessedType(InputType) != types::TY_INVALID)
Chad Rosier9d718632013-01-24 19:14:47 +00002334 AddPreprocessingOptions(C, JA, D, Args, CmdArgs, Output, Inputs);
Daniel Dunbar1d460332009-03-18 10:01:51 +00002335
Rafael Espindola19d9d2e2011-07-21 23:40:37 +00002336 // Don't warn about "clang -c -DPIC -fPIC test.i" because libtool.m4 assumes
2337 // that "The compiler can only warn and ignore the option if not recognized".
2338 // When building with ccache, it will pass -D options to clang even on
2339 // preprocessed inputs and configure concludes that -fPIC is not supported.
2340 Args.ClaimAllArgs(options::OPT_D);
2341
Daniel Dunbar20f0eac2009-09-17 06:53:36 +00002342 // Manually translate -O to -O2 and -O4 to -O3; let clang reject
Daniel Dunbar337a6272009-03-24 20:17:30 +00002343 // others.
2344 if (Arg *A = Args.getLastArg(options::OPT_O_Group)) {
Daniel Dunbarb827a052009-11-19 03:26:40 +00002345 if (A->getOption().matches(options::OPT_O4))
Daniel Dunbar337a6272009-03-24 20:17:30 +00002346 CmdArgs.push_back("-O3");
Daniel Dunbar473916c2010-05-27 06:51:08 +00002347 else if (A->getOption().matches(options::OPT_O) &&
Richard Smith1d489cf2012-11-01 04:30:05 +00002348 A->getValue()[0] == '\0')
Daniel Dunbar20f0eac2009-09-17 06:53:36 +00002349 CmdArgs.push_back("-O2");
Daniel Dunbar1d460332009-03-18 10:01:51 +00002350 else
Daniel Dunbar5697aa02009-03-18 23:39:35 +00002351 A->render(Args, CmdArgs);
Daniel Dunbar1d460332009-03-18 10:01:51 +00002352 }
2353
Chad Rosierb2c08872012-12-12 20:06:31 +00002354 // Don't warn about unused -flto. This can happen when we're preprocessing or
2355 // precompiling.
2356 Args.ClaimAllArgs(options::OPT_flto);
2357
Daniel Dunbar6e8371e2009-10-29 02:24:45 +00002358 Args.AddAllArgs(CmdArgs, options::OPT_W_Group);
Ted Kremeneke8cf7d12012-07-07 05:53:30 +00002359 if (Args.hasFlag(options::OPT_pedantic, options::OPT_no_pedantic, false))
2360 CmdArgs.push_back("-pedantic");
Daniel Dunbar6e8371e2009-10-29 02:24:45 +00002361 Args.AddLastArg(CmdArgs, options::OPT_pedantic_errors);
Daniel Dunbar1d460332009-03-18 10:01:51 +00002362 Args.AddLastArg(CmdArgs, options::OPT_w);
Daniel Dunbard573d262009-04-07 22:13:21 +00002363
2364 // Handle -{std, ansi, trigraphs} -- take the last of -{std, ansi}
2365 // (-ansi is equivalent to -std=c89).
2366 //
2367 // If a std is supplied, only add -trigraphs if it follows the
2368 // option.
2369 if (Arg *Std = Args.getLastArg(options::OPT_std_EQ, options::OPT_ansi)) {
2370 if (Std->getOption().matches(options::OPT_ansi))
Nuno Lopes528365d2009-10-16 14:28:06 +00002371 if (types::isCXX(InputType))
Daniel Dunbar294691e2009-11-04 06:24:38 +00002372 CmdArgs.push_back("-std=c++98");
Nuno Lopes528365d2009-10-16 14:28:06 +00002373 else
Daniel Dunbar294691e2009-11-04 06:24:38 +00002374 CmdArgs.push_back("-std=c89");
Daniel Dunbard573d262009-04-07 22:13:21 +00002375 else
2376 Std->render(Args, CmdArgs);
2377
Daniel Dunbar0e100312010-06-14 21:23:08 +00002378 if (Arg *A = Args.getLastArg(options::OPT_std_EQ, options::OPT_ansi,
2379 options::OPT_trigraphs))
2380 if (A != Std)
Daniel Dunbard573d262009-04-07 22:13:21 +00002381 A->render(Args, CmdArgs);
Daniel Dunbara3ff2022009-04-26 01:10:38 +00002382 } else {
2383 // Honor -std-default.
Daniel Dunbar4a5290e2010-01-29 21:03:02 +00002384 //
2385 // FIXME: Clang doesn't correctly handle -std= when the input language
2386 // doesn't match. For the time being just ignore this for C++ inputs;
2387 // eventually we want to do all the standard defaulting here instead of
2388 // splitting it between the driver and clang -cc1.
2389 if (!types::isCXX(InputType))
Nico Weber50f88b92012-08-30 02:08:31 +00002390 Args.AddAllArgsTranslated(CmdArgs, options::OPT_std_default_EQ,
2391 "-std=", /*Joined=*/true);
2392 else if (getToolChain().getTriple().getOS() == llvm::Triple::Win32)
2393 CmdArgs.push_back("-std=c++11");
2394
Daniel Dunbard573d262009-04-07 22:13:21 +00002395 Args.AddLastArg(CmdArgs, options::OPT_trigraphs);
Daniel Dunbara3ff2022009-04-26 01:10:38 +00002396 }
Daniel Dunbarc21c4852009-04-08 23:54:23 +00002397
Chandler Carruth50465d12011-04-23 06:30:43 +00002398 // Map the bizarre '-Wwrite-strings' flag to a more sensible
2399 // '-fconst-strings'; this better indicates its actual behavior.
2400 if (Args.hasFlag(options::OPT_Wwrite_strings, options::OPT_Wno_write_strings,
2401 false)) {
2402 // For perfect compatibility with GCC, we do this even in the presence of
2403 // '-w'. This flag names something other than a warning for GCC.
2404 CmdArgs.push_back("-fconst-strings");
2405 }
2406
Chandler Carruth1cfe3c32011-04-23 09:27:53 +00002407 // GCC provides a macro definition '__DEPRECATED' when -Wdeprecated is active
Chandler Carruthf8c247d2011-04-23 19:48:40 +00002408 // during C++ compilation, which it is by default. GCC keeps this define even
2409 // in the presence of '-w', match this behavior bug-for-bug.
2410 if (types::isCXX(InputType) &&
2411 Args.hasFlag(options::OPT_Wdeprecated, options::OPT_Wno_deprecated,
2412 true)) {
2413 CmdArgs.push_back("-fdeprecated-macro");
Chandler Carruth1cfe3c32011-04-23 09:27:53 +00002414 }
2415
Chandler Carruthc304ba32010-05-22 02:21:53 +00002416 // Translate GCC's misnamer '-fasm' arguments to '-fgnu-keywords'.
2417 if (Arg *Asm = Args.getLastArg(options::OPT_fasm, options::OPT_fno_asm)) {
2418 if (Asm->getOption().matches(options::OPT_fasm))
2419 CmdArgs.push_back("-fgnu-keywords");
2420 else
2421 CmdArgs.push_back("-fno-gnu-keywords");
2422 }
2423
Rafael Espindola61b1efe2011-05-02 17:43:32 +00002424 if (ShouldDisableCFI(Args, getToolChain()))
2425 CmdArgs.push_back("-fno-dwarf2-cfi-asm");
Rafael Espindolaf24a1512011-04-30 18:35:43 +00002426
Nick Lewyckyea523d72011-10-17 23:05:52 +00002427 if (ShouldDisableDwarfDirectory(Args, getToolChain()))
2428 CmdArgs.push_back("-fno-dwarf-directory-asm");
2429
Chandler Carruthd566df62012-12-17 21:40:04 +00002430 // Add in -fdebug-compilation-dir if necessary.
2431 addDebugCompDirArg(Args, CmdArgs);
Nick Lewycky7c4fd912011-10-21 02:32:14 +00002432
Richard Smithc18c4232011-11-21 19:36:32 +00002433 if (Arg *A = Args.getLastArg(options::OPT_ftemplate_depth_,
2434 options::OPT_ftemplate_depth_EQ)) {
Daniel Dunbar1d460332009-03-18 10:01:51 +00002435 CmdArgs.push_back("-ftemplate-depth");
Richard Smith1d489cf2012-11-01 04:30:05 +00002436 CmdArgs.push_back(A->getValue());
Daniel Dunbar1d460332009-03-18 10:01:51 +00002437 }
2438
Richard Smithc18c4232011-11-21 19:36:32 +00002439 if (Arg *A = Args.getLastArg(options::OPT_fconstexpr_depth_EQ)) {
2440 CmdArgs.push_back("-fconstexpr-depth");
Richard Smith1d489cf2012-11-01 04:30:05 +00002441 CmdArgs.push_back(A->getValue());
Richard Smithc18c4232011-11-21 19:36:32 +00002442 }
2443
Argyrios Kyrtzidis1380a142010-11-18 00:20:36 +00002444 if (Arg *A = Args.getLastArg(options::OPT_Wlarge_by_value_copy_EQ,
2445 options::OPT_Wlarge_by_value_copy_def)) {
Jean-Daniel Dupas2e4fd6d2012-05-04 08:08:37 +00002446 if (A->getNumValues()) {
Richard Smith1d489cf2012-11-01 04:30:05 +00002447 StringRef bytes = A->getValue();
Jean-Daniel Dupas2e4fd6d2012-05-04 08:08:37 +00002448 CmdArgs.push_back(Args.MakeArgString("-Wlarge-by-value-copy=" + bytes));
2449 } else
2450 CmdArgs.push_back("-Wlarge-by-value-copy=64"); // default value
Argyrios Kyrtzidis3532fdd2010-11-17 23:11:54 +00002451 }
2452
Nuno Lopesb3198a82012-05-08 22:10:46 +00002453
Michael J. Spencerc6357102012-10-22 22:13:48 +00002454 if (Args.hasArg(options::OPT_relocatable_pch))
Daniel Dunbar66861e02009-11-20 22:21:36 +00002455 CmdArgs.push_back("-relocatable-pch");
Mike Stump1eb44332009-09-09 15:08:12 +00002456
Daniel Dunbar294691e2009-11-04 06:24:38 +00002457 if (Arg *A = Args.getLastArg(options::OPT_fconstant_string_class_EQ)) {
2458 CmdArgs.push_back("-fconstant-string-class");
Richard Smith1d489cf2012-11-01 04:30:05 +00002459 CmdArgs.push_back(A->getValue());
Daniel Dunbar294691e2009-11-04 06:24:38 +00002460 }
David Chisnall8a5a9aa2009-08-31 16:41:57 +00002461
Chris Lattner124fca52010-01-09 21:54:33 +00002462 if (Arg *A = Args.getLastArg(options::OPT_ftabstop_EQ)) {
2463 CmdArgs.push_back("-ftabstop");
Richard Smith1d489cf2012-11-01 04:30:05 +00002464 CmdArgs.push_back(A->getValue());
Chris Lattner124fca52010-01-09 21:54:33 +00002465 }
2466
Chris Lattner0f0c9632010-04-07 20:49:23 +00002467 CmdArgs.push_back("-ferror-limit");
2468 if (Arg *A = Args.getLastArg(options::OPT_ferror_limit_EQ))
Richard Smith1d489cf2012-11-01 04:30:05 +00002469 CmdArgs.push_back(A->getValue());
Chris Lattner0f0c9632010-04-07 20:49:23 +00002470 else
2471 CmdArgs.push_back("19");
Douglas Gregor575cf372010-04-20 07:18:24 +00002472
Chandler Carruthc40f73c2010-05-06 04:55:18 +00002473 if (Arg *A = Args.getLastArg(options::OPT_fmacro_backtrace_limit_EQ)) {
2474 CmdArgs.push_back("-fmacro-backtrace-limit");
Richard Smith1d489cf2012-11-01 04:30:05 +00002475 CmdArgs.push_back(A->getValue());
Chandler Carruthc40f73c2010-05-06 04:55:18 +00002476 }
2477
2478 if (Arg *A = Args.getLastArg(options::OPT_ftemplate_backtrace_limit_EQ)) {
2479 CmdArgs.push_back("-ftemplate-backtrace-limit");
Richard Smith1d489cf2012-11-01 04:30:05 +00002480 CmdArgs.push_back(A->getValue());
Chandler Carruthc40f73c2010-05-06 04:55:18 +00002481 }
2482
Richard Smith08d6e032011-12-16 19:06:07 +00002483 if (Arg *A = Args.getLastArg(options::OPT_fconstexpr_backtrace_limit_EQ)) {
2484 CmdArgs.push_back("-fconstexpr-backtrace-limit");
Richard Smith1d489cf2012-11-01 04:30:05 +00002485 CmdArgs.push_back(A->getValue());
Richard Smith08d6e032011-12-16 19:06:07 +00002486 }
2487
Daniel Dunbar55efe142009-11-04 06:24:47 +00002488 // Pass -fmessage-length=.
Daniel Dunbara28690e2009-11-30 08:40:54 +00002489 CmdArgs.push_back("-fmessage-length");
Daniel Dunbar55efe142009-11-04 06:24:47 +00002490 if (Arg *A = Args.getLastArg(options::OPT_fmessage_length_EQ)) {
Richard Smith1d489cf2012-11-01 04:30:05 +00002491 CmdArgs.push_back(A->getValue());
Daniel Dunbar55efe142009-11-04 06:24:47 +00002492 } else {
2493 // If -fmessage-length=N was not specified, determine whether this is a
2494 // terminal and, if so, implicitly define -fmessage-length appropriately.
2495 unsigned N = llvm::sys::Process::StandardErrColumns();
Chris Lattner5f9e2722011-07-23 10:55:15 +00002496 CmdArgs.push_back(Args.MakeArgString(Twine(N)));
Daniel Dunbar55efe142009-11-04 06:24:47 +00002497 }
2498
Daniel Dunbarba8d8612009-12-03 18:42:11 +00002499 if (const Arg *A = Args.getLastArg(options::OPT_fvisibility_EQ)) {
2500 CmdArgs.push_back("-fvisibility");
Richard Smith1d489cf2012-11-01 04:30:05 +00002501 CmdArgs.push_back(A->getValue());
Daniel Dunbarba8d8612009-12-03 18:42:11 +00002502 }
2503
Douglas Gregor7cf84d62010-06-15 17:05:35 +00002504 Args.AddLastArg(CmdArgs, options::OPT_fvisibility_inlines_hidden);
Michael J. Spencer20249a12010-10-21 03:16:25 +00002505
Hans Wennborgde981f32012-06-28 08:01:44 +00002506 Args.AddLastArg(CmdArgs, options::OPT_ftlsmodel_EQ);
2507
Daniel Dunbar0a80ba72010-03-20 04:52:14 +00002508 // -fhosted is default.
Chad Rosierafc4baa2012-03-26 22:04:46 +00002509 if (Args.hasFlag(options::OPT_ffreestanding, options::OPT_fhosted, false) ||
2510 KernelOrKext)
Daniel Dunbar0a80ba72010-03-20 04:52:14 +00002511 CmdArgs.push_back("-ffreestanding");
2512
Daniel Dunbarba8d8612009-12-03 18:42:11 +00002513 // Forward -f (flag) options which we can pass directly.
Daniel Dunbar3aaf0822009-04-07 21:51:40 +00002514 Args.AddLastArg(CmdArgs, options::OPT_femit_all_decls);
Daniel Dunbar3aaf0822009-04-07 21:51:40 +00002515 Args.AddLastArg(CmdArgs, options::OPT_fheinous_gnu_extensions);
Devang Patelc69e1cf2010-09-30 19:05:55 +00002516 Args.AddLastArg(CmdArgs, options::OPT_flimit_debug_info);
Devang Patel033be8b2011-11-04 20:05:58 +00002517 Args.AddLastArg(CmdArgs, options::OPT_fno_limit_debug_info);
Eric Christophere88c4512011-10-25 07:13:06 +00002518 Args.AddLastArg(CmdArgs, options::OPT_fno_operator_names);
Anton Yartsev17ba2672011-12-23 20:23:19 +00002519 Args.AddLastArg(CmdArgs, options::OPT_faltivec);
Richard Trieu246b6aa2012-06-26 18:18:47 +00002520 Args.AddLastArg(CmdArgs, options::OPT_fdiagnostics_show_template_tree);
2521 Args.AddLastArg(CmdArgs, options::OPT_fno_elide_type);
Chad Rosier4574c3d2012-03-13 23:45:51 +00002522
Alexey Samsonovbb1071c2012-11-06 15:09:03 +00002523 SanitizerArgs Sanitize(D, Args);
Richard Smithc4dabad2012-11-05 22:04:41 +00002524 Sanitize.addArgs(Args, CmdArgs);
2525
Will Dietz2d382d12012-12-30 20:53:28 +00002526 if (!Args.hasFlag(options::OPT_fsanitize_recover,
2527 options::OPT_fno_sanitize_recover,
2528 true))
2529 CmdArgs.push_back("-fno-sanitize-recover");
2530
Chad Rosier78d85b12013-01-29 23:31:22 +00002531 if (Args.hasArg(options::OPT_fcatch_undefined_behavior) ||
2532 Args.hasFlag(options::OPT_fsanitize_undefined_trap_on_error,
2533 options::OPT_fno_sanitize_undefined_trap_on_error, false))
2534 CmdArgs.push_back("-fsanitize-undefined-trap-on-error");
2535
Chad Rosier4574c3d2012-03-13 23:45:51 +00002536 // Report and error for -faltivec on anything other then PowerPC.
2537 if (const Arg *A = Args.getLastArg(options::OPT_faltivec))
2538 if (!(getToolChain().getTriple().getArch() == llvm::Triple::ppc ||
2539 getToolChain().getTriple().getArch() == llvm::Triple::ppc64))
2540 D.Diag(diag::err_drv_argument_only_allowed_with)
2541 << A->getAsString(Args) << "ppc/ppc64";
2542
Daniel Dunbarbbe8e3e2011-03-01 18:49:30 +00002543 if (getToolChain().SupportsProfiling())
2544 Args.AddLastArg(CmdArgs, options::OPT_pg);
Daniel Dunbar8c6fa842010-03-16 16:57:46 +00002545
2546 // -flax-vector-conversions is default.
2547 if (!Args.hasFlag(options::OPT_flax_vector_conversions,
2548 options::OPT_fno_lax_vector_conversions))
2549 CmdArgs.push_back("-fno-lax-vector-conversions");
2550
Fariborz Jahanianb466d012011-01-07 01:05:02 +00002551 if (Args.getLastArg(options::OPT_fapple_kext))
2552 CmdArgs.push_back("-fapple-kext");
2553
David Blaikie940152f2012-06-14 18:55:27 +00002554 if (Args.hasFlag(options::OPT_frewrite_includes,
2555 options::OPT_fno_rewrite_includes, false))
2556 CmdArgs.push_back("-frewrite-includes");
2557
Fariborz Jahanian34e65772009-05-22 20:17:16 +00002558 Args.AddLastArg(CmdArgs, options::OPT_fobjc_sender_dependent_dispatch);
Chris Lattner182e0922009-04-21 05:34:31 +00002559 Args.AddLastArg(CmdArgs, options::OPT_fdiagnostics_print_source_range_info);
Douglas Gregor4786c152010-08-19 20:24:43 +00002560 Args.AddLastArg(CmdArgs, options::OPT_fdiagnostics_parseable_fixits);
Daniel Dunbar3aaf0822009-04-07 21:51:40 +00002561 Args.AddLastArg(CmdArgs, options::OPT_ftime_report);
2562 Args.AddLastArg(CmdArgs, options::OPT_ftrapv);
David Chisnall7f18e672010-09-17 18:29:54 +00002563
2564 if (Arg *A = Args.getLastArg(options::OPT_ftrapv_handler_EQ)) {
2565 CmdArgs.push_back("-ftrapv-handler");
Richard Smith1d489cf2012-11-01 04:30:05 +00002566 CmdArgs.push_back(A->getValue());
David Chisnall7f18e672010-09-17 18:29:54 +00002567 }
2568
Bob Wilson71fd6cc2012-02-03 06:27:22 +00002569 Args.AddLastArg(CmdArgs, options::OPT_ftrap_function_EQ);
Evan Cheng49af1f32011-04-08 21:37:45 +00002570
Chandler Carruth5adb5a82011-03-27 00:04:55 +00002571 // -fno-strict-overflow implies -fwrapv if it isn't disabled, but
2572 // -fstrict-overflow won't turn off an explicitly enabled -fwrapv.
2573 if (Arg *A = Args.getLastArg(options::OPT_fwrapv,
2574 options::OPT_fno_wrapv)) {
2575 if (A->getOption().matches(options::OPT_fwrapv))
2576 CmdArgs.push_back("-fwrapv");
2577 } else if (Arg *A = Args.getLastArg(options::OPT_fstrict_overflow,
2578 options::OPT_fno_strict_overflow)) {
2579 if (A->getOption().matches(options::OPT_fno_strict_overflow))
2580 CmdArgs.push_back("-fwrapv");
2581 }
Daniel Dunbar3aaf0822009-04-07 21:51:40 +00002582 Args.AddLastArg(CmdArgs, options::OPT_fwritable_strings);
Eric Christopherf84d4092010-08-07 23:08:14 +00002583 Args.AddLastArg(CmdArgs, options::OPT_funroll_loops);
Daniel Dunbar1d460332009-03-18 10:01:51 +00002584
Daniel Dunbar5345c392009-09-03 04:54:28 +00002585 Args.AddLastArg(CmdArgs, options::OPT_pthread);
2586
Mahesha Sf3b52312012-10-27 07:47:56 +00002587
Daniel Dunbar9e5cc6b2009-11-17 08:07:36 +00002588 // -stack-protector=0 is default.
2589 unsigned StackProtectorLevel = 0;
Bill Wendling45483f72009-06-28 07:36:13 +00002590 if (Arg *A = Args.getLastArg(options::OPT_fno_stack_protector,
2591 options::OPT_fstack_protector_all,
2592 options::OPT_fstack_protector)) {
Daniel Dunbar9e5cc6b2009-11-17 08:07:36 +00002593 if (A->getOption().matches(options::OPT_fstack_protector))
2594 StackProtectorLevel = 1;
2595 else if (A->getOption().matches(options::OPT_fstack_protector_all))
2596 StackProtectorLevel = 2;
Nico Weber2fef1112011-08-23 07:38:27 +00002597 } else {
2598 StackProtectorLevel =
2599 getToolChain().GetDefaultStackProtectorLevel(KernelOrKext);
2600 }
Daniel Dunbar9e5cc6b2009-11-17 08:07:36 +00002601 if (StackProtectorLevel) {
2602 CmdArgs.push_back("-stack-protector");
Chris Lattner5f9e2722011-07-23 10:55:15 +00002603 CmdArgs.push_back(Args.MakeArgString(Twine(StackProtectorLevel)));
Joerg Sonnenberger53b43a72012-09-12 13:51:14 +00002604 }
Chad Rosiera7afeb02012-08-21 16:16:06 +00002605
Joerg Sonnenberger53b43a72012-09-12 13:51:14 +00002606 // --param ssp-buffer-size=
2607 for (arg_iterator it = Args.filtered_begin(options::OPT__param),
2608 ie = Args.filtered_end(); it != ie; ++it) {
Richard Smith1d489cf2012-11-01 04:30:05 +00002609 StringRef Str((*it)->getValue());
Joerg Sonnenberger53b43a72012-09-12 13:51:14 +00002610 if (Str.startswith("ssp-buffer-size=")) {
2611 if (StackProtectorLevel) {
Chad Rosiera7afeb02012-08-21 16:16:06 +00002612 CmdArgs.push_back("-stack-protector-buffer-size");
2613 // FIXME: Verify the argument is a valid integer.
2614 CmdArgs.push_back(Args.MakeArgString(Str.drop_front(16)));
Chad Rosiera7afeb02012-08-21 16:16:06 +00002615 }
Joerg Sonnenberger53b43a72012-09-12 13:51:14 +00002616 (*it)->claim();
Chad Rosiera7afeb02012-08-21 16:16:06 +00002617 }
Bill Wendling45483f72009-06-28 07:36:13 +00002618 }
2619
Nick Lewycky4e785c92011-12-06 03:33:03 +00002620 // Translate -mstackrealign
2621 if (Args.hasFlag(options::OPT_mstackrealign, options::OPT_mno_stackrealign,
2622 false)) {
2623 CmdArgs.push_back("-backend-option");
2624 CmdArgs.push_back("-force-align-stack");
2625 }
2626 if (!Args.hasFlag(options::OPT_mno_stackrealign, options::OPT_mstackrealign,
2627 false)) {
2628 CmdArgs.push_back(Args.MakeArgString("-mstackrealign"));
2629 }
2630
Joerg Sonnenbergere9d11db2011-12-05 23:05:23 +00002631 if (Args.hasArg(options::OPT_mstack_alignment)) {
2632 StringRef alignment = Args.getLastArgValue(options::OPT_mstack_alignment);
2633 CmdArgs.push_back(Args.MakeArgString("-mstack-alignment=" + alignment));
Eric Christopher1a584022011-05-02 21:18:22 +00002634 }
Chad Rosier586a0612012-11-29 00:42:06 +00002635 // -mkernel implies -mstrict-align; don't add the redundant option.
2636 if (Args.hasArg(options::OPT_mstrict_align) && !KernelOrKext) {
Chad Rosier485577d2012-11-09 18:27:01 +00002637 CmdArgs.push_back("-backend-option");
2638 CmdArgs.push_back("-arm-strict-align");
Chad Rosier7e293272012-11-09 17:29:19 +00002639 }
Eric Christopher88b7cf02011-08-19 00:30:14 +00002640
Daniel Dunbar48d1ef72009-04-07 21:16:11 +00002641 // Forward -f options with positive and negative forms; we translate
2642 // these by hand.
2643
Fariborz Jahanianb466d012011-01-07 01:05:02 +00002644 if (Args.hasArg(options::OPT_mkernel)) {
Daniel Dunbar2843c192011-02-04 17:24:47 +00002645 if (!Args.hasArg(options::OPT_fapple_kext) && types::isCXX(InputType))
Fariborz Jahanianb466d012011-01-07 01:05:02 +00002646 CmdArgs.push_back("-fapple-kext");
2647 if (!Args.hasArg(options::OPT_fbuiltin))
2648 CmdArgs.push_back("-fno-builtin");
Chad Rosier3d265502012-03-26 21:29:17 +00002649 Args.ClaimAllArgs(options::OPT_fno_builtin);
Fariborz Jahanianb466d012011-01-07 01:05:02 +00002650 }
Daniel Dunbar9e5cc6b2009-11-17 08:07:36 +00002651 // -fbuiltin is default.
Fariborz Jahanianb466d012011-01-07 01:05:02 +00002652 else if (!Args.hasFlag(options::OPT_fbuiltin, options::OPT_fno_builtin))
Daniel Dunbar53e84842009-11-19 04:55:23 +00002653 CmdArgs.push_back("-fno-builtin");
Daniel Dunbar48d1ef72009-04-07 21:16:11 +00002654
Nuno Lopesfc284482009-12-16 16:59:22 +00002655 if (!Args.hasFlag(options::OPT_fassume_sane_operator_new,
2656 options::OPT_fno_assume_sane_operator_new))
2657 CmdArgs.push_back("-fno-assume-sane-operator-new");
2658
Daniel Dunbar9e5cc6b2009-11-17 08:07:36 +00002659 // -fblocks=0 is default.
2660 if (Args.hasFlag(options::OPT_fblocks, options::OPT_fno_blocks,
David Chisnalle6533ff2011-02-28 17:11:43 +00002661 getToolChain().IsBlocksDefault()) ||
2662 (Args.hasArg(options::OPT_fgnu_runtime) &&
2663 Args.hasArg(options::OPT_fobjc_nonfragile_abi) &&
2664 !Args.hasArg(options::OPT_fno_blocks))) {
Daniel Dunbar9e5cc6b2009-11-17 08:07:36 +00002665 CmdArgs.push_back("-fblocks");
John McCall13db5cf2011-09-09 20:41:01 +00002666
2667 if (!Args.hasArg(options::OPT_fgnu_runtime) &&
2668 !getToolChain().hasBlocksRuntime())
2669 CmdArgs.push_back("-fblocks-runtime-optional");
David Chisnall5e530af2009-11-17 19:33:30 +00002670 }
Daniel Dunbar48d1ef72009-04-07 21:16:11 +00002671
Douglas Gregor64554ba2012-01-18 15:19:58 +00002672 // -fmodules enables modules (off by default). However, for C++/Objective-C++,
2673 // users must also pass -fcxx-modules. The latter flag will disappear once the
2674 // modules implementation is solid for C++/Objective-C++ programs as well.
Douglas Gregorf43b7212013-01-16 01:23:41 +00002675 bool HaveModules = false;
Douglas Gregor64554ba2012-01-18 15:19:58 +00002676 if (Args.hasFlag(options::OPT_fmodules, options::OPT_fno_modules, false)) {
2677 bool AllowedInCXX = Args.hasFlag(options::OPT_fcxx_modules,
2678 options::OPT_fno_cxx_modules,
2679 false);
Douglas Gregorf43b7212013-01-16 01:23:41 +00002680 if (AllowedInCXX || !types::isCXX(InputType)) {
Douglas Gregor64554ba2012-01-18 15:19:58 +00002681 CmdArgs.push_back("-fmodules");
Douglas Gregorf43b7212013-01-16 01:23:41 +00002682 HaveModules = true;
2683 }
2684 }
2685
2686 // -fmodules-autolink (on by default when modules is enabled) automatically
2687 // links against libraries for imported modules.
2688 if (HaveModules &&
2689 Args.hasFlag(options::OPT_fmodules_autolink,
2690 options::OPT_fno_modules_autolink,
2691 true)) {
2692 CmdArgs.push_back("-fmodules-autolink");
Douglas Gregor64554ba2012-01-18 15:19:58 +00002693 }
Douglas Gregor7025d2c2012-01-03 17:13:05 +00002694
John McCall32579cf2010-04-09 19:12:06 +00002695 // -faccess-control is default.
John McCall7002f4c2010-04-09 19:03:51 +00002696 if (Args.hasFlag(options::OPT_fno_access_control,
2697 options::OPT_faccess_control,
John McCall32579cf2010-04-09 19:12:06 +00002698 false))
John McCall7002f4c2010-04-09 19:03:51 +00002699 CmdArgs.push_back("-fno-access-control");
John McCall3ddd6e02010-03-17 01:32:13 +00002700
Anders Carlssona4c24752010-11-21 00:09:52 +00002701 // -felide-constructors is the default.
2702 if (Args.hasFlag(options::OPT_fno_elide_constructors,
2703 options::OPT_felide_constructors,
2704 false))
2705 CmdArgs.push_back("-fno-elide-constructors");
2706
Daniel Dunbar0be42c42009-11-17 07:06:20 +00002707 // -frtti is default.
Chad Rosierafc4baa2012-03-26 22:04:46 +00002708 if (!Args.hasFlag(options::OPT_frtti, options::OPT_fno_rtti) ||
Richard Smithc4dabad2012-11-05 22:04:41 +00002709 KernelOrKext) {
Daniel Dunbar53e84842009-11-19 04:55:23 +00002710 CmdArgs.push_back("-fno-rtti");
Mike Stump738f8c22009-07-31 23:15:31 +00002711
Richard Smithc4dabad2012-11-05 22:04:41 +00002712 // -fno-rtti cannot usefully be combined with -fsanitize=vptr.
Alexey Samsonovbb1071c2012-11-06 15:09:03 +00002713 if (Sanitize.sanitizesVptr()) {
NAKAMURA Takumi03c60762012-11-06 22:02:00 +00002714 std::string NoRttiArg =
Richard Smithc4dabad2012-11-05 22:04:41 +00002715 Args.getLastArg(options::OPT_mkernel,
2716 options::OPT_fapple_kext,
Richard Smith04fd3822012-11-06 01:12:02 +00002717 options::OPT_fno_rtti)->getAsString(Args);
Richard Smithc4dabad2012-11-05 22:04:41 +00002718 D.Diag(diag::err_drv_argument_not_allowed_with)
2719 << "-fsanitize=vptr" << NoRttiArg;
2720 }
2721 }
2722
Tony Linthicum96319392011-12-12 21:14:55 +00002723 // -fshort-enums=0 is default for all architectures except Hexagon.
Argyrios Kyrtzidis9a2b9d72010-10-08 00:25:19 +00002724 if (Args.hasFlag(options::OPT_fshort_enums,
Tony Linthicum96319392011-12-12 21:14:55 +00002725 options::OPT_fno_short_enums,
2726 getToolChain().getTriple().getArch() ==
2727 llvm::Triple::hexagon))
Argyrios Kyrtzidis9a2b9d72010-10-08 00:25:19 +00002728 CmdArgs.push_back("-fshort-enums");
2729
Daniel Dunbar1f95e652009-11-17 06:37:03 +00002730 // -fsigned-char is default.
Daniel Dunbar6d2eb4d2009-11-25 10:14:30 +00002731 if (!Args.hasFlag(options::OPT_fsigned_char, options::OPT_funsigned_char,
Daniel Dunbar1f95e652009-11-17 06:37:03 +00002732 isSignedCharDefault(getToolChain().getTriple())))
Daniel Dunbar76743522009-11-29 02:39:08 +00002733 CmdArgs.push_back("-fno-signed-char");
Eli Friedman5a779732009-06-05 07:21:14 +00002734
Anders Carlssona508b7d2010-02-06 23:23:06 +00002735 // -fthreadsafe-static is default.
Michael J. Spencer20249a12010-10-21 03:16:25 +00002736 if (!Args.hasFlag(options::OPT_fthreadsafe_statics,
Anders Carlssona508b7d2010-02-06 23:23:06 +00002737 options::OPT_fno_threadsafe_statics))
2738 CmdArgs.push_back("-fno-threadsafe-statics");
2739
Daniel Dunbarefb0fa92010-03-20 04:15:41 +00002740 // -fuse-cxa-atexit is default.
Chad Rosierafc4baa2012-03-26 22:04:46 +00002741 if (!Args.hasFlag(options::OPT_fuse_cxa_atexit,
2742 options::OPT_fno_use_cxa_atexit,
2743 getToolChain().getTriple().getOS() != llvm::Triple::Cygwin &&
Tony Linthicum96319392011-12-12 21:14:55 +00002744 getToolChain().getTriple().getOS() != llvm::Triple::MinGW32 &&
Chad Rosierafc4baa2012-03-26 22:04:46 +00002745 getToolChain().getTriple().getArch() != llvm::Triple::hexagon) ||
2746 KernelOrKext)
Daniel Dunbarefb0fa92010-03-20 04:15:41 +00002747 CmdArgs.push_back("-fno-use-cxa-atexit");
2748
Daniel Dunbar0be42c42009-11-17 07:06:20 +00002749 // -fms-extensions=0 is default.
Daniel Dunbar6d2eb4d2009-11-25 10:14:30 +00002750 if (Args.hasFlag(options::OPT_fms_extensions, options::OPT_fno_ms_extensions,
Daniel Dunbar0be42c42009-11-17 07:06:20 +00002751 getToolChain().getTriple().getOS() == llvm::Triple::Win32))
2752 CmdArgs.push_back("-fms-extensions");
2753
Francois Pichetae556082011-09-17 04:32:15 +00002754 // -fms-compatibility=0 is default.
Douglas Gregorba97b6e2011-10-24 15:49:38 +00002755 if (Args.hasFlag(options::OPT_fms_compatibility,
2756 options::OPT_fno_ms_compatibility,
2757 (getToolChain().getTriple().getOS() == llvm::Triple::Win32 &&
2758 Args.hasFlag(options::OPT_fms_extensions,
2759 options::OPT_fno_ms_extensions,
2760 true))))
Francois Pichetae556082011-09-17 04:32:15 +00002761 CmdArgs.push_back("-fms-compatibility");
2762
Michael J. Spencerdae4ac42010-10-21 05:21:48 +00002763 // -fmsc-version=1300 is default.
2764 if (Args.hasFlag(options::OPT_fms_extensions, options::OPT_fno_ms_extensions,
2765 getToolChain().getTriple().getOS() == llvm::Triple::Win32) ||
2766 Args.hasArg(options::OPT_fmsc_version)) {
Chris Lattner5f9e2722011-07-23 10:55:15 +00002767 StringRef msc_ver = Args.getLastArgValue(options::OPT_fmsc_version);
Michael J. Spencerdae4ac42010-10-21 05:21:48 +00002768 if (msc_ver.empty())
2769 CmdArgs.push_back("-fmsc-version=1300");
2770 else
2771 CmdArgs.push_back(Args.MakeArgString("-fmsc-version=" + msc_ver));
2772 }
2773
2774
Dawn Perchik400b6072010-09-02 23:59:25 +00002775 // -fborland-extensions=0 is default.
2776 if (Args.hasFlag(options::OPT_fborland_extensions,
2777 options::OPT_fno_borland_extensions, false))
2778 CmdArgs.push_back("-fborland-extensions");
2779
Francois Pichet8efcc012011-09-01 16:38:08 +00002780 // -fno-delayed-template-parsing is default, except for Windows where MSVC STL
2781 // needs it.
Francois Pichet8387e2a2011-04-22 22:18:13 +00002782 if (Args.hasFlag(options::OPT_fdelayed_template_parsing,
2783 options::OPT_fno_delayed_template_parsing,
Francois Pichet8efcc012011-09-01 16:38:08 +00002784 getToolChain().getTriple().getOS() == llvm::Triple::Win32))
Francois Pichet805bc1f2011-08-26 00:22:34 +00002785 CmdArgs.push_back("-fdelayed-template-parsing");
Francois Pichet8387e2a2011-04-22 22:18:13 +00002786
Chandler Carrutheb5d7b72010-04-17 20:17:31 +00002787 // -fgnu-keywords default varies depending on language; only pass if
2788 // specified.
2789 if (Arg *A = Args.getLastArg(options::OPT_fgnu_keywords,
Daniel Dunbar40788d92010-04-24 17:56:39 +00002790 options::OPT_fno_gnu_keywords))
2791 A->render(Args, CmdArgs);
Chandler Carrutheb5d7b72010-04-17 20:17:31 +00002792
Rafael Espindola01ba8542011-06-02 17:30:53 +00002793 if (Args.hasFlag(options::OPT_fgnu89_inline,
2794 options::OPT_fno_gnu89_inline,
2795 false))
Rafael Espindolafb3f4aa2011-06-02 16:13:27 +00002796 CmdArgs.push_back("-fgnu89-inline");
2797
Chad Rosierfc055f92012-03-15 22:31:42 +00002798 if (Args.hasArg(options::OPT_fno_inline))
2799 CmdArgs.push_back("-fno-inline");
2800
Chad Rosier634a4b12012-03-06 21:17:19 +00002801 if (Args.hasArg(options::OPT_fno_inline_functions))
2802 CmdArgs.push_back("-fno-inline-functions");
Chad Rosier250008b2012-03-06 18:49:20 +00002803
John McCall260611a2012-06-20 06:18:46 +00002804 ObjCRuntime objcRuntime = AddObjCRuntimeArgs(Args, CmdArgs, rewriteKind);
John McCall9f084a32011-07-06 00:26:06 +00002805
John McCall260611a2012-06-20 06:18:46 +00002806 // -fobjc-dispatch-method is only relevant with the nonfragile-abi, and
2807 // legacy is the default.
2808 if (objcRuntime.isNonFragile()) {
David Chisnall3c3ccd22011-09-30 13:32:35 +00002809 if (!Args.hasFlag(options::OPT_fobjc_legacy_dispatch,
2810 options::OPT_fno_objc_legacy_dispatch,
David Chisnall2c7886d2012-07-04 11:52:24 +00002811 objcRuntime.isLegacyDispatchDefaultForArch(
2812 getToolChain().getTriple().getArch()))) {
David Chisnall3c3ccd22011-09-30 13:32:35 +00002813 if (getToolChain().UseObjCMixedDispatch())
2814 CmdArgs.push_back("-fobjc-dispatch-method=mixed");
2815 else
2816 CmdArgs.push_back("-fobjc-dispatch-method=non-legacy");
2817 }
2818 }
2819
Nico Weberdf423542012-03-09 21:19:44 +00002820 // -fobjc-default-synthesize-properties=1 is default. This only has an effect
2821 // if the nonfragile objc abi is used.
Fariborz Jahaniane51fe092012-04-09 18:58:55 +00002822 if (getToolChain().IsObjCDefaultSynthPropertiesDefault()) {
David Chisnall3c3ccd22011-09-30 13:32:35 +00002823 CmdArgs.push_back("-fobjc-default-synthesize-properties");
2824 }
2825
Fariborz Jahanian3d145f62012-11-15 19:02:45 +00002826 // -fencode-extended-block-signature=1 is default.
2827 if (getToolChain().IsEncodeExtendedBlockSignatureDefault()) {
2828 CmdArgs.push_back("-fencode-extended-block-signature");
2829 }
2830
John McCall9f084a32011-07-06 00:26:06 +00002831 // Allow -fno-objc-arr to trump -fobjc-arr/-fobjc-arc.
2832 // NOTE: This logic is duplicated in ToolChains.cpp.
2833 bool ARC = isObjCAutoRefCount(Args);
2834 if (ARC) {
John McCall0a7dd782012-08-21 02:47:43 +00002835 getToolChain().CheckObjCARC();
Argyrios Kyrtzidis5840dd92012-02-29 03:43:52 +00002836
John McCall9f084a32011-07-06 00:26:06 +00002837 CmdArgs.push_back("-fobjc-arc");
2838
Chandler Carruth7ffa0322011-11-04 07:34:47 +00002839 // FIXME: It seems like this entire block, and several around it should be
2840 // wrapped in isObjC, but for now we just use it here as this is where it
2841 // was being used previously.
2842 if (types::isCXX(InputType) && types::isObjC(InputType)) {
2843 if (getToolChain().GetCXXStdlibType(Args) == ToolChain::CST_Libcxx)
2844 CmdArgs.push_back("-fobjc-arc-cxxlib=libc++");
2845 else
2846 CmdArgs.push_back("-fobjc-arc-cxxlib=libstdc++");
2847 }
2848
John McCall9f084a32011-07-06 00:26:06 +00002849 // Allow the user to enable full exceptions code emission.
2850 // We define off for Objective-CC, on for Objective-C++.
2851 if (Args.hasFlag(options::OPT_fobjc_arc_exceptions,
2852 options::OPT_fno_objc_arc_exceptions,
2853 /*default*/ types::isCXX(InputType)))
2854 CmdArgs.push_back("-fobjc-arc-exceptions");
2855 }
2856
2857 // -fobjc-infer-related-result-type is the default, except in the Objective-C
2858 // rewriter.
John McCall260611a2012-06-20 06:18:46 +00002859 if (rewriteKind != RK_None)
John McCall9f084a32011-07-06 00:26:06 +00002860 CmdArgs.push_back("-fno-objc-infer-related-result-type");
Eric Christopher88b7cf02011-08-19 00:30:14 +00002861
John McCall9f084a32011-07-06 00:26:06 +00002862 // Handle -fobjc-gc and -fobjc-gc-only. They are exclusive, and -fobjc-gc-only
2863 // takes precedence.
2864 const Arg *GCArg = Args.getLastArg(options::OPT_fobjc_gc_only);
2865 if (!GCArg)
2866 GCArg = Args.getLastArg(options::OPT_fobjc_gc);
2867 if (GCArg) {
2868 if (ARC) {
Chris Lattner5f9e2722011-07-23 10:55:15 +00002869 D.Diag(diag::err_drv_objc_gc_arr)
John McCall9f084a32011-07-06 00:26:06 +00002870 << GCArg->getAsString(Args);
2871 } else if (getToolChain().SupportsObjCGC()) {
2872 GCArg->render(Args, CmdArgs);
2873 } else {
2874 // FIXME: We should move this to a hard error.
Chris Lattner5f9e2722011-07-23 10:55:15 +00002875 D.Diag(diag::warn_drv_objc_gc_unsupported)
John McCall9f084a32011-07-06 00:26:06 +00002876 << GCArg->getAsString(Args);
2877 }
2878 }
2879
John McCalld71315c2011-06-22 00:53:57 +00002880 // Add exception args.
2881 addExceptionArgs(Args, InputType, getToolChain().getTriple(),
John McCall260611a2012-06-20 06:18:46 +00002882 KernelOrKext, objcRuntime, CmdArgs);
John McCalld71315c2011-06-22 00:53:57 +00002883
2884 if (getToolChain().UseSjLjExceptions())
2885 CmdArgs.push_back("-fsjlj-exceptions");
2886
2887 // C++ "sane" operator new.
Daniel Dunbar984eb862010-02-01 21:07:25 +00002888 if (!Args.hasFlag(options::OPT_fassume_sane_operator_new,
2889 options::OPT_fno_assume_sane_operator_new))
2890 CmdArgs.push_back("-fno-assume-sane-operator-new");
2891
Daniel Dunbarf35f14d2010-04-27 15:34:57 +00002892 // -fconstant-cfstrings is default, and may be subject to argument translation
2893 // on Darwin.
2894 if (!Args.hasFlag(options::OPT_fconstant_cfstrings,
2895 options::OPT_fno_constant_cfstrings) ||
2896 !Args.hasFlag(options::OPT_mconstant_cfstrings,
2897 options::OPT_mno_constant_cfstrings))
2898 CmdArgs.push_back("-fno-constant-cfstrings");
2899
John Thompsona6fda122009-11-05 20:14:16 +00002900 // -fshort-wchar default varies depending on platform; only
2901 // pass if specified.
Daniel Dunbar1744a352010-04-27 15:35:03 +00002902 if (Arg *A = Args.getLastArg(options::OPT_fshort_wchar))
2903 A->render(Args, CmdArgs);
John Thompsona6fda122009-11-05 20:14:16 +00002904
Daniel Dunbaree848a72009-10-29 02:39:57 +00002905 // -fno-pascal-strings is default, only pass non-default. If the tool chain
2906 // happened to translate to -mpascal-strings, we want to back translate here.
Daniel Dunbar82d00682009-04-07 23:51:44 +00002907 //
2908 // FIXME: This is gross; that translation should be pulled from the
2909 // tool chain.
Daniel Dunbarc21c4852009-04-08 23:54:23 +00002910 if (Args.hasFlag(options::OPT_fpascal_strings,
Daniel Dunbar82d00682009-04-07 23:51:44 +00002911 options::OPT_fno_pascal_strings,
2912 false) ||
2913 Args.hasFlag(options::OPT_mpascal_strings,
2914 options::OPT_mno_pascal_strings,
2915 false))
Daniel Dunbar48d1ef72009-04-07 21:16:11 +00002916 CmdArgs.push_back("-fpascal-strings");
NAKAMURA Takumi125b4cb2011-02-17 08:50:50 +00002917
Daniel Dunbar88934e82011-10-05 21:04:55 +00002918 // Honor -fpack-struct= and -fpack-struct, if given. Note that
2919 // -fno-pack-struct doesn't apply to -fpack-struct=.
2920 if (Arg *A = Args.getLastArg(options::OPT_fpack_struct_EQ)) {
James Molloy8049c442012-05-02 07:56:14 +00002921 std::string PackStructStr = "-fpack-struct=";
Richard Smith1d489cf2012-11-01 04:30:05 +00002922 PackStructStr += A->getValue();
James Molloy8049c442012-05-02 07:56:14 +00002923 CmdArgs.push_back(Args.MakeArgString(PackStructStr));
Daniel Dunbar88934e82011-10-05 21:04:55 +00002924 } else if (Args.hasFlag(options::OPT_fpack_struct,
2925 options::OPT_fno_pack_struct, false)) {
James Molloy8049c442012-05-02 07:56:14 +00002926 CmdArgs.push_back("-fpack-struct=1");
Daniel Dunbar88934e82011-10-05 21:04:55 +00002927 }
2928
Fariborz Jahanianb466d012011-01-07 01:05:02 +00002929 if (Args.hasArg(options::OPT_mkernel) ||
2930 Args.hasArg(options::OPT_fapple_kext)) {
2931 if (!Args.hasArg(options::OPT_fcommon))
2932 CmdArgs.push_back("-fno-common");
Chad Rosierec09b3e2012-03-26 21:35:40 +00002933 Args.ClaimAllArgs(options::OPT_fno_common);
Fariborz Jahanianb466d012011-01-07 01:05:02 +00002934 }
Daniel Dunbar88934e82011-10-05 21:04:55 +00002935
Daniel Dunbar48d1ef72009-04-07 21:16:11 +00002936 // -fcommon is default, only pass non-default.
Fariborz Jahanianb466d012011-01-07 01:05:02 +00002937 else if (!Args.hasFlag(options::OPT_fcommon, options::OPT_fno_common))
Daniel Dunbar48d1ef72009-04-07 21:16:11 +00002938 CmdArgs.push_back("-fno-common");
2939
Daniel Dunbar70d3c922009-04-15 02:37:43 +00002940 // -fsigned-bitfields is default, and clang doesn't yet support
Daniel Dunbar06205ca2010-10-15 22:30:42 +00002941 // -funsigned-bitfields.
Mike Stump1eb44332009-09-09 15:08:12 +00002942 if (!Args.hasFlag(options::OPT_fsigned_bitfields,
Daniel Dunbar70d3c922009-04-15 02:37:43 +00002943 options::OPT_funsigned_bitfields))
Chris Lattner5f9e2722011-07-23 10:55:15 +00002944 D.Diag(diag::warn_drv_clang_unsupported)
Daniel Dunbar70d3c922009-04-15 02:37:43 +00002945 << Args.getLastArg(options::OPT_funsigned_bitfields)->getAsString(Args);
2946
Daniel Dunbar06205ca2010-10-15 22:30:42 +00002947 // -fsigned-bitfields is default, and clang doesn't support -fno-for-scope.
2948 if (!Args.hasFlag(options::OPT_ffor_scope,
2949 options::OPT_fno_for_scope))
Chris Lattner5f9e2722011-07-23 10:55:15 +00002950 D.Diag(diag::err_drv_clang_unsupported)
Daniel Dunbar06205ca2010-10-15 22:30:42 +00002951 << Args.getLastArg(options::OPT_fno_for_scope)->getAsString(Args);
2952
Jeffrey Yasskin0ea22fd2010-06-08 04:56:20 +00002953 // -fcaret-diagnostics is default.
2954 if (!Args.hasFlag(options::OPT_fcaret_diagnostics,
2955 options::OPT_fno_caret_diagnostics, true))
2956 CmdArgs.push_back("-fno-caret-diagnostics");
2957
Daniel Dunbar49138fc2009-04-19 21:09:34 +00002958 // -fdiagnostics-fixit-info is default, only pass non-default.
Mike Stump1eb44332009-09-09 15:08:12 +00002959 if (!Args.hasFlag(options::OPT_fdiagnostics_fixit_info,
Daniel Dunbar49138fc2009-04-19 21:09:34 +00002960 options::OPT_fno_diagnostics_fixit_info))
2961 CmdArgs.push_back("-fno-diagnostics-fixit-info");
Eric Christopher88b7cf02011-08-19 00:30:14 +00002962
Daniel Dunbar9e820ee2009-04-16 06:32:38 +00002963 // Enable -fdiagnostics-show-option by default.
Mike Stump1eb44332009-09-09 15:08:12 +00002964 if (Args.hasFlag(options::OPT_fdiagnostics_show_option,
Daniel Dunbar9e820ee2009-04-16 06:32:38 +00002965 options::OPT_fno_diagnostics_show_option))
2966 CmdArgs.push_back("-fdiagnostics-show-option");
Daniel Dunbar838be482009-11-04 06:24:57 +00002967
Chris Lattner6fbe8392010-05-04 21:55:25 +00002968 if (const Arg *A =
2969 Args.getLastArg(options::OPT_fdiagnostics_show_category_EQ)) {
2970 CmdArgs.push_back("-fdiagnostics-show-category");
Richard Smith1d489cf2012-11-01 04:30:05 +00002971 CmdArgs.push_back(A->getValue());
Chris Lattner6fbe8392010-05-04 21:55:25 +00002972 }
Daniel Dunbarca0e0542010-08-24 16:47:49 +00002973
Douglas Gregorc9471b02011-05-21 17:07:29 +00002974 if (const Arg *A =
2975 Args.getLastArg(options::OPT_fdiagnostics_format_EQ)) {
2976 CmdArgs.push_back("-fdiagnostics-format");
Richard Smith1d489cf2012-11-01 04:30:05 +00002977 CmdArgs.push_back(A->getValue());
Douglas Gregorc9471b02011-05-21 17:07:29 +00002978 }
2979
Chandler Carruthabaca7a2011-03-27 01:50:55 +00002980 if (Arg *A = Args.getLastArg(
2981 options::OPT_fdiagnostics_show_note_include_stack,
2982 options::OPT_fno_diagnostics_show_note_include_stack)) {
2983 if (A->getOption().matches(
2984 options::OPT_fdiagnostics_show_note_include_stack))
2985 CmdArgs.push_back("-fdiagnostics-show-note-include-stack");
2986 else
2987 CmdArgs.push_back("-fno-diagnostics-show-note-include-stack");
2988 }
2989
Daniel Dunbar838be482009-11-04 06:24:57 +00002990 // Color diagnostics are the default, unless the terminal doesn't support
2991 // them.
2992 if (Args.hasFlag(options::OPT_fcolor_diagnostics,
Argyrios Kyrtzidisf765d762010-09-23 12:56:06 +00002993 options::OPT_fno_color_diagnostics,
2994 llvm::sys::Process::StandardErrHasColors()))
Daniel Dunbar838be482009-11-04 06:24:57 +00002995 CmdArgs.push_back("-fcolor-diagnostics");
2996
Daniel Dunbar75eb1d62009-06-08 21:13:54 +00002997 if (!Args.hasFlag(options::OPT_fshow_source_location,
2998 options::OPT_fno_show_source_location))
2999 CmdArgs.push_back("-fno-show-source-location");
Daniel Dunbar9e820ee2009-04-16 06:32:38 +00003000
Douglas Gregorc9471b02011-05-21 17:07:29 +00003001 if (!Args.hasFlag(options::OPT_fshow_column,
3002 options::OPT_fno_show_column,
3003 true))
3004 CmdArgs.push_back("-fno-show-column");
3005
Douglas Gregora0068fc2010-07-09 17:35:33 +00003006 if (!Args.hasFlag(options::OPT_fspell_checking,
3007 options::OPT_fno_spell_checking))
3008 CmdArgs.push_back("-fno-spell-checking");
Daniel Dunbarca0e0542010-08-24 16:47:49 +00003009
Daniel Dunbar25b26eb2010-10-18 22:49:46 +00003010
Chad Rosier15490fd2012-12-05 21:08:21 +00003011 // -fno-asm-blocks is default.
3012 if (Args.hasFlag(options::OPT_fasm_blocks, options::OPT_fno_asm_blocks,
3013 false))
3014 CmdArgs.push_back("-fasm-blocks");
Daniel Dunbar25b26eb2010-10-18 22:49:46 +00003015
Nadav Rotem0f6ef282012-12-18 23:10:16 +00003016 // -fvectorize is default.
Chad Rosierc04d0932012-12-11 17:12:28 +00003017 if (Args.hasFlag(options::OPT_fvectorize,
Nadav Rotem0f6ef282012-12-18 23:10:16 +00003018 options::OPT_fno_vectorize, true)) {
Chad Rosierc04d0932012-12-11 17:12:28 +00003019 CmdArgs.push_back("-backend-option");
3020 CmdArgs.push_back("-vectorize-loops");
3021 }
3022
Hal Finkel443c9992012-12-11 19:59:32 +00003023 // -fno-slp-vectorize is default.
3024 if (Args.hasFlag(options::OPT_fslp_vectorize,
3025 options::OPT_fno_slp_vectorize, false)) {
3026 CmdArgs.push_back("-backend-option");
3027 CmdArgs.push_back("-vectorize");
3028 }
3029
Jeffrey Yasskin5edbdcc2010-06-11 05:57:47 +00003030 if (Arg *A = Args.getLastArg(options::OPT_fshow_overloads_EQ))
3031 A->render(Args, CmdArgs);
3032
Daniel Dunbar7695fba2009-04-19 21:20:32 +00003033 // -fdollars-in-identifiers default varies depending on platform and
3034 // language; only pass if specified.
Mike Stump1eb44332009-09-09 15:08:12 +00003035 if (Arg *A = Args.getLastArg(options::OPT_fdollars_in_identifiers,
Daniel Dunbar7695fba2009-04-19 21:20:32 +00003036 options::OPT_fno_dollars_in_identifiers)) {
3037 if (A->getOption().matches(options::OPT_fdollars_in_identifiers))
Daniel Dunbar8663b182009-12-16 20:10:18 +00003038 CmdArgs.push_back("-fdollars-in-identifiers");
Daniel Dunbar7695fba2009-04-19 21:20:32 +00003039 else
Daniel Dunbar8663b182009-12-16 20:10:18 +00003040 CmdArgs.push_back("-fno-dollars-in-identifiers");
Daniel Dunbar7695fba2009-04-19 21:20:32 +00003041 }
3042
Daniel Dunbare027a4b2009-05-22 19:02:20 +00003043 // -funit-at-a-time is default, and we don't support -fno-unit-at-a-time for
3044 // practical purposes.
Mike Stump1eb44332009-09-09 15:08:12 +00003045 if (Arg *A = Args.getLastArg(options::OPT_funit_at_a_time,
Daniel Dunbare027a4b2009-05-22 19:02:20 +00003046 options::OPT_fno_unit_at_a_time)) {
3047 if (A->getOption().matches(options::OPT_fno_unit_at_a_time))
Chris Lattner5f9e2722011-07-23 10:55:15 +00003048 D.Diag(diag::warn_drv_clang_unsupported) << A->getAsString(Args);
Daniel Dunbare027a4b2009-05-22 19:02:20 +00003049 }
Eli Friedmanceb5c5b2009-07-14 21:58:17 +00003050
Eli Friedman19bda3a2011-11-02 01:53:16 +00003051 if (Args.hasFlag(options::OPT_fapple_pragma_pack,
3052 options::OPT_fno_apple_pragma_pack, false))
3053 CmdArgs.push_back("-fapple-pragma-pack");
3054
Daniel Dunbar2ba91572009-09-10 03:37:02 +00003055 // Default to -fno-builtin-str{cat,cpy} on Darwin for ARM.
Daniel Dunbarf84a4a42009-09-10 04:57:27 +00003056 //
Daniel Dunbar8ff5b282009-12-11 23:00:49 +00003057 // FIXME: This is disabled until clang -cc1 supports -fno-builtin-foo. PR4941.
Daniel Dunbarf84a4a42009-09-10 04:57:27 +00003058#if 0
Bob Wilson905c45f2011-10-14 05:03:44 +00003059 if (getToolChain().getTriple().isOSDarwin() &&
Daniel Dunbar2ba91572009-09-10 03:37:02 +00003060 (getToolChain().getTriple().getArch() == llvm::Triple::arm ||
3061 getToolChain().getTriple().getArch() == llvm::Triple::thumb)) {
3062 if (!Args.hasArg(options::OPT_fbuiltin_strcat))
3063 CmdArgs.push_back("-fno-builtin-strcat");
3064 if (!Args.hasArg(options::OPT_fbuiltin_strcpy))
3065 CmdArgs.push_back("-fno-builtin-strcpy");
3066 }
Daniel Dunbarf84a4a42009-09-10 04:57:27 +00003067#endif
Daniel Dunbar2ba91572009-09-10 03:37:02 +00003068
Daniel Dunbard98750f2011-03-18 21:23:40 +00003069 // Only allow -traditional or -traditional-cpp outside in preprocessing modes.
Mike Stump1eb44332009-09-09 15:08:12 +00003070 if (Arg *A = Args.getLastArg(options::OPT_traditional,
Daniel Dunbard98750f2011-03-18 21:23:40 +00003071 options::OPT_traditional_cpp)) {
3072 if (isa<PreprocessJobAction>(JA))
3073 CmdArgs.push_back("-traditional-cpp");
Eric Christopher88b7cf02011-08-19 00:30:14 +00003074 else
Chris Lattner5f9e2722011-07-23 10:55:15 +00003075 D.Diag(diag::err_drv_clang_unsupported) << A->getAsString(Args);
Daniel Dunbard98750f2011-03-18 21:23:40 +00003076 }
Eli Friedmanceb5c5b2009-07-14 21:58:17 +00003077
Daniel Dunbar1d460332009-03-18 10:01:51 +00003078 Args.AddLastArg(CmdArgs, options::OPT_dM);
Chris Lattnerd82df3a2009-04-12 01:56:53 +00003079 Args.AddLastArg(CmdArgs, options::OPT_dD);
Ted Kremenek36f6e302011-11-11 00:07:43 +00003080
3081 // Handle serialized diagnostics.
3082 if (Arg *A = Args.getLastArg(options::OPT__serialize_diags)) {
3083 CmdArgs.push_back("-serialize-diagnostic-file");
Richard Smith1d489cf2012-11-01 04:30:05 +00003084 CmdArgs.push_back(Args.MakeArgString(A->getValue()));
Ted Kremenek36f6e302011-11-11 00:07:43 +00003085 }
Daniel Dunbar1d460332009-03-18 10:01:51 +00003086
Ted Kremenek127ff2e2012-09-13 06:41:18 +00003087 if (Args.hasArg(options::OPT_fretain_comments_from_system_headers))
3088 CmdArgs.push_back("-fretain-comments-from-system-headers");
3089
Daniel Dunbar3f87fb02010-04-15 06:09:03 +00003090 // Forward -Xclang arguments to -cc1, and -mllvm arguments to the LLVM option
3091 // parser.
Daniel Dunbar1d460332009-03-18 10:01:51 +00003092 Args.AddAllArgValues(CmdArgs, options::OPT_Xclang);
Daniel Dunbar3f87fb02010-04-15 06:09:03 +00003093 for (arg_iterator it = Args.filtered_begin(options::OPT_mllvm),
3094 ie = Args.filtered_end(); it != ie; ++it) {
Daniel Dunbar7e4953e2010-06-11 22:00:13 +00003095 (*it)->claim();
Daniel Dunbarfb36d212010-04-17 06:10:00 +00003096
Daniel Dunbar3f87fb02010-04-15 06:09:03 +00003097 // We translate this by hand to the -cc1 argument, since nightly test uses
3098 // it and developers have been trained to spell it with -mllvm.
Richard Smith1d489cf2012-11-01 04:30:05 +00003099 if (StringRef((*it)->getValue(0)) == "-disable-llvm-optzns")
Daniel Dunbar3f87fb02010-04-15 06:09:03 +00003100 CmdArgs.push_back("-disable-llvm-optzns");
3101 else
Daniel Dunbar7e4953e2010-06-11 22:00:13 +00003102 (*it)->render(Args, CmdArgs);
Daniel Dunbar3f87fb02010-04-15 06:09:03 +00003103 }
Daniel Dunbar1d460332009-03-18 10:01:51 +00003104
Daniel Dunbarcd8e4c42009-03-30 06:36:42 +00003105 if (Output.getType() == types::TY_Dependencies) {
3106 // Handled with other dependency code.
Daniel Dunbar115a7922009-03-19 07:29:38 +00003107 } else if (Output.isFilename()) {
Daniel Dunbarb488c1d2009-03-18 08:07:30 +00003108 CmdArgs.push_back("-o");
Daniel Dunbar115a7922009-03-19 07:29:38 +00003109 CmdArgs.push_back(Output.getFilename());
3110 } else {
3111 assert(Output.isNothing() && "Invalid output.");
Daniel Dunbarb488c1d2009-03-18 08:07:30 +00003112 }
3113
Daniel Dunbar1d460332009-03-18 10:01:51 +00003114 for (InputInfoList::const_iterator
3115 it = Inputs.begin(), ie = Inputs.end(); it != ie; ++it) {
3116 const InputInfo &II = *it;
3117 CmdArgs.push_back("-x");
Fariborz Jahaniana5ee0892012-09-28 19:05:17 +00003118 if (Args.hasArg(options::OPT_rewrite_objc))
3119 CmdArgs.push_back(types::getTypeName(types::TY_PP_ObjCXX));
3120 else
3121 CmdArgs.push_back(types::getTypeName(II.getType()));
Daniel Dunbar7c1e4652010-08-02 02:38:21 +00003122 if (II.isFilename())
Daniel Dunbar115a7922009-03-19 07:29:38 +00003123 CmdArgs.push_back(II.getFilename());
Daniel Dunbar1d460332009-03-18 10:01:51 +00003124 else
Daniel Dunbar115a7922009-03-19 07:29:38 +00003125 II.getInputArg().renderAsInput(Args, CmdArgs);
Daniel Dunbar1d460332009-03-18 10:01:51 +00003126 }
Daniel Dunbarc21c4852009-04-08 23:54:23 +00003127
Chris Lattnere6113de2009-11-03 19:50:27 +00003128 Args.AddAllArgs(CmdArgs, options::OPT_undef);
3129
Daniel Dunbara001c1c2010-07-18 21:16:15 +00003130 const char *Exec = getToolChain().getDriver().getClangProgramPath();
Daniel Dunbarf2d8b9f2009-12-18 02:43:17 +00003131
3132 // Optionally embed the -cc1 level arguments into the debug info, for build
3133 // analysis.
3134 if (getToolChain().UseDwarfDebugFlags()) {
Daniel Dunbar6e900472010-06-04 18:47:06 +00003135 ArgStringList OriginalArgs;
3136 for (ArgList::const_iterator it = Args.begin(),
3137 ie = Args.end(); it != ie; ++it)
3138 (*it)->render(Args, OriginalArgs);
Daniel Dunbarca0e0542010-08-24 16:47:49 +00003139
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00003140 SmallString<256> Flags;
Daniel Dunbarf2d8b9f2009-12-18 02:43:17 +00003141 Flags += Exec;
Daniel Dunbar6e900472010-06-04 18:47:06 +00003142 for (unsigned i = 0, e = OriginalArgs.size(); i != e; ++i) {
Daniel Dunbarf2d8b9f2009-12-18 02:43:17 +00003143 Flags += " ";
Daniel Dunbar6e900472010-06-04 18:47:06 +00003144 Flags += OriginalArgs[i];
Daniel Dunbarf2d8b9f2009-12-18 02:43:17 +00003145 }
3146 CmdArgs.push_back("-dwarf-debug-flags");
3147 CmdArgs.push_back(Args.MakeArgString(Flags.str()));
3148 }
3149
Daniel Dunbar2fe238e2010-08-02 02:38:28 +00003150 C.addCommand(new Command(JA, *this, Exec, CmdArgs));
Daniel Dunbara880db02009-03-23 19:03:36 +00003151
Roman Divackybe4c8702011-02-10 16:52:03 +00003152 if (Arg *A = Args.getLastArg(options::OPT_pg))
3153 if (Args.hasArg(options::OPT_fomit_frame_pointer))
Chris Lattner5f9e2722011-07-23 10:55:15 +00003154 D.Diag(diag::err_drv_argument_not_allowed_with)
Roman Divackybe4c8702011-02-10 16:52:03 +00003155 << "-fomit-frame-pointer" << A->getAsString(Args);
Michael J. Spencer20249a12010-10-21 03:16:25 +00003156
Daniel Dunbar68fb4692009-04-03 20:51:31 +00003157 // Claim some arguments which clang supports automatically.
3158
Daniel Dunbarf4046862010-04-15 06:18:42 +00003159 // -fpch-preprocess is used with gcc to add a special marker in the output to
3160 // include the PCH file. Clang's PTH solution is completely transparent, so we
3161 // do not need to deal with it at all.
Daniel Dunbar68fb4692009-04-03 20:51:31 +00003162 Args.ClaimAllArgs(options::OPT_fpch_preprocess);
Daniel Dunbarc21c4852009-04-08 23:54:23 +00003163
Daniel Dunbara880db02009-03-23 19:03:36 +00003164 // Claim some arguments which clang doesn't support, but we don't
3165 // care to warn the user about.
Daniel Dunbarcdd96862009-11-25 11:53:23 +00003166 Args.ClaimAllArgs(options::OPT_clang_ignored_f_Group);
3167 Args.ClaimAllArgs(options::OPT_clang_ignored_m_Group);
Rafael Espindola035ff0c2011-02-28 23:29:45 +00003168
Rafael Espindola9c094fb2011-03-01 05:25:27 +00003169 // Disable warnings for clang -E -use-gold-plugin -emit-llvm foo.c
Rafael Espindola035ff0c2011-02-28 23:29:45 +00003170 Args.ClaimAllArgs(options::OPT_use_gold_plugin);
Rafael Espindola9c094fb2011-03-01 05:25:27 +00003171 Args.ClaimAllArgs(options::OPT_emit_llvm);
Daniel Dunbar47ac7d22009-03-18 06:00:36 +00003172}
3173
Jim Grosbachfc308292012-02-10 20:37:10 +00003174void ClangAs::AddARMTargetArgs(const ArgList &Args,
3175 ArgStringList &CmdArgs) const {
3176 const Driver &D = getToolChain().getDriver();
3177 llvm::Triple Triple = getToolChain().getTriple();
3178
3179 // Set the CPU based on -march= and -mcpu=.
3180 CmdArgs.push_back("-target-cpu");
Benjamin Kramer92c4fd52012-06-26 22:20:06 +00003181 CmdArgs.push_back(Args.MakeArgString(getARMTargetCPU(Args, Triple)));
Jim Grosbachfc308292012-02-10 20:37:10 +00003182
3183 // Honor -mfpu=.
Chad Rosier99317272012-04-04 20:51:35 +00003184 if (const Arg *A = Args.getLastArg(options::OPT_mfpu_EQ))
Chad Rosierf80f2a52012-04-04 20:56:36 +00003185 addFPUArgs(D, A, Args, CmdArgs);
Chad Rosier7a938fa2012-04-04 20:39:32 +00003186
3187 // Honor -mfpmath=.
3188 if (const Arg *A = Args.getLastArg(options::OPT_mfpmath_EQ))
Chad Rosier30fe6ba2012-04-04 22:13:40 +00003189 addFPMathArgs(D, A, Args, CmdArgs, getARMTargetCPU(Args, Triple));
Jim Grosbachfc308292012-02-10 20:37:10 +00003190}
3191
John McCall260611a2012-06-20 06:18:46 +00003192/// Add options related to the Objective-C runtime/ABI.
3193///
3194/// Returns true if the runtime is non-fragile.
3195ObjCRuntime Clang::AddObjCRuntimeArgs(const ArgList &args,
3196 ArgStringList &cmdArgs,
3197 RewriteKind rewriteKind) const {
3198 // Look for the controlling runtime option.
3199 Arg *runtimeArg = args.getLastArg(options::OPT_fnext_runtime,
3200 options::OPT_fgnu_runtime,
3201 options::OPT_fobjc_runtime_EQ);
3202
3203 // Just forward -fobjc-runtime= to the frontend. This supercedes
3204 // options about fragility.
3205 if (runtimeArg &&
3206 runtimeArg->getOption().matches(options::OPT_fobjc_runtime_EQ)) {
3207 ObjCRuntime runtime;
Richard Smith1d489cf2012-11-01 04:30:05 +00003208 StringRef value = runtimeArg->getValue();
John McCall260611a2012-06-20 06:18:46 +00003209 if (runtime.tryParse(value)) {
3210 getToolChain().getDriver().Diag(diag::err_drv_unknown_objc_runtime)
3211 << value;
3212 }
3213
3214 runtimeArg->render(args, cmdArgs);
3215 return runtime;
3216 }
3217
3218 // Otherwise, we'll need the ABI "version". Version numbers are
3219 // slightly confusing for historical reasons:
3220 // 1 - Traditional "fragile" ABI
3221 // 2 - Non-fragile ABI, version 1
3222 // 3 - Non-fragile ABI, version 2
3223 unsigned objcABIVersion = 1;
3224 // If -fobjc-abi-version= is present, use that to set the version.
3225 if (Arg *abiArg = args.getLastArg(options::OPT_fobjc_abi_version_EQ)) {
Richard Smith1d489cf2012-11-01 04:30:05 +00003226 StringRef value = abiArg->getValue();
John McCall260611a2012-06-20 06:18:46 +00003227 if (value == "1")
3228 objcABIVersion = 1;
3229 else if (value == "2")
3230 objcABIVersion = 2;
3231 else if (value == "3")
3232 objcABIVersion = 3;
3233 else
3234 getToolChain().getDriver().Diag(diag::err_drv_clang_unsupported)
3235 << value;
3236 } else {
3237 // Otherwise, determine if we are using the non-fragile ABI.
3238 bool nonFragileABIIsDefault =
3239 (rewriteKind == RK_NonFragile ||
3240 (rewriteKind == RK_None &&
3241 getToolChain().IsObjCNonFragileABIDefault()));
3242 if (args.hasFlag(options::OPT_fobjc_nonfragile_abi,
3243 options::OPT_fno_objc_nonfragile_abi,
3244 nonFragileABIIsDefault)) {
3245 // Determine the non-fragile ABI version to use.
3246#ifdef DISABLE_DEFAULT_NONFRAGILEABI_TWO
3247 unsigned nonFragileABIVersion = 1;
3248#else
3249 unsigned nonFragileABIVersion = 2;
3250#endif
3251
3252 if (Arg *abiArg = args.getLastArg(
3253 options::OPT_fobjc_nonfragile_abi_version_EQ)) {
Richard Smith1d489cf2012-11-01 04:30:05 +00003254 StringRef value = abiArg->getValue();
John McCall260611a2012-06-20 06:18:46 +00003255 if (value == "1")
3256 nonFragileABIVersion = 1;
3257 else if (value == "2")
3258 nonFragileABIVersion = 2;
3259 else
3260 getToolChain().getDriver().Diag(diag::err_drv_clang_unsupported)
3261 << value;
3262 }
3263
3264 objcABIVersion = 1 + nonFragileABIVersion;
3265 } else {
3266 objcABIVersion = 1;
3267 }
3268 }
3269
3270 // We don't actually care about the ABI version other than whether
3271 // it's non-fragile.
3272 bool isNonFragile = objcABIVersion != 1;
3273
3274 // If we have no runtime argument, ask the toolchain for its default runtime.
3275 // However, the rewriter only really supports the Mac runtime, so assume that.
3276 ObjCRuntime runtime;
3277 if (!runtimeArg) {
3278 switch (rewriteKind) {
3279 case RK_None:
3280 runtime = getToolChain().getDefaultObjCRuntime(isNonFragile);
3281 break;
3282 case RK_Fragile:
3283 runtime = ObjCRuntime(ObjCRuntime::FragileMacOSX, VersionTuple());
3284 break;
3285 case RK_NonFragile:
3286 runtime = ObjCRuntime(ObjCRuntime::MacOSX, VersionTuple());
3287 break;
3288 }
3289
3290 // -fnext-runtime
3291 } else if (runtimeArg->getOption().matches(options::OPT_fnext_runtime)) {
3292 // On Darwin, make this use the default behavior for the toolchain.
3293 if (getToolChain().getTriple().isOSDarwin()) {
3294 runtime = getToolChain().getDefaultObjCRuntime(isNonFragile);
3295
3296 // Otherwise, build for a generic macosx port.
3297 } else {
3298 runtime = ObjCRuntime(ObjCRuntime::MacOSX, VersionTuple());
3299 }
3300
3301 // -fgnu-runtime
3302 } else {
3303 assert(runtimeArg->getOption().matches(options::OPT_fgnu_runtime));
David Chisnalla422cd02012-07-04 10:37:03 +00003304 // Legacy behaviour is to target the gnustep runtime if we are i
3305 // non-fragile mode or the GCC runtime in fragile mode.
3306 if (isNonFragile)
David Chisnall891dac72012-10-16 15:11:55 +00003307 runtime = ObjCRuntime(ObjCRuntime::GNUstep, VersionTuple(1,6));
David Chisnalla422cd02012-07-04 10:37:03 +00003308 else
3309 runtime = ObjCRuntime(ObjCRuntime::GCC, VersionTuple());
John McCall260611a2012-06-20 06:18:46 +00003310 }
3311
3312 cmdArgs.push_back(args.MakeArgString(
3313 "-fobjc-runtime=" + runtime.getAsString()));
3314 return runtime;
3315}
3316
Daniel Dunbar20a9aa52010-05-20 21:30:13 +00003317void ClangAs::ConstructJob(Compilation &C, const JobAction &JA,
Daniel Dunbar20a9aa52010-05-20 21:30:13 +00003318 const InputInfo &Output,
3319 const InputInfoList &Inputs,
3320 const ArgList &Args,
3321 const char *LinkingOutput) const {
Daniel Dunbar20a9aa52010-05-20 21:30:13 +00003322 ArgStringList CmdArgs;
3323
3324 assert(Inputs.size() == 1 && "Unexpected number of inputs.");
3325 const InputInfo &Input = Inputs[0];
3326
Rafael Espindoladbe80d92010-11-17 22:13:25 +00003327 // Don't warn about "clang -w -c foo.s"
3328 Args.ClaimAllArgs(options::OPT_w);
Rafael Espindola9c094fb2011-03-01 05:25:27 +00003329 // and "clang -emit-llvm -c foo.s"
3330 Args.ClaimAllArgs(options::OPT_emit_llvm);
3331 // and "clang -use-gold-plugin -c foo.s"
3332 Args.ClaimAllArgs(options::OPT_use_gold_plugin);
Rafael Espindoladbe80d92010-11-17 22:13:25 +00003333
Daniel Dunbar20a9aa52010-05-20 21:30:13 +00003334 // Invoke ourselves in -cc1as mode.
3335 //
3336 // FIXME: Implement custom jobs for internal actions.
3337 CmdArgs.push_back("-cc1as");
3338
3339 // Add the "effective" target triple.
3340 CmdArgs.push_back("-triple");
Chad Rosier61ab80a2011-09-20 20:44:06 +00003341 std::string TripleStr =
3342 getToolChain().ComputeEffectiveClangTriple(Args, Input.getType());
Daniel Dunbar20a9aa52010-05-20 21:30:13 +00003343 CmdArgs.push_back(Args.MakeArgString(TripleStr));
3344
3345 // Set the output mode, we currently only expect to be used as a real
3346 // assembler.
3347 CmdArgs.push_back("-filetype");
3348 CmdArgs.push_back("obj");
3349
Eric Christopher27e2b982012-12-18 00:31:10 +00003350 // Set the main file name, so that debug info works even with
3351 // -save-temps or preprocessed assembly.
3352 CmdArgs.push_back("-main-file-name");
3353 CmdArgs.push_back(Clang::getBaseInputName(Args, Inputs));
3354
Joerg Sonnenberger359cf922011-05-06 14:35:16 +00003355 if (UseRelaxAll(C, Args))
Daniel Dunbar469d40e2010-05-28 16:43:21 +00003356 CmdArgs.push_back("-relax-all");
Daniel Dunbar99298002010-05-27 06:18:05 +00003357
Jim Grosbachfc308292012-02-10 20:37:10 +00003358 // Add target specific cpu and features flags.
3359 switch(getToolChain().getTriple().getArch()) {
3360 default:
3361 break;
3362
3363 case llvm::Triple::arm:
3364 case llvm::Triple::thumb:
3365 AddARMTargetArgs(Args, CmdArgs);
3366 break;
3367 }
3368
Daniel Dunbar7f6f8c82011-03-17 17:37:29 +00003369 // Ignore explicit -force_cpusubtype_ALL option.
3370 (void) Args.hasArg(options::OPT_force__cpusubtype__ALL);
Daniel Dunbar20a9aa52010-05-20 21:30:13 +00003371
Eric Christopher8f0a4032012-01-10 00:38:01 +00003372 // Determine the original source input.
3373 const Action *SourceAction = &JA;
3374 while (SourceAction->getKind() != Action::InputClass) {
3375 assert(!SourceAction->getInputs().empty() && "unexpected root action!");
3376 SourceAction = SourceAction->getInputs()[0];
3377 }
3378
Chandler Carruthd566df62012-12-17 21:40:04 +00003379 // Forward -g and handle debug info related flags, assuming we are dealing
3380 // with an actual assembly file.
Eric Christopher8f0a4032012-01-10 00:38:01 +00003381 if (SourceAction->getType() == types::TY_Asm ||
3382 SourceAction->getType() == types::TY_PP_Asm) {
3383 Args.ClaimAllArgs(options::OPT_g_Group);
3384 if (Arg *A = Args.getLastArg(options::OPT_g_Group))
3385 if (!A->getOption().matches(options::OPT_g0))
3386 CmdArgs.push_back("-g");
Chandler Carruthd566df62012-12-17 21:40:04 +00003387
3388 // Add the -fdebug-compilation-dir flag if needed.
3389 addDebugCompDirArg(Args, CmdArgs);
Kevin Enderby02341792013-01-17 21:38:06 +00003390
3391 // Set the AT_producer to the clang version when using the integrated
3392 // assembler on assembly source files.
3393 CmdArgs.push_back("-dwarf-debug-producer");
3394 CmdArgs.push_back(Args.MakeArgString(getClangFullVersion()));
Eric Christopher8f0a4032012-01-10 00:38:01 +00003395 }
Kevin Enderby567003e2011-12-22 19:31:58 +00003396
3397 // Optionally embed the -cc1as level arguments into the debug info, for build
3398 // analysis.
3399 if (getToolChain().UseDwarfDebugFlags()) {
3400 ArgStringList OriginalArgs;
3401 for (ArgList::const_iterator it = Args.begin(),
3402 ie = Args.end(); it != ie; ++it)
3403 (*it)->render(Args, OriginalArgs);
3404
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00003405 SmallString<256> Flags;
Kevin Enderby567003e2011-12-22 19:31:58 +00003406 const char *Exec = getToolChain().getDriver().getClangProgramPath();
3407 Flags += Exec;
3408 for (unsigned i = 0, e = OriginalArgs.size(); i != e; ++i) {
3409 Flags += " ";
3410 Flags += OriginalArgs[i];
3411 }
3412 CmdArgs.push_back("-dwarf-debug-flags");
3413 CmdArgs.push_back(Args.MakeArgString(Flags.str()));
3414 }
Daniel Dunbar20a9aa52010-05-20 21:30:13 +00003415
3416 // FIXME: Add -static support, once we have it.
3417
3418 Args.AddAllArgValues(CmdArgs, options::OPT_Wa_COMMA,
3419 options::OPT_Xassembler);
Daniel Dunbar3df23252011-04-29 17:53:18 +00003420 Args.AddAllArgs(CmdArgs, options::OPT_mllvm);
Daniel Dunbar20a9aa52010-05-20 21:30:13 +00003421
3422 assert(Output.isFilename() && "Unexpected lipo output.");
3423 CmdArgs.push_back("-o");
3424 CmdArgs.push_back(Output.getFilename());
3425
Daniel Dunbar7c1e4652010-08-02 02:38:21 +00003426 assert(Input.isFilename() && "Invalid input.");
3427 CmdArgs.push_back(Input.getFilename());
Daniel Dunbar20a9aa52010-05-20 21:30:13 +00003428
Daniel Dunbara001c1c2010-07-18 21:16:15 +00003429 const char *Exec = getToolChain().getDriver().getClangProgramPath();
Daniel Dunbar2fe238e2010-08-02 02:38:28 +00003430 C.addCommand(new Command(JA, *this, Exec, CmdArgs));
Daniel Dunbar20a9aa52010-05-20 21:30:13 +00003431}
3432
Daniel Dunbarb488c1d2009-03-18 08:07:30 +00003433void gcc::Common::ConstructJob(Compilation &C, const JobAction &JA,
Daniel Dunbarb488c1d2009-03-18 08:07:30 +00003434 const InputInfo &Output,
3435 const InputInfoList &Inputs,
Daniel Dunbar1d460332009-03-18 10:01:51 +00003436 const ArgList &Args,
Daniel Dunbarb488c1d2009-03-18 08:07:30 +00003437 const char *LinkingOutput) const {
Daniel Dunbaree788e72009-12-21 18:54:17 +00003438 const Driver &D = getToolChain().getDriver();
Daniel Dunbarb488c1d2009-03-18 08:07:30 +00003439 ArgStringList CmdArgs;
Daniel Dunbar47ac7d22009-03-18 06:00:36 +00003440
Daniel Dunbarc21c4852009-04-08 23:54:23 +00003441 for (ArgList::const_iterator
Daniel Dunbar1d460332009-03-18 10:01:51 +00003442 it = Args.begin(), ie = Args.end(); it != ie; ++it) {
Daniel Dunbarb488c1d2009-03-18 08:07:30 +00003443 Arg *A = *it;
Michael J. Spencer91e06da2012-10-19 22:37:06 +00003444 if (forwardToGCC(A->getOption())) {
Daniel Dunbar2dffe2d2010-08-03 16:14:14 +00003445 // Don't forward any -g arguments to assembly steps.
3446 if (isa<AssembleJobAction>(JA) &&
3447 A->getOption().matches(options::OPT_g_Group))
3448 continue;
3449
Daniel Dunbar75877192009-03-19 07:55:12 +00003450 // It is unfortunate that we have to claim here, as this means
3451 // we will basically never report anything interesting for
Daniel Dunbar6ecc7a92009-05-02 21:41:52 +00003452 // platforms using a generic gcc, even if we are just using gcc
3453 // to get to the assembler.
Daniel Dunbar75877192009-03-19 07:55:12 +00003454 A->claim();
Daniel Dunbar1d460332009-03-18 10:01:51 +00003455 A->render(Args, CmdArgs);
Daniel Dunbar75877192009-03-19 07:55:12 +00003456 }
Daniel Dunbarb488c1d2009-03-18 08:07:30 +00003457 }
Daniel Dunbarc21c4852009-04-08 23:54:23 +00003458
Daniel Dunbar82b51cc2010-01-25 22:35:08 +00003459 RenderExtraToolArgs(JA, CmdArgs);
Daniel Dunbarb488c1d2009-03-18 08:07:30 +00003460
3461 // If using a driver driver, force the arch.
Rafael Espindola64f7ad92012-10-07 04:44:33 +00003462 llvm::Triple::ArchType Arch = getToolChain().getArch();
Bob Wilson905c45f2011-10-14 05:03:44 +00003463 if (getToolChain().getTriple().isOSDarwin()) {
Daniel Dunbarb488c1d2009-03-18 08:07:30 +00003464 CmdArgs.push_back("-arch");
Daniel Dunbarbf54a062009-04-01 20:33:11 +00003465
3466 // FIXME: Remove these special cases.
Rafael Espindola64f7ad92012-10-07 04:44:33 +00003467 if (Arch == llvm::Triple::ppc)
Daniel Dunbar7cfe31a2009-05-22 02:21:04 +00003468 CmdArgs.push_back("ppc");
Rafael Espindola64f7ad92012-10-07 04:44:33 +00003469 else if (Arch == llvm::Triple::ppc64)
Daniel Dunbar7cfe31a2009-05-22 02:21:04 +00003470 CmdArgs.push_back("ppc64");
3471 else
Rafael Espindola64f7ad92012-10-07 04:44:33 +00003472 CmdArgs.push_back(Args.MakeArgString(getToolChain().getArchName()));
Daniel Dunbarb488c1d2009-03-18 08:07:30 +00003473 }
3474
Daniel Dunbar6ecc7a92009-05-02 21:41:52 +00003475 // Try to force gcc to match the tool chain we want, if we recognize
3476 // the arch.
Daniel Dunbar7cfe31a2009-05-22 02:21:04 +00003477 //
3478 // FIXME: The triple class should directly provide the information we want
3479 // here.
Rafael Espindola64f7ad92012-10-07 04:44:33 +00003480 if (Arch == llvm::Triple::x86 || Arch == llvm::Triple::ppc)
Daniel Dunbar6ecc7a92009-05-02 21:41:52 +00003481 CmdArgs.push_back("-m32");
Rafael Espindola64f7ad92012-10-07 04:44:33 +00003482 else if (Arch == llvm::Triple::x86_64 || Arch == llvm::Triple::x86_64)
Daniel Dunbar6ecc7a92009-05-02 21:41:52 +00003483 CmdArgs.push_back("-m64");
3484
Daniel Dunbar7c1e4652010-08-02 02:38:21 +00003485 if (Output.isFilename()) {
Daniel Dunbarb488c1d2009-03-18 08:07:30 +00003486 CmdArgs.push_back("-o");
Daniel Dunbar115a7922009-03-19 07:29:38 +00003487 CmdArgs.push_back(Output.getFilename());
3488 } else {
3489 assert(Output.isNothing() && "Unexpected output");
Daniel Dunbarb488c1d2009-03-18 08:07:30 +00003490 CmdArgs.push_back("-fsyntax-only");
Daniel Dunbar115a7922009-03-19 07:29:38 +00003491 }
Daniel Dunbarb488c1d2009-03-18 08:07:30 +00003492
Tony Linthicum96319392011-12-12 21:14:55 +00003493 Args.AddAllArgValues(CmdArgs, options::OPT_Wa_COMMA,
3494 options::OPT_Xassembler);
Daniel Dunbarb488c1d2009-03-18 08:07:30 +00003495
3496 // Only pass -x if gcc will understand it; otherwise hope gcc
3497 // understands the suffix correctly. The main use case this would go
3498 // wrong in is for linker inputs if they happened to have an odd
3499 // suffix; really the only way to get this to happen is a command
3500 // like '-x foobar a.c' which will treat a.c like a linker input.
3501 //
3502 // FIXME: For the linker case specifically, can we safely convert
3503 // inputs into '-Wl,' options?
3504 for (InputInfoList::const_iterator
3505 it = Inputs.begin(), ie = Inputs.end(); it != ie; ++it) {
3506 const InputInfo &II = *it;
Daniel Dunbara8304f62009-05-02 20:14:53 +00003507
Daniel Dunbar5915fbf2009-09-01 16:57:46 +00003508 // Don't try to pass LLVM or AST inputs to a generic gcc.
Daniel Dunbar6c6424b2010-06-07 23:28:45 +00003509 if (II.getType() == types::TY_LLVM_IR || II.getType() == types::TY_LTO_IR ||
3510 II.getType() == types::TY_LLVM_BC || II.getType() == types::TY_LTO_BC)
Chris Lattner5f9e2722011-07-23 10:55:15 +00003511 D.Diag(diag::err_drv_no_linker_llvm_support)
Daniel Dunbar88137642009-09-09 22:32:48 +00003512 << getToolChain().getTripleString();
Daniel Dunbar5915fbf2009-09-01 16:57:46 +00003513 else if (II.getType() == types::TY_AST)
Chris Lattner5f9e2722011-07-23 10:55:15 +00003514 D.Diag(diag::err_drv_no_ast_support)
Daniel Dunbar88137642009-09-09 22:32:48 +00003515 << getToolChain().getTripleString();
Daniel Dunbara8304f62009-05-02 20:14:53 +00003516
Daniel Dunbarb488c1d2009-03-18 08:07:30 +00003517 if (types::canTypeBeUserSpecified(II.getType())) {
3518 CmdArgs.push_back("-x");
3519 CmdArgs.push_back(types::getTypeName(II.getType()));
3520 }
3521
Daniel Dunbar7c1e4652010-08-02 02:38:21 +00003522 if (II.isFilename())
Daniel Dunbar115a7922009-03-19 07:29:38 +00003523 CmdArgs.push_back(II.getFilename());
Daniel Dunbar48f99942010-09-25 18:10:05 +00003524 else {
3525 const Arg &A = II.getInputArg();
3526
3527 // Reverse translate some rewritten options.
3528 if (A.getOption().matches(options::OPT_Z_reserved_lib_stdcxx)) {
3529 CmdArgs.push_back("-lstdc++");
3530 continue;
3531 }
3532
Daniel Dunbar115a7922009-03-19 07:29:38 +00003533 // Don't render as input, we need gcc to do the translations.
Daniel Dunbar48f99942010-09-25 18:10:05 +00003534 A.render(Args, CmdArgs);
3535 }
Daniel Dunbarb488c1d2009-03-18 08:07:30 +00003536 }
3537
Dylan Noblesmithb8a3e812011-04-09 13:31:59 +00003538 const std::string customGCCName = D.getCCCGenericGCCName();
3539 const char *GCCName;
3540 if (!customGCCName.empty())
3541 GCCName = customGCCName.c_str();
3542 else if (D.CCCIsCXX) {
Dylan Noblesmithb8a3e812011-04-09 13:31:59 +00003543 GCCName = "g++";
Dylan Noblesmithb8a3e812011-04-09 13:31:59 +00003544 } else
3545 GCCName = "gcc";
3546
Daniel Dunbarc21c4852009-04-08 23:54:23 +00003547 const char *Exec =
Daniel Dunbar4a7e8892010-07-14 18:46:23 +00003548 Args.MakeArgString(getToolChain().GetProgramPath(GCCName));
Daniel Dunbar2fe238e2010-08-02 02:38:28 +00003549 C.addCommand(new Command(JA, *this, Exec, CmdArgs));
Daniel Dunbar47ac7d22009-03-18 06:00:36 +00003550}
3551
Daniel Dunbar82b51cc2010-01-25 22:35:08 +00003552void gcc::Preprocess::RenderExtraToolArgs(const JobAction &JA,
3553 ArgStringList &CmdArgs) const {
Daniel Dunbarb488c1d2009-03-18 08:07:30 +00003554 CmdArgs.push_back("-E");
Daniel Dunbar47ac7d22009-03-18 06:00:36 +00003555}
3556
Daniel Dunbar82b51cc2010-01-25 22:35:08 +00003557void gcc::Precompile::RenderExtraToolArgs(const JobAction &JA,
3558 ArgStringList &CmdArgs) const {
Daniel Dunbarb488c1d2009-03-18 08:07:30 +00003559 // The type is good enough.
Daniel Dunbar47ac7d22009-03-18 06:00:36 +00003560}
3561
Daniel Dunbar82b51cc2010-01-25 22:35:08 +00003562void gcc::Compile::RenderExtraToolArgs(const JobAction &JA,
3563 ArgStringList &CmdArgs) const {
Daniel Dunbar64952502010-02-11 03:16:21 +00003564 const Driver &D = getToolChain().getDriver();
3565
Daniel Dunbar82b51cc2010-01-25 22:35:08 +00003566 // If -flto, etc. are present then make sure not to force assembly output.
Daniel Dunbar6c6424b2010-06-07 23:28:45 +00003567 if (JA.getType() == types::TY_LLVM_IR || JA.getType() == types::TY_LTO_IR ||
3568 JA.getType() == types::TY_LLVM_BC || JA.getType() == types::TY_LTO_BC)
Daniel Dunbar82b51cc2010-01-25 22:35:08 +00003569 CmdArgs.push_back("-c");
Daniel Dunbar64952502010-02-11 03:16:21 +00003570 else {
3571 if (JA.getType() != types::TY_PP_Asm)
Chris Lattner5f9e2722011-07-23 10:55:15 +00003572 D.Diag(diag::err_drv_invalid_gcc_output_type)
Daniel Dunbar64952502010-02-11 03:16:21 +00003573 << getTypeName(JA.getType());
Michael J. Spencer20249a12010-10-21 03:16:25 +00003574
Daniel Dunbar82b51cc2010-01-25 22:35:08 +00003575 CmdArgs.push_back("-S");
Daniel Dunbar64952502010-02-11 03:16:21 +00003576 }
Daniel Dunbar47ac7d22009-03-18 06:00:36 +00003577}
3578
Daniel Dunbar82b51cc2010-01-25 22:35:08 +00003579void gcc::Assemble::RenderExtraToolArgs(const JobAction &JA,
3580 ArgStringList &CmdArgs) const {
Daniel Dunbarb488c1d2009-03-18 08:07:30 +00003581 CmdArgs.push_back("-c");
Daniel Dunbar47ac7d22009-03-18 06:00:36 +00003582}
Daniel Dunbarb488c1d2009-03-18 08:07:30 +00003583
Daniel Dunbar82b51cc2010-01-25 22:35:08 +00003584void gcc::Link::RenderExtraToolArgs(const JobAction &JA,
3585 ArgStringList &CmdArgs) const {
Daniel Dunbarb488c1d2009-03-18 08:07:30 +00003586 // The types are (hopefully) good enough.
3587}
3588
Tony Linthicum96319392011-12-12 21:14:55 +00003589// Hexagon tools start.
3590void hexagon::Assemble::RenderExtraToolArgs(const JobAction &JA,
3591 ArgStringList &CmdArgs) const {
3592
3593}
3594void hexagon::Assemble::ConstructJob(Compilation &C, const JobAction &JA,
3595 const InputInfo &Output,
3596 const InputInfoList &Inputs,
3597 const ArgList &Args,
3598 const char *LinkingOutput) const {
3599
3600 const Driver &D = getToolChain().getDriver();
3601 ArgStringList CmdArgs;
3602
3603 std::string MarchString = "-march=";
Matthew Curtis67814152012-12-06 14:16:43 +00003604 MarchString += toolchains::Hexagon_TC::GetTargetCPU(Args);
Tony Linthicum96319392011-12-12 21:14:55 +00003605 CmdArgs.push_back(Args.MakeArgString(MarchString));
3606
3607 RenderExtraToolArgs(JA, CmdArgs);
3608
3609 if (Output.isFilename()) {
3610 CmdArgs.push_back("-o");
3611 CmdArgs.push_back(Output.getFilename());
3612 } else {
3613 assert(Output.isNothing() && "Unexpected output");
3614 CmdArgs.push_back("-fsyntax-only");
3615 }
3616
Matthew Curtis33c95f12012-12-06 17:49:03 +00003617 std::string SmallDataThreshold = GetHexagonSmallDataThresholdValue(Args);
3618 if (!SmallDataThreshold.empty())
3619 CmdArgs.push_back(
3620 Args.MakeArgString(std::string("-G") + SmallDataThreshold));
Tony Linthicum96319392011-12-12 21:14:55 +00003621
Matthew Curtis3d8d4222012-12-07 17:23:04 +00003622 Args.AddAllArgs(CmdArgs, options::OPT_g_Group);
3623 Args.AddAllArgValues(CmdArgs, options::OPT_Wa_COMMA,
3624 options::OPT_Xassembler);
3625
Tony Linthicum96319392011-12-12 21:14:55 +00003626 // Only pass -x if gcc will understand it; otherwise hope gcc
3627 // understands the suffix correctly. The main use case this would go
3628 // wrong in is for linker inputs if they happened to have an odd
3629 // suffix; really the only way to get this to happen is a command
3630 // like '-x foobar a.c' which will treat a.c like a linker input.
3631 //
3632 // FIXME: For the linker case specifically, can we safely convert
3633 // inputs into '-Wl,' options?
3634 for (InputInfoList::const_iterator
3635 it = Inputs.begin(), ie = Inputs.end(); it != ie; ++it) {
3636 const InputInfo &II = *it;
3637
3638 // Don't try to pass LLVM or AST inputs to a generic gcc.
3639 if (II.getType() == types::TY_LLVM_IR || II.getType() == types::TY_LTO_IR ||
3640 II.getType() == types::TY_LLVM_BC || II.getType() == types::TY_LTO_BC)
3641 D.Diag(clang::diag::err_drv_no_linker_llvm_support)
3642 << getToolChain().getTripleString();
3643 else if (II.getType() == types::TY_AST)
3644 D.Diag(clang::diag::err_drv_no_ast_support)
3645 << getToolChain().getTripleString();
3646
3647 if (II.isFilename())
3648 CmdArgs.push_back(II.getFilename());
3649 else
3650 // Don't render as input, we need gcc to do the translations. FIXME: Pranav: What is this ?
3651 II.getInputArg().render(Args, CmdArgs);
3652 }
3653
3654 const char *GCCName = "hexagon-as";
3655 const char *Exec =
3656 Args.MakeArgString(getToolChain().GetProgramPath(GCCName));
3657 C.addCommand(new Command(JA, *this, Exec, CmdArgs));
3658
3659}
3660void hexagon::Link::RenderExtraToolArgs(const JobAction &JA,
3661 ArgStringList &CmdArgs) const {
3662 // The types are (hopefully) good enough.
3663}
3664
3665void hexagon::Link::ConstructJob(Compilation &C, const JobAction &JA,
3666 const InputInfo &Output,
3667 const InputInfoList &Inputs,
3668 const ArgList &Args,
3669 const char *LinkingOutput) const {
3670
Matthew Curtis5fdf3502012-12-06 15:46:07 +00003671 const toolchains::Hexagon_TC& ToolChain =
3672 static_cast<const toolchains::Hexagon_TC&>(getToolChain());
3673 const Driver &D = ToolChain.getDriver();
3674
Tony Linthicum96319392011-12-12 21:14:55 +00003675 ArgStringList CmdArgs;
3676
Matthew Curtis5fdf3502012-12-06 15:46:07 +00003677 //----------------------------------------------------------------------------
3678 //
3679 //----------------------------------------------------------------------------
3680 bool hasStaticArg = Args.hasArg(options::OPT_static);
3681 bool buildingLib = Args.hasArg(options::OPT_shared);
Matthew Curtis33c95f12012-12-06 17:49:03 +00003682 bool buildPIE = Args.hasArg(options::OPT_pie);
Matthew Curtis5fdf3502012-12-06 15:46:07 +00003683 bool incStdLib = !Args.hasArg(options::OPT_nostdlib);
3684 bool incStartFiles = !Args.hasArg(options::OPT_nostartfiles);
3685 bool incDefLibs = !Args.hasArg(options::OPT_nodefaultlibs);
3686 bool useShared = buildingLib && !hasStaticArg;
Tony Linthicum96319392011-12-12 21:14:55 +00003687
Matthew Curtis5fdf3502012-12-06 15:46:07 +00003688 //----------------------------------------------------------------------------
3689 // Silence warnings for various options
3690 //----------------------------------------------------------------------------
Tony Linthicum96319392011-12-12 21:14:55 +00003691
Matthew Curtis5fdf3502012-12-06 15:46:07 +00003692 Args.ClaimAllArgs(options::OPT_g_Group);
3693 Args.ClaimAllArgs(options::OPT_emit_llvm);
3694 Args.ClaimAllArgs(options::OPT_w); // Other warning options are already
3695 // handled somewhere else.
3696 Args.ClaimAllArgs(options::OPT_static_libgcc);
3697
3698 //----------------------------------------------------------------------------
3699 //
3700 //----------------------------------------------------------------------------
3701 for (std::vector<std::string>::const_iterator i = ToolChain.ExtraOpts.begin(),
3702 e = ToolChain.ExtraOpts.end();
3703 i != e; ++i)
3704 CmdArgs.push_back(i->c_str());
Tony Linthicum96319392011-12-12 21:14:55 +00003705
Matthew Curtis67814152012-12-06 14:16:43 +00003706 std::string MarchString = toolchains::Hexagon_TC::GetTargetCPU(Args);
3707 CmdArgs.push_back(Args.MakeArgString("-m" + MarchString));
Sebastian Pop43115d42012-01-13 20:37:10 +00003708
Matthew Curtis5fdf3502012-12-06 15:46:07 +00003709 if (buildingLib) {
3710 CmdArgs.push_back("-shared");
3711 CmdArgs.push_back("-call_shared"); // should be the default, but doing as
3712 // hexagon-gcc does
Tony Linthicum96319392011-12-12 21:14:55 +00003713 }
3714
Matthew Curtis5fdf3502012-12-06 15:46:07 +00003715 if (hasStaticArg)
3716 CmdArgs.push_back("-static");
Tony Linthicum96319392011-12-12 21:14:55 +00003717
Matthew Curtis33c95f12012-12-06 17:49:03 +00003718 if (buildPIE && !buildingLib)
3719 CmdArgs.push_back("-pie");
3720
3721 std::string SmallDataThreshold = GetHexagonSmallDataThresholdValue(Args);
3722 if (!SmallDataThreshold.empty()) {
3723 CmdArgs.push_back(
3724 Args.MakeArgString(std::string("-G") + SmallDataThreshold));
3725 }
3726
Matthew Curtis5fdf3502012-12-06 15:46:07 +00003727 //----------------------------------------------------------------------------
3728 //
3729 //----------------------------------------------------------------------------
3730 CmdArgs.push_back("-o");
3731 CmdArgs.push_back(Output.getFilename());
Tony Linthicum96319392011-12-12 21:14:55 +00003732
Matthew Curtis5fdf3502012-12-06 15:46:07 +00003733 const std::string MarchSuffix = "/" + MarchString;
3734 const std::string G0Suffix = "/G0";
3735 const std::string MarchG0Suffix = MarchSuffix + G0Suffix;
3736 const std::string RootDir = toolchains::Hexagon_TC::GetGnuDir(D.InstalledDir)
3737 + "/";
3738 const std::string StartFilesDir = RootDir
3739 + "hexagon/lib"
3740 + (buildingLib
3741 ? MarchG0Suffix : MarchSuffix);
3742
3743 //----------------------------------------------------------------------------
3744 // moslib
3745 //----------------------------------------------------------------------------
3746 std::vector<std::string> oslibs;
3747 bool hasStandalone= false;
3748
3749 for (arg_iterator it = Args.filtered_begin(options::OPT_moslib_EQ),
3750 ie = Args.filtered_end(); it != ie; ++it) {
3751 (*it)->claim();
3752 oslibs.push_back((*it)->getValue());
3753 hasStandalone = hasStandalone || (oslibs.back() == "standalone");
Tony Linthicum96319392011-12-12 21:14:55 +00003754 }
Matthew Curtis5fdf3502012-12-06 15:46:07 +00003755 if (oslibs.empty()) {
3756 oslibs.push_back("standalone");
3757 hasStandalone = true;
3758 }
Tony Linthicum96319392011-12-12 21:14:55 +00003759
Matthew Curtis5fdf3502012-12-06 15:46:07 +00003760 //----------------------------------------------------------------------------
3761 // Start Files
3762 //----------------------------------------------------------------------------
3763 if (incStdLib && incStartFiles) {
3764
3765 if (!buildingLib) {
3766 if (hasStandalone) {
3767 CmdArgs.push_back(
3768 Args.MakeArgString(StartFilesDir + "/crt0_standalone.o"));
3769 }
3770 CmdArgs.push_back(Args.MakeArgString(StartFilesDir + "/crt0.o"));
3771 }
3772 std::string initObj = useShared ? "/initS.o" : "/init.o";
3773 CmdArgs.push_back(Args.MakeArgString(StartFilesDir + initObj));
3774 }
3775
3776 //----------------------------------------------------------------------------
3777 // Library Search Paths
3778 //----------------------------------------------------------------------------
3779 const ToolChain::path_list &LibPaths = ToolChain.getFilePaths();
3780 for (ToolChain::path_list::const_iterator
3781 i = LibPaths.begin(),
3782 e = LibPaths.end();
3783 i != e;
3784 ++i)
3785 CmdArgs.push_back(Args.MakeArgString(StringRef("-L") + *i));
3786
3787 //----------------------------------------------------------------------------
3788 //
3789 //----------------------------------------------------------------------------
3790 Args.AddAllArgs(CmdArgs, options::OPT_T_Group);
3791 Args.AddAllArgs(CmdArgs, options::OPT_e);
3792 Args.AddAllArgs(CmdArgs, options::OPT_s);
3793 Args.AddAllArgs(CmdArgs, options::OPT_t);
3794 Args.AddAllArgs(CmdArgs, options::OPT_u_Group);
3795
3796 AddLinkerInputs(ToolChain, Inputs, Args, CmdArgs);
3797
3798 //----------------------------------------------------------------------------
3799 // Libraries
3800 //----------------------------------------------------------------------------
3801 if (incStdLib && incDefLibs) {
3802 if (D.CCCIsCXX) {
3803 ToolChain.AddCXXStdlibLibArgs(Args, CmdArgs);
3804 CmdArgs.push_back("-lm");
3805 }
3806
3807 CmdArgs.push_back("--start-group");
3808
3809 if (!buildingLib) {
3810 for(std::vector<std::string>::iterator i = oslibs.begin(),
3811 e = oslibs.end(); i != e; ++i)
3812 CmdArgs.push_back(Args.MakeArgString("-l" + *i));
3813 CmdArgs.push_back("-lc");
3814 }
3815 CmdArgs.push_back("-lgcc");
3816
3817 CmdArgs.push_back("--end-group");
3818 }
3819
3820 //----------------------------------------------------------------------------
3821 // End files
3822 //----------------------------------------------------------------------------
3823 if (incStdLib && incStartFiles) {
3824 std::string finiObj = useShared ? "/finiS.o" : "/fini.o";
3825 CmdArgs.push_back(Args.MakeArgString(StartFilesDir + finiObj));
3826 }
3827
3828 std::string Linker = ToolChain.GetProgramPath("hexagon-ld");
3829 C.addCommand(
3830 new Command(
3831 JA, *this,
3832 Args.MakeArgString(Linker), CmdArgs));
Tony Linthicum96319392011-12-12 21:14:55 +00003833}
3834// Hexagon tools end.
3835
Rafael Espindolacfed8282012-10-31 18:51:07 +00003836llvm::Triple::ArchType darwin::getArchTypeForDarwinArchName(StringRef Str) {
3837 // See arch(3) and llvm-gcc's driver-driver.c. We don't implement support for
3838 // archs which Darwin doesn't use.
3839
3840 // The matching this routine does is fairly pointless, since it is neither the
3841 // complete architecture list, nor a reasonable subset. The problem is that
3842 // historically the driver driver accepts this and also ties its -march=
3843 // handling to the architecture name, so we need to be careful before removing
3844 // support for it.
3845
3846 // This code must be kept in sync with Clang's Darwin specific argument
3847 // translation.
3848
3849 return llvm::StringSwitch<llvm::Triple::ArchType>(Str)
3850 .Cases("ppc", "ppc601", "ppc603", "ppc604", "ppc604e", llvm::Triple::ppc)
3851 .Cases("ppc750", "ppc7400", "ppc7450", "ppc970", llvm::Triple::ppc)
3852 .Case("ppc64", llvm::Triple::ppc64)
3853 .Cases("i386", "i486", "i486SX", "i586", "i686", llvm::Triple::x86)
3854 .Cases("pentium", "pentpro", "pentIIm3", "pentIIm5", "pentium4",
3855 llvm::Triple::x86)
3856 .Case("x86_64", llvm::Triple::x86_64)
3857 // This is derived from the driver driver.
3858 .Cases("arm", "armv4t", "armv5", "armv6", llvm::Triple::arm)
3859 .Cases("armv7", "armv7f", "armv7k", "armv7s", "xscale", llvm::Triple::arm)
3860 .Case("r600", llvm::Triple::r600)
3861 .Case("nvptx", llvm::Triple::nvptx)
3862 .Case("nvptx64", llvm::Triple::nvptx64)
3863 .Case("amdil", llvm::Triple::amdil)
3864 .Case("spir", llvm::Triple::spir)
3865 .Default(llvm::Triple::UnknownArch);
3866}
Tony Linthicum96319392011-12-12 21:14:55 +00003867
Bob Wilson66b8a662012-11-23 06:14:39 +00003868const char *Clang::getBaseInputName(const ArgList &Args,
3869 const InputInfoList &Inputs) {
Michael J. Spencer472ccff2010-12-18 00:19:12 +00003870 return Args.MakeArgString(
3871 llvm::sys::path::filename(Inputs[0].getBaseInput()));
Daniel Dunbara3ec60e2009-03-29 18:40:18 +00003872}
3873
Bob Wilson66b8a662012-11-23 06:14:39 +00003874const char *Clang::getBaseInputStem(const ArgList &Args,
3875 const InputInfoList &Inputs) {
Daniel Dunbara3ec60e2009-03-29 18:40:18 +00003876 const char *Str = getBaseInputName(Args, Inputs);
3877
Chris Lattner657ca662011-01-16 08:14:11 +00003878 if (const char *End = strrchr(Str, '.'))
Daniel Dunbar88137642009-09-09 22:32:48 +00003879 return Args.MakeArgString(std::string(Str, End));
Daniel Dunbara3ec60e2009-03-29 18:40:18 +00003880
3881 return Str;
3882}
3883
Bob Wilson66b8a662012-11-23 06:14:39 +00003884const char *Clang::getDependencyFileName(const ArgList &Args,
3885 const InputInfoList &Inputs) {
Daniel Dunbara3ec60e2009-03-29 18:40:18 +00003886 // FIXME: Think about this more.
3887 std::string Res;
3888
3889 if (Arg *OutputOpt = Args.getLastArg(options::OPT_o)) {
Richard Smith1d489cf2012-11-01 04:30:05 +00003890 std::string Str(OutputOpt->getValue());
Daniel Dunbara3ec60e2009-03-29 18:40:18 +00003891 Res = Str.substr(0, Str.rfind('.'));
Chad Rosier30601782011-08-17 23:08:45 +00003892 } else {
Bob Wilson66b8a662012-11-23 06:14:39 +00003893 Res = getBaseInputStem(Args, Inputs);
Chad Rosier30601782011-08-17 23:08:45 +00003894 }
Daniel Dunbar88137642009-09-09 22:32:48 +00003895 return Args.MakeArgString(Res + ".d");
Daniel Dunbara3ec60e2009-03-29 18:40:18 +00003896}
3897
Daniel Dunbar8cac5f72009-03-20 16:06:39 +00003898void darwin::Assemble::ConstructJob(Compilation &C, const JobAction &JA,
Daniel Dunbar2fe238e2010-08-02 02:38:28 +00003899 const InputInfo &Output,
Daniel Dunbarc21c4852009-04-08 23:54:23 +00003900 const InputInfoList &Inputs,
3901 const ArgList &Args,
Daniel Dunbar8cac5f72009-03-20 16:06:39 +00003902 const char *LinkingOutput) const {
3903 ArgStringList CmdArgs;
3904
3905 assert(Inputs.size() == 1 && "Unexpected number of inputs.");
3906 const InputInfo &Input = Inputs[0];
3907
Daniel Dunbar34bac1f2011-04-12 23:59:20 +00003908 // Determine the original source input.
3909 const Action *SourceAction = &JA;
3910 while (SourceAction->getKind() != Action::InputClass) {
3911 assert(!SourceAction->getInputs().empty() && "unexpected root action!");
3912 SourceAction = SourceAction->getInputs()[0];
3913 }
3914
3915 // Forward -g, assuming we are dealing with an actual assembly file.
Eric Christopher88b7cf02011-08-19 00:30:14 +00003916 if (SourceAction->getType() == types::TY_Asm ||
Daniel Dunbar34bac1f2011-04-12 23:59:20 +00003917 SourceAction->getType() == types::TY_PP_Asm) {
Daniel Dunbar8e4fea62009-04-01 00:27:44 +00003918 if (Args.hasArg(options::OPT_gstabs))
3919 CmdArgs.push_back("--gstabs");
3920 else if (Args.hasArg(options::OPT_g_Group))
Bob Wilson591ff152011-11-02 05:10:45 +00003921 CmdArgs.push_back("-g");
Daniel Dunbar8e4fea62009-04-01 00:27:44 +00003922 }
Daniel Dunbarc21c4852009-04-08 23:54:23 +00003923
Daniel Dunbar8cac5f72009-03-20 16:06:39 +00003924 // Derived from asm spec.
Daniel Dunbarcc6f8032009-09-09 18:36:27 +00003925 AddDarwinArch(Args, CmdArgs);
Daniel Dunbar8cac5f72009-03-20 16:06:39 +00003926
Daniel Dunbarf5438e32010-07-22 01:47:22 +00003927 // Use -force_cpusubtype_ALL on x86 by default.
3928 if (getToolChain().getTriple().getArch() == llvm::Triple::x86 ||
3929 getToolChain().getTriple().getArch() == llvm::Triple::x86_64 ||
Daniel Dunbarcc6f8032009-09-09 18:36:27 +00003930 Args.hasArg(options::OPT_force__cpusubtype__ALL))
3931 CmdArgs.push_back("-force_cpusubtype_ALL");
3932
Daniel Dunbar0e2679d2009-08-24 22:26:16 +00003933 if (getToolChain().getTriple().getArch() != llvm::Triple::x86_64 &&
Daniel Dunbar7a0c0642012-10-15 22:23:53 +00003934 (((Args.hasArg(options::OPT_mkernel) ||
3935 Args.hasArg(options::OPT_fapple_kext)) &&
3936 (!getDarwinToolChain().isTargetIPhoneOS() ||
3937 getDarwinToolChain().isIPhoneOSVersionLT(6, 0))) ||
3938 Args.hasArg(options::OPT_static)))
Daniel Dunbarc21c4852009-04-08 23:54:23 +00003939 CmdArgs.push_back("-static");
3940
Daniel Dunbar8cac5f72009-03-20 16:06:39 +00003941 Args.AddAllArgValues(CmdArgs, options::OPT_Wa_COMMA,
3942 options::OPT_Xassembler);
3943
3944 assert(Output.isFilename() && "Unexpected lipo output.");
3945 CmdArgs.push_back("-o");
3946 CmdArgs.push_back(Output.getFilename());
3947
Daniel Dunbar7c1e4652010-08-02 02:38:21 +00003948 assert(Input.isFilename() && "Invalid input.");
3949 CmdArgs.push_back(Input.getFilename());
Daniel Dunbar8cac5f72009-03-20 16:06:39 +00003950
3951 // asm_final spec is empty.
3952
Daniel Dunbarc21c4852009-04-08 23:54:23 +00003953 const char *Exec =
Daniel Dunbar4a7e8892010-07-14 18:46:23 +00003954 Args.MakeArgString(getToolChain().GetProgramPath("as"));
Daniel Dunbar2fe238e2010-08-02 02:38:28 +00003955 C.addCommand(new Command(JA, *this, Exec, CmdArgs));
Daniel Dunbar8cac5f72009-03-20 16:06:39 +00003956}
Daniel Dunbarff7488d2009-03-20 00:52:38 +00003957
David Blaikie99ba9e32011-12-20 02:48:34 +00003958void darwin::DarwinTool::anchor() {}
3959
Daniel Dunbarfbefe6b2009-09-09 18:36:20 +00003960void darwin::DarwinTool::AddDarwinArch(const ArgList &Args,
3961 ArgStringList &CmdArgs) const {
Chris Lattner5f9e2722011-07-23 10:55:15 +00003962 StringRef ArchName = getDarwinToolChain().getDarwinArchName(Args);
Daniel Dunbareeff4062010-01-22 02:04:58 +00003963
Daniel Dunbar02633b52009-03-26 16:23:12 +00003964 // Derived from darwin_arch spec.
3965 CmdArgs.push_back("-arch");
Daniel Dunbareeff4062010-01-22 02:04:58 +00003966 CmdArgs.push_back(Args.MakeArgString(ArchName));
Daniel Dunbar78dbd582009-09-04 18:35:31 +00003967
Daniel Dunbareeff4062010-01-22 02:04:58 +00003968 // FIXME: Is this needed anymore?
3969 if (ArchName == "arm")
Daniel Dunbar78dbd582009-09-04 18:35:31 +00003970 CmdArgs.push_back("-force_cpusubtype_ALL");
Daniel Dunbar02633b52009-03-26 16:23:12 +00003971}
3972
Bill Wendling6acf8b42012-10-02 18:02:50 +00003973bool darwin::Link::NeedsTempPath(const InputInfoList &Inputs) const {
3974 // We only need to generate a temp path for LTO if we aren't compiling object
3975 // files. When compiling source files, we run 'dsymutil' after linking. We
3976 // don't run 'dsymutil' when compiling object files.
3977 for (InputInfoList::const_iterator
3978 it = Inputs.begin(), ie = Inputs.end(); it != ie; ++it)
3979 if (it->getType() != types::TY_Object)
3980 return true;
3981
3982 return false;
3983}
3984
Daniel Dunbar748de8e2010-09-09 21:51:05 +00003985void darwin::Link::AddLinkArgs(Compilation &C,
3986 const ArgList &Args,
Bill Wendling6acf8b42012-10-02 18:02:50 +00003987 ArgStringList &CmdArgs,
3988 const InputInfoList &Inputs) const {
Daniel Dunbaree788e72009-12-21 18:54:17 +00003989 const Driver &D = getToolChain().getDriver();
Daniel Dunbarce911f52011-04-28 21:23:41 +00003990 const toolchains::Darwin &DarwinTC = getDarwinToolChain();
Daniel Dunbar02633b52009-03-26 16:23:12 +00003991
Daniel Dunbarb18dc5b2010-08-11 23:07:50 +00003992 unsigned Version[3] = { 0, 0, 0 };
3993 if (Arg *A = Args.getLastArg(options::OPT_mlinker_version_EQ)) {
3994 bool HadExtra;
Richard Smith1d489cf2012-11-01 04:30:05 +00003995 if (!Driver::GetReleaseVersion(A->getValue(), Version[0],
Daniel Dunbarb18dc5b2010-08-11 23:07:50 +00003996 Version[1], Version[2], HadExtra) ||
3997 HadExtra)
Chris Lattner5f9e2722011-07-23 10:55:15 +00003998 D.Diag(diag::err_drv_invalid_version_number)
Daniel Dunbarb18dc5b2010-08-11 23:07:50 +00003999 << A->getAsString(Args);
4000 }
4001
4002 // Newer linkers support -demangle, pass it if supported and not disabled by
4003 // the user.
Daniel Dunbard2d20882012-01-04 21:45:27 +00004004 if (Version[0] >= 100 && !Args.hasArg(options::OPT_Z_Xlinker__no_demangle)) {
Daniel Dunbarbcf1da82010-09-07 17:07:49 +00004005 // Don't pass -demangle to ld_classic.
4006 //
4007 // FIXME: This is a temporary workaround, ld should be handling this.
4008 bool UsesLdClassic = (getToolChain().getArch() == llvm::Triple::x86 &&
4009 Args.hasArg(options::OPT_static));
Daniel Dunbar9ced7042010-09-07 17:50:41 +00004010 if (getToolChain().getArch() == llvm::Triple::x86) {
4011 for (arg_iterator it = Args.filtered_begin(options::OPT_Xlinker,
4012 options::OPT_Wl_COMMA),
4013 ie = Args.filtered_end(); it != ie; ++it) {
4014 const Arg *A = *it;
4015 for (unsigned i = 0, e = A->getNumValues(); i != e; ++i)
Richard Smith1d489cf2012-11-01 04:30:05 +00004016 if (StringRef(A->getValue(i)) == "-kext")
Daniel Dunbar9ced7042010-09-07 17:50:41 +00004017 UsesLdClassic = true;
4018 }
4019 }
Daniel Dunbarbcf1da82010-09-07 17:07:49 +00004020 if (!UsesLdClassic)
4021 CmdArgs.push_back("-demangle");
Daniel Dunbarb18dc5b2010-08-11 23:07:50 +00004022 }
4023
Bill Wendlingc35f9082012-11-16 23:03:00 +00004024 // If we are using LTO, then automatically create a temporary file path for
4025 // the linker to use, so that it's lifetime will extend past a possible
4026 // dsymutil step.
4027 if (Version[0] >= 116 && D.IsUsingLTO(Args) && NeedsTempPath(Inputs)) {
4028 const char *TmpPath = C.getArgs().MakeArgString(
4029 D.GetTemporaryPath("cc", types::getTypeTempSuffix(types::TY_Object)));
4030 C.addTempFile(TmpPath);
4031 CmdArgs.push_back("-object_path_lto");
4032 CmdArgs.push_back(TmpPath);
Daniel Dunbar5bfa6562011-06-21 20:55:11 +00004033 }
4034
Daniel Dunbar02633b52009-03-26 16:23:12 +00004035 // Derived from the "link" spec.
4036 Args.AddAllArgs(CmdArgs, options::OPT_static);
4037 if (!Args.hasArg(options::OPT_static))
4038 CmdArgs.push_back("-dynamic");
4039 if (Args.hasArg(options::OPT_fgnu_runtime)) {
4040 // FIXME: gcc replaces -lobjc in forward args with -lobjc-gnu
4041 // here. How do we wish to handle such things?
4042 }
Daniel Dunbarc21c4852009-04-08 23:54:23 +00004043
Daniel Dunbar02633b52009-03-26 16:23:12 +00004044 if (!Args.hasArg(options::OPT_dynamiclib)) {
Daniel Dunbara6d38492010-01-22 02:04:52 +00004045 AddDarwinArch(Args, CmdArgs);
Daniel Dunbara6d38492010-01-22 02:04:52 +00004046 // FIXME: Why do this only on this path?
Daniel Dunbar8917dd42010-01-22 03:37:33 +00004047 Args.AddLastArg(CmdArgs, options::OPT_force__cpusubtype__ALL);
Daniel Dunbar02633b52009-03-26 16:23:12 +00004048
4049 Args.AddLastArg(CmdArgs, options::OPT_bundle);
4050 Args.AddAllArgs(CmdArgs, options::OPT_bundle__loader);
4051 Args.AddAllArgs(CmdArgs, options::OPT_client__name);
4052
4053 Arg *A;
4054 if ((A = Args.getLastArg(options::OPT_compatibility__version)) ||
4055 (A = Args.getLastArg(options::OPT_current__version)) ||
4056 (A = Args.getLastArg(options::OPT_install__name)))
Chris Lattner5f9e2722011-07-23 10:55:15 +00004057 D.Diag(diag::err_drv_argument_only_allowed_with)
Daniel Dunbar02633b52009-03-26 16:23:12 +00004058 << A->getAsString(Args) << "-dynamiclib";
4059
4060 Args.AddLastArg(CmdArgs, options::OPT_force__flat__namespace);
4061 Args.AddLastArg(CmdArgs, options::OPT_keep__private__externs);
4062 Args.AddLastArg(CmdArgs, options::OPT_private__bundle);
4063 } else {
4064 CmdArgs.push_back("-dylib");
4065
4066 Arg *A;
4067 if ((A = Args.getLastArg(options::OPT_bundle)) ||
4068 (A = Args.getLastArg(options::OPT_bundle__loader)) ||
4069 (A = Args.getLastArg(options::OPT_client__name)) ||
4070 (A = Args.getLastArg(options::OPT_force__flat__namespace)) ||
4071 (A = Args.getLastArg(options::OPT_keep__private__externs)) ||
4072 (A = Args.getLastArg(options::OPT_private__bundle)))
Chris Lattner5f9e2722011-07-23 10:55:15 +00004073 D.Diag(diag::err_drv_argument_not_allowed_with)
Daniel Dunbar02633b52009-03-26 16:23:12 +00004074 << A->getAsString(Args) << "-dynamiclib";
Daniel Dunbarc21c4852009-04-08 23:54:23 +00004075
Daniel Dunbar02633b52009-03-26 16:23:12 +00004076 Args.AddAllArgsTranslated(CmdArgs, options::OPT_compatibility__version,
4077 "-dylib_compatibility_version");
4078 Args.AddAllArgsTranslated(CmdArgs, options::OPT_current__version,
4079 "-dylib_current_version");
4080
Daniel Dunbara6d38492010-01-22 02:04:52 +00004081 AddDarwinArch(Args, CmdArgs);
Daniel Dunbar02633b52009-03-26 16:23:12 +00004082
4083 Args.AddAllArgsTranslated(CmdArgs, options::OPT_install__name,
4084 "-dylib_install_name");
4085 }
4086
4087 Args.AddLastArg(CmdArgs, options::OPT_all__load);
4088 Args.AddAllArgs(CmdArgs, options::OPT_allowable__client);
4089 Args.AddLastArg(CmdArgs, options::OPT_bind__at__load);
Daniel Dunbarce911f52011-04-28 21:23:41 +00004090 if (DarwinTC.isTargetIPhoneOS())
Daniel Dunbard82f8fa2009-09-04 18:35:41 +00004091 Args.AddLastArg(CmdArgs, options::OPT_arch__errors__fatal);
Daniel Dunbar02633b52009-03-26 16:23:12 +00004092 Args.AddLastArg(CmdArgs, options::OPT_dead__strip);
4093 Args.AddLastArg(CmdArgs, options::OPT_no__dead__strip__inits__and__terms);
4094 Args.AddAllArgs(CmdArgs, options::OPT_dylib__file);
4095 Args.AddLastArg(CmdArgs, options::OPT_dynamic);
4096 Args.AddAllArgs(CmdArgs, options::OPT_exported__symbols__list);
4097 Args.AddLastArg(CmdArgs, options::OPT_flat__namespace);
Daniel Dunbar99ca47b2011-06-28 20:16:02 +00004098 Args.AddAllArgs(CmdArgs, options::OPT_force__load);
Daniel Dunbar02633b52009-03-26 16:23:12 +00004099 Args.AddAllArgs(CmdArgs, options::OPT_headerpad__max__install__names);
4100 Args.AddAllArgs(CmdArgs, options::OPT_image__base);
4101 Args.AddAllArgs(CmdArgs, options::OPT_init);
4102
Daniel Dunbarce911f52011-04-28 21:23:41 +00004103 // Add the deployment target.
Benjamin Kramer09c9a562012-03-10 20:55:36 +00004104 VersionTuple TargetVersion = DarwinTC.getTargetVersion();
Daniel Dunbarb7f5ef72011-04-30 04:22:58 +00004105
4106 // If we had an explicit -mios-simulator-version-min argument, honor that,
4107 // otherwise use the traditional deployment targets. We can't just check the
4108 // is-sim attribute because existing code follows this path, and the linker
4109 // may not handle the argument.
4110 //
4111 // FIXME: We may be able to remove this, once we can verify no one depends on
4112 // it.
4113 if (Args.hasArg(options::OPT_mios_simulator_version_min_EQ))
4114 CmdArgs.push_back("-ios_simulator_version_min");
4115 else if (DarwinTC.isTargetIPhoneOS())
4116 CmdArgs.push_back("-iphoneos_version_min");
4117 else
4118 CmdArgs.push_back("-macosx_version_min");
Benjamin Kramer09c9a562012-03-10 20:55:36 +00004119 CmdArgs.push_back(Args.MakeArgString(TargetVersion.getAsString()));
Daniel Dunbarce911f52011-04-28 21:23:41 +00004120
Daniel Dunbar02633b52009-03-26 16:23:12 +00004121 Args.AddLastArg(CmdArgs, options::OPT_nomultidefs);
4122 Args.AddLastArg(CmdArgs, options::OPT_multi__module);
4123 Args.AddLastArg(CmdArgs, options::OPT_single__module);
4124 Args.AddAllArgs(CmdArgs, options::OPT_multiply__defined);
4125 Args.AddAllArgs(CmdArgs, options::OPT_multiply__defined__unused);
Daniel Dunbarc21c4852009-04-08 23:54:23 +00004126
Daniel Dunbar47e879d2010-07-13 23:31:40 +00004127 if (const Arg *A = Args.getLastArg(options::OPT_fpie, options::OPT_fPIE,
4128 options::OPT_fno_pie,
4129 options::OPT_fno_PIE)) {
4130 if (A->getOption().matches(options::OPT_fpie) ||
4131 A->getOption().matches(options::OPT_fPIE))
4132 CmdArgs.push_back("-pie");
4133 else
4134 CmdArgs.push_back("-no_pie");
4135 }
Daniel Dunbar02633b52009-03-26 16:23:12 +00004136
4137 Args.AddLastArg(CmdArgs, options::OPT_prebind);
4138 Args.AddLastArg(CmdArgs, options::OPT_noprebind);
4139 Args.AddLastArg(CmdArgs, options::OPT_nofixprebinding);
4140 Args.AddLastArg(CmdArgs, options::OPT_prebind__all__twolevel__modules);
4141 Args.AddLastArg(CmdArgs, options::OPT_read__only__relocs);
4142 Args.AddAllArgs(CmdArgs, options::OPT_sectcreate);
4143 Args.AddAllArgs(CmdArgs, options::OPT_sectorder);
4144 Args.AddAllArgs(CmdArgs, options::OPT_seg1addr);
4145 Args.AddAllArgs(CmdArgs, options::OPT_segprot);
4146 Args.AddAllArgs(CmdArgs, options::OPT_segaddr);
4147 Args.AddAllArgs(CmdArgs, options::OPT_segs__read__only__addr);
4148 Args.AddAllArgs(CmdArgs, options::OPT_segs__read__write__addr);
4149 Args.AddAllArgs(CmdArgs, options::OPT_seg__addr__table);
4150 Args.AddAllArgs(CmdArgs, options::OPT_seg__addr__table__filename);
4151 Args.AddAllArgs(CmdArgs, options::OPT_sub__library);
4152 Args.AddAllArgs(CmdArgs, options::OPT_sub__umbrella);
Daniel Dunbard82f8fa2009-09-04 18:35:41 +00004153
Daniel Dunbarcc957192011-05-02 21:03:47 +00004154 // Give --sysroot= preference, over the Apple specific behavior to also use
4155 // --isysroot as the syslibroot.
Sebastian Pop4762a2d2012-04-16 04:16:43 +00004156 StringRef sysroot = C.getSysRoot();
4157 if (sysroot != "") {
Daniel Dunbarcc957192011-05-02 21:03:47 +00004158 CmdArgs.push_back("-syslibroot");
Sebastian Pop4762a2d2012-04-16 04:16:43 +00004159 CmdArgs.push_back(C.getArgs().MakeArgString(sysroot));
Daniel Dunbarcc957192011-05-02 21:03:47 +00004160 } else if (const Arg *A = Args.getLastArg(options::OPT_isysroot)) {
4161 CmdArgs.push_back("-syslibroot");
Richard Smith1d489cf2012-11-01 04:30:05 +00004162 CmdArgs.push_back(A->getValue());
Daniel Dunbard82f8fa2009-09-04 18:35:41 +00004163 }
4164
Daniel Dunbar02633b52009-03-26 16:23:12 +00004165 Args.AddLastArg(CmdArgs, options::OPT_twolevel__namespace);
4166 Args.AddLastArg(CmdArgs, options::OPT_twolevel__namespace__hints);
4167 Args.AddAllArgs(CmdArgs, options::OPT_umbrella);
4168 Args.AddAllArgs(CmdArgs, options::OPT_undefined);
4169 Args.AddAllArgs(CmdArgs, options::OPT_unexported__symbols__list);
Daniel Dunbard82f8fa2009-09-04 18:35:41 +00004170 Args.AddAllArgs(CmdArgs, options::OPT_weak__reference__mismatches);
Daniel Dunbar02633b52009-03-26 16:23:12 +00004171 Args.AddLastArg(CmdArgs, options::OPT_X_Flag);
4172 Args.AddAllArgs(CmdArgs, options::OPT_y);
4173 Args.AddLastArg(CmdArgs, options::OPT_w);
4174 Args.AddAllArgs(CmdArgs, options::OPT_pagezero__size);
4175 Args.AddAllArgs(CmdArgs, options::OPT_segs__read__);
4176 Args.AddLastArg(CmdArgs, options::OPT_seglinkedit);
4177 Args.AddLastArg(CmdArgs, options::OPT_noseglinkedit);
4178 Args.AddAllArgs(CmdArgs, options::OPT_sectalign);
4179 Args.AddAllArgs(CmdArgs, options::OPT_sectobjectsymbols);
4180 Args.AddAllArgs(CmdArgs, options::OPT_segcreate);
4181 Args.AddLastArg(CmdArgs, options::OPT_whyload);
4182 Args.AddLastArg(CmdArgs, options::OPT_whatsloaded);
4183 Args.AddAllArgs(CmdArgs, options::OPT_dylinker__install__name);
4184 Args.AddLastArg(CmdArgs, options::OPT_dylinker);
4185 Args.AddLastArg(CmdArgs, options::OPT_Mach);
4186}
4187
4188void darwin::Link::ConstructJob(Compilation &C, const JobAction &JA,
Daniel Dunbar2fe238e2010-08-02 02:38:28 +00004189 const InputInfo &Output,
Daniel Dunbarc21c4852009-04-08 23:54:23 +00004190 const InputInfoList &Inputs,
4191 const ArgList &Args,
Daniel Dunbar02633b52009-03-26 16:23:12 +00004192 const char *LinkingOutput) const {
4193 assert(Output.getType() == types::TY_Image && "Invalid linker output type.");
Daniel Dunbare0be8b12009-09-08 16:39:16 +00004194
Daniel Dunbar02633b52009-03-26 16:23:12 +00004195 // The logic here is derived from gcc's behavior; most of which
4196 // comes from specs (starting with link_command). Consult gcc for
4197 // more information.
Daniel Dunbar02633b52009-03-26 16:23:12 +00004198 ArgStringList CmdArgs;
4199
Argyrios Kyrtzidis22897172011-10-07 22:58:08 +00004200 /// Hack(tm) to ignore linking errors when we are doing ARC migration.
4201 if (Args.hasArg(options::OPT_ccc_arcmt_check,
4202 options::OPT_ccc_arcmt_migrate)) {
4203 for (ArgList::const_iterator I = Args.begin(), E = Args.end(); I != E; ++I)
4204 (*I)->claim();
4205 const char *Exec =
4206 Args.MakeArgString(getToolChain().GetProgramPath("touch"));
4207 CmdArgs.push_back(Output.getFilename());
4208 C.addCommand(new Command(JA, *this, Exec, CmdArgs));
4209 return;
4210 }
4211
Daniel Dunbar02633b52009-03-26 16:23:12 +00004212 // I'm not sure why this particular decomposition exists in gcc, but
4213 // we follow suite for ease of comparison.
Bill Wendling6acf8b42012-10-02 18:02:50 +00004214 AddLinkArgs(C, Args, CmdArgs, Inputs);
Daniel Dunbar02633b52009-03-26 16:23:12 +00004215
Daniel Dunbar02633b52009-03-26 16:23:12 +00004216 Args.AddAllArgs(CmdArgs, options::OPT_d_Flag);
4217 Args.AddAllArgs(CmdArgs, options::OPT_s);
4218 Args.AddAllArgs(CmdArgs, options::OPT_t);
4219 Args.AddAllArgs(CmdArgs, options::OPT_Z_Flag);
4220 Args.AddAllArgs(CmdArgs, options::OPT_u_Group);
Daniel Dunbar02633b52009-03-26 16:23:12 +00004221 Args.AddLastArg(CmdArgs, options::OPT_e);
4222 Args.AddAllArgs(CmdArgs, options::OPT_m_Separate);
4223 Args.AddAllArgs(CmdArgs, options::OPT_r);
4224
Daniel Dunbar270073c2010-10-18 22:08:36 +00004225 // Forward -ObjC when either -ObjC or -ObjC++ is used, to force loading
4226 // members of static archive libraries which implement Objective-C classes or
4227 // categories.
4228 if (Args.hasArg(options::OPT_ObjC) || Args.hasArg(options::OPT_ObjCXX))
4229 CmdArgs.push_back("-ObjC");
Michael J. Spencer20249a12010-10-21 03:16:25 +00004230
Bill Wendlingd56f4032012-12-10 21:48:41 +00004231 if (Args.hasArg(options::OPT_rdynamic))
4232 CmdArgs.push_back("-export_dynamic");
4233
Daniel Dunbar02633b52009-03-26 16:23:12 +00004234 CmdArgs.push_back("-o");
4235 CmdArgs.push_back(Output.getFilename());
4236
Chad Rosier18937312012-05-16 23:45:12 +00004237 if (!Args.hasArg(options::OPT_nostdlib) &&
Daniel Dunbar02633b52009-03-26 16:23:12 +00004238 !Args.hasArg(options::OPT_nostartfiles)) {
4239 // Derived from startfile spec.
4240 if (Args.hasArg(options::OPT_dynamiclib)) {
Daniel Dunbarc21c4852009-04-08 23:54:23 +00004241 // Derived from darwin_dylib1 spec.
Daniel Dunbar1051fc02011-04-01 21:02:42 +00004242 if (getDarwinToolChain().isTargetIOSSimulator()) {
4243 // The simulator doesn't have a versioned crt1 file.
4244 CmdArgs.push_back("-ldylib1.o");
4245 } else if (getDarwinToolChain().isTargetIPhoneOS()) {
Daniel Dunbarcacb0f02010-01-27 00:56:56 +00004246 if (getDarwinToolChain().isIPhoneOSVersionLT(3, 1))
4247 CmdArgs.push_back("-ldylib1.o");
4248 } else {
Daniel Dunbarce3fdf22010-01-27 00:57:03 +00004249 if (getDarwinToolChain().isMacosxVersionLT(10, 5))
Daniel Dunbarcacb0f02010-01-27 00:56:56 +00004250 CmdArgs.push_back("-ldylib1.o");
Daniel Dunbarce3fdf22010-01-27 00:57:03 +00004251 else if (getDarwinToolChain().isMacosxVersionLT(10, 6))
Daniel Dunbarcacb0f02010-01-27 00:56:56 +00004252 CmdArgs.push_back("-ldylib1.10.5.o");
4253 }
Daniel Dunbar02633b52009-03-26 16:23:12 +00004254 } else {
4255 if (Args.hasArg(options::OPT_bundle)) {
Daniel Dunbar8a8d8af2009-04-01 03:17:40 +00004256 if (!Args.hasArg(options::OPT_static)) {
4257 // Derived from darwin_bundle1 spec.
Daniel Dunbar1051fc02011-04-01 21:02:42 +00004258 if (getDarwinToolChain().isTargetIOSSimulator()) {
4259 // The simulator doesn't have a versioned crt1 file.
4260 CmdArgs.push_back("-lbundle1.o");
4261 } else if (getDarwinToolChain().isTargetIPhoneOS()) {
Daniel Dunbarcacb0f02010-01-27 00:56:56 +00004262 if (getDarwinToolChain().isIPhoneOSVersionLT(3, 1))
4263 CmdArgs.push_back("-lbundle1.o");
4264 } else {
Daniel Dunbarce3fdf22010-01-27 00:57:03 +00004265 if (getDarwinToolChain().isMacosxVersionLT(10, 6))
Daniel Dunbarcacb0f02010-01-27 00:56:56 +00004266 CmdArgs.push_back("-lbundle1.o");
4267 }
Daniel Dunbar8a8d8af2009-04-01 03:17:40 +00004268 }
Daniel Dunbar02633b52009-03-26 16:23:12 +00004269 } else {
Daniel Dunbarbbe8e3e2011-03-01 18:49:30 +00004270 if (Args.hasArg(options::OPT_pg) &&
4271 getToolChain().SupportsProfiling()) {
Daniel Dunbar02633b52009-03-26 16:23:12 +00004272 if (Args.hasArg(options::OPT_static) ||
4273 Args.hasArg(options::OPT_object) ||
4274 Args.hasArg(options::OPT_preload)) {
4275 CmdArgs.push_back("-lgcrt0.o");
4276 } else {
4277 CmdArgs.push_back("-lgcrt1.o");
Daniel Dunbarc21c4852009-04-08 23:54:23 +00004278
Daniel Dunbar02633b52009-03-26 16:23:12 +00004279 // darwin_crt2 spec is empty.
Daniel Dunbarc21c4852009-04-08 23:54:23 +00004280 }
Bob Wilson4e6e7912012-07-04 00:18:41 +00004281 // By default on OS X 10.8 and later, we don't link with a crt1.o
4282 // file and the linker knows to use _main as the entry point. But,
4283 // when compiling with -pg, we need to link with the gcrt1.o file,
4284 // so pass the -no_new_main option to tell the linker to use the
4285 // "start" symbol as the entry point.
Bob Wilson1fc6e4f2012-07-03 20:42:10 +00004286 if (getDarwinToolChain().isTargetMacOS() &&
4287 !getDarwinToolChain().isMacosxVersionLT(10, 8))
4288 CmdArgs.push_back("-no_new_main");
Daniel Dunbar02633b52009-03-26 16:23:12 +00004289 } else {
4290 if (Args.hasArg(options::OPT_static) ||
4291 Args.hasArg(options::OPT_object) ||
4292 Args.hasArg(options::OPT_preload)) {
4293 CmdArgs.push_back("-lcrt0.o");
4294 } else {
4295 // Derived from darwin_crt1 spec.
Daniel Dunbar40355802011-03-31 17:12:33 +00004296 if (getDarwinToolChain().isTargetIOSSimulator()) {
4297 // The simulator doesn't have a versioned crt1 file.
4298 CmdArgs.push_back("-lcrt1.o");
4299 } else if (getDarwinToolChain().isTargetIPhoneOS()) {
Daniel Dunbarcacb0f02010-01-27 00:56:56 +00004300 if (getDarwinToolChain().isIPhoneOSVersionLT(3, 1))
4301 CmdArgs.push_back("-lcrt1.o");
Daniel Dunbar7a0c0642012-10-15 22:23:53 +00004302 else if (getDarwinToolChain().isIPhoneOSVersionLT(6, 0))
Daniel Dunbarcacb0f02010-01-27 00:56:56 +00004303 CmdArgs.push_back("-lcrt1.3.1.o");
Daniel Dunbarce3fdf22010-01-27 00:57:03 +00004304 } else {
4305 if (getDarwinToolChain().isMacosxVersionLT(10, 5))
4306 CmdArgs.push_back("-lcrt1.o");
4307 else if (getDarwinToolChain().isMacosxVersionLT(10, 6))
4308 CmdArgs.push_back("-lcrt1.10.5.o");
Ted Kremenekebcb57a2012-03-06 20:05:56 +00004309 else if (getDarwinToolChain().isMacosxVersionLT(10, 8))
Daniel Dunbarce3fdf22010-01-27 00:57:03 +00004310 CmdArgs.push_back("-lcrt1.10.6.o");
Daniel Dunbarc21c4852009-04-08 23:54:23 +00004311
Daniel Dunbarce3fdf22010-01-27 00:57:03 +00004312 // darwin_crt2 spec is empty.
4313 }
Daniel Dunbar02633b52009-03-26 16:23:12 +00004314 }
4315 }
4316 }
4317 }
4318
Daniel Dunbarce3fdf22010-01-27 00:57:03 +00004319 if (!getDarwinToolChain().isTargetIPhoneOS() &&
4320 Args.hasArg(options::OPT_shared_libgcc) &&
4321 getDarwinToolChain().isMacosxVersionLT(10, 5)) {
Daniel Dunbar88137642009-09-09 22:32:48 +00004322 const char *Str =
Daniel Dunbar4a7e8892010-07-14 18:46:23 +00004323 Args.MakeArgString(getToolChain().GetFilePath("crt3.o"));
Daniel Dunbar88137642009-09-09 22:32:48 +00004324 CmdArgs.push_back(Str);
Daniel Dunbar02633b52009-03-26 16:23:12 +00004325 }
4326 }
4327
4328 Args.AddAllArgs(CmdArgs, options::OPT_L);
Daniel Dunbarc21c4852009-04-08 23:54:23 +00004329
Alexey Samsonovbb1071c2012-11-06 15:09:03 +00004330 SanitizerArgs Sanitize(getToolChain().getDriver(), Args);
Alexey Samsonov2cb3d302013-01-21 08:45:02 +00004331 // If we're building a dynamic lib with -fsanitize=address,
4332 // unresolved symbols may appear. Mark all
Alexey Samsonov75fcb192012-11-16 12:53:14 +00004333 // of them as dynamic_lookup. Linking executables is handled in
4334 // lib/Driver/ToolChains.cpp.
Alexey Samsonov2cb3d302013-01-21 08:45:02 +00004335 if (Sanitize.needsAsanRt()) {
Kostya Serebryany7b5f1012011-12-06 19:18:44 +00004336 if (Args.hasArg(options::OPT_dynamiclib) ||
4337 Args.hasArg(options::OPT_bundle)) {
4338 CmdArgs.push_back("-undefined");
4339 CmdArgs.push_back("dynamic_lookup");
4340 }
4341 }
4342
Daniel Dunbar02633b52009-03-26 16:23:12 +00004343 if (Args.hasArg(options::OPT_fopenmp))
4344 // This is more complicated in gcc...
4345 CmdArgs.push_back("-lgomp");
4346
Douglas Gregor04e326b2012-05-15 21:00:27 +00004347 AddLinkerInputs(getToolChain(), Inputs, Args, CmdArgs);
4348
Bob Wilson63d9f3c2012-05-15 18:57:39 +00004349 if (isObjCRuntimeLinked(Args) &&
4350 !Args.hasArg(options::OPT_nostdlib) &&
4351 !Args.hasArg(options::OPT_nodefaultlibs)) {
Ted Kremenekebcb57a2012-03-06 20:05:56 +00004352 // Avoid linking compatibility stubs on i386 mac.
4353 if (!getDarwinToolChain().isTargetMacOS() ||
Rafael Espindola64f7ad92012-10-07 04:44:33 +00004354 getDarwinToolChain().getArch() != llvm::Triple::x86) {
Ted Kremenekebcb57a2012-03-06 20:05:56 +00004355 // If we don't have ARC or subscripting runtime support, link in the
4356 // runtime stubs. We have to do this *before* adding any of the normal
4357 // linker inputs so that its initializer gets run first.
John McCall260611a2012-06-20 06:18:46 +00004358 ObjCRuntime runtime =
4359 getDarwinToolChain().getDefaultObjCRuntime(/*nonfragile*/ true);
Ted Kremenekebcb57a2012-03-06 20:05:56 +00004360 // We use arclite library for both ARC and subscripting support.
John McCall0a7dd782012-08-21 02:47:43 +00004361 if ((!runtime.hasNativeARC() && isObjCAutoRefCount(Args)) ||
John McCall260611a2012-06-20 06:18:46 +00004362 !runtime.hasSubscripting())
Ted Kremenekebcb57a2012-03-06 20:05:56 +00004363 getDarwinToolChain().AddLinkARCArgs(Args, CmdArgs);
Ted Kremenekebcb57a2012-03-06 20:05:56 +00004364 }
Bob Wilson0b1c7152012-04-21 00:21:42 +00004365 CmdArgs.push_back("-framework");
4366 CmdArgs.push_back("Foundation");
Ted Kremenekebcb57a2012-03-06 20:05:56 +00004367 // Link libobj.
4368 CmdArgs.push_back("-lobjc");
John McCall9f084a32011-07-06 00:26:06 +00004369 }
John McCallf85e1932011-06-15 23:02:42 +00004370
Daniel Dunbar02633b52009-03-26 16:23:12 +00004371 if (LinkingOutput) {
4372 CmdArgs.push_back("-arch_multiple");
4373 CmdArgs.push_back("-final_output");
4374 CmdArgs.push_back(LinkingOutput);
4375 }
4376
Daniel Dunbar02633b52009-03-26 16:23:12 +00004377 if (Args.hasArg(options::OPT_fnested_functions))
4378 CmdArgs.push_back("-allow_stack_execute");
Daniel Dunbarc21c4852009-04-08 23:54:23 +00004379
Daniel Dunbar02633b52009-03-26 16:23:12 +00004380 if (!Args.hasArg(options::OPT_nostdlib) &&
4381 !Args.hasArg(options::OPT_nodefaultlibs)) {
Daniel Dunbaree788e72009-12-21 18:54:17 +00004382 if (getToolChain().getDriver().CCCIsCXX)
Daniel Dunbar132e35d2010-09-17 01:20:05 +00004383 getToolChain().AddCXXStdlibLibArgs(Args, CmdArgs);
Daniel Dunbaredfa02b2009-04-08 06:06:21 +00004384
Daniel Dunbar02633b52009-03-26 16:23:12 +00004385 // link_ssp spec is empty.
4386
Daniel Dunbar6cd41542009-09-18 08:15:03 +00004387 // Let the tool chain choose which runtime library to link.
4388 getDarwinToolChain().AddLinkRuntimeLibArgs(Args, CmdArgs);
Daniel Dunbar02633b52009-03-26 16:23:12 +00004389 }
4390
Chad Rosier18937312012-05-16 23:45:12 +00004391 if (!Args.hasArg(options::OPT_nostdlib) &&
Daniel Dunbar02633b52009-03-26 16:23:12 +00004392 !Args.hasArg(options::OPT_nostartfiles)) {
4393 // endfile_spec is empty.
4394 }
4395
4396 Args.AddAllArgs(CmdArgs, options::OPT_T_Group);
4397 Args.AddAllArgs(CmdArgs, options::OPT_F);
4398
Daniel Dunbarc21c4852009-04-08 23:54:23 +00004399 const char *Exec =
Daniel Dunbar4a7e8892010-07-14 18:46:23 +00004400 Args.MakeArgString(getToolChain().GetProgramPath("ld"));
Daniel Dunbar2fe238e2010-08-02 02:38:28 +00004401 C.addCommand(new Command(JA, *this, Exec, CmdArgs));
Daniel Dunbar02633b52009-03-26 16:23:12 +00004402}
4403
Daniel Dunbarff7488d2009-03-20 00:52:38 +00004404void darwin::Lipo::ConstructJob(Compilation &C, const JobAction &JA,
Daniel Dunbar2fe238e2010-08-02 02:38:28 +00004405 const InputInfo &Output,
Daniel Dunbarc21c4852009-04-08 23:54:23 +00004406 const InputInfoList &Inputs,
4407 const ArgList &Args,
Daniel Dunbarff7488d2009-03-20 00:52:38 +00004408 const char *LinkingOutput) const {
4409 ArgStringList CmdArgs;
4410
4411 CmdArgs.push_back("-create");
4412 assert(Output.isFilename() && "Unexpected lipo output.");
Daniel Dunbara428df82009-03-24 00:24:37 +00004413
4414 CmdArgs.push_back("-output");
Daniel Dunbarff7488d2009-03-20 00:52:38 +00004415 CmdArgs.push_back(Output.getFilename());
Daniel Dunbara428df82009-03-24 00:24:37 +00004416
Daniel Dunbarff7488d2009-03-20 00:52:38 +00004417 for (InputInfoList::const_iterator
4418 it = Inputs.begin(), ie = Inputs.end(); it != ie; ++it) {
4419 const InputInfo &II = *it;
4420 assert(II.isFilename() && "Unexpected lipo input.");
4421 CmdArgs.push_back(II.getFilename());
4422 }
Daniel Dunbarc21c4852009-04-08 23:54:23 +00004423 const char *Exec =
Daniel Dunbar4a7e8892010-07-14 18:46:23 +00004424 Args.MakeArgString(getToolChain().GetProgramPath("lipo"));
Daniel Dunbar2fe238e2010-08-02 02:38:28 +00004425 C.addCommand(new Command(JA, *this, Exec, CmdArgs));
Daniel Dunbarff7488d2009-03-20 00:52:38 +00004426}
Daniel Dunbar68a31d42009-03-31 17:45:15 +00004427
Daniel Dunbar6e0f2542010-06-04 18:28:36 +00004428void darwin::Dsymutil::ConstructJob(Compilation &C, const JobAction &JA,
Daniel Dunbar2fe238e2010-08-02 02:38:28 +00004429 const InputInfo &Output,
Daniel Dunbar6e0f2542010-06-04 18:28:36 +00004430 const InputInfoList &Inputs,
4431 const ArgList &Args,
4432 const char *LinkingOutput) const {
4433 ArgStringList CmdArgs;
4434
Daniel Dunbar03e92302011-05-09 17:23:16 +00004435 CmdArgs.push_back("-o");
4436 CmdArgs.push_back(Output.getFilename());
4437
Daniel Dunbar6e0f2542010-06-04 18:28:36 +00004438 assert(Inputs.size() == 1 && "Unable to handle multiple inputs.");
4439 const InputInfo &Input = Inputs[0];
4440 assert(Input.isFilename() && "Unexpected dsymutil input.");
4441 CmdArgs.push_back(Input.getFilename());
4442
Daniel Dunbar6e0f2542010-06-04 18:28:36 +00004443 const char *Exec =
Daniel Dunbar4a7e8892010-07-14 18:46:23 +00004444 Args.MakeArgString(getToolChain().GetProgramPath("dsymutil"));
Daniel Dunbar2fe238e2010-08-02 02:38:28 +00004445 C.addCommand(new Command(JA, *this, Exec, CmdArgs));
Daniel Dunbar6e0f2542010-06-04 18:28:36 +00004446}
4447
Eric Christopherf8571862011-08-23 17:56:55 +00004448void darwin::VerifyDebug::ConstructJob(Compilation &C, const JobAction &JA,
Eric Christopher27e2b982012-12-18 00:31:10 +00004449 const InputInfo &Output,
4450 const InputInfoList &Inputs,
4451 const ArgList &Args,
4452 const char *LinkingOutput) const {
Eric Christopherf8571862011-08-23 17:56:55 +00004453 ArgStringList CmdArgs;
4454 CmdArgs.push_back("--verify");
Eric Christopher1c79dc42012-02-06 19:13:09 +00004455 CmdArgs.push_back("--debug-info");
4456 CmdArgs.push_back("--eh-frame");
Eric Christopherb822f722012-02-06 19:43:51 +00004457 CmdArgs.push_back("--quiet");
Eric Christopherf8571862011-08-23 17:56:55 +00004458
4459 assert(Inputs.size() == 1 && "Unable to handle multiple inputs.");
4460 const InputInfo &Input = Inputs[0];
4461 assert(Input.isFilename() && "Unexpected verify input");
4462
4463 // Grabbing the output of the earlier dsymutil run.
4464 CmdArgs.push_back(Input.getFilename());
4465
4466 const char *Exec =
4467 Args.MakeArgString(getToolChain().GetProgramPath("dwarfdump"));
4468 C.addCommand(new Command(JA, *this, Exec, CmdArgs));
4469}
4470
David Chisnall31c46902012-02-15 13:39:01 +00004471void solaris::Assemble::ConstructJob(Compilation &C, const JobAction &JA,
4472 const InputInfo &Output,
4473 const InputInfoList &Inputs,
4474 const ArgList &Args,
4475 const char *LinkingOutput) const {
4476 ArgStringList CmdArgs;
4477
4478 Args.AddAllArgValues(CmdArgs, options::OPT_Wa_COMMA,
4479 options::OPT_Xassembler);
4480
4481 CmdArgs.push_back("-o");
4482 CmdArgs.push_back(Output.getFilename());
4483
4484 for (InputInfoList::const_iterator
4485 it = Inputs.begin(), ie = Inputs.end(); it != ie; ++it) {
4486 const InputInfo &II = *it;
4487 CmdArgs.push_back(II.getFilename());
4488 }
4489
4490 const char *Exec =
4491 Args.MakeArgString(getToolChain().GetProgramPath("as"));
4492 C.addCommand(new Command(JA, *this, Exec, CmdArgs));
4493}
4494
4495
4496void solaris::Link::ConstructJob(Compilation &C, const JobAction &JA,
4497 const InputInfo &Output,
4498 const InputInfoList &Inputs,
4499 const ArgList &Args,
4500 const char *LinkingOutput) const {
4501 // FIXME: Find a real GCC, don't hard-code versions here
4502 std::string GCCLibPath = "/usr/gcc/4.5/lib/gcc/";
4503 const llvm::Triple &T = getToolChain().getTriple();
4504 std::string LibPath = "/usr/lib/";
4505 llvm::Triple::ArchType Arch = T.getArch();
4506 switch (Arch) {
4507 case llvm::Triple::x86:
4508 GCCLibPath += ("i386-" + T.getVendorName() + "-" +
4509 T.getOSName()).str() + "/4.5.2/";
4510 break;
4511 case llvm::Triple::x86_64:
4512 GCCLibPath += ("i386-" + T.getVendorName() + "-" +
4513 T.getOSName()).str();
4514 GCCLibPath += "/4.5.2/amd64/";
4515 LibPath += "amd64/";
4516 break;
4517 default:
4518 assert(0 && "Unsupported architecture");
4519 }
4520
4521 ArgStringList CmdArgs;
4522
David Chisnall41d476d2012-02-29 15:06:12 +00004523 // Demangle C++ names in errors
4524 CmdArgs.push_back("-C");
4525
David Chisnall31c46902012-02-15 13:39:01 +00004526 if ((!Args.hasArg(options::OPT_nostdlib)) &&
4527 (!Args.hasArg(options::OPT_shared))) {
4528 CmdArgs.push_back("-e");
4529 CmdArgs.push_back("_start");
4530 }
4531
4532 if (Args.hasArg(options::OPT_static)) {
4533 CmdArgs.push_back("-Bstatic");
4534 CmdArgs.push_back("-dn");
4535 } else {
4536 CmdArgs.push_back("-Bdynamic");
4537 if (Args.hasArg(options::OPT_shared)) {
4538 CmdArgs.push_back("-shared");
4539 } else {
4540 CmdArgs.push_back("--dynamic-linker");
4541 CmdArgs.push_back(Args.MakeArgString(LibPath + "ld.so.1"));
4542 }
4543 }
4544
4545 if (Output.isFilename()) {
4546 CmdArgs.push_back("-o");
4547 CmdArgs.push_back(Output.getFilename());
4548 } else {
4549 assert(Output.isNothing() && "Invalid output.");
4550 }
4551
4552 if (!Args.hasArg(options::OPT_nostdlib) &&
4553 !Args.hasArg(options::OPT_nostartfiles)) {
4554 if (!Args.hasArg(options::OPT_shared)) {
4555 CmdArgs.push_back(Args.MakeArgString(LibPath + "crt1.o"));
4556 CmdArgs.push_back(Args.MakeArgString(LibPath + "crti.o"));
David Chisnall165329c2012-02-28 17:10:04 +00004557 CmdArgs.push_back(Args.MakeArgString(LibPath + "values-Xa.o"));
David Chisnall31c46902012-02-15 13:39:01 +00004558 CmdArgs.push_back(Args.MakeArgString(GCCLibPath + "crtbegin.o"));
4559 } else {
4560 CmdArgs.push_back(Args.MakeArgString(LibPath + "crti.o"));
David Chisnall165329c2012-02-28 17:10:04 +00004561 CmdArgs.push_back(Args.MakeArgString(LibPath + "values-Xa.o"));
4562 CmdArgs.push_back(Args.MakeArgString(GCCLibPath + "crtbegin.o"));
David Chisnall31c46902012-02-15 13:39:01 +00004563 }
David Chisnalle6dd6832012-03-13 14:14:54 +00004564 if (getToolChain().getDriver().CCCIsCXX)
4565 CmdArgs.push_back(Args.MakeArgString(LibPath + "cxa_finalize.o"));
David Chisnall31c46902012-02-15 13:39:01 +00004566 }
4567
4568 CmdArgs.push_back(Args.MakeArgString("-L" + GCCLibPath));
4569
4570 Args.AddAllArgs(CmdArgs, options::OPT_L);
4571 Args.AddAllArgs(CmdArgs, options::OPT_T_Group);
4572 Args.AddAllArgs(CmdArgs, options::OPT_e);
David Chisnall165329c2012-02-28 17:10:04 +00004573 Args.AddAllArgs(CmdArgs, options::OPT_r);
David Chisnall31c46902012-02-15 13:39:01 +00004574
4575 AddLinkerInputs(getToolChain(), Inputs, Args, CmdArgs);
4576
4577 if (!Args.hasArg(options::OPT_nostdlib) &&
4578 !Args.hasArg(options::OPT_nodefaultlibs)) {
David Chisnalle58e6f92012-04-10 11:49:50 +00004579 if (getToolChain().getDriver().CCCIsCXX)
4580 getToolChain().AddCXXStdlibLibArgs(Args, CmdArgs);
David Chisnallb6229592012-02-15 18:24:31 +00004581 CmdArgs.push_back("-lgcc_s");
David Chisnall165329c2012-02-28 17:10:04 +00004582 if (!Args.hasArg(options::OPT_shared)) {
4583 CmdArgs.push_back("-lgcc");
David Chisnall31c46902012-02-15 13:39:01 +00004584 CmdArgs.push_back("-lc");
David Chisnall7dbefe12012-02-28 20:06:45 +00004585 CmdArgs.push_back("-lm");
David Chisnall165329c2012-02-28 17:10:04 +00004586 }
David Chisnall31c46902012-02-15 13:39:01 +00004587 }
4588
4589 if (!Args.hasArg(options::OPT_nostdlib) &&
4590 !Args.hasArg(options::OPT_nostartfiles)) {
David Chisnall165329c2012-02-28 17:10:04 +00004591 CmdArgs.push_back(Args.MakeArgString(GCCLibPath + "crtend.o"));
David Chisnall31c46902012-02-15 13:39:01 +00004592 }
David Chisnalld1ac03e2012-02-16 16:00:47 +00004593 CmdArgs.push_back(Args.MakeArgString(LibPath + "crtn.o"));
David Chisnall31c46902012-02-15 13:39:01 +00004594
4595 addProfileRT(getToolChain(), Args, CmdArgs, getToolChain().getTriple());
4596
4597 const char *Exec =
4598 Args.MakeArgString(getToolChain().GetProgramPath("ld"));
4599 C.addCommand(new Command(JA, *this, Exec, CmdArgs));
4600}
4601
Edward O'Callaghane7925a02009-08-22 01:06:46 +00004602void auroraux::Assemble::ConstructJob(Compilation &C, const JobAction &JA,
Daniel Dunbar2fe238e2010-08-02 02:38:28 +00004603 const InputInfo &Output,
Daniel Dunbar294691e2009-11-04 06:24:38 +00004604 const InputInfoList &Inputs,
4605 const ArgList &Args,
4606 const char *LinkingOutput) const {
Edward O'Callaghane7925a02009-08-22 01:06:46 +00004607 ArgStringList CmdArgs;
4608
4609 Args.AddAllArgValues(CmdArgs, options::OPT_Wa_COMMA,
4610 options::OPT_Xassembler);
4611
4612 CmdArgs.push_back("-o");
Daniel Dunbar7c1e4652010-08-02 02:38:21 +00004613 CmdArgs.push_back(Output.getFilename());
Edward O'Callaghane7925a02009-08-22 01:06:46 +00004614
4615 for (InputInfoList::const_iterator
4616 it = Inputs.begin(), ie = Inputs.end(); it != ie; ++it) {
4617 const InputInfo &II = *it;
Daniel Dunbar7c1e4652010-08-02 02:38:21 +00004618 CmdArgs.push_back(II.getFilename());
Edward O'Callaghane7925a02009-08-22 01:06:46 +00004619 }
4620
4621 const char *Exec =
Daniel Dunbar4a7e8892010-07-14 18:46:23 +00004622 Args.MakeArgString(getToolChain().GetProgramPath("gas"));
Daniel Dunbar2fe238e2010-08-02 02:38:28 +00004623 C.addCommand(new Command(JA, *this, Exec, CmdArgs));
Edward O'Callaghane7925a02009-08-22 01:06:46 +00004624}
4625
4626void auroraux::Link::ConstructJob(Compilation &C, const JobAction &JA,
Daniel Dunbar2fe238e2010-08-02 02:38:28 +00004627 const InputInfo &Output,
Daniel Dunbar294691e2009-11-04 06:24:38 +00004628 const InputInfoList &Inputs,
4629 const ArgList &Args,
4630 const char *LinkingOutput) const {
Edward O'Callaghane7925a02009-08-22 01:06:46 +00004631 ArgStringList CmdArgs;
4632
4633 if ((!Args.hasArg(options::OPT_nostdlib)) &&
Daniel Dunbar294691e2009-11-04 06:24:38 +00004634 (!Args.hasArg(options::OPT_shared))) {
Edward O'Callaghane7925a02009-08-22 01:06:46 +00004635 CmdArgs.push_back("-e");
Edward O'Callaghan7adf9492009-10-15 07:44:07 +00004636 CmdArgs.push_back("_start");
Edward O'Callaghane7925a02009-08-22 01:06:46 +00004637 }
4638
4639 if (Args.hasArg(options::OPT_static)) {
4640 CmdArgs.push_back("-Bstatic");
Edward O'Callaghan7adf9492009-10-15 07:44:07 +00004641 CmdArgs.push_back("-dn");
Edward O'Callaghane7925a02009-08-22 01:06:46 +00004642 } else {
Edward O'Callaghan7adf9492009-10-15 07:44:07 +00004643// CmdArgs.push_back("--eh-frame-hdr");
Edward O'Callaghane7925a02009-08-22 01:06:46 +00004644 CmdArgs.push_back("-Bdynamic");
4645 if (Args.hasArg(options::OPT_shared)) {
4646 CmdArgs.push_back("-shared");
4647 } else {
Edward O'Callaghan3cecc192009-10-16 19:44:18 +00004648 CmdArgs.push_back("--dynamic-linker");
Edward O'Callaghane7925a02009-08-22 01:06:46 +00004649 CmdArgs.push_back("/lib/ld.so.1"); // 64Bit Path /lib/amd64/ld.so.1
4650 }
4651 }
4652
Daniel Dunbar7c1e4652010-08-02 02:38:21 +00004653 if (Output.isFilename()) {
Edward O'Callaghane7925a02009-08-22 01:06:46 +00004654 CmdArgs.push_back("-o");
4655 CmdArgs.push_back(Output.getFilename());
4656 } else {
4657 assert(Output.isNothing() && "Invalid output.");
4658 }
4659
4660 if (!Args.hasArg(options::OPT_nostdlib) &&
4661 !Args.hasArg(options::OPT_nostartfiles)) {
4662 if (!Args.hasArg(options::OPT_shared)) {
Chris Lattner38e317d2010-07-07 16:01:42 +00004663 CmdArgs.push_back(Args.MakeArgString(
Daniel Dunbar4a7e8892010-07-14 18:46:23 +00004664 getToolChain().GetFilePath("crt1.o")));
Chris Lattner38e317d2010-07-07 16:01:42 +00004665 CmdArgs.push_back(Args.MakeArgString(
Daniel Dunbar4a7e8892010-07-14 18:46:23 +00004666 getToolChain().GetFilePath("crti.o")));
Chris Lattner38e317d2010-07-07 16:01:42 +00004667 CmdArgs.push_back(Args.MakeArgString(
Daniel Dunbar4a7e8892010-07-14 18:46:23 +00004668 getToolChain().GetFilePath("crtbegin.o")));
Edward O'Callaghane7925a02009-08-22 01:06:46 +00004669 } else {
Chris Lattner38e317d2010-07-07 16:01:42 +00004670 CmdArgs.push_back(Args.MakeArgString(
Daniel Dunbar4a7e8892010-07-14 18:46:23 +00004671 getToolChain().GetFilePath("crti.o")));
Edward O'Callaghane7925a02009-08-22 01:06:46 +00004672 }
Chris Lattner38e317d2010-07-07 16:01:42 +00004673 CmdArgs.push_back(Args.MakeArgString(
Daniel Dunbar4a7e8892010-07-14 18:46:23 +00004674 getToolChain().GetFilePath("crtn.o")));
Edward O'Callaghane7925a02009-08-22 01:06:46 +00004675 }
4676
Daniel Dunbar294691e2009-11-04 06:24:38 +00004677 CmdArgs.push_back(Args.MakeArgString("-L/opt/gcc4/lib/gcc/"
4678 + getToolChain().getTripleString()
Daniel Dunbarf7fb31f2009-10-29 02:24:37 +00004679 + "/4.2.4"));
Edward O'Callaghane7925a02009-08-22 01:06:46 +00004680
4681 Args.AddAllArgs(CmdArgs, options::OPT_L);
4682 Args.AddAllArgs(CmdArgs, options::OPT_T_Group);
4683 Args.AddAllArgs(CmdArgs, options::OPT_e);
4684
Daniel Dunbar2008fee2010-09-17 00:24:54 +00004685 AddLinkerInputs(getToolChain(), Inputs, Args, CmdArgs);
Edward O'Callaghane7925a02009-08-22 01:06:46 +00004686
4687 if (!Args.hasArg(options::OPT_nostdlib) &&
4688 !Args.hasArg(options::OPT_nodefaultlibs)) {
4689 // FIXME: For some reason GCC passes -lgcc before adding
4690 // the default system libraries. Just mimic this for now.
4691 CmdArgs.push_back("-lgcc");
4692
4693 if (Args.hasArg(options::OPT_pthread))
4694 CmdArgs.push_back("-pthread");
4695 if (!Args.hasArg(options::OPT_shared))
4696 CmdArgs.push_back("-lc");
4697 CmdArgs.push_back("-lgcc");
4698 }
4699
4700 if (!Args.hasArg(options::OPT_nostdlib) &&
4701 !Args.hasArg(options::OPT_nostartfiles)) {
4702 if (!Args.hasArg(options::OPT_shared))
Chris Lattner38e317d2010-07-07 16:01:42 +00004703 CmdArgs.push_back(Args.MakeArgString(
Daniel Dunbar4a7e8892010-07-14 18:46:23 +00004704 getToolChain().GetFilePath("crtend.o")));
Edward O'Callaghane7925a02009-08-22 01:06:46 +00004705 }
4706
Bill Wendling3f4be6f2011-06-27 19:15:03 +00004707 addProfileRT(getToolChain(), Args, CmdArgs, getToolChain().getTriple());
Nick Lewycky2e95a6d2011-05-24 21:54:59 +00004708
Edward O'Callaghane7925a02009-08-22 01:06:46 +00004709 const char *Exec =
Daniel Dunbar4a7e8892010-07-14 18:46:23 +00004710 Args.MakeArgString(getToolChain().GetProgramPath("ld"));
Daniel Dunbar2fe238e2010-08-02 02:38:28 +00004711 C.addCommand(new Command(JA, *this, Exec, CmdArgs));
Edward O'Callaghane7925a02009-08-22 01:06:46 +00004712}
4713
Daniel Dunbarf7b8eec2009-06-29 20:52:51 +00004714void openbsd::Assemble::ConstructJob(Compilation &C, const JobAction &JA,
Daniel Dunbar2fe238e2010-08-02 02:38:28 +00004715 const InputInfo &Output,
Daniel Dunbarf7b8eec2009-06-29 20:52:51 +00004716 const InputInfoList &Inputs,
4717 const ArgList &Args,
Mike Stump1eb44332009-09-09 15:08:12 +00004718 const char *LinkingOutput) const {
Daniel Dunbarf7b8eec2009-06-29 20:52:51 +00004719 ArgStringList CmdArgs;
4720
4721 Args.AddAllArgValues(CmdArgs, options::OPT_Wa_COMMA,
4722 options::OPT_Xassembler);
4723
4724 CmdArgs.push_back("-o");
Daniel Dunbar7c1e4652010-08-02 02:38:21 +00004725 CmdArgs.push_back(Output.getFilename());
Daniel Dunbarf7b8eec2009-06-29 20:52:51 +00004726
4727 for (InputInfoList::const_iterator
4728 it = Inputs.begin(), ie = Inputs.end(); it != ie; ++it) {
4729 const InputInfo &II = *it;
Daniel Dunbar7c1e4652010-08-02 02:38:21 +00004730 CmdArgs.push_back(II.getFilename());
Daniel Dunbarf7b8eec2009-06-29 20:52:51 +00004731 }
4732
4733 const char *Exec =
Daniel Dunbar4a7e8892010-07-14 18:46:23 +00004734 Args.MakeArgString(getToolChain().GetProgramPath("as"));
Daniel Dunbar2fe238e2010-08-02 02:38:28 +00004735 C.addCommand(new Command(JA, *this, Exec, CmdArgs));
Daniel Dunbarf7b8eec2009-06-29 20:52:51 +00004736}
4737
4738void openbsd::Link::ConstructJob(Compilation &C, const JobAction &JA,
Daniel Dunbar2fe238e2010-08-02 02:38:28 +00004739 const InputInfo &Output,
Daniel Dunbarf7b8eec2009-06-29 20:52:51 +00004740 const InputInfoList &Inputs,
4741 const ArgList &Args,
4742 const char *LinkingOutput) const {
Daniel Dunbaree788e72009-12-21 18:54:17 +00004743 const Driver &D = getToolChain().getDriver();
Daniel Dunbarf7b8eec2009-06-29 20:52:51 +00004744 ArgStringList CmdArgs;
4745
Rafael Espindola6cc2a682012-12-31 22:41:36 +00004746 // Silence warning for "clang -g foo.o -o foo"
4747 Args.ClaimAllArgs(options::OPT_g_Group);
4748 // and "clang -emit-llvm foo.o -o foo"
4749 Args.ClaimAllArgs(options::OPT_emit_llvm);
4750 // and for "clang -w foo.o -o foo". Other warning options are already
4751 // handled somewhere else.
4752 Args.ClaimAllArgs(options::OPT_w);
4753
Daniel Dunbar2bbcf662009-08-03 01:28:59 +00004754 if ((!Args.hasArg(options::OPT_nostdlib)) &&
Daniel Dunbar294691e2009-11-04 06:24:38 +00004755 (!Args.hasArg(options::OPT_shared))) {
Daniel Dunbar2bbcf662009-08-03 01:28:59 +00004756 CmdArgs.push_back("-e");
4757 CmdArgs.push_back("__start");
4758 }
4759
Daniel Dunbarf7b8eec2009-06-29 20:52:51 +00004760 if (Args.hasArg(options::OPT_static)) {
4761 CmdArgs.push_back("-Bstatic");
4762 } else {
Rafael Espindola65ba55d2010-11-11 02:17:51 +00004763 if (Args.hasArg(options::OPT_rdynamic))
4764 CmdArgs.push_back("-export-dynamic");
Daniel Dunbarf7b8eec2009-06-29 20:52:51 +00004765 CmdArgs.push_back("--eh-frame-hdr");
Daniel Dunbar2bbcf662009-08-03 01:28:59 +00004766 CmdArgs.push_back("-Bdynamic");
Daniel Dunbarf7b8eec2009-06-29 20:52:51 +00004767 if (Args.hasArg(options::OPT_shared)) {
Daniel Dunbar2bbcf662009-08-03 01:28:59 +00004768 CmdArgs.push_back("-shared");
Daniel Dunbarf7b8eec2009-06-29 20:52:51 +00004769 } else {
4770 CmdArgs.push_back("-dynamic-linker");
4771 CmdArgs.push_back("/usr/libexec/ld.so");
4772 }
4773 }
4774
Daniel Dunbar7c1e4652010-08-02 02:38:21 +00004775 if (Output.isFilename()) {
Daniel Dunbarf7b8eec2009-06-29 20:52:51 +00004776 CmdArgs.push_back("-o");
4777 CmdArgs.push_back(Output.getFilename());
4778 } else {
4779 assert(Output.isNothing() && "Invalid output.");
4780 }
4781
4782 if (!Args.hasArg(options::OPT_nostdlib) &&
4783 !Args.hasArg(options::OPT_nostartfiles)) {
4784 if (!Args.hasArg(options::OPT_shared)) {
Eli Friedman62d829a2011-12-15 02:15:56 +00004785 if (Args.hasArg(options::OPT_pg))
4786 CmdArgs.push_back(Args.MakeArgString(
4787 getToolChain().GetFilePath("gcrt0.o")));
4788 else
4789 CmdArgs.push_back(Args.MakeArgString(
4790 getToolChain().GetFilePath("crt0.o")));
Chris Lattner38e317d2010-07-07 16:01:42 +00004791 CmdArgs.push_back(Args.MakeArgString(
Daniel Dunbar4a7e8892010-07-14 18:46:23 +00004792 getToolChain().GetFilePath("crtbegin.o")));
Daniel Dunbarf7b8eec2009-06-29 20:52:51 +00004793 } else {
Chris Lattner38e317d2010-07-07 16:01:42 +00004794 CmdArgs.push_back(Args.MakeArgString(
Daniel Dunbar4a7e8892010-07-14 18:46:23 +00004795 getToolChain().GetFilePath("crtbeginS.o")));
Daniel Dunbarf7b8eec2009-06-29 20:52:51 +00004796 }
4797 }
4798
Edward O'Callaghane7e18202009-10-28 15:13:08 +00004799 std::string Triple = getToolChain().getTripleString();
4800 if (Triple.substr(0, 6) == "x86_64")
Daniel Dunbar294691e2009-11-04 06:24:38 +00004801 Triple.replace(0, 6, "amd64");
Daniel Dunbarf7fb31f2009-10-29 02:24:37 +00004802 CmdArgs.push_back(Args.MakeArgString("-L/usr/lib/gcc-lib/" + Triple +
Daniel Dunbar95c04572010-08-01 23:13:54 +00004803 "/4.2.1"));
Daniel Dunbar2bbcf662009-08-03 01:28:59 +00004804
Daniel Dunbarf7b8eec2009-06-29 20:52:51 +00004805 Args.AddAllArgs(CmdArgs, options::OPT_L);
4806 Args.AddAllArgs(CmdArgs, options::OPT_T_Group);
4807 Args.AddAllArgs(CmdArgs, options::OPT_e);
Rafael Espindola6cc2a682012-12-31 22:41:36 +00004808 Args.AddAllArgs(CmdArgs, options::OPT_s);
4809 Args.AddAllArgs(CmdArgs, options::OPT_t);
4810 Args.AddAllArgs(CmdArgs, options::OPT_Z_Flag);
4811 Args.AddAllArgs(CmdArgs, options::OPT_r);
Daniel Dunbarf7b8eec2009-06-29 20:52:51 +00004812
Daniel Dunbar2008fee2010-09-17 00:24:54 +00004813 AddLinkerInputs(getToolChain(), Inputs, Args, CmdArgs);
Daniel Dunbarf7b8eec2009-06-29 20:52:51 +00004814
4815 if (!Args.hasArg(options::OPT_nostdlib) &&
4816 !Args.hasArg(options::OPT_nodefaultlibs)) {
Daniel Dunbar95c04572010-08-01 23:13:54 +00004817 if (D.CCCIsCXX) {
Daniel Dunbar132e35d2010-09-17 01:20:05 +00004818 getToolChain().AddCXXStdlibLibArgs(Args, CmdArgs);
Eli Friedman62d829a2011-12-15 02:15:56 +00004819 if (Args.hasArg(options::OPT_pg))
4820 CmdArgs.push_back("-lm_p");
4821 else
4822 CmdArgs.push_back("-lm");
Daniel Dunbar95c04572010-08-01 23:13:54 +00004823 }
4824
Daniel Dunbar2bbcf662009-08-03 01:28:59 +00004825 // FIXME: For some reason GCC passes -lgcc before adding
4826 // the default system libraries. Just mimic this for now.
4827 CmdArgs.push_back("-lgcc");
Daniel Dunbarf7b8eec2009-06-29 20:52:51 +00004828
Eric Christopherdc6cc872012-09-13 06:32:34 +00004829 if (Args.hasArg(options::OPT_pthread)) {
4830 if (!Args.hasArg(options::OPT_shared) &&
4831 Args.hasArg(options::OPT_pg))
4832 CmdArgs.push_back("-lpthread_p");
4833 else
4834 CmdArgs.push_back("-lpthread");
4835 }
4836
Chandler Carruth657849c2011-12-17 22:32:42 +00004837 if (!Args.hasArg(options::OPT_shared)) {
Eric Christopherdc6cc872012-09-13 06:32:34 +00004838 if (Args.hasArg(options::OPT_pg))
Eli Friedman62d829a2011-12-15 02:15:56 +00004839 CmdArgs.push_back("-lc_p");
4840 else
4841 CmdArgs.push_back("-lc");
Chandler Carruth657849c2011-12-17 22:32:42 +00004842 }
Eric Christopherdc6cc872012-09-13 06:32:34 +00004843
Daniel Dunbar2bbcf662009-08-03 01:28:59 +00004844 CmdArgs.push_back("-lgcc");
Daniel Dunbarf7b8eec2009-06-29 20:52:51 +00004845 }
4846
4847 if (!Args.hasArg(options::OPT_nostdlib) &&
4848 !Args.hasArg(options::OPT_nostartfiles)) {
4849 if (!Args.hasArg(options::OPT_shared))
Chris Lattner38e317d2010-07-07 16:01:42 +00004850 CmdArgs.push_back(Args.MakeArgString(
Daniel Dunbar4a7e8892010-07-14 18:46:23 +00004851 getToolChain().GetFilePath("crtend.o")));
Daniel Dunbarf7b8eec2009-06-29 20:52:51 +00004852 else
Chris Lattner38e317d2010-07-07 16:01:42 +00004853 CmdArgs.push_back(Args.MakeArgString(
Daniel Dunbar4a7e8892010-07-14 18:46:23 +00004854 getToolChain().GetFilePath("crtendS.o")));
Daniel Dunbarf7b8eec2009-06-29 20:52:51 +00004855 }
4856
4857 const char *Exec =
Daniel Dunbar4a7e8892010-07-14 18:46:23 +00004858 Args.MakeArgString(getToolChain().GetProgramPath("ld"));
Daniel Dunbar2fe238e2010-08-02 02:38:28 +00004859 C.addCommand(new Command(JA, *this, Exec, CmdArgs));
Daniel Dunbarf7b8eec2009-06-29 20:52:51 +00004860}
Ed Schoutenc66a5a32009-04-02 19:13:12 +00004861
Eli Friedman42f74f22012-08-08 23:57:20 +00004862void bitrig::Assemble::ConstructJob(Compilation &C, const JobAction &JA,
4863 const InputInfo &Output,
4864 const InputInfoList &Inputs,
4865 const ArgList &Args,
4866 const char *LinkingOutput) const {
4867 ArgStringList CmdArgs;
4868
4869 Args.AddAllArgValues(CmdArgs, options::OPT_Wa_COMMA,
4870 options::OPT_Xassembler);
4871
4872 CmdArgs.push_back("-o");
4873 CmdArgs.push_back(Output.getFilename());
4874
4875 for (InputInfoList::const_iterator
4876 it = Inputs.begin(), ie = Inputs.end(); it != ie; ++it) {
4877 const InputInfo &II = *it;
4878 CmdArgs.push_back(II.getFilename());
4879 }
4880
4881 const char *Exec =
4882 Args.MakeArgString(getToolChain().GetProgramPath("as"));
4883 C.addCommand(new Command(JA, *this, Exec, CmdArgs));
4884}
4885
4886void bitrig::Link::ConstructJob(Compilation &C, const JobAction &JA,
4887 const InputInfo &Output,
4888 const InputInfoList &Inputs,
4889 const ArgList &Args,
4890 const char *LinkingOutput) const {
4891 const Driver &D = getToolChain().getDriver();
4892 ArgStringList CmdArgs;
4893
4894 if ((!Args.hasArg(options::OPT_nostdlib)) &&
4895 (!Args.hasArg(options::OPT_shared))) {
4896 CmdArgs.push_back("-e");
4897 CmdArgs.push_back("__start");
4898 }
4899
4900 if (Args.hasArg(options::OPT_static)) {
4901 CmdArgs.push_back("-Bstatic");
4902 } else {
4903 if (Args.hasArg(options::OPT_rdynamic))
4904 CmdArgs.push_back("-export-dynamic");
4905 CmdArgs.push_back("--eh-frame-hdr");
4906 CmdArgs.push_back("-Bdynamic");
4907 if (Args.hasArg(options::OPT_shared)) {
4908 CmdArgs.push_back("-shared");
4909 } else {
4910 CmdArgs.push_back("-dynamic-linker");
4911 CmdArgs.push_back("/usr/libexec/ld.so");
4912 }
4913 }
4914
4915 if (Output.isFilename()) {
4916 CmdArgs.push_back("-o");
4917 CmdArgs.push_back(Output.getFilename());
4918 } else {
4919 assert(Output.isNothing() && "Invalid output.");
4920 }
4921
4922 if (!Args.hasArg(options::OPT_nostdlib) &&
4923 !Args.hasArg(options::OPT_nostartfiles)) {
4924 if (!Args.hasArg(options::OPT_shared)) {
4925 if (Args.hasArg(options::OPT_pg))
4926 CmdArgs.push_back(Args.MakeArgString(
4927 getToolChain().GetFilePath("gcrt0.o")));
4928 else
4929 CmdArgs.push_back(Args.MakeArgString(
4930 getToolChain().GetFilePath("crt0.o")));
4931 CmdArgs.push_back(Args.MakeArgString(
4932 getToolChain().GetFilePath("crtbegin.o")));
4933 } else {
4934 CmdArgs.push_back(Args.MakeArgString(
4935 getToolChain().GetFilePath("crtbeginS.o")));
4936 }
4937 }
4938
4939 Args.AddAllArgs(CmdArgs, options::OPT_L);
4940 Args.AddAllArgs(CmdArgs, options::OPT_T_Group);
4941 Args.AddAllArgs(CmdArgs, options::OPT_e);
4942
4943 AddLinkerInputs(getToolChain(), Inputs, Args, CmdArgs);
4944
4945 if (!Args.hasArg(options::OPT_nostdlib) &&
4946 !Args.hasArg(options::OPT_nodefaultlibs)) {
4947 if (D.CCCIsCXX) {
4948 getToolChain().AddCXXStdlibLibArgs(Args, CmdArgs);
4949 if (Args.hasArg(options::OPT_pg))
4950 CmdArgs.push_back("-lm_p");
4951 else
4952 CmdArgs.push_back("-lm");
4953 }
4954
Rafael Espindola3667bbe2012-10-23 17:07:31 +00004955 if (Args.hasArg(options::OPT_pthread)) {
4956 if (!Args.hasArg(options::OPT_shared) &&
4957 Args.hasArg(options::OPT_pg))
4958 CmdArgs.push_back("-lpthread_p");
4959 else
4960 CmdArgs.push_back("-lpthread");
4961 }
4962
Eli Friedman42f74f22012-08-08 23:57:20 +00004963 if (!Args.hasArg(options::OPT_shared)) {
4964 if (Args.hasArg(options::OPT_pg))
4965 CmdArgs.push_back("-lc_p");
4966 else
4967 CmdArgs.push_back("-lc");
4968 }
4969
4970 std::string myarch = "-lclang_rt.";
4971 const llvm::Triple &T = getToolChain().getTriple();
4972 llvm::Triple::ArchType Arch = T.getArch();
4973 switch (Arch) {
4974 case llvm::Triple::arm:
4975 myarch += ("arm");
4976 break;
4977 case llvm::Triple::x86:
4978 myarch += ("i386");
4979 break;
4980 case llvm::Triple::x86_64:
4981 myarch += ("amd64");
4982 break;
4983 default:
4984 assert(0 && "Unsupported architecture");
4985 }
4986 CmdArgs.push_back(Args.MakeArgString(myarch));
4987 }
4988
4989 if (!Args.hasArg(options::OPT_nostdlib) &&
4990 !Args.hasArg(options::OPT_nostartfiles)) {
4991 if (!Args.hasArg(options::OPT_shared))
4992 CmdArgs.push_back(Args.MakeArgString(
4993 getToolChain().GetFilePath("crtend.o")));
4994 else
4995 CmdArgs.push_back(Args.MakeArgString(
4996 getToolChain().GetFilePath("crtendS.o")));
4997 }
Eli Friedmanc9c48db2012-08-09 22:42:04 +00004998
4999 const char *Exec =
5000 Args.MakeArgString(getToolChain().GetProgramPath("ld"));
5001 C.addCommand(new Command(JA, *this, Exec, CmdArgs));
Eli Friedman42f74f22012-08-08 23:57:20 +00005002}
5003
Daniel Dunbar68a31d42009-03-31 17:45:15 +00005004void freebsd::Assemble::ConstructJob(Compilation &C, const JobAction &JA,
Daniel Dunbar2fe238e2010-08-02 02:38:28 +00005005 const InputInfo &Output,
Daniel Dunbarc21c4852009-04-08 23:54:23 +00005006 const InputInfoList &Inputs,
5007 const ArgList &Args,
Mike Stump1eb44332009-09-09 15:08:12 +00005008 const char *LinkingOutput) const {
Daniel Dunbar68a31d42009-03-31 17:45:15 +00005009 ArgStringList CmdArgs;
5010
Daniel Dunbar008f54a2009-04-01 19:36:32 +00005011 // When building 32-bit code on FreeBSD/amd64, we have to explicitly
5012 // instruct as in the base system to assemble 32-bit code.
Eric Christopherc55da4b2012-09-05 21:32:44 +00005013 if (getToolChain().getArch() == llvm::Triple::x86)
Daniel Dunbar68a31d42009-03-31 17:45:15 +00005014 CmdArgs.push_back("--32");
Eric Christopherc55da4b2012-09-05 21:32:44 +00005015 else if (getToolChain().getArch() == llvm::Triple::ppc)
Roman Divacky3393cef2011-06-04 07:37:31 +00005016 CmdArgs.push_back("-a32");
Eric Christopherc55da4b2012-09-05 21:32:44 +00005017 else if (getToolChain().getArch() == llvm::Triple::mips ||
5018 getToolChain().getArch() == llvm::Triple::mipsel ||
5019 getToolChain().getArch() == llvm::Triple::mips64 ||
5020 getToolChain().getArch() == llvm::Triple::mips64el) {
5021 StringRef CPUName;
5022 StringRef ABIName;
5023 getMipsCPUAndABI(Args, getToolChain(), CPUName, ABIName);
Michael J. Spencer20249a12010-10-21 03:16:25 +00005024
Eric Christopherc55da4b2012-09-05 21:32:44 +00005025 CmdArgs.push_back("-march");
5026 CmdArgs.push_back(CPUName.data());
5027
5028 // Convert ABI name to the GNU tools acceptable variant.
5029 if (ABIName == "o32")
5030 ABIName = "32";
5031 else if (ABIName == "n64")
5032 ABIName = "64";
5033
5034 CmdArgs.push_back("-mabi");
5035 CmdArgs.push_back(ABIName.data());
5036
5037 if (getToolChain().getArch() == llvm::Triple::mips ||
5038 getToolChain().getArch() == llvm::Triple::mips64)
5039 CmdArgs.push_back("-EB");
5040 else
5041 CmdArgs.push_back("-EL");
5042
5043 Arg *LastPICArg = Args.getLastArg(options::OPT_fPIC, options::OPT_fno_PIC,
5044 options::OPT_fpic, options::OPT_fno_pic,
5045 options::OPT_fPIE, options::OPT_fno_PIE,
5046 options::OPT_fpie, options::OPT_fno_pie);
5047 if (LastPICArg &&
5048 (LastPICArg->getOption().matches(options::OPT_fPIC) ||
5049 LastPICArg->getOption().matches(options::OPT_fpic) ||
5050 LastPICArg->getOption().matches(options::OPT_fPIE) ||
5051 LastPICArg->getOption().matches(options::OPT_fpie))) {
5052 CmdArgs.push_back("-KPIC");
5053 }
Rafael Espindola27fa2362012-12-13 04:17:14 +00005054 } else if (getToolChain().getArch() == llvm::Triple::arm ||
5055 getToolChain().getArch() == llvm::Triple::thumb) {
5056 CmdArgs.push_back("-mfpu=softvfp");
5057 switch(getToolChain().getTriple().getEnvironment()) {
5058 case llvm::Triple::GNUEABI:
5059 case llvm::Triple::EABI:
5060 break;
5061
5062 default:
5063 CmdArgs.push_back("-matpcs");
5064 }
Eric Christopherc55da4b2012-09-05 21:32:44 +00005065 }
Eric Christophered734732010-03-02 02:41:08 +00005066
Daniel Dunbar68a31d42009-03-31 17:45:15 +00005067 Args.AddAllArgValues(CmdArgs, options::OPT_Wa_COMMA,
5068 options::OPT_Xassembler);
5069
5070 CmdArgs.push_back("-o");
Daniel Dunbar7c1e4652010-08-02 02:38:21 +00005071 CmdArgs.push_back(Output.getFilename());
Daniel Dunbar68a31d42009-03-31 17:45:15 +00005072
5073 for (InputInfoList::const_iterator
5074 it = Inputs.begin(), ie = Inputs.end(); it != ie; ++it) {
5075 const InputInfo &II = *it;
Daniel Dunbar7c1e4652010-08-02 02:38:21 +00005076 CmdArgs.push_back(II.getFilename());
Daniel Dunbar68a31d42009-03-31 17:45:15 +00005077 }
5078
Daniel Dunbarc21c4852009-04-08 23:54:23 +00005079 const char *Exec =
Daniel Dunbar4a7e8892010-07-14 18:46:23 +00005080 Args.MakeArgString(getToolChain().GetProgramPath("as"));
Daniel Dunbar2fe238e2010-08-02 02:38:28 +00005081 C.addCommand(new Command(JA, *this, Exec, CmdArgs));
Daniel Dunbar68a31d42009-03-31 17:45:15 +00005082}
Daniel Dunbar008f54a2009-04-01 19:36:32 +00005083
5084void freebsd::Link::ConstructJob(Compilation &C, const JobAction &JA,
Daniel Dunbar2fe238e2010-08-02 02:38:28 +00005085 const InputInfo &Output,
Daniel Dunbarc21c4852009-04-08 23:54:23 +00005086 const InputInfoList &Inputs,
5087 const ArgList &Args,
Daniel Dunbara8304f62009-05-02 20:14:53 +00005088 const char *LinkingOutput) const {
Roman Divacky94380162012-08-28 15:09:03 +00005089 const toolchains::FreeBSD& ToolChain =
5090 static_cast<const toolchains::FreeBSD&>(getToolChain());
5091 const Driver &D = ToolChain.getDriver();
Daniel Dunbar008f54a2009-04-01 19:36:32 +00005092 ArgStringList CmdArgs;
David Chisnalldfa210b2012-07-29 15:24:44 +00005093
5094 // Silence warning for "clang -g foo.o -o foo"
5095 Args.ClaimAllArgs(options::OPT_g_Group);
5096 // and "clang -emit-llvm foo.o -o foo"
5097 Args.ClaimAllArgs(options::OPT_emit_llvm);
5098 // and for "clang -w foo.o -o foo". Other warning options are already
5099 // handled somewhere else.
5100 Args.ClaimAllArgs(options::OPT_w);
Daniel Dunbar008f54a2009-04-01 19:36:32 +00005101
Joerg Sonnenberger8ab2bdc2011-03-21 13:51:29 +00005102 if (!D.SysRoot.empty())
5103 CmdArgs.push_back(Args.MakeArgString("--sysroot=" + D.SysRoot));
5104
Roman Divacky94380162012-08-28 15:09:03 +00005105 if (Args.hasArg(options::OPT_pie))
5106 CmdArgs.push_back("-pie");
5107
Daniel Dunbar008f54a2009-04-01 19:36:32 +00005108 if (Args.hasArg(options::OPT_static)) {
5109 CmdArgs.push_back("-Bstatic");
5110 } else {
Rafael Espindola65ba55d2010-11-11 02:17:51 +00005111 if (Args.hasArg(options::OPT_rdynamic))
5112 CmdArgs.push_back("-export-dynamic");
Daniel Dunbar008f54a2009-04-01 19:36:32 +00005113 CmdArgs.push_back("--eh-frame-hdr");
5114 if (Args.hasArg(options::OPT_shared)) {
5115 CmdArgs.push_back("-Bshareable");
5116 } else {
5117 CmdArgs.push_back("-dynamic-linker");
5118 CmdArgs.push_back("/libexec/ld-elf.so.1");
5119 }
Roman Divacky94380162012-08-28 15:09:03 +00005120 if (ToolChain.getTriple().getOSMajorVersion() >= 9) {
5121 llvm::Triple::ArchType Arch = ToolChain.getArch();
David Chisnalldfa210b2012-07-29 15:24:44 +00005122 if (Arch == llvm::Triple::arm || Arch == llvm::Triple::sparc ||
5123 Arch == llvm::Triple::x86 || Arch == llvm::Triple::x86_64) {
5124 CmdArgs.push_back("--hash-style=both");
5125 }
5126 }
5127 CmdArgs.push_back("--enable-new-dtags");
Daniel Dunbar008f54a2009-04-01 19:36:32 +00005128 }
5129
5130 // When building 32-bit code on FreeBSD/amd64, we have to explicitly
5131 // instruct ld in the base system to link 32-bit code.
Rafael Espindola64f7ad92012-10-07 04:44:33 +00005132 if (ToolChain.getArch() == llvm::Triple::x86) {
Daniel Dunbar008f54a2009-04-01 19:36:32 +00005133 CmdArgs.push_back("-m");
5134 CmdArgs.push_back("elf_i386_fbsd");
5135 }
5136
Rafael Espindola64f7ad92012-10-07 04:44:33 +00005137 if (ToolChain.getArch() == llvm::Triple::ppc) {
Roman Divacky000a6552011-06-04 07:40:24 +00005138 CmdArgs.push_back("-m");
Roman Divacky1052c1d2011-11-21 16:50:32 +00005139 CmdArgs.push_back("elf32ppc_fbsd");
Roman Divacky000a6552011-06-04 07:40:24 +00005140 }
5141
Daniel Dunbar7c1e4652010-08-02 02:38:21 +00005142 if (Output.isFilename()) {
Daniel Dunbar008f54a2009-04-01 19:36:32 +00005143 CmdArgs.push_back("-o");
5144 CmdArgs.push_back(Output.getFilename());
5145 } else {
5146 assert(Output.isNothing() && "Invalid output.");
5147 }
5148
5149 if (!Args.hasArg(options::OPT_nostdlib) &&
5150 !Args.hasArg(options::OPT_nostartfiles)) {
Roman Divacky94380162012-08-28 15:09:03 +00005151 const char *crt1 = NULL;
Daniel Dunbar008f54a2009-04-01 19:36:32 +00005152 if (!Args.hasArg(options::OPT_shared)) {
Roman Divackyc16bb762011-02-10 16:59:40 +00005153 if (Args.hasArg(options::OPT_pg))
Roman Divacky94380162012-08-28 15:09:03 +00005154 crt1 = "gcrt1.o";
5155 else if (Args.hasArg(options::OPT_pie))
5156 crt1 = "Scrt1.o";
5157 else
5158 crt1 = "crt1.o";
Daniel Dunbar008f54a2009-04-01 19:36:32 +00005159 }
Roman Divacky94380162012-08-28 15:09:03 +00005160 if (crt1)
5161 CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath(crt1)));
5162
5163 CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath("crti.o")));
5164
5165 const char *crtbegin = NULL;
5166 if (Args.hasArg(options::OPT_static))
5167 crtbegin = "crtbeginT.o";
5168 else if (Args.hasArg(options::OPT_shared) || Args.hasArg(options::OPT_pie))
5169 crtbegin = "crtbeginS.o";
5170 else
5171 crtbegin = "crtbegin.o";
5172
5173 CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath(crtbegin)));
Daniel Dunbar008f54a2009-04-01 19:36:32 +00005174 }
5175
5176 Args.AddAllArgs(CmdArgs, options::OPT_L);
Roman Divacky94380162012-08-28 15:09:03 +00005177 const ToolChain::path_list Paths = ToolChain.getFilePaths();
Roman Divacky58e5ac92011-03-01 17:53:14 +00005178 for (ToolChain::path_list::const_iterator i = Paths.begin(), e = Paths.end();
5179 i != e; ++i)
Chris Lattner5f9e2722011-07-23 10:55:15 +00005180 CmdArgs.push_back(Args.MakeArgString(StringRef("-L") + *i));
Daniel Dunbar008f54a2009-04-01 19:36:32 +00005181 Args.AddAllArgs(CmdArgs, options::OPT_T_Group);
5182 Args.AddAllArgs(CmdArgs, options::OPT_e);
David Chisnallc7363772010-08-15 22:58:12 +00005183 Args.AddAllArgs(CmdArgs, options::OPT_s);
5184 Args.AddAllArgs(CmdArgs, options::OPT_t);
5185 Args.AddAllArgs(CmdArgs, options::OPT_Z_Flag);
5186 Args.AddAllArgs(CmdArgs, options::OPT_r);
Daniel Dunbar008f54a2009-04-01 19:36:32 +00005187
Roman Divacky94380162012-08-28 15:09:03 +00005188 AddLinkerInputs(ToolChain, Inputs, Args, CmdArgs);
Daniel Dunbar008f54a2009-04-01 19:36:32 +00005189
5190 if (!Args.hasArg(options::OPT_nostdlib) &&
5191 !Args.hasArg(options::OPT_nodefaultlibs)) {
Daniel Dunbar20022632010-02-17 08:07:51 +00005192 if (D.CCCIsCXX) {
Roman Divacky94380162012-08-28 15:09:03 +00005193 ToolChain.AddCXXStdlibLibArgs(Args, CmdArgs);
Roman Divackyc16bb762011-02-10 16:59:40 +00005194 if (Args.hasArg(options::OPT_pg))
5195 CmdArgs.push_back("-lm_p");
5196 else
5197 CmdArgs.push_back("-lm");
Daniel Dunbar20022632010-02-17 08:07:51 +00005198 }
Daniel Dunbar008f54a2009-04-01 19:36:32 +00005199 // FIXME: For some reason GCC passes -lgcc and -lgcc_s before adding
5200 // the default system libraries. Just mimic this for now.
Roman Divackyc16bb762011-02-10 16:59:40 +00005201 if (Args.hasArg(options::OPT_pg))
5202 CmdArgs.push_back("-lgcc_p");
5203 else
5204 CmdArgs.push_back("-lgcc");
Daniel Dunbar008f54a2009-04-01 19:36:32 +00005205 if (Args.hasArg(options::OPT_static)) {
5206 CmdArgs.push_back("-lgcc_eh");
Roman Divackyc16bb762011-02-10 16:59:40 +00005207 } else if (Args.hasArg(options::OPT_pg)) {
5208 CmdArgs.push_back("-lgcc_eh_p");
Daniel Dunbar008f54a2009-04-01 19:36:32 +00005209 } else {
5210 CmdArgs.push_back("--as-needed");
5211 CmdArgs.push_back("-lgcc_s");
5212 CmdArgs.push_back("--no-as-needed");
5213 }
5214
Matt Beaumont-Gay24230262011-02-10 20:35:01 +00005215 if (Args.hasArg(options::OPT_pthread)) {
Roman Divackyc16bb762011-02-10 16:59:40 +00005216 if (Args.hasArg(options::OPT_pg))
5217 CmdArgs.push_back("-lpthread_p");
5218 else
5219 CmdArgs.push_back("-lpthread");
Matt Beaumont-Gay24230262011-02-10 20:35:01 +00005220 }
Daniel Dunbar008f54a2009-04-01 19:36:32 +00005221
Roman Divackyc16bb762011-02-10 16:59:40 +00005222 if (Args.hasArg(options::OPT_pg)) {
5223 if (Args.hasArg(options::OPT_shared))
5224 CmdArgs.push_back("-lc");
5225 else
5226 CmdArgs.push_back("-lc_p");
5227 CmdArgs.push_back("-lgcc_p");
5228 } else {
5229 CmdArgs.push_back("-lc");
5230 CmdArgs.push_back("-lgcc");
5231 }
5232
Daniel Dunbar008f54a2009-04-01 19:36:32 +00005233 if (Args.hasArg(options::OPT_static)) {
5234 CmdArgs.push_back("-lgcc_eh");
Roman Divackyc16bb762011-02-10 16:59:40 +00005235 } else if (Args.hasArg(options::OPT_pg)) {
5236 CmdArgs.push_back("-lgcc_eh_p");
Daniel Dunbar008f54a2009-04-01 19:36:32 +00005237 } else {
5238 CmdArgs.push_back("--as-needed");
5239 CmdArgs.push_back("-lgcc_s");
5240 CmdArgs.push_back("--no-as-needed");
5241 }
5242 }
5243
5244 if (!Args.hasArg(options::OPT_nostdlib) &&
5245 !Args.hasArg(options::OPT_nostartfiles)) {
Roman Divackyf6513812012-09-07 13:36:21 +00005246 if (Args.hasArg(options::OPT_shared) || Args.hasArg(options::OPT_pie))
Roman Divacky94380162012-08-28 15:09:03 +00005247 CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath("crtendS.o")));
Roman Divackyf6513812012-09-07 13:36:21 +00005248 else
5249 CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath("crtend.o")));
Roman Divacky94380162012-08-28 15:09:03 +00005250 CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath("crtn.o")));
Daniel Dunbar008f54a2009-04-01 19:36:32 +00005251 }
5252
Roman Divacky94380162012-08-28 15:09:03 +00005253 addProfileRT(ToolChain, Args, CmdArgs, ToolChain.getTriple());
Nick Lewycky2e95a6d2011-05-24 21:54:59 +00005254
Daniel Dunbarc21c4852009-04-08 23:54:23 +00005255 const char *Exec =
Roman Divacky94380162012-08-28 15:09:03 +00005256 Args.MakeArgString(ToolChain.GetProgramPath("ld"));
Daniel Dunbar2fe238e2010-08-02 02:38:28 +00005257 C.addCommand(new Command(JA, *this, Exec, CmdArgs));
Daniel Dunbar008f54a2009-04-01 19:36:32 +00005258}
Daniel Dunbar11e1b402009-05-02 18:28:39 +00005259
Benjamin Kramer8e50a962011-02-02 18:59:27 +00005260void netbsd::Assemble::ConstructJob(Compilation &C, const JobAction &JA,
5261 const InputInfo &Output,
5262 const InputInfoList &Inputs,
5263 const ArgList &Args,
5264 const char *LinkingOutput) const {
5265 ArgStringList CmdArgs;
5266
5267 // When building 32-bit code on NetBSD/amd64, we have to explicitly
5268 // instruct as in the base system to assemble 32-bit code.
Joerg Sonnenberger1bd91372012-01-26 22:27:52 +00005269 if (getToolChain().getArch() == llvm::Triple::x86)
Benjamin Kramer8e50a962011-02-02 18:59:27 +00005270 CmdArgs.push_back("--32");
5271
Benjamin Kramer8e50a962011-02-02 18:59:27 +00005272 // Set byte order explicitly
Rafael Espindola64f7ad92012-10-07 04:44:33 +00005273 if (getToolChain().getArch() == llvm::Triple::mips)
Benjamin Kramer8e50a962011-02-02 18:59:27 +00005274 CmdArgs.push_back("-EB");
Rafael Espindola64f7ad92012-10-07 04:44:33 +00005275 else if (getToolChain().getArch() == llvm::Triple::mipsel)
Benjamin Kramer8e50a962011-02-02 18:59:27 +00005276 CmdArgs.push_back("-EL");
5277
5278 Args.AddAllArgValues(CmdArgs, options::OPT_Wa_COMMA,
5279 options::OPT_Xassembler);
5280
5281 CmdArgs.push_back("-o");
5282 CmdArgs.push_back(Output.getFilename());
5283
5284 for (InputInfoList::const_iterator
5285 it = Inputs.begin(), ie = Inputs.end(); it != ie; ++it) {
5286 const InputInfo &II = *it;
5287 CmdArgs.push_back(II.getFilename());
5288 }
5289
David Chisnall5adcec12011-09-27 22:03:18 +00005290 const char *Exec = Args.MakeArgString((getToolChain().GetProgramPath("as")));
Benjamin Kramer8e50a962011-02-02 18:59:27 +00005291 C.addCommand(new Command(JA, *this, Exec, CmdArgs));
5292}
5293
5294void netbsd::Link::ConstructJob(Compilation &C, const JobAction &JA,
5295 const InputInfo &Output,
5296 const InputInfoList &Inputs,
5297 const ArgList &Args,
5298 const char *LinkingOutput) const {
5299 const Driver &D = getToolChain().getDriver();
5300 ArgStringList CmdArgs;
5301
Joerg Sonnenberger8ab2bdc2011-03-21 13:51:29 +00005302 if (!D.SysRoot.empty())
5303 CmdArgs.push_back(Args.MakeArgString("--sysroot=" + D.SysRoot));
5304
Benjamin Kramer8e50a962011-02-02 18:59:27 +00005305 if (Args.hasArg(options::OPT_static)) {
5306 CmdArgs.push_back("-Bstatic");
5307 } else {
5308 if (Args.hasArg(options::OPT_rdynamic))
5309 CmdArgs.push_back("-export-dynamic");
5310 CmdArgs.push_back("--eh-frame-hdr");
5311 if (Args.hasArg(options::OPT_shared)) {
5312 CmdArgs.push_back("-Bshareable");
5313 } else {
5314 CmdArgs.push_back("-dynamic-linker");
5315 CmdArgs.push_back("/libexec/ld.elf_so");
5316 }
5317 }
5318
5319 // When building 32-bit code on NetBSD/amd64, we have to explicitly
5320 // instruct ld in the base system to link 32-bit code.
Joerg Sonnenberger1bd91372012-01-26 22:27:52 +00005321 if (getToolChain().getArch() == llvm::Triple::x86) {
Benjamin Kramer8e50a962011-02-02 18:59:27 +00005322 CmdArgs.push_back("-m");
5323 CmdArgs.push_back("elf_i386");
5324 }
5325
5326 if (Output.isFilename()) {
5327 CmdArgs.push_back("-o");
5328 CmdArgs.push_back(Output.getFilename());
5329 } else {
5330 assert(Output.isNothing() && "Invalid output.");
5331 }
5332
5333 if (!Args.hasArg(options::OPT_nostdlib) &&
5334 !Args.hasArg(options::OPT_nostartfiles)) {
5335 if (!Args.hasArg(options::OPT_shared)) {
5336 CmdArgs.push_back(Args.MakeArgString(
5337 getToolChain().GetFilePath("crt0.o")));
5338 CmdArgs.push_back(Args.MakeArgString(
5339 getToolChain().GetFilePath("crti.o")));
5340 CmdArgs.push_back(Args.MakeArgString(
5341 getToolChain().GetFilePath("crtbegin.o")));
5342 } else {
5343 CmdArgs.push_back(Args.MakeArgString(
5344 getToolChain().GetFilePath("crti.o")));
5345 CmdArgs.push_back(Args.MakeArgString(
5346 getToolChain().GetFilePath("crtbeginS.o")));
5347 }
5348 }
5349
5350 Args.AddAllArgs(CmdArgs, options::OPT_L);
5351 Args.AddAllArgs(CmdArgs, options::OPT_T_Group);
5352 Args.AddAllArgs(CmdArgs, options::OPT_e);
5353 Args.AddAllArgs(CmdArgs, options::OPT_s);
5354 Args.AddAllArgs(CmdArgs, options::OPT_t);
5355 Args.AddAllArgs(CmdArgs, options::OPT_Z_Flag);
5356 Args.AddAllArgs(CmdArgs, options::OPT_r);
5357
5358 AddLinkerInputs(getToolChain(), Inputs, Args, CmdArgs);
5359
5360 if (!Args.hasArg(options::OPT_nostdlib) &&
5361 !Args.hasArg(options::OPT_nodefaultlibs)) {
5362 if (D.CCCIsCXX) {
5363 getToolChain().AddCXXStdlibLibArgs(Args, CmdArgs);
5364 CmdArgs.push_back("-lm");
5365 }
5366 // FIXME: For some reason GCC passes -lgcc and -lgcc_s before adding
5367 // the default system libraries. Just mimic this for now.
Benjamin Kramer8e50a962011-02-02 18:59:27 +00005368 if (Args.hasArg(options::OPT_static)) {
5369 CmdArgs.push_back("-lgcc_eh");
5370 } else {
5371 CmdArgs.push_back("--as-needed");
5372 CmdArgs.push_back("-lgcc_s");
5373 CmdArgs.push_back("--no-as-needed");
5374 }
Joerg Sonnenbergerdb6393f2011-06-07 23:39:17 +00005375 CmdArgs.push_back("-lgcc");
Benjamin Kramer8e50a962011-02-02 18:59:27 +00005376
5377 if (Args.hasArg(options::OPT_pthread))
5378 CmdArgs.push_back("-lpthread");
5379 CmdArgs.push_back("-lc");
5380
5381 CmdArgs.push_back("-lgcc");
5382 if (Args.hasArg(options::OPT_static)) {
5383 CmdArgs.push_back("-lgcc_eh");
5384 } else {
5385 CmdArgs.push_back("--as-needed");
5386 CmdArgs.push_back("-lgcc_s");
5387 CmdArgs.push_back("--no-as-needed");
5388 }
5389 }
5390
5391 if (!Args.hasArg(options::OPT_nostdlib) &&
5392 !Args.hasArg(options::OPT_nostartfiles)) {
5393 if (!Args.hasArg(options::OPT_shared))
5394 CmdArgs.push_back(Args.MakeArgString(getToolChain().GetFilePath(
5395 "crtend.o")));
5396 else
5397 CmdArgs.push_back(Args.MakeArgString(getToolChain().GetFilePath(
5398 "crtendS.o")));
5399 CmdArgs.push_back(Args.MakeArgString(getToolChain().GetFilePath(
5400 "crtn.o")));
5401 }
5402
Bill Wendling3f4be6f2011-06-27 19:15:03 +00005403 addProfileRT(getToolChain(), Args, CmdArgs, getToolChain().getTriple());
Nick Lewycky2e95a6d2011-05-24 21:54:59 +00005404
David Chisnall5adcec12011-09-27 22:03:18 +00005405 const char *Exec = Args.MakeArgString(getToolChain().GetProgramPath("ld"));
Benjamin Kramer8e50a962011-02-02 18:59:27 +00005406 C.addCommand(new Command(JA, *this, Exec, CmdArgs));
5407}
5408
Rafael Espindolaba30bbe2010-08-10 00:25:48 +00005409void linuxtools::Assemble::ConstructJob(Compilation &C, const JobAction &JA,
5410 const InputInfo &Output,
5411 const InputInfoList &Inputs,
5412 const ArgList &Args,
5413 const char *LinkingOutput) const {
5414 ArgStringList CmdArgs;
5415
5416 // Add --32/--64 to make sure we get the format we want.
5417 // This is incomplete
5418 if (getToolChain().getArch() == llvm::Triple::x86) {
5419 CmdArgs.push_back("--32");
5420 } else if (getToolChain().getArch() == llvm::Triple::x86_64) {
5421 CmdArgs.push_back("--64");
Eli Friedman7972c882011-11-28 23:46:52 +00005422 } else if (getToolChain().getArch() == llvm::Triple::ppc) {
5423 CmdArgs.push_back("-a32");
5424 CmdArgs.push_back("-mppc");
5425 CmdArgs.push_back("-many");
5426 } else if (getToolChain().getArch() == llvm::Triple::ppc64) {
5427 CmdArgs.push_back("-a64");
5428 CmdArgs.push_back("-mppc64");
5429 CmdArgs.push_back("-many");
Rafael Espindolaba30bbe2010-08-10 00:25:48 +00005430 } else if (getToolChain().getArch() == llvm::Triple::arm) {
Chris Lattner5f9e2722011-07-23 10:55:15 +00005431 StringRef MArch = getToolChain().getArchName();
Rafael Espindolaba30bbe2010-08-10 00:25:48 +00005432 if (MArch == "armv7" || MArch == "armv7a" || MArch == "armv7-a")
5433 CmdArgs.push_back("-mfpu=neon");
Evgeniy Stepanov700c5082012-04-20 09:03:40 +00005434
5435 StringRef ARMFloatABI = getARMFloatABI(getToolChain().getDriver(), Args,
5436 getToolChain().getTriple());
5437 CmdArgs.push_back(Args.MakeArgString("-mfloat-abi=" + ARMFloatABI));
Evgeniy Stepanoveca187e2012-04-24 09:05:31 +00005438
5439 Args.AddLastArg(CmdArgs, options::OPT_march_EQ);
5440 Args.AddLastArg(CmdArgs, options::OPT_mcpu_EQ);
5441 Args.AddLastArg(CmdArgs, options::OPT_mfpu_EQ);
Akira Hatanakac85900f2011-11-30 19:31:38 +00005442 } else if (getToolChain().getArch() == llvm::Triple::mips ||
5443 getToolChain().getArch() == llvm::Triple::mipsel ||
5444 getToolChain().getArch() == llvm::Triple::mips64 ||
5445 getToolChain().getArch() == llvm::Triple::mips64el) {
Simon Atanasyan073a7802012-04-07 22:31:29 +00005446 StringRef CPUName;
5447 StringRef ABIName;
5448 getMipsCPUAndABI(Args, getToolChain(), CPUName, ABIName);
Akira Hatanakac85900f2011-11-30 19:31:38 +00005449
Simon Atanasyan073a7802012-04-07 22:31:29 +00005450 CmdArgs.push_back("-march");
5451 CmdArgs.push_back(CPUName.data());
5452
5453 // Convert ABI name to the GNU tools acceptable variant.
5454 if (ABIName == "o32")
5455 ABIName = "32";
5456 else if (ABIName == "n64")
5457 ABIName = "64";
5458
5459 CmdArgs.push_back("-mabi");
5460 CmdArgs.push_back(ABIName.data());
Simon Atanasyan5f0a1c12012-04-06 19:15:24 +00005461
5462 if (getToolChain().getArch() == llvm::Triple::mips ||
5463 getToolChain().getArch() == llvm::Triple::mips64)
5464 CmdArgs.push_back("-EB");
5465 else
5466 CmdArgs.push_back("-EL");
Simon Atanasyan1f0646e2012-05-29 19:07:33 +00005467
5468 Arg *LastPICArg = Args.getLastArg(options::OPT_fPIC, options::OPT_fno_PIC,
5469 options::OPT_fpic, options::OPT_fno_pic,
5470 options::OPT_fPIE, options::OPT_fno_PIE,
5471 options::OPT_fpie, options::OPT_fno_pie);
5472 if (LastPICArg &&
5473 (LastPICArg->getOption().matches(options::OPT_fPIC) ||
5474 LastPICArg->getOption().matches(options::OPT_fpic) ||
5475 LastPICArg->getOption().matches(options::OPT_fPIE) ||
5476 LastPICArg->getOption().matches(options::OPT_fpie))) {
5477 CmdArgs.push_back("-KPIC");
5478 }
Rafael Espindolaba30bbe2010-08-10 00:25:48 +00005479 }
5480
5481 Args.AddAllArgValues(CmdArgs, options::OPT_Wa_COMMA,
5482 options::OPT_Xassembler);
5483
5484 CmdArgs.push_back("-o");
5485 CmdArgs.push_back(Output.getFilename());
5486
5487 for (InputInfoList::const_iterator
5488 it = Inputs.begin(), ie = Inputs.end(); it != ie; ++it) {
5489 const InputInfo &II = *it;
5490 CmdArgs.push_back(II.getFilename());
5491 }
5492
5493 const char *Exec =
5494 Args.MakeArgString(getToolChain().GetProgramPath("as"));
5495 C.addCommand(new Command(JA, *this, Exec, CmdArgs));
5496}
5497
Evgeniy Stepanova6ddc022012-04-25 08:59:22 +00005498static void AddLibgcc(llvm::Triple Triple, const Driver &D,
5499 ArgStringList &CmdArgs, const ArgList &Args) {
Logan Chien94a71422012-09-02 09:30:11 +00005500 bool isAndroid = Triple.getEnvironment() == llvm::Triple::Android;
Logan Chien529a73d2012-11-19 12:04:11 +00005501 bool StaticLibgcc = Args.hasArg(options::OPT_static) ||
5502 Args.hasArg(options::OPT_static_libgcc);
Rafael Espindolaabf3ac72011-10-17 21:39:04 +00005503 if (!D.CCCIsCXX)
5504 CmdArgs.push_back("-lgcc");
5505
Logan Chien529a73d2012-11-19 12:04:11 +00005506 if (StaticLibgcc || isAndroid) {
Rafael Espindolaabf3ac72011-10-17 21:39:04 +00005507 if (D.CCCIsCXX)
5508 CmdArgs.push_back("-lgcc");
5509 } else {
5510 if (!D.CCCIsCXX)
5511 CmdArgs.push_back("--as-needed");
5512 CmdArgs.push_back("-lgcc_s");
5513 if (!D.CCCIsCXX)
5514 CmdArgs.push_back("--no-as-needed");
5515 }
5516
Evgeniy Stepanova6ddc022012-04-25 08:59:22 +00005517 if (StaticLibgcc && !isAndroid)
Rafael Espindolaabf3ac72011-10-17 21:39:04 +00005518 CmdArgs.push_back("-lgcc_eh");
5519 else if (!Args.hasArg(options::OPT_shared) && D.CCCIsCXX)
5520 CmdArgs.push_back("-lgcc");
Logan Chien529a73d2012-11-19 12:04:11 +00005521
5522 // According to Android ABI, we have to link with libdl if we are
5523 // linking with non-static libgcc.
5524 //
5525 // NOTE: This fixes a link error on Android MIPS as well. The non-static
5526 // libgcc for MIPS relies on _Unwind_Find_FDE and dl_iterate_phdr from libdl.
5527 if (isAndroid && !StaticLibgcc)
5528 CmdArgs.push_back("-ldl");
Rafael Espindolaabf3ac72011-10-17 21:39:04 +00005529}
5530
Simon Atanasyanf4bd3292012-10-21 11:44:57 +00005531static bool hasMipsN32ABIArg(const ArgList &Args) {
5532 Arg *A = Args.getLastArg(options::OPT_mabi_EQ);
Richard Smith1d489cf2012-11-01 04:30:05 +00005533 return A && (A->getValue() == StringRef("n32"));
Simon Atanasyanf4bd3292012-10-21 11:44:57 +00005534}
5535
Rafael Espindolac1da9812010-11-07 20:14:31 +00005536void linuxtools::Link::ConstructJob(Compilation &C, const JobAction &JA,
5537 const InputInfo &Output,
5538 const InputInfoList &Inputs,
5539 const ArgList &Args,
5540 const char *LinkingOutput) const {
5541 const toolchains::Linux& ToolChain =
5542 static_cast<const toolchains::Linux&>(getToolChain());
5543 const Driver &D = ToolChain.getDriver();
Rafael Espindola715852c2012-11-02 20:41:30 +00005544 const bool isAndroid =
5545 ToolChain.getTriple().getEnvironment() == llvm::Triple::Android;
Evgeniy Stepanova6ddc022012-04-25 08:59:22 +00005546
Rafael Espindolac1da9812010-11-07 20:14:31 +00005547 ArgStringList CmdArgs;
5548
Rafael Espindola26f14c32010-11-15 18:28:16 +00005549 // Silence warning for "clang -g foo.o -o foo"
5550 Args.ClaimAllArgs(options::OPT_g_Group);
Rafael Espindola9c094fb2011-03-01 05:25:27 +00005551 // and "clang -emit-llvm foo.o -o foo"
5552 Args.ClaimAllArgs(options::OPT_emit_llvm);
David Chisnalldfa210b2012-07-29 15:24:44 +00005553 // and for "clang -w foo.o -o foo". Other warning options are already
Rafael Espindola7f6458b2010-11-17 20:37:10 +00005554 // handled somewhere else.
5555 Args.ClaimAllArgs(options::OPT_w);
Rafael Espindola26f14c32010-11-15 18:28:16 +00005556
Joerg Sonnenberger8ab2bdc2011-03-21 13:51:29 +00005557 if (!D.SysRoot.empty())
5558 CmdArgs.push_back(Args.MakeArgString("--sysroot=" + D.SysRoot));
Rafael Espindolac1da9812010-11-07 20:14:31 +00005559
Peter Collingbourne17d481f2013-01-17 20:17:16 +00005560 if (Args.hasArg(options::OPT_pie) && !Args.hasArg(options::OPT_shared))
Rafael Espindolafdda1712010-11-17 22:26:15 +00005561 CmdArgs.push_back("-pie");
5562
Rafael Espindoladc1b76d2010-11-07 22:57:16 +00005563 if (Args.hasArg(options::OPT_rdynamic))
5564 CmdArgs.push_back("-export-dynamic");
5565
Rafael Espindolae0e6d3b2010-11-11 19:34:42 +00005566 if (Args.hasArg(options::OPT_s))
5567 CmdArgs.push_back("-s");
5568
Rafael Espindolac1da9812010-11-07 20:14:31 +00005569 for (std::vector<std::string>::const_iterator i = ToolChain.ExtraOpts.begin(),
5570 e = ToolChain.ExtraOpts.end();
5571 i != e; ++i)
5572 CmdArgs.push_back(i->c_str());
5573
5574 if (!Args.hasArg(options::OPT_static)) {
5575 CmdArgs.push_back("--eh-frame-hdr");
5576 }
5577
5578 CmdArgs.push_back("-m");
5579 if (ToolChain.getArch() == llvm::Triple::x86)
5580 CmdArgs.push_back("elf_i386");
Eric Christopher88b7cf02011-08-19 00:30:14 +00005581 else if (ToolChain.getArch() == llvm::Triple::arm
Douglas Gregorf0594d82011-03-06 19:11:49 +00005582 || ToolChain.getArch() == llvm::Triple::thumb)
Rafael Espindolac1da9812010-11-07 20:14:31 +00005583 CmdArgs.push_back("armelf_linux_eabi");
Ted Kremenek43ac2972011-04-05 22:04:27 +00005584 else if (ToolChain.getArch() == llvm::Triple::ppc)
5585 CmdArgs.push_back("elf32ppclinux");
5586 else if (ToolChain.getArch() == llvm::Triple::ppc64)
5587 CmdArgs.push_back("elf64ppc");
Eli Friedman5bea4f62011-11-08 19:43:37 +00005588 else if (ToolChain.getArch() == llvm::Triple::mips)
5589 CmdArgs.push_back("elf32btsmip");
5590 else if (ToolChain.getArch() == llvm::Triple::mipsel)
5591 CmdArgs.push_back("elf32ltsmip");
Simon Atanasyanf4bd3292012-10-21 11:44:57 +00005592 else if (ToolChain.getArch() == llvm::Triple::mips64) {
5593 if (hasMipsN32ABIArg(Args))
5594 CmdArgs.push_back("elf32btsmipn32");
5595 else
5596 CmdArgs.push_back("elf64btsmip");
5597 }
5598 else if (ToolChain.getArch() == llvm::Triple::mips64el) {
5599 if (hasMipsN32ABIArg(Args))
5600 CmdArgs.push_back("elf32ltsmipn32");
5601 else
5602 CmdArgs.push_back("elf64ltsmip");
5603 }
Rafael Espindolac1da9812010-11-07 20:14:31 +00005604 else
5605 CmdArgs.push_back("elf_x86_64");
5606
5607 if (Args.hasArg(options::OPT_static)) {
Douglas Gregorf0594d82011-03-06 19:11:49 +00005608 if (ToolChain.getArch() == llvm::Triple::arm
5609 || ToolChain.getArch() == llvm::Triple::thumb)
Rafael Espindolac1da9812010-11-07 20:14:31 +00005610 CmdArgs.push_back("-Bstatic");
5611 else
5612 CmdArgs.push_back("-static");
5613 } else if (Args.hasArg(options::OPT_shared)) {
5614 CmdArgs.push_back("-shared");
Rafael Espindola715852c2012-11-02 20:41:30 +00005615 if (isAndroid) {
Evgeniy Stepanova6ddc022012-04-25 08:59:22 +00005616 CmdArgs.push_back("-Bsymbolic");
5617 }
Rafael Espindolac1da9812010-11-07 20:14:31 +00005618 }
5619
5620 if (ToolChain.getArch() == llvm::Triple::arm ||
Douglas Gregorf0594d82011-03-06 19:11:49 +00005621 ToolChain.getArch() == llvm::Triple::thumb ||
Rafael Espindolac1da9812010-11-07 20:14:31 +00005622 (!Args.hasArg(options::OPT_static) &&
5623 !Args.hasArg(options::OPT_shared))) {
5624 CmdArgs.push_back("-dynamic-linker");
Evgeniy Stepanova6ddc022012-04-25 08:59:22 +00005625 if (isAndroid)
5626 CmdArgs.push_back("/system/bin/linker");
5627 else if (ToolChain.getArch() == llvm::Triple::x86)
Rafael Espindolac1da9812010-11-07 20:14:31 +00005628 CmdArgs.push_back("/lib/ld-linux.so.2");
Douglas Gregorf0594d82011-03-06 19:11:49 +00005629 else if (ToolChain.getArch() == llvm::Triple::arm ||
Jiangning Liu6cc9dc82012-07-30 11:05:56 +00005630 ToolChain.getArch() == llvm::Triple::thumb) {
5631 if (ToolChain.getTriple().getEnvironment() == llvm::Triple::GNUEABIHF)
5632 CmdArgs.push_back("/lib/ld-linux-armhf.so.3");
5633 else
5634 CmdArgs.push_back("/lib/ld-linux.so.3");
5635 }
Eli Friedman5bea4f62011-11-08 19:43:37 +00005636 else if (ToolChain.getArch() == llvm::Triple::mips ||
5637 ToolChain.getArch() == llvm::Triple::mipsel)
5638 CmdArgs.push_back("/lib/ld.so.1");
Simon Atanasyan8491cb22012-04-06 20:14:27 +00005639 else if (ToolChain.getArch() == llvm::Triple::mips64 ||
Simon Atanasyanf4bd3292012-10-21 11:44:57 +00005640 ToolChain.getArch() == llvm::Triple::mips64el) {
5641 if (hasMipsN32ABIArg(Args))
5642 CmdArgs.push_back("/lib32/ld.so.1");
5643 else
5644 CmdArgs.push_back("/lib64/ld.so.1");
5645 }
Ted Kremenek43ac2972011-04-05 22:04:27 +00005646 else if (ToolChain.getArch() == llvm::Triple::ppc)
Chris Lattner09f43ed2011-04-11 21:15:37 +00005647 CmdArgs.push_back("/lib/ld.so.1");
Ted Kremenek43ac2972011-04-05 22:04:27 +00005648 else if (ToolChain.getArch() == llvm::Triple::ppc64)
Chris Lattner09f43ed2011-04-11 21:15:37 +00005649 CmdArgs.push_back("/lib64/ld64.so.1");
Rafael Espindolac1da9812010-11-07 20:14:31 +00005650 else
5651 CmdArgs.push_back("/lib64/ld-linux-x86-64.so.2");
5652 }
5653
5654 CmdArgs.push_back("-o");
5655 CmdArgs.push_back(Output.getFilename());
5656
Rafael Espindola49c64fd2010-12-01 01:52:43 +00005657 if (!Args.hasArg(options::OPT_nostdlib) &&
5658 !Args.hasArg(options::OPT_nostartfiles)) {
Evgeniy Stepanova6ddc022012-04-25 08:59:22 +00005659 if (!isAndroid) {
5660 const char *crt1 = NULL;
5661 if (!Args.hasArg(options::OPT_shared)){
5662 if (Args.hasArg(options::OPT_pie))
5663 crt1 = "Scrt1.o";
5664 else
5665 crt1 = "crt1.o";
5666 }
5667 if (crt1)
5668 CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath(crt1)));
Rafael Espindolac1da9812010-11-07 20:14:31 +00005669
Evgeniy Stepanova6ddc022012-04-25 08:59:22 +00005670 CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath("crti.o")));
5671 }
Rafael Espindolac1da9812010-11-07 20:14:31 +00005672
Rafael Espindola89414b32010-11-12 03:00:39 +00005673 const char *crtbegin;
5674 if (Args.hasArg(options::OPT_static))
Evgeniy Stepanova6ddc022012-04-25 08:59:22 +00005675 crtbegin = isAndroid ? "crtbegin_static.o" : "crtbeginT.o";
Evgeniy Stepanova92983d2012-09-10 10:30:12 +00005676 else if (Args.hasArg(options::OPT_shared))
Evgeniy Stepanova6ddc022012-04-25 08:59:22 +00005677 crtbegin = isAndroid ? "crtbegin_so.o" : "crtbeginS.o";
Evgeniy Stepanova92983d2012-09-10 10:30:12 +00005678 else if (Args.hasArg(options::OPT_pie))
5679 crtbegin = isAndroid ? "crtbegin_dynamic.o" : "crtbeginS.o";
Rafael Espindola89414b32010-11-12 03:00:39 +00005680 else
Evgeniy Stepanova6ddc022012-04-25 08:59:22 +00005681 crtbegin = isAndroid ? "crtbegin_dynamic.o" : "crtbegin.o";
Rafael Espindola89414b32010-11-12 03:00:39 +00005682 CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath(crtbegin)));
Benjamin Kramere20e5082012-10-04 19:42:20 +00005683
5684 // Add crtfastmath.o if available and fast math is enabled.
5685 ToolChain.AddFastMathRuntimeIfAvailable(Args, CmdArgs);
Rafael Espindola89414b32010-11-12 03:00:39 +00005686 }
Rafael Espindolac1da9812010-11-07 20:14:31 +00005687
5688 Args.AddAllArgs(CmdArgs, options::OPT_L);
5689
5690 const ToolChain::path_list Paths = ToolChain.getFilePaths();
5691
Roman Divacky58e5ac92011-03-01 17:53:14 +00005692 for (ToolChain::path_list::const_iterator i = Paths.begin(), e = Paths.end();
5693 i != e; ++i)
Chris Lattner5f9e2722011-07-23 10:55:15 +00005694 CmdArgs.push_back(Args.MakeArgString(StringRef("-L") + *i));
Rafael Espindolac1da9812010-11-07 20:14:31 +00005695
Rafael Espindolac5151542012-04-09 23:53:34 +00005696 // Tell the linker to load the plugin. This has to come before AddLinkerInputs
5697 // as gold requires -plugin to come before any -plugin-opt that -Wl might
5698 // forward.
5699 if (D.IsUsingLTO(Args) || Args.hasArg(options::OPT_use_gold_plugin)) {
5700 CmdArgs.push_back("-plugin");
5701 std::string Plugin = ToolChain.getDriver().Dir + "/../lib/LLVMgold.so";
5702 CmdArgs.push_back(Args.MakeArgString(Plugin));
Chandler Carruth700d4e42013-01-13 11:46:33 +00005703
5704 // Try to pass driver level flags relevant to LTO code generation down to
5705 // the plugin.
5706
5707 // Handle architecture-specific flags for selecting CPU variants.
5708 if (ToolChain.getArch() == llvm::Triple::x86 ||
5709 ToolChain.getArch() == llvm::Triple::x86_64)
5710 CmdArgs.push_back(
5711 Args.MakeArgString(Twine("-plugin-opt=mcpu=") +
5712 getX86TargetCPU(Args, ToolChain.getTriple())));
5713 else if (ToolChain.getArch() == llvm::Triple::arm ||
5714 ToolChain.getArch() == llvm::Triple::thumb)
5715 CmdArgs.push_back(
5716 Args.MakeArgString(Twine("-plugin-opt=mcpu=") +
5717 getARMTargetCPU(Args, ToolChain.getTriple())));
5718
5719 // FIXME: Factor out logic for MIPS, PPC, and other targets to support this
5720 // as well.
Rafael Espindolac5151542012-04-09 23:53:34 +00005721 }
5722
Chandler Carruth700d4e42013-01-13 11:46:33 +00005723
Nick Lewyckye276cfc2012-08-17 03:39:16 +00005724 if (Args.hasArg(options::OPT_Z_Xlinker__no_demangle))
5725 CmdArgs.push_back("--no-demangle");
5726
Rafael Espindolac1da9812010-11-07 20:14:31 +00005727 AddLinkerInputs(ToolChain, Inputs, Args, CmdArgs);
5728
Alexey Samsonovbb1071c2012-11-06 15:09:03 +00005729 SanitizerArgs Sanitize(D, Args);
Richard Smithc4dabad2012-11-05 22:04:41 +00005730
Eric Christopher6716d942012-11-29 18:51:05 +00005731 // Call these before we add the C++ ABI library.
Richard Smithc4dabad2012-11-05 22:04:41 +00005732 if (Sanitize.needsUbsanRt())
5733 addUbsanRTLinux(getToolChain(), Args, CmdArgs);
Eric Christopher6716d942012-11-29 18:51:05 +00005734 if (Sanitize.needsAsanRt())
5735 addAsanRTLinux(getToolChain(), Args, CmdArgs);
5736 if (Sanitize.needsTsanRt())
5737 addTsanRTLinux(getToolChain(), Args, CmdArgs);
Evgeniy Stepanov09ccf392012-12-03 13:20:43 +00005738 if (Sanitize.needsMsanRt())
5739 addMsanRTLinux(getToolChain(), Args, CmdArgs);
Richard Smith8e1cee62012-10-25 02:14:12 +00005740
Chandler Carruth2ba542c2012-05-14 18:31:18 +00005741 if (D.CCCIsCXX &&
5742 !Args.hasArg(options::OPT_nostdlib) &&
5743 !Args.hasArg(options::OPT_nodefaultlibs)) {
Rafael Espindola19706f82011-10-17 22:14:51 +00005744 bool OnlyLibstdcxxStatic = Args.hasArg(options::OPT_static_libstdcxx) &&
5745 !Args.hasArg(options::OPT_static);
5746 if (OnlyLibstdcxxStatic)
5747 CmdArgs.push_back("-Bstatic");
Rafael Espindolac1da9812010-11-07 20:14:31 +00005748 ToolChain.AddCXXStdlibLibArgs(Args, CmdArgs);
Rafael Espindola19706f82011-10-17 22:14:51 +00005749 if (OnlyLibstdcxxStatic)
5750 CmdArgs.push_back("-Bdynamic");
Rafael Espindolac1da9812010-11-07 20:14:31 +00005751 CmdArgs.push_back("-lm");
5752 }
5753
Rafael Espindola89414b32010-11-12 03:00:39 +00005754 if (!Args.hasArg(options::OPT_nostdlib)) {
Chandler Carruth2ba542c2012-05-14 18:31:18 +00005755 if (!Args.hasArg(options::OPT_nodefaultlibs)) {
5756 if (Args.hasArg(options::OPT_static))
5757 CmdArgs.push_back("--start-group");
Nick Lewycky80df0252011-06-04 06:27:06 +00005758
Chandler Carruthdf96e022013-01-17 13:19:29 +00005759 bool OpenMP = Args.hasArg(options::OPT_fopenmp);
5760 if (OpenMP) {
5761 CmdArgs.push_back("-lgomp");
5762
5763 // FIXME: Exclude this for platforms whith libgomp that doesn't require
5764 // librt. Most modern Linux platfroms require it, but some may not.
5765 CmdArgs.push_back("-lrt");
5766 }
5767
Evgeniy Stepanova6ddc022012-04-25 08:59:22 +00005768 AddLibgcc(ToolChain.getTriple(), D, CmdArgs, Args);
Rafael Espindola89414b32010-11-12 03:00:39 +00005769
Chandler Carruth2ba542c2012-05-14 18:31:18 +00005770 if (Args.hasArg(options::OPT_pthread) ||
Chandler Carruthdf96e022013-01-17 13:19:29 +00005771 Args.hasArg(options::OPT_pthreads) || OpenMP)
Chandler Carruth2ba542c2012-05-14 18:31:18 +00005772 CmdArgs.push_back("-lpthread");
5773
5774 CmdArgs.push_back("-lc");
5775
5776 if (Args.hasArg(options::OPT_static))
5777 CmdArgs.push_back("--end-group");
5778 else
5779 AddLibgcc(ToolChain.getTriple(), D, CmdArgs, Args);
5780 }
Rafael Espindolafdda1712010-11-17 22:26:15 +00005781
Rafael Espindola49c64fd2010-12-01 01:52:43 +00005782 if (!Args.hasArg(options::OPT_nostartfiles)) {
5783 const char *crtend;
Evgeniy Stepanova92983d2012-09-10 10:30:12 +00005784 if (Args.hasArg(options::OPT_shared))
Evgeniy Stepanova6ddc022012-04-25 08:59:22 +00005785 crtend = isAndroid ? "crtend_so.o" : "crtendS.o";
Evgeniy Stepanova92983d2012-09-10 10:30:12 +00005786 else if (Args.hasArg(options::OPT_pie))
5787 crtend = isAndroid ? "crtend_android.o" : "crtendS.o";
Rafael Espindola49c64fd2010-12-01 01:52:43 +00005788 else
Evgeniy Stepanova6ddc022012-04-25 08:59:22 +00005789 crtend = isAndroid ? "crtend_android.o" : "crtend.o";
Rafael Espindola89414b32010-11-12 03:00:39 +00005790
Rafael Espindola49c64fd2010-12-01 01:52:43 +00005791 CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath(crtend)));
Evgeniy Stepanova6ddc022012-04-25 08:59:22 +00005792 if (!isAndroid)
5793 CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath("crtn.o")));
Rafael Espindola49c64fd2010-12-01 01:52:43 +00005794 }
Rafael Espindolac1da9812010-11-07 20:14:31 +00005795 }
5796
Bill Wendling3f4be6f2011-06-27 19:15:03 +00005797 addProfileRT(getToolChain(), Args, CmdArgs, getToolChain().getTriple());
Nick Lewycky2e95a6d2011-05-24 21:54:59 +00005798
Rafael Espindolac1da9812010-11-07 20:14:31 +00005799 C.addCommand(new Command(JA, *this, ToolChain.Linker.c_str(), CmdArgs));
5800}
Rafael Espindolaba30bbe2010-08-10 00:25:48 +00005801
Chris Lattner38e317d2010-07-07 16:01:42 +00005802void minix::Assemble::ConstructJob(Compilation &C, const JobAction &JA,
Daniel Dunbar2fe238e2010-08-02 02:38:28 +00005803 const InputInfo &Output,
5804 const InputInfoList &Inputs,
5805 const ArgList &Args,
5806 const char *LinkingOutput) const {
Chris Lattner38e317d2010-07-07 16:01:42 +00005807 ArgStringList CmdArgs;
5808
5809 Args.AddAllArgValues(CmdArgs, options::OPT_Wa_COMMA,
5810 options::OPT_Xassembler);
5811
5812 CmdArgs.push_back("-o");
Daniel Dunbar7c1e4652010-08-02 02:38:21 +00005813 CmdArgs.push_back(Output.getFilename());
Chris Lattner38e317d2010-07-07 16:01:42 +00005814
5815 for (InputInfoList::const_iterator
5816 it = Inputs.begin(), ie = Inputs.end(); it != ie; ++it) {
5817 const InputInfo &II = *it;
Daniel Dunbar7c1e4652010-08-02 02:38:21 +00005818 CmdArgs.push_back(II.getFilename());
Chris Lattner38e317d2010-07-07 16:01:42 +00005819 }
5820
5821 const char *Exec =
Eli Friedman6d402dc2011-12-08 23:54:21 +00005822 Args.MakeArgString(getToolChain().GetProgramPath("as"));
Daniel Dunbar2fe238e2010-08-02 02:38:28 +00005823 C.addCommand(new Command(JA, *this, Exec, CmdArgs));
Chris Lattner38e317d2010-07-07 16:01:42 +00005824}
5825
5826void minix::Link::ConstructJob(Compilation &C, const JobAction &JA,
Daniel Dunbar2fe238e2010-08-02 02:38:28 +00005827 const InputInfo &Output,
5828 const InputInfoList &Inputs,
5829 const ArgList &Args,
5830 const char *LinkingOutput) const {
Chris Lattner38e317d2010-07-07 16:01:42 +00005831 const Driver &D = getToolChain().getDriver();
5832 ArgStringList CmdArgs;
5833
Daniel Dunbar7c1e4652010-08-02 02:38:21 +00005834 if (Output.isFilename()) {
Chris Lattner38e317d2010-07-07 16:01:42 +00005835 CmdArgs.push_back("-o");
5836 CmdArgs.push_back(Output.getFilename());
5837 } else {
5838 assert(Output.isNothing() && "Invalid output.");
5839 }
5840
5841 if (!Args.hasArg(options::OPT_nostdlib) &&
Eli Friedman6d402dc2011-12-08 23:54:21 +00005842 !Args.hasArg(options::OPT_nostartfiles)) {
5843 CmdArgs.push_back(Args.MakeArgString(getToolChain().GetFilePath("crt1.o")));
5844 CmdArgs.push_back(Args.MakeArgString(getToolChain().GetFilePath("crti.o")));
5845 CmdArgs.push_back(Args.MakeArgString(getToolChain().GetFilePath("crtbegin.o")));
5846 CmdArgs.push_back(Args.MakeArgString(getToolChain().GetFilePath("crtn.o")));
5847 }
Chris Lattner38e317d2010-07-07 16:01:42 +00005848
5849 Args.AddAllArgs(CmdArgs, options::OPT_L);
5850 Args.AddAllArgs(CmdArgs, options::OPT_T_Group);
5851 Args.AddAllArgs(CmdArgs, options::OPT_e);
5852
Daniel Dunbar2008fee2010-09-17 00:24:54 +00005853 AddLinkerInputs(getToolChain(), Inputs, Args, CmdArgs);
Chris Lattner38e317d2010-07-07 16:01:42 +00005854
Eli Friedman6d402dc2011-12-08 23:54:21 +00005855 addProfileRT(getToolChain(), Args, CmdArgs, getToolChain().getTriple());
5856
Chris Lattner38e317d2010-07-07 16:01:42 +00005857 if (!Args.hasArg(options::OPT_nostdlib) &&
5858 !Args.hasArg(options::OPT_nodefaultlibs)) {
5859 if (D.CCCIsCXX) {
Daniel Dunbar132e35d2010-09-17 01:20:05 +00005860 getToolChain().AddCXXStdlibLibArgs(Args, CmdArgs);
Chris Lattner38e317d2010-07-07 16:01:42 +00005861 CmdArgs.push_back("-lm");
5862 }
Chris Lattner38e317d2010-07-07 16:01:42 +00005863 }
5864
5865 if (!Args.hasArg(options::OPT_nostdlib) &&
5866 !Args.hasArg(options::OPT_nostartfiles)) {
Eli Friedman6d402dc2011-12-08 23:54:21 +00005867 if (Args.hasArg(options::OPT_pthread))
5868 CmdArgs.push_back("-lpthread");
5869 CmdArgs.push_back("-lc");
5870 CmdArgs.push_back("-lCompilerRT-Generic");
5871 CmdArgs.push_back("-L/usr/pkg/compiler-rt/lib");
5872 CmdArgs.push_back(
Eric Christopher27e2b982012-12-18 00:31:10 +00005873 Args.MakeArgString(getToolChain().GetFilePath("crtend.o")));
Chris Lattner38e317d2010-07-07 16:01:42 +00005874 }
5875
Eli Friedman6d402dc2011-12-08 23:54:21 +00005876 const char *Exec = Args.MakeArgString(getToolChain().GetProgramPath("ld"));
Daniel Dunbar2fe238e2010-08-02 02:38:28 +00005877 C.addCommand(new Command(JA, *this, Exec, CmdArgs));
Chris Lattner38e317d2010-07-07 16:01:42 +00005878}
5879
Daniel Dunbar11e1b402009-05-02 18:28:39 +00005880/// DragonFly Tools
5881
5882// For now, DragonFly Assemble does just about the same as for
5883// FreeBSD, but this may change soon.
5884void dragonfly::Assemble::ConstructJob(Compilation &C, const JobAction &JA,
Daniel Dunbar2fe238e2010-08-02 02:38:28 +00005885 const InputInfo &Output,
Daniel Dunbar294691e2009-11-04 06:24:38 +00005886 const InputInfoList &Inputs,
5887 const ArgList &Args,
5888 const char *LinkingOutput) const {
Daniel Dunbar11e1b402009-05-02 18:28:39 +00005889 ArgStringList CmdArgs;
5890
5891 // When building 32-bit code on DragonFly/pc64, we have to explicitly
5892 // instruct as in the base system to assemble 32-bit code.
Rafael Espindola64f7ad92012-10-07 04:44:33 +00005893 if (getToolChain().getArch() == llvm::Triple::x86)
Daniel Dunbar11e1b402009-05-02 18:28:39 +00005894 CmdArgs.push_back("--32");
5895
5896 Args.AddAllArgValues(CmdArgs, options::OPT_Wa_COMMA,
5897 options::OPT_Xassembler);
5898
5899 CmdArgs.push_back("-o");
Daniel Dunbar7c1e4652010-08-02 02:38:21 +00005900 CmdArgs.push_back(Output.getFilename());
Daniel Dunbar11e1b402009-05-02 18:28:39 +00005901
5902 for (InputInfoList::const_iterator
5903 it = Inputs.begin(), ie = Inputs.end(); it != ie; ++it) {
5904 const InputInfo &II = *it;
Daniel Dunbar7c1e4652010-08-02 02:38:21 +00005905 CmdArgs.push_back(II.getFilename());
Daniel Dunbar11e1b402009-05-02 18:28:39 +00005906 }
5907
5908 const char *Exec =
Daniel Dunbar4a7e8892010-07-14 18:46:23 +00005909 Args.MakeArgString(getToolChain().GetProgramPath("as"));
Daniel Dunbar2fe238e2010-08-02 02:38:28 +00005910 C.addCommand(new Command(JA, *this, Exec, CmdArgs));
Daniel Dunbar11e1b402009-05-02 18:28:39 +00005911}
5912
5913void dragonfly::Link::ConstructJob(Compilation &C, const JobAction &JA,
Daniel Dunbar2fe238e2010-08-02 02:38:28 +00005914 const InputInfo &Output,
5915 const InputInfoList &Inputs,
5916 const ArgList &Args,
5917 const char *LinkingOutput) const {
Daniel Dunbaree788e72009-12-21 18:54:17 +00005918 const Driver &D = getToolChain().getDriver();
Daniel Dunbar11e1b402009-05-02 18:28:39 +00005919 ArgStringList CmdArgs;
5920
Joerg Sonnenberger8ab2bdc2011-03-21 13:51:29 +00005921 if (!D.SysRoot.empty())
5922 CmdArgs.push_back(Args.MakeArgString("--sysroot=" + D.SysRoot));
5923
Daniel Dunbar11e1b402009-05-02 18:28:39 +00005924 if (Args.hasArg(options::OPT_static)) {
5925 CmdArgs.push_back("-Bstatic");
5926 } else {
5927 if (Args.hasArg(options::OPT_shared))
5928 CmdArgs.push_back("-Bshareable");
5929 else {
5930 CmdArgs.push_back("-dynamic-linker");
5931 CmdArgs.push_back("/usr/libexec/ld-elf.so.2");
5932 }
5933 }
5934
5935 // When building 32-bit code on DragonFly/pc64, we have to explicitly
5936 // instruct ld in the base system to link 32-bit code.
Rafael Espindola64f7ad92012-10-07 04:44:33 +00005937 if (getToolChain().getArch() == llvm::Triple::x86) {
Daniel Dunbar11e1b402009-05-02 18:28:39 +00005938 CmdArgs.push_back("-m");
5939 CmdArgs.push_back("elf_i386");
5940 }
5941
Daniel Dunbar7c1e4652010-08-02 02:38:21 +00005942 if (Output.isFilename()) {
Daniel Dunbar11e1b402009-05-02 18:28:39 +00005943 CmdArgs.push_back("-o");
5944 CmdArgs.push_back(Output.getFilename());
5945 } else {
5946 assert(Output.isNothing() && "Invalid output.");
5947 }
5948
5949 if (!Args.hasArg(options::OPT_nostdlib) &&
5950 !Args.hasArg(options::OPT_nostartfiles)) {
5951 if (!Args.hasArg(options::OPT_shared)) {
Chris Lattner38e317d2010-07-07 16:01:42 +00005952 CmdArgs.push_back(
Daniel Dunbar4a7e8892010-07-14 18:46:23 +00005953 Args.MakeArgString(getToolChain().GetFilePath("crt1.o")));
Chris Lattner38e317d2010-07-07 16:01:42 +00005954 CmdArgs.push_back(
Daniel Dunbar4a7e8892010-07-14 18:46:23 +00005955 Args.MakeArgString(getToolChain().GetFilePath("crti.o")));
Chris Lattner38e317d2010-07-07 16:01:42 +00005956 CmdArgs.push_back(
Daniel Dunbar4a7e8892010-07-14 18:46:23 +00005957 Args.MakeArgString(getToolChain().GetFilePath("crtbegin.o")));
Daniel Dunbar11e1b402009-05-02 18:28:39 +00005958 } else {
Chris Lattner38e317d2010-07-07 16:01:42 +00005959 CmdArgs.push_back(
Daniel Dunbar4a7e8892010-07-14 18:46:23 +00005960 Args.MakeArgString(getToolChain().GetFilePath("crti.o")));
Chris Lattner38e317d2010-07-07 16:01:42 +00005961 CmdArgs.push_back(
Daniel Dunbar4a7e8892010-07-14 18:46:23 +00005962 Args.MakeArgString(getToolChain().GetFilePath("crtbeginS.o")));
Daniel Dunbar11e1b402009-05-02 18:28:39 +00005963 }
5964 }
5965
5966 Args.AddAllArgs(CmdArgs, options::OPT_L);
5967 Args.AddAllArgs(CmdArgs, options::OPT_T_Group);
5968 Args.AddAllArgs(CmdArgs, options::OPT_e);
5969
Daniel Dunbar2008fee2010-09-17 00:24:54 +00005970 AddLinkerInputs(getToolChain(), Inputs, Args, CmdArgs);
Daniel Dunbar11e1b402009-05-02 18:28:39 +00005971
5972 if (!Args.hasArg(options::OPT_nostdlib) &&
5973 !Args.hasArg(options::OPT_nodefaultlibs)) {
5974 // FIXME: GCC passes on -lgcc, -lgcc_pic and a whole lot of
5975 // rpaths
5976 CmdArgs.push_back("-L/usr/lib/gcc41");
5977
5978 if (!Args.hasArg(options::OPT_static)) {
5979 CmdArgs.push_back("-rpath");
5980 CmdArgs.push_back("/usr/lib/gcc41");
5981
5982 CmdArgs.push_back("-rpath-link");
5983 CmdArgs.push_back("/usr/lib/gcc41");
5984
5985 CmdArgs.push_back("-rpath");
5986 CmdArgs.push_back("/usr/lib");
5987
5988 CmdArgs.push_back("-rpath-link");
5989 CmdArgs.push_back("/usr/lib");
5990 }
5991
Rafael Espindola405861d2010-07-20 12:59:03 +00005992 if (D.CCCIsCXX) {
Daniel Dunbar132e35d2010-09-17 01:20:05 +00005993 getToolChain().AddCXXStdlibLibArgs(Args, CmdArgs);
Rafael Espindola405861d2010-07-20 12:59:03 +00005994 CmdArgs.push_back("-lm");
5995 }
5996
Daniel Dunbar11e1b402009-05-02 18:28:39 +00005997 if (Args.hasArg(options::OPT_shared)) {
5998 CmdArgs.push_back("-lgcc_pic");
5999 } else {
6000 CmdArgs.push_back("-lgcc");
6001 }
6002
6003
6004 if (Args.hasArg(options::OPT_pthread))
Mike Stump4d63f8b2009-10-31 20:11:46 +00006005 CmdArgs.push_back("-lpthread");
Daniel Dunbar11e1b402009-05-02 18:28:39 +00006006
6007 if (!Args.hasArg(options::OPT_nolibc)) {
6008 CmdArgs.push_back("-lc");
6009 }
6010
6011 if (Args.hasArg(options::OPT_shared)) {
6012 CmdArgs.push_back("-lgcc_pic");
6013 } else {
6014 CmdArgs.push_back("-lgcc");
6015 }
6016 }
6017
6018 if (!Args.hasArg(options::OPT_nostdlib) &&
6019 !Args.hasArg(options::OPT_nostartfiles)) {
6020 if (!Args.hasArg(options::OPT_shared))
Chris Lattner38e317d2010-07-07 16:01:42 +00006021 CmdArgs.push_back(Args.MakeArgString(
Daniel Dunbar4a7e8892010-07-14 18:46:23 +00006022 getToolChain().GetFilePath("crtend.o")));
Daniel Dunbar11e1b402009-05-02 18:28:39 +00006023 else
Chris Lattner38e317d2010-07-07 16:01:42 +00006024 CmdArgs.push_back(Args.MakeArgString(
Daniel Dunbar4a7e8892010-07-14 18:46:23 +00006025 getToolChain().GetFilePath("crtendS.o")));
Chris Lattner38e317d2010-07-07 16:01:42 +00006026 CmdArgs.push_back(Args.MakeArgString(
Daniel Dunbar4a7e8892010-07-14 18:46:23 +00006027 getToolChain().GetFilePath("crtn.o")));
Daniel Dunbar11e1b402009-05-02 18:28:39 +00006028 }
6029
Bill Wendling3f4be6f2011-06-27 19:15:03 +00006030 addProfileRT(getToolChain(), Args, CmdArgs, getToolChain().getTriple());
Nick Lewycky2e95a6d2011-05-24 21:54:59 +00006031
Daniel Dunbar11e1b402009-05-02 18:28:39 +00006032 const char *Exec =
Daniel Dunbar4a7e8892010-07-14 18:46:23 +00006033 Args.MakeArgString(getToolChain().GetProgramPath("ld"));
Daniel Dunbar2fe238e2010-08-02 02:38:28 +00006034 C.addCommand(new Command(JA, *this, Exec, CmdArgs));
Daniel Dunbar11e1b402009-05-02 18:28:39 +00006035}
Michael J. Spencerff58e362010-08-21 21:55:07 +00006036
6037void visualstudio::Link::ConstructJob(Compilation &C, const JobAction &JA,
6038 const InputInfo &Output,
6039 const InputInfoList &Inputs,
6040 const ArgList &Args,
6041 const char *LinkingOutput) const {
Michael J. Spencerff58e362010-08-21 21:55:07 +00006042 ArgStringList CmdArgs;
6043
6044 if (Output.isFilename()) {
Daniel Dunbare5a37f42010-09-17 00:45:02 +00006045 CmdArgs.push_back(Args.MakeArgString(std::string("-out:") +
6046 Output.getFilename()));
Michael J. Spencerff58e362010-08-21 21:55:07 +00006047 } else {
6048 assert(Output.isNothing() && "Invalid output.");
6049 }
6050
6051 if (!Args.hasArg(options::OPT_nostdlib) &&
6052 !Args.hasArg(options::OPT_nostartfiles)) {
6053 CmdArgs.push_back("-defaultlib:libcmt");
6054 }
6055
6056 CmdArgs.push_back("-nologo");
6057
Michael J. Spencera2284f52012-06-18 16:56:04 +00006058 Args.AddAllArgValues(CmdArgs, options::OPT_l);
6059
6060 // Add filenames immediately.
6061 for (InputInfoList::const_iterator
6062 it = Inputs.begin(), ie = Inputs.end(); it != ie; ++it) {
6063 if (it->isFilename())
6064 CmdArgs.push_back(it->getFilename());
6065 }
Michael J. Spencerff58e362010-08-21 21:55:07 +00006066
6067 const char *Exec =
Daniel Dunbar2008fee2010-09-17 00:24:54 +00006068 Args.MakeArgString(getToolChain().GetProgramPath("link.exe"));
Michael J. Spencerff58e362010-08-21 21:55:07 +00006069 C.addCommand(new Command(JA, *this, Exec, CmdArgs));
6070}