blob: 28b90686072dd029819e2f8469786531b6ac10ad [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 Hinesef822542014-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 Hinesef822542014-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 Hinesef822542014-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 Hinesef822542014-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 Hinesef822542014-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 Hinesef822542014-07-21 00:47:37 -0700934
935 // Setting -mno-global-merge disables the codegen global merge pass. Setting
936 // -mglobal-merge has no effect as the pass is enabled by default.
937 if (Arg *A = Args.getLastArg(options::OPT_mglobal_merge,
938 options::OPT_mno_global_merge)) {
939 if (A->getOption().matches(options::OPT_mno_global_merge))
940 CmdArgs.push_back("-mno-global-merge");
941 }
Daniel Dunbarb163ef72009-09-10 04:57:17 +0000942}
943
Simon Atanasyana2768be2012-04-07 22:09:23 +0000944// Get CPU and ABI names. They are not independent
945// so we have to calculate them together.
Stephen Hines176edba2014-12-01 14:53:08 -0800946void mips::getMipsCPUAndABI(const ArgList &Args,
947 const llvm::Triple &Triple,
948 StringRef &CPUName,
949 StringRef &ABIName) {
Stephen Hines651f13c2014-04-23 16:59:28 -0700950 const char *DefMips32CPU = "mips32r2";
951 const char *DefMips64CPU = "mips64r2";
Akira Hatanaka9f360622011-09-26 21:07:52 +0000952
Stephen Hinesef822542014-07-21 00:47:37 -0700953 // MIPS32r6 is the default for mips(el)?-img-linux-gnu and MIPS64r6 is the
954 // default for mips64(el)?-img-linux-gnu.
955 if (Triple.getVendor() == llvm::Triple::ImaginationTechnologies &&
956 Triple.getEnvironment() == llvm::Triple::GNU) {
957 DefMips32CPU = "mips32r6";
958 DefMips64CPU = "mips64r6";
959 }
960
Simon Atanasyan89d83ff2012-09-10 08:32:41 +0000961 if (Arg *A = Args.getLastArg(options::OPT_march_EQ,
Simon Atanasyan66751bc2013-10-09 12:12:24 +0000962 options::OPT_mcpu_EQ))
963 CPUName = A->getValue();
Simon Atanasyan89d83ff2012-09-10 08:32:41 +0000964
Simon Atanasyanc7e2a4e2013-04-21 13:30:10 +0000965 if (Arg *A = Args.getLastArg(options::OPT_mabi_EQ)) {
Richard Smith1d489cf2012-11-01 04:30:05 +0000966 ABIName = A->getValue();
Simon Atanasyanc7e2a4e2013-04-21 13:30:10 +0000967 // Convert a GNU style Mips ABI name to the name
968 // accepted by LLVM Mips backend.
969 ABIName = llvm::StringSwitch<llvm::StringRef>(ABIName)
970 .Case("32", "o32")
971 .Case("64", "n64")
972 .Default(ABIName);
973 }
Simon Atanasyan89d83ff2012-09-10 08:32:41 +0000974
975 // Setup default CPU and ABI names.
976 if (CPUName.empty() && ABIName.empty()) {
Rafael Espindolab330e402013-08-20 22:12:08 +0000977 switch (Triple.getArch()) {
Simon Atanasyan89d83ff2012-09-10 08:32:41 +0000978 default:
979 llvm_unreachable("Unexpected triple arch name");
980 case llvm::Triple::mips:
981 case llvm::Triple::mipsel:
982 CPUName = DefMips32CPU;
983 break;
984 case llvm::Triple::mips64:
985 case llvm::Triple::mips64el:
986 CPUName = DefMips64CPU;
987 break;
988 }
989 }
990
Stephen Hinesef822542014-07-21 00:47:37 -0700991 if (ABIName.empty()) {
992 // Deduce ABI name from the target triple.
993 if (Triple.getArch() == llvm::Triple::mips ||
994 Triple.getArch() == llvm::Triple::mipsel)
995 ABIName = "o32";
996 else
997 ABIName = "n64";
Simon Atanasyan89d83ff2012-09-10 08:32:41 +0000998 }
999
Stephen Hinesef822542014-07-21 00:47:37 -07001000 if (CPUName.empty()) {
1001 // Deduce CPU name from ABI name.
1002 CPUName = llvm::StringSwitch<const char *>(ABIName)
1003 .Cases("o32", "eabi", DefMips32CPU)
1004 .Cases("n32", "n64", DefMips64CPU)
1005 .Default("");
1006 }
Stephen Hines176edba2014-12-01 14:53:08 -08001007
1008 // FIXME: Warn on inconsistent use of -march and -mabi.
Simon Atanasyana2768be2012-04-07 22:09:23 +00001009}
1010
Simon Atanasyane9616a42013-02-27 14:55:49 +00001011// Convert ABI name to the GNU tools acceptable variant.
1012static StringRef getGnuCompatibleMipsABIName(StringRef ABI) {
1013 return llvm::StringSwitch<llvm::StringRef>(ABI)
1014 .Case("o32", "32")
1015 .Case("n64", "64")
1016 .Default(ABI);
1017}
1018
Simon Atanasyan5e627792012-06-02 15:06:29 +00001019// Select the MIPS float ABI as determined by -msoft-float, -mhard-float,
1020// and -mfloat-abi=.
1021static StringRef getMipsFloatABI(const Driver &D, const ArgList &Args) {
Chris Lattner5f9e2722011-07-23 10:55:15 +00001022 StringRef FloatABI;
Eric Christophered734732010-03-02 02:41:08 +00001023 if (Arg *A = Args.getLastArg(options::OPT_msoft_float,
Akira Hatanakaad8d8a32012-03-23 23:07:09 +00001024 options::OPT_mhard_float,
1025 options::OPT_mfloat_abi_EQ)) {
Eric Christophered734732010-03-02 02:41:08 +00001026 if (A->getOption().matches(options::OPT_msoft_float))
1027 FloatABI = "soft";
1028 else if (A->getOption().matches(options::OPT_mhard_float))
1029 FloatABI = "hard";
Akira Hatanakaad8d8a32012-03-23 23:07:09 +00001030 else {
Richard Smith1d489cf2012-11-01 04:30:05 +00001031 FloatABI = A->getValue();
Simon Atanasyan2ed42b82013-04-14 08:37:15 +00001032 if (FloatABI != "soft" && FloatABI != "hard") {
Simon Atanasyan5e627792012-06-02 15:06:29 +00001033 D.Diag(diag::err_drv_invalid_mfloat_abi) << A->getAsString(Args);
Akira Hatanakaad8d8a32012-03-23 23:07:09 +00001034 FloatABI = "hard";
1035 }
1036 }
Eric Christophered734732010-03-02 02:41:08 +00001037 }
1038
1039 // If unspecified, choose the default based on the platform.
1040 if (FloatABI.empty()) {
Akira Hatanakaad8d8a32012-03-23 23:07:09 +00001041 // Assume "hard", because it's a default value used by gcc.
1042 // When we start to recognize specific target MIPS processors,
1043 // we will be able to select the default more correctly.
1044 FloatABI = "hard";
Eric Christophered734732010-03-02 02:41:08 +00001045 }
1046
Simon Atanasyan5e627792012-06-02 15:06:29 +00001047 return FloatABI;
1048}
1049
Simon Atanasyandc536f52012-07-05 18:51:43 +00001050static void AddTargetFeature(const ArgList &Args,
Rafael Espindola146dbbf2013-08-21 16:39:20 +00001051 std::vector<const char *> &Features,
1052 OptSpecifier OnOpt, OptSpecifier OffOpt,
Simon Atanasyandc536f52012-07-05 18:51:43 +00001053 StringRef FeatureName) {
1054 if (Arg *A = Args.getLastArg(OnOpt, OffOpt)) {
Simon Atanasyandc536f52012-07-05 18:51:43 +00001055 if (A->getOption().matches(OnOpt))
Rafael Espindola146dbbf2013-08-21 16:39:20 +00001056 Features.push_back(Args.MakeArgString("+" + FeatureName));
Simon Atanasyandc536f52012-07-05 18:51:43 +00001057 else
Rafael Espindola146dbbf2013-08-21 16:39:20 +00001058 Features.push_back(Args.MakeArgString("-" + FeatureName));
Simon Atanasyandc536f52012-07-05 18:51:43 +00001059 }
1060}
1061
Stephen Hines176edba2014-12-01 14:53:08 -08001062static void getMIPSTargetFeatures(const Driver &D, const llvm::Triple &Triple,
1063 const ArgList &Args,
Rafael Espindola146dbbf2013-08-21 16:39:20 +00001064 std::vector<const char *> &Features) {
Stephen Hines176edba2014-12-01 14:53:08 -08001065 StringRef CPUName;
1066 StringRef ABIName;
1067 mips::getMipsCPUAndABI(Args, Triple, CPUName, ABIName);
1068 ABIName = getGnuCompatibleMipsABIName(ABIName);
1069
1070 // Always override the backend's default ABI.
1071 std::string ABIFeature = llvm::StringSwitch<StringRef>(ABIName)
1072 .Case("32", "+o32")
1073 .Case("n32", "+n32")
1074 .Case("64", "+n64")
1075 .Case("eabi", "+eabi")
1076 .Default(("+" + ABIName).str());
1077 Features.push_back("-o32");
1078 Features.push_back("-n64");
1079 Features.push_back(Args.MakeArgString(ABIFeature));
1080
1081 AddTargetFeature(Args, Features, options::OPT_mno_abicalls,
1082 options::OPT_mabicalls, "noabicalls");
1083
Rafael Espindola146dbbf2013-08-21 16:39:20 +00001084 StringRef FloatABI = getMipsFloatABI(D, Args);
Stephen Hines651f13c2014-04-23 16:59:28 -07001085 if (FloatABI == "soft") {
Rafael Espindola146dbbf2013-08-21 16:39:20 +00001086 // FIXME: Note, this is a hack. We need to pass the selected float
1087 // mode to the MipsTargetInfoBase to define appropriate macros there.
1088 // Now it is the only method.
1089 Features.push_back("+soft-float");
1090 }
1091
Simon Atanasyanfc12c4a2013-09-24 09:09:16 +00001092 if (Arg *A = Args.getLastArg(options::OPT_mnan_EQ)) {
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001093 StringRef Val = StringRef(A->getValue());
1094 if (Val == "2008")
Simon Atanasyanfc12c4a2013-09-24 09:09:16 +00001095 Features.push_back("+nan2008");
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001096 else if (Val == "legacy")
1097 Features.push_back("-nan2008");
1098 else
1099 D.Diag(diag::err_drv_unsupported_option_argument)
1100 << A->getOption().getName() << Val;
Simon Atanasyanfc12c4a2013-09-24 09:09:16 +00001101 }
1102
Rafael Espindola146dbbf2013-08-21 16:39:20 +00001103 AddTargetFeature(Args, Features, options::OPT_msingle_float,
1104 options::OPT_mdouble_float, "single-float");
1105 AddTargetFeature(Args, Features, options::OPT_mips16, options::OPT_mno_mips16,
1106 "mips16");
1107 AddTargetFeature(Args, Features, options::OPT_mmicromips,
1108 options::OPT_mno_micromips, "micromips");
1109 AddTargetFeature(Args, Features, options::OPT_mdsp, options::OPT_mno_dsp,
1110 "dsp");
1111 AddTargetFeature(Args, Features, options::OPT_mdspr2, options::OPT_mno_dspr2,
1112 "dspr2");
1113 AddTargetFeature(Args, Features, options::OPT_mmsa, options::OPT_mno_msa,
1114 "msa");
Stephen Hines176edba2014-12-01 14:53:08 -08001115
1116 // Add the last -mfp32/-mfpxx/-mfp64 or if none are given and the ABI is O32
1117 // pass -mfpxx
1118 if (Arg *A = Args.getLastArg(options::OPT_mfp32, options::OPT_mfpxx,
1119 options::OPT_mfp64)) {
1120 if (A->getOption().matches(options::OPT_mfp32))
1121 Features.push_back(Args.MakeArgString("-fp64"));
1122 else if (A->getOption().matches(options::OPT_mfpxx)) {
1123 Features.push_back(Args.MakeArgString("+fpxx"));
1124 Features.push_back(Args.MakeArgString("+nooddspreg"));
1125 } else
1126 Features.push_back(Args.MakeArgString("+fp64"));
1127 } else if (mips::isFPXXDefault(Triple, CPUName, ABIName)) {
1128 Features.push_back(Args.MakeArgString("+fpxx"));
1129 Features.push_back(Args.MakeArgString("+nooddspreg"));
1130 }
1131
Stephen Hinesef822542014-07-21 00:47:37 -07001132 AddTargetFeature(Args, Features, options::OPT_mno_odd_spreg,
1133 options::OPT_modd_spreg, "nooddspreg");
Rafael Espindola146dbbf2013-08-21 16:39:20 +00001134}
1135
Simon Atanasyan5e627792012-06-02 15:06:29 +00001136void Clang::AddMIPSTargetArgs(const ArgList &Args,
Simon Atanasyana8141612013-04-14 14:07:41 +00001137 ArgStringList &CmdArgs) const {
Simon Atanasyan5e627792012-06-02 15:06:29 +00001138 const Driver &D = getToolChain().getDriver();
1139 StringRef CPUName;
1140 StringRef ABIName;
Rafael Espindolab330e402013-08-20 22:12:08 +00001141 const llvm::Triple &Triple = getToolChain().getTriple();
Stephen Hines176edba2014-12-01 14:53:08 -08001142 mips::getMipsCPUAndABI(Args, Triple, CPUName, ABIName);
Simon Atanasyan5e627792012-06-02 15:06:29 +00001143
1144 CmdArgs.push_back("-target-abi");
1145 CmdArgs.push_back(ABIName.data());
1146
1147 StringRef FloatABI = getMipsFloatABI(D, Args);
1148
Stephen Hines651f13c2014-04-23 16:59:28 -07001149 if (FloatABI == "soft") {
Eric Christophered734732010-03-02 02:41:08 +00001150 // Floating point operations and argument passing are soft.
Eric Christophered734732010-03-02 02:41:08 +00001151 CmdArgs.push_back("-msoft-float");
Akira Hatanakaad8d8a32012-03-23 23:07:09 +00001152 CmdArgs.push_back("-mfloat-abi");
1153 CmdArgs.push_back("soft");
Akira Hatanakaad8d8a32012-03-23 23:07:09 +00001154 }
Akira Hatanakaad8d8a32012-03-23 23:07:09 +00001155 else {
1156 // Floating point operations and argument passing are hard.
Eric Christophered734732010-03-02 02:41:08 +00001157 assert(FloatABI == "hard" && "Invalid float abi!");
Akira Hatanakaad8d8a32012-03-23 23:07:09 +00001158 CmdArgs.push_back("-mfloat-abi");
1159 CmdArgs.push_back("hard");
Eric Christophered734732010-03-02 02:41:08 +00001160 }
Simon Atanasyan0b273ef2012-07-05 14:19:39 +00001161
Simon Atanasyanbda07ac2012-12-01 18:27:21 +00001162 if (Arg *A = Args.getLastArg(options::OPT_mxgot, options::OPT_mno_xgot)) {
1163 if (A->getOption().matches(options::OPT_mxgot)) {
1164 CmdArgs.push_back("-mllvm");
1165 CmdArgs.push_back("-mxgot");
1166 }
1167 }
1168
Simon Atanasyan6bdc4c62013-05-11 06:33:44 +00001169 if (Arg *A = Args.getLastArg(options::OPT_mldc1_sdc1,
1170 options::OPT_mno_ldc1_sdc1)) {
1171 if (A->getOption().matches(options::OPT_mno_ldc1_sdc1)) {
1172 CmdArgs.push_back("-mllvm");
1173 CmdArgs.push_back("-mno-ldc1-sdc1");
1174 }
1175 }
1176
Akira Hatanakacdbc3b32013-07-19 18:58:48 +00001177 if (Arg *A = Args.getLastArg(options::OPT_mcheck_zero_division,
1178 options::OPT_mno_check_zero_division)) {
1179 if (A->getOption().matches(options::OPT_mno_check_zero_division)) {
1180 CmdArgs.push_back("-mllvm");
1181 CmdArgs.push_back("-mno-check-zero-division");
1182 }
1183 }
1184
Simon Atanasyan9804b762012-08-27 20:55:56 +00001185 if (Arg *A = Args.getLastArg(options::OPT_G)) {
Richard Smith1d489cf2012-11-01 04:30:05 +00001186 StringRef v = A->getValue();
Simon Atanasyan9804b762012-08-27 20:55:56 +00001187 CmdArgs.push_back("-mllvm");
1188 CmdArgs.push_back(Args.MakeArgString("-mips-ssection-threshold=" + v));
1189 A->claim();
1190 }
Eric Christophered734732010-03-02 02:41:08 +00001191}
1192
Hal Finkel02a84272012-06-11 22:35:19 +00001193/// getPPCTargetCPU - Get the (LLVM) name of the PowerPC cpu we are targeting.
1194static std::string getPPCTargetCPU(const ArgList &Args) {
1195 if (Arg *A = Args.getLastArg(options::OPT_mcpu_EQ)) {
Richard Smith1d489cf2012-11-01 04:30:05 +00001196 StringRef CPUName = A->getValue();
Hal Finkel02a84272012-06-11 22:35:19 +00001197
1198 if (CPUName == "native") {
1199 std::string CPU = llvm::sys::getHostCPUName();
1200 if (!CPU.empty() && CPU != "generic")
1201 return CPU;
1202 else
1203 return "";
1204 }
1205
1206 return llvm::StringSwitch<const char *>(CPUName)
1207 .Case("common", "generic")
1208 .Case("440", "440")
1209 .Case("440fp", "440")
1210 .Case("450", "450")
1211 .Case("601", "601")
1212 .Case("602", "602")
1213 .Case("603", "603")
1214 .Case("603e", "603e")
1215 .Case("603ev", "603ev")
1216 .Case("604", "604")
1217 .Case("604e", "604e")
1218 .Case("620", "620")
Bill Schmidt2821e182013-02-01 20:23:10 +00001219 .Case("630", "pwr3")
Hal Finkel02a84272012-06-11 22:35:19 +00001220 .Case("G3", "g3")
1221 .Case("7400", "7400")
1222 .Case("G4", "g4")
1223 .Case("7450", "7450")
1224 .Case("G4+", "g4+")
1225 .Case("750", "750")
1226 .Case("970", "970")
1227 .Case("G5", "g5")
1228 .Case("a2", "a2")
Hal Finkel5ccd3d02013-02-01 05:53:33 +00001229 .Case("a2q", "a2q")
Hal Finkel7de32962012-09-18 22:25:03 +00001230 .Case("e500mc", "e500mc")
1231 .Case("e5500", "e5500")
Bill Schmidt2821e182013-02-01 20:23:10 +00001232 .Case("power3", "pwr3")
1233 .Case("power4", "pwr4")
1234 .Case("power5", "pwr5")
1235 .Case("power5x", "pwr5x")
Hal Finkel02a84272012-06-11 22:35:19 +00001236 .Case("power6", "pwr6")
Bill Schmidt2821e182013-02-01 20:23:10 +00001237 .Case("power6x", "pwr6x")
Hal Finkel02a84272012-06-11 22:35:19 +00001238 .Case("power7", "pwr7")
Stephen Hinesef822542014-07-21 00:47:37 -07001239 .Case("power8", "pwr8")
Bill Schmidt2821e182013-02-01 20:23:10 +00001240 .Case("pwr3", "pwr3")
1241 .Case("pwr4", "pwr4")
1242 .Case("pwr5", "pwr5")
1243 .Case("pwr5x", "pwr5x")
1244 .Case("pwr6", "pwr6")
1245 .Case("pwr6x", "pwr6x")
1246 .Case("pwr7", "pwr7")
Stephen Hinesef822542014-07-21 00:47:37 -07001247 .Case("pwr8", "pwr8")
Hal Finkel02a84272012-06-11 22:35:19 +00001248 .Case("powerpc", "ppc")
1249 .Case("powerpc64", "ppc64")
Bill Schmidtea7fb0c2013-07-26 01:36:11 +00001250 .Case("powerpc64le", "ppc64le")
Hal Finkel02a84272012-06-11 22:35:19 +00001251 .Default("");
1252 }
1253
1254 return "";
1255}
1256
Rafael Espindola146dbbf2013-08-21 16:39:20 +00001257static void getPPCTargetFeatures(const ArgList &Args,
1258 std::vector<const char *> &Features) {
Eric Christopherd5e59fc2013-10-16 20:40:08 +00001259 for (arg_iterator it = Args.filtered_begin(options::OPT_m_ppc_Features_Group),
1260 ie = Args.filtered_end();
1261 it != ie; ++it) {
1262 StringRef Name = (*it)->getOption().getName();
1263 (*it)->claim();
1264
1265 // Skip over "-m".
1266 assert(Name.startswith("m") && "Invalid feature name.");
1267 Name = Name.substr(1);
1268
1269 bool IsNegative = Name.startswith("no-");
1270 if (IsNegative)
1271 Name = Name.substr(3);
1272
1273 // Note that gcc calls this mfcrf and LLVM calls this mfocrf so we
1274 // pass the correct option to the backend while calling the frontend
1275 // option the same.
1276 // TODO: Change the LLVM backend option maybe?
1277 if (Name == "mfcrf")
1278 Name = "mfocrf";
1279
1280 Features.push_back(Args.MakeArgString((IsNegative ? "-" : "+") + Name));
1281 }
1282
1283 // Altivec is a bit weird, allow overriding of the Altivec feature here.
Rafael Espindola146dbbf2013-08-21 16:39:20 +00001284 AddTargetFeature(Args, Features, options::OPT_faltivec,
1285 options::OPT_fno_altivec, "altivec");
Hal Finkel02a84272012-06-11 22:35:19 +00001286}
1287
Stephen Hines176edba2014-12-01 14:53:08 -08001288void Clang::AddPPCTargetArgs(const ArgList &Args,
1289 ArgStringList &CmdArgs) const {
1290 // Select the ABI to use.
1291 const char *ABIName = nullptr;
1292 if (Arg *A = Args.getLastArg(options::OPT_mabi_EQ)) {
1293 ABIName = A->getValue();
1294 } else if (getToolChain().getTriple().isOSLinux())
1295 switch(getToolChain().getArch()) {
1296 case llvm::Triple::ppc64:
1297 ABIName = "elfv1";
1298 break;
1299 case llvm::Triple::ppc64le:
1300 ABIName = "elfv2";
1301 break;
1302 default:
1303 break;
1304 }
1305
1306 if (ABIName) {
1307 CmdArgs.push_back("-target-abi");
1308 CmdArgs.push_back(ABIName);
1309 }
1310}
1311
1312bool ppc::hasPPCAbiArg(const ArgList &Args, const char *Value) {
1313 Arg *A = Args.getLastArg(options::OPT_mabi_EQ);
1314 return A && (A->getValue() == StringRef(Value));
1315}
1316
Tom Stellarde25d2f62013-04-01 20:56:53 +00001317/// Get the (LLVM) name of the R600 gpu we are targeting.
1318static std::string getR600TargetGPU(const ArgList &Args) {
1319 if (Arg *A = Args.getLastArg(options::OPT_mcpu_EQ)) {
Benjamin Kramer39e4b0f2013-10-21 12:33:55 +00001320 const char *GPUName = A->getValue();
Tom Stellarde25d2f62013-04-01 20:56:53 +00001321 return llvm::StringSwitch<const char *>(GPUName)
Tom Stellardfd4aa4b2013-05-06 16:12:05 +00001322 .Cases("rv630", "rv635", "r600")
1323 .Cases("rv610", "rv620", "rs780", "rs880")
Tom Stellarde25d2f62013-04-01 20:56:53 +00001324 .Case("rv740", "rv770")
1325 .Case("palm", "cedar")
Tom Stellardfd4aa4b2013-05-06 16:12:05 +00001326 .Cases("sumo", "sumo2", "sumo")
Tom Stellarde25d2f62013-04-01 20:56:53 +00001327 .Case("hemlock", "cypress")
1328 .Case("aruba", "cayman")
Benjamin Kramer39e4b0f2013-10-21 12:33:55 +00001329 .Default(GPUName);
Tom Stellarde25d2f62013-04-01 20:56:53 +00001330 }
1331 return "";
1332}
1333
Rafael Espindola146dbbf2013-08-21 16:39:20 +00001334static void getSparcTargetFeatures(const ArgList &Args,
Stephen Hines176edba2014-12-01 14:53:08 -08001335 std::vector<const char *> &Features) {
Rafael Espindola146dbbf2013-08-21 16:39:20 +00001336 bool SoftFloatABI = true;
1337 if (Arg *A =
1338 Args.getLastArg(options::OPT_msoft_float, options::OPT_mhard_float)) {
1339 if (A->getOption().matches(options::OPT_mhard_float))
1340 SoftFloatABI = false;
1341 }
1342 if (SoftFloatABI)
1343 Features.push_back("+soft-float");
1344}
1345
Bruno Cardoso Lopes9284d212010-11-09 17:21:19 +00001346void Clang::AddSparcTargetArgs(const ArgList &Args,
1347 ArgStringList &CmdArgs) const {
1348 const Driver &D = getToolChain().getDriver();
1349
Stephen Hines176edba2014-12-01 14:53:08 -08001350 // Select the float ABI as determined by -msoft-float and -mhard-float.
Chris Lattner5f9e2722011-07-23 10:55:15 +00001351 StringRef FloatABI;
Bruno Cardoso Lopes9284d212010-11-09 17:21:19 +00001352 if (Arg *A = Args.getLastArg(options::OPT_msoft_float,
1353 options::OPT_mhard_float)) {
1354 if (A->getOption().matches(options::OPT_msoft_float))
1355 FloatABI = "soft";
1356 else if (A->getOption().matches(options::OPT_mhard_float))
1357 FloatABI = "hard";
1358 }
1359
1360 // If unspecified, choose the default based on the platform.
1361 if (FloatABI.empty()) {
Aaron Ballmand58915e2013-07-15 13:41:33 +00001362 // Assume "soft", but warn the user we are guessing.
1363 FloatABI = "soft";
1364 D.Diag(diag::warn_drv_assuming_mfloat_abi_is) << "soft";
Bruno Cardoso Lopes9284d212010-11-09 17:21:19 +00001365 }
1366
1367 if (FloatABI == "soft") {
1368 // Floating point operations and argument passing are soft.
1369 //
1370 // FIXME: This changes CPP defines, we need -target-soft-float.
1371 CmdArgs.push_back("-msoft-float");
Bruno Cardoso Lopes9284d212010-11-09 17:21:19 +00001372 } else {
1373 assert(FloatABI == "hard" && "Invalid float abi!");
1374 CmdArgs.push_back("-mhard-float");
1375 }
1376}
1377
Richard Sandiford5c92b9a2013-07-19 16:51:51 +00001378static const char *getSystemZTargetCPU(const ArgList &Args) {
1379 if (const Arg *A = Args.getLastArg(options::OPT_march_EQ))
1380 return A->getValue();
1381 return "z10";
1382}
1383
Chandler Carruth700d4e42013-01-13 11:46:33 +00001384static const char *getX86TargetCPU(const ArgList &Args,
1385 const llvm::Triple &Triple) {
1386 if (const Arg *A = Args.getLastArg(options::OPT_march_EQ)) {
Jim Grosbach32ca73e2013-11-16 00:53:35 +00001387 if (StringRef(A->getValue()) != "native") {
1388 if (Triple.isOSDarwin() && Triple.getArchName() == "x86_64h")
1389 return "core-avx2";
1390
Chandler Carruth700d4e42013-01-13 11:46:33 +00001391 return A->getValue();
Jim Grosbach32ca73e2013-11-16 00:53:35 +00001392 }
Chandler Carruth700d4e42013-01-13 11:46:33 +00001393
1394 // FIXME: Reject attempts to use -march=native unless the target matches
1395 // the host.
1396 //
1397 // FIXME: We should also incorporate the detected target features for use
1398 // with -native.
1399 std::string CPU = llvm::sys::getHostCPUName();
1400 if (!CPU.empty() && CPU != "generic")
1401 return Args.MakeArgString(CPU);
1402 }
1403
1404 // Select the default CPU if none was given (or detection failed).
1405
1406 if (Triple.getArch() != llvm::Triple::x86_64 &&
1407 Triple.getArch() != llvm::Triple::x86)
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001408 return nullptr; // This routine is only handling x86 targets.
Chandler Carruth700d4e42013-01-13 11:46:33 +00001409
1410 bool Is64Bit = Triple.getArch() == llvm::Triple::x86_64;
1411
1412 // FIXME: Need target hooks.
Jim Grosbach32ca73e2013-11-16 00:53:35 +00001413 if (Triple.isOSDarwin()) {
1414 if (Triple.getArchName() == "x86_64h")
1415 return "core-avx2";
Chandler Carruth700d4e42013-01-13 11:46:33 +00001416 return Is64Bit ? "core2" : "yonah";
Jim Grosbach32ca73e2013-11-16 00:53:35 +00001417 }
Chandler Carruth700d4e42013-01-13 11:46:33 +00001418
Stephen Hines651f13c2014-04-23 16:59:28 -07001419 // On Android use targets compatible with gcc
Chandler Carruth700d4e42013-01-13 11:46:33 +00001420 if (Triple.getEnvironment() == llvm::Triple::Android)
Stephen Hines651f13c2014-04-23 16:59:28 -07001421 return Is64Bit ? "x86-64" : "i686";
Chandler Carruth700d4e42013-01-13 11:46:33 +00001422
Benjamin Kramer39e4b0f2013-10-21 12:33:55 +00001423 // Everything else goes to x86-64 in 64-bit mode.
1424 if (Is64Bit)
1425 return "x86-64";
1426
1427 switch (Triple.getOS()) {
1428 case llvm::Triple::FreeBSD:
1429 case llvm::Triple::NetBSD:
1430 case llvm::Triple::OpenBSD:
1431 return "i486";
1432 case llvm::Triple::Haiku:
1433 return "i586";
1434 case llvm::Triple::Bitrig:
1435 return "i686";
1436 default:
1437 // Fallback to p4.
1438 return "pentium4";
1439 }
Chandler Carruth700d4e42013-01-13 11:46:33 +00001440}
1441
Rafael Espindolab330e402013-08-20 22:12:08 +00001442static std::string getCPUName(const ArgList &Args, const llvm::Triple &T) {
1443 switch(T.getArch()) {
1444 default:
1445 return "";
1446
Amara Emerson3bb1b5c2013-10-31 09:32:33 +00001447 case llvm::Triple::aarch64:
Stephen Hines651f13c2014-04-23 16:59:28 -07001448 case llvm::Triple::aarch64_be:
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001449 return getAArch64TargetCPU(Args);
Amara Emerson3bb1b5c2013-10-31 09:32:33 +00001450
Rafael Espindolab330e402013-08-20 22:12:08 +00001451 case llvm::Triple::arm:
Stephen Hines651f13c2014-04-23 16:59:28 -07001452 case llvm::Triple::armeb:
Rafael Espindolab330e402013-08-20 22:12:08 +00001453 case llvm::Triple::thumb:
Stephen Hines651f13c2014-04-23 16:59:28 -07001454 case llvm::Triple::thumbeb:
1455 return arm::getARMTargetCPU(Args, T);
Rafael Espindolab330e402013-08-20 22:12:08 +00001456
1457 case llvm::Triple::mips:
1458 case llvm::Triple::mipsel:
1459 case llvm::Triple::mips64:
1460 case llvm::Triple::mips64el: {
1461 StringRef CPUName;
1462 StringRef ABIName;
Stephen Hines176edba2014-12-01 14:53:08 -08001463 mips::getMipsCPUAndABI(Args, T, CPUName, ABIName);
Rafael Espindolab330e402013-08-20 22:12:08 +00001464 return CPUName;
1465 }
1466
1467 case llvm::Triple::ppc:
1468 case llvm::Triple::ppc64:
1469 case llvm::Triple::ppc64le: {
1470 std::string TargetCPUName = getPPCTargetCPU(Args);
1471 // LLVM may default to generating code for the native CPU,
1472 // but, like gcc, we default to a more generic option for
1473 // each architecture. (except on Darwin)
1474 if (TargetCPUName.empty() && !T.isOSDarwin()) {
1475 if (T.getArch() == llvm::Triple::ppc64)
1476 TargetCPUName = "ppc64";
1477 else if (T.getArch() == llvm::Triple::ppc64le)
1478 TargetCPUName = "ppc64le";
1479 else
1480 TargetCPUName = "ppc";
1481 }
1482 return TargetCPUName;
1483 }
1484
1485 case llvm::Triple::sparc:
Stephen Hines651f13c2014-04-23 16:59:28 -07001486 case llvm::Triple::sparcv9:
1487 if (const Arg *A = Args.getLastArg(options::OPT_mcpu_EQ))
Rafael Espindolab330e402013-08-20 22:12:08 +00001488 return A->getValue();
1489 return "";
1490
1491 case llvm::Triple::x86:
1492 case llvm::Triple::x86_64:
1493 return getX86TargetCPU(Args, T);
1494
1495 case llvm::Triple::hexagon:
1496 return "hexagon" + toolchains::Hexagon_TC::GetTargetCPU(Args).str();
1497
1498 case llvm::Triple::systemz:
1499 return getSystemZTargetCPU(Args);
1500
1501 case llvm::Triple::r600:
1502 return getR600TargetGPU(Args);
1503 }
1504}
1505
Stephen Hines651f13c2014-04-23 16:59:28 -07001506static void AddGoldPlugin(const ToolChain &ToolChain, const ArgList &Args,
1507 ArgStringList &CmdArgs) {
1508 // Tell the linker to load the plugin. This has to come before AddLinkerInputs
1509 // as gold requires -plugin to come before any -plugin-opt that -Wl might
1510 // forward.
1511 CmdArgs.push_back("-plugin");
1512 std::string Plugin = ToolChain.getDriver().Dir + "/../lib/LLVMgold.so";
1513 CmdArgs.push_back(Args.MakeArgString(Plugin));
1514
1515 // Try to pass driver level flags relevant to LTO code generation down to
1516 // the plugin.
1517
1518 // Handle flags for selecting CPU variants.
1519 std::string CPU = getCPUName(Args, ToolChain.getTriple());
1520 if (!CPU.empty())
1521 CmdArgs.push_back(Args.MakeArgString(Twine("-plugin-opt=mcpu=") + CPU));
1522}
1523
Stephen Hines176edba2014-12-01 14:53:08 -08001524static void getX86TargetFeatures(const Driver & D,
1525 const llvm::Triple &Triple,
Jim Grosbach32ca73e2013-11-16 00:53:35 +00001526 const ArgList &Args,
Rafael Espindola146dbbf2013-08-21 16:39:20 +00001527 std::vector<const char *> &Features) {
Jim Grosbach32ca73e2013-11-16 00:53:35 +00001528 if (Triple.getArchName() == "x86_64h") {
1529 // x86_64h implies quite a few of the more modern subtarget features
1530 // for Haswell class CPUs, but not all of them. Opt-out of a few.
1531 Features.push_back("-rdrnd");
1532 Features.push_back("-aes");
1533 Features.push_back("-pclmul");
1534 Features.push_back("-rtm");
1535 Features.push_back("-hle");
1536 Features.push_back("-fsgsbase");
1537 }
1538
Stephen Hinesef822542014-07-21 00:47:37 -07001539 // Add features to comply with gcc on Android
Stephen Hines651f13c2014-04-23 16:59:28 -07001540 if (Triple.getEnvironment() == llvm::Triple::Android) {
Stephen Hinesef822542014-07-21 00:47:37 -07001541 if (Triple.getArch() == llvm::Triple::x86_64) {
1542 Features.push_back("+sse4.2");
1543 Features.push_back("+popcnt");
1544 } else
1545 Features.push_back("+ssse3");
Stephen Hines651f13c2014-04-23 16:59:28 -07001546 }
1547
Stephen Hines176edba2014-12-01 14:53:08 -08001548 // Set features according to the -arch flag on MSVC
1549 if (Arg *A = Args.getLastArg(options::OPT__SLASH_arch)) {
1550 StringRef Arch = A->getValue();
1551 bool ArchUsed = false;
1552 // First, look for flags that are shared in x86 and x86-64.
1553 if (Triple.getArch() == llvm::Triple::x86_64 ||
1554 Triple.getArch() == llvm::Triple::x86) {
1555 if (Arch == "AVX" || Arch == "AVX2") {
1556 ArchUsed = true;
1557 Features.push_back(Args.MakeArgString("+" + Arch.lower()));
1558 }
1559 }
1560 // Then, look for x86-specific flags.
1561 if (Triple.getArch() == llvm::Triple::x86) {
1562 if (Arch == "IA32") {
1563 ArchUsed = true;
1564 } else if (Arch == "SSE" || Arch == "SSE2") {
1565 ArchUsed = true;
1566 Features.push_back(Args.MakeArgString("+" + Arch.lower()));
1567 }
1568 }
1569 if (!ArchUsed)
1570 D.Diag(clang::diag::warn_drv_unused_argument) << A->getAsString(Args);
1571 }
1572
Jim Grosbach32ca73e2013-11-16 00:53:35 +00001573 // Now add any that the user explicitly requested on the command line,
1574 // which may override the defaults.
Rafael Espindola146dbbf2013-08-21 16:39:20 +00001575 for (arg_iterator it = Args.filtered_begin(options::OPT_m_x86_Features_Group),
1576 ie = Args.filtered_end();
1577 it != ie; ++it) {
1578 StringRef Name = (*it)->getOption().getName();
1579 (*it)->claim();
1580
1581 // Skip over "-m".
1582 assert(Name.startswith("m") && "Invalid feature name.");
1583 Name = Name.substr(1);
1584
1585 bool IsNegative = Name.startswith("no-");
1586 if (IsNegative)
1587 Name = Name.substr(3);
1588
1589 Features.push_back(Args.MakeArgString((IsNegative ? "-" : "+") + Name));
1590 }
1591}
1592
Daniel Dunbar6acda162009-09-09 22:33:08 +00001593void Clang::AddX86TargetArgs(const ArgList &Args,
1594 ArgStringList &CmdArgs) const {
Daniel Dunbare6ad3f92009-09-10 22:59:57 +00001595 if (!Args.hasFlag(options::OPT_mred_zone,
1596 options::OPT_mno_red_zone,
1597 true) ||
1598 Args.hasArg(options::OPT_mkernel) ||
1599 Args.hasArg(options::OPT_fapple_kext))
Daniel Dunbar66861e02009-11-20 22:21:36 +00001600 CmdArgs.push_back("-disable-red-zone");
Daniel Dunbare6ad3f92009-09-10 22:59:57 +00001601
Bob Wilsonf0c54562013-02-10 16:01:41 +00001602 // Default to avoid implicit floating-point for kernel/kext code, but allow
1603 // that to be overridden with -mno-soft-float.
1604 bool NoImplicitFloat = (Args.hasArg(options::OPT_mkernel) ||
1605 Args.hasArg(options::OPT_fapple_kext));
1606 if (Arg *A = Args.getLastArg(options::OPT_msoft_float,
1607 options::OPT_mno_soft_float,
Bob Wilsonf6f77bd2013-04-11 18:53:25 +00001608 options::OPT_mimplicit_float,
Bob Wilsonf0c54562013-02-10 16:01:41 +00001609 options::OPT_mno_implicit_float)) {
1610 const Option &O = A->getOption();
1611 NoImplicitFloat = (O.matches(options::OPT_mno_implicit_float) ||
1612 O.matches(options::OPT_msoft_float));
1613 }
1614 if (NoImplicitFloat)
Daniel Dunbar66861e02009-11-20 22:21:36 +00001615 CmdArgs.push_back("-no-implicit-float");
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001616
1617 if (Arg *A = Args.getLastArg(options::OPT_masm_EQ)) {
1618 StringRef Value = A->getValue();
1619 if (Value == "intel" || Value == "att") {
1620 CmdArgs.push_back("-mllvm");
1621 CmdArgs.push_back(Args.MakeArgString("-x86-asm-syntax=" + Value));
1622 } else {
1623 getToolChain().getDriver().Diag(diag::err_drv_unsupported_option_argument)
1624 << A->getOption().getName() << Value;
1625 }
1626 }
Daniel Dunbar6acda162009-09-09 22:33:08 +00001627}
1628
Matthew Curtis33c95f12012-12-06 17:49:03 +00001629static inline bool HasPICArg(const ArgList &Args) {
1630 return Args.hasArg(options::OPT_fPIC)
1631 || Args.hasArg(options::OPT_fpic);
1632}
1633
1634static Arg *GetLastSmallDataThresholdArg(const ArgList &Args) {
1635 return Args.getLastArg(options::OPT_G,
1636 options::OPT_G_EQ,
1637 options::OPT_msmall_data_threshold_EQ);
1638}
1639
1640static std::string GetHexagonSmallDataThresholdValue(const ArgList &Args) {
1641 std::string value;
1642 if (HasPICArg(Args))
1643 value = "0";
1644 else if (Arg *A = GetLastSmallDataThresholdArg(Args)) {
1645 value = A->getValue();
1646 A->claim();
1647 }
1648 return value;
1649}
1650
Tony Linthicum96319392011-12-12 21:14:55 +00001651void Clang::AddHexagonTargetArgs(const ArgList &Args,
1652 ArgStringList &CmdArgs) const {
Tony Linthicum96319392011-12-12 21:14:55 +00001653 CmdArgs.push_back("-fno-signed-char");
Matthew Curtis1dbaef52012-12-07 13:52:44 +00001654 CmdArgs.push_back("-mqdsp6-compat");
1655 CmdArgs.push_back("-Wreturn-type");
Tony Linthicum96319392011-12-12 21:14:55 +00001656
Matthew Curtis33c95f12012-12-06 17:49:03 +00001657 std::string SmallDataThreshold = GetHexagonSmallDataThresholdValue(Args);
1658 if (!SmallDataThreshold.empty()) {
Tony Linthicum96319392011-12-12 21:14:55 +00001659 CmdArgs.push_back ("-mllvm");
Matthew Curtis33c95f12012-12-06 17:49:03 +00001660 CmdArgs.push_back(Args.MakeArgString(
1661 "-hexagon-small-data-threshold=" + SmallDataThreshold));
Tony Linthicum96319392011-12-12 21:14:55 +00001662 }
1663
Sirish Pande5f9688b2012-05-10 20:19:54 +00001664 if (!Args.hasArg(options::OPT_fno_short_enums))
1665 CmdArgs.push_back("-fshort-enums");
1666 if (Args.getLastArg(options::OPT_mieee_rnd_near)) {
1667 CmdArgs.push_back ("-mllvm");
1668 CmdArgs.push_back ("-enable-hexagon-ieee-rnd-near");
1669 }
Tony Linthicum96319392011-12-12 21:14:55 +00001670 CmdArgs.push_back ("-mllvm");
1671 CmdArgs.push_back ("-machine-sink-split=0");
1672}
1673
Stephen Hines176edba2014-12-01 14:53:08 -08001674// Decode AArch64 features from string like +[no]featureA+[no]featureB+...
1675static bool DecodeAArch64Features(const Driver &D, StringRef text,
1676 std::vector<const char *> &Features) {
1677 SmallVector<StringRef, 8> Split;
1678 text.split(Split, StringRef("+"), -1, false);
1679
1680 for (unsigned I = 0, E = Split.size(); I != E; ++I) {
1681 const char *result = llvm::StringSwitch<const char *>(Split[I])
1682 .Case("fp", "+fp-armv8")
1683 .Case("simd", "+neon")
1684 .Case("crc", "+crc")
1685 .Case("crypto", "+crypto")
1686 .Case("nofp", "-fp-armv8")
1687 .Case("nosimd", "-neon")
1688 .Case("nocrc", "-crc")
1689 .Case("nocrypto", "-crypto")
1690 .Default(nullptr);
1691 if (result)
1692 Features.push_back(result);
1693 else if (Split[I] == "neon" || Split[I] == "noneon")
1694 D.Diag(diag::err_drv_no_neon_modifier);
1695 else
1696 return false;
1697 }
1698 return true;
1699}
1700
1701// Check if the CPU name and feature modifiers in -mcpu are legal. If yes,
1702// decode CPU and feature.
1703static bool DecodeAArch64Mcpu(const Driver &D, StringRef Mcpu, StringRef &CPU,
1704 std::vector<const char *> &Features) {
1705 std::pair<StringRef, StringRef> Split = Mcpu.split("+");
1706 CPU = Split.first;
1707 if (CPU == "cyclone" || CPU == "cortex-a53" || CPU == "cortex-a57") {
1708 Features.push_back("+neon");
1709 Features.push_back("+crc");
1710 Features.push_back("+crypto");
1711 } else if (CPU == "generic") {
1712 Features.push_back("+neon");
1713 } else {
1714 return false;
1715 }
1716
1717 if (Split.second.size() && !DecodeAArch64Features(D, Split.second, Features))
1718 return false;
1719
1720 return true;
1721}
1722
1723static bool
1724getAArch64ArchFeaturesFromMarch(const Driver &D, StringRef March,
1725 const ArgList &Args,
1726 std::vector<const char *> &Features) {
1727 std::pair<StringRef, StringRef> Split = March.split("+");
1728 if (Split.first != "armv8-a")
1729 return false;
1730
1731 if (Split.second.size() && !DecodeAArch64Features(D, Split.second, Features))
1732 return false;
1733
1734 return true;
1735}
1736
1737static bool
1738getAArch64ArchFeaturesFromMcpu(const Driver &D, StringRef Mcpu,
1739 const ArgList &Args,
1740 std::vector<const char *> &Features) {
1741 StringRef CPU;
1742 if (!DecodeAArch64Mcpu(D, Mcpu, CPU, Features))
1743 return false;
1744
1745 return true;
1746}
1747
1748static bool
1749getAArch64MicroArchFeaturesFromMtune(const Driver &D, StringRef Mtune,
1750 const ArgList &Args,
1751 std::vector<const char *> &Features) {
1752 // Handle CPU name is 'native'.
1753 if (Mtune == "native")
1754 Mtune = llvm::sys::getHostCPUName();
1755 if (Mtune == "cyclone") {
1756 Features.push_back("+zcm");
1757 Features.push_back("+zcz");
1758 }
1759 return true;
1760}
1761
1762static bool
1763getAArch64MicroArchFeaturesFromMcpu(const Driver &D, StringRef Mcpu,
1764 const ArgList &Args,
1765 std::vector<const char *> &Features) {
1766 StringRef CPU;
1767 std::vector<const char *> DecodedFeature;
1768 if (!DecodeAArch64Mcpu(D, Mcpu, CPU, DecodedFeature))
1769 return false;
1770
1771 return getAArch64MicroArchFeaturesFromMtune(D, CPU, Args, Features);
1772}
1773
Rafael Espindola146dbbf2013-08-21 16:39:20 +00001774static void getAArch64TargetFeatures(const Driver &D, const ArgList &Args,
1775 std::vector<const char *> &Features) {
Stephen Hines176edba2014-12-01 14:53:08 -08001776 Arg *A;
1777 bool success = true;
1778 // Enable NEON by default.
1779 Features.push_back("+neon");
1780 if ((A = Args.getLastArg(options::OPT_march_EQ)))
1781 success = getAArch64ArchFeaturesFromMarch(D, A->getValue(), Args, Features);
1782 else if ((A = Args.getLastArg(options::OPT_mcpu_EQ)))
1783 success = getAArch64ArchFeaturesFromMcpu(D, A->getValue(), Args, Features);
1784 else if (Args.hasArg(options::OPT_arch))
1785 success = getAArch64ArchFeaturesFromMcpu(D, getAArch64TargetCPU(Args), Args,
1786 Features);
1787
1788 if (success && (A = Args.getLastArg(options::OPT_mtune_EQ)))
1789 success =
1790 getAArch64MicroArchFeaturesFromMtune(D, A->getValue(), Args, Features);
1791 else if (success && (A = Args.getLastArg(options::OPT_mcpu_EQ)))
1792 success =
1793 getAArch64MicroArchFeaturesFromMcpu(D, A->getValue(), Args, Features);
1794 else if (Args.hasArg(options::OPT_arch))
1795 success = getAArch64MicroArchFeaturesFromMcpu(D, getAArch64TargetCPU(Args),
1796 Args, Features);
1797
1798 if (!success)
1799 D.Diag(diag::err_drv_clang_unsupported) << A->getAsString(Args);
Stephen Hines651f13c2014-04-23 16:59:28 -07001800
1801 if (Args.getLastArg(options::OPT_mgeneral_regs_only)) {
1802 Features.push_back("-fp-armv8");
1803 Features.push_back("-crypto");
1804 Features.push_back("-neon");
1805 }
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001806
1807 // En/disable crc
1808 if (Arg *A = Args.getLastArg(options::OPT_mcrc,
1809 options::OPT_mnocrc)) {
1810 if (A->getOption().matches(options::OPT_mcrc))
1811 Features.push_back("+crc");
1812 else
1813 Features.push_back("-crc");
1814 }
Rafael Espindola146dbbf2013-08-21 16:39:20 +00001815}
1816
1817static void getTargetFeatures(const Driver &D, const llvm::Triple &Triple,
Stephen Hines651f13c2014-04-23 16:59:28 -07001818 const ArgList &Args, ArgStringList &CmdArgs,
1819 bool ForAS) {
Rafael Espindola146dbbf2013-08-21 16:39:20 +00001820 std::vector<const char *> Features;
1821 switch (Triple.getArch()) {
1822 default:
1823 break;
1824 case llvm::Triple::mips:
1825 case llvm::Triple::mipsel:
1826 case llvm::Triple::mips64:
1827 case llvm::Triple::mips64el:
Stephen Hines176edba2014-12-01 14:53:08 -08001828 getMIPSTargetFeatures(D, Triple, Args, Features);
Rafael Espindola146dbbf2013-08-21 16:39:20 +00001829 break;
1830
1831 case llvm::Triple::arm:
Stephen Hines651f13c2014-04-23 16:59:28 -07001832 case llvm::Triple::armeb:
Rafael Espindola146dbbf2013-08-21 16:39:20 +00001833 case llvm::Triple::thumb:
Stephen Hines651f13c2014-04-23 16:59:28 -07001834 case llvm::Triple::thumbeb:
1835 getARMTargetFeatures(D, Triple, Args, Features, ForAS);
Rafael Espindola146dbbf2013-08-21 16:39:20 +00001836 break;
1837
1838 case llvm::Triple::ppc:
1839 case llvm::Triple::ppc64:
1840 case llvm::Triple::ppc64le:
1841 getPPCTargetFeatures(Args, Features);
1842 break;
1843 case llvm::Triple::sparc:
Stephen Hines176edba2014-12-01 14:53:08 -08001844 case llvm::Triple::sparcv9:
Rafael Espindola146dbbf2013-08-21 16:39:20 +00001845 getSparcTargetFeatures(Args, Features);
1846 break;
1847 case llvm::Triple::aarch64:
Stephen Hines651f13c2014-04-23 16:59:28 -07001848 case llvm::Triple::aarch64_be:
Rafael Espindola146dbbf2013-08-21 16:39:20 +00001849 getAArch64TargetFeatures(D, Args, Features);
1850 break;
1851 case llvm::Triple::x86:
1852 case llvm::Triple::x86_64:
Stephen Hines176edba2014-12-01 14:53:08 -08001853 getX86TargetFeatures(D, Triple, Args, Features);
Rafael Espindola146dbbf2013-08-21 16:39:20 +00001854 break;
1855 }
Rafael Espindolabc1e5452013-08-21 17:34:32 +00001856
1857 // Find the last of each feature.
1858 llvm::StringMap<unsigned> LastOpt;
1859 for (unsigned I = 0, N = Features.size(); I < N; ++I) {
1860 const char *Name = Features[I];
1861 assert(Name[0] == '-' || Name[0] == '+');
1862 LastOpt[Name + 1] = I;
1863 }
1864
1865 for (unsigned I = 0, N = Features.size(); I < N; ++I) {
1866 // If this feature was overridden, ignore it.
1867 const char *Name = Features[I];
1868 llvm::StringMap<unsigned>::iterator LastI = LastOpt.find(Name + 1);
1869 assert(LastI != LastOpt.end());
1870 unsigned Last = LastI->second;
1871 if (Last != I)
1872 continue;
1873
Rafael Espindola146dbbf2013-08-21 16:39:20 +00001874 CmdArgs.push_back("-target-feature");
Rafael Espindolabc1e5452013-08-21 17:34:32 +00001875 CmdArgs.push_back(Name);
Rafael Espindola146dbbf2013-08-21 16:39:20 +00001876 }
Tim Northoverb793f0d2013-08-01 09:23:19 +00001877}
1878
Eric Christopher88b7cf02011-08-19 00:30:14 +00001879static bool
John McCall260611a2012-06-20 06:18:46 +00001880shouldUseExceptionTablesForObjCExceptions(const ObjCRuntime &runtime,
Anders Carlsson525544d2011-02-28 00:44:51 +00001881 const llvm::Triple &Triple) {
1882 // We use the zero-cost exception tables for Objective-C if the non-fragile
1883 // ABI is enabled or when compiling for x86_64 and ARM on Snow Leopard and
1884 // later.
John McCall260611a2012-06-20 06:18:46 +00001885 if (runtime.isNonFragile())
Anders Carlsson525544d2011-02-28 00:44:51 +00001886 return true;
1887
Stephen Hines651f13c2014-04-23 16:59:28 -07001888 if (!Triple.isMacOSX())
Anders Carlsson525544d2011-02-28 00:44:51 +00001889 return false;
1890
Eric Christopheraa7333c2011-07-02 00:20:22 +00001891 return (!Triple.isMacOSXVersionLT(10,5) &&
Anders Carlsson525544d2011-02-28 00:44:51 +00001892 (Triple.getArch() == llvm::Triple::x86_64 ||
Eric Christopher88b7cf02011-08-19 00:30:14 +00001893 Triple.getArch() == llvm::Triple::arm));
Anders Carlsson525544d2011-02-28 00:44:51 +00001894}
1895
Stephen Hines651f13c2014-04-23 16:59:28 -07001896namespace {
1897 struct ExceptionSettings {
1898 bool ExceptionsEnabled;
1899 bool ShouldUseExceptionTables;
1900 ExceptionSettings() : ExceptionsEnabled(false),
1901 ShouldUseExceptionTables(false) {}
1902 };
1903} // end anonymous namespace.
1904
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001905// exceptionSettings() exists to share the logic between -cc1 and linker
1906// invocations.
Stephen Hines651f13c2014-04-23 16:59:28 -07001907static ExceptionSettings exceptionSettings(const ArgList &Args,
1908 const llvm::Triple &Triple) {
1909 ExceptionSettings ES;
1910
1911 // Are exceptions enabled by default?
1912 ES.ExceptionsEnabled = (Triple.getArch() != llvm::Triple::xcore);
1913
1914 // This keeps track of whether exceptions were explicitly turned on or off.
1915 bool DidHaveExplicitExceptionFlag = false;
1916
1917 if (Arg *A = Args.getLastArg(options::OPT_fexceptions,
1918 options::OPT_fno_exceptions)) {
1919 if (A->getOption().matches(options::OPT_fexceptions))
1920 ES.ExceptionsEnabled = true;
1921 else
1922 ES.ExceptionsEnabled = false;
1923
1924 DidHaveExplicitExceptionFlag = true;
1925 }
1926
1927 // Exception tables and cleanups can be enabled with -fexceptions even if the
1928 // language itself doesn't support exceptions.
1929 if (ES.ExceptionsEnabled && DidHaveExplicitExceptionFlag)
1930 ES.ShouldUseExceptionTables = true;
1931
1932 return ES;
1933}
1934
Anders Carlsson15348ae2011-02-28 02:27:16 +00001935/// addExceptionArgs - Adds exception related arguments to the driver command
1936/// arguments. There's a master flag, -fexceptions and also language specific
1937/// flags to enable/disable C++ and Objective-C exceptions.
1938/// This makes it possible to for example disable C++ exceptions but enable
1939/// Objective-C exceptions.
1940static void addExceptionArgs(const ArgList &Args, types::ID InputType,
1941 const llvm::Triple &Triple,
Fariborz Jahanian15b77312012-04-04 18:28:00 +00001942 bool KernelOrKext,
John McCall260611a2012-06-20 06:18:46 +00001943 const ObjCRuntime &objcRuntime,
Anders Carlsson15348ae2011-02-28 02:27:16 +00001944 ArgStringList &CmdArgs) {
Chad Rosierafc4baa2012-03-26 22:04:46 +00001945 if (KernelOrKext) {
1946 // -mkernel and -fapple-kext imply no exceptions, so claim exception related
1947 // arguments now to avoid warnings about unused arguments.
1948 Args.ClaimAllArgs(options::OPT_fexceptions);
1949 Args.ClaimAllArgs(options::OPT_fno_exceptions);
1950 Args.ClaimAllArgs(options::OPT_fobjc_exceptions);
1951 Args.ClaimAllArgs(options::OPT_fno_objc_exceptions);
1952 Args.ClaimAllArgs(options::OPT_fcxx_exceptions);
1953 Args.ClaimAllArgs(options::OPT_fno_cxx_exceptions);
Anders Carlsson15348ae2011-02-28 02:27:16 +00001954 return;
Chad Rosierafc4baa2012-03-26 22:04:46 +00001955 }
Anders Carlsson15348ae2011-02-28 02:27:16 +00001956
Stephen Hines651f13c2014-04-23 16:59:28 -07001957 // Gather the exception settings from the command line arguments.
1958 ExceptionSettings ES = exceptionSettings(Args, Triple);
Daniel Dunbar1a2cd4f2010-09-14 23:12:31 +00001959
Daniel Dunbard47ea692011-03-17 23:28:31 +00001960 // Obj-C exceptions are enabled by default, regardless of -fexceptions. This
1961 // is not necessarily sensible, but follows GCC.
1962 if (types::isObjC(InputType) &&
Eric Christopher88b7cf02011-08-19 00:30:14 +00001963 Args.hasFlag(options::OPT_fobjc_exceptions,
Daniel Dunbard47ea692011-03-17 23:28:31 +00001964 options::OPT_fno_objc_exceptions,
1965 true)) {
1966 CmdArgs.push_back("-fobjc-exceptions");
Anders Carlsson15348ae2011-02-28 02:27:16 +00001967
Stephen Hines651f13c2014-04-23 16:59:28 -07001968 ES.ShouldUseExceptionTables |=
John McCall260611a2012-06-20 06:18:46 +00001969 shouldUseExceptionTablesForObjCExceptions(objcRuntime, Triple);
Anders Carlsson15348ae2011-02-28 02:27:16 +00001970 }
1971
1972 if (types::isCXX(InputType)) {
Stephen Hines651f13c2014-04-23 16:59:28 -07001973 bool CXXExceptionsEnabled = ES.ExceptionsEnabled;
Anders Carlsson15348ae2011-02-28 02:27:16 +00001974
Eric Christopher88b7cf02011-08-19 00:30:14 +00001975 if (Arg *A = Args.getLastArg(options::OPT_fcxx_exceptions,
1976 options::OPT_fno_cxx_exceptions,
Anders Carlsson15348ae2011-02-28 02:27:16 +00001977 options::OPT_fexceptions,
1978 options::OPT_fno_exceptions)) {
1979 if (A->getOption().matches(options::OPT_fcxx_exceptions))
1980 CXXExceptionsEnabled = true;
Chandler Carruth43f220f2011-02-28 07:25:18 +00001981 else if (A->getOption().matches(options::OPT_fno_cxx_exceptions))
Anders Carlsson15348ae2011-02-28 02:27:16 +00001982 CXXExceptionsEnabled = false;
1983 }
1984
1985 if (CXXExceptionsEnabled) {
1986 CmdArgs.push_back("-fcxx-exceptions");
1987
Stephen Hines651f13c2014-04-23 16:59:28 -07001988 ES.ShouldUseExceptionTables = true;
Anders Carlsson15348ae2011-02-28 02:27:16 +00001989 }
1990 }
1991
Stephen Hines651f13c2014-04-23 16:59:28 -07001992 if (ES.ShouldUseExceptionTables)
Anders Carlsson15348ae2011-02-28 02:27:16 +00001993 CmdArgs.push_back("-fexceptions");
Rafael Espindolaf759df02009-10-01 13:33:33 +00001994}
1995
Daniel Dunbarf4910132013-04-16 18:21:19 +00001996static bool ShouldDisableAutolink(const ArgList &Args,
1997 const ToolChain &TC) {
1998 bool Default = true;
1999 if (TC.getTriple().isOSDarwin()) {
2000 // The native darwin assembler doesn't support the linker_option directives,
2001 // so we disable them if we think the .s file will be passed to it.
2002 Default = TC.useIntegratedAs();
2003 }
2004 return !Args.hasFlag(options::OPT_fautolink, options::OPT_fno_autolink,
2005 Default);
2006}
2007
Ted Kremenekc06fcdf2013-03-12 17:02:12 +00002008static bool ShouldDisableDwarfDirectory(const ArgList &Args,
2009 const ToolChain &TC) {
Nick Lewyckyea523d72011-10-17 23:05:52 +00002010 bool UseDwarfDirectory = Args.hasFlag(options::OPT_fdwarf_directory_asm,
2011 options::OPT_fno_dwarf_directory_asm,
Rafael Espindolaaf370e62013-03-18 18:10:27 +00002012 TC.useIntegratedAs());
Nick Lewyckyea523d72011-10-17 23:05:52 +00002013 return !UseDwarfDirectory;
2014}
2015
Joerg Sonnenberger359cf922011-05-06 14:35:16 +00002016/// \brief Check whether the given input tree contains any compilation actions.
2017static bool ContainsCompileAction(const Action *A) {
Chris Lattner5f9e2722011-07-23 10:55:15 +00002018 if (isa<CompileJobAction>(A))
Joerg Sonnenberger359cf922011-05-06 14:35:16 +00002019 return true;
2020
Stephen Hinesef822542014-07-21 00:47:37 -07002021 for (const auto &Act : *A)
2022 if (ContainsCompileAction(Act))
Joerg Sonnenberger359cf922011-05-06 14:35:16 +00002023 return true;
2024
2025 return false;
2026}
2027
2028/// \brief Check if -relax-all should be passed to the internal assembler.
2029/// This is done by default when compiling non-assembler source with -O0.
2030static bool UseRelaxAll(Compilation &C, const ArgList &Args) {
2031 bool RelaxDefault = true;
2032
2033 if (Arg *A = Args.getLastArg(options::OPT_O_Group))
2034 RelaxDefault = A->getOption().matches(options::OPT_O0);
2035
2036 if (RelaxDefault) {
2037 RelaxDefault = false;
Stephen Hinesef822542014-07-21 00:47:37 -07002038 for (const auto &Act : C.getActions()) {
2039 if (ContainsCompileAction(Act)) {
Joerg Sonnenberger359cf922011-05-06 14:35:16 +00002040 RelaxDefault = true;
2041 break;
2042 }
2043 }
2044 }
2045
2046 return Args.hasFlag(options::OPT_mrelax_all, options::OPT_mno_relax_all,
2047 RelaxDefault);
2048}
2049
David Blaikie73168db2013-07-25 21:19:01 +00002050static void CollectArgsForIntegratedAssembler(Compilation &C,
2051 const ArgList &Args,
2052 ArgStringList &CmdArgs,
2053 const Driver &D) {
2054 if (UseRelaxAll(C, Args))
2055 CmdArgs.push_back("-mrelax-all");
2056
David Peixotto4ca9eae2013-11-14 22:52:58 +00002057 // When passing -I arguments to the assembler we sometimes need to
David Peixotto2317f7b2013-11-14 22:58:17 +00002058 // unconditionally take the next argument. For example, when parsing
David Peixotto4ca9eae2013-11-14 22:52:58 +00002059 // '-Wa,-I -Wa,foo' we need to accept the -Wa,foo arg after seeing the
2060 // -Wa,-I arg and when parsing '-Wa,-I,foo' we need to accept the 'foo'
2061 // arg after parsing the '-I' arg.
2062 bool TakeNextArg = false;
2063
David Blaikie73168db2013-07-25 21:19:01 +00002064 // When using an integrated assembler, translate -Wa, and -Xassembler
2065 // options.
Stephen Hines651f13c2014-04-23 16:59:28 -07002066 bool CompressDebugSections = false;
David Blaikie73168db2013-07-25 21:19:01 +00002067 for (arg_iterator it = Args.filtered_begin(options::OPT_Wa_COMMA,
2068 options::OPT_Xassembler),
2069 ie = Args.filtered_end(); it != ie; ++it) {
2070 const Arg *A = *it;
2071 A->claim();
2072
2073 for (unsigned i = 0, e = A->getNumValues(); i != e; ++i) {
2074 StringRef Value = A->getValue(i);
David Peixotto4ca9eae2013-11-14 22:52:58 +00002075 if (TakeNextArg) {
2076 CmdArgs.push_back(Value.data());
2077 TakeNextArg = false;
2078 continue;
2079 }
David Blaikie73168db2013-07-25 21:19:01 +00002080
2081 if (Value == "-force_cpusubtype_ALL") {
2082 // Do nothing, this is the default and we don't support anything else.
2083 } else if (Value == "-L") {
2084 CmdArgs.push_back("-msave-temp-labels");
2085 } else if (Value == "--fatal-warnings") {
Stephen Hines176edba2014-12-01 14:53:08 -08002086 CmdArgs.push_back("-massembler-fatal-warnings");
David Blaikie73168db2013-07-25 21:19:01 +00002087 } else if (Value == "--noexecstack") {
2088 CmdArgs.push_back("-mnoexecstack");
Stephen Hines651f13c2014-04-23 16:59:28 -07002089 } else if (Value == "-compress-debug-sections" ||
2090 Value == "--compress-debug-sections") {
2091 CompressDebugSections = true;
2092 } else if (Value == "-nocompress-debug-sections" ||
2093 Value == "--nocompress-debug-sections") {
2094 CompressDebugSections = false;
David Peixotto4ca9eae2013-11-14 22:52:58 +00002095 } else if (Value.startswith("-I")) {
2096 CmdArgs.push_back(Value.data());
2097 // We need to consume the next argument if the current arg is a plain
2098 // -I. The next arg will be the include directory.
2099 if (Value == "-I")
2100 TakeNextArg = true;
Stephen Hines6bcf27b2014-05-29 04:14:42 -07002101 } else if (Value.startswith("-gdwarf-")) {
2102 CmdArgs.push_back(Value.data());
David Blaikie73168db2013-07-25 21:19:01 +00002103 } else {
2104 D.Diag(diag::err_drv_unsupported_option_argument)
2105 << A->getOption().getName() << Value;
2106 }
2107 }
2108 }
Stephen Hines651f13c2014-04-23 16:59:28 -07002109 if (CompressDebugSections) {
2110 if (llvm::zlib::isAvailable())
2111 CmdArgs.push_back("-compress-debug-sections");
2112 else
2113 D.Diag(diag::warn_debug_compression_unavailable);
2114 }
David Blaikie73168db2013-07-25 21:19:01 +00002115}
2116
Stephen Hines651f13c2014-04-23 16:59:28 -07002117// Until ARM libraries are build separately, we have them all in one library
2118static StringRef getArchNameForCompilerRTLib(const ToolChain &TC) {
2119 if (TC.getArch() == llvm::Triple::arm ||
2120 TC.getArch() == llvm::Triple::armeb)
2121 return "arm";
2122 else
2123 return TC.getArchName();
2124}
2125
2126static SmallString<128> getCompilerRTLibDir(const ToolChain &TC) {
2127 // The runtimes are located in the OS-specific resource directory.
2128 SmallString<128> Res(TC.getDriver().ResourceDir);
2129 const llvm::Triple &Triple = TC.getTriple();
2130 // TC.getOS() yield "freebsd10.0" whereas "freebsd" is expected.
2131 StringRef OSLibName = (Triple.getOS() == llvm::Triple::FreeBSD) ?
2132 "freebsd" : TC.getOS();
2133 llvm::sys::path::append(Res, "lib", OSLibName);
2134 return Res;
2135}
2136
Stephen Hines6bcf27b2014-05-29 04:14:42 -07002137// This adds the static libclang_rt.builtins-arch.a directly to the command line
Stephen Hines651f13c2014-04-23 16:59:28 -07002138// FIXME: Make sure we can also emit shared objects if they're requested
2139// and available, check for possible errors, etc.
2140static void addClangRTLinux(
2141 const ToolChain &TC, const ArgList &Args, ArgStringList &CmdArgs) {
2142 SmallString<128> LibClangRT = getCompilerRTLibDir(TC);
Stephen Hines6bcf27b2014-05-29 04:14:42 -07002143 llvm::sys::path::append(LibClangRT, Twine("libclang_rt.builtins-") +
2144 getArchNameForCompilerRTLib(TC) +
2145 ".a");
Stephen Hines651f13c2014-04-23 16:59:28 -07002146
2147 CmdArgs.push_back(Args.MakeArgString(LibClangRT));
2148 CmdArgs.push_back("-lgcc_s");
2149 if (TC.getDriver().CCCIsCXX())
2150 CmdArgs.push_back("-lgcc_eh");
2151}
2152
Stephen Hines176edba2014-12-01 14:53:08 -08002153static void addClangRTWindows(const ToolChain &TC, const ArgList &Args,
2154 ArgStringList &CmdArgs) {
2155 SmallString<128> LibClangRT = getCompilerRTLibDir(TC);
2156 llvm::sys::path::append(LibClangRT, Twine("libclang_rt.builtins-") +
2157 getArchNameForCompilerRTLib(TC) + ".lib");
2158 CmdArgs.push_back(Args.MakeArgString(LibClangRT));
2159}
2160
Stephen Hines651f13c2014-04-23 16:59:28 -07002161static void addProfileRT(
Chandler Carruth9db37cd2013-06-23 11:28:48 +00002162 const ToolChain &TC, const ArgList &Args, ArgStringList &CmdArgs) {
Stephen Hines176edba2014-12-01 14:53:08 -08002163 if (!(Args.hasFlag(options::OPT_fprofile_arcs, options::OPT_fno_profile_arcs,
2164 false) ||
Chandler Carruth9db37cd2013-06-23 11:28:48 +00002165 Args.hasArg(options::OPT_fprofile_generate) ||
Stephen Hines651f13c2014-04-23 16:59:28 -07002166 Args.hasArg(options::OPT_fprofile_instr_generate) ||
Chandler Carruth9db37cd2013-06-23 11:28:48 +00002167 Args.hasArg(options::OPT_fcreate_profile) ||
2168 Args.hasArg(options::OPT_coverage)))
2169 return;
2170
Stephen Hines651f13c2014-04-23 16:59:28 -07002171 SmallString<128> LibProfile = getCompilerRTLibDir(TC);
Stephen Hines176edba2014-12-01 14:53:08 -08002172 llvm::sys::path::append(LibProfile, Twine("libclang_rt.profile-") +
2173 getArchNameForCompilerRTLib(TC) +
2174 ".a");
Chandler Carruth9db37cd2013-06-23 11:28:48 +00002175
2176 CmdArgs.push_back(Args.MakeArgString(LibProfile));
2177}
2178
Stephen Hines651f13c2014-04-23 16:59:28 -07002179static SmallString<128> getSanitizerRTLibName(const ToolChain &TC,
Stephen Hines176edba2014-12-01 14:53:08 -08002180 StringRef Sanitizer,
Stephen Hines651f13c2014-04-23 16:59:28 -07002181 bool Shared) {
2182 // Sanitizer runtime has name "libclang_rt.<Sanitizer>-<ArchName>.{a,so}"
2183 // (or "libclang_rt.<Sanitizer>-<ArchName>-android.so for Android)
2184 const char *EnvSuffix =
2185 TC.getTriple().getEnvironment() == llvm::Triple::Android ? "-android" : "";
2186 SmallString<128> LibSanitizer = getCompilerRTLibDir(TC);
2187 llvm::sys::path::append(LibSanitizer,
2188 Twine("libclang_rt.") + Sanitizer + "-" +
2189 getArchNameForCompilerRTLib(TC) + EnvSuffix +
2190 (Shared ? ".so" : ".a"));
2191 return LibSanitizer;
2192}
2193
Stephen Hines176edba2014-12-01 14:53:08 -08002194static void addSanitizerRuntime(const ToolChain &TC, const ArgList &Args,
2195 ArgStringList &CmdArgs, StringRef Sanitizer,
2196 bool IsShared) {
2197 SmallString<128> LibSanitizer = getSanitizerRTLibName(TC, Sanitizer, IsShared);
2198 // Static runtimes must be forced into executable, so we wrap them in
Peter Collingbournebf548552013-10-20 21:29:13 +00002199 // whole-archive.
Stephen Hines176edba2014-12-01 14:53:08 -08002200 if (!IsShared)
2201 CmdArgs.push_back("-whole-archive");
2202 CmdArgs.push_back(Args.MakeArgString(LibSanitizer));
2203 if (!IsShared)
2204 CmdArgs.push_back("-no-whole-archive");
Alexey Samsonov86143042013-02-27 11:14:55 +00002205}
2206
Stephen Hines176edba2014-12-01 14:53:08 -08002207// Tries to use a file with the list of dynamic symbols that need to be exported
2208// from the runtime library. Returns true if the file was found.
2209static bool addSanitizerDynamicList(const ToolChain &TC, const ArgList &Args,
2210 ArgStringList &CmdArgs,
2211 StringRef Sanitizer) {
2212 SmallString<128> LibSanitizer = getSanitizerRTLibName(TC, Sanitizer, false);
2213 if (llvm::sys::fs::exists(LibSanitizer + ".syms")) {
2214 CmdArgs.push_back(
2215 Args.MakeArgString("--dynamic-list=" + LibSanitizer + ".syms"));
2216 return true;
2217 }
2218 return false;
2219}
2220
2221static void linkSanitizerRuntimeDeps(const ToolChain &TC,
2222 ArgStringList &CmdArgs) {
2223 // Force linking against the system libraries sanitizers depends on
2224 // (see PR15823 why this is necessary).
2225 CmdArgs.push_back("--no-as-needed");
2226 CmdArgs.push_back("-lpthread");
2227 CmdArgs.push_back("-lrt");
2228 CmdArgs.push_back("-lm");
2229 // There's no libdl on FreeBSD.
2230 if (TC.getTriple().getOS() != llvm::Triple::FreeBSD)
2231 CmdArgs.push_back("-ldl");
2232}
2233
2234static void
2235collectSanitizerRuntimes(const ToolChain &TC, const ArgList &Args,
2236 SmallVectorImpl<StringRef> &SharedRuntimes,
2237 SmallVectorImpl<StringRef> &StaticRuntimes,
2238 SmallVectorImpl<StringRef> &HelperStaticRuntimes) {
2239 const SanitizerArgs &SanArgs = TC.getSanitizerArgs();
2240 // Collect shared runtimes.
2241 if (SanArgs.needsAsanRt() && SanArgs.needsSharedAsanRt()) {
2242 SharedRuntimes.push_back("asan");
Evgeniy Stepanova6ddc022012-04-25 08:59:22 +00002243 }
Stephen Hines651f13c2014-04-23 16:59:28 -07002244
Stephen Hines176edba2014-12-01 14:53:08 -08002245 // Collect static runtimes.
Stephen Hines651f13c2014-04-23 16:59:28 -07002246 if (Args.hasArg(options::OPT_shared) ||
Stephen Hines176edba2014-12-01 14:53:08 -08002247 (TC.getTriple().getEnvironment() == llvm::Triple::Android)) {
2248 // Don't link static runtimes into DSOs or if compiling for Android.
Stephen Hines651f13c2014-04-23 16:59:28 -07002249 return;
Stephen Hines176edba2014-12-01 14:53:08 -08002250 }
2251 if (SanArgs.needsAsanRt()) {
2252 if (SanArgs.needsSharedAsanRt()) {
2253 HelperStaticRuntimes.push_back("asan-preinit");
2254 } else {
2255 StaticRuntimes.push_back("asan");
2256 if (SanArgs.linkCXXRuntimes())
2257 StaticRuntimes.push_back("asan_cxx");
2258 }
2259 }
2260 if (SanArgs.needsDfsanRt())
2261 StaticRuntimes.push_back("dfsan");
2262 if (SanArgs.needsLsanRt())
2263 StaticRuntimes.push_back("lsan");
2264 if (SanArgs.needsMsanRt())
2265 StaticRuntimes.push_back("msan");
2266 if (SanArgs.needsTsanRt())
2267 StaticRuntimes.push_back("tsan");
2268 // WARNING: UBSan should always go last.
2269 if (SanArgs.needsUbsanRt()) {
2270 // If UBSan is not combined with another sanitizer, we need to pull in
2271 // sanitizer_common explicitly.
2272 if (StaticRuntimes.empty())
2273 HelperStaticRuntimes.push_back("san");
2274 StaticRuntimes.push_back("ubsan");
2275 if (SanArgs.linkCXXRuntimes())
2276 StaticRuntimes.push_back("ubsan_cxx");
Stephen Hines6bcf27b2014-05-29 04:14:42 -07002277 }
Kostya Serebryanydff466c2011-11-30 01:39:16 +00002278}
2279
Stephen Hines176edba2014-12-01 14:53:08 -08002280// Should be called before we add system libraries (C++ ABI, libstdc++/libc++,
2281// C runtime, etc). Returns true if sanitizer system deps need to be linked in.
2282static bool addSanitizerRuntimes(const ToolChain &TC, const ArgList &Args,
Stephen Hines651f13c2014-04-23 16:59:28 -07002283 ArgStringList &CmdArgs) {
Stephen Hines176edba2014-12-01 14:53:08 -08002284 SmallVector<StringRef, 4> SharedRuntimes, StaticRuntimes,
2285 HelperStaticRuntimes;
2286 collectSanitizerRuntimes(TC, Args, SharedRuntimes, StaticRuntimes,
2287 HelperStaticRuntimes);
2288 for (auto RT : SharedRuntimes)
2289 addSanitizerRuntime(TC, Args, CmdArgs, RT, true);
2290 for (auto RT : HelperStaticRuntimes)
2291 addSanitizerRuntime(TC, Args, CmdArgs, RT, false);
2292 bool AddExportDynamic = false;
2293 for (auto RT : StaticRuntimes) {
2294 addSanitizerRuntime(TC, Args, CmdArgs, RT, false);
2295 AddExportDynamic |= !addSanitizerDynamicList(TC, Args, CmdArgs, RT);
2296 }
2297 // If there is a static runtime with no dynamic list, force all the symbols
2298 // to be dynamic to be sure we export sanitizer interface functions.
2299 if (AddExportDynamic)
2300 CmdArgs.push_back("-export-dynamic");
2301 return !StaticRuntimes.empty();
Peter Collingbourne2eeed712013-08-07 22:47:34 +00002302}
2303
Benjamin Kramer5322a552013-10-16 17:42:39 +00002304static bool shouldUseFramePointerForTarget(const ArgList &Args,
2305 const llvm::Triple &Triple) {
2306 switch (Triple.getArch()) {
2307 // Don't use a frame pointer on linux if optimizing for certain targets.
2308 case llvm::Triple::mips64:
2309 case llvm::Triple::mips64el:
2310 case llvm::Triple::mips:
2311 case llvm::Triple::mipsel:
2312 case llvm::Triple::systemz:
2313 case llvm::Triple::x86:
2314 case llvm::Triple::x86_64:
2315 if (Triple.isOSLinux())
2316 if (Arg *A = Args.getLastArg(options::OPT_O_Group))
2317 if (!A->getOption().matches(options::OPT_O0))
2318 return false;
2319 return true;
2320 case llvm::Triple::xcore:
2321 return false;
2322 default:
2323 return true;
2324 }
2325}
2326
Rafael Espindola6af27ec2011-12-14 21:02:23 +00002327static bool shouldUseFramePointer(const ArgList &Args,
2328 const llvm::Triple &Triple) {
2329 if (Arg *A = Args.getLastArg(options::OPT_fno_omit_frame_pointer,
2330 options::OPT_fomit_frame_pointer))
2331 return A->getOption().matches(options::OPT_fno_omit_frame_pointer);
2332
Benjamin Kramer5322a552013-10-16 17:42:39 +00002333 return shouldUseFramePointerForTarget(Args, Triple);
Rafael Espindola6af27ec2011-12-14 21:02:23 +00002334}
2335
Eric Christopherd3e22df2013-04-03 01:58:53 +00002336static bool shouldUseLeafFramePointer(const ArgList &Args,
2337 const llvm::Triple &Triple) {
2338 if (Arg *A = Args.getLastArg(options::OPT_mno_omit_leaf_frame_pointer,
2339 options::OPT_momit_leaf_frame_pointer))
2340 return A->getOption().matches(options::OPT_mno_omit_leaf_frame_pointer);
2341
Benjamin Kramer5322a552013-10-16 17:42:39 +00002342 return shouldUseFramePointerForTarget(Args, Triple);
Eric Christopherd3e22df2013-04-03 01:58:53 +00002343}
2344
Rafael Espindolaa2148242013-08-10 01:40:10 +00002345/// Add a CC1 option to specify the debug compilation directory.
Chandler Carruthd566df62012-12-17 21:40:04 +00002346static void addDebugCompDirArg(const ArgList &Args, ArgStringList &CmdArgs) {
Benjamin Kramer6089adc2013-04-27 08:12:29 +00002347 SmallString<128> cwd;
2348 if (!llvm::sys::fs::current_path(cwd)) {
Chad Rosierb1c81222013-04-26 20:49:50 +00002349 CmdArgs.push_back("-fdebug-compilation-dir");
2350 CmdArgs.push_back(Args.MakeArgString(cwd));
Chandler Carruthd566df62012-12-17 21:40:04 +00002351 }
2352}
2353
Eric Christopher80190392013-02-22 20:12:52 +00002354static const char *SplitDebugName(const ArgList &Args,
2355 const InputInfoList &Inputs) {
2356 Arg *FinalOutput = Args.getLastArg(options::OPT_o);
2357 if (FinalOutput && Args.hasArg(options::OPT_c)) {
2358 SmallString<128> T(FinalOutput->getValue());
2359 llvm::sys::path::replace_extension(T, "dwo");
2360 return Args.MakeArgString(T);
2361 } else {
2362 // Use the compilation dir.
Stephen Hines6bcf27b2014-05-29 04:14:42 -07002363 SmallString<128> T(
2364 Args.getLastArgValue(options::OPT_fdebug_compilation_dir));
Eric Christopher80190392013-02-22 20:12:52 +00002365 SmallString<128> F(llvm::sys::path::stem(Inputs[0].getBaseInput()));
2366 llvm::sys::path::replace_extension(F, "dwo");
2367 T += F;
2368 return Args.MakeArgString(F);
2369 }
2370}
2371
2372static void SplitDebugInfo(const ToolChain &TC, Compilation &C,
2373 const Tool &T, const JobAction &JA,
2374 const ArgList &Args, const InputInfo &Output,
2375 const char *OutFile) {
Eric Christopher59320e72013-02-21 22:35:01 +00002376 ArgStringList ExtractArgs;
2377 ExtractArgs.push_back("--extract-dwo");
2378
2379 ArgStringList StripArgs;
2380 StripArgs.push_back("--strip-dwo");
2381
2382 // Grabbing the output of the earlier compile step.
2383 StripArgs.push_back(Output.getFilename());
2384 ExtractArgs.push_back(Output.getFilename());
Eric Christopher59320e72013-02-21 22:35:01 +00002385 ExtractArgs.push_back(OutFile);
2386
2387 const char *Exec =
Eric Christopher80190392013-02-22 20:12:52 +00002388 Args.MakeArgString(TC.GetProgramPath("objcopy"));
Eric Christopher59320e72013-02-21 22:35:01 +00002389
2390 // First extract the dwo sections.
Stephen Hines176edba2014-12-01 14:53:08 -08002391 C.addCommand(llvm::make_unique<Command>(JA, T, Exec, ExtractArgs));
Eric Christopher59320e72013-02-21 22:35:01 +00002392
2393 // Then remove them from the original .o file.
Stephen Hines176edba2014-12-01 14:53:08 -08002394 C.addCommand(llvm::make_unique<Command>(JA, T, Exec, StripArgs));
Eric Christopher59320e72013-02-21 22:35:01 +00002395}
2396
Arnold Schwaighofer99662a12013-08-13 15:46:23 +00002397/// \brief Vectorize at all optimization levels greater than 1 except for -Oz.
Stephen Hines6bcf27b2014-05-29 04:14:42 -07002398/// For -Oz the loop vectorizer is disable, while the slp vectorizer is enabled.
2399static bool shouldEnableVectorizerAtOLevel(const ArgList &Args, bool isSlpVec) {
Arnold Schwaighofer99662a12013-08-13 15:46:23 +00002400 if (Arg *A = Args.getLastArg(options::OPT_O_Group)) {
Rafael Espindola55ee1eb2013-08-27 16:58:15 +00002401 if (A->getOption().matches(options::OPT_O4) ||
2402 A->getOption().matches(options::OPT_Ofast))
Arnold Schwaighofer99662a12013-08-13 15:46:23 +00002403 return true;
2404
2405 if (A->getOption().matches(options::OPT_O0))
2406 return false;
2407
2408 assert(A->getOption().matches(options::OPT_O) && "Must have a -O flag");
2409
Rafael Espindola168de192013-08-26 14:05:41 +00002410 // Vectorize -Os.
Arnold Schwaighofer99662a12013-08-13 15:46:23 +00002411 StringRef S(A->getValue());
Rafael Espindola168de192013-08-26 14:05:41 +00002412 if (S == "s")
Arnold Schwaighofer99662a12013-08-13 15:46:23 +00002413 return true;
2414
Stephen Hines6bcf27b2014-05-29 04:14:42 -07002415 // Don't vectorize -Oz, unless it's the slp vectorizer.
Arnold Schwaighofer99662a12013-08-13 15:46:23 +00002416 if (S == "z")
Stephen Hines6bcf27b2014-05-29 04:14:42 -07002417 return isSlpVec;
Arnold Schwaighofer99662a12013-08-13 15:46:23 +00002418
2419 unsigned OptLevel = 0;
2420 if (S.getAsInteger(10, OptLevel))
2421 return false;
2422
2423 return OptLevel > 1;
2424 }
2425
2426 return false;
2427}
2428
Stephen Hines651f13c2014-04-23 16:59:28 -07002429/// Add -x lang to \p CmdArgs for \p Input.
2430static void addDashXForInput(const ArgList &Args, const InputInfo &Input,
2431 ArgStringList &CmdArgs) {
2432 // When using -verify-pch, we don't want to provide the type
2433 // 'precompiled-header' if it was inferred from the file extension
2434 if (Args.hasArg(options::OPT_verify_pch) && Input.getType() == types::TY_PCH)
2435 return;
2436
2437 CmdArgs.push_back("-x");
2438 if (Args.hasArg(options::OPT_rewrite_objc))
2439 CmdArgs.push_back(types::getTypeName(types::TY_PP_ObjCXX));
2440 else
2441 CmdArgs.push_back(types::getTypeName(Input.getType()));
2442}
2443
Stephen Hines176edba2014-12-01 14:53:08 -08002444static std::string getMSCompatibilityVersion(const char *VersionStr) {
2445 unsigned Version;
2446 if (StringRef(VersionStr).getAsInteger(10, Version))
2447 return "0";
2448
2449 if (Version < 100)
2450 return llvm::utostr_32(Version) + ".0";
2451
2452 if (Version < 10000)
2453 return llvm::utostr_32(Version / 100) + "." +
2454 llvm::utostr_32(Version % 100);
2455
2456 unsigned Build = 0, Factor = 1;
2457 for ( ; Version > 10000; Version = Version / 10, Factor = Factor * 10)
2458 Build = Build + (Version % 10) * Factor;
2459 return llvm::utostr_32(Version / 100) + "." +
2460 llvm::utostr_32(Version % 100) + "." +
2461 llvm::utostr_32(Build);
2462}
2463
Daniel Dunbar47ac7d22009-03-18 06:00:36 +00002464void Clang::ConstructJob(Compilation &C, const JobAction &JA,
Daniel Dunbar871adcf2009-03-18 07:06:02 +00002465 const InputInfo &Output,
Daniel Dunbar62cf6012009-03-18 06:07:59 +00002466 const InputInfoList &Inputs,
Daniel Dunbar1d460332009-03-18 10:01:51 +00002467 const ArgList &Args,
Daniel Dunbar47ac7d22009-03-18 06:00:36 +00002468 const char *LinkingOutput) const {
Daniel Dunbar0a80ba72010-03-20 04:52:14 +00002469 bool KernelOrKext = Args.hasArg(options::OPT_mkernel,
2470 options::OPT_fapple_kext);
Daniel Dunbaree788e72009-12-21 18:54:17 +00002471 const Driver &D = getToolChain().getDriver();
Daniel Dunbarb488c1d2009-03-18 08:07:30 +00002472 ArgStringList CmdArgs;
2473
Stephen Hines651f13c2014-04-23 16:59:28 -07002474 bool IsWindowsGNU = getToolChain().getTriple().isWindowsGNUEnvironment();
Stephen Hines6bcf27b2014-05-29 04:14:42 -07002475 bool IsWindowsCygnus =
2476 getToolChain().getTriple().isWindowsCygwinEnvironment();
Stephen Hines651f13c2014-04-23 16:59:28 -07002477 bool IsWindowsMSVC = getToolChain().getTriple().isWindowsMSVCEnvironment();
2478
Daniel Dunbar077ba6a2009-03-31 20:53:55 +00002479 assert(Inputs.size() == 1 && "Unable to handle multiple inputs.");
2480
Daniel Dunbar8ff5b282009-12-11 23:00:49 +00002481 // Invoke ourselves in -cc1 mode.
2482 //
2483 // FIXME: Implement custom jobs for internal actions.
2484 CmdArgs.push_back("-cc1");
2485
Daniel Dunbardd4fe002009-10-30 18:12:20 +00002486 // Add the "effective" target triple.
Daniel Dunbaraf07f932009-03-31 17:35:15 +00002487 CmdArgs.push_back("-triple");
Daniel Dunbar00577ad2010-08-23 22:35:37 +00002488 std::string TripleStr = getToolChain().ComputeEffectiveClangTriple(Args);
Daniel Dunbardd4fe002009-10-30 18:12:20 +00002489 CmdArgs.push_back(Args.MakeArgString(TripleStr));
Daniel Dunbar728a5122009-09-10 06:49:20 +00002490
Stephen Hines6bcf27b2014-05-29 04:14:42 -07002491 const llvm::Triple TT(TripleStr);
2492 if (TT.isOSWindows() && (TT.getArch() == llvm::Triple::arm ||
2493 TT.getArch() == llvm::Triple::thumb)) {
2494 unsigned Offset = TT.getArch() == llvm::Triple::arm ? 4 : 6;
2495 unsigned Version;
2496 TT.getArchName().substr(Offset).getAsInteger(10, Version);
2497 if (Version < 7)
2498 D.Diag(diag::err_target_unsupported_arch) << TT.getArchName()
2499 << TripleStr;
2500 }
2501
Stephen Hines651f13c2014-04-23 16:59:28 -07002502 // Push all default warning arguments that are specific to
2503 // the given target. These come before user provided warning options
2504 // are provided.
2505 getToolChain().addClangWarningOptions(CmdArgs);
2506
Daniel Dunbardd4fe002009-10-30 18:12:20 +00002507 // Select the appropriate action.
John McCall260611a2012-06-20 06:18:46 +00002508 RewriteKind rewriteKind = RK_None;
Fariborz Jahaniane982cc02012-04-04 18:50:28 +00002509
Daniel Dunbar1d460332009-03-18 10:01:51 +00002510 if (isa<AnalyzeJobAction>(JA)) {
2511 assert(JA.getType() == types::TY_Plist && "Invalid output type.");
2512 CmdArgs.push_back("-analyze");
Ted Kremenek30660a82012-03-06 20:06:33 +00002513 } else if (isa<MigrateJobAction>(JA)) {
2514 CmdArgs.push_back("-migrate");
Daniel Dunbar1d460332009-03-18 10:01:51 +00002515 } else if (isa<PreprocessJobAction>(JA)) {
Daniel Dunbarcd8e4c42009-03-30 06:36:42 +00002516 if (Output.getType() == types::TY_Dependencies)
2517 CmdArgs.push_back("-Eonly");
Fariborz Jahanian51be73d2013-03-18 19:41:18 +00002518 else {
Daniel Dunbarcd8e4c42009-03-30 06:36:42 +00002519 CmdArgs.push_back("-E");
Fariborz Jahanian51be73d2013-03-18 19:41:18 +00002520 if (Args.hasArg(options::OPT_rewrite_objc) &&
2521 !Args.hasArg(options::OPT_g_Group))
2522 CmdArgs.push_back("-P");
2523 }
Daniel Dunbar8767cbc2010-02-03 03:07:56 +00002524 } else if (isa<AssembleJobAction>(JA)) {
2525 CmdArgs.push_back("-emit-obj");
Daniel Dunbar99298002010-05-27 06:18:05 +00002526
David Blaikie73168db2013-07-25 21:19:01 +00002527 CollectArgsForIntegratedAssembler(C, Args, CmdArgs, D);
Daniel Dunbard02bba82010-11-19 16:23:35 +00002528
2529 // Also ignore explicit -force_cpusubtype_ALL option.
2530 (void) Args.hasArg(options::OPT_force__cpusubtype__ALL);
Daniel Dunbar1d460332009-03-18 10:01:51 +00002531 } else if (isa<PrecompileJobAction>(JA)) {
Argyrios Kyrtzidise5c35372010-08-11 23:27:58 +00002532 // Use PCH if the user requested it.
Daniel Dunbar0ebd9322009-10-15 20:02:44 +00002533 bool UsePCH = D.CCCUsePCH;
Daniel Dunbar0ebd9322009-10-15 20:02:44 +00002534
Aaron Ballman761322b2012-07-31 01:21:00 +00002535 if (JA.getType() == types::TY_Nothing)
2536 CmdArgs.push_back("-fsyntax-only");
2537 else if (UsePCH)
Douglas Gregordf91ef32009-04-18 00:34:01 +00002538 CmdArgs.push_back("-emit-pch");
2539 else
2540 CmdArgs.push_back("-emit-pth");
Stephen Hines651f13c2014-04-23 16:59:28 -07002541 } else if (isa<VerifyPCHJobAction>(JA)) {
2542 CmdArgs.push_back("-verify-pch");
Daniel Dunbar1d460332009-03-18 10:01:51 +00002543 } else {
2544 assert(isa<CompileJobAction>(JA) && "Invalid action for clang tool.");
Daniel Dunbarc21c4852009-04-08 23:54:23 +00002545
Daniel Dunbar1d460332009-03-18 10:01:51 +00002546 if (JA.getType() == types::TY_Nothing) {
2547 CmdArgs.push_back("-fsyntax-only");
Daniel Dunbar6c6424b2010-06-07 23:28:45 +00002548 } else if (JA.getType() == types::TY_LLVM_IR ||
2549 JA.getType() == types::TY_LTO_IR) {
Daniel Dunbar1d460332009-03-18 10:01:51 +00002550 CmdArgs.push_back("-emit-llvm");
Daniel Dunbar6c6424b2010-06-07 23:28:45 +00002551 } else if (JA.getType() == types::TY_LLVM_BC ||
2552 JA.getType() == types::TY_LTO_BC) {
Daniel Dunbar1d460332009-03-18 10:01:51 +00002553 CmdArgs.push_back("-emit-llvm-bc");
2554 } else if (JA.getType() == types::TY_PP_Asm) {
Daniel Dunbare3b8d072009-09-17 00:47:53 +00002555 CmdArgs.push_back("-S");
Daniel Dunbar5915fbf2009-09-01 16:57:46 +00002556 } else if (JA.getType() == types::TY_AST) {
2557 CmdArgs.push_back("-emit-pch");
Douglas Gregorc544ba02013-03-27 16:47:18 +00002558 } else if (JA.getType() == types::TY_ModuleFile) {
2559 CmdArgs.push_back("-module-file-info");
Daniel Dunbar64952502010-02-11 03:16:21 +00002560 } else if (JA.getType() == types::TY_RewrittenObjC) {
2561 CmdArgs.push_back("-rewrite-objc");
John McCall260611a2012-06-20 06:18:46 +00002562 rewriteKind = RK_NonFragile;
Fariborz Jahanian582b3952012-04-02 15:59:19 +00002563 } else if (JA.getType() == types::TY_RewrittenLegacyObjC) {
2564 CmdArgs.push_back("-rewrite-objc");
John McCall260611a2012-06-20 06:18:46 +00002565 rewriteKind = RK_Fragile;
Daniel Dunbar64952502010-02-11 03:16:21 +00002566 } else {
2567 assert(JA.getType() == types::TY_PP_Asm &&
2568 "Unexpected output type!");
Daniel Dunbar1d460332009-03-18 10:01:51 +00002569 }
Daniel Dunbarb488c1d2009-03-18 08:07:30 +00002570 }
2571
Stephen Hinesef822542014-07-21 00:47:37 -07002572 // We normally speed up the clang process a bit by skipping destructors at
2573 // exit, but when we're generating diagnostics we can rely on some of the
2574 // cleanup.
2575 if (!C.isForDiagnostics())
2576 CmdArgs.push_back("-disable-free");
Daniel Dunbar1d460332009-03-18 10:01:51 +00002577
John McCallb689afb2010-02-13 03:50:24 +00002578 // Disable the verification pass in -asserts builds.
2579#ifdef NDEBUG
2580 CmdArgs.push_back("-disable-llvm-verifier");
2581#endif
2582
Daniel Dunbarc9abc042009-04-08 05:11:16 +00002583 // Set the main file name, so that debug info works even with
2584 // -save-temps.
2585 CmdArgs.push_back("-main-file-name");
Bob Wilson66b8a662012-11-23 06:14:39 +00002586 CmdArgs.push_back(getBaseInputName(Args, Inputs));
Daniel Dunbarc9abc042009-04-08 05:11:16 +00002587
Daniel Dunbar3bbc7532009-04-08 18:03:55 +00002588 // Some flags which affect the language (via preprocessor
Bob Wilson66b8a662012-11-23 06:14:39 +00002589 // defines).
Daniel Dunbar3bbc7532009-04-08 18:03:55 +00002590 if (Args.hasArg(options::OPT_static))
2591 CmdArgs.push_back("-static-define");
2592
Daniel Dunbar1d460332009-03-18 10:01:51 +00002593 if (isa<AnalyzeJobAction>(JA)) {
Ted Kremenekb8bb3e72009-09-25 05:55:59 +00002594 // Enable region store model by default.
2595 CmdArgs.push_back("-analyzer-store=region");
2596
Ted Kremenekb40d06d2009-12-07 22:26:14 +00002597 // Treat blocks as analysis entry points.
2598 CmdArgs.push_back("-analyzer-opt-analyze-nested-blocks");
2599
Ted Kremenek51885072011-03-24 00:28:47 +00002600 CmdArgs.push_back("-analyzer-eagerly-assume");
2601
Daniel Dunbar1d460332009-03-18 10:01:51 +00002602 // Add default argument set.
Daniel Dunbard8fc0f22009-05-22 00:38:15 +00002603 if (!Args.hasArg(options::OPT__analyzer_no_default_checks)) {
Argyrios Kyrtzidis027a6ab2011-02-15 07:42:33 +00002604 CmdArgs.push_back("-analyzer-checker=core");
Ted Kremenek51885072011-03-24 00:28:47 +00002605
Stephen Hines651f13c2014-04-23 16:59:28 -07002606 if (!IsWindowsMSVC)
Argyrios Kyrtzidis027a6ab2011-02-15 07:42:33 +00002607 CmdArgs.push_back("-analyzer-checker=unix");
Ted Kremenek51885072011-03-24 00:28:47 +00002608
Argyrios Kyrtzidis027a6ab2011-02-15 07:42:33 +00002609 if (getToolChain().getTriple().getVendor() == llvm::Triple::Apple)
Ted Kremenek51885072011-03-24 00:28:47 +00002610 CmdArgs.push_back("-analyzer-checker=osx");
Ted Kremeneka8180e52012-01-20 06:00:17 +00002611
2612 CmdArgs.push_back("-analyzer-checker=deadcode");
Ted Kremenek8dc05062012-01-26 02:27:38 +00002613
Jordan Rosee449edc2013-04-05 17:55:07 +00002614 if (types::isCXX(Inputs[0].getType()))
2615 CmdArgs.push_back("-analyzer-checker=cplusplus");
2616
Stephen Hines6bcf27b2014-05-29 04:14:42 -07002617 // Enable the following experimental checkers for testing.
2618 CmdArgs.push_back(
2619 "-analyzer-checker=security.insecureAPI.UncheckedReturn");
Ted Kremenek8dc05062012-01-26 02:27:38 +00002620 CmdArgs.push_back("-analyzer-checker=security.insecureAPI.getpw");
2621 CmdArgs.push_back("-analyzer-checker=security.insecureAPI.gets");
2622 CmdArgs.push_back("-analyzer-checker=security.insecureAPI.mktemp");
2623 CmdArgs.push_back("-analyzer-checker=security.insecureAPI.mkstemp");
2624 CmdArgs.push_back("-analyzer-checker=security.insecureAPI.vfork");
Daniel Dunbard8fc0f22009-05-22 00:38:15 +00002625 }
Daniel Dunbarc21c4852009-04-08 23:54:23 +00002626
Daniel Dunbard8fc0f22009-05-22 00:38:15 +00002627 // Set the output format. The default is plist, for (lame) historical
2628 // reasons.
2629 CmdArgs.push_back("-analyzer-output");
2630 if (Arg *A = Args.getLastArg(options::OPT__analyzer_output))
Richard Smith1d489cf2012-11-01 04:30:05 +00002631 CmdArgs.push_back(A->getValue());
Daniel Dunbard8fc0f22009-05-22 00:38:15 +00002632 else
2633 CmdArgs.push_back("plist");
Daniel Dunbar1d460332009-03-18 10:01:51 +00002634
Ted Kremenek0647a7b2010-03-22 22:32:05 +00002635 // Disable the presentation of standard compiler warnings when
2636 // using --analyze. We only want to show static analyzer diagnostics
2637 // or frontend errors.
2638 CmdArgs.push_back("-w");
2639
Daniel Dunbar1d460332009-03-18 10:01:51 +00002640 // Add -Xanalyzer arguments when running as analyzer.
2641 Args.AddAllArgValues(CmdArgs, options::OPT_Xanalyzer);
Mike Stump1eb44332009-09-09 15:08:12 +00002642 }
2643
Daniel Dunbare2fd6642009-09-10 01:21:12 +00002644 CheckCodeGenerationOptions(D, Args);
2645
Peter Collingbourne52ca70d2013-04-09 04:35:11 +00002646 bool PIE = getToolChain().isPIEDefault();
2647 bool PIC = PIE || getToolChain().isPICDefault();
Chandler Carruth7ce816a2012-11-19 03:52:03 +00002648 bool IsPICLevelTwo = PIC;
Peter Collingbourne52ca70d2013-04-09 04:35:11 +00002649
Stephen Hines651f13c2014-04-23 16:59:28 -07002650 // Android-specific defaults for PIC/PIE
2651 if (getToolChain().getTriple().getEnvironment() == llvm::Triple::Android) {
2652 switch (getToolChain().getTriple().getArch()) {
2653 case llvm::Triple::arm:
2654 case llvm::Triple::armeb:
2655 case llvm::Triple::thumb:
2656 case llvm::Triple::thumbeb:
Stephen Hines6bcf27b2014-05-29 04:14:42 -07002657 case llvm::Triple::aarch64:
Stephen Hines651f13c2014-04-23 16:59:28 -07002658 case llvm::Triple::mips:
2659 case llvm::Triple::mipsel:
2660 case llvm::Triple::mips64:
2661 case llvm::Triple::mips64el:
2662 PIC = true; // "-fpic"
2663 break;
2664
2665 case llvm::Triple::x86:
2666 case llvm::Triple::x86_64:
2667 PIC = true; // "-fPIC"
2668 IsPICLevelTwo = true;
2669 break;
2670
2671 default:
2672 break;
2673 }
2674 }
2675
Stephen Hinesef822542014-07-21 00:47:37 -07002676 // OpenBSD-specific defaults for PIE
2677 if (getToolChain().getTriple().getOS() == llvm::Triple::OpenBSD) {
2678 switch (getToolChain().getTriple().getArch()) {
2679 case llvm::Triple::mips64:
2680 case llvm::Triple::mips64el:
2681 case llvm::Triple::sparc:
2682 case llvm::Triple::x86:
2683 case llvm::Triple::x86_64:
2684 IsPICLevelTwo = false; // "-fpie"
2685 break;
2686
2687 case llvm::Triple::ppc:
2688 case llvm::Triple::sparcv9:
2689 IsPICLevelTwo = true; // "-fPIE"
2690 break;
2691
2692 default:
2693 break;
2694 }
2695 }
2696
Alexey Samsonovdb68e5a2013-04-09 12:28:19 +00002697 // For the PIC and PIE flag options, this logic is different from the
2698 // legacy logic in very old versions of GCC, as that logic was just
2699 // a bug no one had ever fixed. This logic is both more rational and
2700 // consistent with GCC's new logic now that the bugs are fixed. The last
2701 // argument relating to either PIC or PIE wins, and no other argument is
2702 // used. If the last argument is any flavor of the '-fno-...' arguments,
2703 // both PIC and PIE are disabled. Any PIE option implicitly enables PIC
2704 // at the same level.
2705 Arg *LastPICArg =Args.getLastArg(options::OPT_fPIC, options::OPT_fno_PIC,
2706 options::OPT_fpic, options::OPT_fno_pic,
2707 options::OPT_fPIE, options::OPT_fno_PIE,
2708 options::OPT_fpie, options::OPT_fno_pie);
Chandler Carruth7ce816a2012-11-19 03:52:03 +00002709 // Check whether the tool chain trumps the PIC-ness decision. If the PIC-ness
2710 // is forced, then neither PIC nor PIE flags will have no effect.
Peter Collingbourne52ca70d2013-04-09 04:35:11 +00002711 if (!getToolChain().isPICDefaultForced()) {
Alexey Samsonovdb68e5a2013-04-09 12:28:19 +00002712 if (LastPICArg) {
2713 Option O = LastPICArg->getOption();
Peter Collingbourne52ca70d2013-04-09 04:35:11 +00002714 if (O.matches(options::OPT_fPIC) || O.matches(options::OPT_fpic) ||
2715 O.matches(options::OPT_fPIE) || O.matches(options::OPT_fpie)) {
2716 PIE = O.matches(options::OPT_fPIE) || O.matches(options::OPT_fpie);
2717 PIC = PIE || O.matches(options::OPT_fPIC) ||
2718 O.matches(options::OPT_fpic);
2719 IsPICLevelTwo = O.matches(options::OPT_fPIE) ||
2720 O.matches(options::OPT_fPIC);
2721 } else {
2722 PIE = PIC = false;
2723 }
2724 }
Chandler Carruth5e219cf2012-04-08 16:40:35 +00002725 }
Chandler Carruth7ce816a2012-11-19 03:52:03 +00002726
Nick Lewyckyd4705682013-10-11 03:33:53 +00002727 // Introduce a Darwin-specific hack. If the default is PIC but the flags
Chandler Carruth7ce816a2012-11-19 03:52:03 +00002728 // specified while enabling PIC enabled level 1 PIC, just force it back to
2729 // level 2 PIC instead. This matches the behavior of Darwin GCC (based on my
2730 // informal testing).
2731 if (PIC && getToolChain().getTriple().isOSDarwin())
2732 IsPICLevelTwo |= getToolChain().isPICDefault();
2733
Chandler Carruth5e219cf2012-04-08 16:40:35 +00002734 // Note that these flags are trump-cards. Regardless of the order w.r.t. the
2735 // PIC or PIE options above, if these show up, PIC is disabled.
Daniel Dunbar7a0c0642012-10-15 22:23:53 +00002736 llvm::Triple Triple(TripleStr);
Stephen Hines651f13c2014-04-23 16:59:28 -07002737 if (KernelOrKext && (!Triple.isiOS() || Triple.isOSVersionLT(6) ||
Stephen Hines6bcf27b2014-05-29 04:14:42 -07002738 Triple.getArch() == llvm::Triple::aarch64))
Chandler Carruth7ce816a2012-11-19 03:52:03 +00002739 PIC = PIE = false;
Simon Atanasyanc0e83642013-10-04 11:46:54 +00002740 if (Args.hasArg(options::OPT_static))
Chandler Carruth7ce816a2012-11-19 03:52:03 +00002741 PIC = PIE = false;
Chandler Carruth5e219cf2012-04-08 16:40:35 +00002742
Chandler Carruth7ce816a2012-11-19 03:52:03 +00002743 if (Arg *A = Args.getLastArg(options::OPT_mdynamic_no_pic)) {
2744 // This is a very special mode. It trumps the other modes, almost no one
2745 // uses it, and it isn't even valid on any OS but Darwin.
2746 if (!getToolChain().getTriple().isOSDarwin())
2747 D.Diag(diag::err_drv_unsupported_opt_for_target)
2748 << A->getSpelling() << getToolChain().getTriple().str();
2749
2750 // FIXME: Warn when this flag trumps some other PIC or PIE flag.
2751
Daniel Dunbarf219e7c2009-11-29 07:18:39 +00002752 CmdArgs.push_back("-mrelocation-model");
Chandler Carruth7ce816a2012-11-19 03:52:03 +00002753 CmdArgs.push_back("dynamic-no-pic");
Daniel Dunbarbc85be82009-04-29 18:32:25 +00002754
Chandler Carruth7ce816a2012-11-19 03:52:03 +00002755 // Only a forced PIC mode can cause the actual compile to have PIC defines
2756 // etc., no flags are sufficient. This behavior was selected to closely
2757 // match that of llvm-gcc and Apple GCC before that.
2758 if (getToolChain().isPICDefault() && getToolChain().isPICDefaultForced()) {
2759 CmdArgs.push_back("-pic-level");
2760 CmdArgs.push_back("2");
2761 }
2762 } else {
2763 // Currently, LLVM only knows about PIC vs. static; the PIE differences are
2764 // handled in Clang's IRGen by the -pie-level flag.
2765 CmdArgs.push_back("-mrelocation-model");
2766 CmdArgs.push_back(PIC ? "pic" : "static");
2767
2768 if (PIC) {
2769 CmdArgs.push_back("-pic-level");
2770 CmdArgs.push_back(IsPICLevelTwo ? "2" : "1");
2771 if (PIE) {
2772 CmdArgs.push_back("-pie-level");
2773 CmdArgs.push_back(IsPICLevelTwo ? "2" : "1");
2774 }
2775 }
Daniel Dunbarbc85be82009-04-29 18:32:25 +00002776 }
Chandler Carruth5e219cf2012-04-08 16:40:35 +00002777
Stephen Hines176edba2014-12-01 14:53:08 -08002778 CmdArgs.push_back("-mthread-model");
2779 if (Arg *A = Args.getLastArg(options::OPT_mthread_model))
2780 CmdArgs.push_back(A->getValue());
2781 else
2782 CmdArgs.push_back(Args.MakeArgString(getToolChain().getThreadModel()));
2783
Tanya Lattner59876c22009-11-04 01:18:09 +00002784 if (!Args.hasFlag(options::OPT_fmerge_all_constants,
2785 options::OPT_fno_merge_all_constants))
Chris Lattnerf44a1a02011-04-08 18:06:54 +00002786 CmdArgs.push_back("-fno-merge-all-constants");
Daniel Dunbar6bea73b2009-09-16 06:17:29 +00002787
Daniel Dunbarf219e7c2009-11-29 07:18:39 +00002788 // LLVM Code Generator Options.
2789
Stephen Hinesef822542014-07-21 00:47:37 -07002790 if (Arg *A = Args.getLastArg(options::OPT_Wframe_larger_than_EQ)) {
2791 StringRef v = A->getValue();
2792 CmdArgs.push_back("-mllvm");
2793 CmdArgs.push_back(Args.MakeArgString("-warn-stack-size=" + v));
2794 A->claim();
2795 }
2796
Daniel Dunbar17d3fea2011-02-09 17:54:19 +00002797 if (Arg *A = Args.getLastArg(options::OPT_mregparm_EQ)) {
2798 CmdArgs.push_back("-mregparm");
Richard Smith1d489cf2012-11-01 04:30:05 +00002799 CmdArgs.push_back(A->getValue());
Daniel Dunbar17d3fea2011-02-09 17:54:19 +00002800 }
2801
Nick Lewyckyfdf137b2013-06-25 01:49:44 +00002802 if (Arg *A = Args.getLastArg(options::OPT_fpcc_struct_return,
2803 options::OPT_freg_struct_return)) {
Eli Bendersky8f4269a2013-07-24 22:20:49 +00002804 if (getToolChain().getArch() != llvm::Triple::x86) {
John McCallb8b52972013-06-18 02:46:29 +00002805 D.Diag(diag::err_drv_unsupported_opt_for_target)
2806 << A->getSpelling() << getToolChain().getTriple().str();
2807 } else if (A->getOption().matches(options::OPT_fpcc_struct_return)) {
2808 CmdArgs.push_back("-fpcc-struct-return");
2809 } else {
2810 assert(A->getOption().matches(options::OPT_freg_struct_return));
2811 CmdArgs.push_back("-freg-struct-return");
2812 }
2813 }
2814
Roman Divackycfe9af22011-03-01 17:40:53 +00002815 if (Args.hasFlag(options::OPT_mrtd, options::OPT_mno_rtd, false))
2816 CmdArgs.push_back("-mrtd");
2817
Rafael Espindola6af27ec2011-12-14 21:02:23 +00002818 if (shouldUseFramePointer(Args, getToolChain().getTriple()))
Daniel Dunbarf219e7c2009-11-29 07:18:39 +00002819 CmdArgs.push_back("-mdisable-fp-elim");
2820 if (!Args.hasFlag(options::OPT_fzero_initialized_in_bss,
2821 options::OPT_fno_zero_initialized_in_bss))
2822 CmdArgs.push_back("-mno-zero-initialized-in-bss");
Chad Rosierb82e1172013-04-24 18:09:54 +00002823
2824 bool OFastEnabled = isOptimizationLevelFast(Args);
2825 // If -Ofast is the optimization level, then -fstrict-aliasing should be
2826 // enabled. This alias option is being used to simplify the hasFlag logic.
2827 OptSpecifier StrictAliasingAliasOption = OFastEnabled ? options::OPT_Ofast :
2828 options::OPT_fstrict_aliasing;
Stephen Hines6bcf27b2014-05-29 04:14:42 -07002829 // We turn strict aliasing off by default if we're in CL mode, since MSVC
2830 // doesn't do any TBAA.
2831 bool TBAAOnByDefault = !getToolChain().getDriver().IsCLMode();
Chad Rosierb82e1172013-04-24 18:09:54 +00002832 if (!Args.hasFlag(options::OPT_fstrict_aliasing, StrictAliasingAliasOption,
Stephen Hines6bcf27b2014-05-29 04:14:42 -07002833 options::OPT_fno_strict_aliasing, TBAAOnByDefault))
Dan Gohman4d5625e2010-10-14 22:36:56 +00002834 CmdArgs.push_back("-relaxed-aliasing");
Manman Ren96d6c452013-10-11 20:48:38 +00002835 if (!Args.hasFlag(options::OPT_fstruct_path_tbaa,
2836 options::OPT_fno_struct_path_tbaa))
2837 CmdArgs.push_back("-no-struct-path-tbaa");
Chandler Carruth82fe6ae2012-03-27 23:58:37 +00002838 if (Args.hasFlag(options::OPT_fstrict_enums, options::OPT_fno_strict_enums,
2839 false))
2840 CmdArgs.push_back("-fstrict-enums");
Nick Lewycky1db772b2012-01-23 08:29:12 +00002841 if (!Args.hasFlag(options::OPT_foptimize_sibling_calls,
2842 options::OPT_fno_optimize_sibling_calls))
2843 CmdArgs.push_back("-mdisable-tail-calls");
Daniel Dunbar1b718482010-05-14 22:00:22 +00002844
Eric Christopher31056272013-04-04 06:29:47 +00002845 // Handle segmented stacks.
2846 if (Args.hasArg(options::OPT_fsplit_stack))
2847 CmdArgs.push_back("-split-stacks");
Chad Rosierb82e1172013-04-24 18:09:54 +00002848
2849 // If -Ofast is the optimization level, then -ffast-math should be enabled.
2850 // This alias option is being used to simplify the getLastArg logic.
2851 OptSpecifier FastMathAliasOption = OFastEnabled ? options::OPT_Ofast :
2852 options::OPT_ffast_math;
Eric Christopher31056272013-04-04 06:29:47 +00002853
Chandler Carruthabf07a72012-01-02 14:19:45 +00002854 // Handle various floating point optimization flags, mapping them to the
2855 // appropriate LLVM code generation flags. The pattern for all of these is to
2856 // default off the codegen optimizations, and if any flag enables them and no
2857 // flag disables them after the flag enabling them, enable the codegen
2858 // optimization. This is complicated by several "umbrella" flags.
Chad Rosierb82e1172013-04-24 18:09:54 +00002859 if (Arg *A = Args.getLastArg(options::OPT_ffast_math, FastMathAliasOption,
Chad Rosier80ecf5e2012-09-25 22:03:25 +00002860 options::OPT_fno_fast_math,
Chandler Carruthabf07a72012-01-02 14:19:45 +00002861 options::OPT_ffinite_math_only,
2862 options::OPT_fno_finite_math_only,
2863 options::OPT_fhonor_infinities,
2864 options::OPT_fno_honor_infinities))
Chad Rosier80ecf5e2012-09-25 22:03:25 +00002865 if (A->getOption().getID() != options::OPT_fno_fast_math &&
2866 A->getOption().getID() != options::OPT_fno_finite_math_only &&
Chandler Carruthabf07a72012-01-02 14:19:45 +00002867 A->getOption().getID() != options::OPT_fhonor_infinities)
2868 CmdArgs.push_back("-menable-no-infs");
Chad Rosierb82e1172013-04-24 18:09:54 +00002869 if (Arg *A = Args.getLastArg(options::OPT_ffast_math, FastMathAliasOption,
Chad Rosier80ecf5e2012-09-25 22:03:25 +00002870 options::OPT_fno_fast_math,
Chandler Carruthabf07a72012-01-02 14:19:45 +00002871 options::OPT_ffinite_math_only,
2872 options::OPT_fno_finite_math_only,
2873 options::OPT_fhonor_nans,
2874 options::OPT_fno_honor_nans))
Chad Rosier80ecf5e2012-09-25 22:03:25 +00002875 if (A->getOption().getID() != options::OPT_fno_fast_math &&
2876 A->getOption().getID() != options::OPT_fno_finite_math_only &&
Chandler Carruthabf07a72012-01-02 14:19:45 +00002877 A->getOption().getID() != options::OPT_fhonor_nans)
2878 CmdArgs.push_back("-menable-no-nans");
2879
Benjamin Kramer769aa2d2012-05-02 14:55:48 +00002880 // -fmath-errno is the default on some platforms, e.g. BSD-derived OSes.
2881 bool MathErrno = getToolChain().IsMathErrnoDefault();
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_fmath_errno,
Chandler Carruthb69557e2013-05-18 20:47:36 +00002885 options::OPT_fno_math_errno)) {
2886 // Turning on -ffast_math (with either flag) removes the need for MathErrno.
2887 // However, turning *off* -ffast_math merely restores the toolchain default
2888 // (which may be false).
2889 if (A->getOption().getID() == options::OPT_fno_math_errno ||
2890 A->getOption().getID() == options::OPT_ffast_math ||
2891 A->getOption().getID() == options::OPT_Ofast)
2892 MathErrno = false;
2893 else if (A->getOption().getID() == options::OPT_fmath_errno)
2894 MathErrno = true;
2895 }
Chandler Carruth4f50c502012-04-26 02:10:51 +00002896 if (MathErrno)
2897 CmdArgs.push_back("-fmath-errno");
Chandler Carruthabf07a72012-01-02 14:19:45 +00002898
2899 // There are several flags which require disabling very specific
2900 // optimizations. Any of these being disabled forces us to turn off the
2901 // entire set of LLVM optimizations, so collect them through all the flag
2902 // madness.
2903 bool AssociativeMath = false;
Chad Rosierb82e1172013-04-24 18:09:54 +00002904 if (Arg *A = Args.getLastArg(options::OPT_ffast_math, FastMathAliasOption,
Chad Rosier80ecf5e2012-09-25 22:03:25 +00002905 options::OPT_fno_fast_math,
Chandler Carruthabf07a72012-01-02 14:19:45 +00002906 options::OPT_funsafe_math_optimizations,
2907 options::OPT_fno_unsafe_math_optimizations,
2908 options::OPT_fassociative_math,
2909 options::OPT_fno_associative_math))
Chad Rosier80ecf5e2012-09-25 22:03:25 +00002910 if (A->getOption().getID() != options::OPT_fno_fast_math &&
2911 A->getOption().getID() != options::OPT_fno_unsafe_math_optimizations &&
Chandler Carruthabf07a72012-01-02 14:19:45 +00002912 A->getOption().getID() != options::OPT_fno_associative_math)
2913 AssociativeMath = true;
2914 bool ReciprocalMath = false;
Chad Rosierb82e1172013-04-24 18:09:54 +00002915 if (Arg *A = Args.getLastArg(options::OPT_ffast_math, FastMathAliasOption,
Chad Rosier80ecf5e2012-09-25 22:03:25 +00002916 options::OPT_fno_fast_math,
Chandler Carruthabf07a72012-01-02 14:19:45 +00002917 options::OPT_funsafe_math_optimizations,
2918 options::OPT_fno_unsafe_math_optimizations,
2919 options::OPT_freciprocal_math,
2920 options::OPT_fno_reciprocal_math))
Chad Rosier80ecf5e2012-09-25 22:03:25 +00002921 if (A->getOption().getID() != options::OPT_fno_fast_math &&
2922 A->getOption().getID() != options::OPT_fno_unsafe_math_optimizations &&
Chandler Carruthabf07a72012-01-02 14:19:45 +00002923 A->getOption().getID() != options::OPT_fno_reciprocal_math)
2924 ReciprocalMath = true;
2925 bool SignedZeros = true;
Chad Rosierb82e1172013-04-24 18:09:54 +00002926 if (Arg *A = Args.getLastArg(options::OPT_ffast_math, FastMathAliasOption,
Chad Rosier80ecf5e2012-09-25 22:03:25 +00002927 options::OPT_fno_fast_math,
Chandler Carruthabf07a72012-01-02 14:19:45 +00002928 options::OPT_funsafe_math_optimizations,
2929 options::OPT_fno_unsafe_math_optimizations,
2930 options::OPT_fsigned_zeros,
2931 options::OPT_fno_signed_zeros))
Chad Rosier80ecf5e2012-09-25 22:03:25 +00002932 if (A->getOption().getID() != options::OPT_fno_fast_math &&
2933 A->getOption().getID() != options::OPT_fno_unsafe_math_optimizations &&
Chandler Carruthabf07a72012-01-02 14:19:45 +00002934 A->getOption().getID() != options::OPT_fsigned_zeros)
2935 SignedZeros = false;
2936 bool TrappingMath = true;
Chad Rosierb82e1172013-04-24 18:09:54 +00002937 if (Arg *A = Args.getLastArg(options::OPT_ffast_math, FastMathAliasOption,
Chad Rosier80ecf5e2012-09-25 22:03:25 +00002938 options::OPT_fno_fast_math,
Chandler Carruthabf07a72012-01-02 14:19:45 +00002939 options::OPT_funsafe_math_optimizations,
2940 options::OPT_fno_unsafe_math_optimizations,
2941 options::OPT_ftrapping_math,
2942 options::OPT_fno_trapping_math))
Chad Rosier80ecf5e2012-09-25 22:03:25 +00002943 if (A->getOption().getID() != options::OPT_fno_fast_math &&
2944 A->getOption().getID() != options::OPT_fno_unsafe_math_optimizations &&
Chandler Carruthabf07a72012-01-02 14:19:45 +00002945 A->getOption().getID() != options::OPT_ftrapping_math)
2946 TrappingMath = false;
2947 if (!MathErrno && AssociativeMath && ReciprocalMath && !SignedZeros &&
2948 !TrappingMath)
2949 CmdArgs.push_back("-menable-unsafe-fp-math");
2950
Lang Hamesc9686712012-07-06 00:59:19 +00002951
2952 // Validate and pass through -fp-contract option.
Chad Rosierb82e1172013-04-24 18:09:54 +00002953 if (Arg *A = Args.getLastArg(options::OPT_ffast_math, FastMathAliasOption,
Chad Rosier80ecf5e2012-09-25 22:03:25 +00002954 options::OPT_fno_fast_math,
Lang Hamesc9686712012-07-06 00:59:19 +00002955 options::OPT_ffp_contract)) {
2956 if (A->getOption().getID() == options::OPT_ffp_contract) {
Richard Smith1d489cf2012-11-01 04:30:05 +00002957 StringRef Val = A->getValue();
Lang Hamesc9686712012-07-06 00:59:19 +00002958 if (Val == "fast" || Val == "on" || Val == "off") {
2959 CmdArgs.push_back(Args.MakeArgString("-ffp-contract=" + Val));
2960 } else {
2961 D.Diag(diag::err_drv_unsupported_option_argument)
2962 << A->getOption().getName() << Val;
2963 }
Chad Rosierb82e1172013-04-24 18:09:54 +00002964 } else if (A->getOption().matches(options::OPT_ffast_math) ||
2965 (OFastEnabled && A->getOption().matches(options::OPT_Ofast))) {
Lang Hamesc9686712012-07-06 00:59:19 +00002966 // If fast-math is set then set the fp-contract mode to fast.
2967 CmdArgs.push_back(Args.MakeArgString("-ffp-contract=fast"));
2968 }
2969 }
2970
Bob Wilson455e72e2012-07-19 03:52:53 +00002971 // We separately look for the '-ffast-math' and '-ffinite-math-only' flags,
2972 // and if we find them, tell the frontend to provide the appropriate
2973 // preprocessor macros. This is distinct from enabling any optimizations as
2974 // these options induce language changes which must survive serialization
2975 // and deserialization, etc.
Chad Rosierb82e1172013-04-24 18:09:54 +00002976 if (Arg *A = Args.getLastArg(options::OPT_ffast_math, FastMathAliasOption,
2977 options::OPT_fno_fast_math))
2978 if (!A->getOption().matches(options::OPT_fno_fast_math))
2979 CmdArgs.push_back("-ffast-math");
Stephen Hines6bcf27b2014-05-29 04:14:42 -07002980 if (Arg *A = Args.getLastArg(options::OPT_ffinite_math_only,
2981 options::OPT_fno_fast_math))
Chad Rosier80ecf5e2012-09-25 22:03:25 +00002982 if (A->getOption().matches(options::OPT_ffinite_math_only))
2983 CmdArgs.push_back("-ffinite-math-only");
Chandler Carruthabf07a72012-01-02 14:19:45 +00002984
Daniel Dunbar1b718482010-05-14 22:00:22 +00002985 // Decide whether to use verbose asm. Verbose assembly is the default on
2986 // toolchains which have the integrated assembler on by default.
Stephen Hines651f13c2014-04-23 16:59:28 -07002987 bool IsIntegratedAssemblerDefault =
2988 getToolChain().IsIntegratedAssemblerDefault();
Daniel Dunbar1b718482010-05-14 22:00:22 +00002989 if (Args.hasFlag(options::OPT_fverbose_asm, options::OPT_fno_verbose_asm,
Stephen Hines651f13c2014-04-23 16:59:28 -07002990 IsIntegratedAssemblerDefault) ||
Daniel Dunbar1b718482010-05-14 22:00:22 +00002991 Args.hasArg(options::OPT_dA))
Daniel Dunbarf219e7c2009-11-29 07:18:39 +00002992 CmdArgs.push_back("-masm-verbose");
Daniel Dunbar1b718482010-05-14 22:00:22 +00002993
Stephen Hines651f13c2014-04-23 16:59:28 -07002994 if (!Args.hasFlag(options::OPT_fintegrated_as, options::OPT_fno_integrated_as,
2995 IsIntegratedAssemblerDefault))
2996 CmdArgs.push_back("-no-integrated-as");
2997
Daniel Dunbarf219e7c2009-11-29 07:18:39 +00002998 if (Args.hasArg(options::OPT_fdebug_pass_structure)) {
2999 CmdArgs.push_back("-mdebug-pass");
3000 CmdArgs.push_back("Structure");
3001 }
3002 if (Args.hasArg(options::OPT_fdebug_pass_arguments)) {
3003 CmdArgs.push_back("-mdebug-pass");
3004 CmdArgs.push_back("Arguments");
3005 }
3006
John McCalld0c2ec42010-02-19 02:45:38 +00003007 // Enable -mconstructor-aliases except on darwin, where we have to
3008 // work around a linker bug; see <rdar://problem/7651567>.
Bob Wilson905c45f2011-10-14 05:03:44 +00003009 if (!getToolChain().getTriple().isOSDarwin())
John McCalld0c2ec42010-02-19 02:45:38 +00003010 CmdArgs.push_back("-mconstructor-aliases");
NAKAMURA Takumi125b4cb2011-02-17 08:50:50 +00003011
John McCall32096692011-03-18 02:56:14 +00003012 // Darwin's kernel doesn't support guard variables; just die if we
3013 // try to use them.
Bob Wilson905c45f2011-10-14 05:03:44 +00003014 if (KernelOrKext && getToolChain().getTriple().isOSDarwin())
John McCall32096692011-03-18 02:56:14 +00003015 CmdArgs.push_back("-fforbid-guard-variables");
3016
Douglas Gregor6f755502011-02-01 15:15:22 +00003017 if (Args.hasArg(options::OPT_mms_bitfields)) {
3018 CmdArgs.push_back("-mms-bitfields");
3019 }
John McCalld0c2ec42010-02-19 02:45:38 +00003020
Daniel Dunbar6bea73b2009-09-16 06:17:29 +00003021 // This is a coarse approximation of what llvm-gcc actually does, both
3022 // -fasynchronous-unwind-tables and -fnon-call-exceptions interact in more
3023 // complicated ways.
3024 bool AsynchronousUnwindTables =
Stephen Hines651f13c2014-04-23 16:59:28 -07003025 Args.hasFlag(options::OPT_fasynchronous_unwind_tables,
3026 options::OPT_fno_asynchronous_unwind_tables,
3027 (getToolChain().IsUnwindTablesDefault() ||
3028 getToolChain().getSanitizerArgs().needsUnwindTables()) &&
3029 !KernelOrKext);
Daniel Dunbar6bea73b2009-09-16 06:17:29 +00003030 if (Args.hasFlag(options::OPT_funwind_tables, options::OPT_fno_unwind_tables,
3031 AsynchronousUnwindTables))
Daniel Dunbarf219e7c2009-11-29 07:18:39 +00003032 CmdArgs.push_back("-munwind-tables");
3033
Chandler Carrutha6b25812012-11-21 23:40:23 +00003034 getToolChain().addClangTargetOptions(Args, CmdArgs);
Rafael Espindola8af669f2012-06-19 01:26:10 +00003035
Daniel Dunbarf219e7c2009-11-29 07:18:39 +00003036 if (Arg *A = Args.getLastArg(options::OPT_flimited_precision_EQ)) {
3037 CmdArgs.push_back("-mlimit-float-precision");
Richard Smith1d489cf2012-11-01 04:30:05 +00003038 CmdArgs.push_back(A->getValue());
Daniel Dunbarf219e7c2009-11-29 07:18:39 +00003039 }
Daniel Dunbarbc85be82009-04-29 18:32:25 +00003040
Daniel Dunbar868bd0a2009-05-06 03:16:41 +00003041 // FIXME: Handle -mtune=.
3042 (void) Args.hasArg(options::OPT_mtune_EQ);
Daniel Dunbarbc85be82009-04-29 18:32:25 +00003043
Benjamin Kramer8e9ef0d2009-08-05 14:30:52 +00003044 if (Arg *A = Args.getLastArg(options::OPT_mcmodel_EQ)) {
Daniel Dunbarf219e7c2009-11-29 07:18:39 +00003045 CmdArgs.push_back("-mcode-model");
Richard Smith1d489cf2012-11-01 04:30:05 +00003046 CmdArgs.push_back(A->getValue());
Benjamin Kramer8e9ef0d2009-08-05 14:30:52 +00003047 }
3048
Rafael Espindolab330e402013-08-20 22:12:08 +00003049 // Add the target cpu
3050 std::string ETripleStr = getToolChain().ComputeEffectiveClangTriple(Args);
3051 llvm::Triple ETriple(ETripleStr);
3052 std::string CPU = getCPUName(Args, ETriple);
3053 if (!CPU.empty()) {
3054 CmdArgs.push_back("-target-cpu");
3055 CmdArgs.push_back(Args.MakeArgString(CPU));
3056 }
3057
Rafael Espindola5389b842013-08-21 21:59:03 +00003058 if (const Arg *A = Args.getLastArg(options::OPT_mfpmath_EQ)) {
3059 CmdArgs.push_back("-mfpmath");
3060 CmdArgs.push_back(A->getValue());
3061 }
3062
Rafael Espindola146dbbf2013-08-21 16:39:20 +00003063 // Add the target features
Stephen Hines651f13c2014-04-23 16:59:28 -07003064 getTargetFeatures(D, ETriple, Args, CmdArgs, false);
Rafael Espindola146dbbf2013-08-21 16:39:20 +00003065
Rafael Espindolab330e402013-08-20 22:12:08 +00003066 // Add target specific flags.
Eli Bendersky8f4269a2013-07-24 22:20:49 +00003067 switch(getToolChain().getArch()) {
Daniel Dunbar6acda162009-09-09 22:33:08 +00003068 default:
3069 break;
Daniel Dunbar868bd0a2009-05-06 03:16:41 +00003070
Daniel Dunbarb163ef72009-09-10 04:57:17 +00003071 case llvm::Triple::arm:
Stephen Hines651f13c2014-04-23 16:59:28 -07003072 case llvm::Triple::armeb:
Daniel Dunbarb163ef72009-09-10 04:57:17 +00003073 case llvm::Triple::thumb:
Stephen Hines651f13c2014-04-23 16:59:28 -07003074 case llvm::Triple::thumbeb:
Daniel Dunbarfa41d692011-03-17 17:10:06 +00003075 AddARMTargetArgs(Args, CmdArgs, KernelOrKext);
Daniel Dunbarb163ef72009-09-10 04:57:17 +00003076 break;
3077
Stephen Hines6bcf27b2014-05-29 04:14:42 -07003078 case llvm::Triple::aarch64:
3079 case llvm::Triple::aarch64_be:
Stephen Hines6bcf27b2014-05-29 04:14:42 -07003080 AddAArch64TargetArgs(Args, CmdArgs);
Stephen Hines651f13c2014-04-23 16:59:28 -07003081 break;
3082
Eric Christophered734732010-03-02 02:41:08 +00003083 case llvm::Triple::mips:
3084 case llvm::Triple::mipsel:
Akira Hatanaka7ec02582011-09-21 02:13:07 +00003085 case llvm::Triple::mips64:
3086 case llvm::Triple::mips64el:
Eric Christophered734732010-03-02 02:41:08 +00003087 AddMIPSTargetArgs(Args, CmdArgs);
3088 break;
3089
Stephen Hines176edba2014-12-01 14:53:08 -08003090 case llvm::Triple::ppc:
3091 case llvm::Triple::ppc64:
3092 case llvm::Triple::ppc64le:
3093 AddPPCTargetArgs(Args, CmdArgs);
3094 break;
3095
Bruno Cardoso Lopes9284d212010-11-09 17:21:19 +00003096 case llvm::Triple::sparc:
Stephen Hines176edba2014-12-01 14:53:08 -08003097 case llvm::Triple::sparcv9:
Bruno Cardoso Lopes9284d212010-11-09 17:21:19 +00003098 AddSparcTargetArgs(Args, CmdArgs);
3099 break;
3100
Daniel Dunbar6acda162009-09-09 22:33:08 +00003101 case llvm::Triple::x86:
3102 case llvm::Triple::x86_64:
3103 AddX86TargetArgs(Args, CmdArgs);
3104 break;
Tony Linthicum96319392011-12-12 21:14:55 +00003105
3106 case llvm::Triple::hexagon:
3107 AddHexagonTargetArgs(Args, CmdArgs);
3108 break;
Daniel Dunbarbc85be82009-04-29 18:32:25 +00003109 }
3110
Hans Wennborgb3574792013-08-08 00:17:41 +00003111 // Add clang-cl arguments.
3112 if (getToolChain().getDriver().IsCLMode())
3113 AddClangCLArgs(Args, CmdArgs);
3114
Daniel Dunbarc176bc62010-08-11 23:07:47 +00003115 // Pass the linker version in use.
3116 if (Arg *A = Args.getLastArg(options::OPT_mlinker_version_EQ)) {
3117 CmdArgs.push_back("-target-linker-version");
Richard Smith1d489cf2012-11-01 04:30:05 +00003118 CmdArgs.push_back(A->getValue());
Daniel Dunbarc176bc62010-08-11 23:07:47 +00003119 }
3120
Eric Christopherd3e22df2013-04-03 01:58:53 +00003121 if (!shouldUseLeafFramePointer(Args, getToolChain().getTriple()))
Daniel Dunbar1ad66482010-07-01 01:31:45 +00003122 CmdArgs.push_back("-momit-leaf-frame-pointer");
3123
Daniel Dunbarb30575c2010-05-12 18:19:58 +00003124 // Explicitly error on some things we know we don't support and can't just
3125 // ignore.
3126 types::ID InputType = Inputs[0].getType();
Daniel Dunbare94db472010-09-24 19:39:37 +00003127 if (!Args.hasArg(options::OPT_fallow_unsupported)) {
3128 Arg *Unsupported;
Daniel Dunbare94db472010-09-24 19:39:37 +00003129 if (types::isCXX(InputType) &&
Bob Wilson905c45f2011-10-14 05:03:44 +00003130 getToolChain().getTriple().isOSDarwin() &&
Eli Bendersky8f4269a2013-07-24 22:20:49 +00003131 getToolChain().getArch() == llvm::Triple::x86) {
Bob Wilsona544aee2011-08-13 23:48:55 +00003132 if ((Unsupported = Args.getLastArg(options::OPT_fapple_kext)) ||
3133 (Unsupported = Args.getLastArg(options::OPT_mkernel)))
Chris Lattner5f9e2722011-07-23 10:55:15 +00003134 D.Diag(diag::err_drv_clang_unsupported_opt_cxx_darwin_i386)
Daniel Dunbare94db472010-09-24 19:39:37 +00003135 << Unsupported->getOption().getName();
3136 }
Daniel Dunbarb30575c2010-05-12 18:19:58 +00003137 }
3138
Daniel Dunbar1d460332009-03-18 10:01:51 +00003139 Args.AddAllArgs(CmdArgs, options::OPT_v);
Daniel Dunbarf7c16d92010-08-24 22:44:13 +00003140 Args.AddLastArg(CmdArgs, options::OPT_H);
Chad Rosier2b819102011-08-02 17:58:04 +00003141 if (D.CCPrintHeaders && !D.CCGenDiagnostics) {
Daniel Dunbar322c29f2011-02-02 21:11:35 +00003142 CmdArgs.push_back("-header-include-file");
3143 CmdArgs.push_back(D.CCPrintHeadersFilename ?
3144 D.CCPrintHeadersFilename : "-");
3145 }
Daniel Dunbar1d460332009-03-18 10:01:51 +00003146 Args.AddLastArg(CmdArgs, options::OPT_P);
Mike Stump1eb44332009-09-09 15:08:12 +00003147 Args.AddLastArg(CmdArgs, options::OPT_print_ivar_layout);
Daniel Dunbar1d460332009-03-18 10:01:51 +00003148
Chad Rosier2b819102011-08-02 17:58:04 +00003149 if (D.CCLogDiagnostics && !D.CCGenDiagnostics) {
Daniel Dunbarc8a22b02011-04-07 18:01:20 +00003150 CmdArgs.push_back("-diagnostic-log-file");
3151 CmdArgs.push_back(D.CCLogDiagnosticsFilename ?
3152 D.CCLogDiagnosticsFilename : "-");
3153 }
3154
Stephen Hines651f13c2014-04-23 16:59:28 -07003155 // Use the last option from "-g" group. "-gline-tables-only" and "-gdwarf-x"
3156 // are preserved, all other debug options are substituted with "-g".
Rafael Espindola18f36d92010-03-07 04:46:18 +00003157 Args.ClaimAllArgs(options::OPT_g_Group);
Alexey Samsonova9cd83b2012-05-29 08:10:34 +00003158 if (Arg *A = Args.getLastArg(options::OPT_g_Group)) {
Stephen Hines176edba2014-12-01 14:53:08 -08003159 if (A->getOption().matches(options::OPT_gline_tables_only) ||
3160 A->getOption().matches(options::OPT_g1)) {
Stephen Hines651f13c2014-04-23 16:59:28 -07003161 // FIXME: we should support specifying dwarf version with
3162 // -gline-tables-only.
Alexey Samsonova9cd83b2012-05-29 08:10:34 +00003163 CmdArgs.push_back("-gline-tables-only");
Stephen Hines176edba2014-12-01 14:53:08 -08003164 // Default is dwarf-2 for Darwin, OpenBSD, FreeBSD and Solaris.
Stephen Hinesef822542014-07-21 00:47:37 -07003165 const llvm::Triple &Triple = getToolChain().getTriple();
3166 if (Triple.isOSDarwin() || Triple.getOS() == llvm::Triple::OpenBSD ||
Stephen Hines176edba2014-12-01 14:53:08 -08003167 Triple.getOS() == llvm::Triple::FreeBSD ||
3168 Triple.getOS() == llvm::Triple::Solaris)
Stephen Hines651f13c2014-04-23 16:59:28 -07003169 CmdArgs.push_back("-gdwarf-2");
3170 } else if (A->getOption().matches(options::OPT_gdwarf_2))
Manman Renfc0f91c2013-06-19 01:46:49 +00003171 CmdArgs.push_back("-gdwarf-2");
3172 else if (A->getOption().matches(options::OPT_gdwarf_3))
3173 CmdArgs.push_back("-gdwarf-3");
3174 else if (A->getOption().matches(options::OPT_gdwarf_4))
3175 CmdArgs.push_back("-gdwarf-4");
Eric Christopherc706c8e2013-02-05 07:29:57 +00003176 else if (!A->getOption().matches(options::OPT_g0) &&
Manman Ren8ed38d82013-07-02 23:15:25 +00003177 !A->getOption().matches(options::OPT_ggdb0)) {
Stephen Hines176edba2014-12-01 14:53:08 -08003178 // Default is dwarf-2 for Darwin, OpenBSD, FreeBSD and Solaris.
Stephen Hinesef822542014-07-21 00:47:37 -07003179 const llvm::Triple &Triple = getToolChain().getTriple();
3180 if (Triple.isOSDarwin() || Triple.getOS() == llvm::Triple::OpenBSD ||
Stephen Hines176edba2014-12-01 14:53:08 -08003181 Triple.getOS() == llvm::Triple::FreeBSD ||
3182 Triple.getOS() == llvm::Triple::Solaris)
Manman Ren8ed38d82013-07-02 23:15:25 +00003183 CmdArgs.push_back("-gdwarf-2");
3184 else
3185 CmdArgs.push_back("-g");
3186 }
Alexey Samsonova9cd83b2012-05-29 08:10:34 +00003187 }
Daniel Dunbar1d460332009-03-18 10:01:51 +00003188
Alexey Samsonov7f326072012-06-21 08:22:39 +00003189 // We ignore flags -gstrict-dwarf and -grecord-gcc-switches for now.
3190 Args.ClaimAllArgs(options::OPT_g_flags_Group);
Stephen Hines176edba2014-12-01 14:53:08 -08003191 if (Args.hasFlag(options::OPT_gcolumn_info, options::OPT_gno_column_info,
3192 /*Default*/ true))
Eric Christopherda3301e2012-10-18 21:52:18 +00003193 CmdArgs.push_back("-dwarf-column-info");
Alexey Samsonov7f326072012-06-21 08:22:39 +00003194
Eric Christopher0f43a6d2013-09-13 22:37:55 +00003195 // FIXME: Move backend command line options to the module.
Eric Christopherc706c8e2013-02-05 07:29:57 +00003196 // -gsplit-dwarf should turn on -g and enable the backend dwarf
3197 // splitting and extraction.
Eric Christopherf870e122013-02-21 22:35:05 +00003198 // FIXME: Currently only works on Linux.
Cameron Esfahani57b1da12013-09-14 01:09:11 +00003199 if (getToolChain().getTriple().isOSLinux() &&
Eric Christopherf870e122013-02-21 22:35:05 +00003200 Args.hasArg(options::OPT_gsplit_dwarf)) {
Eric Christopherc706c8e2013-02-05 07:29:57 +00003201 CmdArgs.push_back("-g");
3202 CmdArgs.push_back("-backend-option");
3203 CmdArgs.push_back("-split-dwarf=Enable");
3204 }
3205
Eric Christopher0f43a6d2013-09-13 22:37:55 +00003206 // -ggnu-pubnames turns on gnu style pubnames in the backend.
3207 if (Args.hasArg(options::OPT_ggnu_pubnames)) {
3208 CmdArgs.push_back("-backend-option");
3209 CmdArgs.push_back("-generate-gnu-dwarf-pub-sections");
3210 }
Eric Christopher3e8ac1b2013-06-18 00:03:50 +00003211
Stephen Hines651f13c2014-04-23 16:59:28 -07003212 // -gdwarf-aranges turns on the emission of the aranges section in the
3213 // backend.
3214 if (Args.hasArg(options::OPT_gdwarf_aranges)) {
3215 CmdArgs.push_back("-backend-option");
3216 CmdArgs.push_back("-generate-arange-section");
3217 }
Eric Christopher3e8ac1b2013-06-18 00:03:50 +00003218
Stephen Hines651f13c2014-04-23 16:59:28 -07003219 if (Args.hasFlag(options::OPT_fdebug_types_section,
3220 options::OPT_fno_debug_types_section, false)) {
3221 CmdArgs.push_back("-backend-option");
3222 CmdArgs.push_back("-generate-type-units");
3223 }
3224
3225 if (Args.hasFlag(options::OPT_ffunction_sections,
3226 options::OPT_fno_function_sections, false)) {
3227 CmdArgs.push_back("-ffunction-sections");
3228 }
3229
3230 if (Args.hasFlag(options::OPT_fdata_sections,
3231 options::OPT_fno_data_sections, false)) {
3232 CmdArgs.push_back("-fdata-sections");
3233 }
Rafael Espindola9cf933a2010-05-06 21:06:04 +00003234
Chris Lattner7255a2d2010-06-22 00:03:40 +00003235 Args.AddAllArgs(CmdArgs, options::OPT_finstrument_functions);
3236
Stephen Hines651f13c2014-04-23 16:59:28 -07003237 if (Args.hasArg(options::OPT_fprofile_instr_generate) &&
3238 (Args.hasArg(options::OPT_fprofile_instr_use) ||
3239 Args.hasArg(options::OPT_fprofile_instr_use_EQ)))
3240 D.Diag(diag::err_drv_argument_not_allowed_with)
3241 << "-fprofile-instr-generate" << "-fprofile-instr-use";
3242
3243 Args.AddAllArgs(CmdArgs, options::OPT_fprofile_instr_generate);
3244
3245 if (Arg *A = Args.getLastArg(options::OPT_fprofile_instr_use_EQ))
3246 A->render(Args, CmdArgs);
3247 else if (Args.hasArg(options::OPT_fprofile_instr_use))
3248 CmdArgs.push_back("-fprofile-instr-use=pgo-data");
3249
Nick Lewyckye8ba8d72011-04-21 23:44:07 +00003250 if (Args.hasArg(options::OPT_ftest_coverage) ||
3251 Args.hasArg(options::OPT_coverage))
3252 CmdArgs.push_back("-femit-coverage-notes");
Stephen Hines176edba2014-12-01 14:53:08 -08003253 if (Args.hasFlag(options::OPT_fprofile_arcs, options::OPT_fno_profile_arcs,
3254 false) ||
Nick Lewyckye8ba8d72011-04-21 23:44:07 +00003255 Args.hasArg(options::OPT_coverage))
3256 CmdArgs.push_back("-femit-coverage-data");
3257
Stephen Hines176edba2014-12-01 14:53:08 -08003258 if (Args.hasArg(options::OPT_fcoverage_mapping) &&
3259 !Args.hasArg(options::OPT_fprofile_instr_generate))
3260 D.Diag(diag::err_drv_argument_only_allowed_with)
3261 << "-fcoverage-mapping" << "-fprofile-instr-generate";
3262
3263 if (Args.hasArg(options::OPT_fcoverage_mapping))
3264 CmdArgs.push_back("-fcoverage-mapping");
3265
Nick Lewycky5ea4f442011-05-04 20:46:58 +00003266 if (C.getArgs().hasArg(options::OPT_c) ||
3267 C.getArgs().hasArg(options::OPT_S)) {
3268 if (Output.isFilename()) {
Nick Lewycky3dc05412011-05-05 00:08:20 +00003269 CmdArgs.push_back("-coverage-file");
Stephen Hines176edba2014-12-01 14:53:08 -08003270 SmallString<128> CoverageFilename;
3271 if (Arg *FinalOutput = C.getArgs().getLastArg(options::OPT_o)) {
3272 CoverageFilename = FinalOutput->getValue();
3273 } else {
3274 CoverageFilename = llvm::sys::path::filename(Output.getBaseInput());
3275 }
Nick Lewycky0f815f12013-03-07 08:28:53 +00003276 if (llvm::sys::path::is_relative(CoverageFilename.str())) {
Rafael Espindolaa2148242013-08-10 01:40:10 +00003277 SmallString<128> Pwd;
3278 if (!llvm::sys::fs::current_path(Pwd)) {
3279 llvm::sys::path::append(Pwd, CoverageFilename.str());
3280 CoverageFilename.swap(Pwd);
Nick Lewycky0f815f12013-03-07 08:28:53 +00003281 }
3282 }
Eric Christopher025b3d42013-02-22 00:24:40 +00003283 CmdArgs.push_back(Args.MakeArgString(CoverageFilename));
Nick Lewycky5ea4f442011-05-04 20:46:58 +00003284 }
3285 }
3286
Daniel Dunbara268fc02011-10-11 18:20:10 +00003287 // Pass options for controlling the default header search paths.
3288 if (Args.hasArg(options::OPT_nostdinc)) {
3289 CmdArgs.push_back("-nostdsysteminc");
3290 CmdArgs.push_back("-nobuiltininc");
3291 } else {
Daniel Dunbar92d6d402011-10-11 18:20:16 +00003292 if (Args.hasArg(options::OPT_nostdlibinc))
3293 CmdArgs.push_back("-nostdsysteminc");
Daniel Dunbara268fc02011-10-11 18:20:10 +00003294 Args.AddLastArg(CmdArgs, options::OPT_nostdincxx);
3295 Args.AddLastArg(CmdArgs, options::OPT_nobuiltininc);
3296 }
Daniel Dunbar1d460332009-03-18 10:01:51 +00003297
Daniel Dunbar5f122322009-12-15 01:02:52 +00003298 // Pass the path to compiler resource files.
Daniel Dunbar5f122322009-12-15 01:02:52 +00003299 CmdArgs.push_back("-resource-dir");
Daniel Dunbar225c4172010-01-20 02:35:16 +00003300 CmdArgs.push_back(D.ResourceDir.c_str());
Daniel Dunbar2ac9fc22009-04-07 21:42:00 +00003301
Argyrios Kyrtzidis389db162010-11-03 22:45:23 +00003302 Args.AddLastArg(CmdArgs, options::OPT_working_directory);
3303
Ted Kremenek30660a82012-03-06 20:06:33 +00003304 bool ARCMTEnabled = false;
Argyrios Kyrtzidisdce3ce32013-09-17 19:14:29 +00003305 if (!Args.hasArg(options::OPT_fno_objc_arc, options::OPT_fobjc_arc)) {
Argyrios Kyrtzidis72ac1202011-07-07 04:00:39 +00003306 if (const Arg *A = Args.getLastArg(options::OPT_ccc_arcmt_check,
Argyrios Kyrtzidis69325d52011-07-09 20:00:58 +00003307 options::OPT_ccc_arcmt_modify,
3308 options::OPT_ccc_arcmt_migrate)) {
Ted Kremenek30660a82012-03-06 20:06:33 +00003309 ARCMTEnabled = true;
John McCall8f0e8d22011-06-15 23:25:17 +00003310 switch (A->getOption().getID()) {
3311 default:
3312 llvm_unreachable("missed a case");
Argyrios Kyrtzidis72ac1202011-07-07 04:00:39 +00003313 case options::OPT_ccc_arcmt_check:
John McCall8f0e8d22011-06-15 23:25:17 +00003314 CmdArgs.push_back("-arcmt-check");
3315 break;
Argyrios Kyrtzidis72ac1202011-07-07 04:00:39 +00003316 case options::OPT_ccc_arcmt_modify:
John McCall8f0e8d22011-06-15 23:25:17 +00003317 CmdArgs.push_back("-arcmt-modify");
3318 break;
Argyrios Kyrtzidis69325d52011-07-09 20:00:58 +00003319 case options::OPT_ccc_arcmt_migrate:
3320 CmdArgs.push_back("-arcmt-migrate");
Ted Kremenek30660a82012-03-06 20:06:33 +00003321 CmdArgs.push_back("-mt-migrate-directory");
Richard Smith1d489cf2012-11-01 04:30:05 +00003322 CmdArgs.push_back(A->getValue());
Argyrios Kyrtzidis7ee20492011-07-19 17:20:03 +00003323
3324 Args.AddLastArg(CmdArgs, options::OPT_arcmt_migrate_report_output);
3325 Args.AddLastArg(CmdArgs, options::OPT_arcmt_migrate_emit_arc_errors);
Argyrios Kyrtzidis69325d52011-07-09 20:00:58 +00003326 break;
John McCall8f0e8d22011-06-15 23:25:17 +00003327 }
3328 }
Argyrios Kyrtzidisf75ece42013-06-24 19:01:18 +00003329 } else {
3330 Args.ClaimAllArgs(options::OPT_ccc_arcmt_check);
3331 Args.ClaimAllArgs(options::OPT_ccc_arcmt_modify);
3332 Args.ClaimAllArgs(options::OPT_ccc_arcmt_migrate);
John McCall8f0e8d22011-06-15 23:25:17 +00003333 }
Eric Christopher88b7cf02011-08-19 00:30:14 +00003334
Ted Kremenek30660a82012-03-06 20:06:33 +00003335 if (const Arg *A = Args.getLastArg(options::OPT_ccc_objcmt_migrate)) {
3336 if (ARCMTEnabled) {
3337 D.Diag(diag::err_drv_argument_not_allowed_with)
3338 << A->getAsString(Args) << "-ccc-arcmt-migrate";
3339 }
3340 CmdArgs.push_back("-mt-migrate-directory");
Richard Smith1d489cf2012-11-01 04:30:05 +00003341 CmdArgs.push_back(A->getValue());
Ted Kremenek30660a82012-03-06 20:06:33 +00003342
3343 if (!Args.hasArg(options::OPT_objcmt_migrate_literals,
Fariborz Jahaniand4129992013-07-09 16:59:14 +00003344 options::OPT_objcmt_migrate_subscripting,
3345 options::OPT_objcmt_migrate_property)) {
Ted Kremenek30660a82012-03-06 20:06:33 +00003346 // None specified, means enable them all.
3347 CmdArgs.push_back("-objcmt-migrate-literals");
3348 CmdArgs.push_back("-objcmt-migrate-subscripting");
Fariborz Jahaniand4129992013-07-09 16:59:14 +00003349 CmdArgs.push_back("-objcmt-migrate-property");
Ted Kremenek30660a82012-03-06 20:06:33 +00003350 } else {
3351 Args.AddLastArg(CmdArgs, options::OPT_objcmt_migrate_literals);
3352 Args.AddLastArg(CmdArgs, options::OPT_objcmt_migrate_subscripting);
Fariborz Jahaniand4129992013-07-09 16:59:14 +00003353 Args.AddLastArg(CmdArgs, options::OPT_objcmt_migrate_property);
Ted Kremenek30660a82012-03-06 20:06:33 +00003354 }
Argyrios Kyrtzidis17c384c2013-11-13 23:38:20 +00003355 } else {
3356 Args.AddLastArg(CmdArgs, options::OPT_objcmt_migrate_literals);
3357 Args.AddLastArg(CmdArgs, options::OPT_objcmt_migrate_subscripting);
3358 Args.AddLastArg(CmdArgs, options::OPT_objcmt_migrate_property);
3359 Args.AddLastArg(CmdArgs, options::OPT_objcmt_migrate_all);
3360 Args.AddLastArg(CmdArgs, options::OPT_objcmt_migrate_readonly_property);
3361 Args.AddLastArg(CmdArgs, options::OPT_objcmt_migrate_readwrite_property);
3362 Args.AddLastArg(CmdArgs, options::OPT_objcmt_migrate_annotation);
3363 Args.AddLastArg(CmdArgs, options::OPT_objcmt_migrate_instancetype);
3364 Args.AddLastArg(CmdArgs, options::OPT_objcmt_migrate_nsmacros);
3365 Args.AddLastArg(CmdArgs, options::OPT_objcmt_migrate_protocol_conformance);
3366 Args.AddLastArg(CmdArgs, options::OPT_objcmt_atomic_property);
3367 Args.AddLastArg(CmdArgs, options::OPT_objcmt_returns_innerpointer_property);
3368 Args.AddLastArg(CmdArgs, options::OPT_objcmt_ns_nonatomic_iosonly);
Stephen Hines651f13c2014-04-23 16:59:28 -07003369 Args.AddLastArg(CmdArgs, options::OPT_objcmt_migrate_designated_init);
3370 Args.AddLastArg(CmdArgs, options::OPT_objcmt_whitelist_dir_path);
Ted Kremenek30660a82012-03-06 20:06:33 +00003371 }
3372
Daniel Dunbarc21c4852009-04-08 23:54:23 +00003373 // Add preprocessing options like -I, -D, etc. if we are using the
3374 // preprocessor.
3375 //
3376 // FIXME: Support -fpreprocessed
Daniel Dunbarc21c4852009-04-08 23:54:23 +00003377 if (types::getPreprocessedType(InputType) != types::TY_INVALID)
Chad Rosier9d718632013-01-24 19:14:47 +00003378 AddPreprocessingOptions(C, JA, D, Args, CmdArgs, Output, Inputs);
Daniel Dunbar1d460332009-03-18 10:01:51 +00003379
Rafael Espindola19d9d2e2011-07-21 23:40:37 +00003380 // Don't warn about "clang -c -DPIC -fPIC test.i" because libtool.m4 assumes
3381 // that "The compiler can only warn and ignore the option if not recognized".
3382 // When building with ccache, it will pass -D options to clang even on
3383 // preprocessed inputs and configure concludes that -fPIC is not supported.
3384 Args.ClaimAllArgs(options::OPT_D);
3385
Alp Tokere22017e2013-11-15 20:40:58 +00003386 // Manually translate -O4 to -O3; let clang reject others.
Rafael Espindola55ee1eb2013-08-27 16:58:15 +00003387 if (Arg *A = Args.getLastArg(options::OPT_O_Group)) {
3388 if (A->getOption().matches(options::OPT_O4)) {
3389 CmdArgs.push_back("-O3");
3390 D.Diag(diag::warn_O4_is_O3);
3391 } else {
3392 A->render(Args, CmdArgs);
3393 }
3394 }
Daniel Dunbar1d460332009-03-18 10:01:51 +00003395
Stephen Hines176edba2014-12-01 14:53:08 -08003396 // Warn about ignored options to clang.
3397 for (arg_iterator it = Args.filtered_begin(
3398 options::OPT_clang_ignored_gcc_optimization_f_Group),
3399 ie = Args.filtered_end(); it != ie; ++it) {
3400 D.Diag(diag::warn_ignored_gcc_optimization) << (*it)->getAsString(Args);
3401 }
3402
Chad Rosierb2c08872012-12-12 20:06:31 +00003403 // Don't warn about unused -flto. This can happen when we're preprocessing or
3404 // precompiling.
3405 Args.ClaimAllArgs(options::OPT_flto);
3406
Stephen Hines176edba2014-12-01 14:53:08 -08003407 Args.AddAllArgs(CmdArgs, options::OPT_R_Group);
Daniel Dunbar6e8371e2009-10-29 02:24:45 +00003408 Args.AddAllArgs(CmdArgs, options::OPT_W_Group);
Ted Kremeneke8cf7d12012-07-07 05:53:30 +00003409 if (Args.hasFlag(options::OPT_pedantic, options::OPT_no_pedantic, false))
3410 CmdArgs.push_back("-pedantic");
Daniel Dunbar6e8371e2009-10-29 02:24:45 +00003411 Args.AddLastArg(CmdArgs, options::OPT_pedantic_errors);
Daniel Dunbar1d460332009-03-18 10:01:51 +00003412 Args.AddLastArg(CmdArgs, options::OPT_w);
Daniel Dunbard573d262009-04-07 22:13:21 +00003413
3414 // Handle -{std, ansi, trigraphs} -- take the last of -{std, ansi}
Hans Wennborgadbb4552013-07-31 16:57:56 +00003415 // (-ansi is equivalent to -std=c89 or -std=c++98).
Daniel Dunbard573d262009-04-07 22:13:21 +00003416 //
3417 // If a std is supplied, only add -trigraphs if it follows the
3418 // option.
3419 if (Arg *Std = Args.getLastArg(options::OPT_std_EQ, options::OPT_ansi)) {
3420 if (Std->getOption().matches(options::OPT_ansi))
Nuno Lopes528365d2009-10-16 14:28:06 +00003421 if (types::isCXX(InputType))
Daniel Dunbar294691e2009-11-04 06:24:38 +00003422 CmdArgs.push_back("-std=c++98");
Nuno Lopes528365d2009-10-16 14:28:06 +00003423 else
Daniel Dunbar294691e2009-11-04 06:24:38 +00003424 CmdArgs.push_back("-std=c89");
Daniel Dunbard573d262009-04-07 22:13:21 +00003425 else
3426 Std->render(Args, CmdArgs);
3427
Daniel Dunbar0e100312010-06-14 21:23:08 +00003428 if (Arg *A = Args.getLastArg(options::OPT_std_EQ, options::OPT_ansi,
3429 options::OPT_trigraphs))
3430 if (A != Std)
Daniel Dunbard573d262009-04-07 22:13:21 +00003431 A->render(Args, CmdArgs);
Daniel Dunbara3ff2022009-04-26 01:10:38 +00003432 } else {
3433 // Honor -std-default.
Daniel Dunbar4a5290e2010-01-29 21:03:02 +00003434 //
3435 // FIXME: Clang doesn't correctly handle -std= when the input language
3436 // doesn't match. For the time being just ignore this for C++ inputs;
3437 // eventually we want to do all the standard defaulting here instead of
3438 // splitting it between the driver and clang -cc1.
3439 if (!types::isCXX(InputType))
Nico Weber50f88b92012-08-30 02:08:31 +00003440 Args.AddAllArgsTranslated(CmdArgs, options::OPT_std_default_EQ,
3441 "-std=", /*Joined=*/true);
Stephen Hines651f13c2014-04-23 16:59:28 -07003442 else if (IsWindowsMSVC)
Nico Weber50f88b92012-08-30 02:08:31 +00003443 CmdArgs.push_back("-std=c++11");
3444
Daniel Dunbard573d262009-04-07 22:13:21 +00003445 Args.AddLastArg(CmdArgs, options::OPT_trigraphs);
Daniel Dunbara3ff2022009-04-26 01:10:38 +00003446 }
Daniel Dunbarc21c4852009-04-08 23:54:23 +00003447
Richard Smithe9813b32013-09-04 22:50:31 +00003448 // GCC's behavior for -Wwrite-strings is a bit strange:
3449 // * In C, this "warning flag" changes the types of string literals from
3450 // 'char[N]' to 'const char[N]', and thus triggers an unrelated warning
3451 // for the discarded qualifier.
3452 // * In C++, this is just a normal warning flag.
3453 //
3454 // Implementing this warning correctly in C is hard, so we follow GCC's
3455 // behavior for now. FIXME: Directly diagnose uses of a string literal as
3456 // a non-const char* in C, rather than using this crude hack.
3457 if (!types::isCXX(InputType)) {
Stephen Hines651f13c2014-04-23 16:59:28 -07003458 // FIXME: This should behave just like a warning flag, and thus should also
3459 // respect -Weverything, -Wno-everything, -Werror=write-strings, and so on.
3460 Arg *WriteStrings =
3461 Args.getLastArg(options::OPT_Wwrite_strings,
3462 options::OPT_Wno_write_strings, options::OPT_w);
3463 if (WriteStrings &&
3464 WriteStrings->getOption().matches(options::OPT_Wwrite_strings))
Richard Smithe9813b32013-09-04 22:50:31 +00003465 CmdArgs.push_back("-fconst-strings");
Chandler Carruth50465d12011-04-23 06:30:43 +00003466 }
3467
Chandler Carruth1cfe3c32011-04-23 09:27:53 +00003468 // GCC provides a macro definition '__DEPRECATED' when -Wdeprecated is active
Chandler Carruthf8c247d2011-04-23 19:48:40 +00003469 // during C++ compilation, which it is by default. GCC keeps this define even
3470 // in the presence of '-w', match this behavior bug-for-bug.
3471 if (types::isCXX(InputType) &&
3472 Args.hasFlag(options::OPT_Wdeprecated, options::OPT_Wno_deprecated,
3473 true)) {
3474 CmdArgs.push_back("-fdeprecated-macro");
Chandler Carruth1cfe3c32011-04-23 09:27:53 +00003475 }
3476
Chandler Carruthc304ba32010-05-22 02:21:53 +00003477 // Translate GCC's misnamer '-fasm' arguments to '-fgnu-keywords'.
3478 if (Arg *Asm = Args.getLastArg(options::OPT_fasm, options::OPT_fno_asm)) {
3479 if (Asm->getOption().matches(options::OPT_fasm))
3480 CmdArgs.push_back("-fgnu-keywords");
3481 else
3482 CmdArgs.push_back("-fno-gnu-keywords");
3483 }
3484
Nick Lewyckyea523d72011-10-17 23:05:52 +00003485 if (ShouldDisableDwarfDirectory(Args, getToolChain()))
3486 CmdArgs.push_back("-fno-dwarf-directory-asm");
3487
Daniel Dunbarf4910132013-04-16 18:21:19 +00003488 if (ShouldDisableAutolink(Args, getToolChain()))
3489 CmdArgs.push_back("-fno-autolink");
3490
Chandler Carruthd566df62012-12-17 21:40:04 +00003491 // Add in -fdebug-compilation-dir if necessary.
3492 addDebugCompDirArg(Args, CmdArgs);
Nick Lewycky7c4fd912011-10-21 02:32:14 +00003493
Richard Smithc18c4232011-11-21 19:36:32 +00003494 if (Arg *A = Args.getLastArg(options::OPT_ftemplate_depth_,
3495 options::OPT_ftemplate_depth_EQ)) {
Daniel Dunbar1d460332009-03-18 10:01:51 +00003496 CmdArgs.push_back("-ftemplate-depth");
Richard Smith1d489cf2012-11-01 04:30:05 +00003497 CmdArgs.push_back(A->getValue());
Daniel Dunbar1d460332009-03-18 10:01:51 +00003498 }
3499
Richard Smith195dd7c2013-11-06 19:31:51 +00003500 if (Arg *A = Args.getLastArg(options::OPT_foperator_arrow_depth_EQ)) {
3501 CmdArgs.push_back("-foperator-arrow-depth");
3502 CmdArgs.push_back(A->getValue());
3503 }
3504
Richard Smithc18c4232011-11-21 19:36:32 +00003505 if (Arg *A = Args.getLastArg(options::OPT_fconstexpr_depth_EQ)) {
3506 CmdArgs.push_back("-fconstexpr-depth");
Richard Smith1d489cf2012-11-01 04:30:05 +00003507 CmdArgs.push_back(A->getValue());
Richard Smithc18c4232011-11-21 19:36:32 +00003508 }
3509
Richard Smithe7565632013-05-08 02:12:03 +00003510 if (Arg *A = Args.getLastArg(options::OPT_fconstexpr_steps_EQ)) {
3511 CmdArgs.push_back("-fconstexpr-steps");
3512 CmdArgs.push_back(A->getValue());
3513 }
3514
Richard Smith9e738cc2013-02-22 01:59:51 +00003515 if (Arg *A = Args.getLastArg(options::OPT_fbracket_depth_EQ)) {
3516 CmdArgs.push_back("-fbracket-depth");
3517 CmdArgs.push_back(A->getValue());
3518 }
3519
Argyrios Kyrtzidis1380a142010-11-18 00:20:36 +00003520 if (Arg *A = Args.getLastArg(options::OPT_Wlarge_by_value_copy_EQ,
3521 options::OPT_Wlarge_by_value_copy_def)) {
Jean-Daniel Dupas2e4fd6d2012-05-04 08:08:37 +00003522 if (A->getNumValues()) {
Richard Smith1d489cf2012-11-01 04:30:05 +00003523 StringRef bytes = A->getValue();
Jean-Daniel Dupas2e4fd6d2012-05-04 08:08:37 +00003524 CmdArgs.push_back(Args.MakeArgString("-Wlarge-by-value-copy=" + bytes));
3525 } else
3526 CmdArgs.push_back("-Wlarge-by-value-copy=64"); // default value
Argyrios Kyrtzidis3532fdd2010-11-17 23:11:54 +00003527 }
3528
Nuno Lopesb3198a82012-05-08 22:10:46 +00003529
Michael J. Spencerc6357102012-10-22 22:13:48 +00003530 if (Args.hasArg(options::OPT_relocatable_pch))
Daniel Dunbar66861e02009-11-20 22:21:36 +00003531 CmdArgs.push_back("-relocatable-pch");
Mike Stump1eb44332009-09-09 15:08:12 +00003532
Daniel Dunbar294691e2009-11-04 06:24:38 +00003533 if (Arg *A = Args.getLastArg(options::OPT_fconstant_string_class_EQ)) {
3534 CmdArgs.push_back("-fconstant-string-class");
Richard Smith1d489cf2012-11-01 04:30:05 +00003535 CmdArgs.push_back(A->getValue());
Daniel Dunbar294691e2009-11-04 06:24:38 +00003536 }
David Chisnall8a5a9aa2009-08-31 16:41:57 +00003537
Chris Lattner124fca52010-01-09 21:54:33 +00003538 if (Arg *A = Args.getLastArg(options::OPT_ftabstop_EQ)) {
3539 CmdArgs.push_back("-ftabstop");
Richard Smith1d489cf2012-11-01 04:30:05 +00003540 CmdArgs.push_back(A->getValue());
Chris Lattner124fca52010-01-09 21:54:33 +00003541 }
3542
Chris Lattner0f0c9632010-04-07 20:49:23 +00003543 CmdArgs.push_back("-ferror-limit");
3544 if (Arg *A = Args.getLastArg(options::OPT_ferror_limit_EQ))
Richard Smith1d489cf2012-11-01 04:30:05 +00003545 CmdArgs.push_back(A->getValue());
Chris Lattner0f0c9632010-04-07 20:49:23 +00003546 else
3547 CmdArgs.push_back("19");
Douglas Gregor575cf372010-04-20 07:18:24 +00003548
Chandler Carruthc40f73c2010-05-06 04:55:18 +00003549 if (Arg *A = Args.getLastArg(options::OPT_fmacro_backtrace_limit_EQ)) {
3550 CmdArgs.push_back("-fmacro-backtrace-limit");
Richard Smith1d489cf2012-11-01 04:30:05 +00003551 CmdArgs.push_back(A->getValue());
Chandler Carruthc40f73c2010-05-06 04:55:18 +00003552 }
3553
3554 if (Arg *A = Args.getLastArg(options::OPT_ftemplate_backtrace_limit_EQ)) {
3555 CmdArgs.push_back("-ftemplate-backtrace-limit");
Richard Smith1d489cf2012-11-01 04:30:05 +00003556 CmdArgs.push_back(A->getValue());
Chandler Carruthc40f73c2010-05-06 04:55:18 +00003557 }
3558
Richard Smith08d6e032011-12-16 19:06:07 +00003559 if (Arg *A = Args.getLastArg(options::OPT_fconstexpr_backtrace_limit_EQ)) {
3560 CmdArgs.push_back("-fconstexpr-backtrace-limit");
Richard Smith1d489cf2012-11-01 04:30:05 +00003561 CmdArgs.push_back(A->getValue());
Richard Smith08d6e032011-12-16 19:06:07 +00003562 }
3563
Daniel Dunbar55efe142009-11-04 06:24:47 +00003564 // Pass -fmessage-length=.
Daniel Dunbara28690e2009-11-30 08:40:54 +00003565 CmdArgs.push_back("-fmessage-length");
Daniel Dunbar55efe142009-11-04 06:24:47 +00003566 if (Arg *A = Args.getLastArg(options::OPT_fmessage_length_EQ)) {
Richard Smith1d489cf2012-11-01 04:30:05 +00003567 CmdArgs.push_back(A->getValue());
Daniel Dunbar55efe142009-11-04 06:24:47 +00003568 } else {
3569 // If -fmessage-length=N was not specified, determine whether this is a
3570 // terminal and, if so, implicitly define -fmessage-length appropriately.
3571 unsigned N = llvm::sys::Process::StandardErrColumns();
Chris Lattner5f9e2722011-07-23 10:55:15 +00003572 CmdArgs.push_back(Args.MakeArgString(Twine(N)));
Daniel Dunbar55efe142009-11-04 06:24:47 +00003573 }
3574
John McCalla880b192013-02-19 01:57:35 +00003575 // -fvisibility= and -fvisibility-ms-compat are of a piece.
3576 if (const Arg *A = Args.getLastArg(options::OPT_fvisibility_EQ,
3577 options::OPT_fvisibility_ms_compat)) {
3578 if (A->getOption().matches(options::OPT_fvisibility_EQ)) {
3579 CmdArgs.push_back("-fvisibility");
3580 CmdArgs.push_back(A->getValue());
3581 } else {
3582 assert(A->getOption().matches(options::OPT_fvisibility_ms_compat));
3583 CmdArgs.push_back("-fvisibility");
3584 CmdArgs.push_back("hidden");
3585 CmdArgs.push_back("-ftype-visibility");
3586 CmdArgs.push_back("default");
3587 }
Daniel Dunbarba8d8612009-12-03 18:42:11 +00003588 }
3589
Douglas Gregor7cf84d62010-06-15 17:05:35 +00003590 Args.AddLastArg(CmdArgs, options::OPT_fvisibility_inlines_hidden);
Michael J. Spencer20249a12010-10-21 03:16:25 +00003591
Hans Wennborgde981f32012-06-28 08:01:44 +00003592 Args.AddLastArg(CmdArgs, options::OPT_ftlsmodel_EQ);
3593
Daniel Dunbar0a80ba72010-03-20 04:52:14 +00003594 // -fhosted is default.
Chad Rosierafc4baa2012-03-26 22:04:46 +00003595 if (Args.hasFlag(options::OPT_ffreestanding, options::OPT_fhosted, false) ||
3596 KernelOrKext)
Daniel Dunbar0a80ba72010-03-20 04:52:14 +00003597 CmdArgs.push_back("-ffreestanding");
3598
Daniel Dunbarba8d8612009-12-03 18:42:11 +00003599 // Forward -f (flag) options which we can pass directly.
Daniel Dunbar3aaf0822009-04-07 21:51:40 +00003600 Args.AddLastArg(CmdArgs, options::OPT_femit_all_decls);
Daniel Dunbar3aaf0822009-04-07 21:51:40 +00003601 Args.AddLastArg(CmdArgs, options::OPT_fheinous_gnu_extensions);
Stephen Hines651f13c2014-04-23 16:59:28 -07003602 Args.AddLastArg(CmdArgs, options::OPT_fstandalone_debug);
3603 Args.AddLastArg(CmdArgs, options::OPT_fno_standalone_debug);
Eric Christophere88c4512011-10-25 07:13:06 +00003604 Args.AddLastArg(CmdArgs, options::OPT_fno_operator_names);
Bill Schmidt9e0b6472013-07-03 15:36:02 +00003605 // AltiVec language extensions aren't relevant for assembling.
3606 if (!isa<PreprocessJobAction>(JA) ||
3607 Output.getType() != types::TY_PP_Asm)
3608 Args.AddLastArg(CmdArgs, options::OPT_faltivec);
Richard Trieu246b6aa2012-06-26 18:18:47 +00003609 Args.AddLastArg(CmdArgs, options::OPT_fdiagnostics_show_template_tree);
3610 Args.AddLastArg(CmdArgs, options::OPT_fno_elide_type);
Chad Rosier4574c3d2012-03-13 23:45:51 +00003611
Peter Collingbournec6911a22013-11-01 18:16:25 +00003612 const SanitizerArgs &Sanitize = getToolChain().getSanitizerArgs();
3613 Sanitize.addArgs(Args, CmdArgs);
Richard Smithc4dabad2012-11-05 22:04:41 +00003614
Eric Christopher98654c92013-02-19 06:16:53 +00003615 // Report an error for -faltivec on anything other than PowerPC.
Chad Rosier4574c3d2012-03-13 23:45:51 +00003616 if (const Arg *A = Args.getLastArg(options::OPT_faltivec))
Eli Bendersky8f4269a2013-07-24 22:20:49 +00003617 if (!(getToolChain().getArch() == llvm::Triple::ppc ||
Bill Schmidtea7fb0c2013-07-26 01:36:11 +00003618 getToolChain().getArch() == llvm::Triple::ppc64 ||
3619 getToolChain().getArch() == llvm::Triple::ppc64le))
Chad Rosier4574c3d2012-03-13 23:45:51 +00003620 D.Diag(diag::err_drv_argument_only_allowed_with)
Bill Schmidtea7fb0c2013-07-26 01:36:11 +00003621 << A->getAsString(Args) << "ppc/ppc64/ppc64le";
Chad Rosier4574c3d2012-03-13 23:45:51 +00003622
Daniel Dunbarbbe8e3e2011-03-01 18:49:30 +00003623 if (getToolChain().SupportsProfiling())
3624 Args.AddLastArg(CmdArgs, options::OPT_pg);
Daniel Dunbar8c6fa842010-03-16 16:57:46 +00003625
3626 // -flax-vector-conversions is default.
3627 if (!Args.hasFlag(options::OPT_flax_vector_conversions,
3628 options::OPT_fno_lax_vector_conversions))
3629 CmdArgs.push_back("-fno-lax-vector-conversions");
3630
Fariborz Jahanianb466d012011-01-07 01:05:02 +00003631 if (Args.getLastArg(options::OPT_fapple_kext))
3632 CmdArgs.push_back("-fapple-kext");
3633
Fariborz Jahanian34e65772009-05-22 20:17:16 +00003634 Args.AddLastArg(CmdArgs, options::OPT_fobjc_sender_dependent_dispatch);
Chris Lattner182e0922009-04-21 05:34:31 +00003635 Args.AddLastArg(CmdArgs, options::OPT_fdiagnostics_print_source_range_info);
Douglas Gregor4786c152010-08-19 20:24:43 +00003636 Args.AddLastArg(CmdArgs, options::OPT_fdiagnostics_parseable_fixits);
Daniel Dunbar3aaf0822009-04-07 21:51:40 +00003637 Args.AddLastArg(CmdArgs, options::OPT_ftime_report);
3638 Args.AddLastArg(CmdArgs, options::OPT_ftrapv);
David Chisnall7f18e672010-09-17 18:29:54 +00003639
3640 if (Arg *A = Args.getLastArg(options::OPT_ftrapv_handler_EQ)) {
3641 CmdArgs.push_back("-ftrapv-handler");
Richard Smith1d489cf2012-11-01 04:30:05 +00003642 CmdArgs.push_back(A->getValue());
David Chisnall7f18e672010-09-17 18:29:54 +00003643 }
3644
Bob Wilson71fd6cc2012-02-03 06:27:22 +00003645 Args.AddLastArg(CmdArgs, options::OPT_ftrap_function_EQ);
Evan Cheng49af1f32011-04-08 21:37:45 +00003646
Chandler Carruth5adb5a82011-03-27 00:04:55 +00003647 // -fno-strict-overflow implies -fwrapv if it isn't disabled, but
3648 // -fstrict-overflow won't turn off an explicitly enabled -fwrapv.
3649 if (Arg *A = Args.getLastArg(options::OPT_fwrapv,
3650 options::OPT_fno_wrapv)) {
3651 if (A->getOption().matches(options::OPT_fwrapv))
3652 CmdArgs.push_back("-fwrapv");
3653 } else if (Arg *A = Args.getLastArg(options::OPT_fstrict_overflow,
3654 options::OPT_fno_strict_overflow)) {
3655 if (A->getOption().matches(options::OPT_fno_strict_overflow))
3656 CmdArgs.push_back("-fwrapv");
3657 }
Hal Finkelce5b5f12013-11-17 16:03:29 +00003658
3659 if (Arg *A = Args.getLastArg(options::OPT_freroll_loops,
3660 options::OPT_fno_reroll_loops))
3661 if (A->getOption().matches(options::OPT_freroll_loops))
3662 CmdArgs.push_back("-freroll-loops");
3663
Daniel Dunbar3aaf0822009-04-07 21:51:40 +00003664 Args.AddLastArg(CmdArgs, options::OPT_fwritable_strings);
Chandler Carruthb26404a2013-08-08 08:34:35 +00003665 Args.AddLastArg(CmdArgs, options::OPT_funroll_loops,
3666 options::OPT_fno_unroll_loops);
Daniel Dunbar1d460332009-03-18 10:01:51 +00003667
Daniel Dunbar5345c392009-09-03 04:54:28 +00003668 Args.AddLastArg(CmdArgs, options::OPT_pthread);
3669
Mahesha Sf3b52312012-10-27 07:47:56 +00003670
Daniel Dunbar9e5cc6b2009-11-17 08:07:36 +00003671 // -stack-protector=0 is default.
3672 unsigned StackProtectorLevel = 0;
Bill Wendling45483f72009-06-28 07:36:13 +00003673 if (Arg *A = Args.getLastArg(options::OPT_fno_stack_protector,
3674 options::OPT_fstack_protector_all,
Stephen Hines651f13c2014-04-23 16:59:28 -07003675 options::OPT_fstack_protector_strong,
Bill Wendling45483f72009-06-28 07:36:13 +00003676 options::OPT_fstack_protector)) {
Stephen Hines6bcf27b2014-05-29 04:14:42 -07003677 if (A->getOption().matches(options::OPT_fstack_protector)) {
3678 StackProtectorLevel = std::max<unsigned>(LangOptions::SSPOn,
3679 getToolChain().GetDefaultStackProtectorLevel(KernelOrKext));
3680 } else if (A->getOption().matches(options::OPT_fstack_protector_strong))
Stephen Hines651f13c2014-04-23 16:59:28 -07003681 StackProtectorLevel = LangOptions::SSPStrong;
Daniel Dunbar9e5cc6b2009-11-17 08:07:36 +00003682 else if (A->getOption().matches(options::OPT_fstack_protector_all))
Stephen Hines651f13c2014-04-23 16:59:28 -07003683 StackProtectorLevel = LangOptions::SSPReq;
Nico Weber2fef1112011-08-23 07:38:27 +00003684 } else {
3685 StackProtectorLevel =
3686 getToolChain().GetDefaultStackProtectorLevel(KernelOrKext);
3687 }
Daniel Dunbar9e5cc6b2009-11-17 08:07:36 +00003688 if (StackProtectorLevel) {
3689 CmdArgs.push_back("-stack-protector");
Chris Lattner5f9e2722011-07-23 10:55:15 +00003690 CmdArgs.push_back(Args.MakeArgString(Twine(StackProtectorLevel)));
Joerg Sonnenberger53b43a72012-09-12 13:51:14 +00003691 }
Chad Rosiera7afeb02012-08-21 16:16:06 +00003692
Joerg Sonnenberger53b43a72012-09-12 13:51:14 +00003693 // --param ssp-buffer-size=
3694 for (arg_iterator it = Args.filtered_begin(options::OPT__param),
3695 ie = Args.filtered_end(); it != ie; ++it) {
Richard Smith1d489cf2012-11-01 04:30:05 +00003696 StringRef Str((*it)->getValue());
Joerg Sonnenberger53b43a72012-09-12 13:51:14 +00003697 if (Str.startswith("ssp-buffer-size=")) {
3698 if (StackProtectorLevel) {
Chad Rosiera7afeb02012-08-21 16:16:06 +00003699 CmdArgs.push_back("-stack-protector-buffer-size");
3700 // FIXME: Verify the argument is a valid integer.
3701 CmdArgs.push_back(Args.MakeArgString(Str.drop_front(16)));
Chad Rosiera7afeb02012-08-21 16:16:06 +00003702 }
Joerg Sonnenberger53b43a72012-09-12 13:51:14 +00003703 (*it)->claim();
Chad Rosiera7afeb02012-08-21 16:16:06 +00003704 }
Bill Wendling45483f72009-06-28 07:36:13 +00003705 }
3706
Nick Lewycky4e785c92011-12-06 03:33:03 +00003707 // Translate -mstackrealign
3708 if (Args.hasFlag(options::OPT_mstackrealign, options::OPT_mno_stackrealign,
3709 false)) {
3710 CmdArgs.push_back("-backend-option");
3711 CmdArgs.push_back("-force-align-stack");
3712 }
3713 if (!Args.hasFlag(options::OPT_mno_stackrealign, options::OPT_mstackrealign,
3714 false)) {
3715 CmdArgs.push_back(Args.MakeArgString("-mstackrealign"));
3716 }
3717
Joerg Sonnenbergere9d11db2011-12-05 23:05:23 +00003718 if (Args.hasArg(options::OPT_mstack_alignment)) {
3719 StringRef alignment = Args.getLastArgValue(options::OPT_mstack_alignment);
3720 CmdArgs.push_back(Args.MakeArgString("-mstack-alignment=" + alignment));
Eric Christopher1a584022011-05-02 21:18:22 +00003721 }
Stephen Hines176edba2014-12-01 14:53:08 -08003722
3723 if (getToolChain().getTriple().getArch() == llvm::Triple::aarch64 ||
3724 getToolChain().getTriple().getArch() == llvm::Triple::aarch64_be)
3725 CmdArgs.push_back("-fallow-half-arguments-and-returns");
Eric Christopher88b7cf02011-08-19 00:30:14 +00003726
Weiming Zhao7792fde2013-11-13 18:31:23 +00003727 if (Arg *A = Args.getLastArg(options::OPT_mrestrict_it,
3728 options::OPT_mno_restrict_it)) {
3729 if (A->getOption().matches(options::OPT_mrestrict_it)) {
3730 CmdArgs.push_back("-backend-option");
3731 CmdArgs.push_back("-arm-restrict-it");
3732 } else {
3733 CmdArgs.push_back("-backend-option");
3734 CmdArgs.push_back("-arm-no-restrict-it");
3735 }
Stephen Hines6bcf27b2014-05-29 04:14:42 -07003736 } else if (TT.isOSWindows() && (TT.getArch() == llvm::Triple::arm ||
3737 TT.getArch() == llvm::Triple::thumb)) {
3738 // Windows on ARM expects restricted IT blocks
3739 CmdArgs.push_back("-backend-option");
3740 CmdArgs.push_back("-arm-restrict-it");
Weiming Zhao7792fde2013-11-13 18:31:23 +00003741 }
3742
Stephen Hinesef822542014-07-21 00:47:37 -07003743 if (TT.getArch() == llvm::Triple::arm ||
3744 TT.getArch() == llvm::Triple::thumb) {
3745 if (Arg *A = Args.getLastArg(options::OPT_mlong_calls,
3746 options::OPT_mno_long_calls)) {
3747 if (A->getOption().matches(options::OPT_mlong_calls)) {
3748 CmdArgs.push_back("-backend-option");
3749 CmdArgs.push_back("-arm-long-calls");
3750 }
3751 }
3752 }
3753
Daniel Dunbar48d1ef72009-04-07 21:16:11 +00003754 // Forward -f options with positive and negative forms; we translate
3755 // these by hand.
Diego Novillob85a9ec2013-11-13 12:22:39 +00003756 if (Arg *A = Args.getLastArg(options::OPT_fprofile_sample_use_EQ)) {
3757 StringRef fname = A->getValue();
3758 if (!llvm::sys::fs::exists(fname))
3759 D.Diag(diag::err_drv_no_such_file) << fname;
3760 else
3761 A->render(Args, CmdArgs);
3762 }
Daniel Dunbar48d1ef72009-04-07 21:16:11 +00003763
Fariborz Jahanianb466d012011-01-07 01:05:02 +00003764 if (Args.hasArg(options::OPT_mkernel)) {
Daniel Dunbar2843c192011-02-04 17:24:47 +00003765 if (!Args.hasArg(options::OPT_fapple_kext) && types::isCXX(InputType))
Fariborz Jahanianb466d012011-01-07 01:05:02 +00003766 CmdArgs.push_back("-fapple-kext");
3767 if (!Args.hasArg(options::OPT_fbuiltin))
3768 CmdArgs.push_back("-fno-builtin");
Chad Rosier3d265502012-03-26 21:29:17 +00003769 Args.ClaimAllArgs(options::OPT_fno_builtin);
Fariborz Jahanianb466d012011-01-07 01:05:02 +00003770 }
Daniel Dunbar9e5cc6b2009-11-17 08:07:36 +00003771 // -fbuiltin is default.
Fariborz Jahanianb466d012011-01-07 01:05:02 +00003772 else if (!Args.hasFlag(options::OPT_fbuiltin, options::OPT_fno_builtin))
Daniel Dunbar53e84842009-11-19 04:55:23 +00003773 CmdArgs.push_back("-fno-builtin");
Daniel Dunbar48d1ef72009-04-07 21:16:11 +00003774
Nuno Lopesfc284482009-12-16 16:59:22 +00003775 if (!Args.hasFlag(options::OPT_fassume_sane_operator_new,
3776 options::OPT_fno_assume_sane_operator_new))
3777 CmdArgs.push_back("-fno-assume-sane-operator-new");
3778
Daniel Dunbar9e5cc6b2009-11-17 08:07:36 +00003779 // -fblocks=0 is default.
3780 if (Args.hasFlag(options::OPT_fblocks, options::OPT_fno_blocks,
David Chisnalle6533ff2011-02-28 17:11:43 +00003781 getToolChain().IsBlocksDefault()) ||
3782 (Args.hasArg(options::OPT_fgnu_runtime) &&
3783 Args.hasArg(options::OPT_fobjc_nonfragile_abi) &&
3784 !Args.hasArg(options::OPT_fno_blocks))) {
Daniel Dunbar9e5cc6b2009-11-17 08:07:36 +00003785 CmdArgs.push_back("-fblocks");
John McCall13db5cf2011-09-09 20:41:01 +00003786
3787 if (!Args.hasArg(options::OPT_fgnu_runtime) &&
3788 !getToolChain().hasBlocksRuntime())
3789 CmdArgs.push_back("-fblocks-runtime-optional");
David Chisnall5e530af2009-11-17 19:33:30 +00003790 }
Daniel Dunbar48d1ef72009-04-07 21:16:11 +00003791
Stephen Hines176edba2014-12-01 14:53:08 -08003792 // -fmodules enables modules (off by default).
3793 // Users can pass -fno-cxx-modules to turn off modules support for
3794 // C++/Objective-C++ programs, which is a little less mature.
Douglas Gregorf43b7212013-01-16 01:23:41 +00003795 bool HaveModules = false;
Douglas Gregor64554ba2012-01-18 15:19:58 +00003796 if (Args.hasFlag(options::OPT_fmodules, options::OPT_fno_modules, false)) {
3797 bool AllowedInCXX = Args.hasFlag(options::OPT_fcxx_modules,
3798 options::OPT_fno_cxx_modules,
Stephen Hines176edba2014-12-01 14:53:08 -08003799 true);
Douglas Gregorf43b7212013-01-16 01:23:41 +00003800 if (AllowedInCXX || !types::isCXX(InputType)) {
Douglas Gregor64554ba2012-01-18 15:19:58 +00003801 CmdArgs.push_back("-fmodules");
Douglas Gregorf43b7212013-01-16 01:23:41 +00003802 HaveModules = true;
3803 }
3804 }
3805
Daniel Jasper056ec122013-08-05 20:26:17 +00003806 // -fmodule-maps enables module map processing (off by default) for header
3807 // checking. It is implied by -fmodules.
3808 if (Args.hasFlag(options::OPT_fmodule_maps, options::OPT_fno_module_maps,
3809 false)) {
3810 CmdArgs.push_back("-fmodule-maps");
3811 }
3812
Daniel Jasper95411412013-10-21 06:34:34 +00003813 // -fmodules-decluse checks that modules used are declared so (off by
3814 // default).
Daniel Jasperddd2dfc2013-09-24 09:14:14 +00003815 if (Args.hasFlag(options::OPT_fmodules_decluse,
3816 options::OPT_fno_modules_decluse,
3817 false)) {
Daniel Jasper097595a2013-09-29 12:40:54 +00003818 CmdArgs.push_back("-fmodules-decluse");
Daniel Jasperddd2dfc2013-09-24 09:14:14 +00003819 }
3820
Stephen Hines6bcf27b2014-05-29 04:14:42 -07003821 // -fmodules-strict-decluse is like -fmodule-decluse, but also checks that
3822 // all #included headers are part of modules.
3823 if (Args.hasFlag(options::OPT_fmodules_strict_decluse,
3824 options::OPT_fno_modules_strict_decluse,
3825 false)) {
3826 CmdArgs.push_back("-fmodules-strict-decluse");
3827 }
3828
Daniel Jasper95411412013-10-21 06:34:34 +00003829 // -fmodule-name specifies the module that is currently being built (or
3830 // used for header checking by -fmodule-maps).
Stephen Hines176edba2014-12-01 14:53:08 -08003831 Args.AddLastArg(CmdArgs, options::OPT_fmodule_name);
Daniel Jasper95411412013-10-21 06:34:34 +00003832
Stephen Hines176edba2014-12-01 14:53:08 -08003833 // -fmodule-map-file can be used to specify files containing module
Daniel Jasper95411412013-10-21 06:34:34 +00003834 // definitions.
Stephen Hines176edba2014-12-01 14:53:08 -08003835 Args.AddAllArgs(CmdArgs, options::OPT_fmodule_map_file);
Daniel Jasper95411412013-10-21 06:34:34 +00003836
Stephen Hines176edba2014-12-01 14:53:08 -08003837 // -fmodule-file can be used to specify files containing precompiled modules.
3838 Args.AddAllArgs(CmdArgs, options::OPT_fmodule_file);
3839
3840 // -fmodule-cache-path specifies where our implicitly-built module files
3841 // should be written.
Stephen Hinesef822542014-07-21 00:47:37 -07003842 SmallString<128> ModuleCachePath;
3843 if (Arg *A = Args.getLastArg(options::OPT_fmodules_cache_path))
3844 ModuleCachePath = A->getValue();
3845 if (HaveModules) {
3846 if (C.isForDiagnostics()) {
3847 // When generating crash reports, we want to emit the modules along with
3848 // the reproduction sources, so we ignore any provided module path.
3849 ModuleCachePath = Output.getFilename();
3850 llvm::sys::path::replace_extension(ModuleCachePath, ".cache");
3851 llvm::sys::path::append(ModuleCachePath, "modules");
3852 } else if (ModuleCachePath.empty()) {
3853 // No module path was provided: use the default.
3854 llvm::sys::path::system_temp_directory(/*erasedOnReboot=*/false,
3855 ModuleCachePath);
3856 llvm::sys::path::append(ModuleCachePath, "org.llvm.clang");
3857 llvm::sys::path::append(ModuleCachePath, "ModuleCache");
Douglas Gregor953a61f2013-02-07 19:01:24 +00003858 }
Douglas Gregor250172a2013-02-07 22:59:12 +00003859 const char Arg[] = "-fmodules-cache-path=";
Stephen Hinesef822542014-07-21 00:47:37 -07003860 ModuleCachePath.insert(ModuleCachePath.begin(), Arg, Arg + strlen(Arg));
3861 CmdArgs.push_back(Args.MakeArgString(ModuleCachePath));
Douglas Gregor953a61f2013-02-07 19:01:24 +00003862 }
3863
Stephen Hinesef822542014-07-21 00:47:37 -07003864 // When building modules and generating crashdumps, we need to dump a module
3865 // dependency VFS alongside the output.
3866 if (HaveModules && C.isForDiagnostics()) {
3867 SmallString<128> VFSDir(Output.getFilename());
3868 llvm::sys::path::replace_extension(VFSDir, ".cache");
Stephen Hines176edba2014-12-01 14:53:08 -08003869 // Add the cache directory as a temp so the crash diagnostics pick it up.
3870 C.addTempFile(Args.MakeArgString(VFSDir));
3871
Stephen Hinesef822542014-07-21 00:47:37 -07003872 llvm::sys::path::append(VFSDir, "vfs");
3873 CmdArgs.push_back("-module-dependency-dir");
3874 CmdArgs.push_back(Args.MakeArgString(VFSDir));
Stephen Hines651f13c2014-04-23 16:59:28 -07003875 }
3876
Stephen Hines176edba2014-12-01 14:53:08 -08003877 if (HaveModules)
3878 Args.AddLastArg(CmdArgs, options::OPT_fmodules_user_build_path);
Stephen Hinesef822542014-07-21 00:47:37 -07003879
Douglas Gregor953a61f2013-02-07 19:01:24 +00003880 // Pass through all -fmodules-ignore-macro arguments.
3881 Args.AddAllArgs(CmdArgs, options::OPT_fmodules_ignore_macro);
Douglas Gregord44d2872013-03-25 21:19:16 +00003882 Args.AddLastArg(CmdArgs, options::OPT_fmodules_prune_interval);
3883 Args.AddLastArg(CmdArgs, options::OPT_fmodules_prune_after);
Douglas Gregor953a61f2013-02-07 19:01:24 +00003884
Stephen Hines651f13c2014-04-23 16:59:28 -07003885 Args.AddLastArg(CmdArgs, options::OPT_fbuild_session_timestamp);
3886
Stephen Hines176edba2014-12-01 14:53:08 -08003887 if (Arg *A = Args.getLastArg(options::OPT_fbuild_session_file)) {
3888 if (Args.hasArg(options::OPT_fbuild_session_timestamp))
3889 D.Diag(diag::err_drv_argument_not_allowed_with)
3890 << A->getAsString(Args) << "-fbuild-session-timestamp";
3891
3892 llvm::sys::fs::file_status Status;
3893 if (llvm::sys::fs::status(A->getValue(), Status))
3894 D.Diag(diag::err_drv_no_such_file) << A->getValue();
3895 char TimeStamp[48];
3896 snprintf(TimeStamp, sizeof(TimeStamp), "-fbuild-session-timestamp=%" PRIu64,
3897 (uint64_t)Status.getLastModificationTime().toEpochTime());
3898 CmdArgs.push_back(Args.MakeArgString(TimeStamp));
3899 }
3900
Stephen Hines651f13c2014-04-23 16:59:28 -07003901 if (Args.getLastArg(options::OPT_fmodules_validate_once_per_build_session)) {
Stephen Hines176edba2014-12-01 14:53:08 -08003902 if (!Args.getLastArg(options::OPT_fbuild_session_timestamp,
3903 options::OPT_fbuild_session_file))
Stephen Hines651f13c2014-04-23 16:59:28 -07003904 D.Diag(diag::err_drv_modules_validate_once_requires_timestamp);
3905
3906 Args.AddLastArg(CmdArgs,
3907 options::OPT_fmodules_validate_once_per_build_session);
3908 }
3909
3910 Args.AddLastArg(CmdArgs, options::OPT_fmodules_validate_system_headers);
3911
John McCall32579cf2010-04-09 19:12:06 +00003912 // -faccess-control is default.
John McCall7002f4c2010-04-09 19:03:51 +00003913 if (Args.hasFlag(options::OPT_fno_access_control,
3914 options::OPT_faccess_control,
John McCall32579cf2010-04-09 19:12:06 +00003915 false))
John McCall7002f4c2010-04-09 19:03:51 +00003916 CmdArgs.push_back("-fno-access-control");
John McCall3ddd6e02010-03-17 01:32:13 +00003917
Anders Carlssona4c24752010-11-21 00:09:52 +00003918 // -felide-constructors is the default.
3919 if (Args.hasFlag(options::OPT_fno_elide_constructors,
3920 options::OPT_felide_constructors,
3921 false))
3922 CmdArgs.push_back("-fno-elide-constructors");
3923
Daniel Dunbar0be42c42009-11-17 07:06:20 +00003924 // -frtti is default.
Chad Rosierafc4baa2012-03-26 22:04:46 +00003925 if (!Args.hasFlag(options::OPT_frtti, options::OPT_fno_rtti) ||
Richard Smithc4dabad2012-11-05 22:04:41 +00003926 KernelOrKext) {
Daniel Dunbar53e84842009-11-19 04:55:23 +00003927 CmdArgs.push_back("-fno-rtti");
Mike Stump738f8c22009-07-31 23:15:31 +00003928
Richard Smithc4dabad2012-11-05 22:04:41 +00003929 // -fno-rtti cannot usefully be combined with -fsanitize=vptr.
Alexey Samsonovbb1071c2012-11-06 15:09:03 +00003930 if (Sanitize.sanitizesVptr()) {
NAKAMURA Takumi03c60762012-11-06 22:02:00 +00003931 std::string NoRttiArg =
Richard Smithc4dabad2012-11-05 22:04:41 +00003932 Args.getLastArg(options::OPT_mkernel,
3933 options::OPT_fapple_kext,
Richard Smith04fd3822012-11-06 01:12:02 +00003934 options::OPT_fno_rtti)->getAsString(Args);
Richard Smithc4dabad2012-11-05 22:04:41 +00003935 D.Diag(diag::err_drv_argument_not_allowed_with)
3936 << "-fsanitize=vptr" << NoRttiArg;
3937 }
3938 }
3939
Tony Linthicum96319392011-12-12 21:14:55 +00003940 // -fshort-enums=0 is default for all architectures except Hexagon.
Argyrios Kyrtzidis9a2b9d72010-10-08 00:25:19 +00003941 if (Args.hasFlag(options::OPT_fshort_enums,
Tony Linthicum96319392011-12-12 21:14:55 +00003942 options::OPT_fno_short_enums,
Eli Bendersky8f4269a2013-07-24 22:20:49 +00003943 getToolChain().getArch() ==
Tony Linthicum96319392011-12-12 21:14:55 +00003944 llvm::Triple::hexagon))
Argyrios Kyrtzidis9a2b9d72010-10-08 00:25:19 +00003945 CmdArgs.push_back("-fshort-enums");
3946
Daniel Dunbar1f95e652009-11-17 06:37:03 +00003947 // -fsigned-char is default.
Daniel Dunbar6d2eb4d2009-11-25 10:14:30 +00003948 if (!Args.hasFlag(options::OPT_fsigned_char, options::OPT_funsigned_char,
Daniel Dunbar1f95e652009-11-17 06:37:03 +00003949 isSignedCharDefault(getToolChain().getTriple())))
Daniel Dunbar76743522009-11-29 02:39:08 +00003950 CmdArgs.push_back("-fno-signed-char");
Eli Friedman5a779732009-06-05 07:21:14 +00003951
Anders Carlssona508b7d2010-02-06 23:23:06 +00003952 // -fthreadsafe-static is default.
Michael J. Spencer20249a12010-10-21 03:16:25 +00003953 if (!Args.hasFlag(options::OPT_fthreadsafe_statics,
Anders Carlssona508b7d2010-02-06 23:23:06 +00003954 options::OPT_fno_threadsafe_statics))
3955 CmdArgs.push_back("-fno-threadsafe-statics");
3956
Daniel Dunbarefb0fa92010-03-20 04:15:41 +00003957 // -fuse-cxa-atexit is default.
Stephen Hines651f13c2014-04-23 16:59:28 -07003958 if (!Args.hasFlag(options::OPT_fuse_cxa_atexit,
3959 options::OPT_fno_use_cxa_atexit,
3960 !IsWindowsCygnus && !IsWindowsGNU &&
3961 getToolChain().getArch() != llvm::Triple::hexagon &&
3962 getToolChain().getArch() != llvm::Triple::xcore) ||
Chad Rosierafc4baa2012-03-26 22:04:46 +00003963 KernelOrKext)
Daniel Dunbarefb0fa92010-03-20 04:15:41 +00003964 CmdArgs.push_back("-fno-use-cxa-atexit");
3965
Daniel Dunbar0be42c42009-11-17 07:06:20 +00003966 // -fms-extensions=0 is default.
Daniel Dunbar6d2eb4d2009-11-25 10:14:30 +00003967 if (Args.hasFlag(options::OPT_fms_extensions, options::OPT_fno_ms_extensions,
Stephen Hines651f13c2014-04-23 16:59:28 -07003968 IsWindowsMSVC))
Daniel Dunbar0be42c42009-11-17 07:06:20 +00003969 CmdArgs.push_back("-fms-extensions");
3970
Francois Pichetae556082011-09-17 04:32:15 +00003971 // -fms-compatibility=0 is default.
Douglas Gregorba97b6e2011-10-24 15:49:38 +00003972 if (Args.hasFlag(options::OPT_fms_compatibility,
3973 options::OPT_fno_ms_compatibility,
Stephen Hines651f13c2014-04-23 16:59:28 -07003974 (IsWindowsMSVC && Args.hasFlag(options::OPT_fms_extensions,
3975 options::OPT_fno_ms_extensions,
3976 true))))
Francois Pichetae556082011-09-17 04:32:15 +00003977 CmdArgs.push_back("-fms-compatibility");
3978
Stephen Hines176edba2014-12-01 14:53:08 -08003979 // -fms-compatibility-version=17.00 is default.
Michael J. Spencerdae4ac42010-10-21 05:21:48 +00003980 if (Args.hasFlag(options::OPT_fms_extensions, options::OPT_fno_ms_extensions,
Stephen Hines176edba2014-12-01 14:53:08 -08003981 IsWindowsMSVC) || Args.hasArg(options::OPT_fmsc_version) ||
3982 Args.hasArg(options::OPT_fms_compatibility_version)) {
3983 const Arg *MSCVersion = Args.getLastArg(options::OPT_fmsc_version);
3984 const Arg *MSCompatibilityVersion =
3985 Args.getLastArg(options::OPT_fms_compatibility_version);
Michael J. Spencerdae4ac42010-10-21 05:21:48 +00003986
Stephen Hines176edba2014-12-01 14:53:08 -08003987 if (MSCVersion && MSCompatibilityVersion)
3988 D.Diag(diag::err_drv_argument_not_allowed_with)
3989 << MSCVersion->getAsString(Args)
3990 << MSCompatibilityVersion->getAsString(Args);
3991
3992 std::string Ver;
3993 if (MSCompatibilityVersion)
3994 Ver = Args.getLastArgValue(options::OPT_fms_compatibility_version);
3995 else if (MSCVersion)
3996 Ver = getMSCompatibilityVersion(MSCVersion->getValue());
3997
3998 if (Ver.empty())
3999 CmdArgs.push_back("-fms-compatibility-version=17.00");
4000 else
4001 CmdArgs.push_back(Args.MakeArgString("-fms-compatibility-version=" + Ver));
4002 }
Michael J. Spencerdae4ac42010-10-21 05:21:48 +00004003
Eric Christophercfc01e42013-02-18 00:38:31 +00004004 // -fno-borland-extensions is default.
Dawn Perchik400b6072010-09-02 23:59:25 +00004005 if (Args.hasFlag(options::OPT_fborland_extensions,
4006 options::OPT_fno_borland_extensions, false))
4007 CmdArgs.push_back("-fborland-extensions");
4008
Francois Pichet8efcc012011-09-01 16:38:08 +00004009 // -fno-delayed-template-parsing is default, except for Windows where MSVC STL
4010 // needs it.
Francois Pichet8387e2a2011-04-22 22:18:13 +00004011 if (Args.hasFlag(options::OPT_fdelayed_template_parsing,
Stephen Hines651f13c2014-04-23 16:59:28 -07004012 options::OPT_fno_delayed_template_parsing, IsWindowsMSVC))
Francois Pichet805bc1f2011-08-26 00:22:34 +00004013 CmdArgs.push_back("-fdelayed-template-parsing");
Francois Pichet8387e2a2011-04-22 22:18:13 +00004014
Chandler Carrutheb5d7b72010-04-17 20:17:31 +00004015 // -fgnu-keywords default varies depending on language; only pass if
4016 // specified.
4017 if (Arg *A = Args.getLastArg(options::OPT_fgnu_keywords,
Daniel Dunbar40788d92010-04-24 17:56:39 +00004018 options::OPT_fno_gnu_keywords))
4019 A->render(Args, CmdArgs);
Chandler Carrutheb5d7b72010-04-17 20:17:31 +00004020
Rafael Espindola01ba8542011-06-02 17:30:53 +00004021 if (Args.hasFlag(options::OPT_fgnu89_inline,
4022 options::OPT_fno_gnu89_inline,
4023 false))
Rafael Espindolafb3f4aa2011-06-02 16:13:27 +00004024 CmdArgs.push_back("-fgnu89-inline");
4025
Chad Rosierfc055f92012-03-15 22:31:42 +00004026 if (Args.hasArg(options::OPT_fno_inline))
4027 CmdArgs.push_back("-fno-inline");
4028
Chad Rosier634a4b12012-03-06 21:17:19 +00004029 if (Args.hasArg(options::OPT_fno_inline_functions))
4030 CmdArgs.push_back("-fno-inline-functions");
Chad Rosier250008b2012-03-06 18:49:20 +00004031
John McCall260611a2012-06-20 06:18:46 +00004032 ObjCRuntime objcRuntime = AddObjCRuntimeArgs(Args, CmdArgs, rewriteKind);
John McCall9f084a32011-07-06 00:26:06 +00004033
John McCall260611a2012-06-20 06:18:46 +00004034 // -fobjc-dispatch-method is only relevant with the nonfragile-abi, and
Stephen Hines651f13c2014-04-23 16:59:28 -07004035 // legacy is the default. Except for deployment taget of 10.5,
4036 // next runtime is always legacy dispatch and -fno-objc-legacy-dispatch
4037 // gets ignored silently.
4038 if (objcRuntime.isNonFragile()) {
David Chisnall3c3ccd22011-09-30 13:32:35 +00004039 if (!Args.hasFlag(options::OPT_fobjc_legacy_dispatch,
4040 options::OPT_fno_objc_legacy_dispatch,
David Chisnall2c7886d2012-07-04 11:52:24 +00004041 objcRuntime.isLegacyDispatchDefaultForArch(
Eli Bendersky8f4269a2013-07-24 22:20:49 +00004042 getToolChain().getArch()))) {
David Chisnall3c3ccd22011-09-30 13:32:35 +00004043 if (getToolChain().UseObjCMixedDispatch())
4044 CmdArgs.push_back("-fobjc-dispatch-method=mixed");
4045 else
4046 CmdArgs.push_back("-fobjc-dispatch-method=non-legacy");
4047 }
4048 }
Rafael Espindola669496b2013-11-12 04:33:56 +00004049
Fariborz Jahanian5d5058c2013-11-12 17:08:46 +00004050 // When ObjectiveC legacy runtime is in effect on MacOSX,
4051 // turn on the option to do Array/Dictionary subscripting
4052 // by default.
Fariborz Jahanian08d86e92013-11-12 20:50:26 +00004053 if (getToolChain().getTriple().getArch() == llvm::Triple::x86 &&
4054 getToolChain().getTriple().isMacOSX() &&
4055 !getToolChain().getTriple().isMacOSXVersionLT(10, 7) &&
4056 objcRuntime.getKind() == ObjCRuntime::FragileMacOSX &&
Fariborz Jahanian5d5058c2013-11-12 17:08:46 +00004057 objcRuntime.isNeXTFamily())
4058 CmdArgs.push_back("-fobjc-subscripting-legacy-runtime");
4059
Fariborz Jahanian3d145f62012-11-15 19:02:45 +00004060 // -fencode-extended-block-signature=1 is default.
4061 if (getToolChain().IsEncodeExtendedBlockSignatureDefault()) {
4062 CmdArgs.push_back("-fencode-extended-block-signature");
4063 }
4064
John McCall9f084a32011-07-06 00:26:06 +00004065 // Allow -fno-objc-arr to trump -fobjc-arr/-fobjc-arc.
4066 // NOTE: This logic is duplicated in ToolChains.cpp.
4067 bool ARC = isObjCAutoRefCount(Args);
4068 if (ARC) {
John McCall0a7dd782012-08-21 02:47:43 +00004069 getToolChain().CheckObjCARC();
Argyrios Kyrtzidis5840dd92012-02-29 03:43:52 +00004070
John McCall9f084a32011-07-06 00:26:06 +00004071 CmdArgs.push_back("-fobjc-arc");
4072
Chandler Carruth7ffa0322011-11-04 07:34:47 +00004073 // FIXME: It seems like this entire block, and several around it should be
4074 // wrapped in isObjC, but for now we just use it here as this is where it
4075 // was being used previously.
4076 if (types::isCXX(InputType) && types::isObjC(InputType)) {
4077 if (getToolChain().GetCXXStdlibType(Args) == ToolChain::CST_Libcxx)
4078 CmdArgs.push_back("-fobjc-arc-cxxlib=libc++");
4079 else
4080 CmdArgs.push_back("-fobjc-arc-cxxlib=libstdc++");
4081 }
4082
John McCall9f084a32011-07-06 00:26:06 +00004083 // Allow the user to enable full exceptions code emission.
4084 // We define off for Objective-CC, on for Objective-C++.
4085 if (Args.hasFlag(options::OPT_fobjc_arc_exceptions,
4086 options::OPT_fno_objc_arc_exceptions,
4087 /*default*/ types::isCXX(InputType)))
4088 CmdArgs.push_back("-fobjc-arc-exceptions");
4089 }
4090
4091 // -fobjc-infer-related-result-type is the default, except in the Objective-C
4092 // rewriter.
John McCall260611a2012-06-20 06:18:46 +00004093 if (rewriteKind != RK_None)
John McCall9f084a32011-07-06 00:26:06 +00004094 CmdArgs.push_back("-fno-objc-infer-related-result-type");
Eric Christopher88b7cf02011-08-19 00:30:14 +00004095
John McCall9f084a32011-07-06 00:26:06 +00004096 // Handle -fobjc-gc and -fobjc-gc-only. They are exclusive, and -fobjc-gc-only
4097 // takes precedence.
4098 const Arg *GCArg = Args.getLastArg(options::OPT_fobjc_gc_only);
4099 if (!GCArg)
4100 GCArg = Args.getLastArg(options::OPT_fobjc_gc);
4101 if (GCArg) {
4102 if (ARC) {
Chris Lattner5f9e2722011-07-23 10:55:15 +00004103 D.Diag(diag::err_drv_objc_gc_arr)
John McCall9f084a32011-07-06 00:26:06 +00004104 << GCArg->getAsString(Args);
4105 } else if (getToolChain().SupportsObjCGC()) {
4106 GCArg->render(Args, CmdArgs);
4107 } else {
4108 // FIXME: We should move this to a hard error.
Chris Lattner5f9e2722011-07-23 10:55:15 +00004109 D.Diag(diag::warn_drv_objc_gc_unsupported)
John McCall9f084a32011-07-06 00:26:06 +00004110 << GCArg->getAsString(Args);
4111 }
4112 }
4113
Stephen Hinesef822542014-07-21 00:47:37 -07004114 // Handle GCC-style exception args.
4115 if (!C.getDriver().IsCLMode())
4116 addExceptionArgs(Args, InputType, getToolChain().getTriple(), KernelOrKext,
4117 objcRuntime, CmdArgs);
John McCalld71315c2011-06-22 00:53:57 +00004118
4119 if (getToolChain().UseSjLjExceptions())
4120 CmdArgs.push_back("-fsjlj-exceptions");
4121
4122 // C++ "sane" operator new.
Daniel Dunbar984eb862010-02-01 21:07:25 +00004123 if (!Args.hasFlag(options::OPT_fassume_sane_operator_new,
4124 options::OPT_fno_assume_sane_operator_new))
4125 CmdArgs.push_back("-fno-assume-sane-operator-new");
4126
Daniel Dunbarf35f14d2010-04-27 15:34:57 +00004127 // -fconstant-cfstrings is default, and may be subject to argument translation
4128 // on Darwin.
4129 if (!Args.hasFlag(options::OPT_fconstant_cfstrings,
4130 options::OPT_fno_constant_cfstrings) ||
4131 !Args.hasFlag(options::OPT_mconstant_cfstrings,
4132 options::OPT_mno_constant_cfstrings))
4133 CmdArgs.push_back("-fno-constant-cfstrings");
4134
John Thompsona6fda122009-11-05 20:14:16 +00004135 // -fshort-wchar default varies depending on platform; only
4136 // pass if specified.
Stephen Hines651f13c2014-04-23 16:59:28 -07004137 if (Arg *A = Args.getLastArg(options::OPT_fshort_wchar,
4138 options::OPT_fno_short_wchar))
Daniel Dunbar1744a352010-04-27 15:35:03 +00004139 A->render(Args, CmdArgs);
John Thompsona6fda122009-11-05 20:14:16 +00004140
Hans Wennborgb087a5d2013-07-31 23:39:13 +00004141 // -fno-pascal-strings is default, only pass non-default.
Daniel Dunbarc21c4852009-04-08 23:54:23 +00004142 if (Args.hasFlag(options::OPT_fpascal_strings,
Daniel Dunbar82d00682009-04-07 23:51:44 +00004143 options::OPT_fno_pascal_strings,
Daniel Dunbar82d00682009-04-07 23:51:44 +00004144 false))
Daniel Dunbar48d1ef72009-04-07 21:16:11 +00004145 CmdArgs.push_back("-fpascal-strings");
NAKAMURA Takumi125b4cb2011-02-17 08:50:50 +00004146
Daniel Dunbar88934e82011-10-05 21:04:55 +00004147 // Honor -fpack-struct= and -fpack-struct, if given. Note that
4148 // -fno-pack-struct doesn't apply to -fpack-struct=.
4149 if (Arg *A = Args.getLastArg(options::OPT_fpack_struct_EQ)) {
James Molloy8049c442012-05-02 07:56:14 +00004150 std::string PackStructStr = "-fpack-struct=";
Richard Smith1d489cf2012-11-01 04:30:05 +00004151 PackStructStr += A->getValue();
James Molloy8049c442012-05-02 07:56:14 +00004152 CmdArgs.push_back(Args.MakeArgString(PackStructStr));
Daniel Dunbar88934e82011-10-05 21:04:55 +00004153 } else if (Args.hasFlag(options::OPT_fpack_struct,
4154 options::OPT_fno_pack_struct, false)) {
James Molloy8049c442012-05-02 07:56:14 +00004155 CmdArgs.push_back("-fpack-struct=1");
Daniel Dunbar88934e82011-10-05 21:04:55 +00004156 }
4157
Stephen Hines176edba2014-12-01 14:53:08 -08004158 // Handle -fmax-type-align=N and -fno-type-align
4159 bool SkipMaxTypeAlign = Args.hasArg(options::OPT_fno_max_type_align);
4160 if (Arg *A = Args.getLastArg(options::OPT_fmax_type_align_EQ)) {
4161 if (!SkipMaxTypeAlign) {
4162 std::string MaxTypeAlignStr = "-fmax-type-align=";
4163 MaxTypeAlignStr += A->getValue();
4164 CmdArgs.push_back(Args.MakeArgString(MaxTypeAlignStr));
4165 }
4166 } else if (getToolChain().getTriple().isOSDarwin()) {
4167 if (!SkipMaxTypeAlign) {
4168 std::string MaxTypeAlignStr = "-fmax-type-align=16";
4169 CmdArgs.push_back(Args.MakeArgString(MaxTypeAlignStr));
4170 }
4171 }
4172
Robert Lytton5f15f4d2013-08-13 09:43:10 +00004173 if (KernelOrKext || isNoCommonDefault(getToolChain().getTriple())) {
Fariborz Jahanianb466d012011-01-07 01:05:02 +00004174 if (!Args.hasArg(options::OPT_fcommon))
4175 CmdArgs.push_back("-fno-common");
Chad Rosierec09b3e2012-03-26 21:35:40 +00004176 Args.ClaimAllArgs(options::OPT_fno_common);
Fariborz Jahanianb466d012011-01-07 01:05:02 +00004177 }
Daniel Dunbar88934e82011-10-05 21:04:55 +00004178
Daniel Dunbar48d1ef72009-04-07 21:16:11 +00004179 // -fcommon is default, only pass non-default.
Fariborz Jahanianb466d012011-01-07 01:05:02 +00004180 else if (!Args.hasFlag(options::OPT_fcommon, options::OPT_fno_common))
Daniel Dunbar48d1ef72009-04-07 21:16:11 +00004181 CmdArgs.push_back("-fno-common");
4182
Daniel Dunbar70d3c922009-04-15 02:37:43 +00004183 // -fsigned-bitfields is default, and clang doesn't yet support
Daniel Dunbar06205ca2010-10-15 22:30:42 +00004184 // -funsigned-bitfields.
Mike Stump1eb44332009-09-09 15:08:12 +00004185 if (!Args.hasFlag(options::OPT_fsigned_bitfields,
Daniel Dunbar70d3c922009-04-15 02:37:43 +00004186 options::OPT_funsigned_bitfields))
Chris Lattner5f9e2722011-07-23 10:55:15 +00004187 D.Diag(diag::warn_drv_clang_unsupported)
Daniel Dunbar70d3c922009-04-15 02:37:43 +00004188 << Args.getLastArg(options::OPT_funsigned_bitfields)->getAsString(Args);
4189
Daniel Dunbar06205ca2010-10-15 22:30:42 +00004190 // -fsigned-bitfields is default, and clang doesn't support -fno-for-scope.
4191 if (!Args.hasFlag(options::OPT_ffor_scope,
4192 options::OPT_fno_for_scope))
Chris Lattner5f9e2722011-07-23 10:55:15 +00004193 D.Diag(diag::err_drv_clang_unsupported)
Daniel Dunbar06205ca2010-10-15 22:30:42 +00004194 << Args.getLastArg(options::OPT_fno_for_scope)->getAsString(Args);
4195
Stephen Hinesef822542014-07-21 00:47:37 -07004196 // -finput_charset=UTF-8 is default. Reject others
4197 if (Arg *inputCharset = Args.getLastArg(
4198 options::OPT_finput_charset_EQ)) {
4199 StringRef value = inputCharset->getValue();
4200 if (value != "UTF-8")
4201 D.Diag(diag::err_drv_invalid_value) << inputCharset->getAsString(Args) << value;
4202 }
4203
Stephen Hines176edba2014-12-01 14:53:08 -08004204 // -fexec_charset=UTF-8 is default. Reject others
4205 if (Arg *execCharset = Args.getLastArg(
4206 options::OPT_fexec_charset_EQ)) {
4207 StringRef value = execCharset->getValue();
4208 if (value != "UTF-8")
4209 D.Diag(diag::err_drv_invalid_value) << execCharset->getAsString(Args) << value;
4210 }
4211
Jeffrey Yasskin0ea22fd2010-06-08 04:56:20 +00004212 // -fcaret-diagnostics is default.
4213 if (!Args.hasFlag(options::OPT_fcaret_diagnostics,
4214 options::OPT_fno_caret_diagnostics, true))
4215 CmdArgs.push_back("-fno-caret-diagnostics");
4216
Daniel Dunbar49138fc2009-04-19 21:09:34 +00004217 // -fdiagnostics-fixit-info is default, only pass non-default.
Mike Stump1eb44332009-09-09 15:08:12 +00004218 if (!Args.hasFlag(options::OPT_fdiagnostics_fixit_info,
Daniel Dunbar49138fc2009-04-19 21:09:34 +00004219 options::OPT_fno_diagnostics_fixit_info))
4220 CmdArgs.push_back("-fno-diagnostics-fixit-info");
Eric Christopher88b7cf02011-08-19 00:30:14 +00004221
Daniel Dunbar9e820ee2009-04-16 06:32:38 +00004222 // Enable -fdiagnostics-show-option by default.
Mike Stump1eb44332009-09-09 15:08:12 +00004223 if (Args.hasFlag(options::OPT_fdiagnostics_show_option,
Daniel Dunbar9e820ee2009-04-16 06:32:38 +00004224 options::OPT_fno_diagnostics_show_option))
4225 CmdArgs.push_back("-fdiagnostics-show-option");
Daniel Dunbar838be482009-11-04 06:24:57 +00004226
Chris Lattner6fbe8392010-05-04 21:55:25 +00004227 if (const Arg *A =
4228 Args.getLastArg(options::OPT_fdiagnostics_show_category_EQ)) {
4229 CmdArgs.push_back("-fdiagnostics-show-category");
Richard Smith1d489cf2012-11-01 04:30:05 +00004230 CmdArgs.push_back(A->getValue());
Chris Lattner6fbe8392010-05-04 21:55:25 +00004231 }
Daniel Dunbarca0e0542010-08-24 16:47:49 +00004232
Douglas Gregorc9471b02011-05-21 17:07:29 +00004233 if (const Arg *A =
4234 Args.getLastArg(options::OPT_fdiagnostics_format_EQ)) {
4235 CmdArgs.push_back("-fdiagnostics-format");
Richard Smith1d489cf2012-11-01 04:30:05 +00004236 CmdArgs.push_back(A->getValue());
Douglas Gregorc9471b02011-05-21 17:07:29 +00004237 }
4238
Chandler Carruthabaca7a2011-03-27 01:50:55 +00004239 if (Arg *A = Args.getLastArg(
4240 options::OPT_fdiagnostics_show_note_include_stack,
4241 options::OPT_fno_diagnostics_show_note_include_stack)) {
4242 if (A->getOption().matches(
4243 options::OPT_fdiagnostics_show_note_include_stack))
4244 CmdArgs.push_back("-fdiagnostics-show-note-include-stack");
4245 else
4246 CmdArgs.push_back("-fno-diagnostics-show-note-include-stack");
4247 }
4248
Daniel Dunbar838be482009-11-04 06:24:57 +00004249 // Color diagnostics are the default, unless the terminal doesn't support
4250 // them.
Nico Weber9753d462013-04-17 21:52:44 +00004251 // Support both clang's -f[no-]color-diagnostics and gcc's
4252 // -f[no-]diagnostics-colors[=never|always|auto].
4253 enum { Colors_On, Colors_Off, Colors_Auto } ShowColors = Colors_Auto;
Stephen Hinesef822542014-07-21 00:47:37 -07004254 for (const auto &Arg : Args) {
4255 const Option &O = Arg->getOption();
Nico Weber9753d462013-04-17 21:52:44 +00004256 if (!O.matches(options::OPT_fcolor_diagnostics) &&
4257 !O.matches(options::OPT_fdiagnostics_color) &&
4258 !O.matches(options::OPT_fno_color_diagnostics) &&
4259 !O.matches(options::OPT_fno_diagnostics_color) &&
4260 !O.matches(options::OPT_fdiagnostics_color_EQ))
4261 continue;
4262
Stephen Hinesef822542014-07-21 00:47:37 -07004263 Arg->claim();
Nico Weber9753d462013-04-17 21:52:44 +00004264 if (O.matches(options::OPT_fcolor_diagnostics) ||
4265 O.matches(options::OPT_fdiagnostics_color)) {
4266 ShowColors = Colors_On;
4267 } else if (O.matches(options::OPT_fno_color_diagnostics) ||
4268 O.matches(options::OPT_fno_diagnostics_color)) {
4269 ShowColors = Colors_Off;
4270 } else {
4271 assert(O.matches(options::OPT_fdiagnostics_color_EQ));
Stephen Hinesef822542014-07-21 00:47:37 -07004272 StringRef value(Arg->getValue());
Nico Weber9753d462013-04-17 21:52:44 +00004273 if (value == "always")
4274 ShowColors = Colors_On;
4275 else if (value == "never")
4276 ShowColors = Colors_Off;
4277 else if (value == "auto")
4278 ShowColors = Colors_Auto;
4279 else
4280 getToolChain().getDriver().Diag(diag::err_drv_clang_unsupported)
4281 << ("-fdiagnostics-color=" + value).str();
4282 }
4283 }
4284 if (ShowColors == Colors_On ||
4285 (ShowColors == Colors_Auto && llvm::sys::Process::StandardErrHasColors()))
Daniel Dunbar838be482009-11-04 06:24:57 +00004286 CmdArgs.push_back("-fcolor-diagnostics");
4287
Nico Rieck2956ef42013-09-11 00:38:02 +00004288 if (Args.hasArg(options::OPT_fansi_escape_codes))
4289 CmdArgs.push_back("-fansi-escape-codes");
4290
Daniel Dunbar75eb1d62009-06-08 21:13:54 +00004291 if (!Args.hasFlag(options::OPT_fshow_source_location,
4292 options::OPT_fno_show_source_location))
4293 CmdArgs.push_back("-fno-show-source-location");
Daniel Dunbar9e820ee2009-04-16 06:32:38 +00004294
Douglas Gregorc9471b02011-05-21 17:07:29 +00004295 if (!Args.hasFlag(options::OPT_fshow_column,
4296 options::OPT_fno_show_column,
4297 true))
4298 CmdArgs.push_back("-fno-show-column");
4299
Douglas Gregora0068fc2010-07-09 17:35:33 +00004300 if (!Args.hasFlag(options::OPT_fspell_checking,
4301 options::OPT_fno_spell_checking))
4302 CmdArgs.push_back("-fno-spell-checking");
Daniel Dunbarca0e0542010-08-24 16:47:49 +00004303
Daniel Dunbar25b26eb2010-10-18 22:49:46 +00004304
Chad Rosier15490fd2012-12-05 21:08:21 +00004305 // -fno-asm-blocks is default.
4306 if (Args.hasFlag(options::OPT_fasm_blocks, options::OPT_fno_asm_blocks,
4307 false))
4308 CmdArgs.push_back("-fasm-blocks");
Daniel Dunbar25b26eb2010-10-18 22:49:46 +00004309
Arnold Schwaighofer99662a12013-08-13 15:46:23 +00004310 // Enable vectorization per default according to the optimization level
4311 // selected. For optimization levels that want vectorization we use the alias
4312 // option to simplify the hasFlag logic.
Stephen Hines6bcf27b2014-05-29 04:14:42 -07004313 bool EnableVec = shouldEnableVectorizerAtOLevel(Args, false);
Arnold Schwaighofer99662a12013-08-13 15:46:23 +00004314 OptSpecifier VectorizeAliasOption = EnableVec ? options::OPT_O_Group :
Chad Rosier31422792013-04-24 18:29:59 +00004315 options::OPT_fvectorize;
Chad Rosier31422792013-04-24 18:29:59 +00004316 if (Args.hasFlag(options::OPT_fvectorize, VectorizeAliasOption,
Hal Finkelcf5691e2013-08-28 05:21:45 +00004317 options::OPT_fno_vectorize, EnableVec))
Chad Rosierc04d0932012-12-11 17:12:28 +00004318 CmdArgs.push_back("-vectorize-loops");
Chad Rosierc04d0932012-12-11 17:12:28 +00004319
Stephen Hines6bcf27b2014-05-29 04:14:42 -07004320 // -fslp-vectorize is enabled based on the optimization level selected.
4321 bool EnableSLPVec = shouldEnableVectorizerAtOLevel(Args, true);
4322 OptSpecifier SLPVectAliasOption = EnableSLPVec ? options::OPT_O_Group :
4323 options::OPT_fslp_vectorize;
4324 if (Args.hasFlag(options::OPT_fslp_vectorize, SLPVectAliasOption,
4325 options::OPT_fno_slp_vectorize, EnableSLPVec))
Nadav Rotem50ea9632013-04-15 04:57:18 +00004326 CmdArgs.push_back("-vectorize-slp");
Hal Finkel443c9992012-12-11 19:59:32 +00004327
Nadav Rotem3c6a9b02013-04-15 05:38:41 +00004328 // -fno-slp-vectorize-aggressive is default.
4329 if (Args.hasFlag(options::OPT_fslp_vectorize_aggressive,
Nick Lewyckyfdf137b2013-06-25 01:49:44 +00004330 options::OPT_fno_slp_vectorize_aggressive, false))
Nadav Rotem3c6a9b02013-04-15 05:38:41 +00004331 CmdArgs.push_back("-vectorize-slp-aggressive");
Nadav Rotem3c6a9b02013-04-15 05:38:41 +00004332
Jeffrey Yasskin5edbdcc2010-06-11 05:57:47 +00004333 if (Arg *A = Args.getLastArg(options::OPT_fshow_overloads_EQ))
4334 A->render(Args, CmdArgs);
4335
Daniel Dunbar7695fba2009-04-19 21:20:32 +00004336 // -fdollars-in-identifiers default varies depending on platform and
4337 // language; only pass if specified.
Mike Stump1eb44332009-09-09 15:08:12 +00004338 if (Arg *A = Args.getLastArg(options::OPT_fdollars_in_identifiers,
Daniel Dunbar7695fba2009-04-19 21:20:32 +00004339 options::OPT_fno_dollars_in_identifiers)) {
4340 if (A->getOption().matches(options::OPT_fdollars_in_identifiers))
Daniel Dunbar8663b182009-12-16 20:10:18 +00004341 CmdArgs.push_back("-fdollars-in-identifiers");
Daniel Dunbar7695fba2009-04-19 21:20:32 +00004342 else
Daniel Dunbar8663b182009-12-16 20:10:18 +00004343 CmdArgs.push_back("-fno-dollars-in-identifiers");
Daniel Dunbar7695fba2009-04-19 21:20:32 +00004344 }
4345
Daniel Dunbare027a4b2009-05-22 19:02:20 +00004346 // -funit-at-a-time is default, and we don't support -fno-unit-at-a-time for
4347 // practical purposes.
Mike Stump1eb44332009-09-09 15:08:12 +00004348 if (Arg *A = Args.getLastArg(options::OPT_funit_at_a_time,
Daniel Dunbare027a4b2009-05-22 19:02:20 +00004349 options::OPT_fno_unit_at_a_time)) {
4350 if (A->getOption().matches(options::OPT_fno_unit_at_a_time))
Chris Lattner5f9e2722011-07-23 10:55:15 +00004351 D.Diag(diag::warn_drv_clang_unsupported) << A->getAsString(Args);
Daniel Dunbare027a4b2009-05-22 19:02:20 +00004352 }
Eli Friedmanceb5c5b2009-07-14 21:58:17 +00004353
Eli Friedman19bda3a2011-11-02 01:53:16 +00004354 if (Args.hasFlag(options::OPT_fapple_pragma_pack,
4355 options::OPT_fno_apple_pragma_pack, false))
4356 CmdArgs.push_back("-fapple-pragma-pack");
4357
Eli Benderskyf3ecf892013-07-24 18:20:14 +00004358 // le32-specific flags:
4359 // -fno-math-builtin: clang should not convert math builtins to intrinsics
4360 // by default.
4361 if (getToolChain().getArch() == llvm::Triple::le32) {
4362 CmdArgs.push_back("-fno-math-builtin");
4363 }
4364
Daniel Dunbar2ba91572009-09-10 03:37:02 +00004365 // Default to -fno-builtin-str{cat,cpy} on Darwin for ARM.
Daniel Dunbarf84a4a42009-09-10 04:57:27 +00004366 //
Daniel Dunbar8ff5b282009-12-11 23:00:49 +00004367 // FIXME: This is disabled until clang -cc1 supports -fno-builtin-foo. PR4941.
Daniel Dunbarf84a4a42009-09-10 04:57:27 +00004368#if 0
Bob Wilson905c45f2011-10-14 05:03:44 +00004369 if (getToolChain().getTriple().isOSDarwin() &&
Eli Bendersky8f4269a2013-07-24 22:20:49 +00004370 (getToolChain().getArch() == llvm::Triple::arm ||
4371 getToolChain().getArch() == llvm::Triple::thumb)) {
Daniel Dunbar2ba91572009-09-10 03:37:02 +00004372 if (!Args.hasArg(options::OPT_fbuiltin_strcat))
4373 CmdArgs.push_back("-fno-builtin-strcat");
4374 if (!Args.hasArg(options::OPT_fbuiltin_strcpy))
4375 CmdArgs.push_back("-fno-builtin-strcpy");
4376 }
Daniel Dunbarf84a4a42009-09-10 04:57:27 +00004377#endif
Daniel Dunbar2ba91572009-09-10 03:37:02 +00004378
Stephen Hinesef822542014-07-21 00:47:37 -07004379 // Enable rewrite includes if the user's asked for it or if we're generating
4380 // diagnostics.
4381 // TODO: Once -module-dependency-dir works with -frewrite-includes it'd be
4382 // nice to enable this when doing a crashdump for modules as well.
4383 if (Args.hasFlag(options::OPT_frewrite_includes,
4384 options::OPT_fno_rewrite_includes, false) ||
4385 (C.isForDiagnostics() && !HaveModules))
4386 CmdArgs.push_back("-frewrite-includes");
4387
Daniel Dunbard98750f2011-03-18 21:23:40 +00004388 // Only allow -traditional or -traditional-cpp outside in preprocessing modes.
Mike Stump1eb44332009-09-09 15:08:12 +00004389 if (Arg *A = Args.getLastArg(options::OPT_traditional,
Daniel Dunbard98750f2011-03-18 21:23:40 +00004390 options::OPT_traditional_cpp)) {
4391 if (isa<PreprocessJobAction>(JA))
4392 CmdArgs.push_back("-traditional-cpp");
Eric Christopher88b7cf02011-08-19 00:30:14 +00004393 else
Chris Lattner5f9e2722011-07-23 10:55:15 +00004394 D.Diag(diag::err_drv_clang_unsupported) << A->getAsString(Args);
Daniel Dunbard98750f2011-03-18 21:23:40 +00004395 }
Eli Friedmanceb5c5b2009-07-14 21:58:17 +00004396
Daniel Dunbar1d460332009-03-18 10:01:51 +00004397 Args.AddLastArg(CmdArgs, options::OPT_dM);
Chris Lattnerd82df3a2009-04-12 01:56:53 +00004398 Args.AddLastArg(CmdArgs, options::OPT_dD);
Ted Kremenek36f6e302011-11-11 00:07:43 +00004399
4400 // Handle serialized diagnostics.
4401 if (Arg *A = Args.getLastArg(options::OPT__serialize_diags)) {
4402 CmdArgs.push_back("-serialize-diagnostic-file");
Richard Smith1d489cf2012-11-01 04:30:05 +00004403 CmdArgs.push_back(Args.MakeArgString(A->getValue()));
Ted Kremenek36f6e302011-11-11 00:07:43 +00004404 }
Daniel Dunbar1d460332009-03-18 10:01:51 +00004405
Ted Kremenek127ff2e2012-09-13 06:41:18 +00004406 if (Args.hasArg(options::OPT_fretain_comments_from_system_headers))
4407 CmdArgs.push_back("-fretain-comments-from-system-headers");
4408
Dmitri Gribenko6ebf0912013-02-22 14:21:27 +00004409 // Forward -fcomment-block-commands to -cc1.
4410 Args.AddAllArgs(CmdArgs, options::OPT_fcomment_block_commands);
Dmitri Gribenko6fd7d302013-04-10 15:35:17 +00004411 // Forward -fparse-all-comments to -cc1.
4412 Args.AddAllArgs(CmdArgs, options::OPT_fparse_all_comments);
Dmitri Gribenko6ebf0912013-02-22 14:21:27 +00004413
Daniel Dunbar3f87fb02010-04-15 06:09:03 +00004414 // Forward -Xclang arguments to -cc1, and -mllvm arguments to the LLVM option
4415 // parser.
Daniel Dunbar1d460332009-03-18 10:01:51 +00004416 Args.AddAllArgValues(CmdArgs, options::OPT_Xclang);
Daniel Dunbar3f87fb02010-04-15 06:09:03 +00004417 for (arg_iterator it = Args.filtered_begin(options::OPT_mllvm),
4418 ie = Args.filtered_end(); it != ie; ++it) {
Daniel Dunbar7e4953e2010-06-11 22:00:13 +00004419 (*it)->claim();
Daniel Dunbarfb36d212010-04-17 06:10:00 +00004420
Daniel Dunbar3f87fb02010-04-15 06:09:03 +00004421 // We translate this by hand to the -cc1 argument, since nightly test uses
4422 // it and developers have been trained to spell it with -mllvm.
Richard Smith1d489cf2012-11-01 04:30:05 +00004423 if (StringRef((*it)->getValue(0)) == "-disable-llvm-optzns")
Daniel Dunbar3f87fb02010-04-15 06:09:03 +00004424 CmdArgs.push_back("-disable-llvm-optzns");
4425 else
Daniel Dunbar7e4953e2010-06-11 22:00:13 +00004426 (*it)->render(Args, CmdArgs);
Daniel Dunbar3f87fb02010-04-15 06:09:03 +00004427 }
Daniel Dunbar1d460332009-03-18 10:01:51 +00004428
Daniel Dunbarcd8e4c42009-03-30 06:36:42 +00004429 if (Output.getType() == types::TY_Dependencies) {
4430 // Handled with other dependency code.
Daniel Dunbar115a7922009-03-19 07:29:38 +00004431 } else if (Output.isFilename()) {
Daniel Dunbarb488c1d2009-03-18 08:07:30 +00004432 CmdArgs.push_back("-o");
Daniel Dunbar115a7922009-03-19 07:29:38 +00004433 CmdArgs.push_back(Output.getFilename());
4434 } else {
4435 assert(Output.isNothing() && "Invalid output.");
Daniel Dunbarb488c1d2009-03-18 08:07:30 +00004436 }
4437
Stephen Hinesef822542014-07-21 00:47:37 -07004438 for (const auto &II : Inputs) {
Stephen Hines651f13c2014-04-23 16:59:28 -07004439 addDashXForInput(Args, II, CmdArgs);
4440
Daniel Dunbar7c1e4652010-08-02 02:38:21 +00004441 if (II.isFilename())
Daniel Dunbar115a7922009-03-19 07:29:38 +00004442 CmdArgs.push_back(II.getFilename());
Daniel Dunbar1d460332009-03-18 10:01:51 +00004443 else
Daniel Dunbar115a7922009-03-19 07:29:38 +00004444 II.getInputArg().renderAsInput(Args, CmdArgs);
Daniel Dunbar1d460332009-03-18 10:01:51 +00004445 }
Daniel Dunbarc21c4852009-04-08 23:54:23 +00004446
Chris Lattnere6113de2009-11-03 19:50:27 +00004447 Args.AddAllArgs(CmdArgs, options::OPT_undef);
4448
Daniel Dunbara001c1c2010-07-18 21:16:15 +00004449 const char *Exec = getToolChain().getDriver().getClangProgramPath();
Daniel Dunbarf2d8b9f2009-12-18 02:43:17 +00004450
4451 // Optionally embed the -cc1 level arguments into the debug info, for build
4452 // analysis.
4453 if (getToolChain().UseDwarfDebugFlags()) {
Daniel Dunbar6e900472010-06-04 18:47:06 +00004454 ArgStringList OriginalArgs;
Stephen Hinesef822542014-07-21 00:47:37 -07004455 for (const auto &Arg : Args)
4456 Arg->render(Args, OriginalArgs);
Daniel Dunbarca0e0542010-08-24 16:47:49 +00004457
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00004458 SmallString<256> Flags;
Daniel Dunbarf2d8b9f2009-12-18 02:43:17 +00004459 Flags += Exec;
Daniel Dunbar6e900472010-06-04 18:47:06 +00004460 for (unsigned i = 0, e = OriginalArgs.size(); i != e; ++i) {
Stephen Hines176edba2014-12-01 14:53:08 -08004461 SmallString<128> EscapedArg;
4462 EscapeSpacesAndBackslashes(OriginalArgs[i], EscapedArg);
Daniel Dunbarf2d8b9f2009-12-18 02:43:17 +00004463 Flags += " ";
Stephen Hines176edba2014-12-01 14:53:08 -08004464 Flags += EscapedArg;
Daniel Dunbarf2d8b9f2009-12-18 02:43:17 +00004465 }
4466 CmdArgs.push_back("-dwarf-debug-flags");
4467 CmdArgs.push_back(Args.MakeArgString(Flags.str()));
4468 }
4469
Eric Christopher80190392013-02-22 20:12:52 +00004470 // Add the split debug info name to the command lines here so we
4471 // can propagate it to the backend.
4472 bool SplitDwarf = Args.hasArg(options::OPT_gsplit_dwarf) &&
Cameron Esfahani57b1da12013-09-14 01:09:11 +00004473 getToolChain().getTriple().isOSLinux() &&
Eric Christopherff971d72013-02-22 23:50:16 +00004474 (isa<AssembleJobAction>(JA) || isa<CompileJobAction>(JA));
Eric Christopher80190392013-02-22 20:12:52 +00004475 const char *SplitDwarfOut;
4476 if (SplitDwarf) {
4477 CmdArgs.push_back("-split-dwarf-file");
4478 SplitDwarfOut = SplitDebugName(Args, Inputs);
4479 CmdArgs.push_back(SplitDwarfOut);
4480 }
4481
4482 // Finally add the compile command to the compilation.
Stephen Hines651f13c2014-04-23 16:59:28 -07004483 if (Args.hasArg(options::OPT__SLASH_fallback) &&
Stephen Hines6bcf27b2014-05-29 04:14:42 -07004484 Output.getType() == types::TY_Object &&
4485 (InputType == types::TY_C || InputType == types::TY_CXX)) {
Stephen Hines176edba2014-12-01 14:53:08 -08004486 auto CLCommand =
4487 getCLFallback()->GetCommand(C, JA, Output, Inputs, Args, LinkingOutput);
4488 C.addCommand(llvm::make_unique<FallbackCommand>(JA, *this, Exec, CmdArgs,
4489 std::move(CLCommand)));
Hans Wennborgc8ba0a02013-09-19 20:32:16 +00004490 } else {
Stephen Hines176edba2014-12-01 14:53:08 -08004491 C.addCommand(llvm::make_unique<Command>(JA, *this, Exec, CmdArgs));
Hans Wennborgc8ba0a02013-09-19 20:32:16 +00004492 }
4493
Daniel Dunbara880db02009-03-23 19:03:36 +00004494
Eric Christopherff971d72013-02-22 23:50:16 +00004495 // Handle the debug info splitting at object creation time if we're
4496 // creating an object.
Eric Christopher59320e72013-02-21 22:35:01 +00004497 // TODO: Currently only works on linux with newer objcopy.
Eric Christopherff971d72013-02-22 23:50:16 +00004498 if (SplitDwarf && !isa<CompileJobAction>(JA))
Eric Christopher80190392013-02-22 20:12:52 +00004499 SplitDebugInfo(getToolChain(), C, *this, JA, Args, Output, SplitDwarfOut);
Eric Christopher59320e72013-02-21 22:35:01 +00004500
Roman Divackybe4c8702011-02-10 16:52:03 +00004501 if (Arg *A = Args.getLastArg(options::OPT_pg))
4502 if (Args.hasArg(options::OPT_fomit_frame_pointer))
Chris Lattner5f9e2722011-07-23 10:55:15 +00004503 D.Diag(diag::err_drv_argument_not_allowed_with)
Roman Divackybe4c8702011-02-10 16:52:03 +00004504 << "-fomit-frame-pointer" << A->getAsString(Args);
Michael J. Spencer20249a12010-10-21 03:16:25 +00004505
Daniel Dunbar68fb4692009-04-03 20:51:31 +00004506 // Claim some arguments which clang supports automatically.
4507
Daniel Dunbarf4046862010-04-15 06:18:42 +00004508 // -fpch-preprocess is used with gcc to add a special marker in the output to
4509 // include the PCH file. Clang's PTH solution is completely transparent, so we
4510 // do not need to deal with it at all.
Daniel Dunbar68fb4692009-04-03 20:51:31 +00004511 Args.ClaimAllArgs(options::OPT_fpch_preprocess);
Daniel Dunbarc21c4852009-04-08 23:54:23 +00004512
Daniel Dunbara880db02009-03-23 19:03:36 +00004513 // Claim some arguments which clang doesn't support, but we don't
4514 // care to warn the user about.
Daniel Dunbarcdd96862009-11-25 11:53:23 +00004515 Args.ClaimAllArgs(options::OPT_clang_ignored_f_Group);
4516 Args.ClaimAllArgs(options::OPT_clang_ignored_m_Group);
Rafael Espindola035ff0c2011-02-28 23:29:45 +00004517
Rafael Espindola6155fbe2013-09-04 19:37:35 +00004518 // Disable warnings for clang -E -emit-llvm foo.c
Rafael Espindola9c094fb2011-03-01 05:25:27 +00004519 Args.ClaimAllArgs(options::OPT_emit_llvm);
Daniel Dunbar47ac7d22009-03-18 06:00:36 +00004520}
4521
John McCall260611a2012-06-20 06:18:46 +00004522/// Add options related to the Objective-C runtime/ABI.
4523///
4524/// Returns true if the runtime is non-fragile.
4525ObjCRuntime Clang::AddObjCRuntimeArgs(const ArgList &args,
4526 ArgStringList &cmdArgs,
4527 RewriteKind rewriteKind) const {
4528 // Look for the controlling runtime option.
4529 Arg *runtimeArg = args.getLastArg(options::OPT_fnext_runtime,
4530 options::OPT_fgnu_runtime,
4531 options::OPT_fobjc_runtime_EQ);
4532
4533 // Just forward -fobjc-runtime= to the frontend. This supercedes
4534 // options about fragility.
4535 if (runtimeArg &&
4536 runtimeArg->getOption().matches(options::OPT_fobjc_runtime_EQ)) {
4537 ObjCRuntime runtime;
Richard Smith1d489cf2012-11-01 04:30:05 +00004538 StringRef value = runtimeArg->getValue();
John McCall260611a2012-06-20 06:18:46 +00004539 if (runtime.tryParse(value)) {
4540 getToolChain().getDriver().Diag(diag::err_drv_unknown_objc_runtime)
4541 << value;
4542 }
4543
4544 runtimeArg->render(args, cmdArgs);
4545 return runtime;
4546 }
4547
4548 // Otherwise, we'll need the ABI "version". Version numbers are
4549 // slightly confusing for historical reasons:
4550 // 1 - Traditional "fragile" ABI
4551 // 2 - Non-fragile ABI, version 1
4552 // 3 - Non-fragile ABI, version 2
4553 unsigned objcABIVersion = 1;
4554 // If -fobjc-abi-version= is present, use that to set the version.
4555 if (Arg *abiArg = args.getLastArg(options::OPT_fobjc_abi_version_EQ)) {
Richard Smith1d489cf2012-11-01 04:30:05 +00004556 StringRef value = abiArg->getValue();
John McCall260611a2012-06-20 06:18:46 +00004557 if (value == "1")
4558 objcABIVersion = 1;
4559 else if (value == "2")
4560 objcABIVersion = 2;
4561 else if (value == "3")
4562 objcABIVersion = 3;
4563 else
4564 getToolChain().getDriver().Diag(diag::err_drv_clang_unsupported)
4565 << value;
4566 } else {
4567 // Otherwise, determine if we are using the non-fragile ABI.
4568 bool nonFragileABIIsDefault =
4569 (rewriteKind == RK_NonFragile ||
4570 (rewriteKind == RK_None &&
4571 getToolChain().IsObjCNonFragileABIDefault()));
4572 if (args.hasFlag(options::OPT_fobjc_nonfragile_abi,
4573 options::OPT_fno_objc_nonfragile_abi,
4574 nonFragileABIIsDefault)) {
4575 // Determine the non-fragile ABI version to use.
4576#ifdef DISABLE_DEFAULT_NONFRAGILEABI_TWO
4577 unsigned nonFragileABIVersion = 1;
4578#else
4579 unsigned nonFragileABIVersion = 2;
4580#endif
4581
4582 if (Arg *abiArg = args.getLastArg(
4583 options::OPT_fobjc_nonfragile_abi_version_EQ)) {
Richard Smith1d489cf2012-11-01 04:30:05 +00004584 StringRef value = abiArg->getValue();
John McCall260611a2012-06-20 06:18:46 +00004585 if (value == "1")
4586 nonFragileABIVersion = 1;
4587 else if (value == "2")
4588 nonFragileABIVersion = 2;
4589 else
4590 getToolChain().getDriver().Diag(diag::err_drv_clang_unsupported)
4591 << value;
4592 }
4593
4594 objcABIVersion = 1 + nonFragileABIVersion;
4595 } else {
4596 objcABIVersion = 1;
4597 }
4598 }
4599
4600 // We don't actually care about the ABI version other than whether
4601 // it's non-fragile.
4602 bool isNonFragile = objcABIVersion != 1;
4603
4604 // If we have no runtime argument, ask the toolchain for its default runtime.
4605 // However, the rewriter only really supports the Mac runtime, so assume that.
4606 ObjCRuntime runtime;
4607 if (!runtimeArg) {
4608 switch (rewriteKind) {
4609 case RK_None:
4610 runtime = getToolChain().getDefaultObjCRuntime(isNonFragile);
4611 break;
4612 case RK_Fragile:
4613 runtime = ObjCRuntime(ObjCRuntime::FragileMacOSX, VersionTuple());
4614 break;
4615 case RK_NonFragile:
4616 runtime = ObjCRuntime(ObjCRuntime::MacOSX, VersionTuple());
4617 break;
4618 }
4619
4620 // -fnext-runtime
4621 } else if (runtimeArg->getOption().matches(options::OPT_fnext_runtime)) {
4622 // On Darwin, make this use the default behavior for the toolchain.
4623 if (getToolChain().getTriple().isOSDarwin()) {
4624 runtime = getToolChain().getDefaultObjCRuntime(isNonFragile);
4625
4626 // Otherwise, build for a generic macosx port.
4627 } else {
4628 runtime = ObjCRuntime(ObjCRuntime::MacOSX, VersionTuple());
4629 }
4630
4631 // -fgnu-runtime
4632 } else {
4633 assert(runtimeArg->getOption().matches(options::OPT_fgnu_runtime));
David Chisnalla422cd02012-07-04 10:37:03 +00004634 // Legacy behaviour is to target the gnustep runtime if we are i
4635 // non-fragile mode or the GCC runtime in fragile mode.
4636 if (isNonFragile)
David Chisnall891dac72012-10-16 15:11:55 +00004637 runtime = ObjCRuntime(ObjCRuntime::GNUstep, VersionTuple(1,6));
David Chisnalla422cd02012-07-04 10:37:03 +00004638 else
4639 runtime = ObjCRuntime(ObjCRuntime::GCC, VersionTuple());
John McCall260611a2012-06-20 06:18:46 +00004640 }
4641
4642 cmdArgs.push_back(args.MakeArgString(
4643 "-fobjc-runtime=" + runtime.getAsString()));
4644 return runtime;
4645}
4646
Stephen Hinesef822542014-07-21 00:47:37 -07004647static bool maybeConsumeDash(const std::string &EH, size_t &I) {
4648 bool HaveDash = (I + 1 < EH.size() && EH[I + 1] == '-');
4649 I += HaveDash;
4650 return !HaveDash;
4651}
4652
4653struct EHFlags {
4654 EHFlags() : Synch(false), Asynch(false), NoExceptC(false) {}
4655 bool Synch;
4656 bool Asynch;
4657 bool NoExceptC;
4658};
4659
4660/// /EH controls whether to run destructor cleanups when exceptions are
4661/// thrown. There are three modifiers:
4662/// - s: Cleanup after "synchronous" exceptions, aka C++ exceptions.
4663/// - a: Cleanup after "asynchronous" exceptions, aka structured exceptions.
4664/// The 'a' modifier is unimplemented and fundamentally hard in LLVM IR.
4665/// - c: Assume that extern "C" functions are implicitly noexcept. This
4666/// modifier is an optimization, so we ignore it for now.
4667/// The default is /EHs-c-, meaning cleanups are disabled.
4668static EHFlags parseClangCLEHFlags(const Driver &D, const ArgList &Args) {
4669 EHFlags EH;
4670 std::vector<std::string> EHArgs = Args.getAllArgValues(options::OPT__SLASH_EH);
4671 for (auto EHVal : EHArgs) {
4672 for (size_t I = 0, E = EHVal.size(); I != E; ++I) {
4673 switch (EHVal[I]) {
4674 case 'a': EH.Asynch = maybeConsumeDash(EHVal, I); continue;
4675 case 'c': EH.NoExceptC = maybeConsumeDash(EHVal, I); continue;
4676 case 's': EH.Synch = maybeConsumeDash(EHVal, I); continue;
4677 default: break;
4678 }
4679 D.Diag(clang::diag::err_drv_invalid_value) << "/EH" << EHVal;
4680 break;
4681 }
4682 }
4683 return EH;
4684}
4685
Hans Wennborgb3574792013-08-08 00:17:41 +00004686void Clang::AddClangCLArgs(const ArgList &Args, ArgStringList &CmdArgs) const {
4687 unsigned RTOptionID = options::OPT__SLASH_MT;
4688
Hans Wennborg6d0a8d52013-09-10 20:18:04 +00004689 if (Args.hasArg(options::OPT__SLASH_LDd))
4690 // The /LDd option implies /MTd. The dependent lib part can be overridden,
4691 // but defining _DEBUG is sticky.
4692 RTOptionID = options::OPT__SLASH_MTd;
4693
Hans Wennborg76da1782013-09-18 22:26:39 +00004694 if (Arg *A = Args.getLastArg(options::OPT__SLASH_M_Group))
Hans Wennborgb3574792013-08-08 00:17:41 +00004695 RTOptionID = A->getOption().getID();
Hans Wennborg42ade492013-09-11 16:38:41 +00004696
Hans Wennborgb3574792013-08-08 00:17:41 +00004697 switch(RTOptionID) {
4698 case options::OPT__SLASH_MD:
Hans Wennborg6d0a8d52013-09-10 20:18:04 +00004699 if (Args.hasArg(options::OPT__SLASH_LDd))
4700 CmdArgs.push_back("-D_DEBUG");
Hans Wennborgb3574792013-08-08 00:17:41 +00004701 CmdArgs.push_back("-D_MT");
4702 CmdArgs.push_back("-D_DLL");
4703 CmdArgs.push_back("--dependent-lib=msvcrt");
4704 break;
4705 case options::OPT__SLASH_MDd:
4706 CmdArgs.push_back("-D_DEBUG");
4707 CmdArgs.push_back("-D_MT");
4708 CmdArgs.push_back("-D_DLL");
4709 CmdArgs.push_back("--dependent-lib=msvcrtd");
4710 break;
4711 case options::OPT__SLASH_MT:
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("--dependent-lib=libcmt");
4716 break;
4717 case options::OPT__SLASH_MTd:
4718 CmdArgs.push_back("-D_DEBUG");
4719 CmdArgs.push_back("-D_MT");
4720 CmdArgs.push_back("--dependent-lib=libcmtd");
4721 break;
4722 default:
4723 llvm_unreachable("Unexpected option ID.");
4724 }
4725
Reid Klecknera32c5232013-08-08 19:33:10 +00004726 // This provides POSIX compatibility (maps 'open' to '_open'), which most
4727 // users want. The /Za flag to cl.exe turns this off, but it's not
4728 // implemented in clang.
4729 CmdArgs.push_back("--dependent-lib=oldnames");
Hans Wennborgf0f98912013-08-08 19:54:30 +00004730
Stephen Hines176edba2014-12-01 14:53:08 -08004731 // Both /showIncludes and /E (and /EP) write to stdout. Allowing both
4732 // would produce interleaved output, so ignore /showIncludes in such cases.
4733 if (!Args.hasArg(options::OPT_E) && !Args.hasArg(options::OPT__SLASH_EP))
4734 if (Arg *A = Args.getLastArg(options::OPT_show_includes))
4735 A->render(Args, CmdArgs);
Hans Wennborgb6475522013-09-10 01:07:07 +00004736
Stephen Hinesef822542014-07-21 00:47:37 -07004737 // This controls whether or not we emit RTTI data for polymorphic types.
4738 if (Args.hasFlag(options::OPT__SLASH_GR_, options::OPT__SLASH_GR,
4739 /*default=*/false))
4740 CmdArgs.push_back("-fno-rtti-data");
Stephen Hines651f13c2014-04-23 16:59:28 -07004741
Stephen Hines651f13c2014-04-23 16:59:28 -07004742 const Driver &D = getToolChain().getDriver();
Stephen Hinesef822542014-07-21 00:47:37 -07004743 EHFlags EH = parseClangCLEHFlags(D, Args);
4744 // FIXME: Do something with NoExceptC.
4745 if (EH.Synch || EH.Asynch) {
4746 CmdArgs.push_back("-fexceptions");
4747 CmdArgs.push_back("-fcxx-exceptions");
4748 }
4749
4750 // /EP should expand to -E -P.
4751 if (Args.hasArg(options::OPT__SLASH_EP)) {
4752 CmdArgs.push_back("-E");
4753 CmdArgs.push_back("-P");
4754 }
4755
Stephen Hines651f13c2014-04-23 16:59:28 -07004756 Arg *MostGeneralArg = Args.getLastArg(options::OPT__SLASH_vmg);
4757 Arg *BestCaseArg = Args.getLastArg(options::OPT__SLASH_vmb);
4758 if (MostGeneralArg && BestCaseArg)
4759 D.Diag(clang::diag::err_drv_argument_not_allowed_with)
4760 << MostGeneralArg->getAsString(Args) << BestCaseArg->getAsString(Args);
4761
4762 if (MostGeneralArg) {
4763 Arg *SingleArg = Args.getLastArg(options::OPT__SLASH_vms);
4764 Arg *MultipleArg = Args.getLastArg(options::OPT__SLASH_vmm);
4765 Arg *VirtualArg = Args.getLastArg(options::OPT__SLASH_vmv);
4766
4767 Arg *FirstConflict = SingleArg ? SingleArg : MultipleArg;
4768 Arg *SecondConflict = VirtualArg ? VirtualArg : MultipleArg;
4769 if (FirstConflict && SecondConflict && FirstConflict != SecondConflict)
4770 D.Diag(clang::diag::err_drv_argument_not_allowed_with)
4771 << FirstConflict->getAsString(Args)
4772 << SecondConflict->getAsString(Args);
4773
4774 if (SingleArg)
4775 CmdArgs.push_back("-fms-memptr-rep=single");
4776 else if (MultipleArg)
4777 CmdArgs.push_back("-fms-memptr-rep=multiple");
4778 else
4779 CmdArgs.push_back("-fms-memptr-rep=virtual");
4780 }
4781
4782 if (Arg *A = Args.getLastArg(options::OPT_vtordisp_mode_EQ))
4783 A->render(Args, CmdArgs);
4784
Hans Wennborgb6475522013-09-10 01:07:07 +00004785 if (!Args.hasArg(options::OPT_fdiagnostics_format_EQ)) {
4786 CmdArgs.push_back("-fdiagnostics-format");
Hans Wennborg89e32742013-09-24 00:08:55 +00004787 if (Args.hasArg(options::OPT__SLASH_fallback))
4788 CmdArgs.push_back("msvc-fallback");
4789 else
4790 CmdArgs.push_back("msvc");
Hans Wennborgb6475522013-09-10 01:07:07 +00004791 }
Hans Wennborgb3574792013-08-08 00:17:41 +00004792}
4793
Stephen Hinesef822542014-07-21 00:47:37 -07004794visualstudio::Compile *Clang::getCLFallback() const {
4795 if (!CLFallback)
4796 CLFallback.reset(new visualstudio::Compile(getToolChain()));
4797 return CLFallback.get();
4798}
4799
Daniel Dunbar20a9aa52010-05-20 21:30:13 +00004800void ClangAs::ConstructJob(Compilation &C, const JobAction &JA,
Daniel Dunbar20a9aa52010-05-20 21:30:13 +00004801 const InputInfo &Output,
4802 const InputInfoList &Inputs,
4803 const ArgList &Args,
4804 const char *LinkingOutput) const {
Daniel Dunbar20a9aa52010-05-20 21:30:13 +00004805 ArgStringList CmdArgs;
4806
4807 assert(Inputs.size() == 1 && "Unexpected number of inputs.");
4808 const InputInfo &Input = Inputs[0];
4809
Rafael Espindoladbe80d92010-11-17 22:13:25 +00004810 // Don't warn about "clang -w -c foo.s"
4811 Args.ClaimAllArgs(options::OPT_w);
Rafael Espindola9c094fb2011-03-01 05:25:27 +00004812 // and "clang -emit-llvm -c foo.s"
4813 Args.ClaimAllArgs(options::OPT_emit_llvm);
Rafael Espindoladbe80d92010-11-17 22:13:25 +00004814
Daniel Dunbar20a9aa52010-05-20 21:30:13 +00004815 // Invoke ourselves in -cc1as mode.
4816 //
4817 // FIXME: Implement custom jobs for internal actions.
4818 CmdArgs.push_back("-cc1as");
4819
4820 // Add the "effective" target triple.
4821 CmdArgs.push_back("-triple");
Chad Rosier61ab80a2011-09-20 20:44:06 +00004822 std::string TripleStr =
4823 getToolChain().ComputeEffectiveClangTriple(Args, Input.getType());
Daniel Dunbar20a9aa52010-05-20 21:30:13 +00004824 CmdArgs.push_back(Args.MakeArgString(TripleStr));
4825
4826 // Set the output mode, we currently only expect to be used as a real
4827 // assembler.
4828 CmdArgs.push_back("-filetype");
4829 CmdArgs.push_back("obj");
4830
Eric Christopher27e2b982012-12-18 00:31:10 +00004831 // Set the main file name, so that debug info works even with
4832 // -save-temps or preprocessed assembly.
4833 CmdArgs.push_back("-main-file-name");
4834 CmdArgs.push_back(Clang::getBaseInputName(Args, Inputs));
4835
Rafael Espindolab330e402013-08-20 22:12:08 +00004836 // Add the target cpu
Rafael Espindola146dbbf2013-08-21 16:39:20 +00004837 const llvm::Triple &Triple = getToolChain().getTriple();
4838 std::string CPU = getCPUName(Args, Triple);
Rafael Espindolab330e402013-08-20 22:12:08 +00004839 if (!CPU.empty()) {
4840 CmdArgs.push_back("-target-cpu");
4841 CmdArgs.push_back(Args.MakeArgString(CPU));
4842 }
4843
Rafael Espindola146dbbf2013-08-21 16:39:20 +00004844 // Add the target features
4845 const Driver &D = getToolChain().getDriver();
Stephen Hines651f13c2014-04-23 16:59:28 -07004846 getTargetFeatures(D, Triple, Args, CmdArgs, true);
Jim Grosbachfc308292012-02-10 20:37:10 +00004847
Daniel Dunbar7f6f8c82011-03-17 17:37:29 +00004848 // Ignore explicit -force_cpusubtype_ALL option.
4849 (void) Args.hasArg(options::OPT_force__cpusubtype__ALL);
Daniel Dunbar20a9aa52010-05-20 21:30:13 +00004850
Eric Christopher8f0a4032012-01-10 00:38:01 +00004851 // Determine the original source input.
4852 const Action *SourceAction = &JA;
4853 while (SourceAction->getKind() != Action::InputClass) {
4854 assert(!SourceAction->getInputs().empty() && "unexpected root action!");
4855 SourceAction = SourceAction->getInputs()[0];
4856 }
4857
Chandler Carruthd566df62012-12-17 21:40:04 +00004858 // Forward -g and handle debug info related flags, assuming we are dealing
4859 // with an actual assembly file.
Eric Christopher8f0a4032012-01-10 00:38:01 +00004860 if (SourceAction->getType() == types::TY_Asm ||
4861 SourceAction->getType() == types::TY_PP_Asm) {
4862 Args.ClaimAllArgs(options::OPT_g_Group);
4863 if (Arg *A = Args.getLastArg(options::OPT_g_Group))
4864 if (!A->getOption().matches(options::OPT_g0))
4865 CmdArgs.push_back("-g");
Chandler Carruthd566df62012-12-17 21:40:04 +00004866
Stephen Hines6bcf27b2014-05-29 04:14:42 -07004867 if (Args.hasArg(options::OPT_gdwarf_2))
4868 CmdArgs.push_back("-gdwarf-2");
4869 if (Args.hasArg(options::OPT_gdwarf_3))
4870 CmdArgs.push_back("-gdwarf-3");
4871 if (Args.hasArg(options::OPT_gdwarf_4))
4872 CmdArgs.push_back("-gdwarf-4");
4873
Chandler Carruthd566df62012-12-17 21:40:04 +00004874 // Add the -fdebug-compilation-dir flag if needed.
4875 addDebugCompDirArg(Args, CmdArgs);
Kevin Enderby02341792013-01-17 21:38:06 +00004876
4877 // Set the AT_producer to the clang version when using the integrated
4878 // assembler on assembly source files.
4879 CmdArgs.push_back("-dwarf-debug-producer");
4880 CmdArgs.push_back(Args.MakeArgString(getClangFullVersion()));
Eric Christopher8f0a4032012-01-10 00:38:01 +00004881 }
Kevin Enderby567003e2011-12-22 19:31:58 +00004882
4883 // Optionally embed the -cc1as level arguments into the debug info, for build
4884 // analysis.
4885 if (getToolChain().UseDwarfDebugFlags()) {
4886 ArgStringList OriginalArgs;
Stephen Hinesef822542014-07-21 00:47:37 -07004887 for (const auto &Arg : Args)
4888 Arg->render(Args, OriginalArgs);
Kevin Enderby567003e2011-12-22 19:31:58 +00004889
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00004890 SmallString<256> Flags;
Kevin Enderby567003e2011-12-22 19:31:58 +00004891 const char *Exec = getToolChain().getDriver().getClangProgramPath();
4892 Flags += Exec;
4893 for (unsigned i = 0, e = OriginalArgs.size(); i != e; ++i) {
Stephen Hines176edba2014-12-01 14:53:08 -08004894 SmallString<128> EscapedArg;
4895 EscapeSpacesAndBackslashes(OriginalArgs[i], EscapedArg);
Kevin Enderby567003e2011-12-22 19:31:58 +00004896 Flags += " ";
Stephen Hines176edba2014-12-01 14:53:08 -08004897 Flags += EscapedArg;
Kevin Enderby567003e2011-12-22 19:31:58 +00004898 }
4899 CmdArgs.push_back("-dwarf-debug-flags");
4900 CmdArgs.push_back(Args.MakeArgString(Flags.str()));
4901 }
Daniel Dunbar20a9aa52010-05-20 21:30:13 +00004902
4903 // FIXME: Add -static support, once we have it.
4904
Stephen Hines651f13c2014-04-23 16:59:28 -07004905 // Consume all the warning flags. Usually this would be handled more
4906 // gracefully by -cc1 (warning about unknown warning flags, etc) but -cc1as
4907 // doesn't handle that so rather than warning about unused flags that are
4908 // actually used, we'll lie by omission instead.
4909 // FIXME: Stop lying and consume only the appropriate driver flags
4910 for (arg_iterator it = Args.filtered_begin(options::OPT_W_Group),
4911 ie = Args.filtered_end();
4912 it != ie; ++it)
4913 (*it)->claim();
4914
David Blaikie73168db2013-07-25 21:19:01 +00004915 CollectArgsForIntegratedAssembler(C, Args, CmdArgs,
4916 getToolChain().getDriver());
4917
Daniel Dunbar3df23252011-04-29 17:53:18 +00004918 Args.AddAllArgs(CmdArgs, options::OPT_mllvm);
Daniel Dunbar20a9aa52010-05-20 21:30:13 +00004919
4920 assert(Output.isFilename() && "Unexpected lipo output.");
4921 CmdArgs.push_back("-o");
4922 CmdArgs.push_back(Output.getFilename());
4923
Daniel Dunbar7c1e4652010-08-02 02:38:21 +00004924 assert(Input.isFilename() && "Invalid input.");
4925 CmdArgs.push_back(Input.getFilename());
Daniel Dunbar20a9aa52010-05-20 21:30:13 +00004926
Daniel Dunbara001c1c2010-07-18 21:16:15 +00004927 const char *Exec = getToolChain().getDriver().getClangProgramPath();
Stephen Hines176edba2014-12-01 14:53:08 -08004928 C.addCommand(llvm::make_unique<Command>(JA, *this, Exec, CmdArgs));
Eric Christopher708d72a2013-04-10 21:30:40 +00004929
4930 // Handle the debug info splitting at object creation time if we're
4931 // creating an object.
4932 // TODO: Currently only works on linux with newer objcopy.
4933 if (Args.hasArg(options::OPT_gsplit_dwarf) &&
Cameron Esfahani57b1da12013-09-14 01:09:11 +00004934 getToolChain().getTriple().isOSLinux())
Eric Christopher708d72a2013-04-10 21:30:40 +00004935 SplitDebugInfo(getToolChain(), C, *this, JA, Args, Output,
4936 SplitDebugName(Args, Inputs));
Daniel Dunbar20a9aa52010-05-20 21:30:13 +00004937}
4938
Stephen Hines176edba2014-12-01 14:53:08 -08004939void GnuTool::anchor() {}
4940
Daniel Dunbarb488c1d2009-03-18 08:07:30 +00004941void gcc::Common::ConstructJob(Compilation &C, const JobAction &JA,
Daniel Dunbarb488c1d2009-03-18 08:07:30 +00004942 const InputInfo &Output,
4943 const InputInfoList &Inputs,
Daniel Dunbar1d460332009-03-18 10:01:51 +00004944 const ArgList &Args,
Daniel Dunbarb488c1d2009-03-18 08:07:30 +00004945 const char *LinkingOutput) const {
Daniel Dunbaree788e72009-12-21 18:54:17 +00004946 const Driver &D = getToolChain().getDriver();
Daniel Dunbarb488c1d2009-03-18 08:07:30 +00004947 ArgStringList CmdArgs;
Daniel Dunbar47ac7d22009-03-18 06:00:36 +00004948
Stephen Hinesef822542014-07-21 00:47:37 -07004949 for (const auto &A : Args) {
Michael J. Spencer91e06da2012-10-19 22:37:06 +00004950 if (forwardToGCC(A->getOption())) {
Daniel Dunbar2dffe2d2010-08-03 16:14:14 +00004951 // Don't forward any -g arguments to assembly steps.
4952 if (isa<AssembleJobAction>(JA) &&
4953 A->getOption().matches(options::OPT_g_Group))
4954 continue;
4955
NAKAMURA Takumi3c6c8222013-08-19 11:51:51 +00004956 // Don't forward any -W arguments to assembly and link steps.
4957 if ((isa<AssembleJobAction>(JA) || isa<LinkJobAction>(JA)) &&
4958 A->getOption().matches(options::OPT_W_Group))
4959 continue;
4960
Daniel Dunbar75877192009-03-19 07:55:12 +00004961 // It is unfortunate that we have to claim here, as this means
4962 // we will basically never report anything interesting for
Daniel Dunbar6ecc7a92009-05-02 21:41:52 +00004963 // platforms using a generic gcc, even if we are just using gcc
4964 // to get to the assembler.
Daniel Dunbar75877192009-03-19 07:55:12 +00004965 A->claim();
Daniel Dunbar1d460332009-03-18 10:01:51 +00004966 A->render(Args, CmdArgs);
Daniel Dunbar75877192009-03-19 07:55:12 +00004967 }
Daniel Dunbarb488c1d2009-03-18 08:07:30 +00004968 }
Daniel Dunbarc21c4852009-04-08 23:54:23 +00004969
Daniel Dunbar82b51cc2010-01-25 22:35:08 +00004970 RenderExtraToolArgs(JA, CmdArgs);
Daniel Dunbarb488c1d2009-03-18 08:07:30 +00004971
4972 // If using a driver driver, force the arch.
Bob Wilson905c45f2011-10-14 05:03:44 +00004973 if (getToolChain().getTriple().isOSDarwin()) {
Daniel Dunbarb488c1d2009-03-18 08:07:30 +00004974 CmdArgs.push_back("-arch");
Stephen Hines176edba2014-12-01 14:53:08 -08004975 CmdArgs.push_back(
4976 Args.MakeArgString(getToolChain().getDefaultUniversalArchName()));
Daniel Dunbarb488c1d2009-03-18 08:07:30 +00004977 }
4978
Daniel Dunbar6ecc7a92009-05-02 21:41:52 +00004979 // Try to force gcc to match the tool chain we want, if we recognize
4980 // the arch.
Daniel Dunbar7cfe31a2009-05-22 02:21:04 +00004981 //
4982 // FIXME: The triple class should directly provide the information we want
4983 // here.
Stephen Hines176edba2014-12-01 14:53:08 -08004984 llvm::Triple::ArchType Arch = getToolChain().getArch();
Rafael Espindola64f7ad92012-10-07 04:44:33 +00004985 if (Arch == llvm::Triple::x86 || Arch == llvm::Triple::ppc)
Daniel Dunbar6ecc7a92009-05-02 21:41:52 +00004986 CmdArgs.push_back("-m32");
Bill Schmidtea7fb0c2013-07-26 01:36:11 +00004987 else if (Arch == llvm::Triple::x86_64 || Arch == llvm::Triple::ppc64 ||
4988 Arch == llvm::Triple::ppc64le)
Daniel Dunbar6ecc7a92009-05-02 21:41:52 +00004989 CmdArgs.push_back("-m64");
4990
Daniel Dunbar7c1e4652010-08-02 02:38:21 +00004991 if (Output.isFilename()) {
Daniel Dunbarb488c1d2009-03-18 08:07:30 +00004992 CmdArgs.push_back("-o");
Daniel Dunbar115a7922009-03-19 07:29:38 +00004993 CmdArgs.push_back(Output.getFilename());
4994 } else {
4995 assert(Output.isNothing() && "Unexpected output");
Daniel Dunbarb488c1d2009-03-18 08:07:30 +00004996 CmdArgs.push_back("-fsyntax-only");
Daniel Dunbar115a7922009-03-19 07:29:38 +00004997 }
Daniel Dunbarb488c1d2009-03-18 08:07:30 +00004998
Tony Linthicum96319392011-12-12 21:14:55 +00004999 Args.AddAllArgValues(CmdArgs, options::OPT_Wa_COMMA,
5000 options::OPT_Xassembler);
Daniel Dunbarb488c1d2009-03-18 08:07:30 +00005001
5002 // Only pass -x if gcc will understand it; otherwise hope gcc
5003 // understands the suffix correctly. The main use case this would go
5004 // wrong in is for linker inputs if they happened to have an odd
5005 // suffix; really the only way to get this to happen is a command
5006 // like '-x foobar a.c' which will treat a.c like a linker input.
5007 //
5008 // FIXME: For the linker case specifically, can we safely convert
5009 // inputs into '-Wl,' options?
Stephen Hinesef822542014-07-21 00:47:37 -07005010 for (const auto &II : Inputs) {
Daniel Dunbar5915fbf2009-09-01 16:57:46 +00005011 // Don't try to pass LLVM or AST inputs to a generic gcc.
Daniel Dunbar6c6424b2010-06-07 23:28:45 +00005012 if (II.getType() == types::TY_LLVM_IR || II.getType() == types::TY_LTO_IR ||
5013 II.getType() == types::TY_LLVM_BC || II.getType() == types::TY_LTO_BC)
Chris Lattner5f9e2722011-07-23 10:55:15 +00005014 D.Diag(diag::err_drv_no_linker_llvm_support)
Daniel Dunbar88137642009-09-09 22:32:48 +00005015 << getToolChain().getTripleString();
Daniel Dunbar5915fbf2009-09-01 16:57:46 +00005016 else if (II.getType() == types::TY_AST)
Chris Lattner5f9e2722011-07-23 10:55:15 +00005017 D.Diag(diag::err_drv_no_ast_support)
Daniel Dunbar88137642009-09-09 22:32:48 +00005018 << getToolChain().getTripleString();
Douglas Gregorc544ba02013-03-27 16:47:18 +00005019 else if (II.getType() == types::TY_ModuleFile)
5020 D.Diag(diag::err_drv_no_module_support)
5021 << getToolChain().getTripleString();
Daniel Dunbara8304f62009-05-02 20:14:53 +00005022
Daniel Dunbarb488c1d2009-03-18 08:07:30 +00005023 if (types::canTypeBeUserSpecified(II.getType())) {
5024 CmdArgs.push_back("-x");
5025 CmdArgs.push_back(types::getTypeName(II.getType()));
5026 }
5027
Daniel Dunbar7c1e4652010-08-02 02:38:21 +00005028 if (II.isFilename())
Daniel Dunbar115a7922009-03-19 07:29:38 +00005029 CmdArgs.push_back(II.getFilename());
Daniel Dunbar48f99942010-09-25 18:10:05 +00005030 else {
5031 const Arg &A = II.getInputArg();
5032
5033 // Reverse translate some rewritten options.
5034 if (A.getOption().matches(options::OPT_Z_reserved_lib_stdcxx)) {
5035 CmdArgs.push_back("-lstdc++");
5036 continue;
5037 }
5038
Daniel Dunbar115a7922009-03-19 07:29:38 +00005039 // Don't render as input, we need gcc to do the translations.
Daniel Dunbar48f99942010-09-25 18:10:05 +00005040 A.render(Args, CmdArgs);
5041 }
Daniel Dunbarb488c1d2009-03-18 08:07:30 +00005042 }
5043
Dylan Noblesmithb8a3e812011-04-09 13:31:59 +00005044 const std::string customGCCName = D.getCCCGenericGCCName();
5045 const char *GCCName;
5046 if (!customGCCName.empty())
5047 GCCName = customGCCName.c_str();
Hans Wennborg76b86c22013-07-18 20:29:38 +00005048 else if (D.CCCIsCXX()) {
Dylan Noblesmithb8a3e812011-04-09 13:31:59 +00005049 GCCName = "g++";
Dylan Noblesmithb8a3e812011-04-09 13:31:59 +00005050 } else
5051 GCCName = "gcc";
5052
Daniel Dunbarc21c4852009-04-08 23:54:23 +00005053 const char *Exec =
Daniel Dunbar4a7e8892010-07-14 18:46:23 +00005054 Args.MakeArgString(getToolChain().GetProgramPath(GCCName));
Stephen Hines176edba2014-12-01 14:53:08 -08005055 C.addCommand(llvm::make_unique<Command>(JA, *this, Exec, CmdArgs));
Daniel Dunbar47ac7d22009-03-18 06:00:36 +00005056}
5057
Daniel Dunbar82b51cc2010-01-25 22:35:08 +00005058void gcc::Preprocess::RenderExtraToolArgs(const JobAction &JA,
5059 ArgStringList &CmdArgs) const {
Daniel Dunbarb488c1d2009-03-18 08:07:30 +00005060 CmdArgs.push_back("-E");
Daniel Dunbar47ac7d22009-03-18 06:00:36 +00005061}
5062
Daniel Dunbar82b51cc2010-01-25 22:35:08 +00005063void gcc::Compile::RenderExtraToolArgs(const JobAction &JA,
5064 ArgStringList &CmdArgs) const {
Daniel Dunbar64952502010-02-11 03:16:21 +00005065 const Driver &D = getToolChain().getDriver();
5066
Daniel Dunbar82b51cc2010-01-25 22:35:08 +00005067 // If -flto, etc. are present then make sure not to force assembly output.
Daniel Dunbar6c6424b2010-06-07 23:28:45 +00005068 if (JA.getType() == types::TY_LLVM_IR || JA.getType() == types::TY_LTO_IR ||
5069 JA.getType() == types::TY_LLVM_BC || JA.getType() == types::TY_LTO_BC)
Daniel Dunbar82b51cc2010-01-25 22:35:08 +00005070 CmdArgs.push_back("-c");
Daniel Dunbar64952502010-02-11 03:16:21 +00005071 else {
5072 if (JA.getType() != types::TY_PP_Asm)
Chris Lattner5f9e2722011-07-23 10:55:15 +00005073 D.Diag(diag::err_drv_invalid_gcc_output_type)
Daniel Dunbar64952502010-02-11 03:16:21 +00005074 << getTypeName(JA.getType());
Michael J. Spencer20249a12010-10-21 03:16:25 +00005075
Daniel Dunbar82b51cc2010-01-25 22:35:08 +00005076 CmdArgs.push_back("-S");
Daniel Dunbar64952502010-02-11 03:16:21 +00005077 }
Daniel Dunbar47ac7d22009-03-18 06:00:36 +00005078}
5079
Daniel Dunbar82b51cc2010-01-25 22:35:08 +00005080void gcc::Link::RenderExtraToolArgs(const JobAction &JA,
5081 ArgStringList &CmdArgs) const {
Daniel Dunbarb488c1d2009-03-18 08:07:30 +00005082 // The types are (hopefully) good enough.
5083}
5084
Tony Linthicum96319392011-12-12 21:14:55 +00005085// Hexagon tools start.
5086void hexagon::Assemble::RenderExtraToolArgs(const JobAction &JA,
5087 ArgStringList &CmdArgs) const {
5088
5089}
5090void hexagon::Assemble::ConstructJob(Compilation &C, const JobAction &JA,
5091 const InputInfo &Output,
5092 const InputInfoList &Inputs,
5093 const ArgList &Args,
5094 const char *LinkingOutput) const {
5095
5096 const Driver &D = getToolChain().getDriver();
5097 ArgStringList CmdArgs;
5098
5099 std::string MarchString = "-march=";
Matthew Curtis67814152012-12-06 14:16:43 +00005100 MarchString += toolchains::Hexagon_TC::GetTargetCPU(Args);
Tony Linthicum96319392011-12-12 21:14:55 +00005101 CmdArgs.push_back(Args.MakeArgString(MarchString));
5102
5103 RenderExtraToolArgs(JA, CmdArgs);
5104
5105 if (Output.isFilename()) {
5106 CmdArgs.push_back("-o");
5107 CmdArgs.push_back(Output.getFilename());
5108 } else {
5109 assert(Output.isNothing() && "Unexpected output");
5110 CmdArgs.push_back("-fsyntax-only");
5111 }
5112
Matthew Curtis33c95f12012-12-06 17:49:03 +00005113 std::string SmallDataThreshold = GetHexagonSmallDataThresholdValue(Args);
5114 if (!SmallDataThreshold.empty())
5115 CmdArgs.push_back(
5116 Args.MakeArgString(std::string("-G") + SmallDataThreshold));
Tony Linthicum96319392011-12-12 21:14:55 +00005117
Matthew Curtis3d8d4222012-12-07 17:23:04 +00005118 Args.AddAllArgValues(CmdArgs, options::OPT_Wa_COMMA,
5119 options::OPT_Xassembler);
5120
Tony Linthicum96319392011-12-12 21:14:55 +00005121 // Only pass -x if gcc will understand it; otherwise hope gcc
5122 // understands the suffix correctly. The main use case this would go
5123 // wrong in is for linker inputs if they happened to have an odd
5124 // suffix; really the only way to get this to happen is a command
5125 // like '-x foobar a.c' which will treat a.c like a linker input.
5126 //
5127 // FIXME: For the linker case specifically, can we safely convert
5128 // inputs into '-Wl,' options?
Stephen Hinesef822542014-07-21 00:47:37 -07005129 for (const auto &II : Inputs) {
Tony Linthicum96319392011-12-12 21:14:55 +00005130 // Don't try to pass LLVM or AST inputs to a generic gcc.
5131 if (II.getType() == types::TY_LLVM_IR || II.getType() == types::TY_LTO_IR ||
5132 II.getType() == types::TY_LLVM_BC || II.getType() == types::TY_LTO_BC)
5133 D.Diag(clang::diag::err_drv_no_linker_llvm_support)
5134 << getToolChain().getTripleString();
5135 else if (II.getType() == types::TY_AST)
5136 D.Diag(clang::diag::err_drv_no_ast_support)
5137 << getToolChain().getTripleString();
Douglas Gregorc544ba02013-03-27 16:47:18 +00005138 else if (II.getType() == types::TY_ModuleFile)
5139 D.Diag(diag::err_drv_no_module_support)
5140 << getToolChain().getTripleString();
Tony Linthicum96319392011-12-12 21:14:55 +00005141
5142 if (II.isFilename())
5143 CmdArgs.push_back(II.getFilename());
5144 else
5145 // Don't render as input, we need gcc to do the translations. FIXME: Pranav: What is this ?
5146 II.getInputArg().render(Args, CmdArgs);
5147 }
5148
5149 const char *GCCName = "hexagon-as";
Stephen Hinesef822542014-07-21 00:47:37 -07005150 const char *Exec = Args.MakeArgString(getToolChain().GetProgramPath(GCCName));
Stephen Hines176edba2014-12-01 14:53:08 -08005151 C.addCommand(llvm::make_unique<Command>(JA, *this, Exec, CmdArgs));
Tony Linthicum96319392011-12-12 21:14:55 +00005152}
Stephen Hinesef822542014-07-21 00:47:37 -07005153
Tony Linthicum96319392011-12-12 21:14:55 +00005154void hexagon::Link::RenderExtraToolArgs(const JobAction &JA,
5155 ArgStringList &CmdArgs) const {
5156 // The types are (hopefully) good enough.
5157}
5158
5159void hexagon::Link::ConstructJob(Compilation &C, const JobAction &JA,
5160 const InputInfo &Output,
5161 const InputInfoList &Inputs,
5162 const ArgList &Args,
5163 const char *LinkingOutput) const {
5164
Matthew Curtis5fdf3502012-12-06 15:46:07 +00005165 const toolchains::Hexagon_TC& ToolChain =
5166 static_cast<const toolchains::Hexagon_TC&>(getToolChain());
5167 const Driver &D = ToolChain.getDriver();
5168
Tony Linthicum96319392011-12-12 21:14:55 +00005169 ArgStringList CmdArgs;
5170
Matthew Curtis5fdf3502012-12-06 15:46:07 +00005171 //----------------------------------------------------------------------------
5172 //
5173 //----------------------------------------------------------------------------
5174 bool hasStaticArg = Args.hasArg(options::OPT_static);
5175 bool buildingLib = Args.hasArg(options::OPT_shared);
Matthew Curtis33c95f12012-12-06 17:49:03 +00005176 bool buildPIE = Args.hasArg(options::OPT_pie);
Matthew Curtis5fdf3502012-12-06 15:46:07 +00005177 bool incStdLib = !Args.hasArg(options::OPT_nostdlib);
5178 bool incStartFiles = !Args.hasArg(options::OPT_nostartfiles);
5179 bool incDefLibs = !Args.hasArg(options::OPT_nodefaultlibs);
5180 bool useShared = buildingLib && !hasStaticArg;
Tony Linthicum96319392011-12-12 21:14:55 +00005181
Matthew Curtis5fdf3502012-12-06 15:46:07 +00005182 //----------------------------------------------------------------------------
5183 // Silence warnings for various options
5184 //----------------------------------------------------------------------------
Tony Linthicum96319392011-12-12 21:14:55 +00005185
Matthew Curtis5fdf3502012-12-06 15:46:07 +00005186 Args.ClaimAllArgs(options::OPT_g_Group);
5187 Args.ClaimAllArgs(options::OPT_emit_llvm);
5188 Args.ClaimAllArgs(options::OPT_w); // Other warning options are already
5189 // handled somewhere else.
5190 Args.ClaimAllArgs(options::OPT_static_libgcc);
5191
5192 //----------------------------------------------------------------------------
5193 //
5194 //----------------------------------------------------------------------------
Stephen Hinesef822542014-07-21 00:47:37 -07005195 for (const auto &Opt : ToolChain.ExtraOpts)
5196 CmdArgs.push_back(Opt.c_str());
Tony Linthicum96319392011-12-12 21:14:55 +00005197
Matthew Curtis67814152012-12-06 14:16:43 +00005198 std::string MarchString = toolchains::Hexagon_TC::GetTargetCPU(Args);
5199 CmdArgs.push_back(Args.MakeArgString("-m" + MarchString));
Sebastian Pop43115d42012-01-13 20:37:10 +00005200
Matthew Curtis5fdf3502012-12-06 15:46:07 +00005201 if (buildingLib) {
5202 CmdArgs.push_back("-shared");
5203 CmdArgs.push_back("-call_shared"); // should be the default, but doing as
5204 // hexagon-gcc does
Tony Linthicum96319392011-12-12 21:14:55 +00005205 }
5206
Matthew Curtis5fdf3502012-12-06 15:46:07 +00005207 if (hasStaticArg)
5208 CmdArgs.push_back("-static");
Tony Linthicum96319392011-12-12 21:14:55 +00005209
Matthew Curtis33c95f12012-12-06 17:49:03 +00005210 if (buildPIE && !buildingLib)
5211 CmdArgs.push_back("-pie");
5212
5213 std::string SmallDataThreshold = GetHexagonSmallDataThresholdValue(Args);
5214 if (!SmallDataThreshold.empty()) {
5215 CmdArgs.push_back(
5216 Args.MakeArgString(std::string("-G") + SmallDataThreshold));
5217 }
5218
Matthew Curtis5fdf3502012-12-06 15:46:07 +00005219 //----------------------------------------------------------------------------
5220 //
5221 //----------------------------------------------------------------------------
5222 CmdArgs.push_back("-o");
5223 CmdArgs.push_back(Output.getFilename());
Tony Linthicum96319392011-12-12 21:14:55 +00005224
Matthew Curtis5fdf3502012-12-06 15:46:07 +00005225 const std::string MarchSuffix = "/" + MarchString;
5226 const std::string G0Suffix = "/G0";
5227 const std::string MarchG0Suffix = MarchSuffix + G0Suffix;
Stephen Hines176edba2014-12-01 14:53:08 -08005228 const std::string RootDir =
5229 toolchains::Hexagon_TC::GetGnuDir(D.InstalledDir, Args) + "/";
Matthew Curtis5fdf3502012-12-06 15:46:07 +00005230 const std::string StartFilesDir = RootDir
5231 + "hexagon/lib"
5232 + (buildingLib
5233 ? MarchG0Suffix : MarchSuffix);
5234
5235 //----------------------------------------------------------------------------
5236 // moslib
5237 //----------------------------------------------------------------------------
5238 std::vector<std::string> oslibs;
5239 bool hasStandalone= false;
5240
5241 for (arg_iterator it = Args.filtered_begin(options::OPT_moslib_EQ),
5242 ie = Args.filtered_end(); it != ie; ++it) {
5243 (*it)->claim();
5244 oslibs.push_back((*it)->getValue());
5245 hasStandalone = hasStandalone || (oslibs.back() == "standalone");
Tony Linthicum96319392011-12-12 21:14:55 +00005246 }
Matthew Curtis5fdf3502012-12-06 15:46:07 +00005247 if (oslibs.empty()) {
5248 oslibs.push_back("standalone");
5249 hasStandalone = true;
5250 }
Tony Linthicum96319392011-12-12 21:14:55 +00005251
Matthew Curtis5fdf3502012-12-06 15:46:07 +00005252 //----------------------------------------------------------------------------
5253 // Start Files
5254 //----------------------------------------------------------------------------
5255 if (incStdLib && incStartFiles) {
5256
5257 if (!buildingLib) {
5258 if (hasStandalone) {
5259 CmdArgs.push_back(
5260 Args.MakeArgString(StartFilesDir + "/crt0_standalone.o"));
5261 }
5262 CmdArgs.push_back(Args.MakeArgString(StartFilesDir + "/crt0.o"));
5263 }
5264 std::string initObj = useShared ? "/initS.o" : "/init.o";
5265 CmdArgs.push_back(Args.MakeArgString(StartFilesDir + initObj));
5266 }
5267
5268 //----------------------------------------------------------------------------
5269 // Library Search Paths
5270 //----------------------------------------------------------------------------
5271 const ToolChain::path_list &LibPaths = ToolChain.getFilePaths();
Stephen Hinesef822542014-07-21 00:47:37 -07005272 for (const auto &LibPath : LibPaths)
5273 CmdArgs.push_back(Args.MakeArgString(StringRef("-L") + LibPath));
Matthew Curtis5fdf3502012-12-06 15:46:07 +00005274
5275 //----------------------------------------------------------------------------
5276 //
5277 //----------------------------------------------------------------------------
5278 Args.AddAllArgs(CmdArgs, options::OPT_T_Group);
5279 Args.AddAllArgs(CmdArgs, options::OPT_e);
5280 Args.AddAllArgs(CmdArgs, options::OPT_s);
5281 Args.AddAllArgs(CmdArgs, options::OPT_t);
5282 Args.AddAllArgs(CmdArgs, options::OPT_u_Group);
5283
5284 AddLinkerInputs(ToolChain, Inputs, Args, CmdArgs);
5285
5286 //----------------------------------------------------------------------------
5287 // Libraries
5288 //----------------------------------------------------------------------------
5289 if (incStdLib && incDefLibs) {
Hans Wennborg76b86c22013-07-18 20:29:38 +00005290 if (D.CCCIsCXX()) {
Matthew Curtis5fdf3502012-12-06 15:46:07 +00005291 ToolChain.AddCXXStdlibLibArgs(Args, CmdArgs);
5292 CmdArgs.push_back("-lm");
5293 }
5294
5295 CmdArgs.push_back("--start-group");
5296
5297 if (!buildingLib) {
5298 for(std::vector<std::string>::iterator i = oslibs.begin(),
5299 e = oslibs.end(); i != e; ++i)
5300 CmdArgs.push_back(Args.MakeArgString("-l" + *i));
5301 CmdArgs.push_back("-lc");
5302 }
5303 CmdArgs.push_back("-lgcc");
5304
5305 CmdArgs.push_back("--end-group");
5306 }
5307
5308 //----------------------------------------------------------------------------
5309 // End files
5310 //----------------------------------------------------------------------------
5311 if (incStdLib && incStartFiles) {
5312 std::string finiObj = useShared ? "/finiS.o" : "/fini.o";
5313 CmdArgs.push_back(Args.MakeArgString(StartFilesDir + finiObj));
5314 }
5315
5316 std::string Linker = ToolChain.GetProgramPath("hexagon-ld");
Stephen Hines176edba2014-12-01 14:53:08 -08005317 C.addCommand(llvm::make_unique<Command>(JA, *this, Args.MakeArgString(Linker),
5318 CmdArgs));
Tony Linthicum96319392011-12-12 21:14:55 +00005319}
5320// Hexagon tools end.
5321
Stephen Hines176edba2014-12-01 14:53:08 -08005322/// Get the (LLVM) name of the minimum ARM CPU for the arch we are targeting.
Stephen Hines651f13c2014-04-23 16:59:28 -07005323const char *arm::getARMCPUForMArch(const ArgList &Args,
5324 const llvm::Triple &Triple) {
5325 StringRef MArch;
5326 if (Arg *A = Args.getLastArg(options::OPT_march_EQ)) {
5327 // Otherwise, if we have -march= choose the base CPU for that arch.
5328 MArch = A->getValue();
5329 } else {
5330 // Otherwise, use the Arch from the triple.
5331 MArch = Triple.getArchName();
5332 }
5333
5334 // Handle -march=native.
5335 if (MArch == "native") {
5336 std::string CPU = llvm::sys::getHostCPUName();
5337 if (CPU != "generic") {
5338 // Translate the native cpu into the architecture. The switch below will
5339 // then chose the minimum cpu for that arch.
5340 MArch = std::string("arm") + arm::getLLVMArchSuffixForARM(CPU);
5341 }
5342 }
5343
Stephen Hines176edba2014-12-01 14:53:08 -08005344 return Triple.getARMCPUForArch(MArch);
Stephen Hines651f13c2014-04-23 16:59:28 -07005345}
5346
5347/// getARMTargetCPU - Get the (LLVM) name of the ARM cpu we are targeting.
5348StringRef arm::getARMTargetCPU(const ArgList &Args,
5349 const llvm::Triple &Triple) {
5350 // FIXME: Warn on inconsistent use of -mcpu and -march.
5351 // If we have -mcpu=, use that.
5352 if (Arg *A = Args.getLastArg(options::OPT_mcpu_EQ)) {
5353 StringRef MCPU = A->getValue();
5354 // Handle -mcpu=native.
5355 if (MCPU == "native")
5356 return llvm::sys::getHostCPUName();
5357 else
5358 return MCPU;
5359 }
5360
5361 return getARMCPUForMArch(Args, Triple);
5362}
5363
5364/// getLLVMArchSuffixForARM - Get the LLVM arch name to use for a particular
5365/// CPU.
5366//
5367// FIXME: This is redundant with -mcpu, why does LLVM use this.
5368// FIXME: tblgen this, or kill it!
5369const char *arm::getLLVMArchSuffixForARM(StringRef CPU) {
5370 return llvm::StringSwitch<const char *>(CPU)
5371 .Case("strongarm", "v4")
5372 .Cases("arm7tdmi", "arm7tdmi-s", "arm710t", "v4t")
5373 .Cases("arm720t", "arm9", "arm9tdmi", "v4t")
5374 .Cases("arm920", "arm920t", "arm922t", "v4t")
5375 .Cases("arm940t", "ep9312","v4t")
5376 .Cases("arm10tdmi", "arm1020t", "v5")
5377 .Cases("arm9e", "arm926ej-s", "arm946e-s", "v5e")
5378 .Cases("arm966e-s", "arm968e-s", "arm10e", "v5e")
5379 .Cases("arm1020e", "arm1022e", "xscale", "iwmmxt", "v5e")
5380 .Cases("arm1136j-s", "arm1136jf-s", "arm1176jz-s", "v6")
5381 .Cases("arm1176jzf-s", "mpcorenovfp", "mpcore", "v6")
5382 .Cases("arm1156t2-s", "arm1156t2f-s", "v6t2")
Stephen Hines176edba2014-12-01 14:53:08 -08005383 .Cases("cortex-a5", "cortex-a7", "cortex-a8", "v7")
5384 .Cases("cortex-a9", "cortex-a12", "cortex-a15", "cortex-a17", "krait", "v7")
Stephen Hines651f13c2014-04-23 16:59:28 -07005385 .Cases("cortex-r4", "cortex-r5", "v7r")
5386 .Case("cortex-m0", "v6m")
5387 .Case("cortex-m3", "v7m")
Stephen Hines176edba2014-12-01 14:53:08 -08005388 .Cases("cortex-m4", "cortex-m7", "v7em")
Stephen Hines651f13c2014-04-23 16:59:28 -07005389 .Case("swift", "v7s")
5390 .Case("cyclone", "v8")
5391 .Cases("cortex-a53", "cortex-a57", "v8")
5392 .Default("");
5393}
5394
5395bool mips::hasMipsAbiArg(const ArgList &Args, const char *Value) {
5396 Arg *A = Args.getLastArg(options::OPT_mabi_EQ);
5397 return A && (A->getValue() == StringRef(Value));
5398}
5399
Stephen Hines176edba2014-12-01 14:53:08 -08005400bool mips::isUCLibc(const ArgList &Args) {
5401 Arg *A = Args.getLastArg(options::OPT_m_libc_Group);
5402 return A && A->getOption().matches(options::OPT_muclibc);
5403}
5404
Stephen Hinesef822542014-07-21 00:47:37 -07005405bool mips::isNaN2008(const ArgList &Args, const llvm::Triple &Triple) {
5406 if (Arg *NaNArg = Args.getLastArg(options::OPT_mnan_EQ))
5407 return llvm::StringSwitch<bool>(NaNArg->getValue())
5408 .Case("2008", true)
5409 .Case("legacy", false)
5410 .Default(false);
5411
5412 // NaN2008 is the default for MIPS32r6/MIPS64r6.
5413 return llvm::StringSwitch<bool>(getCPUName(Args, Triple))
5414 .Cases("mips32r6", "mips64r6", true)
5415 .Default(false);
5416
5417 return false;
5418}
5419
Stephen Hines176edba2014-12-01 14:53:08 -08005420bool mips::isFPXXDefault(const llvm::Triple &Triple, StringRef CPUName,
5421 StringRef ABIName) {
5422 if (Triple.getVendor() != llvm::Triple::ImaginationTechnologies &&
5423 Triple.getVendor() != llvm::Triple::MipsTechnologies)
5424 return false;
5425
5426 if (ABIName != "32")
5427 return false;
5428
5429 return llvm::StringSwitch<bool>(CPUName)
5430 .Cases("mips2", "mips3", "mips4", "mips5", true)
5431 .Cases("mips32", "mips32r2", true)
5432 .Cases("mips64", "mips64r2", true)
5433 .Default(false);
5434}
5435
Stephen Hines651f13c2014-04-23 16:59:28 -07005436llvm::Triple::ArchType darwin::getArchTypeForMachOArchName(StringRef Str) {
Rafael Espindolacfed8282012-10-31 18:51:07 +00005437 // See arch(3) and llvm-gcc's driver-driver.c. We don't implement support for
5438 // archs which Darwin doesn't use.
5439
5440 // The matching this routine does is fairly pointless, since it is neither the
5441 // complete architecture list, nor a reasonable subset. The problem is that
5442 // historically the driver driver accepts this and also ties its -march=
5443 // handling to the architecture name, so we need to be careful before removing
5444 // support for it.
5445
5446 // This code must be kept in sync with Clang's Darwin specific argument
5447 // translation.
5448
5449 return llvm::StringSwitch<llvm::Triple::ArchType>(Str)
5450 .Cases("ppc", "ppc601", "ppc603", "ppc604", "ppc604e", llvm::Triple::ppc)
5451 .Cases("ppc750", "ppc7400", "ppc7450", "ppc970", llvm::Triple::ppc)
5452 .Case("ppc64", llvm::Triple::ppc64)
5453 .Cases("i386", "i486", "i486SX", "i586", "i686", llvm::Triple::x86)
5454 .Cases("pentium", "pentpro", "pentIIm3", "pentIIm5", "pentium4",
5455 llvm::Triple::x86)
Jim Grosbach32ca73e2013-11-16 00:53:35 +00005456 .Cases("x86_64", "x86_64h", llvm::Triple::x86_64)
Rafael Espindolacfed8282012-10-31 18:51:07 +00005457 // This is derived from the driver driver.
Bob Wilson2503ebd2013-03-04 22:37:49 +00005458 .Cases("arm", "armv4t", "armv5", "armv6", "armv6m", llvm::Triple::arm)
Stephen Hines651f13c2014-04-23 16:59:28 -07005459 .Cases("armv7", "armv7em", "armv7k", "armv7m", llvm::Triple::arm)
Bob Wilson2503ebd2013-03-04 22:37:49 +00005460 .Cases("armv7s", "xscale", llvm::Triple::arm)
Stephen Hines176edba2014-12-01 14:53:08 -08005461 .Case("arm64", llvm::Triple::aarch64)
Rafael Espindolacfed8282012-10-31 18:51:07 +00005462 .Case("r600", llvm::Triple::r600)
5463 .Case("nvptx", llvm::Triple::nvptx)
5464 .Case("nvptx64", llvm::Triple::nvptx64)
5465 .Case("amdil", llvm::Triple::amdil)
5466 .Case("spir", llvm::Triple::spir)
5467 .Default(llvm::Triple::UnknownArch);
5468}
Tony Linthicum96319392011-12-12 21:14:55 +00005469
Stephen Hines651f13c2014-04-23 16:59:28 -07005470void darwin::setTripleTypeForMachOArchName(llvm::Triple &T, StringRef Str) {
5471 llvm::Triple::ArchType Arch = getArchTypeForMachOArchName(Str);
5472 T.setArch(Arch);
5473
5474 if (Str == "x86_64h")
5475 T.setArchName(Str);
5476 else if (Str == "armv6m" || Str == "armv7m" || Str == "armv7em") {
5477 T.setOS(llvm::Triple::UnknownOS);
5478 T.setObjectFormat(llvm::Triple::MachO);
5479 }
5480}
5481
Bob Wilson66b8a662012-11-23 06:14:39 +00005482const char *Clang::getBaseInputName(const ArgList &Args,
5483 const InputInfoList &Inputs) {
Michael J. Spencer472ccff2010-12-18 00:19:12 +00005484 return Args.MakeArgString(
5485 llvm::sys::path::filename(Inputs[0].getBaseInput()));
Daniel Dunbara3ec60e2009-03-29 18:40:18 +00005486}
5487
Bob Wilson66b8a662012-11-23 06:14:39 +00005488const char *Clang::getBaseInputStem(const ArgList &Args,
5489 const InputInfoList &Inputs) {
Daniel Dunbara3ec60e2009-03-29 18:40:18 +00005490 const char *Str = getBaseInputName(Args, Inputs);
5491
Chris Lattner657ca662011-01-16 08:14:11 +00005492 if (const char *End = strrchr(Str, '.'))
Daniel Dunbar88137642009-09-09 22:32:48 +00005493 return Args.MakeArgString(std::string(Str, End));
Daniel Dunbara3ec60e2009-03-29 18:40:18 +00005494
5495 return Str;
5496}
5497
Bob Wilson66b8a662012-11-23 06:14:39 +00005498const char *Clang::getDependencyFileName(const ArgList &Args,
5499 const InputInfoList &Inputs) {
Daniel Dunbara3ec60e2009-03-29 18:40:18 +00005500 // FIXME: Think about this more.
5501 std::string Res;
5502
5503 if (Arg *OutputOpt = Args.getLastArg(options::OPT_o)) {
Richard Smith1d489cf2012-11-01 04:30:05 +00005504 std::string Str(OutputOpt->getValue());
Daniel Dunbara3ec60e2009-03-29 18:40:18 +00005505 Res = Str.substr(0, Str.rfind('.'));
Chad Rosier30601782011-08-17 23:08:45 +00005506 } else {
Bob Wilson66b8a662012-11-23 06:14:39 +00005507 Res = getBaseInputStem(Args, Inputs);
Chad Rosier30601782011-08-17 23:08:45 +00005508 }
Daniel Dunbar88137642009-09-09 22:32:48 +00005509 return Args.MakeArgString(Res + ".d");
Daniel Dunbara3ec60e2009-03-29 18:40:18 +00005510}
5511
Daniel Dunbar8cac5f72009-03-20 16:06:39 +00005512void darwin::Assemble::ConstructJob(Compilation &C, const JobAction &JA,
Daniel Dunbar2fe238e2010-08-02 02:38:28 +00005513 const InputInfo &Output,
Daniel Dunbarc21c4852009-04-08 23:54:23 +00005514 const InputInfoList &Inputs,
5515 const ArgList &Args,
Daniel Dunbar8cac5f72009-03-20 16:06:39 +00005516 const char *LinkingOutput) const {
5517 ArgStringList CmdArgs;
5518
5519 assert(Inputs.size() == 1 && "Unexpected number of inputs.");
5520 const InputInfo &Input = Inputs[0];
5521
Daniel Dunbar34bac1f2011-04-12 23:59:20 +00005522 // Determine the original source input.
5523 const Action *SourceAction = &JA;
5524 while (SourceAction->getKind() != Action::InputClass) {
5525 assert(!SourceAction->getInputs().empty() && "unexpected root action!");
5526 SourceAction = SourceAction->getInputs()[0];
5527 }
5528
Stephen Hines651f13c2014-04-23 16:59:28 -07005529 // If -fno_integrated_as is used add -Q to the darwin assember driver to make
Kevin Enderby6efcf6f2013-11-18 23:30:29 +00005530 // sure it runs its system assembler not clang's integrated assembler.
Stephen Hines651f13c2014-04-23 16:59:28 -07005531 // Applicable to darwin11+ and Xcode 4+. darwin<10 lacked integrated-as.
5532 // FIXME: at run-time detect assembler capabilities or rely on version
5533 // information forwarded by -target-assembler-version (future)
5534 if (Args.hasArg(options::OPT_fno_integrated_as)) {
5535 const llvm::Triple &T(getToolChain().getTriple());
5536 if (!(T.isMacOSX() && T.isMacOSXVersionLT(10, 7)))
5537 CmdArgs.push_back("-Q");
5538 }
Kevin Enderby6efcf6f2013-11-18 23:30:29 +00005539
Daniel Dunbar34bac1f2011-04-12 23:59:20 +00005540 // Forward -g, assuming we are dealing with an actual assembly file.
Eric Christopher88b7cf02011-08-19 00:30:14 +00005541 if (SourceAction->getType() == types::TY_Asm ||
Daniel Dunbar34bac1f2011-04-12 23:59:20 +00005542 SourceAction->getType() == types::TY_PP_Asm) {
Daniel Dunbar8e4fea62009-04-01 00:27:44 +00005543 if (Args.hasArg(options::OPT_gstabs))
5544 CmdArgs.push_back("--gstabs");
5545 else if (Args.hasArg(options::OPT_g_Group))
Bob Wilson591ff152011-11-02 05:10:45 +00005546 CmdArgs.push_back("-g");
Daniel Dunbar8e4fea62009-04-01 00:27:44 +00005547 }
Daniel Dunbarc21c4852009-04-08 23:54:23 +00005548
Daniel Dunbar8cac5f72009-03-20 16:06:39 +00005549 // Derived from asm spec.
Stephen Hines651f13c2014-04-23 16:59:28 -07005550 AddMachOArch(Args, CmdArgs);
Daniel Dunbar8cac5f72009-03-20 16:06:39 +00005551
Daniel Dunbarf5438e32010-07-22 01:47:22 +00005552 // Use -force_cpusubtype_ALL on x86 by default.
Eli Bendersky8f4269a2013-07-24 22:20:49 +00005553 if (getToolChain().getArch() == llvm::Triple::x86 ||
5554 getToolChain().getArch() == llvm::Triple::x86_64 ||
Daniel Dunbarcc6f8032009-09-09 18:36:27 +00005555 Args.hasArg(options::OPT_force__cpusubtype__ALL))
5556 CmdArgs.push_back("-force_cpusubtype_ALL");
5557
Eli Bendersky8f4269a2013-07-24 22:20:49 +00005558 if (getToolChain().getArch() != llvm::Triple::x86_64 &&
Daniel Dunbar7a0c0642012-10-15 22:23:53 +00005559 (((Args.hasArg(options::OPT_mkernel) ||
Eric Christopher59320e72013-02-21 22:35:01 +00005560 Args.hasArg(options::OPT_fapple_kext)) &&
Stephen Hines651f13c2014-04-23 16:59:28 -07005561 getMachOToolChain().isKernelStatic()) ||
Daniel Dunbar7a0c0642012-10-15 22:23:53 +00005562 Args.hasArg(options::OPT_static)))
Daniel Dunbarc21c4852009-04-08 23:54:23 +00005563 CmdArgs.push_back("-static");
5564
Daniel Dunbar8cac5f72009-03-20 16:06:39 +00005565 Args.AddAllArgValues(CmdArgs, options::OPT_Wa_COMMA,
5566 options::OPT_Xassembler);
5567
5568 assert(Output.isFilename() && "Unexpected lipo output.");
5569 CmdArgs.push_back("-o");
5570 CmdArgs.push_back(Output.getFilename());
5571
Daniel Dunbar7c1e4652010-08-02 02:38:21 +00005572 assert(Input.isFilename() && "Invalid input.");
5573 CmdArgs.push_back(Input.getFilename());
Daniel Dunbar8cac5f72009-03-20 16:06:39 +00005574
5575 // asm_final spec is empty.
5576
Daniel Dunbarc21c4852009-04-08 23:54:23 +00005577 const char *Exec =
Daniel Dunbar4a7e8892010-07-14 18:46:23 +00005578 Args.MakeArgString(getToolChain().GetProgramPath("as"));
Stephen Hines176edba2014-12-01 14:53:08 -08005579 C.addCommand(llvm::make_unique<Command>(JA, *this, Exec, CmdArgs));
Daniel Dunbar8cac5f72009-03-20 16:06:39 +00005580}
Daniel Dunbarff7488d2009-03-20 00:52:38 +00005581
Stephen Hines651f13c2014-04-23 16:59:28 -07005582void darwin::MachOTool::anchor() {}
David Blaikie99ba9e32011-12-20 02:48:34 +00005583
Stephen Hines651f13c2014-04-23 16:59:28 -07005584void darwin::MachOTool::AddMachOArch(const ArgList &Args,
5585 ArgStringList &CmdArgs) const {
5586 StringRef ArchName = getMachOToolChain().getMachOArchName(Args);
Daniel Dunbareeff4062010-01-22 02:04:58 +00005587
Daniel Dunbar02633b52009-03-26 16:23:12 +00005588 // Derived from darwin_arch spec.
5589 CmdArgs.push_back("-arch");
Daniel Dunbareeff4062010-01-22 02:04:58 +00005590 CmdArgs.push_back(Args.MakeArgString(ArchName));
Daniel Dunbar78dbd582009-09-04 18:35:31 +00005591
Daniel Dunbareeff4062010-01-22 02:04:58 +00005592 // FIXME: Is this needed anymore?
5593 if (ArchName == "arm")
Daniel Dunbar78dbd582009-09-04 18:35:31 +00005594 CmdArgs.push_back("-force_cpusubtype_ALL");
Daniel Dunbar02633b52009-03-26 16:23:12 +00005595}
5596
Bill Wendling6acf8b42012-10-02 18:02:50 +00005597bool darwin::Link::NeedsTempPath(const InputInfoList &Inputs) const {
5598 // We only need to generate a temp path for LTO if we aren't compiling object
5599 // files. When compiling source files, we run 'dsymutil' after linking. We
5600 // don't run 'dsymutil' when compiling object files.
Stephen Hinesef822542014-07-21 00:47:37 -07005601 for (const auto &Input : Inputs)
5602 if (Input.getType() != types::TY_Object)
Bill Wendling6acf8b42012-10-02 18:02:50 +00005603 return true;
5604
5605 return false;
5606}
5607
Daniel Dunbar748de8e2010-09-09 21:51:05 +00005608void darwin::Link::AddLinkArgs(Compilation &C,
5609 const ArgList &Args,
Bill Wendling6acf8b42012-10-02 18:02:50 +00005610 ArgStringList &CmdArgs,
5611 const InputInfoList &Inputs) const {
Daniel Dunbaree788e72009-12-21 18:54:17 +00005612 const Driver &D = getToolChain().getDriver();
Stephen Hines651f13c2014-04-23 16:59:28 -07005613 const toolchains::MachO &MachOTC = getMachOToolChain();
Daniel Dunbar02633b52009-03-26 16:23:12 +00005614
Daniel Dunbarb18dc5b2010-08-11 23:07:50 +00005615 unsigned Version[3] = { 0, 0, 0 };
5616 if (Arg *A = Args.getLastArg(options::OPT_mlinker_version_EQ)) {
5617 bool HadExtra;
Richard Smith1d489cf2012-11-01 04:30:05 +00005618 if (!Driver::GetReleaseVersion(A->getValue(), Version[0],
Daniel Dunbarb18dc5b2010-08-11 23:07:50 +00005619 Version[1], Version[2], HadExtra) ||
5620 HadExtra)
Chris Lattner5f9e2722011-07-23 10:55:15 +00005621 D.Diag(diag::err_drv_invalid_version_number)
Daniel Dunbarb18dc5b2010-08-11 23:07:50 +00005622 << A->getAsString(Args);
5623 }
5624
Stephen Hines651f13c2014-04-23 16:59:28 -07005625 // Newer linkers support -demangle. Pass it if supported and not disabled by
Daniel Dunbarb18dc5b2010-08-11 23:07:50 +00005626 // the user.
Stephen Hines651f13c2014-04-23 16:59:28 -07005627 if (Version[0] >= 100 && !Args.hasArg(options::OPT_Z_Xlinker__no_demangle))
5628 CmdArgs.push_back("-demangle");
Daniel Dunbarb18dc5b2010-08-11 23:07:50 +00005629
Bob Wilsonbd77c592013-08-02 22:25:34 +00005630 if (Args.hasArg(options::OPT_rdynamic) && Version[0] >= 137)
5631 CmdArgs.push_back("-export_dynamic");
5632
Bill Wendlingc35f9082012-11-16 23:03:00 +00005633 // If we are using LTO, then automatically create a temporary file path for
5634 // the linker to use, so that it's lifetime will extend past a possible
5635 // dsymutil step.
5636 if (Version[0] >= 116 && D.IsUsingLTO(Args) && NeedsTempPath(Inputs)) {
5637 const char *TmpPath = C.getArgs().MakeArgString(
5638 D.GetTemporaryPath("cc", types::getTypeTempSuffix(types::TY_Object)));
5639 C.addTempFile(TmpPath);
5640 CmdArgs.push_back("-object_path_lto");
5641 CmdArgs.push_back(TmpPath);
Daniel Dunbar5bfa6562011-06-21 20:55:11 +00005642 }
5643
Daniel Dunbar02633b52009-03-26 16:23:12 +00005644 // Derived from the "link" spec.
5645 Args.AddAllArgs(CmdArgs, options::OPT_static);
5646 if (!Args.hasArg(options::OPT_static))
5647 CmdArgs.push_back("-dynamic");
5648 if (Args.hasArg(options::OPT_fgnu_runtime)) {
5649 // FIXME: gcc replaces -lobjc in forward args with -lobjc-gnu
5650 // here. How do we wish to handle such things?
5651 }
Daniel Dunbarc21c4852009-04-08 23:54:23 +00005652
Daniel Dunbar02633b52009-03-26 16:23:12 +00005653 if (!Args.hasArg(options::OPT_dynamiclib)) {
Stephen Hines651f13c2014-04-23 16:59:28 -07005654 AddMachOArch(Args, CmdArgs);
Daniel Dunbara6d38492010-01-22 02:04:52 +00005655 // FIXME: Why do this only on this path?
Daniel Dunbar8917dd42010-01-22 03:37:33 +00005656 Args.AddLastArg(CmdArgs, options::OPT_force__cpusubtype__ALL);
Daniel Dunbar02633b52009-03-26 16:23:12 +00005657
5658 Args.AddLastArg(CmdArgs, options::OPT_bundle);
5659 Args.AddAllArgs(CmdArgs, options::OPT_bundle__loader);
5660 Args.AddAllArgs(CmdArgs, options::OPT_client__name);
5661
5662 Arg *A;
5663 if ((A = Args.getLastArg(options::OPT_compatibility__version)) ||
5664 (A = Args.getLastArg(options::OPT_current__version)) ||
5665 (A = Args.getLastArg(options::OPT_install__name)))
Chris Lattner5f9e2722011-07-23 10:55:15 +00005666 D.Diag(diag::err_drv_argument_only_allowed_with)
Daniel Dunbar02633b52009-03-26 16:23:12 +00005667 << A->getAsString(Args) << "-dynamiclib";
5668
5669 Args.AddLastArg(CmdArgs, options::OPT_force__flat__namespace);
5670 Args.AddLastArg(CmdArgs, options::OPT_keep__private__externs);
5671 Args.AddLastArg(CmdArgs, options::OPT_private__bundle);
5672 } else {
5673 CmdArgs.push_back("-dylib");
5674
5675 Arg *A;
5676 if ((A = Args.getLastArg(options::OPT_bundle)) ||
5677 (A = Args.getLastArg(options::OPT_bundle__loader)) ||
5678 (A = Args.getLastArg(options::OPT_client__name)) ||
5679 (A = Args.getLastArg(options::OPT_force__flat__namespace)) ||
5680 (A = Args.getLastArg(options::OPT_keep__private__externs)) ||
5681 (A = Args.getLastArg(options::OPT_private__bundle)))
Chris Lattner5f9e2722011-07-23 10:55:15 +00005682 D.Diag(diag::err_drv_argument_not_allowed_with)
Daniel Dunbar02633b52009-03-26 16:23:12 +00005683 << A->getAsString(Args) << "-dynamiclib";
Daniel Dunbarc21c4852009-04-08 23:54:23 +00005684
Daniel Dunbar02633b52009-03-26 16:23:12 +00005685 Args.AddAllArgsTranslated(CmdArgs, options::OPT_compatibility__version,
5686 "-dylib_compatibility_version");
5687 Args.AddAllArgsTranslated(CmdArgs, options::OPT_current__version,
5688 "-dylib_current_version");
5689
Stephen Hines651f13c2014-04-23 16:59:28 -07005690 AddMachOArch(Args, CmdArgs);
Daniel Dunbar02633b52009-03-26 16:23:12 +00005691
5692 Args.AddAllArgsTranslated(CmdArgs, options::OPT_install__name,
5693 "-dylib_install_name");
5694 }
5695
5696 Args.AddLastArg(CmdArgs, options::OPT_all__load);
5697 Args.AddAllArgs(CmdArgs, options::OPT_allowable__client);
5698 Args.AddLastArg(CmdArgs, options::OPT_bind__at__load);
Stephen Hines651f13c2014-04-23 16:59:28 -07005699 if (MachOTC.isTargetIOSBased())
Daniel Dunbard82f8fa2009-09-04 18:35:41 +00005700 Args.AddLastArg(CmdArgs, options::OPT_arch__errors__fatal);
Daniel Dunbar02633b52009-03-26 16:23:12 +00005701 Args.AddLastArg(CmdArgs, options::OPT_dead__strip);
5702 Args.AddLastArg(CmdArgs, options::OPT_no__dead__strip__inits__and__terms);
5703 Args.AddAllArgs(CmdArgs, options::OPT_dylib__file);
5704 Args.AddLastArg(CmdArgs, options::OPT_dynamic);
5705 Args.AddAllArgs(CmdArgs, options::OPT_exported__symbols__list);
5706 Args.AddLastArg(CmdArgs, options::OPT_flat__namespace);
Daniel Dunbar99ca47b2011-06-28 20:16:02 +00005707 Args.AddAllArgs(CmdArgs, options::OPT_force__load);
Daniel Dunbar02633b52009-03-26 16:23:12 +00005708 Args.AddAllArgs(CmdArgs, options::OPT_headerpad__max__install__names);
5709 Args.AddAllArgs(CmdArgs, options::OPT_image__base);
5710 Args.AddAllArgs(CmdArgs, options::OPT_init);
5711
Daniel Dunbarce911f52011-04-28 21:23:41 +00005712 // Add the deployment target.
Stephen Hines651f13c2014-04-23 16:59:28 -07005713 MachOTC.addMinVersionArgs(Args, CmdArgs);
Daniel Dunbarce911f52011-04-28 21:23:41 +00005714
Daniel Dunbar02633b52009-03-26 16:23:12 +00005715 Args.AddLastArg(CmdArgs, options::OPT_nomultidefs);
5716 Args.AddLastArg(CmdArgs, options::OPT_multi__module);
5717 Args.AddLastArg(CmdArgs, options::OPT_single__module);
5718 Args.AddAllArgs(CmdArgs, options::OPT_multiply__defined);
5719 Args.AddAllArgs(CmdArgs, options::OPT_multiply__defined__unused);
Daniel Dunbarc21c4852009-04-08 23:54:23 +00005720
Daniel Dunbar47e879d2010-07-13 23:31:40 +00005721 if (const Arg *A = Args.getLastArg(options::OPT_fpie, options::OPT_fPIE,
5722 options::OPT_fno_pie,
5723 options::OPT_fno_PIE)) {
5724 if (A->getOption().matches(options::OPT_fpie) ||
5725 A->getOption().matches(options::OPT_fPIE))
5726 CmdArgs.push_back("-pie");
5727 else
5728 CmdArgs.push_back("-no_pie");
5729 }
Daniel Dunbar02633b52009-03-26 16:23:12 +00005730
5731 Args.AddLastArg(CmdArgs, options::OPT_prebind);
5732 Args.AddLastArg(CmdArgs, options::OPT_noprebind);
5733 Args.AddLastArg(CmdArgs, options::OPT_nofixprebinding);
5734 Args.AddLastArg(CmdArgs, options::OPT_prebind__all__twolevel__modules);
5735 Args.AddLastArg(CmdArgs, options::OPT_read__only__relocs);
5736 Args.AddAllArgs(CmdArgs, options::OPT_sectcreate);
5737 Args.AddAllArgs(CmdArgs, options::OPT_sectorder);
5738 Args.AddAllArgs(CmdArgs, options::OPT_seg1addr);
5739 Args.AddAllArgs(CmdArgs, options::OPT_segprot);
5740 Args.AddAllArgs(CmdArgs, options::OPT_segaddr);
5741 Args.AddAllArgs(CmdArgs, options::OPT_segs__read__only__addr);
5742 Args.AddAllArgs(CmdArgs, options::OPT_segs__read__write__addr);
5743 Args.AddAllArgs(CmdArgs, options::OPT_seg__addr__table);
5744 Args.AddAllArgs(CmdArgs, options::OPT_seg__addr__table__filename);
5745 Args.AddAllArgs(CmdArgs, options::OPT_sub__library);
5746 Args.AddAllArgs(CmdArgs, options::OPT_sub__umbrella);
Daniel Dunbard82f8fa2009-09-04 18:35:41 +00005747
Daniel Dunbarcc957192011-05-02 21:03:47 +00005748 // Give --sysroot= preference, over the Apple specific behavior to also use
5749 // --isysroot as the syslibroot.
Sebastian Pop4762a2d2012-04-16 04:16:43 +00005750 StringRef sysroot = C.getSysRoot();
5751 if (sysroot != "") {
Daniel Dunbarcc957192011-05-02 21:03:47 +00005752 CmdArgs.push_back("-syslibroot");
Sebastian Pop4762a2d2012-04-16 04:16:43 +00005753 CmdArgs.push_back(C.getArgs().MakeArgString(sysroot));
Daniel Dunbarcc957192011-05-02 21:03:47 +00005754 } else if (const Arg *A = Args.getLastArg(options::OPT_isysroot)) {
5755 CmdArgs.push_back("-syslibroot");
Richard Smith1d489cf2012-11-01 04:30:05 +00005756 CmdArgs.push_back(A->getValue());
Daniel Dunbard82f8fa2009-09-04 18:35:41 +00005757 }
5758
Daniel Dunbar02633b52009-03-26 16:23:12 +00005759 Args.AddLastArg(CmdArgs, options::OPT_twolevel__namespace);
5760 Args.AddLastArg(CmdArgs, options::OPT_twolevel__namespace__hints);
5761 Args.AddAllArgs(CmdArgs, options::OPT_umbrella);
5762 Args.AddAllArgs(CmdArgs, options::OPT_undefined);
5763 Args.AddAllArgs(CmdArgs, options::OPT_unexported__symbols__list);
Daniel Dunbard82f8fa2009-09-04 18:35:41 +00005764 Args.AddAllArgs(CmdArgs, options::OPT_weak__reference__mismatches);
Daniel Dunbar02633b52009-03-26 16:23:12 +00005765 Args.AddLastArg(CmdArgs, options::OPT_X_Flag);
5766 Args.AddAllArgs(CmdArgs, options::OPT_y);
5767 Args.AddLastArg(CmdArgs, options::OPT_w);
5768 Args.AddAllArgs(CmdArgs, options::OPT_pagezero__size);
5769 Args.AddAllArgs(CmdArgs, options::OPT_segs__read__);
5770 Args.AddLastArg(CmdArgs, options::OPT_seglinkedit);
5771 Args.AddLastArg(CmdArgs, options::OPT_noseglinkedit);
5772 Args.AddAllArgs(CmdArgs, options::OPT_sectalign);
5773 Args.AddAllArgs(CmdArgs, options::OPT_sectobjectsymbols);
5774 Args.AddAllArgs(CmdArgs, options::OPT_segcreate);
5775 Args.AddLastArg(CmdArgs, options::OPT_whyload);
5776 Args.AddLastArg(CmdArgs, options::OPT_whatsloaded);
5777 Args.AddAllArgs(CmdArgs, options::OPT_dylinker__install__name);
5778 Args.AddLastArg(CmdArgs, options::OPT_dylinker);
5779 Args.AddLastArg(CmdArgs, options::OPT_Mach);
5780}
5781
Stephen Hines651f13c2014-04-23 16:59:28 -07005782enum LibOpenMP {
5783 LibUnknown,
5784 LibGOMP,
5785 LibIOMP5
5786};
5787
Daniel Dunbar02633b52009-03-26 16:23:12 +00005788void darwin::Link::ConstructJob(Compilation &C, const JobAction &JA,
Daniel Dunbar2fe238e2010-08-02 02:38:28 +00005789 const InputInfo &Output,
Daniel Dunbarc21c4852009-04-08 23:54:23 +00005790 const InputInfoList &Inputs,
5791 const ArgList &Args,
Daniel Dunbar02633b52009-03-26 16:23:12 +00005792 const char *LinkingOutput) const {
5793 assert(Output.getType() == types::TY_Image && "Invalid linker output type.");
Daniel Dunbare0be8b12009-09-08 16:39:16 +00005794
Stephen Hines176edba2014-12-01 14:53:08 -08005795 // If the number of arguments surpasses the system limits, we will encode the
5796 // input files in a separate file, shortening the command line. To this end,
5797 // build a list of input file names that can be passed via a file with the
5798 // -filelist linker option.
5799 llvm::opt::ArgStringList InputFileList;
5800
Daniel Dunbar02633b52009-03-26 16:23:12 +00005801 // The logic here is derived from gcc's behavior; most of which
5802 // comes from specs (starting with link_command). Consult gcc for
5803 // more information.
Daniel Dunbar02633b52009-03-26 16:23:12 +00005804 ArgStringList CmdArgs;
5805
Argyrios Kyrtzidis22897172011-10-07 22:58:08 +00005806 /// Hack(tm) to ignore linking errors when we are doing ARC migration.
5807 if (Args.hasArg(options::OPT_ccc_arcmt_check,
5808 options::OPT_ccc_arcmt_migrate)) {
Stephen Hinesef822542014-07-21 00:47:37 -07005809 for (const auto &Arg : Args)
5810 Arg->claim();
Argyrios Kyrtzidis22897172011-10-07 22:58:08 +00005811 const char *Exec =
5812 Args.MakeArgString(getToolChain().GetProgramPath("touch"));
5813 CmdArgs.push_back(Output.getFilename());
Stephen Hines176edba2014-12-01 14:53:08 -08005814 C.addCommand(llvm::make_unique<Command>(JA, *this, Exec, CmdArgs));
Argyrios Kyrtzidis22897172011-10-07 22:58:08 +00005815 return;
5816 }
5817
Daniel Dunbar02633b52009-03-26 16:23:12 +00005818 // I'm not sure why this particular decomposition exists in gcc, but
5819 // we follow suite for ease of comparison.
Bill Wendling6acf8b42012-10-02 18:02:50 +00005820 AddLinkArgs(C, Args, CmdArgs, Inputs);
Daniel Dunbar02633b52009-03-26 16:23:12 +00005821
Daniel Dunbar02633b52009-03-26 16:23:12 +00005822 Args.AddAllArgs(CmdArgs, options::OPT_d_Flag);
5823 Args.AddAllArgs(CmdArgs, options::OPT_s);
5824 Args.AddAllArgs(CmdArgs, options::OPT_t);
5825 Args.AddAllArgs(CmdArgs, options::OPT_Z_Flag);
5826 Args.AddAllArgs(CmdArgs, options::OPT_u_Group);
Daniel Dunbar02633b52009-03-26 16:23:12 +00005827 Args.AddLastArg(CmdArgs, options::OPT_e);
Daniel Dunbar02633b52009-03-26 16:23:12 +00005828 Args.AddAllArgs(CmdArgs, options::OPT_r);
5829
Daniel Dunbar270073c2010-10-18 22:08:36 +00005830 // Forward -ObjC when either -ObjC or -ObjC++ is used, to force loading
5831 // members of static archive libraries which implement Objective-C classes or
5832 // categories.
5833 if (Args.hasArg(options::OPT_ObjC) || Args.hasArg(options::OPT_ObjCXX))
5834 CmdArgs.push_back("-ObjC");
Michael J. Spencer20249a12010-10-21 03:16:25 +00005835
Daniel Dunbar02633b52009-03-26 16:23:12 +00005836 CmdArgs.push_back("-o");
5837 CmdArgs.push_back(Output.getFilename());
5838
Chad Rosier18937312012-05-16 23:45:12 +00005839 if (!Args.hasArg(options::OPT_nostdlib) &&
Stephen Hines651f13c2014-04-23 16:59:28 -07005840 !Args.hasArg(options::OPT_nostartfiles))
5841 getMachOToolChain().addStartObjectFileArgs(Args, CmdArgs);
Daniel Dunbar02633b52009-03-26 16:23:12 +00005842
5843 Args.AddAllArgs(CmdArgs, options::OPT_L);
Daniel Dunbarc21c4852009-04-08 23:54:23 +00005844
Stephen Hines651f13c2014-04-23 16:59:28 -07005845 LibOpenMP UsedOpenMPLib = LibUnknown;
5846 if (Args.hasArg(options::OPT_fopenmp)) {
5847 UsedOpenMPLib = LibGOMP;
5848 } else if (const Arg *A = Args.getLastArg(options::OPT_fopenmp_EQ)) {
5849 UsedOpenMPLib = llvm::StringSwitch<LibOpenMP>(A->getValue())
5850 .Case("libgomp", LibGOMP)
5851 .Case("libiomp5", LibIOMP5)
5852 .Default(LibUnknown);
5853 if (UsedOpenMPLib == LibUnknown)
5854 getToolChain().getDriver().Diag(diag::err_drv_unsupported_option_argument)
5855 << A->getOption().getName() << A->getValue();
5856 }
5857 switch (UsedOpenMPLib) {
5858 case LibGOMP:
Daniel Dunbar02633b52009-03-26 16:23:12 +00005859 CmdArgs.push_back("-lgomp");
Stephen Hines651f13c2014-04-23 16:59:28 -07005860 break;
5861 case LibIOMP5:
5862 CmdArgs.push_back("-liomp5");
5863 break;
5864 case LibUnknown:
5865 break;
5866 }
Daniel Dunbar02633b52009-03-26 16:23:12 +00005867
Douglas Gregor04e326b2012-05-15 21:00:27 +00005868 AddLinkerInputs(getToolChain(), Inputs, Args, CmdArgs);
Stephen Hines176edba2014-12-01 14:53:08 -08005869 // Build the input file for -filelist (list of linker input files) in case we
5870 // need it later
5871 for (const auto &II : Inputs) {
5872 if (!II.isFilename()) {
5873 // This is a linker input argument.
5874 // We cannot mix input arguments and file names in a -filelist input, thus
5875 // we prematurely stop our list (remaining files shall be passed as
5876 // arguments).
5877 if (InputFileList.size() > 0)
5878 break;
5879
5880 continue;
5881 }
5882
5883 InputFileList.push_back(II.getFilename());
5884 }
5885
Bob Wilson63d9f3c2012-05-15 18:57:39 +00005886 if (isObjCRuntimeLinked(Args) &&
5887 !Args.hasArg(options::OPT_nostdlib) &&
5888 !Args.hasArg(options::OPT_nodefaultlibs)) {
Stephen Hines651f13c2014-04-23 16:59:28 -07005889 // We use arclite library for both ARC and subscripting support.
5890 getMachOToolChain().AddLinkARCArgs(Args, CmdArgs);
5891
Bob Wilson0b1c7152012-04-21 00:21:42 +00005892 CmdArgs.push_back("-framework");
5893 CmdArgs.push_back("Foundation");
Ted Kremenekebcb57a2012-03-06 20:05:56 +00005894 // Link libobj.
5895 CmdArgs.push_back("-lobjc");
John McCall9f084a32011-07-06 00:26:06 +00005896 }
John McCallf85e1932011-06-15 23:02:42 +00005897
Daniel Dunbar02633b52009-03-26 16:23:12 +00005898 if (LinkingOutput) {
5899 CmdArgs.push_back("-arch_multiple");
5900 CmdArgs.push_back("-final_output");
5901 CmdArgs.push_back(LinkingOutput);
5902 }
5903
Daniel Dunbar02633b52009-03-26 16:23:12 +00005904 if (Args.hasArg(options::OPT_fnested_functions))
5905 CmdArgs.push_back("-allow_stack_execute");
Daniel Dunbarc21c4852009-04-08 23:54:23 +00005906
Daniel Dunbar02633b52009-03-26 16:23:12 +00005907 if (!Args.hasArg(options::OPT_nostdlib) &&
5908 !Args.hasArg(options::OPT_nodefaultlibs)) {
Hans Wennborg76b86c22013-07-18 20:29:38 +00005909 if (getToolChain().getDriver().CCCIsCXX())
Daniel Dunbar132e35d2010-09-17 01:20:05 +00005910 getToolChain().AddCXXStdlibLibArgs(Args, CmdArgs);
Daniel Dunbaredfa02b2009-04-08 06:06:21 +00005911
Daniel Dunbar02633b52009-03-26 16:23:12 +00005912 // link_ssp spec is empty.
5913
Daniel Dunbar6cd41542009-09-18 08:15:03 +00005914 // Let the tool chain choose which runtime library to link.
Stephen Hines651f13c2014-04-23 16:59:28 -07005915 getMachOToolChain().AddLinkRuntimeLibArgs(Args, CmdArgs);
Daniel Dunbar02633b52009-03-26 16:23:12 +00005916 }
5917
Chad Rosier18937312012-05-16 23:45:12 +00005918 if (!Args.hasArg(options::OPT_nostdlib) &&
Daniel Dunbar02633b52009-03-26 16:23:12 +00005919 !Args.hasArg(options::OPT_nostartfiles)) {
5920 // endfile_spec is empty.
5921 }
5922
5923 Args.AddAllArgs(CmdArgs, options::OPT_T_Group);
5924 Args.AddAllArgs(CmdArgs, options::OPT_F);
5925
Daniel Dunbarc21c4852009-04-08 23:54:23 +00005926 const char *Exec =
Stephen Hinesef822542014-07-21 00:47:37 -07005927 Args.MakeArgString(getToolChain().GetLinkerPath());
Stephen Hines176edba2014-12-01 14:53:08 -08005928 std::unique_ptr<Command> Cmd =
5929 llvm::make_unique<Command>(JA, *this, Exec, CmdArgs);
5930 Cmd->setInputFileList(std::move(InputFileList));
5931 C.addCommand(std::move(Cmd));
Daniel Dunbar02633b52009-03-26 16:23:12 +00005932}
5933
Daniel Dunbarff7488d2009-03-20 00:52:38 +00005934void darwin::Lipo::ConstructJob(Compilation &C, const JobAction &JA,
Daniel Dunbar2fe238e2010-08-02 02:38:28 +00005935 const InputInfo &Output,
Daniel Dunbarc21c4852009-04-08 23:54:23 +00005936 const InputInfoList &Inputs,
5937 const ArgList &Args,
Daniel Dunbarff7488d2009-03-20 00:52:38 +00005938 const char *LinkingOutput) const {
5939 ArgStringList CmdArgs;
5940
5941 CmdArgs.push_back("-create");
5942 assert(Output.isFilename() && "Unexpected lipo output.");
Daniel Dunbara428df82009-03-24 00:24:37 +00005943
5944 CmdArgs.push_back("-output");
Daniel Dunbarff7488d2009-03-20 00:52:38 +00005945 CmdArgs.push_back(Output.getFilename());
Daniel Dunbara428df82009-03-24 00:24:37 +00005946
Stephen Hinesef822542014-07-21 00:47:37 -07005947 for (const auto &II : Inputs) {
Daniel Dunbarff7488d2009-03-20 00:52:38 +00005948 assert(II.isFilename() && "Unexpected lipo input.");
5949 CmdArgs.push_back(II.getFilename());
5950 }
Stephen Hinesef822542014-07-21 00:47:37 -07005951
5952 const char *Exec = Args.MakeArgString(getToolChain().GetProgramPath("lipo"));
Stephen Hines176edba2014-12-01 14:53:08 -08005953 C.addCommand(llvm::make_unique<Command>(JA, *this, Exec, CmdArgs));
Daniel Dunbarff7488d2009-03-20 00:52:38 +00005954}
Daniel Dunbar68a31d42009-03-31 17:45:15 +00005955
Daniel Dunbar6e0f2542010-06-04 18:28:36 +00005956void darwin::Dsymutil::ConstructJob(Compilation &C, const JobAction &JA,
Daniel Dunbar2fe238e2010-08-02 02:38:28 +00005957 const InputInfo &Output,
Daniel Dunbar6e0f2542010-06-04 18:28:36 +00005958 const InputInfoList &Inputs,
5959 const ArgList &Args,
5960 const char *LinkingOutput) const {
5961 ArgStringList CmdArgs;
5962
Daniel Dunbar03e92302011-05-09 17:23:16 +00005963 CmdArgs.push_back("-o");
5964 CmdArgs.push_back(Output.getFilename());
5965
Daniel Dunbar6e0f2542010-06-04 18:28:36 +00005966 assert(Inputs.size() == 1 && "Unable to handle multiple inputs.");
5967 const InputInfo &Input = Inputs[0];
5968 assert(Input.isFilename() && "Unexpected dsymutil input.");
5969 CmdArgs.push_back(Input.getFilename());
5970
Daniel Dunbar6e0f2542010-06-04 18:28:36 +00005971 const char *Exec =
Daniel Dunbar4a7e8892010-07-14 18:46:23 +00005972 Args.MakeArgString(getToolChain().GetProgramPath("dsymutil"));
Stephen Hines176edba2014-12-01 14:53:08 -08005973 C.addCommand(llvm::make_unique<Command>(JA, *this, Exec, CmdArgs));
Daniel Dunbar6e0f2542010-06-04 18:28:36 +00005974}
5975
Eric Christopherf8571862011-08-23 17:56:55 +00005976void darwin::VerifyDebug::ConstructJob(Compilation &C, const JobAction &JA,
Eric Christopher27e2b982012-12-18 00:31:10 +00005977 const InputInfo &Output,
5978 const InputInfoList &Inputs,
5979 const ArgList &Args,
5980 const char *LinkingOutput) const {
Eric Christopherf8571862011-08-23 17:56:55 +00005981 ArgStringList CmdArgs;
5982 CmdArgs.push_back("--verify");
Eric Christopher1c79dc42012-02-06 19:13:09 +00005983 CmdArgs.push_back("--debug-info");
5984 CmdArgs.push_back("--eh-frame");
Eric Christopherb822f722012-02-06 19:43:51 +00005985 CmdArgs.push_back("--quiet");
Eric Christopherf8571862011-08-23 17:56:55 +00005986
5987 assert(Inputs.size() == 1 && "Unable to handle multiple inputs.");
5988 const InputInfo &Input = Inputs[0];
5989 assert(Input.isFilename() && "Unexpected verify input");
5990
5991 // Grabbing the output of the earlier dsymutil run.
5992 CmdArgs.push_back(Input.getFilename());
5993
5994 const char *Exec =
5995 Args.MakeArgString(getToolChain().GetProgramPath("dwarfdump"));
Stephen Hines176edba2014-12-01 14:53:08 -08005996 C.addCommand(llvm::make_unique<Command>(JA, *this, Exec, CmdArgs));
Eric Christopherf8571862011-08-23 17:56:55 +00005997}
5998
David Chisnall31c46902012-02-15 13:39:01 +00005999void solaris::Assemble::ConstructJob(Compilation &C, const JobAction &JA,
6000 const InputInfo &Output,
6001 const InputInfoList &Inputs,
6002 const ArgList &Args,
6003 const char *LinkingOutput) const {
6004 ArgStringList CmdArgs;
6005
6006 Args.AddAllArgValues(CmdArgs, options::OPT_Wa_COMMA,
6007 options::OPT_Xassembler);
6008
6009 CmdArgs.push_back("-o");
6010 CmdArgs.push_back(Output.getFilename());
6011
Stephen Hinesef822542014-07-21 00:47:37 -07006012 for (const auto &II : Inputs)
David Chisnall31c46902012-02-15 13:39:01 +00006013 CmdArgs.push_back(II.getFilename());
David Chisnall31c46902012-02-15 13:39:01 +00006014
Stephen Hinesef822542014-07-21 00:47:37 -07006015 const char *Exec = Args.MakeArgString(getToolChain().GetProgramPath("as"));
Stephen Hines176edba2014-12-01 14:53:08 -08006016 C.addCommand(llvm::make_unique<Command>(JA, *this, Exec, CmdArgs));
David Chisnall31c46902012-02-15 13:39:01 +00006017}
6018
David Chisnall31c46902012-02-15 13:39:01 +00006019void solaris::Link::ConstructJob(Compilation &C, const JobAction &JA,
6020 const InputInfo &Output,
6021 const InputInfoList &Inputs,
6022 const ArgList &Args,
6023 const char *LinkingOutput) const {
6024 // FIXME: Find a real GCC, don't hard-code versions here
6025 std::string GCCLibPath = "/usr/gcc/4.5/lib/gcc/";
6026 const llvm::Triple &T = getToolChain().getTriple();
6027 std::string LibPath = "/usr/lib/";
6028 llvm::Triple::ArchType Arch = T.getArch();
6029 switch (Arch) {
Benjamin Kramer39e4b0f2013-10-21 12:33:55 +00006030 case llvm::Triple::x86:
6031 GCCLibPath +=
6032 ("i386-" + T.getVendorName() + "-" + T.getOSName()).str() + "/4.5.2/";
6033 break;
6034 case llvm::Triple::x86_64:
6035 GCCLibPath += ("i386-" + T.getVendorName() + "-" + T.getOSName()).str();
6036 GCCLibPath += "/4.5.2/amd64/";
6037 LibPath += "amd64/";
6038 break;
6039 default:
6040 llvm_unreachable("Unsupported architecture");
David Chisnall31c46902012-02-15 13:39:01 +00006041 }
6042
6043 ArgStringList CmdArgs;
6044
David Chisnall41d476d2012-02-29 15:06:12 +00006045 // Demangle C++ names in errors
6046 CmdArgs.push_back("-C");
6047
David Chisnall31c46902012-02-15 13:39:01 +00006048 if ((!Args.hasArg(options::OPT_nostdlib)) &&
6049 (!Args.hasArg(options::OPT_shared))) {
6050 CmdArgs.push_back("-e");
6051 CmdArgs.push_back("_start");
6052 }
6053
6054 if (Args.hasArg(options::OPT_static)) {
6055 CmdArgs.push_back("-Bstatic");
6056 CmdArgs.push_back("-dn");
6057 } else {
6058 CmdArgs.push_back("-Bdynamic");
6059 if (Args.hasArg(options::OPT_shared)) {
6060 CmdArgs.push_back("-shared");
6061 } else {
6062 CmdArgs.push_back("--dynamic-linker");
6063 CmdArgs.push_back(Args.MakeArgString(LibPath + "ld.so.1"));
6064 }
6065 }
6066
6067 if (Output.isFilename()) {
6068 CmdArgs.push_back("-o");
6069 CmdArgs.push_back(Output.getFilename());
6070 } else {
6071 assert(Output.isNothing() && "Invalid output.");
6072 }
6073
6074 if (!Args.hasArg(options::OPT_nostdlib) &&
6075 !Args.hasArg(options::OPT_nostartfiles)) {
6076 if (!Args.hasArg(options::OPT_shared)) {
6077 CmdArgs.push_back(Args.MakeArgString(LibPath + "crt1.o"));
6078 CmdArgs.push_back(Args.MakeArgString(LibPath + "crti.o"));
David Chisnall165329c2012-02-28 17:10:04 +00006079 CmdArgs.push_back(Args.MakeArgString(LibPath + "values-Xa.o"));
David Chisnall31c46902012-02-15 13:39:01 +00006080 CmdArgs.push_back(Args.MakeArgString(GCCLibPath + "crtbegin.o"));
6081 } else {
6082 CmdArgs.push_back(Args.MakeArgString(LibPath + "crti.o"));
David Chisnall165329c2012-02-28 17:10:04 +00006083 CmdArgs.push_back(Args.MakeArgString(LibPath + "values-Xa.o"));
6084 CmdArgs.push_back(Args.MakeArgString(GCCLibPath + "crtbegin.o"));
David Chisnall31c46902012-02-15 13:39:01 +00006085 }
Hans Wennborg76b86c22013-07-18 20:29:38 +00006086 if (getToolChain().getDriver().CCCIsCXX())
David Chisnalle6dd6832012-03-13 14:14:54 +00006087 CmdArgs.push_back(Args.MakeArgString(LibPath + "cxa_finalize.o"));
David Chisnall31c46902012-02-15 13:39:01 +00006088 }
6089
6090 CmdArgs.push_back(Args.MakeArgString("-L" + GCCLibPath));
6091
6092 Args.AddAllArgs(CmdArgs, options::OPT_L);
6093 Args.AddAllArgs(CmdArgs, options::OPT_T_Group);
6094 Args.AddAllArgs(CmdArgs, options::OPT_e);
David Chisnall165329c2012-02-28 17:10:04 +00006095 Args.AddAllArgs(CmdArgs, options::OPT_r);
David Chisnall31c46902012-02-15 13:39:01 +00006096
6097 AddLinkerInputs(getToolChain(), Inputs, Args, CmdArgs);
6098
6099 if (!Args.hasArg(options::OPT_nostdlib) &&
6100 !Args.hasArg(options::OPT_nodefaultlibs)) {
Hans Wennborg76b86c22013-07-18 20:29:38 +00006101 if (getToolChain().getDriver().CCCIsCXX())
David Chisnalle58e6f92012-04-10 11:49:50 +00006102 getToolChain().AddCXXStdlibLibArgs(Args, CmdArgs);
David Chisnallb6229592012-02-15 18:24:31 +00006103 CmdArgs.push_back("-lgcc_s");
David Chisnall165329c2012-02-28 17:10:04 +00006104 if (!Args.hasArg(options::OPT_shared)) {
6105 CmdArgs.push_back("-lgcc");
David Chisnall31c46902012-02-15 13:39:01 +00006106 CmdArgs.push_back("-lc");
David Chisnall7dbefe12012-02-28 20:06:45 +00006107 CmdArgs.push_back("-lm");
David Chisnall165329c2012-02-28 17:10:04 +00006108 }
David Chisnall31c46902012-02-15 13:39:01 +00006109 }
6110
6111 if (!Args.hasArg(options::OPT_nostdlib) &&
6112 !Args.hasArg(options::OPT_nostartfiles)) {
David Chisnall165329c2012-02-28 17:10:04 +00006113 CmdArgs.push_back(Args.MakeArgString(GCCLibPath + "crtend.o"));
David Chisnall31c46902012-02-15 13:39:01 +00006114 }
David Chisnalld1ac03e2012-02-16 16:00:47 +00006115 CmdArgs.push_back(Args.MakeArgString(LibPath + "crtn.o"));
David Chisnall31c46902012-02-15 13:39:01 +00006116
Stephen Hines651f13c2014-04-23 16:59:28 -07006117 addProfileRT(getToolChain(), Args, CmdArgs);
David Chisnall31c46902012-02-15 13:39:01 +00006118
6119 const char *Exec =
Stephen Hinesef822542014-07-21 00:47:37 -07006120 Args.MakeArgString(getToolChain().GetLinkerPath());
Stephen Hines176edba2014-12-01 14:53:08 -08006121 C.addCommand(llvm::make_unique<Command>(JA, *this, Exec, CmdArgs));
Edward O'Callaghane7925a02009-08-22 01:06:46 +00006122}
6123
Daniel Dunbarf7b8eec2009-06-29 20:52:51 +00006124void openbsd::Assemble::ConstructJob(Compilation &C, const JobAction &JA,
Daniel Dunbar2fe238e2010-08-02 02:38:28 +00006125 const InputInfo &Output,
Daniel Dunbarf7b8eec2009-06-29 20:52:51 +00006126 const InputInfoList &Inputs,
6127 const ArgList &Args,
Mike Stump1eb44332009-09-09 15:08:12 +00006128 const char *LinkingOutput) const {
Daniel Dunbarf7b8eec2009-06-29 20:52:51 +00006129 ArgStringList CmdArgs;
Stephen Hines651f13c2014-04-23 16:59:28 -07006130 bool NeedsKPIC = false;
Daniel Dunbarf7b8eec2009-06-29 20:52:51 +00006131
Stephen Hines651f13c2014-04-23 16:59:28 -07006132 switch (getToolChain().getArch()) {
6133 case llvm::Triple::x86:
6134 // When building 32-bit code on OpenBSD/amd64, we have to explicitly
6135 // instruct as in the base system to assemble 32-bit code.
Bill Wendlingac66cb82013-12-08 00:21:01 +00006136 CmdArgs.push_back("--32");
Stephen Hines651f13c2014-04-23 16:59:28 -07006137 break;
6138
6139 case llvm::Triple::ppc:
Bill Wendlingac66cb82013-12-08 00:21:01 +00006140 CmdArgs.push_back("-mppc");
6141 CmdArgs.push_back("-many");
Stephen Hines651f13c2014-04-23 16:59:28 -07006142 break;
6143
6144 case llvm::Triple::sparc:
6145 CmdArgs.push_back("-32");
6146 NeedsKPIC = true;
6147 break;
6148
6149 case llvm::Triple::sparcv9:
6150 CmdArgs.push_back("-64");
6151 CmdArgs.push_back("-Av9a");
6152 NeedsKPIC = true;
6153 break;
6154
6155 case llvm::Triple::mips64:
6156 case llvm::Triple::mips64el: {
Bill Wendlingac66cb82013-12-08 00:21:01 +00006157 StringRef CPUName;
6158 StringRef ABIName;
Stephen Hines176edba2014-12-01 14:53:08 -08006159 mips::getMipsCPUAndABI(Args, getToolChain().getTriple(), CPUName, ABIName);
Bill Wendlingac66cb82013-12-08 00:21:01 +00006160
6161 CmdArgs.push_back("-mabi");
6162 CmdArgs.push_back(getGnuCompatibleMipsABIName(ABIName).data());
6163
6164 if (getToolChain().getArch() == llvm::Triple::mips64)
6165 CmdArgs.push_back("-EB");
6166 else
6167 CmdArgs.push_back("-EL");
6168
Stephen Hines651f13c2014-04-23 16:59:28 -07006169 NeedsKPIC = true;
6170 break;
Bill Wendlingac66cb82013-12-08 00:21:01 +00006171 }
6172
Stephen Hines651f13c2014-04-23 16:59:28 -07006173 default:
6174 break;
6175 }
6176
6177 if (NeedsKPIC)
6178 addAssemblerKPIC(Args, CmdArgs);
6179
Daniel Dunbarf7b8eec2009-06-29 20:52:51 +00006180 Args.AddAllArgValues(CmdArgs, options::OPT_Wa_COMMA,
6181 options::OPT_Xassembler);
6182
6183 CmdArgs.push_back("-o");
Daniel Dunbar7c1e4652010-08-02 02:38:21 +00006184 CmdArgs.push_back(Output.getFilename());
Daniel Dunbarf7b8eec2009-06-29 20:52:51 +00006185
Stephen Hinesef822542014-07-21 00:47:37 -07006186 for (const auto &II : Inputs)
Daniel Dunbar7c1e4652010-08-02 02:38:21 +00006187 CmdArgs.push_back(II.getFilename());
Daniel Dunbarf7b8eec2009-06-29 20:52:51 +00006188
6189 const char *Exec =
Daniel Dunbar4a7e8892010-07-14 18:46:23 +00006190 Args.MakeArgString(getToolChain().GetProgramPath("as"));
Stephen Hines176edba2014-12-01 14:53:08 -08006191 C.addCommand(llvm::make_unique<Command>(JA, *this, Exec, CmdArgs));
Daniel Dunbarf7b8eec2009-06-29 20:52:51 +00006192}
6193
6194void openbsd::Link::ConstructJob(Compilation &C, const JobAction &JA,
Daniel Dunbar2fe238e2010-08-02 02:38:28 +00006195 const InputInfo &Output,
Daniel Dunbarf7b8eec2009-06-29 20:52:51 +00006196 const InputInfoList &Inputs,
6197 const ArgList &Args,
6198 const char *LinkingOutput) const {
Daniel Dunbaree788e72009-12-21 18:54:17 +00006199 const Driver &D = getToolChain().getDriver();
Daniel Dunbarf7b8eec2009-06-29 20:52:51 +00006200 ArgStringList CmdArgs;
6201
Rafael Espindola6cc2a682012-12-31 22:41:36 +00006202 // Silence warning for "clang -g foo.o -o foo"
6203 Args.ClaimAllArgs(options::OPT_g_Group);
6204 // and "clang -emit-llvm foo.o -o foo"
6205 Args.ClaimAllArgs(options::OPT_emit_llvm);
6206 // and for "clang -w foo.o -o foo". Other warning options are already
6207 // handled somewhere else.
6208 Args.ClaimAllArgs(options::OPT_w);
6209
Bill Wendlingac66cb82013-12-08 00:21:01 +00006210 if (getToolChain().getArch() == llvm::Triple::mips64)
6211 CmdArgs.push_back("-EB");
6212 else if (getToolChain().getArch() == llvm::Triple::mips64el)
6213 CmdArgs.push_back("-EL");
6214
Daniel Dunbar2bbcf662009-08-03 01:28:59 +00006215 if ((!Args.hasArg(options::OPT_nostdlib)) &&
Daniel Dunbar294691e2009-11-04 06:24:38 +00006216 (!Args.hasArg(options::OPT_shared))) {
Daniel Dunbar2bbcf662009-08-03 01:28:59 +00006217 CmdArgs.push_back("-e");
6218 CmdArgs.push_back("__start");
6219 }
6220
Daniel Dunbarf7b8eec2009-06-29 20:52:51 +00006221 if (Args.hasArg(options::OPT_static)) {
6222 CmdArgs.push_back("-Bstatic");
6223 } else {
Rafael Espindola65ba55d2010-11-11 02:17:51 +00006224 if (Args.hasArg(options::OPT_rdynamic))
6225 CmdArgs.push_back("-export-dynamic");
Daniel Dunbarf7b8eec2009-06-29 20:52:51 +00006226 CmdArgs.push_back("--eh-frame-hdr");
Daniel Dunbar2bbcf662009-08-03 01:28:59 +00006227 CmdArgs.push_back("-Bdynamic");
Daniel Dunbarf7b8eec2009-06-29 20:52:51 +00006228 if (Args.hasArg(options::OPT_shared)) {
Daniel Dunbar2bbcf662009-08-03 01:28:59 +00006229 CmdArgs.push_back("-shared");
Daniel Dunbarf7b8eec2009-06-29 20:52:51 +00006230 } else {
6231 CmdArgs.push_back("-dynamic-linker");
6232 CmdArgs.push_back("/usr/libexec/ld.so");
6233 }
6234 }
6235
Rafael Espindola9adba392013-06-05 04:28:55 +00006236 if (Args.hasArg(options::OPT_nopie))
6237 CmdArgs.push_back("-nopie");
6238
Daniel Dunbar7c1e4652010-08-02 02:38:21 +00006239 if (Output.isFilename()) {
Daniel Dunbarf7b8eec2009-06-29 20:52:51 +00006240 CmdArgs.push_back("-o");
6241 CmdArgs.push_back(Output.getFilename());
6242 } else {
6243 assert(Output.isNothing() && "Invalid output.");
6244 }
6245
6246 if (!Args.hasArg(options::OPT_nostdlib) &&
6247 !Args.hasArg(options::OPT_nostartfiles)) {
6248 if (!Args.hasArg(options::OPT_shared)) {
Eli Friedman62d829a2011-12-15 02:15:56 +00006249 if (Args.hasArg(options::OPT_pg))
6250 CmdArgs.push_back(Args.MakeArgString(
6251 getToolChain().GetFilePath("gcrt0.o")));
6252 else
6253 CmdArgs.push_back(Args.MakeArgString(
6254 getToolChain().GetFilePath("crt0.o")));
Chris Lattner38e317d2010-07-07 16:01:42 +00006255 CmdArgs.push_back(Args.MakeArgString(
Daniel Dunbar4a7e8892010-07-14 18:46:23 +00006256 getToolChain().GetFilePath("crtbegin.o")));
Daniel Dunbarf7b8eec2009-06-29 20:52:51 +00006257 } else {
Chris Lattner38e317d2010-07-07 16:01:42 +00006258 CmdArgs.push_back(Args.MakeArgString(
Daniel Dunbar4a7e8892010-07-14 18:46:23 +00006259 getToolChain().GetFilePath("crtbeginS.o")));
Daniel Dunbarf7b8eec2009-06-29 20:52:51 +00006260 }
6261 }
6262
Edward O'Callaghane7e18202009-10-28 15:13:08 +00006263 std::string Triple = getToolChain().getTripleString();
6264 if (Triple.substr(0, 6) == "x86_64")
Daniel Dunbar294691e2009-11-04 06:24:38 +00006265 Triple.replace(0, 6, "amd64");
Daniel Dunbarf7fb31f2009-10-29 02:24:37 +00006266 CmdArgs.push_back(Args.MakeArgString("-L/usr/lib/gcc-lib/" + Triple +
Daniel Dunbar95c04572010-08-01 23:13:54 +00006267 "/4.2.1"));
Daniel Dunbar2bbcf662009-08-03 01:28:59 +00006268
Daniel Dunbarf7b8eec2009-06-29 20:52:51 +00006269 Args.AddAllArgs(CmdArgs, options::OPT_L);
6270 Args.AddAllArgs(CmdArgs, options::OPT_T_Group);
6271 Args.AddAllArgs(CmdArgs, options::OPT_e);
Rafael Espindola6cc2a682012-12-31 22:41:36 +00006272 Args.AddAllArgs(CmdArgs, options::OPT_s);
6273 Args.AddAllArgs(CmdArgs, options::OPT_t);
6274 Args.AddAllArgs(CmdArgs, options::OPT_Z_Flag);
6275 Args.AddAllArgs(CmdArgs, options::OPT_r);
Daniel Dunbarf7b8eec2009-06-29 20:52:51 +00006276
Daniel Dunbar2008fee2010-09-17 00:24:54 +00006277 AddLinkerInputs(getToolChain(), Inputs, Args, CmdArgs);
Daniel Dunbarf7b8eec2009-06-29 20:52:51 +00006278
6279 if (!Args.hasArg(options::OPT_nostdlib) &&
6280 !Args.hasArg(options::OPT_nodefaultlibs)) {
Hans Wennborg76b86c22013-07-18 20:29:38 +00006281 if (D.CCCIsCXX()) {
Daniel Dunbar132e35d2010-09-17 01:20:05 +00006282 getToolChain().AddCXXStdlibLibArgs(Args, CmdArgs);
Eli Friedman62d829a2011-12-15 02:15:56 +00006283 if (Args.hasArg(options::OPT_pg))
6284 CmdArgs.push_back("-lm_p");
6285 else
6286 CmdArgs.push_back("-lm");
Daniel Dunbar95c04572010-08-01 23:13:54 +00006287 }
6288
Daniel Dunbar2bbcf662009-08-03 01:28:59 +00006289 // FIXME: For some reason GCC passes -lgcc before adding
6290 // the default system libraries. Just mimic this for now.
6291 CmdArgs.push_back("-lgcc");
Daniel Dunbarf7b8eec2009-06-29 20:52:51 +00006292
Eric Christopherdc6cc872012-09-13 06:32:34 +00006293 if (Args.hasArg(options::OPT_pthread)) {
6294 if (!Args.hasArg(options::OPT_shared) &&
6295 Args.hasArg(options::OPT_pg))
6296 CmdArgs.push_back("-lpthread_p");
6297 else
6298 CmdArgs.push_back("-lpthread");
6299 }
6300
Chandler Carruth657849c2011-12-17 22:32:42 +00006301 if (!Args.hasArg(options::OPT_shared)) {
Eric Christopherdc6cc872012-09-13 06:32:34 +00006302 if (Args.hasArg(options::OPT_pg))
Eli Friedman62d829a2011-12-15 02:15:56 +00006303 CmdArgs.push_back("-lc_p");
6304 else
6305 CmdArgs.push_back("-lc");
Chandler Carruth657849c2011-12-17 22:32:42 +00006306 }
Eric Christopherdc6cc872012-09-13 06:32:34 +00006307
Daniel Dunbar2bbcf662009-08-03 01:28:59 +00006308 CmdArgs.push_back("-lgcc");
Daniel Dunbarf7b8eec2009-06-29 20:52:51 +00006309 }
6310
6311 if (!Args.hasArg(options::OPT_nostdlib) &&
6312 !Args.hasArg(options::OPT_nostartfiles)) {
6313 if (!Args.hasArg(options::OPT_shared))
Chris Lattner38e317d2010-07-07 16:01:42 +00006314 CmdArgs.push_back(Args.MakeArgString(
Daniel Dunbar4a7e8892010-07-14 18:46:23 +00006315 getToolChain().GetFilePath("crtend.o")));
Daniel Dunbarf7b8eec2009-06-29 20:52:51 +00006316 else
Chris Lattner38e317d2010-07-07 16:01:42 +00006317 CmdArgs.push_back(Args.MakeArgString(
Daniel Dunbar4a7e8892010-07-14 18:46:23 +00006318 getToolChain().GetFilePath("crtendS.o")));
Daniel Dunbarf7b8eec2009-06-29 20:52:51 +00006319 }
6320
6321 const char *Exec =
Stephen Hinesef822542014-07-21 00:47:37 -07006322 Args.MakeArgString(getToolChain().GetLinkerPath());
Stephen Hines176edba2014-12-01 14:53:08 -08006323 C.addCommand(llvm::make_unique<Command>(JA, *this, Exec, CmdArgs));
Daniel Dunbarf7b8eec2009-06-29 20:52:51 +00006324}
Ed Schoutenc66a5a32009-04-02 19:13:12 +00006325
Eli Friedman42f74f22012-08-08 23:57:20 +00006326void bitrig::Assemble::ConstructJob(Compilation &C, const JobAction &JA,
6327 const InputInfo &Output,
6328 const InputInfoList &Inputs,
6329 const ArgList &Args,
6330 const char *LinkingOutput) const {
6331 ArgStringList CmdArgs;
6332
6333 Args.AddAllArgValues(CmdArgs, options::OPT_Wa_COMMA,
6334 options::OPT_Xassembler);
6335
6336 CmdArgs.push_back("-o");
6337 CmdArgs.push_back(Output.getFilename());
6338
Stephen Hinesef822542014-07-21 00:47:37 -07006339 for (const auto &II : Inputs)
Eli Friedman42f74f22012-08-08 23:57:20 +00006340 CmdArgs.push_back(II.getFilename());
Eli Friedman42f74f22012-08-08 23:57:20 +00006341
Stephen Hinesef822542014-07-21 00:47:37 -07006342 const char *Exec = Args.MakeArgString(getToolChain().GetProgramPath("as"));
Stephen Hines176edba2014-12-01 14:53:08 -08006343 C.addCommand(llvm::make_unique<Command>(JA, *this, Exec, CmdArgs));
Eli Friedman42f74f22012-08-08 23:57:20 +00006344}
6345
6346void bitrig::Link::ConstructJob(Compilation &C, const JobAction &JA,
6347 const InputInfo &Output,
6348 const InputInfoList &Inputs,
6349 const ArgList &Args,
6350 const char *LinkingOutput) const {
6351 const Driver &D = getToolChain().getDriver();
6352 ArgStringList CmdArgs;
6353
6354 if ((!Args.hasArg(options::OPT_nostdlib)) &&
6355 (!Args.hasArg(options::OPT_shared))) {
6356 CmdArgs.push_back("-e");
6357 CmdArgs.push_back("__start");
6358 }
6359
6360 if (Args.hasArg(options::OPT_static)) {
6361 CmdArgs.push_back("-Bstatic");
6362 } else {
6363 if (Args.hasArg(options::OPT_rdynamic))
6364 CmdArgs.push_back("-export-dynamic");
6365 CmdArgs.push_back("--eh-frame-hdr");
6366 CmdArgs.push_back("-Bdynamic");
6367 if (Args.hasArg(options::OPT_shared)) {
6368 CmdArgs.push_back("-shared");
6369 } else {
6370 CmdArgs.push_back("-dynamic-linker");
6371 CmdArgs.push_back("/usr/libexec/ld.so");
6372 }
6373 }
6374
6375 if (Output.isFilename()) {
6376 CmdArgs.push_back("-o");
6377 CmdArgs.push_back(Output.getFilename());
6378 } else {
6379 assert(Output.isNothing() && "Invalid output.");
6380 }
6381
6382 if (!Args.hasArg(options::OPT_nostdlib) &&
6383 !Args.hasArg(options::OPT_nostartfiles)) {
6384 if (!Args.hasArg(options::OPT_shared)) {
6385 if (Args.hasArg(options::OPT_pg))
6386 CmdArgs.push_back(Args.MakeArgString(
6387 getToolChain().GetFilePath("gcrt0.o")));
6388 else
6389 CmdArgs.push_back(Args.MakeArgString(
6390 getToolChain().GetFilePath("crt0.o")));
6391 CmdArgs.push_back(Args.MakeArgString(
6392 getToolChain().GetFilePath("crtbegin.o")));
6393 } else {
6394 CmdArgs.push_back(Args.MakeArgString(
6395 getToolChain().GetFilePath("crtbeginS.o")));
6396 }
6397 }
6398
6399 Args.AddAllArgs(CmdArgs, options::OPT_L);
6400 Args.AddAllArgs(CmdArgs, options::OPT_T_Group);
6401 Args.AddAllArgs(CmdArgs, options::OPT_e);
6402
6403 AddLinkerInputs(getToolChain(), Inputs, Args, CmdArgs);
6404
6405 if (!Args.hasArg(options::OPT_nostdlib) &&
6406 !Args.hasArg(options::OPT_nodefaultlibs)) {
Hans Wennborg76b86c22013-07-18 20:29:38 +00006407 if (D.CCCIsCXX()) {
Eli Friedman42f74f22012-08-08 23:57:20 +00006408 getToolChain().AddCXXStdlibLibArgs(Args, CmdArgs);
6409 if (Args.hasArg(options::OPT_pg))
6410 CmdArgs.push_back("-lm_p");
6411 else
6412 CmdArgs.push_back("-lm");
6413 }
6414
Rafael Espindola3667bbe2012-10-23 17:07:31 +00006415 if (Args.hasArg(options::OPT_pthread)) {
6416 if (!Args.hasArg(options::OPT_shared) &&
6417 Args.hasArg(options::OPT_pg))
6418 CmdArgs.push_back("-lpthread_p");
6419 else
6420 CmdArgs.push_back("-lpthread");
6421 }
6422
Eli Friedman42f74f22012-08-08 23:57:20 +00006423 if (!Args.hasArg(options::OPT_shared)) {
6424 if (Args.hasArg(options::OPT_pg))
6425 CmdArgs.push_back("-lc_p");
6426 else
6427 CmdArgs.push_back("-lc");
6428 }
6429
Benjamin Kramer39e4b0f2013-10-21 12:33:55 +00006430 StringRef MyArch;
6431 switch (getToolChain().getTriple().getArch()) {
6432 case llvm::Triple::arm:
6433 MyArch = "arm";
6434 break;
6435 case llvm::Triple::x86:
6436 MyArch = "i386";
6437 break;
6438 case llvm::Triple::x86_64:
6439 MyArch = "amd64";
6440 break;
6441 default:
6442 llvm_unreachable("Unsupported architecture");
6443 }
6444 CmdArgs.push_back(Args.MakeArgString("-lclang_rt." + MyArch));
Eli Friedman42f74f22012-08-08 23:57:20 +00006445 }
6446
6447 if (!Args.hasArg(options::OPT_nostdlib) &&
6448 !Args.hasArg(options::OPT_nostartfiles)) {
6449 if (!Args.hasArg(options::OPT_shared))
6450 CmdArgs.push_back(Args.MakeArgString(
6451 getToolChain().GetFilePath("crtend.o")));
6452 else
6453 CmdArgs.push_back(Args.MakeArgString(
6454 getToolChain().GetFilePath("crtendS.o")));
6455 }
Eli Friedmanc9c48db2012-08-09 22:42:04 +00006456
6457 const char *Exec =
Stephen Hinesef822542014-07-21 00:47:37 -07006458 Args.MakeArgString(getToolChain().GetLinkerPath());
Stephen Hines176edba2014-12-01 14:53:08 -08006459 C.addCommand(llvm::make_unique<Command>(JA, *this, Exec, CmdArgs));
Eli Friedman42f74f22012-08-08 23:57:20 +00006460}
6461
Daniel Dunbar68a31d42009-03-31 17:45:15 +00006462void freebsd::Assemble::ConstructJob(Compilation &C, const JobAction &JA,
Daniel Dunbar2fe238e2010-08-02 02:38:28 +00006463 const InputInfo &Output,
Daniel Dunbarc21c4852009-04-08 23:54:23 +00006464 const InputInfoList &Inputs,
6465 const ArgList &Args,
Mike Stump1eb44332009-09-09 15:08:12 +00006466 const char *LinkingOutput) const {
Daniel Dunbar68a31d42009-03-31 17:45:15 +00006467 ArgStringList CmdArgs;
6468
Daniel Dunbar008f54a2009-04-01 19:36:32 +00006469 // When building 32-bit code on FreeBSD/amd64, we have to explicitly
6470 // instruct as in the base system to assemble 32-bit code.
Eric Christopherc55da4b2012-09-05 21:32:44 +00006471 if (getToolChain().getArch() == llvm::Triple::x86)
Daniel Dunbar68a31d42009-03-31 17:45:15 +00006472 CmdArgs.push_back("--32");
Eric Christopherc55da4b2012-09-05 21:32:44 +00006473 else if (getToolChain().getArch() == llvm::Triple::ppc)
Roman Divacky3393cef2011-06-04 07:37:31 +00006474 CmdArgs.push_back("-a32");
Eric Christopherc55da4b2012-09-05 21:32:44 +00006475 else if (getToolChain().getArch() == llvm::Triple::mips ||
6476 getToolChain().getArch() == llvm::Triple::mipsel ||
6477 getToolChain().getArch() == llvm::Triple::mips64 ||
6478 getToolChain().getArch() == llvm::Triple::mips64el) {
6479 StringRef CPUName;
6480 StringRef ABIName;
Stephen Hines176edba2014-12-01 14:53:08 -08006481 mips::getMipsCPUAndABI(Args, getToolChain().getTriple(), CPUName, ABIName);
Michael J. Spencer20249a12010-10-21 03:16:25 +00006482
Eric Christopherc55da4b2012-09-05 21:32:44 +00006483 CmdArgs.push_back("-march");
6484 CmdArgs.push_back(CPUName.data());
6485
Eric Christopherc55da4b2012-09-05 21:32:44 +00006486 CmdArgs.push_back("-mabi");
Simon Atanasyane9616a42013-02-27 14:55:49 +00006487 CmdArgs.push_back(getGnuCompatibleMipsABIName(ABIName).data());
Eric Christopherc55da4b2012-09-05 21:32:44 +00006488
6489 if (getToolChain().getArch() == llvm::Triple::mips ||
6490 getToolChain().getArch() == llvm::Triple::mips64)
6491 CmdArgs.push_back("-EB");
6492 else
6493 CmdArgs.push_back("-EL");
6494
Stephen Hines651f13c2014-04-23 16:59:28 -07006495 addAssemblerKPIC(Args, CmdArgs);
Rafael Espindola27fa2362012-12-13 04:17:14 +00006496 } else if (getToolChain().getArch() == llvm::Triple::arm ||
Stephen Hines651f13c2014-04-23 16:59:28 -07006497 getToolChain().getArch() == llvm::Triple::armeb ||
6498 getToolChain().getArch() == llvm::Triple::thumb ||
6499 getToolChain().getArch() == llvm::Triple::thumbeb) {
6500 const Driver &D = getToolChain().getDriver();
6501 const llvm::Triple &Triple = getToolChain().getTriple();
6502 StringRef FloatABI = arm::getARMFloatABI(D, Args, Triple);
6503
6504 if (FloatABI == "hard") {
6505 CmdArgs.push_back("-mfpu=vfp");
6506 } else {
6507 CmdArgs.push_back("-mfpu=softvfp");
6508 }
6509
Rafael Espindola27fa2362012-12-13 04:17:14 +00006510 switch(getToolChain().getTriple().getEnvironment()) {
Stephen Hines651f13c2014-04-23 16:59:28 -07006511 case llvm::Triple::GNUEABIHF:
Rafael Espindola27fa2362012-12-13 04:17:14 +00006512 case llvm::Triple::GNUEABI:
6513 case llvm::Triple::EABI:
Anton Korobeynikovb234e742013-03-18 07:59:20 +00006514 CmdArgs.push_back("-meabi=5");
Rafael Espindola27fa2362012-12-13 04:17:14 +00006515 break;
6516
6517 default:
6518 CmdArgs.push_back("-matpcs");
6519 }
Stephen Hines651f13c2014-04-23 16:59:28 -07006520 } else if (getToolChain().getArch() == llvm::Triple::sparc ||
6521 getToolChain().getArch() == llvm::Triple::sparcv9) {
6522 if (getToolChain().getArch() == llvm::Triple::sparc)
6523 CmdArgs.push_back("-Av8plusa");
6524 else
6525 CmdArgs.push_back("-Av9a");
6526
6527 addAssemblerKPIC(Args, CmdArgs);
Eric Christopherc55da4b2012-09-05 21:32:44 +00006528 }
Eric Christophered734732010-03-02 02:41:08 +00006529
Daniel Dunbar68a31d42009-03-31 17:45:15 +00006530 Args.AddAllArgValues(CmdArgs, options::OPT_Wa_COMMA,
6531 options::OPT_Xassembler);
6532
6533 CmdArgs.push_back("-o");
Daniel Dunbar7c1e4652010-08-02 02:38:21 +00006534 CmdArgs.push_back(Output.getFilename());
Daniel Dunbar68a31d42009-03-31 17:45:15 +00006535
Stephen Hinesef822542014-07-21 00:47:37 -07006536 for (const auto &II : Inputs)
Daniel Dunbar7c1e4652010-08-02 02:38:21 +00006537 CmdArgs.push_back(II.getFilename());
Daniel Dunbar68a31d42009-03-31 17:45:15 +00006538
Stephen Hinesef822542014-07-21 00:47:37 -07006539 const char *Exec = Args.MakeArgString(getToolChain().GetProgramPath("as"));
Stephen Hines176edba2014-12-01 14:53:08 -08006540 C.addCommand(llvm::make_unique<Command>(JA, *this, Exec, CmdArgs));
Daniel Dunbar68a31d42009-03-31 17:45:15 +00006541}
Daniel Dunbar008f54a2009-04-01 19:36:32 +00006542
6543void freebsd::Link::ConstructJob(Compilation &C, const JobAction &JA,
Daniel Dunbar2fe238e2010-08-02 02:38:28 +00006544 const InputInfo &Output,
Daniel Dunbarc21c4852009-04-08 23:54:23 +00006545 const InputInfoList &Inputs,
6546 const ArgList &Args,
Daniel Dunbara8304f62009-05-02 20:14:53 +00006547 const char *LinkingOutput) const {
Roman Divacky94380162012-08-28 15:09:03 +00006548 const toolchains::FreeBSD& ToolChain =
6549 static_cast<const toolchains::FreeBSD&>(getToolChain());
6550 const Driver &D = ToolChain.getDriver();
Stephen Hines651f13c2014-04-23 16:59:28 -07006551 const bool IsPIE =
6552 !Args.hasArg(options::OPT_shared) &&
6553 (Args.hasArg(options::OPT_pie) || ToolChain.isPIEDefault());
Daniel Dunbar008f54a2009-04-01 19:36:32 +00006554 ArgStringList CmdArgs;
David Chisnalldfa210b2012-07-29 15:24:44 +00006555
6556 // Silence warning for "clang -g foo.o -o foo"
6557 Args.ClaimAllArgs(options::OPT_g_Group);
6558 // and "clang -emit-llvm foo.o -o foo"
6559 Args.ClaimAllArgs(options::OPT_emit_llvm);
6560 // and for "clang -w foo.o -o foo". Other warning options are already
6561 // handled somewhere else.
6562 Args.ClaimAllArgs(options::OPT_w);
Daniel Dunbar008f54a2009-04-01 19:36:32 +00006563
Joerg Sonnenberger8ab2bdc2011-03-21 13:51:29 +00006564 if (!D.SysRoot.empty())
6565 CmdArgs.push_back(Args.MakeArgString("--sysroot=" + D.SysRoot));
6566
Stephen Hines651f13c2014-04-23 16:59:28 -07006567 if (IsPIE)
Roman Divacky94380162012-08-28 15:09:03 +00006568 CmdArgs.push_back("-pie");
6569
Daniel Dunbar008f54a2009-04-01 19:36:32 +00006570 if (Args.hasArg(options::OPT_static)) {
6571 CmdArgs.push_back("-Bstatic");
6572 } else {
Rafael Espindola65ba55d2010-11-11 02:17:51 +00006573 if (Args.hasArg(options::OPT_rdynamic))
6574 CmdArgs.push_back("-export-dynamic");
Daniel Dunbar008f54a2009-04-01 19:36:32 +00006575 CmdArgs.push_back("--eh-frame-hdr");
6576 if (Args.hasArg(options::OPT_shared)) {
6577 CmdArgs.push_back("-Bshareable");
6578 } else {
6579 CmdArgs.push_back("-dynamic-linker");
6580 CmdArgs.push_back("/libexec/ld-elf.so.1");
6581 }
Roman Divacky94380162012-08-28 15:09:03 +00006582 if (ToolChain.getTriple().getOSMajorVersion() >= 9) {
6583 llvm::Triple::ArchType Arch = ToolChain.getArch();
David Chisnalldfa210b2012-07-29 15:24:44 +00006584 if (Arch == llvm::Triple::arm || Arch == llvm::Triple::sparc ||
6585 Arch == llvm::Triple::x86 || Arch == llvm::Triple::x86_64) {
6586 CmdArgs.push_back("--hash-style=both");
6587 }
6588 }
6589 CmdArgs.push_back("--enable-new-dtags");
Daniel Dunbar008f54a2009-04-01 19:36:32 +00006590 }
6591
6592 // When building 32-bit code on FreeBSD/amd64, we have to explicitly
6593 // instruct ld in the base system to link 32-bit code.
Rafael Espindola64f7ad92012-10-07 04:44:33 +00006594 if (ToolChain.getArch() == llvm::Triple::x86) {
Daniel Dunbar008f54a2009-04-01 19:36:32 +00006595 CmdArgs.push_back("-m");
6596 CmdArgs.push_back("elf_i386_fbsd");
6597 }
6598
Rafael Espindola64f7ad92012-10-07 04:44:33 +00006599 if (ToolChain.getArch() == llvm::Triple::ppc) {
Roman Divacky000a6552011-06-04 07:40:24 +00006600 CmdArgs.push_back("-m");
Roman Divacky1052c1d2011-11-21 16:50:32 +00006601 CmdArgs.push_back("elf32ppc_fbsd");
Roman Divacky000a6552011-06-04 07:40:24 +00006602 }
6603
Daniel Dunbar7c1e4652010-08-02 02:38:21 +00006604 if (Output.isFilename()) {
Daniel Dunbar008f54a2009-04-01 19:36:32 +00006605 CmdArgs.push_back("-o");
6606 CmdArgs.push_back(Output.getFilename());
6607 } else {
6608 assert(Output.isNothing() && "Invalid output.");
6609 }
6610
6611 if (!Args.hasArg(options::OPT_nostdlib) &&
6612 !Args.hasArg(options::OPT_nostartfiles)) {
Stephen Hines6bcf27b2014-05-29 04:14:42 -07006613 const char *crt1 = nullptr;
Daniel Dunbar008f54a2009-04-01 19:36:32 +00006614 if (!Args.hasArg(options::OPT_shared)) {
Roman Divackyc16bb762011-02-10 16:59:40 +00006615 if (Args.hasArg(options::OPT_pg))
Roman Divacky94380162012-08-28 15:09:03 +00006616 crt1 = "gcrt1.o";
Stephen Hines651f13c2014-04-23 16:59:28 -07006617 else if (IsPIE)
Roman Divacky94380162012-08-28 15:09:03 +00006618 crt1 = "Scrt1.o";
6619 else
6620 crt1 = "crt1.o";
Daniel Dunbar008f54a2009-04-01 19:36:32 +00006621 }
Roman Divacky94380162012-08-28 15:09:03 +00006622 if (crt1)
6623 CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath(crt1)));
6624
6625 CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath("crti.o")));
6626
Stephen Hines6bcf27b2014-05-29 04:14:42 -07006627 const char *crtbegin = nullptr;
Roman Divacky94380162012-08-28 15:09:03 +00006628 if (Args.hasArg(options::OPT_static))
6629 crtbegin = "crtbeginT.o";
Stephen Hines651f13c2014-04-23 16:59:28 -07006630 else if (Args.hasArg(options::OPT_shared) || IsPIE)
Roman Divacky94380162012-08-28 15:09:03 +00006631 crtbegin = "crtbeginS.o";
6632 else
6633 crtbegin = "crtbegin.o";
6634
6635 CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath(crtbegin)));
Daniel Dunbar008f54a2009-04-01 19:36:32 +00006636 }
6637
6638 Args.AddAllArgs(CmdArgs, options::OPT_L);
Stephen Hines176edba2014-12-01 14:53:08 -08006639 const ToolChain::path_list &Paths = ToolChain.getFilePaths();
Stephen Hinesef822542014-07-21 00:47:37 -07006640 for (const auto &Path : Paths)
6641 CmdArgs.push_back(Args.MakeArgString(StringRef("-L") + Path));
Daniel Dunbar008f54a2009-04-01 19:36:32 +00006642 Args.AddAllArgs(CmdArgs, options::OPT_T_Group);
6643 Args.AddAllArgs(CmdArgs, options::OPT_e);
David Chisnallc7363772010-08-15 22:58:12 +00006644 Args.AddAllArgs(CmdArgs, options::OPT_s);
6645 Args.AddAllArgs(CmdArgs, options::OPT_t);
6646 Args.AddAllArgs(CmdArgs, options::OPT_Z_Flag);
6647 Args.AddAllArgs(CmdArgs, options::OPT_r);
Daniel Dunbar008f54a2009-04-01 19:36:32 +00006648
Stephen Hines651f13c2014-04-23 16:59:28 -07006649 if (D.IsUsingLTO(Args))
6650 AddGoldPlugin(ToolChain, Args, CmdArgs);
Roman Divackydb334192013-11-10 09:31:43 +00006651
Stephen Hines176edba2014-12-01 14:53:08 -08006652 bool NeedsSanitizerDeps = addSanitizerRuntimes(ToolChain, Args, CmdArgs);
Roman Divacky94380162012-08-28 15:09:03 +00006653 AddLinkerInputs(ToolChain, Inputs, Args, CmdArgs);
Daniel Dunbar008f54a2009-04-01 19:36:32 +00006654
6655 if (!Args.hasArg(options::OPT_nostdlib) &&
6656 !Args.hasArg(options::OPT_nodefaultlibs)) {
Hans Wennborg76b86c22013-07-18 20:29:38 +00006657 if (D.CCCIsCXX()) {
Roman Divacky94380162012-08-28 15:09:03 +00006658 ToolChain.AddCXXStdlibLibArgs(Args, CmdArgs);
Roman Divackyc16bb762011-02-10 16:59:40 +00006659 if (Args.hasArg(options::OPT_pg))
6660 CmdArgs.push_back("-lm_p");
6661 else
6662 CmdArgs.push_back("-lm");
Daniel Dunbar20022632010-02-17 08:07:51 +00006663 }
Stephen Hines176edba2014-12-01 14:53:08 -08006664 if (NeedsSanitizerDeps)
6665 linkSanitizerRuntimeDeps(ToolChain, CmdArgs);
Daniel Dunbar008f54a2009-04-01 19:36:32 +00006666 // FIXME: For some reason GCC passes -lgcc and -lgcc_s before adding
6667 // the default system libraries. Just mimic this for now.
Roman Divackyc16bb762011-02-10 16:59:40 +00006668 if (Args.hasArg(options::OPT_pg))
6669 CmdArgs.push_back("-lgcc_p");
6670 else
6671 CmdArgs.push_back("-lgcc");
Daniel Dunbar008f54a2009-04-01 19:36:32 +00006672 if (Args.hasArg(options::OPT_static)) {
6673 CmdArgs.push_back("-lgcc_eh");
Roman Divackyc16bb762011-02-10 16:59:40 +00006674 } else if (Args.hasArg(options::OPT_pg)) {
6675 CmdArgs.push_back("-lgcc_eh_p");
Daniel Dunbar008f54a2009-04-01 19:36:32 +00006676 } else {
6677 CmdArgs.push_back("--as-needed");
6678 CmdArgs.push_back("-lgcc_s");
6679 CmdArgs.push_back("--no-as-needed");
6680 }
6681
Matt Beaumont-Gay24230262011-02-10 20:35:01 +00006682 if (Args.hasArg(options::OPT_pthread)) {
Roman Divackyc16bb762011-02-10 16:59:40 +00006683 if (Args.hasArg(options::OPT_pg))
6684 CmdArgs.push_back("-lpthread_p");
6685 else
6686 CmdArgs.push_back("-lpthread");
Matt Beaumont-Gay24230262011-02-10 20:35:01 +00006687 }
Daniel Dunbar008f54a2009-04-01 19:36:32 +00006688
Roman Divackyc16bb762011-02-10 16:59:40 +00006689 if (Args.hasArg(options::OPT_pg)) {
6690 if (Args.hasArg(options::OPT_shared))
6691 CmdArgs.push_back("-lc");
6692 else
6693 CmdArgs.push_back("-lc_p");
6694 CmdArgs.push_back("-lgcc_p");
6695 } else {
6696 CmdArgs.push_back("-lc");
6697 CmdArgs.push_back("-lgcc");
6698 }
6699
Daniel Dunbar008f54a2009-04-01 19:36:32 +00006700 if (Args.hasArg(options::OPT_static)) {
6701 CmdArgs.push_back("-lgcc_eh");
Roman Divackyc16bb762011-02-10 16:59:40 +00006702 } else if (Args.hasArg(options::OPT_pg)) {
6703 CmdArgs.push_back("-lgcc_eh_p");
Daniel Dunbar008f54a2009-04-01 19:36:32 +00006704 } else {
6705 CmdArgs.push_back("--as-needed");
6706 CmdArgs.push_back("-lgcc_s");
6707 CmdArgs.push_back("--no-as-needed");
6708 }
6709 }
6710
6711 if (!Args.hasArg(options::OPT_nostdlib) &&
6712 !Args.hasArg(options::OPT_nostartfiles)) {
Stephen Hines651f13c2014-04-23 16:59:28 -07006713 if (Args.hasArg(options::OPT_shared) || IsPIE)
Roman Divacky94380162012-08-28 15:09:03 +00006714 CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath("crtendS.o")));
Roman Divackyf6513812012-09-07 13:36:21 +00006715 else
6716 CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath("crtend.o")));
Roman Divacky94380162012-08-28 15:09:03 +00006717 CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath("crtn.o")));
Daniel Dunbar008f54a2009-04-01 19:36:32 +00006718 }
6719
Stephen Hines651f13c2014-04-23 16:59:28 -07006720 addProfileRT(ToolChain, Args, CmdArgs);
Nick Lewycky2e95a6d2011-05-24 21:54:59 +00006721
Daniel Dunbarc21c4852009-04-08 23:54:23 +00006722 const char *Exec =
Stephen Hinesef822542014-07-21 00:47:37 -07006723 Args.MakeArgString(getToolChain().GetLinkerPath());
Stephen Hines176edba2014-12-01 14:53:08 -08006724 C.addCommand(llvm::make_unique<Command>(JA, *this, Exec, CmdArgs));
Daniel Dunbar008f54a2009-04-01 19:36:32 +00006725}
Daniel Dunbar11e1b402009-05-02 18:28:39 +00006726
Benjamin Kramer8e50a962011-02-02 18:59:27 +00006727void netbsd::Assemble::ConstructJob(Compilation &C, const JobAction &JA,
6728 const InputInfo &Output,
6729 const InputInfoList &Inputs,
6730 const ArgList &Args,
6731 const char *LinkingOutput) const {
6732 ArgStringList CmdArgs;
6733
Stephen Hines651f13c2014-04-23 16:59:28 -07006734 // GNU as needs different flags for creating the correct output format
6735 // on architectures with different ABIs or optional feature sets.
6736 switch (getToolChain().getArch()) {
6737 case llvm::Triple::x86:
Benjamin Kramer8e50a962011-02-02 18:59:27 +00006738 CmdArgs.push_back("--32");
Stephen Hines651f13c2014-04-23 16:59:28 -07006739 break;
6740 case llvm::Triple::arm:
6741 case llvm::Triple::armeb:
6742 case llvm::Triple::thumb:
6743 case llvm::Triple::thumbeb: {
6744 std::string MArch(arm::getARMTargetCPU(Args, getToolChain().getTriple()));
Bill Wendlingcf660bd2013-12-06 19:12:36 +00006745 CmdArgs.push_back(Args.MakeArgString("-mcpu=" + MArch));
Stephen Hines651f13c2014-04-23 16:59:28 -07006746 break;
Bill Wendlingcf660bd2013-12-06 19:12:36 +00006747 }
6748
Stephen Hines651f13c2014-04-23 16:59:28 -07006749 case llvm::Triple::mips:
6750 case llvm::Triple::mipsel:
6751 case llvm::Triple::mips64:
6752 case llvm::Triple::mips64el: {
Bill Wendlingc54c8852013-12-09 02:59:27 +00006753 StringRef CPUName;
6754 StringRef ABIName;
Stephen Hines176edba2014-12-01 14:53:08 -08006755 mips::getMipsCPUAndABI(Args, getToolChain().getTriple(), CPUName, ABIName);
Bill Wendlingc54c8852013-12-09 02:59:27 +00006756
6757 CmdArgs.push_back("-march");
6758 CmdArgs.push_back(CPUName.data());
6759
6760 CmdArgs.push_back("-mabi");
6761 CmdArgs.push_back(getGnuCompatibleMipsABIName(ABIName).data());
6762
6763 if (getToolChain().getArch() == llvm::Triple::mips ||
6764 getToolChain().getArch() == llvm::Triple::mips64)
6765 CmdArgs.push_back("-EB");
6766 else
6767 CmdArgs.push_back("-EL");
6768
Stephen Hines651f13c2014-04-23 16:59:28 -07006769 addAssemblerKPIC(Args, CmdArgs);
6770 break;
6771 }
6772
6773 case llvm::Triple::sparc:
6774 CmdArgs.push_back("-32");
6775 addAssemblerKPIC(Args, CmdArgs);
6776 break;
6777
6778 case llvm::Triple::sparcv9:
6779 CmdArgs.push_back("-64");
6780 CmdArgs.push_back("-Av9");
6781 addAssemblerKPIC(Args, CmdArgs);
6782 break;
6783
6784 default:
6785 break;
Bill Wendlingc54c8852013-12-09 02:59:27 +00006786 }
Benjamin Kramer8e50a962011-02-02 18:59:27 +00006787
6788 Args.AddAllArgValues(CmdArgs, options::OPT_Wa_COMMA,
6789 options::OPT_Xassembler);
6790
6791 CmdArgs.push_back("-o");
6792 CmdArgs.push_back(Output.getFilename());
6793
Stephen Hinesef822542014-07-21 00:47:37 -07006794 for (const auto &II : Inputs)
Benjamin Kramer8e50a962011-02-02 18:59:27 +00006795 CmdArgs.push_back(II.getFilename());
Benjamin Kramer8e50a962011-02-02 18:59:27 +00006796
David Chisnall5adcec12011-09-27 22:03:18 +00006797 const char *Exec = Args.MakeArgString((getToolChain().GetProgramPath("as")));
Stephen Hines176edba2014-12-01 14:53:08 -08006798 C.addCommand(llvm::make_unique<Command>(JA, *this, Exec, CmdArgs));
Benjamin Kramer8e50a962011-02-02 18:59:27 +00006799}
6800
6801void netbsd::Link::ConstructJob(Compilation &C, const JobAction &JA,
6802 const InputInfo &Output,
6803 const InputInfoList &Inputs,
6804 const ArgList &Args,
6805 const char *LinkingOutput) const {
6806 const Driver &D = getToolChain().getDriver();
6807 ArgStringList CmdArgs;
6808
Joerg Sonnenberger8ab2bdc2011-03-21 13:51:29 +00006809 if (!D.SysRoot.empty())
6810 CmdArgs.push_back(Args.MakeArgString("--sysroot=" + D.SysRoot));
6811
Stephen Hines651f13c2014-04-23 16:59:28 -07006812 CmdArgs.push_back("--eh-frame-hdr");
Benjamin Kramer8e50a962011-02-02 18:59:27 +00006813 if (Args.hasArg(options::OPT_static)) {
6814 CmdArgs.push_back("-Bstatic");
6815 } else {
6816 if (Args.hasArg(options::OPT_rdynamic))
6817 CmdArgs.push_back("-export-dynamic");
Benjamin Kramer8e50a962011-02-02 18:59:27 +00006818 if (Args.hasArg(options::OPT_shared)) {
6819 CmdArgs.push_back("-Bshareable");
6820 } else {
6821 CmdArgs.push_back("-dynamic-linker");
6822 CmdArgs.push_back("/libexec/ld.elf_so");
6823 }
6824 }
6825
Stephen Hines651f13c2014-04-23 16:59:28 -07006826 // Many NetBSD architectures support more than one ABI.
6827 // Determine the correct emulation for ld.
6828 switch (getToolChain().getArch()) {
6829 case llvm::Triple::x86:
Benjamin Kramer8e50a962011-02-02 18:59:27 +00006830 CmdArgs.push_back("-m");
6831 CmdArgs.push_back("elf_i386");
Stephen Hines651f13c2014-04-23 16:59:28 -07006832 break;
6833 case llvm::Triple::arm:
Stephen Hines651f13c2014-04-23 16:59:28 -07006834 case llvm::Triple::thumb:
Stephen Hines651f13c2014-04-23 16:59:28 -07006835 CmdArgs.push_back("-m");
6836 switch (getToolChain().getTriple().getEnvironment()) {
6837 case llvm::Triple::EABI:
6838 case llvm::Triple::GNUEABI:
6839 CmdArgs.push_back("armelf_nbsd_eabi");
6840 break;
6841 case llvm::Triple::EABIHF:
6842 case llvm::Triple::GNUEABIHF:
6843 CmdArgs.push_back("armelf_nbsd_eabihf");
6844 break;
6845 default:
6846 CmdArgs.push_back("armelf_nbsd");
6847 break;
6848 }
6849 break;
Stephen Hines176edba2014-12-01 14:53:08 -08006850 case llvm::Triple::armeb:
6851 case llvm::Triple::thumbeb:
6852 CmdArgs.push_back("-m");
6853 switch (getToolChain().getTriple().getEnvironment()) {
6854 case llvm::Triple::EABI:
6855 case llvm::Triple::GNUEABI:
6856 CmdArgs.push_back("armelfb_nbsd_eabi");
6857 break;
6858 case llvm::Triple::EABIHF:
6859 case llvm::Triple::GNUEABIHF:
6860 CmdArgs.push_back("armelfb_nbsd_eabihf");
6861 break;
6862 default:
6863 CmdArgs.push_back("armelfb_nbsd");
6864 break;
6865 }
6866 break;
Stephen Hines651f13c2014-04-23 16:59:28 -07006867 case llvm::Triple::mips64:
6868 case llvm::Triple::mips64el:
6869 if (mips::hasMipsAbiArg(Args, "32")) {
6870 CmdArgs.push_back("-m");
6871 if (getToolChain().getArch() == llvm::Triple::mips64)
6872 CmdArgs.push_back("elf32btsmip");
6873 else
6874 CmdArgs.push_back("elf32ltsmip");
6875 } else if (mips::hasMipsAbiArg(Args, "64")) {
6876 CmdArgs.push_back("-m");
6877 if (getToolChain().getArch() == llvm::Triple::mips64)
6878 CmdArgs.push_back("elf64btsmip");
6879 else
6880 CmdArgs.push_back("elf64ltsmip");
6881 }
6882 break;
Stephen Hines176edba2014-12-01 14:53:08 -08006883 case llvm::Triple::ppc:
6884 CmdArgs.push_back("-m");
6885 CmdArgs.push_back("elf32ppc_nbsd");
6886 break;
6887
6888 case llvm::Triple::ppc64:
6889 case llvm::Triple::ppc64le:
6890 CmdArgs.push_back("-m");
6891 CmdArgs.push_back("elf64ppc");
6892 break;
Stephen Hines651f13c2014-04-23 16:59:28 -07006893
6894 case llvm::Triple::sparc:
6895 CmdArgs.push_back("-m");
6896 CmdArgs.push_back("elf32_sparc");
6897 break;
6898
6899 case llvm::Triple::sparcv9:
6900 CmdArgs.push_back("-m");
6901 CmdArgs.push_back("elf64_sparc");
6902 break;
6903
6904 default:
6905 break;
Benjamin Kramer8e50a962011-02-02 18:59:27 +00006906 }
6907
6908 if (Output.isFilename()) {
6909 CmdArgs.push_back("-o");
6910 CmdArgs.push_back(Output.getFilename());
6911 } else {
6912 assert(Output.isNothing() && "Invalid output.");
6913 }
6914
6915 if (!Args.hasArg(options::OPT_nostdlib) &&
6916 !Args.hasArg(options::OPT_nostartfiles)) {
6917 if (!Args.hasArg(options::OPT_shared)) {
6918 CmdArgs.push_back(Args.MakeArgString(
6919 getToolChain().GetFilePath("crt0.o")));
6920 CmdArgs.push_back(Args.MakeArgString(
6921 getToolChain().GetFilePath("crti.o")));
6922 CmdArgs.push_back(Args.MakeArgString(
6923 getToolChain().GetFilePath("crtbegin.o")));
6924 } else {
6925 CmdArgs.push_back(Args.MakeArgString(
6926 getToolChain().GetFilePath("crti.o")));
6927 CmdArgs.push_back(Args.MakeArgString(
6928 getToolChain().GetFilePath("crtbeginS.o")));
6929 }
6930 }
6931
6932 Args.AddAllArgs(CmdArgs, options::OPT_L);
6933 Args.AddAllArgs(CmdArgs, options::OPT_T_Group);
6934 Args.AddAllArgs(CmdArgs, options::OPT_e);
6935 Args.AddAllArgs(CmdArgs, options::OPT_s);
6936 Args.AddAllArgs(CmdArgs, options::OPT_t);
6937 Args.AddAllArgs(CmdArgs, options::OPT_Z_Flag);
6938 Args.AddAllArgs(CmdArgs, options::OPT_r);
6939
6940 AddLinkerInputs(getToolChain(), Inputs, Args, CmdArgs);
6941
Joerg Sonnenbergere69cca42013-10-14 20:13:05 +00006942 unsigned Major, Minor, Micro;
6943 getToolChain().getTriple().getOSVersion(Major, Minor, Micro);
6944 bool useLibgcc = true;
Stephen Hines176edba2014-12-01 14:53:08 -08006945 if (Major >= 7 || (Major == 6 && Minor == 99 && Micro >= 49) || Major == 0) {
Stephen Hines651f13c2014-04-23 16:59:28 -07006946 switch(getToolChain().getArch()) {
Stephen Hines176edba2014-12-01 14:53:08 -08006947 case llvm::Triple::aarch64:
Stephen Hines6bcf27b2014-05-29 04:14:42 -07006948 case llvm::Triple::arm:
6949 case llvm::Triple::armeb:
6950 case llvm::Triple::thumb:
6951 case llvm::Triple::thumbeb:
Stephen Hines176edba2014-12-01 14:53:08 -08006952 case llvm::Triple::ppc:
6953 case llvm::Triple::ppc64:
6954 case llvm::Triple::ppc64le:
Stephen Hines651f13c2014-04-23 16:59:28 -07006955 case llvm::Triple::x86:
6956 case llvm::Triple::x86_64:
Joerg Sonnenbergere69cca42013-10-14 20:13:05 +00006957 useLibgcc = false;
Stephen Hines651f13c2014-04-23 16:59:28 -07006958 break;
6959 default:
6960 break;
6961 }
Joerg Sonnenbergere69cca42013-10-14 20:13:05 +00006962 }
6963
Benjamin Kramer8e50a962011-02-02 18:59:27 +00006964 if (!Args.hasArg(options::OPT_nostdlib) &&
6965 !Args.hasArg(options::OPT_nodefaultlibs)) {
Hans Wennborg76b86c22013-07-18 20:29:38 +00006966 if (D.CCCIsCXX()) {
Benjamin Kramer8e50a962011-02-02 18:59:27 +00006967 getToolChain().AddCXXStdlibLibArgs(Args, CmdArgs);
6968 CmdArgs.push_back("-lm");
6969 }
Benjamin Kramer8e50a962011-02-02 18:59:27 +00006970 if (Args.hasArg(options::OPT_pthread))
6971 CmdArgs.push_back("-lpthread");
6972 CmdArgs.push_back("-lc");
6973
Joerg Sonnenbergere69cca42013-10-14 20:13:05 +00006974 if (useLibgcc) {
6975 if (Args.hasArg(options::OPT_static)) {
6976 // libgcc_eh depends on libc, so resolve as much as possible,
6977 // pull in any new requirements from libc and then get the rest
6978 // of libgcc.
6979 CmdArgs.push_back("-lgcc_eh");
6980 CmdArgs.push_back("-lc");
6981 CmdArgs.push_back("-lgcc");
6982 } else {
6983 CmdArgs.push_back("-lgcc");
6984 CmdArgs.push_back("--as-needed");
6985 CmdArgs.push_back("-lgcc_s");
6986 CmdArgs.push_back("--no-as-needed");
6987 }
Benjamin Kramer8e50a962011-02-02 18:59:27 +00006988 }
6989 }
6990
6991 if (!Args.hasArg(options::OPT_nostdlib) &&
6992 !Args.hasArg(options::OPT_nostartfiles)) {
6993 if (!Args.hasArg(options::OPT_shared))
6994 CmdArgs.push_back(Args.MakeArgString(getToolChain().GetFilePath(
6995 "crtend.o")));
6996 else
6997 CmdArgs.push_back(Args.MakeArgString(getToolChain().GetFilePath(
6998 "crtendS.o")));
6999 CmdArgs.push_back(Args.MakeArgString(getToolChain().GetFilePath(
7000 "crtn.o")));
7001 }
7002
Stephen Hines651f13c2014-04-23 16:59:28 -07007003 addProfileRT(getToolChain(), Args, CmdArgs);
Nick Lewycky2e95a6d2011-05-24 21:54:59 +00007004
Stephen Hinesef822542014-07-21 00:47:37 -07007005 const char *Exec = Args.MakeArgString(getToolChain().GetLinkerPath());
Stephen Hines176edba2014-12-01 14:53:08 -08007006 C.addCommand(llvm::make_unique<Command>(JA, *this, Exec, CmdArgs));
Benjamin Kramer8e50a962011-02-02 18:59:27 +00007007}
7008
Thomas Schwinge577bb0a2013-03-28 19:04:25 +00007009void gnutools::Assemble::ConstructJob(Compilation &C, const JobAction &JA,
7010 const InputInfo &Output,
7011 const InputInfoList &Inputs,
7012 const ArgList &Args,
7013 const char *LinkingOutput) const {
Rafael Espindolaba30bbe2010-08-10 00:25:48 +00007014 ArgStringList CmdArgs;
Stephen Hines651f13c2014-04-23 16:59:28 -07007015 bool NeedsKPIC = false;
Rafael Espindolaba30bbe2010-08-10 00:25:48 +00007016
7017 // Add --32/--64 to make sure we get the format we want.
7018 // This is incomplete
7019 if (getToolChain().getArch() == llvm::Triple::x86) {
7020 CmdArgs.push_back("--32");
7021 } else if (getToolChain().getArch() == llvm::Triple::x86_64) {
Stephen Hinesef822542014-07-21 00:47:37 -07007022 if (getToolChain().getTriple().getEnvironment() == llvm::Triple::GNUX32)
7023 CmdArgs.push_back("--x32");
7024 else
7025 CmdArgs.push_back("--64");
Eli Friedman7972c882011-11-28 23:46:52 +00007026 } else if (getToolChain().getArch() == llvm::Triple::ppc) {
7027 CmdArgs.push_back("-a32");
7028 CmdArgs.push_back("-mppc");
7029 CmdArgs.push_back("-many");
7030 } else if (getToolChain().getArch() == llvm::Triple::ppc64) {
7031 CmdArgs.push_back("-a64");
7032 CmdArgs.push_back("-mppc64");
7033 CmdArgs.push_back("-many");
Bill Schmidtea7fb0c2013-07-26 01:36:11 +00007034 } else if (getToolChain().getArch() == llvm::Triple::ppc64le) {
7035 CmdArgs.push_back("-a64");
Stephen Hines651f13c2014-04-23 16:59:28 -07007036 CmdArgs.push_back("-mppc64");
Bill Schmidtea7fb0c2013-07-26 01:36:11 +00007037 CmdArgs.push_back("-many");
Stephen Hines651f13c2014-04-23 16:59:28 -07007038 CmdArgs.push_back("-mlittle-endian");
7039 } else if (getToolChain().getArch() == llvm::Triple::sparc) {
7040 CmdArgs.push_back("-32");
7041 CmdArgs.push_back("-Av8plusa");
7042 NeedsKPIC = true;
7043 } else if (getToolChain().getArch() == llvm::Triple::sparcv9) {
7044 CmdArgs.push_back("-64");
7045 CmdArgs.push_back("-Av9a");
7046 NeedsKPIC = true;
7047 } else if (getToolChain().getArch() == llvm::Triple::arm ||
7048 getToolChain().getArch() == llvm::Triple::armeb) {
Chris Lattner5f9e2722011-07-23 10:55:15 +00007049 StringRef MArch = getToolChain().getArchName();
Rafael Espindolaba30bbe2010-08-10 00:25:48 +00007050 if (MArch == "armv7" || MArch == "armv7a" || MArch == "armv7-a")
7051 CmdArgs.push_back("-mfpu=neon");
Stephen Hines651f13c2014-04-23 16:59:28 -07007052 if (MArch == "armv8" || MArch == "armv8a" || MArch == "armv8-a" ||
7053 MArch == "armebv8" || MArch == "armebv8a" || MArch == "armebv8-a")
Bernard Ogden80e90c22013-10-24 18:32:41 +00007054 CmdArgs.push_back("-mfpu=crypto-neon-fp-armv8");
Evgeniy Stepanov700c5082012-04-20 09:03:40 +00007055
Stephen Hines651f13c2014-04-23 16:59:28 -07007056 StringRef ARMFloatABI = tools::arm::getARMFloatABI(
7057 getToolChain().getDriver(), Args, getToolChain().getTriple());
Evgeniy Stepanov700c5082012-04-20 09:03:40 +00007058 CmdArgs.push_back(Args.MakeArgString("-mfloat-abi=" + ARMFloatABI));
Evgeniy Stepanoveca187e2012-04-24 09:05:31 +00007059
7060 Args.AddLastArg(CmdArgs, options::OPT_march_EQ);
Stephen Hines651f13c2014-04-23 16:59:28 -07007061
7062 // FIXME: remove krait check when GNU tools support krait cpu
7063 // for now replace it with -march=armv7-a to avoid a lower
7064 // march from being picked in the absence of a cpu flag.
7065 Arg *A;
7066 if ((A = Args.getLastArg(options::OPT_mcpu_EQ)) &&
7067 StringRef(A->getValue()) == "krait")
7068 CmdArgs.push_back("-march=armv7-a");
7069 else
7070 Args.AddLastArg(CmdArgs, options::OPT_mcpu_EQ);
Evgeniy Stepanoveca187e2012-04-24 09:05:31 +00007071 Args.AddLastArg(CmdArgs, options::OPT_mfpu_EQ);
Akira Hatanakac85900f2011-11-30 19:31:38 +00007072 } else if (getToolChain().getArch() == llvm::Triple::mips ||
7073 getToolChain().getArch() == llvm::Triple::mipsel ||
7074 getToolChain().getArch() == llvm::Triple::mips64 ||
7075 getToolChain().getArch() == llvm::Triple::mips64el) {
Simon Atanasyan073a7802012-04-07 22:31:29 +00007076 StringRef CPUName;
7077 StringRef ABIName;
Stephen Hines176edba2014-12-01 14:53:08 -08007078 mips::getMipsCPUAndABI(Args, getToolChain().getTriple(), CPUName, ABIName);
7079 ABIName = getGnuCompatibleMipsABIName(ABIName);
Akira Hatanakac85900f2011-11-30 19:31:38 +00007080
Simon Atanasyan073a7802012-04-07 22:31:29 +00007081 CmdArgs.push_back("-march");
7082 CmdArgs.push_back(CPUName.data());
7083
Simon Atanasyan073a7802012-04-07 22:31:29 +00007084 CmdArgs.push_back("-mabi");
Stephen Hines176edba2014-12-01 14:53:08 -08007085 CmdArgs.push_back(ABIName.data());
7086
7087 // -mno-shared should be emitted unless -fpic, -fpie, -fPIC, -fPIE,
7088 // or -mshared (not implemented) is in effect.
7089 bool IsPicOrPie = false;
7090 if (Arg *A = Args.getLastArg(options::OPT_fPIC, options::OPT_fno_PIC,
7091 options::OPT_fpic, options::OPT_fno_pic,
7092 options::OPT_fPIE, options::OPT_fno_PIE,
7093 options::OPT_fpie, options::OPT_fno_pie)) {
7094 if (A->getOption().matches(options::OPT_fPIC) ||
7095 A->getOption().matches(options::OPT_fpic) ||
7096 A->getOption().matches(options::OPT_fPIE) ||
7097 A->getOption().matches(options::OPT_fpie))
7098 IsPicOrPie = true;
7099 }
7100 if (!IsPicOrPie)
7101 CmdArgs.push_back("-mno-shared");
7102
7103 // LLVM doesn't support -mplt yet and acts as if it is always given.
7104 // However, -mplt has no effect with the N64 ABI.
7105 CmdArgs.push_back(ABIName == "64" ? "-KPIC" : "-call_nonpic");
Simon Atanasyan5f0a1c12012-04-06 19:15:24 +00007106
7107 if (getToolChain().getArch() == llvm::Triple::mips ||
7108 getToolChain().getArch() == llvm::Triple::mips64)
7109 CmdArgs.push_back("-EB");
7110 else
7111 CmdArgs.push_back("-EL");
Simon Atanasyan1f0646e2012-05-29 19:07:33 +00007112
Simon Atanasyanfc12c4a2013-09-24 09:09:16 +00007113 if (Arg *A = Args.getLastArg(options::OPT_mnan_EQ)) {
7114 if (StringRef(A->getValue()) == "2008")
7115 CmdArgs.push_back(Args.MakeArgString("-mnan=2008"));
7116 }
7117
Stephen Hines176edba2014-12-01 14:53:08 -08007118 // Add the last -mfp32/-mfpxx/-mfp64 or -mfpxx if it is enabled by default.
7119 if (Arg *A = Args.getLastArg(options::OPT_mfp32, options::OPT_mfpxx,
7120 options::OPT_mfp64)) {
7121 A->claim();
7122 A->render(Args, CmdArgs);
7123 } else if (mips::isFPXXDefault(getToolChain().getTriple(), CPUName,
7124 ABIName))
7125 CmdArgs.push_back("-mfpxx");
7126
7127 // Pass on -mmips16 or -mno-mips16. However, the assembler equivalent of
7128 // -mno-mips16 is actually -no-mips16.
7129 if (Arg *A = Args.getLastArg(options::OPT_mips16,
7130 options::OPT_mno_mips16)) {
7131 if (A->getOption().matches(options::OPT_mips16)) {
7132 A->claim();
7133 A->render(Args, CmdArgs);
7134 } else {
7135 A->claim();
7136 CmdArgs.push_back("-no-mips16");
7137 }
7138 }
7139
Simon Atanasyan9dbfc612013-04-30 07:47:13 +00007140 Args.AddLastArg(CmdArgs, options::OPT_mmicromips,
7141 options::OPT_mno_micromips);
7142 Args.AddLastArg(CmdArgs, options::OPT_mdsp, options::OPT_mno_dsp);
7143 Args.AddLastArg(CmdArgs, options::OPT_mdspr2, options::OPT_mno_dspr2);
7144
Daniel Sanders0d5d6ff2013-12-02 10:14:43 +00007145 if (Arg *A = Args.getLastArg(options::OPT_mmsa, options::OPT_mno_msa)) {
7146 // Do not use AddLastArg because not all versions of MIPS assembler
7147 // support -mmsa / -mno-msa options.
7148 if (A->getOption().matches(options::OPT_mmsa))
7149 CmdArgs.push_back(Args.MakeArgString("-mmsa"));
7150 }
7151
Stephen Hines176edba2014-12-01 14:53:08 -08007152 Args.AddLastArg(CmdArgs, options::OPT_mhard_float,
7153 options::OPT_msoft_float);
7154
7155 Args.AddLastArg(CmdArgs, options::OPT_modd_spreg,
7156 options::OPT_mno_odd_spreg);
7157
Stephen Hines651f13c2014-04-23 16:59:28 -07007158 NeedsKPIC = true;
Ulrich Weigandb8409212013-05-06 16:26:41 +00007159 } else if (getToolChain().getArch() == llvm::Triple::systemz) {
Richard Sandiford5c92b9a2013-07-19 16:51:51 +00007160 // Always pass an -march option, since our default of z10 is later
7161 // than the GNU assembler's default.
7162 StringRef CPUName = getSystemZTargetCPU(Args);
7163 CmdArgs.push_back(Args.MakeArgString("-march=" + CPUName));
Rafael Espindolaba30bbe2010-08-10 00:25:48 +00007164 }
7165
Stephen Hines651f13c2014-04-23 16:59:28 -07007166 if (NeedsKPIC)
7167 addAssemblerKPIC(Args, CmdArgs);
7168
Rafael Espindolaba30bbe2010-08-10 00:25:48 +00007169 Args.AddAllArgValues(CmdArgs, options::OPT_Wa_COMMA,
7170 options::OPT_Xassembler);
7171
7172 CmdArgs.push_back("-o");
7173 CmdArgs.push_back(Output.getFilename());
7174
Stephen Hinesef822542014-07-21 00:47:37 -07007175 for (const auto &II : Inputs)
Rafael Espindolaba30bbe2010-08-10 00:25:48 +00007176 CmdArgs.push_back(II.getFilename());
Rafael Espindolaba30bbe2010-08-10 00:25:48 +00007177
Stephen Hinesef822542014-07-21 00:47:37 -07007178 const char *Exec = Args.MakeArgString(getToolChain().GetProgramPath("as"));
Stephen Hines176edba2014-12-01 14:53:08 -08007179 C.addCommand(llvm::make_unique<Command>(JA, *this, Exec, CmdArgs));
Eric Christopherc47b6f32013-06-05 23:58:15 +00007180
7181 // Handle the debug info splitting at object creation time if we're
7182 // creating an object.
7183 // TODO: Currently only works on linux with newer objcopy.
7184 if (Args.hasArg(options::OPT_gsplit_dwarf) &&
Cameron Esfahani57b1da12013-09-14 01:09:11 +00007185 getToolChain().getTriple().isOSLinux())
Eric Christopherc47b6f32013-06-05 23:58:15 +00007186 SplitDebugInfo(getToolChain(), C, *this, JA, Args, Output,
7187 SplitDebugName(Args, Inputs));
Rafael Espindolaba30bbe2010-08-10 00:25:48 +00007188}
7189
Stephen Hines651f13c2014-04-23 16:59:28 -07007190static void AddLibgcc(const llvm::Triple &Triple, const Driver &D,
Evgeniy Stepanova6ddc022012-04-25 08:59:22 +00007191 ArgStringList &CmdArgs, const ArgList &Args) {
Logan Chien94a71422012-09-02 09:30:11 +00007192 bool isAndroid = Triple.getEnvironment() == llvm::Triple::Android;
Chandler Carruth68f94db2013-03-04 02:07:55 +00007193 bool StaticLibgcc = Args.hasArg(options::OPT_static_libgcc) ||
7194 Args.hasArg(options::OPT_static);
Hans Wennborg76b86c22013-07-18 20:29:38 +00007195 if (!D.CCCIsCXX())
Rafael Espindolaabf3ac72011-10-17 21:39:04 +00007196 CmdArgs.push_back("-lgcc");
7197
Logan Chien529a73d2012-11-19 12:04:11 +00007198 if (StaticLibgcc || isAndroid) {
Hans Wennborg76b86c22013-07-18 20:29:38 +00007199 if (D.CCCIsCXX())
Rafael Espindolaabf3ac72011-10-17 21:39:04 +00007200 CmdArgs.push_back("-lgcc");
7201 } else {
Hans Wennborg76b86c22013-07-18 20:29:38 +00007202 if (!D.CCCIsCXX())
Rafael Espindolaabf3ac72011-10-17 21:39:04 +00007203 CmdArgs.push_back("--as-needed");
7204 CmdArgs.push_back("-lgcc_s");
Hans Wennborg76b86c22013-07-18 20:29:38 +00007205 if (!D.CCCIsCXX())
Rafael Espindolaabf3ac72011-10-17 21:39:04 +00007206 CmdArgs.push_back("--no-as-needed");
7207 }
7208
Evgeniy Stepanova6ddc022012-04-25 08:59:22 +00007209 if (StaticLibgcc && !isAndroid)
Rafael Espindolaabf3ac72011-10-17 21:39:04 +00007210 CmdArgs.push_back("-lgcc_eh");
Hans Wennborg76b86c22013-07-18 20:29:38 +00007211 else if (!Args.hasArg(options::OPT_shared) && D.CCCIsCXX())
Rafael Espindolaabf3ac72011-10-17 21:39:04 +00007212 CmdArgs.push_back("-lgcc");
Logan Chien529a73d2012-11-19 12:04:11 +00007213
7214 // According to Android ABI, we have to link with libdl if we are
7215 // linking with non-static libgcc.
7216 //
7217 // NOTE: This fixes a link error on Android MIPS as well. The non-static
7218 // libgcc for MIPS relies on _Unwind_Find_FDE and dl_iterate_phdr from libdl.
7219 if (isAndroid && !StaticLibgcc)
7220 CmdArgs.push_back("-ldl");
Rafael Espindolaabf3ac72011-10-17 21:39:04 +00007221}
7222
Stephen Hines176edba2014-12-01 14:53:08 -08007223static std::string getLinuxDynamicLinker(const ArgList &Args,
7224 const toolchains::Linux &ToolChain) {
Stephen Hines651f13c2014-04-23 16:59:28 -07007225 if (ToolChain.getTriple().getEnvironment() == llvm::Triple::Android) {
7226 if (ToolChain.getTriple().isArch64Bit())
7227 return "/system/bin/linker64";
7228 else
7229 return "/system/bin/linker";
7230 } else if (ToolChain.getArch() == llvm::Triple::x86 ||
7231 ToolChain.getArch() == llvm::Triple::sparc)
Peter Collingbournebdaa1342013-05-27 21:40:20 +00007232 return "/lib/ld-linux.so.2";
Stephen Hines176edba2014-12-01 14:53:08 -08007233 else if (ToolChain.getArch() == llvm::Triple::aarch64)
Peter Collingbournebdaa1342013-05-27 21:40:20 +00007234 return "/lib/ld-linux-aarch64.so.1";
Stephen Hines176edba2014-12-01 14:53:08 -08007235 else if (ToolChain.getArch() == llvm::Triple::aarch64_be)
Stephen Hines651f13c2014-04-23 16:59:28 -07007236 return "/lib/ld-linux-aarch64_be.so.1";
Peter Collingbournebdaa1342013-05-27 21:40:20 +00007237 else if (ToolChain.getArch() == llvm::Triple::arm ||
7238 ToolChain.getArch() == llvm::Triple::thumb) {
7239 if (ToolChain.getTriple().getEnvironment() == llvm::Triple::GNUEABIHF)
7240 return "/lib/ld-linux-armhf.so.3";
7241 else
7242 return "/lib/ld-linux.so.3";
Stephen Hines651f13c2014-04-23 16:59:28 -07007243 } else if (ToolChain.getArch() == llvm::Triple::armeb ||
7244 ToolChain.getArch() == llvm::Triple::thumbeb) {
7245 if (ToolChain.getTriple().getEnvironment() == llvm::Triple::GNUEABIHF)
7246 return "/lib/ld-linux-armhf.so.3"; /* TODO: check which dynamic linker name. */
7247 else
7248 return "/lib/ld-linux.so.3"; /* TODO: check which dynamic linker name. */
Peter Collingbournebdaa1342013-05-27 21:40:20 +00007249 } else if (ToolChain.getArch() == llvm::Triple::mips ||
Stephen Hines176edba2014-12-01 14:53:08 -08007250 ToolChain.getArch() == llvm::Triple::mipsel ||
7251 ToolChain.getArch() == llvm::Triple::mips64 ||
Stephen Hinesef822542014-07-21 00:47:37 -07007252 ToolChain.getArch() == llvm::Triple::mips64el) {
Stephen Hines176edba2014-12-01 14:53:08 -08007253 StringRef CPUName;
7254 StringRef ABIName;
7255 mips::getMipsCPUAndABI(Args, ToolChain.getTriple(), CPUName, ABIName);
7256 bool IsNaN2008 = mips::isNaN2008(Args, ToolChain.getTriple());
7257
7258 StringRef LibDir = llvm::StringSwitch<llvm::StringRef>(ABIName)
7259 .Case("o32", "/lib")
7260 .Case("n32", "/lib32")
7261 .Case("n64", "/lib64")
7262 .Default("/lib");
7263 StringRef LibName;
7264 if (mips::isUCLibc(Args))
7265 LibName = IsNaN2008 ? "ld-uClibc-mipsn8.so.0" : "ld-uClibc.so.0";
7266 else
7267 LibName = IsNaN2008 ? "ld-linux-mipsn8.so.1" : "ld.so.1";
7268
7269 return (LibDir + "/" + LibName).str();
Peter Collingbournebdaa1342013-05-27 21:40:20 +00007270 } else if (ToolChain.getArch() == llvm::Triple::ppc)
7271 return "/lib/ld.so.1";
Stephen Hines176edba2014-12-01 14:53:08 -08007272 else if (ToolChain.getArch() == llvm::Triple::ppc64) {
7273 if (ppc::hasPPCAbiArg(Args, "elfv2"))
7274 return "/lib64/ld64.so.2";
Peter Collingbournebdaa1342013-05-27 21:40:20 +00007275 return "/lib64/ld64.so.1";
Stephen Hines176edba2014-12-01 14:53:08 -08007276 } else if (ToolChain.getArch() == llvm::Triple::ppc64le) {
7277 if (ppc::hasPPCAbiArg(Args, "elfv1"))
7278 return "/lib64/ld64.so.1";
Stephen Hinesef822542014-07-21 00:47:37 -07007279 return "/lib64/ld64.so.2";
Stephen Hines176edba2014-12-01 14:53:08 -08007280 } else if (ToolChain.getArch() == llvm::Triple::systemz)
7281 return "/lib64/ld64.so.1";
Stephen Hines651f13c2014-04-23 16:59:28 -07007282 else if (ToolChain.getArch() == llvm::Triple::sparcv9)
7283 return "/lib64/ld-linux.so.2";
Stephen Hinesef822542014-07-21 00:47:37 -07007284 else if (ToolChain.getArch() == llvm::Triple::x86_64 &&
7285 ToolChain.getTriple().getEnvironment() == llvm::Triple::GNUX32)
7286 return "/libx32/ld-linux-x32.so.2";
Peter Collingbournebdaa1342013-05-27 21:40:20 +00007287 else
7288 return "/lib64/ld-linux-x86-64.so.2";
7289}
7290
Stephen Hines651f13c2014-04-23 16:59:28 -07007291static void AddRunTimeLibs(const ToolChain &TC, const Driver &D,
7292 ArgStringList &CmdArgs, const ArgList &Args) {
7293 // Make use of compiler-rt if --rtlib option is used
7294 ToolChain::RuntimeLibType RLT = TC.GetRuntimeLibType(Args);
7295
7296 switch(RLT) {
7297 case ToolChain::RLT_CompilerRT:
Stephen Hines176edba2014-12-01 14:53:08 -08007298 switch (TC.getTriple().getOS()) {
7299 default: llvm_unreachable("unsupported OS");
7300 case llvm::Triple::Win32:
7301 addClangRTWindows(TC, Args, CmdArgs);
7302 break;
7303 case llvm::Triple::Linux:
7304 addClangRTLinux(TC, Args, CmdArgs);
7305 break;
7306 }
Stephen Hines651f13c2014-04-23 16:59:28 -07007307 break;
7308 case ToolChain::RLT_Libgcc:
7309 AddLibgcc(TC.getTriple(), D, CmdArgs, Args);
7310 break;
7311 }
7312}
7313
Stephen Hines176edba2014-12-01 14:53:08 -08007314static const char *getLDMOption(const llvm::Triple &T, const ArgList &Args) {
7315 switch (T.getArch()) {
7316 case llvm::Triple::x86:
7317 return "elf_i386";
7318 case llvm::Triple::aarch64:
7319 return "aarch64linux";
7320 case llvm::Triple::aarch64_be:
7321 return "aarch64_be_linux";
7322 case llvm::Triple::arm:
7323 case llvm::Triple::thumb:
7324 return "armelf_linux_eabi";
7325 case llvm::Triple::armeb:
7326 case llvm::Triple::thumbeb:
7327 return "armebelf_linux_eabi"; /* TODO: check which NAME. */
7328 case llvm::Triple::ppc:
7329 return "elf32ppclinux";
7330 case llvm::Triple::ppc64:
7331 return "elf64ppc";
7332 case llvm::Triple::ppc64le:
7333 return "elf64lppc";
7334 case llvm::Triple::sparc:
7335 return "elf32_sparc";
7336 case llvm::Triple::sparcv9:
7337 return "elf64_sparc";
7338 case llvm::Triple::mips:
7339 return "elf32btsmip";
7340 case llvm::Triple::mipsel:
7341 return "elf32ltsmip";
7342 case llvm::Triple::mips64:
7343 if (mips::hasMipsAbiArg(Args, "n32"))
7344 return "elf32btsmipn32";
7345 return "elf64btsmip";
7346 case llvm::Triple::mips64el:
7347 if (mips::hasMipsAbiArg(Args, "n32"))
7348 return "elf32ltsmipn32";
7349 return "elf64ltsmip";
7350 case llvm::Triple::systemz:
7351 return "elf64_s390";
7352 case llvm::Triple::x86_64:
7353 if (T.getEnvironment() == llvm::Triple::GNUX32)
7354 return "elf32_x86_64";
7355 return "elf_x86_64";
7356 default:
7357 llvm_unreachable("Unexpected arch");
7358 }
7359}
7360
Thomas Schwinge577bb0a2013-03-28 19:04:25 +00007361void gnutools::Link::ConstructJob(Compilation &C, const JobAction &JA,
7362 const InputInfo &Output,
7363 const InputInfoList &Inputs,
7364 const ArgList &Args,
7365 const char *LinkingOutput) const {
Rafael Espindolac1da9812010-11-07 20:14:31 +00007366 const toolchains::Linux& ToolChain =
7367 static_cast<const toolchains::Linux&>(getToolChain());
7368 const Driver &D = ToolChain.getDriver();
Rafael Espindola715852c2012-11-02 20:41:30 +00007369 const bool isAndroid =
7370 ToolChain.getTriple().getEnvironment() == llvm::Triple::Android;
Peter Collingbourne52ca70d2013-04-09 04:35:11 +00007371 const bool IsPIE =
7372 !Args.hasArg(options::OPT_shared) &&
Stephen Hines6bcf27b2014-05-29 04:14:42 -07007373 !Args.hasArg(options::OPT_static) &&
7374 (Args.hasArg(options::OPT_pie) || ToolChain.isPIEDefault() ||
7375 // On Android every code is PIC so every executable is PIE
7376 // Cannot use isPIEDefault here since otherwise
7377 // PIE only logic will be enabled during compilation
7378 isAndroid);
Evgeniy Stepanova6ddc022012-04-25 08:59:22 +00007379
Rafael Espindolac1da9812010-11-07 20:14:31 +00007380 ArgStringList CmdArgs;
7381
Rafael Espindola26f14c32010-11-15 18:28:16 +00007382 // Silence warning for "clang -g foo.o -o foo"
7383 Args.ClaimAllArgs(options::OPT_g_Group);
Rafael Espindola9c094fb2011-03-01 05:25:27 +00007384 // and "clang -emit-llvm foo.o -o foo"
7385 Args.ClaimAllArgs(options::OPT_emit_llvm);
David Chisnalldfa210b2012-07-29 15:24:44 +00007386 // and for "clang -w foo.o -o foo". Other warning options are already
Rafael Espindola7f6458b2010-11-17 20:37:10 +00007387 // handled somewhere else.
7388 Args.ClaimAllArgs(options::OPT_w);
Rafael Espindola26f14c32010-11-15 18:28:16 +00007389
Joerg Sonnenberger8ab2bdc2011-03-21 13:51:29 +00007390 if (!D.SysRoot.empty())
7391 CmdArgs.push_back(Args.MakeArgString("--sysroot=" + D.SysRoot));
Rafael Espindolac1da9812010-11-07 20:14:31 +00007392
Peter Collingbourne52ca70d2013-04-09 04:35:11 +00007393 if (IsPIE)
Rafael Espindolafdda1712010-11-17 22:26:15 +00007394 CmdArgs.push_back("-pie");
7395
Rafael Espindoladc1b76d2010-11-07 22:57:16 +00007396 if (Args.hasArg(options::OPT_rdynamic))
7397 CmdArgs.push_back("-export-dynamic");
7398
Rafael Espindolae0e6d3b2010-11-11 19:34:42 +00007399 if (Args.hasArg(options::OPT_s))
7400 CmdArgs.push_back("-s");
7401
Stephen Hinesef822542014-07-21 00:47:37 -07007402 for (const auto &Opt : ToolChain.ExtraOpts)
7403 CmdArgs.push_back(Opt.c_str());
Rafael Espindolac1da9812010-11-07 20:14:31 +00007404
7405 if (!Args.hasArg(options::OPT_static)) {
7406 CmdArgs.push_back("--eh-frame-hdr");
7407 }
7408
7409 CmdArgs.push_back("-m");
Stephen Hines176edba2014-12-01 14:53:08 -08007410 CmdArgs.push_back(getLDMOption(ToolChain.getTriple(), Args));
Rafael Espindolac1da9812010-11-07 20:14:31 +00007411
7412 if (Args.hasArg(options::OPT_static)) {
Stephen Hines651f13c2014-04-23 16:59:28 -07007413 if (ToolChain.getArch() == llvm::Triple::arm ||
7414 ToolChain.getArch() == llvm::Triple::armeb ||
7415 ToolChain.getArch() == llvm::Triple::thumb ||
7416 ToolChain.getArch() == llvm::Triple::thumbeb)
Rafael Espindolac1da9812010-11-07 20:14:31 +00007417 CmdArgs.push_back("-Bstatic");
7418 else
7419 CmdArgs.push_back("-static");
7420 } else if (Args.hasArg(options::OPT_shared)) {
7421 CmdArgs.push_back("-shared");
7422 }
7423
7424 if (ToolChain.getArch() == llvm::Triple::arm ||
Stephen Hines651f13c2014-04-23 16:59:28 -07007425 ToolChain.getArch() == llvm::Triple::armeb ||
Douglas Gregorf0594d82011-03-06 19:11:49 +00007426 ToolChain.getArch() == llvm::Triple::thumb ||
Stephen Hines651f13c2014-04-23 16:59:28 -07007427 ToolChain.getArch() == llvm::Triple::thumbeb ||
Rafael Espindolac1da9812010-11-07 20:14:31 +00007428 (!Args.hasArg(options::OPT_static) &&
7429 !Args.hasArg(options::OPT_shared))) {
7430 CmdArgs.push_back("-dynamic-linker");
Peter Collingbournebdaa1342013-05-27 21:40:20 +00007431 CmdArgs.push_back(Args.MakeArgString(
7432 D.DyldPrefix + getLinuxDynamicLinker(Args, ToolChain)));
Rafael Espindolac1da9812010-11-07 20:14:31 +00007433 }
7434
7435 CmdArgs.push_back("-o");
7436 CmdArgs.push_back(Output.getFilename());
7437
Rafael Espindola49c64fd2010-12-01 01:52:43 +00007438 if (!Args.hasArg(options::OPT_nostdlib) &&
7439 !Args.hasArg(options::OPT_nostartfiles)) {
Evgeniy Stepanova6ddc022012-04-25 08:59:22 +00007440 if (!isAndroid) {
Stephen Hines6bcf27b2014-05-29 04:14:42 -07007441 const char *crt1 = nullptr;
Evgeniy Stepanova6ddc022012-04-25 08:59:22 +00007442 if (!Args.hasArg(options::OPT_shared)){
Eric Christopher61f08682013-06-07 23:25:01 +00007443 if (Args.hasArg(options::OPT_pg))
7444 crt1 = "gcrt1.o";
7445 else if (IsPIE)
Evgeniy Stepanova6ddc022012-04-25 08:59:22 +00007446 crt1 = "Scrt1.o";
7447 else
7448 crt1 = "crt1.o";
7449 }
7450 if (crt1)
7451 CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath(crt1)));
Rafael Espindolac1da9812010-11-07 20:14:31 +00007452
Evgeniy Stepanova6ddc022012-04-25 08:59:22 +00007453 CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath("crti.o")));
7454 }
Rafael Espindolac1da9812010-11-07 20:14:31 +00007455
Rafael Espindola89414b32010-11-12 03:00:39 +00007456 const char *crtbegin;
7457 if (Args.hasArg(options::OPT_static))
Evgeniy Stepanova6ddc022012-04-25 08:59:22 +00007458 crtbegin = isAndroid ? "crtbegin_static.o" : "crtbeginT.o";
Evgeniy Stepanova92983d2012-09-10 10:30:12 +00007459 else if (Args.hasArg(options::OPT_shared))
Evgeniy Stepanova6ddc022012-04-25 08:59:22 +00007460 crtbegin = isAndroid ? "crtbegin_so.o" : "crtbeginS.o";
Peter Collingbourne52ca70d2013-04-09 04:35:11 +00007461 else if (IsPIE)
Evgeniy Stepanova92983d2012-09-10 10:30:12 +00007462 crtbegin = isAndroid ? "crtbegin_dynamic.o" : "crtbeginS.o";
Rafael Espindola89414b32010-11-12 03:00:39 +00007463 else
Evgeniy Stepanova6ddc022012-04-25 08:59:22 +00007464 crtbegin = isAndroid ? "crtbegin_dynamic.o" : "crtbegin.o";
Rafael Espindola89414b32010-11-12 03:00:39 +00007465 CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath(crtbegin)));
Benjamin Kramere20e5082012-10-04 19:42:20 +00007466
7467 // Add crtfastmath.o if available and fast math is enabled.
7468 ToolChain.AddFastMathRuntimeIfAvailable(Args, CmdArgs);
Rafael Espindola89414b32010-11-12 03:00:39 +00007469 }
Rafael Espindolac1da9812010-11-07 20:14:31 +00007470
7471 Args.AddAllArgs(CmdArgs, options::OPT_L);
Stephen Hinesef822542014-07-21 00:47:37 -07007472 Args.AddAllArgs(CmdArgs, options::OPT_u);
Rafael Espindolac1da9812010-11-07 20:14:31 +00007473
Stephen Hines176edba2014-12-01 14:53:08 -08007474 const ToolChain::path_list &Paths = ToolChain.getFilePaths();
Rafael Espindolac1da9812010-11-07 20:14:31 +00007475
Stephen Hinesef822542014-07-21 00:47:37 -07007476 for (const auto &Path : Paths)
7477 CmdArgs.push_back(Args.MakeArgString(StringRef("-L") + Path));
Rafael Espindolac1da9812010-11-07 20:14:31 +00007478
Stephen Hines651f13c2014-04-23 16:59:28 -07007479 if (D.IsUsingLTO(Args))
7480 AddGoldPlugin(ToolChain, Args, CmdArgs);
Chandler Carruth700d4e42013-01-13 11:46:33 +00007481
Nick Lewyckye276cfc2012-08-17 03:39:16 +00007482 if (Args.hasArg(options::OPT_Z_Xlinker__no_demangle))
7483 CmdArgs.push_back("--no-demangle");
7484
Stephen Hines176edba2014-12-01 14:53:08 -08007485 bool NeedsSanitizerDeps = addSanitizerRuntimes(ToolChain, Args, CmdArgs);
Rafael Espindolac1da9812010-11-07 20:14:31 +00007486 AddLinkerInputs(ToolChain, Inputs, Args, CmdArgs);
Chandler Carruth80a393e2013-06-24 09:38:45 +00007487 // The profile runtime also needs access to system libraries.
Stephen Hines651f13c2014-04-23 16:59:28 -07007488 addProfileRT(getToolChain(), Args, CmdArgs);
Chandler Carruth80a393e2013-06-24 09:38:45 +00007489
Hans Wennborg76b86c22013-07-18 20:29:38 +00007490 if (D.CCCIsCXX() &&
Chandler Carruth2ba542c2012-05-14 18:31:18 +00007491 !Args.hasArg(options::OPT_nostdlib) &&
7492 !Args.hasArg(options::OPT_nodefaultlibs)) {
Rafael Espindola19706f82011-10-17 22:14:51 +00007493 bool OnlyLibstdcxxStatic = Args.hasArg(options::OPT_static_libstdcxx) &&
7494 !Args.hasArg(options::OPT_static);
7495 if (OnlyLibstdcxxStatic)
7496 CmdArgs.push_back("-Bstatic");
Rafael Espindolac1da9812010-11-07 20:14:31 +00007497 ToolChain.AddCXXStdlibLibArgs(Args, CmdArgs);
Rafael Espindola19706f82011-10-17 22:14:51 +00007498 if (OnlyLibstdcxxStatic)
7499 CmdArgs.push_back("-Bdynamic");
Rafael Espindolac1da9812010-11-07 20:14:31 +00007500 CmdArgs.push_back("-lm");
7501 }
7502
Rafael Espindola89414b32010-11-12 03:00:39 +00007503 if (!Args.hasArg(options::OPT_nostdlib)) {
Chandler Carruth2ba542c2012-05-14 18:31:18 +00007504 if (!Args.hasArg(options::OPT_nodefaultlibs)) {
7505 if (Args.hasArg(options::OPT_static))
7506 CmdArgs.push_back("--start-group");
Nick Lewycky80df0252011-06-04 06:27:06 +00007507
Stephen Hines176edba2014-12-01 14:53:08 -08007508 if (NeedsSanitizerDeps)
7509 linkSanitizerRuntimeDeps(ToolChain, CmdArgs);
7510
Stephen Hines651f13c2014-04-23 16:59:28 -07007511 LibOpenMP UsedOpenMPLib = LibUnknown;
7512 if (Args.hasArg(options::OPT_fopenmp)) {
7513 UsedOpenMPLib = LibGOMP;
7514 } else if (const Arg *A = Args.getLastArg(options::OPT_fopenmp_EQ)) {
7515 UsedOpenMPLib = llvm::StringSwitch<LibOpenMP>(A->getValue())
7516 .Case("libgomp", LibGOMP)
7517 .Case("libiomp5", LibIOMP5)
7518 .Default(LibUnknown);
7519 if (UsedOpenMPLib == LibUnknown)
7520 D.Diag(diag::err_drv_unsupported_option_argument)
7521 << A->getOption().getName() << A->getValue();
7522 }
7523 switch (UsedOpenMPLib) {
7524 case LibGOMP:
Chandler Carruthdf96e022013-01-17 13:19:29 +00007525 CmdArgs.push_back("-lgomp");
7526
Stephen Hines651f13c2014-04-23 16:59:28 -07007527 // FIXME: Exclude this for platforms with libgomp that don't require
7528 // librt. Most modern Linux platforms require it, but some may not.
Chandler Carruthdf96e022013-01-17 13:19:29 +00007529 CmdArgs.push_back("-lrt");
Stephen Hines651f13c2014-04-23 16:59:28 -07007530 break;
7531 case LibIOMP5:
7532 CmdArgs.push_back("-liomp5");
7533 break;
7534 case LibUnknown:
7535 break;
Chandler Carruthdf96e022013-01-17 13:19:29 +00007536 }
Stephen Hines651f13c2014-04-23 16:59:28 -07007537 AddRunTimeLibs(ToolChain, D, CmdArgs, Args);
Rafael Espindola89414b32010-11-12 03:00:39 +00007538
Stephen Hinesef822542014-07-21 00:47:37 -07007539 if ((Args.hasArg(options::OPT_pthread) ||
7540 Args.hasArg(options::OPT_pthreads) || UsedOpenMPLib != LibUnknown) &&
7541 !isAndroid)
Chandler Carruth2ba542c2012-05-14 18:31:18 +00007542 CmdArgs.push_back("-lpthread");
7543
7544 CmdArgs.push_back("-lc");
7545
7546 if (Args.hasArg(options::OPT_static))
7547 CmdArgs.push_back("--end-group");
7548 else
Stephen Hines651f13c2014-04-23 16:59:28 -07007549 AddRunTimeLibs(ToolChain, D, CmdArgs, Args);
Chandler Carruth2ba542c2012-05-14 18:31:18 +00007550 }
Rafael Espindolafdda1712010-11-17 22:26:15 +00007551
Rafael Espindola49c64fd2010-12-01 01:52:43 +00007552 if (!Args.hasArg(options::OPT_nostartfiles)) {
7553 const char *crtend;
Evgeniy Stepanova92983d2012-09-10 10:30:12 +00007554 if (Args.hasArg(options::OPT_shared))
Evgeniy Stepanova6ddc022012-04-25 08:59:22 +00007555 crtend = isAndroid ? "crtend_so.o" : "crtendS.o";
Peter Collingbourne52ca70d2013-04-09 04:35:11 +00007556 else if (IsPIE)
Evgeniy Stepanova92983d2012-09-10 10:30:12 +00007557 crtend = isAndroid ? "crtend_android.o" : "crtendS.o";
Rafael Espindola49c64fd2010-12-01 01:52:43 +00007558 else
Evgeniy Stepanova6ddc022012-04-25 08:59:22 +00007559 crtend = isAndroid ? "crtend_android.o" : "crtend.o";
Rafael Espindola89414b32010-11-12 03:00:39 +00007560
Rafael Espindola49c64fd2010-12-01 01:52:43 +00007561 CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath(crtend)));
Evgeniy Stepanova6ddc022012-04-25 08:59:22 +00007562 if (!isAndroid)
7563 CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath("crtn.o")));
Rafael Espindola49c64fd2010-12-01 01:52:43 +00007564 }
Rafael Espindolac1da9812010-11-07 20:14:31 +00007565 }
7566
Stephen Hines176edba2014-12-01 14:53:08 -08007567 C.addCommand(
7568 llvm::make_unique<Command>(JA, *this, ToolChain.Linker.c_str(), CmdArgs));
Rafael Espindolac1da9812010-11-07 20:14:31 +00007569}
Rafael Espindolaba30bbe2010-08-10 00:25:48 +00007570
Chris Lattner38e317d2010-07-07 16:01:42 +00007571void minix::Assemble::ConstructJob(Compilation &C, const JobAction &JA,
Daniel Dunbar2fe238e2010-08-02 02:38:28 +00007572 const InputInfo &Output,
7573 const InputInfoList &Inputs,
7574 const ArgList &Args,
7575 const char *LinkingOutput) const {
Chris Lattner38e317d2010-07-07 16:01:42 +00007576 ArgStringList CmdArgs;
7577
Stephen Hinesef822542014-07-21 00:47:37 -07007578 Args.AddAllArgValues(CmdArgs, options::OPT_Wa_COMMA, options::OPT_Xassembler);
Chris Lattner38e317d2010-07-07 16:01:42 +00007579
7580 CmdArgs.push_back("-o");
Daniel Dunbar7c1e4652010-08-02 02:38:21 +00007581 CmdArgs.push_back(Output.getFilename());
Chris Lattner38e317d2010-07-07 16:01:42 +00007582
Stephen Hinesef822542014-07-21 00:47:37 -07007583 for (const auto &II : Inputs)
Daniel Dunbar7c1e4652010-08-02 02:38:21 +00007584 CmdArgs.push_back(II.getFilename());
Chris Lattner38e317d2010-07-07 16:01:42 +00007585
Stephen Hinesef822542014-07-21 00:47:37 -07007586 const char *Exec = Args.MakeArgString(getToolChain().GetProgramPath("as"));
Stephen Hines176edba2014-12-01 14:53:08 -08007587 C.addCommand(llvm::make_unique<Command>(JA, *this, Exec, CmdArgs));
Chris Lattner38e317d2010-07-07 16:01:42 +00007588}
7589
7590void minix::Link::ConstructJob(Compilation &C, const JobAction &JA,
Daniel Dunbar2fe238e2010-08-02 02:38:28 +00007591 const InputInfo &Output,
7592 const InputInfoList &Inputs,
7593 const ArgList &Args,
7594 const char *LinkingOutput) const {
Chris Lattner38e317d2010-07-07 16:01:42 +00007595 const Driver &D = getToolChain().getDriver();
7596 ArgStringList CmdArgs;
7597
Daniel Dunbar7c1e4652010-08-02 02:38:21 +00007598 if (Output.isFilename()) {
Chris Lattner38e317d2010-07-07 16:01:42 +00007599 CmdArgs.push_back("-o");
7600 CmdArgs.push_back(Output.getFilename());
7601 } else {
7602 assert(Output.isNothing() && "Invalid output.");
7603 }
7604
7605 if (!Args.hasArg(options::OPT_nostdlib) &&
Eli Friedman6d402dc2011-12-08 23:54:21 +00007606 !Args.hasArg(options::OPT_nostartfiles)) {
7607 CmdArgs.push_back(Args.MakeArgString(getToolChain().GetFilePath("crt1.o")));
7608 CmdArgs.push_back(Args.MakeArgString(getToolChain().GetFilePath("crti.o")));
7609 CmdArgs.push_back(Args.MakeArgString(getToolChain().GetFilePath("crtbegin.o")));
7610 CmdArgs.push_back(Args.MakeArgString(getToolChain().GetFilePath("crtn.o")));
7611 }
Chris Lattner38e317d2010-07-07 16:01:42 +00007612
7613 Args.AddAllArgs(CmdArgs, options::OPT_L);
7614 Args.AddAllArgs(CmdArgs, options::OPT_T_Group);
7615 Args.AddAllArgs(CmdArgs, options::OPT_e);
7616
Daniel Dunbar2008fee2010-09-17 00:24:54 +00007617 AddLinkerInputs(getToolChain(), Inputs, Args, CmdArgs);
Chris Lattner38e317d2010-07-07 16:01:42 +00007618
Stephen Hines651f13c2014-04-23 16:59:28 -07007619 addProfileRT(getToolChain(), Args, CmdArgs);
Eli Friedman6d402dc2011-12-08 23:54:21 +00007620
Chris Lattner38e317d2010-07-07 16:01:42 +00007621 if (!Args.hasArg(options::OPT_nostdlib) &&
7622 !Args.hasArg(options::OPT_nodefaultlibs)) {
Hans Wennborg76b86c22013-07-18 20:29:38 +00007623 if (D.CCCIsCXX()) {
Daniel Dunbar132e35d2010-09-17 01:20:05 +00007624 getToolChain().AddCXXStdlibLibArgs(Args, CmdArgs);
Chris Lattner38e317d2010-07-07 16:01:42 +00007625 CmdArgs.push_back("-lm");
7626 }
Chris Lattner38e317d2010-07-07 16:01:42 +00007627 }
7628
7629 if (!Args.hasArg(options::OPT_nostdlib) &&
7630 !Args.hasArg(options::OPT_nostartfiles)) {
Eli Friedman6d402dc2011-12-08 23:54:21 +00007631 if (Args.hasArg(options::OPT_pthread))
7632 CmdArgs.push_back("-lpthread");
7633 CmdArgs.push_back("-lc");
7634 CmdArgs.push_back("-lCompilerRT-Generic");
7635 CmdArgs.push_back("-L/usr/pkg/compiler-rt/lib");
7636 CmdArgs.push_back(
Eric Christopher27e2b982012-12-18 00:31:10 +00007637 Args.MakeArgString(getToolChain().GetFilePath("crtend.o")));
Chris Lattner38e317d2010-07-07 16:01:42 +00007638 }
7639
Stephen Hinesef822542014-07-21 00:47:37 -07007640 const char *Exec = Args.MakeArgString(getToolChain().GetLinkerPath());
Stephen Hines176edba2014-12-01 14:53:08 -08007641 C.addCommand(llvm::make_unique<Command>(JA, *this, Exec, CmdArgs));
Chris Lattner38e317d2010-07-07 16:01:42 +00007642}
7643
Daniel Dunbar11e1b402009-05-02 18:28:39 +00007644/// DragonFly Tools
7645
7646// For now, DragonFly Assemble does just about the same as for
7647// FreeBSD, but this may change soon.
7648void dragonfly::Assemble::ConstructJob(Compilation &C, const JobAction &JA,
Daniel Dunbar2fe238e2010-08-02 02:38:28 +00007649 const InputInfo &Output,
Daniel Dunbar294691e2009-11-04 06:24:38 +00007650 const InputInfoList &Inputs,
7651 const ArgList &Args,
7652 const char *LinkingOutput) const {
Daniel Dunbar11e1b402009-05-02 18:28:39 +00007653 ArgStringList CmdArgs;
7654
7655 // When building 32-bit code on DragonFly/pc64, we have to explicitly
7656 // instruct as in the base system to assemble 32-bit code.
Rafael Espindola64f7ad92012-10-07 04:44:33 +00007657 if (getToolChain().getArch() == llvm::Triple::x86)
Daniel Dunbar11e1b402009-05-02 18:28:39 +00007658 CmdArgs.push_back("--32");
7659
Stephen Hinesef822542014-07-21 00:47:37 -07007660 Args.AddAllArgValues(CmdArgs, options::OPT_Wa_COMMA, options::OPT_Xassembler);
Daniel Dunbar11e1b402009-05-02 18:28:39 +00007661
7662 CmdArgs.push_back("-o");
Daniel Dunbar7c1e4652010-08-02 02:38:21 +00007663 CmdArgs.push_back(Output.getFilename());
Daniel Dunbar11e1b402009-05-02 18:28:39 +00007664
Stephen Hinesef822542014-07-21 00:47:37 -07007665 for (const auto &II : Inputs)
Daniel Dunbar7c1e4652010-08-02 02:38:21 +00007666 CmdArgs.push_back(II.getFilename());
Daniel Dunbar11e1b402009-05-02 18:28:39 +00007667
Stephen Hinesef822542014-07-21 00:47:37 -07007668 const char *Exec = Args.MakeArgString(getToolChain().GetProgramPath("as"));
Stephen Hines176edba2014-12-01 14:53:08 -08007669 C.addCommand(llvm::make_unique<Command>(JA, *this, Exec, CmdArgs));
Daniel Dunbar11e1b402009-05-02 18:28:39 +00007670}
7671
7672void dragonfly::Link::ConstructJob(Compilation &C, const JobAction &JA,
Daniel Dunbar2fe238e2010-08-02 02:38:28 +00007673 const InputInfo &Output,
7674 const InputInfoList &Inputs,
7675 const ArgList &Args,
7676 const char *LinkingOutput) const {
Daniel Dunbaree788e72009-12-21 18:54:17 +00007677 const Driver &D = getToolChain().getDriver();
Daniel Dunbar11e1b402009-05-02 18:28:39 +00007678 ArgStringList CmdArgs;
Stephen Hines176edba2014-12-01 14:53:08 -08007679 bool UseGCC47 = llvm::sys::fs::exists("/usr/lib/gcc47");
John McCall8cfb7202013-04-11 22:55:55 +00007680
Joerg Sonnenberger8ab2bdc2011-03-21 13:51:29 +00007681 if (!D.SysRoot.empty())
7682 CmdArgs.push_back(Args.MakeArgString("--sysroot=" + D.SysRoot));
7683
John McCall8cfb7202013-04-11 22:55:55 +00007684 CmdArgs.push_back("--eh-frame-hdr");
Daniel Dunbar11e1b402009-05-02 18:28:39 +00007685 if (Args.hasArg(options::OPT_static)) {
7686 CmdArgs.push_back("-Bstatic");
7687 } else {
John McCall8cfb7202013-04-11 22:55:55 +00007688 if (Args.hasArg(options::OPT_rdynamic))
7689 CmdArgs.push_back("-export-dynamic");
Daniel Dunbar11e1b402009-05-02 18:28:39 +00007690 if (Args.hasArg(options::OPT_shared))
7691 CmdArgs.push_back("-Bshareable");
7692 else {
7693 CmdArgs.push_back("-dynamic-linker");
7694 CmdArgs.push_back("/usr/libexec/ld-elf.so.2");
7695 }
John McCall8cfb7202013-04-11 22:55:55 +00007696 CmdArgs.push_back("--hash-style=both");
Daniel Dunbar11e1b402009-05-02 18:28:39 +00007697 }
7698
7699 // When building 32-bit code on DragonFly/pc64, we have to explicitly
7700 // instruct ld in the base system to link 32-bit code.
Rafael Espindola64f7ad92012-10-07 04:44:33 +00007701 if (getToolChain().getArch() == llvm::Triple::x86) {
Daniel Dunbar11e1b402009-05-02 18:28:39 +00007702 CmdArgs.push_back("-m");
7703 CmdArgs.push_back("elf_i386");
7704 }
7705
Daniel Dunbar7c1e4652010-08-02 02:38:21 +00007706 if (Output.isFilename()) {
Daniel Dunbar11e1b402009-05-02 18:28:39 +00007707 CmdArgs.push_back("-o");
7708 CmdArgs.push_back(Output.getFilename());
7709 } else {
7710 assert(Output.isNothing() && "Invalid output.");
7711 }
7712
7713 if (!Args.hasArg(options::OPT_nostdlib) &&
7714 !Args.hasArg(options::OPT_nostartfiles)) {
7715 if (!Args.hasArg(options::OPT_shared)) {
John McCall8cfb7202013-04-11 22:55:55 +00007716 if (Args.hasArg(options::OPT_pg))
7717 CmdArgs.push_back(Args.MakeArgString(
7718 getToolChain().GetFilePath("gcrt1.o")));
7719 else {
7720 if (Args.hasArg(options::OPT_pie))
7721 CmdArgs.push_back(Args.MakeArgString(
7722 getToolChain().GetFilePath("Scrt1.o")));
7723 else
7724 CmdArgs.push_back(Args.MakeArgString(
7725 getToolChain().GetFilePath("crt1.o")));
7726 }
Daniel Dunbar11e1b402009-05-02 18:28:39 +00007727 }
John McCall8cfb7202013-04-11 22:55:55 +00007728 CmdArgs.push_back(Args.MakeArgString(
7729 getToolChain().GetFilePath("crti.o")));
7730 if (Args.hasArg(options::OPT_shared) || Args.hasArg(options::OPT_pie))
7731 CmdArgs.push_back(Args.MakeArgString(
7732 getToolChain().GetFilePath("crtbeginS.o")));
7733 else
7734 CmdArgs.push_back(Args.MakeArgString(
7735 getToolChain().GetFilePath("crtbegin.o")));
Daniel Dunbar11e1b402009-05-02 18:28:39 +00007736 }
7737
7738 Args.AddAllArgs(CmdArgs, options::OPT_L);
7739 Args.AddAllArgs(CmdArgs, options::OPT_T_Group);
7740 Args.AddAllArgs(CmdArgs, options::OPT_e);
7741
Daniel Dunbar2008fee2010-09-17 00:24:54 +00007742 AddLinkerInputs(getToolChain(), Inputs, Args, CmdArgs);
Daniel Dunbar11e1b402009-05-02 18:28:39 +00007743
7744 if (!Args.hasArg(options::OPT_nostdlib) &&
7745 !Args.hasArg(options::OPT_nodefaultlibs)) {
7746 // FIXME: GCC passes on -lgcc, -lgcc_pic and a whole lot of
7747 // rpaths
John McCall8cfb7202013-04-11 22:55:55 +00007748 if (UseGCC47)
7749 CmdArgs.push_back("-L/usr/lib/gcc47");
7750 else
7751 CmdArgs.push_back("-L/usr/lib/gcc44");
Daniel Dunbar11e1b402009-05-02 18:28:39 +00007752
7753 if (!Args.hasArg(options::OPT_static)) {
John McCall8cfb7202013-04-11 22:55:55 +00007754 if (UseGCC47) {
7755 CmdArgs.push_back("-rpath");
7756 CmdArgs.push_back("/usr/lib/gcc47");
7757 } else {
7758 CmdArgs.push_back("-rpath");
7759 CmdArgs.push_back("/usr/lib/gcc44");
7760 }
Daniel Dunbar11e1b402009-05-02 18:28:39 +00007761 }
7762
Hans Wennborg76b86c22013-07-18 20:29:38 +00007763 if (D.CCCIsCXX()) {
Daniel Dunbar132e35d2010-09-17 01:20:05 +00007764 getToolChain().AddCXXStdlibLibArgs(Args, CmdArgs);
Rafael Espindola405861d2010-07-20 12:59:03 +00007765 CmdArgs.push_back("-lm");
7766 }
7767
Daniel Dunbar11e1b402009-05-02 18:28:39 +00007768 if (Args.hasArg(options::OPT_pthread))
Mike Stump4d63f8b2009-10-31 20:11:46 +00007769 CmdArgs.push_back("-lpthread");
Daniel Dunbar11e1b402009-05-02 18:28:39 +00007770
7771 if (!Args.hasArg(options::OPT_nolibc)) {
7772 CmdArgs.push_back("-lc");
7773 }
7774
John McCall8cfb7202013-04-11 22:55:55 +00007775 if (UseGCC47) {
7776 if (Args.hasArg(options::OPT_static) ||
7777 Args.hasArg(options::OPT_static_libgcc)) {
7778 CmdArgs.push_back("-lgcc");
7779 CmdArgs.push_back("-lgcc_eh");
7780 } else {
7781 if (Args.hasArg(options::OPT_shared_libgcc)) {
7782 CmdArgs.push_back("-lgcc_pic");
7783 if (!Args.hasArg(options::OPT_shared))
7784 CmdArgs.push_back("-lgcc");
7785 } else {
7786 CmdArgs.push_back("-lgcc");
7787 CmdArgs.push_back("--as-needed");
7788 CmdArgs.push_back("-lgcc_pic");
7789 CmdArgs.push_back("--no-as-needed");
7790 }
7791 }
Daniel Dunbar11e1b402009-05-02 18:28:39 +00007792 } else {
John McCall8cfb7202013-04-11 22:55:55 +00007793 if (Args.hasArg(options::OPT_shared)) {
7794 CmdArgs.push_back("-lgcc_pic");
7795 } else {
7796 CmdArgs.push_back("-lgcc");
7797 }
Daniel Dunbar11e1b402009-05-02 18:28:39 +00007798 }
7799 }
7800
7801 if (!Args.hasArg(options::OPT_nostdlib) &&
7802 !Args.hasArg(options::OPT_nostartfiles)) {
John McCall8cfb7202013-04-11 22:55:55 +00007803 if (Args.hasArg(options::OPT_shared) || Args.hasArg(options::OPT_pie))
Chris Lattner38e317d2010-07-07 16:01:42 +00007804 CmdArgs.push_back(Args.MakeArgString(
Daniel Dunbar4a7e8892010-07-14 18:46:23 +00007805 getToolChain().GetFilePath("crtendS.o")));
John McCall8cfb7202013-04-11 22:55:55 +00007806 else
7807 CmdArgs.push_back(Args.MakeArgString(
7808 getToolChain().GetFilePath("crtend.o")));
Chris Lattner38e317d2010-07-07 16:01:42 +00007809 CmdArgs.push_back(Args.MakeArgString(
John McCall8cfb7202013-04-11 22:55:55 +00007810 getToolChain().GetFilePath("crtn.o")));
Daniel Dunbar11e1b402009-05-02 18:28:39 +00007811 }
7812
Stephen Hines651f13c2014-04-23 16:59:28 -07007813 addProfileRT(getToolChain(), Args, CmdArgs);
Nick Lewycky2e95a6d2011-05-24 21:54:59 +00007814
Stephen Hinesef822542014-07-21 00:47:37 -07007815 const char *Exec = Args.MakeArgString(getToolChain().GetLinkerPath());
Stephen Hines176edba2014-12-01 14:53:08 -08007816 C.addCommand(llvm::make_unique<Command>(JA, *this, Exec, CmdArgs));
Daniel Dunbar11e1b402009-05-02 18:28:39 +00007817}
Michael J. Spencerff58e362010-08-21 21:55:07 +00007818
Stephen Hines6bcf27b2014-05-29 04:14:42 -07007819static void addSanitizerRTWindows(const ToolChain &TC, const ArgList &Args,
7820 ArgStringList &CmdArgs,
Stephen Hines176edba2014-12-01 14:53:08 -08007821 StringRef RTName) {
Stephen Hines6bcf27b2014-05-29 04:14:42 -07007822 SmallString<128> LibSanitizer(getCompilerRTLibDir(TC));
7823 llvm::sys::path::append(LibSanitizer,
7824 Twine("clang_rt.") + RTName + ".lib");
7825 CmdArgs.push_back(Args.MakeArgString(LibSanitizer));
7826}
7827
Stephen Hines176edba2014-12-01 14:53:08 -08007828// Try to find Exe from a Visual Studio distribution. This first tries to find
7829// an installed copy of Visual Studio and, failing that, looks in the PATH,
7830// making sure that whatever executable that's found is not a same-named exe
7831// from clang itself to prevent clang from falling back to itself.
7832static std::string FindVisualStudioExecutable(const ToolChain &TC,
7833 const char *Exe,
7834 const char *ClangProgramPath) {
7835 const auto &MSVC = static_cast<const toolchains::MSVCToolChain &>(TC);
7836 std::string visualStudioBinDir;
7837 if (MSVC.getVisualStudioBinariesFolder(ClangProgramPath,
7838 visualStudioBinDir)) {
7839 SmallString<128> FilePath(visualStudioBinDir);
7840 llvm::sys::path::append(FilePath, Exe);
7841 if (llvm::sys::fs::can_execute(FilePath.c_str()))
7842 return FilePath.str();
7843 }
7844
7845 return Exe;
7846}
7847
Michael J. Spencerff58e362010-08-21 21:55:07 +00007848void visualstudio::Link::ConstructJob(Compilation &C, const JobAction &JA,
7849 const InputInfo &Output,
7850 const InputInfoList &Inputs,
7851 const ArgList &Args,
7852 const char *LinkingOutput) const {
Michael J. Spencerff58e362010-08-21 21:55:07 +00007853 ArgStringList CmdArgs;
7854
7855 if (Output.isFilename()) {
Daniel Dunbare5a37f42010-09-17 00:45:02 +00007856 CmdArgs.push_back(Args.MakeArgString(std::string("-out:") +
7857 Output.getFilename()));
Michael J. Spencerff58e362010-08-21 21:55:07 +00007858 } else {
7859 assert(Output.isNothing() && "Invalid output.");
7860 }
7861
7862 if (!Args.hasArg(options::OPT_nostdlib) &&
Hans Wennborg746974d2013-08-09 17:38:42 +00007863 !Args.hasArg(options::OPT_nostartfiles) &&
7864 !C.getDriver().IsCLMode()) {
Michael J. Spencerff58e362010-08-21 21:55:07 +00007865 CmdArgs.push_back("-defaultlib:libcmt");
7866 }
7867
Stephen Hines176edba2014-12-01 14:53:08 -08007868 if (!llvm::sys::Process::GetEnv("LIB")) {
7869 // If the VC environment hasn't been configured (perhaps because the user
7870 // did not run vcvarsall), try to build a consistent link environment. If
7871 // the environment variable is set however, assume the user knows what he's
7872 // doing.
7873 std::string VisualStudioDir;
7874 const auto &MSVC = static_cast<const toolchains::MSVCToolChain &>(getToolChain());
7875 if (MSVC.getVisualStudioInstallDir(VisualStudioDir)) {
7876 SmallString<128> LibDir(VisualStudioDir);
7877 llvm::sys::path::append(LibDir, "VC", "lib");
7878 switch (MSVC.getArch()) {
7879 case llvm::Triple::x86:
7880 // x86 just puts the libraries directly in lib
7881 break;
7882 case llvm::Triple::x86_64:
7883 llvm::sys::path::append(LibDir, "amd64");
7884 break;
7885 case llvm::Triple::arm:
7886 llvm::sys::path::append(LibDir, "arm");
7887 break;
7888 default:
7889 break;
7890 }
7891 CmdArgs.push_back(
7892 Args.MakeArgString(std::string("-libpath:") + LibDir.c_str()));
7893 }
7894
7895 std::string WindowsSdkLibPath;
7896 if (MSVC.getWindowsSDKLibraryPath(WindowsSdkLibPath))
7897 CmdArgs.push_back(Args.MakeArgString(std::string("-libpath:") +
7898 WindowsSdkLibPath.c_str()));
7899 }
7900
Michael J. Spencerff58e362010-08-21 21:55:07 +00007901 CmdArgs.push_back("-nologo");
7902
Stephen Hines6bcf27b2014-05-29 04:14:42 -07007903 if (Args.hasArg(options::OPT_g_Group)) {
7904 CmdArgs.push_back("-debug");
7905 }
7906
Hans Wennborg6d0a8d52013-09-10 20:18:04 +00007907 bool DLL = Args.hasArg(options::OPT__SLASH_LD, options::OPT__SLASH_LDd);
7908
7909 if (DLL) {
7910 CmdArgs.push_back(Args.MakeArgString("-dll"));
7911
7912 SmallString<128> ImplibName(Output.getFilename());
7913 llvm::sys::path::replace_extension(ImplibName, "lib");
7914 CmdArgs.push_back(Args.MakeArgString(std::string("-implib:") +
7915 ImplibName.str()));
7916 }
7917
Peter Collingbournec6911a22013-11-01 18:16:25 +00007918 if (getToolChain().getSanitizerArgs().needsAsanRt()) {
Hans Wennborg324cc032013-08-28 17:36:07 +00007919 CmdArgs.push_back(Args.MakeArgString("-debug"));
Hans Wennborg2ddffa12013-08-30 10:50:52 +00007920 CmdArgs.push_back(Args.MakeArgString("-incremental:no"));
Hans Wennborg6d0a8d52013-09-10 20:18:04 +00007921 // FIXME: Handle 64-bit.
Stephen Hines176edba2014-12-01 14:53:08 -08007922 if (Args.hasArg(options::OPT__SLASH_MD, options::OPT__SLASH_MDd)) {
7923 addSanitizerRTWindows(getToolChain(), Args, CmdArgs, "asan_dynamic-i386");
7924 addSanitizerRTWindows(getToolChain(), Args, CmdArgs,
7925 "asan_dynamic_runtime_thunk-i386");
7926 // Make sure the dynamic runtime thunk is not optimized out at link time
7927 // to ensure proper SEH handling.
7928 CmdArgs.push_back(Args.MakeArgString("-include:___asan_seh_interceptor"));
7929 } else if (DLL) {
Stephen Hines6bcf27b2014-05-29 04:14:42 -07007930 addSanitizerRTWindows(getToolChain(), Args, CmdArgs,
7931 "asan_dll_thunk-i386");
7932 } else {
7933 addSanitizerRTWindows(getToolChain(), Args, CmdArgs, "asan-i386");
7934 addSanitizerRTWindows(getToolChain(), Args, CmdArgs, "asan_cxx-i386");
7935 }
Hans Wennborg3c4da0c2013-08-27 18:10:21 +00007936 }
7937
Hans Wennborg5db95272013-08-13 23:38:57 +00007938 Args.AddAllArgValues(CmdArgs, options::OPT__SLASH_link);
Michael J. Spencera2284f52012-06-18 16:56:04 +00007939
Stephen Hines176edba2014-12-01 14:53:08 -08007940 // Add filenames, libraries, and other linker inputs.
7941 for (const auto &Input : Inputs) {
7942 if (Input.isFilename()) {
Stephen Hinesef822542014-07-21 00:47:37 -07007943 CmdArgs.push_back(Input.getFilename());
Stephen Hines176edba2014-12-01 14:53:08 -08007944 continue;
7945 }
Michael J. Spencerff58e362010-08-21 21:55:07 +00007946
Stephen Hines176edba2014-12-01 14:53:08 -08007947 const Arg &A = Input.getInputArg();
7948
7949 // Render -l options differently for the MSVC linker.
7950 if (A.getOption().matches(options::OPT_l)) {
7951 StringRef Lib = A.getValue();
7952 const char *LinkLibArg;
7953 if (Lib.endswith(".lib"))
7954 LinkLibArg = Args.MakeArgString(Lib);
7955 else
7956 LinkLibArg = Args.MakeArgString(Lib + ".lib");
7957 CmdArgs.push_back(LinkLibArg);
7958 continue;
7959 }
7960
7961 // Otherwise, this is some other kind of linker input option like -Wl, -z,
7962 // or -L. Render it, even if MSVC doesn't understand it.
7963 A.renderAsInput(Args, CmdArgs);
7964 }
7965
7966 // It's not sufficient to just use link from the program PATH, because other
7967 // environments like GnuWin32 install their own link.exe which may come first.
7968 llvm::SmallString<128> linkPath(FindVisualStudioExecutable(
7969 getToolChain(), "link.exe", C.getDriver().getClangProgramPath()));
7970 const char *Exec = Args.MakeArgString(linkPath);
7971 C.addCommand(llvm::make_unique<Command>(JA, *this, Exec, CmdArgs));
Michael J. Spencerff58e362010-08-21 21:55:07 +00007972}
Hans Wennborgc8ba0a02013-09-19 20:32:16 +00007973
7974void visualstudio::Compile::ConstructJob(Compilation &C, const JobAction &JA,
7975 const InputInfo &Output,
7976 const InputInfoList &Inputs,
7977 const ArgList &Args,
7978 const char *LinkingOutput) const {
7979 C.addCommand(GetCommand(C, JA, Output, Inputs, Args, LinkingOutput));
7980}
7981
Stephen Hines176edba2014-12-01 14:53:08 -08007982std::unique_ptr<Command> visualstudio::Compile::GetCommand(
7983 Compilation &C, const JobAction &JA, const InputInfo &Output,
7984 const InputInfoList &Inputs, const ArgList &Args,
7985 const char *LinkingOutput) const {
Hans Wennborgc8ba0a02013-09-19 20:32:16 +00007986 ArgStringList CmdArgs;
Hans Wennborg1413d622013-09-24 17:36:21 +00007987 CmdArgs.push_back("/nologo");
Hans Wennborgc8ba0a02013-09-19 20:32:16 +00007988 CmdArgs.push_back("/c"); // Compile only.
7989 CmdArgs.push_back("/W0"); // No warnings.
7990
7991 // The goal is to be able to invoke this tool correctly based on
7992 // any flag accepted by clang-cl.
7993
7994 // These are spelled the same way in clang and cl.exe,.
7995 Args.AddAllArgs(CmdArgs, options::OPT_D, options::OPT_U);
7996 Args.AddAllArgs(CmdArgs, options::OPT_I);
Hans Wennborga8ffc162013-09-24 18:17:21 +00007997
7998 // Optimization level.
7999 if (Arg *A = Args.getLastArg(options::OPT_O, options::OPT_O0)) {
8000 if (A->getOption().getID() == options::OPT_O0) {
8001 CmdArgs.push_back("/Od");
8002 } else {
8003 StringRef OptLevel = A->getValue();
8004 if (OptLevel == "1" || OptLevel == "2" || OptLevel == "s")
8005 A->render(Args, CmdArgs);
8006 else if (OptLevel == "3")
8007 CmdArgs.push_back("/Ox");
8008 }
8009 }
Hans Wennborgc8ba0a02013-09-19 20:32:16 +00008010
8011 // Flags for which clang-cl have an alias.
8012 // FIXME: How can we ensure this stays in sync with relevant clang-cl options?
8013
Stephen Hinesef822542014-07-21 00:47:37 -07008014 if (Args.hasFlag(options::OPT__SLASH_GR_, options::OPT__SLASH_GR,
8015 /*default=*/false))
8016 CmdArgs.push_back("/GR-");
Stephen Hines651f13c2014-04-23 16:59:28 -07008017 if (Arg *A = Args.getLastArg(options::OPT_ffunction_sections,
8018 options::OPT_fno_function_sections))
8019 CmdArgs.push_back(A->getOption().getID() == options::OPT_ffunction_sections
8020 ? "/Gy"
8021 : "/Gy-");
Stephen Hines6bcf27b2014-05-29 04:14:42 -07008022 if (Arg *A = Args.getLastArg(options::OPT_fdata_sections,
8023 options::OPT_fno_data_sections))
8024 CmdArgs.push_back(
8025 A->getOption().getID() == options::OPT_fdata_sections ? "/Gw" : "/Gw-");
Hans Wennborgc8ba0a02013-09-19 20:32:16 +00008026 if (Args.hasArg(options::OPT_fsyntax_only))
8027 CmdArgs.push_back("/Zs");
Stephen Hines651f13c2014-04-23 16:59:28 -07008028 if (Args.hasArg(options::OPT_g_Flag, options::OPT_gline_tables_only))
8029 CmdArgs.push_back("/Z7");
Hans Wennborgc8ba0a02013-09-19 20:32:16 +00008030
Hans Wennborg4fe475a2013-09-27 17:54:18 +00008031 std::vector<std::string> Includes = Args.getAllArgValues(options::OPT_include);
Stephen Hinesef822542014-07-21 00:47:37 -07008032 for (const auto &Include : Includes)
8033 CmdArgs.push_back(Args.MakeArgString(std::string("/FI") + Include));
Hans Wennborg4fe475a2013-09-27 17:54:18 +00008034
Hans Wennborgc8ba0a02013-09-19 20:32:16 +00008035 // Flags that can simply be passed through.
8036 Args.AddAllArgs(CmdArgs, options::OPT__SLASH_LD);
8037 Args.AddAllArgs(CmdArgs, options::OPT__SLASH_LDd);
Stephen Hinesef822542014-07-21 00:47:37 -07008038 Args.AddAllArgs(CmdArgs, options::OPT__SLASH_EH);
Hans Wennborgc8ba0a02013-09-19 20:32:16 +00008039
8040 // The order of these flags is relevant, so pick the last one.
8041 if (Arg *A = Args.getLastArg(options::OPT__SLASH_MD, options::OPT__SLASH_MDd,
8042 options::OPT__SLASH_MT, options::OPT__SLASH_MTd))
8043 A->render(Args, CmdArgs);
8044
8045
8046 // Input filename.
8047 assert(Inputs.size() == 1);
8048 const InputInfo &II = Inputs[0];
8049 assert(II.getType() == types::TY_C || II.getType() == types::TY_CXX);
8050 CmdArgs.push_back(II.getType() == types::TY_C ? "/Tc" : "/Tp");
8051 if (II.isFilename())
8052 CmdArgs.push_back(II.getFilename());
8053 else
8054 II.getInputArg().renderAsInput(Args, CmdArgs);
8055
8056 // Output filename.
8057 assert(Output.getType() == types::TY_Object);
8058 const char *Fo = Args.MakeArgString(std::string("/Fo") +
8059 Output.getFilename());
8060 CmdArgs.push_back(Fo);
8061
Hans Wennborgdc40bf92013-09-20 18:16:35 +00008062 const Driver &D = getToolChain().getDriver();
Stephen Hines176edba2014-12-01 14:53:08 -08008063 std::string Exec = FindVisualStudioExecutable(getToolChain(), "cl.exe",
8064 D.getClangProgramPath());
8065 return llvm::make_unique<Command>(JA, *this, Args.MakeArgString(Exec),
8066 CmdArgs);
Hans Wennborgc8ba0a02013-09-19 20:32:16 +00008067}
Robert Lytton4e490e22013-10-11 10:29:40 +00008068
8069
8070/// XCore Tools
8071// We pass assemble and link construction to the xcc tool.
8072
8073void XCore::Assemble::ConstructJob(Compilation &C, const JobAction &JA,
8074 const InputInfo &Output,
8075 const InputInfoList &Inputs,
8076 const ArgList &Args,
8077 const char *LinkingOutput) const {
8078 ArgStringList CmdArgs;
8079
8080 CmdArgs.push_back("-o");
8081 CmdArgs.push_back(Output.getFilename());
8082
8083 CmdArgs.push_back("-c");
8084
Stephen Hines651f13c2014-04-23 16:59:28 -07008085 if (Args.hasArg(options::OPT_v))
8086 CmdArgs.push_back("-v");
8087
Stephen Hines6bcf27b2014-05-29 04:14:42 -07008088 if (Arg *A = Args.getLastArg(options::OPT_g_Group))
8089 if (!A->getOption().matches(options::OPT_g0))
8090 CmdArgs.push_back("-g");
Stephen Hines651f13c2014-04-23 16:59:28 -07008091
8092 if (Args.hasFlag(options::OPT_fverbose_asm, options::OPT_fno_verbose_asm,
8093 false))
8094 CmdArgs.push_back("-fverbose-asm");
Robert Lytton4e490e22013-10-11 10:29:40 +00008095
8096 Args.AddAllArgValues(CmdArgs, options::OPT_Wa_COMMA,
8097 options::OPT_Xassembler);
8098
Stephen Hinesef822542014-07-21 00:47:37 -07008099 for (const auto &II : Inputs)
Robert Lytton4e490e22013-10-11 10:29:40 +00008100 CmdArgs.push_back(II.getFilename());
Robert Lytton4e490e22013-10-11 10:29:40 +00008101
Stephen Hinesef822542014-07-21 00:47:37 -07008102 const char *Exec = Args.MakeArgString(getToolChain().GetProgramPath("xcc"));
Stephen Hines176edba2014-12-01 14:53:08 -08008103 C.addCommand(llvm::make_unique<Command>(JA, *this, Exec, CmdArgs));
Robert Lytton4e490e22013-10-11 10:29:40 +00008104}
8105
8106void XCore::Link::ConstructJob(Compilation &C, const JobAction &JA,
8107 const InputInfo &Output,
8108 const InputInfoList &Inputs,
8109 const ArgList &Args,
8110 const char *LinkingOutput) const {
8111 ArgStringList CmdArgs;
8112
8113 if (Output.isFilename()) {
8114 CmdArgs.push_back("-o");
8115 CmdArgs.push_back(Output.getFilename());
8116 } else {
8117 assert(Output.isNothing() && "Invalid output.");
8118 }
8119
Stephen Hines651f13c2014-04-23 16:59:28 -07008120 if (Args.hasArg(options::OPT_v))
8121 CmdArgs.push_back("-v");
8122
8123 ExceptionSettings EH = exceptionSettings(Args, getToolChain().getTriple());
8124 if (EH.ShouldUseExceptionTables)
8125 CmdArgs.push_back("-fexceptions");
8126
Robert Lytton4e490e22013-10-11 10:29:40 +00008127 AddLinkerInputs(getToolChain(), Inputs, Args, CmdArgs);
8128
Stephen Hinesef822542014-07-21 00:47:37 -07008129 const char *Exec = Args.MakeArgString(getToolChain().GetProgramPath("xcc"));
Stephen Hines176edba2014-12-01 14:53:08 -08008130 C.addCommand(llvm::make_unique<Command>(JA, *this, Exec, CmdArgs));
Robert Lytton4e490e22013-10-11 10:29:40 +00008131}
Stephen Hines176edba2014-12-01 14:53:08 -08008132
8133void CrossWindows::Assemble::ConstructJob(Compilation &C, const JobAction &JA,
8134 const InputInfo &Output,
8135 const InputInfoList &Inputs,
8136 const ArgList &Args,
8137 const char *LinkingOutput) const {
8138 const auto &TC =
8139 static_cast<const toolchains::CrossWindowsToolChain &>(getToolChain());
8140 ArgStringList CmdArgs;
8141 const char *Exec;
8142
8143 switch (TC.getArch()) {
8144 default: llvm_unreachable("unsupported architecture");
8145 case llvm::Triple::arm:
8146 case llvm::Triple::thumb:
8147 break;
8148 case llvm::Triple::x86:
8149 CmdArgs.push_back("--32");
8150 break;
8151 case llvm::Triple::x86_64:
8152 CmdArgs.push_back("--64");
8153 break;
8154 }
8155
8156 Args.AddAllArgValues(CmdArgs, options::OPT_Wa_COMMA, options::OPT_Xassembler);
8157
8158 CmdArgs.push_back("-o");
8159 CmdArgs.push_back(Output.getFilename());
8160
8161 for (const auto &Input : Inputs)
8162 CmdArgs.push_back(Input.getFilename());
8163
8164 const std::string Assembler = TC.GetProgramPath("as");
8165 Exec = Args.MakeArgString(Assembler);
8166
8167 C.addCommand(llvm::make_unique<Command>(JA, *this, Exec, CmdArgs));
8168}
8169
8170void CrossWindows::Link::ConstructJob(Compilation &C, const JobAction &JA,
8171 const InputInfo &Output,
8172 const InputInfoList &Inputs,
8173 const ArgList &Args,
8174 const char *LinkingOutput) const {
8175 const auto &TC =
8176 static_cast<const toolchains::CrossWindowsToolChain &>(getToolChain());
8177 const llvm::Triple &T = TC.getTriple();
8178 const Driver &D = TC.getDriver();
8179 SmallString<128> EntryPoint;
8180 ArgStringList CmdArgs;
8181 const char *Exec;
8182
8183 // Silence warning for "clang -g foo.o -o foo"
8184 Args.ClaimAllArgs(options::OPT_g_Group);
8185 // and "clang -emit-llvm foo.o -o foo"
8186 Args.ClaimAllArgs(options::OPT_emit_llvm);
8187 // and for "clang -w foo.o -o foo"
8188 Args.ClaimAllArgs(options::OPT_w);
8189 // Other warning options are already handled somewhere else.
8190
8191 if (!D.SysRoot.empty())
8192 CmdArgs.push_back(Args.MakeArgString("--sysroot=" + D.SysRoot));
8193
8194 if (Args.hasArg(options::OPT_pie))
8195 CmdArgs.push_back("-pie");
8196 if (Args.hasArg(options::OPT_rdynamic))
8197 CmdArgs.push_back("-export-dynamic");
8198 if (Args.hasArg(options::OPT_s))
8199 CmdArgs.push_back("--strip-all");
8200
8201 CmdArgs.push_back("-m");
8202 switch (TC.getArch()) {
8203 default: llvm_unreachable("unsupported architecture");
8204 case llvm::Triple::arm:
8205 case llvm::Triple::thumb:
8206 // FIXME: this is incorrect for WinCE
8207 CmdArgs.push_back("thumb2pe");
8208 break;
8209 case llvm::Triple::x86:
8210 CmdArgs.push_back("i386pe");
8211 EntryPoint.append("_");
8212 break;
8213 case llvm::Triple::x86_64:
8214 CmdArgs.push_back("i386pep");
8215 break;
8216 }
8217
8218 if (Args.hasArg(options::OPT_shared)) {
8219 switch (T.getArch()) {
8220 default: llvm_unreachable("unsupported architecture");
8221 case llvm::Triple::arm:
8222 case llvm::Triple::thumb:
8223 case llvm::Triple::x86_64:
8224 EntryPoint.append("_DllMainCRTStartup");
8225 break;
8226 case llvm::Triple::x86:
8227 EntryPoint.append("_DllMainCRTStartup@12");
8228 break;
8229 }
8230
8231 CmdArgs.push_back("-shared");
8232 CmdArgs.push_back("-Bdynamic");
8233
8234 CmdArgs.push_back("--enable-auto-image-base");
8235
8236 CmdArgs.push_back("--entry");
8237 CmdArgs.push_back(Args.MakeArgString(EntryPoint));
8238 } else {
8239 EntryPoint.append("mainCRTStartup");
8240
8241 CmdArgs.push_back(Args.hasArg(options::OPT_static) ? "-Bstatic"
8242 : "-Bdynamic");
8243
8244 if (!Args.hasArg(options::OPT_nostdlib) &&
8245 !Args.hasArg(options::OPT_nostartfiles)) {
8246 CmdArgs.push_back("--entry");
8247 CmdArgs.push_back(Args.MakeArgString(EntryPoint));
8248 }
8249
8250 // FIXME: handle subsystem
8251 }
8252
8253 // NOTE: deal with multiple definitions on Windows (e.g. COMDAT)
8254 CmdArgs.push_back("--allow-multiple-definition");
8255
8256 CmdArgs.push_back("-o");
8257 CmdArgs.push_back(Output.getFilename());
8258
8259 if (Args.hasArg(options::OPT_shared) || Args.hasArg(options::OPT_rdynamic)) {
8260 SmallString<261> ImpLib(Output.getFilename());
8261 llvm::sys::path::replace_extension(ImpLib, ".lib");
8262
8263 CmdArgs.push_back("--out-implib");
8264 CmdArgs.push_back(Args.MakeArgString(ImpLib));
8265 }
8266
8267 if (!Args.hasArg(options::OPT_nostdlib) &&
8268 !Args.hasArg(options::OPT_nostartfiles)) {
8269 const std::string CRTPath(D.SysRoot + "/usr/lib/");
8270 const char *CRTBegin;
8271
8272 CRTBegin =
8273 Args.hasArg(options::OPT_shared) ? "crtbeginS.obj" : "crtbegin.obj";
8274 CmdArgs.push_back(Args.MakeArgString(CRTPath + CRTBegin));
8275 }
8276
8277 Args.AddAllArgs(CmdArgs, options::OPT_L);
8278
8279 const auto &Paths = TC.getFilePaths();
8280 for (const auto &Path : Paths)
8281 CmdArgs.push_back(Args.MakeArgString(StringRef("-L") + Path));
8282
8283 AddLinkerInputs(TC, Inputs, Args, CmdArgs);
8284
8285 if (D.CCCIsCXX() && !Args.hasArg(options::OPT_nostdlib) &&
8286 !Args.hasArg(options::OPT_nodefaultlibs)) {
8287 bool StaticCXX = Args.hasArg(options::OPT_static_libstdcxx) &&
8288 !Args.hasArg(options::OPT_static);
8289 if (StaticCXX)
8290 CmdArgs.push_back("-Bstatic");
8291 TC.AddCXXStdlibLibArgs(Args, CmdArgs);
8292 if (StaticCXX)
8293 CmdArgs.push_back("-Bdynamic");
8294 }
8295
8296 if (!Args.hasArg(options::OPT_nostdlib)) {
8297 if (!Args.hasArg(options::OPT_nodefaultlibs)) {
8298 // TODO handle /MT[d] /MD[d]
8299 CmdArgs.push_back("-lmsvcrt");
8300 AddRunTimeLibs(TC, D, CmdArgs, Args);
8301 }
8302 }
8303
8304 const std::string Linker = TC.GetProgramPath("ld");
8305 Exec = Args.MakeArgString(Linker);
8306
8307 C.addCommand(llvm::make_unique<Command>(JA, *this, Exec, CmdArgs));
8308}
8309