blob: dec2ba5cec5eafdc9dc5f86f726206c784561585 [file] [log] [blame]
Nick Lewyckye47c2452010-09-23 23:48:20 +00001//===--- Tools.cpp - Tools Implementations --------------------------------===//
Daniel Dunbar1a093d22009-03-18 06:00:36 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
Chad Rosiera35d5a32013-04-26 20:49:50 +000010#include <sys/param.h>
11#include <sys/stat.h>
12#include <unistd.h>
Daniel Dunbar1a093d22009-03-18 06:00:36 +000013#include "Tools.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000014#include "InputInfo.h"
15#include "SanitizerArgs.h"
16#include "ToolChains.h"
17#include "clang/Basic/ObjCRuntime.h"
Kevin Enderbyae2ec472013-01-17 21:38:06 +000018#include "clang/Basic/Version.h"
Daniel Dunbara2aedc62009-03-18 10:01:51 +000019#include "clang/Driver/Action.h"
Daniel Dunbar04c4c2c2009-03-18 07:06:02 +000020#include "clang/Driver/Arg.h"
Daniel Dunbara3246a02009-03-18 08:07:30 +000021#include "clang/Driver/ArgList.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000022#include "clang/Driver/Compilation.h"
Daniel Dunbar1a8a2e82009-10-29 02:39:57 +000023#include "clang/Driver/Driver.h"
24#include "clang/Driver/DriverDiagnostic.h"
Daniel Dunbar04c4c2c2009-03-18 07:06:02 +000025#include "clang/Driver/Job.h"
Daniel Dunbara3246a02009-03-18 08:07:30 +000026#include "clang/Driver/Option.h"
Daniel Dunbarda13faf2009-11-19 04:25:22 +000027#include "clang/Driver/Options.h"
Daniel Dunbara3246a02009-03-18 08:07:30 +000028#include "clang/Driver/ToolChain.h"
Daniel Dunbar04c4c2c2009-03-18 07:06:02 +000029#include "clang/Driver/Util.h"
Daniel Dunbar1c28f1e2009-09-09 22:32:48 +000030#include "llvm/ADT/SmallString.h"
Douglas Gregorf7b87cb2009-10-29 00:41:01 +000031#include "llvm/ADT/StringSwitch.h"
Daniel Dunbarb4a3e432009-09-09 22:32:34 +000032#include "llvm/ADT/Twine.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000033#include "llvm/Support/ErrorHandling.h"
Michael J. Spencerf6efe582011-01-10 02:34:13 +000034#include "llvm/Support/FileSystem.h"
Daniel Dunbarc1964212009-03-26 16:23:12 +000035#include "llvm/Support/Format.h"
Michael J. Spencer8aaf4992010-11-29 18:12:39 +000036#include "llvm/Support/Host.h"
37#include "llvm/Support/Process.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000038#include "llvm/Support/raw_ostream.h"
Daniel Dunbar04c4c2c2009-03-18 07:06:02 +000039
Daniel Dunbar1a093d22009-03-18 06:00:36 +000040using namespace clang::driver;
41using namespace clang::driver::tools;
Chris Lattner0e62c1c2011-07-23 10:55:15 +000042using namespace clang;
Daniel Dunbar1a093d22009-03-18 06:00:36 +000043
Daniel Dunbar64198ef2009-09-10 01:21:05 +000044/// CheckPreprocessingOptions - Perform some validation of preprocessing
45/// arguments that is shared with gcc.
46static void CheckPreprocessingOptions(const Driver &D, const ArgList &Args) {
47 if (Arg *A = Args.getLastArg(options::OPT_C, options::OPT_CC))
Joerg Sonnenbergerb86f5f42011-03-06 23:31:01 +000048 if (!Args.hasArg(options::OPT_E) && !D.CCCIsCPP)
Chris Lattner0e62c1c2011-07-23 10:55:15 +000049 D.Diag(diag::err_drv_argument_only_allowed_with)
Daniel Dunbar64198ef2009-09-10 01:21:05 +000050 << A->getAsString(Args) << "-E";
51}
52
Daniel Dunbar4eadb602009-09-10 01:21:12 +000053/// CheckCodeGenerationOptions - Perform some validation of code generation
54/// arguments that is shared with gcc.
55static void CheckCodeGenerationOptions(const Driver &D, const ArgList &Args) {
56 // In gcc, only ARM checks this, but it seems reasonable to check universally.
57 if (Args.hasArg(options::OPT_static))
58 if (const Arg *A = Args.getLastArg(options::OPT_dynamic,
59 options::OPT_mdynamic_no_pic))
Chris Lattner0e62c1c2011-07-23 10:55:15 +000060 D.Diag(diag::err_drv_argument_not_allowed_with)
Daniel Dunbar4eadb602009-09-10 01:21:12 +000061 << A->getAsString(Args) << "-static";
62}
63
Chris Lattnerbf2803f2010-03-29 17:55:58 +000064// Quote target names for inclusion in GNU Make dependency files.
65// Only the characters '$', '#', ' ', '\t' are quoted.
Chris Lattner0e62c1c2011-07-23 10:55:15 +000066static void QuoteTarget(StringRef Target,
67 SmallVectorImpl<char> &Res) {
Chris Lattnerbf2803f2010-03-29 17:55:58 +000068 for (unsigned i = 0, e = Target.size(); i != e; ++i) {
69 switch (Target[i]) {
70 case ' ':
71 case '\t':
72 // Escape the preceding backslashes
73 for (int j = i - 1; j >= 0 && Target[j] == '\\'; --j)
74 Res.push_back('\\');
75
76 // Escape the space/tab
77 Res.push_back('\\');
78 break;
79 case '$':
80 Res.push_back('$');
81 break;
82 case '#':
83 Res.push_back('\\');
84 break;
85 default:
86 break;
87 }
88
89 Res.push_back(Target[i]);
90 }
91}
92
Bill Wendlingc0938f32012-03-12 22:10:06 +000093static void addDirectoryList(const ArgList &Args,
Bill Wendling281ca292012-03-12 21:22:35 +000094 ArgStringList &CmdArgs,
95 const char *ArgName,
Bill Wendlingc0938f32012-03-12 22:10:06 +000096 const char *EnvVar) {
97 const char *DirList = ::getenv(EnvVar);
Chad Rosier616e8a52012-10-30 21:42:09 +000098 bool CombinedArg = false;
99
Bill Wendling281ca292012-03-12 21:22:35 +0000100 if (!DirList)
101 return; // Nothing to do.
102
Chad Rosier616e8a52012-10-30 21:42:09 +0000103 StringRef Name(ArgName);
104 if (Name.equals("-I") || Name.equals("-L"))
105 CombinedArg = true;
106
Bill Wendling281ca292012-03-12 21:22:35 +0000107 StringRef Dirs(DirList);
108 if (Dirs.empty()) // Empty string should not add '.'.
109 return;
110
111 StringRef::size_type Delim;
112 while ((Delim = Dirs.find(llvm::sys::PathSeparator)) != StringRef::npos) {
113 if (Delim == 0) { // Leading colon.
Chad Rosier616e8a52012-10-30 21:42:09 +0000114 if (CombinedArg) {
115 CmdArgs.push_back(Args.MakeArgString(std::string(ArgName) + "."));
116 } else {
117 CmdArgs.push_back(ArgName);
118 CmdArgs.push_back(".");
119 }
Bill Wendling281ca292012-03-12 21:22:35 +0000120 } else {
Chad Rosier616e8a52012-10-30 21:42:09 +0000121 if (CombinedArg) {
122 CmdArgs.push_back(Args.MakeArgString(std::string(ArgName) + Dirs.substr(0, Delim)));
123 } else {
124 CmdArgs.push_back(ArgName);
125 CmdArgs.push_back(Args.MakeArgString(Dirs.substr(0, Delim)));
126 }
Bill Wendling281ca292012-03-12 21:22:35 +0000127 }
Nico Weber89355782012-03-19 15:00:03 +0000128 Dirs = Dirs.substr(Delim + 1);
Bill Wendling281ca292012-03-12 21:22:35 +0000129 }
130
131 if (Dirs.empty()) { // Trailing colon.
Chad Rosier616e8a52012-10-30 21:42:09 +0000132 if (CombinedArg) {
133 CmdArgs.push_back(Args.MakeArgString(std::string(ArgName) + "."));
134 } else {
135 CmdArgs.push_back(ArgName);
136 CmdArgs.push_back(".");
137 }
Bill Wendling281ca292012-03-12 21:22:35 +0000138 } else { // Add the last path.
Chad Rosier616e8a52012-10-30 21:42:09 +0000139 if (CombinedArg) {
140 CmdArgs.push_back(Args.MakeArgString(std::string(ArgName) + Dirs));
141 } else {
142 CmdArgs.push_back(ArgName);
143 CmdArgs.push_back(Args.MakeArgString(Dirs));
144 }
Bill Wendling281ca292012-03-12 21:22:35 +0000145 }
146}
147
Daniel Dunbar54423b22010-09-17 00:24:54 +0000148static void AddLinkerInputs(const ToolChain &TC,
149 const InputInfoList &Inputs, const ArgList &Args,
150 ArgStringList &CmdArgs) {
151 const Driver &D = TC.getDriver();
152
Daniel Dunbar1094bb12011-02-19 05:33:51 +0000153 // Add extra linker input arguments which are not treated as inputs
154 // (constructed via -Xarch_).
155 Args.AddAllArgValues(CmdArgs, options::OPT_Zlinker_input);
156
Daniel Dunbar54423b22010-09-17 00:24:54 +0000157 for (InputInfoList::const_iterator
158 it = Inputs.begin(), ie = Inputs.end(); it != ie; ++it) {
159 const InputInfo &II = *it;
160
161 if (!TC.HasNativeLLVMSupport()) {
162 // Don't try to pass LLVM inputs unless we have native support.
163 if (II.getType() == types::TY_LLVM_IR ||
164 II.getType() == types::TY_LTO_IR ||
165 II.getType() == types::TY_LLVM_BC ||
166 II.getType() == types::TY_LTO_BC)
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000167 D.Diag(diag::err_drv_no_linker_llvm_support)
Daniel Dunbar54423b22010-09-17 00:24:54 +0000168 << TC.getTripleString();
169 }
170
Daniel Dunbar2cc3f172010-09-17 00:45:02 +0000171 // Add filenames immediately.
172 if (II.isFilename()) {
Daniel Dunbar54423b22010-09-17 00:24:54 +0000173 CmdArgs.push_back(II.getFilename());
Daniel Dunbar2cc3f172010-09-17 00:45:02 +0000174 continue;
175 }
176
177 // Otherwise, this is a linker input argument.
178 const Arg &A = II.getInputArg();
179
180 // Handle reserved library options.
181 if (A.getOption().matches(options::OPT_Z_reserved_lib_stdcxx)) {
Daniel Dunbar3f7796f2010-09-17 01:20:05 +0000182 TC.AddCXXStdlibLibArgs(Args, CmdArgs);
Shantonu Senafeb03b2010-09-17 18:39:08 +0000183 } else if (A.getOption().matches(options::OPT_Z_reserved_lib_cckext)) {
184 TC.AddCCKextLibArgs(Args, CmdArgs);
Daniel Dunbar2cc3f172010-09-17 00:45:02 +0000185 } else
186 A.renderAsInput(Args, CmdArgs);
Daniel Dunbar54423b22010-09-17 00:24:54 +0000187 }
Bill Wendling281ca292012-03-12 21:22:35 +0000188
189 // LIBRARY_PATH - included following the user specified library paths.
Bill Wendlingc0938f32012-03-12 22:10:06 +0000190 addDirectoryList(Args, CmdArgs, "-L", "LIBRARY_PATH");
Daniel Dunbar54423b22010-09-17 00:24:54 +0000191}
192
John McCall31168b02011-06-15 23:02:42 +0000193/// \brief Determine whether Objective-C automated reference counting is
194/// enabled.
195static bool isObjCAutoRefCount(const ArgList &Args) {
196 return Args.hasFlag(options::OPT_fobjc_arc, options::OPT_fno_objc_arc, false);
197}
198
Ted Kremeneke65b0862012-03-06 20:05:56 +0000199/// \brief Determine whether we are linking the ObjC runtime.
200static bool isObjCRuntimeLinked(const ArgList &Args) {
Bob Wilson29536fc2012-08-07 19:58:00 +0000201 if (isObjCAutoRefCount(Args)) {
202 Args.ClaimAllArgs(options::OPT_fobjc_link_runtime);
Ted Kremeneke65b0862012-03-06 20:05:56 +0000203 return true;
Bob Wilson29536fc2012-08-07 19:58:00 +0000204 }
Ted Kremeneke65b0862012-03-06 20:05:56 +0000205 return Args.hasArg(options::OPT_fobjc_link_runtime);
206}
207
Rafael Espindola9d4a8cf2011-06-02 18:58:46 +0000208static void addProfileRT(const ToolChain &TC, const ArgList &Args,
Bill Wendling08760582011-06-27 19:15:03 +0000209 ArgStringList &CmdArgs,
210 llvm::Triple Triple) {
211 if (!(Args.hasArg(options::OPT_fprofile_arcs) ||
212 Args.hasArg(options::OPT_fprofile_generate) ||
213 Args.hasArg(options::OPT_fcreate_profile) ||
214 Args.hasArg(options::OPT_coverage)))
215 return;
216
217 // GCC links libgcov.a by adding -L<inst>/gcc/lib/gcc/<triple>/<ver> -lgcov to
218 // the link line. We cannot do the same thing because unlike gcov there is a
219 // libprofile_rt.so. We used to use the -l:libprofile_rt.a syntax, but that is
220 // not supported by old linkers.
Benjamin Kramerf5fbe432011-11-07 16:02:25 +0000221 std::string ProfileRT =
222 std::string(TC.getDriver().Dir) + "/../lib/libprofile_rt.a";
Bill Wendling08760582011-06-27 19:15:03 +0000223
Bill Wendling08760582011-06-27 19:15:03 +0000224 CmdArgs.push_back(Args.MakeArgString(ProfileRT));
Rafael Espindola9d4a8cf2011-06-02 18:58:46 +0000225}
226
Michael J. Spencer66e2b202012-10-19 22:37:06 +0000227static bool forwardToGCC(const Option &O) {
228 return !O.hasFlag(options::NoForward) &&
229 !O.hasFlag(options::DriverOption) &&
230 !O.hasFlag(options::LinkerInput);
231}
232
Peter Collingbourne9b515cb2011-11-06 00:40:05 +0000233void Clang::AddPreprocessingOptions(Compilation &C,
Chad Rosier633dcdc2013-01-24 19:14:47 +0000234 const JobAction &JA,
Peter Collingbourne9b515cb2011-11-06 00:40:05 +0000235 const Driver &D,
Douglas Gregor111af7d2009-04-18 00:34:01 +0000236 const ArgList &Args,
Daniel Dunbard067f7f2009-04-08 23:54:23 +0000237 ArgStringList &CmdArgs,
238 const InputInfo &Output,
239 const InputInfoList &Inputs) const {
Daniel Dunbard067f7f2009-04-08 23:54:23 +0000240 Arg *A;
Daniel Dunbar367dbb92009-06-08 21:48:20 +0000241
Daniel Dunbar64198ef2009-09-10 01:21:05 +0000242 CheckPreprocessingOptions(D, Args);
243
244 Args.AddLastArg(CmdArgs, options::OPT_C);
245 Args.AddLastArg(CmdArgs, options::OPT_CC);
Daniel Dunbar367dbb92009-06-08 21:48:20 +0000246
247 // Handle dependency file generation.
Daniel Dunbar86aed7d2010-12-08 21:33:40 +0000248 if ((A = Args.getLastArg(options::OPT_M, options::OPT_MM)) ||
Daniel Dunbard067f7f2009-04-08 23:54:23 +0000249 (A = Args.getLastArg(options::OPT_MD)) ||
250 (A = Args.getLastArg(options::OPT_MMD))) {
251 // Determine the output location.
252 const char *DepFile;
Benjamin Kramer2715fce2012-09-26 19:01:49 +0000253 if (Arg *MF = Args.getLastArg(options::OPT_MF)) {
Richard Smithbd55daf2012-11-01 04:30:05 +0000254 DepFile = MF->getValue();
Chad Rosier633dcdc2013-01-24 19:14:47 +0000255 C.addFailureResultFile(DepFile, &JA);
Benjamin Kramer2715fce2012-09-26 19:01:49 +0000256 } else if (Output.getType() == types::TY_Dependencies) {
257 DepFile = Output.getFilename();
Daniel Dunbar0bfb21e2009-11-19 03:26:40 +0000258 } else if (A->getOption().matches(options::OPT_M) ||
259 A->getOption().matches(options::OPT_MM)) {
Daniel Dunbard067f7f2009-04-08 23:54:23 +0000260 DepFile = "-";
261 } else {
Bob Wilsondecc03e2012-11-23 06:14:39 +0000262 DepFile = getDependencyFileName(Args, Inputs);
Chad Rosier633dcdc2013-01-24 19:14:47 +0000263 C.addFailureResultFile(DepFile, &JA);
Daniel Dunbard067f7f2009-04-08 23:54:23 +0000264 }
265 CmdArgs.push_back("-dependency-file");
266 CmdArgs.push_back(DepFile);
267
Chris Lattnerbf2803f2010-03-29 17:55:58 +0000268 // Add a default target if one wasn't specified.
Daniel Dunbard067f7f2009-04-08 23:54:23 +0000269 if (!Args.hasArg(options::OPT_MT) && !Args.hasArg(options::OPT_MQ)) {
270 const char *DepTarget;
271
272 // If user provided -o, that is the dependency target, except
273 // when we are only generating a dependency file.
274 Arg *OutputOpt = Args.getLastArg(options::OPT_o);
275 if (OutputOpt && Output.getType() != types::TY_Dependencies) {
Richard Smithbd55daf2012-11-01 04:30:05 +0000276 DepTarget = OutputOpt->getValue();
Daniel Dunbard067f7f2009-04-08 23:54:23 +0000277 } else {
278 // Otherwise derive from the base input.
279 //
280 // FIXME: This should use the computed output file location.
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +0000281 SmallString<128> P(Inputs[0].getBaseInput());
Michael J. Spencere1696752010-12-18 00:19:12 +0000282 llvm::sys::path::replace_extension(P, "o");
283 DepTarget = Args.MakeArgString(llvm::sys::path::filename(P));
Daniel Dunbard067f7f2009-04-08 23:54:23 +0000284 }
285
286 CmdArgs.push_back("-MT");
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +0000287 SmallString<128> Quoted;
Chris Lattnerbf2803f2010-03-29 17:55:58 +0000288 QuoteTarget(DepTarget, Quoted);
289 CmdArgs.push_back(Args.MakeArgString(Quoted));
Daniel Dunbard067f7f2009-04-08 23:54:23 +0000290 }
291
Daniel Dunbar0bfb21e2009-11-19 03:26:40 +0000292 if (A->getOption().matches(options::OPT_M) ||
293 A->getOption().matches(options::OPT_MD))
Daniel Dunbard067f7f2009-04-08 23:54:23 +0000294 CmdArgs.push_back("-sys-header-deps");
295 }
296
Peter Collingbourne77b0e7f22011-07-12 19:35:15 +0000297 if (Args.hasArg(options::OPT_MG)) {
298 if (!A || A->getOption().matches(options::OPT_MD) ||
299 A->getOption().matches(options::OPT_MMD))
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000300 D.Diag(diag::err_drv_mg_requires_m_or_mm);
Peter Collingbourne77b0e7f22011-07-12 19:35:15 +0000301 CmdArgs.push_back("-MG");
302 }
303
Daniel Dunbard067f7f2009-04-08 23:54:23 +0000304 Args.AddLastArg(CmdArgs, options::OPT_MP);
Chris Lattnerbf2803f2010-03-29 17:55:58 +0000305
306 // Convert all -MQ <target> args to -MT <quoted target>
307 for (arg_iterator it = Args.filtered_begin(options::OPT_MT,
308 options::OPT_MQ),
309 ie = Args.filtered_end(); it != ie; ++it) {
Daniel Dunbara442fd52010-06-11 22:00:13 +0000310 const Arg *A = *it;
311 A->claim();
Chris Lattnerbf2803f2010-03-29 17:55:58 +0000312
Daniel Dunbara442fd52010-06-11 22:00:13 +0000313 if (A->getOption().matches(options::OPT_MQ)) {
Chris Lattnerbf2803f2010-03-29 17:55:58 +0000314 CmdArgs.push_back("-MT");
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +0000315 SmallString<128> Quoted;
Richard Smithbd55daf2012-11-01 04:30:05 +0000316 QuoteTarget(A->getValue(), Quoted);
Chris Lattnerbf2803f2010-03-29 17:55:58 +0000317 CmdArgs.push_back(Args.MakeArgString(Quoted));
318
319 // -MT flag - no change
320 } else {
Daniel Dunbara442fd52010-06-11 22:00:13 +0000321 A->render(Args, CmdArgs);
Chris Lattnerbf2803f2010-03-29 17:55:58 +0000322 }
323 }
Daniel Dunbard067f7f2009-04-08 23:54:23 +0000324
Douglas Gregor111af7d2009-04-18 00:34:01 +0000325 // Add -i* options, and automatically translate to
326 // -include-pch/-include-pth for transparent PCH support. It's
327 // wonky, but we include looking for .gch so we can support seamless
328 // replacement into a build system already set up to be generating
329 // .gch files.
Argyrios Kyrtzidis2f23b412010-09-30 16:53:47 +0000330 bool RenderedImplicitInclude = false;
Daniel Dunbar44b36ee2009-11-25 11:53:23 +0000331 for (arg_iterator it = Args.filtered_begin(options::OPT_clang_i_Group),
332 ie = Args.filtered_end(); it != ie; ++it) {
333 const Arg *A = it;
Daniel Dunbard067f7f2009-04-08 23:54:23 +0000334
335 if (A->getOption().matches(options::OPT_include)) {
Argyrios Kyrtzidis2f23b412010-09-30 16:53:47 +0000336 bool IsFirstImplicitInclude = !RenderedImplicitInclude;
337 RenderedImplicitInclude = true;
338
Argyrios Kyrtzidis90bdfbb2010-08-11 23:27:58 +0000339 // Use PCH if the user requested it.
Daniel Dunbarcbc34b72009-10-15 20:02:44 +0000340 bool UsePCH = D.CCCUsePCH;
Daniel Dunbarcbc34b72009-10-15 20:02:44 +0000341
Daniel Dunbard067f7f2009-04-08 23:54:23 +0000342 bool FoundPTH = false;
Douglas Gregor111af7d2009-04-18 00:34:01 +0000343 bool FoundPCH = false;
Richard Smithbd55daf2012-11-01 04:30:05 +0000344 llvm::sys::Path P(A->getValue());
Michael J. Spencerf6efe582011-01-10 02:34:13 +0000345 bool Exists;
Daniel Dunbarcbc34b72009-10-15 20:02:44 +0000346 if (UsePCH) {
Douglas Gregor111af7d2009-04-18 00:34:01 +0000347 P.appendSuffix("pch");
Michael J. Spencerf6efe582011-01-10 02:34:13 +0000348 if (!llvm::sys::fs::exists(P.str(), Exists) && Exists)
Douglas Gregor111af7d2009-04-18 00:34:01 +0000349 FoundPCH = true;
Mike Stump11289f42009-09-09 15:08:12 +0000350 else
Douglas Gregor111af7d2009-04-18 00:34:01 +0000351 P.eraseSuffix();
Daniel Dunbard067f7f2009-04-08 23:54:23 +0000352 }
353
Douglas Gregor111af7d2009-04-18 00:34:01 +0000354 if (!FoundPCH) {
355 P.appendSuffix("pth");
Michael J. Spencerf6efe582011-01-10 02:34:13 +0000356 if (!llvm::sys::fs::exists(P.str(), Exists) && Exists)
Douglas Gregor111af7d2009-04-18 00:34:01 +0000357 FoundPTH = true;
358 else
359 P.eraseSuffix();
Mike Stump11289f42009-09-09 15:08:12 +0000360 }
361
Douglas Gregor111af7d2009-04-18 00:34:01 +0000362 if (!FoundPCH && !FoundPTH) {
363 P.appendSuffix("gch");
Michael J. Spencerf6efe582011-01-10 02:34:13 +0000364 if (!llvm::sys::fs::exists(P.str(), Exists) && Exists) {
Daniel Dunbarcbc34b72009-10-15 20:02:44 +0000365 FoundPCH = UsePCH;
366 FoundPTH = !UsePCH;
Douglas Gregor111af7d2009-04-18 00:34:01 +0000367 }
Mike Stump11289f42009-09-09 15:08:12 +0000368 else
Douglas Gregor111af7d2009-04-18 00:34:01 +0000369 P.eraseSuffix();
370 }
371
372 if (FoundPCH || FoundPTH) {
Argyrios Kyrtzidis2f23b412010-09-30 16:53:47 +0000373 if (IsFirstImplicitInclude) {
374 A->claim();
375 if (UsePCH)
376 CmdArgs.push_back("-include-pch");
377 else
378 CmdArgs.push_back("-include-pth");
379 CmdArgs.push_back(Args.MakeArgString(P.str()));
380 continue;
381 } else {
382 // Ignore the PCH if not first on command line and emit warning.
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000383 D.Diag(diag::warn_drv_pch_not_first_include)
Argyrios Kyrtzidis2f23b412010-09-30 16:53:47 +0000384 << P.str() << A->getAsString(Args);
385 }
Daniel Dunbard067f7f2009-04-08 23:54:23 +0000386 }
387 }
388
389 // Not translated, render as usual.
390 A->claim();
391 A->render(Args, CmdArgs);
392 }
393
394 Args.AddAllArgs(CmdArgs, options::OPT_D, options::OPT_U);
Douglas Gregor9f93e382011-07-28 04:45:53 +0000395 Args.AddAllArgs(CmdArgs, options::OPT_I_Group, options::OPT_F,
396 options::OPT_index_header_map);
Daniel Dunbard067f7f2009-04-08 23:54:23 +0000397
398 // Add -Wp, and -Xassembler if using the preprocessor.
399
400 // FIXME: There is a very unfortunate problem here, some troubled
401 // souls abuse -Wp, to pass preprocessor options in gcc syntax. To
402 // really support that we would have to parse and then translate
403 // those options. :(
404 Args.AddAllArgValues(CmdArgs, options::OPT_Wp_COMMA,
405 options::OPT_Xpreprocessor);
Daniel Dunbar38b62792009-10-29 01:53:44 +0000406
407 // -I- is a deprecated GCC feature, reject it.
408 if (Arg *A = Args.getLastArg(options::OPT_I_))
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000409 D.Diag(diag::err_drv_I_dash_not_supported) << A->getAsString(Args);
Chandler Carruth24e17e12010-10-20 07:00:47 +0000410
411 // If we have a --sysroot, and don't have an explicit -isysroot flag, add an
412 // -isysroot to the CC1 invocation.
Sebastian Pop980920a2012-04-16 04:16:43 +0000413 StringRef sysroot = C.getSysRoot();
414 if (sysroot != "") {
Chandler Carruth24e17e12010-10-20 07:00:47 +0000415 if (!Args.hasArg(options::OPT_isysroot)) {
416 CmdArgs.push_back("-isysroot");
Sebastian Pop980920a2012-04-16 04:16:43 +0000417 CmdArgs.push_back(C.getArgs().MakeArgString(sysroot));
Chandler Carruth24e17e12010-10-20 07:00:47 +0000418 }
419 }
Douglas Gregor5dc38992013-02-07 00:21:12 +0000420
Benjamin Kramer8404eb02011-09-22 21:41:16 +0000421 // Parse additional include paths from environment variables.
Chandler Carruth9802c142011-11-04 07:12:58 +0000422 // FIXME: We should probably sink the logic for handling these from the
423 // frontend into the driver. It will allow deleting 4 otherwise unused flags.
Benjamin Kramer8404eb02011-09-22 21:41:16 +0000424 // CPATH - included following the user specified includes (but prior to
425 // builtin and standard includes).
Bill Wendlingc0938f32012-03-12 22:10:06 +0000426 addDirectoryList(Args, CmdArgs, "-I", "CPATH");
Benjamin Kramer8404eb02011-09-22 21:41:16 +0000427 // C_INCLUDE_PATH - system includes enabled when compiling C.
Bill Wendlingc0938f32012-03-12 22:10:06 +0000428 addDirectoryList(Args, CmdArgs, "-c-isystem", "C_INCLUDE_PATH");
Benjamin Kramer8404eb02011-09-22 21:41:16 +0000429 // CPLUS_INCLUDE_PATH - system includes enabled when compiling C++.
Bill Wendlingc0938f32012-03-12 22:10:06 +0000430 addDirectoryList(Args, CmdArgs, "-cxx-isystem", "CPLUS_INCLUDE_PATH");
Benjamin Kramer8404eb02011-09-22 21:41:16 +0000431 // OBJC_INCLUDE_PATH - system includes enabled when compiling ObjC.
Bill Wendlingc0938f32012-03-12 22:10:06 +0000432 addDirectoryList(Args, CmdArgs, "-objc-isystem", "OBJC_INCLUDE_PATH");
Benjamin Kramer8404eb02011-09-22 21:41:16 +0000433 // OBJCPLUS_INCLUDE_PATH - system includes enabled when compiling ObjC++.
Bill Wendlingc0938f32012-03-12 22:10:06 +0000434 addDirectoryList(Args, CmdArgs, "-objcxx-isystem", "OBJCPLUS_INCLUDE_PATH");
Chandler Carruth6bfd84f2011-11-04 07:12:53 +0000435
Chandler Carruth6bfd84f2011-11-04 07:12:53 +0000436 // Add C++ include arguments, if needed.
Chandler Carruth4c81dfa2011-11-04 07:43:33 +0000437 if (types::isCXX(Inputs[0].getType()))
Chandler Carruth491db322011-11-04 07:34:47 +0000438 getToolChain().AddClangCXXStdlibIncludeArgs(Args, CmdArgs);
Chandler Carrutha796f532011-11-05 20:17:13 +0000439
440 // Add system include arguments.
441 getToolChain().AddClangSystemIncludeArgs(Args, CmdArgs);
Daniel Dunbard067f7f2009-04-08 23:54:23 +0000442}
443
Daniel Dunbarf492c922009-09-10 22:59:51 +0000444/// getLLVMArchSuffixForARM - Get the LLVM arch name to use for a particular
Daniel Dunbarfb58b0a2009-09-10 06:49:20 +0000445/// CPU.
446//
447// FIXME: This is redundant with -mcpu, why does LLVM use this.
448// FIXME: tblgen this, or kill it!
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000449static const char *getLLVMArchSuffixForARM(StringRef CPU) {
Chad Rosier9ac84512011-10-07 17:48:56 +0000450 return llvm::StringSwitch<const char *>(CPU)
451 .Cases("arm7tdmi", "arm7tdmi-s", "arm710t", "v4t")
452 .Cases("arm720t", "arm9", "arm9tdmi", "v4t")
453 .Cases("arm920", "arm920t", "arm922t", "v4t")
454 .Cases("arm940t", "ep9312","v4t")
455 .Cases("arm10tdmi", "arm1020t", "v5")
456 .Cases("arm9e", "arm926ej-s", "arm946e-s", "v5e")
457 .Cases("arm966e-s", "arm968e-s", "arm10e", "v5e")
458 .Cases("arm1020e", "arm1022e", "xscale", "iwmmxt", "v5e")
459 .Cases("arm1136j-s", "arm1136jf-s", "arm1176jz-s", "v6")
460 .Cases("arm1176jzf-s", "mpcorenovfp", "mpcore", "v6")
461 .Cases("arm1156t2-s", "arm1156t2f-s", "v6t2")
Bob Wilsonf643afc2013-03-04 22:37:46 +0000462 .Cases("cortex-a5", "cortex-a7", "cortex-a8", "v7")
463 .Cases("cortex-a9", "cortex-a15", "v7")
Quentin Colombetf5a37a32012-12-21 17:57:47 +0000464 .Case("cortex-r5", "v7r")
Bob Wilson7f05ca32012-03-21 17:19:12 +0000465 .Case("cortex-m0", "v6m")
Bob Wilson743bf672013-03-04 22:37:49 +0000466 .Case("cortex-m3", "v7m")
467 .Case("cortex-m4", "v7em")
Bob Wilsond7cf1042012-09-29 23:52:50 +0000468 .Case("cortex-a9-mp", "v7f")
469 .Case("swift", "v7s")
Chad Rosier9ac84512011-10-07 17:48:56 +0000470 .Default("");
Daniel Dunbarfb58b0a2009-09-10 06:49:20 +0000471}
472
Benjamin Kramer09811c72012-06-26 22:20:06 +0000473/// getARMTargetCPU - Get the (LLVM) name of the ARM cpu we are targeting.
474//
475// FIXME: tblgen this.
476static std::string getARMTargetCPU(const ArgList &Args,
477 const llvm::Triple &Triple) {
478 // FIXME: Warn on inconsistent use of -mcpu and -march.
479
480 // If we have -mcpu=, use that.
481 if (Arg *A = Args.getLastArg(options::OPT_mcpu_EQ)) {
Richard Smithbd55daf2012-11-01 04:30:05 +0000482 StringRef MCPU = A->getValue();
Benjamin Kramer09811c72012-06-26 22:20:06 +0000483 // Handle -mcpu=native.
484 if (MCPU == "native")
485 return llvm::sys::getHostCPUName();
486 else
487 return MCPU;
488 }
489
490 StringRef MArch;
491 if (Arg *A = Args.getLastArg(options::OPT_march_EQ)) {
492 // Otherwise, if we have -march= choose the base CPU for that arch.
Richard Smithbd55daf2012-11-01 04:30:05 +0000493 MArch = A->getValue();
Benjamin Kramer09811c72012-06-26 22:20:06 +0000494 } else {
495 // Otherwise, use the Arch from the triple.
496 MArch = Triple.getArchName();
497 }
498
499 // Handle -march=native.
500 std::string NativeMArch;
501 if (MArch == "native") {
502 std::string CPU = llvm::sys::getHostCPUName();
503 if (CPU != "generic") {
504 // Translate the native cpu into the architecture. The switch below will
505 // then chose the minimum cpu for that arch.
506 NativeMArch = std::string("arm") + getLLVMArchSuffixForARM(CPU);
507 MArch = NativeMArch;
508 }
509 }
510
511 return llvm::StringSwitch<const char *>(MArch)
512 .Cases("armv2", "armv2a","arm2")
513 .Case("armv3", "arm6")
514 .Case("armv3m", "arm7m")
515 .Cases("armv4", "armv4t", "arm7tdmi")
516 .Cases("armv5", "armv5t", "arm10tdmi")
517 .Cases("armv5e", "armv5te", "arm1022e")
518 .Case("armv5tej", "arm926ej-s")
519 .Cases("armv6", "armv6k", "arm1136jf-s")
520 .Case("armv6j", "arm1136j-s")
521 .Cases("armv6z", "armv6zk", "arm1176jzf-s")
522 .Case("armv6t2", "arm1156t2-s")
Bob Wilson743bf672013-03-04 22:37:49 +0000523 .Cases("armv6m", "armv6-m", "cortex-m0")
Benjamin Kramer09811c72012-06-26 22:20:06 +0000524 .Cases("armv7", "armv7a", "armv7-a", "cortex-a8")
Bob Wilson743bf672013-03-04 22:37:49 +0000525 .Cases("armv7em", "armv7e-m", "cortex-m4")
Bob Wilsond7cf1042012-09-29 23:52:50 +0000526 .Cases("armv7f", "armv7-f", "cortex-a9-mp")
527 .Cases("armv7s", "armv7-s", "swift")
Benjamin Kramer09811c72012-06-26 22:20:06 +0000528 .Cases("armv7r", "armv7-r", "cortex-r4")
529 .Cases("armv7m", "armv7-m", "cortex-m3")
530 .Case("ep9312", "ep9312")
531 .Case("iwmmxt", "iwmmxt")
532 .Case("xscale", "xscale")
Benjamin Kramer09811c72012-06-26 22:20:06 +0000533 // If all else failed, return the most base CPU LLVM supports.
534 .Default("arm7tdmi");
535}
536
Daniel Dunbard609b7b2009-11-17 06:37:03 +0000537// FIXME: Move to target hook.
538static bool isSignedCharDefault(const llvm::Triple &Triple) {
539 switch (Triple.getArch()) {
540 default:
541 return true;
542
Tim Northover9bb857a2013-01-31 12:13:10 +0000543 case llvm::Triple::aarch64:
Jim Grosbach7c2c6642011-05-24 15:40:46 +0000544 case llvm::Triple::arm:
Daniel Dunbard609b7b2009-11-17 06:37:03 +0000545 case llvm::Triple::ppc:
546 case llvm::Triple::ppc64:
Bob Wilson6524dd32011-10-14 05:03:44 +0000547 if (Triple.isOSDarwin())
Daniel Dunbard609b7b2009-11-17 06:37:03 +0000548 return true;
549 return false;
Daniel Dunbard609b7b2009-11-17 06:37:03 +0000550 }
551}
552
Chad Rosiercfbfc582012-04-04 20:51:35 +0000553// Handle -mfpu=.
554//
555// FIXME: Centralize feature selection, defaulting shouldn't be also in the
556// frontend target.
557static void addFPUArgs(const Driver &D, const Arg *A, const ArgList &Args,
558 ArgStringList &CmdArgs) {
Richard Smithbd55daf2012-11-01 04:30:05 +0000559 StringRef FPU = A->getValue();
Chad Rosiercfbfc582012-04-04 20:51:35 +0000560
561 // Set the target features based on the FPU.
562 if (FPU == "fpa" || FPU == "fpe2" || FPU == "fpe3" || FPU == "maverick") {
563 // Disable any default FPU support.
564 CmdArgs.push_back("-target-feature");
565 CmdArgs.push_back("-vfp2");
566 CmdArgs.push_back("-target-feature");
567 CmdArgs.push_back("-vfp3");
568 CmdArgs.push_back("-target-feature");
569 CmdArgs.push_back("-neon");
570 } else if (FPU == "vfp3-d16" || FPU == "vfpv3-d16") {
571 CmdArgs.push_back("-target-feature");
572 CmdArgs.push_back("+vfp3");
573 CmdArgs.push_back("-target-feature");
574 CmdArgs.push_back("+d16");
575 CmdArgs.push_back("-target-feature");
576 CmdArgs.push_back("-neon");
577 } else if (FPU == "vfp") {
578 CmdArgs.push_back("-target-feature");
579 CmdArgs.push_back("+vfp2");
580 CmdArgs.push_back("-target-feature");
581 CmdArgs.push_back("-neon");
582 } else if (FPU == "vfp3" || FPU == "vfpv3") {
583 CmdArgs.push_back("-target-feature");
584 CmdArgs.push_back("+vfp3");
585 CmdArgs.push_back("-target-feature");
586 CmdArgs.push_back("-neon");
587 } else if (FPU == "neon") {
588 CmdArgs.push_back("-target-feature");
589 CmdArgs.push_back("+neon");
590 } else
591 D.Diag(diag::err_drv_clang_unsupported) << A->getAsString(Args);
592}
593
Chad Rosier1f0e52e2012-04-04 20:39:32 +0000594// Handle -mfpmath=.
595static void addFPMathArgs(const Driver &D, const Arg *A, const ArgList &Args,
Chad Rosier45619cb2012-04-04 22:13:40 +0000596 ArgStringList &CmdArgs, StringRef CPU) {
Richard Smithbd55daf2012-11-01 04:30:05 +0000597 StringRef FPMath = A->getValue();
Chad Rosier1f0e52e2012-04-04 20:39:32 +0000598
599 // Set the target features based on the FPMath.
600 if (FPMath == "neon") {
601 CmdArgs.push_back("-target-feature");
602 CmdArgs.push_back("+neonfp");
Chad Rosier45619cb2012-04-04 22:13:40 +0000603
Bob Wilsonf643afc2013-03-04 22:37:46 +0000604 if (CPU != "cortex-a5" && CPU != "cortex-a7" &&
605 CPU != "cortex-a8" && CPU != "cortex-a9" &&
606 CPU != "cortex-a9-mp" && CPU != "cortex-a15")
Chad Rosier45619cb2012-04-04 22:13:40 +0000607 D.Diag(diag::err_drv_invalid_feature) << "-mfpmath=neon" << CPU;
608
Chad Rosier1f0e52e2012-04-04 20:39:32 +0000609 } else if (FPMath == "vfp" || FPMath == "vfp2" || FPMath == "vfp3" ||
610 FPMath == "vfp4") {
611 CmdArgs.push_back("-target-feature");
612 CmdArgs.push_back("-neonfp");
Chad Rosier45619cb2012-04-04 22:13:40 +0000613
614 // FIXME: Add warnings when disabling a feature not present for a given CPU.
Chad Rosier1f0e52e2012-04-04 20:39:32 +0000615 } else
616 D.Diag(diag::err_drv_clang_unsupported) << A->getAsString(Args);
617}
618
Anton Korobeynikova29e4622012-04-09 13:38:30 +0000619// Select the float ABI as determined by -msoft-float, -mhard-float, and
620// -mfloat-abi=.
621static StringRef getARMFloatABI(const Driver &D,
622 const ArgList &Args,
623 const llvm::Triple &Triple) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000624 StringRef FloatABI;
Daniel Dunbar78485922009-09-10 23:00:09 +0000625 if (Arg *A = Args.getLastArg(options::OPT_msoft_float,
626 options::OPT_mhard_float,
627 options::OPT_mfloat_abi_EQ)) {
628 if (A->getOption().matches(options::OPT_msoft_float))
629 FloatABI = "soft";
630 else if (A->getOption().matches(options::OPT_mhard_float))
631 FloatABI = "hard";
632 else {
Richard Smithbd55daf2012-11-01 04:30:05 +0000633 FloatABI = A->getValue();
Daniel Dunbar78485922009-09-10 23:00:09 +0000634 if (FloatABI != "soft" && FloatABI != "softfp" && FloatABI != "hard") {
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000635 D.Diag(diag::err_drv_invalid_mfloat_abi)
Daniel Dunbar78485922009-09-10 23:00:09 +0000636 << A->getAsString(Args);
637 FloatABI = "soft";
638 }
639 }
640 }
641
642 // If unspecified, choose the default based on the platform.
643 if (FloatABI.empty()) {
Rafael Espindola0e1fb4f2010-06-28 17:18:09 +0000644 switch (Triple.getOS()) {
Bob Wilson6524dd32011-10-14 05:03:44 +0000645 case llvm::Triple::Darwin:
646 case llvm::Triple::MacOSX:
647 case llvm::Triple::IOS: {
Daniel Dunbar78485922009-09-10 23:00:09 +0000648 // Darwin defaults to "softfp" for v6 and v7.
649 //
650 // FIXME: Factor out an ARM class so we can cache the arch somewhere.
Benjamin Kramer09811c72012-06-26 22:20:06 +0000651 std::string ArchName =
Rafael Espindola0e1fb4f2010-06-28 17:18:09 +0000652 getLLVMArchSuffixForARM(getARMTargetCPU(Args, Triple));
Benjamin Kramer09811c72012-06-26 22:20:06 +0000653 if (StringRef(ArchName).startswith("v6") ||
654 StringRef(ArchName).startswith("v7"))
Daniel Dunbar78485922009-09-10 23:00:09 +0000655 FloatABI = "softfp";
656 else
657 FloatABI = "soft";
658 break;
659 }
660
Rafael Espindola0f207ed2012-12-13 04:17:14 +0000661 case llvm::Triple::FreeBSD:
662 // FreeBSD defaults to soft float
663 FloatABI = "soft";
664 break;
665
Daniel Dunbar78485922009-09-10 23:00:09 +0000666 default:
Bob Wilsond1447c42011-02-04 17:59:28 +0000667 switch(Triple.getEnvironment()) {
Jiangning Liu61b06cb2012-07-31 08:06:29 +0000668 case llvm::Triple::GNUEABIHF:
669 FloatABI = "hard";
670 break;
Bob Wilsond1447c42011-02-04 17:59:28 +0000671 case llvm::Triple::GNUEABI:
672 FloatABI = "softfp";
673 break;
674 case llvm::Triple::EABI:
675 // EABI is always AAPCS, and if it was not marked 'hard', it's softfp
676 FloatABI = "softfp";
677 break;
Logan Chienc6fd8202012-09-02 09:30:11 +0000678 case llvm::Triple::Android: {
Benjamin Kramer09811c72012-06-26 22:20:06 +0000679 std::string ArchName =
Chandler Carruthc89aa9d2012-01-10 19:47:42 +0000680 getLLVMArchSuffixForARM(getARMTargetCPU(Args, Triple));
Benjamin Kramer09811c72012-06-26 22:20:06 +0000681 if (StringRef(ArchName).startswith("v7"))
Chandler Carruthc89aa9d2012-01-10 19:47:42 +0000682 FloatABI = "softfp";
683 else
684 FloatABI = "soft";
685 break;
686 }
Bob Wilsond1447c42011-02-04 17:59:28 +0000687 default:
688 // Assume "soft", but warn the user we are guessing.
689 FloatABI = "soft";
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000690 D.Diag(diag::warn_drv_assuming_mfloat_abi_is) << "soft";
Bob Wilsond1447c42011-02-04 17:59:28 +0000691 break;
692 }
Daniel Dunbar78485922009-09-10 23:00:09 +0000693 }
694 }
695
Anton Korobeynikova29e4622012-04-09 13:38:30 +0000696 return FloatABI;
697}
698
699
700void Clang::AddARMTargetArgs(const ArgList &Args,
701 ArgStringList &CmdArgs,
702 bool KernelOrKext) const {
703 const Driver &D = getToolChain().getDriver();
Daniel Dunbarbd847cc2012-10-15 22:23:53 +0000704 // Get the effective triple, which takes into account the deployment target.
705 std::string TripleStr = getToolChain().ComputeEffectiveClangTriple(Args);
706 llvm::Triple Triple(TripleStr);
Daniel Dunbar5f18f6e2012-10-22 18:30:51 +0000707 std::string CPUName = getARMTargetCPU(Args, Triple);
Anton Korobeynikova29e4622012-04-09 13:38:30 +0000708
709 // Select the ABI to use.
710 //
711 // FIXME: Support -meabi.
712 const char *ABIName = 0;
713 if (Arg *A = Args.getLastArg(options::OPT_mabi_EQ)) {
Richard Smithbd55daf2012-11-01 04:30:05 +0000714 ABIName = A->getValue();
Daniel Dunbar5f18f6e2012-10-22 18:30:51 +0000715 } else if (Triple.isOSDarwin()) {
716 // The backend is hardwired to assume AAPCS for M-class processors, ensure
717 // the frontend matches that.
718 if (StringRef(CPUName).startswith("cortex-m")) {
719 ABIName = "aapcs";
720 } else {
721 ABIName = "apcs-gnu";
722 }
Anton Korobeynikova29e4622012-04-09 13:38:30 +0000723 } else {
724 // Select the default based on the platform.
725 switch(Triple.getEnvironment()) {
Logan Chienc6fd8202012-09-02 09:30:11 +0000726 case llvm::Triple::Android:
Anton Korobeynikova29e4622012-04-09 13:38:30 +0000727 case llvm::Triple::GNUEABI:
Jiangning Liu61b06cb2012-07-31 08:06:29 +0000728 case llvm::Triple::GNUEABIHF:
Anton Korobeynikova29e4622012-04-09 13:38:30 +0000729 ABIName = "aapcs-linux";
730 break;
731 case llvm::Triple::EABI:
732 ABIName = "aapcs";
733 break;
734 default:
735 ABIName = "apcs-gnu";
736 }
737 }
738 CmdArgs.push_back("-target-abi");
739 CmdArgs.push_back(ABIName);
740
741 // Set the CPU based on -march= and -mcpu=.
742 CmdArgs.push_back("-target-cpu");
Daniel Dunbar5f18f6e2012-10-22 18:30:51 +0000743 CmdArgs.push_back(Args.MakeArgString(CPUName));
Anton Korobeynikova29e4622012-04-09 13:38:30 +0000744
745 // Determine floating point ABI from the options & target defaults.
746 StringRef FloatABI = getARMFloatABI(D, Args, Triple);
Daniel Dunbar78485922009-09-10 23:00:09 +0000747 if (FloatABI == "soft") {
748 // Floating point operations and argument passing are soft.
749 //
750 // FIXME: This changes CPP defines, we need -target-soft-float.
Daniel Dunbara74f8ff2009-11-30 08:42:00 +0000751 CmdArgs.push_back("-msoft-float");
Daniel Dunbar6cc525b2009-12-08 19:49:51 +0000752 CmdArgs.push_back("-mfloat-abi");
753 CmdArgs.push_back("soft");
Daniel Dunbar78485922009-09-10 23:00:09 +0000754 } else if (FloatABI == "softfp") {
755 // Floating point operations are hard, but argument passing is soft.
Daniel Dunbar6cc525b2009-12-08 19:49:51 +0000756 CmdArgs.push_back("-mfloat-abi");
757 CmdArgs.push_back("soft");
Daniel Dunbar78485922009-09-10 23:00:09 +0000758 } else {
759 // Floating point operations and argument passing are hard.
760 assert(FloatABI == "hard" && "Invalid float abi!");
Daniel Dunbar6cc525b2009-12-08 19:49:51 +0000761 CmdArgs.push_back("-mfloat-abi");
762 CmdArgs.push_back("hard");
Daniel Dunbar78485922009-09-10 23:00:09 +0000763 }
Daniel Dunbar893d4752009-12-19 04:15:38 +0000764
765 // Set appropriate target features for floating point mode.
766 //
767 // FIXME: Note, this is a hack, the LLVM backend doesn't actually use these
768 // yet (it uses the -mfloat-abi and -msoft-float options above), and it is
769 // stripped out by the ARM target.
770
771 // Use software floating point operations?
772 if (FloatABI == "soft") {
773 CmdArgs.push_back("-target-feature");
774 CmdArgs.push_back("+soft-float");
775 }
776
777 // Use software floating point argument passing?
778 if (FloatABI != "hard") {
779 CmdArgs.push_back("-target-feature");
780 CmdArgs.push_back("+soft-float-abi");
781 }
Daniel Dunbar0def3d12009-12-21 23:28:17 +0000782
783 // Honor -mfpu=.
Chad Rosiercfbfc582012-04-04 20:51:35 +0000784 if (const Arg *A = Args.getLastArg(options::OPT_mfpu_EQ))
Chad Rosierd1717c12012-04-04 20:56:36 +0000785 addFPUArgs(D, A, Args, CmdArgs);
Daniel Dunbarb1db4b62011-03-17 00:07:34 +0000786
Chad Rosier1f0e52e2012-04-04 20:39:32 +0000787 // Honor -mfpmath=.
788 if (const Arg *A = Args.getLastArg(options::OPT_mfpmath_EQ))
Chad Rosier45619cb2012-04-04 22:13:40 +0000789 addFPMathArgs(D, A, Args, CmdArgs, getARMTargetCPU(Args, Triple));
Chad Rosier1f0e52e2012-04-04 20:39:32 +0000790
Daniel Dunbarb1db4b62011-03-17 00:07:34 +0000791 // Setting -msoft-float effectively disables NEON because of the GCC
792 // implementation, although the same isn't true of VFP or VFP3.
793 if (FloatABI == "soft") {
Daniel Dunbarc9388c12011-03-17 17:10:06 +0000794 CmdArgs.push_back("-target-feature");
795 CmdArgs.push_back("-neon");
796 }
797
798 // Kernel code has more strict alignment requirements.
799 if (KernelOrKext) {
Daniel Dunbarbd847cc2012-10-15 22:23:53 +0000800 if (Triple.getOS() != llvm::Triple::IOS || Triple.isOSVersionLT(6)) {
801 CmdArgs.push_back("-backend-option");
802 CmdArgs.push_back("-arm-long-calls");
803 }
Daniel Dunbarc9388c12011-03-17 17:10:06 +0000804
Daniel Dunbar12100e22011-03-22 16:48:17 +0000805 CmdArgs.push_back("-backend-option");
Daniel Dunbarc9388c12011-03-17 17:10:06 +0000806 CmdArgs.push_back("-arm-strict-align");
Daniel Dunbared904c82011-04-18 21:26:42 +0000807
808 // The kext linker doesn't know how to deal with movw/movt.
Daniel Dunbared904c82011-04-18 21:26:42 +0000809 CmdArgs.push_back("-backend-option");
810 CmdArgs.push_back("-arm-darwin-use-movt=0");
Daniel Dunbarb1db4b62011-03-17 00:07:34 +0000811 }
Chad Rosierba3df1d2011-08-26 00:26:29 +0000812
813 // Setting -mno-global-merge disables the codegen global merge pass. Setting
814 // -mglobal-merge has no effect as the pass is enabled by default.
815 if (Arg *A = Args.getLastArg(options::OPT_mglobal_merge,
816 options::OPT_mno_global_merge)) {
817 if (A->getOption().matches(options::OPT_mno_global_merge))
818 CmdArgs.push_back("-mno-global-merge");
819 }
Chad Rosierf1985d22012-05-16 20:40:09 +0000820
Bob Wilson9c8af452013-04-11 18:53:25 +0000821 if (!Args.hasFlag(options::OPT_mimplicit_float,
822 options::OPT_mno_implicit_float,
823 true))
Chad Rosierf1985d22012-05-16 20:40:09 +0000824 CmdArgs.push_back("-no-implicit-float");
Daniel Dunbar0f5c5422009-09-10 04:57:17 +0000825}
826
Simon Atanasyan2efe53e2012-09-21 20:19:32 +0000827// Translate MIPS CPU name alias option to CPU name.
828static StringRef getMipsCPUFromAlias(const Arg &A) {
829 if (A.getOption().matches(options::OPT_mips32))
830 return "mips32";
831 if (A.getOption().matches(options::OPT_mips32r2))
832 return "mips32r2";
833 if (A.getOption().matches(options::OPT_mips64))
834 return "mips64";
835 if (A.getOption().matches(options::OPT_mips64r2))
836 return "mips64r2";
837 llvm_unreachable("Unexpected option");
838 return "";
839}
840
Simon Atanasyan3b7589a2012-04-07 22:09:23 +0000841// Get CPU and ABI names. They are not independent
842// so we have to calculate them together.
843static void getMipsCPUAndABI(const ArgList &Args,
844 const ToolChain &TC,
845 StringRef &CPUName,
846 StringRef &ABIName) {
Simon Atanasyan464a7f72012-09-10 08:32:41 +0000847 const char *DefMips32CPU = "mips32";
848 const char *DefMips64CPU = "mips64";
Akira Hatanaka37fd9e92011-09-26 21:07:52 +0000849
Simon Atanasyan464a7f72012-09-10 08:32:41 +0000850 if (Arg *A = Args.getLastArg(options::OPT_march_EQ,
Simon Atanasyan2efe53e2012-09-21 20:19:32 +0000851 options::OPT_mcpu_EQ,
852 options::OPT_mips_CPUs_Group)) {
853 if (A->getOption().matches(options::OPT_mips_CPUs_Group))
854 CPUName = getMipsCPUFromAlias(*A);
855 else
Richard Smithbd55daf2012-11-01 04:30:05 +0000856 CPUName = A->getValue();
Simon Atanasyan2efe53e2012-09-21 20:19:32 +0000857 }
Simon Atanasyan464a7f72012-09-10 08:32:41 +0000858
Simon Atanasyan4938ddb2013-04-21 13:30:10 +0000859 if (Arg *A = Args.getLastArg(options::OPT_mabi_EQ)) {
Richard Smithbd55daf2012-11-01 04:30:05 +0000860 ABIName = A->getValue();
Simon Atanasyan4938ddb2013-04-21 13:30:10 +0000861 // Convert a GNU style Mips ABI name to the name
862 // accepted by LLVM Mips backend.
863 ABIName = llvm::StringSwitch<llvm::StringRef>(ABIName)
864 .Case("32", "o32")
865 .Case("64", "n64")
866 .Default(ABIName);
867 }
Simon Atanasyan464a7f72012-09-10 08:32:41 +0000868
869 // Setup default CPU and ABI names.
870 if (CPUName.empty() && ABIName.empty()) {
871 switch (TC.getTriple().getArch()) {
872 default:
873 llvm_unreachable("Unexpected triple arch name");
874 case llvm::Triple::mips:
875 case llvm::Triple::mipsel:
876 CPUName = DefMips32CPU;
877 break;
878 case llvm::Triple::mips64:
879 case llvm::Triple::mips64el:
880 CPUName = DefMips64CPU;
881 break;
882 }
883 }
884
885 if (!ABIName.empty()) {
886 // Deduce CPU name from ABI name.
887 CPUName = llvm::StringSwitch<const char *>(ABIName)
Simon Atanasyan0da400c2013-02-27 14:55:49 +0000888 .Cases("32", "o32", "eabi", DefMips32CPU)
889 .Cases("n32", "n64", "64", DefMips64CPU)
Simon Atanasyan464a7f72012-09-10 08:32:41 +0000890 .Default("");
891 }
892 else if (!CPUName.empty()) {
893 // Deduce ABI name from CPU name.
894 ABIName = llvm::StringSwitch<const char *>(CPUName)
895 .Cases("mips32", "mips32r2", "o32")
896 .Cases("mips64", "mips64r2", "n64")
897 .Default("");
898 }
899
900 // FIXME: Warn on inconsistent cpu and abi usage.
Simon Atanasyan3b7589a2012-04-07 22:09:23 +0000901}
902
Simon Atanasyan0da400c2013-02-27 14:55:49 +0000903// Convert ABI name to the GNU tools acceptable variant.
904static StringRef getGnuCompatibleMipsABIName(StringRef ABI) {
905 return llvm::StringSwitch<llvm::StringRef>(ABI)
906 .Case("o32", "32")
907 .Case("n64", "64")
908 .Default(ABI);
909}
910
Simon Atanasyan590ad8f2012-06-02 15:06:29 +0000911// Select the MIPS float ABI as determined by -msoft-float, -mhard-float,
912// and -mfloat-abi=.
913static StringRef getMipsFloatABI(const Driver &D, const ArgList &Args) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000914 StringRef FloatABI;
Eric Christopher0b26a612010-03-02 02:41:08 +0000915 if (Arg *A = Args.getLastArg(options::OPT_msoft_float,
Akira Hatanaka6976ec82012-03-23 23:07:09 +0000916 options::OPT_mhard_float,
917 options::OPT_mfloat_abi_EQ)) {
Eric Christopher0b26a612010-03-02 02:41:08 +0000918 if (A->getOption().matches(options::OPT_msoft_float))
919 FloatABI = "soft";
920 else if (A->getOption().matches(options::OPT_mhard_float))
921 FloatABI = "hard";
Akira Hatanaka6976ec82012-03-23 23:07:09 +0000922 else {
Richard Smithbd55daf2012-11-01 04:30:05 +0000923 FloatABI = A->getValue();
Simon Atanasyan512dc382013-04-14 08:37:15 +0000924 if (FloatABI != "soft" && FloatABI != "hard") {
Simon Atanasyan590ad8f2012-06-02 15:06:29 +0000925 D.Diag(diag::err_drv_invalid_mfloat_abi) << A->getAsString(Args);
Akira Hatanaka6976ec82012-03-23 23:07:09 +0000926 FloatABI = "hard";
927 }
928 }
Eric Christopher0b26a612010-03-02 02:41:08 +0000929 }
930
931 // If unspecified, choose the default based on the platform.
932 if (FloatABI.empty()) {
Akira Hatanaka6976ec82012-03-23 23:07:09 +0000933 // Assume "hard", because it's a default value used by gcc.
934 // When we start to recognize specific target MIPS processors,
935 // we will be able to select the default more correctly.
936 FloatABI = "hard";
Eric Christopher0b26a612010-03-02 02:41:08 +0000937 }
938
Simon Atanasyan590ad8f2012-06-02 15:06:29 +0000939 return FloatABI;
940}
941
Simon Atanasyan9b1932d2012-07-05 18:51:43 +0000942static void AddTargetFeature(const ArgList &Args,
943 ArgStringList &CmdArgs,
944 OptSpecifier OnOpt,
945 OptSpecifier OffOpt,
946 StringRef FeatureName) {
947 if (Arg *A = Args.getLastArg(OnOpt, OffOpt)) {
948 CmdArgs.push_back("-target-feature");
949 if (A->getOption().matches(OnOpt))
950 CmdArgs.push_back(Args.MakeArgString("+" + FeatureName));
951 else
952 CmdArgs.push_back(Args.MakeArgString("-" + FeatureName));
953 }
954}
955
Simon Atanasyan590ad8f2012-06-02 15:06:29 +0000956void Clang::AddMIPSTargetArgs(const ArgList &Args,
Simon Atanasyanf0087242013-04-14 14:07:41 +0000957 ArgStringList &CmdArgs) const {
Simon Atanasyan590ad8f2012-06-02 15:06:29 +0000958 const Driver &D = getToolChain().getDriver();
959 StringRef CPUName;
960 StringRef ABIName;
961 getMipsCPUAndABI(Args, getToolChain(), CPUName, ABIName);
962
963 CmdArgs.push_back("-target-cpu");
964 CmdArgs.push_back(CPUName.data());
965
966 CmdArgs.push_back("-target-abi");
967 CmdArgs.push_back(ABIName.data());
968
969 StringRef FloatABI = getMipsFloatABI(D, Args);
970
Simon Atanasyan81c423c2013-01-10 12:36:19 +0000971 bool IsMips16 = Args.getLastArg(options::OPT_mips16) != NULL;
972
973 if (FloatABI == "soft" || (FloatABI == "hard" && IsMips16)) {
Eric Christopher0b26a612010-03-02 02:41:08 +0000974 // Floating point operations and argument passing are soft.
Eric Christopher0b26a612010-03-02 02:41:08 +0000975 CmdArgs.push_back("-msoft-float");
Akira Hatanaka6976ec82012-03-23 23:07:09 +0000976 CmdArgs.push_back("-mfloat-abi");
977 CmdArgs.push_back("soft");
978
979 // FIXME: Note, this is a hack. We need to pass the selected float
980 // mode to the MipsTargetInfoBase to define appropriate macros there.
981 // Now it is the only method.
982 CmdArgs.push_back("-target-feature");
983 CmdArgs.push_back("+soft-float");
Simon Atanasyan81c423c2013-01-10 12:36:19 +0000984
985 if (FloatABI == "hard" && IsMips16) {
986 CmdArgs.push_back("-mllvm");
987 CmdArgs.push_back("-mips16-hard-float");
988 }
Akira Hatanaka6976ec82012-03-23 23:07:09 +0000989 }
Akira Hatanaka6976ec82012-03-23 23:07:09 +0000990 else {
991 // Floating point operations and argument passing are hard.
Eric Christopher0b26a612010-03-02 02:41:08 +0000992 assert(FloatABI == "hard" && "Invalid float abi!");
Akira Hatanaka6976ec82012-03-23 23:07:09 +0000993 CmdArgs.push_back("-mfloat-abi");
994 CmdArgs.push_back("hard");
Eric Christopher0b26a612010-03-02 02:41:08 +0000995 }
Simon Atanasyan6f23fa02012-07-05 14:19:39 +0000996
Simon Atanasyan82eec3a2013-04-14 14:07:36 +0000997 AddTargetFeature(Args, CmdArgs,
998 options::OPT_msingle_float, options::OPT_mdouble_float,
999 "single-float");
Simon Atanasyan9b1932d2012-07-05 18:51:43 +00001000 AddTargetFeature(Args, CmdArgs,
1001 options::OPT_mips16, options::OPT_mno_mips16,
1002 "mips16");
Simon Atanasyan9c6f1f72012-07-05 19:23:00 +00001003 AddTargetFeature(Args, CmdArgs,
Simon Atanasyan60777612013-04-14 14:07:51 +00001004 options::OPT_mmicromips, options::OPT_mno_micromips,
1005 "micromips");
1006 AddTargetFeature(Args, CmdArgs,
Simon Atanasyan9c6f1f72012-07-05 19:23:00 +00001007 options::OPT_mdsp, options::OPT_mno_dsp,
1008 "dsp");
1009 AddTargetFeature(Args, CmdArgs,
1010 options::OPT_mdspr2, options::OPT_mno_dspr2,
1011 "dspr2");
Simon Atanasyanec4b1c12012-08-27 20:55:56 +00001012
Simon Atanasyan2eaec512012-12-01 18:27:21 +00001013 if (Arg *A = Args.getLastArg(options::OPT_mxgot, options::OPT_mno_xgot)) {
1014 if (A->getOption().matches(options::OPT_mxgot)) {
1015 CmdArgs.push_back("-mllvm");
1016 CmdArgs.push_back("-mxgot");
1017 }
1018 }
1019
Simon Atanasyanec4b1c12012-08-27 20:55:56 +00001020 if (Arg *A = Args.getLastArg(options::OPT_G)) {
Richard Smithbd55daf2012-11-01 04:30:05 +00001021 StringRef v = A->getValue();
Simon Atanasyanec4b1c12012-08-27 20:55:56 +00001022 CmdArgs.push_back("-mllvm");
1023 CmdArgs.push_back(Args.MakeArgString("-mips-ssection-threshold=" + v));
1024 A->claim();
1025 }
Eric Christopher0b26a612010-03-02 02:41:08 +00001026}
1027
Hal Finkel8eb59282012-06-11 22:35:19 +00001028/// getPPCTargetCPU - Get the (LLVM) name of the PowerPC cpu we are targeting.
1029static std::string getPPCTargetCPU(const ArgList &Args) {
1030 if (Arg *A = Args.getLastArg(options::OPT_mcpu_EQ)) {
Richard Smithbd55daf2012-11-01 04:30:05 +00001031 StringRef CPUName = A->getValue();
Hal Finkel8eb59282012-06-11 22:35:19 +00001032
1033 if (CPUName == "native") {
1034 std::string CPU = llvm::sys::getHostCPUName();
1035 if (!CPU.empty() && CPU != "generic")
1036 return CPU;
1037 else
1038 return "";
1039 }
1040
1041 return llvm::StringSwitch<const char *>(CPUName)
1042 .Case("common", "generic")
1043 .Case("440", "440")
1044 .Case("440fp", "440")
1045 .Case("450", "450")
1046 .Case("601", "601")
1047 .Case("602", "602")
1048 .Case("603", "603")
1049 .Case("603e", "603e")
1050 .Case("603ev", "603ev")
1051 .Case("604", "604")
1052 .Case("604e", "604e")
1053 .Case("620", "620")
Bill Schmidt38378a02013-02-01 20:23:10 +00001054 .Case("630", "pwr3")
Hal Finkel8eb59282012-06-11 22:35:19 +00001055 .Case("G3", "g3")
1056 .Case("7400", "7400")
1057 .Case("G4", "g4")
1058 .Case("7450", "7450")
1059 .Case("G4+", "g4+")
1060 .Case("750", "750")
1061 .Case("970", "970")
1062 .Case("G5", "g5")
1063 .Case("a2", "a2")
Hal Finkeldf1e4bf2013-02-01 05:53:33 +00001064 .Case("a2q", "a2q")
Hal Finkelf6d6cb02012-09-18 22:25:03 +00001065 .Case("e500mc", "e500mc")
1066 .Case("e5500", "e5500")
Bill Schmidt38378a02013-02-01 20:23:10 +00001067 .Case("power3", "pwr3")
1068 .Case("power4", "pwr4")
1069 .Case("power5", "pwr5")
1070 .Case("power5x", "pwr5x")
Hal Finkel8eb59282012-06-11 22:35:19 +00001071 .Case("power6", "pwr6")
Bill Schmidt38378a02013-02-01 20:23:10 +00001072 .Case("power6x", "pwr6x")
Hal Finkel8eb59282012-06-11 22:35:19 +00001073 .Case("power7", "pwr7")
Bill Schmidt38378a02013-02-01 20:23:10 +00001074 .Case("pwr3", "pwr3")
1075 .Case("pwr4", "pwr4")
1076 .Case("pwr5", "pwr5")
1077 .Case("pwr5x", "pwr5x")
1078 .Case("pwr6", "pwr6")
1079 .Case("pwr6x", "pwr6x")
1080 .Case("pwr7", "pwr7")
Hal Finkel8eb59282012-06-11 22:35:19 +00001081 .Case("powerpc", "ppc")
1082 .Case("powerpc64", "ppc64")
1083 .Default("");
1084 }
1085
1086 return "";
1087}
1088
1089void Clang::AddPPCTargetArgs(const ArgList &Args,
1090 ArgStringList &CmdArgs) const {
1091 std::string TargetCPUName = getPPCTargetCPU(Args);
1092
1093 // LLVM may default to generating code for the native CPU,
1094 // but, like gcc, we default to a more generic option for
1095 // each architecture. (except on Darwin)
1096 llvm::Triple Triple = getToolChain().getTriple();
1097 if (TargetCPUName.empty() && !Triple.isOSDarwin()) {
1098 if (Triple.getArch() == llvm::Triple::ppc64)
1099 TargetCPUName = "ppc64";
1100 else
1101 TargetCPUName = "ppc";
1102 }
1103
1104 if (!TargetCPUName.empty()) {
1105 CmdArgs.push_back("-target-cpu");
1106 CmdArgs.push_back(Args.MakeArgString(TargetCPUName.c_str()));
1107 }
Bill Schmidt2fe4c672013-02-01 02:14:03 +00001108
1109 // Allow override of the Altivec feature.
Hal Finkel279ca4d2013-03-28 08:38:53 +00001110 AddTargetFeature(Args, CmdArgs,
1111 options::OPT_faltivec, options::OPT_fno_altivec,
1112 "altivec");
Hal Finkelb58ce852013-02-01 18:44:19 +00001113
Hal Finkel7d458592013-03-30 13:47:44 +00001114 AddTargetFeature(Args, CmdArgs,
1115 options::OPT_mfprnd, options::OPT_mno_fprnd,
1116 "fprnd");
1117
Hal Finkel279ca4d2013-03-28 08:38:53 +00001118 // Note that gcc calls this mfcrf and LLVM calls this mfocrf.
1119 AddTargetFeature(Args, CmdArgs,
1120 options::OPT_mmfcrf, options::OPT_mno_mfcrf,
1121 "mfocrf");
1122
Hal Finkel1fe8b3d2013-03-28 13:51:36 +00001123 AddTargetFeature(Args, CmdArgs,
1124 options::OPT_mpopcntd, options::OPT_mno_popcntd,
1125 "popcntd");
1126
Hal Finkel279ca4d2013-03-28 08:38:53 +00001127 // It is really only possible to turn qpx off because turning qpx on is tied
1128 // to using the a2q CPU.
Hal Finkelb58ce852013-02-01 18:44:19 +00001129 if (Args.hasFlag(options::OPT_mno_qpx, options::OPT_mqpx, false)) {
1130 CmdArgs.push_back("-target-feature");
1131 CmdArgs.push_back("-qpx");
1132 }
Hal Finkel8eb59282012-06-11 22:35:19 +00001133}
1134
Tom Stellard6674c702013-04-01 20:56:53 +00001135/// Get the (LLVM) name of the R600 gpu we are targeting.
1136static std::string getR600TargetGPU(const ArgList &Args) {
1137 if (Arg *A = Args.getLastArg(options::OPT_mcpu_EQ)) {
1138 std::string GPUName = A->getValue();
1139 return llvm::StringSwitch<const char *>(GPUName)
1140 .Cases("rv610", "rv620", "rv630", "r600")
1141 .Cases("rv635", "rs780", "rs880", "r600")
1142 .Case("rv740", "rv770")
1143 .Case("palm", "cedar")
1144 .Cases("sumo", "sumo2", "redwood")
1145 .Case("hemlock", "cypress")
1146 .Case("aruba", "cayman")
1147 .Default(GPUName.c_str());
1148 }
1149 return "";
1150}
1151
1152void Clang::AddR600TargetArgs(const ArgList &Args,
1153 ArgStringList &CmdArgs) const {
1154 std::string TargetGPUName = getR600TargetGPU(Args);
1155 CmdArgs.push_back("-target-cpu");
1156 CmdArgs.push_back(Args.MakeArgString(TargetGPUName.c_str()));
1157}
1158
Bruno Cardoso Lopese7f211c2010-11-09 17:21:19 +00001159void Clang::AddSparcTargetArgs(const ArgList &Args,
1160 ArgStringList &CmdArgs) const {
1161 const Driver &D = getToolChain().getDriver();
1162
1163 if (const Arg *A = Args.getLastArg(options::OPT_march_EQ)) {
Bruno Cardoso Lopese7f211c2010-11-09 17:21:19 +00001164 CmdArgs.push_back("-target-cpu");
Richard Smithbd55daf2012-11-01 04:30:05 +00001165 CmdArgs.push_back(A->getValue());
Bruno Cardoso Lopese7f211c2010-11-09 17:21:19 +00001166 }
1167
1168 // Select the float ABI as determined by -msoft-float, -mhard-float, and
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001169 StringRef FloatABI;
Bruno Cardoso Lopese7f211c2010-11-09 17:21:19 +00001170 if (Arg *A = Args.getLastArg(options::OPT_msoft_float,
1171 options::OPT_mhard_float)) {
1172 if (A->getOption().matches(options::OPT_msoft_float))
1173 FloatABI = "soft";
1174 else if (A->getOption().matches(options::OPT_mhard_float))
1175 FloatABI = "hard";
1176 }
1177
1178 // If unspecified, choose the default based on the platform.
1179 if (FloatABI.empty()) {
1180 switch (getToolChain().getTriple().getOS()) {
1181 default:
1182 // Assume "soft", but warn the user we are guessing.
1183 FloatABI = "soft";
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001184 D.Diag(diag::warn_drv_assuming_mfloat_abi_is) << "soft";
Bruno Cardoso Lopese7f211c2010-11-09 17:21:19 +00001185 break;
1186 }
1187 }
1188
1189 if (FloatABI == "soft") {
1190 // Floating point operations and argument passing are soft.
1191 //
1192 // FIXME: This changes CPP defines, we need -target-soft-float.
1193 CmdArgs.push_back("-msoft-float");
Bruno Cardoso Lopese7f211c2010-11-09 17:21:19 +00001194 CmdArgs.push_back("-target-feature");
1195 CmdArgs.push_back("+soft-float");
1196 } else {
1197 assert(FloatABI == "hard" && "Invalid float abi!");
1198 CmdArgs.push_back("-mhard-float");
1199 }
1200}
1201
Chandler Carruth953fb082013-01-13 11:46:33 +00001202static const char *getX86TargetCPU(const ArgList &Args,
1203 const llvm::Triple &Triple) {
1204 if (const Arg *A = Args.getLastArg(options::OPT_march_EQ)) {
1205 if (StringRef(A->getValue()) != "native")
1206 return A->getValue();
1207
1208 // FIXME: Reject attempts to use -march=native unless the target matches
1209 // the host.
1210 //
1211 // FIXME: We should also incorporate the detected target features for use
1212 // with -native.
1213 std::string CPU = llvm::sys::getHostCPUName();
1214 if (!CPU.empty() && CPU != "generic")
1215 return Args.MakeArgString(CPU);
1216 }
1217
1218 // Select the default CPU if none was given (or detection failed).
1219
1220 if (Triple.getArch() != llvm::Triple::x86_64 &&
1221 Triple.getArch() != llvm::Triple::x86)
1222 return 0; // This routine is only handling x86 targets.
1223
1224 bool Is64Bit = Triple.getArch() == llvm::Triple::x86_64;
1225
1226 // FIXME: Need target hooks.
1227 if (Triple.isOSDarwin())
1228 return Is64Bit ? "core2" : "yonah";
1229
1230 // Everything else goes to x86-64 in 64-bit mode.
1231 if (Is64Bit)
1232 return "x86-64";
1233
1234 if (Triple.getOSName().startswith("haiku"))
1235 return "i586";
1236 if (Triple.getOSName().startswith("openbsd"))
1237 return "i486";
1238 if (Triple.getOSName().startswith("bitrig"))
1239 return "i686";
1240 if (Triple.getOSName().startswith("freebsd"))
1241 return "i486";
1242 if (Triple.getOSName().startswith("netbsd"))
1243 return "i486";
1244 // All x86 devices running Android have core2 as their common
1245 // denominator. This makes a better choice than pentium4.
1246 if (Triple.getEnvironment() == llvm::Triple::Android)
1247 return "core2";
1248
1249 // Fallback to p4.
1250 return "pentium4";
1251}
1252
Daniel Dunbar3b3191f2009-09-09 22:33:08 +00001253void Clang::AddX86TargetArgs(const ArgList &Args,
1254 ArgStringList &CmdArgs) const {
Daniel Dunbare2cf8f72009-09-10 22:59:57 +00001255 if (!Args.hasFlag(options::OPT_mred_zone,
1256 options::OPT_mno_red_zone,
1257 true) ||
1258 Args.hasArg(options::OPT_mkernel) ||
1259 Args.hasArg(options::OPT_fapple_kext))
Daniel Dunbar8bed86c2009-11-20 22:21:36 +00001260 CmdArgs.push_back("-disable-red-zone");
Daniel Dunbare2cf8f72009-09-10 22:59:57 +00001261
Bob Wilson2616e2e2013-02-10 16:01:41 +00001262 // Default to avoid implicit floating-point for kernel/kext code, but allow
1263 // that to be overridden with -mno-soft-float.
1264 bool NoImplicitFloat = (Args.hasArg(options::OPT_mkernel) ||
1265 Args.hasArg(options::OPT_fapple_kext));
1266 if (Arg *A = Args.getLastArg(options::OPT_msoft_float,
1267 options::OPT_mno_soft_float,
Bob Wilson9c8af452013-04-11 18:53:25 +00001268 options::OPT_mimplicit_float,
Bob Wilson2616e2e2013-02-10 16:01:41 +00001269 options::OPT_mno_implicit_float)) {
1270 const Option &O = A->getOption();
1271 NoImplicitFloat = (O.matches(options::OPT_mno_implicit_float) ||
1272 O.matches(options::OPT_msoft_float));
1273 }
1274 if (NoImplicitFloat)
Daniel Dunbar8bed86c2009-11-20 22:21:36 +00001275 CmdArgs.push_back("-no-implicit-float");
Daniel Dunbare2cf8f72009-09-10 22:59:57 +00001276
Chandler Carruth953fb082013-01-13 11:46:33 +00001277 if (const char *CPUName = getX86TargetCPU(Args, getToolChain().getTriple())) {
Daniel Dunbara7d02312009-12-18 06:30:12 +00001278 CmdArgs.push_back("-target-cpu");
Daniel Dunbare13ada62009-11-14 22:04:54 +00001279 CmdArgs.push_back(CPUName);
1280 }
1281
Eli Friedmanad811f02011-07-02 00:34:19 +00001282 // The required algorithm here is slightly strange: the options are applied
1283 // in order (so -mno-sse -msse2 disables SSE3), but any option that gets
1284 // directly overridden later is ignored (so "-mno-sse -msse2 -mno-sse2 -msse"
1285 // is equivalent to "-mno-sse2 -msse"). The -cc1 handling deals with the
1286 // former correctly, but not the latter; handle directly-overridden
1287 // attributes here.
1288 llvm::StringMap<unsigned> PrevFeature;
1289 std::vector<const char*> Features;
Daniel Dunbar44b36ee2009-11-25 11:53:23 +00001290 for (arg_iterator it = Args.filtered_begin(options::OPT_m_x86_Features_Group),
1291 ie = Args.filtered_end(); it != ie; ++it) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001292 StringRef Name = (*it)->getOption().getName();
Daniel Dunbara442fd52010-06-11 22:00:13 +00001293 (*it)->claim();
Daniel Dunbar3b3191f2009-09-09 22:33:08 +00001294
Daniel Dunbar44b36ee2009-11-25 11:53:23 +00001295 // Skip over "-m".
Michael J. Spencer929fccd2012-10-22 22:13:48 +00001296 assert(Name.startswith("m") && "Invalid feature name.");
1297 Name = Name.substr(1);
Daniel Dunbar3b3191f2009-09-09 22:33:08 +00001298
Daniel Dunbar44b36ee2009-11-25 11:53:23 +00001299 bool IsNegative = Name.startswith("no-");
1300 if (IsNegative)
1301 Name = Name.substr(3);
Daniel Dunbar3b3191f2009-09-09 22:33:08 +00001302
Eli Friedmanad811f02011-07-02 00:34:19 +00001303 unsigned& Prev = PrevFeature[Name];
1304 if (Prev)
1305 Features[Prev - 1] = 0;
1306 Prev = Features.size() + 1;
1307 Features.push_back(Args.MakeArgString((IsNegative ? "-" : "+") + Name));
1308 }
1309 for (unsigned i = 0; i < Features.size(); i++) {
1310 if (Features[i]) {
1311 CmdArgs.push_back("-target-feature");
1312 CmdArgs.push_back(Features[i]);
1313 }
Daniel Dunbar3b3191f2009-09-09 22:33:08 +00001314 }
1315}
1316
Matthew Curtise8f80a12012-12-06 17:49:03 +00001317static inline bool HasPICArg(const ArgList &Args) {
1318 return Args.hasArg(options::OPT_fPIC)
1319 || Args.hasArg(options::OPT_fpic);
1320}
1321
1322static Arg *GetLastSmallDataThresholdArg(const ArgList &Args) {
1323 return Args.getLastArg(options::OPT_G,
1324 options::OPT_G_EQ,
1325 options::OPT_msmall_data_threshold_EQ);
1326}
1327
1328static std::string GetHexagonSmallDataThresholdValue(const ArgList &Args) {
1329 std::string value;
1330 if (HasPICArg(Args))
1331 value = "0";
1332 else if (Arg *A = GetLastSmallDataThresholdArg(Args)) {
1333 value = A->getValue();
1334 A->claim();
1335 }
1336 return value;
1337}
1338
Tony Linthicum76329bf2011-12-12 21:14:55 +00001339void Clang::AddHexagonTargetArgs(const ArgList &Args,
1340 ArgStringList &CmdArgs) const {
1341 llvm::Triple Triple = getToolChain().getTriple();
1342
1343 CmdArgs.push_back("-target-cpu");
Matthew Curtisf10a5952012-12-06 14:16:43 +00001344 CmdArgs.push_back(Args.MakeArgString(
1345 "hexagon"
1346 + toolchains::Hexagon_TC::GetTargetCPU(Args)));
Tony Linthicum76329bf2011-12-12 21:14:55 +00001347 CmdArgs.push_back("-fno-signed-char");
Matthew Curtis6b222782012-12-07 13:52:44 +00001348 CmdArgs.push_back("-mqdsp6-compat");
1349 CmdArgs.push_back("-Wreturn-type");
Tony Linthicum76329bf2011-12-12 21:14:55 +00001350
Matthew Curtise8f80a12012-12-06 17:49:03 +00001351 std::string SmallDataThreshold = GetHexagonSmallDataThresholdValue(Args);
1352 if (!SmallDataThreshold.empty()) {
Tony Linthicum76329bf2011-12-12 21:14:55 +00001353 CmdArgs.push_back ("-mllvm");
Matthew Curtise8f80a12012-12-06 17:49:03 +00001354 CmdArgs.push_back(Args.MakeArgString(
1355 "-hexagon-small-data-threshold=" + SmallDataThreshold));
Tony Linthicum76329bf2011-12-12 21:14:55 +00001356 }
1357
Sirish Pande11ebc4e2012-05-10 20:19:54 +00001358 if (!Args.hasArg(options::OPT_fno_short_enums))
1359 CmdArgs.push_back("-fshort-enums");
1360 if (Args.getLastArg(options::OPT_mieee_rnd_near)) {
1361 CmdArgs.push_back ("-mllvm");
1362 CmdArgs.push_back ("-enable-hexagon-ieee-rnd-near");
1363 }
Tony Linthicum76329bf2011-12-12 21:14:55 +00001364 CmdArgs.push_back ("-mllvm");
1365 CmdArgs.push_back ("-machine-sink-split=0");
1366}
1367
Eric Christopher84fbdb42011-08-19 00:30:14 +00001368static bool
John McCall5fb5df92012-06-20 06:18:46 +00001369shouldUseExceptionTablesForObjCExceptions(const ObjCRuntime &runtime,
Anders Carlsson246ff3f2011-02-28 00:44:51 +00001370 const llvm::Triple &Triple) {
1371 // We use the zero-cost exception tables for Objective-C if the non-fragile
1372 // ABI is enabled or when compiling for x86_64 and ARM on Snow Leopard and
1373 // later.
John McCall5fb5df92012-06-20 06:18:46 +00001374 if (runtime.isNonFragile())
Anders Carlsson246ff3f2011-02-28 00:44:51 +00001375 return true;
1376
Bob Wilson6524dd32011-10-14 05:03:44 +00001377 if (!Triple.isOSDarwin())
Anders Carlsson246ff3f2011-02-28 00:44:51 +00001378 return false;
1379
Eric Christopherbf15d2b2011-07-02 00:20:22 +00001380 return (!Triple.isMacOSXVersionLT(10,5) &&
Anders Carlsson246ff3f2011-02-28 00:44:51 +00001381 (Triple.getArch() == llvm::Triple::x86_64 ||
Eric Christopher84fbdb42011-08-19 00:30:14 +00001382 Triple.getArch() == llvm::Triple::arm));
Anders Carlsson246ff3f2011-02-28 00:44:51 +00001383}
1384
Anders Carlssone96ab552011-02-28 02:27:16 +00001385/// addExceptionArgs - Adds exception related arguments to the driver command
1386/// arguments. There's a master flag, -fexceptions and also language specific
1387/// flags to enable/disable C++ and Objective-C exceptions.
1388/// This makes it possible to for example disable C++ exceptions but enable
1389/// Objective-C exceptions.
1390static void addExceptionArgs(const ArgList &Args, types::ID InputType,
1391 const llvm::Triple &Triple,
Fariborz Jahanianb2482212012-04-04 18:28:00 +00001392 bool KernelOrKext,
John McCall5fb5df92012-06-20 06:18:46 +00001393 const ObjCRuntime &objcRuntime,
Anders Carlssone96ab552011-02-28 02:27:16 +00001394 ArgStringList &CmdArgs) {
Chad Rosier4fab82c2012-03-26 22:04:46 +00001395 if (KernelOrKext) {
1396 // -mkernel and -fapple-kext imply no exceptions, so claim exception related
1397 // arguments now to avoid warnings about unused arguments.
1398 Args.ClaimAllArgs(options::OPT_fexceptions);
1399 Args.ClaimAllArgs(options::OPT_fno_exceptions);
1400 Args.ClaimAllArgs(options::OPT_fobjc_exceptions);
1401 Args.ClaimAllArgs(options::OPT_fno_objc_exceptions);
1402 Args.ClaimAllArgs(options::OPT_fcxx_exceptions);
1403 Args.ClaimAllArgs(options::OPT_fno_cxx_exceptions);
Anders Carlssone96ab552011-02-28 02:27:16 +00001404 return;
Chad Rosier4fab82c2012-03-26 22:04:46 +00001405 }
Anders Carlssone96ab552011-02-28 02:27:16 +00001406
1407 // Exceptions are enabled by default.
1408 bool ExceptionsEnabled = true;
1409
1410 // This keeps track of whether exceptions were explicitly turned on or off.
1411 bool DidHaveExplicitExceptionFlag = false;
1412
Rafael Espindola00a66572009-10-01 13:33:33 +00001413 if (Arg *A = Args.getLastArg(options::OPT_fexceptions,
1414 options::OPT_fno_exceptions)) {
1415 if (A->getOption().matches(options::OPT_fexceptions))
Anders Carlssone96ab552011-02-28 02:27:16 +00001416 ExceptionsEnabled = true;
Eric Christopher84fbdb42011-08-19 00:30:14 +00001417 else
Anders Carlssone96ab552011-02-28 02:27:16 +00001418 ExceptionsEnabled = false;
1419
1420 DidHaveExplicitExceptionFlag = true;
Rafael Espindola00a66572009-10-01 13:33:33 +00001421 }
Daniel Dunbar30a12b82010-09-14 23:12:31 +00001422
Anders Carlssone96ab552011-02-28 02:27:16 +00001423 bool ShouldUseExceptionTables = false;
Fariborz Jahaniane4b21ab2009-10-01 20:30:46 +00001424
Anders Carlssone96ab552011-02-28 02:27:16 +00001425 // Exception tables and cleanups can be enabled with -fexceptions even if the
1426 // language itself doesn't support exceptions.
1427 if (ExceptionsEnabled && DidHaveExplicitExceptionFlag)
1428 ShouldUseExceptionTables = true;
Daniel Dunbar30a12b82010-09-14 23:12:31 +00001429
Daniel Dunbarc44f8cf2011-03-17 23:28:31 +00001430 // Obj-C exceptions are enabled by default, regardless of -fexceptions. This
1431 // is not necessarily sensible, but follows GCC.
1432 if (types::isObjC(InputType) &&
Eric Christopher84fbdb42011-08-19 00:30:14 +00001433 Args.hasFlag(options::OPT_fobjc_exceptions,
Daniel Dunbarc44f8cf2011-03-17 23:28:31 +00001434 options::OPT_fno_objc_exceptions,
1435 true)) {
1436 CmdArgs.push_back("-fobjc-exceptions");
Anders Carlssone96ab552011-02-28 02:27:16 +00001437
Eric Christopher84fbdb42011-08-19 00:30:14 +00001438 ShouldUseExceptionTables |=
John McCall5fb5df92012-06-20 06:18:46 +00001439 shouldUseExceptionTablesForObjCExceptions(objcRuntime, Triple);
Anders Carlssone96ab552011-02-28 02:27:16 +00001440 }
1441
1442 if (types::isCXX(InputType)) {
1443 bool CXXExceptionsEnabled = ExceptionsEnabled;
1444
Eric Christopher84fbdb42011-08-19 00:30:14 +00001445 if (Arg *A = Args.getLastArg(options::OPT_fcxx_exceptions,
1446 options::OPT_fno_cxx_exceptions,
Anders Carlssone96ab552011-02-28 02:27:16 +00001447 options::OPT_fexceptions,
1448 options::OPT_fno_exceptions)) {
1449 if (A->getOption().matches(options::OPT_fcxx_exceptions))
1450 CXXExceptionsEnabled = true;
Chandler Carruth74f87112011-02-28 07:25:18 +00001451 else if (A->getOption().matches(options::OPT_fno_cxx_exceptions))
Anders Carlssone96ab552011-02-28 02:27:16 +00001452 CXXExceptionsEnabled = false;
1453 }
1454
1455 if (CXXExceptionsEnabled) {
1456 CmdArgs.push_back("-fcxx-exceptions");
1457
1458 ShouldUseExceptionTables = true;
1459 }
1460 }
1461
1462 if (ShouldUseExceptionTables)
1463 CmdArgs.push_back("-fexceptions");
Rafael Espindola00a66572009-10-01 13:33:33 +00001464}
1465
Daniel Dunbare246fbe2013-04-16 18:21:19 +00001466static bool ShouldDisableAutolink(const ArgList &Args,
1467 const ToolChain &TC) {
1468 bool Default = true;
1469 if (TC.getTriple().isOSDarwin()) {
1470 // The native darwin assembler doesn't support the linker_option directives,
1471 // so we disable them if we think the .s file will be passed to it.
1472 Default = TC.useIntegratedAs();
1473 }
1474 return !Args.hasFlag(options::OPT_fautolink, options::OPT_fno_autolink,
1475 Default);
1476}
1477
Rafael Espindola4cfa7972011-05-02 17:43:32 +00001478static bool ShouldDisableCFI(const ArgList &Args,
1479 const ToolChain &TC) {
Rafael Espindolac41db922012-03-08 14:39:55 +00001480 bool Default = true;
Bob Wilson6524dd32011-10-14 05:03:44 +00001481 if (TC.getTriple().isOSDarwin()) {
Rafael Espindolaf934f982011-05-17 16:26:17 +00001482 // The native darwin assembler doesn't support cfi directives, so
Rafael Espindola35ab91c2011-05-17 19:06:58 +00001483 // we disable them if we think the .s file will be passed to it.
Rafael Espindola84b588b2013-03-18 18:10:27 +00001484 Default = TC.useIntegratedAs();
Rafael Espindolaf934f982011-05-17 16:26:17 +00001485 }
Rafael Espindolac41db922012-03-08 14:39:55 +00001486 return !Args.hasFlag(options::OPT_fdwarf2_cfi_asm,
Eric Christopher45f2e712012-12-18 00:31:10 +00001487 options::OPT_fno_dwarf2_cfi_asm,
1488 Default);
Rafael Espindola4cfa7972011-05-02 17:43:32 +00001489}
1490
Ted Kremenek62093662013-03-12 17:02:12 +00001491static bool ShouldDisableDwarfDirectory(const ArgList &Args,
1492 const ToolChain &TC) {
Nick Lewycky1d617ac2011-10-17 23:05:52 +00001493 bool UseDwarfDirectory = Args.hasFlag(options::OPT_fdwarf_directory_asm,
1494 options::OPT_fno_dwarf_directory_asm,
Rafael Espindola84b588b2013-03-18 18:10:27 +00001495 TC.useIntegratedAs());
Nick Lewycky1d617ac2011-10-17 23:05:52 +00001496 return !UseDwarfDirectory;
1497}
1498
Joerg Sonnenbergeref317a22011-05-06 14:35:16 +00001499/// \brief Check whether the given input tree contains any compilation actions.
1500static bool ContainsCompileAction(const Action *A) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001501 if (isa<CompileJobAction>(A))
Joerg Sonnenbergeref317a22011-05-06 14:35:16 +00001502 return true;
1503
1504 for (Action::const_iterator it = A->begin(), ie = A->end(); it != ie; ++it)
1505 if (ContainsCompileAction(*it))
1506 return true;
1507
1508 return false;
1509}
1510
1511/// \brief Check if -relax-all should be passed to the internal assembler.
1512/// This is done by default when compiling non-assembler source with -O0.
1513static bool UseRelaxAll(Compilation &C, const ArgList &Args) {
1514 bool RelaxDefault = true;
1515
1516 if (Arg *A = Args.getLastArg(options::OPT_O_Group))
1517 RelaxDefault = A->getOption().matches(options::OPT_O0);
1518
1519 if (RelaxDefault) {
1520 RelaxDefault = false;
1521 for (ActionList::const_iterator it = C.getActions().begin(),
1522 ie = C.getActions().end(); it != ie; ++it) {
1523 if (ContainsCompileAction(*it)) {
1524 RelaxDefault = true;
1525 break;
1526 }
1527 }
1528 }
1529
1530 return Args.hasFlag(options::OPT_mrelax_all, options::OPT_mno_relax_all,
1531 RelaxDefault);
1532}
1533
Peter Collingbourne54d770c2013-04-09 04:35:11 +00001534SanitizerArgs::SanitizerArgs(const ToolChain &TC, const ArgList &Args)
Alexey Samsonov29524a92013-01-20 13:12:12 +00001535 : Kind(0), BlacklistFile(""), MsanTrackOrigins(false),
1536 AsanZeroBaseShadow(false) {
Alexey Samsonov2cbcf1a2013-01-28 07:20:44 +00001537 unsigned AllKinds = 0; // All kinds of sanitizers that were turned on
1538 // at least once (possibly, disabled further).
Peter Collingbourne54d770c2013-04-09 04:35:11 +00001539 const Driver &D = TC.getDriver();
Richard Smith52be6192012-11-05 22:04:41 +00001540 for (ArgList::const_iterator I = Args.begin(), E = Args.end(); I != E; ++I) {
Alexey Samsonov53f7e122012-11-28 17:34:24 +00001541 unsigned Add, Remove;
1542 if (!parse(D, Args, *I, Add, Remove, true))
Richard Smith52be6192012-11-05 22:04:41 +00001543 continue;
Richard Smith52be6192012-11-05 22:04:41 +00001544 (*I)->claim();
Alexey Samsonov627b10f2012-11-06 15:09:03 +00001545 Kind |= Add;
1546 Kind &= ~Remove;
Alexey Samsonov2cbcf1a2013-01-28 07:20:44 +00001547 AllKinds |= Add;
Richard Smith52be6192012-11-05 22:04:41 +00001548 }
1549
Chad Rosierae229d52013-01-29 23:31:22 +00001550 UbsanTrapOnError =
1551 Args.hasArg(options::OPT_fcatch_undefined_behavior) ||
1552 Args.hasFlag(options::OPT_fsanitize_undefined_trap_on_error,
1553 options::OPT_fno_sanitize_undefined_trap_on_error, false);
1554
1555 if (Args.hasArg(options::OPT_fcatch_undefined_behavior) &&
1556 !Args.hasFlag(options::OPT_fsanitize_undefined_trap_on_error,
1557 options::OPT_fno_sanitize_undefined_trap_on_error, true)) {
1558 D.Diag(diag::err_drv_argument_not_allowed_with)
1559 << "-fcatch-undefined-behavior"
1560 << "-fno-sanitize-undefined-trap-on-error";
1561 }
1562
1563 // Warn about undefined sanitizer options that require runtime support.
1564 if (UbsanTrapOnError && notAllowedWithTrap()) {
1565 if (Args.hasArg(options::OPT_fcatch_undefined_behavior))
1566 D.Diag(diag::err_drv_argument_not_allowed_with)
1567 << lastArgumentForKind(D, Args, NotAllowedWithTrap)
1568 << "-fcatch-undefined-behavior";
1569 else if (Args.hasFlag(options::OPT_fsanitize_undefined_trap_on_error,
1570 options::OPT_fno_sanitize_undefined_trap_on_error,
1571 false))
1572 D.Diag(diag::err_drv_argument_not_allowed_with)
1573 << lastArgumentForKind(D, Args, NotAllowedWithTrap)
1574 << "-fsanitize-undefined-trap-on-error";
1575 }
1576
Richard Smith52be6192012-11-05 22:04:41 +00001577 // Only one runtime library can be used at once.
Alexey Samsonov627b10f2012-11-06 15:09:03 +00001578 bool NeedsAsan = needsAsanRt();
1579 bool NeedsTsan = needsTsanRt();
Evgeniy Stepanovcdc22552012-12-05 13:37:12 +00001580 bool NeedsMsan = needsMsanRt();
Richard Smith06d87f12012-12-01 01:02:45 +00001581 if (NeedsAsan && NeedsTsan)
Richard Smith52be6192012-11-05 22:04:41 +00001582 D.Diag(diag::err_drv_argument_not_allowed_with)
Richard Smith06d87f12012-12-01 01:02:45 +00001583 << lastArgumentForKind(D, Args, NeedsAsanRt)
1584 << lastArgumentForKind(D, Args, NeedsTsanRt);
Evgeniy Stepanovcdc22552012-12-05 13:37:12 +00001585 if (NeedsAsan && NeedsMsan)
1586 D.Diag(diag::err_drv_argument_not_allowed_with)
1587 << lastArgumentForKind(D, Args, NeedsAsanRt)
1588 << lastArgumentForKind(D, Args, NeedsMsanRt);
1589 if (NeedsTsan && NeedsMsan)
1590 D.Diag(diag::err_drv_argument_not_allowed_with)
1591 << lastArgumentForKind(D, Args, NeedsTsanRt)
1592 << lastArgumentForKind(D, Args, NeedsMsanRt);
Alexey Samsonov0e96bec2012-11-29 22:36:21 +00001593
1594 // If -fsanitize contains extra features of ASan, it should also
Alexey Samsonov2cbcf1a2013-01-28 07:20:44 +00001595 // explicitly contain -fsanitize=address (probably, turned off later in the
1596 // command line).
1597 if ((Kind & AddressFull) != 0 && (AllKinds & Address) == 0)
1598 D.Diag(diag::warn_drv_unused_sanitizer)
1599 << lastArgumentForKind(D, Args, AddressFull)
1600 << "-fsanitize=address";
Alexey Samsonov9ab73622012-12-03 19:12:58 +00001601
1602 // Parse -f(no-)sanitize-blacklist options.
1603 if (Arg *BLArg = Args.getLastArg(options::OPT_fsanitize_blacklist,
1604 options::OPT_fno_sanitize_blacklist)) {
1605 if (BLArg->getOption().matches(options::OPT_fsanitize_blacklist)) {
1606 std::string BLPath = BLArg->getValue();
1607 bool BLExists = false;
1608 if (!llvm::sys::fs::exists(BLPath, BLExists) && BLExists)
1609 BlacklistFile = BLPath;
1610 else
1611 D.Diag(diag::err_drv_no_such_file) << BLPath;
1612 }
Alexey Samsonovd26b3332013-02-19 11:25:29 +00001613 } else {
1614 // If no -fsanitize-blacklist option is specified, try to look up for
1615 // blacklist in the resource directory.
1616 std::string BLPath;
1617 bool BLExists = false;
1618 if (getDefaultBlacklistForKind(D, Kind, BLPath) &&
1619 !llvm::sys::fs::exists(BLPath, BLExists) && BLExists)
1620 BlacklistFile = BLPath;
Alexey Samsonov9ab73622012-12-03 19:12:58 +00001621 }
Evgeniy Stepanovad8ab3d2012-12-24 08:42:34 +00001622
1623 // Parse -f(no-)sanitize-memory-track-origins options.
Alexey Samsonov29524a92013-01-20 13:12:12 +00001624 if (NeedsMsan)
Evgeniy Stepanovad8ab3d2012-12-24 08:42:34 +00001625 MsanTrackOrigins =
1626 Args.hasFlag(options::OPT_fsanitize_memory_track_origins,
1627 options::OPT_fno_sanitize_memory_track_origins,
1628 /* Default */false);
Alexey Samsonov29524a92013-01-20 13:12:12 +00001629
1630 // Parse -f(no-)sanitize-address-zero-base-shadow options.
Alexey Samsonov7e434072013-04-09 07:27:44 +00001631 if (NeedsAsan) {
1632 bool IsAndroid = (TC.getTriple().getEnvironment() == llvm::Triple::Android);
1633 bool ZeroBaseShadowDefault = IsAndroid;
Alexey Samsonov29524a92013-01-20 13:12:12 +00001634 AsanZeroBaseShadow =
Alexey Samsonov7e434072013-04-09 07:27:44 +00001635 Args.hasFlag(options::OPT_fsanitize_address_zero_base_shadow,
1636 options::OPT_fno_sanitize_address_zero_base_shadow,
1637 ZeroBaseShadowDefault);
1638 // Zero-base shadow is a requirement on Android.
1639 if (IsAndroid && !AsanZeroBaseShadow) {
1640 D.Diag(diag::err_drv_argument_not_allowed_with)
1641 << "-fno-sanitize-address-zero-base-shadow"
1642 << lastArgumentForKind(D, Args, Address);
1643 }
1644 }
Richard Smith52be6192012-11-05 22:04:41 +00001645}
1646
Alexey Samsonovbfb0cd32013-02-27 11:14:55 +00001647static void addSanitizerRTLinkFlagsLinux(
1648 const ToolChain &TC, const ArgList &Args, ArgStringList &CmdArgs,
Richard Smithf3e624c2013-03-23 00:30:08 +00001649 const StringRef Sanitizer, bool BeforeLibStdCXX,
1650 bool ExportSymbols = true) {
Alexey Samsonovbfb0cd32013-02-27 11:14:55 +00001651 // Sanitizer runtime is located in the Linux library directory and
1652 // has name "libclang_rt.<Sanitizer>-<ArchName>.a".
1653 SmallString<128> LibSanitizer(TC.getDriver().ResourceDir);
1654 llvm::sys::path::append(
1655 LibSanitizer, "lib", "linux",
1656 (Twine("libclang_rt.") + Sanitizer + "-" + TC.getArchName() + ".a"));
Richard Smithcff3cde2013-03-20 23:49:07 +00001657
Alexey Samsonovbfb0cd32013-02-27 11:14:55 +00001658 // Sanitizer runtime may need to come before -lstdc++ (or -lc++, libstdc++.a,
1659 // etc.) so that the linker picks custom versions of the global 'operator
1660 // new' and 'operator delete' symbols. We take the extreme (but simple)
1661 // strategy of inserting it at the front of the link command. It also
1662 // needs to be forced to end up in the executable, so wrap it in
1663 // whole-archive.
Richard Smithcff3cde2013-03-20 23:49:07 +00001664 SmallVector<const char *, 3> LibSanitizerArgs;
1665 LibSanitizerArgs.push_back("-whole-archive");
1666 LibSanitizerArgs.push_back(Args.MakeArgString(LibSanitizer));
1667 LibSanitizerArgs.push_back("-no-whole-archive");
1668
1669 CmdArgs.insert(BeforeLibStdCXX ? CmdArgs.begin() : CmdArgs.end(),
1670 LibSanitizerArgs.begin(), LibSanitizerArgs.end());
1671
Alexey Samsonovbfb0cd32013-02-27 11:14:55 +00001672 CmdArgs.push_back("-lpthread");
1673 CmdArgs.push_back("-ldl");
Richard Smithf3e624c2013-03-23 00:30:08 +00001674
1675 // If possible, use a dynamic symbols file to export the symbols from the
1676 // runtime library. If we can't do so, use -export-dynamic instead to export
1677 // all symbols from the binary.
1678 if (ExportSymbols) {
1679 if (llvm::sys::fs::exists(LibSanitizer + ".syms"))
1680 CmdArgs.push_back(
1681 Args.MakeArgString("--dynamic-list=" + LibSanitizer + ".syms"));
1682 else
1683 CmdArgs.push_back("-export-dynamic");
1684 }
Alexey Samsonovbfb0cd32013-02-27 11:14:55 +00001685}
1686
Kostya Serebryany7bca6c22011-11-30 01:39:16 +00001687/// If AddressSanitizer is enabled, add appropriate linker flags (Linux).
1688/// This needs to be called before we add the C run-time (malloc, etc).
1689static void addAsanRTLinux(const ToolChain &TC, const ArgList &Args,
Kostya Serebryany0b692ce2011-12-06 19:18:44 +00001690 ArgStringList &CmdArgs) {
Logan Chienc6fd8202012-09-02 09:30:11 +00001691 if(TC.getTriple().getEnvironment() == llvm::Triple::Android) {
Evgeniy Stepanovcac6aaa2012-09-12 09:09:08 +00001692 SmallString<128> LibAsan(TC.getDriver().ResourceDir);
1693 llvm::sys::path::append(LibAsan, "lib", "linux",
1694 (Twine("libclang_rt.asan-") +
1695 TC.getArchName() + "-android.so"));
Matt Beaumont-Gay35439df2012-12-04 21:18:26 +00001696 CmdArgs.insert(CmdArgs.begin(), Args.MakeArgString(LibAsan));
Evgeniy Stepanov77866712012-04-25 08:59:22 +00001697 } else {
1698 if (!Args.hasArg(options::OPT_shared)) {
Alexey Samsonovbfb0cd32013-02-27 11:14:55 +00001699 addSanitizerRTLinkFlagsLinux(TC, Args, CmdArgs, "asan", true);
Evgeniy Stepanov77866712012-04-25 08:59:22 +00001700 }
1701 }
Kostya Serebryany7bca6c22011-11-30 01:39:16 +00001702}
1703
Kostya Serebryany66e8fab2012-05-16 06:36:00 +00001704/// If ThreadSanitizer is enabled, add appropriate linker flags (Linux).
1705/// This needs to be called before we add the C run-time (malloc, etc).
1706static void addTsanRTLinux(const ToolChain &TC, const ArgList &Args,
1707 ArgStringList &CmdArgs) {
Kostya Serebryany66e8fab2012-05-16 06:36:00 +00001708 if (!Args.hasArg(options::OPT_shared)) {
Alexey Samsonovbfb0cd32013-02-27 11:14:55 +00001709 addSanitizerRTLinkFlagsLinux(TC, Args, CmdArgs, "tsan", true);
Kostya Serebryany66e8fab2012-05-16 06:36:00 +00001710 }
1711}
1712
Evgeniy Stepanovaea92e52012-12-03 13:20:43 +00001713/// If MemorySanitizer is enabled, add appropriate linker flags (Linux).
1714/// This needs to be called before we add the C run-time (malloc, etc).
1715static void addMsanRTLinux(const ToolChain &TC, const ArgList &Args,
1716 ArgStringList &CmdArgs) {
1717 if (!Args.hasArg(options::OPT_shared)) {
Alexey Samsonovbfb0cd32013-02-27 11:14:55 +00001718 addSanitizerRTLinkFlagsLinux(TC, Args, CmdArgs, "msan", true);
Evgeniy Stepanovaea92e52012-12-03 13:20:43 +00001719 }
1720}
1721
Richard Smithe30752c2012-10-09 19:52:38 +00001722/// If UndefinedBehaviorSanitizer is enabled, add appropriate linker flags
1723/// (Linux).
1724static void addUbsanRTLinux(const ToolChain &TC, const ArgList &Args,
Richard Smithcff3cde2013-03-20 23:49:07 +00001725 ArgStringList &CmdArgs, bool IsCXX,
1726 bool HasOtherSanitizerRt) {
1727 if (Args.hasArg(options::OPT_shared))
1728 return;
1729
1730 // Need a copy of sanitizer_common. This could come from another sanitizer
1731 // runtime; if we're not including one, include our own copy.
1732 if (!HasOtherSanitizerRt)
Richard Smithf3e624c2013-03-23 00:30:08 +00001733 addSanitizerRTLinkFlagsLinux(TC, Args, CmdArgs, "san", true, false);
Richard Smithcff3cde2013-03-20 23:49:07 +00001734
Alexey Samsonovbfb0cd32013-02-27 11:14:55 +00001735 addSanitizerRTLinkFlagsLinux(TC, Args, CmdArgs, "ubsan", false);
Richard Smithcff3cde2013-03-20 23:49:07 +00001736
1737 // Only include the bits of the runtime which need a C++ ABI library if
1738 // we're linking in C++ mode.
1739 if (IsCXX)
1740 addSanitizerRTLinkFlagsLinux(TC, Args, CmdArgs, "ubsan_cxx", false);
Richard Smithe30752c2012-10-09 19:52:38 +00001741}
1742
Rafael Espindola224dd632011-12-14 21:02:23 +00001743static bool shouldUseFramePointer(const ArgList &Args,
1744 const llvm::Triple &Triple) {
1745 if (Arg *A = Args.getLastArg(options::OPT_fno_omit_frame_pointer,
1746 options::OPT_fomit_frame_pointer))
1747 return A->getOption().matches(options::OPT_fno_omit_frame_pointer);
1748
Rafael Espindola00b29182011-12-14 21:50:24 +00001749 // Don't use a frame pointer on linux x86 and x86_64 if optimizing.
Rafael Espindola224dd632011-12-14 21:02:23 +00001750 if ((Triple.getArch() == llvm::Triple::x86_64 ||
1751 Triple.getArch() == llvm::Triple::x86) &&
1752 Triple.getOS() == llvm::Triple::Linux) {
1753 if (Arg *A = Args.getLastArg(options::OPT_O_Group))
1754 if (!A->getOption().matches(options::OPT_O0))
1755 return false;
1756 }
1757
1758 return true;
1759}
1760
Eric Christopherb7d97e92013-04-03 01:58:53 +00001761static bool shouldUseLeafFramePointer(const ArgList &Args,
1762 const llvm::Triple &Triple) {
1763 if (Arg *A = Args.getLastArg(options::OPT_mno_omit_leaf_frame_pointer,
1764 options::OPT_momit_leaf_frame_pointer))
1765 return A->getOption().matches(options::OPT_mno_omit_leaf_frame_pointer);
1766
1767 // Don't use a leaf frame pointer on linux x86 and x86_64 if optimizing.
1768 if ((Triple.getArch() == llvm::Triple::x86_64 ||
1769 Triple.getArch() == llvm::Triple::x86) &&
1770 Triple.getOS() == llvm::Triple::Linux) {
1771 if (Arg *A = Args.getLastArg(options::OPT_O_Group))
1772 if (!A->getOption().matches(options::OPT_O0))
1773 return false;
1774 }
1775
1776 return true;
1777}
1778
Chandler Carruth4d5e1a92012-12-17 21:40:04 +00001779/// If the PWD environment variable is set, add a CC1 option to specify the
1780/// debug compilation directory.
1781static void addDebugCompDirArg(const ArgList &Args, ArgStringList &CmdArgs) {
Chad Rosiera35d5a32013-04-26 20:49:50 +00001782 struct stat StatPWDBuf, StatDotBuf;
1783
1784 const char *pwd;
1785 if ((pwd = ::getenv("PWD")) != 0 &&
1786 llvm::sys::path::is_absolute(pwd) &&
1787 stat(pwd, &StatPWDBuf) == 0 &&
1788 stat(".", &StatDotBuf) == 0 &&
1789 StatPWDBuf.st_ino == StatDotBuf.st_ino &&
1790 StatPWDBuf.st_dev == StatDotBuf.st_dev) {
1791 CmdArgs.push_back("-fdebug-compilation-dir");
1792 CmdArgs.push_back(Args.MakeArgString(pwd));
1793 return;
1794 }
1795 // Fall back to using getcwd.
1796 char cwd[MAXPATHLEN];
1797 if (pwd && ::getcwd(cwd, MAXPATHLEN)) {
1798 CmdArgs.push_back("-fdebug-compilation-dir");
1799 CmdArgs.push_back(Args.MakeArgString(cwd));
Chandler Carruth4d5e1a92012-12-17 21:40:04 +00001800 }
1801}
1802
Eric Christopherd3804002013-02-22 20:12:52 +00001803static const char *SplitDebugName(const ArgList &Args,
1804 const InputInfoList &Inputs) {
1805 Arg *FinalOutput = Args.getLastArg(options::OPT_o);
1806 if (FinalOutput && Args.hasArg(options::OPT_c)) {
1807 SmallString<128> T(FinalOutput->getValue());
1808 llvm::sys::path::replace_extension(T, "dwo");
1809 return Args.MakeArgString(T);
1810 } else {
1811 // Use the compilation dir.
1812 SmallString<128> T(Args.getLastArgValue(options::OPT_fdebug_compilation_dir));
1813 SmallString<128> F(llvm::sys::path::stem(Inputs[0].getBaseInput()));
1814 llvm::sys::path::replace_extension(F, "dwo");
1815 T += F;
1816 return Args.MakeArgString(F);
1817 }
1818}
1819
1820static void SplitDebugInfo(const ToolChain &TC, Compilation &C,
1821 const Tool &T, const JobAction &JA,
1822 const ArgList &Args, const InputInfo &Output,
1823 const char *OutFile) {
Eric Christopher248357f2013-02-21 22:35:01 +00001824 ArgStringList ExtractArgs;
1825 ExtractArgs.push_back("--extract-dwo");
1826
1827 ArgStringList StripArgs;
1828 StripArgs.push_back("--strip-dwo");
1829
1830 // Grabbing the output of the earlier compile step.
1831 StripArgs.push_back(Output.getFilename());
1832 ExtractArgs.push_back(Output.getFilename());
Eric Christopher248357f2013-02-21 22:35:01 +00001833 ExtractArgs.push_back(OutFile);
1834
1835 const char *Exec =
Eric Christopherd3804002013-02-22 20:12:52 +00001836 Args.MakeArgString(TC.GetProgramPath("objcopy"));
Eric Christopher248357f2013-02-21 22:35:01 +00001837
1838 // First extract the dwo sections.
Eric Christopherd3804002013-02-22 20:12:52 +00001839 C.addCommand(new Command(JA, T, Exec, ExtractArgs));
Eric Christopher248357f2013-02-21 22:35:01 +00001840
1841 // Then remove them from the original .o file.
Eric Christopherd3804002013-02-22 20:12:52 +00001842 C.addCommand(new Command(JA, T, Exec, StripArgs));
Eric Christopher248357f2013-02-21 22:35:01 +00001843}
1844
Chad Rosierb71f6aa2013-04-24 18:09:54 +00001845static bool isOptimizationLevelFast(const ArgList &Args) {
1846 if (Arg *A = Args.getLastArg(options::OPT_O_Group))
1847 if (A->getOption().matches(options::OPT_Ofast))
1848 return true;
1849 return false;
1850}
1851
Daniel Dunbar1a093d22009-03-18 06:00:36 +00001852void Clang::ConstructJob(Compilation &C, const JobAction &JA,
Daniel Dunbar04c4c2c2009-03-18 07:06:02 +00001853 const InputInfo &Output,
Daniel Dunbar0450e6d2009-03-18 06:07:59 +00001854 const InputInfoList &Inputs,
Daniel Dunbara2aedc62009-03-18 10:01:51 +00001855 const ArgList &Args,
Daniel Dunbar1a093d22009-03-18 06:00:36 +00001856 const char *LinkingOutput) const {
Daniel Dunbare46b52a2010-03-20 04:52:14 +00001857 bool KernelOrKext = Args.hasArg(options::OPT_mkernel,
1858 options::OPT_fapple_kext);
Daniel Dunbar083edf72009-12-21 18:54:17 +00001859 const Driver &D = getToolChain().getDriver();
Daniel Dunbara3246a02009-03-18 08:07:30 +00001860 ArgStringList CmdArgs;
1861
Daniel Dunbare521a892009-03-31 20:53:55 +00001862 assert(Inputs.size() == 1 && "Unable to handle multiple inputs.");
1863
Daniel Dunbar6c536aa2009-12-11 23:00:49 +00001864 // Invoke ourselves in -cc1 mode.
1865 //
1866 // FIXME: Implement custom jobs for internal actions.
1867 CmdArgs.push_back("-cc1");
1868
Daniel Dunbar624c21b2009-10-30 18:12:20 +00001869 // Add the "effective" target triple.
Daniel Dunbard640be22009-03-31 17:35:15 +00001870 CmdArgs.push_back("-triple");
Daniel Dunbar82eb4ce2010-08-23 22:35:37 +00001871 std::string TripleStr = getToolChain().ComputeEffectiveClangTriple(Args);
Daniel Dunbar624c21b2009-10-30 18:12:20 +00001872 CmdArgs.push_back(Args.MakeArgString(TripleStr));
Daniel Dunbarfb58b0a2009-09-10 06:49:20 +00001873
Daniel Dunbar624c21b2009-10-30 18:12:20 +00001874 // Select the appropriate action.
John McCall5fb5df92012-06-20 06:18:46 +00001875 RewriteKind rewriteKind = RK_None;
Fariborz Jahanian7ebeede2012-04-04 18:50:28 +00001876
Daniel Dunbara2aedc62009-03-18 10:01:51 +00001877 if (isa<AnalyzeJobAction>(JA)) {
1878 assert(JA.getType() == types::TY_Plist && "Invalid output type.");
1879 CmdArgs.push_back("-analyze");
Ted Kremenekf7639e12012-03-06 20:06:33 +00001880 } else if (isa<MigrateJobAction>(JA)) {
1881 CmdArgs.push_back("-migrate");
Daniel Dunbara2aedc62009-03-18 10:01:51 +00001882 } else if (isa<PreprocessJobAction>(JA)) {
Daniel Dunbard67a3222009-03-30 06:36:42 +00001883 if (Output.getType() == types::TY_Dependencies)
1884 CmdArgs.push_back("-Eonly");
Fariborz Jahaniane0a75d62013-03-18 19:41:18 +00001885 else {
Daniel Dunbard67a3222009-03-30 06:36:42 +00001886 CmdArgs.push_back("-E");
Fariborz Jahaniane0a75d62013-03-18 19:41:18 +00001887 if (Args.hasArg(options::OPT_rewrite_objc) &&
1888 !Args.hasArg(options::OPT_g_Group))
1889 CmdArgs.push_back("-P");
1890 }
Daniel Dunbarc4343942010-02-03 03:07:56 +00001891 } else if (isa<AssembleJobAction>(JA)) {
1892 CmdArgs.push_back("-emit-obj");
Daniel Dunbar06e2cc32010-05-27 06:18:05 +00001893
Joerg Sonnenbergeref317a22011-05-06 14:35:16 +00001894 if (UseRelaxAll(C, Args))
Daniel Dunbar06e2cc32010-05-27 06:18:05 +00001895 CmdArgs.push_back("-mrelax-all");
Daniel Dunbarfe6c97b2010-08-24 16:47:49 +00001896
Daniel Dunbar2b4de142010-10-18 22:36:15 +00001897 // When using an integrated assembler, translate -Wa, and -Xassembler
1898 // options.
1899 for (arg_iterator it = Args.filtered_begin(options::OPT_Wa_COMMA,
1900 options::OPT_Xassembler),
1901 ie = Args.filtered_end(); it != ie; ++it) {
1902 const Arg *A = *it;
1903 A->claim();
1904
1905 for (unsigned i = 0, e = A->getNumValues(); i != e; ++i) {
Richard Smithbd55daf2012-11-01 04:30:05 +00001906 StringRef Value = A->getValue(i);
Daniel Dunbar2b4de142010-10-18 22:36:15 +00001907
1908 if (Value == "-force_cpusubtype_ALL") {
1909 // Do nothing, this is the default and we don't support anything else.
Daniel Dunbara78e5892010-10-28 20:36:23 +00001910 } else if (Value == "-L") {
Daniel Dunbar67919b22011-03-28 22:49:28 +00001911 CmdArgs.push_back("-msave-temp-labels");
Joerg Sonnenberger3028e462011-05-19 20:46:39 +00001912 } else if (Value == "--fatal-warnings") {
Joerg Sonnenbergerb487d2d2011-05-19 18:42:29 +00001913 CmdArgs.push_back("-mllvm");
1914 CmdArgs.push_back("-fatal-assembler-warnings");
Nick Lewyckyca6b90d2011-06-21 00:14:18 +00001915 } else if (Value == "--noexecstack") {
1916 CmdArgs.push_back("-mnoexecstack");
Daniel Dunbar2b4de142010-10-18 22:36:15 +00001917 } else {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001918 D.Diag(diag::err_drv_unsupported_option_argument)
Daniel Dunbar2b4de142010-10-18 22:36:15 +00001919 << A->getOption().getName() << Value;
1920 }
1921 }
1922 }
Daniel Dunbar7c874332010-11-19 16:23:35 +00001923
1924 // Also ignore explicit -force_cpusubtype_ALL option.
1925 (void) Args.hasArg(options::OPT_force__cpusubtype__ALL);
Daniel Dunbara2aedc62009-03-18 10:01:51 +00001926 } else if (isa<PrecompileJobAction>(JA)) {
Argyrios Kyrtzidis90bdfbb2010-08-11 23:27:58 +00001927 // Use PCH if the user requested it.
Daniel Dunbarcbc34b72009-10-15 20:02:44 +00001928 bool UsePCH = D.CCCUsePCH;
Daniel Dunbarcbc34b72009-10-15 20:02:44 +00001929
Aaron Ballman1f10cc52012-07-31 01:21:00 +00001930 if (JA.getType() == types::TY_Nothing)
1931 CmdArgs.push_back("-fsyntax-only");
1932 else if (UsePCH)
Douglas Gregor111af7d2009-04-18 00:34:01 +00001933 CmdArgs.push_back("-emit-pch");
1934 else
1935 CmdArgs.push_back("-emit-pth");
Daniel Dunbara2aedc62009-03-18 10:01:51 +00001936 } else {
1937 assert(isa<CompileJobAction>(JA) && "Invalid action for clang tool.");
Daniel Dunbard067f7f2009-04-08 23:54:23 +00001938
Daniel Dunbara2aedc62009-03-18 10:01:51 +00001939 if (JA.getType() == types::TY_Nothing) {
1940 CmdArgs.push_back("-fsyntax-only");
Daniel Dunbar24e52992010-06-07 23:28:45 +00001941 } else if (JA.getType() == types::TY_LLVM_IR ||
1942 JA.getType() == types::TY_LTO_IR) {
Daniel Dunbara2aedc62009-03-18 10:01:51 +00001943 CmdArgs.push_back("-emit-llvm");
Daniel Dunbar24e52992010-06-07 23:28:45 +00001944 } else if (JA.getType() == types::TY_LLVM_BC ||
1945 JA.getType() == types::TY_LTO_BC) {
Daniel Dunbara2aedc62009-03-18 10:01:51 +00001946 CmdArgs.push_back("-emit-llvm-bc");
1947 } else if (JA.getType() == types::TY_PP_Asm) {
Daniel Dunbard112f102009-09-17 00:47:53 +00001948 CmdArgs.push_back("-S");
Daniel Dunbar6cdf83c2009-09-01 16:57:46 +00001949 } else if (JA.getType() == types::TY_AST) {
1950 CmdArgs.push_back("-emit-pch");
Douglas Gregorbf7fc9c2013-03-27 16:47:18 +00001951 } else if (JA.getType() == types::TY_ModuleFile) {
1952 CmdArgs.push_back("-module-file-info");
Daniel Dunbaraeea8ac2010-02-11 03:16:21 +00001953 } else if (JA.getType() == types::TY_RewrittenObjC) {
1954 CmdArgs.push_back("-rewrite-objc");
John McCall5fb5df92012-06-20 06:18:46 +00001955 rewriteKind = RK_NonFragile;
Fariborz Jahanian73223bb2012-04-02 15:59:19 +00001956 } else if (JA.getType() == types::TY_RewrittenLegacyObjC) {
1957 CmdArgs.push_back("-rewrite-objc");
John McCall5fb5df92012-06-20 06:18:46 +00001958 rewriteKind = RK_Fragile;
Daniel Dunbaraeea8ac2010-02-11 03:16:21 +00001959 } else {
1960 assert(JA.getType() == types::TY_PP_Asm &&
1961 "Unexpected output type!");
Daniel Dunbara2aedc62009-03-18 10:01:51 +00001962 }
Daniel Dunbara3246a02009-03-18 08:07:30 +00001963 }
1964
Daniel Dunbara2aedc62009-03-18 10:01:51 +00001965 // The make clang go fast button.
1966 CmdArgs.push_back("-disable-free");
1967
John McCallbb79b5f2010-02-13 03:50:24 +00001968 // Disable the verification pass in -asserts builds.
1969#ifdef NDEBUG
1970 CmdArgs.push_back("-disable-llvm-verifier");
1971#endif
1972
Daniel Dunbar3b358a32009-04-08 05:11:16 +00001973 // Set the main file name, so that debug info works even with
1974 // -save-temps.
1975 CmdArgs.push_back("-main-file-name");
Bob Wilsondecc03e2012-11-23 06:14:39 +00001976 CmdArgs.push_back(getBaseInputName(Args, Inputs));
Daniel Dunbar3b358a32009-04-08 05:11:16 +00001977
Daniel Dunbar17ddaa62009-04-08 18:03:55 +00001978 // Some flags which affect the language (via preprocessor
Bob Wilsondecc03e2012-11-23 06:14:39 +00001979 // defines).
Daniel Dunbar17ddaa62009-04-08 18:03:55 +00001980 if (Args.hasArg(options::OPT_static))
1981 CmdArgs.push_back("-static-define");
1982
Daniel Dunbara2aedc62009-03-18 10:01:51 +00001983 if (isa<AnalyzeJobAction>(JA)) {
Ted Kremenek05e6f5b2009-09-25 05:55:59 +00001984 // Enable region store model by default.
1985 CmdArgs.push_back("-analyzer-store=region");
1986
Ted Kremenek7bea9a12009-12-07 22:26:14 +00001987 // Treat blocks as analysis entry points.
1988 CmdArgs.push_back("-analyzer-opt-analyze-nested-blocks");
1989
Ted Kremenek49c79792011-03-24 00:28:47 +00001990 CmdArgs.push_back("-analyzer-eagerly-assume");
1991
Daniel Dunbara2aedc62009-03-18 10:01:51 +00001992 // Add default argument set.
Daniel Dunbar58f345ce2009-05-22 00:38:15 +00001993 if (!Args.hasArg(options::OPT__analyzer_no_default_checks)) {
Argyrios Kyrtzidisa6d04d52011-02-15 07:42:33 +00001994 CmdArgs.push_back("-analyzer-checker=core");
Ted Kremenek49c79792011-03-24 00:28:47 +00001995
Argyrios Kyrtzidisa6d04d52011-02-15 07:42:33 +00001996 if (getToolChain().getTriple().getOS() != llvm::Triple::Win32)
1997 CmdArgs.push_back("-analyzer-checker=unix");
Ted Kremenek49c79792011-03-24 00:28:47 +00001998
Argyrios Kyrtzidisa6d04d52011-02-15 07:42:33 +00001999 if (getToolChain().getTriple().getVendor() == llvm::Triple::Apple)
Ted Kremenek49c79792011-03-24 00:28:47 +00002000 CmdArgs.push_back("-analyzer-checker=osx");
Ted Kremenekb9ff6b22012-01-20 06:00:17 +00002001
2002 CmdArgs.push_back("-analyzer-checker=deadcode");
Ted Kremenek37e96522012-01-26 02:27:38 +00002003
Jordan Rose10ad0812013-04-05 17:55:07 +00002004 if (types::isCXX(Inputs[0].getType()))
2005 CmdArgs.push_back("-analyzer-checker=cplusplus");
2006
Ted Kremenek37e96522012-01-26 02:27:38 +00002007 // Enable the following experimental checkers for testing.
Ted Kremenek37e96522012-01-26 02:27:38 +00002008 CmdArgs.push_back("-analyzer-checker=security.insecureAPI.UncheckedReturn");
2009 CmdArgs.push_back("-analyzer-checker=security.insecureAPI.getpw");
2010 CmdArgs.push_back("-analyzer-checker=security.insecureAPI.gets");
2011 CmdArgs.push_back("-analyzer-checker=security.insecureAPI.mktemp");
2012 CmdArgs.push_back("-analyzer-checker=security.insecureAPI.mkstemp");
2013 CmdArgs.push_back("-analyzer-checker=security.insecureAPI.vfork");
Daniel Dunbar58f345ce2009-05-22 00:38:15 +00002014 }
Daniel Dunbard067f7f2009-04-08 23:54:23 +00002015
Daniel Dunbar58f345ce2009-05-22 00:38:15 +00002016 // Set the output format. The default is plist, for (lame) historical
2017 // reasons.
2018 CmdArgs.push_back("-analyzer-output");
2019 if (Arg *A = Args.getLastArg(options::OPT__analyzer_output))
Richard Smithbd55daf2012-11-01 04:30:05 +00002020 CmdArgs.push_back(A->getValue());
Daniel Dunbar58f345ce2009-05-22 00:38:15 +00002021 else
2022 CmdArgs.push_back("plist");
Daniel Dunbara2aedc62009-03-18 10:01:51 +00002023
Ted Kremenekfe449a22010-03-22 22:32:05 +00002024 // Disable the presentation of standard compiler warnings when
2025 // using --analyze. We only want to show static analyzer diagnostics
2026 // or frontend errors.
2027 CmdArgs.push_back("-w");
2028
Daniel Dunbara2aedc62009-03-18 10:01:51 +00002029 // Add -Xanalyzer arguments when running as analyzer.
2030 Args.AddAllArgValues(CmdArgs, options::OPT_Xanalyzer);
Mike Stump11289f42009-09-09 15:08:12 +00002031 }
2032
Daniel Dunbar4eadb602009-09-10 01:21:12 +00002033 CheckCodeGenerationOptions(D, Args);
2034
Peter Collingbourne54d770c2013-04-09 04:35:11 +00002035 bool PIE = getToolChain().isPIEDefault();
2036 bool PIC = PIE || getToolChain().isPICDefault();
Chandler Carruth76a943b2012-11-19 03:52:03 +00002037 bool IsPICLevelTwo = PIC;
Peter Collingbourne54d770c2013-04-09 04:35:11 +00002038
Alexey Samsonov090301e2013-04-09 12:28:19 +00002039 // For the PIC and PIE flag options, this logic is different from the
2040 // legacy logic in very old versions of GCC, as that logic was just
2041 // a bug no one had ever fixed. This logic is both more rational and
2042 // consistent with GCC's new logic now that the bugs are fixed. The last
2043 // argument relating to either PIC or PIE wins, and no other argument is
2044 // used. If the last argument is any flavor of the '-fno-...' arguments,
2045 // both PIC and PIE are disabled. Any PIE option implicitly enables PIC
2046 // at the same level.
2047 Arg *LastPICArg =Args.getLastArg(options::OPT_fPIC, options::OPT_fno_PIC,
2048 options::OPT_fpic, options::OPT_fno_pic,
2049 options::OPT_fPIE, options::OPT_fno_PIE,
2050 options::OPT_fpie, options::OPT_fno_pie);
Chandler Carruth76a943b2012-11-19 03:52:03 +00002051 // Check whether the tool chain trumps the PIC-ness decision. If the PIC-ness
2052 // is forced, then neither PIC nor PIE flags will have no effect.
Peter Collingbourne54d770c2013-04-09 04:35:11 +00002053 if (!getToolChain().isPICDefaultForced()) {
Alexey Samsonov090301e2013-04-09 12:28:19 +00002054 if (LastPICArg) {
2055 Option O = LastPICArg->getOption();
Peter Collingbourne54d770c2013-04-09 04:35:11 +00002056 if (O.matches(options::OPT_fPIC) || O.matches(options::OPT_fpic) ||
2057 O.matches(options::OPT_fPIE) || O.matches(options::OPT_fpie)) {
2058 PIE = O.matches(options::OPT_fPIE) || O.matches(options::OPT_fpie);
2059 PIC = PIE || O.matches(options::OPT_fPIC) ||
2060 O.matches(options::OPT_fpic);
2061 IsPICLevelTwo = O.matches(options::OPT_fPIE) ||
2062 O.matches(options::OPT_fPIC);
2063 } else {
2064 PIE = PIC = false;
2065 }
2066 }
Chandler Carruthc0c04552012-04-08 16:40:35 +00002067 }
Chandler Carruth76a943b2012-11-19 03:52:03 +00002068
2069 // Inroduce a Darwin-specific hack. If the default is PIC but the flags
2070 // specified while enabling PIC enabled level 1 PIC, just force it back to
2071 // level 2 PIC instead. This matches the behavior of Darwin GCC (based on my
2072 // informal testing).
2073 if (PIC && getToolChain().getTriple().isOSDarwin())
2074 IsPICLevelTwo |= getToolChain().isPICDefault();
2075
Chandler Carruthc0c04552012-04-08 16:40:35 +00002076 // Note that these flags are trump-cards. Regardless of the order w.r.t. the
2077 // PIC or PIE options above, if these show up, PIC is disabled.
Daniel Dunbarbd847cc2012-10-15 22:23:53 +00002078 llvm::Triple Triple(TripleStr);
Eric Christopher8d56caa2013-02-18 01:16:37 +00002079 if (KernelOrKext &&
Daniel Dunbarbd847cc2012-10-15 22:23:53 +00002080 (Triple.getOS() != llvm::Triple::IOS ||
2081 Triple.isOSVersionLT(6)))
Chandler Carruth76a943b2012-11-19 03:52:03 +00002082 PIC = PIE = false;
Chandler Carruthc0c04552012-04-08 16:40:35 +00002083 if (Args.hasArg(options::OPT_static))
Chandler Carruth76a943b2012-11-19 03:52:03 +00002084 PIC = PIE = false;
Chandler Carruthc0c04552012-04-08 16:40:35 +00002085
Chandler Carruth76a943b2012-11-19 03:52:03 +00002086 if (Arg *A = Args.getLastArg(options::OPT_mdynamic_no_pic)) {
2087 // This is a very special mode. It trumps the other modes, almost no one
2088 // uses it, and it isn't even valid on any OS but Darwin.
2089 if (!getToolChain().getTriple().isOSDarwin())
2090 D.Diag(diag::err_drv_unsupported_opt_for_target)
2091 << A->getSpelling() << getToolChain().getTriple().str();
2092
2093 // FIXME: Warn when this flag trumps some other PIC or PIE flag.
2094
Daniel Dunbara1b02a22009-11-29 07:18:39 +00002095 CmdArgs.push_back("-mrelocation-model");
Chandler Carruth76a943b2012-11-19 03:52:03 +00002096 CmdArgs.push_back("dynamic-no-pic");
Daniel Dunbar44e71222009-04-29 18:32:25 +00002097
Chandler Carruth76a943b2012-11-19 03:52:03 +00002098 // Only a forced PIC mode can cause the actual compile to have PIC defines
2099 // etc., no flags are sufficient. This behavior was selected to closely
2100 // match that of llvm-gcc and Apple GCC before that.
2101 if (getToolChain().isPICDefault() && getToolChain().isPICDefaultForced()) {
2102 CmdArgs.push_back("-pic-level");
2103 CmdArgs.push_back("2");
2104 }
2105 } else {
2106 // Currently, LLVM only knows about PIC vs. static; the PIE differences are
2107 // handled in Clang's IRGen by the -pie-level flag.
2108 CmdArgs.push_back("-mrelocation-model");
2109 CmdArgs.push_back(PIC ? "pic" : "static");
2110
2111 if (PIC) {
2112 CmdArgs.push_back("-pic-level");
2113 CmdArgs.push_back(IsPICLevelTwo ? "2" : "1");
2114 if (PIE) {
2115 CmdArgs.push_back("-pie-level");
2116 CmdArgs.push_back(IsPICLevelTwo ? "2" : "1");
2117 }
2118 }
Daniel Dunbar44e71222009-04-29 18:32:25 +00002119 }
Chandler Carruthc0c04552012-04-08 16:40:35 +00002120
Tanya Lattnerf9d41df2009-11-04 01:18:09 +00002121 if (!Args.hasFlag(options::OPT_fmerge_all_constants,
2122 options::OPT_fno_merge_all_constants))
Chris Lattner9242b332011-04-08 18:06:54 +00002123 CmdArgs.push_back("-fno-merge-all-constants");
Daniel Dunbar306945d2009-09-16 06:17:29 +00002124
Daniel Dunbara1b02a22009-11-29 07:18:39 +00002125 // LLVM Code Generator Options.
2126
Daniel Dunbar0bb03312011-02-09 17:54:19 +00002127 if (Arg *A = Args.getLastArg(options::OPT_mregparm_EQ)) {
2128 CmdArgs.push_back("-mregparm");
Richard Smithbd55daf2012-11-01 04:30:05 +00002129 CmdArgs.push_back(A->getValue());
Daniel Dunbar0bb03312011-02-09 17:54:19 +00002130 }
2131
Roman Divacky65b88cd2011-03-01 17:40:53 +00002132 if (Args.hasFlag(options::OPT_mrtd, options::OPT_mno_rtd, false))
2133 CmdArgs.push_back("-mrtd");
2134
Rafael Espindola224dd632011-12-14 21:02:23 +00002135 if (shouldUseFramePointer(Args, getToolChain().getTriple()))
Daniel Dunbara1b02a22009-11-29 07:18:39 +00002136 CmdArgs.push_back("-mdisable-fp-elim");
2137 if (!Args.hasFlag(options::OPT_fzero_initialized_in_bss,
2138 options::OPT_fno_zero_initialized_in_bss))
2139 CmdArgs.push_back("-mno-zero-initialized-in-bss");
Chad Rosierb71f6aa2013-04-24 18:09:54 +00002140
2141 bool OFastEnabled = isOptimizationLevelFast(Args);
2142 // If -Ofast is the optimization level, then -fstrict-aliasing should be
2143 // enabled. This alias option is being used to simplify the hasFlag logic.
2144 OptSpecifier StrictAliasingAliasOption = OFastEnabled ? options::OPT_Ofast :
2145 options::OPT_fstrict_aliasing;
2146 if (!Args.hasFlag(options::OPT_fstrict_aliasing, StrictAliasingAliasOption,
Daniel Dunbar7aa71f92011-02-04 02:20:39 +00002147 options::OPT_fno_strict_aliasing,
2148 getToolChain().IsStrictAliasingDefault()))
Dan Gohman10169b92010-10-14 22:36:56 +00002149 CmdArgs.push_back("-relaxed-aliasing");
Manman Renc451e572013-04-04 21:53:22 +00002150 if (Args.hasArg(options::OPT_fstruct_path_tbaa))
2151 CmdArgs.push_back("-struct-path-tbaa");
Chandler Carruth8b4140d2012-03-27 23:58:37 +00002152 if (Args.hasFlag(options::OPT_fstrict_enums, options::OPT_fno_strict_enums,
2153 false))
2154 CmdArgs.push_back("-fstrict-enums");
Nick Lewycky1c8c4362012-01-23 08:29:12 +00002155 if (!Args.hasFlag(options::OPT_foptimize_sibling_calls,
2156 options::OPT_fno_optimize_sibling_calls))
2157 CmdArgs.push_back("-mdisable-tail-calls");
Daniel Dunbar0d8ca9e2010-05-14 22:00:22 +00002158
Eric Christopher006208c2013-04-04 06:29:47 +00002159 // Handle segmented stacks.
2160 if (Args.hasArg(options::OPT_fsplit_stack))
2161 CmdArgs.push_back("-split-stacks");
Chad Rosierb71f6aa2013-04-24 18:09:54 +00002162
2163 // If -Ofast is the optimization level, then -ffast-math should be enabled.
2164 // This alias option is being used to simplify the getLastArg logic.
2165 OptSpecifier FastMathAliasOption = OFastEnabled ? options::OPT_Ofast :
2166 options::OPT_ffast_math;
Eric Christopher006208c2013-04-04 06:29:47 +00002167
Chandler Carruth306bd2c2012-01-02 14:19:45 +00002168 // Handle various floating point optimization flags, mapping them to the
2169 // appropriate LLVM code generation flags. The pattern for all of these is to
2170 // default off the codegen optimizations, and if any flag enables them and no
2171 // flag disables them after the flag enabling them, enable the codegen
2172 // optimization. This is complicated by several "umbrella" flags.
Chad Rosierb71f6aa2013-04-24 18:09:54 +00002173 if (Arg *A = Args.getLastArg(options::OPT_ffast_math, FastMathAliasOption,
Chad Rosierc30eb1c2012-09-25 22:03:25 +00002174 options::OPT_fno_fast_math,
Chandler Carruth306bd2c2012-01-02 14:19:45 +00002175 options::OPT_ffinite_math_only,
2176 options::OPT_fno_finite_math_only,
2177 options::OPT_fhonor_infinities,
2178 options::OPT_fno_honor_infinities))
Chad Rosierc30eb1c2012-09-25 22:03:25 +00002179 if (A->getOption().getID() != options::OPT_fno_fast_math &&
2180 A->getOption().getID() != options::OPT_fno_finite_math_only &&
Chandler Carruth306bd2c2012-01-02 14:19:45 +00002181 A->getOption().getID() != options::OPT_fhonor_infinities)
2182 CmdArgs.push_back("-menable-no-infs");
Chad Rosierb71f6aa2013-04-24 18:09:54 +00002183 if (Arg *A = Args.getLastArg(options::OPT_ffast_math, FastMathAliasOption,
Chad Rosierc30eb1c2012-09-25 22:03:25 +00002184 options::OPT_fno_fast_math,
Chandler Carruth306bd2c2012-01-02 14:19:45 +00002185 options::OPT_ffinite_math_only,
2186 options::OPT_fno_finite_math_only,
2187 options::OPT_fhonor_nans,
2188 options::OPT_fno_honor_nans))
Chad Rosierc30eb1c2012-09-25 22:03:25 +00002189 if (A->getOption().getID() != options::OPT_fno_fast_math &&
2190 A->getOption().getID() != options::OPT_fno_finite_math_only &&
Chandler Carruth306bd2c2012-01-02 14:19:45 +00002191 A->getOption().getID() != options::OPT_fhonor_nans)
2192 CmdArgs.push_back("-menable-no-nans");
2193
Benjamin Kramerc242ef22012-05-02 14:55:48 +00002194 // -fmath-errno is the default on some platforms, e.g. BSD-derived OSes.
2195 bool MathErrno = getToolChain().IsMathErrnoDefault();
Chad Rosierb71f6aa2013-04-24 18:09:54 +00002196 if (Arg *A = Args.getLastArg(options::OPT_ffast_math, FastMathAliasOption,
Chad Rosierc30eb1c2012-09-25 22:03:25 +00002197 options::OPT_fno_fast_math,
Chandler Carruth306bd2c2012-01-02 14:19:45 +00002198 options::OPT_fmath_errno,
Chandler Carruth3634c662012-04-26 02:10:51 +00002199 options::OPT_fno_math_errno))
2200 MathErrno = A->getOption().getID() == options::OPT_fmath_errno;
2201 if (MathErrno)
2202 CmdArgs.push_back("-fmath-errno");
Chandler Carruth306bd2c2012-01-02 14:19:45 +00002203
2204 // There are several flags which require disabling very specific
2205 // optimizations. Any of these being disabled forces us to turn off the
2206 // entire set of LLVM optimizations, so collect them through all the flag
2207 // madness.
2208 bool AssociativeMath = false;
Chad Rosierb71f6aa2013-04-24 18:09:54 +00002209 if (Arg *A = Args.getLastArg(options::OPT_ffast_math, FastMathAliasOption,
Chad Rosierc30eb1c2012-09-25 22:03:25 +00002210 options::OPT_fno_fast_math,
Chandler Carruth306bd2c2012-01-02 14:19:45 +00002211 options::OPT_funsafe_math_optimizations,
2212 options::OPT_fno_unsafe_math_optimizations,
2213 options::OPT_fassociative_math,
2214 options::OPT_fno_associative_math))
Chad Rosierc30eb1c2012-09-25 22:03:25 +00002215 if (A->getOption().getID() != options::OPT_fno_fast_math &&
2216 A->getOption().getID() != options::OPT_fno_unsafe_math_optimizations &&
Chandler Carruth306bd2c2012-01-02 14:19:45 +00002217 A->getOption().getID() != options::OPT_fno_associative_math)
2218 AssociativeMath = true;
2219 bool ReciprocalMath = false;
Chad Rosierb71f6aa2013-04-24 18:09:54 +00002220 if (Arg *A = Args.getLastArg(options::OPT_ffast_math, FastMathAliasOption,
Chad Rosierc30eb1c2012-09-25 22:03:25 +00002221 options::OPT_fno_fast_math,
Chandler Carruth306bd2c2012-01-02 14:19:45 +00002222 options::OPT_funsafe_math_optimizations,
2223 options::OPT_fno_unsafe_math_optimizations,
2224 options::OPT_freciprocal_math,
2225 options::OPT_fno_reciprocal_math))
Chad Rosierc30eb1c2012-09-25 22:03:25 +00002226 if (A->getOption().getID() != options::OPT_fno_fast_math &&
2227 A->getOption().getID() != options::OPT_fno_unsafe_math_optimizations &&
Chandler Carruth306bd2c2012-01-02 14:19:45 +00002228 A->getOption().getID() != options::OPT_fno_reciprocal_math)
2229 ReciprocalMath = true;
2230 bool SignedZeros = true;
Chad Rosierb71f6aa2013-04-24 18:09:54 +00002231 if (Arg *A = Args.getLastArg(options::OPT_ffast_math, FastMathAliasOption,
Chad Rosierc30eb1c2012-09-25 22:03:25 +00002232 options::OPT_fno_fast_math,
Chandler Carruth306bd2c2012-01-02 14:19:45 +00002233 options::OPT_funsafe_math_optimizations,
2234 options::OPT_fno_unsafe_math_optimizations,
2235 options::OPT_fsigned_zeros,
2236 options::OPT_fno_signed_zeros))
Chad Rosierc30eb1c2012-09-25 22:03:25 +00002237 if (A->getOption().getID() != options::OPT_fno_fast_math &&
2238 A->getOption().getID() != options::OPT_fno_unsafe_math_optimizations &&
Chandler Carruth306bd2c2012-01-02 14:19:45 +00002239 A->getOption().getID() != options::OPT_fsigned_zeros)
2240 SignedZeros = false;
2241 bool TrappingMath = true;
Chad Rosierb71f6aa2013-04-24 18:09:54 +00002242 if (Arg *A = Args.getLastArg(options::OPT_ffast_math, FastMathAliasOption,
Chad Rosierc30eb1c2012-09-25 22:03:25 +00002243 options::OPT_fno_fast_math,
Chandler Carruth306bd2c2012-01-02 14:19:45 +00002244 options::OPT_funsafe_math_optimizations,
2245 options::OPT_fno_unsafe_math_optimizations,
2246 options::OPT_ftrapping_math,
2247 options::OPT_fno_trapping_math))
Chad Rosierc30eb1c2012-09-25 22:03:25 +00002248 if (A->getOption().getID() != options::OPT_fno_fast_math &&
2249 A->getOption().getID() != options::OPT_fno_unsafe_math_optimizations &&
Chandler Carruth306bd2c2012-01-02 14:19:45 +00002250 A->getOption().getID() != options::OPT_ftrapping_math)
2251 TrappingMath = false;
2252 if (!MathErrno && AssociativeMath && ReciprocalMath && !SignedZeros &&
2253 !TrappingMath)
2254 CmdArgs.push_back("-menable-unsafe-fp-math");
2255
Lang Hamesaa53b932012-07-06 00:59:19 +00002256
2257 // Validate and pass through -fp-contract option.
Chad Rosierb71f6aa2013-04-24 18:09:54 +00002258 if (Arg *A = Args.getLastArg(options::OPT_ffast_math, FastMathAliasOption,
Chad Rosierc30eb1c2012-09-25 22:03:25 +00002259 options::OPT_fno_fast_math,
Lang Hamesaa53b932012-07-06 00:59:19 +00002260 options::OPT_ffp_contract)) {
2261 if (A->getOption().getID() == options::OPT_ffp_contract) {
Richard Smithbd55daf2012-11-01 04:30:05 +00002262 StringRef Val = A->getValue();
Lang Hamesaa53b932012-07-06 00:59:19 +00002263 if (Val == "fast" || Val == "on" || Val == "off") {
2264 CmdArgs.push_back(Args.MakeArgString("-ffp-contract=" + Val));
2265 } else {
2266 D.Diag(diag::err_drv_unsupported_option_argument)
2267 << A->getOption().getName() << Val;
2268 }
Chad Rosierb71f6aa2013-04-24 18:09:54 +00002269 } else if (A->getOption().matches(options::OPT_ffast_math) ||
2270 (OFastEnabled && A->getOption().matches(options::OPT_Ofast))) {
Lang Hamesaa53b932012-07-06 00:59:19 +00002271 // If fast-math is set then set the fp-contract mode to fast.
2272 CmdArgs.push_back(Args.MakeArgString("-ffp-contract=fast"));
2273 }
2274 }
2275
Bob Wilson6a039162012-07-19 03:52:53 +00002276 // We separately look for the '-ffast-math' and '-ffinite-math-only' flags,
2277 // and if we find them, tell the frontend to provide the appropriate
2278 // preprocessor macros. This is distinct from enabling any optimizations as
2279 // these options induce language changes which must survive serialization
2280 // and deserialization, etc.
Chad Rosierb71f6aa2013-04-24 18:09:54 +00002281 if (Arg *A = Args.getLastArg(options::OPT_ffast_math, FastMathAliasOption,
2282 options::OPT_fno_fast_math))
2283 if (!A->getOption().matches(options::OPT_fno_fast_math))
2284 CmdArgs.push_back("-ffast-math");
Chad Rosierc30eb1c2012-09-25 22:03:25 +00002285 if (Arg *A = Args.getLastArg(options::OPT_ffinite_math_only, options::OPT_fno_fast_math))
2286 if (A->getOption().matches(options::OPT_ffinite_math_only))
2287 CmdArgs.push_back("-ffinite-math-only");
Chandler Carruth306bd2c2012-01-02 14:19:45 +00002288
Daniel Dunbar0d8ca9e2010-05-14 22:00:22 +00002289 // Decide whether to use verbose asm. Verbose assembly is the default on
2290 // toolchains which have the integrated assembler on by default.
2291 bool IsVerboseAsmDefault = getToolChain().IsIntegratedAssemblerDefault();
2292 if (Args.hasFlag(options::OPT_fverbose_asm, options::OPT_fno_verbose_asm,
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00002293 IsVerboseAsmDefault) ||
Daniel Dunbar0d8ca9e2010-05-14 22:00:22 +00002294 Args.hasArg(options::OPT_dA))
Daniel Dunbara1b02a22009-11-29 07:18:39 +00002295 CmdArgs.push_back("-masm-verbose");
Daniel Dunbar0d8ca9e2010-05-14 22:00:22 +00002296
Daniel Dunbara1b02a22009-11-29 07:18:39 +00002297 if (Args.hasArg(options::OPT_fdebug_pass_structure)) {
2298 CmdArgs.push_back("-mdebug-pass");
2299 CmdArgs.push_back("Structure");
2300 }
2301 if (Args.hasArg(options::OPT_fdebug_pass_arguments)) {
2302 CmdArgs.push_back("-mdebug-pass");
2303 CmdArgs.push_back("Arguments");
2304 }
2305
John McCall8517abc2010-02-19 02:45:38 +00002306 // Enable -mconstructor-aliases except on darwin, where we have to
2307 // work around a linker bug; see <rdar://problem/7651567>.
Bob Wilson6524dd32011-10-14 05:03:44 +00002308 if (!getToolChain().getTriple().isOSDarwin())
John McCall8517abc2010-02-19 02:45:38 +00002309 CmdArgs.push_back("-mconstructor-aliases");
NAKAMURA Takumi029d74b2011-02-17 08:50:50 +00002310
John McCall7ef5cb32011-03-18 02:56:14 +00002311 // Darwin's kernel doesn't support guard variables; just die if we
2312 // try to use them.
Bob Wilson6524dd32011-10-14 05:03:44 +00002313 if (KernelOrKext && getToolChain().getTriple().isOSDarwin())
John McCall7ef5cb32011-03-18 02:56:14 +00002314 CmdArgs.push_back("-fforbid-guard-variables");
2315
Douglas Gregordbe39272011-02-01 15:15:22 +00002316 if (Args.hasArg(options::OPT_mms_bitfields)) {
2317 CmdArgs.push_back("-mms-bitfields");
2318 }
John McCall8517abc2010-02-19 02:45:38 +00002319
Daniel Dunbar306945d2009-09-16 06:17:29 +00002320 // This is a coarse approximation of what llvm-gcc actually does, both
2321 // -fasynchronous-unwind-tables and -fnon-call-exceptions interact in more
2322 // complicated ways.
2323 bool AsynchronousUnwindTables =
2324 Args.hasFlag(options::OPT_fasynchronous_unwind_tables,
2325 options::OPT_fno_asynchronous_unwind_tables,
2326 getToolChain().IsUnwindTablesDefault() &&
Daniel Dunbare46b52a2010-03-20 04:52:14 +00002327 !KernelOrKext);
Daniel Dunbar306945d2009-09-16 06:17:29 +00002328 if (Args.hasFlag(options::OPT_funwind_tables, options::OPT_fno_unwind_tables,
2329 AsynchronousUnwindTables))
Daniel Dunbara1b02a22009-11-29 07:18:39 +00002330 CmdArgs.push_back("-munwind-tables");
2331
Chandler Carruth05fb5852012-11-21 23:40:23 +00002332 getToolChain().addClangTargetOptions(Args, CmdArgs);
Rafael Espindola66aa0452012-06-19 01:26:10 +00002333
Daniel Dunbara1b02a22009-11-29 07:18:39 +00002334 if (Arg *A = Args.getLastArg(options::OPT_flimited_precision_EQ)) {
2335 CmdArgs.push_back("-mlimit-float-precision");
Richard Smithbd55daf2012-11-01 04:30:05 +00002336 CmdArgs.push_back(A->getValue());
Daniel Dunbara1b02a22009-11-29 07:18:39 +00002337 }
Daniel Dunbar44e71222009-04-29 18:32:25 +00002338
Daniel Dunbar4dbaaa62009-05-06 03:16:41 +00002339 // FIXME: Handle -mtune=.
2340 (void) Args.hasArg(options::OPT_mtune_EQ);
Daniel Dunbar44e71222009-04-29 18:32:25 +00002341
Benjamin Kramercf4371a2009-08-05 14:30:52 +00002342 if (Arg *A = Args.getLastArg(options::OPT_mcmodel_EQ)) {
Daniel Dunbara1b02a22009-11-29 07:18:39 +00002343 CmdArgs.push_back("-mcode-model");
Richard Smithbd55daf2012-11-01 04:30:05 +00002344 CmdArgs.push_back(A->getValue());
Benjamin Kramercf4371a2009-08-05 14:30:52 +00002345 }
2346
Daniel Dunbar3b3191f2009-09-09 22:33:08 +00002347 // Add target specific cpu and features flags.
2348 switch(getToolChain().getTriple().getArch()) {
2349 default:
2350 break;
Daniel Dunbar4dbaaa62009-05-06 03:16:41 +00002351
Daniel Dunbar0f5c5422009-09-10 04:57:17 +00002352 case llvm::Triple::arm:
2353 case llvm::Triple::thumb:
Daniel Dunbarc9388c12011-03-17 17:10:06 +00002354 AddARMTargetArgs(Args, CmdArgs, KernelOrKext);
Daniel Dunbar0f5c5422009-09-10 04:57:17 +00002355 break;
2356
Eric Christopher0b26a612010-03-02 02:41:08 +00002357 case llvm::Triple::mips:
2358 case llvm::Triple::mipsel:
Akira Hatanaka94ab5542011-09-21 02:13:07 +00002359 case llvm::Triple::mips64:
2360 case llvm::Triple::mips64el:
Eric Christopher0b26a612010-03-02 02:41:08 +00002361 AddMIPSTargetArgs(Args, CmdArgs);
2362 break;
2363
Hal Finkel8eb59282012-06-11 22:35:19 +00002364 case llvm::Triple::ppc:
2365 case llvm::Triple::ppc64:
2366 AddPPCTargetArgs(Args, CmdArgs);
2367 break;
2368
Tom Stellard6674c702013-04-01 20:56:53 +00002369 case llvm::Triple::r600:
2370 AddR600TargetArgs(Args, CmdArgs);
2371 break;
2372
Bruno Cardoso Lopese7f211c2010-11-09 17:21:19 +00002373 case llvm::Triple::sparc:
2374 AddSparcTargetArgs(Args, CmdArgs);
2375 break;
2376
Daniel Dunbar3b3191f2009-09-09 22:33:08 +00002377 case llvm::Triple::x86:
2378 case llvm::Triple::x86_64:
2379 AddX86TargetArgs(Args, CmdArgs);
2380 break;
Tony Linthicum76329bf2011-12-12 21:14:55 +00002381
2382 case llvm::Triple::hexagon:
2383 AddHexagonTargetArgs(Args, CmdArgs);
2384 break;
Daniel Dunbar44e71222009-04-29 18:32:25 +00002385 }
2386
Tony Linthicum76329bf2011-12-12 21:14:55 +00002387
2388
Daniel Dunbar976a2f52010-08-11 23:07:47 +00002389 // Pass the linker version in use.
2390 if (Arg *A = Args.getLastArg(options::OPT_mlinker_version_EQ)) {
2391 CmdArgs.push_back("-target-linker-version");
Richard Smithbd55daf2012-11-01 04:30:05 +00002392 CmdArgs.push_back(A->getValue());
Daniel Dunbar976a2f52010-08-11 23:07:47 +00002393 }
2394
Eric Christopherb7d97e92013-04-03 01:58:53 +00002395 if (!shouldUseLeafFramePointer(Args, getToolChain().getTriple()))
Daniel Dunbarbb7ac522010-07-01 01:31:45 +00002396 CmdArgs.push_back("-momit-leaf-frame-pointer");
2397
Daniel Dunbarfcc49a82010-05-12 18:19:58 +00002398 // Explicitly error on some things we know we don't support and can't just
2399 // ignore.
2400 types::ID InputType = Inputs[0].getType();
Daniel Dunbar4ed214a2010-09-24 19:39:37 +00002401 if (!Args.hasArg(options::OPT_fallow_unsupported)) {
2402 Arg *Unsupported;
Daniel Dunbar4ed214a2010-09-24 19:39:37 +00002403 if (types::isCXX(InputType) &&
Bob Wilson6524dd32011-10-14 05:03:44 +00002404 getToolChain().getTriple().isOSDarwin() &&
Daniel Dunbar4ed214a2010-09-24 19:39:37 +00002405 getToolChain().getTriple().getArch() == llvm::Triple::x86) {
Bob Wilson0d45f582011-08-13 23:48:55 +00002406 if ((Unsupported = Args.getLastArg(options::OPT_fapple_kext)) ||
2407 (Unsupported = Args.getLastArg(options::OPT_mkernel)))
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002408 D.Diag(diag::err_drv_clang_unsupported_opt_cxx_darwin_i386)
Daniel Dunbar4ed214a2010-09-24 19:39:37 +00002409 << Unsupported->getOption().getName();
2410 }
Daniel Dunbarfcc49a82010-05-12 18:19:58 +00002411 }
2412
Daniel Dunbara2aedc62009-03-18 10:01:51 +00002413 Args.AddAllArgs(CmdArgs, options::OPT_v);
Daniel Dunbard4352752010-08-24 22:44:13 +00002414 Args.AddLastArg(CmdArgs, options::OPT_H);
Chad Rosierbe10f982011-08-02 17:58:04 +00002415 if (D.CCPrintHeaders && !D.CCGenDiagnostics) {
Daniel Dunbarac540b32011-02-02 21:11:35 +00002416 CmdArgs.push_back("-header-include-file");
2417 CmdArgs.push_back(D.CCPrintHeadersFilename ?
2418 D.CCPrintHeadersFilename : "-");
2419 }
Daniel Dunbara2aedc62009-03-18 10:01:51 +00002420 Args.AddLastArg(CmdArgs, options::OPT_P);
Mike Stump11289f42009-09-09 15:08:12 +00002421 Args.AddLastArg(CmdArgs, options::OPT_print_ivar_layout);
Daniel Dunbara2aedc62009-03-18 10:01:51 +00002422
Chad Rosierbe10f982011-08-02 17:58:04 +00002423 if (D.CCLogDiagnostics && !D.CCGenDiagnostics) {
Daniel Dunbar529c03b2011-04-07 18:01:20 +00002424 CmdArgs.push_back("-diagnostic-log-file");
2425 CmdArgs.push_back(D.CCLogDiagnosticsFilename ?
2426 D.CCLogDiagnosticsFilename : "-");
2427 }
2428
Eric Christopher2ba5fcb2013-02-05 07:29:57 +00002429 // Use the last option from "-g" group. "-gline-tables-only"
2430 // is preserved, all other debug options are substituted with "-g".
Rafael Espindola08a692a2010-03-07 04:46:18 +00002431 Args.ClaimAllArgs(options::OPT_g_Group);
Alexey Samsonovdda3a7f2012-05-29 08:10:34 +00002432 if (Arg *A = Args.getLastArg(options::OPT_g_Group)) {
Eric Christopher2ba5fcb2013-02-05 07:29:57 +00002433 if (A->getOption().matches(options::OPT_gline_tables_only))
Alexey Samsonovdda3a7f2012-05-29 08:10:34 +00002434 CmdArgs.push_back("-gline-tables-only");
Eric Christopher2ba5fcb2013-02-05 07:29:57 +00002435 else if (!A->getOption().matches(options::OPT_g0) &&
2436 !A->getOption().matches(options::OPT_ggdb0))
Chad Rosier8fe3b112011-11-07 19:52:29 +00002437 CmdArgs.push_back("-g");
Alexey Samsonovdda3a7f2012-05-29 08:10:34 +00002438 }
Daniel Dunbara2aedc62009-03-18 10:01:51 +00002439
Alexey Samsonovf50a9ff2012-06-21 08:22:39 +00002440 // We ignore flags -gstrict-dwarf and -grecord-gcc-switches for now.
2441 Args.ClaimAllArgs(options::OPT_g_flags_Group);
Eric Christophera2f7eb72012-10-18 21:52:18 +00002442 if (Args.hasArg(options::OPT_gcolumn_info))
2443 CmdArgs.push_back("-dwarf-column-info");
Alexey Samsonovf50a9ff2012-06-21 08:22:39 +00002444
Eric Christopher2ba5fcb2013-02-05 07:29:57 +00002445 // -gsplit-dwarf should turn on -g and enable the backend dwarf
2446 // splitting and extraction.
Eric Christopherd42fb732013-02-21 22:35:05 +00002447 // FIXME: Currently only works on Linux.
2448 if (getToolChain().getTriple().getOS() == llvm::Triple::Linux &&
2449 Args.hasArg(options::OPT_gsplit_dwarf)) {
Eric Christopher2ba5fcb2013-02-05 07:29:57 +00002450 CmdArgs.push_back("-g");
2451 CmdArgs.push_back("-backend-option");
2452 CmdArgs.push_back("-split-dwarf=Enable");
2453 }
2454
Rafael Espindola66bfb2752010-05-06 21:06:04 +00002455 Args.AddAllArgs(CmdArgs, options::OPT_ffunction_sections);
2456 Args.AddAllArgs(CmdArgs, options::OPT_fdata_sections);
2457
Chris Lattner3c77a352010-06-22 00:03:40 +00002458 Args.AddAllArgs(CmdArgs, options::OPT_finstrument_functions);
2459
Nick Lewycky207bce32011-04-21 23:44:07 +00002460 if (Args.hasArg(options::OPT_ftest_coverage) ||
2461 Args.hasArg(options::OPT_coverage))
2462 CmdArgs.push_back("-femit-coverage-notes");
2463 if (Args.hasArg(options::OPT_fprofile_arcs) ||
2464 Args.hasArg(options::OPT_coverage))
2465 CmdArgs.push_back("-femit-coverage-data");
2466
Nick Lewycky480cb992011-05-04 20:46:58 +00002467 if (C.getArgs().hasArg(options::OPT_c) ||
2468 C.getArgs().hasArg(options::OPT_S)) {
2469 if (Output.isFilename()) {
Nick Lewycky85c011d2011-05-05 00:08:20 +00002470 CmdArgs.push_back("-coverage-file");
Eric Christophere30f61c2013-02-22 00:24:40 +00002471 SmallString<128> CoverageFilename(Output.getFilename());
Nick Lewycky737a4522013-03-07 08:28:53 +00002472 if (llvm::sys::path::is_relative(CoverageFilename.str())) {
2473 if (const char *pwd = ::getenv("PWD")) {
2474 if (llvm::sys::path::is_absolute(pwd)) {
2475 SmallString<128> Pwd(pwd);
2476 llvm::sys::path::append(Pwd, CoverageFilename.str());
2477 CoverageFilename.swap(Pwd);
2478 }
2479 }
2480 }
Eric Christophere30f61c2013-02-22 00:24:40 +00002481 CmdArgs.push_back(Args.MakeArgString(CoverageFilename));
Nick Lewycky480cb992011-05-04 20:46:58 +00002482 }
2483 }
2484
Daniel Dunbarb25bfde2011-10-11 18:20:10 +00002485 // Pass options for controlling the default header search paths.
2486 if (Args.hasArg(options::OPT_nostdinc)) {
2487 CmdArgs.push_back("-nostdsysteminc");
2488 CmdArgs.push_back("-nobuiltininc");
2489 } else {
Daniel Dunbar0f41eee2011-10-11 18:20:16 +00002490 if (Args.hasArg(options::OPT_nostdlibinc))
2491 CmdArgs.push_back("-nostdsysteminc");
Daniel Dunbarb25bfde2011-10-11 18:20:10 +00002492 Args.AddLastArg(CmdArgs, options::OPT_nostdincxx);
2493 Args.AddLastArg(CmdArgs, options::OPT_nobuiltininc);
2494 }
Daniel Dunbara2aedc62009-03-18 10:01:51 +00002495
Daniel Dunbar34e0b8c2009-12-15 01:02:52 +00002496 // Pass the path to compiler resource files.
Daniel Dunbar34e0b8c2009-12-15 01:02:52 +00002497 CmdArgs.push_back("-resource-dir");
Daniel Dunbar3f3e2cd2010-01-20 02:35:16 +00002498 CmdArgs.push_back(D.ResourceDir.c_str());
Daniel Dunbar9dc82a22009-04-07 21:42:00 +00002499
Argyrios Kyrtzidis71731d62010-11-03 22:45:23 +00002500 Args.AddLastArg(CmdArgs, options::OPT_working_directory);
2501
Ted Kremenekf7639e12012-03-06 20:06:33 +00002502 bool ARCMTEnabled = false;
John McCalld70fb982011-06-15 23:25:17 +00002503 if (!Args.hasArg(options::OPT_fno_objc_arc)) {
Argyrios Kyrtzidisc44b93d2011-07-07 04:00:39 +00002504 if (const Arg *A = Args.getLastArg(options::OPT_ccc_arcmt_check,
Argyrios Kyrtzidis7fbd97f2011-07-09 20:00:58 +00002505 options::OPT_ccc_arcmt_modify,
2506 options::OPT_ccc_arcmt_migrate)) {
Ted Kremenekf7639e12012-03-06 20:06:33 +00002507 ARCMTEnabled = true;
John McCalld70fb982011-06-15 23:25:17 +00002508 switch (A->getOption().getID()) {
2509 default:
2510 llvm_unreachable("missed a case");
Argyrios Kyrtzidisc44b93d2011-07-07 04:00:39 +00002511 case options::OPT_ccc_arcmt_check:
John McCalld70fb982011-06-15 23:25:17 +00002512 CmdArgs.push_back("-arcmt-check");
2513 break;
Argyrios Kyrtzidisc44b93d2011-07-07 04:00:39 +00002514 case options::OPT_ccc_arcmt_modify:
John McCalld70fb982011-06-15 23:25:17 +00002515 CmdArgs.push_back("-arcmt-modify");
2516 break;
Argyrios Kyrtzidis7fbd97f2011-07-09 20:00:58 +00002517 case options::OPT_ccc_arcmt_migrate:
2518 CmdArgs.push_back("-arcmt-migrate");
Ted Kremenekf7639e12012-03-06 20:06:33 +00002519 CmdArgs.push_back("-mt-migrate-directory");
Richard Smithbd55daf2012-11-01 04:30:05 +00002520 CmdArgs.push_back(A->getValue());
Argyrios Kyrtzidisd5713632011-07-19 17:20:03 +00002521
2522 Args.AddLastArg(CmdArgs, options::OPT_arcmt_migrate_report_output);
2523 Args.AddLastArg(CmdArgs, options::OPT_arcmt_migrate_emit_arc_errors);
Argyrios Kyrtzidis7fbd97f2011-07-09 20:00:58 +00002524 break;
John McCalld70fb982011-06-15 23:25:17 +00002525 }
2526 }
2527 }
Eric Christopher84fbdb42011-08-19 00:30:14 +00002528
Ted Kremenekf7639e12012-03-06 20:06:33 +00002529 if (const Arg *A = Args.getLastArg(options::OPT_ccc_objcmt_migrate)) {
2530 if (ARCMTEnabled) {
2531 D.Diag(diag::err_drv_argument_not_allowed_with)
2532 << A->getAsString(Args) << "-ccc-arcmt-migrate";
2533 }
2534 CmdArgs.push_back("-mt-migrate-directory");
Richard Smithbd55daf2012-11-01 04:30:05 +00002535 CmdArgs.push_back(A->getValue());
Ted Kremenekf7639e12012-03-06 20:06:33 +00002536
2537 if (!Args.hasArg(options::OPT_objcmt_migrate_literals,
2538 options::OPT_objcmt_migrate_subscripting)) {
2539 // None specified, means enable them all.
2540 CmdArgs.push_back("-objcmt-migrate-literals");
2541 CmdArgs.push_back("-objcmt-migrate-subscripting");
2542 } else {
2543 Args.AddLastArg(CmdArgs, options::OPT_objcmt_migrate_literals);
2544 Args.AddLastArg(CmdArgs, options::OPT_objcmt_migrate_subscripting);
2545 }
2546 }
2547
Daniel Dunbard067f7f2009-04-08 23:54:23 +00002548 // Add preprocessing options like -I, -D, etc. if we are using the
2549 // preprocessor.
2550 //
2551 // FIXME: Support -fpreprocessed
Daniel Dunbard067f7f2009-04-08 23:54:23 +00002552 if (types::getPreprocessedType(InputType) != types::TY_INVALID)
Chad Rosier633dcdc2013-01-24 19:14:47 +00002553 AddPreprocessingOptions(C, JA, D, Args, CmdArgs, Output, Inputs);
Daniel Dunbara2aedc62009-03-18 10:01:51 +00002554
Rafael Espindolaa7431922011-07-21 23:40:37 +00002555 // Don't warn about "clang -c -DPIC -fPIC test.i" because libtool.m4 assumes
2556 // that "The compiler can only warn and ignore the option if not recognized".
2557 // When building with ccache, it will pass -D options to clang even on
2558 // preprocessed inputs and configure concludes that -fPIC is not supported.
2559 Args.ClaimAllArgs(options::OPT_D);
2560
Daniel Dunbar58f78332009-09-17 06:53:36 +00002561 // Manually translate -O to -O2 and -O4 to -O3; let clang reject
Daniel Dunbar13864952009-03-24 20:17:30 +00002562 // others.
2563 if (Arg *A = Args.getLastArg(options::OPT_O_Group)) {
Daniel Dunbar0bfb21e2009-11-19 03:26:40 +00002564 if (A->getOption().matches(options::OPT_O4))
Daniel Dunbar13864952009-03-24 20:17:30 +00002565 CmdArgs.push_back("-O3");
Daniel Dunbar9296f632010-05-27 06:51:08 +00002566 else if (A->getOption().matches(options::OPT_O) &&
Richard Smithbd55daf2012-11-01 04:30:05 +00002567 A->getValue()[0] == '\0')
Daniel Dunbar58f78332009-09-17 06:53:36 +00002568 CmdArgs.push_back("-O2");
Daniel Dunbara2aedc62009-03-18 10:01:51 +00002569 else
Daniel Dunbar7ef5ed62009-03-18 23:39:35 +00002570 A->render(Args, CmdArgs);
Daniel Dunbara2aedc62009-03-18 10:01:51 +00002571 }
2572
Chad Rosier86b82082012-12-12 20:06:31 +00002573 // Don't warn about unused -flto. This can happen when we're preprocessing or
2574 // precompiling.
2575 Args.ClaimAllArgs(options::OPT_flto);
2576
Daniel Dunbar945577c2009-10-29 02:24:45 +00002577 Args.AddAllArgs(CmdArgs, options::OPT_W_Group);
Ted Kremenekb22ea2a2012-07-07 05:53:30 +00002578 if (Args.hasFlag(options::OPT_pedantic, options::OPT_no_pedantic, false))
2579 CmdArgs.push_back("-pedantic");
Daniel Dunbar945577c2009-10-29 02:24:45 +00002580 Args.AddLastArg(CmdArgs, options::OPT_pedantic_errors);
Daniel Dunbara2aedc62009-03-18 10:01:51 +00002581 Args.AddLastArg(CmdArgs, options::OPT_w);
Daniel Dunbarc44b4cc2009-04-07 22:13:21 +00002582
2583 // Handle -{std, ansi, trigraphs} -- take the last of -{std, ansi}
2584 // (-ansi is equivalent to -std=c89).
2585 //
2586 // If a std is supplied, only add -trigraphs if it follows the
2587 // option.
2588 if (Arg *Std = Args.getLastArg(options::OPT_std_EQ, options::OPT_ansi)) {
2589 if (Std->getOption().matches(options::OPT_ansi))
Nuno Lopes275225d2009-10-16 14:28:06 +00002590 if (types::isCXX(InputType))
Daniel Dunbar1c7577c2009-11-04 06:24:38 +00002591 CmdArgs.push_back("-std=c++98");
Nuno Lopes275225d2009-10-16 14:28:06 +00002592 else
Daniel Dunbar1c7577c2009-11-04 06:24:38 +00002593 CmdArgs.push_back("-std=c89");
Daniel Dunbarc44b4cc2009-04-07 22:13:21 +00002594 else
2595 Std->render(Args, CmdArgs);
2596
Daniel Dunbar3f1a1ff2010-06-14 21:23:08 +00002597 if (Arg *A = Args.getLastArg(options::OPT_std_EQ, options::OPT_ansi,
2598 options::OPT_trigraphs))
2599 if (A != Std)
Daniel Dunbarc44b4cc2009-04-07 22:13:21 +00002600 A->render(Args, CmdArgs);
Daniel Dunbar72a60902009-04-26 01:10:38 +00002601 } else {
2602 // Honor -std-default.
Daniel Dunbar12998192010-01-29 21:03:02 +00002603 //
2604 // FIXME: Clang doesn't correctly handle -std= when the input language
2605 // doesn't match. For the time being just ignore this for C++ inputs;
2606 // eventually we want to do all the standard defaulting here instead of
2607 // splitting it between the driver and clang -cc1.
2608 if (!types::isCXX(InputType))
Nico Weber723b4f02012-08-30 02:08:31 +00002609 Args.AddAllArgsTranslated(CmdArgs, options::OPT_std_default_EQ,
2610 "-std=", /*Joined=*/true);
2611 else if (getToolChain().getTriple().getOS() == llvm::Triple::Win32)
2612 CmdArgs.push_back("-std=c++11");
2613
Daniel Dunbarc44b4cc2009-04-07 22:13:21 +00002614 Args.AddLastArg(CmdArgs, options::OPT_trigraphs);
Daniel Dunbar72a60902009-04-26 01:10:38 +00002615 }
Daniel Dunbard067f7f2009-04-08 23:54:23 +00002616
Chandler Carruthb009b142011-04-23 06:30:43 +00002617 // Map the bizarre '-Wwrite-strings' flag to a more sensible
2618 // '-fconst-strings'; this better indicates its actual behavior.
2619 if (Args.hasFlag(options::OPT_Wwrite_strings, options::OPT_Wno_write_strings,
2620 false)) {
2621 // For perfect compatibility with GCC, we do this even in the presence of
2622 // '-w'. This flag names something other than a warning for GCC.
2623 CmdArgs.push_back("-fconst-strings");
2624 }
2625
Chandler Carruth61fbf622011-04-23 09:27:53 +00002626 // GCC provides a macro definition '__DEPRECATED' when -Wdeprecated is active
Chandler Carruth30483fb2011-04-23 19:48:40 +00002627 // during C++ compilation, which it is by default. GCC keeps this define even
2628 // in the presence of '-w', match this behavior bug-for-bug.
2629 if (types::isCXX(InputType) &&
2630 Args.hasFlag(options::OPT_Wdeprecated, options::OPT_Wno_deprecated,
2631 true)) {
2632 CmdArgs.push_back("-fdeprecated-macro");
Chandler Carruth61fbf622011-04-23 09:27:53 +00002633 }
2634
Chandler Carruthe0391482010-05-22 02:21:53 +00002635 // Translate GCC's misnamer '-fasm' arguments to '-fgnu-keywords'.
2636 if (Arg *Asm = Args.getLastArg(options::OPT_fasm, options::OPT_fno_asm)) {
2637 if (Asm->getOption().matches(options::OPT_fasm))
2638 CmdArgs.push_back("-fgnu-keywords");
2639 else
2640 CmdArgs.push_back("-fno-gnu-keywords");
2641 }
2642
Rafael Espindola4cfa7972011-05-02 17:43:32 +00002643 if (ShouldDisableCFI(Args, getToolChain()))
2644 CmdArgs.push_back("-fno-dwarf2-cfi-asm");
Rafael Espindolae2641872011-04-30 18:35:43 +00002645
Nick Lewycky1d617ac2011-10-17 23:05:52 +00002646 if (ShouldDisableDwarfDirectory(Args, getToolChain()))
2647 CmdArgs.push_back("-fno-dwarf-directory-asm");
2648
Daniel Dunbare246fbe2013-04-16 18:21:19 +00002649 if (ShouldDisableAutolink(Args, getToolChain()))
2650 CmdArgs.push_back("-fno-autolink");
2651
Chandler Carruth4d5e1a92012-12-17 21:40:04 +00002652 // Add in -fdebug-compilation-dir if necessary.
2653 addDebugCompDirArg(Args, CmdArgs);
Nick Lewyckyba743b72011-10-21 02:32:14 +00002654
Richard Smith9a568822011-11-21 19:36:32 +00002655 if (Arg *A = Args.getLastArg(options::OPT_ftemplate_depth_,
2656 options::OPT_ftemplate_depth_EQ)) {
Daniel Dunbara2aedc62009-03-18 10:01:51 +00002657 CmdArgs.push_back("-ftemplate-depth");
Richard Smithbd55daf2012-11-01 04:30:05 +00002658 CmdArgs.push_back(A->getValue());
Daniel Dunbara2aedc62009-03-18 10:01:51 +00002659 }
2660
Richard Smith9a568822011-11-21 19:36:32 +00002661 if (Arg *A = Args.getLastArg(options::OPT_fconstexpr_depth_EQ)) {
2662 CmdArgs.push_back("-fconstexpr-depth");
Richard Smithbd55daf2012-11-01 04:30:05 +00002663 CmdArgs.push_back(A->getValue());
Richard Smith9a568822011-11-21 19:36:32 +00002664 }
2665
Richard Smithb3a14522013-02-22 01:59:51 +00002666 if (Arg *A = Args.getLastArg(options::OPT_fbracket_depth_EQ)) {
2667 CmdArgs.push_back("-fbracket-depth");
2668 CmdArgs.push_back(A->getValue());
2669 }
2670
Argyrios Kyrtzidisef6c8da2010-11-18 00:20:36 +00002671 if (Arg *A = Args.getLastArg(options::OPT_Wlarge_by_value_copy_EQ,
2672 options::OPT_Wlarge_by_value_copy_def)) {
Jean-Daniel Dupas73d801c2012-05-04 08:08:37 +00002673 if (A->getNumValues()) {
Richard Smithbd55daf2012-11-01 04:30:05 +00002674 StringRef bytes = A->getValue();
Jean-Daniel Dupas73d801c2012-05-04 08:08:37 +00002675 CmdArgs.push_back(Args.MakeArgString("-Wlarge-by-value-copy=" + bytes));
2676 } else
2677 CmdArgs.push_back("-Wlarge-by-value-copy=64"); // default value
Argyrios Kyrtzidisaf84ec02010-11-17 23:11:54 +00002678 }
2679
Nuno Lopes3d6311d2012-05-08 22:10:46 +00002680
Michael J. Spencer929fccd2012-10-22 22:13:48 +00002681 if (Args.hasArg(options::OPT_relocatable_pch))
Daniel Dunbar8bed86c2009-11-20 22:21:36 +00002682 CmdArgs.push_back("-relocatable-pch");
Mike Stump11289f42009-09-09 15:08:12 +00002683
Daniel Dunbar1c7577c2009-11-04 06:24:38 +00002684 if (Arg *A = Args.getLastArg(options::OPT_fconstant_string_class_EQ)) {
2685 CmdArgs.push_back("-fconstant-string-class");
Richard Smithbd55daf2012-11-01 04:30:05 +00002686 CmdArgs.push_back(A->getValue());
Daniel Dunbar1c7577c2009-11-04 06:24:38 +00002687 }
David Chisnall5778fce2009-08-31 16:41:57 +00002688
Chris Lattnere23003d2010-01-09 21:54:33 +00002689 if (Arg *A = Args.getLastArg(options::OPT_ftabstop_EQ)) {
2690 CmdArgs.push_back("-ftabstop");
Richard Smithbd55daf2012-11-01 04:30:05 +00002691 CmdArgs.push_back(A->getValue());
Chris Lattnere23003d2010-01-09 21:54:33 +00002692 }
2693
Chris Lattnerb35583d2010-04-07 20:49:23 +00002694 CmdArgs.push_back("-ferror-limit");
2695 if (Arg *A = Args.getLastArg(options::OPT_ferror_limit_EQ))
Richard Smithbd55daf2012-11-01 04:30:05 +00002696 CmdArgs.push_back(A->getValue());
Chris Lattnerb35583d2010-04-07 20:49:23 +00002697 else
2698 CmdArgs.push_back("19");
Douglas Gregorffed1cb2010-04-20 07:18:24 +00002699
Chandler Carrutha77a7272010-05-06 04:55:18 +00002700 if (Arg *A = Args.getLastArg(options::OPT_fmacro_backtrace_limit_EQ)) {
2701 CmdArgs.push_back("-fmacro-backtrace-limit");
Richard Smithbd55daf2012-11-01 04:30:05 +00002702 CmdArgs.push_back(A->getValue());
Chandler Carrutha77a7272010-05-06 04:55:18 +00002703 }
2704
2705 if (Arg *A = Args.getLastArg(options::OPT_ftemplate_backtrace_limit_EQ)) {
2706 CmdArgs.push_back("-ftemplate-backtrace-limit");
Richard Smithbd55daf2012-11-01 04:30:05 +00002707 CmdArgs.push_back(A->getValue());
Chandler Carrutha77a7272010-05-06 04:55:18 +00002708 }
2709
Richard Smithf6f003a2011-12-16 19:06:07 +00002710 if (Arg *A = Args.getLastArg(options::OPT_fconstexpr_backtrace_limit_EQ)) {
2711 CmdArgs.push_back("-fconstexpr-backtrace-limit");
Richard Smithbd55daf2012-11-01 04:30:05 +00002712 CmdArgs.push_back(A->getValue());
Richard Smithf6f003a2011-12-16 19:06:07 +00002713 }
2714
Daniel Dunbar2c978472009-11-04 06:24:47 +00002715 // Pass -fmessage-length=.
Daniel Dunbar84bb7932009-11-30 08:40:54 +00002716 CmdArgs.push_back("-fmessage-length");
Daniel Dunbar2c978472009-11-04 06:24:47 +00002717 if (Arg *A = Args.getLastArg(options::OPT_fmessage_length_EQ)) {
Richard Smithbd55daf2012-11-01 04:30:05 +00002718 CmdArgs.push_back(A->getValue());
Daniel Dunbar2c978472009-11-04 06:24:47 +00002719 } else {
2720 // If -fmessage-length=N was not specified, determine whether this is a
2721 // terminal and, if so, implicitly define -fmessage-length appropriately.
2722 unsigned N = llvm::sys::Process::StandardErrColumns();
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002723 CmdArgs.push_back(Args.MakeArgString(Twine(N)));
Daniel Dunbar2c978472009-11-04 06:24:47 +00002724 }
2725
John McCallb4a99d32013-02-19 01:57:35 +00002726 // -fvisibility= and -fvisibility-ms-compat are of a piece.
2727 if (const Arg *A = Args.getLastArg(options::OPT_fvisibility_EQ,
2728 options::OPT_fvisibility_ms_compat)) {
2729 if (A->getOption().matches(options::OPT_fvisibility_EQ)) {
2730 CmdArgs.push_back("-fvisibility");
2731 CmdArgs.push_back(A->getValue());
2732 } else {
2733 assert(A->getOption().matches(options::OPT_fvisibility_ms_compat));
2734 CmdArgs.push_back("-fvisibility");
2735 CmdArgs.push_back("hidden");
2736 CmdArgs.push_back("-ftype-visibility");
2737 CmdArgs.push_back("default");
2738 }
Daniel Dunbare357d562009-12-03 18:42:11 +00002739 }
2740
Douglas Gregor08329632010-06-15 17:05:35 +00002741 Args.AddLastArg(CmdArgs, options::OPT_fvisibility_inlines_hidden);
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00002742
Hans Wennborgf60f6af2012-06-28 08:01:44 +00002743 Args.AddLastArg(CmdArgs, options::OPT_ftlsmodel_EQ);
2744
Daniel Dunbare46b52a2010-03-20 04:52:14 +00002745 // -fhosted is default.
Chad Rosier4fab82c2012-03-26 22:04:46 +00002746 if (Args.hasFlag(options::OPT_ffreestanding, options::OPT_fhosted, false) ||
2747 KernelOrKext)
Daniel Dunbare46b52a2010-03-20 04:52:14 +00002748 CmdArgs.push_back("-ffreestanding");
2749
Daniel Dunbare357d562009-12-03 18:42:11 +00002750 // Forward -f (flag) options which we can pass directly.
Daniel Dunbar3a148f22009-04-07 21:51:40 +00002751 Args.AddLastArg(CmdArgs, options::OPT_femit_all_decls);
Daniel Dunbar3a148f22009-04-07 21:51:40 +00002752 Args.AddLastArg(CmdArgs, options::OPT_fheinous_gnu_extensions);
Devang Patel91bbb552010-09-30 19:05:55 +00002753 Args.AddLastArg(CmdArgs, options::OPT_flimit_debug_info);
Devang Patel384dfa42011-11-04 20:05:58 +00002754 Args.AddLastArg(CmdArgs, options::OPT_fno_limit_debug_info);
Eric Christopher86050822011-10-25 07:13:06 +00002755 Args.AddLastArg(CmdArgs, options::OPT_fno_operator_names);
Anton Yartsev9b6b2e52011-12-23 20:23:19 +00002756 Args.AddLastArg(CmdArgs, options::OPT_faltivec);
Richard Trieu91844232012-06-26 18:18:47 +00002757 Args.AddLastArg(CmdArgs, options::OPT_fdiagnostics_show_template_tree);
2758 Args.AddLastArg(CmdArgs, options::OPT_fno_elide_type);
Chad Rosier864dfe12012-03-13 23:45:51 +00002759
Peter Collingbourne54d770c2013-04-09 04:35:11 +00002760 SanitizerArgs Sanitize(getToolChain(), Args);
Richard Smith52be6192012-11-05 22:04:41 +00002761 Sanitize.addArgs(Args, CmdArgs);
2762
Will Dietz3676d562012-12-30 20:53:28 +00002763 if (!Args.hasFlag(options::OPT_fsanitize_recover,
2764 options::OPT_fno_sanitize_recover,
2765 true))
2766 CmdArgs.push_back("-fno-sanitize-recover");
2767
Chad Rosierae229d52013-01-29 23:31:22 +00002768 if (Args.hasArg(options::OPT_fcatch_undefined_behavior) ||
2769 Args.hasFlag(options::OPT_fsanitize_undefined_trap_on_error,
2770 options::OPT_fno_sanitize_undefined_trap_on_error, false))
2771 CmdArgs.push_back("-fsanitize-undefined-trap-on-error");
2772
Eric Christopher459d2712013-02-19 06:16:53 +00002773 // Report an error for -faltivec on anything other than PowerPC.
Chad Rosier864dfe12012-03-13 23:45:51 +00002774 if (const Arg *A = Args.getLastArg(options::OPT_faltivec))
2775 if (!(getToolChain().getTriple().getArch() == llvm::Triple::ppc ||
2776 getToolChain().getTriple().getArch() == llvm::Triple::ppc64))
2777 D.Diag(diag::err_drv_argument_only_allowed_with)
2778 << A->getAsString(Args) << "ppc/ppc64";
2779
Daniel Dunbar733b0f82011-03-01 18:49:30 +00002780 if (getToolChain().SupportsProfiling())
2781 Args.AddLastArg(CmdArgs, options::OPT_pg);
Daniel Dunbar35621a92010-03-16 16:57:46 +00002782
2783 // -flax-vector-conversions is default.
2784 if (!Args.hasFlag(options::OPT_flax_vector_conversions,
2785 options::OPT_fno_lax_vector_conversions))
2786 CmdArgs.push_back("-fno-lax-vector-conversions");
2787
Fariborz Jahaniana4cfff82011-01-07 01:05:02 +00002788 if (Args.getLastArg(options::OPT_fapple_kext))
2789 CmdArgs.push_back("-fapple-kext");
2790
David Blaikie690f21e2012-06-14 18:55:27 +00002791 if (Args.hasFlag(options::OPT_frewrite_includes,
2792 options::OPT_fno_rewrite_includes, false))
2793 CmdArgs.push_back("-frewrite-includes");
2794
Fariborz Jahaniana4404f22009-05-22 20:17:16 +00002795 Args.AddLastArg(CmdArgs, options::OPT_fobjc_sender_dependent_dispatch);
Chris Lattner69686412009-04-21 05:34:31 +00002796 Args.AddLastArg(CmdArgs, options::OPT_fdiagnostics_print_source_range_info);
Douglas Gregoreec975c2010-08-19 20:24:43 +00002797 Args.AddLastArg(CmdArgs, options::OPT_fdiagnostics_parseable_fixits);
Daniel Dunbar3a148f22009-04-07 21:51:40 +00002798 Args.AddLastArg(CmdArgs, options::OPT_ftime_report);
2799 Args.AddLastArg(CmdArgs, options::OPT_ftrapv);
David Chisnalldd84ef12010-09-17 18:29:54 +00002800
2801 if (Arg *A = Args.getLastArg(options::OPT_ftrapv_handler_EQ)) {
2802 CmdArgs.push_back("-ftrapv-handler");
Richard Smithbd55daf2012-11-01 04:30:05 +00002803 CmdArgs.push_back(A->getValue());
David Chisnalldd84ef12010-09-17 18:29:54 +00002804 }
2805
Bob Wilson14adb362012-02-03 06:27:22 +00002806 Args.AddLastArg(CmdArgs, options::OPT_ftrap_function_EQ);
Evan Cheng04c94292011-04-08 21:37:45 +00002807
Chandler Carruth6e501032011-03-27 00:04:55 +00002808 // -fno-strict-overflow implies -fwrapv if it isn't disabled, but
2809 // -fstrict-overflow won't turn off an explicitly enabled -fwrapv.
2810 if (Arg *A = Args.getLastArg(options::OPT_fwrapv,
2811 options::OPT_fno_wrapv)) {
2812 if (A->getOption().matches(options::OPT_fwrapv))
2813 CmdArgs.push_back("-fwrapv");
2814 } else if (Arg *A = Args.getLastArg(options::OPT_fstrict_overflow,
2815 options::OPT_fno_strict_overflow)) {
2816 if (A->getOption().matches(options::OPT_fno_strict_overflow))
2817 CmdArgs.push_back("-fwrapv");
2818 }
Daniel Dunbar3a148f22009-04-07 21:51:40 +00002819 Args.AddLastArg(CmdArgs, options::OPT_fwritable_strings);
Eric Christopherf387dbd2010-08-07 23:08:14 +00002820 Args.AddLastArg(CmdArgs, options::OPT_funroll_loops);
Daniel Dunbara2aedc62009-03-18 10:01:51 +00002821
Daniel Dunbara77eaeb2009-09-03 04:54:28 +00002822 Args.AddLastArg(CmdArgs, options::OPT_pthread);
2823
Mahesha S6a682be42012-10-27 07:47:56 +00002824
Daniel Dunbar4930e332009-11-17 08:07:36 +00002825 // -stack-protector=0 is default.
2826 unsigned StackProtectorLevel = 0;
Bill Wendlingd63bbad2009-06-28 07:36:13 +00002827 if (Arg *A = Args.getLastArg(options::OPT_fno_stack_protector,
2828 options::OPT_fstack_protector_all,
2829 options::OPT_fstack_protector)) {
Daniel Dunbar4930e332009-11-17 08:07:36 +00002830 if (A->getOption().matches(options::OPT_fstack_protector))
2831 StackProtectorLevel = 1;
2832 else if (A->getOption().matches(options::OPT_fstack_protector_all))
2833 StackProtectorLevel = 2;
Nico Weberdd473632011-08-23 07:38:27 +00002834 } else {
2835 StackProtectorLevel =
2836 getToolChain().GetDefaultStackProtectorLevel(KernelOrKext);
2837 }
Daniel Dunbar4930e332009-11-17 08:07:36 +00002838 if (StackProtectorLevel) {
2839 CmdArgs.push_back("-stack-protector");
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002840 CmdArgs.push_back(Args.MakeArgString(Twine(StackProtectorLevel)));
Joerg Sonnenberger85e2bbc2012-09-12 13:51:14 +00002841 }
Chad Rosierdb3da832012-08-21 16:16:06 +00002842
Joerg Sonnenberger85e2bbc2012-09-12 13:51:14 +00002843 // --param ssp-buffer-size=
2844 for (arg_iterator it = Args.filtered_begin(options::OPT__param),
2845 ie = Args.filtered_end(); it != ie; ++it) {
Richard Smithbd55daf2012-11-01 04:30:05 +00002846 StringRef Str((*it)->getValue());
Joerg Sonnenberger85e2bbc2012-09-12 13:51:14 +00002847 if (Str.startswith("ssp-buffer-size=")) {
2848 if (StackProtectorLevel) {
Chad Rosierdb3da832012-08-21 16:16:06 +00002849 CmdArgs.push_back("-stack-protector-buffer-size");
2850 // FIXME: Verify the argument is a valid integer.
2851 CmdArgs.push_back(Args.MakeArgString(Str.drop_front(16)));
Chad Rosierdb3da832012-08-21 16:16:06 +00002852 }
Joerg Sonnenberger85e2bbc2012-09-12 13:51:14 +00002853 (*it)->claim();
Chad Rosierdb3da832012-08-21 16:16:06 +00002854 }
Bill Wendlingd63bbad2009-06-28 07:36:13 +00002855 }
2856
Nick Lewyckyf4d3f7a2011-12-06 03:33:03 +00002857 // Translate -mstackrealign
2858 if (Args.hasFlag(options::OPT_mstackrealign, options::OPT_mno_stackrealign,
2859 false)) {
2860 CmdArgs.push_back("-backend-option");
2861 CmdArgs.push_back("-force-align-stack");
2862 }
2863 if (!Args.hasFlag(options::OPT_mno_stackrealign, options::OPT_mstackrealign,
2864 false)) {
2865 CmdArgs.push_back(Args.MakeArgString("-mstackrealign"));
2866 }
2867
Joerg Sonnenbergerdb66ed02011-12-05 23:05:23 +00002868 if (Args.hasArg(options::OPT_mstack_alignment)) {
2869 StringRef alignment = Args.getLastArgValue(options::OPT_mstack_alignment);
2870 CmdArgs.push_back(Args.MakeArgString("-mstack-alignment=" + alignment));
Eric Christopherd5c45f62011-05-02 21:18:22 +00002871 }
Chad Rosier9ae53f82012-11-29 00:42:06 +00002872 // -mkernel implies -mstrict-align; don't add the redundant option.
2873 if (Args.hasArg(options::OPT_mstrict_align) && !KernelOrKext) {
Chad Rosierd87e4f72012-11-09 18:27:01 +00002874 CmdArgs.push_back("-backend-option");
2875 CmdArgs.push_back("-arm-strict-align");
Chad Rosier60027022012-11-09 17:29:19 +00002876 }
Eric Christopher84fbdb42011-08-19 00:30:14 +00002877
Daniel Dunbard18049a2009-04-07 21:16:11 +00002878 // Forward -f options with positive and negative forms; we translate
2879 // these by hand.
2880
Fariborz Jahaniana4cfff82011-01-07 01:05:02 +00002881 if (Args.hasArg(options::OPT_mkernel)) {
Daniel Dunbar80f787c2011-02-04 17:24:47 +00002882 if (!Args.hasArg(options::OPT_fapple_kext) && types::isCXX(InputType))
Fariborz Jahaniana4cfff82011-01-07 01:05:02 +00002883 CmdArgs.push_back("-fapple-kext");
2884 if (!Args.hasArg(options::OPT_fbuiltin))
2885 CmdArgs.push_back("-fno-builtin");
Chad Rosierf27e1c42012-03-26 21:29:17 +00002886 Args.ClaimAllArgs(options::OPT_fno_builtin);
Fariborz Jahaniana4cfff82011-01-07 01:05:02 +00002887 }
Daniel Dunbar4930e332009-11-17 08:07:36 +00002888 // -fbuiltin is default.
Fariborz Jahaniana4cfff82011-01-07 01:05:02 +00002889 else if (!Args.hasFlag(options::OPT_fbuiltin, options::OPT_fno_builtin))
Daniel Dunbar484afa22009-11-19 04:55:23 +00002890 CmdArgs.push_back("-fno-builtin");
Daniel Dunbard18049a2009-04-07 21:16:11 +00002891
Nuno Lopes13c88c72009-12-16 16:59:22 +00002892 if (!Args.hasFlag(options::OPT_fassume_sane_operator_new,
2893 options::OPT_fno_assume_sane_operator_new))
2894 CmdArgs.push_back("-fno-assume-sane-operator-new");
2895
Daniel Dunbar4930e332009-11-17 08:07:36 +00002896 // -fblocks=0 is default.
2897 if (Args.hasFlag(options::OPT_fblocks, options::OPT_fno_blocks,
David Chisnallda209912011-02-28 17:11:43 +00002898 getToolChain().IsBlocksDefault()) ||
2899 (Args.hasArg(options::OPT_fgnu_runtime) &&
2900 Args.hasArg(options::OPT_fobjc_nonfragile_abi) &&
2901 !Args.hasArg(options::OPT_fno_blocks))) {
Daniel Dunbar4930e332009-11-17 08:07:36 +00002902 CmdArgs.push_back("-fblocks");
John McCall7959fee2011-09-09 20:41:01 +00002903
2904 if (!Args.hasArg(options::OPT_fgnu_runtime) &&
2905 !getToolChain().hasBlocksRuntime())
2906 CmdArgs.push_back("-fblocks-runtime-optional");
David Chisnall950a9512009-11-17 19:33:30 +00002907 }
Daniel Dunbard18049a2009-04-07 21:16:11 +00002908
Douglas Gregor226173a2012-01-18 15:19:58 +00002909 // -fmodules enables modules (off by default). However, for C++/Objective-C++,
2910 // users must also pass -fcxx-modules. The latter flag will disappear once the
2911 // modules implementation is solid for C++/Objective-C++ programs as well.
Douglas Gregorc60437f2013-01-16 01:23:41 +00002912 bool HaveModules = false;
Douglas Gregor226173a2012-01-18 15:19:58 +00002913 if (Args.hasFlag(options::OPT_fmodules, options::OPT_fno_modules, false)) {
2914 bool AllowedInCXX = Args.hasFlag(options::OPT_fcxx_modules,
2915 options::OPT_fno_cxx_modules,
2916 false);
Douglas Gregorc60437f2013-01-16 01:23:41 +00002917 if (AllowedInCXX || !types::isCXX(InputType)) {
Douglas Gregor226173a2012-01-18 15:19:58 +00002918 CmdArgs.push_back("-fmodules");
Douglas Gregorc60437f2013-01-16 01:23:41 +00002919 HaveModules = true;
2920 }
2921 }
2922
Douglas Gregor35b04d62013-02-07 19:01:24 +00002923 // If a module path was provided, pass it along. Otherwise, use a temporary
2924 // directory.
2925 if (Arg *A = Args.getLastArg(options::OPT_fmodules_cache_path)) {
2926 A->claim();
2927 if (HaveModules) {
2928 A->render(Args, CmdArgs);
2929 }
2930 } else if (HaveModules) {
2931 SmallString<128> DefaultModuleCache;
2932 llvm::sys::path::system_temp_directory(/*erasedOnReboot=*/false,
2933 DefaultModuleCache);
Douglas Gregor23c7d672013-03-21 21:48:48 +00002934 llvm::sys::path::append(DefaultModuleCache, "org.llvm.clang");
2935 llvm::sys::path::append(DefaultModuleCache, "ModuleCache");
Douglas Gregor4bedb492013-02-07 22:59:12 +00002936 const char Arg[] = "-fmodules-cache-path=";
2937 DefaultModuleCache.insert(DefaultModuleCache.begin(),
2938 Arg, Arg + strlen(Arg));
Douglas Gregor35b04d62013-02-07 19:01:24 +00002939 CmdArgs.push_back(Args.MakeArgString(DefaultModuleCache));
2940 }
2941
2942 // Pass through all -fmodules-ignore-macro arguments.
2943 Args.AddAllArgs(CmdArgs, options::OPT_fmodules_ignore_macro);
Douglas Gregor527b1c92013-03-25 21:19:16 +00002944 Args.AddLastArg(CmdArgs, options::OPT_fmodules_prune_interval);
2945 Args.AddLastArg(CmdArgs, options::OPT_fmodules_prune_after);
Douglas Gregor35b04d62013-02-07 19:01:24 +00002946
John McCalldfea9982010-04-09 19:12:06 +00002947 // -faccess-control is default.
John McCall3155f572010-04-09 19:03:51 +00002948 if (Args.hasFlag(options::OPT_fno_access_control,
2949 options::OPT_faccess_control,
John McCalldfea9982010-04-09 19:12:06 +00002950 false))
John McCall3155f572010-04-09 19:03:51 +00002951 CmdArgs.push_back("-fno-access-control");
John McCall59bb1d42010-03-17 01:32:13 +00002952
Anders Carlssond470fef2010-11-21 00:09:52 +00002953 // -felide-constructors is the default.
2954 if (Args.hasFlag(options::OPT_fno_elide_constructors,
2955 options::OPT_felide_constructors,
2956 false))
2957 CmdArgs.push_back("-fno-elide-constructors");
2958
Daniel Dunbar0730e4f2009-11-17 07:06:20 +00002959 // -frtti is default.
Chad Rosier4fab82c2012-03-26 22:04:46 +00002960 if (!Args.hasFlag(options::OPT_frtti, options::OPT_fno_rtti) ||
Richard Smith52be6192012-11-05 22:04:41 +00002961 KernelOrKext) {
Daniel Dunbar484afa22009-11-19 04:55:23 +00002962 CmdArgs.push_back("-fno-rtti");
Mike Stump183c3d22009-07-31 23:15:31 +00002963
Richard Smith52be6192012-11-05 22:04:41 +00002964 // -fno-rtti cannot usefully be combined with -fsanitize=vptr.
Alexey Samsonov627b10f2012-11-06 15:09:03 +00002965 if (Sanitize.sanitizesVptr()) {
NAKAMURA Takumiab060362012-11-06 22:02:00 +00002966 std::string NoRttiArg =
Richard Smith52be6192012-11-05 22:04:41 +00002967 Args.getLastArg(options::OPT_mkernel,
2968 options::OPT_fapple_kext,
Richard Smithaa716572012-11-06 01:12:02 +00002969 options::OPT_fno_rtti)->getAsString(Args);
Richard Smith52be6192012-11-05 22:04:41 +00002970 D.Diag(diag::err_drv_argument_not_allowed_with)
2971 << "-fsanitize=vptr" << NoRttiArg;
2972 }
2973 }
2974
Tony Linthicum76329bf2011-12-12 21:14:55 +00002975 // -fshort-enums=0 is default for all architectures except Hexagon.
Argyrios Kyrtzidis74825bc2010-10-08 00:25:19 +00002976 if (Args.hasFlag(options::OPT_fshort_enums,
Tony Linthicum76329bf2011-12-12 21:14:55 +00002977 options::OPT_fno_short_enums,
2978 getToolChain().getTriple().getArch() ==
2979 llvm::Triple::hexagon))
Argyrios Kyrtzidis74825bc2010-10-08 00:25:19 +00002980 CmdArgs.push_back("-fshort-enums");
2981
Daniel Dunbard609b7b2009-11-17 06:37:03 +00002982 // -fsigned-char is default.
Daniel Dunbar5bdd2992009-11-25 10:14:30 +00002983 if (!Args.hasFlag(options::OPT_fsigned_char, options::OPT_funsigned_char,
Daniel Dunbard609b7b2009-11-17 06:37:03 +00002984 isSignedCharDefault(getToolChain().getTriple())))
Daniel Dunbar5fe08662009-11-29 02:39:08 +00002985 CmdArgs.push_back("-fno-signed-char");
Eli Friedman327f0b52009-06-05 07:21:14 +00002986
Anders Carlssonfcd764a2010-02-06 23:23:06 +00002987 // -fthreadsafe-static is default.
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00002988 if (!Args.hasFlag(options::OPT_fthreadsafe_statics,
Anders Carlssonfcd764a2010-02-06 23:23:06 +00002989 options::OPT_fno_threadsafe_statics))
2990 CmdArgs.push_back("-fno-threadsafe-statics");
2991
Daniel Dunbarfe06df42010-03-20 04:15:41 +00002992 // -fuse-cxa-atexit is default.
Chad Rosier4fab82c2012-03-26 22:04:46 +00002993 if (!Args.hasFlag(options::OPT_fuse_cxa_atexit,
2994 options::OPT_fno_use_cxa_atexit,
2995 getToolChain().getTriple().getOS() != llvm::Triple::Cygwin &&
Tony Linthicum76329bf2011-12-12 21:14:55 +00002996 getToolChain().getTriple().getOS() != llvm::Triple::MinGW32 &&
Chad Rosier4fab82c2012-03-26 22:04:46 +00002997 getToolChain().getTriple().getArch() != llvm::Triple::hexagon) ||
2998 KernelOrKext)
Daniel Dunbarfe06df42010-03-20 04:15:41 +00002999 CmdArgs.push_back("-fno-use-cxa-atexit");
3000
Daniel Dunbar0730e4f2009-11-17 07:06:20 +00003001 // -fms-extensions=0 is default.
Daniel Dunbar5bdd2992009-11-25 10:14:30 +00003002 if (Args.hasFlag(options::OPT_fms_extensions, options::OPT_fno_ms_extensions,
Daniel Dunbar0730e4f2009-11-17 07:06:20 +00003003 getToolChain().getTriple().getOS() == llvm::Triple::Win32))
3004 CmdArgs.push_back("-fms-extensions");
3005
Francois Pichet1b4f1632011-09-17 04:32:15 +00003006 // -fms-compatibility=0 is default.
Douglas Gregor2b4907e2011-10-24 15:49:38 +00003007 if (Args.hasFlag(options::OPT_fms_compatibility,
3008 options::OPT_fno_ms_compatibility,
3009 (getToolChain().getTriple().getOS() == llvm::Triple::Win32 &&
3010 Args.hasFlag(options::OPT_fms_extensions,
3011 options::OPT_fno_ms_extensions,
3012 true))))
Francois Pichet1b4f1632011-09-17 04:32:15 +00003013 CmdArgs.push_back("-fms-compatibility");
3014
Michael J. Spencer4992ca4b2010-10-21 05:21:48 +00003015 // -fmsc-version=1300 is default.
3016 if (Args.hasFlag(options::OPT_fms_extensions, options::OPT_fno_ms_extensions,
3017 getToolChain().getTriple().getOS() == llvm::Triple::Win32) ||
3018 Args.hasArg(options::OPT_fmsc_version)) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003019 StringRef msc_ver = Args.getLastArgValue(options::OPT_fmsc_version);
Michael J. Spencer4992ca4b2010-10-21 05:21:48 +00003020 if (msc_ver.empty())
3021 CmdArgs.push_back("-fmsc-version=1300");
3022 else
3023 CmdArgs.push_back(Args.MakeArgString("-fmsc-version=" + msc_ver));
3024 }
3025
3026
Eric Christopher5ecce122013-02-18 00:38:31 +00003027 // -fno-borland-extensions is default.
Dawn Perchik68bb1b42010-09-02 23:59:25 +00003028 if (Args.hasFlag(options::OPT_fborland_extensions,
3029 options::OPT_fno_borland_extensions, false))
3030 CmdArgs.push_back("-fborland-extensions");
3031
Francois Pichet02744872011-09-01 16:38:08 +00003032 // -fno-delayed-template-parsing is default, except for Windows where MSVC STL
3033 // needs it.
Francois Pichet1c229c02011-04-22 22:18:13 +00003034 if (Args.hasFlag(options::OPT_fdelayed_template_parsing,
3035 options::OPT_fno_delayed_template_parsing,
Francois Pichet02744872011-09-01 16:38:08 +00003036 getToolChain().getTriple().getOS() == llvm::Triple::Win32))
Francois Pichet35bc5de2011-08-26 00:22:34 +00003037 CmdArgs.push_back("-fdelayed-template-parsing");
Francois Pichet1c229c02011-04-22 22:18:13 +00003038
Chandler Carruthe03aa552010-04-17 20:17:31 +00003039 // -fgnu-keywords default varies depending on language; only pass if
3040 // specified.
3041 if (Arg *A = Args.getLastArg(options::OPT_fgnu_keywords,
Daniel Dunbardb059592010-04-24 17:56:39 +00003042 options::OPT_fno_gnu_keywords))
3043 A->render(Args, CmdArgs);
Chandler Carruthe03aa552010-04-17 20:17:31 +00003044
Rafael Espindola922a6242011-06-02 17:30:53 +00003045 if (Args.hasFlag(options::OPT_fgnu89_inline,
3046 options::OPT_fno_gnu89_inline,
3047 false))
Rafael Espindolafb2af642011-06-02 16:13:27 +00003048 CmdArgs.push_back("-fgnu89-inline");
3049
Chad Rosier9c76d242012-03-15 22:31:42 +00003050 if (Args.hasArg(options::OPT_fno_inline))
3051 CmdArgs.push_back("-fno-inline");
3052
Chad Rosier64d6be92012-03-06 21:17:19 +00003053 if (Args.hasArg(options::OPT_fno_inline_functions))
3054 CmdArgs.push_back("-fno-inline-functions");
Chad Rosier80603182012-03-06 18:49:20 +00003055
John McCall5fb5df92012-06-20 06:18:46 +00003056 ObjCRuntime objcRuntime = AddObjCRuntimeArgs(Args, CmdArgs, rewriteKind);
John McCall24fc0de2011-07-06 00:26:06 +00003057
John McCall5fb5df92012-06-20 06:18:46 +00003058 // -fobjc-dispatch-method is only relevant with the nonfragile-abi, and
3059 // legacy is the default.
3060 if (objcRuntime.isNonFragile()) {
David Chisnall3154e682011-09-30 13:32:35 +00003061 if (!Args.hasFlag(options::OPT_fobjc_legacy_dispatch,
3062 options::OPT_fno_objc_legacy_dispatch,
David Chisnallda225352012-07-04 11:52:24 +00003063 objcRuntime.isLegacyDispatchDefaultForArch(
3064 getToolChain().getTriple().getArch()))) {
David Chisnall3154e682011-09-30 13:32:35 +00003065 if (getToolChain().UseObjCMixedDispatch())
3066 CmdArgs.push_back("-fobjc-dispatch-method=mixed");
3067 else
3068 CmdArgs.push_back("-fobjc-dispatch-method=non-legacy");
3069 }
3070 }
3071
Nico Weber97bd94b2012-03-09 21:19:44 +00003072 // -fobjc-default-synthesize-properties=1 is default. This only has an effect
3073 // if the nonfragile objc abi is used.
Fariborz Jahanian05684072012-04-09 18:58:55 +00003074 if (getToolChain().IsObjCDefaultSynthPropertiesDefault()) {
David Chisnall3154e682011-09-30 13:32:35 +00003075 CmdArgs.push_back("-fobjc-default-synthesize-properties");
3076 }
3077
Fariborz Jahanian0e3043b2012-11-15 19:02:45 +00003078 // -fencode-extended-block-signature=1 is default.
3079 if (getToolChain().IsEncodeExtendedBlockSignatureDefault()) {
3080 CmdArgs.push_back("-fencode-extended-block-signature");
3081 }
3082
John McCall24fc0de2011-07-06 00:26:06 +00003083 // Allow -fno-objc-arr to trump -fobjc-arr/-fobjc-arc.
3084 // NOTE: This logic is duplicated in ToolChains.cpp.
3085 bool ARC = isObjCAutoRefCount(Args);
3086 if (ARC) {
John McCall3deb1ad2012-08-21 02:47:43 +00003087 getToolChain().CheckObjCARC();
Argyrios Kyrtzidis3dbeb552012-02-29 03:43:52 +00003088
John McCall24fc0de2011-07-06 00:26:06 +00003089 CmdArgs.push_back("-fobjc-arc");
3090
Chandler Carruth491db322011-11-04 07:34:47 +00003091 // FIXME: It seems like this entire block, and several around it should be
3092 // wrapped in isObjC, but for now we just use it here as this is where it
3093 // was being used previously.
3094 if (types::isCXX(InputType) && types::isObjC(InputType)) {
3095 if (getToolChain().GetCXXStdlibType(Args) == ToolChain::CST_Libcxx)
3096 CmdArgs.push_back("-fobjc-arc-cxxlib=libc++");
3097 else
3098 CmdArgs.push_back("-fobjc-arc-cxxlib=libstdc++");
3099 }
3100
John McCall24fc0de2011-07-06 00:26:06 +00003101 // Allow the user to enable full exceptions code emission.
3102 // We define off for Objective-CC, on for Objective-C++.
3103 if (Args.hasFlag(options::OPT_fobjc_arc_exceptions,
3104 options::OPT_fno_objc_arc_exceptions,
3105 /*default*/ types::isCXX(InputType)))
3106 CmdArgs.push_back("-fobjc-arc-exceptions");
3107 }
3108
3109 // -fobjc-infer-related-result-type is the default, except in the Objective-C
3110 // rewriter.
John McCall5fb5df92012-06-20 06:18:46 +00003111 if (rewriteKind != RK_None)
John McCall24fc0de2011-07-06 00:26:06 +00003112 CmdArgs.push_back("-fno-objc-infer-related-result-type");
Eric Christopher84fbdb42011-08-19 00:30:14 +00003113
John McCall24fc0de2011-07-06 00:26:06 +00003114 // Handle -fobjc-gc and -fobjc-gc-only. They are exclusive, and -fobjc-gc-only
3115 // takes precedence.
3116 const Arg *GCArg = Args.getLastArg(options::OPT_fobjc_gc_only);
3117 if (!GCArg)
3118 GCArg = Args.getLastArg(options::OPT_fobjc_gc);
3119 if (GCArg) {
3120 if (ARC) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003121 D.Diag(diag::err_drv_objc_gc_arr)
John McCall24fc0de2011-07-06 00:26:06 +00003122 << GCArg->getAsString(Args);
3123 } else if (getToolChain().SupportsObjCGC()) {
3124 GCArg->render(Args, CmdArgs);
3125 } else {
3126 // FIXME: We should move this to a hard error.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003127 D.Diag(diag::warn_drv_objc_gc_unsupported)
John McCall24fc0de2011-07-06 00:26:06 +00003128 << GCArg->getAsString(Args);
3129 }
3130 }
3131
John McCallb5f652e2011-06-22 00:53:57 +00003132 // Add exception args.
3133 addExceptionArgs(Args, InputType, getToolChain().getTriple(),
John McCall5fb5df92012-06-20 06:18:46 +00003134 KernelOrKext, objcRuntime, CmdArgs);
John McCallb5f652e2011-06-22 00:53:57 +00003135
3136 if (getToolChain().UseSjLjExceptions())
3137 CmdArgs.push_back("-fsjlj-exceptions");
3138
3139 // C++ "sane" operator new.
Daniel Dunbar2e3f2c82010-02-01 21:07:25 +00003140 if (!Args.hasFlag(options::OPT_fassume_sane_operator_new,
3141 options::OPT_fno_assume_sane_operator_new))
3142 CmdArgs.push_back("-fno-assume-sane-operator-new");
3143
Daniel Dunbar34d7a992010-04-27 15:34:57 +00003144 // -fconstant-cfstrings is default, and may be subject to argument translation
3145 // on Darwin.
3146 if (!Args.hasFlag(options::OPT_fconstant_cfstrings,
3147 options::OPT_fno_constant_cfstrings) ||
3148 !Args.hasFlag(options::OPT_mconstant_cfstrings,
3149 options::OPT_mno_constant_cfstrings))
3150 CmdArgs.push_back("-fno-constant-cfstrings");
3151
John Thompsoned4e2952009-11-05 20:14:16 +00003152 // -fshort-wchar default varies depending on platform; only
3153 // pass if specified.
Daniel Dunbar2cb4e7a2010-04-27 15:35:03 +00003154 if (Arg *A = Args.getLastArg(options::OPT_fshort_wchar))
3155 A->render(Args, CmdArgs);
John Thompsoned4e2952009-11-05 20:14:16 +00003156
Daniel Dunbar1a8a2e82009-10-29 02:39:57 +00003157 // -fno-pascal-strings is default, only pass non-default. If the tool chain
3158 // happened to translate to -mpascal-strings, we want to back translate here.
Daniel Dunbard4510f22009-04-07 23:51:44 +00003159 //
3160 // FIXME: This is gross; that translation should be pulled from the
3161 // tool chain.
Daniel Dunbard067f7f2009-04-08 23:54:23 +00003162 if (Args.hasFlag(options::OPT_fpascal_strings,
Daniel Dunbard4510f22009-04-07 23:51:44 +00003163 options::OPT_fno_pascal_strings,
3164 false) ||
3165 Args.hasFlag(options::OPT_mpascal_strings,
3166 options::OPT_mno_pascal_strings,
3167 false))
Daniel Dunbard18049a2009-04-07 21:16:11 +00003168 CmdArgs.push_back("-fpascal-strings");
NAKAMURA Takumi029d74b2011-02-17 08:50:50 +00003169
Daniel Dunbar096ed292011-10-05 21:04:55 +00003170 // Honor -fpack-struct= and -fpack-struct, if given. Note that
3171 // -fno-pack-struct doesn't apply to -fpack-struct=.
3172 if (Arg *A = Args.getLastArg(options::OPT_fpack_struct_EQ)) {
James Molloycebf75e2012-05-02 07:56:14 +00003173 std::string PackStructStr = "-fpack-struct=";
Richard Smithbd55daf2012-11-01 04:30:05 +00003174 PackStructStr += A->getValue();
James Molloycebf75e2012-05-02 07:56:14 +00003175 CmdArgs.push_back(Args.MakeArgString(PackStructStr));
Daniel Dunbar096ed292011-10-05 21:04:55 +00003176 } else if (Args.hasFlag(options::OPT_fpack_struct,
3177 options::OPT_fno_pack_struct, false)) {
James Molloycebf75e2012-05-02 07:56:14 +00003178 CmdArgs.push_back("-fpack-struct=1");
Daniel Dunbar096ed292011-10-05 21:04:55 +00003179 }
3180
Eric Christopher8d56caa2013-02-18 01:16:37 +00003181 if (KernelOrKext) {
Fariborz Jahaniana4cfff82011-01-07 01:05:02 +00003182 if (!Args.hasArg(options::OPT_fcommon))
3183 CmdArgs.push_back("-fno-common");
Chad Rosierd9d80e02012-03-26 21:35:40 +00003184 Args.ClaimAllArgs(options::OPT_fno_common);
Fariborz Jahaniana4cfff82011-01-07 01:05:02 +00003185 }
Daniel Dunbar096ed292011-10-05 21:04:55 +00003186
Daniel Dunbard18049a2009-04-07 21:16:11 +00003187 // -fcommon is default, only pass non-default.
Fariborz Jahaniana4cfff82011-01-07 01:05:02 +00003188 else if (!Args.hasFlag(options::OPT_fcommon, options::OPT_fno_common))
Daniel Dunbard18049a2009-04-07 21:16:11 +00003189 CmdArgs.push_back("-fno-common");
3190
Daniel Dunbar2edd9232009-04-15 02:37:43 +00003191 // -fsigned-bitfields is default, and clang doesn't yet support
Daniel Dunbar6358d682010-10-15 22:30:42 +00003192 // -funsigned-bitfields.
Mike Stump11289f42009-09-09 15:08:12 +00003193 if (!Args.hasFlag(options::OPT_fsigned_bitfields,
Daniel Dunbar2edd9232009-04-15 02:37:43 +00003194 options::OPT_funsigned_bitfields))
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003195 D.Diag(diag::warn_drv_clang_unsupported)
Daniel Dunbar2edd9232009-04-15 02:37:43 +00003196 << Args.getLastArg(options::OPT_funsigned_bitfields)->getAsString(Args);
3197
Daniel Dunbar6358d682010-10-15 22:30:42 +00003198 // -fsigned-bitfields is default, and clang doesn't support -fno-for-scope.
3199 if (!Args.hasFlag(options::OPT_ffor_scope,
3200 options::OPT_fno_for_scope))
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003201 D.Diag(diag::err_drv_clang_unsupported)
Daniel Dunbar6358d682010-10-15 22:30:42 +00003202 << Args.getLastArg(options::OPT_fno_for_scope)->getAsString(Args);
3203
Jeffrey Yasskin460aa542010-06-08 04:56:20 +00003204 // -fcaret-diagnostics is default.
3205 if (!Args.hasFlag(options::OPT_fcaret_diagnostics,
3206 options::OPT_fno_caret_diagnostics, true))
3207 CmdArgs.push_back("-fno-caret-diagnostics");
3208
Daniel Dunbar8281bde2009-04-19 21:09:34 +00003209 // -fdiagnostics-fixit-info is default, only pass non-default.
Mike Stump11289f42009-09-09 15:08:12 +00003210 if (!Args.hasFlag(options::OPT_fdiagnostics_fixit_info,
Daniel Dunbar8281bde2009-04-19 21:09:34 +00003211 options::OPT_fno_diagnostics_fixit_info))
3212 CmdArgs.push_back("-fno-diagnostics-fixit-info");
Eric Christopher84fbdb42011-08-19 00:30:14 +00003213
Daniel Dunbar092f0cc2009-04-16 06:32:38 +00003214 // Enable -fdiagnostics-show-option by default.
Mike Stump11289f42009-09-09 15:08:12 +00003215 if (Args.hasFlag(options::OPT_fdiagnostics_show_option,
Daniel Dunbar092f0cc2009-04-16 06:32:38 +00003216 options::OPT_fno_diagnostics_show_option))
3217 CmdArgs.push_back("-fdiagnostics-show-option");
Daniel Dunbar5ec95022009-11-04 06:24:57 +00003218
Chris Lattnerbf6fac82010-05-04 21:55:25 +00003219 if (const Arg *A =
3220 Args.getLastArg(options::OPT_fdiagnostics_show_category_EQ)) {
3221 CmdArgs.push_back("-fdiagnostics-show-category");
Richard Smithbd55daf2012-11-01 04:30:05 +00003222 CmdArgs.push_back(A->getValue());
Chris Lattnerbf6fac82010-05-04 21:55:25 +00003223 }
Daniel Dunbarfe6c97b2010-08-24 16:47:49 +00003224
Douglas Gregor643c9222011-05-21 17:07:29 +00003225 if (const Arg *A =
3226 Args.getLastArg(options::OPT_fdiagnostics_format_EQ)) {
3227 CmdArgs.push_back("-fdiagnostics-format");
Richard Smithbd55daf2012-11-01 04:30:05 +00003228 CmdArgs.push_back(A->getValue());
Douglas Gregor643c9222011-05-21 17:07:29 +00003229 }
3230
Chandler Carruthb6766f02011-03-27 01:50:55 +00003231 if (Arg *A = Args.getLastArg(
3232 options::OPT_fdiagnostics_show_note_include_stack,
3233 options::OPT_fno_diagnostics_show_note_include_stack)) {
3234 if (A->getOption().matches(
3235 options::OPT_fdiagnostics_show_note_include_stack))
3236 CmdArgs.push_back("-fdiagnostics-show-note-include-stack");
3237 else
3238 CmdArgs.push_back("-fno-diagnostics-show-note-include-stack");
3239 }
3240
Daniel Dunbar5ec95022009-11-04 06:24:57 +00003241 // Color diagnostics are the default, unless the terminal doesn't support
3242 // them.
Nico Weber7e2da792013-04-17 21:52:44 +00003243 // Support both clang's -f[no-]color-diagnostics and gcc's
3244 // -f[no-]diagnostics-colors[=never|always|auto].
3245 enum { Colors_On, Colors_Off, Colors_Auto } ShowColors = Colors_Auto;
3246 for (ArgList::const_iterator it = Args.begin(), ie = Args.end();
3247 it != ie; ++it) {
3248 const Option &O = (*it)->getOption();
3249 if (!O.matches(options::OPT_fcolor_diagnostics) &&
3250 !O.matches(options::OPT_fdiagnostics_color) &&
3251 !O.matches(options::OPT_fno_color_diagnostics) &&
3252 !O.matches(options::OPT_fno_diagnostics_color) &&
3253 !O.matches(options::OPT_fdiagnostics_color_EQ))
3254 continue;
3255
3256 (*it)->claim();
3257 if (O.matches(options::OPT_fcolor_diagnostics) ||
3258 O.matches(options::OPT_fdiagnostics_color)) {
3259 ShowColors = Colors_On;
3260 } else if (O.matches(options::OPT_fno_color_diagnostics) ||
3261 O.matches(options::OPT_fno_diagnostics_color)) {
3262 ShowColors = Colors_Off;
3263 } else {
3264 assert(O.matches(options::OPT_fdiagnostics_color_EQ));
3265 StringRef value((*it)->getValue());
3266 if (value == "always")
3267 ShowColors = Colors_On;
3268 else if (value == "never")
3269 ShowColors = Colors_Off;
3270 else if (value == "auto")
3271 ShowColors = Colors_Auto;
3272 else
3273 getToolChain().getDriver().Diag(diag::err_drv_clang_unsupported)
3274 << ("-fdiagnostics-color=" + value).str();
3275 }
3276 }
3277 if (ShowColors == Colors_On ||
3278 (ShowColors == Colors_Auto && llvm::sys::Process::StandardErrHasColors()))
Daniel Dunbar5ec95022009-11-04 06:24:57 +00003279 CmdArgs.push_back("-fcolor-diagnostics");
3280
Daniel Dunbardb097022009-06-08 21:13:54 +00003281 if (!Args.hasFlag(options::OPT_fshow_source_location,
3282 options::OPT_fno_show_source_location))
3283 CmdArgs.push_back("-fno-show-source-location");
Daniel Dunbar092f0cc2009-04-16 06:32:38 +00003284
Douglas Gregor643c9222011-05-21 17:07:29 +00003285 if (!Args.hasFlag(options::OPT_fshow_column,
3286 options::OPT_fno_show_column,
3287 true))
3288 CmdArgs.push_back("-fno-show-column");
3289
Douglas Gregor8ed0c0b2010-07-09 17:35:33 +00003290 if (!Args.hasFlag(options::OPT_fspell_checking,
3291 options::OPT_fno_spell_checking))
3292 CmdArgs.push_back("-fno-spell-checking");
Daniel Dunbarfe6c97b2010-08-24 16:47:49 +00003293
Daniel Dunbar473f8a62010-10-18 22:49:46 +00003294
Chad Rosierc8e56e82012-12-05 21:08:21 +00003295 // -fno-asm-blocks is default.
3296 if (Args.hasFlag(options::OPT_fasm_blocks, options::OPT_fno_asm_blocks,
3297 false))
3298 CmdArgs.push_back("-fasm-blocks");
Daniel Dunbar473f8a62010-10-18 22:49:46 +00003299
Chad Rosier679b0752013-04-24 18:29:59 +00003300 // If -Ofast is the optimization level, then -fvectorize should be enabled.
3301 // This alias option is being used to simplify the hasFlag logic.
3302 OptSpecifier VectorizeAliasOption = OFastEnabled ? options::OPT_Ofast :
3303 options::OPT_fvectorize;
3304
Nadav Rotem1cd71a72012-12-18 23:10:16 +00003305 // -fvectorize is default.
Chad Rosier679b0752013-04-24 18:29:59 +00003306 if (Args.hasFlag(options::OPT_fvectorize, VectorizeAliasOption,
Nadav Rotem1cd71a72012-12-18 23:10:16 +00003307 options::OPT_fno_vectorize, true)) {
Chad Rosier0d3ed6f2012-12-11 17:12:28 +00003308 CmdArgs.push_back("-backend-option");
3309 CmdArgs.push_back("-vectorize-loops");
3310 }
3311
Hal Finkel061f1652012-12-11 19:59:32 +00003312 // -fno-slp-vectorize is default.
3313 if (Args.hasFlag(options::OPT_fslp_vectorize,
3314 options::OPT_fno_slp_vectorize, false)) {
3315 CmdArgs.push_back("-backend-option");
Nadav Rotem0a2604d2013-04-15 04:57:18 +00003316 CmdArgs.push_back("-vectorize-slp");
Hal Finkel061f1652012-12-11 19:59:32 +00003317 }
3318
Nadav Rotem6a0dd6b2013-04-15 05:38:41 +00003319 // -fno-slp-vectorize-aggressive is default.
3320 if (Args.hasFlag(options::OPT_fslp_vectorize_aggressive,
3321 options::OPT_fno_slp_vectorize_aggressive, false)) {
3322 CmdArgs.push_back("-backend-option");
3323 CmdArgs.push_back("-vectorize-slp-aggressive");
3324 }
3325
Jeffrey Yasskin2b99c6f2010-06-11 05:57:47 +00003326 if (Arg *A = Args.getLastArg(options::OPT_fshow_overloads_EQ))
3327 A->render(Args, CmdArgs);
3328
Daniel Dunbarf5e9b1f2009-04-19 21:20:32 +00003329 // -fdollars-in-identifiers default varies depending on platform and
3330 // language; only pass if specified.
Mike Stump11289f42009-09-09 15:08:12 +00003331 if (Arg *A = Args.getLastArg(options::OPT_fdollars_in_identifiers,
Daniel Dunbarf5e9b1f2009-04-19 21:20:32 +00003332 options::OPT_fno_dollars_in_identifiers)) {
3333 if (A->getOption().matches(options::OPT_fdollars_in_identifiers))
Daniel Dunbar15cef0e2009-12-16 20:10:18 +00003334 CmdArgs.push_back("-fdollars-in-identifiers");
Daniel Dunbarf5e9b1f2009-04-19 21:20:32 +00003335 else
Daniel Dunbar15cef0e2009-12-16 20:10:18 +00003336 CmdArgs.push_back("-fno-dollars-in-identifiers");
Daniel Dunbarf5e9b1f2009-04-19 21:20:32 +00003337 }
3338
Daniel Dunbaradeeb052009-05-22 19:02:20 +00003339 // -funit-at-a-time is default, and we don't support -fno-unit-at-a-time for
3340 // practical purposes.
Mike Stump11289f42009-09-09 15:08:12 +00003341 if (Arg *A = Args.getLastArg(options::OPT_funit_at_a_time,
Daniel Dunbaradeeb052009-05-22 19:02:20 +00003342 options::OPT_fno_unit_at_a_time)) {
3343 if (A->getOption().matches(options::OPT_fno_unit_at_a_time))
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003344 D.Diag(diag::warn_drv_clang_unsupported) << A->getAsString(Args);
Daniel Dunbaradeeb052009-05-22 19:02:20 +00003345 }
Eli Friedmanbb0d9a52009-07-14 21:58:17 +00003346
Eli Friedman055c9702011-11-02 01:53:16 +00003347 if (Args.hasFlag(options::OPT_fapple_pragma_pack,
3348 options::OPT_fno_apple_pragma_pack, false))
3349 CmdArgs.push_back("-fapple-pragma-pack");
3350
Daniel Dunbar2ffe0292009-09-10 03:37:02 +00003351 // Default to -fno-builtin-str{cat,cpy} on Darwin for ARM.
Daniel Dunbar4fa08112009-09-10 04:57:27 +00003352 //
Daniel Dunbar6c536aa2009-12-11 23:00:49 +00003353 // FIXME: This is disabled until clang -cc1 supports -fno-builtin-foo. PR4941.
Daniel Dunbar4fa08112009-09-10 04:57:27 +00003354#if 0
Bob Wilson6524dd32011-10-14 05:03:44 +00003355 if (getToolChain().getTriple().isOSDarwin() &&
Daniel Dunbar2ffe0292009-09-10 03:37:02 +00003356 (getToolChain().getTriple().getArch() == llvm::Triple::arm ||
3357 getToolChain().getTriple().getArch() == llvm::Triple::thumb)) {
3358 if (!Args.hasArg(options::OPT_fbuiltin_strcat))
3359 CmdArgs.push_back("-fno-builtin-strcat");
3360 if (!Args.hasArg(options::OPT_fbuiltin_strcpy))
3361 CmdArgs.push_back("-fno-builtin-strcpy");
3362 }
Daniel Dunbar4fa08112009-09-10 04:57:27 +00003363#endif
Daniel Dunbar2ffe0292009-09-10 03:37:02 +00003364
Daniel Dunbar8c3d7352011-03-18 21:23:40 +00003365 // Only allow -traditional or -traditional-cpp outside in preprocessing modes.
Mike Stump11289f42009-09-09 15:08:12 +00003366 if (Arg *A = Args.getLastArg(options::OPT_traditional,
Daniel Dunbar8c3d7352011-03-18 21:23:40 +00003367 options::OPT_traditional_cpp)) {
3368 if (isa<PreprocessJobAction>(JA))
3369 CmdArgs.push_back("-traditional-cpp");
Eric Christopher84fbdb42011-08-19 00:30:14 +00003370 else
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003371 D.Diag(diag::err_drv_clang_unsupported) << A->getAsString(Args);
Daniel Dunbar8c3d7352011-03-18 21:23:40 +00003372 }
Eli Friedmanbb0d9a52009-07-14 21:58:17 +00003373
Daniel Dunbara2aedc62009-03-18 10:01:51 +00003374 Args.AddLastArg(CmdArgs, options::OPT_dM);
Chris Lattnercac63f32009-04-12 01:56:53 +00003375 Args.AddLastArg(CmdArgs, options::OPT_dD);
Ted Kremeneke73d9ed2011-11-11 00:07:43 +00003376
3377 // Handle serialized diagnostics.
3378 if (Arg *A = Args.getLastArg(options::OPT__serialize_diags)) {
3379 CmdArgs.push_back("-serialize-diagnostic-file");
Richard Smithbd55daf2012-11-01 04:30:05 +00003380 CmdArgs.push_back(Args.MakeArgString(A->getValue()));
Ted Kremeneke73d9ed2011-11-11 00:07:43 +00003381 }
Daniel Dunbara2aedc62009-03-18 10:01:51 +00003382
Ted Kremenekb47e6bc2012-09-13 06:41:18 +00003383 if (Args.hasArg(options::OPT_fretain_comments_from_system_headers))
3384 CmdArgs.push_back("-fretain-comments-from-system-headers");
3385
Dmitri Gribenkoacf2e782013-02-22 14:21:27 +00003386 // Forward -fcomment-block-commands to -cc1.
3387 Args.AddAllArgs(CmdArgs, options::OPT_fcomment_block_commands);
Dmitri Gribenkoa7d16ce2013-04-10 15:35:17 +00003388 // Forward -fparse-all-comments to -cc1.
3389 Args.AddAllArgs(CmdArgs, options::OPT_fparse_all_comments);
Dmitri Gribenkoacf2e782013-02-22 14:21:27 +00003390
Daniel Dunbar76fa8402010-04-15 06:09:03 +00003391 // Forward -Xclang arguments to -cc1, and -mllvm arguments to the LLVM option
3392 // parser.
Daniel Dunbara2aedc62009-03-18 10:01:51 +00003393 Args.AddAllArgValues(CmdArgs, options::OPT_Xclang);
Daniel Dunbar76fa8402010-04-15 06:09:03 +00003394 for (arg_iterator it = Args.filtered_begin(options::OPT_mllvm),
3395 ie = Args.filtered_end(); it != ie; ++it) {
Daniel Dunbara442fd52010-06-11 22:00:13 +00003396 (*it)->claim();
Daniel Dunbar88534f42010-04-17 06:10:00 +00003397
Daniel Dunbar76fa8402010-04-15 06:09:03 +00003398 // We translate this by hand to the -cc1 argument, since nightly test uses
3399 // it and developers have been trained to spell it with -mllvm.
Richard Smithbd55daf2012-11-01 04:30:05 +00003400 if (StringRef((*it)->getValue(0)) == "-disable-llvm-optzns")
Daniel Dunbar76fa8402010-04-15 06:09:03 +00003401 CmdArgs.push_back("-disable-llvm-optzns");
3402 else
Daniel Dunbara442fd52010-06-11 22:00:13 +00003403 (*it)->render(Args, CmdArgs);
Daniel Dunbar76fa8402010-04-15 06:09:03 +00003404 }
Daniel Dunbara2aedc62009-03-18 10:01:51 +00003405
Daniel Dunbard67a3222009-03-30 06:36:42 +00003406 if (Output.getType() == types::TY_Dependencies) {
3407 // Handled with other dependency code.
Daniel Dunbar5cdf3e02009-03-19 07:29:38 +00003408 } else if (Output.isFilename()) {
Daniel Dunbara3246a02009-03-18 08:07:30 +00003409 CmdArgs.push_back("-o");
Daniel Dunbar5cdf3e02009-03-19 07:29:38 +00003410 CmdArgs.push_back(Output.getFilename());
3411 } else {
3412 assert(Output.isNothing() && "Invalid output.");
Daniel Dunbara3246a02009-03-18 08:07:30 +00003413 }
3414
Daniel Dunbara2aedc62009-03-18 10:01:51 +00003415 for (InputInfoList::const_iterator
3416 it = Inputs.begin(), ie = Inputs.end(); it != ie; ++it) {
3417 const InputInfo &II = *it;
3418 CmdArgs.push_back("-x");
Fariborz Jahanian659bc4a2012-09-28 19:05:17 +00003419 if (Args.hasArg(options::OPT_rewrite_objc))
3420 CmdArgs.push_back(types::getTypeName(types::TY_PP_ObjCXX));
3421 else
3422 CmdArgs.push_back(types::getTypeName(II.getType()));
Daniel Dunbarb440f562010-08-02 02:38:21 +00003423 if (II.isFilename())
Daniel Dunbar5cdf3e02009-03-19 07:29:38 +00003424 CmdArgs.push_back(II.getFilename());
Daniel Dunbara2aedc62009-03-18 10:01:51 +00003425 else
Daniel Dunbar5cdf3e02009-03-19 07:29:38 +00003426 II.getInputArg().renderAsInput(Args, CmdArgs);
Daniel Dunbara2aedc62009-03-18 10:01:51 +00003427 }
Daniel Dunbard067f7f2009-04-08 23:54:23 +00003428
Chris Lattnere9d7d782009-11-03 19:50:27 +00003429 Args.AddAllArgs(CmdArgs, options::OPT_undef);
3430
Daniel Dunbarb31b76f2010-07-18 21:16:15 +00003431 const char *Exec = getToolChain().getDriver().getClangProgramPath();
Daniel Dunbar24c7f5e2009-12-18 02:43:17 +00003432
3433 // Optionally embed the -cc1 level arguments into the debug info, for build
3434 // analysis.
3435 if (getToolChain().UseDwarfDebugFlags()) {
Daniel Dunbar7f3d9502010-06-04 18:47:06 +00003436 ArgStringList OriginalArgs;
3437 for (ArgList::const_iterator it = Args.begin(),
3438 ie = Args.end(); it != ie; ++it)
3439 (*it)->render(Args, OriginalArgs);
Daniel Dunbarfe6c97b2010-08-24 16:47:49 +00003440
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00003441 SmallString<256> Flags;
Daniel Dunbar24c7f5e2009-12-18 02:43:17 +00003442 Flags += Exec;
Daniel Dunbar7f3d9502010-06-04 18:47:06 +00003443 for (unsigned i = 0, e = OriginalArgs.size(); i != e; ++i) {
Daniel Dunbar24c7f5e2009-12-18 02:43:17 +00003444 Flags += " ";
Daniel Dunbar7f3d9502010-06-04 18:47:06 +00003445 Flags += OriginalArgs[i];
Daniel Dunbar24c7f5e2009-12-18 02:43:17 +00003446 }
3447 CmdArgs.push_back("-dwarf-debug-flags");
3448 CmdArgs.push_back(Args.MakeArgString(Flags.str()));
3449 }
3450
Eric Christopherd3804002013-02-22 20:12:52 +00003451 // Add the split debug info name to the command lines here so we
3452 // can propagate it to the backend.
3453 bool SplitDwarf = Args.hasArg(options::OPT_gsplit_dwarf) &&
3454 (getToolChain().getTriple().getOS() == llvm::Triple::Linux) &&
Eric Christopherf1545832013-02-22 23:50:16 +00003455 (isa<AssembleJobAction>(JA) || isa<CompileJobAction>(JA));
Eric Christopherd3804002013-02-22 20:12:52 +00003456 const char *SplitDwarfOut;
3457 if (SplitDwarf) {
3458 CmdArgs.push_back("-split-dwarf-file");
3459 SplitDwarfOut = SplitDebugName(Args, Inputs);
3460 CmdArgs.push_back(SplitDwarfOut);
3461 }
3462
3463 // Finally add the compile command to the compilation.
Daniel Dunbar7fbaf532010-08-02 02:38:28 +00003464 C.addCommand(new Command(JA, *this, Exec, CmdArgs));
Daniel Dunbar17731772009-03-23 19:03:36 +00003465
Eric Christopherf1545832013-02-22 23:50:16 +00003466 // Handle the debug info splitting at object creation time if we're
3467 // creating an object.
Eric Christopher248357f2013-02-21 22:35:01 +00003468 // TODO: Currently only works on linux with newer objcopy.
Eric Christopherf1545832013-02-22 23:50:16 +00003469 if (SplitDwarf && !isa<CompileJobAction>(JA))
Eric Christopherd3804002013-02-22 20:12:52 +00003470 SplitDebugInfo(getToolChain(), C, *this, JA, Args, Output, SplitDwarfOut);
Eric Christopher248357f2013-02-21 22:35:01 +00003471
Roman Divacky178e01602011-02-10 16:52:03 +00003472 if (Arg *A = Args.getLastArg(options::OPT_pg))
3473 if (Args.hasArg(options::OPT_fomit_frame_pointer))
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003474 D.Diag(diag::err_drv_argument_not_allowed_with)
Roman Divacky178e01602011-02-10 16:52:03 +00003475 << "-fomit-frame-pointer" << A->getAsString(Args);
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00003476
Daniel Dunbarc2a71892009-04-03 20:51:31 +00003477 // Claim some arguments which clang supports automatically.
3478
Daniel Dunbar3e0cac62010-04-15 06:18:42 +00003479 // -fpch-preprocess is used with gcc to add a special marker in the output to
3480 // include the PCH file. Clang's PTH solution is completely transparent, so we
3481 // do not need to deal with it at all.
Daniel Dunbarc2a71892009-04-03 20:51:31 +00003482 Args.ClaimAllArgs(options::OPT_fpch_preprocess);
Daniel Dunbard067f7f2009-04-08 23:54:23 +00003483
Daniel Dunbar17731772009-03-23 19:03:36 +00003484 // Claim some arguments which clang doesn't support, but we don't
3485 // care to warn the user about.
Daniel Dunbar44b36ee2009-11-25 11:53:23 +00003486 Args.ClaimAllArgs(options::OPT_clang_ignored_f_Group);
3487 Args.ClaimAllArgs(options::OPT_clang_ignored_m_Group);
Rafael Espindola22f603032011-02-28 23:29:45 +00003488
Rafael Espindolad95a8122011-03-01 05:25:27 +00003489 // Disable warnings for clang -E -use-gold-plugin -emit-llvm foo.c
Rafael Espindola22f603032011-02-28 23:29:45 +00003490 Args.ClaimAllArgs(options::OPT_use_gold_plugin);
Rafael Espindolad95a8122011-03-01 05:25:27 +00003491 Args.ClaimAllArgs(options::OPT_emit_llvm);
Daniel Dunbar1a093d22009-03-18 06:00:36 +00003492}
3493
Jim Grosbach576452b2012-02-10 20:37:10 +00003494void ClangAs::AddARMTargetArgs(const ArgList &Args,
3495 ArgStringList &CmdArgs) const {
3496 const Driver &D = getToolChain().getDriver();
3497 llvm::Triple Triple = getToolChain().getTriple();
3498
3499 // Set the CPU based on -march= and -mcpu=.
3500 CmdArgs.push_back("-target-cpu");
Benjamin Kramer09811c72012-06-26 22:20:06 +00003501 CmdArgs.push_back(Args.MakeArgString(getARMTargetCPU(Args, Triple)));
Jim Grosbach576452b2012-02-10 20:37:10 +00003502
3503 // Honor -mfpu=.
Chad Rosiercfbfc582012-04-04 20:51:35 +00003504 if (const Arg *A = Args.getLastArg(options::OPT_mfpu_EQ))
Chad Rosierd1717c12012-04-04 20:56:36 +00003505 addFPUArgs(D, A, Args, CmdArgs);
Chad Rosier1f0e52e2012-04-04 20:39:32 +00003506
3507 // Honor -mfpmath=.
3508 if (const Arg *A = Args.getLastArg(options::OPT_mfpmath_EQ))
Chad Rosier45619cb2012-04-04 22:13:40 +00003509 addFPMathArgs(D, A, Args, CmdArgs, getARMTargetCPU(Args, Triple));
Jim Grosbach576452b2012-02-10 20:37:10 +00003510}
3511
Benjamin Kramerb164d672013-02-22 20:55:17 +00003512void ClangAs::AddX86TargetArgs(const ArgList &Args,
3513 ArgStringList &CmdArgs) const {
3514 // Set the CPU based on -march=.
3515 if (const char *CPUName = getX86TargetCPU(Args, getToolChain().getTriple())) {
3516 CmdArgs.push_back("-target-cpu");
3517 CmdArgs.push_back(CPUName);
3518 }
3519}
3520
John McCall5fb5df92012-06-20 06:18:46 +00003521/// Add options related to the Objective-C runtime/ABI.
3522///
3523/// Returns true if the runtime is non-fragile.
3524ObjCRuntime Clang::AddObjCRuntimeArgs(const ArgList &args,
3525 ArgStringList &cmdArgs,
3526 RewriteKind rewriteKind) const {
3527 // Look for the controlling runtime option.
3528 Arg *runtimeArg = args.getLastArg(options::OPT_fnext_runtime,
3529 options::OPT_fgnu_runtime,
3530 options::OPT_fobjc_runtime_EQ);
3531
3532 // Just forward -fobjc-runtime= to the frontend. This supercedes
3533 // options about fragility.
3534 if (runtimeArg &&
3535 runtimeArg->getOption().matches(options::OPT_fobjc_runtime_EQ)) {
3536 ObjCRuntime runtime;
Richard Smithbd55daf2012-11-01 04:30:05 +00003537 StringRef value = runtimeArg->getValue();
John McCall5fb5df92012-06-20 06:18:46 +00003538 if (runtime.tryParse(value)) {
3539 getToolChain().getDriver().Diag(diag::err_drv_unknown_objc_runtime)
3540 << value;
3541 }
3542
3543 runtimeArg->render(args, cmdArgs);
3544 return runtime;
3545 }
3546
3547 // Otherwise, we'll need the ABI "version". Version numbers are
3548 // slightly confusing for historical reasons:
3549 // 1 - Traditional "fragile" ABI
3550 // 2 - Non-fragile ABI, version 1
3551 // 3 - Non-fragile ABI, version 2
3552 unsigned objcABIVersion = 1;
3553 // If -fobjc-abi-version= is present, use that to set the version.
3554 if (Arg *abiArg = args.getLastArg(options::OPT_fobjc_abi_version_EQ)) {
Richard Smithbd55daf2012-11-01 04:30:05 +00003555 StringRef value = abiArg->getValue();
John McCall5fb5df92012-06-20 06:18:46 +00003556 if (value == "1")
3557 objcABIVersion = 1;
3558 else if (value == "2")
3559 objcABIVersion = 2;
3560 else if (value == "3")
3561 objcABIVersion = 3;
3562 else
3563 getToolChain().getDriver().Diag(diag::err_drv_clang_unsupported)
3564 << value;
3565 } else {
3566 // Otherwise, determine if we are using the non-fragile ABI.
3567 bool nonFragileABIIsDefault =
3568 (rewriteKind == RK_NonFragile ||
3569 (rewriteKind == RK_None &&
3570 getToolChain().IsObjCNonFragileABIDefault()));
3571 if (args.hasFlag(options::OPT_fobjc_nonfragile_abi,
3572 options::OPT_fno_objc_nonfragile_abi,
3573 nonFragileABIIsDefault)) {
3574 // Determine the non-fragile ABI version to use.
3575#ifdef DISABLE_DEFAULT_NONFRAGILEABI_TWO
3576 unsigned nonFragileABIVersion = 1;
3577#else
3578 unsigned nonFragileABIVersion = 2;
3579#endif
3580
3581 if (Arg *abiArg = args.getLastArg(
3582 options::OPT_fobjc_nonfragile_abi_version_EQ)) {
Richard Smithbd55daf2012-11-01 04:30:05 +00003583 StringRef value = abiArg->getValue();
John McCall5fb5df92012-06-20 06:18:46 +00003584 if (value == "1")
3585 nonFragileABIVersion = 1;
3586 else if (value == "2")
3587 nonFragileABIVersion = 2;
3588 else
3589 getToolChain().getDriver().Diag(diag::err_drv_clang_unsupported)
3590 << value;
3591 }
3592
3593 objcABIVersion = 1 + nonFragileABIVersion;
3594 } else {
3595 objcABIVersion = 1;
3596 }
3597 }
3598
3599 // We don't actually care about the ABI version other than whether
3600 // it's non-fragile.
3601 bool isNonFragile = objcABIVersion != 1;
3602
3603 // If we have no runtime argument, ask the toolchain for its default runtime.
3604 // However, the rewriter only really supports the Mac runtime, so assume that.
3605 ObjCRuntime runtime;
3606 if (!runtimeArg) {
3607 switch (rewriteKind) {
3608 case RK_None:
3609 runtime = getToolChain().getDefaultObjCRuntime(isNonFragile);
3610 break;
3611 case RK_Fragile:
3612 runtime = ObjCRuntime(ObjCRuntime::FragileMacOSX, VersionTuple());
3613 break;
3614 case RK_NonFragile:
3615 runtime = ObjCRuntime(ObjCRuntime::MacOSX, VersionTuple());
3616 break;
3617 }
3618
3619 // -fnext-runtime
3620 } else if (runtimeArg->getOption().matches(options::OPT_fnext_runtime)) {
3621 // On Darwin, make this use the default behavior for the toolchain.
3622 if (getToolChain().getTriple().isOSDarwin()) {
3623 runtime = getToolChain().getDefaultObjCRuntime(isNonFragile);
3624
3625 // Otherwise, build for a generic macosx port.
3626 } else {
3627 runtime = ObjCRuntime(ObjCRuntime::MacOSX, VersionTuple());
3628 }
3629
3630 // -fgnu-runtime
3631 } else {
3632 assert(runtimeArg->getOption().matches(options::OPT_fgnu_runtime));
David Chisnall314896c2012-07-04 10:37:03 +00003633 // Legacy behaviour is to target the gnustep runtime if we are i
3634 // non-fragile mode or the GCC runtime in fragile mode.
3635 if (isNonFragile)
David Chisnalla5f59412012-10-16 15:11:55 +00003636 runtime = ObjCRuntime(ObjCRuntime::GNUstep, VersionTuple(1,6));
David Chisnall314896c2012-07-04 10:37:03 +00003637 else
3638 runtime = ObjCRuntime(ObjCRuntime::GCC, VersionTuple());
John McCall5fb5df92012-06-20 06:18:46 +00003639 }
3640
3641 cmdArgs.push_back(args.MakeArgString(
3642 "-fobjc-runtime=" + runtime.getAsString()));
3643 return runtime;
3644}
3645
Daniel Dunbar4f5e79c2010-05-20 21:30:13 +00003646void ClangAs::ConstructJob(Compilation &C, const JobAction &JA,
Daniel Dunbar4f5e79c2010-05-20 21:30:13 +00003647 const InputInfo &Output,
3648 const InputInfoList &Inputs,
3649 const ArgList &Args,
3650 const char *LinkingOutput) const {
Daniel Dunbar4f5e79c2010-05-20 21:30:13 +00003651 ArgStringList CmdArgs;
3652
3653 assert(Inputs.size() == 1 && "Unexpected number of inputs.");
3654 const InputInfo &Input = Inputs[0];
3655
Rafael Espindolacfaadda2010-11-17 22:13:25 +00003656 // Don't warn about "clang -w -c foo.s"
3657 Args.ClaimAllArgs(options::OPT_w);
Rafael Espindolad95a8122011-03-01 05:25:27 +00003658 // and "clang -emit-llvm -c foo.s"
3659 Args.ClaimAllArgs(options::OPT_emit_llvm);
3660 // and "clang -use-gold-plugin -c foo.s"
3661 Args.ClaimAllArgs(options::OPT_use_gold_plugin);
Rafael Espindolacfaadda2010-11-17 22:13:25 +00003662
Daniel Dunbar4f5e79c2010-05-20 21:30:13 +00003663 // Invoke ourselves in -cc1as mode.
3664 //
3665 // FIXME: Implement custom jobs for internal actions.
3666 CmdArgs.push_back("-cc1as");
3667
3668 // Add the "effective" target triple.
3669 CmdArgs.push_back("-triple");
Chad Rosierd3a0f952011-09-20 20:44:06 +00003670 std::string TripleStr =
3671 getToolChain().ComputeEffectiveClangTriple(Args, Input.getType());
Daniel Dunbar4f5e79c2010-05-20 21:30:13 +00003672 CmdArgs.push_back(Args.MakeArgString(TripleStr));
3673
3674 // Set the output mode, we currently only expect to be used as a real
3675 // assembler.
3676 CmdArgs.push_back("-filetype");
3677 CmdArgs.push_back("obj");
3678
Eric Christopher45f2e712012-12-18 00:31:10 +00003679 // Set the main file name, so that debug info works even with
3680 // -save-temps or preprocessed assembly.
3681 CmdArgs.push_back("-main-file-name");
3682 CmdArgs.push_back(Clang::getBaseInputName(Args, Inputs));
3683
Joerg Sonnenbergeref317a22011-05-06 14:35:16 +00003684 if (UseRelaxAll(C, Args))
Daniel Dunbar99ca8b72010-05-28 16:43:21 +00003685 CmdArgs.push_back("-relax-all");
Daniel Dunbar06e2cc32010-05-27 06:18:05 +00003686
Jim Grosbach576452b2012-02-10 20:37:10 +00003687 // Add target specific cpu and features flags.
3688 switch(getToolChain().getTriple().getArch()) {
3689 default:
3690 break;
3691
3692 case llvm::Triple::arm:
3693 case llvm::Triple::thumb:
3694 AddARMTargetArgs(Args, CmdArgs);
3695 break;
Benjamin Kramerb164d672013-02-22 20:55:17 +00003696
3697 case llvm::Triple::x86:
3698 case llvm::Triple::x86_64:
3699 AddX86TargetArgs(Args, CmdArgs);
3700 break;
Jim Grosbach576452b2012-02-10 20:37:10 +00003701 }
3702
Daniel Dunbar1d733e22011-03-17 17:37:29 +00003703 // Ignore explicit -force_cpusubtype_ALL option.
3704 (void) Args.hasArg(options::OPT_force__cpusubtype__ALL);
Daniel Dunbar4f5e79c2010-05-20 21:30:13 +00003705
Eric Christopherfc3ee562012-01-10 00:38:01 +00003706 // Determine the original source input.
3707 const Action *SourceAction = &JA;
3708 while (SourceAction->getKind() != Action::InputClass) {
3709 assert(!SourceAction->getInputs().empty() && "unexpected root action!");
3710 SourceAction = SourceAction->getInputs()[0];
3711 }
3712
Chandler Carruth4d5e1a92012-12-17 21:40:04 +00003713 // Forward -g and handle debug info related flags, assuming we are dealing
3714 // with an actual assembly file.
Eric Christopherfc3ee562012-01-10 00:38:01 +00003715 if (SourceAction->getType() == types::TY_Asm ||
3716 SourceAction->getType() == types::TY_PP_Asm) {
3717 Args.ClaimAllArgs(options::OPT_g_Group);
3718 if (Arg *A = Args.getLastArg(options::OPT_g_Group))
3719 if (!A->getOption().matches(options::OPT_g0))
3720 CmdArgs.push_back("-g");
Chandler Carruth4d5e1a92012-12-17 21:40:04 +00003721
3722 // Add the -fdebug-compilation-dir flag if needed.
3723 addDebugCompDirArg(Args, CmdArgs);
Kevin Enderbyae2ec472013-01-17 21:38:06 +00003724
3725 // Set the AT_producer to the clang version when using the integrated
3726 // assembler on assembly source files.
3727 CmdArgs.push_back("-dwarf-debug-producer");
3728 CmdArgs.push_back(Args.MakeArgString(getClangFullVersion()));
Eric Christopherfc3ee562012-01-10 00:38:01 +00003729 }
Kevin Enderby292dc082011-12-22 19:31:58 +00003730
3731 // Optionally embed the -cc1as level arguments into the debug info, for build
3732 // analysis.
3733 if (getToolChain().UseDwarfDebugFlags()) {
3734 ArgStringList OriginalArgs;
3735 for (ArgList::const_iterator it = Args.begin(),
3736 ie = Args.end(); it != ie; ++it)
3737 (*it)->render(Args, OriginalArgs);
3738
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00003739 SmallString<256> Flags;
Kevin Enderby292dc082011-12-22 19:31:58 +00003740 const char *Exec = getToolChain().getDriver().getClangProgramPath();
3741 Flags += Exec;
3742 for (unsigned i = 0, e = OriginalArgs.size(); i != e; ++i) {
3743 Flags += " ";
3744 Flags += OriginalArgs[i];
3745 }
3746 CmdArgs.push_back("-dwarf-debug-flags");
3747 CmdArgs.push_back(Args.MakeArgString(Flags.str()));
3748 }
Daniel Dunbar4f5e79c2010-05-20 21:30:13 +00003749
3750 // FIXME: Add -static support, once we have it.
3751
3752 Args.AddAllArgValues(CmdArgs, options::OPT_Wa_COMMA,
3753 options::OPT_Xassembler);
Daniel Dunbar252e8f92011-04-29 17:53:18 +00003754 Args.AddAllArgs(CmdArgs, options::OPT_mllvm);
Daniel Dunbar4f5e79c2010-05-20 21:30:13 +00003755
3756 assert(Output.isFilename() && "Unexpected lipo output.");
3757 CmdArgs.push_back("-o");
3758 CmdArgs.push_back(Output.getFilename());
3759
Daniel Dunbarb440f562010-08-02 02:38:21 +00003760 assert(Input.isFilename() && "Invalid input.");
3761 CmdArgs.push_back(Input.getFilename());
Daniel Dunbar4f5e79c2010-05-20 21:30:13 +00003762
Daniel Dunbarb31b76f2010-07-18 21:16:15 +00003763 const char *Exec = getToolChain().getDriver().getClangProgramPath();
Daniel Dunbar7fbaf532010-08-02 02:38:28 +00003764 C.addCommand(new Command(JA, *this, Exec, CmdArgs));
Eric Christophera75018a2013-04-10 21:30:40 +00003765
3766 // Handle the debug info splitting at object creation time if we're
3767 // creating an object.
3768 // TODO: Currently only works on linux with newer objcopy.
3769 if (Args.hasArg(options::OPT_gsplit_dwarf) &&
3770 (getToolChain().getTriple().getOS() == llvm::Triple::Linux))
3771 SplitDebugInfo(getToolChain(), C, *this, JA, Args, Output,
3772 SplitDebugName(Args, Inputs));
Daniel Dunbar4f5e79c2010-05-20 21:30:13 +00003773}
3774
Daniel Dunbara3246a02009-03-18 08:07:30 +00003775void gcc::Common::ConstructJob(Compilation &C, const JobAction &JA,
Daniel Dunbara3246a02009-03-18 08:07:30 +00003776 const InputInfo &Output,
3777 const InputInfoList &Inputs,
Daniel Dunbara2aedc62009-03-18 10:01:51 +00003778 const ArgList &Args,
Daniel Dunbara3246a02009-03-18 08:07:30 +00003779 const char *LinkingOutput) const {
Daniel Dunbar083edf72009-12-21 18:54:17 +00003780 const Driver &D = getToolChain().getDriver();
Daniel Dunbara3246a02009-03-18 08:07:30 +00003781 ArgStringList CmdArgs;
Daniel Dunbar1a093d22009-03-18 06:00:36 +00003782
Daniel Dunbard067f7f2009-04-08 23:54:23 +00003783 for (ArgList::const_iterator
Daniel Dunbara2aedc62009-03-18 10:01:51 +00003784 it = Args.begin(), ie = Args.end(); it != ie; ++it) {
Daniel Dunbara3246a02009-03-18 08:07:30 +00003785 Arg *A = *it;
Michael J. Spencer66e2b202012-10-19 22:37:06 +00003786 if (forwardToGCC(A->getOption())) {
Daniel Dunbar939c1212010-08-03 16:14:14 +00003787 // Don't forward any -g arguments to assembly steps.
3788 if (isa<AssembleJobAction>(JA) &&
3789 A->getOption().matches(options::OPT_g_Group))
3790 continue;
3791
Daniel Dunbar2da02722009-03-19 07:55:12 +00003792 // It is unfortunate that we have to claim here, as this means
3793 // we will basically never report anything interesting for
Daniel Dunbar5716d872009-05-02 21:41:52 +00003794 // platforms using a generic gcc, even if we are just using gcc
3795 // to get to the assembler.
Daniel Dunbar2da02722009-03-19 07:55:12 +00003796 A->claim();
Daniel Dunbara2aedc62009-03-18 10:01:51 +00003797 A->render(Args, CmdArgs);
Daniel Dunbar2da02722009-03-19 07:55:12 +00003798 }
Daniel Dunbara3246a02009-03-18 08:07:30 +00003799 }
Daniel Dunbard067f7f2009-04-08 23:54:23 +00003800
Daniel Dunbar4e295052010-01-25 22:35:08 +00003801 RenderExtraToolArgs(JA, CmdArgs);
Daniel Dunbara3246a02009-03-18 08:07:30 +00003802
3803 // If using a driver driver, force the arch.
Rafael Espindola35ca7d92012-10-07 04:44:33 +00003804 llvm::Triple::ArchType Arch = getToolChain().getArch();
Bob Wilson6524dd32011-10-14 05:03:44 +00003805 if (getToolChain().getTriple().isOSDarwin()) {
Daniel Dunbara3246a02009-03-18 08:07:30 +00003806 CmdArgs.push_back("-arch");
Daniel Dunbar0a05d932009-04-01 20:33:11 +00003807
3808 // FIXME: Remove these special cases.
Rafael Espindola35ca7d92012-10-07 04:44:33 +00003809 if (Arch == llvm::Triple::ppc)
Daniel Dunbar5bbebfe2009-05-22 02:21:04 +00003810 CmdArgs.push_back("ppc");
Rafael Espindola35ca7d92012-10-07 04:44:33 +00003811 else if (Arch == llvm::Triple::ppc64)
Daniel Dunbar5bbebfe2009-05-22 02:21:04 +00003812 CmdArgs.push_back("ppc64");
3813 else
Rafael Espindola35ca7d92012-10-07 04:44:33 +00003814 CmdArgs.push_back(Args.MakeArgString(getToolChain().getArchName()));
Daniel Dunbara3246a02009-03-18 08:07:30 +00003815 }
3816
Daniel Dunbar5716d872009-05-02 21:41:52 +00003817 // Try to force gcc to match the tool chain we want, if we recognize
3818 // the arch.
Daniel Dunbar5bbebfe2009-05-22 02:21:04 +00003819 //
3820 // FIXME: The triple class should directly provide the information we want
3821 // here.
Rafael Espindola35ca7d92012-10-07 04:44:33 +00003822 if (Arch == llvm::Triple::x86 || Arch == llvm::Triple::ppc)
Daniel Dunbar5716d872009-05-02 21:41:52 +00003823 CmdArgs.push_back("-m32");
Hans Wennborgc5f4c362013-03-20 07:34:27 +00003824 else if (Arch == llvm::Triple::x86_64 || Arch == llvm::Triple::ppc64)
Daniel Dunbar5716d872009-05-02 21:41:52 +00003825 CmdArgs.push_back("-m64");
3826
Daniel Dunbarb440f562010-08-02 02:38:21 +00003827 if (Output.isFilename()) {
Daniel Dunbara3246a02009-03-18 08:07:30 +00003828 CmdArgs.push_back("-o");
Daniel Dunbar5cdf3e02009-03-19 07:29:38 +00003829 CmdArgs.push_back(Output.getFilename());
3830 } else {
3831 assert(Output.isNothing() && "Unexpected output");
Daniel Dunbara3246a02009-03-18 08:07:30 +00003832 CmdArgs.push_back("-fsyntax-only");
Daniel Dunbar5cdf3e02009-03-19 07:29:38 +00003833 }
Daniel Dunbara3246a02009-03-18 08:07:30 +00003834
Tony Linthicum76329bf2011-12-12 21:14:55 +00003835 Args.AddAllArgValues(CmdArgs, options::OPT_Wa_COMMA,
3836 options::OPT_Xassembler);
Daniel Dunbara3246a02009-03-18 08:07:30 +00003837
3838 // Only pass -x if gcc will understand it; otherwise hope gcc
3839 // understands the suffix correctly. The main use case this would go
3840 // wrong in is for linker inputs if they happened to have an odd
3841 // suffix; really the only way to get this to happen is a command
3842 // like '-x foobar a.c' which will treat a.c like a linker input.
3843 //
3844 // FIXME: For the linker case specifically, can we safely convert
3845 // inputs into '-Wl,' options?
3846 for (InputInfoList::const_iterator
3847 it = Inputs.begin(), ie = Inputs.end(); it != ie; ++it) {
3848 const InputInfo &II = *it;
Daniel Dunbare3e263f2009-05-02 20:14:53 +00003849
Daniel Dunbar6cdf83c2009-09-01 16:57:46 +00003850 // Don't try to pass LLVM or AST inputs to a generic gcc.
Daniel Dunbar24e52992010-06-07 23:28:45 +00003851 if (II.getType() == types::TY_LLVM_IR || II.getType() == types::TY_LTO_IR ||
3852 II.getType() == types::TY_LLVM_BC || II.getType() == types::TY_LTO_BC)
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003853 D.Diag(diag::err_drv_no_linker_llvm_support)
Daniel Dunbar1c28f1e2009-09-09 22:32:48 +00003854 << getToolChain().getTripleString();
Daniel Dunbar6cdf83c2009-09-01 16:57:46 +00003855 else if (II.getType() == types::TY_AST)
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003856 D.Diag(diag::err_drv_no_ast_support)
Daniel Dunbar1c28f1e2009-09-09 22:32:48 +00003857 << getToolChain().getTripleString();
Douglas Gregorbf7fc9c2013-03-27 16:47:18 +00003858 else if (II.getType() == types::TY_ModuleFile)
3859 D.Diag(diag::err_drv_no_module_support)
3860 << getToolChain().getTripleString();
Daniel Dunbare3e263f2009-05-02 20:14:53 +00003861
Daniel Dunbara3246a02009-03-18 08:07:30 +00003862 if (types::canTypeBeUserSpecified(II.getType())) {
3863 CmdArgs.push_back("-x");
3864 CmdArgs.push_back(types::getTypeName(II.getType()));
3865 }
3866
Daniel Dunbarb440f562010-08-02 02:38:21 +00003867 if (II.isFilename())
Daniel Dunbar5cdf3e02009-03-19 07:29:38 +00003868 CmdArgs.push_back(II.getFilename());
Daniel Dunbarf2476752010-09-25 18:10:05 +00003869 else {
3870 const Arg &A = II.getInputArg();
3871
3872 // Reverse translate some rewritten options.
3873 if (A.getOption().matches(options::OPT_Z_reserved_lib_stdcxx)) {
3874 CmdArgs.push_back("-lstdc++");
3875 continue;
3876 }
3877
Daniel Dunbar5cdf3e02009-03-19 07:29:38 +00003878 // Don't render as input, we need gcc to do the translations.
Daniel Dunbarf2476752010-09-25 18:10:05 +00003879 A.render(Args, CmdArgs);
3880 }
Daniel Dunbara3246a02009-03-18 08:07:30 +00003881 }
3882
Dylan Noblesmith70e73a32011-04-09 13:31:59 +00003883 const std::string customGCCName = D.getCCCGenericGCCName();
3884 const char *GCCName;
3885 if (!customGCCName.empty())
3886 GCCName = customGCCName.c_str();
3887 else if (D.CCCIsCXX) {
Dylan Noblesmith70e73a32011-04-09 13:31:59 +00003888 GCCName = "g++";
Dylan Noblesmith70e73a32011-04-09 13:31:59 +00003889 } else
3890 GCCName = "gcc";
3891
Daniel Dunbard067f7f2009-04-08 23:54:23 +00003892 const char *Exec =
Daniel Dunbar9c3ed5f2010-07-14 18:46:23 +00003893 Args.MakeArgString(getToolChain().GetProgramPath(GCCName));
Daniel Dunbar7fbaf532010-08-02 02:38:28 +00003894 C.addCommand(new Command(JA, *this, Exec, CmdArgs));
Daniel Dunbar1a093d22009-03-18 06:00:36 +00003895}
3896
Daniel Dunbar4e295052010-01-25 22:35:08 +00003897void gcc::Preprocess::RenderExtraToolArgs(const JobAction &JA,
3898 ArgStringList &CmdArgs) const {
Daniel Dunbara3246a02009-03-18 08:07:30 +00003899 CmdArgs.push_back("-E");
Daniel Dunbar1a093d22009-03-18 06:00:36 +00003900}
3901
Daniel Dunbar4e295052010-01-25 22:35:08 +00003902void gcc::Precompile::RenderExtraToolArgs(const JobAction &JA,
3903 ArgStringList &CmdArgs) const {
Daniel Dunbara3246a02009-03-18 08:07:30 +00003904 // The type is good enough.
Daniel Dunbar1a093d22009-03-18 06:00:36 +00003905}
3906
Daniel Dunbar4e295052010-01-25 22:35:08 +00003907void gcc::Compile::RenderExtraToolArgs(const JobAction &JA,
3908 ArgStringList &CmdArgs) const {
Daniel Dunbaraeea8ac2010-02-11 03:16:21 +00003909 const Driver &D = getToolChain().getDriver();
3910
Daniel Dunbar4e295052010-01-25 22:35:08 +00003911 // If -flto, etc. are present then make sure not to force assembly output.
Daniel Dunbar24e52992010-06-07 23:28:45 +00003912 if (JA.getType() == types::TY_LLVM_IR || JA.getType() == types::TY_LTO_IR ||
3913 JA.getType() == types::TY_LLVM_BC || JA.getType() == types::TY_LTO_BC)
Daniel Dunbar4e295052010-01-25 22:35:08 +00003914 CmdArgs.push_back("-c");
Daniel Dunbaraeea8ac2010-02-11 03:16:21 +00003915 else {
3916 if (JA.getType() != types::TY_PP_Asm)
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003917 D.Diag(diag::err_drv_invalid_gcc_output_type)
Daniel Dunbaraeea8ac2010-02-11 03:16:21 +00003918 << getTypeName(JA.getType());
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00003919
Daniel Dunbar4e295052010-01-25 22:35:08 +00003920 CmdArgs.push_back("-S");
Daniel Dunbaraeea8ac2010-02-11 03:16:21 +00003921 }
Daniel Dunbar1a093d22009-03-18 06:00:36 +00003922}
3923
Daniel Dunbar4e295052010-01-25 22:35:08 +00003924void gcc::Assemble::RenderExtraToolArgs(const JobAction &JA,
3925 ArgStringList &CmdArgs) const {
Daniel Dunbara3246a02009-03-18 08:07:30 +00003926 CmdArgs.push_back("-c");
Daniel Dunbar1a093d22009-03-18 06:00:36 +00003927}
Daniel Dunbara3246a02009-03-18 08:07:30 +00003928
Daniel Dunbar4e295052010-01-25 22:35:08 +00003929void gcc::Link::RenderExtraToolArgs(const JobAction &JA,
3930 ArgStringList &CmdArgs) const {
Daniel Dunbara3246a02009-03-18 08:07:30 +00003931 // The types are (hopefully) good enough.
3932}
3933
Tony Linthicum76329bf2011-12-12 21:14:55 +00003934// Hexagon tools start.
3935void hexagon::Assemble::RenderExtraToolArgs(const JobAction &JA,
3936 ArgStringList &CmdArgs) const {
3937
3938}
3939void hexagon::Assemble::ConstructJob(Compilation &C, const JobAction &JA,
3940 const InputInfo &Output,
3941 const InputInfoList &Inputs,
3942 const ArgList &Args,
3943 const char *LinkingOutput) const {
3944
3945 const Driver &D = getToolChain().getDriver();
3946 ArgStringList CmdArgs;
3947
3948 std::string MarchString = "-march=";
Matthew Curtisf10a5952012-12-06 14:16:43 +00003949 MarchString += toolchains::Hexagon_TC::GetTargetCPU(Args);
Tony Linthicum76329bf2011-12-12 21:14:55 +00003950 CmdArgs.push_back(Args.MakeArgString(MarchString));
3951
3952 RenderExtraToolArgs(JA, CmdArgs);
3953
3954 if (Output.isFilename()) {
3955 CmdArgs.push_back("-o");
3956 CmdArgs.push_back(Output.getFilename());
3957 } else {
3958 assert(Output.isNothing() && "Unexpected output");
3959 CmdArgs.push_back("-fsyntax-only");
3960 }
3961
Matthew Curtise8f80a12012-12-06 17:49:03 +00003962 std::string SmallDataThreshold = GetHexagonSmallDataThresholdValue(Args);
3963 if (!SmallDataThreshold.empty())
3964 CmdArgs.push_back(
3965 Args.MakeArgString(std::string("-G") + SmallDataThreshold));
Tony Linthicum76329bf2011-12-12 21:14:55 +00003966
Matthew Curtise5df3812012-12-07 17:23:04 +00003967 Args.AddAllArgValues(CmdArgs, options::OPT_Wa_COMMA,
3968 options::OPT_Xassembler);
3969
Tony Linthicum76329bf2011-12-12 21:14:55 +00003970 // Only pass -x if gcc will understand it; otherwise hope gcc
3971 // understands the suffix correctly. The main use case this would go
3972 // wrong in is for linker inputs if they happened to have an odd
3973 // suffix; really the only way to get this to happen is a command
3974 // like '-x foobar a.c' which will treat a.c like a linker input.
3975 //
3976 // FIXME: For the linker case specifically, can we safely convert
3977 // inputs into '-Wl,' options?
3978 for (InputInfoList::const_iterator
3979 it = Inputs.begin(), ie = Inputs.end(); it != ie; ++it) {
3980 const InputInfo &II = *it;
3981
3982 // Don't try to pass LLVM or AST inputs to a generic gcc.
3983 if (II.getType() == types::TY_LLVM_IR || II.getType() == types::TY_LTO_IR ||
3984 II.getType() == types::TY_LLVM_BC || II.getType() == types::TY_LTO_BC)
3985 D.Diag(clang::diag::err_drv_no_linker_llvm_support)
3986 << getToolChain().getTripleString();
3987 else if (II.getType() == types::TY_AST)
3988 D.Diag(clang::diag::err_drv_no_ast_support)
3989 << getToolChain().getTripleString();
Douglas Gregorbf7fc9c2013-03-27 16:47:18 +00003990 else if (II.getType() == types::TY_ModuleFile)
3991 D.Diag(diag::err_drv_no_module_support)
3992 << getToolChain().getTripleString();
Tony Linthicum76329bf2011-12-12 21:14:55 +00003993
3994 if (II.isFilename())
3995 CmdArgs.push_back(II.getFilename());
3996 else
3997 // Don't render as input, we need gcc to do the translations. FIXME: Pranav: What is this ?
3998 II.getInputArg().render(Args, CmdArgs);
3999 }
4000
4001 const char *GCCName = "hexagon-as";
4002 const char *Exec =
4003 Args.MakeArgString(getToolChain().GetProgramPath(GCCName));
4004 C.addCommand(new Command(JA, *this, Exec, CmdArgs));
4005
4006}
4007void hexagon::Link::RenderExtraToolArgs(const JobAction &JA,
4008 ArgStringList &CmdArgs) const {
4009 // The types are (hopefully) good enough.
4010}
4011
4012void hexagon::Link::ConstructJob(Compilation &C, const JobAction &JA,
4013 const InputInfo &Output,
4014 const InputInfoList &Inputs,
4015 const ArgList &Args,
4016 const char *LinkingOutput) const {
4017
Matthew Curtise689b052012-12-06 15:46:07 +00004018 const toolchains::Hexagon_TC& ToolChain =
4019 static_cast<const toolchains::Hexagon_TC&>(getToolChain());
4020 const Driver &D = ToolChain.getDriver();
4021
Tony Linthicum76329bf2011-12-12 21:14:55 +00004022 ArgStringList CmdArgs;
4023
Matthew Curtise689b052012-12-06 15:46:07 +00004024 //----------------------------------------------------------------------------
4025 //
4026 //----------------------------------------------------------------------------
4027 bool hasStaticArg = Args.hasArg(options::OPT_static);
4028 bool buildingLib = Args.hasArg(options::OPT_shared);
Matthew Curtise8f80a12012-12-06 17:49:03 +00004029 bool buildPIE = Args.hasArg(options::OPT_pie);
Matthew Curtise689b052012-12-06 15:46:07 +00004030 bool incStdLib = !Args.hasArg(options::OPT_nostdlib);
4031 bool incStartFiles = !Args.hasArg(options::OPT_nostartfiles);
4032 bool incDefLibs = !Args.hasArg(options::OPT_nodefaultlibs);
4033 bool useShared = buildingLib && !hasStaticArg;
Tony Linthicum76329bf2011-12-12 21:14:55 +00004034
Matthew Curtise689b052012-12-06 15:46:07 +00004035 //----------------------------------------------------------------------------
4036 // Silence warnings for various options
4037 //----------------------------------------------------------------------------
Tony Linthicum76329bf2011-12-12 21:14:55 +00004038
Matthew Curtise689b052012-12-06 15:46:07 +00004039 Args.ClaimAllArgs(options::OPT_g_Group);
4040 Args.ClaimAllArgs(options::OPT_emit_llvm);
4041 Args.ClaimAllArgs(options::OPT_w); // Other warning options are already
4042 // handled somewhere else.
4043 Args.ClaimAllArgs(options::OPT_static_libgcc);
4044
4045 //----------------------------------------------------------------------------
4046 //
4047 //----------------------------------------------------------------------------
4048 for (std::vector<std::string>::const_iterator i = ToolChain.ExtraOpts.begin(),
4049 e = ToolChain.ExtraOpts.end();
4050 i != e; ++i)
4051 CmdArgs.push_back(i->c_str());
Tony Linthicum76329bf2011-12-12 21:14:55 +00004052
Matthew Curtisf10a5952012-12-06 14:16:43 +00004053 std::string MarchString = toolchains::Hexagon_TC::GetTargetCPU(Args);
4054 CmdArgs.push_back(Args.MakeArgString("-m" + MarchString));
Sebastian Pop86500282012-01-13 20:37:10 +00004055
Matthew Curtise689b052012-12-06 15:46:07 +00004056 if (buildingLib) {
4057 CmdArgs.push_back("-shared");
4058 CmdArgs.push_back("-call_shared"); // should be the default, but doing as
4059 // hexagon-gcc does
Tony Linthicum76329bf2011-12-12 21:14:55 +00004060 }
4061
Matthew Curtise689b052012-12-06 15:46:07 +00004062 if (hasStaticArg)
4063 CmdArgs.push_back("-static");
Tony Linthicum76329bf2011-12-12 21:14:55 +00004064
Matthew Curtise8f80a12012-12-06 17:49:03 +00004065 if (buildPIE && !buildingLib)
4066 CmdArgs.push_back("-pie");
4067
4068 std::string SmallDataThreshold = GetHexagonSmallDataThresholdValue(Args);
4069 if (!SmallDataThreshold.empty()) {
4070 CmdArgs.push_back(
4071 Args.MakeArgString(std::string("-G") + SmallDataThreshold));
4072 }
4073
Matthew Curtise689b052012-12-06 15:46:07 +00004074 //----------------------------------------------------------------------------
4075 //
4076 //----------------------------------------------------------------------------
4077 CmdArgs.push_back("-o");
4078 CmdArgs.push_back(Output.getFilename());
Tony Linthicum76329bf2011-12-12 21:14:55 +00004079
Matthew Curtise689b052012-12-06 15:46:07 +00004080 const std::string MarchSuffix = "/" + MarchString;
4081 const std::string G0Suffix = "/G0";
4082 const std::string MarchG0Suffix = MarchSuffix + G0Suffix;
4083 const std::string RootDir = toolchains::Hexagon_TC::GetGnuDir(D.InstalledDir)
4084 + "/";
4085 const std::string StartFilesDir = RootDir
4086 + "hexagon/lib"
4087 + (buildingLib
4088 ? MarchG0Suffix : MarchSuffix);
4089
4090 //----------------------------------------------------------------------------
4091 // moslib
4092 //----------------------------------------------------------------------------
4093 std::vector<std::string> oslibs;
4094 bool hasStandalone= false;
4095
4096 for (arg_iterator it = Args.filtered_begin(options::OPT_moslib_EQ),
4097 ie = Args.filtered_end(); it != ie; ++it) {
4098 (*it)->claim();
4099 oslibs.push_back((*it)->getValue());
4100 hasStandalone = hasStandalone || (oslibs.back() == "standalone");
Tony Linthicum76329bf2011-12-12 21:14:55 +00004101 }
Matthew Curtise689b052012-12-06 15:46:07 +00004102 if (oslibs.empty()) {
4103 oslibs.push_back("standalone");
4104 hasStandalone = true;
4105 }
Tony Linthicum76329bf2011-12-12 21:14:55 +00004106
Matthew Curtise689b052012-12-06 15:46:07 +00004107 //----------------------------------------------------------------------------
4108 // Start Files
4109 //----------------------------------------------------------------------------
4110 if (incStdLib && incStartFiles) {
4111
4112 if (!buildingLib) {
4113 if (hasStandalone) {
4114 CmdArgs.push_back(
4115 Args.MakeArgString(StartFilesDir + "/crt0_standalone.o"));
4116 }
4117 CmdArgs.push_back(Args.MakeArgString(StartFilesDir + "/crt0.o"));
4118 }
4119 std::string initObj = useShared ? "/initS.o" : "/init.o";
4120 CmdArgs.push_back(Args.MakeArgString(StartFilesDir + initObj));
4121 }
4122
4123 //----------------------------------------------------------------------------
4124 // Library Search Paths
4125 //----------------------------------------------------------------------------
4126 const ToolChain::path_list &LibPaths = ToolChain.getFilePaths();
4127 for (ToolChain::path_list::const_iterator
4128 i = LibPaths.begin(),
4129 e = LibPaths.end();
4130 i != e;
4131 ++i)
4132 CmdArgs.push_back(Args.MakeArgString(StringRef("-L") + *i));
4133
4134 //----------------------------------------------------------------------------
4135 //
4136 //----------------------------------------------------------------------------
4137 Args.AddAllArgs(CmdArgs, options::OPT_T_Group);
4138 Args.AddAllArgs(CmdArgs, options::OPT_e);
4139 Args.AddAllArgs(CmdArgs, options::OPT_s);
4140 Args.AddAllArgs(CmdArgs, options::OPT_t);
4141 Args.AddAllArgs(CmdArgs, options::OPT_u_Group);
4142
4143 AddLinkerInputs(ToolChain, Inputs, Args, CmdArgs);
4144
4145 //----------------------------------------------------------------------------
4146 // Libraries
4147 //----------------------------------------------------------------------------
4148 if (incStdLib && incDefLibs) {
4149 if (D.CCCIsCXX) {
4150 ToolChain.AddCXXStdlibLibArgs(Args, CmdArgs);
4151 CmdArgs.push_back("-lm");
4152 }
4153
4154 CmdArgs.push_back("--start-group");
4155
4156 if (!buildingLib) {
4157 for(std::vector<std::string>::iterator i = oslibs.begin(),
4158 e = oslibs.end(); i != e; ++i)
4159 CmdArgs.push_back(Args.MakeArgString("-l" + *i));
4160 CmdArgs.push_back("-lc");
4161 }
4162 CmdArgs.push_back("-lgcc");
4163
4164 CmdArgs.push_back("--end-group");
4165 }
4166
4167 //----------------------------------------------------------------------------
4168 // End files
4169 //----------------------------------------------------------------------------
4170 if (incStdLib && incStartFiles) {
4171 std::string finiObj = useShared ? "/finiS.o" : "/fini.o";
4172 CmdArgs.push_back(Args.MakeArgString(StartFilesDir + finiObj));
4173 }
4174
4175 std::string Linker = ToolChain.GetProgramPath("hexagon-ld");
4176 C.addCommand(
4177 new Command(
4178 JA, *this,
4179 Args.MakeArgString(Linker), CmdArgs));
Tony Linthicum76329bf2011-12-12 21:14:55 +00004180}
4181// Hexagon tools end.
4182
Rafael Espindoladcbf6982012-10-31 18:51:07 +00004183llvm::Triple::ArchType darwin::getArchTypeForDarwinArchName(StringRef Str) {
4184 // See arch(3) and llvm-gcc's driver-driver.c. We don't implement support for
4185 // archs which Darwin doesn't use.
4186
4187 // The matching this routine does is fairly pointless, since it is neither the
4188 // complete architecture list, nor a reasonable subset. The problem is that
4189 // historically the driver driver accepts this and also ties its -march=
4190 // handling to the architecture name, so we need to be careful before removing
4191 // support for it.
4192
4193 // This code must be kept in sync with Clang's Darwin specific argument
4194 // translation.
4195
4196 return llvm::StringSwitch<llvm::Triple::ArchType>(Str)
4197 .Cases("ppc", "ppc601", "ppc603", "ppc604", "ppc604e", llvm::Triple::ppc)
4198 .Cases("ppc750", "ppc7400", "ppc7450", "ppc970", llvm::Triple::ppc)
4199 .Case("ppc64", llvm::Triple::ppc64)
4200 .Cases("i386", "i486", "i486SX", "i586", "i686", llvm::Triple::x86)
4201 .Cases("pentium", "pentpro", "pentIIm3", "pentIIm5", "pentium4",
4202 llvm::Triple::x86)
4203 .Case("x86_64", llvm::Triple::x86_64)
4204 // This is derived from the driver driver.
Bob Wilson743bf672013-03-04 22:37:49 +00004205 .Cases("arm", "armv4t", "armv5", "armv6", "armv6m", llvm::Triple::arm)
4206 .Cases("armv7", "armv7em", "armv7f", "armv7k", "armv7m", llvm::Triple::arm)
4207 .Cases("armv7s", "xscale", llvm::Triple::arm)
Rafael Espindoladcbf6982012-10-31 18:51:07 +00004208 .Case("r600", llvm::Triple::r600)
4209 .Case("nvptx", llvm::Triple::nvptx)
4210 .Case("nvptx64", llvm::Triple::nvptx64)
4211 .Case("amdil", llvm::Triple::amdil)
4212 .Case("spir", llvm::Triple::spir)
4213 .Default(llvm::Triple::UnknownArch);
4214}
Tony Linthicum76329bf2011-12-12 21:14:55 +00004215
Bob Wilsondecc03e2012-11-23 06:14:39 +00004216const char *Clang::getBaseInputName(const ArgList &Args,
4217 const InputInfoList &Inputs) {
Michael J. Spencere1696752010-12-18 00:19:12 +00004218 return Args.MakeArgString(
4219 llvm::sys::path::filename(Inputs[0].getBaseInput()));
Daniel Dunbarafec1f52009-03-29 18:40:18 +00004220}
4221
Bob Wilsondecc03e2012-11-23 06:14:39 +00004222const char *Clang::getBaseInputStem(const ArgList &Args,
4223 const InputInfoList &Inputs) {
Daniel Dunbarafec1f52009-03-29 18:40:18 +00004224 const char *Str = getBaseInputName(Args, Inputs);
4225
Chris Lattner906bb902011-01-16 08:14:11 +00004226 if (const char *End = strrchr(Str, '.'))
Daniel Dunbar1c28f1e2009-09-09 22:32:48 +00004227 return Args.MakeArgString(std::string(Str, End));
Daniel Dunbarafec1f52009-03-29 18:40:18 +00004228
4229 return Str;
4230}
4231
Bob Wilsondecc03e2012-11-23 06:14:39 +00004232const char *Clang::getDependencyFileName(const ArgList &Args,
4233 const InputInfoList &Inputs) {
Daniel Dunbarafec1f52009-03-29 18:40:18 +00004234 // FIXME: Think about this more.
4235 std::string Res;
4236
4237 if (Arg *OutputOpt = Args.getLastArg(options::OPT_o)) {
Richard Smithbd55daf2012-11-01 04:30:05 +00004238 std::string Str(OutputOpt->getValue());
Daniel Dunbarafec1f52009-03-29 18:40:18 +00004239 Res = Str.substr(0, Str.rfind('.'));
Chad Rosier6fdf38b2011-08-17 23:08:45 +00004240 } else {
Bob Wilsondecc03e2012-11-23 06:14:39 +00004241 Res = getBaseInputStem(Args, Inputs);
Chad Rosier6fdf38b2011-08-17 23:08:45 +00004242 }
Daniel Dunbar1c28f1e2009-09-09 22:32:48 +00004243 return Args.MakeArgString(Res + ".d");
Daniel Dunbarafec1f52009-03-29 18:40:18 +00004244}
4245
Daniel Dunbarbe220842009-03-20 16:06:39 +00004246void darwin::Assemble::ConstructJob(Compilation &C, const JobAction &JA,
Daniel Dunbar7fbaf532010-08-02 02:38:28 +00004247 const InputInfo &Output,
Daniel Dunbard067f7f2009-04-08 23:54:23 +00004248 const InputInfoList &Inputs,
4249 const ArgList &Args,
Daniel Dunbarbe220842009-03-20 16:06:39 +00004250 const char *LinkingOutput) const {
4251 ArgStringList CmdArgs;
4252
4253 assert(Inputs.size() == 1 && "Unexpected number of inputs.");
4254 const InputInfo &Input = Inputs[0];
4255
Daniel Dunbardc8355e2011-04-12 23:59:20 +00004256 // Determine the original source input.
4257 const Action *SourceAction = &JA;
4258 while (SourceAction->getKind() != Action::InputClass) {
4259 assert(!SourceAction->getInputs().empty() && "unexpected root action!");
4260 SourceAction = SourceAction->getInputs()[0];
4261 }
4262
4263 // Forward -g, assuming we are dealing with an actual assembly file.
Eric Christopher84fbdb42011-08-19 00:30:14 +00004264 if (SourceAction->getType() == types::TY_Asm ||
Daniel Dunbardc8355e2011-04-12 23:59:20 +00004265 SourceAction->getType() == types::TY_PP_Asm) {
Daniel Dunbar5c9c1182009-04-01 00:27:44 +00004266 if (Args.hasArg(options::OPT_gstabs))
4267 CmdArgs.push_back("--gstabs");
4268 else if (Args.hasArg(options::OPT_g_Group))
Bob Wilson126c4912011-11-02 05:10:45 +00004269 CmdArgs.push_back("-g");
Daniel Dunbar5c9c1182009-04-01 00:27:44 +00004270 }
Daniel Dunbard067f7f2009-04-08 23:54:23 +00004271
Daniel Dunbarbe220842009-03-20 16:06:39 +00004272 // Derived from asm spec.
Daniel Dunbar3571dd92009-09-09 18:36:27 +00004273 AddDarwinArch(Args, CmdArgs);
Daniel Dunbarbe220842009-03-20 16:06:39 +00004274
Daniel Dunbar6d484762010-07-22 01:47:22 +00004275 // Use -force_cpusubtype_ALL on x86 by default.
4276 if (getToolChain().getTriple().getArch() == llvm::Triple::x86 ||
4277 getToolChain().getTriple().getArch() == llvm::Triple::x86_64 ||
Daniel Dunbar3571dd92009-09-09 18:36:27 +00004278 Args.hasArg(options::OPT_force__cpusubtype__ALL))
4279 CmdArgs.push_back("-force_cpusubtype_ALL");
4280
Daniel Dunbara6b4a3d2009-08-24 22:26:16 +00004281 if (getToolChain().getTriple().getArch() != llvm::Triple::x86_64 &&
Daniel Dunbarbd847cc2012-10-15 22:23:53 +00004282 (((Args.hasArg(options::OPT_mkernel) ||
Eric Christopher248357f2013-02-21 22:35:01 +00004283 Args.hasArg(options::OPT_fapple_kext)) &&
Daniel Dunbarbd847cc2012-10-15 22:23:53 +00004284 (!getDarwinToolChain().isTargetIPhoneOS() ||
4285 getDarwinToolChain().isIPhoneOSVersionLT(6, 0))) ||
4286 Args.hasArg(options::OPT_static)))
Daniel Dunbard067f7f2009-04-08 23:54:23 +00004287 CmdArgs.push_back("-static");
4288
Daniel Dunbarbe220842009-03-20 16:06:39 +00004289 Args.AddAllArgValues(CmdArgs, options::OPT_Wa_COMMA,
4290 options::OPT_Xassembler);
4291
4292 assert(Output.isFilename() && "Unexpected lipo output.");
4293 CmdArgs.push_back("-o");
4294 CmdArgs.push_back(Output.getFilename());
4295
Daniel Dunbarb440f562010-08-02 02:38:21 +00004296 assert(Input.isFilename() && "Invalid input.");
4297 CmdArgs.push_back(Input.getFilename());
Daniel Dunbarbe220842009-03-20 16:06:39 +00004298
4299 // asm_final spec is empty.
4300
Daniel Dunbard067f7f2009-04-08 23:54:23 +00004301 const char *Exec =
Daniel Dunbar9c3ed5f2010-07-14 18:46:23 +00004302 Args.MakeArgString(getToolChain().GetProgramPath("as"));
Daniel Dunbar7fbaf532010-08-02 02:38:28 +00004303 C.addCommand(new Command(JA, *this, Exec, CmdArgs));
Daniel Dunbarbe220842009-03-20 16:06:39 +00004304}
Daniel Dunbar64ed5e32009-03-20 00:52:38 +00004305
David Blaikie68e081d2011-12-20 02:48:34 +00004306void darwin::DarwinTool::anchor() {}
4307
Daniel Dunbare9ded432009-09-09 18:36:20 +00004308void darwin::DarwinTool::AddDarwinArch(const ArgList &Args,
4309 ArgStringList &CmdArgs) const {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00004310 StringRef ArchName = getDarwinToolChain().getDarwinArchName(Args);
Daniel Dunbardcc3b652010-01-22 02:04:58 +00004311
Daniel Dunbarc1964212009-03-26 16:23:12 +00004312 // Derived from darwin_arch spec.
4313 CmdArgs.push_back("-arch");
Daniel Dunbardcc3b652010-01-22 02:04:58 +00004314 CmdArgs.push_back(Args.MakeArgString(ArchName));
Daniel Dunbar91dbfd62009-09-04 18:35:31 +00004315
Daniel Dunbardcc3b652010-01-22 02:04:58 +00004316 // FIXME: Is this needed anymore?
4317 if (ArchName == "arm")
Daniel Dunbar91dbfd62009-09-04 18:35:31 +00004318 CmdArgs.push_back("-force_cpusubtype_ALL");
Daniel Dunbarc1964212009-03-26 16:23:12 +00004319}
4320
Bill Wendling3b2000f2012-10-02 18:02:50 +00004321bool darwin::Link::NeedsTempPath(const InputInfoList &Inputs) const {
4322 // We only need to generate a temp path for LTO if we aren't compiling object
4323 // files. When compiling source files, we run 'dsymutil' after linking. We
4324 // don't run 'dsymutil' when compiling object files.
4325 for (InputInfoList::const_iterator
4326 it = Inputs.begin(), ie = Inputs.end(); it != ie; ++it)
4327 if (it->getType() != types::TY_Object)
4328 return true;
4329
4330 return false;
4331}
4332
Daniel Dunbarccbc4522010-09-09 21:51:05 +00004333void darwin::Link::AddLinkArgs(Compilation &C,
4334 const ArgList &Args,
Bill Wendling3b2000f2012-10-02 18:02:50 +00004335 ArgStringList &CmdArgs,
4336 const InputInfoList &Inputs) const {
Daniel Dunbar083edf72009-12-21 18:54:17 +00004337 const Driver &D = getToolChain().getDriver();
Daniel Dunbarc44d3132011-04-28 21:23:41 +00004338 const toolchains::Darwin &DarwinTC = getDarwinToolChain();
Daniel Dunbarc1964212009-03-26 16:23:12 +00004339
Daniel Dunbarcacb0e22010-08-11 23:07:50 +00004340 unsigned Version[3] = { 0, 0, 0 };
4341 if (Arg *A = Args.getLastArg(options::OPT_mlinker_version_EQ)) {
4342 bool HadExtra;
Richard Smithbd55daf2012-11-01 04:30:05 +00004343 if (!Driver::GetReleaseVersion(A->getValue(), Version[0],
Daniel Dunbarcacb0e22010-08-11 23:07:50 +00004344 Version[1], Version[2], HadExtra) ||
4345 HadExtra)
Chris Lattner0e62c1c2011-07-23 10:55:15 +00004346 D.Diag(diag::err_drv_invalid_version_number)
Daniel Dunbarcacb0e22010-08-11 23:07:50 +00004347 << A->getAsString(Args);
4348 }
4349
4350 // Newer linkers support -demangle, pass it if supported and not disabled by
4351 // the user.
Daniel Dunbarecb41cb2012-01-04 21:45:27 +00004352 if (Version[0] >= 100 && !Args.hasArg(options::OPT_Z_Xlinker__no_demangle)) {
Daniel Dunbar1eaf5c62010-09-07 17:07:49 +00004353 // Don't pass -demangle to ld_classic.
4354 //
4355 // FIXME: This is a temporary workaround, ld should be handling this.
4356 bool UsesLdClassic = (getToolChain().getArch() == llvm::Triple::x86 &&
4357 Args.hasArg(options::OPT_static));
Daniel Dunbar3d7e0e22010-09-07 17:50:41 +00004358 if (getToolChain().getArch() == llvm::Triple::x86) {
4359 for (arg_iterator it = Args.filtered_begin(options::OPT_Xlinker,
4360 options::OPT_Wl_COMMA),
4361 ie = Args.filtered_end(); it != ie; ++it) {
4362 const Arg *A = *it;
4363 for (unsigned i = 0, e = A->getNumValues(); i != e; ++i)
Richard Smithbd55daf2012-11-01 04:30:05 +00004364 if (StringRef(A->getValue(i)) == "-kext")
Daniel Dunbar3d7e0e22010-09-07 17:50:41 +00004365 UsesLdClassic = true;
4366 }
4367 }
Daniel Dunbar1eaf5c62010-09-07 17:07:49 +00004368 if (!UsesLdClassic)
4369 CmdArgs.push_back("-demangle");
Daniel Dunbarcacb0e22010-08-11 23:07:50 +00004370 }
4371
Bill Wendling313b6bf2012-11-16 23:03:00 +00004372 // If we are using LTO, then automatically create a temporary file path for
4373 // the linker to use, so that it's lifetime will extend past a possible
4374 // dsymutil step.
4375 if (Version[0] >= 116 && D.IsUsingLTO(Args) && NeedsTempPath(Inputs)) {
4376 const char *TmpPath = C.getArgs().MakeArgString(
4377 D.GetTemporaryPath("cc", types::getTypeTempSuffix(types::TY_Object)));
4378 C.addTempFile(TmpPath);
4379 CmdArgs.push_back("-object_path_lto");
4380 CmdArgs.push_back(TmpPath);
Daniel Dunbaref889c72011-06-21 20:55:11 +00004381 }
4382
Daniel Dunbarc1964212009-03-26 16:23:12 +00004383 // Derived from the "link" spec.
4384 Args.AddAllArgs(CmdArgs, options::OPT_static);
4385 if (!Args.hasArg(options::OPT_static))
4386 CmdArgs.push_back("-dynamic");
4387 if (Args.hasArg(options::OPT_fgnu_runtime)) {
4388 // FIXME: gcc replaces -lobjc in forward args with -lobjc-gnu
4389 // here. How do we wish to handle such things?
4390 }
Daniel Dunbard067f7f2009-04-08 23:54:23 +00004391
Daniel Dunbarc1964212009-03-26 16:23:12 +00004392 if (!Args.hasArg(options::OPT_dynamiclib)) {
Daniel Dunbara48823f2010-01-22 02:04:52 +00004393 AddDarwinArch(Args, CmdArgs);
Daniel Dunbara48823f2010-01-22 02:04:52 +00004394 // FIXME: Why do this only on this path?
Daniel Dunbar93d7acf2010-01-22 03:37:33 +00004395 Args.AddLastArg(CmdArgs, options::OPT_force__cpusubtype__ALL);
Daniel Dunbarc1964212009-03-26 16:23:12 +00004396
4397 Args.AddLastArg(CmdArgs, options::OPT_bundle);
4398 Args.AddAllArgs(CmdArgs, options::OPT_bundle__loader);
4399 Args.AddAllArgs(CmdArgs, options::OPT_client__name);
4400
4401 Arg *A;
4402 if ((A = Args.getLastArg(options::OPT_compatibility__version)) ||
4403 (A = Args.getLastArg(options::OPT_current__version)) ||
4404 (A = Args.getLastArg(options::OPT_install__name)))
Chris Lattner0e62c1c2011-07-23 10:55:15 +00004405 D.Diag(diag::err_drv_argument_only_allowed_with)
Daniel Dunbarc1964212009-03-26 16:23:12 +00004406 << A->getAsString(Args) << "-dynamiclib";
4407
4408 Args.AddLastArg(CmdArgs, options::OPT_force__flat__namespace);
4409 Args.AddLastArg(CmdArgs, options::OPT_keep__private__externs);
4410 Args.AddLastArg(CmdArgs, options::OPT_private__bundle);
4411 } else {
4412 CmdArgs.push_back("-dylib");
4413
4414 Arg *A;
4415 if ((A = Args.getLastArg(options::OPT_bundle)) ||
4416 (A = Args.getLastArg(options::OPT_bundle__loader)) ||
4417 (A = Args.getLastArg(options::OPT_client__name)) ||
4418 (A = Args.getLastArg(options::OPT_force__flat__namespace)) ||
4419 (A = Args.getLastArg(options::OPT_keep__private__externs)) ||
4420 (A = Args.getLastArg(options::OPT_private__bundle)))
Chris Lattner0e62c1c2011-07-23 10:55:15 +00004421 D.Diag(diag::err_drv_argument_not_allowed_with)
Daniel Dunbarc1964212009-03-26 16:23:12 +00004422 << A->getAsString(Args) << "-dynamiclib";
Daniel Dunbard067f7f2009-04-08 23:54:23 +00004423
Daniel Dunbarc1964212009-03-26 16:23:12 +00004424 Args.AddAllArgsTranslated(CmdArgs, options::OPT_compatibility__version,
4425 "-dylib_compatibility_version");
4426 Args.AddAllArgsTranslated(CmdArgs, options::OPT_current__version,
4427 "-dylib_current_version");
4428
Daniel Dunbara48823f2010-01-22 02:04:52 +00004429 AddDarwinArch(Args, CmdArgs);
Daniel Dunbarc1964212009-03-26 16:23:12 +00004430
4431 Args.AddAllArgsTranslated(CmdArgs, options::OPT_install__name,
4432 "-dylib_install_name");
4433 }
4434
4435 Args.AddLastArg(CmdArgs, options::OPT_all__load);
4436 Args.AddAllArgs(CmdArgs, options::OPT_allowable__client);
4437 Args.AddLastArg(CmdArgs, options::OPT_bind__at__load);
Daniel Dunbarc44d3132011-04-28 21:23:41 +00004438 if (DarwinTC.isTargetIPhoneOS())
Daniel Dunbar2b5f6812009-09-04 18:35:41 +00004439 Args.AddLastArg(CmdArgs, options::OPT_arch__errors__fatal);
Daniel Dunbarc1964212009-03-26 16:23:12 +00004440 Args.AddLastArg(CmdArgs, options::OPT_dead__strip);
4441 Args.AddLastArg(CmdArgs, options::OPT_no__dead__strip__inits__and__terms);
4442 Args.AddAllArgs(CmdArgs, options::OPT_dylib__file);
4443 Args.AddLastArg(CmdArgs, options::OPT_dynamic);
4444 Args.AddAllArgs(CmdArgs, options::OPT_exported__symbols__list);
4445 Args.AddLastArg(CmdArgs, options::OPT_flat__namespace);
Daniel Dunbar044a3902011-06-28 20:16:02 +00004446 Args.AddAllArgs(CmdArgs, options::OPT_force__load);
Daniel Dunbarc1964212009-03-26 16:23:12 +00004447 Args.AddAllArgs(CmdArgs, options::OPT_headerpad__max__install__names);
4448 Args.AddAllArgs(CmdArgs, options::OPT_image__base);
4449 Args.AddAllArgs(CmdArgs, options::OPT_init);
4450
Daniel Dunbarc44d3132011-04-28 21:23:41 +00004451 // Add the deployment target.
Benjamin Kramerddefa6d2012-03-10 20:55:36 +00004452 VersionTuple TargetVersion = DarwinTC.getTargetVersion();
Daniel Dunbar72ceb922011-04-30 04:22:58 +00004453
4454 // If we had an explicit -mios-simulator-version-min argument, honor that,
4455 // otherwise use the traditional deployment targets. We can't just check the
4456 // is-sim attribute because existing code follows this path, and the linker
4457 // may not handle the argument.
4458 //
4459 // FIXME: We may be able to remove this, once we can verify no one depends on
4460 // it.
4461 if (Args.hasArg(options::OPT_mios_simulator_version_min_EQ))
4462 CmdArgs.push_back("-ios_simulator_version_min");
4463 else if (DarwinTC.isTargetIPhoneOS())
4464 CmdArgs.push_back("-iphoneos_version_min");
4465 else
4466 CmdArgs.push_back("-macosx_version_min");
Benjamin Kramerddefa6d2012-03-10 20:55:36 +00004467 CmdArgs.push_back(Args.MakeArgString(TargetVersion.getAsString()));
Daniel Dunbarc44d3132011-04-28 21:23:41 +00004468
Daniel Dunbarc1964212009-03-26 16:23:12 +00004469 Args.AddLastArg(CmdArgs, options::OPT_nomultidefs);
4470 Args.AddLastArg(CmdArgs, options::OPT_multi__module);
4471 Args.AddLastArg(CmdArgs, options::OPT_single__module);
4472 Args.AddAllArgs(CmdArgs, options::OPT_multiply__defined);
4473 Args.AddAllArgs(CmdArgs, options::OPT_multiply__defined__unused);
Daniel Dunbard067f7f2009-04-08 23:54:23 +00004474
Daniel Dunbaraf68a882010-07-13 23:31:40 +00004475 if (const Arg *A = Args.getLastArg(options::OPT_fpie, options::OPT_fPIE,
4476 options::OPT_fno_pie,
4477 options::OPT_fno_PIE)) {
4478 if (A->getOption().matches(options::OPT_fpie) ||
4479 A->getOption().matches(options::OPT_fPIE))
4480 CmdArgs.push_back("-pie");
4481 else
4482 CmdArgs.push_back("-no_pie");
4483 }
Daniel Dunbarc1964212009-03-26 16:23:12 +00004484
4485 Args.AddLastArg(CmdArgs, options::OPT_prebind);
4486 Args.AddLastArg(CmdArgs, options::OPT_noprebind);
4487 Args.AddLastArg(CmdArgs, options::OPT_nofixprebinding);
4488 Args.AddLastArg(CmdArgs, options::OPT_prebind__all__twolevel__modules);
4489 Args.AddLastArg(CmdArgs, options::OPT_read__only__relocs);
4490 Args.AddAllArgs(CmdArgs, options::OPT_sectcreate);
4491 Args.AddAllArgs(CmdArgs, options::OPT_sectorder);
4492 Args.AddAllArgs(CmdArgs, options::OPT_seg1addr);
4493 Args.AddAllArgs(CmdArgs, options::OPT_segprot);
4494 Args.AddAllArgs(CmdArgs, options::OPT_segaddr);
4495 Args.AddAllArgs(CmdArgs, options::OPT_segs__read__only__addr);
4496 Args.AddAllArgs(CmdArgs, options::OPT_segs__read__write__addr);
4497 Args.AddAllArgs(CmdArgs, options::OPT_seg__addr__table);
4498 Args.AddAllArgs(CmdArgs, options::OPT_seg__addr__table__filename);
4499 Args.AddAllArgs(CmdArgs, options::OPT_sub__library);
4500 Args.AddAllArgs(CmdArgs, options::OPT_sub__umbrella);
Daniel Dunbar2b5f6812009-09-04 18:35:41 +00004501
Daniel Dunbar84384642011-05-02 21:03:47 +00004502 // Give --sysroot= preference, over the Apple specific behavior to also use
4503 // --isysroot as the syslibroot.
Sebastian Pop980920a2012-04-16 04:16:43 +00004504 StringRef sysroot = C.getSysRoot();
4505 if (sysroot != "") {
Daniel Dunbar84384642011-05-02 21:03:47 +00004506 CmdArgs.push_back("-syslibroot");
Sebastian Pop980920a2012-04-16 04:16:43 +00004507 CmdArgs.push_back(C.getArgs().MakeArgString(sysroot));
Daniel Dunbar84384642011-05-02 21:03:47 +00004508 } else if (const Arg *A = Args.getLastArg(options::OPT_isysroot)) {
4509 CmdArgs.push_back("-syslibroot");
Richard Smithbd55daf2012-11-01 04:30:05 +00004510 CmdArgs.push_back(A->getValue());
Daniel Dunbar2b5f6812009-09-04 18:35:41 +00004511 }
4512
Daniel Dunbarc1964212009-03-26 16:23:12 +00004513 Args.AddLastArg(CmdArgs, options::OPT_twolevel__namespace);
4514 Args.AddLastArg(CmdArgs, options::OPT_twolevel__namespace__hints);
4515 Args.AddAllArgs(CmdArgs, options::OPT_umbrella);
4516 Args.AddAllArgs(CmdArgs, options::OPT_undefined);
4517 Args.AddAllArgs(CmdArgs, options::OPT_unexported__symbols__list);
Daniel Dunbar2b5f6812009-09-04 18:35:41 +00004518 Args.AddAllArgs(CmdArgs, options::OPT_weak__reference__mismatches);
Daniel Dunbarc1964212009-03-26 16:23:12 +00004519 Args.AddLastArg(CmdArgs, options::OPT_X_Flag);
4520 Args.AddAllArgs(CmdArgs, options::OPT_y);
4521 Args.AddLastArg(CmdArgs, options::OPT_w);
4522 Args.AddAllArgs(CmdArgs, options::OPT_pagezero__size);
4523 Args.AddAllArgs(CmdArgs, options::OPT_segs__read__);
4524 Args.AddLastArg(CmdArgs, options::OPT_seglinkedit);
4525 Args.AddLastArg(CmdArgs, options::OPT_noseglinkedit);
4526 Args.AddAllArgs(CmdArgs, options::OPT_sectalign);
4527 Args.AddAllArgs(CmdArgs, options::OPT_sectobjectsymbols);
4528 Args.AddAllArgs(CmdArgs, options::OPT_segcreate);
4529 Args.AddLastArg(CmdArgs, options::OPT_whyload);
4530 Args.AddLastArg(CmdArgs, options::OPT_whatsloaded);
4531 Args.AddAllArgs(CmdArgs, options::OPT_dylinker__install__name);
4532 Args.AddLastArg(CmdArgs, options::OPT_dylinker);
4533 Args.AddLastArg(CmdArgs, options::OPT_Mach);
4534}
4535
4536void darwin::Link::ConstructJob(Compilation &C, const JobAction &JA,
Daniel Dunbar7fbaf532010-08-02 02:38:28 +00004537 const InputInfo &Output,
Daniel Dunbard067f7f2009-04-08 23:54:23 +00004538 const InputInfoList &Inputs,
4539 const ArgList &Args,
Daniel Dunbarc1964212009-03-26 16:23:12 +00004540 const char *LinkingOutput) const {
4541 assert(Output.getType() == types::TY_Image && "Invalid linker output type.");
Daniel Dunbarc09988d2009-09-08 16:39:16 +00004542
Daniel Dunbarc1964212009-03-26 16:23:12 +00004543 // The logic here is derived from gcc's behavior; most of which
4544 // comes from specs (starting with link_command). Consult gcc for
4545 // more information.
Daniel Dunbarc1964212009-03-26 16:23:12 +00004546 ArgStringList CmdArgs;
4547
Argyrios Kyrtzidis741fab12011-10-07 22:58:08 +00004548 /// Hack(tm) to ignore linking errors when we are doing ARC migration.
4549 if (Args.hasArg(options::OPT_ccc_arcmt_check,
4550 options::OPT_ccc_arcmt_migrate)) {
4551 for (ArgList::const_iterator I = Args.begin(), E = Args.end(); I != E; ++I)
4552 (*I)->claim();
4553 const char *Exec =
4554 Args.MakeArgString(getToolChain().GetProgramPath("touch"));
4555 CmdArgs.push_back(Output.getFilename());
4556 C.addCommand(new Command(JA, *this, Exec, CmdArgs));
4557 return;
4558 }
4559
Daniel Dunbarc1964212009-03-26 16:23:12 +00004560 // I'm not sure why this particular decomposition exists in gcc, but
4561 // we follow suite for ease of comparison.
Bill Wendling3b2000f2012-10-02 18:02:50 +00004562 AddLinkArgs(C, Args, CmdArgs, Inputs);
Daniel Dunbarc1964212009-03-26 16:23:12 +00004563
Daniel Dunbarc1964212009-03-26 16:23:12 +00004564 Args.AddAllArgs(CmdArgs, options::OPT_d_Flag);
4565 Args.AddAllArgs(CmdArgs, options::OPT_s);
4566 Args.AddAllArgs(CmdArgs, options::OPT_t);
4567 Args.AddAllArgs(CmdArgs, options::OPT_Z_Flag);
4568 Args.AddAllArgs(CmdArgs, options::OPT_u_Group);
Daniel Dunbarc1964212009-03-26 16:23:12 +00004569 Args.AddLastArg(CmdArgs, options::OPT_e);
4570 Args.AddAllArgs(CmdArgs, options::OPT_m_Separate);
4571 Args.AddAllArgs(CmdArgs, options::OPT_r);
4572
Daniel Dunbar767bbab2010-10-18 22:08:36 +00004573 // Forward -ObjC when either -ObjC or -ObjC++ is used, to force loading
4574 // members of static archive libraries which implement Objective-C classes or
4575 // categories.
4576 if (Args.hasArg(options::OPT_ObjC) || Args.hasArg(options::OPT_ObjCXX))
4577 CmdArgs.push_back("-ObjC");
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00004578
Bill Wendling58979742012-12-10 21:48:41 +00004579 if (Args.hasArg(options::OPT_rdynamic))
4580 CmdArgs.push_back("-export_dynamic");
4581
Daniel Dunbarc1964212009-03-26 16:23:12 +00004582 CmdArgs.push_back("-o");
4583 CmdArgs.push_back(Output.getFilename());
4584
Chad Rosier06fd3c62012-05-16 23:45:12 +00004585 if (!Args.hasArg(options::OPT_nostdlib) &&
Daniel Dunbarc1964212009-03-26 16:23:12 +00004586 !Args.hasArg(options::OPT_nostartfiles)) {
4587 // Derived from startfile spec.
4588 if (Args.hasArg(options::OPT_dynamiclib)) {
Daniel Dunbard067f7f2009-04-08 23:54:23 +00004589 // Derived from darwin_dylib1 spec.
Daniel Dunbar16d97092011-04-01 21:02:42 +00004590 if (getDarwinToolChain().isTargetIOSSimulator()) {
4591 // The simulator doesn't have a versioned crt1 file.
4592 CmdArgs.push_back("-ldylib1.o");
4593 } else if (getDarwinToolChain().isTargetIPhoneOS()) {
Daniel Dunbar83608032010-01-27 00:56:56 +00004594 if (getDarwinToolChain().isIPhoneOSVersionLT(3, 1))
4595 CmdArgs.push_back("-ldylib1.o");
4596 } else {
Daniel Dunbar6d23b2f2010-01-27 00:57:03 +00004597 if (getDarwinToolChain().isMacosxVersionLT(10, 5))
Daniel Dunbar83608032010-01-27 00:56:56 +00004598 CmdArgs.push_back("-ldylib1.o");
Daniel Dunbar6d23b2f2010-01-27 00:57:03 +00004599 else if (getDarwinToolChain().isMacosxVersionLT(10, 6))
Daniel Dunbar83608032010-01-27 00:56:56 +00004600 CmdArgs.push_back("-ldylib1.10.5.o");
4601 }
Daniel Dunbarc1964212009-03-26 16:23:12 +00004602 } else {
4603 if (Args.hasArg(options::OPT_bundle)) {
Daniel Dunbarae8bca02009-04-01 03:17:40 +00004604 if (!Args.hasArg(options::OPT_static)) {
4605 // Derived from darwin_bundle1 spec.
Daniel Dunbar16d97092011-04-01 21:02:42 +00004606 if (getDarwinToolChain().isTargetIOSSimulator()) {
4607 // The simulator doesn't have a versioned crt1 file.
4608 CmdArgs.push_back("-lbundle1.o");
4609 } else if (getDarwinToolChain().isTargetIPhoneOS()) {
Daniel Dunbar83608032010-01-27 00:56:56 +00004610 if (getDarwinToolChain().isIPhoneOSVersionLT(3, 1))
4611 CmdArgs.push_back("-lbundle1.o");
4612 } else {
Daniel Dunbar6d23b2f2010-01-27 00:57:03 +00004613 if (getDarwinToolChain().isMacosxVersionLT(10, 6))
Daniel Dunbar83608032010-01-27 00:56:56 +00004614 CmdArgs.push_back("-lbundle1.o");
4615 }
Daniel Dunbarae8bca02009-04-01 03:17:40 +00004616 }
Daniel Dunbarc1964212009-03-26 16:23:12 +00004617 } else {
Daniel Dunbar733b0f82011-03-01 18:49:30 +00004618 if (Args.hasArg(options::OPT_pg) &&
4619 getToolChain().SupportsProfiling()) {
Daniel Dunbarc1964212009-03-26 16:23:12 +00004620 if (Args.hasArg(options::OPT_static) ||
4621 Args.hasArg(options::OPT_object) ||
4622 Args.hasArg(options::OPT_preload)) {
4623 CmdArgs.push_back("-lgcrt0.o");
4624 } else {
4625 CmdArgs.push_back("-lgcrt1.o");
Daniel Dunbard067f7f2009-04-08 23:54:23 +00004626
Daniel Dunbarc1964212009-03-26 16:23:12 +00004627 // darwin_crt2 spec is empty.
Daniel Dunbard067f7f2009-04-08 23:54:23 +00004628 }
Bob Wilsonef285b42012-07-04 00:18:41 +00004629 // By default on OS X 10.8 and later, we don't link with a crt1.o
4630 // file and the linker knows to use _main as the entry point. But,
4631 // when compiling with -pg, we need to link with the gcrt1.o file,
4632 // so pass the -no_new_main option to tell the linker to use the
4633 // "start" symbol as the entry point.
Bob Wilson1e148fe2012-07-03 20:42:10 +00004634 if (getDarwinToolChain().isTargetMacOS() &&
4635 !getDarwinToolChain().isMacosxVersionLT(10, 8))
4636 CmdArgs.push_back("-no_new_main");
Daniel Dunbarc1964212009-03-26 16:23:12 +00004637 } else {
4638 if (Args.hasArg(options::OPT_static) ||
4639 Args.hasArg(options::OPT_object) ||
4640 Args.hasArg(options::OPT_preload)) {
4641 CmdArgs.push_back("-lcrt0.o");
4642 } else {
4643 // Derived from darwin_crt1 spec.
Daniel Dunbarebc34df2011-03-31 17:12:33 +00004644 if (getDarwinToolChain().isTargetIOSSimulator()) {
4645 // The simulator doesn't have a versioned crt1 file.
4646 CmdArgs.push_back("-lcrt1.o");
4647 } else if (getDarwinToolChain().isTargetIPhoneOS()) {
Daniel Dunbar83608032010-01-27 00:56:56 +00004648 if (getDarwinToolChain().isIPhoneOSVersionLT(3, 1))
4649 CmdArgs.push_back("-lcrt1.o");
Daniel Dunbarbd847cc2012-10-15 22:23:53 +00004650 else if (getDarwinToolChain().isIPhoneOSVersionLT(6, 0))
Daniel Dunbar83608032010-01-27 00:56:56 +00004651 CmdArgs.push_back("-lcrt1.3.1.o");
Daniel Dunbar6d23b2f2010-01-27 00:57:03 +00004652 } else {
4653 if (getDarwinToolChain().isMacosxVersionLT(10, 5))
4654 CmdArgs.push_back("-lcrt1.o");
4655 else if (getDarwinToolChain().isMacosxVersionLT(10, 6))
4656 CmdArgs.push_back("-lcrt1.10.5.o");
Ted Kremeneke65b0862012-03-06 20:05:56 +00004657 else if (getDarwinToolChain().isMacosxVersionLT(10, 8))
Daniel Dunbar6d23b2f2010-01-27 00:57:03 +00004658 CmdArgs.push_back("-lcrt1.10.6.o");
Daniel Dunbard067f7f2009-04-08 23:54:23 +00004659
Daniel Dunbar6d23b2f2010-01-27 00:57:03 +00004660 // darwin_crt2 spec is empty.
4661 }
Daniel Dunbarc1964212009-03-26 16:23:12 +00004662 }
4663 }
4664 }
4665 }
4666
Daniel Dunbar6d23b2f2010-01-27 00:57:03 +00004667 if (!getDarwinToolChain().isTargetIPhoneOS() &&
4668 Args.hasArg(options::OPT_shared_libgcc) &&
4669 getDarwinToolChain().isMacosxVersionLT(10, 5)) {
Daniel Dunbar1c28f1e2009-09-09 22:32:48 +00004670 const char *Str =
Daniel Dunbar9c3ed5f2010-07-14 18:46:23 +00004671 Args.MakeArgString(getToolChain().GetFilePath("crt3.o"));
Daniel Dunbar1c28f1e2009-09-09 22:32:48 +00004672 CmdArgs.push_back(Str);
Daniel Dunbarc1964212009-03-26 16:23:12 +00004673 }
4674 }
4675
4676 Args.AddAllArgs(CmdArgs, options::OPT_L);
Daniel Dunbard067f7f2009-04-08 23:54:23 +00004677
Peter Collingbourne54d770c2013-04-09 04:35:11 +00004678 SanitizerArgs Sanitize(getToolChain(), Args);
Alexey Samsonov969be242013-01-21 08:45:02 +00004679 // If we're building a dynamic lib with -fsanitize=address,
4680 // unresolved symbols may appear. Mark all
Alexey Samsonovcc429802012-11-16 12:53:14 +00004681 // of them as dynamic_lookup. Linking executables is handled in
4682 // lib/Driver/ToolChains.cpp.
Alexey Samsonov969be242013-01-21 08:45:02 +00004683 if (Sanitize.needsAsanRt()) {
Kostya Serebryany0b692ce2011-12-06 19:18:44 +00004684 if (Args.hasArg(options::OPT_dynamiclib) ||
4685 Args.hasArg(options::OPT_bundle)) {
4686 CmdArgs.push_back("-undefined");
4687 CmdArgs.push_back("dynamic_lookup");
4688 }
4689 }
4690
Daniel Dunbarc1964212009-03-26 16:23:12 +00004691 if (Args.hasArg(options::OPT_fopenmp))
4692 // This is more complicated in gcc...
4693 CmdArgs.push_back("-lgomp");
4694
Douglas Gregor9295df02012-05-15 21:00:27 +00004695 AddLinkerInputs(getToolChain(), Inputs, Args, CmdArgs);
4696
Bob Wilson16d93952012-05-15 18:57:39 +00004697 if (isObjCRuntimeLinked(Args) &&
4698 !Args.hasArg(options::OPT_nostdlib) &&
4699 !Args.hasArg(options::OPT_nodefaultlibs)) {
Ted Kremeneke65b0862012-03-06 20:05:56 +00004700 // Avoid linking compatibility stubs on i386 mac.
4701 if (!getDarwinToolChain().isTargetMacOS() ||
Rafael Espindola35ca7d92012-10-07 04:44:33 +00004702 getDarwinToolChain().getArch() != llvm::Triple::x86) {
Ted Kremeneke65b0862012-03-06 20:05:56 +00004703 // If we don't have ARC or subscripting runtime support, link in the
4704 // runtime stubs. We have to do this *before* adding any of the normal
4705 // linker inputs so that its initializer gets run first.
John McCall5fb5df92012-06-20 06:18:46 +00004706 ObjCRuntime runtime =
4707 getDarwinToolChain().getDefaultObjCRuntime(/*nonfragile*/ true);
Ted Kremeneke65b0862012-03-06 20:05:56 +00004708 // We use arclite library for both ARC and subscripting support.
John McCall3deb1ad2012-08-21 02:47:43 +00004709 if ((!runtime.hasNativeARC() && isObjCAutoRefCount(Args)) ||
John McCall5fb5df92012-06-20 06:18:46 +00004710 !runtime.hasSubscripting())
Ted Kremeneke65b0862012-03-06 20:05:56 +00004711 getDarwinToolChain().AddLinkARCArgs(Args, CmdArgs);
Ted Kremeneke65b0862012-03-06 20:05:56 +00004712 }
Bob Wilson7dda0cd2012-04-21 00:21:42 +00004713 CmdArgs.push_back("-framework");
4714 CmdArgs.push_back("Foundation");
Ted Kremeneke65b0862012-03-06 20:05:56 +00004715 // Link libobj.
4716 CmdArgs.push_back("-lobjc");
John McCall24fc0de2011-07-06 00:26:06 +00004717 }
John McCall31168b02011-06-15 23:02:42 +00004718
Daniel Dunbarc1964212009-03-26 16:23:12 +00004719 if (LinkingOutput) {
4720 CmdArgs.push_back("-arch_multiple");
4721 CmdArgs.push_back("-final_output");
4722 CmdArgs.push_back(LinkingOutput);
4723 }
4724
Daniel Dunbarc1964212009-03-26 16:23:12 +00004725 if (Args.hasArg(options::OPT_fnested_functions))
4726 CmdArgs.push_back("-allow_stack_execute");
Daniel Dunbard067f7f2009-04-08 23:54:23 +00004727
Daniel Dunbarc1964212009-03-26 16:23:12 +00004728 if (!Args.hasArg(options::OPT_nostdlib) &&
4729 !Args.hasArg(options::OPT_nodefaultlibs)) {
Daniel Dunbar083edf72009-12-21 18:54:17 +00004730 if (getToolChain().getDriver().CCCIsCXX)
Daniel Dunbar3f7796f2010-09-17 01:20:05 +00004731 getToolChain().AddCXXStdlibLibArgs(Args, CmdArgs);
Daniel Dunbarad0f62b2009-04-08 06:06:21 +00004732
Daniel Dunbarc1964212009-03-26 16:23:12 +00004733 // link_ssp spec is empty.
4734
Daniel Dunbar26d482a2009-09-18 08:15:03 +00004735 // Let the tool chain choose which runtime library to link.
4736 getDarwinToolChain().AddLinkRuntimeLibArgs(Args, CmdArgs);
Daniel Dunbarc1964212009-03-26 16:23:12 +00004737 }
4738
Chad Rosier06fd3c62012-05-16 23:45:12 +00004739 if (!Args.hasArg(options::OPT_nostdlib) &&
Daniel Dunbarc1964212009-03-26 16:23:12 +00004740 !Args.hasArg(options::OPT_nostartfiles)) {
4741 // endfile_spec is empty.
4742 }
4743
4744 Args.AddAllArgs(CmdArgs, options::OPT_T_Group);
4745 Args.AddAllArgs(CmdArgs, options::OPT_F);
4746
Daniel Dunbard067f7f2009-04-08 23:54:23 +00004747 const char *Exec =
Daniel Dunbar9c3ed5f2010-07-14 18:46:23 +00004748 Args.MakeArgString(getToolChain().GetProgramPath("ld"));
Daniel Dunbar7fbaf532010-08-02 02:38:28 +00004749 C.addCommand(new Command(JA, *this, Exec, CmdArgs));
Daniel Dunbarc1964212009-03-26 16:23:12 +00004750}
4751
Daniel Dunbar64ed5e32009-03-20 00:52:38 +00004752void darwin::Lipo::ConstructJob(Compilation &C, const JobAction &JA,
Daniel Dunbar7fbaf532010-08-02 02:38:28 +00004753 const InputInfo &Output,
Daniel Dunbard067f7f2009-04-08 23:54:23 +00004754 const InputInfoList &Inputs,
4755 const ArgList &Args,
Daniel Dunbar64ed5e32009-03-20 00:52:38 +00004756 const char *LinkingOutput) const {
4757 ArgStringList CmdArgs;
4758
4759 CmdArgs.push_back("-create");
4760 assert(Output.isFilename() && "Unexpected lipo output.");
Daniel Dunbar06686ab2009-03-24 00:24:37 +00004761
4762 CmdArgs.push_back("-output");
Daniel Dunbar64ed5e32009-03-20 00:52:38 +00004763 CmdArgs.push_back(Output.getFilename());
Daniel Dunbar06686ab2009-03-24 00:24:37 +00004764
Daniel Dunbar64ed5e32009-03-20 00:52:38 +00004765 for (InputInfoList::const_iterator
4766 it = Inputs.begin(), ie = Inputs.end(); it != ie; ++it) {
4767 const InputInfo &II = *it;
4768 assert(II.isFilename() && "Unexpected lipo input.");
4769 CmdArgs.push_back(II.getFilename());
4770 }
Daniel Dunbard067f7f2009-04-08 23:54:23 +00004771 const char *Exec =
Daniel Dunbar9c3ed5f2010-07-14 18:46:23 +00004772 Args.MakeArgString(getToolChain().GetProgramPath("lipo"));
Daniel Dunbar7fbaf532010-08-02 02:38:28 +00004773 C.addCommand(new Command(JA, *this, Exec, CmdArgs));
Daniel Dunbar64ed5e32009-03-20 00:52:38 +00004774}
Daniel Dunbar8eb473c2009-03-31 17:45:15 +00004775
Daniel Dunbar88299622010-06-04 18:28:36 +00004776void darwin::Dsymutil::ConstructJob(Compilation &C, const JobAction &JA,
Daniel Dunbar7fbaf532010-08-02 02:38:28 +00004777 const InputInfo &Output,
Daniel Dunbar88299622010-06-04 18:28:36 +00004778 const InputInfoList &Inputs,
4779 const ArgList &Args,
4780 const char *LinkingOutput) const {
4781 ArgStringList CmdArgs;
4782
Daniel Dunbareb86b042011-05-09 17:23:16 +00004783 CmdArgs.push_back("-o");
4784 CmdArgs.push_back(Output.getFilename());
4785
Daniel Dunbar88299622010-06-04 18:28:36 +00004786 assert(Inputs.size() == 1 && "Unable to handle multiple inputs.");
4787 const InputInfo &Input = Inputs[0];
4788 assert(Input.isFilename() && "Unexpected dsymutil input.");
4789 CmdArgs.push_back(Input.getFilename());
4790
Daniel Dunbar88299622010-06-04 18:28:36 +00004791 const char *Exec =
Daniel Dunbar9c3ed5f2010-07-14 18:46:23 +00004792 Args.MakeArgString(getToolChain().GetProgramPath("dsymutil"));
Daniel Dunbar7fbaf532010-08-02 02:38:28 +00004793 C.addCommand(new Command(JA, *this, Exec, CmdArgs));
Daniel Dunbar88299622010-06-04 18:28:36 +00004794}
4795
Eric Christopher551ef452011-08-23 17:56:55 +00004796void darwin::VerifyDebug::ConstructJob(Compilation &C, const JobAction &JA,
Eric Christopher45f2e712012-12-18 00:31:10 +00004797 const InputInfo &Output,
4798 const InputInfoList &Inputs,
4799 const ArgList &Args,
4800 const char *LinkingOutput) const {
Eric Christopher551ef452011-08-23 17:56:55 +00004801 ArgStringList CmdArgs;
4802 CmdArgs.push_back("--verify");
Eric Christopher36222212012-02-06 19:13:09 +00004803 CmdArgs.push_back("--debug-info");
4804 CmdArgs.push_back("--eh-frame");
Eric Christopher65c05fa2012-02-06 19:43:51 +00004805 CmdArgs.push_back("--quiet");
Eric Christopher551ef452011-08-23 17:56:55 +00004806
4807 assert(Inputs.size() == 1 && "Unable to handle multiple inputs.");
4808 const InputInfo &Input = Inputs[0];
4809 assert(Input.isFilename() && "Unexpected verify input");
4810
4811 // Grabbing the output of the earlier dsymutil run.
4812 CmdArgs.push_back(Input.getFilename());
4813
4814 const char *Exec =
4815 Args.MakeArgString(getToolChain().GetProgramPath("dwarfdump"));
4816 C.addCommand(new Command(JA, *this, Exec, CmdArgs));
4817}
4818
David Chisnallf571cde2012-02-15 13:39:01 +00004819void solaris::Assemble::ConstructJob(Compilation &C, const JobAction &JA,
4820 const InputInfo &Output,
4821 const InputInfoList &Inputs,
4822 const ArgList &Args,
4823 const char *LinkingOutput) const {
4824 ArgStringList CmdArgs;
4825
4826 Args.AddAllArgValues(CmdArgs, options::OPT_Wa_COMMA,
4827 options::OPT_Xassembler);
4828
4829 CmdArgs.push_back("-o");
4830 CmdArgs.push_back(Output.getFilename());
4831
4832 for (InputInfoList::const_iterator
4833 it = Inputs.begin(), ie = Inputs.end(); it != ie; ++it) {
4834 const InputInfo &II = *it;
4835 CmdArgs.push_back(II.getFilename());
4836 }
4837
4838 const char *Exec =
4839 Args.MakeArgString(getToolChain().GetProgramPath("as"));
4840 C.addCommand(new Command(JA, *this, Exec, CmdArgs));
4841}
4842
4843
4844void solaris::Link::ConstructJob(Compilation &C, const JobAction &JA,
4845 const InputInfo &Output,
4846 const InputInfoList &Inputs,
4847 const ArgList &Args,
4848 const char *LinkingOutput) const {
4849 // FIXME: Find a real GCC, don't hard-code versions here
4850 std::string GCCLibPath = "/usr/gcc/4.5/lib/gcc/";
4851 const llvm::Triple &T = getToolChain().getTriple();
4852 std::string LibPath = "/usr/lib/";
4853 llvm::Triple::ArchType Arch = T.getArch();
4854 switch (Arch) {
4855 case llvm::Triple::x86:
4856 GCCLibPath += ("i386-" + T.getVendorName() + "-" +
4857 T.getOSName()).str() + "/4.5.2/";
4858 break;
4859 case llvm::Triple::x86_64:
4860 GCCLibPath += ("i386-" + T.getVendorName() + "-" +
4861 T.getOSName()).str();
4862 GCCLibPath += "/4.5.2/amd64/";
4863 LibPath += "amd64/";
4864 break;
4865 default:
4866 assert(0 && "Unsupported architecture");
4867 }
4868
4869 ArgStringList CmdArgs;
4870
David Chisnall272a0712012-02-29 15:06:12 +00004871 // Demangle C++ names in errors
4872 CmdArgs.push_back("-C");
4873
David Chisnallf571cde2012-02-15 13:39:01 +00004874 if ((!Args.hasArg(options::OPT_nostdlib)) &&
4875 (!Args.hasArg(options::OPT_shared))) {
4876 CmdArgs.push_back("-e");
4877 CmdArgs.push_back("_start");
4878 }
4879
4880 if (Args.hasArg(options::OPT_static)) {
4881 CmdArgs.push_back("-Bstatic");
4882 CmdArgs.push_back("-dn");
4883 } else {
4884 CmdArgs.push_back("-Bdynamic");
4885 if (Args.hasArg(options::OPT_shared)) {
4886 CmdArgs.push_back("-shared");
4887 } else {
4888 CmdArgs.push_back("--dynamic-linker");
4889 CmdArgs.push_back(Args.MakeArgString(LibPath + "ld.so.1"));
4890 }
4891 }
4892
4893 if (Output.isFilename()) {
4894 CmdArgs.push_back("-o");
4895 CmdArgs.push_back(Output.getFilename());
4896 } else {
4897 assert(Output.isNothing() && "Invalid output.");
4898 }
4899
4900 if (!Args.hasArg(options::OPT_nostdlib) &&
4901 !Args.hasArg(options::OPT_nostartfiles)) {
4902 if (!Args.hasArg(options::OPT_shared)) {
4903 CmdArgs.push_back(Args.MakeArgString(LibPath + "crt1.o"));
4904 CmdArgs.push_back(Args.MakeArgString(LibPath + "crti.o"));
David Chisnall0c52c0f2012-02-28 17:10:04 +00004905 CmdArgs.push_back(Args.MakeArgString(LibPath + "values-Xa.o"));
David Chisnallf571cde2012-02-15 13:39:01 +00004906 CmdArgs.push_back(Args.MakeArgString(GCCLibPath + "crtbegin.o"));
4907 } else {
4908 CmdArgs.push_back(Args.MakeArgString(LibPath + "crti.o"));
David Chisnall0c52c0f2012-02-28 17:10:04 +00004909 CmdArgs.push_back(Args.MakeArgString(LibPath + "values-Xa.o"));
4910 CmdArgs.push_back(Args.MakeArgString(GCCLibPath + "crtbegin.o"));
David Chisnallf571cde2012-02-15 13:39:01 +00004911 }
David Chisnallddc4c9d2012-03-13 14:14:54 +00004912 if (getToolChain().getDriver().CCCIsCXX)
4913 CmdArgs.push_back(Args.MakeArgString(LibPath + "cxa_finalize.o"));
David Chisnallf571cde2012-02-15 13:39:01 +00004914 }
4915
4916 CmdArgs.push_back(Args.MakeArgString("-L" + GCCLibPath));
4917
4918 Args.AddAllArgs(CmdArgs, options::OPT_L);
4919 Args.AddAllArgs(CmdArgs, options::OPT_T_Group);
4920 Args.AddAllArgs(CmdArgs, options::OPT_e);
David Chisnall0c52c0f2012-02-28 17:10:04 +00004921 Args.AddAllArgs(CmdArgs, options::OPT_r);
David Chisnallf571cde2012-02-15 13:39:01 +00004922
4923 AddLinkerInputs(getToolChain(), Inputs, Args, CmdArgs);
4924
4925 if (!Args.hasArg(options::OPT_nostdlib) &&
4926 !Args.hasArg(options::OPT_nodefaultlibs)) {
David Chisnall3d127c72012-04-10 11:49:50 +00004927 if (getToolChain().getDriver().CCCIsCXX)
4928 getToolChain().AddCXXStdlibLibArgs(Args, CmdArgs);
David Chisnall1026fb02012-02-15 18:24:31 +00004929 CmdArgs.push_back("-lgcc_s");
David Chisnall0c52c0f2012-02-28 17:10:04 +00004930 if (!Args.hasArg(options::OPT_shared)) {
4931 CmdArgs.push_back("-lgcc");
David Chisnallf571cde2012-02-15 13:39:01 +00004932 CmdArgs.push_back("-lc");
David Chisnallc73fb892012-02-28 20:06:45 +00004933 CmdArgs.push_back("-lm");
David Chisnall0c52c0f2012-02-28 17:10:04 +00004934 }
David Chisnallf571cde2012-02-15 13:39:01 +00004935 }
4936
4937 if (!Args.hasArg(options::OPT_nostdlib) &&
4938 !Args.hasArg(options::OPT_nostartfiles)) {
David Chisnall0c52c0f2012-02-28 17:10:04 +00004939 CmdArgs.push_back(Args.MakeArgString(GCCLibPath + "crtend.o"));
David Chisnallf571cde2012-02-15 13:39:01 +00004940 }
David Chisnall96de9932012-02-16 16:00:47 +00004941 CmdArgs.push_back(Args.MakeArgString(LibPath + "crtn.o"));
David Chisnallf571cde2012-02-15 13:39:01 +00004942
4943 addProfileRT(getToolChain(), Args, CmdArgs, getToolChain().getTriple());
4944
4945 const char *Exec =
4946 Args.MakeArgString(getToolChain().GetProgramPath("ld"));
4947 C.addCommand(new Command(JA, *this, Exec, CmdArgs));
4948}
4949
Edward O'Callaghan856e4ff2009-08-22 01:06:46 +00004950void auroraux::Assemble::ConstructJob(Compilation &C, const JobAction &JA,
Daniel Dunbar7fbaf532010-08-02 02:38:28 +00004951 const InputInfo &Output,
Daniel Dunbar1c7577c2009-11-04 06:24:38 +00004952 const InputInfoList &Inputs,
4953 const ArgList &Args,
4954 const char *LinkingOutput) const {
Edward O'Callaghan856e4ff2009-08-22 01:06:46 +00004955 ArgStringList CmdArgs;
4956
4957 Args.AddAllArgValues(CmdArgs, options::OPT_Wa_COMMA,
4958 options::OPT_Xassembler);
4959
4960 CmdArgs.push_back("-o");
Daniel Dunbarb440f562010-08-02 02:38:21 +00004961 CmdArgs.push_back(Output.getFilename());
Edward O'Callaghan856e4ff2009-08-22 01:06:46 +00004962
4963 for (InputInfoList::const_iterator
4964 it = Inputs.begin(), ie = Inputs.end(); it != ie; ++it) {
4965 const InputInfo &II = *it;
Daniel Dunbarb440f562010-08-02 02:38:21 +00004966 CmdArgs.push_back(II.getFilename());
Edward O'Callaghan856e4ff2009-08-22 01:06:46 +00004967 }
4968
4969 const char *Exec =
Daniel Dunbar9c3ed5f2010-07-14 18:46:23 +00004970 Args.MakeArgString(getToolChain().GetProgramPath("gas"));
Daniel Dunbar7fbaf532010-08-02 02:38:28 +00004971 C.addCommand(new Command(JA, *this, Exec, CmdArgs));
Edward O'Callaghan856e4ff2009-08-22 01:06:46 +00004972}
4973
4974void auroraux::Link::ConstructJob(Compilation &C, const JobAction &JA,
Daniel Dunbar7fbaf532010-08-02 02:38:28 +00004975 const InputInfo &Output,
Daniel Dunbar1c7577c2009-11-04 06:24:38 +00004976 const InputInfoList &Inputs,
4977 const ArgList &Args,
4978 const char *LinkingOutput) const {
Edward O'Callaghan856e4ff2009-08-22 01:06:46 +00004979 ArgStringList CmdArgs;
4980
4981 if ((!Args.hasArg(options::OPT_nostdlib)) &&
Daniel Dunbar1c7577c2009-11-04 06:24:38 +00004982 (!Args.hasArg(options::OPT_shared))) {
Edward O'Callaghan856e4ff2009-08-22 01:06:46 +00004983 CmdArgs.push_back("-e");
Edward O'Callaghand8712d92009-10-15 07:44:07 +00004984 CmdArgs.push_back("_start");
Edward O'Callaghan856e4ff2009-08-22 01:06:46 +00004985 }
4986
4987 if (Args.hasArg(options::OPT_static)) {
4988 CmdArgs.push_back("-Bstatic");
Edward O'Callaghand8712d92009-10-15 07:44:07 +00004989 CmdArgs.push_back("-dn");
Edward O'Callaghan856e4ff2009-08-22 01:06:46 +00004990 } else {
Edward O'Callaghand8712d92009-10-15 07:44:07 +00004991// CmdArgs.push_back("--eh-frame-hdr");
Edward O'Callaghan856e4ff2009-08-22 01:06:46 +00004992 CmdArgs.push_back("-Bdynamic");
4993 if (Args.hasArg(options::OPT_shared)) {
4994 CmdArgs.push_back("-shared");
4995 } else {
Edward O'Callaghan7d3c2752009-10-16 19:44:18 +00004996 CmdArgs.push_back("--dynamic-linker");
Edward O'Callaghan856e4ff2009-08-22 01:06:46 +00004997 CmdArgs.push_back("/lib/ld.so.1"); // 64Bit Path /lib/amd64/ld.so.1
4998 }
4999 }
5000
Daniel Dunbarb440f562010-08-02 02:38:21 +00005001 if (Output.isFilename()) {
Edward O'Callaghan856e4ff2009-08-22 01:06:46 +00005002 CmdArgs.push_back("-o");
5003 CmdArgs.push_back(Output.getFilename());
5004 } else {
5005 assert(Output.isNothing() && "Invalid output.");
5006 }
5007
5008 if (!Args.hasArg(options::OPT_nostdlib) &&
5009 !Args.hasArg(options::OPT_nostartfiles)) {
5010 if (!Args.hasArg(options::OPT_shared)) {
Chris Lattner3e2ee142010-07-07 16:01:42 +00005011 CmdArgs.push_back(Args.MakeArgString(
Daniel Dunbar9c3ed5f2010-07-14 18:46:23 +00005012 getToolChain().GetFilePath("crt1.o")));
Chris Lattner3e2ee142010-07-07 16:01:42 +00005013 CmdArgs.push_back(Args.MakeArgString(
Daniel Dunbar9c3ed5f2010-07-14 18:46:23 +00005014 getToolChain().GetFilePath("crti.o")));
Chris Lattner3e2ee142010-07-07 16:01:42 +00005015 CmdArgs.push_back(Args.MakeArgString(
Daniel Dunbar9c3ed5f2010-07-14 18:46:23 +00005016 getToolChain().GetFilePath("crtbegin.o")));
Edward O'Callaghan856e4ff2009-08-22 01:06:46 +00005017 } else {
Chris Lattner3e2ee142010-07-07 16:01:42 +00005018 CmdArgs.push_back(Args.MakeArgString(
Daniel Dunbar9c3ed5f2010-07-14 18:46:23 +00005019 getToolChain().GetFilePath("crti.o")));
Edward O'Callaghan856e4ff2009-08-22 01:06:46 +00005020 }
Chris Lattner3e2ee142010-07-07 16:01:42 +00005021 CmdArgs.push_back(Args.MakeArgString(
Daniel Dunbar9c3ed5f2010-07-14 18:46:23 +00005022 getToolChain().GetFilePath("crtn.o")));
Edward O'Callaghan856e4ff2009-08-22 01:06:46 +00005023 }
5024
Daniel Dunbar1c7577c2009-11-04 06:24:38 +00005025 CmdArgs.push_back(Args.MakeArgString("-L/opt/gcc4/lib/gcc/"
5026 + getToolChain().getTripleString()
Daniel Dunbarb0b18612009-10-29 02:24:37 +00005027 + "/4.2.4"));
Edward O'Callaghan856e4ff2009-08-22 01:06:46 +00005028
5029 Args.AddAllArgs(CmdArgs, options::OPT_L);
5030 Args.AddAllArgs(CmdArgs, options::OPT_T_Group);
5031 Args.AddAllArgs(CmdArgs, options::OPT_e);
5032
Daniel Dunbar54423b22010-09-17 00:24:54 +00005033 AddLinkerInputs(getToolChain(), Inputs, Args, CmdArgs);
Edward O'Callaghan856e4ff2009-08-22 01:06:46 +00005034
5035 if (!Args.hasArg(options::OPT_nostdlib) &&
5036 !Args.hasArg(options::OPT_nodefaultlibs)) {
5037 // FIXME: For some reason GCC passes -lgcc before adding
5038 // the default system libraries. Just mimic this for now.
5039 CmdArgs.push_back("-lgcc");
5040
5041 if (Args.hasArg(options::OPT_pthread))
5042 CmdArgs.push_back("-pthread");
5043 if (!Args.hasArg(options::OPT_shared))
5044 CmdArgs.push_back("-lc");
5045 CmdArgs.push_back("-lgcc");
5046 }
5047
5048 if (!Args.hasArg(options::OPT_nostdlib) &&
5049 !Args.hasArg(options::OPT_nostartfiles)) {
5050 if (!Args.hasArg(options::OPT_shared))
Chris Lattner3e2ee142010-07-07 16:01:42 +00005051 CmdArgs.push_back(Args.MakeArgString(
Daniel Dunbar9c3ed5f2010-07-14 18:46:23 +00005052 getToolChain().GetFilePath("crtend.o")));
Edward O'Callaghan856e4ff2009-08-22 01:06:46 +00005053 }
5054
Bill Wendling08760582011-06-27 19:15:03 +00005055 addProfileRT(getToolChain(), Args, CmdArgs, getToolChain().getTriple());
Nick Lewycky82fe5f42011-05-24 21:54:59 +00005056
Edward O'Callaghan856e4ff2009-08-22 01:06:46 +00005057 const char *Exec =
Daniel Dunbar9c3ed5f2010-07-14 18:46:23 +00005058 Args.MakeArgString(getToolChain().GetProgramPath("ld"));
Daniel Dunbar7fbaf532010-08-02 02:38:28 +00005059 C.addCommand(new Command(JA, *this, Exec, CmdArgs));
Edward O'Callaghan856e4ff2009-08-22 01:06:46 +00005060}
5061
Daniel Dunbar10de9e62009-06-29 20:52:51 +00005062void openbsd::Assemble::ConstructJob(Compilation &C, const JobAction &JA,
Daniel Dunbar7fbaf532010-08-02 02:38:28 +00005063 const InputInfo &Output,
Daniel Dunbar10de9e62009-06-29 20:52:51 +00005064 const InputInfoList &Inputs,
5065 const ArgList &Args,
Mike Stump11289f42009-09-09 15:08:12 +00005066 const char *LinkingOutput) const {
Daniel Dunbar10de9e62009-06-29 20:52:51 +00005067 ArgStringList CmdArgs;
5068
5069 Args.AddAllArgValues(CmdArgs, options::OPT_Wa_COMMA,
5070 options::OPT_Xassembler);
5071
5072 CmdArgs.push_back("-o");
Daniel Dunbarb440f562010-08-02 02:38:21 +00005073 CmdArgs.push_back(Output.getFilename());
Daniel Dunbar10de9e62009-06-29 20:52:51 +00005074
5075 for (InputInfoList::const_iterator
5076 it = Inputs.begin(), ie = Inputs.end(); it != ie; ++it) {
5077 const InputInfo &II = *it;
Daniel Dunbarb440f562010-08-02 02:38:21 +00005078 CmdArgs.push_back(II.getFilename());
Daniel Dunbar10de9e62009-06-29 20:52:51 +00005079 }
5080
5081 const char *Exec =
Daniel Dunbar9c3ed5f2010-07-14 18:46:23 +00005082 Args.MakeArgString(getToolChain().GetProgramPath("as"));
Daniel Dunbar7fbaf532010-08-02 02:38:28 +00005083 C.addCommand(new Command(JA, *this, Exec, CmdArgs));
Daniel Dunbar10de9e62009-06-29 20:52:51 +00005084}
5085
5086void openbsd::Link::ConstructJob(Compilation &C, const JobAction &JA,
Daniel Dunbar7fbaf532010-08-02 02:38:28 +00005087 const InputInfo &Output,
Daniel Dunbar10de9e62009-06-29 20:52:51 +00005088 const InputInfoList &Inputs,
5089 const ArgList &Args,
5090 const char *LinkingOutput) const {
Daniel Dunbar083edf72009-12-21 18:54:17 +00005091 const Driver &D = getToolChain().getDriver();
Daniel Dunbar10de9e62009-06-29 20:52:51 +00005092 ArgStringList CmdArgs;
5093
Rafael Espindolaaadd30e2012-12-31 22:41:36 +00005094 // Silence warning for "clang -g foo.o -o foo"
5095 Args.ClaimAllArgs(options::OPT_g_Group);
5096 // and "clang -emit-llvm foo.o -o foo"
5097 Args.ClaimAllArgs(options::OPT_emit_llvm);
5098 // and for "clang -w foo.o -o foo". Other warning options are already
5099 // handled somewhere else.
5100 Args.ClaimAllArgs(options::OPT_w);
5101
Daniel Dunbara8888ac2009-08-03 01:28:59 +00005102 if ((!Args.hasArg(options::OPT_nostdlib)) &&
Daniel Dunbar1c7577c2009-11-04 06:24:38 +00005103 (!Args.hasArg(options::OPT_shared))) {
Daniel Dunbara8888ac2009-08-03 01:28:59 +00005104 CmdArgs.push_back("-e");
5105 CmdArgs.push_back("__start");
5106 }
5107
Daniel Dunbar10de9e62009-06-29 20:52:51 +00005108 if (Args.hasArg(options::OPT_static)) {
5109 CmdArgs.push_back("-Bstatic");
5110 } else {
Rafael Espindola7ba97af2010-11-11 02:17:51 +00005111 if (Args.hasArg(options::OPT_rdynamic))
5112 CmdArgs.push_back("-export-dynamic");
Daniel Dunbar10de9e62009-06-29 20:52:51 +00005113 CmdArgs.push_back("--eh-frame-hdr");
Daniel Dunbara8888ac2009-08-03 01:28:59 +00005114 CmdArgs.push_back("-Bdynamic");
Daniel Dunbar10de9e62009-06-29 20:52:51 +00005115 if (Args.hasArg(options::OPT_shared)) {
Daniel Dunbara8888ac2009-08-03 01:28:59 +00005116 CmdArgs.push_back("-shared");
Daniel Dunbar10de9e62009-06-29 20:52:51 +00005117 } else {
5118 CmdArgs.push_back("-dynamic-linker");
5119 CmdArgs.push_back("/usr/libexec/ld.so");
5120 }
5121 }
5122
Daniel Dunbarb440f562010-08-02 02:38:21 +00005123 if (Output.isFilename()) {
Daniel Dunbar10de9e62009-06-29 20:52:51 +00005124 CmdArgs.push_back("-o");
5125 CmdArgs.push_back(Output.getFilename());
5126 } else {
5127 assert(Output.isNothing() && "Invalid output.");
5128 }
5129
5130 if (!Args.hasArg(options::OPT_nostdlib) &&
5131 !Args.hasArg(options::OPT_nostartfiles)) {
5132 if (!Args.hasArg(options::OPT_shared)) {
Eli Friedman3715d1f2011-12-15 02:15:56 +00005133 if (Args.hasArg(options::OPT_pg))
5134 CmdArgs.push_back(Args.MakeArgString(
5135 getToolChain().GetFilePath("gcrt0.o")));
5136 else
5137 CmdArgs.push_back(Args.MakeArgString(
5138 getToolChain().GetFilePath("crt0.o")));
Chris Lattner3e2ee142010-07-07 16:01:42 +00005139 CmdArgs.push_back(Args.MakeArgString(
Daniel Dunbar9c3ed5f2010-07-14 18:46:23 +00005140 getToolChain().GetFilePath("crtbegin.o")));
Daniel Dunbar10de9e62009-06-29 20:52:51 +00005141 } else {
Chris Lattner3e2ee142010-07-07 16:01:42 +00005142 CmdArgs.push_back(Args.MakeArgString(
Daniel Dunbar9c3ed5f2010-07-14 18:46:23 +00005143 getToolChain().GetFilePath("crtbeginS.o")));
Daniel Dunbar10de9e62009-06-29 20:52:51 +00005144 }
5145 }
5146
Edward O'Callaghan5c521462009-10-28 15:13:08 +00005147 std::string Triple = getToolChain().getTripleString();
5148 if (Triple.substr(0, 6) == "x86_64")
Daniel Dunbar1c7577c2009-11-04 06:24:38 +00005149 Triple.replace(0, 6, "amd64");
Daniel Dunbarb0b18612009-10-29 02:24:37 +00005150 CmdArgs.push_back(Args.MakeArgString("-L/usr/lib/gcc-lib/" + Triple +
Daniel Dunbarea3813f2010-08-01 23:13:54 +00005151 "/4.2.1"));
Daniel Dunbara8888ac2009-08-03 01:28:59 +00005152
Daniel Dunbar10de9e62009-06-29 20:52:51 +00005153 Args.AddAllArgs(CmdArgs, options::OPT_L);
5154 Args.AddAllArgs(CmdArgs, options::OPT_T_Group);
5155 Args.AddAllArgs(CmdArgs, options::OPT_e);
Rafael Espindolaaadd30e2012-12-31 22:41:36 +00005156 Args.AddAllArgs(CmdArgs, options::OPT_s);
5157 Args.AddAllArgs(CmdArgs, options::OPT_t);
5158 Args.AddAllArgs(CmdArgs, options::OPT_Z_Flag);
5159 Args.AddAllArgs(CmdArgs, options::OPT_r);
Daniel Dunbar10de9e62009-06-29 20:52:51 +00005160
Daniel Dunbar54423b22010-09-17 00:24:54 +00005161 AddLinkerInputs(getToolChain(), Inputs, Args, CmdArgs);
Daniel Dunbar10de9e62009-06-29 20:52:51 +00005162
5163 if (!Args.hasArg(options::OPT_nostdlib) &&
5164 !Args.hasArg(options::OPT_nodefaultlibs)) {
Daniel Dunbarea3813f2010-08-01 23:13:54 +00005165 if (D.CCCIsCXX) {
Daniel Dunbar3f7796f2010-09-17 01:20:05 +00005166 getToolChain().AddCXXStdlibLibArgs(Args, CmdArgs);
Eli Friedman3715d1f2011-12-15 02:15:56 +00005167 if (Args.hasArg(options::OPT_pg))
5168 CmdArgs.push_back("-lm_p");
5169 else
5170 CmdArgs.push_back("-lm");
Daniel Dunbarea3813f2010-08-01 23:13:54 +00005171 }
5172
Daniel Dunbara8888ac2009-08-03 01:28:59 +00005173 // FIXME: For some reason GCC passes -lgcc before adding
5174 // the default system libraries. Just mimic this for now.
5175 CmdArgs.push_back("-lgcc");
Daniel Dunbar10de9e62009-06-29 20:52:51 +00005176
Eric Christopher17674ec2012-09-13 06:32:34 +00005177 if (Args.hasArg(options::OPT_pthread)) {
5178 if (!Args.hasArg(options::OPT_shared) &&
5179 Args.hasArg(options::OPT_pg))
5180 CmdArgs.push_back("-lpthread_p");
5181 else
5182 CmdArgs.push_back("-lpthread");
5183 }
5184
Chandler Carruth45661652011-12-17 22:32:42 +00005185 if (!Args.hasArg(options::OPT_shared)) {
Eric Christopher17674ec2012-09-13 06:32:34 +00005186 if (Args.hasArg(options::OPT_pg))
Eli Friedman3715d1f2011-12-15 02:15:56 +00005187 CmdArgs.push_back("-lc_p");
5188 else
5189 CmdArgs.push_back("-lc");
Chandler Carruth45661652011-12-17 22:32:42 +00005190 }
Eric Christopher17674ec2012-09-13 06:32:34 +00005191
Daniel Dunbara8888ac2009-08-03 01:28:59 +00005192 CmdArgs.push_back("-lgcc");
Daniel Dunbar10de9e62009-06-29 20:52:51 +00005193 }
5194
5195 if (!Args.hasArg(options::OPT_nostdlib) &&
5196 !Args.hasArg(options::OPT_nostartfiles)) {
5197 if (!Args.hasArg(options::OPT_shared))
Chris Lattner3e2ee142010-07-07 16:01:42 +00005198 CmdArgs.push_back(Args.MakeArgString(
Daniel Dunbar9c3ed5f2010-07-14 18:46:23 +00005199 getToolChain().GetFilePath("crtend.o")));
Daniel Dunbar10de9e62009-06-29 20:52:51 +00005200 else
Chris Lattner3e2ee142010-07-07 16:01:42 +00005201 CmdArgs.push_back(Args.MakeArgString(
Daniel Dunbar9c3ed5f2010-07-14 18:46:23 +00005202 getToolChain().GetFilePath("crtendS.o")));
Daniel Dunbar10de9e62009-06-29 20:52:51 +00005203 }
5204
5205 const char *Exec =
Daniel Dunbar9c3ed5f2010-07-14 18:46:23 +00005206 Args.MakeArgString(getToolChain().GetProgramPath("ld"));
Daniel Dunbar7fbaf532010-08-02 02:38:28 +00005207 C.addCommand(new Command(JA, *this, Exec, CmdArgs));
Daniel Dunbar10de9e62009-06-29 20:52:51 +00005208}
Ed Schoutene33194b2009-04-02 19:13:12 +00005209
Eli Friedman9fa28852012-08-08 23:57:20 +00005210void bitrig::Assemble::ConstructJob(Compilation &C, const JobAction &JA,
5211 const InputInfo &Output,
5212 const InputInfoList &Inputs,
5213 const ArgList &Args,
5214 const char *LinkingOutput) const {
5215 ArgStringList CmdArgs;
5216
5217 Args.AddAllArgValues(CmdArgs, options::OPT_Wa_COMMA,
5218 options::OPT_Xassembler);
5219
5220 CmdArgs.push_back("-o");
5221 CmdArgs.push_back(Output.getFilename());
5222
5223 for (InputInfoList::const_iterator
5224 it = Inputs.begin(), ie = Inputs.end(); it != ie; ++it) {
5225 const InputInfo &II = *it;
5226 CmdArgs.push_back(II.getFilename());
5227 }
5228
5229 const char *Exec =
5230 Args.MakeArgString(getToolChain().GetProgramPath("as"));
5231 C.addCommand(new Command(JA, *this, Exec, CmdArgs));
5232}
5233
5234void bitrig::Link::ConstructJob(Compilation &C, const JobAction &JA,
5235 const InputInfo &Output,
5236 const InputInfoList &Inputs,
5237 const ArgList &Args,
5238 const char *LinkingOutput) const {
5239 const Driver &D = getToolChain().getDriver();
5240 ArgStringList CmdArgs;
5241
5242 if ((!Args.hasArg(options::OPT_nostdlib)) &&
5243 (!Args.hasArg(options::OPT_shared))) {
5244 CmdArgs.push_back("-e");
5245 CmdArgs.push_back("__start");
5246 }
5247
5248 if (Args.hasArg(options::OPT_static)) {
5249 CmdArgs.push_back("-Bstatic");
5250 } else {
5251 if (Args.hasArg(options::OPT_rdynamic))
5252 CmdArgs.push_back("-export-dynamic");
5253 CmdArgs.push_back("--eh-frame-hdr");
5254 CmdArgs.push_back("-Bdynamic");
5255 if (Args.hasArg(options::OPT_shared)) {
5256 CmdArgs.push_back("-shared");
5257 } else {
5258 CmdArgs.push_back("-dynamic-linker");
5259 CmdArgs.push_back("/usr/libexec/ld.so");
5260 }
5261 }
5262
5263 if (Output.isFilename()) {
5264 CmdArgs.push_back("-o");
5265 CmdArgs.push_back(Output.getFilename());
5266 } else {
5267 assert(Output.isNothing() && "Invalid output.");
5268 }
5269
5270 if (!Args.hasArg(options::OPT_nostdlib) &&
5271 !Args.hasArg(options::OPT_nostartfiles)) {
5272 if (!Args.hasArg(options::OPT_shared)) {
5273 if (Args.hasArg(options::OPT_pg))
5274 CmdArgs.push_back(Args.MakeArgString(
5275 getToolChain().GetFilePath("gcrt0.o")));
5276 else
5277 CmdArgs.push_back(Args.MakeArgString(
5278 getToolChain().GetFilePath("crt0.o")));
5279 CmdArgs.push_back(Args.MakeArgString(
5280 getToolChain().GetFilePath("crtbegin.o")));
5281 } else {
5282 CmdArgs.push_back(Args.MakeArgString(
5283 getToolChain().GetFilePath("crtbeginS.o")));
5284 }
5285 }
5286
5287 Args.AddAllArgs(CmdArgs, options::OPT_L);
5288 Args.AddAllArgs(CmdArgs, options::OPT_T_Group);
5289 Args.AddAllArgs(CmdArgs, options::OPT_e);
5290
5291 AddLinkerInputs(getToolChain(), Inputs, Args, CmdArgs);
5292
5293 if (!Args.hasArg(options::OPT_nostdlib) &&
5294 !Args.hasArg(options::OPT_nodefaultlibs)) {
5295 if (D.CCCIsCXX) {
5296 getToolChain().AddCXXStdlibLibArgs(Args, CmdArgs);
5297 if (Args.hasArg(options::OPT_pg))
5298 CmdArgs.push_back("-lm_p");
5299 else
5300 CmdArgs.push_back("-lm");
5301 }
5302
Rafael Espindola1ad26f02012-10-23 17:07:31 +00005303 if (Args.hasArg(options::OPT_pthread)) {
5304 if (!Args.hasArg(options::OPT_shared) &&
5305 Args.hasArg(options::OPT_pg))
5306 CmdArgs.push_back("-lpthread_p");
5307 else
5308 CmdArgs.push_back("-lpthread");
5309 }
5310
Eli Friedman9fa28852012-08-08 23:57:20 +00005311 if (!Args.hasArg(options::OPT_shared)) {
5312 if (Args.hasArg(options::OPT_pg))
5313 CmdArgs.push_back("-lc_p");
5314 else
5315 CmdArgs.push_back("-lc");
5316 }
5317
5318 std::string myarch = "-lclang_rt.";
5319 const llvm::Triple &T = getToolChain().getTriple();
5320 llvm::Triple::ArchType Arch = T.getArch();
5321 switch (Arch) {
5322 case llvm::Triple::arm:
5323 myarch += ("arm");
5324 break;
5325 case llvm::Triple::x86:
5326 myarch += ("i386");
5327 break;
5328 case llvm::Triple::x86_64:
5329 myarch += ("amd64");
5330 break;
5331 default:
5332 assert(0 && "Unsupported architecture");
5333 }
5334 CmdArgs.push_back(Args.MakeArgString(myarch));
5335 }
5336
5337 if (!Args.hasArg(options::OPT_nostdlib) &&
5338 !Args.hasArg(options::OPT_nostartfiles)) {
5339 if (!Args.hasArg(options::OPT_shared))
5340 CmdArgs.push_back(Args.MakeArgString(
5341 getToolChain().GetFilePath("crtend.o")));
5342 else
5343 CmdArgs.push_back(Args.MakeArgString(
5344 getToolChain().GetFilePath("crtendS.o")));
5345 }
Eli Friedman5bb2ba02012-08-09 22:42:04 +00005346
5347 const char *Exec =
5348 Args.MakeArgString(getToolChain().GetProgramPath("ld"));
5349 C.addCommand(new Command(JA, *this, Exec, CmdArgs));
Eli Friedman9fa28852012-08-08 23:57:20 +00005350}
5351
Daniel Dunbar8eb473c2009-03-31 17:45:15 +00005352void freebsd::Assemble::ConstructJob(Compilation &C, const JobAction &JA,
Daniel Dunbar7fbaf532010-08-02 02:38:28 +00005353 const InputInfo &Output,
Daniel Dunbard067f7f2009-04-08 23:54:23 +00005354 const InputInfoList &Inputs,
5355 const ArgList &Args,
Mike Stump11289f42009-09-09 15:08:12 +00005356 const char *LinkingOutput) const {
Daniel Dunbar8eb473c2009-03-31 17:45:15 +00005357 ArgStringList CmdArgs;
5358
Daniel Dunbard854c8d2009-04-01 19:36:32 +00005359 // When building 32-bit code on FreeBSD/amd64, we have to explicitly
5360 // instruct as in the base system to assemble 32-bit code.
Eric Christopherc0f1a5e2012-09-05 21:32:44 +00005361 if (getToolChain().getArch() == llvm::Triple::x86)
Daniel Dunbar8eb473c2009-03-31 17:45:15 +00005362 CmdArgs.push_back("--32");
Eric Christopherc0f1a5e2012-09-05 21:32:44 +00005363 else if (getToolChain().getArch() == llvm::Triple::ppc)
Roman Divacky00859c22011-06-04 07:37:31 +00005364 CmdArgs.push_back("-a32");
Eric Christopherc0f1a5e2012-09-05 21:32:44 +00005365 else if (getToolChain().getArch() == llvm::Triple::mips ||
5366 getToolChain().getArch() == llvm::Triple::mipsel ||
5367 getToolChain().getArch() == llvm::Triple::mips64 ||
5368 getToolChain().getArch() == llvm::Triple::mips64el) {
5369 StringRef CPUName;
5370 StringRef ABIName;
5371 getMipsCPUAndABI(Args, getToolChain(), CPUName, ABIName);
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00005372
Eric Christopherc0f1a5e2012-09-05 21:32:44 +00005373 CmdArgs.push_back("-march");
5374 CmdArgs.push_back(CPUName.data());
5375
Eric Christopherc0f1a5e2012-09-05 21:32:44 +00005376 CmdArgs.push_back("-mabi");
Simon Atanasyan0da400c2013-02-27 14:55:49 +00005377 CmdArgs.push_back(getGnuCompatibleMipsABIName(ABIName).data());
Eric Christopherc0f1a5e2012-09-05 21:32:44 +00005378
5379 if (getToolChain().getArch() == llvm::Triple::mips ||
5380 getToolChain().getArch() == llvm::Triple::mips64)
5381 CmdArgs.push_back("-EB");
5382 else
5383 CmdArgs.push_back("-EL");
5384
5385 Arg *LastPICArg = Args.getLastArg(options::OPT_fPIC, options::OPT_fno_PIC,
5386 options::OPT_fpic, options::OPT_fno_pic,
5387 options::OPT_fPIE, options::OPT_fno_PIE,
5388 options::OPT_fpie, options::OPT_fno_pie);
5389 if (LastPICArg &&
5390 (LastPICArg->getOption().matches(options::OPT_fPIC) ||
5391 LastPICArg->getOption().matches(options::OPT_fpic) ||
5392 LastPICArg->getOption().matches(options::OPT_fPIE) ||
5393 LastPICArg->getOption().matches(options::OPT_fpie))) {
5394 CmdArgs.push_back("-KPIC");
5395 }
Rafael Espindola0f207ed2012-12-13 04:17:14 +00005396 } else if (getToolChain().getArch() == llvm::Triple::arm ||
5397 getToolChain().getArch() == llvm::Triple::thumb) {
5398 CmdArgs.push_back("-mfpu=softvfp");
5399 switch(getToolChain().getTriple().getEnvironment()) {
5400 case llvm::Triple::GNUEABI:
5401 case llvm::Triple::EABI:
Anton Korobeynikov2bed8472013-03-18 07:59:20 +00005402 CmdArgs.push_back("-meabi=5");
Rafael Espindola0f207ed2012-12-13 04:17:14 +00005403 break;
5404
5405 default:
5406 CmdArgs.push_back("-matpcs");
5407 }
Eric Christopherc0f1a5e2012-09-05 21:32:44 +00005408 }
Eric Christopher0b26a612010-03-02 02:41:08 +00005409
Daniel Dunbar8eb473c2009-03-31 17:45:15 +00005410 Args.AddAllArgValues(CmdArgs, options::OPT_Wa_COMMA,
5411 options::OPT_Xassembler);
5412
5413 CmdArgs.push_back("-o");
Daniel Dunbarb440f562010-08-02 02:38:21 +00005414 CmdArgs.push_back(Output.getFilename());
Daniel Dunbar8eb473c2009-03-31 17:45:15 +00005415
5416 for (InputInfoList::const_iterator
5417 it = Inputs.begin(), ie = Inputs.end(); it != ie; ++it) {
5418 const InputInfo &II = *it;
Daniel Dunbarb440f562010-08-02 02:38:21 +00005419 CmdArgs.push_back(II.getFilename());
Daniel Dunbar8eb473c2009-03-31 17:45:15 +00005420 }
5421
Daniel Dunbard067f7f2009-04-08 23:54:23 +00005422 const char *Exec =
Daniel Dunbar9c3ed5f2010-07-14 18:46:23 +00005423 Args.MakeArgString(getToolChain().GetProgramPath("as"));
Daniel Dunbar7fbaf532010-08-02 02:38:28 +00005424 C.addCommand(new Command(JA, *this, Exec, CmdArgs));
Daniel Dunbar8eb473c2009-03-31 17:45:15 +00005425}
Daniel Dunbard854c8d2009-04-01 19:36:32 +00005426
5427void freebsd::Link::ConstructJob(Compilation &C, const JobAction &JA,
Daniel Dunbar7fbaf532010-08-02 02:38:28 +00005428 const InputInfo &Output,
Daniel Dunbard067f7f2009-04-08 23:54:23 +00005429 const InputInfoList &Inputs,
5430 const ArgList &Args,
Daniel Dunbare3e263f2009-05-02 20:14:53 +00005431 const char *LinkingOutput) const {
Roman Divackyafe2f232012-08-28 15:09:03 +00005432 const toolchains::FreeBSD& ToolChain =
5433 static_cast<const toolchains::FreeBSD&>(getToolChain());
5434 const Driver &D = ToolChain.getDriver();
Daniel Dunbard854c8d2009-04-01 19:36:32 +00005435 ArgStringList CmdArgs;
David Chisnall5f99f482012-07-29 15:24:44 +00005436
5437 // Silence warning for "clang -g foo.o -o foo"
5438 Args.ClaimAllArgs(options::OPT_g_Group);
5439 // and "clang -emit-llvm foo.o -o foo"
5440 Args.ClaimAllArgs(options::OPT_emit_llvm);
5441 // and for "clang -w foo.o -o foo". Other warning options are already
5442 // handled somewhere else.
5443 Args.ClaimAllArgs(options::OPT_w);
Daniel Dunbard854c8d2009-04-01 19:36:32 +00005444
Joerg Sonnenberger6165ab12011-03-21 13:51:29 +00005445 if (!D.SysRoot.empty())
5446 CmdArgs.push_back(Args.MakeArgString("--sysroot=" + D.SysRoot));
5447
Roman Divackyafe2f232012-08-28 15:09:03 +00005448 if (Args.hasArg(options::OPT_pie))
5449 CmdArgs.push_back("-pie");
5450
Daniel Dunbard854c8d2009-04-01 19:36:32 +00005451 if (Args.hasArg(options::OPT_static)) {
5452 CmdArgs.push_back("-Bstatic");
5453 } else {
Rafael Espindola7ba97af2010-11-11 02:17:51 +00005454 if (Args.hasArg(options::OPT_rdynamic))
5455 CmdArgs.push_back("-export-dynamic");
Daniel Dunbard854c8d2009-04-01 19:36:32 +00005456 CmdArgs.push_back("--eh-frame-hdr");
5457 if (Args.hasArg(options::OPT_shared)) {
5458 CmdArgs.push_back("-Bshareable");
5459 } else {
5460 CmdArgs.push_back("-dynamic-linker");
5461 CmdArgs.push_back("/libexec/ld-elf.so.1");
5462 }
Roman Divackyafe2f232012-08-28 15:09:03 +00005463 if (ToolChain.getTriple().getOSMajorVersion() >= 9) {
5464 llvm::Triple::ArchType Arch = ToolChain.getArch();
David Chisnall5f99f482012-07-29 15:24:44 +00005465 if (Arch == llvm::Triple::arm || Arch == llvm::Triple::sparc ||
5466 Arch == llvm::Triple::x86 || Arch == llvm::Triple::x86_64) {
5467 CmdArgs.push_back("--hash-style=both");
5468 }
5469 }
5470 CmdArgs.push_back("--enable-new-dtags");
Daniel Dunbard854c8d2009-04-01 19:36:32 +00005471 }
5472
5473 // When building 32-bit code on FreeBSD/amd64, we have to explicitly
5474 // instruct ld in the base system to link 32-bit code.
Rafael Espindola35ca7d92012-10-07 04:44:33 +00005475 if (ToolChain.getArch() == llvm::Triple::x86) {
Daniel Dunbard854c8d2009-04-01 19:36:32 +00005476 CmdArgs.push_back("-m");
5477 CmdArgs.push_back("elf_i386_fbsd");
5478 }
5479
Rafael Espindola35ca7d92012-10-07 04:44:33 +00005480 if (ToolChain.getArch() == llvm::Triple::ppc) {
Roman Divacky5e300b82011-06-04 07:40:24 +00005481 CmdArgs.push_back("-m");
Roman Divackyd150ad32011-11-21 16:50:32 +00005482 CmdArgs.push_back("elf32ppc_fbsd");
Roman Divacky5e300b82011-06-04 07:40:24 +00005483 }
5484
Daniel Dunbarb440f562010-08-02 02:38:21 +00005485 if (Output.isFilename()) {
Daniel Dunbard854c8d2009-04-01 19:36:32 +00005486 CmdArgs.push_back("-o");
5487 CmdArgs.push_back(Output.getFilename());
5488 } else {
5489 assert(Output.isNothing() && "Invalid output.");
5490 }
5491
5492 if (!Args.hasArg(options::OPT_nostdlib) &&
5493 !Args.hasArg(options::OPT_nostartfiles)) {
Roman Divackyafe2f232012-08-28 15:09:03 +00005494 const char *crt1 = NULL;
Daniel Dunbard854c8d2009-04-01 19:36:32 +00005495 if (!Args.hasArg(options::OPT_shared)) {
Roman Divacky66f22762011-02-10 16:59:40 +00005496 if (Args.hasArg(options::OPT_pg))
Roman Divackyafe2f232012-08-28 15:09:03 +00005497 crt1 = "gcrt1.o";
5498 else if (Args.hasArg(options::OPT_pie))
5499 crt1 = "Scrt1.o";
5500 else
5501 crt1 = "crt1.o";
Daniel Dunbard854c8d2009-04-01 19:36:32 +00005502 }
Roman Divackyafe2f232012-08-28 15:09:03 +00005503 if (crt1)
5504 CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath(crt1)));
5505
5506 CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath("crti.o")));
5507
5508 const char *crtbegin = NULL;
5509 if (Args.hasArg(options::OPT_static))
5510 crtbegin = "crtbeginT.o";
5511 else if (Args.hasArg(options::OPT_shared) || Args.hasArg(options::OPT_pie))
5512 crtbegin = "crtbeginS.o";
5513 else
5514 crtbegin = "crtbegin.o";
5515
5516 CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath(crtbegin)));
Daniel Dunbard854c8d2009-04-01 19:36:32 +00005517 }
5518
5519 Args.AddAllArgs(CmdArgs, options::OPT_L);
Roman Divackyafe2f232012-08-28 15:09:03 +00005520 const ToolChain::path_list Paths = ToolChain.getFilePaths();
Roman Divackyee8188a2011-03-01 17:53:14 +00005521 for (ToolChain::path_list::const_iterator i = Paths.begin(), e = Paths.end();
5522 i != e; ++i)
Chris Lattner0e62c1c2011-07-23 10:55:15 +00005523 CmdArgs.push_back(Args.MakeArgString(StringRef("-L") + *i));
Daniel Dunbard854c8d2009-04-01 19:36:32 +00005524 Args.AddAllArgs(CmdArgs, options::OPT_T_Group);
5525 Args.AddAllArgs(CmdArgs, options::OPT_e);
David Chisnall589a4942010-08-15 22:58:12 +00005526 Args.AddAllArgs(CmdArgs, options::OPT_s);
5527 Args.AddAllArgs(CmdArgs, options::OPT_t);
5528 Args.AddAllArgs(CmdArgs, options::OPT_Z_Flag);
5529 Args.AddAllArgs(CmdArgs, options::OPT_r);
Daniel Dunbard854c8d2009-04-01 19:36:32 +00005530
Roman Divackyafe2f232012-08-28 15:09:03 +00005531 AddLinkerInputs(ToolChain, Inputs, Args, CmdArgs);
Daniel Dunbard854c8d2009-04-01 19:36:32 +00005532
5533 if (!Args.hasArg(options::OPT_nostdlib) &&
5534 !Args.hasArg(options::OPT_nodefaultlibs)) {
Daniel Dunbar4b8ef282010-02-17 08:07:51 +00005535 if (D.CCCIsCXX) {
Roman Divackyafe2f232012-08-28 15:09:03 +00005536 ToolChain.AddCXXStdlibLibArgs(Args, CmdArgs);
Roman Divacky66f22762011-02-10 16:59:40 +00005537 if (Args.hasArg(options::OPT_pg))
5538 CmdArgs.push_back("-lm_p");
5539 else
5540 CmdArgs.push_back("-lm");
Daniel Dunbar4b8ef282010-02-17 08:07:51 +00005541 }
Daniel Dunbard854c8d2009-04-01 19:36:32 +00005542 // FIXME: For some reason GCC passes -lgcc and -lgcc_s before adding
5543 // the default system libraries. Just mimic this for now.
Roman Divacky66f22762011-02-10 16:59:40 +00005544 if (Args.hasArg(options::OPT_pg))
5545 CmdArgs.push_back("-lgcc_p");
5546 else
5547 CmdArgs.push_back("-lgcc");
Daniel Dunbard854c8d2009-04-01 19:36:32 +00005548 if (Args.hasArg(options::OPT_static)) {
5549 CmdArgs.push_back("-lgcc_eh");
Roman Divacky66f22762011-02-10 16:59:40 +00005550 } else if (Args.hasArg(options::OPT_pg)) {
5551 CmdArgs.push_back("-lgcc_eh_p");
Daniel Dunbard854c8d2009-04-01 19:36:32 +00005552 } else {
5553 CmdArgs.push_back("--as-needed");
5554 CmdArgs.push_back("-lgcc_s");
5555 CmdArgs.push_back("--no-as-needed");
5556 }
5557
Matt Beaumont-Gay1fe49152011-02-10 20:35:01 +00005558 if (Args.hasArg(options::OPT_pthread)) {
Roman Divacky66f22762011-02-10 16:59:40 +00005559 if (Args.hasArg(options::OPT_pg))
5560 CmdArgs.push_back("-lpthread_p");
5561 else
5562 CmdArgs.push_back("-lpthread");
Matt Beaumont-Gay1fe49152011-02-10 20:35:01 +00005563 }
Daniel Dunbard854c8d2009-04-01 19:36:32 +00005564
Roman Divacky66f22762011-02-10 16:59:40 +00005565 if (Args.hasArg(options::OPT_pg)) {
5566 if (Args.hasArg(options::OPT_shared))
5567 CmdArgs.push_back("-lc");
5568 else
5569 CmdArgs.push_back("-lc_p");
5570 CmdArgs.push_back("-lgcc_p");
5571 } else {
5572 CmdArgs.push_back("-lc");
5573 CmdArgs.push_back("-lgcc");
5574 }
5575
Daniel Dunbard854c8d2009-04-01 19:36:32 +00005576 if (Args.hasArg(options::OPT_static)) {
5577 CmdArgs.push_back("-lgcc_eh");
Roman Divacky66f22762011-02-10 16:59:40 +00005578 } else if (Args.hasArg(options::OPT_pg)) {
5579 CmdArgs.push_back("-lgcc_eh_p");
Daniel Dunbard854c8d2009-04-01 19:36:32 +00005580 } else {
5581 CmdArgs.push_back("--as-needed");
5582 CmdArgs.push_back("-lgcc_s");
5583 CmdArgs.push_back("--no-as-needed");
5584 }
5585 }
5586
5587 if (!Args.hasArg(options::OPT_nostdlib) &&
5588 !Args.hasArg(options::OPT_nostartfiles)) {
Roman Divackya3c50242012-09-07 13:36:21 +00005589 if (Args.hasArg(options::OPT_shared) || Args.hasArg(options::OPT_pie))
Roman Divackyafe2f232012-08-28 15:09:03 +00005590 CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath("crtendS.o")));
Roman Divackya3c50242012-09-07 13:36:21 +00005591 else
5592 CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath("crtend.o")));
Roman Divackyafe2f232012-08-28 15:09:03 +00005593 CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath("crtn.o")));
Daniel Dunbard854c8d2009-04-01 19:36:32 +00005594 }
5595
Roman Divackyafe2f232012-08-28 15:09:03 +00005596 addProfileRT(ToolChain, Args, CmdArgs, ToolChain.getTriple());
Nick Lewycky82fe5f42011-05-24 21:54:59 +00005597
Daniel Dunbard067f7f2009-04-08 23:54:23 +00005598 const char *Exec =
Roman Divackyafe2f232012-08-28 15:09:03 +00005599 Args.MakeArgString(ToolChain.GetProgramPath("ld"));
Daniel Dunbar7fbaf532010-08-02 02:38:28 +00005600 C.addCommand(new Command(JA, *this, Exec, CmdArgs));
Daniel Dunbard854c8d2009-04-01 19:36:32 +00005601}
Daniel Dunbarcc912342009-05-02 18:28:39 +00005602
Benjamin Kramer24f1d3e2011-02-02 18:59:27 +00005603void netbsd::Assemble::ConstructJob(Compilation &C, const JobAction &JA,
5604 const InputInfo &Output,
5605 const InputInfoList &Inputs,
5606 const ArgList &Args,
5607 const char *LinkingOutput) const {
5608 ArgStringList CmdArgs;
5609
5610 // When building 32-bit code on NetBSD/amd64, we have to explicitly
5611 // instruct as in the base system to assemble 32-bit code.
Joerg Sonnenbergerd64c60e2012-01-26 22:27:52 +00005612 if (getToolChain().getArch() == llvm::Triple::x86)
Benjamin Kramer24f1d3e2011-02-02 18:59:27 +00005613 CmdArgs.push_back("--32");
5614
Benjamin Kramer24f1d3e2011-02-02 18:59:27 +00005615 // Set byte order explicitly
Rafael Espindola35ca7d92012-10-07 04:44:33 +00005616 if (getToolChain().getArch() == llvm::Triple::mips)
Benjamin Kramer24f1d3e2011-02-02 18:59:27 +00005617 CmdArgs.push_back("-EB");
Rafael Espindola35ca7d92012-10-07 04:44:33 +00005618 else if (getToolChain().getArch() == llvm::Triple::mipsel)
Benjamin Kramer24f1d3e2011-02-02 18:59:27 +00005619 CmdArgs.push_back("-EL");
5620
5621 Args.AddAllArgValues(CmdArgs, options::OPT_Wa_COMMA,
5622 options::OPT_Xassembler);
5623
5624 CmdArgs.push_back("-o");
5625 CmdArgs.push_back(Output.getFilename());
5626
5627 for (InputInfoList::const_iterator
5628 it = Inputs.begin(), ie = Inputs.end(); it != ie; ++it) {
5629 const InputInfo &II = *it;
5630 CmdArgs.push_back(II.getFilename());
5631 }
5632
David Chisnallddbd68f2011-09-27 22:03:18 +00005633 const char *Exec = Args.MakeArgString((getToolChain().GetProgramPath("as")));
Benjamin Kramer24f1d3e2011-02-02 18:59:27 +00005634 C.addCommand(new Command(JA, *this, Exec, CmdArgs));
5635}
5636
5637void netbsd::Link::ConstructJob(Compilation &C, const JobAction &JA,
5638 const InputInfo &Output,
5639 const InputInfoList &Inputs,
5640 const ArgList &Args,
5641 const char *LinkingOutput) const {
5642 const Driver &D = getToolChain().getDriver();
5643 ArgStringList CmdArgs;
5644
Joerg Sonnenberger6165ab12011-03-21 13:51:29 +00005645 if (!D.SysRoot.empty())
5646 CmdArgs.push_back(Args.MakeArgString("--sysroot=" + D.SysRoot));
5647
Benjamin Kramer24f1d3e2011-02-02 18:59:27 +00005648 if (Args.hasArg(options::OPT_static)) {
5649 CmdArgs.push_back("-Bstatic");
5650 } else {
5651 if (Args.hasArg(options::OPT_rdynamic))
5652 CmdArgs.push_back("-export-dynamic");
5653 CmdArgs.push_back("--eh-frame-hdr");
5654 if (Args.hasArg(options::OPT_shared)) {
5655 CmdArgs.push_back("-Bshareable");
5656 } else {
5657 CmdArgs.push_back("-dynamic-linker");
5658 CmdArgs.push_back("/libexec/ld.elf_so");
5659 }
5660 }
5661
5662 // When building 32-bit code on NetBSD/amd64, we have to explicitly
5663 // instruct ld in the base system to link 32-bit code.
Joerg Sonnenbergerd64c60e2012-01-26 22:27:52 +00005664 if (getToolChain().getArch() == llvm::Triple::x86) {
Benjamin Kramer24f1d3e2011-02-02 18:59:27 +00005665 CmdArgs.push_back("-m");
5666 CmdArgs.push_back("elf_i386");
5667 }
5668
5669 if (Output.isFilename()) {
5670 CmdArgs.push_back("-o");
5671 CmdArgs.push_back(Output.getFilename());
5672 } else {
5673 assert(Output.isNothing() && "Invalid output.");
5674 }
5675
5676 if (!Args.hasArg(options::OPT_nostdlib) &&
5677 !Args.hasArg(options::OPT_nostartfiles)) {
5678 if (!Args.hasArg(options::OPT_shared)) {
5679 CmdArgs.push_back(Args.MakeArgString(
5680 getToolChain().GetFilePath("crt0.o")));
5681 CmdArgs.push_back(Args.MakeArgString(
5682 getToolChain().GetFilePath("crti.o")));
5683 CmdArgs.push_back(Args.MakeArgString(
5684 getToolChain().GetFilePath("crtbegin.o")));
5685 } else {
5686 CmdArgs.push_back(Args.MakeArgString(
5687 getToolChain().GetFilePath("crti.o")));
5688 CmdArgs.push_back(Args.MakeArgString(
5689 getToolChain().GetFilePath("crtbeginS.o")));
5690 }
5691 }
5692
5693 Args.AddAllArgs(CmdArgs, options::OPT_L);
5694 Args.AddAllArgs(CmdArgs, options::OPT_T_Group);
5695 Args.AddAllArgs(CmdArgs, options::OPT_e);
5696 Args.AddAllArgs(CmdArgs, options::OPT_s);
5697 Args.AddAllArgs(CmdArgs, options::OPT_t);
5698 Args.AddAllArgs(CmdArgs, options::OPT_Z_Flag);
5699 Args.AddAllArgs(CmdArgs, options::OPT_r);
5700
5701 AddLinkerInputs(getToolChain(), Inputs, Args, CmdArgs);
5702
5703 if (!Args.hasArg(options::OPT_nostdlib) &&
5704 !Args.hasArg(options::OPT_nodefaultlibs)) {
5705 if (D.CCCIsCXX) {
5706 getToolChain().AddCXXStdlibLibArgs(Args, CmdArgs);
5707 CmdArgs.push_back("-lm");
5708 }
5709 // FIXME: For some reason GCC passes -lgcc and -lgcc_s before adding
5710 // the default system libraries. Just mimic this for now.
Benjamin Kramer24f1d3e2011-02-02 18:59:27 +00005711 if (Args.hasArg(options::OPT_static)) {
5712 CmdArgs.push_back("-lgcc_eh");
5713 } else {
5714 CmdArgs.push_back("--as-needed");
5715 CmdArgs.push_back("-lgcc_s");
5716 CmdArgs.push_back("--no-as-needed");
5717 }
Joerg Sonnenberger87717772011-06-07 23:39:17 +00005718 CmdArgs.push_back("-lgcc");
Benjamin Kramer24f1d3e2011-02-02 18:59:27 +00005719
5720 if (Args.hasArg(options::OPT_pthread))
5721 CmdArgs.push_back("-lpthread");
5722 CmdArgs.push_back("-lc");
5723
5724 CmdArgs.push_back("-lgcc");
5725 if (Args.hasArg(options::OPT_static)) {
5726 CmdArgs.push_back("-lgcc_eh");
5727 } else {
5728 CmdArgs.push_back("--as-needed");
5729 CmdArgs.push_back("-lgcc_s");
5730 CmdArgs.push_back("--no-as-needed");
5731 }
5732 }
5733
5734 if (!Args.hasArg(options::OPT_nostdlib) &&
5735 !Args.hasArg(options::OPT_nostartfiles)) {
5736 if (!Args.hasArg(options::OPT_shared))
5737 CmdArgs.push_back(Args.MakeArgString(getToolChain().GetFilePath(
5738 "crtend.o")));
5739 else
5740 CmdArgs.push_back(Args.MakeArgString(getToolChain().GetFilePath(
5741 "crtendS.o")));
5742 CmdArgs.push_back(Args.MakeArgString(getToolChain().GetFilePath(
5743 "crtn.o")));
5744 }
5745
Bill Wendling08760582011-06-27 19:15:03 +00005746 addProfileRT(getToolChain(), Args, CmdArgs, getToolChain().getTriple());
Nick Lewycky82fe5f42011-05-24 21:54:59 +00005747
David Chisnallddbd68f2011-09-27 22:03:18 +00005748 const char *Exec = Args.MakeArgString(getToolChain().GetProgramPath("ld"));
Benjamin Kramer24f1d3e2011-02-02 18:59:27 +00005749 C.addCommand(new Command(JA, *this, Exec, CmdArgs));
5750}
5751
Thomas Schwinge4e555262013-03-28 19:04:25 +00005752void gnutools::Assemble::ConstructJob(Compilation &C, const JobAction &JA,
5753 const InputInfo &Output,
5754 const InputInfoList &Inputs,
5755 const ArgList &Args,
5756 const char *LinkingOutput) const {
Rafael Espindola92b00932010-08-10 00:25:48 +00005757 ArgStringList CmdArgs;
5758
5759 // Add --32/--64 to make sure we get the format we want.
5760 // This is incomplete
5761 if (getToolChain().getArch() == llvm::Triple::x86) {
5762 CmdArgs.push_back("--32");
5763 } else if (getToolChain().getArch() == llvm::Triple::x86_64) {
5764 CmdArgs.push_back("--64");
Eli Friedman4a4932c2011-11-28 23:46:52 +00005765 } else if (getToolChain().getArch() == llvm::Triple::ppc) {
5766 CmdArgs.push_back("-a32");
5767 CmdArgs.push_back("-mppc");
5768 CmdArgs.push_back("-many");
5769 } else if (getToolChain().getArch() == llvm::Triple::ppc64) {
5770 CmdArgs.push_back("-a64");
5771 CmdArgs.push_back("-mppc64");
5772 CmdArgs.push_back("-many");
Rafael Espindola92b00932010-08-10 00:25:48 +00005773 } else if (getToolChain().getArch() == llvm::Triple::arm) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00005774 StringRef MArch = getToolChain().getArchName();
Rafael Espindola92b00932010-08-10 00:25:48 +00005775 if (MArch == "armv7" || MArch == "armv7a" || MArch == "armv7-a")
5776 CmdArgs.push_back("-mfpu=neon");
Evgeniy Stepanoved943f62012-04-20 09:03:40 +00005777
5778 StringRef ARMFloatABI = getARMFloatABI(getToolChain().getDriver(), Args,
5779 getToolChain().getTriple());
5780 CmdArgs.push_back(Args.MakeArgString("-mfloat-abi=" + ARMFloatABI));
Evgeniy Stepanov582911a2012-04-24 09:05:31 +00005781
5782 Args.AddLastArg(CmdArgs, options::OPT_march_EQ);
5783 Args.AddLastArg(CmdArgs, options::OPT_mcpu_EQ);
5784 Args.AddLastArg(CmdArgs, options::OPT_mfpu_EQ);
Akira Hatanaka5e9dde32011-11-30 19:31:38 +00005785 } else if (getToolChain().getArch() == llvm::Triple::mips ||
5786 getToolChain().getArch() == llvm::Triple::mipsel ||
5787 getToolChain().getArch() == llvm::Triple::mips64 ||
5788 getToolChain().getArch() == llvm::Triple::mips64el) {
Simon Atanasyan571d7bd2012-04-07 22:31:29 +00005789 StringRef CPUName;
5790 StringRef ABIName;
5791 getMipsCPUAndABI(Args, getToolChain(), CPUName, ABIName);
Akira Hatanaka5e9dde32011-11-30 19:31:38 +00005792
Simon Atanasyan571d7bd2012-04-07 22:31:29 +00005793 CmdArgs.push_back("-march");
5794 CmdArgs.push_back(CPUName.data());
5795
Simon Atanasyan571d7bd2012-04-07 22:31:29 +00005796 CmdArgs.push_back("-mabi");
Simon Atanasyan0da400c2013-02-27 14:55:49 +00005797 CmdArgs.push_back(getGnuCompatibleMipsABIName(ABIName).data());
Simon Atanasyan2390aa12012-04-06 19:15:24 +00005798
5799 if (getToolChain().getArch() == llvm::Triple::mips ||
5800 getToolChain().getArch() == llvm::Triple::mips64)
5801 CmdArgs.push_back("-EB");
5802 else
5803 CmdArgs.push_back("-EL");
Simon Atanasyan217dc2d2012-05-29 19:07:33 +00005804
5805 Arg *LastPICArg = Args.getLastArg(options::OPT_fPIC, options::OPT_fno_PIC,
5806 options::OPT_fpic, options::OPT_fno_pic,
5807 options::OPT_fPIE, options::OPT_fno_PIE,
5808 options::OPT_fpie, options::OPT_fno_pie);
5809 if (LastPICArg &&
5810 (LastPICArg->getOption().matches(options::OPT_fPIC) ||
5811 LastPICArg->getOption().matches(options::OPT_fpic) ||
5812 LastPICArg->getOption().matches(options::OPT_fPIE) ||
5813 LastPICArg->getOption().matches(options::OPT_fpie))) {
5814 CmdArgs.push_back("-KPIC");
5815 }
Rafael Espindola92b00932010-08-10 00:25:48 +00005816 }
5817
5818 Args.AddAllArgValues(CmdArgs, options::OPT_Wa_COMMA,
5819 options::OPT_Xassembler);
5820
5821 CmdArgs.push_back("-o");
5822 CmdArgs.push_back(Output.getFilename());
5823
5824 for (InputInfoList::const_iterator
5825 it = Inputs.begin(), ie = Inputs.end(); it != ie; ++it) {
5826 const InputInfo &II = *it;
5827 CmdArgs.push_back(II.getFilename());
5828 }
5829
5830 const char *Exec =
5831 Args.MakeArgString(getToolChain().GetProgramPath("as"));
5832 C.addCommand(new Command(JA, *this, Exec, CmdArgs));
5833}
5834
Evgeniy Stepanov77866712012-04-25 08:59:22 +00005835static void AddLibgcc(llvm::Triple Triple, const Driver &D,
5836 ArgStringList &CmdArgs, const ArgList &Args) {
Logan Chienc6fd8202012-09-02 09:30:11 +00005837 bool isAndroid = Triple.getEnvironment() == llvm::Triple::Android;
Chandler Carruth58d6eb62013-03-04 02:07:55 +00005838 bool StaticLibgcc = Args.hasArg(options::OPT_static_libgcc) ||
5839 Args.hasArg(options::OPT_static);
Rafael Espindolacc354322011-10-17 21:39:04 +00005840 if (!D.CCCIsCXX)
5841 CmdArgs.push_back("-lgcc");
5842
Logan Chien3d3373c2012-11-19 12:04:11 +00005843 if (StaticLibgcc || isAndroid) {
Rafael Espindolacc354322011-10-17 21:39:04 +00005844 if (D.CCCIsCXX)
5845 CmdArgs.push_back("-lgcc");
5846 } else {
5847 if (!D.CCCIsCXX)
5848 CmdArgs.push_back("--as-needed");
5849 CmdArgs.push_back("-lgcc_s");
5850 if (!D.CCCIsCXX)
5851 CmdArgs.push_back("--no-as-needed");
5852 }
5853
Evgeniy Stepanov77866712012-04-25 08:59:22 +00005854 if (StaticLibgcc && !isAndroid)
Rafael Espindolacc354322011-10-17 21:39:04 +00005855 CmdArgs.push_back("-lgcc_eh");
5856 else if (!Args.hasArg(options::OPT_shared) && D.CCCIsCXX)
5857 CmdArgs.push_back("-lgcc");
Logan Chien3d3373c2012-11-19 12:04:11 +00005858
5859 // According to Android ABI, we have to link with libdl if we are
5860 // linking with non-static libgcc.
5861 //
5862 // NOTE: This fixes a link error on Android MIPS as well. The non-static
5863 // libgcc for MIPS relies on _Unwind_Find_FDE and dl_iterate_phdr from libdl.
5864 if (isAndroid && !StaticLibgcc)
5865 CmdArgs.push_back("-ldl");
Rafael Espindolacc354322011-10-17 21:39:04 +00005866}
5867
Simon Atanasyan2d1b1ad2012-10-21 11:44:57 +00005868static bool hasMipsN32ABIArg(const ArgList &Args) {
5869 Arg *A = Args.getLastArg(options::OPT_mabi_EQ);
Richard Smithbd55daf2012-11-01 04:30:05 +00005870 return A && (A->getValue() == StringRef("n32"));
Simon Atanasyan2d1b1ad2012-10-21 11:44:57 +00005871}
5872
Thomas Schwinge4e555262013-03-28 19:04:25 +00005873void gnutools::Link::ConstructJob(Compilation &C, const JobAction &JA,
5874 const InputInfo &Output,
5875 const InputInfoList &Inputs,
5876 const ArgList &Args,
5877 const char *LinkingOutput) const {
Rafael Espindolac8f008f2010-11-07 20:14:31 +00005878 const toolchains::Linux& ToolChain =
5879 static_cast<const toolchains::Linux&>(getToolChain());
5880 const Driver &D = ToolChain.getDriver();
Rafael Espindola7df35012012-11-02 20:41:30 +00005881 const bool isAndroid =
5882 ToolChain.getTriple().getEnvironment() == llvm::Triple::Android;
Peter Collingbourne54d770c2013-04-09 04:35:11 +00005883 SanitizerArgs Sanitize(getToolChain(), Args);
5884 const bool IsPIE =
5885 !Args.hasArg(options::OPT_shared) &&
5886 (Args.hasArg(options::OPT_pie) || Sanitize.hasZeroBaseShadow());
Evgeniy Stepanov77866712012-04-25 08:59:22 +00005887
Rafael Espindolac8f008f2010-11-07 20:14:31 +00005888 ArgStringList CmdArgs;
5889
Rafael Espindolad1002f62010-11-15 18:28:16 +00005890 // Silence warning for "clang -g foo.o -o foo"
5891 Args.ClaimAllArgs(options::OPT_g_Group);
Rafael Espindolad95a8122011-03-01 05:25:27 +00005892 // and "clang -emit-llvm foo.o -o foo"
5893 Args.ClaimAllArgs(options::OPT_emit_llvm);
David Chisnall5f99f482012-07-29 15:24:44 +00005894 // and for "clang -w foo.o -o foo". Other warning options are already
Rafael Espindolaf92614c2010-11-17 20:37:10 +00005895 // handled somewhere else.
5896 Args.ClaimAllArgs(options::OPT_w);
Rafael Espindolad1002f62010-11-15 18:28:16 +00005897
Joerg Sonnenberger6165ab12011-03-21 13:51:29 +00005898 if (!D.SysRoot.empty())
5899 CmdArgs.push_back(Args.MakeArgString("--sysroot=" + D.SysRoot));
Rafael Espindolac8f008f2010-11-07 20:14:31 +00005900
Peter Collingbourne54d770c2013-04-09 04:35:11 +00005901 if (IsPIE)
Rafael Espindolad47ac232010-11-17 22:26:15 +00005902 CmdArgs.push_back("-pie");
5903
Rafael Espindola1c76c592010-11-07 22:57:16 +00005904 if (Args.hasArg(options::OPT_rdynamic))
5905 CmdArgs.push_back("-export-dynamic");
5906
Rafael Espindola34d77dc2010-11-11 19:34:42 +00005907 if (Args.hasArg(options::OPT_s))
5908 CmdArgs.push_back("-s");
5909
Rafael Espindolac8f008f2010-11-07 20:14:31 +00005910 for (std::vector<std::string>::const_iterator i = ToolChain.ExtraOpts.begin(),
5911 e = ToolChain.ExtraOpts.end();
5912 i != e; ++i)
5913 CmdArgs.push_back(i->c_str());
5914
5915 if (!Args.hasArg(options::OPT_static)) {
5916 CmdArgs.push_back("--eh-frame-hdr");
5917 }
5918
5919 CmdArgs.push_back("-m");
5920 if (ToolChain.getArch() == llvm::Triple::x86)
5921 CmdArgs.push_back("elf_i386");
Tim Northover9bb857a2013-01-31 12:13:10 +00005922 else if (ToolChain.getArch() == llvm::Triple::aarch64)
5923 CmdArgs.push_back("aarch64linux");
Eric Christopher84fbdb42011-08-19 00:30:14 +00005924 else if (ToolChain.getArch() == llvm::Triple::arm
Douglas Gregord9bb1522011-03-06 19:11:49 +00005925 || ToolChain.getArch() == llvm::Triple::thumb)
Rafael Espindolac8f008f2010-11-07 20:14:31 +00005926 CmdArgs.push_back("armelf_linux_eabi");
Ted Kremenek43d47cc2011-04-05 22:04:27 +00005927 else if (ToolChain.getArch() == llvm::Triple::ppc)
5928 CmdArgs.push_back("elf32ppclinux");
5929 else if (ToolChain.getArch() == llvm::Triple::ppc64)
5930 CmdArgs.push_back("elf64ppc");
Eli Friedman27b8c4f2011-11-08 19:43:37 +00005931 else if (ToolChain.getArch() == llvm::Triple::mips)
5932 CmdArgs.push_back("elf32btsmip");
5933 else if (ToolChain.getArch() == llvm::Triple::mipsel)
5934 CmdArgs.push_back("elf32ltsmip");
Simon Atanasyan2d1b1ad2012-10-21 11:44:57 +00005935 else if (ToolChain.getArch() == llvm::Triple::mips64) {
5936 if (hasMipsN32ABIArg(Args))
5937 CmdArgs.push_back("elf32btsmipn32");
5938 else
5939 CmdArgs.push_back("elf64btsmip");
5940 }
5941 else if (ToolChain.getArch() == llvm::Triple::mips64el) {
5942 if (hasMipsN32ABIArg(Args))
5943 CmdArgs.push_back("elf32ltsmipn32");
5944 else
5945 CmdArgs.push_back("elf64ltsmip");
5946 }
Rafael Espindolac8f008f2010-11-07 20:14:31 +00005947 else
5948 CmdArgs.push_back("elf_x86_64");
5949
5950 if (Args.hasArg(options::OPT_static)) {
Douglas Gregord9bb1522011-03-06 19:11:49 +00005951 if (ToolChain.getArch() == llvm::Triple::arm
5952 || ToolChain.getArch() == llvm::Triple::thumb)
Rafael Espindolac8f008f2010-11-07 20:14:31 +00005953 CmdArgs.push_back("-Bstatic");
5954 else
5955 CmdArgs.push_back("-static");
5956 } else if (Args.hasArg(options::OPT_shared)) {
5957 CmdArgs.push_back("-shared");
Rafael Espindola7df35012012-11-02 20:41:30 +00005958 if (isAndroid) {
Evgeniy Stepanov77866712012-04-25 08:59:22 +00005959 CmdArgs.push_back("-Bsymbolic");
5960 }
Rafael Espindolac8f008f2010-11-07 20:14:31 +00005961 }
5962
5963 if (ToolChain.getArch() == llvm::Triple::arm ||
Douglas Gregord9bb1522011-03-06 19:11:49 +00005964 ToolChain.getArch() == llvm::Triple::thumb ||
Rafael Espindolac8f008f2010-11-07 20:14:31 +00005965 (!Args.hasArg(options::OPT_static) &&
5966 !Args.hasArg(options::OPT_shared))) {
5967 CmdArgs.push_back("-dynamic-linker");
Evgeniy Stepanov77866712012-04-25 08:59:22 +00005968 if (isAndroid)
5969 CmdArgs.push_back("/system/bin/linker");
5970 else if (ToolChain.getArch() == llvm::Triple::x86)
Rafael Espindolac8f008f2010-11-07 20:14:31 +00005971 CmdArgs.push_back("/lib/ld-linux.so.2");
Tim Northover9bb857a2013-01-31 12:13:10 +00005972 else if (ToolChain.getArch() == llvm::Triple::aarch64)
5973 CmdArgs.push_back("/lib/ld-linux-aarch64.so.1");
Douglas Gregord9bb1522011-03-06 19:11:49 +00005974 else if (ToolChain.getArch() == llvm::Triple::arm ||
Jiangning Liueabbf922012-07-30 11:05:56 +00005975 ToolChain.getArch() == llvm::Triple::thumb) {
5976 if (ToolChain.getTriple().getEnvironment() == llvm::Triple::GNUEABIHF)
5977 CmdArgs.push_back("/lib/ld-linux-armhf.so.3");
5978 else
5979 CmdArgs.push_back("/lib/ld-linux.so.3");
5980 }
Eli Friedman27b8c4f2011-11-08 19:43:37 +00005981 else if (ToolChain.getArch() == llvm::Triple::mips ||
5982 ToolChain.getArch() == llvm::Triple::mipsel)
5983 CmdArgs.push_back("/lib/ld.so.1");
Simon Atanasyan1e1e2e22012-04-06 20:14:27 +00005984 else if (ToolChain.getArch() == llvm::Triple::mips64 ||
Simon Atanasyan2d1b1ad2012-10-21 11:44:57 +00005985 ToolChain.getArch() == llvm::Triple::mips64el) {
5986 if (hasMipsN32ABIArg(Args))
5987 CmdArgs.push_back("/lib32/ld.so.1");
5988 else
5989 CmdArgs.push_back("/lib64/ld.so.1");
5990 }
Ted Kremenek43d47cc2011-04-05 22:04:27 +00005991 else if (ToolChain.getArch() == llvm::Triple::ppc)
Chris Lattner20b90d02011-04-11 21:15:37 +00005992 CmdArgs.push_back("/lib/ld.so.1");
Ted Kremenek43d47cc2011-04-05 22:04:27 +00005993 else if (ToolChain.getArch() == llvm::Triple::ppc64)
Chris Lattner20b90d02011-04-11 21:15:37 +00005994 CmdArgs.push_back("/lib64/ld64.so.1");
Rafael Espindolac8f008f2010-11-07 20:14:31 +00005995 else
5996 CmdArgs.push_back("/lib64/ld-linux-x86-64.so.2");
5997 }
5998
5999 CmdArgs.push_back("-o");
6000 CmdArgs.push_back(Output.getFilename());
6001
Rafael Espindola81937ec2010-12-01 01:52:43 +00006002 if (!Args.hasArg(options::OPT_nostdlib) &&
6003 !Args.hasArg(options::OPT_nostartfiles)) {
Evgeniy Stepanov77866712012-04-25 08:59:22 +00006004 if (!isAndroid) {
6005 const char *crt1 = NULL;
6006 if (!Args.hasArg(options::OPT_shared)){
Peter Collingbourne54d770c2013-04-09 04:35:11 +00006007 if (IsPIE)
Evgeniy Stepanov77866712012-04-25 08:59:22 +00006008 crt1 = "Scrt1.o";
6009 else
6010 crt1 = "crt1.o";
6011 }
6012 if (crt1)
6013 CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath(crt1)));
Rafael Espindolac8f008f2010-11-07 20:14:31 +00006014
Evgeniy Stepanov77866712012-04-25 08:59:22 +00006015 CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath("crti.o")));
6016 }
Rafael Espindolac8f008f2010-11-07 20:14:31 +00006017
Rafael Espindola9aa60e92010-11-12 03:00:39 +00006018 const char *crtbegin;
6019 if (Args.hasArg(options::OPT_static))
Evgeniy Stepanov77866712012-04-25 08:59:22 +00006020 crtbegin = isAndroid ? "crtbegin_static.o" : "crtbeginT.o";
Evgeniy Stepanovc9cde482012-09-10 10:30:12 +00006021 else if (Args.hasArg(options::OPT_shared))
Evgeniy Stepanov77866712012-04-25 08:59:22 +00006022 crtbegin = isAndroid ? "crtbegin_so.o" : "crtbeginS.o";
Peter Collingbourne54d770c2013-04-09 04:35:11 +00006023 else if (IsPIE)
Evgeniy Stepanovc9cde482012-09-10 10:30:12 +00006024 crtbegin = isAndroid ? "crtbegin_dynamic.o" : "crtbeginS.o";
Rafael Espindola9aa60e92010-11-12 03:00:39 +00006025 else
Evgeniy Stepanov77866712012-04-25 08:59:22 +00006026 crtbegin = isAndroid ? "crtbegin_dynamic.o" : "crtbegin.o";
Rafael Espindola9aa60e92010-11-12 03:00:39 +00006027 CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath(crtbegin)));
Benjamin Kramer058666a2012-10-04 19:42:20 +00006028
6029 // Add crtfastmath.o if available and fast math is enabled.
6030 ToolChain.AddFastMathRuntimeIfAvailable(Args, CmdArgs);
Rafael Espindola9aa60e92010-11-12 03:00:39 +00006031 }
Rafael Espindolac8f008f2010-11-07 20:14:31 +00006032
6033 Args.AddAllArgs(CmdArgs, options::OPT_L);
6034
6035 const ToolChain::path_list Paths = ToolChain.getFilePaths();
6036
Roman Divackyee8188a2011-03-01 17:53:14 +00006037 for (ToolChain::path_list::const_iterator i = Paths.begin(), e = Paths.end();
6038 i != e; ++i)
Chris Lattner0e62c1c2011-07-23 10:55:15 +00006039 CmdArgs.push_back(Args.MakeArgString(StringRef("-L") + *i));
Rafael Espindolac8f008f2010-11-07 20:14:31 +00006040
Rafael Espindola9446d762012-04-09 23:53:34 +00006041 // Tell the linker to load the plugin. This has to come before AddLinkerInputs
6042 // as gold requires -plugin to come before any -plugin-opt that -Wl might
6043 // forward.
6044 if (D.IsUsingLTO(Args) || Args.hasArg(options::OPT_use_gold_plugin)) {
6045 CmdArgs.push_back("-plugin");
6046 std::string Plugin = ToolChain.getDriver().Dir + "/../lib/LLVMgold.so";
6047 CmdArgs.push_back(Args.MakeArgString(Plugin));
Chandler Carruth953fb082013-01-13 11:46:33 +00006048
6049 // Try to pass driver level flags relevant to LTO code generation down to
6050 // the plugin.
6051
6052 // Handle architecture-specific flags for selecting CPU variants.
6053 if (ToolChain.getArch() == llvm::Triple::x86 ||
6054 ToolChain.getArch() == llvm::Triple::x86_64)
6055 CmdArgs.push_back(
6056 Args.MakeArgString(Twine("-plugin-opt=mcpu=") +
6057 getX86TargetCPU(Args, ToolChain.getTriple())));
6058 else if (ToolChain.getArch() == llvm::Triple::arm ||
6059 ToolChain.getArch() == llvm::Triple::thumb)
6060 CmdArgs.push_back(
6061 Args.MakeArgString(Twine("-plugin-opt=mcpu=") +
6062 getARMTargetCPU(Args, ToolChain.getTriple())));
6063
6064 // FIXME: Factor out logic for MIPS, PPC, and other targets to support this
6065 // as well.
Rafael Espindola9446d762012-04-09 23:53:34 +00006066 }
6067
Chandler Carruth953fb082013-01-13 11:46:33 +00006068
Nick Lewycky2fe6aab2012-08-17 03:39:16 +00006069 if (Args.hasArg(options::OPT_Z_Xlinker__no_demangle))
6070 CmdArgs.push_back("--no-demangle");
6071
Rafael Espindolac8f008f2010-11-07 20:14:31 +00006072 AddLinkerInputs(ToolChain, Inputs, Args, CmdArgs);
6073
Eric Christopher04997782012-11-29 18:51:05 +00006074 // Call these before we add the C++ ABI library.
Richard Smith52be6192012-11-05 22:04:41 +00006075 if (Sanitize.needsUbsanRt())
Richard Smithcff3cde2013-03-20 23:49:07 +00006076 addUbsanRTLinux(getToolChain(), Args, CmdArgs, D.CCCIsCXX,
6077 Sanitize.needsAsanRt() || Sanitize.needsTsanRt() ||
6078 Sanitize.needsMsanRt());
Eric Christopher04997782012-11-29 18:51:05 +00006079 if (Sanitize.needsAsanRt())
6080 addAsanRTLinux(getToolChain(), Args, CmdArgs);
6081 if (Sanitize.needsTsanRt())
6082 addTsanRTLinux(getToolChain(), Args, CmdArgs);
Evgeniy Stepanovaea92e52012-12-03 13:20:43 +00006083 if (Sanitize.needsMsanRt())
6084 addMsanRTLinux(getToolChain(), Args, CmdArgs);
Richard Smith4d3110a2012-10-25 02:14:12 +00006085
Chandler Carruth94a32012012-05-14 18:31:18 +00006086 if (D.CCCIsCXX &&
6087 !Args.hasArg(options::OPT_nostdlib) &&
6088 !Args.hasArg(options::OPT_nodefaultlibs)) {
Rafael Espindola70b8d762011-10-17 22:14:51 +00006089 bool OnlyLibstdcxxStatic = Args.hasArg(options::OPT_static_libstdcxx) &&
6090 !Args.hasArg(options::OPT_static);
6091 if (OnlyLibstdcxxStatic)
6092 CmdArgs.push_back("-Bstatic");
Rafael Espindolac8f008f2010-11-07 20:14:31 +00006093 ToolChain.AddCXXStdlibLibArgs(Args, CmdArgs);
Rafael Espindola70b8d762011-10-17 22:14:51 +00006094 if (OnlyLibstdcxxStatic)
6095 CmdArgs.push_back("-Bdynamic");
Rafael Espindolac8f008f2010-11-07 20:14:31 +00006096 CmdArgs.push_back("-lm");
6097 }
6098
Rafael Espindola9aa60e92010-11-12 03:00:39 +00006099 if (!Args.hasArg(options::OPT_nostdlib)) {
Chandler Carruth94a32012012-05-14 18:31:18 +00006100 if (!Args.hasArg(options::OPT_nodefaultlibs)) {
6101 if (Args.hasArg(options::OPT_static))
6102 CmdArgs.push_back("--start-group");
Nick Lewycky97864da2011-06-04 06:27:06 +00006103
Chandler Carruth01538002013-01-17 13:19:29 +00006104 bool OpenMP = Args.hasArg(options::OPT_fopenmp);
6105 if (OpenMP) {
6106 CmdArgs.push_back("-lgomp");
6107
6108 // FIXME: Exclude this for platforms whith libgomp that doesn't require
6109 // librt. Most modern Linux platfroms require it, but some may not.
6110 CmdArgs.push_back("-lrt");
6111 }
6112
Evgeniy Stepanov77866712012-04-25 08:59:22 +00006113 AddLibgcc(ToolChain.getTriple(), D, CmdArgs, Args);
Rafael Espindola9aa60e92010-11-12 03:00:39 +00006114
Chandler Carruth94a32012012-05-14 18:31:18 +00006115 if (Args.hasArg(options::OPT_pthread) ||
Chandler Carruth01538002013-01-17 13:19:29 +00006116 Args.hasArg(options::OPT_pthreads) || OpenMP)
Chandler Carruth94a32012012-05-14 18:31:18 +00006117 CmdArgs.push_back("-lpthread");
6118
6119 CmdArgs.push_back("-lc");
6120
6121 if (Args.hasArg(options::OPT_static))
6122 CmdArgs.push_back("--end-group");
6123 else
6124 AddLibgcc(ToolChain.getTriple(), D, CmdArgs, Args);
6125 }
Rafael Espindolad47ac232010-11-17 22:26:15 +00006126
Rafael Espindola81937ec2010-12-01 01:52:43 +00006127 if (!Args.hasArg(options::OPT_nostartfiles)) {
6128 const char *crtend;
Evgeniy Stepanovc9cde482012-09-10 10:30:12 +00006129 if (Args.hasArg(options::OPT_shared))
Evgeniy Stepanov77866712012-04-25 08:59:22 +00006130 crtend = isAndroid ? "crtend_so.o" : "crtendS.o";
Peter Collingbourne54d770c2013-04-09 04:35:11 +00006131 else if (IsPIE)
Evgeniy Stepanovc9cde482012-09-10 10:30:12 +00006132 crtend = isAndroid ? "crtend_android.o" : "crtendS.o";
Rafael Espindola81937ec2010-12-01 01:52:43 +00006133 else
Evgeniy Stepanov77866712012-04-25 08:59:22 +00006134 crtend = isAndroid ? "crtend_android.o" : "crtend.o";
Rafael Espindola9aa60e92010-11-12 03:00:39 +00006135
Rafael Espindola81937ec2010-12-01 01:52:43 +00006136 CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath(crtend)));
Evgeniy Stepanov77866712012-04-25 08:59:22 +00006137 if (!isAndroid)
6138 CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath("crtn.o")));
Rafael Espindola81937ec2010-12-01 01:52:43 +00006139 }
Rafael Espindolac8f008f2010-11-07 20:14:31 +00006140 }
6141
Bill Wendling08760582011-06-27 19:15:03 +00006142 addProfileRT(getToolChain(), Args, CmdArgs, getToolChain().getTriple());
Nick Lewycky82fe5f42011-05-24 21:54:59 +00006143
Rafael Espindolac8f008f2010-11-07 20:14:31 +00006144 C.addCommand(new Command(JA, *this, ToolChain.Linker.c_str(), CmdArgs));
6145}
Rafael Espindola92b00932010-08-10 00:25:48 +00006146
Chris Lattner3e2ee142010-07-07 16:01:42 +00006147void minix::Assemble::ConstructJob(Compilation &C, const JobAction &JA,
Daniel Dunbar7fbaf532010-08-02 02:38:28 +00006148 const InputInfo &Output,
6149 const InputInfoList &Inputs,
6150 const ArgList &Args,
6151 const char *LinkingOutput) const {
Chris Lattner3e2ee142010-07-07 16:01:42 +00006152 ArgStringList CmdArgs;
6153
6154 Args.AddAllArgValues(CmdArgs, options::OPT_Wa_COMMA,
6155 options::OPT_Xassembler);
6156
6157 CmdArgs.push_back("-o");
Daniel Dunbarb440f562010-08-02 02:38:21 +00006158 CmdArgs.push_back(Output.getFilename());
Chris Lattner3e2ee142010-07-07 16:01:42 +00006159
6160 for (InputInfoList::const_iterator
6161 it = Inputs.begin(), ie = Inputs.end(); it != ie; ++it) {
6162 const InputInfo &II = *it;
Daniel Dunbarb440f562010-08-02 02:38:21 +00006163 CmdArgs.push_back(II.getFilename());
Chris Lattner3e2ee142010-07-07 16:01:42 +00006164 }
6165
6166 const char *Exec =
Eli Friedman83de5132011-12-08 23:54:21 +00006167 Args.MakeArgString(getToolChain().GetProgramPath("as"));
Daniel Dunbar7fbaf532010-08-02 02:38:28 +00006168 C.addCommand(new Command(JA, *this, Exec, CmdArgs));
Chris Lattner3e2ee142010-07-07 16:01:42 +00006169}
6170
6171void minix::Link::ConstructJob(Compilation &C, const JobAction &JA,
Daniel Dunbar7fbaf532010-08-02 02:38:28 +00006172 const InputInfo &Output,
6173 const InputInfoList &Inputs,
6174 const ArgList &Args,
6175 const char *LinkingOutput) const {
Chris Lattner3e2ee142010-07-07 16:01:42 +00006176 const Driver &D = getToolChain().getDriver();
6177 ArgStringList CmdArgs;
6178
Daniel Dunbarb440f562010-08-02 02:38:21 +00006179 if (Output.isFilename()) {
Chris Lattner3e2ee142010-07-07 16:01:42 +00006180 CmdArgs.push_back("-o");
6181 CmdArgs.push_back(Output.getFilename());
6182 } else {
6183 assert(Output.isNothing() && "Invalid output.");
6184 }
6185
6186 if (!Args.hasArg(options::OPT_nostdlib) &&
Eli Friedman83de5132011-12-08 23:54:21 +00006187 !Args.hasArg(options::OPT_nostartfiles)) {
6188 CmdArgs.push_back(Args.MakeArgString(getToolChain().GetFilePath("crt1.o")));
6189 CmdArgs.push_back(Args.MakeArgString(getToolChain().GetFilePath("crti.o")));
6190 CmdArgs.push_back(Args.MakeArgString(getToolChain().GetFilePath("crtbegin.o")));
6191 CmdArgs.push_back(Args.MakeArgString(getToolChain().GetFilePath("crtn.o")));
6192 }
Chris Lattner3e2ee142010-07-07 16:01:42 +00006193
6194 Args.AddAllArgs(CmdArgs, options::OPT_L);
6195 Args.AddAllArgs(CmdArgs, options::OPT_T_Group);
6196 Args.AddAllArgs(CmdArgs, options::OPT_e);
6197
Daniel Dunbar54423b22010-09-17 00:24:54 +00006198 AddLinkerInputs(getToolChain(), Inputs, Args, CmdArgs);
Chris Lattner3e2ee142010-07-07 16:01:42 +00006199
Eli Friedman83de5132011-12-08 23:54:21 +00006200 addProfileRT(getToolChain(), Args, CmdArgs, getToolChain().getTriple());
6201
Chris Lattner3e2ee142010-07-07 16:01:42 +00006202 if (!Args.hasArg(options::OPT_nostdlib) &&
6203 !Args.hasArg(options::OPT_nodefaultlibs)) {
6204 if (D.CCCIsCXX) {
Daniel Dunbar3f7796f2010-09-17 01:20:05 +00006205 getToolChain().AddCXXStdlibLibArgs(Args, CmdArgs);
Chris Lattner3e2ee142010-07-07 16:01:42 +00006206 CmdArgs.push_back("-lm");
6207 }
Chris Lattner3e2ee142010-07-07 16:01:42 +00006208 }
6209
6210 if (!Args.hasArg(options::OPT_nostdlib) &&
6211 !Args.hasArg(options::OPT_nostartfiles)) {
Eli Friedman83de5132011-12-08 23:54:21 +00006212 if (Args.hasArg(options::OPT_pthread))
6213 CmdArgs.push_back("-lpthread");
6214 CmdArgs.push_back("-lc");
6215 CmdArgs.push_back("-lCompilerRT-Generic");
6216 CmdArgs.push_back("-L/usr/pkg/compiler-rt/lib");
6217 CmdArgs.push_back(
Eric Christopher45f2e712012-12-18 00:31:10 +00006218 Args.MakeArgString(getToolChain().GetFilePath("crtend.o")));
Chris Lattner3e2ee142010-07-07 16:01:42 +00006219 }
6220
Eli Friedman83de5132011-12-08 23:54:21 +00006221 const char *Exec = Args.MakeArgString(getToolChain().GetProgramPath("ld"));
Daniel Dunbar7fbaf532010-08-02 02:38:28 +00006222 C.addCommand(new Command(JA, *this, Exec, CmdArgs));
Chris Lattner3e2ee142010-07-07 16:01:42 +00006223}
6224
Daniel Dunbarcc912342009-05-02 18:28:39 +00006225/// DragonFly Tools
6226
6227// For now, DragonFly Assemble does just about the same as for
6228// FreeBSD, but this may change soon.
6229void dragonfly::Assemble::ConstructJob(Compilation &C, const JobAction &JA,
Daniel Dunbar7fbaf532010-08-02 02:38:28 +00006230 const InputInfo &Output,
Daniel Dunbar1c7577c2009-11-04 06:24:38 +00006231 const InputInfoList &Inputs,
6232 const ArgList &Args,
6233 const char *LinkingOutput) const {
Daniel Dunbarcc912342009-05-02 18:28:39 +00006234 ArgStringList CmdArgs;
6235
6236 // When building 32-bit code on DragonFly/pc64, we have to explicitly
6237 // instruct as in the base system to assemble 32-bit code.
Rafael Espindola35ca7d92012-10-07 04:44:33 +00006238 if (getToolChain().getArch() == llvm::Triple::x86)
Daniel Dunbarcc912342009-05-02 18:28:39 +00006239 CmdArgs.push_back("--32");
6240
6241 Args.AddAllArgValues(CmdArgs, options::OPT_Wa_COMMA,
6242 options::OPT_Xassembler);
6243
6244 CmdArgs.push_back("-o");
Daniel Dunbarb440f562010-08-02 02:38:21 +00006245 CmdArgs.push_back(Output.getFilename());
Daniel Dunbarcc912342009-05-02 18:28:39 +00006246
6247 for (InputInfoList::const_iterator
6248 it = Inputs.begin(), ie = Inputs.end(); it != ie; ++it) {
6249 const InputInfo &II = *it;
Daniel Dunbarb440f562010-08-02 02:38:21 +00006250 CmdArgs.push_back(II.getFilename());
Daniel Dunbarcc912342009-05-02 18:28:39 +00006251 }
6252
6253 const char *Exec =
Daniel Dunbar9c3ed5f2010-07-14 18:46:23 +00006254 Args.MakeArgString(getToolChain().GetProgramPath("as"));
Daniel Dunbar7fbaf532010-08-02 02:38:28 +00006255 C.addCommand(new Command(JA, *this, Exec, CmdArgs));
Daniel Dunbarcc912342009-05-02 18:28:39 +00006256}
6257
6258void dragonfly::Link::ConstructJob(Compilation &C, const JobAction &JA,
Daniel Dunbar7fbaf532010-08-02 02:38:28 +00006259 const InputInfo &Output,
6260 const InputInfoList &Inputs,
6261 const ArgList &Args,
6262 const char *LinkingOutput) const {
John McCall65b8da02013-04-11 22:55:55 +00006263 bool UseGCC47 = false;
Daniel Dunbar083edf72009-12-21 18:54:17 +00006264 const Driver &D = getToolChain().getDriver();
Daniel Dunbarcc912342009-05-02 18:28:39 +00006265 ArgStringList CmdArgs;
6266
John McCall65b8da02013-04-11 22:55:55 +00006267 if (llvm::sys::fs::exists("/usr/lib/gcc47", UseGCC47))
6268 UseGCC47 = false;
6269
Joerg Sonnenberger6165ab12011-03-21 13:51:29 +00006270 if (!D.SysRoot.empty())
6271 CmdArgs.push_back(Args.MakeArgString("--sysroot=" + D.SysRoot));
6272
John McCall65b8da02013-04-11 22:55:55 +00006273 CmdArgs.push_back("--eh-frame-hdr");
Daniel Dunbarcc912342009-05-02 18:28:39 +00006274 if (Args.hasArg(options::OPT_static)) {
6275 CmdArgs.push_back("-Bstatic");
6276 } else {
John McCall65b8da02013-04-11 22:55:55 +00006277 if (Args.hasArg(options::OPT_rdynamic))
6278 CmdArgs.push_back("-export-dynamic");
Daniel Dunbarcc912342009-05-02 18:28:39 +00006279 if (Args.hasArg(options::OPT_shared))
6280 CmdArgs.push_back("-Bshareable");
6281 else {
6282 CmdArgs.push_back("-dynamic-linker");
6283 CmdArgs.push_back("/usr/libexec/ld-elf.so.2");
6284 }
John McCall65b8da02013-04-11 22:55:55 +00006285 CmdArgs.push_back("--hash-style=both");
Daniel Dunbarcc912342009-05-02 18:28:39 +00006286 }
6287
6288 // When building 32-bit code on DragonFly/pc64, we have to explicitly
6289 // instruct ld in the base system to link 32-bit code.
Rafael Espindola35ca7d92012-10-07 04:44:33 +00006290 if (getToolChain().getArch() == llvm::Triple::x86) {
Daniel Dunbarcc912342009-05-02 18:28:39 +00006291 CmdArgs.push_back("-m");
6292 CmdArgs.push_back("elf_i386");
6293 }
6294
Daniel Dunbarb440f562010-08-02 02:38:21 +00006295 if (Output.isFilename()) {
Daniel Dunbarcc912342009-05-02 18:28:39 +00006296 CmdArgs.push_back("-o");
6297 CmdArgs.push_back(Output.getFilename());
6298 } else {
6299 assert(Output.isNothing() && "Invalid output.");
6300 }
6301
6302 if (!Args.hasArg(options::OPT_nostdlib) &&
6303 !Args.hasArg(options::OPT_nostartfiles)) {
6304 if (!Args.hasArg(options::OPT_shared)) {
John McCall65b8da02013-04-11 22:55:55 +00006305 if (Args.hasArg(options::OPT_pg))
6306 CmdArgs.push_back(Args.MakeArgString(
6307 getToolChain().GetFilePath("gcrt1.o")));
6308 else {
6309 if (Args.hasArg(options::OPT_pie))
6310 CmdArgs.push_back(Args.MakeArgString(
6311 getToolChain().GetFilePath("Scrt1.o")));
6312 else
6313 CmdArgs.push_back(Args.MakeArgString(
6314 getToolChain().GetFilePath("crt1.o")));
6315 }
Daniel Dunbarcc912342009-05-02 18:28:39 +00006316 }
John McCall65b8da02013-04-11 22:55:55 +00006317 CmdArgs.push_back(Args.MakeArgString(
6318 getToolChain().GetFilePath("crti.o")));
6319 if (Args.hasArg(options::OPT_shared) || Args.hasArg(options::OPT_pie))
6320 CmdArgs.push_back(Args.MakeArgString(
6321 getToolChain().GetFilePath("crtbeginS.o")));
6322 else
6323 CmdArgs.push_back(Args.MakeArgString(
6324 getToolChain().GetFilePath("crtbegin.o")));
Daniel Dunbarcc912342009-05-02 18:28:39 +00006325 }
6326
6327 Args.AddAllArgs(CmdArgs, options::OPT_L);
6328 Args.AddAllArgs(CmdArgs, options::OPT_T_Group);
6329 Args.AddAllArgs(CmdArgs, options::OPT_e);
6330
Daniel Dunbar54423b22010-09-17 00:24:54 +00006331 AddLinkerInputs(getToolChain(), Inputs, Args, CmdArgs);
Daniel Dunbarcc912342009-05-02 18:28:39 +00006332
6333 if (!Args.hasArg(options::OPT_nostdlib) &&
6334 !Args.hasArg(options::OPT_nodefaultlibs)) {
6335 // FIXME: GCC passes on -lgcc, -lgcc_pic and a whole lot of
6336 // rpaths
John McCall65b8da02013-04-11 22:55:55 +00006337 if (UseGCC47)
6338 CmdArgs.push_back("-L/usr/lib/gcc47");
6339 else
6340 CmdArgs.push_back("-L/usr/lib/gcc44");
Daniel Dunbarcc912342009-05-02 18:28:39 +00006341
6342 if (!Args.hasArg(options::OPT_static)) {
John McCall65b8da02013-04-11 22:55:55 +00006343 if (UseGCC47) {
6344 CmdArgs.push_back("-rpath");
6345 CmdArgs.push_back("/usr/lib/gcc47");
6346 } else {
6347 CmdArgs.push_back("-rpath");
6348 CmdArgs.push_back("/usr/lib/gcc44");
6349 }
Daniel Dunbarcc912342009-05-02 18:28:39 +00006350 }
6351
Rafael Espindola38360b32010-07-20 12:59:03 +00006352 if (D.CCCIsCXX) {
Daniel Dunbar3f7796f2010-09-17 01:20:05 +00006353 getToolChain().AddCXXStdlibLibArgs(Args, CmdArgs);
Rafael Espindola38360b32010-07-20 12:59:03 +00006354 CmdArgs.push_back("-lm");
6355 }
6356
Daniel Dunbarcc912342009-05-02 18:28:39 +00006357 if (Args.hasArg(options::OPT_pthread))
Mike Stump0a65b632009-10-31 20:11:46 +00006358 CmdArgs.push_back("-lpthread");
Daniel Dunbarcc912342009-05-02 18:28:39 +00006359
6360 if (!Args.hasArg(options::OPT_nolibc)) {
6361 CmdArgs.push_back("-lc");
6362 }
6363
John McCall65b8da02013-04-11 22:55:55 +00006364 if (UseGCC47) {
6365 if (Args.hasArg(options::OPT_static) ||
6366 Args.hasArg(options::OPT_static_libgcc)) {
6367 CmdArgs.push_back("-lgcc");
6368 CmdArgs.push_back("-lgcc_eh");
6369 } else {
6370 if (Args.hasArg(options::OPT_shared_libgcc)) {
6371 CmdArgs.push_back("-lgcc_pic");
6372 if (!Args.hasArg(options::OPT_shared))
6373 CmdArgs.push_back("-lgcc");
6374 } else {
6375 CmdArgs.push_back("-lgcc");
6376 CmdArgs.push_back("--as-needed");
6377 CmdArgs.push_back("-lgcc_pic");
6378 CmdArgs.push_back("--no-as-needed");
6379 }
6380 }
Daniel Dunbarcc912342009-05-02 18:28:39 +00006381 } else {
John McCall65b8da02013-04-11 22:55:55 +00006382 if (Args.hasArg(options::OPT_shared)) {
6383 CmdArgs.push_back("-lgcc_pic");
6384 } else {
6385 CmdArgs.push_back("-lgcc");
6386 }
Daniel Dunbarcc912342009-05-02 18:28:39 +00006387 }
6388 }
6389
6390 if (!Args.hasArg(options::OPT_nostdlib) &&
6391 !Args.hasArg(options::OPT_nostartfiles)) {
John McCall65b8da02013-04-11 22:55:55 +00006392 if (Args.hasArg(options::OPT_shared) || Args.hasArg(options::OPT_pie))
Chris Lattner3e2ee142010-07-07 16:01:42 +00006393 CmdArgs.push_back(Args.MakeArgString(
Daniel Dunbar9c3ed5f2010-07-14 18:46:23 +00006394 getToolChain().GetFilePath("crtendS.o")));
John McCall65b8da02013-04-11 22:55:55 +00006395 else
6396 CmdArgs.push_back(Args.MakeArgString(
6397 getToolChain().GetFilePath("crtend.o")));
Chris Lattner3e2ee142010-07-07 16:01:42 +00006398 CmdArgs.push_back(Args.MakeArgString(
John McCall65b8da02013-04-11 22:55:55 +00006399 getToolChain().GetFilePath("crtn.o")));
Daniel Dunbarcc912342009-05-02 18:28:39 +00006400 }
6401
Bill Wendling08760582011-06-27 19:15:03 +00006402 addProfileRT(getToolChain(), Args, CmdArgs, getToolChain().getTriple());
Nick Lewycky82fe5f42011-05-24 21:54:59 +00006403
Daniel Dunbarcc912342009-05-02 18:28:39 +00006404 const char *Exec =
Daniel Dunbar9c3ed5f2010-07-14 18:46:23 +00006405 Args.MakeArgString(getToolChain().GetProgramPath("ld"));
Daniel Dunbar7fbaf532010-08-02 02:38:28 +00006406 C.addCommand(new Command(JA, *this, Exec, CmdArgs));
Daniel Dunbarcc912342009-05-02 18:28:39 +00006407}
Michael J. Spencerb186bc32010-08-21 21:55:07 +00006408
6409void visualstudio::Link::ConstructJob(Compilation &C, const JobAction &JA,
6410 const InputInfo &Output,
6411 const InputInfoList &Inputs,
6412 const ArgList &Args,
6413 const char *LinkingOutput) const {
Michael J. Spencerb186bc32010-08-21 21:55:07 +00006414 ArgStringList CmdArgs;
6415
6416 if (Output.isFilename()) {
Daniel Dunbar2cc3f172010-09-17 00:45:02 +00006417 CmdArgs.push_back(Args.MakeArgString(std::string("-out:") +
6418 Output.getFilename()));
Michael J. Spencerb186bc32010-08-21 21:55:07 +00006419 } else {
6420 assert(Output.isNothing() && "Invalid output.");
6421 }
6422
6423 if (!Args.hasArg(options::OPT_nostdlib) &&
6424 !Args.hasArg(options::OPT_nostartfiles)) {
6425 CmdArgs.push_back("-defaultlib:libcmt");
6426 }
6427
6428 CmdArgs.push_back("-nologo");
6429
Michael J. Spencere2f49362012-06-18 16:56:04 +00006430 Args.AddAllArgValues(CmdArgs, options::OPT_l);
6431
6432 // Add filenames immediately.
6433 for (InputInfoList::const_iterator
6434 it = Inputs.begin(), ie = Inputs.end(); it != ie; ++it) {
6435 if (it->isFilename())
6436 CmdArgs.push_back(it->getFilename());
6437 }
Michael J. Spencerb186bc32010-08-21 21:55:07 +00006438
6439 const char *Exec =
Daniel Dunbar54423b22010-09-17 00:24:54 +00006440 Args.MakeArgString(getToolChain().GetProgramPath("link.exe"));
Michael J. Spencerb186bc32010-08-21 21:55:07 +00006441 C.addCommand(new Command(JA, *this, Exec, CmdArgs));
6442}