blob: da27a941a1bd3665150b53f1cd1bdc184d9f1ee1 [file] [log] [blame]
Nick Lewyckye47c2452010-09-23 23:48:20 +00001//===--- Tools.cpp - Tools Implementations --------------------------------===//
Daniel Dunbar1a093d22009-03-18 06:00:36 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10#include "Tools.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000011#include "InputInfo.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000012#include "ToolChains.h"
13#include "clang/Basic/ObjCRuntime.h"
Kevin Enderbyae2ec472013-01-17 21:38:06 +000014#include "clang/Basic/Version.h"
Daniel Dunbara2aedc62009-03-18 10:01:51 +000015#include "clang/Driver/Action.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000016#include "clang/Driver/Compilation.h"
Daniel Dunbar1a8a2e82009-10-29 02:39:57 +000017#include "clang/Driver/Driver.h"
18#include "clang/Driver/DriverDiagnostic.h"
Daniel Dunbar04c4c2c2009-03-18 07:06:02 +000019#include "clang/Driver/Job.h"
Daniel Dunbarda13faf2009-11-19 04:25:22 +000020#include "clang/Driver/Options.h"
Alexey Samsonov609213f92013-08-19 09:14:21 +000021#include "clang/Driver/SanitizerArgs.h"
Daniel Dunbara3246a02009-03-18 08:07:30 +000022#include "clang/Driver/ToolChain.h"
Daniel Dunbar04c4c2c2009-03-18 07:06:02 +000023#include "clang/Driver/Util.h"
Daniel Dunbar1c28f1e2009-09-09 22:32:48 +000024#include "llvm/ADT/SmallString.h"
Douglas Gregorf7b87cb2009-10-29 00:41:01 +000025#include "llvm/ADT/StringSwitch.h"
Daniel Dunbarb4a3e432009-09-09 22:32:34 +000026#include "llvm/ADT/Twine.h"
Reid Kleckner898229a2013-06-14 17:17:23 +000027#include "llvm/Option/Arg.h"
28#include "llvm/Option/ArgList.h"
29#include "llvm/Option/Option.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000030#include "llvm/Support/ErrorHandling.h"
Michael J. Spencerf6efe582011-01-10 02:34:13 +000031#include "llvm/Support/FileSystem.h"
Daniel Dunbarc1964212009-03-26 16:23:12 +000032#include "llvm/Support/Format.h"
Michael J. Spencer8aaf4992010-11-29 18:12:39 +000033#include "llvm/Support/Host.h"
Rafael Espindola04b3fc42013-06-25 14:29:51 +000034#include "llvm/Support/Program.h"
Michael J. Spencer8aaf4992010-11-29 18:12:39 +000035#include "llvm/Support/Process.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000036#include "llvm/Support/raw_ostream.h"
Reid Kleckner898229a2013-06-14 17:17:23 +000037#include <sys/stat.h>
Daniel Dunbar04c4c2c2009-03-18 07:06:02 +000038
Daniel Dunbar1a093d22009-03-18 06:00:36 +000039using namespace clang::driver;
40using namespace clang::driver::tools;
Chris Lattner0e62c1c2011-07-23 10:55:15 +000041using namespace clang;
Reid Kleckner898229a2013-06-14 17:17:23 +000042using namespace llvm::opt;
Daniel Dunbar1a093d22009-03-18 06:00:36 +000043
Daniel Dunbar64198ef2009-09-10 01:21:05 +000044/// CheckPreprocessingOptions - Perform some validation of preprocessing
45/// arguments that is shared with gcc.
46static void CheckPreprocessingOptions(const Driver &D, const ArgList &Args) {
47 if (Arg *A = Args.getLastArg(options::OPT_C, options::OPT_CC))
Hans Wennborg70850d82013-07-18 20:29:38 +000048 if (!Args.hasArg(options::OPT_E) && !D.CCCIsCPP())
Chris Lattner0e62c1c2011-07-23 10:55:15 +000049 D.Diag(diag::err_drv_argument_only_allowed_with)
Daniel Dunbar64198ef2009-09-10 01:21:05 +000050 << A->getAsString(Args) << "-E";
51}
52
Daniel Dunbar4eadb602009-09-10 01:21:12 +000053/// CheckCodeGenerationOptions - Perform some validation of code generation
54/// arguments that is shared with gcc.
55static void CheckCodeGenerationOptions(const Driver &D, const ArgList &Args) {
56 // In gcc, only ARM checks this, but it seems reasonable to check universally.
57 if (Args.hasArg(options::OPT_static))
58 if (const Arg *A = Args.getLastArg(options::OPT_dynamic,
59 options::OPT_mdynamic_no_pic))
Chris Lattner0e62c1c2011-07-23 10:55:15 +000060 D.Diag(diag::err_drv_argument_not_allowed_with)
Daniel Dunbar4eadb602009-09-10 01:21:12 +000061 << A->getAsString(Args) << "-static";
62}
63
Chris Lattnerbf2803f2010-03-29 17:55:58 +000064// Quote target names for inclusion in GNU Make dependency files.
65// Only the characters '$', '#', ' ', '\t' are quoted.
Chris Lattner0e62c1c2011-07-23 10:55:15 +000066static void QuoteTarget(StringRef Target,
67 SmallVectorImpl<char> &Res) {
Chris Lattnerbf2803f2010-03-29 17:55:58 +000068 for (unsigned i = 0, e = Target.size(); i != e; ++i) {
69 switch (Target[i]) {
70 case ' ':
71 case '\t':
72 // Escape the preceding backslashes
73 for (int j = i - 1; j >= 0 && Target[j] == '\\'; --j)
74 Res.push_back('\\');
75
76 // Escape the space/tab
77 Res.push_back('\\');
78 break;
79 case '$':
80 Res.push_back('$');
81 break;
82 case '#':
83 Res.push_back('\\');
84 break;
85 default:
86 break;
87 }
88
89 Res.push_back(Target[i]);
90 }
91}
92
Bill Wendlingc0938f32012-03-12 22:10:06 +000093static void addDirectoryList(const ArgList &Args,
Bill Wendling281ca292012-03-12 21:22:35 +000094 ArgStringList &CmdArgs,
95 const char *ArgName,
Bill Wendlingc0938f32012-03-12 22:10:06 +000096 const char *EnvVar) {
97 const char *DirList = ::getenv(EnvVar);
Chad Rosier616e8a52012-10-30 21:42:09 +000098 bool CombinedArg = false;
99
Bill Wendling281ca292012-03-12 21:22:35 +0000100 if (!DirList)
101 return; // Nothing to do.
102
Chad Rosier616e8a52012-10-30 21:42:09 +0000103 StringRef Name(ArgName);
104 if (Name.equals("-I") || Name.equals("-L"))
105 CombinedArg = true;
106
Bill Wendling281ca292012-03-12 21:22:35 +0000107 StringRef Dirs(DirList);
108 if (Dirs.empty()) // Empty string should not add '.'.
109 return;
110
111 StringRef::size_type Delim;
Rafael Espindola04b3fc42013-06-25 14:29:51 +0000112 while ((Delim = Dirs.find(llvm::sys::EnvPathSeparator)) != StringRef::npos) {
Bill Wendling281ca292012-03-12 21:22:35 +0000113 if (Delim == 0) { // Leading colon.
Chad Rosier616e8a52012-10-30 21:42:09 +0000114 if (CombinedArg) {
115 CmdArgs.push_back(Args.MakeArgString(std::string(ArgName) + "."));
116 } else {
117 CmdArgs.push_back(ArgName);
118 CmdArgs.push_back(".");
119 }
Bill Wendling281ca292012-03-12 21:22:35 +0000120 } else {
Chad Rosier616e8a52012-10-30 21:42:09 +0000121 if (CombinedArg) {
122 CmdArgs.push_back(Args.MakeArgString(std::string(ArgName) + Dirs.substr(0, Delim)));
123 } else {
124 CmdArgs.push_back(ArgName);
125 CmdArgs.push_back(Args.MakeArgString(Dirs.substr(0, Delim)));
126 }
Bill Wendling281ca292012-03-12 21:22:35 +0000127 }
Nico Weber89355782012-03-19 15:00:03 +0000128 Dirs = Dirs.substr(Delim + 1);
Bill Wendling281ca292012-03-12 21:22:35 +0000129 }
130
131 if (Dirs.empty()) { // Trailing colon.
Chad Rosier616e8a52012-10-30 21:42:09 +0000132 if (CombinedArg) {
133 CmdArgs.push_back(Args.MakeArgString(std::string(ArgName) + "."));
134 } else {
135 CmdArgs.push_back(ArgName);
136 CmdArgs.push_back(".");
137 }
Bill Wendling281ca292012-03-12 21:22:35 +0000138 } else { // Add the last path.
Chad Rosier616e8a52012-10-30 21:42:09 +0000139 if (CombinedArg) {
140 CmdArgs.push_back(Args.MakeArgString(std::string(ArgName) + Dirs));
141 } else {
142 CmdArgs.push_back(ArgName);
143 CmdArgs.push_back(Args.MakeArgString(Dirs));
144 }
Bill Wendling281ca292012-03-12 21:22:35 +0000145 }
146}
147
Daniel Dunbar54423b22010-09-17 00:24:54 +0000148static void AddLinkerInputs(const ToolChain &TC,
149 const InputInfoList &Inputs, const ArgList &Args,
150 ArgStringList &CmdArgs) {
151 const Driver &D = TC.getDriver();
152
Daniel Dunbar1094bb12011-02-19 05:33:51 +0000153 // Add extra linker input arguments which are not treated as inputs
154 // (constructed via -Xarch_).
155 Args.AddAllArgValues(CmdArgs, options::OPT_Zlinker_input);
156
Daniel Dunbar54423b22010-09-17 00:24:54 +0000157 for (InputInfoList::const_iterator
158 it = Inputs.begin(), ie = Inputs.end(); it != ie; ++it) {
159 const InputInfo &II = *it;
160
161 if (!TC.HasNativeLLVMSupport()) {
162 // Don't try to pass LLVM inputs unless we have native support.
163 if (II.getType() == types::TY_LLVM_IR ||
164 II.getType() == types::TY_LTO_IR ||
165 II.getType() == types::TY_LLVM_BC ||
166 II.getType() == types::TY_LTO_BC)
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000167 D.Diag(diag::err_drv_no_linker_llvm_support)
Daniel Dunbar54423b22010-09-17 00:24:54 +0000168 << TC.getTripleString();
169 }
170
Daniel Dunbar2cc3f172010-09-17 00:45:02 +0000171 // Add filenames immediately.
172 if (II.isFilename()) {
Daniel Dunbar54423b22010-09-17 00:24:54 +0000173 CmdArgs.push_back(II.getFilename());
Daniel Dunbar2cc3f172010-09-17 00:45:02 +0000174 continue;
175 }
176
177 // Otherwise, this is a linker input argument.
178 const Arg &A = II.getInputArg();
179
180 // Handle reserved library options.
181 if (A.getOption().matches(options::OPT_Z_reserved_lib_stdcxx)) {
Daniel Dunbar3f7796f2010-09-17 01:20:05 +0000182 TC.AddCXXStdlibLibArgs(Args, CmdArgs);
Shantonu Senafeb03b2010-09-17 18:39:08 +0000183 } else if (A.getOption().matches(options::OPT_Z_reserved_lib_cckext)) {
184 TC.AddCCKextLibArgs(Args, CmdArgs);
Daniel Dunbar2cc3f172010-09-17 00:45:02 +0000185 } else
186 A.renderAsInput(Args, CmdArgs);
Daniel Dunbar54423b22010-09-17 00:24:54 +0000187 }
Bill Wendling281ca292012-03-12 21:22:35 +0000188
189 // LIBRARY_PATH - included following the user specified library paths.
Bill Wendlingc0938f32012-03-12 22:10:06 +0000190 addDirectoryList(Args, CmdArgs, "-L", "LIBRARY_PATH");
Daniel Dunbar54423b22010-09-17 00:24:54 +0000191}
192
John McCall31168b02011-06-15 23:02:42 +0000193/// \brief Determine whether Objective-C automated reference counting is
194/// enabled.
195static bool isObjCAutoRefCount(const ArgList &Args) {
196 return Args.hasFlag(options::OPT_fobjc_arc, options::OPT_fno_objc_arc, false);
197}
198
Ted Kremeneke65b0862012-03-06 20:05:56 +0000199/// \brief Determine whether we are linking the ObjC runtime.
200static bool isObjCRuntimeLinked(const ArgList &Args) {
Bob Wilson29536fc2012-08-07 19:58:00 +0000201 if (isObjCAutoRefCount(Args)) {
202 Args.ClaimAllArgs(options::OPT_fobjc_link_runtime);
Ted Kremeneke65b0862012-03-06 20:05:56 +0000203 return true;
Bob Wilson29536fc2012-08-07 19:58:00 +0000204 }
Ted Kremeneke65b0862012-03-06 20:05:56 +0000205 return Args.hasArg(options::OPT_fobjc_link_runtime);
206}
207
Rafael Espindola9d4a8cf2011-06-02 18:58:46 +0000208static void addProfileRT(const ToolChain &TC, const ArgList &Args,
Bill Wendling08760582011-06-27 19:15:03 +0000209 ArgStringList &CmdArgs,
210 llvm::Triple Triple) {
211 if (!(Args.hasArg(options::OPT_fprofile_arcs) ||
212 Args.hasArg(options::OPT_fprofile_generate) ||
213 Args.hasArg(options::OPT_fcreate_profile) ||
214 Args.hasArg(options::OPT_coverage)))
215 return;
216
217 // GCC links libgcov.a by adding -L<inst>/gcc/lib/gcc/<triple>/<ver> -lgcov to
218 // the link line. We cannot do the same thing because unlike gcov there is a
219 // libprofile_rt.so. We used to use the -l:libprofile_rt.a syntax, but that is
220 // not supported by old linkers.
Benjamin Kramerf5fbe432011-11-07 16:02:25 +0000221 std::string ProfileRT =
222 std::string(TC.getDriver().Dir) + "/../lib/libprofile_rt.a";
Bill Wendling08760582011-06-27 19:15:03 +0000223
Bill Wendling08760582011-06-27 19:15:03 +0000224 CmdArgs.push_back(Args.MakeArgString(ProfileRT));
Rafael Espindola9d4a8cf2011-06-02 18:58:46 +0000225}
226
Michael J. Spencer66e2b202012-10-19 22:37:06 +0000227static bool forwardToGCC(const Option &O) {
Reid Kleckner3793d5e2013-06-19 15:09:06 +0000228 // Don't forward inputs from the original command line. They are added from
229 // InputInfoList.
Richard Smith8e5e9762013-06-20 01:33:59 +0000230 return O.getKind() != Option::InputClass &&
Michael J. Spencer66e2b202012-10-19 22:37:06 +0000231 !O.hasFlag(options::DriverOption) &&
232 !O.hasFlag(options::LinkerInput);
233}
234
Peter Collingbourne9b515cb2011-11-06 00:40:05 +0000235void Clang::AddPreprocessingOptions(Compilation &C,
Chad Rosier633dcdc2013-01-24 19:14:47 +0000236 const JobAction &JA,
Peter Collingbourne9b515cb2011-11-06 00:40:05 +0000237 const Driver &D,
Douglas Gregor111af7d2009-04-18 00:34:01 +0000238 const ArgList &Args,
Daniel Dunbard067f7f2009-04-08 23:54:23 +0000239 ArgStringList &CmdArgs,
240 const InputInfo &Output,
241 const InputInfoList &Inputs) const {
Daniel Dunbard067f7f2009-04-08 23:54:23 +0000242 Arg *A;
Daniel Dunbar367dbb92009-06-08 21:48:20 +0000243
Daniel Dunbar64198ef2009-09-10 01:21:05 +0000244 CheckPreprocessingOptions(D, Args);
245
246 Args.AddLastArg(CmdArgs, options::OPT_C);
247 Args.AddLastArg(CmdArgs, options::OPT_CC);
Daniel Dunbar367dbb92009-06-08 21:48:20 +0000248
249 // Handle dependency file generation.
Daniel Dunbar86aed7d2010-12-08 21:33:40 +0000250 if ((A = Args.getLastArg(options::OPT_M, options::OPT_MM)) ||
Daniel Dunbard067f7f2009-04-08 23:54:23 +0000251 (A = Args.getLastArg(options::OPT_MD)) ||
252 (A = Args.getLastArg(options::OPT_MMD))) {
253 // Determine the output location.
254 const char *DepFile;
Benjamin Kramer2715fce2012-09-26 19:01:49 +0000255 if (Arg *MF = Args.getLastArg(options::OPT_MF)) {
Richard Smithbd55daf2012-11-01 04:30:05 +0000256 DepFile = MF->getValue();
Chad Rosier633dcdc2013-01-24 19:14:47 +0000257 C.addFailureResultFile(DepFile, &JA);
Benjamin Kramer2715fce2012-09-26 19:01:49 +0000258 } else if (Output.getType() == types::TY_Dependencies) {
259 DepFile = Output.getFilename();
Daniel Dunbar0bfb21e2009-11-19 03:26:40 +0000260 } else if (A->getOption().matches(options::OPT_M) ||
261 A->getOption().matches(options::OPT_MM)) {
Daniel Dunbard067f7f2009-04-08 23:54:23 +0000262 DepFile = "-";
263 } else {
Bob Wilsondecc03e2012-11-23 06:14:39 +0000264 DepFile = getDependencyFileName(Args, Inputs);
Chad Rosier633dcdc2013-01-24 19:14:47 +0000265 C.addFailureResultFile(DepFile, &JA);
Daniel Dunbard067f7f2009-04-08 23:54:23 +0000266 }
267 CmdArgs.push_back("-dependency-file");
268 CmdArgs.push_back(DepFile);
269
Chris Lattnerbf2803f2010-03-29 17:55:58 +0000270 // Add a default target if one wasn't specified.
Daniel Dunbard067f7f2009-04-08 23:54:23 +0000271 if (!Args.hasArg(options::OPT_MT) && !Args.hasArg(options::OPT_MQ)) {
272 const char *DepTarget;
273
274 // If user provided -o, that is the dependency target, except
275 // when we are only generating a dependency file.
276 Arg *OutputOpt = Args.getLastArg(options::OPT_o);
277 if (OutputOpt && Output.getType() != types::TY_Dependencies) {
Richard Smithbd55daf2012-11-01 04:30:05 +0000278 DepTarget = OutputOpt->getValue();
Daniel Dunbard067f7f2009-04-08 23:54:23 +0000279 } else {
280 // Otherwise derive from the base input.
281 //
282 // FIXME: This should use the computed output file location.
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +0000283 SmallString<128> P(Inputs[0].getBaseInput());
Michael J. Spencere1696752010-12-18 00:19:12 +0000284 llvm::sys::path::replace_extension(P, "o");
285 DepTarget = Args.MakeArgString(llvm::sys::path::filename(P));
Daniel Dunbard067f7f2009-04-08 23:54:23 +0000286 }
287
288 CmdArgs.push_back("-MT");
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +0000289 SmallString<128> Quoted;
Chris Lattnerbf2803f2010-03-29 17:55:58 +0000290 QuoteTarget(DepTarget, Quoted);
291 CmdArgs.push_back(Args.MakeArgString(Quoted));
Daniel Dunbard067f7f2009-04-08 23:54:23 +0000292 }
293
Daniel Dunbar0bfb21e2009-11-19 03:26:40 +0000294 if (A->getOption().matches(options::OPT_M) ||
295 A->getOption().matches(options::OPT_MD))
Daniel Dunbard067f7f2009-04-08 23:54:23 +0000296 CmdArgs.push_back("-sys-header-deps");
297 }
298
Peter Collingbourne77b0e7f22011-07-12 19:35:15 +0000299 if (Args.hasArg(options::OPT_MG)) {
300 if (!A || A->getOption().matches(options::OPT_MD) ||
301 A->getOption().matches(options::OPT_MMD))
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000302 D.Diag(diag::err_drv_mg_requires_m_or_mm);
Peter Collingbourne77b0e7f22011-07-12 19:35:15 +0000303 CmdArgs.push_back("-MG");
304 }
305
Daniel Dunbard067f7f2009-04-08 23:54:23 +0000306 Args.AddLastArg(CmdArgs, options::OPT_MP);
Chris Lattnerbf2803f2010-03-29 17:55:58 +0000307
308 // Convert all -MQ <target> args to -MT <quoted target>
309 for (arg_iterator it = Args.filtered_begin(options::OPT_MT,
310 options::OPT_MQ),
311 ie = Args.filtered_end(); it != ie; ++it) {
Daniel Dunbara442fd52010-06-11 22:00:13 +0000312 const Arg *A = *it;
313 A->claim();
Chris Lattnerbf2803f2010-03-29 17:55:58 +0000314
Daniel Dunbara442fd52010-06-11 22:00:13 +0000315 if (A->getOption().matches(options::OPT_MQ)) {
Chris Lattnerbf2803f2010-03-29 17:55:58 +0000316 CmdArgs.push_back("-MT");
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +0000317 SmallString<128> Quoted;
Richard Smithbd55daf2012-11-01 04:30:05 +0000318 QuoteTarget(A->getValue(), Quoted);
Chris Lattnerbf2803f2010-03-29 17:55:58 +0000319 CmdArgs.push_back(Args.MakeArgString(Quoted));
320
321 // -MT flag - no change
322 } else {
Daniel Dunbara442fd52010-06-11 22:00:13 +0000323 A->render(Args, CmdArgs);
Chris Lattnerbf2803f2010-03-29 17:55:58 +0000324 }
325 }
Daniel Dunbard067f7f2009-04-08 23:54:23 +0000326
Douglas Gregor111af7d2009-04-18 00:34:01 +0000327 // Add -i* options, and automatically translate to
328 // -include-pch/-include-pth for transparent PCH support. It's
329 // wonky, but we include looking for .gch so we can support seamless
330 // replacement into a build system already set up to be generating
331 // .gch files.
Argyrios Kyrtzidis2f23b412010-09-30 16:53:47 +0000332 bool RenderedImplicitInclude = false;
Daniel Dunbar44b36ee2009-11-25 11:53:23 +0000333 for (arg_iterator it = Args.filtered_begin(options::OPT_clang_i_Group),
334 ie = Args.filtered_end(); it != ie; ++it) {
335 const Arg *A = it;
Daniel Dunbard067f7f2009-04-08 23:54:23 +0000336
337 if (A->getOption().matches(options::OPT_include)) {
Argyrios Kyrtzidis2f23b412010-09-30 16:53:47 +0000338 bool IsFirstImplicitInclude = !RenderedImplicitInclude;
339 RenderedImplicitInclude = true;
340
Argyrios Kyrtzidis90bdfbb2010-08-11 23:27:58 +0000341 // Use PCH if the user requested it.
Daniel Dunbarcbc34b72009-10-15 20:02:44 +0000342 bool UsePCH = D.CCCUsePCH;
Daniel Dunbarcbc34b72009-10-15 20:02:44 +0000343
Daniel Dunbard067f7f2009-04-08 23:54:23 +0000344 bool FoundPTH = false;
Douglas Gregor111af7d2009-04-18 00:34:01 +0000345 bool FoundPCH = false;
Rafael Espindola00eaafd2013-06-25 15:03:59 +0000346 SmallString<128> P(A->getValue());
347 // We want the files to have a name like foo.h.pch. Add a dummy extension
348 // so that replace_extension does the right thing.
349 P += ".dummy";
Daniel Dunbarcbc34b72009-10-15 20:02:44 +0000350 if (UsePCH) {
Rafael Espindola00eaafd2013-06-25 15:03:59 +0000351 llvm::sys::path::replace_extension(P, "pch");
Rafael Espindola54bbaad2013-06-25 14:48:00 +0000352 if (llvm::sys::fs::exists(P.str()))
Douglas Gregor111af7d2009-04-18 00:34:01 +0000353 FoundPCH = true;
Daniel Dunbard067f7f2009-04-08 23:54:23 +0000354 }
355
Douglas Gregor111af7d2009-04-18 00:34:01 +0000356 if (!FoundPCH) {
Rafael Espindola00eaafd2013-06-25 15:03:59 +0000357 llvm::sys::path::replace_extension(P, "pth");
Rafael Espindola54bbaad2013-06-25 14:48:00 +0000358 if (llvm::sys::fs::exists(P.str()))
Douglas Gregor111af7d2009-04-18 00:34:01 +0000359 FoundPTH = true;
Mike Stump11289f42009-09-09 15:08:12 +0000360 }
361
Douglas Gregor111af7d2009-04-18 00:34:01 +0000362 if (!FoundPCH && !FoundPTH) {
Rafael Espindola00eaafd2013-06-25 15:03:59 +0000363 llvm::sys::path::replace_extension(P, "gch");
Rafael Espindola54bbaad2013-06-25 14:48:00 +0000364 if (llvm::sys::fs::exists(P.str())) {
Daniel Dunbarcbc34b72009-10-15 20:02:44 +0000365 FoundPCH = UsePCH;
366 FoundPTH = !UsePCH;
Douglas Gregor111af7d2009-04-18 00:34:01 +0000367 }
Douglas Gregor111af7d2009-04-18 00:34:01 +0000368 }
369
370 if (FoundPCH || FoundPTH) {
Argyrios Kyrtzidis2f23b412010-09-30 16:53:47 +0000371 if (IsFirstImplicitInclude) {
372 A->claim();
373 if (UsePCH)
374 CmdArgs.push_back("-include-pch");
375 else
376 CmdArgs.push_back("-include-pth");
377 CmdArgs.push_back(Args.MakeArgString(P.str()));
378 continue;
379 } else {
380 // Ignore the PCH if not first on command line and emit warning.
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000381 D.Diag(diag::warn_drv_pch_not_first_include)
Argyrios Kyrtzidis2f23b412010-09-30 16:53:47 +0000382 << P.str() << A->getAsString(Args);
383 }
Daniel Dunbard067f7f2009-04-08 23:54:23 +0000384 }
385 }
386
387 // Not translated, render as usual.
388 A->claim();
389 A->render(Args, CmdArgs);
390 }
391
392 Args.AddAllArgs(CmdArgs, options::OPT_D, options::OPT_U);
Douglas Gregor9f93e382011-07-28 04:45:53 +0000393 Args.AddAllArgs(CmdArgs, options::OPT_I_Group, options::OPT_F,
394 options::OPT_index_header_map);
Daniel Dunbard067f7f2009-04-08 23:54:23 +0000395
396 // Add -Wp, and -Xassembler if using the preprocessor.
397
398 // FIXME: There is a very unfortunate problem here, some troubled
399 // souls abuse -Wp, to pass preprocessor options in gcc syntax. To
400 // really support that we would have to parse and then translate
401 // those options. :(
402 Args.AddAllArgValues(CmdArgs, options::OPT_Wp_COMMA,
403 options::OPT_Xpreprocessor);
Daniel Dunbar38b62792009-10-29 01:53:44 +0000404
405 // -I- is a deprecated GCC feature, reject it.
406 if (Arg *A = Args.getLastArg(options::OPT_I_))
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000407 D.Diag(diag::err_drv_I_dash_not_supported) << A->getAsString(Args);
Chandler Carruth24e17e12010-10-20 07:00:47 +0000408
409 // If we have a --sysroot, and don't have an explicit -isysroot flag, add an
410 // -isysroot to the CC1 invocation.
Sebastian Pop980920a2012-04-16 04:16:43 +0000411 StringRef sysroot = C.getSysRoot();
412 if (sysroot != "") {
Chandler Carruth24e17e12010-10-20 07:00:47 +0000413 if (!Args.hasArg(options::OPT_isysroot)) {
414 CmdArgs.push_back("-isysroot");
Sebastian Pop980920a2012-04-16 04:16:43 +0000415 CmdArgs.push_back(C.getArgs().MakeArgString(sysroot));
Chandler Carruth24e17e12010-10-20 07:00:47 +0000416 }
417 }
Douglas Gregor5dc38992013-02-07 00:21:12 +0000418
Benjamin Kramer8404eb02011-09-22 21:41:16 +0000419 // Parse additional include paths from environment variables.
Chandler Carruth9802c142011-11-04 07:12:58 +0000420 // FIXME: We should probably sink the logic for handling these from the
421 // frontend into the driver. It will allow deleting 4 otherwise unused flags.
Benjamin Kramer8404eb02011-09-22 21:41:16 +0000422 // CPATH - included following the user specified includes (but prior to
423 // builtin and standard includes).
Bill Wendlingc0938f32012-03-12 22:10:06 +0000424 addDirectoryList(Args, CmdArgs, "-I", "CPATH");
Benjamin Kramer8404eb02011-09-22 21:41:16 +0000425 // C_INCLUDE_PATH - system includes enabled when compiling C.
Bill Wendlingc0938f32012-03-12 22:10:06 +0000426 addDirectoryList(Args, CmdArgs, "-c-isystem", "C_INCLUDE_PATH");
Benjamin Kramer8404eb02011-09-22 21:41:16 +0000427 // CPLUS_INCLUDE_PATH - system includes enabled when compiling C++.
Bill Wendlingc0938f32012-03-12 22:10:06 +0000428 addDirectoryList(Args, CmdArgs, "-cxx-isystem", "CPLUS_INCLUDE_PATH");
Benjamin Kramer8404eb02011-09-22 21:41:16 +0000429 // OBJC_INCLUDE_PATH - system includes enabled when compiling ObjC.
Bill Wendlingc0938f32012-03-12 22:10:06 +0000430 addDirectoryList(Args, CmdArgs, "-objc-isystem", "OBJC_INCLUDE_PATH");
Benjamin Kramer8404eb02011-09-22 21:41:16 +0000431 // OBJCPLUS_INCLUDE_PATH - system includes enabled when compiling ObjC++.
Bill Wendlingc0938f32012-03-12 22:10:06 +0000432 addDirectoryList(Args, CmdArgs, "-objcxx-isystem", "OBJCPLUS_INCLUDE_PATH");
Chandler Carruth6bfd84f2011-11-04 07:12:53 +0000433
Chandler Carruth6bfd84f2011-11-04 07:12:53 +0000434 // Add C++ include arguments, if needed.
Chandler Carruth4c81dfa2011-11-04 07:43:33 +0000435 if (types::isCXX(Inputs[0].getType()))
Chandler Carruth491db322011-11-04 07:34:47 +0000436 getToolChain().AddClangCXXStdlibIncludeArgs(Args, CmdArgs);
Chandler Carrutha796f532011-11-05 20:17:13 +0000437
438 // Add system include arguments.
439 getToolChain().AddClangSystemIncludeArgs(Args, CmdArgs);
Daniel Dunbard067f7f2009-04-08 23:54:23 +0000440}
441
Daniel Dunbarf492c922009-09-10 22:59:51 +0000442/// getLLVMArchSuffixForARM - Get the LLVM arch name to use for a particular
Daniel Dunbarfb58b0a2009-09-10 06:49:20 +0000443/// CPU.
444//
445// FIXME: This is redundant with -mcpu, why does LLVM use this.
446// FIXME: tblgen this, or kill it!
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000447static const char *getLLVMArchSuffixForARM(StringRef CPU) {
Chad Rosier9ac84512011-10-07 17:48:56 +0000448 return llvm::StringSwitch<const char *>(CPU)
Tim Northover92509c12013-06-13 15:02:46 +0000449 .Case("strongarm", "v4")
Chad Rosier9ac84512011-10-07 17:48:56 +0000450 .Cases("arm7tdmi", "arm7tdmi-s", "arm710t", "v4t")
451 .Cases("arm720t", "arm9", "arm9tdmi", "v4t")
452 .Cases("arm920", "arm920t", "arm922t", "v4t")
453 .Cases("arm940t", "ep9312","v4t")
454 .Cases("arm10tdmi", "arm1020t", "v5")
455 .Cases("arm9e", "arm926ej-s", "arm946e-s", "v5e")
456 .Cases("arm966e-s", "arm968e-s", "arm10e", "v5e")
457 .Cases("arm1020e", "arm1022e", "xscale", "iwmmxt", "v5e")
458 .Cases("arm1136j-s", "arm1136jf-s", "arm1176jz-s", "v6")
459 .Cases("arm1176jzf-s", "mpcorenovfp", "mpcore", "v6")
460 .Cases("arm1156t2-s", "arm1156t2f-s", "v6t2")
Bob Wilsonf643afc2013-03-04 22:37:46 +0000461 .Cases("cortex-a5", "cortex-a7", "cortex-a8", "v7")
Renato Golin1a04f222013-09-13 17:02:54 +0000462 .Cases("cortex-a9", "cortex-a12", "cortex-a15", "v7")
463 .Cases("cortex-r4", "cortex-r5", "v7r")
Bob Wilson7f05ca32012-03-21 17:19:12 +0000464 .Case("cortex-m0", "v6m")
Bob Wilson743bf672013-03-04 22:37:49 +0000465 .Case("cortex-m3", "v7m")
466 .Case("cortex-m4", "v7em")
Bob Wilsond7cf1042012-09-29 23:52:50 +0000467 .Case("cortex-a9-mp", "v7f")
468 .Case("swift", "v7s")
Chad Rosier9ac84512011-10-07 17:48:56 +0000469 .Default("");
Daniel Dunbarfb58b0a2009-09-10 06:49:20 +0000470}
471
Benjamin Kramer09811c72012-06-26 22:20:06 +0000472/// getARMTargetCPU - Get the (LLVM) name of the ARM cpu we are targeting.
473//
474// FIXME: tblgen this.
475static std::string getARMTargetCPU(const ArgList &Args,
476 const llvm::Triple &Triple) {
477 // FIXME: Warn on inconsistent use of -mcpu and -march.
478
479 // If we have -mcpu=, use that.
480 if (Arg *A = Args.getLastArg(options::OPT_mcpu_EQ)) {
Richard Smithbd55daf2012-11-01 04:30:05 +0000481 StringRef MCPU = A->getValue();
Benjamin Kramer09811c72012-06-26 22:20:06 +0000482 // Handle -mcpu=native.
483 if (MCPU == "native")
484 return llvm::sys::getHostCPUName();
485 else
486 return MCPU;
487 }
488
489 StringRef MArch;
490 if (Arg *A = Args.getLastArg(options::OPT_march_EQ)) {
491 // Otherwise, if we have -march= choose the base CPU for that arch.
Richard Smithbd55daf2012-11-01 04:30:05 +0000492 MArch = A->getValue();
Benjamin Kramer09811c72012-06-26 22:20:06 +0000493 } else {
494 // Otherwise, use the Arch from the triple.
495 MArch = Triple.getArchName();
496 }
497
498 // Handle -march=native.
499 std::string NativeMArch;
500 if (MArch == "native") {
501 std::string CPU = llvm::sys::getHostCPUName();
502 if (CPU != "generic") {
503 // Translate the native cpu into the architecture. The switch below will
504 // then chose the minimum cpu for that arch.
505 NativeMArch = std::string("arm") + getLLVMArchSuffixForARM(CPU);
506 MArch = NativeMArch;
507 }
508 }
509
510 return llvm::StringSwitch<const char *>(MArch)
511 .Cases("armv2", "armv2a","arm2")
512 .Case("armv3", "arm6")
513 .Case("armv3m", "arm7m")
Tim Northover92509c12013-06-13 15:02:46 +0000514 .Case("armv4", "strongarm")
515 .Case("armv4t", "arm7tdmi")
Benjamin Kramer09811c72012-06-26 22:20:06 +0000516 .Cases("armv5", "armv5t", "arm10tdmi")
517 .Cases("armv5e", "armv5te", "arm1022e")
518 .Case("armv5tej", "arm926ej-s")
519 .Cases("armv6", "armv6k", "arm1136jf-s")
520 .Case("armv6j", "arm1136j-s")
521 .Cases("armv6z", "armv6zk", "arm1176jzf-s")
522 .Case("armv6t2", "arm1156t2-s")
Bob Wilson743bf672013-03-04 22:37:49 +0000523 .Cases("armv6m", "armv6-m", "cortex-m0")
Benjamin Kramer09811c72012-06-26 22:20:06 +0000524 .Cases("armv7", "armv7a", "armv7-a", "cortex-a8")
Bob Wilson743bf672013-03-04 22:37:49 +0000525 .Cases("armv7em", "armv7e-m", "cortex-m4")
Bob Wilsond7cf1042012-09-29 23:52:50 +0000526 .Cases("armv7f", "armv7-f", "cortex-a9-mp")
527 .Cases("armv7s", "armv7-s", "swift")
Benjamin Kramer09811c72012-06-26 22:20:06 +0000528 .Cases("armv7r", "armv7-r", "cortex-r4")
529 .Cases("armv7m", "armv7-m", "cortex-m3")
Joey Goulyd077bc62013-06-26 17:19:48 +0000530 .Cases("armv8", "armv8a", "armv8-a", "cortex-a53")
Benjamin Kramer09811c72012-06-26 22:20:06 +0000531 .Case("ep9312", "ep9312")
532 .Case("iwmmxt", "iwmmxt")
533 .Case("xscale", "xscale")
Tim Northover92509c12013-06-13 15:02:46 +0000534 // If all else failed, return the most base CPU with thumb interworking
535 // supported by LLVM.
Benjamin Kramer09811c72012-06-26 22:20:06 +0000536 .Default("arm7tdmi");
537}
538
Daniel Dunbard609b7b2009-11-17 06:37:03 +0000539// FIXME: Move to target hook.
540static bool isSignedCharDefault(const llvm::Triple &Triple) {
541 switch (Triple.getArch()) {
542 default:
543 return true;
544
Tim Northover9bb857a2013-01-31 12:13:10 +0000545 case llvm::Triple::aarch64:
Jim Grosbach7c2c6642011-05-24 15:40:46 +0000546 case llvm::Triple::arm:
Daniel Dunbard609b7b2009-11-17 06:37:03 +0000547 case llvm::Triple::ppc:
548 case llvm::Triple::ppc64:
Bob Wilson6524dd32011-10-14 05:03:44 +0000549 if (Triple.isOSDarwin())
Daniel Dunbard609b7b2009-11-17 06:37:03 +0000550 return true;
551 return false;
Ulrich Weigand47445072013-05-06 16:26:41 +0000552
Bill Schmidt778d3872013-07-26 01:36:11 +0000553 case llvm::Triple::ppc64le:
Ulrich Weigand47445072013-05-06 16:26:41 +0000554 case llvm::Triple::systemz:
Robert Lytton0e076492013-08-13 09:43:10 +0000555 case llvm::Triple::xcore:
Ulrich Weigand47445072013-05-06 16:26:41 +0000556 return false;
Daniel Dunbard609b7b2009-11-17 06:37:03 +0000557 }
558}
559
Robert Lytton0e076492013-08-13 09:43:10 +0000560static bool isNoCommonDefault(const llvm::Triple &Triple) {
561 switch (Triple.getArch()) {
562 default:
563 return false;
564
565 case llvm::Triple::xcore:
566 return true;
567 }
568}
569
Chad Rosiercfbfc582012-04-04 20:51:35 +0000570// Handle -mfpu=.
571//
572// FIXME: Centralize feature selection, defaulting shouldn't be also in the
573// frontend target.
Rafael Espindola28e1f4b2013-08-21 16:39:20 +0000574static void getFPUFeatures(const Driver &D, const Arg *A, const ArgList &Args,
575 std::vector<const char *> &Features) {
Richard Smithbd55daf2012-11-01 04:30:05 +0000576 StringRef FPU = A->getValue();
Chad Rosiercfbfc582012-04-04 20:51:35 +0000577
578 // Set the target features based on the FPU.
579 if (FPU == "fpa" || FPU == "fpe2" || FPU == "fpe3" || FPU == "maverick") {
580 // Disable any default FPU support.
Rafael Espindola28e1f4b2013-08-21 16:39:20 +0000581 Features.push_back("-vfp2");
582 Features.push_back("-vfp3");
583 Features.push_back("-neon");
Chad Rosiercfbfc582012-04-04 20:51:35 +0000584 } else if (FPU == "vfp3-d16" || FPU == "vfpv3-d16") {
Rafael Espindola28e1f4b2013-08-21 16:39:20 +0000585 Features.push_back("+vfp3");
586 Features.push_back("+d16");
587 Features.push_back("-neon");
Chad Rosiercfbfc582012-04-04 20:51:35 +0000588 } else if (FPU == "vfp") {
Rafael Espindola28e1f4b2013-08-21 16:39:20 +0000589 Features.push_back("+vfp2");
590 Features.push_back("-neon");
Chad Rosiercfbfc582012-04-04 20:51:35 +0000591 } else if (FPU == "vfp3" || FPU == "vfpv3") {
Rafael Espindola28e1f4b2013-08-21 16:39:20 +0000592 Features.push_back("+vfp3");
593 Features.push_back("-neon");
Joey Gouly7db275b2013-06-27 13:19:54 +0000594 } else if (FPU == "fp-armv8") {
Joey Goulybe8c2732013-09-13 13:48:33 +0000595 Features.push_back("+fp-armv8");
Joey Gouly7db275b2013-06-27 13:19:54 +0000596 } else if (FPU == "neon-fp-armv8") {
Joey Goulybe8c2732013-09-13 13:48:33 +0000597 Features.push_back("+fp-armv8");
Rafael Espindola28e1f4b2013-08-21 16:39:20 +0000598 Features.push_back("+neon");
Amara Emersonfc362c62013-09-19 13:54:03 +0000599 } else if (FPU == "crypto-neon-fp-armv8") {
600 Features.push_back("+crypto");
601 Features.push_back("+fp-armv8");
Chad Rosiercfbfc582012-04-04 20:51:35 +0000602 } else if (FPU == "neon") {
Rafael Espindola28e1f4b2013-08-21 16:39:20 +0000603 Features.push_back("+neon");
Chad Rosiercfbfc582012-04-04 20:51:35 +0000604 } else
605 D.Diag(diag::err_drv_clang_unsupported) << A->getAsString(Args);
606}
607
Anton Korobeynikova29e4622012-04-09 13:38:30 +0000608// Select the float ABI as determined by -msoft-float, -mhard-float, and
609// -mfloat-abi=.
610static StringRef getARMFloatABI(const Driver &D,
611 const ArgList &Args,
612 const llvm::Triple &Triple) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000613 StringRef FloatABI;
Daniel Dunbar78485922009-09-10 23:00:09 +0000614 if (Arg *A = Args.getLastArg(options::OPT_msoft_float,
615 options::OPT_mhard_float,
616 options::OPT_mfloat_abi_EQ)) {
617 if (A->getOption().matches(options::OPT_msoft_float))
618 FloatABI = "soft";
619 else if (A->getOption().matches(options::OPT_mhard_float))
620 FloatABI = "hard";
621 else {
Richard Smithbd55daf2012-11-01 04:30:05 +0000622 FloatABI = A->getValue();
Daniel Dunbar78485922009-09-10 23:00:09 +0000623 if (FloatABI != "soft" && FloatABI != "softfp" && FloatABI != "hard") {
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000624 D.Diag(diag::err_drv_invalid_mfloat_abi)
Daniel Dunbar78485922009-09-10 23:00:09 +0000625 << A->getAsString(Args);
626 FloatABI = "soft";
627 }
628 }
629 }
630
631 // If unspecified, choose the default based on the platform.
632 if (FloatABI.empty()) {
Rafael Espindola0e1fb4f2010-06-28 17:18:09 +0000633 switch (Triple.getOS()) {
Bob Wilson6524dd32011-10-14 05:03:44 +0000634 case llvm::Triple::Darwin:
635 case llvm::Triple::MacOSX:
636 case llvm::Triple::IOS: {
Daniel Dunbar78485922009-09-10 23:00:09 +0000637 // Darwin defaults to "softfp" for v6 and v7.
638 //
639 // FIXME: Factor out an ARM class so we can cache the arch somewhere.
Benjamin Kramer09811c72012-06-26 22:20:06 +0000640 std::string ArchName =
Rafael Espindola0e1fb4f2010-06-28 17:18:09 +0000641 getLLVMArchSuffixForARM(getARMTargetCPU(Args, Triple));
Benjamin Kramer09811c72012-06-26 22:20:06 +0000642 if (StringRef(ArchName).startswith("v6") ||
643 StringRef(ArchName).startswith("v7"))
Daniel Dunbar78485922009-09-10 23:00:09 +0000644 FloatABI = "softfp";
645 else
646 FloatABI = "soft";
647 break;
648 }
649
Rafael Espindola0f207ed2012-12-13 04:17:14 +0000650 case llvm::Triple::FreeBSD:
651 // FreeBSD defaults to soft float
652 FloatABI = "soft";
653 break;
654
Daniel Dunbar78485922009-09-10 23:00:09 +0000655 default:
Bob Wilsond1447c42011-02-04 17:59:28 +0000656 switch(Triple.getEnvironment()) {
Jiangning Liu61b06cb2012-07-31 08:06:29 +0000657 case llvm::Triple::GNUEABIHF:
658 FloatABI = "hard";
659 break;
Bob Wilsond1447c42011-02-04 17:59:28 +0000660 case llvm::Triple::GNUEABI:
661 FloatABI = "softfp";
662 break;
663 case llvm::Triple::EABI:
664 // EABI is always AAPCS, and if it was not marked 'hard', it's softfp
665 FloatABI = "softfp";
666 break;
Logan Chienc6fd8202012-09-02 09:30:11 +0000667 case llvm::Triple::Android: {
Benjamin Kramer09811c72012-06-26 22:20:06 +0000668 std::string ArchName =
Chandler Carruthc89aa9d2012-01-10 19:47:42 +0000669 getLLVMArchSuffixForARM(getARMTargetCPU(Args, Triple));
Benjamin Kramer09811c72012-06-26 22:20:06 +0000670 if (StringRef(ArchName).startswith("v7"))
Chandler Carruthc89aa9d2012-01-10 19:47:42 +0000671 FloatABI = "softfp";
672 else
673 FloatABI = "soft";
674 break;
675 }
Bob Wilsond1447c42011-02-04 17:59:28 +0000676 default:
677 // Assume "soft", but warn the user we are guessing.
678 FloatABI = "soft";
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000679 D.Diag(diag::warn_drv_assuming_mfloat_abi_is) << "soft";
Bob Wilsond1447c42011-02-04 17:59:28 +0000680 break;
681 }
Daniel Dunbar78485922009-09-10 23:00:09 +0000682 }
683 }
684
Anton Korobeynikova29e4622012-04-09 13:38:30 +0000685 return FloatABI;
686}
687
Rafael Espindola28e1f4b2013-08-21 16:39:20 +0000688static void getARMTargetFeatures(const Driver &D, const llvm::Triple &Triple,
689 const ArgList &Args,
690 std::vector<const char *> &Features) {
691 StringRef FloatABI = getARMFloatABI(D, Args, Triple);
692 // FIXME: Note, this is a hack, the LLVM backend doesn't actually use these
693 // yet (it uses the -mfloat-abi and -msoft-float options), and it is
694 // stripped out by the ARM target.
695 // Use software floating point operations?
696 if (FloatABI == "soft")
697 Features.push_back("+soft-float");
698
699 // Use software floating point argument passing?
700 if (FloatABI != "hard")
701 Features.push_back("+soft-float-abi");
702
703 // Honor -mfpu=.
704 if (const Arg *A = Args.getLastArg(options::OPT_mfpu_EQ))
705 getFPUFeatures(D, A, Args, Features);
706
Rafael Espindola28e1f4b2013-08-21 16:39:20 +0000707 // Setting -msoft-float effectively disables NEON because of the GCC
708 // implementation, although the same isn't true of VFP or VFP3.
709 if (FloatABI == "soft")
710 Features.push_back("-neon");
711}
Anton Korobeynikova29e4622012-04-09 13:38:30 +0000712
713void Clang::AddARMTargetArgs(const ArgList &Args,
714 ArgStringList &CmdArgs,
715 bool KernelOrKext) const {
716 const Driver &D = getToolChain().getDriver();
Daniel Dunbarbd847cc2012-10-15 22:23:53 +0000717 // Get the effective triple, which takes into account the deployment target.
718 std::string TripleStr = getToolChain().ComputeEffectiveClangTriple(Args);
719 llvm::Triple Triple(TripleStr);
Daniel Dunbar5f18f6e2012-10-22 18:30:51 +0000720 std::string CPUName = getARMTargetCPU(Args, Triple);
Anton Korobeynikova29e4622012-04-09 13:38:30 +0000721
722 // Select the ABI to use.
723 //
724 // FIXME: Support -meabi.
725 const char *ABIName = 0;
726 if (Arg *A = Args.getLastArg(options::OPT_mabi_EQ)) {
Richard Smithbd55daf2012-11-01 04:30:05 +0000727 ABIName = A->getValue();
Daniel Dunbar5f18f6e2012-10-22 18:30:51 +0000728 } else if (Triple.isOSDarwin()) {
729 // The backend is hardwired to assume AAPCS for M-class processors, ensure
730 // the frontend matches that.
731 if (StringRef(CPUName).startswith("cortex-m")) {
732 ABIName = "aapcs";
733 } else {
734 ABIName = "apcs-gnu";
735 }
Anton Korobeynikova29e4622012-04-09 13:38:30 +0000736 } else {
737 // Select the default based on the platform.
738 switch(Triple.getEnvironment()) {
Logan Chienc6fd8202012-09-02 09:30:11 +0000739 case llvm::Triple::Android:
Anton Korobeynikova29e4622012-04-09 13:38:30 +0000740 case llvm::Triple::GNUEABI:
Jiangning Liu61b06cb2012-07-31 08:06:29 +0000741 case llvm::Triple::GNUEABIHF:
Anton Korobeynikova29e4622012-04-09 13:38:30 +0000742 ABIName = "aapcs-linux";
743 break;
744 case llvm::Triple::EABI:
745 ABIName = "aapcs";
746 break;
747 default:
748 ABIName = "apcs-gnu";
749 }
750 }
751 CmdArgs.push_back("-target-abi");
752 CmdArgs.push_back(ABIName);
753
Anton Korobeynikova29e4622012-04-09 13:38:30 +0000754 // Determine floating point ABI from the options & target defaults.
755 StringRef FloatABI = getARMFloatABI(D, Args, Triple);
Daniel Dunbar78485922009-09-10 23:00:09 +0000756 if (FloatABI == "soft") {
757 // Floating point operations and argument passing are soft.
758 //
759 // FIXME: This changes CPP defines, we need -target-soft-float.
Daniel Dunbara74f8ff2009-11-30 08:42:00 +0000760 CmdArgs.push_back("-msoft-float");
Daniel Dunbar6cc525b2009-12-08 19:49:51 +0000761 CmdArgs.push_back("-mfloat-abi");
762 CmdArgs.push_back("soft");
Daniel Dunbar78485922009-09-10 23:00:09 +0000763 } else if (FloatABI == "softfp") {
764 // Floating point operations are hard, but argument passing is soft.
Daniel Dunbar6cc525b2009-12-08 19:49:51 +0000765 CmdArgs.push_back("-mfloat-abi");
766 CmdArgs.push_back("soft");
Daniel Dunbar78485922009-09-10 23:00:09 +0000767 } else {
768 // Floating point operations and argument passing are hard.
769 assert(FloatABI == "hard" && "Invalid float abi!");
Daniel Dunbar6cc525b2009-12-08 19:49:51 +0000770 CmdArgs.push_back("-mfloat-abi");
771 CmdArgs.push_back("hard");
Daniel Dunbar78485922009-09-10 23:00:09 +0000772 }
Daniel Dunbar893d4752009-12-19 04:15:38 +0000773
Daniel Dunbarc9388c12011-03-17 17:10:06 +0000774 // Kernel code has more strict alignment requirements.
775 if (KernelOrKext) {
Cameron Esfahani556d91e2013-09-14 01:09:11 +0000776 if (!Triple.isiOS() || Triple.isOSVersionLT(6)) {
Daniel Dunbarbd847cc2012-10-15 22:23:53 +0000777 CmdArgs.push_back("-backend-option");
778 CmdArgs.push_back("-arm-long-calls");
779 }
Daniel Dunbarc9388c12011-03-17 17:10:06 +0000780
Daniel Dunbar12100e22011-03-22 16:48:17 +0000781 CmdArgs.push_back("-backend-option");
Daniel Dunbarc9388c12011-03-17 17:10:06 +0000782 CmdArgs.push_back("-arm-strict-align");
Daniel Dunbared904c82011-04-18 21:26:42 +0000783
784 // The kext linker doesn't know how to deal with movw/movt.
Daniel Dunbared904c82011-04-18 21:26:42 +0000785 CmdArgs.push_back("-backend-option");
Renato Golin3d510b32013-08-15 20:54:45 +0000786 CmdArgs.push_back("-arm-use-movt=0");
Daniel Dunbarb1db4b62011-03-17 00:07:34 +0000787 }
Chad Rosierba3df1d2011-08-26 00:26:29 +0000788
789 // Setting -mno-global-merge disables the codegen global merge pass. Setting
790 // -mglobal-merge has no effect as the pass is enabled by default.
791 if (Arg *A = Args.getLastArg(options::OPT_mglobal_merge,
792 options::OPT_mno_global_merge)) {
793 if (A->getOption().matches(options::OPT_mno_global_merge))
794 CmdArgs.push_back("-mno-global-merge");
795 }
Chad Rosierf1985d22012-05-16 20:40:09 +0000796
Bob Wilson9c8af452013-04-11 18:53:25 +0000797 if (!Args.hasFlag(options::OPT_mimplicit_float,
798 options::OPT_mno_implicit_float,
799 true))
Chad Rosierf1985d22012-05-16 20:40:09 +0000800 CmdArgs.push_back("-no-implicit-float");
Renato Golindbb77e62013-08-24 14:44:35 +0000801
802 // llvm does not support reserving registers in general. There is support
803 // for reserving r9 on ARM though (defined as a platform-specific register
804 // in ARM EABI).
805 if (Args.hasArg(options::OPT_ffixed_r9)) {
806 CmdArgs.push_back("-backend-option");
807 CmdArgs.push_back("-arm-reserve-r9");
808 }
Daniel Dunbar0f5c5422009-09-10 04:57:17 +0000809}
810
Simon Atanasyan2efe53e2012-09-21 20:19:32 +0000811// Translate MIPS CPU name alias option to CPU name.
812static StringRef getMipsCPUFromAlias(const Arg &A) {
813 if (A.getOption().matches(options::OPT_mips32))
814 return "mips32";
815 if (A.getOption().matches(options::OPT_mips32r2))
816 return "mips32r2";
817 if (A.getOption().matches(options::OPT_mips64))
818 return "mips64";
819 if (A.getOption().matches(options::OPT_mips64r2))
820 return "mips64r2";
821 llvm_unreachable("Unexpected option");
822 return "";
823}
824
Simon Atanasyan3b7589a2012-04-07 22:09:23 +0000825// Get CPU and ABI names. They are not independent
826// so we have to calculate them together.
827static void getMipsCPUAndABI(const ArgList &Args,
Rafael Espindola22ce34a2013-08-20 22:12:08 +0000828 const llvm::Triple &Triple,
Simon Atanasyan3b7589a2012-04-07 22:09:23 +0000829 StringRef &CPUName,
830 StringRef &ABIName) {
Simon Atanasyan464a7f72012-09-10 08:32:41 +0000831 const char *DefMips32CPU = "mips32";
832 const char *DefMips64CPU = "mips64";
Akira Hatanaka37fd9e92011-09-26 21:07:52 +0000833
Simon Atanasyan464a7f72012-09-10 08:32:41 +0000834 if (Arg *A = Args.getLastArg(options::OPT_march_EQ,
Simon Atanasyan2efe53e2012-09-21 20:19:32 +0000835 options::OPT_mcpu_EQ,
836 options::OPT_mips_CPUs_Group)) {
837 if (A->getOption().matches(options::OPT_mips_CPUs_Group))
838 CPUName = getMipsCPUFromAlias(*A);
839 else
Richard Smithbd55daf2012-11-01 04:30:05 +0000840 CPUName = A->getValue();
Simon Atanasyan2efe53e2012-09-21 20:19:32 +0000841 }
Simon Atanasyan464a7f72012-09-10 08:32:41 +0000842
Simon Atanasyan4938ddb2013-04-21 13:30:10 +0000843 if (Arg *A = Args.getLastArg(options::OPT_mabi_EQ)) {
Richard Smithbd55daf2012-11-01 04:30:05 +0000844 ABIName = A->getValue();
Simon Atanasyan4938ddb2013-04-21 13:30:10 +0000845 // Convert a GNU style Mips ABI name to the name
846 // accepted by LLVM Mips backend.
847 ABIName = llvm::StringSwitch<llvm::StringRef>(ABIName)
848 .Case("32", "o32")
849 .Case("64", "n64")
850 .Default(ABIName);
851 }
Simon Atanasyan464a7f72012-09-10 08:32:41 +0000852
853 // Setup default CPU and ABI names.
854 if (CPUName.empty() && ABIName.empty()) {
Rafael Espindola22ce34a2013-08-20 22:12:08 +0000855 switch (Triple.getArch()) {
Simon Atanasyan464a7f72012-09-10 08:32:41 +0000856 default:
857 llvm_unreachable("Unexpected triple arch name");
858 case llvm::Triple::mips:
859 case llvm::Triple::mipsel:
860 CPUName = DefMips32CPU;
861 break;
862 case llvm::Triple::mips64:
863 case llvm::Triple::mips64el:
864 CPUName = DefMips64CPU;
865 break;
866 }
867 }
868
869 if (!ABIName.empty()) {
870 // Deduce CPU name from ABI name.
871 CPUName = llvm::StringSwitch<const char *>(ABIName)
Simon Atanasyan0da400c2013-02-27 14:55:49 +0000872 .Cases("32", "o32", "eabi", DefMips32CPU)
873 .Cases("n32", "n64", "64", DefMips64CPU)
Simon Atanasyan464a7f72012-09-10 08:32:41 +0000874 .Default("");
875 }
876 else if (!CPUName.empty()) {
877 // Deduce ABI name from CPU name.
878 ABIName = llvm::StringSwitch<const char *>(CPUName)
879 .Cases("mips32", "mips32r2", "o32")
880 .Cases("mips64", "mips64r2", "n64")
881 .Default("");
882 }
883
884 // FIXME: Warn on inconsistent cpu and abi usage.
Simon Atanasyan3b7589a2012-04-07 22:09:23 +0000885}
886
Simon Atanasyan0da400c2013-02-27 14:55:49 +0000887// Convert ABI name to the GNU tools acceptable variant.
888static StringRef getGnuCompatibleMipsABIName(StringRef ABI) {
889 return llvm::StringSwitch<llvm::StringRef>(ABI)
890 .Case("o32", "32")
891 .Case("n64", "64")
892 .Default(ABI);
893}
894
Simon Atanasyan590ad8f2012-06-02 15:06:29 +0000895// Select the MIPS float ABI as determined by -msoft-float, -mhard-float,
896// and -mfloat-abi=.
897static StringRef getMipsFloatABI(const Driver &D, const ArgList &Args) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000898 StringRef FloatABI;
Eric Christopher0b26a612010-03-02 02:41:08 +0000899 if (Arg *A = Args.getLastArg(options::OPT_msoft_float,
Akira Hatanaka6976ec82012-03-23 23:07:09 +0000900 options::OPT_mhard_float,
901 options::OPT_mfloat_abi_EQ)) {
Eric Christopher0b26a612010-03-02 02:41:08 +0000902 if (A->getOption().matches(options::OPT_msoft_float))
903 FloatABI = "soft";
904 else if (A->getOption().matches(options::OPT_mhard_float))
905 FloatABI = "hard";
Akira Hatanaka6976ec82012-03-23 23:07:09 +0000906 else {
Richard Smithbd55daf2012-11-01 04:30:05 +0000907 FloatABI = A->getValue();
Simon Atanasyan512dc382013-04-14 08:37:15 +0000908 if (FloatABI != "soft" && FloatABI != "hard") {
Simon Atanasyan590ad8f2012-06-02 15:06:29 +0000909 D.Diag(diag::err_drv_invalid_mfloat_abi) << A->getAsString(Args);
Akira Hatanaka6976ec82012-03-23 23:07:09 +0000910 FloatABI = "hard";
911 }
912 }
Eric Christopher0b26a612010-03-02 02:41:08 +0000913 }
914
915 // If unspecified, choose the default based on the platform.
916 if (FloatABI.empty()) {
Akira Hatanaka6976ec82012-03-23 23:07:09 +0000917 // Assume "hard", because it's a default value used by gcc.
918 // When we start to recognize specific target MIPS processors,
919 // we will be able to select the default more correctly.
920 FloatABI = "hard";
Eric Christopher0b26a612010-03-02 02:41:08 +0000921 }
922
Simon Atanasyan590ad8f2012-06-02 15:06:29 +0000923 return FloatABI;
924}
925
Simon Atanasyan9b1932d2012-07-05 18:51:43 +0000926static void AddTargetFeature(const ArgList &Args,
Rafael Espindola28e1f4b2013-08-21 16:39:20 +0000927 std::vector<const char *> &Features,
928 OptSpecifier OnOpt, OptSpecifier OffOpt,
Simon Atanasyan9b1932d2012-07-05 18:51:43 +0000929 StringRef FeatureName) {
930 if (Arg *A = Args.getLastArg(OnOpt, OffOpt)) {
Simon Atanasyan9b1932d2012-07-05 18:51:43 +0000931 if (A->getOption().matches(OnOpt))
Rafael Espindola28e1f4b2013-08-21 16:39:20 +0000932 Features.push_back(Args.MakeArgString("+" + FeatureName));
Simon Atanasyan9b1932d2012-07-05 18:51:43 +0000933 else
Rafael Espindola28e1f4b2013-08-21 16:39:20 +0000934 Features.push_back(Args.MakeArgString("-" + FeatureName));
Simon Atanasyan9b1932d2012-07-05 18:51:43 +0000935 }
936}
937
Rafael Espindola28e1f4b2013-08-21 16:39:20 +0000938static void getMIPSTargetFeatures(const Driver &D, const ArgList &Args,
939 std::vector<const char *> &Features) {
940 StringRef FloatABI = getMipsFloatABI(D, Args);
941 bool IsMips16 = Args.getLastArg(options::OPT_mips16) != NULL;
942 if (FloatABI == "soft" || (FloatABI == "hard" && IsMips16)) {
943 // FIXME: Note, this is a hack. We need to pass the selected float
944 // mode to the MipsTargetInfoBase to define appropriate macros there.
945 // Now it is the only method.
946 Features.push_back("+soft-float");
947 }
948
949 AddTargetFeature(Args, Features, options::OPT_msingle_float,
950 options::OPT_mdouble_float, "single-float");
951 AddTargetFeature(Args, Features, options::OPT_mips16, options::OPT_mno_mips16,
952 "mips16");
953 AddTargetFeature(Args, Features, options::OPT_mmicromips,
954 options::OPT_mno_micromips, "micromips");
955 AddTargetFeature(Args, Features, options::OPT_mdsp, options::OPT_mno_dsp,
956 "dsp");
957 AddTargetFeature(Args, Features, options::OPT_mdspr2, options::OPT_mno_dspr2,
958 "dspr2");
959 AddTargetFeature(Args, Features, options::OPT_mmsa, options::OPT_mno_msa,
960 "msa");
961}
962
Simon Atanasyan590ad8f2012-06-02 15:06:29 +0000963void Clang::AddMIPSTargetArgs(const ArgList &Args,
Simon Atanasyanf0087242013-04-14 14:07:41 +0000964 ArgStringList &CmdArgs) const {
Simon Atanasyan590ad8f2012-06-02 15:06:29 +0000965 const Driver &D = getToolChain().getDriver();
966 StringRef CPUName;
967 StringRef ABIName;
Rafael Espindola22ce34a2013-08-20 22:12:08 +0000968 const llvm::Triple &Triple = getToolChain().getTriple();
969 getMipsCPUAndABI(Args, Triple, CPUName, ABIName);
Simon Atanasyan590ad8f2012-06-02 15:06:29 +0000970
971 CmdArgs.push_back("-target-abi");
972 CmdArgs.push_back(ABIName.data());
973
974 StringRef FloatABI = getMipsFloatABI(D, Args);
975
Simon Atanasyan81c423c2013-01-10 12:36:19 +0000976 bool IsMips16 = Args.getLastArg(options::OPT_mips16) != NULL;
977
978 if (FloatABI == "soft" || (FloatABI == "hard" && IsMips16)) {
Eric Christopher0b26a612010-03-02 02:41:08 +0000979 // Floating point operations and argument passing are soft.
Eric Christopher0b26a612010-03-02 02:41:08 +0000980 CmdArgs.push_back("-msoft-float");
Akira Hatanaka6976ec82012-03-23 23:07:09 +0000981 CmdArgs.push_back("-mfloat-abi");
982 CmdArgs.push_back("soft");
983
Simon Atanasyan81c423c2013-01-10 12:36:19 +0000984 if (FloatABI == "hard" && IsMips16) {
985 CmdArgs.push_back("-mllvm");
986 CmdArgs.push_back("-mips16-hard-float");
987 }
Akira Hatanaka6976ec82012-03-23 23:07:09 +0000988 }
Akira Hatanaka6976ec82012-03-23 23:07:09 +0000989 else {
990 // Floating point operations and argument passing are hard.
Eric Christopher0b26a612010-03-02 02:41:08 +0000991 assert(FloatABI == "hard" && "Invalid float abi!");
Akira Hatanaka6976ec82012-03-23 23:07:09 +0000992 CmdArgs.push_back("-mfloat-abi");
993 CmdArgs.push_back("hard");
Eric Christopher0b26a612010-03-02 02:41:08 +0000994 }
Simon Atanasyan6f23fa02012-07-05 14:19:39 +0000995
Simon Atanasyan2eaec512012-12-01 18:27:21 +0000996 if (Arg *A = Args.getLastArg(options::OPT_mxgot, options::OPT_mno_xgot)) {
997 if (A->getOption().matches(options::OPT_mxgot)) {
998 CmdArgs.push_back("-mllvm");
999 CmdArgs.push_back("-mxgot");
1000 }
1001 }
1002
Simon Atanasyanc580b322013-05-11 06:33:44 +00001003 if (Arg *A = Args.getLastArg(options::OPT_mldc1_sdc1,
1004 options::OPT_mno_ldc1_sdc1)) {
1005 if (A->getOption().matches(options::OPT_mno_ldc1_sdc1)) {
1006 CmdArgs.push_back("-mllvm");
1007 CmdArgs.push_back("-mno-ldc1-sdc1");
1008 }
1009 }
1010
Akira Hatanaka0aa60ef2013-07-19 18:58:48 +00001011 if (Arg *A = Args.getLastArg(options::OPT_mcheck_zero_division,
1012 options::OPT_mno_check_zero_division)) {
1013 if (A->getOption().matches(options::OPT_mno_check_zero_division)) {
1014 CmdArgs.push_back("-mllvm");
1015 CmdArgs.push_back("-mno-check-zero-division");
1016 }
1017 }
1018
Simon Atanasyanec4b1c12012-08-27 20:55:56 +00001019 if (Arg *A = Args.getLastArg(options::OPT_G)) {
Richard Smithbd55daf2012-11-01 04:30:05 +00001020 StringRef v = A->getValue();
Simon Atanasyanec4b1c12012-08-27 20:55:56 +00001021 CmdArgs.push_back("-mllvm");
1022 CmdArgs.push_back(Args.MakeArgString("-mips-ssection-threshold=" + v));
1023 A->claim();
1024 }
Eric Christopher0b26a612010-03-02 02:41:08 +00001025}
1026
Hal Finkel8eb59282012-06-11 22:35:19 +00001027/// getPPCTargetCPU - Get the (LLVM) name of the PowerPC cpu we are targeting.
1028static std::string getPPCTargetCPU(const ArgList &Args) {
1029 if (Arg *A = Args.getLastArg(options::OPT_mcpu_EQ)) {
Richard Smithbd55daf2012-11-01 04:30:05 +00001030 StringRef CPUName = A->getValue();
Hal Finkel8eb59282012-06-11 22:35:19 +00001031
1032 if (CPUName == "native") {
1033 std::string CPU = llvm::sys::getHostCPUName();
1034 if (!CPU.empty() && CPU != "generic")
1035 return CPU;
1036 else
1037 return "";
1038 }
1039
1040 return llvm::StringSwitch<const char *>(CPUName)
1041 .Case("common", "generic")
1042 .Case("440", "440")
1043 .Case("440fp", "440")
1044 .Case("450", "450")
1045 .Case("601", "601")
1046 .Case("602", "602")
1047 .Case("603", "603")
1048 .Case("603e", "603e")
1049 .Case("603ev", "603ev")
1050 .Case("604", "604")
1051 .Case("604e", "604e")
1052 .Case("620", "620")
Bill Schmidt38378a02013-02-01 20:23:10 +00001053 .Case("630", "pwr3")
Hal Finkel8eb59282012-06-11 22:35:19 +00001054 .Case("G3", "g3")
1055 .Case("7400", "7400")
1056 .Case("G4", "g4")
1057 .Case("7450", "7450")
1058 .Case("G4+", "g4+")
1059 .Case("750", "750")
1060 .Case("970", "970")
1061 .Case("G5", "g5")
1062 .Case("a2", "a2")
Hal Finkeldf1e4bf2013-02-01 05:53:33 +00001063 .Case("a2q", "a2q")
Hal Finkelf6d6cb02012-09-18 22:25:03 +00001064 .Case("e500mc", "e500mc")
1065 .Case("e5500", "e5500")
Bill Schmidt38378a02013-02-01 20:23:10 +00001066 .Case("power3", "pwr3")
1067 .Case("power4", "pwr4")
1068 .Case("power5", "pwr5")
1069 .Case("power5x", "pwr5x")
Hal Finkel8eb59282012-06-11 22:35:19 +00001070 .Case("power6", "pwr6")
Bill Schmidt38378a02013-02-01 20:23:10 +00001071 .Case("power6x", "pwr6x")
Hal Finkel8eb59282012-06-11 22:35:19 +00001072 .Case("power7", "pwr7")
Bill Schmidt38378a02013-02-01 20:23:10 +00001073 .Case("pwr3", "pwr3")
1074 .Case("pwr4", "pwr4")
1075 .Case("pwr5", "pwr5")
1076 .Case("pwr5x", "pwr5x")
1077 .Case("pwr6", "pwr6")
1078 .Case("pwr6x", "pwr6x")
1079 .Case("pwr7", "pwr7")
Hal Finkel8eb59282012-06-11 22:35:19 +00001080 .Case("powerpc", "ppc")
1081 .Case("powerpc64", "ppc64")
Bill Schmidt778d3872013-07-26 01:36:11 +00001082 .Case("powerpc64le", "ppc64le")
Hal Finkel8eb59282012-06-11 22:35:19 +00001083 .Default("");
1084 }
1085
1086 return "";
1087}
1088
Rafael Espindola28e1f4b2013-08-21 16:39:20 +00001089static void getPPCTargetFeatures(const ArgList &Args,
1090 std::vector<const char *> &Features) {
Bill Schmidt2fe4c672013-02-01 02:14:03 +00001091 // Allow override of the Altivec feature.
Rafael Espindola28e1f4b2013-08-21 16:39:20 +00001092 AddTargetFeature(Args, Features, options::OPT_faltivec,
1093 options::OPT_fno_altivec, "altivec");
Hal Finkelb58ce852013-02-01 18:44:19 +00001094
Rafael Espindola28e1f4b2013-08-21 16:39:20 +00001095 AddTargetFeature(Args, Features, options::OPT_mfprnd, options::OPT_mno_fprnd,
Hal Finkel7d458592013-03-30 13:47:44 +00001096 "fprnd");
1097
Hal Finkel279ca4d2013-03-28 08:38:53 +00001098 // Note that gcc calls this mfcrf and LLVM calls this mfocrf.
Rafael Espindola28e1f4b2013-08-21 16:39:20 +00001099 AddTargetFeature(Args, Features, options::OPT_mmfcrf, options::OPT_mno_mfcrf,
Hal Finkel279ca4d2013-03-28 08:38:53 +00001100 "mfocrf");
1101
Rafael Espindola28e1f4b2013-08-21 16:39:20 +00001102 AddTargetFeature(Args, Features, options::OPT_mpopcntd,
1103 options::OPT_mno_popcntd, "popcntd");
Hal Finkel1fe8b3d2013-03-28 13:51:36 +00001104
Hal Finkel279ca4d2013-03-28 08:38:53 +00001105 // It is really only possible to turn qpx off because turning qpx on is tied
1106 // to using the a2q CPU.
Rafael Espindola28e1f4b2013-08-21 16:39:20 +00001107 if (Args.hasFlag(options::OPT_mno_qpx, options::OPT_mqpx, false))
1108 Features.push_back("-qpx");
Hal Finkel8eb59282012-06-11 22:35:19 +00001109}
1110
Tom Stellard6674c702013-04-01 20:56:53 +00001111/// Get the (LLVM) name of the R600 gpu we are targeting.
1112static std::string getR600TargetGPU(const ArgList &Args) {
1113 if (Arg *A = Args.getLastArg(options::OPT_mcpu_EQ)) {
1114 std::string GPUName = A->getValue();
1115 return llvm::StringSwitch<const char *>(GPUName)
Tom Stellardb38600c2013-05-06 16:12:05 +00001116 .Cases("rv630", "rv635", "r600")
1117 .Cases("rv610", "rv620", "rs780", "rs880")
Tom Stellard6674c702013-04-01 20:56:53 +00001118 .Case("rv740", "rv770")
1119 .Case("palm", "cedar")
Tom Stellardb38600c2013-05-06 16:12:05 +00001120 .Cases("sumo", "sumo2", "sumo")
Tom Stellard6674c702013-04-01 20:56:53 +00001121 .Case("hemlock", "cypress")
1122 .Case("aruba", "cayman")
1123 .Default(GPUName.c_str());
1124 }
1125 return "";
1126}
1127
Rafael Espindola28e1f4b2013-08-21 16:39:20 +00001128static void getSparcTargetFeatures(const ArgList &Args,
1129 std::vector<const char *> Features) {
1130 bool SoftFloatABI = true;
1131 if (Arg *A =
1132 Args.getLastArg(options::OPT_msoft_float, options::OPT_mhard_float)) {
1133 if (A->getOption().matches(options::OPT_mhard_float))
1134 SoftFloatABI = false;
1135 }
1136 if (SoftFloatABI)
1137 Features.push_back("+soft-float");
1138}
1139
Bruno Cardoso Lopese7f211c2010-11-09 17:21:19 +00001140void Clang::AddSparcTargetArgs(const ArgList &Args,
1141 ArgStringList &CmdArgs) const {
1142 const Driver &D = getToolChain().getDriver();
1143
Bruno Cardoso Lopese7f211c2010-11-09 17:21:19 +00001144 // Select the float ABI as determined by -msoft-float, -mhard-float, and
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001145 StringRef FloatABI;
Bruno Cardoso Lopese7f211c2010-11-09 17:21:19 +00001146 if (Arg *A = Args.getLastArg(options::OPT_msoft_float,
1147 options::OPT_mhard_float)) {
1148 if (A->getOption().matches(options::OPT_msoft_float))
1149 FloatABI = "soft";
1150 else if (A->getOption().matches(options::OPT_mhard_float))
1151 FloatABI = "hard";
1152 }
1153
1154 // If unspecified, choose the default based on the platform.
1155 if (FloatABI.empty()) {
Aaron Ballmand0d27ab2013-07-15 13:41:33 +00001156 // Assume "soft", but warn the user we are guessing.
1157 FloatABI = "soft";
1158 D.Diag(diag::warn_drv_assuming_mfloat_abi_is) << "soft";
Bruno Cardoso Lopese7f211c2010-11-09 17:21:19 +00001159 }
1160
1161 if (FloatABI == "soft") {
1162 // Floating point operations and argument passing are soft.
1163 //
1164 // FIXME: This changes CPP defines, we need -target-soft-float.
1165 CmdArgs.push_back("-msoft-float");
Bruno Cardoso Lopese7f211c2010-11-09 17:21:19 +00001166 } else {
1167 assert(FloatABI == "hard" && "Invalid float abi!");
1168 CmdArgs.push_back("-mhard-float");
1169 }
1170}
1171
Richard Sandiford4652d892013-07-19 16:51:51 +00001172static const char *getSystemZTargetCPU(const ArgList &Args) {
1173 if (const Arg *A = Args.getLastArg(options::OPT_march_EQ))
1174 return A->getValue();
1175 return "z10";
1176}
1177
Chandler Carruth953fb082013-01-13 11:46:33 +00001178static const char *getX86TargetCPU(const ArgList &Args,
1179 const llvm::Triple &Triple) {
1180 if (const Arg *A = Args.getLastArg(options::OPT_march_EQ)) {
1181 if (StringRef(A->getValue()) != "native")
1182 return A->getValue();
1183
1184 // FIXME: Reject attempts to use -march=native unless the target matches
1185 // the host.
1186 //
1187 // FIXME: We should also incorporate the detected target features for use
1188 // with -native.
1189 std::string CPU = llvm::sys::getHostCPUName();
1190 if (!CPU.empty() && CPU != "generic")
1191 return Args.MakeArgString(CPU);
1192 }
1193
1194 // Select the default CPU if none was given (or detection failed).
1195
1196 if (Triple.getArch() != llvm::Triple::x86_64 &&
1197 Triple.getArch() != llvm::Triple::x86)
1198 return 0; // This routine is only handling x86 targets.
1199
1200 bool Is64Bit = Triple.getArch() == llvm::Triple::x86_64;
1201
1202 // FIXME: Need target hooks.
1203 if (Triple.isOSDarwin())
1204 return Is64Bit ? "core2" : "yonah";
1205
1206 // Everything else goes to x86-64 in 64-bit mode.
1207 if (Is64Bit)
1208 return "x86-64";
1209
1210 if (Triple.getOSName().startswith("haiku"))
1211 return "i586";
1212 if (Triple.getOSName().startswith("openbsd"))
1213 return "i486";
1214 if (Triple.getOSName().startswith("bitrig"))
1215 return "i686";
1216 if (Triple.getOSName().startswith("freebsd"))
1217 return "i486";
1218 if (Triple.getOSName().startswith("netbsd"))
1219 return "i486";
1220 // All x86 devices running Android have core2 as their common
1221 // denominator. This makes a better choice than pentium4.
1222 if (Triple.getEnvironment() == llvm::Triple::Android)
1223 return "core2";
1224
1225 // Fallback to p4.
1226 return "pentium4";
1227}
1228
Rafael Espindola22ce34a2013-08-20 22:12:08 +00001229static std::string getCPUName(const ArgList &Args, const llvm::Triple &T) {
1230 switch(T.getArch()) {
1231 default:
1232 return "";
1233
1234 case llvm::Triple::arm:
1235 case llvm::Triple::thumb:
1236 return getARMTargetCPU(Args, T);
1237
1238 case llvm::Triple::mips:
1239 case llvm::Triple::mipsel:
1240 case llvm::Triple::mips64:
1241 case llvm::Triple::mips64el: {
1242 StringRef CPUName;
1243 StringRef ABIName;
1244 getMipsCPUAndABI(Args, T, CPUName, ABIName);
1245 return CPUName;
1246 }
1247
1248 case llvm::Triple::ppc:
1249 case llvm::Triple::ppc64:
1250 case llvm::Triple::ppc64le: {
1251 std::string TargetCPUName = getPPCTargetCPU(Args);
1252 // LLVM may default to generating code for the native CPU,
1253 // but, like gcc, we default to a more generic option for
1254 // each architecture. (except on Darwin)
1255 if (TargetCPUName.empty() && !T.isOSDarwin()) {
1256 if (T.getArch() == llvm::Triple::ppc64)
1257 TargetCPUName = "ppc64";
1258 else if (T.getArch() == llvm::Triple::ppc64le)
1259 TargetCPUName = "ppc64le";
1260 else
1261 TargetCPUName = "ppc";
1262 }
1263 return TargetCPUName;
1264 }
1265
1266 case llvm::Triple::sparc:
1267 if (const Arg *A = Args.getLastArg(options::OPT_march_EQ))
1268 return A->getValue();
1269 return "";
1270
1271 case llvm::Triple::x86:
1272 case llvm::Triple::x86_64:
1273 return getX86TargetCPU(Args, T);
1274
1275 case llvm::Triple::hexagon:
1276 return "hexagon" + toolchains::Hexagon_TC::GetTargetCPU(Args).str();
1277
1278 case llvm::Triple::systemz:
1279 return getSystemZTargetCPU(Args);
1280
1281 case llvm::Triple::r600:
1282 return getR600TargetGPU(Args);
1283 }
1284}
1285
Rafael Espindola28e1f4b2013-08-21 16:39:20 +00001286static void getX86TargetFeatures(const ArgList &Args,
1287 std::vector<const char *> &Features) {
1288 for (arg_iterator it = Args.filtered_begin(options::OPT_m_x86_Features_Group),
1289 ie = Args.filtered_end();
1290 it != ie; ++it) {
1291 StringRef Name = (*it)->getOption().getName();
1292 (*it)->claim();
1293
1294 // Skip over "-m".
1295 assert(Name.startswith("m") && "Invalid feature name.");
1296 Name = Name.substr(1);
1297
1298 bool IsNegative = Name.startswith("no-");
1299 if (IsNegative)
1300 Name = Name.substr(3);
1301
1302 Features.push_back(Args.MakeArgString((IsNegative ? "-" : "+") + Name));
1303 }
1304}
1305
Daniel Dunbar3b3191f2009-09-09 22:33:08 +00001306void Clang::AddX86TargetArgs(const ArgList &Args,
1307 ArgStringList &CmdArgs) const {
Daniel Dunbare2cf8f72009-09-10 22:59:57 +00001308 if (!Args.hasFlag(options::OPT_mred_zone,
1309 options::OPT_mno_red_zone,
1310 true) ||
1311 Args.hasArg(options::OPT_mkernel) ||
1312 Args.hasArg(options::OPT_fapple_kext))
Daniel Dunbar8bed86c2009-11-20 22:21:36 +00001313 CmdArgs.push_back("-disable-red-zone");
Daniel Dunbare2cf8f72009-09-10 22:59:57 +00001314
Bob Wilson2616e2e2013-02-10 16:01:41 +00001315 // Default to avoid implicit floating-point for kernel/kext code, but allow
1316 // that to be overridden with -mno-soft-float.
1317 bool NoImplicitFloat = (Args.hasArg(options::OPT_mkernel) ||
1318 Args.hasArg(options::OPT_fapple_kext));
1319 if (Arg *A = Args.getLastArg(options::OPT_msoft_float,
1320 options::OPT_mno_soft_float,
Bob Wilson9c8af452013-04-11 18:53:25 +00001321 options::OPT_mimplicit_float,
Bob Wilson2616e2e2013-02-10 16:01:41 +00001322 options::OPT_mno_implicit_float)) {
1323 const Option &O = A->getOption();
1324 NoImplicitFloat = (O.matches(options::OPT_mno_implicit_float) ||
1325 O.matches(options::OPT_msoft_float));
1326 }
1327 if (NoImplicitFloat)
Daniel Dunbar8bed86c2009-11-20 22:21:36 +00001328 CmdArgs.push_back("-no-implicit-float");
Daniel Dunbar3b3191f2009-09-09 22:33:08 +00001329}
1330
Matthew Curtise8f80a12012-12-06 17:49:03 +00001331static inline bool HasPICArg(const ArgList &Args) {
1332 return Args.hasArg(options::OPT_fPIC)
1333 || Args.hasArg(options::OPT_fpic);
1334}
1335
1336static Arg *GetLastSmallDataThresholdArg(const ArgList &Args) {
1337 return Args.getLastArg(options::OPT_G,
1338 options::OPT_G_EQ,
1339 options::OPT_msmall_data_threshold_EQ);
1340}
1341
1342static std::string GetHexagonSmallDataThresholdValue(const ArgList &Args) {
1343 std::string value;
1344 if (HasPICArg(Args))
1345 value = "0";
1346 else if (Arg *A = GetLastSmallDataThresholdArg(Args)) {
1347 value = A->getValue();
1348 A->claim();
1349 }
1350 return value;
1351}
1352
Tony Linthicum76329bf2011-12-12 21:14:55 +00001353void Clang::AddHexagonTargetArgs(const ArgList &Args,
1354 ArgStringList &CmdArgs) const {
Tony Linthicum76329bf2011-12-12 21:14:55 +00001355 CmdArgs.push_back("-fno-signed-char");
Matthew Curtis6b222782012-12-07 13:52:44 +00001356 CmdArgs.push_back("-mqdsp6-compat");
1357 CmdArgs.push_back("-Wreturn-type");
Tony Linthicum76329bf2011-12-12 21:14:55 +00001358
Matthew Curtise8f80a12012-12-06 17:49:03 +00001359 std::string SmallDataThreshold = GetHexagonSmallDataThresholdValue(Args);
1360 if (!SmallDataThreshold.empty()) {
Tony Linthicum76329bf2011-12-12 21:14:55 +00001361 CmdArgs.push_back ("-mllvm");
Matthew Curtise8f80a12012-12-06 17:49:03 +00001362 CmdArgs.push_back(Args.MakeArgString(
1363 "-hexagon-small-data-threshold=" + SmallDataThreshold));
Tony Linthicum76329bf2011-12-12 21:14:55 +00001364 }
1365
Sirish Pande11ebc4e2012-05-10 20:19:54 +00001366 if (!Args.hasArg(options::OPT_fno_short_enums))
1367 CmdArgs.push_back("-fshort-enums");
1368 if (Args.getLastArg(options::OPT_mieee_rnd_near)) {
1369 CmdArgs.push_back ("-mllvm");
1370 CmdArgs.push_back ("-enable-hexagon-ieee-rnd-near");
1371 }
Tony Linthicum76329bf2011-12-12 21:14:55 +00001372 CmdArgs.push_back ("-mllvm");
1373 CmdArgs.push_back ("-machine-sink-split=0");
1374}
1375
Rafael Espindola28e1f4b2013-08-21 16:39:20 +00001376static void getAArch64TargetFeatures(const Driver &D, const ArgList &Args,
1377 std::vector<const char *> &Features) {
Tim Northover2fe823a2013-08-01 09:23:19 +00001378 // Honor -mfpu=.
1379 if (const Arg *A = Args.getLastArg(options::OPT_mfpu_EQ))
Rafael Espindola28e1f4b2013-08-21 16:39:20 +00001380 getFPUFeatures(D, A, Args, Features);
1381}
1382
1383static void getTargetFeatures(const Driver &D, const llvm::Triple &Triple,
1384 const ArgList &Args, ArgStringList &CmdArgs) {
1385 std::vector<const char *> Features;
1386 switch (Triple.getArch()) {
1387 default:
1388 break;
1389 case llvm::Triple::mips:
1390 case llvm::Triple::mipsel:
1391 case llvm::Triple::mips64:
1392 case llvm::Triple::mips64el:
1393 getMIPSTargetFeatures(D, Args, Features);
1394 break;
1395
1396 case llvm::Triple::arm:
1397 case llvm::Triple::thumb:
1398 getARMTargetFeatures(D, Triple, Args, Features);
1399 break;
1400
1401 case llvm::Triple::ppc:
1402 case llvm::Triple::ppc64:
1403 case llvm::Triple::ppc64le:
1404 getPPCTargetFeatures(Args, Features);
1405 break;
1406 case llvm::Triple::sparc:
1407 getSparcTargetFeatures(Args, Features);
1408 break;
1409 case llvm::Triple::aarch64:
1410 getAArch64TargetFeatures(D, Args, Features);
1411 break;
1412 case llvm::Triple::x86:
1413 case llvm::Triple::x86_64:
1414 getX86TargetFeatures(Args, Features);
1415 break;
1416 }
Rafael Espindola43964802013-08-21 17:34:32 +00001417
1418 // Find the last of each feature.
1419 llvm::StringMap<unsigned> LastOpt;
1420 for (unsigned I = 0, N = Features.size(); I < N; ++I) {
1421 const char *Name = Features[I];
1422 assert(Name[0] == '-' || Name[0] == '+');
1423 LastOpt[Name + 1] = I;
1424 }
1425
1426 for (unsigned I = 0, N = Features.size(); I < N; ++I) {
1427 // If this feature was overridden, ignore it.
1428 const char *Name = Features[I];
1429 llvm::StringMap<unsigned>::iterator LastI = LastOpt.find(Name + 1);
1430 assert(LastI != LastOpt.end());
1431 unsigned Last = LastI->second;
1432 if (Last != I)
1433 continue;
1434
Rafael Espindola28e1f4b2013-08-21 16:39:20 +00001435 CmdArgs.push_back("-target-feature");
Rafael Espindola43964802013-08-21 17:34:32 +00001436 CmdArgs.push_back(Name);
Rafael Espindola28e1f4b2013-08-21 16:39:20 +00001437 }
Tim Northover2fe823a2013-08-01 09:23:19 +00001438}
1439
Eric Christopher84fbdb42011-08-19 00:30:14 +00001440static bool
John McCall5fb5df92012-06-20 06:18:46 +00001441shouldUseExceptionTablesForObjCExceptions(const ObjCRuntime &runtime,
Anders Carlsson246ff3f2011-02-28 00:44:51 +00001442 const llvm::Triple &Triple) {
1443 // We use the zero-cost exception tables for Objective-C if the non-fragile
1444 // ABI is enabled or when compiling for x86_64 and ARM on Snow Leopard and
1445 // later.
John McCall5fb5df92012-06-20 06:18:46 +00001446 if (runtime.isNonFragile())
Anders Carlsson246ff3f2011-02-28 00:44:51 +00001447 return true;
1448
Bob Wilson6524dd32011-10-14 05:03:44 +00001449 if (!Triple.isOSDarwin())
Anders Carlsson246ff3f2011-02-28 00:44:51 +00001450 return false;
1451
Eric Christopherbf15d2b2011-07-02 00:20:22 +00001452 return (!Triple.isMacOSXVersionLT(10,5) &&
Anders Carlsson246ff3f2011-02-28 00:44:51 +00001453 (Triple.getArch() == llvm::Triple::x86_64 ||
Eric Christopher84fbdb42011-08-19 00:30:14 +00001454 Triple.getArch() == llvm::Triple::arm));
Anders Carlsson246ff3f2011-02-28 00:44:51 +00001455}
1456
Anders Carlssone96ab552011-02-28 02:27:16 +00001457/// addExceptionArgs - Adds exception related arguments to the driver command
1458/// arguments. There's a master flag, -fexceptions and also language specific
1459/// flags to enable/disable C++ and Objective-C exceptions.
1460/// This makes it possible to for example disable C++ exceptions but enable
1461/// Objective-C exceptions.
1462static void addExceptionArgs(const ArgList &Args, types::ID InputType,
1463 const llvm::Triple &Triple,
Fariborz Jahanianb2482212012-04-04 18:28:00 +00001464 bool KernelOrKext,
John McCall5fb5df92012-06-20 06:18:46 +00001465 const ObjCRuntime &objcRuntime,
Anders Carlssone96ab552011-02-28 02:27:16 +00001466 ArgStringList &CmdArgs) {
Chad Rosier4fab82c2012-03-26 22:04:46 +00001467 if (KernelOrKext) {
1468 // -mkernel and -fapple-kext imply no exceptions, so claim exception related
1469 // arguments now to avoid warnings about unused arguments.
1470 Args.ClaimAllArgs(options::OPT_fexceptions);
1471 Args.ClaimAllArgs(options::OPT_fno_exceptions);
1472 Args.ClaimAllArgs(options::OPT_fobjc_exceptions);
1473 Args.ClaimAllArgs(options::OPT_fno_objc_exceptions);
1474 Args.ClaimAllArgs(options::OPT_fcxx_exceptions);
1475 Args.ClaimAllArgs(options::OPT_fno_cxx_exceptions);
Anders Carlssone96ab552011-02-28 02:27:16 +00001476 return;
Chad Rosier4fab82c2012-03-26 22:04:46 +00001477 }
Anders Carlssone96ab552011-02-28 02:27:16 +00001478
1479 // Exceptions are enabled by default.
1480 bool ExceptionsEnabled = true;
1481
1482 // This keeps track of whether exceptions were explicitly turned on or off.
1483 bool DidHaveExplicitExceptionFlag = false;
1484
Rafael Espindola00a66572009-10-01 13:33:33 +00001485 if (Arg *A = Args.getLastArg(options::OPT_fexceptions,
1486 options::OPT_fno_exceptions)) {
1487 if (A->getOption().matches(options::OPT_fexceptions))
Anders Carlssone96ab552011-02-28 02:27:16 +00001488 ExceptionsEnabled = true;
Eric Christopher84fbdb42011-08-19 00:30:14 +00001489 else
Anders Carlssone96ab552011-02-28 02:27:16 +00001490 ExceptionsEnabled = false;
1491
1492 DidHaveExplicitExceptionFlag = true;
Rafael Espindola00a66572009-10-01 13:33:33 +00001493 }
Daniel Dunbar30a12b82010-09-14 23:12:31 +00001494
Anders Carlssone96ab552011-02-28 02:27:16 +00001495 bool ShouldUseExceptionTables = false;
Fariborz Jahaniane4b21ab2009-10-01 20:30:46 +00001496
Anders Carlssone96ab552011-02-28 02:27:16 +00001497 // Exception tables and cleanups can be enabled with -fexceptions even if the
1498 // language itself doesn't support exceptions.
1499 if (ExceptionsEnabled && DidHaveExplicitExceptionFlag)
1500 ShouldUseExceptionTables = true;
Daniel Dunbar30a12b82010-09-14 23:12:31 +00001501
Daniel Dunbarc44f8cf2011-03-17 23:28:31 +00001502 // Obj-C exceptions are enabled by default, regardless of -fexceptions. This
1503 // is not necessarily sensible, but follows GCC.
1504 if (types::isObjC(InputType) &&
Eric Christopher84fbdb42011-08-19 00:30:14 +00001505 Args.hasFlag(options::OPT_fobjc_exceptions,
Daniel Dunbarc44f8cf2011-03-17 23:28:31 +00001506 options::OPT_fno_objc_exceptions,
1507 true)) {
1508 CmdArgs.push_back("-fobjc-exceptions");
Anders Carlssone96ab552011-02-28 02:27:16 +00001509
Eric Christopher84fbdb42011-08-19 00:30:14 +00001510 ShouldUseExceptionTables |=
John McCall5fb5df92012-06-20 06:18:46 +00001511 shouldUseExceptionTablesForObjCExceptions(objcRuntime, Triple);
Anders Carlssone96ab552011-02-28 02:27:16 +00001512 }
1513
1514 if (types::isCXX(InputType)) {
1515 bool CXXExceptionsEnabled = ExceptionsEnabled;
1516
Eric Christopher84fbdb42011-08-19 00:30:14 +00001517 if (Arg *A = Args.getLastArg(options::OPT_fcxx_exceptions,
1518 options::OPT_fno_cxx_exceptions,
Anders Carlssone96ab552011-02-28 02:27:16 +00001519 options::OPT_fexceptions,
1520 options::OPT_fno_exceptions)) {
1521 if (A->getOption().matches(options::OPT_fcxx_exceptions))
1522 CXXExceptionsEnabled = true;
Chandler Carruth74f87112011-02-28 07:25:18 +00001523 else if (A->getOption().matches(options::OPT_fno_cxx_exceptions))
Anders Carlssone96ab552011-02-28 02:27:16 +00001524 CXXExceptionsEnabled = false;
1525 }
1526
1527 if (CXXExceptionsEnabled) {
1528 CmdArgs.push_back("-fcxx-exceptions");
1529
1530 ShouldUseExceptionTables = true;
1531 }
1532 }
1533
1534 if (ShouldUseExceptionTables)
1535 CmdArgs.push_back("-fexceptions");
Rafael Espindola00a66572009-10-01 13:33:33 +00001536}
1537
Daniel Dunbare246fbe2013-04-16 18:21:19 +00001538static bool ShouldDisableAutolink(const ArgList &Args,
1539 const ToolChain &TC) {
1540 bool Default = true;
1541 if (TC.getTriple().isOSDarwin()) {
1542 // The native darwin assembler doesn't support the linker_option directives,
1543 // so we disable them if we think the .s file will be passed to it.
1544 Default = TC.useIntegratedAs();
1545 }
1546 return !Args.hasFlag(options::OPT_fautolink, options::OPT_fno_autolink,
1547 Default);
1548}
1549
Rafael Espindola4cfa7972011-05-02 17:43:32 +00001550static bool ShouldDisableCFI(const ArgList &Args,
1551 const ToolChain &TC) {
Rafael Espindolac41db922012-03-08 14:39:55 +00001552 bool Default = true;
Bob Wilson6524dd32011-10-14 05:03:44 +00001553 if (TC.getTriple().isOSDarwin()) {
Rafael Espindolaf934f982011-05-17 16:26:17 +00001554 // The native darwin assembler doesn't support cfi directives, so
Rafael Espindola35ab91c2011-05-17 19:06:58 +00001555 // we disable them if we think the .s file will be passed to it.
Rafael Espindola84b588b2013-03-18 18:10:27 +00001556 Default = TC.useIntegratedAs();
Rafael Espindolaf934f982011-05-17 16:26:17 +00001557 }
Rafael Espindolac41db922012-03-08 14:39:55 +00001558 return !Args.hasFlag(options::OPT_fdwarf2_cfi_asm,
Eric Christopher45f2e712012-12-18 00:31:10 +00001559 options::OPT_fno_dwarf2_cfi_asm,
1560 Default);
Rafael Espindola4cfa7972011-05-02 17:43:32 +00001561}
1562
Ted Kremenek62093662013-03-12 17:02:12 +00001563static bool ShouldDisableDwarfDirectory(const ArgList &Args,
1564 const ToolChain &TC) {
Nick Lewycky1d617ac2011-10-17 23:05:52 +00001565 bool UseDwarfDirectory = Args.hasFlag(options::OPT_fdwarf_directory_asm,
1566 options::OPT_fno_dwarf_directory_asm,
Rafael Espindola84b588b2013-03-18 18:10:27 +00001567 TC.useIntegratedAs());
Nick Lewycky1d617ac2011-10-17 23:05:52 +00001568 return !UseDwarfDirectory;
1569}
1570
Joerg Sonnenbergeref317a22011-05-06 14:35:16 +00001571/// \brief Check whether the given input tree contains any compilation actions.
1572static bool ContainsCompileAction(const Action *A) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001573 if (isa<CompileJobAction>(A))
Joerg Sonnenbergeref317a22011-05-06 14:35:16 +00001574 return true;
1575
1576 for (Action::const_iterator it = A->begin(), ie = A->end(); it != ie; ++it)
1577 if (ContainsCompileAction(*it))
1578 return true;
1579
1580 return false;
1581}
1582
1583/// \brief Check if -relax-all should be passed to the internal assembler.
1584/// This is done by default when compiling non-assembler source with -O0.
1585static bool UseRelaxAll(Compilation &C, const ArgList &Args) {
1586 bool RelaxDefault = true;
1587
1588 if (Arg *A = Args.getLastArg(options::OPT_O_Group))
1589 RelaxDefault = A->getOption().matches(options::OPT_O0);
1590
1591 if (RelaxDefault) {
1592 RelaxDefault = false;
1593 for (ActionList::const_iterator it = C.getActions().begin(),
1594 ie = C.getActions().end(); it != ie; ++it) {
1595 if (ContainsCompileAction(*it)) {
1596 RelaxDefault = true;
1597 break;
1598 }
1599 }
1600 }
1601
1602 return Args.hasFlag(options::OPT_mrelax_all, options::OPT_mno_relax_all,
1603 RelaxDefault);
1604}
1605
David Blaikie9260ed62013-07-25 21:19:01 +00001606static void CollectArgsForIntegratedAssembler(Compilation &C,
1607 const ArgList &Args,
1608 ArgStringList &CmdArgs,
1609 const Driver &D) {
1610 if (UseRelaxAll(C, Args))
1611 CmdArgs.push_back("-mrelax-all");
1612
1613 // When using an integrated assembler, translate -Wa, and -Xassembler
1614 // options.
1615 for (arg_iterator it = Args.filtered_begin(options::OPT_Wa_COMMA,
1616 options::OPT_Xassembler),
1617 ie = Args.filtered_end(); it != ie; ++it) {
1618 const Arg *A = *it;
1619 A->claim();
1620
1621 for (unsigned i = 0, e = A->getNumValues(); i != e; ++i) {
1622 StringRef Value = A->getValue(i);
1623
1624 if (Value == "-force_cpusubtype_ALL") {
1625 // Do nothing, this is the default and we don't support anything else.
1626 } else if (Value == "-L") {
1627 CmdArgs.push_back("-msave-temp-labels");
1628 } else if (Value == "--fatal-warnings") {
1629 CmdArgs.push_back("-mllvm");
1630 CmdArgs.push_back("-fatal-assembler-warnings");
1631 } else if (Value == "--noexecstack") {
1632 CmdArgs.push_back("-mnoexecstack");
1633 } else {
1634 D.Diag(diag::err_drv_unsupported_option_argument)
1635 << A->getOption().getName() << Value;
1636 }
1637 }
1638 }
1639}
1640
Chandler Carruth36381702013-06-23 11:28:48 +00001641static void addProfileRTLinux(
1642 const ToolChain &TC, const ArgList &Args, ArgStringList &CmdArgs) {
1643 if (!(Args.hasArg(options::OPT_fprofile_arcs) ||
1644 Args.hasArg(options::OPT_fprofile_generate) ||
1645 Args.hasArg(options::OPT_fcreate_profile) ||
1646 Args.hasArg(options::OPT_coverage)))
1647 return;
1648
1649 // The profile runtime is located in the Linux library directory and has name
1650 // "libclang_rt.profile-<ArchName>.a".
1651 SmallString<128> LibProfile(TC.getDriver().ResourceDir);
1652 llvm::sys::path::append(
1653 LibProfile, "lib", "linux",
1654 Twine("libclang_rt.profile-") + TC.getArchName() + ".a");
1655
1656 CmdArgs.push_back(Args.MakeArgString(LibProfile));
1657}
1658
Alexey Samsonovbfb0cd32013-02-27 11:14:55 +00001659static void addSanitizerRTLinkFlagsLinux(
1660 const ToolChain &TC, const ArgList &Args, ArgStringList &CmdArgs,
Richard Smithf3e624c2013-03-23 00:30:08 +00001661 const StringRef Sanitizer, bool BeforeLibStdCXX,
1662 bool ExportSymbols = true) {
Alexey Samsonovbfb0cd32013-02-27 11:14:55 +00001663 // Sanitizer runtime is located in the Linux library directory and
1664 // has name "libclang_rt.<Sanitizer>-<ArchName>.a".
1665 SmallString<128> LibSanitizer(TC.getDriver().ResourceDir);
1666 llvm::sys::path::append(
1667 LibSanitizer, "lib", "linux",
1668 (Twine("libclang_rt.") + Sanitizer + "-" + TC.getArchName() + ".a"));
Richard Smithcff3cde2013-03-20 23:49:07 +00001669
Alexey Samsonovbfb0cd32013-02-27 11:14:55 +00001670 // Sanitizer runtime may need to come before -lstdc++ (or -lc++, libstdc++.a,
1671 // etc.) so that the linker picks custom versions of the global 'operator
1672 // new' and 'operator delete' symbols. We take the extreme (but simple)
1673 // strategy of inserting it at the front of the link command. It also
1674 // needs to be forced to end up in the executable, so wrap it in
1675 // whole-archive.
Richard Smithcff3cde2013-03-20 23:49:07 +00001676 SmallVector<const char *, 3> LibSanitizerArgs;
1677 LibSanitizerArgs.push_back("-whole-archive");
1678 LibSanitizerArgs.push_back(Args.MakeArgString(LibSanitizer));
1679 LibSanitizerArgs.push_back("-no-whole-archive");
1680
1681 CmdArgs.insert(BeforeLibStdCXX ? CmdArgs.begin() : CmdArgs.end(),
1682 LibSanitizerArgs.begin(), LibSanitizerArgs.end());
1683
Alexey Samsonovbfb0cd32013-02-27 11:14:55 +00001684 CmdArgs.push_back("-lpthread");
Evgeniy Stepanov758b5072013-05-24 14:28:03 +00001685 CmdArgs.push_back("-lrt");
Alexey Samsonovbfb0cd32013-02-27 11:14:55 +00001686 CmdArgs.push_back("-ldl");
Richard Smithf3e624c2013-03-23 00:30:08 +00001687
1688 // If possible, use a dynamic symbols file to export the symbols from the
1689 // runtime library. If we can't do so, use -export-dynamic instead to export
1690 // all symbols from the binary.
1691 if (ExportSymbols) {
1692 if (llvm::sys::fs::exists(LibSanitizer + ".syms"))
1693 CmdArgs.push_back(
1694 Args.MakeArgString("--dynamic-list=" + LibSanitizer + ".syms"));
1695 else
1696 CmdArgs.push_back("-export-dynamic");
1697 }
Alexey Samsonovbfb0cd32013-02-27 11:14:55 +00001698}
1699
Kostya Serebryany7bca6c22011-11-30 01:39:16 +00001700/// If AddressSanitizer is enabled, add appropriate linker flags (Linux).
1701/// This needs to be called before we add the C run-time (malloc, etc).
1702static void addAsanRTLinux(const ToolChain &TC, const ArgList &Args,
Kostya Serebryany0b692ce2011-12-06 19:18:44 +00001703 ArgStringList &CmdArgs) {
Logan Chienc6fd8202012-09-02 09:30:11 +00001704 if(TC.getTriple().getEnvironment() == llvm::Triple::Android) {
Evgeniy Stepanovcac6aaa2012-09-12 09:09:08 +00001705 SmallString<128> LibAsan(TC.getDriver().ResourceDir);
1706 llvm::sys::path::append(LibAsan, "lib", "linux",
1707 (Twine("libclang_rt.asan-") +
1708 TC.getArchName() + "-android.so"));
Matt Beaumont-Gay35439df2012-12-04 21:18:26 +00001709 CmdArgs.insert(CmdArgs.begin(), Args.MakeArgString(LibAsan));
Evgeniy Stepanov77866712012-04-25 08:59:22 +00001710 } else {
Sergey Matveev1814e9e2013-05-27 11:17:01 +00001711 if (!Args.hasArg(options::OPT_shared))
Alexey Samsonovbfb0cd32013-02-27 11:14:55 +00001712 addSanitizerRTLinkFlagsLinux(TC, Args, CmdArgs, "asan", true);
Evgeniy Stepanov77866712012-04-25 08:59:22 +00001713 }
Kostya Serebryany7bca6c22011-11-30 01:39:16 +00001714}
1715
Kostya Serebryany66e8fab2012-05-16 06:36:00 +00001716/// If ThreadSanitizer is enabled, add appropriate linker flags (Linux).
1717/// This needs to be called before we add the C run-time (malloc, etc).
1718static void addTsanRTLinux(const ToolChain &TC, const ArgList &Args,
1719 ArgStringList &CmdArgs) {
Sergey Matveev1814e9e2013-05-27 11:17:01 +00001720 if (!Args.hasArg(options::OPT_shared))
Alexey Samsonovbfb0cd32013-02-27 11:14:55 +00001721 addSanitizerRTLinkFlagsLinux(TC, Args, CmdArgs, "tsan", true);
Kostya Serebryany66e8fab2012-05-16 06:36:00 +00001722}
1723
Evgeniy Stepanovaea92e52012-12-03 13:20:43 +00001724/// If MemorySanitizer is enabled, add appropriate linker flags (Linux).
1725/// This needs to be called before we add the C run-time (malloc, etc).
1726static void addMsanRTLinux(const ToolChain &TC, const ArgList &Args,
1727 ArgStringList &CmdArgs) {
Sergey Matveev1814e9e2013-05-27 11:17:01 +00001728 if (!Args.hasArg(options::OPT_shared))
Alexey Samsonovbfb0cd32013-02-27 11:14:55 +00001729 addSanitizerRTLinkFlagsLinux(TC, Args, CmdArgs, "msan", true);
Sergey Matveev1814e9e2013-05-27 11:17:01 +00001730}
1731
1732/// If LeakSanitizer is enabled, add appropriate linker flags (Linux).
1733/// This needs to be called before we add the C run-time (malloc, etc).
1734static void addLsanRTLinux(const ToolChain &TC, const ArgList &Args,
1735 ArgStringList &CmdArgs) {
1736 if (!Args.hasArg(options::OPT_shared))
1737 addSanitizerRTLinkFlagsLinux(TC, Args, CmdArgs, "lsan", true);
Evgeniy Stepanovaea92e52012-12-03 13:20:43 +00001738}
1739
Richard Smithe30752c2012-10-09 19:52:38 +00001740/// If UndefinedBehaviorSanitizer is enabled, add appropriate linker flags
1741/// (Linux).
1742static void addUbsanRTLinux(const ToolChain &TC, const ArgList &Args,
Richard Smithcff3cde2013-03-20 23:49:07 +00001743 ArgStringList &CmdArgs, bool IsCXX,
1744 bool HasOtherSanitizerRt) {
Richard Smithcff3cde2013-03-20 23:49:07 +00001745 // Need a copy of sanitizer_common. This could come from another sanitizer
1746 // runtime; if we're not including one, include our own copy.
1747 if (!HasOtherSanitizerRt)
Richard Smithf3e624c2013-03-23 00:30:08 +00001748 addSanitizerRTLinkFlagsLinux(TC, Args, CmdArgs, "san", true, false);
Richard Smithcff3cde2013-03-20 23:49:07 +00001749
Alexey Samsonovbfb0cd32013-02-27 11:14:55 +00001750 addSanitizerRTLinkFlagsLinux(TC, Args, CmdArgs, "ubsan", false);
Richard Smithcff3cde2013-03-20 23:49:07 +00001751
1752 // Only include the bits of the runtime which need a C++ ABI library if
1753 // we're linking in C++ mode.
1754 if (IsCXX)
1755 addSanitizerRTLinkFlagsLinux(TC, Args, CmdArgs, "ubsan_cxx", false);
Richard Smithe30752c2012-10-09 19:52:38 +00001756}
1757
Peter Collingbournec3772752013-08-07 22:47:34 +00001758static void addDfsanRTLinux(const ToolChain &TC, const ArgList &Args,
1759 ArgStringList &CmdArgs) {
1760 if (!Args.hasArg(options::OPT_shared))
1761 addSanitizerRTLinkFlagsLinux(TC, Args, CmdArgs, "dfsan", true);
1762}
1763
Rafael Espindola224dd632011-12-14 21:02:23 +00001764static bool shouldUseFramePointer(const ArgList &Args,
1765 const llvm::Triple &Triple) {
1766 if (Arg *A = Args.getLastArg(options::OPT_fno_omit_frame_pointer,
1767 options::OPT_fomit_frame_pointer))
1768 return A->getOption().matches(options::OPT_fno_omit_frame_pointer);
1769
Richard Sandiford896f8db2013-07-19 14:06:49 +00001770 // Don't use a frame pointer on linux x86, x86_64 and z if optimizing.
Rafael Espindola224dd632011-12-14 21:02:23 +00001771 if ((Triple.getArch() == llvm::Triple::x86_64 ||
Richard Sandiford896f8db2013-07-19 14:06:49 +00001772 Triple.getArch() == llvm::Triple::x86 ||
1773 Triple.getArch() == llvm::Triple::systemz) &&
Cameron Esfahani556d91e2013-09-14 01:09:11 +00001774 Triple.isOSLinux()) {
Rafael Espindola224dd632011-12-14 21:02:23 +00001775 if (Arg *A = Args.getLastArg(options::OPT_O_Group))
1776 if (!A->getOption().matches(options::OPT_O0))
1777 return false;
1778 }
1779
Robert Lytton0e076492013-08-13 09:43:10 +00001780 if (Triple.getArch() == llvm::Triple::xcore)
1781 return false;
1782
Rafael Espindola224dd632011-12-14 21:02:23 +00001783 return true;
1784}
1785
Eric Christopherb7d97e92013-04-03 01:58:53 +00001786static bool shouldUseLeafFramePointer(const ArgList &Args,
1787 const llvm::Triple &Triple) {
1788 if (Arg *A = Args.getLastArg(options::OPT_mno_omit_leaf_frame_pointer,
1789 options::OPT_momit_leaf_frame_pointer))
1790 return A->getOption().matches(options::OPT_mno_omit_leaf_frame_pointer);
1791
Richard Sandiford896f8db2013-07-19 14:06:49 +00001792 // Don't use a leaf frame pointer on linux x86, x86_64 and z if optimizing.
Eric Christopherb7d97e92013-04-03 01:58:53 +00001793 if ((Triple.getArch() == llvm::Triple::x86_64 ||
Richard Sandiford896f8db2013-07-19 14:06:49 +00001794 Triple.getArch() == llvm::Triple::x86 ||
1795 Triple.getArch() == llvm::Triple::systemz) &&
Cameron Esfahani556d91e2013-09-14 01:09:11 +00001796 Triple.isOSLinux()) {
Eric Christopherb7d97e92013-04-03 01:58:53 +00001797 if (Arg *A = Args.getLastArg(options::OPT_O_Group))
1798 if (!A->getOption().matches(options::OPT_O0))
1799 return false;
1800 }
1801
Robert Lytton0e076492013-08-13 09:43:10 +00001802 if (Triple.getArch() == llvm::Triple::xcore)
1803 return false;
1804
Eric Christopherb7d97e92013-04-03 01:58:53 +00001805 return true;
1806}
1807
Rafael Espindolac7367ff2013-08-10 01:40:10 +00001808/// Add a CC1 option to specify the debug compilation directory.
Chandler Carruth4d5e1a92012-12-17 21:40:04 +00001809static void addDebugCompDirArg(const ArgList &Args, ArgStringList &CmdArgs) {
Benjamin Kramer698d7c82013-04-27 08:12:29 +00001810 SmallString<128> cwd;
1811 if (!llvm::sys::fs::current_path(cwd)) {
Chad Rosiera35d5a32013-04-26 20:49:50 +00001812 CmdArgs.push_back("-fdebug-compilation-dir");
1813 CmdArgs.push_back(Args.MakeArgString(cwd));
Chandler Carruth4d5e1a92012-12-17 21:40:04 +00001814 }
1815}
1816
Eric Christopherd3804002013-02-22 20:12:52 +00001817static const char *SplitDebugName(const ArgList &Args,
1818 const InputInfoList &Inputs) {
1819 Arg *FinalOutput = Args.getLastArg(options::OPT_o);
1820 if (FinalOutput && Args.hasArg(options::OPT_c)) {
1821 SmallString<128> T(FinalOutput->getValue());
1822 llvm::sys::path::replace_extension(T, "dwo");
1823 return Args.MakeArgString(T);
1824 } else {
1825 // Use the compilation dir.
1826 SmallString<128> T(Args.getLastArgValue(options::OPT_fdebug_compilation_dir));
1827 SmallString<128> F(llvm::sys::path::stem(Inputs[0].getBaseInput()));
1828 llvm::sys::path::replace_extension(F, "dwo");
1829 T += F;
1830 return Args.MakeArgString(F);
1831 }
1832}
1833
1834static void SplitDebugInfo(const ToolChain &TC, Compilation &C,
1835 const Tool &T, const JobAction &JA,
1836 const ArgList &Args, const InputInfo &Output,
1837 const char *OutFile) {
Eric Christopher248357f2013-02-21 22:35:01 +00001838 ArgStringList ExtractArgs;
1839 ExtractArgs.push_back("--extract-dwo");
1840
1841 ArgStringList StripArgs;
1842 StripArgs.push_back("--strip-dwo");
1843
1844 // Grabbing the output of the earlier compile step.
1845 StripArgs.push_back(Output.getFilename());
1846 ExtractArgs.push_back(Output.getFilename());
Eric Christopher248357f2013-02-21 22:35:01 +00001847 ExtractArgs.push_back(OutFile);
1848
1849 const char *Exec =
Eric Christopherd3804002013-02-22 20:12:52 +00001850 Args.MakeArgString(TC.GetProgramPath("objcopy"));
Eric Christopher248357f2013-02-21 22:35:01 +00001851
1852 // First extract the dwo sections.
Eric Christopherd3804002013-02-22 20:12:52 +00001853 C.addCommand(new Command(JA, T, Exec, ExtractArgs));
Eric Christopher248357f2013-02-21 22:35:01 +00001854
1855 // Then remove them from the original .o file.
Eric Christopherd3804002013-02-22 20:12:52 +00001856 C.addCommand(new Command(JA, T, Exec, StripArgs));
Eric Christopher248357f2013-02-21 22:35:01 +00001857}
1858
Chad Rosierb71f6aa2013-04-24 18:09:54 +00001859static bool isOptimizationLevelFast(const ArgList &Args) {
1860 if (Arg *A = Args.getLastArg(options::OPT_O_Group))
1861 if (A->getOption().matches(options::OPT_Ofast))
1862 return true;
1863 return false;
1864}
1865
Arnold Schwaighofer7f994ce2013-08-13 15:46:23 +00001866/// \brief Vectorize at all optimization levels greater than 1 except for -Oz.
1867static bool shouldEnableVectorizerAtOLevel(const ArgList &Args) {
1868 if (Arg *A = Args.getLastArg(options::OPT_O_Group)) {
Rafael Espindolaad70d962013-08-27 16:58:15 +00001869 if (A->getOption().matches(options::OPT_O4) ||
1870 A->getOption().matches(options::OPT_Ofast))
Arnold Schwaighofer7f994ce2013-08-13 15:46:23 +00001871 return true;
1872
1873 if (A->getOption().matches(options::OPT_O0))
1874 return false;
1875
1876 assert(A->getOption().matches(options::OPT_O) && "Must have a -O flag");
1877
Rafael Espindola91780de2013-08-26 14:05:41 +00001878 // Vectorize -Os.
Arnold Schwaighofer7f994ce2013-08-13 15:46:23 +00001879 StringRef S(A->getValue());
Rafael Espindola91780de2013-08-26 14:05:41 +00001880 if (S == "s")
Arnold Schwaighofer7f994ce2013-08-13 15:46:23 +00001881 return true;
1882
1883 // Don't vectorize -Oz.
1884 if (S == "z")
1885 return false;
1886
1887 unsigned OptLevel = 0;
1888 if (S.getAsInteger(10, OptLevel))
1889 return false;
1890
1891 return OptLevel > 1;
1892 }
1893
1894 return false;
1895}
1896
Daniel Dunbar1a093d22009-03-18 06:00:36 +00001897void Clang::ConstructJob(Compilation &C, const JobAction &JA,
Daniel Dunbar04c4c2c2009-03-18 07:06:02 +00001898 const InputInfo &Output,
Daniel Dunbar0450e6d2009-03-18 06:07:59 +00001899 const InputInfoList &Inputs,
Daniel Dunbara2aedc62009-03-18 10:01:51 +00001900 const ArgList &Args,
Daniel Dunbar1a093d22009-03-18 06:00:36 +00001901 const char *LinkingOutput) const {
Daniel Dunbare46b52a2010-03-20 04:52:14 +00001902 bool KernelOrKext = Args.hasArg(options::OPT_mkernel,
1903 options::OPT_fapple_kext);
Daniel Dunbar083edf72009-12-21 18:54:17 +00001904 const Driver &D = getToolChain().getDriver();
Daniel Dunbara3246a02009-03-18 08:07:30 +00001905 ArgStringList CmdArgs;
1906
Daniel Dunbare521a892009-03-31 20:53:55 +00001907 assert(Inputs.size() == 1 && "Unable to handle multiple inputs.");
1908
Daniel Dunbar6c536aa2009-12-11 23:00:49 +00001909 // Invoke ourselves in -cc1 mode.
1910 //
1911 // FIXME: Implement custom jobs for internal actions.
1912 CmdArgs.push_back("-cc1");
1913
Daniel Dunbar624c21b2009-10-30 18:12:20 +00001914 // Add the "effective" target triple.
Daniel Dunbard640be22009-03-31 17:35:15 +00001915 CmdArgs.push_back("-triple");
Daniel Dunbar82eb4ce2010-08-23 22:35:37 +00001916 std::string TripleStr = getToolChain().ComputeEffectiveClangTriple(Args);
Daniel Dunbar624c21b2009-10-30 18:12:20 +00001917 CmdArgs.push_back(Args.MakeArgString(TripleStr));
Daniel Dunbarfb58b0a2009-09-10 06:49:20 +00001918
Daniel Dunbar624c21b2009-10-30 18:12:20 +00001919 // Select the appropriate action.
John McCall5fb5df92012-06-20 06:18:46 +00001920 RewriteKind rewriteKind = RK_None;
Fariborz Jahanian7ebeede2012-04-04 18:50:28 +00001921
Daniel Dunbara2aedc62009-03-18 10:01:51 +00001922 if (isa<AnalyzeJobAction>(JA)) {
1923 assert(JA.getType() == types::TY_Plist && "Invalid output type.");
1924 CmdArgs.push_back("-analyze");
Ted Kremenekf7639e12012-03-06 20:06:33 +00001925 } else if (isa<MigrateJobAction>(JA)) {
1926 CmdArgs.push_back("-migrate");
Daniel Dunbara2aedc62009-03-18 10:01:51 +00001927 } else if (isa<PreprocessJobAction>(JA)) {
Daniel Dunbard67a3222009-03-30 06:36:42 +00001928 if (Output.getType() == types::TY_Dependencies)
1929 CmdArgs.push_back("-Eonly");
Fariborz Jahaniane0a75d62013-03-18 19:41:18 +00001930 else {
Daniel Dunbard67a3222009-03-30 06:36:42 +00001931 CmdArgs.push_back("-E");
Fariborz Jahaniane0a75d62013-03-18 19:41:18 +00001932 if (Args.hasArg(options::OPT_rewrite_objc) &&
1933 !Args.hasArg(options::OPT_g_Group))
1934 CmdArgs.push_back("-P");
1935 }
Daniel Dunbarc4343942010-02-03 03:07:56 +00001936 } else if (isa<AssembleJobAction>(JA)) {
1937 CmdArgs.push_back("-emit-obj");
Daniel Dunbar06e2cc32010-05-27 06:18:05 +00001938
David Blaikie9260ed62013-07-25 21:19:01 +00001939 CollectArgsForIntegratedAssembler(C, Args, CmdArgs, D);
Daniel Dunbar7c874332010-11-19 16:23:35 +00001940
1941 // Also ignore explicit -force_cpusubtype_ALL option.
1942 (void) Args.hasArg(options::OPT_force__cpusubtype__ALL);
Daniel Dunbara2aedc62009-03-18 10:01:51 +00001943 } else if (isa<PrecompileJobAction>(JA)) {
Argyrios Kyrtzidis90bdfbb2010-08-11 23:27:58 +00001944 // Use PCH if the user requested it.
Daniel Dunbarcbc34b72009-10-15 20:02:44 +00001945 bool UsePCH = D.CCCUsePCH;
Daniel Dunbarcbc34b72009-10-15 20:02:44 +00001946
Aaron Ballman1f10cc52012-07-31 01:21:00 +00001947 if (JA.getType() == types::TY_Nothing)
1948 CmdArgs.push_back("-fsyntax-only");
1949 else if (UsePCH)
Douglas Gregor111af7d2009-04-18 00:34:01 +00001950 CmdArgs.push_back("-emit-pch");
1951 else
1952 CmdArgs.push_back("-emit-pth");
Daniel Dunbara2aedc62009-03-18 10:01:51 +00001953 } else {
1954 assert(isa<CompileJobAction>(JA) && "Invalid action for clang tool.");
Daniel Dunbard067f7f2009-04-08 23:54:23 +00001955
Daniel Dunbara2aedc62009-03-18 10:01:51 +00001956 if (JA.getType() == types::TY_Nothing) {
1957 CmdArgs.push_back("-fsyntax-only");
Daniel Dunbar24e52992010-06-07 23:28:45 +00001958 } else if (JA.getType() == types::TY_LLVM_IR ||
1959 JA.getType() == types::TY_LTO_IR) {
Daniel Dunbara2aedc62009-03-18 10:01:51 +00001960 CmdArgs.push_back("-emit-llvm");
Daniel Dunbar24e52992010-06-07 23:28:45 +00001961 } else if (JA.getType() == types::TY_LLVM_BC ||
1962 JA.getType() == types::TY_LTO_BC) {
Daniel Dunbara2aedc62009-03-18 10:01:51 +00001963 CmdArgs.push_back("-emit-llvm-bc");
1964 } else if (JA.getType() == types::TY_PP_Asm) {
Daniel Dunbard112f102009-09-17 00:47:53 +00001965 CmdArgs.push_back("-S");
Daniel Dunbar6cdf83c2009-09-01 16:57:46 +00001966 } else if (JA.getType() == types::TY_AST) {
1967 CmdArgs.push_back("-emit-pch");
Douglas Gregorbf7fc9c2013-03-27 16:47:18 +00001968 } else if (JA.getType() == types::TY_ModuleFile) {
1969 CmdArgs.push_back("-module-file-info");
Daniel Dunbaraeea8ac2010-02-11 03:16:21 +00001970 } else if (JA.getType() == types::TY_RewrittenObjC) {
1971 CmdArgs.push_back("-rewrite-objc");
John McCall5fb5df92012-06-20 06:18:46 +00001972 rewriteKind = RK_NonFragile;
Fariborz Jahanian73223bb2012-04-02 15:59:19 +00001973 } else if (JA.getType() == types::TY_RewrittenLegacyObjC) {
1974 CmdArgs.push_back("-rewrite-objc");
John McCall5fb5df92012-06-20 06:18:46 +00001975 rewriteKind = RK_Fragile;
Daniel Dunbaraeea8ac2010-02-11 03:16:21 +00001976 } else {
1977 assert(JA.getType() == types::TY_PP_Asm &&
1978 "Unexpected output type!");
Daniel Dunbara2aedc62009-03-18 10:01:51 +00001979 }
Daniel Dunbara3246a02009-03-18 08:07:30 +00001980 }
1981
Daniel Dunbara2aedc62009-03-18 10:01:51 +00001982 // The make clang go fast button.
1983 CmdArgs.push_back("-disable-free");
1984
John McCallbb79b5f2010-02-13 03:50:24 +00001985 // Disable the verification pass in -asserts builds.
1986#ifdef NDEBUG
1987 CmdArgs.push_back("-disable-llvm-verifier");
1988#endif
1989
Daniel Dunbar3b358a32009-04-08 05:11:16 +00001990 // Set the main file name, so that debug info works even with
1991 // -save-temps.
1992 CmdArgs.push_back("-main-file-name");
Bob Wilsondecc03e2012-11-23 06:14:39 +00001993 CmdArgs.push_back(getBaseInputName(Args, Inputs));
Daniel Dunbar3b358a32009-04-08 05:11:16 +00001994
Daniel Dunbar17ddaa62009-04-08 18:03:55 +00001995 // Some flags which affect the language (via preprocessor
Bob Wilsondecc03e2012-11-23 06:14:39 +00001996 // defines).
Daniel Dunbar17ddaa62009-04-08 18:03:55 +00001997 if (Args.hasArg(options::OPT_static))
1998 CmdArgs.push_back("-static-define");
1999
Daniel Dunbara2aedc62009-03-18 10:01:51 +00002000 if (isa<AnalyzeJobAction>(JA)) {
Ted Kremenek05e6f5b2009-09-25 05:55:59 +00002001 // Enable region store model by default.
2002 CmdArgs.push_back("-analyzer-store=region");
2003
Ted Kremenek7bea9a12009-12-07 22:26:14 +00002004 // Treat blocks as analysis entry points.
2005 CmdArgs.push_back("-analyzer-opt-analyze-nested-blocks");
2006
Ted Kremenek49c79792011-03-24 00:28:47 +00002007 CmdArgs.push_back("-analyzer-eagerly-assume");
2008
Daniel Dunbara2aedc62009-03-18 10:01:51 +00002009 // Add default argument set.
Daniel Dunbar58f345ce2009-05-22 00:38:15 +00002010 if (!Args.hasArg(options::OPT__analyzer_no_default_checks)) {
Argyrios Kyrtzidisa6d04d52011-02-15 07:42:33 +00002011 CmdArgs.push_back("-analyzer-checker=core");
Ted Kremenek49c79792011-03-24 00:28:47 +00002012
Argyrios Kyrtzidisa6d04d52011-02-15 07:42:33 +00002013 if (getToolChain().getTriple().getOS() != llvm::Triple::Win32)
2014 CmdArgs.push_back("-analyzer-checker=unix");
Ted Kremenek49c79792011-03-24 00:28:47 +00002015
Argyrios Kyrtzidisa6d04d52011-02-15 07:42:33 +00002016 if (getToolChain().getTriple().getVendor() == llvm::Triple::Apple)
Ted Kremenek49c79792011-03-24 00:28:47 +00002017 CmdArgs.push_back("-analyzer-checker=osx");
Ted Kremenekb9ff6b22012-01-20 06:00:17 +00002018
2019 CmdArgs.push_back("-analyzer-checker=deadcode");
Ted Kremenek37e96522012-01-26 02:27:38 +00002020
Jordan Rose10ad0812013-04-05 17:55:07 +00002021 if (types::isCXX(Inputs[0].getType()))
2022 CmdArgs.push_back("-analyzer-checker=cplusplus");
2023
Ted Kremenek37e96522012-01-26 02:27:38 +00002024 // Enable the following experimental checkers for testing.
Ted Kremenek37e96522012-01-26 02:27:38 +00002025 CmdArgs.push_back("-analyzer-checker=security.insecureAPI.UncheckedReturn");
2026 CmdArgs.push_back("-analyzer-checker=security.insecureAPI.getpw");
2027 CmdArgs.push_back("-analyzer-checker=security.insecureAPI.gets");
2028 CmdArgs.push_back("-analyzer-checker=security.insecureAPI.mktemp");
2029 CmdArgs.push_back("-analyzer-checker=security.insecureAPI.mkstemp");
2030 CmdArgs.push_back("-analyzer-checker=security.insecureAPI.vfork");
Daniel Dunbar58f345ce2009-05-22 00:38:15 +00002031 }
Daniel Dunbard067f7f2009-04-08 23:54:23 +00002032
Daniel Dunbar58f345ce2009-05-22 00:38:15 +00002033 // Set the output format. The default is plist, for (lame) historical
2034 // reasons.
2035 CmdArgs.push_back("-analyzer-output");
2036 if (Arg *A = Args.getLastArg(options::OPT__analyzer_output))
Richard Smithbd55daf2012-11-01 04:30:05 +00002037 CmdArgs.push_back(A->getValue());
Daniel Dunbar58f345ce2009-05-22 00:38:15 +00002038 else
2039 CmdArgs.push_back("plist");
Daniel Dunbara2aedc62009-03-18 10:01:51 +00002040
Ted Kremenekfe449a22010-03-22 22:32:05 +00002041 // Disable the presentation of standard compiler warnings when
2042 // using --analyze. We only want to show static analyzer diagnostics
2043 // or frontend errors.
2044 CmdArgs.push_back("-w");
2045
Daniel Dunbara2aedc62009-03-18 10:01:51 +00002046 // Add -Xanalyzer arguments when running as analyzer.
2047 Args.AddAllArgValues(CmdArgs, options::OPT_Xanalyzer);
Mike Stump11289f42009-09-09 15:08:12 +00002048 }
2049
Daniel Dunbar4eadb602009-09-10 01:21:12 +00002050 CheckCodeGenerationOptions(D, Args);
2051
Peter Collingbourne54d770c2013-04-09 04:35:11 +00002052 bool PIE = getToolChain().isPIEDefault();
2053 bool PIC = PIE || getToolChain().isPICDefault();
Chandler Carruth76a943b2012-11-19 03:52:03 +00002054 bool IsPICLevelTwo = PIC;
Peter Collingbourne54d770c2013-04-09 04:35:11 +00002055
Alexey Samsonov090301e2013-04-09 12:28:19 +00002056 // For the PIC and PIE flag options, this logic is different from the
2057 // legacy logic in very old versions of GCC, as that logic was just
2058 // a bug no one had ever fixed. This logic is both more rational and
2059 // consistent with GCC's new logic now that the bugs are fixed. The last
2060 // argument relating to either PIC or PIE wins, and no other argument is
2061 // used. If the last argument is any flavor of the '-fno-...' arguments,
2062 // both PIC and PIE are disabled. Any PIE option implicitly enables PIC
2063 // at the same level.
2064 Arg *LastPICArg =Args.getLastArg(options::OPT_fPIC, options::OPT_fno_PIC,
2065 options::OPT_fpic, options::OPT_fno_pic,
2066 options::OPT_fPIE, options::OPT_fno_PIE,
2067 options::OPT_fpie, options::OPT_fno_pie);
Chandler Carruth76a943b2012-11-19 03:52:03 +00002068 // Check whether the tool chain trumps the PIC-ness decision. If the PIC-ness
2069 // is forced, then neither PIC nor PIE flags will have no effect.
Peter Collingbourne54d770c2013-04-09 04:35:11 +00002070 if (!getToolChain().isPICDefaultForced()) {
Alexey Samsonov090301e2013-04-09 12:28:19 +00002071 if (LastPICArg) {
2072 Option O = LastPICArg->getOption();
Peter Collingbourne54d770c2013-04-09 04:35:11 +00002073 if (O.matches(options::OPT_fPIC) || O.matches(options::OPT_fpic) ||
2074 O.matches(options::OPT_fPIE) || O.matches(options::OPT_fpie)) {
2075 PIE = O.matches(options::OPT_fPIE) || O.matches(options::OPT_fpie);
2076 PIC = PIE || O.matches(options::OPT_fPIC) ||
2077 O.matches(options::OPT_fpic);
2078 IsPICLevelTwo = O.matches(options::OPT_fPIE) ||
2079 O.matches(options::OPT_fPIC);
2080 } else {
2081 PIE = PIC = false;
2082 }
2083 }
Chandler Carruthc0c04552012-04-08 16:40:35 +00002084 }
Chandler Carruth76a943b2012-11-19 03:52:03 +00002085
2086 // Inroduce a Darwin-specific hack. If the default is PIC but the flags
2087 // specified while enabling PIC enabled level 1 PIC, just force it back to
2088 // level 2 PIC instead. This matches the behavior of Darwin GCC (based on my
2089 // informal testing).
2090 if (PIC && getToolChain().getTriple().isOSDarwin())
2091 IsPICLevelTwo |= getToolChain().isPICDefault();
2092
Chandler Carruthc0c04552012-04-08 16:40:35 +00002093 // Note that these flags are trump-cards. Regardless of the order w.r.t. the
2094 // PIC or PIE options above, if these show up, PIC is disabled.
Daniel Dunbarbd847cc2012-10-15 22:23:53 +00002095 llvm::Triple Triple(TripleStr);
Eric Christopher8d56caa2013-02-18 01:16:37 +00002096 if (KernelOrKext &&
Cameron Esfahani556d91e2013-09-14 01:09:11 +00002097 (!Triple.isiOS() || Triple.isOSVersionLT(6)))
Chandler Carruth76a943b2012-11-19 03:52:03 +00002098 PIC = PIE = false;
Chandler Carruthc0c04552012-04-08 16:40:35 +00002099 if (Args.hasArg(options::OPT_static))
Chandler Carruth76a943b2012-11-19 03:52:03 +00002100 PIC = PIE = false;
Chandler Carruthc0c04552012-04-08 16:40:35 +00002101
Chandler Carruth76a943b2012-11-19 03:52:03 +00002102 if (Arg *A = Args.getLastArg(options::OPT_mdynamic_no_pic)) {
2103 // This is a very special mode. It trumps the other modes, almost no one
2104 // uses it, and it isn't even valid on any OS but Darwin.
2105 if (!getToolChain().getTriple().isOSDarwin())
2106 D.Diag(diag::err_drv_unsupported_opt_for_target)
2107 << A->getSpelling() << getToolChain().getTriple().str();
2108
2109 // FIXME: Warn when this flag trumps some other PIC or PIE flag.
2110
Daniel Dunbara1b02a22009-11-29 07:18:39 +00002111 CmdArgs.push_back("-mrelocation-model");
Chandler Carruth76a943b2012-11-19 03:52:03 +00002112 CmdArgs.push_back("dynamic-no-pic");
Daniel Dunbar44e71222009-04-29 18:32:25 +00002113
Chandler Carruth76a943b2012-11-19 03:52:03 +00002114 // Only a forced PIC mode can cause the actual compile to have PIC defines
2115 // etc., no flags are sufficient. This behavior was selected to closely
2116 // match that of llvm-gcc and Apple GCC before that.
2117 if (getToolChain().isPICDefault() && getToolChain().isPICDefaultForced()) {
2118 CmdArgs.push_back("-pic-level");
2119 CmdArgs.push_back("2");
2120 }
2121 } else {
2122 // Currently, LLVM only knows about PIC vs. static; the PIE differences are
2123 // handled in Clang's IRGen by the -pie-level flag.
2124 CmdArgs.push_back("-mrelocation-model");
2125 CmdArgs.push_back(PIC ? "pic" : "static");
2126
2127 if (PIC) {
2128 CmdArgs.push_back("-pic-level");
2129 CmdArgs.push_back(IsPICLevelTwo ? "2" : "1");
2130 if (PIE) {
2131 CmdArgs.push_back("-pie-level");
2132 CmdArgs.push_back(IsPICLevelTwo ? "2" : "1");
2133 }
2134 }
Daniel Dunbar44e71222009-04-29 18:32:25 +00002135 }
Chandler Carruthc0c04552012-04-08 16:40:35 +00002136
Tanya Lattnerf9d41df2009-11-04 01:18:09 +00002137 if (!Args.hasFlag(options::OPT_fmerge_all_constants,
2138 options::OPT_fno_merge_all_constants))
Chris Lattner9242b332011-04-08 18:06:54 +00002139 CmdArgs.push_back("-fno-merge-all-constants");
Daniel Dunbar306945d2009-09-16 06:17:29 +00002140
Daniel Dunbara1b02a22009-11-29 07:18:39 +00002141 // LLVM Code Generator Options.
2142
Daniel Dunbar0bb03312011-02-09 17:54:19 +00002143 if (Arg *A = Args.getLastArg(options::OPT_mregparm_EQ)) {
2144 CmdArgs.push_back("-mregparm");
Richard Smithbd55daf2012-11-01 04:30:05 +00002145 CmdArgs.push_back(A->getValue());
Daniel Dunbar0bb03312011-02-09 17:54:19 +00002146 }
2147
Nick Lewyckyd3f3e4f2013-06-25 01:49:44 +00002148 if (Arg *A = Args.getLastArg(options::OPT_fpcc_struct_return,
2149 options::OPT_freg_struct_return)) {
Eli Benderskyd4a6aec2013-07-24 22:20:49 +00002150 if (getToolChain().getArch() != llvm::Triple::x86) {
John McCall1fe2a8c2013-06-18 02:46:29 +00002151 D.Diag(diag::err_drv_unsupported_opt_for_target)
2152 << A->getSpelling() << getToolChain().getTriple().str();
2153 } else if (A->getOption().matches(options::OPT_fpcc_struct_return)) {
2154 CmdArgs.push_back("-fpcc-struct-return");
2155 } else {
2156 assert(A->getOption().matches(options::OPT_freg_struct_return));
2157 CmdArgs.push_back("-freg-struct-return");
2158 }
2159 }
2160
Roman Divacky65b88cd2011-03-01 17:40:53 +00002161 if (Args.hasFlag(options::OPT_mrtd, options::OPT_mno_rtd, false))
2162 CmdArgs.push_back("-mrtd");
2163
Rafael Espindola224dd632011-12-14 21:02:23 +00002164 if (shouldUseFramePointer(Args, getToolChain().getTriple()))
Daniel Dunbara1b02a22009-11-29 07:18:39 +00002165 CmdArgs.push_back("-mdisable-fp-elim");
2166 if (!Args.hasFlag(options::OPT_fzero_initialized_in_bss,
2167 options::OPT_fno_zero_initialized_in_bss))
2168 CmdArgs.push_back("-mno-zero-initialized-in-bss");
Chad Rosierb71f6aa2013-04-24 18:09:54 +00002169
2170 bool OFastEnabled = isOptimizationLevelFast(Args);
2171 // If -Ofast is the optimization level, then -fstrict-aliasing should be
2172 // enabled. This alias option is being used to simplify the hasFlag logic.
2173 OptSpecifier StrictAliasingAliasOption = OFastEnabled ? options::OPT_Ofast :
2174 options::OPT_fstrict_aliasing;
2175 if (!Args.hasFlag(options::OPT_fstrict_aliasing, StrictAliasingAliasOption,
Daniel Dunbar7aa71f92011-02-04 02:20:39 +00002176 options::OPT_fno_strict_aliasing,
2177 getToolChain().IsStrictAliasingDefault()))
Dan Gohman10169b92010-10-14 22:36:56 +00002178 CmdArgs.push_back("-relaxed-aliasing");
Manman Renc451e572013-04-04 21:53:22 +00002179 if (Args.hasArg(options::OPT_fstruct_path_tbaa))
2180 CmdArgs.push_back("-struct-path-tbaa");
Chandler Carruth8b4140d2012-03-27 23:58:37 +00002181 if (Args.hasFlag(options::OPT_fstrict_enums, options::OPT_fno_strict_enums,
2182 false))
2183 CmdArgs.push_back("-fstrict-enums");
Nick Lewycky1c8c4362012-01-23 08:29:12 +00002184 if (!Args.hasFlag(options::OPT_foptimize_sibling_calls,
2185 options::OPT_fno_optimize_sibling_calls))
2186 CmdArgs.push_back("-mdisable-tail-calls");
Daniel Dunbar0d8ca9e2010-05-14 22:00:22 +00002187
Eric Christopher006208c2013-04-04 06:29:47 +00002188 // Handle segmented stacks.
2189 if (Args.hasArg(options::OPT_fsplit_stack))
2190 CmdArgs.push_back("-split-stacks");
Chad Rosierb71f6aa2013-04-24 18:09:54 +00002191
2192 // If -Ofast is the optimization level, then -ffast-math should be enabled.
2193 // This alias option is being used to simplify the getLastArg logic.
2194 OptSpecifier FastMathAliasOption = OFastEnabled ? options::OPT_Ofast :
2195 options::OPT_ffast_math;
Eric Christopher006208c2013-04-04 06:29:47 +00002196
Chandler Carruth306bd2c2012-01-02 14:19:45 +00002197 // Handle various floating point optimization flags, mapping them to the
2198 // appropriate LLVM code generation flags. The pattern for all of these is to
2199 // default off the codegen optimizations, and if any flag enables them and no
2200 // flag disables them after the flag enabling them, enable the codegen
2201 // optimization. This is complicated by several "umbrella" flags.
Chad Rosierb71f6aa2013-04-24 18:09:54 +00002202 if (Arg *A = Args.getLastArg(options::OPT_ffast_math, FastMathAliasOption,
Chad Rosierc30eb1c2012-09-25 22:03:25 +00002203 options::OPT_fno_fast_math,
Chandler Carruth306bd2c2012-01-02 14:19:45 +00002204 options::OPT_ffinite_math_only,
2205 options::OPT_fno_finite_math_only,
2206 options::OPT_fhonor_infinities,
2207 options::OPT_fno_honor_infinities))
Chad Rosierc30eb1c2012-09-25 22:03:25 +00002208 if (A->getOption().getID() != options::OPT_fno_fast_math &&
2209 A->getOption().getID() != options::OPT_fno_finite_math_only &&
Chandler Carruth306bd2c2012-01-02 14:19:45 +00002210 A->getOption().getID() != options::OPT_fhonor_infinities)
2211 CmdArgs.push_back("-menable-no-infs");
Chad Rosierb71f6aa2013-04-24 18:09:54 +00002212 if (Arg *A = Args.getLastArg(options::OPT_ffast_math, FastMathAliasOption,
Chad Rosierc30eb1c2012-09-25 22:03:25 +00002213 options::OPT_fno_fast_math,
Chandler Carruth306bd2c2012-01-02 14:19:45 +00002214 options::OPT_ffinite_math_only,
2215 options::OPT_fno_finite_math_only,
2216 options::OPT_fhonor_nans,
2217 options::OPT_fno_honor_nans))
Chad Rosierc30eb1c2012-09-25 22:03:25 +00002218 if (A->getOption().getID() != options::OPT_fno_fast_math &&
2219 A->getOption().getID() != options::OPT_fno_finite_math_only &&
Chandler Carruth306bd2c2012-01-02 14:19:45 +00002220 A->getOption().getID() != options::OPT_fhonor_nans)
2221 CmdArgs.push_back("-menable-no-nans");
2222
Benjamin Kramerc242ef22012-05-02 14:55:48 +00002223 // -fmath-errno is the default on some platforms, e.g. BSD-derived OSes.
2224 bool MathErrno = getToolChain().IsMathErrnoDefault();
Chad Rosierb71f6aa2013-04-24 18:09:54 +00002225 if (Arg *A = Args.getLastArg(options::OPT_ffast_math, FastMathAliasOption,
Chad Rosierc30eb1c2012-09-25 22:03:25 +00002226 options::OPT_fno_fast_math,
Chandler Carruth306bd2c2012-01-02 14:19:45 +00002227 options::OPT_fmath_errno,
Chandler Carruth0d4b9e62013-05-18 20:47:36 +00002228 options::OPT_fno_math_errno)) {
2229 // Turning on -ffast_math (with either flag) removes the need for MathErrno.
2230 // However, turning *off* -ffast_math merely restores the toolchain default
2231 // (which may be false).
2232 if (A->getOption().getID() == options::OPT_fno_math_errno ||
2233 A->getOption().getID() == options::OPT_ffast_math ||
2234 A->getOption().getID() == options::OPT_Ofast)
2235 MathErrno = false;
2236 else if (A->getOption().getID() == options::OPT_fmath_errno)
2237 MathErrno = true;
2238 }
Chandler Carruth3634c662012-04-26 02:10:51 +00002239 if (MathErrno)
2240 CmdArgs.push_back("-fmath-errno");
Chandler Carruth306bd2c2012-01-02 14:19:45 +00002241
2242 // There are several flags which require disabling very specific
2243 // optimizations. Any of these being disabled forces us to turn off the
2244 // entire set of LLVM optimizations, so collect them through all the flag
2245 // madness.
2246 bool AssociativeMath = false;
Chad Rosierb71f6aa2013-04-24 18:09:54 +00002247 if (Arg *A = Args.getLastArg(options::OPT_ffast_math, FastMathAliasOption,
Chad Rosierc30eb1c2012-09-25 22:03:25 +00002248 options::OPT_fno_fast_math,
Chandler Carruth306bd2c2012-01-02 14:19:45 +00002249 options::OPT_funsafe_math_optimizations,
2250 options::OPT_fno_unsafe_math_optimizations,
2251 options::OPT_fassociative_math,
2252 options::OPT_fno_associative_math))
Chad Rosierc30eb1c2012-09-25 22:03:25 +00002253 if (A->getOption().getID() != options::OPT_fno_fast_math &&
2254 A->getOption().getID() != options::OPT_fno_unsafe_math_optimizations &&
Chandler Carruth306bd2c2012-01-02 14:19:45 +00002255 A->getOption().getID() != options::OPT_fno_associative_math)
2256 AssociativeMath = true;
2257 bool ReciprocalMath = false;
Chad Rosierb71f6aa2013-04-24 18:09:54 +00002258 if (Arg *A = Args.getLastArg(options::OPT_ffast_math, FastMathAliasOption,
Chad Rosierc30eb1c2012-09-25 22:03:25 +00002259 options::OPT_fno_fast_math,
Chandler Carruth306bd2c2012-01-02 14:19:45 +00002260 options::OPT_funsafe_math_optimizations,
2261 options::OPT_fno_unsafe_math_optimizations,
2262 options::OPT_freciprocal_math,
2263 options::OPT_fno_reciprocal_math))
Chad Rosierc30eb1c2012-09-25 22:03:25 +00002264 if (A->getOption().getID() != options::OPT_fno_fast_math &&
2265 A->getOption().getID() != options::OPT_fno_unsafe_math_optimizations &&
Chandler Carruth306bd2c2012-01-02 14:19:45 +00002266 A->getOption().getID() != options::OPT_fno_reciprocal_math)
2267 ReciprocalMath = true;
2268 bool SignedZeros = true;
Chad Rosierb71f6aa2013-04-24 18:09:54 +00002269 if (Arg *A = Args.getLastArg(options::OPT_ffast_math, FastMathAliasOption,
Chad Rosierc30eb1c2012-09-25 22:03:25 +00002270 options::OPT_fno_fast_math,
Chandler Carruth306bd2c2012-01-02 14:19:45 +00002271 options::OPT_funsafe_math_optimizations,
2272 options::OPT_fno_unsafe_math_optimizations,
2273 options::OPT_fsigned_zeros,
2274 options::OPT_fno_signed_zeros))
Chad Rosierc30eb1c2012-09-25 22:03:25 +00002275 if (A->getOption().getID() != options::OPT_fno_fast_math &&
2276 A->getOption().getID() != options::OPT_fno_unsafe_math_optimizations &&
Chandler Carruth306bd2c2012-01-02 14:19:45 +00002277 A->getOption().getID() != options::OPT_fsigned_zeros)
2278 SignedZeros = false;
2279 bool TrappingMath = true;
Chad Rosierb71f6aa2013-04-24 18:09:54 +00002280 if (Arg *A = Args.getLastArg(options::OPT_ffast_math, FastMathAliasOption,
Chad Rosierc30eb1c2012-09-25 22:03:25 +00002281 options::OPT_fno_fast_math,
Chandler Carruth306bd2c2012-01-02 14:19:45 +00002282 options::OPT_funsafe_math_optimizations,
2283 options::OPT_fno_unsafe_math_optimizations,
2284 options::OPT_ftrapping_math,
2285 options::OPT_fno_trapping_math))
Chad Rosierc30eb1c2012-09-25 22:03:25 +00002286 if (A->getOption().getID() != options::OPT_fno_fast_math &&
2287 A->getOption().getID() != options::OPT_fno_unsafe_math_optimizations &&
Chandler Carruth306bd2c2012-01-02 14:19:45 +00002288 A->getOption().getID() != options::OPT_ftrapping_math)
2289 TrappingMath = false;
2290 if (!MathErrno && AssociativeMath && ReciprocalMath && !SignedZeros &&
2291 !TrappingMath)
2292 CmdArgs.push_back("-menable-unsafe-fp-math");
2293
Lang Hamesaa53b932012-07-06 00:59:19 +00002294
2295 // Validate and pass through -fp-contract option.
Chad Rosierb71f6aa2013-04-24 18:09:54 +00002296 if (Arg *A = Args.getLastArg(options::OPT_ffast_math, FastMathAliasOption,
Chad Rosierc30eb1c2012-09-25 22:03:25 +00002297 options::OPT_fno_fast_math,
Lang Hamesaa53b932012-07-06 00:59:19 +00002298 options::OPT_ffp_contract)) {
2299 if (A->getOption().getID() == options::OPT_ffp_contract) {
Richard Smithbd55daf2012-11-01 04:30:05 +00002300 StringRef Val = A->getValue();
Lang Hamesaa53b932012-07-06 00:59:19 +00002301 if (Val == "fast" || Val == "on" || Val == "off") {
2302 CmdArgs.push_back(Args.MakeArgString("-ffp-contract=" + Val));
2303 } else {
2304 D.Diag(diag::err_drv_unsupported_option_argument)
2305 << A->getOption().getName() << Val;
2306 }
Chad Rosierb71f6aa2013-04-24 18:09:54 +00002307 } else if (A->getOption().matches(options::OPT_ffast_math) ||
2308 (OFastEnabled && A->getOption().matches(options::OPT_Ofast))) {
Lang Hamesaa53b932012-07-06 00:59:19 +00002309 // If fast-math is set then set the fp-contract mode to fast.
2310 CmdArgs.push_back(Args.MakeArgString("-ffp-contract=fast"));
2311 }
2312 }
2313
Bob Wilson6a039162012-07-19 03:52:53 +00002314 // We separately look for the '-ffast-math' and '-ffinite-math-only' flags,
2315 // and if we find them, tell the frontend to provide the appropriate
2316 // preprocessor macros. This is distinct from enabling any optimizations as
2317 // these options induce language changes which must survive serialization
2318 // and deserialization, etc.
Chad Rosierb71f6aa2013-04-24 18:09:54 +00002319 if (Arg *A = Args.getLastArg(options::OPT_ffast_math, FastMathAliasOption,
2320 options::OPT_fno_fast_math))
2321 if (!A->getOption().matches(options::OPT_fno_fast_math))
2322 CmdArgs.push_back("-ffast-math");
Chad Rosierc30eb1c2012-09-25 22:03:25 +00002323 if (Arg *A = Args.getLastArg(options::OPT_ffinite_math_only, options::OPT_fno_fast_math))
2324 if (A->getOption().matches(options::OPT_ffinite_math_only))
2325 CmdArgs.push_back("-ffinite-math-only");
Chandler Carruth306bd2c2012-01-02 14:19:45 +00002326
Daniel Dunbar0d8ca9e2010-05-14 22:00:22 +00002327 // Decide whether to use verbose asm. Verbose assembly is the default on
2328 // toolchains which have the integrated assembler on by default.
2329 bool IsVerboseAsmDefault = getToolChain().IsIntegratedAssemblerDefault();
2330 if (Args.hasFlag(options::OPT_fverbose_asm, options::OPT_fno_verbose_asm,
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00002331 IsVerboseAsmDefault) ||
Daniel Dunbar0d8ca9e2010-05-14 22:00:22 +00002332 Args.hasArg(options::OPT_dA))
Daniel Dunbara1b02a22009-11-29 07:18:39 +00002333 CmdArgs.push_back("-masm-verbose");
Daniel Dunbar0d8ca9e2010-05-14 22:00:22 +00002334
Daniel Dunbara1b02a22009-11-29 07:18:39 +00002335 if (Args.hasArg(options::OPT_fdebug_pass_structure)) {
2336 CmdArgs.push_back("-mdebug-pass");
2337 CmdArgs.push_back("Structure");
2338 }
2339 if (Args.hasArg(options::OPT_fdebug_pass_arguments)) {
2340 CmdArgs.push_back("-mdebug-pass");
2341 CmdArgs.push_back("Arguments");
2342 }
2343
John McCall8517abc2010-02-19 02:45:38 +00002344 // Enable -mconstructor-aliases except on darwin, where we have to
2345 // work around a linker bug; see <rdar://problem/7651567>.
Bob Wilson6524dd32011-10-14 05:03:44 +00002346 if (!getToolChain().getTriple().isOSDarwin())
John McCall8517abc2010-02-19 02:45:38 +00002347 CmdArgs.push_back("-mconstructor-aliases");
NAKAMURA Takumi029d74b2011-02-17 08:50:50 +00002348
John McCall7ef5cb32011-03-18 02:56:14 +00002349 // Darwin's kernel doesn't support guard variables; just die if we
2350 // try to use them.
Bob Wilson6524dd32011-10-14 05:03:44 +00002351 if (KernelOrKext && getToolChain().getTriple().isOSDarwin())
John McCall7ef5cb32011-03-18 02:56:14 +00002352 CmdArgs.push_back("-fforbid-guard-variables");
2353
Douglas Gregordbe39272011-02-01 15:15:22 +00002354 if (Args.hasArg(options::OPT_mms_bitfields)) {
2355 CmdArgs.push_back("-mms-bitfields");
2356 }
John McCall8517abc2010-02-19 02:45:38 +00002357
Daniel Dunbar306945d2009-09-16 06:17:29 +00002358 // This is a coarse approximation of what llvm-gcc actually does, both
2359 // -fasynchronous-unwind-tables and -fnon-call-exceptions interact in more
2360 // complicated ways.
2361 bool AsynchronousUnwindTables =
2362 Args.hasFlag(options::OPT_fasynchronous_unwind_tables,
2363 options::OPT_fno_asynchronous_unwind_tables,
2364 getToolChain().IsUnwindTablesDefault() &&
Daniel Dunbare46b52a2010-03-20 04:52:14 +00002365 !KernelOrKext);
Daniel Dunbar306945d2009-09-16 06:17:29 +00002366 if (Args.hasFlag(options::OPT_funwind_tables, options::OPT_fno_unwind_tables,
2367 AsynchronousUnwindTables))
Daniel Dunbara1b02a22009-11-29 07:18:39 +00002368 CmdArgs.push_back("-munwind-tables");
2369
Chandler Carruth05fb5852012-11-21 23:40:23 +00002370 getToolChain().addClangTargetOptions(Args, CmdArgs);
Rafael Espindola66aa0452012-06-19 01:26:10 +00002371
Daniel Dunbara1b02a22009-11-29 07:18:39 +00002372 if (Arg *A = Args.getLastArg(options::OPT_flimited_precision_EQ)) {
2373 CmdArgs.push_back("-mlimit-float-precision");
Richard Smithbd55daf2012-11-01 04:30:05 +00002374 CmdArgs.push_back(A->getValue());
Daniel Dunbara1b02a22009-11-29 07:18:39 +00002375 }
Daniel Dunbar44e71222009-04-29 18:32:25 +00002376
Daniel Dunbar4dbaaa62009-05-06 03:16:41 +00002377 // FIXME: Handle -mtune=.
2378 (void) Args.hasArg(options::OPT_mtune_EQ);
Daniel Dunbar44e71222009-04-29 18:32:25 +00002379
Benjamin Kramercf4371a2009-08-05 14:30:52 +00002380 if (Arg *A = Args.getLastArg(options::OPT_mcmodel_EQ)) {
Daniel Dunbara1b02a22009-11-29 07:18:39 +00002381 CmdArgs.push_back("-mcode-model");
Richard Smithbd55daf2012-11-01 04:30:05 +00002382 CmdArgs.push_back(A->getValue());
Benjamin Kramercf4371a2009-08-05 14:30:52 +00002383 }
2384
Rafael Espindola22ce34a2013-08-20 22:12:08 +00002385 // Add the target cpu
2386 std::string ETripleStr = getToolChain().ComputeEffectiveClangTriple(Args);
2387 llvm::Triple ETriple(ETripleStr);
2388 std::string CPU = getCPUName(Args, ETriple);
2389 if (!CPU.empty()) {
2390 CmdArgs.push_back("-target-cpu");
2391 CmdArgs.push_back(Args.MakeArgString(CPU));
2392 }
2393
Rafael Espindolaeb265472013-08-21 21:59:03 +00002394 if (const Arg *A = Args.getLastArg(options::OPT_mfpmath_EQ)) {
2395 CmdArgs.push_back("-mfpmath");
2396 CmdArgs.push_back(A->getValue());
2397 }
2398
Rafael Espindola28e1f4b2013-08-21 16:39:20 +00002399 // Add the target features
2400 getTargetFeatures(D, ETriple, Args, CmdArgs);
2401
Rafael Espindola22ce34a2013-08-20 22:12:08 +00002402 // Add target specific flags.
Eli Benderskyd4a6aec2013-07-24 22:20:49 +00002403 switch(getToolChain().getArch()) {
Daniel Dunbar3b3191f2009-09-09 22:33:08 +00002404 default:
2405 break;
Daniel Dunbar4dbaaa62009-05-06 03:16:41 +00002406
Daniel Dunbar0f5c5422009-09-10 04:57:17 +00002407 case llvm::Triple::arm:
2408 case llvm::Triple::thumb:
Daniel Dunbarc9388c12011-03-17 17:10:06 +00002409 AddARMTargetArgs(Args, CmdArgs, KernelOrKext);
Daniel Dunbar0f5c5422009-09-10 04:57:17 +00002410 break;
2411
Eric Christopher0b26a612010-03-02 02:41:08 +00002412 case llvm::Triple::mips:
2413 case llvm::Triple::mipsel:
Akira Hatanaka94ab5542011-09-21 02:13:07 +00002414 case llvm::Triple::mips64:
2415 case llvm::Triple::mips64el:
Eric Christopher0b26a612010-03-02 02:41:08 +00002416 AddMIPSTargetArgs(Args, CmdArgs);
2417 break;
2418
Bruno Cardoso Lopese7f211c2010-11-09 17:21:19 +00002419 case llvm::Triple::sparc:
2420 AddSparcTargetArgs(Args, CmdArgs);
2421 break;
2422
Daniel Dunbar3b3191f2009-09-09 22:33:08 +00002423 case llvm::Triple::x86:
2424 case llvm::Triple::x86_64:
2425 AddX86TargetArgs(Args, CmdArgs);
2426 break;
Tony Linthicum76329bf2011-12-12 21:14:55 +00002427
2428 case llvm::Triple::hexagon:
2429 AddHexagonTargetArgs(Args, CmdArgs);
2430 break;
Daniel Dunbar44e71222009-04-29 18:32:25 +00002431 }
2432
Hans Wennborg75958c42013-08-08 00:17:41 +00002433 // Add clang-cl arguments.
2434 if (getToolChain().getDriver().IsCLMode())
2435 AddClangCLArgs(Args, CmdArgs);
2436
Daniel Dunbar976a2f52010-08-11 23:07:47 +00002437 // Pass the linker version in use.
2438 if (Arg *A = Args.getLastArg(options::OPT_mlinker_version_EQ)) {
2439 CmdArgs.push_back("-target-linker-version");
Richard Smithbd55daf2012-11-01 04:30:05 +00002440 CmdArgs.push_back(A->getValue());
Daniel Dunbar976a2f52010-08-11 23:07:47 +00002441 }
2442
Eric Christopherb7d97e92013-04-03 01:58:53 +00002443 if (!shouldUseLeafFramePointer(Args, getToolChain().getTriple()))
Daniel Dunbarbb7ac522010-07-01 01:31:45 +00002444 CmdArgs.push_back("-momit-leaf-frame-pointer");
2445
Daniel Dunbarfcc49a82010-05-12 18:19:58 +00002446 // Explicitly error on some things we know we don't support and can't just
2447 // ignore.
2448 types::ID InputType = Inputs[0].getType();
Daniel Dunbar4ed214a2010-09-24 19:39:37 +00002449 if (!Args.hasArg(options::OPT_fallow_unsupported)) {
2450 Arg *Unsupported;
Daniel Dunbar4ed214a2010-09-24 19:39:37 +00002451 if (types::isCXX(InputType) &&
Bob Wilson6524dd32011-10-14 05:03:44 +00002452 getToolChain().getTriple().isOSDarwin() &&
Eli Benderskyd4a6aec2013-07-24 22:20:49 +00002453 getToolChain().getArch() == llvm::Triple::x86) {
Bob Wilson0d45f582011-08-13 23:48:55 +00002454 if ((Unsupported = Args.getLastArg(options::OPT_fapple_kext)) ||
2455 (Unsupported = Args.getLastArg(options::OPT_mkernel)))
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002456 D.Diag(diag::err_drv_clang_unsupported_opt_cxx_darwin_i386)
Daniel Dunbar4ed214a2010-09-24 19:39:37 +00002457 << Unsupported->getOption().getName();
2458 }
Daniel Dunbarfcc49a82010-05-12 18:19:58 +00002459 }
2460
Daniel Dunbara2aedc62009-03-18 10:01:51 +00002461 Args.AddAllArgs(CmdArgs, options::OPT_v);
Daniel Dunbard4352752010-08-24 22:44:13 +00002462 Args.AddLastArg(CmdArgs, options::OPT_H);
Chad Rosierbe10f982011-08-02 17:58:04 +00002463 if (D.CCPrintHeaders && !D.CCGenDiagnostics) {
Daniel Dunbarac540b32011-02-02 21:11:35 +00002464 CmdArgs.push_back("-header-include-file");
2465 CmdArgs.push_back(D.CCPrintHeadersFilename ?
2466 D.CCPrintHeadersFilename : "-");
2467 }
Daniel Dunbara2aedc62009-03-18 10:01:51 +00002468 Args.AddLastArg(CmdArgs, options::OPT_P);
Mike Stump11289f42009-09-09 15:08:12 +00002469 Args.AddLastArg(CmdArgs, options::OPT_print_ivar_layout);
Daniel Dunbara2aedc62009-03-18 10:01:51 +00002470
Chad Rosierbe10f982011-08-02 17:58:04 +00002471 if (D.CCLogDiagnostics && !D.CCGenDiagnostics) {
Daniel Dunbar529c03b2011-04-07 18:01:20 +00002472 CmdArgs.push_back("-diagnostic-log-file");
2473 CmdArgs.push_back(D.CCLogDiagnosticsFilename ?
2474 D.CCLogDiagnosticsFilename : "-");
2475 }
2476
Eric Christopher2ba5fcb2013-02-05 07:29:57 +00002477 // Use the last option from "-g" group. "-gline-tables-only"
2478 // is preserved, all other debug options are substituted with "-g".
Rafael Espindola08a692a2010-03-07 04:46:18 +00002479 Args.ClaimAllArgs(options::OPT_g_Group);
Alexey Samsonovdda3a7f2012-05-29 08:10:34 +00002480 if (Arg *A = Args.getLastArg(options::OPT_g_Group)) {
Eric Christopher2ba5fcb2013-02-05 07:29:57 +00002481 if (A->getOption().matches(options::OPT_gline_tables_only))
Alexey Samsonovdda3a7f2012-05-29 08:10:34 +00002482 CmdArgs.push_back("-gline-tables-only");
Manman Ren9691f7f2013-06-19 01:46:49 +00002483 else if (A->getOption().matches(options::OPT_gdwarf_2))
2484 CmdArgs.push_back("-gdwarf-2");
2485 else if (A->getOption().matches(options::OPT_gdwarf_3))
2486 CmdArgs.push_back("-gdwarf-3");
2487 else if (A->getOption().matches(options::OPT_gdwarf_4))
2488 CmdArgs.push_back("-gdwarf-4");
Eric Christopher2ba5fcb2013-02-05 07:29:57 +00002489 else if (!A->getOption().matches(options::OPT_g0) &&
Manman Ren38db0922013-07-02 23:15:25 +00002490 !A->getOption().matches(options::OPT_ggdb0)) {
2491 // Default is dwarf-2 for darwin.
2492 if (getToolChain().getTriple().isOSDarwin())
2493 CmdArgs.push_back("-gdwarf-2");
2494 else
2495 CmdArgs.push_back("-g");
2496 }
Alexey Samsonovdda3a7f2012-05-29 08:10:34 +00002497 }
Daniel Dunbara2aedc62009-03-18 10:01:51 +00002498
Alexey Samsonovf50a9ff2012-06-21 08:22:39 +00002499 // We ignore flags -gstrict-dwarf and -grecord-gcc-switches for now.
2500 Args.ClaimAllArgs(options::OPT_g_flags_Group);
Eric Christophera2f7eb72012-10-18 21:52:18 +00002501 if (Args.hasArg(options::OPT_gcolumn_info))
2502 CmdArgs.push_back("-dwarf-column-info");
Alexey Samsonovf50a9ff2012-06-21 08:22:39 +00002503
Eric Christopher138c32b2013-09-13 22:37:55 +00002504 // FIXME: Move backend command line options to the module.
Eric Christopher2ba5fcb2013-02-05 07:29:57 +00002505 // -gsplit-dwarf should turn on -g and enable the backend dwarf
2506 // splitting and extraction.
Eric Christopherd42fb732013-02-21 22:35:05 +00002507 // FIXME: Currently only works on Linux.
Cameron Esfahani556d91e2013-09-14 01:09:11 +00002508 if (getToolChain().getTriple().isOSLinux() &&
Eric Christopherd42fb732013-02-21 22:35:05 +00002509 Args.hasArg(options::OPT_gsplit_dwarf)) {
Eric Christopher2ba5fcb2013-02-05 07:29:57 +00002510 CmdArgs.push_back("-g");
2511 CmdArgs.push_back("-backend-option");
2512 CmdArgs.push_back("-split-dwarf=Enable");
2513 }
2514
Eric Christopher138c32b2013-09-13 22:37:55 +00002515 // -ggnu-pubnames turns on gnu style pubnames in the backend.
2516 if (Args.hasArg(options::OPT_ggnu_pubnames)) {
2517 CmdArgs.push_back("-backend-option");
2518 CmdArgs.push_back("-generate-gnu-dwarf-pub-sections");
2519 }
Eric Christophereec89c22013-06-18 00:03:50 +00002520
2521 Args.AddAllArgs(CmdArgs, options::OPT_fdebug_types_section);
2522
Rafael Espindola66bfb2752010-05-06 21:06:04 +00002523 Args.AddAllArgs(CmdArgs, options::OPT_ffunction_sections);
2524 Args.AddAllArgs(CmdArgs, options::OPT_fdata_sections);
2525
Chris Lattner3c77a352010-06-22 00:03:40 +00002526 Args.AddAllArgs(CmdArgs, options::OPT_finstrument_functions);
2527
Nick Lewycky207bce32011-04-21 23:44:07 +00002528 if (Args.hasArg(options::OPT_ftest_coverage) ||
2529 Args.hasArg(options::OPT_coverage))
2530 CmdArgs.push_back("-femit-coverage-notes");
2531 if (Args.hasArg(options::OPT_fprofile_arcs) ||
2532 Args.hasArg(options::OPT_coverage))
2533 CmdArgs.push_back("-femit-coverage-data");
2534
Nick Lewycky480cb992011-05-04 20:46:58 +00002535 if (C.getArgs().hasArg(options::OPT_c) ||
2536 C.getArgs().hasArg(options::OPT_S)) {
2537 if (Output.isFilename()) {
Nick Lewycky85c011d2011-05-05 00:08:20 +00002538 CmdArgs.push_back("-coverage-file");
Eric Christophere30f61c2013-02-22 00:24:40 +00002539 SmallString<128> CoverageFilename(Output.getFilename());
Nick Lewycky737a4522013-03-07 08:28:53 +00002540 if (llvm::sys::path::is_relative(CoverageFilename.str())) {
Rafael Espindolac7367ff2013-08-10 01:40:10 +00002541 SmallString<128> Pwd;
2542 if (!llvm::sys::fs::current_path(Pwd)) {
2543 llvm::sys::path::append(Pwd, CoverageFilename.str());
2544 CoverageFilename.swap(Pwd);
Nick Lewycky737a4522013-03-07 08:28:53 +00002545 }
2546 }
Eric Christophere30f61c2013-02-22 00:24:40 +00002547 CmdArgs.push_back(Args.MakeArgString(CoverageFilename));
Nick Lewycky480cb992011-05-04 20:46:58 +00002548 }
2549 }
2550
Daniel Dunbarb25bfde2011-10-11 18:20:10 +00002551 // Pass options for controlling the default header search paths.
2552 if (Args.hasArg(options::OPT_nostdinc)) {
2553 CmdArgs.push_back("-nostdsysteminc");
2554 CmdArgs.push_back("-nobuiltininc");
2555 } else {
Daniel Dunbar0f41eee2011-10-11 18:20:16 +00002556 if (Args.hasArg(options::OPT_nostdlibinc))
2557 CmdArgs.push_back("-nostdsysteminc");
Daniel Dunbarb25bfde2011-10-11 18:20:10 +00002558 Args.AddLastArg(CmdArgs, options::OPT_nostdincxx);
2559 Args.AddLastArg(CmdArgs, options::OPT_nobuiltininc);
2560 }
Daniel Dunbara2aedc62009-03-18 10:01:51 +00002561
Daniel Dunbar34e0b8c2009-12-15 01:02:52 +00002562 // Pass the path to compiler resource files.
Daniel Dunbar34e0b8c2009-12-15 01:02:52 +00002563 CmdArgs.push_back("-resource-dir");
Daniel Dunbar3f3e2cd2010-01-20 02:35:16 +00002564 CmdArgs.push_back(D.ResourceDir.c_str());
Daniel Dunbar9dc82a22009-04-07 21:42:00 +00002565
Argyrios Kyrtzidis71731d62010-11-03 22:45:23 +00002566 Args.AddLastArg(CmdArgs, options::OPT_working_directory);
2567
Ted Kremenekf7639e12012-03-06 20:06:33 +00002568 bool ARCMTEnabled = false;
Argyrios Kyrtzidis85230d52013-09-17 19:14:29 +00002569 if (!Args.hasArg(options::OPT_fno_objc_arc, options::OPT_fobjc_arc)) {
Argyrios Kyrtzidisc44b93d2011-07-07 04:00:39 +00002570 if (const Arg *A = Args.getLastArg(options::OPT_ccc_arcmt_check,
Argyrios Kyrtzidis7fbd97f2011-07-09 20:00:58 +00002571 options::OPT_ccc_arcmt_modify,
2572 options::OPT_ccc_arcmt_migrate)) {
Ted Kremenekf7639e12012-03-06 20:06:33 +00002573 ARCMTEnabled = true;
John McCalld70fb982011-06-15 23:25:17 +00002574 switch (A->getOption().getID()) {
2575 default:
2576 llvm_unreachable("missed a case");
Argyrios Kyrtzidisc44b93d2011-07-07 04:00:39 +00002577 case options::OPT_ccc_arcmt_check:
John McCalld70fb982011-06-15 23:25:17 +00002578 CmdArgs.push_back("-arcmt-check");
2579 break;
Argyrios Kyrtzidisc44b93d2011-07-07 04:00:39 +00002580 case options::OPT_ccc_arcmt_modify:
John McCalld70fb982011-06-15 23:25:17 +00002581 CmdArgs.push_back("-arcmt-modify");
2582 break;
Argyrios Kyrtzidis7fbd97f2011-07-09 20:00:58 +00002583 case options::OPT_ccc_arcmt_migrate:
2584 CmdArgs.push_back("-arcmt-migrate");
Ted Kremenekf7639e12012-03-06 20:06:33 +00002585 CmdArgs.push_back("-mt-migrate-directory");
Richard Smithbd55daf2012-11-01 04:30:05 +00002586 CmdArgs.push_back(A->getValue());
Argyrios Kyrtzidisd5713632011-07-19 17:20:03 +00002587
2588 Args.AddLastArg(CmdArgs, options::OPT_arcmt_migrate_report_output);
2589 Args.AddLastArg(CmdArgs, options::OPT_arcmt_migrate_emit_arc_errors);
Argyrios Kyrtzidis7fbd97f2011-07-09 20:00:58 +00002590 break;
John McCalld70fb982011-06-15 23:25:17 +00002591 }
2592 }
Argyrios Kyrtzidisb11a1922013-06-24 19:01:18 +00002593 } else {
2594 Args.ClaimAllArgs(options::OPT_ccc_arcmt_check);
2595 Args.ClaimAllArgs(options::OPT_ccc_arcmt_modify);
2596 Args.ClaimAllArgs(options::OPT_ccc_arcmt_migrate);
John McCalld70fb982011-06-15 23:25:17 +00002597 }
Eric Christopher84fbdb42011-08-19 00:30:14 +00002598
Ted Kremenekf7639e12012-03-06 20:06:33 +00002599 if (const Arg *A = Args.getLastArg(options::OPT_ccc_objcmt_migrate)) {
2600 if (ARCMTEnabled) {
2601 D.Diag(diag::err_drv_argument_not_allowed_with)
2602 << A->getAsString(Args) << "-ccc-arcmt-migrate";
2603 }
2604 CmdArgs.push_back("-mt-migrate-directory");
Richard Smithbd55daf2012-11-01 04:30:05 +00002605 CmdArgs.push_back(A->getValue());
Ted Kremenekf7639e12012-03-06 20:06:33 +00002606
2607 if (!Args.hasArg(options::OPT_objcmt_migrate_literals,
Fariborz Jahaniand83ef842013-07-09 16:59:14 +00002608 options::OPT_objcmt_migrate_subscripting,
2609 options::OPT_objcmt_migrate_property)) {
Ted Kremenekf7639e12012-03-06 20:06:33 +00002610 // None specified, means enable them all.
2611 CmdArgs.push_back("-objcmt-migrate-literals");
2612 CmdArgs.push_back("-objcmt-migrate-subscripting");
Fariborz Jahaniand83ef842013-07-09 16:59:14 +00002613 CmdArgs.push_back("-objcmt-migrate-property");
Ted Kremenekf7639e12012-03-06 20:06:33 +00002614 } else {
2615 Args.AddLastArg(CmdArgs, options::OPT_objcmt_migrate_literals);
2616 Args.AddLastArg(CmdArgs, options::OPT_objcmt_migrate_subscripting);
Fariborz Jahaniand83ef842013-07-09 16:59:14 +00002617 Args.AddLastArg(CmdArgs, options::OPT_objcmt_migrate_property);
Ted Kremenekf7639e12012-03-06 20:06:33 +00002618 }
2619 }
2620
Daniel Dunbard067f7f2009-04-08 23:54:23 +00002621 // Add preprocessing options like -I, -D, etc. if we are using the
2622 // preprocessor.
2623 //
2624 // FIXME: Support -fpreprocessed
Daniel Dunbard067f7f2009-04-08 23:54:23 +00002625 if (types::getPreprocessedType(InputType) != types::TY_INVALID)
Chad Rosier633dcdc2013-01-24 19:14:47 +00002626 AddPreprocessingOptions(C, JA, D, Args, CmdArgs, Output, Inputs);
Daniel Dunbara2aedc62009-03-18 10:01:51 +00002627
Rafael Espindolaa7431922011-07-21 23:40:37 +00002628 // Don't warn about "clang -c -DPIC -fPIC test.i" because libtool.m4 assumes
2629 // that "The compiler can only warn and ignore the option if not recognized".
2630 // When building with ccache, it will pass -D options to clang even on
2631 // preprocessed inputs and configure concludes that -fPIC is not supported.
2632 Args.ClaimAllArgs(options::OPT_D);
2633
Rafael Espindolaad70d962013-08-27 16:58:15 +00002634 // Manually translate -O4 to -O3; let clang reject others.
2635 if (Arg *A = Args.getLastArg(options::OPT_O_Group)) {
2636 if (A->getOption().matches(options::OPT_O4)) {
2637 CmdArgs.push_back("-O3");
2638 D.Diag(diag::warn_O4_is_O3);
2639 } else {
2640 A->render(Args, CmdArgs);
2641 }
2642 }
Daniel Dunbara2aedc62009-03-18 10:01:51 +00002643
Chad Rosier86b82082012-12-12 20:06:31 +00002644 // Don't warn about unused -flto. This can happen when we're preprocessing or
2645 // precompiling.
2646 Args.ClaimAllArgs(options::OPT_flto);
2647
Daniel Dunbar945577c2009-10-29 02:24:45 +00002648 Args.AddAllArgs(CmdArgs, options::OPT_W_Group);
Ted Kremenekb22ea2a2012-07-07 05:53:30 +00002649 if (Args.hasFlag(options::OPT_pedantic, options::OPT_no_pedantic, false))
2650 CmdArgs.push_back("-pedantic");
Daniel Dunbar945577c2009-10-29 02:24:45 +00002651 Args.AddLastArg(CmdArgs, options::OPT_pedantic_errors);
Daniel Dunbara2aedc62009-03-18 10:01:51 +00002652 Args.AddLastArg(CmdArgs, options::OPT_w);
Daniel Dunbarc44b4cc2009-04-07 22:13:21 +00002653
2654 // Handle -{std, ansi, trigraphs} -- take the last of -{std, ansi}
Hans Wennborgec993822013-07-31 16:57:56 +00002655 // (-ansi is equivalent to -std=c89 or -std=c++98).
Daniel Dunbarc44b4cc2009-04-07 22:13:21 +00002656 //
2657 // If a std is supplied, only add -trigraphs if it follows the
2658 // option.
2659 if (Arg *Std = Args.getLastArg(options::OPT_std_EQ, options::OPT_ansi)) {
2660 if (Std->getOption().matches(options::OPT_ansi))
Nuno Lopes275225d2009-10-16 14:28:06 +00002661 if (types::isCXX(InputType))
Daniel Dunbar1c7577c2009-11-04 06:24:38 +00002662 CmdArgs.push_back("-std=c++98");
Nuno Lopes275225d2009-10-16 14:28:06 +00002663 else
Daniel Dunbar1c7577c2009-11-04 06:24:38 +00002664 CmdArgs.push_back("-std=c89");
Daniel Dunbarc44b4cc2009-04-07 22:13:21 +00002665 else
2666 Std->render(Args, CmdArgs);
2667
Daniel Dunbar3f1a1ff2010-06-14 21:23:08 +00002668 if (Arg *A = Args.getLastArg(options::OPT_std_EQ, options::OPT_ansi,
2669 options::OPT_trigraphs))
2670 if (A != Std)
Daniel Dunbarc44b4cc2009-04-07 22:13:21 +00002671 A->render(Args, CmdArgs);
Daniel Dunbar72a60902009-04-26 01:10:38 +00002672 } else {
2673 // Honor -std-default.
Daniel Dunbar12998192010-01-29 21:03:02 +00002674 //
2675 // FIXME: Clang doesn't correctly handle -std= when the input language
2676 // doesn't match. For the time being just ignore this for C++ inputs;
2677 // eventually we want to do all the standard defaulting here instead of
2678 // splitting it between the driver and clang -cc1.
2679 if (!types::isCXX(InputType))
Nico Weber723b4f02012-08-30 02:08:31 +00002680 Args.AddAllArgsTranslated(CmdArgs, options::OPT_std_default_EQ,
2681 "-std=", /*Joined=*/true);
2682 else if (getToolChain().getTriple().getOS() == llvm::Triple::Win32)
2683 CmdArgs.push_back("-std=c++11");
2684
Daniel Dunbarc44b4cc2009-04-07 22:13:21 +00002685 Args.AddLastArg(CmdArgs, options::OPT_trigraphs);
Daniel Dunbar72a60902009-04-26 01:10:38 +00002686 }
Daniel Dunbard067f7f2009-04-08 23:54:23 +00002687
Richard Smith282b4492013-09-04 22:50:31 +00002688 // GCC's behavior for -Wwrite-strings is a bit strange:
2689 // * In C, this "warning flag" changes the types of string literals from
2690 // 'char[N]' to 'const char[N]', and thus triggers an unrelated warning
2691 // for the discarded qualifier.
2692 // * In C++, this is just a normal warning flag.
2693 //
2694 // Implementing this warning correctly in C is hard, so we follow GCC's
2695 // behavior for now. FIXME: Directly diagnose uses of a string literal as
2696 // a non-const char* in C, rather than using this crude hack.
2697 if (!types::isCXX(InputType)) {
2698 // FIXME: This should behave just like a warning flag, and thus should also
2699 // respect -Weverything, -Wno-everything, -Werror=write-strings, and so on.
2700 Arg *WriteStrings =
2701 Args.getLastArg(options::OPT_Wwrite_strings,
2702 options::OPT_Wno_write_strings, options::OPT_w);
2703 if (WriteStrings &&
2704 WriteStrings->getOption().matches(options::OPT_Wwrite_strings))
2705 CmdArgs.push_back("-fconst-strings");
Chandler Carruthb009b142011-04-23 06:30:43 +00002706 }
2707
Chandler Carruth61fbf622011-04-23 09:27:53 +00002708 // GCC provides a macro definition '__DEPRECATED' when -Wdeprecated is active
Chandler Carruth30483fb2011-04-23 19:48:40 +00002709 // during C++ compilation, which it is by default. GCC keeps this define even
2710 // in the presence of '-w', match this behavior bug-for-bug.
2711 if (types::isCXX(InputType) &&
2712 Args.hasFlag(options::OPT_Wdeprecated, options::OPT_Wno_deprecated,
2713 true)) {
2714 CmdArgs.push_back("-fdeprecated-macro");
Chandler Carruth61fbf622011-04-23 09:27:53 +00002715 }
2716
Chandler Carruthe0391482010-05-22 02:21:53 +00002717 // Translate GCC's misnamer '-fasm' arguments to '-fgnu-keywords'.
2718 if (Arg *Asm = Args.getLastArg(options::OPT_fasm, options::OPT_fno_asm)) {
2719 if (Asm->getOption().matches(options::OPT_fasm))
2720 CmdArgs.push_back("-fgnu-keywords");
2721 else
2722 CmdArgs.push_back("-fno-gnu-keywords");
2723 }
2724
Rafael Espindola4cfa7972011-05-02 17:43:32 +00002725 if (ShouldDisableCFI(Args, getToolChain()))
2726 CmdArgs.push_back("-fno-dwarf2-cfi-asm");
Rafael Espindolae2641872011-04-30 18:35:43 +00002727
Nick Lewycky1d617ac2011-10-17 23:05:52 +00002728 if (ShouldDisableDwarfDirectory(Args, getToolChain()))
2729 CmdArgs.push_back("-fno-dwarf-directory-asm");
2730
Daniel Dunbare246fbe2013-04-16 18:21:19 +00002731 if (ShouldDisableAutolink(Args, getToolChain()))
2732 CmdArgs.push_back("-fno-autolink");
2733
Chandler Carruth4d5e1a92012-12-17 21:40:04 +00002734 // Add in -fdebug-compilation-dir if necessary.
2735 addDebugCompDirArg(Args, CmdArgs);
Nick Lewyckyba743b72011-10-21 02:32:14 +00002736
Richard Smith9a568822011-11-21 19:36:32 +00002737 if (Arg *A = Args.getLastArg(options::OPT_ftemplate_depth_,
2738 options::OPT_ftemplate_depth_EQ)) {
Daniel Dunbara2aedc62009-03-18 10:01:51 +00002739 CmdArgs.push_back("-ftemplate-depth");
Richard Smithbd55daf2012-11-01 04:30:05 +00002740 CmdArgs.push_back(A->getValue());
Daniel Dunbara2aedc62009-03-18 10:01:51 +00002741 }
2742
Richard Smith9a568822011-11-21 19:36:32 +00002743 if (Arg *A = Args.getLastArg(options::OPT_fconstexpr_depth_EQ)) {
2744 CmdArgs.push_back("-fconstexpr-depth");
Richard Smithbd55daf2012-11-01 04:30:05 +00002745 CmdArgs.push_back(A->getValue());
Richard Smith9a568822011-11-21 19:36:32 +00002746 }
2747
Richard Smitha3d3bd22013-05-08 02:12:03 +00002748 if (Arg *A = Args.getLastArg(options::OPT_fconstexpr_steps_EQ)) {
2749 CmdArgs.push_back("-fconstexpr-steps");
2750 CmdArgs.push_back(A->getValue());
2751 }
2752
Richard Smithb3a14522013-02-22 01:59:51 +00002753 if (Arg *A = Args.getLastArg(options::OPT_fbracket_depth_EQ)) {
2754 CmdArgs.push_back("-fbracket-depth");
2755 CmdArgs.push_back(A->getValue());
2756 }
2757
Argyrios Kyrtzidisef6c8da2010-11-18 00:20:36 +00002758 if (Arg *A = Args.getLastArg(options::OPT_Wlarge_by_value_copy_EQ,
2759 options::OPT_Wlarge_by_value_copy_def)) {
Jean-Daniel Dupas73d801c2012-05-04 08:08:37 +00002760 if (A->getNumValues()) {
Richard Smithbd55daf2012-11-01 04:30:05 +00002761 StringRef bytes = A->getValue();
Jean-Daniel Dupas73d801c2012-05-04 08:08:37 +00002762 CmdArgs.push_back(Args.MakeArgString("-Wlarge-by-value-copy=" + bytes));
2763 } else
2764 CmdArgs.push_back("-Wlarge-by-value-copy=64"); // default value
Argyrios Kyrtzidisaf84ec02010-11-17 23:11:54 +00002765 }
2766
Nuno Lopes3d6311d2012-05-08 22:10:46 +00002767
Michael J. Spencer929fccd2012-10-22 22:13:48 +00002768 if (Args.hasArg(options::OPT_relocatable_pch))
Daniel Dunbar8bed86c2009-11-20 22:21:36 +00002769 CmdArgs.push_back("-relocatable-pch");
Mike Stump11289f42009-09-09 15:08:12 +00002770
Daniel Dunbar1c7577c2009-11-04 06:24:38 +00002771 if (Arg *A = Args.getLastArg(options::OPT_fconstant_string_class_EQ)) {
2772 CmdArgs.push_back("-fconstant-string-class");
Richard Smithbd55daf2012-11-01 04:30:05 +00002773 CmdArgs.push_back(A->getValue());
Daniel Dunbar1c7577c2009-11-04 06:24:38 +00002774 }
David Chisnall5778fce2009-08-31 16:41:57 +00002775
Chris Lattnere23003d2010-01-09 21:54:33 +00002776 if (Arg *A = Args.getLastArg(options::OPT_ftabstop_EQ)) {
2777 CmdArgs.push_back("-ftabstop");
Richard Smithbd55daf2012-11-01 04:30:05 +00002778 CmdArgs.push_back(A->getValue());
Chris Lattnere23003d2010-01-09 21:54:33 +00002779 }
2780
Chris Lattnerb35583d2010-04-07 20:49:23 +00002781 CmdArgs.push_back("-ferror-limit");
2782 if (Arg *A = Args.getLastArg(options::OPT_ferror_limit_EQ))
Richard Smithbd55daf2012-11-01 04:30:05 +00002783 CmdArgs.push_back(A->getValue());
Chris Lattnerb35583d2010-04-07 20:49:23 +00002784 else
2785 CmdArgs.push_back("19");
Douglas Gregorffed1cb2010-04-20 07:18:24 +00002786
Chandler Carrutha77a7272010-05-06 04:55:18 +00002787 if (Arg *A = Args.getLastArg(options::OPT_fmacro_backtrace_limit_EQ)) {
2788 CmdArgs.push_back("-fmacro-backtrace-limit");
Richard Smithbd55daf2012-11-01 04:30:05 +00002789 CmdArgs.push_back(A->getValue());
Chandler Carrutha77a7272010-05-06 04:55:18 +00002790 }
2791
2792 if (Arg *A = Args.getLastArg(options::OPT_ftemplate_backtrace_limit_EQ)) {
2793 CmdArgs.push_back("-ftemplate-backtrace-limit");
Richard Smithbd55daf2012-11-01 04:30:05 +00002794 CmdArgs.push_back(A->getValue());
Chandler Carrutha77a7272010-05-06 04:55:18 +00002795 }
2796
Richard Smithf6f003a2011-12-16 19:06:07 +00002797 if (Arg *A = Args.getLastArg(options::OPT_fconstexpr_backtrace_limit_EQ)) {
2798 CmdArgs.push_back("-fconstexpr-backtrace-limit");
Richard Smithbd55daf2012-11-01 04:30:05 +00002799 CmdArgs.push_back(A->getValue());
Richard Smithf6f003a2011-12-16 19:06:07 +00002800 }
2801
Daniel Dunbar2c978472009-11-04 06:24:47 +00002802 // Pass -fmessage-length=.
Daniel Dunbar84bb7932009-11-30 08:40:54 +00002803 CmdArgs.push_back("-fmessage-length");
Daniel Dunbar2c978472009-11-04 06:24:47 +00002804 if (Arg *A = Args.getLastArg(options::OPT_fmessage_length_EQ)) {
Richard Smithbd55daf2012-11-01 04:30:05 +00002805 CmdArgs.push_back(A->getValue());
Daniel Dunbar2c978472009-11-04 06:24:47 +00002806 } else {
2807 // If -fmessage-length=N was not specified, determine whether this is a
2808 // terminal and, if so, implicitly define -fmessage-length appropriately.
2809 unsigned N = llvm::sys::Process::StandardErrColumns();
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002810 CmdArgs.push_back(Args.MakeArgString(Twine(N)));
Daniel Dunbar2c978472009-11-04 06:24:47 +00002811 }
2812
John McCallb4a99d32013-02-19 01:57:35 +00002813 // -fvisibility= and -fvisibility-ms-compat are of a piece.
2814 if (const Arg *A = Args.getLastArg(options::OPT_fvisibility_EQ,
2815 options::OPT_fvisibility_ms_compat)) {
2816 if (A->getOption().matches(options::OPT_fvisibility_EQ)) {
2817 CmdArgs.push_back("-fvisibility");
2818 CmdArgs.push_back(A->getValue());
2819 } else {
2820 assert(A->getOption().matches(options::OPT_fvisibility_ms_compat));
2821 CmdArgs.push_back("-fvisibility");
2822 CmdArgs.push_back("hidden");
2823 CmdArgs.push_back("-ftype-visibility");
2824 CmdArgs.push_back("default");
2825 }
Daniel Dunbare357d562009-12-03 18:42:11 +00002826 }
2827
Douglas Gregor08329632010-06-15 17:05:35 +00002828 Args.AddLastArg(CmdArgs, options::OPT_fvisibility_inlines_hidden);
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00002829
Hans Wennborgf60f6af2012-06-28 08:01:44 +00002830 Args.AddLastArg(CmdArgs, options::OPT_ftlsmodel_EQ);
2831
Daniel Dunbare46b52a2010-03-20 04:52:14 +00002832 // -fhosted is default.
Chad Rosier4fab82c2012-03-26 22:04:46 +00002833 if (Args.hasFlag(options::OPT_ffreestanding, options::OPT_fhosted, false) ||
2834 KernelOrKext)
Daniel Dunbare46b52a2010-03-20 04:52:14 +00002835 CmdArgs.push_back("-ffreestanding");
2836
Daniel Dunbare357d562009-12-03 18:42:11 +00002837 // Forward -f (flag) options which we can pass directly.
Daniel Dunbar3a148f22009-04-07 21:51:40 +00002838 Args.AddLastArg(CmdArgs, options::OPT_femit_all_decls);
Daniel Dunbar3a148f22009-04-07 21:51:40 +00002839 Args.AddLastArg(CmdArgs, options::OPT_fheinous_gnu_extensions);
Devang Patel91bbb552010-09-30 19:05:55 +00002840 Args.AddLastArg(CmdArgs, options::OPT_flimit_debug_info);
Devang Patel384dfa42011-11-04 20:05:58 +00002841 Args.AddLastArg(CmdArgs, options::OPT_fno_limit_debug_info);
Eric Christopher86050822011-10-25 07:13:06 +00002842 Args.AddLastArg(CmdArgs, options::OPT_fno_operator_names);
Bill Schmidtb3b804e2013-07-03 15:36:02 +00002843 // AltiVec language extensions aren't relevant for assembling.
2844 if (!isa<PreprocessJobAction>(JA) ||
2845 Output.getType() != types::TY_PP_Asm)
2846 Args.AddLastArg(CmdArgs, options::OPT_faltivec);
Richard Trieu91844232012-06-26 18:18:47 +00002847 Args.AddLastArg(CmdArgs, options::OPT_fdiagnostics_show_template_tree);
2848 Args.AddLastArg(CmdArgs, options::OPT_fno_elide_type);
Chad Rosier864dfe12012-03-13 23:45:51 +00002849
Alexey Samsonov609213f92013-08-19 09:14:21 +00002850 const SanitizerArgs &Sanitize = D.getOrParseSanitizerArgs(Args);
Alexey Samsonove6203662013-08-09 07:42:13 +00002851 Sanitize.addArgs(getToolChain(), Args, CmdArgs);
Richard Smith52be6192012-11-05 22:04:41 +00002852
Will Dietz3676d562012-12-30 20:53:28 +00002853 if (!Args.hasFlag(options::OPT_fsanitize_recover,
2854 options::OPT_fno_sanitize_recover,
2855 true))
2856 CmdArgs.push_back("-fno-sanitize-recover");
2857
Chad Rosierae229d52013-01-29 23:31:22 +00002858 if (Args.hasArg(options::OPT_fcatch_undefined_behavior) ||
2859 Args.hasFlag(options::OPT_fsanitize_undefined_trap_on_error,
2860 options::OPT_fno_sanitize_undefined_trap_on_error, false))
2861 CmdArgs.push_back("-fsanitize-undefined-trap-on-error");
2862
Eric Christopher459d2712013-02-19 06:16:53 +00002863 // Report an error for -faltivec on anything other than PowerPC.
Chad Rosier864dfe12012-03-13 23:45:51 +00002864 if (const Arg *A = Args.getLastArg(options::OPT_faltivec))
Eli Benderskyd4a6aec2013-07-24 22:20:49 +00002865 if (!(getToolChain().getArch() == llvm::Triple::ppc ||
Bill Schmidt778d3872013-07-26 01:36:11 +00002866 getToolChain().getArch() == llvm::Triple::ppc64 ||
2867 getToolChain().getArch() == llvm::Triple::ppc64le))
Chad Rosier864dfe12012-03-13 23:45:51 +00002868 D.Diag(diag::err_drv_argument_only_allowed_with)
Bill Schmidt778d3872013-07-26 01:36:11 +00002869 << A->getAsString(Args) << "ppc/ppc64/ppc64le";
Chad Rosier864dfe12012-03-13 23:45:51 +00002870
Daniel Dunbar733b0f82011-03-01 18:49:30 +00002871 if (getToolChain().SupportsProfiling())
2872 Args.AddLastArg(CmdArgs, options::OPT_pg);
Daniel Dunbar35621a92010-03-16 16:57:46 +00002873
2874 // -flax-vector-conversions is default.
2875 if (!Args.hasFlag(options::OPT_flax_vector_conversions,
2876 options::OPT_fno_lax_vector_conversions))
2877 CmdArgs.push_back("-fno-lax-vector-conversions");
2878
Fariborz Jahaniana4cfff82011-01-07 01:05:02 +00002879 if (Args.getLastArg(options::OPT_fapple_kext))
2880 CmdArgs.push_back("-fapple-kext");
2881
David Blaikie690f21e2012-06-14 18:55:27 +00002882 if (Args.hasFlag(options::OPT_frewrite_includes,
2883 options::OPT_fno_rewrite_includes, false))
2884 CmdArgs.push_back("-frewrite-includes");
2885
Fariborz Jahaniana4404f22009-05-22 20:17:16 +00002886 Args.AddLastArg(CmdArgs, options::OPT_fobjc_sender_dependent_dispatch);
Chris Lattner69686412009-04-21 05:34:31 +00002887 Args.AddLastArg(CmdArgs, options::OPT_fdiagnostics_print_source_range_info);
Douglas Gregoreec975c2010-08-19 20:24:43 +00002888 Args.AddLastArg(CmdArgs, options::OPT_fdiagnostics_parseable_fixits);
Daniel Dunbar3a148f22009-04-07 21:51:40 +00002889 Args.AddLastArg(CmdArgs, options::OPT_ftime_report);
2890 Args.AddLastArg(CmdArgs, options::OPT_ftrapv);
David Chisnalldd84ef12010-09-17 18:29:54 +00002891
2892 if (Arg *A = Args.getLastArg(options::OPT_ftrapv_handler_EQ)) {
2893 CmdArgs.push_back("-ftrapv-handler");
Richard Smithbd55daf2012-11-01 04:30:05 +00002894 CmdArgs.push_back(A->getValue());
David Chisnalldd84ef12010-09-17 18:29:54 +00002895 }
2896
Bob Wilson14adb362012-02-03 06:27:22 +00002897 Args.AddLastArg(CmdArgs, options::OPT_ftrap_function_EQ);
Evan Cheng04c94292011-04-08 21:37:45 +00002898
Chandler Carruth6e501032011-03-27 00:04:55 +00002899 // -fno-strict-overflow implies -fwrapv if it isn't disabled, but
2900 // -fstrict-overflow won't turn off an explicitly enabled -fwrapv.
2901 if (Arg *A = Args.getLastArg(options::OPT_fwrapv,
2902 options::OPT_fno_wrapv)) {
2903 if (A->getOption().matches(options::OPT_fwrapv))
2904 CmdArgs.push_back("-fwrapv");
2905 } else if (Arg *A = Args.getLastArg(options::OPT_fstrict_overflow,
2906 options::OPT_fno_strict_overflow)) {
2907 if (A->getOption().matches(options::OPT_fno_strict_overflow))
2908 CmdArgs.push_back("-fwrapv");
2909 }
Daniel Dunbar3a148f22009-04-07 21:51:40 +00002910 Args.AddLastArg(CmdArgs, options::OPT_fwritable_strings);
Chandler Carruth54c29102013-08-08 08:34:35 +00002911 Args.AddLastArg(CmdArgs, options::OPT_funroll_loops,
2912 options::OPT_fno_unroll_loops);
Daniel Dunbara2aedc62009-03-18 10:01:51 +00002913
Daniel Dunbara77eaeb2009-09-03 04:54:28 +00002914 Args.AddLastArg(CmdArgs, options::OPT_pthread);
2915
Mahesha S6a682be42012-10-27 07:47:56 +00002916
Daniel Dunbar4930e332009-11-17 08:07:36 +00002917 // -stack-protector=0 is default.
2918 unsigned StackProtectorLevel = 0;
Bill Wendlingd63bbad2009-06-28 07:36:13 +00002919 if (Arg *A = Args.getLastArg(options::OPT_fno_stack_protector,
2920 options::OPT_fstack_protector_all,
2921 options::OPT_fstack_protector)) {
Daniel Dunbar4930e332009-11-17 08:07:36 +00002922 if (A->getOption().matches(options::OPT_fstack_protector))
2923 StackProtectorLevel = 1;
2924 else if (A->getOption().matches(options::OPT_fstack_protector_all))
2925 StackProtectorLevel = 2;
Nico Weberdd473632011-08-23 07:38:27 +00002926 } else {
2927 StackProtectorLevel =
2928 getToolChain().GetDefaultStackProtectorLevel(KernelOrKext);
2929 }
Daniel Dunbar4930e332009-11-17 08:07:36 +00002930 if (StackProtectorLevel) {
2931 CmdArgs.push_back("-stack-protector");
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002932 CmdArgs.push_back(Args.MakeArgString(Twine(StackProtectorLevel)));
Joerg Sonnenberger85e2bbc2012-09-12 13:51:14 +00002933 }
Chad Rosierdb3da832012-08-21 16:16:06 +00002934
Joerg Sonnenberger85e2bbc2012-09-12 13:51:14 +00002935 // --param ssp-buffer-size=
2936 for (arg_iterator it = Args.filtered_begin(options::OPT__param),
2937 ie = Args.filtered_end(); it != ie; ++it) {
Richard Smithbd55daf2012-11-01 04:30:05 +00002938 StringRef Str((*it)->getValue());
Joerg Sonnenberger85e2bbc2012-09-12 13:51:14 +00002939 if (Str.startswith("ssp-buffer-size=")) {
2940 if (StackProtectorLevel) {
Chad Rosierdb3da832012-08-21 16:16:06 +00002941 CmdArgs.push_back("-stack-protector-buffer-size");
2942 // FIXME: Verify the argument is a valid integer.
2943 CmdArgs.push_back(Args.MakeArgString(Str.drop_front(16)));
Chad Rosierdb3da832012-08-21 16:16:06 +00002944 }
Joerg Sonnenberger85e2bbc2012-09-12 13:51:14 +00002945 (*it)->claim();
Chad Rosierdb3da832012-08-21 16:16:06 +00002946 }
Bill Wendlingd63bbad2009-06-28 07:36:13 +00002947 }
2948
Nick Lewyckyf4d3f7a2011-12-06 03:33:03 +00002949 // Translate -mstackrealign
2950 if (Args.hasFlag(options::OPT_mstackrealign, options::OPT_mno_stackrealign,
2951 false)) {
2952 CmdArgs.push_back("-backend-option");
2953 CmdArgs.push_back("-force-align-stack");
2954 }
2955 if (!Args.hasFlag(options::OPT_mno_stackrealign, options::OPT_mstackrealign,
2956 false)) {
2957 CmdArgs.push_back(Args.MakeArgString("-mstackrealign"));
2958 }
2959
Joerg Sonnenbergerdb66ed02011-12-05 23:05:23 +00002960 if (Args.hasArg(options::OPT_mstack_alignment)) {
2961 StringRef alignment = Args.getLastArgValue(options::OPT_mstack_alignment);
2962 CmdArgs.push_back(Args.MakeArgString("-mstack-alignment=" + alignment));
Eric Christopherd5c45f62011-05-02 21:18:22 +00002963 }
Chad Rosier9ae53f82012-11-29 00:42:06 +00002964 // -mkernel implies -mstrict-align; don't add the redundant option.
Renato Golina146a482013-08-24 14:44:41 +00002965 if (!KernelOrKext) {
Renato Golin8d5f3142013-08-28 23:56:07 +00002966 if (Arg *A = Args.getLastArg(options::OPT_mno_unaligned_access,
2967 options::OPT_munaligned_access)) {
2968 if (A->getOption().matches(options::OPT_mno_unaligned_access)) {
2969 CmdArgs.push_back("-backend-option");
2970 CmdArgs.push_back("-arm-strict-align");
2971 } else {
2972 CmdArgs.push_back("-backend-option");
2973 CmdArgs.push_back("-arm-no-strict-align");
2974 }
Renato Golina146a482013-08-24 14:44:41 +00002975 }
Chad Rosier60027022012-11-09 17:29:19 +00002976 }
Eric Christopher84fbdb42011-08-19 00:30:14 +00002977
Daniel Dunbard18049a2009-04-07 21:16:11 +00002978 // Forward -f options with positive and negative forms; we translate
2979 // these by hand.
2980
Fariborz Jahaniana4cfff82011-01-07 01:05:02 +00002981 if (Args.hasArg(options::OPT_mkernel)) {
Daniel Dunbar80f787c2011-02-04 17:24:47 +00002982 if (!Args.hasArg(options::OPT_fapple_kext) && types::isCXX(InputType))
Fariborz Jahaniana4cfff82011-01-07 01:05:02 +00002983 CmdArgs.push_back("-fapple-kext");
2984 if (!Args.hasArg(options::OPT_fbuiltin))
2985 CmdArgs.push_back("-fno-builtin");
Chad Rosierf27e1c42012-03-26 21:29:17 +00002986 Args.ClaimAllArgs(options::OPT_fno_builtin);
Fariborz Jahaniana4cfff82011-01-07 01:05:02 +00002987 }
Daniel Dunbar4930e332009-11-17 08:07:36 +00002988 // -fbuiltin is default.
Fariborz Jahaniana4cfff82011-01-07 01:05:02 +00002989 else if (!Args.hasFlag(options::OPT_fbuiltin, options::OPT_fno_builtin))
Daniel Dunbar484afa22009-11-19 04:55:23 +00002990 CmdArgs.push_back("-fno-builtin");
Daniel Dunbard18049a2009-04-07 21:16:11 +00002991
Nuno Lopes13c88c72009-12-16 16:59:22 +00002992 if (!Args.hasFlag(options::OPT_fassume_sane_operator_new,
2993 options::OPT_fno_assume_sane_operator_new))
2994 CmdArgs.push_back("-fno-assume-sane-operator-new");
2995
Daniel Dunbar4930e332009-11-17 08:07:36 +00002996 // -fblocks=0 is default.
2997 if (Args.hasFlag(options::OPT_fblocks, options::OPT_fno_blocks,
David Chisnallda209912011-02-28 17:11:43 +00002998 getToolChain().IsBlocksDefault()) ||
2999 (Args.hasArg(options::OPT_fgnu_runtime) &&
3000 Args.hasArg(options::OPT_fobjc_nonfragile_abi) &&
3001 !Args.hasArg(options::OPT_fno_blocks))) {
Daniel Dunbar4930e332009-11-17 08:07:36 +00003002 CmdArgs.push_back("-fblocks");
John McCall7959fee2011-09-09 20:41:01 +00003003
3004 if (!Args.hasArg(options::OPT_fgnu_runtime) &&
3005 !getToolChain().hasBlocksRuntime())
3006 CmdArgs.push_back("-fblocks-runtime-optional");
David Chisnall950a9512009-11-17 19:33:30 +00003007 }
Daniel Dunbard18049a2009-04-07 21:16:11 +00003008
Douglas Gregor226173a2012-01-18 15:19:58 +00003009 // -fmodules enables modules (off by default). However, for C++/Objective-C++,
3010 // users must also pass -fcxx-modules. The latter flag will disappear once the
3011 // modules implementation is solid for C++/Objective-C++ programs as well.
Douglas Gregorc60437f2013-01-16 01:23:41 +00003012 bool HaveModules = false;
Douglas Gregor226173a2012-01-18 15:19:58 +00003013 if (Args.hasFlag(options::OPT_fmodules, options::OPT_fno_modules, false)) {
3014 bool AllowedInCXX = Args.hasFlag(options::OPT_fcxx_modules,
3015 options::OPT_fno_cxx_modules,
3016 false);
Douglas Gregorc60437f2013-01-16 01:23:41 +00003017 if (AllowedInCXX || !types::isCXX(InputType)) {
Douglas Gregor226173a2012-01-18 15:19:58 +00003018 CmdArgs.push_back("-fmodules");
Douglas Gregorc60437f2013-01-16 01:23:41 +00003019 HaveModules = true;
3020 }
3021 }
3022
Daniel Jasper07e6c402013-08-05 20:26:17 +00003023 // -fmodule-maps enables module map processing (off by default) for header
3024 // checking. It is implied by -fmodules.
3025 if (Args.hasFlag(options::OPT_fmodule_maps, options::OPT_fno_module_maps,
3026 false)) {
3027 CmdArgs.push_back("-fmodule-maps");
3028 }
3029
Douglas Gregor35b04d62013-02-07 19:01:24 +00003030 // If a module path was provided, pass it along. Otherwise, use a temporary
3031 // directory.
3032 if (Arg *A = Args.getLastArg(options::OPT_fmodules_cache_path)) {
3033 A->claim();
3034 if (HaveModules) {
3035 A->render(Args, CmdArgs);
3036 }
3037 } else if (HaveModules) {
3038 SmallString<128> DefaultModuleCache;
3039 llvm::sys::path::system_temp_directory(/*erasedOnReboot=*/false,
3040 DefaultModuleCache);
Douglas Gregor23c7d672013-03-21 21:48:48 +00003041 llvm::sys::path::append(DefaultModuleCache, "org.llvm.clang");
3042 llvm::sys::path::append(DefaultModuleCache, "ModuleCache");
Douglas Gregor4bedb492013-02-07 22:59:12 +00003043 const char Arg[] = "-fmodules-cache-path=";
3044 DefaultModuleCache.insert(DefaultModuleCache.begin(),
3045 Arg, Arg + strlen(Arg));
Douglas Gregor35b04d62013-02-07 19:01:24 +00003046 CmdArgs.push_back(Args.MakeArgString(DefaultModuleCache));
3047 }
3048
3049 // Pass through all -fmodules-ignore-macro arguments.
3050 Args.AddAllArgs(CmdArgs, options::OPT_fmodules_ignore_macro);
Douglas Gregor527b1c92013-03-25 21:19:16 +00003051 Args.AddLastArg(CmdArgs, options::OPT_fmodules_prune_interval);
3052 Args.AddLastArg(CmdArgs, options::OPT_fmodules_prune_after);
Douglas Gregor35b04d62013-02-07 19:01:24 +00003053
John McCalldfea9982010-04-09 19:12:06 +00003054 // -faccess-control is default.
John McCall3155f572010-04-09 19:03:51 +00003055 if (Args.hasFlag(options::OPT_fno_access_control,
3056 options::OPT_faccess_control,
John McCalldfea9982010-04-09 19:12:06 +00003057 false))
John McCall3155f572010-04-09 19:03:51 +00003058 CmdArgs.push_back("-fno-access-control");
John McCall59bb1d42010-03-17 01:32:13 +00003059
Anders Carlssond470fef2010-11-21 00:09:52 +00003060 // -felide-constructors is the default.
3061 if (Args.hasFlag(options::OPT_fno_elide_constructors,
3062 options::OPT_felide_constructors,
3063 false))
3064 CmdArgs.push_back("-fno-elide-constructors");
3065
Daniel Dunbar0730e4f2009-11-17 07:06:20 +00003066 // -frtti is default.
Chad Rosier4fab82c2012-03-26 22:04:46 +00003067 if (!Args.hasFlag(options::OPT_frtti, options::OPT_fno_rtti) ||
Richard Smith52be6192012-11-05 22:04:41 +00003068 KernelOrKext) {
Daniel Dunbar484afa22009-11-19 04:55:23 +00003069 CmdArgs.push_back("-fno-rtti");
Mike Stump183c3d22009-07-31 23:15:31 +00003070
Richard Smith52be6192012-11-05 22:04:41 +00003071 // -fno-rtti cannot usefully be combined with -fsanitize=vptr.
Alexey Samsonov627b10f2012-11-06 15:09:03 +00003072 if (Sanitize.sanitizesVptr()) {
NAKAMURA Takumiab060362012-11-06 22:02:00 +00003073 std::string NoRttiArg =
Richard Smith52be6192012-11-05 22:04:41 +00003074 Args.getLastArg(options::OPT_mkernel,
3075 options::OPT_fapple_kext,
Richard Smithaa716572012-11-06 01:12:02 +00003076 options::OPT_fno_rtti)->getAsString(Args);
Richard Smith52be6192012-11-05 22:04:41 +00003077 D.Diag(diag::err_drv_argument_not_allowed_with)
3078 << "-fsanitize=vptr" << NoRttiArg;
3079 }
3080 }
3081
Tony Linthicum76329bf2011-12-12 21:14:55 +00003082 // -fshort-enums=0 is default for all architectures except Hexagon.
Argyrios Kyrtzidis74825bc2010-10-08 00:25:19 +00003083 if (Args.hasFlag(options::OPT_fshort_enums,
Tony Linthicum76329bf2011-12-12 21:14:55 +00003084 options::OPT_fno_short_enums,
Eli Benderskyd4a6aec2013-07-24 22:20:49 +00003085 getToolChain().getArch() ==
Tony Linthicum76329bf2011-12-12 21:14:55 +00003086 llvm::Triple::hexagon))
Argyrios Kyrtzidis74825bc2010-10-08 00:25:19 +00003087 CmdArgs.push_back("-fshort-enums");
3088
Daniel Dunbard609b7b2009-11-17 06:37:03 +00003089 // -fsigned-char is default.
Daniel Dunbar5bdd2992009-11-25 10:14:30 +00003090 if (!Args.hasFlag(options::OPT_fsigned_char, options::OPT_funsigned_char,
Daniel Dunbard609b7b2009-11-17 06:37:03 +00003091 isSignedCharDefault(getToolChain().getTriple())))
Daniel Dunbar5fe08662009-11-29 02:39:08 +00003092 CmdArgs.push_back("-fno-signed-char");
Eli Friedman327f0b52009-06-05 07:21:14 +00003093
Anders Carlssonfcd764a2010-02-06 23:23:06 +00003094 // -fthreadsafe-static is default.
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00003095 if (!Args.hasFlag(options::OPT_fthreadsafe_statics,
Anders Carlssonfcd764a2010-02-06 23:23:06 +00003096 options::OPT_fno_threadsafe_statics))
3097 CmdArgs.push_back("-fno-threadsafe-statics");
3098
Daniel Dunbarfe06df42010-03-20 04:15:41 +00003099 // -fuse-cxa-atexit is default.
Chad Rosier4fab82c2012-03-26 22:04:46 +00003100 if (!Args.hasFlag(options::OPT_fuse_cxa_atexit,
3101 options::OPT_fno_use_cxa_atexit,
3102 getToolChain().getTriple().getOS() != llvm::Triple::Cygwin &&
Tony Linthicum76329bf2011-12-12 21:14:55 +00003103 getToolChain().getTriple().getOS() != llvm::Triple::MinGW32 &&
Eli Benderskyd4a6aec2013-07-24 22:20:49 +00003104 getToolChain().getArch() != llvm::Triple::hexagon) ||
Chad Rosier4fab82c2012-03-26 22:04:46 +00003105 KernelOrKext)
Daniel Dunbarfe06df42010-03-20 04:15:41 +00003106 CmdArgs.push_back("-fno-use-cxa-atexit");
3107
Daniel Dunbar0730e4f2009-11-17 07:06:20 +00003108 // -fms-extensions=0 is default.
Daniel Dunbar5bdd2992009-11-25 10:14:30 +00003109 if (Args.hasFlag(options::OPT_fms_extensions, options::OPT_fno_ms_extensions,
Daniel Dunbar0730e4f2009-11-17 07:06:20 +00003110 getToolChain().getTriple().getOS() == llvm::Triple::Win32))
3111 CmdArgs.push_back("-fms-extensions");
3112
Francois Pichet1b4f1632011-09-17 04:32:15 +00003113 // -fms-compatibility=0 is default.
Douglas Gregor2b4907e2011-10-24 15:49:38 +00003114 if (Args.hasFlag(options::OPT_fms_compatibility,
3115 options::OPT_fno_ms_compatibility,
3116 (getToolChain().getTriple().getOS() == llvm::Triple::Win32 &&
3117 Args.hasFlag(options::OPT_fms_extensions,
3118 options::OPT_fno_ms_extensions,
3119 true))))
Francois Pichet1b4f1632011-09-17 04:32:15 +00003120 CmdArgs.push_back("-fms-compatibility");
3121
Reid Klecknerc106fda2013-09-18 00:33:59 +00003122 // -fmsc-version=1700 is default.
Michael J. Spencer4992ca4b2010-10-21 05:21:48 +00003123 if (Args.hasFlag(options::OPT_fms_extensions, options::OPT_fno_ms_extensions,
3124 getToolChain().getTriple().getOS() == llvm::Triple::Win32) ||
3125 Args.hasArg(options::OPT_fmsc_version)) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003126 StringRef msc_ver = Args.getLastArgValue(options::OPT_fmsc_version);
Michael J. Spencer4992ca4b2010-10-21 05:21:48 +00003127 if (msc_ver.empty())
Reid Klecknerc106fda2013-09-18 00:33:59 +00003128 CmdArgs.push_back("-fmsc-version=1700");
Michael J. Spencer4992ca4b2010-10-21 05:21:48 +00003129 else
3130 CmdArgs.push_back(Args.MakeArgString("-fmsc-version=" + msc_ver));
3131 }
3132
3133
Eric Christopher5ecce122013-02-18 00:38:31 +00003134 // -fno-borland-extensions is default.
Dawn Perchik68bb1b42010-09-02 23:59:25 +00003135 if (Args.hasFlag(options::OPT_fborland_extensions,
3136 options::OPT_fno_borland_extensions, false))
3137 CmdArgs.push_back("-fborland-extensions");
3138
Francois Pichet02744872011-09-01 16:38:08 +00003139 // -fno-delayed-template-parsing is default, except for Windows where MSVC STL
3140 // needs it.
Francois Pichet1c229c02011-04-22 22:18:13 +00003141 if (Args.hasFlag(options::OPT_fdelayed_template_parsing,
3142 options::OPT_fno_delayed_template_parsing,
Francois Pichet02744872011-09-01 16:38:08 +00003143 getToolChain().getTriple().getOS() == llvm::Triple::Win32))
Francois Pichet35bc5de2011-08-26 00:22:34 +00003144 CmdArgs.push_back("-fdelayed-template-parsing");
Francois Pichet1c229c02011-04-22 22:18:13 +00003145
Chandler Carruthe03aa552010-04-17 20:17:31 +00003146 // -fgnu-keywords default varies depending on language; only pass if
3147 // specified.
3148 if (Arg *A = Args.getLastArg(options::OPT_fgnu_keywords,
Daniel Dunbardb059592010-04-24 17:56:39 +00003149 options::OPT_fno_gnu_keywords))
3150 A->render(Args, CmdArgs);
Chandler Carruthe03aa552010-04-17 20:17:31 +00003151
Rafael Espindola922a6242011-06-02 17:30:53 +00003152 if (Args.hasFlag(options::OPT_fgnu89_inline,
3153 options::OPT_fno_gnu89_inline,
3154 false))
Rafael Espindolafb2af642011-06-02 16:13:27 +00003155 CmdArgs.push_back("-fgnu89-inline");
3156
Chad Rosier9c76d242012-03-15 22:31:42 +00003157 if (Args.hasArg(options::OPT_fno_inline))
3158 CmdArgs.push_back("-fno-inline");
3159
Chad Rosier64d6be92012-03-06 21:17:19 +00003160 if (Args.hasArg(options::OPT_fno_inline_functions))
3161 CmdArgs.push_back("-fno-inline-functions");
Chad Rosier80603182012-03-06 18:49:20 +00003162
John McCall5fb5df92012-06-20 06:18:46 +00003163 ObjCRuntime objcRuntime = AddObjCRuntimeArgs(Args, CmdArgs, rewriteKind);
John McCall24fc0de2011-07-06 00:26:06 +00003164
John McCall5fb5df92012-06-20 06:18:46 +00003165 // -fobjc-dispatch-method is only relevant with the nonfragile-abi, and
3166 // legacy is the default.
3167 if (objcRuntime.isNonFragile()) {
David Chisnall3154e682011-09-30 13:32:35 +00003168 if (!Args.hasFlag(options::OPT_fobjc_legacy_dispatch,
3169 options::OPT_fno_objc_legacy_dispatch,
David Chisnallda225352012-07-04 11:52:24 +00003170 objcRuntime.isLegacyDispatchDefaultForArch(
Eli Benderskyd4a6aec2013-07-24 22:20:49 +00003171 getToolChain().getArch()))) {
David Chisnall3154e682011-09-30 13:32:35 +00003172 if (getToolChain().UseObjCMixedDispatch())
3173 CmdArgs.push_back("-fobjc-dispatch-method=mixed");
3174 else
3175 CmdArgs.push_back("-fobjc-dispatch-method=non-legacy");
3176 }
3177 }
3178
Nico Weber97bd94b2012-03-09 21:19:44 +00003179 // -fobjc-default-synthesize-properties=1 is default. This only has an effect
3180 // if the nonfragile objc abi is used.
Fariborz Jahanian05684072012-04-09 18:58:55 +00003181 if (getToolChain().IsObjCDefaultSynthPropertiesDefault()) {
David Chisnall3154e682011-09-30 13:32:35 +00003182 CmdArgs.push_back("-fobjc-default-synthesize-properties");
3183 }
3184
Fariborz Jahanian0e3043b2012-11-15 19:02:45 +00003185 // -fencode-extended-block-signature=1 is default.
3186 if (getToolChain().IsEncodeExtendedBlockSignatureDefault()) {
3187 CmdArgs.push_back("-fencode-extended-block-signature");
3188 }
3189
John McCall24fc0de2011-07-06 00:26:06 +00003190 // Allow -fno-objc-arr to trump -fobjc-arr/-fobjc-arc.
3191 // NOTE: This logic is duplicated in ToolChains.cpp.
3192 bool ARC = isObjCAutoRefCount(Args);
3193 if (ARC) {
John McCall3deb1ad2012-08-21 02:47:43 +00003194 getToolChain().CheckObjCARC();
Argyrios Kyrtzidis3dbeb552012-02-29 03:43:52 +00003195
John McCall24fc0de2011-07-06 00:26:06 +00003196 CmdArgs.push_back("-fobjc-arc");
3197
Chandler Carruth491db322011-11-04 07:34:47 +00003198 // FIXME: It seems like this entire block, and several around it should be
3199 // wrapped in isObjC, but for now we just use it here as this is where it
3200 // was being used previously.
3201 if (types::isCXX(InputType) && types::isObjC(InputType)) {
3202 if (getToolChain().GetCXXStdlibType(Args) == ToolChain::CST_Libcxx)
3203 CmdArgs.push_back("-fobjc-arc-cxxlib=libc++");
3204 else
3205 CmdArgs.push_back("-fobjc-arc-cxxlib=libstdc++");
3206 }
3207
John McCall24fc0de2011-07-06 00:26:06 +00003208 // Allow the user to enable full exceptions code emission.
3209 // We define off for Objective-CC, on for Objective-C++.
3210 if (Args.hasFlag(options::OPT_fobjc_arc_exceptions,
3211 options::OPT_fno_objc_arc_exceptions,
3212 /*default*/ types::isCXX(InputType)))
3213 CmdArgs.push_back("-fobjc-arc-exceptions");
3214 }
3215
3216 // -fobjc-infer-related-result-type is the default, except in the Objective-C
3217 // rewriter.
John McCall5fb5df92012-06-20 06:18:46 +00003218 if (rewriteKind != RK_None)
John McCall24fc0de2011-07-06 00:26:06 +00003219 CmdArgs.push_back("-fno-objc-infer-related-result-type");
Eric Christopher84fbdb42011-08-19 00:30:14 +00003220
John McCall24fc0de2011-07-06 00:26:06 +00003221 // Handle -fobjc-gc and -fobjc-gc-only. They are exclusive, and -fobjc-gc-only
3222 // takes precedence.
3223 const Arg *GCArg = Args.getLastArg(options::OPT_fobjc_gc_only);
3224 if (!GCArg)
3225 GCArg = Args.getLastArg(options::OPT_fobjc_gc);
3226 if (GCArg) {
3227 if (ARC) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003228 D.Diag(diag::err_drv_objc_gc_arr)
John McCall24fc0de2011-07-06 00:26:06 +00003229 << GCArg->getAsString(Args);
3230 } else if (getToolChain().SupportsObjCGC()) {
3231 GCArg->render(Args, CmdArgs);
3232 } else {
3233 // FIXME: We should move this to a hard error.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003234 D.Diag(diag::warn_drv_objc_gc_unsupported)
John McCall24fc0de2011-07-06 00:26:06 +00003235 << GCArg->getAsString(Args);
3236 }
3237 }
3238
John McCallb5f652e2011-06-22 00:53:57 +00003239 // Add exception args.
3240 addExceptionArgs(Args, InputType, getToolChain().getTriple(),
John McCall5fb5df92012-06-20 06:18:46 +00003241 KernelOrKext, objcRuntime, CmdArgs);
John McCallb5f652e2011-06-22 00:53:57 +00003242
3243 if (getToolChain().UseSjLjExceptions())
3244 CmdArgs.push_back("-fsjlj-exceptions");
3245
3246 // C++ "sane" operator new.
Daniel Dunbar2e3f2c82010-02-01 21:07:25 +00003247 if (!Args.hasFlag(options::OPT_fassume_sane_operator_new,
3248 options::OPT_fno_assume_sane_operator_new))
3249 CmdArgs.push_back("-fno-assume-sane-operator-new");
3250
Daniel Dunbar34d7a992010-04-27 15:34:57 +00003251 // -fconstant-cfstrings is default, and may be subject to argument translation
3252 // on Darwin.
3253 if (!Args.hasFlag(options::OPT_fconstant_cfstrings,
3254 options::OPT_fno_constant_cfstrings) ||
3255 !Args.hasFlag(options::OPT_mconstant_cfstrings,
3256 options::OPT_mno_constant_cfstrings))
3257 CmdArgs.push_back("-fno-constant-cfstrings");
3258
John Thompsoned4e2952009-11-05 20:14:16 +00003259 // -fshort-wchar default varies depending on platform; only
3260 // pass if specified.
Daniel Dunbar2cb4e7a2010-04-27 15:35:03 +00003261 if (Arg *A = Args.getLastArg(options::OPT_fshort_wchar))
3262 A->render(Args, CmdArgs);
John Thompsoned4e2952009-11-05 20:14:16 +00003263
Hans Wennborg28c96312013-07-31 23:39:13 +00003264 // -fno-pascal-strings is default, only pass non-default.
Daniel Dunbard067f7f2009-04-08 23:54:23 +00003265 if (Args.hasFlag(options::OPT_fpascal_strings,
Daniel Dunbard4510f22009-04-07 23:51:44 +00003266 options::OPT_fno_pascal_strings,
Daniel Dunbard4510f22009-04-07 23:51:44 +00003267 false))
Daniel Dunbard18049a2009-04-07 21:16:11 +00003268 CmdArgs.push_back("-fpascal-strings");
NAKAMURA Takumi029d74b2011-02-17 08:50:50 +00003269
Daniel Dunbar096ed292011-10-05 21:04:55 +00003270 // Honor -fpack-struct= and -fpack-struct, if given. Note that
3271 // -fno-pack-struct doesn't apply to -fpack-struct=.
3272 if (Arg *A = Args.getLastArg(options::OPT_fpack_struct_EQ)) {
James Molloycebf75e2012-05-02 07:56:14 +00003273 std::string PackStructStr = "-fpack-struct=";
Richard Smithbd55daf2012-11-01 04:30:05 +00003274 PackStructStr += A->getValue();
James Molloycebf75e2012-05-02 07:56:14 +00003275 CmdArgs.push_back(Args.MakeArgString(PackStructStr));
Daniel Dunbar096ed292011-10-05 21:04:55 +00003276 } else if (Args.hasFlag(options::OPT_fpack_struct,
3277 options::OPT_fno_pack_struct, false)) {
James Molloycebf75e2012-05-02 07:56:14 +00003278 CmdArgs.push_back("-fpack-struct=1");
Daniel Dunbar096ed292011-10-05 21:04:55 +00003279 }
3280
Robert Lytton0e076492013-08-13 09:43:10 +00003281 if (KernelOrKext || isNoCommonDefault(getToolChain().getTriple())) {
Fariborz Jahaniana4cfff82011-01-07 01:05:02 +00003282 if (!Args.hasArg(options::OPT_fcommon))
3283 CmdArgs.push_back("-fno-common");
Chad Rosierd9d80e02012-03-26 21:35:40 +00003284 Args.ClaimAllArgs(options::OPT_fno_common);
Fariborz Jahaniana4cfff82011-01-07 01:05:02 +00003285 }
Daniel Dunbar096ed292011-10-05 21:04:55 +00003286
Daniel Dunbard18049a2009-04-07 21:16:11 +00003287 // -fcommon is default, only pass non-default.
Fariborz Jahaniana4cfff82011-01-07 01:05:02 +00003288 else if (!Args.hasFlag(options::OPT_fcommon, options::OPT_fno_common))
Daniel Dunbard18049a2009-04-07 21:16:11 +00003289 CmdArgs.push_back("-fno-common");
3290
Daniel Dunbar2edd9232009-04-15 02:37:43 +00003291 // -fsigned-bitfields is default, and clang doesn't yet support
Daniel Dunbar6358d682010-10-15 22:30:42 +00003292 // -funsigned-bitfields.
Mike Stump11289f42009-09-09 15:08:12 +00003293 if (!Args.hasFlag(options::OPT_fsigned_bitfields,
Daniel Dunbar2edd9232009-04-15 02:37:43 +00003294 options::OPT_funsigned_bitfields))
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003295 D.Diag(diag::warn_drv_clang_unsupported)
Daniel Dunbar2edd9232009-04-15 02:37:43 +00003296 << Args.getLastArg(options::OPT_funsigned_bitfields)->getAsString(Args);
3297
Daniel Dunbar6358d682010-10-15 22:30:42 +00003298 // -fsigned-bitfields is default, and clang doesn't support -fno-for-scope.
3299 if (!Args.hasFlag(options::OPT_ffor_scope,
3300 options::OPT_fno_for_scope))
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003301 D.Diag(diag::err_drv_clang_unsupported)
Daniel Dunbar6358d682010-10-15 22:30:42 +00003302 << Args.getLastArg(options::OPT_fno_for_scope)->getAsString(Args);
3303
Jeffrey Yasskin460aa542010-06-08 04:56:20 +00003304 // -fcaret-diagnostics is default.
3305 if (!Args.hasFlag(options::OPT_fcaret_diagnostics,
3306 options::OPT_fno_caret_diagnostics, true))
3307 CmdArgs.push_back("-fno-caret-diagnostics");
3308
Daniel Dunbar8281bde2009-04-19 21:09:34 +00003309 // -fdiagnostics-fixit-info is default, only pass non-default.
Mike Stump11289f42009-09-09 15:08:12 +00003310 if (!Args.hasFlag(options::OPT_fdiagnostics_fixit_info,
Daniel Dunbar8281bde2009-04-19 21:09:34 +00003311 options::OPT_fno_diagnostics_fixit_info))
3312 CmdArgs.push_back("-fno-diagnostics-fixit-info");
Eric Christopher84fbdb42011-08-19 00:30:14 +00003313
Daniel Dunbar092f0cc2009-04-16 06:32:38 +00003314 // Enable -fdiagnostics-show-option by default.
Mike Stump11289f42009-09-09 15:08:12 +00003315 if (Args.hasFlag(options::OPT_fdiagnostics_show_option,
Daniel Dunbar092f0cc2009-04-16 06:32:38 +00003316 options::OPT_fno_diagnostics_show_option))
3317 CmdArgs.push_back("-fdiagnostics-show-option");
Daniel Dunbar5ec95022009-11-04 06:24:57 +00003318
Chris Lattnerbf6fac82010-05-04 21:55:25 +00003319 if (const Arg *A =
3320 Args.getLastArg(options::OPT_fdiagnostics_show_category_EQ)) {
3321 CmdArgs.push_back("-fdiagnostics-show-category");
Richard Smithbd55daf2012-11-01 04:30:05 +00003322 CmdArgs.push_back(A->getValue());
Chris Lattnerbf6fac82010-05-04 21:55:25 +00003323 }
Daniel Dunbarfe6c97b2010-08-24 16:47:49 +00003324
Douglas Gregor643c9222011-05-21 17:07:29 +00003325 if (const Arg *A =
3326 Args.getLastArg(options::OPT_fdiagnostics_format_EQ)) {
3327 CmdArgs.push_back("-fdiagnostics-format");
Richard Smithbd55daf2012-11-01 04:30:05 +00003328 CmdArgs.push_back(A->getValue());
Douglas Gregor643c9222011-05-21 17:07:29 +00003329 }
3330
Chandler Carruthb6766f02011-03-27 01:50:55 +00003331 if (Arg *A = Args.getLastArg(
3332 options::OPT_fdiagnostics_show_note_include_stack,
3333 options::OPT_fno_diagnostics_show_note_include_stack)) {
3334 if (A->getOption().matches(
3335 options::OPT_fdiagnostics_show_note_include_stack))
3336 CmdArgs.push_back("-fdiagnostics-show-note-include-stack");
3337 else
3338 CmdArgs.push_back("-fno-diagnostics-show-note-include-stack");
3339 }
3340
Daniel Dunbar5ec95022009-11-04 06:24:57 +00003341 // Color diagnostics are the default, unless the terminal doesn't support
3342 // them.
Nico Weber7e2da792013-04-17 21:52:44 +00003343 // Support both clang's -f[no-]color-diagnostics and gcc's
3344 // -f[no-]diagnostics-colors[=never|always|auto].
3345 enum { Colors_On, Colors_Off, Colors_Auto } ShowColors = Colors_Auto;
3346 for (ArgList::const_iterator it = Args.begin(), ie = Args.end();
3347 it != ie; ++it) {
3348 const Option &O = (*it)->getOption();
3349 if (!O.matches(options::OPT_fcolor_diagnostics) &&
3350 !O.matches(options::OPT_fdiagnostics_color) &&
3351 !O.matches(options::OPT_fno_color_diagnostics) &&
3352 !O.matches(options::OPT_fno_diagnostics_color) &&
3353 !O.matches(options::OPT_fdiagnostics_color_EQ))
3354 continue;
3355
3356 (*it)->claim();
3357 if (O.matches(options::OPT_fcolor_diagnostics) ||
3358 O.matches(options::OPT_fdiagnostics_color)) {
3359 ShowColors = Colors_On;
3360 } else if (O.matches(options::OPT_fno_color_diagnostics) ||
3361 O.matches(options::OPT_fno_diagnostics_color)) {
3362 ShowColors = Colors_Off;
3363 } else {
3364 assert(O.matches(options::OPT_fdiagnostics_color_EQ));
3365 StringRef value((*it)->getValue());
3366 if (value == "always")
3367 ShowColors = Colors_On;
3368 else if (value == "never")
3369 ShowColors = Colors_Off;
3370 else if (value == "auto")
3371 ShowColors = Colors_Auto;
3372 else
3373 getToolChain().getDriver().Diag(diag::err_drv_clang_unsupported)
3374 << ("-fdiagnostics-color=" + value).str();
3375 }
3376 }
3377 if (ShowColors == Colors_On ||
3378 (ShowColors == Colors_Auto && llvm::sys::Process::StandardErrHasColors()))
Daniel Dunbar5ec95022009-11-04 06:24:57 +00003379 CmdArgs.push_back("-fcolor-diagnostics");
3380
Nico Rieck7857d462013-09-11 00:38:02 +00003381 if (Args.hasArg(options::OPT_fansi_escape_codes))
3382 CmdArgs.push_back("-fansi-escape-codes");
3383
Daniel Dunbardb097022009-06-08 21:13:54 +00003384 if (!Args.hasFlag(options::OPT_fshow_source_location,
3385 options::OPT_fno_show_source_location))
3386 CmdArgs.push_back("-fno-show-source-location");
Daniel Dunbar092f0cc2009-04-16 06:32:38 +00003387
Douglas Gregor643c9222011-05-21 17:07:29 +00003388 if (!Args.hasFlag(options::OPT_fshow_column,
3389 options::OPT_fno_show_column,
3390 true))
3391 CmdArgs.push_back("-fno-show-column");
3392
Douglas Gregor8ed0c0b2010-07-09 17:35:33 +00003393 if (!Args.hasFlag(options::OPT_fspell_checking,
3394 options::OPT_fno_spell_checking))
3395 CmdArgs.push_back("-fno-spell-checking");
Daniel Dunbarfe6c97b2010-08-24 16:47:49 +00003396
Daniel Dunbar473f8a62010-10-18 22:49:46 +00003397
Chad Rosierc8e56e82012-12-05 21:08:21 +00003398 // -fno-asm-blocks is default.
3399 if (Args.hasFlag(options::OPT_fasm_blocks, options::OPT_fno_asm_blocks,
3400 false))
3401 CmdArgs.push_back("-fasm-blocks");
Daniel Dunbar473f8a62010-10-18 22:49:46 +00003402
Arnold Schwaighofer7f994ce2013-08-13 15:46:23 +00003403 // Enable vectorization per default according to the optimization level
3404 // selected. For optimization levels that want vectorization we use the alias
3405 // option to simplify the hasFlag logic.
3406 bool EnableVec = shouldEnableVectorizerAtOLevel(Args);
3407 OptSpecifier VectorizeAliasOption = EnableVec ? options::OPT_O_Group :
Chad Rosier679b0752013-04-24 18:29:59 +00003408 options::OPT_fvectorize;
Chad Rosier679b0752013-04-24 18:29:59 +00003409 if (Args.hasFlag(options::OPT_fvectorize, VectorizeAliasOption,
Hal Finkel108c46a2013-08-28 05:21:45 +00003410 options::OPT_fno_vectorize, EnableVec))
Chad Rosier0d3ed6f2012-12-11 17:12:28 +00003411 CmdArgs.push_back("-vectorize-loops");
Chad Rosier0d3ed6f2012-12-11 17:12:28 +00003412
Rafael Espindolaf818ef42013-08-01 23:56:42 +00003413 // -fslp-vectorize is default.
3414 if (Args.hasFlag(options::OPT_fslp_vectorize,
3415 options::OPT_fno_slp_vectorize, true))
Nadav Rotem0a2604d2013-04-15 04:57:18 +00003416 CmdArgs.push_back("-vectorize-slp");
Hal Finkel061f1652012-12-11 19:59:32 +00003417
Nadav Rotem6a0dd6b2013-04-15 05:38:41 +00003418 // -fno-slp-vectorize-aggressive is default.
3419 if (Args.hasFlag(options::OPT_fslp_vectorize_aggressive,
Nick Lewyckyd3f3e4f2013-06-25 01:49:44 +00003420 options::OPT_fno_slp_vectorize_aggressive, false))
Nadav Rotem6a0dd6b2013-04-15 05:38:41 +00003421 CmdArgs.push_back("-vectorize-slp-aggressive");
Nadav Rotem6a0dd6b2013-04-15 05:38:41 +00003422
Jeffrey Yasskin2b99c6f2010-06-11 05:57:47 +00003423 if (Arg *A = Args.getLastArg(options::OPT_fshow_overloads_EQ))
3424 A->render(Args, CmdArgs);
3425
Daniel Dunbarf5e9b1f2009-04-19 21:20:32 +00003426 // -fdollars-in-identifiers default varies depending on platform and
3427 // language; only pass if specified.
Mike Stump11289f42009-09-09 15:08:12 +00003428 if (Arg *A = Args.getLastArg(options::OPT_fdollars_in_identifiers,
Daniel Dunbarf5e9b1f2009-04-19 21:20:32 +00003429 options::OPT_fno_dollars_in_identifiers)) {
3430 if (A->getOption().matches(options::OPT_fdollars_in_identifiers))
Daniel Dunbar15cef0e2009-12-16 20:10:18 +00003431 CmdArgs.push_back("-fdollars-in-identifiers");
Daniel Dunbarf5e9b1f2009-04-19 21:20:32 +00003432 else
Daniel Dunbar15cef0e2009-12-16 20:10:18 +00003433 CmdArgs.push_back("-fno-dollars-in-identifiers");
Daniel Dunbarf5e9b1f2009-04-19 21:20:32 +00003434 }
3435
Daniel Dunbaradeeb052009-05-22 19:02:20 +00003436 // -funit-at-a-time is default, and we don't support -fno-unit-at-a-time for
3437 // practical purposes.
Mike Stump11289f42009-09-09 15:08:12 +00003438 if (Arg *A = Args.getLastArg(options::OPT_funit_at_a_time,
Daniel Dunbaradeeb052009-05-22 19:02:20 +00003439 options::OPT_fno_unit_at_a_time)) {
3440 if (A->getOption().matches(options::OPT_fno_unit_at_a_time))
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003441 D.Diag(diag::warn_drv_clang_unsupported) << A->getAsString(Args);
Daniel Dunbaradeeb052009-05-22 19:02:20 +00003442 }
Eli Friedmanbb0d9a52009-07-14 21:58:17 +00003443
Eli Friedman055c9702011-11-02 01:53:16 +00003444 if (Args.hasFlag(options::OPT_fapple_pragma_pack,
3445 options::OPT_fno_apple_pragma_pack, false))
3446 CmdArgs.push_back("-fapple-pragma-pack");
3447
Eli Benderskyc95cfe82013-07-24 18:20:14 +00003448 // le32-specific flags:
3449 // -fno-math-builtin: clang should not convert math builtins to intrinsics
3450 // by default.
3451 if (getToolChain().getArch() == llvm::Triple::le32) {
3452 CmdArgs.push_back("-fno-math-builtin");
3453 }
3454
Daniel Dunbar2ffe0292009-09-10 03:37:02 +00003455 // Default to -fno-builtin-str{cat,cpy} on Darwin for ARM.
Daniel Dunbar4fa08112009-09-10 04:57:27 +00003456 //
Daniel Dunbar6c536aa2009-12-11 23:00:49 +00003457 // FIXME: This is disabled until clang -cc1 supports -fno-builtin-foo. PR4941.
Daniel Dunbar4fa08112009-09-10 04:57:27 +00003458#if 0
Bob Wilson6524dd32011-10-14 05:03:44 +00003459 if (getToolChain().getTriple().isOSDarwin() &&
Eli Benderskyd4a6aec2013-07-24 22:20:49 +00003460 (getToolChain().getArch() == llvm::Triple::arm ||
3461 getToolChain().getArch() == llvm::Triple::thumb)) {
Daniel Dunbar2ffe0292009-09-10 03:37:02 +00003462 if (!Args.hasArg(options::OPT_fbuiltin_strcat))
3463 CmdArgs.push_back("-fno-builtin-strcat");
3464 if (!Args.hasArg(options::OPT_fbuiltin_strcpy))
3465 CmdArgs.push_back("-fno-builtin-strcpy");
3466 }
Daniel Dunbar4fa08112009-09-10 04:57:27 +00003467#endif
Daniel Dunbar2ffe0292009-09-10 03:37:02 +00003468
Daniel Dunbar8c3d7352011-03-18 21:23:40 +00003469 // Only allow -traditional or -traditional-cpp outside in preprocessing modes.
Mike Stump11289f42009-09-09 15:08:12 +00003470 if (Arg *A = Args.getLastArg(options::OPT_traditional,
Daniel Dunbar8c3d7352011-03-18 21:23:40 +00003471 options::OPT_traditional_cpp)) {
3472 if (isa<PreprocessJobAction>(JA))
3473 CmdArgs.push_back("-traditional-cpp");
Eric Christopher84fbdb42011-08-19 00:30:14 +00003474 else
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003475 D.Diag(diag::err_drv_clang_unsupported) << A->getAsString(Args);
Daniel Dunbar8c3d7352011-03-18 21:23:40 +00003476 }
Eli Friedmanbb0d9a52009-07-14 21:58:17 +00003477
Daniel Dunbara2aedc62009-03-18 10:01:51 +00003478 Args.AddLastArg(CmdArgs, options::OPT_dM);
Chris Lattnercac63f32009-04-12 01:56:53 +00003479 Args.AddLastArg(CmdArgs, options::OPT_dD);
Ted Kremeneke73d9ed2011-11-11 00:07:43 +00003480
3481 // Handle serialized diagnostics.
3482 if (Arg *A = Args.getLastArg(options::OPT__serialize_diags)) {
3483 CmdArgs.push_back("-serialize-diagnostic-file");
Richard Smithbd55daf2012-11-01 04:30:05 +00003484 CmdArgs.push_back(Args.MakeArgString(A->getValue()));
Ted Kremeneke73d9ed2011-11-11 00:07:43 +00003485 }
Daniel Dunbara2aedc62009-03-18 10:01:51 +00003486
Ted Kremenekb47e6bc2012-09-13 06:41:18 +00003487 if (Args.hasArg(options::OPT_fretain_comments_from_system_headers))
3488 CmdArgs.push_back("-fretain-comments-from-system-headers");
3489
Dmitri Gribenkoacf2e782013-02-22 14:21:27 +00003490 // Forward -fcomment-block-commands to -cc1.
3491 Args.AddAllArgs(CmdArgs, options::OPT_fcomment_block_commands);
Dmitri Gribenkoa7d16ce2013-04-10 15:35:17 +00003492 // Forward -fparse-all-comments to -cc1.
3493 Args.AddAllArgs(CmdArgs, options::OPT_fparse_all_comments);
Dmitri Gribenkoacf2e782013-02-22 14:21:27 +00003494
Daniel Dunbar76fa8402010-04-15 06:09:03 +00003495 // Forward -Xclang arguments to -cc1, and -mllvm arguments to the LLVM option
3496 // parser.
Daniel Dunbara2aedc62009-03-18 10:01:51 +00003497 Args.AddAllArgValues(CmdArgs, options::OPT_Xclang);
Daniel Dunbar76fa8402010-04-15 06:09:03 +00003498 for (arg_iterator it = Args.filtered_begin(options::OPT_mllvm),
3499 ie = Args.filtered_end(); it != ie; ++it) {
Daniel Dunbara442fd52010-06-11 22:00:13 +00003500 (*it)->claim();
Daniel Dunbar88534f42010-04-17 06:10:00 +00003501
Daniel Dunbar76fa8402010-04-15 06:09:03 +00003502 // We translate this by hand to the -cc1 argument, since nightly test uses
3503 // it and developers have been trained to spell it with -mllvm.
Richard Smithbd55daf2012-11-01 04:30:05 +00003504 if (StringRef((*it)->getValue(0)) == "-disable-llvm-optzns")
Daniel Dunbar76fa8402010-04-15 06:09:03 +00003505 CmdArgs.push_back("-disable-llvm-optzns");
3506 else
Daniel Dunbara442fd52010-06-11 22:00:13 +00003507 (*it)->render(Args, CmdArgs);
Daniel Dunbar76fa8402010-04-15 06:09:03 +00003508 }
Daniel Dunbara2aedc62009-03-18 10:01:51 +00003509
Daniel Dunbard67a3222009-03-30 06:36:42 +00003510 if (Output.getType() == types::TY_Dependencies) {
3511 // Handled with other dependency code.
Daniel Dunbar5cdf3e02009-03-19 07:29:38 +00003512 } else if (Output.isFilename()) {
Daniel Dunbara3246a02009-03-18 08:07:30 +00003513 CmdArgs.push_back("-o");
Daniel Dunbar5cdf3e02009-03-19 07:29:38 +00003514 CmdArgs.push_back(Output.getFilename());
3515 } else {
3516 assert(Output.isNothing() && "Invalid output.");
Daniel Dunbara3246a02009-03-18 08:07:30 +00003517 }
3518
Daniel Dunbara2aedc62009-03-18 10:01:51 +00003519 for (InputInfoList::const_iterator
3520 it = Inputs.begin(), ie = Inputs.end(); it != ie; ++it) {
3521 const InputInfo &II = *it;
3522 CmdArgs.push_back("-x");
Fariborz Jahanian659bc4a2012-09-28 19:05:17 +00003523 if (Args.hasArg(options::OPT_rewrite_objc))
3524 CmdArgs.push_back(types::getTypeName(types::TY_PP_ObjCXX));
3525 else
3526 CmdArgs.push_back(types::getTypeName(II.getType()));
Daniel Dunbarb440f562010-08-02 02:38:21 +00003527 if (II.isFilename())
Daniel Dunbar5cdf3e02009-03-19 07:29:38 +00003528 CmdArgs.push_back(II.getFilename());
Daniel Dunbara2aedc62009-03-18 10:01:51 +00003529 else
Daniel Dunbar5cdf3e02009-03-19 07:29:38 +00003530 II.getInputArg().renderAsInput(Args, CmdArgs);
Daniel Dunbara2aedc62009-03-18 10:01:51 +00003531 }
Daniel Dunbard067f7f2009-04-08 23:54:23 +00003532
Chris Lattnere9d7d782009-11-03 19:50:27 +00003533 Args.AddAllArgs(CmdArgs, options::OPT_undef);
3534
Daniel Dunbarb31b76f2010-07-18 21:16:15 +00003535 const char *Exec = getToolChain().getDriver().getClangProgramPath();
Daniel Dunbar24c7f5e2009-12-18 02:43:17 +00003536
3537 // Optionally embed the -cc1 level arguments into the debug info, for build
3538 // analysis.
3539 if (getToolChain().UseDwarfDebugFlags()) {
Daniel Dunbar7f3d9502010-06-04 18:47:06 +00003540 ArgStringList OriginalArgs;
3541 for (ArgList::const_iterator it = Args.begin(),
3542 ie = Args.end(); it != ie; ++it)
3543 (*it)->render(Args, OriginalArgs);
Daniel Dunbarfe6c97b2010-08-24 16:47:49 +00003544
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00003545 SmallString<256> Flags;
Daniel Dunbar24c7f5e2009-12-18 02:43:17 +00003546 Flags += Exec;
Daniel Dunbar7f3d9502010-06-04 18:47:06 +00003547 for (unsigned i = 0, e = OriginalArgs.size(); i != e; ++i) {
Daniel Dunbar24c7f5e2009-12-18 02:43:17 +00003548 Flags += " ";
Daniel Dunbar7f3d9502010-06-04 18:47:06 +00003549 Flags += OriginalArgs[i];
Daniel Dunbar24c7f5e2009-12-18 02:43:17 +00003550 }
3551 CmdArgs.push_back("-dwarf-debug-flags");
3552 CmdArgs.push_back(Args.MakeArgString(Flags.str()));
3553 }
3554
Eric Christopherd3804002013-02-22 20:12:52 +00003555 // Add the split debug info name to the command lines here so we
3556 // can propagate it to the backend.
3557 bool SplitDwarf = Args.hasArg(options::OPT_gsplit_dwarf) &&
Cameron Esfahani556d91e2013-09-14 01:09:11 +00003558 getToolChain().getTriple().isOSLinux() &&
Eric Christopherf1545832013-02-22 23:50:16 +00003559 (isa<AssembleJobAction>(JA) || isa<CompileJobAction>(JA));
Eric Christopherd3804002013-02-22 20:12:52 +00003560 const char *SplitDwarfOut;
3561 if (SplitDwarf) {
3562 CmdArgs.push_back("-split-dwarf-file");
3563 SplitDwarfOut = SplitDebugName(Args, Inputs);
3564 CmdArgs.push_back(SplitDwarfOut);
3565 }
3566
3567 // Finally add the compile command to the compilation.
Daniel Dunbar7fbaf532010-08-02 02:38:28 +00003568 C.addCommand(new Command(JA, *this, Exec, CmdArgs));
Daniel Dunbar17731772009-03-23 19:03:36 +00003569
Eric Christopherf1545832013-02-22 23:50:16 +00003570 // Handle the debug info splitting at object creation time if we're
3571 // creating an object.
Eric Christopher248357f2013-02-21 22:35:01 +00003572 // TODO: Currently only works on linux with newer objcopy.
Eric Christopherf1545832013-02-22 23:50:16 +00003573 if (SplitDwarf && !isa<CompileJobAction>(JA))
Eric Christopherd3804002013-02-22 20:12:52 +00003574 SplitDebugInfo(getToolChain(), C, *this, JA, Args, Output, SplitDwarfOut);
Eric Christopher248357f2013-02-21 22:35:01 +00003575
Roman Divacky178e01602011-02-10 16:52:03 +00003576 if (Arg *A = Args.getLastArg(options::OPT_pg))
3577 if (Args.hasArg(options::OPT_fomit_frame_pointer))
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003578 D.Diag(diag::err_drv_argument_not_allowed_with)
Roman Divacky178e01602011-02-10 16:52:03 +00003579 << "-fomit-frame-pointer" << A->getAsString(Args);
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00003580
Daniel Dunbarc2a71892009-04-03 20:51:31 +00003581 // Claim some arguments which clang supports automatically.
3582
Daniel Dunbar3e0cac62010-04-15 06:18:42 +00003583 // -fpch-preprocess is used with gcc to add a special marker in the output to
3584 // include the PCH file. Clang's PTH solution is completely transparent, so we
3585 // do not need to deal with it at all.
Daniel Dunbarc2a71892009-04-03 20:51:31 +00003586 Args.ClaimAllArgs(options::OPT_fpch_preprocess);
Daniel Dunbard067f7f2009-04-08 23:54:23 +00003587
Daniel Dunbar17731772009-03-23 19:03:36 +00003588 // Claim some arguments which clang doesn't support, but we don't
3589 // care to warn the user about.
Daniel Dunbar44b36ee2009-11-25 11:53:23 +00003590 Args.ClaimAllArgs(options::OPT_clang_ignored_f_Group);
3591 Args.ClaimAllArgs(options::OPT_clang_ignored_m_Group);
Rafael Espindola22f603032011-02-28 23:29:45 +00003592
Rafael Espindolab0092d72013-09-04 19:37:35 +00003593 // Disable warnings for clang -E -emit-llvm foo.c
Rafael Espindolad95a8122011-03-01 05:25:27 +00003594 Args.ClaimAllArgs(options::OPT_emit_llvm);
Daniel Dunbar1a093d22009-03-18 06:00:36 +00003595}
3596
John McCall5fb5df92012-06-20 06:18:46 +00003597/// Add options related to the Objective-C runtime/ABI.
3598///
3599/// Returns true if the runtime is non-fragile.
3600ObjCRuntime Clang::AddObjCRuntimeArgs(const ArgList &args,
3601 ArgStringList &cmdArgs,
3602 RewriteKind rewriteKind) const {
3603 // Look for the controlling runtime option.
3604 Arg *runtimeArg = args.getLastArg(options::OPT_fnext_runtime,
3605 options::OPT_fgnu_runtime,
3606 options::OPT_fobjc_runtime_EQ);
3607
3608 // Just forward -fobjc-runtime= to the frontend. This supercedes
3609 // options about fragility.
3610 if (runtimeArg &&
3611 runtimeArg->getOption().matches(options::OPT_fobjc_runtime_EQ)) {
3612 ObjCRuntime runtime;
Richard Smithbd55daf2012-11-01 04:30:05 +00003613 StringRef value = runtimeArg->getValue();
John McCall5fb5df92012-06-20 06:18:46 +00003614 if (runtime.tryParse(value)) {
3615 getToolChain().getDriver().Diag(diag::err_drv_unknown_objc_runtime)
3616 << value;
3617 }
3618
3619 runtimeArg->render(args, cmdArgs);
3620 return runtime;
3621 }
3622
3623 // Otherwise, we'll need the ABI "version". Version numbers are
3624 // slightly confusing for historical reasons:
3625 // 1 - Traditional "fragile" ABI
3626 // 2 - Non-fragile ABI, version 1
3627 // 3 - Non-fragile ABI, version 2
3628 unsigned objcABIVersion = 1;
3629 // If -fobjc-abi-version= is present, use that to set the version.
3630 if (Arg *abiArg = args.getLastArg(options::OPT_fobjc_abi_version_EQ)) {
Richard Smithbd55daf2012-11-01 04:30:05 +00003631 StringRef value = abiArg->getValue();
John McCall5fb5df92012-06-20 06:18:46 +00003632 if (value == "1")
3633 objcABIVersion = 1;
3634 else if (value == "2")
3635 objcABIVersion = 2;
3636 else if (value == "3")
3637 objcABIVersion = 3;
3638 else
3639 getToolChain().getDriver().Diag(diag::err_drv_clang_unsupported)
3640 << value;
3641 } else {
3642 // Otherwise, determine if we are using the non-fragile ABI.
3643 bool nonFragileABIIsDefault =
3644 (rewriteKind == RK_NonFragile ||
3645 (rewriteKind == RK_None &&
3646 getToolChain().IsObjCNonFragileABIDefault()));
3647 if (args.hasFlag(options::OPT_fobjc_nonfragile_abi,
3648 options::OPT_fno_objc_nonfragile_abi,
3649 nonFragileABIIsDefault)) {
3650 // Determine the non-fragile ABI version to use.
3651#ifdef DISABLE_DEFAULT_NONFRAGILEABI_TWO
3652 unsigned nonFragileABIVersion = 1;
3653#else
3654 unsigned nonFragileABIVersion = 2;
3655#endif
3656
3657 if (Arg *abiArg = args.getLastArg(
3658 options::OPT_fobjc_nonfragile_abi_version_EQ)) {
Richard Smithbd55daf2012-11-01 04:30:05 +00003659 StringRef value = abiArg->getValue();
John McCall5fb5df92012-06-20 06:18:46 +00003660 if (value == "1")
3661 nonFragileABIVersion = 1;
3662 else if (value == "2")
3663 nonFragileABIVersion = 2;
3664 else
3665 getToolChain().getDriver().Diag(diag::err_drv_clang_unsupported)
3666 << value;
3667 }
3668
3669 objcABIVersion = 1 + nonFragileABIVersion;
3670 } else {
3671 objcABIVersion = 1;
3672 }
3673 }
3674
3675 // We don't actually care about the ABI version other than whether
3676 // it's non-fragile.
3677 bool isNonFragile = objcABIVersion != 1;
3678
3679 // If we have no runtime argument, ask the toolchain for its default runtime.
3680 // However, the rewriter only really supports the Mac runtime, so assume that.
3681 ObjCRuntime runtime;
3682 if (!runtimeArg) {
3683 switch (rewriteKind) {
3684 case RK_None:
3685 runtime = getToolChain().getDefaultObjCRuntime(isNonFragile);
3686 break;
3687 case RK_Fragile:
3688 runtime = ObjCRuntime(ObjCRuntime::FragileMacOSX, VersionTuple());
3689 break;
3690 case RK_NonFragile:
3691 runtime = ObjCRuntime(ObjCRuntime::MacOSX, VersionTuple());
3692 break;
3693 }
3694
3695 // -fnext-runtime
3696 } else if (runtimeArg->getOption().matches(options::OPT_fnext_runtime)) {
3697 // On Darwin, make this use the default behavior for the toolchain.
3698 if (getToolChain().getTriple().isOSDarwin()) {
3699 runtime = getToolChain().getDefaultObjCRuntime(isNonFragile);
3700
3701 // Otherwise, build for a generic macosx port.
3702 } else {
3703 runtime = ObjCRuntime(ObjCRuntime::MacOSX, VersionTuple());
3704 }
3705
3706 // -fgnu-runtime
3707 } else {
3708 assert(runtimeArg->getOption().matches(options::OPT_fgnu_runtime));
David Chisnall314896c2012-07-04 10:37:03 +00003709 // Legacy behaviour is to target the gnustep runtime if we are i
3710 // non-fragile mode or the GCC runtime in fragile mode.
3711 if (isNonFragile)
David Chisnalla5f59412012-10-16 15:11:55 +00003712 runtime = ObjCRuntime(ObjCRuntime::GNUstep, VersionTuple(1,6));
David Chisnall314896c2012-07-04 10:37:03 +00003713 else
3714 runtime = ObjCRuntime(ObjCRuntime::GCC, VersionTuple());
John McCall5fb5df92012-06-20 06:18:46 +00003715 }
3716
3717 cmdArgs.push_back(args.MakeArgString(
3718 "-fobjc-runtime=" + runtime.getAsString()));
3719 return runtime;
3720}
3721
Hans Wennborg75958c42013-08-08 00:17:41 +00003722void Clang::AddClangCLArgs(const ArgList &Args, ArgStringList &CmdArgs) const {
3723 unsigned RTOptionID = options::OPT__SLASH_MT;
3724
Hans Wennborgf1a74252013-09-10 20:18:04 +00003725 if (Args.hasArg(options::OPT__SLASH_LDd))
3726 // The /LDd option implies /MTd. The dependent lib part can be overridden,
3727 // but defining _DEBUG is sticky.
3728 RTOptionID = options::OPT__SLASH_MTd;
3729
Hans Wennborg9cb7d9ba2013-09-18 22:26:39 +00003730 if (Arg *A = Args.getLastArg(options::OPT__SLASH_M_Group))
Hans Wennborg75958c42013-08-08 00:17:41 +00003731 RTOptionID = A->getOption().getID();
Hans Wennborgd9ad0682013-09-11 16:38:41 +00003732
Hans Wennborg75958c42013-08-08 00:17:41 +00003733 switch(RTOptionID) {
3734 case options::OPT__SLASH_MD:
Hans Wennborgf1a74252013-09-10 20:18:04 +00003735 if (Args.hasArg(options::OPT__SLASH_LDd))
3736 CmdArgs.push_back("-D_DEBUG");
Hans Wennborg75958c42013-08-08 00:17:41 +00003737 CmdArgs.push_back("-D_MT");
3738 CmdArgs.push_back("-D_DLL");
3739 CmdArgs.push_back("--dependent-lib=msvcrt");
3740 break;
3741 case options::OPT__SLASH_MDd:
3742 CmdArgs.push_back("-D_DEBUG");
3743 CmdArgs.push_back("-D_MT");
3744 CmdArgs.push_back("-D_DLL");
3745 CmdArgs.push_back("--dependent-lib=msvcrtd");
3746 break;
3747 case options::OPT__SLASH_MT:
Hans Wennborgf1a74252013-09-10 20:18:04 +00003748 if (Args.hasArg(options::OPT__SLASH_LDd))
3749 CmdArgs.push_back("-D_DEBUG");
Hans Wennborg75958c42013-08-08 00:17:41 +00003750 CmdArgs.push_back("-D_MT");
3751 CmdArgs.push_back("--dependent-lib=libcmt");
3752 break;
3753 case options::OPT__SLASH_MTd:
3754 CmdArgs.push_back("-D_DEBUG");
3755 CmdArgs.push_back("-D_MT");
3756 CmdArgs.push_back("--dependent-lib=libcmtd");
3757 break;
3758 default:
3759 llvm_unreachable("Unexpected option ID.");
3760 }
3761
Reid Kleckner6beca0e2013-08-08 19:33:10 +00003762 // This provides POSIX compatibility (maps 'open' to '_open'), which most
3763 // users want. The /Za flag to cl.exe turns this off, but it's not
3764 // implemented in clang.
3765 CmdArgs.push_back("--dependent-lib=oldnames");
Hans Wennborg614f7072013-08-08 19:54:30 +00003766
3767 // FIXME: Make this default for the win32 triple.
3768 CmdArgs.push_back("-cxx-abi");
3769 CmdArgs.push_back("microsoft");
Hans Wennborg0fd62072013-08-09 00:32:23 +00003770
3771 if (Arg *A = Args.getLastArg(options::OPT_show_includes))
3772 A->render(Args, CmdArgs);
Hans Wennborg81f74482013-09-10 01:07:07 +00003773
3774 if (!Args.hasArg(options::OPT_fdiagnostics_format_EQ)) {
3775 CmdArgs.push_back("-fdiagnostics-format");
3776 CmdArgs.push_back("msvc");
3777 }
Hans Wennborg75958c42013-08-08 00:17:41 +00003778}
3779
Daniel Dunbar4f5e79c2010-05-20 21:30:13 +00003780void ClangAs::ConstructJob(Compilation &C, const JobAction &JA,
Daniel Dunbar4f5e79c2010-05-20 21:30:13 +00003781 const InputInfo &Output,
3782 const InputInfoList &Inputs,
3783 const ArgList &Args,
3784 const char *LinkingOutput) const {
Daniel Dunbar4f5e79c2010-05-20 21:30:13 +00003785 ArgStringList CmdArgs;
3786
3787 assert(Inputs.size() == 1 && "Unexpected number of inputs.");
3788 const InputInfo &Input = Inputs[0];
3789
Rafael Espindolacfaadda2010-11-17 22:13:25 +00003790 // Don't warn about "clang -w -c foo.s"
3791 Args.ClaimAllArgs(options::OPT_w);
Rafael Espindolad95a8122011-03-01 05:25:27 +00003792 // and "clang -emit-llvm -c foo.s"
3793 Args.ClaimAllArgs(options::OPT_emit_llvm);
Rafael Espindolacfaadda2010-11-17 22:13:25 +00003794
Daniel Dunbar4f5e79c2010-05-20 21:30:13 +00003795 // Invoke ourselves in -cc1as mode.
3796 //
3797 // FIXME: Implement custom jobs for internal actions.
3798 CmdArgs.push_back("-cc1as");
3799
3800 // Add the "effective" target triple.
3801 CmdArgs.push_back("-triple");
Chad Rosierd3a0f952011-09-20 20:44:06 +00003802 std::string TripleStr =
3803 getToolChain().ComputeEffectiveClangTriple(Args, Input.getType());
Daniel Dunbar4f5e79c2010-05-20 21:30:13 +00003804 CmdArgs.push_back(Args.MakeArgString(TripleStr));
3805
3806 // Set the output mode, we currently only expect to be used as a real
3807 // assembler.
3808 CmdArgs.push_back("-filetype");
3809 CmdArgs.push_back("obj");
3810
Eric Christopher45f2e712012-12-18 00:31:10 +00003811 // Set the main file name, so that debug info works even with
3812 // -save-temps or preprocessed assembly.
3813 CmdArgs.push_back("-main-file-name");
3814 CmdArgs.push_back(Clang::getBaseInputName(Args, Inputs));
3815
Rafael Espindola22ce34a2013-08-20 22:12:08 +00003816 // Add the target cpu
Rafael Espindola28e1f4b2013-08-21 16:39:20 +00003817 const llvm::Triple &Triple = getToolChain().getTriple();
3818 std::string CPU = getCPUName(Args, Triple);
Rafael Espindola22ce34a2013-08-20 22:12:08 +00003819 if (!CPU.empty()) {
3820 CmdArgs.push_back("-target-cpu");
3821 CmdArgs.push_back(Args.MakeArgString(CPU));
3822 }
3823
Rafael Espindola28e1f4b2013-08-21 16:39:20 +00003824 // Add the target features
3825 const Driver &D = getToolChain().getDriver();
3826 getTargetFeatures(D, Triple, Args, CmdArgs);
Jim Grosbach576452b2012-02-10 20:37:10 +00003827
Daniel Dunbar1d733e22011-03-17 17:37:29 +00003828 // Ignore explicit -force_cpusubtype_ALL option.
3829 (void) Args.hasArg(options::OPT_force__cpusubtype__ALL);
Daniel Dunbar4f5e79c2010-05-20 21:30:13 +00003830
Eric Christopherfc3ee562012-01-10 00:38:01 +00003831 // Determine the original source input.
3832 const Action *SourceAction = &JA;
3833 while (SourceAction->getKind() != Action::InputClass) {
3834 assert(!SourceAction->getInputs().empty() && "unexpected root action!");
3835 SourceAction = SourceAction->getInputs()[0];
3836 }
3837
Chandler Carruth4d5e1a92012-12-17 21:40:04 +00003838 // Forward -g and handle debug info related flags, assuming we are dealing
3839 // with an actual assembly file.
Eric Christopherfc3ee562012-01-10 00:38:01 +00003840 if (SourceAction->getType() == types::TY_Asm ||
3841 SourceAction->getType() == types::TY_PP_Asm) {
3842 Args.ClaimAllArgs(options::OPT_g_Group);
3843 if (Arg *A = Args.getLastArg(options::OPT_g_Group))
3844 if (!A->getOption().matches(options::OPT_g0))
3845 CmdArgs.push_back("-g");
Chandler Carruth4d5e1a92012-12-17 21:40:04 +00003846
3847 // Add the -fdebug-compilation-dir flag if needed.
3848 addDebugCompDirArg(Args, CmdArgs);
Kevin Enderbyae2ec472013-01-17 21:38:06 +00003849
3850 // Set the AT_producer to the clang version when using the integrated
3851 // assembler on assembly source files.
3852 CmdArgs.push_back("-dwarf-debug-producer");
3853 CmdArgs.push_back(Args.MakeArgString(getClangFullVersion()));
Eric Christopherfc3ee562012-01-10 00:38:01 +00003854 }
Kevin Enderby292dc082011-12-22 19:31:58 +00003855
3856 // Optionally embed the -cc1as level arguments into the debug info, for build
3857 // analysis.
3858 if (getToolChain().UseDwarfDebugFlags()) {
3859 ArgStringList OriginalArgs;
3860 for (ArgList::const_iterator it = Args.begin(),
3861 ie = Args.end(); it != ie; ++it)
3862 (*it)->render(Args, OriginalArgs);
3863
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00003864 SmallString<256> Flags;
Kevin Enderby292dc082011-12-22 19:31:58 +00003865 const char *Exec = getToolChain().getDriver().getClangProgramPath();
3866 Flags += Exec;
3867 for (unsigned i = 0, e = OriginalArgs.size(); i != e; ++i) {
3868 Flags += " ";
3869 Flags += OriginalArgs[i];
3870 }
3871 CmdArgs.push_back("-dwarf-debug-flags");
3872 CmdArgs.push_back(Args.MakeArgString(Flags.str()));
3873 }
Daniel Dunbar4f5e79c2010-05-20 21:30:13 +00003874
3875 // FIXME: Add -static support, once we have it.
3876
David Blaikie9260ed62013-07-25 21:19:01 +00003877 CollectArgsForIntegratedAssembler(C, Args, CmdArgs,
3878 getToolChain().getDriver());
3879
Daniel Dunbar252e8f92011-04-29 17:53:18 +00003880 Args.AddAllArgs(CmdArgs, options::OPT_mllvm);
Daniel Dunbar4f5e79c2010-05-20 21:30:13 +00003881
3882 assert(Output.isFilename() && "Unexpected lipo output.");
3883 CmdArgs.push_back("-o");
3884 CmdArgs.push_back(Output.getFilename());
3885
Daniel Dunbarb440f562010-08-02 02:38:21 +00003886 assert(Input.isFilename() && "Invalid input.");
3887 CmdArgs.push_back(Input.getFilename());
Daniel Dunbar4f5e79c2010-05-20 21:30:13 +00003888
Daniel Dunbarb31b76f2010-07-18 21:16:15 +00003889 const char *Exec = getToolChain().getDriver().getClangProgramPath();
Daniel Dunbar7fbaf532010-08-02 02:38:28 +00003890 C.addCommand(new Command(JA, *this, Exec, CmdArgs));
Eric Christophera75018a2013-04-10 21:30:40 +00003891
3892 // Handle the debug info splitting at object creation time if we're
3893 // creating an object.
3894 // TODO: Currently only works on linux with newer objcopy.
3895 if (Args.hasArg(options::OPT_gsplit_dwarf) &&
Cameron Esfahani556d91e2013-09-14 01:09:11 +00003896 getToolChain().getTriple().isOSLinux())
Eric Christophera75018a2013-04-10 21:30:40 +00003897 SplitDebugInfo(getToolChain(), C, *this, JA, Args, Output,
3898 SplitDebugName(Args, Inputs));
Daniel Dunbar4f5e79c2010-05-20 21:30:13 +00003899}
3900
Daniel Dunbara3246a02009-03-18 08:07:30 +00003901void gcc::Common::ConstructJob(Compilation &C, const JobAction &JA,
Daniel Dunbara3246a02009-03-18 08:07:30 +00003902 const InputInfo &Output,
3903 const InputInfoList &Inputs,
Daniel Dunbara2aedc62009-03-18 10:01:51 +00003904 const ArgList &Args,
Daniel Dunbara3246a02009-03-18 08:07:30 +00003905 const char *LinkingOutput) const {
Daniel Dunbar083edf72009-12-21 18:54:17 +00003906 const Driver &D = getToolChain().getDriver();
Daniel Dunbara3246a02009-03-18 08:07:30 +00003907 ArgStringList CmdArgs;
Daniel Dunbar1a093d22009-03-18 06:00:36 +00003908
Daniel Dunbard067f7f2009-04-08 23:54:23 +00003909 for (ArgList::const_iterator
Daniel Dunbara2aedc62009-03-18 10:01:51 +00003910 it = Args.begin(), ie = Args.end(); it != ie; ++it) {
Daniel Dunbara3246a02009-03-18 08:07:30 +00003911 Arg *A = *it;
Michael J. Spencer66e2b202012-10-19 22:37:06 +00003912 if (forwardToGCC(A->getOption())) {
Daniel Dunbar939c1212010-08-03 16:14:14 +00003913 // Don't forward any -g arguments to assembly steps.
3914 if (isa<AssembleJobAction>(JA) &&
3915 A->getOption().matches(options::OPT_g_Group))
3916 continue;
3917
NAKAMURA Takumi053704f2013-08-19 11:51:51 +00003918 // Don't forward any -W arguments to assembly and link steps.
3919 if ((isa<AssembleJobAction>(JA) || isa<LinkJobAction>(JA)) &&
3920 A->getOption().matches(options::OPT_W_Group))
3921 continue;
3922
Daniel Dunbar2da02722009-03-19 07:55:12 +00003923 // It is unfortunate that we have to claim here, as this means
3924 // we will basically never report anything interesting for
Daniel Dunbar5716d872009-05-02 21:41:52 +00003925 // platforms using a generic gcc, even if we are just using gcc
3926 // to get to the assembler.
Daniel Dunbar2da02722009-03-19 07:55:12 +00003927 A->claim();
Daniel Dunbara2aedc62009-03-18 10:01:51 +00003928 A->render(Args, CmdArgs);
Daniel Dunbar2da02722009-03-19 07:55:12 +00003929 }
Daniel Dunbara3246a02009-03-18 08:07:30 +00003930 }
Daniel Dunbard067f7f2009-04-08 23:54:23 +00003931
Daniel Dunbar4e295052010-01-25 22:35:08 +00003932 RenderExtraToolArgs(JA, CmdArgs);
Daniel Dunbara3246a02009-03-18 08:07:30 +00003933
3934 // If using a driver driver, force the arch.
Rafael Espindola35ca7d92012-10-07 04:44:33 +00003935 llvm::Triple::ArchType Arch = getToolChain().getArch();
Bob Wilson6524dd32011-10-14 05:03:44 +00003936 if (getToolChain().getTriple().isOSDarwin()) {
Daniel Dunbara3246a02009-03-18 08:07:30 +00003937 CmdArgs.push_back("-arch");
Daniel Dunbar0a05d932009-04-01 20:33:11 +00003938
3939 // FIXME: Remove these special cases.
Rafael Espindola35ca7d92012-10-07 04:44:33 +00003940 if (Arch == llvm::Triple::ppc)
Daniel Dunbar5bbebfe2009-05-22 02:21:04 +00003941 CmdArgs.push_back("ppc");
Rafael Espindola35ca7d92012-10-07 04:44:33 +00003942 else if (Arch == llvm::Triple::ppc64)
Daniel Dunbar5bbebfe2009-05-22 02:21:04 +00003943 CmdArgs.push_back("ppc64");
Bill Schmidt778d3872013-07-26 01:36:11 +00003944 else if (Arch == llvm::Triple::ppc64le)
3945 CmdArgs.push_back("ppc64le");
Daniel Dunbar5bbebfe2009-05-22 02:21:04 +00003946 else
Rafael Espindola35ca7d92012-10-07 04:44:33 +00003947 CmdArgs.push_back(Args.MakeArgString(getToolChain().getArchName()));
Daniel Dunbara3246a02009-03-18 08:07:30 +00003948 }
3949
Daniel Dunbar5716d872009-05-02 21:41:52 +00003950 // Try to force gcc to match the tool chain we want, if we recognize
3951 // the arch.
Daniel Dunbar5bbebfe2009-05-22 02:21:04 +00003952 //
3953 // FIXME: The triple class should directly provide the information we want
3954 // here.
Rafael Espindola35ca7d92012-10-07 04:44:33 +00003955 if (Arch == llvm::Triple::x86 || Arch == llvm::Triple::ppc)
Daniel Dunbar5716d872009-05-02 21:41:52 +00003956 CmdArgs.push_back("-m32");
Bill Schmidt778d3872013-07-26 01:36:11 +00003957 else if (Arch == llvm::Triple::x86_64 || Arch == llvm::Triple::ppc64 ||
3958 Arch == llvm::Triple::ppc64le)
Daniel Dunbar5716d872009-05-02 21:41:52 +00003959 CmdArgs.push_back("-m64");
3960
Daniel Dunbarb440f562010-08-02 02:38:21 +00003961 if (Output.isFilename()) {
Daniel Dunbara3246a02009-03-18 08:07:30 +00003962 CmdArgs.push_back("-o");
Daniel Dunbar5cdf3e02009-03-19 07:29:38 +00003963 CmdArgs.push_back(Output.getFilename());
3964 } else {
3965 assert(Output.isNothing() && "Unexpected output");
Daniel Dunbara3246a02009-03-18 08:07:30 +00003966 CmdArgs.push_back("-fsyntax-only");
Daniel Dunbar5cdf3e02009-03-19 07:29:38 +00003967 }
Daniel Dunbara3246a02009-03-18 08:07:30 +00003968
Tony Linthicum76329bf2011-12-12 21:14:55 +00003969 Args.AddAllArgValues(CmdArgs, options::OPT_Wa_COMMA,
3970 options::OPT_Xassembler);
Daniel Dunbara3246a02009-03-18 08:07:30 +00003971
3972 // Only pass -x if gcc will understand it; otherwise hope gcc
3973 // understands the suffix correctly. The main use case this would go
3974 // wrong in is for linker inputs if they happened to have an odd
3975 // suffix; really the only way to get this to happen is a command
3976 // like '-x foobar a.c' which will treat a.c like a linker input.
3977 //
3978 // FIXME: For the linker case specifically, can we safely convert
3979 // inputs into '-Wl,' options?
3980 for (InputInfoList::const_iterator
3981 it = Inputs.begin(), ie = Inputs.end(); it != ie; ++it) {
3982 const InputInfo &II = *it;
Daniel Dunbare3e263f2009-05-02 20:14:53 +00003983
Daniel Dunbar6cdf83c2009-09-01 16:57:46 +00003984 // Don't try to pass LLVM or AST inputs to a generic gcc.
Daniel Dunbar24e52992010-06-07 23:28:45 +00003985 if (II.getType() == types::TY_LLVM_IR || II.getType() == types::TY_LTO_IR ||
3986 II.getType() == types::TY_LLVM_BC || II.getType() == types::TY_LTO_BC)
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003987 D.Diag(diag::err_drv_no_linker_llvm_support)
Daniel Dunbar1c28f1e2009-09-09 22:32:48 +00003988 << getToolChain().getTripleString();
Daniel Dunbar6cdf83c2009-09-01 16:57:46 +00003989 else if (II.getType() == types::TY_AST)
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003990 D.Diag(diag::err_drv_no_ast_support)
Daniel Dunbar1c28f1e2009-09-09 22:32:48 +00003991 << getToolChain().getTripleString();
Douglas Gregorbf7fc9c2013-03-27 16:47:18 +00003992 else if (II.getType() == types::TY_ModuleFile)
3993 D.Diag(diag::err_drv_no_module_support)
3994 << getToolChain().getTripleString();
Daniel Dunbare3e263f2009-05-02 20:14:53 +00003995
Daniel Dunbara3246a02009-03-18 08:07:30 +00003996 if (types::canTypeBeUserSpecified(II.getType())) {
3997 CmdArgs.push_back("-x");
3998 CmdArgs.push_back(types::getTypeName(II.getType()));
3999 }
4000
Daniel Dunbarb440f562010-08-02 02:38:21 +00004001 if (II.isFilename())
Daniel Dunbar5cdf3e02009-03-19 07:29:38 +00004002 CmdArgs.push_back(II.getFilename());
Daniel Dunbarf2476752010-09-25 18:10:05 +00004003 else {
4004 const Arg &A = II.getInputArg();
4005
4006 // Reverse translate some rewritten options.
4007 if (A.getOption().matches(options::OPT_Z_reserved_lib_stdcxx)) {
4008 CmdArgs.push_back("-lstdc++");
4009 continue;
4010 }
4011
Daniel Dunbar5cdf3e02009-03-19 07:29:38 +00004012 // Don't render as input, we need gcc to do the translations.
Daniel Dunbarf2476752010-09-25 18:10:05 +00004013 A.render(Args, CmdArgs);
4014 }
Daniel Dunbara3246a02009-03-18 08:07:30 +00004015 }
4016
Dylan Noblesmith70e73a32011-04-09 13:31:59 +00004017 const std::string customGCCName = D.getCCCGenericGCCName();
4018 const char *GCCName;
4019 if (!customGCCName.empty())
4020 GCCName = customGCCName.c_str();
Hans Wennborg70850d82013-07-18 20:29:38 +00004021 else if (D.CCCIsCXX()) {
Dylan Noblesmith70e73a32011-04-09 13:31:59 +00004022 GCCName = "g++";
Dylan Noblesmith70e73a32011-04-09 13:31:59 +00004023 } else
4024 GCCName = "gcc";
4025
Daniel Dunbard067f7f2009-04-08 23:54:23 +00004026 const char *Exec =
Daniel Dunbar9c3ed5f2010-07-14 18:46:23 +00004027 Args.MakeArgString(getToolChain().GetProgramPath(GCCName));
Daniel Dunbar7fbaf532010-08-02 02:38:28 +00004028 C.addCommand(new Command(JA, *this, Exec, CmdArgs));
Daniel Dunbar1a093d22009-03-18 06:00:36 +00004029}
4030
Daniel Dunbar4e295052010-01-25 22:35:08 +00004031void gcc::Preprocess::RenderExtraToolArgs(const JobAction &JA,
4032 ArgStringList &CmdArgs) const {
Daniel Dunbara3246a02009-03-18 08:07:30 +00004033 CmdArgs.push_back("-E");
Daniel Dunbar1a093d22009-03-18 06:00:36 +00004034}
4035
Daniel Dunbar4e295052010-01-25 22:35:08 +00004036void gcc::Precompile::RenderExtraToolArgs(const JobAction &JA,
4037 ArgStringList &CmdArgs) const {
Daniel Dunbara3246a02009-03-18 08:07:30 +00004038 // The type is good enough.
Daniel Dunbar1a093d22009-03-18 06:00:36 +00004039}
4040
Daniel Dunbar4e295052010-01-25 22:35:08 +00004041void gcc::Compile::RenderExtraToolArgs(const JobAction &JA,
4042 ArgStringList &CmdArgs) const {
Daniel Dunbaraeea8ac2010-02-11 03:16:21 +00004043 const Driver &D = getToolChain().getDriver();
4044
Daniel Dunbar4e295052010-01-25 22:35:08 +00004045 // If -flto, etc. are present then make sure not to force assembly output.
Daniel Dunbar24e52992010-06-07 23:28:45 +00004046 if (JA.getType() == types::TY_LLVM_IR || JA.getType() == types::TY_LTO_IR ||
4047 JA.getType() == types::TY_LLVM_BC || JA.getType() == types::TY_LTO_BC)
Daniel Dunbar4e295052010-01-25 22:35:08 +00004048 CmdArgs.push_back("-c");
Daniel Dunbaraeea8ac2010-02-11 03:16:21 +00004049 else {
4050 if (JA.getType() != types::TY_PP_Asm)
Chris Lattner0e62c1c2011-07-23 10:55:15 +00004051 D.Diag(diag::err_drv_invalid_gcc_output_type)
Daniel Dunbaraeea8ac2010-02-11 03:16:21 +00004052 << getTypeName(JA.getType());
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00004053
Daniel Dunbar4e295052010-01-25 22:35:08 +00004054 CmdArgs.push_back("-S");
Daniel Dunbaraeea8ac2010-02-11 03:16:21 +00004055 }
Daniel Dunbar1a093d22009-03-18 06:00:36 +00004056}
4057
Daniel Dunbar4e295052010-01-25 22:35:08 +00004058void gcc::Assemble::RenderExtraToolArgs(const JobAction &JA,
4059 ArgStringList &CmdArgs) const {
Daniel Dunbara3246a02009-03-18 08:07:30 +00004060 CmdArgs.push_back("-c");
Daniel Dunbar1a093d22009-03-18 06:00:36 +00004061}
Daniel Dunbara3246a02009-03-18 08:07:30 +00004062
Daniel Dunbar4e295052010-01-25 22:35:08 +00004063void gcc::Link::RenderExtraToolArgs(const JobAction &JA,
4064 ArgStringList &CmdArgs) const {
Daniel Dunbara3246a02009-03-18 08:07:30 +00004065 // The types are (hopefully) good enough.
4066}
4067
Tony Linthicum76329bf2011-12-12 21:14:55 +00004068// Hexagon tools start.
4069void hexagon::Assemble::RenderExtraToolArgs(const JobAction &JA,
4070 ArgStringList &CmdArgs) const {
4071
4072}
4073void hexagon::Assemble::ConstructJob(Compilation &C, const JobAction &JA,
4074 const InputInfo &Output,
4075 const InputInfoList &Inputs,
4076 const ArgList &Args,
4077 const char *LinkingOutput) const {
4078
4079 const Driver &D = getToolChain().getDriver();
4080 ArgStringList CmdArgs;
4081
4082 std::string MarchString = "-march=";
Matthew Curtisf10a5952012-12-06 14:16:43 +00004083 MarchString += toolchains::Hexagon_TC::GetTargetCPU(Args);
Tony Linthicum76329bf2011-12-12 21:14:55 +00004084 CmdArgs.push_back(Args.MakeArgString(MarchString));
4085
4086 RenderExtraToolArgs(JA, CmdArgs);
4087
4088 if (Output.isFilename()) {
4089 CmdArgs.push_back("-o");
4090 CmdArgs.push_back(Output.getFilename());
4091 } else {
4092 assert(Output.isNothing() && "Unexpected output");
4093 CmdArgs.push_back("-fsyntax-only");
4094 }
4095
Matthew Curtise8f80a12012-12-06 17:49:03 +00004096 std::string SmallDataThreshold = GetHexagonSmallDataThresholdValue(Args);
4097 if (!SmallDataThreshold.empty())
4098 CmdArgs.push_back(
4099 Args.MakeArgString(std::string("-G") + SmallDataThreshold));
Tony Linthicum76329bf2011-12-12 21:14:55 +00004100
Matthew Curtise5df3812012-12-07 17:23:04 +00004101 Args.AddAllArgValues(CmdArgs, options::OPT_Wa_COMMA,
4102 options::OPT_Xassembler);
4103
Tony Linthicum76329bf2011-12-12 21:14:55 +00004104 // Only pass -x if gcc will understand it; otherwise hope gcc
4105 // understands the suffix correctly. The main use case this would go
4106 // wrong in is for linker inputs if they happened to have an odd
4107 // suffix; really the only way to get this to happen is a command
4108 // like '-x foobar a.c' which will treat a.c like a linker input.
4109 //
4110 // FIXME: For the linker case specifically, can we safely convert
4111 // inputs into '-Wl,' options?
4112 for (InputInfoList::const_iterator
4113 it = Inputs.begin(), ie = Inputs.end(); it != ie; ++it) {
4114 const InputInfo &II = *it;
4115
4116 // Don't try to pass LLVM or AST inputs to a generic gcc.
4117 if (II.getType() == types::TY_LLVM_IR || II.getType() == types::TY_LTO_IR ||
4118 II.getType() == types::TY_LLVM_BC || II.getType() == types::TY_LTO_BC)
4119 D.Diag(clang::diag::err_drv_no_linker_llvm_support)
4120 << getToolChain().getTripleString();
4121 else if (II.getType() == types::TY_AST)
4122 D.Diag(clang::diag::err_drv_no_ast_support)
4123 << getToolChain().getTripleString();
Douglas Gregorbf7fc9c2013-03-27 16:47:18 +00004124 else if (II.getType() == types::TY_ModuleFile)
4125 D.Diag(diag::err_drv_no_module_support)
4126 << getToolChain().getTripleString();
Tony Linthicum76329bf2011-12-12 21:14:55 +00004127
4128 if (II.isFilename())
4129 CmdArgs.push_back(II.getFilename());
4130 else
4131 // Don't render as input, we need gcc to do the translations. FIXME: Pranav: What is this ?
4132 II.getInputArg().render(Args, CmdArgs);
4133 }
4134
4135 const char *GCCName = "hexagon-as";
4136 const char *Exec =
4137 Args.MakeArgString(getToolChain().GetProgramPath(GCCName));
4138 C.addCommand(new Command(JA, *this, Exec, CmdArgs));
4139
4140}
4141void hexagon::Link::RenderExtraToolArgs(const JobAction &JA,
4142 ArgStringList &CmdArgs) const {
4143 // The types are (hopefully) good enough.
4144}
4145
4146void hexagon::Link::ConstructJob(Compilation &C, const JobAction &JA,
4147 const InputInfo &Output,
4148 const InputInfoList &Inputs,
4149 const ArgList &Args,
4150 const char *LinkingOutput) const {
4151
Matthew Curtise689b052012-12-06 15:46:07 +00004152 const toolchains::Hexagon_TC& ToolChain =
4153 static_cast<const toolchains::Hexagon_TC&>(getToolChain());
4154 const Driver &D = ToolChain.getDriver();
4155
Tony Linthicum76329bf2011-12-12 21:14:55 +00004156 ArgStringList CmdArgs;
4157
Matthew Curtise689b052012-12-06 15:46:07 +00004158 //----------------------------------------------------------------------------
4159 //
4160 //----------------------------------------------------------------------------
4161 bool hasStaticArg = Args.hasArg(options::OPT_static);
4162 bool buildingLib = Args.hasArg(options::OPT_shared);
Matthew Curtise8f80a12012-12-06 17:49:03 +00004163 bool buildPIE = Args.hasArg(options::OPT_pie);
Matthew Curtise689b052012-12-06 15:46:07 +00004164 bool incStdLib = !Args.hasArg(options::OPT_nostdlib);
4165 bool incStartFiles = !Args.hasArg(options::OPT_nostartfiles);
4166 bool incDefLibs = !Args.hasArg(options::OPT_nodefaultlibs);
4167 bool useShared = buildingLib && !hasStaticArg;
Tony Linthicum76329bf2011-12-12 21:14:55 +00004168
Matthew Curtise689b052012-12-06 15:46:07 +00004169 //----------------------------------------------------------------------------
4170 // Silence warnings for various options
4171 //----------------------------------------------------------------------------
Tony Linthicum76329bf2011-12-12 21:14:55 +00004172
Matthew Curtise689b052012-12-06 15:46:07 +00004173 Args.ClaimAllArgs(options::OPT_g_Group);
4174 Args.ClaimAllArgs(options::OPT_emit_llvm);
4175 Args.ClaimAllArgs(options::OPT_w); // Other warning options are already
4176 // handled somewhere else.
4177 Args.ClaimAllArgs(options::OPT_static_libgcc);
4178
4179 //----------------------------------------------------------------------------
4180 //
4181 //----------------------------------------------------------------------------
4182 for (std::vector<std::string>::const_iterator i = ToolChain.ExtraOpts.begin(),
4183 e = ToolChain.ExtraOpts.end();
4184 i != e; ++i)
4185 CmdArgs.push_back(i->c_str());
Tony Linthicum76329bf2011-12-12 21:14:55 +00004186
Matthew Curtisf10a5952012-12-06 14:16:43 +00004187 std::string MarchString = toolchains::Hexagon_TC::GetTargetCPU(Args);
4188 CmdArgs.push_back(Args.MakeArgString("-m" + MarchString));
Sebastian Pop86500282012-01-13 20:37:10 +00004189
Matthew Curtise689b052012-12-06 15:46:07 +00004190 if (buildingLib) {
4191 CmdArgs.push_back("-shared");
4192 CmdArgs.push_back("-call_shared"); // should be the default, but doing as
4193 // hexagon-gcc does
Tony Linthicum76329bf2011-12-12 21:14:55 +00004194 }
4195
Matthew Curtise689b052012-12-06 15:46:07 +00004196 if (hasStaticArg)
4197 CmdArgs.push_back("-static");
Tony Linthicum76329bf2011-12-12 21:14:55 +00004198
Matthew Curtise8f80a12012-12-06 17:49:03 +00004199 if (buildPIE && !buildingLib)
4200 CmdArgs.push_back("-pie");
4201
4202 std::string SmallDataThreshold = GetHexagonSmallDataThresholdValue(Args);
4203 if (!SmallDataThreshold.empty()) {
4204 CmdArgs.push_back(
4205 Args.MakeArgString(std::string("-G") + SmallDataThreshold));
4206 }
4207
Matthew Curtise689b052012-12-06 15:46:07 +00004208 //----------------------------------------------------------------------------
4209 //
4210 //----------------------------------------------------------------------------
4211 CmdArgs.push_back("-o");
4212 CmdArgs.push_back(Output.getFilename());
Tony Linthicum76329bf2011-12-12 21:14:55 +00004213
Matthew Curtise689b052012-12-06 15:46:07 +00004214 const std::string MarchSuffix = "/" + MarchString;
4215 const std::string G0Suffix = "/G0";
4216 const std::string MarchG0Suffix = MarchSuffix + G0Suffix;
4217 const std::string RootDir = toolchains::Hexagon_TC::GetGnuDir(D.InstalledDir)
4218 + "/";
4219 const std::string StartFilesDir = RootDir
4220 + "hexagon/lib"
4221 + (buildingLib
4222 ? MarchG0Suffix : MarchSuffix);
4223
4224 //----------------------------------------------------------------------------
4225 // moslib
4226 //----------------------------------------------------------------------------
4227 std::vector<std::string> oslibs;
4228 bool hasStandalone= false;
4229
4230 for (arg_iterator it = Args.filtered_begin(options::OPT_moslib_EQ),
4231 ie = Args.filtered_end(); it != ie; ++it) {
4232 (*it)->claim();
4233 oslibs.push_back((*it)->getValue());
4234 hasStandalone = hasStandalone || (oslibs.back() == "standalone");
Tony Linthicum76329bf2011-12-12 21:14:55 +00004235 }
Matthew Curtise689b052012-12-06 15:46:07 +00004236 if (oslibs.empty()) {
4237 oslibs.push_back("standalone");
4238 hasStandalone = true;
4239 }
Tony Linthicum76329bf2011-12-12 21:14:55 +00004240
Matthew Curtise689b052012-12-06 15:46:07 +00004241 //----------------------------------------------------------------------------
4242 // Start Files
4243 //----------------------------------------------------------------------------
4244 if (incStdLib && incStartFiles) {
4245
4246 if (!buildingLib) {
4247 if (hasStandalone) {
4248 CmdArgs.push_back(
4249 Args.MakeArgString(StartFilesDir + "/crt0_standalone.o"));
4250 }
4251 CmdArgs.push_back(Args.MakeArgString(StartFilesDir + "/crt0.o"));
4252 }
4253 std::string initObj = useShared ? "/initS.o" : "/init.o";
4254 CmdArgs.push_back(Args.MakeArgString(StartFilesDir + initObj));
4255 }
4256
4257 //----------------------------------------------------------------------------
4258 // Library Search Paths
4259 //----------------------------------------------------------------------------
4260 const ToolChain::path_list &LibPaths = ToolChain.getFilePaths();
4261 for (ToolChain::path_list::const_iterator
4262 i = LibPaths.begin(),
4263 e = LibPaths.end();
4264 i != e;
4265 ++i)
4266 CmdArgs.push_back(Args.MakeArgString(StringRef("-L") + *i));
4267
4268 //----------------------------------------------------------------------------
4269 //
4270 //----------------------------------------------------------------------------
4271 Args.AddAllArgs(CmdArgs, options::OPT_T_Group);
4272 Args.AddAllArgs(CmdArgs, options::OPT_e);
4273 Args.AddAllArgs(CmdArgs, options::OPT_s);
4274 Args.AddAllArgs(CmdArgs, options::OPT_t);
4275 Args.AddAllArgs(CmdArgs, options::OPT_u_Group);
4276
4277 AddLinkerInputs(ToolChain, Inputs, Args, CmdArgs);
4278
4279 //----------------------------------------------------------------------------
4280 // Libraries
4281 //----------------------------------------------------------------------------
4282 if (incStdLib && incDefLibs) {
Hans Wennborg70850d82013-07-18 20:29:38 +00004283 if (D.CCCIsCXX()) {
Matthew Curtise689b052012-12-06 15:46:07 +00004284 ToolChain.AddCXXStdlibLibArgs(Args, CmdArgs);
4285 CmdArgs.push_back("-lm");
4286 }
4287
4288 CmdArgs.push_back("--start-group");
4289
4290 if (!buildingLib) {
4291 for(std::vector<std::string>::iterator i = oslibs.begin(),
4292 e = oslibs.end(); i != e; ++i)
4293 CmdArgs.push_back(Args.MakeArgString("-l" + *i));
4294 CmdArgs.push_back("-lc");
4295 }
4296 CmdArgs.push_back("-lgcc");
4297
4298 CmdArgs.push_back("--end-group");
4299 }
4300
4301 //----------------------------------------------------------------------------
4302 // End files
4303 //----------------------------------------------------------------------------
4304 if (incStdLib && incStartFiles) {
4305 std::string finiObj = useShared ? "/finiS.o" : "/fini.o";
4306 CmdArgs.push_back(Args.MakeArgString(StartFilesDir + finiObj));
4307 }
4308
4309 std::string Linker = ToolChain.GetProgramPath("hexagon-ld");
4310 C.addCommand(
4311 new Command(
4312 JA, *this,
4313 Args.MakeArgString(Linker), CmdArgs));
Tony Linthicum76329bf2011-12-12 21:14:55 +00004314}
4315// Hexagon tools end.
4316
Rafael Espindoladcbf6982012-10-31 18:51:07 +00004317llvm::Triple::ArchType darwin::getArchTypeForDarwinArchName(StringRef Str) {
4318 // See arch(3) and llvm-gcc's driver-driver.c. We don't implement support for
4319 // archs which Darwin doesn't use.
4320
4321 // The matching this routine does is fairly pointless, since it is neither the
4322 // complete architecture list, nor a reasonable subset. The problem is that
4323 // historically the driver driver accepts this and also ties its -march=
4324 // handling to the architecture name, so we need to be careful before removing
4325 // support for it.
4326
4327 // This code must be kept in sync with Clang's Darwin specific argument
4328 // translation.
4329
4330 return llvm::StringSwitch<llvm::Triple::ArchType>(Str)
4331 .Cases("ppc", "ppc601", "ppc603", "ppc604", "ppc604e", llvm::Triple::ppc)
4332 .Cases("ppc750", "ppc7400", "ppc7450", "ppc970", llvm::Triple::ppc)
4333 .Case("ppc64", llvm::Triple::ppc64)
4334 .Cases("i386", "i486", "i486SX", "i586", "i686", llvm::Triple::x86)
4335 .Cases("pentium", "pentpro", "pentIIm3", "pentIIm5", "pentium4",
4336 llvm::Triple::x86)
4337 .Case("x86_64", llvm::Triple::x86_64)
4338 // This is derived from the driver driver.
Bob Wilson743bf672013-03-04 22:37:49 +00004339 .Cases("arm", "armv4t", "armv5", "armv6", "armv6m", llvm::Triple::arm)
4340 .Cases("armv7", "armv7em", "armv7f", "armv7k", "armv7m", llvm::Triple::arm)
4341 .Cases("armv7s", "xscale", llvm::Triple::arm)
Rafael Espindoladcbf6982012-10-31 18:51:07 +00004342 .Case("r600", llvm::Triple::r600)
4343 .Case("nvptx", llvm::Triple::nvptx)
4344 .Case("nvptx64", llvm::Triple::nvptx64)
4345 .Case("amdil", llvm::Triple::amdil)
4346 .Case("spir", llvm::Triple::spir)
4347 .Default(llvm::Triple::UnknownArch);
4348}
Tony Linthicum76329bf2011-12-12 21:14:55 +00004349
Bob Wilsondecc03e2012-11-23 06:14:39 +00004350const char *Clang::getBaseInputName(const ArgList &Args,
4351 const InputInfoList &Inputs) {
Michael J. Spencere1696752010-12-18 00:19:12 +00004352 return Args.MakeArgString(
4353 llvm::sys::path::filename(Inputs[0].getBaseInput()));
Daniel Dunbarafec1f52009-03-29 18:40:18 +00004354}
4355
Bob Wilsondecc03e2012-11-23 06:14:39 +00004356const char *Clang::getBaseInputStem(const ArgList &Args,
4357 const InputInfoList &Inputs) {
Daniel Dunbarafec1f52009-03-29 18:40:18 +00004358 const char *Str = getBaseInputName(Args, Inputs);
4359
Chris Lattner906bb902011-01-16 08:14:11 +00004360 if (const char *End = strrchr(Str, '.'))
Daniel Dunbar1c28f1e2009-09-09 22:32:48 +00004361 return Args.MakeArgString(std::string(Str, End));
Daniel Dunbarafec1f52009-03-29 18:40:18 +00004362
4363 return Str;
4364}
4365
Bob Wilsondecc03e2012-11-23 06:14:39 +00004366const char *Clang::getDependencyFileName(const ArgList &Args,
4367 const InputInfoList &Inputs) {
Daniel Dunbarafec1f52009-03-29 18:40:18 +00004368 // FIXME: Think about this more.
4369 std::string Res;
4370
4371 if (Arg *OutputOpt = Args.getLastArg(options::OPT_o)) {
Richard Smithbd55daf2012-11-01 04:30:05 +00004372 std::string Str(OutputOpt->getValue());
Daniel Dunbarafec1f52009-03-29 18:40:18 +00004373 Res = Str.substr(0, Str.rfind('.'));
Chad Rosier6fdf38b2011-08-17 23:08:45 +00004374 } else {
Bob Wilsondecc03e2012-11-23 06:14:39 +00004375 Res = getBaseInputStem(Args, Inputs);
Chad Rosier6fdf38b2011-08-17 23:08:45 +00004376 }
Daniel Dunbar1c28f1e2009-09-09 22:32:48 +00004377 return Args.MakeArgString(Res + ".d");
Daniel Dunbarafec1f52009-03-29 18:40:18 +00004378}
4379
Daniel Dunbarbe220842009-03-20 16:06:39 +00004380void darwin::Assemble::ConstructJob(Compilation &C, const JobAction &JA,
Daniel Dunbar7fbaf532010-08-02 02:38:28 +00004381 const InputInfo &Output,
Daniel Dunbard067f7f2009-04-08 23:54:23 +00004382 const InputInfoList &Inputs,
4383 const ArgList &Args,
Daniel Dunbarbe220842009-03-20 16:06:39 +00004384 const char *LinkingOutput) const {
4385 ArgStringList CmdArgs;
4386
4387 assert(Inputs.size() == 1 && "Unexpected number of inputs.");
4388 const InputInfo &Input = Inputs[0];
4389
Daniel Dunbardc8355e2011-04-12 23:59:20 +00004390 // Determine the original source input.
4391 const Action *SourceAction = &JA;
4392 while (SourceAction->getKind() != Action::InputClass) {
4393 assert(!SourceAction->getInputs().empty() && "unexpected root action!");
4394 SourceAction = SourceAction->getInputs()[0];
4395 }
4396
4397 // Forward -g, assuming we are dealing with an actual assembly file.
Eric Christopher84fbdb42011-08-19 00:30:14 +00004398 if (SourceAction->getType() == types::TY_Asm ||
Daniel Dunbardc8355e2011-04-12 23:59:20 +00004399 SourceAction->getType() == types::TY_PP_Asm) {
Daniel Dunbar5c9c1182009-04-01 00:27:44 +00004400 if (Args.hasArg(options::OPT_gstabs))
4401 CmdArgs.push_back("--gstabs");
4402 else if (Args.hasArg(options::OPT_g_Group))
Bob Wilson126c4912011-11-02 05:10:45 +00004403 CmdArgs.push_back("-g");
Daniel Dunbar5c9c1182009-04-01 00:27:44 +00004404 }
Daniel Dunbard067f7f2009-04-08 23:54:23 +00004405
Daniel Dunbarbe220842009-03-20 16:06:39 +00004406 // Derived from asm spec.
Daniel Dunbar3571dd92009-09-09 18:36:27 +00004407 AddDarwinArch(Args, CmdArgs);
Daniel Dunbarbe220842009-03-20 16:06:39 +00004408
Daniel Dunbar6d484762010-07-22 01:47:22 +00004409 // Use -force_cpusubtype_ALL on x86 by default.
Eli Benderskyd4a6aec2013-07-24 22:20:49 +00004410 if (getToolChain().getArch() == llvm::Triple::x86 ||
4411 getToolChain().getArch() == llvm::Triple::x86_64 ||
Daniel Dunbar3571dd92009-09-09 18:36:27 +00004412 Args.hasArg(options::OPT_force__cpusubtype__ALL))
4413 CmdArgs.push_back("-force_cpusubtype_ALL");
4414
Eli Benderskyd4a6aec2013-07-24 22:20:49 +00004415 if (getToolChain().getArch() != llvm::Triple::x86_64 &&
Daniel Dunbarbd847cc2012-10-15 22:23:53 +00004416 (((Args.hasArg(options::OPT_mkernel) ||
Eric Christopher248357f2013-02-21 22:35:01 +00004417 Args.hasArg(options::OPT_fapple_kext)) &&
Daniel Dunbarbd847cc2012-10-15 22:23:53 +00004418 (!getDarwinToolChain().isTargetIPhoneOS() ||
4419 getDarwinToolChain().isIPhoneOSVersionLT(6, 0))) ||
4420 Args.hasArg(options::OPT_static)))
Daniel Dunbard067f7f2009-04-08 23:54:23 +00004421 CmdArgs.push_back("-static");
4422
Daniel Dunbarbe220842009-03-20 16:06:39 +00004423 Args.AddAllArgValues(CmdArgs, options::OPT_Wa_COMMA,
4424 options::OPT_Xassembler);
4425
4426 assert(Output.isFilename() && "Unexpected lipo output.");
4427 CmdArgs.push_back("-o");
4428 CmdArgs.push_back(Output.getFilename());
4429
Daniel Dunbarb440f562010-08-02 02:38:21 +00004430 assert(Input.isFilename() && "Invalid input.");
4431 CmdArgs.push_back(Input.getFilename());
Daniel Dunbarbe220842009-03-20 16:06:39 +00004432
4433 // asm_final spec is empty.
4434
Daniel Dunbard067f7f2009-04-08 23:54:23 +00004435 const char *Exec =
Daniel Dunbar9c3ed5f2010-07-14 18:46:23 +00004436 Args.MakeArgString(getToolChain().GetProgramPath("as"));
Daniel Dunbar7fbaf532010-08-02 02:38:28 +00004437 C.addCommand(new Command(JA, *this, Exec, CmdArgs));
Daniel Dunbarbe220842009-03-20 16:06:39 +00004438}
Daniel Dunbar64ed5e32009-03-20 00:52:38 +00004439
David Blaikie68e081d2011-12-20 02:48:34 +00004440void darwin::DarwinTool::anchor() {}
4441
Daniel Dunbare9ded432009-09-09 18:36:20 +00004442void darwin::DarwinTool::AddDarwinArch(const ArgList &Args,
4443 ArgStringList &CmdArgs) const {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00004444 StringRef ArchName = getDarwinToolChain().getDarwinArchName(Args);
Daniel Dunbardcc3b652010-01-22 02:04:58 +00004445
Daniel Dunbarc1964212009-03-26 16:23:12 +00004446 // Derived from darwin_arch spec.
4447 CmdArgs.push_back("-arch");
Daniel Dunbardcc3b652010-01-22 02:04:58 +00004448 CmdArgs.push_back(Args.MakeArgString(ArchName));
Daniel Dunbar91dbfd62009-09-04 18:35:31 +00004449
Daniel Dunbardcc3b652010-01-22 02:04:58 +00004450 // FIXME: Is this needed anymore?
4451 if (ArchName == "arm")
Daniel Dunbar91dbfd62009-09-04 18:35:31 +00004452 CmdArgs.push_back("-force_cpusubtype_ALL");
Daniel Dunbarc1964212009-03-26 16:23:12 +00004453}
4454
Bill Wendling3b2000f2012-10-02 18:02:50 +00004455bool darwin::Link::NeedsTempPath(const InputInfoList &Inputs) const {
4456 // We only need to generate a temp path for LTO if we aren't compiling object
4457 // files. When compiling source files, we run 'dsymutil' after linking. We
4458 // don't run 'dsymutil' when compiling object files.
4459 for (InputInfoList::const_iterator
4460 it = Inputs.begin(), ie = Inputs.end(); it != ie; ++it)
4461 if (it->getType() != types::TY_Object)
4462 return true;
4463
4464 return false;
4465}
4466
Daniel Dunbarccbc4522010-09-09 21:51:05 +00004467void darwin::Link::AddLinkArgs(Compilation &C,
4468 const ArgList &Args,
Bill Wendling3b2000f2012-10-02 18:02:50 +00004469 ArgStringList &CmdArgs,
4470 const InputInfoList &Inputs) const {
Daniel Dunbar083edf72009-12-21 18:54:17 +00004471 const Driver &D = getToolChain().getDriver();
Daniel Dunbarc44d3132011-04-28 21:23:41 +00004472 const toolchains::Darwin &DarwinTC = getDarwinToolChain();
Daniel Dunbarc1964212009-03-26 16:23:12 +00004473
Daniel Dunbarcacb0e22010-08-11 23:07:50 +00004474 unsigned Version[3] = { 0, 0, 0 };
4475 if (Arg *A = Args.getLastArg(options::OPT_mlinker_version_EQ)) {
4476 bool HadExtra;
Richard Smithbd55daf2012-11-01 04:30:05 +00004477 if (!Driver::GetReleaseVersion(A->getValue(), Version[0],
Daniel Dunbarcacb0e22010-08-11 23:07:50 +00004478 Version[1], Version[2], HadExtra) ||
4479 HadExtra)
Chris Lattner0e62c1c2011-07-23 10:55:15 +00004480 D.Diag(diag::err_drv_invalid_version_number)
Daniel Dunbarcacb0e22010-08-11 23:07:50 +00004481 << A->getAsString(Args);
4482 }
4483
4484 // Newer linkers support -demangle, pass it if supported and not disabled by
4485 // the user.
Daniel Dunbarecb41cb2012-01-04 21:45:27 +00004486 if (Version[0] >= 100 && !Args.hasArg(options::OPT_Z_Xlinker__no_demangle)) {
Daniel Dunbar1eaf5c62010-09-07 17:07:49 +00004487 // Don't pass -demangle to ld_classic.
4488 //
4489 // FIXME: This is a temporary workaround, ld should be handling this.
4490 bool UsesLdClassic = (getToolChain().getArch() == llvm::Triple::x86 &&
4491 Args.hasArg(options::OPT_static));
Daniel Dunbar3d7e0e22010-09-07 17:50:41 +00004492 if (getToolChain().getArch() == llvm::Triple::x86) {
4493 for (arg_iterator it = Args.filtered_begin(options::OPT_Xlinker,
4494 options::OPT_Wl_COMMA),
4495 ie = Args.filtered_end(); it != ie; ++it) {
4496 const Arg *A = *it;
4497 for (unsigned i = 0, e = A->getNumValues(); i != e; ++i)
Richard Smithbd55daf2012-11-01 04:30:05 +00004498 if (StringRef(A->getValue(i)) == "-kext")
Daniel Dunbar3d7e0e22010-09-07 17:50:41 +00004499 UsesLdClassic = true;
4500 }
4501 }
Daniel Dunbar1eaf5c62010-09-07 17:07:49 +00004502 if (!UsesLdClassic)
4503 CmdArgs.push_back("-demangle");
Daniel Dunbarcacb0e22010-08-11 23:07:50 +00004504 }
4505
Bob Wilson3d27dad2013-08-02 22:25:34 +00004506 if (Args.hasArg(options::OPT_rdynamic) && Version[0] >= 137)
4507 CmdArgs.push_back("-export_dynamic");
4508
Bill Wendling313b6bf2012-11-16 23:03:00 +00004509 // If we are using LTO, then automatically create a temporary file path for
4510 // the linker to use, so that it's lifetime will extend past a possible
4511 // dsymutil step.
4512 if (Version[0] >= 116 && D.IsUsingLTO(Args) && NeedsTempPath(Inputs)) {
4513 const char *TmpPath = C.getArgs().MakeArgString(
4514 D.GetTemporaryPath("cc", types::getTypeTempSuffix(types::TY_Object)));
4515 C.addTempFile(TmpPath);
4516 CmdArgs.push_back("-object_path_lto");
4517 CmdArgs.push_back(TmpPath);
Daniel Dunbaref889c72011-06-21 20:55:11 +00004518 }
4519
Daniel Dunbarc1964212009-03-26 16:23:12 +00004520 // Derived from the "link" spec.
4521 Args.AddAllArgs(CmdArgs, options::OPT_static);
4522 if (!Args.hasArg(options::OPT_static))
4523 CmdArgs.push_back("-dynamic");
4524 if (Args.hasArg(options::OPT_fgnu_runtime)) {
4525 // FIXME: gcc replaces -lobjc in forward args with -lobjc-gnu
4526 // here. How do we wish to handle such things?
4527 }
Daniel Dunbard067f7f2009-04-08 23:54:23 +00004528
Daniel Dunbarc1964212009-03-26 16:23:12 +00004529 if (!Args.hasArg(options::OPT_dynamiclib)) {
Daniel Dunbara48823f2010-01-22 02:04:52 +00004530 AddDarwinArch(Args, CmdArgs);
Daniel Dunbara48823f2010-01-22 02:04:52 +00004531 // FIXME: Why do this only on this path?
Daniel Dunbar93d7acf2010-01-22 03:37:33 +00004532 Args.AddLastArg(CmdArgs, options::OPT_force__cpusubtype__ALL);
Daniel Dunbarc1964212009-03-26 16:23:12 +00004533
4534 Args.AddLastArg(CmdArgs, options::OPT_bundle);
4535 Args.AddAllArgs(CmdArgs, options::OPT_bundle__loader);
4536 Args.AddAllArgs(CmdArgs, options::OPT_client__name);
4537
4538 Arg *A;
4539 if ((A = Args.getLastArg(options::OPT_compatibility__version)) ||
4540 (A = Args.getLastArg(options::OPT_current__version)) ||
4541 (A = Args.getLastArg(options::OPT_install__name)))
Chris Lattner0e62c1c2011-07-23 10:55:15 +00004542 D.Diag(diag::err_drv_argument_only_allowed_with)
Daniel Dunbarc1964212009-03-26 16:23:12 +00004543 << A->getAsString(Args) << "-dynamiclib";
4544
4545 Args.AddLastArg(CmdArgs, options::OPT_force__flat__namespace);
4546 Args.AddLastArg(CmdArgs, options::OPT_keep__private__externs);
4547 Args.AddLastArg(CmdArgs, options::OPT_private__bundle);
4548 } else {
4549 CmdArgs.push_back("-dylib");
4550
4551 Arg *A;
4552 if ((A = Args.getLastArg(options::OPT_bundle)) ||
4553 (A = Args.getLastArg(options::OPT_bundle__loader)) ||
4554 (A = Args.getLastArg(options::OPT_client__name)) ||
4555 (A = Args.getLastArg(options::OPT_force__flat__namespace)) ||
4556 (A = Args.getLastArg(options::OPT_keep__private__externs)) ||
4557 (A = Args.getLastArg(options::OPT_private__bundle)))
Chris Lattner0e62c1c2011-07-23 10:55:15 +00004558 D.Diag(diag::err_drv_argument_not_allowed_with)
Daniel Dunbarc1964212009-03-26 16:23:12 +00004559 << A->getAsString(Args) << "-dynamiclib";
Daniel Dunbard067f7f2009-04-08 23:54:23 +00004560
Daniel Dunbarc1964212009-03-26 16:23:12 +00004561 Args.AddAllArgsTranslated(CmdArgs, options::OPT_compatibility__version,
4562 "-dylib_compatibility_version");
4563 Args.AddAllArgsTranslated(CmdArgs, options::OPT_current__version,
4564 "-dylib_current_version");
4565
Daniel Dunbara48823f2010-01-22 02:04:52 +00004566 AddDarwinArch(Args, CmdArgs);
Daniel Dunbarc1964212009-03-26 16:23:12 +00004567
4568 Args.AddAllArgsTranslated(CmdArgs, options::OPT_install__name,
4569 "-dylib_install_name");
4570 }
4571
4572 Args.AddLastArg(CmdArgs, options::OPT_all__load);
4573 Args.AddAllArgs(CmdArgs, options::OPT_allowable__client);
4574 Args.AddLastArg(CmdArgs, options::OPT_bind__at__load);
Daniel Dunbarc44d3132011-04-28 21:23:41 +00004575 if (DarwinTC.isTargetIPhoneOS())
Daniel Dunbar2b5f6812009-09-04 18:35:41 +00004576 Args.AddLastArg(CmdArgs, options::OPT_arch__errors__fatal);
Daniel Dunbarc1964212009-03-26 16:23:12 +00004577 Args.AddLastArg(CmdArgs, options::OPT_dead__strip);
4578 Args.AddLastArg(CmdArgs, options::OPT_no__dead__strip__inits__and__terms);
4579 Args.AddAllArgs(CmdArgs, options::OPT_dylib__file);
4580 Args.AddLastArg(CmdArgs, options::OPT_dynamic);
4581 Args.AddAllArgs(CmdArgs, options::OPT_exported__symbols__list);
4582 Args.AddLastArg(CmdArgs, options::OPT_flat__namespace);
Daniel Dunbar044a3902011-06-28 20:16:02 +00004583 Args.AddAllArgs(CmdArgs, options::OPT_force__load);
Daniel Dunbarc1964212009-03-26 16:23:12 +00004584 Args.AddAllArgs(CmdArgs, options::OPT_headerpad__max__install__names);
4585 Args.AddAllArgs(CmdArgs, options::OPT_image__base);
4586 Args.AddAllArgs(CmdArgs, options::OPT_init);
4587
Daniel Dunbarc44d3132011-04-28 21:23:41 +00004588 // Add the deployment target.
Benjamin Kramerddefa6d2012-03-10 20:55:36 +00004589 VersionTuple TargetVersion = DarwinTC.getTargetVersion();
Daniel Dunbar72ceb922011-04-30 04:22:58 +00004590
4591 // If we had an explicit -mios-simulator-version-min argument, honor that,
4592 // otherwise use the traditional deployment targets. We can't just check the
4593 // is-sim attribute because existing code follows this path, and the linker
4594 // may not handle the argument.
4595 //
4596 // FIXME: We may be able to remove this, once we can verify no one depends on
4597 // it.
4598 if (Args.hasArg(options::OPT_mios_simulator_version_min_EQ))
4599 CmdArgs.push_back("-ios_simulator_version_min");
4600 else if (DarwinTC.isTargetIPhoneOS())
4601 CmdArgs.push_back("-iphoneos_version_min");
4602 else
4603 CmdArgs.push_back("-macosx_version_min");
Benjamin Kramerddefa6d2012-03-10 20:55:36 +00004604 CmdArgs.push_back(Args.MakeArgString(TargetVersion.getAsString()));
Daniel Dunbarc44d3132011-04-28 21:23:41 +00004605
Daniel Dunbarc1964212009-03-26 16:23:12 +00004606 Args.AddLastArg(CmdArgs, options::OPT_nomultidefs);
4607 Args.AddLastArg(CmdArgs, options::OPT_multi__module);
4608 Args.AddLastArg(CmdArgs, options::OPT_single__module);
4609 Args.AddAllArgs(CmdArgs, options::OPT_multiply__defined);
4610 Args.AddAllArgs(CmdArgs, options::OPT_multiply__defined__unused);
Daniel Dunbard067f7f2009-04-08 23:54:23 +00004611
Daniel Dunbaraf68a882010-07-13 23:31:40 +00004612 if (const Arg *A = Args.getLastArg(options::OPT_fpie, options::OPT_fPIE,
4613 options::OPT_fno_pie,
4614 options::OPT_fno_PIE)) {
4615 if (A->getOption().matches(options::OPT_fpie) ||
4616 A->getOption().matches(options::OPT_fPIE))
4617 CmdArgs.push_back("-pie");
4618 else
4619 CmdArgs.push_back("-no_pie");
4620 }
Daniel Dunbarc1964212009-03-26 16:23:12 +00004621
4622 Args.AddLastArg(CmdArgs, options::OPT_prebind);
4623 Args.AddLastArg(CmdArgs, options::OPT_noprebind);
4624 Args.AddLastArg(CmdArgs, options::OPT_nofixprebinding);
4625 Args.AddLastArg(CmdArgs, options::OPT_prebind__all__twolevel__modules);
4626 Args.AddLastArg(CmdArgs, options::OPT_read__only__relocs);
4627 Args.AddAllArgs(CmdArgs, options::OPT_sectcreate);
4628 Args.AddAllArgs(CmdArgs, options::OPT_sectorder);
4629 Args.AddAllArgs(CmdArgs, options::OPT_seg1addr);
4630 Args.AddAllArgs(CmdArgs, options::OPT_segprot);
4631 Args.AddAllArgs(CmdArgs, options::OPT_segaddr);
4632 Args.AddAllArgs(CmdArgs, options::OPT_segs__read__only__addr);
4633 Args.AddAllArgs(CmdArgs, options::OPT_segs__read__write__addr);
4634 Args.AddAllArgs(CmdArgs, options::OPT_seg__addr__table);
4635 Args.AddAllArgs(CmdArgs, options::OPT_seg__addr__table__filename);
4636 Args.AddAllArgs(CmdArgs, options::OPT_sub__library);
4637 Args.AddAllArgs(CmdArgs, options::OPT_sub__umbrella);
Daniel Dunbar2b5f6812009-09-04 18:35:41 +00004638
Daniel Dunbar84384642011-05-02 21:03:47 +00004639 // Give --sysroot= preference, over the Apple specific behavior to also use
4640 // --isysroot as the syslibroot.
Sebastian Pop980920a2012-04-16 04:16:43 +00004641 StringRef sysroot = C.getSysRoot();
4642 if (sysroot != "") {
Daniel Dunbar84384642011-05-02 21:03:47 +00004643 CmdArgs.push_back("-syslibroot");
Sebastian Pop980920a2012-04-16 04:16:43 +00004644 CmdArgs.push_back(C.getArgs().MakeArgString(sysroot));
Daniel Dunbar84384642011-05-02 21:03:47 +00004645 } else if (const Arg *A = Args.getLastArg(options::OPT_isysroot)) {
4646 CmdArgs.push_back("-syslibroot");
Richard Smithbd55daf2012-11-01 04:30:05 +00004647 CmdArgs.push_back(A->getValue());
Daniel Dunbar2b5f6812009-09-04 18:35:41 +00004648 }
4649
Daniel Dunbarc1964212009-03-26 16:23:12 +00004650 Args.AddLastArg(CmdArgs, options::OPT_twolevel__namespace);
4651 Args.AddLastArg(CmdArgs, options::OPT_twolevel__namespace__hints);
4652 Args.AddAllArgs(CmdArgs, options::OPT_umbrella);
4653 Args.AddAllArgs(CmdArgs, options::OPT_undefined);
4654 Args.AddAllArgs(CmdArgs, options::OPT_unexported__symbols__list);
Daniel Dunbar2b5f6812009-09-04 18:35:41 +00004655 Args.AddAllArgs(CmdArgs, options::OPT_weak__reference__mismatches);
Daniel Dunbarc1964212009-03-26 16:23:12 +00004656 Args.AddLastArg(CmdArgs, options::OPT_X_Flag);
4657 Args.AddAllArgs(CmdArgs, options::OPT_y);
4658 Args.AddLastArg(CmdArgs, options::OPT_w);
4659 Args.AddAllArgs(CmdArgs, options::OPT_pagezero__size);
4660 Args.AddAllArgs(CmdArgs, options::OPT_segs__read__);
4661 Args.AddLastArg(CmdArgs, options::OPT_seglinkedit);
4662 Args.AddLastArg(CmdArgs, options::OPT_noseglinkedit);
4663 Args.AddAllArgs(CmdArgs, options::OPT_sectalign);
4664 Args.AddAllArgs(CmdArgs, options::OPT_sectobjectsymbols);
4665 Args.AddAllArgs(CmdArgs, options::OPT_segcreate);
4666 Args.AddLastArg(CmdArgs, options::OPT_whyload);
4667 Args.AddLastArg(CmdArgs, options::OPT_whatsloaded);
4668 Args.AddAllArgs(CmdArgs, options::OPT_dylinker__install__name);
4669 Args.AddLastArg(CmdArgs, options::OPT_dylinker);
4670 Args.AddLastArg(CmdArgs, options::OPT_Mach);
4671}
4672
4673void darwin::Link::ConstructJob(Compilation &C, const JobAction &JA,
Daniel Dunbar7fbaf532010-08-02 02:38:28 +00004674 const InputInfo &Output,
Daniel Dunbard067f7f2009-04-08 23:54:23 +00004675 const InputInfoList &Inputs,
4676 const ArgList &Args,
Daniel Dunbarc1964212009-03-26 16:23:12 +00004677 const char *LinkingOutput) const {
4678 assert(Output.getType() == types::TY_Image && "Invalid linker output type.");
Daniel Dunbarc09988d2009-09-08 16:39:16 +00004679
Daniel Dunbarc1964212009-03-26 16:23:12 +00004680 // The logic here is derived from gcc's behavior; most of which
4681 // comes from specs (starting with link_command). Consult gcc for
4682 // more information.
Daniel Dunbarc1964212009-03-26 16:23:12 +00004683 ArgStringList CmdArgs;
4684
Argyrios Kyrtzidis741fab12011-10-07 22:58:08 +00004685 /// Hack(tm) to ignore linking errors when we are doing ARC migration.
4686 if (Args.hasArg(options::OPT_ccc_arcmt_check,
4687 options::OPT_ccc_arcmt_migrate)) {
4688 for (ArgList::const_iterator I = Args.begin(), E = Args.end(); I != E; ++I)
4689 (*I)->claim();
4690 const char *Exec =
4691 Args.MakeArgString(getToolChain().GetProgramPath("touch"));
4692 CmdArgs.push_back(Output.getFilename());
4693 C.addCommand(new Command(JA, *this, Exec, CmdArgs));
4694 return;
4695 }
4696
Daniel Dunbarc1964212009-03-26 16:23:12 +00004697 // I'm not sure why this particular decomposition exists in gcc, but
4698 // we follow suite for ease of comparison.
Bill Wendling3b2000f2012-10-02 18:02:50 +00004699 AddLinkArgs(C, Args, CmdArgs, Inputs);
Daniel Dunbarc1964212009-03-26 16:23:12 +00004700
Daniel Dunbarc1964212009-03-26 16:23:12 +00004701 Args.AddAllArgs(CmdArgs, options::OPT_d_Flag);
4702 Args.AddAllArgs(CmdArgs, options::OPT_s);
4703 Args.AddAllArgs(CmdArgs, options::OPT_t);
4704 Args.AddAllArgs(CmdArgs, options::OPT_Z_Flag);
4705 Args.AddAllArgs(CmdArgs, options::OPT_u_Group);
Daniel Dunbarc1964212009-03-26 16:23:12 +00004706 Args.AddLastArg(CmdArgs, options::OPT_e);
4707 Args.AddAllArgs(CmdArgs, options::OPT_m_Separate);
4708 Args.AddAllArgs(CmdArgs, options::OPT_r);
4709
Daniel Dunbar767bbab2010-10-18 22:08:36 +00004710 // Forward -ObjC when either -ObjC or -ObjC++ is used, to force loading
4711 // members of static archive libraries which implement Objective-C classes or
4712 // categories.
4713 if (Args.hasArg(options::OPT_ObjC) || Args.hasArg(options::OPT_ObjCXX))
4714 CmdArgs.push_back("-ObjC");
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00004715
Daniel Dunbarc1964212009-03-26 16:23:12 +00004716 CmdArgs.push_back("-o");
4717 CmdArgs.push_back(Output.getFilename());
4718
Chad Rosier06fd3c62012-05-16 23:45:12 +00004719 if (!Args.hasArg(options::OPT_nostdlib) &&
Daniel Dunbarc1964212009-03-26 16:23:12 +00004720 !Args.hasArg(options::OPT_nostartfiles)) {
4721 // Derived from startfile spec.
4722 if (Args.hasArg(options::OPT_dynamiclib)) {
Daniel Dunbard067f7f2009-04-08 23:54:23 +00004723 // Derived from darwin_dylib1 spec.
Daniel Dunbar16d97092011-04-01 21:02:42 +00004724 if (getDarwinToolChain().isTargetIOSSimulator()) {
4725 // The simulator doesn't have a versioned crt1 file.
4726 CmdArgs.push_back("-ldylib1.o");
4727 } else if (getDarwinToolChain().isTargetIPhoneOS()) {
Daniel Dunbar83608032010-01-27 00:56:56 +00004728 if (getDarwinToolChain().isIPhoneOSVersionLT(3, 1))
4729 CmdArgs.push_back("-ldylib1.o");
4730 } else {
Daniel Dunbar6d23b2f2010-01-27 00:57:03 +00004731 if (getDarwinToolChain().isMacosxVersionLT(10, 5))
Daniel Dunbar83608032010-01-27 00:56:56 +00004732 CmdArgs.push_back("-ldylib1.o");
Daniel Dunbar6d23b2f2010-01-27 00:57:03 +00004733 else if (getDarwinToolChain().isMacosxVersionLT(10, 6))
Daniel Dunbar83608032010-01-27 00:56:56 +00004734 CmdArgs.push_back("-ldylib1.10.5.o");
4735 }
Daniel Dunbarc1964212009-03-26 16:23:12 +00004736 } else {
4737 if (Args.hasArg(options::OPT_bundle)) {
Daniel Dunbarae8bca02009-04-01 03:17:40 +00004738 if (!Args.hasArg(options::OPT_static)) {
4739 // Derived from darwin_bundle1 spec.
Daniel Dunbar16d97092011-04-01 21:02:42 +00004740 if (getDarwinToolChain().isTargetIOSSimulator()) {
4741 // The simulator doesn't have a versioned crt1 file.
4742 CmdArgs.push_back("-lbundle1.o");
4743 } else if (getDarwinToolChain().isTargetIPhoneOS()) {
Daniel Dunbar83608032010-01-27 00:56:56 +00004744 if (getDarwinToolChain().isIPhoneOSVersionLT(3, 1))
4745 CmdArgs.push_back("-lbundle1.o");
4746 } else {
Daniel Dunbar6d23b2f2010-01-27 00:57:03 +00004747 if (getDarwinToolChain().isMacosxVersionLT(10, 6))
Daniel Dunbar83608032010-01-27 00:56:56 +00004748 CmdArgs.push_back("-lbundle1.o");
4749 }
Daniel Dunbarae8bca02009-04-01 03:17:40 +00004750 }
Daniel Dunbarc1964212009-03-26 16:23:12 +00004751 } else {
Daniel Dunbar733b0f82011-03-01 18:49:30 +00004752 if (Args.hasArg(options::OPT_pg) &&
4753 getToolChain().SupportsProfiling()) {
Daniel Dunbarc1964212009-03-26 16:23:12 +00004754 if (Args.hasArg(options::OPT_static) ||
4755 Args.hasArg(options::OPT_object) ||
4756 Args.hasArg(options::OPT_preload)) {
4757 CmdArgs.push_back("-lgcrt0.o");
4758 } else {
4759 CmdArgs.push_back("-lgcrt1.o");
Daniel Dunbard067f7f2009-04-08 23:54:23 +00004760
Daniel Dunbarc1964212009-03-26 16:23:12 +00004761 // darwin_crt2 spec is empty.
Daniel Dunbard067f7f2009-04-08 23:54:23 +00004762 }
Bob Wilsonef285b42012-07-04 00:18:41 +00004763 // By default on OS X 10.8 and later, we don't link with a crt1.o
4764 // file and the linker knows to use _main as the entry point. But,
4765 // when compiling with -pg, we need to link with the gcrt1.o file,
4766 // so pass the -no_new_main option to tell the linker to use the
4767 // "start" symbol as the entry point.
Bob Wilson1e148fe2012-07-03 20:42:10 +00004768 if (getDarwinToolChain().isTargetMacOS() &&
4769 !getDarwinToolChain().isMacosxVersionLT(10, 8))
4770 CmdArgs.push_back("-no_new_main");
Daniel Dunbarc1964212009-03-26 16:23:12 +00004771 } else {
4772 if (Args.hasArg(options::OPT_static) ||
4773 Args.hasArg(options::OPT_object) ||
4774 Args.hasArg(options::OPT_preload)) {
4775 CmdArgs.push_back("-lcrt0.o");
4776 } else {
4777 // Derived from darwin_crt1 spec.
Daniel Dunbarebc34df2011-03-31 17:12:33 +00004778 if (getDarwinToolChain().isTargetIOSSimulator()) {
4779 // The simulator doesn't have a versioned crt1 file.
4780 CmdArgs.push_back("-lcrt1.o");
4781 } else if (getDarwinToolChain().isTargetIPhoneOS()) {
Daniel Dunbar83608032010-01-27 00:56:56 +00004782 if (getDarwinToolChain().isIPhoneOSVersionLT(3, 1))
4783 CmdArgs.push_back("-lcrt1.o");
Daniel Dunbarbd847cc2012-10-15 22:23:53 +00004784 else if (getDarwinToolChain().isIPhoneOSVersionLT(6, 0))
Daniel Dunbar83608032010-01-27 00:56:56 +00004785 CmdArgs.push_back("-lcrt1.3.1.o");
Daniel Dunbar6d23b2f2010-01-27 00:57:03 +00004786 } else {
4787 if (getDarwinToolChain().isMacosxVersionLT(10, 5))
4788 CmdArgs.push_back("-lcrt1.o");
4789 else if (getDarwinToolChain().isMacosxVersionLT(10, 6))
4790 CmdArgs.push_back("-lcrt1.10.5.o");
Ted Kremeneke65b0862012-03-06 20:05:56 +00004791 else if (getDarwinToolChain().isMacosxVersionLT(10, 8))
Daniel Dunbar6d23b2f2010-01-27 00:57:03 +00004792 CmdArgs.push_back("-lcrt1.10.6.o");
Daniel Dunbard067f7f2009-04-08 23:54:23 +00004793
Daniel Dunbar6d23b2f2010-01-27 00:57:03 +00004794 // darwin_crt2 spec is empty.
4795 }
Daniel Dunbarc1964212009-03-26 16:23:12 +00004796 }
4797 }
4798 }
4799 }
4800
Daniel Dunbar6d23b2f2010-01-27 00:57:03 +00004801 if (!getDarwinToolChain().isTargetIPhoneOS() &&
4802 Args.hasArg(options::OPT_shared_libgcc) &&
4803 getDarwinToolChain().isMacosxVersionLT(10, 5)) {
Daniel Dunbar1c28f1e2009-09-09 22:32:48 +00004804 const char *Str =
Daniel Dunbar9c3ed5f2010-07-14 18:46:23 +00004805 Args.MakeArgString(getToolChain().GetFilePath("crt3.o"));
Daniel Dunbar1c28f1e2009-09-09 22:32:48 +00004806 CmdArgs.push_back(Str);
Daniel Dunbarc1964212009-03-26 16:23:12 +00004807 }
4808 }
4809
4810 Args.AddAllArgs(CmdArgs, options::OPT_L);
Daniel Dunbard067f7f2009-04-08 23:54:23 +00004811
Alexey Samsonov609213f92013-08-19 09:14:21 +00004812 const SanitizerArgs &Sanitize =
4813 getToolChain().getDriver().getOrParseSanitizerArgs(Args);
Alexey Samsonov969be242013-01-21 08:45:02 +00004814 // If we're building a dynamic lib with -fsanitize=address,
4815 // unresolved symbols may appear. Mark all
Alexey Samsonovcc429802012-11-16 12:53:14 +00004816 // of them as dynamic_lookup. Linking executables is handled in
4817 // lib/Driver/ToolChains.cpp.
Alexey Samsonov969be242013-01-21 08:45:02 +00004818 if (Sanitize.needsAsanRt()) {
Kostya Serebryany0b692ce2011-12-06 19:18:44 +00004819 if (Args.hasArg(options::OPT_dynamiclib) ||
4820 Args.hasArg(options::OPT_bundle)) {
4821 CmdArgs.push_back("-undefined");
4822 CmdArgs.push_back("dynamic_lookup");
4823 }
4824 }
4825
Daniel Dunbarc1964212009-03-26 16:23:12 +00004826 if (Args.hasArg(options::OPT_fopenmp))
4827 // This is more complicated in gcc...
4828 CmdArgs.push_back("-lgomp");
4829
Douglas Gregor9295df02012-05-15 21:00:27 +00004830 AddLinkerInputs(getToolChain(), Inputs, Args, CmdArgs);
4831
Bob Wilson16d93952012-05-15 18:57:39 +00004832 if (isObjCRuntimeLinked(Args) &&
4833 !Args.hasArg(options::OPT_nostdlib) &&
4834 !Args.hasArg(options::OPT_nodefaultlibs)) {
Ted Kremeneke65b0862012-03-06 20:05:56 +00004835 // Avoid linking compatibility stubs on i386 mac.
4836 if (!getDarwinToolChain().isTargetMacOS() ||
Rafael Espindola35ca7d92012-10-07 04:44:33 +00004837 getDarwinToolChain().getArch() != llvm::Triple::x86) {
Ted Kremeneke65b0862012-03-06 20:05:56 +00004838 // If we don't have ARC or subscripting runtime support, link in the
4839 // runtime stubs. We have to do this *before* adding any of the normal
4840 // linker inputs so that its initializer gets run first.
John McCall5fb5df92012-06-20 06:18:46 +00004841 ObjCRuntime runtime =
4842 getDarwinToolChain().getDefaultObjCRuntime(/*nonfragile*/ true);
Ted Kremeneke65b0862012-03-06 20:05:56 +00004843 // We use arclite library for both ARC and subscripting support.
John McCall3deb1ad2012-08-21 02:47:43 +00004844 if ((!runtime.hasNativeARC() && isObjCAutoRefCount(Args)) ||
John McCall5fb5df92012-06-20 06:18:46 +00004845 !runtime.hasSubscripting())
Ted Kremeneke65b0862012-03-06 20:05:56 +00004846 getDarwinToolChain().AddLinkARCArgs(Args, CmdArgs);
Ted Kremeneke65b0862012-03-06 20:05:56 +00004847 }
Bob Wilson7dda0cd2012-04-21 00:21:42 +00004848 CmdArgs.push_back("-framework");
4849 CmdArgs.push_back("Foundation");
Ted Kremeneke65b0862012-03-06 20:05:56 +00004850 // Link libobj.
4851 CmdArgs.push_back("-lobjc");
John McCall24fc0de2011-07-06 00:26:06 +00004852 }
John McCall31168b02011-06-15 23:02:42 +00004853
Daniel Dunbarc1964212009-03-26 16:23:12 +00004854 if (LinkingOutput) {
4855 CmdArgs.push_back("-arch_multiple");
4856 CmdArgs.push_back("-final_output");
4857 CmdArgs.push_back(LinkingOutput);
4858 }
4859
Daniel Dunbarc1964212009-03-26 16:23:12 +00004860 if (Args.hasArg(options::OPT_fnested_functions))
4861 CmdArgs.push_back("-allow_stack_execute");
Daniel Dunbard067f7f2009-04-08 23:54:23 +00004862
Daniel Dunbarc1964212009-03-26 16:23:12 +00004863 if (!Args.hasArg(options::OPT_nostdlib) &&
4864 !Args.hasArg(options::OPT_nodefaultlibs)) {
Hans Wennborg70850d82013-07-18 20:29:38 +00004865 if (getToolChain().getDriver().CCCIsCXX())
Daniel Dunbar3f7796f2010-09-17 01:20:05 +00004866 getToolChain().AddCXXStdlibLibArgs(Args, CmdArgs);
Daniel Dunbarad0f62b2009-04-08 06:06:21 +00004867
Daniel Dunbarc1964212009-03-26 16:23:12 +00004868 // link_ssp spec is empty.
4869
Daniel Dunbar26d482a2009-09-18 08:15:03 +00004870 // Let the tool chain choose which runtime library to link.
4871 getDarwinToolChain().AddLinkRuntimeLibArgs(Args, CmdArgs);
Daniel Dunbarc1964212009-03-26 16:23:12 +00004872 }
4873
Chad Rosier06fd3c62012-05-16 23:45:12 +00004874 if (!Args.hasArg(options::OPT_nostdlib) &&
Daniel Dunbarc1964212009-03-26 16:23:12 +00004875 !Args.hasArg(options::OPT_nostartfiles)) {
4876 // endfile_spec is empty.
4877 }
4878
4879 Args.AddAllArgs(CmdArgs, options::OPT_T_Group);
4880 Args.AddAllArgs(CmdArgs, options::OPT_F);
4881
Daniel Dunbard067f7f2009-04-08 23:54:23 +00004882 const char *Exec =
Daniel Dunbar9c3ed5f2010-07-14 18:46:23 +00004883 Args.MakeArgString(getToolChain().GetProgramPath("ld"));
Daniel Dunbar7fbaf532010-08-02 02:38:28 +00004884 C.addCommand(new Command(JA, *this, Exec, CmdArgs));
Daniel Dunbarc1964212009-03-26 16:23:12 +00004885}
4886
Daniel Dunbar64ed5e32009-03-20 00:52:38 +00004887void darwin::Lipo::ConstructJob(Compilation &C, const JobAction &JA,
Daniel Dunbar7fbaf532010-08-02 02:38:28 +00004888 const InputInfo &Output,
Daniel Dunbard067f7f2009-04-08 23:54:23 +00004889 const InputInfoList &Inputs,
4890 const ArgList &Args,
Daniel Dunbar64ed5e32009-03-20 00:52:38 +00004891 const char *LinkingOutput) const {
4892 ArgStringList CmdArgs;
4893
4894 CmdArgs.push_back("-create");
4895 assert(Output.isFilename() && "Unexpected lipo output.");
Daniel Dunbar06686ab2009-03-24 00:24:37 +00004896
4897 CmdArgs.push_back("-output");
Daniel Dunbar64ed5e32009-03-20 00:52:38 +00004898 CmdArgs.push_back(Output.getFilename());
Daniel Dunbar06686ab2009-03-24 00:24:37 +00004899
Daniel Dunbar64ed5e32009-03-20 00:52:38 +00004900 for (InputInfoList::const_iterator
4901 it = Inputs.begin(), ie = Inputs.end(); it != ie; ++it) {
4902 const InputInfo &II = *it;
4903 assert(II.isFilename() && "Unexpected lipo input.");
4904 CmdArgs.push_back(II.getFilename());
4905 }
Daniel Dunbard067f7f2009-04-08 23:54:23 +00004906 const char *Exec =
Daniel Dunbar9c3ed5f2010-07-14 18:46:23 +00004907 Args.MakeArgString(getToolChain().GetProgramPath("lipo"));
Daniel Dunbar7fbaf532010-08-02 02:38:28 +00004908 C.addCommand(new Command(JA, *this, Exec, CmdArgs));
Daniel Dunbar64ed5e32009-03-20 00:52:38 +00004909}
Daniel Dunbar8eb473c2009-03-31 17:45:15 +00004910
Daniel Dunbar88299622010-06-04 18:28:36 +00004911void darwin::Dsymutil::ConstructJob(Compilation &C, const JobAction &JA,
Daniel Dunbar7fbaf532010-08-02 02:38:28 +00004912 const InputInfo &Output,
Daniel Dunbar88299622010-06-04 18:28:36 +00004913 const InputInfoList &Inputs,
4914 const ArgList &Args,
4915 const char *LinkingOutput) const {
4916 ArgStringList CmdArgs;
4917
Daniel Dunbareb86b042011-05-09 17:23:16 +00004918 CmdArgs.push_back("-o");
4919 CmdArgs.push_back(Output.getFilename());
4920
Daniel Dunbar88299622010-06-04 18:28:36 +00004921 assert(Inputs.size() == 1 && "Unable to handle multiple inputs.");
4922 const InputInfo &Input = Inputs[0];
4923 assert(Input.isFilename() && "Unexpected dsymutil input.");
4924 CmdArgs.push_back(Input.getFilename());
4925
Daniel Dunbar88299622010-06-04 18:28:36 +00004926 const char *Exec =
Daniel Dunbar9c3ed5f2010-07-14 18:46:23 +00004927 Args.MakeArgString(getToolChain().GetProgramPath("dsymutil"));
Daniel Dunbar7fbaf532010-08-02 02:38:28 +00004928 C.addCommand(new Command(JA, *this, Exec, CmdArgs));
Daniel Dunbar88299622010-06-04 18:28:36 +00004929}
4930
Eric Christopher551ef452011-08-23 17:56:55 +00004931void darwin::VerifyDebug::ConstructJob(Compilation &C, const JobAction &JA,
Eric Christopher45f2e712012-12-18 00:31:10 +00004932 const InputInfo &Output,
4933 const InputInfoList &Inputs,
4934 const ArgList &Args,
4935 const char *LinkingOutput) const {
Eric Christopher551ef452011-08-23 17:56:55 +00004936 ArgStringList CmdArgs;
4937 CmdArgs.push_back("--verify");
Eric Christopher36222212012-02-06 19:13:09 +00004938 CmdArgs.push_back("--debug-info");
4939 CmdArgs.push_back("--eh-frame");
Eric Christopher65c05fa2012-02-06 19:43:51 +00004940 CmdArgs.push_back("--quiet");
Eric Christopher551ef452011-08-23 17:56:55 +00004941
4942 assert(Inputs.size() == 1 && "Unable to handle multiple inputs.");
4943 const InputInfo &Input = Inputs[0];
4944 assert(Input.isFilename() && "Unexpected verify input");
4945
4946 // Grabbing the output of the earlier dsymutil run.
4947 CmdArgs.push_back(Input.getFilename());
4948
4949 const char *Exec =
4950 Args.MakeArgString(getToolChain().GetProgramPath("dwarfdump"));
4951 C.addCommand(new Command(JA, *this, Exec, CmdArgs));
4952}
4953
David Chisnallf571cde2012-02-15 13:39:01 +00004954void solaris::Assemble::ConstructJob(Compilation &C, const JobAction &JA,
4955 const InputInfo &Output,
4956 const InputInfoList &Inputs,
4957 const ArgList &Args,
4958 const char *LinkingOutput) const {
4959 ArgStringList CmdArgs;
4960
4961 Args.AddAllArgValues(CmdArgs, options::OPT_Wa_COMMA,
4962 options::OPT_Xassembler);
4963
4964 CmdArgs.push_back("-o");
4965 CmdArgs.push_back(Output.getFilename());
4966
4967 for (InputInfoList::const_iterator
4968 it = Inputs.begin(), ie = Inputs.end(); it != ie; ++it) {
4969 const InputInfo &II = *it;
4970 CmdArgs.push_back(II.getFilename());
4971 }
4972
4973 const char *Exec =
4974 Args.MakeArgString(getToolChain().GetProgramPath("as"));
4975 C.addCommand(new Command(JA, *this, Exec, CmdArgs));
4976}
4977
4978
4979void solaris::Link::ConstructJob(Compilation &C, const JobAction &JA,
4980 const InputInfo &Output,
4981 const InputInfoList &Inputs,
4982 const ArgList &Args,
4983 const char *LinkingOutput) const {
4984 // FIXME: Find a real GCC, don't hard-code versions here
4985 std::string GCCLibPath = "/usr/gcc/4.5/lib/gcc/";
4986 const llvm::Triple &T = getToolChain().getTriple();
4987 std::string LibPath = "/usr/lib/";
4988 llvm::Triple::ArchType Arch = T.getArch();
4989 switch (Arch) {
4990 case llvm::Triple::x86:
4991 GCCLibPath += ("i386-" + T.getVendorName() + "-" +
4992 T.getOSName()).str() + "/4.5.2/";
4993 break;
4994 case llvm::Triple::x86_64:
4995 GCCLibPath += ("i386-" + T.getVendorName() + "-" +
4996 T.getOSName()).str();
4997 GCCLibPath += "/4.5.2/amd64/";
4998 LibPath += "amd64/";
4999 break;
5000 default:
5001 assert(0 && "Unsupported architecture");
5002 }
5003
5004 ArgStringList CmdArgs;
5005
David Chisnall272a0712012-02-29 15:06:12 +00005006 // Demangle C++ names in errors
5007 CmdArgs.push_back("-C");
5008
David Chisnallf571cde2012-02-15 13:39:01 +00005009 if ((!Args.hasArg(options::OPT_nostdlib)) &&
5010 (!Args.hasArg(options::OPT_shared))) {
5011 CmdArgs.push_back("-e");
5012 CmdArgs.push_back("_start");
5013 }
5014
5015 if (Args.hasArg(options::OPT_static)) {
5016 CmdArgs.push_back("-Bstatic");
5017 CmdArgs.push_back("-dn");
5018 } else {
5019 CmdArgs.push_back("-Bdynamic");
5020 if (Args.hasArg(options::OPT_shared)) {
5021 CmdArgs.push_back("-shared");
5022 } else {
5023 CmdArgs.push_back("--dynamic-linker");
5024 CmdArgs.push_back(Args.MakeArgString(LibPath + "ld.so.1"));
5025 }
5026 }
5027
5028 if (Output.isFilename()) {
5029 CmdArgs.push_back("-o");
5030 CmdArgs.push_back(Output.getFilename());
5031 } else {
5032 assert(Output.isNothing() && "Invalid output.");
5033 }
5034
5035 if (!Args.hasArg(options::OPT_nostdlib) &&
5036 !Args.hasArg(options::OPT_nostartfiles)) {
5037 if (!Args.hasArg(options::OPT_shared)) {
5038 CmdArgs.push_back(Args.MakeArgString(LibPath + "crt1.o"));
5039 CmdArgs.push_back(Args.MakeArgString(LibPath + "crti.o"));
David Chisnall0c52c0f2012-02-28 17:10:04 +00005040 CmdArgs.push_back(Args.MakeArgString(LibPath + "values-Xa.o"));
David Chisnallf571cde2012-02-15 13:39:01 +00005041 CmdArgs.push_back(Args.MakeArgString(GCCLibPath + "crtbegin.o"));
5042 } else {
5043 CmdArgs.push_back(Args.MakeArgString(LibPath + "crti.o"));
David Chisnall0c52c0f2012-02-28 17:10:04 +00005044 CmdArgs.push_back(Args.MakeArgString(LibPath + "values-Xa.o"));
5045 CmdArgs.push_back(Args.MakeArgString(GCCLibPath + "crtbegin.o"));
David Chisnallf571cde2012-02-15 13:39:01 +00005046 }
Hans Wennborg70850d82013-07-18 20:29:38 +00005047 if (getToolChain().getDriver().CCCIsCXX())
David Chisnallddc4c9d2012-03-13 14:14:54 +00005048 CmdArgs.push_back(Args.MakeArgString(LibPath + "cxa_finalize.o"));
David Chisnallf571cde2012-02-15 13:39:01 +00005049 }
5050
5051 CmdArgs.push_back(Args.MakeArgString("-L" + GCCLibPath));
5052
5053 Args.AddAllArgs(CmdArgs, options::OPT_L);
5054 Args.AddAllArgs(CmdArgs, options::OPT_T_Group);
5055 Args.AddAllArgs(CmdArgs, options::OPT_e);
David Chisnall0c52c0f2012-02-28 17:10:04 +00005056 Args.AddAllArgs(CmdArgs, options::OPT_r);
David Chisnallf571cde2012-02-15 13:39:01 +00005057
5058 AddLinkerInputs(getToolChain(), Inputs, Args, CmdArgs);
5059
5060 if (!Args.hasArg(options::OPT_nostdlib) &&
5061 !Args.hasArg(options::OPT_nodefaultlibs)) {
Hans Wennborg70850d82013-07-18 20:29:38 +00005062 if (getToolChain().getDriver().CCCIsCXX())
David Chisnall3d127c72012-04-10 11:49:50 +00005063 getToolChain().AddCXXStdlibLibArgs(Args, CmdArgs);
David Chisnall1026fb02012-02-15 18:24:31 +00005064 CmdArgs.push_back("-lgcc_s");
David Chisnall0c52c0f2012-02-28 17:10:04 +00005065 if (!Args.hasArg(options::OPT_shared)) {
5066 CmdArgs.push_back("-lgcc");
David Chisnallf571cde2012-02-15 13:39:01 +00005067 CmdArgs.push_back("-lc");
David Chisnallc73fb892012-02-28 20:06:45 +00005068 CmdArgs.push_back("-lm");
David Chisnall0c52c0f2012-02-28 17:10:04 +00005069 }
David Chisnallf571cde2012-02-15 13:39:01 +00005070 }
5071
5072 if (!Args.hasArg(options::OPT_nostdlib) &&
5073 !Args.hasArg(options::OPT_nostartfiles)) {
David Chisnall0c52c0f2012-02-28 17:10:04 +00005074 CmdArgs.push_back(Args.MakeArgString(GCCLibPath + "crtend.o"));
David Chisnallf571cde2012-02-15 13:39:01 +00005075 }
David Chisnall96de9932012-02-16 16:00:47 +00005076 CmdArgs.push_back(Args.MakeArgString(LibPath + "crtn.o"));
David Chisnallf571cde2012-02-15 13:39:01 +00005077
5078 addProfileRT(getToolChain(), Args, CmdArgs, getToolChain().getTriple());
5079
5080 const char *Exec =
5081 Args.MakeArgString(getToolChain().GetProgramPath("ld"));
5082 C.addCommand(new Command(JA, *this, Exec, CmdArgs));
5083}
5084
Edward O'Callaghan856e4ff2009-08-22 01:06:46 +00005085void auroraux::Assemble::ConstructJob(Compilation &C, const JobAction &JA,
Daniel Dunbar7fbaf532010-08-02 02:38:28 +00005086 const InputInfo &Output,
Daniel Dunbar1c7577c2009-11-04 06:24:38 +00005087 const InputInfoList &Inputs,
5088 const ArgList &Args,
5089 const char *LinkingOutput) const {
Edward O'Callaghan856e4ff2009-08-22 01:06:46 +00005090 ArgStringList CmdArgs;
5091
5092 Args.AddAllArgValues(CmdArgs, options::OPT_Wa_COMMA,
5093 options::OPT_Xassembler);
5094
5095 CmdArgs.push_back("-o");
Daniel Dunbarb440f562010-08-02 02:38:21 +00005096 CmdArgs.push_back(Output.getFilename());
Edward O'Callaghan856e4ff2009-08-22 01:06:46 +00005097
5098 for (InputInfoList::const_iterator
5099 it = Inputs.begin(), ie = Inputs.end(); it != ie; ++it) {
5100 const InputInfo &II = *it;
Daniel Dunbarb440f562010-08-02 02:38:21 +00005101 CmdArgs.push_back(II.getFilename());
Edward O'Callaghan856e4ff2009-08-22 01:06:46 +00005102 }
5103
5104 const char *Exec =
Daniel Dunbar9c3ed5f2010-07-14 18:46:23 +00005105 Args.MakeArgString(getToolChain().GetProgramPath("gas"));
Daniel Dunbar7fbaf532010-08-02 02:38:28 +00005106 C.addCommand(new Command(JA, *this, Exec, CmdArgs));
Edward O'Callaghan856e4ff2009-08-22 01:06:46 +00005107}
5108
5109void auroraux::Link::ConstructJob(Compilation &C, const JobAction &JA,
Daniel Dunbar7fbaf532010-08-02 02:38:28 +00005110 const InputInfo &Output,
Daniel Dunbar1c7577c2009-11-04 06:24:38 +00005111 const InputInfoList &Inputs,
5112 const ArgList &Args,
5113 const char *LinkingOutput) const {
Edward O'Callaghan856e4ff2009-08-22 01:06:46 +00005114 ArgStringList CmdArgs;
5115
5116 if ((!Args.hasArg(options::OPT_nostdlib)) &&
Daniel Dunbar1c7577c2009-11-04 06:24:38 +00005117 (!Args.hasArg(options::OPT_shared))) {
Edward O'Callaghan856e4ff2009-08-22 01:06:46 +00005118 CmdArgs.push_back("-e");
Edward O'Callaghand8712d92009-10-15 07:44:07 +00005119 CmdArgs.push_back("_start");
Edward O'Callaghan856e4ff2009-08-22 01:06:46 +00005120 }
5121
5122 if (Args.hasArg(options::OPT_static)) {
5123 CmdArgs.push_back("-Bstatic");
Edward O'Callaghand8712d92009-10-15 07:44:07 +00005124 CmdArgs.push_back("-dn");
Edward O'Callaghan856e4ff2009-08-22 01:06:46 +00005125 } else {
Edward O'Callaghand8712d92009-10-15 07:44:07 +00005126// CmdArgs.push_back("--eh-frame-hdr");
Edward O'Callaghan856e4ff2009-08-22 01:06:46 +00005127 CmdArgs.push_back("-Bdynamic");
5128 if (Args.hasArg(options::OPT_shared)) {
5129 CmdArgs.push_back("-shared");
5130 } else {
Edward O'Callaghan7d3c2752009-10-16 19:44:18 +00005131 CmdArgs.push_back("--dynamic-linker");
Edward O'Callaghan856e4ff2009-08-22 01:06:46 +00005132 CmdArgs.push_back("/lib/ld.so.1"); // 64Bit Path /lib/amd64/ld.so.1
5133 }
5134 }
5135
Daniel Dunbarb440f562010-08-02 02:38:21 +00005136 if (Output.isFilename()) {
Edward O'Callaghan856e4ff2009-08-22 01:06:46 +00005137 CmdArgs.push_back("-o");
5138 CmdArgs.push_back(Output.getFilename());
5139 } else {
5140 assert(Output.isNothing() && "Invalid output.");
5141 }
5142
5143 if (!Args.hasArg(options::OPT_nostdlib) &&
5144 !Args.hasArg(options::OPT_nostartfiles)) {
5145 if (!Args.hasArg(options::OPT_shared)) {
Chris Lattner3e2ee142010-07-07 16:01:42 +00005146 CmdArgs.push_back(Args.MakeArgString(
Daniel Dunbar9c3ed5f2010-07-14 18:46:23 +00005147 getToolChain().GetFilePath("crt1.o")));
Chris Lattner3e2ee142010-07-07 16:01:42 +00005148 CmdArgs.push_back(Args.MakeArgString(
Daniel Dunbar9c3ed5f2010-07-14 18:46:23 +00005149 getToolChain().GetFilePath("crti.o")));
Chris Lattner3e2ee142010-07-07 16:01:42 +00005150 CmdArgs.push_back(Args.MakeArgString(
Daniel Dunbar9c3ed5f2010-07-14 18:46:23 +00005151 getToolChain().GetFilePath("crtbegin.o")));
Edward O'Callaghan856e4ff2009-08-22 01:06:46 +00005152 } else {
Chris Lattner3e2ee142010-07-07 16:01:42 +00005153 CmdArgs.push_back(Args.MakeArgString(
Daniel Dunbar9c3ed5f2010-07-14 18:46:23 +00005154 getToolChain().GetFilePath("crti.o")));
Edward O'Callaghan856e4ff2009-08-22 01:06:46 +00005155 }
Chris Lattner3e2ee142010-07-07 16:01:42 +00005156 CmdArgs.push_back(Args.MakeArgString(
Daniel Dunbar9c3ed5f2010-07-14 18:46:23 +00005157 getToolChain().GetFilePath("crtn.o")));
Edward O'Callaghan856e4ff2009-08-22 01:06:46 +00005158 }
5159
Daniel Dunbar1c7577c2009-11-04 06:24:38 +00005160 CmdArgs.push_back(Args.MakeArgString("-L/opt/gcc4/lib/gcc/"
5161 + getToolChain().getTripleString()
Daniel Dunbarb0b18612009-10-29 02:24:37 +00005162 + "/4.2.4"));
Edward O'Callaghan856e4ff2009-08-22 01:06:46 +00005163
5164 Args.AddAllArgs(CmdArgs, options::OPT_L);
5165 Args.AddAllArgs(CmdArgs, options::OPT_T_Group);
5166 Args.AddAllArgs(CmdArgs, options::OPT_e);
5167
Daniel Dunbar54423b22010-09-17 00:24:54 +00005168 AddLinkerInputs(getToolChain(), Inputs, Args, CmdArgs);
Edward O'Callaghan856e4ff2009-08-22 01:06:46 +00005169
5170 if (!Args.hasArg(options::OPT_nostdlib) &&
5171 !Args.hasArg(options::OPT_nodefaultlibs)) {
5172 // FIXME: For some reason GCC passes -lgcc before adding
5173 // the default system libraries. Just mimic this for now.
5174 CmdArgs.push_back("-lgcc");
5175
5176 if (Args.hasArg(options::OPT_pthread))
5177 CmdArgs.push_back("-pthread");
5178 if (!Args.hasArg(options::OPT_shared))
5179 CmdArgs.push_back("-lc");
5180 CmdArgs.push_back("-lgcc");
5181 }
5182
5183 if (!Args.hasArg(options::OPT_nostdlib) &&
5184 !Args.hasArg(options::OPT_nostartfiles)) {
5185 if (!Args.hasArg(options::OPT_shared))
Chris Lattner3e2ee142010-07-07 16:01:42 +00005186 CmdArgs.push_back(Args.MakeArgString(
Daniel Dunbar9c3ed5f2010-07-14 18:46:23 +00005187 getToolChain().GetFilePath("crtend.o")));
Edward O'Callaghan856e4ff2009-08-22 01:06:46 +00005188 }
5189
Bill Wendling08760582011-06-27 19:15:03 +00005190 addProfileRT(getToolChain(), Args, CmdArgs, getToolChain().getTriple());
Nick Lewycky82fe5f42011-05-24 21:54:59 +00005191
Edward O'Callaghan856e4ff2009-08-22 01:06:46 +00005192 const char *Exec =
Daniel Dunbar9c3ed5f2010-07-14 18:46:23 +00005193 Args.MakeArgString(getToolChain().GetProgramPath("ld"));
Daniel Dunbar7fbaf532010-08-02 02:38:28 +00005194 C.addCommand(new Command(JA, *this, Exec, CmdArgs));
Edward O'Callaghan856e4ff2009-08-22 01:06:46 +00005195}
5196
Daniel Dunbar10de9e62009-06-29 20:52:51 +00005197void openbsd::Assemble::ConstructJob(Compilation &C, const JobAction &JA,
Daniel Dunbar7fbaf532010-08-02 02:38:28 +00005198 const InputInfo &Output,
Daniel Dunbar10de9e62009-06-29 20:52:51 +00005199 const InputInfoList &Inputs,
5200 const ArgList &Args,
Mike Stump11289f42009-09-09 15:08:12 +00005201 const char *LinkingOutput) const {
Daniel Dunbar10de9e62009-06-29 20:52:51 +00005202 ArgStringList CmdArgs;
5203
5204 Args.AddAllArgValues(CmdArgs, options::OPT_Wa_COMMA,
5205 options::OPT_Xassembler);
5206
5207 CmdArgs.push_back("-o");
Daniel Dunbarb440f562010-08-02 02:38:21 +00005208 CmdArgs.push_back(Output.getFilename());
Daniel Dunbar10de9e62009-06-29 20:52:51 +00005209
5210 for (InputInfoList::const_iterator
5211 it = Inputs.begin(), ie = Inputs.end(); it != ie; ++it) {
5212 const InputInfo &II = *it;
Daniel Dunbarb440f562010-08-02 02:38:21 +00005213 CmdArgs.push_back(II.getFilename());
Daniel Dunbar10de9e62009-06-29 20:52:51 +00005214 }
5215
5216 const char *Exec =
Daniel Dunbar9c3ed5f2010-07-14 18:46:23 +00005217 Args.MakeArgString(getToolChain().GetProgramPath("as"));
Daniel Dunbar7fbaf532010-08-02 02:38:28 +00005218 C.addCommand(new Command(JA, *this, Exec, CmdArgs));
Daniel Dunbar10de9e62009-06-29 20:52:51 +00005219}
5220
5221void openbsd::Link::ConstructJob(Compilation &C, const JobAction &JA,
Daniel Dunbar7fbaf532010-08-02 02:38:28 +00005222 const InputInfo &Output,
Daniel Dunbar10de9e62009-06-29 20:52:51 +00005223 const InputInfoList &Inputs,
5224 const ArgList &Args,
5225 const char *LinkingOutput) const {
Daniel Dunbar083edf72009-12-21 18:54:17 +00005226 const Driver &D = getToolChain().getDriver();
Daniel Dunbar10de9e62009-06-29 20:52:51 +00005227 ArgStringList CmdArgs;
5228
Rafael Espindolaaadd30e2012-12-31 22:41:36 +00005229 // Silence warning for "clang -g foo.o -o foo"
5230 Args.ClaimAllArgs(options::OPT_g_Group);
5231 // and "clang -emit-llvm foo.o -o foo"
5232 Args.ClaimAllArgs(options::OPT_emit_llvm);
5233 // and for "clang -w foo.o -o foo". Other warning options are already
5234 // handled somewhere else.
5235 Args.ClaimAllArgs(options::OPT_w);
5236
Daniel Dunbara8888ac2009-08-03 01:28:59 +00005237 if ((!Args.hasArg(options::OPT_nostdlib)) &&
Daniel Dunbar1c7577c2009-11-04 06:24:38 +00005238 (!Args.hasArg(options::OPT_shared))) {
Daniel Dunbara8888ac2009-08-03 01:28:59 +00005239 CmdArgs.push_back("-e");
5240 CmdArgs.push_back("__start");
5241 }
5242
Daniel Dunbar10de9e62009-06-29 20:52:51 +00005243 if (Args.hasArg(options::OPT_static)) {
5244 CmdArgs.push_back("-Bstatic");
5245 } else {
Rafael Espindola7ba97af2010-11-11 02:17:51 +00005246 if (Args.hasArg(options::OPT_rdynamic))
5247 CmdArgs.push_back("-export-dynamic");
Daniel Dunbar10de9e62009-06-29 20:52:51 +00005248 CmdArgs.push_back("--eh-frame-hdr");
Daniel Dunbara8888ac2009-08-03 01:28:59 +00005249 CmdArgs.push_back("-Bdynamic");
Daniel Dunbar10de9e62009-06-29 20:52:51 +00005250 if (Args.hasArg(options::OPT_shared)) {
Daniel Dunbara8888ac2009-08-03 01:28:59 +00005251 CmdArgs.push_back("-shared");
Daniel Dunbar10de9e62009-06-29 20:52:51 +00005252 } else {
5253 CmdArgs.push_back("-dynamic-linker");
5254 CmdArgs.push_back("/usr/libexec/ld.so");
5255 }
5256 }
5257
Rafael Espindola044f7832013-06-05 04:28:55 +00005258 if (Args.hasArg(options::OPT_nopie))
5259 CmdArgs.push_back("-nopie");
5260
Daniel Dunbarb440f562010-08-02 02:38:21 +00005261 if (Output.isFilename()) {
Daniel Dunbar10de9e62009-06-29 20:52:51 +00005262 CmdArgs.push_back("-o");
5263 CmdArgs.push_back(Output.getFilename());
5264 } else {
5265 assert(Output.isNothing() && "Invalid output.");
5266 }
5267
5268 if (!Args.hasArg(options::OPT_nostdlib) &&
5269 !Args.hasArg(options::OPT_nostartfiles)) {
5270 if (!Args.hasArg(options::OPT_shared)) {
Eli Friedman3715d1f2011-12-15 02:15:56 +00005271 if (Args.hasArg(options::OPT_pg))
5272 CmdArgs.push_back(Args.MakeArgString(
5273 getToolChain().GetFilePath("gcrt0.o")));
5274 else
5275 CmdArgs.push_back(Args.MakeArgString(
5276 getToolChain().GetFilePath("crt0.o")));
Chris Lattner3e2ee142010-07-07 16:01:42 +00005277 CmdArgs.push_back(Args.MakeArgString(
Daniel Dunbar9c3ed5f2010-07-14 18:46:23 +00005278 getToolChain().GetFilePath("crtbegin.o")));
Daniel Dunbar10de9e62009-06-29 20:52:51 +00005279 } else {
Chris Lattner3e2ee142010-07-07 16:01:42 +00005280 CmdArgs.push_back(Args.MakeArgString(
Daniel Dunbar9c3ed5f2010-07-14 18:46:23 +00005281 getToolChain().GetFilePath("crtbeginS.o")));
Daniel Dunbar10de9e62009-06-29 20:52:51 +00005282 }
5283 }
5284
Edward O'Callaghan5c521462009-10-28 15:13:08 +00005285 std::string Triple = getToolChain().getTripleString();
5286 if (Triple.substr(0, 6) == "x86_64")
Daniel Dunbar1c7577c2009-11-04 06:24:38 +00005287 Triple.replace(0, 6, "amd64");
Daniel Dunbarb0b18612009-10-29 02:24:37 +00005288 CmdArgs.push_back(Args.MakeArgString("-L/usr/lib/gcc-lib/" + Triple +
Daniel Dunbarea3813f2010-08-01 23:13:54 +00005289 "/4.2.1"));
Daniel Dunbara8888ac2009-08-03 01:28:59 +00005290
Daniel Dunbar10de9e62009-06-29 20:52:51 +00005291 Args.AddAllArgs(CmdArgs, options::OPT_L);
5292 Args.AddAllArgs(CmdArgs, options::OPT_T_Group);
5293 Args.AddAllArgs(CmdArgs, options::OPT_e);
Rafael Espindolaaadd30e2012-12-31 22:41:36 +00005294 Args.AddAllArgs(CmdArgs, options::OPT_s);
5295 Args.AddAllArgs(CmdArgs, options::OPT_t);
5296 Args.AddAllArgs(CmdArgs, options::OPT_Z_Flag);
5297 Args.AddAllArgs(CmdArgs, options::OPT_r);
Daniel Dunbar10de9e62009-06-29 20:52:51 +00005298
Daniel Dunbar54423b22010-09-17 00:24:54 +00005299 AddLinkerInputs(getToolChain(), Inputs, Args, CmdArgs);
Daniel Dunbar10de9e62009-06-29 20:52:51 +00005300
5301 if (!Args.hasArg(options::OPT_nostdlib) &&
5302 !Args.hasArg(options::OPT_nodefaultlibs)) {
Hans Wennborg70850d82013-07-18 20:29:38 +00005303 if (D.CCCIsCXX()) {
Daniel Dunbar3f7796f2010-09-17 01:20:05 +00005304 getToolChain().AddCXXStdlibLibArgs(Args, CmdArgs);
Eli Friedman3715d1f2011-12-15 02:15:56 +00005305 if (Args.hasArg(options::OPT_pg))
5306 CmdArgs.push_back("-lm_p");
5307 else
5308 CmdArgs.push_back("-lm");
Daniel Dunbarea3813f2010-08-01 23:13:54 +00005309 }
5310
Daniel Dunbara8888ac2009-08-03 01:28:59 +00005311 // FIXME: For some reason GCC passes -lgcc before adding
5312 // the default system libraries. Just mimic this for now.
5313 CmdArgs.push_back("-lgcc");
Daniel Dunbar10de9e62009-06-29 20:52:51 +00005314
Eric Christopher17674ec2012-09-13 06:32:34 +00005315 if (Args.hasArg(options::OPT_pthread)) {
5316 if (!Args.hasArg(options::OPT_shared) &&
5317 Args.hasArg(options::OPT_pg))
5318 CmdArgs.push_back("-lpthread_p");
5319 else
5320 CmdArgs.push_back("-lpthread");
5321 }
5322
Chandler Carruth45661652011-12-17 22:32:42 +00005323 if (!Args.hasArg(options::OPT_shared)) {
Eric Christopher17674ec2012-09-13 06:32:34 +00005324 if (Args.hasArg(options::OPT_pg))
Eli Friedman3715d1f2011-12-15 02:15:56 +00005325 CmdArgs.push_back("-lc_p");
5326 else
5327 CmdArgs.push_back("-lc");
Chandler Carruth45661652011-12-17 22:32:42 +00005328 }
Eric Christopher17674ec2012-09-13 06:32:34 +00005329
Daniel Dunbara8888ac2009-08-03 01:28:59 +00005330 CmdArgs.push_back("-lgcc");
Daniel Dunbar10de9e62009-06-29 20:52:51 +00005331 }
5332
5333 if (!Args.hasArg(options::OPT_nostdlib) &&
5334 !Args.hasArg(options::OPT_nostartfiles)) {
5335 if (!Args.hasArg(options::OPT_shared))
Chris Lattner3e2ee142010-07-07 16:01:42 +00005336 CmdArgs.push_back(Args.MakeArgString(
Daniel Dunbar9c3ed5f2010-07-14 18:46:23 +00005337 getToolChain().GetFilePath("crtend.o")));
Daniel Dunbar10de9e62009-06-29 20:52:51 +00005338 else
Chris Lattner3e2ee142010-07-07 16:01:42 +00005339 CmdArgs.push_back(Args.MakeArgString(
Daniel Dunbar9c3ed5f2010-07-14 18:46:23 +00005340 getToolChain().GetFilePath("crtendS.o")));
Daniel Dunbar10de9e62009-06-29 20:52:51 +00005341 }
5342
5343 const char *Exec =
Daniel Dunbar9c3ed5f2010-07-14 18:46:23 +00005344 Args.MakeArgString(getToolChain().GetProgramPath("ld"));
Daniel Dunbar7fbaf532010-08-02 02:38:28 +00005345 C.addCommand(new Command(JA, *this, Exec, CmdArgs));
Daniel Dunbar10de9e62009-06-29 20:52:51 +00005346}
Ed Schoutene33194b2009-04-02 19:13:12 +00005347
Eli Friedman9fa28852012-08-08 23:57:20 +00005348void bitrig::Assemble::ConstructJob(Compilation &C, const JobAction &JA,
5349 const InputInfo &Output,
5350 const InputInfoList &Inputs,
5351 const ArgList &Args,
5352 const char *LinkingOutput) const {
5353 ArgStringList CmdArgs;
5354
5355 Args.AddAllArgValues(CmdArgs, options::OPT_Wa_COMMA,
5356 options::OPT_Xassembler);
5357
5358 CmdArgs.push_back("-o");
5359 CmdArgs.push_back(Output.getFilename());
5360
5361 for (InputInfoList::const_iterator
5362 it = Inputs.begin(), ie = Inputs.end(); it != ie; ++it) {
5363 const InputInfo &II = *it;
5364 CmdArgs.push_back(II.getFilename());
5365 }
5366
5367 const char *Exec =
5368 Args.MakeArgString(getToolChain().GetProgramPath("as"));
5369 C.addCommand(new Command(JA, *this, Exec, CmdArgs));
5370}
5371
5372void bitrig::Link::ConstructJob(Compilation &C, const JobAction &JA,
5373 const InputInfo &Output,
5374 const InputInfoList &Inputs,
5375 const ArgList &Args,
5376 const char *LinkingOutput) const {
5377 const Driver &D = getToolChain().getDriver();
5378 ArgStringList CmdArgs;
5379
5380 if ((!Args.hasArg(options::OPT_nostdlib)) &&
5381 (!Args.hasArg(options::OPT_shared))) {
5382 CmdArgs.push_back("-e");
5383 CmdArgs.push_back("__start");
5384 }
5385
5386 if (Args.hasArg(options::OPT_static)) {
5387 CmdArgs.push_back("-Bstatic");
5388 } else {
5389 if (Args.hasArg(options::OPT_rdynamic))
5390 CmdArgs.push_back("-export-dynamic");
5391 CmdArgs.push_back("--eh-frame-hdr");
5392 CmdArgs.push_back("-Bdynamic");
5393 if (Args.hasArg(options::OPT_shared)) {
5394 CmdArgs.push_back("-shared");
5395 } else {
5396 CmdArgs.push_back("-dynamic-linker");
5397 CmdArgs.push_back("/usr/libexec/ld.so");
5398 }
5399 }
5400
5401 if (Output.isFilename()) {
5402 CmdArgs.push_back("-o");
5403 CmdArgs.push_back(Output.getFilename());
5404 } else {
5405 assert(Output.isNothing() && "Invalid output.");
5406 }
5407
5408 if (!Args.hasArg(options::OPT_nostdlib) &&
5409 !Args.hasArg(options::OPT_nostartfiles)) {
5410 if (!Args.hasArg(options::OPT_shared)) {
5411 if (Args.hasArg(options::OPT_pg))
5412 CmdArgs.push_back(Args.MakeArgString(
5413 getToolChain().GetFilePath("gcrt0.o")));
5414 else
5415 CmdArgs.push_back(Args.MakeArgString(
5416 getToolChain().GetFilePath("crt0.o")));
5417 CmdArgs.push_back(Args.MakeArgString(
5418 getToolChain().GetFilePath("crtbegin.o")));
5419 } else {
5420 CmdArgs.push_back(Args.MakeArgString(
5421 getToolChain().GetFilePath("crtbeginS.o")));
5422 }
5423 }
5424
5425 Args.AddAllArgs(CmdArgs, options::OPT_L);
5426 Args.AddAllArgs(CmdArgs, options::OPT_T_Group);
5427 Args.AddAllArgs(CmdArgs, options::OPT_e);
5428
5429 AddLinkerInputs(getToolChain(), Inputs, Args, CmdArgs);
5430
5431 if (!Args.hasArg(options::OPT_nostdlib) &&
5432 !Args.hasArg(options::OPT_nodefaultlibs)) {
Hans Wennborg70850d82013-07-18 20:29:38 +00005433 if (D.CCCIsCXX()) {
Eli Friedman9fa28852012-08-08 23:57:20 +00005434 getToolChain().AddCXXStdlibLibArgs(Args, CmdArgs);
5435 if (Args.hasArg(options::OPT_pg))
5436 CmdArgs.push_back("-lm_p");
5437 else
5438 CmdArgs.push_back("-lm");
5439 }
5440
Rafael Espindola1ad26f02012-10-23 17:07:31 +00005441 if (Args.hasArg(options::OPT_pthread)) {
5442 if (!Args.hasArg(options::OPT_shared) &&
5443 Args.hasArg(options::OPT_pg))
5444 CmdArgs.push_back("-lpthread_p");
5445 else
5446 CmdArgs.push_back("-lpthread");
5447 }
5448
Eli Friedman9fa28852012-08-08 23:57:20 +00005449 if (!Args.hasArg(options::OPT_shared)) {
5450 if (Args.hasArg(options::OPT_pg))
5451 CmdArgs.push_back("-lc_p");
5452 else
5453 CmdArgs.push_back("-lc");
5454 }
5455
5456 std::string myarch = "-lclang_rt.";
5457 const llvm::Triple &T = getToolChain().getTriple();
5458 llvm::Triple::ArchType Arch = T.getArch();
5459 switch (Arch) {
5460 case llvm::Triple::arm:
5461 myarch += ("arm");
5462 break;
5463 case llvm::Triple::x86:
5464 myarch += ("i386");
5465 break;
5466 case llvm::Triple::x86_64:
5467 myarch += ("amd64");
5468 break;
5469 default:
5470 assert(0 && "Unsupported architecture");
5471 }
5472 CmdArgs.push_back(Args.MakeArgString(myarch));
5473 }
5474
5475 if (!Args.hasArg(options::OPT_nostdlib) &&
5476 !Args.hasArg(options::OPT_nostartfiles)) {
5477 if (!Args.hasArg(options::OPT_shared))
5478 CmdArgs.push_back(Args.MakeArgString(
5479 getToolChain().GetFilePath("crtend.o")));
5480 else
5481 CmdArgs.push_back(Args.MakeArgString(
5482 getToolChain().GetFilePath("crtendS.o")));
5483 }
Eli Friedman5bb2ba02012-08-09 22:42:04 +00005484
5485 const char *Exec =
5486 Args.MakeArgString(getToolChain().GetProgramPath("ld"));
5487 C.addCommand(new Command(JA, *this, Exec, CmdArgs));
Eli Friedman9fa28852012-08-08 23:57:20 +00005488}
5489
Daniel Dunbar8eb473c2009-03-31 17:45:15 +00005490void freebsd::Assemble::ConstructJob(Compilation &C, const JobAction &JA,
Daniel Dunbar7fbaf532010-08-02 02:38:28 +00005491 const InputInfo &Output,
Daniel Dunbard067f7f2009-04-08 23:54:23 +00005492 const InputInfoList &Inputs,
5493 const ArgList &Args,
Mike Stump11289f42009-09-09 15:08:12 +00005494 const char *LinkingOutput) const {
Daniel Dunbar8eb473c2009-03-31 17:45:15 +00005495 ArgStringList CmdArgs;
5496
Daniel Dunbard854c8d2009-04-01 19:36:32 +00005497 // When building 32-bit code on FreeBSD/amd64, we have to explicitly
5498 // instruct as in the base system to assemble 32-bit code.
Eric Christopherc0f1a5e2012-09-05 21:32:44 +00005499 if (getToolChain().getArch() == llvm::Triple::x86)
Daniel Dunbar8eb473c2009-03-31 17:45:15 +00005500 CmdArgs.push_back("--32");
Eric Christopherc0f1a5e2012-09-05 21:32:44 +00005501 else if (getToolChain().getArch() == llvm::Triple::ppc)
Roman Divacky00859c22011-06-04 07:37:31 +00005502 CmdArgs.push_back("-a32");
Eric Christopherc0f1a5e2012-09-05 21:32:44 +00005503 else if (getToolChain().getArch() == llvm::Triple::mips ||
5504 getToolChain().getArch() == llvm::Triple::mipsel ||
5505 getToolChain().getArch() == llvm::Triple::mips64 ||
5506 getToolChain().getArch() == llvm::Triple::mips64el) {
5507 StringRef CPUName;
5508 StringRef ABIName;
Rafael Espindola22ce34a2013-08-20 22:12:08 +00005509 getMipsCPUAndABI(Args, getToolChain().getTriple(), CPUName, ABIName);
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00005510
Eric Christopherc0f1a5e2012-09-05 21:32:44 +00005511 CmdArgs.push_back("-march");
5512 CmdArgs.push_back(CPUName.data());
5513
Eric Christopherc0f1a5e2012-09-05 21:32:44 +00005514 CmdArgs.push_back("-mabi");
Simon Atanasyan0da400c2013-02-27 14:55:49 +00005515 CmdArgs.push_back(getGnuCompatibleMipsABIName(ABIName).data());
Eric Christopherc0f1a5e2012-09-05 21:32:44 +00005516
5517 if (getToolChain().getArch() == llvm::Triple::mips ||
5518 getToolChain().getArch() == llvm::Triple::mips64)
5519 CmdArgs.push_back("-EB");
5520 else
5521 CmdArgs.push_back("-EL");
5522
5523 Arg *LastPICArg = Args.getLastArg(options::OPT_fPIC, options::OPT_fno_PIC,
5524 options::OPT_fpic, options::OPT_fno_pic,
5525 options::OPT_fPIE, options::OPT_fno_PIE,
5526 options::OPT_fpie, options::OPT_fno_pie);
5527 if (LastPICArg &&
5528 (LastPICArg->getOption().matches(options::OPT_fPIC) ||
5529 LastPICArg->getOption().matches(options::OPT_fpic) ||
5530 LastPICArg->getOption().matches(options::OPT_fPIE) ||
5531 LastPICArg->getOption().matches(options::OPT_fpie))) {
5532 CmdArgs.push_back("-KPIC");
5533 }
Rafael Espindola0f207ed2012-12-13 04:17:14 +00005534 } else if (getToolChain().getArch() == llvm::Triple::arm ||
5535 getToolChain().getArch() == llvm::Triple::thumb) {
5536 CmdArgs.push_back("-mfpu=softvfp");
5537 switch(getToolChain().getTriple().getEnvironment()) {
5538 case llvm::Triple::GNUEABI:
5539 case llvm::Triple::EABI:
Anton Korobeynikov2bed8472013-03-18 07:59:20 +00005540 CmdArgs.push_back("-meabi=5");
Rafael Espindola0f207ed2012-12-13 04:17:14 +00005541 break;
5542
5543 default:
5544 CmdArgs.push_back("-matpcs");
5545 }
Eric Christopherc0f1a5e2012-09-05 21:32:44 +00005546 }
Eric Christopher0b26a612010-03-02 02:41:08 +00005547
Daniel Dunbar8eb473c2009-03-31 17:45:15 +00005548 Args.AddAllArgValues(CmdArgs, options::OPT_Wa_COMMA,
5549 options::OPT_Xassembler);
5550
5551 CmdArgs.push_back("-o");
Daniel Dunbarb440f562010-08-02 02:38:21 +00005552 CmdArgs.push_back(Output.getFilename());
Daniel Dunbar8eb473c2009-03-31 17:45:15 +00005553
5554 for (InputInfoList::const_iterator
5555 it = Inputs.begin(), ie = Inputs.end(); it != ie; ++it) {
5556 const InputInfo &II = *it;
Daniel Dunbarb440f562010-08-02 02:38:21 +00005557 CmdArgs.push_back(II.getFilename());
Daniel Dunbar8eb473c2009-03-31 17:45:15 +00005558 }
5559
Daniel Dunbard067f7f2009-04-08 23:54:23 +00005560 const char *Exec =
Daniel Dunbar9c3ed5f2010-07-14 18:46:23 +00005561 Args.MakeArgString(getToolChain().GetProgramPath("as"));
Daniel Dunbar7fbaf532010-08-02 02:38:28 +00005562 C.addCommand(new Command(JA, *this, Exec, CmdArgs));
Daniel Dunbar8eb473c2009-03-31 17:45:15 +00005563}
Daniel Dunbard854c8d2009-04-01 19:36:32 +00005564
5565void freebsd::Link::ConstructJob(Compilation &C, const JobAction &JA,
Daniel Dunbar7fbaf532010-08-02 02:38:28 +00005566 const InputInfo &Output,
Daniel Dunbard067f7f2009-04-08 23:54:23 +00005567 const InputInfoList &Inputs,
5568 const ArgList &Args,
Daniel Dunbare3e263f2009-05-02 20:14:53 +00005569 const char *LinkingOutput) const {
Roman Divackyafe2f232012-08-28 15:09:03 +00005570 const toolchains::FreeBSD& ToolChain =
5571 static_cast<const toolchains::FreeBSD&>(getToolChain());
5572 const Driver &D = ToolChain.getDriver();
Daniel Dunbard854c8d2009-04-01 19:36:32 +00005573 ArgStringList CmdArgs;
David Chisnall5f99f482012-07-29 15:24:44 +00005574
5575 // Silence warning for "clang -g foo.o -o foo"
5576 Args.ClaimAllArgs(options::OPT_g_Group);
5577 // and "clang -emit-llvm foo.o -o foo"
5578 Args.ClaimAllArgs(options::OPT_emit_llvm);
5579 // and for "clang -w foo.o -o foo". Other warning options are already
5580 // handled somewhere else.
5581 Args.ClaimAllArgs(options::OPT_w);
Daniel Dunbard854c8d2009-04-01 19:36:32 +00005582
Joerg Sonnenberger6165ab12011-03-21 13:51:29 +00005583 if (!D.SysRoot.empty())
5584 CmdArgs.push_back(Args.MakeArgString("--sysroot=" + D.SysRoot));
5585
Roman Divackyafe2f232012-08-28 15:09:03 +00005586 if (Args.hasArg(options::OPT_pie))
5587 CmdArgs.push_back("-pie");
5588
Daniel Dunbard854c8d2009-04-01 19:36:32 +00005589 if (Args.hasArg(options::OPT_static)) {
5590 CmdArgs.push_back("-Bstatic");
5591 } else {
Rafael Espindola7ba97af2010-11-11 02:17:51 +00005592 if (Args.hasArg(options::OPT_rdynamic))
5593 CmdArgs.push_back("-export-dynamic");
Daniel Dunbard854c8d2009-04-01 19:36:32 +00005594 CmdArgs.push_back("--eh-frame-hdr");
5595 if (Args.hasArg(options::OPT_shared)) {
5596 CmdArgs.push_back("-Bshareable");
5597 } else {
5598 CmdArgs.push_back("-dynamic-linker");
5599 CmdArgs.push_back("/libexec/ld-elf.so.1");
5600 }
Roman Divackyafe2f232012-08-28 15:09:03 +00005601 if (ToolChain.getTriple().getOSMajorVersion() >= 9) {
5602 llvm::Triple::ArchType Arch = ToolChain.getArch();
David Chisnall5f99f482012-07-29 15:24:44 +00005603 if (Arch == llvm::Triple::arm || Arch == llvm::Triple::sparc ||
5604 Arch == llvm::Triple::x86 || Arch == llvm::Triple::x86_64) {
5605 CmdArgs.push_back("--hash-style=both");
5606 }
5607 }
5608 CmdArgs.push_back("--enable-new-dtags");
Daniel Dunbard854c8d2009-04-01 19:36:32 +00005609 }
5610
5611 // When building 32-bit code on FreeBSD/amd64, we have to explicitly
5612 // instruct ld in the base system to link 32-bit code.
Rafael Espindola35ca7d92012-10-07 04:44:33 +00005613 if (ToolChain.getArch() == llvm::Triple::x86) {
Daniel Dunbard854c8d2009-04-01 19:36:32 +00005614 CmdArgs.push_back("-m");
5615 CmdArgs.push_back("elf_i386_fbsd");
5616 }
5617
Rafael Espindola35ca7d92012-10-07 04:44:33 +00005618 if (ToolChain.getArch() == llvm::Triple::ppc) {
Roman Divacky5e300b82011-06-04 07:40:24 +00005619 CmdArgs.push_back("-m");
Roman Divackyd150ad32011-11-21 16:50:32 +00005620 CmdArgs.push_back("elf32ppc_fbsd");
Roman Divacky5e300b82011-06-04 07:40:24 +00005621 }
5622
Daniel Dunbarb440f562010-08-02 02:38:21 +00005623 if (Output.isFilename()) {
Daniel Dunbard854c8d2009-04-01 19:36:32 +00005624 CmdArgs.push_back("-o");
5625 CmdArgs.push_back(Output.getFilename());
5626 } else {
5627 assert(Output.isNothing() && "Invalid output.");
5628 }
5629
5630 if (!Args.hasArg(options::OPT_nostdlib) &&
5631 !Args.hasArg(options::OPT_nostartfiles)) {
Roman Divackyafe2f232012-08-28 15:09:03 +00005632 const char *crt1 = NULL;
Daniel Dunbard854c8d2009-04-01 19:36:32 +00005633 if (!Args.hasArg(options::OPT_shared)) {
Roman Divacky66f22762011-02-10 16:59:40 +00005634 if (Args.hasArg(options::OPT_pg))
Roman Divackyafe2f232012-08-28 15:09:03 +00005635 crt1 = "gcrt1.o";
5636 else if (Args.hasArg(options::OPT_pie))
5637 crt1 = "Scrt1.o";
5638 else
5639 crt1 = "crt1.o";
Daniel Dunbard854c8d2009-04-01 19:36:32 +00005640 }
Roman Divackyafe2f232012-08-28 15:09:03 +00005641 if (crt1)
5642 CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath(crt1)));
5643
5644 CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath("crti.o")));
5645
5646 const char *crtbegin = NULL;
5647 if (Args.hasArg(options::OPT_static))
5648 crtbegin = "crtbeginT.o";
5649 else if (Args.hasArg(options::OPT_shared) || Args.hasArg(options::OPT_pie))
5650 crtbegin = "crtbeginS.o";
5651 else
5652 crtbegin = "crtbegin.o";
5653
5654 CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath(crtbegin)));
Daniel Dunbard854c8d2009-04-01 19:36:32 +00005655 }
5656
5657 Args.AddAllArgs(CmdArgs, options::OPT_L);
Roman Divackyafe2f232012-08-28 15:09:03 +00005658 const ToolChain::path_list Paths = ToolChain.getFilePaths();
Roman Divackyee8188a2011-03-01 17:53:14 +00005659 for (ToolChain::path_list::const_iterator i = Paths.begin(), e = Paths.end();
5660 i != e; ++i)
Chris Lattner0e62c1c2011-07-23 10:55:15 +00005661 CmdArgs.push_back(Args.MakeArgString(StringRef("-L") + *i));
Daniel Dunbard854c8d2009-04-01 19:36:32 +00005662 Args.AddAllArgs(CmdArgs, options::OPT_T_Group);
5663 Args.AddAllArgs(CmdArgs, options::OPT_e);
David Chisnall589a4942010-08-15 22:58:12 +00005664 Args.AddAllArgs(CmdArgs, options::OPT_s);
5665 Args.AddAllArgs(CmdArgs, options::OPT_t);
5666 Args.AddAllArgs(CmdArgs, options::OPT_Z_Flag);
5667 Args.AddAllArgs(CmdArgs, options::OPT_r);
Daniel Dunbard854c8d2009-04-01 19:36:32 +00005668
Roman Divackyafe2f232012-08-28 15:09:03 +00005669 AddLinkerInputs(ToolChain, Inputs, Args, CmdArgs);
Daniel Dunbard854c8d2009-04-01 19:36:32 +00005670
5671 if (!Args.hasArg(options::OPT_nostdlib) &&
5672 !Args.hasArg(options::OPT_nodefaultlibs)) {
Hans Wennborg70850d82013-07-18 20:29:38 +00005673 if (D.CCCIsCXX()) {
Roman Divackyafe2f232012-08-28 15:09:03 +00005674 ToolChain.AddCXXStdlibLibArgs(Args, CmdArgs);
Roman Divacky66f22762011-02-10 16:59:40 +00005675 if (Args.hasArg(options::OPT_pg))
5676 CmdArgs.push_back("-lm_p");
5677 else
5678 CmdArgs.push_back("-lm");
Daniel Dunbar4b8ef282010-02-17 08:07:51 +00005679 }
Daniel Dunbard854c8d2009-04-01 19:36:32 +00005680 // FIXME: For some reason GCC passes -lgcc and -lgcc_s before adding
5681 // the default system libraries. Just mimic this for now.
Roman Divacky66f22762011-02-10 16:59:40 +00005682 if (Args.hasArg(options::OPT_pg))
5683 CmdArgs.push_back("-lgcc_p");
5684 else
5685 CmdArgs.push_back("-lgcc");
Daniel Dunbard854c8d2009-04-01 19:36:32 +00005686 if (Args.hasArg(options::OPT_static)) {
5687 CmdArgs.push_back("-lgcc_eh");
Roman Divacky66f22762011-02-10 16:59:40 +00005688 } else if (Args.hasArg(options::OPT_pg)) {
5689 CmdArgs.push_back("-lgcc_eh_p");
Daniel Dunbard854c8d2009-04-01 19:36:32 +00005690 } else {
5691 CmdArgs.push_back("--as-needed");
5692 CmdArgs.push_back("-lgcc_s");
5693 CmdArgs.push_back("--no-as-needed");
5694 }
5695
Matt Beaumont-Gay1fe49152011-02-10 20:35:01 +00005696 if (Args.hasArg(options::OPT_pthread)) {
Roman Divacky66f22762011-02-10 16:59:40 +00005697 if (Args.hasArg(options::OPT_pg))
5698 CmdArgs.push_back("-lpthread_p");
5699 else
5700 CmdArgs.push_back("-lpthread");
Matt Beaumont-Gay1fe49152011-02-10 20:35:01 +00005701 }
Daniel Dunbard854c8d2009-04-01 19:36:32 +00005702
Roman Divacky66f22762011-02-10 16:59:40 +00005703 if (Args.hasArg(options::OPT_pg)) {
5704 if (Args.hasArg(options::OPT_shared))
5705 CmdArgs.push_back("-lc");
5706 else
5707 CmdArgs.push_back("-lc_p");
5708 CmdArgs.push_back("-lgcc_p");
5709 } else {
5710 CmdArgs.push_back("-lc");
5711 CmdArgs.push_back("-lgcc");
5712 }
5713
Daniel Dunbard854c8d2009-04-01 19:36:32 +00005714 if (Args.hasArg(options::OPT_static)) {
5715 CmdArgs.push_back("-lgcc_eh");
Roman Divacky66f22762011-02-10 16:59:40 +00005716 } else if (Args.hasArg(options::OPT_pg)) {
5717 CmdArgs.push_back("-lgcc_eh_p");
Daniel Dunbard854c8d2009-04-01 19:36:32 +00005718 } else {
5719 CmdArgs.push_back("--as-needed");
5720 CmdArgs.push_back("-lgcc_s");
5721 CmdArgs.push_back("--no-as-needed");
5722 }
5723 }
5724
5725 if (!Args.hasArg(options::OPT_nostdlib) &&
5726 !Args.hasArg(options::OPT_nostartfiles)) {
Roman Divackya3c50242012-09-07 13:36:21 +00005727 if (Args.hasArg(options::OPT_shared) || Args.hasArg(options::OPT_pie))
Roman Divackyafe2f232012-08-28 15:09:03 +00005728 CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath("crtendS.o")));
Roman Divackya3c50242012-09-07 13:36:21 +00005729 else
5730 CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath("crtend.o")));
Roman Divackyafe2f232012-08-28 15:09:03 +00005731 CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath("crtn.o")));
Daniel Dunbard854c8d2009-04-01 19:36:32 +00005732 }
5733
Roman Divackyafe2f232012-08-28 15:09:03 +00005734 addProfileRT(ToolChain, Args, CmdArgs, ToolChain.getTriple());
Nick Lewycky82fe5f42011-05-24 21:54:59 +00005735
Daniel Dunbard067f7f2009-04-08 23:54:23 +00005736 const char *Exec =
Roman Divackyafe2f232012-08-28 15:09:03 +00005737 Args.MakeArgString(ToolChain.GetProgramPath("ld"));
Daniel Dunbar7fbaf532010-08-02 02:38:28 +00005738 C.addCommand(new Command(JA, *this, Exec, CmdArgs));
Daniel Dunbard854c8d2009-04-01 19:36:32 +00005739}
Daniel Dunbarcc912342009-05-02 18:28:39 +00005740
Benjamin Kramer24f1d3e2011-02-02 18:59:27 +00005741void netbsd::Assemble::ConstructJob(Compilation &C, const JobAction &JA,
5742 const InputInfo &Output,
5743 const InputInfoList &Inputs,
5744 const ArgList &Args,
5745 const char *LinkingOutput) const {
5746 ArgStringList CmdArgs;
5747
5748 // When building 32-bit code on NetBSD/amd64, we have to explicitly
5749 // instruct as in the base system to assemble 32-bit code.
Joerg Sonnenbergerd64c60e2012-01-26 22:27:52 +00005750 if (getToolChain().getArch() == llvm::Triple::x86)
Benjamin Kramer24f1d3e2011-02-02 18:59:27 +00005751 CmdArgs.push_back("--32");
5752
Benjamin Kramer24f1d3e2011-02-02 18:59:27 +00005753 // Set byte order explicitly
Rafael Espindola35ca7d92012-10-07 04:44:33 +00005754 if (getToolChain().getArch() == llvm::Triple::mips)
Benjamin Kramer24f1d3e2011-02-02 18:59:27 +00005755 CmdArgs.push_back("-EB");
Rafael Espindola35ca7d92012-10-07 04:44:33 +00005756 else if (getToolChain().getArch() == llvm::Triple::mipsel)
Benjamin Kramer24f1d3e2011-02-02 18:59:27 +00005757 CmdArgs.push_back("-EL");
5758
5759 Args.AddAllArgValues(CmdArgs, options::OPT_Wa_COMMA,
5760 options::OPT_Xassembler);
5761
5762 CmdArgs.push_back("-o");
5763 CmdArgs.push_back(Output.getFilename());
5764
5765 for (InputInfoList::const_iterator
5766 it = Inputs.begin(), ie = Inputs.end(); it != ie; ++it) {
5767 const InputInfo &II = *it;
5768 CmdArgs.push_back(II.getFilename());
5769 }
5770
David Chisnallddbd68f2011-09-27 22:03:18 +00005771 const char *Exec = Args.MakeArgString((getToolChain().GetProgramPath("as")));
Benjamin Kramer24f1d3e2011-02-02 18:59:27 +00005772 C.addCommand(new Command(JA, *this, Exec, CmdArgs));
5773}
5774
5775void netbsd::Link::ConstructJob(Compilation &C, const JobAction &JA,
5776 const InputInfo &Output,
5777 const InputInfoList &Inputs,
5778 const ArgList &Args,
5779 const char *LinkingOutput) const {
5780 const Driver &D = getToolChain().getDriver();
5781 ArgStringList CmdArgs;
5782
Joerg Sonnenberger6165ab12011-03-21 13:51:29 +00005783 if (!D.SysRoot.empty())
5784 CmdArgs.push_back(Args.MakeArgString("--sysroot=" + D.SysRoot));
5785
Benjamin Kramer24f1d3e2011-02-02 18:59:27 +00005786 if (Args.hasArg(options::OPT_static)) {
5787 CmdArgs.push_back("-Bstatic");
5788 } else {
5789 if (Args.hasArg(options::OPT_rdynamic))
5790 CmdArgs.push_back("-export-dynamic");
5791 CmdArgs.push_back("--eh-frame-hdr");
5792 if (Args.hasArg(options::OPT_shared)) {
5793 CmdArgs.push_back("-Bshareable");
5794 } else {
5795 CmdArgs.push_back("-dynamic-linker");
5796 CmdArgs.push_back("/libexec/ld.elf_so");
5797 }
5798 }
5799
5800 // When building 32-bit code on NetBSD/amd64, we have to explicitly
5801 // instruct ld in the base system to link 32-bit code.
Joerg Sonnenbergerd64c60e2012-01-26 22:27:52 +00005802 if (getToolChain().getArch() == llvm::Triple::x86) {
Benjamin Kramer24f1d3e2011-02-02 18:59:27 +00005803 CmdArgs.push_back("-m");
5804 CmdArgs.push_back("elf_i386");
5805 }
5806
5807 if (Output.isFilename()) {
5808 CmdArgs.push_back("-o");
5809 CmdArgs.push_back(Output.getFilename());
5810 } else {
5811 assert(Output.isNothing() && "Invalid output.");
5812 }
5813
5814 if (!Args.hasArg(options::OPT_nostdlib) &&
5815 !Args.hasArg(options::OPT_nostartfiles)) {
5816 if (!Args.hasArg(options::OPT_shared)) {
5817 CmdArgs.push_back(Args.MakeArgString(
5818 getToolChain().GetFilePath("crt0.o")));
5819 CmdArgs.push_back(Args.MakeArgString(
5820 getToolChain().GetFilePath("crti.o")));
5821 CmdArgs.push_back(Args.MakeArgString(
5822 getToolChain().GetFilePath("crtbegin.o")));
5823 } else {
5824 CmdArgs.push_back(Args.MakeArgString(
5825 getToolChain().GetFilePath("crti.o")));
5826 CmdArgs.push_back(Args.MakeArgString(
5827 getToolChain().GetFilePath("crtbeginS.o")));
5828 }
5829 }
5830
5831 Args.AddAllArgs(CmdArgs, options::OPT_L);
5832 Args.AddAllArgs(CmdArgs, options::OPT_T_Group);
5833 Args.AddAllArgs(CmdArgs, options::OPT_e);
5834 Args.AddAllArgs(CmdArgs, options::OPT_s);
5835 Args.AddAllArgs(CmdArgs, options::OPT_t);
5836 Args.AddAllArgs(CmdArgs, options::OPT_Z_Flag);
5837 Args.AddAllArgs(CmdArgs, options::OPT_r);
5838
5839 AddLinkerInputs(getToolChain(), Inputs, Args, CmdArgs);
5840
5841 if (!Args.hasArg(options::OPT_nostdlib) &&
5842 !Args.hasArg(options::OPT_nodefaultlibs)) {
Hans Wennborg70850d82013-07-18 20:29:38 +00005843 if (D.CCCIsCXX()) {
Benjamin Kramer24f1d3e2011-02-02 18:59:27 +00005844 getToolChain().AddCXXStdlibLibArgs(Args, CmdArgs);
5845 CmdArgs.push_back("-lm");
5846 }
5847 // FIXME: For some reason GCC passes -lgcc and -lgcc_s before adding
5848 // the default system libraries. Just mimic this for now.
Benjamin Kramer24f1d3e2011-02-02 18:59:27 +00005849 if (Args.hasArg(options::OPT_static)) {
5850 CmdArgs.push_back("-lgcc_eh");
5851 } else {
5852 CmdArgs.push_back("--as-needed");
5853 CmdArgs.push_back("-lgcc_s");
5854 CmdArgs.push_back("--no-as-needed");
5855 }
Joerg Sonnenberger87717772011-06-07 23:39:17 +00005856 CmdArgs.push_back("-lgcc");
Benjamin Kramer24f1d3e2011-02-02 18:59:27 +00005857
5858 if (Args.hasArg(options::OPT_pthread))
5859 CmdArgs.push_back("-lpthread");
5860 CmdArgs.push_back("-lc");
5861
5862 CmdArgs.push_back("-lgcc");
5863 if (Args.hasArg(options::OPT_static)) {
5864 CmdArgs.push_back("-lgcc_eh");
5865 } else {
5866 CmdArgs.push_back("--as-needed");
5867 CmdArgs.push_back("-lgcc_s");
5868 CmdArgs.push_back("--no-as-needed");
5869 }
5870 }
5871
5872 if (!Args.hasArg(options::OPT_nostdlib) &&
5873 !Args.hasArg(options::OPT_nostartfiles)) {
5874 if (!Args.hasArg(options::OPT_shared))
5875 CmdArgs.push_back(Args.MakeArgString(getToolChain().GetFilePath(
5876 "crtend.o")));
5877 else
5878 CmdArgs.push_back(Args.MakeArgString(getToolChain().GetFilePath(
5879 "crtendS.o")));
5880 CmdArgs.push_back(Args.MakeArgString(getToolChain().GetFilePath(
5881 "crtn.o")));
5882 }
5883
Bill Wendling08760582011-06-27 19:15:03 +00005884 addProfileRT(getToolChain(), Args, CmdArgs, getToolChain().getTriple());
Nick Lewycky82fe5f42011-05-24 21:54:59 +00005885
David Chisnallddbd68f2011-09-27 22:03:18 +00005886 const char *Exec = Args.MakeArgString(getToolChain().GetProgramPath("ld"));
Benjamin Kramer24f1d3e2011-02-02 18:59:27 +00005887 C.addCommand(new Command(JA, *this, Exec, CmdArgs));
5888}
5889
Thomas Schwinge4e555262013-03-28 19:04:25 +00005890void gnutools::Assemble::ConstructJob(Compilation &C, const JobAction &JA,
5891 const InputInfo &Output,
5892 const InputInfoList &Inputs,
5893 const ArgList &Args,
5894 const char *LinkingOutput) const {
Rafael Espindola92b00932010-08-10 00:25:48 +00005895 ArgStringList CmdArgs;
5896
5897 // Add --32/--64 to make sure we get the format we want.
5898 // This is incomplete
5899 if (getToolChain().getArch() == llvm::Triple::x86) {
5900 CmdArgs.push_back("--32");
5901 } else if (getToolChain().getArch() == llvm::Triple::x86_64) {
5902 CmdArgs.push_back("--64");
Eli Friedman4a4932c2011-11-28 23:46:52 +00005903 } else if (getToolChain().getArch() == llvm::Triple::ppc) {
5904 CmdArgs.push_back("-a32");
5905 CmdArgs.push_back("-mppc");
5906 CmdArgs.push_back("-many");
5907 } else if (getToolChain().getArch() == llvm::Triple::ppc64) {
5908 CmdArgs.push_back("-a64");
5909 CmdArgs.push_back("-mppc64");
5910 CmdArgs.push_back("-many");
Bill Schmidt778d3872013-07-26 01:36:11 +00005911 } else if (getToolChain().getArch() == llvm::Triple::ppc64le) {
5912 CmdArgs.push_back("-a64");
5913 CmdArgs.push_back("-mppc64le");
5914 CmdArgs.push_back("-many");
Rafael Espindola92b00932010-08-10 00:25:48 +00005915 } else if (getToolChain().getArch() == llvm::Triple::arm) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00005916 StringRef MArch = getToolChain().getArchName();
Rafael Espindola92b00932010-08-10 00:25:48 +00005917 if (MArch == "armv7" || MArch == "armv7a" || MArch == "armv7-a")
5918 CmdArgs.push_back("-mfpu=neon");
Evgeniy Stepanoved943f62012-04-20 09:03:40 +00005919
5920 StringRef ARMFloatABI = getARMFloatABI(getToolChain().getDriver(), Args,
5921 getToolChain().getTriple());
5922 CmdArgs.push_back(Args.MakeArgString("-mfloat-abi=" + ARMFloatABI));
Evgeniy Stepanov582911a2012-04-24 09:05:31 +00005923
5924 Args.AddLastArg(CmdArgs, options::OPT_march_EQ);
5925 Args.AddLastArg(CmdArgs, options::OPT_mcpu_EQ);
5926 Args.AddLastArg(CmdArgs, options::OPT_mfpu_EQ);
Akira Hatanaka5e9dde32011-11-30 19:31:38 +00005927 } else if (getToolChain().getArch() == llvm::Triple::mips ||
5928 getToolChain().getArch() == llvm::Triple::mipsel ||
5929 getToolChain().getArch() == llvm::Triple::mips64 ||
5930 getToolChain().getArch() == llvm::Triple::mips64el) {
Simon Atanasyan571d7bd2012-04-07 22:31:29 +00005931 StringRef CPUName;
5932 StringRef ABIName;
Rafael Espindola22ce34a2013-08-20 22:12:08 +00005933 getMipsCPUAndABI(Args, getToolChain().getTriple(), CPUName, ABIName);
Akira Hatanaka5e9dde32011-11-30 19:31:38 +00005934
Simon Atanasyan571d7bd2012-04-07 22:31:29 +00005935 CmdArgs.push_back("-march");
5936 CmdArgs.push_back(CPUName.data());
5937
Simon Atanasyan571d7bd2012-04-07 22:31:29 +00005938 CmdArgs.push_back("-mabi");
Simon Atanasyan0da400c2013-02-27 14:55:49 +00005939 CmdArgs.push_back(getGnuCompatibleMipsABIName(ABIName).data());
Simon Atanasyan2390aa12012-04-06 19:15:24 +00005940
5941 if (getToolChain().getArch() == llvm::Triple::mips ||
5942 getToolChain().getArch() == llvm::Triple::mips64)
5943 CmdArgs.push_back("-EB");
5944 else
5945 CmdArgs.push_back("-EL");
Simon Atanasyan217dc2d2012-05-29 19:07:33 +00005946
Simon Atanasyan036d16d2013-04-30 07:47:13 +00005947 Args.AddLastArg(CmdArgs, options::OPT_mips16, options::OPT_mno_mips16);
5948 Args.AddLastArg(CmdArgs, options::OPT_mmicromips,
5949 options::OPT_mno_micromips);
5950 Args.AddLastArg(CmdArgs, options::OPT_mdsp, options::OPT_mno_dsp);
5951 Args.AddLastArg(CmdArgs, options::OPT_mdspr2, options::OPT_mno_dspr2);
5952
Simon Atanasyan217dc2d2012-05-29 19:07:33 +00005953 Arg *LastPICArg = Args.getLastArg(options::OPT_fPIC, options::OPT_fno_PIC,
5954 options::OPT_fpic, options::OPT_fno_pic,
5955 options::OPT_fPIE, options::OPT_fno_PIE,
5956 options::OPT_fpie, options::OPT_fno_pie);
5957 if (LastPICArg &&
5958 (LastPICArg->getOption().matches(options::OPT_fPIC) ||
5959 LastPICArg->getOption().matches(options::OPT_fpic) ||
5960 LastPICArg->getOption().matches(options::OPT_fPIE) ||
5961 LastPICArg->getOption().matches(options::OPT_fpie))) {
5962 CmdArgs.push_back("-KPIC");
5963 }
Ulrich Weigand47445072013-05-06 16:26:41 +00005964 } else if (getToolChain().getArch() == llvm::Triple::systemz) {
Richard Sandiford4652d892013-07-19 16:51:51 +00005965 // Always pass an -march option, since our default of z10 is later
5966 // than the GNU assembler's default.
5967 StringRef CPUName = getSystemZTargetCPU(Args);
5968 CmdArgs.push_back(Args.MakeArgString("-march=" + CPUName));
Rafael Espindola92b00932010-08-10 00:25:48 +00005969 }
5970
5971 Args.AddAllArgValues(CmdArgs, options::OPT_Wa_COMMA,
5972 options::OPT_Xassembler);
5973
5974 CmdArgs.push_back("-o");
5975 CmdArgs.push_back(Output.getFilename());
5976
5977 for (InputInfoList::const_iterator
5978 it = Inputs.begin(), ie = Inputs.end(); it != ie; ++it) {
5979 const InputInfo &II = *it;
5980 CmdArgs.push_back(II.getFilename());
5981 }
5982
5983 const char *Exec =
5984 Args.MakeArgString(getToolChain().GetProgramPath("as"));
5985 C.addCommand(new Command(JA, *this, Exec, CmdArgs));
Eric Christopher30aa6b62013-06-05 23:58:15 +00005986
5987 // Handle the debug info splitting at object creation time if we're
5988 // creating an object.
5989 // TODO: Currently only works on linux with newer objcopy.
5990 if (Args.hasArg(options::OPT_gsplit_dwarf) &&
Cameron Esfahani556d91e2013-09-14 01:09:11 +00005991 getToolChain().getTriple().isOSLinux())
Eric Christopher30aa6b62013-06-05 23:58:15 +00005992 SplitDebugInfo(getToolChain(), C, *this, JA, Args, Output,
5993 SplitDebugName(Args, Inputs));
Rafael Espindola92b00932010-08-10 00:25:48 +00005994}
5995
Evgeniy Stepanov77866712012-04-25 08:59:22 +00005996static void AddLibgcc(llvm::Triple Triple, const Driver &D,
5997 ArgStringList &CmdArgs, const ArgList &Args) {
Logan Chienc6fd8202012-09-02 09:30:11 +00005998 bool isAndroid = Triple.getEnvironment() == llvm::Triple::Android;
Chandler Carruth58d6eb62013-03-04 02:07:55 +00005999 bool StaticLibgcc = Args.hasArg(options::OPT_static_libgcc) ||
6000 Args.hasArg(options::OPT_static);
Hans Wennborg70850d82013-07-18 20:29:38 +00006001 if (!D.CCCIsCXX())
Rafael Espindolacc354322011-10-17 21:39:04 +00006002 CmdArgs.push_back("-lgcc");
6003
Logan Chien3d3373c2012-11-19 12:04:11 +00006004 if (StaticLibgcc || isAndroid) {
Hans Wennborg70850d82013-07-18 20:29:38 +00006005 if (D.CCCIsCXX())
Rafael Espindolacc354322011-10-17 21:39:04 +00006006 CmdArgs.push_back("-lgcc");
6007 } else {
Hans Wennborg70850d82013-07-18 20:29:38 +00006008 if (!D.CCCIsCXX())
Rafael Espindolacc354322011-10-17 21:39:04 +00006009 CmdArgs.push_back("--as-needed");
6010 CmdArgs.push_back("-lgcc_s");
Hans Wennborg70850d82013-07-18 20:29:38 +00006011 if (!D.CCCIsCXX())
Rafael Espindolacc354322011-10-17 21:39:04 +00006012 CmdArgs.push_back("--no-as-needed");
6013 }
6014
Evgeniy Stepanov77866712012-04-25 08:59:22 +00006015 if (StaticLibgcc && !isAndroid)
Rafael Espindolacc354322011-10-17 21:39:04 +00006016 CmdArgs.push_back("-lgcc_eh");
Hans Wennborg70850d82013-07-18 20:29:38 +00006017 else if (!Args.hasArg(options::OPT_shared) && D.CCCIsCXX())
Rafael Espindolacc354322011-10-17 21:39:04 +00006018 CmdArgs.push_back("-lgcc");
Logan Chien3d3373c2012-11-19 12:04:11 +00006019
6020 // According to Android ABI, we have to link with libdl if we are
6021 // linking with non-static libgcc.
6022 //
6023 // NOTE: This fixes a link error on Android MIPS as well. The non-static
6024 // libgcc for MIPS relies on _Unwind_Find_FDE and dl_iterate_phdr from libdl.
6025 if (isAndroid && !StaticLibgcc)
6026 CmdArgs.push_back("-ldl");
Rafael Espindolacc354322011-10-17 21:39:04 +00006027}
6028
Simon Atanasyan2d1b1ad2012-10-21 11:44:57 +00006029static bool hasMipsN32ABIArg(const ArgList &Args) {
6030 Arg *A = Args.getLastArg(options::OPT_mabi_EQ);
Richard Smithbd55daf2012-11-01 04:30:05 +00006031 return A && (A->getValue() == StringRef("n32"));
Simon Atanasyan2d1b1ad2012-10-21 11:44:57 +00006032}
6033
Peter Collingbourne9d9e1fc2013-05-27 21:40:20 +00006034static StringRef getLinuxDynamicLinker(const ArgList &Args,
6035 const toolchains::Linux &ToolChain) {
6036 if (ToolChain.getTriple().getEnvironment() == llvm::Triple::Android)
6037 return "/system/bin/linker";
6038 else if (ToolChain.getArch() == llvm::Triple::x86)
6039 return "/lib/ld-linux.so.2";
6040 else if (ToolChain.getArch() == llvm::Triple::aarch64)
6041 return "/lib/ld-linux-aarch64.so.1";
6042 else if (ToolChain.getArch() == llvm::Triple::arm ||
6043 ToolChain.getArch() == llvm::Triple::thumb) {
6044 if (ToolChain.getTriple().getEnvironment() == llvm::Triple::GNUEABIHF)
6045 return "/lib/ld-linux-armhf.so.3";
6046 else
6047 return "/lib/ld-linux.so.3";
6048 } else if (ToolChain.getArch() == llvm::Triple::mips ||
6049 ToolChain.getArch() == llvm::Triple::mipsel)
6050 return "/lib/ld.so.1";
6051 else if (ToolChain.getArch() == llvm::Triple::mips64 ||
6052 ToolChain.getArch() == llvm::Triple::mips64el) {
6053 if (hasMipsN32ABIArg(Args))
6054 return "/lib32/ld.so.1";
6055 else
6056 return "/lib64/ld.so.1";
6057 } else if (ToolChain.getArch() == llvm::Triple::ppc)
6058 return "/lib/ld.so.1";
6059 else if (ToolChain.getArch() == llvm::Triple::ppc64 ||
Bill Schmidt778d3872013-07-26 01:36:11 +00006060 ToolChain.getArch() == llvm::Triple::ppc64le ||
Peter Collingbourne9d9e1fc2013-05-27 21:40:20 +00006061 ToolChain.getArch() == llvm::Triple::systemz)
6062 return "/lib64/ld64.so.1";
6063 else
6064 return "/lib64/ld-linux-x86-64.so.2";
6065}
6066
Thomas Schwinge4e555262013-03-28 19:04:25 +00006067void gnutools::Link::ConstructJob(Compilation &C, const JobAction &JA,
6068 const InputInfo &Output,
6069 const InputInfoList &Inputs,
6070 const ArgList &Args,
6071 const char *LinkingOutput) const {
Rafael Espindolac8f008f2010-11-07 20:14:31 +00006072 const toolchains::Linux& ToolChain =
6073 static_cast<const toolchains::Linux&>(getToolChain());
6074 const Driver &D = ToolChain.getDriver();
Rafael Espindola7df35012012-11-02 20:41:30 +00006075 const bool isAndroid =
6076 ToolChain.getTriple().getEnvironment() == llvm::Triple::Android;
Alexey Samsonov609213f92013-08-19 09:14:21 +00006077 const SanitizerArgs &Sanitize = D.getOrParseSanitizerArgs(Args);
Peter Collingbourne54d770c2013-04-09 04:35:11 +00006078 const bool IsPIE =
6079 !Args.hasArg(options::OPT_shared) &&
Alexey Samsonove6203662013-08-09 07:42:13 +00006080 (Args.hasArg(options::OPT_pie) || Sanitize.hasZeroBaseShadow(ToolChain));
Evgeniy Stepanov77866712012-04-25 08:59:22 +00006081
Rafael Espindolac8f008f2010-11-07 20:14:31 +00006082 ArgStringList CmdArgs;
6083
Rafael Espindolad1002f62010-11-15 18:28:16 +00006084 // Silence warning for "clang -g foo.o -o foo"
6085 Args.ClaimAllArgs(options::OPT_g_Group);
Rafael Espindolad95a8122011-03-01 05:25:27 +00006086 // and "clang -emit-llvm foo.o -o foo"
6087 Args.ClaimAllArgs(options::OPT_emit_llvm);
David Chisnall5f99f482012-07-29 15:24:44 +00006088 // and for "clang -w foo.o -o foo". Other warning options are already
Rafael Espindolaf92614c2010-11-17 20:37:10 +00006089 // handled somewhere else.
6090 Args.ClaimAllArgs(options::OPT_w);
Rafael Espindolad1002f62010-11-15 18:28:16 +00006091
Joerg Sonnenberger6165ab12011-03-21 13:51:29 +00006092 if (!D.SysRoot.empty())
6093 CmdArgs.push_back(Args.MakeArgString("--sysroot=" + D.SysRoot));
Rafael Espindolac8f008f2010-11-07 20:14:31 +00006094
Peter Collingbourne54d770c2013-04-09 04:35:11 +00006095 if (IsPIE)
Rafael Espindolad47ac232010-11-17 22:26:15 +00006096 CmdArgs.push_back("-pie");
6097
Rafael Espindola1c76c592010-11-07 22:57:16 +00006098 if (Args.hasArg(options::OPT_rdynamic))
6099 CmdArgs.push_back("-export-dynamic");
6100
Rafael Espindola34d77dc2010-11-11 19:34:42 +00006101 if (Args.hasArg(options::OPT_s))
6102 CmdArgs.push_back("-s");
6103
Rafael Espindolac8f008f2010-11-07 20:14:31 +00006104 for (std::vector<std::string>::const_iterator i = ToolChain.ExtraOpts.begin(),
6105 e = ToolChain.ExtraOpts.end();
6106 i != e; ++i)
6107 CmdArgs.push_back(i->c_str());
6108
6109 if (!Args.hasArg(options::OPT_static)) {
6110 CmdArgs.push_back("--eh-frame-hdr");
6111 }
6112
6113 CmdArgs.push_back("-m");
6114 if (ToolChain.getArch() == llvm::Triple::x86)
6115 CmdArgs.push_back("elf_i386");
Tim Northover9bb857a2013-01-31 12:13:10 +00006116 else if (ToolChain.getArch() == llvm::Triple::aarch64)
6117 CmdArgs.push_back("aarch64linux");
Eric Christopher84fbdb42011-08-19 00:30:14 +00006118 else if (ToolChain.getArch() == llvm::Triple::arm
Douglas Gregord9bb1522011-03-06 19:11:49 +00006119 || ToolChain.getArch() == llvm::Triple::thumb)
Rafael Espindolac8f008f2010-11-07 20:14:31 +00006120 CmdArgs.push_back("armelf_linux_eabi");
Ted Kremenek43d47cc2011-04-05 22:04:27 +00006121 else if (ToolChain.getArch() == llvm::Triple::ppc)
6122 CmdArgs.push_back("elf32ppclinux");
6123 else if (ToolChain.getArch() == llvm::Triple::ppc64)
6124 CmdArgs.push_back("elf64ppc");
Eli Friedman27b8c4f2011-11-08 19:43:37 +00006125 else if (ToolChain.getArch() == llvm::Triple::mips)
6126 CmdArgs.push_back("elf32btsmip");
6127 else if (ToolChain.getArch() == llvm::Triple::mipsel)
6128 CmdArgs.push_back("elf32ltsmip");
Simon Atanasyan2d1b1ad2012-10-21 11:44:57 +00006129 else if (ToolChain.getArch() == llvm::Triple::mips64) {
6130 if (hasMipsN32ABIArg(Args))
6131 CmdArgs.push_back("elf32btsmipn32");
6132 else
6133 CmdArgs.push_back("elf64btsmip");
6134 }
6135 else if (ToolChain.getArch() == llvm::Triple::mips64el) {
6136 if (hasMipsN32ABIArg(Args))
6137 CmdArgs.push_back("elf32ltsmipn32");
6138 else
6139 CmdArgs.push_back("elf64ltsmip");
6140 }
Ulrich Weigand47445072013-05-06 16:26:41 +00006141 else if (ToolChain.getArch() == llvm::Triple::systemz)
6142 CmdArgs.push_back("elf64_s390");
Rafael Espindolac8f008f2010-11-07 20:14:31 +00006143 else
6144 CmdArgs.push_back("elf_x86_64");
6145
6146 if (Args.hasArg(options::OPT_static)) {
Douglas Gregord9bb1522011-03-06 19:11:49 +00006147 if (ToolChain.getArch() == llvm::Triple::arm
6148 || ToolChain.getArch() == llvm::Triple::thumb)
Rafael Espindolac8f008f2010-11-07 20:14:31 +00006149 CmdArgs.push_back("-Bstatic");
6150 else
6151 CmdArgs.push_back("-static");
6152 } else if (Args.hasArg(options::OPT_shared)) {
6153 CmdArgs.push_back("-shared");
Rafael Espindola7df35012012-11-02 20:41:30 +00006154 if (isAndroid) {
Evgeniy Stepanov77866712012-04-25 08:59:22 +00006155 CmdArgs.push_back("-Bsymbolic");
6156 }
Rafael Espindolac8f008f2010-11-07 20:14:31 +00006157 }
6158
6159 if (ToolChain.getArch() == llvm::Triple::arm ||
Douglas Gregord9bb1522011-03-06 19:11:49 +00006160 ToolChain.getArch() == llvm::Triple::thumb ||
Rafael Espindolac8f008f2010-11-07 20:14:31 +00006161 (!Args.hasArg(options::OPT_static) &&
6162 !Args.hasArg(options::OPT_shared))) {
6163 CmdArgs.push_back("-dynamic-linker");
Peter Collingbourne9d9e1fc2013-05-27 21:40:20 +00006164 CmdArgs.push_back(Args.MakeArgString(
6165 D.DyldPrefix + getLinuxDynamicLinker(Args, ToolChain)));
Rafael Espindolac8f008f2010-11-07 20:14:31 +00006166 }
6167
6168 CmdArgs.push_back("-o");
6169 CmdArgs.push_back(Output.getFilename());
6170
Rafael Espindola81937ec2010-12-01 01:52:43 +00006171 if (!Args.hasArg(options::OPT_nostdlib) &&
6172 !Args.hasArg(options::OPT_nostartfiles)) {
Evgeniy Stepanov77866712012-04-25 08:59:22 +00006173 if (!isAndroid) {
6174 const char *crt1 = NULL;
6175 if (!Args.hasArg(options::OPT_shared)){
Eric Christopherac021742013-06-07 23:25:01 +00006176 if (Args.hasArg(options::OPT_pg))
6177 crt1 = "gcrt1.o";
6178 else if (IsPIE)
Evgeniy Stepanov77866712012-04-25 08:59:22 +00006179 crt1 = "Scrt1.o";
6180 else
6181 crt1 = "crt1.o";
6182 }
6183 if (crt1)
6184 CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath(crt1)));
Rafael Espindolac8f008f2010-11-07 20:14:31 +00006185
Evgeniy Stepanov77866712012-04-25 08:59:22 +00006186 CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath("crti.o")));
6187 }
Rafael Espindolac8f008f2010-11-07 20:14:31 +00006188
Rafael Espindola9aa60e92010-11-12 03:00:39 +00006189 const char *crtbegin;
6190 if (Args.hasArg(options::OPT_static))
Evgeniy Stepanov77866712012-04-25 08:59:22 +00006191 crtbegin = isAndroid ? "crtbegin_static.o" : "crtbeginT.o";
Evgeniy Stepanovc9cde482012-09-10 10:30:12 +00006192 else if (Args.hasArg(options::OPT_shared))
Evgeniy Stepanov77866712012-04-25 08:59:22 +00006193 crtbegin = isAndroid ? "crtbegin_so.o" : "crtbeginS.o";
Peter Collingbourne54d770c2013-04-09 04:35:11 +00006194 else if (IsPIE)
Evgeniy Stepanovc9cde482012-09-10 10:30:12 +00006195 crtbegin = isAndroid ? "crtbegin_dynamic.o" : "crtbeginS.o";
Rafael Espindola9aa60e92010-11-12 03:00:39 +00006196 else
Evgeniy Stepanov77866712012-04-25 08:59:22 +00006197 crtbegin = isAndroid ? "crtbegin_dynamic.o" : "crtbegin.o";
Rafael Espindola9aa60e92010-11-12 03:00:39 +00006198 CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath(crtbegin)));
Benjamin Kramer058666a2012-10-04 19:42:20 +00006199
6200 // Add crtfastmath.o if available and fast math is enabled.
6201 ToolChain.AddFastMathRuntimeIfAvailable(Args, CmdArgs);
Rafael Espindola9aa60e92010-11-12 03:00:39 +00006202 }
Rafael Espindolac8f008f2010-11-07 20:14:31 +00006203
6204 Args.AddAllArgs(CmdArgs, options::OPT_L);
6205
6206 const ToolChain::path_list Paths = ToolChain.getFilePaths();
6207
Roman Divackyee8188a2011-03-01 17:53:14 +00006208 for (ToolChain::path_list::const_iterator i = Paths.begin(), e = Paths.end();
6209 i != e; ++i)
Chris Lattner0e62c1c2011-07-23 10:55:15 +00006210 CmdArgs.push_back(Args.MakeArgString(StringRef("-L") + *i));
Rafael Espindolac8f008f2010-11-07 20:14:31 +00006211
Rafael Espindola9446d762012-04-09 23:53:34 +00006212 // Tell the linker to load the plugin. This has to come before AddLinkerInputs
6213 // as gold requires -plugin to come before any -plugin-opt that -Wl might
6214 // forward.
Rafael Espindolab0092d72013-09-04 19:37:35 +00006215 if (D.IsUsingLTO(Args)) {
Rafael Espindola9446d762012-04-09 23:53:34 +00006216 CmdArgs.push_back("-plugin");
6217 std::string Plugin = ToolChain.getDriver().Dir + "/../lib/LLVMgold.so";
6218 CmdArgs.push_back(Args.MakeArgString(Plugin));
Chandler Carruth953fb082013-01-13 11:46:33 +00006219
6220 // Try to pass driver level flags relevant to LTO code generation down to
6221 // the plugin.
6222
Rafael Espindola22ce34a2013-08-20 22:12:08 +00006223 // Handle flags for selecting CPU variants.
6224 std::string CPU = getCPUName(Args, ToolChain.getTriple());
6225 if (!CPU.empty()) {
Chandler Carruth953fb082013-01-13 11:46:33 +00006226 CmdArgs.push_back(
Rafael Espindola22ce34a2013-08-20 22:12:08 +00006227 Args.MakeArgString(Twine("-plugin-opt=mcpu=") +
6228 CPU));
6229 }
Rafael Espindola9446d762012-04-09 23:53:34 +00006230 }
6231
Chandler Carruth953fb082013-01-13 11:46:33 +00006232
Nick Lewycky2fe6aab2012-08-17 03:39:16 +00006233 if (Args.hasArg(options::OPT_Z_Xlinker__no_demangle))
6234 CmdArgs.push_back("--no-demangle");
6235
Rafael Espindolac8f008f2010-11-07 20:14:31 +00006236 AddLinkerInputs(ToolChain, Inputs, Args, CmdArgs);
6237
Eric Christopher04997782012-11-29 18:51:05 +00006238 // Call these before we add the C++ ABI library.
Richard Smith52be6192012-11-05 22:04:41 +00006239 if (Sanitize.needsUbsanRt())
Hans Wennborg70850d82013-07-18 20:29:38 +00006240 addUbsanRTLinux(getToolChain(), Args, CmdArgs, D.CCCIsCXX(),
Richard Smithcff3cde2013-03-20 23:49:07 +00006241 Sanitize.needsAsanRt() || Sanitize.needsTsanRt() ||
Sergey Matveev1814e9e2013-05-27 11:17:01 +00006242 Sanitize.needsMsanRt() || Sanitize.needsLsanRt());
Eric Christopher04997782012-11-29 18:51:05 +00006243 if (Sanitize.needsAsanRt())
6244 addAsanRTLinux(getToolChain(), Args, CmdArgs);
6245 if (Sanitize.needsTsanRt())
6246 addTsanRTLinux(getToolChain(), Args, CmdArgs);
Evgeniy Stepanovaea92e52012-12-03 13:20:43 +00006247 if (Sanitize.needsMsanRt())
6248 addMsanRTLinux(getToolChain(), Args, CmdArgs);
Sergey Matveev1814e9e2013-05-27 11:17:01 +00006249 if (Sanitize.needsLsanRt())
6250 addLsanRTLinux(getToolChain(), Args, CmdArgs);
Peter Collingbournec3772752013-08-07 22:47:34 +00006251 if (Sanitize.needsDfsanRt())
6252 addDfsanRTLinux(getToolChain(), Args, CmdArgs);
Richard Smith4d3110a2012-10-25 02:14:12 +00006253
Chandler Carruthe4458b32013-06-24 09:38:45 +00006254 // The profile runtime also needs access to system libraries.
6255 addProfileRTLinux(getToolChain(), Args, CmdArgs);
6256
Hans Wennborg70850d82013-07-18 20:29:38 +00006257 if (D.CCCIsCXX() &&
Chandler Carruth94a32012012-05-14 18:31:18 +00006258 !Args.hasArg(options::OPT_nostdlib) &&
6259 !Args.hasArg(options::OPT_nodefaultlibs)) {
Rafael Espindola70b8d762011-10-17 22:14:51 +00006260 bool OnlyLibstdcxxStatic = Args.hasArg(options::OPT_static_libstdcxx) &&
6261 !Args.hasArg(options::OPT_static);
6262 if (OnlyLibstdcxxStatic)
6263 CmdArgs.push_back("-Bstatic");
Rafael Espindolac8f008f2010-11-07 20:14:31 +00006264 ToolChain.AddCXXStdlibLibArgs(Args, CmdArgs);
Rafael Espindola70b8d762011-10-17 22:14:51 +00006265 if (OnlyLibstdcxxStatic)
6266 CmdArgs.push_back("-Bdynamic");
Rafael Espindolac8f008f2010-11-07 20:14:31 +00006267 CmdArgs.push_back("-lm");
6268 }
6269
Rafael Espindola9aa60e92010-11-12 03:00:39 +00006270 if (!Args.hasArg(options::OPT_nostdlib)) {
Chandler Carruth94a32012012-05-14 18:31:18 +00006271 if (!Args.hasArg(options::OPT_nodefaultlibs)) {
6272 if (Args.hasArg(options::OPT_static))
6273 CmdArgs.push_back("--start-group");
Nick Lewycky97864da2011-06-04 06:27:06 +00006274
Chandler Carruth01538002013-01-17 13:19:29 +00006275 bool OpenMP = Args.hasArg(options::OPT_fopenmp);
6276 if (OpenMP) {
6277 CmdArgs.push_back("-lgomp");
6278
6279 // FIXME: Exclude this for platforms whith libgomp that doesn't require
6280 // librt. Most modern Linux platfroms require it, but some may not.
6281 CmdArgs.push_back("-lrt");
6282 }
6283
Evgeniy Stepanov77866712012-04-25 08:59:22 +00006284 AddLibgcc(ToolChain.getTriple(), D, CmdArgs, Args);
Rafael Espindola9aa60e92010-11-12 03:00:39 +00006285
Chandler Carruth94a32012012-05-14 18:31:18 +00006286 if (Args.hasArg(options::OPT_pthread) ||
Chandler Carruth01538002013-01-17 13:19:29 +00006287 Args.hasArg(options::OPT_pthreads) || OpenMP)
Chandler Carruth94a32012012-05-14 18:31:18 +00006288 CmdArgs.push_back("-lpthread");
6289
6290 CmdArgs.push_back("-lc");
6291
6292 if (Args.hasArg(options::OPT_static))
6293 CmdArgs.push_back("--end-group");
6294 else
6295 AddLibgcc(ToolChain.getTriple(), D, CmdArgs, Args);
6296 }
Rafael Espindolad47ac232010-11-17 22:26:15 +00006297
Rafael Espindola81937ec2010-12-01 01:52:43 +00006298 if (!Args.hasArg(options::OPT_nostartfiles)) {
6299 const char *crtend;
Evgeniy Stepanovc9cde482012-09-10 10:30:12 +00006300 if (Args.hasArg(options::OPT_shared))
Evgeniy Stepanov77866712012-04-25 08:59:22 +00006301 crtend = isAndroid ? "crtend_so.o" : "crtendS.o";
Peter Collingbourne54d770c2013-04-09 04:35:11 +00006302 else if (IsPIE)
Evgeniy Stepanovc9cde482012-09-10 10:30:12 +00006303 crtend = isAndroid ? "crtend_android.o" : "crtendS.o";
Rafael Espindola81937ec2010-12-01 01:52:43 +00006304 else
Evgeniy Stepanov77866712012-04-25 08:59:22 +00006305 crtend = isAndroid ? "crtend_android.o" : "crtend.o";
Rafael Espindola9aa60e92010-11-12 03:00:39 +00006306
Rafael Espindola81937ec2010-12-01 01:52:43 +00006307 CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath(crtend)));
Evgeniy Stepanov77866712012-04-25 08:59:22 +00006308 if (!isAndroid)
6309 CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath("crtn.o")));
Rafael Espindola81937ec2010-12-01 01:52:43 +00006310 }
Rafael Espindolac8f008f2010-11-07 20:14:31 +00006311 }
6312
Rafael Espindolac8f008f2010-11-07 20:14:31 +00006313 C.addCommand(new Command(JA, *this, ToolChain.Linker.c_str(), CmdArgs));
6314}
Rafael Espindola92b00932010-08-10 00:25:48 +00006315
Chris Lattner3e2ee142010-07-07 16:01:42 +00006316void minix::Assemble::ConstructJob(Compilation &C, const JobAction &JA,
Daniel Dunbar7fbaf532010-08-02 02:38:28 +00006317 const InputInfo &Output,
6318 const InputInfoList &Inputs,
6319 const ArgList &Args,
6320 const char *LinkingOutput) const {
Chris Lattner3e2ee142010-07-07 16:01:42 +00006321 ArgStringList CmdArgs;
6322
6323 Args.AddAllArgValues(CmdArgs, options::OPT_Wa_COMMA,
6324 options::OPT_Xassembler);
6325
6326 CmdArgs.push_back("-o");
Daniel Dunbarb440f562010-08-02 02:38:21 +00006327 CmdArgs.push_back(Output.getFilename());
Chris Lattner3e2ee142010-07-07 16:01:42 +00006328
6329 for (InputInfoList::const_iterator
6330 it = Inputs.begin(), ie = Inputs.end(); it != ie; ++it) {
6331 const InputInfo &II = *it;
Daniel Dunbarb440f562010-08-02 02:38:21 +00006332 CmdArgs.push_back(II.getFilename());
Chris Lattner3e2ee142010-07-07 16:01:42 +00006333 }
6334
6335 const char *Exec =
Eli Friedman83de5132011-12-08 23:54:21 +00006336 Args.MakeArgString(getToolChain().GetProgramPath("as"));
Daniel Dunbar7fbaf532010-08-02 02:38:28 +00006337 C.addCommand(new Command(JA, *this, Exec, CmdArgs));
Chris Lattner3e2ee142010-07-07 16:01:42 +00006338}
6339
6340void minix::Link::ConstructJob(Compilation &C, const JobAction &JA,
Daniel Dunbar7fbaf532010-08-02 02:38:28 +00006341 const InputInfo &Output,
6342 const InputInfoList &Inputs,
6343 const ArgList &Args,
6344 const char *LinkingOutput) const {
Chris Lattner3e2ee142010-07-07 16:01:42 +00006345 const Driver &D = getToolChain().getDriver();
6346 ArgStringList CmdArgs;
6347
Daniel Dunbarb440f562010-08-02 02:38:21 +00006348 if (Output.isFilename()) {
Chris Lattner3e2ee142010-07-07 16:01:42 +00006349 CmdArgs.push_back("-o");
6350 CmdArgs.push_back(Output.getFilename());
6351 } else {
6352 assert(Output.isNothing() && "Invalid output.");
6353 }
6354
6355 if (!Args.hasArg(options::OPT_nostdlib) &&
Eli Friedman83de5132011-12-08 23:54:21 +00006356 !Args.hasArg(options::OPT_nostartfiles)) {
6357 CmdArgs.push_back(Args.MakeArgString(getToolChain().GetFilePath("crt1.o")));
6358 CmdArgs.push_back(Args.MakeArgString(getToolChain().GetFilePath("crti.o")));
6359 CmdArgs.push_back(Args.MakeArgString(getToolChain().GetFilePath("crtbegin.o")));
6360 CmdArgs.push_back(Args.MakeArgString(getToolChain().GetFilePath("crtn.o")));
6361 }
Chris Lattner3e2ee142010-07-07 16:01:42 +00006362
6363 Args.AddAllArgs(CmdArgs, options::OPT_L);
6364 Args.AddAllArgs(CmdArgs, options::OPT_T_Group);
6365 Args.AddAllArgs(CmdArgs, options::OPT_e);
6366
Daniel Dunbar54423b22010-09-17 00:24:54 +00006367 AddLinkerInputs(getToolChain(), Inputs, Args, CmdArgs);
Chris Lattner3e2ee142010-07-07 16:01:42 +00006368
Eli Friedman83de5132011-12-08 23:54:21 +00006369 addProfileRT(getToolChain(), Args, CmdArgs, getToolChain().getTriple());
6370
Chris Lattner3e2ee142010-07-07 16:01:42 +00006371 if (!Args.hasArg(options::OPT_nostdlib) &&
6372 !Args.hasArg(options::OPT_nodefaultlibs)) {
Hans Wennborg70850d82013-07-18 20:29:38 +00006373 if (D.CCCIsCXX()) {
Daniel Dunbar3f7796f2010-09-17 01:20:05 +00006374 getToolChain().AddCXXStdlibLibArgs(Args, CmdArgs);
Chris Lattner3e2ee142010-07-07 16:01:42 +00006375 CmdArgs.push_back("-lm");
6376 }
Chris Lattner3e2ee142010-07-07 16:01:42 +00006377 }
6378
6379 if (!Args.hasArg(options::OPT_nostdlib) &&
6380 !Args.hasArg(options::OPT_nostartfiles)) {
Eli Friedman83de5132011-12-08 23:54:21 +00006381 if (Args.hasArg(options::OPT_pthread))
6382 CmdArgs.push_back("-lpthread");
6383 CmdArgs.push_back("-lc");
6384 CmdArgs.push_back("-lCompilerRT-Generic");
6385 CmdArgs.push_back("-L/usr/pkg/compiler-rt/lib");
6386 CmdArgs.push_back(
Eric Christopher45f2e712012-12-18 00:31:10 +00006387 Args.MakeArgString(getToolChain().GetFilePath("crtend.o")));
Chris Lattner3e2ee142010-07-07 16:01:42 +00006388 }
6389
Eli Friedman83de5132011-12-08 23:54:21 +00006390 const char *Exec = Args.MakeArgString(getToolChain().GetProgramPath("ld"));
Daniel Dunbar7fbaf532010-08-02 02:38:28 +00006391 C.addCommand(new Command(JA, *this, Exec, CmdArgs));
Chris Lattner3e2ee142010-07-07 16:01:42 +00006392}
6393
Daniel Dunbarcc912342009-05-02 18:28:39 +00006394/// DragonFly Tools
6395
6396// For now, DragonFly Assemble does just about the same as for
6397// FreeBSD, but this may change soon.
6398void dragonfly::Assemble::ConstructJob(Compilation &C, const JobAction &JA,
Daniel Dunbar7fbaf532010-08-02 02:38:28 +00006399 const InputInfo &Output,
Daniel Dunbar1c7577c2009-11-04 06:24:38 +00006400 const InputInfoList &Inputs,
6401 const ArgList &Args,
6402 const char *LinkingOutput) const {
Daniel Dunbarcc912342009-05-02 18:28:39 +00006403 ArgStringList CmdArgs;
6404
6405 // When building 32-bit code on DragonFly/pc64, we have to explicitly
6406 // instruct as in the base system to assemble 32-bit code.
Rafael Espindola35ca7d92012-10-07 04:44:33 +00006407 if (getToolChain().getArch() == llvm::Triple::x86)
Daniel Dunbarcc912342009-05-02 18:28:39 +00006408 CmdArgs.push_back("--32");
6409
6410 Args.AddAllArgValues(CmdArgs, options::OPT_Wa_COMMA,
6411 options::OPT_Xassembler);
6412
6413 CmdArgs.push_back("-o");
Daniel Dunbarb440f562010-08-02 02:38:21 +00006414 CmdArgs.push_back(Output.getFilename());
Daniel Dunbarcc912342009-05-02 18:28:39 +00006415
6416 for (InputInfoList::const_iterator
6417 it = Inputs.begin(), ie = Inputs.end(); it != ie; ++it) {
6418 const InputInfo &II = *it;
Daniel Dunbarb440f562010-08-02 02:38:21 +00006419 CmdArgs.push_back(II.getFilename());
Daniel Dunbarcc912342009-05-02 18:28:39 +00006420 }
6421
6422 const char *Exec =
Daniel Dunbar9c3ed5f2010-07-14 18:46:23 +00006423 Args.MakeArgString(getToolChain().GetProgramPath("as"));
Daniel Dunbar7fbaf532010-08-02 02:38:28 +00006424 C.addCommand(new Command(JA, *this, Exec, CmdArgs));
Daniel Dunbarcc912342009-05-02 18:28:39 +00006425}
6426
6427void dragonfly::Link::ConstructJob(Compilation &C, const JobAction &JA,
Daniel Dunbar7fbaf532010-08-02 02:38:28 +00006428 const InputInfo &Output,
6429 const InputInfoList &Inputs,
6430 const ArgList &Args,
6431 const char *LinkingOutput) const {
John McCall65b8da02013-04-11 22:55:55 +00006432 bool UseGCC47 = false;
Daniel Dunbar083edf72009-12-21 18:54:17 +00006433 const Driver &D = getToolChain().getDriver();
Daniel Dunbarcc912342009-05-02 18:28:39 +00006434 ArgStringList CmdArgs;
6435
John McCall65b8da02013-04-11 22:55:55 +00006436 if (llvm::sys::fs::exists("/usr/lib/gcc47", UseGCC47))
6437 UseGCC47 = false;
6438
Joerg Sonnenberger6165ab12011-03-21 13:51:29 +00006439 if (!D.SysRoot.empty())
6440 CmdArgs.push_back(Args.MakeArgString("--sysroot=" + D.SysRoot));
6441
John McCall65b8da02013-04-11 22:55:55 +00006442 CmdArgs.push_back("--eh-frame-hdr");
Daniel Dunbarcc912342009-05-02 18:28:39 +00006443 if (Args.hasArg(options::OPT_static)) {
6444 CmdArgs.push_back("-Bstatic");
6445 } else {
John McCall65b8da02013-04-11 22:55:55 +00006446 if (Args.hasArg(options::OPT_rdynamic))
6447 CmdArgs.push_back("-export-dynamic");
Daniel Dunbarcc912342009-05-02 18:28:39 +00006448 if (Args.hasArg(options::OPT_shared))
6449 CmdArgs.push_back("-Bshareable");
6450 else {
6451 CmdArgs.push_back("-dynamic-linker");
6452 CmdArgs.push_back("/usr/libexec/ld-elf.so.2");
6453 }
John McCall65b8da02013-04-11 22:55:55 +00006454 CmdArgs.push_back("--hash-style=both");
Daniel Dunbarcc912342009-05-02 18:28:39 +00006455 }
6456
6457 // When building 32-bit code on DragonFly/pc64, we have to explicitly
6458 // instruct ld in the base system to link 32-bit code.
Rafael Espindola35ca7d92012-10-07 04:44:33 +00006459 if (getToolChain().getArch() == llvm::Triple::x86) {
Daniel Dunbarcc912342009-05-02 18:28:39 +00006460 CmdArgs.push_back("-m");
6461 CmdArgs.push_back("elf_i386");
6462 }
6463
Daniel Dunbarb440f562010-08-02 02:38:21 +00006464 if (Output.isFilename()) {
Daniel Dunbarcc912342009-05-02 18:28:39 +00006465 CmdArgs.push_back("-o");
6466 CmdArgs.push_back(Output.getFilename());
6467 } else {
6468 assert(Output.isNothing() && "Invalid output.");
6469 }
6470
6471 if (!Args.hasArg(options::OPT_nostdlib) &&
6472 !Args.hasArg(options::OPT_nostartfiles)) {
6473 if (!Args.hasArg(options::OPT_shared)) {
John McCall65b8da02013-04-11 22:55:55 +00006474 if (Args.hasArg(options::OPT_pg))
6475 CmdArgs.push_back(Args.MakeArgString(
6476 getToolChain().GetFilePath("gcrt1.o")));
6477 else {
6478 if (Args.hasArg(options::OPT_pie))
6479 CmdArgs.push_back(Args.MakeArgString(
6480 getToolChain().GetFilePath("Scrt1.o")));
6481 else
6482 CmdArgs.push_back(Args.MakeArgString(
6483 getToolChain().GetFilePath("crt1.o")));
6484 }
Daniel Dunbarcc912342009-05-02 18:28:39 +00006485 }
John McCall65b8da02013-04-11 22:55:55 +00006486 CmdArgs.push_back(Args.MakeArgString(
6487 getToolChain().GetFilePath("crti.o")));
6488 if (Args.hasArg(options::OPT_shared) || Args.hasArg(options::OPT_pie))
6489 CmdArgs.push_back(Args.MakeArgString(
6490 getToolChain().GetFilePath("crtbeginS.o")));
6491 else
6492 CmdArgs.push_back(Args.MakeArgString(
6493 getToolChain().GetFilePath("crtbegin.o")));
Daniel Dunbarcc912342009-05-02 18:28:39 +00006494 }
6495
6496 Args.AddAllArgs(CmdArgs, options::OPT_L);
6497 Args.AddAllArgs(CmdArgs, options::OPT_T_Group);
6498 Args.AddAllArgs(CmdArgs, options::OPT_e);
6499
Daniel Dunbar54423b22010-09-17 00:24:54 +00006500 AddLinkerInputs(getToolChain(), Inputs, Args, CmdArgs);
Daniel Dunbarcc912342009-05-02 18:28:39 +00006501
6502 if (!Args.hasArg(options::OPT_nostdlib) &&
6503 !Args.hasArg(options::OPT_nodefaultlibs)) {
6504 // FIXME: GCC passes on -lgcc, -lgcc_pic and a whole lot of
6505 // rpaths
John McCall65b8da02013-04-11 22:55:55 +00006506 if (UseGCC47)
6507 CmdArgs.push_back("-L/usr/lib/gcc47");
6508 else
6509 CmdArgs.push_back("-L/usr/lib/gcc44");
Daniel Dunbarcc912342009-05-02 18:28:39 +00006510
6511 if (!Args.hasArg(options::OPT_static)) {
John McCall65b8da02013-04-11 22:55:55 +00006512 if (UseGCC47) {
6513 CmdArgs.push_back("-rpath");
6514 CmdArgs.push_back("/usr/lib/gcc47");
6515 } else {
6516 CmdArgs.push_back("-rpath");
6517 CmdArgs.push_back("/usr/lib/gcc44");
6518 }
Daniel Dunbarcc912342009-05-02 18:28:39 +00006519 }
6520
Hans Wennborg70850d82013-07-18 20:29:38 +00006521 if (D.CCCIsCXX()) {
Daniel Dunbar3f7796f2010-09-17 01:20:05 +00006522 getToolChain().AddCXXStdlibLibArgs(Args, CmdArgs);
Rafael Espindola38360b32010-07-20 12:59:03 +00006523 CmdArgs.push_back("-lm");
6524 }
6525
Daniel Dunbarcc912342009-05-02 18:28:39 +00006526 if (Args.hasArg(options::OPT_pthread))
Mike Stump0a65b632009-10-31 20:11:46 +00006527 CmdArgs.push_back("-lpthread");
Daniel Dunbarcc912342009-05-02 18:28:39 +00006528
6529 if (!Args.hasArg(options::OPT_nolibc)) {
6530 CmdArgs.push_back("-lc");
6531 }
6532
John McCall65b8da02013-04-11 22:55:55 +00006533 if (UseGCC47) {
6534 if (Args.hasArg(options::OPT_static) ||
6535 Args.hasArg(options::OPT_static_libgcc)) {
6536 CmdArgs.push_back("-lgcc");
6537 CmdArgs.push_back("-lgcc_eh");
6538 } else {
6539 if (Args.hasArg(options::OPT_shared_libgcc)) {
6540 CmdArgs.push_back("-lgcc_pic");
6541 if (!Args.hasArg(options::OPT_shared))
6542 CmdArgs.push_back("-lgcc");
6543 } else {
6544 CmdArgs.push_back("-lgcc");
6545 CmdArgs.push_back("--as-needed");
6546 CmdArgs.push_back("-lgcc_pic");
6547 CmdArgs.push_back("--no-as-needed");
6548 }
6549 }
Daniel Dunbarcc912342009-05-02 18:28:39 +00006550 } else {
John McCall65b8da02013-04-11 22:55:55 +00006551 if (Args.hasArg(options::OPT_shared)) {
6552 CmdArgs.push_back("-lgcc_pic");
6553 } else {
6554 CmdArgs.push_back("-lgcc");
6555 }
Daniel Dunbarcc912342009-05-02 18:28:39 +00006556 }
6557 }
6558
6559 if (!Args.hasArg(options::OPT_nostdlib) &&
6560 !Args.hasArg(options::OPT_nostartfiles)) {
John McCall65b8da02013-04-11 22:55:55 +00006561 if (Args.hasArg(options::OPT_shared) || Args.hasArg(options::OPT_pie))
Chris Lattner3e2ee142010-07-07 16:01:42 +00006562 CmdArgs.push_back(Args.MakeArgString(
Daniel Dunbar9c3ed5f2010-07-14 18:46:23 +00006563 getToolChain().GetFilePath("crtendS.o")));
John McCall65b8da02013-04-11 22:55:55 +00006564 else
6565 CmdArgs.push_back(Args.MakeArgString(
6566 getToolChain().GetFilePath("crtend.o")));
Chris Lattner3e2ee142010-07-07 16:01:42 +00006567 CmdArgs.push_back(Args.MakeArgString(
John McCall65b8da02013-04-11 22:55:55 +00006568 getToolChain().GetFilePath("crtn.o")));
Daniel Dunbarcc912342009-05-02 18:28:39 +00006569 }
6570
Bill Wendling08760582011-06-27 19:15:03 +00006571 addProfileRT(getToolChain(), Args, CmdArgs, getToolChain().getTriple());
Nick Lewycky82fe5f42011-05-24 21:54:59 +00006572
Daniel Dunbarcc912342009-05-02 18:28:39 +00006573 const char *Exec =
Daniel Dunbar9c3ed5f2010-07-14 18:46:23 +00006574 Args.MakeArgString(getToolChain().GetProgramPath("ld"));
Daniel Dunbar7fbaf532010-08-02 02:38:28 +00006575 C.addCommand(new Command(JA, *this, Exec, CmdArgs));
Daniel Dunbarcc912342009-05-02 18:28:39 +00006576}
Michael J. Spencerb186bc32010-08-21 21:55:07 +00006577
6578void visualstudio::Link::ConstructJob(Compilation &C, const JobAction &JA,
6579 const InputInfo &Output,
6580 const InputInfoList &Inputs,
6581 const ArgList &Args,
6582 const char *LinkingOutput) const {
Michael J. Spencerb186bc32010-08-21 21:55:07 +00006583 ArgStringList CmdArgs;
6584
6585 if (Output.isFilename()) {
Daniel Dunbar2cc3f172010-09-17 00:45:02 +00006586 CmdArgs.push_back(Args.MakeArgString(std::string("-out:") +
6587 Output.getFilename()));
Michael J. Spencerb186bc32010-08-21 21:55:07 +00006588 } else {
6589 assert(Output.isNothing() && "Invalid output.");
6590 }
6591
6592 if (!Args.hasArg(options::OPT_nostdlib) &&
Hans Wennborg32e3b722013-08-09 17:38:42 +00006593 !Args.hasArg(options::OPT_nostartfiles) &&
6594 !C.getDriver().IsCLMode()) {
Michael J. Spencerb186bc32010-08-21 21:55:07 +00006595 CmdArgs.push_back("-defaultlib:libcmt");
6596 }
6597
6598 CmdArgs.push_back("-nologo");
6599
Hans Wennborgf1a74252013-09-10 20:18:04 +00006600 bool DLL = Args.hasArg(options::OPT__SLASH_LD, options::OPT__SLASH_LDd);
6601
6602 if (DLL) {
6603 CmdArgs.push_back(Args.MakeArgString("-dll"));
6604
6605 SmallString<128> ImplibName(Output.getFilename());
6606 llvm::sys::path::replace_extension(ImplibName, "lib");
6607 CmdArgs.push_back(Args.MakeArgString(std::string("-implib:") +
6608 ImplibName.str()));
6609 }
6610
Hans Wennborg65f17522013-08-27 18:10:21 +00006611 if (getToolChain().getDriver().getOrParseSanitizerArgs(Args).needsAsanRt()) {
Hans Wennborg0517e752013-08-28 17:36:07 +00006612 CmdArgs.push_back(Args.MakeArgString("-debug"));
Hans Wennborgd024c1c2013-08-30 10:50:52 +00006613 CmdArgs.push_back(Args.MakeArgString("-incremental:no"));
Hans Wennborg65f17522013-08-27 18:10:21 +00006614 SmallString<128> LibSanitizer(getToolChain().getDriver().ResourceDir);
Hans Wennborgf1a74252013-09-10 20:18:04 +00006615 llvm::sys::path::append(LibSanitizer, "lib", "windows");
6616 if (DLL) {
Timur Iskhodzhanov3b6adcb62013-09-11 11:45:31 +00006617 llvm::sys::path::append(LibSanitizer, "clang_rt.asan_dll_thunk-i386.lib");
Hans Wennborgf1a74252013-09-10 20:18:04 +00006618 } else {
6619 llvm::sys::path::append(LibSanitizer, "clang_rt.asan-i386.lib");
6620 }
6621 // FIXME: Handle 64-bit.
Hans Wennborg65f17522013-08-27 18:10:21 +00006622 CmdArgs.push_back(Args.MakeArgString(LibSanitizer));
6623 }
6624
Michael J. Spencere2f49362012-06-18 16:56:04 +00006625 Args.AddAllArgValues(CmdArgs, options::OPT_l);
Hans Wennborg2e274592013-08-13 23:38:57 +00006626 Args.AddAllArgValues(CmdArgs, options::OPT__SLASH_link);
Michael J. Spencere2f49362012-06-18 16:56:04 +00006627
6628 // Add filenames immediately.
6629 for (InputInfoList::const_iterator
6630 it = Inputs.begin(), ie = Inputs.end(); it != ie; ++it) {
6631 if (it->isFilename())
6632 CmdArgs.push_back(it->getFilename());
Hans Wennborgbe4a6762013-08-14 01:24:35 +00006633 else
6634 it->getInputArg().renderAsInput(Args, CmdArgs);
Michael J. Spencere2f49362012-06-18 16:56:04 +00006635 }
Michael J. Spencerb186bc32010-08-21 21:55:07 +00006636
6637 const char *Exec =
Daniel Dunbar54423b22010-09-17 00:24:54 +00006638 Args.MakeArgString(getToolChain().GetProgramPath("link.exe"));
Michael J. Spencerb186bc32010-08-21 21:55:07 +00006639 C.addCommand(new Command(JA, *this, Exec, CmdArgs));
6640}