blob: 973df104d52efb07f8558912c459d0809ec01ea2 [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"
11
Daniel Dunbar1d460332009-03-18 10:01:51 +000012#include "clang/Driver/Action.h"
Daniel Dunbar871adcf2009-03-18 07:06:02 +000013#include "clang/Driver/Arg.h"
Daniel Dunbarb488c1d2009-03-18 08:07:30 +000014#include "clang/Driver/ArgList.h"
Daniel Dunbaree848a72009-10-29 02:39:57 +000015#include "clang/Driver/Driver.h"
16#include "clang/Driver/DriverDiagnostic.h"
Daniel Dunbar871adcf2009-03-18 07:06:02 +000017#include "clang/Driver/Compilation.h"
18#include "clang/Driver/Job.h"
Daniel Dunbarb488c1d2009-03-18 08:07:30 +000019#include "clang/Driver/Option.h"
Daniel Dunbar265e9ef2009-11-19 04:25:22 +000020#include "clang/Driver/Options.h"
Daniel Dunbarb488c1d2009-03-18 08:07:30 +000021#include "clang/Driver/ToolChain.h"
Daniel Dunbar871adcf2009-03-18 07:06:02 +000022#include "clang/Driver/Util.h"
John McCall260611a2012-06-20 06:18:46 +000023#include "clang/Basic/ObjCRuntime.h"
Daniel Dunbar871adcf2009-03-18 07:06:02 +000024
Daniel Dunbar88137642009-09-09 22:32:48 +000025#include "llvm/ADT/SmallString.h"
Douglas Gregor55d3f7a2009-10-29 00:41:01 +000026#include "llvm/ADT/StringSwitch.h"
Daniel Dunbar5b750fe2009-09-09 22:32:34 +000027#include "llvm/ADT/Twine.h"
Michael J. Spencer32bef4e2011-01-10 02:34:13 +000028#include "llvm/Support/FileSystem.h"
Daniel Dunbar02633b52009-03-26 16:23:12 +000029#include "llvm/Support/Format.h"
30#include "llvm/Support/raw_ostream.h"
Michael J. Spencer03013fa2010-11-29 18:12:39 +000031#include "llvm/Support/Host.h"
32#include "llvm/Support/Process.h"
John McCallf85e1932011-06-15 23:02:42 +000033#include "llvm/Support/ErrorHandling.h"
Daniel Dunbar871adcf2009-03-18 07:06:02 +000034
35#include "InputInfo.h"
Daniel Dunbar02633b52009-03-26 16:23:12 +000036#include "ToolChains.h"
Daniel Dunbar871adcf2009-03-18 07:06:02 +000037
Daniel Dunbar47ac7d22009-03-18 06:00:36 +000038using namespace clang::driver;
39using namespace clang::driver::tools;
Chris Lattner5f9e2722011-07-23 10:55:15 +000040using namespace clang;
Daniel Dunbar47ac7d22009-03-18 06:00:36 +000041
Daniel Dunbar88a3d6c2009-09-10 01:21:05 +000042/// CheckPreprocessingOptions - Perform some validation of preprocessing
43/// arguments that is shared with gcc.
44static void CheckPreprocessingOptions(const Driver &D, const ArgList &Args) {
45 if (Arg *A = Args.getLastArg(options::OPT_C, options::OPT_CC))
Joerg Sonnenberger9ade4ae2011-03-06 23:31:01 +000046 if (!Args.hasArg(options::OPT_E) && !D.CCCIsCPP)
Chris Lattner5f9e2722011-07-23 10:55:15 +000047 D.Diag(diag::err_drv_argument_only_allowed_with)
Daniel Dunbar88a3d6c2009-09-10 01:21:05 +000048 << A->getAsString(Args) << "-E";
49}
50
Daniel Dunbare2fd6642009-09-10 01:21:12 +000051/// CheckCodeGenerationOptions - Perform some validation of code generation
52/// arguments that is shared with gcc.
53static void CheckCodeGenerationOptions(const Driver &D, const ArgList &Args) {
54 // In gcc, only ARM checks this, but it seems reasonable to check universally.
55 if (Args.hasArg(options::OPT_static))
56 if (const Arg *A = Args.getLastArg(options::OPT_dynamic,
57 options::OPT_mdynamic_no_pic))
Chris Lattner5f9e2722011-07-23 10:55:15 +000058 D.Diag(diag::err_drv_argument_not_allowed_with)
Daniel Dunbare2fd6642009-09-10 01:21:12 +000059 << A->getAsString(Args) << "-static";
60}
61
Chris Lattner3edbeb72010-03-29 17:55:58 +000062// Quote target names for inclusion in GNU Make dependency files.
63// Only the characters '$', '#', ' ', '\t' are quoted.
Chris Lattner5f9e2722011-07-23 10:55:15 +000064static void QuoteTarget(StringRef Target,
65 SmallVectorImpl<char> &Res) {
Chris Lattner3edbeb72010-03-29 17:55:58 +000066 for (unsigned i = 0, e = Target.size(); i != e; ++i) {
67 switch (Target[i]) {
68 case ' ':
69 case '\t':
70 // Escape the preceding backslashes
71 for (int j = i - 1; j >= 0 && Target[j] == '\\'; --j)
72 Res.push_back('\\');
73
74 // Escape the space/tab
75 Res.push_back('\\');
76 break;
77 case '$':
78 Res.push_back('$');
79 break;
80 case '#':
81 Res.push_back('\\');
82 break;
83 default:
84 break;
85 }
86
87 Res.push_back(Target[i]);
88 }
89}
90
Bill Wendling3d717152012-03-12 22:10:06 +000091static void addDirectoryList(const ArgList &Args,
Bill Wendlingbdb8f3c2012-03-12 21:22:35 +000092 ArgStringList &CmdArgs,
93 const char *ArgName,
Bill Wendling3d717152012-03-12 22:10:06 +000094 const char *EnvVar) {
95 const char *DirList = ::getenv(EnvVar);
Chad Rosier89aa2ce2012-10-30 21:42:09 +000096 bool CombinedArg = false;
97
Bill Wendlingbdb8f3c2012-03-12 21:22:35 +000098 if (!DirList)
99 return; // Nothing to do.
100
Chad Rosier89aa2ce2012-10-30 21:42:09 +0000101 StringRef Name(ArgName);
102 if (Name.equals("-I") || Name.equals("-L"))
103 CombinedArg = true;
104
Bill Wendlingbdb8f3c2012-03-12 21:22:35 +0000105 StringRef Dirs(DirList);
106 if (Dirs.empty()) // Empty string should not add '.'.
107 return;
108
109 StringRef::size_type Delim;
110 while ((Delim = Dirs.find(llvm::sys::PathSeparator)) != StringRef::npos) {
111 if (Delim == 0) { // Leading colon.
Chad Rosier89aa2ce2012-10-30 21:42:09 +0000112 if (CombinedArg) {
113 CmdArgs.push_back(Args.MakeArgString(std::string(ArgName) + "."));
114 } else {
115 CmdArgs.push_back(ArgName);
116 CmdArgs.push_back(".");
117 }
Bill Wendlingbdb8f3c2012-03-12 21:22:35 +0000118 } else {
Chad Rosier89aa2ce2012-10-30 21:42:09 +0000119 if (CombinedArg) {
120 CmdArgs.push_back(Args.MakeArgString(std::string(ArgName) + Dirs.substr(0, Delim)));
121 } else {
122 CmdArgs.push_back(ArgName);
123 CmdArgs.push_back(Args.MakeArgString(Dirs.substr(0, Delim)));
124 }
Bill Wendlingbdb8f3c2012-03-12 21:22:35 +0000125 }
Nico Weber09c5c392012-03-19 15:00:03 +0000126 Dirs = Dirs.substr(Delim + 1);
Bill Wendlingbdb8f3c2012-03-12 21:22:35 +0000127 }
128
129 if (Dirs.empty()) { // Trailing colon.
Chad Rosier89aa2ce2012-10-30 21:42:09 +0000130 if (CombinedArg) {
131 CmdArgs.push_back(Args.MakeArgString(std::string(ArgName) + "."));
132 } else {
133 CmdArgs.push_back(ArgName);
134 CmdArgs.push_back(".");
135 }
Bill Wendlingbdb8f3c2012-03-12 21:22:35 +0000136 } else { // Add the last path.
Chad Rosier89aa2ce2012-10-30 21:42:09 +0000137 if (CombinedArg) {
138 CmdArgs.push_back(Args.MakeArgString(std::string(ArgName) + Dirs));
139 } else {
140 CmdArgs.push_back(ArgName);
141 CmdArgs.push_back(Args.MakeArgString(Dirs));
142 }
Bill Wendlingbdb8f3c2012-03-12 21:22:35 +0000143 }
144}
145
Daniel Dunbar2008fee2010-09-17 00:24:54 +0000146static void AddLinkerInputs(const ToolChain &TC,
147 const InputInfoList &Inputs, const ArgList &Args,
148 ArgStringList &CmdArgs) {
149 const Driver &D = TC.getDriver();
150
Daniel Dunbar8ac38d72011-02-19 05:33:51 +0000151 // Add extra linker input arguments which are not treated as inputs
152 // (constructed via -Xarch_).
153 Args.AddAllArgValues(CmdArgs, options::OPT_Zlinker_input);
154
Daniel Dunbar2008fee2010-09-17 00:24:54 +0000155 for (InputInfoList::const_iterator
156 it = Inputs.begin(), ie = Inputs.end(); it != ie; ++it) {
157 const InputInfo &II = *it;
158
159 if (!TC.HasNativeLLVMSupport()) {
160 // Don't try to pass LLVM inputs unless we have native support.
161 if (II.getType() == types::TY_LLVM_IR ||
162 II.getType() == types::TY_LTO_IR ||
163 II.getType() == types::TY_LLVM_BC ||
164 II.getType() == types::TY_LTO_BC)
Chris Lattner5f9e2722011-07-23 10:55:15 +0000165 D.Diag(diag::err_drv_no_linker_llvm_support)
Daniel Dunbar2008fee2010-09-17 00:24:54 +0000166 << TC.getTripleString();
167 }
168
Daniel Dunbare5a37f42010-09-17 00:45:02 +0000169 // Add filenames immediately.
170 if (II.isFilename()) {
Daniel Dunbar2008fee2010-09-17 00:24:54 +0000171 CmdArgs.push_back(II.getFilename());
Daniel Dunbare5a37f42010-09-17 00:45:02 +0000172 continue;
173 }
174
175 // Otherwise, this is a linker input argument.
176 const Arg &A = II.getInputArg();
177
178 // Handle reserved library options.
179 if (A.getOption().matches(options::OPT_Z_reserved_lib_stdcxx)) {
Daniel Dunbar132e35d2010-09-17 01:20:05 +0000180 TC.AddCXXStdlibLibArgs(Args, CmdArgs);
Shantonu Sen7433fed2010-09-17 18:39:08 +0000181 } else if (A.getOption().matches(options::OPT_Z_reserved_lib_cckext)) {
182 TC.AddCCKextLibArgs(Args, CmdArgs);
Daniel Dunbare5a37f42010-09-17 00:45:02 +0000183 } else
184 A.renderAsInput(Args, CmdArgs);
Daniel Dunbar2008fee2010-09-17 00:24:54 +0000185 }
Bill Wendlingbdb8f3c2012-03-12 21:22:35 +0000186
187 // LIBRARY_PATH - included following the user specified library paths.
Bill Wendling3d717152012-03-12 22:10:06 +0000188 addDirectoryList(Args, CmdArgs, "-L", "LIBRARY_PATH");
Daniel Dunbar2008fee2010-09-17 00:24:54 +0000189}
190
John McCallf85e1932011-06-15 23:02:42 +0000191/// \brief Determine whether Objective-C automated reference counting is
192/// enabled.
193static bool isObjCAutoRefCount(const ArgList &Args) {
194 return Args.hasFlag(options::OPT_fobjc_arc, options::OPT_fno_objc_arc, false);
195}
196
Ted Kremenekebcb57a2012-03-06 20:05:56 +0000197/// \brief Determine whether we are linking the ObjC runtime.
198static bool isObjCRuntimeLinked(const ArgList &Args) {
Bob Wilsona7635f12012-08-07 19:58:00 +0000199 if (isObjCAutoRefCount(Args)) {
200 Args.ClaimAllArgs(options::OPT_fobjc_link_runtime);
Ted Kremenekebcb57a2012-03-06 20:05:56 +0000201 return true;
Bob Wilsona7635f12012-08-07 19:58:00 +0000202 }
Ted Kremenekebcb57a2012-03-06 20:05:56 +0000203 return Args.hasArg(options::OPT_fobjc_link_runtime);
204}
205
Rafael Espindoladb3f24a2011-06-02 18:58:46 +0000206static void addProfileRT(const ToolChain &TC, const ArgList &Args,
Bill Wendling3f4be6f2011-06-27 19:15:03 +0000207 ArgStringList &CmdArgs,
208 llvm::Triple Triple) {
209 if (!(Args.hasArg(options::OPT_fprofile_arcs) ||
210 Args.hasArg(options::OPT_fprofile_generate) ||
211 Args.hasArg(options::OPT_fcreate_profile) ||
212 Args.hasArg(options::OPT_coverage)))
213 return;
214
215 // GCC links libgcov.a by adding -L<inst>/gcc/lib/gcc/<triple>/<ver> -lgcov to
216 // the link line. We cannot do the same thing because unlike gcov there is a
217 // libprofile_rt.so. We used to use the -l:libprofile_rt.a syntax, but that is
218 // not supported by old linkers.
Benjamin Kramerf2db04c2011-11-07 16:02:25 +0000219 std::string ProfileRT =
220 std::string(TC.getDriver().Dir) + "/../lib/libprofile_rt.a";
Bill Wendling3f4be6f2011-06-27 19:15:03 +0000221
Bill Wendling3f4be6f2011-06-27 19:15:03 +0000222 CmdArgs.push_back(Args.MakeArgString(ProfileRT));
Rafael Espindoladb3f24a2011-06-02 18:58:46 +0000223}
224
Michael J. Spencer91e06da2012-10-19 22:37:06 +0000225static bool forwardToGCC(const Option &O) {
226 return !O.hasFlag(options::NoForward) &&
227 !O.hasFlag(options::DriverOption) &&
228 !O.hasFlag(options::LinkerInput);
229}
230
Peter Collingbourne54db68b2011-11-06 00:40:05 +0000231void Clang::AddPreprocessingOptions(Compilation &C,
232 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();
Peter Collingbourne5d4d9802011-11-21 00:01:05 +0000252 C.addFailureResultFile(DepFile);
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 {
259 DepFile = darwin::CC1::getDependencyFileName(Args, Inputs);
Peter Collingbourne5d4d9802011-11-21 00:01:05 +0000260 C.addFailureResultFile(DepFile);
Daniel Dunbarc21c4852009-04-08 23:54:23 +0000261 }
262 CmdArgs.push_back("-dependency-file");
263 CmdArgs.push_back(DepFile);
264
Chris Lattner3edbeb72010-03-29 17:55:58 +0000265 // Add a default target if one wasn't specified.
Daniel Dunbarc21c4852009-04-08 23:54:23 +0000266 if (!Args.hasArg(options::OPT_MT) && !Args.hasArg(options::OPT_MQ)) {
267 const char *DepTarget;
268
269 // If user provided -o, that is the dependency target, except
270 // when we are only generating a dependency file.
271 Arg *OutputOpt = Args.getLastArg(options::OPT_o);
272 if (OutputOpt && Output.getType() != types::TY_Dependencies) {
Richard Smith1d489cf2012-11-01 04:30:05 +0000273 DepTarget = OutputOpt->getValue();
Daniel Dunbarc21c4852009-04-08 23:54:23 +0000274 } else {
275 // Otherwise derive from the base input.
276 //
277 // FIXME: This should use the computed output file location.
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +0000278 SmallString<128> P(Inputs[0].getBaseInput());
Michael J. Spencer472ccff2010-12-18 00:19:12 +0000279 llvm::sys::path::replace_extension(P, "o");
280 DepTarget = Args.MakeArgString(llvm::sys::path::filename(P));
Daniel Dunbarc21c4852009-04-08 23:54:23 +0000281 }
282
283 CmdArgs.push_back("-MT");
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +0000284 SmallString<128> Quoted;
Chris Lattner3edbeb72010-03-29 17:55:58 +0000285 QuoteTarget(DepTarget, Quoted);
286 CmdArgs.push_back(Args.MakeArgString(Quoted));
Daniel Dunbarc21c4852009-04-08 23:54:23 +0000287 }
288
Daniel Dunbarb827a052009-11-19 03:26:40 +0000289 if (A->getOption().matches(options::OPT_M) ||
290 A->getOption().matches(options::OPT_MD))
Daniel Dunbarc21c4852009-04-08 23:54:23 +0000291 CmdArgs.push_back("-sys-header-deps");
292 }
293
Peter Collingbournebb527862011-07-12 19:35:15 +0000294 if (Args.hasArg(options::OPT_MG)) {
295 if (!A || A->getOption().matches(options::OPT_MD) ||
296 A->getOption().matches(options::OPT_MMD))
Chris Lattner5f9e2722011-07-23 10:55:15 +0000297 D.Diag(diag::err_drv_mg_requires_m_or_mm);
Peter Collingbournebb527862011-07-12 19:35:15 +0000298 CmdArgs.push_back("-MG");
299 }
300
Daniel Dunbarc21c4852009-04-08 23:54:23 +0000301 Args.AddLastArg(CmdArgs, options::OPT_MP);
Chris Lattner3edbeb72010-03-29 17:55:58 +0000302
303 // Convert all -MQ <target> args to -MT <quoted target>
304 for (arg_iterator it = Args.filtered_begin(options::OPT_MT,
305 options::OPT_MQ),
306 ie = Args.filtered_end(); it != ie; ++it) {
Daniel Dunbar7e4953e2010-06-11 22:00:13 +0000307 const Arg *A = *it;
308 A->claim();
Chris Lattner3edbeb72010-03-29 17:55:58 +0000309
Daniel Dunbar7e4953e2010-06-11 22:00:13 +0000310 if (A->getOption().matches(options::OPT_MQ)) {
Chris Lattner3edbeb72010-03-29 17:55:58 +0000311 CmdArgs.push_back("-MT");
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +0000312 SmallString<128> Quoted;
Richard Smith1d489cf2012-11-01 04:30:05 +0000313 QuoteTarget(A->getValue(), Quoted);
Chris Lattner3edbeb72010-03-29 17:55:58 +0000314 CmdArgs.push_back(Args.MakeArgString(Quoted));
315
316 // -MT flag - no change
317 } else {
Daniel Dunbar7e4953e2010-06-11 22:00:13 +0000318 A->render(Args, CmdArgs);
Chris Lattner3edbeb72010-03-29 17:55:58 +0000319 }
320 }
Daniel Dunbarc21c4852009-04-08 23:54:23 +0000321
Douglas Gregordf91ef32009-04-18 00:34:01 +0000322 // Add -i* options, and automatically translate to
323 // -include-pch/-include-pth for transparent PCH support. It's
324 // wonky, but we include looking for .gch so we can support seamless
325 // replacement into a build system already set up to be generating
326 // .gch files.
Argyrios Kyrtzidis990142a2010-09-30 16:53:47 +0000327 bool RenderedImplicitInclude = false;
Daniel Dunbarcdd96862009-11-25 11:53:23 +0000328 for (arg_iterator it = Args.filtered_begin(options::OPT_clang_i_Group),
329 ie = Args.filtered_end(); it != ie; ++it) {
330 const Arg *A = it;
Daniel Dunbarc21c4852009-04-08 23:54:23 +0000331
332 if (A->getOption().matches(options::OPT_include)) {
Argyrios Kyrtzidis990142a2010-09-30 16:53:47 +0000333 bool IsFirstImplicitInclude = !RenderedImplicitInclude;
334 RenderedImplicitInclude = true;
335
Argyrios Kyrtzidise5c35372010-08-11 23:27:58 +0000336 // Use PCH if the user requested it.
Daniel Dunbar0ebd9322009-10-15 20:02:44 +0000337 bool UsePCH = D.CCCUsePCH;
Daniel Dunbar0ebd9322009-10-15 20:02:44 +0000338
Daniel Dunbarc21c4852009-04-08 23:54:23 +0000339 bool FoundPTH = false;
Douglas Gregordf91ef32009-04-18 00:34:01 +0000340 bool FoundPCH = false;
Richard Smith1d489cf2012-11-01 04:30:05 +0000341 llvm::sys::Path P(A->getValue());
Michael J. Spencer32bef4e2011-01-10 02:34:13 +0000342 bool Exists;
Daniel Dunbar0ebd9322009-10-15 20:02:44 +0000343 if (UsePCH) {
Douglas Gregordf91ef32009-04-18 00:34:01 +0000344 P.appendSuffix("pch");
Michael J. Spencer32bef4e2011-01-10 02:34:13 +0000345 if (!llvm::sys::fs::exists(P.str(), Exists) && Exists)
Douglas Gregordf91ef32009-04-18 00:34:01 +0000346 FoundPCH = true;
Mike Stump1eb44332009-09-09 15:08:12 +0000347 else
Douglas Gregordf91ef32009-04-18 00:34:01 +0000348 P.eraseSuffix();
Daniel Dunbarc21c4852009-04-08 23:54:23 +0000349 }
350
Douglas Gregordf91ef32009-04-18 00:34:01 +0000351 if (!FoundPCH) {
352 P.appendSuffix("pth");
Michael J. Spencer32bef4e2011-01-10 02:34:13 +0000353 if (!llvm::sys::fs::exists(P.str(), Exists) && Exists)
Douglas Gregordf91ef32009-04-18 00:34:01 +0000354 FoundPTH = true;
355 else
356 P.eraseSuffix();
Mike Stump1eb44332009-09-09 15:08:12 +0000357 }
358
Douglas Gregordf91ef32009-04-18 00:34:01 +0000359 if (!FoundPCH && !FoundPTH) {
360 P.appendSuffix("gch");
Michael J. Spencer32bef4e2011-01-10 02:34:13 +0000361 if (!llvm::sys::fs::exists(P.str(), Exists) && Exists) {
Daniel Dunbar0ebd9322009-10-15 20:02:44 +0000362 FoundPCH = UsePCH;
363 FoundPTH = !UsePCH;
Douglas Gregordf91ef32009-04-18 00:34:01 +0000364 }
Mike Stump1eb44332009-09-09 15:08:12 +0000365 else
Douglas Gregordf91ef32009-04-18 00:34:01 +0000366 P.eraseSuffix();
367 }
368
369 if (FoundPCH || FoundPTH) {
Argyrios Kyrtzidis990142a2010-09-30 16:53:47 +0000370 if (IsFirstImplicitInclude) {
371 A->claim();
372 if (UsePCH)
373 CmdArgs.push_back("-include-pch");
374 else
375 CmdArgs.push_back("-include-pth");
376 CmdArgs.push_back(Args.MakeArgString(P.str()));
377 continue;
378 } else {
379 // Ignore the PCH if not first on command line and emit warning.
Chris Lattner5f9e2722011-07-23 10:55:15 +0000380 D.Diag(diag::warn_drv_pch_not_first_include)
Argyrios Kyrtzidis990142a2010-09-30 16:53:47 +0000381 << P.str() << A->getAsString(Args);
382 }
Daniel Dunbarc21c4852009-04-08 23:54:23 +0000383 }
384 }
385
386 // Not translated, render as usual.
387 A->claim();
388 A->render(Args, CmdArgs);
389 }
390
391 Args.AddAllArgs(CmdArgs, options::OPT_D, options::OPT_U);
Douglas Gregor65e02fa2011-07-28 04:45:53 +0000392 Args.AddAllArgs(CmdArgs, options::OPT_I_Group, options::OPT_F,
393 options::OPT_index_header_map);
Daniel Dunbarc21c4852009-04-08 23:54:23 +0000394
395 // Add -Wp, and -Xassembler if using the preprocessor.
396
397 // FIXME: There is a very unfortunate problem here, some troubled
398 // souls abuse -Wp, to pass preprocessor options in gcc syntax. To
399 // really support that we would have to parse and then translate
400 // those options. :(
401 Args.AddAllArgValues(CmdArgs, options::OPT_Wp_COMMA,
402 options::OPT_Xpreprocessor);
Daniel Dunbar607d7f62009-10-29 01:53:44 +0000403
404 // -I- is a deprecated GCC feature, reject it.
405 if (Arg *A = Args.getLastArg(options::OPT_I_))
Chris Lattner5f9e2722011-07-23 10:55:15 +0000406 D.Diag(diag::err_drv_I_dash_not_supported) << A->getAsString(Args);
Chandler Carruthfeee58c2010-10-20 07:00:47 +0000407
408 // If we have a --sysroot, and don't have an explicit -isysroot flag, add an
409 // -isysroot to the CC1 invocation.
Sebastian Pop4762a2d2012-04-16 04:16:43 +0000410 StringRef sysroot = C.getSysRoot();
411 if (sysroot != "") {
Chandler Carruthfeee58c2010-10-20 07:00:47 +0000412 if (!Args.hasArg(options::OPT_isysroot)) {
413 CmdArgs.push_back("-isysroot");
Sebastian Pop4762a2d2012-04-16 04:16:43 +0000414 CmdArgs.push_back(C.getArgs().MakeArgString(sysroot));
Chandler Carruthfeee58c2010-10-20 07:00:47 +0000415 }
416 }
Douglas Gregor8ee51ef2011-09-14 20:28:46 +0000417
418 // If a module path was provided, pass it along. Otherwise, use a temporary
419 // directory.
420 if (Arg *A = Args.getLastArg(options::OPT_fmodule_cache_path)) {
Douglas Gregor8ee51ef2011-09-14 20:28:46 +0000421 A->claim();
422 A->render(Args, CmdArgs);
423 } else {
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +0000424 SmallString<128> DefaultModuleCache;
Douglas Gregor8ee51ef2011-09-14 20:28:46 +0000425 llvm::sys::path::system_temp_directory(/*erasedOnReboot=*/false,
426 DefaultModuleCache);
427 llvm::sys::path::append(DefaultModuleCache, "clang-module-cache");
428 CmdArgs.push_back("-fmodule-cache-path");
429 CmdArgs.push_back(Args.MakeArgString(DefaultModuleCache));
430 }
Douglas Gregorfba18aa2011-09-15 22:00:41 +0000431
Benjamin Kramer47adebe2011-09-22 21:41:16 +0000432 // Parse additional include paths from environment variables.
Chandler Carruthb5870e72011-11-04 07:12:58 +0000433 // FIXME: We should probably sink the logic for handling these from the
434 // frontend into the driver. It will allow deleting 4 otherwise unused flags.
Benjamin Kramer47adebe2011-09-22 21:41:16 +0000435 // CPATH - included following the user specified includes (but prior to
436 // builtin and standard includes).
Bill Wendling3d717152012-03-12 22:10:06 +0000437 addDirectoryList(Args, CmdArgs, "-I", "CPATH");
Benjamin Kramer47adebe2011-09-22 21:41:16 +0000438 // C_INCLUDE_PATH - system includes enabled when compiling C.
Bill Wendling3d717152012-03-12 22:10:06 +0000439 addDirectoryList(Args, CmdArgs, "-c-isystem", "C_INCLUDE_PATH");
Benjamin Kramer47adebe2011-09-22 21:41:16 +0000440 // CPLUS_INCLUDE_PATH - system includes enabled when compiling C++.
Bill Wendling3d717152012-03-12 22:10:06 +0000441 addDirectoryList(Args, CmdArgs, "-cxx-isystem", "CPLUS_INCLUDE_PATH");
Benjamin Kramer47adebe2011-09-22 21:41:16 +0000442 // OBJC_INCLUDE_PATH - system includes enabled when compiling ObjC.
Bill Wendling3d717152012-03-12 22:10:06 +0000443 addDirectoryList(Args, CmdArgs, "-objc-isystem", "OBJC_INCLUDE_PATH");
Benjamin Kramer47adebe2011-09-22 21:41:16 +0000444 // OBJCPLUS_INCLUDE_PATH - system includes enabled when compiling ObjC++.
Bill Wendling3d717152012-03-12 22:10:06 +0000445 addDirectoryList(Args, CmdArgs, "-objcxx-isystem", "OBJCPLUS_INCLUDE_PATH");
Chandler Carruth88491fc2011-11-04 07:12:53 +0000446
Chandler Carruth88491fc2011-11-04 07:12:53 +0000447 // Add C++ include arguments, if needed.
Chandler Carrutha4614422011-11-04 07:43:33 +0000448 if (types::isCXX(Inputs[0].getType()))
Chandler Carruth7ffa0322011-11-04 07:34:47 +0000449 getToolChain().AddClangCXXStdlibIncludeArgs(Args, CmdArgs);
Chandler Carruth7d7e9f92011-11-05 20:17:13 +0000450
451 // Add system include arguments.
452 getToolChain().AddClangSystemIncludeArgs(Args, CmdArgs);
Daniel Dunbarc21c4852009-04-08 23:54:23 +0000453}
454
Daniel Dunbar1d65e4b2009-09-10 22:59:51 +0000455/// getLLVMArchSuffixForARM - Get the LLVM arch name to use for a particular
Daniel Dunbar728a5122009-09-10 06:49:20 +0000456/// CPU.
457//
458// FIXME: This is redundant with -mcpu, why does LLVM use this.
459// FIXME: tblgen this, or kill it!
Chris Lattner5f9e2722011-07-23 10:55:15 +0000460static const char *getLLVMArchSuffixForARM(StringRef CPU) {
Chad Rosierae1aee62011-10-07 17:48:56 +0000461 return llvm::StringSwitch<const char *>(CPU)
462 .Cases("arm7tdmi", "arm7tdmi-s", "arm710t", "v4t")
463 .Cases("arm720t", "arm9", "arm9tdmi", "v4t")
464 .Cases("arm920", "arm920t", "arm922t", "v4t")
465 .Cases("arm940t", "ep9312","v4t")
466 .Cases("arm10tdmi", "arm1020t", "v5")
467 .Cases("arm9e", "arm926ej-s", "arm946e-s", "v5e")
468 .Cases("arm966e-s", "arm968e-s", "arm10e", "v5e")
469 .Cases("arm1020e", "arm1022e", "xscale", "iwmmxt", "v5e")
470 .Cases("arm1136j-s", "arm1136jf-s", "arm1176jz-s", "v6")
471 .Cases("arm1176jzf-s", "mpcorenovfp", "mpcore", "v6")
472 .Cases("arm1156t2-s", "arm1156t2f-s", "v6t2")
Silviu Baranga2df67ea2012-09-13 15:06:00 +0000473 .Cases("cortex-a8", "cortex-a9", "cortex-a15", "v7")
Bob Wilson57f6d192012-03-21 17:19:12 +0000474 .Case("cortex-m3", "v7m")
Jim Grosbach69033132012-03-29 19:53:34 +0000475 .Case("cortex-m4", "v7m")
Bob Wilson57f6d192012-03-21 17:19:12 +0000476 .Case("cortex-m0", "v6m")
Bob Wilson336bfa32012-09-29 23:52:50 +0000477 .Case("cortex-a9-mp", "v7f")
478 .Case("swift", "v7s")
Chad Rosierae1aee62011-10-07 17:48:56 +0000479 .Default("");
Daniel Dunbar728a5122009-09-10 06:49:20 +0000480}
481
Benjamin Kramer92c4fd52012-06-26 22:20:06 +0000482/// getARMTargetCPU - Get the (LLVM) name of the ARM cpu we are targeting.
483//
484// FIXME: tblgen this.
485static std::string getARMTargetCPU(const ArgList &Args,
486 const llvm::Triple &Triple) {
487 // FIXME: Warn on inconsistent use of -mcpu and -march.
488
489 // If we have -mcpu=, use that.
490 if (Arg *A = Args.getLastArg(options::OPT_mcpu_EQ)) {
Richard Smith1d489cf2012-11-01 04:30:05 +0000491 StringRef MCPU = A->getValue();
Benjamin Kramer92c4fd52012-06-26 22:20:06 +0000492 // Handle -mcpu=native.
493 if (MCPU == "native")
494 return llvm::sys::getHostCPUName();
495 else
496 return MCPU;
497 }
498
499 StringRef MArch;
500 if (Arg *A = Args.getLastArg(options::OPT_march_EQ)) {
501 // Otherwise, if we have -march= choose the base CPU for that arch.
Richard Smith1d489cf2012-11-01 04:30:05 +0000502 MArch = A->getValue();
Benjamin Kramer92c4fd52012-06-26 22:20:06 +0000503 } else {
504 // Otherwise, use the Arch from the triple.
505 MArch = Triple.getArchName();
506 }
507
508 // Handle -march=native.
509 std::string NativeMArch;
510 if (MArch == "native") {
511 std::string CPU = llvm::sys::getHostCPUName();
512 if (CPU != "generic") {
513 // Translate the native cpu into the architecture. The switch below will
514 // then chose the minimum cpu for that arch.
515 NativeMArch = std::string("arm") + getLLVMArchSuffixForARM(CPU);
516 MArch = NativeMArch;
517 }
518 }
519
520 return llvm::StringSwitch<const char *>(MArch)
521 .Cases("armv2", "armv2a","arm2")
522 .Case("armv3", "arm6")
523 .Case("armv3m", "arm7m")
524 .Cases("armv4", "armv4t", "arm7tdmi")
525 .Cases("armv5", "armv5t", "arm10tdmi")
526 .Cases("armv5e", "armv5te", "arm1022e")
527 .Case("armv5tej", "arm926ej-s")
528 .Cases("armv6", "armv6k", "arm1136jf-s")
529 .Case("armv6j", "arm1136j-s")
530 .Cases("armv6z", "armv6zk", "arm1176jzf-s")
531 .Case("armv6t2", "arm1156t2-s")
532 .Cases("armv7", "armv7a", "armv7-a", "cortex-a8")
Bob Wilson336bfa32012-09-29 23:52:50 +0000533 .Cases("armv7f", "armv7-f", "cortex-a9-mp")
534 .Cases("armv7s", "armv7-s", "swift")
Benjamin Kramer92c4fd52012-06-26 22:20:06 +0000535 .Cases("armv7r", "armv7-r", "cortex-r4")
536 .Cases("armv7m", "armv7-m", "cortex-m3")
537 .Case("ep9312", "ep9312")
538 .Case("iwmmxt", "iwmmxt")
539 .Case("xscale", "xscale")
540 .Cases("armv6m", "armv6-m", "cortex-m0")
541 // If all else failed, return the most base CPU LLVM supports.
542 .Default("arm7tdmi");
543}
544
Daniel Dunbar1f95e652009-11-17 06:37:03 +0000545// FIXME: Move to target hook.
546static bool isSignedCharDefault(const llvm::Triple &Triple) {
547 switch (Triple.getArch()) {
548 default:
549 return true;
550
Jim Grosbach5b4e7b12011-05-24 15:40:46 +0000551 case llvm::Triple::arm:
Daniel Dunbar1f95e652009-11-17 06:37:03 +0000552 case llvm::Triple::ppc:
553 case llvm::Triple::ppc64:
Bob Wilson905c45f2011-10-14 05:03:44 +0000554 if (Triple.isOSDarwin())
Daniel Dunbar1f95e652009-11-17 06:37:03 +0000555 return true;
556 return false;
Daniel Dunbar1f95e652009-11-17 06:37:03 +0000557 }
558}
559
Chad Rosier99317272012-04-04 20:51:35 +0000560// Handle -mfpu=.
561//
562// FIXME: Centralize feature selection, defaulting shouldn't be also in the
563// frontend target.
564static void addFPUArgs(const Driver &D, const Arg *A, const ArgList &Args,
565 ArgStringList &CmdArgs) {
Richard Smith1d489cf2012-11-01 04:30:05 +0000566 StringRef FPU = A->getValue();
Chad Rosier99317272012-04-04 20:51:35 +0000567
568 // Set the target features based on the FPU.
569 if (FPU == "fpa" || FPU == "fpe2" || FPU == "fpe3" || FPU == "maverick") {
570 // Disable any default FPU support.
571 CmdArgs.push_back("-target-feature");
572 CmdArgs.push_back("-vfp2");
573 CmdArgs.push_back("-target-feature");
574 CmdArgs.push_back("-vfp3");
575 CmdArgs.push_back("-target-feature");
576 CmdArgs.push_back("-neon");
577 } else if (FPU == "vfp3-d16" || FPU == "vfpv3-d16") {
578 CmdArgs.push_back("-target-feature");
579 CmdArgs.push_back("+vfp3");
580 CmdArgs.push_back("-target-feature");
581 CmdArgs.push_back("+d16");
582 CmdArgs.push_back("-target-feature");
583 CmdArgs.push_back("-neon");
584 } else if (FPU == "vfp") {
585 CmdArgs.push_back("-target-feature");
586 CmdArgs.push_back("+vfp2");
587 CmdArgs.push_back("-target-feature");
588 CmdArgs.push_back("-neon");
589 } else if (FPU == "vfp3" || FPU == "vfpv3") {
590 CmdArgs.push_back("-target-feature");
591 CmdArgs.push_back("+vfp3");
592 CmdArgs.push_back("-target-feature");
593 CmdArgs.push_back("-neon");
594 } else if (FPU == "neon") {
595 CmdArgs.push_back("-target-feature");
596 CmdArgs.push_back("+neon");
597 } else
598 D.Diag(diag::err_drv_clang_unsupported) << A->getAsString(Args);
599}
600
Chad Rosier7a938fa2012-04-04 20:39:32 +0000601// Handle -mfpmath=.
602static void addFPMathArgs(const Driver &D, const Arg *A, const ArgList &Args,
Chad Rosier30fe6ba2012-04-04 22:13:40 +0000603 ArgStringList &CmdArgs, StringRef CPU) {
Richard Smith1d489cf2012-11-01 04:30:05 +0000604 StringRef FPMath = A->getValue();
Chad Rosier7a938fa2012-04-04 20:39:32 +0000605
606 // Set the target features based on the FPMath.
607 if (FPMath == "neon") {
608 CmdArgs.push_back("-target-feature");
609 CmdArgs.push_back("+neonfp");
Chad Rosier30fe6ba2012-04-04 22:13:40 +0000610
Silviu Baranga2df67ea2012-09-13 15:06:00 +0000611 if (CPU != "cortex-a8" && CPU != "cortex-a9" && CPU != "cortex-a9-mp" &&
612 CPU != "cortex-a15")
Chad Rosier30fe6ba2012-04-04 22:13:40 +0000613 D.Diag(diag::err_drv_invalid_feature) << "-mfpmath=neon" << CPU;
614
Chad Rosier7a938fa2012-04-04 20:39:32 +0000615 } else if (FPMath == "vfp" || FPMath == "vfp2" || FPMath == "vfp3" ||
616 FPMath == "vfp4") {
617 CmdArgs.push_back("-target-feature");
618 CmdArgs.push_back("-neonfp");
Chad Rosier30fe6ba2012-04-04 22:13:40 +0000619
620 // FIXME: Add warnings when disabling a feature not present for a given CPU.
Chad Rosier7a938fa2012-04-04 20:39:32 +0000621 } else
622 D.Diag(diag::err_drv_clang_unsupported) << A->getAsString(Args);
623}
624
Anton Korobeynikove2571792012-04-09 13:38:30 +0000625// Select the float ABI as determined by -msoft-float, -mhard-float, and
626// -mfloat-abi=.
627static StringRef getARMFloatABI(const Driver &D,
628 const ArgList &Args,
629 const llvm::Triple &Triple) {
Chris Lattner5f9e2722011-07-23 10:55:15 +0000630 StringRef FloatABI;
Daniel Dunbarcbd19332009-09-10 23:00:09 +0000631 if (Arg *A = Args.getLastArg(options::OPT_msoft_float,
632 options::OPT_mhard_float,
633 options::OPT_mfloat_abi_EQ)) {
634 if (A->getOption().matches(options::OPT_msoft_float))
635 FloatABI = "soft";
636 else if (A->getOption().matches(options::OPT_mhard_float))
637 FloatABI = "hard";
638 else {
Richard Smith1d489cf2012-11-01 04:30:05 +0000639 FloatABI = A->getValue();
Daniel Dunbarcbd19332009-09-10 23:00:09 +0000640 if (FloatABI != "soft" && FloatABI != "softfp" && FloatABI != "hard") {
Chris Lattner5f9e2722011-07-23 10:55:15 +0000641 D.Diag(diag::err_drv_invalid_mfloat_abi)
Daniel Dunbarcbd19332009-09-10 23:00:09 +0000642 << A->getAsString(Args);
643 FloatABI = "soft";
644 }
645 }
646 }
647
648 // If unspecified, choose the default based on the platform.
649 if (FloatABI.empty()) {
Rafael Espindolabcd6df62010-06-28 17:18:09 +0000650 switch (Triple.getOS()) {
Bob Wilson905c45f2011-10-14 05:03:44 +0000651 case llvm::Triple::Darwin:
652 case llvm::Triple::MacOSX:
653 case llvm::Triple::IOS: {
Daniel Dunbarcbd19332009-09-10 23:00:09 +0000654 // Darwin defaults to "softfp" for v6 and v7.
655 //
656 // FIXME: Factor out an ARM class so we can cache the arch somewhere.
Benjamin Kramer92c4fd52012-06-26 22:20:06 +0000657 std::string ArchName =
Rafael Espindolabcd6df62010-06-28 17:18:09 +0000658 getLLVMArchSuffixForARM(getARMTargetCPU(Args, Triple));
Benjamin Kramer92c4fd52012-06-26 22:20:06 +0000659 if (StringRef(ArchName).startswith("v6") ||
660 StringRef(ArchName).startswith("v7"))
Daniel Dunbarcbd19332009-09-10 23:00:09 +0000661 FloatABI = "softfp";
662 else
663 FloatABI = "soft";
664 break;
665 }
666
667 default:
Bob Wilsonfc2bd7c2011-02-04 17:59:28 +0000668 switch(Triple.getEnvironment()) {
Jiangning Liuff104a12012-07-31 08:06:29 +0000669 case llvm::Triple::GNUEABIHF:
670 FloatABI = "hard";
671 break;
Bob Wilsonfc2bd7c2011-02-04 17:59:28 +0000672 case llvm::Triple::GNUEABI:
673 FloatABI = "softfp";
674 break;
675 case llvm::Triple::EABI:
676 // EABI is always AAPCS, and if it was not marked 'hard', it's softfp
677 FloatABI = "softfp";
678 break;
Logan Chien94a71422012-09-02 09:30:11 +0000679 case llvm::Triple::Android: {
Benjamin Kramer92c4fd52012-06-26 22:20:06 +0000680 std::string ArchName =
Chandler Carruthb43550b2012-01-10 19:47:42 +0000681 getLLVMArchSuffixForARM(getARMTargetCPU(Args, Triple));
Benjamin Kramer92c4fd52012-06-26 22:20:06 +0000682 if (StringRef(ArchName).startswith("v7"))
Chandler Carruthb43550b2012-01-10 19:47:42 +0000683 FloatABI = "softfp";
684 else
685 FloatABI = "soft";
686 break;
687 }
Bob Wilsonfc2bd7c2011-02-04 17:59:28 +0000688 default:
689 // Assume "soft", but warn the user we are guessing.
690 FloatABI = "soft";
Chris Lattner5f9e2722011-07-23 10:55:15 +0000691 D.Diag(diag::warn_drv_assuming_mfloat_abi_is) << "soft";
Bob Wilsonfc2bd7c2011-02-04 17:59:28 +0000692 break;
693 }
Daniel Dunbarcbd19332009-09-10 23:00:09 +0000694 }
695 }
696
Anton Korobeynikove2571792012-04-09 13:38:30 +0000697 return FloatABI;
698}
699
700
701void Clang::AddARMTargetArgs(const ArgList &Args,
702 ArgStringList &CmdArgs,
703 bool KernelOrKext) const {
704 const Driver &D = getToolChain().getDriver();
Daniel Dunbar7a0c0642012-10-15 22:23:53 +0000705 // Get the effective triple, which takes into account the deployment target.
706 std::string TripleStr = getToolChain().ComputeEffectiveClangTriple(Args);
707 llvm::Triple Triple(TripleStr);
Daniel Dunbar2e4e1102012-10-22 18:30:51 +0000708 std::string CPUName = getARMTargetCPU(Args, Triple);
Anton Korobeynikove2571792012-04-09 13:38:30 +0000709
710 // Select the ABI to use.
711 //
712 // FIXME: Support -meabi.
713 const char *ABIName = 0;
714 if (Arg *A = Args.getLastArg(options::OPT_mabi_EQ)) {
Richard Smith1d489cf2012-11-01 04:30:05 +0000715 ABIName = A->getValue();
Daniel Dunbar2e4e1102012-10-22 18:30:51 +0000716 } else if (Triple.isOSDarwin()) {
717 // The backend is hardwired to assume AAPCS for M-class processors, ensure
718 // the frontend matches that.
719 if (StringRef(CPUName).startswith("cortex-m")) {
720 ABIName = "aapcs";
721 } else {
722 ABIName = "apcs-gnu";
723 }
Anton Korobeynikove2571792012-04-09 13:38:30 +0000724 } else {
725 // Select the default based on the platform.
726 switch(Triple.getEnvironment()) {
Logan Chien94a71422012-09-02 09:30:11 +0000727 case llvm::Triple::Android:
Anton Korobeynikove2571792012-04-09 13:38:30 +0000728 case llvm::Triple::GNUEABI:
Jiangning Liuff104a12012-07-31 08:06:29 +0000729 case llvm::Triple::GNUEABIHF:
Anton Korobeynikove2571792012-04-09 13:38:30 +0000730 ABIName = "aapcs-linux";
731 break;
732 case llvm::Triple::EABI:
733 ABIName = "aapcs";
734 break;
735 default:
736 ABIName = "apcs-gnu";
737 }
738 }
739 CmdArgs.push_back("-target-abi");
740 CmdArgs.push_back(ABIName);
741
742 // Set the CPU based on -march= and -mcpu=.
743 CmdArgs.push_back("-target-cpu");
Daniel Dunbar2e4e1102012-10-22 18:30:51 +0000744 CmdArgs.push_back(Args.MakeArgString(CPUName));
Anton Korobeynikove2571792012-04-09 13:38:30 +0000745
746 // Determine floating point ABI from the options & target defaults.
747 StringRef FloatABI = getARMFloatABI(D, Args, Triple);
Daniel Dunbarcbd19332009-09-10 23:00:09 +0000748 if (FloatABI == "soft") {
749 // Floating point operations and argument passing are soft.
750 //
751 // FIXME: This changes CPP defines, we need -target-soft-float.
Daniel Dunbar3b315262009-11-30 08:42:00 +0000752 CmdArgs.push_back("-msoft-float");
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 if (FloatABI == "softfp") {
756 // Floating point operations are hard, but argument passing is soft.
Daniel Dunbar87667aa2009-12-08 19:49:51 +0000757 CmdArgs.push_back("-mfloat-abi");
758 CmdArgs.push_back("soft");
Daniel Dunbarcbd19332009-09-10 23:00:09 +0000759 } else {
760 // Floating point operations and argument passing are hard.
761 assert(FloatABI == "hard" && "Invalid float abi!");
Daniel Dunbar87667aa2009-12-08 19:49:51 +0000762 CmdArgs.push_back("-mfloat-abi");
763 CmdArgs.push_back("hard");
Daniel Dunbarcbd19332009-09-10 23:00:09 +0000764 }
Daniel Dunbar97f52ac2009-12-19 04:15:38 +0000765
766 // Set appropriate target features for floating point mode.
767 //
768 // FIXME: Note, this is a hack, the LLVM backend doesn't actually use these
769 // yet (it uses the -mfloat-abi and -msoft-float options above), and it is
770 // stripped out by the ARM target.
771
772 // Use software floating point operations?
773 if (FloatABI == "soft") {
774 CmdArgs.push_back("-target-feature");
775 CmdArgs.push_back("+soft-float");
776 }
777
778 // Use software floating point argument passing?
779 if (FloatABI != "hard") {
780 CmdArgs.push_back("-target-feature");
781 CmdArgs.push_back("+soft-float-abi");
782 }
Daniel Dunbara91320b2009-12-21 23:28:17 +0000783
784 // Honor -mfpu=.
Chad Rosier99317272012-04-04 20:51:35 +0000785 if (const Arg *A = Args.getLastArg(options::OPT_mfpu_EQ))
Chad Rosierf80f2a52012-04-04 20:56:36 +0000786 addFPUArgs(D, A, Args, CmdArgs);
Daniel Dunbar7187fac2011-03-17 00:07:34 +0000787
Chad Rosier7a938fa2012-04-04 20:39:32 +0000788 // Honor -mfpmath=.
789 if (const Arg *A = Args.getLastArg(options::OPT_mfpmath_EQ))
Chad Rosier30fe6ba2012-04-04 22:13:40 +0000790 addFPMathArgs(D, A, Args, CmdArgs, getARMTargetCPU(Args, Triple));
Chad Rosier7a938fa2012-04-04 20:39:32 +0000791
Daniel Dunbar7187fac2011-03-17 00:07:34 +0000792 // Setting -msoft-float effectively disables NEON because of the GCC
793 // implementation, although the same isn't true of VFP or VFP3.
794 if (FloatABI == "soft") {
Daniel Dunbarfa41d692011-03-17 17:10:06 +0000795 CmdArgs.push_back("-target-feature");
796 CmdArgs.push_back("-neon");
797 }
798
799 // Kernel code has more strict alignment requirements.
800 if (KernelOrKext) {
Daniel Dunbar7a0c0642012-10-15 22:23:53 +0000801 if (Triple.getOS() != llvm::Triple::IOS || Triple.isOSVersionLT(6)) {
802 CmdArgs.push_back("-backend-option");
803 CmdArgs.push_back("-arm-long-calls");
804 }
Daniel Dunbarfa41d692011-03-17 17:10:06 +0000805
Daniel Dunbar3c66d302011-03-22 16:48:17 +0000806 CmdArgs.push_back("-backend-option");
Daniel Dunbarfa41d692011-03-17 17:10:06 +0000807 CmdArgs.push_back("-arm-strict-align");
Daniel Dunbarb5fbb892011-04-18 21:26:42 +0000808
809 // The kext linker doesn't know how to deal with movw/movt.
Daniel Dunbarb5fbb892011-04-18 21:26:42 +0000810 CmdArgs.push_back("-backend-option");
811 CmdArgs.push_back("-arm-darwin-use-movt=0");
Daniel Dunbar7187fac2011-03-17 00:07:34 +0000812 }
Chad Rosier1b906052011-08-26 00:26:29 +0000813
814 // Setting -mno-global-merge disables the codegen global merge pass. Setting
815 // -mglobal-merge has no effect as the pass is enabled by default.
816 if (Arg *A = Args.getLastArg(options::OPT_mglobal_merge,
817 options::OPT_mno_global_merge)) {
818 if (A->getOption().matches(options::OPT_mno_global_merge))
819 CmdArgs.push_back("-mno-global-merge");
820 }
Chad Rosieree9ad5c2012-05-16 20:40:09 +0000821
Chad Rosier005af272012-05-16 21:19:55 +0000822 if (Args.hasArg(options::OPT_mno_implicit_float))
Chad Rosieree9ad5c2012-05-16 20:40:09 +0000823 CmdArgs.push_back("-no-implicit-float");
Daniel Dunbarb163ef72009-09-10 04:57:17 +0000824}
825
Simon Atanasyan8e1c5982012-09-21 20:19:32 +0000826// Translate MIPS CPU name alias option to CPU name.
827static StringRef getMipsCPUFromAlias(const Arg &A) {
828 if (A.getOption().matches(options::OPT_mips32))
829 return "mips32";
830 if (A.getOption().matches(options::OPT_mips32r2))
831 return "mips32r2";
832 if (A.getOption().matches(options::OPT_mips64))
833 return "mips64";
834 if (A.getOption().matches(options::OPT_mips64r2))
835 return "mips64r2";
836 llvm_unreachable("Unexpected option");
837 return "";
838}
839
Simon Atanasyana2768be2012-04-07 22:09:23 +0000840// Get CPU and ABI names. They are not independent
841// so we have to calculate them together.
842static void getMipsCPUAndABI(const ArgList &Args,
843 const ToolChain &TC,
844 StringRef &CPUName,
845 StringRef &ABIName) {
Simon Atanasyan89d83ff2012-09-10 08:32:41 +0000846 const char *DefMips32CPU = "mips32";
847 const char *DefMips64CPU = "mips64";
Akira Hatanaka9f360622011-09-26 21:07:52 +0000848
Simon Atanasyan89d83ff2012-09-10 08:32:41 +0000849 if (Arg *A = Args.getLastArg(options::OPT_march_EQ,
Simon Atanasyan8e1c5982012-09-21 20:19:32 +0000850 options::OPT_mcpu_EQ,
851 options::OPT_mips_CPUs_Group)) {
852 if (A->getOption().matches(options::OPT_mips_CPUs_Group))
853 CPUName = getMipsCPUFromAlias(*A);
854 else
Richard Smith1d489cf2012-11-01 04:30:05 +0000855 CPUName = A->getValue();
Simon Atanasyan8e1c5982012-09-21 20:19:32 +0000856 }
Simon Atanasyan89d83ff2012-09-10 08:32:41 +0000857
Akira Hatanaka9f360622011-09-26 21:07:52 +0000858 if (Arg *A = Args.getLastArg(options::OPT_mabi_EQ))
Richard Smith1d489cf2012-11-01 04:30:05 +0000859 ABIName = A->getValue();
Simon Atanasyan89d83ff2012-09-10 08:32:41 +0000860
861 // Setup default CPU and ABI names.
862 if (CPUName.empty() && ABIName.empty()) {
863 switch (TC.getTriple().getArch()) {
864 default:
865 llvm_unreachable("Unexpected triple arch name");
866 case llvm::Triple::mips:
867 case llvm::Triple::mipsel:
868 CPUName = DefMips32CPU;
869 break;
870 case llvm::Triple::mips64:
871 case llvm::Triple::mips64el:
872 CPUName = DefMips64CPU;
873 break;
874 }
875 }
876
877 if (!ABIName.empty()) {
878 // Deduce CPU name from ABI name.
879 CPUName = llvm::StringSwitch<const char *>(ABIName)
880 .Cases("o32", "eabi", DefMips32CPU)
881 .Cases("n32", "n64", DefMips64CPU)
882 .Default("");
883 }
884 else if (!CPUName.empty()) {
885 // Deduce ABI name from CPU name.
886 ABIName = llvm::StringSwitch<const char *>(CPUName)
887 .Cases("mips32", "mips32r2", "o32")
888 .Cases("mips64", "mips64r2", "n64")
889 .Default("");
890 }
891
892 // FIXME: Warn on inconsistent cpu and abi usage.
Simon Atanasyana2768be2012-04-07 22:09:23 +0000893}
894
Simon Atanasyan5e627792012-06-02 15:06:29 +0000895// Select the MIPS float ABI as determined by -msoft-float, -mhard-float,
896// and -mfloat-abi=.
897static StringRef getMipsFloatABI(const Driver &D, const ArgList &Args) {
Akira Hatanakaad8d8a32012-03-23 23:07:09 +0000898 // Select the float ABI as determined by -msoft-float, -mhard-float,
899 // and -mfloat-abi=.
Chris Lattner5f9e2722011-07-23 10:55:15 +0000900 StringRef FloatABI;
Eric Christophered734732010-03-02 02:41:08 +0000901 if (Arg *A = Args.getLastArg(options::OPT_msoft_float,
Akira Hatanakaad8d8a32012-03-23 23:07:09 +0000902 options::OPT_mhard_float,
903 options::OPT_mfloat_abi_EQ)) {
Eric Christophered734732010-03-02 02:41:08 +0000904 if (A->getOption().matches(options::OPT_msoft_float))
905 FloatABI = "soft";
906 else if (A->getOption().matches(options::OPT_mhard_float))
907 FloatABI = "hard";
Akira Hatanakaad8d8a32012-03-23 23:07:09 +0000908 else {
Richard Smith1d489cf2012-11-01 04:30:05 +0000909 FloatABI = A->getValue();
Akira Hatanakaad8d8a32012-03-23 23:07:09 +0000910 if (FloatABI != "soft" && FloatABI != "single" && FloatABI != "hard") {
Simon Atanasyan5e627792012-06-02 15:06:29 +0000911 D.Diag(diag::err_drv_invalid_mfloat_abi) << A->getAsString(Args);
Akira Hatanakaad8d8a32012-03-23 23:07:09 +0000912 FloatABI = "hard";
913 }
914 }
Eric Christophered734732010-03-02 02:41:08 +0000915 }
916
917 // If unspecified, choose the default based on the platform.
918 if (FloatABI.empty()) {
Akira Hatanakaad8d8a32012-03-23 23:07:09 +0000919 // Assume "hard", because it's a default value used by gcc.
920 // When we start to recognize specific target MIPS processors,
921 // we will be able to select the default more correctly.
922 FloatABI = "hard";
Eric Christophered734732010-03-02 02:41:08 +0000923 }
924
Simon Atanasyan5e627792012-06-02 15:06:29 +0000925 return FloatABI;
926}
927
Simon Atanasyandc536f52012-07-05 18:51:43 +0000928static void AddTargetFeature(const ArgList &Args,
929 ArgStringList &CmdArgs,
930 OptSpecifier OnOpt,
931 OptSpecifier OffOpt,
932 StringRef FeatureName) {
933 if (Arg *A = Args.getLastArg(OnOpt, OffOpt)) {
934 CmdArgs.push_back("-target-feature");
935 if (A->getOption().matches(OnOpt))
936 CmdArgs.push_back(Args.MakeArgString("+" + FeatureName));
937 else
938 CmdArgs.push_back(Args.MakeArgString("-" + FeatureName));
939 }
940}
941
Simon Atanasyan5e627792012-06-02 15:06:29 +0000942void Clang::AddMIPSTargetArgs(const ArgList &Args,
943 ArgStringList &CmdArgs) const {
944 const Driver &D = getToolChain().getDriver();
945 StringRef CPUName;
946 StringRef ABIName;
947 getMipsCPUAndABI(Args, getToolChain(), CPUName, ABIName);
948
949 CmdArgs.push_back("-target-cpu");
950 CmdArgs.push_back(CPUName.data());
951
952 CmdArgs.push_back("-target-abi");
953 CmdArgs.push_back(ABIName.data());
954
955 StringRef FloatABI = getMipsFloatABI(D, Args);
956
Eric Christophered734732010-03-02 02:41:08 +0000957 if (FloatABI == "soft") {
958 // Floating point operations and argument passing are soft.
Eric Christophered734732010-03-02 02:41:08 +0000959 CmdArgs.push_back("-msoft-float");
Akira Hatanakaad8d8a32012-03-23 23:07:09 +0000960 CmdArgs.push_back("-mfloat-abi");
961 CmdArgs.push_back("soft");
962
963 // FIXME: Note, this is a hack. We need to pass the selected float
964 // mode to the MipsTargetInfoBase to define appropriate macros there.
965 // Now it is the only method.
966 CmdArgs.push_back("-target-feature");
967 CmdArgs.push_back("+soft-float");
968 }
969 else if (FloatABI == "single") {
970 // Restrict the use of hardware floating-point
971 // instructions to 32-bit operations.
972 CmdArgs.push_back("-target-feature");
973 CmdArgs.push_back("+single-float");
974 }
975 else {
976 // Floating point operations and argument passing are hard.
Eric Christophered734732010-03-02 02:41:08 +0000977 assert(FloatABI == "hard" && "Invalid float abi!");
Akira Hatanakaad8d8a32012-03-23 23:07:09 +0000978 CmdArgs.push_back("-mfloat-abi");
979 CmdArgs.push_back("hard");
Eric Christophered734732010-03-02 02:41:08 +0000980 }
Simon Atanasyan0b273ef2012-07-05 14:19:39 +0000981
Simon Atanasyandc536f52012-07-05 18:51:43 +0000982 AddTargetFeature(Args, CmdArgs,
983 options::OPT_mips16, options::OPT_mno_mips16,
984 "mips16");
Simon Atanasyand797a852012-07-05 19:23:00 +0000985 AddTargetFeature(Args, CmdArgs,
986 options::OPT_mdsp, options::OPT_mno_dsp,
987 "dsp");
988 AddTargetFeature(Args, CmdArgs,
989 options::OPT_mdspr2, options::OPT_mno_dspr2,
990 "dspr2");
Simon Atanasyan9804b762012-08-27 20:55:56 +0000991
992 if (Arg *A = Args.getLastArg(options::OPT_G)) {
Richard Smith1d489cf2012-11-01 04:30:05 +0000993 StringRef v = A->getValue();
Simon Atanasyan9804b762012-08-27 20:55:56 +0000994 CmdArgs.push_back("-mllvm");
995 CmdArgs.push_back(Args.MakeArgString("-mips-ssection-threshold=" + v));
996 A->claim();
997 }
Eric Christophered734732010-03-02 02:41:08 +0000998}
999
Hal Finkel02a84272012-06-11 22:35:19 +00001000/// getPPCTargetCPU - Get the (LLVM) name of the PowerPC cpu we are targeting.
1001static std::string getPPCTargetCPU(const ArgList &Args) {
1002 if (Arg *A = Args.getLastArg(options::OPT_mcpu_EQ)) {
Richard Smith1d489cf2012-11-01 04:30:05 +00001003 StringRef CPUName = A->getValue();
Hal Finkel02a84272012-06-11 22:35:19 +00001004
1005 if (CPUName == "native") {
1006 std::string CPU = llvm::sys::getHostCPUName();
1007 if (!CPU.empty() && CPU != "generic")
1008 return CPU;
1009 else
1010 return "";
1011 }
1012
1013 return llvm::StringSwitch<const char *>(CPUName)
1014 .Case("common", "generic")
1015 .Case("440", "440")
1016 .Case("440fp", "440")
1017 .Case("450", "450")
1018 .Case("601", "601")
1019 .Case("602", "602")
1020 .Case("603", "603")
1021 .Case("603e", "603e")
1022 .Case("603ev", "603ev")
1023 .Case("604", "604")
1024 .Case("604e", "604e")
1025 .Case("620", "620")
1026 .Case("G3", "g3")
1027 .Case("7400", "7400")
1028 .Case("G4", "g4")
1029 .Case("7450", "7450")
1030 .Case("G4+", "g4+")
1031 .Case("750", "750")
1032 .Case("970", "970")
1033 .Case("G5", "g5")
1034 .Case("a2", "a2")
Hal Finkel7de32962012-09-18 22:25:03 +00001035 .Case("e500mc", "e500mc")
1036 .Case("e5500", "e5500")
Hal Finkel02a84272012-06-11 22:35:19 +00001037 .Case("power6", "pwr6")
1038 .Case("power7", "pwr7")
1039 .Case("powerpc", "ppc")
1040 .Case("powerpc64", "ppc64")
1041 .Default("");
1042 }
1043
1044 return "";
1045}
1046
1047void Clang::AddPPCTargetArgs(const ArgList &Args,
1048 ArgStringList &CmdArgs) const {
1049 std::string TargetCPUName = getPPCTargetCPU(Args);
1050
1051 // LLVM may default to generating code for the native CPU,
1052 // but, like gcc, we default to a more generic option for
1053 // each architecture. (except on Darwin)
1054 llvm::Triple Triple = getToolChain().getTriple();
1055 if (TargetCPUName.empty() && !Triple.isOSDarwin()) {
1056 if (Triple.getArch() == llvm::Triple::ppc64)
1057 TargetCPUName = "ppc64";
1058 else
1059 TargetCPUName = "ppc";
1060 }
1061
1062 if (!TargetCPUName.empty()) {
1063 CmdArgs.push_back("-target-cpu");
1064 CmdArgs.push_back(Args.MakeArgString(TargetCPUName.c_str()));
1065 }
1066}
1067
Bruno Cardoso Lopes9284d212010-11-09 17:21:19 +00001068void Clang::AddSparcTargetArgs(const ArgList &Args,
1069 ArgStringList &CmdArgs) const {
1070 const Driver &D = getToolChain().getDriver();
1071
1072 if (const Arg *A = Args.getLastArg(options::OPT_march_EQ)) {
Bruno Cardoso Lopes9284d212010-11-09 17:21:19 +00001073 CmdArgs.push_back("-target-cpu");
Richard Smith1d489cf2012-11-01 04:30:05 +00001074 CmdArgs.push_back(A->getValue());
Bruno Cardoso Lopes9284d212010-11-09 17:21:19 +00001075 }
1076
1077 // Select the float ABI as determined by -msoft-float, -mhard-float, and
Chris Lattner5f9e2722011-07-23 10:55:15 +00001078 StringRef FloatABI;
Bruno Cardoso Lopes9284d212010-11-09 17:21:19 +00001079 if (Arg *A = Args.getLastArg(options::OPT_msoft_float,
1080 options::OPT_mhard_float)) {
1081 if (A->getOption().matches(options::OPT_msoft_float))
1082 FloatABI = "soft";
1083 else if (A->getOption().matches(options::OPT_mhard_float))
1084 FloatABI = "hard";
1085 }
1086
1087 // If unspecified, choose the default based on the platform.
1088 if (FloatABI.empty()) {
1089 switch (getToolChain().getTriple().getOS()) {
1090 default:
1091 // Assume "soft", but warn the user we are guessing.
1092 FloatABI = "soft";
Chris Lattner5f9e2722011-07-23 10:55:15 +00001093 D.Diag(diag::warn_drv_assuming_mfloat_abi_is) << "soft";
Bruno Cardoso Lopes9284d212010-11-09 17:21:19 +00001094 break;
1095 }
1096 }
1097
1098 if (FloatABI == "soft") {
1099 // Floating point operations and argument passing are soft.
1100 //
1101 // FIXME: This changes CPP defines, we need -target-soft-float.
1102 CmdArgs.push_back("-msoft-float");
Bruno Cardoso Lopes9284d212010-11-09 17:21:19 +00001103 CmdArgs.push_back("-target-feature");
1104 CmdArgs.push_back("+soft-float");
1105 } else {
1106 assert(FloatABI == "hard" && "Invalid float abi!");
1107 CmdArgs.push_back("-mhard-float");
1108 }
1109}
1110
Daniel Dunbar6acda162009-09-09 22:33:08 +00001111void Clang::AddX86TargetArgs(const ArgList &Args,
1112 ArgStringList &CmdArgs) const {
Rafael Espindola715852c2012-11-02 20:41:30 +00001113 const bool isAndroid =
1114 getToolChain().getTriple().getEnvironment() == llvm::Triple::Android;
Daniel Dunbare6ad3f92009-09-10 22:59:57 +00001115 if (!Args.hasFlag(options::OPT_mred_zone,
1116 options::OPT_mno_red_zone,
1117 true) ||
1118 Args.hasArg(options::OPT_mkernel) ||
1119 Args.hasArg(options::OPT_fapple_kext))
Daniel Dunbar66861e02009-11-20 22:21:36 +00001120 CmdArgs.push_back("-disable-red-zone");
Daniel Dunbare6ad3f92009-09-10 22:59:57 +00001121
Daniel Dunbare6ad3f92009-09-10 22:59:57 +00001122 if (Args.hasFlag(options::OPT_msoft_float,
1123 options::OPT_mno_soft_float,
1124 false))
Daniel Dunbar66861e02009-11-20 22:21:36 +00001125 CmdArgs.push_back("-no-implicit-float");
Daniel Dunbare6ad3f92009-09-10 22:59:57 +00001126
Daniel Dunbarf86fedd2009-11-14 22:04:54 +00001127 const char *CPUName = 0;
Daniel Dunbar6acda162009-09-09 22:33:08 +00001128 if (const Arg *A = Args.getLastArg(options::OPT_march_EQ)) {
Richard Smith1d489cf2012-11-01 04:30:05 +00001129 if (StringRef(A->getValue()) == "native") {
Daniel Dunbarf86fedd2009-11-14 22:04:54 +00001130 // FIXME: Reject attempts to use -march=native unless the target matches
1131 // the host.
1132 //
1133 // FIXME: We should also incorporate the detected target features for use
1134 // with -native.
1135 std::string CPU = llvm::sys::getHostCPUName();
Bob Wilsone0cc3092012-05-09 17:53:10 +00001136 if (!CPU.empty() && CPU != "generic")
Daniel Dunbarf86fedd2009-11-14 22:04:54 +00001137 CPUName = Args.MakeArgString(CPU);
1138 } else
Richard Smith1d489cf2012-11-01 04:30:05 +00001139 CPUName = A->getValue();
Daniel Dunbarf86fedd2009-11-14 22:04:54 +00001140 }
Daniel Dunbar6acda162009-09-09 22:33:08 +00001141
Daniel Dunbarf86fedd2009-11-14 22:04:54 +00001142 // Select the default CPU if none was given (or detection failed).
1143 if (!CPUName) {
Daniel Dunbar6acda162009-09-09 22:33:08 +00001144 // FIXME: Need target hooks.
Bob Wilson905c45f2011-10-14 05:03:44 +00001145 if (getToolChain().getTriple().isOSDarwin()) {
Daniel Dunbar951733d2011-05-31 15:58:55 +00001146 if (getToolChain().getArch() == llvm::Triple::x86_64)
Daniel Dunbarf86fedd2009-11-14 22:04:54 +00001147 CPUName = "core2";
Daniel Dunbar951733d2011-05-31 15:58:55 +00001148 else if (getToolChain().getArch() == llvm::Triple::x86)
Daniel Dunbarf86fedd2009-11-14 22:04:54 +00001149 CPUName = "yonah";
Chris Lattner86ed3a32010-04-11 19:29:39 +00001150 } else if (getToolChain().getOS().startswith("haiku")) {
Daniel Dunbar951733d2011-05-31 15:58:55 +00001151 if (getToolChain().getArch() == llvm::Triple::x86_64)
Chris Lattner86ed3a32010-04-11 19:29:39 +00001152 CPUName = "x86-64";
Daniel Dunbar951733d2011-05-31 15:58:55 +00001153 else if (getToolChain().getArch() == llvm::Triple::x86)
Chris Lattner86ed3a32010-04-11 19:29:39 +00001154 CPUName = "i586";
Daniel Dunbar95c04572010-08-01 23:13:54 +00001155 } else if (getToolChain().getOS().startswith("openbsd")) {
Daniel Dunbar951733d2011-05-31 15:58:55 +00001156 if (getToolChain().getArch() == llvm::Triple::x86_64)
Daniel Dunbar95c04572010-08-01 23:13:54 +00001157 CPUName = "x86-64";
Daniel Dunbar951733d2011-05-31 15:58:55 +00001158 else if (getToolChain().getArch() == llvm::Triple::x86)
Daniel Dunbar95c04572010-08-01 23:13:54 +00001159 CPUName = "i486";
Eli Friedman42f74f22012-08-08 23:57:20 +00001160 } else if (getToolChain().getOS().startswith("bitrig")) {
1161 if (getToolChain().getArch() == llvm::Triple::x86_64)
1162 CPUName = "x86-64";
1163 else if (getToolChain().getArch() == llvm::Triple::x86)
1164 CPUName = "i686";
Roman Divacky451f8ca2011-03-01 18:11:37 +00001165 } else if (getToolChain().getOS().startswith("freebsd")) {
Daniel Dunbar951733d2011-05-31 15:58:55 +00001166 if (getToolChain().getArch() == llvm::Triple::x86_64)
Roman Divacky451f8ca2011-03-01 18:11:37 +00001167 CPUName = "x86-64";
Daniel Dunbar951733d2011-05-31 15:58:55 +00001168 else if (getToolChain().getArch() == llvm::Triple::x86)
Roman Divacky451f8ca2011-03-01 18:11:37 +00001169 CPUName = "i486";
Benjamin Kramer8e50a962011-02-02 18:59:27 +00001170 } else if (getToolChain().getOS().startswith("netbsd")) {
Daniel Dunbar951733d2011-05-31 15:58:55 +00001171 if (getToolChain().getArch() == llvm::Triple::x86_64)
Benjamin Kramer8e50a962011-02-02 18:59:27 +00001172 CPUName = "x86-64";
Daniel Dunbar951733d2011-05-31 15:58:55 +00001173 else if (getToolChain().getArch() == llvm::Triple::x86)
Benjamin Kramer8e50a962011-02-02 18:59:27 +00001174 CPUName = "i486";
Daniel Dunbar6acda162009-09-09 22:33:08 +00001175 } else {
Daniel Dunbar951733d2011-05-31 15:58:55 +00001176 if (getToolChain().getArch() == llvm::Triple::x86_64)
Daniel Dunbarf86fedd2009-11-14 22:04:54 +00001177 CPUName = "x86-64";
Daniel Dunbar951733d2011-05-31 15:58:55 +00001178 else if (getToolChain().getArch() == llvm::Triple::x86)
Rafael Espindola715852c2012-11-02 20:41:30 +00001179 // All x86 devices running Android have core2 as their common
1180 // denominator. This makes a better choice than pentium4.
1181 CPUName = isAndroid ? "core2" : "pentium4";
Daniel Dunbar6acda162009-09-09 22:33:08 +00001182 }
1183 }
1184
Daniel Dunbarf86fedd2009-11-14 22:04:54 +00001185 if (CPUName) {
Daniel Dunbar38b48af2009-12-18 06:30:12 +00001186 CmdArgs.push_back("-target-cpu");
Daniel Dunbarf86fedd2009-11-14 22:04:54 +00001187 CmdArgs.push_back(CPUName);
1188 }
1189
Eli Friedmand18eeca2011-07-02 00:34:19 +00001190 // The required algorithm here is slightly strange: the options are applied
1191 // in order (so -mno-sse -msse2 disables SSE3), but any option that gets
1192 // directly overridden later is ignored (so "-mno-sse -msse2 -mno-sse2 -msse"
1193 // is equivalent to "-mno-sse2 -msse"). The -cc1 handling deals with the
1194 // former correctly, but not the latter; handle directly-overridden
1195 // attributes here.
1196 llvm::StringMap<unsigned> PrevFeature;
1197 std::vector<const char*> Features;
Daniel Dunbarcdd96862009-11-25 11:53:23 +00001198 for (arg_iterator it = Args.filtered_begin(options::OPT_m_x86_Features_Group),
1199 ie = Args.filtered_end(); it != ie; ++it) {
Chris Lattner5f9e2722011-07-23 10:55:15 +00001200 StringRef Name = (*it)->getOption().getName();
Daniel Dunbar7e4953e2010-06-11 22:00:13 +00001201 (*it)->claim();
Daniel Dunbar6acda162009-09-09 22:33:08 +00001202
Daniel Dunbarcdd96862009-11-25 11:53:23 +00001203 // Skip over "-m".
Michael J. Spencerc6357102012-10-22 22:13:48 +00001204 assert(Name.startswith("m") && "Invalid feature name.");
1205 Name = Name.substr(1);
Daniel Dunbar6acda162009-09-09 22:33:08 +00001206
Daniel Dunbarcdd96862009-11-25 11:53:23 +00001207 bool IsNegative = Name.startswith("no-");
1208 if (IsNegative)
1209 Name = Name.substr(3);
Daniel Dunbar6acda162009-09-09 22:33:08 +00001210
Eli Friedmand18eeca2011-07-02 00:34:19 +00001211 unsigned& Prev = PrevFeature[Name];
1212 if (Prev)
1213 Features[Prev - 1] = 0;
1214 Prev = Features.size() + 1;
1215 Features.push_back(Args.MakeArgString((IsNegative ? "-" : "+") + Name));
1216 }
1217 for (unsigned i = 0; i < Features.size(); i++) {
1218 if (Features[i]) {
1219 CmdArgs.push_back("-target-feature");
1220 CmdArgs.push_back(Features[i]);
1221 }
Daniel Dunbar6acda162009-09-09 22:33:08 +00001222 }
1223}
1224
Tony Linthicum96319392011-12-12 21:14:55 +00001225static Arg* getLastHexagonArchArg (const ArgList &Args)
1226{
1227 Arg * A = NULL;
1228
Sebastian Pop43115d42012-01-13 20:37:10 +00001229 for (ArgList::const_iterator it = Args.begin(), ie = Args.end();
1230 it != ie; ++it) {
1231 if ((*it)->getOption().matches(options::OPT_march_EQ) ||
Tony Linthicum96319392011-12-12 21:14:55 +00001232 (*it)->getOption().matches(options::OPT_mcpu_EQ)) {
1233 A = *it;
1234 A->claim();
1235 }
Sebastian Pop43115d42012-01-13 20:37:10 +00001236 else if ((*it)->getOption().matches(options::OPT_m_Joined)){
Richard Smith1d489cf2012-11-01 04:30:05 +00001237 StringRef Value = (*it)->getValue(0);
Sebastian Pop43115d42012-01-13 20:37:10 +00001238 if (Value.startswith("v")) {
1239 A = *it;
1240 A->claim();
1241 }
1242 }
Tony Linthicum96319392011-12-12 21:14:55 +00001243 }
1244 return A;
1245}
1246
Sebastian Pop43115d42012-01-13 20:37:10 +00001247static StringRef getHexagonTargetCPU(const ArgList &Args)
Tony Linthicum96319392011-12-12 21:14:55 +00001248{
1249 Arg *A;
1250 llvm::StringRef WhichHexagon;
1251
Sebastian Pop43115d42012-01-13 20:37:10 +00001252 // Select the default CPU (v4) if none was given or detection failed.
Tony Linthicum96319392011-12-12 21:14:55 +00001253 if ((A = getLastHexagonArchArg (Args))) {
Richard Smith1d489cf2012-11-01 04:30:05 +00001254 WhichHexagon = A->getValue();
Sebastian Pop43115d42012-01-13 20:37:10 +00001255 if (WhichHexagon == "")
1256 return "v4";
1257 else
1258 return WhichHexagon;
Tony Linthicum96319392011-12-12 21:14:55 +00001259 }
Sebastian Pop43115d42012-01-13 20:37:10 +00001260 else
1261 return "v4";
Tony Linthicum96319392011-12-12 21:14:55 +00001262}
1263
1264void Clang::AddHexagonTargetArgs(const ArgList &Args,
1265 ArgStringList &CmdArgs) const {
1266 llvm::Triple Triple = getToolChain().getTriple();
1267
1268 CmdArgs.push_back("-target-cpu");
Sebastian Pop43115d42012-01-13 20:37:10 +00001269 CmdArgs.push_back(Args.MakeArgString("hexagon" + getHexagonTargetCPU(Args)));
Tony Linthicum96319392011-12-12 21:14:55 +00001270 CmdArgs.push_back("-fno-signed-char");
1271 CmdArgs.push_back("-nobuiltininc");
1272
Sirish Pande5f9688b2012-05-10 20:19:54 +00001273 if (Args.hasArg(options::OPT_mqdsp6_compat))
Tony Linthicum96319392011-12-12 21:14:55 +00001274 CmdArgs.push_back("-mqdsp6-compat");
1275
1276 if (Arg *A = Args.getLastArg(options::OPT_G,
1277 options::OPT_msmall_data_threshold_EQ)) {
1278 std::string SmallDataThreshold="-small-data-threshold=";
Richard Smith1d489cf2012-11-01 04:30:05 +00001279 SmallDataThreshold += A->getValue();
Tony Linthicum96319392011-12-12 21:14:55 +00001280 CmdArgs.push_back ("-mllvm");
1281 CmdArgs.push_back(Args.MakeArgString(SmallDataThreshold));
1282 A->claim();
1283 }
1284
Sirish Pande5f9688b2012-05-10 20:19:54 +00001285 if (!Args.hasArg(options::OPT_fno_short_enums))
1286 CmdArgs.push_back("-fshort-enums");
1287 if (Args.getLastArg(options::OPT_mieee_rnd_near)) {
1288 CmdArgs.push_back ("-mllvm");
1289 CmdArgs.push_back ("-enable-hexagon-ieee-rnd-near");
1290 }
Tony Linthicum96319392011-12-12 21:14:55 +00001291 CmdArgs.push_back ("-mllvm");
1292 CmdArgs.push_back ("-machine-sink-split=0");
1293}
1294
Eric Christopher88b7cf02011-08-19 00:30:14 +00001295static bool
John McCall260611a2012-06-20 06:18:46 +00001296shouldUseExceptionTablesForObjCExceptions(const ObjCRuntime &runtime,
Anders Carlsson525544d2011-02-28 00:44:51 +00001297 const llvm::Triple &Triple) {
1298 // We use the zero-cost exception tables for Objective-C if the non-fragile
1299 // ABI is enabled or when compiling for x86_64 and ARM on Snow Leopard and
1300 // later.
John McCall260611a2012-06-20 06:18:46 +00001301 if (runtime.isNonFragile())
Anders Carlsson525544d2011-02-28 00:44:51 +00001302 return true;
1303
Bob Wilson905c45f2011-10-14 05:03:44 +00001304 if (!Triple.isOSDarwin())
Anders Carlsson525544d2011-02-28 00:44:51 +00001305 return false;
1306
Eric Christopheraa7333c2011-07-02 00:20:22 +00001307 return (!Triple.isMacOSXVersionLT(10,5) &&
Anders Carlsson525544d2011-02-28 00:44:51 +00001308 (Triple.getArch() == llvm::Triple::x86_64 ||
Eric Christopher88b7cf02011-08-19 00:30:14 +00001309 Triple.getArch() == llvm::Triple::arm));
Anders Carlsson525544d2011-02-28 00:44:51 +00001310}
1311
Anders Carlsson15348ae2011-02-28 02:27:16 +00001312/// addExceptionArgs - Adds exception related arguments to the driver command
1313/// arguments. There's a master flag, -fexceptions and also language specific
1314/// flags to enable/disable C++ and Objective-C exceptions.
1315/// This makes it possible to for example disable C++ exceptions but enable
1316/// Objective-C exceptions.
1317static void addExceptionArgs(const ArgList &Args, types::ID InputType,
1318 const llvm::Triple &Triple,
Fariborz Jahanian15b77312012-04-04 18:28:00 +00001319 bool KernelOrKext,
John McCall260611a2012-06-20 06:18:46 +00001320 const ObjCRuntime &objcRuntime,
Anders Carlsson15348ae2011-02-28 02:27:16 +00001321 ArgStringList &CmdArgs) {
Chad Rosierafc4baa2012-03-26 22:04:46 +00001322 if (KernelOrKext) {
1323 // -mkernel and -fapple-kext imply no exceptions, so claim exception related
1324 // arguments now to avoid warnings about unused arguments.
1325 Args.ClaimAllArgs(options::OPT_fexceptions);
1326 Args.ClaimAllArgs(options::OPT_fno_exceptions);
1327 Args.ClaimAllArgs(options::OPT_fobjc_exceptions);
1328 Args.ClaimAllArgs(options::OPT_fno_objc_exceptions);
1329 Args.ClaimAllArgs(options::OPT_fcxx_exceptions);
1330 Args.ClaimAllArgs(options::OPT_fno_cxx_exceptions);
Anders Carlsson15348ae2011-02-28 02:27:16 +00001331 return;
Chad Rosierafc4baa2012-03-26 22:04:46 +00001332 }
Anders Carlsson15348ae2011-02-28 02:27:16 +00001333
1334 // Exceptions are enabled by default.
1335 bool ExceptionsEnabled = true;
1336
1337 // This keeps track of whether exceptions were explicitly turned on or off.
1338 bool DidHaveExplicitExceptionFlag = false;
1339
Rafael Espindolaf759df02009-10-01 13:33:33 +00001340 if (Arg *A = Args.getLastArg(options::OPT_fexceptions,
1341 options::OPT_fno_exceptions)) {
1342 if (A->getOption().matches(options::OPT_fexceptions))
Anders Carlsson15348ae2011-02-28 02:27:16 +00001343 ExceptionsEnabled = true;
Eric Christopher88b7cf02011-08-19 00:30:14 +00001344 else
Anders Carlsson15348ae2011-02-28 02:27:16 +00001345 ExceptionsEnabled = false;
1346
1347 DidHaveExplicitExceptionFlag = true;
Rafael Espindolaf759df02009-10-01 13:33:33 +00001348 }
Daniel Dunbar1a2cd4f2010-09-14 23:12:31 +00001349
Anders Carlsson15348ae2011-02-28 02:27:16 +00001350 bool ShouldUseExceptionTables = false;
Fariborz Jahanian85caf032009-10-01 20:30:46 +00001351
Anders Carlsson15348ae2011-02-28 02:27:16 +00001352 // Exception tables and cleanups can be enabled with -fexceptions even if the
1353 // language itself doesn't support exceptions.
1354 if (ExceptionsEnabled && DidHaveExplicitExceptionFlag)
1355 ShouldUseExceptionTables = true;
Daniel Dunbar1a2cd4f2010-09-14 23:12:31 +00001356
Daniel Dunbard47ea692011-03-17 23:28:31 +00001357 // Obj-C exceptions are enabled by default, regardless of -fexceptions. This
1358 // is not necessarily sensible, but follows GCC.
1359 if (types::isObjC(InputType) &&
Eric Christopher88b7cf02011-08-19 00:30:14 +00001360 Args.hasFlag(options::OPT_fobjc_exceptions,
Daniel Dunbard47ea692011-03-17 23:28:31 +00001361 options::OPT_fno_objc_exceptions,
1362 true)) {
1363 CmdArgs.push_back("-fobjc-exceptions");
Anders Carlsson15348ae2011-02-28 02:27:16 +00001364
Eric Christopher88b7cf02011-08-19 00:30:14 +00001365 ShouldUseExceptionTables |=
John McCall260611a2012-06-20 06:18:46 +00001366 shouldUseExceptionTablesForObjCExceptions(objcRuntime, Triple);
Anders Carlsson15348ae2011-02-28 02:27:16 +00001367 }
1368
1369 if (types::isCXX(InputType)) {
1370 bool CXXExceptionsEnabled = ExceptionsEnabled;
1371
Eric Christopher88b7cf02011-08-19 00:30:14 +00001372 if (Arg *A = Args.getLastArg(options::OPT_fcxx_exceptions,
1373 options::OPT_fno_cxx_exceptions,
Anders Carlsson15348ae2011-02-28 02:27:16 +00001374 options::OPT_fexceptions,
1375 options::OPT_fno_exceptions)) {
1376 if (A->getOption().matches(options::OPT_fcxx_exceptions))
1377 CXXExceptionsEnabled = true;
Chandler Carruth43f220f2011-02-28 07:25:18 +00001378 else if (A->getOption().matches(options::OPT_fno_cxx_exceptions))
Anders Carlsson15348ae2011-02-28 02:27:16 +00001379 CXXExceptionsEnabled = false;
1380 }
1381
1382 if (CXXExceptionsEnabled) {
1383 CmdArgs.push_back("-fcxx-exceptions");
1384
1385 ShouldUseExceptionTables = true;
1386 }
1387 }
1388
1389 if (ShouldUseExceptionTables)
1390 CmdArgs.push_back("-fexceptions");
Rafael Espindolaf759df02009-10-01 13:33:33 +00001391}
1392
Rafael Espindola61b1efe2011-05-02 17:43:32 +00001393static bool ShouldDisableCFI(const ArgList &Args,
1394 const ToolChain &TC) {
Rafael Espindola701ec8d2012-03-08 14:39:55 +00001395 bool Default = true;
Bob Wilson905c45f2011-10-14 05:03:44 +00001396 if (TC.getTriple().isOSDarwin()) {
Rafael Espindola97f6abb2011-05-17 16:26:17 +00001397 // The native darwin assembler doesn't support cfi directives, so
Rafael Espindolacb773922011-05-17 19:06:58 +00001398 // we disable them if we think the .s file will be passed to it.
Rafael Espindola701ec8d2012-03-08 14:39:55 +00001399 Default = Args.hasFlag(options::OPT_integrated_as,
1400 options::OPT_no_integrated_as,
1401 TC.IsIntegratedAssemblerDefault());
Rafael Espindola97f6abb2011-05-17 16:26:17 +00001402 }
Rafael Espindola701ec8d2012-03-08 14:39:55 +00001403 return !Args.hasFlag(options::OPT_fdwarf2_cfi_asm,
1404 options::OPT_fno_dwarf2_cfi_asm,
1405 Default);
Rafael Espindola61b1efe2011-05-02 17:43:32 +00001406}
1407
Nick Lewyckyea523d72011-10-17 23:05:52 +00001408static bool ShouldDisableDwarfDirectory(const ArgList &Args,
1409 const ToolChain &TC) {
1410 bool IsIADefault = TC.IsIntegratedAssemblerDefault();
1411 bool UseIntegratedAs = Args.hasFlag(options::OPT_integrated_as,
1412 options::OPT_no_integrated_as,
1413 IsIADefault);
1414 bool UseDwarfDirectory = Args.hasFlag(options::OPT_fdwarf_directory_asm,
1415 options::OPT_fno_dwarf_directory_asm,
1416 UseIntegratedAs);
1417 return !UseDwarfDirectory;
1418}
1419
Joerg Sonnenberger359cf922011-05-06 14:35:16 +00001420/// \brief Check whether the given input tree contains any compilation actions.
1421static bool ContainsCompileAction(const Action *A) {
Chris Lattner5f9e2722011-07-23 10:55:15 +00001422 if (isa<CompileJobAction>(A))
Joerg Sonnenberger359cf922011-05-06 14:35:16 +00001423 return true;
1424
1425 for (Action::const_iterator it = A->begin(), ie = A->end(); it != ie; ++it)
1426 if (ContainsCompileAction(*it))
1427 return true;
1428
1429 return false;
1430}
1431
1432/// \brief Check if -relax-all should be passed to the internal assembler.
1433/// This is done by default when compiling non-assembler source with -O0.
1434static bool UseRelaxAll(Compilation &C, const ArgList &Args) {
1435 bool RelaxDefault = true;
1436
1437 if (Arg *A = Args.getLastArg(options::OPT_O_Group))
1438 RelaxDefault = A->getOption().matches(options::OPT_O0);
1439
1440 if (RelaxDefault) {
1441 RelaxDefault = false;
1442 for (ActionList::const_iterator it = C.getActions().begin(),
1443 ie = C.getActions().end(); it != ie; ++it) {
1444 if (ContainsCompileAction(*it)) {
1445 RelaxDefault = true;
1446 break;
1447 }
1448 }
1449 }
1450
1451 return Args.hasFlag(options::OPT_mrelax_all, options::OPT_mno_relax_all,
1452 RelaxDefault);
1453}
1454
Richard Smithc4dabad2012-11-05 22:04:41 +00001455namespace {
1456struct SanitizerArgs {
1457 /// Assign ordinals to sanitizer flags. We'll use the ordinal values as
1458 /// bit positions within \c Kind.
1459 enum SanitizeOrdinal {
1460#define SANITIZER(NAME, ID) SO_##ID,
1461#include "clang/Basic/Sanitizers.def"
1462 SO_Count
1463 };
1464
1465 /// Bugs to catch at runtime.
1466 enum SanitizeKind {
1467#define SANITIZER(NAME, ID) ID = 1 << SO_##ID,
1468#define SANITIZER_GROUP(NAME, ID, ALIAS) ID = ALIAS,
1469#include "clang/Basic/Sanitizers.def"
1470
1471 NeedsAsanRt = Address,
1472 NeedsTsanRt = Thread,
1473 NeedsUbsanRt = Undefined
1474 };
1475 unsigned Kind;
1476
1477 SanitizerArgs() : Kind(0) {}
1478
1479 bool needsAsanRt() const { return Kind & NeedsAsanRt; }
1480 bool needsTsanRt() const { return Kind & NeedsTsanRt; }
1481 bool needsUbsanRt() const { return Kind & NeedsUbsanRt; }
1482
1483 /// Parse a single value from a -fsanitize= or -fno-sanitize= value list.
1484 /// Returns a member of the \c SanitizeKind enumeration, or \c 0 if \p Value
1485 /// is not known.
1486 static unsigned parse(const char *Value) {
1487 return llvm::StringSwitch<SanitizeKind>(Value)
1488#define SANITIZER(NAME, ID) .Case(NAME, ID)
1489#define SANITIZER_GROUP(NAME, ID, ALIAS) .Case(NAME, ID)
1490#include "clang/Basic/Sanitizers.def"
1491 .Default(SanitizeKind());
1492 }
1493
1494 /// Parse a -fsanitize= or -fno-sanitize= argument's values, diagnosing any
1495 /// invalid components.
1496 static unsigned parse(const Driver &D, const Arg *A) {
1497 unsigned Kind = 0;
1498 for (unsigned I = 0, N = A->getNumValues(); I != N; ++I) {
1499 if (unsigned K = parse(A->getValue(I)))
1500 Kind |= K;
1501 else
1502 D.Diag(diag::err_drv_unsupported_option_argument)
1503 << A->getOption().getName() << A->getValue(I);
1504 }
1505 return Kind;
1506 }
1507
1508 void addArgs(const ArgList &Args, ArgStringList &CmdArgs) const {
Richard Smithc4dabad2012-11-05 22:04:41 +00001509 if (!Kind)
1510 return;
1511 llvm::SmallString<256> SanitizeOpt("-fsanitize=");
1512#define SANITIZER(NAME, ID) \
1513 if (Kind & ID) \
1514 SanitizeOpt += NAME ",";
1515#include "clang/Basic/Sanitizers.def"
1516 SanitizeOpt.pop_back();
1517 CmdArgs.push_back(Args.MakeArgString(SanitizeOpt));
1518 }
1519};
1520}
1521
1522/// Produce an argument string from argument \p A, which shows how it provides a
1523/// value in \p Mask. For instance, the argument "-fsanitize=address,alignment"
1524/// with mask \c NeedsUbsanRt would produce "-fsanitize=alignment".
Richard Smith04fd3822012-11-06 01:12:02 +00001525static std::string describeSanitizeArg(const ArgList &Args, const Arg *A,
1526 unsigned Mask) {
Richard Smithc4dabad2012-11-05 22:04:41 +00001527 if (!A->getOption().matches(options::OPT_fsanitize_EQ))
Richard Smith04fd3822012-11-06 01:12:02 +00001528 return A->getAsString(Args);
Richard Smithc4dabad2012-11-05 22:04:41 +00001529
1530 for (unsigned I = 0, N = A->getNumValues(); I != N; ++I)
1531 if (SanitizerArgs::parse(A->getValue(I)) & Mask)
1532 return std::string("-fsanitize=") + A->getValue(I);
1533
1534 llvm_unreachable("arg didn't provide expected value");
1535}
1536
1537/// Parse the sanitizer arguments from an argument list.
1538static SanitizerArgs getSanitizerArgs(const Driver &D, const ArgList &Args) {
1539 SanitizerArgs Sanitize;
1540
1541 const Arg *AsanArg, *TsanArg, *UbsanArg;
1542
1543 for (ArgList::const_iterator I = Args.begin(), E = Args.end(); I != E; ++I) {
1544 unsigned Add = 0, Remove = 0;
Richard Smith04fd3822012-11-06 01:12:02 +00001545 const char *DeprecatedReplacement = 0;
1546 if ((*I)->getOption().matches(options::OPT_faddress_sanitizer)) {
Richard Smithc4dabad2012-11-05 22:04:41 +00001547 Add = SanitizerArgs::Address;
Richard Smith04fd3822012-11-06 01:12:02 +00001548 DeprecatedReplacement = "-fsanitize=address";
1549 } else if ((*I)->getOption().matches(options::OPT_fno_address_sanitizer)) {
Richard Smithc4dabad2012-11-05 22:04:41 +00001550 Remove = SanitizerArgs::Address;
Richard Smith04fd3822012-11-06 01:12:02 +00001551 DeprecatedReplacement = "-fno-sanitize=address";
1552 } else if ((*I)->getOption().matches(options::OPT_fthread_sanitizer)) {
Richard Smithc4dabad2012-11-05 22:04:41 +00001553 Add = SanitizerArgs::Thread;
Richard Smith04fd3822012-11-06 01:12:02 +00001554 DeprecatedReplacement = "-fsanitize=thread";
1555 } else if ((*I)->getOption().matches(options::OPT_fno_thread_sanitizer)) {
Richard Smithc4dabad2012-11-05 22:04:41 +00001556 Remove = SanitizerArgs::Thread;
Richard Smith04fd3822012-11-06 01:12:02 +00001557 DeprecatedReplacement = "-fno-sanitize=thread";
1558 } else if ((*I)->getOption().matches(options::OPT_fcatch_undefined_behavior)) {
Richard Smithc4dabad2012-11-05 22:04:41 +00001559 Add = SanitizerArgs::Undefined;
Richard Smith04fd3822012-11-06 01:12:02 +00001560 DeprecatedReplacement = "-fsanitize=undefined";
1561 } else if ((*I)->getOption().matches(options::OPT_fsanitize_EQ)) {
Richard Smithc4dabad2012-11-05 22:04:41 +00001562 Add = SanitizerArgs::parse(D, *I);
Richard Smith04fd3822012-11-06 01:12:02 +00001563 } else if ((*I)->getOption().matches(options::OPT_fno_sanitize_EQ)) {
Richard Smithc4dabad2012-11-05 22:04:41 +00001564 Remove = SanitizerArgs::parse(D, *I);
Richard Smith04fd3822012-11-06 01:12:02 +00001565 } else {
Richard Smithc4dabad2012-11-05 22:04:41 +00001566 continue;
Richard Smith04fd3822012-11-06 01:12:02 +00001567 }
Richard Smithc4dabad2012-11-05 22:04:41 +00001568
1569 (*I)->claim();
1570
1571 Sanitize.Kind |= Add;
1572 Sanitize.Kind &= ~Remove;
1573
1574 if (Add & SanitizerArgs::NeedsAsanRt) AsanArg = *I;
1575 if (Add & SanitizerArgs::NeedsTsanRt) TsanArg = *I;
1576 if (Add & SanitizerArgs::NeedsUbsanRt) UbsanArg = *I;
Richard Smith04fd3822012-11-06 01:12:02 +00001577
1578 // If this is a deprecated synonym, produce a warning directing users
1579 // towards the new spelling.
1580 if (DeprecatedReplacement)
1581 D.Diag(diag::warn_drv_deprecated_arg)
1582 << (*I)->getAsString(Args) << DeprecatedReplacement;
Richard Smithc4dabad2012-11-05 22:04:41 +00001583 }
1584
1585 // Only one runtime library can be used at once.
1586 // FIXME: Allow Ubsan to be combined with the other two.
1587 bool NeedsAsan = Sanitize.needsAsanRt();
1588 bool NeedsTsan = Sanitize.needsTsanRt();
1589 bool NeedsUbsan = Sanitize.needsUbsanRt();
1590 if (NeedsAsan + NeedsTsan + NeedsUbsan > 1)
1591 D.Diag(diag::err_drv_argument_not_allowed_with)
Richard Smith04fd3822012-11-06 01:12:02 +00001592 << describeSanitizeArg(Args, NeedsAsan ? AsanArg : TsanArg,
Richard Smithc4dabad2012-11-05 22:04:41 +00001593 NeedsAsan ? SanitizerArgs::NeedsAsanRt
1594 : SanitizerArgs::NeedsTsanRt)
Richard Smith04fd3822012-11-06 01:12:02 +00001595 << describeSanitizeArg(Args, NeedsUbsan ? UbsanArg : TsanArg,
Richard Smithc4dabad2012-11-05 22:04:41 +00001596 NeedsUbsan ? SanitizerArgs::NeedsUbsanRt
1597 : SanitizerArgs::NeedsTsanRt);
1598
1599 return Sanitize;
1600}
1601
Kostya Serebryanydff466c2011-11-30 01:39:16 +00001602/// If AddressSanitizer is enabled, add appropriate linker flags (Linux).
1603/// This needs to be called before we add the C run-time (malloc, etc).
1604static void addAsanRTLinux(const ToolChain &TC, const ArgList &Args,
Kostya Serebryany7b5f1012011-12-06 19:18:44 +00001605 ArgStringList &CmdArgs) {
Logan Chien94a71422012-09-02 09:30:11 +00001606 if(TC.getTriple().getEnvironment() == llvm::Triple::Android) {
Evgeniy Stepanova6ddc022012-04-25 08:59:22 +00001607 if (!Args.hasArg(options::OPT_shared)) {
Evgeniy Stepanov83738622012-06-04 11:15:05 +00001608 if (!Args.hasArg(options::OPT_pie))
1609 TC.getDriver().Diag(diag::err_drv_asan_android_requires_pie);
Evgeniy Stepanova6ddc022012-04-25 08:59:22 +00001610 }
Daniel Dunbar8cd0d252011-12-07 23:22:17 +00001611
Evgeniy Stepanov8ba75412012-09-12 09:09:08 +00001612 SmallString<128> LibAsan(TC.getDriver().ResourceDir);
1613 llvm::sys::path::append(LibAsan, "lib", "linux",
1614 (Twine("libclang_rt.asan-") +
1615 TC.getArchName() + "-android.so"));
1616 CmdArgs.push_back(Args.MakeArgString(LibAsan));
Evgeniy Stepanova6ddc022012-04-25 08:59:22 +00001617 } else {
1618 if (!Args.hasArg(options::OPT_shared)) {
1619 // LibAsan is "libclang_rt.asan-<ArchName>.a" in the Linux library
1620 // resource directory.
1621 SmallString<128> LibAsan(TC.getDriver().ResourceDir);
1622 llvm::sys::path::append(LibAsan, "lib", "linux",
1623 (Twine("libclang_rt.asan-") +
1624 TC.getArchName() + ".a"));
1625 CmdArgs.push_back(Args.MakeArgString(LibAsan));
1626 CmdArgs.push_back("-lpthread");
1627 CmdArgs.push_back("-ldl");
1628 CmdArgs.push_back("-export-dynamic");
1629 }
1630 }
Kostya Serebryanydff466c2011-11-30 01:39:16 +00001631}
1632
Kostya Serebryanyf7efb0e2012-05-16 06:36:00 +00001633/// If ThreadSanitizer is enabled, add appropriate linker flags (Linux).
1634/// This needs to be called before we add the C run-time (malloc, etc).
1635static void addTsanRTLinux(const ToolChain &TC, const ArgList &Args,
1636 ArgStringList &CmdArgs) {
Kostya Serebryanyf7efb0e2012-05-16 06:36:00 +00001637 if (!Args.hasArg(options::OPT_shared)) {
1638 // LibTsan is "libclang_rt.tsan-<ArchName>.a" in the Linux library
1639 // resource directory.
1640 SmallString<128> LibTsan(TC.getDriver().ResourceDir);
1641 llvm::sys::path::append(LibTsan, "lib", "linux",
1642 (Twine("libclang_rt.tsan-") +
1643 TC.getArchName() + ".a"));
1644 CmdArgs.push_back(Args.MakeArgString(LibTsan));
1645 CmdArgs.push_back("-lpthread");
1646 CmdArgs.push_back("-ldl");
1647 CmdArgs.push_back("-export-dynamic");
1648 }
1649}
1650
Richard Smith4def70d2012-10-09 19:52:38 +00001651/// If UndefinedBehaviorSanitizer is enabled, add appropriate linker flags
1652/// (Linux).
1653static void addUbsanRTLinux(const ToolChain &TC, const ArgList &Args,
1654 ArgStringList &CmdArgs) {
Richard Smith4def70d2012-10-09 19:52:38 +00001655 if (!Args.hasArg(options::OPT_shared)) {
1656 // LibUbsan is "libclang_rt.ubsan-<ArchName>.a" in the Linux library
1657 // resource directory.
1658 SmallString<128> LibUbsan(TC.getDriver().ResourceDir);
1659 llvm::sys::path::append(LibUbsan, "lib", "linux",
1660 (Twine("libclang_rt.ubsan-") +
1661 TC.getArchName() + ".a"));
1662 CmdArgs.push_back(Args.MakeArgString(LibUbsan));
Richard Smitheb52aed2012-11-02 20:32:19 +00001663 CmdArgs.push_back("-lpthread");
Richard Smith4def70d2012-10-09 19:52:38 +00001664 }
1665}
1666
Rafael Espindola6af27ec2011-12-14 21:02:23 +00001667static bool shouldUseFramePointer(const ArgList &Args,
1668 const llvm::Triple &Triple) {
1669 if (Arg *A = Args.getLastArg(options::OPT_fno_omit_frame_pointer,
1670 options::OPT_fomit_frame_pointer))
1671 return A->getOption().matches(options::OPT_fno_omit_frame_pointer);
1672
Rafael Espindolaa2a17892011-12-14 21:50:24 +00001673 // Don't use a frame pointer on linux x86 and x86_64 if optimizing.
Rafael Espindola6af27ec2011-12-14 21:02:23 +00001674 if ((Triple.getArch() == llvm::Triple::x86_64 ||
1675 Triple.getArch() == llvm::Triple::x86) &&
1676 Triple.getOS() == llvm::Triple::Linux) {
1677 if (Arg *A = Args.getLastArg(options::OPT_O_Group))
1678 if (!A->getOption().matches(options::OPT_O0))
1679 return false;
1680 }
1681
1682 return true;
1683}
1684
Daniel Dunbar47ac7d22009-03-18 06:00:36 +00001685void Clang::ConstructJob(Compilation &C, const JobAction &JA,
Daniel Dunbar871adcf2009-03-18 07:06:02 +00001686 const InputInfo &Output,
Daniel Dunbar62cf6012009-03-18 06:07:59 +00001687 const InputInfoList &Inputs,
Daniel Dunbar1d460332009-03-18 10:01:51 +00001688 const ArgList &Args,
Daniel Dunbar47ac7d22009-03-18 06:00:36 +00001689 const char *LinkingOutput) const {
Daniel Dunbar0a80ba72010-03-20 04:52:14 +00001690 bool KernelOrKext = Args.hasArg(options::OPT_mkernel,
1691 options::OPT_fapple_kext);
Daniel Dunbaree788e72009-12-21 18:54:17 +00001692 const Driver &D = getToolChain().getDriver();
Daniel Dunbarb488c1d2009-03-18 08:07:30 +00001693 ArgStringList CmdArgs;
1694
Daniel Dunbar077ba6a2009-03-31 20:53:55 +00001695 assert(Inputs.size() == 1 && "Unable to handle multiple inputs.");
1696
Daniel Dunbar8ff5b282009-12-11 23:00:49 +00001697 // Invoke ourselves in -cc1 mode.
1698 //
1699 // FIXME: Implement custom jobs for internal actions.
1700 CmdArgs.push_back("-cc1");
1701
Daniel Dunbardd4fe002009-10-30 18:12:20 +00001702 // Add the "effective" target triple.
Daniel Dunbaraf07f932009-03-31 17:35:15 +00001703 CmdArgs.push_back("-triple");
Daniel Dunbar00577ad2010-08-23 22:35:37 +00001704 std::string TripleStr = getToolChain().ComputeEffectiveClangTriple(Args);
Daniel Dunbardd4fe002009-10-30 18:12:20 +00001705 CmdArgs.push_back(Args.MakeArgString(TripleStr));
Daniel Dunbar728a5122009-09-10 06:49:20 +00001706
Daniel Dunbardd4fe002009-10-30 18:12:20 +00001707 // Select the appropriate action.
John McCall260611a2012-06-20 06:18:46 +00001708 RewriteKind rewriteKind = RK_None;
Fariborz Jahaniane982cc02012-04-04 18:50:28 +00001709
Daniel Dunbar1d460332009-03-18 10:01:51 +00001710 if (isa<AnalyzeJobAction>(JA)) {
1711 assert(JA.getType() == types::TY_Plist && "Invalid output type.");
1712 CmdArgs.push_back("-analyze");
Ted Kremenek30660a82012-03-06 20:06:33 +00001713 } else if (isa<MigrateJobAction>(JA)) {
1714 CmdArgs.push_back("-migrate");
Daniel Dunbar1d460332009-03-18 10:01:51 +00001715 } else if (isa<PreprocessJobAction>(JA)) {
Daniel Dunbarcd8e4c42009-03-30 06:36:42 +00001716 if (Output.getType() == types::TY_Dependencies)
1717 CmdArgs.push_back("-Eonly");
1718 else
1719 CmdArgs.push_back("-E");
Daniel Dunbar8767cbc2010-02-03 03:07:56 +00001720 } else if (isa<AssembleJobAction>(JA)) {
1721 CmdArgs.push_back("-emit-obj");
Daniel Dunbar99298002010-05-27 06:18:05 +00001722
Joerg Sonnenberger359cf922011-05-06 14:35:16 +00001723 if (UseRelaxAll(C, Args))
Daniel Dunbar99298002010-05-27 06:18:05 +00001724 CmdArgs.push_back("-mrelax-all");
Daniel Dunbarca0e0542010-08-24 16:47:49 +00001725
Daniel Dunbarfcec10b2010-10-18 22:36:15 +00001726 // When using an integrated assembler, translate -Wa, and -Xassembler
1727 // options.
1728 for (arg_iterator it = Args.filtered_begin(options::OPT_Wa_COMMA,
1729 options::OPT_Xassembler),
1730 ie = Args.filtered_end(); it != ie; ++it) {
1731 const Arg *A = *it;
1732 A->claim();
1733
1734 for (unsigned i = 0, e = A->getNumValues(); i != e; ++i) {
Richard Smith1d489cf2012-11-01 04:30:05 +00001735 StringRef Value = A->getValue(i);
Daniel Dunbarfcec10b2010-10-18 22:36:15 +00001736
1737 if (Value == "-force_cpusubtype_ALL") {
1738 // Do nothing, this is the default and we don't support anything else.
Daniel Dunbarb14eed02010-10-28 20:36:23 +00001739 } else if (Value == "-L") {
Daniel Dunbar96932322011-03-28 22:49:28 +00001740 CmdArgs.push_back("-msave-temp-labels");
Joerg Sonnenberger46a49392011-05-19 20:46:39 +00001741 } else if (Value == "--fatal-warnings") {
Joerg Sonnenbergerd7933502011-05-19 18:42:29 +00001742 CmdArgs.push_back("-mllvm");
1743 CmdArgs.push_back("-fatal-assembler-warnings");
Nick Lewyckyc3b90142011-06-21 00:14:18 +00001744 } else if (Value == "--noexecstack") {
1745 CmdArgs.push_back("-mnoexecstack");
Daniel Dunbarfcec10b2010-10-18 22:36:15 +00001746 } else {
Chris Lattner5f9e2722011-07-23 10:55:15 +00001747 D.Diag(diag::err_drv_unsupported_option_argument)
Daniel Dunbarfcec10b2010-10-18 22:36:15 +00001748 << A->getOption().getName() << Value;
1749 }
1750 }
1751 }
Daniel Dunbard02bba82010-11-19 16:23:35 +00001752
1753 // Also ignore explicit -force_cpusubtype_ALL option.
1754 (void) Args.hasArg(options::OPT_force__cpusubtype__ALL);
Daniel Dunbar1d460332009-03-18 10:01:51 +00001755 } else if (isa<PrecompileJobAction>(JA)) {
Argyrios Kyrtzidise5c35372010-08-11 23:27:58 +00001756 // Use PCH if the user requested it.
Daniel Dunbar0ebd9322009-10-15 20:02:44 +00001757 bool UsePCH = D.CCCUsePCH;
Daniel Dunbar0ebd9322009-10-15 20:02:44 +00001758
Aaron Ballman761322b2012-07-31 01:21:00 +00001759 if (JA.getType() == types::TY_Nothing)
1760 CmdArgs.push_back("-fsyntax-only");
1761 else if (UsePCH)
Douglas Gregordf91ef32009-04-18 00:34:01 +00001762 CmdArgs.push_back("-emit-pch");
1763 else
1764 CmdArgs.push_back("-emit-pth");
Daniel Dunbar1d460332009-03-18 10:01:51 +00001765 } else {
1766 assert(isa<CompileJobAction>(JA) && "Invalid action for clang tool.");
Daniel Dunbarc21c4852009-04-08 23:54:23 +00001767
Daniel Dunbar1d460332009-03-18 10:01:51 +00001768 if (JA.getType() == types::TY_Nothing) {
1769 CmdArgs.push_back("-fsyntax-only");
Daniel Dunbar6c6424b2010-06-07 23:28:45 +00001770 } else if (JA.getType() == types::TY_LLVM_IR ||
1771 JA.getType() == types::TY_LTO_IR) {
Daniel Dunbar1d460332009-03-18 10:01:51 +00001772 CmdArgs.push_back("-emit-llvm");
Daniel Dunbar6c6424b2010-06-07 23:28:45 +00001773 } else if (JA.getType() == types::TY_LLVM_BC ||
1774 JA.getType() == types::TY_LTO_BC) {
Daniel Dunbar1d460332009-03-18 10:01:51 +00001775 CmdArgs.push_back("-emit-llvm-bc");
1776 } else if (JA.getType() == types::TY_PP_Asm) {
Daniel Dunbare3b8d072009-09-17 00:47:53 +00001777 CmdArgs.push_back("-S");
Daniel Dunbar5915fbf2009-09-01 16:57:46 +00001778 } else if (JA.getType() == types::TY_AST) {
1779 CmdArgs.push_back("-emit-pch");
Daniel Dunbar64952502010-02-11 03:16:21 +00001780 } else if (JA.getType() == types::TY_RewrittenObjC) {
1781 CmdArgs.push_back("-rewrite-objc");
John McCall260611a2012-06-20 06:18:46 +00001782 rewriteKind = RK_NonFragile;
Fariborz Jahanian582b3952012-04-02 15:59:19 +00001783 } else if (JA.getType() == types::TY_RewrittenLegacyObjC) {
1784 CmdArgs.push_back("-rewrite-objc");
John McCall260611a2012-06-20 06:18:46 +00001785 rewriteKind = RK_Fragile;
Daniel Dunbar64952502010-02-11 03:16:21 +00001786 } else {
1787 assert(JA.getType() == types::TY_PP_Asm &&
1788 "Unexpected output type!");
Daniel Dunbar1d460332009-03-18 10:01:51 +00001789 }
Daniel Dunbarb488c1d2009-03-18 08:07:30 +00001790 }
1791
Daniel Dunbar1d460332009-03-18 10:01:51 +00001792 // The make clang go fast button.
1793 CmdArgs.push_back("-disable-free");
1794
John McCallb689afb2010-02-13 03:50:24 +00001795 // Disable the verification pass in -asserts builds.
1796#ifdef NDEBUG
1797 CmdArgs.push_back("-disable-llvm-verifier");
1798#endif
1799
Daniel Dunbarc9abc042009-04-08 05:11:16 +00001800 // Set the main file name, so that debug info works even with
1801 // -save-temps.
1802 CmdArgs.push_back("-main-file-name");
1803 CmdArgs.push_back(darwin::CC1::getBaseInputName(Args, Inputs));
1804
Daniel Dunbar3bbc7532009-04-08 18:03:55 +00001805 // Some flags which affect the language (via preprocessor
1806 // defines). See darwin::CC1::AddCPPArgs.
1807 if (Args.hasArg(options::OPT_static))
1808 CmdArgs.push_back("-static-define");
1809
Daniel Dunbar1d460332009-03-18 10:01:51 +00001810 if (isa<AnalyzeJobAction>(JA)) {
Ted Kremenekb8bb3e72009-09-25 05:55:59 +00001811 // Enable region store model by default.
1812 CmdArgs.push_back("-analyzer-store=region");
1813
Ted Kremenekb40d06d2009-12-07 22:26:14 +00001814 // Treat blocks as analysis entry points.
1815 CmdArgs.push_back("-analyzer-opt-analyze-nested-blocks");
1816
Ted Kremenek51885072011-03-24 00:28:47 +00001817 CmdArgs.push_back("-analyzer-eagerly-assume");
1818
Daniel Dunbar1d460332009-03-18 10:01:51 +00001819 // Add default argument set.
Daniel Dunbard8fc0f22009-05-22 00:38:15 +00001820 if (!Args.hasArg(options::OPT__analyzer_no_default_checks)) {
Argyrios Kyrtzidis027a6ab2011-02-15 07:42:33 +00001821 CmdArgs.push_back("-analyzer-checker=core");
Ted Kremenek51885072011-03-24 00:28:47 +00001822
Argyrios Kyrtzidis027a6ab2011-02-15 07:42:33 +00001823 if (getToolChain().getTriple().getOS() != llvm::Triple::Win32)
1824 CmdArgs.push_back("-analyzer-checker=unix");
Ted Kremenek51885072011-03-24 00:28:47 +00001825
Argyrios Kyrtzidis027a6ab2011-02-15 07:42:33 +00001826 if (getToolChain().getTriple().getVendor() == llvm::Triple::Apple)
Ted Kremenek51885072011-03-24 00:28:47 +00001827 CmdArgs.push_back("-analyzer-checker=osx");
Ted Kremeneka8180e52012-01-20 06:00:17 +00001828
1829 CmdArgs.push_back("-analyzer-checker=deadcode");
Ted Kremenek8dc05062012-01-26 02:27:38 +00001830
1831 // Enable the following experimental checkers for testing.
Ted Kremenek8dc05062012-01-26 02:27:38 +00001832 CmdArgs.push_back("-analyzer-checker=security.insecureAPI.UncheckedReturn");
1833 CmdArgs.push_back("-analyzer-checker=security.insecureAPI.getpw");
1834 CmdArgs.push_back("-analyzer-checker=security.insecureAPI.gets");
1835 CmdArgs.push_back("-analyzer-checker=security.insecureAPI.mktemp");
1836 CmdArgs.push_back("-analyzer-checker=security.insecureAPI.mkstemp");
1837 CmdArgs.push_back("-analyzer-checker=security.insecureAPI.vfork");
Daniel Dunbard8fc0f22009-05-22 00:38:15 +00001838 }
Daniel Dunbarc21c4852009-04-08 23:54:23 +00001839
Daniel Dunbard8fc0f22009-05-22 00:38:15 +00001840 // Set the output format. The default is plist, for (lame) historical
1841 // reasons.
1842 CmdArgs.push_back("-analyzer-output");
1843 if (Arg *A = Args.getLastArg(options::OPT__analyzer_output))
Richard Smith1d489cf2012-11-01 04:30:05 +00001844 CmdArgs.push_back(A->getValue());
Daniel Dunbard8fc0f22009-05-22 00:38:15 +00001845 else
1846 CmdArgs.push_back("plist");
Daniel Dunbar1d460332009-03-18 10:01:51 +00001847
Ted Kremenek0647a7b2010-03-22 22:32:05 +00001848 // Disable the presentation of standard compiler warnings when
1849 // using --analyze. We only want to show static analyzer diagnostics
1850 // or frontend errors.
1851 CmdArgs.push_back("-w");
1852
Daniel Dunbar1d460332009-03-18 10:01:51 +00001853 // Add -Xanalyzer arguments when running as analyzer.
1854 Args.AddAllArgValues(CmdArgs, options::OPT_Xanalyzer);
Mike Stump1eb44332009-09-09 15:08:12 +00001855 }
1856
Daniel Dunbare2fd6642009-09-10 01:21:12 +00001857 CheckCodeGenerationOptions(D, Args);
1858
Daniel Dunbarbc85be82009-04-29 18:32:25 +00001859 // Perform argument translation for LLVM backend. This
1860 // takes some care in reconciling with llvm-gcc. The
1861 // issue is that llvm-gcc translates these options based on
1862 // the values in cc1, whereas we are processing based on
1863 // the driver arguments.
Daniel Dunbarc21c4852009-04-08 23:54:23 +00001864
Daniel Dunbarbc85be82009-04-29 18:32:25 +00001865 // This comes from the default translation the driver + cc1
1866 // would do to enable flag_pic.
Simon Atanasyan003ab662012-05-29 18:50:33 +00001867
1868 Arg *LastPICArg = Args.getLastArg(options::OPT_fPIC, options::OPT_fno_PIC,
1869 options::OPT_fpic, options::OPT_fno_pic,
1870 options::OPT_fPIE, options::OPT_fno_PIE,
1871 options::OPT_fpie, options::OPT_fno_pie);
Chandler Carruth5e219cf2012-04-08 16:40:35 +00001872 bool PICDisabled = false;
1873 bool PICEnabled = false;
1874 bool PICForPIE = false;
1875 if (LastPICArg) {
1876 PICForPIE = (LastPICArg->getOption().matches(options::OPT_fPIE) ||
1877 LastPICArg->getOption().matches(options::OPT_fpie));
1878 PICEnabled = (PICForPIE ||
1879 LastPICArg->getOption().matches(options::OPT_fPIC) ||
1880 LastPICArg->getOption().matches(options::OPT_fpic));
1881 PICDisabled = !PICEnabled;
1882 }
1883 // Note that these flags are trump-cards. Regardless of the order w.r.t. the
1884 // PIC or PIE options above, if these show up, PIC is disabled.
Daniel Dunbar7a0c0642012-10-15 22:23:53 +00001885 llvm::Triple Triple(TripleStr);
1886 if ((Args.hasArg(options::OPT_mkernel) ||
1887 Args.hasArg(options::OPT_fapple_kext)) &&
1888 (Triple.getOS() != llvm::Triple::IOS ||
1889 Triple.isOSVersionLT(6)))
Chandler Carruth5e219cf2012-04-08 16:40:35 +00001890 PICDisabled = true;
1891 if (Args.hasArg(options::OPT_static))
1892 PICDisabled = true;
1893 bool DynamicNoPIC = Args.hasArg(options::OPT_mdynamic_no_pic);
1894
1895 // Select the relocation model.
Daniel Dunbarbc85be82009-04-29 18:32:25 +00001896 const char *Model = getToolChain().GetForcedPicModel();
1897 if (!Model) {
Chandler Carruth5e219cf2012-04-08 16:40:35 +00001898 if (DynamicNoPIC)
Daniel Dunbarbc85be82009-04-29 18:32:25 +00001899 Model = "dynamic-no-pic";
1900 else if (PICDisabled)
1901 Model = "static";
1902 else if (PICEnabled)
1903 Model = "pic";
Daniel Dunbar1d460332009-03-18 10:01:51 +00001904 else
Daniel Dunbarbc85be82009-04-29 18:32:25 +00001905 Model = getToolChain().GetDefaultRelocationModel();
Daniel Dunbar1d460332009-03-18 10:01:51 +00001906 }
Chandler Carruth5e219cf2012-04-08 16:40:35 +00001907 StringRef ModelStr = Model ? Model : "";
1908 if (Model && ModelStr != "pic") {
Daniel Dunbarf219e7c2009-11-29 07:18:39 +00001909 CmdArgs.push_back("-mrelocation-model");
1910 CmdArgs.push_back(Model);
1911 }
Daniel Dunbarbc85be82009-04-29 18:32:25 +00001912
Chandler Carruth5e219cf2012-04-08 16:40:35 +00001913 // Infer the __PIC__ and __PIE__ values.
1914 if (ModelStr == "pic" && PICForPIE) {
1915 CmdArgs.push_back("-pie-level");
1916 CmdArgs.push_back((LastPICArg &&
1917 LastPICArg->getOption().matches(options::OPT_fPIE)) ?
1918 "2" : "1");
1919 } else if (ModelStr == "pic" || ModelStr == "dynamic-no-pic") {
Daniel Dunbar76743522009-11-29 02:39:08 +00001920 CmdArgs.push_back("-pic-level");
Chandler Carruth5e219cf2012-04-08 16:40:35 +00001921 CmdArgs.push_back(((ModelStr != "dynamic-no-pic" && LastPICArg &&
1922 LastPICArg->getOption().matches(options::OPT_fPIC)) ||
1923 getToolChain().getTriple().isOSDarwin()) ? "2" : "1");
Daniel Dunbarbc85be82009-04-29 18:32:25 +00001924 }
Chandler Carruth5e219cf2012-04-08 16:40:35 +00001925
Tanya Lattner59876c22009-11-04 01:18:09 +00001926 if (!Args.hasFlag(options::OPT_fmerge_all_constants,
1927 options::OPT_fno_merge_all_constants))
Chris Lattnerf44a1a02011-04-08 18:06:54 +00001928 CmdArgs.push_back("-fno-merge-all-constants");
Daniel Dunbar6bea73b2009-09-16 06:17:29 +00001929
Daniel Dunbarf219e7c2009-11-29 07:18:39 +00001930 // LLVM Code Generator Options.
1931
Daniel Dunbar17d3fea2011-02-09 17:54:19 +00001932 if (Arg *A = Args.getLastArg(options::OPT_mregparm_EQ)) {
1933 CmdArgs.push_back("-mregparm");
Richard Smith1d489cf2012-11-01 04:30:05 +00001934 CmdArgs.push_back(A->getValue());
Daniel Dunbar17d3fea2011-02-09 17:54:19 +00001935 }
1936
Roman Divackycfe9af22011-03-01 17:40:53 +00001937 if (Args.hasFlag(options::OPT_mrtd, options::OPT_mno_rtd, false))
1938 CmdArgs.push_back("-mrtd");
1939
Rafael Espindola6af27ec2011-12-14 21:02:23 +00001940 if (shouldUseFramePointer(Args, getToolChain().getTriple()))
Daniel Dunbarf219e7c2009-11-29 07:18:39 +00001941 CmdArgs.push_back("-mdisable-fp-elim");
1942 if (!Args.hasFlag(options::OPT_fzero_initialized_in_bss,
1943 options::OPT_fno_zero_initialized_in_bss))
1944 CmdArgs.push_back("-mno-zero-initialized-in-bss");
Daniel Dunbar398c6102011-02-04 02:20:39 +00001945 if (!Args.hasFlag(options::OPT_fstrict_aliasing,
1946 options::OPT_fno_strict_aliasing,
1947 getToolChain().IsStrictAliasingDefault()))
Dan Gohman4d5625e2010-10-14 22:36:56 +00001948 CmdArgs.push_back("-relaxed-aliasing");
Chandler Carruth82fe6ae2012-03-27 23:58:37 +00001949 if (Args.hasFlag(options::OPT_fstrict_enums, options::OPT_fno_strict_enums,
1950 false))
1951 CmdArgs.push_back("-fstrict-enums");
Nick Lewycky1db772b2012-01-23 08:29:12 +00001952 if (!Args.hasFlag(options::OPT_foptimize_sibling_calls,
1953 options::OPT_fno_optimize_sibling_calls))
1954 CmdArgs.push_back("-mdisable-tail-calls");
Daniel Dunbar1b718482010-05-14 22:00:22 +00001955
Chandler Carruthabf07a72012-01-02 14:19:45 +00001956 // Handle various floating point optimization flags, mapping them to the
1957 // appropriate LLVM code generation flags. The pattern for all of these is to
1958 // default off the codegen optimizations, and if any flag enables them and no
1959 // flag disables them after the flag enabling them, enable the codegen
1960 // optimization. This is complicated by several "umbrella" flags.
1961 if (Arg *A = Args.getLastArg(options::OPT_ffast_math,
Chad Rosier80ecf5e2012-09-25 22:03:25 +00001962 options::OPT_fno_fast_math,
Chandler Carruthabf07a72012-01-02 14:19:45 +00001963 options::OPT_ffinite_math_only,
1964 options::OPT_fno_finite_math_only,
1965 options::OPT_fhonor_infinities,
1966 options::OPT_fno_honor_infinities))
Chad Rosier80ecf5e2012-09-25 22:03:25 +00001967 if (A->getOption().getID() != options::OPT_fno_fast_math &&
1968 A->getOption().getID() != options::OPT_fno_finite_math_only &&
Chandler Carruthabf07a72012-01-02 14:19:45 +00001969 A->getOption().getID() != options::OPT_fhonor_infinities)
1970 CmdArgs.push_back("-menable-no-infs");
1971 if (Arg *A = Args.getLastArg(options::OPT_ffast_math,
Chad Rosier80ecf5e2012-09-25 22:03:25 +00001972 options::OPT_fno_fast_math,
Chandler Carruthabf07a72012-01-02 14:19:45 +00001973 options::OPT_ffinite_math_only,
1974 options::OPT_fno_finite_math_only,
1975 options::OPT_fhonor_nans,
1976 options::OPT_fno_honor_nans))
Chad Rosier80ecf5e2012-09-25 22:03:25 +00001977 if (A->getOption().getID() != options::OPT_fno_fast_math &&
1978 A->getOption().getID() != options::OPT_fno_finite_math_only &&
Chandler Carruthabf07a72012-01-02 14:19:45 +00001979 A->getOption().getID() != options::OPT_fhonor_nans)
1980 CmdArgs.push_back("-menable-no-nans");
1981
Benjamin Kramer769aa2d2012-05-02 14:55:48 +00001982 // -fmath-errno is the default on some platforms, e.g. BSD-derived OSes.
1983 bool MathErrno = getToolChain().IsMathErrnoDefault();
Chandler Carruthabf07a72012-01-02 14:19:45 +00001984 if (Arg *A = Args.getLastArg(options::OPT_ffast_math,
Chad Rosier80ecf5e2012-09-25 22:03:25 +00001985 options::OPT_fno_fast_math,
Chandler Carruthabf07a72012-01-02 14:19:45 +00001986 options::OPT_fmath_errno,
Chandler Carruth4f50c502012-04-26 02:10:51 +00001987 options::OPT_fno_math_errno))
1988 MathErrno = A->getOption().getID() == options::OPT_fmath_errno;
1989 if (MathErrno)
1990 CmdArgs.push_back("-fmath-errno");
Chandler Carruthabf07a72012-01-02 14:19:45 +00001991
1992 // There are several flags which require disabling very specific
1993 // optimizations. Any of these being disabled forces us to turn off the
1994 // entire set of LLVM optimizations, so collect them through all the flag
1995 // madness.
1996 bool AssociativeMath = false;
1997 if (Arg *A = Args.getLastArg(options::OPT_ffast_math,
Chad Rosier80ecf5e2012-09-25 22:03:25 +00001998 options::OPT_fno_fast_math,
Chandler Carruthabf07a72012-01-02 14:19:45 +00001999 options::OPT_funsafe_math_optimizations,
2000 options::OPT_fno_unsafe_math_optimizations,
2001 options::OPT_fassociative_math,
2002 options::OPT_fno_associative_math))
Chad Rosier80ecf5e2012-09-25 22:03:25 +00002003 if (A->getOption().getID() != options::OPT_fno_fast_math &&
2004 A->getOption().getID() != options::OPT_fno_unsafe_math_optimizations &&
Chandler Carruthabf07a72012-01-02 14:19:45 +00002005 A->getOption().getID() != options::OPT_fno_associative_math)
2006 AssociativeMath = true;
2007 bool ReciprocalMath = false;
2008 if (Arg *A = Args.getLastArg(options::OPT_ffast_math,
Chad Rosier80ecf5e2012-09-25 22:03:25 +00002009 options::OPT_fno_fast_math,
Chandler Carruthabf07a72012-01-02 14:19:45 +00002010 options::OPT_funsafe_math_optimizations,
2011 options::OPT_fno_unsafe_math_optimizations,
2012 options::OPT_freciprocal_math,
2013 options::OPT_fno_reciprocal_math))
Chad Rosier80ecf5e2012-09-25 22:03:25 +00002014 if (A->getOption().getID() != options::OPT_fno_fast_math &&
2015 A->getOption().getID() != options::OPT_fno_unsafe_math_optimizations &&
Chandler Carruthabf07a72012-01-02 14:19:45 +00002016 A->getOption().getID() != options::OPT_fno_reciprocal_math)
2017 ReciprocalMath = true;
2018 bool SignedZeros = true;
2019 if (Arg *A = Args.getLastArg(options::OPT_ffast_math,
Chad Rosier80ecf5e2012-09-25 22:03:25 +00002020 options::OPT_fno_fast_math,
Chandler Carruthabf07a72012-01-02 14:19:45 +00002021 options::OPT_funsafe_math_optimizations,
2022 options::OPT_fno_unsafe_math_optimizations,
2023 options::OPT_fsigned_zeros,
2024 options::OPT_fno_signed_zeros))
Chad Rosier80ecf5e2012-09-25 22:03:25 +00002025 if (A->getOption().getID() != options::OPT_fno_fast_math &&
2026 A->getOption().getID() != options::OPT_fno_unsafe_math_optimizations &&
Chandler Carruthabf07a72012-01-02 14:19:45 +00002027 A->getOption().getID() != options::OPT_fsigned_zeros)
2028 SignedZeros = false;
2029 bool TrappingMath = true;
2030 if (Arg *A = Args.getLastArg(options::OPT_ffast_math,
Chad Rosier80ecf5e2012-09-25 22:03:25 +00002031 options::OPT_fno_fast_math,
Chandler Carruthabf07a72012-01-02 14:19:45 +00002032 options::OPT_funsafe_math_optimizations,
2033 options::OPT_fno_unsafe_math_optimizations,
2034 options::OPT_ftrapping_math,
2035 options::OPT_fno_trapping_math))
Chad Rosier80ecf5e2012-09-25 22:03:25 +00002036 if (A->getOption().getID() != options::OPT_fno_fast_math &&
2037 A->getOption().getID() != options::OPT_fno_unsafe_math_optimizations &&
Chandler Carruthabf07a72012-01-02 14:19:45 +00002038 A->getOption().getID() != options::OPT_ftrapping_math)
2039 TrappingMath = false;
2040 if (!MathErrno && AssociativeMath && ReciprocalMath && !SignedZeros &&
2041 !TrappingMath)
2042 CmdArgs.push_back("-menable-unsafe-fp-math");
2043
Lang Hamesc9686712012-07-06 00:59:19 +00002044
2045 // Validate and pass through -fp-contract option.
2046 if (Arg *A = Args.getLastArg(options::OPT_ffast_math,
Chad Rosier80ecf5e2012-09-25 22:03:25 +00002047 options::OPT_fno_fast_math,
Lang Hamesc9686712012-07-06 00:59:19 +00002048 options::OPT_ffp_contract)) {
2049 if (A->getOption().getID() == options::OPT_ffp_contract) {
Richard Smith1d489cf2012-11-01 04:30:05 +00002050 StringRef Val = A->getValue();
Lang Hamesc9686712012-07-06 00:59:19 +00002051 if (Val == "fast" || Val == "on" || Val == "off") {
2052 CmdArgs.push_back(Args.MakeArgString("-ffp-contract=" + Val));
2053 } else {
2054 D.Diag(diag::err_drv_unsupported_option_argument)
2055 << A->getOption().getName() << Val;
2056 }
Chad Rosier80ecf5e2012-09-25 22:03:25 +00002057 } else if (A->getOption().getID() == options::OPT_ffast_math) {
Lang Hamesc9686712012-07-06 00:59:19 +00002058 // If fast-math is set then set the fp-contract mode to fast.
2059 CmdArgs.push_back(Args.MakeArgString("-ffp-contract=fast"));
2060 }
2061 }
2062
Bob Wilson455e72e2012-07-19 03:52:53 +00002063 // We separately look for the '-ffast-math' and '-ffinite-math-only' flags,
2064 // and if we find them, tell the frontend to provide the appropriate
2065 // preprocessor macros. This is distinct from enabling any optimizations as
2066 // these options induce language changes which must survive serialization
2067 // and deserialization, etc.
Chad Rosier80ecf5e2012-09-25 22:03:25 +00002068 if (Arg *A = Args.getLastArg(options::OPT_ffast_math, options::OPT_fno_fast_math))
2069 if (A->getOption().matches(options::OPT_ffast_math))
2070 CmdArgs.push_back("-ffast-math");
2071 if (Arg *A = Args.getLastArg(options::OPT_ffinite_math_only, options::OPT_fno_fast_math))
2072 if (A->getOption().matches(options::OPT_ffinite_math_only))
2073 CmdArgs.push_back("-ffinite-math-only");
Chandler Carruthabf07a72012-01-02 14:19:45 +00002074
Daniel Dunbar1b718482010-05-14 22:00:22 +00002075 // Decide whether to use verbose asm. Verbose assembly is the default on
2076 // toolchains which have the integrated assembler on by default.
2077 bool IsVerboseAsmDefault = getToolChain().IsIntegratedAssemblerDefault();
2078 if (Args.hasFlag(options::OPT_fverbose_asm, options::OPT_fno_verbose_asm,
Michael J. Spencer20249a12010-10-21 03:16:25 +00002079 IsVerboseAsmDefault) ||
Daniel Dunbar1b718482010-05-14 22:00:22 +00002080 Args.hasArg(options::OPT_dA))
Daniel Dunbarf219e7c2009-11-29 07:18:39 +00002081 CmdArgs.push_back("-masm-verbose");
Daniel Dunbar1b718482010-05-14 22:00:22 +00002082
Daniel Dunbarf219e7c2009-11-29 07:18:39 +00002083 if (Args.hasArg(options::OPT_fdebug_pass_structure)) {
2084 CmdArgs.push_back("-mdebug-pass");
2085 CmdArgs.push_back("Structure");
2086 }
2087 if (Args.hasArg(options::OPT_fdebug_pass_arguments)) {
2088 CmdArgs.push_back("-mdebug-pass");
2089 CmdArgs.push_back("Arguments");
2090 }
2091
John McCalld0c2ec42010-02-19 02:45:38 +00002092 // Enable -mconstructor-aliases except on darwin, where we have to
2093 // work around a linker bug; see <rdar://problem/7651567>.
Bob Wilson905c45f2011-10-14 05:03:44 +00002094 if (!getToolChain().getTriple().isOSDarwin())
John McCalld0c2ec42010-02-19 02:45:38 +00002095 CmdArgs.push_back("-mconstructor-aliases");
NAKAMURA Takumi125b4cb2011-02-17 08:50:50 +00002096
John McCall32096692011-03-18 02:56:14 +00002097 // Darwin's kernel doesn't support guard variables; just die if we
2098 // try to use them.
Bob Wilson905c45f2011-10-14 05:03:44 +00002099 if (KernelOrKext && getToolChain().getTriple().isOSDarwin())
John McCall32096692011-03-18 02:56:14 +00002100 CmdArgs.push_back("-fforbid-guard-variables");
2101
Douglas Gregor6f755502011-02-01 15:15:22 +00002102 if (Args.hasArg(options::OPT_mms_bitfields)) {
2103 CmdArgs.push_back("-mms-bitfields");
2104 }
John McCalld0c2ec42010-02-19 02:45:38 +00002105
Daniel Dunbar6bea73b2009-09-16 06:17:29 +00002106 // This is a coarse approximation of what llvm-gcc actually does, both
2107 // -fasynchronous-unwind-tables and -fnon-call-exceptions interact in more
2108 // complicated ways.
2109 bool AsynchronousUnwindTables =
2110 Args.hasFlag(options::OPT_fasynchronous_unwind_tables,
2111 options::OPT_fno_asynchronous_unwind_tables,
2112 getToolChain().IsUnwindTablesDefault() &&
Daniel Dunbar0a80ba72010-03-20 04:52:14 +00002113 !KernelOrKext);
Daniel Dunbar6bea73b2009-09-16 06:17:29 +00002114 if (Args.hasFlag(options::OPT_funwind_tables, options::OPT_fno_unwind_tables,
2115 AsynchronousUnwindTables))
Daniel Dunbarf219e7c2009-11-29 07:18:39 +00002116 CmdArgs.push_back("-munwind-tables");
2117
Rafael Espindola8af669f2012-06-19 01:26:10 +00002118 getToolChain().addClangTargetOptions(CmdArgs);
2119
Daniel Dunbarf219e7c2009-11-29 07:18:39 +00002120 if (Arg *A = Args.getLastArg(options::OPT_flimited_precision_EQ)) {
2121 CmdArgs.push_back("-mlimit-float-precision");
Richard Smith1d489cf2012-11-01 04:30:05 +00002122 CmdArgs.push_back(A->getValue());
Daniel Dunbarf219e7c2009-11-29 07:18:39 +00002123 }
Daniel Dunbarbc85be82009-04-29 18:32:25 +00002124
Daniel Dunbar868bd0a2009-05-06 03:16:41 +00002125 // FIXME: Handle -mtune=.
2126 (void) Args.hasArg(options::OPT_mtune_EQ);
Daniel Dunbarbc85be82009-04-29 18:32:25 +00002127
Benjamin Kramer8e9ef0d2009-08-05 14:30:52 +00002128 if (Arg *A = Args.getLastArg(options::OPT_mcmodel_EQ)) {
Daniel Dunbarf219e7c2009-11-29 07:18:39 +00002129 CmdArgs.push_back("-mcode-model");
Richard Smith1d489cf2012-11-01 04:30:05 +00002130 CmdArgs.push_back(A->getValue());
Benjamin Kramer8e9ef0d2009-08-05 14:30:52 +00002131 }
2132
Daniel Dunbar6acda162009-09-09 22:33:08 +00002133 // Add target specific cpu and features flags.
2134 switch(getToolChain().getTriple().getArch()) {
2135 default:
2136 break;
Daniel Dunbar868bd0a2009-05-06 03:16:41 +00002137
Daniel Dunbarb163ef72009-09-10 04:57:17 +00002138 case llvm::Triple::arm:
2139 case llvm::Triple::thumb:
Daniel Dunbarfa41d692011-03-17 17:10:06 +00002140 AddARMTargetArgs(Args, CmdArgs, KernelOrKext);
Daniel Dunbarb163ef72009-09-10 04:57:17 +00002141 break;
2142
Eric Christophered734732010-03-02 02:41:08 +00002143 case llvm::Triple::mips:
2144 case llvm::Triple::mipsel:
Akira Hatanaka7ec02582011-09-21 02:13:07 +00002145 case llvm::Triple::mips64:
2146 case llvm::Triple::mips64el:
Eric Christophered734732010-03-02 02:41:08 +00002147 AddMIPSTargetArgs(Args, CmdArgs);
2148 break;
2149
Hal Finkel02a84272012-06-11 22:35:19 +00002150 case llvm::Triple::ppc:
2151 case llvm::Triple::ppc64:
2152 AddPPCTargetArgs(Args, CmdArgs);
2153 break;
2154
Bruno Cardoso Lopes9284d212010-11-09 17:21:19 +00002155 case llvm::Triple::sparc:
2156 AddSparcTargetArgs(Args, CmdArgs);
2157 break;
2158
Daniel Dunbar6acda162009-09-09 22:33:08 +00002159 case llvm::Triple::x86:
2160 case llvm::Triple::x86_64:
2161 AddX86TargetArgs(Args, CmdArgs);
2162 break;
Tony Linthicum96319392011-12-12 21:14:55 +00002163
2164 case llvm::Triple::hexagon:
2165 AddHexagonTargetArgs(Args, CmdArgs);
2166 break;
Daniel Dunbarbc85be82009-04-29 18:32:25 +00002167 }
2168
Tony Linthicum96319392011-12-12 21:14:55 +00002169
2170
Daniel Dunbarc176bc62010-08-11 23:07:47 +00002171 // Pass the linker version in use.
2172 if (Arg *A = Args.getLastArg(options::OPT_mlinker_version_EQ)) {
2173 CmdArgs.push_back("-target-linker-version");
Richard Smith1d489cf2012-11-01 04:30:05 +00002174 CmdArgs.push_back(A->getValue());
Daniel Dunbarc176bc62010-08-11 23:07:47 +00002175 }
2176
Nick Lewyckyb2d11cc2011-02-02 06:43:03 +00002177 // -mno-omit-leaf-frame-pointer is the default on Darwin.
Daniel Dunbar1ad66482010-07-01 01:31:45 +00002178 if (Args.hasFlag(options::OPT_momit_leaf_frame_pointer,
Nick Lewyckyb2d11cc2011-02-02 06:43:03 +00002179 options::OPT_mno_omit_leaf_frame_pointer,
Bob Wilson905c45f2011-10-14 05:03:44 +00002180 !getToolChain().getTriple().isOSDarwin()))
Daniel Dunbar1ad66482010-07-01 01:31:45 +00002181 CmdArgs.push_back("-momit-leaf-frame-pointer");
2182
Daniel Dunbarb30575c2010-05-12 18:19:58 +00002183 // Explicitly error on some things we know we don't support and can't just
2184 // ignore.
2185 types::ID InputType = Inputs[0].getType();
Daniel Dunbare94db472010-09-24 19:39:37 +00002186 if (!Args.hasArg(options::OPT_fallow_unsupported)) {
2187 Arg *Unsupported;
Daniel Dunbare94db472010-09-24 19:39:37 +00002188 if (types::isCXX(InputType) &&
Bob Wilson905c45f2011-10-14 05:03:44 +00002189 getToolChain().getTriple().isOSDarwin() &&
Daniel Dunbare94db472010-09-24 19:39:37 +00002190 getToolChain().getTriple().getArch() == llvm::Triple::x86) {
Bob Wilsona544aee2011-08-13 23:48:55 +00002191 if ((Unsupported = Args.getLastArg(options::OPT_fapple_kext)) ||
2192 (Unsupported = Args.getLastArg(options::OPT_mkernel)))
Chris Lattner5f9e2722011-07-23 10:55:15 +00002193 D.Diag(diag::err_drv_clang_unsupported_opt_cxx_darwin_i386)
Daniel Dunbare94db472010-09-24 19:39:37 +00002194 << Unsupported->getOption().getName();
2195 }
Daniel Dunbarb30575c2010-05-12 18:19:58 +00002196 }
2197
Daniel Dunbar1d460332009-03-18 10:01:51 +00002198 Args.AddAllArgs(CmdArgs, options::OPT_v);
Daniel Dunbarf7c16d92010-08-24 22:44:13 +00002199 Args.AddLastArg(CmdArgs, options::OPT_H);
Chad Rosier2b819102011-08-02 17:58:04 +00002200 if (D.CCPrintHeaders && !D.CCGenDiagnostics) {
Daniel Dunbar322c29f2011-02-02 21:11:35 +00002201 CmdArgs.push_back("-header-include-file");
2202 CmdArgs.push_back(D.CCPrintHeadersFilename ?
2203 D.CCPrintHeadersFilename : "-");
2204 }
Daniel Dunbar1d460332009-03-18 10:01:51 +00002205 Args.AddLastArg(CmdArgs, options::OPT_P);
Mike Stump1eb44332009-09-09 15:08:12 +00002206 Args.AddLastArg(CmdArgs, options::OPT_print_ivar_layout);
Daniel Dunbar1d460332009-03-18 10:01:51 +00002207
Chad Rosier2b819102011-08-02 17:58:04 +00002208 if (D.CCLogDiagnostics && !D.CCGenDiagnostics) {
Daniel Dunbarc8a22b02011-04-07 18:01:20 +00002209 CmdArgs.push_back("-diagnostic-log-file");
2210 CmdArgs.push_back(D.CCLogDiagnosticsFilename ?
2211 D.CCLogDiagnosticsFilename : "-");
2212 }
2213
Alexey Samsonova9cd83b2012-05-29 08:10:34 +00002214 // Use the last option from "-g" group. "-gline-tables-only" is
2215 // preserved, all other debug options are substituted with "-g".
Rafael Espindola18f36d92010-03-07 04:46:18 +00002216 Args.ClaimAllArgs(options::OPT_g_Group);
Alexey Samsonova9cd83b2012-05-29 08:10:34 +00002217 if (Arg *A = Args.getLastArg(options::OPT_g_Group)) {
2218 if (A->getOption().matches(options::OPT_gline_tables_only)) {
2219 CmdArgs.push_back("-gline-tables-only");
Alexey Samsonov7f326072012-06-21 08:22:39 +00002220 } else if (!A->getOption().matches(options::OPT_g0) &&
2221 !A->getOption().matches(options::OPT_ggdb0)) {
Chad Rosiercf6ba2e2011-11-07 19:52:29 +00002222 CmdArgs.push_back("-g");
Chad Rosier2875bda2011-11-04 19:28:44 +00002223 }
Alexey Samsonova9cd83b2012-05-29 08:10:34 +00002224 }
Daniel Dunbar1d460332009-03-18 10:01:51 +00002225
Alexey Samsonov7f326072012-06-21 08:22:39 +00002226 // We ignore flags -gstrict-dwarf and -grecord-gcc-switches for now.
2227 Args.ClaimAllArgs(options::OPT_g_flags_Group);
Eric Christopherda3301e2012-10-18 21:52:18 +00002228 if (Args.hasArg(options::OPT_gcolumn_info))
2229 CmdArgs.push_back("-dwarf-column-info");
Alexey Samsonov7f326072012-06-21 08:22:39 +00002230
Rafael Espindola9cf933a2010-05-06 21:06:04 +00002231 Args.AddAllArgs(CmdArgs, options::OPT_ffunction_sections);
2232 Args.AddAllArgs(CmdArgs, options::OPT_fdata_sections);
2233
Chris Lattner7255a2d2010-06-22 00:03:40 +00002234 Args.AddAllArgs(CmdArgs, options::OPT_finstrument_functions);
2235
Nick Lewyckye8ba8d72011-04-21 23:44:07 +00002236 if (Args.hasArg(options::OPT_ftest_coverage) ||
2237 Args.hasArg(options::OPT_coverage))
2238 CmdArgs.push_back("-femit-coverage-notes");
2239 if (Args.hasArg(options::OPT_fprofile_arcs) ||
2240 Args.hasArg(options::OPT_coverage))
2241 CmdArgs.push_back("-femit-coverage-data");
2242
Nick Lewycky5ea4f442011-05-04 20:46:58 +00002243 if (C.getArgs().hasArg(options::OPT_c) ||
2244 C.getArgs().hasArg(options::OPT_S)) {
2245 if (Output.isFilename()) {
Nick Lewycky3dc05412011-05-05 00:08:20 +00002246 CmdArgs.push_back("-coverage-file");
Bill Wendlingecbbea42012-08-30 00:43:41 +00002247 SmallString<128> absFilename(Output.getFilename());
2248 llvm::sys::fs::make_absolute(absFilename);
2249 CmdArgs.push_back(Args.MakeArgString(absFilename));
Nick Lewycky5ea4f442011-05-04 20:46:58 +00002250 }
2251 }
2252
Daniel Dunbara268fc02011-10-11 18:20:10 +00002253 // Pass options for controlling the default header search paths.
2254 if (Args.hasArg(options::OPT_nostdinc)) {
2255 CmdArgs.push_back("-nostdsysteminc");
2256 CmdArgs.push_back("-nobuiltininc");
2257 } else {
Daniel Dunbar92d6d402011-10-11 18:20:16 +00002258 if (Args.hasArg(options::OPT_nostdlibinc))
2259 CmdArgs.push_back("-nostdsysteminc");
Daniel Dunbara268fc02011-10-11 18:20:10 +00002260 Args.AddLastArg(CmdArgs, options::OPT_nostdincxx);
2261 Args.AddLastArg(CmdArgs, options::OPT_nobuiltininc);
2262 }
Daniel Dunbar1d460332009-03-18 10:01:51 +00002263
Daniel Dunbar5f122322009-12-15 01:02:52 +00002264 // Pass the path to compiler resource files.
Daniel Dunbar5f122322009-12-15 01:02:52 +00002265 CmdArgs.push_back("-resource-dir");
Daniel Dunbar225c4172010-01-20 02:35:16 +00002266 CmdArgs.push_back(D.ResourceDir.c_str());
Daniel Dunbar2ac9fc22009-04-07 21:42:00 +00002267
Argyrios Kyrtzidis389db162010-11-03 22:45:23 +00002268 Args.AddLastArg(CmdArgs, options::OPT_working_directory);
2269
Ted Kremenek30660a82012-03-06 20:06:33 +00002270 bool ARCMTEnabled = false;
John McCall8f0e8d22011-06-15 23:25:17 +00002271 if (!Args.hasArg(options::OPT_fno_objc_arc)) {
Argyrios Kyrtzidis72ac1202011-07-07 04:00:39 +00002272 if (const Arg *A = Args.getLastArg(options::OPT_ccc_arcmt_check,
Argyrios Kyrtzidis69325d52011-07-09 20:00:58 +00002273 options::OPT_ccc_arcmt_modify,
2274 options::OPT_ccc_arcmt_migrate)) {
Ted Kremenek30660a82012-03-06 20:06:33 +00002275 ARCMTEnabled = true;
John McCall8f0e8d22011-06-15 23:25:17 +00002276 switch (A->getOption().getID()) {
2277 default:
2278 llvm_unreachable("missed a case");
Argyrios Kyrtzidis72ac1202011-07-07 04:00:39 +00002279 case options::OPT_ccc_arcmt_check:
John McCall8f0e8d22011-06-15 23:25:17 +00002280 CmdArgs.push_back("-arcmt-check");
2281 break;
Argyrios Kyrtzidis72ac1202011-07-07 04:00:39 +00002282 case options::OPT_ccc_arcmt_modify:
John McCall8f0e8d22011-06-15 23:25:17 +00002283 CmdArgs.push_back("-arcmt-modify");
2284 break;
Argyrios Kyrtzidis69325d52011-07-09 20:00:58 +00002285 case options::OPT_ccc_arcmt_migrate:
2286 CmdArgs.push_back("-arcmt-migrate");
Ted Kremenek30660a82012-03-06 20:06:33 +00002287 CmdArgs.push_back("-mt-migrate-directory");
Richard Smith1d489cf2012-11-01 04:30:05 +00002288 CmdArgs.push_back(A->getValue());
Argyrios Kyrtzidis7ee20492011-07-19 17:20:03 +00002289
2290 Args.AddLastArg(CmdArgs, options::OPT_arcmt_migrate_report_output);
2291 Args.AddLastArg(CmdArgs, options::OPT_arcmt_migrate_emit_arc_errors);
Argyrios Kyrtzidis69325d52011-07-09 20:00:58 +00002292 break;
John McCall8f0e8d22011-06-15 23:25:17 +00002293 }
2294 }
2295 }
Eric Christopher88b7cf02011-08-19 00:30:14 +00002296
Ted Kremenek30660a82012-03-06 20:06:33 +00002297 if (const Arg *A = Args.getLastArg(options::OPT_ccc_objcmt_migrate)) {
2298 if (ARCMTEnabled) {
2299 D.Diag(diag::err_drv_argument_not_allowed_with)
2300 << A->getAsString(Args) << "-ccc-arcmt-migrate";
2301 }
2302 CmdArgs.push_back("-mt-migrate-directory");
Richard Smith1d489cf2012-11-01 04:30:05 +00002303 CmdArgs.push_back(A->getValue());
Ted Kremenek30660a82012-03-06 20:06:33 +00002304
2305 if (!Args.hasArg(options::OPT_objcmt_migrate_literals,
2306 options::OPT_objcmt_migrate_subscripting)) {
2307 // None specified, means enable them all.
2308 CmdArgs.push_back("-objcmt-migrate-literals");
2309 CmdArgs.push_back("-objcmt-migrate-subscripting");
2310 } else {
2311 Args.AddLastArg(CmdArgs, options::OPT_objcmt_migrate_literals);
2312 Args.AddLastArg(CmdArgs, options::OPT_objcmt_migrate_subscripting);
2313 }
2314 }
2315
Daniel Dunbarc21c4852009-04-08 23:54:23 +00002316 // Add preprocessing options like -I, -D, etc. if we are using the
2317 // preprocessor.
2318 //
2319 // FIXME: Support -fpreprocessed
Daniel Dunbarc21c4852009-04-08 23:54:23 +00002320 if (types::getPreprocessedType(InputType) != types::TY_INVALID)
Peter Collingbourne54db68b2011-11-06 00:40:05 +00002321 AddPreprocessingOptions(C, D, Args, CmdArgs, Output, Inputs);
Daniel Dunbar1d460332009-03-18 10:01:51 +00002322
Rafael Espindola19d9d2e2011-07-21 23:40:37 +00002323 // Don't warn about "clang -c -DPIC -fPIC test.i" because libtool.m4 assumes
2324 // that "The compiler can only warn and ignore the option if not recognized".
2325 // When building with ccache, it will pass -D options to clang even on
2326 // preprocessed inputs and configure concludes that -fPIC is not supported.
2327 Args.ClaimAllArgs(options::OPT_D);
2328
Daniel Dunbar20f0eac2009-09-17 06:53:36 +00002329 // Manually translate -O to -O2 and -O4 to -O3; let clang reject
Daniel Dunbar337a6272009-03-24 20:17:30 +00002330 // others.
2331 if (Arg *A = Args.getLastArg(options::OPT_O_Group)) {
Daniel Dunbarb827a052009-11-19 03:26:40 +00002332 if (A->getOption().matches(options::OPT_O4))
Daniel Dunbar337a6272009-03-24 20:17:30 +00002333 CmdArgs.push_back("-O3");
Daniel Dunbar473916c2010-05-27 06:51:08 +00002334 else if (A->getOption().matches(options::OPT_O) &&
Richard Smith1d489cf2012-11-01 04:30:05 +00002335 A->getValue()[0] == '\0')
Daniel Dunbar20f0eac2009-09-17 06:53:36 +00002336 CmdArgs.push_back("-O2");
Daniel Dunbar1d460332009-03-18 10:01:51 +00002337 else
Daniel Dunbar5697aa02009-03-18 23:39:35 +00002338 A->render(Args, CmdArgs);
Daniel Dunbar1d460332009-03-18 10:01:51 +00002339 }
2340
Daniel Dunbar6e8371e2009-10-29 02:24:45 +00002341 Args.AddAllArgs(CmdArgs, options::OPT_W_Group);
Ted Kremeneke8cf7d12012-07-07 05:53:30 +00002342 if (Args.hasFlag(options::OPT_pedantic, options::OPT_no_pedantic, false))
2343 CmdArgs.push_back("-pedantic");
Daniel Dunbar6e8371e2009-10-29 02:24:45 +00002344 Args.AddLastArg(CmdArgs, options::OPT_pedantic_errors);
Daniel Dunbar1d460332009-03-18 10:01:51 +00002345 Args.AddLastArg(CmdArgs, options::OPT_w);
Daniel Dunbard573d262009-04-07 22:13:21 +00002346
2347 // Handle -{std, ansi, trigraphs} -- take the last of -{std, ansi}
2348 // (-ansi is equivalent to -std=c89).
2349 //
2350 // If a std is supplied, only add -trigraphs if it follows the
2351 // option.
2352 if (Arg *Std = Args.getLastArg(options::OPT_std_EQ, options::OPT_ansi)) {
2353 if (Std->getOption().matches(options::OPT_ansi))
Nuno Lopes528365d2009-10-16 14:28:06 +00002354 if (types::isCXX(InputType))
Daniel Dunbar294691e2009-11-04 06:24:38 +00002355 CmdArgs.push_back("-std=c++98");
Nuno Lopes528365d2009-10-16 14:28:06 +00002356 else
Daniel Dunbar294691e2009-11-04 06:24:38 +00002357 CmdArgs.push_back("-std=c89");
Daniel Dunbard573d262009-04-07 22:13:21 +00002358 else
2359 Std->render(Args, CmdArgs);
2360
Daniel Dunbar0e100312010-06-14 21:23:08 +00002361 if (Arg *A = Args.getLastArg(options::OPT_std_EQ, options::OPT_ansi,
2362 options::OPT_trigraphs))
2363 if (A != Std)
Daniel Dunbard573d262009-04-07 22:13:21 +00002364 A->render(Args, CmdArgs);
Daniel Dunbara3ff2022009-04-26 01:10:38 +00002365 } else {
2366 // Honor -std-default.
Daniel Dunbar4a5290e2010-01-29 21:03:02 +00002367 //
2368 // FIXME: Clang doesn't correctly handle -std= when the input language
2369 // doesn't match. For the time being just ignore this for C++ inputs;
2370 // eventually we want to do all the standard defaulting here instead of
2371 // splitting it between the driver and clang -cc1.
2372 if (!types::isCXX(InputType))
Nico Weber50f88b92012-08-30 02:08:31 +00002373 Args.AddAllArgsTranslated(CmdArgs, options::OPT_std_default_EQ,
2374 "-std=", /*Joined=*/true);
2375 else if (getToolChain().getTriple().getOS() == llvm::Triple::Win32)
2376 CmdArgs.push_back("-std=c++11");
2377
Daniel Dunbard573d262009-04-07 22:13:21 +00002378 Args.AddLastArg(CmdArgs, options::OPT_trigraphs);
Daniel Dunbara3ff2022009-04-26 01:10:38 +00002379 }
Daniel Dunbarc21c4852009-04-08 23:54:23 +00002380
Chandler Carruth50465d12011-04-23 06:30:43 +00002381 // Map the bizarre '-Wwrite-strings' flag to a more sensible
2382 // '-fconst-strings'; this better indicates its actual behavior.
2383 if (Args.hasFlag(options::OPT_Wwrite_strings, options::OPT_Wno_write_strings,
2384 false)) {
2385 // For perfect compatibility with GCC, we do this even in the presence of
2386 // '-w'. This flag names something other than a warning for GCC.
2387 CmdArgs.push_back("-fconst-strings");
2388 }
2389
Chandler Carruth1cfe3c32011-04-23 09:27:53 +00002390 // GCC provides a macro definition '__DEPRECATED' when -Wdeprecated is active
Chandler Carruthf8c247d2011-04-23 19:48:40 +00002391 // during C++ compilation, which it is by default. GCC keeps this define even
2392 // in the presence of '-w', match this behavior bug-for-bug.
2393 if (types::isCXX(InputType) &&
2394 Args.hasFlag(options::OPT_Wdeprecated, options::OPT_Wno_deprecated,
2395 true)) {
2396 CmdArgs.push_back("-fdeprecated-macro");
Chandler Carruth1cfe3c32011-04-23 09:27:53 +00002397 }
2398
Chandler Carruthc304ba32010-05-22 02:21:53 +00002399 // Translate GCC's misnamer '-fasm' arguments to '-fgnu-keywords'.
2400 if (Arg *Asm = Args.getLastArg(options::OPT_fasm, options::OPT_fno_asm)) {
2401 if (Asm->getOption().matches(options::OPT_fasm))
2402 CmdArgs.push_back("-fgnu-keywords");
2403 else
2404 CmdArgs.push_back("-fno-gnu-keywords");
2405 }
2406
Rafael Espindola61b1efe2011-05-02 17:43:32 +00002407 if (ShouldDisableCFI(Args, getToolChain()))
2408 CmdArgs.push_back("-fno-dwarf2-cfi-asm");
Rafael Espindolaf24a1512011-04-30 18:35:43 +00002409
Nick Lewyckyea523d72011-10-17 23:05:52 +00002410 if (ShouldDisableDwarfDirectory(Args, getToolChain()))
2411 CmdArgs.push_back("-fno-dwarf-directory-asm");
2412
Nick Lewycky7c4fd912011-10-21 02:32:14 +00002413 if (const char *pwd = ::getenv("PWD")) {
2414 // GCC also verifies that stat(pwd) and stat(".") have the same inode
2415 // number. Not doing those because stats are slow, but we could.
NAKAMURA Takumi813a4072011-10-22 10:25:25 +00002416 if (llvm::sys::path::is_absolute(pwd)) {
Nick Lewycky7c4fd912011-10-21 02:32:14 +00002417 std::string CompDir = pwd;
2418 CmdArgs.push_back("-fdebug-compilation-dir");
2419 CmdArgs.push_back(Args.MakeArgString(CompDir));
2420 }
2421 }
2422
Richard Smithc18c4232011-11-21 19:36:32 +00002423 if (Arg *A = Args.getLastArg(options::OPT_ftemplate_depth_,
2424 options::OPT_ftemplate_depth_EQ)) {
Daniel Dunbar1d460332009-03-18 10:01:51 +00002425 CmdArgs.push_back("-ftemplate-depth");
Richard Smith1d489cf2012-11-01 04:30:05 +00002426 CmdArgs.push_back(A->getValue());
Daniel Dunbar1d460332009-03-18 10:01:51 +00002427 }
2428
Richard Smithc18c4232011-11-21 19:36:32 +00002429 if (Arg *A = Args.getLastArg(options::OPT_fconstexpr_depth_EQ)) {
2430 CmdArgs.push_back("-fconstexpr-depth");
Richard Smith1d489cf2012-11-01 04:30:05 +00002431 CmdArgs.push_back(A->getValue());
Richard Smithc18c4232011-11-21 19:36:32 +00002432 }
2433
Argyrios Kyrtzidis1380a142010-11-18 00:20:36 +00002434 if (Arg *A = Args.getLastArg(options::OPT_Wlarge_by_value_copy_EQ,
2435 options::OPT_Wlarge_by_value_copy_def)) {
Jean-Daniel Dupas2e4fd6d2012-05-04 08:08:37 +00002436 if (A->getNumValues()) {
Richard Smith1d489cf2012-11-01 04:30:05 +00002437 StringRef bytes = A->getValue();
Jean-Daniel Dupas2e4fd6d2012-05-04 08:08:37 +00002438 CmdArgs.push_back(Args.MakeArgString("-Wlarge-by-value-copy=" + bytes));
2439 } else
2440 CmdArgs.push_back("-Wlarge-by-value-copy=64"); // default value
Argyrios Kyrtzidis3532fdd2010-11-17 23:11:54 +00002441 }
2442
Nuno Lopesb3198a82012-05-08 22:10:46 +00002443 if (Arg *A = Args.getLastArg(options::OPT_fbounds_checking,
2444 options::OPT_fbounds_checking_EQ)) {
2445 if (A->getNumValues()) {
Richard Smith1d489cf2012-11-01 04:30:05 +00002446 StringRef val = A->getValue();
Nuno Lopesb3198a82012-05-08 22:10:46 +00002447 CmdArgs.push_back(Args.MakeArgString("-fbounds-checking=" + val));
2448 } else
2449 CmdArgs.push_back("-fbounds-checking=1");
2450 }
2451
Michael J. Spencerc6357102012-10-22 22:13:48 +00002452 if (Args.hasArg(options::OPT_relocatable_pch))
Daniel Dunbar66861e02009-11-20 22:21:36 +00002453 CmdArgs.push_back("-relocatable-pch");
Mike Stump1eb44332009-09-09 15:08:12 +00002454
Daniel Dunbar294691e2009-11-04 06:24:38 +00002455 if (Arg *A = Args.getLastArg(options::OPT_fconstant_string_class_EQ)) {
2456 CmdArgs.push_back("-fconstant-string-class");
Richard Smith1d489cf2012-11-01 04:30:05 +00002457 CmdArgs.push_back(A->getValue());
Daniel Dunbar294691e2009-11-04 06:24:38 +00002458 }
David Chisnall8a5a9aa2009-08-31 16:41:57 +00002459
Chris Lattner124fca52010-01-09 21:54:33 +00002460 if (Arg *A = Args.getLastArg(options::OPT_ftabstop_EQ)) {
2461 CmdArgs.push_back("-ftabstop");
Richard Smith1d489cf2012-11-01 04:30:05 +00002462 CmdArgs.push_back(A->getValue());
Chris Lattner124fca52010-01-09 21:54:33 +00002463 }
2464
Chris Lattner0f0c9632010-04-07 20:49:23 +00002465 CmdArgs.push_back("-ferror-limit");
2466 if (Arg *A = Args.getLastArg(options::OPT_ferror_limit_EQ))
Richard Smith1d489cf2012-11-01 04:30:05 +00002467 CmdArgs.push_back(A->getValue());
Chris Lattner0f0c9632010-04-07 20:49:23 +00002468 else
2469 CmdArgs.push_back("19");
Douglas Gregor575cf372010-04-20 07:18:24 +00002470
Chandler Carruthc40f73c2010-05-06 04:55:18 +00002471 if (Arg *A = Args.getLastArg(options::OPT_fmacro_backtrace_limit_EQ)) {
2472 CmdArgs.push_back("-fmacro-backtrace-limit");
Richard Smith1d489cf2012-11-01 04:30:05 +00002473 CmdArgs.push_back(A->getValue());
Chandler Carruthc40f73c2010-05-06 04:55:18 +00002474 }
2475
2476 if (Arg *A = Args.getLastArg(options::OPT_ftemplate_backtrace_limit_EQ)) {
2477 CmdArgs.push_back("-ftemplate-backtrace-limit");
Richard Smith1d489cf2012-11-01 04:30:05 +00002478 CmdArgs.push_back(A->getValue());
Chandler Carruthc40f73c2010-05-06 04:55:18 +00002479 }
2480
Richard Smith08d6e032011-12-16 19:06:07 +00002481 if (Arg *A = Args.getLastArg(options::OPT_fconstexpr_backtrace_limit_EQ)) {
2482 CmdArgs.push_back("-fconstexpr-backtrace-limit");
Richard Smith1d489cf2012-11-01 04:30:05 +00002483 CmdArgs.push_back(A->getValue());
Richard Smith08d6e032011-12-16 19:06:07 +00002484 }
2485
Daniel Dunbar55efe142009-11-04 06:24:47 +00002486 // Pass -fmessage-length=.
Daniel Dunbara28690e2009-11-30 08:40:54 +00002487 CmdArgs.push_back("-fmessage-length");
Daniel Dunbar55efe142009-11-04 06:24:47 +00002488 if (Arg *A = Args.getLastArg(options::OPT_fmessage_length_EQ)) {
Richard Smith1d489cf2012-11-01 04:30:05 +00002489 CmdArgs.push_back(A->getValue());
Daniel Dunbar55efe142009-11-04 06:24:47 +00002490 } else {
2491 // If -fmessage-length=N was not specified, determine whether this is a
2492 // terminal and, if so, implicitly define -fmessage-length appropriately.
2493 unsigned N = llvm::sys::Process::StandardErrColumns();
Chris Lattner5f9e2722011-07-23 10:55:15 +00002494 CmdArgs.push_back(Args.MakeArgString(Twine(N)));
Daniel Dunbar55efe142009-11-04 06:24:47 +00002495 }
2496
Daniel Dunbarba8d8612009-12-03 18:42:11 +00002497 if (const Arg *A = Args.getLastArg(options::OPT_fvisibility_EQ)) {
2498 CmdArgs.push_back("-fvisibility");
Richard Smith1d489cf2012-11-01 04:30:05 +00002499 CmdArgs.push_back(A->getValue());
Daniel Dunbarba8d8612009-12-03 18:42:11 +00002500 }
2501
Douglas Gregor7cf84d62010-06-15 17:05:35 +00002502 Args.AddLastArg(CmdArgs, options::OPT_fvisibility_inlines_hidden);
Michael J. Spencer20249a12010-10-21 03:16:25 +00002503
Hans Wennborgde981f32012-06-28 08:01:44 +00002504 Args.AddLastArg(CmdArgs, options::OPT_ftlsmodel_EQ);
2505
Daniel Dunbar0a80ba72010-03-20 04:52:14 +00002506 // -fhosted is default.
Chad Rosierafc4baa2012-03-26 22:04:46 +00002507 if (Args.hasFlag(options::OPT_ffreestanding, options::OPT_fhosted, false) ||
2508 KernelOrKext)
Daniel Dunbar0a80ba72010-03-20 04:52:14 +00002509 CmdArgs.push_back("-ffreestanding");
2510
Daniel Dunbarba8d8612009-12-03 18:42:11 +00002511 // Forward -f (flag) options which we can pass directly.
Daniel Dunbar3aaf0822009-04-07 21:51:40 +00002512 Args.AddLastArg(CmdArgs, options::OPT_femit_all_decls);
Daniel Dunbar3aaf0822009-04-07 21:51:40 +00002513 Args.AddLastArg(CmdArgs, options::OPT_fheinous_gnu_extensions);
Devang Patelc69e1cf2010-09-30 19:05:55 +00002514 Args.AddLastArg(CmdArgs, options::OPT_flimit_debug_info);
Devang Patel033be8b2011-11-04 20:05:58 +00002515 Args.AddLastArg(CmdArgs, options::OPT_fno_limit_debug_info);
Eric Christophere88c4512011-10-25 07:13:06 +00002516 Args.AddLastArg(CmdArgs, options::OPT_fno_operator_names);
Anton Yartsev17ba2672011-12-23 20:23:19 +00002517 Args.AddLastArg(CmdArgs, options::OPT_faltivec);
Richard Trieu246b6aa2012-06-26 18:18:47 +00002518 Args.AddLastArg(CmdArgs, options::OPT_fdiagnostics_show_template_tree);
2519 Args.AddLastArg(CmdArgs, options::OPT_fno_elide_type);
Chad Rosier4574c3d2012-03-13 23:45:51 +00002520
Richard Smithc4dabad2012-11-05 22:04:41 +00002521 SanitizerArgs Sanitize = getSanitizerArgs(D, Args);
2522 Sanitize.addArgs(Args, CmdArgs);
2523
Chad Rosier4574c3d2012-03-13 23:45:51 +00002524 // Report and error for -faltivec on anything other then PowerPC.
2525 if (const Arg *A = Args.getLastArg(options::OPT_faltivec))
2526 if (!(getToolChain().getTriple().getArch() == llvm::Triple::ppc ||
2527 getToolChain().getTriple().getArch() == llvm::Triple::ppc64))
2528 D.Diag(diag::err_drv_argument_only_allowed_with)
2529 << A->getAsString(Args) << "ppc/ppc64";
2530
Daniel Dunbarbbe8e3e2011-03-01 18:49:30 +00002531 if (getToolChain().SupportsProfiling())
2532 Args.AddLastArg(CmdArgs, options::OPT_pg);
Daniel Dunbar8c6fa842010-03-16 16:57:46 +00002533
2534 // -flax-vector-conversions is default.
2535 if (!Args.hasFlag(options::OPT_flax_vector_conversions,
2536 options::OPT_fno_lax_vector_conversions))
2537 CmdArgs.push_back("-fno-lax-vector-conversions");
2538
Fariborz Jahanianb466d012011-01-07 01:05:02 +00002539 if (Args.getLastArg(options::OPT_fapple_kext))
2540 CmdArgs.push_back("-fapple-kext");
2541
David Blaikie940152f2012-06-14 18:55:27 +00002542 if (Args.hasFlag(options::OPT_frewrite_includes,
2543 options::OPT_fno_rewrite_includes, false))
2544 CmdArgs.push_back("-frewrite-includes");
2545
Fariborz Jahanian34e65772009-05-22 20:17:16 +00002546 Args.AddLastArg(CmdArgs, options::OPT_fobjc_sender_dependent_dispatch);
Chris Lattner182e0922009-04-21 05:34:31 +00002547 Args.AddLastArg(CmdArgs, options::OPT_fdiagnostics_print_source_range_info);
Douglas Gregor4786c152010-08-19 20:24:43 +00002548 Args.AddLastArg(CmdArgs, options::OPT_fdiagnostics_parseable_fixits);
Daniel Dunbar3aaf0822009-04-07 21:51:40 +00002549 Args.AddLastArg(CmdArgs, options::OPT_ftime_report);
2550 Args.AddLastArg(CmdArgs, options::OPT_ftrapv);
David Chisnall7f18e672010-09-17 18:29:54 +00002551
2552 if (Arg *A = Args.getLastArg(options::OPT_ftrapv_handler_EQ)) {
2553 CmdArgs.push_back("-ftrapv-handler");
Richard Smith1d489cf2012-11-01 04:30:05 +00002554 CmdArgs.push_back(A->getValue());
David Chisnall7f18e672010-09-17 18:29:54 +00002555 }
2556
Bob Wilson71fd6cc2012-02-03 06:27:22 +00002557 Args.AddLastArg(CmdArgs, options::OPT_ftrap_function_EQ);
Evan Cheng49af1f32011-04-08 21:37:45 +00002558
Chandler Carruth5adb5a82011-03-27 00:04:55 +00002559 // -fno-strict-overflow implies -fwrapv if it isn't disabled, but
2560 // -fstrict-overflow won't turn off an explicitly enabled -fwrapv.
2561 if (Arg *A = Args.getLastArg(options::OPT_fwrapv,
2562 options::OPT_fno_wrapv)) {
2563 if (A->getOption().matches(options::OPT_fwrapv))
2564 CmdArgs.push_back("-fwrapv");
2565 } else if (Arg *A = Args.getLastArg(options::OPT_fstrict_overflow,
2566 options::OPT_fno_strict_overflow)) {
2567 if (A->getOption().matches(options::OPT_fno_strict_overflow))
2568 CmdArgs.push_back("-fwrapv");
2569 }
Daniel Dunbar3aaf0822009-04-07 21:51:40 +00002570 Args.AddLastArg(CmdArgs, options::OPT_fwritable_strings);
Eric Christopherf84d4092010-08-07 23:08:14 +00002571 Args.AddLastArg(CmdArgs, options::OPT_funroll_loops);
Daniel Dunbar1d460332009-03-18 10:01:51 +00002572
Daniel Dunbar5345c392009-09-03 04:54:28 +00002573 Args.AddLastArg(CmdArgs, options::OPT_pthread);
2574
Mahesha Sf3b52312012-10-27 07:47:56 +00002575
Daniel Dunbar9e5cc6b2009-11-17 08:07:36 +00002576 // -stack-protector=0 is default.
2577 unsigned StackProtectorLevel = 0;
Bill Wendling45483f72009-06-28 07:36:13 +00002578 if (Arg *A = Args.getLastArg(options::OPT_fno_stack_protector,
2579 options::OPT_fstack_protector_all,
2580 options::OPT_fstack_protector)) {
Daniel Dunbar9e5cc6b2009-11-17 08:07:36 +00002581 if (A->getOption().matches(options::OPT_fstack_protector))
2582 StackProtectorLevel = 1;
2583 else if (A->getOption().matches(options::OPT_fstack_protector_all))
2584 StackProtectorLevel = 2;
Nico Weber2fef1112011-08-23 07:38:27 +00002585 } else {
2586 StackProtectorLevel =
2587 getToolChain().GetDefaultStackProtectorLevel(KernelOrKext);
2588 }
Daniel Dunbar9e5cc6b2009-11-17 08:07:36 +00002589 if (StackProtectorLevel) {
2590 CmdArgs.push_back("-stack-protector");
Chris Lattner5f9e2722011-07-23 10:55:15 +00002591 CmdArgs.push_back(Args.MakeArgString(Twine(StackProtectorLevel)));
Joerg Sonnenberger53b43a72012-09-12 13:51:14 +00002592 }
Chad Rosiera7afeb02012-08-21 16:16:06 +00002593
Joerg Sonnenberger53b43a72012-09-12 13:51:14 +00002594 // --param ssp-buffer-size=
2595 for (arg_iterator it = Args.filtered_begin(options::OPT__param),
2596 ie = Args.filtered_end(); it != ie; ++it) {
Richard Smith1d489cf2012-11-01 04:30:05 +00002597 StringRef Str((*it)->getValue());
Joerg Sonnenberger53b43a72012-09-12 13:51:14 +00002598 if (Str.startswith("ssp-buffer-size=")) {
2599 if (StackProtectorLevel) {
Chad Rosiera7afeb02012-08-21 16:16:06 +00002600 CmdArgs.push_back("-stack-protector-buffer-size");
2601 // FIXME: Verify the argument is a valid integer.
2602 CmdArgs.push_back(Args.MakeArgString(Str.drop_front(16)));
Chad Rosiera7afeb02012-08-21 16:16:06 +00002603 }
Joerg Sonnenberger53b43a72012-09-12 13:51:14 +00002604 (*it)->claim();
Chad Rosiera7afeb02012-08-21 16:16:06 +00002605 }
Bill Wendling45483f72009-06-28 07:36:13 +00002606 }
2607
Nick Lewycky4e785c92011-12-06 03:33:03 +00002608 // Translate -mstackrealign
2609 if (Args.hasFlag(options::OPT_mstackrealign, options::OPT_mno_stackrealign,
2610 false)) {
2611 CmdArgs.push_back("-backend-option");
2612 CmdArgs.push_back("-force-align-stack");
2613 }
2614 if (!Args.hasFlag(options::OPT_mno_stackrealign, options::OPT_mstackrealign,
2615 false)) {
2616 CmdArgs.push_back(Args.MakeArgString("-mstackrealign"));
2617 }
2618
Joerg Sonnenbergere9d11db2011-12-05 23:05:23 +00002619 if (Args.hasArg(options::OPT_mstack_alignment)) {
2620 StringRef alignment = Args.getLastArgValue(options::OPT_mstack_alignment);
2621 CmdArgs.push_back(Args.MakeArgString("-mstack-alignment=" + alignment));
Eric Christopher1a584022011-05-02 21:18:22 +00002622 }
Eric Christopher88b7cf02011-08-19 00:30:14 +00002623
Daniel Dunbar48d1ef72009-04-07 21:16:11 +00002624 // Forward -f options with positive and negative forms; we translate
2625 // these by hand.
2626
Fariborz Jahanianb466d012011-01-07 01:05:02 +00002627 if (Args.hasArg(options::OPT_mkernel)) {
Daniel Dunbar2843c192011-02-04 17:24:47 +00002628 if (!Args.hasArg(options::OPT_fapple_kext) && types::isCXX(InputType))
Fariborz Jahanianb466d012011-01-07 01:05:02 +00002629 CmdArgs.push_back("-fapple-kext");
2630 if (!Args.hasArg(options::OPT_fbuiltin))
2631 CmdArgs.push_back("-fno-builtin");
Chad Rosier3d265502012-03-26 21:29:17 +00002632 Args.ClaimAllArgs(options::OPT_fno_builtin);
Fariborz Jahanianb466d012011-01-07 01:05:02 +00002633 }
Daniel Dunbar9e5cc6b2009-11-17 08:07:36 +00002634 // -fbuiltin is default.
Fariborz Jahanianb466d012011-01-07 01:05:02 +00002635 else if (!Args.hasFlag(options::OPT_fbuiltin, options::OPT_fno_builtin))
Daniel Dunbar53e84842009-11-19 04:55:23 +00002636 CmdArgs.push_back("-fno-builtin");
Daniel Dunbar48d1ef72009-04-07 21:16:11 +00002637
Nuno Lopesfc284482009-12-16 16:59:22 +00002638 if (!Args.hasFlag(options::OPT_fassume_sane_operator_new,
2639 options::OPT_fno_assume_sane_operator_new))
2640 CmdArgs.push_back("-fno-assume-sane-operator-new");
2641
Daniel Dunbar9e5cc6b2009-11-17 08:07:36 +00002642 // -fblocks=0 is default.
2643 if (Args.hasFlag(options::OPT_fblocks, options::OPT_fno_blocks,
David Chisnalle6533ff2011-02-28 17:11:43 +00002644 getToolChain().IsBlocksDefault()) ||
2645 (Args.hasArg(options::OPT_fgnu_runtime) &&
2646 Args.hasArg(options::OPT_fobjc_nonfragile_abi) &&
2647 !Args.hasArg(options::OPT_fno_blocks))) {
Daniel Dunbar9e5cc6b2009-11-17 08:07:36 +00002648 CmdArgs.push_back("-fblocks");
John McCall13db5cf2011-09-09 20:41:01 +00002649
2650 if (!Args.hasArg(options::OPT_fgnu_runtime) &&
2651 !getToolChain().hasBlocksRuntime())
2652 CmdArgs.push_back("-fblocks-runtime-optional");
David Chisnall5e530af2009-11-17 19:33:30 +00002653 }
Daniel Dunbar48d1ef72009-04-07 21:16:11 +00002654
Douglas Gregor64554ba2012-01-18 15:19:58 +00002655 // -fmodules enables modules (off by default). However, for C++/Objective-C++,
2656 // users must also pass -fcxx-modules. The latter flag will disappear once the
2657 // modules implementation is solid for C++/Objective-C++ programs as well.
2658 if (Args.hasFlag(options::OPT_fmodules, options::OPT_fno_modules, false)) {
2659 bool AllowedInCXX = Args.hasFlag(options::OPT_fcxx_modules,
2660 options::OPT_fno_cxx_modules,
2661 false);
2662 if (AllowedInCXX || !types::isCXX(InputType))
2663 CmdArgs.push_back("-fmodules");
2664 }
Douglas Gregor7025d2c2012-01-03 17:13:05 +00002665
John McCall32579cf2010-04-09 19:12:06 +00002666 // -faccess-control is default.
John McCall7002f4c2010-04-09 19:03:51 +00002667 if (Args.hasFlag(options::OPT_fno_access_control,
2668 options::OPT_faccess_control,
John McCall32579cf2010-04-09 19:12:06 +00002669 false))
John McCall7002f4c2010-04-09 19:03:51 +00002670 CmdArgs.push_back("-fno-access-control");
John McCall3ddd6e02010-03-17 01:32:13 +00002671
Anders Carlssona4c24752010-11-21 00:09:52 +00002672 // -felide-constructors is the default.
2673 if (Args.hasFlag(options::OPT_fno_elide_constructors,
2674 options::OPT_felide_constructors,
2675 false))
2676 CmdArgs.push_back("-fno-elide-constructors");
2677
Daniel Dunbar0be42c42009-11-17 07:06:20 +00002678 // -frtti is default.
Chad Rosierafc4baa2012-03-26 22:04:46 +00002679 if (!Args.hasFlag(options::OPT_frtti, options::OPT_fno_rtti) ||
Richard Smithc4dabad2012-11-05 22:04:41 +00002680 KernelOrKext) {
Daniel Dunbar53e84842009-11-19 04:55:23 +00002681 CmdArgs.push_back("-fno-rtti");
Mike Stump738f8c22009-07-31 23:15:31 +00002682
Richard Smithc4dabad2012-11-05 22:04:41 +00002683 // -fno-rtti cannot usefully be combined with -fsanitize=vptr.
2684 if (Sanitize.Kind & SanitizerArgs::Vptr) {
2685 llvm::StringRef NoRttiArg =
2686 Args.getLastArg(options::OPT_mkernel,
2687 options::OPT_fapple_kext,
Richard Smith04fd3822012-11-06 01:12:02 +00002688 options::OPT_fno_rtti)->getAsString(Args);
Richard Smithc4dabad2012-11-05 22:04:41 +00002689 D.Diag(diag::err_drv_argument_not_allowed_with)
2690 << "-fsanitize=vptr" << NoRttiArg;
2691 }
2692 }
2693
Tony Linthicum96319392011-12-12 21:14:55 +00002694 // -fshort-enums=0 is default for all architectures except Hexagon.
Argyrios Kyrtzidis9a2b9d72010-10-08 00:25:19 +00002695 if (Args.hasFlag(options::OPT_fshort_enums,
Tony Linthicum96319392011-12-12 21:14:55 +00002696 options::OPT_fno_short_enums,
2697 getToolChain().getTriple().getArch() ==
2698 llvm::Triple::hexagon))
Argyrios Kyrtzidis9a2b9d72010-10-08 00:25:19 +00002699 CmdArgs.push_back("-fshort-enums");
2700
Daniel Dunbar1f95e652009-11-17 06:37:03 +00002701 // -fsigned-char is default.
Daniel Dunbar6d2eb4d2009-11-25 10:14:30 +00002702 if (!Args.hasFlag(options::OPT_fsigned_char, options::OPT_funsigned_char,
Daniel Dunbar1f95e652009-11-17 06:37:03 +00002703 isSignedCharDefault(getToolChain().getTriple())))
Daniel Dunbar76743522009-11-29 02:39:08 +00002704 CmdArgs.push_back("-fno-signed-char");
Eli Friedman5a779732009-06-05 07:21:14 +00002705
Anders Carlssona508b7d2010-02-06 23:23:06 +00002706 // -fthreadsafe-static is default.
Michael J. Spencer20249a12010-10-21 03:16:25 +00002707 if (!Args.hasFlag(options::OPT_fthreadsafe_statics,
Anders Carlssona508b7d2010-02-06 23:23:06 +00002708 options::OPT_fno_threadsafe_statics))
2709 CmdArgs.push_back("-fno-threadsafe-statics");
2710
Daniel Dunbarefb0fa92010-03-20 04:15:41 +00002711 // -fuse-cxa-atexit is default.
Chad Rosierafc4baa2012-03-26 22:04:46 +00002712 if (!Args.hasFlag(options::OPT_fuse_cxa_atexit,
2713 options::OPT_fno_use_cxa_atexit,
2714 getToolChain().getTriple().getOS() != llvm::Triple::Cygwin &&
Tony Linthicum96319392011-12-12 21:14:55 +00002715 getToolChain().getTriple().getOS() != llvm::Triple::MinGW32 &&
Chad Rosierafc4baa2012-03-26 22:04:46 +00002716 getToolChain().getTriple().getArch() != llvm::Triple::hexagon) ||
2717 KernelOrKext)
Daniel Dunbarefb0fa92010-03-20 04:15:41 +00002718 CmdArgs.push_back("-fno-use-cxa-atexit");
2719
Daniel Dunbar0be42c42009-11-17 07:06:20 +00002720 // -fms-extensions=0 is default.
Daniel Dunbar6d2eb4d2009-11-25 10:14:30 +00002721 if (Args.hasFlag(options::OPT_fms_extensions, options::OPT_fno_ms_extensions,
Daniel Dunbar0be42c42009-11-17 07:06:20 +00002722 getToolChain().getTriple().getOS() == llvm::Triple::Win32))
2723 CmdArgs.push_back("-fms-extensions");
2724
Chad Rosierf925e042012-07-20 21:20:33 +00002725 // -fms-inline-asm.
Chad Rosierd256f862012-07-20 23:12:26 +00002726 if (Args.hasArg(options::OPT_fenable_experimental_ms_inline_asm))
2727 CmdArgs.push_back("-fenable-experimental-ms-inline-asm");
Chad Rosierf925e042012-07-20 21:20:33 +00002728
Francois Pichetae556082011-09-17 04:32:15 +00002729 // -fms-compatibility=0 is default.
Douglas Gregorba97b6e2011-10-24 15:49:38 +00002730 if (Args.hasFlag(options::OPT_fms_compatibility,
2731 options::OPT_fno_ms_compatibility,
2732 (getToolChain().getTriple().getOS() == llvm::Triple::Win32 &&
2733 Args.hasFlag(options::OPT_fms_extensions,
2734 options::OPT_fno_ms_extensions,
2735 true))))
Francois Pichetae556082011-09-17 04:32:15 +00002736 CmdArgs.push_back("-fms-compatibility");
2737
Michael J. Spencerdae4ac42010-10-21 05:21:48 +00002738 // -fmsc-version=1300 is default.
2739 if (Args.hasFlag(options::OPT_fms_extensions, options::OPT_fno_ms_extensions,
2740 getToolChain().getTriple().getOS() == llvm::Triple::Win32) ||
2741 Args.hasArg(options::OPT_fmsc_version)) {
Chris Lattner5f9e2722011-07-23 10:55:15 +00002742 StringRef msc_ver = Args.getLastArgValue(options::OPT_fmsc_version);
Michael J. Spencerdae4ac42010-10-21 05:21:48 +00002743 if (msc_ver.empty())
2744 CmdArgs.push_back("-fmsc-version=1300");
2745 else
2746 CmdArgs.push_back(Args.MakeArgString("-fmsc-version=" + msc_ver));
2747 }
2748
2749
Dawn Perchik400b6072010-09-02 23:59:25 +00002750 // -fborland-extensions=0 is default.
2751 if (Args.hasFlag(options::OPT_fborland_extensions,
2752 options::OPT_fno_borland_extensions, false))
2753 CmdArgs.push_back("-fborland-extensions");
2754
Francois Pichet8efcc012011-09-01 16:38:08 +00002755 // -fno-delayed-template-parsing is default, except for Windows where MSVC STL
2756 // needs it.
Francois Pichet8387e2a2011-04-22 22:18:13 +00002757 if (Args.hasFlag(options::OPT_fdelayed_template_parsing,
2758 options::OPT_fno_delayed_template_parsing,
Francois Pichet8efcc012011-09-01 16:38:08 +00002759 getToolChain().getTriple().getOS() == llvm::Triple::Win32))
Francois Pichet805bc1f2011-08-26 00:22:34 +00002760 CmdArgs.push_back("-fdelayed-template-parsing");
Francois Pichet8387e2a2011-04-22 22:18:13 +00002761
Chandler Carrutheb5d7b72010-04-17 20:17:31 +00002762 // -fgnu-keywords default varies depending on language; only pass if
2763 // specified.
2764 if (Arg *A = Args.getLastArg(options::OPT_fgnu_keywords,
Daniel Dunbar40788d92010-04-24 17:56:39 +00002765 options::OPT_fno_gnu_keywords))
2766 A->render(Args, CmdArgs);
Chandler Carrutheb5d7b72010-04-17 20:17:31 +00002767
Rafael Espindola01ba8542011-06-02 17:30:53 +00002768 if (Args.hasFlag(options::OPT_fgnu89_inline,
2769 options::OPT_fno_gnu89_inline,
2770 false))
Rafael Espindolafb3f4aa2011-06-02 16:13:27 +00002771 CmdArgs.push_back("-fgnu89-inline");
2772
Chad Rosierfc055f92012-03-15 22:31:42 +00002773 if (Args.hasArg(options::OPT_fno_inline))
2774 CmdArgs.push_back("-fno-inline");
2775
Chad Rosier634a4b12012-03-06 21:17:19 +00002776 if (Args.hasArg(options::OPT_fno_inline_functions))
2777 CmdArgs.push_back("-fno-inline-functions");
Chad Rosier250008b2012-03-06 18:49:20 +00002778
John McCall260611a2012-06-20 06:18:46 +00002779 ObjCRuntime objcRuntime = AddObjCRuntimeArgs(Args, CmdArgs, rewriteKind);
John McCall9f084a32011-07-06 00:26:06 +00002780
John McCall260611a2012-06-20 06:18:46 +00002781 // -fobjc-dispatch-method is only relevant with the nonfragile-abi, and
2782 // legacy is the default.
2783 if (objcRuntime.isNonFragile()) {
David Chisnall3c3ccd22011-09-30 13:32:35 +00002784 if (!Args.hasFlag(options::OPT_fobjc_legacy_dispatch,
2785 options::OPT_fno_objc_legacy_dispatch,
David Chisnall2c7886d2012-07-04 11:52:24 +00002786 objcRuntime.isLegacyDispatchDefaultForArch(
2787 getToolChain().getTriple().getArch()))) {
David Chisnall3c3ccd22011-09-30 13:32:35 +00002788 if (getToolChain().UseObjCMixedDispatch())
2789 CmdArgs.push_back("-fobjc-dispatch-method=mixed");
2790 else
2791 CmdArgs.push_back("-fobjc-dispatch-method=non-legacy");
2792 }
2793 }
2794
Nico Weberdf423542012-03-09 21:19:44 +00002795 // -fobjc-default-synthesize-properties=1 is default. This only has an effect
2796 // if the nonfragile objc abi is used.
Fariborz Jahaniane51fe092012-04-09 18:58:55 +00002797 if (getToolChain().IsObjCDefaultSynthPropertiesDefault()) {
David Chisnall3c3ccd22011-09-30 13:32:35 +00002798 CmdArgs.push_back("-fobjc-default-synthesize-properties");
2799 }
2800
John McCall9f084a32011-07-06 00:26:06 +00002801 // Allow -fno-objc-arr to trump -fobjc-arr/-fobjc-arc.
2802 // NOTE: This logic is duplicated in ToolChains.cpp.
2803 bool ARC = isObjCAutoRefCount(Args);
2804 if (ARC) {
John McCall0a7dd782012-08-21 02:47:43 +00002805 getToolChain().CheckObjCARC();
Argyrios Kyrtzidis5840dd92012-02-29 03:43:52 +00002806
John McCall9f084a32011-07-06 00:26:06 +00002807 CmdArgs.push_back("-fobjc-arc");
2808
Chandler Carruth7ffa0322011-11-04 07:34:47 +00002809 // FIXME: It seems like this entire block, and several around it should be
2810 // wrapped in isObjC, but for now we just use it here as this is where it
2811 // was being used previously.
2812 if (types::isCXX(InputType) && types::isObjC(InputType)) {
2813 if (getToolChain().GetCXXStdlibType(Args) == ToolChain::CST_Libcxx)
2814 CmdArgs.push_back("-fobjc-arc-cxxlib=libc++");
2815 else
2816 CmdArgs.push_back("-fobjc-arc-cxxlib=libstdc++");
2817 }
2818
John McCall9f084a32011-07-06 00:26:06 +00002819 // Allow the user to enable full exceptions code emission.
2820 // We define off for Objective-CC, on for Objective-C++.
2821 if (Args.hasFlag(options::OPT_fobjc_arc_exceptions,
2822 options::OPT_fno_objc_arc_exceptions,
2823 /*default*/ types::isCXX(InputType)))
2824 CmdArgs.push_back("-fobjc-arc-exceptions");
2825 }
2826
2827 // -fobjc-infer-related-result-type is the default, except in the Objective-C
2828 // rewriter.
John McCall260611a2012-06-20 06:18:46 +00002829 if (rewriteKind != RK_None)
John McCall9f084a32011-07-06 00:26:06 +00002830 CmdArgs.push_back("-fno-objc-infer-related-result-type");
Eric Christopher88b7cf02011-08-19 00:30:14 +00002831
John McCall9f084a32011-07-06 00:26:06 +00002832 // Handle -fobjc-gc and -fobjc-gc-only. They are exclusive, and -fobjc-gc-only
2833 // takes precedence.
2834 const Arg *GCArg = Args.getLastArg(options::OPT_fobjc_gc_only);
2835 if (!GCArg)
2836 GCArg = Args.getLastArg(options::OPT_fobjc_gc);
2837 if (GCArg) {
2838 if (ARC) {
Chris Lattner5f9e2722011-07-23 10:55:15 +00002839 D.Diag(diag::err_drv_objc_gc_arr)
John McCall9f084a32011-07-06 00:26:06 +00002840 << GCArg->getAsString(Args);
2841 } else if (getToolChain().SupportsObjCGC()) {
2842 GCArg->render(Args, CmdArgs);
2843 } else {
2844 // FIXME: We should move this to a hard error.
Chris Lattner5f9e2722011-07-23 10:55:15 +00002845 D.Diag(diag::warn_drv_objc_gc_unsupported)
John McCall9f084a32011-07-06 00:26:06 +00002846 << GCArg->getAsString(Args);
2847 }
2848 }
2849
John McCalld71315c2011-06-22 00:53:57 +00002850 // Add exception args.
2851 addExceptionArgs(Args, InputType, getToolChain().getTriple(),
John McCall260611a2012-06-20 06:18:46 +00002852 KernelOrKext, objcRuntime, CmdArgs);
John McCalld71315c2011-06-22 00:53:57 +00002853
2854 if (getToolChain().UseSjLjExceptions())
2855 CmdArgs.push_back("-fsjlj-exceptions");
2856
2857 // C++ "sane" operator new.
Daniel Dunbar984eb862010-02-01 21:07:25 +00002858 if (!Args.hasFlag(options::OPT_fassume_sane_operator_new,
2859 options::OPT_fno_assume_sane_operator_new))
2860 CmdArgs.push_back("-fno-assume-sane-operator-new");
2861
Daniel Dunbarf35f14d2010-04-27 15:34:57 +00002862 // -fconstant-cfstrings is default, and may be subject to argument translation
2863 // on Darwin.
2864 if (!Args.hasFlag(options::OPT_fconstant_cfstrings,
2865 options::OPT_fno_constant_cfstrings) ||
2866 !Args.hasFlag(options::OPT_mconstant_cfstrings,
2867 options::OPT_mno_constant_cfstrings))
2868 CmdArgs.push_back("-fno-constant-cfstrings");
2869
John Thompsona6fda122009-11-05 20:14:16 +00002870 // -fshort-wchar default varies depending on platform; only
2871 // pass if specified.
Daniel Dunbar1744a352010-04-27 15:35:03 +00002872 if (Arg *A = Args.getLastArg(options::OPT_fshort_wchar))
2873 A->render(Args, CmdArgs);
John Thompsona6fda122009-11-05 20:14:16 +00002874
Daniel Dunbaree848a72009-10-29 02:39:57 +00002875 // -fno-pascal-strings is default, only pass non-default. If the tool chain
2876 // happened to translate to -mpascal-strings, we want to back translate here.
Daniel Dunbar82d00682009-04-07 23:51:44 +00002877 //
2878 // FIXME: This is gross; that translation should be pulled from the
2879 // tool chain.
Daniel Dunbarc21c4852009-04-08 23:54:23 +00002880 if (Args.hasFlag(options::OPT_fpascal_strings,
Daniel Dunbar82d00682009-04-07 23:51:44 +00002881 options::OPT_fno_pascal_strings,
2882 false) ||
2883 Args.hasFlag(options::OPT_mpascal_strings,
2884 options::OPT_mno_pascal_strings,
2885 false))
Daniel Dunbar48d1ef72009-04-07 21:16:11 +00002886 CmdArgs.push_back("-fpascal-strings");
NAKAMURA Takumi125b4cb2011-02-17 08:50:50 +00002887
Daniel Dunbar88934e82011-10-05 21:04:55 +00002888 // Honor -fpack-struct= and -fpack-struct, if given. Note that
2889 // -fno-pack-struct doesn't apply to -fpack-struct=.
2890 if (Arg *A = Args.getLastArg(options::OPT_fpack_struct_EQ)) {
James Molloy8049c442012-05-02 07:56:14 +00002891 std::string PackStructStr = "-fpack-struct=";
Richard Smith1d489cf2012-11-01 04:30:05 +00002892 PackStructStr += A->getValue();
James Molloy8049c442012-05-02 07:56:14 +00002893 CmdArgs.push_back(Args.MakeArgString(PackStructStr));
Daniel Dunbar88934e82011-10-05 21:04:55 +00002894 } else if (Args.hasFlag(options::OPT_fpack_struct,
2895 options::OPT_fno_pack_struct, false)) {
James Molloy8049c442012-05-02 07:56:14 +00002896 CmdArgs.push_back("-fpack-struct=1");
Daniel Dunbar88934e82011-10-05 21:04:55 +00002897 }
2898
Fariborz Jahanianb466d012011-01-07 01:05:02 +00002899 if (Args.hasArg(options::OPT_mkernel) ||
2900 Args.hasArg(options::OPT_fapple_kext)) {
2901 if (!Args.hasArg(options::OPT_fcommon))
2902 CmdArgs.push_back("-fno-common");
Chad Rosierec09b3e2012-03-26 21:35:40 +00002903 Args.ClaimAllArgs(options::OPT_fno_common);
Fariborz Jahanianb466d012011-01-07 01:05:02 +00002904 }
Daniel Dunbar88934e82011-10-05 21:04:55 +00002905
Daniel Dunbar48d1ef72009-04-07 21:16:11 +00002906 // -fcommon is default, only pass non-default.
Fariborz Jahanianb466d012011-01-07 01:05:02 +00002907 else if (!Args.hasFlag(options::OPT_fcommon, options::OPT_fno_common))
Daniel Dunbar48d1ef72009-04-07 21:16:11 +00002908 CmdArgs.push_back("-fno-common");
2909
Daniel Dunbar70d3c922009-04-15 02:37:43 +00002910 // -fsigned-bitfields is default, and clang doesn't yet support
Daniel Dunbar06205ca2010-10-15 22:30:42 +00002911 // -funsigned-bitfields.
Mike Stump1eb44332009-09-09 15:08:12 +00002912 if (!Args.hasFlag(options::OPT_fsigned_bitfields,
Daniel Dunbar70d3c922009-04-15 02:37:43 +00002913 options::OPT_funsigned_bitfields))
Chris Lattner5f9e2722011-07-23 10:55:15 +00002914 D.Diag(diag::warn_drv_clang_unsupported)
Daniel Dunbar70d3c922009-04-15 02:37:43 +00002915 << Args.getLastArg(options::OPT_funsigned_bitfields)->getAsString(Args);
2916
Daniel Dunbar06205ca2010-10-15 22:30:42 +00002917 // -fsigned-bitfields is default, and clang doesn't support -fno-for-scope.
2918 if (!Args.hasFlag(options::OPT_ffor_scope,
2919 options::OPT_fno_for_scope))
Chris Lattner5f9e2722011-07-23 10:55:15 +00002920 D.Diag(diag::err_drv_clang_unsupported)
Daniel Dunbar06205ca2010-10-15 22:30:42 +00002921 << Args.getLastArg(options::OPT_fno_for_scope)->getAsString(Args);
2922
Jeffrey Yasskin0ea22fd2010-06-08 04:56:20 +00002923 // -fcaret-diagnostics is default.
2924 if (!Args.hasFlag(options::OPT_fcaret_diagnostics,
2925 options::OPT_fno_caret_diagnostics, true))
2926 CmdArgs.push_back("-fno-caret-diagnostics");
2927
Daniel Dunbar49138fc2009-04-19 21:09:34 +00002928 // -fdiagnostics-fixit-info is default, only pass non-default.
Mike Stump1eb44332009-09-09 15:08:12 +00002929 if (!Args.hasFlag(options::OPT_fdiagnostics_fixit_info,
Daniel Dunbar49138fc2009-04-19 21:09:34 +00002930 options::OPT_fno_diagnostics_fixit_info))
2931 CmdArgs.push_back("-fno-diagnostics-fixit-info");
Eric Christopher88b7cf02011-08-19 00:30:14 +00002932
Daniel Dunbar9e820ee2009-04-16 06:32:38 +00002933 // Enable -fdiagnostics-show-option by default.
Mike Stump1eb44332009-09-09 15:08:12 +00002934 if (Args.hasFlag(options::OPT_fdiagnostics_show_option,
Daniel Dunbar9e820ee2009-04-16 06:32:38 +00002935 options::OPT_fno_diagnostics_show_option))
2936 CmdArgs.push_back("-fdiagnostics-show-option");
Daniel Dunbar838be482009-11-04 06:24:57 +00002937
Chris Lattner6fbe8392010-05-04 21:55:25 +00002938 if (const Arg *A =
2939 Args.getLastArg(options::OPT_fdiagnostics_show_category_EQ)) {
2940 CmdArgs.push_back("-fdiagnostics-show-category");
Richard Smith1d489cf2012-11-01 04:30:05 +00002941 CmdArgs.push_back(A->getValue());
Chris Lattner6fbe8392010-05-04 21:55:25 +00002942 }
Daniel Dunbarca0e0542010-08-24 16:47:49 +00002943
Douglas Gregorc9471b02011-05-21 17:07:29 +00002944 if (const Arg *A =
2945 Args.getLastArg(options::OPT_fdiagnostics_format_EQ)) {
2946 CmdArgs.push_back("-fdiagnostics-format");
Richard Smith1d489cf2012-11-01 04:30:05 +00002947 CmdArgs.push_back(A->getValue());
Douglas Gregorc9471b02011-05-21 17:07:29 +00002948 }
2949
Chandler Carruthabaca7a2011-03-27 01:50:55 +00002950 if (Arg *A = Args.getLastArg(
2951 options::OPT_fdiagnostics_show_note_include_stack,
2952 options::OPT_fno_diagnostics_show_note_include_stack)) {
2953 if (A->getOption().matches(
2954 options::OPT_fdiagnostics_show_note_include_stack))
2955 CmdArgs.push_back("-fdiagnostics-show-note-include-stack");
2956 else
2957 CmdArgs.push_back("-fno-diagnostics-show-note-include-stack");
2958 }
2959
Daniel Dunbar838be482009-11-04 06:24:57 +00002960 // Color diagnostics are the default, unless the terminal doesn't support
2961 // them.
2962 if (Args.hasFlag(options::OPT_fcolor_diagnostics,
Argyrios Kyrtzidisf765d762010-09-23 12:56:06 +00002963 options::OPT_fno_color_diagnostics,
2964 llvm::sys::Process::StandardErrHasColors()))
Daniel Dunbar838be482009-11-04 06:24:57 +00002965 CmdArgs.push_back("-fcolor-diagnostics");
2966
Daniel Dunbar75eb1d62009-06-08 21:13:54 +00002967 if (!Args.hasFlag(options::OPT_fshow_source_location,
2968 options::OPT_fno_show_source_location))
2969 CmdArgs.push_back("-fno-show-source-location");
Daniel Dunbar9e820ee2009-04-16 06:32:38 +00002970
Douglas Gregorc9471b02011-05-21 17:07:29 +00002971 if (!Args.hasFlag(options::OPT_fshow_column,
2972 options::OPT_fno_show_column,
2973 true))
2974 CmdArgs.push_back("-fno-show-column");
2975
Douglas Gregora0068fc2010-07-09 17:35:33 +00002976 if (!Args.hasFlag(options::OPT_fspell_checking,
2977 options::OPT_fno_spell_checking))
2978 CmdArgs.push_back("-fno-spell-checking");
Daniel Dunbarca0e0542010-08-24 16:47:49 +00002979
Daniel Dunbar25b26eb2010-10-18 22:49:46 +00002980
Daniel Dunbar16894392010-11-02 19:42:04 +00002981 // Silently ignore -fasm-blocks for now.
2982 (void) Args.hasFlag(options::OPT_fasm_blocks, options::OPT_fno_asm_blocks,
2983 false);
Daniel Dunbar25b26eb2010-10-18 22:49:46 +00002984
Jeffrey Yasskin5edbdcc2010-06-11 05:57:47 +00002985 if (Arg *A = Args.getLastArg(options::OPT_fshow_overloads_EQ))
2986 A->render(Args, CmdArgs);
2987
Daniel Dunbar7695fba2009-04-19 21:20:32 +00002988 // -fdollars-in-identifiers default varies depending on platform and
2989 // language; only pass if specified.
Mike Stump1eb44332009-09-09 15:08:12 +00002990 if (Arg *A = Args.getLastArg(options::OPT_fdollars_in_identifiers,
Daniel Dunbar7695fba2009-04-19 21:20:32 +00002991 options::OPT_fno_dollars_in_identifiers)) {
2992 if (A->getOption().matches(options::OPT_fdollars_in_identifiers))
Daniel Dunbar8663b182009-12-16 20:10:18 +00002993 CmdArgs.push_back("-fdollars-in-identifiers");
Daniel Dunbar7695fba2009-04-19 21:20:32 +00002994 else
Daniel Dunbar8663b182009-12-16 20:10:18 +00002995 CmdArgs.push_back("-fno-dollars-in-identifiers");
Daniel Dunbar7695fba2009-04-19 21:20:32 +00002996 }
2997
Daniel Dunbare027a4b2009-05-22 19:02:20 +00002998 // -funit-at-a-time is default, and we don't support -fno-unit-at-a-time for
2999 // practical purposes.
Mike Stump1eb44332009-09-09 15:08:12 +00003000 if (Arg *A = Args.getLastArg(options::OPT_funit_at_a_time,
Daniel Dunbare027a4b2009-05-22 19:02:20 +00003001 options::OPT_fno_unit_at_a_time)) {
3002 if (A->getOption().matches(options::OPT_fno_unit_at_a_time))
Chris Lattner5f9e2722011-07-23 10:55:15 +00003003 D.Diag(diag::warn_drv_clang_unsupported) << A->getAsString(Args);
Daniel Dunbare027a4b2009-05-22 19:02:20 +00003004 }
Eli Friedmanceb5c5b2009-07-14 21:58:17 +00003005
Eli Friedman19bda3a2011-11-02 01:53:16 +00003006 if (Args.hasFlag(options::OPT_fapple_pragma_pack,
3007 options::OPT_fno_apple_pragma_pack, false))
3008 CmdArgs.push_back("-fapple-pragma-pack");
3009
Daniel Dunbar2ba91572009-09-10 03:37:02 +00003010 // Default to -fno-builtin-str{cat,cpy} on Darwin for ARM.
Daniel Dunbarf84a4a42009-09-10 04:57:27 +00003011 //
Daniel Dunbar8ff5b282009-12-11 23:00:49 +00003012 // FIXME: This is disabled until clang -cc1 supports -fno-builtin-foo. PR4941.
Daniel Dunbarf84a4a42009-09-10 04:57:27 +00003013#if 0
Bob Wilson905c45f2011-10-14 05:03:44 +00003014 if (getToolChain().getTriple().isOSDarwin() &&
Daniel Dunbar2ba91572009-09-10 03:37:02 +00003015 (getToolChain().getTriple().getArch() == llvm::Triple::arm ||
3016 getToolChain().getTriple().getArch() == llvm::Triple::thumb)) {
3017 if (!Args.hasArg(options::OPT_fbuiltin_strcat))
3018 CmdArgs.push_back("-fno-builtin-strcat");
3019 if (!Args.hasArg(options::OPT_fbuiltin_strcpy))
3020 CmdArgs.push_back("-fno-builtin-strcpy");
3021 }
Daniel Dunbarf84a4a42009-09-10 04:57:27 +00003022#endif
Daniel Dunbar2ba91572009-09-10 03:37:02 +00003023
Daniel Dunbard98750f2011-03-18 21:23:40 +00003024 // Only allow -traditional or -traditional-cpp outside in preprocessing modes.
Mike Stump1eb44332009-09-09 15:08:12 +00003025 if (Arg *A = Args.getLastArg(options::OPT_traditional,
Daniel Dunbard98750f2011-03-18 21:23:40 +00003026 options::OPT_traditional_cpp)) {
3027 if (isa<PreprocessJobAction>(JA))
3028 CmdArgs.push_back("-traditional-cpp");
Eric Christopher88b7cf02011-08-19 00:30:14 +00003029 else
Chris Lattner5f9e2722011-07-23 10:55:15 +00003030 D.Diag(diag::err_drv_clang_unsupported) << A->getAsString(Args);
Daniel Dunbard98750f2011-03-18 21:23:40 +00003031 }
Eli Friedmanceb5c5b2009-07-14 21:58:17 +00003032
Daniel Dunbar1d460332009-03-18 10:01:51 +00003033 Args.AddLastArg(CmdArgs, options::OPT_dM);
Chris Lattnerd82df3a2009-04-12 01:56:53 +00003034 Args.AddLastArg(CmdArgs, options::OPT_dD);
Ted Kremenek36f6e302011-11-11 00:07:43 +00003035
3036 // Handle serialized diagnostics.
3037 if (Arg *A = Args.getLastArg(options::OPT__serialize_diags)) {
3038 CmdArgs.push_back("-serialize-diagnostic-file");
Richard Smith1d489cf2012-11-01 04:30:05 +00003039 CmdArgs.push_back(Args.MakeArgString(A->getValue()));
Ted Kremenek36f6e302011-11-11 00:07:43 +00003040 }
Daniel Dunbar1d460332009-03-18 10:01:51 +00003041
Ted Kremenek127ff2e2012-09-13 06:41:18 +00003042 if (Args.hasArg(options::OPT_fretain_comments_from_system_headers))
3043 CmdArgs.push_back("-fretain-comments-from-system-headers");
3044
Daniel Dunbar3f87fb02010-04-15 06:09:03 +00003045 // Forward -Xclang arguments to -cc1, and -mllvm arguments to the LLVM option
3046 // parser.
Daniel Dunbar1d460332009-03-18 10:01:51 +00003047 Args.AddAllArgValues(CmdArgs, options::OPT_Xclang);
Daniel Dunbar3f87fb02010-04-15 06:09:03 +00003048 for (arg_iterator it = Args.filtered_begin(options::OPT_mllvm),
3049 ie = Args.filtered_end(); it != ie; ++it) {
Daniel Dunbar7e4953e2010-06-11 22:00:13 +00003050 (*it)->claim();
Daniel Dunbarfb36d212010-04-17 06:10:00 +00003051
Daniel Dunbar3f87fb02010-04-15 06:09:03 +00003052 // We translate this by hand to the -cc1 argument, since nightly test uses
3053 // it and developers have been trained to spell it with -mllvm.
Richard Smith1d489cf2012-11-01 04:30:05 +00003054 if (StringRef((*it)->getValue(0)) == "-disable-llvm-optzns")
Daniel Dunbar3f87fb02010-04-15 06:09:03 +00003055 CmdArgs.push_back("-disable-llvm-optzns");
3056 else
Daniel Dunbar7e4953e2010-06-11 22:00:13 +00003057 (*it)->render(Args, CmdArgs);
Daniel Dunbar3f87fb02010-04-15 06:09:03 +00003058 }
Daniel Dunbar1d460332009-03-18 10:01:51 +00003059
Daniel Dunbarcd8e4c42009-03-30 06:36:42 +00003060 if (Output.getType() == types::TY_Dependencies) {
3061 // Handled with other dependency code.
Daniel Dunbar115a7922009-03-19 07:29:38 +00003062 } else if (Output.isFilename()) {
Daniel Dunbarb488c1d2009-03-18 08:07:30 +00003063 CmdArgs.push_back("-o");
Daniel Dunbar115a7922009-03-19 07:29:38 +00003064 CmdArgs.push_back(Output.getFilename());
3065 } else {
3066 assert(Output.isNothing() && "Invalid output.");
Daniel Dunbarb488c1d2009-03-18 08:07:30 +00003067 }
3068
Daniel Dunbar1d460332009-03-18 10:01:51 +00003069 for (InputInfoList::const_iterator
3070 it = Inputs.begin(), ie = Inputs.end(); it != ie; ++it) {
3071 const InputInfo &II = *it;
3072 CmdArgs.push_back("-x");
Fariborz Jahaniana5ee0892012-09-28 19:05:17 +00003073 if (Args.hasArg(options::OPT_rewrite_objc))
3074 CmdArgs.push_back(types::getTypeName(types::TY_PP_ObjCXX));
3075 else
3076 CmdArgs.push_back(types::getTypeName(II.getType()));
Daniel Dunbar7c1e4652010-08-02 02:38:21 +00003077 if (II.isFilename())
Daniel Dunbar115a7922009-03-19 07:29:38 +00003078 CmdArgs.push_back(II.getFilename());
Daniel Dunbar1d460332009-03-18 10:01:51 +00003079 else
Daniel Dunbar115a7922009-03-19 07:29:38 +00003080 II.getInputArg().renderAsInput(Args, CmdArgs);
Daniel Dunbar1d460332009-03-18 10:01:51 +00003081 }
Daniel Dunbarc21c4852009-04-08 23:54:23 +00003082
Chris Lattnere6113de2009-11-03 19:50:27 +00003083 Args.AddAllArgs(CmdArgs, options::OPT_undef);
3084
Daniel Dunbara001c1c2010-07-18 21:16:15 +00003085 const char *Exec = getToolChain().getDriver().getClangProgramPath();
Daniel Dunbarf2d8b9f2009-12-18 02:43:17 +00003086
3087 // Optionally embed the -cc1 level arguments into the debug info, for build
3088 // analysis.
3089 if (getToolChain().UseDwarfDebugFlags()) {
Daniel Dunbar6e900472010-06-04 18:47:06 +00003090 ArgStringList OriginalArgs;
3091 for (ArgList::const_iterator it = Args.begin(),
3092 ie = Args.end(); it != ie; ++it)
3093 (*it)->render(Args, OriginalArgs);
Daniel Dunbarca0e0542010-08-24 16:47:49 +00003094
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00003095 SmallString<256> Flags;
Daniel Dunbarf2d8b9f2009-12-18 02:43:17 +00003096 Flags += Exec;
Daniel Dunbar6e900472010-06-04 18:47:06 +00003097 for (unsigned i = 0, e = OriginalArgs.size(); i != e; ++i) {
Daniel Dunbarf2d8b9f2009-12-18 02:43:17 +00003098 Flags += " ";
Daniel Dunbar6e900472010-06-04 18:47:06 +00003099 Flags += OriginalArgs[i];
Daniel Dunbarf2d8b9f2009-12-18 02:43:17 +00003100 }
3101 CmdArgs.push_back("-dwarf-debug-flags");
3102 CmdArgs.push_back(Args.MakeArgString(Flags.str()));
3103 }
3104
Daniel Dunbar2fe238e2010-08-02 02:38:28 +00003105 C.addCommand(new Command(JA, *this, Exec, CmdArgs));
Daniel Dunbara880db02009-03-23 19:03:36 +00003106
Roman Divackybe4c8702011-02-10 16:52:03 +00003107 if (Arg *A = Args.getLastArg(options::OPT_pg))
3108 if (Args.hasArg(options::OPT_fomit_frame_pointer))
Chris Lattner5f9e2722011-07-23 10:55:15 +00003109 D.Diag(diag::err_drv_argument_not_allowed_with)
Roman Divackybe4c8702011-02-10 16:52:03 +00003110 << "-fomit-frame-pointer" << A->getAsString(Args);
Michael J. Spencer20249a12010-10-21 03:16:25 +00003111
Daniel Dunbar68fb4692009-04-03 20:51:31 +00003112 // Claim some arguments which clang supports automatically.
3113
Daniel Dunbarf4046862010-04-15 06:18:42 +00003114 // -fpch-preprocess is used with gcc to add a special marker in the output to
3115 // include the PCH file. Clang's PTH solution is completely transparent, so we
3116 // do not need to deal with it at all.
Daniel Dunbar68fb4692009-04-03 20:51:31 +00003117 Args.ClaimAllArgs(options::OPT_fpch_preprocess);
Daniel Dunbarc21c4852009-04-08 23:54:23 +00003118
Daniel Dunbara880db02009-03-23 19:03:36 +00003119 // Claim some arguments which clang doesn't support, but we don't
3120 // care to warn the user about.
Daniel Dunbarcdd96862009-11-25 11:53:23 +00003121 Args.ClaimAllArgs(options::OPT_clang_ignored_f_Group);
3122 Args.ClaimAllArgs(options::OPT_clang_ignored_m_Group);
Rafael Espindola035ff0c2011-02-28 23:29:45 +00003123
Rafael Espindola9c094fb2011-03-01 05:25:27 +00003124 // Disable warnings for clang -E -use-gold-plugin -emit-llvm foo.c
Rafael Espindola035ff0c2011-02-28 23:29:45 +00003125 Args.ClaimAllArgs(options::OPT_use_gold_plugin);
Rafael Espindola9c094fb2011-03-01 05:25:27 +00003126 Args.ClaimAllArgs(options::OPT_emit_llvm);
Daniel Dunbar47ac7d22009-03-18 06:00:36 +00003127}
3128
Jim Grosbachfc308292012-02-10 20:37:10 +00003129void ClangAs::AddARMTargetArgs(const ArgList &Args,
3130 ArgStringList &CmdArgs) const {
3131 const Driver &D = getToolChain().getDriver();
3132 llvm::Triple Triple = getToolChain().getTriple();
3133
3134 // Set the CPU based on -march= and -mcpu=.
3135 CmdArgs.push_back("-target-cpu");
Benjamin Kramer92c4fd52012-06-26 22:20:06 +00003136 CmdArgs.push_back(Args.MakeArgString(getARMTargetCPU(Args, Triple)));
Jim Grosbachfc308292012-02-10 20:37:10 +00003137
3138 // Honor -mfpu=.
Chad Rosier99317272012-04-04 20:51:35 +00003139 if (const Arg *A = Args.getLastArg(options::OPT_mfpu_EQ))
Chad Rosierf80f2a52012-04-04 20:56:36 +00003140 addFPUArgs(D, A, Args, CmdArgs);
Chad Rosier7a938fa2012-04-04 20:39:32 +00003141
3142 // Honor -mfpmath=.
3143 if (const Arg *A = Args.getLastArg(options::OPT_mfpmath_EQ))
Chad Rosier30fe6ba2012-04-04 22:13:40 +00003144 addFPMathArgs(D, A, Args, CmdArgs, getARMTargetCPU(Args, Triple));
Jim Grosbachfc308292012-02-10 20:37:10 +00003145}
3146
John McCall260611a2012-06-20 06:18:46 +00003147/// Add options related to the Objective-C runtime/ABI.
3148///
3149/// Returns true if the runtime is non-fragile.
3150ObjCRuntime Clang::AddObjCRuntimeArgs(const ArgList &args,
3151 ArgStringList &cmdArgs,
3152 RewriteKind rewriteKind) const {
3153 // Look for the controlling runtime option.
3154 Arg *runtimeArg = args.getLastArg(options::OPT_fnext_runtime,
3155 options::OPT_fgnu_runtime,
3156 options::OPT_fobjc_runtime_EQ);
3157
3158 // Just forward -fobjc-runtime= to the frontend. This supercedes
3159 // options about fragility.
3160 if (runtimeArg &&
3161 runtimeArg->getOption().matches(options::OPT_fobjc_runtime_EQ)) {
3162 ObjCRuntime runtime;
Richard Smith1d489cf2012-11-01 04:30:05 +00003163 StringRef value = runtimeArg->getValue();
John McCall260611a2012-06-20 06:18:46 +00003164 if (runtime.tryParse(value)) {
3165 getToolChain().getDriver().Diag(diag::err_drv_unknown_objc_runtime)
3166 << value;
3167 }
3168
3169 runtimeArg->render(args, cmdArgs);
3170 return runtime;
3171 }
3172
3173 // Otherwise, we'll need the ABI "version". Version numbers are
3174 // slightly confusing for historical reasons:
3175 // 1 - Traditional "fragile" ABI
3176 // 2 - Non-fragile ABI, version 1
3177 // 3 - Non-fragile ABI, version 2
3178 unsigned objcABIVersion = 1;
3179 // If -fobjc-abi-version= is present, use that to set the version.
3180 if (Arg *abiArg = args.getLastArg(options::OPT_fobjc_abi_version_EQ)) {
Richard Smith1d489cf2012-11-01 04:30:05 +00003181 StringRef value = abiArg->getValue();
John McCall260611a2012-06-20 06:18:46 +00003182 if (value == "1")
3183 objcABIVersion = 1;
3184 else if (value == "2")
3185 objcABIVersion = 2;
3186 else if (value == "3")
3187 objcABIVersion = 3;
3188 else
3189 getToolChain().getDriver().Diag(diag::err_drv_clang_unsupported)
3190 << value;
3191 } else {
3192 // Otherwise, determine if we are using the non-fragile ABI.
3193 bool nonFragileABIIsDefault =
3194 (rewriteKind == RK_NonFragile ||
3195 (rewriteKind == RK_None &&
3196 getToolChain().IsObjCNonFragileABIDefault()));
3197 if (args.hasFlag(options::OPT_fobjc_nonfragile_abi,
3198 options::OPT_fno_objc_nonfragile_abi,
3199 nonFragileABIIsDefault)) {
3200 // Determine the non-fragile ABI version to use.
3201#ifdef DISABLE_DEFAULT_NONFRAGILEABI_TWO
3202 unsigned nonFragileABIVersion = 1;
3203#else
3204 unsigned nonFragileABIVersion = 2;
3205#endif
3206
3207 if (Arg *abiArg = args.getLastArg(
3208 options::OPT_fobjc_nonfragile_abi_version_EQ)) {
Richard Smith1d489cf2012-11-01 04:30:05 +00003209 StringRef value = abiArg->getValue();
John McCall260611a2012-06-20 06:18:46 +00003210 if (value == "1")
3211 nonFragileABIVersion = 1;
3212 else if (value == "2")
3213 nonFragileABIVersion = 2;
3214 else
3215 getToolChain().getDriver().Diag(diag::err_drv_clang_unsupported)
3216 << value;
3217 }
3218
3219 objcABIVersion = 1 + nonFragileABIVersion;
3220 } else {
3221 objcABIVersion = 1;
3222 }
3223 }
3224
3225 // We don't actually care about the ABI version other than whether
3226 // it's non-fragile.
3227 bool isNonFragile = objcABIVersion != 1;
3228
3229 // If we have no runtime argument, ask the toolchain for its default runtime.
3230 // However, the rewriter only really supports the Mac runtime, so assume that.
3231 ObjCRuntime runtime;
3232 if (!runtimeArg) {
3233 switch (rewriteKind) {
3234 case RK_None:
3235 runtime = getToolChain().getDefaultObjCRuntime(isNonFragile);
3236 break;
3237 case RK_Fragile:
3238 runtime = ObjCRuntime(ObjCRuntime::FragileMacOSX, VersionTuple());
3239 break;
3240 case RK_NonFragile:
3241 runtime = ObjCRuntime(ObjCRuntime::MacOSX, VersionTuple());
3242 break;
3243 }
3244
3245 // -fnext-runtime
3246 } else if (runtimeArg->getOption().matches(options::OPT_fnext_runtime)) {
3247 // On Darwin, make this use the default behavior for the toolchain.
3248 if (getToolChain().getTriple().isOSDarwin()) {
3249 runtime = getToolChain().getDefaultObjCRuntime(isNonFragile);
3250
3251 // Otherwise, build for a generic macosx port.
3252 } else {
3253 runtime = ObjCRuntime(ObjCRuntime::MacOSX, VersionTuple());
3254 }
3255
3256 // -fgnu-runtime
3257 } else {
3258 assert(runtimeArg->getOption().matches(options::OPT_fgnu_runtime));
David Chisnalla422cd02012-07-04 10:37:03 +00003259 // Legacy behaviour is to target the gnustep runtime if we are i
3260 // non-fragile mode or the GCC runtime in fragile mode.
3261 if (isNonFragile)
David Chisnall891dac72012-10-16 15:11:55 +00003262 runtime = ObjCRuntime(ObjCRuntime::GNUstep, VersionTuple(1,6));
David Chisnalla422cd02012-07-04 10:37:03 +00003263 else
3264 runtime = ObjCRuntime(ObjCRuntime::GCC, VersionTuple());
John McCall260611a2012-06-20 06:18:46 +00003265 }
3266
3267 cmdArgs.push_back(args.MakeArgString(
3268 "-fobjc-runtime=" + runtime.getAsString()));
3269 return runtime;
3270}
3271
Daniel Dunbar20a9aa52010-05-20 21:30:13 +00003272void ClangAs::ConstructJob(Compilation &C, const JobAction &JA,
Daniel Dunbar20a9aa52010-05-20 21:30:13 +00003273 const InputInfo &Output,
3274 const InputInfoList &Inputs,
3275 const ArgList &Args,
3276 const char *LinkingOutput) const {
Daniel Dunbar20a9aa52010-05-20 21:30:13 +00003277 ArgStringList CmdArgs;
3278
3279 assert(Inputs.size() == 1 && "Unexpected number of inputs.");
3280 const InputInfo &Input = Inputs[0];
3281
Rafael Espindoladbe80d92010-11-17 22:13:25 +00003282 // Don't warn about "clang -w -c foo.s"
3283 Args.ClaimAllArgs(options::OPT_w);
Rafael Espindola9c094fb2011-03-01 05:25:27 +00003284 // and "clang -emit-llvm -c foo.s"
3285 Args.ClaimAllArgs(options::OPT_emit_llvm);
3286 // and "clang -use-gold-plugin -c foo.s"
3287 Args.ClaimAllArgs(options::OPT_use_gold_plugin);
Rafael Espindoladbe80d92010-11-17 22:13:25 +00003288
Daniel Dunbar20a9aa52010-05-20 21:30:13 +00003289 // Invoke ourselves in -cc1as mode.
3290 //
3291 // FIXME: Implement custom jobs for internal actions.
3292 CmdArgs.push_back("-cc1as");
3293
3294 // Add the "effective" target triple.
3295 CmdArgs.push_back("-triple");
Chad Rosier61ab80a2011-09-20 20:44:06 +00003296 std::string TripleStr =
3297 getToolChain().ComputeEffectiveClangTriple(Args, Input.getType());
Daniel Dunbar20a9aa52010-05-20 21:30:13 +00003298 CmdArgs.push_back(Args.MakeArgString(TripleStr));
3299
3300 // Set the output mode, we currently only expect to be used as a real
3301 // assembler.
3302 CmdArgs.push_back("-filetype");
3303 CmdArgs.push_back("obj");
3304
Joerg Sonnenberger359cf922011-05-06 14:35:16 +00003305 if (UseRelaxAll(C, Args))
Daniel Dunbar469d40e2010-05-28 16:43:21 +00003306 CmdArgs.push_back("-relax-all");
Daniel Dunbar99298002010-05-27 06:18:05 +00003307
Jim Grosbachfc308292012-02-10 20:37:10 +00003308 // Add target specific cpu and features flags.
3309 switch(getToolChain().getTriple().getArch()) {
3310 default:
3311 break;
3312
3313 case llvm::Triple::arm:
3314 case llvm::Triple::thumb:
3315 AddARMTargetArgs(Args, CmdArgs);
3316 break;
3317 }
3318
Daniel Dunbar7f6f8c82011-03-17 17:37:29 +00003319 // Ignore explicit -force_cpusubtype_ALL option.
3320 (void) Args.hasArg(options::OPT_force__cpusubtype__ALL);
Daniel Dunbar20a9aa52010-05-20 21:30:13 +00003321
Eric Christopher8f0a4032012-01-10 00:38:01 +00003322 // Determine the original source input.
3323 const Action *SourceAction = &JA;
3324 while (SourceAction->getKind() != Action::InputClass) {
3325 assert(!SourceAction->getInputs().empty() && "unexpected root action!");
3326 SourceAction = SourceAction->getInputs()[0];
3327 }
3328
3329 // Forward -g, assuming we are dealing with an actual assembly file.
3330 if (SourceAction->getType() == types::TY_Asm ||
3331 SourceAction->getType() == types::TY_PP_Asm) {
3332 Args.ClaimAllArgs(options::OPT_g_Group);
3333 if (Arg *A = Args.getLastArg(options::OPT_g_Group))
3334 if (!A->getOption().matches(options::OPT_g0))
3335 CmdArgs.push_back("-g");
3336 }
Kevin Enderby567003e2011-12-22 19:31:58 +00003337
3338 // Optionally embed the -cc1as level arguments into the debug info, for build
3339 // analysis.
3340 if (getToolChain().UseDwarfDebugFlags()) {
3341 ArgStringList OriginalArgs;
3342 for (ArgList::const_iterator it = Args.begin(),
3343 ie = Args.end(); it != ie; ++it)
3344 (*it)->render(Args, OriginalArgs);
3345
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00003346 SmallString<256> Flags;
Kevin Enderby567003e2011-12-22 19:31:58 +00003347 const char *Exec = getToolChain().getDriver().getClangProgramPath();
3348 Flags += Exec;
3349 for (unsigned i = 0, e = OriginalArgs.size(); i != e; ++i) {
3350 Flags += " ";
3351 Flags += OriginalArgs[i];
3352 }
3353 CmdArgs.push_back("-dwarf-debug-flags");
3354 CmdArgs.push_back(Args.MakeArgString(Flags.str()));
3355 }
Daniel Dunbar20a9aa52010-05-20 21:30:13 +00003356
3357 // FIXME: Add -static support, once we have it.
3358
3359 Args.AddAllArgValues(CmdArgs, options::OPT_Wa_COMMA,
3360 options::OPT_Xassembler);
Daniel Dunbar3df23252011-04-29 17:53:18 +00003361 Args.AddAllArgs(CmdArgs, options::OPT_mllvm);
Daniel Dunbar20a9aa52010-05-20 21:30:13 +00003362
3363 assert(Output.isFilename() && "Unexpected lipo output.");
3364 CmdArgs.push_back("-o");
3365 CmdArgs.push_back(Output.getFilename());
3366
Daniel Dunbar7c1e4652010-08-02 02:38:21 +00003367 assert(Input.isFilename() && "Invalid input.");
3368 CmdArgs.push_back(Input.getFilename());
Daniel Dunbar20a9aa52010-05-20 21:30:13 +00003369
Daniel Dunbara001c1c2010-07-18 21:16:15 +00003370 const char *Exec = getToolChain().getDriver().getClangProgramPath();
Daniel Dunbar2fe238e2010-08-02 02:38:28 +00003371 C.addCommand(new Command(JA, *this, Exec, CmdArgs));
Daniel Dunbar20a9aa52010-05-20 21:30:13 +00003372}
3373
Daniel Dunbarb488c1d2009-03-18 08:07:30 +00003374void gcc::Common::ConstructJob(Compilation &C, const JobAction &JA,
Daniel Dunbarb488c1d2009-03-18 08:07:30 +00003375 const InputInfo &Output,
3376 const InputInfoList &Inputs,
Daniel Dunbar1d460332009-03-18 10:01:51 +00003377 const ArgList &Args,
Daniel Dunbarb488c1d2009-03-18 08:07:30 +00003378 const char *LinkingOutput) const {
Daniel Dunbaree788e72009-12-21 18:54:17 +00003379 const Driver &D = getToolChain().getDriver();
Daniel Dunbarb488c1d2009-03-18 08:07:30 +00003380 ArgStringList CmdArgs;
Daniel Dunbar47ac7d22009-03-18 06:00:36 +00003381
Daniel Dunbarc21c4852009-04-08 23:54:23 +00003382 for (ArgList::const_iterator
Daniel Dunbar1d460332009-03-18 10:01:51 +00003383 it = Args.begin(), ie = Args.end(); it != ie; ++it) {
Daniel Dunbarb488c1d2009-03-18 08:07:30 +00003384 Arg *A = *it;
Michael J. Spencer91e06da2012-10-19 22:37:06 +00003385 if (forwardToGCC(A->getOption())) {
Daniel Dunbar2dffe2d2010-08-03 16:14:14 +00003386 // Don't forward any -g arguments to assembly steps.
3387 if (isa<AssembleJobAction>(JA) &&
3388 A->getOption().matches(options::OPT_g_Group))
3389 continue;
3390
Daniel Dunbar75877192009-03-19 07:55:12 +00003391 // It is unfortunate that we have to claim here, as this means
3392 // we will basically never report anything interesting for
Daniel Dunbar6ecc7a92009-05-02 21:41:52 +00003393 // platforms using a generic gcc, even if we are just using gcc
3394 // to get to the assembler.
Daniel Dunbar75877192009-03-19 07:55:12 +00003395 A->claim();
Daniel Dunbar1d460332009-03-18 10:01:51 +00003396 A->render(Args, CmdArgs);
Daniel Dunbar75877192009-03-19 07:55:12 +00003397 }
Daniel Dunbarb488c1d2009-03-18 08:07:30 +00003398 }
Daniel Dunbarc21c4852009-04-08 23:54:23 +00003399
Daniel Dunbar82b51cc2010-01-25 22:35:08 +00003400 RenderExtraToolArgs(JA, CmdArgs);
Daniel Dunbarb488c1d2009-03-18 08:07:30 +00003401
3402 // If using a driver driver, force the arch.
Rafael Espindola64f7ad92012-10-07 04:44:33 +00003403 llvm::Triple::ArchType Arch = getToolChain().getArch();
Bob Wilson905c45f2011-10-14 05:03:44 +00003404 if (getToolChain().getTriple().isOSDarwin()) {
Daniel Dunbarb488c1d2009-03-18 08:07:30 +00003405 CmdArgs.push_back("-arch");
Daniel Dunbarbf54a062009-04-01 20:33:11 +00003406
3407 // FIXME: Remove these special cases.
Rafael Espindola64f7ad92012-10-07 04:44:33 +00003408 if (Arch == llvm::Triple::ppc)
Daniel Dunbar7cfe31a2009-05-22 02:21:04 +00003409 CmdArgs.push_back("ppc");
Rafael Espindola64f7ad92012-10-07 04:44:33 +00003410 else if (Arch == llvm::Triple::ppc64)
Daniel Dunbar7cfe31a2009-05-22 02:21:04 +00003411 CmdArgs.push_back("ppc64");
3412 else
Rafael Espindola64f7ad92012-10-07 04:44:33 +00003413 CmdArgs.push_back(Args.MakeArgString(getToolChain().getArchName()));
Daniel Dunbarb488c1d2009-03-18 08:07:30 +00003414 }
3415
Daniel Dunbar6ecc7a92009-05-02 21:41:52 +00003416 // Try to force gcc to match the tool chain we want, if we recognize
3417 // the arch.
Daniel Dunbar7cfe31a2009-05-22 02:21:04 +00003418 //
3419 // FIXME: The triple class should directly provide the information we want
3420 // here.
Rafael Espindola64f7ad92012-10-07 04:44:33 +00003421 if (Arch == llvm::Triple::x86 || Arch == llvm::Triple::ppc)
Daniel Dunbar6ecc7a92009-05-02 21:41:52 +00003422 CmdArgs.push_back("-m32");
Rafael Espindola64f7ad92012-10-07 04:44:33 +00003423 else if (Arch == llvm::Triple::x86_64 || Arch == llvm::Triple::x86_64)
Daniel Dunbar6ecc7a92009-05-02 21:41:52 +00003424 CmdArgs.push_back("-m64");
3425
Daniel Dunbar7c1e4652010-08-02 02:38:21 +00003426 if (Output.isFilename()) {
Daniel Dunbarb488c1d2009-03-18 08:07:30 +00003427 CmdArgs.push_back("-o");
Daniel Dunbar115a7922009-03-19 07:29:38 +00003428 CmdArgs.push_back(Output.getFilename());
3429 } else {
3430 assert(Output.isNothing() && "Unexpected output");
Daniel Dunbarb488c1d2009-03-18 08:07:30 +00003431 CmdArgs.push_back("-fsyntax-only");
Daniel Dunbar115a7922009-03-19 07:29:38 +00003432 }
Daniel Dunbarb488c1d2009-03-18 08:07:30 +00003433
Tony Linthicum96319392011-12-12 21:14:55 +00003434 Args.AddAllArgValues(CmdArgs, options::OPT_Wa_COMMA,
3435 options::OPT_Xassembler);
Daniel Dunbarb488c1d2009-03-18 08:07:30 +00003436
3437 // Only pass -x if gcc will understand it; otherwise hope gcc
3438 // understands the suffix correctly. The main use case this would go
3439 // wrong in is for linker inputs if they happened to have an odd
3440 // suffix; really the only way to get this to happen is a command
3441 // like '-x foobar a.c' which will treat a.c like a linker input.
3442 //
3443 // FIXME: For the linker case specifically, can we safely convert
3444 // inputs into '-Wl,' options?
3445 for (InputInfoList::const_iterator
3446 it = Inputs.begin(), ie = Inputs.end(); it != ie; ++it) {
3447 const InputInfo &II = *it;
Daniel Dunbara8304f62009-05-02 20:14:53 +00003448
Daniel Dunbar5915fbf2009-09-01 16:57:46 +00003449 // Don't try to pass LLVM or AST inputs to a generic gcc.
Daniel Dunbar6c6424b2010-06-07 23:28:45 +00003450 if (II.getType() == types::TY_LLVM_IR || II.getType() == types::TY_LTO_IR ||
3451 II.getType() == types::TY_LLVM_BC || II.getType() == types::TY_LTO_BC)
Chris Lattner5f9e2722011-07-23 10:55:15 +00003452 D.Diag(diag::err_drv_no_linker_llvm_support)
Daniel Dunbar88137642009-09-09 22:32:48 +00003453 << getToolChain().getTripleString();
Daniel Dunbar5915fbf2009-09-01 16:57:46 +00003454 else if (II.getType() == types::TY_AST)
Chris Lattner5f9e2722011-07-23 10:55:15 +00003455 D.Diag(diag::err_drv_no_ast_support)
Daniel Dunbar88137642009-09-09 22:32:48 +00003456 << getToolChain().getTripleString();
Daniel Dunbara8304f62009-05-02 20:14:53 +00003457
Daniel Dunbarb488c1d2009-03-18 08:07:30 +00003458 if (types::canTypeBeUserSpecified(II.getType())) {
3459 CmdArgs.push_back("-x");
3460 CmdArgs.push_back(types::getTypeName(II.getType()));
3461 }
3462
Daniel Dunbar7c1e4652010-08-02 02:38:21 +00003463 if (II.isFilename())
Daniel Dunbar115a7922009-03-19 07:29:38 +00003464 CmdArgs.push_back(II.getFilename());
Daniel Dunbar48f99942010-09-25 18:10:05 +00003465 else {
3466 const Arg &A = II.getInputArg();
3467
3468 // Reverse translate some rewritten options.
3469 if (A.getOption().matches(options::OPT_Z_reserved_lib_stdcxx)) {
3470 CmdArgs.push_back("-lstdc++");
3471 continue;
3472 }
3473
Daniel Dunbar115a7922009-03-19 07:29:38 +00003474 // Don't render as input, we need gcc to do the translations.
Daniel Dunbar48f99942010-09-25 18:10:05 +00003475 A.render(Args, CmdArgs);
3476 }
Daniel Dunbarb488c1d2009-03-18 08:07:30 +00003477 }
3478
Dylan Noblesmithb8a3e812011-04-09 13:31:59 +00003479 const std::string customGCCName = D.getCCCGenericGCCName();
3480 const char *GCCName;
3481 if (!customGCCName.empty())
3482 GCCName = customGCCName.c_str();
3483 else if (D.CCCIsCXX) {
Dylan Noblesmithb8a3e812011-04-09 13:31:59 +00003484 GCCName = "g++";
Dylan Noblesmithb8a3e812011-04-09 13:31:59 +00003485 } else
3486 GCCName = "gcc";
3487
Daniel Dunbarc21c4852009-04-08 23:54:23 +00003488 const char *Exec =
Daniel Dunbar4a7e8892010-07-14 18:46:23 +00003489 Args.MakeArgString(getToolChain().GetProgramPath(GCCName));
Daniel Dunbar2fe238e2010-08-02 02:38:28 +00003490 C.addCommand(new Command(JA, *this, Exec, CmdArgs));
Daniel Dunbar47ac7d22009-03-18 06:00:36 +00003491}
3492
Daniel Dunbar82b51cc2010-01-25 22:35:08 +00003493void gcc::Preprocess::RenderExtraToolArgs(const JobAction &JA,
3494 ArgStringList &CmdArgs) const {
Daniel Dunbarb488c1d2009-03-18 08:07:30 +00003495 CmdArgs.push_back("-E");
Daniel Dunbar47ac7d22009-03-18 06:00:36 +00003496}
3497
Daniel Dunbar82b51cc2010-01-25 22:35:08 +00003498void gcc::Precompile::RenderExtraToolArgs(const JobAction &JA,
3499 ArgStringList &CmdArgs) const {
Daniel Dunbarb488c1d2009-03-18 08:07:30 +00003500 // The type is good enough.
Daniel Dunbar47ac7d22009-03-18 06:00:36 +00003501}
3502
Daniel Dunbar82b51cc2010-01-25 22:35:08 +00003503void gcc::Compile::RenderExtraToolArgs(const JobAction &JA,
3504 ArgStringList &CmdArgs) const {
Daniel Dunbar64952502010-02-11 03:16:21 +00003505 const Driver &D = getToolChain().getDriver();
3506
Daniel Dunbar82b51cc2010-01-25 22:35:08 +00003507 // If -flto, etc. are present then make sure not to force assembly output.
Daniel Dunbar6c6424b2010-06-07 23:28:45 +00003508 if (JA.getType() == types::TY_LLVM_IR || JA.getType() == types::TY_LTO_IR ||
3509 JA.getType() == types::TY_LLVM_BC || JA.getType() == types::TY_LTO_BC)
Daniel Dunbar82b51cc2010-01-25 22:35:08 +00003510 CmdArgs.push_back("-c");
Daniel Dunbar64952502010-02-11 03:16:21 +00003511 else {
3512 if (JA.getType() != types::TY_PP_Asm)
Chris Lattner5f9e2722011-07-23 10:55:15 +00003513 D.Diag(diag::err_drv_invalid_gcc_output_type)
Daniel Dunbar64952502010-02-11 03:16:21 +00003514 << getTypeName(JA.getType());
Michael J. Spencer20249a12010-10-21 03:16:25 +00003515
Daniel Dunbar82b51cc2010-01-25 22:35:08 +00003516 CmdArgs.push_back("-S");
Daniel Dunbar64952502010-02-11 03:16:21 +00003517 }
Daniel Dunbar47ac7d22009-03-18 06:00:36 +00003518}
3519
Daniel Dunbar82b51cc2010-01-25 22:35:08 +00003520void gcc::Assemble::RenderExtraToolArgs(const JobAction &JA,
3521 ArgStringList &CmdArgs) const {
Daniel Dunbarb488c1d2009-03-18 08:07:30 +00003522 CmdArgs.push_back("-c");
Daniel Dunbar47ac7d22009-03-18 06:00:36 +00003523}
Daniel Dunbarb488c1d2009-03-18 08:07:30 +00003524
Daniel Dunbar82b51cc2010-01-25 22:35:08 +00003525void gcc::Link::RenderExtraToolArgs(const JobAction &JA,
3526 ArgStringList &CmdArgs) const {
Daniel Dunbarb488c1d2009-03-18 08:07:30 +00003527 // The types are (hopefully) good enough.
3528}
3529
Tony Linthicum96319392011-12-12 21:14:55 +00003530// Hexagon tools start.
3531void hexagon::Assemble::RenderExtraToolArgs(const JobAction &JA,
3532 ArgStringList &CmdArgs) const {
3533
3534}
3535void hexagon::Assemble::ConstructJob(Compilation &C, const JobAction &JA,
3536 const InputInfo &Output,
3537 const InputInfoList &Inputs,
3538 const ArgList &Args,
3539 const char *LinkingOutput) const {
3540
3541 const Driver &D = getToolChain().getDriver();
3542 ArgStringList CmdArgs;
3543
3544 std::string MarchString = "-march=";
3545 MarchString += getHexagonTargetCPU(Args);
3546 CmdArgs.push_back(Args.MakeArgString(MarchString));
3547
3548 RenderExtraToolArgs(JA, CmdArgs);
3549
3550 if (Output.isFilename()) {
3551 CmdArgs.push_back("-o");
3552 CmdArgs.push_back(Output.getFilename());
3553 } else {
3554 assert(Output.isNothing() && "Unexpected output");
3555 CmdArgs.push_back("-fsyntax-only");
3556 }
3557
3558
3559 // Only pass -x if gcc will understand it; otherwise hope gcc
3560 // understands the suffix correctly. The main use case this would go
3561 // wrong in is for linker inputs if they happened to have an odd
3562 // suffix; really the only way to get this to happen is a command
3563 // like '-x foobar a.c' which will treat a.c like a linker input.
3564 //
3565 // FIXME: For the linker case specifically, can we safely convert
3566 // inputs into '-Wl,' options?
3567 for (InputInfoList::const_iterator
3568 it = Inputs.begin(), ie = Inputs.end(); it != ie; ++it) {
3569 const InputInfo &II = *it;
3570
3571 // Don't try to pass LLVM or AST inputs to a generic gcc.
3572 if (II.getType() == types::TY_LLVM_IR || II.getType() == types::TY_LTO_IR ||
3573 II.getType() == types::TY_LLVM_BC || II.getType() == types::TY_LTO_BC)
3574 D.Diag(clang::diag::err_drv_no_linker_llvm_support)
3575 << getToolChain().getTripleString();
3576 else if (II.getType() == types::TY_AST)
3577 D.Diag(clang::diag::err_drv_no_ast_support)
3578 << getToolChain().getTripleString();
3579
3580 if (II.isFilename())
3581 CmdArgs.push_back(II.getFilename());
3582 else
3583 // Don't render as input, we need gcc to do the translations. FIXME: Pranav: What is this ?
3584 II.getInputArg().render(Args, CmdArgs);
3585 }
3586
3587 const char *GCCName = "hexagon-as";
3588 const char *Exec =
3589 Args.MakeArgString(getToolChain().GetProgramPath(GCCName));
3590 C.addCommand(new Command(JA, *this, Exec, CmdArgs));
3591
3592}
3593void hexagon::Link::RenderExtraToolArgs(const JobAction &JA,
3594 ArgStringList &CmdArgs) const {
3595 // The types are (hopefully) good enough.
3596}
3597
3598void hexagon::Link::ConstructJob(Compilation &C, const JobAction &JA,
3599 const InputInfo &Output,
3600 const InputInfoList &Inputs,
3601 const ArgList &Args,
3602 const char *LinkingOutput) const {
3603
3604 const Driver &D = getToolChain().getDriver();
3605 ArgStringList CmdArgs;
3606
3607 for (ArgList::const_iterator
3608 it = Args.begin(), ie = Args.end(); it != ie; ++it) {
3609 Arg *A = *it;
Michael J. Spencer91e06da2012-10-19 22:37:06 +00003610 if (forwardToGCC(A->getOption())) {
Tony Linthicum96319392011-12-12 21:14:55 +00003611 // Don't forward any -g arguments to assembly steps.
3612 if (isa<AssembleJobAction>(JA) &&
3613 A->getOption().matches(options::OPT_g_Group))
3614 continue;
3615
3616 // It is unfortunate that we have to claim here, as this means
3617 // we will basically never report anything interesting for
3618 // platforms using a generic gcc, even if we are just using gcc
3619 // to get to the assembler.
3620 A->claim();
3621 A->render(Args, CmdArgs);
3622 }
3623 }
3624
3625 RenderExtraToolArgs(JA, CmdArgs);
3626
3627 // Add Arch Information
3628 Arg *A;
Sebastian Pop43115d42012-01-13 20:37:10 +00003629 if ((A = getLastHexagonArchArg(Args))) {
3630 if (A->getOption().matches(options::OPT_m_Joined))
3631 A->render(Args, CmdArgs);
Tony Linthicum96319392011-12-12 21:14:55 +00003632 else
Sebastian Pop43115d42012-01-13 20:37:10 +00003633 CmdArgs.push_back (Args.MakeArgString("-m" + getHexagonTargetCPU(Args)));
Tony Linthicum96319392011-12-12 21:14:55 +00003634 }
Sebastian Pop43115d42012-01-13 20:37:10 +00003635 else {
3636 CmdArgs.push_back (Args.MakeArgString("-m" + getHexagonTargetCPU(Args)));
3637 }
3638
Tony Linthicum96319392011-12-12 21:14:55 +00003639 CmdArgs.push_back("-mqdsp6-compat");
3640
3641 const char *GCCName;
3642 if (C.getDriver().CCCIsCXX)
3643 GCCName = "hexagon-g++";
3644 else
3645 GCCName = "hexagon-gcc";
3646 const char *Exec =
3647 Args.MakeArgString(getToolChain().GetProgramPath(GCCName));
3648
3649 if (Output.isFilename()) {
3650 CmdArgs.push_back("-o");
3651 CmdArgs.push_back(Output.getFilename());
3652 }
3653
3654 for (InputInfoList::const_iterator
3655 it = Inputs.begin(), ie = Inputs.end(); it != ie; ++it) {
3656 const InputInfo &II = *it;
3657
3658 // Don't try to pass LLVM or AST inputs to a generic gcc.
3659 if (II.getType() == types::TY_LLVM_IR || II.getType() == types::TY_LTO_IR ||
3660 II.getType() == types::TY_LLVM_BC || II.getType() == types::TY_LTO_BC)
3661 D.Diag(clang::diag::err_drv_no_linker_llvm_support)
3662 << getToolChain().getTripleString();
3663 else if (II.getType() == types::TY_AST)
3664 D.Diag(clang::diag::err_drv_no_ast_support)
3665 << getToolChain().getTripleString();
3666
3667 if (II.isFilename())
3668 CmdArgs.push_back(II.getFilename());
3669 else
3670 // Don't render as input, we need gcc to do the translations. FIXME: Pranav: What is this ?
3671 II.getInputArg().render(Args, CmdArgs);
3672 }
3673 C.addCommand(new Command(JA, *this, Exec, CmdArgs));
3674
3675}
3676// Hexagon tools end.
3677
Rafael Espindolacfed8282012-10-31 18:51:07 +00003678llvm::Triple::ArchType darwin::getArchTypeForDarwinArchName(StringRef Str) {
3679 // See arch(3) and llvm-gcc's driver-driver.c. We don't implement support for
3680 // archs which Darwin doesn't use.
3681
3682 // The matching this routine does is fairly pointless, since it is neither the
3683 // complete architecture list, nor a reasonable subset. The problem is that
3684 // historically the driver driver accepts this and also ties its -march=
3685 // handling to the architecture name, so we need to be careful before removing
3686 // support for it.
3687
3688 // This code must be kept in sync with Clang's Darwin specific argument
3689 // translation.
3690
3691 return llvm::StringSwitch<llvm::Triple::ArchType>(Str)
3692 .Cases("ppc", "ppc601", "ppc603", "ppc604", "ppc604e", llvm::Triple::ppc)
3693 .Cases("ppc750", "ppc7400", "ppc7450", "ppc970", llvm::Triple::ppc)
3694 .Case("ppc64", llvm::Triple::ppc64)
3695 .Cases("i386", "i486", "i486SX", "i586", "i686", llvm::Triple::x86)
3696 .Cases("pentium", "pentpro", "pentIIm3", "pentIIm5", "pentium4",
3697 llvm::Triple::x86)
3698 .Case("x86_64", llvm::Triple::x86_64)
3699 // This is derived from the driver driver.
3700 .Cases("arm", "armv4t", "armv5", "armv6", llvm::Triple::arm)
3701 .Cases("armv7", "armv7f", "armv7k", "armv7s", "xscale", llvm::Triple::arm)
3702 .Case("r600", llvm::Triple::r600)
3703 .Case("nvptx", llvm::Triple::nvptx)
3704 .Case("nvptx64", llvm::Triple::nvptx64)
3705 .Case("amdil", llvm::Triple::amdil)
3706 .Case("spir", llvm::Triple::spir)
3707 .Default(llvm::Triple::UnknownArch);
3708}
Tony Linthicum96319392011-12-12 21:14:55 +00003709
Daniel Dunbar40f12652009-03-29 17:08:39 +00003710const char *darwin::CC1::getCC1Name(types::ID Type) const {
3711 switch (Type) {
3712 default:
David Blaikieb219cfc2011-09-23 05:06:16 +00003713 llvm_unreachable("Unexpected type for Darwin CC1 tool.");
Daniel Dunbar40f12652009-03-29 17:08:39 +00003714 case types::TY_Asm:
3715 case types::TY_C: case types::TY_CHeader:
3716 case types::TY_PP_C: case types::TY_PP_CHeader:
3717 return "cc1";
3718 case types::TY_ObjC: case types::TY_ObjCHeader:
Nico Webercc52a062011-08-13 23:13:37 +00003719 case types::TY_PP_ObjC: case types::TY_PP_ObjC_Alias:
3720 case types::TY_PP_ObjCHeader:
Daniel Dunbar40f12652009-03-29 17:08:39 +00003721 return "cc1obj";
3722 case types::TY_CXX: case types::TY_CXXHeader:
3723 case types::TY_PP_CXX: case types::TY_PP_CXXHeader:
3724 return "cc1plus";
3725 case types::TY_ObjCXX: case types::TY_ObjCXXHeader:
Nico Webercc52a062011-08-13 23:13:37 +00003726 case types::TY_PP_ObjCXX: case types::TY_PP_ObjCXX_Alias:
3727 case types::TY_PP_ObjCXXHeader:
Daniel Dunbar40f12652009-03-29 17:08:39 +00003728 return "cc1objplus";
3729 }
3730}
3731
David Blaikie99ba9e32011-12-20 02:48:34 +00003732void darwin::CC1::anchor() {}
3733
Daniel Dunbarc21c4852009-04-08 23:54:23 +00003734const char *darwin::CC1::getBaseInputName(const ArgList &Args,
Daniel Dunbara5a7bd02009-03-30 00:34:04 +00003735 const InputInfoList &Inputs) {
Michael J. Spencer472ccff2010-12-18 00:19:12 +00003736 return Args.MakeArgString(
3737 llvm::sys::path::filename(Inputs[0].getBaseInput()));
Daniel Dunbara3ec60e2009-03-29 18:40:18 +00003738}
3739
Daniel Dunbarc21c4852009-04-08 23:54:23 +00003740const char *darwin::CC1::getBaseInputStem(const ArgList &Args,
Daniel Dunbara5a7bd02009-03-30 00:34:04 +00003741 const InputInfoList &Inputs) {
Daniel Dunbara3ec60e2009-03-29 18:40:18 +00003742 const char *Str = getBaseInputName(Args, Inputs);
3743
Chris Lattner657ca662011-01-16 08:14:11 +00003744 if (const char *End = strrchr(Str, '.'))
Daniel Dunbar88137642009-09-09 22:32:48 +00003745 return Args.MakeArgString(std::string(Str, End));
Daniel Dunbara3ec60e2009-03-29 18:40:18 +00003746
3747 return Str;
3748}
3749
3750const char *
Daniel Dunbarc21c4852009-04-08 23:54:23 +00003751darwin::CC1::getDependencyFileName(const ArgList &Args,
Daniel Dunbara5a7bd02009-03-30 00:34:04 +00003752 const InputInfoList &Inputs) {
Daniel Dunbara3ec60e2009-03-29 18:40:18 +00003753 // FIXME: Think about this more.
3754 std::string Res;
3755
3756 if (Arg *OutputOpt = Args.getLastArg(options::OPT_o)) {
Richard Smith1d489cf2012-11-01 04:30:05 +00003757 std::string Str(OutputOpt->getValue());
Daniel Dunbara3ec60e2009-03-29 18:40:18 +00003758 Res = Str.substr(0, Str.rfind('.'));
Chad Rosier30601782011-08-17 23:08:45 +00003759 } else {
Daniel Dunbara5a7bd02009-03-30 00:34:04 +00003760 Res = darwin::CC1::getBaseInputStem(Args, Inputs);
Chad Rosier30601782011-08-17 23:08:45 +00003761 }
Daniel Dunbar88137642009-09-09 22:32:48 +00003762 return Args.MakeArgString(Res + ".d");
Daniel Dunbara3ec60e2009-03-29 18:40:18 +00003763}
3764
Chad Rosier285f9a22011-08-17 18:24:55 +00003765void darwin::CC1::RemoveCC1UnsupportedArgs(ArgStringList &CmdArgs) const {
Eric Christopher88b7cf02011-08-19 00:30:14 +00003766 for (ArgStringList::iterator it = CmdArgs.begin(), ie = CmdArgs.end();
Chad Rosier285f9a22011-08-17 18:24:55 +00003767 it != ie;) {
Chad Rosier8722a5d2011-08-18 17:56:32 +00003768
3769 StringRef Option = *it;
Chad Rosier80717972011-08-26 18:30:43 +00003770 bool RemoveOption = false;
Chad Rosier04225c12011-08-18 01:18:28 +00003771
Bob Wilson2872c8d2012-02-07 01:17:55 +00003772 // Erase both -fmodule-cache-path and its argument.
3773 if (Option.equals("-fmodule-cache-path") && it+2 != ie) {
3774 it = CmdArgs.erase(it, it+2);
Chad Rosier80717972011-08-26 18:30:43 +00003775 ie = CmdArgs.end();
Chad Rosier04225c12011-08-18 01:18:28 +00003776 continue;
3777 }
3778
Bob Wilson2872c8d2012-02-07 01:17:55 +00003779 // Remove unsupported -f options.
3780 if (Option.startswith("-f")) {
3781 // Remove -f/-fno- to reduce the number of cases.
3782 if (Option.startswith("-fno-"))
3783 Option = Option.substr(5);
3784 else
3785 Option = Option.substr(2);
3786 RemoveOption = llvm::StringSwitch<bool>(Option)
3787 .Case("altivec", true)
3788 .Case("modules", true)
3789 .Case("diagnostics-show-note-include-stack", true)
3790 .Default(false);
3791 }
3792
Chad Rosier80717972011-08-26 18:30:43 +00003793 // Handle machine specific options.
3794 if (Option.startswith("-m")) {
3795 RemoveOption = llvm::StringSwitch<bool>(Option)
3796 .Case("-mthumb", true)
3797 .Case("-mno-thumb", true)
3798 .Case("-mno-fused-madd", true)
3799 .Case("-mlong-branch", true)
3800 .Case("-mlongcall", true)
3801 .Case("-mcpu=G4", true)
3802 .Case("-mcpu=G5", true)
3803 .Default(false);
3804 }
3805
3806 // Handle warning options.
3807 if (Option.startswith("-W")) {
3808 // Remove -W/-Wno- to reduce the number of cases.
3809 if (Option.startswith("-Wno-"))
3810 Option = Option.substr(5);
3811 else
3812 Option = Option.substr(2);
3813
3814 RemoveOption = llvm::StringSwitch<bool>(Option)
3815 .Case("address-of-temporary", true)
3816 .Case("ambiguous-member-template", true)
3817 .Case("analyzer-incompatible-plugin", true)
3818 .Case("array-bounds", true)
3819 .Case("array-bounds-pointer-arithmetic", true)
3820 .Case("bind-to-temporary-copy", true)
3821 .Case("bitwise-op-parentheses", true)
3822 .Case("bool-conversions", true)
3823 .Case("builtin-macro-redefined", true)
3824 .Case("c++-hex-floats", true)
3825 .Case("c++0x-compat", true)
3826 .Case("c++0x-extensions", true)
3827 .Case("c++0x-narrowing", true)
Richard Smith575fdda2011-10-13 23:32:09 +00003828 .Case("c++11-compat", true)
3829 .Case("c++11-extensions", true)
3830 .Case("c++11-narrowing", true)
Chad Rosier80717972011-08-26 18:30:43 +00003831 .Case("conditional-uninitialized", true)
3832 .Case("constant-conversion", true)
David Blaikie95187bd2012-03-15 04:50:32 +00003833 .Case("conversion-null", true)
Chad Rosier80717972011-08-26 18:30:43 +00003834 .Case("CFString-literal", true)
3835 .Case("constant-logical-operand", true)
3836 .Case("custom-atomic-properties", true)
3837 .Case("default-arg-special-member", true)
3838 .Case("delegating-ctor-cycles", true)
3839 .Case("delete-non-virtual-dtor", true)
3840 .Case("deprecated-implementations", true)
3841 .Case("deprecated-writable-strings", true)
3842 .Case("distributed-object-modifiers", true)
3843 .Case("duplicate-method-arg", true)
3844 .Case("dynamic-class-memaccess", true)
3845 .Case("enum-compare", true)
Ted Kremenek2c3d8fa2012-10-09 19:29:48 +00003846 .Case("enum-conversion", true)
Chad Rosier80717972011-08-26 18:30:43 +00003847 .Case("exit-time-destructors", true)
3848 .Case("gnu", true)
3849 .Case("gnu-designator", true)
3850 .Case("header-hygiene", true)
3851 .Case("idiomatic-parentheses", true)
3852 .Case("ignored-qualifiers", true)
3853 .Case("implicit-atomic-properties", true)
3854 .Case("incompatible-pointer-types", true)
3855 .Case("incomplete-implementation", true)
Ted Kremenek28e2aff2012-10-09 19:29:46 +00003856 .Case("int-conversion", true)
Chad Rosier80717972011-08-26 18:30:43 +00003857 .Case("initializer-overrides", true)
3858 .Case("invalid-noreturn", true)
3859 .Case("invalid-token-paste", true)
Ivan Krasin08f35a72011-10-06 02:46:34 +00003860 .Case("language-extension-token", true)
Chad Rosier80717972011-08-26 18:30:43 +00003861 .Case("literal-conversion", true)
3862 .Case("literal-range", true)
3863 .Case("local-type-template-args", true)
3864 .Case("logical-op-parentheses", true)
3865 .Case("method-signatures", true)
3866 .Case("microsoft", true)
3867 .Case("mismatched-tags", true)
3868 .Case("missing-method-return-type", true)
3869 .Case("non-pod-varargs", true)
3870 .Case("nonfragile-abi2", true)
3871 .Case("null-arithmetic", true)
3872 .Case("null-dereference", true)
3873 .Case("out-of-line-declaration", true)
3874 .Case("overriding-method-mismatch", true)
3875 .Case("readonly-setter-attrs", true)
3876 .Case("return-stack-address", true)
3877 .Case("self-assign", true)
3878 .Case("semicolon-before-method-body", true)
3879 .Case("sentinel", true)
3880 .Case("shift-overflow", true)
3881 .Case("shift-sign-overflow", true)
3882 .Case("sign-conversion", true)
3883 .Case("sizeof-array-argument", true)
3884 .Case("sizeof-pointer-memaccess", true)
3885 .Case("string-compare", true)
3886 .Case("super-class-method-mismatch", true)
3887 .Case("tautological-compare", true)
3888 .Case("typedef-redefinition", true)
3889 .Case("typename-missing", true)
3890 .Case("undefined-reinterpret-cast", true)
3891 .Case("unknown-warning-option", true)
3892 .Case("unnamed-type-template-args", true)
3893 .Case("unneeded-internal-declaration", true)
3894 .Case("unneeded-member-function", true)
3895 .Case("unused-comparison", true)
3896 .Case("unused-exception-parameter", true)
3897 .Case("unused-member-function", true)
3898 .Case("unused-result", true)
3899 .Case("vector-conversions", true)
3900 .Case("vla", true)
3901 .Case("used-but-marked-unused", true)
3902 .Case("weak-vtables", true)
3903 .Default(false);
3904 } // if (Option.startswith("-W"))
Chad Rosier04225c12011-08-18 01:18:28 +00003905 if (RemoveOption) {
Chad Rosier285f9a22011-08-17 18:24:55 +00003906 it = CmdArgs.erase(it);
Chad Rosiercc0de8c2011-08-17 18:51:56 +00003907 ie = CmdArgs.end();
Chad Rosier30601782011-08-17 23:08:45 +00003908 } else {
Chad Rosier285f9a22011-08-17 18:24:55 +00003909 ++it;
Chad Rosier30601782011-08-17 23:08:45 +00003910 }
Chad Rosier285f9a22011-08-17 18:24:55 +00003911 }
3912}
3913
Daniel Dunbarc21c4852009-04-08 23:54:23 +00003914void darwin::CC1::AddCC1Args(const ArgList &Args,
Daniel Dunbara3ec60e2009-03-29 18:40:18 +00003915 ArgStringList &CmdArgs) const {
Daniel Dunbaree788e72009-12-21 18:54:17 +00003916 const Driver &D = getToolChain().getDriver();
Daniel Dunbare2fd6642009-09-10 01:21:12 +00003917
3918 CheckCodeGenerationOptions(D, Args);
3919
Daniel Dunbara3ec60e2009-03-29 18:40:18 +00003920 // Derived from cc1 spec.
Daniel Dunbar7a0c0642012-10-15 22:23:53 +00003921 if ((!Args.hasArg(options::OPT_mkernel) ||
3922 (getDarwinToolChain().isTargetIPhoneOS() &&
3923 !getDarwinToolChain().isIPhoneOSVersionLT(6, 0))) &&
3924 !Args.hasArg(options::OPT_static) &&
Daniel Dunbara3ec60e2009-03-29 18:40:18 +00003925 !Args.hasArg(options::OPT_mdynamic_no_pic))
3926 CmdArgs.push_back("-fPIC");
3927
Daniel Dunbar2ba91572009-09-10 03:37:02 +00003928 if (getToolChain().getTriple().getArch() == llvm::Triple::arm ||
3929 getToolChain().getTriple().getArch() == llvm::Triple::thumb) {
3930 if (!Args.hasArg(options::OPT_fbuiltin_strcat))
3931 CmdArgs.push_back("-fno-builtin-strcat");
3932 if (!Args.hasArg(options::OPT_fbuiltin_strcpy))
3933 CmdArgs.push_back("-fno-builtin-strcpy");
3934 }
3935
Daniel Dunbara3ec60e2009-03-29 18:40:18 +00003936 if (Args.hasArg(options::OPT_g_Flag) &&
3937 !Args.hasArg(options::OPT_fno_eliminate_unused_debug_symbols))
3938 CmdArgs.push_back("-feliminate-unused-debug-symbols");
3939}
3940
3941void darwin::CC1::AddCC1OptionsArgs(const ArgList &Args, ArgStringList &CmdArgs,
3942 const InputInfoList &Inputs,
3943 const ArgStringList &OutputArgs) const {
Daniel Dunbaree788e72009-12-21 18:54:17 +00003944 const Driver &D = getToolChain().getDriver();
Daniel Dunbara3ec60e2009-03-29 18:40:18 +00003945
3946 // Derived from cc1_options spec.
3947 if (Args.hasArg(options::OPT_fast) ||
3948 Args.hasArg(options::OPT_fastf) ||
3949 Args.hasArg(options::OPT_fastcp))
3950 CmdArgs.push_back("-O3");
Daniel Dunbarc21c4852009-04-08 23:54:23 +00003951
Daniel Dunbara3ec60e2009-03-29 18:40:18 +00003952 if (Arg *A = Args.getLastArg(options::OPT_pg))
3953 if (Args.hasArg(options::OPT_fomit_frame_pointer))
Chris Lattner5f9e2722011-07-23 10:55:15 +00003954 D.Diag(diag::err_drv_argument_not_allowed_with)
Daniel Dunbara3ec60e2009-03-29 18:40:18 +00003955 << A->getAsString(Args) << "-fomit-frame-pointer";
3956
3957 AddCC1Args(Args, CmdArgs);
3958
3959 if (!Args.hasArg(options::OPT_Q))
3960 CmdArgs.push_back("-quiet");
3961
3962 CmdArgs.push_back("-dumpbase");
Daniel Dunbara5a7bd02009-03-30 00:34:04 +00003963 CmdArgs.push_back(darwin::CC1::getBaseInputName(Args, Inputs));
Daniel Dunbara3ec60e2009-03-29 18:40:18 +00003964
3965 Args.AddAllArgs(CmdArgs, options::OPT_d_Group);
3966
3967 Args.AddAllArgs(CmdArgs, options::OPT_m_Group);
3968 Args.AddAllArgs(CmdArgs, options::OPT_a_Group);
3969
3970 // FIXME: The goal is to use the user provided -o if that is our
3971 // final output, otherwise to drive from the original input
3972 // name. Find a clean way to go about this.
3973 if ((Args.hasArg(options::OPT_c) || Args.hasArg(options::OPT_S)) &&
3974 Args.hasArg(options::OPT_o)) {
3975 Arg *OutputOpt = Args.getLastArg(options::OPT_o);
3976 CmdArgs.push_back("-auxbase-strip");
Richard Smith1d489cf2012-11-01 04:30:05 +00003977 CmdArgs.push_back(OutputOpt->getValue());
Daniel Dunbara3ec60e2009-03-29 18:40:18 +00003978 } else {
3979 CmdArgs.push_back("-auxbase");
Daniel Dunbara5a7bd02009-03-30 00:34:04 +00003980 CmdArgs.push_back(darwin::CC1::getBaseInputStem(Args, Inputs));
Daniel Dunbara3ec60e2009-03-29 18:40:18 +00003981 }
3982
3983 Args.AddAllArgs(CmdArgs, options::OPT_g_Group);
3984
3985 Args.AddAllArgs(CmdArgs, options::OPT_O);
3986 // FIXME: -Wall is getting some special treatment. Investigate.
3987 Args.AddAllArgs(CmdArgs, options::OPT_W_Group, options::OPT_pedantic_Group);
3988 Args.AddLastArg(CmdArgs, options::OPT_w);
Daniel Dunbarc21c4852009-04-08 23:54:23 +00003989 Args.AddAllArgs(CmdArgs, options::OPT_std_EQ, options::OPT_ansi,
Daniel Dunbara3ec60e2009-03-29 18:40:18 +00003990 options::OPT_trigraphs);
Daniel Dunbara3ff2022009-04-26 01:10:38 +00003991 if (!Args.getLastArg(options::OPT_std_EQ, options::OPT_ansi)) {
3992 // Honor -std-default.
3993 Args.AddAllArgsTranslated(CmdArgs, options::OPT_std_default_EQ,
3994 "-std=", /*Joined=*/true);
3995 }
3996
Daniel Dunbara3ec60e2009-03-29 18:40:18 +00003997 if (Args.hasArg(options::OPT_v))
3998 CmdArgs.push_back("-version");
Daniel Dunbarbbe8e3e2011-03-01 18:49:30 +00003999 if (Args.hasArg(options::OPT_pg) &&
4000 getToolChain().SupportsProfiling())
Daniel Dunbara3ec60e2009-03-29 18:40:18 +00004001 CmdArgs.push_back("-p");
4002 Args.AddLastArg(CmdArgs, options::OPT_p);
Daniel Dunbarc21c4852009-04-08 23:54:23 +00004003
Daniel Dunbara3ec60e2009-03-29 18:40:18 +00004004 // The driver treats -fsyntax-only specially.
Daniel Dunbar2ba91572009-09-10 03:37:02 +00004005 if (getToolChain().getTriple().getArch() == llvm::Triple::arm ||
4006 getToolChain().getTriple().getArch() == llvm::Triple::thumb) {
4007 // Removes -fbuiltin-str{cat,cpy}; these aren't recognized by cc1 but are
4008 // used to inhibit the default -fno-builtin-str{cat,cpy}.
4009 //
4010 // FIXME: Should we grow a better way to deal with "removing" args?
Daniel Dunbarcdd96862009-11-25 11:53:23 +00004011 for (arg_iterator it = Args.filtered_begin(options::OPT_f_Group,
4012 options::OPT_fsyntax_only),
4013 ie = Args.filtered_end(); it != ie; ++it) {
Daniel Dunbar7e4953e2010-06-11 22:00:13 +00004014 if (!(*it)->getOption().matches(options::OPT_fbuiltin_strcat) &&
4015 !(*it)->getOption().matches(options::OPT_fbuiltin_strcpy)) {
4016 (*it)->claim();
4017 (*it)->render(Args, CmdArgs);
Daniel Dunbar2ba91572009-09-10 03:37:02 +00004018 }
4019 }
4020 } else
4021 Args.AddAllArgs(CmdArgs, options::OPT_f_Group, options::OPT_fsyntax_only);
Daniel Dunbarc21c4852009-04-08 23:54:23 +00004022
Daniel Dunbar089f8722011-04-07 20:41:03 +00004023 // Claim Clang only -f options, they aren't worth warning about.
4024 Args.ClaimAllArgs(options::OPT_f_clang_Group);
4025
Daniel Dunbara3ec60e2009-03-29 18:40:18 +00004026 Args.AddAllArgs(CmdArgs, options::OPT_undef);
4027 if (Args.hasArg(options::OPT_Qn))
4028 CmdArgs.push_back("-fno-ident");
Daniel Dunbarc21c4852009-04-08 23:54:23 +00004029
Daniel Dunbara3ec60e2009-03-29 18:40:18 +00004030 // FIXME: This isn't correct.
4031 //Args.AddLastArg(CmdArgs, options::OPT__help)
4032 //Args.AddLastArg(CmdArgs, options::OPT__targetHelp)
4033
4034 CmdArgs.append(OutputArgs.begin(), OutputArgs.end());
4035
4036 // FIXME: Still don't get what is happening here. Investigate.
4037 Args.AddAllArgs(CmdArgs, options::OPT__param);
4038
4039 if (Args.hasArg(options::OPT_fmudflap) ||
4040 Args.hasArg(options::OPT_fmudflapth)) {
4041 CmdArgs.push_back("-fno-builtin");
4042 CmdArgs.push_back("-fno-merge-constants");
4043 }
Daniel Dunbarc21c4852009-04-08 23:54:23 +00004044
Daniel Dunbara3ec60e2009-03-29 18:40:18 +00004045 if (Args.hasArg(options::OPT_coverage)) {
4046 CmdArgs.push_back("-fprofile-arcs");
4047 CmdArgs.push_back("-ftest-coverage");
4048 }
4049
4050 if (types::isCXX(Inputs[0].getType()))
4051 CmdArgs.push_back("-D__private_extern__=extern");
4052}
4053
4054void darwin::CC1::AddCPPOptionsArgs(const ArgList &Args, ArgStringList &CmdArgs,
4055 const InputInfoList &Inputs,
4056 const ArgStringList &OutputArgs) const {
4057 // Derived from cpp_options
4058 AddCPPUniqueOptionsArgs(Args, CmdArgs, Inputs);
Daniel Dunbarc21c4852009-04-08 23:54:23 +00004059
Daniel Dunbara3ec60e2009-03-29 18:40:18 +00004060 CmdArgs.append(OutputArgs.begin(), OutputArgs.end());
4061
4062 AddCC1Args(Args, CmdArgs);
4063
4064 // NOTE: The code below has some commonality with cpp_options, but
4065 // in classic gcc style ends up sending things in different
4066 // orders. This may be a good merge candidate once we drop pedantic
4067 // compatibility.
4068
4069 Args.AddAllArgs(CmdArgs, options::OPT_m_Group);
Daniel Dunbarc21c4852009-04-08 23:54:23 +00004070 Args.AddAllArgs(CmdArgs, options::OPT_std_EQ, options::OPT_ansi,
Daniel Dunbara3ec60e2009-03-29 18:40:18 +00004071 options::OPT_trigraphs);
Daniel Dunbara3ff2022009-04-26 01:10:38 +00004072 if (!Args.getLastArg(options::OPT_std_EQ, options::OPT_ansi)) {
4073 // Honor -std-default.
4074 Args.AddAllArgsTranslated(CmdArgs, options::OPT_std_default_EQ,
4075 "-std=", /*Joined=*/true);
4076 }
Daniel Dunbara3ec60e2009-03-29 18:40:18 +00004077 Args.AddAllArgs(CmdArgs, options::OPT_W_Group, options::OPT_pedantic_Group);
4078 Args.AddLastArg(CmdArgs, options::OPT_w);
Daniel Dunbarc21c4852009-04-08 23:54:23 +00004079
Daniel Dunbara3ec60e2009-03-29 18:40:18 +00004080 // The driver treats -fsyntax-only specially.
4081 Args.AddAllArgs(CmdArgs, options::OPT_f_Group, options::OPT_fsyntax_only);
4082
Daniel Dunbar089f8722011-04-07 20:41:03 +00004083 // Claim Clang only -f options, they aren't worth warning about.
4084 Args.ClaimAllArgs(options::OPT_f_clang_Group);
4085
Daniel Dunbara3ec60e2009-03-29 18:40:18 +00004086 if (Args.hasArg(options::OPT_g_Group) && !Args.hasArg(options::OPT_g0) &&
4087 !Args.hasArg(options::OPT_fno_working_directory))
4088 CmdArgs.push_back("-fworking-directory");
4089
4090 Args.AddAllArgs(CmdArgs, options::OPT_O);
4091 Args.AddAllArgs(CmdArgs, options::OPT_undef);
4092 if (Args.hasArg(options::OPT_save_temps))
4093 CmdArgs.push_back("-fpch-preprocess");
4094}
4095
Daniel Dunbarc21c4852009-04-08 23:54:23 +00004096void darwin::CC1::AddCPPUniqueOptionsArgs(const ArgList &Args,
Daniel Dunbara3ec60e2009-03-29 18:40:18 +00004097 ArgStringList &CmdArgs,
Mike Stump1eb44332009-09-09 15:08:12 +00004098 const InputInfoList &Inputs) const {
Daniel Dunbaree788e72009-12-21 18:54:17 +00004099 const Driver &D = getToolChain().getDriver();
Daniel Dunbara3ec60e2009-03-29 18:40:18 +00004100
Daniel Dunbar88a3d6c2009-09-10 01:21:05 +00004101 CheckPreprocessingOptions(D, Args);
4102
Daniel Dunbara3ec60e2009-03-29 18:40:18 +00004103 // Derived from cpp_unique_options.
Daniel Dunbar88a3d6c2009-09-10 01:21:05 +00004104 // -{C,CC} only with -E is checked in CheckPreprocessingOptions().
4105 Args.AddLastArg(CmdArgs, options::OPT_C);
4106 Args.AddLastArg(CmdArgs, options::OPT_CC);
Daniel Dunbara3ec60e2009-03-29 18:40:18 +00004107 if (!Args.hasArg(options::OPT_Q))
4108 CmdArgs.push_back("-quiet");
4109 Args.AddAllArgs(CmdArgs, options::OPT_nostdinc);
Douglas Gregor4c2bcad2010-03-24 20:13:48 +00004110 Args.AddAllArgs(CmdArgs, options::OPT_nostdincxx);
Daniel Dunbara3ec60e2009-03-29 18:40:18 +00004111 Args.AddLastArg(CmdArgs, options::OPT_v);
4112 Args.AddAllArgs(CmdArgs, options::OPT_I_Group, options::OPT_F);
4113 Args.AddLastArg(CmdArgs, options::OPT_P);
4114
4115 // FIXME: Handle %I properly.
Rafael Espindola64f7ad92012-10-07 04:44:33 +00004116 if (getToolChain().getArch() == llvm::Triple::x86_64) {
Daniel Dunbara3ec60e2009-03-29 18:40:18 +00004117 CmdArgs.push_back("-imultilib");
4118 CmdArgs.push_back("x86_64");
4119 }
4120
4121 if (Args.hasArg(options::OPT_MD)) {
4122 CmdArgs.push_back("-MD");
Daniel Dunbara5a7bd02009-03-30 00:34:04 +00004123 CmdArgs.push_back(darwin::CC1::getDependencyFileName(Args, Inputs));
Daniel Dunbara3ec60e2009-03-29 18:40:18 +00004124 }
4125
4126 if (Args.hasArg(options::OPT_MMD)) {
4127 CmdArgs.push_back("-MMD");
Daniel Dunbara5a7bd02009-03-30 00:34:04 +00004128 CmdArgs.push_back(darwin::CC1::getDependencyFileName(Args, Inputs));
Daniel Dunbara3ec60e2009-03-29 18:40:18 +00004129 }
4130
4131 Args.AddLastArg(CmdArgs, options::OPT_M);
4132 Args.AddLastArg(CmdArgs, options::OPT_MM);
4133 Args.AddAllArgs(CmdArgs, options::OPT_MF);
4134 Args.AddLastArg(CmdArgs, options::OPT_MG);
4135 Args.AddLastArg(CmdArgs, options::OPT_MP);
4136 Args.AddAllArgs(CmdArgs, options::OPT_MQ);
4137 Args.AddAllArgs(CmdArgs, options::OPT_MT);
4138 if (!Args.hasArg(options::OPT_M) && !Args.hasArg(options::OPT_MM) &&
4139 (Args.hasArg(options::OPT_MD) || Args.hasArg(options::OPT_MMD))) {
4140 if (Arg *OutputOpt = Args.getLastArg(options::OPT_o)) {
4141 CmdArgs.push_back("-MQ");
Richard Smith1d489cf2012-11-01 04:30:05 +00004142 CmdArgs.push_back(OutputOpt->getValue());
Daniel Dunbara3ec60e2009-03-29 18:40:18 +00004143 }
4144 }
4145
4146 Args.AddLastArg(CmdArgs, options::OPT_remap);
4147 if (Args.hasArg(options::OPT_g3))
4148 CmdArgs.push_back("-dD");
4149 Args.AddLastArg(CmdArgs, options::OPT_H);
4150
4151 AddCPPArgs(Args, CmdArgs);
4152
4153 Args.AddAllArgs(CmdArgs, options::OPT_D, options::OPT_U, options::OPT_A);
4154 Args.AddAllArgs(CmdArgs, options::OPT_i_Group);
4155
4156 for (InputInfoList::const_iterator
4157 it = Inputs.begin(), ie = Inputs.end(); it != ie; ++it) {
4158 const InputInfo &II = *it;
Daniel Dunbarc21c4852009-04-08 23:54:23 +00004159
Daniel Dunbar7c1e4652010-08-02 02:38:21 +00004160 CmdArgs.push_back(II.getFilename());
Daniel Dunbara3ec60e2009-03-29 18:40:18 +00004161 }
4162
4163 Args.AddAllArgValues(CmdArgs, options::OPT_Wp_COMMA,
4164 options::OPT_Xpreprocessor);
4165
4166 if (Args.hasArg(options::OPT_fmudflap)) {
4167 CmdArgs.push_back("-D_MUDFLAP");
4168 CmdArgs.push_back("-include");
4169 CmdArgs.push_back("mf-runtime.h");
4170 }
4171
4172 if (Args.hasArg(options::OPT_fmudflapth)) {
4173 CmdArgs.push_back("-D_MUDFLAP");
4174 CmdArgs.push_back("-D_MUDFLAPTH");
4175 CmdArgs.push_back("-include");
4176 CmdArgs.push_back("mf-runtime.h");
4177 }
4178}
4179
Daniel Dunbarc21c4852009-04-08 23:54:23 +00004180void darwin::CC1::AddCPPArgs(const ArgList &Args,
Daniel Dunbara3ec60e2009-03-29 18:40:18 +00004181 ArgStringList &CmdArgs) const {
4182 // Derived from cpp spec.
4183
4184 if (Args.hasArg(options::OPT_static)) {
4185 // The gcc spec is broken here, it refers to dynamic but
4186 // that has been translated. Start by being bug compatible.
Daniel Dunbarc21c4852009-04-08 23:54:23 +00004187
Daniel Dunbara3ec60e2009-03-29 18:40:18 +00004188 // if (!Args.hasArg(arglist.parser.dynamicOption))
4189 CmdArgs.push_back("-D__STATIC__");
4190 } else
4191 CmdArgs.push_back("-D__DYNAMIC__");
4192
4193 if (Args.hasArg(options::OPT_pthread))
4194 CmdArgs.push_back("-D_REENTRANT");
4195}
4196
Daniel Dunbar40f12652009-03-29 17:08:39 +00004197void darwin::Preprocess::ConstructJob(Compilation &C, const JobAction &JA,
Daniel Dunbar2fe238e2010-08-02 02:38:28 +00004198 const InputInfo &Output,
Daniel Dunbarc21c4852009-04-08 23:54:23 +00004199 const InputInfoList &Inputs,
4200 const ArgList &Args,
Daniel Dunbar40f12652009-03-29 17:08:39 +00004201 const char *LinkingOutput) const {
4202 ArgStringList CmdArgs;
4203
4204 assert(Inputs.size() == 1 && "Unexpected number of inputs!");
4205
4206 CmdArgs.push_back("-E");
4207
4208 if (Args.hasArg(options::OPT_traditional) ||
Daniel Dunbar40f12652009-03-29 17:08:39 +00004209 Args.hasArg(options::OPT_traditional_cpp))
4210 CmdArgs.push_back("-traditional-cpp");
Daniel Dunbarc21c4852009-04-08 23:54:23 +00004211
Daniel Dunbar40f12652009-03-29 17:08:39 +00004212 ArgStringList OutputArgs;
Daniel Dunbar7c1e4652010-08-02 02:38:21 +00004213 assert(Output.isFilename() && "Unexpected CC1 output.");
4214 OutputArgs.push_back("-o");
4215 OutputArgs.push_back(Output.getFilename());
Daniel Dunbar40f12652009-03-29 17:08:39 +00004216
Joerg Sonnenberger9ade4ae2011-03-06 23:31:01 +00004217 if (Args.hasArg(options::OPT_E) || getToolChain().getDriver().CCCIsCPP) {
Daniel Dunbar9120f172009-03-29 22:27:40 +00004218 AddCPPOptionsArgs(Args, CmdArgs, Inputs, OutputArgs);
4219 } else {
4220 AddCPPOptionsArgs(Args, CmdArgs, Inputs, ArgStringList());
4221 CmdArgs.append(OutputArgs.begin(), OutputArgs.end());
4222 }
Daniel Dunbarc21c4852009-04-08 23:54:23 +00004223
Daniel Dunbar8a2073a2009-04-03 01:27:06 +00004224 Args.AddAllArgs(CmdArgs, options::OPT_d_Group);
4225
Chad Rosier4fe4d732011-09-08 00:38:00 +00004226 RemoveCC1UnsupportedArgs(CmdArgs);
4227
Daniel Dunbar40f12652009-03-29 17:08:39 +00004228 const char *CC1Name = getCC1Name(Inputs[0].getType());
Daniel Dunbarc21c4852009-04-08 23:54:23 +00004229 const char *Exec =
Daniel Dunbar4a7e8892010-07-14 18:46:23 +00004230 Args.MakeArgString(getToolChain().GetProgramPath(CC1Name));
Daniel Dunbar2fe238e2010-08-02 02:38:28 +00004231 C.addCommand(new Command(JA, *this, Exec, CmdArgs));
Daniel Dunbar40f12652009-03-29 17:08:39 +00004232}
4233
4234void darwin::Compile::ConstructJob(Compilation &C, const JobAction &JA,
Daniel Dunbar2fe238e2010-08-02 02:38:28 +00004235 const InputInfo &Output,
Daniel Dunbarc21c4852009-04-08 23:54:23 +00004236 const InputInfoList &Inputs,
4237 const ArgList &Args,
Daniel Dunbar40f12652009-03-29 17:08:39 +00004238 const char *LinkingOutput) const {
Daniel Dunbaree788e72009-12-21 18:54:17 +00004239 const Driver &D = getToolChain().getDriver();
Daniel Dunbar40f12652009-03-29 17:08:39 +00004240 ArgStringList CmdArgs;
4241
4242 assert(Inputs.size() == 1 && "Unexpected number of inputs!");
4243
Bob Wilson651f3d42012-04-01 23:03:29 +00004244 // Silence warning about unused --serialize-diagnostics
4245 Args.ClaimAllArgs(options::OPT__serialize_diags);
4246
Daniel Dunbar40f12652009-03-29 17:08:39 +00004247 types::ID InputType = Inputs[0].getType();
David Blaikied624a5b2012-04-04 20:43:14 +00004248 if (const Arg *A = Args.getLastArg(options::OPT_traditional))
Chris Lattner5f9e2722011-07-23 10:55:15 +00004249 D.Diag(diag::err_drv_argument_only_allowed_with)
Daniel Dunbar40f12652009-03-29 17:08:39 +00004250 << A->getAsString(Args) << "-E";
4251
Daniel Dunbar6c6424b2010-06-07 23:28:45 +00004252 if (JA.getType() == types::TY_LLVM_IR ||
4253 JA.getType() == types::TY_LTO_IR)
Daniel Dunbar40f12652009-03-29 17:08:39 +00004254 CmdArgs.push_back("-emit-llvm");
Daniel Dunbar6c6424b2010-06-07 23:28:45 +00004255 else if (JA.getType() == types::TY_LLVM_BC ||
4256 JA.getType() == types::TY_LTO_BC)
Daniel Dunbar40f12652009-03-29 17:08:39 +00004257 CmdArgs.push_back("-emit-llvm-bc");
Daniel Dunbar5915fbf2009-09-01 16:57:46 +00004258 else if (Output.getType() == types::TY_AST)
Chris Lattner5f9e2722011-07-23 10:55:15 +00004259 D.Diag(diag::err_drv_no_ast_support)
Daniel Dunbar88137642009-09-09 22:32:48 +00004260 << getToolChain().getTripleString();
Daniel Dunbarae24a882010-02-11 17:33:45 +00004261 else if (JA.getType() != types::TY_PP_Asm &&
4262 JA.getType() != types::TY_PCH)
Chris Lattner5f9e2722011-07-23 10:55:15 +00004263 D.Diag(diag::err_drv_invalid_gcc_output_type)
Daniel Dunbar64952502010-02-11 03:16:21 +00004264 << getTypeName(JA.getType());
Daniel Dunbar40f12652009-03-29 17:08:39 +00004265
4266 ArgStringList OutputArgs;
4267 if (Output.getType() != types::TY_PCH) {
4268 OutputArgs.push_back("-o");
Daniel Dunbar7c1e4652010-08-02 02:38:21 +00004269 if (Output.isNothing())
Daniel Dunbar40f12652009-03-29 17:08:39 +00004270 OutputArgs.push_back("/dev/null");
4271 else
4272 OutputArgs.push_back(Output.getFilename());
4273 }
4274
4275 // There is no need for this level of compatibility, but it makes
4276 // diffing easier.
4277 bool OutputArgsEarly = (Args.hasArg(options::OPT_fsyntax_only) ||
4278 Args.hasArg(options::OPT_S));
4279
4280 if (types::getPreprocessedType(InputType) != types::TY_INVALID) {
Daniel Dunbara3ec60e2009-03-29 18:40:18 +00004281 AddCPPUniqueOptionsArgs(Args, CmdArgs, Inputs);
Daniel Dunbar40f12652009-03-29 17:08:39 +00004282 if (OutputArgsEarly) {
4283 AddCC1OptionsArgs(Args, CmdArgs, Inputs, OutputArgs);
4284 } else {
4285 AddCC1OptionsArgs(Args, CmdArgs, Inputs, ArgStringList());
4286 CmdArgs.append(OutputArgs.begin(), OutputArgs.end());
4287 }
4288 } else {
4289 CmdArgs.push_back("-fpreprocessed");
Daniel Dunbarc21c4852009-04-08 23:54:23 +00004290
Daniel Dunbar40f12652009-03-29 17:08:39 +00004291 for (InputInfoList::const_iterator
4292 it = Inputs.begin(), ie = Inputs.end(); it != ie; ++it) {
4293 const InputInfo &II = *it;
4294
Daniel Dunbar5915fbf2009-09-01 16:57:46 +00004295 // Reject AST inputs.
4296 if (II.getType() == types::TY_AST) {
Chris Lattner5f9e2722011-07-23 10:55:15 +00004297 D.Diag(diag::err_drv_no_ast_support)
Daniel Dunbar88137642009-09-09 22:32:48 +00004298 << getToolChain().getTripleString();
Daniel Dunbar5915fbf2009-09-01 16:57:46 +00004299 return;
4300 }
4301
Daniel Dunbar7c1e4652010-08-02 02:38:21 +00004302 CmdArgs.push_back(II.getFilename());
Daniel Dunbar40f12652009-03-29 17:08:39 +00004303 }
4304
4305 if (OutputArgsEarly) {
4306 AddCC1OptionsArgs(Args, CmdArgs, Inputs, OutputArgs);
4307 } else {
4308 AddCC1OptionsArgs(Args, CmdArgs, Inputs, ArgStringList());
4309 CmdArgs.append(OutputArgs.begin(), OutputArgs.end());
4310 }
4311 }
Daniel Dunbarc21c4852009-04-08 23:54:23 +00004312
Daniel Dunbar40f12652009-03-29 17:08:39 +00004313 if (Output.getType() == types::TY_PCH) {
4314 assert(Output.isFilename() && "Invalid PCH output.");
4315
4316 CmdArgs.push_back("-o");
4317 // NOTE: gcc uses a temp .s file for this, but there doesn't seem
4318 // to be a good reason.
Chad Rosier8c221b82011-08-01 19:58:48 +00004319 const char *TmpPath = C.getArgs().MakeArgString(
Chad Rosierfe87fc72011-08-26 21:28:44 +00004320 D.GetTemporaryPath("cc", "s"));
Chad Rosier8c221b82011-08-01 19:58:48 +00004321 C.addTempFile(TmpPath);
4322 CmdArgs.push_back(TmpPath);
Daniel Dunbarc21c4852009-04-08 23:54:23 +00004323
Eric Christopher88b7cf02011-08-19 00:30:14 +00004324 // If we're emitting a pch file with the last 4 characters of ".pth"
4325 // and falling back to llvm-gcc we want to use ".gch" instead.
4326 std::string OutputFile(Output.getFilename());
4327 size_t loc = OutputFile.rfind(".pth");
4328 if (loc != std::string::npos)
4329 OutputFile.replace(loc, 4, ".gch");
4330 const char *Tmp = C.getArgs().MakeArgString("--output-pch="+OutputFile);
4331 CmdArgs.push_back(Tmp);
Daniel Dunbarc21c4852009-04-08 23:54:23 +00004332 }
Daniel Dunbar40f12652009-03-29 17:08:39 +00004333
Chad Rosier285f9a22011-08-17 18:24:55 +00004334 RemoveCC1UnsupportedArgs(CmdArgs);
4335
Daniel Dunbar40f12652009-03-29 17:08:39 +00004336 const char *CC1Name = getCC1Name(Inputs[0].getType());
Daniel Dunbarc21c4852009-04-08 23:54:23 +00004337 const char *Exec =
Daniel Dunbar4a7e8892010-07-14 18:46:23 +00004338 Args.MakeArgString(getToolChain().GetProgramPath(CC1Name));
Daniel Dunbar2fe238e2010-08-02 02:38:28 +00004339 C.addCommand(new Command(JA, *this, Exec, CmdArgs));
Daniel Dunbar40f12652009-03-29 17:08:39 +00004340}
4341
Daniel Dunbar8cac5f72009-03-20 16:06:39 +00004342void darwin::Assemble::ConstructJob(Compilation &C, const JobAction &JA,
Daniel Dunbar2fe238e2010-08-02 02:38:28 +00004343 const InputInfo &Output,
Daniel Dunbarc21c4852009-04-08 23:54:23 +00004344 const InputInfoList &Inputs,
4345 const ArgList &Args,
Daniel Dunbar8cac5f72009-03-20 16:06:39 +00004346 const char *LinkingOutput) const {
4347 ArgStringList CmdArgs;
4348
4349 assert(Inputs.size() == 1 && "Unexpected number of inputs.");
4350 const InputInfo &Input = Inputs[0];
4351
Daniel Dunbar34bac1f2011-04-12 23:59:20 +00004352 // Determine the original source input.
4353 const Action *SourceAction = &JA;
4354 while (SourceAction->getKind() != Action::InputClass) {
4355 assert(!SourceAction->getInputs().empty() && "unexpected root action!");
4356 SourceAction = SourceAction->getInputs()[0];
4357 }
4358
4359 // Forward -g, assuming we are dealing with an actual assembly file.
Eric Christopher88b7cf02011-08-19 00:30:14 +00004360 if (SourceAction->getType() == types::TY_Asm ||
Daniel Dunbar34bac1f2011-04-12 23:59:20 +00004361 SourceAction->getType() == types::TY_PP_Asm) {
Daniel Dunbar8e4fea62009-04-01 00:27:44 +00004362 if (Args.hasArg(options::OPT_gstabs))
4363 CmdArgs.push_back("--gstabs");
4364 else if (Args.hasArg(options::OPT_g_Group))
Bob Wilson591ff152011-11-02 05:10:45 +00004365 CmdArgs.push_back("-g");
Daniel Dunbar8e4fea62009-04-01 00:27:44 +00004366 }
Daniel Dunbarc21c4852009-04-08 23:54:23 +00004367
Daniel Dunbar8cac5f72009-03-20 16:06:39 +00004368 // Derived from asm spec.
Daniel Dunbarcc6f8032009-09-09 18:36:27 +00004369 AddDarwinArch(Args, CmdArgs);
Daniel Dunbar8cac5f72009-03-20 16:06:39 +00004370
Daniel Dunbarf5438e32010-07-22 01:47:22 +00004371 // Use -force_cpusubtype_ALL on x86 by default.
4372 if (getToolChain().getTriple().getArch() == llvm::Triple::x86 ||
4373 getToolChain().getTriple().getArch() == llvm::Triple::x86_64 ||
Daniel Dunbarcc6f8032009-09-09 18:36:27 +00004374 Args.hasArg(options::OPT_force__cpusubtype__ALL))
4375 CmdArgs.push_back("-force_cpusubtype_ALL");
4376
Daniel Dunbar0e2679d2009-08-24 22:26:16 +00004377 if (getToolChain().getTriple().getArch() != llvm::Triple::x86_64 &&
Daniel Dunbar7a0c0642012-10-15 22:23:53 +00004378 (((Args.hasArg(options::OPT_mkernel) ||
4379 Args.hasArg(options::OPT_fapple_kext)) &&
4380 (!getDarwinToolChain().isTargetIPhoneOS() ||
4381 getDarwinToolChain().isIPhoneOSVersionLT(6, 0))) ||
4382 Args.hasArg(options::OPT_static)))
Daniel Dunbarc21c4852009-04-08 23:54:23 +00004383 CmdArgs.push_back("-static");
4384
Daniel Dunbar8cac5f72009-03-20 16:06:39 +00004385 Args.AddAllArgValues(CmdArgs, options::OPT_Wa_COMMA,
4386 options::OPT_Xassembler);
4387
4388 assert(Output.isFilename() && "Unexpected lipo output.");
4389 CmdArgs.push_back("-o");
4390 CmdArgs.push_back(Output.getFilename());
4391
Daniel Dunbar7c1e4652010-08-02 02:38:21 +00004392 assert(Input.isFilename() && "Invalid input.");
4393 CmdArgs.push_back(Input.getFilename());
Daniel Dunbar8cac5f72009-03-20 16:06:39 +00004394
4395 // asm_final spec is empty.
4396
Daniel Dunbarc21c4852009-04-08 23:54:23 +00004397 const char *Exec =
Daniel Dunbar4a7e8892010-07-14 18:46:23 +00004398 Args.MakeArgString(getToolChain().GetProgramPath("as"));
Daniel Dunbar2fe238e2010-08-02 02:38:28 +00004399 C.addCommand(new Command(JA, *this, Exec, CmdArgs));
Daniel Dunbar8cac5f72009-03-20 16:06:39 +00004400}
Daniel Dunbarff7488d2009-03-20 00:52:38 +00004401
David Blaikie99ba9e32011-12-20 02:48:34 +00004402void darwin::DarwinTool::anchor() {}
4403
Daniel Dunbarfbefe6b2009-09-09 18:36:20 +00004404void darwin::DarwinTool::AddDarwinArch(const ArgList &Args,
4405 ArgStringList &CmdArgs) const {
Chris Lattner5f9e2722011-07-23 10:55:15 +00004406 StringRef ArchName = getDarwinToolChain().getDarwinArchName(Args);
Daniel Dunbareeff4062010-01-22 02:04:58 +00004407
Daniel Dunbar02633b52009-03-26 16:23:12 +00004408 // Derived from darwin_arch spec.
4409 CmdArgs.push_back("-arch");
Daniel Dunbareeff4062010-01-22 02:04:58 +00004410 CmdArgs.push_back(Args.MakeArgString(ArchName));
Daniel Dunbar78dbd582009-09-04 18:35:31 +00004411
Daniel Dunbareeff4062010-01-22 02:04:58 +00004412 // FIXME: Is this needed anymore?
4413 if (ArchName == "arm")
Daniel Dunbar78dbd582009-09-04 18:35:31 +00004414 CmdArgs.push_back("-force_cpusubtype_ALL");
Daniel Dunbar02633b52009-03-26 16:23:12 +00004415}
4416
Bill Wendling6acf8b42012-10-02 18:02:50 +00004417bool darwin::Link::NeedsTempPath(const InputInfoList &Inputs) const {
4418 // We only need to generate a temp path for LTO if we aren't compiling object
4419 // files. When compiling source files, we run 'dsymutil' after linking. We
4420 // don't run 'dsymutil' when compiling object files.
4421 for (InputInfoList::const_iterator
4422 it = Inputs.begin(), ie = Inputs.end(); it != ie; ++it)
4423 if (it->getType() != types::TY_Object)
4424 return true;
4425
4426 return false;
4427}
4428
Daniel Dunbar748de8e2010-09-09 21:51:05 +00004429void darwin::Link::AddLinkArgs(Compilation &C,
4430 const ArgList &Args,
Bill Wendling6acf8b42012-10-02 18:02:50 +00004431 ArgStringList &CmdArgs,
4432 const InputInfoList &Inputs) const {
Daniel Dunbaree788e72009-12-21 18:54:17 +00004433 const Driver &D = getToolChain().getDriver();
Daniel Dunbarce911f52011-04-28 21:23:41 +00004434 const toolchains::Darwin &DarwinTC = getDarwinToolChain();
Daniel Dunbar02633b52009-03-26 16:23:12 +00004435
Daniel Dunbarb18dc5b2010-08-11 23:07:50 +00004436 unsigned Version[3] = { 0, 0, 0 };
4437 if (Arg *A = Args.getLastArg(options::OPT_mlinker_version_EQ)) {
4438 bool HadExtra;
Richard Smith1d489cf2012-11-01 04:30:05 +00004439 if (!Driver::GetReleaseVersion(A->getValue(), Version[0],
Daniel Dunbarb18dc5b2010-08-11 23:07:50 +00004440 Version[1], Version[2], HadExtra) ||
4441 HadExtra)
Chris Lattner5f9e2722011-07-23 10:55:15 +00004442 D.Diag(diag::err_drv_invalid_version_number)
Daniel Dunbarb18dc5b2010-08-11 23:07:50 +00004443 << A->getAsString(Args);
4444 }
4445
4446 // Newer linkers support -demangle, pass it if supported and not disabled by
4447 // the user.
Daniel Dunbard2d20882012-01-04 21:45:27 +00004448 if (Version[0] >= 100 && !Args.hasArg(options::OPT_Z_Xlinker__no_demangle)) {
Daniel Dunbarbcf1da82010-09-07 17:07:49 +00004449 // Don't pass -demangle to ld_classic.
4450 //
4451 // FIXME: This is a temporary workaround, ld should be handling this.
4452 bool UsesLdClassic = (getToolChain().getArch() == llvm::Triple::x86 &&
4453 Args.hasArg(options::OPT_static));
Daniel Dunbar9ced7042010-09-07 17:50:41 +00004454 if (getToolChain().getArch() == llvm::Triple::x86) {
4455 for (arg_iterator it = Args.filtered_begin(options::OPT_Xlinker,
4456 options::OPT_Wl_COMMA),
4457 ie = Args.filtered_end(); it != ie; ++it) {
4458 const Arg *A = *it;
4459 for (unsigned i = 0, e = A->getNumValues(); i != e; ++i)
Richard Smith1d489cf2012-11-01 04:30:05 +00004460 if (StringRef(A->getValue(i)) == "-kext")
Daniel Dunbar9ced7042010-09-07 17:50:41 +00004461 UsesLdClassic = true;
4462 }
4463 }
Daniel Dunbarbcf1da82010-09-07 17:07:49 +00004464 if (!UsesLdClassic)
4465 CmdArgs.push_back("-demangle");
Daniel Dunbarb18dc5b2010-08-11 23:07:50 +00004466 }
4467
Daniel Dunbar5bfa6562011-06-21 20:55:11 +00004468 // If we are using LTO, then automatically create a temporary file path for
4469 // the linker to use, so that it's lifetime will extend past a possible
4470 // dsymutil step.
Bill Wendling6acf8b42012-10-02 18:02:50 +00004471 if (Version[0] >= 116 && D.IsUsingLTO(Args) && NeedsTempPath(Inputs)) {
Daniel Dunbar5bfa6562011-06-21 20:55:11 +00004472 const char *TmpPath = C.getArgs().MakeArgString(
Chad Rosierfe87fc72011-08-26 21:28:44 +00004473 D.GetTemporaryPath("cc", types::getTypeTempSuffix(types::TY_Object)));
Daniel Dunbar5bfa6562011-06-21 20:55:11 +00004474 C.addTempFile(TmpPath);
4475 CmdArgs.push_back("-object_path_lto");
4476 CmdArgs.push_back(TmpPath);
4477 }
4478
Daniel Dunbar02633b52009-03-26 16:23:12 +00004479 // Derived from the "link" spec.
4480 Args.AddAllArgs(CmdArgs, options::OPT_static);
4481 if (!Args.hasArg(options::OPT_static))
4482 CmdArgs.push_back("-dynamic");
4483 if (Args.hasArg(options::OPT_fgnu_runtime)) {
4484 // FIXME: gcc replaces -lobjc in forward args with -lobjc-gnu
4485 // here. How do we wish to handle such things?
4486 }
Daniel Dunbarc21c4852009-04-08 23:54:23 +00004487
Daniel Dunbar02633b52009-03-26 16:23:12 +00004488 if (!Args.hasArg(options::OPT_dynamiclib)) {
Daniel Dunbara6d38492010-01-22 02:04:52 +00004489 AddDarwinArch(Args, CmdArgs);
Daniel Dunbara6d38492010-01-22 02:04:52 +00004490 // FIXME: Why do this only on this path?
Daniel Dunbar8917dd42010-01-22 03:37:33 +00004491 Args.AddLastArg(CmdArgs, options::OPT_force__cpusubtype__ALL);
Daniel Dunbar02633b52009-03-26 16:23:12 +00004492
4493 Args.AddLastArg(CmdArgs, options::OPT_bundle);
4494 Args.AddAllArgs(CmdArgs, options::OPT_bundle__loader);
4495 Args.AddAllArgs(CmdArgs, options::OPT_client__name);
4496
4497 Arg *A;
4498 if ((A = Args.getLastArg(options::OPT_compatibility__version)) ||
4499 (A = Args.getLastArg(options::OPT_current__version)) ||
4500 (A = Args.getLastArg(options::OPT_install__name)))
Chris Lattner5f9e2722011-07-23 10:55:15 +00004501 D.Diag(diag::err_drv_argument_only_allowed_with)
Daniel Dunbar02633b52009-03-26 16:23:12 +00004502 << A->getAsString(Args) << "-dynamiclib";
4503
4504 Args.AddLastArg(CmdArgs, options::OPT_force__flat__namespace);
4505 Args.AddLastArg(CmdArgs, options::OPT_keep__private__externs);
4506 Args.AddLastArg(CmdArgs, options::OPT_private__bundle);
4507 } else {
4508 CmdArgs.push_back("-dylib");
4509
4510 Arg *A;
4511 if ((A = Args.getLastArg(options::OPT_bundle)) ||
4512 (A = Args.getLastArg(options::OPT_bundle__loader)) ||
4513 (A = Args.getLastArg(options::OPT_client__name)) ||
4514 (A = Args.getLastArg(options::OPT_force__flat__namespace)) ||
4515 (A = Args.getLastArg(options::OPT_keep__private__externs)) ||
4516 (A = Args.getLastArg(options::OPT_private__bundle)))
Chris Lattner5f9e2722011-07-23 10:55:15 +00004517 D.Diag(diag::err_drv_argument_not_allowed_with)
Daniel Dunbar02633b52009-03-26 16:23:12 +00004518 << A->getAsString(Args) << "-dynamiclib";
Daniel Dunbarc21c4852009-04-08 23:54:23 +00004519
Daniel Dunbar02633b52009-03-26 16:23:12 +00004520 Args.AddAllArgsTranslated(CmdArgs, options::OPT_compatibility__version,
4521 "-dylib_compatibility_version");
4522 Args.AddAllArgsTranslated(CmdArgs, options::OPT_current__version,
4523 "-dylib_current_version");
4524
Daniel Dunbara6d38492010-01-22 02:04:52 +00004525 AddDarwinArch(Args, CmdArgs);
Daniel Dunbar02633b52009-03-26 16:23:12 +00004526
4527 Args.AddAllArgsTranslated(CmdArgs, options::OPT_install__name,
4528 "-dylib_install_name");
4529 }
4530
4531 Args.AddLastArg(CmdArgs, options::OPT_all__load);
4532 Args.AddAllArgs(CmdArgs, options::OPT_allowable__client);
4533 Args.AddLastArg(CmdArgs, options::OPT_bind__at__load);
Daniel Dunbarce911f52011-04-28 21:23:41 +00004534 if (DarwinTC.isTargetIPhoneOS())
Daniel Dunbard82f8fa2009-09-04 18:35:41 +00004535 Args.AddLastArg(CmdArgs, options::OPT_arch__errors__fatal);
Daniel Dunbar02633b52009-03-26 16:23:12 +00004536 Args.AddLastArg(CmdArgs, options::OPT_dead__strip);
4537 Args.AddLastArg(CmdArgs, options::OPT_no__dead__strip__inits__and__terms);
4538 Args.AddAllArgs(CmdArgs, options::OPT_dylib__file);
4539 Args.AddLastArg(CmdArgs, options::OPT_dynamic);
4540 Args.AddAllArgs(CmdArgs, options::OPT_exported__symbols__list);
4541 Args.AddLastArg(CmdArgs, options::OPT_flat__namespace);
Daniel Dunbar99ca47b2011-06-28 20:16:02 +00004542 Args.AddAllArgs(CmdArgs, options::OPT_force__load);
Daniel Dunbar02633b52009-03-26 16:23:12 +00004543 Args.AddAllArgs(CmdArgs, options::OPT_headerpad__max__install__names);
4544 Args.AddAllArgs(CmdArgs, options::OPT_image__base);
4545 Args.AddAllArgs(CmdArgs, options::OPT_init);
4546
Daniel Dunbarce911f52011-04-28 21:23:41 +00004547 // Add the deployment target.
Benjamin Kramer09c9a562012-03-10 20:55:36 +00004548 VersionTuple TargetVersion = DarwinTC.getTargetVersion();
Daniel Dunbarb7f5ef72011-04-30 04:22:58 +00004549
4550 // If we had an explicit -mios-simulator-version-min argument, honor that,
4551 // otherwise use the traditional deployment targets. We can't just check the
4552 // is-sim attribute because existing code follows this path, and the linker
4553 // may not handle the argument.
4554 //
4555 // FIXME: We may be able to remove this, once we can verify no one depends on
4556 // it.
4557 if (Args.hasArg(options::OPT_mios_simulator_version_min_EQ))
4558 CmdArgs.push_back("-ios_simulator_version_min");
4559 else if (DarwinTC.isTargetIPhoneOS())
4560 CmdArgs.push_back("-iphoneos_version_min");
4561 else
4562 CmdArgs.push_back("-macosx_version_min");
Benjamin Kramer09c9a562012-03-10 20:55:36 +00004563 CmdArgs.push_back(Args.MakeArgString(TargetVersion.getAsString()));
Daniel Dunbarce911f52011-04-28 21:23:41 +00004564
Daniel Dunbar02633b52009-03-26 16:23:12 +00004565 Args.AddLastArg(CmdArgs, options::OPT_nomultidefs);
4566 Args.AddLastArg(CmdArgs, options::OPT_multi__module);
4567 Args.AddLastArg(CmdArgs, options::OPT_single__module);
4568 Args.AddAllArgs(CmdArgs, options::OPT_multiply__defined);
4569 Args.AddAllArgs(CmdArgs, options::OPT_multiply__defined__unused);
Daniel Dunbarc21c4852009-04-08 23:54:23 +00004570
Daniel Dunbar47e879d2010-07-13 23:31:40 +00004571 if (const Arg *A = Args.getLastArg(options::OPT_fpie, options::OPT_fPIE,
4572 options::OPT_fno_pie,
4573 options::OPT_fno_PIE)) {
4574 if (A->getOption().matches(options::OPT_fpie) ||
4575 A->getOption().matches(options::OPT_fPIE))
4576 CmdArgs.push_back("-pie");
4577 else
4578 CmdArgs.push_back("-no_pie");
4579 }
Daniel Dunbar02633b52009-03-26 16:23:12 +00004580
4581 Args.AddLastArg(CmdArgs, options::OPT_prebind);
4582 Args.AddLastArg(CmdArgs, options::OPT_noprebind);
4583 Args.AddLastArg(CmdArgs, options::OPT_nofixprebinding);
4584 Args.AddLastArg(CmdArgs, options::OPT_prebind__all__twolevel__modules);
4585 Args.AddLastArg(CmdArgs, options::OPT_read__only__relocs);
4586 Args.AddAllArgs(CmdArgs, options::OPT_sectcreate);
4587 Args.AddAllArgs(CmdArgs, options::OPT_sectorder);
4588 Args.AddAllArgs(CmdArgs, options::OPT_seg1addr);
4589 Args.AddAllArgs(CmdArgs, options::OPT_segprot);
4590 Args.AddAllArgs(CmdArgs, options::OPT_segaddr);
4591 Args.AddAllArgs(CmdArgs, options::OPT_segs__read__only__addr);
4592 Args.AddAllArgs(CmdArgs, options::OPT_segs__read__write__addr);
4593 Args.AddAllArgs(CmdArgs, options::OPT_seg__addr__table);
4594 Args.AddAllArgs(CmdArgs, options::OPT_seg__addr__table__filename);
4595 Args.AddAllArgs(CmdArgs, options::OPT_sub__library);
4596 Args.AddAllArgs(CmdArgs, options::OPT_sub__umbrella);
Daniel Dunbard82f8fa2009-09-04 18:35:41 +00004597
Daniel Dunbarcc957192011-05-02 21:03:47 +00004598 // Give --sysroot= preference, over the Apple specific behavior to also use
4599 // --isysroot as the syslibroot.
Sebastian Pop4762a2d2012-04-16 04:16:43 +00004600 StringRef sysroot = C.getSysRoot();
4601 if (sysroot != "") {
Daniel Dunbarcc957192011-05-02 21:03:47 +00004602 CmdArgs.push_back("-syslibroot");
Sebastian Pop4762a2d2012-04-16 04:16:43 +00004603 CmdArgs.push_back(C.getArgs().MakeArgString(sysroot));
Daniel Dunbarcc957192011-05-02 21:03:47 +00004604 } else if (const Arg *A = Args.getLastArg(options::OPT_isysroot)) {
4605 CmdArgs.push_back("-syslibroot");
Richard Smith1d489cf2012-11-01 04:30:05 +00004606 CmdArgs.push_back(A->getValue());
Daniel Dunbard82f8fa2009-09-04 18:35:41 +00004607 }
4608
Daniel Dunbar02633b52009-03-26 16:23:12 +00004609 Args.AddLastArg(CmdArgs, options::OPT_twolevel__namespace);
4610 Args.AddLastArg(CmdArgs, options::OPT_twolevel__namespace__hints);
4611 Args.AddAllArgs(CmdArgs, options::OPT_umbrella);
4612 Args.AddAllArgs(CmdArgs, options::OPT_undefined);
4613 Args.AddAllArgs(CmdArgs, options::OPT_unexported__symbols__list);
Daniel Dunbard82f8fa2009-09-04 18:35:41 +00004614 Args.AddAllArgs(CmdArgs, options::OPT_weak__reference__mismatches);
Daniel Dunbar02633b52009-03-26 16:23:12 +00004615 Args.AddLastArg(CmdArgs, options::OPT_X_Flag);
4616 Args.AddAllArgs(CmdArgs, options::OPT_y);
4617 Args.AddLastArg(CmdArgs, options::OPT_w);
4618 Args.AddAllArgs(CmdArgs, options::OPT_pagezero__size);
4619 Args.AddAllArgs(CmdArgs, options::OPT_segs__read__);
4620 Args.AddLastArg(CmdArgs, options::OPT_seglinkedit);
4621 Args.AddLastArg(CmdArgs, options::OPT_noseglinkedit);
4622 Args.AddAllArgs(CmdArgs, options::OPT_sectalign);
4623 Args.AddAllArgs(CmdArgs, options::OPT_sectobjectsymbols);
4624 Args.AddAllArgs(CmdArgs, options::OPT_segcreate);
4625 Args.AddLastArg(CmdArgs, options::OPT_whyload);
4626 Args.AddLastArg(CmdArgs, options::OPT_whatsloaded);
4627 Args.AddAllArgs(CmdArgs, options::OPT_dylinker__install__name);
4628 Args.AddLastArg(CmdArgs, options::OPT_dylinker);
4629 Args.AddLastArg(CmdArgs, options::OPT_Mach);
4630}
4631
4632void darwin::Link::ConstructJob(Compilation &C, const JobAction &JA,
Daniel Dunbar2fe238e2010-08-02 02:38:28 +00004633 const InputInfo &Output,
Daniel Dunbarc21c4852009-04-08 23:54:23 +00004634 const InputInfoList &Inputs,
4635 const ArgList &Args,
Daniel Dunbar02633b52009-03-26 16:23:12 +00004636 const char *LinkingOutput) const {
4637 assert(Output.getType() == types::TY_Image && "Invalid linker output type.");
Daniel Dunbare0be8b12009-09-08 16:39:16 +00004638
Daniel Dunbar02633b52009-03-26 16:23:12 +00004639 // The logic here is derived from gcc's behavior; most of which
4640 // comes from specs (starting with link_command). Consult gcc for
4641 // more information.
Daniel Dunbar02633b52009-03-26 16:23:12 +00004642 ArgStringList CmdArgs;
4643
Argyrios Kyrtzidis22897172011-10-07 22:58:08 +00004644 /// Hack(tm) to ignore linking errors when we are doing ARC migration.
4645 if (Args.hasArg(options::OPT_ccc_arcmt_check,
4646 options::OPT_ccc_arcmt_migrate)) {
4647 for (ArgList::const_iterator I = Args.begin(), E = Args.end(); I != E; ++I)
4648 (*I)->claim();
4649 const char *Exec =
4650 Args.MakeArgString(getToolChain().GetProgramPath("touch"));
4651 CmdArgs.push_back(Output.getFilename());
4652 C.addCommand(new Command(JA, *this, Exec, CmdArgs));
4653 return;
4654 }
4655
Daniel Dunbar02633b52009-03-26 16:23:12 +00004656 // I'm not sure why this particular decomposition exists in gcc, but
4657 // we follow suite for ease of comparison.
Bill Wendling6acf8b42012-10-02 18:02:50 +00004658 AddLinkArgs(C, Args, CmdArgs, Inputs);
Daniel Dunbar02633b52009-03-26 16:23:12 +00004659
Daniel Dunbar02633b52009-03-26 16:23:12 +00004660 Args.AddAllArgs(CmdArgs, options::OPT_d_Flag);
4661 Args.AddAllArgs(CmdArgs, options::OPT_s);
4662 Args.AddAllArgs(CmdArgs, options::OPT_t);
4663 Args.AddAllArgs(CmdArgs, options::OPT_Z_Flag);
4664 Args.AddAllArgs(CmdArgs, options::OPT_u_Group);
Daniel Dunbar02633b52009-03-26 16:23:12 +00004665 Args.AddLastArg(CmdArgs, options::OPT_e);
4666 Args.AddAllArgs(CmdArgs, options::OPT_m_Separate);
4667 Args.AddAllArgs(CmdArgs, options::OPT_r);
4668
Daniel Dunbar270073c2010-10-18 22:08:36 +00004669 // Forward -ObjC when either -ObjC or -ObjC++ is used, to force loading
4670 // members of static archive libraries which implement Objective-C classes or
4671 // categories.
4672 if (Args.hasArg(options::OPT_ObjC) || Args.hasArg(options::OPT_ObjCXX))
4673 CmdArgs.push_back("-ObjC");
Michael J. Spencer20249a12010-10-21 03:16:25 +00004674
Daniel Dunbar02633b52009-03-26 16:23:12 +00004675 CmdArgs.push_back("-o");
4676 CmdArgs.push_back(Output.getFilename());
4677
Chad Rosier18937312012-05-16 23:45:12 +00004678 if (!Args.hasArg(options::OPT_nostdlib) &&
Daniel Dunbar02633b52009-03-26 16:23:12 +00004679 !Args.hasArg(options::OPT_nostartfiles)) {
4680 // Derived from startfile spec.
4681 if (Args.hasArg(options::OPT_dynamiclib)) {
Daniel Dunbarc21c4852009-04-08 23:54:23 +00004682 // Derived from darwin_dylib1 spec.
Daniel Dunbar1051fc02011-04-01 21:02:42 +00004683 if (getDarwinToolChain().isTargetIOSSimulator()) {
4684 // The simulator doesn't have a versioned crt1 file.
4685 CmdArgs.push_back("-ldylib1.o");
4686 } else if (getDarwinToolChain().isTargetIPhoneOS()) {
Daniel Dunbarcacb0f02010-01-27 00:56:56 +00004687 if (getDarwinToolChain().isIPhoneOSVersionLT(3, 1))
4688 CmdArgs.push_back("-ldylib1.o");
4689 } else {
Daniel Dunbarce3fdf22010-01-27 00:57:03 +00004690 if (getDarwinToolChain().isMacosxVersionLT(10, 5))
Daniel Dunbarcacb0f02010-01-27 00:56:56 +00004691 CmdArgs.push_back("-ldylib1.o");
Daniel Dunbarce3fdf22010-01-27 00:57:03 +00004692 else if (getDarwinToolChain().isMacosxVersionLT(10, 6))
Daniel Dunbarcacb0f02010-01-27 00:56:56 +00004693 CmdArgs.push_back("-ldylib1.10.5.o");
4694 }
Daniel Dunbar02633b52009-03-26 16:23:12 +00004695 } else {
4696 if (Args.hasArg(options::OPT_bundle)) {
Daniel Dunbar8a8d8af2009-04-01 03:17:40 +00004697 if (!Args.hasArg(options::OPT_static)) {
4698 // Derived from darwin_bundle1 spec.
Daniel Dunbar1051fc02011-04-01 21:02:42 +00004699 if (getDarwinToolChain().isTargetIOSSimulator()) {
4700 // The simulator doesn't have a versioned crt1 file.
4701 CmdArgs.push_back("-lbundle1.o");
4702 } else if (getDarwinToolChain().isTargetIPhoneOS()) {
Daniel Dunbarcacb0f02010-01-27 00:56:56 +00004703 if (getDarwinToolChain().isIPhoneOSVersionLT(3, 1))
4704 CmdArgs.push_back("-lbundle1.o");
4705 } else {
Daniel Dunbarce3fdf22010-01-27 00:57:03 +00004706 if (getDarwinToolChain().isMacosxVersionLT(10, 6))
Daniel Dunbarcacb0f02010-01-27 00:56:56 +00004707 CmdArgs.push_back("-lbundle1.o");
4708 }
Daniel Dunbar8a8d8af2009-04-01 03:17:40 +00004709 }
Daniel Dunbar02633b52009-03-26 16:23:12 +00004710 } else {
Daniel Dunbarbbe8e3e2011-03-01 18:49:30 +00004711 if (Args.hasArg(options::OPT_pg) &&
4712 getToolChain().SupportsProfiling()) {
Daniel Dunbar02633b52009-03-26 16:23:12 +00004713 if (Args.hasArg(options::OPT_static) ||
4714 Args.hasArg(options::OPT_object) ||
4715 Args.hasArg(options::OPT_preload)) {
4716 CmdArgs.push_back("-lgcrt0.o");
4717 } else {
4718 CmdArgs.push_back("-lgcrt1.o");
Daniel Dunbarc21c4852009-04-08 23:54:23 +00004719
Daniel Dunbar02633b52009-03-26 16:23:12 +00004720 // darwin_crt2 spec is empty.
Daniel Dunbarc21c4852009-04-08 23:54:23 +00004721 }
Bob Wilson4e6e7912012-07-04 00:18:41 +00004722 // By default on OS X 10.8 and later, we don't link with a crt1.o
4723 // file and the linker knows to use _main as the entry point. But,
4724 // when compiling with -pg, we need to link with the gcrt1.o file,
4725 // so pass the -no_new_main option to tell the linker to use the
4726 // "start" symbol as the entry point.
Bob Wilson1fc6e4f2012-07-03 20:42:10 +00004727 if (getDarwinToolChain().isTargetMacOS() &&
4728 !getDarwinToolChain().isMacosxVersionLT(10, 8))
4729 CmdArgs.push_back("-no_new_main");
Daniel Dunbar02633b52009-03-26 16:23:12 +00004730 } else {
4731 if (Args.hasArg(options::OPT_static) ||
4732 Args.hasArg(options::OPT_object) ||
4733 Args.hasArg(options::OPT_preload)) {
4734 CmdArgs.push_back("-lcrt0.o");
4735 } else {
4736 // Derived from darwin_crt1 spec.
Daniel Dunbar40355802011-03-31 17:12:33 +00004737 if (getDarwinToolChain().isTargetIOSSimulator()) {
4738 // The simulator doesn't have a versioned crt1 file.
4739 CmdArgs.push_back("-lcrt1.o");
4740 } else if (getDarwinToolChain().isTargetIPhoneOS()) {
Daniel Dunbarcacb0f02010-01-27 00:56:56 +00004741 if (getDarwinToolChain().isIPhoneOSVersionLT(3, 1))
4742 CmdArgs.push_back("-lcrt1.o");
Daniel Dunbar7a0c0642012-10-15 22:23:53 +00004743 else if (getDarwinToolChain().isIPhoneOSVersionLT(6, 0))
Daniel Dunbarcacb0f02010-01-27 00:56:56 +00004744 CmdArgs.push_back("-lcrt1.3.1.o");
Daniel Dunbarce3fdf22010-01-27 00:57:03 +00004745 } else {
4746 if (getDarwinToolChain().isMacosxVersionLT(10, 5))
4747 CmdArgs.push_back("-lcrt1.o");
4748 else if (getDarwinToolChain().isMacosxVersionLT(10, 6))
4749 CmdArgs.push_back("-lcrt1.10.5.o");
Ted Kremenekebcb57a2012-03-06 20:05:56 +00004750 else if (getDarwinToolChain().isMacosxVersionLT(10, 8))
Daniel Dunbarce3fdf22010-01-27 00:57:03 +00004751 CmdArgs.push_back("-lcrt1.10.6.o");
Daniel Dunbarc21c4852009-04-08 23:54:23 +00004752
Daniel Dunbarce3fdf22010-01-27 00:57:03 +00004753 // darwin_crt2 spec is empty.
4754 }
Daniel Dunbar02633b52009-03-26 16:23:12 +00004755 }
4756 }
4757 }
4758 }
4759
Daniel Dunbarce3fdf22010-01-27 00:57:03 +00004760 if (!getDarwinToolChain().isTargetIPhoneOS() &&
4761 Args.hasArg(options::OPT_shared_libgcc) &&
4762 getDarwinToolChain().isMacosxVersionLT(10, 5)) {
Daniel Dunbar88137642009-09-09 22:32:48 +00004763 const char *Str =
Daniel Dunbar4a7e8892010-07-14 18:46:23 +00004764 Args.MakeArgString(getToolChain().GetFilePath("crt3.o"));
Daniel Dunbar88137642009-09-09 22:32:48 +00004765 CmdArgs.push_back(Str);
Daniel Dunbar02633b52009-03-26 16:23:12 +00004766 }
4767 }
4768
4769 Args.AddAllArgs(CmdArgs, options::OPT_L);
Daniel Dunbarc21c4852009-04-08 23:54:23 +00004770
Kostya Serebryany7b5f1012011-12-06 19:18:44 +00004771 // If we're building a dynamic lib with -faddress-sanitizer, unresolved
4772 // symbols may appear. Mark all of them as dynamic_lookup.
4773 // Linking executables is handled in lib/Driver/ToolChains.cpp.
4774 if (Args.hasFlag(options::OPT_faddress_sanitizer,
4775 options::OPT_fno_address_sanitizer, false)) {
4776 if (Args.hasArg(options::OPT_dynamiclib) ||
4777 Args.hasArg(options::OPT_bundle)) {
4778 CmdArgs.push_back("-undefined");
4779 CmdArgs.push_back("dynamic_lookup");
4780 }
4781 }
4782
Daniel Dunbar02633b52009-03-26 16:23:12 +00004783 if (Args.hasArg(options::OPT_fopenmp))
4784 // This is more complicated in gcc...
4785 CmdArgs.push_back("-lgomp");
4786
Douglas Gregor04e326b2012-05-15 21:00:27 +00004787 AddLinkerInputs(getToolChain(), Inputs, Args, CmdArgs);
4788
Bob Wilson63d9f3c2012-05-15 18:57:39 +00004789 if (isObjCRuntimeLinked(Args) &&
4790 !Args.hasArg(options::OPT_nostdlib) &&
4791 !Args.hasArg(options::OPT_nodefaultlibs)) {
Ted Kremenekebcb57a2012-03-06 20:05:56 +00004792 // Avoid linking compatibility stubs on i386 mac.
4793 if (!getDarwinToolChain().isTargetMacOS() ||
Rafael Espindola64f7ad92012-10-07 04:44:33 +00004794 getDarwinToolChain().getArch() != llvm::Triple::x86) {
Ted Kremenekebcb57a2012-03-06 20:05:56 +00004795 // If we don't have ARC or subscripting runtime support, link in the
4796 // runtime stubs. We have to do this *before* adding any of the normal
4797 // linker inputs so that its initializer gets run first.
John McCall260611a2012-06-20 06:18:46 +00004798 ObjCRuntime runtime =
4799 getDarwinToolChain().getDefaultObjCRuntime(/*nonfragile*/ true);
Ted Kremenekebcb57a2012-03-06 20:05:56 +00004800 // We use arclite library for both ARC and subscripting support.
John McCall0a7dd782012-08-21 02:47:43 +00004801 if ((!runtime.hasNativeARC() && isObjCAutoRefCount(Args)) ||
John McCall260611a2012-06-20 06:18:46 +00004802 !runtime.hasSubscripting())
Ted Kremenekebcb57a2012-03-06 20:05:56 +00004803 getDarwinToolChain().AddLinkARCArgs(Args, CmdArgs);
Ted Kremenekebcb57a2012-03-06 20:05:56 +00004804 }
Bob Wilson0b1c7152012-04-21 00:21:42 +00004805 CmdArgs.push_back("-framework");
4806 CmdArgs.push_back("Foundation");
Ted Kremenekebcb57a2012-03-06 20:05:56 +00004807 // Link libobj.
4808 CmdArgs.push_back("-lobjc");
John McCall9f084a32011-07-06 00:26:06 +00004809 }
John McCallf85e1932011-06-15 23:02:42 +00004810
Daniel Dunbar02633b52009-03-26 16:23:12 +00004811 if (LinkingOutput) {
4812 CmdArgs.push_back("-arch_multiple");
4813 CmdArgs.push_back("-final_output");
4814 CmdArgs.push_back(LinkingOutput);
4815 }
4816
Daniel Dunbar02633b52009-03-26 16:23:12 +00004817 if (Args.hasArg(options::OPT_fnested_functions))
4818 CmdArgs.push_back("-allow_stack_execute");
Daniel Dunbarc21c4852009-04-08 23:54:23 +00004819
Daniel Dunbar02633b52009-03-26 16:23:12 +00004820 if (!Args.hasArg(options::OPT_nostdlib) &&
4821 !Args.hasArg(options::OPT_nodefaultlibs)) {
Daniel Dunbaree788e72009-12-21 18:54:17 +00004822 if (getToolChain().getDriver().CCCIsCXX)
Daniel Dunbar132e35d2010-09-17 01:20:05 +00004823 getToolChain().AddCXXStdlibLibArgs(Args, CmdArgs);
Daniel Dunbaredfa02b2009-04-08 06:06:21 +00004824
Daniel Dunbar02633b52009-03-26 16:23:12 +00004825 // link_ssp spec is empty.
4826
Daniel Dunbar6cd41542009-09-18 08:15:03 +00004827 // Let the tool chain choose which runtime library to link.
4828 getDarwinToolChain().AddLinkRuntimeLibArgs(Args, CmdArgs);
Daniel Dunbar02633b52009-03-26 16:23:12 +00004829 }
4830
Chad Rosier18937312012-05-16 23:45:12 +00004831 if (!Args.hasArg(options::OPT_nostdlib) &&
Daniel Dunbar02633b52009-03-26 16:23:12 +00004832 !Args.hasArg(options::OPT_nostartfiles)) {
4833 // endfile_spec is empty.
4834 }
4835
4836 Args.AddAllArgs(CmdArgs, options::OPT_T_Group);
4837 Args.AddAllArgs(CmdArgs, options::OPT_F);
4838
Daniel Dunbarc21c4852009-04-08 23:54:23 +00004839 const char *Exec =
Daniel Dunbar4a7e8892010-07-14 18:46:23 +00004840 Args.MakeArgString(getToolChain().GetProgramPath("ld"));
Daniel Dunbar2fe238e2010-08-02 02:38:28 +00004841 C.addCommand(new Command(JA, *this, Exec, CmdArgs));
Daniel Dunbar02633b52009-03-26 16:23:12 +00004842}
4843
Daniel Dunbarff7488d2009-03-20 00:52:38 +00004844void darwin::Lipo::ConstructJob(Compilation &C, const JobAction &JA,
Daniel Dunbar2fe238e2010-08-02 02:38:28 +00004845 const InputInfo &Output,
Daniel Dunbarc21c4852009-04-08 23:54:23 +00004846 const InputInfoList &Inputs,
4847 const ArgList &Args,
Daniel Dunbarff7488d2009-03-20 00:52:38 +00004848 const char *LinkingOutput) const {
4849 ArgStringList CmdArgs;
4850
4851 CmdArgs.push_back("-create");
4852 assert(Output.isFilename() && "Unexpected lipo output.");
Daniel Dunbara428df82009-03-24 00:24:37 +00004853
4854 CmdArgs.push_back("-output");
Daniel Dunbarff7488d2009-03-20 00:52:38 +00004855 CmdArgs.push_back(Output.getFilename());
Daniel Dunbara428df82009-03-24 00:24:37 +00004856
Daniel Dunbarff7488d2009-03-20 00:52:38 +00004857 for (InputInfoList::const_iterator
4858 it = Inputs.begin(), ie = Inputs.end(); it != ie; ++it) {
4859 const InputInfo &II = *it;
4860 assert(II.isFilename() && "Unexpected lipo input.");
4861 CmdArgs.push_back(II.getFilename());
4862 }
Daniel Dunbarc21c4852009-04-08 23:54:23 +00004863 const char *Exec =
Daniel Dunbar4a7e8892010-07-14 18:46:23 +00004864 Args.MakeArgString(getToolChain().GetProgramPath("lipo"));
Daniel Dunbar2fe238e2010-08-02 02:38:28 +00004865 C.addCommand(new Command(JA, *this, Exec, CmdArgs));
Daniel Dunbarff7488d2009-03-20 00:52:38 +00004866}
Daniel Dunbar68a31d42009-03-31 17:45:15 +00004867
Daniel Dunbar6e0f2542010-06-04 18:28:36 +00004868void darwin::Dsymutil::ConstructJob(Compilation &C, const JobAction &JA,
Daniel Dunbar2fe238e2010-08-02 02:38:28 +00004869 const InputInfo &Output,
Daniel Dunbar6e0f2542010-06-04 18:28:36 +00004870 const InputInfoList &Inputs,
4871 const ArgList &Args,
4872 const char *LinkingOutput) const {
4873 ArgStringList CmdArgs;
4874
Daniel Dunbar03e92302011-05-09 17:23:16 +00004875 CmdArgs.push_back("-o");
4876 CmdArgs.push_back(Output.getFilename());
4877
Daniel Dunbar6e0f2542010-06-04 18:28:36 +00004878 assert(Inputs.size() == 1 && "Unable to handle multiple inputs.");
4879 const InputInfo &Input = Inputs[0];
4880 assert(Input.isFilename() && "Unexpected dsymutil input.");
4881 CmdArgs.push_back(Input.getFilename());
4882
Daniel Dunbar6e0f2542010-06-04 18:28:36 +00004883 const char *Exec =
Daniel Dunbar4a7e8892010-07-14 18:46:23 +00004884 Args.MakeArgString(getToolChain().GetProgramPath("dsymutil"));
Daniel Dunbar2fe238e2010-08-02 02:38:28 +00004885 C.addCommand(new Command(JA, *this, Exec, CmdArgs));
Daniel Dunbar6e0f2542010-06-04 18:28:36 +00004886}
4887
Eric Christopherf8571862011-08-23 17:56:55 +00004888void darwin::VerifyDebug::ConstructJob(Compilation &C, const JobAction &JA,
4889 const InputInfo &Output,
4890 const InputInfoList &Inputs,
4891 const ArgList &Args,
4892 const char *LinkingOutput) const {
4893 ArgStringList CmdArgs;
4894 CmdArgs.push_back("--verify");
Eric Christopher1c79dc42012-02-06 19:13:09 +00004895 CmdArgs.push_back("--debug-info");
4896 CmdArgs.push_back("--eh-frame");
Eric Christopherb822f722012-02-06 19:43:51 +00004897 CmdArgs.push_back("--quiet");
Eric Christopherf8571862011-08-23 17:56:55 +00004898
4899 assert(Inputs.size() == 1 && "Unable to handle multiple inputs.");
4900 const InputInfo &Input = Inputs[0];
4901 assert(Input.isFilename() && "Unexpected verify input");
4902
4903 // Grabbing the output of the earlier dsymutil run.
4904 CmdArgs.push_back(Input.getFilename());
4905
4906 const char *Exec =
4907 Args.MakeArgString(getToolChain().GetProgramPath("dwarfdump"));
4908 C.addCommand(new Command(JA, *this, Exec, CmdArgs));
4909}
4910
David Chisnall31c46902012-02-15 13:39:01 +00004911void solaris::Assemble::ConstructJob(Compilation &C, const JobAction &JA,
4912 const InputInfo &Output,
4913 const InputInfoList &Inputs,
4914 const ArgList &Args,
4915 const char *LinkingOutput) const {
4916 ArgStringList CmdArgs;
4917
4918 Args.AddAllArgValues(CmdArgs, options::OPT_Wa_COMMA,
4919 options::OPT_Xassembler);
4920
4921 CmdArgs.push_back("-o");
4922 CmdArgs.push_back(Output.getFilename());
4923
4924 for (InputInfoList::const_iterator
4925 it = Inputs.begin(), ie = Inputs.end(); it != ie; ++it) {
4926 const InputInfo &II = *it;
4927 CmdArgs.push_back(II.getFilename());
4928 }
4929
4930 const char *Exec =
4931 Args.MakeArgString(getToolChain().GetProgramPath("as"));
4932 C.addCommand(new Command(JA, *this, Exec, CmdArgs));
4933}
4934
4935
4936void solaris::Link::ConstructJob(Compilation &C, const JobAction &JA,
4937 const InputInfo &Output,
4938 const InputInfoList &Inputs,
4939 const ArgList &Args,
4940 const char *LinkingOutput) const {
4941 // FIXME: Find a real GCC, don't hard-code versions here
4942 std::string GCCLibPath = "/usr/gcc/4.5/lib/gcc/";
4943 const llvm::Triple &T = getToolChain().getTriple();
4944 std::string LibPath = "/usr/lib/";
4945 llvm::Triple::ArchType Arch = T.getArch();
4946 switch (Arch) {
4947 case llvm::Triple::x86:
4948 GCCLibPath += ("i386-" + T.getVendorName() + "-" +
4949 T.getOSName()).str() + "/4.5.2/";
4950 break;
4951 case llvm::Triple::x86_64:
4952 GCCLibPath += ("i386-" + T.getVendorName() + "-" +
4953 T.getOSName()).str();
4954 GCCLibPath += "/4.5.2/amd64/";
4955 LibPath += "amd64/";
4956 break;
4957 default:
4958 assert(0 && "Unsupported architecture");
4959 }
4960
4961 ArgStringList CmdArgs;
4962
David Chisnall41d476d2012-02-29 15:06:12 +00004963 // Demangle C++ names in errors
4964 CmdArgs.push_back("-C");
4965
David Chisnall31c46902012-02-15 13:39:01 +00004966 if ((!Args.hasArg(options::OPT_nostdlib)) &&
4967 (!Args.hasArg(options::OPT_shared))) {
4968 CmdArgs.push_back("-e");
4969 CmdArgs.push_back("_start");
4970 }
4971
4972 if (Args.hasArg(options::OPT_static)) {
4973 CmdArgs.push_back("-Bstatic");
4974 CmdArgs.push_back("-dn");
4975 } else {
4976 CmdArgs.push_back("-Bdynamic");
4977 if (Args.hasArg(options::OPT_shared)) {
4978 CmdArgs.push_back("-shared");
4979 } else {
4980 CmdArgs.push_back("--dynamic-linker");
4981 CmdArgs.push_back(Args.MakeArgString(LibPath + "ld.so.1"));
4982 }
4983 }
4984
4985 if (Output.isFilename()) {
4986 CmdArgs.push_back("-o");
4987 CmdArgs.push_back(Output.getFilename());
4988 } else {
4989 assert(Output.isNothing() && "Invalid output.");
4990 }
4991
4992 if (!Args.hasArg(options::OPT_nostdlib) &&
4993 !Args.hasArg(options::OPT_nostartfiles)) {
4994 if (!Args.hasArg(options::OPT_shared)) {
4995 CmdArgs.push_back(Args.MakeArgString(LibPath + "crt1.o"));
4996 CmdArgs.push_back(Args.MakeArgString(LibPath + "crti.o"));
David Chisnall165329c2012-02-28 17:10:04 +00004997 CmdArgs.push_back(Args.MakeArgString(LibPath + "values-Xa.o"));
David Chisnall31c46902012-02-15 13:39:01 +00004998 CmdArgs.push_back(Args.MakeArgString(GCCLibPath + "crtbegin.o"));
4999 } else {
5000 CmdArgs.push_back(Args.MakeArgString(LibPath + "crti.o"));
David Chisnall165329c2012-02-28 17:10:04 +00005001 CmdArgs.push_back(Args.MakeArgString(LibPath + "values-Xa.o"));
5002 CmdArgs.push_back(Args.MakeArgString(GCCLibPath + "crtbegin.o"));
David Chisnall31c46902012-02-15 13:39:01 +00005003 }
David Chisnalle6dd6832012-03-13 14:14:54 +00005004 if (getToolChain().getDriver().CCCIsCXX)
5005 CmdArgs.push_back(Args.MakeArgString(LibPath + "cxa_finalize.o"));
David Chisnall31c46902012-02-15 13:39:01 +00005006 }
5007
5008 CmdArgs.push_back(Args.MakeArgString("-L" + GCCLibPath));
5009
5010 Args.AddAllArgs(CmdArgs, options::OPT_L);
5011 Args.AddAllArgs(CmdArgs, options::OPT_T_Group);
5012 Args.AddAllArgs(CmdArgs, options::OPT_e);
David Chisnall165329c2012-02-28 17:10:04 +00005013 Args.AddAllArgs(CmdArgs, options::OPT_r);
David Chisnall31c46902012-02-15 13:39:01 +00005014
5015 AddLinkerInputs(getToolChain(), Inputs, Args, CmdArgs);
5016
5017 if (!Args.hasArg(options::OPT_nostdlib) &&
5018 !Args.hasArg(options::OPT_nodefaultlibs)) {
David Chisnalle58e6f92012-04-10 11:49:50 +00005019 if (getToolChain().getDriver().CCCIsCXX)
5020 getToolChain().AddCXXStdlibLibArgs(Args, CmdArgs);
David Chisnallb6229592012-02-15 18:24:31 +00005021 CmdArgs.push_back("-lgcc_s");
David Chisnall165329c2012-02-28 17:10:04 +00005022 if (!Args.hasArg(options::OPT_shared)) {
5023 CmdArgs.push_back("-lgcc");
David Chisnall31c46902012-02-15 13:39:01 +00005024 CmdArgs.push_back("-lc");
David Chisnall7dbefe12012-02-28 20:06:45 +00005025 CmdArgs.push_back("-lm");
David Chisnall165329c2012-02-28 17:10:04 +00005026 }
David Chisnall31c46902012-02-15 13:39:01 +00005027 }
5028
5029 if (!Args.hasArg(options::OPT_nostdlib) &&
5030 !Args.hasArg(options::OPT_nostartfiles)) {
David Chisnall165329c2012-02-28 17:10:04 +00005031 CmdArgs.push_back(Args.MakeArgString(GCCLibPath + "crtend.o"));
David Chisnall31c46902012-02-15 13:39:01 +00005032 }
David Chisnalld1ac03e2012-02-16 16:00:47 +00005033 CmdArgs.push_back(Args.MakeArgString(LibPath + "crtn.o"));
David Chisnall31c46902012-02-15 13:39:01 +00005034
5035 addProfileRT(getToolChain(), Args, CmdArgs, getToolChain().getTriple());
5036
5037 const char *Exec =
5038 Args.MakeArgString(getToolChain().GetProgramPath("ld"));
5039 C.addCommand(new Command(JA, *this, Exec, CmdArgs));
5040}
5041
Edward O'Callaghane7925a02009-08-22 01:06:46 +00005042void auroraux::Assemble::ConstructJob(Compilation &C, const JobAction &JA,
Daniel Dunbar2fe238e2010-08-02 02:38:28 +00005043 const InputInfo &Output,
Daniel Dunbar294691e2009-11-04 06:24:38 +00005044 const InputInfoList &Inputs,
5045 const ArgList &Args,
5046 const char *LinkingOutput) const {
Edward O'Callaghane7925a02009-08-22 01:06:46 +00005047 ArgStringList CmdArgs;
5048
5049 Args.AddAllArgValues(CmdArgs, options::OPT_Wa_COMMA,
5050 options::OPT_Xassembler);
5051
5052 CmdArgs.push_back("-o");
Daniel Dunbar7c1e4652010-08-02 02:38:21 +00005053 CmdArgs.push_back(Output.getFilename());
Edward O'Callaghane7925a02009-08-22 01:06:46 +00005054
5055 for (InputInfoList::const_iterator
5056 it = Inputs.begin(), ie = Inputs.end(); it != ie; ++it) {
5057 const InputInfo &II = *it;
Daniel Dunbar7c1e4652010-08-02 02:38:21 +00005058 CmdArgs.push_back(II.getFilename());
Edward O'Callaghane7925a02009-08-22 01:06:46 +00005059 }
5060
5061 const char *Exec =
Daniel Dunbar4a7e8892010-07-14 18:46:23 +00005062 Args.MakeArgString(getToolChain().GetProgramPath("gas"));
Daniel Dunbar2fe238e2010-08-02 02:38:28 +00005063 C.addCommand(new Command(JA, *this, Exec, CmdArgs));
Edward O'Callaghane7925a02009-08-22 01:06:46 +00005064}
5065
5066void auroraux::Link::ConstructJob(Compilation &C, const JobAction &JA,
Daniel Dunbar2fe238e2010-08-02 02:38:28 +00005067 const InputInfo &Output,
Daniel Dunbar294691e2009-11-04 06:24:38 +00005068 const InputInfoList &Inputs,
5069 const ArgList &Args,
5070 const char *LinkingOutput) const {
Edward O'Callaghane7925a02009-08-22 01:06:46 +00005071 ArgStringList CmdArgs;
5072
5073 if ((!Args.hasArg(options::OPT_nostdlib)) &&
Daniel Dunbar294691e2009-11-04 06:24:38 +00005074 (!Args.hasArg(options::OPT_shared))) {
Edward O'Callaghane7925a02009-08-22 01:06:46 +00005075 CmdArgs.push_back("-e");
Edward O'Callaghan7adf9492009-10-15 07:44:07 +00005076 CmdArgs.push_back("_start");
Edward O'Callaghane7925a02009-08-22 01:06:46 +00005077 }
5078
5079 if (Args.hasArg(options::OPT_static)) {
5080 CmdArgs.push_back("-Bstatic");
Edward O'Callaghan7adf9492009-10-15 07:44:07 +00005081 CmdArgs.push_back("-dn");
Edward O'Callaghane7925a02009-08-22 01:06:46 +00005082 } else {
Edward O'Callaghan7adf9492009-10-15 07:44:07 +00005083// CmdArgs.push_back("--eh-frame-hdr");
Edward O'Callaghane7925a02009-08-22 01:06:46 +00005084 CmdArgs.push_back("-Bdynamic");
5085 if (Args.hasArg(options::OPT_shared)) {
5086 CmdArgs.push_back("-shared");
5087 } else {
Edward O'Callaghan3cecc192009-10-16 19:44:18 +00005088 CmdArgs.push_back("--dynamic-linker");
Edward O'Callaghane7925a02009-08-22 01:06:46 +00005089 CmdArgs.push_back("/lib/ld.so.1"); // 64Bit Path /lib/amd64/ld.so.1
5090 }
5091 }
5092
Daniel Dunbar7c1e4652010-08-02 02:38:21 +00005093 if (Output.isFilename()) {
Edward O'Callaghane7925a02009-08-22 01:06:46 +00005094 CmdArgs.push_back("-o");
5095 CmdArgs.push_back(Output.getFilename());
5096 } else {
5097 assert(Output.isNothing() && "Invalid output.");
5098 }
5099
5100 if (!Args.hasArg(options::OPT_nostdlib) &&
5101 !Args.hasArg(options::OPT_nostartfiles)) {
5102 if (!Args.hasArg(options::OPT_shared)) {
Chris Lattner38e317d2010-07-07 16:01:42 +00005103 CmdArgs.push_back(Args.MakeArgString(
Daniel Dunbar4a7e8892010-07-14 18:46:23 +00005104 getToolChain().GetFilePath("crt1.o")));
Chris Lattner38e317d2010-07-07 16:01:42 +00005105 CmdArgs.push_back(Args.MakeArgString(
Daniel Dunbar4a7e8892010-07-14 18:46:23 +00005106 getToolChain().GetFilePath("crti.o")));
Chris Lattner38e317d2010-07-07 16:01:42 +00005107 CmdArgs.push_back(Args.MakeArgString(
Daniel Dunbar4a7e8892010-07-14 18:46:23 +00005108 getToolChain().GetFilePath("crtbegin.o")));
Edward O'Callaghane7925a02009-08-22 01:06:46 +00005109 } else {
Chris Lattner38e317d2010-07-07 16:01:42 +00005110 CmdArgs.push_back(Args.MakeArgString(
Daniel Dunbar4a7e8892010-07-14 18:46:23 +00005111 getToolChain().GetFilePath("crti.o")));
Edward O'Callaghane7925a02009-08-22 01:06:46 +00005112 }
Chris Lattner38e317d2010-07-07 16:01:42 +00005113 CmdArgs.push_back(Args.MakeArgString(
Daniel Dunbar4a7e8892010-07-14 18:46:23 +00005114 getToolChain().GetFilePath("crtn.o")));
Edward O'Callaghane7925a02009-08-22 01:06:46 +00005115 }
5116
Daniel Dunbar294691e2009-11-04 06:24:38 +00005117 CmdArgs.push_back(Args.MakeArgString("-L/opt/gcc4/lib/gcc/"
5118 + getToolChain().getTripleString()
Daniel Dunbarf7fb31f2009-10-29 02:24:37 +00005119 + "/4.2.4"));
Edward O'Callaghane7925a02009-08-22 01:06:46 +00005120
5121 Args.AddAllArgs(CmdArgs, options::OPT_L);
5122 Args.AddAllArgs(CmdArgs, options::OPT_T_Group);
5123 Args.AddAllArgs(CmdArgs, options::OPT_e);
5124
Daniel Dunbar2008fee2010-09-17 00:24:54 +00005125 AddLinkerInputs(getToolChain(), Inputs, Args, CmdArgs);
Edward O'Callaghane7925a02009-08-22 01:06:46 +00005126
5127 if (!Args.hasArg(options::OPT_nostdlib) &&
5128 !Args.hasArg(options::OPT_nodefaultlibs)) {
5129 // FIXME: For some reason GCC passes -lgcc before adding
5130 // the default system libraries. Just mimic this for now.
5131 CmdArgs.push_back("-lgcc");
5132
5133 if (Args.hasArg(options::OPT_pthread))
5134 CmdArgs.push_back("-pthread");
5135 if (!Args.hasArg(options::OPT_shared))
5136 CmdArgs.push_back("-lc");
5137 CmdArgs.push_back("-lgcc");
5138 }
5139
5140 if (!Args.hasArg(options::OPT_nostdlib) &&
5141 !Args.hasArg(options::OPT_nostartfiles)) {
5142 if (!Args.hasArg(options::OPT_shared))
Chris Lattner38e317d2010-07-07 16:01:42 +00005143 CmdArgs.push_back(Args.MakeArgString(
Daniel Dunbar4a7e8892010-07-14 18:46:23 +00005144 getToolChain().GetFilePath("crtend.o")));
Edward O'Callaghane7925a02009-08-22 01:06:46 +00005145 }
5146
Bill Wendling3f4be6f2011-06-27 19:15:03 +00005147 addProfileRT(getToolChain(), Args, CmdArgs, getToolChain().getTriple());
Nick Lewycky2e95a6d2011-05-24 21:54:59 +00005148
Edward O'Callaghane7925a02009-08-22 01:06:46 +00005149 const char *Exec =
Daniel Dunbar4a7e8892010-07-14 18:46:23 +00005150 Args.MakeArgString(getToolChain().GetProgramPath("ld"));
Daniel Dunbar2fe238e2010-08-02 02:38:28 +00005151 C.addCommand(new Command(JA, *this, Exec, CmdArgs));
Edward O'Callaghane7925a02009-08-22 01:06:46 +00005152}
5153
Daniel Dunbarf7b8eec2009-06-29 20:52:51 +00005154void openbsd::Assemble::ConstructJob(Compilation &C, const JobAction &JA,
Daniel Dunbar2fe238e2010-08-02 02:38:28 +00005155 const InputInfo &Output,
Daniel Dunbarf7b8eec2009-06-29 20:52:51 +00005156 const InputInfoList &Inputs,
5157 const ArgList &Args,
Mike Stump1eb44332009-09-09 15:08:12 +00005158 const char *LinkingOutput) const {
Daniel Dunbarf7b8eec2009-06-29 20:52:51 +00005159 ArgStringList CmdArgs;
5160
5161 Args.AddAllArgValues(CmdArgs, options::OPT_Wa_COMMA,
5162 options::OPT_Xassembler);
5163
5164 CmdArgs.push_back("-o");
Daniel Dunbar7c1e4652010-08-02 02:38:21 +00005165 CmdArgs.push_back(Output.getFilename());
Daniel Dunbarf7b8eec2009-06-29 20:52:51 +00005166
5167 for (InputInfoList::const_iterator
5168 it = Inputs.begin(), ie = Inputs.end(); it != ie; ++it) {
5169 const InputInfo &II = *it;
Daniel Dunbar7c1e4652010-08-02 02:38:21 +00005170 CmdArgs.push_back(II.getFilename());
Daniel Dunbarf7b8eec2009-06-29 20:52:51 +00005171 }
5172
5173 const char *Exec =
Daniel Dunbar4a7e8892010-07-14 18:46:23 +00005174 Args.MakeArgString(getToolChain().GetProgramPath("as"));
Daniel Dunbar2fe238e2010-08-02 02:38:28 +00005175 C.addCommand(new Command(JA, *this, Exec, CmdArgs));
Daniel Dunbarf7b8eec2009-06-29 20:52:51 +00005176}
5177
5178void openbsd::Link::ConstructJob(Compilation &C, const JobAction &JA,
Daniel Dunbar2fe238e2010-08-02 02:38:28 +00005179 const InputInfo &Output,
Daniel Dunbarf7b8eec2009-06-29 20:52:51 +00005180 const InputInfoList &Inputs,
5181 const ArgList &Args,
5182 const char *LinkingOutput) const {
Daniel Dunbaree788e72009-12-21 18:54:17 +00005183 const Driver &D = getToolChain().getDriver();
Daniel Dunbarf7b8eec2009-06-29 20:52:51 +00005184 ArgStringList CmdArgs;
5185
Daniel Dunbar2bbcf662009-08-03 01:28:59 +00005186 if ((!Args.hasArg(options::OPT_nostdlib)) &&
Daniel Dunbar294691e2009-11-04 06:24:38 +00005187 (!Args.hasArg(options::OPT_shared))) {
Daniel Dunbar2bbcf662009-08-03 01:28:59 +00005188 CmdArgs.push_back("-e");
5189 CmdArgs.push_back("__start");
5190 }
5191
Daniel Dunbarf7b8eec2009-06-29 20:52:51 +00005192 if (Args.hasArg(options::OPT_static)) {
5193 CmdArgs.push_back("-Bstatic");
5194 } else {
Rafael Espindola65ba55d2010-11-11 02:17:51 +00005195 if (Args.hasArg(options::OPT_rdynamic))
5196 CmdArgs.push_back("-export-dynamic");
Daniel Dunbarf7b8eec2009-06-29 20:52:51 +00005197 CmdArgs.push_back("--eh-frame-hdr");
Daniel Dunbar2bbcf662009-08-03 01:28:59 +00005198 CmdArgs.push_back("-Bdynamic");
Daniel Dunbarf7b8eec2009-06-29 20:52:51 +00005199 if (Args.hasArg(options::OPT_shared)) {
Daniel Dunbar2bbcf662009-08-03 01:28:59 +00005200 CmdArgs.push_back("-shared");
Daniel Dunbarf7b8eec2009-06-29 20:52:51 +00005201 } else {
5202 CmdArgs.push_back("-dynamic-linker");
5203 CmdArgs.push_back("/usr/libexec/ld.so");
5204 }
5205 }
5206
Daniel Dunbar7c1e4652010-08-02 02:38:21 +00005207 if (Output.isFilename()) {
Daniel Dunbarf7b8eec2009-06-29 20:52:51 +00005208 CmdArgs.push_back("-o");
5209 CmdArgs.push_back(Output.getFilename());
5210 } else {
5211 assert(Output.isNothing() && "Invalid output.");
5212 }
5213
5214 if (!Args.hasArg(options::OPT_nostdlib) &&
5215 !Args.hasArg(options::OPT_nostartfiles)) {
5216 if (!Args.hasArg(options::OPT_shared)) {
Eli Friedman62d829a2011-12-15 02:15:56 +00005217 if (Args.hasArg(options::OPT_pg))
5218 CmdArgs.push_back(Args.MakeArgString(
5219 getToolChain().GetFilePath("gcrt0.o")));
5220 else
5221 CmdArgs.push_back(Args.MakeArgString(
5222 getToolChain().GetFilePath("crt0.o")));
Chris Lattner38e317d2010-07-07 16:01:42 +00005223 CmdArgs.push_back(Args.MakeArgString(
Daniel Dunbar4a7e8892010-07-14 18:46:23 +00005224 getToolChain().GetFilePath("crtbegin.o")));
Daniel Dunbarf7b8eec2009-06-29 20:52:51 +00005225 } else {
Chris Lattner38e317d2010-07-07 16:01:42 +00005226 CmdArgs.push_back(Args.MakeArgString(
Daniel Dunbar4a7e8892010-07-14 18:46:23 +00005227 getToolChain().GetFilePath("crtbeginS.o")));
Daniel Dunbarf7b8eec2009-06-29 20:52:51 +00005228 }
5229 }
5230
Edward O'Callaghane7e18202009-10-28 15:13:08 +00005231 std::string Triple = getToolChain().getTripleString();
5232 if (Triple.substr(0, 6) == "x86_64")
Daniel Dunbar294691e2009-11-04 06:24:38 +00005233 Triple.replace(0, 6, "amd64");
Daniel Dunbarf7fb31f2009-10-29 02:24:37 +00005234 CmdArgs.push_back(Args.MakeArgString("-L/usr/lib/gcc-lib/" + Triple +
Daniel Dunbar95c04572010-08-01 23:13:54 +00005235 "/4.2.1"));
Daniel Dunbar2bbcf662009-08-03 01:28:59 +00005236
Daniel Dunbarf7b8eec2009-06-29 20:52:51 +00005237 Args.AddAllArgs(CmdArgs, options::OPT_L);
5238 Args.AddAllArgs(CmdArgs, options::OPT_T_Group);
5239 Args.AddAllArgs(CmdArgs, options::OPT_e);
5240
Daniel Dunbar2008fee2010-09-17 00:24:54 +00005241 AddLinkerInputs(getToolChain(), Inputs, Args, CmdArgs);
Daniel Dunbarf7b8eec2009-06-29 20:52:51 +00005242
5243 if (!Args.hasArg(options::OPT_nostdlib) &&
5244 !Args.hasArg(options::OPT_nodefaultlibs)) {
Daniel Dunbar95c04572010-08-01 23:13:54 +00005245 if (D.CCCIsCXX) {
Daniel Dunbar132e35d2010-09-17 01:20:05 +00005246 getToolChain().AddCXXStdlibLibArgs(Args, CmdArgs);
Eli Friedman62d829a2011-12-15 02:15:56 +00005247 if (Args.hasArg(options::OPT_pg))
5248 CmdArgs.push_back("-lm_p");
5249 else
5250 CmdArgs.push_back("-lm");
Daniel Dunbar95c04572010-08-01 23:13:54 +00005251 }
5252
Daniel Dunbar2bbcf662009-08-03 01:28:59 +00005253 // FIXME: For some reason GCC passes -lgcc before adding
5254 // the default system libraries. Just mimic this for now.
5255 CmdArgs.push_back("-lgcc");
Daniel Dunbarf7b8eec2009-06-29 20:52:51 +00005256
Eric Christopherdc6cc872012-09-13 06:32:34 +00005257 if (Args.hasArg(options::OPT_pthread)) {
5258 if (!Args.hasArg(options::OPT_shared) &&
5259 Args.hasArg(options::OPT_pg))
5260 CmdArgs.push_back("-lpthread_p");
5261 else
5262 CmdArgs.push_back("-lpthread");
5263 }
5264
Chandler Carruth657849c2011-12-17 22:32:42 +00005265 if (!Args.hasArg(options::OPT_shared)) {
Eric Christopherdc6cc872012-09-13 06:32:34 +00005266 if (Args.hasArg(options::OPT_pg))
Eli Friedman62d829a2011-12-15 02:15:56 +00005267 CmdArgs.push_back("-lc_p");
5268 else
5269 CmdArgs.push_back("-lc");
Chandler Carruth657849c2011-12-17 22:32:42 +00005270 }
Eric Christopherdc6cc872012-09-13 06:32:34 +00005271
Daniel Dunbar2bbcf662009-08-03 01:28:59 +00005272 CmdArgs.push_back("-lgcc");
Daniel Dunbarf7b8eec2009-06-29 20:52:51 +00005273 }
5274
5275 if (!Args.hasArg(options::OPT_nostdlib) &&
5276 !Args.hasArg(options::OPT_nostartfiles)) {
5277 if (!Args.hasArg(options::OPT_shared))
Chris Lattner38e317d2010-07-07 16:01:42 +00005278 CmdArgs.push_back(Args.MakeArgString(
Daniel Dunbar4a7e8892010-07-14 18:46:23 +00005279 getToolChain().GetFilePath("crtend.o")));
Daniel Dunbarf7b8eec2009-06-29 20:52:51 +00005280 else
Chris Lattner38e317d2010-07-07 16:01:42 +00005281 CmdArgs.push_back(Args.MakeArgString(
Daniel Dunbar4a7e8892010-07-14 18:46:23 +00005282 getToolChain().GetFilePath("crtendS.o")));
Daniel Dunbarf7b8eec2009-06-29 20:52:51 +00005283 }
5284
5285 const char *Exec =
Daniel Dunbar4a7e8892010-07-14 18:46:23 +00005286 Args.MakeArgString(getToolChain().GetProgramPath("ld"));
Daniel Dunbar2fe238e2010-08-02 02:38:28 +00005287 C.addCommand(new Command(JA, *this, Exec, CmdArgs));
Daniel Dunbarf7b8eec2009-06-29 20:52:51 +00005288}
Ed Schoutenc66a5a32009-04-02 19:13:12 +00005289
Eli Friedman42f74f22012-08-08 23:57:20 +00005290void bitrig::Assemble::ConstructJob(Compilation &C, const JobAction &JA,
5291 const InputInfo &Output,
5292 const InputInfoList &Inputs,
5293 const ArgList &Args,
5294 const char *LinkingOutput) const {
5295 ArgStringList CmdArgs;
5296
5297 Args.AddAllArgValues(CmdArgs, options::OPT_Wa_COMMA,
5298 options::OPT_Xassembler);
5299
5300 CmdArgs.push_back("-o");
5301 CmdArgs.push_back(Output.getFilename());
5302
5303 for (InputInfoList::const_iterator
5304 it = Inputs.begin(), ie = Inputs.end(); it != ie; ++it) {
5305 const InputInfo &II = *it;
5306 CmdArgs.push_back(II.getFilename());
5307 }
5308
5309 const char *Exec =
5310 Args.MakeArgString(getToolChain().GetProgramPath("as"));
5311 C.addCommand(new Command(JA, *this, Exec, CmdArgs));
5312}
5313
5314void bitrig::Link::ConstructJob(Compilation &C, const JobAction &JA,
5315 const InputInfo &Output,
5316 const InputInfoList &Inputs,
5317 const ArgList &Args,
5318 const char *LinkingOutput) const {
5319 const Driver &D = getToolChain().getDriver();
5320 ArgStringList CmdArgs;
5321
5322 if ((!Args.hasArg(options::OPT_nostdlib)) &&
5323 (!Args.hasArg(options::OPT_shared))) {
5324 CmdArgs.push_back("-e");
5325 CmdArgs.push_back("__start");
5326 }
5327
5328 if (Args.hasArg(options::OPT_static)) {
5329 CmdArgs.push_back("-Bstatic");
5330 } else {
5331 if (Args.hasArg(options::OPT_rdynamic))
5332 CmdArgs.push_back("-export-dynamic");
5333 CmdArgs.push_back("--eh-frame-hdr");
5334 CmdArgs.push_back("-Bdynamic");
5335 if (Args.hasArg(options::OPT_shared)) {
5336 CmdArgs.push_back("-shared");
5337 } else {
5338 CmdArgs.push_back("-dynamic-linker");
5339 CmdArgs.push_back("/usr/libexec/ld.so");
5340 }
5341 }
5342
5343 if (Output.isFilename()) {
5344 CmdArgs.push_back("-o");
5345 CmdArgs.push_back(Output.getFilename());
5346 } else {
5347 assert(Output.isNothing() && "Invalid output.");
5348 }
5349
5350 if (!Args.hasArg(options::OPT_nostdlib) &&
5351 !Args.hasArg(options::OPT_nostartfiles)) {
5352 if (!Args.hasArg(options::OPT_shared)) {
5353 if (Args.hasArg(options::OPT_pg))
5354 CmdArgs.push_back(Args.MakeArgString(
5355 getToolChain().GetFilePath("gcrt0.o")));
5356 else
5357 CmdArgs.push_back(Args.MakeArgString(
5358 getToolChain().GetFilePath("crt0.o")));
5359 CmdArgs.push_back(Args.MakeArgString(
5360 getToolChain().GetFilePath("crtbegin.o")));
5361 } else {
5362 CmdArgs.push_back(Args.MakeArgString(
5363 getToolChain().GetFilePath("crtbeginS.o")));
5364 }
5365 }
5366
5367 Args.AddAllArgs(CmdArgs, options::OPT_L);
5368 Args.AddAllArgs(CmdArgs, options::OPT_T_Group);
5369 Args.AddAllArgs(CmdArgs, options::OPT_e);
5370
5371 AddLinkerInputs(getToolChain(), Inputs, Args, CmdArgs);
5372
5373 if (!Args.hasArg(options::OPT_nostdlib) &&
5374 !Args.hasArg(options::OPT_nodefaultlibs)) {
5375 if (D.CCCIsCXX) {
5376 getToolChain().AddCXXStdlibLibArgs(Args, CmdArgs);
5377 if (Args.hasArg(options::OPT_pg))
5378 CmdArgs.push_back("-lm_p");
5379 else
5380 CmdArgs.push_back("-lm");
5381 }
5382
Rafael Espindola3667bbe2012-10-23 17:07:31 +00005383 if (Args.hasArg(options::OPT_pthread)) {
5384 if (!Args.hasArg(options::OPT_shared) &&
5385 Args.hasArg(options::OPT_pg))
5386 CmdArgs.push_back("-lpthread_p");
5387 else
5388 CmdArgs.push_back("-lpthread");
5389 }
5390
Eli Friedman42f74f22012-08-08 23:57:20 +00005391 if (!Args.hasArg(options::OPT_shared)) {
5392 if (Args.hasArg(options::OPT_pg))
5393 CmdArgs.push_back("-lc_p");
5394 else
5395 CmdArgs.push_back("-lc");
5396 }
5397
5398 std::string myarch = "-lclang_rt.";
5399 const llvm::Triple &T = getToolChain().getTriple();
5400 llvm::Triple::ArchType Arch = T.getArch();
5401 switch (Arch) {
5402 case llvm::Triple::arm:
5403 myarch += ("arm");
5404 break;
5405 case llvm::Triple::x86:
5406 myarch += ("i386");
5407 break;
5408 case llvm::Triple::x86_64:
5409 myarch += ("amd64");
5410 break;
5411 default:
5412 assert(0 && "Unsupported architecture");
5413 }
5414 CmdArgs.push_back(Args.MakeArgString(myarch));
5415 }
5416
5417 if (!Args.hasArg(options::OPT_nostdlib) &&
5418 !Args.hasArg(options::OPT_nostartfiles)) {
5419 if (!Args.hasArg(options::OPT_shared))
5420 CmdArgs.push_back(Args.MakeArgString(
5421 getToolChain().GetFilePath("crtend.o")));
5422 else
5423 CmdArgs.push_back(Args.MakeArgString(
5424 getToolChain().GetFilePath("crtendS.o")));
5425 }
Eli Friedmanc9c48db2012-08-09 22:42:04 +00005426
5427 const char *Exec =
5428 Args.MakeArgString(getToolChain().GetProgramPath("ld"));
5429 C.addCommand(new Command(JA, *this, Exec, CmdArgs));
Eli Friedman42f74f22012-08-08 23:57:20 +00005430}
5431
Daniel Dunbar68a31d42009-03-31 17:45:15 +00005432void freebsd::Assemble::ConstructJob(Compilation &C, const JobAction &JA,
Daniel Dunbar2fe238e2010-08-02 02:38:28 +00005433 const InputInfo &Output,
Daniel Dunbarc21c4852009-04-08 23:54:23 +00005434 const InputInfoList &Inputs,
5435 const ArgList &Args,
Mike Stump1eb44332009-09-09 15:08:12 +00005436 const char *LinkingOutput) const {
Daniel Dunbar68a31d42009-03-31 17:45:15 +00005437 ArgStringList CmdArgs;
5438
Daniel Dunbar008f54a2009-04-01 19:36:32 +00005439 // When building 32-bit code on FreeBSD/amd64, we have to explicitly
5440 // instruct as in the base system to assemble 32-bit code.
Eric Christopherc55da4b2012-09-05 21:32:44 +00005441 if (getToolChain().getArch() == llvm::Triple::x86)
Daniel Dunbar68a31d42009-03-31 17:45:15 +00005442 CmdArgs.push_back("--32");
Eric Christopherc55da4b2012-09-05 21:32:44 +00005443 else if (getToolChain().getArch() == llvm::Triple::ppc)
Roman Divacky3393cef2011-06-04 07:37:31 +00005444 CmdArgs.push_back("-a32");
Eric Christopherc55da4b2012-09-05 21:32:44 +00005445 else if (getToolChain().getArch() == llvm::Triple::mips ||
5446 getToolChain().getArch() == llvm::Triple::mipsel ||
5447 getToolChain().getArch() == llvm::Triple::mips64 ||
5448 getToolChain().getArch() == llvm::Triple::mips64el) {
5449 StringRef CPUName;
5450 StringRef ABIName;
5451 getMipsCPUAndABI(Args, getToolChain(), CPUName, ABIName);
Michael J. Spencer20249a12010-10-21 03:16:25 +00005452
Eric Christopherc55da4b2012-09-05 21:32:44 +00005453 CmdArgs.push_back("-march");
5454 CmdArgs.push_back(CPUName.data());
5455
5456 // Convert ABI name to the GNU tools acceptable variant.
5457 if (ABIName == "o32")
5458 ABIName = "32";
5459 else if (ABIName == "n64")
5460 ABIName = "64";
5461
5462 CmdArgs.push_back("-mabi");
5463 CmdArgs.push_back(ABIName.data());
5464
5465 if (getToolChain().getArch() == llvm::Triple::mips ||
5466 getToolChain().getArch() == llvm::Triple::mips64)
5467 CmdArgs.push_back("-EB");
5468 else
5469 CmdArgs.push_back("-EL");
5470
5471 Arg *LastPICArg = Args.getLastArg(options::OPT_fPIC, options::OPT_fno_PIC,
5472 options::OPT_fpic, options::OPT_fno_pic,
5473 options::OPT_fPIE, options::OPT_fno_PIE,
5474 options::OPT_fpie, options::OPT_fno_pie);
5475 if (LastPICArg &&
5476 (LastPICArg->getOption().matches(options::OPT_fPIC) ||
5477 LastPICArg->getOption().matches(options::OPT_fpic) ||
5478 LastPICArg->getOption().matches(options::OPT_fPIE) ||
5479 LastPICArg->getOption().matches(options::OPT_fpie))) {
5480 CmdArgs.push_back("-KPIC");
5481 }
5482 }
Eric Christophered734732010-03-02 02:41:08 +00005483
Daniel Dunbar68a31d42009-03-31 17:45:15 +00005484 Args.AddAllArgValues(CmdArgs, options::OPT_Wa_COMMA,
5485 options::OPT_Xassembler);
5486
5487 CmdArgs.push_back("-o");
Daniel Dunbar7c1e4652010-08-02 02:38:21 +00005488 CmdArgs.push_back(Output.getFilename());
Daniel Dunbar68a31d42009-03-31 17:45:15 +00005489
5490 for (InputInfoList::const_iterator
5491 it = Inputs.begin(), ie = Inputs.end(); it != ie; ++it) {
5492 const InputInfo &II = *it;
Daniel Dunbar7c1e4652010-08-02 02:38:21 +00005493 CmdArgs.push_back(II.getFilename());
Daniel Dunbar68a31d42009-03-31 17:45:15 +00005494 }
5495
Daniel Dunbarc21c4852009-04-08 23:54:23 +00005496 const char *Exec =
Daniel Dunbar4a7e8892010-07-14 18:46:23 +00005497 Args.MakeArgString(getToolChain().GetProgramPath("as"));
Daniel Dunbar2fe238e2010-08-02 02:38:28 +00005498 C.addCommand(new Command(JA, *this, Exec, CmdArgs));
Daniel Dunbar68a31d42009-03-31 17:45:15 +00005499}
Daniel Dunbar008f54a2009-04-01 19:36:32 +00005500
5501void freebsd::Link::ConstructJob(Compilation &C, const JobAction &JA,
Daniel Dunbar2fe238e2010-08-02 02:38:28 +00005502 const InputInfo &Output,
Daniel Dunbarc21c4852009-04-08 23:54:23 +00005503 const InputInfoList &Inputs,
5504 const ArgList &Args,
Daniel Dunbara8304f62009-05-02 20:14:53 +00005505 const char *LinkingOutput) const {
Roman Divacky94380162012-08-28 15:09:03 +00005506 const toolchains::FreeBSD& ToolChain =
5507 static_cast<const toolchains::FreeBSD&>(getToolChain());
5508 const Driver &D = ToolChain.getDriver();
Daniel Dunbar008f54a2009-04-01 19:36:32 +00005509 ArgStringList CmdArgs;
David Chisnalldfa210b2012-07-29 15:24:44 +00005510
5511 // Silence warning for "clang -g foo.o -o foo"
5512 Args.ClaimAllArgs(options::OPT_g_Group);
5513 // and "clang -emit-llvm foo.o -o foo"
5514 Args.ClaimAllArgs(options::OPT_emit_llvm);
5515 // and for "clang -w foo.o -o foo". Other warning options are already
5516 // handled somewhere else.
5517 Args.ClaimAllArgs(options::OPT_w);
Daniel Dunbar008f54a2009-04-01 19:36:32 +00005518
Joerg Sonnenberger8ab2bdc2011-03-21 13:51:29 +00005519 if (!D.SysRoot.empty())
5520 CmdArgs.push_back(Args.MakeArgString("--sysroot=" + D.SysRoot));
5521
Roman Divacky94380162012-08-28 15:09:03 +00005522 if (Args.hasArg(options::OPT_pie))
5523 CmdArgs.push_back("-pie");
5524
Daniel Dunbar008f54a2009-04-01 19:36:32 +00005525 if (Args.hasArg(options::OPT_static)) {
5526 CmdArgs.push_back("-Bstatic");
5527 } else {
Rafael Espindola65ba55d2010-11-11 02:17:51 +00005528 if (Args.hasArg(options::OPT_rdynamic))
5529 CmdArgs.push_back("-export-dynamic");
Daniel Dunbar008f54a2009-04-01 19:36:32 +00005530 CmdArgs.push_back("--eh-frame-hdr");
5531 if (Args.hasArg(options::OPT_shared)) {
5532 CmdArgs.push_back("-Bshareable");
5533 } else {
5534 CmdArgs.push_back("-dynamic-linker");
5535 CmdArgs.push_back("/libexec/ld-elf.so.1");
5536 }
Roman Divacky94380162012-08-28 15:09:03 +00005537 if (ToolChain.getTriple().getOSMajorVersion() >= 9) {
5538 llvm::Triple::ArchType Arch = ToolChain.getArch();
David Chisnalldfa210b2012-07-29 15:24:44 +00005539 if (Arch == llvm::Triple::arm || Arch == llvm::Triple::sparc ||
5540 Arch == llvm::Triple::x86 || Arch == llvm::Triple::x86_64) {
5541 CmdArgs.push_back("--hash-style=both");
5542 }
5543 }
5544 CmdArgs.push_back("--enable-new-dtags");
Daniel Dunbar008f54a2009-04-01 19:36:32 +00005545 }
5546
5547 // When building 32-bit code on FreeBSD/amd64, we have to explicitly
5548 // instruct ld in the base system to link 32-bit code.
Rafael Espindola64f7ad92012-10-07 04:44:33 +00005549 if (ToolChain.getArch() == llvm::Triple::x86) {
Daniel Dunbar008f54a2009-04-01 19:36:32 +00005550 CmdArgs.push_back("-m");
5551 CmdArgs.push_back("elf_i386_fbsd");
5552 }
5553
Rafael Espindola64f7ad92012-10-07 04:44:33 +00005554 if (ToolChain.getArch() == llvm::Triple::ppc) {
Roman Divacky000a6552011-06-04 07:40:24 +00005555 CmdArgs.push_back("-m");
Roman Divacky1052c1d2011-11-21 16:50:32 +00005556 CmdArgs.push_back("elf32ppc_fbsd");
Roman Divacky000a6552011-06-04 07:40:24 +00005557 }
5558
Daniel Dunbar7c1e4652010-08-02 02:38:21 +00005559 if (Output.isFilename()) {
Daniel Dunbar008f54a2009-04-01 19:36:32 +00005560 CmdArgs.push_back("-o");
5561 CmdArgs.push_back(Output.getFilename());
5562 } else {
5563 assert(Output.isNothing() && "Invalid output.");
5564 }
5565
5566 if (!Args.hasArg(options::OPT_nostdlib) &&
5567 !Args.hasArg(options::OPT_nostartfiles)) {
Roman Divacky94380162012-08-28 15:09:03 +00005568 const char *crt1 = NULL;
Daniel Dunbar008f54a2009-04-01 19:36:32 +00005569 if (!Args.hasArg(options::OPT_shared)) {
Roman Divackyc16bb762011-02-10 16:59:40 +00005570 if (Args.hasArg(options::OPT_pg))
Roman Divacky94380162012-08-28 15:09:03 +00005571 crt1 = "gcrt1.o";
5572 else if (Args.hasArg(options::OPT_pie))
5573 crt1 = "Scrt1.o";
5574 else
5575 crt1 = "crt1.o";
Daniel Dunbar008f54a2009-04-01 19:36:32 +00005576 }
Roman Divacky94380162012-08-28 15:09:03 +00005577 if (crt1)
5578 CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath(crt1)));
5579
5580 CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath("crti.o")));
5581
5582 const char *crtbegin = NULL;
5583 if (Args.hasArg(options::OPT_static))
5584 crtbegin = "crtbeginT.o";
5585 else if (Args.hasArg(options::OPT_shared) || Args.hasArg(options::OPT_pie))
5586 crtbegin = "crtbeginS.o";
5587 else
5588 crtbegin = "crtbegin.o";
5589
5590 CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath(crtbegin)));
Daniel Dunbar008f54a2009-04-01 19:36:32 +00005591 }
5592
5593 Args.AddAllArgs(CmdArgs, options::OPT_L);
Roman Divacky94380162012-08-28 15:09:03 +00005594 const ToolChain::path_list Paths = ToolChain.getFilePaths();
Roman Divacky58e5ac92011-03-01 17:53:14 +00005595 for (ToolChain::path_list::const_iterator i = Paths.begin(), e = Paths.end();
5596 i != e; ++i)
Chris Lattner5f9e2722011-07-23 10:55:15 +00005597 CmdArgs.push_back(Args.MakeArgString(StringRef("-L") + *i));
Daniel Dunbar008f54a2009-04-01 19:36:32 +00005598 Args.AddAllArgs(CmdArgs, options::OPT_T_Group);
5599 Args.AddAllArgs(CmdArgs, options::OPT_e);
David Chisnallc7363772010-08-15 22:58:12 +00005600 Args.AddAllArgs(CmdArgs, options::OPT_s);
5601 Args.AddAllArgs(CmdArgs, options::OPT_t);
5602 Args.AddAllArgs(CmdArgs, options::OPT_Z_Flag);
5603 Args.AddAllArgs(CmdArgs, options::OPT_r);
Daniel Dunbar008f54a2009-04-01 19:36:32 +00005604
Roman Divacky94380162012-08-28 15:09:03 +00005605 AddLinkerInputs(ToolChain, Inputs, Args, CmdArgs);
Daniel Dunbar008f54a2009-04-01 19:36:32 +00005606
5607 if (!Args.hasArg(options::OPT_nostdlib) &&
5608 !Args.hasArg(options::OPT_nodefaultlibs)) {
Daniel Dunbar20022632010-02-17 08:07:51 +00005609 if (D.CCCIsCXX) {
Roman Divacky94380162012-08-28 15:09:03 +00005610 ToolChain.AddCXXStdlibLibArgs(Args, CmdArgs);
Roman Divackyc16bb762011-02-10 16:59:40 +00005611 if (Args.hasArg(options::OPT_pg))
5612 CmdArgs.push_back("-lm_p");
5613 else
5614 CmdArgs.push_back("-lm");
Daniel Dunbar20022632010-02-17 08:07:51 +00005615 }
Daniel Dunbar008f54a2009-04-01 19:36:32 +00005616 // FIXME: For some reason GCC passes -lgcc and -lgcc_s before adding
5617 // the default system libraries. Just mimic this for now.
Roman Divackyc16bb762011-02-10 16:59:40 +00005618 if (Args.hasArg(options::OPT_pg))
5619 CmdArgs.push_back("-lgcc_p");
5620 else
5621 CmdArgs.push_back("-lgcc");
Daniel Dunbar008f54a2009-04-01 19:36:32 +00005622 if (Args.hasArg(options::OPT_static)) {
5623 CmdArgs.push_back("-lgcc_eh");
Roman Divackyc16bb762011-02-10 16:59:40 +00005624 } else if (Args.hasArg(options::OPT_pg)) {
5625 CmdArgs.push_back("-lgcc_eh_p");
Daniel Dunbar008f54a2009-04-01 19:36:32 +00005626 } else {
5627 CmdArgs.push_back("--as-needed");
5628 CmdArgs.push_back("-lgcc_s");
5629 CmdArgs.push_back("--no-as-needed");
5630 }
5631
Matt Beaumont-Gay24230262011-02-10 20:35:01 +00005632 if (Args.hasArg(options::OPT_pthread)) {
Roman Divackyc16bb762011-02-10 16:59:40 +00005633 if (Args.hasArg(options::OPT_pg))
5634 CmdArgs.push_back("-lpthread_p");
5635 else
5636 CmdArgs.push_back("-lpthread");
Matt Beaumont-Gay24230262011-02-10 20:35:01 +00005637 }
Daniel Dunbar008f54a2009-04-01 19:36:32 +00005638
Roman Divackyc16bb762011-02-10 16:59:40 +00005639 if (Args.hasArg(options::OPT_pg)) {
5640 if (Args.hasArg(options::OPT_shared))
5641 CmdArgs.push_back("-lc");
5642 else
5643 CmdArgs.push_back("-lc_p");
5644 CmdArgs.push_back("-lgcc_p");
5645 } else {
5646 CmdArgs.push_back("-lc");
5647 CmdArgs.push_back("-lgcc");
5648 }
5649
Daniel Dunbar008f54a2009-04-01 19:36:32 +00005650 if (Args.hasArg(options::OPT_static)) {
5651 CmdArgs.push_back("-lgcc_eh");
Roman Divackyc16bb762011-02-10 16:59:40 +00005652 } else if (Args.hasArg(options::OPT_pg)) {
5653 CmdArgs.push_back("-lgcc_eh_p");
Daniel Dunbar008f54a2009-04-01 19:36:32 +00005654 } else {
5655 CmdArgs.push_back("--as-needed");
5656 CmdArgs.push_back("-lgcc_s");
5657 CmdArgs.push_back("--no-as-needed");
5658 }
5659 }
5660
5661 if (!Args.hasArg(options::OPT_nostdlib) &&
5662 !Args.hasArg(options::OPT_nostartfiles)) {
Roman Divackyf6513812012-09-07 13:36:21 +00005663 if (Args.hasArg(options::OPT_shared) || Args.hasArg(options::OPT_pie))
Roman Divacky94380162012-08-28 15:09:03 +00005664 CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath("crtendS.o")));
Roman Divackyf6513812012-09-07 13:36:21 +00005665 else
5666 CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath("crtend.o")));
Roman Divacky94380162012-08-28 15:09:03 +00005667 CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath("crtn.o")));
Daniel Dunbar008f54a2009-04-01 19:36:32 +00005668 }
5669
Roman Divacky94380162012-08-28 15:09:03 +00005670 addProfileRT(ToolChain, Args, CmdArgs, ToolChain.getTriple());
Nick Lewycky2e95a6d2011-05-24 21:54:59 +00005671
Daniel Dunbarc21c4852009-04-08 23:54:23 +00005672 const char *Exec =
Roman Divacky94380162012-08-28 15:09:03 +00005673 Args.MakeArgString(ToolChain.GetProgramPath("ld"));
Daniel Dunbar2fe238e2010-08-02 02:38:28 +00005674 C.addCommand(new Command(JA, *this, Exec, CmdArgs));
Daniel Dunbar008f54a2009-04-01 19:36:32 +00005675}
Daniel Dunbar11e1b402009-05-02 18:28:39 +00005676
Benjamin Kramer8e50a962011-02-02 18:59:27 +00005677void netbsd::Assemble::ConstructJob(Compilation &C, const JobAction &JA,
5678 const InputInfo &Output,
5679 const InputInfoList &Inputs,
5680 const ArgList &Args,
5681 const char *LinkingOutput) const {
5682 ArgStringList CmdArgs;
5683
5684 // When building 32-bit code on NetBSD/amd64, we have to explicitly
5685 // instruct as in the base system to assemble 32-bit code.
Joerg Sonnenberger1bd91372012-01-26 22:27:52 +00005686 if (getToolChain().getArch() == llvm::Triple::x86)
Benjamin Kramer8e50a962011-02-02 18:59:27 +00005687 CmdArgs.push_back("--32");
5688
Benjamin Kramer8e50a962011-02-02 18:59:27 +00005689 // Set byte order explicitly
Rafael Espindola64f7ad92012-10-07 04:44:33 +00005690 if (getToolChain().getArch() == llvm::Triple::mips)
Benjamin Kramer8e50a962011-02-02 18:59:27 +00005691 CmdArgs.push_back("-EB");
Rafael Espindola64f7ad92012-10-07 04:44:33 +00005692 else if (getToolChain().getArch() == llvm::Triple::mipsel)
Benjamin Kramer8e50a962011-02-02 18:59:27 +00005693 CmdArgs.push_back("-EL");
5694
5695 Args.AddAllArgValues(CmdArgs, options::OPT_Wa_COMMA,
5696 options::OPT_Xassembler);
5697
5698 CmdArgs.push_back("-o");
5699 CmdArgs.push_back(Output.getFilename());
5700
5701 for (InputInfoList::const_iterator
5702 it = Inputs.begin(), ie = Inputs.end(); it != ie; ++it) {
5703 const InputInfo &II = *it;
5704 CmdArgs.push_back(II.getFilename());
5705 }
5706
David Chisnall5adcec12011-09-27 22:03:18 +00005707 const char *Exec = Args.MakeArgString((getToolChain().GetProgramPath("as")));
Benjamin Kramer8e50a962011-02-02 18:59:27 +00005708 C.addCommand(new Command(JA, *this, Exec, CmdArgs));
5709}
5710
5711void netbsd::Link::ConstructJob(Compilation &C, const JobAction &JA,
5712 const InputInfo &Output,
5713 const InputInfoList &Inputs,
5714 const ArgList &Args,
5715 const char *LinkingOutput) const {
5716 const Driver &D = getToolChain().getDriver();
5717 ArgStringList CmdArgs;
5718
Joerg Sonnenberger8ab2bdc2011-03-21 13:51:29 +00005719 if (!D.SysRoot.empty())
5720 CmdArgs.push_back(Args.MakeArgString("--sysroot=" + D.SysRoot));
5721
Benjamin Kramer8e50a962011-02-02 18:59:27 +00005722 if (Args.hasArg(options::OPT_static)) {
5723 CmdArgs.push_back("-Bstatic");
5724 } else {
5725 if (Args.hasArg(options::OPT_rdynamic))
5726 CmdArgs.push_back("-export-dynamic");
5727 CmdArgs.push_back("--eh-frame-hdr");
5728 if (Args.hasArg(options::OPT_shared)) {
5729 CmdArgs.push_back("-Bshareable");
5730 } else {
5731 CmdArgs.push_back("-dynamic-linker");
5732 CmdArgs.push_back("/libexec/ld.elf_so");
5733 }
5734 }
5735
5736 // When building 32-bit code on NetBSD/amd64, we have to explicitly
5737 // instruct ld in the base system to link 32-bit code.
Joerg Sonnenberger1bd91372012-01-26 22:27:52 +00005738 if (getToolChain().getArch() == llvm::Triple::x86) {
Benjamin Kramer8e50a962011-02-02 18:59:27 +00005739 CmdArgs.push_back("-m");
5740 CmdArgs.push_back("elf_i386");
5741 }
5742
5743 if (Output.isFilename()) {
5744 CmdArgs.push_back("-o");
5745 CmdArgs.push_back(Output.getFilename());
5746 } else {
5747 assert(Output.isNothing() && "Invalid output.");
5748 }
5749
5750 if (!Args.hasArg(options::OPT_nostdlib) &&
5751 !Args.hasArg(options::OPT_nostartfiles)) {
5752 if (!Args.hasArg(options::OPT_shared)) {
5753 CmdArgs.push_back(Args.MakeArgString(
5754 getToolChain().GetFilePath("crt0.o")));
5755 CmdArgs.push_back(Args.MakeArgString(
5756 getToolChain().GetFilePath("crti.o")));
5757 CmdArgs.push_back(Args.MakeArgString(
5758 getToolChain().GetFilePath("crtbegin.o")));
5759 } else {
5760 CmdArgs.push_back(Args.MakeArgString(
5761 getToolChain().GetFilePath("crti.o")));
5762 CmdArgs.push_back(Args.MakeArgString(
5763 getToolChain().GetFilePath("crtbeginS.o")));
5764 }
5765 }
5766
5767 Args.AddAllArgs(CmdArgs, options::OPT_L);
5768 Args.AddAllArgs(CmdArgs, options::OPT_T_Group);
5769 Args.AddAllArgs(CmdArgs, options::OPT_e);
5770 Args.AddAllArgs(CmdArgs, options::OPT_s);
5771 Args.AddAllArgs(CmdArgs, options::OPT_t);
5772 Args.AddAllArgs(CmdArgs, options::OPT_Z_Flag);
5773 Args.AddAllArgs(CmdArgs, options::OPT_r);
5774
5775 AddLinkerInputs(getToolChain(), Inputs, Args, CmdArgs);
5776
5777 if (!Args.hasArg(options::OPT_nostdlib) &&
5778 !Args.hasArg(options::OPT_nodefaultlibs)) {
5779 if (D.CCCIsCXX) {
5780 getToolChain().AddCXXStdlibLibArgs(Args, CmdArgs);
5781 CmdArgs.push_back("-lm");
5782 }
5783 // FIXME: For some reason GCC passes -lgcc and -lgcc_s before adding
5784 // the default system libraries. Just mimic this for now.
Benjamin Kramer8e50a962011-02-02 18:59:27 +00005785 if (Args.hasArg(options::OPT_static)) {
5786 CmdArgs.push_back("-lgcc_eh");
5787 } else {
5788 CmdArgs.push_back("--as-needed");
5789 CmdArgs.push_back("-lgcc_s");
5790 CmdArgs.push_back("--no-as-needed");
5791 }
Joerg Sonnenbergerdb6393f2011-06-07 23:39:17 +00005792 CmdArgs.push_back("-lgcc");
Benjamin Kramer8e50a962011-02-02 18:59:27 +00005793
5794 if (Args.hasArg(options::OPT_pthread))
5795 CmdArgs.push_back("-lpthread");
5796 CmdArgs.push_back("-lc");
5797
5798 CmdArgs.push_back("-lgcc");
5799 if (Args.hasArg(options::OPT_static)) {
5800 CmdArgs.push_back("-lgcc_eh");
5801 } else {
5802 CmdArgs.push_back("--as-needed");
5803 CmdArgs.push_back("-lgcc_s");
5804 CmdArgs.push_back("--no-as-needed");
5805 }
5806 }
5807
5808 if (!Args.hasArg(options::OPT_nostdlib) &&
5809 !Args.hasArg(options::OPT_nostartfiles)) {
5810 if (!Args.hasArg(options::OPT_shared))
5811 CmdArgs.push_back(Args.MakeArgString(getToolChain().GetFilePath(
5812 "crtend.o")));
5813 else
5814 CmdArgs.push_back(Args.MakeArgString(getToolChain().GetFilePath(
5815 "crtendS.o")));
5816 CmdArgs.push_back(Args.MakeArgString(getToolChain().GetFilePath(
5817 "crtn.o")));
5818 }
5819
Bill Wendling3f4be6f2011-06-27 19:15:03 +00005820 addProfileRT(getToolChain(), Args, CmdArgs, getToolChain().getTriple());
Nick Lewycky2e95a6d2011-05-24 21:54:59 +00005821
David Chisnall5adcec12011-09-27 22:03:18 +00005822 const char *Exec = Args.MakeArgString(getToolChain().GetProgramPath("ld"));
Benjamin Kramer8e50a962011-02-02 18:59:27 +00005823 C.addCommand(new Command(JA, *this, Exec, CmdArgs));
5824}
5825
Rafael Espindolaba30bbe2010-08-10 00:25:48 +00005826void linuxtools::Assemble::ConstructJob(Compilation &C, const JobAction &JA,
5827 const InputInfo &Output,
5828 const InputInfoList &Inputs,
5829 const ArgList &Args,
5830 const char *LinkingOutput) const {
5831 ArgStringList CmdArgs;
5832
5833 // Add --32/--64 to make sure we get the format we want.
5834 // This is incomplete
5835 if (getToolChain().getArch() == llvm::Triple::x86) {
5836 CmdArgs.push_back("--32");
5837 } else if (getToolChain().getArch() == llvm::Triple::x86_64) {
5838 CmdArgs.push_back("--64");
Eli Friedman7972c882011-11-28 23:46:52 +00005839 } else if (getToolChain().getArch() == llvm::Triple::ppc) {
5840 CmdArgs.push_back("-a32");
5841 CmdArgs.push_back("-mppc");
5842 CmdArgs.push_back("-many");
5843 } else if (getToolChain().getArch() == llvm::Triple::ppc64) {
5844 CmdArgs.push_back("-a64");
5845 CmdArgs.push_back("-mppc64");
5846 CmdArgs.push_back("-many");
Rafael Espindolaba30bbe2010-08-10 00:25:48 +00005847 } else if (getToolChain().getArch() == llvm::Triple::arm) {
Chris Lattner5f9e2722011-07-23 10:55:15 +00005848 StringRef MArch = getToolChain().getArchName();
Rafael Espindolaba30bbe2010-08-10 00:25:48 +00005849 if (MArch == "armv7" || MArch == "armv7a" || MArch == "armv7-a")
5850 CmdArgs.push_back("-mfpu=neon");
Evgeniy Stepanov700c5082012-04-20 09:03:40 +00005851
5852 StringRef ARMFloatABI = getARMFloatABI(getToolChain().getDriver(), Args,
5853 getToolChain().getTriple());
5854 CmdArgs.push_back(Args.MakeArgString("-mfloat-abi=" + ARMFloatABI));
Evgeniy Stepanoveca187e2012-04-24 09:05:31 +00005855
5856 Args.AddLastArg(CmdArgs, options::OPT_march_EQ);
5857 Args.AddLastArg(CmdArgs, options::OPT_mcpu_EQ);
5858 Args.AddLastArg(CmdArgs, options::OPT_mfpu_EQ);
Akira Hatanakac85900f2011-11-30 19:31:38 +00005859 } else if (getToolChain().getArch() == llvm::Triple::mips ||
5860 getToolChain().getArch() == llvm::Triple::mipsel ||
5861 getToolChain().getArch() == llvm::Triple::mips64 ||
5862 getToolChain().getArch() == llvm::Triple::mips64el) {
Simon Atanasyan073a7802012-04-07 22:31:29 +00005863 StringRef CPUName;
5864 StringRef ABIName;
5865 getMipsCPUAndABI(Args, getToolChain(), CPUName, ABIName);
Akira Hatanakac85900f2011-11-30 19:31:38 +00005866
Simon Atanasyan073a7802012-04-07 22:31:29 +00005867 CmdArgs.push_back("-march");
5868 CmdArgs.push_back(CPUName.data());
5869
5870 // Convert ABI name to the GNU tools acceptable variant.
5871 if (ABIName == "o32")
5872 ABIName = "32";
5873 else if (ABIName == "n64")
5874 ABIName = "64";
5875
5876 CmdArgs.push_back("-mabi");
5877 CmdArgs.push_back(ABIName.data());
Simon Atanasyan5f0a1c12012-04-06 19:15:24 +00005878
5879 if (getToolChain().getArch() == llvm::Triple::mips ||
5880 getToolChain().getArch() == llvm::Triple::mips64)
5881 CmdArgs.push_back("-EB");
5882 else
5883 CmdArgs.push_back("-EL");
Simon Atanasyan1f0646e2012-05-29 19:07:33 +00005884
5885 Arg *LastPICArg = Args.getLastArg(options::OPT_fPIC, options::OPT_fno_PIC,
5886 options::OPT_fpic, options::OPT_fno_pic,
5887 options::OPT_fPIE, options::OPT_fno_PIE,
5888 options::OPT_fpie, options::OPT_fno_pie);
5889 if (LastPICArg &&
5890 (LastPICArg->getOption().matches(options::OPT_fPIC) ||
5891 LastPICArg->getOption().matches(options::OPT_fpic) ||
5892 LastPICArg->getOption().matches(options::OPT_fPIE) ||
5893 LastPICArg->getOption().matches(options::OPT_fpie))) {
5894 CmdArgs.push_back("-KPIC");
5895 }
Rafael Espindolaba30bbe2010-08-10 00:25:48 +00005896 }
5897
5898 Args.AddAllArgValues(CmdArgs, options::OPT_Wa_COMMA,
5899 options::OPT_Xassembler);
5900
5901 CmdArgs.push_back("-o");
5902 CmdArgs.push_back(Output.getFilename());
5903
5904 for (InputInfoList::const_iterator
5905 it = Inputs.begin(), ie = Inputs.end(); it != ie; ++it) {
5906 const InputInfo &II = *it;
5907 CmdArgs.push_back(II.getFilename());
5908 }
5909
5910 const char *Exec =
5911 Args.MakeArgString(getToolChain().GetProgramPath("as"));
5912 C.addCommand(new Command(JA, *this, Exec, CmdArgs));
5913}
5914
Evgeniy Stepanova6ddc022012-04-25 08:59:22 +00005915static void AddLibgcc(llvm::Triple Triple, const Driver &D,
5916 ArgStringList &CmdArgs, const ArgList &Args) {
Logan Chien94a71422012-09-02 09:30:11 +00005917 bool isAndroid = Triple.getEnvironment() == llvm::Triple::Android;
Evgeniy Stepanova6ddc022012-04-25 08:59:22 +00005918 bool StaticLibgcc = isAndroid || Args.hasArg(options::OPT_static) ||
Rafael Espindolaabf3ac72011-10-17 21:39:04 +00005919 Args.hasArg(options::OPT_static_libgcc);
5920 if (!D.CCCIsCXX)
5921 CmdArgs.push_back("-lgcc");
5922
5923 if (StaticLibgcc) {
5924 if (D.CCCIsCXX)
5925 CmdArgs.push_back("-lgcc");
5926 } else {
5927 if (!D.CCCIsCXX)
5928 CmdArgs.push_back("--as-needed");
5929 CmdArgs.push_back("-lgcc_s");
5930 if (!D.CCCIsCXX)
5931 CmdArgs.push_back("--no-as-needed");
5932 }
5933
Evgeniy Stepanova6ddc022012-04-25 08:59:22 +00005934 if (StaticLibgcc && !isAndroid)
Rafael Espindolaabf3ac72011-10-17 21:39:04 +00005935 CmdArgs.push_back("-lgcc_eh");
5936 else if (!Args.hasArg(options::OPT_shared) && D.CCCIsCXX)
5937 CmdArgs.push_back("-lgcc");
5938}
5939
Simon Atanasyanf4bd3292012-10-21 11:44:57 +00005940static bool hasMipsN32ABIArg(const ArgList &Args) {
5941 Arg *A = Args.getLastArg(options::OPT_mabi_EQ);
Richard Smith1d489cf2012-11-01 04:30:05 +00005942 return A && (A->getValue() == StringRef("n32"));
Simon Atanasyanf4bd3292012-10-21 11:44:57 +00005943}
5944
Rafael Espindolac1da9812010-11-07 20:14:31 +00005945void linuxtools::Link::ConstructJob(Compilation &C, const JobAction &JA,
5946 const InputInfo &Output,
5947 const InputInfoList &Inputs,
5948 const ArgList &Args,
5949 const char *LinkingOutput) const {
5950 const toolchains::Linux& ToolChain =
5951 static_cast<const toolchains::Linux&>(getToolChain());
5952 const Driver &D = ToolChain.getDriver();
Rafael Espindola715852c2012-11-02 20:41:30 +00005953 const bool isAndroid =
5954 ToolChain.getTriple().getEnvironment() == llvm::Triple::Android;
Evgeniy Stepanova6ddc022012-04-25 08:59:22 +00005955
Rafael Espindolac1da9812010-11-07 20:14:31 +00005956 ArgStringList CmdArgs;
5957
Rafael Espindola26f14c32010-11-15 18:28:16 +00005958 // Silence warning for "clang -g foo.o -o foo"
5959 Args.ClaimAllArgs(options::OPT_g_Group);
Rafael Espindola9c094fb2011-03-01 05:25:27 +00005960 // and "clang -emit-llvm foo.o -o foo"
5961 Args.ClaimAllArgs(options::OPT_emit_llvm);
David Chisnalldfa210b2012-07-29 15:24:44 +00005962 // and for "clang -w foo.o -o foo". Other warning options are already
Rafael Espindola7f6458b2010-11-17 20:37:10 +00005963 // handled somewhere else.
5964 Args.ClaimAllArgs(options::OPT_w);
Rafael Espindola26f14c32010-11-15 18:28:16 +00005965
Joerg Sonnenberger8ab2bdc2011-03-21 13:51:29 +00005966 if (!D.SysRoot.empty())
5967 CmdArgs.push_back(Args.MakeArgString("--sysroot=" + D.SysRoot));
Rafael Espindolac1da9812010-11-07 20:14:31 +00005968
Rafael Espindolafdda1712010-11-17 22:26:15 +00005969 if (Args.hasArg(options::OPT_pie))
5970 CmdArgs.push_back("-pie");
5971
Rafael Espindoladc1b76d2010-11-07 22:57:16 +00005972 if (Args.hasArg(options::OPT_rdynamic))
5973 CmdArgs.push_back("-export-dynamic");
5974
Rafael Espindolae0e6d3b2010-11-11 19:34:42 +00005975 if (Args.hasArg(options::OPT_s))
5976 CmdArgs.push_back("-s");
5977
Rafael Espindolac1da9812010-11-07 20:14:31 +00005978 for (std::vector<std::string>::const_iterator i = ToolChain.ExtraOpts.begin(),
5979 e = ToolChain.ExtraOpts.end();
5980 i != e; ++i)
5981 CmdArgs.push_back(i->c_str());
5982
5983 if (!Args.hasArg(options::OPT_static)) {
5984 CmdArgs.push_back("--eh-frame-hdr");
5985 }
5986
5987 CmdArgs.push_back("-m");
5988 if (ToolChain.getArch() == llvm::Triple::x86)
5989 CmdArgs.push_back("elf_i386");
Eric Christopher88b7cf02011-08-19 00:30:14 +00005990 else if (ToolChain.getArch() == llvm::Triple::arm
Douglas Gregorf0594d82011-03-06 19:11:49 +00005991 || ToolChain.getArch() == llvm::Triple::thumb)
Rafael Espindolac1da9812010-11-07 20:14:31 +00005992 CmdArgs.push_back("armelf_linux_eabi");
Ted Kremenek43ac2972011-04-05 22:04:27 +00005993 else if (ToolChain.getArch() == llvm::Triple::ppc)
5994 CmdArgs.push_back("elf32ppclinux");
5995 else if (ToolChain.getArch() == llvm::Triple::ppc64)
5996 CmdArgs.push_back("elf64ppc");
Eli Friedman5bea4f62011-11-08 19:43:37 +00005997 else if (ToolChain.getArch() == llvm::Triple::mips)
5998 CmdArgs.push_back("elf32btsmip");
5999 else if (ToolChain.getArch() == llvm::Triple::mipsel)
6000 CmdArgs.push_back("elf32ltsmip");
Simon Atanasyanf4bd3292012-10-21 11:44:57 +00006001 else if (ToolChain.getArch() == llvm::Triple::mips64) {
6002 if (hasMipsN32ABIArg(Args))
6003 CmdArgs.push_back("elf32btsmipn32");
6004 else
6005 CmdArgs.push_back("elf64btsmip");
6006 }
6007 else if (ToolChain.getArch() == llvm::Triple::mips64el) {
6008 if (hasMipsN32ABIArg(Args))
6009 CmdArgs.push_back("elf32ltsmipn32");
6010 else
6011 CmdArgs.push_back("elf64ltsmip");
6012 }
Rafael Espindolac1da9812010-11-07 20:14:31 +00006013 else
6014 CmdArgs.push_back("elf_x86_64");
6015
6016 if (Args.hasArg(options::OPT_static)) {
Douglas Gregorf0594d82011-03-06 19:11:49 +00006017 if (ToolChain.getArch() == llvm::Triple::arm
6018 || ToolChain.getArch() == llvm::Triple::thumb)
Rafael Espindolac1da9812010-11-07 20:14:31 +00006019 CmdArgs.push_back("-Bstatic");
6020 else
6021 CmdArgs.push_back("-static");
6022 } else if (Args.hasArg(options::OPT_shared)) {
6023 CmdArgs.push_back("-shared");
Rafael Espindola715852c2012-11-02 20:41:30 +00006024 if (isAndroid) {
Evgeniy Stepanova6ddc022012-04-25 08:59:22 +00006025 CmdArgs.push_back("-Bsymbolic");
6026 }
Rafael Espindolac1da9812010-11-07 20:14:31 +00006027 }
6028
6029 if (ToolChain.getArch() == llvm::Triple::arm ||
Douglas Gregorf0594d82011-03-06 19:11:49 +00006030 ToolChain.getArch() == llvm::Triple::thumb ||
Rafael Espindolac1da9812010-11-07 20:14:31 +00006031 (!Args.hasArg(options::OPT_static) &&
6032 !Args.hasArg(options::OPT_shared))) {
6033 CmdArgs.push_back("-dynamic-linker");
Evgeniy Stepanova6ddc022012-04-25 08:59:22 +00006034 if (isAndroid)
6035 CmdArgs.push_back("/system/bin/linker");
6036 else if (ToolChain.getArch() == llvm::Triple::x86)
Rafael Espindolac1da9812010-11-07 20:14:31 +00006037 CmdArgs.push_back("/lib/ld-linux.so.2");
Douglas Gregorf0594d82011-03-06 19:11:49 +00006038 else if (ToolChain.getArch() == llvm::Triple::arm ||
Jiangning Liu6cc9dc82012-07-30 11:05:56 +00006039 ToolChain.getArch() == llvm::Triple::thumb) {
6040 if (ToolChain.getTriple().getEnvironment() == llvm::Triple::GNUEABIHF)
6041 CmdArgs.push_back("/lib/ld-linux-armhf.so.3");
6042 else
6043 CmdArgs.push_back("/lib/ld-linux.so.3");
6044 }
Eli Friedman5bea4f62011-11-08 19:43:37 +00006045 else if (ToolChain.getArch() == llvm::Triple::mips ||
6046 ToolChain.getArch() == llvm::Triple::mipsel)
6047 CmdArgs.push_back("/lib/ld.so.1");
Simon Atanasyan8491cb22012-04-06 20:14:27 +00006048 else if (ToolChain.getArch() == llvm::Triple::mips64 ||
Simon Atanasyanf4bd3292012-10-21 11:44:57 +00006049 ToolChain.getArch() == llvm::Triple::mips64el) {
6050 if (hasMipsN32ABIArg(Args))
6051 CmdArgs.push_back("/lib32/ld.so.1");
6052 else
6053 CmdArgs.push_back("/lib64/ld.so.1");
6054 }
Ted Kremenek43ac2972011-04-05 22:04:27 +00006055 else if (ToolChain.getArch() == llvm::Triple::ppc)
Chris Lattner09f43ed2011-04-11 21:15:37 +00006056 CmdArgs.push_back("/lib/ld.so.1");
Ted Kremenek43ac2972011-04-05 22:04:27 +00006057 else if (ToolChain.getArch() == llvm::Triple::ppc64)
Chris Lattner09f43ed2011-04-11 21:15:37 +00006058 CmdArgs.push_back("/lib64/ld64.so.1");
Rafael Espindolac1da9812010-11-07 20:14:31 +00006059 else
6060 CmdArgs.push_back("/lib64/ld-linux-x86-64.so.2");
6061 }
6062
6063 CmdArgs.push_back("-o");
6064 CmdArgs.push_back(Output.getFilename());
6065
Rafael Espindola49c64fd2010-12-01 01:52:43 +00006066 if (!Args.hasArg(options::OPT_nostdlib) &&
6067 !Args.hasArg(options::OPT_nostartfiles)) {
Evgeniy Stepanova6ddc022012-04-25 08:59:22 +00006068 if (!isAndroid) {
6069 const char *crt1 = NULL;
6070 if (!Args.hasArg(options::OPT_shared)){
6071 if (Args.hasArg(options::OPT_pie))
6072 crt1 = "Scrt1.o";
6073 else
6074 crt1 = "crt1.o";
6075 }
6076 if (crt1)
6077 CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath(crt1)));
Rafael Espindolac1da9812010-11-07 20:14:31 +00006078
Evgeniy Stepanova6ddc022012-04-25 08:59:22 +00006079 CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath("crti.o")));
6080 }
Rafael Espindolac1da9812010-11-07 20:14:31 +00006081
Rafael Espindola89414b32010-11-12 03:00:39 +00006082 const char *crtbegin;
6083 if (Args.hasArg(options::OPT_static))
Evgeniy Stepanova6ddc022012-04-25 08:59:22 +00006084 crtbegin = isAndroid ? "crtbegin_static.o" : "crtbeginT.o";
Evgeniy Stepanova92983d2012-09-10 10:30:12 +00006085 else if (Args.hasArg(options::OPT_shared))
Evgeniy Stepanova6ddc022012-04-25 08:59:22 +00006086 crtbegin = isAndroid ? "crtbegin_so.o" : "crtbeginS.o";
Evgeniy Stepanova92983d2012-09-10 10:30:12 +00006087 else if (Args.hasArg(options::OPT_pie))
6088 crtbegin = isAndroid ? "crtbegin_dynamic.o" : "crtbeginS.o";
Rafael Espindola89414b32010-11-12 03:00:39 +00006089 else
Evgeniy Stepanova6ddc022012-04-25 08:59:22 +00006090 crtbegin = isAndroid ? "crtbegin_dynamic.o" : "crtbegin.o";
Rafael Espindola89414b32010-11-12 03:00:39 +00006091 CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath(crtbegin)));
Benjamin Kramere20e5082012-10-04 19:42:20 +00006092
6093 // Add crtfastmath.o if available and fast math is enabled.
6094 ToolChain.AddFastMathRuntimeIfAvailable(Args, CmdArgs);
Rafael Espindola89414b32010-11-12 03:00:39 +00006095 }
Rafael Espindolac1da9812010-11-07 20:14:31 +00006096
6097 Args.AddAllArgs(CmdArgs, options::OPT_L);
6098
6099 const ToolChain::path_list Paths = ToolChain.getFilePaths();
6100
Roman Divacky58e5ac92011-03-01 17:53:14 +00006101 for (ToolChain::path_list::const_iterator i = Paths.begin(), e = Paths.end();
6102 i != e; ++i)
Chris Lattner5f9e2722011-07-23 10:55:15 +00006103 CmdArgs.push_back(Args.MakeArgString(StringRef("-L") + *i));
Rafael Espindolac1da9812010-11-07 20:14:31 +00006104
Rafael Espindolac5151542012-04-09 23:53:34 +00006105 // Tell the linker to load the plugin. This has to come before AddLinkerInputs
6106 // as gold requires -plugin to come before any -plugin-opt that -Wl might
6107 // forward.
6108 if (D.IsUsingLTO(Args) || Args.hasArg(options::OPT_use_gold_plugin)) {
6109 CmdArgs.push_back("-plugin");
6110 std::string Plugin = ToolChain.getDriver().Dir + "/../lib/LLVMgold.so";
6111 CmdArgs.push_back(Args.MakeArgString(Plugin));
6112 }
6113
Nick Lewyckye276cfc2012-08-17 03:39:16 +00006114 if (Args.hasArg(options::OPT_Z_Xlinker__no_demangle))
6115 CmdArgs.push_back("--no-demangle");
6116
Rafael Espindolac1da9812010-11-07 20:14:31 +00006117 AddLinkerInputs(ToolChain, Inputs, Args, CmdArgs);
6118
Richard Smithc4dabad2012-11-05 22:04:41 +00006119 SanitizerArgs Sanitize = getSanitizerArgs(D, Args);
6120
Richard Smith8e1cee62012-10-25 02:14:12 +00006121 // Call this before we add the C++ ABI library.
Richard Smithc4dabad2012-11-05 22:04:41 +00006122 if (Sanitize.needsUbsanRt())
6123 addUbsanRTLinux(getToolChain(), Args, CmdArgs);
Richard Smith8e1cee62012-10-25 02:14:12 +00006124
Chandler Carruth2ba542c2012-05-14 18:31:18 +00006125 if (D.CCCIsCXX &&
6126 !Args.hasArg(options::OPT_nostdlib) &&
6127 !Args.hasArg(options::OPT_nodefaultlibs)) {
Rafael Espindola19706f82011-10-17 22:14:51 +00006128 bool OnlyLibstdcxxStatic = Args.hasArg(options::OPT_static_libstdcxx) &&
6129 !Args.hasArg(options::OPT_static);
6130 if (OnlyLibstdcxxStatic)
6131 CmdArgs.push_back("-Bstatic");
Rafael Espindolac1da9812010-11-07 20:14:31 +00006132 ToolChain.AddCXXStdlibLibArgs(Args, CmdArgs);
Rafael Espindola19706f82011-10-17 22:14:51 +00006133 if (OnlyLibstdcxxStatic)
6134 CmdArgs.push_back("-Bdynamic");
Rafael Espindolac1da9812010-11-07 20:14:31 +00006135 CmdArgs.push_back("-lm");
6136 }
6137
Kostya Serebryanydff466c2011-11-30 01:39:16 +00006138 // Call this before we add the C run-time.
Richard Smithc4dabad2012-11-05 22:04:41 +00006139 if (Sanitize.needsAsanRt())
6140 addAsanRTLinux(getToolChain(), Args, CmdArgs);
6141 if (Sanitize.needsTsanRt())
6142 addTsanRTLinux(getToolChain(), Args, CmdArgs);
Kostya Serebryanydff466c2011-11-30 01:39:16 +00006143
Rafael Espindola89414b32010-11-12 03:00:39 +00006144 if (!Args.hasArg(options::OPT_nostdlib)) {
Chandler Carruth2ba542c2012-05-14 18:31:18 +00006145 if (!Args.hasArg(options::OPT_nodefaultlibs)) {
6146 if (Args.hasArg(options::OPT_static))
6147 CmdArgs.push_back("--start-group");
Nick Lewycky80df0252011-06-04 06:27:06 +00006148
Evgeniy Stepanova6ddc022012-04-25 08:59:22 +00006149 AddLibgcc(ToolChain.getTriple(), D, CmdArgs, Args);
Rafael Espindola89414b32010-11-12 03:00:39 +00006150
Chandler Carruth2ba542c2012-05-14 18:31:18 +00006151 if (Args.hasArg(options::OPT_pthread) ||
6152 Args.hasArg(options::OPT_pthreads))
6153 CmdArgs.push_back("-lpthread");
6154
6155 CmdArgs.push_back("-lc");
6156
6157 if (Args.hasArg(options::OPT_static))
6158 CmdArgs.push_back("--end-group");
6159 else
6160 AddLibgcc(ToolChain.getTriple(), D, CmdArgs, Args);
6161 }
Rafael Espindolafdda1712010-11-17 22:26:15 +00006162
Rafael Espindola49c64fd2010-12-01 01:52:43 +00006163 if (!Args.hasArg(options::OPT_nostartfiles)) {
6164 const char *crtend;
Evgeniy Stepanova92983d2012-09-10 10:30:12 +00006165 if (Args.hasArg(options::OPT_shared))
Evgeniy Stepanova6ddc022012-04-25 08:59:22 +00006166 crtend = isAndroid ? "crtend_so.o" : "crtendS.o";
Evgeniy Stepanova92983d2012-09-10 10:30:12 +00006167 else if (Args.hasArg(options::OPT_pie))
6168 crtend = isAndroid ? "crtend_android.o" : "crtendS.o";
Rafael Espindola49c64fd2010-12-01 01:52:43 +00006169 else
Evgeniy Stepanova6ddc022012-04-25 08:59:22 +00006170 crtend = isAndroid ? "crtend_android.o" : "crtend.o";
Rafael Espindola89414b32010-11-12 03:00:39 +00006171
Rafael Espindola49c64fd2010-12-01 01:52:43 +00006172 CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath(crtend)));
Evgeniy Stepanova6ddc022012-04-25 08:59:22 +00006173 if (!isAndroid)
6174 CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath("crtn.o")));
Rafael Espindola49c64fd2010-12-01 01:52:43 +00006175 }
Rafael Espindolac1da9812010-11-07 20:14:31 +00006176 }
6177
Bill Wendling3f4be6f2011-06-27 19:15:03 +00006178 addProfileRT(getToolChain(), Args, CmdArgs, getToolChain().getTriple());
Nick Lewycky2e95a6d2011-05-24 21:54:59 +00006179
Rafael Espindolac1da9812010-11-07 20:14:31 +00006180 C.addCommand(new Command(JA, *this, ToolChain.Linker.c_str(), CmdArgs));
6181}
Rafael Espindolaba30bbe2010-08-10 00:25:48 +00006182
Chris Lattner38e317d2010-07-07 16:01:42 +00006183void minix::Assemble::ConstructJob(Compilation &C, const JobAction &JA,
Daniel Dunbar2fe238e2010-08-02 02:38:28 +00006184 const InputInfo &Output,
6185 const InputInfoList &Inputs,
6186 const ArgList &Args,
6187 const char *LinkingOutput) const {
Chris Lattner38e317d2010-07-07 16:01:42 +00006188 ArgStringList CmdArgs;
6189
6190 Args.AddAllArgValues(CmdArgs, options::OPT_Wa_COMMA,
6191 options::OPT_Xassembler);
6192
6193 CmdArgs.push_back("-o");
Daniel Dunbar7c1e4652010-08-02 02:38:21 +00006194 CmdArgs.push_back(Output.getFilename());
Chris Lattner38e317d2010-07-07 16:01:42 +00006195
6196 for (InputInfoList::const_iterator
6197 it = Inputs.begin(), ie = Inputs.end(); it != ie; ++it) {
6198 const InputInfo &II = *it;
Daniel Dunbar7c1e4652010-08-02 02:38:21 +00006199 CmdArgs.push_back(II.getFilename());
Chris Lattner38e317d2010-07-07 16:01:42 +00006200 }
6201
6202 const char *Exec =
Eli Friedman6d402dc2011-12-08 23:54:21 +00006203 Args.MakeArgString(getToolChain().GetProgramPath("as"));
Daniel Dunbar2fe238e2010-08-02 02:38:28 +00006204 C.addCommand(new Command(JA, *this, Exec, CmdArgs));
Chris Lattner38e317d2010-07-07 16:01:42 +00006205}
6206
6207void minix::Link::ConstructJob(Compilation &C, const JobAction &JA,
Daniel Dunbar2fe238e2010-08-02 02:38:28 +00006208 const InputInfo &Output,
6209 const InputInfoList &Inputs,
6210 const ArgList &Args,
6211 const char *LinkingOutput) const {
Chris Lattner38e317d2010-07-07 16:01:42 +00006212 const Driver &D = getToolChain().getDriver();
6213 ArgStringList CmdArgs;
6214
Daniel Dunbar7c1e4652010-08-02 02:38:21 +00006215 if (Output.isFilename()) {
Chris Lattner38e317d2010-07-07 16:01:42 +00006216 CmdArgs.push_back("-o");
6217 CmdArgs.push_back(Output.getFilename());
6218 } else {
6219 assert(Output.isNothing() && "Invalid output.");
6220 }
6221
6222 if (!Args.hasArg(options::OPT_nostdlib) &&
Eli Friedman6d402dc2011-12-08 23:54:21 +00006223 !Args.hasArg(options::OPT_nostartfiles)) {
6224 CmdArgs.push_back(Args.MakeArgString(getToolChain().GetFilePath("crt1.o")));
6225 CmdArgs.push_back(Args.MakeArgString(getToolChain().GetFilePath("crti.o")));
6226 CmdArgs.push_back(Args.MakeArgString(getToolChain().GetFilePath("crtbegin.o")));
6227 CmdArgs.push_back(Args.MakeArgString(getToolChain().GetFilePath("crtn.o")));
6228 }
Chris Lattner38e317d2010-07-07 16:01:42 +00006229
6230 Args.AddAllArgs(CmdArgs, options::OPT_L);
6231 Args.AddAllArgs(CmdArgs, options::OPT_T_Group);
6232 Args.AddAllArgs(CmdArgs, options::OPT_e);
6233
Daniel Dunbar2008fee2010-09-17 00:24:54 +00006234 AddLinkerInputs(getToolChain(), Inputs, Args, CmdArgs);
Chris Lattner38e317d2010-07-07 16:01:42 +00006235
Eli Friedman6d402dc2011-12-08 23:54:21 +00006236 addProfileRT(getToolChain(), Args, CmdArgs, getToolChain().getTriple());
6237
Chris Lattner38e317d2010-07-07 16:01:42 +00006238 if (!Args.hasArg(options::OPT_nostdlib) &&
6239 !Args.hasArg(options::OPT_nodefaultlibs)) {
6240 if (D.CCCIsCXX) {
Daniel Dunbar132e35d2010-09-17 01:20:05 +00006241 getToolChain().AddCXXStdlibLibArgs(Args, CmdArgs);
Chris Lattner38e317d2010-07-07 16:01:42 +00006242 CmdArgs.push_back("-lm");
6243 }
Chris Lattner38e317d2010-07-07 16:01:42 +00006244 }
6245
6246 if (!Args.hasArg(options::OPT_nostdlib) &&
6247 !Args.hasArg(options::OPT_nostartfiles)) {
Eli Friedman6d402dc2011-12-08 23:54:21 +00006248 if (Args.hasArg(options::OPT_pthread))
6249 CmdArgs.push_back("-lpthread");
6250 CmdArgs.push_back("-lc");
6251 CmdArgs.push_back("-lCompilerRT-Generic");
6252 CmdArgs.push_back("-L/usr/pkg/compiler-rt/lib");
6253 CmdArgs.push_back(
6254 Args.MakeArgString(getToolChain().GetFilePath("crtend.o")));
Chris Lattner38e317d2010-07-07 16:01:42 +00006255 }
6256
Eli Friedman6d402dc2011-12-08 23:54:21 +00006257 const char *Exec = Args.MakeArgString(getToolChain().GetProgramPath("ld"));
Daniel Dunbar2fe238e2010-08-02 02:38:28 +00006258 C.addCommand(new Command(JA, *this, Exec, CmdArgs));
Chris Lattner38e317d2010-07-07 16:01:42 +00006259}
6260
Daniel Dunbar11e1b402009-05-02 18:28:39 +00006261/// DragonFly Tools
6262
6263// For now, DragonFly Assemble does just about the same as for
6264// FreeBSD, but this may change soon.
6265void dragonfly::Assemble::ConstructJob(Compilation &C, const JobAction &JA,
Daniel Dunbar2fe238e2010-08-02 02:38:28 +00006266 const InputInfo &Output,
Daniel Dunbar294691e2009-11-04 06:24:38 +00006267 const InputInfoList &Inputs,
6268 const ArgList &Args,
6269 const char *LinkingOutput) const {
Daniel Dunbar11e1b402009-05-02 18:28:39 +00006270 ArgStringList CmdArgs;
6271
6272 // When building 32-bit code on DragonFly/pc64, we have to explicitly
6273 // instruct as in the base system to assemble 32-bit code.
Rafael Espindola64f7ad92012-10-07 04:44:33 +00006274 if (getToolChain().getArch() == llvm::Triple::x86)
Daniel Dunbar11e1b402009-05-02 18:28:39 +00006275 CmdArgs.push_back("--32");
6276
6277 Args.AddAllArgValues(CmdArgs, options::OPT_Wa_COMMA,
6278 options::OPT_Xassembler);
6279
6280 CmdArgs.push_back("-o");
Daniel Dunbar7c1e4652010-08-02 02:38:21 +00006281 CmdArgs.push_back(Output.getFilename());
Daniel Dunbar11e1b402009-05-02 18:28:39 +00006282
6283 for (InputInfoList::const_iterator
6284 it = Inputs.begin(), ie = Inputs.end(); it != ie; ++it) {
6285 const InputInfo &II = *it;
Daniel Dunbar7c1e4652010-08-02 02:38:21 +00006286 CmdArgs.push_back(II.getFilename());
Daniel Dunbar11e1b402009-05-02 18:28:39 +00006287 }
6288
6289 const char *Exec =
Daniel Dunbar4a7e8892010-07-14 18:46:23 +00006290 Args.MakeArgString(getToolChain().GetProgramPath("as"));
Daniel Dunbar2fe238e2010-08-02 02:38:28 +00006291 C.addCommand(new Command(JA, *this, Exec, CmdArgs));
Daniel Dunbar11e1b402009-05-02 18:28:39 +00006292}
6293
6294void dragonfly::Link::ConstructJob(Compilation &C, const JobAction &JA,
Daniel Dunbar2fe238e2010-08-02 02:38:28 +00006295 const InputInfo &Output,
6296 const InputInfoList &Inputs,
6297 const ArgList &Args,
6298 const char *LinkingOutput) const {
Daniel Dunbaree788e72009-12-21 18:54:17 +00006299 const Driver &D = getToolChain().getDriver();
Daniel Dunbar11e1b402009-05-02 18:28:39 +00006300 ArgStringList CmdArgs;
6301
Joerg Sonnenberger8ab2bdc2011-03-21 13:51:29 +00006302 if (!D.SysRoot.empty())
6303 CmdArgs.push_back(Args.MakeArgString("--sysroot=" + D.SysRoot));
6304
Daniel Dunbar11e1b402009-05-02 18:28:39 +00006305 if (Args.hasArg(options::OPT_static)) {
6306 CmdArgs.push_back("-Bstatic");
6307 } else {
6308 if (Args.hasArg(options::OPT_shared))
6309 CmdArgs.push_back("-Bshareable");
6310 else {
6311 CmdArgs.push_back("-dynamic-linker");
6312 CmdArgs.push_back("/usr/libexec/ld-elf.so.2");
6313 }
6314 }
6315
6316 // When building 32-bit code on DragonFly/pc64, we have to explicitly
6317 // instruct ld in the base system to link 32-bit code.
Rafael Espindola64f7ad92012-10-07 04:44:33 +00006318 if (getToolChain().getArch() == llvm::Triple::x86) {
Daniel Dunbar11e1b402009-05-02 18:28:39 +00006319 CmdArgs.push_back("-m");
6320 CmdArgs.push_back("elf_i386");
6321 }
6322
Daniel Dunbar7c1e4652010-08-02 02:38:21 +00006323 if (Output.isFilename()) {
Daniel Dunbar11e1b402009-05-02 18:28:39 +00006324 CmdArgs.push_back("-o");
6325 CmdArgs.push_back(Output.getFilename());
6326 } else {
6327 assert(Output.isNothing() && "Invalid output.");
6328 }
6329
6330 if (!Args.hasArg(options::OPT_nostdlib) &&
6331 !Args.hasArg(options::OPT_nostartfiles)) {
6332 if (!Args.hasArg(options::OPT_shared)) {
Chris Lattner38e317d2010-07-07 16:01:42 +00006333 CmdArgs.push_back(
Daniel Dunbar4a7e8892010-07-14 18:46:23 +00006334 Args.MakeArgString(getToolChain().GetFilePath("crt1.o")));
Chris Lattner38e317d2010-07-07 16:01:42 +00006335 CmdArgs.push_back(
Daniel Dunbar4a7e8892010-07-14 18:46:23 +00006336 Args.MakeArgString(getToolChain().GetFilePath("crti.o")));
Chris Lattner38e317d2010-07-07 16:01:42 +00006337 CmdArgs.push_back(
Daniel Dunbar4a7e8892010-07-14 18:46:23 +00006338 Args.MakeArgString(getToolChain().GetFilePath("crtbegin.o")));
Daniel Dunbar11e1b402009-05-02 18:28:39 +00006339 } else {
Chris Lattner38e317d2010-07-07 16:01:42 +00006340 CmdArgs.push_back(
Daniel Dunbar4a7e8892010-07-14 18:46:23 +00006341 Args.MakeArgString(getToolChain().GetFilePath("crti.o")));
Chris Lattner38e317d2010-07-07 16:01:42 +00006342 CmdArgs.push_back(
Daniel Dunbar4a7e8892010-07-14 18:46:23 +00006343 Args.MakeArgString(getToolChain().GetFilePath("crtbeginS.o")));
Daniel Dunbar11e1b402009-05-02 18:28:39 +00006344 }
6345 }
6346
6347 Args.AddAllArgs(CmdArgs, options::OPT_L);
6348 Args.AddAllArgs(CmdArgs, options::OPT_T_Group);
6349 Args.AddAllArgs(CmdArgs, options::OPT_e);
6350
Daniel Dunbar2008fee2010-09-17 00:24:54 +00006351 AddLinkerInputs(getToolChain(), Inputs, Args, CmdArgs);
Daniel Dunbar11e1b402009-05-02 18:28:39 +00006352
6353 if (!Args.hasArg(options::OPT_nostdlib) &&
6354 !Args.hasArg(options::OPT_nodefaultlibs)) {
6355 // FIXME: GCC passes on -lgcc, -lgcc_pic and a whole lot of
6356 // rpaths
6357 CmdArgs.push_back("-L/usr/lib/gcc41");
6358
6359 if (!Args.hasArg(options::OPT_static)) {
6360 CmdArgs.push_back("-rpath");
6361 CmdArgs.push_back("/usr/lib/gcc41");
6362
6363 CmdArgs.push_back("-rpath-link");
6364 CmdArgs.push_back("/usr/lib/gcc41");
6365
6366 CmdArgs.push_back("-rpath");
6367 CmdArgs.push_back("/usr/lib");
6368
6369 CmdArgs.push_back("-rpath-link");
6370 CmdArgs.push_back("/usr/lib");
6371 }
6372
Rafael Espindola405861d2010-07-20 12:59:03 +00006373 if (D.CCCIsCXX) {
Daniel Dunbar132e35d2010-09-17 01:20:05 +00006374 getToolChain().AddCXXStdlibLibArgs(Args, CmdArgs);
Rafael Espindola405861d2010-07-20 12:59:03 +00006375 CmdArgs.push_back("-lm");
6376 }
6377
Daniel Dunbar11e1b402009-05-02 18:28:39 +00006378 if (Args.hasArg(options::OPT_shared)) {
6379 CmdArgs.push_back("-lgcc_pic");
6380 } else {
6381 CmdArgs.push_back("-lgcc");
6382 }
6383
6384
6385 if (Args.hasArg(options::OPT_pthread))
Mike Stump4d63f8b2009-10-31 20:11:46 +00006386 CmdArgs.push_back("-lpthread");
Daniel Dunbar11e1b402009-05-02 18:28:39 +00006387
6388 if (!Args.hasArg(options::OPT_nolibc)) {
6389 CmdArgs.push_back("-lc");
6390 }
6391
6392 if (Args.hasArg(options::OPT_shared)) {
6393 CmdArgs.push_back("-lgcc_pic");
6394 } else {
6395 CmdArgs.push_back("-lgcc");
6396 }
6397 }
6398
6399 if (!Args.hasArg(options::OPT_nostdlib) &&
6400 !Args.hasArg(options::OPT_nostartfiles)) {
6401 if (!Args.hasArg(options::OPT_shared))
Chris Lattner38e317d2010-07-07 16:01:42 +00006402 CmdArgs.push_back(Args.MakeArgString(
Daniel Dunbar4a7e8892010-07-14 18:46:23 +00006403 getToolChain().GetFilePath("crtend.o")));
Daniel Dunbar11e1b402009-05-02 18:28:39 +00006404 else
Chris Lattner38e317d2010-07-07 16:01:42 +00006405 CmdArgs.push_back(Args.MakeArgString(
Daniel Dunbar4a7e8892010-07-14 18:46:23 +00006406 getToolChain().GetFilePath("crtendS.o")));
Chris Lattner38e317d2010-07-07 16:01:42 +00006407 CmdArgs.push_back(Args.MakeArgString(
Daniel Dunbar4a7e8892010-07-14 18:46:23 +00006408 getToolChain().GetFilePath("crtn.o")));
Daniel Dunbar11e1b402009-05-02 18:28:39 +00006409 }
6410
Bill Wendling3f4be6f2011-06-27 19:15:03 +00006411 addProfileRT(getToolChain(), Args, CmdArgs, getToolChain().getTriple());
Nick Lewycky2e95a6d2011-05-24 21:54:59 +00006412
Daniel Dunbar11e1b402009-05-02 18:28:39 +00006413 const char *Exec =
Daniel Dunbar4a7e8892010-07-14 18:46:23 +00006414 Args.MakeArgString(getToolChain().GetProgramPath("ld"));
Daniel Dunbar2fe238e2010-08-02 02:38:28 +00006415 C.addCommand(new Command(JA, *this, Exec, CmdArgs));
Daniel Dunbar11e1b402009-05-02 18:28:39 +00006416}
Michael J. Spencerff58e362010-08-21 21:55:07 +00006417
6418void visualstudio::Link::ConstructJob(Compilation &C, const JobAction &JA,
6419 const InputInfo &Output,
6420 const InputInfoList &Inputs,
6421 const ArgList &Args,
6422 const char *LinkingOutput) const {
Michael J. Spencerff58e362010-08-21 21:55:07 +00006423 ArgStringList CmdArgs;
6424
6425 if (Output.isFilename()) {
Daniel Dunbare5a37f42010-09-17 00:45:02 +00006426 CmdArgs.push_back(Args.MakeArgString(std::string("-out:") +
6427 Output.getFilename()));
Michael J. Spencerff58e362010-08-21 21:55:07 +00006428 } else {
6429 assert(Output.isNothing() && "Invalid output.");
6430 }
6431
6432 if (!Args.hasArg(options::OPT_nostdlib) &&
6433 !Args.hasArg(options::OPT_nostartfiles)) {
6434 CmdArgs.push_back("-defaultlib:libcmt");
6435 }
6436
6437 CmdArgs.push_back("-nologo");
6438
Michael J. Spencera2284f52012-06-18 16:56:04 +00006439 Args.AddAllArgValues(CmdArgs, options::OPT_l);
6440
6441 // Add filenames immediately.
6442 for (InputInfoList::const_iterator
6443 it = Inputs.begin(), ie = Inputs.end(); it != ie; ++it) {
6444 if (it->isFilename())
6445 CmdArgs.push_back(it->getFilename());
6446 }
Michael J. Spencerff58e362010-08-21 21:55:07 +00006447
6448 const char *Exec =
Daniel Dunbar2008fee2010-09-17 00:24:54 +00006449 Args.MakeArgString(getToolChain().GetProgramPath("link.exe"));
Michael J. Spencerff58e362010-08-21 21:55:07 +00006450 C.addCommand(new Command(JA, *this, Exec, CmdArgs));
6451}