blob: 6a85fc00b3ac2af6c99ec7b1a869e78364fde6fb [file] [log] [blame]
Nick Lewyckye3365aa2010-09-23 23:48:20 +00001//===--- Tools.cpp - Tools Implementations --------------------------------===//
Daniel Dunbar47ac7d22009-03-18 06:00:36 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10#include "Tools.h"
11
Daniel Dunbar1d460332009-03-18 10:01:51 +000012#include "clang/Driver/Action.h"
Daniel Dunbar871adcf2009-03-18 07:06:02 +000013#include "clang/Driver/Arg.h"
Daniel Dunbarb488c1d2009-03-18 08:07:30 +000014#include "clang/Driver/ArgList.h"
Daniel Dunbaree848a72009-10-29 02:39:57 +000015#include "clang/Driver/Driver.h"
16#include "clang/Driver/DriverDiagnostic.h"
Daniel Dunbar871adcf2009-03-18 07:06:02 +000017#include "clang/Driver/Compilation.h"
18#include "clang/Driver/Job.h"
Daniel Dunbarb488c1d2009-03-18 08:07:30 +000019#include "clang/Driver/HostInfo.h"
John McCall9f084a32011-07-06 00:26:06 +000020#include "clang/Driver/ObjCRuntime.h"
Daniel Dunbarb488c1d2009-03-18 08:07:30 +000021#include "clang/Driver/Option.h"
Daniel Dunbar265e9ef2009-11-19 04:25:22 +000022#include "clang/Driver/Options.h"
Daniel Dunbarb488c1d2009-03-18 08:07:30 +000023#include "clang/Driver/ToolChain.h"
Daniel Dunbar871adcf2009-03-18 07:06:02 +000024#include "clang/Driver/Util.h"
25
Daniel Dunbar88137642009-09-09 22:32:48 +000026#include "llvm/ADT/SmallString.h"
Douglas Gregor55d3f7a2009-10-29 00:41:01 +000027#include "llvm/ADT/StringSwitch.h"
Daniel Dunbar5b750fe2009-09-09 22:32:34 +000028#include "llvm/ADT/Twine.h"
Michael J. Spencer32bef4e2011-01-10 02:34:13 +000029#include "llvm/Support/FileSystem.h"
Daniel Dunbar02633b52009-03-26 16:23:12 +000030#include "llvm/Support/Format.h"
31#include "llvm/Support/raw_ostream.h"
Michael J. Spencer03013fa2010-11-29 18:12:39 +000032#include "llvm/Support/Host.h"
33#include "llvm/Support/Process.h"
John McCallf85e1932011-06-15 23:02:42 +000034#include "llvm/Support/ErrorHandling.h"
Daniel Dunbar871adcf2009-03-18 07:06:02 +000035
36#include "InputInfo.h"
Daniel Dunbar02633b52009-03-26 16:23:12 +000037#include "ToolChains.h"
Daniel Dunbar871adcf2009-03-18 07:06:02 +000038
Dylan Noblesmithb8a3e812011-04-09 13:31:59 +000039#ifdef __CYGWIN__
40#include <cygwin/version.h>
41#if defined(CYGWIN_VERSION_DLL_MAJOR) && CYGWIN_VERSION_DLL_MAJOR<1007
42#define IS_CYGWIN15 1
43#endif
44#endif
45
Daniel Dunbar47ac7d22009-03-18 06:00:36 +000046using namespace clang::driver;
47using namespace clang::driver::tools;
Chris Lattner5f9e2722011-07-23 10:55:15 +000048using namespace clang;
Daniel Dunbar47ac7d22009-03-18 06:00:36 +000049
Daniel Dunbar88a3d6c2009-09-10 01:21:05 +000050/// CheckPreprocessingOptions - Perform some validation of preprocessing
51/// arguments that is shared with gcc.
52static void CheckPreprocessingOptions(const Driver &D, const ArgList &Args) {
53 if (Arg *A = Args.getLastArg(options::OPT_C, options::OPT_CC))
Joerg Sonnenberger9ade4ae2011-03-06 23:31:01 +000054 if (!Args.hasArg(options::OPT_E) && !D.CCCIsCPP)
Chris Lattner5f9e2722011-07-23 10:55:15 +000055 D.Diag(diag::err_drv_argument_only_allowed_with)
Daniel Dunbar88a3d6c2009-09-10 01:21:05 +000056 << A->getAsString(Args) << "-E";
57}
58
Daniel Dunbare2fd6642009-09-10 01:21:12 +000059/// CheckCodeGenerationOptions - Perform some validation of code generation
60/// arguments that is shared with gcc.
61static void CheckCodeGenerationOptions(const Driver &D, const ArgList &Args) {
62 // In gcc, only ARM checks this, but it seems reasonable to check universally.
63 if (Args.hasArg(options::OPT_static))
64 if (const Arg *A = Args.getLastArg(options::OPT_dynamic,
65 options::OPT_mdynamic_no_pic))
Chris Lattner5f9e2722011-07-23 10:55:15 +000066 D.Diag(diag::err_drv_argument_not_allowed_with)
Daniel Dunbare2fd6642009-09-10 01:21:12 +000067 << A->getAsString(Args) << "-static";
68}
69
Chris Lattner3edbeb72010-03-29 17:55:58 +000070// Quote target names for inclusion in GNU Make dependency files.
71// Only the characters '$', '#', ' ', '\t' are quoted.
Chris Lattner5f9e2722011-07-23 10:55:15 +000072static void QuoteTarget(StringRef Target,
73 SmallVectorImpl<char> &Res) {
Chris Lattner3edbeb72010-03-29 17:55:58 +000074 for (unsigned i = 0, e = Target.size(); i != e; ++i) {
75 switch (Target[i]) {
76 case ' ':
77 case '\t':
78 // Escape the preceding backslashes
79 for (int j = i - 1; j >= 0 && Target[j] == '\\'; --j)
80 Res.push_back('\\');
81
82 // Escape the space/tab
83 Res.push_back('\\');
84 break;
85 case '$':
86 Res.push_back('$');
87 break;
88 case '#':
89 Res.push_back('\\');
90 break;
91 default:
92 break;
93 }
94
95 Res.push_back(Target[i]);
96 }
97}
98
Daniel Dunbar2008fee2010-09-17 00:24:54 +000099static void AddLinkerInputs(const ToolChain &TC,
100 const InputInfoList &Inputs, const ArgList &Args,
101 ArgStringList &CmdArgs) {
102 const Driver &D = TC.getDriver();
103
Daniel Dunbar8ac38d72011-02-19 05:33:51 +0000104 // Add extra linker input arguments which are not treated as inputs
105 // (constructed via -Xarch_).
106 Args.AddAllArgValues(CmdArgs, options::OPT_Zlinker_input);
107
Daniel Dunbar2008fee2010-09-17 00:24:54 +0000108 for (InputInfoList::const_iterator
109 it = Inputs.begin(), ie = Inputs.end(); it != ie; ++it) {
110 const InputInfo &II = *it;
111
112 if (!TC.HasNativeLLVMSupport()) {
113 // Don't try to pass LLVM inputs unless we have native support.
114 if (II.getType() == types::TY_LLVM_IR ||
115 II.getType() == types::TY_LTO_IR ||
116 II.getType() == types::TY_LLVM_BC ||
117 II.getType() == types::TY_LTO_BC)
Chris Lattner5f9e2722011-07-23 10:55:15 +0000118 D.Diag(diag::err_drv_no_linker_llvm_support)
Daniel Dunbar2008fee2010-09-17 00:24:54 +0000119 << TC.getTripleString();
120 }
121
Daniel Dunbare5a37f42010-09-17 00:45:02 +0000122 // Add filenames immediately.
123 if (II.isFilename()) {
Daniel Dunbar2008fee2010-09-17 00:24:54 +0000124 CmdArgs.push_back(II.getFilename());
Daniel Dunbare5a37f42010-09-17 00:45:02 +0000125 continue;
126 }
127
128 // Otherwise, this is a linker input argument.
129 const Arg &A = II.getInputArg();
130
131 // Handle reserved library options.
132 if (A.getOption().matches(options::OPT_Z_reserved_lib_stdcxx)) {
Daniel Dunbar132e35d2010-09-17 01:20:05 +0000133 TC.AddCXXStdlibLibArgs(Args, CmdArgs);
Shantonu Sen7433fed2010-09-17 18:39:08 +0000134 } else if (A.getOption().matches(options::OPT_Z_reserved_lib_cckext)) {
135 TC.AddCCKextLibArgs(Args, CmdArgs);
Daniel Dunbare5a37f42010-09-17 00:45:02 +0000136 } else
137 A.renderAsInput(Args, CmdArgs);
Daniel Dunbar2008fee2010-09-17 00:24:54 +0000138 }
139}
140
John McCallf85e1932011-06-15 23:02:42 +0000141/// \brief Determine whether Objective-C automated reference counting is
142/// enabled.
143static bool isObjCAutoRefCount(const ArgList &Args) {
144 return Args.hasFlag(options::OPT_fobjc_arc, options::OPT_fno_objc_arc, false);
145}
146
Rafael Espindoladb3f24a2011-06-02 18:58:46 +0000147static void addProfileRT(const ToolChain &TC, const ArgList &Args,
Bill Wendling3f4be6f2011-06-27 19:15:03 +0000148 ArgStringList &CmdArgs,
149 llvm::Triple Triple) {
150 if (!(Args.hasArg(options::OPT_fprofile_arcs) ||
151 Args.hasArg(options::OPT_fprofile_generate) ||
152 Args.hasArg(options::OPT_fcreate_profile) ||
153 Args.hasArg(options::OPT_coverage)))
154 return;
155
156 // GCC links libgcov.a by adding -L<inst>/gcc/lib/gcc/<triple>/<ver> -lgcov to
157 // the link line. We cannot do the same thing because unlike gcov there is a
158 // libprofile_rt.so. We used to use the -l:libprofile_rt.a syntax, but that is
159 // not supported by old linkers.
Benjamin Kramerf2db04c2011-11-07 16:02:25 +0000160 std::string ProfileRT =
161 std::string(TC.getDriver().Dir) + "/../lib/libprofile_rt.a";
Bill Wendling3f4be6f2011-06-27 19:15:03 +0000162
Bob Wilson905c45f2011-10-14 05:03:44 +0000163 if (Triple.isOSDarwin()) {
Bill Wendling3f4be6f2011-06-27 19:15:03 +0000164 // On Darwin, if the static library doesn't exist try the dylib.
165 bool Exists;
Benjamin Kramerf2db04c2011-11-07 16:02:25 +0000166 if (llvm::sys::fs::exists(ProfileRT, Exists) || !Exists)
167 ProfileRT.replace(ProfileRT.size() - 1, 1, "dylib");
Rafael Espindoladb3f24a2011-06-02 18:58:46 +0000168 }
Bill Wendling3f4be6f2011-06-27 19:15:03 +0000169
170 CmdArgs.push_back(Args.MakeArgString(ProfileRT));
Rafael Espindoladb3f24a2011-06-02 18:58:46 +0000171}
172
Benjamin Kramer47adebe2011-09-22 21:41:16 +0000173static void AddIncludeDirectoryList(const ArgList &Args,
174 ArgStringList &CmdArgs,
175 const char *ArgName,
176 const char *DirList) {
177 if (!DirList)
178 return; // Nothing to do.
179
180 StringRef Dirs(DirList);
181 if (Dirs.empty()) // Empty string should not add '.'.
182 return;
183
184 StringRef::size_type Delim;
185 while ((Delim = Dirs.find(llvm::sys::PathSeparator)) != StringRef::npos) {
186 if (Delim == 0) { // Leading colon.
187 CmdArgs.push_back(ArgName);
188 CmdArgs.push_back(".");
189 } else {
190 CmdArgs.push_back(ArgName);
191 CmdArgs.push_back(Args.MakeArgString(Dirs.substr(0, Delim)));
192 }
193 Dirs = Dirs.substr(Delim + 1);
194 }
195
196 if (Dirs.empty()) { // Trailing colon.
197 CmdArgs.push_back(ArgName);
198 CmdArgs.push_back(".");
199 } else { // Add the last path.
200 CmdArgs.push_back(ArgName);
201 CmdArgs.push_back(Args.MakeArgString(Dirs));
202 }
203}
204
Peter Collingbourne54db68b2011-11-06 00:40:05 +0000205void Clang::AddPreprocessingOptions(Compilation &C,
206 const Driver &D,
Douglas Gregordf91ef32009-04-18 00:34:01 +0000207 const ArgList &Args,
Daniel Dunbarc21c4852009-04-08 23:54:23 +0000208 ArgStringList &CmdArgs,
209 const InputInfo &Output,
210 const InputInfoList &Inputs) const {
Daniel Dunbarc21c4852009-04-08 23:54:23 +0000211 Arg *A;
Daniel Dunbar3a183d32009-06-08 21:48:20 +0000212
Daniel Dunbar88a3d6c2009-09-10 01:21:05 +0000213 CheckPreprocessingOptions(D, Args);
214
215 Args.AddLastArg(CmdArgs, options::OPT_C);
216 Args.AddLastArg(CmdArgs, options::OPT_CC);
Daniel Dunbar3a183d32009-06-08 21:48:20 +0000217
218 // Handle dependency file generation.
Daniel Dunbar9eb93b02010-12-08 21:33:40 +0000219 if ((A = Args.getLastArg(options::OPT_M, options::OPT_MM)) ||
Daniel Dunbarc21c4852009-04-08 23:54:23 +0000220 (A = Args.getLastArg(options::OPT_MD)) ||
221 (A = Args.getLastArg(options::OPT_MMD))) {
222 // Determine the output location.
223 const char *DepFile;
224 if (Output.getType() == types::TY_Dependencies) {
Daniel Dunbar7c1e4652010-08-02 02:38:21 +0000225 DepFile = Output.getFilename();
Daniel Dunbarc21c4852009-04-08 23:54:23 +0000226 } else if (Arg *MF = Args.getLastArg(options::OPT_MF)) {
227 DepFile = MF->getValue(Args);
Daniel Dunbarb827a052009-11-19 03:26:40 +0000228 } else if (A->getOption().matches(options::OPT_M) ||
229 A->getOption().matches(options::OPT_MM)) {
Daniel Dunbarc21c4852009-04-08 23:54:23 +0000230 DepFile = "-";
231 } else {
232 DepFile = darwin::CC1::getDependencyFileName(Args, Inputs);
233 }
234 CmdArgs.push_back("-dependency-file");
235 CmdArgs.push_back(DepFile);
236
Chris Lattner3edbeb72010-03-29 17:55:58 +0000237 // Add a default target if one wasn't specified.
Daniel Dunbarc21c4852009-04-08 23:54:23 +0000238 if (!Args.hasArg(options::OPT_MT) && !Args.hasArg(options::OPT_MQ)) {
239 const char *DepTarget;
240
241 // If user provided -o, that is the dependency target, except
242 // when we are only generating a dependency file.
243 Arg *OutputOpt = Args.getLastArg(options::OPT_o);
244 if (OutputOpt && Output.getType() != types::TY_Dependencies) {
245 DepTarget = OutputOpt->getValue(Args);
246 } else {
247 // Otherwise derive from the base input.
248 //
249 // FIXME: This should use the computed output file location.
Michael J. Spencer472ccff2010-12-18 00:19:12 +0000250 llvm::SmallString<128> P(Inputs[0].getBaseInput());
251 llvm::sys::path::replace_extension(P, "o");
252 DepTarget = Args.MakeArgString(llvm::sys::path::filename(P));
Daniel Dunbarc21c4852009-04-08 23:54:23 +0000253 }
254
255 CmdArgs.push_back("-MT");
Chris Lattner3edbeb72010-03-29 17:55:58 +0000256 llvm::SmallString<128> Quoted;
257 QuoteTarget(DepTarget, Quoted);
258 CmdArgs.push_back(Args.MakeArgString(Quoted));
Daniel Dunbarc21c4852009-04-08 23:54:23 +0000259 }
260
Daniel Dunbarb827a052009-11-19 03:26:40 +0000261 if (A->getOption().matches(options::OPT_M) ||
262 A->getOption().matches(options::OPT_MD))
Daniel Dunbarc21c4852009-04-08 23:54:23 +0000263 CmdArgs.push_back("-sys-header-deps");
264 }
265
Peter Collingbournebb527862011-07-12 19:35:15 +0000266 if (Args.hasArg(options::OPT_MG)) {
267 if (!A || A->getOption().matches(options::OPT_MD) ||
268 A->getOption().matches(options::OPT_MMD))
Chris Lattner5f9e2722011-07-23 10:55:15 +0000269 D.Diag(diag::err_drv_mg_requires_m_or_mm);
Peter Collingbournebb527862011-07-12 19:35:15 +0000270 CmdArgs.push_back("-MG");
271 }
272
Daniel Dunbarc21c4852009-04-08 23:54:23 +0000273 Args.AddLastArg(CmdArgs, options::OPT_MP);
Chris Lattner3edbeb72010-03-29 17:55:58 +0000274
275 // Convert all -MQ <target> args to -MT <quoted target>
276 for (arg_iterator it = Args.filtered_begin(options::OPT_MT,
277 options::OPT_MQ),
278 ie = Args.filtered_end(); it != ie; ++it) {
Daniel Dunbar7e4953e2010-06-11 22:00:13 +0000279 const Arg *A = *it;
280 A->claim();
Chris Lattner3edbeb72010-03-29 17:55:58 +0000281
Daniel Dunbar7e4953e2010-06-11 22:00:13 +0000282 if (A->getOption().matches(options::OPT_MQ)) {
Chris Lattner3edbeb72010-03-29 17:55:58 +0000283 CmdArgs.push_back("-MT");
284 llvm::SmallString<128> Quoted;
Daniel Dunbar7e4953e2010-06-11 22:00:13 +0000285 QuoteTarget(A->getValue(Args), Quoted);
Chris Lattner3edbeb72010-03-29 17:55:58 +0000286 CmdArgs.push_back(Args.MakeArgString(Quoted));
287
288 // -MT flag - no change
289 } else {
Daniel Dunbar7e4953e2010-06-11 22:00:13 +0000290 A->render(Args, CmdArgs);
Chris Lattner3edbeb72010-03-29 17:55:58 +0000291 }
292 }
Daniel Dunbarc21c4852009-04-08 23:54:23 +0000293
Douglas Gregordf91ef32009-04-18 00:34:01 +0000294 // Add -i* options, and automatically translate to
295 // -include-pch/-include-pth for transparent PCH support. It's
296 // wonky, but we include looking for .gch so we can support seamless
297 // replacement into a build system already set up to be generating
298 // .gch files.
Argyrios Kyrtzidis990142a2010-09-30 16:53:47 +0000299 bool RenderedImplicitInclude = false;
Daniel Dunbarcdd96862009-11-25 11:53:23 +0000300 for (arg_iterator it = Args.filtered_begin(options::OPT_clang_i_Group),
301 ie = Args.filtered_end(); it != ie; ++it) {
302 const Arg *A = it;
Daniel Dunbarc21c4852009-04-08 23:54:23 +0000303
304 if (A->getOption().matches(options::OPT_include)) {
Argyrios Kyrtzidis990142a2010-09-30 16:53:47 +0000305 bool IsFirstImplicitInclude = !RenderedImplicitInclude;
306 RenderedImplicitInclude = true;
307
Argyrios Kyrtzidise5c35372010-08-11 23:27:58 +0000308 // Use PCH if the user requested it.
Daniel Dunbar0ebd9322009-10-15 20:02:44 +0000309 bool UsePCH = D.CCCUsePCH;
Daniel Dunbar0ebd9322009-10-15 20:02:44 +0000310
Daniel Dunbarc21c4852009-04-08 23:54:23 +0000311 bool FoundPTH = false;
Douglas Gregordf91ef32009-04-18 00:34:01 +0000312 bool FoundPCH = false;
Daniel Dunbarc21c4852009-04-08 23:54:23 +0000313 llvm::sys::Path P(A->getValue(Args));
Michael J. Spencer32bef4e2011-01-10 02:34:13 +0000314 bool Exists;
Daniel Dunbar0ebd9322009-10-15 20:02:44 +0000315 if (UsePCH) {
Douglas Gregordf91ef32009-04-18 00:34:01 +0000316 P.appendSuffix("pch");
Michael J. Spencer32bef4e2011-01-10 02:34:13 +0000317 if (!llvm::sys::fs::exists(P.str(), Exists) && Exists)
Douglas Gregordf91ef32009-04-18 00:34:01 +0000318 FoundPCH = true;
Mike Stump1eb44332009-09-09 15:08:12 +0000319 else
Douglas Gregordf91ef32009-04-18 00:34:01 +0000320 P.eraseSuffix();
Daniel Dunbarc21c4852009-04-08 23:54:23 +0000321 }
322
Douglas Gregordf91ef32009-04-18 00:34:01 +0000323 if (!FoundPCH) {
324 P.appendSuffix("pth");
Michael J. Spencer32bef4e2011-01-10 02:34:13 +0000325 if (!llvm::sys::fs::exists(P.str(), Exists) && Exists)
Douglas Gregordf91ef32009-04-18 00:34:01 +0000326 FoundPTH = true;
327 else
328 P.eraseSuffix();
Mike Stump1eb44332009-09-09 15:08:12 +0000329 }
330
Douglas Gregordf91ef32009-04-18 00:34:01 +0000331 if (!FoundPCH && !FoundPTH) {
332 P.appendSuffix("gch");
Michael J. Spencer32bef4e2011-01-10 02:34:13 +0000333 if (!llvm::sys::fs::exists(P.str(), Exists) && Exists) {
Daniel Dunbar0ebd9322009-10-15 20:02:44 +0000334 FoundPCH = UsePCH;
335 FoundPTH = !UsePCH;
Douglas Gregordf91ef32009-04-18 00:34:01 +0000336 }
Mike Stump1eb44332009-09-09 15:08:12 +0000337 else
Douglas Gregordf91ef32009-04-18 00:34:01 +0000338 P.eraseSuffix();
339 }
340
341 if (FoundPCH || FoundPTH) {
Argyrios Kyrtzidis990142a2010-09-30 16:53:47 +0000342 if (IsFirstImplicitInclude) {
343 A->claim();
344 if (UsePCH)
345 CmdArgs.push_back("-include-pch");
346 else
347 CmdArgs.push_back("-include-pth");
348 CmdArgs.push_back(Args.MakeArgString(P.str()));
349 continue;
350 } else {
351 // Ignore the PCH if not first on command line and emit warning.
Chris Lattner5f9e2722011-07-23 10:55:15 +0000352 D.Diag(diag::warn_drv_pch_not_first_include)
Argyrios Kyrtzidis990142a2010-09-30 16:53:47 +0000353 << P.str() << A->getAsString(Args);
354 }
Daniel Dunbarc21c4852009-04-08 23:54:23 +0000355 }
356 }
357
358 // Not translated, render as usual.
359 A->claim();
360 A->render(Args, CmdArgs);
361 }
362
363 Args.AddAllArgs(CmdArgs, options::OPT_D, options::OPT_U);
Douglas Gregor65e02fa2011-07-28 04:45:53 +0000364 Args.AddAllArgs(CmdArgs, options::OPT_I_Group, options::OPT_F,
365 options::OPT_index_header_map);
Daniel Dunbarc21c4852009-04-08 23:54:23 +0000366
367 // Add -Wp, and -Xassembler if using the preprocessor.
368
369 // FIXME: There is a very unfortunate problem here, some troubled
370 // souls abuse -Wp, to pass preprocessor options in gcc syntax. To
371 // really support that we would have to parse and then translate
372 // those options. :(
373 Args.AddAllArgValues(CmdArgs, options::OPT_Wp_COMMA,
374 options::OPT_Xpreprocessor);
Daniel Dunbar607d7f62009-10-29 01:53:44 +0000375
376 // -I- is a deprecated GCC feature, reject it.
377 if (Arg *A = Args.getLastArg(options::OPT_I_))
Chris Lattner5f9e2722011-07-23 10:55:15 +0000378 D.Diag(diag::err_drv_I_dash_not_supported) << A->getAsString(Args);
Chandler Carruthfeee58c2010-10-20 07:00:47 +0000379
380 // If we have a --sysroot, and don't have an explicit -isysroot flag, add an
381 // -isysroot to the CC1 invocation.
382 if (Arg *A = Args.getLastArg(options::OPT__sysroot_EQ)) {
383 if (!Args.hasArg(options::OPT_isysroot)) {
384 CmdArgs.push_back("-isysroot");
385 CmdArgs.push_back(A->getValue(Args));
386 }
387 }
Douglas Gregor8ee51ef2011-09-14 20:28:46 +0000388
389 // If a module path was provided, pass it along. Otherwise, use a temporary
390 // directory.
391 if (Arg *A = Args.getLastArg(options::OPT_fmodule_cache_path)) {
Douglas Gregor8ee51ef2011-09-14 20:28:46 +0000392 A->claim();
393 A->render(Args, CmdArgs);
394 } else {
395 llvm::SmallString<128> DefaultModuleCache;
396 llvm::sys::path::system_temp_directory(/*erasedOnReboot=*/false,
397 DefaultModuleCache);
398 llvm::sys::path::append(DefaultModuleCache, "clang-module-cache");
399 CmdArgs.push_back("-fmodule-cache-path");
400 CmdArgs.push_back(Args.MakeArgString(DefaultModuleCache));
401 }
Douglas Gregorfba18aa2011-09-15 22:00:41 +0000402
403 Args.AddAllArgs(CmdArgs, options::OPT_fauto_module_import);
Benjamin Kramer47adebe2011-09-22 21:41:16 +0000404
405 // Parse additional include paths from environment variables.
Chandler Carruthb5870e72011-11-04 07:12:58 +0000406 // FIXME: We should probably sink the logic for handling these from the
407 // frontend into the driver. It will allow deleting 4 otherwise unused flags.
Benjamin Kramer47adebe2011-09-22 21:41:16 +0000408 // CPATH - included following the user specified includes (but prior to
409 // builtin and standard includes).
410 AddIncludeDirectoryList(Args, CmdArgs, "-I", ::getenv("CPATH"));
411 // C_INCLUDE_PATH - system includes enabled when compiling C.
412 AddIncludeDirectoryList(Args, CmdArgs, "-c-isystem",
413 ::getenv("C_INCLUDE_PATH"));
414 // CPLUS_INCLUDE_PATH - system includes enabled when compiling C++.
415 AddIncludeDirectoryList(Args, CmdArgs, "-cxx-isystem",
416 ::getenv("CPLUS_INCLUDE_PATH"));
417 // OBJC_INCLUDE_PATH - system includes enabled when compiling ObjC.
418 AddIncludeDirectoryList(Args, CmdArgs, "-objc-isystem",
419 ::getenv("OBJC_INCLUDE_PATH"));
420 // OBJCPLUS_INCLUDE_PATH - system includes enabled when compiling ObjC++.
421 AddIncludeDirectoryList(Args, CmdArgs, "-objcxx-isystem",
422 ::getenv("OBJCPLUS_INCLUDE_PATH"));
Chandler Carruth88491fc2011-11-04 07:12:53 +0000423
Chandler Carruth88491fc2011-11-04 07:12:53 +0000424 // Add C++ include arguments, if needed.
Chandler Carrutha4614422011-11-04 07:43:33 +0000425 if (types::isCXX(Inputs[0].getType()))
Chandler Carruth7ffa0322011-11-04 07:34:47 +0000426 getToolChain().AddClangCXXStdlibIncludeArgs(Args, CmdArgs);
Chandler Carruth7d7e9f92011-11-05 20:17:13 +0000427
428 // Add system include arguments.
429 getToolChain().AddClangSystemIncludeArgs(Args, CmdArgs);
Daniel Dunbarc21c4852009-04-08 23:54:23 +0000430}
431
Chris Lattnerfc8f0e12011-04-15 05:22:18 +0000432/// getARMTargetCPU - Get the (LLVM) name of the ARM cpu we are targeting.
Daniel Dunbar728a5122009-09-10 06:49:20 +0000433//
434// FIXME: tblgen this.
Rafael Espindolabcd6df62010-06-28 17:18:09 +0000435static const char *getARMTargetCPU(const ArgList &Args,
436 const llvm::Triple &Triple) {
Daniel Dunbar728a5122009-09-10 06:49:20 +0000437 // FIXME: Warn on inconsistent use of -mcpu and -march.
Daniel Dunbarb163ef72009-09-10 04:57:17 +0000438
439 // If we have -mcpu=, use that.
440 if (Arg *A = Args.getLastArg(options::OPT_mcpu_EQ))
441 return A->getValue(Args);
442
Chris Lattner5f9e2722011-07-23 10:55:15 +0000443 StringRef MArch;
Daniel Dunbarb163ef72009-09-10 04:57:17 +0000444 if (Arg *A = Args.getLastArg(options::OPT_march_EQ)) {
Rafael Espindolabcd6df62010-06-28 17:18:09 +0000445 // Otherwise, if we have -march= choose the base CPU for that arch.
446 MArch = A->getValue(Args);
447 } else {
448 // Otherwise, use the Arch from the triple.
449 MArch = Triple.getArchName();
Daniel Dunbarb163ef72009-09-10 04:57:17 +0000450 }
451
Chad Rosierae1aee62011-10-07 17:48:56 +0000452 return llvm::StringSwitch<const char *>(MArch)
453 .Cases("armv2", "armv2a","arm2")
454 .Case("armv3", "arm6")
455 .Case("armv3m", "arm7m")
456 .Cases("armv4", "armv4t", "arm7tdmi")
457 .Cases("armv5", "armv5t", "arm10tdmi")
458 .Cases("armv5e", "armv5te", "arm1026ejs")
459 .Case("armv5tej", "arm926ej-s")
460 .Cases("armv6", "armv6k", "arm1136jf-s")
461 .Case("armv6j", "arm1136j-s")
462 .Cases("armv6z", "armv6zk", "arm1176jzf-s")
463 .Case("armv6t2", "arm1156t2-s")
464 .Cases("armv7", "armv7a", "armv7-a", "cortex-a8")
465 .Cases("armv7r", "armv7-r", "cortex-r4")
466 .Cases("armv7m", "armv7-m", "cortex-m3")
467 .Case("ep9312", "ep9312")
468 .Case("iwmmxt", "iwmmxt")
469 .Case("xscale", "xscale")
470 .Cases("armv6m", "armv6-m", "cortex-m0")
471 // If all else failed, return the most base CPU LLVM supports.
472 .Default("arm7tdmi");
Daniel Dunbarb163ef72009-09-10 04:57:17 +0000473}
474
Daniel Dunbar1d65e4b2009-09-10 22:59:51 +0000475/// getLLVMArchSuffixForARM - Get the LLVM arch name to use for a particular
Daniel Dunbar728a5122009-09-10 06:49:20 +0000476/// CPU.
477//
478// FIXME: This is redundant with -mcpu, why does LLVM use this.
479// FIXME: tblgen this, or kill it!
Chris Lattner5f9e2722011-07-23 10:55:15 +0000480static const char *getLLVMArchSuffixForARM(StringRef CPU) {
Chad Rosierae1aee62011-10-07 17:48:56 +0000481 return llvm::StringSwitch<const char *>(CPU)
482 .Cases("arm7tdmi", "arm7tdmi-s", "arm710t", "v4t")
483 .Cases("arm720t", "arm9", "arm9tdmi", "v4t")
484 .Cases("arm920", "arm920t", "arm922t", "v4t")
485 .Cases("arm940t", "ep9312","v4t")
486 .Cases("arm10tdmi", "arm1020t", "v5")
487 .Cases("arm9e", "arm926ej-s", "arm946e-s", "v5e")
488 .Cases("arm966e-s", "arm968e-s", "arm10e", "v5e")
489 .Cases("arm1020e", "arm1022e", "xscale", "iwmmxt", "v5e")
490 .Cases("arm1136j-s", "arm1136jf-s", "arm1176jz-s", "v6")
491 .Cases("arm1176jzf-s", "mpcorenovfp", "mpcore", "v6")
492 .Cases("arm1156t2-s", "arm1156t2f-s", "v6t2")
493 .Cases("cortex-a8", "cortex-a9", "v7")
494 .Default("");
Daniel Dunbar728a5122009-09-10 06:49:20 +0000495}
496
Daniel Dunbar1f95e652009-11-17 06:37:03 +0000497// FIXME: Move to target hook.
498static bool isSignedCharDefault(const llvm::Triple &Triple) {
499 switch (Triple.getArch()) {
500 default:
501 return true;
502
Jim Grosbach5b4e7b12011-05-24 15:40:46 +0000503 case llvm::Triple::arm:
Daniel Dunbar1f95e652009-11-17 06:37:03 +0000504 case llvm::Triple::ppc:
505 case llvm::Triple::ppc64:
Bob Wilson905c45f2011-10-14 05:03:44 +0000506 if (Triple.isOSDarwin())
Daniel Dunbar1f95e652009-11-17 06:37:03 +0000507 return true;
508 return false;
Daniel Dunbar1f95e652009-11-17 06:37:03 +0000509 }
510}
511
Daniel Dunbarb163ef72009-09-10 04:57:17 +0000512void Clang::AddARMTargetArgs(const ArgList &Args,
Daniel Dunbarfa41d692011-03-17 17:10:06 +0000513 ArgStringList &CmdArgs,
514 bool KernelOrKext) const {
Daniel Dunbaree788e72009-12-21 18:54:17 +0000515 const Driver &D = getToolChain().getDriver();
Rafael Espindolabcd6df62010-06-28 17:18:09 +0000516 llvm::Triple Triple = getToolChain().getTriple();
Daniel Dunbarcbd19332009-09-10 23:00:09 +0000517
Daniel Dunbar2030d8f2009-09-14 00:34:46 +0000518 // Select the ABI to use.
519 //
520 // FIXME: Support -meabi.
521 const char *ABIName = 0;
522 if (Arg *A = Args.getLastArg(options::OPT_mabi_EQ)) {
523 ABIName = A->getValue(Args);
524 } else {
525 // Select the default based on the platform.
Bob Wilsonfc2bd7c2011-02-04 17:59:28 +0000526 switch(Triple.getEnvironment()) {
527 case llvm::Triple::GNUEABI:
Daniel Dunbar2030d8f2009-09-14 00:34:46 +0000528 ABIName = "aapcs-linux";
Bob Wilsonfc2bd7c2011-02-04 17:59:28 +0000529 break;
530 case llvm::Triple::EABI:
Rafael Espindola1ed1a592010-06-16 19:01:17 +0000531 ABIName = "aapcs";
Bob Wilsonfc2bd7c2011-02-04 17:59:28 +0000532 break;
533 default:
Rafael Espindola1ed1a592010-06-16 19:01:17 +0000534 ABIName = "apcs-gnu";
Bob Wilsonfc2bd7c2011-02-04 17:59:28 +0000535 }
Daniel Dunbar2030d8f2009-09-14 00:34:46 +0000536 }
537 CmdArgs.push_back("-target-abi");
538 CmdArgs.push_back(ABIName);
539
Daniel Dunbarb163ef72009-09-10 04:57:17 +0000540 // Set the CPU based on -march= and -mcpu=.
Daniel Dunbar38b48af2009-12-18 06:30:12 +0000541 CmdArgs.push_back("-target-cpu");
Rafael Espindolabcd6df62010-06-28 17:18:09 +0000542 CmdArgs.push_back(getARMTargetCPU(Args, Triple));
Daniel Dunbarb163ef72009-09-10 04:57:17 +0000543
Daniel Dunbarcbd19332009-09-10 23:00:09 +0000544 // Select the float ABI as determined by -msoft-float, -mhard-float, and
545 // -mfloat-abi=.
Chris Lattner5f9e2722011-07-23 10:55:15 +0000546 StringRef FloatABI;
Daniel Dunbarcbd19332009-09-10 23:00:09 +0000547 if (Arg *A = Args.getLastArg(options::OPT_msoft_float,
548 options::OPT_mhard_float,
549 options::OPT_mfloat_abi_EQ)) {
550 if (A->getOption().matches(options::OPT_msoft_float))
551 FloatABI = "soft";
552 else if (A->getOption().matches(options::OPT_mhard_float))
553 FloatABI = "hard";
554 else {
555 FloatABI = A->getValue(Args);
556 if (FloatABI != "soft" && FloatABI != "softfp" && FloatABI != "hard") {
Chris Lattner5f9e2722011-07-23 10:55:15 +0000557 D.Diag(diag::err_drv_invalid_mfloat_abi)
Daniel Dunbarcbd19332009-09-10 23:00:09 +0000558 << A->getAsString(Args);
559 FloatABI = "soft";
560 }
561 }
562 }
563
564 // If unspecified, choose the default based on the platform.
565 if (FloatABI.empty()) {
Rafael Espindolabcd6df62010-06-28 17:18:09 +0000566 const llvm::Triple &Triple = getToolChain().getTriple();
567 switch (Triple.getOS()) {
Bob Wilson905c45f2011-10-14 05:03:44 +0000568 case llvm::Triple::Darwin:
569 case llvm::Triple::MacOSX:
570 case llvm::Triple::IOS: {
Daniel Dunbarcbd19332009-09-10 23:00:09 +0000571 // Darwin defaults to "softfp" for v6 and v7.
572 //
573 // FIXME: Factor out an ARM class so we can cache the arch somewhere.
Chris Lattner5f9e2722011-07-23 10:55:15 +0000574 StringRef ArchName =
Rafael Espindolabcd6df62010-06-28 17:18:09 +0000575 getLLVMArchSuffixForARM(getARMTargetCPU(Args, Triple));
Daniel Dunbarcbd19332009-09-10 23:00:09 +0000576 if (ArchName.startswith("v6") || ArchName.startswith("v7"))
577 FloatABI = "softfp";
578 else
579 FloatABI = "soft";
580 break;
581 }
582
Rafael Espindola7e9cebf2010-06-27 18:29:21 +0000583 case llvm::Triple::Linux: {
Bob Wilsonfc2bd7c2011-02-04 17:59:28 +0000584 if (getToolChain().getTriple().getEnvironment() == llvm::Triple::GNUEABI) {
Rafael Espindola7e9cebf2010-06-27 18:29:21 +0000585 FloatABI = "softfp";
586 break;
587 }
588 }
589 // fall through
590
Daniel Dunbarcbd19332009-09-10 23:00:09 +0000591 default:
Bob Wilsonfc2bd7c2011-02-04 17:59:28 +0000592 switch(Triple.getEnvironment()) {
593 case llvm::Triple::GNUEABI:
594 FloatABI = "softfp";
595 break;
596 case llvm::Triple::EABI:
597 // EABI is always AAPCS, and if it was not marked 'hard', it's softfp
598 FloatABI = "softfp";
599 break;
600 default:
601 // Assume "soft", but warn the user we are guessing.
602 FloatABI = "soft";
Chris Lattner5f9e2722011-07-23 10:55:15 +0000603 D.Diag(diag::warn_drv_assuming_mfloat_abi_is) << "soft";
Bob Wilsonfc2bd7c2011-02-04 17:59:28 +0000604 break;
605 }
Daniel Dunbarcbd19332009-09-10 23:00:09 +0000606 }
607 }
608
609 if (FloatABI == "soft") {
610 // Floating point operations and argument passing are soft.
611 //
612 // FIXME: This changes CPP defines, we need -target-soft-float.
Daniel Dunbar3b315262009-11-30 08:42:00 +0000613 CmdArgs.push_back("-msoft-float");
Daniel Dunbar87667aa2009-12-08 19:49:51 +0000614 CmdArgs.push_back("-mfloat-abi");
615 CmdArgs.push_back("soft");
Daniel Dunbarcbd19332009-09-10 23:00:09 +0000616 } else if (FloatABI == "softfp") {
617 // Floating point operations are hard, but argument passing is soft.
Daniel Dunbar87667aa2009-12-08 19:49:51 +0000618 CmdArgs.push_back("-mfloat-abi");
619 CmdArgs.push_back("soft");
Daniel Dunbarcbd19332009-09-10 23:00:09 +0000620 } else {
621 // Floating point operations and argument passing are hard.
622 assert(FloatABI == "hard" && "Invalid float abi!");
Daniel Dunbar87667aa2009-12-08 19:49:51 +0000623 CmdArgs.push_back("-mfloat-abi");
624 CmdArgs.push_back("hard");
Daniel Dunbarcbd19332009-09-10 23:00:09 +0000625 }
Daniel Dunbar97f52ac2009-12-19 04:15:38 +0000626
627 // Set appropriate target features for floating point mode.
628 //
629 // FIXME: Note, this is a hack, the LLVM backend doesn't actually use these
630 // yet (it uses the -mfloat-abi and -msoft-float options above), and it is
631 // stripped out by the ARM target.
632
633 // Use software floating point operations?
634 if (FloatABI == "soft") {
635 CmdArgs.push_back("-target-feature");
636 CmdArgs.push_back("+soft-float");
637 }
638
639 // Use software floating point argument passing?
640 if (FloatABI != "hard") {
641 CmdArgs.push_back("-target-feature");
642 CmdArgs.push_back("+soft-float-abi");
643 }
Daniel Dunbara91320b2009-12-21 23:28:17 +0000644
645 // Honor -mfpu=.
646 //
647 // FIXME: Centralize feature selection, defaulting shouldn't be also in the
648 // frontend target.
649 if (const Arg *A = Args.getLastArg(options::OPT_mfpu_EQ)) {
Chris Lattner5f9e2722011-07-23 10:55:15 +0000650 StringRef FPU = A->getValue(Args);
Daniel Dunbara91320b2009-12-21 23:28:17 +0000651
652 // Set the target features based on the FPU.
653 if (FPU == "fpa" || FPU == "fpe2" || FPU == "fpe3" || FPU == "maverick") {
654 // Disable any default FPU support.
655 CmdArgs.push_back("-target-feature");
656 CmdArgs.push_back("-vfp2");
657 CmdArgs.push_back("-target-feature");
658 CmdArgs.push_back("-vfp3");
659 CmdArgs.push_back("-target-feature");
660 CmdArgs.push_back("-neon");
661 } else if (FPU == "vfp") {
662 CmdArgs.push_back("-target-feature");
663 CmdArgs.push_back("+vfp2");
664 } else if (FPU == "vfp3") {
665 CmdArgs.push_back("-target-feature");
666 CmdArgs.push_back("+vfp3");
667 } else if (FPU == "neon") {
668 CmdArgs.push_back("-target-feature");
669 CmdArgs.push_back("+neon");
670 } else
Chris Lattner5f9e2722011-07-23 10:55:15 +0000671 D.Diag(diag::err_drv_clang_unsupported) << A->getAsString(Args);
Daniel Dunbara91320b2009-12-21 23:28:17 +0000672 }
Daniel Dunbar7187fac2011-03-17 00:07:34 +0000673
674 // Setting -msoft-float effectively disables NEON because of the GCC
675 // implementation, although the same isn't true of VFP or VFP3.
676 if (FloatABI == "soft") {
Daniel Dunbarfa41d692011-03-17 17:10:06 +0000677 CmdArgs.push_back("-target-feature");
678 CmdArgs.push_back("-neon");
679 }
680
681 // Kernel code has more strict alignment requirements.
682 if (KernelOrKext) {
Daniel Dunbar3c66d302011-03-22 16:48:17 +0000683 CmdArgs.push_back("-backend-option");
Daniel Dunbarfa41d692011-03-17 17:10:06 +0000684 CmdArgs.push_back("-arm-long-calls");
685
Daniel Dunbar3c66d302011-03-22 16:48:17 +0000686 CmdArgs.push_back("-backend-option");
Daniel Dunbarfa41d692011-03-17 17:10:06 +0000687 CmdArgs.push_back("-arm-strict-align");
Daniel Dunbarb5fbb892011-04-18 21:26:42 +0000688
689 // The kext linker doesn't know how to deal with movw/movt.
Daniel Dunbarb5fbb892011-04-18 21:26:42 +0000690 CmdArgs.push_back("-backend-option");
691 CmdArgs.push_back("-arm-darwin-use-movt=0");
Daniel Dunbar7187fac2011-03-17 00:07:34 +0000692 }
Chad Rosier1b906052011-08-26 00:26:29 +0000693
694 // Setting -mno-global-merge disables the codegen global merge pass. Setting
695 // -mglobal-merge has no effect as the pass is enabled by default.
696 if (Arg *A = Args.getLastArg(options::OPT_mglobal_merge,
697 options::OPT_mno_global_merge)) {
698 if (A->getOption().matches(options::OPT_mno_global_merge))
699 CmdArgs.push_back("-mno-global-merge");
700 }
Daniel Dunbarb163ef72009-09-10 04:57:17 +0000701}
702
Akira Hatanaka9f360622011-09-26 21:07:52 +0000703// Get default architecture.
704static const char* getMipsArchFromCPU(StringRef CPUName) {
705 if (CPUName == "mips32r1" || CPUName == "4ke")
706 return "mips";
707
708 assert((CPUName == "mips64r1" || CPUName == "mips64r2") &&
709 "Unexpected cpu name.");
710
711 return "mips64";
712}
713
714// Get default target cpu.
715static const char* getMipsCPUFromArch(StringRef ArchName, const Driver &D) {
716 if (ArchName == "mips" || ArchName == "mipsel")
717 return "mips32r1";
718 else if (ArchName == "mips64" || ArchName == "mips64el")
719 return "mips64r1";
720 else
721 D.Diag(diag::err_drv_invalid_arch_name) << ArchName;
722
723 return 0;
724}
725
726// Get default ABI.
727static const char* getMipsABIFromArch(StringRef ArchName) {
728 if (ArchName == "mips" || ArchName == "mipsel")
729 return "o32";
730
731 assert((ArchName == "mips64" || ArchName == "mips64el") &&
732 "Unexpected arch name.");
733 return "n64";
734}
735
Eric Christophered734732010-03-02 02:41:08 +0000736void Clang::AddMIPSTargetArgs(const ArgList &Args,
737 ArgStringList &CmdArgs) const {
738 const Driver &D = getToolChain().getDriver();
739
Akira Hatanaka9f360622011-09-26 21:07:52 +0000740 StringRef ArchName;
741 const char *CPUName;
742
743 // Set target cpu and architecture.
744 if (Arg *A = Args.getLastArg(options::OPT_mcpu_EQ)) {
745 CPUName = A->getValue(Args);
746 ArchName = getMipsArchFromCPU(CPUName);
747 }
748 else {
749 ArchName = Args.MakeArgString(getToolChain().getArchName());
750 CPUName = getMipsCPUFromArch(ArchName, D);
751 }
752
753 CmdArgs.push_back("-target-cpu");
754 CmdArgs.push_back(CPUName);
755
Eric Christophered734732010-03-02 02:41:08 +0000756 // Select the ABI to use.
757 const char *ABIName = 0;
Akira Hatanaka9f360622011-09-26 21:07:52 +0000758 if (Arg *A = Args.getLastArg(options::OPT_mabi_EQ))
Eric Christophered734732010-03-02 02:41:08 +0000759 ABIName = A->getValue(Args);
Akira Hatanaka9f360622011-09-26 21:07:52 +0000760 else
761 ABIName = getMipsABIFromArch(ArchName);
Eric Christophered734732010-03-02 02:41:08 +0000762
763 CmdArgs.push_back("-target-abi");
764 CmdArgs.push_back(ABIName);
765
Eric Christophered734732010-03-02 02:41:08 +0000766 // Select the float ABI as determined by -msoft-float, -mhard-float, and
Chris Lattner5f9e2722011-07-23 10:55:15 +0000767 StringRef FloatABI;
Eric Christophered734732010-03-02 02:41:08 +0000768 if (Arg *A = Args.getLastArg(options::OPT_msoft_float,
769 options::OPT_mhard_float)) {
770 if (A->getOption().matches(options::OPT_msoft_float))
771 FloatABI = "soft";
772 else if (A->getOption().matches(options::OPT_mhard_float))
773 FloatABI = "hard";
774 }
775
776 // If unspecified, choose the default based on the platform.
777 if (FloatABI.empty()) {
Benjamin Kramer71c972a2010-04-08 15:44:22 +0000778 // Assume "soft", but warn the user we are guessing.
779 FloatABI = "soft";
Chris Lattner5f9e2722011-07-23 10:55:15 +0000780 D.Diag(diag::warn_drv_assuming_mfloat_abi_is) << "soft";
Eric Christophered734732010-03-02 02:41:08 +0000781 }
782
783 if (FloatABI == "soft") {
784 // Floating point operations and argument passing are soft.
785 //
786 // FIXME: This changes CPP defines, we need -target-soft-float.
787 CmdArgs.push_back("-msoft-float");
788 } else {
789 assert(FloatABI == "hard" && "Invalid float abi!");
790 CmdArgs.push_back("-mhard-float");
791 }
792}
793
Bruno Cardoso Lopes9284d212010-11-09 17:21:19 +0000794void Clang::AddSparcTargetArgs(const ArgList &Args,
795 ArgStringList &CmdArgs) const {
796 const Driver &D = getToolChain().getDriver();
797
798 if (const Arg *A = Args.getLastArg(options::OPT_march_EQ)) {
Chris Lattner5f9e2722011-07-23 10:55:15 +0000799 StringRef MArch = A->getValue(Args);
Bruno Cardoso Lopes9284d212010-11-09 17:21:19 +0000800 CmdArgs.push_back("-target-cpu");
801 CmdArgs.push_back(MArch.str().c_str());
802 }
803
804 // Select the float ABI as determined by -msoft-float, -mhard-float, and
Chris Lattner5f9e2722011-07-23 10:55:15 +0000805 StringRef FloatABI;
Bruno Cardoso Lopes9284d212010-11-09 17:21:19 +0000806 if (Arg *A = Args.getLastArg(options::OPT_msoft_float,
807 options::OPT_mhard_float)) {
808 if (A->getOption().matches(options::OPT_msoft_float))
809 FloatABI = "soft";
810 else if (A->getOption().matches(options::OPT_mhard_float))
811 FloatABI = "hard";
812 }
813
814 // If unspecified, choose the default based on the platform.
815 if (FloatABI.empty()) {
816 switch (getToolChain().getTriple().getOS()) {
817 default:
818 // Assume "soft", but warn the user we are guessing.
819 FloatABI = "soft";
Chris Lattner5f9e2722011-07-23 10:55:15 +0000820 D.Diag(diag::warn_drv_assuming_mfloat_abi_is) << "soft";
Bruno Cardoso Lopes9284d212010-11-09 17:21:19 +0000821 break;
822 }
823 }
824
825 if (FloatABI == "soft") {
826 // Floating point operations and argument passing are soft.
827 //
828 // FIXME: This changes CPP defines, we need -target-soft-float.
829 CmdArgs.push_back("-msoft-float");
Bruno Cardoso Lopes9284d212010-11-09 17:21:19 +0000830 CmdArgs.push_back("-target-feature");
831 CmdArgs.push_back("+soft-float");
832 } else {
833 assert(FloatABI == "hard" && "Invalid float abi!");
834 CmdArgs.push_back("-mhard-float");
835 }
836}
837
Daniel Dunbar6acda162009-09-09 22:33:08 +0000838void Clang::AddX86TargetArgs(const ArgList &Args,
839 ArgStringList &CmdArgs) const {
Daniel Dunbare6ad3f92009-09-10 22:59:57 +0000840 if (!Args.hasFlag(options::OPT_mred_zone,
841 options::OPT_mno_red_zone,
842 true) ||
843 Args.hasArg(options::OPT_mkernel) ||
844 Args.hasArg(options::OPT_fapple_kext))
Daniel Dunbar66861e02009-11-20 22:21:36 +0000845 CmdArgs.push_back("-disable-red-zone");
Daniel Dunbare6ad3f92009-09-10 22:59:57 +0000846
Daniel Dunbare6ad3f92009-09-10 22:59:57 +0000847 if (Args.hasFlag(options::OPT_msoft_float,
848 options::OPT_mno_soft_float,
849 false))
Daniel Dunbar66861e02009-11-20 22:21:36 +0000850 CmdArgs.push_back("-no-implicit-float");
Daniel Dunbare6ad3f92009-09-10 22:59:57 +0000851
Daniel Dunbarf86fedd2009-11-14 22:04:54 +0000852 const char *CPUName = 0;
Daniel Dunbar6acda162009-09-09 22:33:08 +0000853 if (const Arg *A = Args.getLastArg(options::OPT_march_EQ)) {
Chris Lattner5f9e2722011-07-23 10:55:15 +0000854 if (StringRef(A->getValue(Args)) == "native") {
Daniel Dunbarf86fedd2009-11-14 22:04:54 +0000855 // FIXME: Reject attempts to use -march=native unless the target matches
856 // the host.
857 //
858 // FIXME: We should also incorporate the detected target features for use
859 // with -native.
860 std::string CPU = llvm::sys::getHostCPUName();
861 if (!CPU.empty())
862 CPUName = Args.MakeArgString(CPU);
863 } else
864 CPUName = A->getValue(Args);
865 }
Daniel Dunbar6acda162009-09-09 22:33:08 +0000866
Daniel Dunbarf86fedd2009-11-14 22:04:54 +0000867 // Select the default CPU if none was given (or detection failed).
868 if (!CPUName) {
Daniel Dunbar6acda162009-09-09 22:33:08 +0000869 // FIXME: Need target hooks.
Bob Wilson905c45f2011-10-14 05:03:44 +0000870 if (getToolChain().getTriple().isOSDarwin()) {
Daniel Dunbar951733d2011-05-31 15:58:55 +0000871 if (getToolChain().getArch() == llvm::Triple::x86_64)
Daniel Dunbarf86fedd2009-11-14 22:04:54 +0000872 CPUName = "core2";
Daniel Dunbar951733d2011-05-31 15:58:55 +0000873 else if (getToolChain().getArch() == llvm::Triple::x86)
Daniel Dunbarf86fedd2009-11-14 22:04:54 +0000874 CPUName = "yonah";
Chris Lattner86ed3a32010-04-11 19:29:39 +0000875 } else if (getToolChain().getOS().startswith("haiku")) {
Daniel Dunbar951733d2011-05-31 15:58:55 +0000876 if (getToolChain().getArch() == llvm::Triple::x86_64)
Chris Lattner86ed3a32010-04-11 19:29:39 +0000877 CPUName = "x86-64";
Daniel Dunbar951733d2011-05-31 15:58:55 +0000878 else if (getToolChain().getArch() == llvm::Triple::x86)
Chris Lattner86ed3a32010-04-11 19:29:39 +0000879 CPUName = "i586";
Daniel Dunbar95c04572010-08-01 23:13:54 +0000880 } else if (getToolChain().getOS().startswith("openbsd")) {
Daniel Dunbar951733d2011-05-31 15:58:55 +0000881 if (getToolChain().getArch() == llvm::Triple::x86_64)
Daniel Dunbar95c04572010-08-01 23:13:54 +0000882 CPUName = "x86-64";
Daniel Dunbar951733d2011-05-31 15:58:55 +0000883 else if (getToolChain().getArch() == llvm::Triple::x86)
Daniel Dunbar95c04572010-08-01 23:13:54 +0000884 CPUName = "i486";
Roman Divacky451f8ca2011-03-01 18:11:37 +0000885 } else if (getToolChain().getOS().startswith("freebsd")) {
Daniel Dunbar951733d2011-05-31 15:58:55 +0000886 if (getToolChain().getArch() == llvm::Triple::x86_64)
Roman Divacky451f8ca2011-03-01 18:11:37 +0000887 CPUName = "x86-64";
Daniel Dunbar951733d2011-05-31 15:58:55 +0000888 else if (getToolChain().getArch() == llvm::Triple::x86)
Roman Divacky451f8ca2011-03-01 18:11:37 +0000889 CPUName = "i486";
Benjamin Kramer8e50a962011-02-02 18:59:27 +0000890 } else if (getToolChain().getOS().startswith("netbsd")) {
Daniel Dunbar951733d2011-05-31 15:58:55 +0000891 if (getToolChain().getArch() == llvm::Triple::x86_64)
Benjamin Kramer8e50a962011-02-02 18:59:27 +0000892 CPUName = "x86-64";
Daniel Dunbar951733d2011-05-31 15:58:55 +0000893 else if (getToolChain().getArch() == llvm::Triple::x86)
Benjamin Kramer8e50a962011-02-02 18:59:27 +0000894 CPUName = "i486";
Daniel Dunbar6acda162009-09-09 22:33:08 +0000895 } else {
Daniel Dunbar951733d2011-05-31 15:58:55 +0000896 if (getToolChain().getArch() == llvm::Triple::x86_64)
Daniel Dunbarf86fedd2009-11-14 22:04:54 +0000897 CPUName = "x86-64";
Daniel Dunbar951733d2011-05-31 15:58:55 +0000898 else if (getToolChain().getArch() == llvm::Triple::x86)
Daniel Dunbarf86fedd2009-11-14 22:04:54 +0000899 CPUName = "pentium4";
Daniel Dunbar6acda162009-09-09 22:33:08 +0000900 }
901 }
902
Daniel Dunbarf86fedd2009-11-14 22:04:54 +0000903 if (CPUName) {
Daniel Dunbar38b48af2009-12-18 06:30:12 +0000904 CmdArgs.push_back("-target-cpu");
Daniel Dunbarf86fedd2009-11-14 22:04:54 +0000905 CmdArgs.push_back(CPUName);
906 }
907
Eli Friedmand18eeca2011-07-02 00:34:19 +0000908 // The required algorithm here is slightly strange: the options are applied
909 // in order (so -mno-sse -msse2 disables SSE3), but any option that gets
910 // directly overridden later is ignored (so "-mno-sse -msse2 -mno-sse2 -msse"
911 // is equivalent to "-mno-sse2 -msse"). The -cc1 handling deals with the
912 // former correctly, but not the latter; handle directly-overridden
913 // attributes here.
914 llvm::StringMap<unsigned> PrevFeature;
915 std::vector<const char*> Features;
Daniel Dunbarcdd96862009-11-25 11:53:23 +0000916 for (arg_iterator it = Args.filtered_begin(options::OPT_m_x86_Features_Group),
917 ie = Args.filtered_end(); it != ie; ++it) {
Chris Lattner5f9e2722011-07-23 10:55:15 +0000918 StringRef Name = (*it)->getOption().getName();
Daniel Dunbar7e4953e2010-06-11 22:00:13 +0000919 (*it)->claim();
Daniel Dunbar6acda162009-09-09 22:33:08 +0000920
Daniel Dunbarcdd96862009-11-25 11:53:23 +0000921 // Skip over "-m".
922 assert(Name.startswith("-m") && "Invalid feature name.");
923 Name = Name.substr(2);
Daniel Dunbar6acda162009-09-09 22:33:08 +0000924
Daniel Dunbarcdd96862009-11-25 11:53:23 +0000925 bool IsNegative = Name.startswith("no-");
926 if (IsNegative)
927 Name = Name.substr(3);
Daniel Dunbar6acda162009-09-09 22:33:08 +0000928
Eli Friedmand18eeca2011-07-02 00:34:19 +0000929 unsigned& Prev = PrevFeature[Name];
930 if (Prev)
931 Features[Prev - 1] = 0;
932 Prev = Features.size() + 1;
933 Features.push_back(Args.MakeArgString((IsNegative ? "-" : "+") + Name));
934 }
935 for (unsigned i = 0; i < Features.size(); i++) {
936 if (Features[i]) {
937 CmdArgs.push_back("-target-feature");
938 CmdArgs.push_back(Features[i]);
939 }
Daniel Dunbar6acda162009-09-09 22:33:08 +0000940 }
941}
942
Eric Christopher88b7cf02011-08-19 00:30:14 +0000943static bool
John McCalld71315c2011-06-22 00:53:57 +0000944shouldUseExceptionTablesForObjCExceptions(unsigned objcABIVersion,
Anders Carlsson525544d2011-02-28 00:44:51 +0000945 const llvm::Triple &Triple) {
946 // We use the zero-cost exception tables for Objective-C if the non-fragile
947 // ABI is enabled or when compiling for x86_64 and ARM on Snow Leopard and
948 // later.
949
John McCalld71315c2011-06-22 00:53:57 +0000950 if (objcABIVersion >= 2)
Anders Carlsson525544d2011-02-28 00:44:51 +0000951 return true;
952
Bob Wilson905c45f2011-10-14 05:03:44 +0000953 if (!Triple.isOSDarwin())
Anders Carlsson525544d2011-02-28 00:44:51 +0000954 return false;
955
Eric Christopheraa7333c2011-07-02 00:20:22 +0000956 return (!Triple.isMacOSXVersionLT(10,5) &&
Anders Carlsson525544d2011-02-28 00:44:51 +0000957 (Triple.getArch() == llvm::Triple::x86_64 ||
Eric Christopher88b7cf02011-08-19 00:30:14 +0000958 Triple.getArch() == llvm::Triple::arm));
Anders Carlsson525544d2011-02-28 00:44:51 +0000959}
960
Anders Carlsson15348ae2011-02-28 02:27:16 +0000961/// addExceptionArgs - Adds exception related arguments to the driver command
962/// arguments. There's a master flag, -fexceptions and also language specific
963/// flags to enable/disable C++ and Objective-C exceptions.
964/// This makes it possible to for example disable C++ exceptions but enable
965/// Objective-C exceptions.
966static void addExceptionArgs(const ArgList &Args, types::ID InputType,
967 const llvm::Triple &Triple,
968 bool KernelOrKext, bool IsRewriter,
John McCalld71315c2011-06-22 00:53:57 +0000969 unsigned objcABIVersion,
Anders Carlsson15348ae2011-02-28 02:27:16 +0000970 ArgStringList &CmdArgs) {
971 if (KernelOrKext)
972 return;
973
974 // Exceptions are enabled by default.
975 bool ExceptionsEnabled = true;
976
977 // This keeps track of whether exceptions were explicitly turned on or off.
978 bool DidHaveExplicitExceptionFlag = false;
979
Rafael Espindolaf759df02009-10-01 13:33:33 +0000980 if (Arg *A = Args.getLastArg(options::OPT_fexceptions,
981 options::OPT_fno_exceptions)) {
982 if (A->getOption().matches(options::OPT_fexceptions))
Anders Carlsson15348ae2011-02-28 02:27:16 +0000983 ExceptionsEnabled = true;
Eric Christopher88b7cf02011-08-19 00:30:14 +0000984 else
Anders Carlsson15348ae2011-02-28 02:27:16 +0000985 ExceptionsEnabled = false;
986
987 DidHaveExplicitExceptionFlag = true;
Rafael Espindolaf759df02009-10-01 13:33:33 +0000988 }
Daniel Dunbar1a2cd4f2010-09-14 23:12:31 +0000989
Anders Carlsson15348ae2011-02-28 02:27:16 +0000990 bool ShouldUseExceptionTables = false;
Fariborz Jahanian85caf032009-10-01 20:30:46 +0000991
Anders Carlsson15348ae2011-02-28 02:27:16 +0000992 // Exception tables and cleanups can be enabled with -fexceptions even if the
993 // language itself doesn't support exceptions.
994 if (ExceptionsEnabled && DidHaveExplicitExceptionFlag)
995 ShouldUseExceptionTables = true;
Daniel Dunbar1a2cd4f2010-09-14 23:12:31 +0000996
Daniel Dunbard47ea692011-03-17 23:28:31 +0000997 // Obj-C exceptions are enabled by default, regardless of -fexceptions. This
998 // is not necessarily sensible, but follows GCC.
999 if (types::isObjC(InputType) &&
Eric Christopher88b7cf02011-08-19 00:30:14 +00001000 Args.hasFlag(options::OPT_fobjc_exceptions,
Daniel Dunbard47ea692011-03-17 23:28:31 +00001001 options::OPT_fno_objc_exceptions,
1002 true)) {
1003 CmdArgs.push_back("-fobjc-exceptions");
Anders Carlsson15348ae2011-02-28 02:27:16 +00001004
Eric Christopher88b7cf02011-08-19 00:30:14 +00001005 ShouldUseExceptionTables |=
John McCalld71315c2011-06-22 00:53:57 +00001006 shouldUseExceptionTablesForObjCExceptions(objcABIVersion, Triple);
Anders Carlsson15348ae2011-02-28 02:27:16 +00001007 }
1008
1009 if (types::isCXX(InputType)) {
1010 bool CXXExceptionsEnabled = ExceptionsEnabled;
1011
Eric Christopher88b7cf02011-08-19 00:30:14 +00001012 if (Arg *A = Args.getLastArg(options::OPT_fcxx_exceptions,
1013 options::OPT_fno_cxx_exceptions,
Anders Carlsson15348ae2011-02-28 02:27:16 +00001014 options::OPT_fexceptions,
1015 options::OPT_fno_exceptions)) {
1016 if (A->getOption().matches(options::OPT_fcxx_exceptions))
1017 CXXExceptionsEnabled = true;
Chandler Carruth43f220f2011-02-28 07:25:18 +00001018 else if (A->getOption().matches(options::OPT_fno_cxx_exceptions))
Anders Carlsson15348ae2011-02-28 02:27:16 +00001019 CXXExceptionsEnabled = false;
1020 }
1021
1022 if (CXXExceptionsEnabled) {
1023 CmdArgs.push_back("-fcxx-exceptions");
1024
1025 ShouldUseExceptionTables = true;
1026 }
1027 }
1028
1029 if (ShouldUseExceptionTables)
1030 CmdArgs.push_back("-fexceptions");
Rafael Espindolaf759df02009-10-01 13:33:33 +00001031}
1032
Rafael Espindola61b1efe2011-05-02 17:43:32 +00001033static bool ShouldDisableCFI(const ArgList &Args,
1034 const ToolChain &TC) {
Bob Wilson905c45f2011-10-14 05:03:44 +00001035 if (TC.getTriple().isOSDarwin()) {
Rafael Espindola97f6abb2011-05-17 16:26:17 +00001036 // The native darwin assembler doesn't support cfi directives, so
Rafael Espindolacb773922011-05-17 19:06:58 +00001037 // we disable them if we think the .s file will be passed to it.
Rafael Espindola97f6abb2011-05-17 16:26:17 +00001038 bool UseIntegratedAs = Args.hasFlag(options::OPT_integrated_as,
1039 options::OPT_no_integrated_as,
Bob Wilson1a1764b2011-10-30 00:20:28 +00001040 TC.IsIntegratedAssemblerDefault());
Rafael Espindola97f6abb2011-05-17 16:26:17 +00001041 bool UseCFI = Args.hasFlag(options::OPT_fdwarf2_cfi_asm,
1042 options::OPT_fno_dwarf2_cfi_asm,
1043 UseIntegratedAs);
1044 return !UseCFI;
1045 }
1046
1047 // For now we assume that every other assembler support CFI.
1048 return false;
Rafael Espindola61b1efe2011-05-02 17:43:32 +00001049}
1050
Nick Lewyckyea523d72011-10-17 23:05:52 +00001051static bool ShouldDisableDwarfDirectory(const ArgList &Args,
1052 const ToolChain &TC) {
1053 bool IsIADefault = TC.IsIntegratedAssemblerDefault();
1054 bool UseIntegratedAs = Args.hasFlag(options::OPT_integrated_as,
1055 options::OPT_no_integrated_as,
1056 IsIADefault);
1057 bool UseDwarfDirectory = Args.hasFlag(options::OPT_fdwarf_directory_asm,
1058 options::OPT_fno_dwarf_directory_asm,
1059 UseIntegratedAs);
1060 return !UseDwarfDirectory;
1061}
1062
Joerg Sonnenberger359cf922011-05-06 14:35:16 +00001063/// \brief Check whether the given input tree contains any compilation actions.
1064static bool ContainsCompileAction(const Action *A) {
Chris Lattner5f9e2722011-07-23 10:55:15 +00001065 if (isa<CompileJobAction>(A))
Joerg Sonnenberger359cf922011-05-06 14:35:16 +00001066 return true;
1067
1068 for (Action::const_iterator it = A->begin(), ie = A->end(); it != ie; ++it)
1069 if (ContainsCompileAction(*it))
1070 return true;
1071
1072 return false;
1073}
1074
1075/// \brief Check if -relax-all should be passed to the internal assembler.
1076/// This is done by default when compiling non-assembler source with -O0.
1077static bool UseRelaxAll(Compilation &C, const ArgList &Args) {
1078 bool RelaxDefault = true;
1079
1080 if (Arg *A = Args.getLastArg(options::OPT_O_Group))
1081 RelaxDefault = A->getOption().matches(options::OPT_O0);
1082
1083 if (RelaxDefault) {
1084 RelaxDefault = false;
1085 for (ActionList::const_iterator it = C.getActions().begin(),
1086 ie = C.getActions().end(); it != ie; ++it) {
1087 if (ContainsCompileAction(*it)) {
1088 RelaxDefault = true;
1089 break;
1090 }
1091 }
1092 }
1093
1094 return Args.hasFlag(options::OPT_mrelax_all, options::OPT_mno_relax_all,
1095 RelaxDefault);
1096}
1097
Daniel Dunbar47ac7d22009-03-18 06:00:36 +00001098void Clang::ConstructJob(Compilation &C, const JobAction &JA,
Daniel Dunbar871adcf2009-03-18 07:06:02 +00001099 const InputInfo &Output,
Daniel Dunbar62cf6012009-03-18 06:07:59 +00001100 const InputInfoList &Inputs,
Daniel Dunbar1d460332009-03-18 10:01:51 +00001101 const ArgList &Args,
Daniel Dunbar47ac7d22009-03-18 06:00:36 +00001102 const char *LinkingOutput) const {
Daniel Dunbar0a80ba72010-03-20 04:52:14 +00001103 bool KernelOrKext = Args.hasArg(options::OPT_mkernel,
1104 options::OPT_fapple_kext);
Daniel Dunbaree788e72009-12-21 18:54:17 +00001105 const Driver &D = getToolChain().getDriver();
Daniel Dunbarb488c1d2009-03-18 08:07:30 +00001106 ArgStringList CmdArgs;
1107
Daniel Dunbar077ba6a2009-03-31 20:53:55 +00001108 assert(Inputs.size() == 1 && "Unable to handle multiple inputs.");
1109
Daniel Dunbar8ff5b282009-12-11 23:00:49 +00001110 // Invoke ourselves in -cc1 mode.
1111 //
1112 // FIXME: Implement custom jobs for internal actions.
1113 CmdArgs.push_back("-cc1");
1114
Daniel Dunbardd4fe002009-10-30 18:12:20 +00001115 // Add the "effective" target triple.
Daniel Dunbaraf07f932009-03-31 17:35:15 +00001116 CmdArgs.push_back("-triple");
Daniel Dunbar00577ad2010-08-23 22:35:37 +00001117 std::string TripleStr = getToolChain().ComputeEffectiveClangTriple(Args);
Daniel Dunbardd4fe002009-10-30 18:12:20 +00001118 CmdArgs.push_back(Args.MakeArgString(TripleStr));
Daniel Dunbar728a5122009-09-10 06:49:20 +00001119
Daniel Dunbardd4fe002009-10-30 18:12:20 +00001120 // Select the appropriate action.
Daniel Dunbar5314e442010-07-19 19:44:22 +00001121 bool IsRewriter = false;
Daniel Dunbar1d460332009-03-18 10:01:51 +00001122 if (isa<AnalyzeJobAction>(JA)) {
1123 assert(JA.getType() == types::TY_Plist && "Invalid output type.");
1124 CmdArgs.push_back("-analyze");
1125 } else if (isa<PreprocessJobAction>(JA)) {
Daniel Dunbarcd8e4c42009-03-30 06:36:42 +00001126 if (Output.getType() == types::TY_Dependencies)
1127 CmdArgs.push_back("-Eonly");
1128 else
1129 CmdArgs.push_back("-E");
Daniel Dunbar8767cbc2010-02-03 03:07:56 +00001130 } else if (isa<AssembleJobAction>(JA)) {
1131 CmdArgs.push_back("-emit-obj");
Daniel Dunbar99298002010-05-27 06:18:05 +00001132
Joerg Sonnenberger359cf922011-05-06 14:35:16 +00001133 if (UseRelaxAll(C, Args))
Daniel Dunbar99298002010-05-27 06:18:05 +00001134 CmdArgs.push_back("-mrelax-all");
Daniel Dunbarca0e0542010-08-24 16:47:49 +00001135
Daniel Dunbarfcec10b2010-10-18 22:36:15 +00001136 // When using an integrated assembler, translate -Wa, and -Xassembler
1137 // options.
1138 for (arg_iterator it = Args.filtered_begin(options::OPT_Wa_COMMA,
1139 options::OPT_Xassembler),
1140 ie = Args.filtered_end(); it != ie; ++it) {
1141 const Arg *A = *it;
1142 A->claim();
1143
1144 for (unsigned i = 0, e = A->getNumValues(); i != e; ++i) {
Chris Lattner5f9e2722011-07-23 10:55:15 +00001145 StringRef Value = A->getValue(Args, i);
Daniel Dunbarfcec10b2010-10-18 22:36:15 +00001146
1147 if (Value == "-force_cpusubtype_ALL") {
1148 // Do nothing, this is the default and we don't support anything else.
Daniel Dunbarb14eed02010-10-28 20:36:23 +00001149 } else if (Value == "-L") {
Daniel Dunbar96932322011-03-28 22:49:28 +00001150 CmdArgs.push_back("-msave-temp-labels");
Joerg Sonnenberger46a49392011-05-19 20:46:39 +00001151 } else if (Value == "--fatal-warnings") {
Joerg Sonnenbergerd7933502011-05-19 18:42:29 +00001152 CmdArgs.push_back("-mllvm");
1153 CmdArgs.push_back("-fatal-assembler-warnings");
Nick Lewyckyc3b90142011-06-21 00:14:18 +00001154 } else if (Value == "--noexecstack") {
1155 CmdArgs.push_back("-mnoexecstack");
Daniel Dunbarfcec10b2010-10-18 22:36:15 +00001156 } else {
Chris Lattner5f9e2722011-07-23 10:55:15 +00001157 D.Diag(diag::err_drv_unsupported_option_argument)
Daniel Dunbarfcec10b2010-10-18 22:36:15 +00001158 << A->getOption().getName() << Value;
1159 }
1160 }
1161 }
Daniel Dunbard02bba82010-11-19 16:23:35 +00001162
1163 // Also ignore explicit -force_cpusubtype_ALL option.
1164 (void) Args.hasArg(options::OPT_force__cpusubtype__ALL);
Daniel Dunbar1d460332009-03-18 10:01:51 +00001165 } else if (isa<PrecompileJobAction>(JA)) {
Argyrios Kyrtzidise5c35372010-08-11 23:27:58 +00001166 // Use PCH if the user requested it.
Daniel Dunbar0ebd9322009-10-15 20:02:44 +00001167 bool UsePCH = D.CCCUsePCH;
Daniel Dunbar0ebd9322009-10-15 20:02:44 +00001168
1169 if (UsePCH)
Douglas Gregordf91ef32009-04-18 00:34:01 +00001170 CmdArgs.push_back("-emit-pch");
1171 else
1172 CmdArgs.push_back("-emit-pth");
Daniel Dunbar1d460332009-03-18 10:01:51 +00001173 } else {
1174 assert(isa<CompileJobAction>(JA) && "Invalid action for clang tool.");
Daniel Dunbarc21c4852009-04-08 23:54:23 +00001175
Daniel Dunbar1d460332009-03-18 10:01:51 +00001176 if (JA.getType() == types::TY_Nothing) {
1177 CmdArgs.push_back("-fsyntax-only");
Daniel Dunbar6c6424b2010-06-07 23:28:45 +00001178 } else if (JA.getType() == types::TY_LLVM_IR ||
1179 JA.getType() == types::TY_LTO_IR) {
Daniel Dunbar1d460332009-03-18 10:01:51 +00001180 CmdArgs.push_back("-emit-llvm");
Daniel Dunbar6c6424b2010-06-07 23:28:45 +00001181 } else if (JA.getType() == types::TY_LLVM_BC ||
1182 JA.getType() == types::TY_LTO_BC) {
Daniel Dunbar1d460332009-03-18 10:01:51 +00001183 CmdArgs.push_back("-emit-llvm-bc");
1184 } else if (JA.getType() == types::TY_PP_Asm) {
Daniel Dunbare3b8d072009-09-17 00:47:53 +00001185 CmdArgs.push_back("-S");
Daniel Dunbar5915fbf2009-09-01 16:57:46 +00001186 } else if (JA.getType() == types::TY_AST) {
1187 CmdArgs.push_back("-emit-pch");
Daniel Dunbar64952502010-02-11 03:16:21 +00001188 } else if (JA.getType() == types::TY_RewrittenObjC) {
1189 CmdArgs.push_back("-rewrite-objc");
Daniel Dunbar5314e442010-07-19 19:44:22 +00001190 IsRewriter = true;
Daniel Dunbar64952502010-02-11 03:16:21 +00001191 } else {
1192 assert(JA.getType() == types::TY_PP_Asm &&
1193 "Unexpected output type!");
Daniel Dunbar1d460332009-03-18 10:01:51 +00001194 }
Daniel Dunbarb488c1d2009-03-18 08:07:30 +00001195 }
1196
Daniel Dunbar1d460332009-03-18 10:01:51 +00001197 // The make clang go fast button.
1198 CmdArgs.push_back("-disable-free");
1199
John McCallb689afb2010-02-13 03:50:24 +00001200 // Disable the verification pass in -asserts builds.
1201#ifdef NDEBUG
1202 CmdArgs.push_back("-disable-llvm-verifier");
1203#endif
1204
Daniel Dunbarc9abc042009-04-08 05:11:16 +00001205 // Set the main file name, so that debug info works even with
1206 // -save-temps.
1207 CmdArgs.push_back("-main-file-name");
1208 CmdArgs.push_back(darwin::CC1::getBaseInputName(Args, Inputs));
1209
Daniel Dunbar3bbc7532009-04-08 18:03:55 +00001210 // Some flags which affect the language (via preprocessor
1211 // defines). See darwin::CC1::AddCPPArgs.
1212 if (Args.hasArg(options::OPT_static))
1213 CmdArgs.push_back("-static-define");
1214
Daniel Dunbar1d460332009-03-18 10:01:51 +00001215 if (isa<AnalyzeJobAction>(JA)) {
Ted Kremenekb8bb3e72009-09-25 05:55:59 +00001216 // Enable region store model by default.
1217 CmdArgs.push_back("-analyzer-store=region");
1218
Ted Kremenekb40d06d2009-12-07 22:26:14 +00001219 // Treat blocks as analysis entry points.
1220 CmdArgs.push_back("-analyzer-opt-analyze-nested-blocks");
1221
Ted Kremenek51885072011-03-24 00:28:47 +00001222 CmdArgs.push_back("-analyzer-eagerly-assume");
1223
Daniel Dunbar1d460332009-03-18 10:01:51 +00001224 // Add default argument set.
Daniel Dunbard8fc0f22009-05-22 00:38:15 +00001225 if (!Args.hasArg(options::OPT__analyzer_no_default_checks)) {
Argyrios Kyrtzidis027a6ab2011-02-15 07:42:33 +00001226 CmdArgs.push_back("-analyzer-checker=core");
Ted Kremenek51885072011-03-24 00:28:47 +00001227 CmdArgs.push_back("-analyzer-checker=deadcode");
1228 CmdArgs.push_back("-analyzer-checker=security");
1229
Argyrios Kyrtzidis027a6ab2011-02-15 07:42:33 +00001230 if (getToolChain().getTriple().getOS() != llvm::Triple::Win32)
1231 CmdArgs.push_back("-analyzer-checker=unix");
Ted Kremenek51885072011-03-24 00:28:47 +00001232
Argyrios Kyrtzidis027a6ab2011-02-15 07:42:33 +00001233 if (getToolChain().getTriple().getVendor() == llvm::Triple::Apple)
Ted Kremenek51885072011-03-24 00:28:47 +00001234 CmdArgs.push_back("-analyzer-checker=osx");
Daniel Dunbard8fc0f22009-05-22 00:38:15 +00001235 }
Daniel Dunbarc21c4852009-04-08 23:54:23 +00001236
Daniel Dunbard8fc0f22009-05-22 00:38:15 +00001237 // Set the output format. The default is plist, for (lame) historical
1238 // reasons.
1239 CmdArgs.push_back("-analyzer-output");
1240 if (Arg *A = Args.getLastArg(options::OPT__analyzer_output))
1241 CmdArgs.push_back(A->getValue(Args));
1242 else
1243 CmdArgs.push_back("plist");
Daniel Dunbar1d460332009-03-18 10:01:51 +00001244
Ted Kremenek0647a7b2010-03-22 22:32:05 +00001245 // Disable the presentation of standard compiler warnings when
1246 // using --analyze. We only want to show static analyzer diagnostics
1247 // or frontend errors.
1248 CmdArgs.push_back("-w");
1249
Daniel Dunbar1d460332009-03-18 10:01:51 +00001250 // Add -Xanalyzer arguments when running as analyzer.
1251 Args.AddAllArgValues(CmdArgs, options::OPT_Xanalyzer);
Mike Stump1eb44332009-09-09 15:08:12 +00001252 }
1253
Daniel Dunbare2fd6642009-09-10 01:21:12 +00001254 CheckCodeGenerationOptions(D, Args);
1255
Daniel Dunbarbc85be82009-04-29 18:32:25 +00001256 // Perform argument translation for LLVM backend. This
1257 // takes some care in reconciling with llvm-gcc. The
1258 // issue is that llvm-gcc translates these options based on
1259 // the values in cc1, whereas we are processing based on
1260 // the driver arguments.
Daniel Dunbarc21c4852009-04-08 23:54:23 +00001261
Daniel Dunbarbc85be82009-04-29 18:32:25 +00001262 // This comes from the default translation the driver + cc1
1263 // would do to enable flag_pic.
1264 //
1265 // FIXME: Centralize this code.
1266 bool PICEnabled = (Args.hasArg(options::OPT_fPIC) ||
1267 Args.hasArg(options::OPT_fpic) ||
1268 Args.hasArg(options::OPT_fPIE) ||
1269 Args.hasArg(options::OPT_fpie));
1270 bool PICDisabled = (Args.hasArg(options::OPT_mkernel) ||
1271 Args.hasArg(options::OPT_static));
1272 const char *Model = getToolChain().GetForcedPicModel();
1273 if (!Model) {
1274 if (Args.hasArg(options::OPT_mdynamic_no_pic))
1275 Model = "dynamic-no-pic";
1276 else if (PICDisabled)
1277 Model = "static";
1278 else if (PICEnabled)
1279 Model = "pic";
Daniel Dunbar1d460332009-03-18 10:01:51 +00001280 else
Daniel Dunbarbc85be82009-04-29 18:32:25 +00001281 Model = getToolChain().GetDefaultRelocationModel();
Daniel Dunbar1d460332009-03-18 10:01:51 +00001282 }
Chris Lattner5f9e2722011-07-23 10:55:15 +00001283 if (StringRef(Model) != "pic") {
Daniel Dunbarf219e7c2009-11-29 07:18:39 +00001284 CmdArgs.push_back("-mrelocation-model");
1285 CmdArgs.push_back(Model);
1286 }
Daniel Dunbarbc85be82009-04-29 18:32:25 +00001287
1288 // Infer the __PIC__ value.
1289 //
1290 // FIXME: This isn't quite right on Darwin, which always sets
1291 // __PIC__=2.
1292 if (strcmp(Model, "pic") == 0 || strcmp(Model, "dynamic-no-pic") == 0) {
Daniel Dunbar76743522009-11-29 02:39:08 +00001293 CmdArgs.push_back("-pic-level");
1294 CmdArgs.push_back(Args.hasArg(options::OPT_fPIC) ? "2" : "1");
Daniel Dunbarbc85be82009-04-29 18:32:25 +00001295 }
Tanya Lattner59876c22009-11-04 01:18:09 +00001296 if (!Args.hasFlag(options::OPT_fmerge_all_constants,
1297 options::OPT_fno_merge_all_constants))
Chris Lattnerf44a1a02011-04-08 18:06:54 +00001298 CmdArgs.push_back("-fno-merge-all-constants");
Daniel Dunbar6bea73b2009-09-16 06:17:29 +00001299
Daniel Dunbarf219e7c2009-11-29 07:18:39 +00001300 // LLVM Code Generator Options.
1301
Daniel Dunbar17d3fea2011-02-09 17:54:19 +00001302 if (Arg *A = Args.getLastArg(options::OPT_mregparm_EQ)) {
1303 CmdArgs.push_back("-mregparm");
1304 CmdArgs.push_back(A->getValue(Args));
1305 }
1306
Roman Divackycfe9af22011-03-01 17:40:53 +00001307 if (Args.hasFlag(options::OPT_mrtd, options::OPT_mno_rtd, false))
1308 CmdArgs.push_back("-mrtd");
1309
Daniel Dunbarf219e7c2009-11-29 07:18:39 +00001310 // FIXME: Set --enable-unsafe-fp-math.
1311 if (Args.hasFlag(options::OPT_fno_omit_frame_pointer,
1312 options::OPT_fomit_frame_pointer))
1313 CmdArgs.push_back("-mdisable-fp-elim");
1314 if (!Args.hasFlag(options::OPT_fzero_initialized_in_bss,
1315 options::OPT_fno_zero_initialized_in_bss))
1316 CmdArgs.push_back("-mno-zero-initialized-in-bss");
Daniel Dunbar398c6102011-02-04 02:20:39 +00001317 if (!Args.hasFlag(options::OPT_fstrict_aliasing,
1318 options::OPT_fno_strict_aliasing,
1319 getToolChain().IsStrictAliasingDefault()))
Dan Gohman4d5625e2010-10-14 22:36:56 +00001320 CmdArgs.push_back("-relaxed-aliasing");
Daniel Dunbar1b718482010-05-14 22:00:22 +00001321
1322 // Decide whether to use verbose asm. Verbose assembly is the default on
1323 // toolchains which have the integrated assembler on by default.
1324 bool IsVerboseAsmDefault = getToolChain().IsIntegratedAssemblerDefault();
1325 if (Args.hasFlag(options::OPT_fverbose_asm, options::OPT_fno_verbose_asm,
Michael J. Spencer20249a12010-10-21 03:16:25 +00001326 IsVerboseAsmDefault) ||
Daniel Dunbar1b718482010-05-14 22:00:22 +00001327 Args.hasArg(options::OPT_dA))
Daniel Dunbarf219e7c2009-11-29 07:18:39 +00001328 CmdArgs.push_back("-masm-verbose");
Daniel Dunbar1b718482010-05-14 22:00:22 +00001329
Daniel Dunbarf219e7c2009-11-29 07:18:39 +00001330 if (Args.hasArg(options::OPT_fdebug_pass_structure)) {
1331 CmdArgs.push_back("-mdebug-pass");
1332 CmdArgs.push_back("Structure");
1333 }
1334 if (Args.hasArg(options::OPT_fdebug_pass_arguments)) {
1335 CmdArgs.push_back("-mdebug-pass");
1336 CmdArgs.push_back("Arguments");
1337 }
1338
John McCalld0c2ec42010-02-19 02:45:38 +00001339 // Enable -mconstructor-aliases except on darwin, where we have to
1340 // work around a linker bug; see <rdar://problem/7651567>.
Bob Wilson905c45f2011-10-14 05:03:44 +00001341 if (!getToolChain().getTriple().isOSDarwin())
John McCalld0c2ec42010-02-19 02:45:38 +00001342 CmdArgs.push_back("-mconstructor-aliases");
NAKAMURA Takumi125b4cb2011-02-17 08:50:50 +00001343
John McCall32096692011-03-18 02:56:14 +00001344 // Darwin's kernel doesn't support guard variables; just die if we
1345 // try to use them.
Bob Wilson905c45f2011-10-14 05:03:44 +00001346 if (KernelOrKext && getToolChain().getTriple().isOSDarwin())
John McCall32096692011-03-18 02:56:14 +00001347 CmdArgs.push_back("-fforbid-guard-variables");
1348
Douglas Gregor6f755502011-02-01 15:15:22 +00001349 if (Args.hasArg(options::OPT_mms_bitfields)) {
1350 CmdArgs.push_back("-mms-bitfields");
1351 }
John McCalld0c2ec42010-02-19 02:45:38 +00001352
Daniel Dunbar6bea73b2009-09-16 06:17:29 +00001353 // This is a coarse approximation of what llvm-gcc actually does, both
1354 // -fasynchronous-unwind-tables and -fnon-call-exceptions interact in more
1355 // complicated ways.
1356 bool AsynchronousUnwindTables =
1357 Args.hasFlag(options::OPT_fasynchronous_unwind_tables,
1358 options::OPT_fno_asynchronous_unwind_tables,
1359 getToolChain().IsUnwindTablesDefault() &&
Daniel Dunbar0a80ba72010-03-20 04:52:14 +00001360 !KernelOrKext);
Daniel Dunbar6bea73b2009-09-16 06:17:29 +00001361 if (Args.hasFlag(options::OPT_funwind_tables, options::OPT_fno_unwind_tables,
1362 AsynchronousUnwindTables))
Daniel Dunbarf219e7c2009-11-29 07:18:39 +00001363 CmdArgs.push_back("-munwind-tables");
1364
1365 if (Arg *A = Args.getLastArg(options::OPT_flimited_precision_EQ)) {
1366 CmdArgs.push_back("-mlimit-float-precision");
1367 CmdArgs.push_back(A->getValue(Args));
1368 }
Daniel Dunbarbc85be82009-04-29 18:32:25 +00001369
Daniel Dunbar868bd0a2009-05-06 03:16:41 +00001370 // FIXME: Handle -mtune=.
1371 (void) Args.hasArg(options::OPT_mtune_EQ);
Daniel Dunbarbc85be82009-04-29 18:32:25 +00001372
Benjamin Kramer8e9ef0d2009-08-05 14:30:52 +00001373 if (Arg *A = Args.getLastArg(options::OPT_mcmodel_EQ)) {
Daniel Dunbarf219e7c2009-11-29 07:18:39 +00001374 CmdArgs.push_back("-mcode-model");
Benjamin Kramer8e9ef0d2009-08-05 14:30:52 +00001375 CmdArgs.push_back(A->getValue(Args));
1376 }
1377
Daniel Dunbar6acda162009-09-09 22:33:08 +00001378 // Add target specific cpu and features flags.
1379 switch(getToolChain().getTriple().getArch()) {
1380 default:
1381 break;
Daniel Dunbar868bd0a2009-05-06 03:16:41 +00001382
Daniel Dunbarb163ef72009-09-10 04:57:17 +00001383 case llvm::Triple::arm:
1384 case llvm::Triple::thumb:
Daniel Dunbarfa41d692011-03-17 17:10:06 +00001385 AddARMTargetArgs(Args, CmdArgs, KernelOrKext);
Daniel Dunbarb163ef72009-09-10 04:57:17 +00001386 break;
1387
Eric Christophered734732010-03-02 02:41:08 +00001388 case llvm::Triple::mips:
1389 case llvm::Triple::mipsel:
Akira Hatanaka7ec02582011-09-21 02:13:07 +00001390 case llvm::Triple::mips64:
1391 case llvm::Triple::mips64el:
Eric Christophered734732010-03-02 02:41:08 +00001392 AddMIPSTargetArgs(Args, CmdArgs);
1393 break;
1394
Bruno Cardoso Lopes9284d212010-11-09 17:21:19 +00001395 case llvm::Triple::sparc:
1396 AddSparcTargetArgs(Args, CmdArgs);
1397 break;
1398
Daniel Dunbar6acda162009-09-09 22:33:08 +00001399 case llvm::Triple::x86:
1400 case llvm::Triple::x86_64:
1401 AddX86TargetArgs(Args, CmdArgs);
1402 break;
Daniel Dunbarbc85be82009-04-29 18:32:25 +00001403 }
1404
Daniel Dunbarc176bc62010-08-11 23:07:47 +00001405 // Pass the linker version in use.
1406 if (Arg *A = Args.getLastArg(options::OPT_mlinker_version_EQ)) {
1407 CmdArgs.push_back("-target-linker-version");
1408 CmdArgs.push_back(A->getValue(Args));
1409 }
1410
Nick Lewyckyb2d11cc2011-02-02 06:43:03 +00001411 // -mno-omit-leaf-frame-pointer is the default on Darwin.
Daniel Dunbar1ad66482010-07-01 01:31:45 +00001412 if (Args.hasFlag(options::OPT_momit_leaf_frame_pointer,
Nick Lewyckyb2d11cc2011-02-02 06:43:03 +00001413 options::OPT_mno_omit_leaf_frame_pointer,
Bob Wilson905c45f2011-10-14 05:03:44 +00001414 !getToolChain().getTriple().isOSDarwin()))
Daniel Dunbar1ad66482010-07-01 01:31:45 +00001415 CmdArgs.push_back("-momit-leaf-frame-pointer");
1416
Dan Gohmanc31176d2010-01-08 02:20:44 +00001417 // -fno-math-errno is default.
1418 if (Args.hasFlag(options::OPT_fmath_errno,
Daniel Dunbarbc85be82009-04-29 18:32:25 +00001419 options::OPT_fno_math_errno,
Dan Gohmanc31176d2010-01-08 02:20:44 +00001420 false))
1421 CmdArgs.push_back("-fmath-errno");
Daniel Dunbarbc85be82009-04-29 18:32:25 +00001422
Daniel Dunbarb30575c2010-05-12 18:19:58 +00001423 // Explicitly error on some things we know we don't support and can't just
1424 // ignore.
1425 types::ID InputType = Inputs[0].getType();
Daniel Dunbare94db472010-09-24 19:39:37 +00001426 if (!Args.hasArg(options::OPT_fallow_unsupported)) {
1427 Arg *Unsupported;
Daniel Dunbare94db472010-09-24 19:39:37 +00001428 if (types::isCXX(InputType) &&
Bob Wilson905c45f2011-10-14 05:03:44 +00001429 getToolChain().getTriple().isOSDarwin() &&
Daniel Dunbare94db472010-09-24 19:39:37 +00001430 getToolChain().getTriple().getArch() == llvm::Triple::x86) {
Bob Wilsona544aee2011-08-13 23:48:55 +00001431 if ((Unsupported = Args.getLastArg(options::OPT_fapple_kext)) ||
1432 (Unsupported = Args.getLastArg(options::OPT_mkernel)))
Chris Lattner5f9e2722011-07-23 10:55:15 +00001433 D.Diag(diag::err_drv_clang_unsupported_opt_cxx_darwin_i386)
Daniel Dunbare94db472010-09-24 19:39:37 +00001434 << Unsupported->getOption().getName();
1435 }
Daniel Dunbarb30575c2010-05-12 18:19:58 +00001436 }
1437
Daniel Dunbar1d460332009-03-18 10:01:51 +00001438 Args.AddAllArgs(CmdArgs, options::OPT_v);
Daniel Dunbarf7c16d92010-08-24 22:44:13 +00001439 Args.AddLastArg(CmdArgs, options::OPT_H);
Chad Rosier2b819102011-08-02 17:58:04 +00001440 if (D.CCPrintHeaders && !D.CCGenDiagnostics) {
Daniel Dunbar322c29f2011-02-02 21:11:35 +00001441 CmdArgs.push_back("-header-include-file");
1442 CmdArgs.push_back(D.CCPrintHeadersFilename ?
1443 D.CCPrintHeadersFilename : "-");
1444 }
Daniel Dunbar1d460332009-03-18 10:01:51 +00001445 Args.AddLastArg(CmdArgs, options::OPT_P);
Mike Stump1eb44332009-09-09 15:08:12 +00001446 Args.AddLastArg(CmdArgs, options::OPT_print_ivar_layout);
Daniel Dunbar1d460332009-03-18 10:01:51 +00001447
Chad Rosier2b819102011-08-02 17:58:04 +00001448 if (D.CCLogDiagnostics && !D.CCGenDiagnostics) {
Daniel Dunbarc8a22b02011-04-07 18:01:20 +00001449 CmdArgs.push_back("-diagnostic-log-file");
1450 CmdArgs.push_back(D.CCLogDiagnosticsFilename ?
1451 D.CCLogDiagnosticsFilename : "-");
1452 }
1453
Daniel Dunbar1d460332009-03-18 10:01:51 +00001454 // Special case debug options to only pass -g to clang. This is
1455 // wrong.
Rafael Espindola18f36d92010-03-07 04:46:18 +00001456 Args.ClaimAllArgs(options::OPT_g_Group);
Daniel Dunbar6b3d5a62010-05-12 18:19:55 +00001457 if (Arg *A = Args.getLastArg(options::OPT_g_Group))
Chad Rosier2875bda2011-11-04 19:28:44 +00001458 if (!A->getOption().matches(options::OPT_g0)) {
Chad Rosiercf6ba2e2011-11-07 19:52:29 +00001459 CmdArgs.push_back("-g");
Chad Rosier2875bda2011-11-04 19:28:44 +00001460 }
Daniel Dunbar1d460332009-03-18 10:01:51 +00001461
Rafael Espindola9cf933a2010-05-06 21:06:04 +00001462 Args.AddAllArgs(CmdArgs, options::OPT_ffunction_sections);
1463 Args.AddAllArgs(CmdArgs, options::OPT_fdata_sections);
1464
Chris Lattner7255a2d2010-06-22 00:03:40 +00001465 Args.AddAllArgs(CmdArgs, options::OPT_finstrument_functions);
1466
Nick Lewyckye8ba8d72011-04-21 23:44:07 +00001467 if (Args.hasArg(options::OPT_ftest_coverage) ||
1468 Args.hasArg(options::OPT_coverage))
1469 CmdArgs.push_back("-femit-coverage-notes");
1470 if (Args.hasArg(options::OPT_fprofile_arcs) ||
1471 Args.hasArg(options::OPT_coverage))
1472 CmdArgs.push_back("-femit-coverage-data");
1473
Nick Lewycky5ea4f442011-05-04 20:46:58 +00001474 if (C.getArgs().hasArg(options::OPT_c) ||
1475 C.getArgs().hasArg(options::OPT_S)) {
1476 if (Output.isFilename()) {
Nick Lewycky3dc05412011-05-05 00:08:20 +00001477 CmdArgs.push_back("-coverage-file");
1478 CmdArgs.push_back(Args.MakeArgString(Output.getFilename()));
Nick Lewycky5ea4f442011-05-04 20:46:58 +00001479 }
1480 }
1481
Daniel Dunbara268fc02011-10-11 18:20:10 +00001482 // Pass options for controlling the default header search paths.
1483 if (Args.hasArg(options::OPT_nostdinc)) {
1484 CmdArgs.push_back("-nostdsysteminc");
1485 CmdArgs.push_back("-nobuiltininc");
1486 } else {
Daniel Dunbar92d6d402011-10-11 18:20:16 +00001487 if (Args.hasArg(options::OPT_nostdlibinc))
1488 CmdArgs.push_back("-nostdsysteminc");
Daniel Dunbara268fc02011-10-11 18:20:10 +00001489 Args.AddLastArg(CmdArgs, options::OPT_nostdincxx);
1490 Args.AddLastArg(CmdArgs, options::OPT_nobuiltininc);
1491 }
Daniel Dunbar1d460332009-03-18 10:01:51 +00001492
Daniel Dunbar5f122322009-12-15 01:02:52 +00001493 // Pass the path to compiler resource files.
Daniel Dunbar5f122322009-12-15 01:02:52 +00001494 CmdArgs.push_back("-resource-dir");
Daniel Dunbar225c4172010-01-20 02:35:16 +00001495 CmdArgs.push_back(D.ResourceDir.c_str());
Daniel Dunbar2ac9fc22009-04-07 21:42:00 +00001496
Argyrios Kyrtzidis389db162010-11-03 22:45:23 +00001497 Args.AddLastArg(CmdArgs, options::OPT_working_directory);
1498
John McCall8f0e8d22011-06-15 23:25:17 +00001499 if (!Args.hasArg(options::OPT_fno_objc_arc)) {
Argyrios Kyrtzidis72ac1202011-07-07 04:00:39 +00001500 if (const Arg *A = Args.getLastArg(options::OPT_ccc_arcmt_check,
Argyrios Kyrtzidis69325d52011-07-09 20:00:58 +00001501 options::OPT_ccc_arcmt_modify,
1502 options::OPT_ccc_arcmt_migrate)) {
John McCall8f0e8d22011-06-15 23:25:17 +00001503 switch (A->getOption().getID()) {
1504 default:
1505 llvm_unreachable("missed a case");
Argyrios Kyrtzidis72ac1202011-07-07 04:00:39 +00001506 case options::OPT_ccc_arcmt_check:
John McCall8f0e8d22011-06-15 23:25:17 +00001507 CmdArgs.push_back("-arcmt-check");
1508 break;
Argyrios Kyrtzidis72ac1202011-07-07 04:00:39 +00001509 case options::OPT_ccc_arcmt_modify:
John McCall8f0e8d22011-06-15 23:25:17 +00001510 CmdArgs.push_back("-arcmt-modify");
1511 break;
Argyrios Kyrtzidis69325d52011-07-09 20:00:58 +00001512 case options::OPT_ccc_arcmt_migrate:
1513 CmdArgs.push_back("-arcmt-migrate");
1514 CmdArgs.push_back("-arcmt-migrate-directory");
1515 CmdArgs.push_back(A->getValue(Args));
Argyrios Kyrtzidis7ee20492011-07-19 17:20:03 +00001516
1517 Args.AddLastArg(CmdArgs, options::OPT_arcmt_migrate_report_output);
1518 Args.AddLastArg(CmdArgs, options::OPT_arcmt_migrate_emit_arc_errors);
Argyrios Kyrtzidis69325d52011-07-09 20:00:58 +00001519 break;
John McCall8f0e8d22011-06-15 23:25:17 +00001520 }
1521 }
1522 }
Eric Christopher88b7cf02011-08-19 00:30:14 +00001523
Daniel Dunbarc21c4852009-04-08 23:54:23 +00001524 // Add preprocessing options like -I, -D, etc. if we are using the
1525 // preprocessor.
1526 //
1527 // FIXME: Support -fpreprocessed
Daniel Dunbarc21c4852009-04-08 23:54:23 +00001528 if (types::getPreprocessedType(InputType) != types::TY_INVALID)
Peter Collingbourne54db68b2011-11-06 00:40:05 +00001529 AddPreprocessingOptions(C, D, Args, CmdArgs, Output, Inputs);
Daniel Dunbar1d460332009-03-18 10:01:51 +00001530
Rafael Espindola19d9d2e2011-07-21 23:40:37 +00001531 // Don't warn about "clang -c -DPIC -fPIC test.i" because libtool.m4 assumes
1532 // that "The compiler can only warn and ignore the option if not recognized".
1533 // When building with ccache, it will pass -D options to clang even on
1534 // preprocessed inputs and configure concludes that -fPIC is not supported.
1535 Args.ClaimAllArgs(options::OPT_D);
1536
Daniel Dunbar20f0eac2009-09-17 06:53:36 +00001537 // Manually translate -O to -O2 and -O4 to -O3; let clang reject
Daniel Dunbar337a6272009-03-24 20:17:30 +00001538 // others.
1539 if (Arg *A = Args.getLastArg(options::OPT_O_Group)) {
Daniel Dunbarb827a052009-11-19 03:26:40 +00001540 if (A->getOption().matches(options::OPT_O4))
Daniel Dunbar337a6272009-03-24 20:17:30 +00001541 CmdArgs.push_back("-O3");
Daniel Dunbar473916c2010-05-27 06:51:08 +00001542 else if (A->getOption().matches(options::OPT_O) &&
1543 A->getValue(Args)[0] == '\0')
Daniel Dunbar20f0eac2009-09-17 06:53:36 +00001544 CmdArgs.push_back("-O2");
Daniel Dunbar1d460332009-03-18 10:01:51 +00001545 else
Daniel Dunbar5697aa02009-03-18 23:39:35 +00001546 A->render(Args, CmdArgs);
Daniel Dunbar1d460332009-03-18 10:01:51 +00001547 }
1548
Daniel Dunbar6e8371e2009-10-29 02:24:45 +00001549 Args.AddAllArgs(CmdArgs, options::OPT_W_Group);
1550 Args.AddLastArg(CmdArgs, options::OPT_pedantic);
1551 Args.AddLastArg(CmdArgs, options::OPT_pedantic_errors);
Daniel Dunbar1d460332009-03-18 10:01:51 +00001552 Args.AddLastArg(CmdArgs, options::OPT_w);
Daniel Dunbard573d262009-04-07 22:13:21 +00001553
1554 // Handle -{std, ansi, trigraphs} -- take the last of -{std, ansi}
1555 // (-ansi is equivalent to -std=c89).
1556 //
1557 // If a std is supplied, only add -trigraphs if it follows the
1558 // option.
1559 if (Arg *Std = Args.getLastArg(options::OPT_std_EQ, options::OPT_ansi)) {
1560 if (Std->getOption().matches(options::OPT_ansi))
Nuno Lopes528365d2009-10-16 14:28:06 +00001561 if (types::isCXX(InputType))
Daniel Dunbar294691e2009-11-04 06:24:38 +00001562 CmdArgs.push_back("-std=c++98");
Nuno Lopes528365d2009-10-16 14:28:06 +00001563 else
Daniel Dunbar294691e2009-11-04 06:24:38 +00001564 CmdArgs.push_back("-std=c89");
Daniel Dunbard573d262009-04-07 22:13:21 +00001565 else
1566 Std->render(Args, CmdArgs);
1567
Daniel Dunbar0e100312010-06-14 21:23:08 +00001568 if (Arg *A = Args.getLastArg(options::OPT_std_EQ, options::OPT_ansi,
1569 options::OPT_trigraphs))
1570 if (A != Std)
Daniel Dunbard573d262009-04-07 22:13:21 +00001571 A->render(Args, CmdArgs);
Daniel Dunbara3ff2022009-04-26 01:10:38 +00001572 } else {
1573 // Honor -std-default.
Daniel Dunbar4a5290e2010-01-29 21:03:02 +00001574 //
1575 // FIXME: Clang doesn't correctly handle -std= when the input language
1576 // doesn't match. For the time being just ignore this for C++ inputs;
1577 // eventually we want to do all the standard defaulting here instead of
1578 // splitting it between the driver and clang -cc1.
1579 if (!types::isCXX(InputType))
1580 Args.AddAllArgsTranslated(CmdArgs, options::OPT_std_default_EQ,
1581 "-std=", /*Joined=*/true);
Daniel Dunbard573d262009-04-07 22:13:21 +00001582 Args.AddLastArg(CmdArgs, options::OPT_trigraphs);
Daniel Dunbara3ff2022009-04-26 01:10:38 +00001583 }
Daniel Dunbarc21c4852009-04-08 23:54:23 +00001584
Chandler Carruth50465d12011-04-23 06:30:43 +00001585 // Map the bizarre '-Wwrite-strings' flag to a more sensible
1586 // '-fconst-strings'; this better indicates its actual behavior.
1587 if (Args.hasFlag(options::OPT_Wwrite_strings, options::OPT_Wno_write_strings,
1588 false)) {
1589 // For perfect compatibility with GCC, we do this even in the presence of
1590 // '-w'. This flag names something other than a warning for GCC.
1591 CmdArgs.push_back("-fconst-strings");
1592 }
1593
Chandler Carruth1cfe3c32011-04-23 09:27:53 +00001594 // GCC provides a macro definition '__DEPRECATED' when -Wdeprecated is active
Chandler Carruthf8c247d2011-04-23 19:48:40 +00001595 // during C++ compilation, which it is by default. GCC keeps this define even
1596 // in the presence of '-w', match this behavior bug-for-bug.
1597 if (types::isCXX(InputType) &&
1598 Args.hasFlag(options::OPT_Wdeprecated, options::OPT_Wno_deprecated,
1599 true)) {
1600 CmdArgs.push_back("-fdeprecated-macro");
Chandler Carruth1cfe3c32011-04-23 09:27:53 +00001601 }
1602
Chandler Carruthc304ba32010-05-22 02:21:53 +00001603 // Translate GCC's misnamer '-fasm' arguments to '-fgnu-keywords'.
1604 if (Arg *Asm = Args.getLastArg(options::OPT_fasm, options::OPT_fno_asm)) {
1605 if (Asm->getOption().matches(options::OPT_fasm))
1606 CmdArgs.push_back("-fgnu-keywords");
1607 else
1608 CmdArgs.push_back("-fno-gnu-keywords");
1609 }
1610
Rafael Espindola61b1efe2011-05-02 17:43:32 +00001611 if (ShouldDisableCFI(Args, getToolChain()))
1612 CmdArgs.push_back("-fno-dwarf2-cfi-asm");
Rafael Espindolaf24a1512011-04-30 18:35:43 +00001613
Nick Lewyckyea523d72011-10-17 23:05:52 +00001614 if (ShouldDisableDwarfDirectory(Args, getToolChain()))
1615 CmdArgs.push_back("-fno-dwarf-directory-asm");
1616
Nick Lewycky7c4fd912011-10-21 02:32:14 +00001617 if (const char *pwd = ::getenv("PWD")) {
1618 // GCC also verifies that stat(pwd) and stat(".") have the same inode
1619 // number. Not doing those because stats are slow, but we could.
NAKAMURA Takumi813a4072011-10-22 10:25:25 +00001620 if (llvm::sys::path::is_absolute(pwd)) {
Nick Lewycky7c4fd912011-10-21 02:32:14 +00001621 std::string CompDir = pwd;
1622 CmdArgs.push_back("-fdebug-compilation-dir");
1623 CmdArgs.push_back(Args.MakeArgString(CompDir));
1624 }
1625 }
1626
Daniel Dunbar1d460332009-03-18 10:01:51 +00001627 if (Arg *A = Args.getLastArg(options::OPT_ftemplate_depth_)) {
1628 CmdArgs.push_back("-ftemplate-depth");
1629 CmdArgs.push_back(A->getValue(Args));
1630 }
1631
Argyrios Kyrtzidis1380a142010-11-18 00:20:36 +00001632 if (Arg *A = Args.getLastArg(options::OPT_Wlarge_by_value_copy_EQ,
1633 options::OPT_Wlarge_by_value_copy_def)) {
1634 CmdArgs.push_back("-Wlarge-by-value-copy");
Argyrios Kyrtzidis3532fdd2010-11-17 23:11:54 +00001635 if (A->getNumValues())
1636 CmdArgs.push_back(A->getValue(Args));
1637 else
Argyrios Kyrtzidis1380a142010-11-18 00:20:36 +00001638 CmdArgs.push_back("64"); // default value for -Wlarge-by-value-copy.
Argyrios Kyrtzidis3532fdd2010-11-17 23:11:54 +00001639 }
1640
Daniel Dunbare4bdae72009-11-19 04:00:53 +00001641 if (Args.hasArg(options::OPT__relocatable_pch))
Daniel Dunbar66861e02009-11-20 22:21:36 +00001642 CmdArgs.push_back("-relocatable-pch");
Mike Stump1eb44332009-09-09 15:08:12 +00001643
Daniel Dunbar294691e2009-11-04 06:24:38 +00001644 if (Arg *A = Args.getLastArg(options::OPT_fconstant_string_class_EQ)) {
1645 CmdArgs.push_back("-fconstant-string-class");
1646 CmdArgs.push_back(A->getValue(Args));
1647 }
David Chisnall8a5a9aa2009-08-31 16:41:57 +00001648
Chris Lattner124fca52010-01-09 21:54:33 +00001649 if (Arg *A = Args.getLastArg(options::OPT_ftabstop_EQ)) {
1650 CmdArgs.push_back("-ftabstop");
1651 CmdArgs.push_back(A->getValue(Args));
1652 }
1653
Chris Lattner0f0c9632010-04-07 20:49:23 +00001654 CmdArgs.push_back("-ferror-limit");
1655 if (Arg *A = Args.getLastArg(options::OPT_ferror_limit_EQ))
1656 CmdArgs.push_back(A->getValue(Args));
1657 else
1658 CmdArgs.push_back("19");
Douglas Gregor575cf372010-04-20 07:18:24 +00001659
Chandler Carruthc40f73c2010-05-06 04:55:18 +00001660 if (Arg *A = Args.getLastArg(options::OPT_fmacro_backtrace_limit_EQ)) {
1661 CmdArgs.push_back("-fmacro-backtrace-limit");
Douglas Gregor6c1cb992010-05-04 17:13:42 +00001662 CmdArgs.push_back(A->getValue(Args));
Chandler Carruthc40f73c2010-05-06 04:55:18 +00001663 }
1664
1665 if (Arg *A = Args.getLastArg(options::OPT_ftemplate_backtrace_limit_EQ)) {
1666 CmdArgs.push_back("-ftemplate-backtrace-limit");
Douglas Gregor575cf372010-04-20 07:18:24 +00001667 CmdArgs.push_back(A->getValue(Args));
Chandler Carruthc40f73c2010-05-06 04:55:18 +00001668 }
1669
Daniel Dunbar55efe142009-11-04 06:24:47 +00001670 // Pass -fmessage-length=.
Daniel Dunbara28690e2009-11-30 08:40:54 +00001671 CmdArgs.push_back("-fmessage-length");
Daniel Dunbar55efe142009-11-04 06:24:47 +00001672 if (Arg *A = Args.getLastArg(options::OPT_fmessage_length_EQ)) {
Daniel Dunbara28690e2009-11-30 08:40:54 +00001673 CmdArgs.push_back(A->getValue(Args));
Daniel Dunbar55efe142009-11-04 06:24:47 +00001674 } else {
1675 // If -fmessage-length=N was not specified, determine whether this is a
1676 // terminal and, if so, implicitly define -fmessage-length appropriately.
1677 unsigned N = llvm::sys::Process::StandardErrColumns();
Chris Lattner5f9e2722011-07-23 10:55:15 +00001678 CmdArgs.push_back(Args.MakeArgString(Twine(N)));
Daniel Dunbar55efe142009-11-04 06:24:47 +00001679 }
1680
Daniel Dunbarba8d8612009-12-03 18:42:11 +00001681 if (const Arg *A = Args.getLastArg(options::OPT_fvisibility_EQ)) {
1682 CmdArgs.push_back("-fvisibility");
1683 CmdArgs.push_back(A->getValue(Args));
1684 }
1685
Douglas Gregor7cf84d62010-06-15 17:05:35 +00001686 Args.AddLastArg(CmdArgs, options::OPT_fvisibility_inlines_hidden);
Michael J. Spencer20249a12010-10-21 03:16:25 +00001687
Daniel Dunbar0a80ba72010-03-20 04:52:14 +00001688 // -fhosted is default.
1689 if (KernelOrKext || Args.hasFlag(options::OPT_ffreestanding,
1690 options::OPT_fhosted,
1691 false))
1692 CmdArgs.push_back("-ffreestanding");
1693
Daniel Dunbarba8d8612009-12-03 18:42:11 +00001694 // Forward -f (flag) options which we can pass directly.
Mike Stump9c276ae2009-12-12 01:27:46 +00001695 Args.AddLastArg(CmdArgs, options::OPT_fcatch_undefined_behavior);
Daniel Dunbar3aaf0822009-04-07 21:51:40 +00001696 Args.AddLastArg(CmdArgs, options::OPT_femit_all_decls);
Daniel Dunbar3aaf0822009-04-07 21:51:40 +00001697 Args.AddLastArg(CmdArgs, options::OPT_fheinous_gnu_extensions);
Devang Patelc69e1cf2010-09-30 19:05:55 +00001698 Args.AddLastArg(CmdArgs, options::OPT_flimit_debug_info);
Devang Patel033be8b2011-11-04 20:05:58 +00001699 Args.AddLastArg(CmdArgs, options::OPT_fno_limit_debug_info);
Eric Christophere88c4512011-10-25 07:13:06 +00001700 Args.AddLastArg(CmdArgs, options::OPT_fno_operator_names);
Daniel Dunbarbbe8e3e2011-03-01 18:49:30 +00001701 if (getToolChain().SupportsProfiling())
1702 Args.AddLastArg(CmdArgs, options::OPT_pg);
Daniel Dunbar8c6fa842010-03-16 16:57:46 +00001703
1704 // -flax-vector-conversions is default.
1705 if (!Args.hasFlag(options::OPT_flax_vector_conversions,
1706 options::OPT_fno_lax_vector_conversions))
1707 CmdArgs.push_back("-fno-lax-vector-conversions");
1708
Fariborz Jahanianb466d012011-01-07 01:05:02 +00001709 if (Args.getLastArg(options::OPT_fapple_kext))
1710 CmdArgs.push_back("-fapple-kext");
1711
Fariborz Jahanian34e65772009-05-22 20:17:16 +00001712 Args.AddLastArg(CmdArgs, options::OPT_fobjc_sender_dependent_dispatch);
Chris Lattner182e0922009-04-21 05:34:31 +00001713 Args.AddLastArg(CmdArgs, options::OPT_fdiagnostics_print_source_range_info);
Douglas Gregor4786c152010-08-19 20:24:43 +00001714 Args.AddLastArg(CmdArgs, options::OPT_fdiagnostics_parseable_fixits);
Daniel Dunbar3aaf0822009-04-07 21:51:40 +00001715 Args.AddLastArg(CmdArgs, options::OPT_ftime_report);
1716 Args.AddLastArg(CmdArgs, options::OPT_ftrapv);
David Chisnall7f18e672010-09-17 18:29:54 +00001717
1718 if (Arg *A = Args.getLastArg(options::OPT_ftrapv_handler_EQ)) {
1719 CmdArgs.push_back("-ftrapv-handler");
1720 CmdArgs.push_back(A->getValue(Args));
1721 }
1722
Evan Cheng49af1f32011-04-08 21:37:45 +00001723 // Forward -ftrap_function= options to the backend.
1724 if (Arg *A = Args.getLastArg(options::OPT_ftrap_function_EQ)) {
Chris Lattner5f9e2722011-07-23 10:55:15 +00001725 StringRef FuncName = A->getValue(Args);
Evan Cheng49af1f32011-04-08 21:37:45 +00001726 CmdArgs.push_back("-backend-option");
1727 CmdArgs.push_back(Args.MakeArgString("-trap-func=" + FuncName));
1728 }
1729
Chandler Carruth5adb5a82011-03-27 00:04:55 +00001730 // -fno-strict-overflow implies -fwrapv if it isn't disabled, but
1731 // -fstrict-overflow won't turn off an explicitly enabled -fwrapv.
1732 if (Arg *A = Args.getLastArg(options::OPT_fwrapv,
1733 options::OPT_fno_wrapv)) {
1734 if (A->getOption().matches(options::OPT_fwrapv))
1735 CmdArgs.push_back("-fwrapv");
1736 } else if (Arg *A = Args.getLastArg(options::OPT_fstrict_overflow,
1737 options::OPT_fno_strict_overflow)) {
1738 if (A->getOption().matches(options::OPT_fno_strict_overflow))
1739 CmdArgs.push_back("-fwrapv");
1740 }
Daniel Dunbar3aaf0822009-04-07 21:51:40 +00001741 Args.AddLastArg(CmdArgs, options::OPT_fwritable_strings);
Eric Christopherf84d4092010-08-07 23:08:14 +00001742 Args.AddLastArg(CmdArgs, options::OPT_funroll_loops);
Daniel Dunbar1d460332009-03-18 10:01:51 +00001743
Daniel Dunbar5345c392009-09-03 04:54:28 +00001744 Args.AddLastArg(CmdArgs, options::OPT_pthread);
1745
Daniel Dunbar9e5cc6b2009-11-17 08:07:36 +00001746 // -stack-protector=0 is default.
1747 unsigned StackProtectorLevel = 0;
Bill Wendling45483f72009-06-28 07:36:13 +00001748 if (Arg *A = Args.getLastArg(options::OPT_fno_stack_protector,
1749 options::OPT_fstack_protector_all,
1750 options::OPT_fstack_protector)) {
Daniel Dunbar9e5cc6b2009-11-17 08:07:36 +00001751 if (A->getOption().matches(options::OPT_fstack_protector))
1752 StackProtectorLevel = 1;
1753 else if (A->getOption().matches(options::OPT_fstack_protector_all))
1754 StackProtectorLevel = 2;
Nico Weber2fef1112011-08-23 07:38:27 +00001755 } else {
1756 StackProtectorLevel =
1757 getToolChain().GetDefaultStackProtectorLevel(KernelOrKext);
1758 }
Daniel Dunbar9e5cc6b2009-11-17 08:07:36 +00001759 if (StackProtectorLevel) {
1760 CmdArgs.push_back("-stack-protector");
Chris Lattner5f9e2722011-07-23 10:55:15 +00001761 CmdArgs.push_back(Args.MakeArgString(Twine(StackProtectorLevel)));
Bill Wendling45483f72009-06-28 07:36:13 +00001762 }
1763
Eric Christopher1a584022011-05-02 21:18:22 +00001764 // Translate -mstackrealign
1765 if (Args.hasArg(options::OPT_mstackrealign)) {
1766 CmdArgs.push_back("-backend-option");
1767 CmdArgs.push_back("-force-align-stack");
1768 }
Eric Christopher88b7cf02011-08-19 00:30:14 +00001769
Daniel Dunbar48d1ef72009-04-07 21:16:11 +00001770 // Forward -f options with positive and negative forms; we translate
1771 // these by hand.
1772
Fariborz Jahanianb466d012011-01-07 01:05:02 +00001773 if (Args.hasArg(options::OPT_mkernel)) {
Daniel Dunbar2843c192011-02-04 17:24:47 +00001774 if (!Args.hasArg(options::OPT_fapple_kext) && types::isCXX(InputType))
Fariborz Jahanianb466d012011-01-07 01:05:02 +00001775 CmdArgs.push_back("-fapple-kext");
1776 if (!Args.hasArg(options::OPT_fbuiltin))
1777 CmdArgs.push_back("-fno-builtin");
1778 }
Daniel Dunbar9e5cc6b2009-11-17 08:07:36 +00001779 // -fbuiltin is default.
Fariborz Jahanianb466d012011-01-07 01:05:02 +00001780 else if (!Args.hasFlag(options::OPT_fbuiltin, options::OPT_fno_builtin))
Daniel Dunbar53e84842009-11-19 04:55:23 +00001781 CmdArgs.push_back("-fno-builtin");
Daniel Dunbar48d1ef72009-04-07 21:16:11 +00001782
Nuno Lopesfc284482009-12-16 16:59:22 +00001783 if (!Args.hasFlag(options::OPT_fassume_sane_operator_new,
1784 options::OPT_fno_assume_sane_operator_new))
1785 CmdArgs.push_back("-fno-assume-sane-operator-new");
1786
Daniel Dunbar9e5cc6b2009-11-17 08:07:36 +00001787 // -fblocks=0 is default.
1788 if (Args.hasFlag(options::OPT_fblocks, options::OPT_fno_blocks,
David Chisnalle6533ff2011-02-28 17:11:43 +00001789 getToolChain().IsBlocksDefault()) ||
1790 (Args.hasArg(options::OPT_fgnu_runtime) &&
1791 Args.hasArg(options::OPT_fobjc_nonfragile_abi) &&
1792 !Args.hasArg(options::OPT_fno_blocks))) {
Daniel Dunbar9e5cc6b2009-11-17 08:07:36 +00001793 CmdArgs.push_back("-fblocks");
John McCall13db5cf2011-09-09 20:41:01 +00001794
1795 if (!Args.hasArg(options::OPT_fgnu_runtime) &&
1796 !getToolChain().hasBlocksRuntime())
1797 CmdArgs.push_back("-fblocks-runtime-optional");
David Chisnall5e530af2009-11-17 19:33:30 +00001798 }
Daniel Dunbar48d1ef72009-04-07 21:16:11 +00001799
John McCall32579cf2010-04-09 19:12:06 +00001800 // -faccess-control is default.
John McCall7002f4c2010-04-09 19:03:51 +00001801 if (Args.hasFlag(options::OPT_fno_access_control,
1802 options::OPT_faccess_control,
John McCall32579cf2010-04-09 19:12:06 +00001803 false))
John McCall7002f4c2010-04-09 19:03:51 +00001804 CmdArgs.push_back("-fno-access-control");
John McCall3ddd6e02010-03-17 01:32:13 +00001805
Anders Carlssona4c24752010-11-21 00:09:52 +00001806 // -felide-constructors is the default.
1807 if (Args.hasFlag(options::OPT_fno_elide_constructors,
1808 options::OPT_felide_constructors,
1809 false))
1810 CmdArgs.push_back("-fno-elide-constructors");
1811
Daniel Dunbar0be42c42009-11-17 07:06:20 +00001812 // -frtti is default.
Daniel Dunbar0a80ba72010-03-20 04:52:14 +00001813 if (KernelOrKext ||
1814 !Args.hasFlag(options::OPT_frtti, options::OPT_fno_rtti))
Daniel Dunbar53e84842009-11-19 04:55:23 +00001815 CmdArgs.push_back("-fno-rtti");
Mike Stump738f8c22009-07-31 23:15:31 +00001816
Argyrios Kyrtzidis9a2b9d72010-10-08 00:25:19 +00001817 // -fshort-enums=0 is default.
1818 // FIXME: Are there targers where -fshort-enums is on by default ?
1819 if (Args.hasFlag(options::OPT_fshort_enums,
1820 options::OPT_fno_short_enums, false))
1821 CmdArgs.push_back("-fshort-enums");
1822
Daniel Dunbar1f95e652009-11-17 06:37:03 +00001823 // -fsigned-char is default.
Daniel Dunbar6d2eb4d2009-11-25 10:14:30 +00001824 if (!Args.hasFlag(options::OPT_fsigned_char, options::OPT_funsigned_char,
Daniel Dunbar1f95e652009-11-17 06:37:03 +00001825 isSignedCharDefault(getToolChain().getTriple())))
Daniel Dunbar76743522009-11-29 02:39:08 +00001826 CmdArgs.push_back("-fno-signed-char");
Eli Friedman5a779732009-06-05 07:21:14 +00001827
Anders Carlssona508b7d2010-02-06 23:23:06 +00001828 // -fthreadsafe-static is default.
Michael J. Spencer20249a12010-10-21 03:16:25 +00001829 if (!Args.hasFlag(options::OPT_fthreadsafe_statics,
Anders Carlssona508b7d2010-02-06 23:23:06 +00001830 options::OPT_fno_threadsafe_statics))
1831 CmdArgs.push_back("-fno-threadsafe-statics");
1832
Daniel Dunbarefb0fa92010-03-20 04:15:41 +00001833 // -fuse-cxa-atexit is default.
Anton Korobeynikovd0a57d52010-09-11 11:17:06 +00001834 if (KernelOrKext ||
1835 !Args.hasFlag(options::OPT_fuse_cxa_atexit, options::OPT_fno_use_cxa_atexit,
NAKAMURA Takumicd7a30b2010-10-10 01:53:03 +00001836 getToolChain().getTriple().getOS() != llvm::Triple::Cygwin &&
NAKAMURA Takumi0aa20572011-02-17 08:51:38 +00001837 getToolChain().getTriple().getOS() != llvm::Triple::MinGW32))
Daniel Dunbarefb0fa92010-03-20 04:15:41 +00001838 CmdArgs.push_back("-fno-use-cxa-atexit");
1839
Daniel Dunbar0be42c42009-11-17 07:06:20 +00001840 // -fms-extensions=0 is default.
Daniel Dunbar6d2eb4d2009-11-25 10:14:30 +00001841 if (Args.hasFlag(options::OPT_fms_extensions, options::OPT_fno_ms_extensions,
Daniel Dunbar0be42c42009-11-17 07:06:20 +00001842 getToolChain().getTriple().getOS() == llvm::Triple::Win32))
1843 CmdArgs.push_back("-fms-extensions");
1844
Francois Pichetae556082011-09-17 04:32:15 +00001845 // -fms-compatibility=0 is default.
Douglas Gregorba97b6e2011-10-24 15:49:38 +00001846 if (Args.hasFlag(options::OPT_fms_compatibility,
1847 options::OPT_fno_ms_compatibility,
1848 (getToolChain().getTriple().getOS() == llvm::Triple::Win32 &&
1849 Args.hasFlag(options::OPT_fms_extensions,
1850 options::OPT_fno_ms_extensions,
1851 true))))
Francois Pichetae556082011-09-17 04:32:15 +00001852 CmdArgs.push_back("-fms-compatibility");
1853
Michael J. Spencerdae4ac42010-10-21 05:21:48 +00001854 // -fmsc-version=1300 is default.
1855 if (Args.hasFlag(options::OPT_fms_extensions, options::OPT_fno_ms_extensions,
1856 getToolChain().getTriple().getOS() == llvm::Triple::Win32) ||
1857 Args.hasArg(options::OPT_fmsc_version)) {
Chris Lattner5f9e2722011-07-23 10:55:15 +00001858 StringRef msc_ver = Args.getLastArgValue(options::OPT_fmsc_version);
Michael J. Spencerdae4ac42010-10-21 05:21:48 +00001859 if (msc_ver.empty())
1860 CmdArgs.push_back("-fmsc-version=1300");
1861 else
1862 CmdArgs.push_back(Args.MakeArgString("-fmsc-version=" + msc_ver));
1863 }
1864
1865
Dawn Perchik400b6072010-09-02 23:59:25 +00001866 // -fborland-extensions=0 is default.
1867 if (Args.hasFlag(options::OPT_fborland_extensions,
1868 options::OPT_fno_borland_extensions, false))
1869 CmdArgs.push_back("-fborland-extensions");
1870
Francois Pichet8efcc012011-09-01 16:38:08 +00001871 // -fno-delayed-template-parsing is default, except for Windows where MSVC STL
1872 // needs it.
Francois Pichet8387e2a2011-04-22 22:18:13 +00001873 if (Args.hasFlag(options::OPT_fdelayed_template_parsing,
1874 options::OPT_fno_delayed_template_parsing,
Francois Pichet8efcc012011-09-01 16:38:08 +00001875 getToolChain().getTriple().getOS() == llvm::Triple::Win32))
Francois Pichet805bc1f2011-08-26 00:22:34 +00001876 CmdArgs.push_back("-fdelayed-template-parsing");
Francois Pichet8387e2a2011-04-22 22:18:13 +00001877
Chandler Carrutheb5d7b72010-04-17 20:17:31 +00001878 // -fgnu-keywords default varies depending on language; only pass if
1879 // specified.
1880 if (Arg *A = Args.getLastArg(options::OPT_fgnu_keywords,
Daniel Dunbar40788d92010-04-24 17:56:39 +00001881 options::OPT_fno_gnu_keywords))
1882 A->render(Args, CmdArgs);
Chandler Carrutheb5d7b72010-04-17 20:17:31 +00001883
Rafael Espindola01ba8542011-06-02 17:30:53 +00001884 if (Args.hasFlag(options::OPT_fgnu89_inline,
1885 options::OPT_fno_gnu89_inline,
1886 false))
Rafael Espindolafb3f4aa2011-06-02 16:13:27 +00001887 CmdArgs.push_back("-fgnu89-inline");
1888
Daniel Dunbar9e5cc6b2009-11-17 08:07:36 +00001889 // -fobjc-nonfragile-abi=0 is default.
John McCall9f084a32011-07-06 00:26:06 +00001890 ObjCRuntime objCRuntime;
John McCalld71315c2011-06-22 00:53:57 +00001891 unsigned objcABIVersion = 0;
David Chisnall3c3ccd22011-09-30 13:32:35 +00001892 bool NeXTRuntimeIsDefault
1893 = (IsRewriter || getToolChain().getTriple().isOSDarwin());
1894 if (Args.hasFlag(options::OPT_fnext_runtime, options::OPT_fgnu_runtime,
1895 NeXTRuntimeIsDefault)) {
1896 objCRuntime.setKind(ObjCRuntime::NeXT);
1897 } else {
1898 CmdArgs.push_back("-fgnu-runtime");
1899 objCRuntime.setKind(ObjCRuntime::GNU);
1900 }
1901 getToolChain().configureObjCRuntime(objCRuntime);
1902 if (objCRuntime.HasARC)
1903 CmdArgs.push_back("-fobjc-runtime-has-arc");
1904 if (objCRuntime.HasWeak)
1905 CmdArgs.push_back("-fobjc-runtime-has-weak");
1906 if (objCRuntime.HasTerminate)
1907 CmdArgs.push_back("-fobjc-runtime-has-terminate");
John McCall9f084a32011-07-06 00:26:06 +00001908
David Chisnall3c3ccd22011-09-30 13:32:35 +00001909 // Compute the Objective-C ABI "version" to use. Version numbers are
1910 // slightly confusing for historical reasons:
1911 // 1 - Traditional "fragile" ABI
1912 // 2 - Non-fragile ABI, version 1
1913 // 3 - Non-fragile ABI, version 2
1914 objcABIVersion = 1;
1915 // If -fobjc-abi-version= is present, use that to set the version.
1916 if (Arg *A = Args.getLastArg(options::OPT_fobjc_abi_version_EQ)) {
1917 if (StringRef(A->getValue(Args)) == "1")
1918 objcABIVersion = 1;
1919 else if (StringRef(A->getValue(Args)) == "2")
1920 objcABIVersion = 2;
1921 else if (StringRef(A->getValue(Args)) == "3")
1922 objcABIVersion = 3;
1923 else
1924 D.Diag(diag::err_drv_clang_unsupported) << A->getAsString(Args);
1925 } else {
1926 // Otherwise, determine if we are using the non-fragile ABI.
Douglas Gregord596c4d2011-10-12 18:28:53 +00001927 bool NonFragileABIIsDefault
1928 = (!IsRewriter && getToolChain().IsObjCNonFragileABIDefault());
David Chisnall3c3ccd22011-09-30 13:32:35 +00001929 if (Args.hasFlag(options::OPT_fobjc_nonfragile_abi,
1930 options::OPT_fno_objc_nonfragile_abi,
Douglas Gregord596c4d2011-10-12 18:28:53 +00001931 NonFragileABIIsDefault)) {
David Chisnall3c3ccd22011-09-30 13:32:35 +00001932 // Determine the non-fragile ABI version to use.
Daniel Dunbarde17ba72010-11-11 16:08:59 +00001933#ifdef DISABLE_DEFAULT_NONFRAGILEABI_TWO
David Chisnall3c3ccd22011-09-30 13:32:35 +00001934 unsigned NonFragileABIVersion = 1;
Daniel Dunbarde17ba72010-11-11 16:08:59 +00001935#else
David Chisnall3c3ccd22011-09-30 13:32:35 +00001936 unsigned NonFragileABIVersion = 2;
Daniel Dunbarde17ba72010-11-11 16:08:59 +00001937#endif
Daniel Dunbardea63132010-09-20 18:19:55 +00001938
David Chisnall3c3ccd22011-09-30 13:32:35 +00001939 if (Arg *A = Args.getLastArg(
1940 options::OPT_fobjc_nonfragile_abi_version_EQ)) {
1941 if (StringRef(A->getValue(Args)) == "1")
1942 NonFragileABIVersion = 1;
1943 else if (StringRef(A->getValue(Args)) == "2")
1944 NonFragileABIVersion = 2;
Daniel Dunbarf643b9b2010-04-24 17:56:46 +00001945 else
David Chisnall3c3ccd22011-09-30 13:32:35 +00001946 D.Diag(diag::err_drv_clang_unsupported)
1947 << A->getAsString(Args);
Daniel Dunbarf643b9b2010-04-24 17:56:46 +00001948 }
NAKAMURA Takumi125b4cb2011-02-17 08:50:50 +00001949
David Chisnall3c3ccd22011-09-30 13:32:35 +00001950 objcABIVersion = 1 + NonFragileABIVersion;
1951 } else {
1952 objcABIVersion = 1;
Ted Kremenekc32647d2010-12-23 21:35:43 +00001953 }
Daniel Dunbar9e5cc6b2009-11-17 08:07:36 +00001954 }
1955
John McCalld1e40d52011-10-02 01:16:38 +00001956 if (objcABIVersion == 1) {
1957 CmdArgs.push_back("-fobjc-fragile-abi");
1958 } else {
David Chisnall3c3ccd22011-09-30 13:32:35 +00001959 // -fobjc-dispatch-method is only relevant with the nonfragile-abi, and
1960 // legacy is the default.
1961 if (!Args.hasFlag(options::OPT_fobjc_legacy_dispatch,
1962 options::OPT_fno_objc_legacy_dispatch,
1963 getToolChain().IsObjCLegacyDispatchDefault())) {
1964 if (getToolChain().UseObjCMixedDispatch())
1965 CmdArgs.push_back("-fobjc-dispatch-method=mixed");
1966 else
1967 CmdArgs.push_back("-fobjc-dispatch-method=non-legacy");
1968 }
1969 }
1970
1971 // -fobjc-default-synthesize-properties=0 is default.
1972 if (Args.hasFlag(options::OPT_fobjc_default_synthesize_properties,
1973 options::OPT_fno_objc_default_synthesize_properties,
1974 getToolChain().IsObjCDefaultSynthPropertiesDefault())) {
1975 CmdArgs.push_back("-fobjc-default-synthesize-properties");
1976 }
1977
John McCall9f084a32011-07-06 00:26:06 +00001978 // Allow -fno-objc-arr to trump -fobjc-arr/-fobjc-arc.
1979 // NOTE: This logic is duplicated in ToolChains.cpp.
1980 bool ARC = isObjCAutoRefCount(Args);
1981 if (ARC) {
1982 CmdArgs.push_back("-fobjc-arc");
1983
Chandler Carruth7ffa0322011-11-04 07:34:47 +00001984 // FIXME: It seems like this entire block, and several around it should be
1985 // wrapped in isObjC, but for now we just use it here as this is where it
1986 // was being used previously.
1987 if (types::isCXX(InputType) && types::isObjC(InputType)) {
1988 if (getToolChain().GetCXXStdlibType(Args) == ToolChain::CST_Libcxx)
1989 CmdArgs.push_back("-fobjc-arc-cxxlib=libc++");
1990 else
1991 CmdArgs.push_back("-fobjc-arc-cxxlib=libstdc++");
1992 }
1993
John McCall9f084a32011-07-06 00:26:06 +00001994 // Allow the user to enable full exceptions code emission.
1995 // We define off for Objective-CC, on for Objective-C++.
1996 if (Args.hasFlag(options::OPT_fobjc_arc_exceptions,
1997 options::OPT_fno_objc_arc_exceptions,
1998 /*default*/ types::isCXX(InputType)))
1999 CmdArgs.push_back("-fobjc-arc-exceptions");
2000 }
2001
2002 // -fobjc-infer-related-result-type is the default, except in the Objective-C
2003 // rewriter.
2004 if (IsRewriter)
2005 CmdArgs.push_back("-fno-objc-infer-related-result-type");
Eric Christopher88b7cf02011-08-19 00:30:14 +00002006
John McCall9f084a32011-07-06 00:26:06 +00002007 // Handle -fobjc-gc and -fobjc-gc-only. They are exclusive, and -fobjc-gc-only
2008 // takes precedence.
2009 const Arg *GCArg = Args.getLastArg(options::OPT_fobjc_gc_only);
2010 if (!GCArg)
2011 GCArg = Args.getLastArg(options::OPT_fobjc_gc);
2012 if (GCArg) {
2013 if (ARC) {
Chris Lattner5f9e2722011-07-23 10:55:15 +00002014 D.Diag(diag::err_drv_objc_gc_arr)
John McCall9f084a32011-07-06 00:26:06 +00002015 << GCArg->getAsString(Args);
2016 } else if (getToolChain().SupportsObjCGC()) {
2017 GCArg->render(Args, CmdArgs);
2018 } else {
2019 // FIXME: We should move this to a hard error.
Chris Lattner5f9e2722011-07-23 10:55:15 +00002020 D.Diag(diag::warn_drv_objc_gc_unsupported)
John McCall9f084a32011-07-06 00:26:06 +00002021 << GCArg->getAsString(Args);
2022 }
2023 }
2024
John McCalld71315c2011-06-22 00:53:57 +00002025 // Add exception args.
2026 addExceptionArgs(Args, InputType, getToolChain().getTriple(),
2027 KernelOrKext, IsRewriter, objcABIVersion, CmdArgs);
2028
2029 if (getToolChain().UseSjLjExceptions())
2030 CmdArgs.push_back("-fsjlj-exceptions");
2031
2032 // C++ "sane" operator new.
Daniel Dunbar984eb862010-02-01 21:07:25 +00002033 if (!Args.hasFlag(options::OPT_fassume_sane_operator_new,
2034 options::OPT_fno_assume_sane_operator_new))
2035 CmdArgs.push_back("-fno-assume-sane-operator-new");
2036
Daniel Dunbarf35f14d2010-04-27 15:34:57 +00002037 // -fconstant-cfstrings is default, and may be subject to argument translation
2038 // on Darwin.
2039 if (!Args.hasFlag(options::OPT_fconstant_cfstrings,
2040 options::OPT_fno_constant_cfstrings) ||
2041 !Args.hasFlag(options::OPT_mconstant_cfstrings,
2042 options::OPT_mno_constant_cfstrings))
2043 CmdArgs.push_back("-fno-constant-cfstrings");
2044
John Thompsona6fda122009-11-05 20:14:16 +00002045 // -fshort-wchar default varies depending on platform; only
2046 // pass if specified.
Daniel Dunbar1744a352010-04-27 15:35:03 +00002047 if (Arg *A = Args.getLastArg(options::OPT_fshort_wchar))
2048 A->render(Args, CmdArgs);
John Thompsona6fda122009-11-05 20:14:16 +00002049
Daniel Dunbaree848a72009-10-29 02:39:57 +00002050 // -fno-pascal-strings is default, only pass non-default. If the tool chain
2051 // happened to translate to -mpascal-strings, we want to back translate here.
Daniel Dunbar82d00682009-04-07 23:51:44 +00002052 //
2053 // FIXME: This is gross; that translation should be pulled from the
2054 // tool chain.
Daniel Dunbarc21c4852009-04-08 23:54:23 +00002055 if (Args.hasFlag(options::OPT_fpascal_strings,
Daniel Dunbar82d00682009-04-07 23:51:44 +00002056 options::OPT_fno_pascal_strings,
2057 false) ||
2058 Args.hasFlag(options::OPT_mpascal_strings,
2059 options::OPT_mno_pascal_strings,
2060 false))
Daniel Dunbar48d1ef72009-04-07 21:16:11 +00002061 CmdArgs.push_back("-fpascal-strings");
NAKAMURA Takumi125b4cb2011-02-17 08:50:50 +00002062
Daniel Dunbar88934e82011-10-05 21:04:55 +00002063 // Honor -fpack-struct= and -fpack-struct, if given. Note that
2064 // -fno-pack-struct doesn't apply to -fpack-struct=.
2065 if (Arg *A = Args.getLastArg(options::OPT_fpack_struct_EQ)) {
2066 CmdArgs.push_back("-fpack-struct");
2067 CmdArgs.push_back(A->getValue(Args));
2068 } else if (Args.hasFlag(options::OPT_fpack_struct,
2069 options::OPT_fno_pack_struct, false)) {
2070 CmdArgs.push_back("-fpack-struct");
2071 CmdArgs.push_back("1");
2072 }
2073
Fariborz Jahanianb466d012011-01-07 01:05:02 +00002074 if (Args.hasArg(options::OPT_mkernel) ||
2075 Args.hasArg(options::OPT_fapple_kext)) {
2076 if (!Args.hasArg(options::OPT_fcommon))
2077 CmdArgs.push_back("-fno-common");
2078 }
Daniel Dunbar88934e82011-10-05 21:04:55 +00002079
Daniel Dunbar48d1ef72009-04-07 21:16:11 +00002080 // -fcommon is default, only pass non-default.
Fariborz Jahanianb466d012011-01-07 01:05:02 +00002081 else if (!Args.hasFlag(options::OPT_fcommon, options::OPT_fno_common))
Daniel Dunbar48d1ef72009-04-07 21:16:11 +00002082 CmdArgs.push_back("-fno-common");
2083
Daniel Dunbar70d3c922009-04-15 02:37:43 +00002084 // -fsigned-bitfields is default, and clang doesn't yet support
Daniel Dunbar06205ca2010-10-15 22:30:42 +00002085 // -funsigned-bitfields.
Mike Stump1eb44332009-09-09 15:08:12 +00002086 if (!Args.hasFlag(options::OPT_fsigned_bitfields,
Daniel Dunbar70d3c922009-04-15 02:37:43 +00002087 options::OPT_funsigned_bitfields))
Chris Lattner5f9e2722011-07-23 10:55:15 +00002088 D.Diag(diag::warn_drv_clang_unsupported)
Daniel Dunbar70d3c922009-04-15 02:37:43 +00002089 << Args.getLastArg(options::OPT_funsigned_bitfields)->getAsString(Args);
2090
Daniel Dunbar06205ca2010-10-15 22:30:42 +00002091 // -fsigned-bitfields is default, and clang doesn't support -fno-for-scope.
2092 if (!Args.hasFlag(options::OPT_ffor_scope,
2093 options::OPT_fno_for_scope))
Chris Lattner5f9e2722011-07-23 10:55:15 +00002094 D.Diag(diag::err_drv_clang_unsupported)
Daniel Dunbar06205ca2010-10-15 22:30:42 +00002095 << Args.getLastArg(options::OPT_fno_for_scope)->getAsString(Args);
2096
Jeffrey Yasskin0ea22fd2010-06-08 04:56:20 +00002097 // -fcaret-diagnostics is default.
2098 if (!Args.hasFlag(options::OPT_fcaret_diagnostics,
2099 options::OPT_fno_caret_diagnostics, true))
2100 CmdArgs.push_back("-fno-caret-diagnostics");
2101
Daniel Dunbar49138fc2009-04-19 21:09:34 +00002102 // -fdiagnostics-fixit-info is default, only pass non-default.
Mike Stump1eb44332009-09-09 15:08:12 +00002103 if (!Args.hasFlag(options::OPT_fdiagnostics_fixit_info,
Daniel Dunbar49138fc2009-04-19 21:09:34 +00002104 options::OPT_fno_diagnostics_fixit_info))
2105 CmdArgs.push_back("-fno-diagnostics-fixit-info");
Eric Christopher88b7cf02011-08-19 00:30:14 +00002106
Douglas Gregor7d2b8c12011-04-15 22:04:17 +00002107 // Enable -fdiagnostics-show-name by default.
2108 if (Args.hasFlag(options::OPT_fdiagnostics_show_name,
2109 options::OPT_fno_diagnostics_show_name, false))
2110 CmdArgs.push_back("-fdiagnostics-show-name");
Daniel Dunbar49138fc2009-04-19 21:09:34 +00002111
Daniel Dunbar9e820ee2009-04-16 06:32:38 +00002112 // Enable -fdiagnostics-show-option by default.
Mike Stump1eb44332009-09-09 15:08:12 +00002113 if (Args.hasFlag(options::OPT_fdiagnostics_show_option,
Daniel Dunbar9e820ee2009-04-16 06:32:38 +00002114 options::OPT_fno_diagnostics_show_option))
2115 CmdArgs.push_back("-fdiagnostics-show-option");
Daniel Dunbar838be482009-11-04 06:24:57 +00002116
Chris Lattner6fbe8392010-05-04 21:55:25 +00002117 if (const Arg *A =
2118 Args.getLastArg(options::OPT_fdiagnostics_show_category_EQ)) {
2119 CmdArgs.push_back("-fdiagnostics-show-category");
2120 CmdArgs.push_back(A->getValue(Args));
2121 }
Daniel Dunbarca0e0542010-08-24 16:47:49 +00002122
Douglas Gregorc9471b02011-05-21 17:07:29 +00002123 if (const Arg *A =
2124 Args.getLastArg(options::OPT_fdiagnostics_format_EQ)) {
2125 CmdArgs.push_back("-fdiagnostics-format");
2126 CmdArgs.push_back(A->getValue(Args));
2127 }
2128
Chandler Carruthabaca7a2011-03-27 01:50:55 +00002129 if (Arg *A = Args.getLastArg(
2130 options::OPT_fdiagnostics_show_note_include_stack,
2131 options::OPT_fno_diagnostics_show_note_include_stack)) {
2132 if (A->getOption().matches(
2133 options::OPT_fdiagnostics_show_note_include_stack))
2134 CmdArgs.push_back("-fdiagnostics-show-note-include-stack");
2135 else
2136 CmdArgs.push_back("-fno-diagnostics-show-note-include-stack");
2137 }
2138
Daniel Dunbar838be482009-11-04 06:24:57 +00002139 // Color diagnostics are the default, unless the terminal doesn't support
2140 // them.
2141 if (Args.hasFlag(options::OPT_fcolor_diagnostics,
Argyrios Kyrtzidisf765d762010-09-23 12:56:06 +00002142 options::OPT_fno_color_diagnostics,
2143 llvm::sys::Process::StandardErrHasColors()))
Daniel Dunbar838be482009-11-04 06:24:57 +00002144 CmdArgs.push_back("-fcolor-diagnostics");
2145
Daniel Dunbar75eb1d62009-06-08 21:13:54 +00002146 if (!Args.hasFlag(options::OPT_fshow_source_location,
2147 options::OPT_fno_show_source_location))
2148 CmdArgs.push_back("-fno-show-source-location");
Daniel Dunbar9e820ee2009-04-16 06:32:38 +00002149
Douglas Gregorc9471b02011-05-21 17:07:29 +00002150 if (!Args.hasFlag(options::OPT_fshow_column,
2151 options::OPT_fno_show_column,
2152 true))
2153 CmdArgs.push_back("-fno-show-column");
2154
Douglas Gregora0068fc2010-07-09 17:35:33 +00002155 if (!Args.hasFlag(options::OPT_fspell_checking,
2156 options::OPT_fno_spell_checking))
2157 CmdArgs.push_back("-fno-spell-checking");
Daniel Dunbarca0e0542010-08-24 16:47:49 +00002158
Daniel Dunbar25b26eb2010-10-18 22:49:46 +00002159
Daniel Dunbar16894392010-11-02 19:42:04 +00002160 // Silently ignore -fasm-blocks for now.
2161 (void) Args.hasFlag(options::OPT_fasm_blocks, options::OPT_fno_asm_blocks,
2162 false);
Daniel Dunbar25b26eb2010-10-18 22:49:46 +00002163
Jeffrey Yasskin5edbdcc2010-06-11 05:57:47 +00002164 if (Arg *A = Args.getLastArg(options::OPT_fshow_overloads_EQ))
2165 A->render(Args, CmdArgs);
2166
Daniel Dunbar7695fba2009-04-19 21:20:32 +00002167 // -fdollars-in-identifiers default varies depending on platform and
2168 // language; only pass if specified.
Mike Stump1eb44332009-09-09 15:08:12 +00002169 if (Arg *A = Args.getLastArg(options::OPT_fdollars_in_identifiers,
Daniel Dunbar7695fba2009-04-19 21:20:32 +00002170 options::OPT_fno_dollars_in_identifiers)) {
2171 if (A->getOption().matches(options::OPT_fdollars_in_identifiers))
Daniel Dunbar8663b182009-12-16 20:10:18 +00002172 CmdArgs.push_back("-fdollars-in-identifiers");
Daniel Dunbar7695fba2009-04-19 21:20:32 +00002173 else
Daniel Dunbar8663b182009-12-16 20:10:18 +00002174 CmdArgs.push_back("-fno-dollars-in-identifiers");
Daniel Dunbar7695fba2009-04-19 21:20:32 +00002175 }
2176
Daniel Dunbare027a4b2009-05-22 19:02:20 +00002177 // -funit-at-a-time is default, and we don't support -fno-unit-at-a-time for
2178 // practical purposes.
Mike Stump1eb44332009-09-09 15:08:12 +00002179 if (Arg *A = Args.getLastArg(options::OPT_funit_at_a_time,
Daniel Dunbare027a4b2009-05-22 19:02:20 +00002180 options::OPT_fno_unit_at_a_time)) {
2181 if (A->getOption().matches(options::OPT_fno_unit_at_a_time))
Chris Lattner5f9e2722011-07-23 10:55:15 +00002182 D.Diag(diag::warn_drv_clang_unsupported) << A->getAsString(Args);
Daniel Dunbare027a4b2009-05-22 19:02:20 +00002183 }
Eli Friedmanceb5c5b2009-07-14 21:58:17 +00002184
Eli Friedman19bda3a2011-11-02 01:53:16 +00002185 if (Args.hasFlag(options::OPT_fapple_pragma_pack,
2186 options::OPT_fno_apple_pragma_pack, false))
2187 CmdArgs.push_back("-fapple-pragma-pack");
2188
Daniel Dunbar2ba91572009-09-10 03:37:02 +00002189 // Default to -fno-builtin-str{cat,cpy} on Darwin for ARM.
Daniel Dunbarf84a4a42009-09-10 04:57:27 +00002190 //
Daniel Dunbar8ff5b282009-12-11 23:00:49 +00002191 // FIXME: This is disabled until clang -cc1 supports -fno-builtin-foo. PR4941.
Daniel Dunbarf84a4a42009-09-10 04:57:27 +00002192#if 0
Bob Wilson905c45f2011-10-14 05:03:44 +00002193 if (getToolChain().getTriple().isOSDarwin() &&
Daniel Dunbar2ba91572009-09-10 03:37:02 +00002194 (getToolChain().getTriple().getArch() == llvm::Triple::arm ||
2195 getToolChain().getTriple().getArch() == llvm::Triple::thumb)) {
2196 if (!Args.hasArg(options::OPT_fbuiltin_strcat))
2197 CmdArgs.push_back("-fno-builtin-strcat");
2198 if (!Args.hasArg(options::OPT_fbuiltin_strcpy))
2199 CmdArgs.push_back("-fno-builtin-strcpy");
2200 }
Daniel Dunbarf84a4a42009-09-10 04:57:27 +00002201#endif
Daniel Dunbar2ba91572009-09-10 03:37:02 +00002202
Daniel Dunbard98750f2011-03-18 21:23:40 +00002203 // Only allow -traditional or -traditional-cpp outside in preprocessing modes.
Mike Stump1eb44332009-09-09 15:08:12 +00002204 if (Arg *A = Args.getLastArg(options::OPT_traditional,
Daniel Dunbard98750f2011-03-18 21:23:40 +00002205 options::OPT_traditional_cpp)) {
2206 if (isa<PreprocessJobAction>(JA))
2207 CmdArgs.push_back("-traditional-cpp");
Eric Christopher88b7cf02011-08-19 00:30:14 +00002208 else
Chris Lattner5f9e2722011-07-23 10:55:15 +00002209 D.Diag(diag::err_drv_clang_unsupported) << A->getAsString(Args);
Daniel Dunbard98750f2011-03-18 21:23:40 +00002210 }
Eli Friedmanceb5c5b2009-07-14 21:58:17 +00002211
Daniel Dunbar1d460332009-03-18 10:01:51 +00002212 Args.AddLastArg(CmdArgs, options::OPT_dM);
Chris Lattnerd82df3a2009-04-12 01:56:53 +00002213 Args.AddLastArg(CmdArgs, options::OPT_dD);
Ted Kremenek36f6e302011-11-11 00:07:43 +00002214
2215 // Handle serialized diagnostics.
2216 if (Arg *A = Args.getLastArg(options::OPT__serialize_diags)) {
2217 CmdArgs.push_back("-serialize-diagnostic-file");
2218 CmdArgs.push_back(Args.MakeArgString(A->getValue(Args)));
2219 }
Daniel Dunbar1d460332009-03-18 10:01:51 +00002220
Daniel Dunbar3f87fb02010-04-15 06:09:03 +00002221 // Forward -Xclang arguments to -cc1, and -mllvm arguments to the LLVM option
2222 // parser.
Daniel Dunbar1d460332009-03-18 10:01:51 +00002223 Args.AddAllArgValues(CmdArgs, options::OPT_Xclang);
Daniel Dunbar3f87fb02010-04-15 06:09:03 +00002224 for (arg_iterator it = Args.filtered_begin(options::OPT_mllvm),
2225 ie = Args.filtered_end(); it != ie; ++it) {
Daniel Dunbar7e4953e2010-06-11 22:00:13 +00002226 (*it)->claim();
Daniel Dunbarfb36d212010-04-17 06:10:00 +00002227
Daniel Dunbar3f87fb02010-04-15 06:09:03 +00002228 // We translate this by hand to the -cc1 argument, since nightly test uses
2229 // it and developers have been trained to spell it with -mllvm.
Chris Lattner5f9e2722011-07-23 10:55:15 +00002230 if (StringRef((*it)->getValue(Args, 0)) == "-disable-llvm-optzns")
Daniel Dunbar3f87fb02010-04-15 06:09:03 +00002231 CmdArgs.push_back("-disable-llvm-optzns");
2232 else
Daniel Dunbar7e4953e2010-06-11 22:00:13 +00002233 (*it)->render(Args, CmdArgs);
Daniel Dunbar3f87fb02010-04-15 06:09:03 +00002234 }
Daniel Dunbar1d460332009-03-18 10:01:51 +00002235
Daniel Dunbarcd8e4c42009-03-30 06:36:42 +00002236 if (Output.getType() == types::TY_Dependencies) {
2237 // Handled with other dependency code.
Daniel Dunbar115a7922009-03-19 07:29:38 +00002238 } else if (Output.isFilename()) {
Daniel Dunbarb488c1d2009-03-18 08:07:30 +00002239 CmdArgs.push_back("-o");
Daniel Dunbar115a7922009-03-19 07:29:38 +00002240 CmdArgs.push_back(Output.getFilename());
2241 } else {
2242 assert(Output.isNothing() && "Invalid output.");
Daniel Dunbarb488c1d2009-03-18 08:07:30 +00002243 }
2244
Daniel Dunbar1d460332009-03-18 10:01:51 +00002245 for (InputInfoList::const_iterator
2246 it = Inputs.begin(), ie = Inputs.end(); it != ie; ++it) {
2247 const InputInfo &II = *it;
2248 CmdArgs.push_back("-x");
2249 CmdArgs.push_back(types::getTypeName(II.getType()));
Daniel Dunbar7c1e4652010-08-02 02:38:21 +00002250 if (II.isFilename())
Daniel Dunbar115a7922009-03-19 07:29:38 +00002251 CmdArgs.push_back(II.getFilename());
Daniel Dunbar1d460332009-03-18 10:01:51 +00002252 else
Daniel Dunbar115a7922009-03-19 07:29:38 +00002253 II.getInputArg().renderAsInput(Args, CmdArgs);
Daniel Dunbar1d460332009-03-18 10:01:51 +00002254 }
Daniel Dunbarc21c4852009-04-08 23:54:23 +00002255
Chris Lattnere6113de2009-11-03 19:50:27 +00002256 Args.AddAllArgs(CmdArgs, options::OPT_undef);
2257
Daniel Dunbara001c1c2010-07-18 21:16:15 +00002258 const char *Exec = getToolChain().getDriver().getClangProgramPath();
Daniel Dunbarf2d8b9f2009-12-18 02:43:17 +00002259
2260 // Optionally embed the -cc1 level arguments into the debug info, for build
2261 // analysis.
2262 if (getToolChain().UseDwarfDebugFlags()) {
Daniel Dunbar6e900472010-06-04 18:47:06 +00002263 ArgStringList OriginalArgs;
2264 for (ArgList::const_iterator it = Args.begin(),
2265 ie = Args.end(); it != ie; ++it)
2266 (*it)->render(Args, OriginalArgs);
Daniel Dunbarca0e0542010-08-24 16:47:49 +00002267
Daniel Dunbarf2d8b9f2009-12-18 02:43:17 +00002268 llvm::SmallString<256> Flags;
2269 Flags += Exec;
Daniel Dunbar6e900472010-06-04 18:47:06 +00002270 for (unsigned i = 0, e = OriginalArgs.size(); i != e; ++i) {
Daniel Dunbarf2d8b9f2009-12-18 02:43:17 +00002271 Flags += " ";
Daniel Dunbar6e900472010-06-04 18:47:06 +00002272 Flags += OriginalArgs[i];
Daniel Dunbarf2d8b9f2009-12-18 02:43:17 +00002273 }
2274 CmdArgs.push_back("-dwarf-debug-flags");
2275 CmdArgs.push_back(Args.MakeArgString(Flags.str()));
2276 }
2277
Daniel Dunbar2fe238e2010-08-02 02:38:28 +00002278 C.addCommand(new Command(JA, *this, Exec, CmdArgs));
Daniel Dunbara880db02009-03-23 19:03:36 +00002279
Roman Divackybe4c8702011-02-10 16:52:03 +00002280 if (Arg *A = Args.getLastArg(options::OPT_pg))
2281 if (Args.hasArg(options::OPT_fomit_frame_pointer))
Chris Lattner5f9e2722011-07-23 10:55:15 +00002282 D.Diag(diag::err_drv_argument_not_allowed_with)
Roman Divackybe4c8702011-02-10 16:52:03 +00002283 << "-fomit-frame-pointer" << A->getAsString(Args);
Michael J. Spencer20249a12010-10-21 03:16:25 +00002284
Daniel Dunbar68fb4692009-04-03 20:51:31 +00002285 // Claim some arguments which clang supports automatically.
2286
Daniel Dunbarf4046862010-04-15 06:18:42 +00002287 // -fpch-preprocess is used with gcc to add a special marker in the output to
2288 // include the PCH file. Clang's PTH solution is completely transparent, so we
2289 // do not need to deal with it at all.
Daniel Dunbar68fb4692009-04-03 20:51:31 +00002290 Args.ClaimAllArgs(options::OPT_fpch_preprocess);
Daniel Dunbarc21c4852009-04-08 23:54:23 +00002291
Daniel Dunbara880db02009-03-23 19:03:36 +00002292 // Claim some arguments which clang doesn't support, but we don't
2293 // care to warn the user about.
Daniel Dunbarcdd96862009-11-25 11:53:23 +00002294 Args.ClaimAllArgs(options::OPT_clang_ignored_f_Group);
2295 Args.ClaimAllArgs(options::OPT_clang_ignored_m_Group);
Rafael Espindola035ff0c2011-02-28 23:29:45 +00002296
Rafael Espindola9c094fb2011-03-01 05:25:27 +00002297 // Disable warnings for clang -E -use-gold-plugin -emit-llvm foo.c
Rafael Espindola035ff0c2011-02-28 23:29:45 +00002298 Args.ClaimAllArgs(options::OPT_use_gold_plugin);
Rafael Espindola9c094fb2011-03-01 05:25:27 +00002299 Args.ClaimAllArgs(options::OPT_emit_llvm);
Daniel Dunbar47ac7d22009-03-18 06:00:36 +00002300}
2301
Daniel Dunbar20a9aa52010-05-20 21:30:13 +00002302void ClangAs::ConstructJob(Compilation &C, const JobAction &JA,
Daniel Dunbar20a9aa52010-05-20 21:30:13 +00002303 const InputInfo &Output,
2304 const InputInfoList &Inputs,
2305 const ArgList &Args,
2306 const char *LinkingOutput) const {
Daniel Dunbar20a9aa52010-05-20 21:30:13 +00002307 ArgStringList CmdArgs;
2308
2309 assert(Inputs.size() == 1 && "Unexpected number of inputs.");
2310 const InputInfo &Input = Inputs[0];
2311
Rafael Espindoladbe80d92010-11-17 22:13:25 +00002312 // Don't warn about "clang -w -c foo.s"
2313 Args.ClaimAllArgs(options::OPT_w);
Rafael Espindola9c094fb2011-03-01 05:25:27 +00002314 // and "clang -emit-llvm -c foo.s"
2315 Args.ClaimAllArgs(options::OPT_emit_llvm);
2316 // and "clang -use-gold-plugin -c foo.s"
2317 Args.ClaimAllArgs(options::OPT_use_gold_plugin);
Rafael Espindoladbe80d92010-11-17 22:13:25 +00002318
Daniel Dunbar20a9aa52010-05-20 21:30:13 +00002319 // Invoke ourselves in -cc1as mode.
2320 //
2321 // FIXME: Implement custom jobs for internal actions.
2322 CmdArgs.push_back("-cc1as");
2323
2324 // Add the "effective" target triple.
2325 CmdArgs.push_back("-triple");
Chad Rosier61ab80a2011-09-20 20:44:06 +00002326 std::string TripleStr =
2327 getToolChain().ComputeEffectiveClangTriple(Args, Input.getType());
Daniel Dunbar20a9aa52010-05-20 21:30:13 +00002328 CmdArgs.push_back(Args.MakeArgString(TripleStr));
2329
2330 // Set the output mode, we currently only expect to be used as a real
2331 // assembler.
2332 CmdArgs.push_back("-filetype");
2333 CmdArgs.push_back("obj");
2334
Joerg Sonnenberger359cf922011-05-06 14:35:16 +00002335 if (UseRelaxAll(C, Args))
Daniel Dunbar469d40e2010-05-28 16:43:21 +00002336 CmdArgs.push_back("-relax-all");
Daniel Dunbar99298002010-05-27 06:18:05 +00002337
Daniel Dunbar7f6f8c82011-03-17 17:37:29 +00002338 // Ignore explicit -force_cpusubtype_ALL option.
2339 (void) Args.hasArg(options::OPT_force__cpusubtype__ALL);
Daniel Dunbar20a9aa52010-05-20 21:30:13 +00002340
Chad Rosier43f7e622011-10-04 22:35:48 +00002341 // FIXME: Add -g support, once we have it.
Daniel Dunbar20a9aa52010-05-20 21:30:13 +00002342
2343 // FIXME: Add -static support, once we have it.
2344
2345 Args.AddAllArgValues(CmdArgs, options::OPT_Wa_COMMA,
2346 options::OPT_Xassembler);
Daniel Dunbar3df23252011-04-29 17:53:18 +00002347 Args.AddAllArgs(CmdArgs, options::OPT_mllvm);
Daniel Dunbar20a9aa52010-05-20 21:30:13 +00002348
2349 assert(Output.isFilename() && "Unexpected lipo output.");
2350 CmdArgs.push_back("-o");
2351 CmdArgs.push_back(Output.getFilename());
2352
Daniel Dunbar7c1e4652010-08-02 02:38:21 +00002353 assert(Input.isFilename() && "Invalid input.");
2354 CmdArgs.push_back(Input.getFilename());
Daniel Dunbar20a9aa52010-05-20 21:30:13 +00002355
Daniel Dunbara001c1c2010-07-18 21:16:15 +00002356 const char *Exec = getToolChain().getDriver().getClangProgramPath();
Daniel Dunbar2fe238e2010-08-02 02:38:28 +00002357 C.addCommand(new Command(JA, *this, Exec, CmdArgs));
Daniel Dunbar20a9aa52010-05-20 21:30:13 +00002358}
2359
Daniel Dunbarb488c1d2009-03-18 08:07:30 +00002360void gcc::Common::ConstructJob(Compilation &C, const JobAction &JA,
Daniel Dunbarb488c1d2009-03-18 08:07:30 +00002361 const InputInfo &Output,
2362 const InputInfoList &Inputs,
Daniel Dunbar1d460332009-03-18 10:01:51 +00002363 const ArgList &Args,
Daniel Dunbarb488c1d2009-03-18 08:07:30 +00002364 const char *LinkingOutput) const {
Daniel Dunbaree788e72009-12-21 18:54:17 +00002365 const Driver &D = getToolChain().getDriver();
Daniel Dunbarb488c1d2009-03-18 08:07:30 +00002366 ArgStringList CmdArgs;
Daniel Dunbar47ac7d22009-03-18 06:00:36 +00002367
Daniel Dunbarc21c4852009-04-08 23:54:23 +00002368 for (ArgList::const_iterator
Daniel Dunbar1d460332009-03-18 10:01:51 +00002369 it = Args.begin(), ie = Args.end(); it != ie; ++it) {
Daniel Dunbarb488c1d2009-03-18 08:07:30 +00002370 Arg *A = *it;
Daniel Dunbar75877192009-03-19 07:55:12 +00002371 if (A->getOption().hasForwardToGCC()) {
Daniel Dunbar2dffe2d2010-08-03 16:14:14 +00002372 // Don't forward any -g arguments to assembly steps.
2373 if (isa<AssembleJobAction>(JA) &&
2374 A->getOption().matches(options::OPT_g_Group))
2375 continue;
2376
Daniel Dunbar75877192009-03-19 07:55:12 +00002377 // It is unfortunate that we have to claim here, as this means
2378 // we will basically never report anything interesting for
Daniel Dunbar6ecc7a92009-05-02 21:41:52 +00002379 // platforms using a generic gcc, even if we are just using gcc
2380 // to get to the assembler.
Daniel Dunbar75877192009-03-19 07:55:12 +00002381 A->claim();
Daniel Dunbar1d460332009-03-18 10:01:51 +00002382 A->render(Args, CmdArgs);
Daniel Dunbar75877192009-03-19 07:55:12 +00002383 }
Daniel Dunbarb488c1d2009-03-18 08:07:30 +00002384 }
Daniel Dunbarc21c4852009-04-08 23:54:23 +00002385
Daniel Dunbar82b51cc2010-01-25 22:35:08 +00002386 RenderExtraToolArgs(JA, CmdArgs);
Daniel Dunbarb488c1d2009-03-18 08:07:30 +00002387
2388 // If using a driver driver, force the arch.
Daniel Dunbar7cfe31a2009-05-22 02:21:04 +00002389 const std::string &Arch = getToolChain().getArchName();
Bob Wilson905c45f2011-10-14 05:03:44 +00002390 if (getToolChain().getTriple().isOSDarwin()) {
Daniel Dunbarb488c1d2009-03-18 08:07:30 +00002391 CmdArgs.push_back("-arch");
Daniel Dunbarbf54a062009-04-01 20:33:11 +00002392
2393 // FIXME: Remove these special cases.
Daniel Dunbar7cfe31a2009-05-22 02:21:04 +00002394 if (Arch == "powerpc")
2395 CmdArgs.push_back("ppc");
2396 else if (Arch == "powerpc64")
2397 CmdArgs.push_back("ppc64");
2398 else
Daniel Dunbar88137642009-09-09 22:32:48 +00002399 CmdArgs.push_back(Args.MakeArgString(Arch));
Daniel Dunbarb488c1d2009-03-18 08:07:30 +00002400 }
2401
Daniel Dunbar6ecc7a92009-05-02 21:41:52 +00002402 // Try to force gcc to match the tool chain we want, if we recognize
2403 // the arch.
Daniel Dunbar7cfe31a2009-05-22 02:21:04 +00002404 //
2405 // FIXME: The triple class should directly provide the information we want
2406 // here.
2407 if (Arch == "i386" || Arch == "powerpc")
Daniel Dunbar6ecc7a92009-05-02 21:41:52 +00002408 CmdArgs.push_back("-m32");
Daniel Dunbar7cfe31a2009-05-22 02:21:04 +00002409 else if (Arch == "x86_64" || Arch == "powerpc64")
Daniel Dunbar6ecc7a92009-05-02 21:41:52 +00002410 CmdArgs.push_back("-m64");
2411
Daniel Dunbar7c1e4652010-08-02 02:38:21 +00002412 if (Output.isFilename()) {
Daniel Dunbarb488c1d2009-03-18 08:07:30 +00002413 CmdArgs.push_back("-o");
Daniel Dunbar115a7922009-03-19 07:29:38 +00002414 CmdArgs.push_back(Output.getFilename());
2415 } else {
2416 assert(Output.isNothing() && "Unexpected output");
Daniel Dunbarb488c1d2009-03-18 08:07:30 +00002417 CmdArgs.push_back("-fsyntax-only");
Daniel Dunbar115a7922009-03-19 07:29:38 +00002418 }
Daniel Dunbarb488c1d2009-03-18 08:07:30 +00002419
2420
2421 // Only pass -x if gcc will understand it; otherwise hope gcc
2422 // understands the suffix correctly. The main use case this would go
2423 // wrong in is for linker inputs if they happened to have an odd
2424 // suffix; really the only way to get this to happen is a command
2425 // like '-x foobar a.c' which will treat a.c like a linker input.
2426 //
2427 // FIXME: For the linker case specifically, can we safely convert
2428 // inputs into '-Wl,' options?
2429 for (InputInfoList::const_iterator
2430 it = Inputs.begin(), ie = Inputs.end(); it != ie; ++it) {
2431 const InputInfo &II = *it;
Daniel Dunbara8304f62009-05-02 20:14:53 +00002432
Daniel Dunbar5915fbf2009-09-01 16:57:46 +00002433 // Don't try to pass LLVM or AST inputs to a generic gcc.
Daniel Dunbar6c6424b2010-06-07 23:28:45 +00002434 if (II.getType() == types::TY_LLVM_IR || II.getType() == types::TY_LTO_IR ||
2435 II.getType() == types::TY_LLVM_BC || II.getType() == types::TY_LTO_BC)
Chris Lattner5f9e2722011-07-23 10:55:15 +00002436 D.Diag(diag::err_drv_no_linker_llvm_support)
Daniel Dunbar88137642009-09-09 22:32:48 +00002437 << getToolChain().getTripleString();
Daniel Dunbar5915fbf2009-09-01 16:57:46 +00002438 else if (II.getType() == types::TY_AST)
Chris Lattner5f9e2722011-07-23 10:55:15 +00002439 D.Diag(diag::err_drv_no_ast_support)
Daniel Dunbar88137642009-09-09 22:32:48 +00002440 << getToolChain().getTripleString();
Daniel Dunbara8304f62009-05-02 20:14:53 +00002441
Daniel Dunbarb488c1d2009-03-18 08:07:30 +00002442 if (types::canTypeBeUserSpecified(II.getType())) {
2443 CmdArgs.push_back("-x");
2444 CmdArgs.push_back(types::getTypeName(II.getType()));
2445 }
2446
Daniel Dunbar7c1e4652010-08-02 02:38:21 +00002447 if (II.isFilename())
Daniel Dunbar115a7922009-03-19 07:29:38 +00002448 CmdArgs.push_back(II.getFilename());
Daniel Dunbar48f99942010-09-25 18:10:05 +00002449 else {
2450 const Arg &A = II.getInputArg();
2451
2452 // Reverse translate some rewritten options.
2453 if (A.getOption().matches(options::OPT_Z_reserved_lib_stdcxx)) {
2454 CmdArgs.push_back("-lstdc++");
2455 continue;
2456 }
2457
Daniel Dunbar115a7922009-03-19 07:29:38 +00002458 // Don't render as input, we need gcc to do the translations.
Daniel Dunbar48f99942010-09-25 18:10:05 +00002459 A.render(Args, CmdArgs);
2460 }
Daniel Dunbarb488c1d2009-03-18 08:07:30 +00002461 }
2462
Dylan Noblesmithb8a3e812011-04-09 13:31:59 +00002463 const std::string customGCCName = D.getCCCGenericGCCName();
2464 const char *GCCName;
2465 if (!customGCCName.empty())
2466 GCCName = customGCCName.c_str();
2467 else if (D.CCCIsCXX) {
2468#ifdef IS_CYGWIN15
2469 // FIXME: Detect the version of Cygwin at runtime?
2470 GCCName = "g++-4";
2471#else
2472 GCCName = "g++";
2473#endif
2474 } else
2475 GCCName = "gcc";
2476
Daniel Dunbarc21c4852009-04-08 23:54:23 +00002477 const char *Exec =
Daniel Dunbar4a7e8892010-07-14 18:46:23 +00002478 Args.MakeArgString(getToolChain().GetProgramPath(GCCName));
Daniel Dunbar2fe238e2010-08-02 02:38:28 +00002479 C.addCommand(new Command(JA, *this, Exec, CmdArgs));
Daniel Dunbar47ac7d22009-03-18 06:00:36 +00002480}
2481
Daniel Dunbar82b51cc2010-01-25 22:35:08 +00002482void gcc::Preprocess::RenderExtraToolArgs(const JobAction &JA,
2483 ArgStringList &CmdArgs) const {
Daniel Dunbarb488c1d2009-03-18 08:07:30 +00002484 CmdArgs.push_back("-E");
Daniel Dunbar47ac7d22009-03-18 06:00:36 +00002485}
2486
Daniel Dunbar82b51cc2010-01-25 22:35:08 +00002487void gcc::Precompile::RenderExtraToolArgs(const JobAction &JA,
2488 ArgStringList &CmdArgs) const {
Daniel Dunbarb488c1d2009-03-18 08:07:30 +00002489 // The type is good enough.
Daniel Dunbar47ac7d22009-03-18 06:00:36 +00002490}
2491
Daniel Dunbar82b51cc2010-01-25 22:35:08 +00002492void gcc::Compile::RenderExtraToolArgs(const JobAction &JA,
2493 ArgStringList &CmdArgs) const {
Daniel Dunbar64952502010-02-11 03:16:21 +00002494 const Driver &D = getToolChain().getDriver();
2495
Daniel Dunbar82b51cc2010-01-25 22:35:08 +00002496 // If -flto, etc. are present then make sure not to force assembly output.
Daniel Dunbar6c6424b2010-06-07 23:28:45 +00002497 if (JA.getType() == types::TY_LLVM_IR || JA.getType() == types::TY_LTO_IR ||
2498 JA.getType() == types::TY_LLVM_BC || JA.getType() == types::TY_LTO_BC)
Daniel Dunbar82b51cc2010-01-25 22:35:08 +00002499 CmdArgs.push_back("-c");
Daniel Dunbar64952502010-02-11 03:16:21 +00002500 else {
2501 if (JA.getType() != types::TY_PP_Asm)
Chris Lattner5f9e2722011-07-23 10:55:15 +00002502 D.Diag(diag::err_drv_invalid_gcc_output_type)
Daniel Dunbar64952502010-02-11 03:16:21 +00002503 << getTypeName(JA.getType());
Michael J. Spencer20249a12010-10-21 03:16:25 +00002504
Daniel Dunbar82b51cc2010-01-25 22:35:08 +00002505 CmdArgs.push_back("-S");
Daniel Dunbar64952502010-02-11 03:16:21 +00002506 }
Daniel Dunbar47ac7d22009-03-18 06:00:36 +00002507}
2508
Daniel Dunbar82b51cc2010-01-25 22:35:08 +00002509void gcc::Assemble::RenderExtraToolArgs(const JobAction &JA,
2510 ArgStringList &CmdArgs) const {
Daniel Dunbarb488c1d2009-03-18 08:07:30 +00002511 CmdArgs.push_back("-c");
Daniel Dunbar47ac7d22009-03-18 06:00:36 +00002512}
Daniel Dunbarb488c1d2009-03-18 08:07:30 +00002513
Daniel Dunbar82b51cc2010-01-25 22:35:08 +00002514void gcc::Link::RenderExtraToolArgs(const JobAction &JA,
2515 ArgStringList &CmdArgs) const {
Daniel Dunbarb488c1d2009-03-18 08:07:30 +00002516 // The types are (hopefully) good enough.
2517}
2518
Daniel Dunbar40f12652009-03-29 17:08:39 +00002519const char *darwin::CC1::getCC1Name(types::ID Type) const {
2520 switch (Type) {
2521 default:
David Blaikieb219cfc2011-09-23 05:06:16 +00002522 llvm_unreachable("Unexpected type for Darwin CC1 tool.");
Daniel Dunbar40f12652009-03-29 17:08:39 +00002523 case types::TY_Asm:
2524 case types::TY_C: case types::TY_CHeader:
2525 case types::TY_PP_C: case types::TY_PP_CHeader:
2526 return "cc1";
2527 case types::TY_ObjC: case types::TY_ObjCHeader:
Nico Webercc52a062011-08-13 23:13:37 +00002528 case types::TY_PP_ObjC: case types::TY_PP_ObjC_Alias:
2529 case types::TY_PP_ObjCHeader:
Daniel Dunbar40f12652009-03-29 17:08:39 +00002530 return "cc1obj";
2531 case types::TY_CXX: case types::TY_CXXHeader:
2532 case types::TY_PP_CXX: case types::TY_PP_CXXHeader:
2533 return "cc1plus";
2534 case types::TY_ObjCXX: case types::TY_ObjCXXHeader:
Nico Webercc52a062011-08-13 23:13:37 +00002535 case types::TY_PP_ObjCXX: case types::TY_PP_ObjCXX_Alias:
2536 case types::TY_PP_ObjCXXHeader:
Daniel Dunbar40f12652009-03-29 17:08:39 +00002537 return "cc1objplus";
2538 }
2539}
2540
Daniel Dunbarc21c4852009-04-08 23:54:23 +00002541const char *darwin::CC1::getBaseInputName(const ArgList &Args,
Daniel Dunbara5a7bd02009-03-30 00:34:04 +00002542 const InputInfoList &Inputs) {
Michael J. Spencer472ccff2010-12-18 00:19:12 +00002543 return Args.MakeArgString(
2544 llvm::sys::path::filename(Inputs[0].getBaseInput()));
Daniel Dunbara3ec60e2009-03-29 18:40:18 +00002545}
2546
Daniel Dunbarc21c4852009-04-08 23:54:23 +00002547const char *darwin::CC1::getBaseInputStem(const ArgList &Args,
Daniel Dunbara5a7bd02009-03-30 00:34:04 +00002548 const InputInfoList &Inputs) {
Daniel Dunbara3ec60e2009-03-29 18:40:18 +00002549 const char *Str = getBaseInputName(Args, Inputs);
2550
Chris Lattner657ca662011-01-16 08:14:11 +00002551 if (const char *End = strrchr(Str, '.'))
Daniel Dunbar88137642009-09-09 22:32:48 +00002552 return Args.MakeArgString(std::string(Str, End));
Daniel Dunbara3ec60e2009-03-29 18:40:18 +00002553
2554 return Str;
2555}
2556
2557const char *
Daniel Dunbarc21c4852009-04-08 23:54:23 +00002558darwin::CC1::getDependencyFileName(const ArgList &Args,
Daniel Dunbara5a7bd02009-03-30 00:34:04 +00002559 const InputInfoList &Inputs) {
Daniel Dunbara3ec60e2009-03-29 18:40:18 +00002560 // FIXME: Think about this more.
2561 std::string Res;
2562
2563 if (Arg *OutputOpt = Args.getLastArg(options::OPT_o)) {
2564 std::string Str(OutputOpt->getValue(Args));
Daniel Dunbara3ec60e2009-03-29 18:40:18 +00002565 Res = Str.substr(0, Str.rfind('.'));
Chad Rosier30601782011-08-17 23:08:45 +00002566 } else {
Daniel Dunbara5a7bd02009-03-30 00:34:04 +00002567 Res = darwin::CC1::getBaseInputStem(Args, Inputs);
Chad Rosier30601782011-08-17 23:08:45 +00002568 }
Daniel Dunbar88137642009-09-09 22:32:48 +00002569 return Args.MakeArgString(Res + ".d");
Daniel Dunbara3ec60e2009-03-29 18:40:18 +00002570}
2571
Chad Rosier285f9a22011-08-17 18:24:55 +00002572void darwin::CC1::RemoveCC1UnsupportedArgs(ArgStringList &CmdArgs) const {
Eric Christopher88b7cf02011-08-19 00:30:14 +00002573 for (ArgStringList::iterator it = CmdArgs.begin(), ie = CmdArgs.end();
Chad Rosier285f9a22011-08-17 18:24:55 +00002574 it != ie;) {
Chad Rosier8722a5d2011-08-18 17:56:32 +00002575
2576 StringRef Option = *it;
Chad Rosier80717972011-08-26 18:30:43 +00002577 bool RemoveOption = false;
Chad Rosier04225c12011-08-18 01:18:28 +00002578
Chad Rosier80717972011-08-26 18:30:43 +00002579 // Remove -faltivec
2580 if (Option.equals("-faltivec")) {
2581 it = CmdArgs.erase(it);
2582 ie = CmdArgs.end();
Chad Rosier04225c12011-08-18 01:18:28 +00002583 continue;
2584 }
2585
Chad Rosier80717972011-08-26 18:30:43 +00002586 // Handle machine specific options.
2587 if (Option.startswith("-m")) {
2588 RemoveOption = llvm::StringSwitch<bool>(Option)
2589 .Case("-mthumb", true)
2590 .Case("-mno-thumb", true)
2591 .Case("-mno-fused-madd", true)
2592 .Case("-mlong-branch", true)
2593 .Case("-mlongcall", true)
2594 .Case("-mcpu=G4", true)
2595 .Case("-mcpu=G5", true)
2596 .Default(false);
2597 }
2598
2599 // Handle warning options.
2600 if (Option.startswith("-W")) {
2601 // Remove -W/-Wno- to reduce the number of cases.
2602 if (Option.startswith("-Wno-"))
2603 Option = Option.substr(5);
2604 else
2605 Option = Option.substr(2);
2606
2607 RemoveOption = llvm::StringSwitch<bool>(Option)
2608 .Case("address-of-temporary", true)
2609 .Case("ambiguous-member-template", true)
2610 .Case("analyzer-incompatible-plugin", true)
2611 .Case("array-bounds", true)
2612 .Case("array-bounds-pointer-arithmetic", true)
2613 .Case("bind-to-temporary-copy", true)
2614 .Case("bitwise-op-parentheses", true)
2615 .Case("bool-conversions", true)
2616 .Case("builtin-macro-redefined", true)
2617 .Case("c++-hex-floats", true)
2618 .Case("c++0x-compat", true)
2619 .Case("c++0x-extensions", true)
2620 .Case("c++0x-narrowing", true)
Richard Smith575fdda2011-10-13 23:32:09 +00002621 .Case("c++11-compat", true)
2622 .Case("c++11-extensions", true)
2623 .Case("c++11-narrowing", true)
Chad Rosier80717972011-08-26 18:30:43 +00002624 .Case("conditional-uninitialized", true)
2625 .Case("constant-conversion", true)
2626 .Case("CFString-literal", true)
2627 .Case("constant-logical-operand", true)
2628 .Case("custom-atomic-properties", true)
2629 .Case("default-arg-special-member", true)
2630 .Case("delegating-ctor-cycles", true)
2631 .Case("delete-non-virtual-dtor", true)
2632 .Case("deprecated-implementations", true)
2633 .Case("deprecated-writable-strings", true)
2634 .Case("distributed-object-modifiers", true)
2635 .Case("duplicate-method-arg", true)
2636 .Case("dynamic-class-memaccess", true)
2637 .Case("enum-compare", true)
2638 .Case("exit-time-destructors", true)
2639 .Case("gnu", true)
2640 .Case("gnu-designator", true)
2641 .Case("header-hygiene", true)
2642 .Case("idiomatic-parentheses", true)
2643 .Case("ignored-qualifiers", true)
2644 .Case("implicit-atomic-properties", true)
2645 .Case("incompatible-pointer-types", true)
2646 .Case("incomplete-implementation", true)
2647 .Case("initializer-overrides", true)
2648 .Case("invalid-noreturn", true)
2649 .Case("invalid-token-paste", true)
Ivan Krasin08f35a72011-10-06 02:46:34 +00002650 .Case("language-extension-token", true)
Chad Rosier80717972011-08-26 18:30:43 +00002651 .Case("literal-conversion", true)
2652 .Case("literal-range", true)
2653 .Case("local-type-template-args", true)
2654 .Case("logical-op-parentheses", true)
2655 .Case("method-signatures", true)
2656 .Case("microsoft", true)
2657 .Case("mismatched-tags", true)
2658 .Case("missing-method-return-type", true)
2659 .Case("non-pod-varargs", true)
2660 .Case("nonfragile-abi2", true)
2661 .Case("null-arithmetic", true)
2662 .Case("null-dereference", true)
2663 .Case("out-of-line-declaration", true)
2664 .Case("overriding-method-mismatch", true)
2665 .Case("readonly-setter-attrs", true)
2666 .Case("return-stack-address", true)
2667 .Case("self-assign", true)
2668 .Case("semicolon-before-method-body", true)
2669 .Case("sentinel", true)
2670 .Case("shift-overflow", true)
2671 .Case("shift-sign-overflow", true)
2672 .Case("sign-conversion", true)
2673 .Case("sizeof-array-argument", true)
2674 .Case("sizeof-pointer-memaccess", true)
2675 .Case("string-compare", true)
2676 .Case("super-class-method-mismatch", true)
2677 .Case("tautological-compare", true)
2678 .Case("typedef-redefinition", true)
2679 .Case("typename-missing", true)
2680 .Case("undefined-reinterpret-cast", true)
2681 .Case("unknown-warning-option", true)
2682 .Case("unnamed-type-template-args", true)
2683 .Case("unneeded-internal-declaration", true)
2684 .Case("unneeded-member-function", true)
2685 .Case("unused-comparison", true)
2686 .Case("unused-exception-parameter", true)
2687 .Case("unused-member-function", true)
2688 .Case("unused-result", true)
2689 .Case("vector-conversions", true)
2690 .Case("vla", true)
2691 .Case("used-but-marked-unused", true)
2692 .Case("weak-vtables", true)
2693 .Default(false);
2694 } // if (Option.startswith("-W"))
Chad Rosier04225c12011-08-18 01:18:28 +00002695 if (RemoveOption) {
Chad Rosier285f9a22011-08-17 18:24:55 +00002696 it = CmdArgs.erase(it);
Chad Rosiercc0de8c2011-08-17 18:51:56 +00002697 ie = CmdArgs.end();
Chad Rosier30601782011-08-17 23:08:45 +00002698 } else {
Chad Rosier285f9a22011-08-17 18:24:55 +00002699 ++it;
Chad Rosier30601782011-08-17 23:08:45 +00002700 }
Chad Rosier285f9a22011-08-17 18:24:55 +00002701 }
2702}
2703
Daniel Dunbarc21c4852009-04-08 23:54:23 +00002704void darwin::CC1::AddCC1Args(const ArgList &Args,
Daniel Dunbara3ec60e2009-03-29 18:40:18 +00002705 ArgStringList &CmdArgs) const {
Daniel Dunbaree788e72009-12-21 18:54:17 +00002706 const Driver &D = getToolChain().getDriver();
Daniel Dunbare2fd6642009-09-10 01:21:12 +00002707
2708 CheckCodeGenerationOptions(D, Args);
2709
Daniel Dunbara3ec60e2009-03-29 18:40:18 +00002710 // Derived from cc1 spec.
Daniel Dunbara3ec60e2009-03-29 18:40:18 +00002711 if (!Args.hasArg(options::OPT_mkernel) && !Args.hasArg(options::OPT_static) &&
2712 !Args.hasArg(options::OPT_mdynamic_no_pic))
2713 CmdArgs.push_back("-fPIC");
2714
Daniel Dunbar2ba91572009-09-10 03:37:02 +00002715 if (getToolChain().getTriple().getArch() == llvm::Triple::arm ||
2716 getToolChain().getTriple().getArch() == llvm::Triple::thumb) {
2717 if (!Args.hasArg(options::OPT_fbuiltin_strcat))
2718 CmdArgs.push_back("-fno-builtin-strcat");
2719 if (!Args.hasArg(options::OPT_fbuiltin_strcpy))
2720 CmdArgs.push_back("-fno-builtin-strcpy");
2721 }
2722
Daniel Dunbara3ec60e2009-03-29 18:40:18 +00002723 if (Args.hasArg(options::OPT_g_Flag) &&
2724 !Args.hasArg(options::OPT_fno_eliminate_unused_debug_symbols))
2725 CmdArgs.push_back("-feliminate-unused-debug-symbols");
2726}
2727
2728void darwin::CC1::AddCC1OptionsArgs(const ArgList &Args, ArgStringList &CmdArgs,
2729 const InputInfoList &Inputs,
2730 const ArgStringList &OutputArgs) const {
Daniel Dunbaree788e72009-12-21 18:54:17 +00002731 const Driver &D = getToolChain().getDriver();
Daniel Dunbara3ec60e2009-03-29 18:40:18 +00002732
2733 // Derived from cc1_options spec.
2734 if (Args.hasArg(options::OPT_fast) ||
2735 Args.hasArg(options::OPT_fastf) ||
2736 Args.hasArg(options::OPT_fastcp))
2737 CmdArgs.push_back("-O3");
Daniel Dunbarc21c4852009-04-08 23:54:23 +00002738
Daniel Dunbara3ec60e2009-03-29 18:40:18 +00002739 if (Arg *A = Args.getLastArg(options::OPT_pg))
2740 if (Args.hasArg(options::OPT_fomit_frame_pointer))
Chris Lattner5f9e2722011-07-23 10:55:15 +00002741 D.Diag(diag::err_drv_argument_not_allowed_with)
Daniel Dunbara3ec60e2009-03-29 18:40:18 +00002742 << A->getAsString(Args) << "-fomit-frame-pointer";
2743
2744 AddCC1Args(Args, CmdArgs);
2745
2746 if (!Args.hasArg(options::OPT_Q))
2747 CmdArgs.push_back("-quiet");
2748
2749 CmdArgs.push_back("-dumpbase");
Daniel Dunbara5a7bd02009-03-30 00:34:04 +00002750 CmdArgs.push_back(darwin::CC1::getBaseInputName(Args, Inputs));
Daniel Dunbara3ec60e2009-03-29 18:40:18 +00002751
2752 Args.AddAllArgs(CmdArgs, options::OPT_d_Group);
2753
2754 Args.AddAllArgs(CmdArgs, options::OPT_m_Group);
2755 Args.AddAllArgs(CmdArgs, options::OPT_a_Group);
2756
2757 // FIXME: The goal is to use the user provided -o if that is our
2758 // final output, otherwise to drive from the original input
2759 // name. Find a clean way to go about this.
2760 if ((Args.hasArg(options::OPT_c) || Args.hasArg(options::OPT_S)) &&
2761 Args.hasArg(options::OPT_o)) {
2762 Arg *OutputOpt = Args.getLastArg(options::OPT_o);
2763 CmdArgs.push_back("-auxbase-strip");
2764 CmdArgs.push_back(OutputOpt->getValue(Args));
2765 } else {
2766 CmdArgs.push_back("-auxbase");
Daniel Dunbara5a7bd02009-03-30 00:34:04 +00002767 CmdArgs.push_back(darwin::CC1::getBaseInputStem(Args, Inputs));
Daniel Dunbara3ec60e2009-03-29 18:40:18 +00002768 }
2769
2770 Args.AddAllArgs(CmdArgs, options::OPT_g_Group);
2771
2772 Args.AddAllArgs(CmdArgs, options::OPT_O);
2773 // FIXME: -Wall is getting some special treatment. Investigate.
2774 Args.AddAllArgs(CmdArgs, options::OPT_W_Group, options::OPT_pedantic_Group);
2775 Args.AddLastArg(CmdArgs, options::OPT_w);
Daniel Dunbarc21c4852009-04-08 23:54:23 +00002776 Args.AddAllArgs(CmdArgs, options::OPT_std_EQ, options::OPT_ansi,
Daniel Dunbara3ec60e2009-03-29 18:40:18 +00002777 options::OPT_trigraphs);
Daniel Dunbara3ff2022009-04-26 01:10:38 +00002778 if (!Args.getLastArg(options::OPT_std_EQ, options::OPT_ansi)) {
2779 // Honor -std-default.
2780 Args.AddAllArgsTranslated(CmdArgs, options::OPT_std_default_EQ,
2781 "-std=", /*Joined=*/true);
2782 }
2783
Daniel Dunbara3ec60e2009-03-29 18:40:18 +00002784 if (Args.hasArg(options::OPT_v))
2785 CmdArgs.push_back("-version");
Daniel Dunbarbbe8e3e2011-03-01 18:49:30 +00002786 if (Args.hasArg(options::OPT_pg) &&
2787 getToolChain().SupportsProfiling())
Daniel Dunbara3ec60e2009-03-29 18:40:18 +00002788 CmdArgs.push_back("-p");
2789 Args.AddLastArg(CmdArgs, options::OPT_p);
Daniel Dunbarc21c4852009-04-08 23:54:23 +00002790
Daniel Dunbara3ec60e2009-03-29 18:40:18 +00002791 // The driver treats -fsyntax-only specially.
Daniel Dunbar2ba91572009-09-10 03:37:02 +00002792 if (getToolChain().getTriple().getArch() == llvm::Triple::arm ||
2793 getToolChain().getTriple().getArch() == llvm::Triple::thumb) {
2794 // Removes -fbuiltin-str{cat,cpy}; these aren't recognized by cc1 but are
2795 // used to inhibit the default -fno-builtin-str{cat,cpy}.
2796 //
2797 // FIXME: Should we grow a better way to deal with "removing" args?
Daniel Dunbarcdd96862009-11-25 11:53:23 +00002798 for (arg_iterator it = Args.filtered_begin(options::OPT_f_Group,
2799 options::OPT_fsyntax_only),
2800 ie = Args.filtered_end(); it != ie; ++it) {
Daniel Dunbar7e4953e2010-06-11 22:00:13 +00002801 if (!(*it)->getOption().matches(options::OPT_fbuiltin_strcat) &&
2802 !(*it)->getOption().matches(options::OPT_fbuiltin_strcpy)) {
2803 (*it)->claim();
2804 (*it)->render(Args, CmdArgs);
Daniel Dunbar2ba91572009-09-10 03:37:02 +00002805 }
2806 }
2807 } else
2808 Args.AddAllArgs(CmdArgs, options::OPT_f_Group, options::OPT_fsyntax_only);
Daniel Dunbarc21c4852009-04-08 23:54:23 +00002809
Daniel Dunbar089f8722011-04-07 20:41:03 +00002810 // Claim Clang only -f options, they aren't worth warning about.
2811 Args.ClaimAllArgs(options::OPT_f_clang_Group);
2812
Daniel Dunbara3ec60e2009-03-29 18:40:18 +00002813 Args.AddAllArgs(CmdArgs, options::OPT_undef);
2814 if (Args.hasArg(options::OPT_Qn))
2815 CmdArgs.push_back("-fno-ident");
Daniel Dunbarc21c4852009-04-08 23:54:23 +00002816
Daniel Dunbara3ec60e2009-03-29 18:40:18 +00002817 // FIXME: This isn't correct.
2818 //Args.AddLastArg(CmdArgs, options::OPT__help)
2819 //Args.AddLastArg(CmdArgs, options::OPT__targetHelp)
2820
2821 CmdArgs.append(OutputArgs.begin(), OutputArgs.end());
2822
2823 // FIXME: Still don't get what is happening here. Investigate.
2824 Args.AddAllArgs(CmdArgs, options::OPT__param);
2825
2826 if (Args.hasArg(options::OPT_fmudflap) ||
2827 Args.hasArg(options::OPT_fmudflapth)) {
2828 CmdArgs.push_back("-fno-builtin");
2829 CmdArgs.push_back("-fno-merge-constants");
2830 }
Daniel Dunbarc21c4852009-04-08 23:54:23 +00002831
Daniel Dunbara3ec60e2009-03-29 18:40:18 +00002832 if (Args.hasArg(options::OPT_coverage)) {
2833 CmdArgs.push_back("-fprofile-arcs");
2834 CmdArgs.push_back("-ftest-coverage");
2835 }
2836
2837 if (types::isCXX(Inputs[0].getType()))
2838 CmdArgs.push_back("-D__private_extern__=extern");
2839}
2840
2841void darwin::CC1::AddCPPOptionsArgs(const ArgList &Args, ArgStringList &CmdArgs,
2842 const InputInfoList &Inputs,
2843 const ArgStringList &OutputArgs) const {
2844 // Derived from cpp_options
2845 AddCPPUniqueOptionsArgs(Args, CmdArgs, Inputs);
Daniel Dunbarc21c4852009-04-08 23:54:23 +00002846
Daniel Dunbara3ec60e2009-03-29 18:40:18 +00002847 CmdArgs.append(OutputArgs.begin(), OutputArgs.end());
2848
2849 AddCC1Args(Args, CmdArgs);
2850
2851 // NOTE: The code below has some commonality with cpp_options, but
2852 // in classic gcc style ends up sending things in different
2853 // orders. This may be a good merge candidate once we drop pedantic
2854 // compatibility.
2855
2856 Args.AddAllArgs(CmdArgs, options::OPT_m_Group);
Daniel Dunbarc21c4852009-04-08 23:54:23 +00002857 Args.AddAllArgs(CmdArgs, options::OPT_std_EQ, options::OPT_ansi,
Daniel Dunbara3ec60e2009-03-29 18:40:18 +00002858 options::OPT_trigraphs);
Daniel Dunbara3ff2022009-04-26 01:10:38 +00002859 if (!Args.getLastArg(options::OPT_std_EQ, options::OPT_ansi)) {
2860 // Honor -std-default.
2861 Args.AddAllArgsTranslated(CmdArgs, options::OPT_std_default_EQ,
2862 "-std=", /*Joined=*/true);
2863 }
Daniel Dunbara3ec60e2009-03-29 18:40:18 +00002864 Args.AddAllArgs(CmdArgs, options::OPT_W_Group, options::OPT_pedantic_Group);
2865 Args.AddLastArg(CmdArgs, options::OPT_w);
Daniel Dunbarc21c4852009-04-08 23:54:23 +00002866
Daniel Dunbara3ec60e2009-03-29 18:40:18 +00002867 // The driver treats -fsyntax-only specially.
2868 Args.AddAllArgs(CmdArgs, options::OPT_f_Group, options::OPT_fsyntax_only);
2869
Daniel Dunbar089f8722011-04-07 20:41:03 +00002870 // Claim Clang only -f options, they aren't worth warning about.
2871 Args.ClaimAllArgs(options::OPT_f_clang_Group);
2872
Daniel Dunbara3ec60e2009-03-29 18:40:18 +00002873 if (Args.hasArg(options::OPT_g_Group) && !Args.hasArg(options::OPT_g0) &&
2874 !Args.hasArg(options::OPT_fno_working_directory))
2875 CmdArgs.push_back("-fworking-directory");
2876
2877 Args.AddAllArgs(CmdArgs, options::OPT_O);
2878 Args.AddAllArgs(CmdArgs, options::OPT_undef);
2879 if (Args.hasArg(options::OPT_save_temps))
2880 CmdArgs.push_back("-fpch-preprocess");
2881}
2882
Daniel Dunbarc21c4852009-04-08 23:54:23 +00002883void darwin::CC1::AddCPPUniqueOptionsArgs(const ArgList &Args,
Daniel Dunbara3ec60e2009-03-29 18:40:18 +00002884 ArgStringList &CmdArgs,
Mike Stump1eb44332009-09-09 15:08:12 +00002885 const InputInfoList &Inputs) const {
Daniel Dunbaree788e72009-12-21 18:54:17 +00002886 const Driver &D = getToolChain().getDriver();
Daniel Dunbara3ec60e2009-03-29 18:40:18 +00002887
Daniel Dunbar88a3d6c2009-09-10 01:21:05 +00002888 CheckPreprocessingOptions(D, Args);
2889
Daniel Dunbara3ec60e2009-03-29 18:40:18 +00002890 // Derived from cpp_unique_options.
Daniel Dunbar88a3d6c2009-09-10 01:21:05 +00002891 // -{C,CC} only with -E is checked in CheckPreprocessingOptions().
2892 Args.AddLastArg(CmdArgs, options::OPT_C);
2893 Args.AddLastArg(CmdArgs, options::OPT_CC);
Daniel Dunbara3ec60e2009-03-29 18:40:18 +00002894 if (!Args.hasArg(options::OPT_Q))
2895 CmdArgs.push_back("-quiet");
2896 Args.AddAllArgs(CmdArgs, options::OPT_nostdinc);
Douglas Gregor4c2bcad2010-03-24 20:13:48 +00002897 Args.AddAllArgs(CmdArgs, options::OPT_nostdincxx);
Daniel Dunbara3ec60e2009-03-29 18:40:18 +00002898 Args.AddLastArg(CmdArgs, options::OPT_v);
2899 Args.AddAllArgs(CmdArgs, options::OPT_I_Group, options::OPT_F);
2900 Args.AddLastArg(CmdArgs, options::OPT_P);
2901
2902 // FIXME: Handle %I properly.
2903 if (getToolChain().getArchName() == "x86_64") {
2904 CmdArgs.push_back("-imultilib");
2905 CmdArgs.push_back("x86_64");
2906 }
2907
2908 if (Args.hasArg(options::OPT_MD)) {
2909 CmdArgs.push_back("-MD");
Daniel Dunbara5a7bd02009-03-30 00:34:04 +00002910 CmdArgs.push_back(darwin::CC1::getDependencyFileName(Args, Inputs));
Daniel Dunbara3ec60e2009-03-29 18:40:18 +00002911 }
2912
2913 if (Args.hasArg(options::OPT_MMD)) {
2914 CmdArgs.push_back("-MMD");
Daniel Dunbara5a7bd02009-03-30 00:34:04 +00002915 CmdArgs.push_back(darwin::CC1::getDependencyFileName(Args, Inputs));
Daniel Dunbara3ec60e2009-03-29 18:40:18 +00002916 }
2917
2918 Args.AddLastArg(CmdArgs, options::OPT_M);
2919 Args.AddLastArg(CmdArgs, options::OPT_MM);
2920 Args.AddAllArgs(CmdArgs, options::OPT_MF);
2921 Args.AddLastArg(CmdArgs, options::OPT_MG);
2922 Args.AddLastArg(CmdArgs, options::OPT_MP);
2923 Args.AddAllArgs(CmdArgs, options::OPT_MQ);
2924 Args.AddAllArgs(CmdArgs, options::OPT_MT);
2925 if (!Args.hasArg(options::OPT_M) && !Args.hasArg(options::OPT_MM) &&
2926 (Args.hasArg(options::OPT_MD) || Args.hasArg(options::OPT_MMD))) {
2927 if (Arg *OutputOpt = Args.getLastArg(options::OPT_o)) {
2928 CmdArgs.push_back("-MQ");
2929 CmdArgs.push_back(OutputOpt->getValue(Args));
2930 }
2931 }
2932
2933 Args.AddLastArg(CmdArgs, options::OPT_remap);
2934 if (Args.hasArg(options::OPT_g3))
2935 CmdArgs.push_back("-dD");
2936 Args.AddLastArg(CmdArgs, options::OPT_H);
2937
2938 AddCPPArgs(Args, CmdArgs);
2939
2940 Args.AddAllArgs(CmdArgs, options::OPT_D, options::OPT_U, options::OPT_A);
2941 Args.AddAllArgs(CmdArgs, options::OPT_i_Group);
2942
2943 for (InputInfoList::const_iterator
2944 it = Inputs.begin(), ie = Inputs.end(); it != ie; ++it) {
2945 const InputInfo &II = *it;
Daniel Dunbarc21c4852009-04-08 23:54:23 +00002946
Daniel Dunbar7c1e4652010-08-02 02:38:21 +00002947 CmdArgs.push_back(II.getFilename());
Daniel Dunbara3ec60e2009-03-29 18:40:18 +00002948 }
2949
2950 Args.AddAllArgValues(CmdArgs, options::OPT_Wp_COMMA,
2951 options::OPT_Xpreprocessor);
2952
2953 if (Args.hasArg(options::OPT_fmudflap)) {
2954 CmdArgs.push_back("-D_MUDFLAP");
2955 CmdArgs.push_back("-include");
2956 CmdArgs.push_back("mf-runtime.h");
2957 }
2958
2959 if (Args.hasArg(options::OPT_fmudflapth)) {
2960 CmdArgs.push_back("-D_MUDFLAP");
2961 CmdArgs.push_back("-D_MUDFLAPTH");
2962 CmdArgs.push_back("-include");
2963 CmdArgs.push_back("mf-runtime.h");
2964 }
2965}
2966
Daniel Dunbarc21c4852009-04-08 23:54:23 +00002967void darwin::CC1::AddCPPArgs(const ArgList &Args,
Daniel Dunbara3ec60e2009-03-29 18:40:18 +00002968 ArgStringList &CmdArgs) const {
2969 // Derived from cpp spec.
2970
2971 if (Args.hasArg(options::OPT_static)) {
2972 // The gcc spec is broken here, it refers to dynamic but
2973 // that has been translated. Start by being bug compatible.
Daniel Dunbarc21c4852009-04-08 23:54:23 +00002974
Daniel Dunbara3ec60e2009-03-29 18:40:18 +00002975 // if (!Args.hasArg(arglist.parser.dynamicOption))
2976 CmdArgs.push_back("-D__STATIC__");
2977 } else
2978 CmdArgs.push_back("-D__DYNAMIC__");
2979
2980 if (Args.hasArg(options::OPT_pthread))
2981 CmdArgs.push_back("-D_REENTRANT");
2982}
2983
Daniel Dunbar40f12652009-03-29 17:08:39 +00002984void darwin::Preprocess::ConstructJob(Compilation &C, const JobAction &JA,
Daniel Dunbar2fe238e2010-08-02 02:38:28 +00002985 const InputInfo &Output,
Daniel Dunbarc21c4852009-04-08 23:54:23 +00002986 const InputInfoList &Inputs,
2987 const ArgList &Args,
Daniel Dunbar40f12652009-03-29 17:08:39 +00002988 const char *LinkingOutput) const {
2989 ArgStringList CmdArgs;
2990
2991 assert(Inputs.size() == 1 && "Unexpected number of inputs!");
2992
2993 CmdArgs.push_back("-E");
2994
2995 if (Args.hasArg(options::OPT_traditional) ||
Daniel Dunbar40f12652009-03-29 17:08:39 +00002996 Args.hasArg(options::OPT_traditional_cpp))
2997 CmdArgs.push_back("-traditional-cpp");
Daniel Dunbarc21c4852009-04-08 23:54:23 +00002998
Daniel Dunbar40f12652009-03-29 17:08:39 +00002999 ArgStringList OutputArgs;
Daniel Dunbar7c1e4652010-08-02 02:38:21 +00003000 assert(Output.isFilename() && "Unexpected CC1 output.");
3001 OutputArgs.push_back("-o");
3002 OutputArgs.push_back(Output.getFilename());
Daniel Dunbar40f12652009-03-29 17:08:39 +00003003
Joerg Sonnenberger9ade4ae2011-03-06 23:31:01 +00003004 if (Args.hasArg(options::OPT_E) || getToolChain().getDriver().CCCIsCPP) {
Daniel Dunbar9120f172009-03-29 22:27:40 +00003005 AddCPPOptionsArgs(Args, CmdArgs, Inputs, OutputArgs);
3006 } else {
3007 AddCPPOptionsArgs(Args, CmdArgs, Inputs, ArgStringList());
3008 CmdArgs.append(OutputArgs.begin(), OutputArgs.end());
3009 }
Daniel Dunbarc21c4852009-04-08 23:54:23 +00003010
Daniel Dunbar8a2073a2009-04-03 01:27:06 +00003011 Args.AddAllArgs(CmdArgs, options::OPT_d_Group);
3012
Chad Rosier4fe4d732011-09-08 00:38:00 +00003013 RemoveCC1UnsupportedArgs(CmdArgs);
3014
Daniel Dunbar40f12652009-03-29 17:08:39 +00003015 const char *CC1Name = getCC1Name(Inputs[0].getType());
Daniel Dunbarc21c4852009-04-08 23:54:23 +00003016 const char *Exec =
Daniel Dunbar4a7e8892010-07-14 18:46:23 +00003017 Args.MakeArgString(getToolChain().GetProgramPath(CC1Name));
Daniel Dunbar2fe238e2010-08-02 02:38:28 +00003018 C.addCommand(new Command(JA, *this, Exec, CmdArgs));
Daniel Dunbar40f12652009-03-29 17:08:39 +00003019}
3020
3021void darwin::Compile::ConstructJob(Compilation &C, const JobAction &JA,
Daniel Dunbar2fe238e2010-08-02 02:38:28 +00003022 const InputInfo &Output,
Daniel Dunbarc21c4852009-04-08 23:54:23 +00003023 const InputInfoList &Inputs,
3024 const ArgList &Args,
Daniel Dunbar40f12652009-03-29 17:08:39 +00003025 const char *LinkingOutput) const {
Daniel Dunbaree788e72009-12-21 18:54:17 +00003026 const Driver &D = getToolChain().getDriver();
Daniel Dunbar40f12652009-03-29 17:08:39 +00003027 ArgStringList CmdArgs;
3028
3029 assert(Inputs.size() == 1 && "Unexpected number of inputs!");
3030
3031 types::ID InputType = Inputs[0].getType();
3032 const Arg *A;
Eli Friedmanceb5c5b2009-07-14 21:58:17 +00003033 if ((A = Args.getLastArg(options::OPT_traditional)))
Chris Lattner5f9e2722011-07-23 10:55:15 +00003034 D.Diag(diag::err_drv_argument_only_allowed_with)
Daniel Dunbar40f12652009-03-29 17:08:39 +00003035 << A->getAsString(Args) << "-E";
3036
Daniel Dunbar6c6424b2010-06-07 23:28:45 +00003037 if (JA.getType() == types::TY_LLVM_IR ||
3038 JA.getType() == types::TY_LTO_IR)
Daniel Dunbar40f12652009-03-29 17:08:39 +00003039 CmdArgs.push_back("-emit-llvm");
Daniel Dunbar6c6424b2010-06-07 23:28:45 +00003040 else if (JA.getType() == types::TY_LLVM_BC ||
3041 JA.getType() == types::TY_LTO_BC)
Daniel Dunbar40f12652009-03-29 17:08:39 +00003042 CmdArgs.push_back("-emit-llvm-bc");
Daniel Dunbar5915fbf2009-09-01 16:57:46 +00003043 else if (Output.getType() == types::TY_AST)
Chris Lattner5f9e2722011-07-23 10:55:15 +00003044 D.Diag(diag::err_drv_no_ast_support)
Daniel Dunbar88137642009-09-09 22:32:48 +00003045 << getToolChain().getTripleString();
Daniel Dunbarae24a882010-02-11 17:33:45 +00003046 else if (JA.getType() != types::TY_PP_Asm &&
3047 JA.getType() != types::TY_PCH)
Chris Lattner5f9e2722011-07-23 10:55:15 +00003048 D.Diag(diag::err_drv_invalid_gcc_output_type)
Daniel Dunbar64952502010-02-11 03:16:21 +00003049 << getTypeName(JA.getType());
Daniel Dunbar40f12652009-03-29 17:08:39 +00003050
3051 ArgStringList OutputArgs;
3052 if (Output.getType() != types::TY_PCH) {
3053 OutputArgs.push_back("-o");
Daniel Dunbar7c1e4652010-08-02 02:38:21 +00003054 if (Output.isNothing())
Daniel Dunbar40f12652009-03-29 17:08:39 +00003055 OutputArgs.push_back("/dev/null");
3056 else
3057 OutputArgs.push_back(Output.getFilename());
3058 }
3059
3060 // There is no need for this level of compatibility, but it makes
3061 // diffing easier.
3062 bool OutputArgsEarly = (Args.hasArg(options::OPT_fsyntax_only) ||
3063 Args.hasArg(options::OPT_S));
3064
3065 if (types::getPreprocessedType(InputType) != types::TY_INVALID) {
Daniel Dunbara3ec60e2009-03-29 18:40:18 +00003066 AddCPPUniqueOptionsArgs(Args, CmdArgs, Inputs);
Daniel Dunbar40f12652009-03-29 17:08:39 +00003067 if (OutputArgsEarly) {
3068 AddCC1OptionsArgs(Args, CmdArgs, Inputs, OutputArgs);
3069 } else {
3070 AddCC1OptionsArgs(Args, CmdArgs, Inputs, ArgStringList());
3071 CmdArgs.append(OutputArgs.begin(), OutputArgs.end());
3072 }
3073 } else {
3074 CmdArgs.push_back("-fpreprocessed");
Daniel Dunbarc21c4852009-04-08 23:54:23 +00003075
Daniel Dunbar40f12652009-03-29 17:08:39 +00003076 for (InputInfoList::const_iterator
3077 it = Inputs.begin(), ie = Inputs.end(); it != ie; ++it) {
3078 const InputInfo &II = *it;
3079
Daniel Dunbar5915fbf2009-09-01 16:57:46 +00003080 // Reject AST inputs.
3081 if (II.getType() == types::TY_AST) {
Chris Lattner5f9e2722011-07-23 10:55:15 +00003082 D.Diag(diag::err_drv_no_ast_support)
Daniel Dunbar88137642009-09-09 22:32:48 +00003083 << getToolChain().getTripleString();
Daniel Dunbar5915fbf2009-09-01 16:57:46 +00003084 return;
3085 }
3086
Daniel Dunbar7c1e4652010-08-02 02:38:21 +00003087 CmdArgs.push_back(II.getFilename());
Daniel Dunbar40f12652009-03-29 17:08:39 +00003088 }
3089
3090 if (OutputArgsEarly) {
3091 AddCC1OptionsArgs(Args, CmdArgs, Inputs, OutputArgs);
3092 } else {
3093 AddCC1OptionsArgs(Args, CmdArgs, Inputs, ArgStringList());
3094 CmdArgs.append(OutputArgs.begin(), OutputArgs.end());
3095 }
3096 }
Daniel Dunbarc21c4852009-04-08 23:54:23 +00003097
Daniel Dunbar40f12652009-03-29 17:08:39 +00003098 if (Output.getType() == types::TY_PCH) {
3099 assert(Output.isFilename() && "Invalid PCH output.");
3100
3101 CmdArgs.push_back("-o");
3102 // NOTE: gcc uses a temp .s file for this, but there doesn't seem
3103 // to be a good reason.
Chad Rosier8c221b82011-08-01 19:58:48 +00003104 const char *TmpPath = C.getArgs().MakeArgString(
Chad Rosierfe87fc72011-08-26 21:28:44 +00003105 D.GetTemporaryPath("cc", "s"));
Chad Rosier8c221b82011-08-01 19:58:48 +00003106 C.addTempFile(TmpPath);
3107 CmdArgs.push_back(TmpPath);
Daniel Dunbarc21c4852009-04-08 23:54:23 +00003108
Eric Christopher88b7cf02011-08-19 00:30:14 +00003109 // If we're emitting a pch file with the last 4 characters of ".pth"
3110 // and falling back to llvm-gcc we want to use ".gch" instead.
3111 std::string OutputFile(Output.getFilename());
3112 size_t loc = OutputFile.rfind(".pth");
3113 if (loc != std::string::npos)
3114 OutputFile.replace(loc, 4, ".gch");
3115 const char *Tmp = C.getArgs().MakeArgString("--output-pch="+OutputFile);
3116 CmdArgs.push_back(Tmp);
Daniel Dunbarc21c4852009-04-08 23:54:23 +00003117 }
Daniel Dunbar40f12652009-03-29 17:08:39 +00003118
Chad Rosier285f9a22011-08-17 18:24:55 +00003119 RemoveCC1UnsupportedArgs(CmdArgs);
3120
Daniel Dunbar40f12652009-03-29 17:08:39 +00003121 const char *CC1Name = getCC1Name(Inputs[0].getType());
Daniel Dunbarc21c4852009-04-08 23:54:23 +00003122 const char *Exec =
Daniel Dunbar4a7e8892010-07-14 18:46:23 +00003123 Args.MakeArgString(getToolChain().GetProgramPath(CC1Name));
Daniel Dunbar2fe238e2010-08-02 02:38:28 +00003124 C.addCommand(new Command(JA, *this, Exec, CmdArgs));
Daniel Dunbar40f12652009-03-29 17:08:39 +00003125}
3126
Daniel Dunbar8cac5f72009-03-20 16:06:39 +00003127void darwin::Assemble::ConstructJob(Compilation &C, const JobAction &JA,
Daniel Dunbar2fe238e2010-08-02 02:38:28 +00003128 const InputInfo &Output,
Daniel Dunbarc21c4852009-04-08 23:54:23 +00003129 const InputInfoList &Inputs,
3130 const ArgList &Args,
Daniel Dunbar8cac5f72009-03-20 16:06:39 +00003131 const char *LinkingOutput) const {
3132 ArgStringList CmdArgs;
3133
3134 assert(Inputs.size() == 1 && "Unexpected number of inputs.");
3135 const InputInfo &Input = Inputs[0];
3136
Daniel Dunbar34bac1f2011-04-12 23:59:20 +00003137 // Determine the original source input.
3138 const Action *SourceAction = &JA;
3139 while (SourceAction->getKind() != Action::InputClass) {
3140 assert(!SourceAction->getInputs().empty() && "unexpected root action!");
3141 SourceAction = SourceAction->getInputs()[0];
3142 }
3143
3144 // Forward -g, assuming we are dealing with an actual assembly file.
Eric Christopher88b7cf02011-08-19 00:30:14 +00003145 if (SourceAction->getType() == types::TY_Asm ||
Daniel Dunbar34bac1f2011-04-12 23:59:20 +00003146 SourceAction->getType() == types::TY_PP_Asm) {
Daniel Dunbar8e4fea62009-04-01 00:27:44 +00003147 if (Args.hasArg(options::OPT_gstabs))
3148 CmdArgs.push_back("--gstabs");
3149 else if (Args.hasArg(options::OPT_g_Group))
Bob Wilson591ff152011-11-02 05:10:45 +00003150 CmdArgs.push_back("-g");
Daniel Dunbar8e4fea62009-04-01 00:27:44 +00003151 }
Daniel Dunbarc21c4852009-04-08 23:54:23 +00003152
Daniel Dunbar8cac5f72009-03-20 16:06:39 +00003153 // Derived from asm spec.
Daniel Dunbarcc6f8032009-09-09 18:36:27 +00003154 AddDarwinArch(Args, CmdArgs);
Daniel Dunbar8cac5f72009-03-20 16:06:39 +00003155
Daniel Dunbarf5438e32010-07-22 01:47:22 +00003156 // Use -force_cpusubtype_ALL on x86 by default.
3157 if (getToolChain().getTriple().getArch() == llvm::Triple::x86 ||
3158 getToolChain().getTriple().getArch() == llvm::Triple::x86_64 ||
Daniel Dunbarcc6f8032009-09-09 18:36:27 +00003159 Args.hasArg(options::OPT_force__cpusubtype__ALL))
3160 CmdArgs.push_back("-force_cpusubtype_ALL");
3161
Daniel Dunbar0e2679d2009-08-24 22:26:16 +00003162 if (getToolChain().getTriple().getArch() != llvm::Triple::x86_64 &&
3163 (Args.hasArg(options::OPT_mkernel) ||
Daniel Dunbar8cac5f72009-03-20 16:06:39 +00003164 Args.hasArg(options::OPT_static) ||
Daniel Dunbar0e2679d2009-08-24 22:26:16 +00003165 Args.hasArg(options::OPT_fapple_kext)))
Daniel Dunbarc21c4852009-04-08 23:54:23 +00003166 CmdArgs.push_back("-static");
3167
Daniel Dunbar8cac5f72009-03-20 16:06:39 +00003168 Args.AddAllArgValues(CmdArgs, options::OPT_Wa_COMMA,
3169 options::OPT_Xassembler);
3170
3171 assert(Output.isFilename() && "Unexpected lipo output.");
3172 CmdArgs.push_back("-o");
3173 CmdArgs.push_back(Output.getFilename());
3174
Daniel Dunbar7c1e4652010-08-02 02:38:21 +00003175 assert(Input.isFilename() && "Invalid input.");
3176 CmdArgs.push_back(Input.getFilename());
Daniel Dunbar8cac5f72009-03-20 16:06:39 +00003177
3178 // asm_final spec is empty.
3179
Daniel Dunbarc21c4852009-04-08 23:54:23 +00003180 const char *Exec =
Daniel Dunbar4a7e8892010-07-14 18:46:23 +00003181 Args.MakeArgString(getToolChain().GetProgramPath("as"));
Daniel Dunbar2fe238e2010-08-02 02:38:28 +00003182 C.addCommand(new Command(JA, *this, Exec, CmdArgs));
Daniel Dunbar8cac5f72009-03-20 16:06:39 +00003183}
Daniel Dunbarff7488d2009-03-20 00:52:38 +00003184
Daniel Dunbarfbefe6b2009-09-09 18:36:20 +00003185void darwin::DarwinTool::AddDarwinArch(const ArgList &Args,
3186 ArgStringList &CmdArgs) const {
Chris Lattner5f9e2722011-07-23 10:55:15 +00003187 StringRef ArchName = getDarwinToolChain().getDarwinArchName(Args);
Daniel Dunbareeff4062010-01-22 02:04:58 +00003188
Daniel Dunbar02633b52009-03-26 16:23:12 +00003189 // Derived from darwin_arch spec.
3190 CmdArgs.push_back("-arch");
Daniel Dunbareeff4062010-01-22 02:04:58 +00003191 CmdArgs.push_back(Args.MakeArgString(ArchName));
Daniel Dunbar78dbd582009-09-04 18:35:31 +00003192
Daniel Dunbareeff4062010-01-22 02:04:58 +00003193 // FIXME: Is this needed anymore?
3194 if (ArchName == "arm")
Daniel Dunbar78dbd582009-09-04 18:35:31 +00003195 CmdArgs.push_back("-force_cpusubtype_ALL");
Daniel Dunbar02633b52009-03-26 16:23:12 +00003196}
3197
Daniel Dunbar748de8e2010-09-09 21:51:05 +00003198void darwin::Link::AddLinkArgs(Compilation &C,
3199 const ArgList &Args,
Daniel Dunbar02633b52009-03-26 16:23:12 +00003200 ArgStringList &CmdArgs) const {
Daniel Dunbaree788e72009-12-21 18:54:17 +00003201 const Driver &D = getToolChain().getDriver();
Daniel Dunbarce911f52011-04-28 21:23:41 +00003202 const toolchains::Darwin &DarwinTC = getDarwinToolChain();
Daniel Dunbar02633b52009-03-26 16:23:12 +00003203
Daniel Dunbarb18dc5b2010-08-11 23:07:50 +00003204 unsigned Version[3] = { 0, 0, 0 };
3205 if (Arg *A = Args.getLastArg(options::OPT_mlinker_version_EQ)) {
3206 bool HadExtra;
3207 if (!Driver::GetReleaseVersion(A->getValue(Args), Version[0],
3208 Version[1], Version[2], HadExtra) ||
3209 HadExtra)
Chris Lattner5f9e2722011-07-23 10:55:15 +00003210 D.Diag(diag::err_drv_invalid_version_number)
Daniel Dunbarb18dc5b2010-08-11 23:07:50 +00003211 << A->getAsString(Args);
3212 }
3213
3214 // Newer linkers support -demangle, pass it if supported and not disabled by
3215 // the user.
Daniel Dunbara30d6cd2010-11-19 17:51:40 +00003216 //
3217 // FIXME: We temporarily avoid passing -demangle to any iOS linker, because
3218 // unfortunately we can't be guaranteed that the linker version used there
3219 // will match the linker version detected at configure time. We need the
3220 // universal driver.
3221 if (Version[0] >= 100 && !Args.hasArg(options::OPT_Z_Xlinker__no_demangle) &&
Daniel Dunbarce911f52011-04-28 21:23:41 +00003222 !DarwinTC.isTargetIPhoneOS()) {
Daniel Dunbarbcf1da82010-09-07 17:07:49 +00003223 // Don't pass -demangle to ld_classic.
3224 //
3225 // FIXME: This is a temporary workaround, ld should be handling this.
3226 bool UsesLdClassic = (getToolChain().getArch() == llvm::Triple::x86 &&
3227 Args.hasArg(options::OPT_static));
Daniel Dunbar9ced7042010-09-07 17:50:41 +00003228 if (getToolChain().getArch() == llvm::Triple::x86) {
3229 for (arg_iterator it = Args.filtered_begin(options::OPT_Xlinker,
3230 options::OPT_Wl_COMMA),
3231 ie = Args.filtered_end(); it != ie; ++it) {
3232 const Arg *A = *it;
3233 for (unsigned i = 0, e = A->getNumValues(); i != e; ++i)
Chris Lattner5f9e2722011-07-23 10:55:15 +00003234 if (StringRef(A->getValue(Args, i)) == "-kext")
Daniel Dunbar9ced7042010-09-07 17:50:41 +00003235 UsesLdClassic = true;
3236 }
3237 }
Daniel Dunbarbcf1da82010-09-07 17:07:49 +00003238 if (!UsesLdClassic)
3239 CmdArgs.push_back("-demangle");
Daniel Dunbarb18dc5b2010-08-11 23:07:50 +00003240 }
3241
Daniel Dunbar5bfa6562011-06-21 20:55:11 +00003242 // If we are using LTO, then automatically create a temporary file path for
3243 // the linker to use, so that it's lifetime will extend past a possible
3244 // dsymutil step.
Daniel Dunbara68e1c32011-06-21 21:18:32 +00003245 if (Version[0] >= 116 && D.IsUsingLTO(Args)) {
Daniel Dunbar5bfa6562011-06-21 20:55:11 +00003246 const char *TmpPath = C.getArgs().MakeArgString(
Chad Rosierfe87fc72011-08-26 21:28:44 +00003247 D.GetTemporaryPath("cc", types::getTypeTempSuffix(types::TY_Object)));
Daniel Dunbar5bfa6562011-06-21 20:55:11 +00003248 C.addTempFile(TmpPath);
3249 CmdArgs.push_back("-object_path_lto");
3250 CmdArgs.push_back(TmpPath);
3251 }
3252
Daniel Dunbar02633b52009-03-26 16:23:12 +00003253 // Derived from the "link" spec.
3254 Args.AddAllArgs(CmdArgs, options::OPT_static);
3255 if (!Args.hasArg(options::OPT_static))
3256 CmdArgs.push_back("-dynamic");
3257 if (Args.hasArg(options::OPT_fgnu_runtime)) {
3258 // FIXME: gcc replaces -lobjc in forward args with -lobjc-gnu
3259 // here. How do we wish to handle such things?
3260 }
Daniel Dunbarc21c4852009-04-08 23:54:23 +00003261
Daniel Dunbar02633b52009-03-26 16:23:12 +00003262 if (!Args.hasArg(options::OPT_dynamiclib)) {
Daniel Dunbara6d38492010-01-22 02:04:52 +00003263 AddDarwinArch(Args, CmdArgs);
Daniel Dunbara6d38492010-01-22 02:04:52 +00003264 // FIXME: Why do this only on this path?
Daniel Dunbar8917dd42010-01-22 03:37:33 +00003265 Args.AddLastArg(CmdArgs, options::OPT_force__cpusubtype__ALL);
Daniel Dunbar02633b52009-03-26 16:23:12 +00003266
3267 Args.AddLastArg(CmdArgs, options::OPT_bundle);
3268 Args.AddAllArgs(CmdArgs, options::OPT_bundle__loader);
3269 Args.AddAllArgs(CmdArgs, options::OPT_client__name);
3270
3271 Arg *A;
3272 if ((A = Args.getLastArg(options::OPT_compatibility__version)) ||
3273 (A = Args.getLastArg(options::OPT_current__version)) ||
3274 (A = Args.getLastArg(options::OPT_install__name)))
Chris Lattner5f9e2722011-07-23 10:55:15 +00003275 D.Diag(diag::err_drv_argument_only_allowed_with)
Daniel Dunbar02633b52009-03-26 16:23:12 +00003276 << A->getAsString(Args) << "-dynamiclib";
3277
3278 Args.AddLastArg(CmdArgs, options::OPT_force__flat__namespace);
3279 Args.AddLastArg(CmdArgs, options::OPT_keep__private__externs);
3280 Args.AddLastArg(CmdArgs, options::OPT_private__bundle);
3281 } else {
3282 CmdArgs.push_back("-dylib");
3283
3284 Arg *A;
3285 if ((A = Args.getLastArg(options::OPT_bundle)) ||
3286 (A = Args.getLastArg(options::OPT_bundle__loader)) ||
3287 (A = Args.getLastArg(options::OPT_client__name)) ||
3288 (A = Args.getLastArg(options::OPT_force__flat__namespace)) ||
3289 (A = Args.getLastArg(options::OPT_keep__private__externs)) ||
3290 (A = Args.getLastArg(options::OPT_private__bundle)))
Chris Lattner5f9e2722011-07-23 10:55:15 +00003291 D.Diag(diag::err_drv_argument_not_allowed_with)
Daniel Dunbar02633b52009-03-26 16:23:12 +00003292 << A->getAsString(Args) << "-dynamiclib";
Daniel Dunbarc21c4852009-04-08 23:54:23 +00003293
Daniel Dunbar02633b52009-03-26 16:23:12 +00003294 Args.AddAllArgsTranslated(CmdArgs, options::OPT_compatibility__version,
3295 "-dylib_compatibility_version");
3296 Args.AddAllArgsTranslated(CmdArgs, options::OPT_current__version,
3297 "-dylib_current_version");
3298
Daniel Dunbara6d38492010-01-22 02:04:52 +00003299 AddDarwinArch(Args, CmdArgs);
Daniel Dunbar02633b52009-03-26 16:23:12 +00003300
3301 Args.AddAllArgsTranslated(CmdArgs, options::OPT_install__name,
3302 "-dylib_install_name");
3303 }
3304
3305 Args.AddLastArg(CmdArgs, options::OPT_all__load);
3306 Args.AddAllArgs(CmdArgs, options::OPT_allowable__client);
3307 Args.AddLastArg(CmdArgs, options::OPT_bind__at__load);
Daniel Dunbarce911f52011-04-28 21:23:41 +00003308 if (DarwinTC.isTargetIPhoneOS())
Daniel Dunbard82f8fa2009-09-04 18:35:41 +00003309 Args.AddLastArg(CmdArgs, options::OPT_arch__errors__fatal);
Daniel Dunbar02633b52009-03-26 16:23:12 +00003310 Args.AddLastArg(CmdArgs, options::OPT_dead__strip);
3311 Args.AddLastArg(CmdArgs, options::OPT_no__dead__strip__inits__and__terms);
3312 Args.AddAllArgs(CmdArgs, options::OPT_dylib__file);
3313 Args.AddLastArg(CmdArgs, options::OPT_dynamic);
3314 Args.AddAllArgs(CmdArgs, options::OPT_exported__symbols__list);
3315 Args.AddLastArg(CmdArgs, options::OPT_flat__namespace);
Daniel Dunbar99ca47b2011-06-28 20:16:02 +00003316 Args.AddAllArgs(CmdArgs, options::OPT_force__load);
Daniel Dunbar02633b52009-03-26 16:23:12 +00003317 Args.AddAllArgs(CmdArgs, options::OPT_headerpad__max__install__names);
3318 Args.AddAllArgs(CmdArgs, options::OPT_image__base);
3319 Args.AddAllArgs(CmdArgs, options::OPT_init);
3320
Daniel Dunbarce911f52011-04-28 21:23:41 +00003321 // Add the deployment target.
3322 unsigned TargetVersion[3];
3323 DarwinTC.getTargetVersion(TargetVersion);
Daniel Dunbarb7f5ef72011-04-30 04:22:58 +00003324
3325 // If we had an explicit -mios-simulator-version-min argument, honor that,
3326 // otherwise use the traditional deployment targets. We can't just check the
3327 // is-sim attribute because existing code follows this path, and the linker
3328 // may not handle the argument.
3329 //
3330 // FIXME: We may be able to remove this, once we can verify no one depends on
3331 // it.
3332 if (Args.hasArg(options::OPT_mios_simulator_version_min_EQ))
3333 CmdArgs.push_back("-ios_simulator_version_min");
3334 else if (DarwinTC.isTargetIPhoneOS())
3335 CmdArgs.push_back("-iphoneos_version_min");
3336 else
3337 CmdArgs.push_back("-macosx_version_min");
Chris Lattner5f9e2722011-07-23 10:55:15 +00003338 CmdArgs.push_back(Args.MakeArgString(Twine(TargetVersion[0]) + "." +
3339 Twine(TargetVersion[1]) + "." +
3340 Twine(TargetVersion[2])));
Daniel Dunbarce911f52011-04-28 21:23:41 +00003341
Daniel Dunbar02633b52009-03-26 16:23:12 +00003342 Args.AddLastArg(CmdArgs, options::OPT_nomultidefs);
3343 Args.AddLastArg(CmdArgs, options::OPT_multi__module);
3344 Args.AddLastArg(CmdArgs, options::OPT_single__module);
3345 Args.AddAllArgs(CmdArgs, options::OPT_multiply__defined);
3346 Args.AddAllArgs(CmdArgs, options::OPT_multiply__defined__unused);
Daniel Dunbarc21c4852009-04-08 23:54:23 +00003347
Daniel Dunbar47e879d2010-07-13 23:31:40 +00003348 if (const Arg *A = Args.getLastArg(options::OPT_fpie, options::OPT_fPIE,
3349 options::OPT_fno_pie,
3350 options::OPT_fno_PIE)) {
3351 if (A->getOption().matches(options::OPT_fpie) ||
3352 A->getOption().matches(options::OPT_fPIE))
3353 CmdArgs.push_back("-pie");
3354 else
3355 CmdArgs.push_back("-no_pie");
3356 }
Daniel Dunbar02633b52009-03-26 16:23:12 +00003357
3358 Args.AddLastArg(CmdArgs, options::OPT_prebind);
3359 Args.AddLastArg(CmdArgs, options::OPT_noprebind);
3360 Args.AddLastArg(CmdArgs, options::OPT_nofixprebinding);
3361 Args.AddLastArg(CmdArgs, options::OPT_prebind__all__twolevel__modules);
3362 Args.AddLastArg(CmdArgs, options::OPT_read__only__relocs);
3363 Args.AddAllArgs(CmdArgs, options::OPT_sectcreate);
3364 Args.AddAllArgs(CmdArgs, options::OPT_sectorder);
3365 Args.AddAllArgs(CmdArgs, options::OPT_seg1addr);
3366 Args.AddAllArgs(CmdArgs, options::OPT_segprot);
3367 Args.AddAllArgs(CmdArgs, options::OPT_segaddr);
3368 Args.AddAllArgs(CmdArgs, options::OPT_segs__read__only__addr);
3369 Args.AddAllArgs(CmdArgs, options::OPT_segs__read__write__addr);
3370 Args.AddAllArgs(CmdArgs, options::OPT_seg__addr__table);
3371 Args.AddAllArgs(CmdArgs, options::OPT_seg__addr__table__filename);
3372 Args.AddAllArgs(CmdArgs, options::OPT_sub__library);
3373 Args.AddAllArgs(CmdArgs, options::OPT_sub__umbrella);
Daniel Dunbard82f8fa2009-09-04 18:35:41 +00003374
Daniel Dunbarcc957192011-05-02 21:03:47 +00003375 // Give --sysroot= preference, over the Apple specific behavior to also use
3376 // --isysroot as the syslibroot.
3377 if (const Arg *A = Args.getLastArg(options::OPT__sysroot_EQ)) {
3378 CmdArgs.push_back("-syslibroot");
3379 CmdArgs.push_back(A->getValue(Args));
3380 } else if (const Arg *A = Args.getLastArg(options::OPT_isysroot)) {
3381 CmdArgs.push_back("-syslibroot");
3382 CmdArgs.push_back(A->getValue(Args));
3383 } else if (getDarwinToolChain().isTargetIPhoneOS()) {
3384 CmdArgs.push_back("-syslibroot");
3385 CmdArgs.push_back("/Developer/SDKs/Extra");
Daniel Dunbard82f8fa2009-09-04 18:35:41 +00003386 }
3387
Daniel Dunbar02633b52009-03-26 16:23:12 +00003388 Args.AddLastArg(CmdArgs, options::OPT_twolevel__namespace);
3389 Args.AddLastArg(CmdArgs, options::OPT_twolevel__namespace__hints);
3390 Args.AddAllArgs(CmdArgs, options::OPT_umbrella);
3391 Args.AddAllArgs(CmdArgs, options::OPT_undefined);
3392 Args.AddAllArgs(CmdArgs, options::OPT_unexported__symbols__list);
Daniel Dunbard82f8fa2009-09-04 18:35:41 +00003393 Args.AddAllArgs(CmdArgs, options::OPT_weak__reference__mismatches);
Daniel Dunbar02633b52009-03-26 16:23:12 +00003394 Args.AddLastArg(CmdArgs, options::OPT_X_Flag);
3395 Args.AddAllArgs(CmdArgs, options::OPT_y);
3396 Args.AddLastArg(CmdArgs, options::OPT_w);
3397 Args.AddAllArgs(CmdArgs, options::OPT_pagezero__size);
3398 Args.AddAllArgs(CmdArgs, options::OPT_segs__read__);
3399 Args.AddLastArg(CmdArgs, options::OPT_seglinkedit);
3400 Args.AddLastArg(CmdArgs, options::OPT_noseglinkedit);
3401 Args.AddAllArgs(CmdArgs, options::OPT_sectalign);
3402 Args.AddAllArgs(CmdArgs, options::OPT_sectobjectsymbols);
3403 Args.AddAllArgs(CmdArgs, options::OPT_segcreate);
3404 Args.AddLastArg(CmdArgs, options::OPT_whyload);
3405 Args.AddLastArg(CmdArgs, options::OPT_whatsloaded);
3406 Args.AddAllArgs(CmdArgs, options::OPT_dylinker__install__name);
3407 Args.AddLastArg(CmdArgs, options::OPT_dylinker);
3408 Args.AddLastArg(CmdArgs, options::OPT_Mach);
3409}
3410
3411void darwin::Link::ConstructJob(Compilation &C, const JobAction &JA,
Daniel Dunbar2fe238e2010-08-02 02:38:28 +00003412 const InputInfo &Output,
Daniel Dunbarc21c4852009-04-08 23:54:23 +00003413 const InputInfoList &Inputs,
3414 const ArgList &Args,
Daniel Dunbar02633b52009-03-26 16:23:12 +00003415 const char *LinkingOutput) const {
3416 assert(Output.getType() == types::TY_Image && "Invalid linker output type.");
Daniel Dunbare0be8b12009-09-08 16:39:16 +00003417
Daniel Dunbar02633b52009-03-26 16:23:12 +00003418 // The logic here is derived from gcc's behavior; most of which
3419 // comes from specs (starting with link_command). Consult gcc for
3420 // more information.
Daniel Dunbar02633b52009-03-26 16:23:12 +00003421 ArgStringList CmdArgs;
3422
Argyrios Kyrtzidis22897172011-10-07 22:58:08 +00003423 /// Hack(tm) to ignore linking errors when we are doing ARC migration.
3424 if (Args.hasArg(options::OPT_ccc_arcmt_check,
3425 options::OPT_ccc_arcmt_migrate)) {
3426 for (ArgList::const_iterator I = Args.begin(), E = Args.end(); I != E; ++I)
3427 (*I)->claim();
3428 const char *Exec =
3429 Args.MakeArgString(getToolChain().GetProgramPath("touch"));
3430 CmdArgs.push_back(Output.getFilename());
3431 C.addCommand(new Command(JA, *this, Exec, CmdArgs));
3432 return;
3433 }
3434
Daniel Dunbar02633b52009-03-26 16:23:12 +00003435 // I'm not sure why this particular decomposition exists in gcc, but
3436 // we follow suite for ease of comparison.
Daniel Dunbar748de8e2010-09-09 21:51:05 +00003437 AddLinkArgs(C, Args, CmdArgs);
Daniel Dunbar02633b52009-03-26 16:23:12 +00003438
Daniel Dunbar02633b52009-03-26 16:23:12 +00003439 Args.AddAllArgs(CmdArgs, options::OPT_d_Flag);
3440 Args.AddAllArgs(CmdArgs, options::OPT_s);
3441 Args.AddAllArgs(CmdArgs, options::OPT_t);
3442 Args.AddAllArgs(CmdArgs, options::OPT_Z_Flag);
3443 Args.AddAllArgs(CmdArgs, options::OPT_u_Group);
3444 Args.AddAllArgs(CmdArgs, options::OPT_A);
3445 Args.AddLastArg(CmdArgs, options::OPT_e);
3446 Args.AddAllArgs(CmdArgs, options::OPT_m_Separate);
3447 Args.AddAllArgs(CmdArgs, options::OPT_r);
3448
Daniel Dunbar270073c2010-10-18 22:08:36 +00003449 // Forward -ObjC when either -ObjC or -ObjC++ is used, to force loading
3450 // members of static archive libraries which implement Objective-C classes or
3451 // categories.
3452 if (Args.hasArg(options::OPT_ObjC) || Args.hasArg(options::OPT_ObjCXX))
3453 CmdArgs.push_back("-ObjC");
Michael J. Spencer20249a12010-10-21 03:16:25 +00003454
Daniel Dunbar02633b52009-03-26 16:23:12 +00003455 CmdArgs.push_back("-o");
3456 CmdArgs.push_back(Output.getFilename());
3457
Daniel Dunbar02633b52009-03-26 16:23:12 +00003458 if (!Args.hasArg(options::OPT_A) &&
3459 !Args.hasArg(options::OPT_nostdlib) &&
3460 !Args.hasArg(options::OPT_nostartfiles)) {
3461 // Derived from startfile spec.
3462 if (Args.hasArg(options::OPT_dynamiclib)) {
Daniel Dunbarc21c4852009-04-08 23:54:23 +00003463 // Derived from darwin_dylib1 spec.
Daniel Dunbar1051fc02011-04-01 21:02:42 +00003464 if (getDarwinToolChain().isTargetIOSSimulator()) {
3465 // The simulator doesn't have a versioned crt1 file.
3466 CmdArgs.push_back("-ldylib1.o");
3467 } else if (getDarwinToolChain().isTargetIPhoneOS()) {
Daniel Dunbarcacb0f02010-01-27 00:56:56 +00003468 if (getDarwinToolChain().isIPhoneOSVersionLT(3, 1))
3469 CmdArgs.push_back("-ldylib1.o");
3470 } else {
Daniel Dunbarce3fdf22010-01-27 00:57:03 +00003471 if (getDarwinToolChain().isMacosxVersionLT(10, 5))
Daniel Dunbarcacb0f02010-01-27 00:56:56 +00003472 CmdArgs.push_back("-ldylib1.o");
Daniel Dunbarce3fdf22010-01-27 00:57:03 +00003473 else if (getDarwinToolChain().isMacosxVersionLT(10, 6))
Daniel Dunbarcacb0f02010-01-27 00:56:56 +00003474 CmdArgs.push_back("-ldylib1.10.5.o");
3475 }
Daniel Dunbar02633b52009-03-26 16:23:12 +00003476 } else {
3477 if (Args.hasArg(options::OPT_bundle)) {
Daniel Dunbar8a8d8af2009-04-01 03:17:40 +00003478 if (!Args.hasArg(options::OPT_static)) {
3479 // Derived from darwin_bundle1 spec.
Daniel Dunbar1051fc02011-04-01 21:02:42 +00003480 if (getDarwinToolChain().isTargetIOSSimulator()) {
3481 // The simulator doesn't have a versioned crt1 file.
3482 CmdArgs.push_back("-lbundle1.o");
3483 } else if (getDarwinToolChain().isTargetIPhoneOS()) {
Daniel Dunbarcacb0f02010-01-27 00:56:56 +00003484 if (getDarwinToolChain().isIPhoneOSVersionLT(3, 1))
3485 CmdArgs.push_back("-lbundle1.o");
3486 } else {
Daniel Dunbarce3fdf22010-01-27 00:57:03 +00003487 if (getDarwinToolChain().isMacosxVersionLT(10, 6))
Daniel Dunbarcacb0f02010-01-27 00:56:56 +00003488 CmdArgs.push_back("-lbundle1.o");
3489 }
Daniel Dunbar8a8d8af2009-04-01 03:17:40 +00003490 }
Daniel Dunbar02633b52009-03-26 16:23:12 +00003491 } else {
Daniel Dunbarbbe8e3e2011-03-01 18:49:30 +00003492 if (Args.hasArg(options::OPT_pg) &&
3493 getToolChain().SupportsProfiling()) {
Daniel Dunbar02633b52009-03-26 16:23:12 +00003494 if (Args.hasArg(options::OPT_static) ||
3495 Args.hasArg(options::OPT_object) ||
3496 Args.hasArg(options::OPT_preload)) {
3497 CmdArgs.push_back("-lgcrt0.o");
3498 } else {
3499 CmdArgs.push_back("-lgcrt1.o");
Daniel Dunbarc21c4852009-04-08 23:54:23 +00003500
Daniel Dunbar02633b52009-03-26 16:23:12 +00003501 // darwin_crt2 spec is empty.
Daniel Dunbarc21c4852009-04-08 23:54:23 +00003502 }
Daniel Dunbar02633b52009-03-26 16:23:12 +00003503 } else {
3504 if (Args.hasArg(options::OPT_static) ||
3505 Args.hasArg(options::OPT_object) ||
3506 Args.hasArg(options::OPT_preload)) {
3507 CmdArgs.push_back("-lcrt0.o");
3508 } else {
3509 // Derived from darwin_crt1 spec.
Daniel Dunbar40355802011-03-31 17:12:33 +00003510 if (getDarwinToolChain().isTargetIOSSimulator()) {
3511 // The simulator doesn't have a versioned crt1 file.
3512 CmdArgs.push_back("-lcrt1.o");
3513 } else if (getDarwinToolChain().isTargetIPhoneOS()) {
Daniel Dunbarcacb0f02010-01-27 00:56:56 +00003514 if (getDarwinToolChain().isIPhoneOSVersionLT(3, 1))
3515 CmdArgs.push_back("-lcrt1.o");
3516 else
3517 CmdArgs.push_back("-lcrt1.3.1.o");
Daniel Dunbarce3fdf22010-01-27 00:57:03 +00003518 } else {
3519 if (getDarwinToolChain().isMacosxVersionLT(10, 5))
3520 CmdArgs.push_back("-lcrt1.o");
3521 else if (getDarwinToolChain().isMacosxVersionLT(10, 6))
3522 CmdArgs.push_back("-lcrt1.10.5.o");
3523 else
3524 CmdArgs.push_back("-lcrt1.10.6.o");
Daniel Dunbarc21c4852009-04-08 23:54:23 +00003525
Daniel Dunbarce3fdf22010-01-27 00:57:03 +00003526 // darwin_crt2 spec is empty.
3527 }
Daniel Dunbar02633b52009-03-26 16:23:12 +00003528 }
3529 }
3530 }
3531 }
3532
Daniel Dunbarce3fdf22010-01-27 00:57:03 +00003533 if (!getDarwinToolChain().isTargetIPhoneOS() &&
3534 Args.hasArg(options::OPT_shared_libgcc) &&
3535 getDarwinToolChain().isMacosxVersionLT(10, 5)) {
Daniel Dunbar88137642009-09-09 22:32:48 +00003536 const char *Str =
Daniel Dunbar4a7e8892010-07-14 18:46:23 +00003537 Args.MakeArgString(getToolChain().GetFilePath("crt3.o"));
Daniel Dunbar88137642009-09-09 22:32:48 +00003538 CmdArgs.push_back(Str);
Daniel Dunbar02633b52009-03-26 16:23:12 +00003539 }
3540 }
3541
3542 Args.AddAllArgs(CmdArgs, options::OPT_L);
Daniel Dunbarc21c4852009-04-08 23:54:23 +00003543
Daniel Dunbar02633b52009-03-26 16:23:12 +00003544 if (Args.hasArg(options::OPT_fopenmp))
3545 // This is more complicated in gcc...
3546 CmdArgs.push_back("-lgomp");
3547
Daniel Dunbar6b200b22009-09-18 08:14:36 +00003548 getDarwinToolChain().AddLinkSearchPathArgs(Args, CmdArgs);
Daniel Dunbarc21c4852009-04-08 23:54:23 +00003549
John McCallf85e1932011-06-15 23:02:42 +00003550 // In ARC, if we don't have runtime support, link in the runtime
3551 // stubs. We have to do this *before* adding any of the normal
3552 // linker inputs so that its initializer gets run first.
John McCall9f084a32011-07-06 00:26:06 +00003553 if (isObjCAutoRefCount(Args)) {
3554 ObjCRuntime runtime;
3555 getDarwinToolChain().configureObjCRuntime(runtime);
3556 if (!runtime.HasARC)
3557 getDarwinToolChain().AddLinkARCArgs(Args, CmdArgs);
3558 }
John McCallf85e1932011-06-15 23:02:42 +00003559
Daniel Dunbar2008fee2010-09-17 00:24:54 +00003560 AddLinkerInputs(getToolChain(), Inputs, Args, CmdArgs);
Daniel Dunbar02633b52009-03-26 16:23:12 +00003561
3562 if (LinkingOutput) {
3563 CmdArgs.push_back("-arch_multiple");
3564 CmdArgs.push_back("-final_output");
3565 CmdArgs.push_back(LinkingOutput);
3566 }
3567
Daniel Dunbar02633b52009-03-26 16:23:12 +00003568 if (Args.hasArg(options::OPT_fnested_functions))
3569 CmdArgs.push_back("-allow_stack_execute");
Daniel Dunbarc21c4852009-04-08 23:54:23 +00003570
Daniel Dunbar02633b52009-03-26 16:23:12 +00003571 if (!Args.hasArg(options::OPT_nostdlib) &&
3572 !Args.hasArg(options::OPT_nodefaultlibs)) {
Daniel Dunbaree788e72009-12-21 18:54:17 +00003573 if (getToolChain().getDriver().CCCIsCXX)
Daniel Dunbar132e35d2010-09-17 01:20:05 +00003574 getToolChain().AddCXXStdlibLibArgs(Args, CmdArgs);
Daniel Dunbaredfa02b2009-04-08 06:06:21 +00003575
Daniel Dunbar02633b52009-03-26 16:23:12 +00003576 // link_ssp spec is empty.
3577
Daniel Dunbar6cd41542009-09-18 08:15:03 +00003578 // Let the tool chain choose which runtime library to link.
3579 getDarwinToolChain().AddLinkRuntimeLibArgs(Args, CmdArgs);
Daniel Dunbar02633b52009-03-26 16:23:12 +00003580 }
3581
3582 if (!Args.hasArg(options::OPT_A) &&
3583 !Args.hasArg(options::OPT_nostdlib) &&
3584 !Args.hasArg(options::OPT_nostartfiles)) {
3585 // endfile_spec is empty.
3586 }
3587
Bill Wendling3f4be6f2011-06-27 19:15:03 +00003588 addProfileRT(getToolChain(), Args, CmdArgs, getToolChain().getTriple());
Nick Lewycky2e95a6d2011-05-24 21:54:59 +00003589
Daniel Dunbar02633b52009-03-26 16:23:12 +00003590 Args.AddAllArgs(CmdArgs, options::OPT_T_Group);
3591 Args.AddAllArgs(CmdArgs, options::OPT_F);
3592
Daniel Dunbarc21c4852009-04-08 23:54:23 +00003593 const char *Exec =
Daniel Dunbar4a7e8892010-07-14 18:46:23 +00003594 Args.MakeArgString(getToolChain().GetProgramPath("ld"));
Daniel Dunbar2fe238e2010-08-02 02:38:28 +00003595 C.addCommand(new Command(JA, *this, Exec, CmdArgs));
Daniel Dunbar02633b52009-03-26 16:23:12 +00003596}
3597
Daniel Dunbarff7488d2009-03-20 00:52:38 +00003598void darwin::Lipo::ConstructJob(Compilation &C, const JobAction &JA,
Daniel Dunbar2fe238e2010-08-02 02:38:28 +00003599 const InputInfo &Output,
Daniel Dunbarc21c4852009-04-08 23:54:23 +00003600 const InputInfoList &Inputs,
3601 const ArgList &Args,
Daniel Dunbarff7488d2009-03-20 00:52:38 +00003602 const char *LinkingOutput) const {
3603 ArgStringList CmdArgs;
3604
3605 CmdArgs.push_back("-create");
3606 assert(Output.isFilename() && "Unexpected lipo output.");
Daniel Dunbara428df82009-03-24 00:24:37 +00003607
3608 CmdArgs.push_back("-output");
Daniel Dunbarff7488d2009-03-20 00:52:38 +00003609 CmdArgs.push_back(Output.getFilename());
Daniel Dunbara428df82009-03-24 00:24:37 +00003610
Daniel Dunbarff7488d2009-03-20 00:52:38 +00003611 for (InputInfoList::const_iterator
3612 it = Inputs.begin(), ie = Inputs.end(); it != ie; ++it) {
3613 const InputInfo &II = *it;
3614 assert(II.isFilename() && "Unexpected lipo input.");
3615 CmdArgs.push_back(II.getFilename());
3616 }
Daniel Dunbarc21c4852009-04-08 23:54:23 +00003617 const char *Exec =
Daniel Dunbar4a7e8892010-07-14 18:46:23 +00003618 Args.MakeArgString(getToolChain().GetProgramPath("lipo"));
Daniel Dunbar2fe238e2010-08-02 02:38:28 +00003619 C.addCommand(new Command(JA, *this, Exec, CmdArgs));
Daniel Dunbarff7488d2009-03-20 00:52:38 +00003620}
Daniel Dunbar68a31d42009-03-31 17:45:15 +00003621
Daniel Dunbar6e0f2542010-06-04 18:28:36 +00003622void darwin::Dsymutil::ConstructJob(Compilation &C, const JobAction &JA,
Daniel Dunbar2fe238e2010-08-02 02:38:28 +00003623 const InputInfo &Output,
Daniel Dunbar6e0f2542010-06-04 18:28:36 +00003624 const InputInfoList &Inputs,
3625 const ArgList &Args,
3626 const char *LinkingOutput) const {
3627 ArgStringList CmdArgs;
3628
Daniel Dunbar03e92302011-05-09 17:23:16 +00003629 CmdArgs.push_back("-o");
3630 CmdArgs.push_back(Output.getFilename());
3631
Daniel Dunbar6e0f2542010-06-04 18:28:36 +00003632 assert(Inputs.size() == 1 && "Unable to handle multiple inputs.");
3633 const InputInfo &Input = Inputs[0];
3634 assert(Input.isFilename() && "Unexpected dsymutil input.");
3635 CmdArgs.push_back(Input.getFilename());
3636
Daniel Dunbar6e0f2542010-06-04 18:28:36 +00003637 const char *Exec =
Daniel Dunbar4a7e8892010-07-14 18:46:23 +00003638 Args.MakeArgString(getToolChain().GetProgramPath("dsymutil"));
Daniel Dunbar2fe238e2010-08-02 02:38:28 +00003639 C.addCommand(new Command(JA, *this, Exec, CmdArgs));
Daniel Dunbar6e0f2542010-06-04 18:28:36 +00003640}
3641
Eric Christopherf8571862011-08-23 17:56:55 +00003642void darwin::VerifyDebug::ConstructJob(Compilation &C, const JobAction &JA,
3643 const InputInfo &Output,
3644 const InputInfoList &Inputs,
3645 const ArgList &Args,
3646 const char *LinkingOutput) const {
3647 ArgStringList CmdArgs;
3648 CmdArgs.push_back("--verify");
3649
3650 assert(Inputs.size() == 1 && "Unable to handle multiple inputs.");
3651 const InputInfo &Input = Inputs[0];
3652 assert(Input.isFilename() && "Unexpected verify input");
3653
3654 // Grabbing the output of the earlier dsymutil run.
3655 CmdArgs.push_back(Input.getFilename());
3656
3657 const char *Exec =
3658 Args.MakeArgString(getToolChain().GetProgramPath("dwarfdump"));
3659 C.addCommand(new Command(JA, *this, Exec, CmdArgs));
3660}
3661
Edward O'Callaghane7925a02009-08-22 01:06:46 +00003662void auroraux::Assemble::ConstructJob(Compilation &C, const JobAction &JA,
Daniel Dunbar2fe238e2010-08-02 02:38:28 +00003663 const InputInfo &Output,
Daniel Dunbar294691e2009-11-04 06:24:38 +00003664 const InputInfoList &Inputs,
3665 const ArgList &Args,
3666 const char *LinkingOutput) const {
Edward O'Callaghane7925a02009-08-22 01:06:46 +00003667 ArgStringList CmdArgs;
3668
3669 Args.AddAllArgValues(CmdArgs, options::OPT_Wa_COMMA,
3670 options::OPT_Xassembler);
3671
3672 CmdArgs.push_back("-o");
Daniel Dunbar7c1e4652010-08-02 02:38:21 +00003673 CmdArgs.push_back(Output.getFilename());
Edward O'Callaghane7925a02009-08-22 01:06:46 +00003674
3675 for (InputInfoList::const_iterator
3676 it = Inputs.begin(), ie = Inputs.end(); it != ie; ++it) {
3677 const InputInfo &II = *it;
Daniel Dunbar7c1e4652010-08-02 02:38:21 +00003678 CmdArgs.push_back(II.getFilename());
Edward O'Callaghane7925a02009-08-22 01:06:46 +00003679 }
3680
3681 const char *Exec =
Daniel Dunbar4a7e8892010-07-14 18:46:23 +00003682 Args.MakeArgString(getToolChain().GetProgramPath("gas"));
Daniel Dunbar2fe238e2010-08-02 02:38:28 +00003683 C.addCommand(new Command(JA, *this, Exec, CmdArgs));
Edward O'Callaghane7925a02009-08-22 01:06:46 +00003684}
3685
3686void auroraux::Link::ConstructJob(Compilation &C, const JobAction &JA,
Daniel Dunbar2fe238e2010-08-02 02:38:28 +00003687 const InputInfo &Output,
Daniel Dunbar294691e2009-11-04 06:24:38 +00003688 const InputInfoList &Inputs,
3689 const ArgList &Args,
3690 const char *LinkingOutput) const {
Edward O'Callaghane7925a02009-08-22 01:06:46 +00003691 ArgStringList CmdArgs;
3692
3693 if ((!Args.hasArg(options::OPT_nostdlib)) &&
Daniel Dunbar294691e2009-11-04 06:24:38 +00003694 (!Args.hasArg(options::OPT_shared))) {
Edward O'Callaghane7925a02009-08-22 01:06:46 +00003695 CmdArgs.push_back("-e");
Edward O'Callaghan7adf9492009-10-15 07:44:07 +00003696 CmdArgs.push_back("_start");
Edward O'Callaghane7925a02009-08-22 01:06:46 +00003697 }
3698
3699 if (Args.hasArg(options::OPT_static)) {
3700 CmdArgs.push_back("-Bstatic");
Edward O'Callaghan7adf9492009-10-15 07:44:07 +00003701 CmdArgs.push_back("-dn");
Edward O'Callaghane7925a02009-08-22 01:06:46 +00003702 } else {
Edward O'Callaghan7adf9492009-10-15 07:44:07 +00003703// CmdArgs.push_back("--eh-frame-hdr");
Edward O'Callaghane7925a02009-08-22 01:06:46 +00003704 CmdArgs.push_back("-Bdynamic");
3705 if (Args.hasArg(options::OPT_shared)) {
3706 CmdArgs.push_back("-shared");
3707 } else {
Edward O'Callaghan3cecc192009-10-16 19:44:18 +00003708 CmdArgs.push_back("--dynamic-linker");
Edward O'Callaghane7925a02009-08-22 01:06:46 +00003709 CmdArgs.push_back("/lib/ld.so.1"); // 64Bit Path /lib/amd64/ld.so.1
3710 }
3711 }
3712
Daniel Dunbar7c1e4652010-08-02 02:38:21 +00003713 if (Output.isFilename()) {
Edward O'Callaghane7925a02009-08-22 01:06:46 +00003714 CmdArgs.push_back("-o");
3715 CmdArgs.push_back(Output.getFilename());
3716 } else {
3717 assert(Output.isNothing() && "Invalid output.");
3718 }
3719
3720 if (!Args.hasArg(options::OPT_nostdlib) &&
3721 !Args.hasArg(options::OPT_nostartfiles)) {
3722 if (!Args.hasArg(options::OPT_shared)) {
Chris Lattner38e317d2010-07-07 16:01:42 +00003723 CmdArgs.push_back(Args.MakeArgString(
Daniel Dunbar4a7e8892010-07-14 18:46:23 +00003724 getToolChain().GetFilePath("crt1.o")));
Chris Lattner38e317d2010-07-07 16:01:42 +00003725 CmdArgs.push_back(Args.MakeArgString(
Daniel Dunbar4a7e8892010-07-14 18:46:23 +00003726 getToolChain().GetFilePath("crti.o")));
Chris Lattner38e317d2010-07-07 16:01:42 +00003727 CmdArgs.push_back(Args.MakeArgString(
Daniel Dunbar4a7e8892010-07-14 18:46:23 +00003728 getToolChain().GetFilePath("crtbegin.o")));
Edward O'Callaghane7925a02009-08-22 01:06:46 +00003729 } else {
Chris Lattner38e317d2010-07-07 16:01:42 +00003730 CmdArgs.push_back(Args.MakeArgString(
Daniel Dunbar4a7e8892010-07-14 18:46:23 +00003731 getToolChain().GetFilePath("crti.o")));
Edward O'Callaghane7925a02009-08-22 01:06:46 +00003732 }
Chris Lattner38e317d2010-07-07 16:01:42 +00003733 CmdArgs.push_back(Args.MakeArgString(
Daniel Dunbar4a7e8892010-07-14 18:46:23 +00003734 getToolChain().GetFilePath("crtn.o")));
Edward O'Callaghane7925a02009-08-22 01:06:46 +00003735 }
3736
Daniel Dunbar294691e2009-11-04 06:24:38 +00003737 CmdArgs.push_back(Args.MakeArgString("-L/opt/gcc4/lib/gcc/"
3738 + getToolChain().getTripleString()
Daniel Dunbarf7fb31f2009-10-29 02:24:37 +00003739 + "/4.2.4"));
Edward O'Callaghane7925a02009-08-22 01:06:46 +00003740
3741 Args.AddAllArgs(CmdArgs, options::OPT_L);
3742 Args.AddAllArgs(CmdArgs, options::OPT_T_Group);
3743 Args.AddAllArgs(CmdArgs, options::OPT_e);
3744
Daniel Dunbar2008fee2010-09-17 00:24:54 +00003745 AddLinkerInputs(getToolChain(), Inputs, Args, CmdArgs);
Edward O'Callaghane7925a02009-08-22 01:06:46 +00003746
3747 if (!Args.hasArg(options::OPT_nostdlib) &&
3748 !Args.hasArg(options::OPT_nodefaultlibs)) {
3749 // FIXME: For some reason GCC passes -lgcc before adding
3750 // the default system libraries. Just mimic this for now.
3751 CmdArgs.push_back("-lgcc");
3752
3753 if (Args.hasArg(options::OPT_pthread))
3754 CmdArgs.push_back("-pthread");
3755 if (!Args.hasArg(options::OPT_shared))
3756 CmdArgs.push_back("-lc");
3757 CmdArgs.push_back("-lgcc");
3758 }
3759
3760 if (!Args.hasArg(options::OPT_nostdlib) &&
3761 !Args.hasArg(options::OPT_nostartfiles)) {
3762 if (!Args.hasArg(options::OPT_shared))
Chris Lattner38e317d2010-07-07 16:01:42 +00003763 CmdArgs.push_back(Args.MakeArgString(
Daniel Dunbar4a7e8892010-07-14 18:46:23 +00003764 getToolChain().GetFilePath("crtend.o")));
Edward O'Callaghane7925a02009-08-22 01:06:46 +00003765 }
3766
Bill Wendling3f4be6f2011-06-27 19:15:03 +00003767 addProfileRT(getToolChain(), Args, CmdArgs, getToolChain().getTriple());
Nick Lewycky2e95a6d2011-05-24 21:54:59 +00003768
Edward O'Callaghane7925a02009-08-22 01:06:46 +00003769 const char *Exec =
Daniel Dunbar4a7e8892010-07-14 18:46:23 +00003770 Args.MakeArgString(getToolChain().GetProgramPath("ld"));
Daniel Dunbar2fe238e2010-08-02 02:38:28 +00003771 C.addCommand(new Command(JA, *this, Exec, CmdArgs));
Edward O'Callaghane7925a02009-08-22 01:06:46 +00003772}
3773
Daniel Dunbarf7b8eec2009-06-29 20:52:51 +00003774void openbsd::Assemble::ConstructJob(Compilation &C, const JobAction &JA,
Daniel Dunbar2fe238e2010-08-02 02:38:28 +00003775 const InputInfo &Output,
Daniel Dunbarf7b8eec2009-06-29 20:52:51 +00003776 const InputInfoList &Inputs,
3777 const ArgList &Args,
Mike Stump1eb44332009-09-09 15:08:12 +00003778 const char *LinkingOutput) const {
Daniel Dunbarf7b8eec2009-06-29 20:52:51 +00003779 ArgStringList CmdArgs;
3780
3781 Args.AddAllArgValues(CmdArgs, options::OPT_Wa_COMMA,
3782 options::OPT_Xassembler);
3783
3784 CmdArgs.push_back("-o");
Daniel Dunbar7c1e4652010-08-02 02:38:21 +00003785 CmdArgs.push_back(Output.getFilename());
Daniel Dunbarf7b8eec2009-06-29 20:52:51 +00003786
3787 for (InputInfoList::const_iterator
3788 it = Inputs.begin(), ie = Inputs.end(); it != ie; ++it) {
3789 const InputInfo &II = *it;
Daniel Dunbar7c1e4652010-08-02 02:38:21 +00003790 CmdArgs.push_back(II.getFilename());
Daniel Dunbarf7b8eec2009-06-29 20:52:51 +00003791 }
3792
3793 const char *Exec =
Daniel Dunbar4a7e8892010-07-14 18:46:23 +00003794 Args.MakeArgString(getToolChain().GetProgramPath("as"));
Daniel Dunbar2fe238e2010-08-02 02:38:28 +00003795 C.addCommand(new Command(JA, *this, Exec, CmdArgs));
Daniel Dunbarf7b8eec2009-06-29 20:52:51 +00003796}
3797
3798void openbsd::Link::ConstructJob(Compilation &C, const JobAction &JA,
Daniel Dunbar2fe238e2010-08-02 02:38:28 +00003799 const InputInfo &Output,
Daniel Dunbarf7b8eec2009-06-29 20:52:51 +00003800 const InputInfoList &Inputs,
3801 const ArgList &Args,
3802 const char *LinkingOutput) const {
Daniel Dunbaree788e72009-12-21 18:54:17 +00003803 const Driver &D = getToolChain().getDriver();
Daniel Dunbarf7b8eec2009-06-29 20:52:51 +00003804 ArgStringList CmdArgs;
3805
Daniel Dunbar2bbcf662009-08-03 01:28:59 +00003806 if ((!Args.hasArg(options::OPT_nostdlib)) &&
Daniel Dunbar294691e2009-11-04 06:24:38 +00003807 (!Args.hasArg(options::OPT_shared))) {
Daniel Dunbar2bbcf662009-08-03 01:28:59 +00003808 CmdArgs.push_back("-e");
3809 CmdArgs.push_back("__start");
3810 }
3811
Daniel Dunbarf7b8eec2009-06-29 20:52:51 +00003812 if (Args.hasArg(options::OPT_static)) {
3813 CmdArgs.push_back("-Bstatic");
3814 } else {
Rafael Espindola65ba55d2010-11-11 02:17:51 +00003815 if (Args.hasArg(options::OPT_rdynamic))
3816 CmdArgs.push_back("-export-dynamic");
Daniel Dunbarf7b8eec2009-06-29 20:52:51 +00003817 CmdArgs.push_back("--eh-frame-hdr");
Daniel Dunbar2bbcf662009-08-03 01:28:59 +00003818 CmdArgs.push_back("-Bdynamic");
Daniel Dunbarf7b8eec2009-06-29 20:52:51 +00003819 if (Args.hasArg(options::OPT_shared)) {
Daniel Dunbar2bbcf662009-08-03 01:28:59 +00003820 CmdArgs.push_back("-shared");
Daniel Dunbarf7b8eec2009-06-29 20:52:51 +00003821 } else {
3822 CmdArgs.push_back("-dynamic-linker");
3823 CmdArgs.push_back("/usr/libexec/ld.so");
3824 }
3825 }
3826
Daniel Dunbar7c1e4652010-08-02 02:38:21 +00003827 if (Output.isFilename()) {
Daniel Dunbarf7b8eec2009-06-29 20:52:51 +00003828 CmdArgs.push_back("-o");
3829 CmdArgs.push_back(Output.getFilename());
3830 } else {
3831 assert(Output.isNothing() && "Invalid output.");
3832 }
3833
3834 if (!Args.hasArg(options::OPT_nostdlib) &&
3835 !Args.hasArg(options::OPT_nostartfiles)) {
3836 if (!Args.hasArg(options::OPT_shared)) {
Chris Lattner38e317d2010-07-07 16:01:42 +00003837 CmdArgs.push_back(Args.MakeArgString(
Daniel Dunbar4a7e8892010-07-14 18:46:23 +00003838 getToolChain().GetFilePath("crt0.o")));
Chris Lattner38e317d2010-07-07 16:01:42 +00003839 CmdArgs.push_back(Args.MakeArgString(
Daniel Dunbar4a7e8892010-07-14 18:46:23 +00003840 getToolChain().GetFilePath("crtbegin.o")));
Daniel Dunbarf7b8eec2009-06-29 20:52:51 +00003841 } else {
Chris Lattner38e317d2010-07-07 16:01:42 +00003842 CmdArgs.push_back(Args.MakeArgString(
Daniel Dunbar4a7e8892010-07-14 18:46:23 +00003843 getToolChain().GetFilePath("crtbeginS.o")));
Daniel Dunbarf7b8eec2009-06-29 20:52:51 +00003844 }
3845 }
3846
Edward O'Callaghane7e18202009-10-28 15:13:08 +00003847 std::string Triple = getToolChain().getTripleString();
3848 if (Triple.substr(0, 6) == "x86_64")
Daniel Dunbar294691e2009-11-04 06:24:38 +00003849 Triple.replace(0, 6, "amd64");
Daniel Dunbarf7fb31f2009-10-29 02:24:37 +00003850 CmdArgs.push_back(Args.MakeArgString("-L/usr/lib/gcc-lib/" + Triple +
Daniel Dunbar95c04572010-08-01 23:13:54 +00003851 "/4.2.1"));
Daniel Dunbar2bbcf662009-08-03 01:28:59 +00003852
Daniel Dunbarf7b8eec2009-06-29 20:52:51 +00003853 Args.AddAllArgs(CmdArgs, options::OPT_L);
3854 Args.AddAllArgs(CmdArgs, options::OPT_T_Group);
3855 Args.AddAllArgs(CmdArgs, options::OPT_e);
3856
Daniel Dunbar2008fee2010-09-17 00:24:54 +00003857 AddLinkerInputs(getToolChain(), Inputs, Args, CmdArgs);
Daniel Dunbarf7b8eec2009-06-29 20:52:51 +00003858
3859 if (!Args.hasArg(options::OPT_nostdlib) &&
3860 !Args.hasArg(options::OPT_nodefaultlibs)) {
Daniel Dunbar95c04572010-08-01 23:13:54 +00003861 if (D.CCCIsCXX) {
Daniel Dunbar132e35d2010-09-17 01:20:05 +00003862 getToolChain().AddCXXStdlibLibArgs(Args, CmdArgs);
Daniel Dunbar95c04572010-08-01 23:13:54 +00003863 CmdArgs.push_back("-lm");
3864 }
3865
Daniel Dunbar2bbcf662009-08-03 01:28:59 +00003866 // FIXME: For some reason GCC passes -lgcc before adding
3867 // the default system libraries. Just mimic this for now.
3868 CmdArgs.push_back("-lgcc");
Daniel Dunbarf7b8eec2009-06-29 20:52:51 +00003869
3870 if (Args.hasArg(options::OPT_pthread))
Chris Lattnera2e40212011-02-21 18:36:51 +00003871 CmdArgs.push_back("-lpthread");
Daniel Dunbar2bbcf662009-08-03 01:28:59 +00003872 if (!Args.hasArg(options::OPT_shared))
3873 CmdArgs.push_back("-lc");
3874 CmdArgs.push_back("-lgcc");
Daniel Dunbarf7b8eec2009-06-29 20:52:51 +00003875 }
3876
3877 if (!Args.hasArg(options::OPT_nostdlib) &&
3878 !Args.hasArg(options::OPT_nostartfiles)) {
3879 if (!Args.hasArg(options::OPT_shared))
Chris Lattner38e317d2010-07-07 16:01:42 +00003880 CmdArgs.push_back(Args.MakeArgString(
Daniel Dunbar4a7e8892010-07-14 18:46:23 +00003881 getToolChain().GetFilePath("crtend.o")));
Daniel Dunbarf7b8eec2009-06-29 20:52:51 +00003882 else
Chris Lattner38e317d2010-07-07 16:01:42 +00003883 CmdArgs.push_back(Args.MakeArgString(
Daniel Dunbar4a7e8892010-07-14 18:46:23 +00003884 getToolChain().GetFilePath("crtendS.o")));
Daniel Dunbarf7b8eec2009-06-29 20:52:51 +00003885 }
3886
3887 const char *Exec =
Daniel Dunbar4a7e8892010-07-14 18:46:23 +00003888 Args.MakeArgString(getToolChain().GetProgramPath("ld"));
Daniel Dunbar2fe238e2010-08-02 02:38:28 +00003889 C.addCommand(new Command(JA, *this, Exec, CmdArgs));
Daniel Dunbarf7b8eec2009-06-29 20:52:51 +00003890}
Ed Schoutenc66a5a32009-04-02 19:13:12 +00003891
Daniel Dunbar68a31d42009-03-31 17:45:15 +00003892void freebsd::Assemble::ConstructJob(Compilation &C, const JobAction &JA,
Daniel Dunbar2fe238e2010-08-02 02:38:28 +00003893 const InputInfo &Output,
Daniel Dunbarc21c4852009-04-08 23:54:23 +00003894 const InputInfoList &Inputs,
3895 const ArgList &Args,
Mike Stump1eb44332009-09-09 15:08:12 +00003896 const char *LinkingOutput) const {
Daniel Dunbar68a31d42009-03-31 17:45:15 +00003897 ArgStringList CmdArgs;
3898
Daniel Dunbar008f54a2009-04-01 19:36:32 +00003899 // When building 32-bit code on FreeBSD/amd64, we have to explicitly
3900 // instruct as in the base system to assemble 32-bit code.
3901 if (getToolChain().getArchName() == "i386")
Daniel Dunbar68a31d42009-03-31 17:45:15 +00003902 CmdArgs.push_back("--32");
3903
Roman Divacky3393cef2011-06-04 07:37:31 +00003904 if (getToolChain().getArchName() == "powerpc")
3905 CmdArgs.push_back("-a32");
Michael J. Spencer20249a12010-10-21 03:16:25 +00003906
Eric Christophered734732010-03-02 02:41:08 +00003907 // Set byte order explicitly
3908 if (getToolChain().getArchName() == "mips")
3909 CmdArgs.push_back("-EB");
3910 else if (getToolChain().getArchName() == "mipsel")
3911 CmdArgs.push_back("-EL");
3912
Daniel Dunbar68a31d42009-03-31 17:45:15 +00003913 Args.AddAllArgValues(CmdArgs, options::OPT_Wa_COMMA,
3914 options::OPT_Xassembler);
3915
3916 CmdArgs.push_back("-o");
Daniel Dunbar7c1e4652010-08-02 02:38:21 +00003917 CmdArgs.push_back(Output.getFilename());
Daniel Dunbar68a31d42009-03-31 17:45:15 +00003918
3919 for (InputInfoList::const_iterator
3920 it = Inputs.begin(), ie = Inputs.end(); it != ie; ++it) {
3921 const InputInfo &II = *it;
Daniel Dunbar7c1e4652010-08-02 02:38:21 +00003922 CmdArgs.push_back(II.getFilename());
Daniel Dunbar68a31d42009-03-31 17:45:15 +00003923 }
3924
Daniel Dunbarc21c4852009-04-08 23:54:23 +00003925 const char *Exec =
Daniel Dunbar4a7e8892010-07-14 18:46:23 +00003926 Args.MakeArgString(getToolChain().GetProgramPath("as"));
Daniel Dunbar2fe238e2010-08-02 02:38:28 +00003927 C.addCommand(new Command(JA, *this, Exec, CmdArgs));
Daniel Dunbar68a31d42009-03-31 17:45:15 +00003928}
Daniel Dunbar008f54a2009-04-01 19:36:32 +00003929
3930void freebsd::Link::ConstructJob(Compilation &C, const JobAction &JA,
Daniel Dunbar2fe238e2010-08-02 02:38:28 +00003931 const InputInfo &Output,
Daniel Dunbarc21c4852009-04-08 23:54:23 +00003932 const InputInfoList &Inputs,
3933 const ArgList &Args,
Daniel Dunbara8304f62009-05-02 20:14:53 +00003934 const char *LinkingOutput) const {
Daniel Dunbaree788e72009-12-21 18:54:17 +00003935 const Driver &D = getToolChain().getDriver();
Daniel Dunbar008f54a2009-04-01 19:36:32 +00003936 ArgStringList CmdArgs;
3937
Joerg Sonnenberger8ab2bdc2011-03-21 13:51:29 +00003938 if (!D.SysRoot.empty())
3939 CmdArgs.push_back(Args.MakeArgString("--sysroot=" + D.SysRoot));
3940
Daniel Dunbar008f54a2009-04-01 19:36:32 +00003941 if (Args.hasArg(options::OPT_static)) {
3942 CmdArgs.push_back("-Bstatic");
3943 } else {
Rafael Espindola65ba55d2010-11-11 02:17:51 +00003944 if (Args.hasArg(options::OPT_rdynamic))
3945 CmdArgs.push_back("-export-dynamic");
Daniel Dunbar008f54a2009-04-01 19:36:32 +00003946 CmdArgs.push_back("--eh-frame-hdr");
3947 if (Args.hasArg(options::OPT_shared)) {
3948 CmdArgs.push_back("-Bshareable");
3949 } else {
3950 CmdArgs.push_back("-dynamic-linker");
3951 CmdArgs.push_back("/libexec/ld-elf.so.1");
3952 }
3953 }
3954
3955 // When building 32-bit code on FreeBSD/amd64, we have to explicitly
3956 // instruct ld in the base system to link 32-bit code.
3957 if (getToolChain().getArchName() == "i386") {
3958 CmdArgs.push_back("-m");
3959 CmdArgs.push_back("elf_i386_fbsd");
3960 }
3961
Roman Divacky000a6552011-06-04 07:40:24 +00003962 if (getToolChain().getArchName() == "powerpc") {
3963 CmdArgs.push_back("-m");
3964 CmdArgs.push_back("elf32ppc");
3965 }
3966
Daniel Dunbar7c1e4652010-08-02 02:38:21 +00003967 if (Output.isFilename()) {
Daniel Dunbar008f54a2009-04-01 19:36:32 +00003968 CmdArgs.push_back("-o");
3969 CmdArgs.push_back(Output.getFilename());
3970 } else {
3971 assert(Output.isNothing() && "Invalid output.");
3972 }
3973
3974 if (!Args.hasArg(options::OPT_nostdlib) &&
3975 !Args.hasArg(options::OPT_nostartfiles)) {
3976 if (!Args.hasArg(options::OPT_shared)) {
Roman Divackyc16bb762011-02-10 16:59:40 +00003977 if (Args.hasArg(options::OPT_pg))
3978 CmdArgs.push_back(Args.MakeArgString(
3979 getToolChain().GetFilePath("gcrt1.o")));
Roman Divacky8642f212011-10-10 19:47:45 +00003980 else {
3981 const char *crt = Args.hasArg(options::OPT_pie) ? "Scrt1.o" : "crt1.o";
Roman Divackyc16bb762011-02-10 16:59:40 +00003982 CmdArgs.push_back(Args.MakeArgString(
Roman Divacky8642f212011-10-10 19:47:45 +00003983 getToolChain().GetFilePath(crt)));
3984 }
Chris Lattner38e317d2010-07-07 16:01:42 +00003985 CmdArgs.push_back(Args.MakeArgString(
Daniel Dunbar4a7e8892010-07-14 18:46:23 +00003986 getToolChain().GetFilePath("crti.o")));
Chris Lattner38e317d2010-07-07 16:01:42 +00003987 CmdArgs.push_back(Args.MakeArgString(
Daniel Dunbar4a7e8892010-07-14 18:46:23 +00003988 getToolChain().GetFilePath("crtbegin.o")));
Daniel Dunbar008f54a2009-04-01 19:36:32 +00003989 } else {
Chris Lattner38e317d2010-07-07 16:01:42 +00003990 CmdArgs.push_back(Args.MakeArgString(
Daniel Dunbar4a7e8892010-07-14 18:46:23 +00003991 getToolChain().GetFilePath("crti.o")));
Chris Lattner38e317d2010-07-07 16:01:42 +00003992 CmdArgs.push_back(Args.MakeArgString(
Daniel Dunbar4a7e8892010-07-14 18:46:23 +00003993 getToolChain().GetFilePath("crtbeginS.o")));
Daniel Dunbar008f54a2009-04-01 19:36:32 +00003994 }
3995 }
3996
3997 Args.AddAllArgs(CmdArgs, options::OPT_L);
Roman Divacky58e5ac92011-03-01 17:53:14 +00003998 const ToolChain::path_list Paths = getToolChain().getFilePaths();
3999 for (ToolChain::path_list::const_iterator i = Paths.begin(), e = Paths.end();
4000 i != e; ++i)
Chris Lattner5f9e2722011-07-23 10:55:15 +00004001 CmdArgs.push_back(Args.MakeArgString(StringRef("-L") + *i));
Daniel Dunbar008f54a2009-04-01 19:36:32 +00004002 Args.AddAllArgs(CmdArgs, options::OPT_T_Group);
4003 Args.AddAllArgs(CmdArgs, options::OPT_e);
David Chisnallc7363772010-08-15 22:58:12 +00004004 Args.AddAllArgs(CmdArgs, options::OPT_s);
4005 Args.AddAllArgs(CmdArgs, options::OPT_t);
4006 Args.AddAllArgs(CmdArgs, options::OPT_Z_Flag);
4007 Args.AddAllArgs(CmdArgs, options::OPT_r);
Daniel Dunbar008f54a2009-04-01 19:36:32 +00004008
Daniel Dunbar2008fee2010-09-17 00:24:54 +00004009 AddLinkerInputs(getToolChain(), Inputs, Args, CmdArgs);
Daniel Dunbar008f54a2009-04-01 19:36:32 +00004010
4011 if (!Args.hasArg(options::OPT_nostdlib) &&
4012 !Args.hasArg(options::OPT_nodefaultlibs)) {
Daniel Dunbar20022632010-02-17 08:07:51 +00004013 if (D.CCCIsCXX) {
Daniel Dunbar132e35d2010-09-17 01:20:05 +00004014 getToolChain().AddCXXStdlibLibArgs(Args, CmdArgs);
Roman Divackyc16bb762011-02-10 16:59:40 +00004015 if (Args.hasArg(options::OPT_pg))
4016 CmdArgs.push_back("-lm_p");
4017 else
4018 CmdArgs.push_back("-lm");
Daniel Dunbar20022632010-02-17 08:07:51 +00004019 }
Daniel Dunbar008f54a2009-04-01 19:36:32 +00004020 // FIXME: For some reason GCC passes -lgcc and -lgcc_s before adding
4021 // the default system libraries. Just mimic this for now.
Roman Divackyc16bb762011-02-10 16:59:40 +00004022 if (Args.hasArg(options::OPT_pg))
4023 CmdArgs.push_back("-lgcc_p");
4024 else
4025 CmdArgs.push_back("-lgcc");
Daniel Dunbar008f54a2009-04-01 19:36:32 +00004026 if (Args.hasArg(options::OPT_static)) {
4027 CmdArgs.push_back("-lgcc_eh");
Roman Divackyc16bb762011-02-10 16:59:40 +00004028 } else if (Args.hasArg(options::OPT_pg)) {
4029 CmdArgs.push_back("-lgcc_eh_p");
Daniel Dunbar008f54a2009-04-01 19:36:32 +00004030 } else {
4031 CmdArgs.push_back("--as-needed");
4032 CmdArgs.push_back("-lgcc_s");
4033 CmdArgs.push_back("--no-as-needed");
4034 }
4035
Matt Beaumont-Gay24230262011-02-10 20:35:01 +00004036 if (Args.hasArg(options::OPT_pthread)) {
Roman Divackyc16bb762011-02-10 16:59:40 +00004037 if (Args.hasArg(options::OPT_pg))
4038 CmdArgs.push_back("-lpthread_p");
4039 else
4040 CmdArgs.push_back("-lpthread");
Matt Beaumont-Gay24230262011-02-10 20:35:01 +00004041 }
Daniel Dunbar008f54a2009-04-01 19:36:32 +00004042
Roman Divackyc16bb762011-02-10 16:59:40 +00004043 if (Args.hasArg(options::OPT_pg)) {
4044 if (Args.hasArg(options::OPT_shared))
4045 CmdArgs.push_back("-lc");
4046 else
4047 CmdArgs.push_back("-lc_p");
4048 CmdArgs.push_back("-lgcc_p");
4049 } else {
4050 CmdArgs.push_back("-lc");
4051 CmdArgs.push_back("-lgcc");
4052 }
4053
Daniel Dunbar008f54a2009-04-01 19:36:32 +00004054 if (Args.hasArg(options::OPT_static)) {
4055 CmdArgs.push_back("-lgcc_eh");
Roman Divackyc16bb762011-02-10 16:59:40 +00004056 } else if (Args.hasArg(options::OPT_pg)) {
4057 CmdArgs.push_back("-lgcc_eh_p");
Daniel Dunbar008f54a2009-04-01 19:36:32 +00004058 } else {
4059 CmdArgs.push_back("--as-needed");
4060 CmdArgs.push_back("-lgcc_s");
4061 CmdArgs.push_back("--no-as-needed");
4062 }
4063 }
4064
4065 if (!Args.hasArg(options::OPT_nostdlib) &&
4066 !Args.hasArg(options::OPT_nostartfiles)) {
4067 if (!Args.hasArg(options::OPT_shared))
Daniel Dunbar4a7e8892010-07-14 18:46:23 +00004068 CmdArgs.push_back(Args.MakeArgString(getToolChain().GetFilePath(
Chris Lattner38e317d2010-07-07 16:01:42 +00004069 "crtend.o")));
Daniel Dunbar008f54a2009-04-01 19:36:32 +00004070 else
Daniel Dunbar4a7e8892010-07-14 18:46:23 +00004071 CmdArgs.push_back(Args.MakeArgString(getToolChain().GetFilePath(
Chris Lattner38e317d2010-07-07 16:01:42 +00004072 "crtendS.o")));
Daniel Dunbar4a7e8892010-07-14 18:46:23 +00004073 CmdArgs.push_back(Args.MakeArgString(getToolChain().GetFilePath(
Chris Lattner38e317d2010-07-07 16:01:42 +00004074 "crtn.o")));
Daniel Dunbar008f54a2009-04-01 19:36:32 +00004075 }
4076
Bill Wendling3f4be6f2011-06-27 19:15:03 +00004077 addProfileRT(getToolChain(), Args, CmdArgs, getToolChain().getTriple());
Nick Lewycky2e95a6d2011-05-24 21:54:59 +00004078
Daniel Dunbarc21c4852009-04-08 23:54:23 +00004079 const char *Exec =
Daniel Dunbar4a7e8892010-07-14 18:46:23 +00004080 Args.MakeArgString(getToolChain().GetProgramPath("ld"));
Daniel Dunbar2fe238e2010-08-02 02:38:28 +00004081 C.addCommand(new Command(JA, *this, Exec, CmdArgs));
Daniel Dunbar008f54a2009-04-01 19:36:32 +00004082}
Daniel Dunbar11e1b402009-05-02 18:28:39 +00004083
Benjamin Kramer8e50a962011-02-02 18:59:27 +00004084void netbsd::Assemble::ConstructJob(Compilation &C, const JobAction &JA,
4085 const InputInfo &Output,
4086 const InputInfoList &Inputs,
4087 const ArgList &Args,
4088 const char *LinkingOutput) const {
4089 ArgStringList CmdArgs;
4090
4091 // When building 32-bit code on NetBSD/amd64, we have to explicitly
4092 // instruct as in the base system to assemble 32-bit code.
Joerg Sonnenberger182564c2011-05-16 13:35:02 +00004093 if (ToolTriple.getArch() == llvm::Triple::x86_64 &&
4094 getToolChain().getArch() == llvm::Triple::x86)
Benjamin Kramer8e50a962011-02-02 18:59:27 +00004095 CmdArgs.push_back("--32");
4096
4097
4098 // Set byte order explicitly
4099 if (getToolChain().getArchName() == "mips")
4100 CmdArgs.push_back("-EB");
4101 else if (getToolChain().getArchName() == "mipsel")
4102 CmdArgs.push_back("-EL");
4103
4104 Args.AddAllArgValues(CmdArgs, options::OPT_Wa_COMMA,
4105 options::OPT_Xassembler);
4106
4107 CmdArgs.push_back("-o");
4108 CmdArgs.push_back(Output.getFilename());
4109
4110 for (InputInfoList::const_iterator
4111 it = Inputs.begin(), ie = Inputs.end(); it != ie; ++it) {
4112 const InputInfo &II = *it;
4113 CmdArgs.push_back(II.getFilename());
4114 }
4115
David Chisnall5adcec12011-09-27 22:03:18 +00004116 const char *Exec = Args.MakeArgString((getToolChain().GetProgramPath("as")));
Benjamin Kramer8e50a962011-02-02 18:59:27 +00004117 C.addCommand(new Command(JA, *this, Exec, CmdArgs));
4118}
4119
4120void netbsd::Link::ConstructJob(Compilation &C, const JobAction &JA,
4121 const InputInfo &Output,
4122 const InputInfoList &Inputs,
4123 const ArgList &Args,
4124 const char *LinkingOutput) const {
4125 const Driver &D = getToolChain().getDriver();
4126 ArgStringList CmdArgs;
4127
Joerg Sonnenberger8ab2bdc2011-03-21 13:51:29 +00004128 if (!D.SysRoot.empty())
4129 CmdArgs.push_back(Args.MakeArgString("--sysroot=" + D.SysRoot));
4130
Benjamin Kramer8e50a962011-02-02 18:59:27 +00004131 if (Args.hasArg(options::OPT_static)) {
4132 CmdArgs.push_back("-Bstatic");
4133 } else {
4134 if (Args.hasArg(options::OPT_rdynamic))
4135 CmdArgs.push_back("-export-dynamic");
4136 CmdArgs.push_back("--eh-frame-hdr");
4137 if (Args.hasArg(options::OPT_shared)) {
4138 CmdArgs.push_back("-Bshareable");
4139 } else {
4140 CmdArgs.push_back("-dynamic-linker");
4141 CmdArgs.push_back("/libexec/ld.elf_so");
4142 }
4143 }
4144
4145 // When building 32-bit code on NetBSD/amd64, we have to explicitly
4146 // instruct ld in the base system to link 32-bit code.
Joerg Sonnenberger182564c2011-05-16 13:35:02 +00004147 if (ToolTriple.getArch() == llvm::Triple::x86_64 &&
4148 getToolChain().getArch() == llvm::Triple::x86) {
Benjamin Kramer8e50a962011-02-02 18:59:27 +00004149 CmdArgs.push_back("-m");
4150 CmdArgs.push_back("elf_i386");
4151 }
4152
4153 if (Output.isFilename()) {
4154 CmdArgs.push_back("-o");
4155 CmdArgs.push_back(Output.getFilename());
4156 } else {
4157 assert(Output.isNothing() && "Invalid output.");
4158 }
4159
4160 if (!Args.hasArg(options::OPT_nostdlib) &&
4161 !Args.hasArg(options::OPT_nostartfiles)) {
4162 if (!Args.hasArg(options::OPT_shared)) {
4163 CmdArgs.push_back(Args.MakeArgString(
4164 getToolChain().GetFilePath("crt0.o")));
4165 CmdArgs.push_back(Args.MakeArgString(
4166 getToolChain().GetFilePath("crti.o")));
4167 CmdArgs.push_back(Args.MakeArgString(
4168 getToolChain().GetFilePath("crtbegin.o")));
4169 } else {
4170 CmdArgs.push_back(Args.MakeArgString(
4171 getToolChain().GetFilePath("crti.o")));
4172 CmdArgs.push_back(Args.MakeArgString(
4173 getToolChain().GetFilePath("crtbeginS.o")));
4174 }
4175 }
4176
4177 Args.AddAllArgs(CmdArgs, options::OPT_L);
4178 Args.AddAllArgs(CmdArgs, options::OPT_T_Group);
4179 Args.AddAllArgs(CmdArgs, options::OPT_e);
4180 Args.AddAllArgs(CmdArgs, options::OPT_s);
4181 Args.AddAllArgs(CmdArgs, options::OPT_t);
4182 Args.AddAllArgs(CmdArgs, options::OPT_Z_Flag);
4183 Args.AddAllArgs(CmdArgs, options::OPT_r);
4184
4185 AddLinkerInputs(getToolChain(), Inputs, Args, CmdArgs);
4186
4187 if (!Args.hasArg(options::OPT_nostdlib) &&
4188 !Args.hasArg(options::OPT_nodefaultlibs)) {
4189 if (D.CCCIsCXX) {
4190 getToolChain().AddCXXStdlibLibArgs(Args, CmdArgs);
4191 CmdArgs.push_back("-lm");
4192 }
4193 // FIXME: For some reason GCC passes -lgcc and -lgcc_s before adding
4194 // the default system libraries. Just mimic this for now.
Benjamin Kramer8e50a962011-02-02 18:59:27 +00004195 if (Args.hasArg(options::OPT_static)) {
4196 CmdArgs.push_back("-lgcc_eh");
4197 } else {
4198 CmdArgs.push_back("--as-needed");
4199 CmdArgs.push_back("-lgcc_s");
4200 CmdArgs.push_back("--no-as-needed");
4201 }
Joerg Sonnenbergerdb6393f2011-06-07 23:39:17 +00004202 CmdArgs.push_back("-lgcc");
Benjamin Kramer8e50a962011-02-02 18:59:27 +00004203
4204 if (Args.hasArg(options::OPT_pthread))
4205 CmdArgs.push_back("-lpthread");
4206 CmdArgs.push_back("-lc");
4207
4208 CmdArgs.push_back("-lgcc");
4209 if (Args.hasArg(options::OPT_static)) {
4210 CmdArgs.push_back("-lgcc_eh");
4211 } else {
4212 CmdArgs.push_back("--as-needed");
4213 CmdArgs.push_back("-lgcc_s");
4214 CmdArgs.push_back("--no-as-needed");
4215 }
4216 }
4217
4218 if (!Args.hasArg(options::OPT_nostdlib) &&
4219 !Args.hasArg(options::OPT_nostartfiles)) {
4220 if (!Args.hasArg(options::OPT_shared))
4221 CmdArgs.push_back(Args.MakeArgString(getToolChain().GetFilePath(
4222 "crtend.o")));
4223 else
4224 CmdArgs.push_back(Args.MakeArgString(getToolChain().GetFilePath(
4225 "crtendS.o")));
4226 CmdArgs.push_back(Args.MakeArgString(getToolChain().GetFilePath(
4227 "crtn.o")));
4228 }
4229
Bill Wendling3f4be6f2011-06-27 19:15:03 +00004230 addProfileRT(getToolChain(), Args, CmdArgs, getToolChain().getTriple());
Nick Lewycky2e95a6d2011-05-24 21:54:59 +00004231
David Chisnall5adcec12011-09-27 22:03:18 +00004232 const char *Exec = Args.MakeArgString(getToolChain().GetProgramPath("ld"));
Benjamin Kramer8e50a962011-02-02 18:59:27 +00004233 C.addCommand(new Command(JA, *this, Exec, CmdArgs));
4234}
4235
Rafael Espindolaba30bbe2010-08-10 00:25:48 +00004236void linuxtools::Assemble::ConstructJob(Compilation &C, const JobAction &JA,
4237 const InputInfo &Output,
4238 const InputInfoList &Inputs,
4239 const ArgList &Args,
4240 const char *LinkingOutput) const {
4241 ArgStringList CmdArgs;
4242
4243 // Add --32/--64 to make sure we get the format we want.
4244 // This is incomplete
4245 if (getToolChain().getArch() == llvm::Triple::x86) {
4246 CmdArgs.push_back("--32");
4247 } else if (getToolChain().getArch() == llvm::Triple::x86_64) {
4248 CmdArgs.push_back("--64");
4249 } else if (getToolChain().getArch() == llvm::Triple::arm) {
Chris Lattner5f9e2722011-07-23 10:55:15 +00004250 StringRef MArch = getToolChain().getArchName();
Rafael Espindolaba30bbe2010-08-10 00:25:48 +00004251 if (MArch == "armv7" || MArch == "armv7a" || MArch == "armv7-a")
4252 CmdArgs.push_back("-mfpu=neon");
4253 }
4254
4255 Args.AddAllArgValues(CmdArgs, options::OPT_Wa_COMMA,
4256 options::OPT_Xassembler);
4257
4258 CmdArgs.push_back("-o");
4259 CmdArgs.push_back(Output.getFilename());
4260
4261 for (InputInfoList::const_iterator
4262 it = Inputs.begin(), ie = Inputs.end(); it != ie; ++it) {
4263 const InputInfo &II = *it;
4264 CmdArgs.push_back(II.getFilename());
4265 }
4266
4267 const char *Exec =
4268 Args.MakeArgString(getToolChain().GetProgramPath("as"));
4269 C.addCommand(new Command(JA, *this, Exec, CmdArgs));
4270}
4271
Rafael Espindolaabf3ac72011-10-17 21:39:04 +00004272static void AddLibgcc(const Driver &D, ArgStringList &CmdArgs,
4273 const ArgList &Args) {
4274 bool StaticLibgcc = Args.hasArg(options::OPT_static) ||
4275 Args.hasArg(options::OPT_static_libgcc);
4276 if (!D.CCCIsCXX)
4277 CmdArgs.push_back("-lgcc");
4278
4279 if (StaticLibgcc) {
4280 if (D.CCCIsCXX)
4281 CmdArgs.push_back("-lgcc");
4282 } else {
4283 if (!D.CCCIsCXX)
4284 CmdArgs.push_back("--as-needed");
4285 CmdArgs.push_back("-lgcc_s");
4286 if (!D.CCCIsCXX)
4287 CmdArgs.push_back("--no-as-needed");
4288 }
4289
4290 if (StaticLibgcc)
4291 CmdArgs.push_back("-lgcc_eh");
4292 else if (!Args.hasArg(options::OPT_shared) && D.CCCIsCXX)
4293 CmdArgs.push_back("-lgcc");
4294}
4295
Rafael Espindolac1da9812010-11-07 20:14:31 +00004296void linuxtools::Link::ConstructJob(Compilation &C, const JobAction &JA,
4297 const InputInfo &Output,
4298 const InputInfoList &Inputs,
4299 const ArgList &Args,
4300 const char *LinkingOutput) const {
4301 const toolchains::Linux& ToolChain =
4302 static_cast<const toolchains::Linux&>(getToolChain());
4303 const Driver &D = ToolChain.getDriver();
4304 ArgStringList CmdArgs;
4305
Rafael Espindola26f14c32010-11-15 18:28:16 +00004306 // Silence warning for "clang -g foo.o -o foo"
4307 Args.ClaimAllArgs(options::OPT_g_Group);
Rafael Espindola9c094fb2011-03-01 05:25:27 +00004308 // and "clang -emit-llvm foo.o -o foo"
4309 Args.ClaimAllArgs(options::OPT_emit_llvm);
Rafael Espindola7f6458b2010-11-17 20:37:10 +00004310 // and for "clang -g foo.o -o foo". Other warning options are already
4311 // handled somewhere else.
4312 Args.ClaimAllArgs(options::OPT_w);
Rafael Espindola26f14c32010-11-15 18:28:16 +00004313
Joerg Sonnenberger8ab2bdc2011-03-21 13:51:29 +00004314 if (!D.SysRoot.empty())
4315 CmdArgs.push_back(Args.MakeArgString("--sysroot=" + D.SysRoot));
Rafael Espindolac1da9812010-11-07 20:14:31 +00004316
Rafael Espindolafdda1712010-11-17 22:26:15 +00004317 if (Args.hasArg(options::OPT_pie))
4318 CmdArgs.push_back("-pie");
4319
Rafael Espindoladc1b76d2010-11-07 22:57:16 +00004320 if (Args.hasArg(options::OPT_rdynamic))
4321 CmdArgs.push_back("-export-dynamic");
4322
Rafael Espindolae0e6d3b2010-11-11 19:34:42 +00004323 if (Args.hasArg(options::OPT_s))
4324 CmdArgs.push_back("-s");
4325
Rafael Espindolac1da9812010-11-07 20:14:31 +00004326 for (std::vector<std::string>::const_iterator i = ToolChain.ExtraOpts.begin(),
4327 e = ToolChain.ExtraOpts.end();
4328 i != e; ++i)
4329 CmdArgs.push_back(i->c_str());
4330
4331 if (!Args.hasArg(options::OPT_static)) {
4332 CmdArgs.push_back("--eh-frame-hdr");
4333 }
4334
4335 CmdArgs.push_back("-m");
4336 if (ToolChain.getArch() == llvm::Triple::x86)
4337 CmdArgs.push_back("elf_i386");
Eric Christopher88b7cf02011-08-19 00:30:14 +00004338 else if (ToolChain.getArch() == llvm::Triple::arm
Douglas Gregorf0594d82011-03-06 19:11:49 +00004339 || ToolChain.getArch() == llvm::Triple::thumb)
Rafael Espindolac1da9812010-11-07 20:14:31 +00004340 CmdArgs.push_back("armelf_linux_eabi");
Ted Kremenek43ac2972011-04-05 22:04:27 +00004341 else if (ToolChain.getArch() == llvm::Triple::ppc)
4342 CmdArgs.push_back("elf32ppclinux");
4343 else if (ToolChain.getArch() == llvm::Triple::ppc64)
4344 CmdArgs.push_back("elf64ppc");
Eli Friedman5bea4f62011-11-08 19:43:37 +00004345 else if (ToolChain.getArch() == llvm::Triple::mips)
4346 CmdArgs.push_back("elf32btsmip");
4347 else if (ToolChain.getArch() == llvm::Triple::mipsel)
4348 CmdArgs.push_back("elf32ltsmip");
4349 else if (ToolChain.getArch() == llvm::Triple::mips64)
4350 CmdArgs.push_back("elf64btsmip");
4351 else if (ToolChain.getArch() == llvm::Triple::mips64el)
4352 CmdArgs.push_back("elf64ltsmip");
Rafael Espindolac1da9812010-11-07 20:14:31 +00004353 else
4354 CmdArgs.push_back("elf_x86_64");
4355
4356 if (Args.hasArg(options::OPT_static)) {
Douglas Gregorf0594d82011-03-06 19:11:49 +00004357 if (ToolChain.getArch() == llvm::Triple::arm
4358 || ToolChain.getArch() == llvm::Triple::thumb)
Rafael Espindolac1da9812010-11-07 20:14:31 +00004359 CmdArgs.push_back("-Bstatic");
4360 else
4361 CmdArgs.push_back("-static");
4362 } else if (Args.hasArg(options::OPT_shared)) {
4363 CmdArgs.push_back("-shared");
4364 }
4365
4366 if (ToolChain.getArch() == llvm::Triple::arm ||
Douglas Gregorf0594d82011-03-06 19:11:49 +00004367 ToolChain.getArch() == llvm::Triple::thumb ||
Rafael Espindolac1da9812010-11-07 20:14:31 +00004368 (!Args.hasArg(options::OPT_static) &&
4369 !Args.hasArg(options::OPT_shared))) {
4370 CmdArgs.push_back("-dynamic-linker");
4371 if (ToolChain.getArch() == llvm::Triple::x86)
4372 CmdArgs.push_back("/lib/ld-linux.so.2");
Douglas Gregorf0594d82011-03-06 19:11:49 +00004373 else if (ToolChain.getArch() == llvm::Triple::arm ||
4374 ToolChain.getArch() == llvm::Triple::thumb)
Rafael Espindolac1da9812010-11-07 20:14:31 +00004375 CmdArgs.push_back("/lib/ld-linux.so.3");
Eli Friedman5bea4f62011-11-08 19:43:37 +00004376 else if (ToolChain.getArch() == llvm::Triple::mips ||
4377 ToolChain.getArch() == llvm::Triple::mipsel)
4378 CmdArgs.push_back("/lib/ld.so.1");
Ted Kremenek43ac2972011-04-05 22:04:27 +00004379 else if (ToolChain.getArch() == llvm::Triple::ppc)
Chris Lattner09f43ed2011-04-11 21:15:37 +00004380 CmdArgs.push_back("/lib/ld.so.1");
Ted Kremenek43ac2972011-04-05 22:04:27 +00004381 else if (ToolChain.getArch() == llvm::Triple::ppc64)
Chris Lattner09f43ed2011-04-11 21:15:37 +00004382 CmdArgs.push_back("/lib64/ld64.so.1");
Rafael Espindolac1da9812010-11-07 20:14:31 +00004383 else
4384 CmdArgs.push_back("/lib64/ld-linux-x86-64.so.2");
4385 }
4386
4387 CmdArgs.push_back("-o");
4388 CmdArgs.push_back(Output.getFilename());
4389
Rafael Espindola49c64fd2010-12-01 01:52:43 +00004390 if (!Args.hasArg(options::OPT_nostdlib) &&
4391 !Args.hasArg(options::OPT_nostartfiles)) {
Rafael Espindolafdda1712010-11-17 22:26:15 +00004392 const char *crt1 = NULL;
4393 if (!Args.hasArg(options::OPT_shared)){
4394 if (Args.hasArg(options::OPT_pie))
4395 crt1 = "Scrt1.o";
4396 else
4397 crt1 = "crt1.o";
4398 }
4399 if (crt1)
4400 CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath(crt1)));
Rafael Espindolac1da9812010-11-07 20:14:31 +00004401
Rafael Espindola89414b32010-11-12 03:00:39 +00004402 CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath("crti.o")));
Rafael Espindolac1da9812010-11-07 20:14:31 +00004403
Rafael Espindola89414b32010-11-12 03:00:39 +00004404 const char *crtbegin;
4405 if (Args.hasArg(options::OPT_static))
4406 crtbegin = "crtbeginT.o";
Rafael Espindolafdda1712010-11-17 22:26:15 +00004407 else if (Args.hasArg(options::OPT_shared) || Args.hasArg(options::OPT_pie))
Rafael Espindola89414b32010-11-12 03:00:39 +00004408 crtbegin = "crtbeginS.o";
4409 else
4410 crtbegin = "crtbegin.o";
4411 CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath(crtbegin)));
4412 }
Rafael Espindolac1da9812010-11-07 20:14:31 +00004413
4414 Args.AddAllArgs(CmdArgs, options::OPT_L);
4415
4416 const ToolChain::path_list Paths = ToolChain.getFilePaths();
4417
Roman Divacky58e5ac92011-03-01 17:53:14 +00004418 for (ToolChain::path_list::const_iterator i = Paths.begin(), e = Paths.end();
4419 i != e; ++i)
Chris Lattner5f9e2722011-07-23 10:55:15 +00004420 CmdArgs.push_back(Args.MakeArgString(StringRef("-L") + *i));
Rafael Espindolac1da9812010-11-07 20:14:31 +00004421
4422 AddLinkerInputs(ToolChain, Inputs, Args, CmdArgs);
4423
Rafael Espindola89414b32010-11-12 03:00:39 +00004424 if (D.CCCIsCXX && !Args.hasArg(options::OPT_nostdlib)) {
Rafael Espindola19706f82011-10-17 22:14:51 +00004425 bool OnlyLibstdcxxStatic = Args.hasArg(options::OPT_static_libstdcxx) &&
4426 !Args.hasArg(options::OPT_static);
4427 if (OnlyLibstdcxxStatic)
4428 CmdArgs.push_back("-Bstatic");
Rafael Espindolac1da9812010-11-07 20:14:31 +00004429 ToolChain.AddCXXStdlibLibArgs(Args, CmdArgs);
Rafael Espindola19706f82011-10-17 22:14:51 +00004430 if (OnlyLibstdcxxStatic)
4431 CmdArgs.push_back("-Bdynamic");
Rafael Espindolac1da9812010-11-07 20:14:31 +00004432 CmdArgs.push_back("-lm");
4433 }
4434
Rafael Espindola89414b32010-11-12 03:00:39 +00004435 if (!Args.hasArg(options::OPT_nostdlib)) {
Nick Lewycky80df0252011-06-04 06:27:06 +00004436 if (Args.hasArg(options::OPT_static))
4437 CmdArgs.push_back("--start-group");
4438
Rafael Espindolaabf3ac72011-10-17 21:39:04 +00004439 AddLibgcc(D, CmdArgs, Args);
Rafael Espindola89414b32010-11-12 03:00:39 +00004440
4441 if (Args.hasArg(options::OPT_pthread) ||
4442 Args.hasArg(options::OPT_pthreads))
4443 CmdArgs.push_back("-lpthread");
4444
4445 CmdArgs.push_back("-lc");
4446
4447 if (Args.hasArg(options::OPT_static))
4448 CmdArgs.push_back("--end-group");
Rafael Espindolaabf3ac72011-10-17 21:39:04 +00004449 else
4450 AddLibgcc(D, CmdArgs, Args);
Rafael Espindola89414b32010-11-12 03:00:39 +00004451
Rafael Espindolafdda1712010-11-17 22:26:15 +00004452
Rafael Espindola49c64fd2010-12-01 01:52:43 +00004453 if (!Args.hasArg(options::OPT_nostartfiles)) {
4454 const char *crtend;
4455 if (Args.hasArg(options::OPT_shared) || Args.hasArg(options::OPT_pie))
4456 crtend = "crtendS.o";
4457 else
4458 crtend = "crtend.o";
Rafael Espindola89414b32010-11-12 03:00:39 +00004459
Rafael Espindola49c64fd2010-12-01 01:52:43 +00004460 CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath(crtend)));
4461 CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath("crtn.o")));
4462 }
Rafael Espindolac1da9812010-11-07 20:14:31 +00004463 }
4464
Bill Wendling3f4be6f2011-06-27 19:15:03 +00004465 addProfileRT(getToolChain(), Args, CmdArgs, getToolChain().getTriple());
Nick Lewycky2e95a6d2011-05-24 21:54:59 +00004466
Peter Collingbourne79d481a2011-11-05 03:47:53 +00004467 if (D.IsUsingLTO(Args) || Args.hasArg(options::OPT_use_gold_plugin)) {
Rafael Espindolac1da9812010-11-07 20:14:31 +00004468 CmdArgs.push_back("-plugin");
4469 std::string Plugin = ToolChain.getDriver().Dir + "/../lib/LLVMgold.so";
4470 CmdArgs.push_back(Args.MakeArgString(Plugin));
4471 }
4472
4473 C.addCommand(new Command(JA, *this, ToolChain.Linker.c_str(), CmdArgs));
4474}
Rafael Espindolaba30bbe2010-08-10 00:25:48 +00004475
Chris Lattner38e317d2010-07-07 16:01:42 +00004476void minix::Assemble::ConstructJob(Compilation &C, const JobAction &JA,
Daniel Dunbar2fe238e2010-08-02 02:38:28 +00004477 const InputInfo &Output,
4478 const InputInfoList &Inputs,
4479 const ArgList &Args,
4480 const char *LinkingOutput) const {
Chris Lattner38e317d2010-07-07 16:01:42 +00004481 ArgStringList CmdArgs;
4482
4483 Args.AddAllArgValues(CmdArgs, options::OPT_Wa_COMMA,
4484 options::OPT_Xassembler);
4485
4486 CmdArgs.push_back("-o");
Daniel Dunbar7c1e4652010-08-02 02:38:21 +00004487 CmdArgs.push_back(Output.getFilename());
Chris Lattner38e317d2010-07-07 16:01:42 +00004488
4489 for (InputInfoList::const_iterator
4490 it = Inputs.begin(), ie = Inputs.end(); it != ie; ++it) {
4491 const InputInfo &II = *it;
Daniel Dunbar7c1e4652010-08-02 02:38:21 +00004492 CmdArgs.push_back(II.getFilename());
Chris Lattner38e317d2010-07-07 16:01:42 +00004493 }
4494
4495 const char *Exec =
Daniel Dunbar4a7e8892010-07-14 18:46:23 +00004496 Args.MakeArgString(getToolChain().GetProgramPath("gas"));
Daniel Dunbar2fe238e2010-08-02 02:38:28 +00004497 C.addCommand(new Command(JA, *this, Exec, CmdArgs));
Chris Lattner38e317d2010-07-07 16:01:42 +00004498}
4499
4500void minix::Link::ConstructJob(Compilation &C, const JobAction &JA,
Daniel Dunbar2fe238e2010-08-02 02:38:28 +00004501 const InputInfo &Output,
4502 const InputInfoList &Inputs,
4503 const ArgList &Args,
4504 const char *LinkingOutput) const {
Chris Lattner38e317d2010-07-07 16:01:42 +00004505 const Driver &D = getToolChain().getDriver();
4506 ArgStringList CmdArgs;
4507
Daniel Dunbar7c1e4652010-08-02 02:38:21 +00004508 if (Output.isFilename()) {
Chris Lattner38e317d2010-07-07 16:01:42 +00004509 CmdArgs.push_back("-o");
4510 CmdArgs.push_back(Output.getFilename());
4511 } else {
4512 assert(Output.isNothing() && "Invalid output.");
4513 }
4514
4515 if (!Args.hasArg(options::OPT_nostdlib) &&
4516 !Args.hasArg(options::OPT_nostartfiles))
Daniel Dunbar4a7e8892010-07-14 18:46:23 +00004517 CmdArgs.push_back(Args.MakeArgString(getToolChain().GetFilePath(
Chris Lattner38e317d2010-07-07 16:01:42 +00004518 "/usr/gnu/lib/crtso.o")));
4519
4520 Args.AddAllArgs(CmdArgs, options::OPT_L);
4521 Args.AddAllArgs(CmdArgs, options::OPT_T_Group);
4522 Args.AddAllArgs(CmdArgs, options::OPT_e);
4523
Daniel Dunbar2008fee2010-09-17 00:24:54 +00004524 AddLinkerInputs(getToolChain(), Inputs, Args, CmdArgs);
Chris Lattner38e317d2010-07-07 16:01:42 +00004525
4526 if (!Args.hasArg(options::OPT_nostdlib) &&
4527 !Args.hasArg(options::OPT_nodefaultlibs)) {
4528 if (D.CCCIsCXX) {
Daniel Dunbar132e35d2010-09-17 01:20:05 +00004529 getToolChain().AddCXXStdlibLibArgs(Args, CmdArgs);
Chris Lattner38e317d2010-07-07 16:01:42 +00004530 CmdArgs.push_back("-lm");
4531 }
4532
4533 if (Args.hasArg(options::OPT_pthread))
4534 CmdArgs.push_back("-lpthread");
4535 CmdArgs.push_back("-lc");
4536 CmdArgs.push_back("-lgcc");
4537 CmdArgs.push_back("-L/usr/gnu/lib");
4538 // FIXME: fill in the correct search path for the final
4539 // support libraries.
4540 CmdArgs.push_back("-L/usr/gnu/lib/gcc/i686-pc-minix/4.4.3");
4541 }
4542
4543 if (!Args.hasArg(options::OPT_nostdlib) &&
4544 !Args.hasArg(options::OPT_nostartfiles)) {
Daniel Dunbar4a7e8892010-07-14 18:46:23 +00004545 CmdArgs.push_back(Args.MakeArgString(getToolChain().GetFilePath(
Chris Lattner38e317d2010-07-07 16:01:42 +00004546 "/usr/gnu/lib/libend.a")));
4547 }
4548
Bill Wendling3f4be6f2011-06-27 19:15:03 +00004549 addProfileRT(getToolChain(), Args, CmdArgs, getToolChain().getTriple());
Nick Lewycky2e95a6d2011-05-24 21:54:59 +00004550
Chris Lattner38e317d2010-07-07 16:01:42 +00004551 const char *Exec =
Daniel Dunbar4a7e8892010-07-14 18:46:23 +00004552 Args.MakeArgString(getToolChain().GetProgramPath("/usr/gnu/bin/gld"));
Daniel Dunbar2fe238e2010-08-02 02:38:28 +00004553 C.addCommand(new Command(JA, *this, Exec, CmdArgs));
Chris Lattner38e317d2010-07-07 16:01:42 +00004554}
4555
Daniel Dunbar11e1b402009-05-02 18:28:39 +00004556/// DragonFly Tools
4557
4558// For now, DragonFly Assemble does just about the same as for
4559// FreeBSD, but this may change soon.
4560void dragonfly::Assemble::ConstructJob(Compilation &C, const JobAction &JA,
Daniel Dunbar2fe238e2010-08-02 02:38:28 +00004561 const InputInfo &Output,
Daniel Dunbar294691e2009-11-04 06:24:38 +00004562 const InputInfoList &Inputs,
4563 const ArgList &Args,
4564 const char *LinkingOutput) const {
Daniel Dunbar11e1b402009-05-02 18:28:39 +00004565 ArgStringList CmdArgs;
4566
4567 // When building 32-bit code on DragonFly/pc64, we have to explicitly
4568 // instruct as in the base system to assemble 32-bit code.
4569 if (getToolChain().getArchName() == "i386")
4570 CmdArgs.push_back("--32");
4571
4572 Args.AddAllArgValues(CmdArgs, options::OPT_Wa_COMMA,
4573 options::OPT_Xassembler);
4574
4575 CmdArgs.push_back("-o");
Daniel Dunbar7c1e4652010-08-02 02:38:21 +00004576 CmdArgs.push_back(Output.getFilename());
Daniel Dunbar11e1b402009-05-02 18:28:39 +00004577
4578 for (InputInfoList::const_iterator
4579 it = Inputs.begin(), ie = Inputs.end(); it != ie; ++it) {
4580 const InputInfo &II = *it;
Daniel Dunbar7c1e4652010-08-02 02:38:21 +00004581 CmdArgs.push_back(II.getFilename());
Daniel Dunbar11e1b402009-05-02 18:28:39 +00004582 }
4583
4584 const char *Exec =
Daniel Dunbar4a7e8892010-07-14 18:46:23 +00004585 Args.MakeArgString(getToolChain().GetProgramPath("as"));
Daniel Dunbar2fe238e2010-08-02 02:38:28 +00004586 C.addCommand(new Command(JA, *this, Exec, CmdArgs));
Daniel Dunbar11e1b402009-05-02 18:28:39 +00004587}
4588
4589void dragonfly::Link::ConstructJob(Compilation &C, const JobAction &JA,
Daniel Dunbar2fe238e2010-08-02 02:38:28 +00004590 const InputInfo &Output,
4591 const InputInfoList &Inputs,
4592 const ArgList &Args,
4593 const char *LinkingOutput) const {
Daniel Dunbaree788e72009-12-21 18:54:17 +00004594 const Driver &D = getToolChain().getDriver();
Daniel Dunbar11e1b402009-05-02 18:28:39 +00004595 ArgStringList CmdArgs;
4596
Joerg Sonnenberger8ab2bdc2011-03-21 13:51:29 +00004597 if (!D.SysRoot.empty())
4598 CmdArgs.push_back(Args.MakeArgString("--sysroot=" + D.SysRoot));
4599
Daniel Dunbar11e1b402009-05-02 18:28:39 +00004600 if (Args.hasArg(options::OPT_static)) {
4601 CmdArgs.push_back("-Bstatic");
4602 } else {
4603 if (Args.hasArg(options::OPT_shared))
4604 CmdArgs.push_back("-Bshareable");
4605 else {
4606 CmdArgs.push_back("-dynamic-linker");
4607 CmdArgs.push_back("/usr/libexec/ld-elf.so.2");
4608 }
4609 }
4610
4611 // When building 32-bit code on DragonFly/pc64, we have to explicitly
4612 // instruct ld in the base system to link 32-bit code.
4613 if (getToolChain().getArchName() == "i386") {
4614 CmdArgs.push_back("-m");
4615 CmdArgs.push_back("elf_i386");
4616 }
4617
Daniel Dunbar7c1e4652010-08-02 02:38:21 +00004618 if (Output.isFilename()) {
Daniel Dunbar11e1b402009-05-02 18:28:39 +00004619 CmdArgs.push_back("-o");
4620 CmdArgs.push_back(Output.getFilename());
4621 } else {
4622 assert(Output.isNothing() && "Invalid output.");
4623 }
4624
4625 if (!Args.hasArg(options::OPT_nostdlib) &&
4626 !Args.hasArg(options::OPT_nostartfiles)) {
4627 if (!Args.hasArg(options::OPT_shared)) {
Chris Lattner38e317d2010-07-07 16:01:42 +00004628 CmdArgs.push_back(
Daniel Dunbar4a7e8892010-07-14 18:46:23 +00004629 Args.MakeArgString(getToolChain().GetFilePath("crt1.o")));
Chris Lattner38e317d2010-07-07 16:01:42 +00004630 CmdArgs.push_back(
Daniel Dunbar4a7e8892010-07-14 18:46:23 +00004631 Args.MakeArgString(getToolChain().GetFilePath("crti.o")));
Chris Lattner38e317d2010-07-07 16:01:42 +00004632 CmdArgs.push_back(
Daniel Dunbar4a7e8892010-07-14 18:46:23 +00004633 Args.MakeArgString(getToolChain().GetFilePath("crtbegin.o")));
Daniel Dunbar11e1b402009-05-02 18:28:39 +00004634 } else {
Chris Lattner38e317d2010-07-07 16:01:42 +00004635 CmdArgs.push_back(
Daniel Dunbar4a7e8892010-07-14 18:46:23 +00004636 Args.MakeArgString(getToolChain().GetFilePath("crti.o")));
Chris Lattner38e317d2010-07-07 16:01:42 +00004637 CmdArgs.push_back(
Daniel Dunbar4a7e8892010-07-14 18:46:23 +00004638 Args.MakeArgString(getToolChain().GetFilePath("crtbeginS.o")));
Daniel Dunbar11e1b402009-05-02 18:28:39 +00004639 }
4640 }
4641
4642 Args.AddAllArgs(CmdArgs, options::OPT_L);
4643 Args.AddAllArgs(CmdArgs, options::OPT_T_Group);
4644 Args.AddAllArgs(CmdArgs, options::OPT_e);
4645
Daniel Dunbar2008fee2010-09-17 00:24:54 +00004646 AddLinkerInputs(getToolChain(), Inputs, Args, CmdArgs);
Daniel Dunbar11e1b402009-05-02 18:28:39 +00004647
4648 if (!Args.hasArg(options::OPT_nostdlib) &&
4649 !Args.hasArg(options::OPT_nodefaultlibs)) {
4650 // FIXME: GCC passes on -lgcc, -lgcc_pic and a whole lot of
4651 // rpaths
4652 CmdArgs.push_back("-L/usr/lib/gcc41");
4653
4654 if (!Args.hasArg(options::OPT_static)) {
4655 CmdArgs.push_back("-rpath");
4656 CmdArgs.push_back("/usr/lib/gcc41");
4657
4658 CmdArgs.push_back("-rpath-link");
4659 CmdArgs.push_back("/usr/lib/gcc41");
4660
4661 CmdArgs.push_back("-rpath");
4662 CmdArgs.push_back("/usr/lib");
4663
4664 CmdArgs.push_back("-rpath-link");
4665 CmdArgs.push_back("/usr/lib");
4666 }
4667
Rafael Espindola405861d2010-07-20 12:59:03 +00004668 if (D.CCCIsCXX) {
Daniel Dunbar132e35d2010-09-17 01:20:05 +00004669 getToolChain().AddCXXStdlibLibArgs(Args, CmdArgs);
Rafael Espindola405861d2010-07-20 12:59:03 +00004670 CmdArgs.push_back("-lm");
4671 }
4672
Daniel Dunbar11e1b402009-05-02 18:28:39 +00004673 if (Args.hasArg(options::OPT_shared)) {
4674 CmdArgs.push_back("-lgcc_pic");
4675 } else {
4676 CmdArgs.push_back("-lgcc");
4677 }
4678
4679
4680 if (Args.hasArg(options::OPT_pthread))
Mike Stump4d63f8b2009-10-31 20:11:46 +00004681 CmdArgs.push_back("-lpthread");
Daniel Dunbar11e1b402009-05-02 18:28:39 +00004682
4683 if (!Args.hasArg(options::OPT_nolibc)) {
4684 CmdArgs.push_back("-lc");
4685 }
4686
4687 if (Args.hasArg(options::OPT_shared)) {
4688 CmdArgs.push_back("-lgcc_pic");
4689 } else {
4690 CmdArgs.push_back("-lgcc");
4691 }
4692 }
4693
4694 if (!Args.hasArg(options::OPT_nostdlib) &&
4695 !Args.hasArg(options::OPT_nostartfiles)) {
4696 if (!Args.hasArg(options::OPT_shared))
Chris Lattner38e317d2010-07-07 16:01:42 +00004697 CmdArgs.push_back(Args.MakeArgString(
Daniel Dunbar4a7e8892010-07-14 18:46:23 +00004698 getToolChain().GetFilePath("crtend.o")));
Daniel Dunbar11e1b402009-05-02 18:28:39 +00004699 else
Chris Lattner38e317d2010-07-07 16:01:42 +00004700 CmdArgs.push_back(Args.MakeArgString(
Daniel Dunbar4a7e8892010-07-14 18:46:23 +00004701 getToolChain().GetFilePath("crtendS.o")));
Chris Lattner38e317d2010-07-07 16:01:42 +00004702 CmdArgs.push_back(Args.MakeArgString(
Daniel Dunbar4a7e8892010-07-14 18:46:23 +00004703 getToolChain().GetFilePath("crtn.o")));
Daniel Dunbar11e1b402009-05-02 18:28:39 +00004704 }
4705
Bill Wendling3f4be6f2011-06-27 19:15:03 +00004706 addProfileRT(getToolChain(), Args, CmdArgs, getToolChain().getTriple());
Nick Lewycky2e95a6d2011-05-24 21:54:59 +00004707
Daniel Dunbar11e1b402009-05-02 18:28:39 +00004708 const char *Exec =
Daniel Dunbar4a7e8892010-07-14 18:46:23 +00004709 Args.MakeArgString(getToolChain().GetProgramPath("ld"));
Daniel Dunbar2fe238e2010-08-02 02:38:28 +00004710 C.addCommand(new Command(JA, *this, Exec, CmdArgs));
Daniel Dunbar11e1b402009-05-02 18:28:39 +00004711}
Michael J. Spencerff58e362010-08-21 21:55:07 +00004712
4713void visualstudio::Link::ConstructJob(Compilation &C, const JobAction &JA,
4714 const InputInfo &Output,
4715 const InputInfoList &Inputs,
4716 const ArgList &Args,
4717 const char *LinkingOutput) const {
Michael J. Spencerff58e362010-08-21 21:55:07 +00004718 ArgStringList CmdArgs;
4719
4720 if (Output.isFilename()) {
Daniel Dunbare5a37f42010-09-17 00:45:02 +00004721 CmdArgs.push_back(Args.MakeArgString(std::string("-out:") +
4722 Output.getFilename()));
Michael J. Spencerff58e362010-08-21 21:55:07 +00004723 } else {
4724 assert(Output.isNothing() && "Invalid output.");
4725 }
4726
4727 if (!Args.hasArg(options::OPT_nostdlib) &&
4728 !Args.hasArg(options::OPT_nostartfiles)) {
4729 CmdArgs.push_back("-defaultlib:libcmt");
4730 }
4731
4732 CmdArgs.push_back("-nologo");
4733
Daniel Dunbar2008fee2010-09-17 00:24:54 +00004734 AddLinkerInputs(getToolChain(), Inputs, Args, CmdArgs);
Michael J. Spencerff58e362010-08-21 21:55:07 +00004735
4736 const char *Exec =
Daniel Dunbar2008fee2010-09-17 00:24:54 +00004737 Args.MakeArgString(getToolChain().GetProgramPath("link.exe"));
Michael J. Spencerff58e362010-08-21 21:55:07 +00004738 C.addCommand(new Command(JA, *this, Exec, CmdArgs));
4739}