blob: 253c2dc7a1f9e31c6c852ddaf789006dfce9fffe [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 Gregor2a060852013-02-07 00:21:12 +0000417
Benjamin Kramer47adebe2011-09-22 21:41:16 +0000418 // Parse additional include paths from environment variables.
Chandler Carruthb5870e72011-11-04 07:12:58 +0000419 // FIXME: We should probably sink the logic for handling these from the
420 // frontend into the driver. It will allow deleting 4 otherwise unused flags.
Benjamin Kramer47adebe2011-09-22 21:41:16 +0000421 // CPATH - included following the user specified includes (but prior to
422 // builtin and standard includes).
Bill Wendling3d717152012-03-12 22:10:06 +0000423 addDirectoryList(Args, CmdArgs, "-I", "CPATH");
Benjamin Kramer47adebe2011-09-22 21:41:16 +0000424 // C_INCLUDE_PATH - system includes enabled when compiling C.
Bill Wendling3d717152012-03-12 22:10:06 +0000425 addDirectoryList(Args, CmdArgs, "-c-isystem", "C_INCLUDE_PATH");
Benjamin Kramer47adebe2011-09-22 21:41:16 +0000426 // CPLUS_INCLUDE_PATH - system includes enabled when compiling C++.
Bill Wendling3d717152012-03-12 22:10:06 +0000427 addDirectoryList(Args, CmdArgs, "-cxx-isystem", "CPLUS_INCLUDE_PATH");
Benjamin Kramer47adebe2011-09-22 21:41:16 +0000428 // OBJC_INCLUDE_PATH - system includes enabled when compiling ObjC.
Bill Wendling3d717152012-03-12 22:10:06 +0000429 addDirectoryList(Args, CmdArgs, "-objc-isystem", "OBJC_INCLUDE_PATH");
Benjamin Kramer47adebe2011-09-22 21:41:16 +0000430 // OBJCPLUS_INCLUDE_PATH - system includes enabled when compiling ObjC++.
Bill Wendling3d717152012-03-12 22:10:06 +0000431 addDirectoryList(Args, CmdArgs, "-objcxx-isystem", "OBJCPLUS_INCLUDE_PATH");
Chandler Carruth88491fc2011-11-04 07:12:53 +0000432
Chandler Carruth88491fc2011-11-04 07:12:53 +0000433 // Add C++ include arguments, if needed.
Chandler Carrutha4614422011-11-04 07:43:33 +0000434 if (types::isCXX(Inputs[0].getType()))
Chandler Carruth7ffa0322011-11-04 07:34:47 +0000435 getToolChain().AddClangCXXStdlibIncludeArgs(Args, CmdArgs);
Chandler Carruth7d7e9f92011-11-05 20:17:13 +0000436
437 // Add system include arguments.
438 getToolChain().AddClangSystemIncludeArgs(Args, CmdArgs);
Daniel Dunbarc21c4852009-04-08 23:54:23 +0000439}
440
Daniel Dunbar1d65e4b2009-09-10 22:59:51 +0000441/// getLLVMArchSuffixForARM - Get the LLVM arch name to use for a particular
Daniel Dunbar728a5122009-09-10 06:49:20 +0000442/// CPU.
443//
444// FIXME: This is redundant with -mcpu, why does LLVM use this.
445// FIXME: tblgen this, or kill it!
Chris Lattner5f9e2722011-07-23 10:55:15 +0000446static const char *getLLVMArchSuffixForARM(StringRef CPU) {
Chad Rosierae1aee62011-10-07 17:48:56 +0000447 return llvm::StringSwitch<const char *>(CPU)
448 .Cases("arm7tdmi", "arm7tdmi-s", "arm710t", "v4t")
449 .Cases("arm720t", "arm9", "arm9tdmi", "v4t")
450 .Cases("arm920", "arm920t", "arm922t", "v4t")
451 .Cases("arm940t", "ep9312","v4t")
452 .Cases("arm10tdmi", "arm1020t", "v5")
453 .Cases("arm9e", "arm926ej-s", "arm946e-s", "v5e")
454 .Cases("arm966e-s", "arm968e-s", "arm10e", "v5e")
455 .Cases("arm1020e", "arm1022e", "xscale", "iwmmxt", "v5e")
456 .Cases("arm1136j-s", "arm1136jf-s", "arm1176jz-s", "v6")
457 .Cases("arm1176jzf-s", "mpcorenovfp", "mpcore", "v6")
458 .Cases("arm1156t2-s", "arm1156t2f-s", "v6t2")
Bob Wilsonfc553452013-03-04 22:37:46 +0000459 .Cases("cortex-a5", "cortex-a7", "cortex-a8", "v7")
460 .Cases("cortex-a9", "cortex-a15", "v7")
Quentin Colombetab137512012-12-21 17:57:47 +0000461 .Case("cortex-r5", "v7r")
Bob Wilson57f6d192012-03-21 17:19:12 +0000462 .Case("cortex-m0", "v6m")
Bob Wilson2503ebd2013-03-04 22:37:49 +0000463 .Case("cortex-m3", "v7m")
464 .Case("cortex-m4", "v7em")
Bob Wilson336bfa32012-09-29 23:52:50 +0000465 .Case("cortex-a9-mp", "v7f")
466 .Case("swift", "v7s")
Chad Rosierae1aee62011-10-07 17:48:56 +0000467 .Default("");
Daniel Dunbar728a5122009-09-10 06:49:20 +0000468}
469
Benjamin Kramer92c4fd52012-06-26 22:20:06 +0000470/// getARMTargetCPU - Get the (LLVM) name of the ARM cpu we are targeting.
471//
472// FIXME: tblgen this.
473static std::string getARMTargetCPU(const ArgList &Args,
474 const llvm::Triple &Triple) {
475 // FIXME: Warn on inconsistent use of -mcpu and -march.
476
477 // If we have -mcpu=, use that.
478 if (Arg *A = Args.getLastArg(options::OPT_mcpu_EQ)) {
Richard Smith1d489cf2012-11-01 04:30:05 +0000479 StringRef MCPU = A->getValue();
Benjamin Kramer92c4fd52012-06-26 22:20:06 +0000480 // Handle -mcpu=native.
481 if (MCPU == "native")
482 return llvm::sys::getHostCPUName();
483 else
484 return MCPU;
485 }
486
487 StringRef MArch;
488 if (Arg *A = Args.getLastArg(options::OPT_march_EQ)) {
489 // Otherwise, if we have -march= choose the base CPU for that arch.
Richard Smith1d489cf2012-11-01 04:30:05 +0000490 MArch = A->getValue();
Benjamin Kramer92c4fd52012-06-26 22:20:06 +0000491 } else {
492 // Otherwise, use the Arch from the triple.
493 MArch = Triple.getArchName();
494 }
495
496 // Handle -march=native.
497 std::string NativeMArch;
498 if (MArch == "native") {
499 std::string CPU = llvm::sys::getHostCPUName();
500 if (CPU != "generic") {
501 // Translate the native cpu into the architecture. The switch below will
502 // then chose the minimum cpu for that arch.
503 NativeMArch = std::string("arm") + getLLVMArchSuffixForARM(CPU);
504 MArch = NativeMArch;
505 }
506 }
507
508 return llvm::StringSwitch<const char *>(MArch)
509 .Cases("armv2", "armv2a","arm2")
510 .Case("armv3", "arm6")
511 .Case("armv3m", "arm7m")
512 .Cases("armv4", "armv4t", "arm7tdmi")
513 .Cases("armv5", "armv5t", "arm10tdmi")
514 .Cases("armv5e", "armv5te", "arm1022e")
515 .Case("armv5tej", "arm926ej-s")
516 .Cases("armv6", "armv6k", "arm1136jf-s")
517 .Case("armv6j", "arm1136j-s")
518 .Cases("armv6z", "armv6zk", "arm1176jzf-s")
519 .Case("armv6t2", "arm1156t2-s")
Bob Wilson2503ebd2013-03-04 22:37:49 +0000520 .Cases("armv6m", "armv6-m", "cortex-m0")
Benjamin Kramer92c4fd52012-06-26 22:20:06 +0000521 .Cases("armv7", "armv7a", "armv7-a", "cortex-a8")
Bob Wilson2503ebd2013-03-04 22:37:49 +0000522 .Cases("armv7em", "armv7e-m", "cortex-m4")
Bob Wilson336bfa32012-09-29 23:52:50 +0000523 .Cases("armv7f", "armv7-f", "cortex-a9-mp")
524 .Cases("armv7s", "armv7-s", "swift")
Benjamin Kramer92c4fd52012-06-26 22:20:06 +0000525 .Cases("armv7r", "armv7-r", "cortex-r4")
526 .Cases("armv7m", "armv7-m", "cortex-m3")
527 .Case("ep9312", "ep9312")
528 .Case("iwmmxt", "iwmmxt")
529 .Case("xscale", "xscale")
Benjamin Kramer92c4fd52012-06-26 22:20:06 +0000530 // If all else failed, return the most base CPU LLVM supports.
531 .Default("arm7tdmi");
532}
533
Daniel Dunbar1f95e652009-11-17 06:37:03 +0000534// FIXME: Move to target hook.
535static bool isSignedCharDefault(const llvm::Triple &Triple) {
536 switch (Triple.getArch()) {
537 default:
538 return true;
539
Tim Northoverc264e162013-01-31 12:13:10 +0000540 case llvm::Triple::aarch64:
Jim Grosbach5b4e7b12011-05-24 15:40:46 +0000541 case llvm::Triple::arm:
Daniel Dunbar1f95e652009-11-17 06:37:03 +0000542 case llvm::Triple::ppc:
543 case llvm::Triple::ppc64:
Bob Wilson905c45f2011-10-14 05:03:44 +0000544 if (Triple.isOSDarwin())
Daniel Dunbar1f95e652009-11-17 06:37:03 +0000545 return true;
546 return false;
Daniel Dunbar1f95e652009-11-17 06:37:03 +0000547 }
548}
549
Chad Rosier99317272012-04-04 20:51:35 +0000550// Handle -mfpu=.
551//
552// FIXME: Centralize feature selection, defaulting shouldn't be also in the
553// frontend target.
554static void addFPUArgs(const Driver &D, const Arg *A, const ArgList &Args,
555 ArgStringList &CmdArgs) {
Richard Smith1d489cf2012-11-01 04:30:05 +0000556 StringRef FPU = A->getValue();
Chad Rosier99317272012-04-04 20:51:35 +0000557
558 // Set the target features based on the FPU.
559 if (FPU == "fpa" || FPU == "fpe2" || FPU == "fpe3" || FPU == "maverick") {
560 // Disable any default FPU support.
561 CmdArgs.push_back("-target-feature");
562 CmdArgs.push_back("-vfp2");
563 CmdArgs.push_back("-target-feature");
564 CmdArgs.push_back("-vfp3");
565 CmdArgs.push_back("-target-feature");
566 CmdArgs.push_back("-neon");
567 } else if (FPU == "vfp3-d16" || FPU == "vfpv3-d16") {
568 CmdArgs.push_back("-target-feature");
569 CmdArgs.push_back("+vfp3");
570 CmdArgs.push_back("-target-feature");
571 CmdArgs.push_back("+d16");
572 CmdArgs.push_back("-target-feature");
573 CmdArgs.push_back("-neon");
574 } else if (FPU == "vfp") {
575 CmdArgs.push_back("-target-feature");
576 CmdArgs.push_back("+vfp2");
577 CmdArgs.push_back("-target-feature");
578 CmdArgs.push_back("-neon");
579 } else if (FPU == "vfp3" || FPU == "vfpv3") {
580 CmdArgs.push_back("-target-feature");
581 CmdArgs.push_back("+vfp3");
582 CmdArgs.push_back("-target-feature");
583 CmdArgs.push_back("-neon");
584 } else if (FPU == "neon") {
585 CmdArgs.push_back("-target-feature");
586 CmdArgs.push_back("+neon");
587 } else
588 D.Diag(diag::err_drv_clang_unsupported) << A->getAsString(Args);
589}
590
Chad Rosier7a938fa2012-04-04 20:39:32 +0000591// Handle -mfpmath=.
592static void addFPMathArgs(const Driver &D, const Arg *A, const ArgList &Args,
Chad Rosier30fe6ba2012-04-04 22:13:40 +0000593 ArgStringList &CmdArgs, StringRef CPU) {
Richard Smith1d489cf2012-11-01 04:30:05 +0000594 StringRef FPMath = A->getValue();
Chad Rosier7a938fa2012-04-04 20:39:32 +0000595
596 // Set the target features based on the FPMath.
597 if (FPMath == "neon") {
598 CmdArgs.push_back("-target-feature");
599 CmdArgs.push_back("+neonfp");
Chad Rosier30fe6ba2012-04-04 22:13:40 +0000600
Bob Wilsonfc553452013-03-04 22:37:46 +0000601 if (CPU != "cortex-a5" && CPU != "cortex-a7" &&
602 CPU != "cortex-a8" && CPU != "cortex-a9" &&
603 CPU != "cortex-a9-mp" && CPU != "cortex-a15")
Chad Rosier30fe6ba2012-04-04 22:13:40 +0000604 D.Diag(diag::err_drv_invalid_feature) << "-mfpmath=neon" << CPU;
605
Chad Rosier7a938fa2012-04-04 20:39:32 +0000606 } else if (FPMath == "vfp" || FPMath == "vfp2" || FPMath == "vfp3" ||
607 FPMath == "vfp4") {
608 CmdArgs.push_back("-target-feature");
609 CmdArgs.push_back("-neonfp");
Chad Rosier30fe6ba2012-04-04 22:13:40 +0000610
611 // FIXME: Add warnings when disabling a feature not present for a given CPU.
Chad Rosier7a938fa2012-04-04 20:39:32 +0000612 } else
613 D.Diag(diag::err_drv_clang_unsupported) << A->getAsString(Args);
614}
615
Anton Korobeynikove2571792012-04-09 13:38:30 +0000616// Select the float ABI as determined by -msoft-float, -mhard-float, and
617// -mfloat-abi=.
618static StringRef getARMFloatABI(const Driver &D,
619 const ArgList &Args,
620 const llvm::Triple &Triple) {
Chris Lattner5f9e2722011-07-23 10:55:15 +0000621 StringRef FloatABI;
Daniel Dunbarcbd19332009-09-10 23:00:09 +0000622 if (Arg *A = Args.getLastArg(options::OPT_msoft_float,
623 options::OPT_mhard_float,
624 options::OPT_mfloat_abi_EQ)) {
625 if (A->getOption().matches(options::OPT_msoft_float))
626 FloatABI = "soft";
627 else if (A->getOption().matches(options::OPT_mhard_float))
628 FloatABI = "hard";
629 else {
Richard Smith1d489cf2012-11-01 04:30:05 +0000630 FloatABI = A->getValue();
Daniel Dunbarcbd19332009-09-10 23:00:09 +0000631 if (FloatABI != "soft" && FloatABI != "softfp" && FloatABI != "hard") {
Chris Lattner5f9e2722011-07-23 10:55:15 +0000632 D.Diag(diag::err_drv_invalid_mfloat_abi)
Daniel Dunbarcbd19332009-09-10 23:00:09 +0000633 << A->getAsString(Args);
634 FloatABI = "soft";
635 }
636 }
637 }
638
639 // If unspecified, choose the default based on the platform.
640 if (FloatABI.empty()) {
Rafael Espindolabcd6df62010-06-28 17:18:09 +0000641 switch (Triple.getOS()) {
Bob Wilson905c45f2011-10-14 05:03:44 +0000642 case llvm::Triple::Darwin:
643 case llvm::Triple::MacOSX:
644 case llvm::Triple::IOS: {
Daniel Dunbarcbd19332009-09-10 23:00:09 +0000645 // Darwin defaults to "softfp" for v6 and v7.
646 //
647 // FIXME: Factor out an ARM class so we can cache the arch somewhere.
Benjamin Kramer92c4fd52012-06-26 22:20:06 +0000648 std::string ArchName =
Rafael Espindolabcd6df62010-06-28 17:18:09 +0000649 getLLVMArchSuffixForARM(getARMTargetCPU(Args, Triple));
Benjamin Kramer92c4fd52012-06-26 22:20:06 +0000650 if (StringRef(ArchName).startswith("v6") ||
651 StringRef(ArchName).startswith("v7"))
Daniel Dunbarcbd19332009-09-10 23:00:09 +0000652 FloatABI = "softfp";
653 else
654 FloatABI = "soft";
655 break;
656 }
657
Rafael Espindola27fa2362012-12-13 04:17:14 +0000658 case llvm::Triple::FreeBSD:
659 // FreeBSD defaults to soft float
660 FloatABI = "soft";
661 break;
662
Daniel Dunbarcbd19332009-09-10 23:00:09 +0000663 default:
Bob Wilsonfc2bd7c2011-02-04 17:59:28 +0000664 switch(Triple.getEnvironment()) {
Jiangning Liuff104a12012-07-31 08:06:29 +0000665 case llvm::Triple::GNUEABIHF:
666 FloatABI = "hard";
667 break;
Bob Wilsonfc2bd7c2011-02-04 17:59:28 +0000668 case llvm::Triple::GNUEABI:
669 FloatABI = "softfp";
670 break;
671 case llvm::Triple::EABI:
672 // EABI is always AAPCS, and if it was not marked 'hard', it's softfp
673 FloatABI = "softfp";
674 break;
Logan Chien94a71422012-09-02 09:30:11 +0000675 case llvm::Triple::Android: {
Benjamin Kramer92c4fd52012-06-26 22:20:06 +0000676 std::string ArchName =
Chandler Carruthb43550b2012-01-10 19:47:42 +0000677 getLLVMArchSuffixForARM(getARMTargetCPU(Args, Triple));
Benjamin Kramer92c4fd52012-06-26 22:20:06 +0000678 if (StringRef(ArchName).startswith("v7"))
Chandler Carruthb43550b2012-01-10 19:47:42 +0000679 FloatABI = "softfp";
680 else
681 FloatABI = "soft";
682 break;
683 }
Bob Wilsonfc2bd7c2011-02-04 17:59:28 +0000684 default:
685 // Assume "soft", but warn the user we are guessing.
686 FloatABI = "soft";
Chris Lattner5f9e2722011-07-23 10:55:15 +0000687 D.Diag(diag::warn_drv_assuming_mfloat_abi_is) << "soft";
Bob Wilsonfc2bd7c2011-02-04 17:59:28 +0000688 break;
689 }
Daniel Dunbarcbd19332009-09-10 23:00:09 +0000690 }
691 }
692
Anton Korobeynikove2571792012-04-09 13:38:30 +0000693 return FloatABI;
694}
695
696
697void Clang::AddARMTargetArgs(const ArgList &Args,
698 ArgStringList &CmdArgs,
699 bool KernelOrKext) const {
700 const Driver &D = getToolChain().getDriver();
Daniel Dunbar7a0c0642012-10-15 22:23:53 +0000701 // Get the effective triple, which takes into account the deployment target.
702 std::string TripleStr = getToolChain().ComputeEffectiveClangTriple(Args);
703 llvm::Triple Triple(TripleStr);
Daniel Dunbar2e4e1102012-10-22 18:30:51 +0000704 std::string CPUName = getARMTargetCPU(Args, Triple);
Anton Korobeynikove2571792012-04-09 13:38:30 +0000705
706 // Select the ABI to use.
707 //
708 // FIXME: Support -meabi.
709 const char *ABIName = 0;
710 if (Arg *A = Args.getLastArg(options::OPT_mabi_EQ)) {
Richard Smith1d489cf2012-11-01 04:30:05 +0000711 ABIName = A->getValue();
Daniel Dunbar2e4e1102012-10-22 18:30:51 +0000712 } else if (Triple.isOSDarwin()) {
713 // The backend is hardwired to assume AAPCS for M-class processors, ensure
714 // the frontend matches that.
715 if (StringRef(CPUName).startswith("cortex-m")) {
716 ABIName = "aapcs";
717 } else {
718 ABIName = "apcs-gnu";
719 }
Anton Korobeynikove2571792012-04-09 13:38:30 +0000720 } else {
721 // Select the default based on the platform.
722 switch(Triple.getEnvironment()) {
Logan Chien94a71422012-09-02 09:30:11 +0000723 case llvm::Triple::Android:
Anton Korobeynikove2571792012-04-09 13:38:30 +0000724 case llvm::Triple::GNUEABI:
Jiangning Liuff104a12012-07-31 08:06:29 +0000725 case llvm::Triple::GNUEABIHF:
Anton Korobeynikove2571792012-04-09 13:38:30 +0000726 ABIName = "aapcs-linux";
727 break;
728 case llvm::Triple::EABI:
729 ABIName = "aapcs";
730 break;
731 default:
732 ABIName = "apcs-gnu";
733 }
734 }
735 CmdArgs.push_back("-target-abi");
736 CmdArgs.push_back(ABIName);
737
738 // Set the CPU based on -march= and -mcpu=.
739 CmdArgs.push_back("-target-cpu");
Daniel Dunbar2e4e1102012-10-22 18:30:51 +0000740 CmdArgs.push_back(Args.MakeArgString(CPUName));
Anton Korobeynikove2571792012-04-09 13:38:30 +0000741
742 // Determine floating point ABI from the options & target defaults.
743 StringRef FloatABI = getARMFloatABI(D, Args, Triple);
Daniel Dunbarcbd19332009-09-10 23:00:09 +0000744 if (FloatABI == "soft") {
745 // Floating point operations and argument passing are soft.
746 //
747 // FIXME: This changes CPP defines, we need -target-soft-float.
Daniel Dunbar3b315262009-11-30 08:42:00 +0000748 CmdArgs.push_back("-msoft-float");
Daniel Dunbar87667aa2009-12-08 19:49:51 +0000749 CmdArgs.push_back("-mfloat-abi");
750 CmdArgs.push_back("soft");
Daniel Dunbarcbd19332009-09-10 23:00:09 +0000751 } else if (FloatABI == "softfp") {
752 // Floating point operations are hard, but argument passing is soft.
Daniel Dunbar87667aa2009-12-08 19:49:51 +0000753 CmdArgs.push_back("-mfloat-abi");
754 CmdArgs.push_back("soft");
Daniel Dunbarcbd19332009-09-10 23:00:09 +0000755 } else {
756 // Floating point operations and argument passing are hard.
757 assert(FloatABI == "hard" && "Invalid float abi!");
Daniel Dunbar87667aa2009-12-08 19:49:51 +0000758 CmdArgs.push_back("-mfloat-abi");
759 CmdArgs.push_back("hard");
Daniel Dunbarcbd19332009-09-10 23:00:09 +0000760 }
Daniel Dunbar97f52ac2009-12-19 04:15:38 +0000761
762 // Set appropriate target features for floating point mode.
763 //
764 // FIXME: Note, this is a hack, the LLVM backend doesn't actually use these
765 // yet (it uses the -mfloat-abi and -msoft-float options above), and it is
766 // stripped out by the ARM target.
767
768 // Use software floating point operations?
769 if (FloatABI == "soft") {
770 CmdArgs.push_back("-target-feature");
771 CmdArgs.push_back("+soft-float");
772 }
773
774 // Use software floating point argument passing?
775 if (FloatABI != "hard") {
776 CmdArgs.push_back("-target-feature");
777 CmdArgs.push_back("+soft-float-abi");
778 }
Daniel Dunbara91320b2009-12-21 23:28:17 +0000779
780 // Honor -mfpu=.
Chad Rosier99317272012-04-04 20:51:35 +0000781 if (const Arg *A = Args.getLastArg(options::OPT_mfpu_EQ))
Chad Rosierf80f2a52012-04-04 20:56:36 +0000782 addFPUArgs(D, A, Args, CmdArgs);
Daniel Dunbar7187fac2011-03-17 00:07:34 +0000783
Chad Rosier7a938fa2012-04-04 20:39:32 +0000784 // Honor -mfpmath=.
785 if (const Arg *A = Args.getLastArg(options::OPT_mfpmath_EQ))
Chad Rosier30fe6ba2012-04-04 22:13:40 +0000786 addFPMathArgs(D, A, Args, CmdArgs, getARMTargetCPU(Args, Triple));
Chad Rosier7a938fa2012-04-04 20:39:32 +0000787
Daniel Dunbar7187fac2011-03-17 00:07:34 +0000788 // Setting -msoft-float effectively disables NEON because of the GCC
789 // implementation, although the same isn't true of VFP or VFP3.
790 if (FloatABI == "soft") {
Daniel Dunbarfa41d692011-03-17 17:10:06 +0000791 CmdArgs.push_back("-target-feature");
792 CmdArgs.push_back("-neon");
793 }
794
795 // Kernel code has more strict alignment requirements.
796 if (KernelOrKext) {
Daniel Dunbar7a0c0642012-10-15 22:23:53 +0000797 if (Triple.getOS() != llvm::Triple::IOS || Triple.isOSVersionLT(6)) {
798 CmdArgs.push_back("-backend-option");
799 CmdArgs.push_back("-arm-long-calls");
800 }
Daniel Dunbarfa41d692011-03-17 17:10:06 +0000801
Daniel Dunbar3c66d302011-03-22 16:48:17 +0000802 CmdArgs.push_back("-backend-option");
Daniel Dunbarfa41d692011-03-17 17:10:06 +0000803 CmdArgs.push_back("-arm-strict-align");
Daniel Dunbarb5fbb892011-04-18 21:26:42 +0000804
805 // The kext linker doesn't know how to deal with movw/movt.
Daniel Dunbarb5fbb892011-04-18 21:26:42 +0000806 CmdArgs.push_back("-backend-option");
807 CmdArgs.push_back("-arm-darwin-use-movt=0");
Daniel Dunbar7187fac2011-03-17 00:07:34 +0000808 }
Chad Rosier1b906052011-08-26 00:26:29 +0000809
810 // Setting -mno-global-merge disables the codegen global merge pass. Setting
811 // -mglobal-merge has no effect as the pass is enabled by default.
812 if (Arg *A = Args.getLastArg(options::OPT_mglobal_merge,
813 options::OPT_mno_global_merge)) {
814 if (A->getOption().matches(options::OPT_mno_global_merge))
815 CmdArgs.push_back("-mno-global-merge");
816 }
Chad Rosieree9ad5c2012-05-16 20:40:09 +0000817
Chad Rosier005af272012-05-16 21:19:55 +0000818 if (Args.hasArg(options::OPT_mno_implicit_float))
Chad Rosieree9ad5c2012-05-16 20:40:09 +0000819 CmdArgs.push_back("-no-implicit-float");
Daniel Dunbarb163ef72009-09-10 04:57:17 +0000820}
821
Simon Atanasyan8e1c5982012-09-21 20:19:32 +0000822// Translate MIPS CPU name alias option to CPU name.
823static StringRef getMipsCPUFromAlias(const Arg &A) {
824 if (A.getOption().matches(options::OPT_mips32))
825 return "mips32";
826 if (A.getOption().matches(options::OPT_mips32r2))
827 return "mips32r2";
828 if (A.getOption().matches(options::OPT_mips64))
829 return "mips64";
830 if (A.getOption().matches(options::OPT_mips64r2))
831 return "mips64r2";
832 llvm_unreachable("Unexpected option");
833 return "";
834}
835
Simon Atanasyana2768be2012-04-07 22:09:23 +0000836// Get CPU and ABI names. They are not independent
837// so we have to calculate them together.
838static void getMipsCPUAndABI(const ArgList &Args,
839 const ToolChain &TC,
840 StringRef &CPUName,
841 StringRef &ABIName) {
Simon Atanasyan89d83ff2012-09-10 08:32:41 +0000842 const char *DefMips32CPU = "mips32";
843 const char *DefMips64CPU = "mips64";
Akira Hatanaka9f360622011-09-26 21:07:52 +0000844
Simon Atanasyan89d83ff2012-09-10 08:32:41 +0000845 if (Arg *A = Args.getLastArg(options::OPT_march_EQ,
Simon Atanasyan8e1c5982012-09-21 20:19:32 +0000846 options::OPT_mcpu_EQ,
847 options::OPT_mips_CPUs_Group)) {
848 if (A->getOption().matches(options::OPT_mips_CPUs_Group))
849 CPUName = getMipsCPUFromAlias(*A);
850 else
Richard Smith1d489cf2012-11-01 04:30:05 +0000851 CPUName = A->getValue();
Simon Atanasyan8e1c5982012-09-21 20:19:32 +0000852 }
Simon Atanasyan89d83ff2012-09-10 08:32:41 +0000853
Akira Hatanaka9f360622011-09-26 21:07:52 +0000854 if (Arg *A = Args.getLastArg(options::OPT_mabi_EQ))
Richard Smith1d489cf2012-11-01 04:30:05 +0000855 ABIName = A->getValue();
Simon Atanasyan89d83ff2012-09-10 08:32:41 +0000856
857 // Setup default CPU and ABI names.
858 if (CPUName.empty() && ABIName.empty()) {
859 switch (TC.getTriple().getArch()) {
860 default:
861 llvm_unreachable("Unexpected triple arch name");
862 case llvm::Triple::mips:
863 case llvm::Triple::mipsel:
864 CPUName = DefMips32CPU;
865 break;
866 case llvm::Triple::mips64:
867 case llvm::Triple::mips64el:
868 CPUName = DefMips64CPU;
869 break;
870 }
871 }
872
873 if (!ABIName.empty()) {
874 // Deduce CPU name from ABI name.
875 CPUName = llvm::StringSwitch<const char *>(ABIName)
Simon Atanasyane9616a42013-02-27 14:55:49 +0000876 .Cases("32", "o32", "eabi", DefMips32CPU)
877 .Cases("n32", "n64", "64", DefMips64CPU)
Simon Atanasyan89d83ff2012-09-10 08:32:41 +0000878 .Default("");
879 }
880 else if (!CPUName.empty()) {
881 // Deduce ABI name from CPU name.
882 ABIName = llvm::StringSwitch<const char *>(CPUName)
883 .Cases("mips32", "mips32r2", "o32")
884 .Cases("mips64", "mips64r2", "n64")
885 .Default("");
886 }
887
888 // FIXME: Warn on inconsistent cpu and abi usage.
Simon Atanasyana2768be2012-04-07 22:09:23 +0000889}
890
Simon Atanasyane9616a42013-02-27 14:55:49 +0000891// Convert ABI name to the GNU tools acceptable variant.
892static StringRef getGnuCompatibleMipsABIName(StringRef ABI) {
893 return llvm::StringSwitch<llvm::StringRef>(ABI)
894 .Case("o32", "32")
895 .Case("n64", "64")
896 .Default(ABI);
897}
898
Simon Atanasyan5e627792012-06-02 15:06:29 +0000899// Select the MIPS float ABI as determined by -msoft-float, -mhard-float,
900// and -mfloat-abi=.
901static StringRef getMipsFloatABI(const Driver &D, const ArgList &Args) {
Akira Hatanakaad8d8a32012-03-23 23:07:09 +0000902 // Select the float ABI as determined by -msoft-float, -mhard-float,
903 // and -mfloat-abi=.
Chris Lattner5f9e2722011-07-23 10:55:15 +0000904 StringRef FloatABI;
Eric Christophered734732010-03-02 02:41:08 +0000905 if (Arg *A = Args.getLastArg(options::OPT_msoft_float,
Akira Hatanakaad8d8a32012-03-23 23:07:09 +0000906 options::OPT_mhard_float,
907 options::OPT_mfloat_abi_EQ)) {
Eric Christophered734732010-03-02 02:41:08 +0000908 if (A->getOption().matches(options::OPT_msoft_float))
909 FloatABI = "soft";
910 else if (A->getOption().matches(options::OPT_mhard_float))
911 FloatABI = "hard";
Akira Hatanakaad8d8a32012-03-23 23:07:09 +0000912 else {
Richard Smith1d489cf2012-11-01 04:30:05 +0000913 FloatABI = A->getValue();
Akira Hatanakaad8d8a32012-03-23 23:07:09 +0000914 if (FloatABI != "soft" && FloatABI != "single" && FloatABI != "hard") {
Simon Atanasyan5e627792012-06-02 15:06:29 +0000915 D.Diag(diag::err_drv_invalid_mfloat_abi) << A->getAsString(Args);
Akira Hatanakaad8d8a32012-03-23 23:07:09 +0000916 FloatABI = "hard";
917 }
918 }
Eric Christophered734732010-03-02 02:41:08 +0000919 }
920
921 // If unspecified, choose the default based on the platform.
922 if (FloatABI.empty()) {
Akira Hatanakaad8d8a32012-03-23 23:07:09 +0000923 // Assume "hard", because it's a default value used by gcc.
924 // When we start to recognize specific target MIPS processors,
925 // we will be able to select the default more correctly.
926 FloatABI = "hard";
Eric Christophered734732010-03-02 02:41:08 +0000927 }
928
Simon Atanasyan5e627792012-06-02 15:06:29 +0000929 return FloatABI;
930}
931
Simon Atanasyandc536f52012-07-05 18:51:43 +0000932static void AddTargetFeature(const ArgList &Args,
933 ArgStringList &CmdArgs,
934 OptSpecifier OnOpt,
935 OptSpecifier OffOpt,
936 StringRef FeatureName) {
937 if (Arg *A = Args.getLastArg(OnOpt, OffOpt)) {
938 CmdArgs.push_back("-target-feature");
939 if (A->getOption().matches(OnOpt))
940 CmdArgs.push_back(Args.MakeArgString("+" + FeatureName));
941 else
942 CmdArgs.push_back(Args.MakeArgString("-" + FeatureName));
943 }
944}
945
Simon Atanasyan5e627792012-06-02 15:06:29 +0000946void Clang::AddMIPSTargetArgs(const ArgList &Args,
947 ArgStringList &CmdArgs) const {
948 const Driver &D = getToolChain().getDriver();
949 StringRef CPUName;
950 StringRef ABIName;
951 getMipsCPUAndABI(Args, getToolChain(), CPUName, ABIName);
952
953 CmdArgs.push_back("-target-cpu");
954 CmdArgs.push_back(CPUName.data());
955
956 CmdArgs.push_back("-target-abi");
957 CmdArgs.push_back(ABIName.data());
958
959 StringRef FloatABI = getMipsFloatABI(D, Args);
960
Simon Atanasyane1d792f2013-01-10 12:36:19 +0000961 bool IsMips16 = Args.getLastArg(options::OPT_mips16) != NULL;
962
963 if (FloatABI == "soft" || (FloatABI == "hard" && IsMips16)) {
Eric Christophered734732010-03-02 02:41:08 +0000964 // Floating point operations and argument passing are soft.
Eric Christophered734732010-03-02 02:41:08 +0000965 CmdArgs.push_back("-msoft-float");
Akira Hatanakaad8d8a32012-03-23 23:07:09 +0000966 CmdArgs.push_back("-mfloat-abi");
967 CmdArgs.push_back("soft");
968
969 // FIXME: Note, this is a hack. We need to pass the selected float
970 // mode to the MipsTargetInfoBase to define appropriate macros there.
971 // Now it is the only method.
972 CmdArgs.push_back("-target-feature");
973 CmdArgs.push_back("+soft-float");
Simon Atanasyane1d792f2013-01-10 12:36:19 +0000974
975 if (FloatABI == "hard" && IsMips16) {
976 CmdArgs.push_back("-mllvm");
977 CmdArgs.push_back("-mips16-hard-float");
978 }
Akira Hatanakaad8d8a32012-03-23 23:07:09 +0000979 }
980 else if (FloatABI == "single") {
981 // Restrict the use of hardware floating-point
982 // instructions to 32-bit operations.
983 CmdArgs.push_back("-target-feature");
984 CmdArgs.push_back("+single-float");
985 }
986 else {
987 // Floating point operations and argument passing are hard.
Eric Christophered734732010-03-02 02:41:08 +0000988 assert(FloatABI == "hard" && "Invalid float abi!");
Akira Hatanakaad8d8a32012-03-23 23:07:09 +0000989 CmdArgs.push_back("-mfloat-abi");
990 CmdArgs.push_back("hard");
Eric Christophered734732010-03-02 02:41:08 +0000991 }
Simon Atanasyan0b273ef2012-07-05 14:19:39 +0000992
Simon Atanasyandc536f52012-07-05 18:51:43 +0000993 AddTargetFeature(Args, CmdArgs,
994 options::OPT_mips16, options::OPT_mno_mips16,
995 "mips16");
Simon Atanasyand797a852012-07-05 19:23:00 +0000996 AddTargetFeature(Args, CmdArgs,
997 options::OPT_mdsp, options::OPT_mno_dsp,
998 "dsp");
999 AddTargetFeature(Args, CmdArgs,
1000 options::OPT_mdspr2, options::OPT_mno_dspr2,
1001 "dspr2");
Simon Atanasyan9804b762012-08-27 20:55:56 +00001002
Simon Atanasyanbda07ac2012-12-01 18:27:21 +00001003 if (Arg *A = Args.getLastArg(options::OPT_mxgot, options::OPT_mno_xgot)) {
1004 if (A->getOption().matches(options::OPT_mxgot)) {
1005 CmdArgs.push_back("-mllvm");
1006 CmdArgs.push_back("-mxgot");
1007 }
1008 }
1009
Simon Atanasyan9804b762012-08-27 20:55:56 +00001010 if (Arg *A = Args.getLastArg(options::OPT_G)) {
Richard Smith1d489cf2012-11-01 04:30:05 +00001011 StringRef v = A->getValue();
Simon Atanasyan9804b762012-08-27 20:55:56 +00001012 CmdArgs.push_back("-mllvm");
1013 CmdArgs.push_back(Args.MakeArgString("-mips-ssection-threshold=" + v));
1014 A->claim();
1015 }
Eric Christophered734732010-03-02 02:41:08 +00001016}
1017
Hal Finkel02a84272012-06-11 22:35:19 +00001018/// getPPCTargetCPU - Get the (LLVM) name of the PowerPC cpu we are targeting.
1019static std::string getPPCTargetCPU(const ArgList &Args) {
1020 if (Arg *A = Args.getLastArg(options::OPT_mcpu_EQ)) {
Richard Smith1d489cf2012-11-01 04:30:05 +00001021 StringRef CPUName = A->getValue();
Hal Finkel02a84272012-06-11 22:35:19 +00001022
1023 if (CPUName == "native") {
1024 std::string CPU = llvm::sys::getHostCPUName();
1025 if (!CPU.empty() && CPU != "generic")
1026 return CPU;
1027 else
1028 return "";
1029 }
1030
1031 return llvm::StringSwitch<const char *>(CPUName)
1032 .Case("common", "generic")
1033 .Case("440", "440")
1034 .Case("440fp", "440")
1035 .Case("450", "450")
1036 .Case("601", "601")
1037 .Case("602", "602")
1038 .Case("603", "603")
1039 .Case("603e", "603e")
1040 .Case("603ev", "603ev")
1041 .Case("604", "604")
1042 .Case("604e", "604e")
1043 .Case("620", "620")
Bill Schmidt2821e182013-02-01 20:23:10 +00001044 .Case("630", "pwr3")
Hal Finkel02a84272012-06-11 22:35:19 +00001045 .Case("G3", "g3")
1046 .Case("7400", "7400")
1047 .Case("G4", "g4")
1048 .Case("7450", "7450")
1049 .Case("G4+", "g4+")
1050 .Case("750", "750")
1051 .Case("970", "970")
1052 .Case("G5", "g5")
1053 .Case("a2", "a2")
Hal Finkel5ccd3d02013-02-01 05:53:33 +00001054 .Case("a2q", "a2q")
Hal Finkel7de32962012-09-18 22:25:03 +00001055 .Case("e500mc", "e500mc")
1056 .Case("e5500", "e5500")
Bill Schmidt2821e182013-02-01 20:23:10 +00001057 .Case("power3", "pwr3")
1058 .Case("power4", "pwr4")
1059 .Case("power5", "pwr5")
1060 .Case("power5x", "pwr5x")
Hal Finkel02a84272012-06-11 22:35:19 +00001061 .Case("power6", "pwr6")
Bill Schmidt2821e182013-02-01 20:23:10 +00001062 .Case("power6x", "pwr6x")
Hal Finkel02a84272012-06-11 22:35:19 +00001063 .Case("power7", "pwr7")
Bill Schmidt2821e182013-02-01 20:23:10 +00001064 .Case("pwr3", "pwr3")
1065 .Case("pwr4", "pwr4")
1066 .Case("pwr5", "pwr5")
1067 .Case("pwr5x", "pwr5x")
1068 .Case("pwr6", "pwr6")
1069 .Case("pwr6x", "pwr6x")
1070 .Case("pwr7", "pwr7")
Hal Finkel02a84272012-06-11 22:35:19 +00001071 .Case("powerpc", "ppc")
1072 .Case("powerpc64", "ppc64")
1073 .Default("");
1074 }
1075
1076 return "";
1077}
1078
1079void Clang::AddPPCTargetArgs(const ArgList &Args,
1080 ArgStringList &CmdArgs) const {
1081 std::string TargetCPUName = getPPCTargetCPU(Args);
1082
1083 // LLVM may default to generating code for the native CPU,
1084 // but, like gcc, we default to a more generic option for
1085 // each architecture. (except on Darwin)
1086 llvm::Triple Triple = getToolChain().getTriple();
1087 if (TargetCPUName.empty() && !Triple.isOSDarwin()) {
1088 if (Triple.getArch() == llvm::Triple::ppc64)
1089 TargetCPUName = "ppc64";
1090 else
1091 TargetCPUName = "ppc";
1092 }
1093
1094 if (!TargetCPUName.empty()) {
1095 CmdArgs.push_back("-target-cpu");
1096 CmdArgs.push_back(Args.MakeArgString(TargetCPUName.c_str()));
1097 }
Bill Schmidt199402b2013-02-01 02:14:03 +00001098
1099 // Allow override of the Altivec feature.
Hal Finkelf4320ab2013-03-28 08:38:53 +00001100 AddTargetFeature(Args, CmdArgs,
1101 options::OPT_faltivec, options::OPT_fno_altivec,
1102 "altivec");
Hal Finkel3c6aaeb2013-02-01 18:44:19 +00001103
Hal Finkelfe6b2712013-03-30 13:47:44 +00001104 AddTargetFeature(Args, CmdArgs,
1105 options::OPT_mfprnd, options::OPT_mno_fprnd,
1106 "fprnd");
1107
Hal Finkelf4320ab2013-03-28 08:38:53 +00001108 // Note that gcc calls this mfcrf and LLVM calls this mfocrf.
1109 AddTargetFeature(Args, CmdArgs,
1110 options::OPT_mmfcrf, options::OPT_mno_mfcrf,
1111 "mfocrf");
1112
Hal Finkel829d1872013-03-28 13:51:36 +00001113 AddTargetFeature(Args, CmdArgs,
1114 options::OPT_mpopcntd, options::OPT_mno_popcntd,
1115 "popcntd");
1116
Hal Finkelf4320ab2013-03-28 08:38:53 +00001117 // It is really only possible to turn qpx off because turning qpx on is tied
1118 // to using the a2q CPU.
Hal Finkel3c6aaeb2013-02-01 18:44:19 +00001119 if (Args.hasFlag(options::OPT_mno_qpx, options::OPT_mqpx, false)) {
1120 CmdArgs.push_back("-target-feature");
1121 CmdArgs.push_back("-qpx");
1122 }
Hal Finkel02a84272012-06-11 22:35:19 +00001123}
1124
Tom Stellarde25d2f62013-04-01 20:56:53 +00001125/// Get the (LLVM) name of the R600 gpu we are targeting.
1126static std::string getR600TargetGPU(const ArgList &Args) {
1127 if (Arg *A = Args.getLastArg(options::OPT_mcpu_EQ)) {
1128 std::string GPUName = A->getValue();
1129 return llvm::StringSwitch<const char *>(GPUName)
1130 .Cases("rv610", "rv620", "rv630", "r600")
1131 .Cases("rv635", "rs780", "rs880", "r600")
1132 .Case("rv740", "rv770")
1133 .Case("palm", "cedar")
1134 .Cases("sumo", "sumo2", "redwood")
1135 .Case("hemlock", "cypress")
1136 .Case("aruba", "cayman")
1137 .Default(GPUName.c_str());
1138 }
1139 return "";
1140}
1141
1142void Clang::AddR600TargetArgs(const ArgList &Args,
1143 ArgStringList &CmdArgs) const {
1144 std::string TargetGPUName = getR600TargetGPU(Args);
1145 CmdArgs.push_back("-target-cpu");
1146 CmdArgs.push_back(Args.MakeArgString(TargetGPUName.c_str()));
1147}
1148
Bruno Cardoso Lopes9284d212010-11-09 17:21:19 +00001149void Clang::AddSparcTargetArgs(const ArgList &Args,
1150 ArgStringList &CmdArgs) const {
1151 const Driver &D = getToolChain().getDriver();
1152
1153 if (const Arg *A = Args.getLastArg(options::OPT_march_EQ)) {
Bruno Cardoso Lopes9284d212010-11-09 17:21:19 +00001154 CmdArgs.push_back("-target-cpu");
Richard Smith1d489cf2012-11-01 04:30:05 +00001155 CmdArgs.push_back(A->getValue());
Bruno Cardoso Lopes9284d212010-11-09 17:21:19 +00001156 }
1157
1158 // Select the float ABI as determined by -msoft-float, -mhard-float, and
Chris Lattner5f9e2722011-07-23 10:55:15 +00001159 StringRef FloatABI;
Bruno Cardoso Lopes9284d212010-11-09 17:21:19 +00001160 if (Arg *A = Args.getLastArg(options::OPT_msoft_float,
1161 options::OPT_mhard_float)) {
1162 if (A->getOption().matches(options::OPT_msoft_float))
1163 FloatABI = "soft";
1164 else if (A->getOption().matches(options::OPT_mhard_float))
1165 FloatABI = "hard";
1166 }
1167
1168 // If unspecified, choose the default based on the platform.
1169 if (FloatABI.empty()) {
1170 switch (getToolChain().getTriple().getOS()) {
1171 default:
1172 // Assume "soft", but warn the user we are guessing.
1173 FloatABI = "soft";
Chris Lattner5f9e2722011-07-23 10:55:15 +00001174 D.Diag(diag::warn_drv_assuming_mfloat_abi_is) << "soft";
Bruno Cardoso Lopes9284d212010-11-09 17:21:19 +00001175 break;
1176 }
1177 }
1178
1179 if (FloatABI == "soft") {
1180 // Floating point operations and argument passing are soft.
1181 //
1182 // FIXME: This changes CPP defines, we need -target-soft-float.
1183 CmdArgs.push_back("-msoft-float");
Bruno Cardoso Lopes9284d212010-11-09 17:21:19 +00001184 CmdArgs.push_back("-target-feature");
1185 CmdArgs.push_back("+soft-float");
1186 } else {
1187 assert(FloatABI == "hard" && "Invalid float abi!");
1188 CmdArgs.push_back("-mhard-float");
1189 }
1190}
1191
Chandler Carruth700d4e42013-01-13 11:46:33 +00001192static const char *getX86TargetCPU(const ArgList &Args,
1193 const llvm::Triple &Triple) {
1194 if (const Arg *A = Args.getLastArg(options::OPT_march_EQ)) {
1195 if (StringRef(A->getValue()) != "native")
1196 return A->getValue();
1197
1198 // FIXME: Reject attempts to use -march=native unless the target matches
1199 // the host.
1200 //
1201 // FIXME: We should also incorporate the detected target features for use
1202 // with -native.
1203 std::string CPU = llvm::sys::getHostCPUName();
1204 if (!CPU.empty() && CPU != "generic")
1205 return Args.MakeArgString(CPU);
1206 }
1207
1208 // Select the default CPU if none was given (or detection failed).
1209
1210 if (Triple.getArch() != llvm::Triple::x86_64 &&
1211 Triple.getArch() != llvm::Triple::x86)
1212 return 0; // This routine is only handling x86 targets.
1213
1214 bool Is64Bit = Triple.getArch() == llvm::Triple::x86_64;
1215
1216 // FIXME: Need target hooks.
1217 if (Triple.isOSDarwin())
1218 return Is64Bit ? "core2" : "yonah";
1219
1220 // Everything else goes to x86-64 in 64-bit mode.
1221 if (Is64Bit)
1222 return "x86-64";
1223
1224 if (Triple.getOSName().startswith("haiku"))
1225 return "i586";
1226 if (Triple.getOSName().startswith("openbsd"))
1227 return "i486";
1228 if (Triple.getOSName().startswith("bitrig"))
1229 return "i686";
1230 if (Triple.getOSName().startswith("freebsd"))
1231 return "i486";
1232 if (Triple.getOSName().startswith("netbsd"))
1233 return "i486";
1234 // All x86 devices running Android have core2 as their common
1235 // denominator. This makes a better choice than pentium4.
1236 if (Triple.getEnvironment() == llvm::Triple::Android)
1237 return "core2";
1238
1239 // Fallback to p4.
1240 return "pentium4";
1241}
1242
Daniel Dunbar6acda162009-09-09 22:33:08 +00001243void Clang::AddX86TargetArgs(const ArgList &Args,
1244 ArgStringList &CmdArgs) const {
Daniel Dunbare6ad3f92009-09-10 22:59:57 +00001245 if (!Args.hasFlag(options::OPT_mred_zone,
1246 options::OPT_mno_red_zone,
1247 true) ||
1248 Args.hasArg(options::OPT_mkernel) ||
1249 Args.hasArg(options::OPT_fapple_kext))
Daniel Dunbar66861e02009-11-20 22:21:36 +00001250 CmdArgs.push_back("-disable-red-zone");
Daniel Dunbare6ad3f92009-09-10 22:59:57 +00001251
Bob Wilsonf0c54562013-02-10 16:01:41 +00001252 // Default to avoid implicit floating-point for kernel/kext code, but allow
1253 // that to be overridden with -mno-soft-float.
1254 bool NoImplicitFloat = (Args.hasArg(options::OPT_mkernel) ||
1255 Args.hasArg(options::OPT_fapple_kext));
1256 if (Arg *A = Args.getLastArg(options::OPT_msoft_float,
1257 options::OPT_mno_soft_float,
1258 options::OPT_mno_implicit_float)) {
1259 const Option &O = A->getOption();
1260 NoImplicitFloat = (O.matches(options::OPT_mno_implicit_float) ||
1261 O.matches(options::OPT_msoft_float));
1262 }
1263 if (NoImplicitFloat)
Daniel Dunbar66861e02009-11-20 22:21:36 +00001264 CmdArgs.push_back("-no-implicit-float");
Daniel Dunbare6ad3f92009-09-10 22:59:57 +00001265
Chandler Carruth700d4e42013-01-13 11:46:33 +00001266 if (const char *CPUName = getX86TargetCPU(Args, getToolChain().getTriple())) {
Daniel Dunbar38b48af2009-12-18 06:30:12 +00001267 CmdArgs.push_back("-target-cpu");
Daniel Dunbarf86fedd2009-11-14 22:04:54 +00001268 CmdArgs.push_back(CPUName);
1269 }
1270
Eli Friedmand18eeca2011-07-02 00:34:19 +00001271 // The required algorithm here is slightly strange: the options are applied
1272 // in order (so -mno-sse -msse2 disables SSE3), but any option that gets
1273 // directly overridden later is ignored (so "-mno-sse -msse2 -mno-sse2 -msse"
1274 // is equivalent to "-mno-sse2 -msse"). The -cc1 handling deals with the
1275 // former correctly, but not the latter; handle directly-overridden
1276 // attributes here.
1277 llvm::StringMap<unsigned> PrevFeature;
1278 std::vector<const char*> Features;
Daniel Dunbarcdd96862009-11-25 11:53:23 +00001279 for (arg_iterator it = Args.filtered_begin(options::OPT_m_x86_Features_Group),
1280 ie = Args.filtered_end(); it != ie; ++it) {
Chris Lattner5f9e2722011-07-23 10:55:15 +00001281 StringRef Name = (*it)->getOption().getName();
Daniel Dunbar7e4953e2010-06-11 22:00:13 +00001282 (*it)->claim();
Daniel Dunbar6acda162009-09-09 22:33:08 +00001283
Daniel Dunbarcdd96862009-11-25 11:53:23 +00001284 // Skip over "-m".
Michael J. Spencerc6357102012-10-22 22:13:48 +00001285 assert(Name.startswith("m") && "Invalid feature name.");
1286 Name = Name.substr(1);
Daniel Dunbar6acda162009-09-09 22:33:08 +00001287
Daniel Dunbarcdd96862009-11-25 11:53:23 +00001288 bool IsNegative = Name.startswith("no-");
1289 if (IsNegative)
1290 Name = Name.substr(3);
Daniel Dunbar6acda162009-09-09 22:33:08 +00001291
Eli Friedmand18eeca2011-07-02 00:34:19 +00001292 unsigned& Prev = PrevFeature[Name];
1293 if (Prev)
1294 Features[Prev - 1] = 0;
1295 Prev = Features.size() + 1;
1296 Features.push_back(Args.MakeArgString((IsNegative ? "-" : "+") + Name));
1297 }
1298 for (unsigned i = 0; i < Features.size(); i++) {
1299 if (Features[i]) {
1300 CmdArgs.push_back("-target-feature");
1301 CmdArgs.push_back(Features[i]);
1302 }
Daniel Dunbar6acda162009-09-09 22:33:08 +00001303 }
1304}
1305
Matthew Curtis33c95f12012-12-06 17:49:03 +00001306static inline bool HasPICArg(const ArgList &Args) {
1307 return Args.hasArg(options::OPT_fPIC)
1308 || Args.hasArg(options::OPT_fpic);
1309}
1310
1311static Arg *GetLastSmallDataThresholdArg(const ArgList &Args) {
1312 return Args.getLastArg(options::OPT_G,
1313 options::OPT_G_EQ,
1314 options::OPT_msmall_data_threshold_EQ);
1315}
1316
1317static std::string GetHexagonSmallDataThresholdValue(const ArgList &Args) {
1318 std::string value;
1319 if (HasPICArg(Args))
1320 value = "0";
1321 else if (Arg *A = GetLastSmallDataThresholdArg(Args)) {
1322 value = A->getValue();
1323 A->claim();
1324 }
1325 return value;
1326}
1327
Tony Linthicum96319392011-12-12 21:14:55 +00001328void Clang::AddHexagonTargetArgs(const ArgList &Args,
1329 ArgStringList &CmdArgs) const {
1330 llvm::Triple Triple = getToolChain().getTriple();
1331
1332 CmdArgs.push_back("-target-cpu");
Matthew Curtis67814152012-12-06 14:16:43 +00001333 CmdArgs.push_back(Args.MakeArgString(
1334 "hexagon"
1335 + toolchains::Hexagon_TC::GetTargetCPU(Args)));
Tony Linthicum96319392011-12-12 21:14:55 +00001336 CmdArgs.push_back("-fno-signed-char");
Matthew Curtis1dbaef52012-12-07 13:52:44 +00001337 CmdArgs.push_back("-mqdsp6-compat");
1338 CmdArgs.push_back("-Wreturn-type");
Tony Linthicum96319392011-12-12 21:14:55 +00001339
Matthew Curtis33c95f12012-12-06 17:49:03 +00001340 std::string SmallDataThreshold = GetHexagonSmallDataThresholdValue(Args);
1341 if (!SmallDataThreshold.empty()) {
Tony Linthicum96319392011-12-12 21:14:55 +00001342 CmdArgs.push_back ("-mllvm");
Matthew Curtis33c95f12012-12-06 17:49:03 +00001343 CmdArgs.push_back(Args.MakeArgString(
1344 "-hexagon-small-data-threshold=" + SmallDataThreshold));
Tony Linthicum96319392011-12-12 21:14:55 +00001345 }
1346
Sirish Pande5f9688b2012-05-10 20:19:54 +00001347 if (!Args.hasArg(options::OPT_fno_short_enums))
1348 CmdArgs.push_back("-fshort-enums");
1349 if (Args.getLastArg(options::OPT_mieee_rnd_near)) {
1350 CmdArgs.push_back ("-mllvm");
1351 CmdArgs.push_back ("-enable-hexagon-ieee-rnd-near");
1352 }
Tony Linthicum96319392011-12-12 21:14:55 +00001353 CmdArgs.push_back ("-mllvm");
1354 CmdArgs.push_back ("-machine-sink-split=0");
1355}
1356
Eric Christopher88b7cf02011-08-19 00:30:14 +00001357static bool
John McCall260611a2012-06-20 06:18:46 +00001358shouldUseExceptionTablesForObjCExceptions(const ObjCRuntime &runtime,
Anders Carlsson525544d2011-02-28 00:44:51 +00001359 const llvm::Triple &Triple) {
1360 // We use the zero-cost exception tables for Objective-C if the non-fragile
1361 // ABI is enabled or when compiling for x86_64 and ARM on Snow Leopard and
1362 // later.
John McCall260611a2012-06-20 06:18:46 +00001363 if (runtime.isNonFragile())
Anders Carlsson525544d2011-02-28 00:44:51 +00001364 return true;
1365
Bob Wilson905c45f2011-10-14 05:03:44 +00001366 if (!Triple.isOSDarwin())
Anders Carlsson525544d2011-02-28 00:44:51 +00001367 return false;
1368
Eric Christopheraa7333c2011-07-02 00:20:22 +00001369 return (!Triple.isMacOSXVersionLT(10,5) &&
Anders Carlsson525544d2011-02-28 00:44:51 +00001370 (Triple.getArch() == llvm::Triple::x86_64 ||
Eric Christopher88b7cf02011-08-19 00:30:14 +00001371 Triple.getArch() == llvm::Triple::arm));
Anders Carlsson525544d2011-02-28 00:44:51 +00001372}
1373
Anders Carlsson15348ae2011-02-28 02:27:16 +00001374/// addExceptionArgs - Adds exception related arguments to the driver command
1375/// arguments. There's a master flag, -fexceptions and also language specific
1376/// flags to enable/disable C++ and Objective-C exceptions.
1377/// This makes it possible to for example disable C++ exceptions but enable
1378/// Objective-C exceptions.
1379static void addExceptionArgs(const ArgList &Args, types::ID InputType,
1380 const llvm::Triple &Triple,
Fariborz Jahanian15b77312012-04-04 18:28:00 +00001381 bool KernelOrKext,
John McCall260611a2012-06-20 06:18:46 +00001382 const ObjCRuntime &objcRuntime,
Anders Carlsson15348ae2011-02-28 02:27:16 +00001383 ArgStringList &CmdArgs) {
Chad Rosierafc4baa2012-03-26 22:04:46 +00001384 if (KernelOrKext) {
1385 // -mkernel and -fapple-kext imply no exceptions, so claim exception related
1386 // arguments now to avoid warnings about unused arguments.
1387 Args.ClaimAllArgs(options::OPT_fexceptions);
1388 Args.ClaimAllArgs(options::OPT_fno_exceptions);
1389 Args.ClaimAllArgs(options::OPT_fobjc_exceptions);
1390 Args.ClaimAllArgs(options::OPT_fno_objc_exceptions);
1391 Args.ClaimAllArgs(options::OPT_fcxx_exceptions);
1392 Args.ClaimAllArgs(options::OPT_fno_cxx_exceptions);
Anders Carlsson15348ae2011-02-28 02:27:16 +00001393 return;
Chad Rosierafc4baa2012-03-26 22:04:46 +00001394 }
Anders Carlsson15348ae2011-02-28 02:27:16 +00001395
1396 // Exceptions are enabled by default.
1397 bool ExceptionsEnabled = true;
1398
1399 // This keeps track of whether exceptions were explicitly turned on or off.
1400 bool DidHaveExplicitExceptionFlag = false;
1401
Rafael Espindolaf759df02009-10-01 13:33:33 +00001402 if (Arg *A = Args.getLastArg(options::OPT_fexceptions,
1403 options::OPT_fno_exceptions)) {
1404 if (A->getOption().matches(options::OPT_fexceptions))
Anders Carlsson15348ae2011-02-28 02:27:16 +00001405 ExceptionsEnabled = true;
Eric Christopher88b7cf02011-08-19 00:30:14 +00001406 else
Anders Carlsson15348ae2011-02-28 02:27:16 +00001407 ExceptionsEnabled = false;
1408
1409 DidHaveExplicitExceptionFlag = true;
Rafael Espindolaf759df02009-10-01 13:33:33 +00001410 }
Daniel Dunbar1a2cd4f2010-09-14 23:12:31 +00001411
Anders Carlsson15348ae2011-02-28 02:27:16 +00001412 bool ShouldUseExceptionTables = false;
Fariborz Jahanian85caf032009-10-01 20:30:46 +00001413
Anders Carlsson15348ae2011-02-28 02:27:16 +00001414 // Exception tables and cleanups can be enabled with -fexceptions even if the
1415 // language itself doesn't support exceptions.
1416 if (ExceptionsEnabled && DidHaveExplicitExceptionFlag)
1417 ShouldUseExceptionTables = true;
Daniel Dunbar1a2cd4f2010-09-14 23:12:31 +00001418
Daniel Dunbard47ea692011-03-17 23:28:31 +00001419 // Obj-C exceptions are enabled by default, regardless of -fexceptions. This
1420 // is not necessarily sensible, but follows GCC.
1421 if (types::isObjC(InputType) &&
Eric Christopher88b7cf02011-08-19 00:30:14 +00001422 Args.hasFlag(options::OPT_fobjc_exceptions,
Daniel Dunbard47ea692011-03-17 23:28:31 +00001423 options::OPT_fno_objc_exceptions,
1424 true)) {
1425 CmdArgs.push_back("-fobjc-exceptions");
Anders Carlsson15348ae2011-02-28 02:27:16 +00001426
Eric Christopher88b7cf02011-08-19 00:30:14 +00001427 ShouldUseExceptionTables |=
John McCall260611a2012-06-20 06:18:46 +00001428 shouldUseExceptionTablesForObjCExceptions(objcRuntime, Triple);
Anders Carlsson15348ae2011-02-28 02:27:16 +00001429 }
1430
1431 if (types::isCXX(InputType)) {
1432 bool CXXExceptionsEnabled = ExceptionsEnabled;
1433
Eric Christopher88b7cf02011-08-19 00:30:14 +00001434 if (Arg *A = Args.getLastArg(options::OPT_fcxx_exceptions,
1435 options::OPT_fno_cxx_exceptions,
Anders Carlsson15348ae2011-02-28 02:27:16 +00001436 options::OPT_fexceptions,
1437 options::OPT_fno_exceptions)) {
1438 if (A->getOption().matches(options::OPT_fcxx_exceptions))
1439 CXXExceptionsEnabled = true;
Chandler Carruth43f220f2011-02-28 07:25:18 +00001440 else if (A->getOption().matches(options::OPT_fno_cxx_exceptions))
Anders Carlsson15348ae2011-02-28 02:27:16 +00001441 CXXExceptionsEnabled = false;
1442 }
1443
1444 if (CXXExceptionsEnabled) {
1445 CmdArgs.push_back("-fcxx-exceptions");
1446
1447 ShouldUseExceptionTables = true;
1448 }
1449 }
1450
1451 if (ShouldUseExceptionTables)
1452 CmdArgs.push_back("-fexceptions");
Rafael Espindolaf759df02009-10-01 13:33:33 +00001453}
1454
Rafael Espindola61b1efe2011-05-02 17:43:32 +00001455static bool ShouldDisableCFI(const ArgList &Args,
1456 const ToolChain &TC) {
Rafael Espindola701ec8d2012-03-08 14:39:55 +00001457 bool Default = true;
Bob Wilson905c45f2011-10-14 05:03:44 +00001458 if (TC.getTriple().isOSDarwin()) {
Rafael Espindola97f6abb2011-05-17 16:26:17 +00001459 // The native darwin assembler doesn't support cfi directives, so
Rafael Espindolacb773922011-05-17 19:06:58 +00001460 // we disable them if we think the .s file will be passed to it.
Rafael Espindolaaf370e62013-03-18 18:10:27 +00001461 Default = TC.useIntegratedAs();
Rafael Espindola97f6abb2011-05-17 16:26:17 +00001462 }
Rafael Espindola701ec8d2012-03-08 14:39:55 +00001463 return !Args.hasFlag(options::OPT_fdwarf2_cfi_asm,
Eric Christopher27e2b982012-12-18 00:31:10 +00001464 options::OPT_fno_dwarf2_cfi_asm,
1465 Default);
Rafael Espindola61b1efe2011-05-02 17:43:32 +00001466}
1467
Ted Kremenekc06fcdf2013-03-12 17:02:12 +00001468static bool ShouldDisableDwarfDirectory(const ArgList &Args,
1469 const ToolChain &TC) {
Nick Lewyckyea523d72011-10-17 23:05:52 +00001470 bool UseDwarfDirectory = Args.hasFlag(options::OPT_fdwarf_directory_asm,
1471 options::OPT_fno_dwarf_directory_asm,
Rafael Espindolaaf370e62013-03-18 18:10:27 +00001472 TC.useIntegratedAs());
Nick Lewyckyea523d72011-10-17 23:05:52 +00001473 return !UseDwarfDirectory;
1474}
1475
Joerg Sonnenberger359cf922011-05-06 14:35:16 +00001476/// \brief Check whether the given input tree contains any compilation actions.
1477static bool ContainsCompileAction(const Action *A) {
Chris Lattner5f9e2722011-07-23 10:55:15 +00001478 if (isa<CompileJobAction>(A))
Joerg Sonnenberger359cf922011-05-06 14:35:16 +00001479 return true;
1480
1481 for (Action::const_iterator it = A->begin(), ie = A->end(); it != ie; ++it)
1482 if (ContainsCompileAction(*it))
1483 return true;
1484
1485 return false;
1486}
1487
1488/// \brief Check if -relax-all should be passed to the internal assembler.
1489/// This is done by default when compiling non-assembler source with -O0.
1490static bool UseRelaxAll(Compilation &C, const ArgList &Args) {
1491 bool RelaxDefault = true;
1492
1493 if (Arg *A = Args.getLastArg(options::OPT_O_Group))
1494 RelaxDefault = A->getOption().matches(options::OPT_O0);
1495
1496 if (RelaxDefault) {
1497 RelaxDefault = false;
1498 for (ActionList::const_iterator it = C.getActions().begin(),
1499 ie = C.getActions().end(); it != ie; ++it) {
1500 if (ContainsCompileAction(*it)) {
1501 RelaxDefault = true;
1502 break;
1503 }
1504 }
1505 }
1506
1507 return Args.hasFlag(options::OPT_mrelax_all, options::OPT_mno_relax_all,
1508 RelaxDefault);
1509}
1510
Peter Collingbourne52ca70d2013-04-09 04:35:11 +00001511SanitizerArgs::SanitizerArgs(const ToolChain &TC, const ArgList &Args)
Alexey Samsonov4bdc6042013-01-20 13:12:12 +00001512 : Kind(0), BlacklistFile(""), MsanTrackOrigins(false),
1513 AsanZeroBaseShadow(false) {
Alexey Samsonov3e335c12013-01-28 07:20:44 +00001514 unsigned AllKinds = 0; // All kinds of sanitizers that were turned on
1515 // at least once (possibly, disabled further).
Peter Collingbourne52ca70d2013-04-09 04:35:11 +00001516 const Driver &D = TC.getDriver();
Richard Smithc4dabad2012-11-05 22:04:41 +00001517 for (ArgList::const_iterator I = Args.begin(), E = Args.end(); I != E; ++I) {
Alexey Samsonov3325b162012-11-28 17:34:24 +00001518 unsigned Add, Remove;
1519 if (!parse(D, Args, *I, Add, Remove, true))
Richard Smithc4dabad2012-11-05 22:04:41 +00001520 continue;
Richard Smithc4dabad2012-11-05 22:04:41 +00001521 (*I)->claim();
Alexey Samsonovbb1071c2012-11-06 15:09:03 +00001522 Kind |= Add;
1523 Kind &= ~Remove;
Alexey Samsonov3e335c12013-01-28 07:20:44 +00001524 AllKinds |= Add;
Richard Smithc4dabad2012-11-05 22:04:41 +00001525 }
1526
Chad Rosier78d85b12013-01-29 23:31:22 +00001527 UbsanTrapOnError =
1528 Args.hasArg(options::OPT_fcatch_undefined_behavior) ||
1529 Args.hasFlag(options::OPT_fsanitize_undefined_trap_on_error,
1530 options::OPT_fno_sanitize_undefined_trap_on_error, false);
1531
1532 if (Args.hasArg(options::OPT_fcatch_undefined_behavior) &&
1533 !Args.hasFlag(options::OPT_fsanitize_undefined_trap_on_error,
1534 options::OPT_fno_sanitize_undefined_trap_on_error, true)) {
1535 D.Diag(diag::err_drv_argument_not_allowed_with)
1536 << "-fcatch-undefined-behavior"
1537 << "-fno-sanitize-undefined-trap-on-error";
1538 }
1539
1540 // Warn about undefined sanitizer options that require runtime support.
1541 if (UbsanTrapOnError && notAllowedWithTrap()) {
1542 if (Args.hasArg(options::OPT_fcatch_undefined_behavior))
1543 D.Diag(diag::err_drv_argument_not_allowed_with)
1544 << lastArgumentForKind(D, Args, NotAllowedWithTrap)
1545 << "-fcatch-undefined-behavior";
1546 else if (Args.hasFlag(options::OPT_fsanitize_undefined_trap_on_error,
1547 options::OPT_fno_sanitize_undefined_trap_on_error,
1548 false))
1549 D.Diag(diag::err_drv_argument_not_allowed_with)
1550 << lastArgumentForKind(D, Args, NotAllowedWithTrap)
1551 << "-fsanitize-undefined-trap-on-error";
1552 }
1553
Richard Smithc4dabad2012-11-05 22:04:41 +00001554 // Only one runtime library can be used at once.
Alexey Samsonovbb1071c2012-11-06 15:09:03 +00001555 bool NeedsAsan = needsAsanRt();
1556 bool NeedsTsan = needsTsanRt();
Evgeniy Stepanov99469f72012-12-05 13:37:12 +00001557 bool NeedsMsan = needsMsanRt();
Richard Smith05650372012-12-01 01:02:45 +00001558 if (NeedsAsan && NeedsTsan)
Richard Smithc4dabad2012-11-05 22:04:41 +00001559 D.Diag(diag::err_drv_argument_not_allowed_with)
Richard Smith05650372012-12-01 01:02:45 +00001560 << lastArgumentForKind(D, Args, NeedsAsanRt)
1561 << lastArgumentForKind(D, Args, NeedsTsanRt);
Evgeniy Stepanov99469f72012-12-05 13:37:12 +00001562 if (NeedsAsan && NeedsMsan)
1563 D.Diag(diag::err_drv_argument_not_allowed_with)
1564 << lastArgumentForKind(D, Args, NeedsAsanRt)
1565 << lastArgumentForKind(D, Args, NeedsMsanRt);
1566 if (NeedsTsan && NeedsMsan)
1567 D.Diag(diag::err_drv_argument_not_allowed_with)
1568 << lastArgumentForKind(D, Args, NeedsTsanRt)
1569 << lastArgumentForKind(D, Args, NeedsMsanRt);
Alexey Samsonov4d1a6e42012-11-29 22:36:21 +00001570
1571 // If -fsanitize contains extra features of ASan, it should also
Alexey Samsonov3e335c12013-01-28 07:20:44 +00001572 // explicitly contain -fsanitize=address (probably, turned off later in the
1573 // command line).
1574 if ((Kind & AddressFull) != 0 && (AllKinds & Address) == 0)
1575 D.Diag(diag::warn_drv_unused_sanitizer)
1576 << lastArgumentForKind(D, Args, AddressFull)
1577 << "-fsanitize=address";
Alexey Samsonov91ecfa62012-12-03 19:12:58 +00001578
1579 // Parse -f(no-)sanitize-blacklist options.
1580 if (Arg *BLArg = Args.getLastArg(options::OPT_fsanitize_blacklist,
1581 options::OPT_fno_sanitize_blacklist)) {
1582 if (BLArg->getOption().matches(options::OPT_fsanitize_blacklist)) {
1583 std::string BLPath = BLArg->getValue();
1584 bool BLExists = false;
1585 if (!llvm::sys::fs::exists(BLPath, BLExists) && BLExists)
1586 BlacklistFile = BLPath;
1587 else
1588 D.Diag(diag::err_drv_no_such_file) << BLPath;
1589 }
Alexey Samsonov24697b02013-02-19 11:25:29 +00001590 } else {
1591 // If no -fsanitize-blacklist option is specified, try to look up for
1592 // blacklist in the resource directory.
1593 std::string BLPath;
1594 bool BLExists = false;
1595 if (getDefaultBlacklistForKind(D, Kind, BLPath) &&
1596 !llvm::sys::fs::exists(BLPath, BLExists) && BLExists)
1597 BlacklistFile = BLPath;
Alexey Samsonov91ecfa62012-12-03 19:12:58 +00001598 }
Evgeniy Stepanov34ef11b2012-12-24 08:42:34 +00001599
1600 // Parse -f(no-)sanitize-memory-track-origins options.
Alexey Samsonov4bdc6042013-01-20 13:12:12 +00001601 if (NeedsMsan)
Evgeniy Stepanov34ef11b2012-12-24 08:42:34 +00001602 MsanTrackOrigins =
1603 Args.hasFlag(options::OPT_fsanitize_memory_track_origins,
1604 options::OPT_fno_sanitize_memory_track_origins,
1605 /* Default */false);
Alexey Samsonov4bdc6042013-01-20 13:12:12 +00001606
1607 // Parse -f(no-)sanitize-address-zero-base-shadow options.
Alexey Samsonov4a1ea522013-04-09 07:27:44 +00001608 if (NeedsAsan) {
1609 bool IsAndroid = (TC.getTriple().getEnvironment() == llvm::Triple::Android);
1610 bool ZeroBaseShadowDefault = IsAndroid;
Alexey Samsonov4bdc6042013-01-20 13:12:12 +00001611 AsanZeroBaseShadow =
Alexey Samsonov4a1ea522013-04-09 07:27:44 +00001612 Args.hasFlag(options::OPT_fsanitize_address_zero_base_shadow,
1613 options::OPT_fno_sanitize_address_zero_base_shadow,
1614 ZeroBaseShadowDefault);
1615 // Zero-base shadow is a requirement on Android.
1616 if (IsAndroid && !AsanZeroBaseShadow) {
1617 D.Diag(diag::err_drv_argument_not_allowed_with)
1618 << "-fno-sanitize-address-zero-base-shadow"
1619 << lastArgumentForKind(D, Args, Address);
1620 }
1621 }
Richard Smithc4dabad2012-11-05 22:04:41 +00001622}
1623
Alexey Samsonov86143042013-02-27 11:14:55 +00001624static void addSanitizerRTLinkFlagsLinux(
1625 const ToolChain &TC, const ArgList &Args, ArgStringList &CmdArgs,
Richard Smith76e6e132013-03-23 00:30:08 +00001626 const StringRef Sanitizer, bool BeforeLibStdCXX,
1627 bool ExportSymbols = true) {
Alexey Samsonov86143042013-02-27 11:14:55 +00001628 // Sanitizer runtime is located in the Linux library directory and
1629 // has name "libclang_rt.<Sanitizer>-<ArchName>.a".
1630 SmallString<128> LibSanitizer(TC.getDriver().ResourceDir);
1631 llvm::sys::path::append(
1632 LibSanitizer, "lib", "linux",
1633 (Twine("libclang_rt.") + Sanitizer + "-" + TC.getArchName() + ".a"));
Richard Smith61a574f2013-03-20 23:49:07 +00001634
Alexey Samsonov86143042013-02-27 11:14:55 +00001635 // Sanitizer runtime may need to come before -lstdc++ (or -lc++, libstdc++.a,
1636 // etc.) so that the linker picks custom versions of the global 'operator
1637 // new' and 'operator delete' symbols. We take the extreme (but simple)
1638 // strategy of inserting it at the front of the link command. It also
1639 // needs to be forced to end up in the executable, so wrap it in
1640 // whole-archive.
Richard Smith61a574f2013-03-20 23:49:07 +00001641 SmallVector<const char *, 3> LibSanitizerArgs;
1642 LibSanitizerArgs.push_back("-whole-archive");
1643 LibSanitizerArgs.push_back(Args.MakeArgString(LibSanitizer));
1644 LibSanitizerArgs.push_back("-no-whole-archive");
1645
1646 CmdArgs.insert(BeforeLibStdCXX ? CmdArgs.begin() : CmdArgs.end(),
1647 LibSanitizerArgs.begin(), LibSanitizerArgs.end());
1648
Alexey Samsonov86143042013-02-27 11:14:55 +00001649 CmdArgs.push_back("-lpthread");
1650 CmdArgs.push_back("-ldl");
Richard Smith76e6e132013-03-23 00:30:08 +00001651
1652 // If possible, use a dynamic symbols file to export the symbols from the
1653 // runtime library. If we can't do so, use -export-dynamic instead to export
1654 // all symbols from the binary.
1655 if (ExportSymbols) {
1656 if (llvm::sys::fs::exists(LibSanitizer + ".syms"))
1657 CmdArgs.push_back(
1658 Args.MakeArgString("--dynamic-list=" + LibSanitizer + ".syms"));
1659 else
1660 CmdArgs.push_back("-export-dynamic");
1661 }
Alexey Samsonov86143042013-02-27 11:14:55 +00001662}
1663
Kostya Serebryanydff466c2011-11-30 01:39:16 +00001664/// If AddressSanitizer is enabled, add appropriate linker flags (Linux).
1665/// This needs to be called before we add the C run-time (malloc, etc).
1666static void addAsanRTLinux(const ToolChain &TC, const ArgList &Args,
Kostya Serebryany7b5f1012011-12-06 19:18:44 +00001667 ArgStringList &CmdArgs) {
Logan Chien94a71422012-09-02 09:30:11 +00001668 if(TC.getTriple().getEnvironment() == llvm::Triple::Android) {
Evgeniy Stepanov8ba75412012-09-12 09:09:08 +00001669 SmallString<128> LibAsan(TC.getDriver().ResourceDir);
1670 llvm::sys::path::append(LibAsan, "lib", "linux",
1671 (Twine("libclang_rt.asan-") +
1672 TC.getArchName() + "-android.so"));
Matt Beaumont-Gay45b27382012-12-04 21:18:26 +00001673 CmdArgs.insert(CmdArgs.begin(), Args.MakeArgString(LibAsan));
Evgeniy Stepanova6ddc022012-04-25 08:59:22 +00001674 } else {
1675 if (!Args.hasArg(options::OPT_shared)) {
Alexey Samsonov86143042013-02-27 11:14:55 +00001676 addSanitizerRTLinkFlagsLinux(TC, Args, CmdArgs, "asan", true);
Evgeniy Stepanova6ddc022012-04-25 08:59:22 +00001677 }
1678 }
Kostya Serebryanydff466c2011-11-30 01:39:16 +00001679}
1680
Kostya Serebryanyf7efb0e2012-05-16 06:36:00 +00001681/// If ThreadSanitizer is enabled, add appropriate linker flags (Linux).
1682/// This needs to be called before we add the C run-time (malloc, etc).
1683static void addTsanRTLinux(const ToolChain &TC, const ArgList &Args,
1684 ArgStringList &CmdArgs) {
Kostya Serebryanyf7efb0e2012-05-16 06:36:00 +00001685 if (!Args.hasArg(options::OPT_shared)) {
Alexey Samsonov86143042013-02-27 11:14:55 +00001686 addSanitizerRTLinkFlagsLinux(TC, Args, CmdArgs, "tsan", true);
Kostya Serebryanyf7efb0e2012-05-16 06:36:00 +00001687 }
1688}
1689
Evgeniy Stepanov09ccf392012-12-03 13:20:43 +00001690/// If MemorySanitizer is enabled, add appropriate linker flags (Linux).
1691/// This needs to be called before we add the C run-time (malloc, etc).
1692static void addMsanRTLinux(const ToolChain &TC, const ArgList &Args,
1693 ArgStringList &CmdArgs) {
1694 if (!Args.hasArg(options::OPT_shared)) {
Alexey Samsonov86143042013-02-27 11:14:55 +00001695 addSanitizerRTLinkFlagsLinux(TC, Args, CmdArgs, "msan", true);
Evgeniy Stepanov09ccf392012-12-03 13:20:43 +00001696 }
1697}
1698
Richard Smith4def70d2012-10-09 19:52:38 +00001699/// If UndefinedBehaviorSanitizer is enabled, add appropriate linker flags
1700/// (Linux).
1701static void addUbsanRTLinux(const ToolChain &TC, const ArgList &Args,
Richard Smith61a574f2013-03-20 23:49:07 +00001702 ArgStringList &CmdArgs, bool IsCXX,
1703 bool HasOtherSanitizerRt) {
1704 if (Args.hasArg(options::OPT_shared))
1705 return;
1706
1707 // Need a copy of sanitizer_common. This could come from another sanitizer
1708 // runtime; if we're not including one, include our own copy.
1709 if (!HasOtherSanitizerRt)
Richard Smith76e6e132013-03-23 00:30:08 +00001710 addSanitizerRTLinkFlagsLinux(TC, Args, CmdArgs, "san", true, false);
Richard Smith61a574f2013-03-20 23:49:07 +00001711
Alexey Samsonov86143042013-02-27 11:14:55 +00001712 addSanitizerRTLinkFlagsLinux(TC, Args, CmdArgs, "ubsan", false);
Richard Smith61a574f2013-03-20 23:49:07 +00001713
1714 // Only include the bits of the runtime which need a C++ ABI library if
1715 // we're linking in C++ mode.
1716 if (IsCXX)
1717 addSanitizerRTLinkFlagsLinux(TC, Args, CmdArgs, "ubsan_cxx", false);
Richard Smith4def70d2012-10-09 19:52:38 +00001718}
1719
Rafael Espindola6af27ec2011-12-14 21:02:23 +00001720static bool shouldUseFramePointer(const ArgList &Args,
1721 const llvm::Triple &Triple) {
1722 if (Arg *A = Args.getLastArg(options::OPT_fno_omit_frame_pointer,
1723 options::OPT_fomit_frame_pointer))
1724 return A->getOption().matches(options::OPT_fno_omit_frame_pointer);
1725
Rafael Espindolaa2a17892011-12-14 21:50:24 +00001726 // Don't use a frame pointer on linux x86 and x86_64 if optimizing.
Rafael Espindola6af27ec2011-12-14 21:02:23 +00001727 if ((Triple.getArch() == llvm::Triple::x86_64 ||
1728 Triple.getArch() == llvm::Triple::x86) &&
1729 Triple.getOS() == llvm::Triple::Linux) {
1730 if (Arg *A = Args.getLastArg(options::OPT_O_Group))
1731 if (!A->getOption().matches(options::OPT_O0))
1732 return false;
1733 }
1734
1735 return true;
1736}
1737
Eric Christopherd3e22df2013-04-03 01:58:53 +00001738static bool shouldUseLeafFramePointer(const ArgList &Args,
1739 const llvm::Triple &Triple) {
1740 if (Arg *A = Args.getLastArg(options::OPT_mno_omit_leaf_frame_pointer,
1741 options::OPT_momit_leaf_frame_pointer))
1742 return A->getOption().matches(options::OPT_mno_omit_leaf_frame_pointer);
1743
1744 // Don't use a leaf frame pointer on linux x86 and x86_64 if optimizing.
1745 if ((Triple.getArch() == llvm::Triple::x86_64 ||
1746 Triple.getArch() == llvm::Triple::x86) &&
1747 Triple.getOS() == llvm::Triple::Linux) {
1748 if (Arg *A = Args.getLastArg(options::OPT_O_Group))
1749 if (!A->getOption().matches(options::OPT_O0))
1750 return false;
1751 }
1752
1753 return true;
1754}
1755
Chandler Carruthd566df62012-12-17 21:40:04 +00001756/// If the PWD environment variable is set, add a CC1 option to specify the
1757/// debug compilation directory.
1758static void addDebugCompDirArg(const ArgList &Args, ArgStringList &CmdArgs) {
1759 if (const char *pwd = ::getenv("PWD")) {
1760 // GCC also verifies that stat(pwd) and stat(".") have the same inode
1761 // number. Not doing those because stats are slow, but we could.
1762 if (llvm::sys::path::is_absolute(pwd)) {
1763 std::string CompDir = pwd;
1764 CmdArgs.push_back("-fdebug-compilation-dir");
1765 CmdArgs.push_back(Args.MakeArgString(CompDir));
1766 }
1767 }
1768}
1769
Eric Christopher80190392013-02-22 20:12:52 +00001770static const char *SplitDebugName(const ArgList &Args,
1771 const InputInfoList &Inputs) {
1772 Arg *FinalOutput = Args.getLastArg(options::OPT_o);
1773 if (FinalOutput && Args.hasArg(options::OPT_c)) {
1774 SmallString<128> T(FinalOutput->getValue());
1775 llvm::sys::path::replace_extension(T, "dwo");
1776 return Args.MakeArgString(T);
1777 } else {
1778 // Use the compilation dir.
1779 SmallString<128> T(Args.getLastArgValue(options::OPT_fdebug_compilation_dir));
1780 SmallString<128> F(llvm::sys::path::stem(Inputs[0].getBaseInput()));
1781 llvm::sys::path::replace_extension(F, "dwo");
1782 T += F;
1783 return Args.MakeArgString(F);
1784 }
1785}
1786
1787static void SplitDebugInfo(const ToolChain &TC, Compilation &C,
1788 const Tool &T, const JobAction &JA,
1789 const ArgList &Args, const InputInfo &Output,
1790 const char *OutFile) {
Eric Christopher59320e72013-02-21 22:35:01 +00001791 ArgStringList ExtractArgs;
1792 ExtractArgs.push_back("--extract-dwo");
1793
1794 ArgStringList StripArgs;
1795 StripArgs.push_back("--strip-dwo");
1796
1797 // Grabbing the output of the earlier compile step.
1798 StripArgs.push_back(Output.getFilename());
1799 ExtractArgs.push_back(Output.getFilename());
Eric Christopher59320e72013-02-21 22:35:01 +00001800 ExtractArgs.push_back(OutFile);
1801
1802 const char *Exec =
Eric Christopher80190392013-02-22 20:12:52 +00001803 Args.MakeArgString(TC.GetProgramPath("objcopy"));
Eric Christopher59320e72013-02-21 22:35:01 +00001804
1805 // First extract the dwo sections.
Eric Christopher80190392013-02-22 20:12:52 +00001806 C.addCommand(new Command(JA, T, Exec, ExtractArgs));
Eric Christopher59320e72013-02-21 22:35:01 +00001807
1808 // Then remove them from the original .o file.
Eric Christopher80190392013-02-22 20:12:52 +00001809 C.addCommand(new Command(JA, T, Exec, StripArgs));
Eric Christopher59320e72013-02-21 22:35:01 +00001810}
1811
Daniel Dunbar47ac7d22009-03-18 06:00:36 +00001812void Clang::ConstructJob(Compilation &C, const JobAction &JA,
Daniel Dunbar871adcf2009-03-18 07:06:02 +00001813 const InputInfo &Output,
Daniel Dunbar62cf6012009-03-18 06:07:59 +00001814 const InputInfoList &Inputs,
Daniel Dunbar1d460332009-03-18 10:01:51 +00001815 const ArgList &Args,
Daniel Dunbar47ac7d22009-03-18 06:00:36 +00001816 const char *LinkingOutput) const {
Daniel Dunbar0a80ba72010-03-20 04:52:14 +00001817 bool KernelOrKext = Args.hasArg(options::OPT_mkernel,
1818 options::OPT_fapple_kext);
Daniel Dunbaree788e72009-12-21 18:54:17 +00001819 const Driver &D = getToolChain().getDriver();
Daniel Dunbarb488c1d2009-03-18 08:07:30 +00001820 ArgStringList CmdArgs;
1821
Daniel Dunbar077ba6a2009-03-31 20:53:55 +00001822 assert(Inputs.size() == 1 && "Unable to handle multiple inputs.");
1823
Daniel Dunbar8ff5b282009-12-11 23:00:49 +00001824 // Invoke ourselves in -cc1 mode.
1825 //
1826 // FIXME: Implement custom jobs for internal actions.
1827 CmdArgs.push_back("-cc1");
1828
Daniel Dunbardd4fe002009-10-30 18:12:20 +00001829 // Add the "effective" target triple.
Daniel Dunbaraf07f932009-03-31 17:35:15 +00001830 CmdArgs.push_back("-triple");
Daniel Dunbar00577ad2010-08-23 22:35:37 +00001831 std::string TripleStr = getToolChain().ComputeEffectiveClangTriple(Args);
Daniel Dunbardd4fe002009-10-30 18:12:20 +00001832 CmdArgs.push_back(Args.MakeArgString(TripleStr));
Daniel Dunbar728a5122009-09-10 06:49:20 +00001833
Daniel Dunbardd4fe002009-10-30 18:12:20 +00001834 // Select the appropriate action.
John McCall260611a2012-06-20 06:18:46 +00001835 RewriteKind rewriteKind = RK_None;
Fariborz Jahaniane982cc02012-04-04 18:50:28 +00001836
Daniel Dunbar1d460332009-03-18 10:01:51 +00001837 if (isa<AnalyzeJobAction>(JA)) {
1838 assert(JA.getType() == types::TY_Plist && "Invalid output type.");
1839 CmdArgs.push_back("-analyze");
Ted Kremenek30660a82012-03-06 20:06:33 +00001840 } else if (isa<MigrateJobAction>(JA)) {
1841 CmdArgs.push_back("-migrate");
Daniel Dunbar1d460332009-03-18 10:01:51 +00001842 } else if (isa<PreprocessJobAction>(JA)) {
Daniel Dunbarcd8e4c42009-03-30 06:36:42 +00001843 if (Output.getType() == types::TY_Dependencies)
1844 CmdArgs.push_back("-Eonly");
Fariborz Jahanian51be73d2013-03-18 19:41:18 +00001845 else {
Daniel Dunbarcd8e4c42009-03-30 06:36:42 +00001846 CmdArgs.push_back("-E");
Fariborz Jahanian51be73d2013-03-18 19:41:18 +00001847 if (Args.hasArg(options::OPT_rewrite_objc) &&
1848 !Args.hasArg(options::OPT_g_Group))
1849 CmdArgs.push_back("-P");
1850 }
Daniel Dunbar8767cbc2010-02-03 03:07:56 +00001851 } else if (isa<AssembleJobAction>(JA)) {
1852 CmdArgs.push_back("-emit-obj");
Daniel Dunbar99298002010-05-27 06:18:05 +00001853
Joerg Sonnenberger359cf922011-05-06 14:35:16 +00001854 if (UseRelaxAll(C, Args))
Daniel Dunbar99298002010-05-27 06:18:05 +00001855 CmdArgs.push_back("-mrelax-all");
Daniel Dunbarca0e0542010-08-24 16:47:49 +00001856
Daniel Dunbarfcec10b2010-10-18 22:36:15 +00001857 // When using an integrated assembler, translate -Wa, and -Xassembler
1858 // options.
1859 for (arg_iterator it = Args.filtered_begin(options::OPT_Wa_COMMA,
1860 options::OPT_Xassembler),
1861 ie = Args.filtered_end(); it != ie; ++it) {
1862 const Arg *A = *it;
1863 A->claim();
1864
1865 for (unsigned i = 0, e = A->getNumValues(); i != e; ++i) {
Richard Smith1d489cf2012-11-01 04:30:05 +00001866 StringRef Value = A->getValue(i);
Daniel Dunbarfcec10b2010-10-18 22:36:15 +00001867
1868 if (Value == "-force_cpusubtype_ALL") {
1869 // Do nothing, this is the default and we don't support anything else.
Daniel Dunbarb14eed02010-10-28 20:36:23 +00001870 } else if (Value == "-L") {
Daniel Dunbar96932322011-03-28 22:49:28 +00001871 CmdArgs.push_back("-msave-temp-labels");
Joerg Sonnenberger46a49392011-05-19 20:46:39 +00001872 } else if (Value == "--fatal-warnings") {
Joerg Sonnenbergerd7933502011-05-19 18:42:29 +00001873 CmdArgs.push_back("-mllvm");
1874 CmdArgs.push_back("-fatal-assembler-warnings");
Nick Lewyckyc3b90142011-06-21 00:14:18 +00001875 } else if (Value == "--noexecstack") {
1876 CmdArgs.push_back("-mnoexecstack");
Daniel Dunbarfcec10b2010-10-18 22:36:15 +00001877 } else {
Chris Lattner5f9e2722011-07-23 10:55:15 +00001878 D.Diag(diag::err_drv_unsupported_option_argument)
Daniel Dunbarfcec10b2010-10-18 22:36:15 +00001879 << A->getOption().getName() << Value;
1880 }
1881 }
1882 }
Daniel Dunbard02bba82010-11-19 16:23:35 +00001883
1884 // Also ignore explicit -force_cpusubtype_ALL option.
1885 (void) Args.hasArg(options::OPT_force__cpusubtype__ALL);
Daniel Dunbar1d460332009-03-18 10:01:51 +00001886 } else if (isa<PrecompileJobAction>(JA)) {
Argyrios Kyrtzidise5c35372010-08-11 23:27:58 +00001887 // Use PCH if the user requested it.
Daniel Dunbar0ebd9322009-10-15 20:02:44 +00001888 bool UsePCH = D.CCCUsePCH;
Daniel Dunbar0ebd9322009-10-15 20:02:44 +00001889
Aaron Ballman761322b2012-07-31 01:21:00 +00001890 if (JA.getType() == types::TY_Nothing)
1891 CmdArgs.push_back("-fsyntax-only");
1892 else if (UsePCH)
Douglas Gregordf91ef32009-04-18 00:34:01 +00001893 CmdArgs.push_back("-emit-pch");
1894 else
1895 CmdArgs.push_back("-emit-pth");
Daniel Dunbar1d460332009-03-18 10:01:51 +00001896 } else {
1897 assert(isa<CompileJobAction>(JA) && "Invalid action for clang tool.");
Daniel Dunbarc21c4852009-04-08 23:54:23 +00001898
Daniel Dunbar1d460332009-03-18 10:01:51 +00001899 if (JA.getType() == types::TY_Nothing) {
1900 CmdArgs.push_back("-fsyntax-only");
Daniel Dunbar6c6424b2010-06-07 23:28:45 +00001901 } else if (JA.getType() == types::TY_LLVM_IR ||
1902 JA.getType() == types::TY_LTO_IR) {
Daniel Dunbar1d460332009-03-18 10:01:51 +00001903 CmdArgs.push_back("-emit-llvm");
Daniel Dunbar6c6424b2010-06-07 23:28:45 +00001904 } else if (JA.getType() == types::TY_LLVM_BC ||
1905 JA.getType() == types::TY_LTO_BC) {
Daniel Dunbar1d460332009-03-18 10:01:51 +00001906 CmdArgs.push_back("-emit-llvm-bc");
1907 } else if (JA.getType() == types::TY_PP_Asm) {
Daniel Dunbare3b8d072009-09-17 00:47:53 +00001908 CmdArgs.push_back("-S");
Daniel Dunbar5915fbf2009-09-01 16:57:46 +00001909 } else if (JA.getType() == types::TY_AST) {
1910 CmdArgs.push_back("-emit-pch");
Douglas Gregorc544ba02013-03-27 16:47:18 +00001911 } else if (JA.getType() == types::TY_ModuleFile) {
1912 CmdArgs.push_back("-module-file-info");
Daniel Dunbar64952502010-02-11 03:16:21 +00001913 } else if (JA.getType() == types::TY_RewrittenObjC) {
1914 CmdArgs.push_back("-rewrite-objc");
John McCall260611a2012-06-20 06:18:46 +00001915 rewriteKind = RK_NonFragile;
Fariborz Jahanian582b3952012-04-02 15:59:19 +00001916 } else if (JA.getType() == types::TY_RewrittenLegacyObjC) {
1917 CmdArgs.push_back("-rewrite-objc");
John McCall260611a2012-06-20 06:18:46 +00001918 rewriteKind = RK_Fragile;
Daniel Dunbar64952502010-02-11 03:16:21 +00001919 } else {
1920 assert(JA.getType() == types::TY_PP_Asm &&
1921 "Unexpected output type!");
Daniel Dunbar1d460332009-03-18 10:01:51 +00001922 }
Daniel Dunbarb488c1d2009-03-18 08:07:30 +00001923 }
1924
Daniel Dunbar1d460332009-03-18 10:01:51 +00001925 // The make clang go fast button.
1926 CmdArgs.push_back("-disable-free");
1927
John McCallb689afb2010-02-13 03:50:24 +00001928 // Disable the verification pass in -asserts builds.
1929#ifdef NDEBUG
1930 CmdArgs.push_back("-disable-llvm-verifier");
1931#endif
1932
Daniel Dunbarc9abc042009-04-08 05:11:16 +00001933 // Set the main file name, so that debug info works even with
1934 // -save-temps.
1935 CmdArgs.push_back("-main-file-name");
Bob Wilson66b8a662012-11-23 06:14:39 +00001936 CmdArgs.push_back(getBaseInputName(Args, Inputs));
Daniel Dunbarc9abc042009-04-08 05:11:16 +00001937
Daniel Dunbar3bbc7532009-04-08 18:03:55 +00001938 // Some flags which affect the language (via preprocessor
Bob Wilson66b8a662012-11-23 06:14:39 +00001939 // defines).
Daniel Dunbar3bbc7532009-04-08 18:03:55 +00001940 if (Args.hasArg(options::OPT_static))
1941 CmdArgs.push_back("-static-define");
1942
Daniel Dunbar1d460332009-03-18 10:01:51 +00001943 if (isa<AnalyzeJobAction>(JA)) {
Ted Kremenekb8bb3e72009-09-25 05:55:59 +00001944 // Enable region store model by default.
1945 CmdArgs.push_back("-analyzer-store=region");
1946
Ted Kremenekb40d06d2009-12-07 22:26:14 +00001947 // Treat blocks as analysis entry points.
1948 CmdArgs.push_back("-analyzer-opt-analyze-nested-blocks");
1949
Ted Kremenek51885072011-03-24 00:28:47 +00001950 CmdArgs.push_back("-analyzer-eagerly-assume");
1951
Daniel Dunbar1d460332009-03-18 10:01:51 +00001952 // Add default argument set.
Daniel Dunbard8fc0f22009-05-22 00:38:15 +00001953 if (!Args.hasArg(options::OPT__analyzer_no_default_checks)) {
Argyrios Kyrtzidis027a6ab2011-02-15 07:42:33 +00001954 CmdArgs.push_back("-analyzer-checker=core");
Ted Kremenek51885072011-03-24 00:28:47 +00001955
Argyrios Kyrtzidis027a6ab2011-02-15 07:42:33 +00001956 if (getToolChain().getTriple().getOS() != llvm::Triple::Win32)
1957 CmdArgs.push_back("-analyzer-checker=unix");
Ted Kremenek51885072011-03-24 00:28:47 +00001958
Argyrios Kyrtzidis027a6ab2011-02-15 07:42:33 +00001959 if (getToolChain().getTriple().getVendor() == llvm::Triple::Apple)
Ted Kremenek51885072011-03-24 00:28:47 +00001960 CmdArgs.push_back("-analyzer-checker=osx");
Ted Kremeneka8180e52012-01-20 06:00:17 +00001961
1962 CmdArgs.push_back("-analyzer-checker=deadcode");
Ted Kremenek8dc05062012-01-26 02:27:38 +00001963
Jordan Rosee449edc2013-04-05 17:55:07 +00001964 if (types::isCXX(Inputs[0].getType()))
1965 CmdArgs.push_back("-analyzer-checker=cplusplus");
1966
Ted Kremenek8dc05062012-01-26 02:27:38 +00001967 // Enable the following experimental checkers for testing.
Ted Kremenek8dc05062012-01-26 02:27:38 +00001968 CmdArgs.push_back("-analyzer-checker=security.insecureAPI.UncheckedReturn");
1969 CmdArgs.push_back("-analyzer-checker=security.insecureAPI.getpw");
1970 CmdArgs.push_back("-analyzer-checker=security.insecureAPI.gets");
1971 CmdArgs.push_back("-analyzer-checker=security.insecureAPI.mktemp");
1972 CmdArgs.push_back("-analyzer-checker=security.insecureAPI.mkstemp");
1973 CmdArgs.push_back("-analyzer-checker=security.insecureAPI.vfork");
Daniel Dunbard8fc0f22009-05-22 00:38:15 +00001974 }
Daniel Dunbarc21c4852009-04-08 23:54:23 +00001975
Daniel Dunbard8fc0f22009-05-22 00:38:15 +00001976 // Set the output format. The default is plist, for (lame) historical
1977 // reasons.
1978 CmdArgs.push_back("-analyzer-output");
1979 if (Arg *A = Args.getLastArg(options::OPT__analyzer_output))
Richard Smith1d489cf2012-11-01 04:30:05 +00001980 CmdArgs.push_back(A->getValue());
Daniel Dunbard8fc0f22009-05-22 00:38:15 +00001981 else
1982 CmdArgs.push_back("plist");
Daniel Dunbar1d460332009-03-18 10:01:51 +00001983
Ted Kremenek0647a7b2010-03-22 22:32:05 +00001984 // Disable the presentation of standard compiler warnings when
1985 // using --analyze. We only want to show static analyzer diagnostics
1986 // or frontend errors.
1987 CmdArgs.push_back("-w");
1988
Daniel Dunbar1d460332009-03-18 10:01:51 +00001989 // Add -Xanalyzer arguments when running as analyzer.
1990 Args.AddAllArgValues(CmdArgs, options::OPT_Xanalyzer);
Mike Stump1eb44332009-09-09 15:08:12 +00001991 }
1992
Daniel Dunbare2fd6642009-09-10 01:21:12 +00001993 CheckCodeGenerationOptions(D, Args);
1994
Peter Collingbourne52ca70d2013-04-09 04:35:11 +00001995 bool PIE = getToolChain().isPIEDefault();
1996 bool PIC = PIE || getToolChain().isPICDefault();
Chandler Carruth7ce816a2012-11-19 03:52:03 +00001997 bool IsPICLevelTwo = PIC;
Peter Collingbourne52ca70d2013-04-09 04:35:11 +00001998
Alexey Samsonovdb68e5a2013-04-09 12:28:19 +00001999 // For the PIC and PIE flag options, this logic is different from the
2000 // legacy logic in very old versions of GCC, as that logic was just
2001 // a bug no one had ever fixed. This logic is both more rational and
2002 // consistent with GCC's new logic now that the bugs are fixed. The last
2003 // argument relating to either PIC or PIE wins, and no other argument is
2004 // used. If the last argument is any flavor of the '-fno-...' arguments,
2005 // both PIC and PIE are disabled. Any PIE option implicitly enables PIC
2006 // at the same level.
2007 Arg *LastPICArg =Args.getLastArg(options::OPT_fPIC, options::OPT_fno_PIC,
2008 options::OPT_fpic, options::OPT_fno_pic,
2009 options::OPT_fPIE, options::OPT_fno_PIE,
2010 options::OPT_fpie, options::OPT_fno_pie);
Chandler Carruth7ce816a2012-11-19 03:52:03 +00002011 // Check whether the tool chain trumps the PIC-ness decision. If the PIC-ness
2012 // is forced, then neither PIC nor PIE flags will have no effect.
Peter Collingbourne52ca70d2013-04-09 04:35:11 +00002013 if (!getToolChain().isPICDefaultForced()) {
Alexey Samsonovdb68e5a2013-04-09 12:28:19 +00002014 if (LastPICArg) {
2015 Option O = LastPICArg->getOption();
Peter Collingbourne52ca70d2013-04-09 04:35:11 +00002016 if (O.matches(options::OPT_fPIC) || O.matches(options::OPT_fpic) ||
2017 O.matches(options::OPT_fPIE) || O.matches(options::OPT_fpie)) {
2018 PIE = O.matches(options::OPT_fPIE) || O.matches(options::OPT_fpie);
2019 PIC = PIE || O.matches(options::OPT_fPIC) ||
2020 O.matches(options::OPT_fpic);
2021 IsPICLevelTwo = O.matches(options::OPT_fPIE) ||
2022 O.matches(options::OPT_fPIC);
2023 } else {
2024 PIE = PIC = false;
2025 }
2026 }
Chandler Carruth5e219cf2012-04-08 16:40:35 +00002027 }
Chandler Carruth7ce816a2012-11-19 03:52:03 +00002028
2029 // Inroduce a Darwin-specific hack. If the default is PIC but the flags
2030 // specified while enabling PIC enabled level 1 PIC, just force it back to
2031 // level 2 PIC instead. This matches the behavior of Darwin GCC (based on my
2032 // informal testing).
2033 if (PIC && getToolChain().getTriple().isOSDarwin())
2034 IsPICLevelTwo |= getToolChain().isPICDefault();
2035
Chandler Carruth5e219cf2012-04-08 16:40:35 +00002036 // Note that these flags are trump-cards. Regardless of the order w.r.t. the
2037 // PIC or PIE options above, if these show up, PIC is disabled.
Daniel Dunbar7a0c0642012-10-15 22:23:53 +00002038 llvm::Triple Triple(TripleStr);
Eric Christopher6c7db892013-02-18 01:16:37 +00002039 if (KernelOrKext &&
Daniel Dunbar7a0c0642012-10-15 22:23:53 +00002040 (Triple.getOS() != llvm::Triple::IOS ||
2041 Triple.isOSVersionLT(6)))
Chandler Carruth7ce816a2012-11-19 03:52:03 +00002042 PIC = PIE = false;
Chandler Carruth5e219cf2012-04-08 16:40:35 +00002043 if (Args.hasArg(options::OPT_static))
Chandler Carruth7ce816a2012-11-19 03:52:03 +00002044 PIC = PIE = false;
Chandler Carruth5e219cf2012-04-08 16:40:35 +00002045
Chandler Carruth7ce816a2012-11-19 03:52:03 +00002046 if (Arg *A = Args.getLastArg(options::OPT_mdynamic_no_pic)) {
2047 // This is a very special mode. It trumps the other modes, almost no one
2048 // uses it, and it isn't even valid on any OS but Darwin.
2049 if (!getToolChain().getTriple().isOSDarwin())
2050 D.Diag(diag::err_drv_unsupported_opt_for_target)
2051 << A->getSpelling() << getToolChain().getTriple().str();
2052
2053 // FIXME: Warn when this flag trumps some other PIC or PIE flag.
2054
Daniel Dunbarf219e7c2009-11-29 07:18:39 +00002055 CmdArgs.push_back("-mrelocation-model");
Chandler Carruth7ce816a2012-11-19 03:52:03 +00002056 CmdArgs.push_back("dynamic-no-pic");
Daniel Dunbarbc85be82009-04-29 18:32:25 +00002057
Chandler Carruth7ce816a2012-11-19 03:52:03 +00002058 // Only a forced PIC mode can cause the actual compile to have PIC defines
2059 // etc., no flags are sufficient. This behavior was selected to closely
2060 // match that of llvm-gcc and Apple GCC before that.
2061 if (getToolChain().isPICDefault() && getToolChain().isPICDefaultForced()) {
2062 CmdArgs.push_back("-pic-level");
2063 CmdArgs.push_back("2");
2064 }
2065 } else {
2066 // Currently, LLVM only knows about PIC vs. static; the PIE differences are
2067 // handled in Clang's IRGen by the -pie-level flag.
2068 CmdArgs.push_back("-mrelocation-model");
2069 CmdArgs.push_back(PIC ? "pic" : "static");
2070
2071 if (PIC) {
2072 CmdArgs.push_back("-pic-level");
2073 CmdArgs.push_back(IsPICLevelTwo ? "2" : "1");
2074 if (PIE) {
2075 CmdArgs.push_back("-pie-level");
2076 CmdArgs.push_back(IsPICLevelTwo ? "2" : "1");
2077 }
2078 }
Daniel Dunbarbc85be82009-04-29 18:32:25 +00002079 }
Chandler Carruth5e219cf2012-04-08 16:40:35 +00002080
Tanya Lattner59876c22009-11-04 01:18:09 +00002081 if (!Args.hasFlag(options::OPT_fmerge_all_constants,
2082 options::OPT_fno_merge_all_constants))
Chris Lattnerf44a1a02011-04-08 18:06:54 +00002083 CmdArgs.push_back("-fno-merge-all-constants");
Daniel Dunbar6bea73b2009-09-16 06:17:29 +00002084
Daniel Dunbarf219e7c2009-11-29 07:18:39 +00002085 // LLVM Code Generator Options.
2086
Daniel Dunbar17d3fea2011-02-09 17:54:19 +00002087 if (Arg *A = Args.getLastArg(options::OPT_mregparm_EQ)) {
2088 CmdArgs.push_back("-mregparm");
Richard Smith1d489cf2012-11-01 04:30:05 +00002089 CmdArgs.push_back(A->getValue());
Daniel Dunbar17d3fea2011-02-09 17:54:19 +00002090 }
2091
Roman Divackycfe9af22011-03-01 17:40:53 +00002092 if (Args.hasFlag(options::OPT_mrtd, options::OPT_mno_rtd, false))
2093 CmdArgs.push_back("-mrtd");
2094
Rafael Espindola6af27ec2011-12-14 21:02:23 +00002095 if (shouldUseFramePointer(Args, getToolChain().getTriple()))
Daniel Dunbarf219e7c2009-11-29 07:18:39 +00002096 CmdArgs.push_back("-mdisable-fp-elim");
2097 if (!Args.hasFlag(options::OPT_fzero_initialized_in_bss,
2098 options::OPT_fno_zero_initialized_in_bss))
2099 CmdArgs.push_back("-mno-zero-initialized-in-bss");
Daniel Dunbar398c6102011-02-04 02:20:39 +00002100 if (!Args.hasFlag(options::OPT_fstrict_aliasing,
2101 options::OPT_fno_strict_aliasing,
2102 getToolChain().IsStrictAliasingDefault()))
Dan Gohman4d5625e2010-10-14 22:36:56 +00002103 CmdArgs.push_back("-relaxed-aliasing");
Manman Renb37a73d2013-04-04 21:53:22 +00002104 if (Args.hasArg(options::OPT_fstruct_path_tbaa))
2105 CmdArgs.push_back("-struct-path-tbaa");
Chandler Carruth82fe6ae2012-03-27 23:58:37 +00002106 if (Args.hasFlag(options::OPT_fstrict_enums, options::OPT_fno_strict_enums,
2107 false))
2108 CmdArgs.push_back("-fstrict-enums");
Nick Lewycky1db772b2012-01-23 08:29:12 +00002109 if (!Args.hasFlag(options::OPT_foptimize_sibling_calls,
2110 options::OPT_fno_optimize_sibling_calls))
2111 CmdArgs.push_back("-mdisable-tail-calls");
Daniel Dunbar1b718482010-05-14 22:00:22 +00002112
Eric Christopher31056272013-04-04 06:29:47 +00002113 // Handle segmented stacks.
2114 if (Args.hasArg(options::OPT_fsplit_stack))
2115 CmdArgs.push_back("-split-stacks");
2116
Chandler Carruthabf07a72012-01-02 14:19:45 +00002117 // Handle various floating point optimization flags, mapping them to the
2118 // appropriate LLVM code generation flags. The pattern for all of these is to
2119 // default off the codegen optimizations, and if any flag enables them and no
2120 // flag disables them after the flag enabling them, enable the codegen
2121 // optimization. This is complicated by several "umbrella" flags.
2122 if (Arg *A = Args.getLastArg(options::OPT_ffast_math,
Chad Rosier80ecf5e2012-09-25 22:03:25 +00002123 options::OPT_fno_fast_math,
Chandler Carruthabf07a72012-01-02 14:19:45 +00002124 options::OPT_ffinite_math_only,
2125 options::OPT_fno_finite_math_only,
2126 options::OPT_fhonor_infinities,
2127 options::OPT_fno_honor_infinities))
Chad Rosier80ecf5e2012-09-25 22:03:25 +00002128 if (A->getOption().getID() != options::OPT_fno_fast_math &&
2129 A->getOption().getID() != options::OPT_fno_finite_math_only &&
Chandler Carruthabf07a72012-01-02 14:19:45 +00002130 A->getOption().getID() != options::OPT_fhonor_infinities)
2131 CmdArgs.push_back("-menable-no-infs");
2132 if (Arg *A = Args.getLastArg(options::OPT_ffast_math,
Chad Rosier80ecf5e2012-09-25 22:03:25 +00002133 options::OPT_fno_fast_math,
Chandler Carruthabf07a72012-01-02 14:19:45 +00002134 options::OPT_ffinite_math_only,
2135 options::OPT_fno_finite_math_only,
2136 options::OPT_fhonor_nans,
2137 options::OPT_fno_honor_nans))
Chad Rosier80ecf5e2012-09-25 22:03:25 +00002138 if (A->getOption().getID() != options::OPT_fno_fast_math &&
2139 A->getOption().getID() != options::OPT_fno_finite_math_only &&
Chandler Carruthabf07a72012-01-02 14:19:45 +00002140 A->getOption().getID() != options::OPT_fhonor_nans)
2141 CmdArgs.push_back("-menable-no-nans");
2142
Benjamin Kramer769aa2d2012-05-02 14:55:48 +00002143 // -fmath-errno is the default on some platforms, e.g. BSD-derived OSes.
2144 bool MathErrno = getToolChain().IsMathErrnoDefault();
Chandler Carruthabf07a72012-01-02 14:19:45 +00002145 if (Arg *A = Args.getLastArg(options::OPT_ffast_math,
Chad Rosier80ecf5e2012-09-25 22:03:25 +00002146 options::OPT_fno_fast_math,
Chandler Carruthabf07a72012-01-02 14:19:45 +00002147 options::OPT_fmath_errno,
Chandler Carruth4f50c502012-04-26 02:10:51 +00002148 options::OPT_fno_math_errno))
2149 MathErrno = A->getOption().getID() == options::OPT_fmath_errno;
2150 if (MathErrno)
2151 CmdArgs.push_back("-fmath-errno");
Chandler Carruthabf07a72012-01-02 14:19:45 +00002152
2153 // There are several flags which require disabling very specific
2154 // optimizations. Any of these being disabled forces us to turn off the
2155 // entire set of LLVM optimizations, so collect them through all the flag
2156 // madness.
2157 bool AssociativeMath = false;
2158 if (Arg *A = Args.getLastArg(options::OPT_ffast_math,
Chad Rosier80ecf5e2012-09-25 22:03:25 +00002159 options::OPT_fno_fast_math,
Chandler Carruthabf07a72012-01-02 14:19:45 +00002160 options::OPT_funsafe_math_optimizations,
2161 options::OPT_fno_unsafe_math_optimizations,
2162 options::OPT_fassociative_math,
2163 options::OPT_fno_associative_math))
Chad Rosier80ecf5e2012-09-25 22:03:25 +00002164 if (A->getOption().getID() != options::OPT_fno_fast_math &&
2165 A->getOption().getID() != options::OPT_fno_unsafe_math_optimizations &&
Chandler Carruthabf07a72012-01-02 14:19:45 +00002166 A->getOption().getID() != options::OPT_fno_associative_math)
2167 AssociativeMath = true;
2168 bool ReciprocalMath = false;
2169 if (Arg *A = Args.getLastArg(options::OPT_ffast_math,
Chad Rosier80ecf5e2012-09-25 22:03:25 +00002170 options::OPT_fno_fast_math,
Chandler Carruthabf07a72012-01-02 14:19:45 +00002171 options::OPT_funsafe_math_optimizations,
2172 options::OPT_fno_unsafe_math_optimizations,
2173 options::OPT_freciprocal_math,
2174 options::OPT_fno_reciprocal_math))
Chad Rosier80ecf5e2012-09-25 22:03:25 +00002175 if (A->getOption().getID() != options::OPT_fno_fast_math &&
2176 A->getOption().getID() != options::OPT_fno_unsafe_math_optimizations &&
Chandler Carruthabf07a72012-01-02 14:19:45 +00002177 A->getOption().getID() != options::OPT_fno_reciprocal_math)
2178 ReciprocalMath = true;
2179 bool SignedZeros = true;
2180 if (Arg *A = Args.getLastArg(options::OPT_ffast_math,
Chad Rosier80ecf5e2012-09-25 22:03:25 +00002181 options::OPT_fno_fast_math,
Chandler Carruthabf07a72012-01-02 14:19:45 +00002182 options::OPT_funsafe_math_optimizations,
2183 options::OPT_fno_unsafe_math_optimizations,
2184 options::OPT_fsigned_zeros,
2185 options::OPT_fno_signed_zeros))
Chad Rosier80ecf5e2012-09-25 22:03:25 +00002186 if (A->getOption().getID() != options::OPT_fno_fast_math &&
2187 A->getOption().getID() != options::OPT_fno_unsafe_math_optimizations &&
Chandler Carruthabf07a72012-01-02 14:19:45 +00002188 A->getOption().getID() != options::OPT_fsigned_zeros)
2189 SignedZeros = false;
2190 bool TrappingMath = true;
2191 if (Arg *A = Args.getLastArg(options::OPT_ffast_math,
Chad Rosier80ecf5e2012-09-25 22:03:25 +00002192 options::OPT_fno_fast_math,
Chandler Carruthabf07a72012-01-02 14:19:45 +00002193 options::OPT_funsafe_math_optimizations,
2194 options::OPT_fno_unsafe_math_optimizations,
2195 options::OPT_ftrapping_math,
2196 options::OPT_fno_trapping_math))
Chad Rosier80ecf5e2012-09-25 22:03:25 +00002197 if (A->getOption().getID() != options::OPT_fno_fast_math &&
2198 A->getOption().getID() != options::OPT_fno_unsafe_math_optimizations &&
Chandler Carruthabf07a72012-01-02 14:19:45 +00002199 A->getOption().getID() != options::OPT_ftrapping_math)
2200 TrappingMath = false;
2201 if (!MathErrno && AssociativeMath && ReciprocalMath && !SignedZeros &&
2202 !TrappingMath)
2203 CmdArgs.push_back("-menable-unsafe-fp-math");
2204
Lang Hamesc9686712012-07-06 00:59:19 +00002205
2206 // Validate and pass through -fp-contract option.
2207 if (Arg *A = Args.getLastArg(options::OPT_ffast_math,
Chad Rosier80ecf5e2012-09-25 22:03:25 +00002208 options::OPT_fno_fast_math,
Lang Hamesc9686712012-07-06 00:59:19 +00002209 options::OPT_ffp_contract)) {
2210 if (A->getOption().getID() == options::OPT_ffp_contract) {
Richard Smith1d489cf2012-11-01 04:30:05 +00002211 StringRef Val = A->getValue();
Lang Hamesc9686712012-07-06 00:59:19 +00002212 if (Val == "fast" || Val == "on" || Val == "off") {
2213 CmdArgs.push_back(Args.MakeArgString("-ffp-contract=" + Val));
2214 } else {
2215 D.Diag(diag::err_drv_unsupported_option_argument)
2216 << A->getOption().getName() << Val;
2217 }
Chad Rosier80ecf5e2012-09-25 22:03:25 +00002218 } else if (A->getOption().getID() == options::OPT_ffast_math) {
Lang Hamesc9686712012-07-06 00:59:19 +00002219 // If fast-math is set then set the fp-contract mode to fast.
2220 CmdArgs.push_back(Args.MakeArgString("-ffp-contract=fast"));
2221 }
2222 }
2223
Bob Wilson455e72e2012-07-19 03:52:53 +00002224 // We separately look for the '-ffast-math' and '-ffinite-math-only' flags,
2225 // and if we find them, tell the frontend to provide the appropriate
2226 // preprocessor macros. This is distinct from enabling any optimizations as
2227 // these options induce language changes which must survive serialization
2228 // and deserialization, etc.
Chad Rosier80ecf5e2012-09-25 22:03:25 +00002229 if (Arg *A = Args.getLastArg(options::OPT_ffast_math, options::OPT_fno_fast_math))
2230 if (A->getOption().matches(options::OPT_ffast_math))
2231 CmdArgs.push_back("-ffast-math");
2232 if (Arg *A = Args.getLastArg(options::OPT_ffinite_math_only, options::OPT_fno_fast_math))
2233 if (A->getOption().matches(options::OPT_ffinite_math_only))
2234 CmdArgs.push_back("-ffinite-math-only");
Chandler Carruthabf07a72012-01-02 14:19:45 +00002235
Daniel Dunbar1b718482010-05-14 22:00:22 +00002236 // Decide whether to use verbose asm. Verbose assembly is the default on
2237 // toolchains which have the integrated assembler on by default.
2238 bool IsVerboseAsmDefault = getToolChain().IsIntegratedAssemblerDefault();
2239 if (Args.hasFlag(options::OPT_fverbose_asm, options::OPT_fno_verbose_asm,
Michael J. Spencer20249a12010-10-21 03:16:25 +00002240 IsVerboseAsmDefault) ||
Daniel Dunbar1b718482010-05-14 22:00:22 +00002241 Args.hasArg(options::OPT_dA))
Daniel Dunbarf219e7c2009-11-29 07:18:39 +00002242 CmdArgs.push_back("-masm-verbose");
Daniel Dunbar1b718482010-05-14 22:00:22 +00002243
Daniel Dunbarf219e7c2009-11-29 07:18:39 +00002244 if (Args.hasArg(options::OPT_fdebug_pass_structure)) {
2245 CmdArgs.push_back("-mdebug-pass");
2246 CmdArgs.push_back("Structure");
2247 }
2248 if (Args.hasArg(options::OPT_fdebug_pass_arguments)) {
2249 CmdArgs.push_back("-mdebug-pass");
2250 CmdArgs.push_back("Arguments");
2251 }
2252
John McCalld0c2ec42010-02-19 02:45:38 +00002253 // Enable -mconstructor-aliases except on darwin, where we have to
2254 // work around a linker bug; see <rdar://problem/7651567>.
Bob Wilson905c45f2011-10-14 05:03:44 +00002255 if (!getToolChain().getTriple().isOSDarwin())
John McCalld0c2ec42010-02-19 02:45:38 +00002256 CmdArgs.push_back("-mconstructor-aliases");
NAKAMURA Takumi125b4cb2011-02-17 08:50:50 +00002257
John McCall32096692011-03-18 02:56:14 +00002258 // Darwin's kernel doesn't support guard variables; just die if we
2259 // try to use them.
Bob Wilson905c45f2011-10-14 05:03:44 +00002260 if (KernelOrKext && getToolChain().getTriple().isOSDarwin())
John McCall32096692011-03-18 02:56:14 +00002261 CmdArgs.push_back("-fforbid-guard-variables");
2262
Douglas Gregor6f755502011-02-01 15:15:22 +00002263 if (Args.hasArg(options::OPT_mms_bitfields)) {
2264 CmdArgs.push_back("-mms-bitfields");
2265 }
John McCalld0c2ec42010-02-19 02:45:38 +00002266
Daniel Dunbar6bea73b2009-09-16 06:17:29 +00002267 // This is a coarse approximation of what llvm-gcc actually does, both
2268 // -fasynchronous-unwind-tables and -fnon-call-exceptions interact in more
2269 // complicated ways.
2270 bool AsynchronousUnwindTables =
2271 Args.hasFlag(options::OPT_fasynchronous_unwind_tables,
2272 options::OPT_fno_asynchronous_unwind_tables,
2273 getToolChain().IsUnwindTablesDefault() &&
Daniel Dunbar0a80ba72010-03-20 04:52:14 +00002274 !KernelOrKext);
Daniel Dunbar6bea73b2009-09-16 06:17:29 +00002275 if (Args.hasFlag(options::OPT_funwind_tables, options::OPT_fno_unwind_tables,
2276 AsynchronousUnwindTables))
Daniel Dunbarf219e7c2009-11-29 07:18:39 +00002277 CmdArgs.push_back("-munwind-tables");
2278
Chandler Carrutha6b25812012-11-21 23:40:23 +00002279 getToolChain().addClangTargetOptions(Args, CmdArgs);
Rafael Espindola8af669f2012-06-19 01:26:10 +00002280
Daniel Dunbarf219e7c2009-11-29 07:18:39 +00002281 if (Arg *A = Args.getLastArg(options::OPT_flimited_precision_EQ)) {
2282 CmdArgs.push_back("-mlimit-float-precision");
Richard Smith1d489cf2012-11-01 04:30:05 +00002283 CmdArgs.push_back(A->getValue());
Daniel Dunbarf219e7c2009-11-29 07:18:39 +00002284 }
Daniel Dunbarbc85be82009-04-29 18:32:25 +00002285
Daniel Dunbar868bd0a2009-05-06 03:16:41 +00002286 // FIXME: Handle -mtune=.
2287 (void) Args.hasArg(options::OPT_mtune_EQ);
Daniel Dunbarbc85be82009-04-29 18:32:25 +00002288
Benjamin Kramer8e9ef0d2009-08-05 14:30:52 +00002289 if (Arg *A = Args.getLastArg(options::OPT_mcmodel_EQ)) {
Daniel Dunbarf219e7c2009-11-29 07:18:39 +00002290 CmdArgs.push_back("-mcode-model");
Richard Smith1d489cf2012-11-01 04:30:05 +00002291 CmdArgs.push_back(A->getValue());
Benjamin Kramer8e9ef0d2009-08-05 14:30:52 +00002292 }
2293
Daniel Dunbar6acda162009-09-09 22:33:08 +00002294 // Add target specific cpu and features flags.
2295 switch(getToolChain().getTriple().getArch()) {
2296 default:
2297 break;
Daniel Dunbar868bd0a2009-05-06 03:16:41 +00002298
Daniel Dunbarb163ef72009-09-10 04:57:17 +00002299 case llvm::Triple::arm:
2300 case llvm::Triple::thumb:
Daniel Dunbarfa41d692011-03-17 17:10:06 +00002301 AddARMTargetArgs(Args, CmdArgs, KernelOrKext);
Daniel Dunbarb163ef72009-09-10 04:57:17 +00002302 break;
2303
Eric Christophered734732010-03-02 02:41:08 +00002304 case llvm::Triple::mips:
2305 case llvm::Triple::mipsel:
Akira Hatanaka7ec02582011-09-21 02:13:07 +00002306 case llvm::Triple::mips64:
2307 case llvm::Triple::mips64el:
Eric Christophered734732010-03-02 02:41:08 +00002308 AddMIPSTargetArgs(Args, CmdArgs);
2309 break;
2310
Hal Finkel02a84272012-06-11 22:35:19 +00002311 case llvm::Triple::ppc:
2312 case llvm::Triple::ppc64:
2313 AddPPCTargetArgs(Args, CmdArgs);
2314 break;
2315
Tom Stellarde25d2f62013-04-01 20:56:53 +00002316 case llvm::Triple::r600:
2317 AddR600TargetArgs(Args, CmdArgs);
2318 break;
2319
Bruno Cardoso Lopes9284d212010-11-09 17:21:19 +00002320 case llvm::Triple::sparc:
2321 AddSparcTargetArgs(Args, CmdArgs);
2322 break;
2323
Daniel Dunbar6acda162009-09-09 22:33:08 +00002324 case llvm::Triple::x86:
2325 case llvm::Triple::x86_64:
2326 AddX86TargetArgs(Args, CmdArgs);
2327 break;
Tony Linthicum96319392011-12-12 21:14:55 +00002328
2329 case llvm::Triple::hexagon:
2330 AddHexagonTargetArgs(Args, CmdArgs);
2331 break;
Daniel Dunbarbc85be82009-04-29 18:32:25 +00002332 }
2333
Tony Linthicum96319392011-12-12 21:14:55 +00002334
2335
Daniel Dunbarc176bc62010-08-11 23:07:47 +00002336 // Pass the linker version in use.
2337 if (Arg *A = Args.getLastArg(options::OPT_mlinker_version_EQ)) {
2338 CmdArgs.push_back("-target-linker-version");
Richard Smith1d489cf2012-11-01 04:30:05 +00002339 CmdArgs.push_back(A->getValue());
Daniel Dunbarc176bc62010-08-11 23:07:47 +00002340 }
2341
Eric Christopherd3e22df2013-04-03 01:58:53 +00002342 if (!shouldUseLeafFramePointer(Args, getToolChain().getTriple()))
Daniel Dunbar1ad66482010-07-01 01:31:45 +00002343 CmdArgs.push_back("-momit-leaf-frame-pointer");
2344
Daniel Dunbarb30575c2010-05-12 18:19:58 +00002345 // Explicitly error on some things we know we don't support and can't just
2346 // ignore.
2347 types::ID InputType = Inputs[0].getType();
Daniel Dunbare94db472010-09-24 19:39:37 +00002348 if (!Args.hasArg(options::OPT_fallow_unsupported)) {
2349 Arg *Unsupported;
Daniel Dunbare94db472010-09-24 19:39:37 +00002350 if (types::isCXX(InputType) &&
Bob Wilson905c45f2011-10-14 05:03:44 +00002351 getToolChain().getTriple().isOSDarwin() &&
Daniel Dunbare94db472010-09-24 19:39:37 +00002352 getToolChain().getTriple().getArch() == llvm::Triple::x86) {
Bob Wilsona544aee2011-08-13 23:48:55 +00002353 if ((Unsupported = Args.getLastArg(options::OPT_fapple_kext)) ||
2354 (Unsupported = Args.getLastArg(options::OPT_mkernel)))
Chris Lattner5f9e2722011-07-23 10:55:15 +00002355 D.Diag(diag::err_drv_clang_unsupported_opt_cxx_darwin_i386)
Daniel Dunbare94db472010-09-24 19:39:37 +00002356 << Unsupported->getOption().getName();
2357 }
Daniel Dunbarb30575c2010-05-12 18:19:58 +00002358 }
2359
Daniel Dunbar1d460332009-03-18 10:01:51 +00002360 Args.AddAllArgs(CmdArgs, options::OPT_v);
Daniel Dunbarf7c16d92010-08-24 22:44:13 +00002361 Args.AddLastArg(CmdArgs, options::OPT_H);
Chad Rosier2b819102011-08-02 17:58:04 +00002362 if (D.CCPrintHeaders && !D.CCGenDiagnostics) {
Daniel Dunbar322c29f2011-02-02 21:11:35 +00002363 CmdArgs.push_back("-header-include-file");
2364 CmdArgs.push_back(D.CCPrintHeadersFilename ?
2365 D.CCPrintHeadersFilename : "-");
2366 }
Daniel Dunbar1d460332009-03-18 10:01:51 +00002367 Args.AddLastArg(CmdArgs, options::OPT_P);
Mike Stump1eb44332009-09-09 15:08:12 +00002368 Args.AddLastArg(CmdArgs, options::OPT_print_ivar_layout);
Daniel Dunbar1d460332009-03-18 10:01:51 +00002369
Chad Rosier2b819102011-08-02 17:58:04 +00002370 if (D.CCLogDiagnostics && !D.CCGenDiagnostics) {
Daniel Dunbarc8a22b02011-04-07 18:01:20 +00002371 CmdArgs.push_back("-diagnostic-log-file");
2372 CmdArgs.push_back(D.CCLogDiagnosticsFilename ?
2373 D.CCLogDiagnosticsFilename : "-");
2374 }
2375
Eric Christopherc706c8e2013-02-05 07:29:57 +00002376 // Use the last option from "-g" group. "-gline-tables-only"
2377 // is preserved, all other debug options are substituted with "-g".
Rafael Espindola18f36d92010-03-07 04:46:18 +00002378 Args.ClaimAllArgs(options::OPT_g_Group);
Alexey Samsonova9cd83b2012-05-29 08:10:34 +00002379 if (Arg *A = Args.getLastArg(options::OPT_g_Group)) {
Eric Christopherc706c8e2013-02-05 07:29:57 +00002380 if (A->getOption().matches(options::OPT_gline_tables_only))
Alexey Samsonova9cd83b2012-05-29 08:10:34 +00002381 CmdArgs.push_back("-gline-tables-only");
Eric Christopherc706c8e2013-02-05 07:29:57 +00002382 else if (!A->getOption().matches(options::OPT_g0) &&
2383 !A->getOption().matches(options::OPT_ggdb0))
Chad Rosiercf6ba2e2011-11-07 19:52:29 +00002384 CmdArgs.push_back("-g");
Alexey Samsonova9cd83b2012-05-29 08:10:34 +00002385 }
Daniel Dunbar1d460332009-03-18 10:01:51 +00002386
Alexey Samsonov7f326072012-06-21 08:22:39 +00002387 // We ignore flags -gstrict-dwarf and -grecord-gcc-switches for now.
2388 Args.ClaimAllArgs(options::OPT_g_flags_Group);
Eric Christopherda3301e2012-10-18 21:52:18 +00002389 if (Args.hasArg(options::OPT_gcolumn_info))
2390 CmdArgs.push_back("-dwarf-column-info");
Alexey Samsonov7f326072012-06-21 08:22:39 +00002391
Eric Christopherc706c8e2013-02-05 07:29:57 +00002392 // -gsplit-dwarf should turn on -g and enable the backend dwarf
2393 // splitting and extraction.
Eric Christopherf870e122013-02-21 22:35:05 +00002394 // FIXME: Currently only works on Linux.
2395 if (getToolChain().getTriple().getOS() == llvm::Triple::Linux &&
2396 Args.hasArg(options::OPT_gsplit_dwarf)) {
Eric Christopherc706c8e2013-02-05 07:29:57 +00002397 CmdArgs.push_back("-g");
2398 CmdArgs.push_back("-backend-option");
2399 CmdArgs.push_back("-split-dwarf=Enable");
2400 }
2401
Rafael Espindola9cf933a2010-05-06 21:06:04 +00002402 Args.AddAllArgs(CmdArgs, options::OPT_ffunction_sections);
2403 Args.AddAllArgs(CmdArgs, options::OPT_fdata_sections);
2404
Chris Lattner7255a2d2010-06-22 00:03:40 +00002405 Args.AddAllArgs(CmdArgs, options::OPT_finstrument_functions);
2406
Nick Lewyckye8ba8d72011-04-21 23:44:07 +00002407 if (Args.hasArg(options::OPT_ftest_coverage) ||
2408 Args.hasArg(options::OPT_coverage))
2409 CmdArgs.push_back("-femit-coverage-notes");
2410 if (Args.hasArg(options::OPT_fprofile_arcs) ||
2411 Args.hasArg(options::OPT_coverage))
2412 CmdArgs.push_back("-femit-coverage-data");
2413
Nick Lewycky5ea4f442011-05-04 20:46:58 +00002414 if (C.getArgs().hasArg(options::OPT_c) ||
2415 C.getArgs().hasArg(options::OPT_S)) {
2416 if (Output.isFilename()) {
Nick Lewycky3dc05412011-05-05 00:08:20 +00002417 CmdArgs.push_back("-coverage-file");
Eric Christopher025b3d42013-02-22 00:24:40 +00002418 SmallString<128> CoverageFilename(Output.getFilename());
Nick Lewycky0f815f12013-03-07 08:28:53 +00002419 if (llvm::sys::path::is_relative(CoverageFilename.str())) {
2420 if (const char *pwd = ::getenv("PWD")) {
2421 if (llvm::sys::path::is_absolute(pwd)) {
2422 SmallString<128> Pwd(pwd);
2423 llvm::sys::path::append(Pwd, CoverageFilename.str());
2424 CoverageFilename.swap(Pwd);
2425 }
2426 }
2427 }
Eric Christopher025b3d42013-02-22 00:24:40 +00002428 CmdArgs.push_back(Args.MakeArgString(CoverageFilename));
Nick Lewycky5ea4f442011-05-04 20:46:58 +00002429 }
2430 }
2431
Daniel Dunbara268fc02011-10-11 18:20:10 +00002432 // Pass options for controlling the default header search paths.
2433 if (Args.hasArg(options::OPT_nostdinc)) {
2434 CmdArgs.push_back("-nostdsysteminc");
2435 CmdArgs.push_back("-nobuiltininc");
2436 } else {
Daniel Dunbar92d6d402011-10-11 18:20:16 +00002437 if (Args.hasArg(options::OPT_nostdlibinc))
2438 CmdArgs.push_back("-nostdsysteminc");
Daniel Dunbara268fc02011-10-11 18:20:10 +00002439 Args.AddLastArg(CmdArgs, options::OPT_nostdincxx);
2440 Args.AddLastArg(CmdArgs, options::OPT_nobuiltininc);
2441 }
Daniel Dunbar1d460332009-03-18 10:01:51 +00002442
Daniel Dunbar5f122322009-12-15 01:02:52 +00002443 // Pass the path to compiler resource files.
Daniel Dunbar5f122322009-12-15 01:02:52 +00002444 CmdArgs.push_back("-resource-dir");
Daniel Dunbar225c4172010-01-20 02:35:16 +00002445 CmdArgs.push_back(D.ResourceDir.c_str());
Daniel Dunbar2ac9fc22009-04-07 21:42:00 +00002446
Argyrios Kyrtzidis389db162010-11-03 22:45:23 +00002447 Args.AddLastArg(CmdArgs, options::OPT_working_directory);
2448
Ted Kremenek30660a82012-03-06 20:06:33 +00002449 bool ARCMTEnabled = false;
John McCall8f0e8d22011-06-15 23:25:17 +00002450 if (!Args.hasArg(options::OPT_fno_objc_arc)) {
Argyrios Kyrtzidis72ac1202011-07-07 04:00:39 +00002451 if (const Arg *A = Args.getLastArg(options::OPT_ccc_arcmt_check,
Argyrios Kyrtzidis69325d52011-07-09 20:00:58 +00002452 options::OPT_ccc_arcmt_modify,
2453 options::OPT_ccc_arcmt_migrate)) {
Ted Kremenek30660a82012-03-06 20:06:33 +00002454 ARCMTEnabled = true;
John McCall8f0e8d22011-06-15 23:25:17 +00002455 switch (A->getOption().getID()) {
2456 default:
2457 llvm_unreachable("missed a case");
Argyrios Kyrtzidis72ac1202011-07-07 04:00:39 +00002458 case options::OPT_ccc_arcmt_check:
John McCall8f0e8d22011-06-15 23:25:17 +00002459 CmdArgs.push_back("-arcmt-check");
2460 break;
Argyrios Kyrtzidis72ac1202011-07-07 04:00:39 +00002461 case options::OPT_ccc_arcmt_modify:
John McCall8f0e8d22011-06-15 23:25:17 +00002462 CmdArgs.push_back("-arcmt-modify");
2463 break;
Argyrios Kyrtzidis69325d52011-07-09 20:00:58 +00002464 case options::OPT_ccc_arcmt_migrate:
2465 CmdArgs.push_back("-arcmt-migrate");
Ted Kremenek30660a82012-03-06 20:06:33 +00002466 CmdArgs.push_back("-mt-migrate-directory");
Richard Smith1d489cf2012-11-01 04:30:05 +00002467 CmdArgs.push_back(A->getValue());
Argyrios Kyrtzidis7ee20492011-07-19 17:20:03 +00002468
2469 Args.AddLastArg(CmdArgs, options::OPT_arcmt_migrate_report_output);
2470 Args.AddLastArg(CmdArgs, options::OPT_arcmt_migrate_emit_arc_errors);
Argyrios Kyrtzidis69325d52011-07-09 20:00:58 +00002471 break;
John McCall8f0e8d22011-06-15 23:25:17 +00002472 }
2473 }
2474 }
Eric Christopher88b7cf02011-08-19 00:30:14 +00002475
Ted Kremenek30660a82012-03-06 20:06:33 +00002476 if (const Arg *A = Args.getLastArg(options::OPT_ccc_objcmt_migrate)) {
2477 if (ARCMTEnabled) {
2478 D.Diag(diag::err_drv_argument_not_allowed_with)
2479 << A->getAsString(Args) << "-ccc-arcmt-migrate";
2480 }
2481 CmdArgs.push_back("-mt-migrate-directory");
Richard Smith1d489cf2012-11-01 04:30:05 +00002482 CmdArgs.push_back(A->getValue());
Ted Kremenek30660a82012-03-06 20:06:33 +00002483
2484 if (!Args.hasArg(options::OPT_objcmt_migrate_literals,
2485 options::OPT_objcmt_migrate_subscripting)) {
2486 // None specified, means enable them all.
2487 CmdArgs.push_back("-objcmt-migrate-literals");
2488 CmdArgs.push_back("-objcmt-migrate-subscripting");
2489 } else {
2490 Args.AddLastArg(CmdArgs, options::OPT_objcmt_migrate_literals);
2491 Args.AddLastArg(CmdArgs, options::OPT_objcmt_migrate_subscripting);
2492 }
2493 }
2494
Daniel Dunbarc21c4852009-04-08 23:54:23 +00002495 // Add preprocessing options like -I, -D, etc. if we are using the
2496 // preprocessor.
2497 //
2498 // FIXME: Support -fpreprocessed
Daniel Dunbarc21c4852009-04-08 23:54:23 +00002499 if (types::getPreprocessedType(InputType) != types::TY_INVALID)
Chad Rosier9d718632013-01-24 19:14:47 +00002500 AddPreprocessingOptions(C, JA, D, Args, CmdArgs, Output, Inputs);
Daniel Dunbar1d460332009-03-18 10:01:51 +00002501
Rafael Espindola19d9d2e2011-07-21 23:40:37 +00002502 // Don't warn about "clang -c -DPIC -fPIC test.i" because libtool.m4 assumes
2503 // that "The compiler can only warn and ignore the option if not recognized".
2504 // When building with ccache, it will pass -D options to clang even on
2505 // preprocessed inputs and configure concludes that -fPIC is not supported.
2506 Args.ClaimAllArgs(options::OPT_D);
2507
Daniel Dunbar20f0eac2009-09-17 06:53:36 +00002508 // Manually translate -O to -O2 and -O4 to -O3; let clang reject
Daniel Dunbar337a6272009-03-24 20:17:30 +00002509 // others.
2510 if (Arg *A = Args.getLastArg(options::OPT_O_Group)) {
Daniel Dunbarb827a052009-11-19 03:26:40 +00002511 if (A->getOption().matches(options::OPT_O4))
Daniel Dunbar337a6272009-03-24 20:17:30 +00002512 CmdArgs.push_back("-O3");
Daniel Dunbar473916c2010-05-27 06:51:08 +00002513 else if (A->getOption().matches(options::OPT_O) &&
Richard Smith1d489cf2012-11-01 04:30:05 +00002514 A->getValue()[0] == '\0')
Daniel Dunbar20f0eac2009-09-17 06:53:36 +00002515 CmdArgs.push_back("-O2");
Daniel Dunbar1d460332009-03-18 10:01:51 +00002516 else
Daniel Dunbar5697aa02009-03-18 23:39:35 +00002517 A->render(Args, CmdArgs);
Daniel Dunbar1d460332009-03-18 10:01:51 +00002518 }
2519
Chad Rosierb2c08872012-12-12 20:06:31 +00002520 // Don't warn about unused -flto. This can happen when we're preprocessing or
2521 // precompiling.
2522 Args.ClaimAllArgs(options::OPT_flto);
2523
Daniel Dunbar6e8371e2009-10-29 02:24:45 +00002524 Args.AddAllArgs(CmdArgs, options::OPT_W_Group);
Ted Kremeneke8cf7d12012-07-07 05:53:30 +00002525 if (Args.hasFlag(options::OPT_pedantic, options::OPT_no_pedantic, false))
2526 CmdArgs.push_back("-pedantic");
Daniel Dunbar6e8371e2009-10-29 02:24:45 +00002527 Args.AddLastArg(CmdArgs, options::OPT_pedantic_errors);
Daniel Dunbar1d460332009-03-18 10:01:51 +00002528 Args.AddLastArg(CmdArgs, options::OPT_w);
Daniel Dunbard573d262009-04-07 22:13:21 +00002529
2530 // Handle -{std, ansi, trigraphs} -- take the last of -{std, ansi}
2531 // (-ansi is equivalent to -std=c89).
2532 //
2533 // If a std is supplied, only add -trigraphs if it follows the
2534 // option.
2535 if (Arg *Std = Args.getLastArg(options::OPT_std_EQ, options::OPT_ansi)) {
2536 if (Std->getOption().matches(options::OPT_ansi))
Nuno Lopes528365d2009-10-16 14:28:06 +00002537 if (types::isCXX(InputType))
Daniel Dunbar294691e2009-11-04 06:24:38 +00002538 CmdArgs.push_back("-std=c++98");
Nuno Lopes528365d2009-10-16 14:28:06 +00002539 else
Daniel Dunbar294691e2009-11-04 06:24:38 +00002540 CmdArgs.push_back("-std=c89");
Daniel Dunbard573d262009-04-07 22:13:21 +00002541 else
2542 Std->render(Args, CmdArgs);
2543
Daniel Dunbar0e100312010-06-14 21:23:08 +00002544 if (Arg *A = Args.getLastArg(options::OPT_std_EQ, options::OPT_ansi,
2545 options::OPT_trigraphs))
2546 if (A != Std)
Daniel Dunbard573d262009-04-07 22:13:21 +00002547 A->render(Args, CmdArgs);
Daniel Dunbara3ff2022009-04-26 01:10:38 +00002548 } else {
2549 // Honor -std-default.
Daniel Dunbar4a5290e2010-01-29 21:03:02 +00002550 //
2551 // FIXME: Clang doesn't correctly handle -std= when the input language
2552 // doesn't match. For the time being just ignore this for C++ inputs;
2553 // eventually we want to do all the standard defaulting here instead of
2554 // splitting it between the driver and clang -cc1.
2555 if (!types::isCXX(InputType))
Nico Weber50f88b92012-08-30 02:08:31 +00002556 Args.AddAllArgsTranslated(CmdArgs, options::OPT_std_default_EQ,
2557 "-std=", /*Joined=*/true);
2558 else if (getToolChain().getTriple().getOS() == llvm::Triple::Win32)
2559 CmdArgs.push_back("-std=c++11");
2560
Daniel Dunbard573d262009-04-07 22:13:21 +00002561 Args.AddLastArg(CmdArgs, options::OPT_trigraphs);
Daniel Dunbara3ff2022009-04-26 01:10:38 +00002562 }
Daniel Dunbarc21c4852009-04-08 23:54:23 +00002563
Chandler Carruth50465d12011-04-23 06:30:43 +00002564 // Map the bizarre '-Wwrite-strings' flag to a more sensible
2565 // '-fconst-strings'; this better indicates its actual behavior.
2566 if (Args.hasFlag(options::OPT_Wwrite_strings, options::OPT_Wno_write_strings,
2567 false)) {
2568 // For perfect compatibility with GCC, we do this even in the presence of
2569 // '-w'. This flag names something other than a warning for GCC.
2570 CmdArgs.push_back("-fconst-strings");
2571 }
2572
Chandler Carruth1cfe3c32011-04-23 09:27:53 +00002573 // GCC provides a macro definition '__DEPRECATED' when -Wdeprecated is active
Chandler Carruthf8c247d2011-04-23 19:48:40 +00002574 // during C++ compilation, which it is by default. GCC keeps this define even
2575 // in the presence of '-w', match this behavior bug-for-bug.
2576 if (types::isCXX(InputType) &&
2577 Args.hasFlag(options::OPT_Wdeprecated, options::OPT_Wno_deprecated,
2578 true)) {
2579 CmdArgs.push_back("-fdeprecated-macro");
Chandler Carruth1cfe3c32011-04-23 09:27:53 +00002580 }
2581
Chandler Carruthc304ba32010-05-22 02:21:53 +00002582 // Translate GCC's misnamer '-fasm' arguments to '-fgnu-keywords'.
2583 if (Arg *Asm = Args.getLastArg(options::OPT_fasm, options::OPT_fno_asm)) {
2584 if (Asm->getOption().matches(options::OPT_fasm))
2585 CmdArgs.push_back("-fgnu-keywords");
2586 else
2587 CmdArgs.push_back("-fno-gnu-keywords");
2588 }
2589
Rafael Espindola61b1efe2011-05-02 17:43:32 +00002590 if (ShouldDisableCFI(Args, getToolChain()))
2591 CmdArgs.push_back("-fno-dwarf2-cfi-asm");
Rafael Espindolaf24a1512011-04-30 18:35:43 +00002592
Nick Lewyckyea523d72011-10-17 23:05:52 +00002593 if (ShouldDisableDwarfDirectory(Args, getToolChain()))
2594 CmdArgs.push_back("-fno-dwarf-directory-asm");
2595
Chandler Carruthd566df62012-12-17 21:40:04 +00002596 // Add in -fdebug-compilation-dir if necessary.
2597 addDebugCompDirArg(Args, CmdArgs);
Nick Lewycky7c4fd912011-10-21 02:32:14 +00002598
Richard Smithc18c4232011-11-21 19:36:32 +00002599 if (Arg *A = Args.getLastArg(options::OPT_ftemplate_depth_,
2600 options::OPT_ftemplate_depth_EQ)) {
Daniel Dunbar1d460332009-03-18 10:01:51 +00002601 CmdArgs.push_back("-ftemplate-depth");
Richard Smith1d489cf2012-11-01 04:30:05 +00002602 CmdArgs.push_back(A->getValue());
Daniel Dunbar1d460332009-03-18 10:01:51 +00002603 }
2604
Richard Smithc18c4232011-11-21 19:36:32 +00002605 if (Arg *A = Args.getLastArg(options::OPT_fconstexpr_depth_EQ)) {
2606 CmdArgs.push_back("-fconstexpr-depth");
Richard Smith1d489cf2012-11-01 04:30:05 +00002607 CmdArgs.push_back(A->getValue());
Richard Smithc18c4232011-11-21 19:36:32 +00002608 }
2609
Richard Smith9e738cc2013-02-22 01:59:51 +00002610 if (Arg *A = Args.getLastArg(options::OPT_fbracket_depth_EQ)) {
2611 CmdArgs.push_back("-fbracket-depth");
2612 CmdArgs.push_back(A->getValue());
2613 }
2614
Argyrios Kyrtzidis1380a142010-11-18 00:20:36 +00002615 if (Arg *A = Args.getLastArg(options::OPT_Wlarge_by_value_copy_EQ,
2616 options::OPT_Wlarge_by_value_copy_def)) {
Jean-Daniel Dupas2e4fd6d2012-05-04 08:08:37 +00002617 if (A->getNumValues()) {
Richard Smith1d489cf2012-11-01 04:30:05 +00002618 StringRef bytes = A->getValue();
Jean-Daniel Dupas2e4fd6d2012-05-04 08:08:37 +00002619 CmdArgs.push_back(Args.MakeArgString("-Wlarge-by-value-copy=" + bytes));
2620 } else
2621 CmdArgs.push_back("-Wlarge-by-value-copy=64"); // default value
Argyrios Kyrtzidis3532fdd2010-11-17 23:11:54 +00002622 }
2623
Nuno Lopesb3198a82012-05-08 22:10:46 +00002624
Michael J. Spencerc6357102012-10-22 22:13:48 +00002625 if (Args.hasArg(options::OPT_relocatable_pch))
Daniel Dunbar66861e02009-11-20 22:21:36 +00002626 CmdArgs.push_back("-relocatable-pch");
Mike Stump1eb44332009-09-09 15:08:12 +00002627
Daniel Dunbar294691e2009-11-04 06:24:38 +00002628 if (Arg *A = Args.getLastArg(options::OPT_fconstant_string_class_EQ)) {
2629 CmdArgs.push_back("-fconstant-string-class");
Richard Smith1d489cf2012-11-01 04:30:05 +00002630 CmdArgs.push_back(A->getValue());
Daniel Dunbar294691e2009-11-04 06:24:38 +00002631 }
David Chisnall8a5a9aa2009-08-31 16:41:57 +00002632
Chris Lattner124fca52010-01-09 21:54:33 +00002633 if (Arg *A = Args.getLastArg(options::OPT_ftabstop_EQ)) {
2634 CmdArgs.push_back("-ftabstop");
Richard Smith1d489cf2012-11-01 04:30:05 +00002635 CmdArgs.push_back(A->getValue());
Chris Lattner124fca52010-01-09 21:54:33 +00002636 }
2637
Chris Lattner0f0c9632010-04-07 20:49:23 +00002638 CmdArgs.push_back("-ferror-limit");
2639 if (Arg *A = Args.getLastArg(options::OPT_ferror_limit_EQ))
Richard Smith1d489cf2012-11-01 04:30:05 +00002640 CmdArgs.push_back(A->getValue());
Chris Lattner0f0c9632010-04-07 20:49:23 +00002641 else
2642 CmdArgs.push_back("19");
Douglas Gregor575cf372010-04-20 07:18:24 +00002643
Chandler Carruthc40f73c2010-05-06 04:55:18 +00002644 if (Arg *A = Args.getLastArg(options::OPT_fmacro_backtrace_limit_EQ)) {
2645 CmdArgs.push_back("-fmacro-backtrace-limit");
Richard Smith1d489cf2012-11-01 04:30:05 +00002646 CmdArgs.push_back(A->getValue());
Chandler Carruthc40f73c2010-05-06 04:55:18 +00002647 }
2648
2649 if (Arg *A = Args.getLastArg(options::OPT_ftemplate_backtrace_limit_EQ)) {
2650 CmdArgs.push_back("-ftemplate-backtrace-limit");
Richard Smith1d489cf2012-11-01 04:30:05 +00002651 CmdArgs.push_back(A->getValue());
Chandler Carruthc40f73c2010-05-06 04:55:18 +00002652 }
2653
Richard Smith08d6e032011-12-16 19:06:07 +00002654 if (Arg *A = Args.getLastArg(options::OPT_fconstexpr_backtrace_limit_EQ)) {
2655 CmdArgs.push_back("-fconstexpr-backtrace-limit");
Richard Smith1d489cf2012-11-01 04:30:05 +00002656 CmdArgs.push_back(A->getValue());
Richard Smith08d6e032011-12-16 19:06:07 +00002657 }
2658
Daniel Dunbar55efe142009-11-04 06:24:47 +00002659 // Pass -fmessage-length=.
Daniel Dunbara28690e2009-11-30 08:40:54 +00002660 CmdArgs.push_back("-fmessage-length");
Daniel Dunbar55efe142009-11-04 06:24:47 +00002661 if (Arg *A = Args.getLastArg(options::OPT_fmessage_length_EQ)) {
Richard Smith1d489cf2012-11-01 04:30:05 +00002662 CmdArgs.push_back(A->getValue());
Daniel Dunbar55efe142009-11-04 06:24:47 +00002663 } else {
2664 // If -fmessage-length=N was not specified, determine whether this is a
2665 // terminal and, if so, implicitly define -fmessage-length appropriately.
2666 unsigned N = llvm::sys::Process::StandardErrColumns();
Chris Lattner5f9e2722011-07-23 10:55:15 +00002667 CmdArgs.push_back(Args.MakeArgString(Twine(N)));
Daniel Dunbar55efe142009-11-04 06:24:47 +00002668 }
2669
John McCalla880b192013-02-19 01:57:35 +00002670 // -fvisibility= and -fvisibility-ms-compat are of a piece.
2671 if (const Arg *A = Args.getLastArg(options::OPT_fvisibility_EQ,
2672 options::OPT_fvisibility_ms_compat)) {
2673 if (A->getOption().matches(options::OPT_fvisibility_EQ)) {
2674 CmdArgs.push_back("-fvisibility");
2675 CmdArgs.push_back(A->getValue());
2676 } else {
2677 assert(A->getOption().matches(options::OPT_fvisibility_ms_compat));
2678 CmdArgs.push_back("-fvisibility");
2679 CmdArgs.push_back("hidden");
2680 CmdArgs.push_back("-ftype-visibility");
2681 CmdArgs.push_back("default");
2682 }
Daniel Dunbarba8d8612009-12-03 18:42:11 +00002683 }
2684
Douglas Gregor7cf84d62010-06-15 17:05:35 +00002685 Args.AddLastArg(CmdArgs, options::OPT_fvisibility_inlines_hidden);
Michael J. Spencer20249a12010-10-21 03:16:25 +00002686
Hans Wennborgde981f32012-06-28 08:01:44 +00002687 Args.AddLastArg(CmdArgs, options::OPT_ftlsmodel_EQ);
2688
Daniel Dunbar0a80ba72010-03-20 04:52:14 +00002689 // -fhosted is default.
Chad Rosierafc4baa2012-03-26 22:04:46 +00002690 if (Args.hasFlag(options::OPT_ffreestanding, options::OPT_fhosted, false) ||
2691 KernelOrKext)
Daniel Dunbar0a80ba72010-03-20 04:52:14 +00002692 CmdArgs.push_back("-ffreestanding");
2693
Daniel Dunbarba8d8612009-12-03 18:42:11 +00002694 // Forward -f (flag) options which we can pass directly.
Daniel Dunbar3aaf0822009-04-07 21:51:40 +00002695 Args.AddLastArg(CmdArgs, options::OPT_femit_all_decls);
Daniel Dunbar3aaf0822009-04-07 21:51:40 +00002696 Args.AddLastArg(CmdArgs, options::OPT_fheinous_gnu_extensions);
Devang Patelc69e1cf2010-09-30 19:05:55 +00002697 Args.AddLastArg(CmdArgs, options::OPT_flimit_debug_info);
Devang Patel033be8b2011-11-04 20:05:58 +00002698 Args.AddLastArg(CmdArgs, options::OPT_fno_limit_debug_info);
Eric Christophere88c4512011-10-25 07:13:06 +00002699 Args.AddLastArg(CmdArgs, options::OPT_fno_operator_names);
Anton Yartsev17ba2672011-12-23 20:23:19 +00002700 Args.AddLastArg(CmdArgs, options::OPT_faltivec);
Richard Trieu246b6aa2012-06-26 18:18:47 +00002701 Args.AddLastArg(CmdArgs, options::OPT_fdiagnostics_show_template_tree);
2702 Args.AddLastArg(CmdArgs, options::OPT_fno_elide_type);
Chad Rosier4574c3d2012-03-13 23:45:51 +00002703
Peter Collingbourne52ca70d2013-04-09 04:35:11 +00002704 SanitizerArgs Sanitize(getToolChain(), Args);
Richard Smithc4dabad2012-11-05 22:04:41 +00002705 Sanitize.addArgs(Args, CmdArgs);
2706
Will Dietz2d382d12012-12-30 20:53:28 +00002707 if (!Args.hasFlag(options::OPT_fsanitize_recover,
2708 options::OPT_fno_sanitize_recover,
2709 true))
2710 CmdArgs.push_back("-fno-sanitize-recover");
2711
Chad Rosier78d85b12013-01-29 23:31:22 +00002712 if (Args.hasArg(options::OPT_fcatch_undefined_behavior) ||
2713 Args.hasFlag(options::OPT_fsanitize_undefined_trap_on_error,
2714 options::OPT_fno_sanitize_undefined_trap_on_error, false))
2715 CmdArgs.push_back("-fsanitize-undefined-trap-on-error");
2716
Eric Christopher98654c92013-02-19 06:16:53 +00002717 // Report an error for -faltivec on anything other than PowerPC.
Chad Rosier4574c3d2012-03-13 23:45:51 +00002718 if (const Arg *A = Args.getLastArg(options::OPT_faltivec))
2719 if (!(getToolChain().getTriple().getArch() == llvm::Triple::ppc ||
2720 getToolChain().getTriple().getArch() == llvm::Triple::ppc64))
2721 D.Diag(diag::err_drv_argument_only_allowed_with)
2722 << A->getAsString(Args) << "ppc/ppc64";
2723
Daniel Dunbarbbe8e3e2011-03-01 18:49:30 +00002724 if (getToolChain().SupportsProfiling())
2725 Args.AddLastArg(CmdArgs, options::OPT_pg);
Daniel Dunbar8c6fa842010-03-16 16:57:46 +00002726
2727 // -flax-vector-conversions is default.
2728 if (!Args.hasFlag(options::OPT_flax_vector_conversions,
2729 options::OPT_fno_lax_vector_conversions))
2730 CmdArgs.push_back("-fno-lax-vector-conversions");
2731
Fariborz Jahanianb466d012011-01-07 01:05:02 +00002732 if (Args.getLastArg(options::OPT_fapple_kext))
2733 CmdArgs.push_back("-fapple-kext");
2734
David Blaikie940152f2012-06-14 18:55:27 +00002735 if (Args.hasFlag(options::OPT_frewrite_includes,
2736 options::OPT_fno_rewrite_includes, false))
2737 CmdArgs.push_back("-frewrite-includes");
2738
Fariborz Jahanian34e65772009-05-22 20:17:16 +00002739 Args.AddLastArg(CmdArgs, options::OPT_fobjc_sender_dependent_dispatch);
Chris Lattner182e0922009-04-21 05:34:31 +00002740 Args.AddLastArg(CmdArgs, options::OPT_fdiagnostics_print_source_range_info);
Douglas Gregor4786c152010-08-19 20:24:43 +00002741 Args.AddLastArg(CmdArgs, options::OPT_fdiagnostics_parseable_fixits);
Daniel Dunbar3aaf0822009-04-07 21:51:40 +00002742 Args.AddLastArg(CmdArgs, options::OPT_ftime_report);
2743 Args.AddLastArg(CmdArgs, options::OPT_ftrapv);
David Chisnall7f18e672010-09-17 18:29:54 +00002744
2745 if (Arg *A = Args.getLastArg(options::OPT_ftrapv_handler_EQ)) {
2746 CmdArgs.push_back("-ftrapv-handler");
Richard Smith1d489cf2012-11-01 04:30:05 +00002747 CmdArgs.push_back(A->getValue());
David Chisnall7f18e672010-09-17 18:29:54 +00002748 }
2749
Bob Wilson71fd6cc2012-02-03 06:27:22 +00002750 Args.AddLastArg(CmdArgs, options::OPT_ftrap_function_EQ);
Evan Cheng49af1f32011-04-08 21:37:45 +00002751
Chandler Carruth5adb5a82011-03-27 00:04:55 +00002752 // -fno-strict-overflow implies -fwrapv if it isn't disabled, but
2753 // -fstrict-overflow won't turn off an explicitly enabled -fwrapv.
2754 if (Arg *A = Args.getLastArg(options::OPT_fwrapv,
2755 options::OPT_fno_wrapv)) {
2756 if (A->getOption().matches(options::OPT_fwrapv))
2757 CmdArgs.push_back("-fwrapv");
2758 } else if (Arg *A = Args.getLastArg(options::OPT_fstrict_overflow,
2759 options::OPT_fno_strict_overflow)) {
2760 if (A->getOption().matches(options::OPT_fno_strict_overflow))
2761 CmdArgs.push_back("-fwrapv");
2762 }
Daniel Dunbar3aaf0822009-04-07 21:51:40 +00002763 Args.AddLastArg(CmdArgs, options::OPT_fwritable_strings);
Eric Christopherf84d4092010-08-07 23:08:14 +00002764 Args.AddLastArg(CmdArgs, options::OPT_funroll_loops);
Daniel Dunbar1d460332009-03-18 10:01:51 +00002765
Daniel Dunbar5345c392009-09-03 04:54:28 +00002766 Args.AddLastArg(CmdArgs, options::OPT_pthread);
2767
Mahesha Sf3b52312012-10-27 07:47:56 +00002768
Daniel Dunbar9e5cc6b2009-11-17 08:07:36 +00002769 // -stack-protector=0 is default.
2770 unsigned StackProtectorLevel = 0;
Bill Wendling45483f72009-06-28 07:36:13 +00002771 if (Arg *A = Args.getLastArg(options::OPT_fno_stack_protector,
2772 options::OPT_fstack_protector_all,
2773 options::OPT_fstack_protector)) {
Daniel Dunbar9e5cc6b2009-11-17 08:07:36 +00002774 if (A->getOption().matches(options::OPT_fstack_protector))
2775 StackProtectorLevel = 1;
2776 else if (A->getOption().matches(options::OPT_fstack_protector_all))
2777 StackProtectorLevel = 2;
Nico Weber2fef1112011-08-23 07:38:27 +00002778 } else {
2779 StackProtectorLevel =
2780 getToolChain().GetDefaultStackProtectorLevel(KernelOrKext);
2781 }
Daniel Dunbar9e5cc6b2009-11-17 08:07:36 +00002782 if (StackProtectorLevel) {
2783 CmdArgs.push_back("-stack-protector");
Chris Lattner5f9e2722011-07-23 10:55:15 +00002784 CmdArgs.push_back(Args.MakeArgString(Twine(StackProtectorLevel)));
Joerg Sonnenberger53b43a72012-09-12 13:51:14 +00002785 }
Chad Rosiera7afeb02012-08-21 16:16:06 +00002786
Joerg Sonnenberger53b43a72012-09-12 13:51:14 +00002787 // --param ssp-buffer-size=
2788 for (arg_iterator it = Args.filtered_begin(options::OPT__param),
2789 ie = Args.filtered_end(); it != ie; ++it) {
Richard Smith1d489cf2012-11-01 04:30:05 +00002790 StringRef Str((*it)->getValue());
Joerg Sonnenberger53b43a72012-09-12 13:51:14 +00002791 if (Str.startswith("ssp-buffer-size=")) {
2792 if (StackProtectorLevel) {
Chad Rosiera7afeb02012-08-21 16:16:06 +00002793 CmdArgs.push_back("-stack-protector-buffer-size");
2794 // FIXME: Verify the argument is a valid integer.
2795 CmdArgs.push_back(Args.MakeArgString(Str.drop_front(16)));
Chad Rosiera7afeb02012-08-21 16:16:06 +00002796 }
Joerg Sonnenberger53b43a72012-09-12 13:51:14 +00002797 (*it)->claim();
Chad Rosiera7afeb02012-08-21 16:16:06 +00002798 }
Bill Wendling45483f72009-06-28 07:36:13 +00002799 }
2800
Nick Lewycky4e785c92011-12-06 03:33:03 +00002801 // Translate -mstackrealign
2802 if (Args.hasFlag(options::OPT_mstackrealign, options::OPT_mno_stackrealign,
2803 false)) {
2804 CmdArgs.push_back("-backend-option");
2805 CmdArgs.push_back("-force-align-stack");
2806 }
2807 if (!Args.hasFlag(options::OPT_mno_stackrealign, options::OPT_mstackrealign,
2808 false)) {
2809 CmdArgs.push_back(Args.MakeArgString("-mstackrealign"));
2810 }
2811
Joerg Sonnenbergere9d11db2011-12-05 23:05:23 +00002812 if (Args.hasArg(options::OPT_mstack_alignment)) {
2813 StringRef alignment = Args.getLastArgValue(options::OPT_mstack_alignment);
2814 CmdArgs.push_back(Args.MakeArgString("-mstack-alignment=" + alignment));
Eric Christopher1a584022011-05-02 21:18:22 +00002815 }
Chad Rosier586a0612012-11-29 00:42:06 +00002816 // -mkernel implies -mstrict-align; don't add the redundant option.
2817 if (Args.hasArg(options::OPT_mstrict_align) && !KernelOrKext) {
Chad Rosier485577d2012-11-09 18:27:01 +00002818 CmdArgs.push_back("-backend-option");
2819 CmdArgs.push_back("-arm-strict-align");
Chad Rosier7e293272012-11-09 17:29:19 +00002820 }
Eric Christopher88b7cf02011-08-19 00:30:14 +00002821
Daniel Dunbar48d1ef72009-04-07 21:16:11 +00002822 // Forward -f options with positive and negative forms; we translate
2823 // these by hand.
2824
Fariborz Jahanianb466d012011-01-07 01:05:02 +00002825 if (Args.hasArg(options::OPT_mkernel)) {
Daniel Dunbar2843c192011-02-04 17:24:47 +00002826 if (!Args.hasArg(options::OPT_fapple_kext) && types::isCXX(InputType))
Fariborz Jahanianb466d012011-01-07 01:05:02 +00002827 CmdArgs.push_back("-fapple-kext");
2828 if (!Args.hasArg(options::OPT_fbuiltin))
2829 CmdArgs.push_back("-fno-builtin");
Chad Rosier3d265502012-03-26 21:29:17 +00002830 Args.ClaimAllArgs(options::OPT_fno_builtin);
Fariborz Jahanianb466d012011-01-07 01:05:02 +00002831 }
Daniel Dunbar9e5cc6b2009-11-17 08:07:36 +00002832 // -fbuiltin is default.
Fariborz Jahanianb466d012011-01-07 01:05:02 +00002833 else if (!Args.hasFlag(options::OPT_fbuiltin, options::OPT_fno_builtin))
Daniel Dunbar53e84842009-11-19 04:55:23 +00002834 CmdArgs.push_back("-fno-builtin");
Daniel Dunbar48d1ef72009-04-07 21:16:11 +00002835
Nuno Lopesfc284482009-12-16 16:59:22 +00002836 if (!Args.hasFlag(options::OPT_fassume_sane_operator_new,
2837 options::OPT_fno_assume_sane_operator_new))
2838 CmdArgs.push_back("-fno-assume-sane-operator-new");
2839
Daniel Dunbar9e5cc6b2009-11-17 08:07:36 +00002840 // -fblocks=0 is default.
2841 if (Args.hasFlag(options::OPT_fblocks, options::OPT_fno_blocks,
David Chisnalle6533ff2011-02-28 17:11:43 +00002842 getToolChain().IsBlocksDefault()) ||
2843 (Args.hasArg(options::OPT_fgnu_runtime) &&
2844 Args.hasArg(options::OPT_fobjc_nonfragile_abi) &&
2845 !Args.hasArg(options::OPT_fno_blocks))) {
Daniel Dunbar9e5cc6b2009-11-17 08:07:36 +00002846 CmdArgs.push_back("-fblocks");
John McCall13db5cf2011-09-09 20:41:01 +00002847
2848 if (!Args.hasArg(options::OPT_fgnu_runtime) &&
2849 !getToolChain().hasBlocksRuntime())
2850 CmdArgs.push_back("-fblocks-runtime-optional");
David Chisnall5e530af2009-11-17 19:33:30 +00002851 }
Daniel Dunbar48d1ef72009-04-07 21:16:11 +00002852
Douglas Gregor64554ba2012-01-18 15:19:58 +00002853 // -fmodules enables modules (off by default). However, for C++/Objective-C++,
2854 // users must also pass -fcxx-modules. The latter flag will disappear once the
2855 // modules implementation is solid for C++/Objective-C++ programs as well.
Douglas Gregorf43b7212013-01-16 01:23:41 +00002856 bool HaveModules = false;
Douglas Gregor64554ba2012-01-18 15:19:58 +00002857 if (Args.hasFlag(options::OPT_fmodules, options::OPT_fno_modules, false)) {
2858 bool AllowedInCXX = Args.hasFlag(options::OPT_fcxx_modules,
2859 options::OPT_fno_cxx_modules,
2860 false);
Douglas Gregorf43b7212013-01-16 01:23:41 +00002861 if (AllowedInCXX || !types::isCXX(InputType)) {
Douglas Gregor64554ba2012-01-18 15:19:58 +00002862 CmdArgs.push_back("-fmodules");
Douglas Gregorf43b7212013-01-16 01:23:41 +00002863 HaveModules = true;
2864 }
2865 }
2866
Douglas Gregor953a61f2013-02-07 19:01:24 +00002867 // If a module path was provided, pass it along. Otherwise, use a temporary
2868 // directory.
2869 if (Arg *A = Args.getLastArg(options::OPT_fmodules_cache_path)) {
2870 A->claim();
2871 if (HaveModules) {
2872 A->render(Args, CmdArgs);
2873 }
2874 } else if (HaveModules) {
2875 SmallString<128> DefaultModuleCache;
2876 llvm::sys::path::system_temp_directory(/*erasedOnReboot=*/false,
2877 DefaultModuleCache);
Douglas Gregor892b6fb2013-03-21 21:48:48 +00002878 llvm::sys::path::append(DefaultModuleCache, "org.llvm.clang");
2879 llvm::sys::path::append(DefaultModuleCache, "ModuleCache");
Douglas Gregor250172a2013-02-07 22:59:12 +00002880 const char Arg[] = "-fmodules-cache-path=";
2881 DefaultModuleCache.insert(DefaultModuleCache.begin(),
2882 Arg, Arg + strlen(Arg));
Douglas Gregor953a61f2013-02-07 19:01:24 +00002883 CmdArgs.push_back(Args.MakeArgString(DefaultModuleCache));
2884 }
2885
2886 // Pass through all -fmodules-ignore-macro arguments.
2887 Args.AddAllArgs(CmdArgs, options::OPT_fmodules_ignore_macro);
Douglas Gregord44d2872013-03-25 21:19:16 +00002888 Args.AddLastArg(CmdArgs, options::OPT_fmodules_prune_interval);
2889 Args.AddLastArg(CmdArgs, options::OPT_fmodules_prune_after);
Douglas Gregor953a61f2013-02-07 19:01:24 +00002890
Douglas Gregorf43b7212013-01-16 01:23:41 +00002891 // -fmodules-autolink (on by default when modules is enabled) automatically
Ted Kremenek4c1adcc2013-03-12 21:08:08 +00002892 // links against libraries for imported modules. This requires the
2893 // integrated assembler.
Rafael Espindolaaf370e62013-03-18 18:10:27 +00002894 if (HaveModules && getToolChain().useIntegratedAs() &&
Douglas Gregorf43b7212013-01-16 01:23:41 +00002895 Args.hasFlag(options::OPT_fmodules_autolink,
2896 options::OPT_fno_modules_autolink,
2897 true)) {
2898 CmdArgs.push_back("-fmodules-autolink");
Douglas Gregor64554ba2012-01-18 15:19:58 +00002899 }
Douglas Gregor7025d2c2012-01-03 17:13:05 +00002900
John McCall32579cf2010-04-09 19:12:06 +00002901 // -faccess-control is default.
John McCall7002f4c2010-04-09 19:03:51 +00002902 if (Args.hasFlag(options::OPT_fno_access_control,
2903 options::OPT_faccess_control,
John McCall32579cf2010-04-09 19:12:06 +00002904 false))
John McCall7002f4c2010-04-09 19:03:51 +00002905 CmdArgs.push_back("-fno-access-control");
John McCall3ddd6e02010-03-17 01:32:13 +00002906
Anders Carlssona4c24752010-11-21 00:09:52 +00002907 // -felide-constructors is the default.
2908 if (Args.hasFlag(options::OPT_fno_elide_constructors,
2909 options::OPT_felide_constructors,
2910 false))
2911 CmdArgs.push_back("-fno-elide-constructors");
2912
Daniel Dunbar0be42c42009-11-17 07:06:20 +00002913 // -frtti is default.
Chad Rosierafc4baa2012-03-26 22:04:46 +00002914 if (!Args.hasFlag(options::OPT_frtti, options::OPT_fno_rtti) ||
Richard Smithc4dabad2012-11-05 22:04:41 +00002915 KernelOrKext) {
Daniel Dunbar53e84842009-11-19 04:55:23 +00002916 CmdArgs.push_back("-fno-rtti");
Mike Stump738f8c22009-07-31 23:15:31 +00002917
Richard Smithc4dabad2012-11-05 22:04:41 +00002918 // -fno-rtti cannot usefully be combined with -fsanitize=vptr.
Alexey Samsonovbb1071c2012-11-06 15:09:03 +00002919 if (Sanitize.sanitizesVptr()) {
NAKAMURA Takumi03c60762012-11-06 22:02:00 +00002920 std::string NoRttiArg =
Richard Smithc4dabad2012-11-05 22:04:41 +00002921 Args.getLastArg(options::OPT_mkernel,
2922 options::OPT_fapple_kext,
Richard Smith04fd3822012-11-06 01:12:02 +00002923 options::OPT_fno_rtti)->getAsString(Args);
Richard Smithc4dabad2012-11-05 22:04:41 +00002924 D.Diag(diag::err_drv_argument_not_allowed_with)
2925 << "-fsanitize=vptr" << NoRttiArg;
2926 }
2927 }
2928
Tony Linthicum96319392011-12-12 21:14:55 +00002929 // -fshort-enums=0 is default for all architectures except Hexagon.
Argyrios Kyrtzidis9a2b9d72010-10-08 00:25:19 +00002930 if (Args.hasFlag(options::OPT_fshort_enums,
Tony Linthicum96319392011-12-12 21:14:55 +00002931 options::OPT_fno_short_enums,
2932 getToolChain().getTriple().getArch() ==
2933 llvm::Triple::hexagon))
Argyrios Kyrtzidis9a2b9d72010-10-08 00:25:19 +00002934 CmdArgs.push_back("-fshort-enums");
2935
Daniel Dunbar1f95e652009-11-17 06:37:03 +00002936 // -fsigned-char is default.
Daniel Dunbar6d2eb4d2009-11-25 10:14:30 +00002937 if (!Args.hasFlag(options::OPT_fsigned_char, options::OPT_funsigned_char,
Daniel Dunbar1f95e652009-11-17 06:37:03 +00002938 isSignedCharDefault(getToolChain().getTriple())))
Daniel Dunbar76743522009-11-29 02:39:08 +00002939 CmdArgs.push_back("-fno-signed-char");
Eli Friedman5a779732009-06-05 07:21:14 +00002940
Anders Carlssona508b7d2010-02-06 23:23:06 +00002941 // -fthreadsafe-static is default.
Michael J. Spencer20249a12010-10-21 03:16:25 +00002942 if (!Args.hasFlag(options::OPT_fthreadsafe_statics,
Anders Carlssona508b7d2010-02-06 23:23:06 +00002943 options::OPT_fno_threadsafe_statics))
2944 CmdArgs.push_back("-fno-threadsafe-statics");
2945
Daniel Dunbarefb0fa92010-03-20 04:15:41 +00002946 // -fuse-cxa-atexit is default.
Chad Rosierafc4baa2012-03-26 22:04:46 +00002947 if (!Args.hasFlag(options::OPT_fuse_cxa_atexit,
2948 options::OPT_fno_use_cxa_atexit,
2949 getToolChain().getTriple().getOS() != llvm::Triple::Cygwin &&
Tony Linthicum96319392011-12-12 21:14:55 +00002950 getToolChain().getTriple().getOS() != llvm::Triple::MinGW32 &&
Chad Rosierafc4baa2012-03-26 22:04:46 +00002951 getToolChain().getTriple().getArch() != llvm::Triple::hexagon) ||
2952 KernelOrKext)
Daniel Dunbarefb0fa92010-03-20 04:15:41 +00002953 CmdArgs.push_back("-fno-use-cxa-atexit");
2954
Daniel Dunbar0be42c42009-11-17 07:06:20 +00002955 // -fms-extensions=0 is default.
Daniel Dunbar6d2eb4d2009-11-25 10:14:30 +00002956 if (Args.hasFlag(options::OPT_fms_extensions, options::OPT_fno_ms_extensions,
Daniel Dunbar0be42c42009-11-17 07:06:20 +00002957 getToolChain().getTriple().getOS() == llvm::Triple::Win32))
2958 CmdArgs.push_back("-fms-extensions");
2959
Francois Pichetae556082011-09-17 04:32:15 +00002960 // -fms-compatibility=0 is default.
Douglas Gregorba97b6e2011-10-24 15:49:38 +00002961 if (Args.hasFlag(options::OPT_fms_compatibility,
2962 options::OPT_fno_ms_compatibility,
2963 (getToolChain().getTriple().getOS() == llvm::Triple::Win32 &&
2964 Args.hasFlag(options::OPT_fms_extensions,
2965 options::OPT_fno_ms_extensions,
2966 true))))
Francois Pichetae556082011-09-17 04:32:15 +00002967 CmdArgs.push_back("-fms-compatibility");
2968
Michael J. Spencerdae4ac42010-10-21 05:21:48 +00002969 // -fmsc-version=1300 is default.
2970 if (Args.hasFlag(options::OPT_fms_extensions, options::OPT_fno_ms_extensions,
2971 getToolChain().getTriple().getOS() == llvm::Triple::Win32) ||
2972 Args.hasArg(options::OPT_fmsc_version)) {
Chris Lattner5f9e2722011-07-23 10:55:15 +00002973 StringRef msc_ver = Args.getLastArgValue(options::OPT_fmsc_version);
Michael J. Spencerdae4ac42010-10-21 05:21:48 +00002974 if (msc_ver.empty())
2975 CmdArgs.push_back("-fmsc-version=1300");
2976 else
2977 CmdArgs.push_back(Args.MakeArgString("-fmsc-version=" + msc_ver));
2978 }
2979
2980
Eric Christophercfc01e42013-02-18 00:38:31 +00002981 // -fno-borland-extensions is default.
Dawn Perchik400b6072010-09-02 23:59:25 +00002982 if (Args.hasFlag(options::OPT_fborland_extensions,
2983 options::OPT_fno_borland_extensions, false))
2984 CmdArgs.push_back("-fborland-extensions");
2985
Francois Pichet8efcc012011-09-01 16:38:08 +00002986 // -fno-delayed-template-parsing is default, except for Windows where MSVC STL
2987 // needs it.
Francois Pichet8387e2a2011-04-22 22:18:13 +00002988 if (Args.hasFlag(options::OPT_fdelayed_template_parsing,
2989 options::OPT_fno_delayed_template_parsing,
Francois Pichet8efcc012011-09-01 16:38:08 +00002990 getToolChain().getTriple().getOS() == llvm::Triple::Win32))
Francois Pichet805bc1f2011-08-26 00:22:34 +00002991 CmdArgs.push_back("-fdelayed-template-parsing");
Francois Pichet8387e2a2011-04-22 22:18:13 +00002992
Chandler Carrutheb5d7b72010-04-17 20:17:31 +00002993 // -fgnu-keywords default varies depending on language; only pass if
2994 // specified.
2995 if (Arg *A = Args.getLastArg(options::OPT_fgnu_keywords,
Daniel Dunbar40788d92010-04-24 17:56:39 +00002996 options::OPT_fno_gnu_keywords))
2997 A->render(Args, CmdArgs);
Chandler Carrutheb5d7b72010-04-17 20:17:31 +00002998
Rafael Espindola01ba8542011-06-02 17:30:53 +00002999 if (Args.hasFlag(options::OPT_fgnu89_inline,
3000 options::OPT_fno_gnu89_inline,
3001 false))
Rafael Espindolafb3f4aa2011-06-02 16:13:27 +00003002 CmdArgs.push_back("-fgnu89-inline");
3003
Chad Rosierfc055f92012-03-15 22:31:42 +00003004 if (Args.hasArg(options::OPT_fno_inline))
3005 CmdArgs.push_back("-fno-inline");
3006
Chad Rosier634a4b12012-03-06 21:17:19 +00003007 if (Args.hasArg(options::OPT_fno_inline_functions))
3008 CmdArgs.push_back("-fno-inline-functions");
Chad Rosier250008b2012-03-06 18:49:20 +00003009
John McCall260611a2012-06-20 06:18:46 +00003010 ObjCRuntime objcRuntime = AddObjCRuntimeArgs(Args, CmdArgs, rewriteKind);
John McCall9f084a32011-07-06 00:26:06 +00003011
John McCall260611a2012-06-20 06:18:46 +00003012 // -fobjc-dispatch-method is only relevant with the nonfragile-abi, and
3013 // legacy is the default.
3014 if (objcRuntime.isNonFragile()) {
David Chisnall3c3ccd22011-09-30 13:32:35 +00003015 if (!Args.hasFlag(options::OPT_fobjc_legacy_dispatch,
3016 options::OPT_fno_objc_legacy_dispatch,
David Chisnall2c7886d2012-07-04 11:52:24 +00003017 objcRuntime.isLegacyDispatchDefaultForArch(
3018 getToolChain().getTriple().getArch()))) {
David Chisnall3c3ccd22011-09-30 13:32:35 +00003019 if (getToolChain().UseObjCMixedDispatch())
3020 CmdArgs.push_back("-fobjc-dispatch-method=mixed");
3021 else
3022 CmdArgs.push_back("-fobjc-dispatch-method=non-legacy");
3023 }
3024 }
3025
Nico Weberdf423542012-03-09 21:19:44 +00003026 // -fobjc-default-synthesize-properties=1 is default. This only has an effect
3027 // if the nonfragile objc abi is used.
Fariborz Jahaniane51fe092012-04-09 18:58:55 +00003028 if (getToolChain().IsObjCDefaultSynthPropertiesDefault()) {
David Chisnall3c3ccd22011-09-30 13:32:35 +00003029 CmdArgs.push_back("-fobjc-default-synthesize-properties");
3030 }
3031
Fariborz Jahanian3d145f62012-11-15 19:02:45 +00003032 // -fencode-extended-block-signature=1 is default.
3033 if (getToolChain().IsEncodeExtendedBlockSignatureDefault()) {
3034 CmdArgs.push_back("-fencode-extended-block-signature");
3035 }
3036
John McCall9f084a32011-07-06 00:26:06 +00003037 // Allow -fno-objc-arr to trump -fobjc-arr/-fobjc-arc.
3038 // NOTE: This logic is duplicated in ToolChains.cpp.
3039 bool ARC = isObjCAutoRefCount(Args);
3040 if (ARC) {
John McCall0a7dd782012-08-21 02:47:43 +00003041 getToolChain().CheckObjCARC();
Argyrios Kyrtzidis5840dd92012-02-29 03:43:52 +00003042
John McCall9f084a32011-07-06 00:26:06 +00003043 CmdArgs.push_back("-fobjc-arc");
3044
Chandler Carruth7ffa0322011-11-04 07:34:47 +00003045 // FIXME: It seems like this entire block, and several around it should be
3046 // wrapped in isObjC, but for now we just use it here as this is where it
3047 // was being used previously.
3048 if (types::isCXX(InputType) && types::isObjC(InputType)) {
3049 if (getToolChain().GetCXXStdlibType(Args) == ToolChain::CST_Libcxx)
3050 CmdArgs.push_back("-fobjc-arc-cxxlib=libc++");
3051 else
3052 CmdArgs.push_back("-fobjc-arc-cxxlib=libstdc++");
3053 }
3054
John McCall9f084a32011-07-06 00:26:06 +00003055 // Allow the user to enable full exceptions code emission.
3056 // We define off for Objective-CC, on for Objective-C++.
3057 if (Args.hasFlag(options::OPT_fobjc_arc_exceptions,
3058 options::OPT_fno_objc_arc_exceptions,
3059 /*default*/ types::isCXX(InputType)))
3060 CmdArgs.push_back("-fobjc-arc-exceptions");
3061 }
3062
3063 // -fobjc-infer-related-result-type is the default, except in the Objective-C
3064 // rewriter.
John McCall260611a2012-06-20 06:18:46 +00003065 if (rewriteKind != RK_None)
John McCall9f084a32011-07-06 00:26:06 +00003066 CmdArgs.push_back("-fno-objc-infer-related-result-type");
Eric Christopher88b7cf02011-08-19 00:30:14 +00003067
John McCall9f084a32011-07-06 00:26:06 +00003068 // Handle -fobjc-gc and -fobjc-gc-only. They are exclusive, and -fobjc-gc-only
3069 // takes precedence.
3070 const Arg *GCArg = Args.getLastArg(options::OPT_fobjc_gc_only);
3071 if (!GCArg)
3072 GCArg = Args.getLastArg(options::OPT_fobjc_gc);
3073 if (GCArg) {
3074 if (ARC) {
Chris Lattner5f9e2722011-07-23 10:55:15 +00003075 D.Diag(diag::err_drv_objc_gc_arr)
John McCall9f084a32011-07-06 00:26:06 +00003076 << GCArg->getAsString(Args);
3077 } else if (getToolChain().SupportsObjCGC()) {
3078 GCArg->render(Args, CmdArgs);
3079 } else {
3080 // FIXME: We should move this to a hard error.
Chris Lattner5f9e2722011-07-23 10:55:15 +00003081 D.Diag(diag::warn_drv_objc_gc_unsupported)
John McCall9f084a32011-07-06 00:26:06 +00003082 << GCArg->getAsString(Args);
3083 }
3084 }
3085
John McCalld71315c2011-06-22 00:53:57 +00003086 // Add exception args.
3087 addExceptionArgs(Args, InputType, getToolChain().getTriple(),
John McCall260611a2012-06-20 06:18:46 +00003088 KernelOrKext, objcRuntime, CmdArgs);
John McCalld71315c2011-06-22 00:53:57 +00003089
3090 if (getToolChain().UseSjLjExceptions())
3091 CmdArgs.push_back("-fsjlj-exceptions");
3092
3093 // C++ "sane" operator new.
Daniel Dunbar984eb862010-02-01 21:07:25 +00003094 if (!Args.hasFlag(options::OPT_fassume_sane_operator_new,
3095 options::OPT_fno_assume_sane_operator_new))
3096 CmdArgs.push_back("-fno-assume-sane-operator-new");
3097
Daniel Dunbarf35f14d2010-04-27 15:34:57 +00003098 // -fconstant-cfstrings is default, and may be subject to argument translation
3099 // on Darwin.
3100 if (!Args.hasFlag(options::OPT_fconstant_cfstrings,
3101 options::OPT_fno_constant_cfstrings) ||
3102 !Args.hasFlag(options::OPT_mconstant_cfstrings,
3103 options::OPT_mno_constant_cfstrings))
3104 CmdArgs.push_back("-fno-constant-cfstrings");
3105
John Thompsona6fda122009-11-05 20:14:16 +00003106 // -fshort-wchar default varies depending on platform; only
3107 // pass if specified.
Daniel Dunbar1744a352010-04-27 15:35:03 +00003108 if (Arg *A = Args.getLastArg(options::OPT_fshort_wchar))
3109 A->render(Args, CmdArgs);
John Thompsona6fda122009-11-05 20:14:16 +00003110
Daniel Dunbaree848a72009-10-29 02:39:57 +00003111 // -fno-pascal-strings is default, only pass non-default. If the tool chain
3112 // happened to translate to -mpascal-strings, we want to back translate here.
Daniel Dunbar82d00682009-04-07 23:51:44 +00003113 //
3114 // FIXME: This is gross; that translation should be pulled from the
3115 // tool chain.
Daniel Dunbarc21c4852009-04-08 23:54:23 +00003116 if (Args.hasFlag(options::OPT_fpascal_strings,
Daniel Dunbar82d00682009-04-07 23:51:44 +00003117 options::OPT_fno_pascal_strings,
3118 false) ||
3119 Args.hasFlag(options::OPT_mpascal_strings,
3120 options::OPT_mno_pascal_strings,
3121 false))
Daniel Dunbar48d1ef72009-04-07 21:16:11 +00003122 CmdArgs.push_back("-fpascal-strings");
NAKAMURA Takumi125b4cb2011-02-17 08:50:50 +00003123
Daniel Dunbar88934e82011-10-05 21:04:55 +00003124 // Honor -fpack-struct= and -fpack-struct, if given. Note that
3125 // -fno-pack-struct doesn't apply to -fpack-struct=.
3126 if (Arg *A = Args.getLastArg(options::OPT_fpack_struct_EQ)) {
James Molloy8049c442012-05-02 07:56:14 +00003127 std::string PackStructStr = "-fpack-struct=";
Richard Smith1d489cf2012-11-01 04:30:05 +00003128 PackStructStr += A->getValue();
James Molloy8049c442012-05-02 07:56:14 +00003129 CmdArgs.push_back(Args.MakeArgString(PackStructStr));
Daniel Dunbar88934e82011-10-05 21:04:55 +00003130 } else if (Args.hasFlag(options::OPT_fpack_struct,
3131 options::OPT_fno_pack_struct, false)) {
James Molloy8049c442012-05-02 07:56:14 +00003132 CmdArgs.push_back("-fpack-struct=1");
Daniel Dunbar88934e82011-10-05 21:04:55 +00003133 }
3134
Eric Christopher6c7db892013-02-18 01:16:37 +00003135 if (KernelOrKext) {
Fariborz Jahanianb466d012011-01-07 01:05:02 +00003136 if (!Args.hasArg(options::OPT_fcommon))
3137 CmdArgs.push_back("-fno-common");
Chad Rosierec09b3e2012-03-26 21:35:40 +00003138 Args.ClaimAllArgs(options::OPT_fno_common);
Fariborz Jahanianb466d012011-01-07 01:05:02 +00003139 }
Daniel Dunbar88934e82011-10-05 21:04:55 +00003140
Daniel Dunbar48d1ef72009-04-07 21:16:11 +00003141 // -fcommon is default, only pass non-default.
Fariborz Jahanianb466d012011-01-07 01:05:02 +00003142 else if (!Args.hasFlag(options::OPT_fcommon, options::OPT_fno_common))
Daniel Dunbar48d1ef72009-04-07 21:16:11 +00003143 CmdArgs.push_back("-fno-common");
3144
Daniel Dunbar70d3c922009-04-15 02:37:43 +00003145 // -fsigned-bitfields is default, and clang doesn't yet support
Daniel Dunbar06205ca2010-10-15 22:30:42 +00003146 // -funsigned-bitfields.
Mike Stump1eb44332009-09-09 15:08:12 +00003147 if (!Args.hasFlag(options::OPT_fsigned_bitfields,
Daniel Dunbar70d3c922009-04-15 02:37:43 +00003148 options::OPT_funsigned_bitfields))
Chris Lattner5f9e2722011-07-23 10:55:15 +00003149 D.Diag(diag::warn_drv_clang_unsupported)
Daniel Dunbar70d3c922009-04-15 02:37:43 +00003150 << Args.getLastArg(options::OPT_funsigned_bitfields)->getAsString(Args);
3151
Daniel Dunbar06205ca2010-10-15 22:30:42 +00003152 // -fsigned-bitfields is default, and clang doesn't support -fno-for-scope.
3153 if (!Args.hasFlag(options::OPT_ffor_scope,
3154 options::OPT_fno_for_scope))
Chris Lattner5f9e2722011-07-23 10:55:15 +00003155 D.Diag(diag::err_drv_clang_unsupported)
Daniel Dunbar06205ca2010-10-15 22:30:42 +00003156 << Args.getLastArg(options::OPT_fno_for_scope)->getAsString(Args);
3157
Jeffrey Yasskin0ea22fd2010-06-08 04:56:20 +00003158 // -fcaret-diagnostics is default.
3159 if (!Args.hasFlag(options::OPT_fcaret_diagnostics,
3160 options::OPT_fno_caret_diagnostics, true))
3161 CmdArgs.push_back("-fno-caret-diagnostics");
3162
Daniel Dunbar49138fc2009-04-19 21:09:34 +00003163 // -fdiagnostics-fixit-info is default, only pass non-default.
Mike Stump1eb44332009-09-09 15:08:12 +00003164 if (!Args.hasFlag(options::OPT_fdiagnostics_fixit_info,
Daniel Dunbar49138fc2009-04-19 21:09:34 +00003165 options::OPT_fno_diagnostics_fixit_info))
3166 CmdArgs.push_back("-fno-diagnostics-fixit-info");
Eric Christopher88b7cf02011-08-19 00:30:14 +00003167
Daniel Dunbar9e820ee2009-04-16 06:32:38 +00003168 // Enable -fdiagnostics-show-option by default.
Mike Stump1eb44332009-09-09 15:08:12 +00003169 if (Args.hasFlag(options::OPT_fdiagnostics_show_option,
Daniel Dunbar9e820ee2009-04-16 06:32:38 +00003170 options::OPT_fno_diagnostics_show_option))
3171 CmdArgs.push_back("-fdiagnostics-show-option");
Daniel Dunbar838be482009-11-04 06:24:57 +00003172
Chris Lattner6fbe8392010-05-04 21:55:25 +00003173 if (const Arg *A =
3174 Args.getLastArg(options::OPT_fdiagnostics_show_category_EQ)) {
3175 CmdArgs.push_back("-fdiagnostics-show-category");
Richard Smith1d489cf2012-11-01 04:30:05 +00003176 CmdArgs.push_back(A->getValue());
Chris Lattner6fbe8392010-05-04 21:55:25 +00003177 }
Daniel Dunbarca0e0542010-08-24 16:47:49 +00003178
Douglas Gregorc9471b02011-05-21 17:07:29 +00003179 if (const Arg *A =
3180 Args.getLastArg(options::OPT_fdiagnostics_format_EQ)) {
3181 CmdArgs.push_back("-fdiagnostics-format");
Richard Smith1d489cf2012-11-01 04:30:05 +00003182 CmdArgs.push_back(A->getValue());
Douglas Gregorc9471b02011-05-21 17:07:29 +00003183 }
3184
Chandler Carruthabaca7a2011-03-27 01:50:55 +00003185 if (Arg *A = Args.getLastArg(
3186 options::OPT_fdiagnostics_show_note_include_stack,
3187 options::OPT_fno_diagnostics_show_note_include_stack)) {
3188 if (A->getOption().matches(
3189 options::OPT_fdiagnostics_show_note_include_stack))
3190 CmdArgs.push_back("-fdiagnostics-show-note-include-stack");
3191 else
3192 CmdArgs.push_back("-fno-diagnostics-show-note-include-stack");
3193 }
3194
Daniel Dunbar838be482009-11-04 06:24:57 +00003195 // Color diagnostics are the default, unless the terminal doesn't support
3196 // them.
3197 if (Args.hasFlag(options::OPT_fcolor_diagnostics,
Argyrios Kyrtzidisf765d762010-09-23 12:56:06 +00003198 options::OPT_fno_color_diagnostics,
3199 llvm::sys::Process::StandardErrHasColors()))
Daniel Dunbar838be482009-11-04 06:24:57 +00003200 CmdArgs.push_back("-fcolor-diagnostics");
3201
Daniel Dunbar75eb1d62009-06-08 21:13:54 +00003202 if (!Args.hasFlag(options::OPT_fshow_source_location,
3203 options::OPT_fno_show_source_location))
3204 CmdArgs.push_back("-fno-show-source-location");
Daniel Dunbar9e820ee2009-04-16 06:32:38 +00003205
Douglas Gregorc9471b02011-05-21 17:07:29 +00003206 if (!Args.hasFlag(options::OPT_fshow_column,
3207 options::OPT_fno_show_column,
3208 true))
3209 CmdArgs.push_back("-fno-show-column");
3210
Douglas Gregora0068fc2010-07-09 17:35:33 +00003211 if (!Args.hasFlag(options::OPT_fspell_checking,
3212 options::OPT_fno_spell_checking))
3213 CmdArgs.push_back("-fno-spell-checking");
Daniel Dunbarca0e0542010-08-24 16:47:49 +00003214
Daniel Dunbar25b26eb2010-10-18 22:49:46 +00003215
Chad Rosier15490fd2012-12-05 21:08:21 +00003216 // -fno-asm-blocks is default.
3217 if (Args.hasFlag(options::OPT_fasm_blocks, options::OPT_fno_asm_blocks,
3218 false))
3219 CmdArgs.push_back("-fasm-blocks");
Daniel Dunbar25b26eb2010-10-18 22:49:46 +00003220
Nadav Rotem0f6ef282012-12-18 23:10:16 +00003221 // -fvectorize is default.
Chad Rosierc04d0932012-12-11 17:12:28 +00003222 if (Args.hasFlag(options::OPT_fvectorize,
Nadav Rotem0f6ef282012-12-18 23:10:16 +00003223 options::OPT_fno_vectorize, true)) {
Chad Rosierc04d0932012-12-11 17:12:28 +00003224 CmdArgs.push_back("-backend-option");
3225 CmdArgs.push_back("-vectorize-loops");
3226 }
3227
Hal Finkel443c9992012-12-11 19:59:32 +00003228 // -fno-slp-vectorize is default.
3229 if (Args.hasFlag(options::OPT_fslp_vectorize,
3230 options::OPT_fno_slp_vectorize, false)) {
3231 CmdArgs.push_back("-backend-option");
3232 CmdArgs.push_back("-vectorize");
3233 }
3234
Jeffrey Yasskin5edbdcc2010-06-11 05:57:47 +00003235 if (Arg *A = Args.getLastArg(options::OPT_fshow_overloads_EQ))
3236 A->render(Args, CmdArgs);
3237
Daniel Dunbar7695fba2009-04-19 21:20:32 +00003238 // -fdollars-in-identifiers default varies depending on platform and
3239 // language; only pass if specified.
Mike Stump1eb44332009-09-09 15:08:12 +00003240 if (Arg *A = Args.getLastArg(options::OPT_fdollars_in_identifiers,
Daniel Dunbar7695fba2009-04-19 21:20:32 +00003241 options::OPT_fno_dollars_in_identifiers)) {
3242 if (A->getOption().matches(options::OPT_fdollars_in_identifiers))
Daniel Dunbar8663b182009-12-16 20:10:18 +00003243 CmdArgs.push_back("-fdollars-in-identifiers");
Daniel Dunbar7695fba2009-04-19 21:20:32 +00003244 else
Daniel Dunbar8663b182009-12-16 20:10:18 +00003245 CmdArgs.push_back("-fno-dollars-in-identifiers");
Daniel Dunbar7695fba2009-04-19 21:20:32 +00003246 }
3247
Daniel Dunbare027a4b2009-05-22 19:02:20 +00003248 // -funit-at-a-time is default, and we don't support -fno-unit-at-a-time for
3249 // practical purposes.
Mike Stump1eb44332009-09-09 15:08:12 +00003250 if (Arg *A = Args.getLastArg(options::OPT_funit_at_a_time,
Daniel Dunbare027a4b2009-05-22 19:02:20 +00003251 options::OPT_fno_unit_at_a_time)) {
3252 if (A->getOption().matches(options::OPT_fno_unit_at_a_time))
Chris Lattner5f9e2722011-07-23 10:55:15 +00003253 D.Diag(diag::warn_drv_clang_unsupported) << A->getAsString(Args);
Daniel Dunbare027a4b2009-05-22 19:02:20 +00003254 }
Eli Friedmanceb5c5b2009-07-14 21:58:17 +00003255
Eli Friedman19bda3a2011-11-02 01:53:16 +00003256 if (Args.hasFlag(options::OPT_fapple_pragma_pack,
3257 options::OPT_fno_apple_pragma_pack, false))
3258 CmdArgs.push_back("-fapple-pragma-pack");
3259
Daniel Dunbar2ba91572009-09-10 03:37:02 +00003260 // Default to -fno-builtin-str{cat,cpy} on Darwin for ARM.
Daniel Dunbarf84a4a42009-09-10 04:57:27 +00003261 //
Daniel Dunbar8ff5b282009-12-11 23:00:49 +00003262 // FIXME: This is disabled until clang -cc1 supports -fno-builtin-foo. PR4941.
Daniel Dunbarf84a4a42009-09-10 04:57:27 +00003263#if 0
Bob Wilson905c45f2011-10-14 05:03:44 +00003264 if (getToolChain().getTriple().isOSDarwin() &&
Daniel Dunbar2ba91572009-09-10 03:37:02 +00003265 (getToolChain().getTriple().getArch() == llvm::Triple::arm ||
3266 getToolChain().getTriple().getArch() == llvm::Triple::thumb)) {
3267 if (!Args.hasArg(options::OPT_fbuiltin_strcat))
3268 CmdArgs.push_back("-fno-builtin-strcat");
3269 if (!Args.hasArg(options::OPT_fbuiltin_strcpy))
3270 CmdArgs.push_back("-fno-builtin-strcpy");
3271 }
Daniel Dunbarf84a4a42009-09-10 04:57:27 +00003272#endif
Daniel Dunbar2ba91572009-09-10 03:37:02 +00003273
Daniel Dunbard98750f2011-03-18 21:23:40 +00003274 // Only allow -traditional or -traditional-cpp outside in preprocessing modes.
Mike Stump1eb44332009-09-09 15:08:12 +00003275 if (Arg *A = Args.getLastArg(options::OPT_traditional,
Daniel Dunbard98750f2011-03-18 21:23:40 +00003276 options::OPT_traditional_cpp)) {
3277 if (isa<PreprocessJobAction>(JA))
3278 CmdArgs.push_back("-traditional-cpp");
Eric Christopher88b7cf02011-08-19 00:30:14 +00003279 else
Chris Lattner5f9e2722011-07-23 10:55:15 +00003280 D.Diag(diag::err_drv_clang_unsupported) << A->getAsString(Args);
Daniel Dunbard98750f2011-03-18 21:23:40 +00003281 }
Eli Friedmanceb5c5b2009-07-14 21:58:17 +00003282
Daniel Dunbar1d460332009-03-18 10:01:51 +00003283 Args.AddLastArg(CmdArgs, options::OPT_dM);
Chris Lattnerd82df3a2009-04-12 01:56:53 +00003284 Args.AddLastArg(CmdArgs, options::OPT_dD);
Ted Kremenek36f6e302011-11-11 00:07:43 +00003285
3286 // Handle serialized diagnostics.
3287 if (Arg *A = Args.getLastArg(options::OPT__serialize_diags)) {
3288 CmdArgs.push_back("-serialize-diagnostic-file");
Richard Smith1d489cf2012-11-01 04:30:05 +00003289 CmdArgs.push_back(Args.MakeArgString(A->getValue()));
Ted Kremenek36f6e302011-11-11 00:07:43 +00003290 }
Daniel Dunbar1d460332009-03-18 10:01:51 +00003291
Ted Kremenek127ff2e2012-09-13 06:41:18 +00003292 if (Args.hasArg(options::OPT_fretain_comments_from_system_headers))
3293 CmdArgs.push_back("-fretain-comments-from-system-headers");
3294
Dmitri Gribenko6ebf0912013-02-22 14:21:27 +00003295 // Forward -fcomment-block-commands to -cc1.
3296 Args.AddAllArgs(CmdArgs, options::OPT_fcomment_block_commands);
Dmitri Gribenko6fd7d302013-04-10 15:35:17 +00003297 // Forward -fparse-all-comments to -cc1.
3298 Args.AddAllArgs(CmdArgs, options::OPT_fparse_all_comments);
Dmitri Gribenko6ebf0912013-02-22 14:21:27 +00003299
Daniel Dunbar3f87fb02010-04-15 06:09:03 +00003300 // Forward -Xclang arguments to -cc1, and -mllvm arguments to the LLVM option
3301 // parser.
Daniel Dunbar1d460332009-03-18 10:01:51 +00003302 Args.AddAllArgValues(CmdArgs, options::OPT_Xclang);
Daniel Dunbar3f87fb02010-04-15 06:09:03 +00003303 for (arg_iterator it = Args.filtered_begin(options::OPT_mllvm),
3304 ie = Args.filtered_end(); it != ie; ++it) {
Daniel Dunbar7e4953e2010-06-11 22:00:13 +00003305 (*it)->claim();
Daniel Dunbarfb36d212010-04-17 06:10:00 +00003306
Daniel Dunbar3f87fb02010-04-15 06:09:03 +00003307 // We translate this by hand to the -cc1 argument, since nightly test uses
3308 // it and developers have been trained to spell it with -mllvm.
Richard Smith1d489cf2012-11-01 04:30:05 +00003309 if (StringRef((*it)->getValue(0)) == "-disable-llvm-optzns")
Daniel Dunbar3f87fb02010-04-15 06:09:03 +00003310 CmdArgs.push_back("-disable-llvm-optzns");
3311 else
Daniel Dunbar7e4953e2010-06-11 22:00:13 +00003312 (*it)->render(Args, CmdArgs);
Daniel Dunbar3f87fb02010-04-15 06:09:03 +00003313 }
Daniel Dunbar1d460332009-03-18 10:01:51 +00003314
Daniel Dunbarcd8e4c42009-03-30 06:36:42 +00003315 if (Output.getType() == types::TY_Dependencies) {
3316 // Handled with other dependency code.
Daniel Dunbar115a7922009-03-19 07:29:38 +00003317 } else if (Output.isFilename()) {
Daniel Dunbarb488c1d2009-03-18 08:07:30 +00003318 CmdArgs.push_back("-o");
Daniel Dunbar115a7922009-03-19 07:29:38 +00003319 CmdArgs.push_back(Output.getFilename());
3320 } else {
3321 assert(Output.isNothing() && "Invalid output.");
Daniel Dunbarb488c1d2009-03-18 08:07:30 +00003322 }
3323
Daniel Dunbar1d460332009-03-18 10:01:51 +00003324 for (InputInfoList::const_iterator
3325 it = Inputs.begin(), ie = Inputs.end(); it != ie; ++it) {
3326 const InputInfo &II = *it;
3327 CmdArgs.push_back("-x");
Fariborz Jahaniana5ee0892012-09-28 19:05:17 +00003328 if (Args.hasArg(options::OPT_rewrite_objc))
3329 CmdArgs.push_back(types::getTypeName(types::TY_PP_ObjCXX));
3330 else
3331 CmdArgs.push_back(types::getTypeName(II.getType()));
Daniel Dunbar7c1e4652010-08-02 02:38:21 +00003332 if (II.isFilename())
Daniel Dunbar115a7922009-03-19 07:29:38 +00003333 CmdArgs.push_back(II.getFilename());
Daniel Dunbar1d460332009-03-18 10:01:51 +00003334 else
Daniel Dunbar115a7922009-03-19 07:29:38 +00003335 II.getInputArg().renderAsInput(Args, CmdArgs);
Daniel Dunbar1d460332009-03-18 10:01:51 +00003336 }
Daniel Dunbarc21c4852009-04-08 23:54:23 +00003337
Chris Lattnere6113de2009-11-03 19:50:27 +00003338 Args.AddAllArgs(CmdArgs, options::OPT_undef);
3339
Daniel Dunbara001c1c2010-07-18 21:16:15 +00003340 const char *Exec = getToolChain().getDriver().getClangProgramPath();
Daniel Dunbarf2d8b9f2009-12-18 02:43:17 +00003341
3342 // Optionally embed the -cc1 level arguments into the debug info, for build
3343 // analysis.
3344 if (getToolChain().UseDwarfDebugFlags()) {
Daniel Dunbar6e900472010-06-04 18:47:06 +00003345 ArgStringList OriginalArgs;
3346 for (ArgList::const_iterator it = Args.begin(),
3347 ie = Args.end(); it != ie; ++it)
3348 (*it)->render(Args, OriginalArgs);
Daniel Dunbarca0e0542010-08-24 16:47:49 +00003349
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00003350 SmallString<256> Flags;
Daniel Dunbarf2d8b9f2009-12-18 02:43:17 +00003351 Flags += Exec;
Daniel Dunbar6e900472010-06-04 18:47:06 +00003352 for (unsigned i = 0, e = OriginalArgs.size(); i != e; ++i) {
Daniel Dunbarf2d8b9f2009-12-18 02:43:17 +00003353 Flags += " ";
Daniel Dunbar6e900472010-06-04 18:47:06 +00003354 Flags += OriginalArgs[i];
Daniel Dunbarf2d8b9f2009-12-18 02:43:17 +00003355 }
3356 CmdArgs.push_back("-dwarf-debug-flags");
3357 CmdArgs.push_back(Args.MakeArgString(Flags.str()));
3358 }
3359
Eric Christopher80190392013-02-22 20:12:52 +00003360 // Add the split debug info name to the command lines here so we
3361 // can propagate it to the backend.
3362 bool SplitDwarf = Args.hasArg(options::OPT_gsplit_dwarf) &&
3363 (getToolChain().getTriple().getOS() == llvm::Triple::Linux) &&
Eric Christopherff971d72013-02-22 23:50:16 +00003364 (isa<AssembleJobAction>(JA) || isa<CompileJobAction>(JA));
Eric Christopher80190392013-02-22 20:12:52 +00003365 const char *SplitDwarfOut;
3366 if (SplitDwarf) {
3367 CmdArgs.push_back("-split-dwarf-file");
3368 SplitDwarfOut = SplitDebugName(Args, Inputs);
3369 CmdArgs.push_back(SplitDwarfOut);
3370 }
3371
3372 // Finally add the compile command to the compilation.
Daniel Dunbar2fe238e2010-08-02 02:38:28 +00003373 C.addCommand(new Command(JA, *this, Exec, CmdArgs));
Daniel Dunbara880db02009-03-23 19:03:36 +00003374
Eric Christopherff971d72013-02-22 23:50:16 +00003375 // Handle the debug info splitting at object creation time if we're
3376 // creating an object.
Eric Christopher59320e72013-02-21 22:35:01 +00003377 // TODO: Currently only works on linux with newer objcopy.
Eric Christopherff971d72013-02-22 23:50:16 +00003378 if (SplitDwarf && !isa<CompileJobAction>(JA))
Eric Christopher80190392013-02-22 20:12:52 +00003379 SplitDebugInfo(getToolChain(), C, *this, JA, Args, Output, SplitDwarfOut);
Eric Christopher59320e72013-02-21 22:35:01 +00003380
Roman Divackybe4c8702011-02-10 16:52:03 +00003381 if (Arg *A = Args.getLastArg(options::OPT_pg))
3382 if (Args.hasArg(options::OPT_fomit_frame_pointer))
Chris Lattner5f9e2722011-07-23 10:55:15 +00003383 D.Diag(diag::err_drv_argument_not_allowed_with)
Roman Divackybe4c8702011-02-10 16:52:03 +00003384 << "-fomit-frame-pointer" << A->getAsString(Args);
Michael J. Spencer20249a12010-10-21 03:16:25 +00003385
Daniel Dunbar68fb4692009-04-03 20:51:31 +00003386 // Claim some arguments which clang supports automatically.
3387
Daniel Dunbarf4046862010-04-15 06:18:42 +00003388 // -fpch-preprocess is used with gcc to add a special marker in the output to
3389 // include the PCH file. Clang's PTH solution is completely transparent, so we
3390 // do not need to deal with it at all.
Daniel Dunbar68fb4692009-04-03 20:51:31 +00003391 Args.ClaimAllArgs(options::OPT_fpch_preprocess);
Daniel Dunbarc21c4852009-04-08 23:54:23 +00003392
Daniel Dunbara880db02009-03-23 19:03:36 +00003393 // Claim some arguments which clang doesn't support, but we don't
3394 // care to warn the user about.
Daniel Dunbarcdd96862009-11-25 11:53:23 +00003395 Args.ClaimAllArgs(options::OPT_clang_ignored_f_Group);
3396 Args.ClaimAllArgs(options::OPT_clang_ignored_m_Group);
Rafael Espindola035ff0c2011-02-28 23:29:45 +00003397
Rafael Espindola9c094fb2011-03-01 05:25:27 +00003398 // Disable warnings for clang -E -use-gold-plugin -emit-llvm foo.c
Rafael Espindola035ff0c2011-02-28 23:29:45 +00003399 Args.ClaimAllArgs(options::OPT_use_gold_plugin);
Rafael Espindola9c094fb2011-03-01 05:25:27 +00003400 Args.ClaimAllArgs(options::OPT_emit_llvm);
Daniel Dunbar47ac7d22009-03-18 06:00:36 +00003401}
3402
Jim Grosbachfc308292012-02-10 20:37:10 +00003403void ClangAs::AddARMTargetArgs(const ArgList &Args,
3404 ArgStringList &CmdArgs) const {
3405 const Driver &D = getToolChain().getDriver();
3406 llvm::Triple Triple = getToolChain().getTriple();
3407
3408 // Set the CPU based on -march= and -mcpu=.
3409 CmdArgs.push_back("-target-cpu");
Benjamin Kramer92c4fd52012-06-26 22:20:06 +00003410 CmdArgs.push_back(Args.MakeArgString(getARMTargetCPU(Args, Triple)));
Jim Grosbachfc308292012-02-10 20:37:10 +00003411
3412 // Honor -mfpu=.
Chad Rosier99317272012-04-04 20:51:35 +00003413 if (const Arg *A = Args.getLastArg(options::OPT_mfpu_EQ))
Chad Rosierf80f2a52012-04-04 20:56:36 +00003414 addFPUArgs(D, A, Args, CmdArgs);
Chad Rosier7a938fa2012-04-04 20:39:32 +00003415
3416 // Honor -mfpmath=.
3417 if (const Arg *A = Args.getLastArg(options::OPT_mfpmath_EQ))
Chad Rosier30fe6ba2012-04-04 22:13:40 +00003418 addFPMathArgs(D, A, Args, CmdArgs, getARMTargetCPU(Args, Triple));
Jim Grosbachfc308292012-02-10 20:37:10 +00003419}
3420
Benjamin Kramerefc5b672013-02-22 20:55:17 +00003421void ClangAs::AddX86TargetArgs(const ArgList &Args,
3422 ArgStringList &CmdArgs) const {
3423 // Set the CPU based on -march=.
3424 if (const char *CPUName = getX86TargetCPU(Args, getToolChain().getTriple())) {
3425 CmdArgs.push_back("-target-cpu");
3426 CmdArgs.push_back(CPUName);
3427 }
3428}
3429
John McCall260611a2012-06-20 06:18:46 +00003430/// Add options related to the Objective-C runtime/ABI.
3431///
3432/// Returns true if the runtime is non-fragile.
3433ObjCRuntime Clang::AddObjCRuntimeArgs(const ArgList &args,
3434 ArgStringList &cmdArgs,
3435 RewriteKind rewriteKind) const {
3436 // Look for the controlling runtime option.
3437 Arg *runtimeArg = args.getLastArg(options::OPT_fnext_runtime,
3438 options::OPT_fgnu_runtime,
3439 options::OPT_fobjc_runtime_EQ);
3440
3441 // Just forward -fobjc-runtime= to the frontend. This supercedes
3442 // options about fragility.
3443 if (runtimeArg &&
3444 runtimeArg->getOption().matches(options::OPT_fobjc_runtime_EQ)) {
3445 ObjCRuntime runtime;
Richard Smith1d489cf2012-11-01 04:30:05 +00003446 StringRef value = runtimeArg->getValue();
John McCall260611a2012-06-20 06:18:46 +00003447 if (runtime.tryParse(value)) {
3448 getToolChain().getDriver().Diag(diag::err_drv_unknown_objc_runtime)
3449 << value;
3450 }
3451
3452 runtimeArg->render(args, cmdArgs);
3453 return runtime;
3454 }
3455
3456 // Otherwise, we'll need the ABI "version". Version numbers are
3457 // slightly confusing for historical reasons:
3458 // 1 - Traditional "fragile" ABI
3459 // 2 - Non-fragile ABI, version 1
3460 // 3 - Non-fragile ABI, version 2
3461 unsigned objcABIVersion = 1;
3462 // If -fobjc-abi-version= is present, use that to set the version.
3463 if (Arg *abiArg = args.getLastArg(options::OPT_fobjc_abi_version_EQ)) {
Richard Smith1d489cf2012-11-01 04:30:05 +00003464 StringRef value = abiArg->getValue();
John McCall260611a2012-06-20 06:18:46 +00003465 if (value == "1")
3466 objcABIVersion = 1;
3467 else if (value == "2")
3468 objcABIVersion = 2;
3469 else if (value == "3")
3470 objcABIVersion = 3;
3471 else
3472 getToolChain().getDriver().Diag(diag::err_drv_clang_unsupported)
3473 << value;
3474 } else {
3475 // Otherwise, determine if we are using the non-fragile ABI.
3476 bool nonFragileABIIsDefault =
3477 (rewriteKind == RK_NonFragile ||
3478 (rewriteKind == RK_None &&
3479 getToolChain().IsObjCNonFragileABIDefault()));
3480 if (args.hasFlag(options::OPT_fobjc_nonfragile_abi,
3481 options::OPT_fno_objc_nonfragile_abi,
3482 nonFragileABIIsDefault)) {
3483 // Determine the non-fragile ABI version to use.
3484#ifdef DISABLE_DEFAULT_NONFRAGILEABI_TWO
3485 unsigned nonFragileABIVersion = 1;
3486#else
3487 unsigned nonFragileABIVersion = 2;
3488#endif
3489
3490 if (Arg *abiArg = args.getLastArg(
3491 options::OPT_fobjc_nonfragile_abi_version_EQ)) {
Richard Smith1d489cf2012-11-01 04:30:05 +00003492 StringRef value = abiArg->getValue();
John McCall260611a2012-06-20 06:18:46 +00003493 if (value == "1")
3494 nonFragileABIVersion = 1;
3495 else if (value == "2")
3496 nonFragileABIVersion = 2;
3497 else
3498 getToolChain().getDriver().Diag(diag::err_drv_clang_unsupported)
3499 << value;
3500 }
3501
3502 objcABIVersion = 1 + nonFragileABIVersion;
3503 } else {
3504 objcABIVersion = 1;
3505 }
3506 }
3507
3508 // We don't actually care about the ABI version other than whether
3509 // it's non-fragile.
3510 bool isNonFragile = objcABIVersion != 1;
3511
3512 // If we have no runtime argument, ask the toolchain for its default runtime.
3513 // However, the rewriter only really supports the Mac runtime, so assume that.
3514 ObjCRuntime runtime;
3515 if (!runtimeArg) {
3516 switch (rewriteKind) {
3517 case RK_None:
3518 runtime = getToolChain().getDefaultObjCRuntime(isNonFragile);
3519 break;
3520 case RK_Fragile:
3521 runtime = ObjCRuntime(ObjCRuntime::FragileMacOSX, VersionTuple());
3522 break;
3523 case RK_NonFragile:
3524 runtime = ObjCRuntime(ObjCRuntime::MacOSX, VersionTuple());
3525 break;
3526 }
3527
3528 // -fnext-runtime
3529 } else if (runtimeArg->getOption().matches(options::OPT_fnext_runtime)) {
3530 // On Darwin, make this use the default behavior for the toolchain.
3531 if (getToolChain().getTriple().isOSDarwin()) {
3532 runtime = getToolChain().getDefaultObjCRuntime(isNonFragile);
3533
3534 // Otherwise, build for a generic macosx port.
3535 } else {
3536 runtime = ObjCRuntime(ObjCRuntime::MacOSX, VersionTuple());
3537 }
3538
3539 // -fgnu-runtime
3540 } else {
3541 assert(runtimeArg->getOption().matches(options::OPT_fgnu_runtime));
David Chisnalla422cd02012-07-04 10:37:03 +00003542 // Legacy behaviour is to target the gnustep runtime if we are i
3543 // non-fragile mode or the GCC runtime in fragile mode.
3544 if (isNonFragile)
David Chisnall891dac72012-10-16 15:11:55 +00003545 runtime = ObjCRuntime(ObjCRuntime::GNUstep, VersionTuple(1,6));
David Chisnalla422cd02012-07-04 10:37:03 +00003546 else
3547 runtime = ObjCRuntime(ObjCRuntime::GCC, VersionTuple());
John McCall260611a2012-06-20 06:18:46 +00003548 }
3549
3550 cmdArgs.push_back(args.MakeArgString(
3551 "-fobjc-runtime=" + runtime.getAsString()));
3552 return runtime;
3553}
3554
Daniel Dunbar20a9aa52010-05-20 21:30:13 +00003555void ClangAs::ConstructJob(Compilation &C, const JobAction &JA,
Daniel Dunbar20a9aa52010-05-20 21:30:13 +00003556 const InputInfo &Output,
3557 const InputInfoList &Inputs,
3558 const ArgList &Args,
3559 const char *LinkingOutput) const {
Daniel Dunbar20a9aa52010-05-20 21:30:13 +00003560 ArgStringList CmdArgs;
3561
3562 assert(Inputs.size() == 1 && "Unexpected number of inputs.");
3563 const InputInfo &Input = Inputs[0];
3564
Rafael Espindoladbe80d92010-11-17 22:13:25 +00003565 // Don't warn about "clang -w -c foo.s"
3566 Args.ClaimAllArgs(options::OPT_w);
Rafael Espindola9c094fb2011-03-01 05:25:27 +00003567 // and "clang -emit-llvm -c foo.s"
3568 Args.ClaimAllArgs(options::OPT_emit_llvm);
3569 // and "clang -use-gold-plugin -c foo.s"
3570 Args.ClaimAllArgs(options::OPT_use_gold_plugin);
Rafael Espindoladbe80d92010-11-17 22:13:25 +00003571
Daniel Dunbar20a9aa52010-05-20 21:30:13 +00003572 // Invoke ourselves in -cc1as mode.
3573 //
3574 // FIXME: Implement custom jobs for internal actions.
3575 CmdArgs.push_back("-cc1as");
3576
3577 // Add the "effective" target triple.
3578 CmdArgs.push_back("-triple");
Chad Rosier61ab80a2011-09-20 20:44:06 +00003579 std::string TripleStr =
3580 getToolChain().ComputeEffectiveClangTriple(Args, Input.getType());
Daniel Dunbar20a9aa52010-05-20 21:30:13 +00003581 CmdArgs.push_back(Args.MakeArgString(TripleStr));
3582
3583 // Set the output mode, we currently only expect to be used as a real
3584 // assembler.
3585 CmdArgs.push_back("-filetype");
3586 CmdArgs.push_back("obj");
3587
Eric Christopher27e2b982012-12-18 00:31:10 +00003588 // Set the main file name, so that debug info works even with
3589 // -save-temps or preprocessed assembly.
3590 CmdArgs.push_back("-main-file-name");
3591 CmdArgs.push_back(Clang::getBaseInputName(Args, Inputs));
3592
Joerg Sonnenberger359cf922011-05-06 14:35:16 +00003593 if (UseRelaxAll(C, Args))
Daniel Dunbar469d40e2010-05-28 16:43:21 +00003594 CmdArgs.push_back("-relax-all");
Daniel Dunbar99298002010-05-27 06:18:05 +00003595
Jim Grosbachfc308292012-02-10 20:37:10 +00003596 // Add target specific cpu and features flags.
3597 switch(getToolChain().getTriple().getArch()) {
3598 default:
3599 break;
3600
3601 case llvm::Triple::arm:
3602 case llvm::Triple::thumb:
3603 AddARMTargetArgs(Args, CmdArgs);
3604 break;
Benjamin Kramerefc5b672013-02-22 20:55:17 +00003605
3606 case llvm::Triple::x86:
3607 case llvm::Triple::x86_64:
3608 AddX86TargetArgs(Args, CmdArgs);
3609 break;
Jim Grosbachfc308292012-02-10 20:37:10 +00003610 }
3611
Daniel Dunbar7f6f8c82011-03-17 17:37:29 +00003612 // Ignore explicit -force_cpusubtype_ALL option.
3613 (void) Args.hasArg(options::OPT_force__cpusubtype__ALL);
Daniel Dunbar20a9aa52010-05-20 21:30:13 +00003614
Eric Christopher8f0a4032012-01-10 00:38:01 +00003615 // Determine the original source input.
3616 const Action *SourceAction = &JA;
3617 while (SourceAction->getKind() != Action::InputClass) {
3618 assert(!SourceAction->getInputs().empty() && "unexpected root action!");
3619 SourceAction = SourceAction->getInputs()[0];
3620 }
3621
Chandler Carruthd566df62012-12-17 21:40:04 +00003622 // Forward -g and handle debug info related flags, assuming we are dealing
3623 // with an actual assembly file.
Eric Christopher8f0a4032012-01-10 00:38:01 +00003624 if (SourceAction->getType() == types::TY_Asm ||
3625 SourceAction->getType() == types::TY_PP_Asm) {
3626 Args.ClaimAllArgs(options::OPT_g_Group);
3627 if (Arg *A = Args.getLastArg(options::OPT_g_Group))
3628 if (!A->getOption().matches(options::OPT_g0))
3629 CmdArgs.push_back("-g");
Chandler Carruthd566df62012-12-17 21:40:04 +00003630
3631 // Add the -fdebug-compilation-dir flag if needed.
3632 addDebugCompDirArg(Args, CmdArgs);
Kevin Enderby02341792013-01-17 21:38:06 +00003633
3634 // Set the AT_producer to the clang version when using the integrated
3635 // assembler on assembly source files.
3636 CmdArgs.push_back("-dwarf-debug-producer");
3637 CmdArgs.push_back(Args.MakeArgString(getClangFullVersion()));
Eric Christopher8f0a4032012-01-10 00:38:01 +00003638 }
Kevin Enderby567003e2011-12-22 19:31:58 +00003639
3640 // Optionally embed the -cc1as level arguments into the debug info, for build
3641 // analysis.
3642 if (getToolChain().UseDwarfDebugFlags()) {
3643 ArgStringList OriginalArgs;
3644 for (ArgList::const_iterator it = Args.begin(),
3645 ie = Args.end(); it != ie; ++it)
3646 (*it)->render(Args, OriginalArgs);
3647
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00003648 SmallString<256> Flags;
Kevin Enderby567003e2011-12-22 19:31:58 +00003649 const char *Exec = getToolChain().getDriver().getClangProgramPath();
3650 Flags += Exec;
3651 for (unsigned i = 0, e = OriginalArgs.size(); i != e; ++i) {
3652 Flags += " ";
3653 Flags += OriginalArgs[i];
3654 }
3655 CmdArgs.push_back("-dwarf-debug-flags");
3656 CmdArgs.push_back(Args.MakeArgString(Flags.str()));
3657 }
Daniel Dunbar20a9aa52010-05-20 21:30:13 +00003658
3659 // FIXME: Add -static support, once we have it.
3660
3661 Args.AddAllArgValues(CmdArgs, options::OPT_Wa_COMMA,
3662 options::OPT_Xassembler);
Daniel Dunbar3df23252011-04-29 17:53:18 +00003663 Args.AddAllArgs(CmdArgs, options::OPT_mllvm);
Daniel Dunbar20a9aa52010-05-20 21:30:13 +00003664
3665 assert(Output.isFilename() && "Unexpected lipo output.");
3666 CmdArgs.push_back("-o");
3667 CmdArgs.push_back(Output.getFilename());
3668
Daniel Dunbar7c1e4652010-08-02 02:38:21 +00003669 assert(Input.isFilename() && "Invalid input.");
3670 CmdArgs.push_back(Input.getFilename());
Daniel Dunbar20a9aa52010-05-20 21:30:13 +00003671
Daniel Dunbara001c1c2010-07-18 21:16:15 +00003672 const char *Exec = getToolChain().getDriver().getClangProgramPath();
Daniel Dunbar2fe238e2010-08-02 02:38:28 +00003673 C.addCommand(new Command(JA, *this, Exec, CmdArgs));
Daniel Dunbar20a9aa52010-05-20 21:30:13 +00003674}
3675
Daniel Dunbarb488c1d2009-03-18 08:07:30 +00003676void gcc::Common::ConstructJob(Compilation &C, const JobAction &JA,
Daniel Dunbarb488c1d2009-03-18 08:07:30 +00003677 const InputInfo &Output,
3678 const InputInfoList &Inputs,
Daniel Dunbar1d460332009-03-18 10:01:51 +00003679 const ArgList &Args,
Daniel Dunbarb488c1d2009-03-18 08:07:30 +00003680 const char *LinkingOutput) const {
Daniel Dunbaree788e72009-12-21 18:54:17 +00003681 const Driver &D = getToolChain().getDriver();
Daniel Dunbarb488c1d2009-03-18 08:07:30 +00003682 ArgStringList CmdArgs;
Daniel Dunbar47ac7d22009-03-18 06:00:36 +00003683
Daniel Dunbarc21c4852009-04-08 23:54:23 +00003684 for (ArgList::const_iterator
Daniel Dunbar1d460332009-03-18 10:01:51 +00003685 it = Args.begin(), ie = Args.end(); it != ie; ++it) {
Daniel Dunbarb488c1d2009-03-18 08:07:30 +00003686 Arg *A = *it;
Michael J. Spencer91e06da2012-10-19 22:37:06 +00003687 if (forwardToGCC(A->getOption())) {
Daniel Dunbar2dffe2d2010-08-03 16:14:14 +00003688 // Don't forward any -g arguments to assembly steps.
3689 if (isa<AssembleJobAction>(JA) &&
3690 A->getOption().matches(options::OPT_g_Group))
3691 continue;
3692
Daniel Dunbar75877192009-03-19 07:55:12 +00003693 // It is unfortunate that we have to claim here, as this means
3694 // we will basically never report anything interesting for
Daniel Dunbar6ecc7a92009-05-02 21:41:52 +00003695 // platforms using a generic gcc, even if we are just using gcc
3696 // to get to the assembler.
Daniel Dunbar75877192009-03-19 07:55:12 +00003697 A->claim();
Daniel Dunbar1d460332009-03-18 10:01:51 +00003698 A->render(Args, CmdArgs);
Daniel Dunbar75877192009-03-19 07:55:12 +00003699 }
Daniel Dunbarb488c1d2009-03-18 08:07:30 +00003700 }
Daniel Dunbarc21c4852009-04-08 23:54:23 +00003701
Daniel Dunbar82b51cc2010-01-25 22:35:08 +00003702 RenderExtraToolArgs(JA, CmdArgs);
Daniel Dunbarb488c1d2009-03-18 08:07:30 +00003703
3704 // If using a driver driver, force the arch.
Rafael Espindola64f7ad92012-10-07 04:44:33 +00003705 llvm::Triple::ArchType Arch = getToolChain().getArch();
Bob Wilson905c45f2011-10-14 05:03:44 +00003706 if (getToolChain().getTriple().isOSDarwin()) {
Daniel Dunbarb488c1d2009-03-18 08:07:30 +00003707 CmdArgs.push_back("-arch");
Daniel Dunbarbf54a062009-04-01 20:33:11 +00003708
3709 // FIXME: Remove these special cases.
Rafael Espindola64f7ad92012-10-07 04:44:33 +00003710 if (Arch == llvm::Triple::ppc)
Daniel Dunbar7cfe31a2009-05-22 02:21:04 +00003711 CmdArgs.push_back("ppc");
Rafael Espindola64f7ad92012-10-07 04:44:33 +00003712 else if (Arch == llvm::Triple::ppc64)
Daniel Dunbar7cfe31a2009-05-22 02:21:04 +00003713 CmdArgs.push_back("ppc64");
3714 else
Rafael Espindola64f7ad92012-10-07 04:44:33 +00003715 CmdArgs.push_back(Args.MakeArgString(getToolChain().getArchName()));
Daniel Dunbarb488c1d2009-03-18 08:07:30 +00003716 }
3717
Daniel Dunbar6ecc7a92009-05-02 21:41:52 +00003718 // Try to force gcc to match the tool chain we want, if we recognize
3719 // the arch.
Daniel Dunbar7cfe31a2009-05-22 02:21:04 +00003720 //
3721 // FIXME: The triple class should directly provide the information we want
3722 // here.
Rafael Espindola64f7ad92012-10-07 04:44:33 +00003723 if (Arch == llvm::Triple::x86 || Arch == llvm::Triple::ppc)
Daniel Dunbar6ecc7a92009-05-02 21:41:52 +00003724 CmdArgs.push_back("-m32");
Hans Wennborg33092292013-03-20 07:34:27 +00003725 else if (Arch == llvm::Triple::x86_64 || Arch == llvm::Triple::ppc64)
Daniel Dunbar6ecc7a92009-05-02 21:41:52 +00003726 CmdArgs.push_back("-m64");
3727
Daniel Dunbar7c1e4652010-08-02 02:38:21 +00003728 if (Output.isFilename()) {
Daniel Dunbarb488c1d2009-03-18 08:07:30 +00003729 CmdArgs.push_back("-o");
Daniel Dunbar115a7922009-03-19 07:29:38 +00003730 CmdArgs.push_back(Output.getFilename());
3731 } else {
3732 assert(Output.isNothing() && "Unexpected output");
Daniel Dunbarb488c1d2009-03-18 08:07:30 +00003733 CmdArgs.push_back("-fsyntax-only");
Daniel Dunbar115a7922009-03-19 07:29:38 +00003734 }
Daniel Dunbarb488c1d2009-03-18 08:07:30 +00003735
Tony Linthicum96319392011-12-12 21:14:55 +00003736 Args.AddAllArgValues(CmdArgs, options::OPT_Wa_COMMA,
3737 options::OPT_Xassembler);
Daniel Dunbarb488c1d2009-03-18 08:07:30 +00003738
3739 // Only pass -x if gcc will understand it; otherwise hope gcc
3740 // understands the suffix correctly. The main use case this would go
3741 // wrong in is for linker inputs if they happened to have an odd
3742 // suffix; really the only way to get this to happen is a command
3743 // like '-x foobar a.c' which will treat a.c like a linker input.
3744 //
3745 // FIXME: For the linker case specifically, can we safely convert
3746 // inputs into '-Wl,' options?
3747 for (InputInfoList::const_iterator
3748 it = Inputs.begin(), ie = Inputs.end(); it != ie; ++it) {
3749 const InputInfo &II = *it;
Daniel Dunbara8304f62009-05-02 20:14:53 +00003750
Daniel Dunbar5915fbf2009-09-01 16:57:46 +00003751 // Don't try to pass LLVM or AST inputs to a generic gcc.
Daniel Dunbar6c6424b2010-06-07 23:28:45 +00003752 if (II.getType() == types::TY_LLVM_IR || II.getType() == types::TY_LTO_IR ||
3753 II.getType() == types::TY_LLVM_BC || II.getType() == types::TY_LTO_BC)
Chris Lattner5f9e2722011-07-23 10:55:15 +00003754 D.Diag(diag::err_drv_no_linker_llvm_support)
Daniel Dunbar88137642009-09-09 22:32:48 +00003755 << getToolChain().getTripleString();
Daniel Dunbar5915fbf2009-09-01 16:57:46 +00003756 else if (II.getType() == types::TY_AST)
Chris Lattner5f9e2722011-07-23 10:55:15 +00003757 D.Diag(diag::err_drv_no_ast_support)
Daniel Dunbar88137642009-09-09 22:32:48 +00003758 << getToolChain().getTripleString();
Douglas Gregorc544ba02013-03-27 16:47:18 +00003759 else if (II.getType() == types::TY_ModuleFile)
3760 D.Diag(diag::err_drv_no_module_support)
3761 << getToolChain().getTripleString();
Daniel Dunbara8304f62009-05-02 20:14:53 +00003762
Daniel Dunbarb488c1d2009-03-18 08:07:30 +00003763 if (types::canTypeBeUserSpecified(II.getType())) {
3764 CmdArgs.push_back("-x");
3765 CmdArgs.push_back(types::getTypeName(II.getType()));
3766 }
3767
Daniel Dunbar7c1e4652010-08-02 02:38:21 +00003768 if (II.isFilename())
Daniel Dunbar115a7922009-03-19 07:29:38 +00003769 CmdArgs.push_back(II.getFilename());
Daniel Dunbar48f99942010-09-25 18:10:05 +00003770 else {
3771 const Arg &A = II.getInputArg();
3772
3773 // Reverse translate some rewritten options.
3774 if (A.getOption().matches(options::OPT_Z_reserved_lib_stdcxx)) {
3775 CmdArgs.push_back("-lstdc++");
3776 continue;
3777 }
3778
Daniel Dunbar115a7922009-03-19 07:29:38 +00003779 // Don't render as input, we need gcc to do the translations.
Daniel Dunbar48f99942010-09-25 18:10:05 +00003780 A.render(Args, CmdArgs);
3781 }
Daniel Dunbarb488c1d2009-03-18 08:07:30 +00003782 }
3783
Dylan Noblesmithb8a3e812011-04-09 13:31:59 +00003784 const std::string customGCCName = D.getCCCGenericGCCName();
3785 const char *GCCName;
3786 if (!customGCCName.empty())
3787 GCCName = customGCCName.c_str();
3788 else if (D.CCCIsCXX) {
Dylan Noblesmithb8a3e812011-04-09 13:31:59 +00003789 GCCName = "g++";
Dylan Noblesmithb8a3e812011-04-09 13:31:59 +00003790 } else
3791 GCCName = "gcc";
3792
Daniel Dunbarc21c4852009-04-08 23:54:23 +00003793 const char *Exec =
Daniel Dunbar4a7e8892010-07-14 18:46:23 +00003794 Args.MakeArgString(getToolChain().GetProgramPath(GCCName));
Daniel Dunbar2fe238e2010-08-02 02:38:28 +00003795 C.addCommand(new Command(JA, *this, Exec, CmdArgs));
Daniel Dunbar47ac7d22009-03-18 06:00:36 +00003796}
3797
Daniel Dunbar82b51cc2010-01-25 22:35:08 +00003798void gcc::Preprocess::RenderExtraToolArgs(const JobAction &JA,
3799 ArgStringList &CmdArgs) const {
Daniel Dunbarb488c1d2009-03-18 08:07:30 +00003800 CmdArgs.push_back("-E");
Daniel Dunbar47ac7d22009-03-18 06:00:36 +00003801}
3802
Daniel Dunbar82b51cc2010-01-25 22:35:08 +00003803void gcc::Precompile::RenderExtraToolArgs(const JobAction &JA,
3804 ArgStringList &CmdArgs) const {
Daniel Dunbarb488c1d2009-03-18 08:07:30 +00003805 // The type is good enough.
Daniel Dunbar47ac7d22009-03-18 06:00:36 +00003806}
3807
Daniel Dunbar82b51cc2010-01-25 22:35:08 +00003808void gcc::Compile::RenderExtraToolArgs(const JobAction &JA,
3809 ArgStringList &CmdArgs) const {
Daniel Dunbar64952502010-02-11 03:16:21 +00003810 const Driver &D = getToolChain().getDriver();
3811
Daniel Dunbar82b51cc2010-01-25 22:35:08 +00003812 // If -flto, etc. are present then make sure not to force assembly output.
Daniel Dunbar6c6424b2010-06-07 23:28:45 +00003813 if (JA.getType() == types::TY_LLVM_IR || JA.getType() == types::TY_LTO_IR ||
3814 JA.getType() == types::TY_LLVM_BC || JA.getType() == types::TY_LTO_BC)
Daniel Dunbar82b51cc2010-01-25 22:35:08 +00003815 CmdArgs.push_back("-c");
Daniel Dunbar64952502010-02-11 03:16:21 +00003816 else {
3817 if (JA.getType() != types::TY_PP_Asm)
Chris Lattner5f9e2722011-07-23 10:55:15 +00003818 D.Diag(diag::err_drv_invalid_gcc_output_type)
Daniel Dunbar64952502010-02-11 03:16:21 +00003819 << getTypeName(JA.getType());
Michael J. Spencer20249a12010-10-21 03:16:25 +00003820
Daniel Dunbar82b51cc2010-01-25 22:35:08 +00003821 CmdArgs.push_back("-S");
Daniel Dunbar64952502010-02-11 03:16:21 +00003822 }
Daniel Dunbar47ac7d22009-03-18 06:00:36 +00003823}
3824
Daniel Dunbar82b51cc2010-01-25 22:35:08 +00003825void gcc::Assemble::RenderExtraToolArgs(const JobAction &JA,
3826 ArgStringList &CmdArgs) const {
Daniel Dunbarb488c1d2009-03-18 08:07:30 +00003827 CmdArgs.push_back("-c");
Daniel Dunbar47ac7d22009-03-18 06:00:36 +00003828}
Daniel Dunbarb488c1d2009-03-18 08:07:30 +00003829
Daniel Dunbar82b51cc2010-01-25 22:35:08 +00003830void gcc::Link::RenderExtraToolArgs(const JobAction &JA,
3831 ArgStringList &CmdArgs) const {
Daniel Dunbarb488c1d2009-03-18 08:07:30 +00003832 // The types are (hopefully) good enough.
3833}
3834
Tony Linthicum96319392011-12-12 21:14:55 +00003835// Hexagon tools start.
3836void hexagon::Assemble::RenderExtraToolArgs(const JobAction &JA,
3837 ArgStringList &CmdArgs) const {
3838
3839}
3840void hexagon::Assemble::ConstructJob(Compilation &C, const JobAction &JA,
3841 const InputInfo &Output,
3842 const InputInfoList &Inputs,
3843 const ArgList &Args,
3844 const char *LinkingOutput) const {
3845
3846 const Driver &D = getToolChain().getDriver();
3847 ArgStringList CmdArgs;
3848
3849 std::string MarchString = "-march=";
Matthew Curtis67814152012-12-06 14:16:43 +00003850 MarchString += toolchains::Hexagon_TC::GetTargetCPU(Args);
Tony Linthicum96319392011-12-12 21:14:55 +00003851 CmdArgs.push_back(Args.MakeArgString(MarchString));
3852
3853 RenderExtraToolArgs(JA, CmdArgs);
3854
3855 if (Output.isFilename()) {
3856 CmdArgs.push_back("-o");
3857 CmdArgs.push_back(Output.getFilename());
3858 } else {
3859 assert(Output.isNothing() && "Unexpected output");
3860 CmdArgs.push_back("-fsyntax-only");
3861 }
3862
Matthew Curtis33c95f12012-12-06 17:49:03 +00003863 std::string SmallDataThreshold = GetHexagonSmallDataThresholdValue(Args);
3864 if (!SmallDataThreshold.empty())
3865 CmdArgs.push_back(
3866 Args.MakeArgString(std::string("-G") + SmallDataThreshold));
Tony Linthicum96319392011-12-12 21:14:55 +00003867
Matthew Curtis3d8d4222012-12-07 17:23:04 +00003868 Args.AddAllArgs(CmdArgs, options::OPT_g_Group);
3869 Args.AddAllArgValues(CmdArgs, options::OPT_Wa_COMMA,
3870 options::OPT_Xassembler);
3871
Tony Linthicum96319392011-12-12 21:14:55 +00003872 // Only pass -x if gcc will understand it; otherwise hope gcc
3873 // understands the suffix correctly. The main use case this would go
3874 // wrong in is for linker inputs if they happened to have an odd
3875 // suffix; really the only way to get this to happen is a command
3876 // like '-x foobar a.c' which will treat a.c like a linker input.
3877 //
3878 // FIXME: For the linker case specifically, can we safely convert
3879 // inputs into '-Wl,' options?
3880 for (InputInfoList::const_iterator
3881 it = Inputs.begin(), ie = Inputs.end(); it != ie; ++it) {
3882 const InputInfo &II = *it;
3883
3884 // Don't try to pass LLVM or AST inputs to a generic gcc.
3885 if (II.getType() == types::TY_LLVM_IR || II.getType() == types::TY_LTO_IR ||
3886 II.getType() == types::TY_LLVM_BC || II.getType() == types::TY_LTO_BC)
3887 D.Diag(clang::diag::err_drv_no_linker_llvm_support)
3888 << getToolChain().getTripleString();
3889 else if (II.getType() == types::TY_AST)
3890 D.Diag(clang::diag::err_drv_no_ast_support)
3891 << getToolChain().getTripleString();
Douglas Gregorc544ba02013-03-27 16:47:18 +00003892 else if (II.getType() == types::TY_ModuleFile)
3893 D.Diag(diag::err_drv_no_module_support)
3894 << getToolChain().getTripleString();
Tony Linthicum96319392011-12-12 21:14:55 +00003895
3896 if (II.isFilename())
3897 CmdArgs.push_back(II.getFilename());
3898 else
3899 // Don't render as input, we need gcc to do the translations. FIXME: Pranav: What is this ?
3900 II.getInputArg().render(Args, CmdArgs);
3901 }
3902
3903 const char *GCCName = "hexagon-as";
3904 const char *Exec =
3905 Args.MakeArgString(getToolChain().GetProgramPath(GCCName));
3906 C.addCommand(new Command(JA, *this, Exec, CmdArgs));
3907
3908}
3909void hexagon::Link::RenderExtraToolArgs(const JobAction &JA,
3910 ArgStringList &CmdArgs) const {
3911 // The types are (hopefully) good enough.
3912}
3913
3914void hexagon::Link::ConstructJob(Compilation &C, const JobAction &JA,
3915 const InputInfo &Output,
3916 const InputInfoList &Inputs,
3917 const ArgList &Args,
3918 const char *LinkingOutput) const {
3919
Matthew Curtis5fdf3502012-12-06 15:46:07 +00003920 const toolchains::Hexagon_TC& ToolChain =
3921 static_cast<const toolchains::Hexagon_TC&>(getToolChain());
3922 const Driver &D = ToolChain.getDriver();
3923
Tony Linthicum96319392011-12-12 21:14:55 +00003924 ArgStringList CmdArgs;
3925
Matthew Curtis5fdf3502012-12-06 15:46:07 +00003926 //----------------------------------------------------------------------------
3927 //
3928 //----------------------------------------------------------------------------
3929 bool hasStaticArg = Args.hasArg(options::OPT_static);
3930 bool buildingLib = Args.hasArg(options::OPT_shared);
Matthew Curtis33c95f12012-12-06 17:49:03 +00003931 bool buildPIE = Args.hasArg(options::OPT_pie);
Matthew Curtis5fdf3502012-12-06 15:46:07 +00003932 bool incStdLib = !Args.hasArg(options::OPT_nostdlib);
3933 bool incStartFiles = !Args.hasArg(options::OPT_nostartfiles);
3934 bool incDefLibs = !Args.hasArg(options::OPT_nodefaultlibs);
3935 bool useShared = buildingLib && !hasStaticArg;
Tony Linthicum96319392011-12-12 21:14:55 +00003936
Matthew Curtis5fdf3502012-12-06 15:46:07 +00003937 //----------------------------------------------------------------------------
3938 // Silence warnings for various options
3939 //----------------------------------------------------------------------------
Tony Linthicum96319392011-12-12 21:14:55 +00003940
Matthew Curtis5fdf3502012-12-06 15:46:07 +00003941 Args.ClaimAllArgs(options::OPT_g_Group);
3942 Args.ClaimAllArgs(options::OPT_emit_llvm);
3943 Args.ClaimAllArgs(options::OPT_w); // Other warning options are already
3944 // handled somewhere else.
3945 Args.ClaimAllArgs(options::OPT_static_libgcc);
3946
3947 //----------------------------------------------------------------------------
3948 //
3949 //----------------------------------------------------------------------------
3950 for (std::vector<std::string>::const_iterator i = ToolChain.ExtraOpts.begin(),
3951 e = ToolChain.ExtraOpts.end();
3952 i != e; ++i)
3953 CmdArgs.push_back(i->c_str());
Tony Linthicum96319392011-12-12 21:14:55 +00003954
Matthew Curtis67814152012-12-06 14:16:43 +00003955 std::string MarchString = toolchains::Hexagon_TC::GetTargetCPU(Args);
3956 CmdArgs.push_back(Args.MakeArgString("-m" + MarchString));
Sebastian Pop43115d42012-01-13 20:37:10 +00003957
Matthew Curtis5fdf3502012-12-06 15:46:07 +00003958 if (buildingLib) {
3959 CmdArgs.push_back("-shared");
3960 CmdArgs.push_back("-call_shared"); // should be the default, but doing as
3961 // hexagon-gcc does
Tony Linthicum96319392011-12-12 21:14:55 +00003962 }
3963
Matthew Curtis5fdf3502012-12-06 15:46:07 +00003964 if (hasStaticArg)
3965 CmdArgs.push_back("-static");
Tony Linthicum96319392011-12-12 21:14:55 +00003966
Matthew Curtis33c95f12012-12-06 17:49:03 +00003967 if (buildPIE && !buildingLib)
3968 CmdArgs.push_back("-pie");
3969
3970 std::string SmallDataThreshold = GetHexagonSmallDataThresholdValue(Args);
3971 if (!SmallDataThreshold.empty()) {
3972 CmdArgs.push_back(
3973 Args.MakeArgString(std::string("-G") + SmallDataThreshold));
3974 }
3975
Matthew Curtis5fdf3502012-12-06 15:46:07 +00003976 //----------------------------------------------------------------------------
3977 //
3978 //----------------------------------------------------------------------------
3979 CmdArgs.push_back("-o");
3980 CmdArgs.push_back(Output.getFilename());
Tony Linthicum96319392011-12-12 21:14:55 +00003981
Matthew Curtis5fdf3502012-12-06 15:46:07 +00003982 const std::string MarchSuffix = "/" + MarchString;
3983 const std::string G0Suffix = "/G0";
3984 const std::string MarchG0Suffix = MarchSuffix + G0Suffix;
3985 const std::string RootDir = toolchains::Hexagon_TC::GetGnuDir(D.InstalledDir)
3986 + "/";
3987 const std::string StartFilesDir = RootDir
3988 + "hexagon/lib"
3989 + (buildingLib
3990 ? MarchG0Suffix : MarchSuffix);
3991
3992 //----------------------------------------------------------------------------
3993 // moslib
3994 //----------------------------------------------------------------------------
3995 std::vector<std::string> oslibs;
3996 bool hasStandalone= false;
3997
3998 for (arg_iterator it = Args.filtered_begin(options::OPT_moslib_EQ),
3999 ie = Args.filtered_end(); it != ie; ++it) {
4000 (*it)->claim();
4001 oslibs.push_back((*it)->getValue());
4002 hasStandalone = hasStandalone || (oslibs.back() == "standalone");
Tony Linthicum96319392011-12-12 21:14:55 +00004003 }
Matthew Curtis5fdf3502012-12-06 15:46:07 +00004004 if (oslibs.empty()) {
4005 oslibs.push_back("standalone");
4006 hasStandalone = true;
4007 }
Tony Linthicum96319392011-12-12 21:14:55 +00004008
Matthew Curtis5fdf3502012-12-06 15:46:07 +00004009 //----------------------------------------------------------------------------
4010 // Start Files
4011 //----------------------------------------------------------------------------
4012 if (incStdLib && incStartFiles) {
4013
4014 if (!buildingLib) {
4015 if (hasStandalone) {
4016 CmdArgs.push_back(
4017 Args.MakeArgString(StartFilesDir + "/crt0_standalone.o"));
4018 }
4019 CmdArgs.push_back(Args.MakeArgString(StartFilesDir + "/crt0.o"));
4020 }
4021 std::string initObj = useShared ? "/initS.o" : "/init.o";
4022 CmdArgs.push_back(Args.MakeArgString(StartFilesDir + initObj));
4023 }
4024
4025 //----------------------------------------------------------------------------
4026 // Library Search Paths
4027 //----------------------------------------------------------------------------
4028 const ToolChain::path_list &LibPaths = ToolChain.getFilePaths();
4029 for (ToolChain::path_list::const_iterator
4030 i = LibPaths.begin(),
4031 e = LibPaths.end();
4032 i != e;
4033 ++i)
4034 CmdArgs.push_back(Args.MakeArgString(StringRef("-L") + *i));
4035
4036 //----------------------------------------------------------------------------
4037 //
4038 //----------------------------------------------------------------------------
4039 Args.AddAllArgs(CmdArgs, options::OPT_T_Group);
4040 Args.AddAllArgs(CmdArgs, options::OPT_e);
4041 Args.AddAllArgs(CmdArgs, options::OPT_s);
4042 Args.AddAllArgs(CmdArgs, options::OPT_t);
4043 Args.AddAllArgs(CmdArgs, options::OPT_u_Group);
4044
4045 AddLinkerInputs(ToolChain, Inputs, Args, CmdArgs);
4046
4047 //----------------------------------------------------------------------------
4048 // Libraries
4049 //----------------------------------------------------------------------------
4050 if (incStdLib && incDefLibs) {
4051 if (D.CCCIsCXX) {
4052 ToolChain.AddCXXStdlibLibArgs(Args, CmdArgs);
4053 CmdArgs.push_back("-lm");
4054 }
4055
4056 CmdArgs.push_back("--start-group");
4057
4058 if (!buildingLib) {
4059 for(std::vector<std::string>::iterator i = oslibs.begin(),
4060 e = oslibs.end(); i != e; ++i)
4061 CmdArgs.push_back(Args.MakeArgString("-l" + *i));
4062 CmdArgs.push_back("-lc");
4063 }
4064 CmdArgs.push_back("-lgcc");
4065
4066 CmdArgs.push_back("--end-group");
4067 }
4068
4069 //----------------------------------------------------------------------------
4070 // End files
4071 //----------------------------------------------------------------------------
4072 if (incStdLib && incStartFiles) {
4073 std::string finiObj = useShared ? "/finiS.o" : "/fini.o";
4074 CmdArgs.push_back(Args.MakeArgString(StartFilesDir + finiObj));
4075 }
4076
4077 std::string Linker = ToolChain.GetProgramPath("hexagon-ld");
4078 C.addCommand(
4079 new Command(
4080 JA, *this,
4081 Args.MakeArgString(Linker), CmdArgs));
Tony Linthicum96319392011-12-12 21:14:55 +00004082}
4083// Hexagon tools end.
4084
Rafael Espindolacfed8282012-10-31 18:51:07 +00004085llvm::Triple::ArchType darwin::getArchTypeForDarwinArchName(StringRef Str) {
4086 // See arch(3) and llvm-gcc's driver-driver.c. We don't implement support for
4087 // archs which Darwin doesn't use.
4088
4089 // The matching this routine does is fairly pointless, since it is neither the
4090 // complete architecture list, nor a reasonable subset. The problem is that
4091 // historically the driver driver accepts this and also ties its -march=
4092 // handling to the architecture name, so we need to be careful before removing
4093 // support for it.
4094
4095 // This code must be kept in sync with Clang's Darwin specific argument
4096 // translation.
4097
4098 return llvm::StringSwitch<llvm::Triple::ArchType>(Str)
4099 .Cases("ppc", "ppc601", "ppc603", "ppc604", "ppc604e", llvm::Triple::ppc)
4100 .Cases("ppc750", "ppc7400", "ppc7450", "ppc970", llvm::Triple::ppc)
4101 .Case("ppc64", llvm::Triple::ppc64)
4102 .Cases("i386", "i486", "i486SX", "i586", "i686", llvm::Triple::x86)
4103 .Cases("pentium", "pentpro", "pentIIm3", "pentIIm5", "pentium4",
4104 llvm::Triple::x86)
4105 .Case("x86_64", llvm::Triple::x86_64)
4106 // This is derived from the driver driver.
Bob Wilson2503ebd2013-03-04 22:37:49 +00004107 .Cases("arm", "armv4t", "armv5", "armv6", "armv6m", llvm::Triple::arm)
4108 .Cases("armv7", "armv7em", "armv7f", "armv7k", "armv7m", llvm::Triple::arm)
4109 .Cases("armv7s", "xscale", llvm::Triple::arm)
Rafael Espindolacfed8282012-10-31 18:51:07 +00004110 .Case("r600", llvm::Triple::r600)
4111 .Case("nvptx", llvm::Triple::nvptx)
4112 .Case("nvptx64", llvm::Triple::nvptx64)
4113 .Case("amdil", llvm::Triple::amdil)
4114 .Case("spir", llvm::Triple::spir)
4115 .Default(llvm::Triple::UnknownArch);
4116}
Tony Linthicum96319392011-12-12 21:14:55 +00004117
Bob Wilson66b8a662012-11-23 06:14:39 +00004118const char *Clang::getBaseInputName(const ArgList &Args,
4119 const InputInfoList &Inputs) {
Michael J. Spencer472ccff2010-12-18 00:19:12 +00004120 return Args.MakeArgString(
4121 llvm::sys::path::filename(Inputs[0].getBaseInput()));
Daniel Dunbara3ec60e2009-03-29 18:40:18 +00004122}
4123
Bob Wilson66b8a662012-11-23 06:14:39 +00004124const char *Clang::getBaseInputStem(const ArgList &Args,
4125 const InputInfoList &Inputs) {
Daniel Dunbara3ec60e2009-03-29 18:40:18 +00004126 const char *Str = getBaseInputName(Args, Inputs);
4127
Chris Lattner657ca662011-01-16 08:14:11 +00004128 if (const char *End = strrchr(Str, '.'))
Daniel Dunbar88137642009-09-09 22:32:48 +00004129 return Args.MakeArgString(std::string(Str, End));
Daniel Dunbara3ec60e2009-03-29 18:40:18 +00004130
4131 return Str;
4132}
4133
Bob Wilson66b8a662012-11-23 06:14:39 +00004134const char *Clang::getDependencyFileName(const ArgList &Args,
4135 const InputInfoList &Inputs) {
Daniel Dunbara3ec60e2009-03-29 18:40:18 +00004136 // FIXME: Think about this more.
4137 std::string Res;
4138
4139 if (Arg *OutputOpt = Args.getLastArg(options::OPT_o)) {
Richard Smith1d489cf2012-11-01 04:30:05 +00004140 std::string Str(OutputOpt->getValue());
Daniel Dunbara3ec60e2009-03-29 18:40:18 +00004141 Res = Str.substr(0, Str.rfind('.'));
Chad Rosier30601782011-08-17 23:08:45 +00004142 } else {
Bob Wilson66b8a662012-11-23 06:14:39 +00004143 Res = getBaseInputStem(Args, Inputs);
Chad Rosier30601782011-08-17 23:08:45 +00004144 }
Daniel Dunbar88137642009-09-09 22:32:48 +00004145 return Args.MakeArgString(Res + ".d");
Daniel Dunbara3ec60e2009-03-29 18:40:18 +00004146}
4147
Daniel Dunbar8cac5f72009-03-20 16:06:39 +00004148void darwin::Assemble::ConstructJob(Compilation &C, const JobAction &JA,
Daniel Dunbar2fe238e2010-08-02 02:38:28 +00004149 const InputInfo &Output,
Daniel Dunbarc21c4852009-04-08 23:54:23 +00004150 const InputInfoList &Inputs,
4151 const ArgList &Args,
Daniel Dunbar8cac5f72009-03-20 16:06:39 +00004152 const char *LinkingOutput) const {
4153 ArgStringList CmdArgs;
4154
4155 assert(Inputs.size() == 1 && "Unexpected number of inputs.");
4156 const InputInfo &Input = Inputs[0];
4157
Daniel Dunbar34bac1f2011-04-12 23:59:20 +00004158 // Determine the original source input.
4159 const Action *SourceAction = &JA;
4160 while (SourceAction->getKind() != Action::InputClass) {
4161 assert(!SourceAction->getInputs().empty() && "unexpected root action!");
4162 SourceAction = SourceAction->getInputs()[0];
4163 }
4164
4165 // Forward -g, assuming we are dealing with an actual assembly file.
Eric Christopher88b7cf02011-08-19 00:30:14 +00004166 if (SourceAction->getType() == types::TY_Asm ||
Daniel Dunbar34bac1f2011-04-12 23:59:20 +00004167 SourceAction->getType() == types::TY_PP_Asm) {
Daniel Dunbar8e4fea62009-04-01 00:27:44 +00004168 if (Args.hasArg(options::OPT_gstabs))
4169 CmdArgs.push_back("--gstabs");
4170 else if (Args.hasArg(options::OPT_g_Group))
Bob Wilson591ff152011-11-02 05:10:45 +00004171 CmdArgs.push_back("-g");
Daniel Dunbar8e4fea62009-04-01 00:27:44 +00004172 }
Daniel Dunbarc21c4852009-04-08 23:54:23 +00004173
Daniel Dunbar8cac5f72009-03-20 16:06:39 +00004174 // Derived from asm spec.
Daniel Dunbarcc6f8032009-09-09 18:36:27 +00004175 AddDarwinArch(Args, CmdArgs);
Daniel Dunbar8cac5f72009-03-20 16:06:39 +00004176
Daniel Dunbarf5438e32010-07-22 01:47:22 +00004177 // Use -force_cpusubtype_ALL on x86 by default.
4178 if (getToolChain().getTriple().getArch() == llvm::Triple::x86 ||
4179 getToolChain().getTriple().getArch() == llvm::Triple::x86_64 ||
Daniel Dunbarcc6f8032009-09-09 18:36:27 +00004180 Args.hasArg(options::OPT_force__cpusubtype__ALL))
4181 CmdArgs.push_back("-force_cpusubtype_ALL");
4182
Daniel Dunbar0e2679d2009-08-24 22:26:16 +00004183 if (getToolChain().getTriple().getArch() != llvm::Triple::x86_64 &&
Daniel Dunbar7a0c0642012-10-15 22:23:53 +00004184 (((Args.hasArg(options::OPT_mkernel) ||
Eric Christopher59320e72013-02-21 22:35:01 +00004185 Args.hasArg(options::OPT_fapple_kext)) &&
Daniel Dunbar7a0c0642012-10-15 22:23:53 +00004186 (!getDarwinToolChain().isTargetIPhoneOS() ||
4187 getDarwinToolChain().isIPhoneOSVersionLT(6, 0))) ||
4188 Args.hasArg(options::OPT_static)))
Daniel Dunbarc21c4852009-04-08 23:54:23 +00004189 CmdArgs.push_back("-static");
4190
Daniel Dunbar8cac5f72009-03-20 16:06:39 +00004191 Args.AddAllArgValues(CmdArgs, options::OPT_Wa_COMMA,
4192 options::OPT_Xassembler);
4193
4194 assert(Output.isFilename() && "Unexpected lipo output.");
4195 CmdArgs.push_back("-o");
4196 CmdArgs.push_back(Output.getFilename());
4197
Daniel Dunbar7c1e4652010-08-02 02:38:21 +00004198 assert(Input.isFilename() && "Invalid input.");
4199 CmdArgs.push_back(Input.getFilename());
Daniel Dunbar8cac5f72009-03-20 16:06:39 +00004200
4201 // asm_final spec is empty.
4202
Daniel Dunbarc21c4852009-04-08 23:54:23 +00004203 const char *Exec =
Daniel Dunbar4a7e8892010-07-14 18:46:23 +00004204 Args.MakeArgString(getToolChain().GetProgramPath("as"));
Daniel Dunbar2fe238e2010-08-02 02:38:28 +00004205 C.addCommand(new Command(JA, *this, Exec, CmdArgs));
Daniel Dunbar8cac5f72009-03-20 16:06:39 +00004206}
Daniel Dunbarff7488d2009-03-20 00:52:38 +00004207
David Blaikie99ba9e32011-12-20 02:48:34 +00004208void darwin::DarwinTool::anchor() {}
4209
Daniel Dunbarfbefe6b2009-09-09 18:36:20 +00004210void darwin::DarwinTool::AddDarwinArch(const ArgList &Args,
4211 ArgStringList &CmdArgs) const {
Chris Lattner5f9e2722011-07-23 10:55:15 +00004212 StringRef ArchName = getDarwinToolChain().getDarwinArchName(Args);
Daniel Dunbareeff4062010-01-22 02:04:58 +00004213
Daniel Dunbar02633b52009-03-26 16:23:12 +00004214 // Derived from darwin_arch spec.
4215 CmdArgs.push_back("-arch");
Daniel Dunbareeff4062010-01-22 02:04:58 +00004216 CmdArgs.push_back(Args.MakeArgString(ArchName));
Daniel Dunbar78dbd582009-09-04 18:35:31 +00004217
Daniel Dunbareeff4062010-01-22 02:04:58 +00004218 // FIXME: Is this needed anymore?
4219 if (ArchName == "arm")
Daniel Dunbar78dbd582009-09-04 18:35:31 +00004220 CmdArgs.push_back("-force_cpusubtype_ALL");
Daniel Dunbar02633b52009-03-26 16:23:12 +00004221}
4222
Bill Wendling6acf8b42012-10-02 18:02:50 +00004223bool darwin::Link::NeedsTempPath(const InputInfoList &Inputs) const {
4224 // We only need to generate a temp path for LTO if we aren't compiling object
4225 // files. When compiling source files, we run 'dsymutil' after linking. We
4226 // don't run 'dsymutil' when compiling object files.
4227 for (InputInfoList::const_iterator
4228 it = Inputs.begin(), ie = Inputs.end(); it != ie; ++it)
4229 if (it->getType() != types::TY_Object)
4230 return true;
4231
4232 return false;
4233}
4234
Daniel Dunbar748de8e2010-09-09 21:51:05 +00004235void darwin::Link::AddLinkArgs(Compilation &C,
4236 const ArgList &Args,
Bill Wendling6acf8b42012-10-02 18:02:50 +00004237 ArgStringList &CmdArgs,
4238 const InputInfoList &Inputs) const {
Daniel Dunbaree788e72009-12-21 18:54:17 +00004239 const Driver &D = getToolChain().getDriver();
Daniel Dunbarce911f52011-04-28 21:23:41 +00004240 const toolchains::Darwin &DarwinTC = getDarwinToolChain();
Daniel Dunbar02633b52009-03-26 16:23:12 +00004241
Daniel Dunbarb18dc5b2010-08-11 23:07:50 +00004242 unsigned Version[3] = { 0, 0, 0 };
4243 if (Arg *A = Args.getLastArg(options::OPT_mlinker_version_EQ)) {
4244 bool HadExtra;
Richard Smith1d489cf2012-11-01 04:30:05 +00004245 if (!Driver::GetReleaseVersion(A->getValue(), Version[0],
Daniel Dunbarb18dc5b2010-08-11 23:07:50 +00004246 Version[1], Version[2], HadExtra) ||
4247 HadExtra)
Chris Lattner5f9e2722011-07-23 10:55:15 +00004248 D.Diag(diag::err_drv_invalid_version_number)
Daniel Dunbarb18dc5b2010-08-11 23:07:50 +00004249 << A->getAsString(Args);
4250 }
4251
4252 // Newer linkers support -demangle, pass it if supported and not disabled by
4253 // the user.
Daniel Dunbard2d20882012-01-04 21:45:27 +00004254 if (Version[0] >= 100 && !Args.hasArg(options::OPT_Z_Xlinker__no_demangle)) {
Daniel Dunbarbcf1da82010-09-07 17:07:49 +00004255 // Don't pass -demangle to ld_classic.
4256 //
4257 // FIXME: This is a temporary workaround, ld should be handling this.
4258 bool UsesLdClassic = (getToolChain().getArch() == llvm::Triple::x86 &&
4259 Args.hasArg(options::OPT_static));
Daniel Dunbar9ced7042010-09-07 17:50:41 +00004260 if (getToolChain().getArch() == llvm::Triple::x86) {
4261 for (arg_iterator it = Args.filtered_begin(options::OPT_Xlinker,
4262 options::OPT_Wl_COMMA),
4263 ie = Args.filtered_end(); it != ie; ++it) {
4264 const Arg *A = *it;
4265 for (unsigned i = 0, e = A->getNumValues(); i != e; ++i)
Richard Smith1d489cf2012-11-01 04:30:05 +00004266 if (StringRef(A->getValue(i)) == "-kext")
Daniel Dunbar9ced7042010-09-07 17:50:41 +00004267 UsesLdClassic = true;
4268 }
4269 }
Daniel Dunbarbcf1da82010-09-07 17:07:49 +00004270 if (!UsesLdClassic)
4271 CmdArgs.push_back("-demangle");
Daniel Dunbarb18dc5b2010-08-11 23:07:50 +00004272 }
4273
Bill Wendlingc35f9082012-11-16 23:03:00 +00004274 // If we are using LTO, then automatically create a temporary file path for
4275 // the linker to use, so that it's lifetime will extend past a possible
4276 // dsymutil step.
4277 if (Version[0] >= 116 && D.IsUsingLTO(Args) && NeedsTempPath(Inputs)) {
4278 const char *TmpPath = C.getArgs().MakeArgString(
4279 D.GetTemporaryPath("cc", types::getTypeTempSuffix(types::TY_Object)));
4280 C.addTempFile(TmpPath);
4281 CmdArgs.push_back("-object_path_lto");
4282 CmdArgs.push_back(TmpPath);
Daniel Dunbar5bfa6562011-06-21 20:55:11 +00004283 }
4284
Daniel Dunbar02633b52009-03-26 16:23:12 +00004285 // Derived from the "link" spec.
4286 Args.AddAllArgs(CmdArgs, options::OPT_static);
4287 if (!Args.hasArg(options::OPT_static))
4288 CmdArgs.push_back("-dynamic");
4289 if (Args.hasArg(options::OPT_fgnu_runtime)) {
4290 // FIXME: gcc replaces -lobjc in forward args with -lobjc-gnu
4291 // here. How do we wish to handle such things?
4292 }
Daniel Dunbarc21c4852009-04-08 23:54:23 +00004293
Daniel Dunbar02633b52009-03-26 16:23:12 +00004294 if (!Args.hasArg(options::OPT_dynamiclib)) {
Daniel Dunbara6d38492010-01-22 02:04:52 +00004295 AddDarwinArch(Args, CmdArgs);
Daniel Dunbara6d38492010-01-22 02:04:52 +00004296 // FIXME: Why do this only on this path?
Daniel Dunbar8917dd42010-01-22 03:37:33 +00004297 Args.AddLastArg(CmdArgs, options::OPT_force__cpusubtype__ALL);
Daniel Dunbar02633b52009-03-26 16:23:12 +00004298
4299 Args.AddLastArg(CmdArgs, options::OPT_bundle);
4300 Args.AddAllArgs(CmdArgs, options::OPT_bundle__loader);
4301 Args.AddAllArgs(CmdArgs, options::OPT_client__name);
4302
4303 Arg *A;
4304 if ((A = Args.getLastArg(options::OPT_compatibility__version)) ||
4305 (A = Args.getLastArg(options::OPT_current__version)) ||
4306 (A = Args.getLastArg(options::OPT_install__name)))
Chris Lattner5f9e2722011-07-23 10:55:15 +00004307 D.Diag(diag::err_drv_argument_only_allowed_with)
Daniel Dunbar02633b52009-03-26 16:23:12 +00004308 << A->getAsString(Args) << "-dynamiclib";
4309
4310 Args.AddLastArg(CmdArgs, options::OPT_force__flat__namespace);
4311 Args.AddLastArg(CmdArgs, options::OPT_keep__private__externs);
4312 Args.AddLastArg(CmdArgs, options::OPT_private__bundle);
4313 } else {
4314 CmdArgs.push_back("-dylib");
4315
4316 Arg *A;
4317 if ((A = Args.getLastArg(options::OPT_bundle)) ||
4318 (A = Args.getLastArg(options::OPT_bundle__loader)) ||
4319 (A = Args.getLastArg(options::OPT_client__name)) ||
4320 (A = Args.getLastArg(options::OPT_force__flat__namespace)) ||
4321 (A = Args.getLastArg(options::OPT_keep__private__externs)) ||
4322 (A = Args.getLastArg(options::OPT_private__bundle)))
Chris Lattner5f9e2722011-07-23 10:55:15 +00004323 D.Diag(diag::err_drv_argument_not_allowed_with)
Daniel Dunbar02633b52009-03-26 16:23:12 +00004324 << A->getAsString(Args) << "-dynamiclib";
Daniel Dunbarc21c4852009-04-08 23:54:23 +00004325
Daniel Dunbar02633b52009-03-26 16:23:12 +00004326 Args.AddAllArgsTranslated(CmdArgs, options::OPT_compatibility__version,
4327 "-dylib_compatibility_version");
4328 Args.AddAllArgsTranslated(CmdArgs, options::OPT_current__version,
4329 "-dylib_current_version");
4330
Daniel Dunbara6d38492010-01-22 02:04:52 +00004331 AddDarwinArch(Args, CmdArgs);
Daniel Dunbar02633b52009-03-26 16:23:12 +00004332
4333 Args.AddAllArgsTranslated(CmdArgs, options::OPT_install__name,
4334 "-dylib_install_name");
4335 }
4336
4337 Args.AddLastArg(CmdArgs, options::OPT_all__load);
4338 Args.AddAllArgs(CmdArgs, options::OPT_allowable__client);
4339 Args.AddLastArg(CmdArgs, options::OPT_bind__at__load);
Daniel Dunbarce911f52011-04-28 21:23:41 +00004340 if (DarwinTC.isTargetIPhoneOS())
Daniel Dunbard82f8fa2009-09-04 18:35:41 +00004341 Args.AddLastArg(CmdArgs, options::OPT_arch__errors__fatal);
Daniel Dunbar02633b52009-03-26 16:23:12 +00004342 Args.AddLastArg(CmdArgs, options::OPT_dead__strip);
4343 Args.AddLastArg(CmdArgs, options::OPT_no__dead__strip__inits__and__terms);
4344 Args.AddAllArgs(CmdArgs, options::OPT_dylib__file);
4345 Args.AddLastArg(CmdArgs, options::OPT_dynamic);
4346 Args.AddAllArgs(CmdArgs, options::OPT_exported__symbols__list);
4347 Args.AddLastArg(CmdArgs, options::OPT_flat__namespace);
Daniel Dunbar99ca47b2011-06-28 20:16:02 +00004348 Args.AddAllArgs(CmdArgs, options::OPT_force__load);
Daniel Dunbar02633b52009-03-26 16:23:12 +00004349 Args.AddAllArgs(CmdArgs, options::OPT_headerpad__max__install__names);
4350 Args.AddAllArgs(CmdArgs, options::OPT_image__base);
4351 Args.AddAllArgs(CmdArgs, options::OPT_init);
4352
Daniel Dunbarce911f52011-04-28 21:23:41 +00004353 // Add the deployment target.
Benjamin Kramer09c9a562012-03-10 20:55:36 +00004354 VersionTuple TargetVersion = DarwinTC.getTargetVersion();
Daniel Dunbarb7f5ef72011-04-30 04:22:58 +00004355
4356 // If we had an explicit -mios-simulator-version-min argument, honor that,
4357 // otherwise use the traditional deployment targets. We can't just check the
4358 // is-sim attribute because existing code follows this path, and the linker
4359 // may not handle the argument.
4360 //
4361 // FIXME: We may be able to remove this, once we can verify no one depends on
4362 // it.
4363 if (Args.hasArg(options::OPT_mios_simulator_version_min_EQ))
4364 CmdArgs.push_back("-ios_simulator_version_min");
4365 else if (DarwinTC.isTargetIPhoneOS())
4366 CmdArgs.push_back("-iphoneos_version_min");
4367 else
4368 CmdArgs.push_back("-macosx_version_min");
Benjamin Kramer09c9a562012-03-10 20:55:36 +00004369 CmdArgs.push_back(Args.MakeArgString(TargetVersion.getAsString()));
Daniel Dunbarce911f52011-04-28 21:23:41 +00004370
Daniel Dunbar02633b52009-03-26 16:23:12 +00004371 Args.AddLastArg(CmdArgs, options::OPT_nomultidefs);
4372 Args.AddLastArg(CmdArgs, options::OPT_multi__module);
4373 Args.AddLastArg(CmdArgs, options::OPT_single__module);
4374 Args.AddAllArgs(CmdArgs, options::OPT_multiply__defined);
4375 Args.AddAllArgs(CmdArgs, options::OPT_multiply__defined__unused);
Daniel Dunbarc21c4852009-04-08 23:54:23 +00004376
Daniel Dunbar47e879d2010-07-13 23:31:40 +00004377 if (const Arg *A = Args.getLastArg(options::OPT_fpie, options::OPT_fPIE,
4378 options::OPT_fno_pie,
4379 options::OPT_fno_PIE)) {
4380 if (A->getOption().matches(options::OPT_fpie) ||
4381 A->getOption().matches(options::OPT_fPIE))
4382 CmdArgs.push_back("-pie");
4383 else
4384 CmdArgs.push_back("-no_pie");
4385 }
Daniel Dunbar02633b52009-03-26 16:23:12 +00004386
4387 Args.AddLastArg(CmdArgs, options::OPT_prebind);
4388 Args.AddLastArg(CmdArgs, options::OPT_noprebind);
4389 Args.AddLastArg(CmdArgs, options::OPT_nofixprebinding);
4390 Args.AddLastArg(CmdArgs, options::OPT_prebind__all__twolevel__modules);
4391 Args.AddLastArg(CmdArgs, options::OPT_read__only__relocs);
4392 Args.AddAllArgs(CmdArgs, options::OPT_sectcreate);
4393 Args.AddAllArgs(CmdArgs, options::OPT_sectorder);
4394 Args.AddAllArgs(CmdArgs, options::OPT_seg1addr);
4395 Args.AddAllArgs(CmdArgs, options::OPT_segprot);
4396 Args.AddAllArgs(CmdArgs, options::OPT_segaddr);
4397 Args.AddAllArgs(CmdArgs, options::OPT_segs__read__only__addr);
4398 Args.AddAllArgs(CmdArgs, options::OPT_segs__read__write__addr);
4399 Args.AddAllArgs(CmdArgs, options::OPT_seg__addr__table);
4400 Args.AddAllArgs(CmdArgs, options::OPT_seg__addr__table__filename);
4401 Args.AddAllArgs(CmdArgs, options::OPT_sub__library);
4402 Args.AddAllArgs(CmdArgs, options::OPT_sub__umbrella);
Daniel Dunbard82f8fa2009-09-04 18:35:41 +00004403
Daniel Dunbarcc957192011-05-02 21:03:47 +00004404 // Give --sysroot= preference, over the Apple specific behavior to also use
4405 // --isysroot as the syslibroot.
Sebastian Pop4762a2d2012-04-16 04:16:43 +00004406 StringRef sysroot = C.getSysRoot();
4407 if (sysroot != "") {
Daniel Dunbarcc957192011-05-02 21:03:47 +00004408 CmdArgs.push_back("-syslibroot");
Sebastian Pop4762a2d2012-04-16 04:16:43 +00004409 CmdArgs.push_back(C.getArgs().MakeArgString(sysroot));
Daniel Dunbarcc957192011-05-02 21:03:47 +00004410 } else if (const Arg *A = Args.getLastArg(options::OPT_isysroot)) {
4411 CmdArgs.push_back("-syslibroot");
Richard Smith1d489cf2012-11-01 04:30:05 +00004412 CmdArgs.push_back(A->getValue());
Daniel Dunbard82f8fa2009-09-04 18:35:41 +00004413 }
4414
Daniel Dunbar02633b52009-03-26 16:23:12 +00004415 Args.AddLastArg(CmdArgs, options::OPT_twolevel__namespace);
4416 Args.AddLastArg(CmdArgs, options::OPT_twolevel__namespace__hints);
4417 Args.AddAllArgs(CmdArgs, options::OPT_umbrella);
4418 Args.AddAllArgs(CmdArgs, options::OPT_undefined);
4419 Args.AddAllArgs(CmdArgs, options::OPT_unexported__symbols__list);
Daniel Dunbard82f8fa2009-09-04 18:35:41 +00004420 Args.AddAllArgs(CmdArgs, options::OPT_weak__reference__mismatches);
Daniel Dunbar02633b52009-03-26 16:23:12 +00004421 Args.AddLastArg(CmdArgs, options::OPT_X_Flag);
4422 Args.AddAllArgs(CmdArgs, options::OPT_y);
4423 Args.AddLastArg(CmdArgs, options::OPT_w);
4424 Args.AddAllArgs(CmdArgs, options::OPT_pagezero__size);
4425 Args.AddAllArgs(CmdArgs, options::OPT_segs__read__);
4426 Args.AddLastArg(CmdArgs, options::OPT_seglinkedit);
4427 Args.AddLastArg(CmdArgs, options::OPT_noseglinkedit);
4428 Args.AddAllArgs(CmdArgs, options::OPT_sectalign);
4429 Args.AddAllArgs(CmdArgs, options::OPT_sectobjectsymbols);
4430 Args.AddAllArgs(CmdArgs, options::OPT_segcreate);
4431 Args.AddLastArg(CmdArgs, options::OPT_whyload);
4432 Args.AddLastArg(CmdArgs, options::OPT_whatsloaded);
4433 Args.AddAllArgs(CmdArgs, options::OPT_dylinker__install__name);
4434 Args.AddLastArg(CmdArgs, options::OPT_dylinker);
4435 Args.AddLastArg(CmdArgs, options::OPT_Mach);
4436}
4437
4438void darwin::Link::ConstructJob(Compilation &C, const JobAction &JA,
Daniel Dunbar2fe238e2010-08-02 02:38:28 +00004439 const InputInfo &Output,
Daniel Dunbarc21c4852009-04-08 23:54:23 +00004440 const InputInfoList &Inputs,
4441 const ArgList &Args,
Daniel Dunbar02633b52009-03-26 16:23:12 +00004442 const char *LinkingOutput) const {
4443 assert(Output.getType() == types::TY_Image && "Invalid linker output type.");
Daniel Dunbare0be8b12009-09-08 16:39:16 +00004444
Daniel Dunbar02633b52009-03-26 16:23:12 +00004445 // The logic here is derived from gcc's behavior; most of which
4446 // comes from specs (starting with link_command). Consult gcc for
4447 // more information.
Daniel Dunbar02633b52009-03-26 16:23:12 +00004448 ArgStringList CmdArgs;
4449
Argyrios Kyrtzidis22897172011-10-07 22:58:08 +00004450 /// Hack(tm) to ignore linking errors when we are doing ARC migration.
4451 if (Args.hasArg(options::OPT_ccc_arcmt_check,
4452 options::OPT_ccc_arcmt_migrate)) {
4453 for (ArgList::const_iterator I = Args.begin(), E = Args.end(); I != E; ++I)
4454 (*I)->claim();
4455 const char *Exec =
4456 Args.MakeArgString(getToolChain().GetProgramPath("touch"));
4457 CmdArgs.push_back(Output.getFilename());
4458 C.addCommand(new Command(JA, *this, Exec, CmdArgs));
4459 return;
4460 }
4461
Daniel Dunbar02633b52009-03-26 16:23:12 +00004462 // I'm not sure why this particular decomposition exists in gcc, but
4463 // we follow suite for ease of comparison.
Bill Wendling6acf8b42012-10-02 18:02:50 +00004464 AddLinkArgs(C, Args, CmdArgs, Inputs);
Daniel Dunbar02633b52009-03-26 16:23:12 +00004465
Daniel Dunbar02633b52009-03-26 16:23:12 +00004466 Args.AddAllArgs(CmdArgs, options::OPT_d_Flag);
4467 Args.AddAllArgs(CmdArgs, options::OPT_s);
4468 Args.AddAllArgs(CmdArgs, options::OPT_t);
4469 Args.AddAllArgs(CmdArgs, options::OPT_Z_Flag);
4470 Args.AddAllArgs(CmdArgs, options::OPT_u_Group);
Daniel Dunbar02633b52009-03-26 16:23:12 +00004471 Args.AddLastArg(CmdArgs, options::OPT_e);
4472 Args.AddAllArgs(CmdArgs, options::OPT_m_Separate);
4473 Args.AddAllArgs(CmdArgs, options::OPT_r);
4474
Daniel Dunbar270073c2010-10-18 22:08:36 +00004475 // Forward -ObjC when either -ObjC or -ObjC++ is used, to force loading
4476 // members of static archive libraries which implement Objective-C classes or
4477 // categories.
4478 if (Args.hasArg(options::OPT_ObjC) || Args.hasArg(options::OPT_ObjCXX))
4479 CmdArgs.push_back("-ObjC");
Michael J. Spencer20249a12010-10-21 03:16:25 +00004480
Bill Wendlingd56f4032012-12-10 21:48:41 +00004481 if (Args.hasArg(options::OPT_rdynamic))
4482 CmdArgs.push_back("-export_dynamic");
4483
Daniel Dunbar02633b52009-03-26 16:23:12 +00004484 CmdArgs.push_back("-o");
4485 CmdArgs.push_back(Output.getFilename());
4486
Chad Rosier18937312012-05-16 23:45:12 +00004487 if (!Args.hasArg(options::OPT_nostdlib) &&
Daniel Dunbar02633b52009-03-26 16:23:12 +00004488 !Args.hasArg(options::OPT_nostartfiles)) {
4489 // Derived from startfile spec.
4490 if (Args.hasArg(options::OPT_dynamiclib)) {
Daniel Dunbarc21c4852009-04-08 23:54:23 +00004491 // Derived from darwin_dylib1 spec.
Daniel Dunbar1051fc02011-04-01 21:02:42 +00004492 if (getDarwinToolChain().isTargetIOSSimulator()) {
4493 // The simulator doesn't have a versioned crt1 file.
4494 CmdArgs.push_back("-ldylib1.o");
4495 } else if (getDarwinToolChain().isTargetIPhoneOS()) {
Daniel Dunbarcacb0f02010-01-27 00:56:56 +00004496 if (getDarwinToolChain().isIPhoneOSVersionLT(3, 1))
4497 CmdArgs.push_back("-ldylib1.o");
4498 } else {
Daniel Dunbarce3fdf22010-01-27 00:57:03 +00004499 if (getDarwinToolChain().isMacosxVersionLT(10, 5))
Daniel Dunbarcacb0f02010-01-27 00:56:56 +00004500 CmdArgs.push_back("-ldylib1.o");
Daniel Dunbarce3fdf22010-01-27 00:57:03 +00004501 else if (getDarwinToolChain().isMacosxVersionLT(10, 6))
Daniel Dunbarcacb0f02010-01-27 00:56:56 +00004502 CmdArgs.push_back("-ldylib1.10.5.o");
4503 }
Daniel Dunbar02633b52009-03-26 16:23:12 +00004504 } else {
4505 if (Args.hasArg(options::OPT_bundle)) {
Daniel Dunbar8a8d8af2009-04-01 03:17:40 +00004506 if (!Args.hasArg(options::OPT_static)) {
4507 // Derived from darwin_bundle1 spec.
Daniel Dunbar1051fc02011-04-01 21:02:42 +00004508 if (getDarwinToolChain().isTargetIOSSimulator()) {
4509 // The simulator doesn't have a versioned crt1 file.
4510 CmdArgs.push_back("-lbundle1.o");
4511 } else if (getDarwinToolChain().isTargetIPhoneOS()) {
Daniel Dunbarcacb0f02010-01-27 00:56:56 +00004512 if (getDarwinToolChain().isIPhoneOSVersionLT(3, 1))
4513 CmdArgs.push_back("-lbundle1.o");
4514 } else {
Daniel Dunbarce3fdf22010-01-27 00:57:03 +00004515 if (getDarwinToolChain().isMacosxVersionLT(10, 6))
Daniel Dunbarcacb0f02010-01-27 00:56:56 +00004516 CmdArgs.push_back("-lbundle1.o");
4517 }
Daniel Dunbar8a8d8af2009-04-01 03:17:40 +00004518 }
Daniel Dunbar02633b52009-03-26 16:23:12 +00004519 } else {
Daniel Dunbarbbe8e3e2011-03-01 18:49:30 +00004520 if (Args.hasArg(options::OPT_pg) &&
4521 getToolChain().SupportsProfiling()) {
Daniel Dunbar02633b52009-03-26 16:23:12 +00004522 if (Args.hasArg(options::OPT_static) ||
4523 Args.hasArg(options::OPT_object) ||
4524 Args.hasArg(options::OPT_preload)) {
4525 CmdArgs.push_back("-lgcrt0.o");
4526 } else {
4527 CmdArgs.push_back("-lgcrt1.o");
Daniel Dunbarc21c4852009-04-08 23:54:23 +00004528
Daniel Dunbar02633b52009-03-26 16:23:12 +00004529 // darwin_crt2 spec is empty.
Daniel Dunbarc21c4852009-04-08 23:54:23 +00004530 }
Bob Wilson4e6e7912012-07-04 00:18:41 +00004531 // By default on OS X 10.8 and later, we don't link with a crt1.o
4532 // file and the linker knows to use _main as the entry point. But,
4533 // when compiling with -pg, we need to link with the gcrt1.o file,
4534 // so pass the -no_new_main option to tell the linker to use the
4535 // "start" symbol as the entry point.
Bob Wilson1fc6e4f2012-07-03 20:42:10 +00004536 if (getDarwinToolChain().isTargetMacOS() &&
4537 !getDarwinToolChain().isMacosxVersionLT(10, 8))
4538 CmdArgs.push_back("-no_new_main");
Daniel Dunbar02633b52009-03-26 16:23:12 +00004539 } else {
4540 if (Args.hasArg(options::OPT_static) ||
4541 Args.hasArg(options::OPT_object) ||
4542 Args.hasArg(options::OPT_preload)) {
4543 CmdArgs.push_back("-lcrt0.o");
4544 } else {
4545 // Derived from darwin_crt1 spec.
Daniel Dunbar40355802011-03-31 17:12:33 +00004546 if (getDarwinToolChain().isTargetIOSSimulator()) {
4547 // The simulator doesn't have a versioned crt1 file.
4548 CmdArgs.push_back("-lcrt1.o");
4549 } else if (getDarwinToolChain().isTargetIPhoneOS()) {
Daniel Dunbarcacb0f02010-01-27 00:56:56 +00004550 if (getDarwinToolChain().isIPhoneOSVersionLT(3, 1))
4551 CmdArgs.push_back("-lcrt1.o");
Daniel Dunbar7a0c0642012-10-15 22:23:53 +00004552 else if (getDarwinToolChain().isIPhoneOSVersionLT(6, 0))
Daniel Dunbarcacb0f02010-01-27 00:56:56 +00004553 CmdArgs.push_back("-lcrt1.3.1.o");
Daniel Dunbarce3fdf22010-01-27 00:57:03 +00004554 } else {
4555 if (getDarwinToolChain().isMacosxVersionLT(10, 5))
4556 CmdArgs.push_back("-lcrt1.o");
4557 else if (getDarwinToolChain().isMacosxVersionLT(10, 6))
4558 CmdArgs.push_back("-lcrt1.10.5.o");
Ted Kremenekebcb57a2012-03-06 20:05:56 +00004559 else if (getDarwinToolChain().isMacosxVersionLT(10, 8))
Daniel Dunbarce3fdf22010-01-27 00:57:03 +00004560 CmdArgs.push_back("-lcrt1.10.6.o");
Daniel Dunbarc21c4852009-04-08 23:54:23 +00004561
Daniel Dunbarce3fdf22010-01-27 00:57:03 +00004562 // darwin_crt2 spec is empty.
4563 }
Daniel Dunbar02633b52009-03-26 16:23:12 +00004564 }
4565 }
4566 }
4567 }
4568
Daniel Dunbarce3fdf22010-01-27 00:57:03 +00004569 if (!getDarwinToolChain().isTargetIPhoneOS() &&
4570 Args.hasArg(options::OPT_shared_libgcc) &&
4571 getDarwinToolChain().isMacosxVersionLT(10, 5)) {
Daniel Dunbar88137642009-09-09 22:32:48 +00004572 const char *Str =
Daniel Dunbar4a7e8892010-07-14 18:46:23 +00004573 Args.MakeArgString(getToolChain().GetFilePath("crt3.o"));
Daniel Dunbar88137642009-09-09 22:32:48 +00004574 CmdArgs.push_back(Str);
Daniel Dunbar02633b52009-03-26 16:23:12 +00004575 }
4576 }
4577
4578 Args.AddAllArgs(CmdArgs, options::OPT_L);
Daniel Dunbarc21c4852009-04-08 23:54:23 +00004579
Peter Collingbourne52ca70d2013-04-09 04:35:11 +00004580 SanitizerArgs Sanitize(getToolChain(), Args);
Alexey Samsonov2cb3d302013-01-21 08:45:02 +00004581 // If we're building a dynamic lib with -fsanitize=address,
4582 // unresolved symbols may appear. Mark all
Alexey Samsonov75fcb192012-11-16 12:53:14 +00004583 // of them as dynamic_lookup. Linking executables is handled in
4584 // lib/Driver/ToolChains.cpp.
Alexey Samsonov2cb3d302013-01-21 08:45:02 +00004585 if (Sanitize.needsAsanRt()) {
Kostya Serebryany7b5f1012011-12-06 19:18:44 +00004586 if (Args.hasArg(options::OPT_dynamiclib) ||
4587 Args.hasArg(options::OPT_bundle)) {
4588 CmdArgs.push_back("-undefined");
4589 CmdArgs.push_back("dynamic_lookup");
4590 }
4591 }
4592
Daniel Dunbar02633b52009-03-26 16:23:12 +00004593 if (Args.hasArg(options::OPT_fopenmp))
4594 // This is more complicated in gcc...
4595 CmdArgs.push_back("-lgomp");
4596
Douglas Gregor04e326b2012-05-15 21:00:27 +00004597 AddLinkerInputs(getToolChain(), Inputs, Args, CmdArgs);
4598
Bob Wilson63d9f3c2012-05-15 18:57:39 +00004599 if (isObjCRuntimeLinked(Args) &&
4600 !Args.hasArg(options::OPT_nostdlib) &&
4601 !Args.hasArg(options::OPT_nodefaultlibs)) {
Ted Kremenekebcb57a2012-03-06 20:05:56 +00004602 // Avoid linking compatibility stubs on i386 mac.
4603 if (!getDarwinToolChain().isTargetMacOS() ||
Rafael Espindola64f7ad92012-10-07 04:44:33 +00004604 getDarwinToolChain().getArch() != llvm::Triple::x86) {
Ted Kremenekebcb57a2012-03-06 20:05:56 +00004605 // If we don't have ARC or subscripting runtime support, link in the
4606 // runtime stubs. We have to do this *before* adding any of the normal
4607 // linker inputs so that its initializer gets run first.
John McCall260611a2012-06-20 06:18:46 +00004608 ObjCRuntime runtime =
4609 getDarwinToolChain().getDefaultObjCRuntime(/*nonfragile*/ true);
Ted Kremenekebcb57a2012-03-06 20:05:56 +00004610 // We use arclite library for both ARC and subscripting support.
John McCall0a7dd782012-08-21 02:47:43 +00004611 if ((!runtime.hasNativeARC() && isObjCAutoRefCount(Args)) ||
John McCall260611a2012-06-20 06:18:46 +00004612 !runtime.hasSubscripting())
Ted Kremenekebcb57a2012-03-06 20:05:56 +00004613 getDarwinToolChain().AddLinkARCArgs(Args, CmdArgs);
Ted Kremenekebcb57a2012-03-06 20:05:56 +00004614 }
Bob Wilson0b1c7152012-04-21 00:21:42 +00004615 CmdArgs.push_back("-framework");
4616 CmdArgs.push_back("Foundation");
Ted Kremenekebcb57a2012-03-06 20:05:56 +00004617 // Link libobj.
4618 CmdArgs.push_back("-lobjc");
John McCall9f084a32011-07-06 00:26:06 +00004619 }
John McCallf85e1932011-06-15 23:02:42 +00004620
Daniel Dunbar02633b52009-03-26 16:23:12 +00004621 if (LinkingOutput) {
4622 CmdArgs.push_back("-arch_multiple");
4623 CmdArgs.push_back("-final_output");
4624 CmdArgs.push_back(LinkingOutput);
4625 }
4626
Daniel Dunbar02633b52009-03-26 16:23:12 +00004627 if (Args.hasArg(options::OPT_fnested_functions))
4628 CmdArgs.push_back("-allow_stack_execute");
Daniel Dunbarc21c4852009-04-08 23:54:23 +00004629
Daniel Dunbar02633b52009-03-26 16:23:12 +00004630 if (!Args.hasArg(options::OPT_nostdlib) &&
4631 !Args.hasArg(options::OPT_nodefaultlibs)) {
Daniel Dunbaree788e72009-12-21 18:54:17 +00004632 if (getToolChain().getDriver().CCCIsCXX)
Daniel Dunbar132e35d2010-09-17 01:20:05 +00004633 getToolChain().AddCXXStdlibLibArgs(Args, CmdArgs);
Daniel Dunbaredfa02b2009-04-08 06:06:21 +00004634
Daniel Dunbar02633b52009-03-26 16:23:12 +00004635 // link_ssp spec is empty.
4636
Daniel Dunbar6cd41542009-09-18 08:15:03 +00004637 // Let the tool chain choose which runtime library to link.
4638 getDarwinToolChain().AddLinkRuntimeLibArgs(Args, CmdArgs);
Daniel Dunbar02633b52009-03-26 16:23:12 +00004639 }
4640
Chad Rosier18937312012-05-16 23:45:12 +00004641 if (!Args.hasArg(options::OPT_nostdlib) &&
Daniel Dunbar02633b52009-03-26 16:23:12 +00004642 !Args.hasArg(options::OPT_nostartfiles)) {
4643 // endfile_spec is empty.
4644 }
4645
4646 Args.AddAllArgs(CmdArgs, options::OPT_T_Group);
4647 Args.AddAllArgs(CmdArgs, options::OPT_F);
4648
Daniel Dunbarc21c4852009-04-08 23:54:23 +00004649 const char *Exec =
Daniel Dunbar4a7e8892010-07-14 18:46:23 +00004650 Args.MakeArgString(getToolChain().GetProgramPath("ld"));
Daniel Dunbar2fe238e2010-08-02 02:38:28 +00004651 C.addCommand(new Command(JA, *this, Exec, CmdArgs));
Daniel Dunbar02633b52009-03-26 16:23:12 +00004652}
4653
Daniel Dunbarff7488d2009-03-20 00:52:38 +00004654void darwin::Lipo::ConstructJob(Compilation &C, const JobAction &JA,
Daniel Dunbar2fe238e2010-08-02 02:38:28 +00004655 const InputInfo &Output,
Daniel Dunbarc21c4852009-04-08 23:54:23 +00004656 const InputInfoList &Inputs,
4657 const ArgList &Args,
Daniel Dunbarff7488d2009-03-20 00:52:38 +00004658 const char *LinkingOutput) const {
4659 ArgStringList CmdArgs;
4660
4661 CmdArgs.push_back("-create");
4662 assert(Output.isFilename() && "Unexpected lipo output.");
Daniel Dunbara428df82009-03-24 00:24:37 +00004663
4664 CmdArgs.push_back("-output");
Daniel Dunbarff7488d2009-03-20 00:52:38 +00004665 CmdArgs.push_back(Output.getFilename());
Daniel Dunbara428df82009-03-24 00:24:37 +00004666
Daniel Dunbarff7488d2009-03-20 00:52:38 +00004667 for (InputInfoList::const_iterator
4668 it = Inputs.begin(), ie = Inputs.end(); it != ie; ++it) {
4669 const InputInfo &II = *it;
4670 assert(II.isFilename() && "Unexpected lipo input.");
4671 CmdArgs.push_back(II.getFilename());
4672 }
Daniel Dunbarc21c4852009-04-08 23:54:23 +00004673 const char *Exec =
Daniel Dunbar4a7e8892010-07-14 18:46:23 +00004674 Args.MakeArgString(getToolChain().GetProgramPath("lipo"));
Daniel Dunbar2fe238e2010-08-02 02:38:28 +00004675 C.addCommand(new Command(JA, *this, Exec, CmdArgs));
Daniel Dunbarff7488d2009-03-20 00:52:38 +00004676}
Daniel Dunbar68a31d42009-03-31 17:45:15 +00004677
Daniel Dunbar6e0f2542010-06-04 18:28:36 +00004678void darwin::Dsymutil::ConstructJob(Compilation &C, const JobAction &JA,
Daniel Dunbar2fe238e2010-08-02 02:38:28 +00004679 const InputInfo &Output,
Daniel Dunbar6e0f2542010-06-04 18:28:36 +00004680 const InputInfoList &Inputs,
4681 const ArgList &Args,
4682 const char *LinkingOutput) const {
4683 ArgStringList CmdArgs;
4684
Daniel Dunbar03e92302011-05-09 17:23:16 +00004685 CmdArgs.push_back("-o");
4686 CmdArgs.push_back(Output.getFilename());
4687
Daniel Dunbar6e0f2542010-06-04 18:28:36 +00004688 assert(Inputs.size() == 1 && "Unable to handle multiple inputs.");
4689 const InputInfo &Input = Inputs[0];
4690 assert(Input.isFilename() && "Unexpected dsymutil input.");
4691 CmdArgs.push_back(Input.getFilename());
4692
Daniel Dunbar6e0f2542010-06-04 18:28:36 +00004693 const char *Exec =
Daniel Dunbar4a7e8892010-07-14 18:46:23 +00004694 Args.MakeArgString(getToolChain().GetProgramPath("dsymutil"));
Daniel Dunbar2fe238e2010-08-02 02:38:28 +00004695 C.addCommand(new Command(JA, *this, Exec, CmdArgs));
Daniel Dunbar6e0f2542010-06-04 18:28:36 +00004696}
4697
Eric Christopherf8571862011-08-23 17:56:55 +00004698void darwin::VerifyDebug::ConstructJob(Compilation &C, const JobAction &JA,
Eric Christopher27e2b982012-12-18 00:31:10 +00004699 const InputInfo &Output,
4700 const InputInfoList &Inputs,
4701 const ArgList &Args,
4702 const char *LinkingOutput) const {
Eric Christopherf8571862011-08-23 17:56:55 +00004703 ArgStringList CmdArgs;
4704 CmdArgs.push_back("--verify");
Eric Christopher1c79dc42012-02-06 19:13:09 +00004705 CmdArgs.push_back("--debug-info");
4706 CmdArgs.push_back("--eh-frame");
Eric Christopherb822f722012-02-06 19:43:51 +00004707 CmdArgs.push_back("--quiet");
Eric Christopherf8571862011-08-23 17:56:55 +00004708
4709 assert(Inputs.size() == 1 && "Unable to handle multiple inputs.");
4710 const InputInfo &Input = Inputs[0];
4711 assert(Input.isFilename() && "Unexpected verify input");
4712
4713 // Grabbing the output of the earlier dsymutil run.
4714 CmdArgs.push_back(Input.getFilename());
4715
4716 const char *Exec =
4717 Args.MakeArgString(getToolChain().GetProgramPath("dwarfdump"));
4718 C.addCommand(new Command(JA, *this, Exec, CmdArgs));
4719}
4720
David Chisnall31c46902012-02-15 13:39:01 +00004721void solaris::Assemble::ConstructJob(Compilation &C, const JobAction &JA,
4722 const InputInfo &Output,
4723 const InputInfoList &Inputs,
4724 const ArgList &Args,
4725 const char *LinkingOutput) const {
4726 ArgStringList CmdArgs;
4727
4728 Args.AddAllArgValues(CmdArgs, options::OPT_Wa_COMMA,
4729 options::OPT_Xassembler);
4730
4731 CmdArgs.push_back("-o");
4732 CmdArgs.push_back(Output.getFilename());
4733
4734 for (InputInfoList::const_iterator
4735 it = Inputs.begin(), ie = Inputs.end(); it != ie; ++it) {
4736 const InputInfo &II = *it;
4737 CmdArgs.push_back(II.getFilename());
4738 }
4739
4740 const char *Exec =
4741 Args.MakeArgString(getToolChain().GetProgramPath("as"));
4742 C.addCommand(new Command(JA, *this, Exec, CmdArgs));
4743}
4744
4745
4746void solaris::Link::ConstructJob(Compilation &C, const JobAction &JA,
4747 const InputInfo &Output,
4748 const InputInfoList &Inputs,
4749 const ArgList &Args,
4750 const char *LinkingOutput) const {
4751 // FIXME: Find a real GCC, don't hard-code versions here
4752 std::string GCCLibPath = "/usr/gcc/4.5/lib/gcc/";
4753 const llvm::Triple &T = getToolChain().getTriple();
4754 std::string LibPath = "/usr/lib/";
4755 llvm::Triple::ArchType Arch = T.getArch();
4756 switch (Arch) {
4757 case llvm::Triple::x86:
4758 GCCLibPath += ("i386-" + T.getVendorName() + "-" +
4759 T.getOSName()).str() + "/4.5.2/";
4760 break;
4761 case llvm::Triple::x86_64:
4762 GCCLibPath += ("i386-" + T.getVendorName() + "-" +
4763 T.getOSName()).str();
4764 GCCLibPath += "/4.5.2/amd64/";
4765 LibPath += "amd64/";
4766 break;
4767 default:
4768 assert(0 && "Unsupported architecture");
4769 }
4770
4771 ArgStringList CmdArgs;
4772
David Chisnall41d476d2012-02-29 15:06:12 +00004773 // Demangle C++ names in errors
4774 CmdArgs.push_back("-C");
4775
David Chisnall31c46902012-02-15 13:39:01 +00004776 if ((!Args.hasArg(options::OPT_nostdlib)) &&
4777 (!Args.hasArg(options::OPT_shared))) {
4778 CmdArgs.push_back("-e");
4779 CmdArgs.push_back("_start");
4780 }
4781
4782 if (Args.hasArg(options::OPT_static)) {
4783 CmdArgs.push_back("-Bstatic");
4784 CmdArgs.push_back("-dn");
4785 } else {
4786 CmdArgs.push_back("-Bdynamic");
4787 if (Args.hasArg(options::OPT_shared)) {
4788 CmdArgs.push_back("-shared");
4789 } else {
4790 CmdArgs.push_back("--dynamic-linker");
4791 CmdArgs.push_back(Args.MakeArgString(LibPath + "ld.so.1"));
4792 }
4793 }
4794
4795 if (Output.isFilename()) {
4796 CmdArgs.push_back("-o");
4797 CmdArgs.push_back(Output.getFilename());
4798 } else {
4799 assert(Output.isNothing() && "Invalid output.");
4800 }
4801
4802 if (!Args.hasArg(options::OPT_nostdlib) &&
4803 !Args.hasArg(options::OPT_nostartfiles)) {
4804 if (!Args.hasArg(options::OPT_shared)) {
4805 CmdArgs.push_back(Args.MakeArgString(LibPath + "crt1.o"));
4806 CmdArgs.push_back(Args.MakeArgString(LibPath + "crti.o"));
David Chisnall165329c2012-02-28 17:10:04 +00004807 CmdArgs.push_back(Args.MakeArgString(LibPath + "values-Xa.o"));
David Chisnall31c46902012-02-15 13:39:01 +00004808 CmdArgs.push_back(Args.MakeArgString(GCCLibPath + "crtbegin.o"));
4809 } else {
4810 CmdArgs.push_back(Args.MakeArgString(LibPath + "crti.o"));
David Chisnall165329c2012-02-28 17:10:04 +00004811 CmdArgs.push_back(Args.MakeArgString(LibPath + "values-Xa.o"));
4812 CmdArgs.push_back(Args.MakeArgString(GCCLibPath + "crtbegin.o"));
David Chisnall31c46902012-02-15 13:39:01 +00004813 }
David Chisnalle6dd6832012-03-13 14:14:54 +00004814 if (getToolChain().getDriver().CCCIsCXX)
4815 CmdArgs.push_back(Args.MakeArgString(LibPath + "cxa_finalize.o"));
David Chisnall31c46902012-02-15 13:39:01 +00004816 }
4817
4818 CmdArgs.push_back(Args.MakeArgString("-L" + GCCLibPath));
4819
4820 Args.AddAllArgs(CmdArgs, options::OPT_L);
4821 Args.AddAllArgs(CmdArgs, options::OPT_T_Group);
4822 Args.AddAllArgs(CmdArgs, options::OPT_e);
David Chisnall165329c2012-02-28 17:10:04 +00004823 Args.AddAllArgs(CmdArgs, options::OPT_r);
David Chisnall31c46902012-02-15 13:39:01 +00004824
4825 AddLinkerInputs(getToolChain(), Inputs, Args, CmdArgs);
4826
4827 if (!Args.hasArg(options::OPT_nostdlib) &&
4828 !Args.hasArg(options::OPT_nodefaultlibs)) {
David Chisnalle58e6f92012-04-10 11:49:50 +00004829 if (getToolChain().getDriver().CCCIsCXX)
4830 getToolChain().AddCXXStdlibLibArgs(Args, CmdArgs);
David Chisnallb6229592012-02-15 18:24:31 +00004831 CmdArgs.push_back("-lgcc_s");
David Chisnall165329c2012-02-28 17:10:04 +00004832 if (!Args.hasArg(options::OPT_shared)) {
4833 CmdArgs.push_back("-lgcc");
David Chisnall31c46902012-02-15 13:39:01 +00004834 CmdArgs.push_back("-lc");
David Chisnall7dbefe12012-02-28 20:06:45 +00004835 CmdArgs.push_back("-lm");
David Chisnall165329c2012-02-28 17:10:04 +00004836 }
David Chisnall31c46902012-02-15 13:39:01 +00004837 }
4838
4839 if (!Args.hasArg(options::OPT_nostdlib) &&
4840 !Args.hasArg(options::OPT_nostartfiles)) {
David Chisnall165329c2012-02-28 17:10:04 +00004841 CmdArgs.push_back(Args.MakeArgString(GCCLibPath + "crtend.o"));
David Chisnall31c46902012-02-15 13:39:01 +00004842 }
David Chisnalld1ac03e2012-02-16 16:00:47 +00004843 CmdArgs.push_back(Args.MakeArgString(LibPath + "crtn.o"));
David Chisnall31c46902012-02-15 13:39:01 +00004844
4845 addProfileRT(getToolChain(), Args, CmdArgs, getToolChain().getTriple());
4846
4847 const char *Exec =
4848 Args.MakeArgString(getToolChain().GetProgramPath("ld"));
4849 C.addCommand(new Command(JA, *this, Exec, CmdArgs));
4850}
4851
Edward O'Callaghane7925a02009-08-22 01:06:46 +00004852void auroraux::Assemble::ConstructJob(Compilation &C, const JobAction &JA,
Daniel Dunbar2fe238e2010-08-02 02:38:28 +00004853 const InputInfo &Output,
Daniel Dunbar294691e2009-11-04 06:24:38 +00004854 const InputInfoList &Inputs,
4855 const ArgList &Args,
4856 const char *LinkingOutput) const {
Edward O'Callaghane7925a02009-08-22 01:06:46 +00004857 ArgStringList CmdArgs;
4858
4859 Args.AddAllArgValues(CmdArgs, options::OPT_Wa_COMMA,
4860 options::OPT_Xassembler);
4861
4862 CmdArgs.push_back("-o");
Daniel Dunbar7c1e4652010-08-02 02:38:21 +00004863 CmdArgs.push_back(Output.getFilename());
Edward O'Callaghane7925a02009-08-22 01:06:46 +00004864
4865 for (InputInfoList::const_iterator
4866 it = Inputs.begin(), ie = Inputs.end(); it != ie; ++it) {
4867 const InputInfo &II = *it;
Daniel Dunbar7c1e4652010-08-02 02:38:21 +00004868 CmdArgs.push_back(II.getFilename());
Edward O'Callaghane7925a02009-08-22 01:06:46 +00004869 }
4870
4871 const char *Exec =
Daniel Dunbar4a7e8892010-07-14 18:46:23 +00004872 Args.MakeArgString(getToolChain().GetProgramPath("gas"));
Daniel Dunbar2fe238e2010-08-02 02:38:28 +00004873 C.addCommand(new Command(JA, *this, Exec, CmdArgs));
Edward O'Callaghane7925a02009-08-22 01:06:46 +00004874}
4875
4876void auroraux::Link::ConstructJob(Compilation &C, const JobAction &JA,
Daniel Dunbar2fe238e2010-08-02 02:38:28 +00004877 const InputInfo &Output,
Daniel Dunbar294691e2009-11-04 06:24:38 +00004878 const InputInfoList &Inputs,
4879 const ArgList &Args,
4880 const char *LinkingOutput) const {
Edward O'Callaghane7925a02009-08-22 01:06:46 +00004881 ArgStringList CmdArgs;
4882
4883 if ((!Args.hasArg(options::OPT_nostdlib)) &&
Daniel Dunbar294691e2009-11-04 06:24:38 +00004884 (!Args.hasArg(options::OPT_shared))) {
Edward O'Callaghane7925a02009-08-22 01:06:46 +00004885 CmdArgs.push_back("-e");
Edward O'Callaghan7adf9492009-10-15 07:44:07 +00004886 CmdArgs.push_back("_start");
Edward O'Callaghane7925a02009-08-22 01:06:46 +00004887 }
4888
4889 if (Args.hasArg(options::OPT_static)) {
4890 CmdArgs.push_back("-Bstatic");
Edward O'Callaghan7adf9492009-10-15 07:44:07 +00004891 CmdArgs.push_back("-dn");
Edward O'Callaghane7925a02009-08-22 01:06:46 +00004892 } else {
Edward O'Callaghan7adf9492009-10-15 07:44:07 +00004893// CmdArgs.push_back("--eh-frame-hdr");
Edward O'Callaghane7925a02009-08-22 01:06:46 +00004894 CmdArgs.push_back("-Bdynamic");
4895 if (Args.hasArg(options::OPT_shared)) {
4896 CmdArgs.push_back("-shared");
4897 } else {
Edward O'Callaghan3cecc192009-10-16 19:44:18 +00004898 CmdArgs.push_back("--dynamic-linker");
Edward O'Callaghane7925a02009-08-22 01:06:46 +00004899 CmdArgs.push_back("/lib/ld.so.1"); // 64Bit Path /lib/amd64/ld.so.1
4900 }
4901 }
4902
Daniel Dunbar7c1e4652010-08-02 02:38:21 +00004903 if (Output.isFilename()) {
Edward O'Callaghane7925a02009-08-22 01:06:46 +00004904 CmdArgs.push_back("-o");
4905 CmdArgs.push_back(Output.getFilename());
4906 } else {
4907 assert(Output.isNothing() && "Invalid output.");
4908 }
4909
4910 if (!Args.hasArg(options::OPT_nostdlib) &&
4911 !Args.hasArg(options::OPT_nostartfiles)) {
4912 if (!Args.hasArg(options::OPT_shared)) {
Chris Lattner38e317d2010-07-07 16:01:42 +00004913 CmdArgs.push_back(Args.MakeArgString(
Daniel Dunbar4a7e8892010-07-14 18:46:23 +00004914 getToolChain().GetFilePath("crt1.o")));
Chris Lattner38e317d2010-07-07 16:01:42 +00004915 CmdArgs.push_back(Args.MakeArgString(
Daniel Dunbar4a7e8892010-07-14 18:46:23 +00004916 getToolChain().GetFilePath("crti.o")));
Chris Lattner38e317d2010-07-07 16:01:42 +00004917 CmdArgs.push_back(Args.MakeArgString(
Daniel Dunbar4a7e8892010-07-14 18:46:23 +00004918 getToolChain().GetFilePath("crtbegin.o")));
Edward O'Callaghane7925a02009-08-22 01:06:46 +00004919 } else {
Chris Lattner38e317d2010-07-07 16:01:42 +00004920 CmdArgs.push_back(Args.MakeArgString(
Daniel Dunbar4a7e8892010-07-14 18:46:23 +00004921 getToolChain().GetFilePath("crti.o")));
Edward O'Callaghane7925a02009-08-22 01:06:46 +00004922 }
Chris Lattner38e317d2010-07-07 16:01:42 +00004923 CmdArgs.push_back(Args.MakeArgString(
Daniel Dunbar4a7e8892010-07-14 18:46:23 +00004924 getToolChain().GetFilePath("crtn.o")));
Edward O'Callaghane7925a02009-08-22 01:06:46 +00004925 }
4926
Daniel Dunbar294691e2009-11-04 06:24:38 +00004927 CmdArgs.push_back(Args.MakeArgString("-L/opt/gcc4/lib/gcc/"
4928 + getToolChain().getTripleString()
Daniel Dunbarf7fb31f2009-10-29 02:24:37 +00004929 + "/4.2.4"));
Edward O'Callaghane7925a02009-08-22 01:06:46 +00004930
4931 Args.AddAllArgs(CmdArgs, options::OPT_L);
4932 Args.AddAllArgs(CmdArgs, options::OPT_T_Group);
4933 Args.AddAllArgs(CmdArgs, options::OPT_e);
4934
Daniel Dunbar2008fee2010-09-17 00:24:54 +00004935 AddLinkerInputs(getToolChain(), Inputs, Args, CmdArgs);
Edward O'Callaghane7925a02009-08-22 01:06:46 +00004936
4937 if (!Args.hasArg(options::OPT_nostdlib) &&
4938 !Args.hasArg(options::OPT_nodefaultlibs)) {
4939 // FIXME: For some reason GCC passes -lgcc before adding
4940 // the default system libraries. Just mimic this for now.
4941 CmdArgs.push_back("-lgcc");
4942
4943 if (Args.hasArg(options::OPT_pthread))
4944 CmdArgs.push_back("-pthread");
4945 if (!Args.hasArg(options::OPT_shared))
4946 CmdArgs.push_back("-lc");
4947 CmdArgs.push_back("-lgcc");
4948 }
4949
4950 if (!Args.hasArg(options::OPT_nostdlib) &&
4951 !Args.hasArg(options::OPT_nostartfiles)) {
4952 if (!Args.hasArg(options::OPT_shared))
Chris Lattner38e317d2010-07-07 16:01:42 +00004953 CmdArgs.push_back(Args.MakeArgString(
Daniel Dunbar4a7e8892010-07-14 18:46:23 +00004954 getToolChain().GetFilePath("crtend.o")));
Edward O'Callaghane7925a02009-08-22 01:06:46 +00004955 }
4956
Bill Wendling3f4be6f2011-06-27 19:15:03 +00004957 addProfileRT(getToolChain(), Args, CmdArgs, getToolChain().getTriple());
Nick Lewycky2e95a6d2011-05-24 21:54:59 +00004958
Edward O'Callaghane7925a02009-08-22 01:06:46 +00004959 const char *Exec =
Daniel Dunbar4a7e8892010-07-14 18:46:23 +00004960 Args.MakeArgString(getToolChain().GetProgramPath("ld"));
Daniel Dunbar2fe238e2010-08-02 02:38:28 +00004961 C.addCommand(new Command(JA, *this, Exec, CmdArgs));
Edward O'Callaghane7925a02009-08-22 01:06:46 +00004962}
4963
Daniel Dunbarf7b8eec2009-06-29 20:52:51 +00004964void openbsd::Assemble::ConstructJob(Compilation &C, const JobAction &JA,
Daniel Dunbar2fe238e2010-08-02 02:38:28 +00004965 const InputInfo &Output,
Daniel Dunbarf7b8eec2009-06-29 20:52:51 +00004966 const InputInfoList &Inputs,
4967 const ArgList &Args,
Mike Stump1eb44332009-09-09 15:08:12 +00004968 const char *LinkingOutput) const {
Daniel Dunbarf7b8eec2009-06-29 20:52:51 +00004969 ArgStringList CmdArgs;
4970
4971 Args.AddAllArgValues(CmdArgs, options::OPT_Wa_COMMA,
4972 options::OPT_Xassembler);
4973
4974 CmdArgs.push_back("-o");
Daniel Dunbar7c1e4652010-08-02 02:38:21 +00004975 CmdArgs.push_back(Output.getFilename());
Daniel Dunbarf7b8eec2009-06-29 20:52:51 +00004976
4977 for (InputInfoList::const_iterator
4978 it = Inputs.begin(), ie = Inputs.end(); it != ie; ++it) {
4979 const InputInfo &II = *it;
Daniel Dunbar7c1e4652010-08-02 02:38:21 +00004980 CmdArgs.push_back(II.getFilename());
Daniel Dunbarf7b8eec2009-06-29 20:52:51 +00004981 }
4982
4983 const char *Exec =
Daniel Dunbar4a7e8892010-07-14 18:46:23 +00004984 Args.MakeArgString(getToolChain().GetProgramPath("as"));
Daniel Dunbar2fe238e2010-08-02 02:38:28 +00004985 C.addCommand(new Command(JA, *this, Exec, CmdArgs));
Daniel Dunbarf7b8eec2009-06-29 20:52:51 +00004986}
4987
4988void openbsd::Link::ConstructJob(Compilation &C, const JobAction &JA,
Daniel Dunbar2fe238e2010-08-02 02:38:28 +00004989 const InputInfo &Output,
Daniel Dunbarf7b8eec2009-06-29 20:52:51 +00004990 const InputInfoList &Inputs,
4991 const ArgList &Args,
4992 const char *LinkingOutput) const {
Daniel Dunbaree788e72009-12-21 18:54:17 +00004993 const Driver &D = getToolChain().getDriver();
Daniel Dunbarf7b8eec2009-06-29 20:52:51 +00004994 ArgStringList CmdArgs;
4995
Rafael Espindola6cc2a682012-12-31 22:41:36 +00004996 // Silence warning for "clang -g foo.o -o foo"
4997 Args.ClaimAllArgs(options::OPT_g_Group);
4998 // and "clang -emit-llvm foo.o -o foo"
4999 Args.ClaimAllArgs(options::OPT_emit_llvm);
5000 // and for "clang -w foo.o -o foo". Other warning options are already
5001 // handled somewhere else.
5002 Args.ClaimAllArgs(options::OPT_w);
5003
Daniel Dunbar2bbcf662009-08-03 01:28:59 +00005004 if ((!Args.hasArg(options::OPT_nostdlib)) &&
Daniel Dunbar294691e2009-11-04 06:24:38 +00005005 (!Args.hasArg(options::OPT_shared))) {
Daniel Dunbar2bbcf662009-08-03 01:28:59 +00005006 CmdArgs.push_back("-e");
5007 CmdArgs.push_back("__start");
5008 }
5009
Daniel Dunbarf7b8eec2009-06-29 20:52:51 +00005010 if (Args.hasArg(options::OPT_static)) {
5011 CmdArgs.push_back("-Bstatic");
5012 } else {
Rafael Espindola65ba55d2010-11-11 02:17:51 +00005013 if (Args.hasArg(options::OPT_rdynamic))
5014 CmdArgs.push_back("-export-dynamic");
Daniel Dunbarf7b8eec2009-06-29 20:52:51 +00005015 CmdArgs.push_back("--eh-frame-hdr");
Daniel Dunbar2bbcf662009-08-03 01:28:59 +00005016 CmdArgs.push_back("-Bdynamic");
Daniel Dunbarf7b8eec2009-06-29 20:52:51 +00005017 if (Args.hasArg(options::OPT_shared)) {
Daniel Dunbar2bbcf662009-08-03 01:28:59 +00005018 CmdArgs.push_back("-shared");
Daniel Dunbarf7b8eec2009-06-29 20:52:51 +00005019 } else {
5020 CmdArgs.push_back("-dynamic-linker");
5021 CmdArgs.push_back("/usr/libexec/ld.so");
5022 }
5023 }
5024
Daniel Dunbar7c1e4652010-08-02 02:38:21 +00005025 if (Output.isFilename()) {
Daniel Dunbarf7b8eec2009-06-29 20:52:51 +00005026 CmdArgs.push_back("-o");
5027 CmdArgs.push_back(Output.getFilename());
5028 } else {
5029 assert(Output.isNothing() && "Invalid output.");
5030 }
5031
5032 if (!Args.hasArg(options::OPT_nostdlib) &&
5033 !Args.hasArg(options::OPT_nostartfiles)) {
5034 if (!Args.hasArg(options::OPT_shared)) {
Eli Friedman62d829a2011-12-15 02:15:56 +00005035 if (Args.hasArg(options::OPT_pg))
5036 CmdArgs.push_back(Args.MakeArgString(
5037 getToolChain().GetFilePath("gcrt0.o")));
5038 else
5039 CmdArgs.push_back(Args.MakeArgString(
5040 getToolChain().GetFilePath("crt0.o")));
Chris Lattner38e317d2010-07-07 16:01:42 +00005041 CmdArgs.push_back(Args.MakeArgString(
Daniel Dunbar4a7e8892010-07-14 18:46:23 +00005042 getToolChain().GetFilePath("crtbegin.o")));
Daniel Dunbarf7b8eec2009-06-29 20:52:51 +00005043 } else {
Chris Lattner38e317d2010-07-07 16:01:42 +00005044 CmdArgs.push_back(Args.MakeArgString(
Daniel Dunbar4a7e8892010-07-14 18:46:23 +00005045 getToolChain().GetFilePath("crtbeginS.o")));
Daniel Dunbarf7b8eec2009-06-29 20:52:51 +00005046 }
5047 }
5048
Edward O'Callaghane7e18202009-10-28 15:13:08 +00005049 std::string Triple = getToolChain().getTripleString();
5050 if (Triple.substr(0, 6) == "x86_64")
Daniel Dunbar294691e2009-11-04 06:24:38 +00005051 Triple.replace(0, 6, "amd64");
Daniel Dunbarf7fb31f2009-10-29 02:24:37 +00005052 CmdArgs.push_back(Args.MakeArgString("-L/usr/lib/gcc-lib/" + Triple +
Daniel Dunbar95c04572010-08-01 23:13:54 +00005053 "/4.2.1"));
Daniel Dunbar2bbcf662009-08-03 01:28:59 +00005054
Daniel Dunbarf7b8eec2009-06-29 20:52:51 +00005055 Args.AddAllArgs(CmdArgs, options::OPT_L);
5056 Args.AddAllArgs(CmdArgs, options::OPT_T_Group);
5057 Args.AddAllArgs(CmdArgs, options::OPT_e);
Rafael Espindola6cc2a682012-12-31 22:41:36 +00005058 Args.AddAllArgs(CmdArgs, options::OPT_s);
5059 Args.AddAllArgs(CmdArgs, options::OPT_t);
5060 Args.AddAllArgs(CmdArgs, options::OPT_Z_Flag);
5061 Args.AddAllArgs(CmdArgs, options::OPT_r);
Daniel Dunbarf7b8eec2009-06-29 20:52:51 +00005062
Daniel Dunbar2008fee2010-09-17 00:24:54 +00005063 AddLinkerInputs(getToolChain(), Inputs, Args, CmdArgs);
Daniel Dunbarf7b8eec2009-06-29 20:52:51 +00005064
5065 if (!Args.hasArg(options::OPT_nostdlib) &&
5066 !Args.hasArg(options::OPT_nodefaultlibs)) {
Daniel Dunbar95c04572010-08-01 23:13:54 +00005067 if (D.CCCIsCXX) {
Daniel Dunbar132e35d2010-09-17 01:20:05 +00005068 getToolChain().AddCXXStdlibLibArgs(Args, CmdArgs);
Eli Friedman62d829a2011-12-15 02:15:56 +00005069 if (Args.hasArg(options::OPT_pg))
5070 CmdArgs.push_back("-lm_p");
5071 else
5072 CmdArgs.push_back("-lm");
Daniel Dunbar95c04572010-08-01 23:13:54 +00005073 }
5074
Daniel Dunbar2bbcf662009-08-03 01:28:59 +00005075 // FIXME: For some reason GCC passes -lgcc before adding
5076 // the default system libraries. Just mimic this for now.
5077 CmdArgs.push_back("-lgcc");
Daniel Dunbarf7b8eec2009-06-29 20:52:51 +00005078
Eric Christopherdc6cc872012-09-13 06:32:34 +00005079 if (Args.hasArg(options::OPT_pthread)) {
5080 if (!Args.hasArg(options::OPT_shared) &&
5081 Args.hasArg(options::OPT_pg))
5082 CmdArgs.push_back("-lpthread_p");
5083 else
5084 CmdArgs.push_back("-lpthread");
5085 }
5086
Chandler Carruth657849c2011-12-17 22:32:42 +00005087 if (!Args.hasArg(options::OPT_shared)) {
Eric Christopherdc6cc872012-09-13 06:32:34 +00005088 if (Args.hasArg(options::OPT_pg))
Eli Friedman62d829a2011-12-15 02:15:56 +00005089 CmdArgs.push_back("-lc_p");
5090 else
5091 CmdArgs.push_back("-lc");
Chandler Carruth657849c2011-12-17 22:32:42 +00005092 }
Eric Christopherdc6cc872012-09-13 06:32:34 +00005093
Daniel Dunbar2bbcf662009-08-03 01:28:59 +00005094 CmdArgs.push_back("-lgcc");
Daniel Dunbarf7b8eec2009-06-29 20:52:51 +00005095 }
5096
5097 if (!Args.hasArg(options::OPT_nostdlib) &&
5098 !Args.hasArg(options::OPT_nostartfiles)) {
5099 if (!Args.hasArg(options::OPT_shared))
Chris Lattner38e317d2010-07-07 16:01:42 +00005100 CmdArgs.push_back(Args.MakeArgString(
Daniel Dunbar4a7e8892010-07-14 18:46:23 +00005101 getToolChain().GetFilePath("crtend.o")));
Daniel Dunbarf7b8eec2009-06-29 20:52:51 +00005102 else
Chris Lattner38e317d2010-07-07 16:01:42 +00005103 CmdArgs.push_back(Args.MakeArgString(
Daniel Dunbar4a7e8892010-07-14 18:46:23 +00005104 getToolChain().GetFilePath("crtendS.o")));
Daniel Dunbarf7b8eec2009-06-29 20:52:51 +00005105 }
5106
5107 const char *Exec =
Daniel Dunbar4a7e8892010-07-14 18:46:23 +00005108 Args.MakeArgString(getToolChain().GetProgramPath("ld"));
Daniel Dunbar2fe238e2010-08-02 02:38:28 +00005109 C.addCommand(new Command(JA, *this, Exec, CmdArgs));
Daniel Dunbarf7b8eec2009-06-29 20:52:51 +00005110}
Ed Schoutenc66a5a32009-04-02 19:13:12 +00005111
Eli Friedman42f74f22012-08-08 23:57:20 +00005112void bitrig::Assemble::ConstructJob(Compilation &C, const JobAction &JA,
5113 const InputInfo &Output,
5114 const InputInfoList &Inputs,
5115 const ArgList &Args,
5116 const char *LinkingOutput) const {
5117 ArgStringList CmdArgs;
5118
5119 Args.AddAllArgValues(CmdArgs, options::OPT_Wa_COMMA,
5120 options::OPT_Xassembler);
5121
5122 CmdArgs.push_back("-o");
5123 CmdArgs.push_back(Output.getFilename());
5124
5125 for (InputInfoList::const_iterator
5126 it = Inputs.begin(), ie = Inputs.end(); it != ie; ++it) {
5127 const InputInfo &II = *it;
5128 CmdArgs.push_back(II.getFilename());
5129 }
5130
5131 const char *Exec =
5132 Args.MakeArgString(getToolChain().GetProgramPath("as"));
5133 C.addCommand(new Command(JA, *this, Exec, CmdArgs));
5134}
5135
5136void bitrig::Link::ConstructJob(Compilation &C, const JobAction &JA,
5137 const InputInfo &Output,
5138 const InputInfoList &Inputs,
5139 const ArgList &Args,
5140 const char *LinkingOutput) const {
5141 const Driver &D = getToolChain().getDriver();
5142 ArgStringList CmdArgs;
5143
5144 if ((!Args.hasArg(options::OPT_nostdlib)) &&
5145 (!Args.hasArg(options::OPT_shared))) {
5146 CmdArgs.push_back("-e");
5147 CmdArgs.push_back("__start");
5148 }
5149
5150 if (Args.hasArg(options::OPT_static)) {
5151 CmdArgs.push_back("-Bstatic");
5152 } else {
5153 if (Args.hasArg(options::OPT_rdynamic))
5154 CmdArgs.push_back("-export-dynamic");
5155 CmdArgs.push_back("--eh-frame-hdr");
5156 CmdArgs.push_back("-Bdynamic");
5157 if (Args.hasArg(options::OPT_shared)) {
5158 CmdArgs.push_back("-shared");
5159 } else {
5160 CmdArgs.push_back("-dynamic-linker");
5161 CmdArgs.push_back("/usr/libexec/ld.so");
5162 }
5163 }
5164
5165 if (Output.isFilename()) {
5166 CmdArgs.push_back("-o");
5167 CmdArgs.push_back(Output.getFilename());
5168 } else {
5169 assert(Output.isNothing() && "Invalid output.");
5170 }
5171
5172 if (!Args.hasArg(options::OPT_nostdlib) &&
5173 !Args.hasArg(options::OPT_nostartfiles)) {
5174 if (!Args.hasArg(options::OPT_shared)) {
5175 if (Args.hasArg(options::OPT_pg))
5176 CmdArgs.push_back(Args.MakeArgString(
5177 getToolChain().GetFilePath("gcrt0.o")));
5178 else
5179 CmdArgs.push_back(Args.MakeArgString(
5180 getToolChain().GetFilePath("crt0.o")));
5181 CmdArgs.push_back(Args.MakeArgString(
5182 getToolChain().GetFilePath("crtbegin.o")));
5183 } else {
5184 CmdArgs.push_back(Args.MakeArgString(
5185 getToolChain().GetFilePath("crtbeginS.o")));
5186 }
5187 }
5188
5189 Args.AddAllArgs(CmdArgs, options::OPT_L);
5190 Args.AddAllArgs(CmdArgs, options::OPT_T_Group);
5191 Args.AddAllArgs(CmdArgs, options::OPT_e);
5192
5193 AddLinkerInputs(getToolChain(), Inputs, Args, CmdArgs);
5194
5195 if (!Args.hasArg(options::OPT_nostdlib) &&
5196 !Args.hasArg(options::OPT_nodefaultlibs)) {
5197 if (D.CCCIsCXX) {
5198 getToolChain().AddCXXStdlibLibArgs(Args, CmdArgs);
5199 if (Args.hasArg(options::OPT_pg))
5200 CmdArgs.push_back("-lm_p");
5201 else
5202 CmdArgs.push_back("-lm");
5203 }
5204
Rafael Espindola3667bbe2012-10-23 17:07:31 +00005205 if (Args.hasArg(options::OPT_pthread)) {
5206 if (!Args.hasArg(options::OPT_shared) &&
5207 Args.hasArg(options::OPT_pg))
5208 CmdArgs.push_back("-lpthread_p");
5209 else
5210 CmdArgs.push_back("-lpthread");
5211 }
5212
Eli Friedman42f74f22012-08-08 23:57:20 +00005213 if (!Args.hasArg(options::OPT_shared)) {
5214 if (Args.hasArg(options::OPT_pg))
5215 CmdArgs.push_back("-lc_p");
5216 else
5217 CmdArgs.push_back("-lc");
5218 }
5219
5220 std::string myarch = "-lclang_rt.";
5221 const llvm::Triple &T = getToolChain().getTriple();
5222 llvm::Triple::ArchType Arch = T.getArch();
5223 switch (Arch) {
5224 case llvm::Triple::arm:
5225 myarch += ("arm");
5226 break;
5227 case llvm::Triple::x86:
5228 myarch += ("i386");
5229 break;
5230 case llvm::Triple::x86_64:
5231 myarch += ("amd64");
5232 break;
5233 default:
5234 assert(0 && "Unsupported architecture");
5235 }
5236 CmdArgs.push_back(Args.MakeArgString(myarch));
5237 }
5238
5239 if (!Args.hasArg(options::OPT_nostdlib) &&
5240 !Args.hasArg(options::OPT_nostartfiles)) {
5241 if (!Args.hasArg(options::OPT_shared))
5242 CmdArgs.push_back(Args.MakeArgString(
5243 getToolChain().GetFilePath("crtend.o")));
5244 else
5245 CmdArgs.push_back(Args.MakeArgString(
5246 getToolChain().GetFilePath("crtendS.o")));
5247 }
Eli Friedmanc9c48db2012-08-09 22:42:04 +00005248
5249 const char *Exec =
5250 Args.MakeArgString(getToolChain().GetProgramPath("ld"));
5251 C.addCommand(new Command(JA, *this, Exec, CmdArgs));
Eli Friedman42f74f22012-08-08 23:57:20 +00005252}
5253
Daniel Dunbar68a31d42009-03-31 17:45:15 +00005254void freebsd::Assemble::ConstructJob(Compilation &C, const JobAction &JA,
Daniel Dunbar2fe238e2010-08-02 02:38:28 +00005255 const InputInfo &Output,
Daniel Dunbarc21c4852009-04-08 23:54:23 +00005256 const InputInfoList &Inputs,
5257 const ArgList &Args,
Mike Stump1eb44332009-09-09 15:08:12 +00005258 const char *LinkingOutput) const {
Daniel Dunbar68a31d42009-03-31 17:45:15 +00005259 ArgStringList CmdArgs;
5260
Daniel Dunbar008f54a2009-04-01 19:36:32 +00005261 // When building 32-bit code on FreeBSD/amd64, we have to explicitly
5262 // instruct as in the base system to assemble 32-bit code.
Eric Christopherc55da4b2012-09-05 21:32:44 +00005263 if (getToolChain().getArch() == llvm::Triple::x86)
Daniel Dunbar68a31d42009-03-31 17:45:15 +00005264 CmdArgs.push_back("--32");
Eric Christopherc55da4b2012-09-05 21:32:44 +00005265 else if (getToolChain().getArch() == llvm::Triple::ppc)
Roman Divacky3393cef2011-06-04 07:37:31 +00005266 CmdArgs.push_back("-a32");
Eric Christopherc55da4b2012-09-05 21:32:44 +00005267 else if (getToolChain().getArch() == llvm::Triple::mips ||
5268 getToolChain().getArch() == llvm::Triple::mipsel ||
5269 getToolChain().getArch() == llvm::Triple::mips64 ||
5270 getToolChain().getArch() == llvm::Triple::mips64el) {
5271 StringRef CPUName;
5272 StringRef ABIName;
5273 getMipsCPUAndABI(Args, getToolChain(), CPUName, ABIName);
Michael J. Spencer20249a12010-10-21 03:16:25 +00005274
Eric Christopherc55da4b2012-09-05 21:32:44 +00005275 CmdArgs.push_back("-march");
5276 CmdArgs.push_back(CPUName.data());
5277
Eric Christopherc55da4b2012-09-05 21:32:44 +00005278 CmdArgs.push_back("-mabi");
Simon Atanasyane9616a42013-02-27 14:55:49 +00005279 CmdArgs.push_back(getGnuCompatibleMipsABIName(ABIName).data());
Eric Christopherc55da4b2012-09-05 21:32:44 +00005280
5281 if (getToolChain().getArch() == llvm::Triple::mips ||
5282 getToolChain().getArch() == llvm::Triple::mips64)
5283 CmdArgs.push_back("-EB");
5284 else
5285 CmdArgs.push_back("-EL");
5286
5287 Arg *LastPICArg = Args.getLastArg(options::OPT_fPIC, options::OPT_fno_PIC,
5288 options::OPT_fpic, options::OPT_fno_pic,
5289 options::OPT_fPIE, options::OPT_fno_PIE,
5290 options::OPT_fpie, options::OPT_fno_pie);
5291 if (LastPICArg &&
5292 (LastPICArg->getOption().matches(options::OPT_fPIC) ||
5293 LastPICArg->getOption().matches(options::OPT_fpic) ||
5294 LastPICArg->getOption().matches(options::OPT_fPIE) ||
5295 LastPICArg->getOption().matches(options::OPT_fpie))) {
5296 CmdArgs.push_back("-KPIC");
5297 }
Rafael Espindola27fa2362012-12-13 04:17:14 +00005298 } else if (getToolChain().getArch() == llvm::Triple::arm ||
5299 getToolChain().getArch() == llvm::Triple::thumb) {
5300 CmdArgs.push_back("-mfpu=softvfp");
5301 switch(getToolChain().getTriple().getEnvironment()) {
5302 case llvm::Triple::GNUEABI:
5303 case llvm::Triple::EABI:
Anton Korobeynikovb234e742013-03-18 07:59:20 +00005304 CmdArgs.push_back("-meabi=5");
Rafael Espindola27fa2362012-12-13 04:17:14 +00005305 break;
5306
5307 default:
5308 CmdArgs.push_back("-matpcs");
5309 }
Eric Christopherc55da4b2012-09-05 21:32:44 +00005310 }
Eric Christophered734732010-03-02 02:41:08 +00005311
Daniel Dunbar68a31d42009-03-31 17:45:15 +00005312 Args.AddAllArgValues(CmdArgs, options::OPT_Wa_COMMA,
5313 options::OPT_Xassembler);
5314
5315 CmdArgs.push_back("-o");
Daniel Dunbar7c1e4652010-08-02 02:38:21 +00005316 CmdArgs.push_back(Output.getFilename());
Daniel Dunbar68a31d42009-03-31 17:45:15 +00005317
5318 for (InputInfoList::const_iterator
5319 it = Inputs.begin(), ie = Inputs.end(); it != ie; ++it) {
5320 const InputInfo &II = *it;
Daniel Dunbar7c1e4652010-08-02 02:38:21 +00005321 CmdArgs.push_back(II.getFilename());
Daniel Dunbar68a31d42009-03-31 17:45:15 +00005322 }
5323
Daniel Dunbarc21c4852009-04-08 23:54:23 +00005324 const char *Exec =
Daniel Dunbar4a7e8892010-07-14 18:46:23 +00005325 Args.MakeArgString(getToolChain().GetProgramPath("as"));
Daniel Dunbar2fe238e2010-08-02 02:38:28 +00005326 C.addCommand(new Command(JA, *this, Exec, CmdArgs));
Daniel Dunbar68a31d42009-03-31 17:45:15 +00005327}
Daniel Dunbar008f54a2009-04-01 19:36:32 +00005328
5329void freebsd::Link::ConstructJob(Compilation &C, const JobAction &JA,
Daniel Dunbar2fe238e2010-08-02 02:38:28 +00005330 const InputInfo &Output,
Daniel Dunbarc21c4852009-04-08 23:54:23 +00005331 const InputInfoList &Inputs,
5332 const ArgList &Args,
Daniel Dunbara8304f62009-05-02 20:14:53 +00005333 const char *LinkingOutput) const {
Roman Divacky94380162012-08-28 15:09:03 +00005334 const toolchains::FreeBSD& ToolChain =
5335 static_cast<const toolchains::FreeBSD&>(getToolChain());
5336 const Driver &D = ToolChain.getDriver();
Daniel Dunbar008f54a2009-04-01 19:36:32 +00005337 ArgStringList CmdArgs;
David Chisnalldfa210b2012-07-29 15:24:44 +00005338
5339 // Silence warning for "clang -g foo.o -o foo"
5340 Args.ClaimAllArgs(options::OPT_g_Group);
5341 // and "clang -emit-llvm foo.o -o foo"
5342 Args.ClaimAllArgs(options::OPT_emit_llvm);
5343 // and for "clang -w foo.o -o foo". Other warning options are already
5344 // handled somewhere else.
5345 Args.ClaimAllArgs(options::OPT_w);
Daniel Dunbar008f54a2009-04-01 19:36:32 +00005346
Joerg Sonnenberger8ab2bdc2011-03-21 13:51:29 +00005347 if (!D.SysRoot.empty())
5348 CmdArgs.push_back(Args.MakeArgString("--sysroot=" + D.SysRoot));
5349
Roman Divacky94380162012-08-28 15:09:03 +00005350 if (Args.hasArg(options::OPT_pie))
5351 CmdArgs.push_back("-pie");
5352
Daniel Dunbar008f54a2009-04-01 19:36:32 +00005353 if (Args.hasArg(options::OPT_static)) {
5354 CmdArgs.push_back("-Bstatic");
5355 } else {
Rafael Espindola65ba55d2010-11-11 02:17:51 +00005356 if (Args.hasArg(options::OPT_rdynamic))
5357 CmdArgs.push_back("-export-dynamic");
Daniel Dunbar008f54a2009-04-01 19:36:32 +00005358 CmdArgs.push_back("--eh-frame-hdr");
5359 if (Args.hasArg(options::OPT_shared)) {
5360 CmdArgs.push_back("-Bshareable");
5361 } else {
5362 CmdArgs.push_back("-dynamic-linker");
5363 CmdArgs.push_back("/libexec/ld-elf.so.1");
5364 }
Roman Divacky94380162012-08-28 15:09:03 +00005365 if (ToolChain.getTriple().getOSMajorVersion() >= 9) {
5366 llvm::Triple::ArchType Arch = ToolChain.getArch();
David Chisnalldfa210b2012-07-29 15:24:44 +00005367 if (Arch == llvm::Triple::arm || Arch == llvm::Triple::sparc ||
5368 Arch == llvm::Triple::x86 || Arch == llvm::Triple::x86_64) {
5369 CmdArgs.push_back("--hash-style=both");
5370 }
5371 }
5372 CmdArgs.push_back("--enable-new-dtags");
Daniel Dunbar008f54a2009-04-01 19:36:32 +00005373 }
5374
5375 // When building 32-bit code on FreeBSD/amd64, we have to explicitly
5376 // instruct ld in the base system to link 32-bit code.
Rafael Espindola64f7ad92012-10-07 04:44:33 +00005377 if (ToolChain.getArch() == llvm::Triple::x86) {
Daniel Dunbar008f54a2009-04-01 19:36:32 +00005378 CmdArgs.push_back("-m");
5379 CmdArgs.push_back("elf_i386_fbsd");
5380 }
5381
Rafael Espindola64f7ad92012-10-07 04:44:33 +00005382 if (ToolChain.getArch() == llvm::Triple::ppc) {
Roman Divacky000a6552011-06-04 07:40:24 +00005383 CmdArgs.push_back("-m");
Roman Divacky1052c1d2011-11-21 16:50:32 +00005384 CmdArgs.push_back("elf32ppc_fbsd");
Roman Divacky000a6552011-06-04 07:40:24 +00005385 }
5386
Daniel Dunbar7c1e4652010-08-02 02:38:21 +00005387 if (Output.isFilename()) {
Daniel Dunbar008f54a2009-04-01 19:36:32 +00005388 CmdArgs.push_back("-o");
5389 CmdArgs.push_back(Output.getFilename());
5390 } else {
5391 assert(Output.isNothing() && "Invalid output.");
5392 }
5393
5394 if (!Args.hasArg(options::OPT_nostdlib) &&
5395 !Args.hasArg(options::OPT_nostartfiles)) {
Roman Divacky94380162012-08-28 15:09:03 +00005396 const char *crt1 = NULL;
Daniel Dunbar008f54a2009-04-01 19:36:32 +00005397 if (!Args.hasArg(options::OPT_shared)) {
Roman Divackyc16bb762011-02-10 16:59:40 +00005398 if (Args.hasArg(options::OPT_pg))
Roman Divacky94380162012-08-28 15:09:03 +00005399 crt1 = "gcrt1.o";
5400 else if (Args.hasArg(options::OPT_pie))
5401 crt1 = "Scrt1.o";
5402 else
5403 crt1 = "crt1.o";
Daniel Dunbar008f54a2009-04-01 19:36:32 +00005404 }
Roman Divacky94380162012-08-28 15:09:03 +00005405 if (crt1)
5406 CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath(crt1)));
5407
5408 CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath("crti.o")));
5409
5410 const char *crtbegin = NULL;
5411 if (Args.hasArg(options::OPT_static))
5412 crtbegin = "crtbeginT.o";
5413 else if (Args.hasArg(options::OPT_shared) || Args.hasArg(options::OPT_pie))
5414 crtbegin = "crtbeginS.o";
5415 else
5416 crtbegin = "crtbegin.o";
5417
5418 CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath(crtbegin)));
Daniel Dunbar008f54a2009-04-01 19:36:32 +00005419 }
5420
5421 Args.AddAllArgs(CmdArgs, options::OPT_L);
Roman Divacky94380162012-08-28 15:09:03 +00005422 const ToolChain::path_list Paths = ToolChain.getFilePaths();
Roman Divacky58e5ac92011-03-01 17:53:14 +00005423 for (ToolChain::path_list::const_iterator i = Paths.begin(), e = Paths.end();
5424 i != e; ++i)
Chris Lattner5f9e2722011-07-23 10:55:15 +00005425 CmdArgs.push_back(Args.MakeArgString(StringRef("-L") + *i));
Daniel Dunbar008f54a2009-04-01 19:36:32 +00005426 Args.AddAllArgs(CmdArgs, options::OPT_T_Group);
5427 Args.AddAllArgs(CmdArgs, options::OPT_e);
David Chisnallc7363772010-08-15 22:58:12 +00005428 Args.AddAllArgs(CmdArgs, options::OPT_s);
5429 Args.AddAllArgs(CmdArgs, options::OPT_t);
5430 Args.AddAllArgs(CmdArgs, options::OPT_Z_Flag);
5431 Args.AddAllArgs(CmdArgs, options::OPT_r);
Daniel Dunbar008f54a2009-04-01 19:36:32 +00005432
Roman Divacky94380162012-08-28 15:09:03 +00005433 AddLinkerInputs(ToolChain, Inputs, Args, CmdArgs);
Daniel Dunbar008f54a2009-04-01 19:36:32 +00005434
5435 if (!Args.hasArg(options::OPT_nostdlib) &&
5436 !Args.hasArg(options::OPT_nodefaultlibs)) {
Daniel Dunbar20022632010-02-17 08:07:51 +00005437 if (D.CCCIsCXX) {
Roman Divacky94380162012-08-28 15:09:03 +00005438 ToolChain.AddCXXStdlibLibArgs(Args, CmdArgs);
Roman Divackyc16bb762011-02-10 16:59:40 +00005439 if (Args.hasArg(options::OPT_pg))
5440 CmdArgs.push_back("-lm_p");
5441 else
5442 CmdArgs.push_back("-lm");
Daniel Dunbar20022632010-02-17 08:07:51 +00005443 }
Daniel Dunbar008f54a2009-04-01 19:36:32 +00005444 // FIXME: For some reason GCC passes -lgcc and -lgcc_s before adding
5445 // the default system libraries. Just mimic this for now.
Roman Divackyc16bb762011-02-10 16:59:40 +00005446 if (Args.hasArg(options::OPT_pg))
5447 CmdArgs.push_back("-lgcc_p");
5448 else
5449 CmdArgs.push_back("-lgcc");
Daniel Dunbar008f54a2009-04-01 19:36:32 +00005450 if (Args.hasArg(options::OPT_static)) {
5451 CmdArgs.push_back("-lgcc_eh");
Roman Divackyc16bb762011-02-10 16:59:40 +00005452 } else if (Args.hasArg(options::OPT_pg)) {
5453 CmdArgs.push_back("-lgcc_eh_p");
Daniel Dunbar008f54a2009-04-01 19:36:32 +00005454 } else {
5455 CmdArgs.push_back("--as-needed");
5456 CmdArgs.push_back("-lgcc_s");
5457 CmdArgs.push_back("--no-as-needed");
5458 }
5459
Matt Beaumont-Gay24230262011-02-10 20:35:01 +00005460 if (Args.hasArg(options::OPT_pthread)) {
Roman Divackyc16bb762011-02-10 16:59:40 +00005461 if (Args.hasArg(options::OPT_pg))
5462 CmdArgs.push_back("-lpthread_p");
5463 else
5464 CmdArgs.push_back("-lpthread");
Matt Beaumont-Gay24230262011-02-10 20:35:01 +00005465 }
Daniel Dunbar008f54a2009-04-01 19:36:32 +00005466
Roman Divackyc16bb762011-02-10 16:59:40 +00005467 if (Args.hasArg(options::OPT_pg)) {
5468 if (Args.hasArg(options::OPT_shared))
5469 CmdArgs.push_back("-lc");
5470 else
5471 CmdArgs.push_back("-lc_p");
5472 CmdArgs.push_back("-lgcc_p");
5473 } else {
5474 CmdArgs.push_back("-lc");
5475 CmdArgs.push_back("-lgcc");
5476 }
5477
Daniel Dunbar008f54a2009-04-01 19:36:32 +00005478 if (Args.hasArg(options::OPT_static)) {
5479 CmdArgs.push_back("-lgcc_eh");
Roman Divackyc16bb762011-02-10 16:59:40 +00005480 } else if (Args.hasArg(options::OPT_pg)) {
5481 CmdArgs.push_back("-lgcc_eh_p");
Daniel Dunbar008f54a2009-04-01 19:36:32 +00005482 } else {
5483 CmdArgs.push_back("--as-needed");
5484 CmdArgs.push_back("-lgcc_s");
5485 CmdArgs.push_back("--no-as-needed");
5486 }
5487 }
5488
5489 if (!Args.hasArg(options::OPT_nostdlib) &&
5490 !Args.hasArg(options::OPT_nostartfiles)) {
Roman Divackyf6513812012-09-07 13:36:21 +00005491 if (Args.hasArg(options::OPT_shared) || Args.hasArg(options::OPT_pie))
Roman Divacky94380162012-08-28 15:09:03 +00005492 CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath("crtendS.o")));
Roman Divackyf6513812012-09-07 13:36:21 +00005493 else
5494 CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath("crtend.o")));
Roman Divacky94380162012-08-28 15:09:03 +00005495 CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath("crtn.o")));
Daniel Dunbar008f54a2009-04-01 19:36:32 +00005496 }
5497
Roman Divacky94380162012-08-28 15:09:03 +00005498 addProfileRT(ToolChain, Args, CmdArgs, ToolChain.getTriple());
Nick Lewycky2e95a6d2011-05-24 21:54:59 +00005499
Daniel Dunbarc21c4852009-04-08 23:54:23 +00005500 const char *Exec =
Roman Divacky94380162012-08-28 15:09:03 +00005501 Args.MakeArgString(ToolChain.GetProgramPath("ld"));
Daniel Dunbar2fe238e2010-08-02 02:38:28 +00005502 C.addCommand(new Command(JA, *this, Exec, CmdArgs));
Daniel Dunbar008f54a2009-04-01 19:36:32 +00005503}
Daniel Dunbar11e1b402009-05-02 18:28:39 +00005504
Benjamin Kramer8e50a962011-02-02 18:59:27 +00005505void netbsd::Assemble::ConstructJob(Compilation &C, const JobAction &JA,
5506 const InputInfo &Output,
5507 const InputInfoList &Inputs,
5508 const ArgList &Args,
5509 const char *LinkingOutput) const {
5510 ArgStringList CmdArgs;
5511
5512 // When building 32-bit code on NetBSD/amd64, we have to explicitly
5513 // instruct as in the base system to assemble 32-bit code.
Joerg Sonnenberger1bd91372012-01-26 22:27:52 +00005514 if (getToolChain().getArch() == llvm::Triple::x86)
Benjamin Kramer8e50a962011-02-02 18:59:27 +00005515 CmdArgs.push_back("--32");
5516
Benjamin Kramer8e50a962011-02-02 18:59:27 +00005517 // Set byte order explicitly
Rafael Espindola64f7ad92012-10-07 04:44:33 +00005518 if (getToolChain().getArch() == llvm::Triple::mips)
Benjamin Kramer8e50a962011-02-02 18:59:27 +00005519 CmdArgs.push_back("-EB");
Rafael Espindola64f7ad92012-10-07 04:44:33 +00005520 else if (getToolChain().getArch() == llvm::Triple::mipsel)
Benjamin Kramer8e50a962011-02-02 18:59:27 +00005521 CmdArgs.push_back("-EL");
5522
5523 Args.AddAllArgValues(CmdArgs, options::OPT_Wa_COMMA,
5524 options::OPT_Xassembler);
5525
5526 CmdArgs.push_back("-o");
5527 CmdArgs.push_back(Output.getFilename());
5528
5529 for (InputInfoList::const_iterator
5530 it = Inputs.begin(), ie = Inputs.end(); it != ie; ++it) {
5531 const InputInfo &II = *it;
5532 CmdArgs.push_back(II.getFilename());
5533 }
5534
David Chisnall5adcec12011-09-27 22:03:18 +00005535 const char *Exec = Args.MakeArgString((getToolChain().GetProgramPath("as")));
Benjamin Kramer8e50a962011-02-02 18:59:27 +00005536 C.addCommand(new Command(JA, *this, Exec, CmdArgs));
5537}
5538
5539void netbsd::Link::ConstructJob(Compilation &C, const JobAction &JA,
5540 const InputInfo &Output,
5541 const InputInfoList &Inputs,
5542 const ArgList &Args,
5543 const char *LinkingOutput) const {
5544 const Driver &D = getToolChain().getDriver();
5545 ArgStringList CmdArgs;
5546
Joerg Sonnenberger8ab2bdc2011-03-21 13:51:29 +00005547 if (!D.SysRoot.empty())
5548 CmdArgs.push_back(Args.MakeArgString("--sysroot=" + D.SysRoot));
5549
Benjamin Kramer8e50a962011-02-02 18:59:27 +00005550 if (Args.hasArg(options::OPT_static)) {
5551 CmdArgs.push_back("-Bstatic");
5552 } else {
5553 if (Args.hasArg(options::OPT_rdynamic))
5554 CmdArgs.push_back("-export-dynamic");
5555 CmdArgs.push_back("--eh-frame-hdr");
5556 if (Args.hasArg(options::OPT_shared)) {
5557 CmdArgs.push_back("-Bshareable");
5558 } else {
5559 CmdArgs.push_back("-dynamic-linker");
5560 CmdArgs.push_back("/libexec/ld.elf_so");
5561 }
5562 }
5563
5564 // When building 32-bit code on NetBSD/amd64, we have to explicitly
5565 // instruct ld in the base system to link 32-bit code.
Joerg Sonnenberger1bd91372012-01-26 22:27:52 +00005566 if (getToolChain().getArch() == llvm::Triple::x86) {
Benjamin Kramer8e50a962011-02-02 18:59:27 +00005567 CmdArgs.push_back("-m");
5568 CmdArgs.push_back("elf_i386");
5569 }
5570
5571 if (Output.isFilename()) {
5572 CmdArgs.push_back("-o");
5573 CmdArgs.push_back(Output.getFilename());
5574 } else {
5575 assert(Output.isNothing() && "Invalid output.");
5576 }
5577
5578 if (!Args.hasArg(options::OPT_nostdlib) &&
5579 !Args.hasArg(options::OPT_nostartfiles)) {
5580 if (!Args.hasArg(options::OPT_shared)) {
5581 CmdArgs.push_back(Args.MakeArgString(
5582 getToolChain().GetFilePath("crt0.o")));
5583 CmdArgs.push_back(Args.MakeArgString(
5584 getToolChain().GetFilePath("crti.o")));
5585 CmdArgs.push_back(Args.MakeArgString(
5586 getToolChain().GetFilePath("crtbegin.o")));
5587 } else {
5588 CmdArgs.push_back(Args.MakeArgString(
5589 getToolChain().GetFilePath("crti.o")));
5590 CmdArgs.push_back(Args.MakeArgString(
5591 getToolChain().GetFilePath("crtbeginS.o")));
5592 }
5593 }
5594
5595 Args.AddAllArgs(CmdArgs, options::OPT_L);
5596 Args.AddAllArgs(CmdArgs, options::OPT_T_Group);
5597 Args.AddAllArgs(CmdArgs, options::OPT_e);
5598 Args.AddAllArgs(CmdArgs, options::OPT_s);
5599 Args.AddAllArgs(CmdArgs, options::OPT_t);
5600 Args.AddAllArgs(CmdArgs, options::OPT_Z_Flag);
5601 Args.AddAllArgs(CmdArgs, options::OPT_r);
5602
5603 AddLinkerInputs(getToolChain(), Inputs, Args, CmdArgs);
5604
5605 if (!Args.hasArg(options::OPT_nostdlib) &&
5606 !Args.hasArg(options::OPT_nodefaultlibs)) {
5607 if (D.CCCIsCXX) {
5608 getToolChain().AddCXXStdlibLibArgs(Args, CmdArgs);
5609 CmdArgs.push_back("-lm");
5610 }
5611 // FIXME: For some reason GCC passes -lgcc and -lgcc_s before adding
5612 // the default system libraries. Just mimic this for now.
Benjamin Kramer8e50a962011-02-02 18:59:27 +00005613 if (Args.hasArg(options::OPT_static)) {
5614 CmdArgs.push_back("-lgcc_eh");
5615 } else {
5616 CmdArgs.push_back("--as-needed");
5617 CmdArgs.push_back("-lgcc_s");
5618 CmdArgs.push_back("--no-as-needed");
5619 }
Joerg Sonnenbergerdb6393f2011-06-07 23:39:17 +00005620 CmdArgs.push_back("-lgcc");
Benjamin Kramer8e50a962011-02-02 18:59:27 +00005621
5622 if (Args.hasArg(options::OPT_pthread))
5623 CmdArgs.push_back("-lpthread");
5624 CmdArgs.push_back("-lc");
5625
5626 CmdArgs.push_back("-lgcc");
5627 if (Args.hasArg(options::OPT_static)) {
5628 CmdArgs.push_back("-lgcc_eh");
5629 } else {
5630 CmdArgs.push_back("--as-needed");
5631 CmdArgs.push_back("-lgcc_s");
5632 CmdArgs.push_back("--no-as-needed");
5633 }
5634 }
5635
5636 if (!Args.hasArg(options::OPT_nostdlib) &&
5637 !Args.hasArg(options::OPT_nostartfiles)) {
5638 if (!Args.hasArg(options::OPT_shared))
5639 CmdArgs.push_back(Args.MakeArgString(getToolChain().GetFilePath(
5640 "crtend.o")));
5641 else
5642 CmdArgs.push_back(Args.MakeArgString(getToolChain().GetFilePath(
5643 "crtendS.o")));
5644 CmdArgs.push_back(Args.MakeArgString(getToolChain().GetFilePath(
5645 "crtn.o")));
5646 }
5647
Bill Wendling3f4be6f2011-06-27 19:15:03 +00005648 addProfileRT(getToolChain(), Args, CmdArgs, getToolChain().getTriple());
Nick Lewycky2e95a6d2011-05-24 21:54:59 +00005649
David Chisnall5adcec12011-09-27 22:03:18 +00005650 const char *Exec = Args.MakeArgString(getToolChain().GetProgramPath("ld"));
Benjamin Kramer8e50a962011-02-02 18:59:27 +00005651 C.addCommand(new Command(JA, *this, Exec, CmdArgs));
5652}
5653
Thomas Schwinge577bb0a2013-03-28 19:04:25 +00005654void gnutools::Assemble::ConstructJob(Compilation &C, const JobAction &JA,
5655 const InputInfo &Output,
5656 const InputInfoList &Inputs,
5657 const ArgList &Args,
5658 const char *LinkingOutput) const {
Rafael Espindolaba30bbe2010-08-10 00:25:48 +00005659 ArgStringList CmdArgs;
5660
5661 // Add --32/--64 to make sure we get the format we want.
5662 // This is incomplete
5663 if (getToolChain().getArch() == llvm::Triple::x86) {
5664 CmdArgs.push_back("--32");
5665 } else if (getToolChain().getArch() == llvm::Triple::x86_64) {
5666 CmdArgs.push_back("--64");
Eli Friedman7972c882011-11-28 23:46:52 +00005667 } else if (getToolChain().getArch() == llvm::Triple::ppc) {
5668 CmdArgs.push_back("-a32");
5669 CmdArgs.push_back("-mppc");
5670 CmdArgs.push_back("-many");
5671 } else if (getToolChain().getArch() == llvm::Triple::ppc64) {
5672 CmdArgs.push_back("-a64");
5673 CmdArgs.push_back("-mppc64");
5674 CmdArgs.push_back("-many");
Rafael Espindolaba30bbe2010-08-10 00:25:48 +00005675 } else if (getToolChain().getArch() == llvm::Triple::arm) {
Chris Lattner5f9e2722011-07-23 10:55:15 +00005676 StringRef MArch = getToolChain().getArchName();
Rafael Espindolaba30bbe2010-08-10 00:25:48 +00005677 if (MArch == "armv7" || MArch == "armv7a" || MArch == "armv7-a")
5678 CmdArgs.push_back("-mfpu=neon");
Evgeniy Stepanov700c5082012-04-20 09:03:40 +00005679
5680 StringRef ARMFloatABI = getARMFloatABI(getToolChain().getDriver(), Args,
5681 getToolChain().getTriple());
5682 CmdArgs.push_back(Args.MakeArgString("-mfloat-abi=" + ARMFloatABI));
Evgeniy Stepanoveca187e2012-04-24 09:05:31 +00005683
5684 Args.AddLastArg(CmdArgs, options::OPT_march_EQ);
5685 Args.AddLastArg(CmdArgs, options::OPT_mcpu_EQ);
5686 Args.AddLastArg(CmdArgs, options::OPT_mfpu_EQ);
Akira Hatanakac85900f2011-11-30 19:31:38 +00005687 } else if (getToolChain().getArch() == llvm::Triple::mips ||
5688 getToolChain().getArch() == llvm::Triple::mipsel ||
5689 getToolChain().getArch() == llvm::Triple::mips64 ||
5690 getToolChain().getArch() == llvm::Triple::mips64el) {
Simon Atanasyan073a7802012-04-07 22:31:29 +00005691 StringRef CPUName;
5692 StringRef ABIName;
5693 getMipsCPUAndABI(Args, getToolChain(), CPUName, ABIName);
Akira Hatanakac85900f2011-11-30 19:31:38 +00005694
Simon Atanasyan073a7802012-04-07 22:31:29 +00005695 CmdArgs.push_back("-march");
5696 CmdArgs.push_back(CPUName.data());
5697
Simon Atanasyan073a7802012-04-07 22:31:29 +00005698 CmdArgs.push_back("-mabi");
Simon Atanasyane9616a42013-02-27 14:55:49 +00005699 CmdArgs.push_back(getGnuCompatibleMipsABIName(ABIName).data());
Simon Atanasyan5f0a1c12012-04-06 19:15:24 +00005700
5701 if (getToolChain().getArch() == llvm::Triple::mips ||
5702 getToolChain().getArch() == llvm::Triple::mips64)
5703 CmdArgs.push_back("-EB");
5704 else
5705 CmdArgs.push_back("-EL");
Simon Atanasyan1f0646e2012-05-29 19:07:33 +00005706
5707 Arg *LastPICArg = Args.getLastArg(options::OPT_fPIC, options::OPT_fno_PIC,
5708 options::OPT_fpic, options::OPT_fno_pic,
5709 options::OPT_fPIE, options::OPT_fno_PIE,
5710 options::OPT_fpie, options::OPT_fno_pie);
5711 if (LastPICArg &&
5712 (LastPICArg->getOption().matches(options::OPT_fPIC) ||
5713 LastPICArg->getOption().matches(options::OPT_fpic) ||
5714 LastPICArg->getOption().matches(options::OPT_fPIE) ||
5715 LastPICArg->getOption().matches(options::OPT_fpie))) {
5716 CmdArgs.push_back("-KPIC");
5717 }
Rafael Espindolaba30bbe2010-08-10 00:25:48 +00005718 }
5719
5720 Args.AddAllArgValues(CmdArgs, options::OPT_Wa_COMMA,
5721 options::OPT_Xassembler);
5722
5723 CmdArgs.push_back("-o");
5724 CmdArgs.push_back(Output.getFilename());
5725
5726 for (InputInfoList::const_iterator
5727 it = Inputs.begin(), ie = Inputs.end(); it != ie; ++it) {
5728 const InputInfo &II = *it;
5729 CmdArgs.push_back(II.getFilename());
5730 }
5731
5732 const char *Exec =
5733 Args.MakeArgString(getToolChain().GetProgramPath("as"));
5734 C.addCommand(new Command(JA, *this, Exec, CmdArgs));
5735}
5736
Evgeniy Stepanova6ddc022012-04-25 08:59:22 +00005737static void AddLibgcc(llvm::Triple Triple, const Driver &D,
5738 ArgStringList &CmdArgs, const ArgList &Args) {
Logan Chien94a71422012-09-02 09:30:11 +00005739 bool isAndroid = Triple.getEnvironment() == llvm::Triple::Android;
Chandler Carruth68f94db2013-03-04 02:07:55 +00005740 bool StaticLibgcc = Args.hasArg(options::OPT_static_libgcc) ||
5741 Args.hasArg(options::OPT_static);
Rafael Espindolaabf3ac72011-10-17 21:39:04 +00005742 if (!D.CCCIsCXX)
5743 CmdArgs.push_back("-lgcc");
5744
Logan Chien529a73d2012-11-19 12:04:11 +00005745 if (StaticLibgcc || isAndroid) {
Rafael Espindolaabf3ac72011-10-17 21:39:04 +00005746 if (D.CCCIsCXX)
5747 CmdArgs.push_back("-lgcc");
5748 } else {
5749 if (!D.CCCIsCXX)
5750 CmdArgs.push_back("--as-needed");
5751 CmdArgs.push_back("-lgcc_s");
5752 if (!D.CCCIsCXX)
5753 CmdArgs.push_back("--no-as-needed");
5754 }
5755
Evgeniy Stepanova6ddc022012-04-25 08:59:22 +00005756 if (StaticLibgcc && !isAndroid)
Rafael Espindolaabf3ac72011-10-17 21:39:04 +00005757 CmdArgs.push_back("-lgcc_eh");
5758 else if (!Args.hasArg(options::OPT_shared) && D.CCCIsCXX)
5759 CmdArgs.push_back("-lgcc");
Logan Chien529a73d2012-11-19 12:04:11 +00005760
5761 // According to Android ABI, we have to link with libdl if we are
5762 // linking with non-static libgcc.
5763 //
5764 // NOTE: This fixes a link error on Android MIPS as well. The non-static
5765 // libgcc for MIPS relies on _Unwind_Find_FDE and dl_iterate_phdr from libdl.
5766 if (isAndroid && !StaticLibgcc)
5767 CmdArgs.push_back("-ldl");
Rafael Espindolaabf3ac72011-10-17 21:39:04 +00005768}
5769
Simon Atanasyanf4bd3292012-10-21 11:44:57 +00005770static bool hasMipsN32ABIArg(const ArgList &Args) {
5771 Arg *A = Args.getLastArg(options::OPT_mabi_EQ);
Richard Smith1d489cf2012-11-01 04:30:05 +00005772 return A && (A->getValue() == StringRef("n32"));
Simon Atanasyanf4bd3292012-10-21 11:44:57 +00005773}
5774
Thomas Schwinge577bb0a2013-03-28 19:04:25 +00005775void gnutools::Link::ConstructJob(Compilation &C, const JobAction &JA,
5776 const InputInfo &Output,
5777 const InputInfoList &Inputs,
5778 const ArgList &Args,
5779 const char *LinkingOutput) const {
Rafael Espindolac1da9812010-11-07 20:14:31 +00005780 const toolchains::Linux& ToolChain =
5781 static_cast<const toolchains::Linux&>(getToolChain());
5782 const Driver &D = ToolChain.getDriver();
Rafael Espindola715852c2012-11-02 20:41:30 +00005783 const bool isAndroid =
5784 ToolChain.getTriple().getEnvironment() == llvm::Triple::Android;
Peter Collingbourne52ca70d2013-04-09 04:35:11 +00005785 SanitizerArgs Sanitize(getToolChain(), Args);
5786 const bool IsPIE =
5787 !Args.hasArg(options::OPT_shared) &&
5788 (Args.hasArg(options::OPT_pie) || Sanitize.hasZeroBaseShadow());
Evgeniy Stepanova6ddc022012-04-25 08:59:22 +00005789
Rafael Espindolac1da9812010-11-07 20:14:31 +00005790 ArgStringList CmdArgs;
5791
Rafael Espindola26f14c32010-11-15 18:28:16 +00005792 // Silence warning for "clang -g foo.o -o foo"
5793 Args.ClaimAllArgs(options::OPT_g_Group);
Rafael Espindola9c094fb2011-03-01 05:25:27 +00005794 // and "clang -emit-llvm foo.o -o foo"
5795 Args.ClaimAllArgs(options::OPT_emit_llvm);
David Chisnalldfa210b2012-07-29 15:24:44 +00005796 // and for "clang -w foo.o -o foo". Other warning options are already
Rafael Espindola7f6458b2010-11-17 20:37:10 +00005797 // handled somewhere else.
5798 Args.ClaimAllArgs(options::OPT_w);
Rafael Espindola26f14c32010-11-15 18:28:16 +00005799
Joerg Sonnenberger8ab2bdc2011-03-21 13:51:29 +00005800 if (!D.SysRoot.empty())
5801 CmdArgs.push_back(Args.MakeArgString("--sysroot=" + D.SysRoot));
Rafael Espindolac1da9812010-11-07 20:14:31 +00005802
Peter Collingbourne52ca70d2013-04-09 04:35:11 +00005803 if (IsPIE)
Rafael Espindolafdda1712010-11-17 22:26:15 +00005804 CmdArgs.push_back("-pie");
5805
Rafael Espindoladc1b76d2010-11-07 22:57:16 +00005806 if (Args.hasArg(options::OPT_rdynamic))
5807 CmdArgs.push_back("-export-dynamic");
5808
Rafael Espindolae0e6d3b2010-11-11 19:34:42 +00005809 if (Args.hasArg(options::OPT_s))
5810 CmdArgs.push_back("-s");
5811
Rafael Espindolac1da9812010-11-07 20:14:31 +00005812 for (std::vector<std::string>::const_iterator i = ToolChain.ExtraOpts.begin(),
5813 e = ToolChain.ExtraOpts.end();
5814 i != e; ++i)
5815 CmdArgs.push_back(i->c_str());
5816
5817 if (!Args.hasArg(options::OPT_static)) {
5818 CmdArgs.push_back("--eh-frame-hdr");
5819 }
5820
5821 CmdArgs.push_back("-m");
5822 if (ToolChain.getArch() == llvm::Triple::x86)
5823 CmdArgs.push_back("elf_i386");
Tim Northoverc264e162013-01-31 12:13:10 +00005824 else if (ToolChain.getArch() == llvm::Triple::aarch64)
5825 CmdArgs.push_back("aarch64linux");
Eric Christopher88b7cf02011-08-19 00:30:14 +00005826 else if (ToolChain.getArch() == llvm::Triple::arm
Douglas Gregorf0594d82011-03-06 19:11:49 +00005827 || ToolChain.getArch() == llvm::Triple::thumb)
Rafael Espindolac1da9812010-11-07 20:14:31 +00005828 CmdArgs.push_back("armelf_linux_eabi");
Ted Kremenek43ac2972011-04-05 22:04:27 +00005829 else if (ToolChain.getArch() == llvm::Triple::ppc)
5830 CmdArgs.push_back("elf32ppclinux");
5831 else if (ToolChain.getArch() == llvm::Triple::ppc64)
5832 CmdArgs.push_back("elf64ppc");
Eli Friedman5bea4f62011-11-08 19:43:37 +00005833 else if (ToolChain.getArch() == llvm::Triple::mips)
5834 CmdArgs.push_back("elf32btsmip");
5835 else if (ToolChain.getArch() == llvm::Triple::mipsel)
5836 CmdArgs.push_back("elf32ltsmip");
Simon Atanasyanf4bd3292012-10-21 11:44:57 +00005837 else if (ToolChain.getArch() == llvm::Triple::mips64) {
5838 if (hasMipsN32ABIArg(Args))
5839 CmdArgs.push_back("elf32btsmipn32");
5840 else
5841 CmdArgs.push_back("elf64btsmip");
5842 }
5843 else if (ToolChain.getArch() == llvm::Triple::mips64el) {
5844 if (hasMipsN32ABIArg(Args))
5845 CmdArgs.push_back("elf32ltsmipn32");
5846 else
5847 CmdArgs.push_back("elf64ltsmip");
5848 }
Rafael Espindolac1da9812010-11-07 20:14:31 +00005849 else
5850 CmdArgs.push_back("elf_x86_64");
5851
5852 if (Args.hasArg(options::OPT_static)) {
Douglas Gregorf0594d82011-03-06 19:11:49 +00005853 if (ToolChain.getArch() == llvm::Triple::arm
5854 || ToolChain.getArch() == llvm::Triple::thumb)
Rafael Espindolac1da9812010-11-07 20:14:31 +00005855 CmdArgs.push_back("-Bstatic");
5856 else
5857 CmdArgs.push_back("-static");
5858 } else if (Args.hasArg(options::OPT_shared)) {
5859 CmdArgs.push_back("-shared");
Rafael Espindola715852c2012-11-02 20:41:30 +00005860 if (isAndroid) {
Evgeniy Stepanova6ddc022012-04-25 08:59:22 +00005861 CmdArgs.push_back("-Bsymbolic");
5862 }
Rafael Espindolac1da9812010-11-07 20:14:31 +00005863 }
5864
5865 if (ToolChain.getArch() == llvm::Triple::arm ||
Douglas Gregorf0594d82011-03-06 19:11:49 +00005866 ToolChain.getArch() == llvm::Triple::thumb ||
Rafael Espindolac1da9812010-11-07 20:14:31 +00005867 (!Args.hasArg(options::OPT_static) &&
5868 !Args.hasArg(options::OPT_shared))) {
5869 CmdArgs.push_back("-dynamic-linker");
Evgeniy Stepanova6ddc022012-04-25 08:59:22 +00005870 if (isAndroid)
5871 CmdArgs.push_back("/system/bin/linker");
5872 else if (ToolChain.getArch() == llvm::Triple::x86)
Rafael Espindolac1da9812010-11-07 20:14:31 +00005873 CmdArgs.push_back("/lib/ld-linux.so.2");
Tim Northoverc264e162013-01-31 12:13:10 +00005874 else if (ToolChain.getArch() == llvm::Triple::aarch64)
5875 CmdArgs.push_back("/lib/ld-linux-aarch64.so.1");
Douglas Gregorf0594d82011-03-06 19:11:49 +00005876 else if (ToolChain.getArch() == llvm::Triple::arm ||
Jiangning Liu6cc9dc82012-07-30 11:05:56 +00005877 ToolChain.getArch() == llvm::Triple::thumb) {
5878 if (ToolChain.getTriple().getEnvironment() == llvm::Triple::GNUEABIHF)
5879 CmdArgs.push_back("/lib/ld-linux-armhf.so.3");
5880 else
5881 CmdArgs.push_back("/lib/ld-linux.so.3");
5882 }
Eli Friedman5bea4f62011-11-08 19:43:37 +00005883 else if (ToolChain.getArch() == llvm::Triple::mips ||
5884 ToolChain.getArch() == llvm::Triple::mipsel)
5885 CmdArgs.push_back("/lib/ld.so.1");
Simon Atanasyan8491cb22012-04-06 20:14:27 +00005886 else if (ToolChain.getArch() == llvm::Triple::mips64 ||
Simon Atanasyanf4bd3292012-10-21 11:44:57 +00005887 ToolChain.getArch() == llvm::Triple::mips64el) {
5888 if (hasMipsN32ABIArg(Args))
5889 CmdArgs.push_back("/lib32/ld.so.1");
5890 else
5891 CmdArgs.push_back("/lib64/ld.so.1");
5892 }
Ted Kremenek43ac2972011-04-05 22:04:27 +00005893 else if (ToolChain.getArch() == llvm::Triple::ppc)
Chris Lattner09f43ed2011-04-11 21:15:37 +00005894 CmdArgs.push_back("/lib/ld.so.1");
Ted Kremenek43ac2972011-04-05 22:04:27 +00005895 else if (ToolChain.getArch() == llvm::Triple::ppc64)
Chris Lattner09f43ed2011-04-11 21:15:37 +00005896 CmdArgs.push_back("/lib64/ld64.so.1");
Rafael Espindolac1da9812010-11-07 20:14:31 +00005897 else
5898 CmdArgs.push_back("/lib64/ld-linux-x86-64.so.2");
5899 }
5900
5901 CmdArgs.push_back("-o");
5902 CmdArgs.push_back(Output.getFilename());
5903
Rafael Espindola49c64fd2010-12-01 01:52:43 +00005904 if (!Args.hasArg(options::OPT_nostdlib) &&
5905 !Args.hasArg(options::OPT_nostartfiles)) {
Evgeniy Stepanova6ddc022012-04-25 08:59:22 +00005906 if (!isAndroid) {
5907 const char *crt1 = NULL;
5908 if (!Args.hasArg(options::OPT_shared)){
Peter Collingbourne52ca70d2013-04-09 04:35:11 +00005909 if (IsPIE)
Evgeniy Stepanova6ddc022012-04-25 08:59:22 +00005910 crt1 = "Scrt1.o";
5911 else
5912 crt1 = "crt1.o";
5913 }
5914 if (crt1)
5915 CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath(crt1)));
Rafael Espindolac1da9812010-11-07 20:14:31 +00005916
Evgeniy Stepanova6ddc022012-04-25 08:59:22 +00005917 CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath("crti.o")));
5918 }
Rafael Espindolac1da9812010-11-07 20:14:31 +00005919
Rafael Espindola89414b32010-11-12 03:00:39 +00005920 const char *crtbegin;
5921 if (Args.hasArg(options::OPT_static))
Evgeniy Stepanova6ddc022012-04-25 08:59:22 +00005922 crtbegin = isAndroid ? "crtbegin_static.o" : "crtbeginT.o";
Evgeniy Stepanova92983d2012-09-10 10:30:12 +00005923 else if (Args.hasArg(options::OPT_shared))
Evgeniy Stepanova6ddc022012-04-25 08:59:22 +00005924 crtbegin = isAndroid ? "crtbegin_so.o" : "crtbeginS.o";
Peter Collingbourne52ca70d2013-04-09 04:35:11 +00005925 else if (IsPIE)
Evgeniy Stepanova92983d2012-09-10 10:30:12 +00005926 crtbegin = isAndroid ? "crtbegin_dynamic.o" : "crtbeginS.o";
Rafael Espindola89414b32010-11-12 03:00:39 +00005927 else
Evgeniy Stepanova6ddc022012-04-25 08:59:22 +00005928 crtbegin = isAndroid ? "crtbegin_dynamic.o" : "crtbegin.o";
Rafael Espindola89414b32010-11-12 03:00:39 +00005929 CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath(crtbegin)));
Benjamin Kramere20e5082012-10-04 19:42:20 +00005930
5931 // Add crtfastmath.o if available and fast math is enabled.
5932 ToolChain.AddFastMathRuntimeIfAvailable(Args, CmdArgs);
Rafael Espindola89414b32010-11-12 03:00:39 +00005933 }
Rafael Espindolac1da9812010-11-07 20:14:31 +00005934
5935 Args.AddAllArgs(CmdArgs, options::OPT_L);
5936
5937 const ToolChain::path_list Paths = ToolChain.getFilePaths();
5938
Roman Divacky58e5ac92011-03-01 17:53:14 +00005939 for (ToolChain::path_list::const_iterator i = Paths.begin(), e = Paths.end();
5940 i != e; ++i)
Chris Lattner5f9e2722011-07-23 10:55:15 +00005941 CmdArgs.push_back(Args.MakeArgString(StringRef("-L") + *i));
Rafael Espindolac1da9812010-11-07 20:14:31 +00005942
Rafael Espindolac5151542012-04-09 23:53:34 +00005943 // Tell the linker to load the plugin. This has to come before AddLinkerInputs
5944 // as gold requires -plugin to come before any -plugin-opt that -Wl might
5945 // forward.
5946 if (D.IsUsingLTO(Args) || Args.hasArg(options::OPT_use_gold_plugin)) {
5947 CmdArgs.push_back("-plugin");
5948 std::string Plugin = ToolChain.getDriver().Dir + "/../lib/LLVMgold.so";
5949 CmdArgs.push_back(Args.MakeArgString(Plugin));
Chandler Carruth700d4e42013-01-13 11:46:33 +00005950
5951 // Try to pass driver level flags relevant to LTO code generation down to
5952 // the plugin.
5953
5954 // Handle architecture-specific flags for selecting CPU variants.
5955 if (ToolChain.getArch() == llvm::Triple::x86 ||
5956 ToolChain.getArch() == llvm::Triple::x86_64)
5957 CmdArgs.push_back(
5958 Args.MakeArgString(Twine("-plugin-opt=mcpu=") +
5959 getX86TargetCPU(Args, ToolChain.getTriple())));
5960 else if (ToolChain.getArch() == llvm::Triple::arm ||
5961 ToolChain.getArch() == llvm::Triple::thumb)
5962 CmdArgs.push_back(
5963 Args.MakeArgString(Twine("-plugin-opt=mcpu=") +
5964 getARMTargetCPU(Args, ToolChain.getTriple())));
5965
5966 // FIXME: Factor out logic for MIPS, PPC, and other targets to support this
5967 // as well.
Rafael Espindolac5151542012-04-09 23:53:34 +00005968 }
5969
Chandler Carruth700d4e42013-01-13 11:46:33 +00005970
Nick Lewyckye276cfc2012-08-17 03:39:16 +00005971 if (Args.hasArg(options::OPT_Z_Xlinker__no_demangle))
5972 CmdArgs.push_back("--no-demangle");
5973
Rafael Espindolac1da9812010-11-07 20:14:31 +00005974 AddLinkerInputs(ToolChain, Inputs, Args, CmdArgs);
5975
Eric Christopher6716d942012-11-29 18:51:05 +00005976 // Call these before we add the C++ ABI library.
Richard Smithc4dabad2012-11-05 22:04:41 +00005977 if (Sanitize.needsUbsanRt())
Richard Smith61a574f2013-03-20 23:49:07 +00005978 addUbsanRTLinux(getToolChain(), Args, CmdArgs, D.CCCIsCXX,
5979 Sanitize.needsAsanRt() || Sanitize.needsTsanRt() ||
5980 Sanitize.needsMsanRt());
Eric Christopher6716d942012-11-29 18:51:05 +00005981 if (Sanitize.needsAsanRt())
5982 addAsanRTLinux(getToolChain(), Args, CmdArgs);
5983 if (Sanitize.needsTsanRt())
5984 addTsanRTLinux(getToolChain(), Args, CmdArgs);
Evgeniy Stepanov09ccf392012-12-03 13:20:43 +00005985 if (Sanitize.needsMsanRt())
5986 addMsanRTLinux(getToolChain(), Args, CmdArgs);
Richard Smith8e1cee62012-10-25 02:14:12 +00005987
Chandler Carruth2ba542c2012-05-14 18:31:18 +00005988 if (D.CCCIsCXX &&
5989 !Args.hasArg(options::OPT_nostdlib) &&
5990 !Args.hasArg(options::OPT_nodefaultlibs)) {
Rafael Espindola19706f82011-10-17 22:14:51 +00005991 bool OnlyLibstdcxxStatic = Args.hasArg(options::OPT_static_libstdcxx) &&
5992 !Args.hasArg(options::OPT_static);
5993 if (OnlyLibstdcxxStatic)
5994 CmdArgs.push_back("-Bstatic");
Rafael Espindolac1da9812010-11-07 20:14:31 +00005995 ToolChain.AddCXXStdlibLibArgs(Args, CmdArgs);
Rafael Espindola19706f82011-10-17 22:14:51 +00005996 if (OnlyLibstdcxxStatic)
5997 CmdArgs.push_back("-Bdynamic");
Rafael Espindolac1da9812010-11-07 20:14:31 +00005998 CmdArgs.push_back("-lm");
5999 }
6000
Rafael Espindola89414b32010-11-12 03:00:39 +00006001 if (!Args.hasArg(options::OPT_nostdlib)) {
Chandler Carruth2ba542c2012-05-14 18:31:18 +00006002 if (!Args.hasArg(options::OPT_nodefaultlibs)) {
6003 if (Args.hasArg(options::OPT_static))
6004 CmdArgs.push_back("--start-group");
Nick Lewycky80df0252011-06-04 06:27:06 +00006005
Chandler Carruthdf96e022013-01-17 13:19:29 +00006006 bool OpenMP = Args.hasArg(options::OPT_fopenmp);
6007 if (OpenMP) {
6008 CmdArgs.push_back("-lgomp");
6009
6010 // FIXME: Exclude this for platforms whith libgomp that doesn't require
6011 // librt. Most modern Linux platfroms require it, but some may not.
6012 CmdArgs.push_back("-lrt");
6013 }
6014
Evgeniy Stepanova6ddc022012-04-25 08:59:22 +00006015 AddLibgcc(ToolChain.getTriple(), D, CmdArgs, Args);
Rafael Espindola89414b32010-11-12 03:00:39 +00006016
Chandler Carruth2ba542c2012-05-14 18:31:18 +00006017 if (Args.hasArg(options::OPT_pthread) ||
Chandler Carruthdf96e022013-01-17 13:19:29 +00006018 Args.hasArg(options::OPT_pthreads) || OpenMP)
Chandler Carruth2ba542c2012-05-14 18:31:18 +00006019 CmdArgs.push_back("-lpthread");
6020
6021 CmdArgs.push_back("-lc");
6022
6023 if (Args.hasArg(options::OPT_static))
6024 CmdArgs.push_back("--end-group");
6025 else
6026 AddLibgcc(ToolChain.getTriple(), D, CmdArgs, Args);
6027 }
Rafael Espindolafdda1712010-11-17 22:26:15 +00006028
Rafael Espindola49c64fd2010-12-01 01:52:43 +00006029 if (!Args.hasArg(options::OPT_nostartfiles)) {
6030 const char *crtend;
Evgeniy Stepanova92983d2012-09-10 10:30:12 +00006031 if (Args.hasArg(options::OPT_shared))
Evgeniy Stepanova6ddc022012-04-25 08:59:22 +00006032 crtend = isAndroid ? "crtend_so.o" : "crtendS.o";
Peter Collingbourne52ca70d2013-04-09 04:35:11 +00006033 else if (IsPIE)
Evgeniy Stepanova92983d2012-09-10 10:30:12 +00006034 crtend = isAndroid ? "crtend_android.o" : "crtendS.o";
Rafael Espindola49c64fd2010-12-01 01:52:43 +00006035 else
Evgeniy Stepanova6ddc022012-04-25 08:59:22 +00006036 crtend = isAndroid ? "crtend_android.o" : "crtend.o";
Rafael Espindola89414b32010-11-12 03:00:39 +00006037
Rafael Espindola49c64fd2010-12-01 01:52:43 +00006038 CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath(crtend)));
Evgeniy Stepanova6ddc022012-04-25 08:59:22 +00006039 if (!isAndroid)
6040 CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath("crtn.o")));
Rafael Espindola49c64fd2010-12-01 01:52:43 +00006041 }
Rafael Espindolac1da9812010-11-07 20:14:31 +00006042 }
6043
Bill Wendling3f4be6f2011-06-27 19:15:03 +00006044 addProfileRT(getToolChain(), Args, CmdArgs, getToolChain().getTriple());
Nick Lewycky2e95a6d2011-05-24 21:54:59 +00006045
Rafael Espindolac1da9812010-11-07 20:14:31 +00006046 C.addCommand(new Command(JA, *this, ToolChain.Linker.c_str(), CmdArgs));
6047}
Rafael Espindolaba30bbe2010-08-10 00:25:48 +00006048
Chris Lattner38e317d2010-07-07 16:01:42 +00006049void minix::Assemble::ConstructJob(Compilation &C, const JobAction &JA,
Daniel Dunbar2fe238e2010-08-02 02:38:28 +00006050 const InputInfo &Output,
6051 const InputInfoList &Inputs,
6052 const ArgList &Args,
6053 const char *LinkingOutput) const {
Chris Lattner38e317d2010-07-07 16:01:42 +00006054 ArgStringList CmdArgs;
6055
6056 Args.AddAllArgValues(CmdArgs, options::OPT_Wa_COMMA,
6057 options::OPT_Xassembler);
6058
6059 CmdArgs.push_back("-o");
Daniel Dunbar7c1e4652010-08-02 02:38:21 +00006060 CmdArgs.push_back(Output.getFilename());
Chris Lattner38e317d2010-07-07 16:01:42 +00006061
6062 for (InputInfoList::const_iterator
6063 it = Inputs.begin(), ie = Inputs.end(); it != ie; ++it) {
6064 const InputInfo &II = *it;
Daniel Dunbar7c1e4652010-08-02 02:38:21 +00006065 CmdArgs.push_back(II.getFilename());
Chris Lattner38e317d2010-07-07 16:01:42 +00006066 }
6067
6068 const char *Exec =
Eli Friedman6d402dc2011-12-08 23:54:21 +00006069 Args.MakeArgString(getToolChain().GetProgramPath("as"));
Daniel Dunbar2fe238e2010-08-02 02:38:28 +00006070 C.addCommand(new Command(JA, *this, Exec, CmdArgs));
Chris Lattner38e317d2010-07-07 16:01:42 +00006071}
6072
6073void minix::Link::ConstructJob(Compilation &C, const JobAction &JA,
Daniel Dunbar2fe238e2010-08-02 02:38:28 +00006074 const InputInfo &Output,
6075 const InputInfoList &Inputs,
6076 const ArgList &Args,
6077 const char *LinkingOutput) const {
Chris Lattner38e317d2010-07-07 16:01:42 +00006078 const Driver &D = getToolChain().getDriver();
6079 ArgStringList CmdArgs;
6080
Daniel Dunbar7c1e4652010-08-02 02:38:21 +00006081 if (Output.isFilename()) {
Chris Lattner38e317d2010-07-07 16:01:42 +00006082 CmdArgs.push_back("-o");
6083 CmdArgs.push_back(Output.getFilename());
6084 } else {
6085 assert(Output.isNothing() && "Invalid output.");
6086 }
6087
6088 if (!Args.hasArg(options::OPT_nostdlib) &&
Eli Friedman6d402dc2011-12-08 23:54:21 +00006089 !Args.hasArg(options::OPT_nostartfiles)) {
6090 CmdArgs.push_back(Args.MakeArgString(getToolChain().GetFilePath("crt1.o")));
6091 CmdArgs.push_back(Args.MakeArgString(getToolChain().GetFilePath("crti.o")));
6092 CmdArgs.push_back(Args.MakeArgString(getToolChain().GetFilePath("crtbegin.o")));
6093 CmdArgs.push_back(Args.MakeArgString(getToolChain().GetFilePath("crtn.o")));
6094 }
Chris Lattner38e317d2010-07-07 16:01:42 +00006095
6096 Args.AddAllArgs(CmdArgs, options::OPT_L);
6097 Args.AddAllArgs(CmdArgs, options::OPT_T_Group);
6098 Args.AddAllArgs(CmdArgs, options::OPT_e);
6099
Daniel Dunbar2008fee2010-09-17 00:24:54 +00006100 AddLinkerInputs(getToolChain(), Inputs, Args, CmdArgs);
Chris Lattner38e317d2010-07-07 16:01:42 +00006101
Eli Friedman6d402dc2011-12-08 23:54:21 +00006102 addProfileRT(getToolChain(), Args, CmdArgs, getToolChain().getTriple());
6103
Chris Lattner38e317d2010-07-07 16:01:42 +00006104 if (!Args.hasArg(options::OPT_nostdlib) &&
6105 !Args.hasArg(options::OPT_nodefaultlibs)) {
6106 if (D.CCCIsCXX) {
Daniel Dunbar132e35d2010-09-17 01:20:05 +00006107 getToolChain().AddCXXStdlibLibArgs(Args, CmdArgs);
Chris Lattner38e317d2010-07-07 16:01:42 +00006108 CmdArgs.push_back("-lm");
6109 }
Chris Lattner38e317d2010-07-07 16:01:42 +00006110 }
6111
6112 if (!Args.hasArg(options::OPT_nostdlib) &&
6113 !Args.hasArg(options::OPT_nostartfiles)) {
Eli Friedman6d402dc2011-12-08 23:54:21 +00006114 if (Args.hasArg(options::OPT_pthread))
6115 CmdArgs.push_back("-lpthread");
6116 CmdArgs.push_back("-lc");
6117 CmdArgs.push_back("-lCompilerRT-Generic");
6118 CmdArgs.push_back("-L/usr/pkg/compiler-rt/lib");
6119 CmdArgs.push_back(
Eric Christopher27e2b982012-12-18 00:31:10 +00006120 Args.MakeArgString(getToolChain().GetFilePath("crtend.o")));
Chris Lattner38e317d2010-07-07 16:01:42 +00006121 }
6122
Eli Friedman6d402dc2011-12-08 23:54:21 +00006123 const char *Exec = Args.MakeArgString(getToolChain().GetProgramPath("ld"));
Daniel Dunbar2fe238e2010-08-02 02:38:28 +00006124 C.addCommand(new Command(JA, *this, Exec, CmdArgs));
Chris Lattner38e317d2010-07-07 16:01:42 +00006125}
6126
Daniel Dunbar11e1b402009-05-02 18:28:39 +00006127/// DragonFly Tools
6128
6129// For now, DragonFly Assemble does just about the same as for
6130// FreeBSD, but this may change soon.
6131void dragonfly::Assemble::ConstructJob(Compilation &C, const JobAction &JA,
Daniel Dunbar2fe238e2010-08-02 02:38:28 +00006132 const InputInfo &Output,
Daniel Dunbar294691e2009-11-04 06:24:38 +00006133 const InputInfoList &Inputs,
6134 const ArgList &Args,
6135 const char *LinkingOutput) const {
Daniel Dunbar11e1b402009-05-02 18:28:39 +00006136 ArgStringList CmdArgs;
6137
6138 // When building 32-bit code on DragonFly/pc64, we have to explicitly
6139 // instruct as in the base system to assemble 32-bit code.
Rafael Espindola64f7ad92012-10-07 04:44:33 +00006140 if (getToolChain().getArch() == llvm::Triple::x86)
Daniel Dunbar11e1b402009-05-02 18:28:39 +00006141 CmdArgs.push_back("--32");
6142
6143 Args.AddAllArgValues(CmdArgs, options::OPT_Wa_COMMA,
6144 options::OPT_Xassembler);
6145
6146 CmdArgs.push_back("-o");
Daniel Dunbar7c1e4652010-08-02 02:38:21 +00006147 CmdArgs.push_back(Output.getFilename());
Daniel Dunbar11e1b402009-05-02 18:28:39 +00006148
6149 for (InputInfoList::const_iterator
6150 it = Inputs.begin(), ie = Inputs.end(); it != ie; ++it) {
6151 const InputInfo &II = *it;
Daniel Dunbar7c1e4652010-08-02 02:38:21 +00006152 CmdArgs.push_back(II.getFilename());
Daniel Dunbar11e1b402009-05-02 18:28:39 +00006153 }
6154
6155 const char *Exec =
Daniel Dunbar4a7e8892010-07-14 18:46:23 +00006156 Args.MakeArgString(getToolChain().GetProgramPath("as"));
Daniel Dunbar2fe238e2010-08-02 02:38:28 +00006157 C.addCommand(new Command(JA, *this, Exec, CmdArgs));
Daniel Dunbar11e1b402009-05-02 18:28:39 +00006158}
6159
6160void dragonfly::Link::ConstructJob(Compilation &C, const JobAction &JA,
Daniel Dunbar2fe238e2010-08-02 02:38:28 +00006161 const InputInfo &Output,
6162 const InputInfoList &Inputs,
6163 const ArgList &Args,
6164 const char *LinkingOutput) const {
Daniel Dunbaree788e72009-12-21 18:54:17 +00006165 const Driver &D = getToolChain().getDriver();
Daniel Dunbar11e1b402009-05-02 18:28:39 +00006166 ArgStringList CmdArgs;
6167
Joerg Sonnenberger8ab2bdc2011-03-21 13:51:29 +00006168 if (!D.SysRoot.empty())
6169 CmdArgs.push_back(Args.MakeArgString("--sysroot=" + D.SysRoot));
6170
Daniel Dunbar11e1b402009-05-02 18:28:39 +00006171 if (Args.hasArg(options::OPT_static)) {
6172 CmdArgs.push_back("-Bstatic");
6173 } else {
6174 if (Args.hasArg(options::OPT_shared))
6175 CmdArgs.push_back("-Bshareable");
6176 else {
6177 CmdArgs.push_back("-dynamic-linker");
6178 CmdArgs.push_back("/usr/libexec/ld-elf.so.2");
6179 }
6180 }
6181
6182 // When building 32-bit code on DragonFly/pc64, we have to explicitly
6183 // instruct ld in the base system to link 32-bit code.
Rafael Espindola64f7ad92012-10-07 04:44:33 +00006184 if (getToolChain().getArch() == llvm::Triple::x86) {
Daniel Dunbar11e1b402009-05-02 18:28:39 +00006185 CmdArgs.push_back("-m");
6186 CmdArgs.push_back("elf_i386");
6187 }
6188
Daniel Dunbar7c1e4652010-08-02 02:38:21 +00006189 if (Output.isFilename()) {
Daniel Dunbar11e1b402009-05-02 18:28:39 +00006190 CmdArgs.push_back("-o");
6191 CmdArgs.push_back(Output.getFilename());
6192 } else {
6193 assert(Output.isNothing() && "Invalid output.");
6194 }
6195
6196 if (!Args.hasArg(options::OPT_nostdlib) &&
6197 !Args.hasArg(options::OPT_nostartfiles)) {
6198 if (!Args.hasArg(options::OPT_shared)) {
Chris Lattner38e317d2010-07-07 16:01:42 +00006199 CmdArgs.push_back(
Daniel Dunbar4a7e8892010-07-14 18:46:23 +00006200 Args.MakeArgString(getToolChain().GetFilePath("crt1.o")));
Chris Lattner38e317d2010-07-07 16:01:42 +00006201 CmdArgs.push_back(
Daniel Dunbar4a7e8892010-07-14 18:46:23 +00006202 Args.MakeArgString(getToolChain().GetFilePath("crti.o")));
Chris Lattner38e317d2010-07-07 16:01:42 +00006203 CmdArgs.push_back(
Daniel Dunbar4a7e8892010-07-14 18:46:23 +00006204 Args.MakeArgString(getToolChain().GetFilePath("crtbegin.o")));
Daniel Dunbar11e1b402009-05-02 18:28:39 +00006205 } else {
Chris Lattner38e317d2010-07-07 16:01:42 +00006206 CmdArgs.push_back(
Daniel Dunbar4a7e8892010-07-14 18:46:23 +00006207 Args.MakeArgString(getToolChain().GetFilePath("crti.o")));
Chris Lattner38e317d2010-07-07 16:01:42 +00006208 CmdArgs.push_back(
Daniel Dunbar4a7e8892010-07-14 18:46:23 +00006209 Args.MakeArgString(getToolChain().GetFilePath("crtbeginS.o")));
Daniel Dunbar11e1b402009-05-02 18:28:39 +00006210 }
6211 }
6212
6213 Args.AddAllArgs(CmdArgs, options::OPT_L);
6214 Args.AddAllArgs(CmdArgs, options::OPT_T_Group);
6215 Args.AddAllArgs(CmdArgs, options::OPT_e);
6216
Daniel Dunbar2008fee2010-09-17 00:24:54 +00006217 AddLinkerInputs(getToolChain(), Inputs, Args, CmdArgs);
Daniel Dunbar11e1b402009-05-02 18:28:39 +00006218
6219 if (!Args.hasArg(options::OPT_nostdlib) &&
6220 !Args.hasArg(options::OPT_nodefaultlibs)) {
6221 // FIXME: GCC passes on -lgcc, -lgcc_pic and a whole lot of
6222 // rpaths
6223 CmdArgs.push_back("-L/usr/lib/gcc41");
6224
6225 if (!Args.hasArg(options::OPT_static)) {
6226 CmdArgs.push_back("-rpath");
6227 CmdArgs.push_back("/usr/lib/gcc41");
6228
6229 CmdArgs.push_back("-rpath-link");
6230 CmdArgs.push_back("/usr/lib/gcc41");
6231
6232 CmdArgs.push_back("-rpath");
6233 CmdArgs.push_back("/usr/lib");
6234
6235 CmdArgs.push_back("-rpath-link");
6236 CmdArgs.push_back("/usr/lib");
6237 }
6238
Rafael Espindola405861d2010-07-20 12:59:03 +00006239 if (D.CCCIsCXX) {
Daniel Dunbar132e35d2010-09-17 01:20:05 +00006240 getToolChain().AddCXXStdlibLibArgs(Args, CmdArgs);
Rafael Espindola405861d2010-07-20 12:59:03 +00006241 CmdArgs.push_back("-lm");
6242 }
6243
Daniel Dunbar11e1b402009-05-02 18:28:39 +00006244 if (Args.hasArg(options::OPT_shared)) {
6245 CmdArgs.push_back("-lgcc_pic");
6246 } else {
6247 CmdArgs.push_back("-lgcc");
6248 }
6249
6250
6251 if (Args.hasArg(options::OPT_pthread))
Mike Stump4d63f8b2009-10-31 20:11:46 +00006252 CmdArgs.push_back("-lpthread");
Daniel Dunbar11e1b402009-05-02 18:28:39 +00006253
6254 if (!Args.hasArg(options::OPT_nolibc)) {
6255 CmdArgs.push_back("-lc");
6256 }
6257
6258 if (Args.hasArg(options::OPT_shared)) {
6259 CmdArgs.push_back("-lgcc_pic");
6260 } else {
6261 CmdArgs.push_back("-lgcc");
6262 }
6263 }
6264
6265 if (!Args.hasArg(options::OPT_nostdlib) &&
6266 !Args.hasArg(options::OPT_nostartfiles)) {
6267 if (!Args.hasArg(options::OPT_shared))
Chris Lattner38e317d2010-07-07 16:01:42 +00006268 CmdArgs.push_back(Args.MakeArgString(
Daniel Dunbar4a7e8892010-07-14 18:46:23 +00006269 getToolChain().GetFilePath("crtend.o")));
Daniel Dunbar11e1b402009-05-02 18:28:39 +00006270 else
Chris Lattner38e317d2010-07-07 16:01:42 +00006271 CmdArgs.push_back(Args.MakeArgString(
Daniel Dunbar4a7e8892010-07-14 18:46:23 +00006272 getToolChain().GetFilePath("crtendS.o")));
Chris Lattner38e317d2010-07-07 16:01:42 +00006273 CmdArgs.push_back(Args.MakeArgString(
Daniel Dunbar4a7e8892010-07-14 18:46:23 +00006274 getToolChain().GetFilePath("crtn.o")));
Daniel Dunbar11e1b402009-05-02 18:28:39 +00006275 }
6276
Bill Wendling3f4be6f2011-06-27 19:15:03 +00006277 addProfileRT(getToolChain(), Args, CmdArgs, getToolChain().getTriple());
Nick Lewycky2e95a6d2011-05-24 21:54:59 +00006278
Daniel Dunbar11e1b402009-05-02 18:28:39 +00006279 const char *Exec =
Daniel Dunbar4a7e8892010-07-14 18:46:23 +00006280 Args.MakeArgString(getToolChain().GetProgramPath("ld"));
Daniel Dunbar2fe238e2010-08-02 02:38:28 +00006281 C.addCommand(new Command(JA, *this, Exec, CmdArgs));
Daniel Dunbar11e1b402009-05-02 18:28:39 +00006282}
Michael J. Spencerff58e362010-08-21 21:55:07 +00006283
6284void visualstudio::Link::ConstructJob(Compilation &C, const JobAction &JA,
6285 const InputInfo &Output,
6286 const InputInfoList &Inputs,
6287 const ArgList &Args,
6288 const char *LinkingOutput) const {
Michael J. Spencerff58e362010-08-21 21:55:07 +00006289 ArgStringList CmdArgs;
6290
6291 if (Output.isFilename()) {
Daniel Dunbare5a37f42010-09-17 00:45:02 +00006292 CmdArgs.push_back(Args.MakeArgString(std::string("-out:") +
6293 Output.getFilename()));
Michael J. Spencerff58e362010-08-21 21:55:07 +00006294 } else {
6295 assert(Output.isNothing() && "Invalid output.");
6296 }
6297
6298 if (!Args.hasArg(options::OPT_nostdlib) &&
6299 !Args.hasArg(options::OPT_nostartfiles)) {
6300 CmdArgs.push_back("-defaultlib:libcmt");
6301 }
6302
6303 CmdArgs.push_back("-nologo");
6304
Michael J. Spencera2284f52012-06-18 16:56:04 +00006305 Args.AddAllArgValues(CmdArgs, options::OPT_l);
6306
6307 // Add filenames immediately.
6308 for (InputInfoList::const_iterator
6309 it = Inputs.begin(), ie = Inputs.end(); it != ie; ++it) {
6310 if (it->isFilename())
6311 CmdArgs.push_back(it->getFilename());
6312 }
Michael J. Spencerff58e362010-08-21 21:55:07 +00006313
6314 const char *Exec =
Daniel Dunbar2008fee2010-09-17 00:24:54 +00006315 Args.MakeArgString(getToolChain().GetProgramPath("link.exe"));
Michael J. Spencerff58e362010-08-21 21:55:07 +00006316 C.addCommand(new Command(JA, *this, Exec, CmdArgs));
6317}