blob: b372b65c3152bedae98a45529ad18ca7820e5fc6 [file] [log] [blame]
Nick Lewyckye3365aa2010-09-23 23:48:20 +00001//===--- Tools.cpp - Tools Implementations --------------------------------===//
Daniel Dunbar47ac7d22009-03-18 06:00:36 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10#include "Tools.h"
Chandler Carruth55fc8732012-12-04 09:13:33 +000011#include "InputInfo.h"
Chandler Carruth55fc8732012-12-04 09:13:33 +000012#include "ToolChains.h"
Stephen Hines651f13c2014-04-23 16:59:28 -070013#include "clang/Basic/LangOptions.h"
Chandler Carruth55fc8732012-12-04 09:13:33 +000014#include "clang/Basic/ObjCRuntime.h"
Kevin Enderby02341792013-01-17 21:38:06 +000015#include "clang/Basic/Version.h"
Daniel Dunbar1d460332009-03-18 10:01:51 +000016#include "clang/Driver/Action.h"
Chandler Carruth55fc8732012-12-04 09:13:33 +000017#include "clang/Driver/Compilation.h"
Daniel Dunbaree848a72009-10-29 02:39:57 +000018#include "clang/Driver/Driver.h"
19#include "clang/Driver/DriverDiagnostic.h"
Daniel Dunbar871adcf2009-03-18 07:06:02 +000020#include "clang/Driver/Job.h"
Daniel Dunbar265e9ef2009-11-19 04:25:22 +000021#include "clang/Driver/Options.h"
Alexey Samsonov1b8f12d2013-08-19 09:14:21 +000022#include "clang/Driver/SanitizerArgs.h"
Daniel Dunbarb488c1d2009-03-18 08:07:30 +000023#include "clang/Driver/ToolChain.h"
Daniel Dunbar871adcf2009-03-18 07:06:02 +000024#include "clang/Driver/Util.h"
Daniel Dunbar88137642009-09-09 22:32:48 +000025#include "llvm/ADT/SmallString.h"
Hans Wennborgdc40bf92013-09-20 18:16:35 +000026#include "llvm/ADT/StringExtras.h"
Douglas Gregor55d3f7a2009-10-29 00:41:01 +000027#include "llvm/ADT/StringSwitch.h"
Daniel Dunbar5b750fe2009-09-09 22:32:34 +000028#include "llvm/ADT/Twine.h"
Reid Klecknerb1e25a12013-06-14 17:17:23 +000029#include "llvm/Option/Arg.h"
30#include "llvm/Option/ArgList.h"
31#include "llvm/Option/Option.h"
Stephen Hines651f13c2014-04-23 16:59:28 -070032#include "llvm/Support/Compression.h"
Chandler Carruth55fc8732012-12-04 09:13:33 +000033#include "llvm/Support/ErrorHandling.h"
Michael J. Spencer32bef4e2011-01-10 02:34:13 +000034#include "llvm/Support/FileSystem.h"
Daniel Dunbar02633b52009-03-26 16:23:12 +000035#include "llvm/Support/Format.h"
Michael J. Spencer03013fa2010-11-29 18:12:39 +000036#include "llvm/Support/Host.h"
Hans Wennborgdc40bf92013-09-20 18:16:35 +000037#include "llvm/Support/Path.h"
Michael J. Spencer03013fa2010-11-29 18:12:39 +000038#include "llvm/Support/Process.h"
Stephen Hines651f13c2014-04-23 16:59:28 -070039#include "llvm/Support/Program.h"
Chandler Carruth55fc8732012-12-04 09:13:33 +000040#include "llvm/Support/raw_ostream.h"
Daniel Dunbar871adcf2009-03-18 07:06:02 +000041
Daniel Dunbar47ac7d22009-03-18 06:00:36 +000042using namespace clang::driver;
43using namespace clang::driver::tools;
Chris Lattner5f9e2722011-07-23 10:55:15 +000044using namespace clang;
Reid Klecknerb1e25a12013-06-14 17:17:23 +000045using namespace llvm::opt;
Daniel Dunbar47ac7d22009-03-18 06:00:36 +000046
Stephen Hines651f13c2014-04-23 16:59:28 -070047static void addAssemblerKPIC(const ArgList &Args, ArgStringList &CmdArgs) {
48 Arg *LastPICArg = Args.getLastArg(options::OPT_fPIC, options::OPT_fno_PIC,
49 options::OPT_fpic, options::OPT_fno_pic,
50 options::OPT_fPIE, options::OPT_fno_PIE,
51 options::OPT_fpie, options::OPT_fno_pie);
52 if (!LastPICArg)
53 return;
54 if (LastPICArg->getOption().matches(options::OPT_fPIC) ||
55 LastPICArg->getOption().matches(options::OPT_fpic) ||
56 LastPICArg->getOption().matches(options::OPT_fPIE) ||
57 LastPICArg->getOption().matches(options::OPT_fpie)) {
58 CmdArgs.push_back("-KPIC");
59 }
60}
61
Daniel Dunbar88a3d6c2009-09-10 01:21:05 +000062/// CheckPreprocessingOptions - Perform some validation of preprocessing
63/// arguments that is shared with gcc.
64static void CheckPreprocessingOptions(const Driver &D, const ArgList &Args) {
Stephen Hinesc568f1e2014-07-21 00:47:37 -070065 if (Arg *A = Args.getLastArg(options::OPT_C, options::OPT_CC)) {
66 if (!Args.hasArg(options::OPT_E) && !Args.hasArg(options::OPT__SLASH_P) &&
67 !Args.hasArg(options::OPT__SLASH_EP) && !D.CCCIsCPP()) {
Chris Lattner5f9e2722011-07-23 10:55:15 +000068 D.Diag(diag::err_drv_argument_only_allowed_with)
Stephen Hinesc568f1e2014-07-21 00:47:37 -070069 << A->getBaseArg().getAsString(Args)
70 << (D.IsCLMode() ? "/E, /P or /EP" : "-E");
71 }
72 }
Daniel Dunbar88a3d6c2009-09-10 01:21:05 +000073}
74
Daniel Dunbare2fd6642009-09-10 01:21:12 +000075/// CheckCodeGenerationOptions - Perform some validation of code generation
76/// arguments that is shared with gcc.
77static void CheckCodeGenerationOptions(const Driver &D, const ArgList &Args) {
78 // In gcc, only ARM checks this, but it seems reasonable to check universally.
79 if (Args.hasArg(options::OPT_static))
80 if (const Arg *A = Args.getLastArg(options::OPT_dynamic,
81 options::OPT_mdynamic_no_pic))
Chris Lattner5f9e2722011-07-23 10:55:15 +000082 D.Diag(diag::err_drv_argument_not_allowed_with)
Daniel Dunbare2fd6642009-09-10 01:21:12 +000083 << A->getAsString(Args) << "-static";
84}
85
Stephen Hines176edba2014-12-01 14:53:08 -080086// Add backslashes to escape spaces and other backslashes.
87// This is used for the space-separated argument list specified with
88// the -dwarf-debug-flags option.
89static void EscapeSpacesAndBackslashes(const char *Arg,
90 SmallVectorImpl<char> &Res) {
91 for ( ; *Arg; ++Arg) {
92 switch (*Arg) {
93 default: break;
94 case ' ':
95 case '\\':
96 Res.push_back('\\');
97 break;
98 }
99 Res.push_back(*Arg);
100 }
101}
102
Chris Lattner3edbeb72010-03-29 17:55:58 +0000103// Quote target names for inclusion in GNU Make dependency files.
104// Only the characters '$', '#', ' ', '\t' are quoted.
Chris Lattner5f9e2722011-07-23 10:55:15 +0000105static void QuoteTarget(StringRef Target,
106 SmallVectorImpl<char> &Res) {
Chris Lattner3edbeb72010-03-29 17:55:58 +0000107 for (unsigned i = 0, e = Target.size(); i != e; ++i) {
108 switch (Target[i]) {
109 case ' ':
110 case '\t':
111 // Escape the preceding backslashes
112 for (int j = i - 1; j >= 0 && Target[j] == '\\'; --j)
113 Res.push_back('\\');
114
115 // Escape the space/tab
116 Res.push_back('\\');
117 break;
118 case '$':
119 Res.push_back('$');
120 break;
121 case '#':
122 Res.push_back('\\');
123 break;
124 default:
125 break;
126 }
127
128 Res.push_back(Target[i]);
129 }
130}
131
Bill Wendling3d717152012-03-12 22:10:06 +0000132static void addDirectoryList(const ArgList &Args,
Bill Wendlingbdb8f3c2012-03-12 21:22:35 +0000133 ArgStringList &CmdArgs,
134 const char *ArgName,
Bill Wendling3d717152012-03-12 22:10:06 +0000135 const char *EnvVar) {
136 const char *DirList = ::getenv(EnvVar);
Chad Rosier89aa2ce2012-10-30 21:42:09 +0000137 bool CombinedArg = false;
138
Bill Wendlingbdb8f3c2012-03-12 21:22:35 +0000139 if (!DirList)
140 return; // Nothing to do.
141
Chad Rosier89aa2ce2012-10-30 21:42:09 +0000142 StringRef Name(ArgName);
143 if (Name.equals("-I") || Name.equals("-L"))
144 CombinedArg = true;
145
Bill Wendlingbdb8f3c2012-03-12 21:22:35 +0000146 StringRef Dirs(DirList);
147 if (Dirs.empty()) // Empty string should not add '.'.
148 return;
149
150 StringRef::size_type Delim;
Rafael Espindola8db7ec02013-06-25 14:29:51 +0000151 while ((Delim = Dirs.find(llvm::sys::EnvPathSeparator)) != StringRef::npos) {
Bill Wendlingbdb8f3c2012-03-12 21:22:35 +0000152 if (Delim == 0) { // Leading colon.
Chad Rosier89aa2ce2012-10-30 21:42:09 +0000153 if (CombinedArg) {
154 CmdArgs.push_back(Args.MakeArgString(std::string(ArgName) + "."));
155 } else {
156 CmdArgs.push_back(ArgName);
157 CmdArgs.push_back(".");
158 }
Bill Wendlingbdb8f3c2012-03-12 21:22:35 +0000159 } else {
Chad Rosier89aa2ce2012-10-30 21:42:09 +0000160 if (CombinedArg) {
161 CmdArgs.push_back(Args.MakeArgString(std::string(ArgName) + Dirs.substr(0, Delim)));
162 } else {
163 CmdArgs.push_back(ArgName);
164 CmdArgs.push_back(Args.MakeArgString(Dirs.substr(0, Delim)));
165 }
Bill Wendlingbdb8f3c2012-03-12 21:22:35 +0000166 }
Nico Weber09c5c392012-03-19 15:00:03 +0000167 Dirs = Dirs.substr(Delim + 1);
Bill Wendlingbdb8f3c2012-03-12 21:22:35 +0000168 }
169
170 if (Dirs.empty()) { // Trailing colon.
Chad Rosier89aa2ce2012-10-30 21:42:09 +0000171 if (CombinedArg) {
172 CmdArgs.push_back(Args.MakeArgString(std::string(ArgName) + "."));
173 } else {
174 CmdArgs.push_back(ArgName);
175 CmdArgs.push_back(".");
176 }
Bill Wendlingbdb8f3c2012-03-12 21:22:35 +0000177 } else { // Add the last path.
Chad Rosier89aa2ce2012-10-30 21:42:09 +0000178 if (CombinedArg) {
179 CmdArgs.push_back(Args.MakeArgString(std::string(ArgName) + Dirs));
180 } else {
181 CmdArgs.push_back(ArgName);
182 CmdArgs.push_back(Args.MakeArgString(Dirs));
183 }
Bill Wendlingbdb8f3c2012-03-12 21:22:35 +0000184 }
185}
186
Daniel Dunbar2008fee2010-09-17 00:24:54 +0000187static void AddLinkerInputs(const ToolChain &TC,
188 const InputInfoList &Inputs, const ArgList &Args,
189 ArgStringList &CmdArgs) {
190 const Driver &D = TC.getDriver();
191
Daniel Dunbar8ac38d72011-02-19 05:33:51 +0000192 // Add extra linker input arguments which are not treated as inputs
193 // (constructed via -Xarch_).
194 Args.AddAllArgValues(CmdArgs, options::OPT_Zlinker_input);
195
Stephen Hinesc568f1e2014-07-21 00:47:37 -0700196 for (const auto &II : Inputs) {
Daniel Dunbar2008fee2010-09-17 00:24:54 +0000197 if (!TC.HasNativeLLVMSupport()) {
198 // Don't try to pass LLVM inputs unless we have native support.
199 if (II.getType() == types::TY_LLVM_IR ||
200 II.getType() == types::TY_LTO_IR ||
201 II.getType() == types::TY_LLVM_BC ||
202 II.getType() == types::TY_LTO_BC)
Chris Lattner5f9e2722011-07-23 10:55:15 +0000203 D.Diag(diag::err_drv_no_linker_llvm_support)
Daniel Dunbar2008fee2010-09-17 00:24:54 +0000204 << TC.getTripleString();
205 }
206
Daniel Dunbare5a37f42010-09-17 00:45:02 +0000207 // Add filenames immediately.
208 if (II.isFilename()) {
Daniel Dunbar2008fee2010-09-17 00:24:54 +0000209 CmdArgs.push_back(II.getFilename());
Daniel Dunbare5a37f42010-09-17 00:45:02 +0000210 continue;
211 }
212
213 // Otherwise, this is a linker input argument.
214 const Arg &A = II.getInputArg();
215
216 // Handle reserved library options.
Stephen Hinesc568f1e2014-07-21 00:47:37 -0700217 if (A.getOption().matches(options::OPT_Z_reserved_lib_stdcxx))
Daniel Dunbar132e35d2010-09-17 01:20:05 +0000218 TC.AddCXXStdlibLibArgs(Args, CmdArgs);
Stephen Hinesc568f1e2014-07-21 00:47:37 -0700219 else if (A.getOption().matches(options::OPT_Z_reserved_lib_cckext))
Shantonu Sen7433fed2010-09-17 18:39:08 +0000220 TC.AddCCKextLibArgs(Args, CmdArgs);
Stephen Hines176edba2014-12-01 14:53:08 -0800221 else if (A.getOption().matches(options::OPT_z)) {
222 // Pass -z prefix for gcc linker compatibility.
223 A.claim();
224 A.render(Args, CmdArgs);
225 } else {
226 A.renderAsInput(Args, CmdArgs);
227 }
Daniel Dunbar2008fee2010-09-17 00:24:54 +0000228 }
Bill Wendlingbdb8f3c2012-03-12 21:22:35 +0000229
230 // LIBRARY_PATH - included following the user specified library paths.
Stephen Hines651f13c2014-04-23 16:59:28 -0700231 // and only supported on native toolchains.
232 if (!TC.isCrossCompiling())
233 addDirectoryList(Args, CmdArgs, "-L", "LIBRARY_PATH");
Daniel Dunbar2008fee2010-09-17 00:24:54 +0000234}
235
John McCallf85e1932011-06-15 23:02:42 +0000236/// \brief Determine whether Objective-C automated reference counting is
237/// enabled.
238static bool isObjCAutoRefCount(const ArgList &Args) {
239 return Args.hasFlag(options::OPT_fobjc_arc, options::OPT_fno_objc_arc, false);
240}
241
Ted Kremenekebcb57a2012-03-06 20:05:56 +0000242/// \brief Determine whether we are linking the ObjC runtime.
243static bool isObjCRuntimeLinked(const ArgList &Args) {
Bob Wilsona7635f12012-08-07 19:58:00 +0000244 if (isObjCAutoRefCount(Args)) {
245 Args.ClaimAllArgs(options::OPT_fobjc_link_runtime);
Ted Kremenekebcb57a2012-03-06 20:05:56 +0000246 return true;
Bob Wilsona7635f12012-08-07 19:58:00 +0000247 }
Ted Kremenekebcb57a2012-03-06 20:05:56 +0000248 return Args.hasArg(options::OPT_fobjc_link_runtime);
249}
250
Michael J. Spencer91e06da2012-10-19 22:37:06 +0000251static bool forwardToGCC(const Option &O) {
Reid Kleckner4cd90df2013-06-19 15:09:06 +0000252 // Don't forward inputs from the original command line. They are added from
253 // InputInfoList.
Richard Smithe40bc4b2013-06-20 01:33:59 +0000254 return O.getKind() != Option::InputClass &&
Michael J. Spencer91e06da2012-10-19 22:37:06 +0000255 !O.hasFlag(options::DriverOption) &&
256 !O.hasFlag(options::LinkerInput);
257}
258
Peter Collingbourne54db68b2011-11-06 00:40:05 +0000259void Clang::AddPreprocessingOptions(Compilation &C,
Chad Rosier9d718632013-01-24 19:14:47 +0000260 const JobAction &JA,
Peter Collingbourne54db68b2011-11-06 00:40:05 +0000261 const Driver &D,
Douglas Gregordf91ef32009-04-18 00:34:01 +0000262 const ArgList &Args,
Daniel Dunbarc21c4852009-04-08 23:54:23 +0000263 ArgStringList &CmdArgs,
264 const InputInfo &Output,
265 const InputInfoList &Inputs) const {
Daniel Dunbarc21c4852009-04-08 23:54:23 +0000266 Arg *A;
Daniel Dunbar3a183d32009-06-08 21:48:20 +0000267
Daniel Dunbar88a3d6c2009-09-10 01:21:05 +0000268 CheckPreprocessingOptions(D, Args);
269
270 Args.AddLastArg(CmdArgs, options::OPT_C);
271 Args.AddLastArg(CmdArgs, options::OPT_CC);
Daniel Dunbar3a183d32009-06-08 21:48:20 +0000272
273 // Handle dependency file generation.
Daniel Dunbar9eb93b02010-12-08 21:33:40 +0000274 if ((A = Args.getLastArg(options::OPT_M, options::OPT_MM)) ||
Daniel Dunbarc21c4852009-04-08 23:54:23 +0000275 (A = Args.getLastArg(options::OPT_MD)) ||
276 (A = Args.getLastArg(options::OPT_MMD))) {
277 // Determine the output location.
278 const char *DepFile;
Benjamin Kramer99c72082012-09-26 19:01:49 +0000279 if (Arg *MF = Args.getLastArg(options::OPT_MF)) {
Richard Smith1d489cf2012-11-01 04:30:05 +0000280 DepFile = MF->getValue();
Chad Rosier9d718632013-01-24 19:14:47 +0000281 C.addFailureResultFile(DepFile, &JA);
Benjamin Kramer99c72082012-09-26 19:01:49 +0000282 } else if (Output.getType() == types::TY_Dependencies) {
283 DepFile = Output.getFilename();
Daniel Dunbarb827a052009-11-19 03:26:40 +0000284 } else if (A->getOption().matches(options::OPT_M) ||
285 A->getOption().matches(options::OPT_MM)) {
Daniel Dunbarc21c4852009-04-08 23:54:23 +0000286 DepFile = "-";
287 } else {
Bob Wilson66b8a662012-11-23 06:14:39 +0000288 DepFile = getDependencyFileName(Args, Inputs);
Chad Rosier9d718632013-01-24 19:14:47 +0000289 C.addFailureResultFile(DepFile, &JA);
Daniel Dunbarc21c4852009-04-08 23:54:23 +0000290 }
291 CmdArgs.push_back("-dependency-file");
292 CmdArgs.push_back(DepFile);
293
Chris Lattner3edbeb72010-03-29 17:55:58 +0000294 // Add a default target if one wasn't specified.
Daniel Dunbarc21c4852009-04-08 23:54:23 +0000295 if (!Args.hasArg(options::OPT_MT) && !Args.hasArg(options::OPT_MQ)) {
296 const char *DepTarget;
297
298 // If user provided -o, that is the dependency target, except
299 // when we are only generating a dependency file.
300 Arg *OutputOpt = Args.getLastArg(options::OPT_o);
301 if (OutputOpt && Output.getType() != types::TY_Dependencies) {
Richard Smith1d489cf2012-11-01 04:30:05 +0000302 DepTarget = OutputOpt->getValue();
Daniel Dunbarc21c4852009-04-08 23:54:23 +0000303 } else {
304 // Otherwise derive from the base input.
305 //
306 // FIXME: This should use the computed output file location.
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +0000307 SmallString<128> P(Inputs[0].getBaseInput());
Michael J. Spencer472ccff2010-12-18 00:19:12 +0000308 llvm::sys::path::replace_extension(P, "o");
309 DepTarget = Args.MakeArgString(llvm::sys::path::filename(P));
Daniel Dunbarc21c4852009-04-08 23:54:23 +0000310 }
311
312 CmdArgs.push_back("-MT");
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +0000313 SmallString<128> Quoted;
Chris Lattner3edbeb72010-03-29 17:55:58 +0000314 QuoteTarget(DepTarget, Quoted);
315 CmdArgs.push_back(Args.MakeArgString(Quoted));
Daniel Dunbarc21c4852009-04-08 23:54:23 +0000316 }
317
Daniel Dunbarb827a052009-11-19 03:26:40 +0000318 if (A->getOption().matches(options::OPT_M) ||
319 A->getOption().matches(options::OPT_MD))
Daniel Dunbarc21c4852009-04-08 23:54:23 +0000320 CmdArgs.push_back("-sys-header-deps");
Stephen Hines651f13c2014-04-23 16:59:28 -0700321
322 if (isa<PrecompileJobAction>(JA))
323 CmdArgs.push_back("-module-file-deps");
Daniel Dunbarc21c4852009-04-08 23:54:23 +0000324 }
325
Peter Collingbournebb527862011-07-12 19:35:15 +0000326 if (Args.hasArg(options::OPT_MG)) {
327 if (!A || A->getOption().matches(options::OPT_MD) ||
328 A->getOption().matches(options::OPT_MMD))
Chris Lattner5f9e2722011-07-23 10:55:15 +0000329 D.Diag(diag::err_drv_mg_requires_m_or_mm);
Peter Collingbournebb527862011-07-12 19:35:15 +0000330 CmdArgs.push_back("-MG");
331 }
332
Daniel Dunbarc21c4852009-04-08 23:54:23 +0000333 Args.AddLastArg(CmdArgs, options::OPT_MP);
Chris Lattner3edbeb72010-03-29 17:55:58 +0000334
335 // Convert all -MQ <target> args to -MT <quoted target>
336 for (arg_iterator it = Args.filtered_begin(options::OPT_MT,
337 options::OPT_MQ),
338 ie = Args.filtered_end(); it != ie; ++it) {
Daniel Dunbar7e4953e2010-06-11 22:00:13 +0000339 const Arg *A = *it;
340 A->claim();
Chris Lattner3edbeb72010-03-29 17:55:58 +0000341
Daniel Dunbar7e4953e2010-06-11 22:00:13 +0000342 if (A->getOption().matches(options::OPT_MQ)) {
Chris Lattner3edbeb72010-03-29 17:55:58 +0000343 CmdArgs.push_back("-MT");
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +0000344 SmallString<128> Quoted;
Richard Smith1d489cf2012-11-01 04:30:05 +0000345 QuoteTarget(A->getValue(), Quoted);
Chris Lattner3edbeb72010-03-29 17:55:58 +0000346 CmdArgs.push_back(Args.MakeArgString(Quoted));
347
348 // -MT flag - no change
349 } else {
Daniel Dunbar7e4953e2010-06-11 22:00:13 +0000350 A->render(Args, CmdArgs);
Chris Lattner3edbeb72010-03-29 17:55:58 +0000351 }
352 }
Daniel Dunbarc21c4852009-04-08 23:54:23 +0000353
Douglas Gregordf91ef32009-04-18 00:34:01 +0000354 // Add -i* options, and automatically translate to
355 // -include-pch/-include-pth for transparent PCH support. It's
356 // wonky, but we include looking for .gch so we can support seamless
357 // replacement into a build system already set up to be generating
358 // .gch files.
Argyrios Kyrtzidis990142a2010-09-30 16:53:47 +0000359 bool RenderedImplicitInclude = false;
Daniel Dunbarcdd96862009-11-25 11:53:23 +0000360 for (arg_iterator it = Args.filtered_begin(options::OPT_clang_i_Group),
361 ie = Args.filtered_end(); it != ie; ++it) {
362 const Arg *A = it;
Daniel Dunbarc21c4852009-04-08 23:54:23 +0000363
364 if (A->getOption().matches(options::OPT_include)) {
Argyrios Kyrtzidis990142a2010-09-30 16:53:47 +0000365 bool IsFirstImplicitInclude = !RenderedImplicitInclude;
366 RenderedImplicitInclude = true;
367
Argyrios Kyrtzidise5c35372010-08-11 23:27:58 +0000368 // Use PCH if the user requested it.
Daniel Dunbar0ebd9322009-10-15 20:02:44 +0000369 bool UsePCH = D.CCCUsePCH;
Daniel Dunbar0ebd9322009-10-15 20:02:44 +0000370
Daniel Dunbarc21c4852009-04-08 23:54:23 +0000371 bool FoundPTH = false;
Douglas Gregordf91ef32009-04-18 00:34:01 +0000372 bool FoundPCH = false;
Rafael Espindolaf8edb992013-06-25 15:03:59 +0000373 SmallString<128> P(A->getValue());
374 // We want the files to have a name like foo.h.pch. Add a dummy extension
375 // so that replace_extension does the right thing.
376 P += ".dummy";
Daniel Dunbar0ebd9322009-10-15 20:02:44 +0000377 if (UsePCH) {
Rafael Espindolaf8edb992013-06-25 15:03:59 +0000378 llvm::sys::path::replace_extension(P, "pch");
Rafael Espindola829e88d2013-06-25 14:48:00 +0000379 if (llvm::sys::fs::exists(P.str()))
Douglas Gregordf91ef32009-04-18 00:34:01 +0000380 FoundPCH = true;
Daniel Dunbarc21c4852009-04-08 23:54:23 +0000381 }
382
Douglas Gregordf91ef32009-04-18 00:34:01 +0000383 if (!FoundPCH) {
Rafael Espindolaf8edb992013-06-25 15:03:59 +0000384 llvm::sys::path::replace_extension(P, "pth");
Rafael Espindola829e88d2013-06-25 14:48:00 +0000385 if (llvm::sys::fs::exists(P.str()))
Douglas Gregordf91ef32009-04-18 00:34:01 +0000386 FoundPTH = true;
Mike Stump1eb44332009-09-09 15:08:12 +0000387 }
388
Douglas Gregordf91ef32009-04-18 00:34:01 +0000389 if (!FoundPCH && !FoundPTH) {
Rafael Espindolaf8edb992013-06-25 15:03:59 +0000390 llvm::sys::path::replace_extension(P, "gch");
Rafael Espindola829e88d2013-06-25 14:48:00 +0000391 if (llvm::sys::fs::exists(P.str())) {
Daniel Dunbar0ebd9322009-10-15 20:02:44 +0000392 FoundPCH = UsePCH;
393 FoundPTH = !UsePCH;
Douglas Gregordf91ef32009-04-18 00:34:01 +0000394 }
Douglas Gregordf91ef32009-04-18 00:34:01 +0000395 }
396
397 if (FoundPCH || FoundPTH) {
Argyrios Kyrtzidis990142a2010-09-30 16:53:47 +0000398 if (IsFirstImplicitInclude) {
399 A->claim();
400 if (UsePCH)
401 CmdArgs.push_back("-include-pch");
402 else
403 CmdArgs.push_back("-include-pth");
404 CmdArgs.push_back(Args.MakeArgString(P.str()));
405 continue;
406 } else {
407 // Ignore the PCH if not first on command line and emit warning.
Chris Lattner5f9e2722011-07-23 10:55:15 +0000408 D.Diag(diag::warn_drv_pch_not_first_include)
Argyrios Kyrtzidis990142a2010-09-30 16:53:47 +0000409 << P.str() << A->getAsString(Args);
410 }
Daniel Dunbarc21c4852009-04-08 23:54:23 +0000411 }
412 }
413
414 // Not translated, render as usual.
415 A->claim();
416 A->render(Args, CmdArgs);
417 }
418
419 Args.AddAllArgs(CmdArgs, options::OPT_D, options::OPT_U);
Douglas Gregor65e02fa2011-07-28 04:45:53 +0000420 Args.AddAllArgs(CmdArgs, options::OPT_I_Group, options::OPT_F,
421 options::OPT_index_header_map);
Daniel Dunbarc21c4852009-04-08 23:54:23 +0000422
423 // Add -Wp, and -Xassembler if using the preprocessor.
424
425 // FIXME: There is a very unfortunate problem here, some troubled
426 // souls abuse -Wp, to pass preprocessor options in gcc syntax. To
427 // really support that we would have to parse and then translate
428 // those options. :(
429 Args.AddAllArgValues(CmdArgs, options::OPT_Wp_COMMA,
430 options::OPT_Xpreprocessor);
Daniel Dunbar607d7f62009-10-29 01:53:44 +0000431
432 // -I- is a deprecated GCC feature, reject it.
433 if (Arg *A = Args.getLastArg(options::OPT_I_))
Chris Lattner5f9e2722011-07-23 10:55:15 +0000434 D.Diag(diag::err_drv_I_dash_not_supported) << A->getAsString(Args);
Chandler Carruthfeee58c2010-10-20 07:00:47 +0000435
436 // If we have a --sysroot, and don't have an explicit -isysroot flag, add an
437 // -isysroot to the CC1 invocation.
Sebastian Pop4762a2d2012-04-16 04:16:43 +0000438 StringRef sysroot = C.getSysRoot();
439 if (sysroot != "") {
Chandler Carruthfeee58c2010-10-20 07:00:47 +0000440 if (!Args.hasArg(options::OPT_isysroot)) {
441 CmdArgs.push_back("-isysroot");
Sebastian Pop4762a2d2012-04-16 04:16:43 +0000442 CmdArgs.push_back(C.getArgs().MakeArgString(sysroot));
Chandler Carruthfeee58c2010-10-20 07:00:47 +0000443 }
444 }
Douglas Gregor2a060852013-02-07 00:21:12 +0000445
Benjamin Kramer47adebe2011-09-22 21:41:16 +0000446 // Parse additional include paths from environment variables.
Chandler Carruthb5870e72011-11-04 07:12:58 +0000447 // FIXME: We should probably sink the logic for handling these from the
448 // frontend into the driver. It will allow deleting 4 otherwise unused flags.
Benjamin Kramer47adebe2011-09-22 21:41:16 +0000449 // CPATH - included following the user specified includes (but prior to
450 // builtin and standard includes).
Bill Wendling3d717152012-03-12 22:10:06 +0000451 addDirectoryList(Args, CmdArgs, "-I", "CPATH");
Benjamin Kramer47adebe2011-09-22 21:41:16 +0000452 // C_INCLUDE_PATH - system includes enabled when compiling C.
Bill Wendling3d717152012-03-12 22:10:06 +0000453 addDirectoryList(Args, CmdArgs, "-c-isystem", "C_INCLUDE_PATH");
Benjamin Kramer47adebe2011-09-22 21:41:16 +0000454 // CPLUS_INCLUDE_PATH - system includes enabled when compiling C++.
Bill Wendling3d717152012-03-12 22:10:06 +0000455 addDirectoryList(Args, CmdArgs, "-cxx-isystem", "CPLUS_INCLUDE_PATH");
Benjamin Kramer47adebe2011-09-22 21:41:16 +0000456 // OBJC_INCLUDE_PATH - system includes enabled when compiling ObjC.
Bill Wendling3d717152012-03-12 22:10:06 +0000457 addDirectoryList(Args, CmdArgs, "-objc-isystem", "OBJC_INCLUDE_PATH");
Benjamin Kramer47adebe2011-09-22 21:41:16 +0000458 // OBJCPLUS_INCLUDE_PATH - system includes enabled when compiling ObjC++.
Bill Wendling3d717152012-03-12 22:10:06 +0000459 addDirectoryList(Args, CmdArgs, "-objcxx-isystem", "OBJCPLUS_INCLUDE_PATH");
Chandler Carruth88491fc2011-11-04 07:12:53 +0000460
Chandler Carruth88491fc2011-11-04 07:12:53 +0000461 // Add C++ include arguments, if needed.
Chandler Carrutha4614422011-11-04 07:43:33 +0000462 if (types::isCXX(Inputs[0].getType()))
Chandler Carruth7ffa0322011-11-04 07:34:47 +0000463 getToolChain().AddClangCXXStdlibIncludeArgs(Args, CmdArgs);
Chandler Carruth7d7e9f92011-11-05 20:17:13 +0000464
465 // Add system include arguments.
466 getToolChain().AddClangSystemIncludeArgs(Args, CmdArgs);
Daniel Dunbarc21c4852009-04-08 23:54:23 +0000467}
468
Daniel Dunbar1f95e652009-11-17 06:37:03 +0000469// FIXME: Move to target hook.
470static bool isSignedCharDefault(const llvm::Triple &Triple) {
471 switch (Triple.getArch()) {
472 default:
473 return true;
474
Tim Northoverc264e162013-01-31 12:13:10 +0000475 case llvm::Triple::aarch64:
Stephen Hines651f13c2014-04-23 16:59:28 -0700476 case llvm::Triple::aarch64_be:
Jim Grosbach5b4e7b12011-05-24 15:40:46 +0000477 case llvm::Triple::arm:
Stephen Hines651f13c2014-04-23 16:59:28 -0700478 case llvm::Triple::armeb:
Stephen Hines176edba2014-12-01 14:53:08 -0800479 case llvm::Triple::thumb:
480 case llvm::Triple::thumbeb:
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700481 if (Triple.isOSDarwin() || Triple.isOSWindows())
482 return true;
483 return false;
484
Daniel Dunbar1f95e652009-11-17 06:37:03 +0000485 case llvm::Triple::ppc:
486 case llvm::Triple::ppc64:
Bob Wilson905c45f2011-10-14 05:03:44 +0000487 if (Triple.isOSDarwin())
Daniel Dunbar1f95e652009-11-17 06:37:03 +0000488 return true;
489 return false;
Ulrich Weigandb8409212013-05-06 16:26:41 +0000490
Bill Schmidtea7fb0c2013-07-26 01:36:11 +0000491 case llvm::Triple::ppc64le:
Ulrich Weigandb8409212013-05-06 16:26:41 +0000492 case llvm::Triple::systemz:
Robert Lytton5f15f4d2013-08-13 09:43:10 +0000493 case llvm::Triple::xcore:
Ulrich Weigandb8409212013-05-06 16:26:41 +0000494 return false;
Daniel Dunbar1f95e652009-11-17 06:37:03 +0000495 }
496}
497
Robert Lytton5f15f4d2013-08-13 09:43:10 +0000498static bool isNoCommonDefault(const llvm::Triple &Triple) {
499 switch (Triple.getArch()) {
500 default:
501 return false;
502
503 case llvm::Triple::xcore:
504 return true;
505 }
506}
507
Silviu Baranga1db2e272013-10-21 10:54:53 +0000508// Handle -mhwdiv=.
509static void getARMHWDivFeatures(const Driver &D, const Arg *A,
510 const ArgList &Args,
511 std::vector<const char *> &Features) {
512 StringRef HWDiv = A->getValue();
513 if (HWDiv == "arm") {
514 Features.push_back("+hwdiv-arm");
515 Features.push_back("-hwdiv");
516 } else if (HWDiv == "thumb") {
517 Features.push_back("-hwdiv-arm");
518 Features.push_back("+hwdiv");
519 } else if (HWDiv == "arm,thumb" || HWDiv == "thumb,arm") {
520 Features.push_back("+hwdiv-arm");
521 Features.push_back("+hwdiv");
522 } else if (HWDiv == "none") {
523 Features.push_back("-hwdiv-arm");
524 Features.push_back("-hwdiv");
525 } else
526 D.Diag(diag::err_drv_clang_unsupported) << A->getAsString(Args);
527}
Stephen Hines176edba2014-12-01 14:53:08 -0800528
Amara Emersonfe7ed042013-10-01 10:20:54 +0000529// Handle -mfpu=.
530//
531// FIXME: Centralize feature selection, defaulting shouldn't be also in the
532// frontend target.
533static void getARMFPUFeatures(const Driver &D, const Arg *A,
534 const ArgList &Args,
535 std::vector<const char *> &Features) {
Richard Smith1d489cf2012-11-01 04:30:05 +0000536 StringRef FPU = A->getValue();
Chad Rosier99317272012-04-04 20:51:35 +0000537
538 // Set the target features based on the FPU.
539 if (FPU == "fpa" || FPU == "fpe2" || FPU == "fpe3" || FPU == "maverick") {
540 // Disable any default FPU support.
Rafael Espindola146dbbf2013-08-21 16:39:20 +0000541 Features.push_back("-vfp2");
542 Features.push_back("-vfp3");
543 Features.push_back("-neon");
Stephen Hines651f13c2014-04-23 16:59:28 -0700544 } else if (FPU == "vfp") {
545 Features.push_back("+vfp2");
546 Features.push_back("-neon");
Chad Rosier99317272012-04-04 20:51:35 +0000547 } else if (FPU == "vfp3-d16" || FPU == "vfpv3-d16") {
Rafael Espindola146dbbf2013-08-21 16:39:20 +0000548 Features.push_back("+vfp3");
549 Features.push_back("+d16");
550 Features.push_back("-neon");
Chad Rosier99317272012-04-04 20:51:35 +0000551 } else if (FPU == "vfp3" || FPU == "vfpv3") {
Rafael Espindola146dbbf2013-08-21 16:39:20 +0000552 Features.push_back("+vfp3");
553 Features.push_back("-neon");
Stephen Hines651f13c2014-04-23 16:59:28 -0700554 } else if (FPU == "vfp4-d16" || FPU == "vfpv4-d16") {
555 Features.push_back("+vfp4");
556 Features.push_back("+d16");
557 Features.push_back("-neon");
558 } else if (FPU == "vfp4" || FPU == "vfpv4") {
559 Features.push_back("+vfp4");
560 Features.push_back("-neon");
561 } else if (FPU == "fp4-sp-d16" || FPU == "fpv4-sp-d16") {
562 Features.push_back("+vfp4");
563 Features.push_back("+d16");
564 Features.push_back("+fp-only-sp");
565 Features.push_back("-neon");
Stephen Hines176edba2014-12-01 14:53:08 -0800566 } else if (FPU == "fp5-sp-d16" || FPU == "fpv5-sp-d16") {
567 Features.push_back("+fp-armv8");
568 Features.push_back("+fp-only-sp");
569 Features.push_back("+d16");
570 Features.push_back("-neon");
571 Features.push_back("-crypto");
572 } else if (FPU == "fp5-dp-d16" || FPU == "fpv5-dp-d16" ||
573 FPU == "fp5-d16" || FPU == "fpv5-d16") {
574 Features.push_back("+fp-armv8");
575 Features.push_back("+d16");
576 Features.push_back("-neon");
577 Features.push_back("-crypto");
Joey Goulycbed3bf2013-06-27 13:19:54 +0000578 } else if (FPU == "fp-armv8") {
Joey Gouly2b33b7e2013-09-13 13:48:33 +0000579 Features.push_back("+fp-armv8");
Bernard Ogdenf779e652013-10-24 18:32:51 +0000580 Features.push_back("-neon");
581 Features.push_back("-crypto");
Joey Goulycbed3bf2013-06-27 13:19:54 +0000582 } else if (FPU == "neon-fp-armv8") {
Joey Gouly2b33b7e2013-09-13 13:48:33 +0000583 Features.push_back("+fp-armv8");
Rafael Espindola146dbbf2013-08-21 16:39:20 +0000584 Features.push_back("+neon");
Bernard Ogdenf779e652013-10-24 18:32:51 +0000585 Features.push_back("-crypto");
Amara Emersoncdc532c2013-09-19 13:54:03 +0000586 } else if (FPU == "crypto-neon-fp-armv8") {
Amara Emersoncdc532c2013-09-19 13:54:03 +0000587 Features.push_back("+fp-armv8");
Bernard Ogdenf779e652013-10-24 18:32:51 +0000588 Features.push_back("+neon");
589 Features.push_back("+crypto");
Chad Rosier99317272012-04-04 20:51:35 +0000590 } else if (FPU == "neon") {
Rafael Espindola146dbbf2013-08-21 16:39:20 +0000591 Features.push_back("+neon");
Amara Emersonfe7ed042013-10-01 10:20:54 +0000592 } else if (FPU == "none") {
593 Features.push_back("-vfp2");
594 Features.push_back("-vfp3");
595 Features.push_back("-vfp4");
596 Features.push_back("-fp-armv8");
597 Features.push_back("-crypto");
598 Features.push_back("-neon");
Chad Rosier99317272012-04-04 20:51:35 +0000599 } else
600 D.Diag(diag::err_drv_clang_unsupported) << A->getAsString(Args);
601}
602
Anton Korobeynikove2571792012-04-09 13:38:30 +0000603// Select the float ABI as determined by -msoft-float, -mhard-float, and
604// -mfloat-abi=.
Stephen Hines651f13c2014-04-23 16:59:28 -0700605StringRef tools::arm::getARMFloatABI(const Driver &D, const ArgList &Args,
606 const llvm::Triple &Triple) {
Chris Lattner5f9e2722011-07-23 10:55:15 +0000607 StringRef FloatABI;
Daniel Dunbarcbd19332009-09-10 23:00:09 +0000608 if (Arg *A = Args.getLastArg(options::OPT_msoft_float,
609 options::OPT_mhard_float,
610 options::OPT_mfloat_abi_EQ)) {
611 if (A->getOption().matches(options::OPT_msoft_float))
612 FloatABI = "soft";
613 else if (A->getOption().matches(options::OPT_mhard_float))
614 FloatABI = "hard";
615 else {
Richard Smith1d489cf2012-11-01 04:30:05 +0000616 FloatABI = A->getValue();
Daniel Dunbarcbd19332009-09-10 23:00:09 +0000617 if (FloatABI != "soft" && FloatABI != "softfp" && FloatABI != "hard") {
Chris Lattner5f9e2722011-07-23 10:55:15 +0000618 D.Diag(diag::err_drv_invalid_mfloat_abi)
Daniel Dunbarcbd19332009-09-10 23:00:09 +0000619 << A->getAsString(Args);
620 FloatABI = "soft";
621 }
622 }
623 }
624
625 // If unspecified, choose the default based on the platform.
626 if (FloatABI.empty()) {
Rafael Espindolabcd6df62010-06-28 17:18:09 +0000627 switch (Triple.getOS()) {
Bob Wilson905c45f2011-10-14 05:03:44 +0000628 case llvm::Triple::Darwin:
629 case llvm::Triple::MacOSX:
630 case llvm::Triple::IOS: {
Daniel Dunbarcbd19332009-09-10 23:00:09 +0000631 // Darwin defaults to "softfp" for v6 and v7.
632 //
633 // FIXME: Factor out an ARM class so we can cache the arch somewhere.
Benjamin Kramer92c4fd52012-06-26 22:20:06 +0000634 std::string ArchName =
Stephen Hines651f13c2014-04-23 16:59:28 -0700635 arm::getLLVMArchSuffixForARM(arm::getARMTargetCPU(Args, Triple));
Benjamin Kramer92c4fd52012-06-26 22:20:06 +0000636 if (StringRef(ArchName).startswith("v6") ||
637 StringRef(ArchName).startswith("v7"))
Daniel Dunbarcbd19332009-09-10 23:00:09 +0000638 FloatABI = "softfp";
639 else
640 FloatABI = "soft";
641 break;
642 }
643
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700644 // FIXME: this is invalid for WindowsCE
645 case llvm::Triple::Win32:
646 FloatABI = "hard";
647 break;
648
Rafael Espindola27fa2362012-12-13 04:17:14 +0000649 case llvm::Triple::FreeBSD:
Stephen Hines651f13c2014-04-23 16:59:28 -0700650 switch(Triple.getEnvironment()) {
651 case llvm::Triple::GNUEABIHF:
652 FloatABI = "hard";
653 break;
654 default:
655 // FreeBSD defaults to soft float
656 FloatABI = "soft";
657 break;
658 }
Rafael Espindola27fa2362012-12-13 04:17:14 +0000659 break;
660
Daniel Dunbarcbd19332009-09-10 23:00:09 +0000661 default:
Bob Wilsonfc2bd7c2011-02-04 17:59:28 +0000662 switch(Triple.getEnvironment()) {
Jiangning Liuff104a12012-07-31 08:06:29 +0000663 case llvm::Triple::GNUEABIHF:
664 FloatABI = "hard";
665 break;
Bob Wilsonfc2bd7c2011-02-04 17:59:28 +0000666 case llvm::Triple::GNUEABI:
667 FloatABI = "softfp";
668 break;
Stephen Hines651f13c2014-04-23 16:59:28 -0700669 case llvm::Triple::EABIHF:
670 FloatABI = "hard";
671 break;
Bob Wilsonfc2bd7c2011-02-04 17:59:28 +0000672 case llvm::Triple::EABI:
673 // EABI is always AAPCS, and if it was not marked 'hard', it's softfp
674 FloatABI = "softfp";
675 break;
Logan Chien94a71422012-09-02 09:30:11 +0000676 case llvm::Triple::Android: {
Benjamin Kramer92c4fd52012-06-26 22:20:06 +0000677 std::string ArchName =
Stephen Hines651f13c2014-04-23 16:59:28 -0700678 arm::getLLVMArchSuffixForARM(arm::getARMTargetCPU(Args, Triple));
Benjamin Kramer92c4fd52012-06-26 22:20:06 +0000679 if (StringRef(ArchName).startswith("v7"))
Chandler Carruthb43550b2012-01-10 19:47:42 +0000680 FloatABI = "softfp";
681 else
682 FloatABI = "soft";
683 break;
684 }
Bob Wilsonfc2bd7c2011-02-04 17:59:28 +0000685 default:
686 // Assume "soft", but warn the user we are guessing.
687 FloatABI = "soft";
Stephen Hines651f13c2014-04-23 16:59:28 -0700688 if (Triple.getOS() != llvm::Triple::UnknownOS ||
689 !Triple.isOSBinFormatMachO())
690 D.Diag(diag::warn_drv_assuming_mfloat_abi_is) << "soft";
Bob Wilsonfc2bd7c2011-02-04 17:59:28 +0000691 break;
692 }
Daniel Dunbarcbd19332009-09-10 23:00:09 +0000693 }
694 }
695
Anton Korobeynikove2571792012-04-09 13:38:30 +0000696 return FloatABI;
697}
698
Rafael Espindola146dbbf2013-08-21 16:39:20 +0000699static void getARMTargetFeatures(const Driver &D, const llvm::Triple &Triple,
700 const ArgList &Args,
Stephen Hines651f13c2014-04-23 16:59:28 -0700701 std::vector<const char *> &Features,
702 bool ForAS) {
703 StringRef FloatABI = tools::arm::getARMFloatABI(D, Args, Triple);
704 if (!ForAS) {
705 // FIXME: Note, this is a hack, the LLVM backend doesn't actually use these
706 // yet (it uses the -mfloat-abi and -msoft-float options), and it is
707 // stripped out by the ARM target. We should probably pass this a new
708 // -target-option, which is handled by the -cc1/-cc1as invocation.
709 //
710 // FIXME2: For consistency, it would be ideal if we set up the target
711 // machine state the same when using the frontend or the assembler. We don't
712 // currently do that for the assembler, we pass the options directly to the
713 // backend and never even instantiate the frontend TargetInfo. If we did,
714 // and used its handleTargetFeatures hook, then we could ensure the
715 // assembler and the frontend behave the same.
Rafael Espindola146dbbf2013-08-21 16:39:20 +0000716
Stephen Hines651f13c2014-04-23 16:59:28 -0700717 // Use software floating point operations?
718 if (FloatABI == "soft")
719 Features.push_back("+soft-float");
720
721 // Use software floating point argument passing?
722 if (FloatABI != "hard")
723 Features.push_back("+soft-float-abi");
724 }
Rafael Espindola146dbbf2013-08-21 16:39:20 +0000725
726 // Honor -mfpu=.
727 if (const Arg *A = Args.getLastArg(options::OPT_mfpu_EQ))
Amara Emersonfe7ed042013-10-01 10:20:54 +0000728 getARMFPUFeatures(D, A, Args, Features);
Silviu Baranga1db2e272013-10-21 10:54:53 +0000729 if (const Arg *A = Args.getLastArg(options::OPT_mhwdiv_EQ))
730 getARMHWDivFeatures(D, A, Args, Features);
Rafael Espindola146dbbf2013-08-21 16:39:20 +0000731
Rafael Espindola146dbbf2013-08-21 16:39:20 +0000732 // Setting -msoft-float effectively disables NEON because of the GCC
733 // implementation, although the same isn't true of VFP or VFP3.
Stephen Hines651f13c2014-04-23 16:59:28 -0700734 if (FloatABI == "soft") {
Rafael Espindola146dbbf2013-08-21 16:39:20 +0000735 Features.push_back("-neon");
Stephen Hines651f13c2014-04-23 16:59:28 -0700736 // Also need to explicitly disable features which imply NEON.
737 Features.push_back("-crypto");
738 }
Bernard Ogden909f35a2013-10-29 09:47:51 +0000739
740 // En/disable crc
741 if (Arg *A = Args.getLastArg(options::OPT_mcrc,
742 options::OPT_mnocrc)) {
743 if (A->getOption().matches(options::OPT_mcrc))
744 Features.push_back("+crc");
745 else
746 Features.push_back("-crc");
747 }
Rafael Espindola146dbbf2013-08-21 16:39:20 +0000748}
Anton Korobeynikove2571792012-04-09 13:38:30 +0000749
750void Clang::AddARMTargetArgs(const ArgList &Args,
751 ArgStringList &CmdArgs,
752 bool KernelOrKext) const {
753 const Driver &D = getToolChain().getDriver();
Daniel Dunbar7a0c0642012-10-15 22:23:53 +0000754 // Get the effective triple, which takes into account the deployment target.
755 std::string TripleStr = getToolChain().ComputeEffectiveClangTriple(Args);
756 llvm::Triple Triple(TripleStr);
Stephen Hines651f13c2014-04-23 16:59:28 -0700757 std::string CPUName = arm::getARMTargetCPU(Args, Triple);
Anton Korobeynikove2571792012-04-09 13:38:30 +0000758
759 // Select the ABI to use.
760 //
761 // FIXME: Support -meabi.
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700762 const char *ABIName = nullptr;
Anton Korobeynikove2571792012-04-09 13:38:30 +0000763 if (Arg *A = Args.getLastArg(options::OPT_mabi_EQ)) {
Richard Smith1d489cf2012-11-01 04:30:05 +0000764 ABIName = A->getValue();
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700765 } else if (Triple.isOSBinFormatMachO()) {
Daniel Dunbar2e4e1102012-10-22 18:30:51 +0000766 // The backend is hardwired to assume AAPCS for M-class processors, ensure
767 // the frontend matches that.
Tim Northoverfc1a75b2013-10-03 14:23:28 +0000768 if (Triple.getEnvironment() == llvm::Triple::EABI ||
Stephen Hines651f13c2014-04-23 16:59:28 -0700769 (Triple.getOS() == llvm::Triple::UnknownOS &&
770 Triple.getObjectFormat() == llvm::Triple::MachO) ||
Tim Northoverfc1a75b2013-10-03 14:23:28 +0000771 StringRef(CPUName).startswith("cortex-m")) {
Daniel Dunbar2e4e1102012-10-22 18:30:51 +0000772 ABIName = "aapcs";
773 } else {
774 ABIName = "apcs-gnu";
775 }
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700776 } else if (Triple.isOSWindows()) {
777 // FIXME: this is invalid for WindowsCE
778 ABIName = "aapcs";
Anton Korobeynikove2571792012-04-09 13:38:30 +0000779 } else {
780 // Select the default based on the platform.
781 switch(Triple.getEnvironment()) {
Logan Chien94a71422012-09-02 09:30:11 +0000782 case llvm::Triple::Android:
Anton Korobeynikove2571792012-04-09 13:38:30 +0000783 case llvm::Triple::GNUEABI:
Jiangning Liuff104a12012-07-31 08:06:29 +0000784 case llvm::Triple::GNUEABIHF:
Anton Korobeynikove2571792012-04-09 13:38:30 +0000785 ABIName = "aapcs-linux";
786 break;
Stephen Hines651f13c2014-04-23 16:59:28 -0700787 case llvm::Triple::EABIHF:
Anton Korobeynikove2571792012-04-09 13:38:30 +0000788 case llvm::Triple::EABI:
789 ABIName = "aapcs";
790 break;
791 default:
Stephen Hines176edba2014-12-01 14:53:08 -0800792 if (Triple.getOS() == llvm::Triple::NetBSD)
793 ABIName = "apcs-gnu";
794 else
795 ABIName = "aapcs";
796 break;
Anton Korobeynikove2571792012-04-09 13:38:30 +0000797 }
798 }
799 CmdArgs.push_back("-target-abi");
800 CmdArgs.push_back(ABIName);
801
Anton Korobeynikove2571792012-04-09 13:38:30 +0000802 // Determine floating point ABI from the options & target defaults.
Stephen Hines651f13c2014-04-23 16:59:28 -0700803 StringRef FloatABI = tools::arm::getARMFloatABI(D, Args, Triple);
Daniel Dunbarcbd19332009-09-10 23:00:09 +0000804 if (FloatABI == "soft") {
805 // Floating point operations and argument passing are soft.
806 //
807 // FIXME: This changes CPP defines, we need -target-soft-float.
Daniel Dunbar3b315262009-11-30 08:42:00 +0000808 CmdArgs.push_back("-msoft-float");
Daniel Dunbar87667aa2009-12-08 19:49:51 +0000809 CmdArgs.push_back("-mfloat-abi");
810 CmdArgs.push_back("soft");
Daniel Dunbarcbd19332009-09-10 23:00:09 +0000811 } else if (FloatABI == "softfp") {
812 // Floating point operations are hard, but argument passing is soft.
Daniel Dunbar87667aa2009-12-08 19:49:51 +0000813 CmdArgs.push_back("-mfloat-abi");
814 CmdArgs.push_back("soft");
Daniel Dunbarcbd19332009-09-10 23:00:09 +0000815 } else {
816 // Floating point operations and argument passing are hard.
817 assert(FloatABI == "hard" && "Invalid float abi!");
Daniel Dunbar87667aa2009-12-08 19:49:51 +0000818 CmdArgs.push_back("-mfloat-abi");
819 CmdArgs.push_back("hard");
Daniel Dunbarcbd19332009-09-10 23:00:09 +0000820 }
Daniel Dunbar97f52ac2009-12-19 04:15:38 +0000821
Daniel Dunbarfa41d692011-03-17 17:10:06 +0000822 // Kernel code has more strict alignment requirements.
823 if (KernelOrKext) {
Cameron Esfahani57b1da12013-09-14 01:09:11 +0000824 if (!Triple.isiOS() || Triple.isOSVersionLT(6)) {
Daniel Dunbar7a0c0642012-10-15 22:23:53 +0000825 CmdArgs.push_back("-backend-option");
826 CmdArgs.push_back("-arm-long-calls");
827 }
Daniel Dunbarfa41d692011-03-17 17:10:06 +0000828
Daniel Dunbar3c66d302011-03-22 16:48:17 +0000829 CmdArgs.push_back("-backend-option");
Daniel Dunbarfa41d692011-03-17 17:10:06 +0000830 CmdArgs.push_back("-arm-strict-align");
Daniel Dunbarb5fbb892011-04-18 21:26:42 +0000831
832 // The kext linker doesn't know how to deal with movw/movt.
Daniel Dunbarb5fbb892011-04-18 21:26:42 +0000833 CmdArgs.push_back("-backend-option");
Renato Golinebc313d2013-08-15 20:54:45 +0000834 CmdArgs.push_back("-arm-use-movt=0");
Daniel Dunbar7187fac2011-03-17 00:07:34 +0000835 }
Chad Rosier1b906052011-08-26 00:26:29 +0000836
Stephen Hines176edba2014-12-01 14:53:08 -0800837 // -mkernel implies -mstrict-align; don't add the redundant option.
838 if (!KernelOrKext) {
839 if (Arg *A = Args.getLastArg(options::OPT_mno_unaligned_access,
840 options::OPT_munaligned_access)) {
841 CmdArgs.push_back("-backend-option");
842 if (A->getOption().matches(options::OPT_mno_unaligned_access))
843 CmdArgs.push_back("-arm-strict-align");
844 else {
845 if (Triple.getSubArch() == llvm::Triple::SubArchType::ARMSubArch_v6m)
846 D.Diag(diag::err_target_unsupported_unaligned) << "v6m";
847 CmdArgs.push_back("-arm-no-strict-align");
848 }
849 }
850 }
851
Chad Rosier1b906052011-08-26 00:26:29 +0000852 // Setting -mno-global-merge disables the codegen global merge pass. Setting
853 // -mglobal-merge has no effect as the pass is enabled by default.
854 if (Arg *A = Args.getLastArg(options::OPT_mglobal_merge,
855 options::OPT_mno_global_merge)) {
856 if (A->getOption().matches(options::OPT_mno_global_merge))
857 CmdArgs.push_back("-mno-global-merge");
858 }
Chad Rosieree9ad5c2012-05-16 20:40:09 +0000859
Bob Wilsonf6f77bd2013-04-11 18:53:25 +0000860 if (!Args.hasFlag(options::OPT_mimplicit_float,
861 options::OPT_mno_implicit_float,
862 true))
Chad Rosieree9ad5c2012-05-16 20:40:09 +0000863 CmdArgs.push_back("-no-implicit-float");
Renato Golin45bd2942013-08-24 14:44:35 +0000864
Stephen Hines651f13c2014-04-23 16:59:28 -0700865 // llvm does not support reserving registers in general. There is support
866 // for reserving r9 on ARM though (defined as a platform-specific register
867 // in ARM EABI).
868 if (Args.hasArg(options::OPT_ffixed_r9)) {
869 CmdArgs.push_back("-backend-option");
870 CmdArgs.push_back("-arm-reserve-r9");
871 }
872}
873
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700874/// getAArch64TargetCPU - Get the (LLVM) name of the AArch64 cpu we are
875/// targeting.
876static std::string getAArch64TargetCPU(const ArgList &Args) {
Stephen Hines176edba2014-12-01 14:53:08 -0800877 Arg *A;
878 std::string CPU;
879 // If we have -mtune or -mcpu, use that.
880 if ((A = Args.getLastArg(options::OPT_mtune_EQ))) {
881 CPU = A->getValue();
882 } else if ((A = Args.getLastArg(options::OPT_mcpu_EQ))) {
883 StringRef Mcpu = A->getValue();
884 CPU = Mcpu.split("+").first;
Stephen Hines651f13c2014-04-23 16:59:28 -0700885 }
886
Stephen Hines176edba2014-12-01 14:53:08 -0800887 // Handle CPU name is 'native'.
888 if (CPU == "native")
889 return llvm::sys::getHostCPUName();
890 else if (CPU.size())
891 return CPU;
Stephen Hines651f13c2014-04-23 16:59:28 -0700892
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700893 // Make sure we pick "cyclone" if -arch is used.
894 // FIXME: Should this be picked by checking the target triple instead?
895 if (Args.getLastArg(options::OPT_arch))
896 return "cyclone";
897
898 return "generic";
Stephen Hines651f13c2014-04-23 16:59:28 -0700899}
900
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700901void Clang::AddAArch64TargetArgs(const ArgList &Args,
902 ArgStringList &CmdArgs) const {
Stephen Hines651f13c2014-04-23 16:59:28 -0700903 std::string TripleStr = getToolChain().ComputeEffectiveClangTriple(Args);
904 llvm::Triple Triple(TripleStr);
905
906 if (!Args.hasFlag(options::OPT_mred_zone, options::OPT_mno_red_zone, true) ||
907 Args.hasArg(options::OPT_mkernel) ||
908 Args.hasArg(options::OPT_fapple_kext))
909 CmdArgs.push_back("-disable-red-zone");
910
911 if (!Args.hasFlag(options::OPT_mimplicit_float,
912 options::OPT_mno_implicit_float, true))
913 CmdArgs.push_back("-no-implicit-float");
914
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700915 const char *ABIName = nullptr;
Stephen Hines651f13c2014-04-23 16:59:28 -0700916 if (Arg *A = Args.getLastArg(options::OPT_mabi_EQ))
917 ABIName = A->getValue();
918 else if (Triple.isOSDarwin())
919 ABIName = "darwinpcs";
920 else
921 ABIName = "aapcs";
922
923 CmdArgs.push_back("-target-abi");
924 CmdArgs.push_back(ABIName);
925
Stephen Hines176edba2014-12-01 14:53:08 -0800926 if (Arg *A = Args.getLastArg(options::OPT_mno_unaligned_access,
927 options::OPT_munaligned_access)) {
Stephen Hines651f13c2014-04-23 16:59:28 -0700928 CmdArgs.push_back("-backend-option");
Stephen Hines176edba2014-12-01 14:53:08 -0800929 if (A->getOption().matches(options::OPT_mno_unaligned_access))
930 CmdArgs.push_back("-aarch64-strict-align");
931 else
932 CmdArgs.push_back("-aarch64-no-strict-align");
Stephen Hines651f13c2014-04-23 16:59:28 -0700933 }
Stephen Hinesc568f1e2014-07-21 00:47:37 -0700934
Stephen Hines7fb0af82014-10-17 08:52:15 -0700935 if (Arg *A = Args.getLastArg(options::OPT_mfix_cortex_a53_835769,
936 options::OPT_mno_fix_cortex_a53_835769)) {
937 CmdArgs.push_back("-backend-option");
938 if (A->getOption().matches(options::OPT_mfix_cortex_a53_835769))
939 CmdArgs.push_back("-aarch64-fix-cortex-a53-835769=1");
940 else
941 CmdArgs.push_back("-aarch64-fix-cortex-a53-835769=0");
942 } else if (Triple.getEnvironment() == llvm::Triple::Android) {
943 // Enabled A53 errata (835769) workaround by default on android
944 CmdArgs.push_back("-backend-option");
945 CmdArgs.push_back("-aarch64-fix-cortex-a53-835769=1");
946 }
947
Stephen Hinesc568f1e2014-07-21 00:47:37 -0700948 // Setting -mno-global-merge disables the codegen global merge pass. Setting
949 // -mglobal-merge has no effect as the pass is enabled by default.
950 if (Arg *A = Args.getLastArg(options::OPT_mglobal_merge,
951 options::OPT_mno_global_merge)) {
952 if (A->getOption().matches(options::OPT_mno_global_merge))
953 CmdArgs.push_back("-mno-global-merge");
954 }
Daniel Dunbarb163ef72009-09-10 04:57:17 +0000955}
956
Simon Atanasyana2768be2012-04-07 22:09:23 +0000957// Get CPU and ABI names. They are not independent
958// so we have to calculate them together.
Stephen Hines176edba2014-12-01 14:53:08 -0800959void mips::getMipsCPUAndABI(const ArgList &Args,
960 const llvm::Triple &Triple,
961 StringRef &CPUName,
962 StringRef &ABIName) {
Stephen Hines651f13c2014-04-23 16:59:28 -0700963 const char *DefMips32CPU = "mips32r2";
964 const char *DefMips64CPU = "mips64r2";
Akira Hatanaka9f360622011-09-26 21:07:52 +0000965
Stephen Hinesc568f1e2014-07-21 00:47:37 -0700966 // MIPS32r6 is the default for mips(el)?-img-linux-gnu and MIPS64r6 is the
967 // default for mips64(el)?-img-linux-gnu.
968 if (Triple.getVendor() == llvm::Triple::ImaginationTechnologies &&
969 Triple.getEnvironment() == llvm::Triple::GNU) {
970 DefMips32CPU = "mips32r6";
971 DefMips64CPU = "mips64r6";
972 }
973
Simon Atanasyan89d83ff2012-09-10 08:32:41 +0000974 if (Arg *A = Args.getLastArg(options::OPT_march_EQ,
Simon Atanasyan66751bc2013-10-09 12:12:24 +0000975 options::OPT_mcpu_EQ))
976 CPUName = A->getValue();
Simon Atanasyan89d83ff2012-09-10 08:32:41 +0000977
Simon Atanasyanc7e2a4e2013-04-21 13:30:10 +0000978 if (Arg *A = Args.getLastArg(options::OPT_mabi_EQ)) {
Richard Smith1d489cf2012-11-01 04:30:05 +0000979 ABIName = A->getValue();
Simon Atanasyanc7e2a4e2013-04-21 13:30:10 +0000980 // Convert a GNU style Mips ABI name to the name
981 // accepted by LLVM Mips backend.
982 ABIName = llvm::StringSwitch<llvm::StringRef>(ABIName)
983 .Case("32", "o32")
984 .Case("64", "n64")
985 .Default(ABIName);
986 }
Simon Atanasyan89d83ff2012-09-10 08:32:41 +0000987
988 // Setup default CPU and ABI names.
989 if (CPUName.empty() && ABIName.empty()) {
Rafael Espindolab330e402013-08-20 22:12:08 +0000990 switch (Triple.getArch()) {
Simon Atanasyan89d83ff2012-09-10 08:32:41 +0000991 default:
992 llvm_unreachable("Unexpected triple arch name");
993 case llvm::Triple::mips:
994 case llvm::Triple::mipsel:
995 CPUName = DefMips32CPU;
996 break;
997 case llvm::Triple::mips64:
998 case llvm::Triple::mips64el:
999 CPUName = DefMips64CPU;
1000 break;
1001 }
1002 }
1003
Stephen Hinesc568f1e2014-07-21 00:47:37 -07001004 if (ABIName.empty()) {
1005 // Deduce ABI name from the target triple.
1006 if (Triple.getArch() == llvm::Triple::mips ||
1007 Triple.getArch() == llvm::Triple::mipsel)
1008 ABIName = "o32";
1009 else
1010 ABIName = "n64";
Simon Atanasyan89d83ff2012-09-10 08:32:41 +00001011 }
1012
Stephen Hinesc568f1e2014-07-21 00:47:37 -07001013 if (CPUName.empty()) {
1014 // Deduce CPU name from ABI name.
1015 CPUName = llvm::StringSwitch<const char *>(ABIName)
1016 .Cases("o32", "eabi", DefMips32CPU)
1017 .Cases("n32", "n64", DefMips64CPU)
1018 .Default("");
1019 }
Stephen Hines176edba2014-12-01 14:53:08 -08001020
1021 // FIXME: Warn on inconsistent use of -march and -mabi.
Simon Atanasyana2768be2012-04-07 22:09:23 +00001022}
1023
Simon Atanasyane9616a42013-02-27 14:55:49 +00001024// Convert ABI name to the GNU tools acceptable variant.
1025static StringRef getGnuCompatibleMipsABIName(StringRef ABI) {
1026 return llvm::StringSwitch<llvm::StringRef>(ABI)
1027 .Case("o32", "32")
1028 .Case("n64", "64")
1029 .Default(ABI);
1030}
1031
Simon Atanasyan5e627792012-06-02 15:06:29 +00001032// Select the MIPS float ABI as determined by -msoft-float, -mhard-float,
1033// and -mfloat-abi=.
1034static StringRef getMipsFloatABI(const Driver &D, const ArgList &Args) {
Chris Lattner5f9e2722011-07-23 10:55:15 +00001035 StringRef FloatABI;
Eric Christophered734732010-03-02 02:41:08 +00001036 if (Arg *A = Args.getLastArg(options::OPT_msoft_float,
Akira Hatanakaad8d8a32012-03-23 23:07:09 +00001037 options::OPT_mhard_float,
1038 options::OPT_mfloat_abi_EQ)) {
Eric Christophered734732010-03-02 02:41:08 +00001039 if (A->getOption().matches(options::OPT_msoft_float))
1040 FloatABI = "soft";
1041 else if (A->getOption().matches(options::OPT_mhard_float))
1042 FloatABI = "hard";
Akira Hatanakaad8d8a32012-03-23 23:07:09 +00001043 else {
Richard Smith1d489cf2012-11-01 04:30:05 +00001044 FloatABI = A->getValue();
Simon Atanasyan2ed42b82013-04-14 08:37:15 +00001045 if (FloatABI != "soft" && FloatABI != "hard") {
Simon Atanasyan5e627792012-06-02 15:06:29 +00001046 D.Diag(diag::err_drv_invalid_mfloat_abi) << A->getAsString(Args);
Akira Hatanakaad8d8a32012-03-23 23:07:09 +00001047 FloatABI = "hard";
1048 }
1049 }
Eric Christophered734732010-03-02 02:41:08 +00001050 }
1051
1052 // If unspecified, choose the default based on the platform.
1053 if (FloatABI.empty()) {
Akira Hatanakaad8d8a32012-03-23 23:07:09 +00001054 // Assume "hard", because it's a default value used by gcc.
1055 // When we start to recognize specific target MIPS processors,
1056 // we will be able to select the default more correctly.
1057 FloatABI = "hard";
Eric Christophered734732010-03-02 02:41:08 +00001058 }
1059
Simon Atanasyan5e627792012-06-02 15:06:29 +00001060 return FloatABI;
1061}
1062
Simon Atanasyandc536f52012-07-05 18:51:43 +00001063static void AddTargetFeature(const ArgList &Args,
Rafael Espindola146dbbf2013-08-21 16:39:20 +00001064 std::vector<const char *> &Features,
1065 OptSpecifier OnOpt, OptSpecifier OffOpt,
Simon Atanasyandc536f52012-07-05 18:51:43 +00001066 StringRef FeatureName) {
1067 if (Arg *A = Args.getLastArg(OnOpt, OffOpt)) {
Simon Atanasyandc536f52012-07-05 18:51:43 +00001068 if (A->getOption().matches(OnOpt))
Rafael Espindola146dbbf2013-08-21 16:39:20 +00001069 Features.push_back(Args.MakeArgString("+" + FeatureName));
Simon Atanasyandc536f52012-07-05 18:51:43 +00001070 else
Rafael Espindola146dbbf2013-08-21 16:39:20 +00001071 Features.push_back(Args.MakeArgString("-" + FeatureName));
Simon Atanasyandc536f52012-07-05 18:51:43 +00001072 }
1073}
1074
Stephen Hines176edba2014-12-01 14:53:08 -08001075static void getMIPSTargetFeatures(const Driver &D, const llvm::Triple &Triple,
1076 const ArgList &Args,
Rafael Espindola146dbbf2013-08-21 16:39:20 +00001077 std::vector<const char *> &Features) {
Stephen Hines176edba2014-12-01 14:53:08 -08001078 StringRef CPUName;
1079 StringRef ABIName;
1080 mips::getMipsCPUAndABI(Args, Triple, CPUName, ABIName);
1081 ABIName = getGnuCompatibleMipsABIName(ABIName);
1082
1083 // Always override the backend's default ABI.
1084 std::string ABIFeature = llvm::StringSwitch<StringRef>(ABIName)
1085 .Case("32", "+o32")
1086 .Case("n32", "+n32")
1087 .Case("64", "+n64")
1088 .Case("eabi", "+eabi")
1089 .Default(("+" + ABIName).str());
1090 Features.push_back("-o32");
1091 Features.push_back("-n64");
1092 Features.push_back(Args.MakeArgString(ABIFeature));
1093
1094 AddTargetFeature(Args, Features, options::OPT_mno_abicalls,
1095 options::OPT_mabicalls, "noabicalls");
1096
Rafael Espindola146dbbf2013-08-21 16:39:20 +00001097 StringRef FloatABI = getMipsFloatABI(D, Args);
Stephen Hines651f13c2014-04-23 16:59:28 -07001098 if (FloatABI == "soft") {
Rafael Espindola146dbbf2013-08-21 16:39:20 +00001099 // FIXME: Note, this is a hack. We need to pass the selected float
1100 // mode to the MipsTargetInfoBase to define appropriate macros there.
1101 // Now it is the only method.
1102 Features.push_back("+soft-float");
1103 }
1104
Simon Atanasyanfc12c4a2013-09-24 09:09:16 +00001105 if (Arg *A = Args.getLastArg(options::OPT_mnan_EQ)) {
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001106 StringRef Val = StringRef(A->getValue());
1107 if (Val == "2008")
Simon Atanasyanfc12c4a2013-09-24 09:09:16 +00001108 Features.push_back("+nan2008");
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001109 else if (Val == "legacy")
1110 Features.push_back("-nan2008");
1111 else
1112 D.Diag(diag::err_drv_unsupported_option_argument)
1113 << A->getOption().getName() << Val;
Simon Atanasyanfc12c4a2013-09-24 09:09:16 +00001114 }
1115
Rafael Espindola146dbbf2013-08-21 16:39:20 +00001116 AddTargetFeature(Args, Features, options::OPT_msingle_float,
1117 options::OPT_mdouble_float, "single-float");
1118 AddTargetFeature(Args, Features, options::OPT_mips16, options::OPT_mno_mips16,
1119 "mips16");
1120 AddTargetFeature(Args, Features, options::OPT_mmicromips,
1121 options::OPT_mno_micromips, "micromips");
1122 AddTargetFeature(Args, Features, options::OPT_mdsp, options::OPT_mno_dsp,
1123 "dsp");
1124 AddTargetFeature(Args, Features, options::OPT_mdspr2, options::OPT_mno_dspr2,
1125 "dspr2");
1126 AddTargetFeature(Args, Features, options::OPT_mmsa, options::OPT_mno_msa,
1127 "msa");
Stephen Hines176edba2014-12-01 14:53:08 -08001128
1129 // Add the last -mfp32/-mfpxx/-mfp64 or if none are given and the ABI is O32
1130 // pass -mfpxx
1131 if (Arg *A = Args.getLastArg(options::OPT_mfp32, options::OPT_mfpxx,
1132 options::OPT_mfp64)) {
1133 if (A->getOption().matches(options::OPT_mfp32))
1134 Features.push_back(Args.MakeArgString("-fp64"));
1135 else if (A->getOption().matches(options::OPT_mfpxx)) {
1136 Features.push_back(Args.MakeArgString("+fpxx"));
1137 Features.push_back(Args.MakeArgString("+nooddspreg"));
1138 } else
1139 Features.push_back(Args.MakeArgString("+fp64"));
1140 } else if (mips::isFPXXDefault(Triple, CPUName, ABIName)) {
1141 Features.push_back(Args.MakeArgString("+fpxx"));
1142 Features.push_back(Args.MakeArgString("+nooddspreg"));
1143 }
1144
Stephen Hinesc568f1e2014-07-21 00:47:37 -07001145 AddTargetFeature(Args, Features, options::OPT_mno_odd_spreg,
1146 options::OPT_modd_spreg, "nooddspreg");
Rafael Espindola146dbbf2013-08-21 16:39:20 +00001147}
1148
Simon Atanasyan5e627792012-06-02 15:06:29 +00001149void Clang::AddMIPSTargetArgs(const ArgList &Args,
Simon Atanasyana8141612013-04-14 14:07:41 +00001150 ArgStringList &CmdArgs) const {
Simon Atanasyan5e627792012-06-02 15:06:29 +00001151 const Driver &D = getToolChain().getDriver();
1152 StringRef CPUName;
1153 StringRef ABIName;
Rafael Espindolab330e402013-08-20 22:12:08 +00001154 const llvm::Triple &Triple = getToolChain().getTriple();
Stephen Hines176edba2014-12-01 14:53:08 -08001155 mips::getMipsCPUAndABI(Args, Triple, CPUName, ABIName);
Simon Atanasyan5e627792012-06-02 15:06:29 +00001156
1157 CmdArgs.push_back("-target-abi");
1158 CmdArgs.push_back(ABIName.data());
1159
1160 StringRef FloatABI = getMipsFloatABI(D, Args);
1161
Stephen Hines651f13c2014-04-23 16:59:28 -07001162 if (FloatABI == "soft") {
Eric Christophered734732010-03-02 02:41:08 +00001163 // Floating point operations and argument passing are soft.
Eric Christophered734732010-03-02 02:41:08 +00001164 CmdArgs.push_back("-msoft-float");
Akira Hatanakaad8d8a32012-03-23 23:07:09 +00001165 CmdArgs.push_back("-mfloat-abi");
1166 CmdArgs.push_back("soft");
Akira Hatanakaad8d8a32012-03-23 23:07:09 +00001167 }
Akira Hatanakaad8d8a32012-03-23 23:07:09 +00001168 else {
1169 // Floating point operations and argument passing are hard.
Eric Christophered734732010-03-02 02:41:08 +00001170 assert(FloatABI == "hard" && "Invalid float abi!");
Akira Hatanakaad8d8a32012-03-23 23:07:09 +00001171 CmdArgs.push_back("-mfloat-abi");
1172 CmdArgs.push_back("hard");
Eric Christophered734732010-03-02 02:41:08 +00001173 }
Simon Atanasyan0b273ef2012-07-05 14:19:39 +00001174
Simon Atanasyanbda07ac2012-12-01 18:27:21 +00001175 if (Arg *A = Args.getLastArg(options::OPT_mxgot, options::OPT_mno_xgot)) {
1176 if (A->getOption().matches(options::OPT_mxgot)) {
1177 CmdArgs.push_back("-mllvm");
1178 CmdArgs.push_back("-mxgot");
1179 }
1180 }
1181
Simon Atanasyan6bdc4c62013-05-11 06:33:44 +00001182 if (Arg *A = Args.getLastArg(options::OPT_mldc1_sdc1,
1183 options::OPT_mno_ldc1_sdc1)) {
1184 if (A->getOption().matches(options::OPT_mno_ldc1_sdc1)) {
1185 CmdArgs.push_back("-mllvm");
1186 CmdArgs.push_back("-mno-ldc1-sdc1");
1187 }
1188 }
1189
Akira Hatanakacdbc3b32013-07-19 18:58:48 +00001190 if (Arg *A = Args.getLastArg(options::OPT_mcheck_zero_division,
1191 options::OPT_mno_check_zero_division)) {
1192 if (A->getOption().matches(options::OPT_mno_check_zero_division)) {
1193 CmdArgs.push_back("-mllvm");
1194 CmdArgs.push_back("-mno-check-zero-division");
1195 }
1196 }
1197
Simon Atanasyan9804b762012-08-27 20:55:56 +00001198 if (Arg *A = Args.getLastArg(options::OPT_G)) {
Richard Smith1d489cf2012-11-01 04:30:05 +00001199 StringRef v = A->getValue();
Simon Atanasyan9804b762012-08-27 20:55:56 +00001200 CmdArgs.push_back("-mllvm");
1201 CmdArgs.push_back(Args.MakeArgString("-mips-ssection-threshold=" + v));
1202 A->claim();
1203 }
Eric Christophered734732010-03-02 02:41:08 +00001204}
1205
Hal Finkel02a84272012-06-11 22:35:19 +00001206/// getPPCTargetCPU - Get the (LLVM) name of the PowerPC cpu we are targeting.
1207static std::string getPPCTargetCPU(const ArgList &Args) {
1208 if (Arg *A = Args.getLastArg(options::OPT_mcpu_EQ)) {
Richard Smith1d489cf2012-11-01 04:30:05 +00001209 StringRef CPUName = A->getValue();
Hal Finkel02a84272012-06-11 22:35:19 +00001210
1211 if (CPUName == "native") {
1212 std::string CPU = llvm::sys::getHostCPUName();
1213 if (!CPU.empty() && CPU != "generic")
1214 return CPU;
1215 else
1216 return "";
1217 }
1218
1219 return llvm::StringSwitch<const char *>(CPUName)
1220 .Case("common", "generic")
1221 .Case("440", "440")
1222 .Case("440fp", "440")
1223 .Case("450", "450")
1224 .Case("601", "601")
1225 .Case("602", "602")
1226 .Case("603", "603")
1227 .Case("603e", "603e")
1228 .Case("603ev", "603ev")
1229 .Case("604", "604")
1230 .Case("604e", "604e")
1231 .Case("620", "620")
Bill Schmidt2821e182013-02-01 20:23:10 +00001232 .Case("630", "pwr3")
Hal Finkel02a84272012-06-11 22:35:19 +00001233 .Case("G3", "g3")
1234 .Case("7400", "7400")
1235 .Case("G4", "g4")
1236 .Case("7450", "7450")
1237 .Case("G4+", "g4+")
1238 .Case("750", "750")
1239 .Case("970", "970")
1240 .Case("G5", "g5")
1241 .Case("a2", "a2")
Hal Finkel5ccd3d02013-02-01 05:53:33 +00001242 .Case("a2q", "a2q")
Hal Finkel7de32962012-09-18 22:25:03 +00001243 .Case("e500mc", "e500mc")
1244 .Case("e5500", "e5500")
Bill Schmidt2821e182013-02-01 20:23:10 +00001245 .Case("power3", "pwr3")
1246 .Case("power4", "pwr4")
1247 .Case("power5", "pwr5")
1248 .Case("power5x", "pwr5x")
Hal Finkel02a84272012-06-11 22:35:19 +00001249 .Case("power6", "pwr6")
Bill Schmidt2821e182013-02-01 20:23:10 +00001250 .Case("power6x", "pwr6x")
Hal Finkel02a84272012-06-11 22:35:19 +00001251 .Case("power7", "pwr7")
Stephen Hinesc568f1e2014-07-21 00:47:37 -07001252 .Case("power8", "pwr8")
Bill Schmidt2821e182013-02-01 20:23:10 +00001253 .Case("pwr3", "pwr3")
1254 .Case("pwr4", "pwr4")
1255 .Case("pwr5", "pwr5")
1256 .Case("pwr5x", "pwr5x")
1257 .Case("pwr6", "pwr6")
1258 .Case("pwr6x", "pwr6x")
1259 .Case("pwr7", "pwr7")
Stephen Hinesc568f1e2014-07-21 00:47:37 -07001260 .Case("pwr8", "pwr8")
Hal Finkel02a84272012-06-11 22:35:19 +00001261 .Case("powerpc", "ppc")
1262 .Case("powerpc64", "ppc64")
Bill Schmidtea7fb0c2013-07-26 01:36:11 +00001263 .Case("powerpc64le", "ppc64le")
Hal Finkel02a84272012-06-11 22:35:19 +00001264 .Default("");
1265 }
1266
1267 return "";
1268}
1269
Rafael Espindola146dbbf2013-08-21 16:39:20 +00001270static void getPPCTargetFeatures(const ArgList &Args,
1271 std::vector<const char *> &Features) {
Eric Christopherd5e59fc2013-10-16 20:40:08 +00001272 for (arg_iterator it = Args.filtered_begin(options::OPT_m_ppc_Features_Group),
1273 ie = Args.filtered_end();
1274 it != ie; ++it) {
1275 StringRef Name = (*it)->getOption().getName();
1276 (*it)->claim();
1277
1278 // Skip over "-m".
1279 assert(Name.startswith("m") && "Invalid feature name.");
1280 Name = Name.substr(1);
1281
1282 bool IsNegative = Name.startswith("no-");
1283 if (IsNegative)
1284 Name = Name.substr(3);
1285
1286 // Note that gcc calls this mfcrf and LLVM calls this mfocrf so we
1287 // pass the correct option to the backend while calling the frontend
1288 // option the same.
1289 // TODO: Change the LLVM backend option maybe?
1290 if (Name == "mfcrf")
1291 Name = "mfocrf";
1292
1293 Features.push_back(Args.MakeArgString((IsNegative ? "-" : "+") + Name));
1294 }
1295
1296 // Altivec is a bit weird, allow overriding of the Altivec feature here.
Rafael Espindola146dbbf2013-08-21 16:39:20 +00001297 AddTargetFeature(Args, Features, options::OPT_faltivec,
1298 options::OPT_fno_altivec, "altivec");
Hal Finkel02a84272012-06-11 22:35:19 +00001299}
1300
Stephen Hines176edba2014-12-01 14:53:08 -08001301void Clang::AddPPCTargetArgs(const ArgList &Args,
1302 ArgStringList &CmdArgs) const {
1303 // Select the ABI to use.
1304 const char *ABIName = nullptr;
1305 if (Arg *A = Args.getLastArg(options::OPT_mabi_EQ)) {
1306 ABIName = A->getValue();
1307 } else if (getToolChain().getTriple().isOSLinux())
1308 switch(getToolChain().getArch()) {
1309 case llvm::Triple::ppc64:
1310 ABIName = "elfv1";
1311 break;
1312 case llvm::Triple::ppc64le:
1313 ABIName = "elfv2";
1314 break;
1315 default:
1316 break;
1317 }
1318
1319 if (ABIName) {
1320 CmdArgs.push_back("-target-abi");
1321 CmdArgs.push_back(ABIName);
1322 }
1323}
1324
1325bool ppc::hasPPCAbiArg(const ArgList &Args, const char *Value) {
1326 Arg *A = Args.getLastArg(options::OPT_mabi_EQ);
1327 return A && (A->getValue() == StringRef(Value));
1328}
1329
Tom Stellarde25d2f62013-04-01 20:56:53 +00001330/// Get the (LLVM) name of the R600 gpu we are targeting.
1331static std::string getR600TargetGPU(const ArgList &Args) {
1332 if (Arg *A = Args.getLastArg(options::OPT_mcpu_EQ)) {
Benjamin Kramer39e4b0f2013-10-21 12:33:55 +00001333 const char *GPUName = A->getValue();
Tom Stellarde25d2f62013-04-01 20:56:53 +00001334 return llvm::StringSwitch<const char *>(GPUName)
Tom Stellardfd4aa4b2013-05-06 16:12:05 +00001335 .Cases("rv630", "rv635", "r600")
1336 .Cases("rv610", "rv620", "rs780", "rs880")
Tom Stellarde25d2f62013-04-01 20:56:53 +00001337 .Case("rv740", "rv770")
1338 .Case("palm", "cedar")
Tom Stellardfd4aa4b2013-05-06 16:12:05 +00001339 .Cases("sumo", "sumo2", "sumo")
Tom Stellarde25d2f62013-04-01 20:56:53 +00001340 .Case("hemlock", "cypress")
1341 .Case("aruba", "cayman")
Benjamin Kramer39e4b0f2013-10-21 12:33:55 +00001342 .Default(GPUName);
Tom Stellarde25d2f62013-04-01 20:56:53 +00001343 }
1344 return "";
1345}
1346
Rafael Espindola146dbbf2013-08-21 16:39:20 +00001347static void getSparcTargetFeatures(const ArgList &Args,
Stephen Hines176edba2014-12-01 14:53:08 -08001348 std::vector<const char *> &Features) {
Rafael Espindola146dbbf2013-08-21 16:39:20 +00001349 bool SoftFloatABI = true;
1350 if (Arg *A =
1351 Args.getLastArg(options::OPT_msoft_float, options::OPT_mhard_float)) {
1352 if (A->getOption().matches(options::OPT_mhard_float))
1353 SoftFloatABI = false;
1354 }
1355 if (SoftFloatABI)
1356 Features.push_back("+soft-float");
1357}
1358
Bruno Cardoso Lopes9284d212010-11-09 17:21:19 +00001359void Clang::AddSparcTargetArgs(const ArgList &Args,
1360 ArgStringList &CmdArgs) const {
1361 const Driver &D = getToolChain().getDriver();
1362
Stephen Hines176edba2014-12-01 14:53:08 -08001363 // Select the float ABI as determined by -msoft-float and -mhard-float.
Chris Lattner5f9e2722011-07-23 10:55:15 +00001364 StringRef FloatABI;
Bruno Cardoso Lopes9284d212010-11-09 17:21:19 +00001365 if (Arg *A = Args.getLastArg(options::OPT_msoft_float,
1366 options::OPT_mhard_float)) {
1367 if (A->getOption().matches(options::OPT_msoft_float))
1368 FloatABI = "soft";
1369 else if (A->getOption().matches(options::OPT_mhard_float))
1370 FloatABI = "hard";
1371 }
1372
1373 // If unspecified, choose the default based on the platform.
1374 if (FloatABI.empty()) {
Aaron Ballmand58915e2013-07-15 13:41:33 +00001375 // Assume "soft", but warn the user we are guessing.
1376 FloatABI = "soft";
1377 D.Diag(diag::warn_drv_assuming_mfloat_abi_is) << "soft";
Bruno Cardoso Lopes9284d212010-11-09 17:21:19 +00001378 }
1379
1380 if (FloatABI == "soft") {
1381 // Floating point operations and argument passing are soft.
1382 //
1383 // FIXME: This changes CPP defines, we need -target-soft-float.
1384 CmdArgs.push_back("-msoft-float");
Bruno Cardoso Lopes9284d212010-11-09 17:21:19 +00001385 } else {
1386 assert(FloatABI == "hard" && "Invalid float abi!");
1387 CmdArgs.push_back("-mhard-float");
1388 }
1389}
1390
Richard Sandiford5c92b9a2013-07-19 16:51:51 +00001391static const char *getSystemZTargetCPU(const ArgList &Args) {
1392 if (const Arg *A = Args.getLastArg(options::OPT_march_EQ))
1393 return A->getValue();
1394 return "z10";
1395}
1396
Chandler Carruth700d4e42013-01-13 11:46:33 +00001397static const char *getX86TargetCPU(const ArgList &Args,
1398 const llvm::Triple &Triple) {
1399 if (const Arg *A = Args.getLastArg(options::OPT_march_EQ)) {
Jim Grosbach32ca73e2013-11-16 00:53:35 +00001400 if (StringRef(A->getValue()) != "native") {
1401 if (Triple.isOSDarwin() && Triple.getArchName() == "x86_64h")
1402 return "core-avx2";
1403
Chandler Carruth700d4e42013-01-13 11:46:33 +00001404 return A->getValue();
Jim Grosbach32ca73e2013-11-16 00:53:35 +00001405 }
Chandler Carruth700d4e42013-01-13 11:46:33 +00001406
1407 // FIXME: Reject attempts to use -march=native unless the target matches
1408 // the host.
1409 //
1410 // FIXME: We should also incorporate the detected target features for use
1411 // with -native.
1412 std::string CPU = llvm::sys::getHostCPUName();
1413 if (!CPU.empty() && CPU != "generic")
1414 return Args.MakeArgString(CPU);
1415 }
1416
1417 // Select the default CPU if none was given (or detection failed).
1418
1419 if (Triple.getArch() != llvm::Triple::x86_64 &&
1420 Triple.getArch() != llvm::Triple::x86)
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001421 return nullptr; // This routine is only handling x86 targets.
Chandler Carruth700d4e42013-01-13 11:46:33 +00001422
1423 bool Is64Bit = Triple.getArch() == llvm::Triple::x86_64;
1424
1425 // FIXME: Need target hooks.
Jim Grosbach32ca73e2013-11-16 00:53:35 +00001426 if (Triple.isOSDarwin()) {
1427 if (Triple.getArchName() == "x86_64h")
1428 return "core-avx2";
Chandler Carruth700d4e42013-01-13 11:46:33 +00001429 return Is64Bit ? "core2" : "yonah";
Jim Grosbach32ca73e2013-11-16 00:53:35 +00001430 }
Chandler Carruth700d4e42013-01-13 11:46:33 +00001431
Stephen Hines651f13c2014-04-23 16:59:28 -07001432 // On Android use targets compatible with gcc
Chandler Carruth700d4e42013-01-13 11:46:33 +00001433 if (Triple.getEnvironment() == llvm::Triple::Android)
Stephen Hines651f13c2014-04-23 16:59:28 -07001434 return Is64Bit ? "x86-64" : "i686";
Chandler Carruth700d4e42013-01-13 11:46:33 +00001435
Benjamin Kramer39e4b0f2013-10-21 12:33:55 +00001436 // Everything else goes to x86-64 in 64-bit mode.
1437 if (Is64Bit)
1438 return "x86-64";
1439
1440 switch (Triple.getOS()) {
1441 case llvm::Triple::FreeBSD:
1442 case llvm::Triple::NetBSD:
1443 case llvm::Triple::OpenBSD:
1444 return "i486";
1445 case llvm::Triple::Haiku:
1446 return "i586";
1447 case llvm::Triple::Bitrig:
1448 return "i686";
1449 default:
1450 // Fallback to p4.
1451 return "pentium4";
1452 }
Chandler Carruth700d4e42013-01-13 11:46:33 +00001453}
1454
Rafael Espindolab330e402013-08-20 22:12:08 +00001455static std::string getCPUName(const ArgList &Args, const llvm::Triple &T) {
1456 switch(T.getArch()) {
1457 default:
1458 return "";
1459
Amara Emerson3bb1b5c2013-10-31 09:32:33 +00001460 case llvm::Triple::aarch64:
Stephen Hines651f13c2014-04-23 16:59:28 -07001461 case llvm::Triple::aarch64_be:
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001462 return getAArch64TargetCPU(Args);
Amara Emerson3bb1b5c2013-10-31 09:32:33 +00001463
Rafael Espindolab330e402013-08-20 22:12:08 +00001464 case llvm::Triple::arm:
Stephen Hines651f13c2014-04-23 16:59:28 -07001465 case llvm::Triple::armeb:
Rafael Espindolab330e402013-08-20 22:12:08 +00001466 case llvm::Triple::thumb:
Stephen Hines651f13c2014-04-23 16:59:28 -07001467 case llvm::Triple::thumbeb:
1468 return arm::getARMTargetCPU(Args, T);
Rafael Espindolab330e402013-08-20 22:12:08 +00001469
1470 case llvm::Triple::mips:
1471 case llvm::Triple::mipsel:
1472 case llvm::Triple::mips64:
1473 case llvm::Triple::mips64el: {
1474 StringRef CPUName;
1475 StringRef ABIName;
Stephen Hines176edba2014-12-01 14:53:08 -08001476 mips::getMipsCPUAndABI(Args, T, CPUName, ABIName);
Rafael Espindolab330e402013-08-20 22:12:08 +00001477 return CPUName;
1478 }
1479
1480 case llvm::Triple::ppc:
1481 case llvm::Triple::ppc64:
1482 case llvm::Triple::ppc64le: {
1483 std::string TargetCPUName = getPPCTargetCPU(Args);
1484 // LLVM may default to generating code for the native CPU,
1485 // but, like gcc, we default to a more generic option for
1486 // each architecture. (except on Darwin)
1487 if (TargetCPUName.empty() && !T.isOSDarwin()) {
1488 if (T.getArch() == llvm::Triple::ppc64)
1489 TargetCPUName = "ppc64";
1490 else if (T.getArch() == llvm::Triple::ppc64le)
1491 TargetCPUName = "ppc64le";
1492 else
1493 TargetCPUName = "ppc";
1494 }
1495 return TargetCPUName;
1496 }
1497
1498 case llvm::Triple::sparc:
Stephen Hines651f13c2014-04-23 16:59:28 -07001499 case llvm::Triple::sparcv9:
1500 if (const Arg *A = Args.getLastArg(options::OPT_mcpu_EQ))
Rafael Espindolab330e402013-08-20 22:12:08 +00001501 return A->getValue();
1502 return "";
1503
1504 case llvm::Triple::x86:
1505 case llvm::Triple::x86_64:
1506 return getX86TargetCPU(Args, T);
1507
1508 case llvm::Triple::hexagon:
1509 return "hexagon" + toolchains::Hexagon_TC::GetTargetCPU(Args).str();
1510
1511 case llvm::Triple::systemz:
1512 return getSystemZTargetCPU(Args);
1513
1514 case llvm::Triple::r600:
1515 return getR600TargetGPU(Args);
1516 }
1517}
1518
Stephen Hines651f13c2014-04-23 16:59:28 -07001519static void AddGoldPlugin(const ToolChain &ToolChain, const ArgList &Args,
1520 ArgStringList &CmdArgs) {
1521 // Tell the linker to load the plugin. This has to come before AddLinkerInputs
1522 // as gold requires -plugin to come before any -plugin-opt that -Wl might
1523 // forward.
1524 CmdArgs.push_back("-plugin");
1525 std::string Plugin = ToolChain.getDriver().Dir + "/../lib/LLVMgold.so";
1526 CmdArgs.push_back(Args.MakeArgString(Plugin));
1527
1528 // Try to pass driver level flags relevant to LTO code generation down to
1529 // the plugin.
1530
1531 // Handle flags for selecting CPU variants.
1532 std::string CPU = getCPUName(Args, ToolChain.getTriple());
1533 if (!CPU.empty())
1534 CmdArgs.push_back(Args.MakeArgString(Twine("-plugin-opt=mcpu=") + CPU));
1535}
1536
Stephen Hines176edba2014-12-01 14:53:08 -08001537static void getX86TargetFeatures(const Driver & D,
1538 const llvm::Triple &Triple,
Jim Grosbach32ca73e2013-11-16 00:53:35 +00001539 const ArgList &Args,
Rafael Espindola146dbbf2013-08-21 16:39:20 +00001540 std::vector<const char *> &Features) {
Jim Grosbach32ca73e2013-11-16 00:53:35 +00001541 if (Triple.getArchName() == "x86_64h") {
1542 // x86_64h implies quite a few of the more modern subtarget features
1543 // for Haswell class CPUs, but not all of them. Opt-out of a few.
1544 Features.push_back("-rdrnd");
1545 Features.push_back("-aes");
1546 Features.push_back("-pclmul");
1547 Features.push_back("-rtm");
1548 Features.push_back("-hle");
1549 Features.push_back("-fsgsbase");
1550 }
1551
Stephen Hinesc568f1e2014-07-21 00:47:37 -07001552 // Add features to comply with gcc on Android
Stephen Hines651f13c2014-04-23 16:59:28 -07001553 if (Triple.getEnvironment() == llvm::Triple::Android) {
Stephen Hinesc568f1e2014-07-21 00:47:37 -07001554 if (Triple.getArch() == llvm::Triple::x86_64) {
1555 Features.push_back("+sse4.2");
1556 Features.push_back("+popcnt");
1557 } else
1558 Features.push_back("+ssse3");
Stephen Hines651f13c2014-04-23 16:59:28 -07001559 }
1560
Stephen Hines176edba2014-12-01 14:53:08 -08001561 // Set features according to the -arch flag on MSVC
1562 if (Arg *A = Args.getLastArg(options::OPT__SLASH_arch)) {
1563 StringRef Arch = A->getValue();
1564 bool ArchUsed = false;
1565 // First, look for flags that are shared in x86 and x86-64.
1566 if (Triple.getArch() == llvm::Triple::x86_64 ||
1567 Triple.getArch() == llvm::Triple::x86) {
1568 if (Arch == "AVX" || Arch == "AVX2") {
1569 ArchUsed = true;
1570 Features.push_back(Args.MakeArgString("+" + Arch.lower()));
1571 }
1572 }
1573 // Then, look for x86-specific flags.
1574 if (Triple.getArch() == llvm::Triple::x86) {
1575 if (Arch == "IA32") {
1576 ArchUsed = true;
1577 } else if (Arch == "SSE" || Arch == "SSE2") {
1578 ArchUsed = true;
1579 Features.push_back(Args.MakeArgString("+" + Arch.lower()));
1580 }
1581 }
1582 if (!ArchUsed)
1583 D.Diag(clang::diag::warn_drv_unused_argument) << A->getAsString(Args);
1584 }
1585
Jim Grosbach32ca73e2013-11-16 00:53:35 +00001586 // Now add any that the user explicitly requested on the command line,
1587 // which may override the defaults.
Rafael Espindola146dbbf2013-08-21 16:39:20 +00001588 for (arg_iterator it = Args.filtered_begin(options::OPT_m_x86_Features_Group),
1589 ie = Args.filtered_end();
1590 it != ie; ++it) {
1591 StringRef Name = (*it)->getOption().getName();
1592 (*it)->claim();
1593
1594 // Skip over "-m".
1595 assert(Name.startswith("m") && "Invalid feature name.");
1596 Name = Name.substr(1);
1597
1598 bool IsNegative = Name.startswith("no-");
1599 if (IsNegative)
1600 Name = Name.substr(3);
1601
1602 Features.push_back(Args.MakeArgString((IsNegative ? "-" : "+") + Name));
1603 }
1604}
1605
Daniel Dunbar6acda162009-09-09 22:33:08 +00001606void Clang::AddX86TargetArgs(const ArgList &Args,
1607 ArgStringList &CmdArgs) const {
Daniel Dunbare6ad3f92009-09-10 22:59:57 +00001608 if (!Args.hasFlag(options::OPT_mred_zone,
1609 options::OPT_mno_red_zone,
1610 true) ||
1611 Args.hasArg(options::OPT_mkernel) ||
1612 Args.hasArg(options::OPT_fapple_kext))
Daniel Dunbar66861e02009-11-20 22:21:36 +00001613 CmdArgs.push_back("-disable-red-zone");
Daniel Dunbare6ad3f92009-09-10 22:59:57 +00001614
Bob Wilsonf0c54562013-02-10 16:01:41 +00001615 // Default to avoid implicit floating-point for kernel/kext code, but allow
1616 // that to be overridden with -mno-soft-float.
1617 bool NoImplicitFloat = (Args.hasArg(options::OPT_mkernel) ||
1618 Args.hasArg(options::OPT_fapple_kext));
1619 if (Arg *A = Args.getLastArg(options::OPT_msoft_float,
1620 options::OPT_mno_soft_float,
Bob Wilsonf6f77bd2013-04-11 18:53:25 +00001621 options::OPT_mimplicit_float,
Bob Wilsonf0c54562013-02-10 16:01:41 +00001622 options::OPT_mno_implicit_float)) {
1623 const Option &O = A->getOption();
1624 NoImplicitFloat = (O.matches(options::OPT_mno_implicit_float) ||
1625 O.matches(options::OPT_msoft_float));
1626 }
1627 if (NoImplicitFloat)
Daniel Dunbar66861e02009-11-20 22:21:36 +00001628 CmdArgs.push_back("-no-implicit-float");
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001629
1630 if (Arg *A = Args.getLastArg(options::OPT_masm_EQ)) {
1631 StringRef Value = A->getValue();
1632 if (Value == "intel" || Value == "att") {
1633 CmdArgs.push_back("-mllvm");
1634 CmdArgs.push_back(Args.MakeArgString("-x86-asm-syntax=" + Value));
1635 } else {
1636 getToolChain().getDriver().Diag(diag::err_drv_unsupported_option_argument)
1637 << A->getOption().getName() << Value;
1638 }
1639 }
Daniel Dunbar6acda162009-09-09 22:33:08 +00001640}
1641
Matthew Curtis33c95f12012-12-06 17:49:03 +00001642static inline bool HasPICArg(const ArgList &Args) {
1643 return Args.hasArg(options::OPT_fPIC)
1644 || Args.hasArg(options::OPT_fpic);
1645}
1646
1647static Arg *GetLastSmallDataThresholdArg(const ArgList &Args) {
1648 return Args.getLastArg(options::OPT_G,
1649 options::OPT_G_EQ,
1650 options::OPT_msmall_data_threshold_EQ);
1651}
1652
1653static std::string GetHexagonSmallDataThresholdValue(const ArgList &Args) {
1654 std::string value;
1655 if (HasPICArg(Args))
1656 value = "0";
1657 else if (Arg *A = GetLastSmallDataThresholdArg(Args)) {
1658 value = A->getValue();
1659 A->claim();
1660 }
1661 return value;
1662}
1663
Tony Linthicum96319392011-12-12 21:14:55 +00001664void Clang::AddHexagonTargetArgs(const ArgList &Args,
1665 ArgStringList &CmdArgs) const {
Tony Linthicum96319392011-12-12 21:14:55 +00001666 CmdArgs.push_back("-fno-signed-char");
Matthew Curtis1dbaef52012-12-07 13:52:44 +00001667 CmdArgs.push_back("-mqdsp6-compat");
1668 CmdArgs.push_back("-Wreturn-type");
Tony Linthicum96319392011-12-12 21:14:55 +00001669
Matthew Curtis33c95f12012-12-06 17:49:03 +00001670 std::string SmallDataThreshold = GetHexagonSmallDataThresholdValue(Args);
1671 if (!SmallDataThreshold.empty()) {
Tony Linthicum96319392011-12-12 21:14:55 +00001672 CmdArgs.push_back ("-mllvm");
Matthew Curtis33c95f12012-12-06 17:49:03 +00001673 CmdArgs.push_back(Args.MakeArgString(
1674 "-hexagon-small-data-threshold=" + SmallDataThreshold));
Tony Linthicum96319392011-12-12 21:14:55 +00001675 }
1676
Sirish Pande5f9688b2012-05-10 20:19:54 +00001677 if (!Args.hasArg(options::OPT_fno_short_enums))
1678 CmdArgs.push_back("-fshort-enums");
1679 if (Args.getLastArg(options::OPT_mieee_rnd_near)) {
1680 CmdArgs.push_back ("-mllvm");
1681 CmdArgs.push_back ("-enable-hexagon-ieee-rnd-near");
1682 }
Tony Linthicum96319392011-12-12 21:14:55 +00001683 CmdArgs.push_back ("-mllvm");
1684 CmdArgs.push_back ("-machine-sink-split=0");
1685}
1686
Stephen Hines176edba2014-12-01 14:53:08 -08001687// Decode AArch64 features from string like +[no]featureA+[no]featureB+...
1688static bool DecodeAArch64Features(const Driver &D, StringRef text,
1689 std::vector<const char *> &Features) {
1690 SmallVector<StringRef, 8> Split;
1691 text.split(Split, StringRef("+"), -1, false);
1692
1693 for (unsigned I = 0, E = Split.size(); I != E; ++I) {
1694 const char *result = llvm::StringSwitch<const char *>(Split[I])
1695 .Case("fp", "+fp-armv8")
1696 .Case("simd", "+neon")
1697 .Case("crc", "+crc")
1698 .Case("crypto", "+crypto")
1699 .Case("nofp", "-fp-armv8")
1700 .Case("nosimd", "-neon")
1701 .Case("nocrc", "-crc")
1702 .Case("nocrypto", "-crypto")
1703 .Default(nullptr);
1704 if (result)
1705 Features.push_back(result);
1706 else if (Split[I] == "neon" || Split[I] == "noneon")
1707 D.Diag(diag::err_drv_no_neon_modifier);
1708 else
1709 return false;
1710 }
1711 return true;
1712}
1713
1714// Check if the CPU name and feature modifiers in -mcpu are legal. If yes,
1715// decode CPU and feature.
1716static bool DecodeAArch64Mcpu(const Driver &D, StringRef Mcpu, StringRef &CPU,
1717 std::vector<const char *> &Features) {
1718 std::pair<StringRef, StringRef> Split = Mcpu.split("+");
1719 CPU = Split.first;
1720 if (CPU == "cyclone" || CPU == "cortex-a53" || CPU == "cortex-a57") {
1721 Features.push_back("+neon");
1722 Features.push_back("+crc");
1723 Features.push_back("+crypto");
1724 } else if (CPU == "generic") {
1725 Features.push_back("+neon");
1726 } else {
1727 return false;
1728 }
1729
1730 if (Split.second.size() && !DecodeAArch64Features(D, Split.second, Features))
1731 return false;
1732
1733 return true;
1734}
1735
1736static bool
1737getAArch64ArchFeaturesFromMarch(const Driver &D, StringRef March,
1738 const ArgList &Args,
1739 std::vector<const char *> &Features) {
1740 std::pair<StringRef, StringRef> Split = March.split("+");
1741 if (Split.first != "armv8-a")
1742 return false;
1743
1744 if (Split.second.size() && !DecodeAArch64Features(D, Split.second, Features))
1745 return false;
1746
1747 return true;
1748}
1749
1750static bool
1751getAArch64ArchFeaturesFromMcpu(const Driver &D, StringRef Mcpu,
1752 const ArgList &Args,
1753 std::vector<const char *> &Features) {
1754 StringRef CPU;
1755 if (!DecodeAArch64Mcpu(D, Mcpu, CPU, Features))
1756 return false;
1757
1758 return true;
1759}
1760
1761static bool
1762getAArch64MicroArchFeaturesFromMtune(const Driver &D, StringRef Mtune,
1763 const ArgList &Args,
1764 std::vector<const char *> &Features) {
1765 // Handle CPU name is 'native'.
1766 if (Mtune == "native")
1767 Mtune = llvm::sys::getHostCPUName();
1768 if (Mtune == "cyclone") {
1769 Features.push_back("+zcm");
1770 Features.push_back("+zcz");
1771 }
1772 return true;
1773}
1774
1775static bool
1776getAArch64MicroArchFeaturesFromMcpu(const Driver &D, StringRef Mcpu,
1777 const ArgList &Args,
1778 std::vector<const char *> &Features) {
1779 StringRef CPU;
1780 std::vector<const char *> DecodedFeature;
1781 if (!DecodeAArch64Mcpu(D, Mcpu, CPU, DecodedFeature))
1782 return false;
1783
1784 return getAArch64MicroArchFeaturesFromMtune(D, CPU, Args, Features);
1785}
1786
Rafael Espindola146dbbf2013-08-21 16:39:20 +00001787static void getAArch64TargetFeatures(const Driver &D, const ArgList &Args,
1788 std::vector<const char *> &Features) {
Stephen Hines176edba2014-12-01 14:53:08 -08001789 Arg *A;
1790 bool success = true;
1791 // Enable NEON by default.
1792 Features.push_back("+neon");
1793 if ((A = Args.getLastArg(options::OPT_march_EQ)))
1794 success = getAArch64ArchFeaturesFromMarch(D, A->getValue(), Args, Features);
1795 else if ((A = Args.getLastArg(options::OPT_mcpu_EQ)))
1796 success = getAArch64ArchFeaturesFromMcpu(D, A->getValue(), Args, Features);
1797 else if (Args.hasArg(options::OPT_arch))
1798 success = getAArch64ArchFeaturesFromMcpu(D, getAArch64TargetCPU(Args), Args,
1799 Features);
1800
1801 if (success && (A = Args.getLastArg(options::OPT_mtune_EQ)))
1802 success =
1803 getAArch64MicroArchFeaturesFromMtune(D, A->getValue(), Args, Features);
1804 else if (success && (A = Args.getLastArg(options::OPT_mcpu_EQ)))
1805 success =
1806 getAArch64MicroArchFeaturesFromMcpu(D, A->getValue(), Args, Features);
1807 else if (Args.hasArg(options::OPT_arch))
1808 success = getAArch64MicroArchFeaturesFromMcpu(D, getAArch64TargetCPU(Args),
1809 Args, Features);
1810
1811 if (!success)
1812 D.Diag(diag::err_drv_clang_unsupported) << A->getAsString(Args);
Stephen Hines651f13c2014-04-23 16:59:28 -07001813
1814 if (Args.getLastArg(options::OPT_mgeneral_regs_only)) {
1815 Features.push_back("-fp-armv8");
1816 Features.push_back("-crypto");
1817 Features.push_back("-neon");
1818 }
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001819
1820 // En/disable crc
1821 if (Arg *A = Args.getLastArg(options::OPT_mcrc,
1822 options::OPT_mnocrc)) {
1823 if (A->getOption().matches(options::OPT_mcrc))
1824 Features.push_back("+crc");
1825 else
1826 Features.push_back("-crc");
1827 }
Rafael Espindola146dbbf2013-08-21 16:39:20 +00001828}
1829
1830static void getTargetFeatures(const Driver &D, const llvm::Triple &Triple,
Stephen Hines651f13c2014-04-23 16:59:28 -07001831 const ArgList &Args, ArgStringList &CmdArgs,
1832 bool ForAS) {
Rafael Espindola146dbbf2013-08-21 16:39:20 +00001833 std::vector<const char *> Features;
1834 switch (Triple.getArch()) {
1835 default:
1836 break;
1837 case llvm::Triple::mips:
1838 case llvm::Triple::mipsel:
1839 case llvm::Triple::mips64:
1840 case llvm::Triple::mips64el:
Stephen Hines176edba2014-12-01 14:53:08 -08001841 getMIPSTargetFeatures(D, Triple, Args, Features);
Rafael Espindola146dbbf2013-08-21 16:39:20 +00001842 break;
1843
1844 case llvm::Triple::arm:
Stephen Hines651f13c2014-04-23 16:59:28 -07001845 case llvm::Triple::armeb:
Rafael Espindola146dbbf2013-08-21 16:39:20 +00001846 case llvm::Triple::thumb:
Stephen Hines651f13c2014-04-23 16:59:28 -07001847 case llvm::Triple::thumbeb:
1848 getARMTargetFeatures(D, Triple, Args, Features, ForAS);
Rafael Espindola146dbbf2013-08-21 16:39:20 +00001849 break;
1850
1851 case llvm::Triple::ppc:
1852 case llvm::Triple::ppc64:
1853 case llvm::Triple::ppc64le:
1854 getPPCTargetFeatures(Args, Features);
1855 break;
1856 case llvm::Triple::sparc:
Stephen Hines176edba2014-12-01 14:53:08 -08001857 case llvm::Triple::sparcv9:
Rafael Espindola146dbbf2013-08-21 16:39:20 +00001858 getSparcTargetFeatures(Args, Features);
1859 break;
1860 case llvm::Triple::aarch64:
Stephen Hines651f13c2014-04-23 16:59:28 -07001861 case llvm::Triple::aarch64_be:
Rafael Espindola146dbbf2013-08-21 16:39:20 +00001862 getAArch64TargetFeatures(D, Args, Features);
1863 break;
1864 case llvm::Triple::x86:
1865 case llvm::Triple::x86_64:
Stephen Hines176edba2014-12-01 14:53:08 -08001866 getX86TargetFeatures(D, Triple, Args, Features);
Rafael Espindola146dbbf2013-08-21 16:39:20 +00001867 break;
1868 }
Rafael Espindolabc1e5452013-08-21 17:34:32 +00001869
1870 // Find the last of each feature.
1871 llvm::StringMap<unsigned> LastOpt;
1872 for (unsigned I = 0, N = Features.size(); I < N; ++I) {
1873 const char *Name = Features[I];
1874 assert(Name[0] == '-' || Name[0] == '+');
1875 LastOpt[Name + 1] = I;
1876 }
1877
1878 for (unsigned I = 0, N = Features.size(); I < N; ++I) {
1879 // If this feature was overridden, ignore it.
1880 const char *Name = Features[I];
1881 llvm::StringMap<unsigned>::iterator LastI = LastOpt.find(Name + 1);
1882 assert(LastI != LastOpt.end());
1883 unsigned Last = LastI->second;
1884 if (Last != I)
1885 continue;
1886
Rafael Espindola146dbbf2013-08-21 16:39:20 +00001887 CmdArgs.push_back("-target-feature");
Rafael Espindolabc1e5452013-08-21 17:34:32 +00001888 CmdArgs.push_back(Name);
Rafael Espindola146dbbf2013-08-21 16:39:20 +00001889 }
Tim Northoverb793f0d2013-08-01 09:23:19 +00001890}
1891
Eric Christopher88b7cf02011-08-19 00:30:14 +00001892static bool
John McCall260611a2012-06-20 06:18:46 +00001893shouldUseExceptionTablesForObjCExceptions(const ObjCRuntime &runtime,
Anders Carlsson525544d2011-02-28 00:44:51 +00001894 const llvm::Triple &Triple) {
1895 // We use the zero-cost exception tables for Objective-C if the non-fragile
1896 // ABI is enabled or when compiling for x86_64 and ARM on Snow Leopard and
1897 // later.
John McCall260611a2012-06-20 06:18:46 +00001898 if (runtime.isNonFragile())
Anders Carlsson525544d2011-02-28 00:44:51 +00001899 return true;
1900
Stephen Hines651f13c2014-04-23 16:59:28 -07001901 if (!Triple.isMacOSX())
Anders Carlsson525544d2011-02-28 00:44:51 +00001902 return false;
1903
Eric Christopheraa7333c2011-07-02 00:20:22 +00001904 return (!Triple.isMacOSXVersionLT(10,5) &&
Anders Carlsson525544d2011-02-28 00:44:51 +00001905 (Triple.getArch() == llvm::Triple::x86_64 ||
Eric Christopher88b7cf02011-08-19 00:30:14 +00001906 Triple.getArch() == llvm::Triple::arm));
Anders Carlsson525544d2011-02-28 00:44:51 +00001907}
1908
Stephen Hines651f13c2014-04-23 16:59:28 -07001909namespace {
1910 struct ExceptionSettings {
1911 bool ExceptionsEnabled;
1912 bool ShouldUseExceptionTables;
1913 ExceptionSettings() : ExceptionsEnabled(false),
1914 ShouldUseExceptionTables(false) {}
1915 };
1916} // end anonymous namespace.
1917
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001918// exceptionSettings() exists to share the logic between -cc1 and linker
1919// invocations.
Stephen Hines651f13c2014-04-23 16:59:28 -07001920static ExceptionSettings exceptionSettings(const ArgList &Args,
1921 const llvm::Triple &Triple) {
1922 ExceptionSettings ES;
1923
1924 // Are exceptions enabled by default?
1925 ES.ExceptionsEnabled = (Triple.getArch() != llvm::Triple::xcore);
1926
1927 // This keeps track of whether exceptions were explicitly turned on or off.
1928 bool DidHaveExplicitExceptionFlag = false;
1929
1930 if (Arg *A = Args.getLastArg(options::OPT_fexceptions,
1931 options::OPT_fno_exceptions)) {
1932 if (A->getOption().matches(options::OPT_fexceptions))
1933 ES.ExceptionsEnabled = true;
1934 else
1935 ES.ExceptionsEnabled = false;
1936
1937 DidHaveExplicitExceptionFlag = true;
1938 }
1939
1940 // Exception tables and cleanups can be enabled with -fexceptions even if the
1941 // language itself doesn't support exceptions.
1942 if (ES.ExceptionsEnabled && DidHaveExplicitExceptionFlag)
1943 ES.ShouldUseExceptionTables = true;
1944
1945 return ES;
1946}
1947
Anders Carlsson15348ae2011-02-28 02:27:16 +00001948/// addExceptionArgs - Adds exception related arguments to the driver command
1949/// arguments. There's a master flag, -fexceptions and also language specific
1950/// flags to enable/disable C++ and Objective-C exceptions.
1951/// This makes it possible to for example disable C++ exceptions but enable
1952/// Objective-C exceptions.
1953static void addExceptionArgs(const ArgList &Args, types::ID InputType,
1954 const llvm::Triple &Triple,
Fariborz Jahanian15b77312012-04-04 18:28:00 +00001955 bool KernelOrKext,
John McCall260611a2012-06-20 06:18:46 +00001956 const ObjCRuntime &objcRuntime,
Anders Carlsson15348ae2011-02-28 02:27:16 +00001957 ArgStringList &CmdArgs) {
Chad Rosierafc4baa2012-03-26 22:04:46 +00001958 if (KernelOrKext) {
1959 // -mkernel and -fapple-kext imply no exceptions, so claim exception related
1960 // arguments now to avoid warnings about unused arguments.
1961 Args.ClaimAllArgs(options::OPT_fexceptions);
1962 Args.ClaimAllArgs(options::OPT_fno_exceptions);
1963 Args.ClaimAllArgs(options::OPT_fobjc_exceptions);
1964 Args.ClaimAllArgs(options::OPT_fno_objc_exceptions);
1965 Args.ClaimAllArgs(options::OPT_fcxx_exceptions);
1966 Args.ClaimAllArgs(options::OPT_fno_cxx_exceptions);
Anders Carlsson15348ae2011-02-28 02:27:16 +00001967 return;
Chad Rosierafc4baa2012-03-26 22:04:46 +00001968 }
Anders Carlsson15348ae2011-02-28 02:27:16 +00001969
Stephen Hines651f13c2014-04-23 16:59:28 -07001970 // Gather the exception settings from the command line arguments.
1971 ExceptionSettings ES = exceptionSettings(Args, Triple);
Daniel Dunbar1a2cd4f2010-09-14 23:12:31 +00001972
Daniel Dunbard47ea692011-03-17 23:28:31 +00001973 // Obj-C exceptions are enabled by default, regardless of -fexceptions. This
1974 // is not necessarily sensible, but follows GCC.
1975 if (types::isObjC(InputType) &&
Eric Christopher88b7cf02011-08-19 00:30:14 +00001976 Args.hasFlag(options::OPT_fobjc_exceptions,
Daniel Dunbard47ea692011-03-17 23:28:31 +00001977 options::OPT_fno_objc_exceptions,
1978 true)) {
1979 CmdArgs.push_back("-fobjc-exceptions");
Anders Carlsson15348ae2011-02-28 02:27:16 +00001980
Stephen Hines651f13c2014-04-23 16:59:28 -07001981 ES.ShouldUseExceptionTables |=
John McCall260611a2012-06-20 06:18:46 +00001982 shouldUseExceptionTablesForObjCExceptions(objcRuntime, Triple);
Anders Carlsson15348ae2011-02-28 02:27:16 +00001983 }
1984
1985 if (types::isCXX(InputType)) {
Stephen Hines651f13c2014-04-23 16:59:28 -07001986 bool CXXExceptionsEnabled = ES.ExceptionsEnabled;
Anders Carlsson15348ae2011-02-28 02:27:16 +00001987
Eric Christopher88b7cf02011-08-19 00:30:14 +00001988 if (Arg *A = Args.getLastArg(options::OPT_fcxx_exceptions,
1989 options::OPT_fno_cxx_exceptions,
Anders Carlsson15348ae2011-02-28 02:27:16 +00001990 options::OPT_fexceptions,
1991 options::OPT_fno_exceptions)) {
1992 if (A->getOption().matches(options::OPT_fcxx_exceptions))
1993 CXXExceptionsEnabled = true;
Chandler Carruth43f220f2011-02-28 07:25:18 +00001994 else if (A->getOption().matches(options::OPT_fno_cxx_exceptions))
Anders Carlsson15348ae2011-02-28 02:27:16 +00001995 CXXExceptionsEnabled = false;
1996 }
1997
1998 if (CXXExceptionsEnabled) {
1999 CmdArgs.push_back("-fcxx-exceptions");
2000
Stephen Hines651f13c2014-04-23 16:59:28 -07002001 ES.ShouldUseExceptionTables = true;
Anders Carlsson15348ae2011-02-28 02:27:16 +00002002 }
2003 }
2004
Stephen Hines651f13c2014-04-23 16:59:28 -07002005 if (ES.ShouldUseExceptionTables)
Anders Carlsson15348ae2011-02-28 02:27:16 +00002006 CmdArgs.push_back("-fexceptions");
Rafael Espindolaf759df02009-10-01 13:33:33 +00002007}
2008
Daniel Dunbarf4910132013-04-16 18:21:19 +00002009static bool ShouldDisableAutolink(const ArgList &Args,
2010 const ToolChain &TC) {
2011 bool Default = true;
2012 if (TC.getTriple().isOSDarwin()) {
2013 // The native darwin assembler doesn't support the linker_option directives,
2014 // so we disable them if we think the .s file will be passed to it.
2015 Default = TC.useIntegratedAs();
2016 }
2017 return !Args.hasFlag(options::OPT_fautolink, options::OPT_fno_autolink,
2018 Default);
2019}
2020
Ted Kremenekc06fcdf2013-03-12 17:02:12 +00002021static bool ShouldDisableDwarfDirectory(const ArgList &Args,
2022 const ToolChain &TC) {
Nick Lewyckyea523d72011-10-17 23:05:52 +00002023 bool UseDwarfDirectory = Args.hasFlag(options::OPT_fdwarf_directory_asm,
2024 options::OPT_fno_dwarf_directory_asm,
Rafael Espindolaaf370e62013-03-18 18:10:27 +00002025 TC.useIntegratedAs());
Nick Lewyckyea523d72011-10-17 23:05:52 +00002026 return !UseDwarfDirectory;
2027}
2028
Joerg Sonnenberger359cf922011-05-06 14:35:16 +00002029/// \brief Check whether the given input tree contains any compilation actions.
2030static bool ContainsCompileAction(const Action *A) {
Chris Lattner5f9e2722011-07-23 10:55:15 +00002031 if (isa<CompileJobAction>(A))
Joerg Sonnenberger359cf922011-05-06 14:35:16 +00002032 return true;
2033
Stephen Hinesc568f1e2014-07-21 00:47:37 -07002034 for (const auto &Act : *A)
2035 if (ContainsCompileAction(Act))
Joerg Sonnenberger359cf922011-05-06 14:35:16 +00002036 return true;
2037
2038 return false;
2039}
2040
2041/// \brief Check if -relax-all should be passed to the internal assembler.
2042/// This is done by default when compiling non-assembler source with -O0.
2043static bool UseRelaxAll(Compilation &C, const ArgList &Args) {
2044 bool RelaxDefault = true;
2045
2046 if (Arg *A = Args.getLastArg(options::OPT_O_Group))
2047 RelaxDefault = A->getOption().matches(options::OPT_O0);
2048
2049 if (RelaxDefault) {
2050 RelaxDefault = false;
Stephen Hinesc568f1e2014-07-21 00:47:37 -07002051 for (const auto &Act : C.getActions()) {
2052 if (ContainsCompileAction(Act)) {
Joerg Sonnenberger359cf922011-05-06 14:35:16 +00002053 RelaxDefault = true;
2054 break;
2055 }
2056 }
2057 }
2058
2059 return Args.hasFlag(options::OPT_mrelax_all, options::OPT_mno_relax_all,
2060 RelaxDefault);
2061}
2062
David Blaikie73168db2013-07-25 21:19:01 +00002063static void CollectArgsForIntegratedAssembler(Compilation &C,
2064 const ArgList &Args,
2065 ArgStringList &CmdArgs,
2066 const Driver &D) {
2067 if (UseRelaxAll(C, Args))
2068 CmdArgs.push_back("-mrelax-all");
2069
David Peixotto4ca9eae2013-11-14 22:52:58 +00002070 // When passing -I arguments to the assembler we sometimes need to
David Peixotto2317f7b2013-11-14 22:58:17 +00002071 // unconditionally take the next argument. For example, when parsing
David Peixotto4ca9eae2013-11-14 22:52:58 +00002072 // '-Wa,-I -Wa,foo' we need to accept the -Wa,foo arg after seeing the
2073 // -Wa,-I arg and when parsing '-Wa,-I,foo' we need to accept the 'foo'
2074 // arg after parsing the '-I' arg.
2075 bool TakeNextArg = false;
2076
David Blaikie73168db2013-07-25 21:19:01 +00002077 // When using an integrated assembler, translate -Wa, and -Xassembler
2078 // options.
Stephen Hines651f13c2014-04-23 16:59:28 -07002079 bool CompressDebugSections = false;
David Blaikie73168db2013-07-25 21:19:01 +00002080 for (arg_iterator it = Args.filtered_begin(options::OPT_Wa_COMMA,
2081 options::OPT_Xassembler),
2082 ie = Args.filtered_end(); it != ie; ++it) {
2083 const Arg *A = *it;
2084 A->claim();
2085
2086 for (unsigned i = 0, e = A->getNumValues(); i != e; ++i) {
2087 StringRef Value = A->getValue(i);
David Peixotto4ca9eae2013-11-14 22:52:58 +00002088 if (TakeNextArg) {
2089 CmdArgs.push_back(Value.data());
2090 TakeNextArg = false;
2091 continue;
2092 }
David Blaikie73168db2013-07-25 21:19:01 +00002093
2094 if (Value == "-force_cpusubtype_ALL") {
2095 // Do nothing, this is the default and we don't support anything else.
2096 } else if (Value == "-L") {
2097 CmdArgs.push_back("-msave-temp-labels");
2098 } else if (Value == "--fatal-warnings") {
Stephen Hines176edba2014-12-01 14:53:08 -08002099 CmdArgs.push_back("-massembler-fatal-warnings");
David Blaikie73168db2013-07-25 21:19:01 +00002100 } else if (Value == "--noexecstack") {
2101 CmdArgs.push_back("-mnoexecstack");
Stephen Hines651f13c2014-04-23 16:59:28 -07002102 } else if (Value == "-compress-debug-sections" ||
2103 Value == "--compress-debug-sections") {
2104 CompressDebugSections = true;
2105 } else if (Value == "-nocompress-debug-sections" ||
2106 Value == "--nocompress-debug-sections") {
2107 CompressDebugSections = false;
David Peixotto4ca9eae2013-11-14 22:52:58 +00002108 } else if (Value.startswith("-I")) {
2109 CmdArgs.push_back(Value.data());
2110 // We need to consume the next argument if the current arg is a plain
2111 // -I. The next arg will be the include directory.
2112 if (Value == "-I")
2113 TakeNextArg = true;
Stephen Hines6bcf27b2014-05-29 04:14:42 -07002114 } else if (Value.startswith("-gdwarf-")) {
2115 CmdArgs.push_back(Value.data());
David Blaikie73168db2013-07-25 21:19:01 +00002116 } else {
2117 D.Diag(diag::err_drv_unsupported_option_argument)
2118 << A->getOption().getName() << Value;
2119 }
2120 }
2121 }
Stephen Hines651f13c2014-04-23 16:59:28 -07002122 if (CompressDebugSections) {
2123 if (llvm::zlib::isAvailable())
2124 CmdArgs.push_back("-compress-debug-sections");
2125 else
2126 D.Diag(diag::warn_debug_compression_unavailable);
2127 }
David Blaikie73168db2013-07-25 21:19:01 +00002128}
2129
Stephen Hines651f13c2014-04-23 16:59:28 -07002130// Until ARM libraries are build separately, we have them all in one library
2131static StringRef getArchNameForCompilerRTLib(const ToolChain &TC) {
2132 if (TC.getArch() == llvm::Triple::arm ||
2133 TC.getArch() == llvm::Triple::armeb)
2134 return "arm";
2135 else
2136 return TC.getArchName();
2137}
2138
2139static SmallString<128> getCompilerRTLibDir(const ToolChain &TC) {
2140 // The runtimes are located in the OS-specific resource directory.
2141 SmallString<128> Res(TC.getDriver().ResourceDir);
2142 const llvm::Triple &Triple = TC.getTriple();
2143 // TC.getOS() yield "freebsd10.0" whereas "freebsd" is expected.
2144 StringRef OSLibName = (Triple.getOS() == llvm::Triple::FreeBSD) ?
2145 "freebsd" : TC.getOS();
2146 llvm::sys::path::append(Res, "lib", OSLibName);
2147 return Res;
2148}
2149
Stephen Hines6bcf27b2014-05-29 04:14:42 -07002150// This adds the static libclang_rt.builtins-arch.a directly to the command line
Stephen Hines651f13c2014-04-23 16:59:28 -07002151// FIXME: Make sure we can also emit shared objects if they're requested
2152// and available, check for possible errors, etc.
2153static void addClangRTLinux(
2154 const ToolChain &TC, const ArgList &Args, ArgStringList &CmdArgs) {
2155 SmallString<128> LibClangRT = getCompilerRTLibDir(TC);
Stephen Hines6bcf27b2014-05-29 04:14:42 -07002156 llvm::sys::path::append(LibClangRT, Twine("libclang_rt.builtins-") +
2157 getArchNameForCompilerRTLib(TC) +
2158 ".a");
Stephen Hines651f13c2014-04-23 16:59:28 -07002159
2160 CmdArgs.push_back(Args.MakeArgString(LibClangRT));
2161 CmdArgs.push_back("-lgcc_s");
2162 if (TC.getDriver().CCCIsCXX())
2163 CmdArgs.push_back("-lgcc_eh");
2164}
2165
Stephen Hines176edba2014-12-01 14:53:08 -08002166static void addClangRTWindows(const ToolChain &TC, const ArgList &Args,
2167 ArgStringList &CmdArgs) {
2168 SmallString<128> LibClangRT = getCompilerRTLibDir(TC);
2169 llvm::sys::path::append(LibClangRT, Twine("libclang_rt.builtins-") +
2170 getArchNameForCompilerRTLib(TC) + ".lib");
2171 CmdArgs.push_back(Args.MakeArgString(LibClangRT));
2172}
2173
Stephen Hines651f13c2014-04-23 16:59:28 -07002174static void addProfileRT(
Chandler Carruth9db37cd2013-06-23 11:28:48 +00002175 const ToolChain &TC, const ArgList &Args, ArgStringList &CmdArgs) {
Stephen Hines176edba2014-12-01 14:53:08 -08002176 if (!(Args.hasFlag(options::OPT_fprofile_arcs, options::OPT_fno_profile_arcs,
2177 false) ||
Chandler Carruth9db37cd2013-06-23 11:28:48 +00002178 Args.hasArg(options::OPT_fprofile_generate) ||
Stephen Hines651f13c2014-04-23 16:59:28 -07002179 Args.hasArg(options::OPT_fprofile_instr_generate) ||
Chandler Carruth9db37cd2013-06-23 11:28:48 +00002180 Args.hasArg(options::OPT_fcreate_profile) ||
2181 Args.hasArg(options::OPT_coverage)))
2182 return;
2183
Stephen Hines651f13c2014-04-23 16:59:28 -07002184 SmallString<128> LibProfile = getCompilerRTLibDir(TC);
Stephen Hines176edba2014-12-01 14:53:08 -08002185 llvm::sys::path::append(LibProfile, Twine("libclang_rt.profile-") +
2186 getArchNameForCompilerRTLib(TC) +
2187 ".a");
Chandler Carruth9db37cd2013-06-23 11:28:48 +00002188
2189 CmdArgs.push_back(Args.MakeArgString(LibProfile));
2190}
2191
Stephen Hines651f13c2014-04-23 16:59:28 -07002192static SmallString<128> getSanitizerRTLibName(const ToolChain &TC,
Stephen Hines176edba2014-12-01 14:53:08 -08002193 StringRef Sanitizer,
Stephen Hines651f13c2014-04-23 16:59:28 -07002194 bool Shared) {
2195 // Sanitizer runtime has name "libclang_rt.<Sanitizer>-<ArchName>.{a,so}"
2196 // (or "libclang_rt.<Sanitizer>-<ArchName>-android.so for Android)
2197 const char *EnvSuffix =
2198 TC.getTriple().getEnvironment() == llvm::Triple::Android ? "-android" : "";
2199 SmallString<128> LibSanitizer = getCompilerRTLibDir(TC);
2200 llvm::sys::path::append(LibSanitizer,
2201 Twine("libclang_rt.") + Sanitizer + "-" +
2202 getArchNameForCompilerRTLib(TC) + EnvSuffix +
2203 (Shared ? ".so" : ".a"));
2204 return LibSanitizer;
2205}
2206
Stephen Hines176edba2014-12-01 14:53:08 -08002207static void addSanitizerRuntime(const ToolChain &TC, const ArgList &Args,
2208 ArgStringList &CmdArgs, StringRef Sanitizer,
2209 bool IsShared) {
2210 SmallString<128> LibSanitizer = getSanitizerRTLibName(TC, Sanitizer, IsShared);
2211 // Static runtimes must be forced into executable, so we wrap them in
Peter Collingbournebf548552013-10-20 21:29:13 +00002212 // whole-archive.
Stephen Hines176edba2014-12-01 14:53:08 -08002213 if (!IsShared)
2214 CmdArgs.push_back("-whole-archive");
2215 CmdArgs.push_back(Args.MakeArgString(LibSanitizer));
2216 if (!IsShared)
2217 CmdArgs.push_back("-no-whole-archive");
Alexey Samsonov86143042013-02-27 11:14:55 +00002218}
2219
Stephen Hines176edba2014-12-01 14:53:08 -08002220// Tries to use a file with the list of dynamic symbols that need to be exported
2221// from the runtime library. Returns true if the file was found.
2222static bool addSanitizerDynamicList(const ToolChain &TC, const ArgList &Args,
2223 ArgStringList &CmdArgs,
2224 StringRef Sanitizer) {
2225 SmallString<128> LibSanitizer = getSanitizerRTLibName(TC, Sanitizer, false);
2226 if (llvm::sys::fs::exists(LibSanitizer + ".syms")) {
2227 CmdArgs.push_back(
2228 Args.MakeArgString("--dynamic-list=" + LibSanitizer + ".syms"));
2229 return true;
2230 }
2231 return false;
2232}
2233
2234static void linkSanitizerRuntimeDeps(const ToolChain &TC,
2235 ArgStringList &CmdArgs) {
2236 // Force linking against the system libraries sanitizers depends on
2237 // (see PR15823 why this is necessary).
2238 CmdArgs.push_back("--no-as-needed");
2239 CmdArgs.push_back("-lpthread");
2240 CmdArgs.push_back("-lrt");
2241 CmdArgs.push_back("-lm");
2242 // There's no libdl on FreeBSD.
2243 if (TC.getTriple().getOS() != llvm::Triple::FreeBSD)
2244 CmdArgs.push_back("-ldl");
2245}
2246
2247static void
2248collectSanitizerRuntimes(const ToolChain &TC, const ArgList &Args,
2249 SmallVectorImpl<StringRef> &SharedRuntimes,
2250 SmallVectorImpl<StringRef> &StaticRuntimes,
2251 SmallVectorImpl<StringRef> &HelperStaticRuntimes) {
2252 const SanitizerArgs &SanArgs = TC.getSanitizerArgs();
2253 // Collect shared runtimes.
2254 if (SanArgs.needsAsanRt() && SanArgs.needsSharedAsanRt()) {
2255 SharedRuntimes.push_back("asan");
Evgeniy Stepanova6ddc022012-04-25 08:59:22 +00002256 }
Stephen Hines651f13c2014-04-23 16:59:28 -07002257
Stephen Hines176edba2014-12-01 14:53:08 -08002258 // Collect static runtimes.
Stephen Hines651f13c2014-04-23 16:59:28 -07002259 if (Args.hasArg(options::OPT_shared) ||
Stephen Hines176edba2014-12-01 14:53:08 -08002260 (TC.getTriple().getEnvironment() == llvm::Triple::Android)) {
2261 // Don't link static runtimes into DSOs or if compiling for Android.
Stephen Hines651f13c2014-04-23 16:59:28 -07002262 return;
Stephen Hines176edba2014-12-01 14:53:08 -08002263 }
2264 if (SanArgs.needsAsanRt()) {
2265 if (SanArgs.needsSharedAsanRt()) {
2266 HelperStaticRuntimes.push_back("asan-preinit");
2267 } else {
2268 StaticRuntimes.push_back("asan");
2269 if (SanArgs.linkCXXRuntimes())
2270 StaticRuntimes.push_back("asan_cxx");
2271 }
2272 }
2273 if (SanArgs.needsDfsanRt())
2274 StaticRuntimes.push_back("dfsan");
2275 if (SanArgs.needsLsanRt())
2276 StaticRuntimes.push_back("lsan");
2277 if (SanArgs.needsMsanRt())
2278 StaticRuntimes.push_back("msan");
2279 if (SanArgs.needsTsanRt())
2280 StaticRuntimes.push_back("tsan");
2281 // WARNING: UBSan should always go last.
2282 if (SanArgs.needsUbsanRt()) {
2283 // If UBSan is not combined with another sanitizer, we need to pull in
2284 // sanitizer_common explicitly.
2285 if (StaticRuntimes.empty())
2286 HelperStaticRuntimes.push_back("san");
2287 StaticRuntimes.push_back("ubsan");
2288 if (SanArgs.linkCXXRuntimes())
2289 StaticRuntimes.push_back("ubsan_cxx");
Stephen Hines6bcf27b2014-05-29 04:14:42 -07002290 }
Kostya Serebryanydff466c2011-11-30 01:39:16 +00002291}
2292
Stephen Hines176edba2014-12-01 14:53:08 -08002293// Should be called before we add system libraries (C++ ABI, libstdc++/libc++,
2294// C runtime, etc). Returns true if sanitizer system deps need to be linked in.
2295static bool addSanitizerRuntimes(const ToolChain &TC, const ArgList &Args,
Stephen Hines651f13c2014-04-23 16:59:28 -07002296 ArgStringList &CmdArgs) {
Stephen Hines176edba2014-12-01 14:53:08 -08002297 SmallVector<StringRef, 4> SharedRuntimes, StaticRuntimes,
2298 HelperStaticRuntimes;
2299 collectSanitizerRuntimes(TC, Args, SharedRuntimes, StaticRuntimes,
2300 HelperStaticRuntimes);
2301 for (auto RT : SharedRuntimes)
2302 addSanitizerRuntime(TC, Args, CmdArgs, RT, true);
2303 for (auto RT : HelperStaticRuntimes)
2304 addSanitizerRuntime(TC, Args, CmdArgs, RT, false);
2305 bool AddExportDynamic = false;
2306 for (auto RT : StaticRuntimes) {
2307 addSanitizerRuntime(TC, Args, CmdArgs, RT, false);
2308 AddExportDynamic |= !addSanitizerDynamicList(TC, Args, CmdArgs, RT);
2309 }
2310 // If there is a static runtime with no dynamic list, force all the symbols
2311 // to be dynamic to be sure we export sanitizer interface functions.
2312 if (AddExportDynamic)
2313 CmdArgs.push_back("-export-dynamic");
2314 return !StaticRuntimes.empty();
Peter Collingbourne2eeed712013-08-07 22:47:34 +00002315}
2316
Benjamin Kramer5322a552013-10-16 17:42:39 +00002317static bool shouldUseFramePointerForTarget(const ArgList &Args,
2318 const llvm::Triple &Triple) {
2319 switch (Triple.getArch()) {
2320 // Don't use a frame pointer on linux if optimizing for certain targets.
2321 case llvm::Triple::mips64:
2322 case llvm::Triple::mips64el:
2323 case llvm::Triple::mips:
2324 case llvm::Triple::mipsel:
2325 case llvm::Triple::systemz:
2326 case llvm::Triple::x86:
2327 case llvm::Triple::x86_64:
2328 if (Triple.isOSLinux())
2329 if (Arg *A = Args.getLastArg(options::OPT_O_Group))
2330 if (!A->getOption().matches(options::OPT_O0))
2331 return false;
2332 return true;
2333 case llvm::Triple::xcore:
2334 return false;
2335 default:
2336 return true;
2337 }
2338}
2339
Rafael Espindola6af27ec2011-12-14 21:02:23 +00002340static bool shouldUseFramePointer(const ArgList &Args,
2341 const llvm::Triple &Triple) {
2342 if (Arg *A = Args.getLastArg(options::OPT_fno_omit_frame_pointer,
2343 options::OPT_fomit_frame_pointer))
2344 return A->getOption().matches(options::OPT_fno_omit_frame_pointer);
2345
Benjamin Kramer5322a552013-10-16 17:42:39 +00002346 return shouldUseFramePointerForTarget(Args, Triple);
Rafael Espindola6af27ec2011-12-14 21:02:23 +00002347}
2348
Eric Christopherd3e22df2013-04-03 01:58:53 +00002349static bool shouldUseLeafFramePointer(const ArgList &Args,
2350 const llvm::Triple &Triple) {
2351 if (Arg *A = Args.getLastArg(options::OPT_mno_omit_leaf_frame_pointer,
2352 options::OPT_momit_leaf_frame_pointer))
2353 return A->getOption().matches(options::OPT_mno_omit_leaf_frame_pointer);
2354
Benjamin Kramer5322a552013-10-16 17:42:39 +00002355 return shouldUseFramePointerForTarget(Args, Triple);
Eric Christopherd3e22df2013-04-03 01:58:53 +00002356}
2357
Rafael Espindolaa2148242013-08-10 01:40:10 +00002358/// Add a CC1 option to specify the debug compilation directory.
Chandler Carruthd566df62012-12-17 21:40:04 +00002359static void addDebugCompDirArg(const ArgList &Args, ArgStringList &CmdArgs) {
Benjamin Kramer6089adc2013-04-27 08:12:29 +00002360 SmallString<128> cwd;
2361 if (!llvm::sys::fs::current_path(cwd)) {
Chad Rosierb1c81222013-04-26 20:49:50 +00002362 CmdArgs.push_back("-fdebug-compilation-dir");
2363 CmdArgs.push_back(Args.MakeArgString(cwd));
Chandler Carruthd566df62012-12-17 21:40:04 +00002364 }
2365}
2366
Eric Christopher80190392013-02-22 20:12:52 +00002367static const char *SplitDebugName(const ArgList &Args,
2368 const InputInfoList &Inputs) {
2369 Arg *FinalOutput = Args.getLastArg(options::OPT_o);
2370 if (FinalOutput && Args.hasArg(options::OPT_c)) {
2371 SmallString<128> T(FinalOutput->getValue());
2372 llvm::sys::path::replace_extension(T, "dwo");
2373 return Args.MakeArgString(T);
2374 } else {
2375 // Use the compilation dir.
Stephen Hines6bcf27b2014-05-29 04:14:42 -07002376 SmallString<128> T(
2377 Args.getLastArgValue(options::OPT_fdebug_compilation_dir));
Eric Christopher80190392013-02-22 20:12:52 +00002378 SmallString<128> F(llvm::sys::path::stem(Inputs[0].getBaseInput()));
2379 llvm::sys::path::replace_extension(F, "dwo");
2380 T += F;
2381 return Args.MakeArgString(F);
2382 }
2383}
2384
2385static void SplitDebugInfo(const ToolChain &TC, Compilation &C,
2386 const Tool &T, const JobAction &JA,
2387 const ArgList &Args, const InputInfo &Output,
2388 const char *OutFile) {
Eric Christopher59320e72013-02-21 22:35:01 +00002389 ArgStringList ExtractArgs;
2390 ExtractArgs.push_back("--extract-dwo");
2391
2392 ArgStringList StripArgs;
2393 StripArgs.push_back("--strip-dwo");
2394
2395 // Grabbing the output of the earlier compile step.
2396 StripArgs.push_back(Output.getFilename());
2397 ExtractArgs.push_back(Output.getFilename());
Eric Christopher59320e72013-02-21 22:35:01 +00002398 ExtractArgs.push_back(OutFile);
2399
2400 const char *Exec =
Eric Christopher80190392013-02-22 20:12:52 +00002401 Args.MakeArgString(TC.GetProgramPath("objcopy"));
Eric Christopher59320e72013-02-21 22:35:01 +00002402
2403 // First extract the dwo sections.
Stephen Hines176edba2014-12-01 14:53:08 -08002404 C.addCommand(llvm::make_unique<Command>(JA, T, Exec, ExtractArgs));
Eric Christopher59320e72013-02-21 22:35:01 +00002405
2406 // Then remove them from the original .o file.
Stephen Hines176edba2014-12-01 14:53:08 -08002407 C.addCommand(llvm::make_unique<Command>(JA, T, Exec, StripArgs));
Eric Christopher59320e72013-02-21 22:35:01 +00002408}
2409
Arnold Schwaighofer99662a12013-08-13 15:46:23 +00002410/// \brief Vectorize at all optimization levels greater than 1 except for -Oz.
Stephen Hines6bcf27b2014-05-29 04:14:42 -07002411/// For -Oz the loop vectorizer is disable, while the slp vectorizer is enabled.
2412static bool shouldEnableVectorizerAtOLevel(const ArgList &Args, bool isSlpVec) {
Arnold Schwaighofer99662a12013-08-13 15:46:23 +00002413 if (Arg *A = Args.getLastArg(options::OPT_O_Group)) {
Rafael Espindola55ee1eb2013-08-27 16:58:15 +00002414 if (A->getOption().matches(options::OPT_O4) ||
2415 A->getOption().matches(options::OPT_Ofast))
Arnold Schwaighofer99662a12013-08-13 15:46:23 +00002416 return true;
2417
2418 if (A->getOption().matches(options::OPT_O0))
2419 return false;
2420
2421 assert(A->getOption().matches(options::OPT_O) && "Must have a -O flag");
2422
Rafael Espindola168de192013-08-26 14:05:41 +00002423 // Vectorize -Os.
Arnold Schwaighofer99662a12013-08-13 15:46:23 +00002424 StringRef S(A->getValue());
Rafael Espindola168de192013-08-26 14:05:41 +00002425 if (S == "s")
Arnold Schwaighofer99662a12013-08-13 15:46:23 +00002426 return true;
2427
Stephen Hines6bcf27b2014-05-29 04:14:42 -07002428 // Don't vectorize -Oz, unless it's the slp vectorizer.
Arnold Schwaighofer99662a12013-08-13 15:46:23 +00002429 if (S == "z")
Stephen Hines6bcf27b2014-05-29 04:14:42 -07002430 return isSlpVec;
Arnold Schwaighofer99662a12013-08-13 15:46:23 +00002431
2432 unsigned OptLevel = 0;
2433 if (S.getAsInteger(10, OptLevel))
2434 return false;
2435
2436 return OptLevel > 1;
2437 }
2438
2439 return false;
2440}
2441
Stephen Hines651f13c2014-04-23 16:59:28 -07002442/// Add -x lang to \p CmdArgs for \p Input.
2443static void addDashXForInput(const ArgList &Args, const InputInfo &Input,
2444 ArgStringList &CmdArgs) {
2445 // When using -verify-pch, we don't want to provide the type
2446 // 'precompiled-header' if it was inferred from the file extension
2447 if (Args.hasArg(options::OPT_verify_pch) && Input.getType() == types::TY_PCH)
2448 return;
2449
2450 CmdArgs.push_back("-x");
2451 if (Args.hasArg(options::OPT_rewrite_objc))
2452 CmdArgs.push_back(types::getTypeName(types::TY_PP_ObjCXX));
2453 else
2454 CmdArgs.push_back(types::getTypeName(Input.getType()));
2455}
2456
Stephen Hines176edba2014-12-01 14:53:08 -08002457static std::string getMSCompatibilityVersion(const char *VersionStr) {
2458 unsigned Version;
2459 if (StringRef(VersionStr).getAsInteger(10, Version))
2460 return "0";
2461
2462 if (Version < 100)
2463 return llvm::utostr_32(Version) + ".0";
2464
2465 if (Version < 10000)
2466 return llvm::utostr_32(Version / 100) + "." +
2467 llvm::utostr_32(Version % 100);
2468
2469 unsigned Build = 0, Factor = 1;
2470 for ( ; Version > 10000; Version = Version / 10, Factor = Factor * 10)
2471 Build = Build + (Version % 10) * Factor;
2472 return llvm::utostr_32(Version / 100) + "." +
2473 llvm::utostr_32(Version % 100) + "." +
2474 llvm::utostr_32(Build);
2475}
2476
Daniel Dunbar47ac7d22009-03-18 06:00:36 +00002477void Clang::ConstructJob(Compilation &C, const JobAction &JA,
Daniel Dunbar871adcf2009-03-18 07:06:02 +00002478 const InputInfo &Output,
Daniel Dunbar62cf6012009-03-18 06:07:59 +00002479 const InputInfoList &Inputs,
Daniel Dunbar1d460332009-03-18 10:01:51 +00002480 const ArgList &Args,
Daniel Dunbar47ac7d22009-03-18 06:00:36 +00002481 const char *LinkingOutput) const {
Daniel Dunbar0a80ba72010-03-20 04:52:14 +00002482 bool KernelOrKext = Args.hasArg(options::OPT_mkernel,
2483 options::OPT_fapple_kext);
Daniel Dunbaree788e72009-12-21 18:54:17 +00002484 const Driver &D = getToolChain().getDriver();
Daniel Dunbarb488c1d2009-03-18 08:07:30 +00002485 ArgStringList CmdArgs;
2486
Stephen Hines651f13c2014-04-23 16:59:28 -07002487 bool IsWindowsGNU = getToolChain().getTriple().isWindowsGNUEnvironment();
Stephen Hines6bcf27b2014-05-29 04:14:42 -07002488 bool IsWindowsCygnus =
2489 getToolChain().getTriple().isWindowsCygwinEnvironment();
Stephen Hines651f13c2014-04-23 16:59:28 -07002490 bool IsWindowsMSVC = getToolChain().getTriple().isWindowsMSVCEnvironment();
2491
Daniel Dunbar077ba6a2009-03-31 20:53:55 +00002492 assert(Inputs.size() == 1 && "Unable to handle multiple inputs.");
2493
Daniel Dunbar8ff5b282009-12-11 23:00:49 +00002494 // Invoke ourselves in -cc1 mode.
2495 //
2496 // FIXME: Implement custom jobs for internal actions.
2497 CmdArgs.push_back("-cc1");
2498
Daniel Dunbardd4fe002009-10-30 18:12:20 +00002499 // Add the "effective" target triple.
Daniel Dunbaraf07f932009-03-31 17:35:15 +00002500 CmdArgs.push_back("-triple");
Daniel Dunbar00577ad2010-08-23 22:35:37 +00002501 std::string TripleStr = getToolChain().ComputeEffectiveClangTriple(Args);
Daniel Dunbardd4fe002009-10-30 18:12:20 +00002502 CmdArgs.push_back(Args.MakeArgString(TripleStr));
Daniel Dunbar728a5122009-09-10 06:49:20 +00002503
Stephen Hines6bcf27b2014-05-29 04:14:42 -07002504 const llvm::Triple TT(TripleStr);
2505 if (TT.isOSWindows() && (TT.getArch() == llvm::Triple::arm ||
2506 TT.getArch() == llvm::Triple::thumb)) {
2507 unsigned Offset = TT.getArch() == llvm::Triple::arm ? 4 : 6;
2508 unsigned Version;
2509 TT.getArchName().substr(Offset).getAsInteger(10, Version);
2510 if (Version < 7)
2511 D.Diag(diag::err_target_unsupported_arch) << TT.getArchName()
2512 << TripleStr;
2513 }
2514
Stephen Hines651f13c2014-04-23 16:59:28 -07002515 // Push all default warning arguments that are specific to
2516 // the given target. These come before user provided warning options
2517 // are provided.
2518 getToolChain().addClangWarningOptions(CmdArgs);
2519
Daniel Dunbardd4fe002009-10-30 18:12:20 +00002520 // Select the appropriate action.
John McCall260611a2012-06-20 06:18:46 +00002521 RewriteKind rewriteKind = RK_None;
Fariborz Jahaniane982cc02012-04-04 18:50:28 +00002522
Daniel Dunbar1d460332009-03-18 10:01:51 +00002523 if (isa<AnalyzeJobAction>(JA)) {
2524 assert(JA.getType() == types::TY_Plist && "Invalid output type.");
2525 CmdArgs.push_back("-analyze");
Ted Kremenek30660a82012-03-06 20:06:33 +00002526 } else if (isa<MigrateJobAction>(JA)) {
2527 CmdArgs.push_back("-migrate");
Daniel Dunbar1d460332009-03-18 10:01:51 +00002528 } else if (isa<PreprocessJobAction>(JA)) {
Daniel Dunbarcd8e4c42009-03-30 06:36:42 +00002529 if (Output.getType() == types::TY_Dependencies)
2530 CmdArgs.push_back("-Eonly");
Fariborz Jahanian51be73d2013-03-18 19:41:18 +00002531 else {
Daniel Dunbarcd8e4c42009-03-30 06:36:42 +00002532 CmdArgs.push_back("-E");
Fariborz Jahanian51be73d2013-03-18 19:41:18 +00002533 if (Args.hasArg(options::OPT_rewrite_objc) &&
2534 !Args.hasArg(options::OPT_g_Group))
2535 CmdArgs.push_back("-P");
2536 }
Daniel Dunbar8767cbc2010-02-03 03:07:56 +00002537 } else if (isa<AssembleJobAction>(JA)) {
2538 CmdArgs.push_back("-emit-obj");
Daniel Dunbar99298002010-05-27 06:18:05 +00002539
David Blaikie73168db2013-07-25 21:19:01 +00002540 CollectArgsForIntegratedAssembler(C, Args, CmdArgs, D);
Daniel Dunbard02bba82010-11-19 16:23:35 +00002541
2542 // Also ignore explicit -force_cpusubtype_ALL option.
2543 (void) Args.hasArg(options::OPT_force__cpusubtype__ALL);
Daniel Dunbar1d460332009-03-18 10:01:51 +00002544 } else if (isa<PrecompileJobAction>(JA)) {
Argyrios Kyrtzidise5c35372010-08-11 23:27:58 +00002545 // Use PCH if the user requested it.
Daniel Dunbar0ebd9322009-10-15 20:02:44 +00002546 bool UsePCH = D.CCCUsePCH;
Daniel Dunbar0ebd9322009-10-15 20:02:44 +00002547
Aaron Ballman761322b2012-07-31 01:21:00 +00002548 if (JA.getType() == types::TY_Nothing)
2549 CmdArgs.push_back("-fsyntax-only");
2550 else if (UsePCH)
Douglas Gregordf91ef32009-04-18 00:34:01 +00002551 CmdArgs.push_back("-emit-pch");
2552 else
2553 CmdArgs.push_back("-emit-pth");
Stephen Hines651f13c2014-04-23 16:59:28 -07002554 } else if (isa<VerifyPCHJobAction>(JA)) {
2555 CmdArgs.push_back("-verify-pch");
Daniel Dunbar1d460332009-03-18 10:01:51 +00002556 } else {
2557 assert(isa<CompileJobAction>(JA) && "Invalid action for clang tool.");
Daniel Dunbarc21c4852009-04-08 23:54:23 +00002558
Daniel Dunbar1d460332009-03-18 10:01:51 +00002559 if (JA.getType() == types::TY_Nothing) {
2560 CmdArgs.push_back("-fsyntax-only");
Daniel Dunbar6c6424b2010-06-07 23:28:45 +00002561 } else if (JA.getType() == types::TY_LLVM_IR ||
2562 JA.getType() == types::TY_LTO_IR) {
Daniel Dunbar1d460332009-03-18 10:01:51 +00002563 CmdArgs.push_back("-emit-llvm");
Daniel Dunbar6c6424b2010-06-07 23:28:45 +00002564 } else if (JA.getType() == types::TY_LLVM_BC ||
2565 JA.getType() == types::TY_LTO_BC) {
Daniel Dunbar1d460332009-03-18 10:01:51 +00002566 CmdArgs.push_back("-emit-llvm-bc");
2567 } else if (JA.getType() == types::TY_PP_Asm) {
Daniel Dunbare3b8d072009-09-17 00:47:53 +00002568 CmdArgs.push_back("-S");
Daniel Dunbar5915fbf2009-09-01 16:57:46 +00002569 } else if (JA.getType() == types::TY_AST) {
2570 CmdArgs.push_back("-emit-pch");
Douglas Gregorc544ba02013-03-27 16:47:18 +00002571 } else if (JA.getType() == types::TY_ModuleFile) {
2572 CmdArgs.push_back("-module-file-info");
Daniel Dunbar64952502010-02-11 03:16:21 +00002573 } else if (JA.getType() == types::TY_RewrittenObjC) {
2574 CmdArgs.push_back("-rewrite-objc");
John McCall260611a2012-06-20 06:18:46 +00002575 rewriteKind = RK_NonFragile;
Fariborz Jahanian582b3952012-04-02 15:59:19 +00002576 } else if (JA.getType() == types::TY_RewrittenLegacyObjC) {
2577 CmdArgs.push_back("-rewrite-objc");
John McCall260611a2012-06-20 06:18:46 +00002578 rewriteKind = RK_Fragile;
Daniel Dunbar64952502010-02-11 03:16:21 +00002579 } else {
2580 assert(JA.getType() == types::TY_PP_Asm &&
2581 "Unexpected output type!");
Daniel Dunbar1d460332009-03-18 10:01:51 +00002582 }
Daniel Dunbarb488c1d2009-03-18 08:07:30 +00002583 }
2584
Stephen Hinesc568f1e2014-07-21 00:47:37 -07002585 // We normally speed up the clang process a bit by skipping destructors at
2586 // exit, but when we're generating diagnostics we can rely on some of the
2587 // cleanup.
2588 if (!C.isForDiagnostics())
2589 CmdArgs.push_back("-disable-free");
Daniel Dunbar1d460332009-03-18 10:01:51 +00002590
John McCallb689afb2010-02-13 03:50:24 +00002591 // Disable the verification pass in -asserts builds.
2592#ifdef NDEBUG
2593 CmdArgs.push_back("-disable-llvm-verifier");
2594#endif
2595
Daniel Dunbarc9abc042009-04-08 05:11:16 +00002596 // Set the main file name, so that debug info works even with
2597 // -save-temps.
2598 CmdArgs.push_back("-main-file-name");
Bob Wilson66b8a662012-11-23 06:14:39 +00002599 CmdArgs.push_back(getBaseInputName(Args, Inputs));
Daniel Dunbarc9abc042009-04-08 05:11:16 +00002600
Daniel Dunbar3bbc7532009-04-08 18:03:55 +00002601 // Some flags which affect the language (via preprocessor
Bob Wilson66b8a662012-11-23 06:14:39 +00002602 // defines).
Daniel Dunbar3bbc7532009-04-08 18:03:55 +00002603 if (Args.hasArg(options::OPT_static))
2604 CmdArgs.push_back("-static-define");
2605
Daniel Dunbar1d460332009-03-18 10:01:51 +00002606 if (isa<AnalyzeJobAction>(JA)) {
Ted Kremenekb8bb3e72009-09-25 05:55:59 +00002607 // Enable region store model by default.
2608 CmdArgs.push_back("-analyzer-store=region");
2609
Ted Kremenekb40d06d2009-12-07 22:26:14 +00002610 // Treat blocks as analysis entry points.
2611 CmdArgs.push_back("-analyzer-opt-analyze-nested-blocks");
2612
Ted Kremenek51885072011-03-24 00:28:47 +00002613 CmdArgs.push_back("-analyzer-eagerly-assume");
2614
Daniel Dunbar1d460332009-03-18 10:01:51 +00002615 // Add default argument set.
Daniel Dunbard8fc0f22009-05-22 00:38:15 +00002616 if (!Args.hasArg(options::OPT__analyzer_no_default_checks)) {
Argyrios Kyrtzidis027a6ab2011-02-15 07:42:33 +00002617 CmdArgs.push_back("-analyzer-checker=core");
Ted Kremenek51885072011-03-24 00:28:47 +00002618
Stephen Hines651f13c2014-04-23 16:59:28 -07002619 if (!IsWindowsMSVC)
Argyrios Kyrtzidis027a6ab2011-02-15 07:42:33 +00002620 CmdArgs.push_back("-analyzer-checker=unix");
Ted Kremenek51885072011-03-24 00:28:47 +00002621
Argyrios Kyrtzidis027a6ab2011-02-15 07:42:33 +00002622 if (getToolChain().getTriple().getVendor() == llvm::Triple::Apple)
Ted Kremenek51885072011-03-24 00:28:47 +00002623 CmdArgs.push_back("-analyzer-checker=osx");
Ted Kremeneka8180e52012-01-20 06:00:17 +00002624
2625 CmdArgs.push_back("-analyzer-checker=deadcode");
Ted Kremenek8dc05062012-01-26 02:27:38 +00002626
Jordan Rosee449edc2013-04-05 17:55:07 +00002627 if (types::isCXX(Inputs[0].getType()))
2628 CmdArgs.push_back("-analyzer-checker=cplusplus");
2629
Stephen Hines6bcf27b2014-05-29 04:14:42 -07002630 // Enable the following experimental checkers for testing.
2631 CmdArgs.push_back(
2632 "-analyzer-checker=security.insecureAPI.UncheckedReturn");
Ted Kremenek8dc05062012-01-26 02:27:38 +00002633 CmdArgs.push_back("-analyzer-checker=security.insecureAPI.getpw");
2634 CmdArgs.push_back("-analyzer-checker=security.insecureAPI.gets");
2635 CmdArgs.push_back("-analyzer-checker=security.insecureAPI.mktemp");
2636 CmdArgs.push_back("-analyzer-checker=security.insecureAPI.mkstemp");
2637 CmdArgs.push_back("-analyzer-checker=security.insecureAPI.vfork");
Daniel Dunbard8fc0f22009-05-22 00:38:15 +00002638 }
Daniel Dunbarc21c4852009-04-08 23:54:23 +00002639
Daniel Dunbard8fc0f22009-05-22 00:38:15 +00002640 // Set the output format. The default is plist, for (lame) historical
2641 // reasons.
2642 CmdArgs.push_back("-analyzer-output");
2643 if (Arg *A = Args.getLastArg(options::OPT__analyzer_output))
Richard Smith1d489cf2012-11-01 04:30:05 +00002644 CmdArgs.push_back(A->getValue());
Daniel Dunbard8fc0f22009-05-22 00:38:15 +00002645 else
2646 CmdArgs.push_back("plist");
Daniel Dunbar1d460332009-03-18 10:01:51 +00002647
Ted Kremenek0647a7b2010-03-22 22:32:05 +00002648 // Disable the presentation of standard compiler warnings when
2649 // using --analyze. We only want to show static analyzer diagnostics
2650 // or frontend errors.
2651 CmdArgs.push_back("-w");
2652
Daniel Dunbar1d460332009-03-18 10:01:51 +00002653 // Add -Xanalyzer arguments when running as analyzer.
2654 Args.AddAllArgValues(CmdArgs, options::OPT_Xanalyzer);
Mike Stump1eb44332009-09-09 15:08:12 +00002655 }
2656
Daniel Dunbare2fd6642009-09-10 01:21:12 +00002657 CheckCodeGenerationOptions(D, Args);
2658
Peter Collingbourne52ca70d2013-04-09 04:35:11 +00002659 bool PIE = getToolChain().isPIEDefault();
2660 bool PIC = PIE || getToolChain().isPICDefault();
Chandler Carruth7ce816a2012-11-19 03:52:03 +00002661 bool IsPICLevelTwo = PIC;
Peter Collingbourne52ca70d2013-04-09 04:35:11 +00002662
Stephen Hines651f13c2014-04-23 16:59:28 -07002663 // Android-specific defaults for PIC/PIE
2664 if (getToolChain().getTriple().getEnvironment() == llvm::Triple::Android) {
2665 switch (getToolChain().getTriple().getArch()) {
2666 case llvm::Triple::arm:
2667 case llvm::Triple::armeb:
2668 case llvm::Triple::thumb:
2669 case llvm::Triple::thumbeb:
Stephen Hines6bcf27b2014-05-29 04:14:42 -07002670 case llvm::Triple::aarch64:
Stephen Hines651f13c2014-04-23 16:59:28 -07002671 case llvm::Triple::mips:
2672 case llvm::Triple::mipsel:
2673 case llvm::Triple::mips64:
2674 case llvm::Triple::mips64el:
2675 PIC = true; // "-fpic"
2676 break;
2677
2678 case llvm::Triple::x86:
2679 case llvm::Triple::x86_64:
2680 PIC = true; // "-fPIC"
2681 IsPICLevelTwo = true;
2682 break;
2683
2684 default:
2685 break;
2686 }
2687 }
2688
Stephen Hinesc568f1e2014-07-21 00:47:37 -07002689 // OpenBSD-specific defaults for PIE
2690 if (getToolChain().getTriple().getOS() == llvm::Triple::OpenBSD) {
2691 switch (getToolChain().getTriple().getArch()) {
2692 case llvm::Triple::mips64:
2693 case llvm::Triple::mips64el:
2694 case llvm::Triple::sparc:
2695 case llvm::Triple::x86:
2696 case llvm::Triple::x86_64:
2697 IsPICLevelTwo = false; // "-fpie"
2698 break;
2699
2700 case llvm::Triple::ppc:
2701 case llvm::Triple::sparcv9:
2702 IsPICLevelTwo = true; // "-fPIE"
2703 break;
2704
2705 default:
2706 break;
2707 }
2708 }
2709
Alexey Samsonovdb68e5a2013-04-09 12:28:19 +00002710 // For the PIC and PIE flag options, this logic is different from the
2711 // legacy logic in very old versions of GCC, as that logic was just
2712 // a bug no one had ever fixed. This logic is both more rational and
2713 // consistent with GCC's new logic now that the bugs are fixed. The last
2714 // argument relating to either PIC or PIE wins, and no other argument is
2715 // used. If the last argument is any flavor of the '-fno-...' arguments,
2716 // both PIC and PIE are disabled. Any PIE option implicitly enables PIC
2717 // at the same level.
2718 Arg *LastPICArg =Args.getLastArg(options::OPT_fPIC, options::OPT_fno_PIC,
2719 options::OPT_fpic, options::OPT_fno_pic,
2720 options::OPT_fPIE, options::OPT_fno_PIE,
2721 options::OPT_fpie, options::OPT_fno_pie);
Chandler Carruth7ce816a2012-11-19 03:52:03 +00002722 // Check whether the tool chain trumps the PIC-ness decision. If the PIC-ness
2723 // is forced, then neither PIC nor PIE flags will have no effect.
Peter Collingbourne52ca70d2013-04-09 04:35:11 +00002724 if (!getToolChain().isPICDefaultForced()) {
Alexey Samsonovdb68e5a2013-04-09 12:28:19 +00002725 if (LastPICArg) {
2726 Option O = LastPICArg->getOption();
Peter Collingbourne52ca70d2013-04-09 04:35:11 +00002727 if (O.matches(options::OPT_fPIC) || O.matches(options::OPT_fpic) ||
2728 O.matches(options::OPT_fPIE) || O.matches(options::OPT_fpie)) {
2729 PIE = O.matches(options::OPT_fPIE) || O.matches(options::OPT_fpie);
2730 PIC = PIE || O.matches(options::OPT_fPIC) ||
2731 O.matches(options::OPT_fpic);
2732 IsPICLevelTwo = O.matches(options::OPT_fPIE) ||
2733 O.matches(options::OPT_fPIC);
2734 } else {
2735 PIE = PIC = false;
2736 }
2737 }
Chandler Carruth5e219cf2012-04-08 16:40:35 +00002738 }
Chandler Carruth7ce816a2012-11-19 03:52:03 +00002739
Nick Lewyckyd4705682013-10-11 03:33:53 +00002740 // Introduce a Darwin-specific hack. If the default is PIC but the flags
Chandler Carruth7ce816a2012-11-19 03:52:03 +00002741 // specified while enabling PIC enabled level 1 PIC, just force it back to
2742 // level 2 PIC instead. This matches the behavior of Darwin GCC (based on my
2743 // informal testing).
2744 if (PIC && getToolChain().getTriple().isOSDarwin())
2745 IsPICLevelTwo |= getToolChain().isPICDefault();
2746
Chandler Carruth5e219cf2012-04-08 16:40:35 +00002747 // Note that these flags are trump-cards. Regardless of the order w.r.t. the
2748 // PIC or PIE options above, if these show up, PIC is disabled.
Daniel Dunbar7a0c0642012-10-15 22:23:53 +00002749 llvm::Triple Triple(TripleStr);
Stephen Hines651f13c2014-04-23 16:59:28 -07002750 if (KernelOrKext && (!Triple.isiOS() || Triple.isOSVersionLT(6) ||
Stephen Hines6bcf27b2014-05-29 04:14:42 -07002751 Triple.getArch() == llvm::Triple::aarch64))
Chandler Carruth7ce816a2012-11-19 03:52:03 +00002752 PIC = PIE = false;
Simon Atanasyanc0e83642013-10-04 11:46:54 +00002753 if (Args.hasArg(options::OPT_static))
Chandler Carruth7ce816a2012-11-19 03:52:03 +00002754 PIC = PIE = false;
Chandler Carruth5e219cf2012-04-08 16:40:35 +00002755
Chandler Carruth7ce816a2012-11-19 03:52:03 +00002756 if (Arg *A = Args.getLastArg(options::OPT_mdynamic_no_pic)) {
2757 // This is a very special mode. It trumps the other modes, almost no one
2758 // uses it, and it isn't even valid on any OS but Darwin.
2759 if (!getToolChain().getTriple().isOSDarwin())
2760 D.Diag(diag::err_drv_unsupported_opt_for_target)
2761 << A->getSpelling() << getToolChain().getTriple().str();
2762
2763 // FIXME: Warn when this flag trumps some other PIC or PIE flag.
2764
Daniel Dunbarf219e7c2009-11-29 07:18:39 +00002765 CmdArgs.push_back("-mrelocation-model");
Chandler Carruth7ce816a2012-11-19 03:52:03 +00002766 CmdArgs.push_back("dynamic-no-pic");
Daniel Dunbarbc85be82009-04-29 18:32:25 +00002767
Chandler Carruth7ce816a2012-11-19 03:52:03 +00002768 // Only a forced PIC mode can cause the actual compile to have PIC defines
2769 // etc., no flags are sufficient. This behavior was selected to closely
2770 // match that of llvm-gcc and Apple GCC before that.
2771 if (getToolChain().isPICDefault() && getToolChain().isPICDefaultForced()) {
2772 CmdArgs.push_back("-pic-level");
2773 CmdArgs.push_back("2");
2774 }
2775 } else {
2776 // Currently, LLVM only knows about PIC vs. static; the PIE differences are
2777 // handled in Clang's IRGen by the -pie-level flag.
2778 CmdArgs.push_back("-mrelocation-model");
2779 CmdArgs.push_back(PIC ? "pic" : "static");
2780
2781 if (PIC) {
2782 CmdArgs.push_back("-pic-level");
2783 CmdArgs.push_back(IsPICLevelTwo ? "2" : "1");
2784 if (PIE) {
2785 CmdArgs.push_back("-pie-level");
2786 CmdArgs.push_back(IsPICLevelTwo ? "2" : "1");
2787 }
2788 }
Daniel Dunbarbc85be82009-04-29 18:32:25 +00002789 }
Chandler Carruth5e219cf2012-04-08 16:40:35 +00002790
Stephen Hines176edba2014-12-01 14:53:08 -08002791 CmdArgs.push_back("-mthread-model");
2792 if (Arg *A = Args.getLastArg(options::OPT_mthread_model))
2793 CmdArgs.push_back(A->getValue());
2794 else
2795 CmdArgs.push_back(Args.MakeArgString(getToolChain().getThreadModel()));
2796
Tanya Lattner59876c22009-11-04 01:18:09 +00002797 if (!Args.hasFlag(options::OPT_fmerge_all_constants,
2798 options::OPT_fno_merge_all_constants))
Chris Lattnerf44a1a02011-04-08 18:06:54 +00002799 CmdArgs.push_back("-fno-merge-all-constants");
Daniel Dunbar6bea73b2009-09-16 06:17:29 +00002800
Daniel Dunbarf219e7c2009-11-29 07:18:39 +00002801 // LLVM Code Generator Options.
2802
Stephen Hinesc568f1e2014-07-21 00:47:37 -07002803 if (Arg *A = Args.getLastArg(options::OPT_Wframe_larger_than_EQ)) {
2804 StringRef v = A->getValue();
2805 CmdArgs.push_back("-mllvm");
2806 CmdArgs.push_back(Args.MakeArgString("-warn-stack-size=" + v));
2807 A->claim();
2808 }
2809
Daniel Dunbar17d3fea2011-02-09 17:54:19 +00002810 if (Arg *A = Args.getLastArg(options::OPT_mregparm_EQ)) {
2811 CmdArgs.push_back("-mregparm");
Richard Smith1d489cf2012-11-01 04:30:05 +00002812 CmdArgs.push_back(A->getValue());
Daniel Dunbar17d3fea2011-02-09 17:54:19 +00002813 }
2814
Nick Lewyckyfdf137b2013-06-25 01:49:44 +00002815 if (Arg *A = Args.getLastArg(options::OPT_fpcc_struct_return,
2816 options::OPT_freg_struct_return)) {
Eli Bendersky8f4269a2013-07-24 22:20:49 +00002817 if (getToolChain().getArch() != llvm::Triple::x86) {
John McCallb8b52972013-06-18 02:46:29 +00002818 D.Diag(diag::err_drv_unsupported_opt_for_target)
2819 << A->getSpelling() << getToolChain().getTriple().str();
2820 } else if (A->getOption().matches(options::OPT_fpcc_struct_return)) {
2821 CmdArgs.push_back("-fpcc-struct-return");
2822 } else {
2823 assert(A->getOption().matches(options::OPT_freg_struct_return));
2824 CmdArgs.push_back("-freg-struct-return");
2825 }
2826 }
2827
Roman Divackycfe9af22011-03-01 17:40:53 +00002828 if (Args.hasFlag(options::OPT_mrtd, options::OPT_mno_rtd, false))
2829 CmdArgs.push_back("-mrtd");
2830
Rafael Espindola6af27ec2011-12-14 21:02:23 +00002831 if (shouldUseFramePointer(Args, getToolChain().getTriple()))
Daniel Dunbarf219e7c2009-11-29 07:18:39 +00002832 CmdArgs.push_back("-mdisable-fp-elim");
2833 if (!Args.hasFlag(options::OPT_fzero_initialized_in_bss,
2834 options::OPT_fno_zero_initialized_in_bss))
2835 CmdArgs.push_back("-mno-zero-initialized-in-bss");
Chad Rosierb82e1172013-04-24 18:09:54 +00002836
2837 bool OFastEnabled = isOptimizationLevelFast(Args);
2838 // If -Ofast is the optimization level, then -fstrict-aliasing should be
2839 // enabled. This alias option is being used to simplify the hasFlag logic.
2840 OptSpecifier StrictAliasingAliasOption = OFastEnabled ? options::OPT_Ofast :
2841 options::OPT_fstrict_aliasing;
Stephen Hines6bcf27b2014-05-29 04:14:42 -07002842 // We turn strict aliasing off by default if we're in CL mode, since MSVC
2843 // doesn't do any TBAA.
2844 bool TBAAOnByDefault = !getToolChain().getDriver().IsCLMode();
Chad Rosierb82e1172013-04-24 18:09:54 +00002845 if (!Args.hasFlag(options::OPT_fstrict_aliasing, StrictAliasingAliasOption,
Stephen Hines6bcf27b2014-05-29 04:14:42 -07002846 options::OPT_fno_strict_aliasing, TBAAOnByDefault))
Dan Gohman4d5625e2010-10-14 22:36:56 +00002847 CmdArgs.push_back("-relaxed-aliasing");
Manman Ren96d6c452013-10-11 20:48:38 +00002848 if (!Args.hasFlag(options::OPT_fstruct_path_tbaa,
2849 options::OPT_fno_struct_path_tbaa))
2850 CmdArgs.push_back("-no-struct-path-tbaa");
Chandler Carruth82fe6ae2012-03-27 23:58:37 +00002851 if (Args.hasFlag(options::OPT_fstrict_enums, options::OPT_fno_strict_enums,
2852 false))
2853 CmdArgs.push_back("-fstrict-enums");
Nick Lewycky1db772b2012-01-23 08:29:12 +00002854 if (!Args.hasFlag(options::OPT_foptimize_sibling_calls,
2855 options::OPT_fno_optimize_sibling_calls))
2856 CmdArgs.push_back("-mdisable-tail-calls");
Daniel Dunbar1b718482010-05-14 22:00:22 +00002857
Eric Christopher31056272013-04-04 06:29:47 +00002858 // Handle segmented stacks.
2859 if (Args.hasArg(options::OPT_fsplit_stack))
2860 CmdArgs.push_back("-split-stacks");
Chad Rosierb82e1172013-04-24 18:09:54 +00002861
2862 // If -Ofast is the optimization level, then -ffast-math should be enabled.
2863 // This alias option is being used to simplify the getLastArg logic.
2864 OptSpecifier FastMathAliasOption = OFastEnabled ? options::OPT_Ofast :
2865 options::OPT_ffast_math;
Eric Christopher31056272013-04-04 06:29:47 +00002866
Chandler Carruthabf07a72012-01-02 14:19:45 +00002867 // Handle various floating point optimization flags, mapping them to the
2868 // appropriate LLVM code generation flags. The pattern for all of these is to
2869 // default off the codegen optimizations, and if any flag enables them and no
2870 // flag disables them after the flag enabling them, enable the codegen
2871 // optimization. This is complicated by several "umbrella" flags.
Chad Rosierb82e1172013-04-24 18:09:54 +00002872 if (Arg *A = Args.getLastArg(options::OPT_ffast_math, FastMathAliasOption,
Chad Rosier80ecf5e2012-09-25 22:03:25 +00002873 options::OPT_fno_fast_math,
Chandler Carruthabf07a72012-01-02 14:19:45 +00002874 options::OPT_ffinite_math_only,
2875 options::OPT_fno_finite_math_only,
2876 options::OPT_fhonor_infinities,
2877 options::OPT_fno_honor_infinities))
Chad Rosier80ecf5e2012-09-25 22:03:25 +00002878 if (A->getOption().getID() != options::OPT_fno_fast_math &&
2879 A->getOption().getID() != options::OPT_fno_finite_math_only &&
Chandler Carruthabf07a72012-01-02 14:19:45 +00002880 A->getOption().getID() != options::OPT_fhonor_infinities)
2881 CmdArgs.push_back("-menable-no-infs");
Chad Rosierb82e1172013-04-24 18:09:54 +00002882 if (Arg *A = Args.getLastArg(options::OPT_ffast_math, FastMathAliasOption,
Chad Rosier80ecf5e2012-09-25 22:03:25 +00002883 options::OPT_fno_fast_math,
Chandler Carruthabf07a72012-01-02 14:19:45 +00002884 options::OPT_ffinite_math_only,
2885 options::OPT_fno_finite_math_only,
2886 options::OPT_fhonor_nans,
2887 options::OPT_fno_honor_nans))
Chad Rosier80ecf5e2012-09-25 22:03:25 +00002888 if (A->getOption().getID() != options::OPT_fno_fast_math &&
2889 A->getOption().getID() != options::OPT_fno_finite_math_only &&
Chandler Carruthabf07a72012-01-02 14:19:45 +00002890 A->getOption().getID() != options::OPT_fhonor_nans)
2891 CmdArgs.push_back("-menable-no-nans");
2892
Benjamin Kramer769aa2d2012-05-02 14:55:48 +00002893 // -fmath-errno is the default on some platforms, e.g. BSD-derived OSes.
2894 bool MathErrno = getToolChain().IsMathErrnoDefault();
Chad Rosierb82e1172013-04-24 18:09:54 +00002895 if (Arg *A = Args.getLastArg(options::OPT_ffast_math, FastMathAliasOption,
Chad Rosier80ecf5e2012-09-25 22:03:25 +00002896 options::OPT_fno_fast_math,
Chandler Carruthabf07a72012-01-02 14:19:45 +00002897 options::OPT_fmath_errno,
Chandler Carruthb69557e2013-05-18 20:47:36 +00002898 options::OPT_fno_math_errno)) {
2899 // Turning on -ffast_math (with either flag) removes the need for MathErrno.
2900 // However, turning *off* -ffast_math merely restores the toolchain default
2901 // (which may be false).
2902 if (A->getOption().getID() == options::OPT_fno_math_errno ||
2903 A->getOption().getID() == options::OPT_ffast_math ||
2904 A->getOption().getID() == options::OPT_Ofast)
2905 MathErrno = false;
2906 else if (A->getOption().getID() == options::OPT_fmath_errno)
2907 MathErrno = true;
2908 }
Chandler Carruth4f50c502012-04-26 02:10:51 +00002909 if (MathErrno)
2910 CmdArgs.push_back("-fmath-errno");
Chandler Carruthabf07a72012-01-02 14:19:45 +00002911
2912 // There are several flags which require disabling very specific
2913 // optimizations. Any of these being disabled forces us to turn off the
2914 // entire set of LLVM optimizations, so collect them through all the flag
2915 // madness.
2916 bool AssociativeMath = false;
Chad Rosierb82e1172013-04-24 18:09:54 +00002917 if (Arg *A = Args.getLastArg(options::OPT_ffast_math, FastMathAliasOption,
Chad Rosier80ecf5e2012-09-25 22:03:25 +00002918 options::OPT_fno_fast_math,
Chandler Carruthabf07a72012-01-02 14:19:45 +00002919 options::OPT_funsafe_math_optimizations,
2920 options::OPT_fno_unsafe_math_optimizations,
2921 options::OPT_fassociative_math,
2922 options::OPT_fno_associative_math))
Chad Rosier80ecf5e2012-09-25 22:03:25 +00002923 if (A->getOption().getID() != options::OPT_fno_fast_math &&
2924 A->getOption().getID() != options::OPT_fno_unsafe_math_optimizations &&
Chandler Carruthabf07a72012-01-02 14:19:45 +00002925 A->getOption().getID() != options::OPT_fno_associative_math)
2926 AssociativeMath = true;
2927 bool ReciprocalMath = false;
Chad Rosierb82e1172013-04-24 18:09:54 +00002928 if (Arg *A = Args.getLastArg(options::OPT_ffast_math, FastMathAliasOption,
Chad Rosier80ecf5e2012-09-25 22:03:25 +00002929 options::OPT_fno_fast_math,
Chandler Carruthabf07a72012-01-02 14:19:45 +00002930 options::OPT_funsafe_math_optimizations,
2931 options::OPT_fno_unsafe_math_optimizations,
2932 options::OPT_freciprocal_math,
2933 options::OPT_fno_reciprocal_math))
Chad Rosier80ecf5e2012-09-25 22:03:25 +00002934 if (A->getOption().getID() != options::OPT_fno_fast_math &&
2935 A->getOption().getID() != options::OPT_fno_unsafe_math_optimizations &&
Chandler Carruthabf07a72012-01-02 14:19:45 +00002936 A->getOption().getID() != options::OPT_fno_reciprocal_math)
2937 ReciprocalMath = true;
2938 bool SignedZeros = true;
Chad Rosierb82e1172013-04-24 18:09:54 +00002939 if (Arg *A = Args.getLastArg(options::OPT_ffast_math, FastMathAliasOption,
Chad Rosier80ecf5e2012-09-25 22:03:25 +00002940 options::OPT_fno_fast_math,
Chandler Carruthabf07a72012-01-02 14:19:45 +00002941 options::OPT_funsafe_math_optimizations,
2942 options::OPT_fno_unsafe_math_optimizations,
2943 options::OPT_fsigned_zeros,
2944 options::OPT_fno_signed_zeros))
Chad Rosier80ecf5e2012-09-25 22:03:25 +00002945 if (A->getOption().getID() != options::OPT_fno_fast_math &&
2946 A->getOption().getID() != options::OPT_fno_unsafe_math_optimizations &&
Chandler Carruthabf07a72012-01-02 14:19:45 +00002947 A->getOption().getID() != options::OPT_fsigned_zeros)
2948 SignedZeros = false;
2949 bool TrappingMath = true;
Chad Rosierb82e1172013-04-24 18:09:54 +00002950 if (Arg *A = Args.getLastArg(options::OPT_ffast_math, FastMathAliasOption,
Chad Rosier80ecf5e2012-09-25 22:03:25 +00002951 options::OPT_fno_fast_math,
Chandler Carruthabf07a72012-01-02 14:19:45 +00002952 options::OPT_funsafe_math_optimizations,
2953 options::OPT_fno_unsafe_math_optimizations,
2954 options::OPT_ftrapping_math,
2955 options::OPT_fno_trapping_math))
Chad Rosier80ecf5e2012-09-25 22:03:25 +00002956 if (A->getOption().getID() != options::OPT_fno_fast_math &&
2957 A->getOption().getID() != options::OPT_fno_unsafe_math_optimizations &&
Chandler Carruthabf07a72012-01-02 14:19:45 +00002958 A->getOption().getID() != options::OPT_ftrapping_math)
2959 TrappingMath = false;
2960 if (!MathErrno && AssociativeMath && ReciprocalMath && !SignedZeros &&
2961 !TrappingMath)
2962 CmdArgs.push_back("-menable-unsafe-fp-math");
2963
Lang Hamesc9686712012-07-06 00:59:19 +00002964
2965 // Validate and pass through -fp-contract option.
Chad Rosierb82e1172013-04-24 18:09:54 +00002966 if (Arg *A = Args.getLastArg(options::OPT_ffast_math, FastMathAliasOption,
Chad Rosier80ecf5e2012-09-25 22:03:25 +00002967 options::OPT_fno_fast_math,
Lang Hamesc9686712012-07-06 00:59:19 +00002968 options::OPT_ffp_contract)) {
2969 if (A->getOption().getID() == options::OPT_ffp_contract) {
Richard Smith1d489cf2012-11-01 04:30:05 +00002970 StringRef Val = A->getValue();
Lang Hamesc9686712012-07-06 00:59:19 +00002971 if (Val == "fast" || Val == "on" || Val == "off") {
2972 CmdArgs.push_back(Args.MakeArgString("-ffp-contract=" + Val));
2973 } else {
2974 D.Diag(diag::err_drv_unsupported_option_argument)
2975 << A->getOption().getName() << Val;
2976 }
Chad Rosierb82e1172013-04-24 18:09:54 +00002977 } else if (A->getOption().matches(options::OPT_ffast_math) ||
2978 (OFastEnabled && A->getOption().matches(options::OPT_Ofast))) {
Lang Hamesc9686712012-07-06 00:59:19 +00002979 // If fast-math is set then set the fp-contract mode to fast.
2980 CmdArgs.push_back(Args.MakeArgString("-ffp-contract=fast"));
2981 }
2982 }
2983
Bob Wilson455e72e2012-07-19 03:52:53 +00002984 // We separately look for the '-ffast-math' and '-ffinite-math-only' flags,
2985 // and if we find them, tell the frontend to provide the appropriate
2986 // preprocessor macros. This is distinct from enabling any optimizations as
2987 // these options induce language changes which must survive serialization
2988 // and deserialization, etc.
Chad Rosierb82e1172013-04-24 18:09:54 +00002989 if (Arg *A = Args.getLastArg(options::OPT_ffast_math, FastMathAliasOption,
2990 options::OPT_fno_fast_math))
2991 if (!A->getOption().matches(options::OPT_fno_fast_math))
2992 CmdArgs.push_back("-ffast-math");
Stephen Hines6bcf27b2014-05-29 04:14:42 -07002993 if (Arg *A = Args.getLastArg(options::OPT_ffinite_math_only,
2994 options::OPT_fno_fast_math))
Chad Rosier80ecf5e2012-09-25 22:03:25 +00002995 if (A->getOption().matches(options::OPT_ffinite_math_only))
2996 CmdArgs.push_back("-ffinite-math-only");
Chandler Carruthabf07a72012-01-02 14:19:45 +00002997
Daniel Dunbar1b718482010-05-14 22:00:22 +00002998 // Decide whether to use verbose asm. Verbose assembly is the default on
2999 // toolchains which have the integrated assembler on by default.
Stephen Hines651f13c2014-04-23 16:59:28 -07003000 bool IsIntegratedAssemblerDefault =
3001 getToolChain().IsIntegratedAssemblerDefault();
Daniel Dunbar1b718482010-05-14 22:00:22 +00003002 if (Args.hasFlag(options::OPT_fverbose_asm, options::OPT_fno_verbose_asm,
Stephen Hines651f13c2014-04-23 16:59:28 -07003003 IsIntegratedAssemblerDefault) ||
Daniel Dunbar1b718482010-05-14 22:00:22 +00003004 Args.hasArg(options::OPT_dA))
Daniel Dunbarf219e7c2009-11-29 07:18:39 +00003005 CmdArgs.push_back("-masm-verbose");
Daniel Dunbar1b718482010-05-14 22:00:22 +00003006
Stephen Hines651f13c2014-04-23 16:59:28 -07003007 if (!Args.hasFlag(options::OPT_fintegrated_as, options::OPT_fno_integrated_as,
3008 IsIntegratedAssemblerDefault))
3009 CmdArgs.push_back("-no-integrated-as");
3010
Daniel Dunbarf219e7c2009-11-29 07:18:39 +00003011 if (Args.hasArg(options::OPT_fdebug_pass_structure)) {
3012 CmdArgs.push_back("-mdebug-pass");
3013 CmdArgs.push_back("Structure");
3014 }
3015 if (Args.hasArg(options::OPT_fdebug_pass_arguments)) {
3016 CmdArgs.push_back("-mdebug-pass");
3017 CmdArgs.push_back("Arguments");
3018 }
3019
John McCalld0c2ec42010-02-19 02:45:38 +00003020 // Enable -mconstructor-aliases except on darwin, where we have to
3021 // work around a linker bug; see <rdar://problem/7651567>.
Bob Wilson905c45f2011-10-14 05:03:44 +00003022 if (!getToolChain().getTriple().isOSDarwin())
John McCalld0c2ec42010-02-19 02:45:38 +00003023 CmdArgs.push_back("-mconstructor-aliases");
NAKAMURA Takumi125b4cb2011-02-17 08:50:50 +00003024
John McCall32096692011-03-18 02:56:14 +00003025 // Darwin's kernel doesn't support guard variables; just die if we
3026 // try to use them.
Bob Wilson905c45f2011-10-14 05:03:44 +00003027 if (KernelOrKext && getToolChain().getTriple().isOSDarwin())
John McCall32096692011-03-18 02:56:14 +00003028 CmdArgs.push_back("-fforbid-guard-variables");
3029
Douglas Gregor6f755502011-02-01 15:15:22 +00003030 if (Args.hasArg(options::OPT_mms_bitfields)) {
3031 CmdArgs.push_back("-mms-bitfields");
3032 }
John McCalld0c2ec42010-02-19 02:45:38 +00003033
Daniel Dunbar6bea73b2009-09-16 06:17:29 +00003034 // This is a coarse approximation of what llvm-gcc actually does, both
3035 // -fasynchronous-unwind-tables and -fnon-call-exceptions interact in more
3036 // complicated ways.
3037 bool AsynchronousUnwindTables =
Stephen Hines651f13c2014-04-23 16:59:28 -07003038 Args.hasFlag(options::OPT_fasynchronous_unwind_tables,
3039 options::OPT_fno_asynchronous_unwind_tables,
3040 (getToolChain().IsUnwindTablesDefault() ||
3041 getToolChain().getSanitizerArgs().needsUnwindTables()) &&
3042 !KernelOrKext);
Daniel Dunbar6bea73b2009-09-16 06:17:29 +00003043 if (Args.hasFlag(options::OPT_funwind_tables, options::OPT_fno_unwind_tables,
3044 AsynchronousUnwindTables))
Daniel Dunbarf219e7c2009-11-29 07:18:39 +00003045 CmdArgs.push_back("-munwind-tables");
3046
Chandler Carrutha6b25812012-11-21 23:40:23 +00003047 getToolChain().addClangTargetOptions(Args, CmdArgs);
Rafael Espindola8af669f2012-06-19 01:26:10 +00003048
Daniel Dunbarf219e7c2009-11-29 07:18:39 +00003049 if (Arg *A = Args.getLastArg(options::OPT_flimited_precision_EQ)) {
3050 CmdArgs.push_back("-mlimit-float-precision");
Richard Smith1d489cf2012-11-01 04:30:05 +00003051 CmdArgs.push_back(A->getValue());
Daniel Dunbarf219e7c2009-11-29 07:18:39 +00003052 }
Daniel Dunbarbc85be82009-04-29 18:32:25 +00003053
Daniel Dunbar868bd0a2009-05-06 03:16:41 +00003054 // FIXME: Handle -mtune=.
3055 (void) Args.hasArg(options::OPT_mtune_EQ);
Daniel Dunbarbc85be82009-04-29 18:32:25 +00003056
Benjamin Kramer8e9ef0d2009-08-05 14:30:52 +00003057 if (Arg *A = Args.getLastArg(options::OPT_mcmodel_EQ)) {
Daniel Dunbarf219e7c2009-11-29 07:18:39 +00003058 CmdArgs.push_back("-mcode-model");
Richard Smith1d489cf2012-11-01 04:30:05 +00003059 CmdArgs.push_back(A->getValue());
Benjamin Kramer8e9ef0d2009-08-05 14:30:52 +00003060 }
3061
Rafael Espindolab330e402013-08-20 22:12:08 +00003062 // Add the target cpu
3063 std::string ETripleStr = getToolChain().ComputeEffectiveClangTriple(Args);
3064 llvm::Triple ETriple(ETripleStr);
3065 std::string CPU = getCPUName(Args, ETriple);
3066 if (!CPU.empty()) {
3067 CmdArgs.push_back("-target-cpu");
3068 CmdArgs.push_back(Args.MakeArgString(CPU));
3069 }
3070
Rafael Espindola5389b842013-08-21 21:59:03 +00003071 if (const Arg *A = Args.getLastArg(options::OPT_mfpmath_EQ)) {
3072 CmdArgs.push_back("-mfpmath");
3073 CmdArgs.push_back(A->getValue());
3074 }
3075
Rafael Espindola146dbbf2013-08-21 16:39:20 +00003076 // Add the target features
Stephen Hines651f13c2014-04-23 16:59:28 -07003077 getTargetFeatures(D, ETriple, Args, CmdArgs, false);
Rafael Espindola146dbbf2013-08-21 16:39:20 +00003078
Rafael Espindolab330e402013-08-20 22:12:08 +00003079 // Add target specific flags.
Eli Bendersky8f4269a2013-07-24 22:20:49 +00003080 switch(getToolChain().getArch()) {
Daniel Dunbar6acda162009-09-09 22:33:08 +00003081 default:
3082 break;
Daniel Dunbar868bd0a2009-05-06 03:16:41 +00003083
Daniel Dunbarb163ef72009-09-10 04:57:17 +00003084 case llvm::Triple::arm:
Stephen Hines651f13c2014-04-23 16:59:28 -07003085 case llvm::Triple::armeb:
Daniel Dunbarb163ef72009-09-10 04:57:17 +00003086 case llvm::Triple::thumb:
Stephen Hines651f13c2014-04-23 16:59:28 -07003087 case llvm::Triple::thumbeb:
Daniel Dunbarfa41d692011-03-17 17:10:06 +00003088 AddARMTargetArgs(Args, CmdArgs, KernelOrKext);
Daniel Dunbarb163ef72009-09-10 04:57:17 +00003089 break;
3090
Stephen Hines6bcf27b2014-05-29 04:14:42 -07003091 case llvm::Triple::aarch64:
3092 case llvm::Triple::aarch64_be:
Stephen Hines6bcf27b2014-05-29 04:14:42 -07003093 AddAArch64TargetArgs(Args, CmdArgs);
Stephen Hines651f13c2014-04-23 16:59:28 -07003094 break;
3095
Eric Christophered734732010-03-02 02:41:08 +00003096 case llvm::Triple::mips:
3097 case llvm::Triple::mipsel:
Akira Hatanaka7ec02582011-09-21 02:13:07 +00003098 case llvm::Triple::mips64:
3099 case llvm::Triple::mips64el:
Eric Christophered734732010-03-02 02:41:08 +00003100 AddMIPSTargetArgs(Args, CmdArgs);
3101 break;
3102
Stephen Hines176edba2014-12-01 14:53:08 -08003103 case llvm::Triple::ppc:
3104 case llvm::Triple::ppc64:
3105 case llvm::Triple::ppc64le:
3106 AddPPCTargetArgs(Args, CmdArgs);
3107 break;
3108
Bruno Cardoso Lopes9284d212010-11-09 17:21:19 +00003109 case llvm::Triple::sparc:
Stephen Hines176edba2014-12-01 14:53:08 -08003110 case llvm::Triple::sparcv9:
Bruno Cardoso Lopes9284d212010-11-09 17:21:19 +00003111 AddSparcTargetArgs(Args, CmdArgs);
3112 break;
3113
Daniel Dunbar6acda162009-09-09 22:33:08 +00003114 case llvm::Triple::x86:
3115 case llvm::Triple::x86_64:
3116 AddX86TargetArgs(Args, CmdArgs);
3117 break;
Tony Linthicum96319392011-12-12 21:14:55 +00003118
3119 case llvm::Triple::hexagon:
3120 AddHexagonTargetArgs(Args, CmdArgs);
3121 break;
Daniel Dunbarbc85be82009-04-29 18:32:25 +00003122 }
3123
Hans Wennborgb3574792013-08-08 00:17:41 +00003124 // Add clang-cl arguments.
3125 if (getToolChain().getDriver().IsCLMode())
3126 AddClangCLArgs(Args, CmdArgs);
3127
Daniel Dunbarc176bc62010-08-11 23:07:47 +00003128 // Pass the linker version in use.
3129 if (Arg *A = Args.getLastArg(options::OPT_mlinker_version_EQ)) {
3130 CmdArgs.push_back("-target-linker-version");
Richard Smith1d489cf2012-11-01 04:30:05 +00003131 CmdArgs.push_back(A->getValue());
Daniel Dunbarc176bc62010-08-11 23:07:47 +00003132 }
3133
Eric Christopherd3e22df2013-04-03 01:58:53 +00003134 if (!shouldUseLeafFramePointer(Args, getToolChain().getTriple()))
Daniel Dunbar1ad66482010-07-01 01:31:45 +00003135 CmdArgs.push_back("-momit-leaf-frame-pointer");
3136
Daniel Dunbarb30575c2010-05-12 18:19:58 +00003137 // Explicitly error on some things we know we don't support and can't just
3138 // ignore.
3139 types::ID InputType = Inputs[0].getType();
Daniel Dunbare94db472010-09-24 19:39:37 +00003140 if (!Args.hasArg(options::OPT_fallow_unsupported)) {
3141 Arg *Unsupported;
Daniel Dunbare94db472010-09-24 19:39:37 +00003142 if (types::isCXX(InputType) &&
Bob Wilson905c45f2011-10-14 05:03:44 +00003143 getToolChain().getTriple().isOSDarwin() &&
Eli Bendersky8f4269a2013-07-24 22:20:49 +00003144 getToolChain().getArch() == llvm::Triple::x86) {
Bob Wilsona544aee2011-08-13 23:48:55 +00003145 if ((Unsupported = Args.getLastArg(options::OPT_fapple_kext)) ||
3146 (Unsupported = Args.getLastArg(options::OPT_mkernel)))
Chris Lattner5f9e2722011-07-23 10:55:15 +00003147 D.Diag(diag::err_drv_clang_unsupported_opt_cxx_darwin_i386)
Daniel Dunbare94db472010-09-24 19:39:37 +00003148 << Unsupported->getOption().getName();
3149 }
Daniel Dunbarb30575c2010-05-12 18:19:58 +00003150 }
3151
Daniel Dunbar1d460332009-03-18 10:01:51 +00003152 Args.AddAllArgs(CmdArgs, options::OPT_v);
Daniel Dunbarf7c16d92010-08-24 22:44:13 +00003153 Args.AddLastArg(CmdArgs, options::OPT_H);
Chad Rosier2b819102011-08-02 17:58:04 +00003154 if (D.CCPrintHeaders && !D.CCGenDiagnostics) {
Daniel Dunbar322c29f2011-02-02 21:11:35 +00003155 CmdArgs.push_back("-header-include-file");
3156 CmdArgs.push_back(D.CCPrintHeadersFilename ?
3157 D.CCPrintHeadersFilename : "-");
3158 }
Daniel Dunbar1d460332009-03-18 10:01:51 +00003159 Args.AddLastArg(CmdArgs, options::OPT_P);
Mike Stump1eb44332009-09-09 15:08:12 +00003160 Args.AddLastArg(CmdArgs, options::OPT_print_ivar_layout);
Daniel Dunbar1d460332009-03-18 10:01:51 +00003161
Chad Rosier2b819102011-08-02 17:58:04 +00003162 if (D.CCLogDiagnostics && !D.CCGenDiagnostics) {
Daniel Dunbarc8a22b02011-04-07 18:01:20 +00003163 CmdArgs.push_back("-diagnostic-log-file");
3164 CmdArgs.push_back(D.CCLogDiagnosticsFilename ?
3165 D.CCLogDiagnosticsFilename : "-");
3166 }
3167
Stephen Hines651f13c2014-04-23 16:59:28 -07003168 // Use the last option from "-g" group. "-gline-tables-only" and "-gdwarf-x"
3169 // are preserved, all other debug options are substituted with "-g".
Rafael Espindola18f36d92010-03-07 04:46:18 +00003170 Args.ClaimAllArgs(options::OPT_g_Group);
Alexey Samsonova9cd83b2012-05-29 08:10:34 +00003171 if (Arg *A = Args.getLastArg(options::OPT_g_Group)) {
Stephen Hines176edba2014-12-01 14:53:08 -08003172 if (A->getOption().matches(options::OPT_gline_tables_only) ||
3173 A->getOption().matches(options::OPT_g1)) {
Stephen Hines651f13c2014-04-23 16:59:28 -07003174 // FIXME: we should support specifying dwarf version with
3175 // -gline-tables-only.
Alexey Samsonova9cd83b2012-05-29 08:10:34 +00003176 CmdArgs.push_back("-gline-tables-only");
Stephen Hines176edba2014-12-01 14:53:08 -08003177 // Default is dwarf-2 for Darwin, OpenBSD, FreeBSD and Solaris.
Stephen Hinesc568f1e2014-07-21 00:47:37 -07003178 const llvm::Triple &Triple = getToolChain().getTriple();
3179 if (Triple.isOSDarwin() || Triple.getOS() == llvm::Triple::OpenBSD ||
Stephen Hines176edba2014-12-01 14:53:08 -08003180 Triple.getOS() == llvm::Triple::FreeBSD ||
3181 Triple.getOS() == llvm::Triple::Solaris)
Stephen Hines651f13c2014-04-23 16:59:28 -07003182 CmdArgs.push_back("-gdwarf-2");
3183 } else if (A->getOption().matches(options::OPT_gdwarf_2))
Manman Renfc0f91c2013-06-19 01:46:49 +00003184 CmdArgs.push_back("-gdwarf-2");
3185 else if (A->getOption().matches(options::OPT_gdwarf_3))
3186 CmdArgs.push_back("-gdwarf-3");
3187 else if (A->getOption().matches(options::OPT_gdwarf_4))
3188 CmdArgs.push_back("-gdwarf-4");
Eric Christopherc706c8e2013-02-05 07:29:57 +00003189 else if (!A->getOption().matches(options::OPT_g0) &&
Manman Ren8ed38d82013-07-02 23:15:25 +00003190 !A->getOption().matches(options::OPT_ggdb0)) {
Stephen Hines176edba2014-12-01 14:53:08 -08003191 // Default is dwarf-2 for Darwin, OpenBSD, FreeBSD and Solaris.
Stephen Hinesc568f1e2014-07-21 00:47:37 -07003192 const llvm::Triple &Triple = getToolChain().getTriple();
3193 if (Triple.isOSDarwin() || Triple.getOS() == llvm::Triple::OpenBSD ||
Stephen Hines176edba2014-12-01 14:53:08 -08003194 Triple.getOS() == llvm::Triple::FreeBSD ||
3195 Triple.getOS() == llvm::Triple::Solaris)
Manman Ren8ed38d82013-07-02 23:15:25 +00003196 CmdArgs.push_back("-gdwarf-2");
3197 else
3198 CmdArgs.push_back("-g");
3199 }
Alexey Samsonova9cd83b2012-05-29 08:10:34 +00003200 }
Daniel Dunbar1d460332009-03-18 10:01:51 +00003201
Alexey Samsonov7f326072012-06-21 08:22:39 +00003202 // We ignore flags -gstrict-dwarf and -grecord-gcc-switches for now.
3203 Args.ClaimAllArgs(options::OPT_g_flags_Group);
Stephen Hines176edba2014-12-01 14:53:08 -08003204 if (Args.hasFlag(options::OPT_gcolumn_info, options::OPT_gno_column_info,
3205 /*Default*/ true))
Eric Christopherda3301e2012-10-18 21:52:18 +00003206 CmdArgs.push_back("-dwarf-column-info");
Alexey Samsonov7f326072012-06-21 08:22:39 +00003207
Eric Christopher0f43a6d2013-09-13 22:37:55 +00003208 // FIXME: Move backend command line options to the module.
Eric Christopherc706c8e2013-02-05 07:29:57 +00003209 // -gsplit-dwarf should turn on -g and enable the backend dwarf
3210 // splitting and extraction.
Eric Christopherf870e122013-02-21 22:35:05 +00003211 // FIXME: Currently only works on Linux.
Cameron Esfahani57b1da12013-09-14 01:09:11 +00003212 if (getToolChain().getTriple().isOSLinux() &&
Eric Christopherf870e122013-02-21 22:35:05 +00003213 Args.hasArg(options::OPT_gsplit_dwarf)) {
Eric Christopherc706c8e2013-02-05 07:29:57 +00003214 CmdArgs.push_back("-g");
3215 CmdArgs.push_back("-backend-option");
3216 CmdArgs.push_back("-split-dwarf=Enable");
3217 }
3218
Eric Christopher0f43a6d2013-09-13 22:37:55 +00003219 // -ggnu-pubnames turns on gnu style pubnames in the backend.
3220 if (Args.hasArg(options::OPT_ggnu_pubnames)) {
3221 CmdArgs.push_back("-backend-option");
3222 CmdArgs.push_back("-generate-gnu-dwarf-pub-sections");
3223 }
Eric Christopher3e8ac1b2013-06-18 00:03:50 +00003224
Stephen Hines651f13c2014-04-23 16:59:28 -07003225 // -gdwarf-aranges turns on the emission of the aranges section in the
3226 // backend.
3227 if (Args.hasArg(options::OPT_gdwarf_aranges)) {
3228 CmdArgs.push_back("-backend-option");
3229 CmdArgs.push_back("-generate-arange-section");
3230 }
Eric Christopher3e8ac1b2013-06-18 00:03:50 +00003231
Stephen Hines651f13c2014-04-23 16:59:28 -07003232 if (Args.hasFlag(options::OPT_fdebug_types_section,
3233 options::OPT_fno_debug_types_section, false)) {
3234 CmdArgs.push_back("-backend-option");
3235 CmdArgs.push_back("-generate-type-units");
3236 }
3237
3238 if (Args.hasFlag(options::OPT_ffunction_sections,
3239 options::OPT_fno_function_sections, false)) {
3240 CmdArgs.push_back("-ffunction-sections");
3241 }
3242
3243 if (Args.hasFlag(options::OPT_fdata_sections,
3244 options::OPT_fno_data_sections, false)) {
3245 CmdArgs.push_back("-fdata-sections");
3246 }
Rafael Espindola9cf933a2010-05-06 21:06:04 +00003247
Chris Lattner7255a2d2010-06-22 00:03:40 +00003248 Args.AddAllArgs(CmdArgs, options::OPT_finstrument_functions);
3249
Stephen Hines651f13c2014-04-23 16:59:28 -07003250 if (Args.hasArg(options::OPT_fprofile_instr_generate) &&
3251 (Args.hasArg(options::OPT_fprofile_instr_use) ||
3252 Args.hasArg(options::OPT_fprofile_instr_use_EQ)))
3253 D.Diag(diag::err_drv_argument_not_allowed_with)
3254 << "-fprofile-instr-generate" << "-fprofile-instr-use";
3255
3256 Args.AddAllArgs(CmdArgs, options::OPT_fprofile_instr_generate);
3257
3258 if (Arg *A = Args.getLastArg(options::OPT_fprofile_instr_use_EQ))
3259 A->render(Args, CmdArgs);
3260 else if (Args.hasArg(options::OPT_fprofile_instr_use))
3261 CmdArgs.push_back("-fprofile-instr-use=pgo-data");
3262
Nick Lewyckye8ba8d72011-04-21 23:44:07 +00003263 if (Args.hasArg(options::OPT_ftest_coverage) ||
3264 Args.hasArg(options::OPT_coverage))
3265 CmdArgs.push_back("-femit-coverage-notes");
Stephen Hines176edba2014-12-01 14:53:08 -08003266 if (Args.hasFlag(options::OPT_fprofile_arcs, options::OPT_fno_profile_arcs,
3267 false) ||
Nick Lewyckye8ba8d72011-04-21 23:44:07 +00003268 Args.hasArg(options::OPT_coverage))
3269 CmdArgs.push_back("-femit-coverage-data");
3270
Stephen Hines176edba2014-12-01 14:53:08 -08003271 if (Args.hasArg(options::OPT_fcoverage_mapping) &&
3272 !Args.hasArg(options::OPT_fprofile_instr_generate))
3273 D.Diag(diag::err_drv_argument_only_allowed_with)
3274 << "-fcoverage-mapping" << "-fprofile-instr-generate";
3275
3276 if (Args.hasArg(options::OPT_fcoverage_mapping))
3277 CmdArgs.push_back("-fcoverage-mapping");
3278
Nick Lewycky5ea4f442011-05-04 20:46:58 +00003279 if (C.getArgs().hasArg(options::OPT_c) ||
3280 C.getArgs().hasArg(options::OPT_S)) {
3281 if (Output.isFilename()) {
Nick Lewycky3dc05412011-05-05 00:08:20 +00003282 CmdArgs.push_back("-coverage-file");
Stephen Hines176edba2014-12-01 14:53:08 -08003283 SmallString<128> CoverageFilename;
3284 if (Arg *FinalOutput = C.getArgs().getLastArg(options::OPT_o)) {
3285 CoverageFilename = FinalOutput->getValue();
3286 } else {
3287 CoverageFilename = llvm::sys::path::filename(Output.getBaseInput());
3288 }
Nick Lewycky0f815f12013-03-07 08:28:53 +00003289 if (llvm::sys::path::is_relative(CoverageFilename.str())) {
Rafael Espindolaa2148242013-08-10 01:40:10 +00003290 SmallString<128> Pwd;
3291 if (!llvm::sys::fs::current_path(Pwd)) {
3292 llvm::sys::path::append(Pwd, CoverageFilename.str());
3293 CoverageFilename.swap(Pwd);
Nick Lewycky0f815f12013-03-07 08:28:53 +00003294 }
3295 }
Eric Christopher025b3d42013-02-22 00:24:40 +00003296 CmdArgs.push_back(Args.MakeArgString(CoverageFilename));
Nick Lewycky5ea4f442011-05-04 20:46:58 +00003297 }
3298 }
3299
Daniel Dunbara268fc02011-10-11 18:20:10 +00003300 // Pass options for controlling the default header search paths.
3301 if (Args.hasArg(options::OPT_nostdinc)) {
3302 CmdArgs.push_back("-nostdsysteminc");
3303 CmdArgs.push_back("-nobuiltininc");
3304 } else {
Daniel Dunbar92d6d402011-10-11 18:20:16 +00003305 if (Args.hasArg(options::OPT_nostdlibinc))
3306 CmdArgs.push_back("-nostdsysteminc");
Daniel Dunbara268fc02011-10-11 18:20:10 +00003307 Args.AddLastArg(CmdArgs, options::OPT_nostdincxx);
3308 Args.AddLastArg(CmdArgs, options::OPT_nobuiltininc);
3309 }
Daniel Dunbar1d460332009-03-18 10:01:51 +00003310
Daniel Dunbar5f122322009-12-15 01:02:52 +00003311 // Pass the path to compiler resource files.
Daniel Dunbar5f122322009-12-15 01:02:52 +00003312 CmdArgs.push_back("-resource-dir");
Daniel Dunbar225c4172010-01-20 02:35:16 +00003313 CmdArgs.push_back(D.ResourceDir.c_str());
Daniel Dunbar2ac9fc22009-04-07 21:42:00 +00003314
Argyrios Kyrtzidis389db162010-11-03 22:45:23 +00003315 Args.AddLastArg(CmdArgs, options::OPT_working_directory);
3316
Ted Kremenek30660a82012-03-06 20:06:33 +00003317 bool ARCMTEnabled = false;
Argyrios Kyrtzidisdce3ce32013-09-17 19:14:29 +00003318 if (!Args.hasArg(options::OPT_fno_objc_arc, options::OPT_fobjc_arc)) {
Argyrios Kyrtzidis72ac1202011-07-07 04:00:39 +00003319 if (const Arg *A = Args.getLastArg(options::OPT_ccc_arcmt_check,
Argyrios Kyrtzidis69325d52011-07-09 20:00:58 +00003320 options::OPT_ccc_arcmt_modify,
3321 options::OPT_ccc_arcmt_migrate)) {
Ted Kremenek30660a82012-03-06 20:06:33 +00003322 ARCMTEnabled = true;
John McCall8f0e8d22011-06-15 23:25:17 +00003323 switch (A->getOption().getID()) {
3324 default:
3325 llvm_unreachable("missed a case");
Argyrios Kyrtzidis72ac1202011-07-07 04:00:39 +00003326 case options::OPT_ccc_arcmt_check:
John McCall8f0e8d22011-06-15 23:25:17 +00003327 CmdArgs.push_back("-arcmt-check");
3328 break;
Argyrios Kyrtzidis72ac1202011-07-07 04:00:39 +00003329 case options::OPT_ccc_arcmt_modify:
John McCall8f0e8d22011-06-15 23:25:17 +00003330 CmdArgs.push_back("-arcmt-modify");
3331 break;
Argyrios Kyrtzidis69325d52011-07-09 20:00:58 +00003332 case options::OPT_ccc_arcmt_migrate:
3333 CmdArgs.push_back("-arcmt-migrate");
Ted Kremenek30660a82012-03-06 20:06:33 +00003334 CmdArgs.push_back("-mt-migrate-directory");
Richard Smith1d489cf2012-11-01 04:30:05 +00003335 CmdArgs.push_back(A->getValue());
Argyrios Kyrtzidis7ee20492011-07-19 17:20:03 +00003336
3337 Args.AddLastArg(CmdArgs, options::OPT_arcmt_migrate_report_output);
3338 Args.AddLastArg(CmdArgs, options::OPT_arcmt_migrate_emit_arc_errors);
Argyrios Kyrtzidis69325d52011-07-09 20:00:58 +00003339 break;
John McCall8f0e8d22011-06-15 23:25:17 +00003340 }
3341 }
Argyrios Kyrtzidisf75ece42013-06-24 19:01:18 +00003342 } else {
3343 Args.ClaimAllArgs(options::OPT_ccc_arcmt_check);
3344 Args.ClaimAllArgs(options::OPT_ccc_arcmt_modify);
3345 Args.ClaimAllArgs(options::OPT_ccc_arcmt_migrate);
John McCall8f0e8d22011-06-15 23:25:17 +00003346 }
Eric Christopher88b7cf02011-08-19 00:30:14 +00003347
Ted Kremenek30660a82012-03-06 20:06:33 +00003348 if (const Arg *A = Args.getLastArg(options::OPT_ccc_objcmt_migrate)) {
3349 if (ARCMTEnabled) {
3350 D.Diag(diag::err_drv_argument_not_allowed_with)
3351 << A->getAsString(Args) << "-ccc-arcmt-migrate";
3352 }
3353 CmdArgs.push_back("-mt-migrate-directory");
Richard Smith1d489cf2012-11-01 04:30:05 +00003354 CmdArgs.push_back(A->getValue());
Ted Kremenek30660a82012-03-06 20:06:33 +00003355
3356 if (!Args.hasArg(options::OPT_objcmt_migrate_literals,
Fariborz Jahaniand4129992013-07-09 16:59:14 +00003357 options::OPT_objcmt_migrate_subscripting,
3358 options::OPT_objcmt_migrate_property)) {
Ted Kremenek30660a82012-03-06 20:06:33 +00003359 // None specified, means enable them all.
3360 CmdArgs.push_back("-objcmt-migrate-literals");
3361 CmdArgs.push_back("-objcmt-migrate-subscripting");
Fariborz Jahaniand4129992013-07-09 16:59:14 +00003362 CmdArgs.push_back("-objcmt-migrate-property");
Ted Kremenek30660a82012-03-06 20:06:33 +00003363 } else {
3364 Args.AddLastArg(CmdArgs, options::OPT_objcmt_migrate_literals);
3365 Args.AddLastArg(CmdArgs, options::OPT_objcmt_migrate_subscripting);
Fariborz Jahaniand4129992013-07-09 16:59:14 +00003366 Args.AddLastArg(CmdArgs, options::OPT_objcmt_migrate_property);
Ted Kremenek30660a82012-03-06 20:06:33 +00003367 }
Argyrios Kyrtzidis17c384c2013-11-13 23:38:20 +00003368 } else {
3369 Args.AddLastArg(CmdArgs, options::OPT_objcmt_migrate_literals);
3370 Args.AddLastArg(CmdArgs, options::OPT_objcmt_migrate_subscripting);
3371 Args.AddLastArg(CmdArgs, options::OPT_objcmt_migrate_property);
3372 Args.AddLastArg(CmdArgs, options::OPT_objcmt_migrate_all);
3373 Args.AddLastArg(CmdArgs, options::OPT_objcmt_migrate_readonly_property);
3374 Args.AddLastArg(CmdArgs, options::OPT_objcmt_migrate_readwrite_property);
3375 Args.AddLastArg(CmdArgs, options::OPT_objcmt_migrate_annotation);
3376 Args.AddLastArg(CmdArgs, options::OPT_objcmt_migrate_instancetype);
3377 Args.AddLastArg(CmdArgs, options::OPT_objcmt_migrate_nsmacros);
3378 Args.AddLastArg(CmdArgs, options::OPT_objcmt_migrate_protocol_conformance);
3379 Args.AddLastArg(CmdArgs, options::OPT_objcmt_atomic_property);
3380 Args.AddLastArg(CmdArgs, options::OPT_objcmt_returns_innerpointer_property);
3381 Args.AddLastArg(CmdArgs, options::OPT_objcmt_ns_nonatomic_iosonly);
Stephen Hines651f13c2014-04-23 16:59:28 -07003382 Args.AddLastArg(CmdArgs, options::OPT_objcmt_migrate_designated_init);
3383 Args.AddLastArg(CmdArgs, options::OPT_objcmt_whitelist_dir_path);
Ted Kremenek30660a82012-03-06 20:06:33 +00003384 }
3385
Daniel Dunbarc21c4852009-04-08 23:54:23 +00003386 // Add preprocessing options like -I, -D, etc. if we are using the
3387 // preprocessor.
3388 //
3389 // FIXME: Support -fpreprocessed
Daniel Dunbarc21c4852009-04-08 23:54:23 +00003390 if (types::getPreprocessedType(InputType) != types::TY_INVALID)
Chad Rosier9d718632013-01-24 19:14:47 +00003391 AddPreprocessingOptions(C, JA, D, Args, CmdArgs, Output, Inputs);
Daniel Dunbar1d460332009-03-18 10:01:51 +00003392
Rafael Espindola19d9d2e2011-07-21 23:40:37 +00003393 // Don't warn about "clang -c -DPIC -fPIC test.i" because libtool.m4 assumes
3394 // that "The compiler can only warn and ignore the option if not recognized".
3395 // When building with ccache, it will pass -D options to clang even on
3396 // preprocessed inputs and configure concludes that -fPIC is not supported.
3397 Args.ClaimAllArgs(options::OPT_D);
3398
Alp Tokere22017e2013-11-15 20:40:58 +00003399 // Manually translate -O4 to -O3; let clang reject others.
Rafael Espindola55ee1eb2013-08-27 16:58:15 +00003400 if (Arg *A = Args.getLastArg(options::OPT_O_Group)) {
3401 if (A->getOption().matches(options::OPT_O4)) {
3402 CmdArgs.push_back("-O3");
3403 D.Diag(diag::warn_O4_is_O3);
3404 } else {
3405 A->render(Args, CmdArgs);
3406 }
3407 }
Daniel Dunbar1d460332009-03-18 10:01:51 +00003408
Stephen Hines176edba2014-12-01 14:53:08 -08003409 // Warn about ignored options to clang.
3410 for (arg_iterator it = Args.filtered_begin(
3411 options::OPT_clang_ignored_gcc_optimization_f_Group),
3412 ie = Args.filtered_end(); it != ie; ++it) {
3413 D.Diag(diag::warn_ignored_gcc_optimization) << (*it)->getAsString(Args);
3414 }
3415
Chad Rosierb2c08872012-12-12 20:06:31 +00003416 // Don't warn about unused -flto. This can happen when we're preprocessing or
3417 // precompiling.
3418 Args.ClaimAllArgs(options::OPT_flto);
3419
Stephen Hines176edba2014-12-01 14:53:08 -08003420 Args.AddAllArgs(CmdArgs, options::OPT_R_Group);
Daniel Dunbar6e8371e2009-10-29 02:24:45 +00003421 Args.AddAllArgs(CmdArgs, options::OPT_W_Group);
Ted Kremeneke8cf7d12012-07-07 05:53:30 +00003422 if (Args.hasFlag(options::OPT_pedantic, options::OPT_no_pedantic, false))
3423 CmdArgs.push_back("-pedantic");
Daniel Dunbar6e8371e2009-10-29 02:24:45 +00003424 Args.AddLastArg(CmdArgs, options::OPT_pedantic_errors);
Daniel Dunbar1d460332009-03-18 10:01:51 +00003425 Args.AddLastArg(CmdArgs, options::OPT_w);
Daniel Dunbard573d262009-04-07 22:13:21 +00003426
3427 // Handle -{std, ansi, trigraphs} -- take the last of -{std, ansi}
Hans Wennborgadbb4552013-07-31 16:57:56 +00003428 // (-ansi is equivalent to -std=c89 or -std=c++98).
Daniel Dunbard573d262009-04-07 22:13:21 +00003429 //
3430 // If a std is supplied, only add -trigraphs if it follows the
3431 // option.
3432 if (Arg *Std = Args.getLastArg(options::OPT_std_EQ, options::OPT_ansi)) {
3433 if (Std->getOption().matches(options::OPT_ansi))
Nuno Lopes528365d2009-10-16 14:28:06 +00003434 if (types::isCXX(InputType))
Daniel Dunbar294691e2009-11-04 06:24:38 +00003435 CmdArgs.push_back("-std=c++98");
Nuno Lopes528365d2009-10-16 14:28:06 +00003436 else
Daniel Dunbar294691e2009-11-04 06:24:38 +00003437 CmdArgs.push_back("-std=c89");
Daniel Dunbard573d262009-04-07 22:13:21 +00003438 else
3439 Std->render(Args, CmdArgs);
3440
Daniel Dunbar0e100312010-06-14 21:23:08 +00003441 if (Arg *A = Args.getLastArg(options::OPT_std_EQ, options::OPT_ansi,
3442 options::OPT_trigraphs))
3443 if (A != Std)
Daniel Dunbard573d262009-04-07 22:13:21 +00003444 A->render(Args, CmdArgs);
Daniel Dunbara3ff2022009-04-26 01:10:38 +00003445 } else {
3446 // Honor -std-default.
Daniel Dunbar4a5290e2010-01-29 21:03:02 +00003447 //
3448 // FIXME: Clang doesn't correctly handle -std= when the input language
3449 // doesn't match. For the time being just ignore this for C++ inputs;
3450 // eventually we want to do all the standard defaulting here instead of
3451 // splitting it between the driver and clang -cc1.
3452 if (!types::isCXX(InputType))
Nico Weber50f88b92012-08-30 02:08:31 +00003453 Args.AddAllArgsTranslated(CmdArgs, options::OPT_std_default_EQ,
3454 "-std=", /*Joined=*/true);
Stephen Hines651f13c2014-04-23 16:59:28 -07003455 else if (IsWindowsMSVC)
Nico Weber50f88b92012-08-30 02:08:31 +00003456 CmdArgs.push_back("-std=c++11");
3457
Daniel Dunbard573d262009-04-07 22:13:21 +00003458 Args.AddLastArg(CmdArgs, options::OPT_trigraphs);
Daniel Dunbara3ff2022009-04-26 01:10:38 +00003459 }
Daniel Dunbarc21c4852009-04-08 23:54:23 +00003460
Richard Smithe9813b32013-09-04 22:50:31 +00003461 // GCC's behavior for -Wwrite-strings is a bit strange:
3462 // * In C, this "warning flag" changes the types of string literals from
3463 // 'char[N]' to 'const char[N]', and thus triggers an unrelated warning
3464 // for the discarded qualifier.
3465 // * In C++, this is just a normal warning flag.
3466 //
3467 // Implementing this warning correctly in C is hard, so we follow GCC's
3468 // behavior for now. FIXME: Directly diagnose uses of a string literal as
3469 // a non-const char* in C, rather than using this crude hack.
3470 if (!types::isCXX(InputType)) {
Stephen Hines651f13c2014-04-23 16:59:28 -07003471 // FIXME: This should behave just like a warning flag, and thus should also
3472 // respect -Weverything, -Wno-everything, -Werror=write-strings, and so on.
3473 Arg *WriteStrings =
3474 Args.getLastArg(options::OPT_Wwrite_strings,
3475 options::OPT_Wno_write_strings, options::OPT_w);
3476 if (WriteStrings &&
3477 WriteStrings->getOption().matches(options::OPT_Wwrite_strings))
Richard Smithe9813b32013-09-04 22:50:31 +00003478 CmdArgs.push_back("-fconst-strings");
Chandler Carruth50465d12011-04-23 06:30:43 +00003479 }
3480
Chandler Carruth1cfe3c32011-04-23 09:27:53 +00003481 // GCC provides a macro definition '__DEPRECATED' when -Wdeprecated is active
Chandler Carruthf8c247d2011-04-23 19:48:40 +00003482 // during C++ compilation, which it is by default. GCC keeps this define even
3483 // in the presence of '-w', match this behavior bug-for-bug.
3484 if (types::isCXX(InputType) &&
3485 Args.hasFlag(options::OPT_Wdeprecated, options::OPT_Wno_deprecated,
3486 true)) {
3487 CmdArgs.push_back("-fdeprecated-macro");
Chandler Carruth1cfe3c32011-04-23 09:27:53 +00003488 }
3489
Chandler Carruthc304ba32010-05-22 02:21:53 +00003490 // Translate GCC's misnamer '-fasm' arguments to '-fgnu-keywords'.
3491 if (Arg *Asm = Args.getLastArg(options::OPT_fasm, options::OPT_fno_asm)) {
3492 if (Asm->getOption().matches(options::OPT_fasm))
3493 CmdArgs.push_back("-fgnu-keywords");
3494 else
3495 CmdArgs.push_back("-fno-gnu-keywords");
3496 }
3497
Nick Lewyckyea523d72011-10-17 23:05:52 +00003498 if (ShouldDisableDwarfDirectory(Args, getToolChain()))
3499 CmdArgs.push_back("-fno-dwarf-directory-asm");
3500
Daniel Dunbarf4910132013-04-16 18:21:19 +00003501 if (ShouldDisableAutolink(Args, getToolChain()))
3502 CmdArgs.push_back("-fno-autolink");
3503
Chandler Carruthd566df62012-12-17 21:40:04 +00003504 // Add in -fdebug-compilation-dir if necessary.
3505 addDebugCompDirArg(Args, CmdArgs);
Nick Lewycky7c4fd912011-10-21 02:32:14 +00003506
Richard Smithc18c4232011-11-21 19:36:32 +00003507 if (Arg *A = Args.getLastArg(options::OPT_ftemplate_depth_,
3508 options::OPT_ftemplate_depth_EQ)) {
Daniel Dunbar1d460332009-03-18 10:01:51 +00003509 CmdArgs.push_back("-ftemplate-depth");
Richard Smith1d489cf2012-11-01 04:30:05 +00003510 CmdArgs.push_back(A->getValue());
Daniel Dunbar1d460332009-03-18 10:01:51 +00003511 }
3512
Richard Smith195dd7c2013-11-06 19:31:51 +00003513 if (Arg *A = Args.getLastArg(options::OPT_foperator_arrow_depth_EQ)) {
3514 CmdArgs.push_back("-foperator-arrow-depth");
3515 CmdArgs.push_back(A->getValue());
3516 }
3517
Richard Smithc18c4232011-11-21 19:36:32 +00003518 if (Arg *A = Args.getLastArg(options::OPT_fconstexpr_depth_EQ)) {
3519 CmdArgs.push_back("-fconstexpr-depth");
Richard Smith1d489cf2012-11-01 04:30:05 +00003520 CmdArgs.push_back(A->getValue());
Richard Smithc18c4232011-11-21 19:36:32 +00003521 }
3522
Richard Smithe7565632013-05-08 02:12:03 +00003523 if (Arg *A = Args.getLastArg(options::OPT_fconstexpr_steps_EQ)) {
3524 CmdArgs.push_back("-fconstexpr-steps");
3525 CmdArgs.push_back(A->getValue());
3526 }
3527
Richard Smith9e738cc2013-02-22 01:59:51 +00003528 if (Arg *A = Args.getLastArg(options::OPT_fbracket_depth_EQ)) {
3529 CmdArgs.push_back("-fbracket-depth");
3530 CmdArgs.push_back(A->getValue());
3531 }
3532
Argyrios Kyrtzidis1380a142010-11-18 00:20:36 +00003533 if (Arg *A = Args.getLastArg(options::OPT_Wlarge_by_value_copy_EQ,
3534 options::OPT_Wlarge_by_value_copy_def)) {
Jean-Daniel Dupas2e4fd6d2012-05-04 08:08:37 +00003535 if (A->getNumValues()) {
Richard Smith1d489cf2012-11-01 04:30:05 +00003536 StringRef bytes = A->getValue();
Jean-Daniel Dupas2e4fd6d2012-05-04 08:08:37 +00003537 CmdArgs.push_back(Args.MakeArgString("-Wlarge-by-value-copy=" + bytes));
3538 } else
3539 CmdArgs.push_back("-Wlarge-by-value-copy=64"); // default value
Argyrios Kyrtzidis3532fdd2010-11-17 23:11:54 +00003540 }
3541
Nuno Lopesb3198a82012-05-08 22:10:46 +00003542
Michael J. Spencerc6357102012-10-22 22:13:48 +00003543 if (Args.hasArg(options::OPT_relocatable_pch))
Daniel Dunbar66861e02009-11-20 22:21:36 +00003544 CmdArgs.push_back("-relocatable-pch");
Mike Stump1eb44332009-09-09 15:08:12 +00003545
Daniel Dunbar294691e2009-11-04 06:24:38 +00003546 if (Arg *A = Args.getLastArg(options::OPT_fconstant_string_class_EQ)) {
3547 CmdArgs.push_back("-fconstant-string-class");
Richard Smith1d489cf2012-11-01 04:30:05 +00003548 CmdArgs.push_back(A->getValue());
Daniel Dunbar294691e2009-11-04 06:24:38 +00003549 }
David Chisnall8a5a9aa2009-08-31 16:41:57 +00003550
Chris Lattner124fca52010-01-09 21:54:33 +00003551 if (Arg *A = Args.getLastArg(options::OPT_ftabstop_EQ)) {
3552 CmdArgs.push_back("-ftabstop");
Richard Smith1d489cf2012-11-01 04:30:05 +00003553 CmdArgs.push_back(A->getValue());
Chris Lattner124fca52010-01-09 21:54:33 +00003554 }
3555
Chris Lattner0f0c9632010-04-07 20:49:23 +00003556 CmdArgs.push_back("-ferror-limit");
3557 if (Arg *A = Args.getLastArg(options::OPT_ferror_limit_EQ))
Richard Smith1d489cf2012-11-01 04:30:05 +00003558 CmdArgs.push_back(A->getValue());
Chris Lattner0f0c9632010-04-07 20:49:23 +00003559 else
3560 CmdArgs.push_back("19");
Douglas Gregor575cf372010-04-20 07:18:24 +00003561
Chandler Carruthc40f73c2010-05-06 04:55:18 +00003562 if (Arg *A = Args.getLastArg(options::OPT_fmacro_backtrace_limit_EQ)) {
3563 CmdArgs.push_back("-fmacro-backtrace-limit");
Richard Smith1d489cf2012-11-01 04:30:05 +00003564 CmdArgs.push_back(A->getValue());
Chandler Carruthc40f73c2010-05-06 04:55:18 +00003565 }
3566
3567 if (Arg *A = Args.getLastArg(options::OPT_ftemplate_backtrace_limit_EQ)) {
3568 CmdArgs.push_back("-ftemplate-backtrace-limit");
Richard Smith1d489cf2012-11-01 04:30:05 +00003569 CmdArgs.push_back(A->getValue());
Chandler Carruthc40f73c2010-05-06 04:55:18 +00003570 }
3571
Richard Smith08d6e032011-12-16 19:06:07 +00003572 if (Arg *A = Args.getLastArg(options::OPT_fconstexpr_backtrace_limit_EQ)) {
3573 CmdArgs.push_back("-fconstexpr-backtrace-limit");
Richard Smith1d489cf2012-11-01 04:30:05 +00003574 CmdArgs.push_back(A->getValue());
Richard Smith08d6e032011-12-16 19:06:07 +00003575 }
3576
Daniel Dunbar55efe142009-11-04 06:24:47 +00003577 // Pass -fmessage-length=.
Daniel Dunbara28690e2009-11-30 08:40:54 +00003578 CmdArgs.push_back("-fmessage-length");
Daniel Dunbar55efe142009-11-04 06:24:47 +00003579 if (Arg *A = Args.getLastArg(options::OPT_fmessage_length_EQ)) {
Richard Smith1d489cf2012-11-01 04:30:05 +00003580 CmdArgs.push_back(A->getValue());
Daniel Dunbar55efe142009-11-04 06:24:47 +00003581 } else {
3582 // If -fmessage-length=N was not specified, determine whether this is a
3583 // terminal and, if so, implicitly define -fmessage-length appropriately.
3584 unsigned N = llvm::sys::Process::StandardErrColumns();
Chris Lattner5f9e2722011-07-23 10:55:15 +00003585 CmdArgs.push_back(Args.MakeArgString(Twine(N)));
Daniel Dunbar55efe142009-11-04 06:24:47 +00003586 }
3587
John McCalla880b192013-02-19 01:57:35 +00003588 // -fvisibility= and -fvisibility-ms-compat are of a piece.
3589 if (const Arg *A = Args.getLastArg(options::OPT_fvisibility_EQ,
3590 options::OPT_fvisibility_ms_compat)) {
3591 if (A->getOption().matches(options::OPT_fvisibility_EQ)) {
3592 CmdArgs.push_back("-fvisibility");
3593 CmdArgs.push_back(A->getValue());
3594 } else {
3595 assert(A->getOption().matches(options::OPT_fvisibility_ms_compat));
3596 CmdArgs.push_back("-fvisibility");
3597 CmdArgs.push_back("hidden");
3598 CmdArgs.push_back("-ftype-visibility");
3599 CmdArgs.push_back("default");
3600 }
Daniel Dunbarba8d8612009-12-03 18:42:11 +00003601 }
3602
Douglas Gregor7cf84d62010-06-15 17:05:35 +00003603 Args.AddLastArg(CmdArgs, options::OPT_fvisibility_inlines_hidden);
Michael J. Spencer20249a12010-10-21 03:16:25 +00003604
Hans Wennborgde981f32012-06-28 08:01:44 +00003605 Args.AddLastArg(CmdArgs, options::OPT_ftlsmodel_EQ);
3606
Daniel Dunbar0a80ba72010-03-20 04:52:14 +00003607 // -fhosted is default.
Chad Rosierafc4baa2012-03-26 22:04:46 +00003608 if (Args.hasFlag(options::OPT_ffreestanding, options::OPT_fhosted, false) ||
3609 KernelOrKext)
Daniel Dunbar0a80ba72010-03-20 04:52:14 +00003610 CmdArgs.push_back("-ffreestanding");
3611
Daniel Dunbarba8d8612009-12-03 18:42:11 +00003612 // Forward -f (flag) options which we can pass directly.
Daniel Dunbar3aaf0822009-04-07 21:51:40 +00003613 Args.AddLastArg(CmdArgs, options::OPT_femit_all_decls);
Daniel Dunbar3aaf0822009-04-07 21:51:40 +00003614 Args.AddLastArg(CmdArgs, options::OPT_fheinous_gnu_extensions);
Stephen Hines651f13c2014-04-23 16:59:28 -07003615 Args.AddLastArg(CmdArgs, options::OPT_fstandalone_debug);
3616 Args.AddLastArg(CmdArgs, options::OPT_fno_standalone_debug);
Eric Christophere88c4512011-10-25 07:13:06 +00003617 Args.AddLastArg(CmdArgs, options::OPT_fno_operator_names);
Bill Schmidt9e0b6472013-07-03 15:36:02 +00003618 // AltiVec language extensions aren't relevant for assembling.
3619 if (!isa<PreprocessJobAction>(JA) ||
3620 Output.getType() != types::TY_PP_Asm)
3621 Args.AddLastArg(CmdArgs, options::OPT_faltivec);
Richard Trieu246b6aa2012-06-26 18:18:47 +00003622 Args.AddLastArg(CmdArgs, options::OPT_fdiagnostics_show_template_tree);
3623 Args.AddLastArg(CmdArgs, options::OPT_fno_elide_type);
Chad Rosier4574c3d2012-03-13 23:45:51 +00003624
Peter Collingbournec6911a22013-11-01 18:16:25 +00003625 const SanitizerArgs &Sanitize = getToolChain().getSanitizerArgs();
3626 Sanitize.addArgs(Args, CmdArgs);
Richard Smithc4dabad2012-11-05 22:04:41 +00003627
Eric Christopher98654c92013-02-19 06:16:53 +00003628 // Report an error for -faltivec on anything other than PowerPC.
Chad Rosier4574c3d2012-03-13 23:45:51 +00003629 if (const Arg *A = Args.getLastArg(options::OPT_faltivec))
Eli Bendersky8f4269a2013-07-24 22:20:49 +00003630 if (!(getToolChain().getArch() == llvm::Triple::ppc ||
Bill Schmidtea7fb0c2013-07-26 01:36:11 +00003631 getToolChain().getArch() == llvm::Triple::ppc64 ||
3632 getToolChain().getArch() == llvm::Triple::ppc64le))
Chad Rosier4574c3d2012-03-13 23:45:51 +00003633 D.Diag(diag::err_drv_argument_only_allowed_with)
Bill Schmidtea7fb0c2013-07-26 01:36:11 +00003634 << A->getAsString(Args) << "ppc/ppc64/ppc64le";
Chad Rosier4574c3d2012-03-13 23:45:51 +00003635
Daniel Dunbarbbe8e3e2011-03-01 18:49:30 +00003636 if (getToolChain().SupportsProfiling())
3637 Args.AddLastArg(CmdArgs, options::OPT_pg);
Daniel Dunbar8c6fa842010-03-16 16:57:46 +00003638
3639 // -flax-vector-conversions is default.
3640 if (!Args.hasFlag(options::OPT_flax_vector_conversions,
3641 options::OPT_fno_lax_vector_conversions))
3642 CmdArgs.push_back("-fno-lax-vector-conversions");
3643
Fariborz Jahanianb466d012011-01-07 01:05:02 +00003644 if (Args.getLastArg(options::OPT_fapple_kext))
3645 CmdArgs.push_back("-fapple-kext");
3646
Fariborz Jahanian34e65772009-05-22 20:17:16 +00003647 Args.AddLastArg(CmdArgs, options::OPT_fobjc_sender_dependent_dispatch);
Chris Lattner182e0922009-04-21 05:34:31 +00003648 Args.AddLastArg(CmdArgs, options::OPT_fdiagnostics_print_source_range_info);
Douglas Gregor4786c152010-08-19 20:24:43 +00003649 Args.AddLastArg(CmdArgs, options::OPT_fdiagnostics_parseable_fixits);
Daniel Dunbar3aaf0822009-04-07 21:51:40 +00003650 Args.AddLastArg(CmdArgs, options::OPT_ftime_report);
3651 Args.AddLastArg(CmdArgs, options::OPT_ftrapv);
David Chisnall7f18e672010-09-17 18:29:54 +00003652
3653 if (Arg *A = Args.getLastArg(options::OPT_ftrapv_handler_EQ)) {
3654 CmdArgs.push_back("-ftrapv-handler");
Richard Smith1d489cf2012-11-01 04:30:05 +00003655 CmdArgs.push_back(A->getValue());
David Chisnall7f18e672010-09-17 18:29:54 +00003656 }
3657
Bob Wilson71fd6cc2012-02-03 06:27:22 +00003658 Args.AddLastArg(CmdArgs, options::OPT_ftrap_function_EQ);
Evan Cheng49af1f32011-04-08 21:37:45 +00003659
Chandler Carruth5adb5a82011-03-27 00:04:55 +00003660 // -fno-strict-overflow implies -fwrapv if it isn't disabled, but
3661 // -fstrict-overflow won't turn off an explicitly enabled -fwrapv.
3662 if (Arg *A = Args.getLastArg(options::OPT_fwrapv,
3663 options::OPT_fno_wrapv)) {
3664 if (A->getOption().matches(options::OPT_fwrapv))
3665 CmdArgs.push_back("-fwrapv");
3666 } else if (Arg *A = Args.getLastArg(options::OPT_fstrict_overflow,
3667 options::OPT_fno_strict_overflow)) {
3668 if (A->getOption().matches(options::OPT_fno_strict_overflow))
3669 CmdArgs.push_back("-fwrapv");
3670 }
Hal Finkelce5b5f12013-11-17 16:03:29 +00003671
3672 if (Arg *A = Args.getLastArg(options::OPT_freroll_loops,
3673 options::OPT_fno_reroll_loops))
3674 if (A->getOption().matches(options::OPT_freroll_loops))
3675 CmdArgs.push_back("-freroll-loops");
3676
Daniel Dunbar3aaf0822009-04-07 21:51:40 +00003677 Args.AddLastArg(CmdArgs, options::OPT_fwritable_strings);
Chandler Carruthb26404a2013-08-08 08:34:35 +00003678 Args.AddLastArg(CmdArgs, options::OPT_funroll_loops,
3679 options::OPT_fno_unroll_loops);
Daniel Dunbar1d460332009-03-18 10:01:51 +00003680
Daniel Dunbar5345c392009-09-03 04:54:28 +00003681 Args.AddLastArg(CmdArgs, options::OPT_pthread);
3682
Mahesha Sf3b52312012-10-27 07:47:56 +00003683
Daniel Dunbar9e5cc6b2009-11-17 08:07:36 +00003684 // -stack-protector=0 is default.
3685 unsigned StackProtectorLevel = 0;
Bill Wendling45483f72009-06-28 07:36:13 +00003686 if (Arg *A = Args.getLastArg(options::OPT_fno_stack_protector,
3687 options::OPT_fstack_protector_all,
Stephen Hines651f13c2014-04-23 16:59:28 -07003688 options::OPT_fstack_protector_strong,
Bill Wendling45483f72009-06-28 07:36:13 +00003689 options::OPT_fstack_protector)) {
Stephen Hines6bcf27b2014-05-29 04:14:42 -07003690 if (A->getOption().matches(options::OPT_fstack_protector)) {
3691 StackProtectorLevel = std::max<unsigned>(LangOptions::SSPOn,
3692 getToolChain().GetDefaultStackProtectorLevel(KernelOrKext));
3693 } else if (A->getOption().matches(options::OPT_fstack_protector_strong))
Stephen Hines651f13c2014-04-23 16:59:28 -07003694 StackProtectorLevel = LangOptions::SSPStrong;
Daniel Dunbar9e5cc6b2009-11-17 08:07:36 +00003695 else if (A->getOption().matches(options::OPT_fstack_protector_all))
Stephen Hines651f13c2014-04-23 16:59:28 -07003696 StackProtectorLevel = LangOptions::SSPReq;
Nico Weber2fef1112011-08-23 07:38:27 +00003697 } else {
3698 StackProtectorLevel =
3699 getToolChain().GetDefaultStackProtectorLevel(KernelOrKext);
3700 }
Daniel Dunbar9e5cc6b2009-11-17 08:07:36 +00003701 if (StackProtectorLevel) {
3702 CmdArgs.push_back("-stack-protector");
Chris Lattner5f9e2722011-07-23 10:55:15 +00003703 CmdArgs.push_back(Args.MakeArgString(Twine(StackProtectorLevel)));
Joerg Sonnenberger53b43a72012-09-12 13:51:14 +00003704 }
Chad Rosiera7afeb02012-08-21 16:16:06 +00003705
Joerg Sonnenberger53b43a72012-09-12 13:51:14 +00003706 // --param ssp-buffer-size=
3707 for (arg_iterator it = Args.filtered_begin(options::OPT__param),
3708 ie = Args.filtered_end(); it != ie; ++it) {
Richard Smith1d489cf2012-11-01 04:30:05 +00003709 StringRef Str((*it)->getValue());
Joerg Sonnenberger53b43a72012-09-12 13:51:14 +00003710 if (Str.startswith("ssp-buffer-size=")) {
3711 if (StackProtectorLevel) {
Chad Rosiera7afeb02012-08-21 16:16:06 +00003712 CmdArgs.push_back("-stack-protector-buffer-size");
3713 // FIXME: Verify the argument is a valid integer.
3714 CmdArgs.push_back(Args.MakeArgString(Str.drop_front(16)));
Chad Rosiera7afeb02012-08-21 16:16:06 +00003715 }
Joerg Sonnenberger53b43a72012-09-12 13:51:14 +00003716 (*it)->claim();
Chad Rosiera7afeb02012-08-21 16:16:06 +00003717 }
Bill Wendling45483f72009-06-28 07:36:13 +00003718 }
3719
Nick Lewycky4e785c92011-12-06 03:33:03 +00003720 // Translate -mstackrealign
3721 if (Args.hasFlag(options::OPT_mstackrealign, options::OPT_mno_stackrealign,
3722 false)) {
3723 CmdArgs.push_back("-backend-option");
3724 CmdArgs.push_back("-force-align-stack");
3725 }
3726 if (!Args.hasFlag(options::OPT_mno_stackrealign, options::OPT_mstackrealign,
3727 false)) {
3728 CmdArgs.push_back(Args.MakeArgString("-mstackrealign"));
3729 }
3730
Joerg Sonnenbergere9d11db2011-12-05 23:05:23 +00003731 if (Args.hasArg(options::OPT_mstack_alignment)) {
3732 StringRef alignment = Args.getLastArgValue(options::OPT_mstack_alignment);
3733 CmdArgs.push_back(Args.MakeArgString("-mstack-alignment=" + alignment));
Eric Christopher1a584022011-05-02 21:18:22 +00003734 }
Stephen Hines176edba2014-12-01 14:53:08 -08003735
3736 if (getToolChain().getTriple().getArch() == llvm::Triple::aarch64 ||
3737 getToolChain().getTriple().getArch() == llvm::Triple::aarch64_be)
3738 CmdArgs.push_back("-fallow-half-arguments-and-returns");
Eric Christopher88b7cf02011-08-19 00:30:14 +00003739
Weiming Zhao7792fde2013-11-13 18:31:23 +00003740 if (Arg *A = Args.getLastArg(options::OPT_mrestrict_it,
3741 options::OPT_mno_restrict_it)) {
3742 if (A->getOption().matches(options::OPT_mrestrict_it)) {
3743 CmdArgs.push_back("-backend-option");
3744 CmdArgs.push_back("-arm-restrict-it");
3745 } else {
3746 CmdArgs.push_back("-backend-option");
3747 CmdArgs.push_back("-arm-no-restrict-it");
3748 }
Stephen Hines6bcf27b2014-05-29 04:14:42 -07003749 } else if (TT.isOSWindows() && (TT.getArch() == llvm::Triple::arm ||
3750 TT.getArch() == llvm::Triple::thumb)) {
3751 // Windows on ARM expects restricted IT blocks
3752 CmdArgs.push_back("-backend-option");
3753 CmdArgs.push_back("-arm-restrict-it");
Weiming Zhao7792fde2013-11-13 18:31:23 +00003754 }
3755
Stephen Hinesc568f1e2014-07-21 00:47:37 -07003756 if (TT.getArch() == llvm::Triple::arm ||
3757 TT.getArch() == llvm::Triple::thumb) {
3758 if (Arg *A = Args.getLastArg(options::OPT_mlong_calls,
3759 options::OPT_mno_long_calls)) {
3760 if (A->getOption().matches(options::OPT_mlong_calls)) {
3761 CmdArgs.push_back("-backend-option");
3762 CmdArgs.push_back("-arm-long-calls");
3763 }
3764 }
3765 }
3766
Daniel Dunbar48d1ef72009-04-07 21:16:11 +00003767 // Forward -f options with positive and negative forms; we translate
3768 // these by hand.
Diego Novillob85a9ec2013-11-13 12:22:39 +00003769 if (Arg *A = Args.getLastArg(options::OPT_fprofile_sample_use_EQ)) {
3770 StringRef fname = A->getValue();
3771 if (!llvm::sys::fs::exists(fname))
3772 D.Diag(diag::err_drv_no_such_file) << fname;
3773 else
3774 A->render(Args, CmdArgs);
3775 }
Daniel Dunbar48d1ef72009-04-07 21:16:11 +00003776
Fariborz Jahanianb466d012011-01-07 01:05:02 +00003777 if (Args.hasArg(options::OPT_mkernel)) {
Daniel Dunbar2843c192011-02-04 17:24:47 +00003778 if (!Args.hasArg(options::OPT_fapple_kext) && types::isCXX(InputType))
Fariborz Jahanianb466d012011-01-07 01:05:02 +00003779 CmdArgs.push_back("-fapple-kext");
3780 if (!Args.hasArg(options::OPT_fbuiltin))
3781 CmdArgs.push_back("-fno-builtin");
Chad Rosier3d265502012-03-26 21:29:17 +00003782 Args.ClaimAllArgs(options::OPT_fno_builtin);
Fariborz Jahanianb466d012011-01-07 01:05:02 +00003783 }
Daniel Dunbar9e5cc6b2009-11-17 08:07:36 +00003784 // -fbuiltin is default.
Fariborz Jahanianb466d012011-01-07 01:05:02 +00003785 else if (!Args.hasFlag(options::OPT_fbuiltin, options::OPT_fno_builtin))
Daniel Dunbar53e84842009-11-19 04:55:23 +00003786 CmdArgs.push_back("-fno-builtin");
Daniel Dunbar48d1ef72009-04-07 21:16:11 +00003787
Nuno Lopesfc284482009-12-16 16:59:22 +00003788 if (!Args.hasFlag(options::OPT_fassume_sane_operator_new,
3789 options::OPT_fno_assume_sane_operator_new))
3790 CmdArgs.push_back("-fno-assume-sane-operator-new");
3791
Daniel Dunbar9e5cc6b2009-11-17 08:07:36 +00003792 // -fblocks=0 is default.
3793 if (Args.hasFlag(options::OPT_fblocks, options::OPT_fno_blocks,
David Chisnalle6533ff2011-02-28 17:11:43 +00003794 getToolChain().IsBlocksDefault()) ||
3795 (Args.hasArg(options::OPT_fgnu_runtime) &&
3796 Args.hasArg(options::OPT_fobjc_nonfragile_abi) &&
3797 !Args.hasArg(options::OPT_fno_blocks))) {
Daniel Dunbar9e5cc6b2009-11-17 08:07:36 +00003798 CmdArgs.push_back("-fblocks");
John McCall13db5cf2011-09-09 20:41:01 +00003799
3800 if (!Args.hasArg(options::OPT_fgnu_runtime) &&
3801 !getToolChain().hasBlocksRuntime())
3802 CmdArgs.push_back("-fblocks-runtime-optional");
David Chisnall5e530af2009-11-17 19:33:30 +00003803 }
Daniel Dunbar48d1ef72009-04-07 21:16:11 +00003804
Stephen Hines176edba2014-12-01 14:53:08 -08003805 // -fmodules enables modules (off by default).
3806 // Users can pass -fno-cxx-modules to turn off modules support for
3807 // C++/Objective-C++ programs, which is a little less mature.
Douglas Gregorf43b7212013-01-16 01:23:41 +00003808 bool HaveModules = false;
Douglas Gregor64554ba2012-01-18 15:19:58 +00003809 if (Args.hasFlag(options::OPT_fmodules, options::OPT_fno_modules, false)) {
3810 bool AllowedInCXX = Args.hasFlag(options::OPT_fcxx_modules,
3811 options::OPT_fno_cxx_modules,
Stephen Hines176edba2014-12-01 14:53:08 -08003812 true);
Douglas Gregorf43b7212013-01-16 01:23:41 +00003813 if (AllowedInCXX || !types::isCXX(InputType)) {
Douglas Gregor64554ba2012-01-18 15:19:58 +00003814 CmdArgs.push_back("-fmodules");
Douglas Gregorf43b7212013-01-16 01:23:41 +00003815 HaveModules = true;
3816 }
3817 }
3818
Daniel Jasper056ec122013-08-05 20:26:17 +00003819 // -fmodule-maps enables module map processing (off by default) for header
3820 // checking. It is implied by -fmodules.
3821 if (Args.hasFlag(options::OPT_fmodule_maps, options::OPT_fno_module_maps,
3822 false)) {
3823 CmdArgs.push_back("-fmodule-maps");
3824 }
3825
Daniel Jasper95411412013-10-21 06:34:34 +00003826 // -fmodules-decluse checks that modules used are declared so (off by
3827 // default).
Daniel Jasperddd2dfc2013-09-24 09:14:14 +00003828 if (Args.hasFlag(options::OPT_fmodules_decluse,
3829 options::OPT_fno_modules_decluse,
3830 false)) {
Daniel Jasper097595a2013-09-29 12:40:54 +00003831 CmdArgs.push_back("-fmodules-decluse");
Daniel Jasperddd2dfc2013-09-24 09:14:14 +00003832 }
3833
Stephen Hines6bcf27b2014-05-29 04:14:42 -07003834 // -fmodules-strict-decluse is like -fmodule-decluse, but also checks that
3835 // all #included headers are part of modules.
3836 if (Args.hasFlag(options::OPT_fmodules_strict_decluse,
3837 options::OPT_fno_modules_strict_decluse,
3838 false)) {
3839 CmdArgs.push_back("-fmodules-strict-decluse");
3840 }
3841
Daniel Jasper95411412013-10-21 06:34:34 +00003842 // -fmodule-name specifies the module that is currently being built (or
3843 // used for header checking by -fmodule-maps).
Stephen Hines176edba2014-12-01 14:53:08 -08003844 Args.AddLastArg(CmdArgs, options::OPT_fmodule_name);
Daniel Jasper95411412013-10-21 06:34:34 +00003845
Stephen Hines176edba2014-12-01 14:53:08 -08003846 // -fmodule-map-file can be used to specify files containing module
Daniel Jasper95411412013-10-21 06:34:34 +00003847 // definitions.
Stephen Hines176edba2014-12-01 14:53:08 -08003848 Args.AddAllArgs(CmdArgs, options::OPT_fmodule_map_file);
Daniel Jasper95411412013-10-21 06:34:34 +00003849
Stephen Hines176edba2014-12-01 14:53:08 -08003850 // -fmodule-file can be used to specify files containing precompiled modules.
3851 Args.AddAllArgs(CmdArgs, options::OPT_fmodule_file);
3852
3853 // -fmodule-cache-path specifies where our implicitly-built module files
3854 // should be written.
Stephen Hinesc568f1e2014-07-21 00:47:37 -07003855 SmallString<128> ModuleCachePath;
3856 if (Arg *A = Args.getLastArg(options::OPT_fmodules_cache_path))
3857 ModuleCachePath = A->getValue();
3858 if (HaveModules) {
3859 if (C.isForDiagnostics()) {
3860 // When generating crash reports, we want to emit the modules along with
3861 // the reproduction sources, so we ignore any provided module path.
3862 ModuleCachePath = Output.getFilename();
3863 llvm::sys::path::replace_extension(ModuleCachePath, ".cache");
3864 llvm::sys::path::append(ModuleCachePath, "modules");
3865 } else if (ModuleCachePath.empty()) {
3866 // No module path was provided: use the default.
3867 llvm::sys::path::system_temp_directory(/*erasedOnReboot=*/false,
3868 ModuleCachePath);
3869 llvm::sys::path::append(ModuleCachePath, "org.llvm.clang");
3870 llvm::sys::path::append(ModuleCachePath, "ModuleCache");
Douglas Gregor953a61f2013-02-07 19:01:24 +00003871 }
Douglas Gregor250172a2013-02-07 22:59:12 +00003872 const char Arg[] = "-fmodules-cache-path=";
Stephen Hinesc568f1e2014-07-21 00:47:37 -07003873 ModuleCachePath.insert(ModuleCachePath.begin(), Arg, Arg + strlen(Arg));
3874 CmdArgs.push_back(Args.MakeArgString(ModuleCachePath));
Douglas Gregor953a61f2013-02-07 19:01:24 +00003875 }
3876
Stephen Hinesc568f1e2014-07-21 00:47:37 -07003877 // When building modules and generating crashdumps, we need to dump a module
3878 // dependency VFS alongside the output.
3879 if (HaveModules && C.isForDiagnostics()) {
3880 SmallString<128> VFSDir(Output.getFilename());
3881 llvm::sys::path::replace_extension(VFSDir, ".cache");
Stephen Hines176edba2014-12-01 14:53:08 -08003882 // Add the cache directory as a temp so the crash diagnostics pick it up.
3883 C.addTempFile(Args.MakeArgString(VFSDir));
3884
Stephen Hinesc568f1e2014-07-21 00:47:37 -07003885 llvm::sys::path::append(VFSDir, "vfs");
3886 CmdArgs.push_back("-module-dependency-dir");
3887 CmdArgs.push_back(Args.MakeArgString(VFSDir));
Stephen Hines651f13c2014-04-23 16:59:28 -07003888 }
3889
Stephen Hines176edba2014-12-01 14:53:08 -08003890 if (HaveModules)
3891 Args.AddLastArg(CmdArgs, options::OPT_fmodules_user_build_path);
Stephen Hinesc568f1e2014-07-21 00:47:37 -07003892
Douglas Gregor953a61f2013-02-07 19:01:24 +00003893 // Pass through all -fmodules-ignore-macro arguments.
3894 Args.AddAllArgs(CmdArgs, options::OPT_fmodules_ignore_macro);
Douglas Gregord44d2872013-03-25 21:19:16 +00003895 Args.AddLastArg(CmdArgs, options::OPT_fmodules_prune_interval);
3896 Args.AddLastArg(CmdArgs, options::OPT_fmodules_prune_after);
Douglas Gregor953a61f2013-02-07 19:01:24 +00003897
Stephen Hines651f13c2014-04-23 16:59:28 -07003898 Args.AddLastArg(CmdArgs, options::OPT_fbuild_session_timestamp);
3899
Stephen Hines176edba2014-12-01 14:53:08 -08003900 if (Arg *A = Args.getLastArg(options::OPT_fbuild_session_file)) {
3901 if (Args.hasArg(options::OPT_fbuild_session_timestamp))
3902 D.Diag(diag::err_drv_argument_not_allowed_with)
3903 << A->getAsString(Args) << "-fbuild-session-timestamp";
3904
3905 llvm::sys::fs::file_status Status;
3906 if (llvm::sys::fs::status(A->getValue(), Status))
3907 D.Diag(diag::err_drv_no_such_file) << A->getValue();
3908 char TimeStamp[48];
3909 snprintf(TimeStamp, sizeof(TimeStamp), "-fbuild-session-timestamp=%" PRIu64,
3910 (uint64_t)Status.getLastModificationTime().toEpochTime());
3911 CmdArgs.push_back(Args.MakeArgString(TimeStamp));
3912 }
3913
Stephen Hines651f13c2014-04-23 16:59:28 -07003914 if (Args.getLastArg(options::OPT_fmodules_validate_once_per_build_session)) {
Stephen Hines176edba2014-12-01 14:53:08 -08003915 if (!Args.getLastArg(options::OPT_fbuild_session_timestamp,
3916 options::OPT_fbuild_session_file))
Stephen Hines651f13c2014-04-23 16:59:28 -07003917 D.Diag(diag::err_drv_modules_validate_once_requires_timestamp);
3918
3919 Args.AddLastArg(CmdArgs,
3920 options::OPT_fmodules_validate_once_per_build_session);
3921 }
3922
3923 Args.AddLastArg(CmdArgs, options::OPT_fmodules_validate_system_headers);
3924
John McCall32579cf2010-04-09 19:12:06 +00003925 // -faccess-control is default.
John McCall7002f4c2010-04-09 19:03:51 +00003926 if (Args.hasFlag(options::OPT_fno_access_control,
3927 options::OPT_faccess_control,
John McCall32579cf2010-04-09 19:12:06 +00003928 false))
John McCall7002f4c2010-04-09 19:03:51 +00003929 CmdArgs.push_back("-fno-access-control");
John McCall3ddd6e02010-03-17 01:32:13 +00003930
Anders Carlssona4c24752010-11-21 00:09:52 +00003931 // -felide-constructors is the default.
3932 if (Args.hasFlag(options::OPT_fno_elide_constructors,
3933 options::OPT_felide_constructors,
3934 false))
3935 CmdArgs.push_back("-fno-elide-constructors");
3936
Daniel Dunbar0be42c42009-11-17 07:06:20 +00003937 // -frtti is default.
Chad Rosierafc4baa2012-03-26 22:04:46 +00003938 if (!Args.hasFlag(options::OPT_frtti, options::OPT_fno_rtti) ||
Richard Smithc4dabad2012-11-05 22:04:41 +00003939 KernelOrKext) {
Daniel Dunbar53e84842009-11-19 04:55:23 +00003940 CmdArgs.push_back("-fno-rtti");
Mike Stump738f8c22009-07-31 23:15:31 +00003941
Richard Smithc4dabad2012-11-05 22:04:41 +00003942 // -fno-rtti cannot usefully be combined with -fsanitize=vptr.
Alexey Samsonovbb1071c2012-11-06 15:09:03 +00003943 if (Sanitize.sanitizesVptr()) {
NAKAMURA Takumi03c60762012-11-06 22:02:00 +00003944 std::string NoRttiArg =
Richard Smithc4dabad2012-11-05 22:04:41 +00003945 Args.getLastArg(options::OPT_mkernel,
3946 options::OPT_fapple_kext,
Richard Smith04fd3822012-11-06 01:12:02 +00003947 options::OPT_fno_rtti)->getAsString(Args);
Richard Smithc4dabad2012-11-05 22:04:41 +00003948 D.Diag(diag::err_drv_argument_not_allowed_with)
3949 << "-fsanitize=vptr" << NoRttiArg;
3950 }
3951 }
3952
Tony Linthicum96319392011-12-12 21:14:55 +00003953 // -fshort-enums=0 is default for all architectures except Hexagon.
Argyrios Kyrtzidis9a2b9d72010-10-08 00:25:19 +00003954 if (Args.hasFlag(options::OPT_fshort_enums,
Tony Linthicum96319392011-12-12 21:14:55 +00003955 options::OPT_fno_short_enums,
Eli Bendersky8f4269a2013-07-24 22:20:49 +00003956 getToolChain().getArch() ==
Tony Linthicum96319392011-12-12 21:14:55 +00003957 llvm::Triple::hexagon))
Argyrios Kyrtzidis9a2b9d72010-10-08 00:25:19 +00003958 CmdArgs.push_back("-fshort-enums");
3959
Daniel Dunbar1f95e652009-11-17 06:37:03 +00003960 // -fsigned-char is default.
Daniel Dunbar6d2eb4d2009-11-25 10:14:30 +00003961 if (!Args.hasFlag(options::OPT_fsigned_char, options::OPT_funsigned_char,
Daniel Dunbar1f95e652009-11-17 06:37:03 +00003962 isSignedCharDefault(getToolChain().getTriple())))
Daniel Dunbar76743522009-11-29 02:39:08 +00003963 CmdArgs.push_back("-fno-signed-char");
Eli Friedman5a779732009-06-05 07:21:14 +00003964
Anders Carlssona508b7d2010-02-06 23:23:06 +00003965 // -fthreadsafe-static is default.
Michael J. Spencer20249a12010-10-21 03:16:25 +00003966 if (!Args.hasFlag(options::OPT_fthreadsafe_statics,
Anders Carlssona508b7d2010-02-06 23:23:06 +00003967 options::OPT_fno_threadsafe_statics))
3968 CmdArgs.push_back("-fno-threadsafe-statics");
3969
Daniel Dunbarefb0fa92010-03-20 04:15:41 +00003970 // -fuse-cxa-atexit is default.
Stephen Hines651f13c2014-04-23 16:59:28 -07003971 if (!Args.hasFlag(options::OPT_fuse_cxa_atexit,
3972 options::OPT_fno_use_cxa_atexit,
3973 !IsWindowsCygnus && !IsWindowsGNU &&
3974 getToolChain().getArch() != llvm::Triple::hexagon &&
3975 getToolChain().getArch() != llvm::Triple::xcore) ||
Chad Rosierafc4baa2012-03-26 22:04:46 +00003976 KernelOrKext)
Daniel Dunbarefb0fa92010-03-20 04:15:41 +00003977 CmdArgs.push_back("-fno-use-cxa-atexit");
3978
Daniel Dunbar0be42c42009-11-17 07:06:20 +00003979 // -fms-extensions=0 is default.
Daniel Dunbar6d2eb4d2009-11-25 10:14:30 +00003980 if (Args.hasFlag(options::OPT_fms_extensions, options::OPT_fno_ms_extensions,
Stephen Hines651f13c2014-04-23 16:59:28 -07003981 IsWindowsMSVC))
Daniel Dunbar0be42c42009-11-17 07:06:20 +00003982 CmdArgs.push_back("-fms-extensions");
3983
Francois Pichetae556082011-09-17 04:32:15 +00003984 // -fms-compatibility=0 is default.
Douglas Gregorba97b6e2011-10-24 15:49:38 +00003985 if (Args.hasFlag(options::OPT_fms_compatibility,
3986 options::OPT_fno_ms_compatibility,
Stephen Hines651f13c2014-04-23 16:59:28 -07003987 (IsWindowsMSVC && Args.hasFlag(options::OPT_fms_extensions,
3988 options::OPT_fno_ms_extensions,
3989 true))))
Francois Pichetae556082011-09-17 04:32:15 +00003990 CmdArgs.push_back("-fms-compatibility");
3991
Stephen Hines176edba2014-12-01 14:53:08 -08003992 // -fms-compatibility-version=17.00 is default.
Michael J. Spencerdae4ac42010-10-21 05:21:48 +00003993 if (Args.hasFlag(options::OPT_fms_extensions, options::OPT_fno_ms_extensions,
Stephen Hines176edba2014-12-01 14:53:08 -08003994 IsWindowsMSVC) || Args.hasArg(options::OPT_fmsc_version) ||
3995 Args.hasArg(options::OPT_fms_compatibility_version)) {
3996 const Arg *MSCVersion = Args.getLastArg(options::OPT_fmsc_version);
3997 const Arg *MSCompatibilityVersion =
3998 Args.getLastArg(options::OPT_fms_compatibility_version);
Michael J. Spencerdae4ac42010-10-21 05:21:48 +00003999
Stephen Hines176edba2014-12-01 14:53:08 -08004000 if (MSCVersion && MSCompatibilityVersion)
4001 D.Diag(diag::err_drv_argument_not_allowed_with)
4002 << MSCVersion->getAsString(Args)
4003 << MSCompatibilityVersion->getAsString(Args);
4004
4005 std::string Ver;
4006 if (MSCompatibilityVersion)
4007 Ver = Args.getLastArgValue(options::OPT_fms_compatibility_version);
4008 else if (MSCVersion)
4009 Ver = getMSCompatibilityVersion(MSCVersion->getValue());
4010
4011 if (Ver.empty())
4012 CmdArgs.push_back("-fms-compatibility-version=17.00");
4013 else
4014 CmdArgs.push_back(Args.MakeArgString("-fms-compatibility-version=" + Ver));
4015 }
Michael J. Spencerdae4ac42010-10-21 05:21:48 +00004016
Eric Christophercfc01e42013-02-18 00:38:31 +00004017 // -fno-borland-extensions is default.
Dawn Perchik400b6072010-09-02 23:59:25 +00004018 if (Args.hasFlag(options::OPT_fborland_extensions,
4019 options::OPT_fno_borland_extensions, false))
4020 CmdArgs.push_back("-fborland-extensions");
4021
Francois Pichet8efcc012011-09-01 16:38:08 +00004022 // -fno-delayed-template-parsing is default, except for Windows where MSVC STL
4023 // needs it.
Francois Pichet8387e2a2011-04-22 22:18:13 +00004024 if (Args.hasFlag(options::OPT_fdelayed_template_parsing,
Stephen Hines651f13c2014-04-23 16:59:28 -07004025 options::OPT_fno_delayed_template_parsing, IsWindowsMSVC))
Francois Pichet805bc1f2011-08-26 00:22:34 +00004026 CmdArgs.push_back("-fdelayed-template-parsing");
Francois Pichet8387e2a2011-04-22 22:18:13 +00004027
Chandler Carrutheb5d7b72010-04-17 20:17:31 +00004028 // -fgnu-keywords default varies depending on language; only pass if
4029 // specified.
4030 if (Arg *A = Args.getLastArg(options::OPT_fgnu_keywords,
Daniel Dunbar40788d92010-04-24 17:56:39 +00004031 options::OPT_fno_gnu_keywords))
4032 A->render(Args, CmdArgs);
Chandler Carrutheb5d7b72010-04-17 20:17:31 +00004033
Rafael Espindola01ba8542011-06-02 17:30:53 +00004034 if (Args.hasFlag(options::OPT_fgnu89_inline,
4035 options::OPT_fno_gnu89_inline,
4036 false))
Rafael Espindolafb3f4aa2011-06-02 16:13:27 +00004037 CmdArgs.push_back("-fgnu89-inline");
4038
Chad Rosierfc055f92012-03-15 22:31:42 +00004039 if (Args.hasArg(options::OPT_fno_inline))
4040 CmdArgs.push_back("-fno-inline");
4041
Chad Rosier634a4b12012-03-06 21:17:19 +00004042 if (Args.hasArg(options::OPT_fno_inline_functions))
4043 CmdArgs.push_back("-fno-inline-functions");
Chad Rosier250008b2012-03-06 18:49:20 +00004044
John McCall260611a2012-06-20 06:18:46 +00004045 ObjCRuntime objcRuntime = AddObjCRuntimeArgs(Args, CmdArgs, rewriteKind);
John McCall9f084a32011-07-06 00:26:06 +00004046
John McCall260611a2012-06-20 06:18:46 +00004047 // -fobjc-dispatch-method is only relevant with the nonfragile-abi, and
Stephen Hines651f13c2014-04-23 16:59:28 -07004048 // legacy is the default. Except for deployment taget of 10.5,
4049 // next runtime is always legacy dispatch and -fno-objc-legacy-dispatch
4050 // gets ignored silently.
4051 if (objcRuntime.isNonFragile()) {
David Chisnall3c3ccd22011-09-30 13:32:35 +00004052 if (!Args.hasFlag(options::OPT_fobjc_legacy_dispatch,
4053 options::OPT_fno_objc_legacy_dispatch,
David Chisnall2c7886d2012-07-04 11:52:24 +00004054 objcRuntime.isLegacyDispatchDefaultForArch(
Eli Bendersky8f4269a2013-07-24 22:20:49 +00004055 getToolChain().getArch()))) {
David Chisnall3c3ccd22011-09-30 13:32:35 +00004056 if (getToolChain().UseObjCMixedDispatch())
4057 CmdArgs.push_back("-fobjc-dispatch-method=mixed");
4058 else
4059 CmdArgs.push_back("-fobjc-dispatch-method=non-legacy");
4060 }
4061 }
Rafael Espindola669496b2013-11-12 04:33:56 +00004062
Fariborz Jahanian5d5058c2013-11-12 17:08:46 +00004063 // When ObjectiveC legacy runtime is in effect on MacOSX,
4064 // turn on the option to do Array/Dictionary subscripting
4065 // by default.
Fariborz Jahanian08d86e92013-11-12 20:50:26 +00004066 if (getToolChain().getTriple().getArch() == llvm::Triple::x86 &&
4067 getToolChain().getTriple().isMacOSX() &&
4068 !getToolChain().getTriple().isMacOSXVersionLT(10, 7) &&
4069 objcRuntime.getKind() == ObjCRuntime::FragileMacOSX &&
Fariborz Jahanian5d5058c2013-11-12 17:08:46 +00004070 objcRuntime.isNeXTFamily())
4071 CmdArgs.push_back("-fobjc-subscripting-legacy-runtime");
4072
Fariborz Jahanian3d145f62012-11-15 19:02:45 +00004073 // -fencode-extended-block-signature=1 is default.
4074 if (getToolChain().IsEncodeExtendedBlockSignatureDefault()) {
4075 CmdArgs.push_back("-fencode-extended-block-signature");
4076 }
4077
John McCall9f084a32011-07-06 00:26:06 +00004078 // Allow -fno-objc-arr to trump -fobjc-arr/-fobjc-arc.
4079 // NOTE: This logic is duplicated in ToolChains.cpp.
4080 bool ARC = isObjCAutoRefCount(Args);
4081 if (ARC) {
John McCall0a7dd782012-08-21 02:47:43 +00004082 getToolChain().CheckObjCARC();
Argyrios Kyrtzidis5840dd92012-02-29 03:43:52 +00004083
John McCall9f084a32011-07-06 00:26:06 +00004084 CmdArgs.push_back("-fobjc-arc");
4085
Chandler Carruth7ffa0322011-11-04 07:34:47 +00004086 // FIXME: It seems like this entire block, and several around it should be
4087 // wrapped in isObjC, but for now we just use it here as this is where it
4088 // was being used previously.
4089 if (types::isCXX(InputType) && types::isObjC(InputType)) {
4090 if (getToolChain().GetCXXStdlibType(Args) == ToolChain::CST_Libcxx)
4091 CmdArgs.push_back("-fobjc-arc-cxxlib=libc++");
4092 else
4093 CmdArgs.push_back("-fobjc-arc-cxxlib=libstdc++");
4094 }
4095
John McCall9f084a32011-07-06 00:26:06 +00004096 // Allow the user to enable full exceptions code emission.
4097 // We define off for Objective-CC, on for Objective-C++.
4098 if (Args.hasFlag(options::OPT_fobjc_arc_exceptions,
4099 options::OPT_fno_objc_arc_exceptions,
4100 /*default*/ types::isCXX(InputType)))
4101 CmdArgs.push_back("-fobjc-arc-exceptions");
4102 }
4103
4104 // -fobjc-infer-related-result-type is the default, except in the Objective-C
4105 // rewriter.
John McCall260611a2012-06-20 06:18:46 +00004106 if (rewriteKind != RK_None)
John McCall9f084a32011-07-06 00:26:06 +00004107 CmdArgs.push_back("-fno-objc-infer-related-result-type");
Eric Christopher88b7cf02011-08-19 00:30:14 +00004108
John McCall9f084a32011-07-06 00:26:06 +00004109 // Handle -fobjc-gc and -fobjc-gc-only. They are exclusive, and -fobjc-gc-only
4110 // takes precedence.
4111 const Arg *GCArg = Args.getLastArg(options::OPT_fobjc_gc_only);
4112 if (!GCArg)
4113 GCArg = Args.getLastArg(options::OPT_fobjc_gc);
4114 if (GCArg) {
4115 if (ARC) {
Chris Lattner5f9e2722011-07-23 10:55:15 +00004116 D.Diag(diag::err_drv_objc_gc_arr)
John McCall9f084a32011-07-06 00:26:06 +00004117 << GCArg->getAsString(Args);
4118 } else if (getToolChain().SupportsObjCGC()) {
4119 GCArg->render(Args, CmdArgs);
4120 } else {
4121 // FIXME: We should move this to a hard error.
Chris Lattner5f9e2722011-07-23 10:55:15 +00004122 D.Diag(diag::warn_drv_objc_gc_unsupported)
John McCall9f084a32011-07-06 00:26:06 +00004123 << GCArg->getAsString(Args);
4124 }
4125 }
4126
Stephen Hinesc568f1e2014-07-21 00:47:37 -07004127 // Handle GCC-style exception args.
4128 if (!C.getDriver().IsCLMode())
4129 addExceptionArgs(Args, InputType, getToolChain().getTriple(), KernelOrKext,
4130 objcRuntime, CmdArgs);
John McCalld71315c2011-06-22 00:53:57 +00004131
4132 if (getToolChain().UseSjLjExceptions())
4133 CmdArgs.push_back("-fsjlj-exceptions");
4134
4135 // C++ "sane" operator new.
Daniel Dunbar984eb862010-02-01 21:07:25 +00004136 if (!Args.hasFlag(options::OPT_fassume_sane_operator_new,
4137 options::OPT_fno_assume_sane_operator_new))
4138 CmdArgs.push_back("-fno-assume-sane-operator-new");
4139
Daniel Dunbarf35f14d2010-04-27 15:34:57 +00004140 // -fconstant-cfstrings is default, and may be subject to argument translation
4141 // on Darwin.
4142 if (!Args.hasFlag(options::OPT_fconstant_cfstrings,
4143 options::OPT_fno_constant_cfstrings) ||
4144 !Args.hasFlag(options::OPT_mconstant_cfstrings,
4145 options::OPT_mno_constant_cfstrings))
4146 CmdArgs.push_back("-fno-constant-cfstrings");
4147
John Thompsona6fda122009-11-05 20:14:16 +00004148 // -fshort-wchar default varies depending on platform; only
4149 // pass if specified.
Stephen Hines651f13c2014-04-23 16:59:28 -07004150 if (Arg *A = Args.getLastArg(options::OPT_fshort_wchar,
4151 options::OPT_fno_short_wchar))
Daniel Dunbar1744a352010-04-27 15:35:03 +00004152 A->render(Args, CmdArgs);
John Thompsona6fda122009-11-05 20:14:16 +00004153
Hans Wennborgb087a5d2013-07-31 23:39:13 +00004154 // -fno-pascal-strings is default, only pass non-default.
Daniel Dunbarc21c4852009-04-08 23:54:23 +00004155 if (Args.hasFlag(options::OPT_fpascal_strings,
Daniel Dunbar82d00682009-04-07 23:51:44 +00004156 options::OPT_fno_pascal_strings,
Daniel Dunbar82d00682009-04-07 23:51:44 +00004157 false))
Daniel Dunbar48d1ef72009-04-07 21:16:11 +00004158 CmdArgs.push_back("-fpascal-strings");
NAKAMURA Takumi125b4cb2011-02-17 08:50:50 +00004159
Daniel Dunbar88934e82011-10-05 21:04:55 +00004160 // Honor -fpack-struct= and -fpack-struct, if given. Note that
4161 // -fno-pack-struct doesn't apply to -fpack-struct=.
4162 if (Arg *A = Args.getLastArg(options::OPT_fpack_struct_EQ)) {
James Molloy8049c442012-05-02 07:56:14 +00004163 std::string PackStructStr = "-fpack-struct=";
Richard Smith1d489cf2012-11-01 04:30:05 +00004164 PackStructStr += A->getValue();
James Molloy8049c442012-05-02 07:56:14 +00004165 CmdArgs.push_back(Args.MakeArgString(PackStructStr));
Daniel Dunbar88934e82011-10-05 21:04:55 +00004166 } else if (Args.hasFlag(options::OPT_fpack_struct,
4167 options::OPT_fno_pack_struct, false)) {
James Molloy8049c442012-05-02 07:56:14 +00004168 CmdArgs.push_back("-fpack-struct=1");
Daniel Dunbar88934e82011-10-05 21:04:55 +00004169 }
4170
Stephen Hines176edba2014-12-01 14:53:08 -08004171 // Handle -fmax-type-align=N and -fno-type-align
4172 bool SkipMaxTypeAlign = Args.hasArg(options::OPT_fno_max_type_align);
4173 if (Arg *A = Args.getLastArg(options::OPT_fmax_type_align_EQ)) {
4174 if (!SkipMaxTypeAlign) {
4175 std::string MaxTypeAlignStr = "-fmax-type-align=";
4176 MaxTypeAlignStr += A->getValue();
4177 CmdArgs.push_back(Args.MakeArgString(MaxTypeAlignStr));
4178 }
4179 } else if (getToolChain().getTriple().isOSDarwin()) {
4180 if (!SkipMaxTypeAlign) {
4181 std::string MaxTypeAlignStr = "-fmax-type-align=16";
4182 CmdArgs.push_back(Args.MakeArgString(MaxTypeAlignStr));
4183 }
4184 }
4185
Robert Lytton5f15f4d2013-08-13 09:43:10 +00004186 if (KernelOrKext || isNoCommonDefault(getToolChain().getTriple())) {
Fariborz Jahanianb466d012011-01-07 01:05:02 +00004187 if (!Args.hasArg(options::OPT_fcommon))
4188 CmdArgs.push_back("-fno-common");
Chad Rosierec09b3e2012-03-26 21:35:40 +00004189 Args.ClaimAllArgs(options::OPT_fno_common);
Fariborz Jahanianb466d012011-01-07 01:05:02 +00004190 }
Daniel Dunbar88934e82011-10-05 21:04:55 +00004191
Daniel Dunbar48d1ef72009-04-07 21:16:11 +00004192 // -fcommon is default, only pass non-default.
Fariborz Jahanianb466d012011-01-07 01:05:02 +00004193 else if (!Args.hasFlag(options::OPT_fcommon, options::OPT_fno_common))
Daniel Dunbar48d1ef72009-04-07 21:16:11 +00004194 CmdArgs.push_back("-fno-common");
4195
Daniel Dunbar70d3c922009-04-15 02:37:43 +00004196 // -fsigned-bitfields is default, and clang doesn't yet support
Daniel Dunbar06205ca2010-10-15 22:30:42 +00004197 // -funsigned-bitfields.
Mike Stump1eb44332009-09-09 15:08:12 +00004198 if (!Args.hasFlag(options::OPT_fsigned_bitfields,
Daniel Dunbar70d3c922009-04-15 02:37:43 +00004199 options::OPT_funsigned_bitfields))
Chris Lattner5f9e2722011-07-23 10:55:15 +00004200 D.Diag(diag::warn_drv_clang_unsupported)
Daniel Dunbar70d3c922009-04-15 02:37:43 +00004201 << Args.getLastArg(options::OPT_funsigned_bitfields)->getAsString(Args);
4202
Daniel Dunbar06205ca2010-10-15 22:30:42 +00004203 // -fsigned-bitfields is default, and clang doesn't support -fno-for-scope.
4204 if (!Args.hasFlag(options::OPT_ffor_scope,
4205 options::OPT_fno_for_scope))
Chris Lattner5f9e2722011-07-23 10:55:15 +00004206 D.Diag(diag::err_drv_clang_unsupported)
Daniel Dunbar06205ca2010-10-15 22:30:42 +00004207 << Args.getLastArg(options::OPT_fno_for_scope)->getAsString(Args);
4208
Stephen Hinesc568f1e2014-07-21 00:47:37 -07004209 // -finput_charset=UTF-8 is default. Reject others
4210 if (Arg *inputCharset = Args.getLastArg(
4211 options::OPT_finput_charset_EQ)) {
4212 StringRef value = inputCharset->getValue();
4213 if (value != "UTF-8")
4214 D.Diag(diag::err_drv_invalid_value) << inputCharset->getAsString(Args) << value;
4215 }
4216
Stephen Hines176edba2014-12-01 14:53:08 -08004217 // -fexec_charset=UTF-8 is default. Reject others
4218 if (Arg *execCharset = Args.getLastArg(
4219 options::OPT_fexec_charset_EQ)) {
4220 StringRef value = execCharset->getValue();
4221 if (value != "UTF-8")
4222 D.Diag(diag::err_drv_invalid_value) << execCharset->getAsString(Args) << value;
4223 }
4224
Jeffrey Yasskin0ea22fd2010-06-08 04:56:20 +00004225 // -fcaret-diagnostics is default.
4226 if (!Args.hasFlag(options::OPT_fcaret_diagnostics,
4227 options::OPT_fno_caret_diagnostics, true))
4228 CmdArgs.push_back("-fno-caret-diagnostics");
4229
Daniel Dunbar49138fc2009-04-19 21:09:34 +00004230 // -fdiagnostics-fixit-info is default, only pass non-default.
Mike Stump1eb44332009-09-09 15:08:12 +00004231 if (!Args.hasFlag(options::OPT_fdiagnostics_fixit_info,
Daniel Dunbar49138fc2009-04-19 21:09:34 +00004232 options::OPT_fno_diagnostics_fixit_info))
4233 CmdArgs.push_back("-fno-diagnostics-fixit-info");
Eric Christopher88b7cf02011-08-19 00:30:14 +00004234
Daniel Dunbar9e820ee2009-04-16 06:32:38 +00004235 // Enable -fdiagnostics-show-option by default.
Mike Stump1eb44332009-09-09 15:08:12 +00004236 if (Args.hasFlag(options::OPT_fdiagnostics_show_option,
Daniel Dunbar9e820ee2009-04-16 06:32:38 +00004237 options::OPT_fno_diagnostics_show_option))
4238 CmdArgs.push_back("-fdiagnostics-show-option");
Daniel Dunbar838be482009-11-04 06:24:57 +00004239
Chris Lattner6fbe8392010-05-04 21:55:25 +00004240 if (const Arg *A =
4241 Args.getLastArg(options::OPT_fdiagnostics_show_category_EQ)) {
4242 CmdArgs.push_back("-fdiagnostics-show-category");
Richard Smith1d489cf2012-11-01 04:30:05 +00004243 CmdArgs.push_back(A->getValue());
Chris Lattner6fbe8392010-05-04 21:55:25 +00004244 }
Daniel Dunbarca0e0542010-08-24 16:47:49 +00004245
Douglas Gregorc9471b02011-05-21 17:07:29 +00004246 if (const Arg *A =
4247 Args.getLastArg(options::OPT_fdiagnostics_format_EQ)) {
4248 CmdArgs.push_back("-fdiagnostics-format");
Richard Smith1d489cf2012-11-01 04:30:05 +00004249 CmdArgs.push_back(A->getValue());
Douglas Gregorc9471b02011-05-21 17:07:29 +00004250 }
4251
Chandler Carruthabaca7a2011-03-27 01:50:55 +00004252 if (Arg *A = Args.getLastArg(
4253 options::OPT_fdiagnostics_show_note_include_stack,
4254 options::OPT_fno_diagnostics_show_note_include_stack)) {
4255 if (A->getOption().matches(
4256 options::OPT_fdiagnostics_show_note_include_stack))
4257 CmdArgs.push_back("-fdiagnostics-show-note-include-stack");
4258 else
4259 CmdArgs.push_back("-fno-diagnostics-show-note-include-stack");
4260 }
4261
Daniel Dunbar838be482009-11-04 06:24:57 +00004262 // Color diagnostics are the default, unless the terminal doesn't support
4263 // them.
Nico Weber9753d462013-04-17 21:52:44 +00004264 // Support both clang's -f[no-]color-diagnostics and gcc's
4265 // -f[no-]diagnostics-colors[=never|always|auto].
4266 enum { Colors_On, Colors_Off, Colors_Auto } ShowColors = Colors_Auto;
Stephen Hinesc568f1e2014-07-21 00:47:37 -07004267 for (const auto &Arg : Args) {
4268 const Option &O = Arg->getOption();
Nico Weber9753d462013-04-17 21:52:44 +00004269 if (!O.matches(options::OPT_fcolor_diagnostics) &&
4270 !O.matches(options::OPT_fdiagnostics_color) &&
4271 !O.matches(options::OPT_fno_color_diagnostics) &&
4272 !O.matches(options::OPT_fno_diagnostics_color) &&
4273 !O.matches(options::OPT_fdiagnostics_color_EQ))
4274 continue;
4275
Stephen Hinesc568f1e2014-07-21 00:47:37 -07004276 Arg->claim();
Nico Weber9753d462013-04-17 21:52:44 +00004277 if (O.matches(options::OPT_fcolor_diagnostics) ||
4278 O.matches(options::OPT_fdiagnostics_color)) {
4279 ShowColors = Colors_On;
4280 } else if (O.matches(options::OPT_fno_color_diagnostics) ||
4281 O.matches(options::OPT_fno_diagnostics_color)) {
4282 ShowColors = Colors_Off;
4283 } else {
4284 assert(O.matches(options::OPT_fdiagnostics_color_EQ));
Stephen Hinesc568f1e2014-07-21 00:47:37 -07004285 StringRef value(Arg->getValue());
Nico Weber9753d462013-04-17 21:52:44 +00004286 if (value == "always")
4287 ShowColors = Colors_On;
4288 else if (value == "never")
4289 ShowColors = Colors_Off;
4290 else if (value == "auto")
4291 ShowColors = Colors_Auto;
4292 else
4293 getToolChain().getDriver().Diag(diag::err_drv_clang_unsupported)
4294 << ("-fdiagnostics-color=" + value).str();
4295 }
4296 }
4297 if (ShowColors == Colors_On ||
4298 (ShowColors == Colors_Auto && llvm::sys::Process::StandardErrHasColors()))
Daniel Dunbar838be482009-11-04 06:24:57 +00004299 CmdArgs.push_back("-fcolor-diagnostics");
4300
Nico Rieck2956ef42013-09-11 00:38:02 +00004301 if (Args.hasArg(options::OPT_fansi_escape_codes))
4302 CmdArgs.push_back("-fansi-escape-codes");
4303
Daniel Dunbar75eb1d62009-06-08 21:13:54 +00004304 if (!Args.hasFlag(options::OPT_fshow_source_location,
4305 options::OPT_fno_show_source_location))
4306 CmdArgs.push_back("-fno-show-source-location");
Daniel Dunbar9e820ee2009-04-16 06:32:38 +00004307
Douglas Gregorc9471b02011-05-21 17:07:29 +00004308 if (!Args.hasFlag(options::OPT_fshow_column,
4309 options::OPT_fno_show_column,
4310 true))
4311 CmdArgs.push_back("-fno-show-column");
4312
Douglas Gregora0068fc2010-07-09 17:35:33 +00004313 if (!Args.hasFlag(options::OPT_fspell_checking,
4314 options::OPT_fno_spell_checking))
4315 CmdArgs.push_back("-fno-spell-checking");
Daniel Dunbarca0e0542010-08-24 16:47:49 +00004316
Daniel Dunbar25b26eb2010-10-18 22:49:46 +00004317
Chad Rosier15490fd2012-12-05 21:08:21 +00004318 // -fno-asm-blocks is default.
4319 if (Args.hasFlag(options::OPT_fasm_blocks, options::OPT_fno_asm_blocks,
4320 false))
4321 CmdArgs.push_back("-fasm-blocks");
Daniel Dunbar25b26eb2010-10-18 22:49:46 +00004322
Arnold Schwaighofer99662a12013-08-13 15:46:23 +00004323 // Enable vectorization per default according to the optimization level
4324 // selected. For optimization levels that want vectorization we use the alias
4325 // option to simplify the hasFlag logic.
Stephen Hines6bcf27b2014-05-29 04:14:42 -07004326 bool EnableVec = shouldEnableVectorizerAtOLevel(Args, false);
Arnold Schwaighofer99662a12013-08-13 15:46:23 +00004327 OptSpecifier VectorizeAliasOption = EnableVec ? options::OPT_O_Group :
Chad Rosier31422792013-04-24 18:29:59 +00004328 options::OPT_fvectorize;
Chad Rosier31422792013-04-24 18:29:59 +00004329 if (Args.hasFlag(options::OPT_fvectorize, VectorizeAliasOption,
Hal Finkelcf5691e2013-08-28 05:21:45 +00004330 options::OPT_fno_vectorize, EnableVec))
Chad Rosierc04d0932012-12-11 17:12:28 +00004331 CmdArgs.push_back("-vectorize-loops");
Chad Rosierc04d0932012-12-11 17:12:28 +00004332
Stephen Hines6bcf27b2014-05-29 04:14:42 -07004333 // -fslp-vectorize is enabled based on the optimization level selected.
4334 bool EnableSLPVec = shouldEnableVectorizerAtOLevel(Args, true);
4335 OptSpecifier SLPVectAliasOption = EnableSLPVec ? options::OPT_O_Group :
4336 options::OPT_fslp_vectorize;
4337 if (Args.hasFlag(options::OPT_fslp_vectorize, SLPVectAliasOption,
4338 options::OPT_fno_slp_vectorize, EnableSLPVec))
Nadav Rotem50ea9632013-04-15 04:57:18 +00004339 CmdArgs.push_back("-vectorize-slp");
Hal Finkel443c9992012-12-11 19:59:32 +00004340
Nadav Rotem3c6a9b02013-04-15 05:38:41 +00004341 // -fno-slp-vectorize-aggressive is default.
4342 if (Args.hasFlag(options::OPT_fslp_vectorize_aggressive,
Nick Lewyckyfdf137b2013-06-25 01:49:44 +00004343 options::OPT_fno_slp_vectorize_aggressive, false))
Nadav Rotem3c6a9b02013-04-15 05:38:41 +00004344 CmdArgs.push_back("-vectorize-slp-aggressive");
Nadav Rotem3c6a9b02013-04-15 05:38:41 +00004345
Jeffrey Yasskin5edbdcc2010-06-11 05:57:47 +00004346 if (Arg *A = Args.getLastArg(options::OPT_fshow_overloads_EQ))
4347 A->render(Args, CmdArgs);
4348
Daniel Dunbar7695fba2009-04-19 21:20:32 +00004349 // -fdollars-in-identifiers default varies depending on platform and
4350 // language; only pass if specified.
Mike Stump1eb44332009-09-09 15:08:12 +00004351 if (Arg *A = Args.getLastArg(options::OPT_fdollars_in_identifiers,
Daniel Dunbar7695fba2009-04-19 21:20:32 +00004352 options::OPT_fno_dollars_in_identifiers)) {
4353 if (A->getOption().matches(options::OPT_fdollars_in_identifiers))
Daniel Dunbar8663b182009-12-16 20:10:18 +00004354 CmdArgs.push_back("-fdollars-in-identifiers");
Daniel Dunbar7695fba2009-04-19 21:20:32 +00004355 else
Daniel Dunbar8663b182009-12-16 20:10:18 +00004356 CmdArgs.push_back("-fno-dollars-in-identifiers");
Daniel Dunbar7695fba2009-04-19 21:20:32 +00004357 }
4358
Daniel Dunbare027a4b2009-05-22 19:02:20 +00004359 // -funit-at-a-time is default, and we don't support -fno-unit-at-a-time for
4360 // practical purposes.
Mike Stump1eb44332009-09-09 15:08:12 +00004361 if (Arg *A = Args.getLastArg(options::OPT_funit_at_a_time,
Daniel Dunbare027a4b2009-05-22 19:02:20 +00004362 options::OPT_fno_unit_at_a_time)) {
4363 if (A->getOption().matches(options::OPT_fno_unit_at_a_time))
Chris Lattner5f9e2722011-07-23 10:55:15 +00004364 D.Diag(diag::warn_drv_clang_unsupported) << A->getAsString(Args);
Daniel Dunbare027a4b2009-05-22 19:02:20 +00004365 }
Eli Friedmanceb5c5b2009-07-14 21:58:17 +00004366
Eli Friedman19bda3a2011-11-02 01:53:16 +00004367 if (Args.hasFlag(options::OPT_fapple_pragma_pack,
4368 options::OPT_fno_apple_pragma_pack, false))
4369 CmdArgs.push_back("-fapple-pragma-pack");
4370
Eli Benderskyf3ecf892013-07-24 18:20:14 +00004371 // le32-specific flags:
4372 // -fno-math-builtin: clang should not convert math builtins to intrinsics
4373 // by default.
4374 if (getToolChain().getArch() == llvm::Triple::le32) {
4375 CmdArgs.push_back("-fno-math-builtin");
4376 }
4377
Daniel Dunbar2ba91572009-09-10 03:37:02 +00004378 // Default to -fno-builtin-str{cat,cpy} on Darwin for ARM.
Daniel Dunbarf84a4a42009-09-10 04:57:27 +00004379 //
Daniel Dunbar8ff5b282009-12-11 23:00:49 +00004380 // FIXME: This is disabled until clang -cc1 supports -fno-builtin-foo. PR4941.
Daniel Dunbarf84a4a42009-09-10 04:57:27 +00004381#if 0
Bob Wilson905c45f2011-10-14 05:03:44 +00004382 if (getToolChain().getTriple().isOSDarwin() &&
Eli Bendersky8f4269a2013-07-24 22:20:49 +00004383 (getToolChain().getArch() == llvm::Triple::arm ||
4384 getToolChain().getArch() == llvm::Triple::thumb)) {
Daniel Dunbar2ba91572009-09-10 03:37:02 +00004385 if (!Args.hasArg(options::OPT_fbuiltin_strcat))
4386 CmdArgs.push_back("-fno-builtin-strcat");
4387 if (!Args.hasArg(options::OPT_fbuiltin_strcpy))
4388 CmdArgs.push_back("-fno-builtin-strcpy");
4389 }
Daniel Dunbarf84a4a42009-09-10 04:57:27 +00004390#endif
Daniel Dunbar2ba91572009-09-10 03:37:02 +00004391
Stephen Hinesc568f1e2014-07-21 00:47:37 -07004392 // Enable rewrite includes if the user's asked for it or if we're generating
4393 // diagnostics.
4394 // TODO: Once -module-dependency-dir works with -frewrite-includes it'd be
4395 // nice to enable this when doing a crashdump for modules as well.
4396 if (Args.hasFlag(options::OPT_frewrite_includes,
4397 options::OPT_fno_rewrite_includes, false) ||
4398 (C.isForDiagnostics() && !HaveModules))
4399 CmdArgs.push_back("-frewrite-includes");
4400
Daniel Dunbard98750f2011-03-18 21:23:40 +00004401 // Only allow -traditional or -traditional-cpp outside in preprocessing modes.
Mike Stump1eb44332009-09-09 15:08:12 +00004402 if (Arg *A = Args.getLastArg(options::OPT_traditional,
Daniel Dunbard98750f2011-03-18 21:23:40 +00004403 options::OPT_traditional_cpp)) {
4404 if (isa<PreprocessJobAction>(JA))
4405 CmdArgs.push_back("-traditional-cpp");
Eric Christopher88b7cf02011-08-19 00:30:14 +00004406 else
Chris Lattner5f9e2722011-07-23 10:55:15 +00004407 D.Diag(diag::err_drv_clang_unsupported) << A->getAsString(Args);
Daniel Dunbard98750f2011-03-18 21:23:40 +00004408 }
Eli Friedmanceb5c5b2009-07-14 21:58:17 +00004409
Daniel Dunbar1d460332009-03-18 10:01:51 +00004410 Args.AddLastArg(CmdArgs, options::OPT_dM);
Chris Lattnerd82df3a2009-04-12 01:56:53 +00004411 Args.AddLastArg(CmdArgs, options::OPT_dD);
Ted Kremenek36f6e302011-11-11 00:07:43 +00004412
4413 // Handle serialized diagnostics.
4414 if (Arg *A = Args.getLastArg(options::OPT__serialize_diags)) {
4415 CmdArgs.push_back("-serialize-diagnostic-file");
Richard Smith1d489cf2012-11-01 04:30:05 +00004416 CmdArgs.push_back(Args.MakeArgString(A->getValue()));
Ted Kremenek36f6e302011-11-11 00:07:43 +00004417 }
Daniel Dunbar1d460332009-03-18 10:01:51 +00004418
Ted Kremenek127ff2e2012-09-13 06:41:18 +00004419 if (Args.hasArg(options::OPT_fretain_comments_from_system_headers))
4420 CmdArgs.push_back("-fretain-comments-from-system-headers");
4421
Dmitri Gribenko6ebf0912013-02-22 14:21:27 +00004422 // Forward -fcomment-block-commands to -cc1.
4423 Args.AddAllArgs(CmdArgs, options::OPT_fcomment_block_commands);
Dmitri Gribenko6fd7d302013-04-10 15:35:17 +00004424 // Forward -fparse-all-comments to -cc1.
4425 Args.AddAllArgs(CmdArgs, options::OPT_fparse_all_comments);
Dmitri Gribenko6ebf0912013-02-22 14:21:27 +00004426
Daniel Dunbar3f87fb02010-04-15 06:09:03 +00004427 // Forward -Xclang arguments to -cc1, and -mllvm arguments to the LLVM option
4428 // parser.
Daniel Dunbar1d460332009-03-18 10:01:51 +00004429 Args.AddAllArgValues(CmdArgs, options::OPT_Xclang);
Daniel Dunbar3f87fb02010-04-15 06:09:03 +00004430 for (arg_iterator it = Args.filtered_begin(options::OPT_mllvm),
4431 ie = Args.filtered_end(); it != ie; ++it) {
Daniel Dunbar7e4953e2010-06-11 22:00:13 +00004432 (*it)->claim();
Daniel Dunbarfb36d212010-04-17 06:10:00 +00004433
Daniel Dunbar3f87fb02010-04-15 06:09:03 +00004434 // We translate this by hand to the -cc1 argument, since nightly test uses
4435 // it and developers have been trained to spell it with -mllvm.
Richard Smith1d489cf2012-11-01 04:30:05 +00004436 if (StringRef((*it)->getValue(0)) == "-disable-llvm-optzns")
Daniel Dunbar3f87fb02010-04-15 06:09:03 +00004437 CmdArgs.push_back("-disable-llvm-optzns");
4438 else
Daniel Dunbar7e4953e2010-06-11 22:00:13 +00004439 (*it)->render(Args, CmdArgs);
Daniel Dunbar3f87fb02010-04-15 06:09:03 +00004440 }
Daniel Dunbar1d460332009-03-18 10:01:51 +00004441
Daniel Dunbarcd8e4c42009-03-30 06:36:42 +00004442 if (Output.getType() == types::TY_Dependencies) {
4443 // Handled with other dependency code.
Daniel Dunbar115a7922009-03-19 07:29:38 +00004444 } else if (Output.isFilename()) {
Daniel Dunbarb488c1d2009-03-18 08:07:30 +00004445 CmdArgs.push_back("-o");
Daniel Dunbar115a7922009-03-19 07:29:38 +00004446 CmdArgs.push_back(Output.getFilename());
4447 } else {
4448 assert(Output.isNothing() && "Invalid output.");
Daniel Dunbarb488c1d2009-03-18 08:07:30 +00004449 }
4450
Stephen Hinesc568f1e2014-07-21 00:47:37 -07004451 for (const auto &II : Inputs) {
Stephen Hines651f13c2014-04-23 16:59:28 -07004452 addDashXForInput(Args, II, CmdArgs);
4453
Daniel Dunbar7c1e4652010-08-02 02:38:21 +00004454 if (II.isFilename())
Daniel Dunbar115a7922009-03-19 07:29:38 +00004455 CmdArgs.push_back(II.getFilename());
Daniel Dunbar1d460332009-03-18 10:01:51 +00004456 else
Daniel Dunbar115a7922009-03-19 07:29:38 +00004457 II.getInputArg().renderAsInput(Args, CmdArgs);
Daniel Dunbar1d460332009-03-18 10:01:51 +00004458 }
Daniel Dunbarc21c4852009-04-08 23:54:23 +00004459
Chris Lattnere6113de2009-11-03 19:50:27 +00004460 Args.AddAllArgs(CmdArgs, options::OPT_undef);
4461
Daniel Dunbara001c1c2010-07-18 21:16:15 +00004462 const char *Exec = getToolChain().getDriver().getClangProgramPath();
Daniel Dunbarf2d8b9f2009-12-18 02:43:17 +00004463
4464 // Optionally embed the -cc1 level arguments into the debug info, for build
4465 // analysis.
4466 if (getToolChain().UseDwarfDebugFlags()) {
Daniel Dunbar6e900472010-06-04 18:47:06 +00004467 ArgStringList OriginalArgs;
Stephen Hinesc568f1e2014-07-21 00:47:37 -07004468 for (const auto &Arg : Args)
4469 Arg->render(Args, OriginalArgs);
Daniel Dunbarca0e0542010-08-24 16:47:49 +00004470
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00004471 SmallString<256> Flags;
Daniel Dunbarf2d8b9f2009-12-18 02:43:17 +00004472 Flags += Exec;
Daniel Dunbar6e900472010-06-04 18:47:06 +00004473 for (unsigned i = 0, e = OriginalArgs.size(); i != e; ++i) {
Stephen Hines176edba2014-12-01 14:53:08 -08004474 SmallString<128> EscapedArg;
4475 EscapeSpacesAndBackslashes(OriginalArgs[i], EscapedArg);
Daniel Dunbarf2d8b9f2009-12-18 02:43:17 +00004476 Flags += " ";
Stephen Hines176edba2014-12-01 14:53:08 -08004477 Flags += EscapedArg;
Daniel Dunbarf2d8b9f2009-12-18 02:43:17 +00004478 }
4479 CmdArgs.push_back("-dwarf-debug-flags");
4480 CmdArgs.push_back(Args.MakeArgString(Flags.str()));
4481 }
4482
Eric Christopher80190392013-02-22 20:12:52 +00004483 // Add the split debug info name to the command lines here so we
4484 // can propagate it to the backend.
4485 bool SplitDwarf = Args.hasArg(options::OPT_gsplit_dwarf) &&
Cameron Esfahani57b1da12013-09-14 01:09:11 +00004486 getToolChain().getTriple().isOSLinux() &&
Eric Christopherff971d72013-02-22 23:50:16 +00004487 (isa<AssembleJobAction>(JA) || isa<CompileJobAction>(JA));
Eric Christopher80190392013-02-22 20:12:52 +00004488 const char *SplitDwarfOut;
4489 if (SplitDwarf) {
4490 CmdArgs.push_back("-split-dwarf-file");
4491 SplitDwarfOut = SplitDebugName(Args, Inputs);
4492 CmdArgs.push_back(SplitDwarfOut);
4493 }
4494
4495 // Finally add the compile command to the compilation.
Stephen Hines651f13c2014-04-23 16:59:28 -07004496 if (Args.hasArg(options::OPT__SLASH_fallback) &&
Stephen Hines6bcf27b2014-05-29 04:14:42 -07004497 Output.getType() == types::TY_Object &&
4498 (InputType == types::TY_C || InputType == types::TY_CXX)) {
Stephen Hines176edba2014-12-01 14:53:08 -08004499 auto CLCommand =
4500 getCLFallback()->GetCommand(C, JA, Output, Inputs, Args, LinkingOutput);
4501 C.addCommand(llvm::make_unique<FallbackCommand>(JA, *this, Exec, CmdArgs,
4502 std::move(CLCommand)));
Hans Wennborgc8ba0a02013-09-19 20:32:16 +00004503 } else {
Stephen Hines176edba2014-12-01 14:53:08 -08004504 C.addCommand(llvm::make_unique<Command>(JA, *this, Exec, CmdArgs));
Hans Wennborgc8ba0a02013-09-19 20:32:16 +00004505 }
4506
Daniel Dunbara880db02009-03-23 19:03:36 +00004507
Eric Christopherff971d72013-02-22 23:50:16 +00004508 // Handle the debug info splitting at object creation time if we're
4509 // creating an object.
Eric Christopher59320e72013-02-21 22:35:01 +00004510 // TODO: Currently only works on linux with newer objcopy.
Eric Christopherff971d72013-02-22 23:50:16 +00004511 if (SplitDwarf && !isa<CompileJobAction>(JA))
Eric Christopher80190392013-02-22 20:12:52 +00004512 SplitDebugInfo(getToolChain(), C, *this, JA, Args, Output, SplitDwarfOut);
Eric Christopher59320e72013-02-21 22:35:01 +00004513
Roman Divackybe4c8702011-02-10 16:52:03 +00004514 if (Arg *A = Args.getLastArg(options::OPT_pg))
4515 if (Args.hasArg(options::OPT_fomit_frame_pointer))
Chris Lattner5f9e2722011-07-23 10:55:15 +00004516 D.Diag(diag::err_drv_argument_not_allowed_with)
Roman Divackybe4c8702011-02-10 16:52:03 +00004517 << "-fomit-frame-pointer" << A->getAsString(Args);
Michael J. Spencer20249a12010-10-21 03:16:25 +00004518
Daniel Dunbar68fb4692009-04-03 20:51:31 +00004519 // Claim some arguments which clang supports automatically.
4520
Daniel Dunbarf4046862010-04-15 06:18:42 +00004521 // -fpch-preprocess is used with gcc to add a special marker in the output to
4522 // include the PCH file. Clang's PTH solution is completely transparent, so we
4523 // do not need to deal with it at all.
Daniel Dunbar68fb4692009-04-03 20:51:31 +00004524 Args.ClaimAllArgs(options::OPT_fpch_preprocess);
Daniel Dunbarc21c4852009-04-08 23:54:23 +00004525
Daniel Dunbara880db02009-03-23 19:03:36 +00004526 // Claim some arguments which clang doesn't support, but we don't
4527 // care to warn the user about.
Daniel Dunbarcdd96862009-11-25 11:53:23 +00004528 Args.ClaimAllArgs(options::OPT_clang_ignored_f_Group);
4529 Args.ClaimAllArgs(options::OPT_clang_ignored_m_Group);
Rafael Espindola035ff0c2011-02-28 23:29:45 +00004530
Rafael Espindola6155fbe2013-09-04 19:37:35 +00004531 // Disable warnings for clang -E -emit-llvm foo.c
Rafael Espindola9c094fb2011-03-01 05:25:27 +00004532 Args.ClaimAllArgs(options::OPT_emit_llvm);
Daniel Dunbar47ac7d22009-03-18 06:00:36 +00004533}
4534
John McCall260611a2012-06-20 06:18:46 +00004535/// Add options related to the Objective-C runtime/ABI.
4536///
4537/// Returns true if the runtime is non-fragile.
4538ObjCRuntime Clang::AddObjCRuntimeArgs(const ArgList &args,
4539 ArgStringList &cmdArgs,
4540 RewriteKind rewriteKind) const {
4541 // Look for the controlling runtime option.
4542 Arg *runtimeArg = args.getLastArg(options::OPT_fnext_runtime,
4543 options::OPT_fgnu_runtime,
4544 options::OPT_fobjc_runtime_EQ);
4545
4546 // Just forward -fobjc-runtime= to the frontend. This supercedes
4547 // options about fragility.
4548 if (runtimeArg &&
4549 runtimeArg->getOption().matches(options::OPT_fobjc_runtime_EQ)) {
4550 ObjCRuntime runtime;
Richard Smith1d489cf2012-11-01 04:30:05 +00004551 StringRef value = runtimeArg->getValue();
John McCall260611a2012-06-20 06:18:46 +00004552 if (runtime.tryParse(value)) {
4553 getToolChain().getDriver().Diag(diag::err_drv_unknown_objc_runtime)
4554 << value;
4555 }
4556
4557 runtimeArg->render(args, cmdArgs);
4558 return runtime;
4559 }
4560
4561 // Otherwise, we'll need the ABI "version". Version numbers are
4562 // slightly confusing for historical reasons:
4563 // 1 - Traditional "fragile" ABI
4564 // 2 - Non-fragile ABI, version 1
4565 // 3 - Non-fragile ABI, version 2
4566 unsigned objcABIVersion = 1;
4567 // If -fobjc-abi-version= is present, use that to set the version.
4568 if (Arg *abiArg = args.getLastArg(options::OPT_fobjc_abi_version_EQ)) {
Richard Smith1d489cf2012-11-01 04:30:05 +00004569 StringRef value = abiArg->getValue();
John McCall260611a2012-06-20 06:18:46 +00004570 if (value == "1")
4571 objcABIVersion = 1;
4572 else if (value == "2")
4573 objcABIVersion = 2;
4574 else if (value == "3")
4575 objcABIVersion = 3;
4576 else
4577 getToolChain().getDriver().Diag(diag::err_drv_clang_unsupported)
4578 << value;
4579 } else {
4580 // Otherwise, determine if we are using the non-fragile ABI.
4581 bool nonFragileABIIsDefault =
4582 (rewriteKind == RK_NonFragile ||
4583 (rewriteKind == RK_None &&
4584 getToolChain().IsObjCNonFragileABIDefault()));
4585 if (args.hasFlag(options::OPT_fobjc_nonfragile_abi,
4586 options::OPT_fno_objc_nonfragile_abi,
4587 nonFragileABIIsDefault)) {
4588 // Determine the non-fragile ABI version to use.
4589#ifdef DISABLE_DEFAULT_NONFRAGILEABI_TWO
4590 unsigned nonFragileABIVersion = 1;
4591#else
4592 unsigned nonFragileABIVersion = 2;
4593#endif
4594
4595 if (Arg *abiArg = args.getLastArg(
4596 options::OPT_fobjc_nonfragile_abi_version_EQ)) {
Richard Smith1d489cf2012-11-01 04:30:05 +00004597 StringRef value = abiArg->getValue();
John McCall260611a2012-06-20 06:18:46 +00004598 if (value == "1")
4599 nonFragileABIVersion = 1;
4600 else if (value == "2")
4601 nonFragileABIVersion = 2;
4602 else
4603 getToolChain().getDriver().Diag(diag::err_drv_clang_unsupported)
4604 << value;
4605 }
4606
4607 objcABIVersion = 1 + nonFragileABIVersion;
4608 } else {
4609 objcABIVersion = 1;
4610 }
4611 }
4612
4613 // We don't actually care about the ABI version other than whether
4614 // it's non-fragile.
4615 bool isNonFragile = objcABIVersion != 1;
4616
4617 // If we have no runtime argument, ask the toolchain for its default runtime.
4618 // However, the rewriter only really supports the Mac runtime, so assume that.
4619 ObjCRuntime runtime;
4620 if (!runtimeArg) {
4621 switch (rewriteKind) {
4622 case RK_None:
4623 runtime = getToolChain().getDefaultObjCRuntime(isNonFragile);
4624 break;
4625 case RK_Fragile:
4626 runtime = ObjCRuntime(ObjCRuntime::FragileMacOSX, VersionTuple());
4627 break;
4628 case RK_NonFragile:
4629 runtime = ObjCRuntime(ObjCRuntime::MacOSX, VersionTuple());
4630 break;
4631 }
4632
4633 // -fnext-runtime
4634 } else if (runtimeArg->getOption().matches(options::OPT_fnext_runtime)) {
4635 // On Darwin, make this use the default behavior for the toolchain.
4636 if (getToolChain().getTriple().isOSDarwin()) {
4637 runtime = getToolChain().getDefaultObjCRuntime(isNonFragile);
4638
4639 // Otherwise, build for a generic macosx port.
4640 } else {
4641 runtime = ObjCRuntime(ObjCRuntime::MacOSX, VersionTuple());
4642 }
4643
4644 // -fgnu-runtime
4645 } else {
4646 assert(runtimeArg->getOption().matches(options::OPT_fgnu_runtime));
David Chisnalla422cd02012-07-04 10:37:03 +00004647 // Legacy behaviour is to target the gnustep runtime if we are i
4648 // non-fragile mode or the GCC runtime in fragile mode.
4649 if (isNonFragile)
David Chisnall891dac72012-10-16 15:11:55 +00004650 runtime = ObjCRuntime(ObjCRuntime::GNUstep, VersionTuple(1,6));
David Chisnalla422cd02012-07-04 10:37:03 +00004651 else
4652 runtime = ObjCRuntime(ObjCRuntime::GCC, VersionTuple());
John McCall260611a2012-06-20 06:18:46 +00004653 }
4654
4655 cmdArgs.push_back(args.MakeArgString(
4656 "-fobjc-runtime=" + runtime.getAsString()));
4657 return runtime;
4658}
4659
Stephen Hinesc568f1e2014-07-21 00:47:37 -07004660static bool maybeConsumeDash(const std::string &EH, size_t &I) {
4661 bool HaveDash = (I + 1 < EH.size() && EH[I + 1] == '-');
4662 I += HaveDash;
4663 return !HaveDash;
4664}
4665
4666struct EHFlags {
4667 EHFlags() : Synch(false), Asynch(false), NoExceptC(false) {}
4668 bool Synch;
4669 bool Asynch;
4670 bool NoExceptC;
4671};
4672
4673/// /EH controls whether to run destructor cleanups when exceptions are
4674/// thrown. There are three modifiers:
4675/// - s: Cleanup after "synchronous" exceptions, aka C++ exceptions.
4676/// - a: Cleanup after "asynchronous" exceptions, aka structured exceptions.
4677/// The 'a' modifier is unimplemented and fundamentally hard in LLVM IR.
4678/// - c: Assume that extern "C" functions are implicitly noexcept. This
4679/// modifier is an optimization, so we ignore it for now.
4680/// The default is /EHs-c-, meaning cleanups are disabled.
4681static EHFlags parseClangCLEHFlags(const Driver &D, const ArgList &Args) {
4682 EHFlags EH;
4683 std::vector<std::string> EHArgs = Args.getAllArgValues(options::OPT__SLASH_EH);
4684 for (auto EHVal : EHArgs) {
4685 for (size_t I = 0, E = EHVal.size(); I != E; ++I) {
4686 switch (EHVal[I]) {
4687 case 'a': EH.Asynch = maybeConsumeDash(EHVal, I); continue;
4688 case 'c': EH.NoExceptC = maybeConsumeDash(EHVal, I); continue;
4689 case 's': EH.Synch = maybeConsumeDash(EHVal, I); continue;
4690 default: break;
4691 }
4692 D.Diag(clang::diag::err_drv_invalid_value) << "/EH" << EHVal;
4693 break;
4694 }
4695 }
4696 return EH;
4697}
4698
Hans Wennborgb3574792013-08-08 00:17:41 +00004699void Clang::AddClangCLArgs(const ArgList &Args, ArgStringList &CmdArgs) const {
4700 unsigned RTOptionID = options::OPT__SLASH_MT;
4701
Hans Wennborg6d0a8d52013-09-10 20:18:04 +00004702 if (Args.hasArg(options::OPT__SLASH_LDd))
4703 // The /LDd option implies /MTd. The dependent lib part can be overridden,
4704 // but defining _DEBUG is sticky.
4705 RTOptionID = options::OPT__SLASH_MTd;
4706
Hans Wennborg76da1782013-09-18 22:26:39 +00004707 if (Arg *A = Args.getLastArg(options::OPT__SLASH_M_Group))
Hans Wennborgb3574792013-08-08 00:17:41 +00004708 RTOptionID = A->getOption().getID();
Hans Wennborg42ade492013-09-11 16:38:41 +00004709
Hans Wennborgb3574792013-08-08 00:17:41 +00004710 switch(RTOptionID) {
4711 case options::OPT__SLASH_MD:
Hans Wennborg6d0a8d52013-09-10 20:18:04 +00004712 if (Args.hasArg(options::OPT__SLASH_LDd))
4713 CmdArgs.push_back("-D_DEBUG");
Hans Wennborgb3574792013-08-08 00:17:41 +00004714 CmdArgs.push_back("-D_MT");
4715 CmdArgs.push_back("-D_DLL");
4716 CmdArgs.push_back("--dependent-lib=msvcrt");
4717 break;
4718 case options::OPT__SLASH_MDd:
4719 CmdArgs.push_back("-D_DEBUG");
4720 CmdArgs.push_back("-D_MT");
4721 CmdArgs.push_back("-D_DLL");
4722 CmdArgs.push_back("--dependent-lib=msvcrtd");
4723 break;
4724 case options::OPT__SLASH_MT:
Hans Wennborg6d0a8d52013-09-10 20:18:04 +00004725 if (Args.hasArg(options::OPT__SLASH_LDd))
4726 CmdArgs.push_back("-D_DEBUG");
Hans Wennborgb3574792013-08-08 00:17:41 +00004727 CmdArgs.push_back("-D_MT");
4728 CmdArgs.push_back("--dependent-lib=libcmt");
4729 break;
4730 case options::OPT__SLASH_MTd:
4731 CmdArgs.push_back("-D_DEBUG");
4732 CmdArgs.push_back("-D_MT");
4733 CmdArgs.push_back("--dependent-lib=libcmtd");
4734 break;
4735 default:
4736 llvm_unreachable("Unexpected option ID.");
4737 }
4738
Reid Klecknera32c5232013-08-08 19:33:10 +00004739 // This provides POSIX compatibility (maps 'open' to '_open'), which most
4740 // users want. The /Za flag to cl.exe turns this off, but it's not
4741 // implemented in clang.
4742 CmdArgs.push_back("--dependent-lib=oldnames");
Hans Wennborgf0f98912013-08-08 19:54:30 +00004743
Stephen Hines176edba2014-12-01 14:53:08 -08004744 // Both /showIncludes and /E (and /EP) write to stdout. Allowing both
4745 // would produce interleaved output, so ignore /showIncludes in such cases.
4746 if (!Args.hasArg(options::OPT_E) && !Args.hasArg(options::OPT__SLASH_EP))
4747 if (Arg *A = Args.getLastArg(options::OPT_show_includes))
4748 A->render(Args, CmdArgs);
Hans Wennborgb6475522013-09-10 01:07:07 +00004749
Stephen Hinesc568f1e2014-07-21 00:47:37 -07004750 // This controls whether or not we emit RTTI data for polymorphic types.
4751 if (Args.hasFlag(options::OPT__SLASH_GR_, options::OPT__SLASH_GR,
4752 /*default=*/false))
4753 CmdArgs.push_back("-fno-rtti-data");
Stephen Hines651f13c2014-04-23 16:59:28 -07004754
Stephen Hines651f13c2014-04-23 16:59:28 -07004755 const Driver &D = getToolChain().getDriver();
Stephen Hinesc568f1e2014-07-21 00:47:37 -07004756 EHFlags EH = parseClangCLEHFlags(D, Args);
4757 // FIXME: Do something with NoExceptC.
4758 if (EH.Synch || EH.Asynch) {
4759 CmdArgs.push_back("-fexceptions");
4760 CmdArgs.push_back("-fcxx-exceptions");
4761 }
4762
4763 // /EP should expand to -E -P.
4764 if (Args.hasArg(options::OPT__SLASH_EP)) {
4765 CmdArgs.push_back("-E");
4766 CmdArgs.push_back("-P");
4767 }
4768
Stephen Hines651f13c2014-04-23 16:59:28 -07004769 Arg *MostGeneralArg = Args.getLastArg(options::OPT__SLASH_vmg);
4770 Arg *BestCaseArg = Args.getLastArg(options::OPT__SLASH_vmb);
4771 if (MostGeneralArg && BestCaseArg)
4772 D.Diag(clang::diag::err_drv_argument_not_allowed_with)
4773 << MostGeneralArg->getAsString(Args) << BestCaseArg->getAsString(Args);
4774
4775 if (MostGeneralArg) {
4776 Arg *SingleArg = Args.getLastArg(options::OPT__SLASH_vms);
4777 Arg *MultipleArg = Args.getLastArg(options::OPT__SLASH_vmm);
4778 Arg *VirtualArg = Args.getLastArg(options::OPT__SLASH_vmv);
4779
4780 Arg *FirstConflict = SingleArg ? SingleArg : MultipleArg;
4781 Arg *SecondConflict = VirtualArg ? VirtualArg : MultipleArg;
4782 if (FirstConflict && SecondConflict && FirstConflict != SecondConflict)
4783 D.Diag(clang::diag::err_drv_argument_not_allowed_with)
4784 << FirstConflict->getAsString(Args)
4785 << SecondConflict->getAsString(Args);
4786
4787 if (SingleArg)
4788 CmdArgs.push_back("-fms-memptr-rep=single");
4789 else if (MultipleArg)
4790 CmdArgs.push_back("-fms-memptr-rep=multiple");
4791 else
4792 CmdArgs.push_back("-fms-memptr-rep=virtual");
4793 }
4794
4795 if (Arg *A = Args.getLastArg(options::OPT_vtordisp_mode_EQ))
4796 A->render(Args, CmdArgs);
4797
Hans Wennborgb6475522013-09-10 01:07:07 +00004798 if (!Args.hasArg(options::OPT_fdiagnostics_format_EQ)) {
4799 CmdArgs.push_back("-fdiagnostics-format");
Hans Wennborg89e32742013-09-24 00:08:55 +00004800 if (Args.hasArg(options::OPT__SLASH_fallback))
4801 CmdArgs.push_back("msvc-fallback");
4802 else
4803 CmdArgs.push_back("msvc");
Hans Wennborgb6475522013-09-10 01:07:07 +00004804 }
Hans Wennborgb3574792013-08-08 00:17:41 +00004805}
4806
Stephen Hinesc568f1e2014-07-21 00:47:37 -07004807visualstudio::Compile *Clang::getCLFallback() const {
4808 if (!CLFallback)
4809 CLFallback.reset(new visualstudio::Compile(getToolChain()));
4810 return CLFallback.get();
4811}
4812
Daniel Dunbar20a9aa52010-05-20 21:30:13 +00004813void ClangAs::ConstructJob(Compilation &C, const JobAction &JA,
Daniel Dunbar20a9aa52010-05-20 21:30:13 +00004814 const InputInfo &Output,
4815 const InputInfoList &Inputs,
4816 const ArgList &Args,
4817 const char *LinkingOutput) const {
Daniel Dunbar20a9aa52010-05-20 21:30:13 +00004818 ArgStringList CmdArgs;
4819
4820 assert(Inputs.size() == 1 && "Unexpected number of inputs.");
4821 const InputInfo &Input = Inputs[0];
4822
Rafael Espindoladbe80d92010-11-17 22:13:25 +00004823 // Don't warn about "clang -w -c foo.s"
4824 Args.ClaimAllArgs(options::OPT_w);
Rafael Espindola9c094fb2011-03-01 05:25:27 +00004825 // and "clang -emit-llvm -c foo.s"
4826 Args.ClaimAllArgs(options::OPT_emit_llvm);
Rafael Espindoladbe80d92010-11-17 22:13:25 +00004827
Daniel Dunbar20a9aa52010-05-20 21:30:13 +00004828 // Invoke ourselves in -cc1as mode.
4829 //
4830 // FIXME: Implement custom jobs for internal actions.
4831 CmdArgs.push_back("-cc1as");
4832
4833 // Add the "effective" target triple.
4834 CmdArgs.push_back("-triple");
Chad Rosier61ab80a2011-09-20 20:44:06 +00004835 std::string TripleStr =
4836 getToolChain().ComputeEffectiveClangTriple(Args, Input.getType());
Daniel Dunbar20a9aa52010-05-20 21:30:13 +00004837 CmdArgs.push_back(Args.MakeArgString(TripleStr));
4838
4839 // Set the output mode, we currently only expect to be used as a real
4840 // assembler.
4841 CmdArgs.push_back("-filetype");
4842 CmdArgs.push_back("obj");
4843
Eric Christopher27e2b982012-12-18 00:31:10 +00004844 // Set the main file name, so that debug info works even with
4845 // -save-temps or preprocessed assembly.
4846 CmdArgs.push_back("-main-file-name");
4847 CmdArgs.push_back(Clang::getBaseInputName(Args, Inputs));
4848
Rafael Espindolab330e402013-08-20 22:12:08 +00004849 // Add the target cpu
Rafael Espindola146dbbf2013-08-21 16:39:20 +00004850 const llvm::Triple &Triple = getToolChain().getTriple();
4851 std::string CPU = getCPUName(Args, Triple);
Rafael Espindolab330e402013-08-20 22:12:08 +00004852 if (!CPU.empty()) {
4853 CmdArgs.push_back("-target-cpu");
4854 CmdArgs.push_back(Args.MakeArgString(CPU));
4855 }
4856
Rafael Espindola146dbbf2013-08-21 16:39:20 +00004857 // Add the target features
4858 const Driver &D = getToolChain().getDriver();
Stephen Hines651f13c2014-04-23 16:59:28 -07004859 getTargetFeatures(D, Triple, Args, CmdArgs, true);
Jim Grosbachfc308292012-02-10 20:37:10 +00004860
Daniel Dunbar7f6f8c82011-03-17 17:37:29 +00004861 // Ignore explicit -force_cpusubtype_ALL option.
4862 (void) Args.hasArg(options::OPT_force__cpusubtype__ALL);
Daniel Dunbar20a9aa52010-05-20 21:30:13 +00004863
Eric Christopher8f0a4032012-01-10 00:38:01 +00004864 // Determine the original source input.
4865 const Action *SourceAction = &JA;
4866 while (SourceAction->getKind() != Action::InputClass) {
4867 assert(!SourceAction->getInputs().empty() && "unexpected root action!");
4868 SourceAction = SourceAction->getInputs()[0];
4869 }
4870
Chandler Carruthd566df62012-12-17 21:40:04 +00004871 // Forward -g and handle debug info related flags, assuming we are dealing
4872 // with an actual assembly file.
Eric Christopher8f0a4032012-01-10 00:38:01 +00004873 if (SourceAction->getType() == types::TY_Asm ||
4874 SourceAction->getType() == types::TY_PP_Asm) {
4875 Args.ClaimAllArgs(options::OPT_g_Group);
4876 if (Arg *A = Args.getLastArg(options::OPT_g_Group))
4877 if (!A->getOption().matches(options::OPT_g0))
4878 CmdArgs.push_back("-g");
Chandler Carruthd566df62012-12-17 21:40:04 +00004879
Stephen Hines6bcf27b2014-05-29 04:14:42 -07004880 if (Args.hasArg(options::OPT_gdwarf_2))
4881 CmdArgs.push_back("-gdwarf-2");
4882 if (Args.hasArg(options::OPT_gdwarf_3))
4883 CmdArgs.push_back("-gdwarf-3");
4884 if (Args.hasArg(options::OPT_gdwarf_4))
4885 CmdArgs.push_back("-gdwarf-4");
4886
Chandler Carruthd566df62012-12-17 21:40:04 +00004887 // Add the -fdebug-compilation-dir flag if needed.
4888 addDebugCompDirArg(Args, CmdArgs);
Kevin Enderby02341792013-01-17 21:38:06 +00004889
4890 // Set the AT_producer to the clang version when using the integrated
4891 // assembler on assembly source files.
4892 CmdArgs.push_back("-dwarf-debug-producer");
4893 CmdArgs.push_back(Args.MakeArgString(getClangFullVersion()));
Eric Christopher8f0a4032012-01-10 00:38:01 +00004894 }
Kevin Enderby567003e2011-12-22 19:31:58 +00004895
4896 // Optionally embed the -cc1as level arguments into the debug info, for build
4897 // analysis.
4898 if (getToolChain().UseDwarfDebugFlags()) {
4899 ArgStringList OriginalArgs;
Stephen Hinesc568f1e2014-07-21 00:47:37 -07004900 for (const auto &Arg : Args)
4901 Arg->render(Args, OriginalArgs);
Kevin Enderby567003e2011-12-22 19:31:58 +00004902
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00004903 SmallString<256> Flags;
Kevin Enderby567003e2011-12-22 19:31:58 +00004904 const char *Exec = getToolChain().getDriver().getClangProgramPath();
4905 Flags += Exec;
4906 for (unsigned i = 0, e = OriginalArgs.size(); i != e; ++i) {
Stephen Hines176edba2014-12-01 14:53:08 -08004907 SmallString<128> EscapedArg;
4908 EscapeSpacesAndBackslashes(OriginalArgs[i], EscapedArg);
Kevin Enderby567003e2011-12-22 19:31:58 +00004909 Flags += " ";
Stephen Hines176edba2014-12-01 14:53:08 -08004910 Flags += EscapedArg;
Kevin Enderby567003e2011-12-22 19:31:58 +00004911 }
4912 CmdArgs.push_back("-dwarf-debug-flags");
4913 CmdArgs.push_back(Args.MakeArgString(Flags.str()));
4914 }
Daniel Dunbar20a9aa52010-05-20 21:30:13 +00004915
4916 // FIXME: Add -static support, once we have it.
4917
Stephen Hines651f13c2014-04-23 16:59:28 -07004918 // Consume all the warning flags. Usually this would be handled more
4919 // gracefully by -cc1 (warning about unknown warning flags, etc) but -cc1as
4920 // doesn't handle that so rather than warning about unused flags that are
4921 // actually used, we'll lie by omission instead.
4922 // FIXME: Stop lying and consume only the appropriate driver flags
4923 for (arg_iterator it = Args.filtered_begin(options::OPT_W_Group),
4924 ie = Args.filtered_end();
4925 it != ie; ++it)
4926 (*it)->claim();
4927
David Blaikie73168db2013-07-25 21:19:01 +00004928 CollectArgsForIntegratedAssembler(C, Args, CmdArgs,
4929 getToolChain().getDriver());
4930
Daniel Dunbar3df23252011-04-29 17:53:18 +00004931 Args.AddAllArgs(CmdArgs, options::OPT_mllvm);
Daniel Dunbar20a9aa52010-05-20 21:30:13 +00004932
4933 assert(Output.isFilename() && "Unexpected lipo output.");
4934 CmdArgs.push_back("-o");
4935 CmdArgs.push_back(Output.getFilename());
4936
Daniel Dunbar7c1e4652010-08-02 02:38:21 +00004937 assert(Input.isFilename() && "Invalid input.");
4938 CmdArgs.push_back(Input.getFilename());
Daniel Dunbar20a9aa52010-05-20 21:30:13 +00004939
Daniel Dunbara001c1c2010-07-18 21:16:15 +00004940 const char *Exec = getToolChain().getDriver().getClangProgramPath();
Stephen Hines176edba2014-12-01 14:53:08 -08004941 C.addCommand(llvm::make_unique<Command>(JA, *this, Exec, CmdArgs));
Eric Christopher708d72a2013-04-10 21:30:40 +00004942
4943 // Handle the debug info splitting at object creation time if we're
4944 // creating an object.
4945 // TODO: Currently only works on linux with newer objcopy.
4946 if (Args.hasArg(options::OPT_gsplit_dwarf) &&
Cameron Esfahani57b1da12013-09-14 01:09:11 +00004947 getToolChain().getTriple().isOSLinux())
Eric Christopher708d72a2013-04-10 21:30:40 +00004948 SplitDebugInfo(getToolChain(), C, *this, JA, Args, Output,
4949 SplitDebugName(Args, Inputs));
Daniel Dunbar20a9aa52010-05-20 21:30:13 +00004950}
4951
Stephen Hines176edba2014-12-01 14:53:08 -08004952void GnuTool::anchor() {}
4953
Daniel Dunbarb488c1d2009-03-18 08:07:30 +00004954void gcc::Common::ConstructJob(Compilation &C, const JobAction &JA,
Daniel Dunbarb488c1d2009-03-18 08:07:30 +00004955 const InputInfo &Output,
4956 const InputInfoList &Inputs,
Daniel Dunbar1d460332009-03-18 10:01:51 +00004957 const ArgList &Args,
Daniel Dunbarb488c1d2009-03-18 08:07:30 +00004958 const char *LinkingOutput) const {
Daniel Dunbaree788e72009-12-21 18:54:17 +00004959 const Driver &D = getToolChain().getDriver();
Daniel Dunbarb488c1d2009-03-18 08:07:30 +00004960 ArgStringList CmdArgs;
Daniel Dunbar47ac7d22009-03-18 06:00:36 +00004961
Stephen Hinesc568f1e2014-07-21 00:47:37 -07004962 for (const auto &A : Args) {
Michael J. Spencer91e06da2012-10-19 22:37:06 +00004963 if (forwardToGCC(A->getOption())) {
Daniel Dunbar2dffe2d2010-08-03 16:14:14 +00004964 // Don't forward any -g arguments to assembly steps.
4965 if (isa<AssembleJobAction>(JA) &&
4966 A->getOption().matches(options::OPT_g_Group))
4967 continue;
4968
NAKAMURA Takumi3c6c8222013-08-19 11:51:51 +00004969 // Don't forward any -W arguments to assembly and link steps.
4970 if ((isa<AssembleJobAction>(JA) || isa<LinkJobAction>(JA)) &&
4971 A->getOption().matches(options::OPT_W_Group))
4972 continue;
4973
Daniel Dunbar75877192009-03-19 07:55:12 +00004974 // It is unfortunate that we have to claim here, as this means
4975 // we will basically never report anything interesting for
Daniel Dunbar6ecc7a92009-05-02 21:41:52 +00004976 // platforms using a generic gcc, even if we are just using gcc
4977 // to get to the assembler.
Daniel Dunbar75877192009-03-19 07:55:12 +00004978 A->claim();
Daniel Dunbar1d460332009-03-18 10:01:51 +00004979 A->render(Args, CmdArgs);
Daniel Dunbar75877192009-03-19 07:55:12 +00004980 }
Daniel Dunbarb488c1d2009-03-18 08:07:30 +00004981 }
Daniel Dunbarc21c4852009-04-08 23:54:23 +00004982
Daniel Dunbar82b51cc2010-01-25 22:35:08 +00004983 RenderExtraToolArgs(JA, CmdArgs);
Daniel Dunbarb488c1d2009-03-18 08:07:30 +00004984
4985 // If using a driver driver, force the arch.
Bob Wilson905c45f2011-10-14 05:03:44 +00004986 if (getToolChain().getTriple().isOSDarwin()) {
Daniel Dunbarb488c1d2009-03-18 08:07:30 +00004987 CmdArgs.push_back("-arch");
Stephen Hines176edba2014-12-01 14:53:08 -08004988 CmdArgs.push_back(
4989 Args.MakeArgString(getToolChain().getDefaultUniversalArchName()));
Daniel Dunbarb488c1d2009-03-18 08:07:30 +00004990 }
4991
Daniel Dunbar6ecc7a92009-05-02 21:41:52 +00004992 // Try to force gcc to match the tool chain we want, if we recognize
4993 // the arch.
Daniel Dunbar7cfe31a2009-05-22 02:21:04 +00004994 //
4995 // FIXME: The triple class should directly provide the information we want
4996 // here.
Stephen Hines176edba2014-12-01 14:53:08 -08004997 llvm::Triple::ArchType Arch = getToolChain().getArch();
Rafael Espindola64f7ad92012-10-07 04:44:33 +00004998 if (Arch == llvm::Triple::x86 || Arch == llvm::Triple::ppc)
Daniel Dunbar6ecc7a92009-05-02 21:41:52 +00004999 CmdArgs.push_back("-m32");
Bill Schmidtea7fb0c2013-07-26 01:36:11 +00005000 else if (Arch == llvm::Triple::x86_64 || Arch == llvm::Triple::ppc64 ||
5001 Arch == llvm::Triple::ppc64le)
Daniel Dunbar6ecc7a92009-05-02 21:41:52 +00005002 CmdArgs.push_back("-m64");
5003
Daniel Dunbar7c1e4652010-08-02 02:38:21 +00005004 if (Output.isFilename()) {
Daniel Dunbarb488c1d2009-03-18 08:07:30 +00005005 CmdArgs.push_back("-o");
Daniel Dunbar115a7922009-03-19 07:29:38 +00005006 CmdArgs.push_back(Output.getFilename());
5007 } else {
5008 assert(Output.isNothing() && "Unexpected output");
Daniel Dunbarb488c1d2009-03-18 08:07:30 +00005009 CmdArgs.push_back("-fsyntax-only");
Daniel Dunbar115a7922009-03-19 07:29:38 +00005010 }
Daniel Dunbarb488c1d2009-03-18 08:07:30 +00005011
Tony Linthicum96319392011-12-12 21:14:55 +00005012 Args.AddAllArgValues(CmdArgs, options::OPT_Wa_COMMA,
5013 options::OPT_Xassembler);
Daniel Dunbarb488c1d2009-03-18 08:07:30 +00005014
5015 // Only pass -x if gcc will understand it; otherwise hope gcc
5016 // understands the suffix correctly. The main use case this would go
5017 // wrong in is for linker inputs if they happened to have an odd
5018 // suffix; really the only way to get this to happen is a command
5019 // like '-x foobar a.c' which will treat a.c like a linker input.
5020 //
5021 // FIXME: For the linker case specifically, can we safely convert
5022 // inputs into '-Wl,' options?
Stephen Hinesc568f1e2014-07-21 00:47:37 -07005023 for (const auto &II : Inputs) {
Daniel Dunbar5915fbf2009-09-01 16:57:46 +00005024 // Don't try to pass LLVM or AST inputs to a generic gcc.
Daniel Dunbar6c6424b2010-06-07 23:28:45 +00005025 if (II.getType() == types::TY_LLVM_IR || II.getType() == types::TY_LTO_IR ||
5026 II.getType() == types::TY_LLVM_BC || II.getType() == types::TY_LTO_BC)
Chris Lattner5f9e2722011-07-23 10:55:15 +00005027 D.Diag(diag::err_drv_no_linker_llvm_support)
Daniel Dunbar88137642009-09-09 22:32:48 +00005028 << getToolChain().getTripleString();
Daniel Dunbar5915fbf2009-09-01 16:57:46 +00005029 else if (II.getType() == types::TY_AST)
Chris Lattner5f9e2722011-07-23 10:55:15 +00005030 D.Diag(diag::err_drv_no_ast_support)
Daniel Dunbar88137642009-09-09 22:32:48 +00005031 << getToolChain().getTripleString();
Douglas Gregorc544ba02013-03-27 16:47:18 +00005032 else if (II.getType() == types::TY_ModuleFile)
5033 D.Diag(diag::err_drv_no_module_support)
5034 << getToolChain().getTripleString();
Daniel Dunbara8304f62009-05-02 20:14:53 +00005035
Daniel Dunbarb488c1d2009-03-18 08:07:30 +00005036 if (types::canTypeBeUserSpecified(II.getType())) {
5037 CmdArgs.push_back("-x");
5038 CmdArgs.push_back(types::getTypeName(II.getType()));
5039 }
5040
Daniel Dunbar7c1e4652010-08-02 02:38:21 +00005041 if (II.isFilename())
Daniel Dunbar115a7922009-03-19 07:29:38 +00005042 CmdArgs.push_back(II.getFilename());
Daniel Dunbar48f99942010-09-25 18:10:05 +00005043 else {
5044 const Arg &A = II.getInputArg();
5045
5046 // Reverse translate some rewritten options.
5047 if (A.getOption().matches(options::OPT_Z_reserved_lib_stdcxx)) {
5048 CmdArgs.push_back("-lstdc++");
5049 continue;
5050 }
5051
Daniel Dunbar115a7922009-03-19 07:29:38 +00005052 // Don't render as input, we need gcc to do the translations.
Daniel Dunbar48f99942010-09-25 18:10:05 +00005053 A.render(Args, CmdArgs);
5054 }
Daniel Dunbarb488c1d2009-03-18 08:07:30 +00005055 }
5056
Dylan Noblesmithb8a3e812011-04-09 13:31:59 +00005057 const std::string customGCCName = D.getCCCGenericGCCName();
5058 const char *GCCName;
5059 if (!customGCCName.empty())
5060 GCCName = customGCCName.c_str();
Hans Wennborg76b86c22013-07-18 20:29:38 +00005061 else if (D.CCCIsCXX()) {
Dylan Noblesmithb8a3e812011-04-09 13:31:59 +00005062 GCCName = "g++";
Dylan Noblesmithb8a3e812011-04-09 13:31:59 +00005063 } else
5064 GCCName = "gcc";
5065
Daniel Dunbarc21c4852009-04-08 23:54:23 +00005066 const char *Exec =
Daniel Dunbar4a7e8892010-07-14 18:46:23 +00005067 Args.MakeArgString(getToolChain().GetProgramPath(GCCName));
Stephen Hines176edba2014-12-01 14:53:08 -08005068 C.addCommand(llvm::make_unique<Command>(JA, *this, Exec, CmdArgs));
Daniel Dunbar47ac7d22009-03-18 06:00:36 +00005069}
5070
Daniel Dunbar82b51cc2010-01-25 22:35:08 +00005071void gcc::Preprocess::RenderExtraToolArgs(const JobAction &JA,
5072 ArgStringList &CmdArgs) const {
Daniel Dunbarb488c1d2009-03-18 08:07:30 +00005073 CmdArgs.push_back("-E");
Daniel Dunbar47ac7d22009-03-18 06:00:36 +00005074}
5075
Daniel Dunbar82b51cc2010-01-25 22:35:08 +00005076void gcc::Compile::RenderExtraToolArgs(const JobAction &JA,
5077 ArgStringList &CmdArgs) const {
Daniel Dunbar64952502010-02-11 03:16:21 +00005078 const Driver &D = getToolChain().getDriver();
5079
Daniel Dunbar82b51cc2010-01-25 22:35:08 +00005080 // If -flto, etc. are present then make sure not to force assembly output.
Daniel Dunbar6c6424b2010-06-07 23:28:45 +00005081 if (JA.getType() == types::TY_LLVM_IR || JA.getType() == types::TY_LTO_IR ||
5082 JA.getType() == types::TY_LLVM_BC || JA.getType() == types::TY_LTO_BC)
Daniel Dunbar82b51cc2010-01-25 22:35:08 +00005083 CmdArgs.push_back("-c");
Daniel Dunbar64952502010-02-11 03:16:21 +00005084 else {
5085 if (JA.getType() != types::TY_PP_Asm)
Chris Lattner5f9e2722011-07-23 10:55:15 +00005086 D.Diag(diag::err_drv_invalid_gcc_output_type)
Daniel Dunbar64952502010-02-11 03:16:21 +00005087 << getTypeName(JA.getType());
Michael J. Spencer20249a12010-10-21 03:16:25 +00005088
Daniel Dunbar82b51cc2010-01-25 22:35:08 +00005089 CmdArgs.push_back("-S");
Daniel Dunbar64952502010-02-11 03:16:21 +00005090 }
Daniel Dunbar47ac7d22009-03-18 06:00:36 +00005091}
5092
Daniel Dunbar82b51cc2010-01-25 22:35:08 +00005093void gcc::Link::RenderExtraToolArgs(const JobAction &JA,
5094 ArgStringList &CmdArgs) const {
Daniel Dunbarb488c1d2009-03-18 08:07:30 +00005095 // The types are (hopefully) good enough.
5096}
5097
Tony Linthicum96319392011-12-12 21:14:55 +00005098// Hexagon tools start.
5099void hexagon::Assemble::RenderExtraToolArgs(const JobAction &JA,
5100 ArgStringList &CmdArgs) const {
5101
5102}
5103void hexagon::Assemble::ConstructJob(Compilation &C, const JobAction &JA,
5104 const InputInfo &Output,
5105 const InputInfoList &Inputs,
5106 const ArgList &Args,
5107 const char *LinkingOutput) const {
5108
5109 const Driver &D = getToolChain().getDriver();
5110 ArgStringList CmdArgs;
5111
5112 std::string MarchString = "-march=";
Matthew Curtis67814152012-12-06 14:16:43 +00005113 MarchString += toolchains::Hexagon_TC::GetTargetCPU(Args);
Tony Linthicum96319392011-12-12 21:14:55 +00005114 CmdArgs.push_back(Args.MakeArgString(MarchString));
5115
5116 RenderExtraToolArgs(JA, CmdArgs);
5117
5118 if (Output.isFilename()) {
5119 CmdArgs.push_back("-o");
5120 CmdArgs.push_back(Output.getFilename());
5121 } else {
5122 assert(Output.isNothing() && "Unexpected output");
5123 CmdArgs.push_back("-fsyntax-only");
5124 }
5125
Matthew Curtis33c95f12012-12-06 17:49:03 +00005126 std::string SmallDataThreshold = GetHexagonSmallDataThresholdValue(Args);
5127 if (!SmallDataThreshold.empty())
5128 CmdArgs.push_back(
5129 Args.MakeArgString(std::string("-G") + SmallDataThreshold));
Tony Linthicum96319392011-12-12 21:14:55 +00005130
Matthew Curtis3d8d4222012-12-07 17:23:04 +00005131 Args.AddAllArgValues(CmdArgs, options::OPT_Wa_COMMA,
5132 options::OPT_Xassembler);
5133
Tony Linthicum96319392011-12-12 21:14:55 +00005134 // Only pass -x if gcc will understand it; otherwise hope gcc
5135 // understands the suffix correctly. The main use case this would go
5136 // wrong in is for linker inputs if they happened to have an odd
5137 // suffix; really the only way to get this to happen is a command
5138 // like '-x foobar a.c' which will treat a.c like a linker input.
5139 //
5140 // FIXME: For the linker case specifically, can we safely convert
5141 // inputs into '-Wl,' options?
Stephen Hinesc568f1e2014-07-21 00:47:37 -07005142 for (const auto &II : Inputs) {
Tony Linthicum96319392011-12-12 21:14:55 +00005143 // Don't try to pass LLVM or AST inputs to a generic gcc.
5144 if (II.getType() == types::TY_LLVM_IR || II.getType() == types::TY_LTO_IR ||
5145 II.getType() == types::TY_LLVM_BC || II.getType() == types::TY_LTO_BC)
5146 D.Diag(clang::diag::err_drv_no_linker_llvm_support)
5147 << getToolChain().getTripleString();
5148 else if (II.getType() == types::TY_AST)
5149 D.Diag(clang::diag::err_drv_no_ast_support)
5150 << getToolChain().getTripleString();
Douglas Gregorc544ba02013-03-27 16:47:18 +00005151 else if (II.getType() == types::TY_ModuleFile)
5152 D.Diag(diag::err_drv_no_module_support)
5153 << getToolChain().getTripleString();
Tony Linthicum96319392011-12-12 21:14:55 +00005154
5155 if (II.isFilename())
5156 CmdArgs.push_back(II.getFilename());
5157 else
5158 // Don't render as input, we need gcc to do the translations. FIXME: Pranav: What is this ?
5159 II.getInputArg().render(Args, CmdArgs);
5160 }
5161
5162 const char *GCCName = "hexagon-as";
Stephen Hinesc568f1e2014-07-21 00:47:37 -07005163 const char *Exec = Args.MakeArgString(getToolChain().GetProgramPath(GCCName));
Stephen Hines176edba2014-12-01 14:53:08 -08005164 C.addCommand(llvm::make_unique<Command>(JA, *this, Exec, CmdArgs));
Tony Linthicum96319392011-12-12 21:14:55 +00005165}
Stephen Hinesc568f1e2014-07-21 00:47:37 -07005166
Tony Linthicum96319392011-12-12 21:14:55 +00005167void hexagon::Link::RenderExtraToolArgs(const JobAction &JA,
5168 ArgStringList &CmdArgs) const {
5169 // The types are (hopefully) good enough.
5170}
5171
5172void hexagon::Link::ConstructJob(Compilation &C, const JobAction &JA,
5173 const InputInfo &Output,
5174 const InputInfoList &Inputs,
5175 const ArgList &Args,
5176 const char *LinkingOutput) const {
5177
Matthew Curtis5fdf3502012-12-06 15:46:07 +00005178 const toolchains::Hexagon_TC& ToolChain =
5179 static_cast<const toolchains::Hexagon_TC&>(getToolChain());
5180 const Driver &D = ToolChain.getDriver();
5181
Tony Linthicum96319392011-12-12 21:14:55 +00005182 ArgStringList CmdArgs;
5183
Matthew Curtis5fdf3502012-12-06 15:46:07 +00005184 //----------------------------------------------------------------------------
5185 //
5186 //----------------------------------------------------------------------------
5187 bool hasStaticArg = Args.hasArg(options::OPT_static);
5188 bool buildingLib = Args.hasArg(options::OPT_shared);
Matthew Curtis33c95f12012-12-06 17:49:03 +00005189 bool buildPIE = Args.hasArg(options::OPT_pie);
Matthew Curtis5fdf3502012-12-06 15:46:07 +00005190 bool incStdLib = !Args.hasArg(options::OPT_nostdlib);
5191 bool incStartFiles = !Args.hasArg(options::OPT_nostartfiles);
5192 bool incDefLibs = !Args.hasArg(options::OPT_nodefaultlibs);
5193 bool useShared = buildingLib && !hasStaticArg;
Tony Linthicum96319392011-12-12 21:14:55 +00005194
Matthew Curtis5fdf3502012-12-06 15:46:07 +00005195 //----------------------------------------------------------------------------
5196 // Silence warnings for various options
5197 //----------------------------------------------------------------------------
Tony Linthicum96319392011-12-12 21:14:55 +00005198
Matthew Curtis5fdf3502012-12-06 15:46:07 +00005199 Args.ClaimAllArgs(options::OPT_g_Group);
5200 Args.ClaimAllArgs(options::OPT_emit_llvm);
5201 Args.ClaimAllArgs(options::OPT_w); // Other warning options are already
5202 // handled somewhere else.
5203 Args.ClaimAllArgs(options::OPT_static_libgcc);
5204
5205 //----------------------------------------------------------------------------
5206 //
5207 //----------------------------------------------------------------------------
Stephen Hinesc568f1e2014-07-21 00:47:37 -07005208 for (const auto &Opt : ToolChain.ExtraOpts)
5209 CmdArgs.push_back(Opt.c_str());
Tony Linthicum96319392011-12-12 21:14:55 +00005210
Matthew Curtis67814152012-12-06 14:16:43 +00005211 std::string MarchString = toolchains::Hexagon_TC::GetTargetCPU(Args);
5212 CmdArgs.push_back(Args.MakeArgString("-m" + MarchString));
Sebastian Pop43115d42012-01-13 20:37:10 +00005213
Matthew Curtis5fdf3502012-12-06 15:46:07 +00005214 if (buildingLib) {
5215 CmdArgs.push_back("-shared");
5216 CmdArgs.push_back("-call_shared"); // should be the default, but doing as
5217 // hexagon-gcc does
Tony Linthicum96319392011-12-12 21:14:55 +00005218 }
5219
Matthew Curtis5fdf3502012-12-06 15:46:07 +00005220 if (hasStaticArg)
5221 CmdArgs.push_back("-static");
Tony Linthicum96319392011-12-12 21:14:55 +00005222
Matthew Curtis33c95f12012-12-06 17:49:03 +00005223 if (buildPIE && !buildingLib)
5224 CmdArgs.push_back("-pie");
5225
5226 std::string SmallDataThreshold = GetHexagonSmallDataThresholdValue(Args);
5227 if (!SmallDataThreshold.empty()) {
5228 CmdArgs.push_back(
5229 Args.MakeArgString(std::string("-G") + SmallDataThreshold));
5230 }
5231
Matthew Curtis5fdf3502012-12-06 15:46:07 +00005232 //----------------------------------------------------------------------------
5233 //
5234 //----------------------------------------------------------------------------
5235 CmdArgs.push_back("-o");
5236 CmdArgs.push_back(Output.getFilename());
Tony Linthicum96319392011-12-12 21:14:55 +00005237
Matthew Curtis5fdf3502012-12-06 15:46:07 +00005238 const std::string MarchSuffix = "/" + MarchString;
5239 const std::string G0Suffix = "/G0";
5240 const std::string MarchG0Suffix = MarchSuffix + G0Suffix;
Stephen Hines176edba2014-12-01 14:53:08 -08005241 const std::string RootDir =
5242 toolchains::Hexagon_TC::GetGnuDir(D.InstalledDir, Args) + "/";
Matthew Curtis5fdf3502012-12-06 15:46:07 +00005243 const std::string StartFilesDir = RootDir
5244 + "hexagon/lib"
5245 + (buildingLib
5246 ? MarchG0Suffix : MarchSuffix);
5247
5248 //----------------------------------------------------------------------------
5249 // moslib
5250 //----------------------------------------------------------------------------
5251 std::vector<std::string> oslibs;
5252 bool hasStandalone= false;
5253
5254 for (arg_iterator it = Args.filtered_begin(options::OPT_moslib_EQ),
5255 ie = Args.filtered_end(); it != ie; ++it) {
5256 (*it)->claim();
5257 oslibs.push_back((*it)->getValue());
5258 hasStandalone = hasStandalone || (oslibs.back() == "standalone");
Tony Linthicum96319392011-12-12 21:14:55 +00005259 }
Matthew Curtis5fdf3502012-12-06 15:46:07 +00005260 if (oslibs.empty()) {
5261 oslibs.push_back("standalone");
5262 hasStandalone = true;
5263 }
Tony Linthicum96319392011-12-12 21:14:55 +00005264
Matthew Curtis5fdf3502012-12-06 15:46:07 +00005265 //----------------------------------------------------------------------------
5266 // Start Files
5267 //----------------------------------------------------------------------------
5268 if (incStdLib && incStartFiles) {
5269
5270 if (!buildingLib) {
5271 if (hasStandalone) {
5272 CmdArgs.push_back(
5273 Args.MakeArgString(StartFilesDir + "/crt0_standalone.o"));
5274 }
5275 CmdArgs.push_back(Args.MakeArgString(StartFilesDir + "/crt0.o"));
5276 }
5277 std::string initObj = useShared ? "/initS.o" : "/init.o";
5278 CmdArgs.push_back(Args.MakeArgString(StartFilesDir + initObj));
5279 }
5280
5281 //----------------------------------------------------------------------------
5282 // Library Search Paths
5283 //----------------------------------------------------------------------------
5284 const ToolChain::path_list &LibPaths = ToolChain.getFilePaths();
Stephen Hinesc568f1e2014-07-21 00:47:37 -07005285 for (const auto &LibPath : LibPaths)
5286 CmdArgs.push_back(Args.MakeArgString(StringRef("-L") + LibPath));
Matthew Curtis5fdf3502012-12-06 15:46:07 +00005287
5288 //----------------------------------------------------------------------------
5289 //
5290 //----------------------------------------------------------------------------
5291 Args.AddAllArgs(CmdArgs, options::OPT_T_Group);
5292 Args.AddAllArgs(CmdArgs, options::OPT_e);
5293 Args.AddAllArgs(CmdArgs, options::OPT_s);
5294 Args.AddAllArgs(CmdArgs, options::OPT_t);
5295 Args.AddAllArgs(CmdArgs, options::OPT_u_Group);
5296
5297 AddLinkerInputs(ToolChain, Inputs, Args, CmdArgs);
5298
5299 //----------------------------------------------------------------------------
5300 // Libraries
5301 //----------------------------------------------------------------------------
5302 if (incStdLib && incDefLibs) {
Hans Wennborg76b86c22013-07-18 20:29:38 +00005303 if (D.CCCIsCXX()) {
Matthew Curtis5fdf3502012-12-06 15:46:07 +00005304 ToolChain.AddCXXStdlibLibArgs(Args, CmdArgs);
5305 CmdArgs.push_back("-lm");
5306 }
5307
5308 CmdArgs.push_back("--start-group");
5309
5310 if (!buildingLib) {
5311 for(std::vector<std::string>::iterator i = oslibs.begin(),
5312 e = oslibs.end(); i != e; ++i)
5313 CmdArgs.push_back(Args.MakeArgString("-l" + *i));
5314 CmdArgs.push_back("-lc");
5315 }
5316 CmdArgs.push_back("-lgcc");
5317
5318 CmdArgs.push_back("--end-group");
5319 }
5320
5321 //----------------------------------------------------------------------------
5322 // End files
5323 //----------------------------------------------------------------------------
5324 if (incStdLib && incStartFiles) {
5325 std::string finiObj = useShared ? "/finiS.o" : "/fini.o";
5326 CmdArgs.push_back(Args.MakeArgString(StartFilesDir + finiObj));
5327 }
5328
5329 std::string Linker = ToolChain.GetProgramPath("hexagon-ld");
Stephen Hines176edba2014-12-01 14:53:08 -08005330 C.addCommand(llvm::make_unique<Command>(JA, *this, Args.MakeArgString(Linker),
5331 CmdArgs));
Tony Linthicum96319392011-12-12 21:14:55 +00005332}
5333// Hexagon tools end.
5334
Stephen Hines176edba2014-12-01 14:53:08 -08005335/// Get the (LLVM) name of the minimum ARM CPU for the arch we are targeting.
Stephen Hines651f13c2014-04-23 16:59:28 -07005336const char *arm::getARMCPUForMArch(const ArgList &Args,
5337 const llvm::Triple &Triple) {
5338 StringRef MArch;
5339 if (Arg *A = Args.getLastArg(options::OPT_march_EQ)) {
5340 // Otherwise, if we have -march= choose the base CPU for that arch.
5341 MArch = A->getValue();
5342 } else {
5343 // Otherwise, use the Arch from the triple.
5344 MArch = Triple.getArchName();
5345 }
5346
5347 // Handle -march=native.
5348 if (MArch == "native") {
5349 std::string CPU = llvm::sys::getHostCPUName();
5350 if (CPU != "generic") {
5351 // Translate the native cpu into the architecture. The switch below will
5352 // then chose the minimum cpu for that arch.
5353 MArch = std::string("arm") + arm::getLLVMArchSuffixForARM(CPU);
5354 }
5355 }
5356
Stephen Hines176edba2014-12-01 14:53:08 -08005357 return Triple.getARMCPUForArch(MArch);
Stephen Hines651f13c2014-04-23 16:59:28 -07005358}
5359
5360/// getARMTargetCPU - Get the (LLVM) name of the ARM cpu we are targeting.
5361StringRef arm::getARMTargetCPU(const ArgList &Args,
5362 const llvm::Triple &Triple) {
5363 // FIXME: Warn on inconsistent use of -mcpu and -march.
5364 // If we have -mcpu=, use that.
5365 if (Arg *A = Args.getLastArg(options::OPT_mcpu_EQ)) {
5366 StringRef MCPU = A->getValue();
5367 // Handle -mcpu=native.
5368 if (MCPU == "native")
5369 return llvm::sys::getHostCPUName();
5370 else
5371 return MCPU;
5372 }
5373
5374 return getARMCPUForMArch(Args, Triple);
5375}
5376
5377/// getLLVMArchSuffixForARM - Get the LLVM arch name to use for a particular
5378/// CPU.
5379//
5380// FIXME: This is redundant with -mcpu, why does LLVM use this.
5381// FIXME: tblgen this, or kill it!
5382const char *arm::getLLVMArchSuffixForARM(StringRef CPU) {
5383 return llvm::StringSwitch<const char *>(CPU)
5384 .Case("strongarm", "v4")
5385 .Cases("arm7tdmi", "arm7tdmi-s", "arm710t", "v4t")
5386 .Cases("arm720t", "arm9", "arm9tdmi", "v4t")
5387 .Cases("arm920", "arm920t", "arm922t", "v4t")
5388 .Cases("arm940t", "ep9312","v4t")
5389 .Cases("arm10tdmi", "arm1020t", "v5")
5390 .Cases("arm9e", "arm926ej-s", "arm946e-s", "v5e")
5391 .Cases("arm966e-s", "arm968e-s", "arm10e", "v5e")
5392 .Cases("arm1020e", "arm1022e", "xscale", "iwmmxt", "v5e")
5393 .Cases("arm1136j-s", "arm1136jf-s", "arm1176jz-s", "v6")
5394 .Cases("arm1176jzf-s", "mpcorenovfp", "mpcore", "v6")
5395 .Cases("arm1156t2-s", "arm1156t2f-s", "v6t2")
Stephen Hines176edba2014-12-01 14:53:08 -08005396 .Cases("cortex-a5", "cortex-a7", "cortex-a8", "v7")
5397 .Cases("cortex-a9", "cortex-a12", "cortex-a15", "cortex-a17", "krait", "v7")
Stephen Hines651f13c2014-04-23 16:59:28 -07005398 .Cases("cortex-r4", "cortex-r5", "v7r")
5399 .Case("cortex-m0", "v6m")
5400 .Case("cortex-m3", "v7m")
Stephen Hines176edba2014-12-01 14:53:08 -08005401 .Cases("cortex-m4", "cortex-m7", "v7em")
Stephen Hines651f13c2014-04-23 16:59:28 -07005402 .Case("swift", "v7s")
5403 .Case("cyclone", "v8")
5404 .Cases("cortex-a53", "cortex-a57", "v8")
5405 .Default("");
5406}
5407
5408bool mips::hasMipsAbiArg(const ArgList &Args, const char *Value) {
5409 Arg *A = Args.getLastArg(options::OPT_mabi_EQ);
5410 return A && (A->getValue() == StringRef(Value));
5411}
5412
Stephen Hines176edba2014-12-01 14:53:08 -08005413bool mips::isUCLibc(const ArgList &Args) {
5414 Arg *A = Args.getLastArg(options::OPT_m_libc_Group);
5415 return A && A->getOption().matches(options::OPT_muclibc);
5416}
5417
Stephen Hinesc568f1e2014-07-21 00:47:37 -07005418bool mips::isNaN2008(const ArgList &Args, const llvm::Triple &Triple) {
5419 if (Arg *NaNArg = Args.getLastArg(options::OPT_mnan_EQ))
5420 return llvm::StringSwitch<bool>(NaNArg->getValue())
5421 .Case("2008", true)
5422 .Case("legacy", false)
5423 .Default(false);
5424
5425 // NaN2008 is the default for MIPS32r6/MIPS64r6.
5426 return llvm::StringSwitch<bool>(getCPUName(Args, Triple))
5427 .Cases("mips32r6", "mips64r6", true)
5428 .Default(false);
5429
5430 return false;
5431}
5432
Stephen Hines176edba2014-12-01 14:53:08 -08005433bool mips::isFPXXDefault(const llvm::Triple &Triple, StringRef CPUName,
5434 StringRef ABIName) {
5435 if (Triple.getVendor() != llvm::Triple::ImaginationTechnologies &&
5436 Triple.getVendor() != llvm::Triple::MipsTechnologies)
5437 return false;
5438
5439 if (ABIName != "32")
5440 return false;
5441
5442 return llvm::StringSwitch<bool>(CPUName)
5443 .Cases("mips2", "mips3", "mips4", "mips5", true)
5444 .Cases("mips32", "mips32r2", true)
5445 .Cases("mips64", "mips64r2", true)
5446 .Default(false);
5447}
5448
Stephen Hines651f13c2014-04-23 16:59:28 -07005449llvm::Triple::ArchType darwin::getArchTypeForMachOArchName(StringRef Str) {
Rafael Espindolacfed8282012-10-31 18:51:07 +00005450 // See arch(3) and llvm-gcc's driver-driver.c. We don't implement support for
5451 // archs which Darwin doesn't use.
5452
5453 // The matching this routine does is fairly pointless, since it is neither the
5454 // complete architecture list, nor a reasonable subset. The problem is that
5455 // historically the driver driver accepts this and also ties its -march=
5456 // handling to the architecture name, so we need to be careful before removing
5457 // support for it.
5458
5459 // This code must be kept in sync with Clang's Darwin specific argument
5460 // translation.
5461
5462 return llvm::StringSwitch<llvm::Triple::ArchType>(Str)
5463 .Cases("ppc", "ppc601", "ppc603", "ppc604", "ppc604e", llvm::Triple::ppc)
5464 .Cases("ppc750", "ppc7400", "ppc7450", "ppc970", llvm::Triple::ppc)
5465 .Case("ppc64", llvm::Triple::ppc64)
5466 .Cases("i386", "i486", "i486SX", "i586", "i686", llvm::Triple::x86)
5467 .Cases("pentium", "pentpro", "pentIIm3", "pentIIm5", "pentium4",
5468 llvm::Triple::x86)
Jim Grosbach32ca73e2013-11-16 00:53:35 +00005469 .Cases("x86_64", "x86_64h", llvm::Triple::x86_64)
Rafael Espindolacfed8282012-10-31 18:51:07 +00005470 // This is derived from the driver driver.
Bob Wilson2503ebd2013-03-04 22:37:49 +00005471 .Cases("arm", "armv4t", "armv5", "armv6", "armv6m", llvm::Triple::arm)
Stephen Hines651f13c2014-04-23 16:59:28 -07005472 .Cases("armv7", "armv7em", "armv7k", "armv7m", llvm::Triple::arm)
Bob Wilson2503ebd2013-03-04 22:37:49 +00005473 .Cases("armv7s", "xscale", llvm::Triple::arm)
Stephen Hines176edba2014-12-01 14:53:08 -08005474 .Case("arm64", llvm::Triple::aarch64)
Rafael Espindolacfed8282012-10-31 18:51:07 +00005475 .Case("r600", llvm::Triple::r600)
5476 .Case("nvptx", llvm::Triple::nvptx)
5477 .Case("nvptx64", llvm::Triple::nvptx64)
5478 .Case("amdil", llvm::Triple::amdil)
5479 .Case("spir", llvm::Triple::spir)
5480 .Default(llvm::Triple::UnknownArch);
5481}
Tony Linthicum96319392011-12-12 21:14:55 +00005482
Stephen Hines651f13c2014-04-23 16:59:28 -07005483void darwin::setTripleTypeForMachOArchName(llvm::Triple &T, StringRef Str) {
5484 llvm::Triple::ArchType Arch = getArchTypeForMachOArchName(Str);
5485 T.setArch(Arch);
5486
5487 if (Str == "x86_64h")
5488 T.setArchName(Str);
5489 else if (Str == "armv6m" || Str == "armv7m" || Str == "armv7em") {
5490 T.setOS(llvm::Triple::UnknownOS);
5491 T.setObjectFormat(llvm::Triple::MachO);
5492 }
5493}
5494
Bob Wilson66b8a662012-11-23 06:14:39 +00005495const char *Clang::getBaseInputName(const ArgList &Args,
5496 const InputInfoList &Inputs) {
Michael J. Spencer472ccff2010-12-18 00:19:12 +00005497 return Args.MakeArgString(
5498 llvm::sys::path::filename(Inputs[0].getBaseInput()));
Daniel Dunbara3ec60e2009-03-29 18:40:18 +00005499}
5500
Bob Wilson66b8a662012-11-23 06:14:39 +00005501const char *Clang::getBaseInputStem(const ArgList &Args,
5502 const InputInfoList &Inputs) {
Daniel Dunbara3ec60e2009-03-29 18:40:18 +00005503 const char *Str = getBaseInputName(Args, Inputs);
5504
Chris Lattner657ca662011-01-16 08:14:11 +00005505 if (const char *End = strrchr(Str, '.'))
Daniel Dunbar88137642009-09-09 22:32:48 +00005506 return Args.MakeArgString(std::string(Str, End));
Daniel Dunbara3ec60e2009-03-29 18:40:18 +00005507
5508 return Str;
5509}
5510
Bob Wilson66b8a662012-11-23 06:14:39 +00005511const char *Clang::getDependencyFileName(const ArgList &Args,
5512 const InputInfoList &Inputs) {
Daniel Dunbara3ec60e2009-03-29 18:40:18 +00005513 // FIXME: Think about this more.
5514 std::string Res;
5515
5516 if (Arg *OutputOpt = Args.getLastArg(options::OPT_o)) {
Richard Smith1d489cf2012-11-01 04:30:05 +00005517 std::string Str(OutputOpt->getValue());
Daniel Dunbara3ec60e2009-03-29 18:40:18 +00005518 Res = Str.substr(0, Str.rfind('.'));
Chad Rosier30601782011-08-17 23:08:45 +00005519 } else {
Bob Wilson66b8a662012-11-23 06:14:39 +00005520 Res = getBaseInputStem(Args, Inputs);
Chad Rosier30601782011-08-17 23:08:45 +00005521 }
Daniel Dunbar88137642009-09-09 22:32:48 +00005522 return Args.MakeArgString(Res + ".d");
Daniel Dunbara3ec60e2009-03-29 18:40:18 +00005523}
5524
Daniel Dunbar8cac5f72009-03-20 16:06:39 +00005525void darwin::Assemble::ConstructJob(Compilation &C, const JobAction &JA,
Daniel Dunbar2fe238e2010-08-02 02:38:28 +00005526 const InputInfo &Output,
Daniel Dunbarc21c4852009-04-08 23:54:23 +00005527 const InputInfoList &Inputs,
5528 const ArgList &Args,
Daniel Dunbar8cac5f72009-03-20 16:06:39 +00005529 const char *LinkingOutput) const {
5530 ArgStringList CmdArgs;
5531
5532 assert(Inputs.size() == 1 && "Unexpected number of inputs.");
5533 const InputInfo &Input = Inputs[0];
5534
Daniel Dunbar34bac1f2011-04-12 23:59:20 +00005535 // Determine the original source input.
5536 const Action *SourceAction = &JA;
5537 while (SourceAction->getKind() != Action::InputClass) {
5538 assert(!SourceAction->getInputs().empty() && "unexpected root action!");
5539 SourceAction = SourceAction->getInputs()[0];
5540 }
5541
Stephen Hines651f13c2014-04-23 16:59:28 -07005542 // If -fno_integrated_as is used add -Q to the darwin assember driver to make
Kevin Enderby6efcf6f2013-11-18 23:30:29 +00005543 // sure it runs its system assembler not clang's integrated assembler.
Stephen Hines651f13c2014-04-23 16:59:28 -07005544 // Applicable to darwin11+ and Xcode 4+. darwin<10 lacked integrated-as.
5545 // FIXME: at run-time detect assembler capabilities or rely on version
5546 // information forwarded by -target-assembler-version (future)
5547 if (Args.hasArg(options::OPT_fno_integrated_as)) {
5548 const llvm::Triple &T(getToolChain().getTriple());
5549 if (!(T.isMacOSX() && T.isMacOSXVersionLT(10, 7)))
5550 CmdArgs.push_back("-Q");
5551 }
Kevin Enderby6efcf6f2013-11-18 23:30:29 +00005552
Daniel Dunbar34bac1f2011-04-12 23:59:20 +00005553 // Forward -g, assuming we are dealing with an actual assembly file.
Eric Christopher88b7cf02011-08-19 00:30:14 +00005554 if (SourceAction->getType() == types::TY_Asm ||
Daniel Dunbar34bac1f2011-04-12 23:59:20 +00005555 SourceAction->getType() == types::TY_PP_Asm) {
Daniel Dunbar8e4fea62009-04-01 00:27:44 +00005556 if (Args.hasArg(options::OPT_gstabs))
5557 CmdArgs.push_back("--gstabs");
5558 else if (Args.hasArg(options::OPT_g_Group))
Bob Wilson591ff152011-11-02 05:10:45 +00005559 CmdArgs.push_back("-g");
Daniel Dunbar8e4fea62009-04-01 00:27:44 +00005560 }
Daniel Dunbarc21c4852009-04-08 23:54:23 +00005561
Daniel Dunbar8cac5f72009-03-20 16:06:39 +00005562 // Derived from asm spec.
Stephen Hines651f13c2014-04-23 16:59:28 -07005563 AddMachOArch(Args, CmdArgs);
Daniel Dunbar8cac5f72009-03-20 16:06:39 +00005564
Daniel Dunbarf5438e32010-07-22 01:47:22 +00005565 // Use -force_cpusubtype_ALL on x86 by default.
Eli Bendersky8f4269a2013-07-24 22:20:49 +00005566 if (getToolChain().getArch() == llvm::Triple::x86 ||
5567 getToolChain().getArch() == llvm::Triple::x86_64 ||
Daniel Dunbarcc6f8032009-09-09 18:36:27 +00005568 Args.hasArg(options::OPT_force__cpusubtype__ALL))
5569 CmdArgs.push_back("-force_cpusubtype_ALL");
5570
Eli Bendersky8f4269a2013-07-24 22:20:49 +00005571 if (getToolChain().getArch() != llvm::Triple::x86_64 &&
Daniel Dunbar7a0c0642012-10-15 22:23:53 +00005572 (((Args.hasArg(options::OPT_mkernel) ||
Eric Christopher59320e72013-02-21 22:35:01 +00005573 Args.hasArg(options::OPT_fapple_kext)) &&
Stephen Hines651f13c2014-04-23 16:59:28 -07005574 getMachOToolChain().isKernelStatic()) ||
Daniel Dunbar7a0c0642012-10-15 22:23:53 +00005575 Args.hasArg(options::OPT_static)))
Daniel Dunbarc21c4852009-04-08 23:54:23 +00005576 CmdArgs.push_back("-static");
5577
Daniel Dunbar8cac5f72009-03-20 16:06:39 +00005578 Args.AddAllArgValues(CmdArgs, options::OPT_Wa_COMMA,
5579 options::OPT_Xassembler);
5580
5581 assert(Output.isFilename() && "Unexpected lipo output.");
5582 CmdArgs.push_back("-o");
5583 CmdArgs.push_back(Output.getFilename());
5584
Daniel Dunbar7c1e4652010-08-02 02:38:21 +00005585 assert(Input.isFilename() && "Invalid input.");
5586 CmdArgs.push_back(Input.getFilename());
Daniel Dunbar8cac5f72009-03-20 16:06:39 +00005587
5588 // asm_final spec is empty.
5589
Daniel Dunbarc21c4852009-04-08 23:54:23 +00005590 const char *Exec =
Daniel Dunbar4a7e8892010-07-14 18:46:23 +00005591 Args.MakeArgString(getToolChain().GetProgramPath("as"));
Stephen Hines176edba2014-12-01 14:53:08 -08005592 C.addCommand(llvm::make_unique<Command>(JA, *this, Exec, CmdArgs));
Daniel Dunbar8cac5f72009-03-20 16:06:39 +00005593}
Daniel Dunbarff7488d2009-03-20 00:52:38 +00005594
Stephen Hines651f13c2014-04-23 16:59:28 -07005595void darwin::MachOTool::anchor() {}
David Blaikie99ba9e32011-12-20 02:48:34 +00005596
Stephen Hines651f13c2014-04-23 16:59:28 -07005597void darwin::MachOTool::AddMachOArch(const ArgList &Args,
5598 ArgStringList &CmdArgs) const {
5599 StringRef ArchName = getMachOToolChain().getMachOArchName(Args);
Daniel Dunbareeff4062010-01-22 02:04:58 +00005600
Daniel Dunbar02633b52009-03-26 16:23:12 +00005601 // Derived from darwin_arch spec.
5602 CmdArgs.push_back("-arch");
Daniel Dunbareeff4062010-01-22 02:04:58 +00005603 CmdArgs.push_back(Args.MakeArgString(ArchName));
Daniel Dunbar78dbd582009-09-04 18:35:31 +00005604
Daniel Dunbareeff4062010-01-22 02:04:58 +00005605 // FIXME: Is this needed anymore?
5606 if (ArchName == "arm")
Daniel Dunbar78dbd582009-09-04 18:35:31 +00005607 CmdArgs.push_back("-force_cpusubtype_ALL");
Daniel Dunbar02633b52009-03-26 16:23:12 +00005608}
5609
Bill Wendling6acf8b42012-10-02 18:02:50 +00005610bool darwin::Link::NeedsTempPath(const InputInfoList &Inputs) const {
5611 // We only need to generate a temp path for LTO if we aren't compiling object
5612 // files. When compiling source files, we run 'dsymutil' after linking. We
5613 // don't run 'dsymutil' when compiling object files.
Stephen Hinesc568f1e2014-07-21 00:47:37 -07005614 for (const auto &Input : Inputs)
5615 if (Input.getType() != types::TY_Object)
Bill Wendling6acf8b42012-10-02 18:02:50 +00005616 return true;
5617
5618 return false;
5619}
5620
Daniel Dunbar748de8e2010-09-09 21:51:05 +00005621void darwin::Link::AddLinkArgs(Compilation &C,
5622 const ArgList &Args,
Bill Wendling6acf8b42012-10-02 18:02:50 +00005623 ArgStringList &CmdArgs,
5624 const InputInfoList &Inputs) const {
Daniel Dunbaree788e72009-12-21 18:54:17 +00005625 const Driver &D = getToolChain().getDriver();
Stephen Hines651f13c2014-04-23 16:59:28 -07005626 const toolchains::MachO &MachOTC = getMachOToolChain();
Daniel Dunbar02633b52009-03-26 16:23:12 +00005627
Daniel Dunbarb18dc5b2010-08-11 23:07:50 +00005628 unsigned Version[3] = { 0, 0, 0 };
5629 if (Arg *A = Args.getLastArg(options::OPT_mlinker_version_EQ)) {
5630 bool HadExtra;
Richard Smith1d489cf2012-11-01 04:30:05 +00005631 if (!Driver::GetReleaseVersion(A->getValue(), Version[0],
Daniel Dunbarb18dc5b2010-08-11 23:07:50 +00005632 Version[1], Version[2], HadExtra) ||
5633 HadExtra)
Chris Lattner5f9e2722011-07-23 10:55:15 +00005634 D.Diag(diag::err_drv_invalid_version_number)
Daniel Dunbarb18dc5b2010-08-11 23:07:50 +00005635 << A->getAsString(Args);
5636 }
5637
Stephen Hines651f13c2014-04-23 16:59:28 -07005638 // Newer linkers support -demangle. Pass it if supported and not disabled by
Daniel Dunbarb18dc5b2010-08-11 23:07:50 +00005639 // the user.
Stephen Hines651f13c2014-04-23 16:59:28 -07005640 if (Version[0] >= 100 && !Args.hasArg(options::OPT_Z_Xlinker__no_demangle))
5641 CmdArgs.push_back("-demangle");
Daniel Dunbarb18dc5b2010-08-11 23:07:50 +00005642
Bob Wilsonbd77c592013-08-02 22:25:34 +00005643 if (Args.hasArg(options::OPT_rdynamic) && Version[0] >= 137)
5644 CmdArgs.push_back("-export_dynamic");
5645
Bill Wendlingc35f9082012-11-16 23:03:00 +00005646 // If we are using LTO, then automatically create a temporary file path for
5647 // the linker to use, so that it's lifetime will extend past a possible
5648 // dsymutil step.
5649 if (Version[0] >= 116 && D.IsUsingLTO(Args) && NeedsTempPath(Inputs)) {
5650 const char *TmpPath = C.getArgs().MakeArgString(
5651 D.GetTemporaryPath("cc", types::getTypeTempSuffix(types::TY_Object)));
5652 C.addTempFile(TmpPath);
5653 CmdArgs.push_back("-object_path_lto");
5654 CmdArgs.push_back(TmpPath);
Daniel Dunbar5bfa6562011-06-21 20:55:11 +00005655 }
5656
Daniel Dunbar02633b52009-03-26 16:23:12 +00005657 // Derived from the "link" spec.
5658 Args.AddAllArgs(CmdArgs, options::OPT_static);
5659 if (!Args.hasArg(options::OPT_static))
5660 CmdArgs.push_back("-dynamic");
5661 if (Args.hasArg(options::OPT_fgnu_runtime)) {
5662 // FIXME: gcc replaces -lobjc in forward args with -lobjc-gnu
5663 // here. How do we wish to handle such things?
5664 }
Daniel Dunbarc21c4852009-04-08 23:54:23 +00005665
Daniel Dunbar02633b52009-03-26 16:23:12 +00005666 if (!Args.hasArg(options::OPT_dynamiclib)) {
Stephen Hines651f13c2014-04-23 16:59:28 -07005667 AddMachOArch(Args, CmdArgs);
Daniel Dunbara6d38492010-01-22 02:04:52 +00005668 // FIXME: Why do this only on this path?
Daniel Dunbar8917dd42010-01-22 03:37:33 +00005669 Args.AddLastArg(CmdArgs, options::OPT_force__cpusubtype__ALL);
Daniel Dunbar02633b52009-03-26 16:23:12 +00005670
5671 Args.AddLastArg(CmdArgs, options::OPT_bundle);
5672 Args.AddAllArgs(CmdArgs, options::OPT_bundle__loader);
5673 Args.AddAllArgs(CmdArgs, options::OPT_client__name);
5674
5675 Arg *A;
5676 if ((A = Args.getLastArg(options::OPT_compatibility__version)) ||
5677 (A = Args.getLastArg(options::OPT_current__version)) ||
5678 (A = Args.getLastArg(options::OPT_install__name)))
Chris Lattner5f9e2722011-07-23 10:55:15 +00005679 D.Diag(diag::err_drv_argument_only_allowed_with)
Daniel Dunbar02633b52009-03-26 16:23:12 +00005680 << A->getAsString(Args) << "-dynamiclib";
5681
5682 Args.AddLastArg(CmdArgs, options::OPT_force__flat__namespace);
5683 Args.AddLastArg(CmdArgs, options::OPT_keep__private__externs);
5684 Args.AddLastArg(CmdArgs, options::OPT_private__bundle);
5685 } else {
5686 CmdArgs.push_back("-dylib");
5687
5688 Arg *A;
5689 if ((A = Args.getLastArg(options::OPT_bundle)) ||
5690 (A = Args.getLastArg(options::OPT_bundle__loader)) ||
5691 (A = Args.getLastArg(options::OPT_client__name)) ||
5692 (A = Args.getLastArg(options::OPT_force__flat__namespace)) ||
5693 (A = Args.getLastArg(options::OPT_keep__private__externs)) ||
5694 (A = Args.getLastArg(options::OPT_private__bundle)))
Chris Lattner5f9e2722011-07-23 10:55:15 +00005695 D.Diag(diag::err_drv_argument_not_allowed_with)
Daniel Dunbar02633b52009-03-26 16:23:12 +00005696 << A->getAsString(Args) << "-dynamiclib";
Daniel Dunbarc21c4852009-04-08 23:54:23 +00005697
Daniel Dunbar02633b52009-03-26 16:23:12 +00005698 Args.AddAllArgsTranslated(CmdArgs, options::OPT_compatibility__version,
5699 "-dylib_compatibility_version");
5700 Args.AddAllArgsTranslated(CmdArgs, options::OPT_current__version,
5701 "-dylib_current_version");
5702
Stephen Hines651f13c2014-04-23 16:59:28 -07005703 AddMachOArch(Args, CmdArgs);
Daniel Dunbar02633b52009-03-26 16:23:12 +00005704
5705 Args.AddAllArgsTranslated(CmdArgs, options::OPT_install__name,
5706 "-dylib_install_name");
5707 }
5708
5709 Args.AddLastArg(CmdArgs, options::OPT_all__load);
5710 Args.AddAllArgs(CmdArgs, options::OPT_allowable__client);
5711 Args.AddLastArg(CmdArgs, options::OPT_bind__at__load);
Stephen Hines651f13c2014-04-23 16:59:28 -07005712 if (MachOTC.isTargetIOSBased())
Daniel Dunbard82f8fa2009-09-04 18:35:41 +00005713 Args.AddLastArg(CmdArgs, options::OPT_arch__errors__fatal);
Daniel Dunbar02633b52009-03-26 16:23:12 +00005714 Args.AddLastArg(CmdArgs, options::OPT_dead__strip);
5715 Args.AddLastArg(CmdArgs, options::OPT_no__dead__strip__inits__and__terms);
5716 Args.AddAllArgs(CmdArgs, options::OPT_dylib__file);
5717 Args.AddLastArg(CmdArgs, options::OPT_dynamic);
5718 Args.AddAllArgs(CmdArgs, options::OPT_exported__symbols__list);
5719 Args.AddLastArg(CmdArgs, options::OPT_flat__namespace);
Daniel Dunbar99ca47b2011-06-28 20:16:02 +00005720 Args.AddAllArgs(CmdArgs, options::OPT_force__load);
Daniel Dunbar02633b52009-03-26 16:23:12 +00005721 Args.AddAllArgs(CmdArgs, options::OPT_headerpad__max__install__names);
5722 Args.AddAllArgs(CmdArgs, options::OPT_image__base);
5723 Args.AddAllArgs(CmdArgs, options::OPT_init);
5724
Daniel Dunbarce911f52011-04-28 21:23:41 +00005725 // Add the deployment target.
Stephen Hines651f13c2014-04-23 16:59:28 -07005726 MachOTC.addMinVersionArgs(Args, CmdArgs);
Daniel Dunbarce911f52011-04-28 21:23:41 +00005727
Daniel Dunbar02633b52009-03-26 16:23:12 +00005728 Args.AddLastArg(CmdArgs, options::OPT_nomultidefs);
5729 Args.AddLastArg(CmdArgs, options::OPT_multi__module);
5730 Args.AddLastArg(CmdArgs, options::OPT_single__module);
5731 Args.AddAllArgs(CmdArgs, options::OPT_multiply__defined);
5732 Args.AddAllArgs(CmdArgs, options::OPT_multiply__defined__unused);
Daniel Dunbarc21c4852009-04-08 23:54:23 +00005733
Daniel Dunbar47e879d2010-07-13 23:31:40 +00005734 if (const Arg *A = Args.getLastArg(options::OPT_fpie, options::OPT_fPIE,
5735 options::OPT_fno_pie,
5736 options::OPT_fno_PIE)) {
5737 if (A->getOption().matches(options::OPT_fpie) ||
5738 A->getOption().matches(options::OPT_fPIE))
5739 CmdArgs.push_back("-pie");
5740 else
5741 CmdArgs.push_back("-no_pie");
5742 }
Daniel Dunbar02633b52009-03-26 16:23:12 +00005743
5744 Args.AddLastArg(CmdArgs, options::OPT_prebind);
5745 Args.AddLastArg(CmdArgs, options::OPT_noprebind);
5746 Args.AddLastArg(CmdArgs, options::OPT_nofixprebinding);
5747 Args.AddLastArg(CmdArgs, options::OPT_prebind__all__twolevel__modules);
5748 Args.AddLastArg(CmdArgs, options::OPT_read__only__relocs);
5749 Args.AddAllArgs(CmdArgs, options::OPT_sectcreate);
5750 Args.AddAllArgs(CmdArgs, options::OPT_sectorder);
5751 Args.AddAllArgs(CmdArgs, options::OPT_seg1addr);
5752 Args.AddAllArgs(CmdArgs, options::OPT_segprot);
5753 Args.AddAllArgs(CmdArgs, options::OPT_segaddr);
5754 Args.AddAllArgs(CmdArgs, options::OPT_segs__read__only__addr);
5755 Args.AddAllArgs(CmdArgs, options::OPT_segs__read__write__addr);
5756 Args.AddAllArgs(CmdArgs, options::OPT_seg__addr__table);
5757 Args.AddAllArgs(CmdArgs, options::OPT_seg__addr__table__filename);
5758 Args.AddAllArgs(CmdArgs, options::OPT_sub__library);
5759 Args.AddAllArgs(CmdArgs, options::OPT_sub__umbrella);
Daniel Dunbard82f8fa2009-09-04 18:35:41 +00005760
Daniel Dunbarcc957192011-05-02 21:03:47 +00005761 // Give --sysroot= preference, over the Apple specific behavior to also use
5762 // --isysroot as the syslibroot.
Sebastian Pop4762a2d2012-04-16 04:16:43 +00005763 StringRef sysroot = C.getSysRoot();
5764 if (sysroot != "") {
Daniel Dunbarcc957192011-05-02 21:03:47 +00005765 CmdArgs.push_back("-syslibroot");
Sebastian Pop4762a2d2012-04-16 04:16:43 +00005766 CmdArgs.push_back(C.getArgs().MakeArgString(sysroot));
Daniel Dunbarcc957192011-05-02 21:03:47 +00005767 } else if (const Arg *A = Args.getLastArg(options::OPT_isysroot)) {
5768 CmdArgs.push_back("-syslibroot");
Richard Smith1d489cf2012-11-01 04:30:05 +00005769 CmdArgs.push_back(A->getValue());
Daniel Dunbard82f8fa2009-09-04 18:35:41 +00005770 }
5771
Daniel Dunbar02633b52009-03-26 16:23:12 +00005772 Args.AddLastArg(CmdArgs, options::OPT_twolevel__namespace);
5773 Args.AddLastArg(CmdArgs, options::OPT_twolevel__namespace__hints);
5774 Args.AddAllArgs(CmdArgs, options::OPT_umbrella);
5775 Args.AddAllArgs(CmdArgs, options::OPT_undefined);
5776 Args.AddAllArgs(CmdArgs, options::OPT_unexported__symbols__list);
Daniel Dunbard82f8fa2009-09-04 18:35:41 +00005777 Args.AddAllArgs(CmdArgs, options::OPT_weak__reference__mismatches);
Daniel Dunbar02633b52009-03-26 16:23:12 +00005778 Args.AddLastArg(CmdArgs, options::OPT_X_Flag);
5779 Args.AddAllArgs(CmdArgs, options::OPT_y);
5780 Args.AddLastArg(CmdArgs, options::OPT_w);
5781 Args.AddAllArgs(CmdArgs, options::OPT_pagezero__size);
5782 Args.AddAllArgs(CmdArgs, options::OPT_segs__read__);
5783 Args.AddLastArg(CmdArgs, options::OPT_seglinkedit);
5784 Args.AddLastArg(CmdArgs, options::OPT_noseglinkedit);
5785 Args.AddAllArgs(CmdArgs, options::OPT_sectalign);
5786 Args.AddAllArgs(CmdArgs, options::OPT_sectobjectsymbols);
5787 Args.AddAllArgs(CmdArgs, options::OPT_segcreate);
5788 Args.AddLastArg(CmdArgs, options::OPT_whyload);
5789 Args.AddLastArg(CmdArgs, options::OPT_whatsloaded);
5790 Args.AddAllArgs(CmdArgs, options::OPT_dylinker__install__name);
5791 Args.AddLastArg(CmdArgs, options::OPT_dylinker);
5792 Args.AddLastArg(CmdArgs, options::OPT_Mach);
5793}
5794
Stephen Hines651f13c2014-04-23 16:59:28 -07005795enum LibOpenMP {
5796 LibUnknown,
5797 LibGOMP,
5798 LibIOMP5
5799};
5800
Daniel Dunbar02633b52009-03-26 16:23:12 +00005801void darwin::Link::ConstructJob(Compilation &C, const JobAction &JA,
Daniel Dunbar2fe238e2010-08-02 02:38:28 +00005802 const InputInfo &Output,
Daniel Dunbarc21c4852009-04-08 23:54:23 +00005803 const InputInfoList &Inputs,
5804 const ArgList &Args,
Daniel Dunbar02633b52009-03-26 16:23:12 +00005805 const char *LinkingOutput) const {
5806 assert(Output.getType() == types::TY_Image && "Invalid linker output type.");
Daniel Dunbare0be8b12009-09-08 16:39:16 +00005807
Stephen Hines176edba2014-12-01 14:53:08 -08005808 // If the number of arguments surpasses the system limits, we will encode the
5809 // input files in a separate file, shortening the command line. To this end,
5810 // build a list of input file names that can be passed via a file with the
5811 // -filelist linker option.
5812 llvm::opt::ArgStringList InputFileList;
5813
Daniel Dunbar02633b52009-03-26 16:23:12 +00005814 // The logic here is derived from gcc's behavior; most of which
5815 // comes from specs (starting with link_command). Consult gcc for
5816 // more information.
Daniel Dunbar02633b52009-03-26 16:23:12 +00005817 ArgStringList CmdArgs;
5818
Argyrios Kyrtzidis22897172011-10-07 22:58:08 +00005819 /// Hack(tm) to ignore linking errors when we are doing ARC migration.
5820 if (Args.hasArg(options::OPT_ccc_arcmt_check,
5821 options::OPT_ccc_arcmt_migrate)) {
Stephen Hinesc568f1e2014-07-21 00:47:37 -07005822 for (const auto &Arg : Args)
5823 Arg->claim();
Argyrios Kyrtzidis22897172011-10-07 22:58:08 +00005824 const char *Exec =
5825 Args.MakeArgString(getToolChain().GetProgramPath("touch"));
5826 CmdArgs.push_back(Output.getFilename());
Stephen Hines176edba2014-12-01 14:53:08 -08005827 C.addCommand(llvm::make_unique<Command>(JA, *this, Exec, CmdArgs));
Argyrios Kyrtzidis22897172011-10-07 22:58:08 +00005828 return;
5829 }
5830
Daniel Dunbar02633b52009-03-26 16:23:12 +00005831 // I'm not sure why this particular decomposition exists in gcc, but
5832 // we follow suite for ease of comparison.
Bill Wendling6acf8b42012-10-02 18:02:50 +00005833 AddLinkArgs(C, Args, CmdArgs, Inputs);
Daniel Dunbar02633b52009-03-26 16:23:12 +00005834
Daniel Dunbar02633b52009-03-26 16:23:12 +00005835 Args.AddAllArgs(CmdArgs, options::OPT_d_Flag);
5836 Args.AddAllArgs(CmdArgs, options::OPT_s);
5837 Args.AddAllArgs(CmdArgs, options::OPT_t);
5838 Args.AddAllArgs(CmdArgs, options::OPT_Z_Flag);
5839 Args.AddAllArgs(CmdArgs, options::OPT_u_Group);
Daniel Dunbar02633b52009-03-26 16:23:12 +00005840 Args.AddLastArg(CmdArgs, options::OPT_e);
Daniel Dunbar02633b52009-03-26 16:23:12 +00005841 Args.AddAllArgs(CmdArgs, options::OPT_r);
5842
Daniel Dunbar270073c2010-10-18 22:08:36 +00005843 // Forward -ObjC when either -ObjC or -ObjC++ is used, to force loading
5844 // members of static archive libraries which implement Objective-C classes or
5845 // categories.
5846 if (Args.hasArg(options::OPT_ObjC) || Args.hasArg(options::OPT_ObjCXX))
5847 CmdArgs.push_back("-ObjC");
Michael J. Spencer20249a12010-10-21 03:16:25 +00005848
Daniel Dunbar02633b52009-03-26 16:23:12 +00005849 CmdArgs.push_back("-o");
5850 CmdArgs.push_back(Output.getFilename());
5851
Chad Rosier18937312012-05-16 23:45:12 +00005852 if (!Args.hasArg(options::OPT_nostdlib) &&
Stephen Hines651f13c2014-04-23 16:59:28 -07005853 !Args.hasArg(options::OPT_nostartfiles))
5854 getMachOToolChain().addStartObjectFileArgs(Args, CmdArgs);
Daniel Dunbar02633b52009-03-26 16:23:12 +00005855
5856 Args.AddAllArgs(CmdArgs, options::OPT_L);
Daniel Dunbarc21c4852009-04-08 23:54:23 +00005857
Stephen Hines651f13c2014-04-23 16:59:28 -07005858 LibOpenMP UsedOpenMPLib = LibUnknown;
5859 if (Args.hasArg(options::OPT_fopenmp)) {
5860 UsedOpenMPLib = LibGOMP;
5861 } else if (const Arg *A = Args.getLastArg(options::OPT_fopenmp_EQ)) {
5862 UsedOpenMPLib = llvm::StringSwitch<LibOpenMP>(A->getValue())
5863 .Case("libgomp", LibGOMP)
5864 .Case("libiomp5", LibIOMP5)
5865 .Default(LibUnknown);
5866 if (UsedOpenMPLib == LibUnknown)
5867 getToolChain().getDriver().Diag(diag::err_drv_unsupported_option_argument)
5868 << A->getOption().getName() << A->getValue();
5869 }
5870 switch (UsedOpenMPLib) {
5871 case LibGOMP:
Daniel Dunbar02633b52009-03-26 16:23:12 +00005872 CmdArgs.push_back("-lgomp");
Stephen Hines651f13c2014-04-23 16:59:28 -07005873 break;
5874 case LibIOMP5:
5875 CmdArgs.push_back("-liomp5");
5876 break;
5877 case LibUnknown:
5878 break;
5879 }
Daniel Dunbar02633b52009-03-26 16:23:12 +00005880
Douglas Gregor04e326b2012-05-15 21:00:27 +00005881 AddLinkerInputs(getToolChain(), Inputs, Args, CmdArgs);
Stephen Hines176edba2014-12-01 14:53:08 -08005882 // Build the input file for -filelist (list of linker input files) in case we
5883 // need it later
5884 for (const auto &II : Inputs) {
5885 if (!II.isFilename()) {
5886 // This is a linker input argument.
5887 // We cannot mix input arguments and file names in a -filelist input, thus
5888 // we prematurely stop our list (remaining files shall be passed as
5889 // arguments).
5890 if (InputFileList.size() > 0)
5891 break;
5892
5893 continue;
5894 }
5895
5896 InputFileList.push_back(II.getFilename());
5897 }
5898
Bob Wilson63d9f3c2012-05-15 18:57:39 +00005899 if (isObjCRuntimeLinked(Args) &&
5900 !Args.hasArg(options::OPT_nostdlib) &&
5901 !Args.hasArg(options::OPT_nodefaultlibs)) {
Stephen Hines651f13c2014-04-23 16:59:28 -07005902 // We use arclite library for both ARC and subscripting support.
5903 getMachOToolChain().AddLinkARCArgs(Args, CmdArgs);
5904
Bob Wilson0b1c7152012-04-21 00:21:42 +00005905 CmdArgs.push_back("-framework");
5906 CmdArgs.push_back("Foundation");
Ted Kremenekebcb57a2012-03-06 20:05:56 +00005907 // Link libobj.
5908 CmdArgs.push_back("-lobjc");
John McCall9f084a32011-07-06 00:26:06 +00005909 }
John McCallf85e1932011-06-15 23:02:42 +00005910
Daniel Dunbar02633b52009-03-26 16:23:12 +00005911 if (LinkingOutput) {
5912 CmdArgs.push_back("-arch_multiple");
5913 CmdArgs.push_back("-final_output");
5914 CmdArgs.push_back(LinkingOutput);
5915 }
5916
Daniel Dunbar02633b52009-03-26 16:23:12 +00005917 if (Args.hasArg(options::OPT_fnested_functions))
5918 CmdArgs.push_back("-allow_stack_execute");
Daniel Dunbarc21c4852009-04-08 23:54:23 +00005919
Daniel Dunbar02633b52009-03-26 16:23:12 +00005920 if (!Args.hasArg(options::OPT_nostdlib) &&
5921 !Args.hasArg(options::OPT_nodefaultlibs)) {
Hans Wennborg76b86c22013-07-18 20:29:38 +00005922 if (getToolChain().getDriver().CCCIsCXX())
Daniel Dunbar132e35d2010-09-17 01:20:05 +00005923 getToolChain().AddCXXStdlibLibArgs(Args, CmdArgs);
Daniel Dunbaredfa02b2009-04-08 06:06:21 +00005924
Daniel Dunbar02633b52009-03-26 16:23:12 +00005925 // link_ssp spec is empty.
5926
Daniel Dunbar6cd41542009-09-18 08:15:03 +00005927 // Let the tool chain choose which runtime library to link.
Stephen Hines651f13c2014-04-23 16:59:28 -07005928 getMachOToolChain().AddLinkRuntimeLibArgs(Args, CmdArgs);
Daniel Dunbar02633b52009-03-26 16:23:12 +00005929 }
5930
Chad Rosier18937312012-05-16 23:45:12 +00005931 if (!Args.hasArg(options::OPT_nostdlib) &&
Daniel Dunbar02633b52009-03-26 16:23:12 +00005932 !Args.hasArg(options::OPT_nostartfiles)) {
5933 // endfile_spec is empty.
5934 }
5935
5936 Args.AddAllArgs(CmdArgs, options::OPT_T_Group);
5937 Args.AddAllArgs(CmdArgs, options::OPT_F);
5938
Daniel Dunbarc21c4852009-04-08 23:54:23 +00005939 const char *Exec =
Stephen Hinesc568f1e2014-07-21 00:47:37 -07005940 Args.MakeArgString(getToolChain().GetLinkerPath());
Stephen Hines176edba2014-12-01 14:53:08 -08005941 std::unique_ptr<Command> Cmd =
5942 llvm::make_unique<Command>(JA, *this, Exec, CmdArgs);
5943 Cmd->setInputFileList(std::move(InputFileList));
5944 C.addCommand(std::move(Cmd));
Daniel Dunbar02633b52009-03-26 16:23:12 +00005945}
5946
Daniel Dunbarff7488d2009-03-20 00:52:38 +00005947void darwin::Lipo::ConstructJob(Compilation &C, const JobAction &JA,
Daniel Dunbar2fe238e2010-08-02 02:38:28 +00005948 const InputInfo &Output,
Daniel Dunbarc21c4852009-04-08 23:54:23 +00005949 const InputInfoList &Inputs,
5950 const ArgList &Args,
Daniel Dunbarff7488d2009-03-20 00:52:38 +00005951 const char *LinkingOutput) const {
5952 ArgStringList CmdArgs;
5953
5954 CmdArgs.push_back("-create");
5955 assert(Output.isFilename() && "Unexpected lipo output.");
Daniel Dunbara428df82009-03-24 00:24:37 +00005956
5957 CmdArgs.push_back("-output");
Daniel Dunbarff7488d2009-03-20 00:52:38 +00005958 CmdArgs.push_back(Output.getFilename());
Daniel Dunbara428df82009-03-24 00:24:37 +00005959
Stephen Hinesc568f1e2014-07-21 00:47:37 -07005960 for (const auto &II : Inputs) {
Daniel Dunbarff7488d2009-03-20 00:52:38 +00005961 assert(II.isFilename() && "Unexpected lipo input.");
5962 CmdArgs.push_back(II.getFilename());
5963 }
Stephen Hinesc568f1e2014-07-21 00:47:37 -07005964
5965 const char *Exec = Args.MakeArgString(getToolChain().GetProgramPath("lipo"));
Stephen Hines176edba2014-12-01 14:53:08 -08005966 C.addCommand(llvm::make_unique<Command>(JA, *this, Exec, CmdArgs));
Daniel Dunbarff7488d2009-03-20 00:52:38 +00005967}
Daniel Dunbar68a31d42009-03-31 17:45:15 +00005968
Daniel Dunbar6e0f2542010-06-04 18:28:36 +00005969void darwin::Dsymutil::ConstructJob(Compilation &C, const JobAction &JA,
Daniel Dunbar2fe238e2010-08-02 02:38:28 +00005970 const InputInfo &Output,
Daniel Dunbar6e0f2542010-06-04 18:28:36 +00005971 const InputInfoList &Inputs,
5972 const ArgList &Args,
5973 const char *LinkingOutput) const {
5974 ArgStringList CmdArgs;
5975
Daniel Dunbar03e92302011-05-09 17:23:16 +00005976 CmdArgs.push_back("-o");
5977 CmdArgs.push_back(Output.getFilename());
5978
Daniel Dunbar6e0f2542010-06-04 18:28:36 +00005979 assert(Inputs.size() == 1 && "Unable to handle multiple inputs.");
5980 const InputInfo &Input = Inputs[0];
5981 assert(Input.isFilename() && "Unexpected dsymutil input.");
5982 CmdArgs.push_back(Input.getFilename());
5983
Daniel Dunbar6e0f2542010-06-04 18:28:36 +00005984 const char *Exec =
Daniel Dunbar4a7e8892010-07-14 18:46:23 +00005985 Args.MakeArgString(getToolChain().GetProgramPath("dsymutil"));
Stephen Hines176edba2014-12-01 14:53:08 -08005986 C.addCommand(llvm::make_unique<Command>(JA, *this, Exec, CmdArgs));
Daniel Dunbar6e0f2542010-06-04 18:28:36 +00005987}
5988
Eric Christopherf8571862011-08-23 17:56:55 +00005989void darwin::VerifyDebug::ConstructJob(Compilation &C, const JobAction &JA,
Eric Christopher27e2b982012-12-18 00:31:10 +00005990 const InputInfo &Output,
5991 const InputInfoList &Inputs,
5992 const ArgList &Args,
5993 const char *LinkingOutput) const {
Eric Christopherf8571862011-08-23 17:56:55 +00005994 ArgStringList CmdArgs;
5995 CmdArgs.push_back("--verify");
Eric Christopher1c79dc42012-02-06 19:13:09 +00005996 CmdArgs.push_back("--debug-info");
5997 CmdArgs.push_back("--eh-frame");
Eric Christopherb822f722012-02-06 19:43:51 +00005998 CmdArgs.push_back("--quiet");
Eric Christopherf8571862011-08-23 17:56:55 +00005999
6000 assert(Inputs.size() == 1 && "Unable to handle multiple inputs.");
6001 const InputInfo &Input = Inputs[0];
6002 assert(Input.isFilename() && "Unexpected verify input");
6003
6004 // Grabbing the output of the earlier dsymutil run.
6005 CmdArgs.push_back(Input.getFilename());
6006
6007 const char *Exec =
6008 Args.MakeArgString(getToolChain().GetProgramPath("dwarfdump"));
Stephen Hines176edba2014-12-01 14:53:08 -08006009 C.addCommand(llvm::make_unique<Command>(JA, *this, Exec, CmdArgs));
Eric Christopherf8571862011-08-23 17:56:55 +00006010}
6011
David Chisnall31c46902012-02-15 13:39:01 +00006012void solaris::Assemble::ConstructJob(Compilation &C, const JobAction &JA,
6013 const InputInfo &Output,
6014 const InputInfoList &Inputs,
6015 const ArgList &Args,
6016 const char *LinkingOutput) const {
6017 ArgStringList CmdArgs;
6018
6019 Args.AddAllArgValues(CmdArgs, options::OPT_Wa_COMMA,
6020 options::OPT_Xassembler);
6021
6022 CmdArgs.push_back("-o");
6023 CmdArgs.push_back(Output.getFilename());
6024
Stephen Hinesc568f1e2014-07-21 00:47:37 -07006025 for (const auto &II : Inputs)
David Chisnall31c46902012-02-15 13:39:01 +00006026 CmdArgs.push_back(II.getFilename());
David Chisnall31c46902012-02-15 13:39:01 +00006027
Stephen Hinesc568f1e2014-07-21 00:47:37 -07006028 const char *Exec = Args.MakeArgString(getToolChain().GetProgramPath("as"));
Stephen Hines176edba2014-12-01 14:53:08 -08006029 C.addCommand(llvm::make_unique<Command>(JA, *this, Exec, CmdArgs));
David Chisnall31c46902012-02-15 13:39:01 +00006030}
6031
David Chisnall31c46902012-02-15 13:39:01 +00006032void solaris::Link::ConstructJob(Compilation &C, const JobAction &JA,
6033 const InputInfo &Output,
6034 const InputInfoList &Inputs,
6035 const ArgList &Args,
6036 const char *LinkingOutput) const {
6037 // FIXME: Find a real GCC, don't hard-code versions here
6038 std::string GCCLibPath = "/usr/gcc/4.5/lib/gcc/";
6039 const llvm::Triple &T = getToolChain().getTriple();
6040 std::string LibPath = "/usr/lib/";
6041 llvm::Triple::ArchType Arch = T.getArch();
6042 switch (Arch) {
Benjamin Kramer39e4b0f2013-10-21 12:33:55 +00006043 case llvm::Triple::x86:
6044 GCCLibPath +=
6045 ("i386-" + T.getVendorName() + "-" + T.getOSName()).str() + "/4.5.2/";
6046 break;
6047 case llvm::Triple::x86_64:
6048 GCCLibPath += ("i386-" + T.getVendorName() + "-" + T.getOSName()).str();
6049 GCCLibPath += "/4.5.2/amd64/";
6050 LibPath += "amd64/";
6051 break;
6052 default:
6053 llvm_unreachable("Unsupported architecture");
David Chisnall31c46902012-02-15 13:39:01 +00006054 }
6055
6056 ArgStringList CmdArgs;
6057
David Chisnall41d476d2012-02-29 15:06:12 +00006058 // Demangle C++ names in errors
6059 CmdArgs.push_back("-C");
6060
David Chisnall31c46902012-02-15 13:39:01 +00006061 if ((!Args.hasArg(options::OPT_nostdlib)) &&
6062 (!Args.hasArg(options::OPT_shared))) {
6063 CmdArgs.push_back("-e");
6064 CmdArgs.push_back("_start");
6065 }
6066
6067 if (Args.hasArg(options::OPT_static)) {
6068 CmdArgs.push_back("-Bstatic");
6069 CmdArgs.push_back("-dn");
6070 } else {
6071 CmdArgs.push_back("-Bdynamic");
6072 if (Args.hasArg(options::OPT_shared)) {
6073 CmdArgs.push_back("-shared");
6074 } else {
6075 CmdArgs.push_back("--dynamic-linker");
6076 CmdArgs.push_back(Args.MakeArgString(LibPath + "ld.so.1"));
6077 }
6078 }
6079
6080 if (Output.isFilename()) {
6081 CmdArgs.push_back("-o");
6082 CmdArgs.push_back(Output.getFilename());
6083 } else {
6084 assert(Output.isNothing() && "Invalid output.");
6085 }
6086
6087 if (!Args.hasArg(options::OPT_nostdlib) &&
6088 !Args.hasArg(options::OPT_nostartfiles)) {
6089 if (!Args.hasArg(options::OPT_shared)) {
6090 CmdArgs.push_back(Args.MakeArgString(LibPath + "crt1.o"));
6091 CmdArgs.push_back(Args.MakeArgString(LibPath + "crti.o"));
David Chisnall165329c2012-02-28 17:10:04 +00006092 CmdArgs.push_back(Args.MakeArgString(LibPath + "values-Xa.o"));
David Chisnall31c46902012-02-15 13:39:01 +00006093 CmdArgs.push_back(Args.MakeArgString(GCCLibPath + "crtbegin.o"));
6094 } else {
6095 CmdArgs.push_back(Args.MakeArgString(LibPath + "crti.o"));
David Chisnall165329c2012-02-28 17:10:04 +00006096 CmdArgs.push_back(Args.MakeArgString(LibPath + "values-Xa.o"));
6097 CmdArgs.push_back(Args.MakeArgString(GCCLibPath + "crtbegin.o"));
David Chisnall31c46902012-02-15 13:39:01 +00006098 }
Hans Wennborg76b86c22013-07-18 20:29:38 +00006099 if (getToolChain().getDriver().CCCIsCXX())
David Chisnalle6dd6832012-03-13 14:14:54 +00006100 CmdArgs.push_back(Args.MakeArgString(LibPath + "cxa_finalize.o"));
David Chisnall31c46902012-02-15 13:39:01 +00006101 }
6102
6103 CmdArgs.push_back(Args.MakeArgString("-L" + GCCLibPath));
6104
6105 Args.AddAllArgs(CmdArgs, options::OPT_L);
6106 Args.AddAllArgs(CmdArgs, options::OPT_T_Group);
6107 Args.AddAllArgs(CmdArgs, options::OPT_e);
David Chisnall165329c2012-02-28 17:10:04 +00006108 Args.AddAllArgs(CmdArgs, options::OPT_r);
David Chisnall31c46902012-02-15 13:39:01 +00006109
6110 AddLinkerInputs(getToolChain(), Inputs, Args, CmdArgs);
6111
6112 if (!Args.hasArg(options::OPT_nostdlib) &&
6113 !Args.hasArg(options::OPT_nodefaultlibs)) {
Hans Wennborg76b86c22013-07-18 20:29:38 +00006114 if (getToolChain().getDriver().CCCIsCXX())
David Chisnalle58e6f92012-04-10 11:49:50 +00006115 getToolChain().AddCXXStdlibLibArgs(Args, CmdArgs);
David Chisnallb6229592012-02-15 18:24:31 +00006116 CmdArgs.push_back("-lgcc_s");
David Chisnall165329c2012-02-28 17:10:04 +00006117 if (!Args.hasArg(options::OPT_shared)) {
6118 CmdArgs.push_back("-lgcc");
David Chisnall31c46902012-02-15 13:39:01 +00006119 CmdArgs.push_back("-lc");
David Chisnall7dbefe12012-02-28 20:06:45 +00006120 CmdArgs.push_back("-lm");
David Chisnall165329c2012-02-28 17:10:04 +00006121 }
David Chisnall31c46902012-02-15 13:39:01 +00006122 }
6123
6124 if (!Args.hasArg(options::OPT_nostdlib) &&
6125 !Args.hasArg(options::OPT_nostartfiles)) {
David Chisnall165329c2012-02-28 17:10:04 +00006126 CmdArgs.push_back(Args.MakeArgString(GCCLibPath + "crtend.o"));
David Chisnall31c46902012-02-15 13:39:01 +00006127 }
David Chisnalld1ac03e2012-02-16 16:00:47 +00006128 CmdArgs.push_back(Args.MakeArgString(LibPath + "crtn.o"));
David Chisnall31c46902012-02-15 13:39:01 +00006129
Stephen Hines651f13c2014-04-23 16:59:28 -07006130 addProfileRT(getToolChain(), Args, CmdArgs);
David Chisnall31c46902012-02-15 13:39:01 +00006131
6132 const char *Exec =
Stephen Hinesc568f1e2014-07-21 00:47:37 -07006133 Args.MakeArgString(getToolChain().GetLinkerPath());
Stephen Hines176edba2014-12-01 14:53:08 -08006134 C.addCommand(llvm::make_unique<Command>(JA, *this, Exec, CmdArgs));
Edward O'Callaghane7925a02009-08-22 01:06:46 +00006135}
6136
Daniel Dunbarf7b8eec2009-06-29 20:52:51 +00006137void openbsd::Assemble::ConstructJob(Compilation &C, const JobAction &JA,
Daniel Dunbar2fe238e2010-08-02 02:38:28 +00006138 const InputInfo &Output,
Daniel Dunbarf7b8eec2009-06-29 20:52:51 +00006139 const InputInfoList &Inputs,
6140 const ArgList &Args,
Mike Stump1eb44332009-09-09 15:08:12 +00006141 const char *LinkingOutput) const {
Daniel Dunbarf7b8eec2009-06-29 20:52:51 +00006142 ArgStringList CmdArgs;
Stephen Hines651f13c2014-04-23 16:59:28 -07006143 bool NeedsKPIC = false;
Daniel Dunbarf7b8eec2009-06-29 20:52:51 +00006144
Stephen Hines651f13c2014-04-23 16:59:28 -07006145 switch (getToolChain().getArch()) {
6146 case llvm::Triple::x86:
6147 // When building 32-bit code on OpenBSD/amd64, we have to explicitly
6148 // instruct as in the base system to assemble 32-bit code.
Bill Wendlingac66cb82013-12-08 00:21:01 +00006149 CmdArgs.push_back("--32");
Stephen Hines651f13c2014-04-23 16:59:28 -07006150 break;
6151
6152 case llvm::Triple::ppc:
Bill Wendlingac66cb82013-12-08 00:21:01 +00006153 CmdArgs.push_back("-mppc");
6154 CmdArgs.push_back("-many");
Stephen Hines651f13c2014-04-23 16:59:28 -07006155 break;
6156
6157 case llvm::Triple::sparc:
6158 CmdArgs.push_back("-32");
6159 NeedsKPIC = true;
6160 break;
6161
6162 case llvm::Triple::sparcv9:
6163 CmdArgs.push_back("-64");
6164 CmdArgs.push_back("-Av9a");
6165 NeedsKPIC = true;
6166 break;
6167
6168 case llvm::Triple::mips64:
6169 case llvm::Triple::mips64el: {
Bill Wendlingac66cb82013-12-08 00:21:01 +00006170 StringRef CPUName;
6171 StringRef ABIName;
Stephen Hines176edba2014-12-01 14:53:08 -08006172 mips::getMipsCPUAndABI(Args, getToolChain().getTriple(), CPUName, ABIName);
Bill Wendlingac66cb82013-12-08 00:21:01 +00006173
6174 CmdArgs.push_back("-mabi");
6175 CmdArgs.push_back(getGnuCompatibleMipsABIName(ABIName).data());
6176
6177 if (getToolChain().getArch() == llvm::Triple::mips64)
6178 CmdArgs.push_back("-EB");
6179 else
6180 CmdArgs.push_back("-EL");
6181
Stephen Hines651f13c2014-04-23 16:59:28 -07006182 NeedsKPIC = true;
6183 break;
Bill Wendlingac66cb82013-12-08 00:21:01 +00006184 }
6185
Stephen Hines651f13c2014-04-23 16:59:28 -07006186 default:
6187 break;
6188 }
6189
6190 if (NeedsKPIC)
6191 addAssemblerKPIC(Args, CmdArgs);
6192
Daniel Dunbarf7b8eec2009-06-29 20:52:51 +00006193 Args.AddAllArgValues(CmdArgs, options::OPT_Wa_COMMA,
6194 options::OPT_Xassembler);
6195
6196 CmdArgs.push_back("-o");
Daniel Dunbar7c1e4652010-08-02 02:38:21 +00006197 CmdArgs.push_back(Output.getFilename());
Daniel Dunbarf7b8eec2009-06-29 20:52:51 +00006198
Stephen Hinesc568f1e2014-07-21 00:47:37 -07006199 for (const auto &II : Inputs)
Daniel Dunbar7c1e4652010-08-02 02:38:21 +00006200 CmdArgs.push_back(II.getFilename());
Daniel Dunbarf7b8eec2009-06-29 20:52:51 +00006201
6202 const char *Exec =
Daniel Dunbar4a7e8892010-07-14 18:46:23 +00006203 Args.MakeArgString(getToolChain().GetProgramPath("as"));
Stephen Hines176edba2014-12-01 14:53:08 -08006204 C.addCommand(llvm::make_unique<Command>(JA, *this, Exec, CmdArgs));
Daniel Dunbarf7b8eec2009-06-29 20:52:51 +00006205}
6206
6207void openbsd::Link::ConstructJob(Compilation &C, const JobAction &JA,
Daniel Dunbar2fe238e2010-08-02 02:38:28 +00006208 const InputInfo &Output,
Daniel Dunbarf7b8eec2009-06-29 20:52:51 +00006209 const InputInfoList &Inputs,
6210 const ArgList &Args,
6211 const char *LinkingOutput) const {
Daniel Dunbaree788e72009-12-21 18:54:17 +00006212 const Driver &D = getToolChain().getDriver();
Daniel Dunbarf7b8eec2009-06-29 20:52:51 +00006213 ArgStringList CmdArgs;
6214
Rafael Espindola6cc2a682012-12-31 22:41:36 +00006215 // Silence warning for "clang -g foo.o -o foo"
6216 Args.ClaimAllArgs(options::OPT_g_Group);
6217 // and "clang -emit-llvm foo.o -o foo"
6218 Args.ClaimAllArgs(options::OPT_emit_llvm);
6219 // and for "clang -w foo.o -o foo". Other warning options are already
6220 // handled somewhere else.
6221 Args.ClaimAllArgs(options::OPT_w);
6222
Bill Wendlingac66cb82013-12-08 00:21:01 +00006223 if (getToolChain().getArch() == llvm::Triple::mips64)
6224 CmdArgs.push_back("-EB");
6225 else if (getToolChain().getArch() == llvm::Triple::mips64el)
6226 CmdArgs.push_back("-EL");
6227
Daniel Dunbar2bbcf662009-08-03 01:28:59 +00006228 if ((!Args.hasArg(options::OPT_nostdlib)) &&
Daniel Dunbar294691e2009-11-04 06:24:38 +00006229 (!Args.hasArg(options::OPT_shared))) {
Daniel Dunbar2bbcf662009-08-03 01:28:59 +00006230 CmdArgs.push_back("-e");
6231 CmdArgs.push_back("__start");
6232 }
6233
Daniel Dunbarf7b8eec2009-06-29 20:52:51 +00006234 if (Args.hasArg(options::OPT_static)) {
6235 CmdArgs.push_back("-Bstatic");
6236 } else {
Rafael Espindola65ba55d2010-11-11 02:17:51 +00006237 if (Args.hasArg(options::OPT_rdynamic))
6238 CmdArgs.push_back("-export-dynamic");
Daniel Dunbarf7b8eec2009-06-29 20:52:51 +00006239 CmdArgs.push_back("--eh-frame-hdr");
Daniel Dunbar2bbcf662009-08-03 01:28:59 +00006240 CmdArgs.push_back("-Bdynamic");
Daniel Dunbarf7b8eec2009-06-29 20:52:51 +00006241 if (Args.hasArg(options::OPT_shared)) {
Daniel Dunbar2bbcf662009-08-03 01:28:59 +00006242 CmdArgs.push_back("-shared");
Daniel Dunbarf7b8eec2009-06-29 20:52:51 +00006243 } else {
6244 CmdArgs.push_back("-dynamic-linker");
6245 CmdArgs.push_back("/usr/libexec/ld.so");
6246 }
6247 }
6248
Rafael Espindola9adba392013-06-05 04:28:55 +00006249 if (Args.hasArg(options::OPT_nopie))
6250 CmdArgs.push_back("-nopie");
6251
Daniel Dunbar7c1e4652010-08-02 02:38:21 +00006252 if (Output.isFilename()) {
Daniel Dunbarf7b8eec2009-06-29 20:52:51 +00006253 CmdArgs.push_back("-o");
6254 CmdArgs.push_back(Output.getFilename());
6255 } else {
6256 assert(Output.isNothing() && "Invalid output.");
6257 }
6258
6259 if (!Args.hasArg(options::OPT_nostdlib) &&
6260 !Args.hasArg(options::OPT_nostartfiles)) {
6261 if (!Args.hasArg(options::OPT_shared)) {
Eli Friedman62d829a2011-12-15 02:15:56 +00006262 if (Args.hasArg(options::OPT_pg))
6263 CmdArgs.push_back(Args.MakeArgString(
6264 getToolChain().GetFilePath("gcrt0.o")));
6265 else
6266 CmdArgs.push_back(Args.MakeArgString(
6267 getToolChain().GetFilePath("crt0.o")));
Chris Lattner38e317d2010-07-07 16:01:42 +00006268 CmdArgs.push_back(Args.MakeArgString(
Daniel Dunbar4a7e8892010-07-14 18:46:23 +00006269 getToolChain().GetFilePath("crtbegin.o")));
Daniel Dunbarf7b8eec2009-06-29 20:52:51 +00006270 } else {
Chris Lattner38e317d2010-07-07 16:01:42 +00006271 CmdArgs.push_back(Args.MakeArgString(
Daniel Dunbar4a7e8892010-07-14 18:46:23 +00006272 getToolChain().GetFilePath("crtbeginS.o")));
Daniel Dunbarf7b8eec2009-06-29 20:52:51 +00006273 }
6274 }
6275
Edward O'Callaghane7e18202009-10-28 15:13:08 +00006276 std::string Triple = getToolChain().getTripleString();
6277 if (Triple.substr(0, 6) == "x86_64")
Daniel Dunbar294691e2009-11-04 06:24:38 +00006278 Triple.replace(0, 6, "amd64");
Daniel Dunbarf7fb31f2009-10-29 02:24:37 +00006279 CmdArgs.push_back(Args.MakeArgString("-L/usr/lib/gcc-lib/" + Triple +
Daniel Dunbar95c04572010-08-01 23:13:54 +00006280 "/4.2.1"));
Daniel Dunbar2bbcf662009-08-03 01:28:59 +00006281
Daniel Dunbarf7b8eec2009-06-29 20:52:51 +00006282 Args.AddAllArgs(CmdArgs, options::OPT_L);
6283 Args.AddAllArgs(CmdArgs, options::OPT_T_Group);
6284 Args.AddAllArgs(CmdArgs, options::OPT_e);
Rafael Espindola6cc2a682012-12-31 22:41:36 +00006285 Args.AddAllArgs(CmdArgs, options::OPT_s);
6286 Args.AddAllArgs(CmdArgs, options::OPT_t);
6287 Args.AddAllArgs(CmdArgs, options::OPT_Z_Flag);
6288 Args.AddAllArgs(CmdArgs, options::OPT_r);
Daniel Dunbarf7b8eec2009-06-29 20:52:51 +00006289
Daniel Dunbar2008fee2010-09-17 00:24:54 +00006290 AddLinkerInputs(getToolChain(), Inputs, Args, CmdArgs);
Daniel Dunbarf7b8eec2009-06-29 20:52:51 +00006291
6292 if (!Args.hasArg(options::OPT_nostdlib) &&
6293 !Args.hasArg(options::OPT_nodefaultlibs)) {
Hans Wennborg76b86c22013-07-18 20:29:38 +00006294 if (D.CCCIsCXX()) {
Daniel Dunbar132e35d2010-09-17 01:20:05 +00006295 getToolChain().AddCXXStdlibLibArgs(Args, CmdArgs);
Eli Friedman62d829a2011-12-15 02:15:56 +00006296 if (Args.hasArg(options::OPT_pg))
6297 CmdArgs.push_back("-lm_p");
6298 else
6299 CmdArgs.push_back("-lm");
Daniel Dunbar95c04572010-08-01 23:13:54 +00006300 }
6301
Daniel Dunbar2bbcf662009-08-03 01:28:59 +00006302 // FIXME: For some reason GCC passes -lgcc before adding
6303 // the default system libraries. Just mimic this for now.
6304 CmdArgs.push_back("-lgcc");
Daniel Dunbarf7b8eec2009-06-29 20:52:51 +00006305
Eric Christopherdc6cc872012-09-13 06:32:34 +00006306 if (Args.hasArg(options::OPT_pthread)) {
6307 if (!Args.hasArg(options::OPT_shared) &&
6308 Args.hasArg(options::OPT_pg))
6309 CmdArgs.push_back("-lpthread_p");
6310 else
6311 CmdArgs.push_back("-lpthread");
6312 }
6313
Chandler Carruth657849c2011-12-17 22:32:42 +00006314 if (!Args.hasArg(options::OPT_shared)) {
Eric Christopherdc6cc872012-09-13 06:32:34 +00006315 if (Args.hasArg(options::OPT_pg))
Eli Friedman62d829a2011-12-15 02:15:56 +00006316 CmdArgs.push_back("-lc_p");
6317 else
6318 CmdArgs.push_back("-lc");
Chandler Carruth657849c2011-12-17 22:32:42 +00006319 }
Eric Christopherdc6cc872012-09-13 06:32:34 +00006320
Daniel Dunbar2bbcf662009-08-03 01:28:59 +00006321 CmdArgs.push_back("-lgcc");
Daniel Dunbarf7b8eec2009-06-29 20:52:51 +00006322 }
6323
6324 if (!Args.hasArg(options::OPT_nostdlib) &&
6325 !Args.hasArg(options::OPT_nostartfiles)) {
6326 if (!Args.hasArg(options::OPT_shared))
Chris Lattner38e317d2010-07-07 16:01:42 +00006327 CmdArgs.push_back(Args.MakeArgString(
Daniel Dunbar4a7e8892010-07-14 18:46:23 +00006328 getToolChain().GetFilePath("crtend.o")));
Daniel Dunbarf7b8eec2009-06-29 20:52:51 +00006329 else
Chris Lattner38e317d2010-07-07 16:01:42 +00006330 CmdArgs.push_back(Args.MakeArgString(
Daniel Dunbar4a7e8892010-07-14 18:46:23 +00006331 getToolChain().GetFilePath("crtendS.o")));
Daniel Dunbarf7b8eec2009-06-29 20:52:51 +00006332 }
6333
6334 const char *Exec =
Stephen Hinesc568f1e2014-07-21 00:47:37 -07006335 Args.MakeArgString(getToolChain().GetLinkerPath());
Stephen Hines176edba2014-12-01 14:53:08 -08006336 C.addCommand(llvm::make_unique<Command>(JA, *this, Exec, CmdArgs));
Daniel Dunbarf7b8eec2009-06-29 20:52:51 +00006337}
Ed Schoutenc66a5a32009-04-02 19:13:12 +00006338
Eli Friedman42f74f22012-08-08 23:57:20 +00006339void bitrig::Assemble::ConstructJob(Compilation &C, const JobAction &JA,
6340 const InputInfo &Output,
6341 const InputInfoList &Inputs,
6342 const ArgList &Args,
6343 const char *LinkingOutput) const {
6344 ArgStringList CmdArgs;
6345
6346 Args.AddAllArgValues(CmdArgs, options::OPT_Wa_COMMA,
6347 options::OPT_Xassembler);
6348
6349 CmdArgs.push_back("-o");
6350 CmdArgs.push_back(Output.getFilename());
6351
Stephen Hinesc568f1e2014-07-21 00:47:37 -07006352 for (const auto &II : Inputs)
Eli Friedman42f74f22012-08-08 23:57:20 +00006353 CmdArgs.push_back(II.getFilename());
Eli Friedman42f74f22012-08-08 23:57:20 +00006354
Stephen Hinesc568f1e2014-07-21 00:47:37 -07006355 const char *Exec = Args.MakeArgString(getToolChain().GetProgramPath("as"));
Stephen Hines176edba2014-12-01 14:53:08 -08006356 C.addCommand(llvm::make_unique<Command>(JA, *this, Exec, CmdArgs));
Eli Friedman42f74f22012-08-08 23:57:20 +00006357}
6358
6359void bitrig::Link::ConstructJob(Compilation &C, const JobAction &JA,
6360 const InputInfo &Output,
6361 const InputInfoList &Inputs,
6362 const ArgList &Args,
6363 const char *LinkingOutput) const {
6364 const Driver &D = getToolChain().getDriver();
6365 ArgStringList CmdArgs;
6366
6367 if ((!Args.hasArg(options::OPT_nostdlib)) &&
6368 (!Args.hasArg(options::OPT_shared))) {
6369 CmdArgs.push_back("-e");
6370 CmdArgs.push_back("__start");
6371 }
6372
6373 if (Args.hasArg(options::OPT_static)) {
6374 CmdArgs.push_back("-Bstatic");
6375 } else {
6376 if (Args.hasArg(options::OPT_rdynamic))
6377 CmdArgs.push_back("-export-dynamic");
6378 CmdArgs.push_back("--eh-frame-hdr");
6379 CmdArgs.push_back("-Bdynamic");
6380 if (Args.hasArg(options::OPT_shared)) {
6381 CmdArgs.push_back("-shared");
6382 } else {
6383 CmdArgs.push_back("-dynamic-linker");
6384 CmdArgs.push_back("/usr/libexec/ld.so");
6385 }
6386 }
6387
6388 if (Output.isFilename()) {
6389 CmdArgs.push_back("-o");
6390 CmdArgs.push_back(Output.getFilename());
6391 } else {
6392 assert(Output.isNothing() && "Invalid output.");
6393 }
6394
6395 if (!Args.hasArg(options::OPT_nostdlib) &&
6396 !Args.hasArg(options::OPT_nostartfiles)) {
6397 if (!Args.hasArg(options::OPT_shared)) {
6398 if (Args.hasArg(options::OPT_pg))
6399 CmdArgs.push_back(Args.MakeArgString(
6400 getToolChain().GetFilePath("gcrt0.o")));
6401 else
6402 CmdArgs.push_back(Args.MakeArgString(
6403 getToolChain().GetFilePath("crt0.o")));
6404 CmdArgs.push_back(Args.MakeArgString(
6405 getToolChain().GetFilePath("crtbegin.o")));
6406 } else {
6407 CmdArgs.push_back(Args.MakeArgString(
6408 getToolChain().GetFilePath("crtbeginS.o")));
6409 }
6410 }
6411
6412 Args.AddAllArgs(CmdArgs, options::OPT_L);
6413 Args.AddAllArgs(CmdArgs, options::OPT_T_Group);
6414 Args.AddAllArgs(CmdArgs, options::OPT_e);
6415
6416 AddLinkerInputs(getToolChain(), Inputs, Args, CmdArgs);
6417
6418 if (!Args.hasArg(options::OPT_nostdlib) &&
6419 !Args.hasArg(options::OPT_nodefaultlibs)) {
Hans Wennborg76b86c22013-07-18 20:29:38 +00006420 if (D.CCCIsCXX()) {
Eli Friedman42f74f22012-08-08 23:57:20 +00006421 getToolChain().AddCXXStdlibLibArgs(Args, CmdArgs);
6422 if (Args.hasArg(options::OPT_pg))
6423 CmdArgs.push_back("-lm_p");
6424 else
6425 CmdArgs.push_back("-lm");
6426 }
6427
Rafael Espindola3667bbe2012-10-23 17:07:31 +00006428 if (Args.hasArg(options::OPT_pthread)) {
6429 if (!Args.hasArg(options::OPT_shared) &&
6430 Args.hasArg(options::OPT_pg))
6431 CmdArgs.push_back("-lpthread_p");
6432 else
6433 CmdArgs.push_back("-lpthread");
6434 }
6435
Eli Friedman42f74f22012-08-08 23:57:20 +00006436 if (!Args.hasArg(options::OPT_shared)) {
6437 if (Args.hasArg(options::OPT_pg))
6438 CmdArgs.push_back("-lc_p");
6439 else
6440 CmdArgs.push_back("-lc");
6441 }
6442
Benjamin Kramer39e4b0f2013-10-21 12:33:55 +00006443 StringRef MyArch;
6444 switch (getToolChain().getTriple().getArch()) {
6445 case llvm::Triple::arm:
6446 MyArch = "arm";
6447 break;
6448 case llvm::Triple::x86:
6449 MyArch = "i386";
6450 break;
6451 case llvm::Triple::x86_64:
6452 MyArch = "amd64";
6453 break;
6454 default:
6455 llvm_unreachable("Unsupported architecture");
6456 }
6457 CmdArgs.push_back(Args.MakeArgString("-lclang_rt." + MyArch));
Eli Friedman42f74f22012-08-08 23:57:20 +00006458 }
6459
6460 if (!Args.hasArg(options::OPT_nostdlib) &&
6461 !Args.hasArg(options::OPT_nostartfiles)) {
6462 if (!Args.hasArg(options::OPT_shared))
6463 CmdArgs.push_back(Args.MakeArgString(
6464 getToolChain().GetFilePath("crtend.o")));
6465 else
6466 CmdArgs.push_back(Args.MakeArgString(
6467 getToolChain().GetFilePath("crtendS.o")));
6468 }
Eli Friedmanc9c48db2012-08-09 22:42:04 +00006469
6470 const char *Exec =
Stephen Hinesc568f1e2014-07-21 00:47:37 -07006471 Args.MakeArgString(getToolChain().GetLinkerPath());
Stephen Hines176edba2014-12-01 14:53:08 -08006472 C.addCommand(llvm::make_unique<Command>(JA, *this, Exec, CmdArgs));
Eli Friedman42f74f22012-08-08 23:57:20 +00006473}
6474
Daniel Dunbar68a31d42009-03-31 17:45:15 +00006475void freebsd::Assemble::ConstructJob(Compilation &C, const JobAction &JA,
Daniel Dunbar2fe238e2010-08-02 02:38:28 +00006476 const InputInfo &Output,
Daniel Dunbarc21c4852009-04-08 23:54:23 +00006477 const InputInfoList &Inputs,
6478 const ArgList &Args,
Mike Stump1eb44332009-09-09 15:08:12 +00006479 const char *LinkingOutput) const {
Daniel Dunbar68a31d42009-03-31 17:45:15 +00006480 ArgStringList CmdArgs;
6481
Daniel Dunbar008f54a2009-04-01 19:36:32 +00006482 // When building 32-bit code on FreeBSD/amd64, we have to explicitly
6483 // instruct as in the base system to assemble 32-bit code.
Eric Christopherc55da4b2012-09-05 21:32:44 +00006484 if (getToolChain().getArch() == llvm::Triple::x86)
Daniel Dunbar68a31d42009-03-31 17:45:15 +00006485 CmdArgs.push_back("--32");
Eric Christopherc55da4b2012-09-05 21:32:44 +00006486 else if (getToolChain().getArch() == llvm::Triple::ppc)
Roman Divacky3393cef2011-06-04 07:37:31 +00006487 CmdArgs.push_back("-a32");
Eric Christopherc55da4b2012-09-05 21:32:44 +00006488 else if (getToolChain().getArch() == llvm::Triple::mips ||
6489 getToolChain().getArch() == llvm::Triple::mipsel ||
6490 getToolChain().getArch() == llvm::Triple::mips64 ||
6491 getToolChain().getArch() == llvm::Triple::mips64el) {
6492 StringRef CPUName;
6493 StringRef ABIName;
Stephen Hines176edba2014-12-01 14:53:08 -08006494 mips::getMipsCPUAndABI(Args, getToolChain().getTriple(), CPUName, ABIName);
Michael J. Spencer20249a12010-10-21 03:16:25 +00006495
Eric Christopherc55da4b2012-09-05 21:32:44 +00006496 CmdArgs.push_back("-march");
6497 CmdArgs.push_back(CPUName.data());
6498
Eric Christopherc55da4b2012-09-05 21:32:44 +00006499 CmdArgs.push_back("-mabi");
Simon Atanasyane9616a42013-02-27 14:55:49 +00006500 CmdArgs.push_back(getGnuCompatibleMipsABIName(ABIName).data());
Eric Christopherc55da4b2012-09-05 21:32:44 +00006501
6502 if (getToolChain().getArch() == llvm::Triple::mips ||
6503 getToolChain().getArch() == llvm::Triple::mips64)
6504 CmdArgs.push_back("-EB");
6505 else
6506 CmdArgs.push_back("-EL");
6507
Stephen Hines651f13c2014-04-23 16:59:28 -07006508 addAssemblerKPIC(Args, CmdArgs);
Rafael Espindola27fa2362012-12-13 04:17:14 +00006509 } else if (getToolChain().getArch() == llvm::Triple::arm ||
Stephen Hines651f13c2014-04-23 16:59:28 -07006510 getToolChain().getArch() == llvm::Triple::armeb ||
6511 getToolChain().getArch() == llvm::Triple::thumb ||
6512 getToolChain().getArch() == llvm::Triple::thumbeb) {
6513 const Driver &D = getToolChain().getDriver();
6514 const llvm::Triple &Triple = getToolChain().getTriple();
6515 StringRef FloatABI = arm::getARMFloatABI(D, Args, Triple);
6516
6517 if (FloatABI == "hard") {
6518 CmdArgs.push_back("-mfpu=vfp");
6519 } else {
6520 CmdArgs.push_back("-mfpu=softvfp");
6521 }
6522
Rafael Espindola27fa2362012-12-13 04:17:14 +00006523 switch(getToolChain().getTriple().getEnvironment()) {
Stephen Hines651f13c2014-04-23 16:59:28 -07006524 case llvm::Triple::GNUEABIHF:
Rafael Espindola27fa2362012-12-13 04:17:14 +00006525 case llvm::Triple::GNUEABI:
6526 case llvm::Triple::EABI:
Anton Korobeynikovb234e742013-03-18 07:59:20 +00006527 CmdArgs.push_back("-meabi=5");
Rafael Espindola27fa2362012-12-13 04:17:14 +00006528 break;
6529
6530 default:
6531 CmdArgs.push_back("-matpcs");
6532 }
Stephen Hines651f13c2014-04-23 16:59:28 -07006533 } else if (getToolChain().getArch() == llvm::Triple::sparc ||
6534 getToolChain().getArch() == llvm::Triple::sparcv9) {
6535 if (getToolChain().getArch() == llvm::Triple::sparc)
6536 CmdArgs.push_back("-Av8plusa");
6537 else
6538 CmdArgs.push_back("-Av9a");
6539
6540 addAssemblerKPIC(Args, CmdArgs);
Eric Christopherc55da4b2012-09-05 21:32:44 +00006541 }
Eric Christophered734732010-03-02 02:41:08 +00006542
Daniel Dunbar68a31d42009-03-31 17:45:15 +00006543 Args.AddAllArgValues(CmdArgs, options::OPT_Wa_COMMA,
6544 options::OPT_Xassembler);
6545
6546 CmdArgs.push_back("-o");
Daniel Dunbar7c1e4652010-08-02 02:38:21 +00006547 CmdArgs.push_back(Output.getFilename());
Daniel Dunbar68a31d42009-03-31 17:45:15 +00006548
Stephen Hinesc568f1e2014-07-21 00:47:37 -07006549 for (const auto &II : Inputs)
Daniel Dunbar7c1e4652010-08-02 02:38:21 +00006550 CmdArgs.push_back(II.getFilename());
Daniel Dunbar68a31d42009-03-31 17:45:15 +00006551
Stephen Hinesc568f1e2014-07-21 00:47:37 -07006552 const char *Exec = Args.MakeArgString(getToolChain().GetProgramPath("as"));
Stephen Hines176edba2014-12-01 14:53:08 -08006553 C.addCommand(llvm::make_unique<Command>(JA, *this, Exec, CmdArgs));
Daniel Dunbar68a31d42009-03-31 17:45:15 +00006554}
Daniel Dunbar008f54a2009-04-01 19:36:32 +00006555
6556void freebsd::Link::ConstructJob(Compilation &C, const JobAction &JA,
Daniel Dunbar2fe238e2010-08-02 02:38:28 +00006557 const InputInfo &Output,
Daniel Dunbarc21c4852009-04-08 23:54:23 +00006558 const InputInfoList &Inputs,
6559 const ArgList &Args,
Daniel Dunbara8304f62009-05-02 20:14:53 +00006560 const char *LinkingOutput) const {
Roman Divacky94380162012-08-28 15:09:03 +00006561 const toolchains::FreeBSD& ToolChain =
6562 static_cast<const toolchains::FreeBSD&>(getToolChain());
6563 const Driver &D = ToolChain.getDriver();
Stephen Hines651f13c2014-04-23 16:59:28 -07006564 const bool IsPIE =
6565 !Args.hasArg(options::OPT_shared) &&
6566 (Args.hasArg(options::OPT_pie) || ToolChain.isPIEDefault());
Daniel Dunbar008f54a2009-04-01 19:36:32 +00006567 ArgStringList CmdArgs;
David Chisnalldfa210b2012-07-29 15:24:44 +00006568
6569 // Silence warning for "clang -g foo.o -o foo"
6570 Args.ClaimAllArgs(options::OPT_g_Group);
6571 // and "clang -emit-llvm foo.o -o foo"
6572 Args.ClaimAllArgs(options::OPT_emit_llvm);
6573 // and for "clang -w foo.o -o foo". Other warning options are already
6574 // handled somewhere else.
6575 Args.ClaimAllArgs(options::OPT_w);
Daniel Dunbar008f54a2009-04-01 19:36:32 +00006576
Joerg Sonnenberger8ab2bdc2011-03-21 13:51:29 +00006577 if (!D.SysRoot.empty())
6578 CmdArgs.push_back(Args.MakeArgString("--sysroot=" + D.SysRoot));
6579
Stephen Hines651f13c2014-04-23 16:59:28 -07006580 if (IsPIE)
Roman Divacky94380162012-08-28 15:09:03 +00006581 CmdArgs.push_back("-pie");
6582
Daniel Dunbar008f54a2009-04-01 19:36:32 +00006583 if (Args.hasArg(options::OPT_static)) {
6584 CmdArgs.push_back("-Bstatic");
6585 } else {
Rafael Espindola65ba55d2010-11-11 02:17:51 +00006586 if (Args.hasArg(options::OPT_rdynamic))
6587 CmdArgs.push_back("-export-dynamic");
Daniel Dunbar008f54a2009-04-01 19:36:32 +00006588 CmdArgs.push_back("--eh-frame-hdr");
6589 if (Args.hasArg(options::OPT_shared)) {
6590 CmdArgs.push_back("-Bshareable");
6591 } else {
6592 CmdArgs.push_back("-dynamic-linker");
6593 CmdArgs.push_back("/libexec/ld-elf.so.1");
6594 }
Roman Divacky94380162012-08-28 15:09:03 +00006595 if (ToolChain.getTriple().getOSMajorVersion() >= 9) {
6596 llvm::Triple::ArchType Arch = ToolChain.getArch();
David Chisnalldfa210b2012-07-29 15:24:44 +00006597 if (Arch == llvm::Triple::arm || Arch == llvm::Triple::sparc ||
6598 Arch == llvm::Triple::x86 || Arch == llvm::Triple::x86_64) {
6599 CmdArgs.push_back("--hash-style=both");
6600 }
6601 }
6602 CmdArgs.push_back("--enable-new-dtags");
Daniel Dunbar008f54a2009-04-01 19:36:32 +00006603 }
6604
6605 // When building 32-bit code on FreeBSD/amd64, we have to explicitly
6606 // instruct ld in the base system to link 32-bit code.
Rafael Espindola64f7ad92012-10-07 04:44:33 +00006607 if (ToolChain.getArch() == llvm::Triple::x86) {
Daniel Dunbar008f54a2009-04-01 19:36:32 +00006608 CmdArgs.push_back("-m");
6609 CmdArgs.push_back("elf_i386_fbsd");
6610 }
6611
Rafael Espindola64f7ad92012-10-07 04:44:33 +00006612 if (ToolChain.getArch() == llvm::Triple::ppc) {
Roman Divacky000a6552011-06-04 07:40:24 +00006613 CmdArgs.push_back("-m");
Roman Divacky1052c1d2011-11-21 16:50:32 +00006614 CmdArgs.push_back("elf32ppc_fbsd");
Roman Divacky000a6552011-06-04 07:40:24 +00006615 }
6616
Daniel Dunbar7c1e4652010-08-02 02:38:21 +00006617 if (Output.isFilename()) {
Daniel Dunbar008f54a2009-04-01 19:36:32 +00006618 CmdArgs.push_back("-o");
6619 CmdArgs.push_back(Output.getFilename());
6620 } else {
6621 assert(Output.isNothing() && "Invalid output.");
6622 }
6623
6624 if (!Args.hasArg(options::OPT_nostdlib) &&
6625 !Args.hasArg(options::OPT_nostartfiles)) {
Stephen Hines6bcf27b2014-05-29 04:14:42 -07006626 const char *crt1 = nullptr;
Daniel Dunbar008f54a2009-04-01 19:36:32 +00006627 if (!Args.hasArg(options::OPT_shared)) {
Roman Divackyc16bb762011-02-10 16:59:40 +00006628 if (Args.hasArg(options::OPT_pg))
Roman Divacky94380162012-08-28 15:09:03 +00006629 crt1 = "gcrt1.o";
Stephen Hines651f13c2014-04-23 16:59:28 -07006630 else if (IsPIE)
Roman Divacky94380162012-08-28 15:09:03 +00006631 crt1 = "Scrt1.o";
6632 else
6633 crt1 = "crt1.o";
Daniel Dunbar008f54a2009-04-01 19:36:32 +00006634 }
Roman Divacky94380162012-08-28 15:09:03 +00006635 if (crt1)
6636 CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath(crt1)));
6637
6638 CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath("crti.o")));
6639
Stephen Hines6bcf27b2014-05-29 04:14:42 -07006640 const char *crtbegin = nullptr;
Roman Divacky94380162012-08-28 15:09:03 +00006641 if (Args.hasArg(options::OPT_static))
6642 crtbegin = "crtbeginT.o";
Stephen Hines651f13c2014-04-23 16:59:28 -07006643 else if (Args.hasArg(options::OPT_shared) || IsPIE)
Roman Divacky94380162012-08-28 15:09:03 +00006644 crtbegin = "crtbeginS.o";
6645 else
6646 crtbegin = "crtbegin.o";
6647
6648 CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath(crtbegin)));
Daniel Dunbar008f54a2009-04-01 19:36:32 +00006649 }
6650
6651 Args.AddAllArgs(CmdArgs, options::OPT_L);
Stephen Hines176edba2014-12-01 14:53:08 -08006652 const ToolChain::path_list &Paths = ToolChain.getFilePaths();
Stephen Hinesc568f1e2014-07-21 00:47:37 -07006653 for (const auto &Path : Paths)
6654 CmdArgs.push_back(Args.MakeArgString(StringRef("-L") + Path));
Daniel Dunbar008f54a2009-04-01 19:36:32 +00006655 Args.AddAllArgs(CmdArgs, options::OPT_T_Group);
6656 Args.AddAllArgs(CmdArgs, options::OPT_e);
David Chisnallc7363772010-08-15 22:58:12 +00006657 Args.AddAllArgs(CmdArgs, options::OPT_s);
6658 Args.AddAllArgs(CmdArgs, options::OPT_t);
6659 Args.AddAllArgs(CmdArgs, options::OPT_Z_Flag);
6660 Args.AddAllArgs(CmdArgs, options::OPT_r);
Daniel Dunbar008f54a2009-04-01 19:36:32 +00006661
Stephen Hines651f13c2014-04-23 16:59:28 -07006662 if (D.IsUsingLTO(Args))
6663 AddGoldPlugin(ToolChain, Args, CmdArgs);
Roman Divackydb334192013-11-10 09:31:43 +00006664
Stephen Hines176edba2014-12-01 14:53:08 -08006665 bool NeedsSanitizerDeps = addSanitizerRuntimes(ToolChain, Args, CmdArgs);
Roman Divacky94380162012-08-28 15:09:03 +00006666 AddLinkerInputs(ToolChain, Inputs, Args, CmdArgs);
Daniel Dunbar008f54a2009-04-01 19:36:32 +00006667
6668 if (!Args.hasArg(options::OPT_nostdlib) &&
6669 !Args.hasArg(options::OPT_nodefaultlibs)) {
Hans Wennborg76b86c22013-07-18 20:29:38 +00006670 if (D.CCCIsCXX()) {
Roman Divacky94380162012-08-28 15:09:03 +00006671 ToolChain.AddCXXStdlibLibArgs(Args, CmdArgs);
Roman Divackyc16bb762011-02-10 16:59:40 +00006672 if (Args.hasArg(options::OPT_pg))
6673 CmdArgs.push_back("-lm_p");
6674 else
6675 CmdArgs.push_back("-lm");
Daniel Dunbar20022632010-02-17 08:07:51 +00006676 }
Stephen Hines176edba2014-12-01 14:53:08 -08006677 if (NeedsSanitizerDeps)
6678 linkSanitizerRuntimeDeps(ToolChain, CmdArgs);
Daniel Dunbar008f54a2009-04-01 19:36:32 +00006679 // FIXME: For some reason GCC passes -lgcc and -lgcc_s before adding
6680 // the default system libraries. Just mimic this for now.
Roman Divackyc16bb762011-02-10 16:59:40 +00006681 if (Args.hasArg(options::OPT_pg))
6682 CmdArgs.push_back("-lgcc_p");
6683 else
6684 CmdArgs.push_back("-lgcc");
Daniel Dunbar008f54a2009-04-01 19:36:32 +00006685 if (Args.hasArg(options::OPT_static)) {
6686 CmdArgs.push_back("-lgcc_eh");
Roman Divackyc16bb762011-02-10 16:59:40 +00006687 } else if (Args.hasArg(options::OPT_pg)) {
6688 CmdArgs.push_back("-lgcc_eh_p");
Daniel Dunbar008f54a2009-04-01 19:36:32 +00006689 } else {
6690 CmdArgs.push_back("--as-needed");
6691 CmdArgs.push_back("-lgcc_s");
6692 CmdArgs.push_back("--no-as-needed");
6693 }
6694
Matt Beaumont-Gay24230262011-02-10 20:35:01 +00006695 if (Args.hasArg(options::OPT_pthread)) {
Roman Divackyc16bb762011-02-10 16:59:40 +00006696 if (Args.hasArg(options::OPT_pg))
6697 CmdArgs.push_back("-lpthread_p");
6698 else
6699 CmdArgs.push_back("-lpthread");
Matt Beaumont-Gay24230262011-02-10 20:35:01 +00006700 }
Daniel Dunbar008f54a2009-04-01 19:36:32 +00006701
Roman Divackyc16bb762011-02-10 16:59:40 +00006702 if (Args.hasArg(options::OPT_pg)) {
6703 if (Args.hasArg(options::OPT_shared))
6704 CmdArgs.push_back("-lc");
6705 else
6706 CmdArgs.push_back("-lc_p");
6707 CmdArgs.push_back("-lgcc_p");
6708 } else {
6709 CmdArgs.push_back("-lc");
6710 CmdArgs.push_back("-lgcc");
6711 }
6712
Daniel Dunbar008f54a2009-04-01 19:36:32 +00006713 if (Args.hasArg(options::OPT_static)) {
6714 CmdArgs.push_back("-lgcc_eh");
Roman Divackyc16bb762011-02-10 16:59:40 +00006715 } else if (Args.hasArg(options::OPT_pg)) {
6716 CmdArgs.push_back("-lgcc_eh_p");
Daniel Dunbar008f54a2009-04-01 19:36:32 +00006717 } else {
6718 CmdArgs.push_back("--as-needed");
6719 CmdArgs.push_back("-lgcc_s");
6720 CmdArgs.push_back("--no-as-needed");
6721 }
6722 }
6723
6724 if (!Args.hasArg(options::OPT_nostdlib) &&
6725 !Args.hasArg(options::OPT_nostartfiles)) {
Stephen Hines651f13c2014-04-23 16:59:28 -07006726 if (Args.hasArg(options::OPT_shared) || IsPIE)
Roman Divacky94380162012-08-28 15:09:03 +00006727 CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath("crtendS.o")));
Roman Divackyf6513812012-09-07 13:36:21 +00006728 else
6729 CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath("crtend.o")));
Roman Divacky94380162012-08-28 15:09:03 +00006730 CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath("crtn.o")));
Daniel Dunbar008f54a2009-04-01 19:36:32 +00006731 }
6732
Stephen Hines651f13c2014-04-23 16:59:28 -07006733 addProfileRT(ToolChain, Args, CmdArgs);
Nick Lewycky2e95a6d2011-05-24 21:54:59 +00006734
Daniel Dunbarc21c4852009-04-08 23:54:23 +00006735 const char *Exec =
Stephen Hinesc568f1e2014-07-21 00:47:37 -07006736 Args.MakeArgString(getToolChain().GetLinkerPath());
Stephen Hines176edba2014-12-01 14:53:08 -08006737 C.addCommand(llvm::make_unique<Command>(JA, *this, Exec, CmdArgs));
Daniel Dunbar008f54a2009-04-01 19:36:32 +00006738}
Daniel Dunbar11e1b402009-05-02 18:28:39 +00006739
Benjamin Kramer8e50a962011-02-02 18:59:27 +00006740void netbsd::Assemble::ConstructJob(Compilation &C, const JobAction &JA,
6741 const InputInfo &Output,
6742 const InputInfoList &Inputs,
6743 const ArgList &Args,
6744 const char *LinkingOutput) const {
6745 ArgStringList CmdArgs;
6746
Stephen Hines651f13c2014-04-23 16:59:28 -07006747 // GNU as needs different flags for creating the correct output format
6748 // on architectures with different ABIs or optional feature sets.
6749 switch (getToolChain().getArch()) {
6750 case llvm::Triple::x86:
Benjamin Kramer8e50a962011-02-02 18:59:27 +00006751 CmdArgs.push_back("--32");
Stephen Hines651f13c2014-04-23 16:59:28 -07006752 break;
6753 case llvm::Triple::arm:
6754 case llvm::Triple::armeb:
6755 case llvm::Triple::thumb:
6756 case llvm::Triple::thumbeb: {
6757 std::string MArch(arm::getARMTargetCPU(Args, getToolChain().getTriple()));
Bill Wendlingcf660bd2013-12-06 19:12:36 +00006758 CmdArgs.push_back(Args.MakeArgString("-mcpu=" + MArch));
Stephen Hines651f13c2014-04-23 16:59:28 -07006759 break;
Bill Wendlingcf660bd2013-12-06 19:12:36 +00006760 }
6761
Stephen Hines651f13c2014-04-23 16:59:28 -07006762 case llvm::Triple::mips:
6763 case llvm::Triple::mipsel:
6764 case llvm::Triple::mips64:
6765 case llvm::Triple::mips64el: {
Bill Wendlingc54c8852013-12-09 02:59:27 +00006766 StringRef CPUName;
6767 StringRef ABIName;
Stephen Hines176edba2014-12-01 14:53:08 -08006768 mips::getMipsCPUAndABI(Args, getToolChain().getTriple(), CPUName, ABIName);
Bill Wendlingc54c8852013-12-09 02:59:27 +00006769
6770 CmdArgs.push_back("-march");
6771 CmdArgs.push_back(CPUName.data());
6772
6773 CmdArgs.push_back("-mabi");
6774 CmdArgs.push_back(getGnuCompatibleMipsABIName(ABIName).data());
6775
6776 if (getToolChain().getArch() == llvm::Triple::mips ||
6777 getToolChain().getArch() == llvm::Triple::mips64)
6778 CmdArgs.push_back("-EB");
6779 else
6780 CmdArgs.push_back("-EL");
6781
Stephen Hines651f13c2014-04-23 16:59:28 -07006782 addAssemblerKPIC(Args, CmdArgs);
6783 break;
6784 }
6785
6786 case llvm::Triple::sparc:
6787 CmdArgs.push_back("-32");
6788 addAssemblerKPIC(Args, CmdArgs);
6789 break;
6790
6791 case llvm::Triple::sparcv9:
6792 CmdArgs.push_back("-64");
6793 CmdArgs.push_back("-Av9");
6794 addAssemblerKPIC(Args, CmdArgs);
6795 break;
6796
6797 default:
6798 break;
Bill Wendlingc54c8852013-12-09 02:59:27 +00006799 }
Benjamin Kramer8e50a962011-02-02 18:59:27 +00006800
6801 Args.AddAllArgValues(CmdArgs, options::OPT_Wa_COMMA,
6802 options::OPT_Xassembler);
6803
6804 CmdArgs.push_back("-o");
6805 CmdArgs.push_back(Output.getFilename());
6806
Stephen Hinesc568f1e2014-07-21 00:47:37 -07006807 for (const auto &II : Inputs)
Benjamin Kramer8e50a962011-02-02 18:59:27 +00006808 CmdArgs.push_back(II.getFilename());
Benjamin Kramer8e50a962011-02-02 18:59:27 +00006809
David Chisnall5adcec12011-09-27 22:03:18 +00006810 const char *Exec = Args.MakeArgString((getToolChain().GetProgramPath("as")));
Stephen Hines176edba2014-12-01 14:53:08 -08006811 C.addCommand(llvm::make_unique<Command>(JA, *this, Exec, CmdArgs));
Benjamin Kramer8e50a962011-02-02 18:59:27 +00006812}
6813
6814void netbsd::Link::ConstructJob(Compilation &C, const JobAction &JA,
6815 const InputInfo &Output,
6816 const InputInfoList &Inputs,
6817 const ArgList &Args,
6818 const char *LinkingOutput) const {
6819 const Driver &D = getToolChain().getDriver();
6820 ArgStringList CmdArgs;
6821
Joerg Sonnenberger8ab2bdc2011-03-21 13:51:29 +00006822 if (!D.SysRoot.empty())
6823 CmdArgs.push_back(Args.MakeArgString("--sysroot=" + D.SysRoot));
6824
Stephen Hines651f13c2014-04-23 16:59:28 -07006825 CmdArgs.push_back("--eh-frame-hdr");
Benjamin Kramer8e50a962011-02-02 18:59:27 +00006826 if (Args.hasArg(options::OPT_static)) {
6827 CmdArgs.push_back("-Bstatic");
6828 } else {
6829 if (Args.hasArg(options::OPT_rdynamic))
6830 CmdArgs.push_back("-export-dynamic");
Benjamin Kramer8e50a962011-02-02 18:59:27 +00006831 if (Args.hasArg(options::OPT_shared)) {
6832 CmdArgs.push_back("-Bshareable");
6833 } else {
6834 CmdArgs.push_back("-dynamic-linker");
6835 CmdArgs.push_back("/libexec/ld.elf_so");
6836 }
6837 }
6838
Stephen Hines651f13c2014-04-23 16:59:28 -07006839 // Many NetBSD architectures support more than one ABI.
6840 // Determine the correct emulation for ld.
6841 switch (getToolChain().getArch()) {
6842 case llvm::Triple::x86:
Benjamin Kramer8e50a962011-02-02 18:59:27 +00006843 CmdArgs.push_back("-m");
6844 CmdArgs.push_back("elf_i386");
Stephen Hines651f13c2014-04-23 16:59:28 -07006845 break;
6846 case llvm::Triple::arm:
Stephen Hines651f13c2014-04-23 16:59:28 -07006847 case llvm::Triple::thumb:
Stephen Hines651f13c2014-04-23 16:59:28 -07006848 CmdArgs.push_back("-m");
6849 switch (getToolChain().getTriple().getEnvironment()) {
6850 case llvm::Triple::EABI:
6851 case llvm::Triple::GNUEABI:
6852 CmdArgs.push_back("armelf_nbsd_eabi");
6853 break;
6854 case llvm::Triple::EABIHF:
6855 case llvm::Triple::GNUEABIHF:
6856 CmdArgs.push_back("armelf_nbsd_eabihf");
6857 break;
6858 default:
6859 CmdArgs.push_back("armelf_nbsd");
6860 break;
6861 }
6862 break;
Stephen Hines176edba2014-12-01 14:53:08 -08006863 case llvm::Triple::armeb:
6864 case llvm::Triple::thumbeb:
6865 CmdArgs.push_back("-m");
6866 switch (getToolChain().getTriple().getEnvironment()) {
6867 case llvm::Triple::EABI:
6868 case llvm::Triple::GNUEABI:
6869 CmdArgs.push_back("armelfb_nbsd_eabi");
6870 break;
6871 case llvm::Triple::EABIHF:
6872 case llvm::Triple::GNUEABIHF:
6873 CmdArgs.push_back("armelfb_nbsd_eabihf");
6874 break;
6875 default:
6876 CmdArgs.push_back("armelfb_nbsd");
6877 break;
6878 }
6879 break;
Stephen Hines651f13c2014-04-23 16:59:28 -07006880 case llvm::Triple::mips64:
6881 case llvm::Triple::mips64el:
6882 if (mips::hasMipsAbiArg(Args, "32")) {
6883 CmdArgs.push_back("-m");
6884 if (getToolChain().getArch() == llvm::Triple::mips64)
6885 CmdArgs.push_back("elf32btsmip");
6886 else
6887 CmdArgs.push_back("elf32ltsmip");
6888 } else if (mips::hasMipsAbiArg(Args, "64")) {
6889 CmdArgs.push_back("-m");
6890 if (getToolChain().getArch() == llvm::Triple::mips64)
6891 CmdArgs.push_back("elf64btsmip");
6892 else
6893 CmdArgs.push_back("elf64ltsmip");
6894 }
6895 break;
Stephen Hines176edba2014-12-01 14:53:08 -08006896 case llvm::Triple::ppc:
6897 CmdArgs.push_back("-m");
6898 CmdArgs.push_back("elf32ppc_nbsd");
6899 break;
6900
6901 case llvm::Triple::ppc64:
6902 case llvm::Triple::ppc64le:
6903 CmdArgs.push_back("-m");
6904 CmdArgs.push_back("elf64ppc");
6905 break;
Stephen Hines651f13c2014-04-23 16:59:28 -07006906
6907 case llvm::Triple::sparc:
6908 CmdArgs.push_back("-m");
6909 CmdArgs.push_back("elf32_sparc");
6910 break;
6911
6912 case llvm::Triple::sparcv9:
6913 CmdArgs.push_back("-m");
6914 CmdArgs.push_back("elf64_sparc");
6915 break;
6916
6917 default:
6918 break;
Benjamin Kramer8e50a962011-02-02 18:59:27 +00006919 }
6920
6921 if (Output.isFilename()) {
6922 CmdArgs.push_back("-o");
6923 CmdArgs.push_back(Output.getFilename());
6924 } else {
6925 assert(Output.isNothing() && "Invalid output.");
6926 }
6927
6928 if (!Args.hasArg(options::OPT_nostdlib) &&
6929 !Args.hasArg(options::OPT_nostartfiles)) {
6930 if (!Args.hasArg(options::OPT_shared)) {
6931 CmdArgs.push_back(Args.MakeArgString(
6932 getToolChain().GetFilePath("crt0.o")));
6933 CmdArgs.push_back(Args.MakeArgString(
6934 getToolChain().GetFilePath("crti.o")));
6935 CmdArgs.push_back(Args.MakeArgString(
6936 getToolChain().GetFilePath("crtbegin.o")));
6937 } else {
6938 CmdArgs.push_back(Args.MakeArgString(
6939 getToolChain().GetFilePath("crti.o")));
6940 CmdArgs.push_back(Args.MakeArgString(
6941 getToolChain().GetFilePath("crtbeginS.o")));
6942 }
6943 }
6944
6945 Args.AddAllArgs(CmdArgs, options::OPT_L);
6946 Args.AddAllArgs(CmdArgs, options::OPT_T_Group);
6947 Args.AddAllArgs(CmdArgs, options::OPT_e);
6948 Args.AddAllArgs(CmdArgs, options::OPT_s);
6949 Args.AddAllArgs(CmdArgs, options::OPT_t);
6950 Args.AddAllArgs(CmdArgs, options::OPT_Z_Flag);
6951 Args.AddAllArgs(CmdArgs, options::OPT_r);
6952
6953 AddLinkerInputs(getToolChain(), Inputs, Args, CmdArgs);
6954
Joerg Sonnenbergere69cca42013-10-14 20:13:05 +00006955 unsigned Major, Minor, Micro;
6956 getToolChain().getTriple().getOSVersion(Major, Minor, Micro);
6957 bool useLibgcc = true;
Stephen Hines176edba2014-12-01 14:53:08 -08006958 if (Major >= 7 || (Major == 6 && Minor == 99 && Micro >= 49) || Major == 0) {
Stephen Hines651f13c2014-04-23 16:59:28 -07006959 switch(getToolChain().getArch()) {
Stephen Hines176edba2014-12-01 14:53:08 -08006960 case llvm::Triple::aarch64:
Stephen Hines6bcf27b2014-05-29 04:14:42 -07006961 case llvm::Triple::arm:
6962 case llvm::Triple::armeb:
6963 case llvm::Triple::thumb:
6964 case llvm::Triple::thumbeb:
Stephen Hines176edba2014-12-01 14:53:08 -08006965 case llvm::Triple::ppc:
6966 case llvm::Triple::ppc64:
6967 case llvm::Triple::ppc64le:
Stephen Hines651f13c2014-04-23 16:59:28 -07006968 case llvm::Triple::x86:
6969 case llvm::Triple::x86_64:
Joerg Sonnenbergere69cca42013-10-14 20:13:05 +00006970 useLibgcc = false;
Stephen Hines651f13c2014-04-23 16:59:28 -07006971 break;
6972 default:
6973 break;
6974 }
Joerg Sonnenbergere69cca42013-10-14 20:13:05 +00006975 }
6976
Benjamin Kramer8e50a962011-02-02 18:59:27 +00006977 if (!Args.hasArg(options::OPT_nostdlib) &&
6978 !Args.hasArg(options::OPT_nodefaultlibs)) {
Hans Wennborg76b86c22013-07-18 20:29:38 +00006979 if (D.CCCIsCXX()) {
Benjamin Kramer8e50a962011-02-02 18:59:27 +00006980 getToolChain().AddCXXStdlibLibArgs(Args, CmdArgs);
6981 CmdArgs.push_back("-lm");
6982 }
Benjamin Kramer8e50a962011-02-02 18:59:27 +00006983 if (Args.hasArg(options::OPT_pthread))
6984 CmdArgs.push_back("-lpthread");
6985 CmdArgs.push_back("-lc");
6986
Joerg Sonnenbergere69cca42013-10-14 20:13:05 +00006987 if (useLibgcc) {
6988 if (Args.hasArg(options::OPT_static)) {
6989 // libgcc_eh depends on libc, so resolve as much as possible,
6990 // pull in any new requirements from libc and then get the rest
6991 // of libgcc.
6992 CmdArgs.push_back("-lgcc_eh");
6993 CmdArgs.push_back("-lc");
6994 CmdArgs.push_back("-lgcc");
6995 } else {
6996 CmdArgs.push_back("-lgcc");
6997 CmdArgs.push_back("--as-needed");
6998 CmdArgs.push_back("-lgcc_s");
6999 CmdArgs.push_back("--no-as-needed");
7000 }
Benjamin Kramer8e50a962011-02-02 18:59:27 +00007001 }
7002 }
7003
7004 if (!Args.hasArg(options::OPT_nostdlib) &&
7005 !Args.hasArg(options::OPT_nostartfiles)) {
7006 if (!Args.hasArg(options::OPT_shared))
7007 CmdArgs.push_back(Args.MakeArgString(getToolChain().GetFilePath(
7008 "crtend.o")));
7009 else
7010 CmdArgs.push_back(Args.MakeArgString(getToolChain().GetFilePath(
7011 "crtendS.o")));
7012 CmdArgs.push_back(Args.MakeArgString(getToolChain().GetFilePath(
7013 "crtn.o")));
7014 }
7015
Stephen Hines651f13c2014-04-23 16:59:28 -07007016 addProfileRT(getToolChain(), Args, CmdArgs);
Nick Lewycky2e95a6d2011-05-24 21:54:59 +00007017
Stephen Hinesc568f1e2014-07-21 00:47:37 -07007018 const char *Exec = Args.MakeArgString(getToolChain().GetLinkerPath());
Stephen Hines176edba2014-12-01 14:53:08 -08007019 C.addCommand(llvm::make_unique<Command>(JA, *this, Exec, CmdArgs));
Benjamin Kramer8e50a962011-02-02 18:59:27 +00007020}
7021
Thomas Schwinge577bb0a2013-03-28 19:04:25 +00007022void gnutools::Assemble::ConstructJob(Compilation &C, const JobAction &JA,
7023 const InputInfo &Output,
7024 const InputInfoList &Inputs,
7025 const ArgList &Args,
7026 const char *LinkingOutput) const {
Rafael Espindolaba30bbe2010-08-10 00:25:48 +00007027 ArgStringList CmdArgs;
Stephen Hines651f13c2014-04-23 16:59:28 -07007028 bool NeedsKPIC = false;
Rafael Espindolaba30bbe2010-08-10 00:25:48 +00007029
7030 // Add --32/--64 to make sure we get the format we want.
7031 // This is incomplete
7032 if (getToolChain().getArch() == llvm::Triple::x86) {
7033 CmdArgs.push_back("--32");
7034 } else if (getToolChain().getArch() == llvm::Triple::x86_64) {
Stephen Hinesc568f1e2014-07-21 00:47:37 -07007035 if (getToolChain().getTriple().getEnvironment() == llvm::Triple::GNUX32)
7036 CmdArgs.push_back("--x32");
7037 else
7038 CmdArgs.push_back("--64");
Eli Friedman7972c882011-11-28 23:46:52 +00007039 } else if (getToolChain().getArch() == llvm::Triple::ppc) {
7040 CmdArgs.push_back("-a32");
7041 CmdArgs.push_back("-mppc");
7042 CmdArgs.push_back("-many");
7043 } else if (getToolChain().getArch() == llvm::Triple::ppc64) {
7044 CmdArgs.push_back("-a64");
7045 CmdArgs.push_back("-mppc64");
7046 CmdArgs.push_back("-many");
Bill Schmidtea7fb0c2013-07-26 01:36:11 +00007047 } else if (getToolChain().getArch() == llvm::Triple::ppc64le) {
7048 CmdArgs.push_back("-a64");
Stephen Hines651f13c2014-04-23 16:59:28 -07007049 CmdArgs.push_back("-mppc64");
Bill Schmidtea7fb0c2013-07-26 01:36:11 +00007050 CmdArgs.push_back("-many");
Stephen Hines651f13c2014-04-23 16:59:28 -07007051 CmdArgs.push_back("-mlittle-endian");
7052 } else if (getToolChain().getArch() == llvm::Triple::sparc) {
7053 CmdArgs.push_back("-32");
7054 CmdArgs.push_back("-Av8plusa");
7055 NeedsKPIC = true;
7056 } else if (getToolChain().getArch() == llvm::Triple::sparcv9) {
7057 CmdArgs.push_back("-64");
7058 CmdArgs.push_back("-Av9a");
7059 NeedsKPIC = true;
7060 } else if (getToolChain().getArch() == llvm::Triple::arm ||
7061 getToolChain().getArch() == llvm::Triple::armeb) {
Chris Lattner5f9e2722011-07-23 10:55:15 +00007062 StringRef MArch = getToolChain().getArchName();
Rafael Espindolaba30bbe2010-08-10 00:25:48 +00007063 if (MArch == "armv7" || MArch == "armv7a" || MArch == "armv7-a")
7064 CmdArgs.push_back("-mfpu=neon");
Stephen Hines651f13c2014-04-23 16:59:28 -07007065 if (MArch == "armv8" || MArch == "armv8a" || MArch == "armv8-a" ||
7066 MArch == "armebv8" || MArch == "armebv8a" || MArch == "armebv8-a")
Bernard Ogden80e90c22013-10-24 18:32:41 +00007067 CmdArgs.push_back("-mfpu=crypto-neon-fp-armv8");
Evgeniy Stepanov700c5082012-04-20 09:03:40 +00007068
Stephen Hines651f13c2014-04-23 16:59:28 -07007069 StringRef ARMFloatABI = tools::arm::getARMFloatABI(
7070 getToolChain().getDriver(), Args, getToolChain().getTriple());
Evgeniy Stepanov700c5082012-04-20 09:03:40 +00007071 CmdArgs.push_back(Args.MakeArgString("-mfloat-abi=" + ARMFloatABI));
Evgeniy Stepanoveca187e2012-04-24 09:05:31 +00007072
7073 Args.AddLastArg(CmdArgs, options::OPT_march_EQ);
Stephen Hines651f13c2014-04-23 16:59:28 -07007074
7075 // FIXME: remove krait check when GNU tools support krait cpu
7076 // for now replace it with -march=armv7-a to avoid a lower
7077 // march from being picked in the absence of a cpu flag.
7078 Arg *A;
7079 if ((A = Args.getLastArg(options::OPT_mcpu_EQ)) &&
7080 StringRef(A->getValue()) == "krait")
7081 CmdArgs.push_back("-march=armv7-a");
7082 else
7083 Args.AddLastArg(CmdArgs, options::OPT_mcpu_EQ);
Evgeniy Stepanoveca187e2012-04-24 09:05:31 +00007084 Args.AddLastArg(CmdArgs, options::OPT_mfpu_EQ);
Akira Hatanakac85900f2011-11-30 19:31:38 +00007085 } else if (getToolChain().getArch() == llvm::Triple::mips ||
7086 getToolChain().getArch() == llvm::Triple::mipsel ||
7087 getToolChain().getArch() == llvm::Triple::mips64 ||
7088 getToolChain().getArch() == llvm::Triple::mips64el) {
Simon Atanasyan073a7802012-04-07 22:31:29 +00007089 StringRef CPUName;
7090 StringRef ABIName;
Stephen Hines176edba2014-12-01 14:53:08 -08007091 mips::getMipsCPUAndABI(Args, getToolChain().getTriple(), CPUName, ABIName);
7092 ABIName = getGnuCompatibleMipsABIName(ABIName);
Akira Hatanakac85900f2011-11-30 19:31:38 +00007093
Simon Atanasyan073a7802012-04-07 22:31:29 +00007094 CmdArgs.push_back("-march");
7095 CmdArgs.push_back(CPUName.data());
7096
Simon Atanasyan073a7802012-04-07 22:31:29 +00007097 CmdArgs.push_back("-mabi");
Stephen Hines176edba2014-12-01 14:53:08 -08007098 CmdArgs.push_back(ABIName.data());
7099
7100 // -mno-shared should be emitted unless -fpic, -fpie, -fPIC, -fPIE,
7101 // or -mshared (not implemented) is in effect.
7102 bool IsPicOrPie = false;
7103 if (Arg *A = Args.getLastArg(options::OPT_fPIC, options::OPT_fno_PIC,
7104 options::OPT_fpic, options::OPT_fno_pic,
7105 options::OPT_fPIE, options::OPT_fno_PIE,
7106 options::OPT_fpie, options::OPT_fno_pie)) {
7107 if (A->getOption().matches(options::OPT_fPIC) ||
7108 A->getOption().matches(options::OPT_fpic) ||
7109 A->getOption().matches(options::OPT_fPIE) ||
7110 A->getOption().matches(options::OPT_fpie))
7111 IsPicOrPie = true;
7112 }
7113 if (!IsPicOrPie)
7114 CmdArgs.push_back("-mno-shared");
7115
7116 // LLVM doesn't support -mplt yet and acts as if it is always given.
7117 // However, -mplt has no effect with the N64 ABI.
7118 CmdArgs.push_back(ABIName == "64" ? "-KPIC" : "-call_nonpic");
Simon Atanasyan5f0a1c12012-04-06 19:15:24 +00007119
7120 if (getToolChain().getArch() == llvm::Triple::mips ||
7121 getToolChain().getArch() == llvm::Triple::mips64)
7122 CmdArgs.push_back("-EB");
7123 else
7124 CmdArgs.push_back("-EL");
Simon Atanasyan1f0646e2012-05-29 19:07:33 +00007125
Simon Atanasyanfc12c4a2013-09-24 09:09:16 +00007126 if (Arg *A = Args.getLastArg(options::OPT_mnan_EQ)) {
7127 if (StringRef(A->getValue()) == "2008")
7128 CmdArgs.push_back(Args.MakeArgString("-mnan=2008"));
7129 }
7130
Stephen Hines176edba2014-12-01 14:53:08 -08007131 // Add the last -mfp32/-mfpxx/-mfp64 or -mfpxx if it is enabled by default.
7132 if (Arg *A = Args.getLastArg(options::OPT_mfp32, options::OPT_mfpxx,
7133 options::OPT_mfp64)) {
7134 A->claim();
7135 A->render(Args, CmdArgs);
7136 } else if (mips::isFPXXDefault(getToolChain().getTriple(), CPUName,
7137 ABIName))
7138 CmdArgs.push_back("-mfpxx");
7139
7140 // Pass on -mmips16 or -mno-mips16. However, the assembler equivalent of
7141 // -mno-mips16 is actually -no-mips16.
7142 if (Arg *A = Args.getLastArg(options::OPT_mips16,
7143 options::OPT_mno_mips16)) {
7144 if (A->getOption().matches(options::OPT_mips16)) {
7145 A->claim();
7146 A->render(Args, CmdArgs);
7147 } else {
7148 A->claim();
7149 CmdArgs.push_back("-no-mips16");
7150 }
7151 }
7152
Simon Atanasyan9dbfc612013-04-30 07:47:13 +00007153 Args.AddLastArg(CmdArgs, options::OPT_mmicromips,
7154 options::OPT_mno_micromips);
7155 Args.AddLastArg(CmdArgs, options::OPT_mdsp, options::OPT_mno_dsp);
7156 Args.AddLastArg(CmdArgs, options::OPT_mdspr2, options::OPT_mno_dspr2);
7157
Daniel Sanders0d5d6ff2013-12-02 10:14:43 +00007158 if (Arg *A = Args.getLastArg(options::OPT_mmsa, options::OPT_mno_msa)) {
7159 // Do not use AddLastArg because not all versions of MIPS assembler
7160 // support -mmsa / -mno-msa options.
7161 if (A->getOption().matches(options::OPT_mmsa))
7162 CmdArgs.push_back(Args.MakeArgString("-mmsa"));
7163 }
7164
Stephen Hines176edba2014-12-01 14:53:08 -08007165 Args.AddLastArg(CmdArgs, options::OPT_mhard_float,
7166 options::OPT_msoft_float);
7167
7168 Args.AddLastArg(CmdArgs, options::OPT_modd_spreg,
7169 options::OPT_mno_odd_spreg);
7170
Stephen Hines651f13c2014-04-23 16:59:28 -07007171 NeedsKPIC = true;
Ulrich Weigandb8409212013-05-06 16:26:41 +00007172 } else if (getToolChain().getArch() == llvm::Triple::systemz) {
Richard Sandiford5c92b9a2013-07-19 16:51:51 +00007173 // Always pass an -march option, since our default of z10 is later
7174 // than the GNU assembler's default.
7175 StringRef CPUName = getSystemZTargetCPU(Args);
7176 CmdArgs.push_back(Args.MakeArgString("-march=" + CPUName));
Rafael Espindolaba30bbe2010-08-10 00:25:48 +00007177 }
7178
Stephen Hines651f13c2014-04-23 16:59:28 -07007179 if (NeedsKPIC)
7180 addAssemblerKPIC(Args, CmdArgs);
7181
Rafael Espindolaba30bbe2010-08-10 00:25:48 +00007182 Args.AddAllArgValues(CmdArgs, options::OPT_Wa_COMMA,
7183 options::OPT_Xassembler);
7184
7185 CmdArgs.push_back("-o");
7186 CmdArgs.push_back(Output.getFilename());
7187
Stephen Hinesc568f1e2014-07-21 00:47:37 -07007188 for (const auto &II : Inputs)
Rafael Espindolaba30bbe2010-08-10 00:25:48 +00007189 CmdArgs.push_back(II.getFilename());
Rafael Espindolaba30bbe2010-08-10 00:25:48 +00007190
Stephen Hinesc568f1e2014-07-21 00:47:37 -07007191 const char *Exec = Args.MakeArgString(getToolChain().GetProgramPath("as"));
Stephen Hines176edba2014-12-01 14:53:08 -08007192 C.addCommand(llvm::make_unique<Command>(JA, *this, Exec, CmdArgs));
Eric Christopherc47b6f32013-06-05 23:58:15 +00007193
7194 // Handle the debug info splitting at object creation time if we're
7195 // creating an object.
7196 // TODO: Currently only works on linux with newer objcopy.
7197 if (Args.hasArg(options::OPT_gsplit_dwarf) &&
Cameron Esfahani57b1da12013-09-14 01:09:11 +00007198 getToolChain().getTriple().isOSLinux())
Eric Christopherc47b6f32013-06-05 23:58:15 +00007199 SplitDebugInfo(getToolChain(), C, *this, JA, Args, Output,
7200 SplitDebugName(Args, Inputs));
Rafael Espindolaba30bbe2010-08-10 00:25:48 +00007201}
7202
Stephen Hines651f13c2014-04-23 16:59:28 -07007203static void AddLibgcc(const llvm::Triple &Triple, const Driver &D,
Evgeniy Stepanova6ddc022012-04-25 08:59:22 +00007204 ArgStringList &CmdArgs, const ArgList &Args) {
Logan Chien94a71422012-09-02 09:30:11 +00007205 bool isAndroid = Triple.getEnvironment() == llvm::Triple::Android;
Chandler Carruth68f94db2013-03-04 02:07:55 +00007206 bool StaticLibgcc = Args.hasArg(options::OPT_static_libgcc) ||
7207 Args.hasArg(options::OPT_static);
Hans Wennborg76b86c22013-07-18 20:29:38 +00007208 if (!D.CCCIsCXX())
Rafael Espindolaabf3ac72011-10-17 21:39:04 +00007209 CmdArgs.push_back("-lgcc");
7210
Logan Chien529a73d2012-11-19 12:04:11 +00007211 if (StaticLibgcc || isAndroid) {
Hans Wennborg76b86c22013-07-18 20:29:38 +00007212 if (D.CCCIsCXX())
Rafael Espindolaabf3ac72011-10-17 21:39:04 +00007213 CmdArgs.push_back("-lgcc");
7214 } else {
Hans Wennborg76b86c22013-07-18 20:29:38 +00007215 if (!D.CCCIsCXX())
Rafael Espindolaabf3ac72011-10-17 21:39:04 +00007216 CmdArgs.push_back("--as-needed");
7217 CmdArgs.push_back("-lgcc_s");
Hans Wennborg76b86c22013-07-18 20:29:38 +00007218 if (!D.CCCIsCXX())
Rafael Espindolaabf3ac72011-10-17 21:39:04 +00007219 CmdArgs.push_back("--no-as-needed");
7220 }
7221
Evgeniy Stepanova6ddc022012-04-25 08:59:22 +00007222 if (StaticLibgcc && !isAndroid)
Rafael Espindolaabf3ac72011-10-17 21:39:04 +00007223 CmdArgs.push_back("-lgcc_eh");
Hans Wennborg76b86c22013-07-18 20:29:38 +00007224 else if (!Args.hasArg(options::OPT_shared) && D.CCCIsCXX())
Rafael Espindolaabf3ac72011-10-17 21:39:04 +00007225 CmdArgs.push_back("-lgcc");
Logan Chien529a73d2012-11-19 12:04:11 +00007226
7227 // According to Android ABI, we have to link with libdl if we are
7228 // linking with non-static libgcc.
7229 //
7230 // NOTE: This fixes a link error on Android MIPS as well. The non-static
7231 // libgcc for MIPS relies on _Unwind_Find_FDE and dl_iterate_phdr from libdl.
7232 if (isAndroid && !StaticLibgcc)
7233 CmdArgs.push_back("-ldl");
Rafael Espindolaabf3ac72011-10-17 21:39:04 +00007234}
7235
Stephen Hines176edba2014-12-01 14:53:08 -08007236static std::string getLinuxDynamicLinker(const ArgList &Args,
7237 const toolchains::Linux &ToolChain) {
Stephen Hines651f13c2014-04-23 16:59:28 -07007238 if (ToolChain.getTriple().getEnvironment() == llvm::Triple::Android) {
7239 if (ToolChain.getTriple().isArch64Bit())
7240 return "/system/bin/linker64";
7241 else
7242 return "/system/bin/linker";
7243 } else if (ToolChain.getArch() == llvm::Triple::x86 ||
7244 ToolChain.getArch() == llvm::Triple::sparc)
Peter Collingbournebdaa1342013-05-27 21:40:20 +00007245 return "/lib/ld-linux.so.2";
Stephen Hines176edba2014-12-01 14:53:08 -08007246 else if (ToolChain.getArch() == llvm::Triple::aarch64)
Peter Collingbournebdaa1342013-05-27 21:40:20 +00007247 return "/lib/ld-linux-aarch64.so.1";
Stephen Hines176edba2014-12-01 14:53:08 -08007248 else if (ToolChain.getArch() == llvm::Triple::aarch64_be)
Stephen Hines651f13c2014-04-23 16:59:28 -07007249 return "/lib/ld-linux-aarch64_be.so.1";
Peter Collingbournebdaa1342013-05-27 21:40:20 +00007250 else if (ToolChain.getArch() == llvm::Triple::arm ||
7251 ToolChain.getArch() == llvm::Triple::thumb) {
7252 if (ToolChain.getTriple().getEnvironment() == llvm::Triple::GNUEABIHF)
7253 return "/lib/ld-linux-armhf.so.3";
7254 else
7255 return "/lib/ld-linux.so.3";
Stephen Hines651f13c2014-04-23 16:59:28 -07007256 } else if (ToolChain.getArch() == llvm::Triple::armeb ||
7257 ToolChain.getArch() == llvm::Triple::thumbeb) {
7258 if (ToolChain.getTriple().getEnvironment() == llvm::Triple::GNUEABIHF)
7259 return "/lib/ld-linux-armhf.so.3"; /* TODO: check which dynamic linker name. */
7260 else
7261 return "/lib/ld-linux.so.3"; /* TODO: check which dynamic linker name. */
Peter Collingbournebdaa1342013-05-27 21:40:20 +00007262 } else if (ToolChain.getArch() == llvm::Triple::mips ||
Stephen Hines176edba2014-12-01 14:53:08 -08007263 ToolChain.getArch() == llvm::Triple::mipsel ||
7264 ToolChain.getArch() == llvm::Triple::mips64 ||
Stephen Hinesc568f1e2014-07-21 00:47:37 -07007265 ToolChain.getArch() == llvm::Triple::mips64el) {
Stephen Hines176edba2014-12-01 14:53:08 -08007266 StringRef CPUName;
7267 StringRef ABIName;
7268 mips::getMipsCPUAndABI(Args, ToolChain.getTriple(), CPUName, ABIName);
7269 bool IsNaN2008 = mips::isNaN2008(Args, ToolChain.getTriple());
7270
7271 StringRef LibDir = llvm::StringSwitch<llvm::StringRef>(ABIName)
7272 .Case("o32", "/lib")
7273 .Case("n32", "/lib32")
7274 .Case("n64", "/lib64")
7275 .Default("/lib");
7276 StringRef LibName;
7277 if (mips::isUCLibc(Args))
7278 LibName = IsNaN2008 ? "ld-uClibc-mipsn8.so.0" : "ld-uClibc.so.0";
7279 else
7280 LibName = IsNaN2008 ? "ld-linux-mipsn8.so.1" : "ld.so.1";
7281
7282 return (LibDir + "/" + LibName).str();
Peter Collingbournebdaa1342013-05-27 21:40:20 +00007283 } else if (ToolChain.getArch() == llvm::Triple::ppc)
7284 return "/lib/ld.so.1";
Stephen Hines176edba2014-12-01 14:53:08 -08007285 else if (ToolChain.getArch() == llvm::Triple::ppc64) {
7286 if (ppc::hasPPCAbiArg(Args, "elfv2"))
7287 return "/lib64/ld64.so.2";
Peter Collingbournebdaa1342013-05-27 21:40:20 +00007288 return "/lib64/ld64.so.1";
Stephen Hines176edba2014-12-01 14:53:08 -08007289 } else if (ToolChain.getArch() == llvm::Triple::ppc64le) {
7290 if (ppc::hasPPCAbiArg(Args, "elfv1"))
7291 return "/lib64/ld64.so.1";
Stephen Hinesc568f1e2014-07-21 00:47:37 -07007292 return "/lib64/ld64.so.2";
Stephen Hines176edba2014-12-01 14:53:08 -08007293 } else if (ToolChain.getArch() == llvm::Triple::systemz)
7294 return "/lib64/ld64.so.1";
Stephen Hines651f13c2014-04-23 16:59:28 -07007295 else if (ToolChain.getArch() == llvm::Triple::sparcv9)
7296 return "/lib64/ld-linux.so.2";
Stephen Hinesc568f1e2014-07-21 00:47:37 -07007297 else if (ToolChain.getArch() == llvm::Triple::x86_64 &&
7298 ToolChain.getTriple().getEnvironment() == llvm::Triple::GNUX32)
7299 return "/libx32/ld-linux-x32.so.2";
Peter Collingbournebdaa1342013-05-27 21:40:20 +00007300 else
7301 return "/lib64/ld-linux-x86-64.so.2";
7302}
7303
Stephen Hines651f13c2014-04-23 16:59:28 -07007304static void AddRunTimeLibs(const ToolChain &TC, const Driver &D,
7305 ArgStringList &CmdArgs, const ArgList &Args) {
7306 // Make use of compiler-rt if --rtlib option is used
7307 ToolChain::RuntimeLibType RLT = TC.GetRuntimeLibType(Args);
7308
7309 switch(RLT) {
7310 case ToolChain::RLT_CompilerRT:
Stephen Hines176edba2014-12-01 14:53:08 -08007311 switch (TC.getTriple().getOS()) {
7312 default: llvm_unreachable("unsupported OS");
7313 case llvm::Triple::Win32:
7314 addClangRTWindows(TC, Args, CmdArgs);
7315 break;
7316 case llvm::Triple::Linux:
7317 addClangRTLinux(TC, Args, CmdArgs);
7318 break;
7319 }
Stephen Hines651f13c2014-04-23 16:59:28 -07007320 break;
7321 case ToolChain::RLT_Libgcc:
7322 AddLibgcc(TC.getTriple(), D, CmdArgs, Args);
7323 break;
7324 }
7325}
7326
Stephen Hines176edba2014-12-01 14:53:08 -08007327static const char *getLDMOption(const llvm::Triple &T, const ArgList &Args) {
7328 switch (T.getArch()) {
7329 case llvm::Triple::x86:
7330 return "elf_i386";
7331 case llvm::Triple::aarch64:
7332 return "aarch64linux";
7333 case llvm::Triple::aarch64_be:
7334 return "aarch64_be_linux";
7335 case llvm::Triple::arm:
7336 case llvm::Triple::thumb:
7337 return "armelf_linux_eabi";
7338 case llvm::Triple::armeb:
7339 case llvm::Triple::thumbeb:
7340 return "armebelf_linux_eabi"; /* TODO: check which NAME. */
7341 case llvm::Triple::ppc:
7342 return "elf32ppclinux";
7343 case llvm::Triple::ppc64:
7344 return "elf64ppc";
7345 case llvm::Triple::ppc64le:
7346 return "elf64lppc";
7347 case llvm::Triple::sparc:
7348 return "elf32_sparc";
7349 case llvm::Triple::sparcv9:
7350 return "elf64_sparc";
7351 case llvm::Triple::mips:
7352 return "elf32btsmip";
7353 case llvm::Triple::mipsel:
7354 return "elf32ltsmip";
7355 case llvm::Triple::mips64:
7356 if (mips::hasMipsAbiArg(Args, "n32"))
7357 return "elf32btsmipn32";
7358 return "elf64btsmip";
7359 case llvm::Triple::mips64el:
7360 if (mips::hasMipsAbiArg(Args, "n32"))
7361 return "elf32ltsmipn32";
7362 return "elf64ltsmip";
7363 case llvm::Triple::systemz:
7364 return "elf64_s390";
7365 case llvm::Triple::x86_64:
7366 if (T.getEnvironment() == llvm::Triple::GNUX32)
7367 return "elf32_x86_64";
7368 return "elf_x86_64";
7369 default:
7370 llvm_unreachable("Unexpected arch");
7371 }
7372}
7373
Thomas Schwinge577bb0a2013-03-28 19:04:25 +00007374void gnutools::Link::ConstructJob(Compilation &C, const JobAction &JA,
7375 const InputInfo &Output,
7376 const InputInfoList &Inputs,
7377 const ArgList &Args,
7378 const char *LinkingOutput) const {
Rafael Espindolac1da9812010-11-07 20:14:31 +00007379 const toolchains::Linux& ToolChain =
7380 static_cast<const toolchains::Linux&>(getToolChain());
7381 const Driver &D = ToolChain.getDriver();
Rafael Espindola715852c2012-11-02 20:41:30 +00007382 const bool isAndroid =
7383 ToolChain.getTriple().getEnvironment() == llvm::Triple::Android;
Peter Collingbourne52ca70d2013-04-09 04:35:11 +00007384 const bool IsPIE =
7385 !Args.hasArg(options::OPT_shared) &&
Stephen Hines6bcf27b2014-05-29 04:14:42 -07007386 !Args.hasArg(options::OPT_static) &&
7387 (Args.hasArg(options::OPT_pie) || ToolChain.isPIEDefault() ||
7388 // On Android every code is PIC so every executable is PIE
7389 // Cannot use isPIEDefault here since otherwise
7390 // PIE only logic will be enabled during compilation
7391 isAndroid);
Evgeniy Stepanova6ddc022012-04-25 08:59:22 +00007392
Rafael Espindolac1da9812010-11-07 20:14:31 +00007393 ArgStringList CmdArgs;
7394
Rafael Espindola26f14c32010-11-15 18:28:16 +00007395 // Silence warning for "clang -g foo.o -o foo"
7396 Args.ClaimAllArgs(options::OPT_g_Group);
Rafael Espindola9c094fb2011-03-01 05:25:27 +00007397 // and "clang -emit-llvm foo.o -o foo"
7398 Args.ClaimAllArgs(options::OPT_emit_llvm);
David Chisnalldfa210b2012-07-29 15:24:44 +00007399 // and for "clang -w foo.o -o foo". Other warning options are already
Rafael Espindola7f6458b2010-11-17 20:37:10 +00007400 // handled somewhere else.
7401 Args.ClaimAllArgs(options::OPT_w);
Rafael Espindola26f14c32010-11-15 18:28:16 +00007402
Joerg Sonnenberger8ab2bdc2011-03-21 13:51:29 +00007403 if (!D.SysRoot.empty())
7404 CmdArgs.push_back(Args.MakeArgString("--sysroot=" + D.SysRoot));
Rafael Espindolac1da9812010-11-07 20:14:31 +00007405
Peter Collingbourne52ca70d2013-04-09 04:35:11 +00007406 if (IsPIE)
Rafael Espindolafdda1712010-11-17 22:26:15 +00007407 CmdArgs.push_back("-pie");
7408
Rafael Espindoladc1b76d2010-11-07 22:57:16 +00007409 if (Args.hasArg(options::OPT_rdynamic))
7410 CmdArgs.push_back("-export-dynamic");
7411
Rafael Espindolae0e6d3b2010-11-11 19:34:42 +00007412 if (Args.hasArg(options::OPT_s))
7413 CmdArgs.push_back("-s");
7414
Stephen Hinesc568f1e2014-07-21 00:47:37 -07007415 for (const auto &Opt : ToolChain.ExtraOpts)
7416 CmdArgs.push_back(Opt.c_str());
Rafael Espindolac1da9812010-11-07 20:14:31 +00007417
7418 if (!Args.hasArg(options::OPT_static)) {
7419 CmdArgs.push_back("--eh-frame-hdr");
7420 }
7421
7422 CmdArgs.push_back("-m");
Stephen Hines176edba2014-12-01 14:53:08 -08007423 CmdArgs.push_back(getLDMOption(ToolChain.getTriple(), Args));
Rafael Espindolac1da9812010-11-07 20:14:31 +00007424
7425 if (Args.hasArg(options::OPT_static)) {
Stephen Hines651f13c2014-04-23 16:59:28 -07007426 if (ToolChain.getArch() == llvm::Triple::arm ||
7427 ToolChain.getArch() == llvm::Triple::armeb ||
7428 ToolChain.getArch() == llvm::Triple::thumb ||
7429 ToolChain.getArch() == llvm::Triple::thumbeb)
Rafael Espindolac1da9812010-11-07 20:14:31 +00007430 CmdArgs.push_back("-Bstatic");
7431 else
7432 CmdArgs.push_back("-static");
7433 } else if (Args.hasArg(options::OPT_shared)) {
7434 CmdArgs.push_back("-shared");
7435 }
7436
7437 if (ToolChain.getArch() == llvm::Triple::arm ||
Stephen Hines651f13c2014-04-23 16:59:28 -07007438 ToolChain.getArch() == llvm::Triple::armeb ||
Douglas Gregorf0594d82011-03-06 19:11:49 +00007439 ToolChain.getArch() == llvm::Triple::thumb ||
Stephen Hines651f13c2014-04-23 16:59:28 -07007440 ToolChain.getArch() == llvm::Triple::thumbeb ||
Rafael Espindolac1da9812010-11-07 20:14:31 +00007441 (!Args.hasArg(options::OPT_static) &&
7442 !Args.hasArg(options::OPT_shared))) {
7443 CmdArgs.push_back("-dynamic-linker");
Peter Collingbournebdaa1342013-05-27 21:40:20 +00007444 CmdArgs.push_back(Args.MakeArgString(
7445 D.DyldPrefix + getLinuxDynamicLinker(Args, ToolChain)));
Rafael Espindolac1da9812010-11-07 20:14:31 +00007446 }
7447
7448 CmdArgs.push_back("-o");
7449 CmdArgs.push_back(Output.getFilename());
7450
Rafael Espindola49c64fd2010-12-01 01:52:43 +00007451 if (!Args.hasArg(options::OPT_nostdlib) &&
7452 !Args.hasArg(options::OPT_nostartfiles)) {
Evgeniy Stepanova6ddc022012-04-25 08:59:22 +00007453 if (!isAndroid) {
Stephen Hines6bcf27b2014-05-29 04:14:42 -07007454 const char *crt1 = nullptr;
Evgeniy Stepanova6ddc022012-04-25 08:59:22 +00007455 if (!Args.hasArg(options::OPT_shared)){
Eric Christopher61f08682013-06-07 23:25:01 +00007456 if (Args.hasArg(options::OPT_pg))
7457 crt1 = "gcrt1.o";
7458 else if (IsPIE)
Evgeniy Stepanova6ddc022012-04-25 08:59:22 +00007459 crt1 = "Scrt1.o";
7460 else
7461 crt1 = "crt1.o";
7462 }
7463 if (crt1)
7464 CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath(crt1)));
Rafael Espindolac1da9812010-11-07 20:14:31 +00007465
Evgeniy Stepanova6ddc022012-04-25 08:59:22 +00007466 CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath("crti.o")));
7467 }
Rafael Espindolac1da9812010-11-07 20:14:31 +00007468
Rafael Espindola89414b32010-11-12 03:00:39 +00007469 const char *crtbegin;
7470 if (Args.hasArg(options::OPT_static))
Evgeniy Stepanova6ddc022012-04-25 08:59:22 +00007471 crtbegin = isAndroid ? "crtbegin_static.o" : "crtbeginT.o";
Evgeniy Stepanova92983d2012-09-10 10:30:12 +00007472 else if (Args.hasArg(options::OPT_shared))
Evgeniy Stepanova6ddc022012-04-25 08:59:22 +00007473 crtbegin = isAndroid ? "crtbegin_so.o" : "crtbeginS.o";
Peter Collingbourne52ca70d2013-04-09 04:35:11 +00007474 else if (IsPIE)
Evgeniy Stepanova92983d2012-09-10 10:30:12 +00007475 crtbegin = isAndroid ? "crtbegin_dynamic.o" : "crtbeginS.o";
Rafael Espindola89414b32010-11-12 03:00:39 +00007476 else
Evgeniy Stepanova6ddc022012-04-25 08:59:22 +00007477 crtbegin = isAndroid ? "crtbegin_dynamic.o" : "crtbegin.o";
Rafael Espindola89414b32010-11-12 03:00:39 +00007478 CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath(crtbegin)));
Benjamin Kramere20e5082012-10-04 19:42:20 +00007479
7480 // Add crtfastmath.o if available and fast math is enabled.
7481 ToolChain.AddFastMathRuntimeIfAvailable(Args, CmdArgs);
Rafael Espindola89414b32010-11-12 03:00:39 +00007482 }
Rafael Espindolac1da9812010-11-07 20:14:31 +00007483
7484 Args.AddAllArgs(CmdArgs, options::OPT_L);
Stephen Hinesc568f1e2014-07-21 00:47:37 -07007485 Args.AddAllArgs(CmdArgs, options::OPT_u);
Rafael Espindolac1da9812010-11-07 20:14:31 +00007486
Stephen Hines176edba2014-12-01 14:53:08 -08007487 const ToolChain::path_list &Paths = ToolChain.getFilePaths();
Rafael Espindolac1da9812010-11-07 20:14:31 +00007488
Stephen Hinesc568f1e2014-07-21 00:47:37 -07007489 for (const auto &Path : Paths)
7490 CmdArgs.push_back(Args.MakeArgString(StringRef("-L") + Path));
Rafael Espindolac1da9812010-11-07 20:14:31 +00007491
Stephen Hines651f13c2014-04-23 16:59:28 -07007492 if (D.IsUsingLTO(Args))
7493 AddGoldPlugin(ToolChain, Args, CmdArgs);
Chandler Carruth700d4e42013-01-13 11:46:33 +00007494
Nick Lewyckye276cfc2012-08-17 03:39:16 +00007495 if (Args.hasArg(options::OPT_Z_Xlinker__no_demangle))
7496 CmdArgs.push_back("--no-demangle");
7497
Stephen Hines176edba2014-12-01 14:53:08 -08007498 bool NeedsSanitizerDeps = addSanitizerRuntimes(ToolChain, Args, CmdArgs);
Rafael Espindolac1da9812010-11-07 20:14:31 +00007499 AddLinkerInputs(ToolChain, Inputs, Args, CmdArgs);
Chandler Carruth80a393e2013-06-24 09:38:45 +00007500 // The profile runtime also needs access to system libraries.
Stephen Hines651f13c2014-04-23 16:59:28 -07007501 addProfileRT(getToolChain(), Args, CmdArgs);
Chandler Carruth80a393e2013-06-24 09:38:45 +00007502
Hans Wennborg76b86c22013-07-18 20:29:38 +00007503 if (D.CCCIsCXX() &&
Chandler Carruth2ba542c2012-05-14 18:31:18 +00007504 !Args.hasArg(options::OPT_nostdlib) &&
7505 !Args.hasArg(options::OPT_nodefaultlibs)) {
Rafael Espindola19706f82011-10-17 22:14:51 +00007506 bool OnlyLibstdcxxStatic = Args.hasArg(options::OPT_static_libstdcxx) &&
7507 !Args.hasArg(options::OPT_static);
7508 if (OnlyLibstdcxxStatic)
7509 CmdArgs.push_back("-Bstatic");
Rafael Espindolac1da9812010-11-07 20:14:31 +00007510 ToolChain.AddCXXStdlibLibArgs(Args, CmdArgs);
Rafael Espindola19706f82011-10-17 22:14:51 +00007511 if (OnlyLibstdcxxStatic)
7512 CmdArgs.push_back("-Bdynamic");
Rafael Espindolac1da9812010-11-07 20:14:31 +00007513 CmdArgs.push_back("-lm");
7514 }
7515
Rafael Espindola89414b32010-11-12 03:00:39 +00007516 if (!Args.hasArg(options::OPT_nostdlib)) {
Chandler Carruth2ba542c2012-05-14 18:31:18 +00007517 if (!Args.hasArg(options::OPT_nodefaultlibs)) {
7518 if (Args.hasArg(options::OPT_static))
7519 CmdArgs.push_back("--start-group");
Nick Lewycky80df0252011-06-04 06:27:06 +00007520
Stephen Hines176edba2014-12-01 14:53:08 -08007521 if (NeedsSanitizerDeps)
7522 linkSanitizerRuntimeDeps(ToolChain, CmdArgs);
7523
Stephen Hines651f13c2014-04-23 16:59:28 -07007524 LibOpenMP UsedOpenMPLib = LibUnknown;
7525 if (Args.hasArg(options::OPT_fopenmp)) {
7526 UsedOpenMPLib = LibGOMP;
7527 } else if (const Arg *A = Args.getLastArg(options::OPT_fopenmp_EQ)) {
7528 UsedOpenMPLib = llvm::StringSwitch<LibOpenMP>(A->getValue())
7529 .Case("libgomp", LibGOMP)
7530 .Case("libiomp5", LibIOMP5)
7531 .Default(LibUnknown);
7532 if (UsedOpenMPLib == LibUnknown)
7533 D.Diag(diag::err_drv_unsupported_option_argument)
7534 << A->getOption().getName() << A->getValue();
7535 }
7536 switch (UsedOpenMPLib) {
7537 case LibGOMP:
Chandler Carruthdf96e022013-01-17 13:19:29 +00007538 CmdArgs.push_back("-lgomp");
7539
Stephen Hines651f13c2014-04-23 16:59:28 -07007540 // FIXME: Exclude this for platforms with libgomp that don't require
7541 // librt. Most modern Linux platforms require it, but some may not.
Chandler Carruthdf96e022013-01-17 13:19:29 +00007542 CmdArgs.push_back("-lrt");
Stephen Hines651f13c2014-04-23 16:59:28 -07007543 break;
7544 case LibIOMP5:
7545 CmdArgs.push_back("-liomp5");
7546 break;
7547 case LibUnknown:
7548 break;
Chandler Carruthdf96e022013-01-17 13:19:29 +00007549 }
Stephen Hines651f13c2014-04-23 16:59:28 -07007550 AddRunTimeLibs(ToolChain, D, CmdArgs, Args);
Rafael Espindola89414b32010-11-12 03:00:39 +00007551
Stephen Hinesc568f1e2014-07-21 00:47:37 -07007552 if ((Args.hasArg(options::OPT_pthread) ||
7553 Args.hasArg(options::OPT_pthreads) || UsedOpenMPLib != LibUnknown) &&
7554 !isAndroid)
Chandler Carruth2ba542c2012-05-14 18:31:18 +00007555 CmdArgs.push_back("-lpthread");
7556
7557 CmdArgs.push_back("-lc");
7558
7559 if (Args.hasArg(options::OPT_static))
7560 CmdArgs.push_back("--end-group");
7561 else
Stephen Hines651f13c2014-04-23 16:59:28 -07007562 AddRunTimeLibs(ToolChain, D, CmdArgs, Args);
Chandler Carruth2ba542c2012-05-14 18:31:18 +00007563 }
Rafael Espindolafdda1712010-11-17 22:26:15 +00007564
Rafael Espindola49c64fd2010-12-01 01:52:43 +00007565 if (!Args.hasArg(options::OPT_nostartfiles)) {
7566 const char *crtend;
Evgeniy Stepanova92983d2012-09-10 10:30:12 +00007567 if (Args.hasArg(options::OPT_shared))
Evgeniy Stepanova6ddc022012-04-25 08:59:22 +00007568 crtend = isAndroid ? "crtend_so.o" : "crtendS.o";
Peter Collingbourne52ca70d2013-04-09 04:35:11 +00007569 else if (IsPIE)
Evgeniy Stepanova92983d2012-09-10 10:30:12 +00007570 crtend = isAndroid ? "crtend_android.o" : "crtendS.o";
Rafael Espindola49c64fd2010-12-01 01:52:43 +00007571 else
Evgeniy Stepanova6ddc022012-04-25 08:59:22 +00007572 crtend = isAndroid ? "crtend_android.o" : "crtend.o";
Rafael Espindola89414b32010-11-12 03:00:39 +00007573
Rafael Espindola49c64fd2010-12-01 01:52:43 +00007574 CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath(crtend)));
Evgeniy Stepanova6ddc022012-04-25 08:59:22 +00007575 if (!isAndroid)
7576 CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath("crtn.o")));
Rafael Espindola49c64fd2010-12-01 01:52:43 +00007577 }
Rafael Espindolac1da9812010-11-07 20:14:31 +00007578 }
7579
Stephen Hines176edba2014-12-01 14:53:08 -08007580 C.addCommand(
7581 llvm::make_unique<Command>(JA, *this, ToolChain.Linker.c_str(), CmdArgs));
Rafael Espindolac1da9812010-11-07 20:14:31 +00007582}
Rafael Espindolaba30bbe2010-08-10 00:25:48 +00007583
Chris Lattner38e317d2010-07-07 16:01:42 +00007584void minix::Assemble::ConstructJob(Compilation &C, const JobAction &JA,
Daniel Dunbar2fe238e2010-08-02 02:38:28 +00007585 const InputInfo &Output,
7586 const InputInfoList &Inputs,
7587 const ArgList &Args,
7588 const char *LinkingOutput) const {
Chris Lattner38e317d2010-07-07 16:01:42 +00007589 ArgStringList CmdArgs;
7590
Stephen Hinesc568f1e2014-07-21 00:47:37 -07007591 Args.AddAllArgValues(CmdArgs, options::OPT_Wa_COMMA, options::OPT_Xassembler);
Chris Lattner38e317d2010-07-07 16:01:42 +00007592
7593 CmdArgs.push_back("-o");
Daniel Dunbar7c1e4652010-08-02 02:38:21 +00007594 CmdArgs.push_back(Output.getFilename());
Chris Lattner38e317d2010-07-07 16:01:42 +00007595
Stephen Hinesc568f1e2014-07-21 00:47:37 -07007596 for (const auto &II : Inputs)
Daniel Dunbar7c1e4652010-08-02 02:38:21 +00007597 CmdArgs.push_back(II.getFilename());
Chris Lattner38e317d2010-07-07 16:01:42 +00007598
Stephen Hinesc568f1e2014-07-21 00:47:37 -07007599 const char *Exec = Args.MakeArgString(getToolChain().GetProgramPath("as"));
Stephen Hines176edba2014-12-01 14:53:08 -08007600 C.addCommand(llvm::make_unique<Command>(JA, *this, Exec, CmdArgs));
Chris Lattner38e317d2010-07-07 16:01:42 +00007601}
7602
7603void minix::Link::ConstructJob(Compilation &C, const JobAction &JA,
Daniel Dunbar2fe238e2010-08-02 02:38:28 +00007604 const InputInfo &Output,
7605 const InputInfoList &Inputs,
7606 const ArgList &Args,
7607 const char *LinkingOutput) const {
Chris Lattner38e317d2010-07-07 16:01:42 +00007608 const Driver &D = getToolChain().getDriver();
7609 ArgStringList CmdArgs;
7610
Daniel Dunbar7c1e4652010-08-02 02:38:21 +00007611 if (Output.isFilename()) {
Chris Lattner38e317d2010-07-07 16:01:42 +00007612 CmdArgs.push_back("-o");
7613 CmdArgs.push_back(Output.getFilename());
7614 } else {
7615 assert(Output.isNothing() && "Invalid output.");
7616 }
7617
7618 if (!Args.hasArg(options::OPT_nostdlib) &&
Eli Friedman6d402dc2011-12-08 23:54:21 +00007619 !Args.hasArg(options::OPT_nostartfiles)) {
7620 CmdArgs.push_back(Args.MakeArgString(getToolChain().GetFilePath("crt1.o")));
7621 CmdArgs.push_back(Args.MakeArgString(getToolChain().GetFilePath("crti.o")));
7622 CmdArgs.push_back(Args.MakeArgString(getToolChain().GetFilePath("crtbegin.o")));
7623 CmdArgs.push_back(Args.MakeArgString(getToolChain().GetFilePath("crtn.o")));
7624 }
Chris Lattner38e317d2010-07-07 16:01:42 +00007625
7626 Args.AddAllArgs(CmdArgs, options::OPT_L);
7627 Args.AddAllArgs(CmdArgs, options::OPT_T_Group);
7628 Args.AddAllArgs(CmdArgs, options::OPT_e);
7629
Daniel Dunbar2008fee2010-09-17 00:24:54 +00007630 AddLinkerInputs(getToolChain(), Inputs, Args, CmdArgs);
Chris Lattner38e317d2010-07-07 16:01:42 +00007631
Stephen Hines651f13c2014-04-23 16:59:28 -07007632 addProfileRT(getToolChain(), Args, CmdArgs);
Eli Friedman6d402dc2011-12-08 23:54:21 +00007633
Chris Lattner38e317d2010-07-07 16:01:42 +00007634 if (!Args.hasArg(options::OPT_nostdlib) &&
7635 !Args.hasArg(options::OPT_nodefaultlibs)) {
Hans Wennborg76b86c22013-07-18 20:29:38 +00007636 if (D.CCCIsCXX()) {
Daniel Dunbar132e35d2010-09-17 01:20:05 +00007637 getToolChain().AddCXXStdlibLibArgs(Args, CmdArgs);
Chris Lattner38e317d2010-07-07 16:01:42 +00007638 CmdArgs.push_back("-lm");
7639 }
Chris Lattner38e317d2010-07-07 16:01:42 +00007640 }
7641
7642 if (!Args.hasArg(options::OPT_nostdlib) &&
7643 !Args.hasArg(options::OPT_nostartfiles)) {
Eli Friedman6d402dc2011-12-08 23:54:21 +00007644 if (Args.hasArg(options::OPT_pthread))
7645 CmdArgs.push_back("-lpthread");
7646 CmdArgs.push_back("-lc");
7647 CmdArgs.push_back("-lCompilerRT-Generic");
7648 CmdArgs.push_back("-L/usr/pkg/compiler-rt/lib");
7649 CmdArgs.push_back(
Eric Christopher27e2b982012-12-18 00:31:10 +00007650 Args.MakeArgString(getToolChain().GetFilePath("crtend.o")));
Chris Lattner38e317d2010-07-07 16:01:42 +00007651 }
7652
Stephen Hinesc568f1e2014-07-21 00:47:37 -07007653 const char *Exec = Args.MakeArgString(getToolChain().GetLinkerPath());
Stephen Hines176edba2014-12-01 14:53:08 -08007654 C.addCommand(llvm::make_unique<Command>(JA, *this, Exec, CmdArgs));
Chris Lattner38e317d2010-07-07 16:01:42 +00007655}
7656
Daniel Dunbar11e1b402009-05-02 18:28:39 +00007657/// DragonFly Tools
7658
7659// For now, DragonFly Assemble does just about the same as for
7660// FreeBSD, but this may change soon.
7661void dragonfly::Assemble::ConstructJob(Compilation &C, const JobAction &JA,
Daniel Dunbar2fe238e2010-08-02 02:38:28 +00007662 const InputInfo &Output,
Daniel Dunbar294691e2009-11-04 06:24:38 +00007663 const InputInfoList &Inputs,
7664 const ArgList &Args,
7665 const char *LinkingOutput) const {
Daniel Dunbar11e1b402009-05-02 18:28:39 +00007666 ArgStringList CmdArgs;
7667
7668 // When building 32-bit code on DragonFly/pc64, we have to explicitly
7669 // instruct as in the base system to assemble 32-bit code.
Rafael Espindola64f7ad92012-10-07 04:44:33 +00007670 if (getToolChain().getArch() == llvm::Triple::x86)
Daniel Dunbar11e1b402009-05-02 18:28:39 +00007671 CmdArgs.push_back("--32");
7672
Stephen Hinesc568f1e2014-07-21 00:47:37 -07007673 Args.AddAllArgValues(CmdArgs, options::OPT_Wa_COMMA, options::OPT_Xassembler);
Daniel Dunbar11e1b402009-05-02 18:28:39 +00007674
7675 CmdArgs.push_back("-o");
Daniel Dunbar7c1e4652010-08-02 02:38:21 +00007676 CmdArgs.push_back(Output.getFilename());
Daniel Dunbar11e1b402009-05-02 18:28:39 +00007677
Stephen Hinesc568f1e2014-07-21 00:47:37 -07007678 for (const auto &II : Inputs)
Daniel Dunbar7c1e4652010-08-02 02:38:21 +00007679 CmdArgs.push_back(II.getFilename());
Daniel Dunbar11e1b402009-05-02 18:28:39 +00007680
Stephen Hinesc568f1e2014-07-21 00:47:37 -07007681 const char *Exec = Args.MakeArgString(getToolChain().GetProgramPath("as"));
Stephen Hines176edba2014-12-01 14:53:08 -08007682 C.addCommand(llvm::make_unique<Command>(JA, *this, Exec, CmdArgs));
Daniel Dunbar11e1b402009-05-02 18:28:39 +00007683}
7684
7685void dragonfly::Link::ConstructJob(Compilation &C, const JobAction &JA,
Daniel Dunbar2fe238e2010-08-02 02:38:28 +00007686 const InputInfo &Output,
7687 const InputInfoList &Inputs,
7688 const ArgList &Args,
7689 const char *LinkingOutput) const {
Daniel Dunbaree788e72009-12-21 18:54:17 +00007690 const Driver &D = getToolChain().getDriver();
Daniel Dunbar11e1b402009-05-02 18:28:39 +00007691 ArgStringList CmdArgs;
Stephen Hines176edba2014-12-01 14:53:08 -08007692 bool UseGCC47 = llvm::sys::fs::exists("/usr/lib/gcc47");
John McCall8cfb7202013-04-11 22:55:55 +00007693
Joerg Sonnenberger8ab2bdc2011-03-21 13:51:29 +00007694 if (!D.SysRoot.empty())
7695 CmdArgs.push_back(Args.MakeArgString("--sysroot=" + D.SysRoot));
7696
John McCall8cfb7202013-04-11 22:55:55 +00007697 CmdArgs.push_back("--eh-frame-hdr");
Daniel Dunbar11e1b402009-05-02 18:28:39 +00007698 if (Args.hasArg(options::OPT_static)) {
7699 CmdArgs.push_back("-Bstatic");
7700 } else {
John McCall8cfb7202013-04-11 22:55:55 +00007701 if (Args.hasArg(options::OPT_rdynamic))
7702 CmdArgs.push_back("-export-dynamic");
Daniel Dunbar11e1b402009-05-02 18:28:39 +00007703 if (Args.hasArg(options::OPT_shared))
7704 CmdArgs.push_back("-Bshareable");
7705 else {
7706 CmdArgs.push_back("-dynamic-linker");
7707 CmdArgs.push_back("/usr/libexec/ld-elf.so.2");
7708 }
John McCall8cfb7202013-04-11 22:55:55 +00007709 CmdArgs.push_back("--hash-style=both");
Daniel Dunbar11e1b402009-05-02 18:28:39 +00007710 }
7711
7712 // When building 32-bit code on DragonFly/pc64, we have to explicitly
7713 // instruct ld in the base system to link 32-bit code.
Rafael Espindola64f7ad92012-10-07 04:44:33 +00007714 if (getToolChain().getArch() == llvm::Triple::x86) {
Daniel Dunbar11e1b402009-05-02 18:28:39 +00007715 CmdArgs.push_back("-m");
7716 CmdArgs.push_back("elf_i386");
7717 }
7718
Daniel Dunbar7c1e4652010-08-02 02:38:21 +00007719 if (Output.isFilename()) {
Daniel Dunbar11e1b402009-05-02 18:28:39 +00007720 CmdArgs.push_back("-o");
7721 CmdArgs.push_back(Output.getFilename());
7722 } else {
7723 assert(Output.isNothing() && "Invalid output.");
7724 }
7725
7726 if (!Args.hasArg(options::OPT_nostdlib) &&
7727 !Args.hasArg(options::OPT_nostartfiles)) {
7728 if (!Args.hasArg(options::OPT_shared)) {
John McCall8cfb7202013-04-11 22:55:55 +00007729 if (Args.hasArg(options::OPT_pg))
7730 CmdArgs.push_back(Args.MakeArgString(
7731 getToolChain().GetFilePath("gcrt1.o")));
7732 else {
7733 if (Args.hasArg(options::OPT_pie))
7734 CmdArgs.push_back(Args.MakeArgString(
7735 getToolChain().GetFilePath("Scrt1.o")));
7736 else
7737 CmdArgs.push_back(Args.MakeArgString(
7738 getToolChain().GetFilePath("crt1.o")));
7739 }
Daniel Dunbar11e1b402009-05-02 18:28:39 +00007740 }
John McCall8cfb7202013-04-11 22:55:55 +00007741 CmdArgs.push_back(Args.MakeArgString(
7742 getToolChain().GetFilePath("crti.o")));
7743 if (Args.hasArg(options::OPT_shared) || Args.hasArg(options::OPT_pie))
7744 CmdArgs.push_back(Args.MakeArgString(
7745 getToolChain().GetFilePath("crtbeginS.o")));
7746 else
7747 CmdArgs.push_back(Args.MakeArgString(
7748 getToolChain().GetFilePath("crtbegin.o")));
Daniel Dunbar11e1b402009-05-02 18:28:39 +00007749 }
7750
7751 Args.AddAllArgs(CmdArgs, options::OPT_L);
7752 Args.AddAllArgs(CmdArgs, options::OPT_T_Group);
7753 Args.AddAllArgs(CmdArgs, options::OPT_e);
7754
Daniel Dunbar2008fee2010-09-17 00:24:54 +00007755 AddLinkerInputs(getToolChain(), Inputs, Args, CmdArgs);
Daniel Dunbar11e1b402009-05-02 18:28:39 +00007756
7757 if (!Args.hasArg(options::OPT_nostdlib) &&
7758 !Args.hasArg(options::OPT_nodefaultlibs)) {
7759 // FIXME: GCC passes on -lgcc, -lgcc_pic and a whole lot of
7760 // rpaths
John McCall8cfb7202013-04-11 22:55:55 +00007761 if (UseGCC47)
7762 CmdArgs.push_back("-L/usr/lib/gcc47");
7763 else
7764 CmdArgs.push_back("-L/usr/lib/gcc44");
Daniel Dunbar11e1b402009-05-02 18:28:39 +00007765
7766 if (!Args.hasArg(options::OPT_static)) {
John McCall8cfb7202013-04-11 22:55:55 +00007767 if (UseGCC47) {
7768 CmdArgs.push_back("-rpath");
7769 CmdArgs.push_back("/usr/lib/gcc47");
7770 } else {
7771 CmdArgs.push_back("-rpath");
7772 CmdArgs.push_back("/usr/lib/gcc44");
7773 }
Daniel Dunbar11e1b402009-05-02 18:28:39 +00007774 }
7775
Hans Wennborg76b86c22013-07-18 20:29:38 +00007776 if (D.CCCIsCXX()) {
Daniel Dunbar132e35d2010-09-17 01:20:05 +00007777 getToolChain().AddCXXStdlibLibArgs(Args, CmdArgs);
Rafael Espindola405861d2010-07-20 12:59:03 +00007778 CmdArgs.push_back("-lm");
7779 }
7780
Daniel Dunbar11e1b402009-05-02 18:28:39 +00007781 if (Args.hasArg(options::OPT_pthread))
Mike Stump4d63f8b2009-10-31 20:11:46 +00007782 CmdArgs.push_back("-lpthread");
Daniel Dunbar11e1b402009-05-02 18:28:39 +00007783
7784 if (!Args.hasArg(options::OPT_nolibc)) {
7785 CmdArgs.push_back("-lc");
7786 }
7787
John McCall8cfb7202013-04-11 22:55:55 +00007788 if (UseGCC47) {
7789 if (Args.hasArg(options::OPT_static) ||
7790 Args.hasArg(options::OPT_static_libgcc)) {
7791 CmdArgs.push_back("-lgcc");
7792 CmdArgs.push_back("-lgcc_eh");
7793 } else {
7794 if (Args.hasArg(options::OPT_shared_libgcc)) {
7795 CmdArgs.push_back("-lgcc_pic");
7796 if (!Args.hasArg(options::OPT_shared))
7797 CmdArgs.push_back("-lgcc");
7798 } else {
7799 CmdArgs.push_back("-lgcc");
7800 CmdArgs.push_back("--as-needed");
7801 CmdArgs.push_back("-lgcc_pic");
7802 CmdArgs.push_back("--no-as-needed");
7803 }
7804 }
Daniel Dunbar11e1b402009-05-02 18:28:39 +00007805 } else {
John McCall8cfb7202013-04-11 22:55:55 +00007806 if (Args.hasArg(options::OPT_shared)) {
7807 CmdArgs.push_back("-lgcc_pic");
7808 } else {
7809 CmdArgs.push_back("-lgcc");
7810 }
Daniel Dunbar11e1b402009-05-02 18:28:39 +00007811 }
7812 }
7813
7814 if (!Args.hasArg(options::OPT_nostdlib) &&
7815 !Args.hasArg(options::OPT_nostartfiles)) {
John McCall8cfb7202013-04-11 22:55:55 +00007816 if (Args.hasArg(options::OPT_shared) || Args.hasArg(options::OPT_pie))
Chris Lattner38e317d2010-07-07 16:01:42 +00007817 CmdArgs.push_back(Args.MakeArgString(
Daniel Dunbar4a7e8892010-07-14 18:46:23 +00007818 getToolChain().GetFilePath("crtendS.o")));
John McCall8cfb7202013-04-11 22:55:55 +00007819 else
7820 CmdArgs.push_back(Args.MakeArgString(
7821 getToolChain().GetFilePath("crtend.o")));
Chris Lattner38e317d2010-07-07 16:01:42 +00007822 CmdArgs.push_back(Args.MakeArgString(
John McCall8cfb7202013-04-11 22:55:55 +00007823 getToolChain().GetFilePath("crtn.o")));
Daniel Dunbar11e1b402009-05-02 18:28:39 +00007824 }
7825
Stephen Hines651f13c2014-04-23 16:59:28 -07007826 addProfileRT(getToolChain(), Args, CmdArgs);
Nick Lewycky2e95a6d2011-05-24 21:54:59 +00007827
Stephen Hinesc568f1e2014-07-21 00:47:37 -07007828 const char *Exec = Args.MakeArgString(getToolChain().GetLinkerPath());
Stephen Hines176edba2014-12-01 14:53:08 -08007829 C.addCommand(llvm::make_unique<Command>(JA, *this, Exec, CmdArgs));
Daniel Dunbar11e1b402009-05-02 18:28:39 +00007830}
Michael J. Spencerff58e362010-08-21 21:55:07 +00007831
Stephen Hines6bcf27b2014-05-29 04:14:42 -07007832static void addSanitizerRTWindows(const ToolChain &TC, const ArgList &Args,
7833 ArgStringList &CmdArgs,
Stephen Hines176edba2014-12-01 14:53:08 -08007834 StringRef RTName) {
Stephen Hines6bcf27b2014-05-29 04:14:42 -07007835 SmallString<128> LibSanitizer(getCompilerRTLibDir(TC));
7836 llvm::sys::path::append(LibSanitizer,
7837 Twine("clang_rt.") + RTName + ".lib");
7838 CmdArgs.push_back(Args.MakeArgString(LibSanitizer));
7839}
7840
Stephen Hines176edba2014-12-01 14:53:08 -08007841// Try to find Exe from a Visual Studio distribution. This first tries to find
7842// an installed copy of Visual Studio and, failing that, looks in the PATH,
7843// making sure that whatever executable that's found is not a same-named exe
7844// from clang itself to prevent clang from falling back to itself.
7845static std::string FindVisualStudioExecutable(const ToolChain &TC,
7846 const char *Exe,
7847 const char *ClangProgramPath) {
7848 const auto &MSVC = static_cast<const toolchains::MSVCToolChain &>(TC);
7849 std::string visualStudioBinDir;
7850 if (MSVC.getVisualStudioBinariesFolder(ClangProgramPath,
7851 visualStudioBinDir)) {
7852 SmallString<128> FilePath(visualStudioBinDir);
7853 llvm::sys::path::append(FilePath, Exe);
7854 if (llvm::sys::fs::can_execute(FilePath.c_str()))
7855 return FilePath.str();
7856 }
7857
7858 return Exe;
7859}
7860
Michael J. Spencerff58e362010-08-21 21:55:07 +00007861void visualstudio::Link::ConstructJob(Compilation &C, const JobAction &JA,
7862 const InputInfo &Output,
7863 const InputInfoList &Inputs,
7864 const ArgList &Args,
7865 const char *LinkingOutput) const {
Michael J. Spencerff58e362010-08-21 21:55:07 +00007866 ArgStringList CmdArgs;
7867
7868 if (Output.isFilename()) {
Daniel Dunbare5a37f42010-09-17 00:45:02 +00007869 CmdArgs.push_back(Args.MakeArgString(std::string("-out:") +
7870 Output.getFilename()));
Michael J. Spencerff58e362010-08-21 21:55:07 +00007871 } else {
7872 assert(Output.isNothing() && "Invalid output.");
7873 }
7874
7875 if (!Args.hasArg(options::OPT_nostdlib) &&
Hans Wennborg746974d2013-08-09 17:38:42 +00007876 !Args.hasArg(options::OPT_nostartfiles) &&
7877 !C.getDriver().IsCLMode()) {
Michael J. Spencerff58e362010-08-21 21:55:07 +00007878 CmdArgs.push_back("-defaultlib:libcmt");
7879 }
7880
Stephen Hines176edba2014-12-01 14:53:08 -08007881 if (!llvm::sys::Process::GetEnv("LIB")) {
7882 // If the VC environment hasn't been configured (perhaps because the user
7883 // did not run vcvarsall), try to build a consistent link environment. If
7884 // the environment variable is set however, assume the user knows what he's
7885 // doing.
7886 std::string VisualStudioDir;
7887 const auto &MSVC = static_cast<const toolchains::MSVCToolChain &>(getToolChain());
7888 if (MSVC.getVisualStudioInstallDir(VisualStudioDir)) {
7889 SmallString<128> LibDir(VisualStudioDir);
7890 llvm::sys::path::append(LibDir, "VC", "lib");
7891 switch (MSVC.getArch()) {
7892 case llvm::Triple::x86:
7893 // x86 just puts the libraries directly in lib
7894 break;
7895 case llvm::Triple::x86_64:
7896 llvm::sys::path::append(LibDir, "amd64");
7897 break;
7898 case llvm::Triple::arm:
7899 llvm::sys::path::append(LibDir, "arm");
7900 break;
7901 default:
7902 break;
7903 }
7904 CmdArgs.push_back(
7905 Args.MakeArgString(std::string("-libpath:") + LibDir.c_str()));
7906 }
7907
7908 std::string WindowsSdkLibPath;
7909 if (MSVC.getWindowsSDKLibraryPath(WindowsSdkLibPath))
7910 CmdArgs.push_back(Args.MakeArgString(std::string("-libpath:") +
7911 WindowsSdkLibPath.c_str()));
7912 }
7913
Michael J. Spencerff58e362010-08-21 21:55:07 +00007914 CmdArgs.push_back("-nologo");
7915
Stephen Hines6bcf27b2014-05-29 04:14:42 -07007916 if (Args.hasArg(options::OPT_g_Group)) {
7917 CmdArgs.push_back("-debug");
7918 }
7919
Hans Wennborg6d0a8d52013-09-10 20:18:04 +00007920 bool DLL = Args.hasArg(options::OPT__SLASH_LD, options::OPT__SLASH_LDd);
7921
7922 if (DLL) {
7923 CmdArgs.push_back(Args.MakeArgString("-dll"));
7924
7925 SmallString<128> ImplibName(Output.getFilename());
7926 llvm::sys::path::replace_extension(ImplibName, "lib");
7927 CmdArgs.push_back(Args.MakeArgString(std::string("-implib:") +
7928 ImplibName.str()));
7929 }
7930
Peter Collingbournec6911a22013-11-01 18:16:25 +00007931 if (getToolChain().getSanitizerArgs().needsAsanRt()) {
Hans Wennborg324cc032013-08-28 17:36:07 +00007932 CmdArgs.push_back(Args.MakeArgString("-debug"));
Hans Wennborg2ddffa12013-08-30 10:50:52 +00007933 CmdArgs.push_back(Args.MakeArgString("-incremental:no"));
Hans Wennborg6d0a8d52013-09-10 20:18:04 +00007934 // FIXME: Handle 64-bit.
Stephen Hines176edba2014-12-01 14:53:08 -08007935 if (Args.hasArg(options::OPT__SLASH_MD, options::OPT__SLASH_MDd)) {
7936 addSanitizerRTWindows(getToolChain(), Args, CmdArgs, "asan_dynamic-i386");
7937 addSanitizerRTWindows(getToolChain(), Args, CmdArgs,
7938 "asan_dynamic_runtime_thunk-i386");
7939 // Make sure the dynamic runtime thunk is not optimized out at link time
7940 // to ensure proper SEH handling.
7941 CmdArgs.push_back(Args.MakeArgString("-include:___asan_seh_interceptor"));
7942 } else if (DLL) {
Stephen Hines6bcf27b2014-05-29 04:14:42 -07007943 addSanitizerRTWindows(getToolChain(), Args, CmdArgs,
7944 "asan_dll_thunk-i386");
7945 } else {
7946 addSanitizerRTWindows(getToolChain(), Args, CmdArgs, "asan-i386");
7947 addSanitizerRTWindows(getToolChain(), Args, CmdArgs, "asan_cxx-i386");
7948 }
Hans Wennborg3c4da0c2013-08-27 18:10:21 +00007949 }
7950
Hans Wennborg5db95272013-08-13 23:38:57 +00007951 Args.AddAllArgValues(CmdArgs, options::OPT__SLASH_link);
Michael J. Spencera2284f52012-06-18 16:56:04 +00007952
Stephen Hines176edba2014-12-01 14:53:08 -08007953 // Add filenames, libraries, and other linker inputs.
7954 for (const auto &Input : Inputs) {
7955 if (Input.isFilename()) {
Stephen Hinesc568f1e2014-07-21 00:47:37 -07007956 CmdArgs.push_back(Input.getFilename());
Stephen Hines176edba2014-12-01 14:53:08 -08007957 continue;
7958 }
Michael J. Spencerff58e362010-08-21 21:55:07 +00007959
Stephen Hines176edba2014-12-01 14:53:08 -08007960 const Arg &A = Input.getInputArg();
7961
7962 // Render -l options differently for the MSVC linker.
7963 if (A.getOption().matches(options::OPT_l)) {
7964 StringRef Lib = A.getValue();
7965 const char *LinkLibArg;
7966 if (Lib.endswith(".lib"))
7967 LinkLibArg = Args.MakeArgString(Lib);
7968 else
7969 LinkLibArg = Args.MakeArgString(Lib + ".lib");
7970 CmdArgs.push_back(LinkLibArg);
7971 continue;
7972 }
7973
7974 // Otherwise, this is some other kind of linker input option like -Wl, -z,
7975 // or -L. Render it, even if MSVC doesn't understand it.
7976 A.renderAsInput(Args, CmdArgs);
7977 }
7978
7979 // It's not sufficient to just use link from the program PATH, because other
7980 // environments like GnuWin32 install their own link.exe which may come first.
7981 llvm::SmallString<128> linkPath(FindVisualStudioExecutable(
7982 getToolChain(), "link.exe", C.getDriver().getClangProgramPath()));
7983 const char *Exec = Args.MakeArgString(linkPath);
7984 C.addCommand(llvm::make_unique<Command>(JA, *this, Exec, CmdArgs));
Michael J. Spencerff58e362010-08-21 21:55:07 +00007985}
Hans Wennborgc8ba0a02013-09-19 20:32:16 +00007986
7987void visualstudio::Compile::ConstructJob(Compilation &C, const JobAction &JA,
7988 const InputInfo &Output,
7989 const InputInfoList &Inputs,
7990 const ArgList &Args,
7991 const char *LinkingOutput) const {
7992 C.addCommand(GetCommand(C, JA, Output, Inputs, Args, LinkingOutput));
7993}
7994
Stephen Hines176edba2014-12-01 14:53:08 -08007995std::unique_ptr<Command> visualstudio::Compile::GetCommand(
7996 Compilation &C, const JobAction &JA, const InputInfo &Output,
7997 const InputInfoList &Inputs, const ArgList &Args,
7998 const char *LinkingOutput) const {
Hans Wennborgc8ba0a02013-09-19 20:32:16 +00007999 ArgStringList CmdArgs;
Hans Wennborg1413d622013-09-24 17:36:21 +00008000 CmdArgs.push_back("/nologo");
Hans Wennborgc8ba0a02013-09-19 20:32:16 +00008001 CmdArgs.push_back("/c"); // Compile only.
8002 CmdArgs.push_back("/W0"); // No warnings.
8003
8004 // The goal is to be able to invoke this tool correctly based on
8005 // any flag accepted by clang-cl.
8006
8007 // These are spelled the same way in clang and cl.exe,.
8008 Args.AddAllArgs(CmdArgs, options::OPT_D, options::OPT_U);
8009 Args.AddAllArgs(CmdArgs, options::OPT_I);
Hans Wennborga8ffc162013-09-24 18:17:21 +00008010
8011 // Optimization level.
8012 if (Arg *A = Args.getLastArg(options::OPT_O, options::OPT_O0)) {
8013 if (A->getOption().getID() == options::OPT_O0) {
8014 CmdArgs.push_back("/Od");
8015 } else {
8016 StringRef OptLevel = A->getValue();
8017 if (OptLevel == "1" || OptLevel == "2" || OptLevel == "s")
8018 A->render(Args, CmdArgs);
8019 else if (OptLevel == "3")
8020 CmdArgs.push_back("/Ox");
8021 }
8022 }
Hans Wennborgc8ba0a02013-09-19 20:32:16 +00008023
8024 // Flags for which clang-cl have an alias.
8025 // FIXME: How can we ensure this stays in sync with relevant clang-cl options?
8026
Stephen Hinesc568f1e2014-07-21 00:47:37 -07008027 if (Args.hasFlag(options::OPT__SLASH_GR_, options::OPT__SLASH_GR,
8028 /*default=*/false))
8029 CmdArgs.push_back("/GR-");
Stephen Hines651f13c2014-04-23 16:59:28 -07008030 if (Arg *A = Args.getLastArg(options::OPT_ffunction_sections,
8031 options::OPT_fno_function_sections))
8032 CmdArgs.push_back(A->getOption().getID() == options::OPT_ffunction_sections
8033 ? "/Gy"
8034 : "/Gy-");
Stephen Hines6bcf27b2014-05-29 04:14:42 -07008035 if (Arg *A = Args.getLastArg(options::OPT_fdata_sections,
8036 options::OPT_fno_data_sections))
8037 CmdArgs.push_back(
8038 A->getOption().getID() == options::OPT_fdata_sections ? "/Gw" : "/Gw-");
Hans Wennborgc8ba0a02013-09-19 20:32:16 +00008039 if (Args.hasArg(options::OPT_fsyntax_only))
8040 CmdArgs.push_back("/Zs");
Stephen Hines651f13c2014-04-23 16:59:28 -07008041 if (Args.hasArg(options::OPT_g_Flag, options::OPT_gline_tables_only))
8042 CmdArgs.push_back("/Z7");
Hans Wennborgc8ba0a02013-09-19 20:32:16 +00008043
Hans Wennborg4fe475a2013-09-27 17:54:18 +00008044 std::vector<std::string> Includes = Args.getAllArgValues(options::OPT_include);
Stephen Hinesc568f1e2014-07-21 00:47:37 -07008045 for (const auto &Include : Includes)
8046 CmdArgs.push_back(Args.MakeArgString(std::string("/FI") + Include));
Hans Wennborg4fe475a2013-09-27 17:54:18 +00008047
Hans Wennborgc8ba0a02013-09-19 20:32:16 +00008048 // Flags that can simply be passed through.
8049 Args.AddAllArgs(CmdArgs, options::OPT__SLASH_LD);
8050 Args.AddAllArgs(CmdArgs, options::OPT__SLASH_LDd);
Stephen Hinesc568f1e2014-07-21 00:47:37 -07008051 Args.AddAllArgs(CmdArgs, options::OPT__SLASH_EH);
Hans Wennborgc8ba0a02013-09-19 20:32:16 +00008052
8053 // The order of these flags is relevant, so pick the last one.
8054 if (Arg *A = Args.getLastArg(options::OPT__SLASH_MD, options::OPT__SLASH_MDd,
8055 options::OPT__SLASH_MT, options::OPT__SLASH_MTd))
8056 A->render(Args, CmdArgs);
8057
8058
8059 // Input filename.
8060 assert(Inputs.size() == 1);
8061 const InputInfo &II = Inputs[0];
8062 assert(II.getType() == types::TY_C || II.getType() == types::TY_CXX);
8063 CmdArgs.push_back(II.getType() == types::TY_C ? "/Tc" : "/Tp");
8064 if (II.isFilename())
8065 CmdArgs.push_back(II.getFilename());
8066 else
8067 II.getInputArg().renderAsInput(Args, CmdArgs);
8068
8069 // Output filename.
8070 assert(Output.getType() == types::TY_Object);
8071 const char *Fo = Args.MakeArgString(std::string("/Fo") +
8072 Output.getFilename());
8073 CmdArgs.push_back(Fo);
8074
Hans Wennborgdc40bf92013-09-20 18:16:35 +00008075 const Driver &D = getToolChain().getDriver();
Stephen Hines176edba2014-12-01 14:53:08 -08008076 std::string Exec = FindVisualStudioExecutable(getToolChain(), "cl.exe",
8077 D.getClangProgramPath());
8078 return llvm::make_unique<Command>(JA, *this, Args.MakeArgString(Exec),
8079 CmdArgs);
Hans Wennborgc8ba0a02013-09-19 20:32:16 +00008080}
Robert Lytton4e490e22013-10-11 10:29:40 +00008081
8082
8083/// XCore Tools
8084// We pass assemble and link construction to the xcc tool.
8085
8086void XCore::Assemble::ConstructJob(Compilation &C, const JobAction &JA,
8087 const InputInfo &Output,
8088 const InputInfoList &Inputs,
8089 const ArgList &Args,
8090 const char *LinkingOutput) const {
8091 ArgStringList CmdArgs;
8092
8093 CmdArgs.push_back("-o");
8094 CmdArgs.push_back(Output.getFilename());
8095
8096 CmdArgs.push_back("-c");
8097
Stephen Hines651f13c2014-04-23 16:59:28 -07008098 if (Args.hasArg(options::OPT_v))
8099 CmdArgs.push_back("-v");
8100
Stephen Hines6bcf27b2014-05-29 04:14:42 -07008101 if (Arg *A = Args.getLastArg(options::OPT_g_Group))
8102 if (!A->getOption().matches(options::OPT_g0))
8103 CmdArgs.push_back("-g");
Stephen Hines651f13c2014-04-23 16:59:28 -07008104
8105 if (Args.hasFlag(options::OPT_fverbose_asm, options::OPT_fno_verbose_asm,
8106 false))
8107 CmdArgs.push_back("-fverbose-asm");
Robert Lytton4e490e22013-10-11 10:29:40 +00008108
8109 Args.AddAllArgValues(CmdArgs, options::OPT_Wa_COMMA,
8110 options::OPT_Xassembler);
8111
Stephen Hinesc568f1e2014-07-21 00:47:37 -07008112 for (const auto &II : Inputs)
Robert Lytton4e490e22013-10-11 10:29:40 +00008113 CmdArgs.push_back(II.getFilename());
Robert Lytton4e490e22013-10-11 10:29:40 +00008114
Stephen Hinesc568f1e2014-07-21 00:47:37 -07008115 const char *Exec = Args.MakeArgString(getToolChain().GetProgramPath("xcc"));
Stephen Hines176edba2014-12-01 14:53:08 -08008116 C.addCommand(llvm::make_unique<Command>(JA, *this, Exec, CmdArgs));
Robert Lytton4e490e22013-10-11 10:29:40 +00008117}
8118
8119void XCore::Link::ConstructJob(Compilation &C, const JobAction &JA,
8120 const InputInfo &Output,
8121 const InputInfoList &Inputs,
8122 const ArgList &Args,
8123 const char *LinkingOutput) const {
8124 ArgStringList CmdArgs;
8125
8126 if (Output.isFilename()) {
8127 CmdArgs.push_back("-o");
8128 CmdArgs.push_back(Output.getFilename());
8129 } else {
8130 assert(Output.isNothing() && "Invalid output.");
8131 }
8132
Stephen Hines651f13c2014-04-23 16:59:28 -07008133 if (Args.hasArg(options::OPT_v))
8134 CmdArgs.push_back("-v");
8135
8136 ExceptionSettings EH = exceptionSettings(Args, getToolChain().getTriple());
8137 if (EH.ShouldUseExceptionTables)
8138 CmdArgs.push_back("-fexceptions");
8139
Robert Lytton4e490e22013-10-11 10:29:40 +00008140 AddLinkerInputs(getToolChain(), Inputs, Args, CmdArgs);
8141
Stephen Hinesc568f1e2014-07-21 00:47:37 -07008142 const char *Exec = Args.MakeArgString(getToolChain().GetProgramPath("xcc"));
Stephen Hines176edba2014-12-01 14:53:08 -08008143 C.addCommand(llvm::make_unique<Command>(JA, *this, Exec, CmdArgs));
Robert Lytton4e490e22013-10-11 10:29:40 +00008144}
Stephen Hines176edba2014-12-01 14:53:08 -08008145
8146void CrossWindows::Assemble::ConstructJob(Compilation &C, const JobAction &JA,
8147 const InputInfo &Output,
8148 const InputInfoList &Inputs,
8149 const ArgList &Args,
8150 const char *LinkingOutput) const {
8151 const auto &TC =
8152 static_cast<const toolchains::CrossWindowsToolChain &>(getToolChain());
8153 ArgStringList CmdArgs;
8154 const char *Exec;
8155
8156 switch (TC.getArch()) {
8157 default: llvm_unreachable("unsupported architecture");
8158 case llvm::Triple::arm:
8159 case llvm::Triple::thumb:
8160 break;
8161 case llvm::Triple::x86:
8162 CmdArgs.push_back("--32");
8163 break;
8164 case llvm::Triple::x86_64:
8165 CmdArgs.push_back("--64");
8166 break;
8167 }
8168
8169 Args.AddAllArgValues(CmdArgs, options::OPT_Wa_COMMA, options::OPT_Xassembler);
8170
8171 CmdArgs.push_back("-o");
8172 CmdArgs.push_back(Output.getFilename());
8173
8174 for (const auto &Input : Inputs)
8175 CmdArgs.push_back(Input.getFilename());
8176
8177 const std::string Assembler = TC.GetProgramPath("as");
8178 Exec = Args.MakeArgString(Assembler);
8179
8180 C.addCommand(llvm::make_unique<Command>(JA, *this, Exec, CmdArgs));
8181}
8182
8183void CrossWindows::Link::ConstructJob(Compilation &C, const JobAction &JA,
8184 const InputInfo &Output,
8185 const InputInfoList &Inputs,
8186 const ArgList &Args,
8187 const char *LinkingOutput) const {
8188 const auto &TC =
8189 static_cast<const toolchains::CrossWindowsToolChain &>(getToolChain());
8190 const llvm::Triple &T = TC.getTriple();
8191 const Driver &D = TC.getDriver();
8192 SmallString<128> EntryPoint;
8193 ArgStringList CmdArgs;
8194 const char *Exec;
8195
8196 // Silence warning for "clang -g foo.o -o foo"
8197 Args.ClaimAllArgs(options::OPT_g_Group);
8198 // and "clang -emit-llvm foo.o -o foo"
8199 Args.ClaimAllArgs(options::OPT_emit_llvm);
8200 // and for "clang -w foo.o -o foo"
8201 Args.ClaimAllArgs(options::OPT_w);
8202 // Other warning options are already handled somewhere else.
8203
8204 if (!D.SysRoot.empty())
8205 CmdArgs.push_back(Args.MakeArgString("--sysroot=" + D.SysRoot));
8206
8207 if (Args.hasArg(options::OPT_pie))
8208 CmdArgs.push_back("-pie");
8209 if (Args.hasArg(options::OPT_rdynamic))
8210 CmdArgs.push_back("-export-dynamic");
8211 if (Args.hasArg(options::OPT_s))
8212 CmdArgs.push_back("--strip-all");
8213
8214 CmdArgs.push_back("-m");
8215 switch (TC.getArch()) {
8216 default: llvm_unreachable("unsupported architecture");
8217 case llvm::Triple::arm:
8218 case llvm::Triple::thumb:
8219 // FIXME: this is incorrect for WinCE
8220 CmdArgs.push_back("thumb2pe");
8221 break;
8222 case llvm::Triple::x86:
8223 CmdArgs.push_back("i386pe");
8224 EntryPoint.append("_");
8225 break;
8226 case llvm::Triple::x86_64:
8227 CmdArgs.push_back("i386pep");
8228 break;
8229 }
8230
8231 if (Args.hasArg(options::OPT_shared)) {
8232 switch (T.getArch()) {
8233 default: llvm_unreachable("unsupported architecture");
8234 case llvm::Triple::arm:
8235 case llvm::Triple::thumb:
8236 case llvm::Triple::x86_64:
8237 EntryPoint.append("_DllMainCRTStartup");
8238 break;
8239 case llvm::Triple::x86:
8240 EntryPoint.append("_DllMainCRTStartup@12");
8241 break;
8242 }
8243
8244 CmdArgs.push_back("-shared");
8245 CmdArgs.push_back("-Bdynamic");
8246
8247 CmdArgs.push_back("--enable-auto-image-base");
8248
8249 CmdArgs.push_back("--entry");
8250 CmdArgs.push_back(Args.MakeArgString(EntryPoint));
8251 } else {
8252 EntryPoint.append("mainCRTStartup");
8253
8254 CmdArgs.push_back(Args.hasArg(options::OPT_static) ? "-Bstatic"
8255 : "-Bdynamic");
8256
8257 if (!Args.hasArg(options::OPT_nostdlib) &&
8258 !Args.hasArg(options::OPT_nostartfiles)) {
8259 CmdArgs.push_back("--entry");
8260 CmdArgs.push_back(Args.MakeArgString(EntryPoint));
8261 }
8262
8263 // FIXME: handle subsystem
8264 }
8265
8266 // NOTE: deal with multiple definitions on Windows (e.g. COMDAT)
8267 CmdArgs.push_back("--allow-multiple-definition");
8268
8269 CmdArgs.push_back("-o");
8270 CmdArgs.push_back(Output.getFilename());
8271
8272 if (Args.hasArg(options::OPT_shared) || Args.hasArg(options::OPT_rdynamic)) {
8273 SmallString<261> ImpLib(Output.getFilename());
8274 llvm::sys::path::replace_extension(ImpLib, ".lib");
8275
8276 CmdArgs.push_back("--out-implib");
8277 CmdArgs.push_back(Args.MakeArgString(ImpLib));
8278 }
8279
8280 if (!Args.hasArg(options::OPT_nostdlib) &&
8281 !Args.hasArg(options::OPT_nostartfiles)) {
8282 const std::string CRTPath(D.SysRoot + "/usr/lib/");
8283 const char *CRTBegin;
8284
8285 CRTBegin =
8286 Args.hasArg(options::OPT_shared) ? "crtbeginS.obj" : "crtbegin.obj";
8287 CmdArgs.push_back(Args.MakeArgString(CRTPath + CRTBegin));
8288 }
8289
8290 Args.AddAllArgs(CmdArgs, options::OPT_L);
8291
8292 const auto &Paths = TC.getFilePaths();
8293 for (const auto &Path : Paths)
8294 CmdArgs.push_back(Args.MakeArgString(StringRef("-L") + Path));
8295
8296 AddLinkerInputs(TC, Inputs, Args, CmdArgs);
8297
8298 if (D.CCCIsCXX() && !Args.hasArg(options::OPT_nostdlib) &&
8299 !Args.hasArg(options::OPT_nodefaultlibs)) {
8300 bool StaticCXX = Args.hasArg(options::OPT_static_libstdcxx) &&
8301 !Args.hasArg(options::OPT_static);
8302 if (StaticCXX)
8303 CmdArgs.push_back("-Bstatic");
8304 TC.AddCXXStdlibLibArgs(Args, CmdArgs);
8305 if (StaticCXX)
8306 CmdArgs.push_back("-Bdynamic");
8307 }
8308
8309 if (!Args.hasArg(options::OPT_nostdlib)) {
8310 if (!Args.hasArg(options::OPT_nodefaultlibs)) {
8311 // TODO handle /MT[d] /MD[d]
8312 CmdArgs.push_back("-lmsvcrt");
8313 AddRunTimeLibs(TC, D, CmdArgs, Args);
8314 }
8315 }
8316
8317 const std::string Linker = TC.GetProgramPath("ld");
8318 Exec = Args.MakeArgString(Linker);
8319
8320 C.addCommand(llvm::make_unique<Command>(JA, *this, Exec, CmdArgs));
8321}
8322