blob: 1486065411d4ad97b301a4818b07bcfb4ae67478 [file] [log] [blame]
Nick Lewyckye47c2452010-09-23 23:48:20 +00001//===--- Tools.cpp - Tools Implementations --------------------------------===//
Daniel Dunbar1a093d22009-03-18 06:00:36 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10#include "Tools.h"
11
Daniel Dunbara2aedc62009-03-18 10:01:51 +000012#include "clang/Driver/Action.h"
Daniel Dunbar04c4c2c2009-03-18 07:06:02 +000013#include "clang/Driver/Arg.h"
Daniel Dunbara3246a02009-03-18 08:07:30 +000014#include "clang/Driver/ArgList.h"
Daniel Dunbar1a8a2e82009-10-29 02:39:57 +000015#include "clang/Driver/Driver.h"
16#include "clang/Driver/DriverDiagnostic.h"
Daniel Dunbar04c4c2c2009-03-18 07:06:02 +000017#include "clang/Driver/Compilation.h"
18#include "clang/Driver/Job.h"
Daniel Dunbara3246a02009-03-18 08:07:30 +000019#include "clang/Driver/HostInfo.h"
John McCall24fc0de2011-07-06 00:26:06 +000020#include "clang/Driver/ObjCRuntime.h"
Daniel Dunbara3246a02009-03-18 08:07:30 +000021#include "clang/Driver/Option.h"
Daniel Dunbarda13faf2009-11-19 04:25:22 +000022#include "clang/Driver/Options.h"
Daniel Dunbara3246a02009-03-18 08:07:30 +000023#include "clang/Driver/ToolChain.h"
Daniel Dunbar04c4c2c2009-03-18 07:06:02 +000024#include "clang/Driver/Util.h"
25
Daniel Dunbar1c28f1e2009-09-09 22:32:48 +000026#include "llvm/ADT/SmallString.h"
Douglas Gregorf7b87cb2009-10-29 00:41:01 +000027#include "llvm/ADT/StringSwitch.h"
Daniel Dunbarb4a3e432009-09-09 22:32:34 +000028#include "llvm/ADT/Twine.h"
Michael J. Spencerf6efe582011-01-10 02:34:13 +000029#include "llvm/Support/FileSystem.h"
Daniel Dunbarc1964212009-03-26 16:23:12 +000030#include "llvm/Support/Format.h"
31#include "llvm/Support/raw_ostream.h"
Michael J. Spencer8aaf4992010-11-29 18:12:39 +000032#include "llvm/Support/Host.h"
33#include "llvm/Support/Process.h"
John McCall31168b02011-06-15 23:02:42 +000034#include "llvm/Support/ErrorHandling.h"
Daniel Dunbar04c4c2c2009-03-18 07:06:02 +000035
36#include "InputInfo.h"
Daniel Dunbarc1964212009-03-26 16:23:12 +000037#include "ToolChains.h"
Daniel Dunbar04c4c2c2009-03-18 07:06:02 +000038
Dylan Noblesmith70e73a32011-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 Dunbar1a093d22009-03-18 06:00:36 +000046using namespace clang::driver;
47using namespace clang::driver::tools;
Chris Lattner0e62c1c2011-07-23 10:55:15 +000048using namespace clang;
Daniel Dunbar1a093d22009-03-18 06:00:36 +000049
Joerg Sonnenbergerfcc3ec92011-03-21 14:01:40 +000050/// FindTargetProgramPath - Return path of the target specific version of
51/// ProgName. If it doesn't exist, return path of ProgName itself.
52static std::string FindTargetProgramPath(const ToolChain &TheToolChain,
Joerg Sonnenberger637603a2011-05-16 13:35:02 +000053 const std::string TripleString,
Joerg Sonnenbergerfcc3ec92011-03-21 14:01:40 +000054 const char *ProgName) {
Joerg Sonnenberger637603a2011-05-16 13:35:02 +000055 std::string Executable(TripleString + "-" + ProgName);
Joerg Sonnenbergerfcc3ec92011-03-21 14:01:40 +000056 std::string Path(TheToolChain.GetProgramPath(Executable.c_str()));
57 if (Path != Executable)
58 return Path;
59 return TheToolChain.GetProgramPath(ProgName);
60}
61
Daniel Dunbar64198ef2009-09-10 01:21:05 +000062/// CheckPreprocessingOptions - Perform some validation of preprocessing
63/// arguments that is shared with gcc.
64static void CheckPreprocessingOptions(const Driver &D, const ArgList &Args) {
65 if (Arg *A = Args.getLastArg(options::OPT_C, options::OPT_CC))
Joerg Sonnenbergerb86f5f42011-03-06 23:31:01 +000066 if (!Args.hasArg(options::OPT_E) && !D.CCCIsCPP)
Chris Lattner0e62c1c2011-07-23 10:55:15 +000067 D.Diag(diag::err_drv_argument_only_allowed_with)
Daniel Dunbar64198ef2009-09-10 01:21:05 +000068 << A->getAsString(Args) << "-E";
69}
70
Daniel Dunbar4eadb602009-09-10 01:21:12 +000071/// CheckCodeGenerationOptions - Perform some validation of code generation
72/// arguments that is shared with gcc.
73static void CheckCodeGenerationOptions(const Driver &D, const ArgList &Args) {
74 // In gcc, only ARM checks this, but it seems reasonable to check universally.
75 if (Args.hasArg(options::OPT_static))
76 if (const Arg *A = Args.getLastArg(options::OPT_dynamic,
77 options::OPT_mdynamic_no_pic))
Chris Lattner0e62c1c2011-07-23 10:55:15 +000078 D.Diag(diag::err_drv_argument_not_allowed_with)
Daniel Dunbar4eadb602009-09-10 01:21:12 +000079 << A->getAsString(Args) << "-static";
80}
81
Chris Lattnerbf2803f2010-03-29 17:55:58 +000082// Quote target names for inclusion in GNU Make dependency files.
83// Only the characters '$', '#', ' ', '\t' are quoted.
Chris Lattner0e62c1c2011-07-23 10:55:15 +000084static void QuoteTarget(StringRef Target,
85 SmallVectorImpl<char> &Res) {
Chris Lattnerbf2803f2010-03-29 17:55:58 +000086 for (unsigned i = 0, e = Target.size(); i != e; ++i) {
87 switch (Target[i]) {
88 case ' ':
89 case '\t':
90 // Escape the preceding backslashes
91 for (int j = i - 1; j >= 0 && Target[j] == '\\'; --j)
92 Res.push_back('\\');
93
94 // Escape the space/tab
95 Res.push_back('\\');
96 break;
97 case '$':
98 Res.push_back('$');
99 break;
100 case '#':
101 Res.push_back('\\');
102 break;
103 default:
104 break;
105 }
106
107 Res.push_back(Target[i]);
108 }
109}
110
Daniel Dunbar54423b22010-09-17 00:24:54 +0000111static void AddLinkerInputs(const ToolChain &TC,
112 const InputInfoList &Inputs, const ArgList &Args,
113 ArgStringList &CmdArgs) {
114 const Driver &D = TC.getDriver();
115
Daniel Dunbar1094bb12011-02-19 05:33:51 +0000116 // Add extra linker input arguments which are not treated as inputs
117 // (constructed via -Xarch_).
118 Args.AddAllArgValues(CmdArgs, options::OPT_Zlinker_input);
119
Daniel Dunbar54423b22010-09-17 00:24:54 +0000120 for (InputInfoList::const_iterator
121 it = Inputs.begin(), ie = Inputs.end(); it != ie; ++it) {
122 const InputInfo &II = *it;
123
124 if (!TC.HasNativeLLVMSupport()) {
125 // Don't try to pass LLVM inputs unless we have native support.
126 if (II.getType() == types::TY_LLVM_IR ||
127 II.getType() == types::TY_LTO_IR ||
128 II.getType() == types::TY_LLVM_BC ||
129 II.getType() == types::TY_LTO_BC)
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000130 D.Diag(diag::err_drv_no_linker_llvm_support)
Daniel Dunbar54423b22010-09-17 00:24:54 +0000131 << TC.getTripleString();
132 }
133
Daniel Dunbar2cc3f172010-09-17 00:45:02 +0000134 // Add filenames immediately.
135 if (II.isFilename()) {
Daniel Dunbar54423b22010-09-17 00:24:54 +0000136 CmdArgs.push_back(II.getFilename());
Daniel Dunbar2cc3f172010-09-17 00:45:02 +0000137 continue;
138 }
139
140 // Otherwise, this is a linker input argument.
141 const Arg &A = II.getInputArg();
142
143 // Handle reserved library options.
144 if (A.getOption().matches(options::OPT_Z_reserved_lib_stdcxx)) {
Daniel Dunbar3f7796f2010-09-17 01:20:05 +0000145 TC.AddCXXStdlibLibArgs(Args, CmdArgs);
Shantonu Senafeb03b2010-09-17 18:39:08 +0000146 } else if (A.getOption().matches(options::OPT_Z_reserved_lib_cckext)) {
147 TC.AddCCKextLibArgs(Args, CmdArgs);
Daniel Dunbar2cc3f172010-09-17 00:45:02 +0000148 } else
149 A.renderAsInput(Args, CmdArgs);
Daniel Dunbar54423b22010-09-17 00:24:54 +0000150 }
151}
152
John McCall31168b02011-06-15 23:02:42 +0000153/// \brief Determine whether Objective-C automated reference counting is
154/// enabled.
155static bool isObjCAutoRefCount(const ArgList &Args) {
156 return Args.hasFlag(options::OPT_fobjc_arc, options::OPT_fno_objc_arc, false);
157}
158
Rafael Espindola9d4a8cf2011-06-02 18:58:46 +0000159static void addProfileRT(const ToolChain &TC, const ArgList &Args,
Bill Wendling08760582011-06-27 19:15:03 +0000160 ArgStringList &CmdArgs,
161 llvm::Triple Triple) {
162 if (!(Args.hasArg(options::OPT_fprofile_arcs) ||
163 Args.hasArg(options::OPT_fprofile_generate) ||
164 Args.hasArg(options::OPT_fcreate_profile) ||
165 Args.hasArg(options::OPT_coverage)))
166 return;
167
168 // GCC links libgcov.a by adding -L<inst>/gcc/lib/gcc/<triple>/<ver> -lgcov to
169 // the link line. We cannot do the same thing because unlike gcov there is a
170 // libprofile_rt.so. We used to use the -l:libprofile_rt.a syntax, but that is
171 // not supported by old linkers.
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000172 Twine ProfileRT =
173 Twine(TC.getDriver().Dir) + "/../lib/" + "libprofile_rt.a";
Bill Wendling08760582011-06-27 19:15:03 +0000174
175 if (Triple.getOS() == llvm::Triple::Darwin) {
176 // On Darwin, if the static library doesn't exist try the dylib.
177 bool Exists;
178 if (llvm::sys::fs::exists(ProfileRT.str(), Exists) || !Exists)
179 ProfileRT =
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000180 Twine(TC.getDriver().Dir) + "/../lib/" + "libprofile_rt.dylib";
Rafael Espindola9d4a8cf2011-06-02 18:58:46 +0000181 }
Bill Wendling08760582011-06-27 19:15:03 +0000182
183 CmdArgs.push_back(Args.MakeArgString(ProfileRT));
Rafael Espindola9d4a8cf2011-06-02 18:58:46 +0000184}
185
Mike Stump11289f42009-09-09 15:08:12 +0000186void Clang::AddPreprocessingOptions(const Driver &D,
Douglas Gregor111af7d2009-04-18 00:34:01 +0000187 const ArgList &Args,
Daniel Dunbard067f7f2009-04-08 23:54:23 +0000188 ArgStringList &CmdArgs,
189 const InputInfo &Output,
190 const InputInfoList &Inputs) const {
Daniel Dunbard067f7f2009-04-08 23:54:23 +0000191 Arg *A;
Daniel Dunbar367dbb92009-06-08 21:48:20 +0000192
Daniel Dunbar64198ef2009-09-10 01:21:05 +0000193 CheckPreprocessingOptions(D, Args);
194
195 Args.AddLastArg(CmdArgs, options::OPT_C);
196 Args.AddLastArg(CmdArgs, options::OPT_CC);
Daniel Dunbar367dbb92009-06-08 21:48:20 +0000197
198 // Handle dependency file generation.
Daniel Dunbar86aed7d2010-12-08 21:33:40 +0000199 if ((A = Args.getLastArg(options::OPT_M, options::OPT_MM)) ||
Daniel Dunbard067f7f2009-04-08 23:54:23 +0000200 (A = Args.getLastArg(options::OPT_MD)) ||
201 (A = Args.getLastArg(options::OPT_MMD))) {
202 // Determine the output location.
203 const char *DepFile;
204 if (Output.getType() == types::TY_Dependencies) {
Daniel Dunbarb440f562010-08-02 02:38:21 +0000205 DepFile = Output.getFilename();
Daniel Dunbard067f7f2009-04-08 23:54:23 +0000206 } else if (Arg *MF = Args.getLastArg(options::OPT_MF)) {
207 DepFile = MF->getValue(Args);
Daniel Dunbar0bfb21e2009-11-19 03:26:40 +0000208 } else if (A->getOption().matches(options::OPT_M) ||
209 A->getOption().matches(options::OPT_MM)) {
Daniel Dunbard067f7f2009-04-08 23:54:23 +0000210 DepFile = "-";
211 } else {
212 DepFile = darwin::CC1::getDependencyFileName(Args, Inputs);
213 }
214 CmdArgs.push_back("-dependency-file");
215 CmdArgs.push_back(DepFile);
216
Chris Lattnerbf2803f2010-03-29 17:55:58 +0000217 // Add a default target if one wasn't specified.
Daniel Dunbard067f7f2009-04-08 23:54:23 +0000218 if (!Args.hasArg(options::OPT_MT) && !Args.hasArg(options::OPT_MQ)) {
219 const char *DepTarget;
220
221 // If user provided -o, that is the dependency target, except
222 // when we are only generating a dependency file.
223 Arg *OutputOpt = Args.getLastArg(options::OPT_o);
224 if (OutputOpt && Output.getType() != types::TY_Dependencies) {
225 DepTarget = OutputOpt->getValue(Args);
226 } else {
227 // Otherwise derive from the base input.
228 //
229 // FIXME: This should use the computed output file location.
Michael J. Spencere1696752010-12-18 00:19:12 +0000230 llvm::SmallString<128> P(Inputs[0].getBaseInput());
231 llvm::sys::path::replace_extension(P, "o");
232 DepTarget = Args.MakeArgString(llvm::sys::path::filename(P));
Daniel Dunbard067f7f2009-04-08 23:54:23 +0000233 }
234
235 CmdArgs.push_back("-MT");
Chris Lattnerbf2803f2010-03-29 17:55:58 +0000236 llvm::SmallString<128> Quoted;
237 QuoteTarget(DepTarget, Quoted);
238 CmdArgs.push_back(Args.MakeArgString(Quoted));
Daniel Dunbard067f7f2009-04-08 23:54:23 +0000239 }
240
Daniel Dunbar0bfb21e2009-11-19 03:26:40 +0000241 if (A->getOption().matches(options::OPT_M) ||
242 A->getOption().matches(options::OPT_MD))
Daniel Dunbard067f7f2009-04-08 23:54:23 +0000243 CmdArgs.push_back("-sys-header-deps");
244 }
245
Peter Collingbourne77b0e7f22011-07-12 19:35:15 +0000246 if (Args.hasArg(options::OPT_MG)) {
247 if (!A || A->getOption().matches(options::OPT_MD) ||
248 A->getOption().matches(options::OPT_MMD))
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000249 D.Diag(diag::err_drv_mg_requires_m_or_mm);
Peter Collingbourne77b0e7f22011-07-12 19:35:15 +0000250 CmdArgs.push_back("-MG");
251 }
252
Daniel Dunbard067f7f2009-04-08 23:54:23 +0000253 Args.AddLastArg(CmdArgs, options::OPT_MP);
Chris Lattnerbf2803f2010-03-29 17:55:58 +0000254
255 // Convert all -MQ <target> args to -MT <quoted target>
256 for (arg_iterator it = Args.filtered_begin(options::OPT_MT,
257 options::OPT_MQ),
258 ie = Args.filtered_end(); it != ie; ++it) {
Daniel Dunbara442fd52010-06-11 22:00:13 +0000259 const Arg *A = *it;
260 A->claim();
Chris Lattnerbf2803f2010-03-29 17:55:58 +0000261
Daniel Dunbara442fd52010-06-11 22:00:13 +0000262 if (A->getOption().matches(options::OPT_MQ)) {
Chris Lattnerbf2803f2010-03-29 17:55:58 +0000263 CmdArgs.push_back("-MT");
264 llvm::SmallString<128> Quoted;
Daniel Dunbara442fd52010-06-11 22:00:13 +0000265 QuoteTarget(A->getValue(Args), Quoted);
Chris Lattnerbf2803f2010-03-29 17:55:58 +0000266 CmdArgs.push_back(Args.MakeArgString(Quoted));
267
268 // -MT flag - no change
269 } else {
Daniel Dunbara442fd52010-06-11 22:00:13 +0000270 A->render(Args, CmdArgs);
Chris Lattnerbf2803f2010-03-29 17:55:58 +0000271 }
272 }
Daniel Dunbard067f7f2009-04-08 23:54:23 +0000273
Douglas Gregor111af7d2009-04-18 00:34:01 +0000274 // Add -i* options, and automatically translate to
275 // -include-pch/-include-pth for transparent PCH support. It's
276 // wonky, but we include looking for .gch so we can support seamless
277 // replacement into a build system already set up to be generating
278 // .gch files.
Argyrios Kyrtzidis2f23b412010-09-30 16:53:47 +0000279 bool RenderedImplicitInclude = false;
Daniel Dunbar44b36ee2009-11-25 11:53:23 +0000280 for (arg_iterator it = Args.filtered_begin(options::OPT_clang_i_Group),
281 ie = Args.filtered_end(); it != ie; ++it) {
282 const Arg *A = it;
Daniel Dunbard067f7f2009-04-08 23:54:23 +0000283
284 if (A->getOption().matches(options::OPT_include)) {
Argyrios Kyrtzidis2f23b412010-09-30 16:53:47 +0000285 bool IsFirstImplicitInclude = !RenderedImplicitInclude;
286 RenderedImplicitInclude = true;
287
Argyrios Kyrtzidis90bdfbb2010-08-11 23:27:58 +0000288 // Use PCH if the user requested it.
Daniel Dunbarcbc34b72009-10-15 20:02:44 +0000289 bool UsePCH = D.CCCUsePCH;
Daniel Dunbarcbc34b72009-10-15 20:02:44 +0000290
Daniel Dunbard067f7f2009-04-08 23:54:23 +0000291 bool FoundPTH = false;
Douglas Gregor111af7d2009-04-18 00:34:01 +0000292 bool FoundPCH = false;
Daniel Dunbard067f7f2009-04-08 23:54:23 +0000293 llvm::sys::Path P(A->getValue(Args));
Michael J. Spencerf6efe582011-01-10 02:34:13 +0000294 bool Exists;
Daniel Dunbarcbc34b72009-10-15 20:02:44 +0000295 if (UsePCH) {
Douglas Gregor111af7d2009-04-18 00:34:01 +0000296 P.appendSuffix("pch");
Michael J. Spencerf6efe582011-01-10 02:34:13 +0000297 if (!llvm::sys::fs::exists(P.str(), Exists) && Exists)
Douglas Gregor111af7d2009-04-18 00:34:01 +0000298 FoundPCH = true;
Mike Stump11289f42009-09-09 15:08:12 +0000299 else
Douglas Gregor111af7d2009-04-18 00:34:01 +0000300 P.eraseSuffix();
Daniel Dunbard067f7f2009-04-08 23:54:23 +0000301 }
302
Douglas Gregor111af7d2009-04-18 00:34:01 +0000303 if (!FoundPCH) {
304 P.appendSuffix("pth");
Michael J. Spencerf6efe582011-01-10 02:34:13 +0000305 if (!llvm::sys::fs::exists(P.str(), Exists) && Exists)
Douglas Gregor111af7d2009-04-18 00:34:01 +0000306 FoundPTH = true;
307 else
308 P.eraseSuffix();
Mike Stump11289f42009-09-09 15:08:12 +0000309 }
310
Douglas Gregor111af7d2009-04-18 00:34:01 +0000311 if (!FoundPCH && !FoundPTH) {
312 P.appendSuffix("gch");
Michael J. Spencerf6efe582011-01-10 02:34:13 +0000313 if (!llvm::sys::fs::exists(P.str(), Exists) && Exists) {
Daniel Dunbarcbc34b72009-10-15 20:02:44 +0000314 FoundPCH = UsePCH;
315 FoundPTH = !UsePCH;
Douglas Gregor111af7d2009-04-18 00:34:01 +0000316 }
Mike Stump11289f42009-09-09 15:08:12 +0000317 else
Douglas Gregor111af7d2009-04-18 00:34:01 +0000318 P.eraseSuffix();
319 }
320
321 if (FoundPCH || FoundPTH) {
Argyrios Kyrtzidis2f23b412010-09-30 16:53:47 +0000322 if (IsFirstImplicitInclude) {
323 A->claim();
324 if (UsePCH)
325 CmdArgs.push_back("-include-pch");
326 else
327 CmdArgs.push_back("-include-pth");
328 CmdArgs.push_back(Args.MakeArgString(P.str()));
329 continue;
330 } else {
331 // Ignore the PCH if not first on command line and emit warning.
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000332 D.Diag(diag::warn_drv_pch_not_first_include)
Argyrios Kyrtzidis2f23b412010-09-30 16:53:47 +0000333 << P.str() << A->getAsString(Args);
334 }
Daniel Dunbard067f7f2009-04-08 23:54:23 +0000335 }
336 }
337
338 // Not translated, render as usual.
339 A->claim();
340 A->render(Args, CmdArgs);
341 }
342
343 Args.AddAllArgs(CmdArgs, options::OPT_D, options::OPT_U);
Douglas Gregor9f93e382011-07-28 04:45:53 +0000344 Args.AddAllArgs(CmdArgs, options::OPT_I_Group, options::OPT_F,
345 options::OPT_index_header_map);
Daniel Dunbard067f7f2009-04-08 23:54:23 +0000346
Daniel Dunbarbf11f792010-09-14 23:12:35 +0000347 // Add C++ include arguments, if needed.
348 types::ID InputType = Inputs[0].getType();
John McCall31168b02011-06-15 23:02:42 +0000349 if (types::isCXX(InputType)) {
350 bool ObjCXXAutoRefCount
351 = types::isObjC(InputType) && isObjCAutoRefCount(Args);
Eric Christopher84fbdb42011-08-19 00:30:14 +0000352 getToolChain().AddClangCXXStdlibIncludeArgs(Args, CmdArgs,
John McCall31168b02011-06-15 23:02:42 +0000353 ObjCXXAutoRefCount);
Bob Wilsonb02ea3d2011-06-21 21:12:29 +0000354 Args.AddAllArgs(CmdArgs, options::OPT_stdlib_EQ);
John McCall31168b02011-06-15 23:02:42 +0000355 }
Daniel Dunbarbf11f792010-09-14 23:12:35 +0000356
Daniel Dunbard067f7f2009-04-08 23:54:23 +0000357 // Add -Wp, and -Xassembler if using the preprocessor.
358
359 // FIXME: There is a very unfortunate problem here, some troubled
360 // souls abuse -Wp, to pass preprocessor options in gcc syntax. To
361 // really support that we would have to parse and then translate
362 // those options. :(
363 Args.AddAllArgValues(CmdArgs, options::OPT_Wp_COMMA,
364 options::OPT_Xpreprocessor);
Daniel Dunbar38b62792009-10-29 01:53:44 +0000365
366 // -I- is a deprecated GCC feature, reject it.
367 if (Arg *A = Args.getLastArg(options::OPT_I_))
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000368 D.Diag(diag::err_drv_I_dash_not_supported) << A->getAsString(Args);
Chandler Carruth24e17e12010-10-20 07:00:47 +0000369
370 // If we have a --sysroot, and don't have an explicit -isysroot flag, add an
371 // -isysroot to the CC1 invocation.
372 if (Arg *A = Args.getLastArg(options::OPT__sysroot_EQ)) {
373 if (!Args.hasArg(options::OPT_isysroot)) {
374 CmdArgs.push_back("-isysroot");
375 CmdArgs.push_back(A->getValue(Args));
376 }
377 }
Daniel Dunbard067f7f2009-04-08 23:54:23 +0000378}
379
Chris Lattner57540c52011-04-15 05:22:18 +0000380/// getARMTargetCPU - Get the (LLVM) name of the ARM cpu we are targeting.
Daniel Dunbarfb58b0a2009-09-10 06:49:20 +0000381//
382// FIXME: tblgen this.
Rafael Espindola0e1fb4f2010-06-28 17:18:09 +0000383static const char *getARMTargetCPU(const ArgList &Args,
384 const llvm::Triple &Triple) {
Daniel Dunbarfb58b0a2009-09-10 06:49:20 +0000385 // FIXME: Warn on inconsistent use of -mcpu and -march.
Daniel Dunbar0f5c5422009-09-10 04:57:17 +0000386
387 // If we have -mcpu=, use that.
388 if (Arg *A = Args.getLastArg(options::OPT_mcpu_EQ))
389 return A->getValue(Args);
390
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000391 StringRef MArch;
Daniel Dunbar0f5c5422009-09-10 04:57:17 +0000392 if (Arg *A = Args.getLastArg(options::OPT_march_EQ)) {
Rafael Espindola0e1fb4f2010-06-28 17:18:09 +0000393 // Otherwise, if we have -march= choose the base CPU for that arch.
394 MArch = A->getValue(Args);
395 } else {
396 // Otherwise, use the Arch from the triple.
397 MArch = Triple.getArchName();
Daniel Dunbar0f5c5422009-09-10 04:57:17 +0000398 }
399
Rafael Espindola0e1fb4f2010-06-28 17:18:09 +0000400 if (MArch == "armv2" || MArch == "armv2a")
401 return "arm2";
402 if (MArch == "armv3")
403 return "arm6";
404 if (MArch == "armv3m")
405 return "arm7m";
406 if (MArch == "armv4" || MArch == "armv4t")
407 return "arm7tdmi";
408 if (MArch == "armv5" || MArch == "armv5t")
409 return "arm10tdmi";
410 if (MArch == "armv5e" || MArch == "armv5te")
411 return "arm1026ejs";
412 if (MArch == "armv5tej")
413 return "arm926ej-s";
414 if (MArch == "armv6" || MArch == "armv6k")
415 return "arm1136jf-s";
416 if (MArch == "armv6j")
417 return "arm1136j-s";
418 if (MArch == "armv6z" || MArch == "armv6zk")
419 return "arm1176jzf-s";
420 if (MArch == "armv6t2")
421 return "arm1156t2-s";
422 if (MArch == "armv7" || MArch == "armv7a" || MArch == "armv7-a")
423 return "cortex-a8";
424 if (MArch == "armv7r" || MArch == "armv7-r")
425 return "cortex-r4";
426 if (MArch == "armv7m" || MArch == "armv7-m")
427 return "cortex-m3";
428 if (MArch == "ep9312")
429 return "ep9312";
430 if (MArch == "iwmmxt")
431 return "iwmmxt";
432 if (MArch == "xscale")
433 return "xscale";
Bob Wilsond9249412011-03-21 20:40:05 +0000434 if (MArch == "armv6m" || MArch == "armv6-m")
435 return "cortex-m0";
Rafael Espindola0e1fb4f2010-06-28 17:18:09 +0000436
437 // If all else failed, return the most base CPU LLVM supports.
Daniel Dunbar0f5c5422009-09-10 04:57:17 +0000438 return "arm7tdmi";
439}
440
Daniel Dunbarf492c922009-09-10 22:59:51 +0000441/// getLLVMArchSuffixForARM - Get the LLVM arch name to use for a particular
Daniel Dunbarfb58b0a2009-09-10 06:49:20 +0000442/// CPU.
443//
444// FIXME: This is redundant with -mcpu, why does LLVM use this.
445// FIXME: tblgen this, or kill it!
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000446static const char *getLLVMArchSuffixForARM(StringRef CPU) {
Daniel Dunbarfb58b0a2009-09-10 06:49:20 +0000447 if (CPU == "arm7tdmi" || CPU == "arm7tdmi-s" || CPU == "arm710t" ||
448 CPU == "arm720t" || CPU == "arm9" || CPU == "arm9tdmi" ||
449 CPU == "arm920" || CPU == "arm920t" || CPU == "arm922t" ||
450 CPU == "arm940t" || CPU == "ep9312")
Daniel Dunbarf492c922009-09-10 22:59:51 +0000451 return "v4t";
Daniel Dunbarfb58b0a2009-09-10 06:49:20 +0000452
453 if (CPU == "arm10tdmi" || CPU == "arm1020t")
Daniel Dunbarf492c922009-09-10 22:59:51 +0000454 return "v5";
Daniel Dunbarfb58b0a2009-09-10 06:49:20 +0000455
456 if (CPU == "arm9e" || CPU == "arm926ej-s" || CPU == "arm946e-s" ||
457 CPU == "arm966e-s" || CPU == "arm968e-s" || CPU == "arm10e" ||
458 CPU == "arm1020e" || CPU == "arm1022e" || CPU == "xscale" ||
459 CPU == "iwmmxt")
Daniel Dunbarf492c922009-09-10 22:59:51 +0000460 return "v5e";
Daniel Dunbarfb58b0a2009-09-10 06:49:20 +0000461
462 if (CPU == "arm1136j-s" || CPU == "arm1136jf-s" || CPU == "arm1176jz-s" ||
463 CPU == "arm1176jzf-s" || CPU == "mpcorenovfp" || CPU == "mpcore")
Daniel Dunbarf492c922009-09-10 22:59:51 +0000464 return "v6";
Daniel Dunbarfb58b0a2009-09-10 06:49:20 +0000465
466 if (CPU == "arm1156t2-s" || CPU == "arm1156t2f-s")
Daniel Dunbarf492c922009-09-10 22:59:51 +0000467 return "v6t2";
Daniel Dunbarfb58b0a2009-09-10 06:49:20 +0000468
469 if (CPU == "cortex-a8" || CPU == "cortex-a9")
Daniel Dunbarf492c922009-09-10 22:59:51 +0000470 return "v7";
Daniel Dunbarfb58b0a2009-09-10 06:49:20 +0000471
Daniel Dunbarf492c922009-09-10 22:59:51 +0000472 return "";
Daniel Dunbarfb58b0a2009-09-10 06:49:20 +0000473}
474
Daniel Dunbard609b7b2009-11-17 06:37:03 +0000475// FIXME: Move to target hook.
476static bool isSignedCharDefault(const llvm::Triple &Triple) {
477 switch (Triple.getArch()) {
478 default:
479 return true;
480
Jim Grosbach7c2c6642011-05-24 15:40:46 +0000481 case llvm::Triple::arm:
Daniel Dunbard609b7b2009-11-17 06:37:03 +0000482 case llvm::Triple::ppc:
483 case llvm::Triple::ppc64:
484 if (Triple.getOS() == llvm::Triple::Darwin)
485 return true;
486 return false;
487
488 case llvm::Triple::systemz:
489 return false;
490 }
491}
492
Daniel Dunbar0f5c5422009-09-10 04:57:17 +0000493void Clang::AddARMTargetArgs(const ArgList &Args,
Daniel Dunbarc9388c12011-03-17 17:10:06 +0000494 ArgStringList &CmdArgs,
495 bool KernelOrKext) const {
Daniel Dunbar083edf72009-12-21 18:54:17 +0000496 const Driver &D = getToolChain().getDriver();
Rafael Espindola0e1fb4f2010-06-28 17:18:09 +0000497 llvm::Triple Triple = getToolChain().getTriple();
Daniel Dunbar78485922009-09-10 23:00:09 +0000498
Daniel Dunbar908b4852011-03-02 00:55:57 +0000499 // Disable movt generation, if requested.
500#ifdef DISABLE_ARM_DARWIN_USE_MOVT
Daniel Dunbar12100e22011-03-22 16:48:17 +0000501 CmdArgs.push_back("-backend-option");
Daniel Dunbar908b4852011-03-02 00:55:57 +0000502 CmdArgs.push_back("-arm-darwin-use-movt=0");
503#endif
504
Daniel Dunbar4ed78982009-09-14 00:34:46 +0000505 // Select the ABI to use.
506 //
507 // FIXME: Support -meabi.
508 const char *ABIName = 0;
509 if (Arg *A = Args.getLastArg(options::OPT_mabi_EQ)) {
510 ABIName = A->getValue(Args);
511 } else {
512 // Select the default based on the platform.
Bob Wilsond1447c42011-02-04 17:59:28 +0000513 switch(Triple.getEnvironment()) {
514 case llvm::Triple::GNUEABI:
Daniel Dunbar4ed78982009-09-14 00:34:46 +0000515 ABIName = "aapcs-linux";
Bob Wilsond1447c42011-02-04 17:59:28 +0000516 break;
517 case llvm::Triple::EABI:
Rafael Espindola23a8a062010-06-16 19:01:17 +0000518 ABIName = "aapcs";
Bob Wilsond1447c42011-02-04 17:59:28 +0000519 break;
520 default:
Rafael Espindola23a8a062010-06-16 19:01:17 +0000521 ABIName = "apcs-gnu";
Bob Wilsond1447c42011-02-04 17:59:28 +0000522 }
Daniel Dunbar4ed78982009-09-14 00:34:46 +0000523 }
524 CmdArgs.push_back("-target-abi");
525 CmdArgs.push_back(ABIName);
526
Daniel Dunbar0f5c5422009-09-10 04:57:17 +0000527 // Set the CPU based on -march= and -mcpu=.
Daniel Dunbara7d02312009-12-18 06:30:12 +0000528 CmdArgs.push_back("-target-cpu");
Rafael Espindola0e1fb4f2010-06-28 17:18:09 +0000529 CmdArgs.push_back(getARMTargetCPU(Args, Triple));
Daniel Dunbar0f5c5422009-09-10 04:57:17 +0000530
Daniel Dunbar78485922009-09-10 23:00:09 +0000531 // Select the float ABI as determined by -msoft-float, -mhard-float, and
532 // -mfloat-abi=.
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000533 StringRef FloatABI;
Daniel Dunbar78485922009-09-10 23:00:09 +0000534 if (Arg *A = Args.getLastArg(options::OPT_msoft_float,
535 options::OPT_mhard_float,
536 options::OPT_mfloat_abi_EQ)) {
537 if (A->getOption().matches(options::OPT_msoft_float))
538 FloatABI = "soft";
539 else if (A->getOption().matches(options::OPT_mhard_float))
540 FloatABI = "hard";
541 else {
542 FloatABI = A->getValue(Args);
543 if (FloatABI != "soft" && FloatABI != "softfp" && FloatABI != "hard") {
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000544 D.Diag(diag::err_drv_invalid_mfloat_abi)
Daniel Dunbar78485922009-09-10 23:00:09 +0000545 << A->getAsString(Args);
546 FloatABI = "soft";
547 }
548 }
549 }
550
551 // If unspecified, choose the default based on the platform.
552 if (FloatABI.empty()) {
Rafael Espindola0e1fb4f2010-06-28 17:18:09 +0000553 const llvm::Triple &Triple = getToolChain().getTriple();
554 switch (Triple.getOS()) {
Daniel Dunbar78485922009-09-10 23:00:09 +0000555 case llvm::Triple::Darwin: {
556 // Darwin defaults to "softfp" for v6 and v7.
557 //
558 // FIXME: Factor out an ARM class so we can cache the arch somewhere.
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000559 StringRef ArchName =
Rafael Espindola0e1fb4f2010-06-28 17:18:09 +0000560 getLLVMArchSuffixForARM(getARMTargetCPU(Args, Triple));
Daniel Dunbar78485922009-09-10 23:00:09 +0000561 if (ArchName.startswith("v6") || ArchName.startswith("v7"))
562 FloatABI = "softfp";
563 else
564 FloatABI = "soft";
565 break;
566 }
567
Rafael Espindolab1ef8ff2010-06-27 18:29:21 +0000568 case llvm::Triple::Linux: {
Bob Wilsond1447c42011-02-04 17:59:28 +0000569 if (getToolChain().getTriple().getEnvironment() == llvm::Triple::GNUEABI) {
Rafael Espindolab1ef8ff2010-06-27 18:29:21 +0000570 FloatABI = "softfp";
571 break;
572 }
573 }
574 // fall through
575
Daniel Dunbar78485922009-09-10 23:00:09 +0000576 default:
Bob Wilsond1447c42011-02-04 17:59:28 +0000577 switch(Triple.getEnvironment()) {
578 case llvm::Triple::GNUEABI:
579 FloatABI = "softfp";
580 break;
581 case llvm::Triple::EABI:
582 // EABI is always AAPCS, and if it was not marked 'hard', it's softfp
583 FloatABI = "softfp";
584 break;
585 default:
586 // Assume "soft", but warn the user we are guessing.
587 FloatABI = "soft";
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000588 D.Diag(diag::warn_drv_assuming_mfloat_abi_is) << "soft";
Bob Wilsond1447c42011-02-04 17:59:28 +0000589 break;
590 }
Daniel Dunbar78485922009-09-10 23:00:09 +0000591 }
592 }
593
594 if (FloatABI == "soft") {
595 // Floating point operations and argument passing are soft.
596 //
597 // FIXME: This changes CPP defines, we need -target-soft-float.
Daniel Dunbara74f8ff2009-11-30 08:42:00 +0000598 CmdArgs.push_back("-msoft-float");
Daniel Dunbar6cc525b2009-12-08 19:49:51 +0000599 CmdArgs.push_back("-mfloat-abi");
600 CmdArgs.push_back("soft");
Daniel Dunbar78485922009-09-10 23:00:09 +0000601 } else if (FloatABI == "softfp") {
602 // Floating point operations are hard, but argument passing is soft.
Daniel Dunbar6cc525b2009-12-08 19:49:51 +0000603 CmdArgs.push_back("-mfloat-abi");
604 CmdArgs.push_back("soft");
Daniel Dunbar78485922009-09-10 23:00:09 +0000605 } else {
606 // Floating point operations and argument passing are hard.
607 assert(FloatABI == "hard" && "Invalid float abi!");
Daniel Dunbar6cc525b2009-12-08 19:49:51 +0000608 CmdArgs.push_back("-mfloat-abi");
609 CmdArgs.push_back("hard");
Daniel Dunbar78485922009-09-10 23:00:09 +0000610 }
Daniel Dunbar893d4752009-12-19 04:15:38 +0000611
612 // Set appropriate target features for floating point mode.
613 //
614 // FIXME: Note, this is a hack, the LLVM backend doesn't actually use these
615 // yet (it uses the -mfloat-abi and -msoft-float options above), and it is
616 // stripped out by the ARM target.
617
618 // Use software floating point operations?
619 if (FloatABI == "soft") {
620 CmdArgs.push_back("-target-feature");
621 CmdArgs.push_back("+soft-float");
622 }
623
624 // Use software floating point argument passing?
625 if (FloatABI != "hard") {
626 CmdArgs.push_back("-target-feature");
627 CmdArgs.push_back("+soft-float-abi");
628 }
Daniel Dunbar0def3d12009-12-21 23:28:17 +0000629
630 // Honor -mfpu=.
631 //
632 // FIXME: Centralize feature selection, defaulting shouldn't be also in the
633 // frontend target.
634 if (const Arg *A = Args.getLastArg(options::OPT_mfpu_EQ)) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000635 StringRef FPU = A->getValue(Args);
Daniel Dunbar0def3d12009-12-21 23:28:17 +0000636
637 // Set the target features based on the FPU.
638 if (FPU == "fpa" || FPU == "fpe2" || FPU == "fpe3" || FPU == "maverick") {
639 // Disable any default FPU support.
640 CmdArgs.push_back("-target-feature");
641 CmdArgs.push_back("-vfp2");
642 CmdArgs.push_back("-target-feature");
643 CmdArgs.push_back("-vfp3");
644 CmdArgs.push_back("-target-feature");
645 CmdArgs.push_back("-neon");
646 } else if (FPU == "vfp") {
647 CmdArgs.push_back("-target-feature");
648 CmdArgs.push_back("+vfp2");
649 } else if (FPU == "vfp3") {
650 CmdArgs.push_back("-target-feature");
651 CmdArgs.push_back("+vfp3");
652 } else if (FPU == "neon") {
653 CmdArgs.push_back("-target-feature");
654 CmdArgs.push_back("+neon");
655 } else
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000656 D.Diag(diag::err_drv_clang_unsupported) << A->getAsString(Args);
Daniel Dunbar0def3d12009-12-21 23:28:17 +0000657 }
Daniel Dunbarb1db4b62011-03-17 00:07:34 +0000658
659 // Setting -msoft-float effectively disables NEON because of the GCC
660 // implementation, although the same isn't true of VFP or VFP3.
661 if (FloatABI == "soft") {
Daniel Dunbarc9388c12011-03-17 17:10:06 +0000662 CmdArgs.push_back("-target-feature");
663 CmdArgs.push_back("-neon");
664 }
665
666 // Kernel code has more strict alignment requirements.
667 if (KernelOrKext) {
Daniel Dunbar12100e22011-03-22 16:48:17 +0000668 CmdArgs.push_back("-backend-option");
Daniel Dunbarc9388c12011-03-17 17:10:06 +0000669 CmdArgs.push_back("-arm-long-calls");
670
Daniel Dunbar12100e22011-03-22 16:48:17 +0000671 CmdArgs.push_back("-backend-option");
Daniel Dunbarc9388c12011-03-17 17:10:06 +0000672 CmdArgs.push_back("-arm-strict-align");
Daniel Dunbared904c82011-04-18 21:26:42 +0000673
674 // The kext linker doesn't know how to deal with movw/movt.
675#ifndef DISABLE_ARM_DARWIN_USE_MOVT
676 CmdArgs.push_back("-backend-option");
677 CmdArgs.push_back("-arm-darwin-use-movt=0");
678#endif
Daniel Dunbarb1db4b62011-03-17 00:07:34 +0000679 }
Chad Rosierba3df1d2011-08-26 00:26:29 +0000680
681 // Setting -mno-global-merge disables the codegen global merge pass. Setting
682 // -mglobal-merge has no effect as the pass is enabled by default.
683 if (Arg *A = Args.getLastArg(options::OPT_mglobal_merge,
684 options::OPT_mno_global_merge)) {
685 if (A->getOption().matches(options::OPT_mno_global_merge))
686 CmdArgs.push_back("-mno-global-merge");
687 }
Daniel Dunbar0f5c5422009-09-10 04:57:17 +0000688}
689
Eric Christopher0b26a612010-03-02 02:41:08 +0000690void Clang::AddMIPSTargetArgs(const ArgList &Args,
691 ArgStringList &CmdArgs) const {
692 const Driver &D = getToolChain().getDriver();
693
694 // Select the ABI to use.
695 const char *ABIName = 0;
696 if (Arg *A = Args.getLastArg(options::OPT_mabi_EQ)) {
697 ABIName = A->getValue(Args);
698 } else {
699 ABIName = "o32";
700 }
701
702 CmdArgs.push_back("-target-abi");
703 CmdArgs.push_back(ABIName);
704
705 if (const Arg *A = Args.getLastArg(options::OPT_march_EQ)) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000706 StringRef MArch = A->getValue(Args);
Eric Christopher0b26a612010-03-02 02:41:08 +0000707 CmdArgs.push_back("-target-cpu");
708
709 if ((MArch == "r2000") || (MArch == "r3000"))
710 CmdArgs.push_back("mips1");
711 else if (MArch == "r6000")
712 CmdArgs.push_back("mips2");
713 else
Nick Lewycky36d8f052011-05-04 03:44:01 +0000714 CmdArgs.push_back(Args.MakeArgString(MArch));
Eric Christopher0b26a612010-03-02 02:41:08 +0000715 }
716
717 // Select the float ABI as determined by -msoft-float, -mhard-float, and
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000718 StringRef FloatABI;
Eric Christopher0b26a612010-03-02 02:41:08 +0000719 if (Arg *A = Args.getLastArg(options::OPT_msoft_float,
720 options::OPT_mhard_float)) {
721 if (A->getOption().matches(options::OPT_msoft_float))
722 FloatABI = "soft";
723 else if (A->getOption().matches(options::OPT_mhard_float))
724 FloatABI = "hard";
725 }
726
727 // If unspecified, choose the default based on the platform.
728 if (FloatABI.empty()) {
Benjamin Kramerf41ccef2010-04-08 15:44:22 +0000729 // Assume "soft", but warn the user we are guessing.
730 FloatABI = "soft";
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000731 D.Diag(diag::warn_drv_assuming_mfloat_abi_is) << "soft";
Eric Christopher0b26a612010-03-02 02:41:08 +0000732 }
733
734 if (FloatABI == "soft") {
735 // Floating point operations and argument passing are soft.
736 //
737 // FIXME: This changes CPP defines, we need -target-soft-float.
738 CmdArgs.push_back("-msoft-float");
739 } else {
740 assert(FloatABI == "hard" && "Invalid float abi!");
741 CmdArgs.push_back("-mhard-float");
742 }
743}
744
Bruno Cardoso Lopese7f211c2010-11-09 17:21:19 +0000745void Clang::AddSparcTargetArgs(const ArgList &Args,
746 ArgStringList &CmdArgs) const {
747 const Driver &D = getToolChain().getDriver();
748
749 if (const Arg *A = Args.getLastArg(options::OPT_march_EQ)) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000750 StringRef MArch = A->getValue(Args);
Bruno Cardoso Lopese7f211c2010-11-09 17:21:19 +0000751 CmdArgs.push_back("-target-cpu");
752 CmdArgs.push_back(MArch.str().c_str());
753 }
754
755 // Select the float ABI as determined by -msoft-float, -mhard-float, and
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000756 StringRef FloatABI;
Bruno Cardoso Lopese7f211c2010-11-09 17:21:19 +0000757 if (Arg *A = Args.getLastArg(options::OPT_msoft_float,
758 options::OPT_mhard_float)) {
759 if (A->getOption().matches(options::OPT_msoft_float))
760 FloatABI = "soft";
761 else if (A->getOption().matches(options::OPT_mhard_float))
762 FloatABI = "hard";
763 }
764
765 // If unspecified, choose the default based on the platform.
766 if (FloatABI.empty()) {
767 switch (getToolChain().getTriple().getOS()) {
768 default:
769 // Assume "soft", but warn the user we are guessing.
770 FloatABI = "soft";
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000771 D.Diag(diag::warn_drv_assuming_mfloat_abi_is) << "soft";
Bruno Cardoso Lopese7f211c2010-11-09 17:21:19 +0000772 break;
773 }
774 }
775
776 if (FloatABI == "soft") {
777 // Floating point operations and argument passing are soft.
778 //
779 // FIXME: This changes CPP defines, we need -target-soft-float.
780 CmdArgs.push_back("-msoft-float");
Bruno Cardoso Lopese7f211c2010-11-09 17:21:19 +0000781 CmdArgs.push_back("-target-feature");
782 CmdArgs.push_back("+soft-float");
783 } else {
784 assert(FloatABI == "hard" && "Invalid float abi!");
785 CmdArgs.push_back("-mhard-float");
786 }
787}
788
Daniel Dunbar3b3191f2009-09-09 22:33:08 +0000789void Clang::AddX86TargetArgs(const ArgList &Args,
790 ArgStringList &CmdArgs) const {
Daniel Dunbare2cf8f72009-09-10 22:59:57 +0000791 if (!Args.hasFlag(options::OPT_mred_zone,
792 options::OPT_mno_red_zone,
793 true) ||
794 Args.hasArg(options::OPT_mkernel) ||
795 Args.hasArg(options::OPT_fapple_kext))
Daniel Dunbar8bed86c2009-11-20 22:21:36 +0000796 CmdArgs.push_back("-disable-red-zone");
Daniel Dunbare2cf8f72009-09-10 22:59:57 +0000797
Daniel Dunbare2cf8f72009-09-10 22:59:57 +0000798 if (Args.hasFlag(options::OPT_msoft_float,
799 options::OPT_mno_soft_float,
800 false))
Daniel Dunbar8bed86c2009-11-20 22:21:36 +0000801 CmdArgs.push_back("-no-implicit-float");
Daniel Dunbare2cf8f72009-09-10 22:59:57 +0000802
Daniel Dunbare13ada62009-11-14 22:04:54 +0000803 const char *CPUName = 0;
Daniel Dunbar3b3191f2009-09-09 22:33:08 +0000804 if (const Arg *A = Args.getLastArg(options::OPT_march_EQ)) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000805 if (StringRef(A->getValue(Args)) == "native") {
Daniel Dunbare13ada62009-11-14 22:04:54 +0000806 // FIXME: Reject attempts to use -march=native unless the target matches
807 // the host.
808 //
809 // FIXME: We should also incorporate the detected target features for use
810 // with -native.
811 std::string CPU = llvm::sys::getHostCPUName();
812 if (!CPU.empty())
813 CPUName = Args.MakeArgString(CPU);
814 } else
815 CPUName = A->getValue(Args);
816 }
Daniel Dunbar3b3191f2009-09-09 22:33:08 +0000817
Daniel Dunbare13ada62009-11-14 22:04:54 +0000818 // Select the default CPU if none was given (or detection failed).
819 if (!CPUName) {
Daniel Dunbar3b3191f2009-09-09 22:33:08 +0000820 // FIXME: Need target hooks.
Benjamin Kramer842bf172010-01-30 15:01:47 +0000821 if (getToolChain().getOS().startswith("darwin")) {
Daniel Dunbar116b3052011-05-31 15:58:55 +0000822 if (getToolChain().getArch() == llvm::Triple::x86_64)
Daniel Dunbare13ada62009-11-14 22:04:54 +0000823 CPUName = "core2";
Daniel Dunbar116b3052011-05-31 15:58:55 +0000824 else if (getToolChain().getArch() == llvm::Triple::x86)
Daniel Dunbare13ada62009-11-14 22:04:54 +0000825 CPUName = "yonah";
Chris Lattnerb986aba2010-04-11 19:29:39 +0000826 } else if (getToolChain().getOS().startswith("haiku")) {
Daniel Dunbar116b3052011-05-31 15:58:55 +0000827 if (getToolChain().getArch() == llvm::Triple::x86_64)
Chris Lattnerb986aba2010-04-11 19:29:39 +0000828 CPUName = "x86-64";
Daniel Dunbar116b3052011-05-31 15:58:55 +0000829 else if (getToolChain().getArch() == llvm::Triple::x86)
Chris Lattnerb986aba2010-04-11 19:29:39 +0000830 CPUName = "i586";
Daniel Dunbarea3813f2010-08-01 23:13:54 +0000831 } else if (getToolChain().getOS().startswith("openbsd")) {
Daniel Dunbar116b3052011-05-31 15:58:55 +0000832 if (getToolChain().getArch() == llvm::Triple::x86_64)
Daniel Dunbarea3813f2010-08-01 23:13:54 +0000833 CPUName = "x86-64";
Daniel Dunbar116b3052011-05-31 15:58:55 +0000834 else if (getToolChain().getArch() == llvm::Triple::x86)
Daniel Dunbarea3813f2010-08-01 23:13:54 +0000835 CPUName = "i486";
Roman Divacky432f10d2011-03-01 18:11:37 +0000836 } else if (getToolChain().getOS().startswith("freebsd")) {
Daniel Dunbar116b3052011-05-31 15:58:55 +0000837 if (getToolChain().getArch() == llvm::Triple::x86_64)
Roman Divacky432f10d2011-03-01 18:11:37 +0000838 CPUName = "x86-64";
Daniel Dunbar116b3052011-05-31 15:58:55 +0000839 else if (getToolChain().getArch() == llvm::Triple::x86)
Roman Divacky432f10d2011-03-01 18:11:37 +0000840 CPUName = "i486";
Benjamin Kramer24f1d3e2011-02-02 18:59:27 +0000841 } else if (getToolChain().getOS().startswith("netbsd")) {
Daniel Dunbar116b3052011-05-31 15:58:55 +0000842 if (getToolChain().getArch() == llvm::Triple::x86_64)
Benjamin Kramer24f1d3e2011-02-02 18:59:27 +0000843 CPUName = "x86-64";
Daniel Dunbar116b3052011-05-31 15:58:55 +0000844 else if (getToolChain().getArch() == llvm::Triple::x86)
Benjamin Kramer24f1d3e2011-02-02 18:59:27 +0000845 CPUName = "i486";
Daniel Dunbar3b3191f2009-09-09 22:33:08 +0000846 } else {
Daniel Dunbar116b3052011-05-31 15:58:55 +0000847 if (getToolChain().getArch() == llvm::Triple::x86_64)
Daniel Dunbare13ada62009-11-14 22:04:54 +0000848 CPUName = "x86-64";
Daniel Dunbar116b3052011-05-31 15:58:55 +0000849 else if (getToolChain().getArch() == llvm::Triple::x86)
Daniel Dunbare13ada62009-11-14 22:04:54 +0000850 CPUName = "pentium4";
Daniel Dunbar3b3191f2009-09-09 22:33:08 +0000851 }
852 }
853
Daniel Dunbare13ada62009-11-14 22:04:54 +0000854 if (CPUName) {
Daniel Dunbara7d02312009-12-18 06:30:12 +0000855 CmdArgs.push_back("-target-cpu");
Daniel Dunbare13ada62009-11-14 22:04:54 +0000856 CmdArgs.push_back(CPUName);
857 }
858
Eli Friedmanad811f02011-07-02 00:34:19 +0000859 // The required algorithm here is slightly strange: the options are applied
860 // in order (so -mno-sse -msse2 disables SSE3), but any option that gets
861 // directly overridden later is ignored (so "-mno-sse -msse2 -mno-sse2 -msse"
862 // is equivalent to "-mno-sse2 -msse"). The -cc1 handling deals with the
863 // former correctly, but not the latter; handle directly-overridden
864 // attributes here.
865 llvm::StringMap<unsigned> PrevFeature;
866 std::vector<const char*> Features;
Daniel Dunbar44b36ee2009-11-25 11:53:23 +0000867 for (arg_iterator it = Args.filtered_begin(options::OPT_m_x86_Features_Group),
868 ie = Args.filtered_end(); it != ie; ++it) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000869 StringRef Name = (*it)->getOption().getName();
Daniel Dunbara442fd52010-06-11 22:00:13 +0000870 (*it)->claim();
Daniel Dunbar3b3191f2009-09-09 22:33:08 +0000871
Daniel Dunbar44b36ee2009-11-25 11:53:23 +0000872 // Skip over "-m".
873 assert(Name.startswith("-m") && "Invalid feature name.");
874 Name = Name.substr(2);
Daniel Dunbar3b3191f2009-09-09 22:33:08 +0000875
Daniel Dunbar44b36ee2009-11-25 11:53:23 +0000876 bool IsNegative = Name.startswith("no-");
877 if (IsNegative)
878 Name = Name.substr(3);
Daniel Dunbar3b3191f2009-09-09 22:33:08 +0000879
Eli Friedmanad811f02011-07-02 00:34:19 +0000880 unsigned& Prev = PrevFeature[Name];
881 if (Prev)
882 Features[Prev - 1] = 0;
883 Prev = Features.size() + 1;
884 Features.push_back(Args.MakeArgString((IsNegative ? "-" : "+") + Name));
885 }
886 for (unsigned i = 0; i < Features.size(); i++) {
887 if (Features[i]) {
888 CmdArgs.push_back("-target-feature");
889 CmdArgs.push_back(Features[i]);
890 }
Daniel Dunbar3b3191f2009-09-09 22:33:08 +0000891 }
892}
893
Eric Christopher84fbdb42011-08-19 00:30:14 +0000894static bool
John McCallb5f652e2011-06-22 00:53:57 +0000895shouldUseExceptionTablesForObjCExceptions(unsigned objcABIVersion,
Anders Carlsson246ff3f2011-02-28 00:44:51 +0000896 const llvm::Triple &Triple) {
897 // We use the zero-cost exception tables for Objective-C if the non-fragile
898 // ABI is enabled or when compiling for x86_64 and ARM on Snow Leopard and
899 // later.
900
John McCallb5f652e2011-06-22 00:53:57 +0000901 if (objcABIVersion >= 2)
Anders Carlsson246ff3f2011-02-28 00:44:51 +0000902 return true;
903
904 if (Triple.getOS() != llvm::Triple::Darwin)
905 return false;
906
Eric Christopherbf15d2b2011-07-02 00:20:22 +0000907 return (!Triple.isMacOSXVersionLT(10,5) &&
Anders Carlsson246ff3f2011-02-28 00:44:51 +0000908 (Triple.getArch() == llvm::Triple::x86_64 ||
Eric Christopher84fbdb42011-08-19 00:30:14 +0000909 Triple.getArch() == llvm::Triple::arm));
Anders Carlsson246ff3f2011-02-28 00:44:51 +0000910}
911
Anders Carlssone96ab552011-02-28 02:27:16 +0000912/// addExceptionArgs - Adds exception related arguments to the driver command
913/// arguments. There's a master flag, -fexceptions and also language specific
914/// flags to enable/disable C++ and Objective-C exceptions.
915/// This makes it possible to for example disable C++ exceptions but enable
916/// Objective-C exceptions.
917static void addExceptionArgs(const ArgList &Args, types::ID InputType,
918 const llvm::Triple &Triple,
919 bool KernelOrKext, bool IsRewriter,
John McCallb5f652e2011-06-22 00:53:57 +0000920 unsigned objcABIVersion,
Anders Carlssone96ab552011-02-28 02:27:16 +0000921 ArgStringList &CmdArgs) {
922 if (KernelOrKext)
923 return;
924
925 // Exceptions are enabled by default.
926 bool ExceptionsEnabled = true;
927
928 // This keeps track of whether exceptions were explicitly turned on or off.
929 bool DidHaveExplicitExceptionFlag = false;
930
Rafael Espindola00a66572009-10-01 13:33:33 +0000931 if (Arg *A = Args.getLastArg(options::OPT_fexceptions,
932 options::OPT_fno_exceptions)) {
933 if (A->getOption().matches(options::OPT_fexceptions))
Anders Carlssone96ab552011-02-28 02:27:16 +0000934 ExceptionsEnabled = true;
Eric Christopher84fbdb42011-08-19 00:30:14 +0000935 else
Anders Carlssone96ab552011-02-28 02:27:16 +0000936 ExceptionsEnabled = false;
937
938 DidHaveExplicitExceptionFlag = true;
Rafael Espindola00a66572009-10-01 13:33:33 +0000939 }
Daniel Dunbar30a12b82010-09-14 23:12:31 +0000940
Anders Carlssone96ab552011-02-28 02:27:16 +0000941 bool ShouldUseExceptionTables = false;
Fariborz Jahaniane4b21ab2009-10-01 20:30:46 +0000942
Anders Carlssone96ab552011-02-28 02:27:16 +0000943 // Exception tables and cleanups can be enabled with -fexceptions even if the
944 // language itself doesn't support exceptions.
945 if (ExceptionsEnabled && DidHaveExplicitExceptionFlag)
946 ShouldUseExceptionTables = true;
Daniel Dunbar30a12b82010-09-14 23:12:31 +0000947
Daniel Dunbarc44f8cf2011-03-17 23:28:31 +0000948 // Obj-C exceptions are enabled by default, regardless of -fexceptions. This
949 // is not necessarily sensible, but follows GCC.
950 if (types::isObjC(InputType) &&
Eric Christopher84fbdb42011-08-19 00:30:14 +0000951 Args.hasFlag(options::OPT_fobjc_exceptions,
Daniel Dunbarc44f8cf2011-03-17 23:28:31 +0000952 options::OPT_fno_objc_exceptions,
953 true)) {
954 CmdArgs.push_back("-fobjc-exceptions");
Anders Carlssone96ab552011-02-28 02:27:16 +0000955
Eric Christopher84fbdb42011-08-19 00:30:14 +0000956 ShouldUseExceptionTables |=
John McCallb5f652e2011-06-22 00:53:57 +0000957 shouldUseExceptionTablesForObjCExceptions(objcABIVersion, Triple);
Anders Carlssone96ab552011-02-28 02:27:16 +0000958 }
959
960 if (types::isCXX(InputType)) {
961 bool CXXExceptionsEnabled = ExceptionsEnabled;
962
Eric Christopher84fbdb42011-08-19 00:30:14 +0000963 if (Arg *A = Args.getLastArg(options::OPT_fcxx_exceptions,
964 options::OPT_fno_cxx_exceptions,
Anders Carlssone96ab552011-02-28 02:27:16 +0000965 options::OPT_fexceptions,
966 options::OPT_fno_exceptions)) {
967 if (A->getOption().matches(options::OPT_fcxx_exceptions))
968 CXXExceptionsEnabled = true;
Chandler Carruth74f87112011-02-28 07:25:18 +0000969 else if (A->getOption().matches(options::OPT_fno_cxx_exceptions))
Anders Carlssone96ab552011-02-28 02:27:16 +0000970 CXXExceptionsEnabled = false;
971 }
972
973 if (CXXExceptionsEnabled) {
974 CmdArgs.push_back("-fcxx-exceptions");
975
976 ShouldUseExceptionTables = true;
977 }
978 }
979
980 if (ShouldUseExceptionTables)
981 CmdArgs.push_back("-fexceptions");
Rafael Espindola00a66572009-10-01 13:33:33 +0000982}
983
Rafael Espindola4cfa7972011-05-02 17:43:32 +0000984static bool ShouldDisableCFI(const ArgList &Args,
985 const ToolChain &TC) {
Rafael Espindolaf934f982011-05-17 16:26:17 +0000986 if (TC.getTriple().getOS() == llvm::Triple::Darwin) {
987 // The native darwin assembler doesn't support cfi directives, so
Rafael Espindola35ab91c2011-05-17 19:06:58 +0000988 // we disable them if we think the .s file will be passed to it.
Rafael Espindola4cfa7972011-05-02 17:43:32 +0000989
Rafael Espindolaf934f982011-05-17 16:26:17 +0000990 // FIXME: Duplicated code with ToolChains.cpp
991 // FIXME: This doesn't belong here, but ideally we will support static soon
992 // anyway.
993 bool HasStatic = (Args.hasArg(options::OPT_mkernel) ||
994 Args.hasArg(options::OPT_static) ||
995 Args.hasArg(options::OPT_fapple_kext));
996 bool IsIADefault = TC.IsIntegratedAssemblerDefault() && !HasStatic;
997 bool UseIntegratedAs = Args.hasFlag(options::OPT_integrated_as,
998 options::OPT_no_integrated_as,
999 IsIADefault);
1000 bool UseCFI = Args.hasFlag(options::OPT_fdwarf2_cfi_asm,
1001 options::OPT_fno_dwarf2_cfi_asm,
1002 UseIntegratedAs);
1003 return !UseCFI;
1004 }
1005
1006 // For now we assume that every other assembler support CFI.
1007 return false;
Rafael Espindola4cfa7972011-05-02 17:43:32 +00001008}
1009
Joerg Sonnenbergeref317a22011-05-06 14:35:16 +00001010/// \brief Check whether the given input tree contains any compilation actions.
1011static bool ContainsCompileAction(const Action *A) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001012 if (isa<CompileJobAction>(A))
Joerg Sonnenbergeref317a22011-05-06 14:35:16 +00001013 return true;
1014
1015 for (Action::const_iterator it = A->begin(), ie = A->end(); it != ie; ++it)
1016 if (ContainsCompileAction(*it))
1017 return true;
1018
1019 return false;
1020}
1021
1022/// \brief Check if -relax-all should be passed to the internal assembler.
1023/// This is done by default when compiling non-assembler source with -O0.
1024static bool UseRelaxAll(Compilation &C, const ArgList &Args) {
1025 bool RelaxDefault = true;
1026
1027 if (Arg *A = Args.getLastArg(options::OPT_O_Group))
1028 RelaxDefault = A->getOption().matches(options::OPT_O0);
1029
1030 if (RelaxDefault) {
1031 RelaxDefault = false;
1032 for (ActionList::const_iterator it = C.getActions().begin(),
1033 ie = C.getActions().end(); it != ie; ++it) {
1034 if (ContainsCompileAction(*it)) {
1035 RelaxDefault = true;
1036 break;
1037 }
1038 }
1039 }
1040
1041 return Args.hasFlag(options::OPT_mrelax_all, options::OPT_mno_relax_all,
1042 RelaxDefault);
1043}
1044
Daniel Dunbar1a093d22009-03-18 06:00:36 +00001045void Clang::ConstructJob(Compilation &C, const JobAction &JA,
Daniel Dunbar04c4c2c2009-03-18 07:06:02 +00001046 const InputInfo &Output,
Daniel Dunbar0450e6d2009-03-18 06:07:59 +00001047 const InputInfoList &Inputs,
Daniel Dunbara2aedc62009-03-18 10:01:51 +00001048 const ArgList &Args,
Daniel Dunbar1a093d22009-03-18 06:00:36 +00001049 const char *LinkingOutput) const {
Daniel Dunbare46b52a2010-03-20 04:52:14 +00001050 bool KernelOrKext = Args.hasArg(options::OPT_mkernel,
1051 options::OPT_fapple_kext);
Daniel Dunbar083edf72009-12-21 18:54:17 +00001052 const Driver &D = getToolChain().getDriver();
Daniel Dunbara3246a02009-03-18 08:07:30 +00001053 ArgStringList CmdArgs;
1054
Daniel Dunbare521a892009-03-31 20:53:55 +00001055 assert(Inputs.size() == 1 && "Unable to handle multiple inputs.");
1056
Daniel Dunbar6c536aa2009-12-11 23:00:49 +00001057 // Invoke ourselves in -cc1 mode.
1058 //
1059 // FIXME: Implement custom jobs for internal actions.
1060 CmdArgs.push_back("-cc1");
1061
Daniel Dunbar624c21b2009-10-30 18:12:20 +00001062 // Add the "effective" target triple.
Daniel Dunbard640be22009-03-31 17:35:15 +00001063 CmdArgs.push_back("-triple");
Daniel Dunbar82eb4ce2010-08-23 22:35:37 +00001064 std::string TripleStr = getToolChain().ComputeEffectiveClangTriple(Args);
Daniel Dunbar624c21b2009-10-30 18:12:20 +00001065 CmdArgs.push_back(Args.MakeArgString(TripleStr));
Daniel Dunbarfb58b0a2009-09-10 06:49:20 +00001066
Daniel Dunbar624c21b2009-10-30 18:12:20 +00001067 // Select the appropriate action.
Daniel Dunbar99b55242010-07-19 19:44:22 +00001068 bool IsRewriter = false;
Daniel Dunbara2aedc62009-03-18 10:01:51 +00001069 if (isa<AnalyzeJobAction>(JA)) {
1070 assert(JA.getType() == types::TY_Plist && "Invalid output type.");
1071 CmdArgs.push_back("-analyze");
1072 } else if (isa<PreprocessJobAction>(JA)) {
Daniel Dunbard67a3222009-03-30 06:36:42 +00001073 if (Output.getType() == types::TY_Dependencies)
1074 CmdArgs.push_back("-Eonly");
1075 else
1076 CmdArgs.push_back("-E");
Daniel Dunbarc4343942010-02-03 03:07:56 +00001077 } else if (isa<AssembleJobAction>(JA)) {
1078 CmdArgs.push_back("-emit-obj");
Daniel Dunbar06e2cc32010-05-27 06:18:05 +00001079
Joerg Sonnenbergeref317a22011-05-06 14:35:16 +00001080 if (UseRelaxAll(C, Args))
Daniel Dunbar06e2cc32010-05-27 06:18:05 +00001081 CmdArgs.push_back("-mrelax-all");
Daniel Dunbarfe6c97b2010-08-24 16:47:49 +00001082
Daniel Dunbar2b4de142010-10-18 22:36:15 +00001083 // When using an integrated assembler, translate -Wa, and -Xassembler
1084 // options.
1085 for (arg_iterator it = Args.filtered_begin(options::OPT_Wa_COMMA,
1086 options::OPT_Xassembler),
1087 ie = Args.filtered_end(); it != ie; ++it) {
1088 const Arg *A = *it;
1089 A->claim();
1090
1091 for (unsigned i = 0, e = A->getNumValues(); i != e; ++i) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001092 StringRef Value = A->getValue(Args, i);
Daniel Dunbar2b4de142010-10-18 22:36:15 +00001093
1094 if (Value == "-force_cpusubtype_ALL") {
1095 // Do nothing, this is the default and we don't support anything else.
Daniel Dunbara78e5892010-10-28 20:36:23 +00001096 } else if (Value == "-L") {
Daniel Dunbar67919b22011-03-28 22:49:28 +00001097 CmdArgs.push_back("-msave-temp-labels");
Joerg Sonnenberger3028e462011-05-19 20:46:39 +00001098 } else if (Value == "--fatal-warnings") {
Joerg Sonnenbergerb487d2d2011-05-19 18:42:29 +00001099 CmdArgs.push_back("-mllvm");
1100 CmdArgs.push_back("-fatal-assembler-warnings");
Nick Lewyckyca6b90d2011-06-21 00:14:18 +00001101 } else if (Value == "--noexecstack") {
1102 CmdArgs.push_back("-mnoexecstack");
Daniel Dunbar2b4de142010-10-18 22:36:15 +00001103 } else {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001104 D.Diag(diag::err_drv_unsupported_option_argument)
Daniel Dunbar2b4de142010-10-18 22:36:15 +00001105 << A->getOption().getName() << Value;
1106 }
1107 }
1108 }
Daniel Dunbar7c874332010-11-19 16:23:35 +00001109
1110 // Also ignore explicit -force_cpusubtype_ALL option.
1111 (void) Args.hasArg(options::OPT_force__cpusubtype__ALL);
Daniel Dunbara2aedc62009-03-18 10:01:51 +00001112 } else if (isa<PrecompileJobAction>(JA)) {
Argyrios Kyrtzidis90bdfbb2010-08-11 23:27:58 +00001113 // Use PCH if the user requested it.
Daniel Dunbarcbc34b72009-10-15 20:02:44 +00001114 bool UsePCH = D.CCCUsePCH;
Daniel Dunbarcbc34b72009-10-15 20:02:44 +00001115
1116 if (UsePCH)
Douglas Gregor111af7d2009-04-18 00:34:01 +00001117 CmdArgs.push_back("-emit-pch");
1118 else
1119 CmdArgs.push_back("-emit-pth");
Daniel Dunbara2aedc62009-03-18 10:01:51 +00001120 } else {
1121 assert(isa<CompileJobAction>(JA) && "Invalid action for clang tool.");
Daniel Dunbard067f7f2009-04-08 23:54:23 +00001122
Daniel Dunbara2aedc62009-03-18 10:01:51 +00001123 if (JA.getType() == types::TY_Nothing) {
1124 CmdArgs.push_back("-fsyntax-only");
Daniel Dunbar24e52992010-06-07 23:28:45 +00001125 } else if (JA.getType() == types::TY_LLVM_IR ||
1126 JA.getType() == types::TY_LTO_IR) {
Daniel Dunbara2aedc62009-03-18 10:01:51 +00001127 CmdArgs.push_back("-emit-llvm");
Daniel Dunbar24e52992010-06-07 23:28:45 +00001128 } else if (JA.getType() == types::TY_LLVM_BC ||
1129 JA.getType() == types::TY_LTO_BC) {
Daniel Dunbara2aedc62009-03-18 10:01:51 +00001130 CmdArgs.push_back("-emit-llvm-bc");
1131 } else if (JA.getType() == types::TY_PP_Asm) {
Daniel Dunbard112f102009-09-17 00:47:53 +00001132 CmdArgs.push_back("-S");
Daniel Dunbar6cdf83c2009-09-01 16:57:46 +00001133 } else if (JA.getType() == types::TY_AST) {
1134 CmdArgs.push_back("-emit-pch");
Daniel Dunbaraeea8ac2010-02-11 03:16:21 +00001135 } else if (JA.getType() == types::TY_RewrittenObjC) {
1136 CmdArgs.push_back("-rewrite-objc");
Daniel Dunbar99b55242010-07-19 19:44:22 +00001137 IsRewriter = true;
Daniel Dunbaraeea8ac2010-02-11 03:16:21 +00001138 } else {
1139 assert(JA.getType() == types::TY_PP_Asm &&
1140 "Unexpected output type!");
Daniel Dunbara2aedc62009-03-18 10:01:51 +00001141 }
Daniel Dunbara3246a02009-03-18 08:07:30 +00001142 }
1143
Daniel Dunbara2aedc62009-03-18 10:01:51 +00001144 // The make clang go fast button.
1145 CmdArgs.push_back("-disable-free");
1146
John McCallbb79b5f2010-02-13 03:50:24 +00001147 // Disable the verification pass in -asserts builds.
1148#ifdef NDEBUG
1149 CmdArgs.push_back("-disable-llvm-verifier");
1150#endif
1151
Daniel Dunbar3b358a32009-04-08 05:11:16 +00001152 // Set the main file name, so that debug info works even with
1153 // -save-temps.
1154 CmdArgs.push_back("-main-file-name");
1155 CmdArgs.push_back(darwin::CC1::getBaseInputName(Args, Inputs));
1156
Daniel Dunbar17ddaa62009-04-08 18:03:55 +00001157 // Some flags which affect the language (via preprocessor
1158 // defines). See darwin::CC1::AddCPPArgs.
1159 if (Args.hasArg(options::OPT_static))
1160 CmdArgs.push_back("-static-define");
1161
Daniel Dunbara2aedc62009-03-18 10:01:51 +00001162 if (isa<AnalyzeJobAction>(JA)) {
Ted Kremenek05e6f5b2009-09-25 05:55:59 +00001163 // Enable region store model by default.
1164 CmdArgs.push_back("-analyzer-store=region");
1165
Ted Kremenek7bea9a12009-12-07 22:26:14 +00001166 // Treat blocks as analysis entry points.
1167 CmdArgs.push_back("-analyzer-opt-analyze-nested-blocks");
1168
Ted Kremenek49c79792011-03-24 00:28:47 +00001169 CmdArgs.push_back("-analyzer-eagerly-assume");
1170
Daniel Dunbara2aedc62009-03-18 10:01:51 +00001171 // Add default argument set.
Daniel Dunbar58f345ce2009-05-22 00:38:15 +00001172 if (!Args.hasArg(options::OPT__analyzer_no_default_checks)) {
Argyrios Kyrtzidisa6d04d52011-02-15 07:42:33 +00001173 CmdArgs.push_back("-analyzer-checker=core");
Ted Kremenek49c79792011-03-24 00:28:47 +00001174 CmdArgs.push_back("-analyzer-checker=deadcode");
1175 CmdArgs.push_back("-analyzer-checker=security");
1176
Argyrios Kyrtzidisa6d04d52011-02-15 07:42:33 +00001177 if (getToolChain().getTriple().getOS() != llvm::Triple::Win32)
1178 CmdArgs.push_back("-analyzer-checker=unix");
Ted Kremenek49c79792011-03-24 00:28:47 +00001179
Argyrios Kyrtzidisa6d04d52011-02-15 07:42:33 +00001180 if (getToolChain().getTriple().getVendor() == llvm::Triple::Apple)
Ted Kremenek49c79792011-03-24 00:28:47 +00001181 CmdArgs.push_back("-analyzer-checker=osx");
Daniel Dunbar58f345ce2009-05-22 00:38:15 +00001182 }
Daniel Dunbard067f7f2009-04-08 23:54:23 +00001183
Daniel Dunbar58f345ce2009-05-22 00:38:15 +00001184 // Set the output format. The default is plist, for (lame) historical
1185 // reasons.
1186 CmdArgs.push_back("-analyzer-output");
1187 if (Arg *A = Args.getLastArg(options::OPT__analyzer_output))
1188 CmdArgs.push_back(A->getValue(Args));
1189 else
1190 CmdArgs.push_back("plist");
Daniel Dunbara2aedc62009-03-18 10:01:51 +00001191
Ted Kremenekfe449a22010-03-22 22:32:05 +00001192 // Disable the presentation of standard compiler warnings when
1193 // using --analyze. We only want to show static analyzer diagnostics
1194 // or frontend errors.
1195 CmdArgs.push_back("-w");
1196
Daniel Dunbara2aedc62009-03-18 10:01:51 +00001197 // Add -Xanalyzer arguments when running as analyzer.
1198 Args.AddAllArgValues(CmdArgs, options::OPT_Xanalyzer);
Mike Stump11289f42009-09-09 15:08:12 +00001199 }
1200
Daniel Dunbar4eadb602009-09-10 01:21:12 +00001201 CheckCodeGenerationOptions(D, Args);
1202
Daniel Dunbar44e71222009-04-29 18:32:25 +00001203 // Perform argument translation for LLVM backend. This
1204 // takes some care in reconciling with llvm-gcc. The
1205 // issue is that llvm-gcc translates these options based on
1206 // the values in cc1, whereas we are processing based on
1207 // the driver arguments.
Daniel Dunbard067f7f2009-04-08 23:54:23 +00001208
Daniel Dunbar44e71222009-04-29 18:32:25 +00001209 // This comes from the default translation the driver + cc1
1210 // would do to enable flag_pic.
1211 //
1212 // FIXME: Centralize this code.
1213 bool PICEnabled = (Args.hasArg(options::OPT_fPIC) ||
1214 Args.hasArg(options::OPT_fpic) ||
1215 Args.hasArg(options::OPT_fPIE) ||
1216 Args.hasArg(options::OPT_fpie));
1217 bool PICDisabled = (Args.hasArg(options::OPT_mkernel) ||
1218 Args.hasArg(options::OPT_static));
1219 const char *Model = getToolChain().GetForcedPicModel();
1220 if (!Model) {
1221 if (Args.hasArg(options::OPT_mdynamic_no_pic))
1222 Model = "dynamic-no-pic";
1223 else if (PICDisabled)
1224 Model = "static";
1225 else if (PICEnabled)
1226 Model = "pic";
Daniel Dunbara2aedc62009-03-18 10:01:51 +00001227 else
Daniel Dunbar44e71222009-04-29 18:32:25 +00001228 Model = getToolChain().GetDefaultRelocationModel();
Daniel Dunbara2aedc62009-03-18 10:01:51 +00001229 }
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001230 if (StringRef(Model) != "pic") {
Daniel Dunbara1b02a22009-11-29 07:18:39 +00001231 CmdArgs.push_back("-mrelocation-model");
1232 CmdArgs.push_back(Model);
1233 }
Daniel Dunbar44e71222009-04-29 18:32:25 +00001234
1235 // Infer the __PIC__ value.
1236 //
1237 // FIXME: This isn't quite right on Darwin, which always sets
1238 // __PIC__=2.
1239 if (strcmp(Model, "pic") == 0 || strcmp(Model, "dynamic-no-pic") == 0) {
Daniel Dunbar5fe08662009-11-29 02:39:08 +00001240 CmdArgs.push_back("-pic-level");
1241 CmdArgs.push_back(Args.hasArg(options::OPT_fPIC) ? "2" : "1");
Daniel Dunbar44e71222009-04-29 18:32:25 +00001242 }
Tanya Lattnerf9d41df2009-11-04 01:18:09 +00001243 if (!Args.hasFlag(options::OPT_fmerge_all_constants,
1244 options::OPT_fno_merge_all_constants))
Chris Lattner9242b332011-04-08 18:06:54 +00001245 CmdArgs.push_back("-fno-merge-all-constants");
Daniel Dunbar306945d2009-09-16 06:17:29 +00001246
Daniel Dunbara1b02a22009-11-29 07:18:39 +00001247 // LLVM Code Generator Options.
1248
Daniel Dunbar0bb03312011-02-09 17:54:19 +00001249 if (Arg *A = Args.getLastArg(options::OPT_mregparm_EQ)) {
1250 CmdArgs.push_back("-mregparm");
1251 CmdArgs.push_back(A->getValue(Args));
1252 }
1253
Roman Divacky65b88cd2011-03-01 17:40:53 +00001254 if (Args.hasFlag(options::OPT_mrtd, options::OPT_mno_rtd, false))
1255 CmdArgs.push_back("-mrtd");
1256
Daniel Dunbara1b02a22009-11-29 07:18:39 +00001257 // FIXME: Set --enable-unsafe-fp-math.
1258 if (Args.hasFlag(options::OPT_fno_omit_frame_pointer,
1259 options::OPT_fomit_frame_pointer))
1260 CmdArgs.push_back("-mdisable-fp-elim");
1261 if (!Args.hasFlag(options::OPT_fzero_initialized_in_bss,
1262 options::OPT_fno_zero_initialized_in_bss))
1263 CmdArgs.push_back("-mno-zero-initialized-in-bss");
Daniel Dunbar7aa71f92011-02-04 02:20:39 +00001264 if (!Args.hasFlag(options::OPT_fstrict_aliasing,
1265 options::OPT_fno_strict_aliasing,
1266 getToolChain().IsStrictAliasingDefault()))
Dan Gohman10169b92010-10-14 22:36:56 +00001267 CmdArgs.push_back("-relaxed-aliasing");
Daniel Dunbar0d8ca9e2010-05-14 22:00:22 +00001268
1269 // Decide whether to use verbose asm. Verbose assembly is the default on
1270 // toolchains which have the integrated assembler on by default.
1271 bool IsVerboseAsmDefault = getToolChain().IsIntegratedAssemblerDefault();
1272 if (Args.hasFlag(options::OPT_fverbose_asm, options::OPT_fno_verbose_asm,
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001273 IsVerboseAsmDefault) ||
Daniel Dunbar0d8ca9e2010-05-14 22:00:22 +00001274 Args.hasArg(options::OPT_dA))
Daniel Dunbara1b02a22009-11-29 07:18:39 +00001275 CmdArgs.push_back("-masm-verbose");
Daniel Dunbar0d8ca9e2010-05-14 22:00:22 +00001276
Daniel Dunbara1b02a22009-11-29 07:18:39 +00001277 if (Args.hasArg(options::OPT_fdebug_pass_structure)) {
1278 CmdArgs.push_back("-mdebug-pass");
1279 CmdArgs.push_back("Structure");
1280 }
1281 if (Args.hasArg(options::OPT_fdebug_pass_arguments)) {
1282 CmdArgs.push_back("-mdebug-pass");
1283 CmdArgs.push_back("Arguments");
1284 }
1285
John McCall8517abc2010-02-19 02:45:38 +00001286 // Enable -mconstructor-aliases except on darwin, where we have to
1287 // work around a linker bug; see <rdar://problem/7651567>.
1288 if (getToolChain().getTriple().getOS() != llvm::Triple::Darwin)
1289 CmdArgs.push_back("-mconstructor-aliases");
NAKAMURA Takumi029d74b2011-02-17 08:50:50 +00001290
John McCall7ef5cb32011-03-18 02:56:14 +00001291 // Darwin's kernel doesn't support guard variables; just die if we
1292 // try to use them.
1293 if (KernelOrKext &&
1294 getToolChain().getTriple().getOS() == llvm::Triple::Darwin)
1295 CmdArgs.push_back("-fforbid-guard-variables");
1296
Douglas Gregordbe39272011-02-01 15:15:22 +00001297 if (Args.hasArg(options::OPT_mms_bitfields)) {
1298 CmdArgs.push_back("-mms-bitfields");
1299 }
John McCall8517abc2010-02-19 02:45:38 +00001300
Daniel Dunbar306945d2009-09-16 06:17:29 +00001301 // This is a coarse approximation of what llvm-gcc actually does, both
1302 // -fasynchronous-unwind-tables and -fnon-call-exceptions interact in more
1303 // complicated ways.
1304 bool AsynchronousUnwindTables =
1305 Args.hasFlag(options::OPT_fasynchronous_unwind_tables,
1306 options::OPT_fno_asynchronous_unwind_tables,
1307 getToolChain().IsUnwindTablesDefault() &&
Daniel Dunbare46b52a2010-03-20 04:52:14 +00001308 !KernelOrKext);
Daniel Dunbar306945d2009-09-16 06:17:29 +00001309 if (Args.hasFlag(options::OPT_funwind_tables, options::OPT_fno_unwind_tables,
1310 AsynchronousUnwindTables))
Daniel Dunbara1b02a22009-11-29 07:18:39 +00001311 CmdArgs.push_back("-munwind-tables");
1312
1313 if (Arg *A = Args.getLastArg(options::OPT_flimited_precision_EQ)) {
1314 CmdArgs.push_back("-mlimit-float-precision");
1315 CmdArgs.push_back(A->getValue(Args));
1316 }
Daniel Dunbar44e71222009-04-29 18:32:25 +00001317
Daniel Dunbar4dbaaa62009-05-06 03:16:41 +00001318 // FIXME: Handle -mtune=.
1319 (void) Args.hasArg(options::OPT_mtune_EQ);
Daniel Dunbar44e71222009-04-29 18:32:25 +00001320
Benjamin Kramercf4371a2009-08-05 14:30:52 +00001321 if (Arg *A = Args.getLastArg(options::OPT_mcmodel_EQ)) {
Daniel Dunbara1b02a22009-11-29 07:18:39 +00001322 CmdArgs.push_back("-mcode-model");
Benjamin Kramercf4371a2009-08-05 14:30:52 +00001323 CmdArgs.push_back(A->getValue(Args));
1324 }
1325
Daniel Dunbar3b3191f2009-09-09 22:33:08 +00001326 // Add target specific cpu and features flags.
1327 switch(getToolChain().getTriple().getArch()) {
1328 default:
1329 break;
Daniel Dunbar4dbaaa62009-05-06 03:16:41 +00001330
Daniel Dunbar0f5c5422009-09-10 04:57:17 +00001331 case llvm::Triple::arm:
1332 case llvm::Triple::thumb:
Daniel Dunbarc9388c12011-03-17 17:10:06 +00001333 AddARMTargetArgs(Args, CmdArgs, KernelOrKext);
Daniel Dunbar0f5c5422009-09-10 04:57:17 +00001334 break;
1335
Eric Christopher0b26a612010-03-02 02:41:08 +00001336 case llvm::Triple::mips:
1337 case llvm::Triple::mipsel:
1338 AddMIPSTargetArgs(Args, CmdArgs);
1339 break;
1340
Bruno Cardoso Lopese7f211c2010-11-09 17:21:19 +00001341 case llvm::Triple::sparc:
1342 AddSparcTargetArgs(Args, CmdArgs);
1343 break;
1344
Daniel Dunbar3b3191f2009-09-09 22:33:08 +00001345 case llvm::Triple::x86:
1346 case llvm::Triple::x86_64:
1347 AddX86TargetArgs(Args, CmdArgs);
1348 break;
Daniel Dunbar44e71222009-04-29 18:32:25 +00001349 }
1350
Daniel Dunbar976a2f52010-08-11 23:07:47 +00001351 // Pass the linker version in use.
1352 if (Arg *A = Args.getLastArg(options::OPT_mlinker_version_EQ)) {
1353 CmdArgs.push_back("-target-linker-version");
1354 CmdArgs.push_back(A->getValue(Args));
1355 }
1356
Nick Lewycky75033772011-02-02 06:43:03 +00001357 // -mno-omit-leaf-frame-pointer is the default on Darwin.
Daniel Dunbarbb7ac522010-07-01 01:31:45 +00001358 if (Args.hasFlag(options::OPT_momit_leaf_frame_pointer,
Nick Lewycky75033772011-02-02 06:43:03 +00001359 options::OPT_mno_omit_leaf_frame_pointer,
1360 getToolChain().getTriple().getOS() != llvm::Triple::Darwin))
Daniel Dunbarbb7ac522010-07-01 01:31:45 +00001361 CmdArgs.push_back("-momit-leaf-frame-pointer");
1362
Dan Gohmand1e76b92010-01-08 02:20:44 +00001363 // -fno-math-errno is default.
1364 if (Args.hasFlag(options::OPT_fmath_errno,
Daniel Dunbar44e71222009-04-29 18:32:25 +00001365 options::OPT_fno_math_errno,
Dan Gohmand1e76b92010-01-08 02:20:44 +00001366 false))
1367 CmdArgs.push_back("-fmath-errno");
Daniel Dunbar44e71222009-04-29 18:32:25 +00001368
Daniel Dunbarfcc49a82010-05-12 18:19:58 +00001369 // Explicitly error on some things we know we don't support and can't just
1370 // ignore.
1371 types::ID InputType = Inputs[0].getType();
Daniel Dunbar4ed214a2010-09-24 19:39:37 +00001372 if (!Args.hasArg(options::OPT_fallow_unsupported)) {
1373 Arg *Unsupported;
Peter Collingbourne77b0e7f22011-07-12 19:35:15 +00001374 if ((Unsupported = Args.getLastArg(options::OPT_iframework)))
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001375 D.Diag(diag::err_drv_clang_unsupported)
Daniel Dunbarfcc49a82010-05-12 18:19:58 +00001376 << Unsupported->getOption().getName();
Daniel Dunbar4ed214a2010-09-24 19:39:37 +00001377
1378 if (types::isCXX(InputType) &&
1379 getToolChain().getTriple().getOS() == llvm::Triple::Darwin &&
1380 getToolChain().getTriple().getArch() == llvm::Triple::x86) {
Bob Wilson0d45f582011-08-13 23:48:55 +00001381 if ((Unsupported = Args.getLastArg(options::OPT_fapple_kext)) ||
1382 (Unsupported = Args.getLastArg(options::OPT_mkernel)))
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001383 D.Diag(diag::err_drv_clang_unsupported_opt_cxx_darwin_i386)
Daniel Dunbar4ed214a2010-09-24 19:39:37 +00001384 << Unsupported->getOption().getName();
1385 }
Daniel Dunbarfcc49a82010-05-12 18:19:58 +00001386 }
1387
Daniel Dunbara2aedc62009-03-18 10:01:51 +00001388 Args.AddAllArgs(CmdArgs, options::OPT_v);
Daniel Dunbard4352752010-08-24 22:44:13 +00001389 Args.AddLastArg(CmdArgs, options::OPT_H);
Chad Rosierbe10f982011-08-02 17:58:04 +00001390 if (D.CCPrintHeaders && !D.CCGenDiagnostics) {
Daniel Dunbarac540b32011-02-02 21:11:35 +00001391 CmdArgs.push_back("-header-include-file");
1392 CmdArgs.push_back(D.CCPrintHeadersFilename ?
1393 D.CCPrintHeadersFilename : "-");
1394 }
Daniel Dunbara2aedc62009-03-18 10:01:51 +00001395 Args.AddLastArg(CmdArgs, options::OPT_P);
Mike Stump11289f42009-09-09 15:08:12 +00001396 Args.AddLastArg(CmdArgs, options::OPT_print_ivar_layout);
Daniel Dunbara2aedc62009-03-18 10:01:51 +00001397
Chad Rosierbe10f982011-08-02 17:58:04 +00001398 if (D.CCLogDiagnostics && !D.CCGenDiagnostics) {
Daniel Dunbar529c03b2011-04-07 18:01:20 +00001399 CmdArgs.push_back("-diagnostic-log-file");
1400 CmdArgs.push_back(D.CCLogDiagnosticsFilename ?
1401 D.CCLogDiagnosticsFilename : "-");
1402 }
1403
Daniel Dunbara2aedc62009-03-18 10:01:51 +00001404 // Special case debug options to only pass -g to clang. This is
1405 // wrong.
Rafael Espindola08a692a2010-03-07 04:46:18 +00001406 Args.ClaimAllArgs(options::OPT_g_Group);
Daniel Dunbar4083d042010-05-12 18:19:55 +00001407 if (Arg *A = Args.getLastArg(options::OPT_g_Group))
1408 if (!A->getOption().matches(options::OPT_g0))
1409 CmdArgs.push_back("-g");
Daniel Dunbara2aedc62009-03-18 10:01:51 +00001410
Rafael Espindola66bfb2752010-05-06 21:06:04 +00001411 Args.AddAllArgs(CmdArgs, options::OPT_ffunction_sections);
1412 Args.AddAllArgs(CmdArgs, options::OPT_fdata_sections);
1413
Chris Lattner3c77a352010-06-22 00:03:40 +00001414 Args.AddAllArgs(CmdArgs, options::OPT_finstrument_functions);
1415
Nick Lewycky207bce32011-04-21 23:44:07 +00001416 if (Args.hasArg(options::OPT_ftest_coverage) ||
1417 Args.hasArg(options::OPT_coverage))
1418 CmdArgs.push_back("-femit-coverage-notes");
1419 if (Args.hasArg(options::OPT_fprofile_arcs) ||
1420 Args.hasArg(options::OPT_coverage))
1421 CmdArgs.push_back("-femit-coverage-data");
1422
Nick Lewycky480cb992011-05-04 20:46:58 +00001423 if (C.getArgs().hasArg(options::OPT_c) ||
1424 C.getArgs().hasArg(options::OPT_S)) {
1425 if (Output.isFilename()) {
Nick Lewycky85c011d2011-05-05 00:08:20 +00001426 CmdArgs.push_back("-coverage-file");
1427 CmdArgs.push_back(Args.MakeArgString(Output.getFilename()));
Nick Lewycky480cb992011-05-04 20:46:58 +00001428 }
1429 }
1430
Daniel Dunbara2aedc62009-03-18 10:01:51 +00001431 Args.AddLastArg(CmdArgs, options::OPT_nostdinc);
Douglas Gregor64b046f2010-03-24 20:13:48 +00001432 Args.AddLastArg(CmdArgs, options::OPT_nostdincxx);
Rafael Espindolab3549d72009-10-26 13:36:57 +00001433 Args.AddLastArg(CmdArgs, options::OPT_nobuiltininc);
Daniel Dunbara2aedc62009-03-18 10:01:51 +00001434
Daniel Dunbar34e0b8c2009-12-15 01:02:52 +00001435 // Pass the path to compiler resource files.
Daniel Dunbar34e0b8c2009-12-15 01:02:52 +00001436 CmdArgs.push_back("-resource-dir");
Daniel Dunbar3f3e2cd2010-01-20 02:35:16 +00001437 CmdArgs.push_back(D.ResourceDir.c_str());
Daniel Dunbar9dc82a22009-04-07 21:42:00 +00001438
Argyrios Kyrtzidis71731d62010-11-03 22:45:23 +00001439 Args.AddLastArg(CmdArgs, options::OPT_working_directory);
1440
John McCalld70fb982011-06-15 23:25:17 +00001441 if (!Args.hasArg(options::OPT_fno_objc_arc)) {
Argyrios Kyrtzidisc44b93d2011-07-07 04:00:39 +00001442 if (const Arg *A = Args.getLastArg(options::OPT_ccc_arcmt_check,
Argyrios Kyrtzidis7fbd97f2011-07-09 20:00:58 +00001443 options::OPT_ccc_arcmt_modify,
1444 options::OPT_ccc_arcmt_migrate)) {
John McCalld70fb982011-06-15 23:25:17 +00001445 switch (A->getOption().getID()) {
1446 default:
1447 llvm_unreachable("missed a case");
Argyrios Kyrtzidisc44b93d2011-07-07 04:00:39 +00001448 case options::OPT_ccc_arcmt_check:
John McCalld70fb982011-06-15 23:25:17 +00001449 CmdArgs.push_back("-arcmt-check");
1450 break;
Argyrios Kyrtzidisc44b93d2011-07-07 04:00:39 +00001451 case options::OPT_ccc_arcmt_modify:
John McCalld70fb982011-06-15 23:25:17 +00001452 CmdArgs.push_back("-arcmt-modify");
1453 break;
Argyrios Kyrtzidis7fbd97f2011-07-09 20:00:58 +00001454 case options::OPT_ccc_arcmt_migrate:
1455 CmdArgs.push_back("-arcmt-migrate");
1456 CmdArgs.push_back("-arcmt-migrate-directory");
1457 CmdArgs.push_back(A->getValue(Args));
Argyrios Kyrtzidisd5713632011-07-19 17:20:03 +00001458
1459 Args.AddLastArg(CmdArgs, options::OPT_arcmt_migrate_report_output);
1460 Args.AddLastArg(CmdArgs, options::OPT_arcmt_migrate_emit_arc_errors);
Argyrios Kyrtzidis7fbd97f2011-07-09 20:00:58 +00001461 break;
John McCalld70fb982011-06-15 23:25:17 +00001462 }
1463 }
1464 }
Eric Christopher84fbdb42011-08-19 00:30:14 +00001465
Daniel Dunbard067f7f2009-04-08 23:54:23 +00001466 // Add preprocessing options like -I, -D, etc. if we are using the
1467 // preprocessor.
1468 //
1469 // FIXME: Support -fpreprocessed
Daniel Dunbard067f7f2009-04-08 23:54:23 +00001470 if (types::getPreprocessedType(InputType) != types::TY_INVALID)
Douglas Gregor111af7d2009-04-18 00:34:01 +00001471 AddPreprocessingOptions(D, Args, CmdArgs, Output, Inputs);
Daniel Dunbara2aedc62009-03-18 10:01:51 +00001472
Rafael Espindolaa7431922011-07-21 23:40:37 +00001473 // Don't warn about "clang -c -DPIC -fPIC test.i" because libtool.m4 assumes
1474 // that "The compiler can only warn and ignore the option if not recognized".
1475 // When building with ccache, it will pass -D options to clang even on
1476 // preprocessed inputs and configure concludes that -fPIC is not supported.
1477 Args.ClaimAllArgs(options::OPT_D);
1478
Daniel Dunbar58f78332009-09-17 06:53:36 +00001479 // Manually translate -O to -O2 and -O4 to -O3; let clang reject
Daniel Dunbar13864952009-03-24 20:17:30 +00001480 // others.
1481 if (Arg *A = Args.getLastArg(options::OPT_O_Group)) {
Daniel Dunbar0bfb21e2009-11-19 03:26:40 +00001482 if (A->getOption().matches(options::OPT_O4))
Daniel Dunbar13864952009-03-24 20:17:30 +00001483 CmdArgs.push_back("-O3");
Daniel Dunbar9296f632010-05-27 06:51:08 +00001484 else if (A->getOption().matches(options::OPT_O) &&
1485 A->getValue(Args)[0] == '\0')
Daniel Dunbar58f78332009-09-17 06:53:36 +00001486 CmdArgs.push_back("-O2");
Daniel Dunbara2aedc62009-03-18 10:01:51 +00001487 else
Daniel Dunbar7ef5ed62009-03-18 23:39:35 +00001488 A->render(Args, CmdArgs);
Daniel Dunbara2aedc62009-03-18 10:01:51 +00001489 }
1490
Daniel Dunbar945577c2009-10-29 02:24:45 +00001491 Args.AddAllArgs(CmdArgs, options::OPT_W_Group);
1492 Args.AddLastArg(CmdArgs, options::OPT_pedantic);
1493 Args.AddLastArg(CmdArgs, options::OPT_pedantic_errors);
Daniel Dunbara2aedc62009-03-18 10:01:51 +00001494 Args.AddLastArg(CmdArgs, options::OPT_w);
Daniel Dunbarc44b4cc2009-04-07 22:13:21 +00001495
1496 // Handle -{std, ansi, trigraphs} -- take the last of -{std, ansi}
1497 // (-ansi is equivalent to -std=c89).
1498 //
1499 // If a std is supplied, only add -trigraphs if it follows the
1500 // option.
1501 if (Arg *Std = Args.getLastArg(options::OPT_std_EQ, options::OPT_ansi)) {
1502 if (Std->getOption().matches(options::OPT_ansi))
Nuno Lopes275225d2009-10-16 14:28:06 +00001503 if (types::isCXX(InputType))
Daniel Dunbar1c7577c2009-11-04 06:24:38 +00001504 CmdArgs.push_back("-std=c++98");
Nuno Lopes275225d2009-10-16 14:28:06 +00001505 else
Daniel Dunbar1c7577c2009-11-04 06:24:38 +00001506 CmdArgs.push_back("-std=c89");
Daniel Dunbarc44b4cc2009-04-07 22:13:21 +00001507 else
1508 Std->render(Args, CmdArgs);
1509
Daniel Dunbar3f1a1ff2010-06-14 21:23:08 +00001510 if (Arg *A = Args.getLastArg(options::OPT_std_EQ, options::OPT_ansi,
1511 options::OPT_trigraphs))
1512 if (A != Std)
Daniel Dunbarc44b4cc2009-04-07 22:13:21 +00001513 A->render(Args, CmdArgs);
Daniel Dunbar72a60902009-04-26 01:10:38 +00001514 } else {
1515 // Honor -std-default.
Daniel Dunbar12998192010-01-29 21:03:02 +00001516 //
1517 // FIXME: Clang doesn't correctly handle -std= when the input language
1518 // doesn't match. For the time being just ignore this for C++ inputs;
1519 // eventually we want to do all the standard defaulting here instead of
1520 // splitting it between the driver and clang -cc1.
1521 if (!types::isCXX(InputType))
1522 Args.AddAllArgsTranslated(CmdArgs, options::OPT_std_default_EQ,
1523 "-std=", /*Joined=*/true);
Daniel Dunbarc44b4cc2009-04-07 22:13:21 +00001524 Args.AddLastArg(CmdArgs, options::OPT_trigraphs);
Daniel Dunbar72a60902009-04-26 01:10:38 +00001525 }
Daniel Dunbard067f7f2009-04-08 23:54:23 +00001526
Chandler Carruthb009b142011-04-23 06:30:43 +00001527 // Map the bizarre '-Wwrite-strings' flag to a more sensible
1528 // '-fconst-strings'; this better indicates its actual behavior.
1529 if (Args.hasFlag(options::OPT_Wwrite_strings, options::OPT_Wno_write_strings,
1530 false)) {
1531 // For perfect compatibility with GCC, we do this even in the presence of
1532 // '-w'. This flag names something other than a warning for GCC.
1533 CmdArgs.push_back("-fconst-strings");
1534 }
1535
Chandler Carruth61fbf622011-04-23 09:27:53 +00001536 // GCC provides a macro definition '__DEPRECATED' when -Wdeprecated is active
Chandler Carruth30483fb2011-04-23 19:48:40 +00001537 // during C++ compilation, which it is by default. GCC keeps this define even
1538 // in the presence of '-w', match this behavior bug-for-bug.
1539 if (types::isCXX(InputType) &&
1540 Args.hasFlag(options::OPT_Wdeprecated, options::OPT_Wno_deprecated,
1541 true)) {
1542 CmdArgs.push_back("-fdeprecated-macro");
Chandler Carruth61fbf622011-04-23 09:27:53 +00001543 }
1544
Chandler Carruthe0391482010-05-22 02:21:53 +00001545 // Translate GCC's misnamer '-fasm' arguments to '-fgnu-keywords'.
1546 if (Arg *Asm = Args.getLastArg(options::OPT_fasm, options::OPT_fno_asm)) {
1547 if (Asm->getOption().matches(options::OPT_fasm))
1548 CmdArgs.push_back("-fgnu-keywords");
1549 else
1550 CmdArgs.push_back("-fno-gnu-keywords");
1551 }
1552
Rafael Espindola4cfa7972011-05-02 17:43:32 +00001553 if (ShouldDisableCFI(Args, getToolChain()))
1554 CmdArgs.push_back("-fno-dwarf2-cfi-asm");
Rafael Espindolae2641872011-04-30 18:35:43 +00001555
Daniel Dunbara2aedc62009-03-18 10:01:51 +00001556 if (Arg *A = Args.getLastArg(options::OPT_ftemplate_depth_)) {
1557 CmdArgs.push_back("-ftemplate-depth");
1558 CmdArgs.push_back(A->getValue(Args));
1559 }
1560
Argyrios Kyrtzidisef6c8da2010-11-18 00:20:36 +00001561 if (Arg *A = Args.getLastArg(options::OPT_Wlarge_by_value_copy_EQ,
1562 options::OPT_Wlarge_by_value_copy_def)) {
1563 CmdArgs.push_back("-Wlarge-by-value-copy");
Argyrios Kyrtzidisaf84ec02010-11-17 23:11:54 +00001564 if (A->getNumValues())
1565 CmdArgs.push_back(A->getValue(Args));
1566 else
Argyrios Kyrtzidisef6c8da2010-11-18 00:20:36 +00001567 CmdArgs.push_back("64"); // default value for -Wlarge-by-value-copy.
Argyrios Kyrtzidisaf84ec02010-11-17 23:11:54 +00001568 }
1569
Daniel Dunbarfffd1812009-11-19 04:00:53 +00001570 if (Args.hasArg(options::OPT__relocatable_pch))
Daniel Dunbar8bed86c2009-11-20 22:21:36 +00001571 CmdArgs.push_back("-relocatable-pch");
Mike Stump11289f42009-09-09 15:08:12 +00001572
Daniel Dunbar1c7577c2009-11-04 06:24:38 +00001573 if (Arg *A = Args.getLastArg(options::OPT_fconstant_string_class_EQ)) {
1574 CmdArgs.push_back("-fconstant-string-class");
1575 CmdArgs.push_back(A->getValue(Args));
1576 }
David Chisnall5778fce2009-08-31 16:41:57 +00001577
Chris Lattnere23003d2010-01-09 21:54:33 +00001578 if (Arg *A = Args.getLastArg(options::OPT_ftabstop_EQ)) {
1579 CmdArgs.push_back("-ftabstop");
1580 CmdArgs.push_back(A->getValue(Args));
1581 }
1582
Chris Lattnerb35583d2010-04-07 20:49:23 +00001583 CmdArgs.push_back("-ferror-limit");
1584 if (Arg *A = Args.getLastArg(options::OPT_ferror_limit_EQ))
1585 CmdArgs.push_back(A->getValue(Args));
1586 else
1587 CmdArgs.push_back("19");
Douglas Gregorffed1cb2010-04-20 07:18:24 +00001588
Chandler Carrutha77a7272010-05-06 04:55:18 +00001589 if (Arg *A = Args.getLastArg(options::OPT_fmacro_backtrace_limit_EQ)) {
1590 CmdArgs.push_back("-fmacro-backtrace-limit");
Douglas Gregorcd121fb2010-05-04 17:13:42 +00001591 CmdArgs.push_back(A->getValue(Args));
Chandler Carrutha77a7272010-05-06 04:55:18 +00001592 }
1593
1594 if (Arg *A = Args.getLastArg(options::OPT_ftemplate_backtrace_limit_EQ)) {
1595 CmdArgs.push_back("-ftemplate-backtrace-limit");
Douglas Gregorffed1cb2010-04-20 07:18:24 +00001596 CmdArgs.push_back(A->getValue(Args));
Chandler Carrutha77a7272010-05-06 04:55:18 +00001597 }
1598
Daniel Dunbar2c978472009-11-04 06:24:47 +00001599 // Pass -fmessage-length=.
Daniel Dunbar84bb7932009-11-30 08:40:54 +00001600 CmdArgs.push_back("-fmessage-length");
Daniel Dunbar2c978472009-11-04 06:24:47 +00001601 if (Arg *A = Args.getLastArg(options::OPT_fmessage_length_EQ)) {
Daniel Dunbar84bb7932009-11-30 08:40:54 +00001602 CmdArgs.push_back(A->getValue(Args));
Daniel Dunbar2c978472009-11-04 06:24:47 +00001603 } else {
1604 // If -fmessage-length=N was not specified, determine whether this is a
1605 // terminal and, if so, implicitly define -fmessage-length appropriately.
1606 unsigned N = llvm::sys::Process::StandardErrColumns();
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001607 CmdArgs.push_back(Args.MakeArgString(Twine(N)));
Daniel Dunbar2c978472009-11-04 06:24:47 +00001608 }
1609
Daniel Dunbare357d562009-12-03 18:42:11 +00001610 if (const Arg *A = Args.getLastArg(options::OPT_fvisibility_EQ)) {
1611 CmdArgs.push_back("-fvisibility");
1612 CmdArgs.push_back(A->getValue(Args));
1613 }
1614
Douglas Gregor08329632010-06-15 17:05:35 +00001615 Args.AddLastArg(CmdArgs, options::OPT_fvisibility_inlines_hidden);
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001616
Daniel Dunbare46b52a2010-03-20 04:52:14 +00001617 // -fhosted is default.
1618 if (KernelOrKext || Args.hasFlag(options::OPT_ffreestanding,
1619 options::OPT_fhosted,
1620 false))
1621 CmdArgs.push_back("-ffreestanding");
1622
Daniel Dunbare357d562009-12-03 18:42:11 +00001623 // Forward -f (flag) options which we can pass directly.
Mike Stumpd9546382009-12-12 01:27:46 +00001624 Args.AddLastArg(CmdArgs, options::OPT_fcatch_undefined_behavior);
Daniel Dunbar3a148f22009-04-07 21:51:40 +00001625 Args.AddLastArg(CmdArgs, options::OPT_femit_all_decls);
Daniel Dunbar3a148f22009-04-07 21:51:40 +00001626 Args.AddLastArg(CmdArgs, options::OPT_fheinous_gnu_extensions);
Devang Patel91bbb552010-09-30 19:05:55 +00001627 Args.AddLastArg(CmdArgs, options::OPT_flimit_debug_info);
Daniel Dunbar733b0f82011-03-01 18:49:30 +00001628 if (getToolChain().SupportsProfiling())
1629 Args.AddLastArg(CmdArgs, options::OPT_pg);
Daniel Dunbar35621a92010-03-16 16:57:46 +00001630
1631 // -flax-vector-conversions is default.
1632 if (!Args.hasFlag(options::OPT_flax_vector_conversions,
1633 options::OPT_fno_lax_vector_conversions))
1634 CmdArgs.push_back("-fno-lax-vector-conversions");
1635
Fariborz Jahaniana4cfff82011-01-07 01:05:02 +00001636 if (Args.getLastArg(options::OPT_fapple_kext))
1637 CmdArgs.push_back("-fapple-kext");
1638
Fariborz Jahaniana4404f22009-05-22 20:17:16 +00001639 Args.AddLastArg(CmdArgs, options::OPT_fobjc_sender_dependent_dispatch);
Chris Lattner69686412009-04-21 05:34:31 +00001640 Args.AddLastArg(CmdArgs, options::OPT_fdiagnostics_print_source_range_info);
Douglas Gregoreec975c2010-08-19 20:24:43 +00001641 Args.AddLastArg(CmdArgs, options::OPT_fdiagnostics_parseable_fixits);
Daniel Dunbar3a148f22009-04-07 21:51:40 +00001642 Args.AddLastArg(CmdArgs, options::OPT_ftime_report);
1643 Args.AddLastArg(CmdArgs, options::OPT_ftrapv);
David Chisnalldd84ef12010-09-17 18:29:54 +00001644
1645 if (Arg *A = Args.getLastArg(options::OPT_ftrapv_handler_EQ)) {
1646 CmdArgs.push_back("-ftrapv-handler");
1647 CmdArgs.push_back(A->getValue(Args));
1648 }
1649
Evan Cheng04c94292011-04-08 21:37:45 +00001650 // Forward -ftrap_function= options to the backend.
1651 if (Arg *A = Args.getLastArg(options::OPT_ftrap_function_EQ)) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001652 StringRef FuncName = A->getValue(Args);
Evan Cheng04c94292011-04-08 21:37:45 +00001653 CmdArgs.push_back("-backend-option");
1654 CmdArgs.push_back(Args.MakeArgString("-trap-func=" + FuncName));
1655 }
1656
Chandler Carruth6e501032011-03-27 00:04:55 +00001657 // -fno-strict-overflow implies -fwrapv if it isn't disabled, but
1658 // -fstrict-overflow won't turn off an explicitly enabled -fwrapv.
1659 if (Arg *A = Args.getLastArg(options::OPT_fwrapv,
1660 options::OPT_fno_wrapv)) {
1661 if (A->getOption().matches(options::OPT_fwrapv))
1662 CmdArgs.push_back("-fwrapv");
1663 } else if (Arg *A = Args.getLastArg(options::OPT_fstrict_overflow,
1664 options::OPT_fno_strict_overflow)) {
1665 if (A->getOption().matches(options::OPT_fno_strict_overflow))
1666 CmdArgs.push_back("-fwrapv");
1667 }
Daniel Dunbar3a148f22009-04-07 21:51:40 +00001668 Args.AddLastArg(CmdArgs, options::OPT_fwritable_strings);
Eric Christopherf387dbd2010-08-07 23:08:14 +00001669 Args.AddLastArg(CmdArgs, options::OPT_funroll_loops);
Daniel Dunbara2aedc62009-03-18 10:01:51 +00001670
Daniel Dunbara77eaeb2009-09-03 04:54:28 +00001671 Args.AddLastArg(CmdArgs, options::OPT_pthread);
1672
Daniel Dunbar4930e332009-11-17 08:07:36 +00001673 // -stack-protector=0 is default.
1674 unsigned StackProtectorLevel = 0;
Bill Wendlingd63bbad2009-06-28 07:36:13 +00001675 if (Arg *A = Args.getLastArg(options::OPT_fno_stack_protector,
1676 options::OPT_fstack_protector_all,
1677 options::OPT_fstack_protector)) {
Daniel Dunbar4930e332009-11-17 08:07:36 +00001678 if (A->getOption().matches(options::OPT_fstack_protector))
1679 StackProtectorLevel = 1;
1680 else if (A->getOption().matches(options::OPT_fstack_protector_all))
1681 StackProtectorLevel = 2;
Nico Weberdd473632011-08-23 07:38:27 +00001682 } else {
1683 StackProtectorLevel =
1684 getToolChain().GetDefaultStackProtectorLevel(KernelOrKext);
1685 }
Daniel Dunbar4930e332009-11-17 08:07:36 +00001686 if (StackProtectorLevel) {
1687 CmdArgs.push_back("-stack-protector");
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001688 CmdArgs.push_back(Args.MakeArgString(Twine(StackProtectorLevel)));
Bill Wendlingd63bbad2009-06-28 07:36:13 +00001689 }
1690
Eric Christopherd5c45f62011-05-02 21:18:22 +00001691 // Translate -mstackrealign
1692 if (Args.hasArg(options::OPT_mstackrealign)) {
1693 CmdArgs.push_back("-backend-option");
1694 CmdArgs.push_back("-force-align-stack");
1695 }
Eric Christopher84fbdb42011-08-19 00:30:14 +00001696
Daniel Dunbard18049a2009-04-07 21:16:11 +00001697 // Forward -f options with positive and negative forms; we translate
1698 // these by hand.
1699
Fariborz Jahaniana4cfff82011-01-07 01:05:02 +00001700 if (Args.hasArg(options::OPT_mkernel)) {
Daniel Dunbar80f787c2011-02-04 17:24:47 +00001701 if (!Args.hasArg(options::OPT_fapple_kext) && types::isCXX(InputType))
Fariborz Jahaniana4cfff82011-01-07 01:05:02 +00001702 CmdArgs.push_back("-fapple-kext");
1703 if (!Args.hasArg(options::OPT_fbuiltin))
1704 CmdArgs.push_back("-fno-builtin");
1705 }
Daniel Dunbar4930e332009-11-17 08:07:36 +00001706 // -fbuiltin is default.
Fariborz Jahaniana4cfff82011-01-07 01:05:02 +00001707 else if (!Args.hasFlag(options::OPT_fbuiltin, options::OPT_fno_builtin))
Daniel Dunbar484afa22009-11-19 04:55:23 +00001708 CmdArgs.push_back("-fno-builtin");
Daniel Dunbard18049a2009-04-07 21:16:11 +00001709
Nuno Lopes13c88c72009-12-16 16:59:22 +00001710 if (!Args.hasFlag(options::OPT_fassume_sane_operator_new,
1711 options::OPT_fno_assume_sane_operator_new))
1712 CmdArgs.push_back("-fno-assume-sane-operator-new");
1713
Daniel Dunbar4930e332009-11-17 08:07:36 +00001714 // -fblocks=0 is default.
1715 if (Args.hasFlag(options::OPT_fblocks, options::OPT_fno_blocks,
David Chisnallda209912011-02-28 17:11:43 +00001716 getToolChain().IsBlocksDefault()) ||
1717 (Args.hasArg(options::OPT_fgnu_runtime) &&
1718 Args.hasArg(options::OPT_fobjc_nonfragile_abi) &&
1719 !Args.hasArg(options::OPT_fno_blocks))) {
Daniel Dunbar4930e332009-11-17 08:07:36 +00001720 CmdArgs.push_back("-fblocks");
David Chisnall950a9512009-11-17 19:33:30 +00001721 }
Daniel Dunbard18049a2009-04-07 21:16:11 +00001722
John McCalldfea9982010-04-09 19:12:06 +00001723 // -faccess-control is default.
John McCall3155f572010-04-09 19:03:51 +00001724 if (Args.hasFlag(options::OPT_fno_access_control,
1725 options::OPT_faccess_control,
John McCalldfea9982010-04-09 19:12:06 +00001726 false))
John McCall3155f572010-04-09 19:03:51 +00001727 CmdArgs.push_back("-fno-access-control");
John McCall59bb1d42010-03-17 01:32:13 +00001728
Anders Carlssond470fef2010-11-21 00:09:52 +00001729 // -felide-constructors is the default.
1730 if (Args.hasFlag(options::OPT_fno_elide_constructors,
1731 options::OPT_felide_constructors,
1732 false))
1733 CmdArgs.push_back("-fno-elide-constructors");
1734
Daniel Dunbar0730e4f2009-11-17 07:06:20 +00001735 // -frtti is default.
Daniel Dunbare46b52a2010-03-20 04:52:14 +00001736 if (KernelOrKext ||
1737 !Args.hasFlag(options::OPT_frtti, options::OPT_fno_rtti))
Daniel Dunbar484afa22009-11-19 04:55:23 +00001738 CmdArgs.push_back("-fno-rtti");
Mike Stump183c3d22009-07-31 23:15:31 +00001739
Argyrios Kyrtzidis74825bc2010-10-08 00:25:19 +00001740 // -fshort-enums=0 is default.
1741 // FIXME: Are there targers where -fshort-enums is on by default ?
1742 if (Args.hasFlag(options::OPT_fshort_enums,
1743 options::OPT_fno_short_enums, false))
1744 CmdArgs.push_back("-fshort-enums");
1745
Daniel Dunbard609b7b2009-11-17 06:37:03 +00001746 // -fsigned-char is default.
Daniel Dunbar5bdd2992009-11-25 10:14:30 +00001747 if (!Args.hasFlag(options::OPT_fsigned_char, options::OPT_funsigned_char,
Daniel Dunbard609b7b2009-11-17 06:37:03 +00001748 isSignedCharDefault(getToolChain().getTriple())))
Daniel Dunbar5fe08662009-11-29 02:39:08 +00001749 CmdArgs.push_back("-fno-signed-char");
Eli Friedman327f0b52009-06-05 07:21:14 +00001750
Anders Carlssonfcd764a2010-02-06 23:23:06 +00001751 // -fthreadsafe-static is default.
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001752 if (!Args.hasFlag(options::OPT_fthreadsafe_statics,
Anders Carlssonfcd764a2010-02-06 23:23:06 +00001753 options::OPT_fno_threadsafe_statics))
1754 CmdArgs.push_back("-fno-threadsafe-statics");
1755
Daniel Dunbarfe06df42010-03-20 04:15:41 +00001756 // -fuse-cxa-atexit is default.
Anton Korobeynikov82b33332010-09-11 11:17:06 +00001757 if (KernelOrKext ||
1758 !Args.hasFlag(options::OPT_fuse_cxa_atexit, options::OPT_fno_use_cxa_atexit,
NAKAMURA Takumi6bdc8a22010-10-10 01:53:03 +00001759 getToolChain().getTriple().getOS() != llvm::Triple::Cygwin &&
NAKAMURA Takumi31ea2f12011-02-17 08:51:38 +00001760 getToolChain().getTriple().getOS() != llvm::Triple::MinGW32))
Daniel Dunbarfe06df42010-03-20 04:15:41 +00001761 CmdArgs.push_back("-fno-use-cxa-atexit");
1762
Daniel Dunbar0730e4f2009-11-17 07:06:20 +00001763 // -fms-extensions=0 is default.
Daniel Dunbar5bdd2992009-11-25 10:14:30 +00001764 if (Args.hasFlag(options::OPT_fms_extensions, options::OPT_fno_ms_extensions,
Daniel Dunbar0730e4f2009-11-17 07:06:20 +00001765 getToolChain().getTriple().getOS() == llvm::Triple::Win32))
1766 CmdArgs.push_back("-fms-extensions");
1767
Michael J. Spencer4992ca4b2010-10-21 05:21:48 +00001768 // -fmsc-version=1300 is default.
1769 if (Args.hasFlag(options::OPT_fms_extensions, options::OPT_fno_ms_extensions,
1770 getToolChain().getTriple().getOS() == llvm::Triple::Win32) ||
1771 Args.hasArg(options::OPT_fmsc_version)) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001772 StringRef msc_ver = Args.getLastArgValue(options::OPT_fmsc_version);
Michael J. Spencer4992ca4b2010-10-21 05:21:48 +00001773 if (msc_ver.empty())
1774 CmdArgs.push_back("-fmsc-version=1300");
1775 else
1776 CmdArgs.push_back(Args.MakeArgString("-fmsc-version=" + msc_ver));
1777 }
1778
1779
Dawn Perchik68bb1b42010-09-02 23:59:25 +00001780 // -fborland-extensions=0 is default.
1781 if (Args.hasFlag(options::OPT_fborland_extensions,
1782 options::OPT_fno_borland_extensions, false))
1783 CmdArgs.push_back("-fborland-extensions");
1784
Francois Pichet1c229c02011-04-22 22:18:13 +00001785 // -fno-delayed-template-parsing is default.
1786 if (Args.hasFlag(options::OPT_fdelayed_template_parsing,
1787 options::OPT_fno_delayed_template_parsing,
Francois Pichet35bc5de2011-08-26 00:22:34 +00001788 false))
1789 CmdArgs.push_back("-fdelayed-template-parsing");
Francois Pichet1c229c02011-04-22 22:18:13 +00001790
Chandler Carruthe03aa552010-04-17 20:17:31 +00001791 // -fgnu-keywords default varies depending on language; only pass if
1792 // specified.
1793 if (Arg *A = Args.getLastArg(options::OPT_fgnu_keywords,
Daniel Dunbardb059592010-04-24 17:56:39 +00001794 options::OPT_fno_gnu_keywords))
1795 A->render(Args, CmdArgs);
Chandler Carruthe03aa552010-04-17 20:17:31 +00001796
Rafael Espindola922a6242011-06-02 17:30:53 +00001797 if (Args.hasFlag(options::OPT_fgnu89_inline,
1798 options::OPT_fno_gnu89_inline,
1799 false))
Rafael Espindolafb2af642011-06-02 16:13:27 +00001800 CmdArgs.push_back("-fgnu89-inline");
1801
Daniel Dunbar4930e332009-11-17 08:07:36 +00001802 // -fobjc-nonfragile-abi=0 is default.
John McCall24fc0de2011-07-06 00:26:06 +00001803 ObjCRuntime objCRuntime;
John McCallb5f652e2011-06-22 00:53:57 +00001804 unsigned objcABIVersion = 0;
Daniel Dunbar4930e332009-11-17 08:07:36 +00001805 if (types::isObjC(InputType)) {
John McCall24fc0de2011-07-06 00:26:06 +00001806 bool NeXTRuntimeIsDefault
1807 = (IsRewriter || getToolChain().getTriple().isOSDarwin());
1808 if (Args.hasFlag(options::OPT_fnext_runtime, options::OPT_fgnu_runtime,
John McCall75bc7732011-07-06 02:36:30 +00001809 NeXTRuntimeIsDefault)) {
John McCall24fc0de2011-07-06 00:26:06 +00001810 objCRuntime.setKind(ObjCRuntime::NeXT);
John McCall75bc7732011-07-06 02:36:30 +00001811 } else {
1812 CmdArgs.push_back("-fgnu-runtime");
John McCall24fc0de2011-07-06 00:26:06 +00001813 objCRuntime.setKind(ObjCRuntime::GNU);
John McCall75bc7732011-07-06 02:36:30 +00001814 }
John McCall24fc0de2011-07-06 00:26:06 +00001815 getToolChain().configureObjCRuntime(objCRuntime);
1816 if (objCRuntime.HasARC)
1817 CmdArgs.push_back("-fobjc-runtime-has-arc");
1818 if (objCRuntime.HasWeak)
1819 CmdArgs.push_back("-fobjc-runtime-has-weak");
John McCall9de19782011-07-06 01:22:26 +00001820 if (objCRuntime.HasTerminate)
1821 CmdArgs.push_back("-fobjc-runtime-has-terminate");
John McCall24fc0de2011-07-06 00:26:06 +00001822
Daniel Dunbarc1dd0e92010-09-20 18:19:55 +00001823 // Compute the Objective-C ABI "version" to use. Version numbers are
1824 // slightly confusing for historical reasons:
1825 // 1 - Traditional "fragile" ABI
1826 // 2 - Non-fragile ABI, version 1
1827 // 3 - Non-fragile ABI, version 2
John McCallb5f652e2011-06-22 00:53:57 +00001828 objcABIVersion = 1;
Daniel Dunbarc1dd0e92010-09-20 18:19:55 +00001829 // If -fobjc-abi-version= is present, use that to set the version.
Daniel Dunbar12c82082010-04-28 23:25:24 +00001830 if (Arg *A = Args.getLastArg(options::OPT_fobjc_abi_version_EQ)) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001831 if (StringRef(A->getValue(Args)) == "1")
John McCallb5f652e2011-06-22 00:53:57 +00001832 objcABIVersion = 1;
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001833 else if (StringRef(A->getValue(Args)) == "2")
John McCallb5f652e2011-06-22 00:53:57 +00001834 objcABIVersion = 2;
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001835 else if (StringRef(A->getValue(Args)) == "3")
John McCallb5f652e2011-06-22 00:53:57 +00001836 objcABIVersion = 3;
Daniel Dunbar12c82082010-04-28 23:25:24 +00001837 else
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001838 D.Diag(diag::err_drv_clang_unsupported) << A->getAsString(Args);
Daniel Dunbarc1dd0e92010-09-20 18:19:55 +00001839 } else {
1840 // Otherwise, determine if we are using the non-fragile ABI.
1841 if (Args.hasFlag(options::OPT_fobjc_nonfragile_abi,
1842 options::OPT_fno_objc_nonfragile_abi,
1843 getToolChain().IsObjCNonFragileABIDefault())) {
1844 // Determine the non-fragile ABI version to use.
Daniel Dunbaraeed5fe2010-11-11 16:08:59 +00001845#ifdef DISABLE_DEFAULT_NONFRAGILEABI_TWO
1846 unsigned NonFragileABIVersion = 1;
1847#else
Daniel Dunbarc1dd0e92010-09-20 18:19:55 +00001848 unsigned NonFragileABIVersion = 2;
Daniel Dunbaraeed5fe2010-11-11 16:08:59 +00001849#endif
Daniel Dunbarc1dd0e92010-09-20 18:19:55 +00001850
1851 if (Arg *A = Args.getLastArg(
1852 options::OPT_fobjc_nonfragile_abi_version_EQ)) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001853 if (StringRef(A->getValue(Args)) == "1")
Daniel Dunbarc1dd0e92010-09-20 18:19:55 +00001854 NonFragileABIVersion = 1;
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001855 else if (StringRef(A->getValue(Args)) == "2")
Daniel Dunbarc1dd0e92010-09-20 18:19:55 +00001856 NonFragileABIVersion = 2;
1857 else
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001858 D.Diag(diag::err_drv_clang_unsupported)
Daniel Dunbarc1dd0e92010-09-20 18:19:55 +00001859 << A->getAsString(Args);
1860 }
1861
John McCallb5f652e2011-06-22 00:53:57 +00001862 objcABIVersion = 1 + NonFragileABIVersion;
Daniel Dunbarc1dd0e92010-09-20 18:19:55 +00001863 } else {
John McCallb5f652e2011-06-22 00:53:57 +00001864 objcABIVersion = 1;
Daniel Dunbarc1dd0e92010-09-20 18:19:55 +00001865 }
Daniel Dunbar12c82082010-04-28 23:25:24 +00001866 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001867
John McCallb5f652e2011-06-22 00:53:57 +00001868 if (objcABIVersion == 2 || objcABIVersion == 3) {
Fariborz Jahanian3aa19e9a2011-01-04 20:05:20 +00001869 CmdArgs.push_back("-fobjc-nonfragile-abi");
Daniel Dunbarfca18c1b42010-04-24 17:56:46 +00001870
1871 // -fobjc-dispatch-method is only relevant with the nonfragile-abi, and
1872 // legacy is the default.
1873 if (!Args.hasFlag(options::OPT_fobjc_legacy_dispatch,
1874 options::OPT_fno_objc_legacy_dispatch,
1875 getToolChain().IsObjCLegacyDispatchDefault())) {
1876 if (getToolChain().UseObjCMixedDispatch())
1877 CmdArgs.push_back("-fobjc-dispatch-method=mixed");
1878 else
1879 CmdArgs.push_back("-fobjc-dispatch-method=non-legacy");
1880 }
Daniel Dunbar2e3f2c82010-02-01 21:07:25 +00001881 }
NAKAMURA Takumi029d74b2011-02-17 08:50:50 +00001882
Ted Kremenek1d56c9e2010-12-23 21:35:43 +00001883 // -fobjc-default-synthesize-properties=0 is default.
1884 if (Args.hasFlag(options::OPT_fobjc_default_synthesize_properties,
1885 options::OPT_fno_objc_default_synthesize_properties,
1886 getToolChain().IsObjCDefaultSynthPropertiesDefault())) {
1887 CmdArgs.push_back("-fobjc-default-synthesize-properties");
1888 }
Daniel Dunbar4930e332009-11-17 08:07:36 +00001889 }
1890
John McCall24fc0de2011-07-06 00:26:06 +00001891 // Allow -fno-objc-arr to trump -fobjc-arr/-fobjc-arc.
1892 // NOTE: This logic is duplicated in ToolChains.cpp.
1893 bool ARC = isObjCAutoRefCount(Args);
1894 if (ARC) {
1895 CmdArgs.push_back("-fobjc-arc");
1896
1897 // Allow the user to enable full exceptions code emission.
1898 // We define off for Objective-CC, on for Objective-C++.
1899 if (Args.hasFlag(options::OPT_fobjc_arc_exceptions,
1900 options::OPT_fno_objc_arc_exceptions,
1901 /*default*/ types::isCXX(InputType)))
1902 CmdArgs.push_back("-fobjc-arc-exceptions");
1903 }
1904
1905 // -fobjc-infer-related-result-type is the default, except in the Objective-C
1906 // rewriter.
1907 if (IsRewriter)
1908 CmdArgs.push_back("-fno-objc-infer-related-result-type");
Eric Christopher84fbdb42011-08-19 00:30:14 +00001909
John McCall24fc0de2011-07-06 00:26:06 +00001910 // Handle -fobjc-gc and -fobjc-gc-only. They are exclusive, and -fobjc-gc-only
1911 // takes precedence.
1912 const Arg *GCArg = Args.getLastArg(options::OPT_fobjc_gc_only);
1913 if (!GCArg)
1914 GCArg = Args.getLastArg(options::OPT_fobjc_gc);
1915 if (GCArg) {
1916 if (ARC) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001917 D.Diag(diag::err_drv_objc_gc_arr)
John McCall24fc0de2011-07-06 00:26:06 +00001918 << GCArg->getAsString(Args);
1919 } else if (getToolChain().SupportsObjCGC()) {
1920 GCArg->render(Args, CmdArgs);
1921 } else {
1922 // FIXME: We should move this to a hard error.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001923 D.Diag(diag::warn_drv_objc_gc_unsupported)
John McCall24fc0de2011-07-06 00:26:06 +00001924 << GCArg->getAsString(Args);
1925 }
1926 }
1927
John McCallb5f652e2011-06-22 00:53:57 +00001928 // Add exception args.
1929 addExceptionArgs(Args, InputType, getToolChain().getTriple(),
1930 KernelOrKext, IsRewriter, objcABIVersion, CmdArgs);
1931
1932 if (getToolChain().UseSjLjExceptions())
1933 CmdArgs.push_back("-fsjlj-exceptions");
1934
1935 // C++ "sane" operator new.
Daniel Dunbar2e3f2c82010-02-01 21:07:25 +00001936 if (!Args.hasFlag(options::OPT_fassume_sane_operator_new,
1937 options::OPT_fno_assume_sane_operator_new))
1938 CmdArgs.push_back("-fno-assume-sane-operator-new");
1939
Daniel Dunbar34d7a992010-04-27 15:34:57 +00001940 // -fconstant-cfstrings is default, and may be subject to argument translation
1941 // on Darwin.
1942 if (!Args.hasFlag(options::OPT_fconstant_cfstrings,
1943 options::OPT_fno_constant_cfstrings) ||
1944 !Args.hasFlag(options::OPT_mconstant_cfstrings,
1945 options::OPT_mno_constant_cfstrings))
1946 CmdArgs.push_back("-fno-constant-cfstrings");
1947
John Thompsoned4e2952009-11-05 20:14:16 +00001948 // -fshort-wchar default varies depending on platform; only
1949 // pass if specified.
Daniel Dunbar2cb4e7a2010-04-27 15:35:03 +00001950 if (Arg *A = Args.getLastArg(options::OPT_fshort_wchar))
1951 A->render(Args, CmdArgs);
John Thompsoned4e2952009-11-05 20:14:16 +00001952
Daniel Dunbar1a8a2e82009-10-29 02:39:57 +00001953 // -fno-pascal-strings is default, only pass non-default. If the tool chain
1954 // happened to translate to -mpascal-strings, we want to back translate here.
Daniel Dunbard4510f22009-04-07 23:51:44 +00001955 //
1956 // FIXME: This is gross; that translation should be pulled from the
1957 // tool chain.
Daniel Dunbard067f7f2009-04-08 23:54:23 +00001958 if (Args.hasFlag(options::OPT_fpascal_strings,
Daniel Dunbard4510f22009-04-07 23:51:44 +00001959 options::OPT_fno_pascal_strings,
1960 false) ||
1961 Args.hasFlag(options::OPT_mpascal_strings,
1962 options::OPT_mno_pascal_strings,
1963 false))
Daniel Dunbard18049a2009-04-07 21:16:11 +00001964 CmdArgs.push_back("-fpascal-strings");
NAKAMURA Takumi029d74b2011-02-17 08:50:50 +00001965
Fariborz Jahaniana4cfff82011-01-07 01:05:02 +00001966 if (Args.hasArg(options::OPT_mkernel) ||
1967 Args.hasArg(options::OPT_fapple_kext)) {
1968 if (!Args.hasArg(options::OPT_fcommon))
1969 CmdArgs.push_back("-fno-common");
1970 }
Daniel Dunbard18049a2009-04-07 21:16:11 +00001971 // -fcommon is default, only pass non-default.
Fariborz Jahaniana4cfff82011-01-07 01:05:02 +00001972 else if (!Args.hasFlag(options::OPT_fcommon, options::OPT_fno_common))
Daniel Dunbard18049a2009-04-07 21:16:11 +00001973 CmdArgs.push_back("-fno-common");
1974
Daniel Dunbar2edd9232009-04-15 02:37:43 +00001975 // -fsigned-bitfields is default, and clang doesn't yet support
Daniel Dunbar6358d682010-10-15 22:30:42 +00001976 // -funsigned-bitfields.
Mike Stump11289f42009-09-09 15:08:12 +00001977 if (!Args.hasFlag(options::OPT_fsigned_bitfields,
Daniel Dunbar2edd9232009-04-15 02:37:43 +00001978 options::OPT_funsigned_bitfields))
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001979 D.Diag(diag::warn_drv_clang_unsupported)
Daniel Dunbar2edd9232009-04-15 02:37:43 +00001980 << Args.getLastArg(options::OPT_funsigned_bitfields)->getAsString(Args);
1981
Daniel Dunbar6358d682010-10-15 22:30:42 +00001982 // -fsigned-bitfields is default, and clang doesn't support -fno-for-scope.
1983 if (!Args.hasFlag(options::OPT_ffor_scope,
1984 options::OPT_fno_for_scope))
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001985 D.Diag(diag::err_drv_clang_unsupported)
Daniel Dunbar6358d682010-10-15 22:30:42 +00001986 << Args.getLastArg(options::OPT_fno_for_scope)->getAsString(Args);
1987
Jeffrey Yasskin460aa542010-06-08 04:56:20 +00001988 // -fcaret-diagnostics is default.
1989 if (!Args.hasFlag(options::OPT_fcaret_diagnostics,
1990 options::OPT_fno_caret_diagnostics, true))
1991 CmdArgs.push_back("-fno-caret-diagnostics");
1992
Daniel Dunbar8281bde2009-04-19 21:09:34 +00001993 // -fdiagnostics-fixit-info is default, only pass non-default.
Mike Stump11289f42009-09-09 15:08:12 +00001994 if (!Args.hasFlag(options::OPT_fdiagnostics_fixit_info,
Daniel Dunbar8281bde2009-04-19 21:09:34 +00001995 options::OPT_fno_diagnostics_fixit_info))
1996 CmdArgs.push_back("-fno-diagnostics-fixit-info");
Eric Christopher84fbdb42011-08-19 00:30:14 +00001997
Douglas Gregor46ce91a2011-04-15 22:04:17 +00001998 // Enable -fdiagnostics-show-name by default.
1999 if (Args.hasFlag(options::OPT_fdiagnostics_show_name,
2000 options::OPT_fno_diagnostics_show_name, false))
2001 CmdArgs.push_back("-fdiagnostics-show-name");
Daniel Dunbar8281bde2009-04-19 21:09:34 +00002002
Daniel Dunbar092f0cc2009-04-16 06:32:38 +00002003 // Enable -fdiagnostics-show-option by default.
Mike Stump11289f42009-09-09 15:08:12 +00002004 if (Args.hasFlag(options::OPT_fdiagnostics_show_option,
Daniel Dunbar092f0cc2009-04-16 06:32:38 +00002005 options::OPT_fno_diagnostics_show_option))
2006 CmdArgs.push_back("-fdiagnostics-show-option");
Daniel Dunbar5ec95022009-11-04 06:24:57 +00002007
Chris Lattnerbf6fac82010-05-04 21:55:25 +00002008 if (const Arg *A =
2009 Args.getLastArg(options::OPT_fdiagnostics_show_category_EQ)) {
2010 CmdArgs.push_back("-fdiagnostics-show-category");
2011 CmdArgs.push_back(A->getValue(Args));
2012 }
Daniel Dunbarfe6c97b2010-08-24 16:47:49 +00002013
Douglas Gregor643c9222011-05-21 17:07:29 +00002014 if (const Arg *A =
2015 Args.getLastArg(options::OPT_fdiagnostics_format_EQ)) {
2016 CmdArgs.push_back("-fdiagnostics-format");
2017 CmdArgs.push_back(A->getValue(Args));
2018 }
2019
Chandler Carruthb6766f02011-03-27 01:50:55 +00002020 if (Arg *A = Args.getLastArg(
2021 options::OPT_fdiagnostics_show_note_include_stack,
2022 options::OPT_fno_diagnostics_show_note_include_stack)) {
2023 if (A->getOption().matches(
2024 options::OPT_fdiagnostics_show_note_include_stack))
2025 CmdArgs.push_back("-fdiagnostics-show-note-include-stack");
2026 else
2027 CmdArgs.push_back("-fno-diagnostics-show-note-include-stack");
2028 }
2029
Daniel Dunbar5ec95022009-11-04 06:24:57 +00002030 // Color diagnostics are the default, unless the terminal doesn't support
2031 // them.
2032 if (Args.hasFlag(options::OPT_fcolor_diagnostics,
Argyrios Kyrtzidis4f920162010-09-23 12:56:06 +00002033 options::OPT_fno_color_diagnostics,
2034 llvm::sys::Process::StandardErrHasColors()))
Daniel Dunbar5ec95022009-11-04 06:24:57 +00002035 CmdArgs.push_back("-fcolor-diagnostics");
2036
Daniel Dunbardb097022009-06-08 21:13:54 +00002037 if (!Args.hasFlag(options::OPT_fshow_source_location,
2038 options::OPT_fno_show_source_location))
2039 CmdArgs.push_back("-fno-show-source-location");
Daniel Dunbar092f0cc2009-04-16 06:32:38 +00002040
Douglas Gregor643c9222011-05-21 17:07:29 +00002041 if (!Args.hasFlag(options::OPT_fshow_column,
2042 options::OPT_fno_show_column,
2043 true))
2044 CmdArgs.push_back("-fno-show-column");
2045
Douglas Gregor8ed0c0b2010-07-09 17:35:33 +00002046 if (!Args.hasFlag(options::OPT_fspell_checking,
2047 options::OPT_fno_spell_checking))
2048 CmdArgs.push_back("-fno-spell-checking");
Daniel Dunbarfe6c97b2010-08-24 16:47:49 +00002049
Daniel Dunbar473f8a62010-10-18 22:49:46 +00002050
Daniel Dunbar3ada2b72010-11-02 19:42:04 +00002051 // Silently ignore -fasm-blocks for now.
2052 (void) Args.hasFlag(options::OPT_fasm_blocks, options::OPT_fno_asm_blocks,
2053 false);
Daniel Dunbar473f8a62010-10-18 22:49:46 +00002054
Jeffrey Yasskin2b99c6f2010-06-11 05:57:47 +00002055 if (Arg *A = Args.getLastArg(options::OPT_fshow_overloads_EQ))
2056 A->render(Args, CmdArgs);
2057
Daniel Dunbarf5e9b1f2009-04-19 21:20:32 +00002058 // -fdollars-in-identifiers default varies depending on platform and
2059 // language; only pass if specified.
Mike Stump11289f42009-09-09 15:08:12 +00002060 if (Arg *A = Args.getLastArg(options::OPT_fdollars_in_identifiers,
Daniel Dunbarf5e9b1f2009-04-19 21:20:32 +00002061 options::OPT_fno_dollars_in_identifiers)) {
2062 if (A->getOption().matches(options::OPT_fdollars_in_identifiers))
Daniel Dunbar15cef0e2009-12-16 20:10:18 +00002063 CmdArgs.push_back("-fdollars-in-identifiers");
Daniel Dunbarf5e9b1f2009-04-19 21:20:32 +00002064 else
Daniel Dunbar15cef0e2009-12-16 20:10:18 +00002065 CmdArgs.push_back("-fno-dollars-in-identifiers");
Daniel Dunbarf5e9b1f2009-04-19 21:20:32 +00002066 }
2067
Daniel Dunbaradeeb052009-05-22 19:02:20 +00002068 // -funit-at-a-time is default, and we don't support -fno-unit-at-a-time for
2069 // practical purposes.
Mike Stump11289f42009-09-09 15:08:12 +00002070 if (Arg *A = Args.getLastArg(options::OPT_funit_at_a_time,
Daniel Dunbaradeeb052009-05-22 19:02:20 +00002071 options::OPT_fno_unit_at_a_time)) {
2072 if (A->getOption().matches(options::OPT_fno_unit_at_a_time))
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002073 D.Diag(diag::warn_drv_clang_unsupported) << A->getAsString(Args);
Daniel Dunbaradeeb052009-05-22 19:02:20 +00002074 }
Eli Friedmanbb0d9a52009-07-14 21:58:17 +00002075
Daniel Dunbar2ffe0292009-09-10 03:37:02 +00002076 // Default to -fno-builtin-str{cat,cpy} on Darwin for ARM.
Daniel Dunbar4fa08112009-09-10 04:57:27 +00002077 //
Daniel Dunbar6c536aa2009-12-11 23:00:49 +00002078 // FIXME: This is disabled until clang -cc1 supports -fno-builtin-foo. PR4941.
Daniel Dunbar4fa08112009-09-10 04:57:27 +00002079#if 0
Daniel Dunbar2ffe0292009-09-10 03:37:02 +00002080 if (getToolChain().getTriple().getOS() == llvm::Triple::Darwin &&
2081 (getToolChain().getTriple().getArch() == llvm::Triple::arm ||
2082 getToolChain().getTriple().getArch() == llvm::Triple::thumb)) {
2083 if (!Args.hasArg(options::OPT_fbuiltin_strcat))
2084 CmdArgs.push_back("-fno-builtin-strcat");
2085 if (!Args.hasArg(options::OPT_fbuiltin_strcpy))
2086 CmdArgs.push_back("-fno-builtin-strcpy");
2087 }
Daniel Dunbar4fa08112009-09-10 04:57:27 +00002088#endif
Daniel Dunbar2ffe0292009-09-10 03:37:02 +00002089
Daniel Dunbar8c3d7352011-03-18 21:23:40 +00002090 // Only allow -traditional or -traditional-cpp outside in preprocessing modes.
Mike Stump11289f42009-09-09 15:08:12 +00002091 if (Arg *A = Args.getLastArg(options::OPT_traditional,
Daniel Dunbar8c3d7352011-03-18 21:23:40 +00002092 options::OPT_traditional_cpp)) {
2093 if (isa<PreprocessJobAction>(JA))
2094 CmdArgs.push_back("-traditional-cpp");
Eric Christopher84fbdb42011-08-19 00:30:14 +00002095 else
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002096 D.Diag(diag::err_drv_clang_unsupported) << A->getAsString(Args);
Daniel Dunbar8c3d7352011-03-18 21:23:40 +00002097 }
Eli Friedmanbb0d9a52009-07-14 21:58:17 +00002098
Daniel Dunbara2aedc62009-03-18 10:01:51 +00002099 Args.AddLastArg(CmdArgs, options::OPT_dM);
Chris Lattnercac63f32009-04-12 01:56:53 +00002100 Args.AddLastArg(CmdArgs, options::OPT_dD);
Daniel Dunbara2aedc62009-03-18 10:01:51 +00002101
Daniel Dunbar76fa8402010-04-15 06:09:03 +00002102 // Forward -Xclang arguments to -cc1, and -mllvm arguments to the LLVM option
2103 // parser.
Daniel Dunbara2aedc62009-03-18 10:01:51 +00002104 Args.AddAllArgValues(CmdArgs, options::OPT_Xclang);
Daniel Dunbar76fa8402010-04-15 06:09:03 +00002105 for (arg_iterator it = Args.filtered_begin(options::OPT_mllvm),
2106 ie = Args.filtered_end(); it != ie; ++it) {
Daniel Dunbara442fd52010-06-11 22:00:13 +00002107 (*it)->claim();
Daniel Dunbar88534f42010-04-17 06:10:00 +00002108
Daniel Dunbar76fa8402010-04-15 06:09:03 +00002109 // We translate this by hand to the -cc1 argument, since nightly test uses
2110 // it and developers have been trained to spell it with -mllvm.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002111 if (StringRef((*it)->getValue(Args, 0)) == "-disable-llvm-optzns")
Daniel Dunbar76fa8402010-04-15 06:09:03 +00002112 CmdArgs.push_back("-disable-llvm-optzns");
2113 else
Daniel Dunbara442fd52010-06-11 22:00:13 +00002114 (*it)->render(Args, CmdArgs);
Daniel Dunbar76fa8402010-04-15 06:09:03 +00002115 }
Daniel Dunbara2aedc62009-03-18 10:01:51 +00002116
Daniel Dunbard67a3222009-03-30 06:36:42 +00002117 if (Output.getType() == types::TY_Dependencies) {
2118 // Handled with other dependency code.
Daniel Dunbar5cdf3e02009-03-19 07:29:38 +00002119 } else if (Output.isFilename()) {
Daniel Dunbara3246a02009-03-18 08:07:30 +00002120 CmdArgs.push_back("-o");
Daniel Dunbar5cdf3e02009-03-19 07:29:38 +00002121 CmdArgs.push_back(Output.getFilename());
2122 } else {
2123 assert(Output.isNothing() && "Invalid output.");
Daniel Dunbara3246a02009-03-18 08:07:30 +00002124 }
2125
Daniel Dunbara2aedc62009-03-18 10:01:51 +00002126 for (InputInfoList::const_iterator
2127 it = Inputs.begin(), ie = Inputs.end(); it != ie; ++it) {
2128 const InputInfo &II = *it;
2129 CmdArgs.push_back("-x");
2130 CmdArgs.push_back(types::getTypeName(II.getType()));
Daniel Dunbarb440f562010-08-02 02:38:21 +00002131 if (II.isFilename())
Daniel Dunbar5cdf3e02009-03-19 07:29:38 +00002132 CmdArgs.push_back(II.getFilename());
Daniel Dunbara2aedc62009-03-18 10:01:51 +00002133 else
Daniel Dunbar5cdf3e02009-03-19 07:29:38 +00002134 II.getInputArg().renderAsInput(Args, CmdArgs);
Daniel Dunbara2aedc62009-03-18 10:01:51 +00002135 }
Daniel Dunbard067f7f2009-04-08 23:54:23 +00002136
Chris Lattnere9d7d782009-11-03 19:50:27 +00002137 Args.AddAllArgs(CmdArgs, options::OPT_undef);
2138
Daniel Dunbarb31b76f2010-07-18 21:16:15 +00002139 const char *Exec = getToolChain().getDriver().getClangProgramPath();
Daniel Dunbar24c7f5e2009-12-18 02:43:17 +00002140
2141 // Optionally embed the -cc1 level arguments into the debug info, for build
2142 // analysis.
2143 if (getToolChain().UseDwarfDebugFlags()) {
Daniel Dunbar7f3d9502010-06-04 18:47:06 +00002144 ArgStringList OriginalArgs;
2145 for (ArgList::const_iterator it = Args.begin(),
2146 ie = Args.end(); it != ie; ++it)
2147 (*it)->render(Args, OriginalArgs);
Daniel Dunbarfe6c97b2010-08-24 16:47:49 +00002148
Daniel Dunbar24c7f5e2009-12-18 02:43:17 +00002149 llvm::SmallString<256> Flags;
2150 Flags += Exec;
Daniel Dunbar7f3d9502010-06-04 18:47:06 +00002151 for (unsigned i = 0, e = OriginalArgs.size(); i != e; ++i) {
Daniel Dunbar24c7f5e2009-12-18 02:43:17 +00002152 Flags += " ";
Daniel Dunbar7f3d9502010-06-04 18:47:06 +00002153 Flags += OriginalArgs[i];
Daniel Dunbar24c7f5e2009-12-18 02:43:17 +00002154 }
2155 CmdArgs.push_back("-dwarf-debug-flags");
2156 CmdArgs.push_back(Args.MakeArgString(Flags.str()));
2157 }
2158
Daniel Dunbar7fbaf532010-08-02 02:38:28 +00002159 C.addCommand(new Command(JA, *this, Exec, CmdArgs));
Daniel Dunbar17731772009-03-23 19:03:36 +00002160
Roman Divacky178e01602011-02-10 16:52:03 +00002161 if (Arg *A = Args.getLastArg(options::OPT_pg))
2162 if (Args.hasArg(options::OPT_fomit_frame_pointer))
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002163 D.Diag(diag::err_drv_argument_not_allowed_with)
Roman Divacky178e01602011-02-10 16:52:03 +00002164 << "-fomit-frame-pointer" << A->getAsString(Args);
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00002165
Daniel Dunbarc2a71892009-04-03 20:51:31 +00002166 // Claim some arguments which clang supports automatically.
2167
Daniel Dunbar3e0cac62010-04-15 06:18:42 +00002168 // -fpch-preprocess is used with gcc to add a special marker in the output to
2169 // include the PCH file. Clang's PTH solution is completely transparent, so we
2170 // do not need to deal with it at all.
Daniel Dunbarc2a71892009-04-03 20:51:31 +00002171 Args.ClaimAllArgs(options::OPT_fpch_preprocess);
Daniel Dunbard067f7f2009-04-08 23:54:23 +00002172
Daniel Dunbar17731772009-03-23 19:03:36 +00002173 // Claim some arguments which clang doesn't support, but we don't
2174 // care to warn the user about.
Daniel Dunbar44b36ee2009-11-25 11:53:23 +00002175 Args.ClaimAllArgs(options::OPT_clang_ignored_f_Group);
2176 Args.ClaimAllArgs(options::OPT_clang_ignored_m_Group);
Rafael Espindola22f603032011-02-28 23:29:45 +00002177
Rafael Espindolad95a8122011-03-01 05:25:27 +00002178 // Disable warnings for clang -E -use-gold-plugin -emit-llvm foo.c
Rafael Espindola22f603032011-02-28 23:29:45 +00002179 Args.ClaimAllArgs(options::OPT_use_gold_plugin);
Rafael Espindolad95a8122011-03-01 05:25:27 +00002180 Args.ClaimAllArgs(options::OPT_emit_llvm);
Daniel Dunbar1a093d22009-03-18 06:00:36 +00002181}
2182
Daniel Dunbar4f5e79c2010-05-20 21:30:13 +00002183void ClangAs::ConstructJob(Compilation &C, const JobAction &JA,
Daniel Dunbar4f5e79c2010-05-20 21:30:13 +00002184 const InputInfo &Output,
2185 const InputInfoList &Inputs,
2186 const ArgList &Args,
2187 const char *LinkingOutput) const {
Daniel Dunbar4f5e79c2010-05-20 21:30:13 +00002188 ArgStringList CmdArgs;
2189
2190 assert(Inputs.size() == 1 && "Unexpected number of inputs.");
2191 const InputInfo &Input = Inputs[0];
2192
Rafael Espindolacfaadda2010-11-17 22:13:25 +00002193 // Don't warn about "clang -w -c foo.s"
2194 Args.ClaimAllArgs(options::OPT_w);
Rafael Espindolad95a8122011-03-01 05:25:27 +00002195 // and "clang -emit-llvm -c foo.s"
2196 Args.ClaimAllArgs(options::OPT_emit_llvm);
2197 // and "clang -use-gold-plugin -c foo.s"
2198 Args.ClaimAllArgs(options::OPT_use_gold_plugin);
Rafael Espindolacfaadda2010-11-17 22:13:25 +00002199
Daniel Dunbar4f5e79c2010-05-20 21:30:13 +00002200 // Invoke ourselves in -cc1as mode.
2201 //
2202 // FIXME: Implement custom jobs for internal actions.
2203 CmdArgs.push_back("-cc1as");
2204
2205 // Add the "effective" target triple.
2206 CmdArgs.push_back("-triple");
Daniel Dunbar82eb4ce2010-08-23 22:35:37 +00002207 std::string TripleStr = getToolChain().ComputeEffectiveClangTriple(Args);
Daniel Dunbar4f5e79c2010-05-20 21:30:13 +00002208 CmdArgs.push_back(Args.MakeArgString(TripleStr));
2209
2210 // Set the output mode, we currently only expect to be used as a real
2211 // assembler.
2212 CmdArgs.push_back("-filetype");
2213 CmdArgs.push_back("obj");
2214
Joerg Sonnenbergeref317a22011-05-06 14:35:16 +00002215 if (UseRelaxAll(C, Args))
Daniel Dunbar99ca8b72010-05-28 16:43:21 +00002216 CmdArgs.push_back("-relax-all");
Daniel Dunbar06e2cc32010-05-27 06:18:05 +00002217
Daniel Dunbar1d733e22011-03-17 17:37:29 +00002218 // Ignore explicit -force_cpusubtype_ALL option.
2219 (void) Args.hasArg(options::OPT_force__cpusubtype__ALL);
Daniel Dunbar4f5e79c2010-05-20 21:30:13 +00002220
2221 // FIXME: Add -g support, once we have it.
2222
2223 // FIXME: Add -static support, once we have it.
2224
2225 Args.AddAllArgValues(CmdArgs, options::OPT_Wa_COMMA,
2226 options::OPT_Xassembler);
Daniel Dunbar252e8f92011-04-29 17:53:18 +00002227 Args.AddAllArgs(CmdArgs, options::OPT_mllvm);
Daniel Dunbar4f5e79c2010-05-20 21:30:13 +00002228
2229 assert(Output.isFilename() && "Unexpected lipo output.");
2230 CmdArgs.push_back("-o");
2231 CmdArgs.push_back(Output.getFilename());
2232
Daniel Dunbarb440f562010-08-02 02:38:21 +00002233 assert(Input.isFilename() && "Invalid input.");
2234 CmdArgs.push_back(Input.getFilename());
Daniel Dunbar4f5e79c2010-05-20 21:30:13 +00002235
Daniel Dunbarb31b76f2010-07-18 21:16:15 +00002236 const char *Exec = getToolChain().getDriver().getClangProgramPath();
Daniel Dunbar7fbaf532010-08-02 02:38:28 +00002237 C.addCommand(new Command(JA, *this, Exec, CmdArgs));
Daniel Dunbar4f5e79c2010-05-20 21:30:13 +00002238}
2239
Daniel Dunbara3246a02009-03-18 08:07:30 +00002240void gcc::Common::ConstructJob(Compilation &C, const JobAction &JA,
Daniel Dunbara3246a02009-03-18 08:07:30 +00002241 const InputInfo &Output,
2242 const InputInfoList &Inputs,
Daniel Dunbara2aedc62009-03-18 10:01:51 +00002243 const ArgList &Args,
Daniel Dunbara3246a02009-03-18 08:07:30 +00002244 const char *LinkingOutput) const {
Daniel Dunbar083edf72009-12-21 18:54:17 +00002245 const Driver &D = getToolChain().getDriver();
Daniel Dunbara3246a02009-03-18 08:07:30 +00002246 ArgStringList CmdArgs;
Daniel Dunbar1a093d22009-03-18 06:00:36 +00002247
Daniel Dunbard067f7f2009-04-08 23:54:23 +00002248 for (ArgList::const_iterator
Daniel Dunbara2aedc62009-03-18 10:01:51 +00002249 it = Args.begin(), ie = Args.end(); it != ie; ++it) {
Daniel Dunbara3246a02009-03-18 08:07:30 +00002250 Arg *A = *it;
Daniel Dunbar2da02722009-03-19 07:55:12 +00002251 if (A->getOption().hasForwardToGCC()) {
Daniel Dunbar939c1212010-08-03 16:14:14 +00002252 // Don't forward any -g arguments to assembly steps.
2253 if (isa<AssembleJobAction>(JA) &&
2254 A->getOption().matches(options::OPT_g_Group))
2255 continue;
2256
Daniel Dunbar2da02722009-03-19 07:55:12 +00002257 // It is unfortunate that we have to claim here, as this means
2258 // we will basically never report anything interesting for
Daniel Dunbar5716d872009-05-02 21:41:52 +00002259 // platforms using a generic gcc, even if we are just using gcc
2260 // to get to the assembler.
Daniel Dunbar2da02722009-03-19 07:55:12 +00002261 A->claim();
Daniel Dunbara2aedc62009-03-18 10:01:51 +00002262 A->render(Args, CmdArgs);
Daniel Dunbar2da02722009-03-19 07:55:12 +00002263 }
Daniel Dunbara3246a02009-03-18 08:07:30 +00002264 }
Daniel Dunbard067f7f2009-04-08 23:54:23 +00002265
Daniel Dunbar4e295052010-01-25 22:35:08 +00002266 RenderExtraToolArgs(JA, CmdArgs);
Daniel Dunbara3246a02009-03-18 08:07:30 +00002267
2268 // If using a driver driver, force the arch.
Daniel Dunbar5bbebfe2009-05-22 02:21:04 +00002269 const std::string &Arch = getToolChain().getArchName();
Daniel Dunbarf4894fe2009-12-23 00:46:38 +00002270 if (getToolChain().getTriple().getOS() == llvm::Triple::Darwin) {
Daniel Dunbara3246a02009-03-18 08:07:30 +00002271 CmdArgs.push_back("-arch");
Daniel Dunbar0a05d932009-04-01 20:33:11 +00002272
2273 // FIXME: Remove these special cases.
Daniel Dunbar5bbebfe2009-05-22 02:21:04 +00002274 if (Arch == "powerpc")
2275 CmdArgs.push_back("ppc");
2276 else if (Arch == "powerpc64")
2277 CmdArgs.push_back("ppc64");
2278 else
Daniel Dunbar1c28f1e2009-09-09 22:32:48 +00002279 CmdArgs.push_back(Args.MakeArgString(Arch));
Daniel Dunbara3246a02009-03-18 08:07:30 +00002280 }
2281
Daniel Dunbar5716d872009-05-02 21:41:52 +00002282 // Try to force gcc to match the tool chain we want, if we recognize
2283 // the arch.
Daniel Dunbar5bbebfe2009-05-22 02:21:04 +00002284 //
2285 // FIXME: The triple class should directly provide the information we want
2286 // here.
2287 if (Arch == "i386" || Arch == "powerpc")
Daniel Dunbar5716d872009-05-02 21:41:52 +00002288 CmdArgs.push_back("-m32");
Daniel Dunbar5bbebfe2009-05-22 02:21:04 +00002289 else if (Arch == "x86_64" || Arch == "powerpc64")
Daniel Dunbar5716d872009-05-02 21:41:52 +00002290 CmdArgs.push_back("-m64");
2291
Daniel Dunbarb440f562010-08-02 02:38:21 +00002292 if (Output.isFilename()) {
Daniel Dunbara3246a02009-03-18 08:07:30 +00002293 CmdArgs.push_back("-o");
Daniel Dunbar5cdf3e02009-03-19 07:29:38 +00002294 CmdArgs.push_back(Output.getFilename());
2295 } else {
2296 assert(Output.isNothing() && "Unexpected output");
Daniel Dunbara3246a02009-03-18 08:07:30 +00002297 CmdArgs.push_back("-fsyntax-only");
Daniel Dunbar5cdf3e02009-03-19 07:29:38 +00002298 }
Daniel Dunbara3246a02009-03-18 08:07:30 +00002299
2300
2301 // Only pass -x if gcc will understand it; otherwise hope gcc
2302 // understands the suffix correctly. The main use case this would go
2303 // wrong in is for linker inputs if they happened to have an odd
2304 // suffix; really the only way to get this to happen is a command
2305 // like '-x foobar a.c' which will treat a.c like a linker input.
2306 //
2307 // FIXME: For the linker case specifically, can we safely convert
2308 // inputs into '-Wl,' options?
2309 for (InputInfoList::const_iterator
2310 it = Inputs.begin(), ie = Inputs.end(); it != ie; ++it) {
2311 const InputInfo &II = *it;
Daniel Dunbare3e263f2009-05-02 20:14:53 +00002312
Daniel Dunbar6cdf83c2009-09-01 16:57:46 +00002313 // Don't try to pass LLVM or AST inputs to a generic gcc.
Daniel Dunbar24e52992010-06-07 23:28:45 +00002314 if (II.getType() == types::TY_LLVM_IR || II.getType() == types::TY_LTO_IR ||
2315 II.getType() == types::TY_LLVM_BC || II.getType() == types::TY_LTO_BC)
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002316 D.Diag(diag::err_drv_no_linker_llvm_support)
Daniel Dunbar1c28f1e2009-09-09 22:32:48 +00002317 << getToolChain().getTripleString();
Daniel Dunbar6cdf83c2009-09-01 16:57:46 +00002318 else if (II.getType() == types::TY_AST)
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002319 D.Diag(diag::err_drv_no_ast_support)
Daniel Dunbar1c28f1e2009-09-09 22:32:48 +00002320 << getToolChain().getTripleString();
Daniel Dunbare3e263f2009-05-02 20:14:53 +00002321
Daniel Dunbara3246a02009-03-18 08:07:30 +00002322 if (types::canTypeBeUserSpecified(II.getType())) {
2323 CmdArgs.push_back("-x");
2324 CmdArgs.push_back(types::getTypeName(II.getType()));
2325 }
2326
Daniel Dunbarb440f562010-08-02 02:38:21 +00002327 if (II.isFilename())
Daniel Dunbar5cdf3e02009-03-19 07:29:38 +00002328 CmdArgs.push_back(II.getFilename());
Daniel Dunbarf2476752010-09-25 18:10:05 +00002329 else {
2330 const Arg &A = II.getInputArg();
2331
2332 // Reverse translate some rewritten options.
2333 if (A.getOption().matches(options::OPT_Z_reserved_lib_stdcxx)) {
2334 CmdArgs.push_back("-lstdc++");
2335 continue;
2336 }
2337
Daniel Dunbar5cdf3e02009-03-19 07:29:38 +00002338 // Don't render as input, we need gcc to do the translations.
Daniel Dunbarf2476752010-09-25 18:10:05 +00002339 A.render(Args, CmdArgs);
2340 }
Daniel Dunbara3246a02009-03-18 08:07:30 +00002341 }
2342
Dylan Noblesmith70e73a32011-04-09 13:31:59 +00002343 const std::string customGCCName = D.getCCCGenericGCCName();
2344 const char *GCCName;
2345 if (!customGCCName.empty())
2346 GCCName = customGCCName.c_str();
2347 else if (D.CCCIsCXX) {
2348#ifdef IS_CYGWIN15
2349 // FIXME: Detect the version of Cygwin at runtime?
2350 GCCName = "g++-4";
2351#else
2352 GCCName = "g++";
2353#endif
2354 } else
2355 GCCName = "gcc";
2356
Daniel Dunbard067f7f2009-04-08 23:54:23 +00002357 const char *Exec =
Daniel Dunbar9c3ed5f2010-07-14 18:46:23 +00002358 Args.MakeArgString(getToolChain().GetProgramPath(GCCName));
Daniel Dunbar7fbaf532010-08-02 02:38:28 +00002359 C.addCommand(new Command(JA, *this, Exec, CmdArgs));
Daniel Dunbar1a093d22009-03-18 06:00:36 +00002360}
2361
Daniel Dunbar4e295052010-01-25 22:35:08 +00002362void gcc::Preprocess::RenderExtraToolArgs(const JobAction &JA,
2363 ArgStringList &CmdArgs) const {
Daniel Dunbara3246a02009-03-18 08:07:30 +00002364 CmdArgs.push_back("-E");
Daniel Dunbar1a093d22009-03-18 06:00:36 +00002365}
2366
Daniel Dunbar4e295052010-01-25 22:35:08 +00002367void gcc::Precompile::RenderExtraToolArgs(const JobAction &JA,
2368 ArgStringList &CmdArgs) const {
Daniel Dunbara3246a02009-03-18 08:07:30 +00002369 // The type is good enough.
Daniel Dunbar1a093d22009-03-18 06:00:36 +00002370}
2371
Daniel Dunbar4e295052010-01-25 22:35:08 +00002372void gcc::Compile::RenderExtraToolArgs(const JobAction &JA,
2373 ArgStringList &CmdArgs) const {
Daniel Dunbaraeea8ac2010-02-11 03:16:21 +00002374 const Driver &D = getToolChain().getDriver();
2375
Daniel Dunbar4e295052010-01-25 22:35:08 +00002376 // If -flto, etc. are present then make sure not to force assembly output.
Daniel Dunbar24e52992010-06-07 23:28:45 +00002377 if (JA.getType() == types::TY_LLVM_IR || JA.getType() == types::TY_LTO_IR ||
2378 JA.getType() == types::TY_LLVM_BC || JA.getType() == types::TY_LTO_BC)
Daniel Dunbar4e295052010-01-25 22:35:08 +00002379 CmdArgs.push_back("-c");
Daniel Dunbaraeea8ac2010-02-11 03:16:21 +00002380 else {
2381 if (JA.getType() != types::TY_PP_Asm)
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002382 D.Diag(diag::err_drv_invalid_gcc_output_type)
Daniel Dunbaraeea8ac2010-02-11 03:16:21 +00002383 << getTypeName(JA.getType());
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00002384
Daniel Dunbar4e295052010-01-25 22:35:08 +00002385 CmdArgs.push_back("-S");
Daniel Dunbaraeea8ac2010-02-11 03:16:21 +00002386 }
Daniel Dunbar1a093d22009-03-18 06:00:36 +00002387}
2388
Daniel Dunbar4e295052010-01-25 22:35:08 +00002389void gcc::Assemble::RenderExtraToolArgs(const JobAction &JA,
2390 ArgStringList &CmdArgs) const {
Daniel Dunbara3246a02009-03-18 08:07:30 +00002391 CmdArgs.push_back("-c");
Daniel Dunbar1a093d22009-03-18 06:00:36 +00002392}
Daniel Dunbara3246a02009-03-18 08:07:30 +00002393
Daniel Dunbar4e295052010-01-25 22:35:08 +00002394void gcc::Link::RenderExtraToolArgs(const JobAction &JA,
2395 ArgStringList &CmdArgs) const {
Daniel Dunbara3246a02009-03-18 08:07:30 +00002396 // The types are (hopefully) good enough.
2397}
2398
Daniel Dunbare6adeee2009-03-29 17:08:39 +00002399const char *darwin::CC1::getCC1Name(types::ID Type) const {
2400 switch (Type) {
2401 default:
2402 assert(0 && "Unexpected type for Darwin CC1 tool.");
2403 case types::TY_Asm:
2404 case types::TY_C: case types::TY_CHeader:
2405 case types::TY_PP_C: case types::TY_PP_CHeader:
2406 return "cc1";
2407 case types::TY_ObjC: case types::TY_ObjCHeader:
Nico Webered8080c2011-08-13 23:13:37 +00002408 case types::TY_PP_ObjC: case types::TY_PP_ObjC_Alias:
2409 case types::TY_PP_ObjCHeader:
Daniel Dunbare6adeee2009-03-29 17:08:39 +00002410 return "cc1obj";
2411 case types::TY_CXX: case types::TY_CXXHeader:
2412 case types::TY_PP_CXX: case types::TY_PP_CXXHeader:
2413 return "cc1plus";
2414 case types::TY_ObjCXX: case types::TY_ObjCXXHeader:
Nico Webered8080c2011-08-13 23:13:37 +00002415 case types::TY_PP_ObjCXX: case types::TY_PP_ObjCXX_Alias:
2416 case types::TY_PP_ObjCXXHeader:
Daniel Dunbare6adeee2009-03-29 17:08:39 +00002417 return "cc1objplus";
2418 }
2419}
2420
Daniel Dunbard067f7f2009-04-08 23:54:23 +00002421const char *darwin::CC1::getBaseInputName(const ArgList &Args,
Daniel Dunbar52e96cc2009-03-30 00:34:04 +00002422 const InputInfoList &Inputs) {
Michael J. Spencere1696752010-12-18 00:19:12 +00002423 return Args.MakeArgString(
2424 llvm::sys::path::filename(Inputs[0].getBaseInput()));
Daniel Dunbarafec1f52009-03-29 18:40:18 +00002425}
2426
Daniel Dunbard067f7f2009-04-08 23:54:23 +00002427const char *darwin::CC1::getBaseInputStem(const ArgList &Args,
Daniel Dunbar52e96cc2009-03-30 00:34:04 +00002428 const InputInfoList &Inputs) {
Daniel Dunbarafec1f52009-03-29 18:40:18 +00002429 const char *Str = getBaseInputName(Args, Inputs);
2430
Chris Lattner906bb902011-01-16 08:14:11 +00002431 if (const char *End = strrchr(Str, '.'))
Daniel Dunbar1c28f1e2009-09-09 22:32:48 +00002432 return Args.MakeArgString(std::string(Str, End));
Daniel Dunbarafec1f52009-03-29 18:40:18 +00002433
2434 return Str;
2435}
2436
2437const char *
Daniel Dunbard067f7f2009-04-08 23:54:23 +00002438darwin::CC1::getDependencyFileName(const ArgList &Args,
Daniel Dunbar52e96cc2009-03-30 00:34:04 +00002439 const InputInfoList &Inputs) {
Daniel Dunbarafec1f52009-03-29 18:40:18 +00002440 // FIXME: Think about this more.
2441 std::string Res;
2442
2443 if (Arg *OutputOpt = Args.getLastArg(options::OPT_o)) {
2444 std::string Str(OutputOpt->getValue(Args));
Daniel Dunbarafec1f52009-03-29 18:40:18 +00002445 Res = Str.substr(0, Str.rfind('.'));
Chad Rosier6fdf38b2011-08-17 23:08:45 +00002446 } else {
Daniel Dunbar52e96cc2009-03-30 00:34:04 +00002447 Res = darwin::CC1::getBaseInputStem(Args, Inputs);
Chad Rosier6fdf38b2011-08-17 23:08:45 +00002448 }
Daniel Dunbar1c28f1e2009-09-09 22:32:48 +00002449 return Args.MakeArgString(Res + ".d");
Daniel Dunbarafec1f52009-03-29 18:40:18 +00002450}
2451
Chad Rosierbc5ea3d2011-08-17 18:24:55 +00002452void darwin::CC1::RemoveCC1UnsupportedArgs(ArgStringList &CmdArgs) const {
Eric Christopher84fbdb42011-08-19 00:30:14 +00002453 for (ArgStringList::iterator it = CmdArgs.begin(), ie = CmdArgs.end();
Chad Rosierbc5ea3d2011-08-17 18:24:55 +00002454 it != ie;) {
Chad Rosier2f818792011-08-18 17:56:32 +00002455
2456 StringRef Option = *it;
Chad Rosierf29d9aa2011-08-26 18:30:43 +00002457 bool RemoveOption = false;
Chad Rosier30453862011-08-18 01:18:28 +00002458
Chad Rosierf29d9aa2011-08-26 18:30:43 +00002459 // Remove -faltivec
2460 if (Option.equals("-faltivec")) {
2461 it = CmdArgs.erase(it);
2462 ie = CmdArgs.end();
Chad Rosier30453862011-08-18 01:18:28 +00002463 continue;
2464 }
2465
Chad Rosierf29d9aa2011-08-26 18:30:43 +00002466 // Handle machine specific options.
2467 if (Option.startswith("-m")) {
2468 RemoveOption = llvm::StringSwitch<bool>(Option)
2469 .Case("-mthumb", true)
2470 .Case("-mno-thumb", true)
2471 .Case("-mno-fused-madd", true)
2472 .Case("-mlong-branch", true)
2473 .Case("-mlongcall", true)
2474 .Case("-mcpu=G4", true)
2475 .Case("-mcpu=G5", true)
2476 .Default(false);
2477 }
2478
2479 // Handle warning options.
2480 if (Option.startswith("-W")) {
2481 // Remove -W/-Wno- to reduce the number of cases.
2482 if (Option.startswith("-Wno-"))
2483 Option = Option.substr(5);
2484 else
2485 Option = Option.substr(2);
2486
2487 RemoveOption = llvm::StringSwitch<bool>(Option)
2488 .Case("address-of-temporary", true)
2489 .Case("ambiguous-member-template", true)
2490 .Case("analyzer-incompatible-plugin", true)
2491 .Case("array-bounds", true)
2492 .Case("array-bounds-pointer-arithmetic", true)
2493 .Case("bind-to-temporary-copy", true)
2494 .Case("bitwise-op-parentheses", true)
2495 .Case("bool-conversions", true)
2496 .Case("builtin-macro-redefined", true)
2497 .Case("c++-hex-floats", true)
2498 .Case("c++0x-compat", true)
2499 .Case("c++0x-extensions", true)
2500 .Case("c++0x-narrowing", true)
2501 .Case("c++0x-static-nonintegral-init", true)
2502 .Case("conditional-uninitialized", true)
2503 .Case("constant-conversion", true)
2504 .Case("CFString-literal", true)
2505 .Case("constant-logical-operand", true)
2506 .Case("custom-atomic-properties", true)
2507 .Case("default-arg-special-member", true)
2508 .Case("delegating-ctor-cycles", true)
2509 .Case("delete-non-virtual-dtor", true)
2510 .Case("deprecated-implementations", true)
2511 .Case("deprecated-writable-strings", true)
2512 .Case("distributed-object-modifiers", true)
2513 .Case("duplicate-method-arg", true)
2514 .Case("dynamic-class-memaccess", true)
2515 .Case("enum-compare", true)
2516 .Case("exit-time-destructors", true)
2517 .Case("gnu", true)
2518 .Case("gnu-designator", true)
2519 .Case("header-hygiene", true)
2520 .Case("idiomatic-parentheses", true)
2521 .Case("ignored-qualifiers", true)
2522 .Case("implicit-atomic-properties", true)
2523 .Case("incompatible-pointer-types", true)
2524 .Case("incomplete-implementation", true)
2525 .Case("initializer-overrides", true)
2526 .Case("invalid-noreturn", true)
2527 .Case("invalid-token-paste", true)
2528 .Case("literal-conversion", true)
2529 .Case("literal-range", true)
2530 .Case("local-type-template-args", true)
2531 .Case("logical-op-parentheses", true)
2532 .Case("method-signatures", true)
2533 .Case("microsoft", true)
2534 .Case("mismatched-tags", true)
2535 .Case("missing-method-return-type", true)
2536 .Case("non-pod-varargs", true)
2537 .Case("nonfragile-abi2", true)
2538 .Case("null-arithmetic", true)
2539 .Case("null-dereference", true)
2540 .Case("out-of-line-declaration", true)
2541 .Case("overriding-method-mismatch", true)
2542 .Case("readonly-setter-attrs", true)
2543 .Case("return-stack-address", true)
2544 .Case("self-assign", true)
2545 .Case("semicolon-before-method-body", true)
2546 .Case("sentinel", true)
2547 .Case("shift-overflow", true)
2548 .Case("shift-sign-overflow", true)
2549 .Case("sign-conversion", true)
2550 .Case("sizeof-array-argument", true)
2551 .Case("sizeof-pointer-memaccess", true)
2552 .Case("string-compare", true)
2553 .Case("super-class-method-mismatch", true)
2554 .Case("tautological-compare", true)
2555 .Case("typedef-redefinition", true)
2556 .Case("typename-missing", true)
2557 .Case("undefined-reinterpret-cast", true)
2558 .Case("unknown-warning-option", true)
2559 .Case("unnamed-type-template-args", true)
2560 .Case("unneeded-internal-declaration", true)
2561 .Case("unneeded-member-function", true)
2562 .Case("unused-comparison", true)
2563 .Case("unused-exception-parameter", true)
2564 .Case("unused-member-function", true)
2565 .Case("unused-result", true)
2566 .Case("vector-conversions", true)
2567 .Case("vla", true)
2568 .Case("used-but-marked-unused", true)
2569 .Case("weak-vtables", true)
2570 .Default(false);
2571 } // if (Option.startswith("-W"))
Chad Rosier30453862011-08-18 01:18:28 +00002572 if (RemoveOption) {
Chad Rosierbc5ea3d2011-08-17 18:24:55 +00002573 it = CmdArgs.erase(it);
Chad Rosier23594f62011-08-17 18:51:56 +00002574 ie = CmdArgs.end();
Chad Rosier6fdf38b2011-08-17 23:08:45 +00002575 } else {
Chad Rosierbc5ea3d2011-08-17 18:24:55 +00002576 ++it;
Chad Rosier6fdf38b2011-08-17 23:08:45 +00002577 }
Chad Rosierbc5ea3d2011-08-17 18:24:55 +00002578 }
2579}
2580
Daniel Dunbard067f7f2009-04-08 23:54:23 +00002581void darwin::CC1::AddCC1Args(const ArgList &Args,
Daniel Dunbarafec1f52009-03-29 18:40:18 +00002582 ArgStringList &CmdArgs) const {
Daniel Dunbar083edf72009-12-21 18:54:17 +00002583 const Driver &D = getToolChain().getDriver();
Daniel Dunbar4eadb602009-09-10 01:21:12 +00002584
2585 CheckCodeGenerationOptions(D, Args);
2586
Daniel Dunbarafec1f52009-03-29 18:40:18 +00002587 // Derived from cc1 spec.
Daniel Dunbarafec1f52009-03-29 18:40:18 +00002588 if (!Args.hasArg(options::OPT_mkernel) && !Args.hasArg(options::OPT_static) &&
2589 !Args.hasArg(options::OPT_mdynamic_no_pic))
2590 CmdArgs.push_back("-fPIC");
2591
Daniel Dunbar2ffe0292009-09-10 03:37:02 +00002592 if (getToolChain().getTriple().getArch() == llvm::Triple::arm ||
2593 getToolChain().getTriple().getArch() == llvm::Triple::thumb) {
2594 if (!Args.hasArg(options::OPT_fbuiltin_strcat))
2595 CmdArgs.push_back("-fno-builtin-strcat");
2596 if (!Args.hasArg(options::OPT_fbuiltin_strcpy))
2597 CmdArgs.push_back("-fno-builtin-strcpy");
2598 }
2599
Daniel Dunbarafec1f52009-03-29 18:40:18 +00002600 if (Args.hasArg(options::OPT_g_Flag) &&
2601 !Args.hasArg(options::OPT_fno_eliminate_unused_debug_symbols))
2602 CmdArgs.push_back("-feliminate-unused-debug-symbols");
2603}
2604
2605void darwin::CC1::AddCC1OptionsArgs(const ArgList &Args, ArgStringList &CmdArgs,
2606 const InputInfoList &Inputs,
2607 const ArgStringList &OutputArgs) const {
Daniel Dunbar083edf72009-12-21 18:54:17 +00002608 const Driver &D = getToolChain().getDriver();
Daniel Dunbarafec1f52009-03-29 18:40:18 +00002609
2610 // Derived from cc1_options spec.
2611 if (Args.hasArg(options::OPT_fast) ||
2612 Args.hasArg(options::OPT_fastf) ||
2613 Args.hasArg(options::OPT_fastcp))
2614 CmdArgs.push_back("-O3");
Daniel Dunbard067f7f2009-04-08 23:54:23 +00002615
Daniel Dunbarafec1f52009-03-29 18:40:18 +00002616 if (Arg *A = Args.getLastArg(options::OPT_pg))
2617 if (Args.hasArg(options::OPT_fomit_frame_pointer))
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002618 D.Diag(diag::err_drv_argument_not_allowed_with)
Daniel Dunbarafec1f52009-03-29 18:40:18 +00002619 << A->getAsString(Args) << "-fomit-frame-pointer";
2620
2621 AddCC1Args(Args, CmdArgs);
2622
2623 if (!Args.hasArg(options::OPT_Q))
2624 CmdArgs.push_back("-quiet");
2625
2626 CmdArgs.push_back("-dumpbase");
Daniel Dunbar52e96cc2009-03-30 00:34:04 +00002627 CmdArgs.push_back(darwin::CC1::getBaseInputName(Args, Inputs));
Daniel Dunbarafec1f52009-03-29 18:40:18 +00002628
2629 Args.AddAllArgs(CmdArgs, options::OPT_d_Group);
2630
2631 Args.AddAllArgs(CmdArgs, options::OPT_m_Group);
2632 Args.AddAllArgs(CmdArgs, options::OPT_a_Group);
2633
2634 // FIXME: The goal is to use the user provided -o if that is our
2635 // final output, otherwise to drive from the original input
2636 // name. Find a clean way to go about this.
2637 if ((Args.hasArg(options::OPT_c) || Args.hasArg(options::OPT_S)) &&
2638 Args.hasArg(options::OPT_o)) {
2639 Arg *OutputOpt = Args.getLastArg(options::OPT_o);
2640 CmdArgs.push_back("-auxbase-strip");
2641 CmdArgs.push_back(OutputOpt->getValue(Args));
2642 } else {
2643 CmdArgs.push_back("-auxbase");
Daniel Dunbar52e96cc2009-03-30 00:34:04 +00002644 CmdArgs.push_back(darwin::CC1::getBaseInputStem(Args, Inputs));
Daniel Dunbarafec1f52009-03-29 18:40:18 +00002645 }
2646
2647 Args.AddAllArgs(CmdArgs, options::OPT_g_Group);
2648
2649 Args.AddAllArgs(CmdArgs, options::OPT_O);
2650 // FIXME: -Wall is getting some special treatment. Investigate.
2651 Args.AddAllArgs(CmdArgs, options::OPT_W_Group, options::OPT_pedantic_Group);
2652 Args.AddLastArg(CmdArgs, options::OPT_w);
Daniel Dunbard067f7f2009-04-08 23:54:23 +00002653 Args.AddAllArgs(CmdArgs, options::OPT_std_EQ, options::OPT_ansi,
Daniel Dunbarafec1f52009-03-29 18:40:18 +00002654 options::OPT_trigraphs);
Daniel Dunbar72a60902009-04-26 01:10:38 +00002655 if (!Args.getLastArg(options::OPT_std_EQ, options::OPT_ansi)) {
2656 // Honor -std-default.
2657 Args.AddAllArgsTranslated(CmdArgs, options::OPT_std_default_EQ,
2658 "-std=", /*Joined=*/true);
2659 }
2660
Daniel Dunbarafec1f52009-03-29 18:40:18 +00002661 if (Args.hasArg(options::OPT_v))
2662 CmdArgs.push_back("-version");
Daniel Dunbar733b0f82011-03-01 18:49:30 +00002663 if (Args.hasArg(options::OPT_pg) &&
2664 getToolChain().SupportsProfiling())
Daniel Dunbarafec1f52009-03-29 18:40:18 +00002665 CmdArgs.push_back("-p");
2666 Args.AddLastArg(CmdArgs, options::OPT_p);
Daniel Dunbard067f7f2009-04-08 23:54:23 +00002667
Daniel Dunbarafec1f52009-03-29 18:40:18 +00002668 // The driver treats -fsyntax-only specially.
Daniel Dunbar2ffe0292009-09-10 03:37:02 +00002669 if (getToolChain().getTriple().getArch() == llvm::Triple::arm ||
2670 getToolChain().getTriple().getArch() == llvm::Triple::thumb) {
2671 // Removes -fbuiltin-str{cat,cpy}; these aren't recognized by cc1 but are
2672 // used to inhibit the default -fno-builtin-str{cat,cpy}.
2673 //
2674 // FIXME: Should we grow a better way to deal with "removing" args?
Daniel Dunbar44b36ee2009-11-25 11:53:23 +00002675 for (arg_iterator it = Args.filtered_begin(options::OPT_f_Group,
2676 options::OPT_fsyntax_only),
2677 ie = Args.filtered_end(); it != ie; ++it) {
Daniel Dunbara442fd52010-06-11 22:00:13 +00002678 if (!(*it)->getOption().matches(options::OPT_fbuiltin_strcat) &&
2679 !(*it)->getOption().matches(options::OPT_fbuiltin_strcpy)) {
2680 (*it)->claim();
2681 (*it)->render(Args, CmdArgs);
Daniel Dunbar2ffe0292009-09-10 03:37:02 +00002682 }
2683 }
2684 } else
2685 Args.AddAllArgs(CmdArgs, options::OPT_f_Group, options::OPT_fsyntax_only);
Daniel Dunbard067f7f2009-04-08 23:54:23 +00002686
Daniel Dunbarf28c2ff2011-04-07 20:41:03 +00002687 // Claim Clang only -f options, they aren't worth warning about.
2688 Args.ClaimAllArgs(options::OPT_f_clang_Group);
2689
Daniel Dunbarafec1f52009-03-29 18:40:18 +00002690 Args.AddAllArgs(CmdArgs, options::OPT_undef);
2691 if (Args.hasArg(options::OPT_Qn))
2692 CmdArgs.push_back("-fno-ident");
Daniel Dunbard067f7f2009-04-08 23:54:23 +00002693
Daniel Dunbarafec1f52009-03-29 18:40:18 +00002694 // FIXME: This isn't correct.
2695 //Args.AddLastArg(CmdArgs, options::OPT__help)
2696 //Args.AddLastArg(CmdArgs, options::OPT__targetHelp)
2697
2698 CmdArgs.append(OutputArgs.begin(), OutputArgs.end());
2699
2700 // FIXME: Still don't get what is happening here. Investigate.
2701 Args.AddAllArgs(CmdArgs, options::OPT__param);
2702
2703 if (Args.hasArg(options::OPT_fmudflap) ||
2704 Args.hasArg(options::OPT_fmudflapth)) {
2705 CmdArgs.push_back("-fno-builtin");
2706 CmdArgs.push_back("-fno-merge-constants");
2707 }
Daniel Dunbard067f7f2009-04-08 23:54:23 +00002708
Daniel Dunbarafec1f52009-03-29 18:40:18 +00002709 if (Args.hasArg(options::OPT_coverage)) {
2710 CmdArgs.push_back("-fprofile-arcs");
2711 CmdArgs.push_back("-ftest-coverage");
2712 }
2713
2714 if (types::isCXX(Inputs[0].getType()))
2715 CmdArgs.push_back("-D__private_extern__=extern");
2716}
2717
2718void darwin::CC1::AddCPPOptionsArgs(const ArgList &Args, ArgStringList &CmdArgs,
2719 const InputInfoList &Inputs,
2720 const ArgStringList &OutputArgs) const {
2721 // Derived from cpp_options
2722 AddCPPUniqueOptionsArgs(Args, CmdArgs, Inputs);
Daniel Dunbard067f7f2009-04-08 23:54:23 +00002723
Daniel Dunbarafec1f52009-03-29 18:40:18 +00002724 CmdArgs.append(OutputArgs.begin(), OutputArgs.end());
2725
2726 AddCC1Args(Args, CmdArgs);
2727
2728 // NOTE: The code below has some commonality with cpp_options, but
2729 // in classic gcc style ends up sending things in different
2730 // orders. This may be a good merge candidate once we drop pedantic
2731 // compatibility.
2732
2733 Args.AddAllArgs(CmdArgs, options::OPT_m_Group);
Daniel Dunbard067f7f2009-04-08 23:54:23 +00002734 Args.AddAllArgs(CmdArgs, options::OPT_std_EQ, options::OPT_ansi,
Daniel Dunbarafec1f52009-03-29 18:40:18 +00002735 options::OPT_trigraphs);
Daniel Dunbar72a60902009-04-26 01:10:38 +00002736 if (!Args.getLastArg(options::OPT_std_EQ, options::OPT_ansi)) {
2737 // Honor -std-default.
2738 Args.AddAllArgsTranslated(CmdArgs, options::OPT_std_default_EQ,
2739 "-std=", /*Joined=*/true);
2740 }
Daniel Dunbarafec1f52009-03-29 18:40:18 +00002741 Args.AddAllArgs(CmdArgs, options::OPT_W_Group, options::OPT_pedantic_Group);
2742 Args.AddLastArg(CmdArgs, options::OPT_w);
Daniel Dunbard067f7f2009-04-08 23:54:23 +00002743
Daniel Dunbarafec1f52009-03-29 18:40:18 +00002744 // The driver treats -fsyntax-only specially.
2745 Args.AddAllArgs(CmdArgs, options::OPT_f_Group, options::OPT_fsyntax_only);
2746
Daniel Dunbarf28c2ff2011-04-07 20:41:03 +00002747 // Claim Clang only -f options, they aren't worth warning about.
2748 Args.ClaimAllArgs(options::OPT_f_clang_Group);
2749
Daniel Dunbarafec1f52009-03-29 18:40:18 +00002750 if (Args.hasArg(options::OPT_g_Group) && !Args.hasArg(options::OPT_g0) &&
2751 !Args.hasArg(options::OPT_fno_working_directory))
2752 CmdArgs.push_back("-fworking-directory");
2753
2754 Args.AddAllArgs(CmdArgs, options::OPT_O);
2755 Args.AddAllArgs(CmdArgs, options::OPT_undef);
2756 if (Args.hasArg(options::OPT_save_temps))
2757 CmdArgs.push_back("-fpch-preprocess");
2758}
2759
Daniel Dunbard067f7f2009-04-08 23:54:23 +00002760void darwin::CC1::AddCPPUniqueOptionsArgs(const ArgList &Args,
Daniel Dunbarafec1f52009-03-29 18:40:18 +00002761 ArgStringList &CmdArgs,
Mike Stump11289f42009-09-09 15:08:12 +00002762 const InputInfoList &Inputs) const {
Daniel Dunbar083edf72009-12-21 18:54:17 +00002763 const Driver &D = getToolChain().getDriver();
Daniel Dunbarafec1f52009-03-29 18:40:18 +00002764
Daniel Dunbar64198ef2009-09-10 01:21:05 +00002765 CheckPreprocessingOptions(D, Args);
2766
Daniel Dunbarafec1f52009-03-29 18:40:18 +00002767 // Derived from cpp_unique_options.
Daniel Dunbar64198ef2009-09-10 01:21:05 +00002768 // -{C,CC} only with -E is checked in CheckPreprocessingOptions().
2769 Args.AddLastArg(CmdArgs, options::OPT_C);
2770 Args.AddLastArg(CmdArgs, options::OPT_CC);
Daniel Dunbarafec1f52009-03-29 18:40:18 +00002771 if (!Args.hasArg(options::OPT_Q))
2772 CmdArgs.push_back("-quiet");
2773 Args.AddAllArgs(CmdArgs, options::OPT_nostdinc);
Douglas Gregor64b046f2010-03-24 20:13:48 +00002774 Args.AddAllArgs(CmdArgs, options::OPT_nostdincxx);
Daniel Dunbarafec1f52009-03-29 18:40:18 +00002775 Args.AddLastArg(CmdArgs, options::OPT_v);
2776 Args.AddAllArgs(CmdArgs, options::OPT_I_Group, options::OPT_F);
2777 Args.AddLastArg(CmdArgs, options::OPT_P);
2778
2779 // FIXME: Handle %I properly.
2780 if (getToolChain().getArchName() == "x86_64") {
2781 CmdArgs.push_back("-imultilib");
2782 CmdArgs.push_back("x86_64");
2783 }
2784
2785 if (Args.hasArg(options::OPT_MD)) {
2786 CmdArgs.push_back("-MD");
Daniel Dunbar52e96cc2009-03-30 00:34:04 +00002787 CmdArgs.push_back(darwin::CC1::getDependencyFileName(Args, Inputs));
Daniel Dunbarafec1f52009-03-29 18:40:18 +00002788 }
2789
2790 if (Args.hasArg(options::OPT_MMD)) {
2791 CmdArgs.push_back("-MMD");
Daniel Dunbar52e96cc2009-03-30 00:34:04 +00002792 CmdArgs.push_back(darwin::CC1::getDependencyFileName(Args, Inputs));
Daniel Dunbarafec1f52009-03-29 18:40:18 +00002793 }
2794
2795 Args.AddLastArg(CmdArgs, options::OPT_M);
2796 Args.AddLastArg(CmdArgs, options::OPT_MM);
2797 Args.AddAllArgs(CmdArgs, options::OPT_MF);
2798 Args.AddLastArg(CmdArgs, options::OPT_MG);
2799 Args.AddLastArg(CmdArgs, options::OPT_MP);
2800 Args.AddAllArgs(CmdArgs, options::OPT_MQ);
2801 Args.AddAllArgs(CmdArgs, options::OPT_MT);
2802 if (!Args.hasArg(options::OPT_M) && !Args.hasArg(options::OPT_MM) &&
2803 (Args.hasArg(options::OPT_MD) || Args.hasArg(options::OPT_MMD))) {
2804 if (Arg *OutputOpt = Args.getLastArg(options::OPT_o)) {
2805 CmdArgs.push_back("-MQ");
2806 CmdArgs.push_back(OutputOpt->getValue(Args));
2807 }
2808 }
2809
2810 Args.AddLastArg(CmdArgs, options::OPT_remap);
2811 if (Args.hasArg(options::OPT_g3))
2812 CmdArgs.push_back("-dD");
2813 Args.AddLastArg(CmdArgs, options::OPT_H);
2814
2815 AddCPPArgs(Args, CmdArgs);
2816
2817 Args.AddAllArgs(CmdArgs, options::OPT_D, options::OPT_U, options::OPT_A);
2818 Args.AddAllArgs(CmdArgs, options::OPT_i_Group);
2819
2820 for (InputInfoList::const_iterator
2821 it = Inputs.begin(), ie = Inputs.end(); it != ie; ++it) {
2822 const InputInfo &II = *it;
Daniel Dunbard067f7f2009-04-08 23:54:23 +00002823
Daniel Dunbarb440f562010-08-02 02:38:21 +00002824 CmdArgs.push_back(II.getFilename());
Daniel Dunbarafec1f52009-03-29 18:40:18 +00002825 }
2826
2827 Args.AddAllArgValues(CmdArgs, options::OPT_Wp_COMMA,
2828 options::OPT_Xpreprocessor);
2829
2830 if (Args.hasArg(options::OPT_fmudflap)) {
2831 CmdArgs.push_back("-D_MUDFLAP");
2832 CmdArgs.push_back("-include");
2833 CmdArgs.push_back("mf-runtime.h");
2834 }
2835
2836 if (Args.hasArg(options::OPT_fmudflapth)) {
2837 CmdArgs.push_back("-D_MUDFLAP");
2838 CmdArgs.push_back("-D_MUDFLAPTH");
2839 CmdArgs.push_back("-include");
2840 CmdArgs.push_back("mf-runtime.h");
2841 }
2842}
2843
Daniel Dunbard067f7f2009-04-08 23:54:23 +00002844void darwin::CC1::AddCPPArgs(const ArgList &Args,
Daniel Dunbarafec1f52009-03-29 18:40:18 +00002845 ArgStringList &CmdArgs) const {
2846 // Derived from cpp spec.
2847
2848 if (Args.hasArg(options::OPT_static)) {
2849 // The gcc spec is broken here, it refers to dynamic but
2850 // that has been translated. Start by being bug compatible.
Daniel Dunbard067f7f2009-04-08 23:54:23 +00002851
Daniel Dunbarafec1f52009-03-29 18:40:18 +00002852 // if (!Args.hasArg(arglist.parser.dynamicOption))
2853 CmdArgs.push_back("-D__STATIC__");
2854 } else
2855 CmdArgs.push_back("-D__DYNAMIC__");
2856
2857 if (Args.hasArg(options::OPT_pthread))
2858 CmdArgs.push_back("-D_REENTRANT");
2859}
2860
Daniel Dunbare6adeee2009-03-29 17:08:39 +00002861void darwin::Preprocess::ConstructJob(Compilation &C, const JobAction &JA,
Daniel Dunbar7fbaf532010-08-02 02:38:28 +00002862 const InputInfo &Output,
Daniel Dunbard067f7f2009-04-08 23:54:23 +00002863 const InputInfoList &Inputs,
2864 const ArgList &Args,
Daniel Dunbare6adeee2009-03-29 17:08:39 +00002865 const char *LinkingOutput) const {
2866 ArgStringList CmdArgs;
2867
2868 assert(Inputs.size() == 1 && "Unexpected number of inputs!");
2869
2870 CmdArgs.push_back("-E");
2871
2872 if (Args.hasArg(options::OPT_traditional) ||
Daniel Dunbare6adeee2009-03-29 17:08:39 +00002873 Args.hasArg(options::OPT_traditional_cpp))
2874 CmdArgs.push_back("-traditional-cpp");
Daniel Dunbard067f7f2009-04-08 23:54:23 +00002875
Daniel Dunbare6adeee2009-03-29 17:08:39 +00002876 ArgStringList OutputArgs;
Daniel Dunbarb440f562010-08-02 02:38:21 +00002877 assert(Output.isFilename() && "Unexpected CC1 output.");
2878 OutputArgs.push_back("-o");
2879 OutputArgs.push_back(Output.getFilename());
Daniel Dunbare6adeee2009-03-29 17:08:39 +00002880
Joerg Sonnenbergerb86f5f42011-03-06 23:31:01 +00002881 if (Args.hasArg(options::OPT_E) || getToolChain().getDriver().CCCIsCPP) {
Daniel Dunbarf64f5302009-03-29 22:27:40 +00002882 AddCPPOptionsArgs(Args, CmdArgs, Inputs, OutputArgs);
2883 } else {
2884 AddCPPOptionsArgs(Args, CmdArgs, Inputs, ArgStringList());
2885 CmdArgs.append(OutputArgs.begin(), OutputArgs.end());
2886 }
Daniel Dunbard067f7f2009-04-08 23:54:23 +00002887
Daniel Dunbar6a8803a2009-04-03 01:27:06 +00002888 Args.AddAllArgs(CmdArgs, options::OPT_d_Group);
2889
Daniel Dunbare6adeee2009-03-29 17:08:39 +00002890 const char *CC1Name = getCC1Name(Inputs[0].getType());
Daniel Dunbard067f7f2009-04-08 23:54:23 +00002891 const char *Exec =
Daniel Dunbar9c3ed5f2010-07-14 18:46:23 +00002892 Args.MakeArgString(getToolChain().GetProgramPath(CC1Name));
Daniel Dunbar7fbaf532010-08-02 02:38:28 +00002893 C.addCommand(new Command(JA, *this, Exec, CmdArgs));
Daniel Dunbare6adeee2009-03-29 17:08:39 +00002894}
2895
2896void darwin::Compile::ConstructJob(Compilation &C, const JobAction &JA,
Daniel Dunbar7fbaf532010-08-02 02:38:28 +00002897 const InputInfo &Output,
Daniel Dunbard067f7f2009-04-08 23:54:23 +00002898 const InputInfoList &Inputs,
2899 const ArgList &Args,
Daniel Dunbare6adeee2009-03-29 17:08:39 +00002900 const char *LinkingOutput) const {
Daniel Dunbar083edf72009-12-21 18:54:17 +00002901 const Driver &D = getToolChain().getDriver();
Daniel Dunbare6adeee2009-03-29 17:08:39 +00002902 ArgStringList CmdArgs;
2903
2904 assert(Inputs.size() == 1 && "Unexpected number of inputs!");
2905
2906 types::ID InputType = Inputs[0].getType();
2907 const Arg *A;
Eli Friedmanbb0d9a52009-07-14 21:58:17 +00002908 if ((A = Args.getLastArg(options::OPT_traditional)))
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002909 D.Diag(diag::err_drv_argument_only_allowed_with)
Daniel Dunbare6adeee2009-03-29 17:08:39 +00002910 << A->getAsString(Args) << "-E";
2911
Daniel Dunbar24e52992010-06-07 23:28:45 +00002912 if (JA.getType() == types::TY_LLVM_IR ||
2913 JA.getType() == types::TY_LTO_IR)
Daniel Dunbare6adeee2009-03-29 17:08:39 +00002914 CmdArgs.push_back("-emit-llvm");
Daniel Dunbar24e52992010-06-07 23:28:45 +00002915 else if (JA.getType() == types::TY_LLVM_BC ||
2916 JA.getType() == types::TY_LTO_BC)
Daniel Dunbare6adeee2009-03-29 17:08:39 +00002917 CmdArgs.push_back("-emit-llvm-bc");
Daniel Dunbar6cdf83c2009-09-01 16:57:46 +00002918 else if (Output.getType() == types::TY_AST)
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002919 D.Diag(diag::err_drv_no_ast_support)
Daniel Dunbar1c28f1e2009-09-09 22:32:48 +00002920 << getToolChain().getTripleString();
Daniel Dunbarbcd554f2010-02-11 17:33:45 +00002921 else if (JA.getType() != types::TY_PP_Asm &&
2922 JA.getType() != types::TY_PCH)
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002923 D.Diag(diag::err_drv_invalid_gcc_output_type)
Daniel Dunbaraeea8ac2010-02-11 03:16:21 +00002924 << getTypeName(JA.getType());
Daniel Dunbare6adeee2009-03-29 17:08:39 +00002925
2926 ArgStringList OutputArgs;
2927 if (Output.getType() != types::TY_PCH) {
2928 OutputArgs.push_back("-o");
Daniel Dunbarb440f562010-08-02 02:38:21 +00002929 if (Output.isNothing())
Daniel Dunbare6adeee2009-03-29 17:08:39 +00002930 OutputArgs.push_back("/dev/null");
2931 else
2932 OutputArgs.push_back(Output.getFilename());
2933 }
2934
2935 // There is no need for this level of compatibility, but it makes
2936 // diffing easier.
2937 bool OutputArgsEarly = (Args.hasArg(options::OPT_fsyntax_only) ||
2938 Args.hasArg(options::OPT_S));
2939
2940 if (types::getPreprocessedType(InputType) != types::TY_INVALID) {
Daniel Dunbarafec1f52009-03-29 18:40:18 +00002941 AddCPPUniqueOptionsArgs(Args, CmdArgs, Inputs);
Daniel Dunbare6adeee2009-03-29 17:08:39 +00002942 if (OutputArgsEarly) {
2943 AddCC1OptionsArgs(Args, CmdArgs, Inputs, OutputArgs);
2944 } else {
2945 AddCC1OptionsArgs(Args, CmdArgs, Inputs, ArgStringList());
2946 CmdArgs.append(OutputArgs.begin(), OutputArgs.end());
2947 }
2948 } else {
2949 CmdArgs.push_back("-fpreprocessed");
Daniel Dunbard067f7f2009-04-08 23:54:23 +00002950
Daniel Dunbare6adeee2009-03-29 17:08:39 +00002951 for (InputInfoList::const_iterator
2952 it = Inputs.begin(), ie = Inputs.end(); it != ie; ++it) {
2953 const InputInfo &II = *it;
2954
Daniel Dunbar6cdf83c2009-09-01 16:57:46 +00002955 // Reject AST inputs.
2956 if (II.getType() == types::TY_AST) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002957 D.Diag(diag::err_drv_no_ast_support)
Daniel Dunbar1c28f1e2009-09-09 22:32:48 +00002958 << getToolChain().getTripleString();
Daniel Dunbar6cdf83c2009-09-01 16:57:46 +00002959 return;
2960 }
2961
Daniel Dunbarb440f562010-08-02 02:38:21 +00002962 CmdArgs.push_back(II.getFilename());
Daniel Dunbare6adeee2009-03-29 17:08:39 +00002963 }
2964
2965 if (OutputArgsEarly) {
2966 AddCC1OptionsArgs(Args, CmdArgs, Inputs, OutputArgs);
2967 } else {
2968 AddCC1OptionsArgs(Args, CmdArgs, Inputs, ArgStringList());
2969 CmdArgs.append(OutputArgs.begin(), OutputArgs.end());
2970 }
2971 }
Daniel Dunbard067f7f2009-04-08 23:54:23 +00002972
Daniel Dunbare6adeee2009-03-29 17:08:39 +00002973 if (Output.getType() == types::TY_PCH) {
2974 assert(Output.isFilename() && "Invalid PCH output.");
2975
2976 CmdArgs.push_back("-o");
2977 // NOTE: gcc uses a temp .s file for this, but there doesn't seem
2978 // to be a good reason.
Chad Rosier96d690c2011-08-01 19:58:48 +00002979 const char *TmpPath = C.getArgs().MakeArgString(
Chad Rosier39ab7432011-08-26 21:28:44 +00002980 D.GetTemporaryPath("cc", "s"));
Chad Rosier96d690c2011-08-01 19:58:48 +00002981 C.addTempFile(TmpPath);
2982 CmdArgs.push_back(TmpPath);
Daniel Dunbard067f7f2009-04-08 23:54:23 +00002983
Eric Christopher84fbdb42011-08-19 00:30:14 +00002984 // If we're emitting a pch file with the last 4 characters of ".pth"
2985 // and falling back to llvm-gcc we want to use ".gch" instead.
2986 std::string OutputFile(Output.getFilename());
2987 size_t loc = OutputFile.rfind(".pth");
2988 if (loc != std::string::npos)
2989 OutputFile.replace(loc, 4, ".gch");
2990 const char *Tmp = C.getArgs().MakeArgString("--output-pch="+OutputFile);
2991 CmdArgs.push_back(Tmp);
Daniel Dunbard067f7f2009-04-08 23:54:23 +00002992 }
Daniel Dunbare6adeee2009-03-29 17:08:39 +00002993
Chad Rosierbc5ea3d2011-08-17 18:24:55 +00002994 RemoveCC1UnsupportedArgs(CmdArgs);
2995
Daniel Dunbare6adeee2009-03-29 17:08:39 +00002996 const char *CC1Name = getCC1Name(Inputs[0].getType());
Daniel Dunbard067f7f2009-04-08 23:54:23 +00002997 const char *Exec =
Daniel Dunbar9c3ed5f2010-07-14 18:46:23 +00002998 Args.MakeArgString(getToolChain().GetProgramPath(CC1Name));
Daniel Dunbar7fbaf532010-08-02 02:38:28 +00002999 C.addCommand(new Command(JA, *this, Exec, CmdArgs));
Daniel Dunbare6adeee2009-03-29 17:08:39 +00003000}
3001
Daniel Dunbarbe220842009-03-20 16:06:39 +00003002void darwin::Assemble::ConstructJob(Compilation &C, const JobAction &JA,
Daniel Dunbar7fbaf532010-08-02 02:38:28 +00003003 const InputInfo &Output,
Daniel Dunbard067f7f2009-04-08 23:54:23 +00003004 const InputInfoList &Inputs,
3005 const ArgList &Args,
Daniel Dunbarbe220842009-03-20 16:06:39 +00003006 const char *LinkingOutput) const {
3007 ArgStringList CmdArgs;
3008
3009 assert(Inputs.size() == 1 && "Unexpected number of inputs.");
3010 const InputInfo &Input = Inputs[0];
3011
Daniel Dunbardc8355e2011-04-12 23:59:20 +00003012 // Determine the original source input.
3013 const Action *SourceAction = &JA;
3014 while (SourceAction->getKind() != Action::InputClass) {
3015 assert(!SourceAction->getInputs().empty() && "unexpected root action!");
3016 SourceAction = SourceAction->getInputs()[0];
3017 }
3018
3019 // Forward -g, assuming we are dealing with an actual assembly file.
Eric Christopher84fbdb42011-08-19 00:30:14 +00003020 if (SourceAction->getType() == types::TY_Asm ||
Daniel Dunbardc8355e2011-04-12 23:59:20 +00003021 SourceAction->getType() == types::TY_PP_Asm) {
Daniel Dunbar5c9c1182009-04-01 00:27:44 +00003022 if (Args.hasArg(options::OPT_gstabs))
3023 CmdArgs.push_back("--gstabs");
3024 else if (Args.hasArg(options::OPT_g_Group))
3025 CmdArgs.push_back("--gdwarf2");
3026 }
Daniel Dunbard067f7f2009-04-08 23:54:23 +00003027
Daniel Dunbarbe220842009-03-20 16:06:39 +00003028 // Derived from asm spec.
Daniel Dunbar3571dd92009-09-09 18:36:27 +00003029 AddDarwinArch(Args, CmdArgs);
Daniel Dunbarbe220842009-03-20 16:06:39 +00003030
Daniel Dunbar6d484762010-07-22 01:47:22 +00003031 // Use -force_cpusubtype_ALL on x86 by default.
3032 if (getToolChain().getTriple().getArch() == llvm::Triple::x86 ||
3033 getToolChain().getTriple().getArch() == llvm::Triple::x86_64 ||
Daniel Dunbar3571dd92009-09-09 18:36:27 +00003034 Args.hasArg(options::OPT_force__cpusubtype__ALL))
3035 CmdArgs.push_back("-force_cpusubtype_ALL");
3036
Daniel Dunbara6b4a3d2009-08-24 22:26:16 +00003037 if (getToolChain().getTriple().getArch() != llvm::Triple::x86_64 &&
3038 (Args.hasArg(options::OPT_mkernel) ||
Daniel Dunbarbe220842009-03-20 16:06:39 +00003039 Args.hasArg(options::OPT_static) ||
Daniel Dunbara6b4a3d2009-08-24 22:26:16 +00003040 Args.hasArg(options::OPT_fapple_kext)))
Daniel Dunbard067f7f2009-04-08 23:54:23 +00003041 CmdArgs.push_back("-static");
3042
Daniel Dunbarbe220842009-03-20 16:06:39 +00003043 Args.AddAllArgValues(CmdArgs, options::OPT_Wa_COMMA,
3044 options::OPT_Xassembler);
3045
3046 assert(Output.isFilename() && "Unexpected lipo output.");
3047 CmdArgs.push_back("-o");
3048 CmdArgs.push_back(Output.getFilename());
3049
Daniel Dunbarb440f562010-08-02 02:38:21 +00003050 assert(Input.isFilename() && "Invalid input.");
3051 CmdArgs.push_back(Input.getFilename());
Daniel Dunbarbe220842009-03-20 16:06:39 +00003052
3053 // asm_final spec is empty.
3054
Daniel Dunbard067f7f2009-04-08 23:54:23 +00003055 const char *Exec =
Daniel Dunbar9c3ed5f2010-07-14 18:46:23 +00003056 Args.MakeArgString(getToolChain().GetProgramPath("as"));
Daniel Dunbar7fbaf532010-08-02 02:38:28 +00003057 C.addCommand(new Command(JA, *this, Exec, CmdArgs));
Daniel Dunbarbe220842009-03-20 16:06:39 +00003058}
Daniel Dunbar64ed5e32009-03-20 00:52:38 +00003059
Daniel Dunbare9ded432009-09-09 18:36:20 +00003060void darwin::DarwinTool::AddDarwinArch(const ArgList &Args,
3061 ArgStringList &CmdArgs) const {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003062 StringRef ArchName = getDarwinToolChain().getDarwinArchName(Args);
Daniel Dunbardcc3b652010-01-22 02:04:58 +00003063
Daniel Dunbarc1964212009-03-26 16:23:12 +00003064 // Derived from darwin_arch spec.
3065 CmdArgs.push_back("-arch");
Daniel Dunbardcc3b652010-01-22 02:04:58 +00003066 CmdArgs.push_back(Args.MakeArgString(ArchName));
Daniel Dunbar91dbfd62009-09-04 18:35:31 +00003067
Daniel Dunbardcc3b652010-01-22 02:04:58 +00003068 // FIXME: Is this needed anymore?
3069 if (ArchName == "arm")
Daniel Dunbar91dbfd62009-09-04 18:35:31 +00003070 CmdArgs.push_back("-force_cpusubtype_ALL");
Daniel Dunbarc1964212009-03-26 16:23:12 +00003071}
3072
Daniel Dunbarccbc4522010-09-09 21:51:05 +00003073void darwin::Link::AddLinkArgs(Compilation &C,
3074 const ArgList &Args,
Daniel Dunbarc1964212009-03-26 16:23:12 +00003075 ArgStringList &CmdArgs) const {
Daniel Dunbar083edf72009-12-21 18:54:17 +00003076 const Driver &D = getToolChain().getDriver();
Daniel Dunbarc44d3132011-04-28 21:23:41 +00003077 const toolchains::Darwin &DarwinTC = getDarwinToolChain();
Daniel Dunbarc1964212009-03-26 16:23:12 +00003078
Daniel Dunbarcacb0e22010-08-11 23:07:50 +00003079 unsigned Version[3] = { 0, 0, 0 };
3080 if (Arg *A = Args.getLastArg(options::OPT_mlinker_version_EQ)) {
3081 bool HadExtra;
3082 if (!Driver::GetReleaseVersion(A->getValue(Args), Version[0],
3083 Version[1], Version[2], HadExtra) ||
3084 HadExtra)
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003085 D.Diag(diag::err_drv_invalid_version_number)
Daniel Dunbarcacb0e22010-08-11 23:07:50 +00003086 << A->getAsString(Args);
3087 }
3088
3089 // Newer linkers support -demangle, pass it if supported and not disabled by
3090 // the user.
Daniel Dunbar6d776eb2010-11-19 17:51:40 +00003091 //
3092 // FIXME: We temporarily avoid passing -demangle to any iOS linker, because
3093 // unfortunately we can't be guaranteed that the linker version used there
3094 // will match the linker version detected at configure time. We need the
3095 // universal driver.
3096 if (Version[0] >= 100 && !Args.hasArg(options::OPT_Z_Xlinker__no_demangle) &&
Daniel Dunbarc44d3132011-04-28 21:23:41 +00003097 !DarwinTC.isTargetIPhoneOS()) {
Daniel Dunbar1eaf5c62010-09-07 17:07:49 +00003098 // Don't pass -demangle to ld_classic.
3099 //
3100 // FIXME: This is a temporary workaround, ld should be handling this.
3101 bool UsesLdClassic = (getToolChain().getArch() == llvm::Triple::x86 &&
3102 Args.hasArg(options::OPT_static));
Daniel Dunbar3d7e0e22010-09-07 17:50:41 +00003103 if (getToolChain().getArch() == llvm::Triple::x86) {
3104 for (arg_iterator it = Args.filtered_begin(options::OPT_Xlinker,
3105 options::OPT_Wl_COMMA),
3106 ie = Args.filtered_end(); it != ie; ++it) {
3107 const Arg *A = *it;
3108 for (unsigned i = 0, e = A->getNumValues(); i != e; ++i)
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003109 if (StringRef(A->getValue(Args, i)) == "-kext")
Daniel Dunbar3d7e0e22010-09-07 17:50:41 +00003110 UsesLdClassic = true;
3111 }
3112 }
Daniel Dunbar1eaf5c62010-09-07 17:07:49 +00003113 if (!UsesLdClassic)
3114 CmdArgs.push_back("-demangle");
Daniel Dunbarcacb0e22010-08-11 23:07:50 +00003115 }
3116
Daniel Dunbaref889c72011-06-21 20:55:11 +00003117 // If we are using LTO, then automatically create a temporary file path for
3118 // the linker to use, so that it's lifetime will extend past a possible
3119 // dsymutil step.
Daniel Dunbar3d125d32011-06-21 21:18:32 +00003120 if (Version[0] >= 116 && D.IsUsingLTO(Args)) {
Daniel Dunbaref889c72011-06-21 20:55:11 +00003121 const char *TmpPath = C.getArgs().MakeArgString(
Chad Rosier39ab7432011-08-26 21:28:44 +00003122 D.GetTemporaryPath("cc", types::getTypeTempSuffix(types::TY_Object)));
Daniel Dunbaref889c72011-06-21 20:55:11 +00003123 C.addTempFile(TmpPath);
3124 CmdArgs.push_back("-object_path_lto");
3125 CmdArgs.push_back(TmpPath);
3126 }
3127
Daniel Dunbarc1964212009-03-26 16:23:12 +00003128 // Derived from the "link" spec.
3129 Args.AddAllArgs(CmdArgs, options::OPT_static);
3130 if (!Args.hasArg(options::OPT_static))
3131 CmdArgs.push_back("-dynamic");
3132 if (Args.hasArg(options::OPT_fgnu_runtime)) {
3133 // FIXME: gcc replaces -lobjc in forward args with -lobjc-gnu
3134 // here. How do we wish to handle such things?
3135 }
Daniel Dunbard067f7f2009-04-08 23:54:23 +00003136
Daniel Dunbarc1964212009-03-26 16:23:12 +00003137 if (!Args.hasArg(options::OPT_dynamiclib)) {
Daniel Dunbara48823f2010-01-22 02:04:52 +00003138 AddDarwinArch(Args, CmdArgs);
Daniel Dunbara48823f2010-01-22 02:04:52 +00003139 // FIXME: Why do this only on this path?
Daniel Dunbar93d7acf2010-01-22 03:37:33 +00003140 Args.AddLastArg(CmdArgs, options::OPT_force__cpusubtype__ALL);
Daniel Dunbarc1964212009-03-26 16:23:12 +00003141
3142 Args.AddLastArg(CmdArgs, options::OPT_bundle);
3143 Args.AddAllArgs(CmdArgs, options::OPT_bundle__loader);
3144 Args.AddAllArgs(CmdArgs, options::OPT_client__name);
3145
3146 Arg *A;
3147 if ((A = Args.getLastArg(options::OPT_compatibility__version)) ||
3148 (A = Args.getLastArg(options::OPT_current__version)) ||
3149 (A = Args.getLastArg(options::OPT_install__name)))
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003150 D.Diag(diag::err_drv_argument_only_allowed_with)
Daniel Dunbarc1964212009-03-26 16:23:12 +00003151 << A->getAsString(Args) << "-dynamiclib";
3152
3153 Args.AddLastArg(CmdArgs, options::OPT_force__flat__namespace);
3154 Args.AddLastArg(CmdArgs, options::OPT_keep__private__externs);
3155 Args.AddLastArg(CmdArgs, options::OPT_private__bundle);
3156 } else {
3157 CmdArgs.push_back("-dylib");
3158
3159 Arg *A;
3160 if ((A = Args.getLastArg(options::OPT_bundle)) ||
3161 (A = Args.getLastArg(options::OPT_bundle__loader)) ||
3162 (A = Args.getLastArg(options::OPT_client__name)) ||
3163 (A = Args.getLastArg(options::OPT_force__flat__namespace)) ||
3164 (A = Args.getLastArg(options::OPT_keep__private__externs)) ||
3165 (A = Args.getLastArg(options::OPT_private__bundle)))
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003166 D.Diag(diag::err_drv_argument_not_allowed_with)
Daniel Dunbarc1964212009-03-26 16:23:12 +00003167 << A->getAsString(Args) << "-dynamiclib";
Daniel Dunbard067f7f2009-04-08 23:54:23 +00003168
Daniel Dunbarc1964212009-03-26 16:23:12 +00003169 Args.AddAllArgsTranslated(CmdArgs, options::OPT_compatibility__version,
3170 "-dylib_compatibility_version");
3171 Args.AddAllArgsTranslated(CmdArgs, options::OPT_current__version,
3172 "-dylib_current_version");
3173
Daniel Dunbara48823f2010-01-22 02:04:52 +00003174 AddDarwinArch(Args, CmdArgs);
Daniel Dunbarc1964212009-03-26 16:23:12 +00003175
3176 Args.AddAllArgsTranslated(CmdArgs, options::OPT_install__name,
3177 "-dylib_install_name");
3178 }
3179
3180 Args.AddLastArg(CmdArgs, options::OPT_all__load);
3181 Args.AddAllArgs(CmdArgs, options::OPT_allowable__client);
3182 Args.AddLastArg(CmdArgs, options::OPT_bind__at__load);
Daniel Dunbarc44d3132011-04-28 21:23:41 +00003183 if (DarwinTC.isTargetIPhoneOS())
Daniel Dunbar2b5f6812009-09-04 18:35:41 +00003184 Args.AddLastArg(CmdArgs, options::OPT_arch__errors__fatal);
Daniel Dunbarc1964212009-03-26 16:23:12 +00003185 Args.AddLastArg(CmdArgs, options::OPT_dead__strip);
3186 Args.AddLastArg(CmdArgs, options::OPT_no__dead__strip__inits__and__terms);
3187 Args.AddAllArgs(CmdArgs, options::OPT_dylib__file);
3188 Args.AddLastArg(CmdArgs, options::OPT_dynamic);
3189 Args.AddAllArgs(CmdArgs, options::OPT_exported__symbols__list);
3190 Args.AddLastArg(CmdArgs, options::OPT_flat__namespace);
Daniel Dunbar044a3902011-06-28 20:16:02 +00003191 Args.AddAllArgs(CmdArgs, options::OPT_force__load);
Daniel Dunbarc1964212009-03-26 16:23:12 +00003192 Args.AddAllArgs(CmdArgs, options::OPT_headerpad__max__install__names);
3193 Args.AddAllArgs(CmdArgs, options::OPT_image__base);
3194 Args.AddAllArgs(CmdArgs, options::OPT_init);
3195
Daniel Dunbarc44d3132011-04-28 21:23:41 +00003196 // Add the deployment target.
3197 unsigned TargetVersion[3];
3198 DarwinTC.getTargetVersion(TargetVersion);
Daniel Dunbar72ceb922011-04-30 04:22:58 +00003199
3200 // If we had an explicit -mios-simulator-version-min argument, honor that,
3201 // otherwise use the traditional deployment targets. We can't just check the
3202 // is-sim attribute because existing code follows this path, and the linker
3203 // may not handle the argument.
3204 //
3205 // FIXME: We may be able to remove this, once we can verify no one depends on
3206 // it.
3207 if (Args.hasArg(options::OPT_mios_simulator_version_min_EQ))
3208 CmdArgs.push_back("-ios_simulator_version_min");
3209 else if (DarwinTC.isTargetIPhoneOS())
3210 CmdArgs.push_back("-iphoneos_version_min");
3211 else
3212 CmdArgs.push_back("-macosx_version_min");
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003213 CmdArgs.push_back(Args.MakeArgString(Twine(TargetVersion[0]) + "." +
3214 Twine(TargetVersion[1]) + "." +
3215 Twine(TargetVersion[2])));
Daniel Dunbarc44d3132011-04-28 21:23:41 +00003216
Daniel Dunbarc1964212009-03-26 16:23:12 +00003217 Args.AddLastArg(CmdArgs, options::OPT_nomultidefs);
3218 Args.AddLastArg(CmdArgs, options::OPT_multi__module);
3219 Args.AddLastArg(CmdArgs, options::OPT_single__module);
3220 Args.AddAllArgs(CmdArgs, options::OPT_multiply__defined);
3221 Args.AddAllArgs(CmdArgs, options::OPT_multiply__defined__unused);
Daniel Dunbard067f7f2009-04-08 23:54:23 +00003222
Daniel Dunbaraf68a882010-07-13 23:31:40 +00003223 if (const Arg *A = Args.getLastArg(options::OPT_fpie, options::OPT_fPIE,
3224 options::OPT_fno_pie,
3225 options::OPT_fno_PIE)) {
3226 if (A->getOption().matches(options::OPT_fpie) ||
3227 A->getOption().matches(options::OPT_fPIE))
3228 CmdArgs.push_back("-pie");
3229 else
3230 CmdArgs.push_back("-no_pie");
3231 }
Daniel Dunbarc1964212009-03-26 16:23:12 +00003232
3233 Args.AddLastArg(CmdArgs, options::OPT_prebind);
3234 Args.AddLastArg(CmdArgs, options::OPT_noprebind);
3235 Args.AddLastArg(CmdArgs, options::OPT_nofixprebinding);
3236 Args.AddLastArg(CmdArgs, options::OPT_prebind__all__twolevel__modules);
3237 Args.AddLastArg(CmdArgs, options::OPT_read__only__relocs);
3238 Args.AddAllArgs(CmdArgs, options::OPT_sectcreate);
3239 Args.AddAllArgs(CmdArgs, options::OPT_sectorder);
3240 Args.AddAllArgs(CmdArgs, options::OPT_seg1addr);
3241 Args.AddAllArgs(CmdArgs, options::OPT_segprot);
3242 Args.AddAllArgs(CmdArgs, options::OPT_segaddr);
3243 Args.AddAllArgs(CmdArgs, options::OPT_segs__read__only__addr);
3244 Args.AddAllArgs(CmdArgs, options::OPT_segs__read__write__addr);
3245 Args.AddAllArgs(CmdArgs, options::OPT_seg__addr__table);
3246 Args.AddAllArgs(CmdArgs, options::OPT_seg__addr__table__filename);
3247 Args.AddAllArgs(CmdArgs, options::OPT_sub__library);
3248 Args.AddAllArgs(CmdArgs, options::OPT_sub__umbrella);
Daniel Dunbar2b5f6812009-09-04 18:35:41 +00003249
Daniel Dunbar84384642011-05-02 21:03:47 +00003250 // Give --sysroot= preference, over the Apple specific behavior to also use
3251 // --isysroot as the syslibroot.
3252 if (const Arg *A = Args.getLastArg(options::OPT__sysroot_EQ)) {
3253 CmdArgs.push_back("-syslibroot");
3254 CmdArgs.push_back(A->getValue(Args));
3255 } else if (const Arg *A = Args.getLastArg(options::OPT_isysroot)) {
3256 CmdArgs.push_back("-syslibroot");
3257 CmdArgs.push_back(A->getValue(Args));
3258 } else if (getDarwinToolChain().isTargetIPhoneOS()) {
3259 CmdArgs.push_back("-syslibroot");
3260 CmdArgs.push_back("/Developer/SDKs/Extra");
Daniel Dunbar2b5f6812009-09-04 18:35:41 +00003261 }
3262
Daniel Dunbarc1964212009-03-26 16:23:12 +00003263 Args.AddLastArg(CmdArgs, options::OPT_twolevel__namespace);
3264 Args.AddLastArg(CmdArgs, options::OPT_twolevel__namespace__hints);
3265 Args.AddAllArgs(CmdArgs, options::OPT_umbrella);
3266 Args.AddAllArgs(CmdArgs, options::OPT_undefined);
3267 Args.AddAllArgs(CmdArgs, options::OPT_unexported__symbols__list);
Daniel Dunbar2b5f6812009-09-04 18:35:41 +00003268 Args.AddAllArgs(CmdArgs, options::OPT_weak__reference__mismatches);
Daniel Dunbarc1964212009-03-26 16:23:12 +00003269 Args.AddLastArg(CmdArgs, options::OPT_X_Flag);
3270 Args.AddAllArgs(CmdArgs, options::OPT_y);
3271 Args.AddLastArg(CmdArgs, options::OPT_w);
3272 Args.AddAllArgs(CmdArgs, options::OPT_pagezero__size);
3273 Args.AddAllArgs(CmdArgs, options::OPT_segs__read__);
3274 Args.AddLastArg(CmdArgs, options::OPT_seglinkedit);
3275 Args.AddLastArg(CmdArgs, options::OPT_noseglinkedit);
3276 Args.AddAllArgs(CmdArgs, options::OPT_sectalign);
3277 Args.AddAllArgs(CmdArgs, options::OPT_sectobjectsymbols);
3278 Args.AddAllArgs(CmdArgs, options::OPT_segcreate);
3279 Args.AddLastArg(CmdArgs, options::OPT_whyload);
3280 Args.AddLastArg(CmdArgs, options::OPT_whatsloaded);
3281 Args.AddAllArgs(CmdArgs, options::OPT_dylinker__install__name);
3282 Args.AddLastArg(CmdArgs, options::OPT_dylinker);
3283 Args.AddLastArg(CmdArgs, options::OPT_Mach);
3284}
3285
3286void darwin::Link::ConstructJob(Compilation &C, const JobAction &JA,
Daniel Dunbar7fbaf532010-08-02 02:38:28 +00003287 const InputInfo &Output,
Daniel Dunbard067f7f2009-04-08 23:54:23 +00003288 const InputInfoList &Inputs,
3289 const ArgList &Args,
Daniel Dunbarc1964212009-03-26 16:23:12 +00003290 const char *LinkingOutput) const {
3291 assert(Output.getType() == types::TY_Image && "Invalid linker output type.");
Daniel Dunbarc09988d2009-09-08 16:39:16 +00003292
Daniel Dunbarc1964212009-03-26 16:23:12 +00003293 // The logic here is derived from gcc's behavior; most of which
3294 // comes from specs (starting with link_command). Consult gcc for
3295 // more information.
Daniel Dunbarc1964212009-03-26 16:23:12 +00003296 ArgStringList CmdArgs;
3297
3298 // I'm not sure why this particular decomposition exists in gcc, but
3299 // we follow suite for ease of comparison.
Daniel Dunbarccbc4522010-09-09 21:51:05 +00003300 AddLinkArgs(C, Args, CmdArgs);
Daniel Dunbarc1964212009-03-26 16:23:12 +00003301
Daniel Dunbarc1964212009-03-26 16:23:12 +00003302 Args.AddAllArgs(CmdArgs, options::OPT_d_Flag);
3303 Args.AddAllArgs(CmdArgs, options::OPT_s);
3304 Args.AddAllArgs(CmdArgs, options::OPT_t);
3305 Args.AddAllArgs(CmdArgs, options::OPT_Z_Flag);
3306 Args.AddAllArgs(CmdArgs, options::OPT_u_Group);
3307 Args.AddAllArgs(CmdArgs, options::OPT_A);
3308 Args.AddLastArg(CmdArgs, options::OPT_e);
3309 Args.AddAllArgs(CmdArgs, options::OPT_m_Separate);
3310 Args.AddAllArgs(CmdArgs, options::OPT_r);
3311
Daniel Dunbar767bbab2010-10-18 22:08:36 +00003312 // Forward -ObjC when either -ObjC or -ObjC++ is used, to force loading
3313 // members of static archive libraries which implement Objective-C classes or
3314 // categories.
3315 if (Args.hasArg(options::OPT_ObjC) || Args.hasArg(options::OPT_ObjCXX))
3316 CmdArgs.push_back("-ObjC");
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00003317
Daniel Dunbarc1964212009-03-26 16:23:12 +00003318 CmdArgs.push_back("-o");
3319 CmdArgs.push_back(Output.getFilename());
3320
Daniel Dunbarc1964212009-03-26 16:23:12 +00003321 if (!Args.hasArg(options::OPT_A) &&
3322 !Args.hasArg(options::OPT_nostdlib) &&
3323 !Args.hasArg(options::OPT_nostartfiles)) {
3324 // Derived from startfile spec.
3325 if (Args.hasArg(options::OPT_dynamiclib)) {
Daniel Dunbard067f7f2009-04-08 23:54:23 +00003326 // Derived from darwin_dylib1 spec.
Daniel Dunbar16d97092011-04-01 21:02:42 +00003327 if (getDarwinToolChain().isTargetIOSSimulator()) {
3328 // The simulator doesn't have a versioned crt1 file.
3329 CmdArgs.push_back("-ldylib1.o");
3330 } else if (getDarwinToolChain().isTargetIPhoneOS()) {
Daniel Dunbar83608032010-01-27 00:56:56 +00003331 if (getDarwinToolChain().isIPhoneOSVersionLT(3, 1))
3332 CmdArgs.push_back("-ldylib1.o");
3333 } else {
Daniel Dunbar6d23b2f2010-01-27 00:57:03 +00003334 if (getDarwinToolChain().isMacosxVersionLT(10, 5))
Daniel Dunbar83608032010-01-27 00:56:56 +00003335 CmdArgs.push_back("-ldylib1.o");
Daniel Dunbar6d23b2f2010-01-27 00:57:03 +00003336 else if (getDarwinToolChain().isMacosxVersionLT(10, 6))
Daniel Dunbar83608032010-01-27 00:56:56 +00003337 CmdArgs.push_back("-ldylib1.10.5.o");
3338 }
Daniel Dunbarc1964212009-03-26 16:23:12 +00003339 } else {
3340 if (Args.hasArg(options::OPT_bundle)) {
Daniel Dunbarae8bca02009-04-01 03:17:40 +00003341 if (!Args.hasArg(options::OPT_static)) {
3342 // Derived from darwin_bundle1 spec.
Daniel Dunbar16d97092011-04-01 21:02:42 +00003343 if (getDarwinToolChain().isTargetIOSSimulator()) {
3344 // The simulator doesn't have a versioned crt1 file.
3345 CmdArgs.push_back("-lbundle1.o");
3346 } else if (getDarwinToolChain().isTargetIPhoneOS()) {
Daniel Dunbar83608032010-01-27 00:56:56 +00003347 if (getDarwinToolChain().isIPhoneOSVersionLT(3, 1))
3348 CmdArgs.push_back("-lbundle1.o");
3349 } else {
Daniel Dunbar6d23b2f2010-01-27 00:57:03 +00003350 if (getDarwinToolChain().isMacosxVersionLT(10, 6))
Daniel Dunbar83608032010-01-27 00:56:56 +00003351 CmdArgs.push_back("-lbundle1.o");
3352 }
Daniel Dunbarae8bca02009-04-01 03:17:40 +00003353 }
Daniel Dunbarc1964212009-03-26 16:23:12 +00003354 } else {
Daniel Dunbar733b0f82011-03-01 18:49:30 +00003355 if (Args.hasArg(options::OPT_pg) &&
3356 getToolChain().SupportsProfiling()) {
Daniel Dunbarc1964212009-03-26 16:23:12 +00003357 if (Args.hasArg(options::OPT_static) ||
3358 Args.hasArg(options::OPT_object) ||
3359 Args.hasArg(options::OPT_preload)) {
3360 CmdArgs.push_back("-lgcrt0.o");
3361 } else {
3362 CmdArgs.push_back("-lgcrt1.o");
Daniel Dunbard067f7f2009-04-08 23:54:23 +00003363
Daniel Dunbarc1964212009-03-26 16:23:12 +00003364 // darwin_crt2 spec is empty.
Daniel Dunbard067f7f2009-04-08 23:54:23 +00003365 }
Daniel Dunbarc1964212009-03-26 16:23:12 +00003366 } else {
3367 if (Args.hasArg(options::OPT_static) ||
3368 Args.hasArg(options::OPT_object) ||
3369 Args.hasArg(options::OPT_preload)) {
3370 CmdArgs.push_back("-lcrt0.o");
3371 } else {
3372 // Derived from darwin_crt1 spec.
Daniel Dunbarebc34df2011-03-31 17:12:33 +00003373 if (getDarwinToolChain().isTargetIOSSimulator()) {
3374 // The simulator doesn't have a versioned crt1 file.
3375 CmdArgs.push_back("-lcrt1.o");
3376 } else if (getDarwinToolChain().isTargetIPhoneOS()) {
Daniel Dunbar83608032010-01-27 00:56:56 +00003377 if (getDarwinToolChain().isIPhoneOSVersionLT(3, 1))
3378 CmdArgs.push_back("-lcrt1.o");
3379 else
3380 CmdArgs.push_back("-lcrt1.3.1.o");
Daniel Dunbar6d23b2f2010-01-27 00:57:03 +00003381 } else {
3382 if (getDarwinToolChain().isMacosxVersionLT(10, 5))
3383 CmdArgs.push_back("-lcrt1.o");
3384 else if (getDarwinToolChain().isMacosxVersionLT(10, 6))
3385 CmdArgs.push_back("-lcrt1.10.5.o");
3386 else
3387 CmdArgs.push_back("-lcrt1.10.6.o");
Daniel Dunbard067f7f2009-04-08 23:54:23 +00003388
Daniel Dunbar6d23b2f2010-01-27 00:57:03 +00003389 // darwin_crt2 spec is empty.
3390 }
Daniel Dunbarc1964212009-03-26 16:23:12 +00003391 }
3392 }
3393 }
3394 }
3395
Daniel Dunbar6d23b2f2010-01-27 00:57:03 +00003396 if (!getDarwinToolChain().isTargetIPhoneOS() &&
3397 Args.hasArg(options::OPT_shared_libgcc) &&
3398 getDarwinToolChain().isMacosxVersionLT(10, 5)) {
Daniel Dunbar1c28f1e2009-09-09 22:32:48 +00003399 const char *Str =
Daniel Dunbar9c3ed5f2010-07-14 18:46:23 +00003400 Args.MakeArgString(getToolChain().GetFilePath("crt3.o"));
Daniel Dunbar1c28f1e2009-09-09 22:32:48 +00003401 CmdArgs.push_back(Str);
Daniel Dunbarc1964212009-03-26 16:23:12 +00003402 }
3403 }
3404
3405 Args.AddAllArgs(CmdArgs, options::OPT_L);
Daniel Dunbard067f7f2009-04-08 23:54:23 +00003406
Daniel Dunbarc1964212009-03-26 16:23:12 +00003407 if (Args.hasArg(options::OPT_fopenmp))
3408 // This is more complicated in gcc...
3409 CmdArgs.push_back("-lgomp");
3410
Daniel Dunbar4c30b892009-09-18 08:14:36 +00003411 getDarwinToolChain().AddLinkSearchPathArgs(Args, CmdArgs);
Daniel Dunbard067f7f2009-04-08 23:54:23 +00003412
John McCall31168b02011-06-15 23:02:42 +00003413 // In ARC, if we don't have runtime support, link in the runtime
3414 // stubs. We have to do this *before* adding any of the normal
3415 // linker inputs so that its initializer gets run first.
John McCall24fc0de2011-07-06 00:26:06 +00003416 if (isObjCAutoRefCount(Args)) {
3417 ObjCRuntime runtime;
3418 getDarwinToolChain().configureObjCRuntime(runtime);
3419 if (!runtime.HasARC)
3420 getDarwinToolChain().AddLinkARCArgs(Args, CmdArgs);
3421 }
John McCall31168b02011-06-15 23:02:42 +00003422
Daniel Dunbar54423b22010-09-17 00:24:54 +00003423 AddLinkerInputs(getToolChain(), Inputs, Args, CmdArgs);
Daniel Dunbarc1964212009-03-26 16:23:12 +00003424
3425 if (LinkingOutput) {
3426 CmdArgs.push_back("-arch_multiple");
3427 CmdArgs.push_back("-final_output");
3428 CmdArgs.push_back(LinkingOutput);
3429 }
3430
Daniel Dunbarc1964212009-03-26 16:23:12 +00003431 if (Args.hasArg(options::OPT_fnested_functions))
3432 CmdArgs.push_back("-allow_stack_execute");
Daniel Dunbard067f7f2009-04-08 23:54:23 +00003433
Daniel Dunbarc1964212009-03-26 16:23:12 +00003434 if (!Args.hasArg(options::OPT_nostdlib) &&
3435 !Args.hasArg(options::OPT_nodefaultlibs)) {
Daniel Dunbar083edf72009-12-21 18:54:17 +00003436 if (getToolChain().getDriver().CCCIsCXX)
Daniel Dunbar3f7796f2010-09-17 01:20:05 +00003437 getToolChain().AddCXXStdlibLibArgs(Args, CmdArgs);
Daniel Dunbarad0f62b2009-04-08 06:06:21 +00003438
Daniel Dunbarc1964212009-03-26 16:23:12 +00003439 // link_ssp spec is empty.
3440
Daniel Dunbar26d482a2009-09-18 08:15:03 +00003441 // Let the tool chain choose which runtime library to link.
3442 getDarwinToolChain().AddLinkRuntimeLibArgs(Args, CmdArgs);
Daniel Dunbarc1964212009-03-26 16:23:12 +00003443 }
3444
3445 if (!Args.hasArg(options::OPT_A) &&
3446 !Args.hasArg(options::OPT_nostdlib) &&
3447 !Args.hasArg(options::OPT_nostartfiles)) {
3448 // endfile_spec is empty.
3449 }
3450
Bill Wendling08760582011-06-27 19:15:03 +00003451 addProfileRT(getToolChain(), Args, CmdArgs, getToolChain().getTriple());
Nick Lewycky82fe5f42011-05-24 21:54:59 +00003452
Daniel Dunbarc1964212009-03-26 16:23:12 +00003453 Args.AddAllArgs(CmdArgs, options::OPT_T_Group);
3454 Args.AddAllArgs(CmdArgs, options::OPT_F);
3455
Daniel Dunbard067f7f2009-04-08 23:54:23 +00003456 const char *Exec =
Daniel Dunbar9c3ed5f2010-07-14 18:46:23 +00003457 Args.MakeArgString(getToolChain().GetProgramPath("ld"));
Daniel Dunbar7fbaf532010-08-02 02:38:28 +00003458 C.addCommand(new Command(JA, *this, Exec, CmdArgs));
Daniel Dunbarc1964212009-03-26 16:23:12 +00003459}
3460
Daniel Dunbar64ed5e32009-03-20 00:52:38 +00003461void darwin::Lipo::ConstructJob(Compilation &C, const JobAction &JA,
Daniel Dunbar7fbaf532010-08-02 02:38:28 +00003462 const InputInfo &Output,
Daniel Dunbard067f7f2009-04-08 23:54:23 +00003463 const InputInfoList &Inputs,
3464 const ArgList &Args,
Daniel Dunbar64ed5e32009-03-20 00:52:38 +00003465 const char *LinkingOutput) const {
3466 ArgStringList CmdArgs;
3467
3468 CmdArgs.push_back("-create");
3469 assert(Output.isFilename() && "Unexpected lipo output.");
Daniel Dunbar06686ab2009-03-24 00:24:37 +00003470
3471 CmdArgs.push_back("-output");
Daniel Dunbar64ed5e32009-03-20 00:52:38 +00003472 CmdArgs.push_back(Output.getFilename());
Daniel Dunbar06686ab2009-03-24 00:24:37 +00003473
Daniel Dunbar64ed5e32009-03-20 00:52:38 +00003474 for (InputInfoList::const_iterator
3475 it = Inputs.begin(), ie = Inputs.end(); it != ie; ++it) {
3476 const InputInfo &II = *it;
3477 assert(II.isFilename() && "Unexpected lipo input.");
3478 CmdArgs.push_back(II.getFilename());
3479 }
Daniel Dunbard067f7f2009-04-08 23:54:23 +00003480 const char *Exec =
Daniel Dunbar9c3ed5f2010-07-14 18:46:23 +00003481 Args.MakeArgString(getToolChain().GetProgramPath("lipo"));
Daniel Dunbar7fbaf532010-08-02 02:38:28 +00003482 C.addCommand(new Command(JA, *this, Exec, CmdArgs));
Daniel Dunbar64ed5e32009-03-20 00:52:38 +00003483}
Daniel Dunbar8eb473c2009-03-31 17:45:15 +00003484
Daniel Dunbar88299622010-06-04 18:28:36 +00003485void darwin::Dsymutil::ConstructJob(Compilation &C, const JobAction &JA,
Daniel Dunbar7fbaf532010-08-02 02:38:28 +00003486 const InputInfo &Output,
Daniel Dunbar88299622010-06-04 18:28:36 +00003487 const InputInfoList &Inputs,
3488 const ArgList &Args,
3489 const char *LinkingOutput) const {
3490 ArgStringList CmdArgs;
3491
Daniel Dunbareb86b042011-05-09 17:23:16 +00003492 CmdArgs.push_back("-o");
3493 CmdArgs.push_back(Output.getFilename());
3494
Daniel Dunbar88299622010-06-04 18:28:36 +00003495 assert(Inputs.size() == 1 && "Unable to handle multiple inputs.");
3496 const InputInfo &Input = Inputs[0];
3497 assert(Input.isFilename() && "Unexpected dsymutil input.");
3498 CmdArgs.push_back(Input.getFilename());
3499
Daniel Dunbar88299622010-06-04 18:28:36 +00003500 const char *Exec =
Daniel Dunbar9c3ed5f2010-07-14 18:46:23 +00003501 Args.MakeArgString(getToolChain().GetProgramPath("dsymutil"));
Daniel Dunbar7fbaf532010-08-02 02:38:28 +00003502 C.addCommand(new Command(JA, *this, Exec, CmdArgs));
Daniel Dunbar88299622010-06-04 18:28:36 +00003503}
3504
Eric Christopher551ef452011-08-23 17:56:55 +00003505void darwin::VerifyDebug::ConstructJob(Compilation &C, const JobAction &JA,
3506 const InputInfo &Output,
3507 const InputInfoList &Inputs,
3508 const ArgList &Args,
3509 const char *LinkingOutput) const {
3510 ArgStringList CmdArgs;
3511 CmdArgs.push_back("--verify");
3512
3513 assert(Inputs.size() == 1 && "Unable to handle multiple inputs.");
3514 const InputInfo &Input = Inputs[0];
3515 assert(Input.isFilename() && "Unexpected verify input");
3516
3517 // Grabbing the output of the earlier dsymutil run.
3518 CmdArgs.push_back(Input.getFilename());
3519
3520 const char *Exec =
3521 Args.MakeArgString(getToolChain().GetProgramPath("dwarfdump"));
3522 C.addCommand(new Command(JA, *this, Exec, CmdArgs));
3523}
3524
Edward O'Callaghan856e4ff2009-08-22 01:06:46 +00003525void auroraux::Assemble::ConstructJob(Compilation &C, const JobAction &JA,
Daniel Dunbar7fbaf532010-08-02 02:38:28 +00003526 const InputInfo &Output,
Daniel Dunbar1c7577c2009-11-04 06:24:38 +00003527 const InputInfoList &Inputs,
3528 const ArgList &Args,
3529 const char *LinkingOutput) const {
Edward O'Callaghan856e4ff2009-08-22 01:06:46 +00003530 ArgStringList CmdArgs;
3531
3532 Args.AddAllArgValues(CmdArgs, options::OPT_Wa_COMMA,
3533 options::OPT_Xassembler);
3534
3535 CmdArgs.push_back("-o");
Daniel Dunbarb440f562010-08-02 02:38:21 +00003536 CmdArgs.push_back(Output.getFilename());
Edward O'Callaghan856e4ff2009-08-22 01:06:46 +00003537
3538 for (InputInfoList::const_iterator
3539 it = Inputs.begin(), ie = Inputs.end(); it != ie; ++it) {
3540 const InputInfo &II = *it;
Daniel Dunbarb440f562010-08-02 02:38:21 +00003541 CmdArgs.push_back(II.getFilename());
Edward O'Callaghan856e4ff2009-08-22 01:06:46 +00003542 }
3543
3544 const char *Exec =
Daniel Dunbar9c3ed5f2010-07-14 18:46:23 +00003545 Args.MakeArgString(getToolChain().GetProgramPath("gas"));
Daniel Dunbar7fbaf532010-08-02 02:38:28 +00003546 C.addCommand(new Command(JA, *this, Exec, CmdArgs));
Edward O'Callaghan856e4ff2009-08-22 01:06:46 +00003547}
3548
3549void auroraux::Link::ConstructJob(Compilation &C, const JobAction &JA,
Daniel Dunbar7fbaf532010-08-02 02:38:28 +00003550 const InputInfo &Output,
Daniel Dunbar1c7577c2009-11-04 06:24:38 +00003551 const InputInfoList &Inputs,
3552 const ArgList &Args,
3553 const char *LinkingOutput) const {
Edward O'Callaghan856e4ff2009-08-22 01:06:46 +00003554 ArgStringList CmdArgs;
3555
3556 if ((!Args.hasArg(options::OPT_nostdlib)) &&
Daniel Dunbar1c7577c2009-11-04 06:24:38 +00003557 (!Args.hasArg(options::OPT_shared))) {
Edward O'Callaghan856e4ff2009-08-22 01:06:46 +00003558 CmdArgs.push_back("-e");
Edward O'Callaghand8712d92009-10-15 07:44:07 +00003559 CmdArgs.push_back("_start");
Edward O'Callaghan856e4ff2009-08-22 01:06:46 +00003560 }
3561
3562 if (Args.hasArg(options::OPT_static)) {
3563 CmdArgs.push_back("-Bstatic");
Edward O'Callaghand8712d92009-10-15 07:44:07 +00003564 CmdArgs.push_back("-dn");
Edward O'Callaghan856e4ff2009-08-22 01:06:46 +00003565 } else {
Edward O'Callaghand8712d92009-10-15 07:44:07 +00003566// CmdArgs.push_back("--eh-frame-hdr");
Edward O'Callaghan856e4ff2009-08-22 01:06:46 +00003567 CmdArgs.push_back("-Bdynamic");
3568 if (Args.hasArg(options::OPT_shared)) {
3569 CmdArgs.push_back("-shared");
3570 } else {
Edward O'Callaghan7d3c2752009-10-16 19:44:18 +00003571 CmdArgs.push_back("--dynamic-linker");
Edward O'Callaghan856e4ff2009-08-22 01:06:46 +00003572 CmdArgs.push_back("/lib/ld.so.1"); // 64Bit Path /lib/amd64/ld.so.1
3573 }
3574 }
3575
Daniel Dunbarb440f562010-08-02 02:38:21 +00003576 if (Output.isFilename()) {
Edward O'Callaghan856e4ff2009-08-22 01:06:46 +00003577 CmdArgs.push_back("-o");
3578 CmdArgs.push_back(Output.getFilename());
3579 } else {
3580 assert(Output.isNothing() && "Invalid output.");
3581 }
3582
3583 if (!Args.hasArg(options::OPT_nostdlib) &&
3584 !Args.hasArg(options::OPT_nostartfiles)) {
3585 if (!Args.hasArg(options::OPT_shared)) {
Chris Lattner3e2ee142010-07-07 16:01:42 +00003586 CmdArgs.push_back(Args.MakeArgString(
Daniel Dunbar9c3ed5f2010-07-14 18:46:23 +00003587 getToolChain().GetFilePath("crt1.o")));
Chris Lattner3e2ee142010-07-07 16:01:42 +00003588 CmdArgs.push_back(Args.MakeArgString(
Daniel Dunbar9c3ed5f2010-07-14 18:46:23 +00003589 getToolChain().GetFilePath("crti.o")));
Chris Lattner3e2ee142010-07-07 16:01:42 +00003590 CmdArgs.push_back(Args.MakeArgString(
Daniel Dunbar9c3ed5f2010-07-14 18:46:23 +00003591 getToolChain().GetFilePath("crtbegin.o")));
Edward O'Callaghan856e4ff2009-08-22 01:06:46 +00003592 } else {
Chris Lattner3e2ee142010-07-07 16:01:42 +00003593 CmdArgs.push_back(Args.MakeArgString(
Daniel Dunbar9c3ed5f2010-07-14 18:46:23 +00003594 getToolChain().GetFilePath("crti.o")));
Edward O'Callaghan856e4ff2009-08-22 01:06:46 +00003595 }
Chris Lattner3e2ee142010-07-07 16:01:42 +00003596 CmdArgs.push_back(Args.MakeArgString(
Daniel Dunbar9c3ed5f2010-07-14 18:46:23 +00003597 getToolChain().GetFilePath("crtn.o")));
Edward O'Callaghan856e4ff2009-08-22 01:06:46 +00003598 }
3599
Daniel Dunbar1c7577c2009-11-04 06:24:38 +00003600 CmdArgs.push_back(Args.MakeArgString("-L/opt/gcc4/lib/gcc/"
3601 + getToolChain().getTripleString()
Daniel Dunbarb0b18612009-10-29 02:24:37 +00003602 + "/4.2.4"));
Edward O'Callaghan856e4ff2009-08-22 01:06:46 +00003603
3604 Args.AddAllArgs(CmdArgs, options::OPT_L);
3605 Args.AddAllArgs(CmdArgs, options::OPT_T_Group);
3606 Args.AddAllArgs(CmdArgs, options::OPT_e);
3607
Daniel Dunbar54423b22010-09-17 00:24:54 +00003608 AddLinkerInputs(getToolChain(), Inputs, Args, CmdArgs);
Edward O'Callaghan856e4ff2009-08-22 01:06:46 +00003609
3610 if (!Args.hasArg(options::OPT_nostdlib) &&
3611 !Args.hasArg(options::OPT_nodefaultlibs)) {
3612 // FIXME: For some reason GCC passes -lgcc before adding
3613 // the default system libraries. Just mimic this for now.
3614 CmdArgs.push_back("-lgcc");
3615
3616 if (Args.hasArg(options::OPT_pthread))
3617 CmdArgs.push_back("-pthread");
3618 if (!Args.hasArg(options::OPT_shared))
3619 CmdArgs.push_back("-lc");
3620 CmdArgs.push_back("-lgcc");
3621 }
3622
3623 if (!Args.hasArg(options::OPT_nostdlib) &&
3624 !Args.hasArg(options::OPT_nostartfiles)) {
3625 if (!Args.hasArg(options::OPT_shared))
Chris Lattner3e2ee142010-07-07 16:01:42 +00003626 CmdArgs.push_back(Args.MakeArgString(
Daniel Dunbar9c3ed5f2010-07-14 18:46:23 +00003627 getToolChain().GetFilePath("crtend.o")));
Edward O'Callaghan856e4ff2009-08-22 01:06:46 +00003628 }
3629
Bill Wendling08760582011-06-27 19:15:03 +00003630 addProfileRT(getToolChain(), Args, CmdArgs, getToolChain().getTriple());
Nick Lewycky82fe5f42011-05-24 21:54:59 +00003631
Edward O'Callaghan856e4ff2009-08-22 01:06:46 +00003632 const char *Exec =
Daniel Dunbar9c3ed5f2010-07-14 18:46:23 +00003633 Args.MakeArgString(getToolChain().GetProgramPath("ld"));
Daniel Dunbar7fbaf532010-08-02 02:38:28 +00003634 C.addCommand(new Command(JA, *this, Exec, CmdArgs));
Edward O'Callaghan856e4ff2009-08-22 01:06:46 +00003635}
3636
Daniel Dunbar10de9e62009-06-29 20:52:51 +00003637void openbsd::Assemble::ConstructJob(Compilation &C, const JobAction &JA,
Daniel Dunbar7fbaf532010-08-02 02:38:28 +00003638 const InputInfo &Output,
Daniel Dunbar10de9e62009-06-29 20:52:51 +00003639 const InputInfoList &Inputs,
3640 const ArgList &Args,
Mike Stump11289f42009-09-09 15:08:12 +00003641 const char *LinkingOutput) const {
Daniel Dunbar10de9e62009-06-29 20:52:51 +00003642 ArgStringList CmdArgs;
3643
3644 Args.AddAllArgValues(CmdArgs, options::OPT_Wa_COMMA,
3645 options::OPT_Xassembler);
3646
3647 CmdArgs.push_back("-o");
Daniel Dunbarb440f562010-08-02 02:38:21 +00003648 CmdArgs.push_back(Output.getFilename());
Daniel Dunbar10de9e62009-06-29 20:52:51 +00003649
3650 for (InputInfoList::const_iterator
3651 it = Inputs.begin(), ie = Inputs.end(); it != ie; ++it) {
3652 const InputInfo &II = *it;
Daniel Dunbarb440f562010-08-02 02:38:21 +00003653 CmdArgs.push_back(II.getFilename());
Daniel Dunbar10de9e62009-06-29 20:52:51 +00003654 }
3655
3656 const char *Exec =
Daniel Dunbar9c3ed5f2010-07-14 18:46:23 +00003657 Args.MakeArgString(getToolChain().GetProgramPath("as"));
Daniel Dunbar7fbaf532010-08-02 02:38:28 +00003658 C.addCommand(new Command(JA, *this, Exec, CmdArgs));
Daniel Dunbar10de9e62009-06-29 20:52:51 +00003659}
3660
3661void openbsd::Link::ConstructJob(Compilation &C, const JobAction &JA,
Daniel Dunbar7fbaf532010-08-02 02:38:28 +00003662 const InputInfo &Output,
Daniel Dunbar10de9e62009-06-29 20:52:51 +00003663 const InputInfoList &Inputs,
3664 const ArgList &Args,
3665 const char *LinkingOutput) const {
Daniel Dunbar083edf72009-12-21 18:54:17 +00003666 const Driver &D = getToolChain().getDriver();
Daniel Dunbar10de9e62009-06-29 20:52:51 +00003667 ArgStringList CmdArgs;
3668
Daniel Dunbara8888ac2009-08-03 01:28:59 +00003669 if ((!Args.hasArg(options::OPT_nostdlib)) &&
Daniel Dunbar1c7577c2009-11-04 06:24:38 +00003670 (!Args.hasArg(options::OPT_shared))) {
Daniel Dunbara8888ac2009-08-03 01:28:59 +00003671 CmdArgs.push_back("-e");
3672 CmdArgs.push_back("__start");
3673 }
3674
Daniel Dunbar10de9e62009-06-29 20:52:51 +00003675 if (Args.hasArg(options::OPT_static)) {
3676 CmdArgs.push_back("-Bstatic");
3677 } else {
Rafael Espindola7ba97af2010-11-11 02:17:51 +00003678 if (Args.hasArg(options::OPT_rdynamic))
3679 CmdArgs.push_back("-export-dynamic");
Daniel Dunbar10de9e62009-06-29 20:52:51 +00003680 CmdArgs.push_back("--eh-frame-hdr");
Daniel Dunbara8888ac2009-08-03 01:28:59 +00003681 CmdArgs.push_back("-Bdynamic");
Daniel Dunbar10de9e62009-06-29 20:52:51 +00003682 if (Args.hasArg(options::OPT_shared)) {
Daniel Dunbara8888ac2009-08-03 01:28:59 +00003683 CmdArgs.push_back("-shared");
Daniel Dunbar10de9e62009-06-29 20:52:51 +00003684 } else {
3685 CmdArgs.push_back("-dynamic-linker");
3686 CmdArgs.push_back("/usr/libexec/ld.so");
3687 }
3688 }
3689
Daniel Dunbarb440f562010-08-02 02:38:21 +00003690 if (Output.isFilename()) {
Daniel Dunbar10de9e62009-06-29 20:52:51 +00003691 CmdArgs.push_back("-o");
3692 CmdArgs.push_back(Output.getFilename());
3693 } else {
3694 assert(Output.isNothing() && "Invalid output.");
3695 }
3696
3697 if (!Args.hasArg(options::OPT_nostdlib) &&
3698 !Args.hasArg(options::OPT_nostartfiles)) {
3699 if (!Args.hasArg(options::OPT_shared)) {
Chris Lattner3e2ee142010-07-07 16:01:42 +00003700 CmdArgs.push_back(Args.MakeArgString(
Daniel Dunbar9c3ed5f2010-07-14 18:46:23 +00003701 getToolChain().GetFilePath("crt0.o")));
Chris Lattner3e2ee142010-07-07 16:01:42 +00003702 CmdArgs.push_back(Args.MakeArgString(
Daniel Dunbar9c3ed5f2010-07-14 18:46:23 +00003703 getToolChain().GetFilePath("crtbegin.o")));
Daniel Dunbar10de9e62009-06-29 20:52:51 +00003704 } else {
Chris Lattner3e2ee142010-07-07 16:01:42 +00003705 CmdArgs.push_back(Args.MakeArgString(
Daniel Dunbar9c3ed5f2010-07-14 18:46:23 +00003706 getToolChain().GetFilePath("crtbeginS.o")));
Daniel Dunbar10de9e62009-06-29 20:52:51 +00003707 }
3708 }
3709
Edward O'Callaghan5c521462009-10-28 15:13:08 +00003710 std::string Triple = getToolChain().getTripleString();
3711 if (Triple.substr(0, 6) == "x86_64")
Daniel Dunbar1c7577c2009-11-04 06:24:38 +00003712 Triple.replace(0, 6, "amd64");
Daniel Dunbarb0b18612009-10-29 02:24:37 +00003713 CmdArgs.push_back(Args.MakeArgString("-L/usr/lib/gcc-lib/" + Triple +
Daniel Dunbarea3813f2010-08-01 23:13:54 +00003714 "/4.2.1"));
Daniel Dunbara8888ac2009-08-03 01:28:59 +00003715
Daniel Dunbar10de9e62009-06-29 20:52:51 +00003716 Args.AddAllArgs(CmdArgs, options::OPT_L);
3717 Args.AddAllArgs(CmdArgs, options::OPT_T_Group);
3718 Args.AddAllArgs(CmdArgs, options::OPT_e);
3719
Daniel Dunbar54423b22010-09-17 00:24:54 +00003720 AddLinkerInputs(getToolChain(), Inputs, Args, CmdArgs);
Daniel Dunbar10de9e62009-06-29 20:52:51 +00003721
3722 if (!Args.hasArg(options::OPT_nostdlib) &&
3723 !Args.hasArg(options::OPT_nodefaultlibs)) {
Daniel Dunbarea3813f2010-08-01 23:13:54 +00003724 if (D.CCCIsCXX) {
Daniel Dunbar3f7796f2010-09-17 01:20:05 +00003725 getToolChain().AddCXXStdlibLibArgs(Args, CmdArgs);
Daniel Dunbarea3813f2010-08-01 23:13:54 +00003726 CmdArgs.push_back("-lm");
3727 }
3728
Daniel Dunbara8888ac2009-08-03 01:28:59 +00003729 // FIXME: For some reason GCC passes -lgcc before adding
3730 // the default system libraries. Just mimic this for now.
3731 CmdArgs.push_back("-lgcc");
Daniel Dunbar10de9e62009-06-29 20:52:51 +00003732
3733 if (Args.hasArg(options::OPT_pthread))
Chris Lattnerd0257f72011-02-21 18:36:51 +00003734 CmdArgs.push_back("-lpthread");
Daniel Dunbara8888ac2009-08-03 01:28:59 +00003735 if (!Args.hasArg(options::OPT_shared))
3736 CmdArgs.push_back("-lc");
3737 CmdArgs.push_back("-lgcc");
Daniel Dunbar10de9e62009-06-29 20:52:51 +00003738 }
3739
3740 if (!Args.hasArg(options::OPT_nostdlib) &&
3741 !Args.hasArg(options::OPT_nostartfiles)) {
3742 if (!Args.hasArg(options::OPT_shared))
Chris Lattner3e2ee142010-07-07 16:01:42 +00003743 CmdArgs.push_back(Args.MakeArgString(
Daniel Dunbar9c3ed5f2010-07-14 18:46:23 +00003744 getToolChain().GetFilePath("crtend.o")));
Daniel Dunbar10de9e62009-06-29 20:52:51 +00003745 else
Chris Lattner3e2ee142010-07-07 16:01:42 +00003746 CmdArgs.push_back(Args.MakeArgString(
Daniel Dunbar9c3ed5f2010-07-14 18:46:23 +00003747 getToolChain().GetFilePath("crtendS.o")));
Daniel Dunbar10de9e62009-06-29 20:52:51 +00003748 }
3749
3750 const char *Exec =
Daniel Dunbar9c3ed5f2010-07-14 18:46:23 +00003751 Args.MakeArgString(getToolChain().GetProgramPath("ld"));
Daniel Dunbar7fbaf532010-08-02 02:38:28 +00003752 C.addCommand(new Command(JA, *this, Exec, CmdArgs));
Daniel Dunbar10de9e62009-06-29 20:52:51 +00003753}
Ed Schoutene33194b2009-04-02 19:13:12 +00003754
Daniel Dunbar8eb473c2009-03-31 17:45:15 +00003755void freebsd::Assemble::ConstructJob(Compilation &C, const JobAction &JA,
Daniel Dunbar7fbaf532010-08-02 02:38:28 +00003756 const InputInfo &Output,
Daniel Dunbard067f7f2009-04-08 23:54:23 +00003757 const InputInfoList &Inputs,
3758 const ArgList &Args,
Mike Stump11289f42009-09-09 15:08:12 +00003759 const char *LinkingOutput) const {
Daniel Dunbar8eb473c2009-03-31 17:45:15 +00003760 ArgStringList CmdArgs;
3761
Daniel Dunbard854c8d2009-04-01 19:36:32 +00003762 // When building 32-bit code on FreeBSD/amd64, we have to explicitly
3763 // instruct as in the base system to assemble 32-bit code.
3764 if (getToolChain().getArchName() == "i386")
Daniel Dunbar8eb473c2009-03-31 17:45:15 +00003765 CmdArgs.push_back("--32");
3766
Roman Divacky00859c22011-06-04 07:37:31 +00003767 if (getToolChain().getArchName() == "powerpc")
3768 CmdArgs.push_back("-a32");
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00003769
Eric Christopher0b26a612010-03-02 02:41:08 +00003770 // Set byte order explicitly
3771 if (getToolChain().getArchName() == "mips")
3772 CmdArgs.push_back("-EB");
3773 else if (getToolChain().getArchName() == "mipsel")
3774 CmdArgs.push_back("-EL");
3775
Daniel Dunbar8eb473c2009-03-31 17:45:15 +00003776 Args.AddAllArgValues(CmdArgs, options::OPT_Wa_COMMA,
3777 options::OPT_Xassembler);
3778
3779 CmdArgs.push_back("-o");
Daniel Dunbarb440f562010-08-02 02:38:21 +00003780 CmdArgs.push_back(Output.getFilename());
Daniel Dunbar8eb473c2009-03-31 17:45:15 +00003781
3782 for (InputInfoList::const_iterator
3783 it = Inputs.begin(), ie = Inputs.end(); it != ie; ++it) {
3784 const InputInfo &II = *it;
Daniel Dunbarb440f562010-08-02 02:38:21 +00003785 CmdArgs.push_back(II.getFilename());
Daniel Dunbar8eb473c2009-03-31 17:45:15 +00003786 }
3787
Daniel Dunbard067f7f2009-04-08 23:54:23 +00003788 const char *Exec =
Daniel Dunbar9c3ed5f2010-07-14 18:46:23 +00003789 Args.MakeArgString(getToolChain().GetProgramPath("as"));
Daniel Dunbar7fbaf532010-08-02 02:38:28 +00003790 C.addCommand(new Command(JA, *this, Exec, CmdArgs));
Daniel Dunbar8eb473c2009-03-31 17:45:15 +00003791}
Daniel Dunbard854c8d2009-04-01 19:36:32 +00003792
3793void freebsd::Link::ConstructJob(Compilation &C, const JobAction &JA,
Daniel Dunbar7fbaf532010-08-02 02:38:28 +00003794 const InputInfo &Output,
Daniel Dunbard067f7f2009-04-08 23:54:23 +00003795 const InputInfoList &Inputs,
3796 const ArgList &Args,
Daniel Dunbare3e263f2009-05-02 20:14:53 +00003797 const char *LinkingOutput) const {
Daniel Dunbar083edf72009-12-21 18:54:17 +00003798 const Driver &D = getToolChain().getDriver();
Daniel Dunbard854c8d2009-04-01 19:36:32 +00003799 ArgStringList CmdArgs;
3800
Joerg Sonnenberger6165ab12011-03-21 13:51:29 +00003801 if (!D.SysRoot.empty())
3802 CmdArgs.push_back(Args.MakeArgString("--sysroot=" + D.SysRoot));
3803
Daniel Dunbard854c8d2009-04-01 19:36:32 +00003804 if (Args.hasArg(options::OPT_static)) {
3805 CmdArgs.push_back("-Bstatic");
3806 } else {
Rafael Espindola7ba97af2010-11-11 02:17:51 +00003807 if (Args.hasArg(options::OPT_rdynamic))
3808 CmdArgs.push_back("-export-dynamic");
Daniel Dunbard854c8d2009-04-01 19:36:32 +00003809 CmdArgs.push_back("--eh-frame-hdr");
3810 if (Args.hasArg(options::OPT_shared)) {
3811 CmdArgs.push_back("-Bshareable");
3812 } else {
3813 CmdArgs.push_back("-dynamic-linker");
3814 CmdArgs.push_back("/libexec/ld-elf.so.1");
3815 }
3816 }
3817
3818 // When building 32-bit code on FreeBSD/amd64, we have to explicitly
3819 // instruct ld in the base system to link 32-bit code.
3820 if (getToolChain().getArchName() == "i386") {
3821 CmdArgs.push_back("-m");
3822 CmdArgs.push_back("elf_i386_fbsd");
3823 }
3824
Roman Divacky5e300b82011-06-04 07:40:24 +00003825 if (getToolChain().getArchName() == "powerpc") {
3826 CmdArgs.push_back("-m");
3827 CmdArgs.push_back("elf32ppc");
3828 }
3829
Daniel Dunbarb440f562010-08-02 02:38:21 +00003830 if (Output.isFilename()) {
Daniel Dunbard854c8d2009-04-01 19:36:32 +00003831 CmdArgs.push_back("-o");
3832 CmdArgs.push_back(Output.getFilename());
3833 } else {
3834 assert(Output.isNothing() && "Invalid output.");
3835 }
3836
3837 if (!Args.hasArg(options::OPT_nostdlib) &&
3838 !Args.hasArg(options::OPT_nostartfiles)) {
3839 if (!Args.hasArg(options::OPT_shared)) {
Roman Divacky66f22762011-02-10 16:59:40 +00003840 if (Args.hasArg(options::OPT_pg))
3841 CmdArgs.push_back(Args.MakeArgString(
3842 getToolChain().GetFilePath("gcrt1.o")));
3843 else
3844 CmdArgs.push_back(Args.MakeArgString(
3845 getToolChain().GetFilePath("crt1.o")));
Chris Lattner3e2ee142010-07-07 16:01:42 +00003846 CmdArgs.push_back(Args.MakeArgString(
Daniel Dunbar9c3ed5f2010-07-14 18:46:23 +00003847 getToolChain().GetFilePath("crti.o")));
Chris Lattner3e2ee142010-07-07 16:01:42 +00003848 CmdArgs.push_back(Args.MakeArgString(
Daniel Dunbar9c3ed5f2010-07-14 18:46:23 +00003849 getToolChain().GetFilePath("crtbegin.o")));
Daniel Dunbard854c8d2009-04-01 19:36:32 +00003850 } else {
Chris Lattner3e2ee142010-07-07 16:01:42 +00003851 CmdArgs.push_back(Args.MakeArgString(
Daniel Dunbar9c3ed5f2010-07-14 18:46:23 +00003852 getToolChain().GetFilePath("crti.o")));
Chris Lattner3e2ee142010-07-07 16:01:42 +00003853 CmdArgs.push_back(Args.MakeArgString(
Daniel Dunbar9c3ed5f2010-07-14 18:46:23 +00003854 getToolChain().GetFilePath("crtbeginS.o")));
Daniel Dunbard854c8d2009-04-01 19:36:32 +00003855 }
3856 }
3857
3858 Args.AddAllArgs(CmdArgs, options::OPT_L);
Roman Divackyee8188a2011-03-01 17:53:14 +00003859 const ToolChain::path_list Paths = getToolChain().getFilePaths();
3860 for (ToolChain::path_list::const_iterator i = Paths.begin(), e = Paths.end();
3861 i != e; ++i)
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003862 CmdArgs.push_back(Args.MakeArgString(StringRef("-L") + *i));
Daniel Dunbard854c8d2009-04-01 19:36:32 +00003863 Args.AddAllArgs(CmdArgs, options::OPT_T_Group);
3864 Args.AddAllArgs(CmdArgs, options::OPT_e);
David Chisnall589a4942010-08-15 22:58:12 +00003865 Args.AddAllArgs(CmdArgs, options::OPT_s);
3866 Args.AddAllArgs(CmdArgs, options::OPT_t);
3867 Args.AddAllArgs(CmdArgs, options::OPT_Z_Flag);
3868 Args.AddAllArgs(CmdArgs, options::OPT_r);
Daniel Dunbard854c8d2009-04-01 19:36:32 +00003869
Daniel Dunbar54423b22010-09-17 00:24:54 +00003870 AddLinkerInputs(getToolChain(), Inputs, Args, CmdArgs);
Daniel Dunbard854c8d2009-04-01 19:36:32 +00003871
3872 if (!Args.hasArg(options::OPT_nostdlib) &&
3873 !Args.hasArg(options::OPT_nodefaultlibs)) {
Daniel Dunbar4b8ef282010-02-17 08:07:51 +00003874 if (D.CCCIsCXX) {
Daniel Dunbar3f7796f2010-09-17 01:20:05 +00003875 getToolChain().AddCXXStdlibLibArgs(Args, CmdArgs);
Roman Divacky66f22762011-02-10 16:59:40 +00003876 if (Args.hasArg(options::OPT_pg))
3877 CmdArgs.push_back("-lm_p");
3878 else
3879 CmdArgs.push_back("-lm");
Daniel Dunbar4b8ef282010-02-17 08:07:51 +00003880 }
Daniel Dunbard854c8d2009-04-01 19:36:32 +00003881 // FIXME: For some reason GCC passes -lgcc and -lgcc_s before adding
3882 // the default system libraries. Just mimic this for now.
Roman Divacky66f22762011-02-10 16:59:40 +00003883 if (Args.hasArg(options::OPT_pg))
3884 CmdArgs.push_back("-lgcc_p");
3885 else
3886 CmdArgs.push_back("-lgcc");
Daniel Dunbard854c8d2009-04-01 19:36:32 +00003887 if (Args.hasArg(options::OPT_static)) {
3888 CmdArgs.push_back("-lgcc_eh");
Roman Divacky66f22762011-02-10 16:59:40 +00003889 } else if (Args.hasArg(options::OPT_pg)) {
3890 CmdArgs.push_back("-lgcc_eh_p");
Daniel Dunbard854c8d2009-04-01 19:36:32 +00003891 } else {
3892 CmdArgs.push_back("--as-needed");
3893 CmdArgs.push_back("-lgcc_s");
3894 CmdArgs.push_back("--no-as-needed");
3895 }
3896
Matt Beaumont-Gay1fe49152011-02-10 20:35:01 +00003897 if (Args.hasArg(options::OPT_pthread)) {
Roman Divacky66f22762011-02-10 16:59:40 +00003898 if (Args.hasArg(options::OPT_pg))
3899 CmdArgs.push_back("-lpthread_p");
3900 else
3901 CmdArgs.push_back("-lpthread");
Matt Beaumont-Gay1fe49152011-02-10 20:35:01 +00003902 }
Daniel Dunbard854c8d2009-04-01 19:36:32 +00003903
Roman Divacky66f22762011-02-10 16:59:40 +00003904 if (Args.hasArg(options::OPT_pg)) {
3905 if (Args.hasArg(options::OPT_shared))
3906 CmdArgs.push_back("-lc");
3907 else
3908 CmdArgs.push_back("-lc_p");
3909 CmdArgs.push_back("-lgcc_p");
3910 } else {
3911 CmdArgs.push_back("-lc");
3912 CmdArgs.push_back("-lgcc");
3913 }
3914
Daniel Dunbard854c8d2009-04-01 19:36:32 +00003915 if (Args.hasArg(options::OPT_static)) {
3916 CmdArgs.push_back("-lgcc_eh");
Roman Divacky66f22762011-02-10 16:59:40 +00003917 } else if (Args.hasArg(options::OPT_pg)) {
3918 CmdArgs.push_back("-lgcc_eh_p");
Daniel Dunbard854c8d2009-04-01 19:36:32 +00003919 } else {
3920 CmdArgs.push_back("--as-needed");
3921 CmdArgs.push_back("-lgcc_s");
3922 CmdArgs.push_back("--no-as-needed");
3923 }
3924 }
3925
3926 if (!Args.hasArg(options::OPT_nostdlib) &&
3927 !Args.hasArg(options::OPT_nostartfiles)) {
3928 if (!Args.hasArg(options::OPT_shared))
Daniel Dunbar9c3ed5f2010-07-14 18:46:23 +00003929 CmdArgs.push_back(Args.MakeArgString(getToolChain().GetFilePath(
Chris Lattner3e2ee142010-07-07 16:01:42 +00003930 "crtend.o")));
Daniel Dunbard854c8d2009-04-01 19:36:32 +00003931 else
Daniel Dunbar9c3ed5f2010-07-14 18:46:23 +00003932 CmdArgs.push_back(Args.MakeArgString(getToolChain().GetFilePath(
Chris Lattner3e2ee142010-07-07 16:01:42 +00003933 "crtendS.o")));
Daniel Dunbar9c3ed5f2010-07-14 18:46:23 +00003934 CmdArgs.push_back(Args.MakeArgString(getToolChain().GetFilePath(
Chris Lattner3e2ee142010-07-07 16:01:42 +00003935 "crtn.o")));
Daniel Dunbard854c8d2009-04-01 19:36:32 +00003936 }
3937
Bill Wendling08760582011-06-27 19:15:03 +00003938 addProfileRT(getToolChain(), Args, CmdArgs, getToolChain().getTriple());
Nick Lewycky82fe5f42011-05-24 21:54:59 +00003939
Daniel Dunbard067f7f2009-04-08 23:54:23 +00003940 const char *Exec =
Daniel Dunbar9c3ed5f2010-07-14 18:46:23 +00003941 Args.MakeArgString(getToolChain().GetProgramPath("ld"));
Daniel Dunbar7fbaf532010-08-02 02:38:28 +00003942 C.addCommand(new Command(JA, *this, Exec, CmdArgs));
Daniel Dunbard854c8d2009-04-01 19:36:32 +00003943}
Daniel Dunbarcc912342009-05-02 18:28:39 +00003944
Benjamin Kramer24f1d3e2011-02-02 18:59:27 +00003945void netbsd::Assemble::ConstructJob(Compilation &C, const JobAction &JA,
3946 const InputInfo &Output,
3947 const InputInfoList &Inputs,
3948 const ArgList &Args,
3949 const char *LinkingOutput) const {
3950 ArgStringList CmdArgs;
3951
3952 // When building 32-bit code on NetBSD/amd64, we have to explicitly
3953 // instruct as in the base system to assemble 32-bit code.
Joerg Sonnenberger637603a2011-05-16 13:35:02 +00003954 if (ToolTriple.getArch() == llvm::Triple::x86_64 &&
3955 getToolChain().getArch() == llvm::Triple::x86)
Benjamin Kramer24f1d3e2011-02-02 18:59:27 +00003956 CmdArgs.push_back("--32");
3957
3958
3959 // Set byte order explicitly
3960 if (getToolChain().getArchName() == "mips")
3961 CmdArgs.push_back("-EB");
3962 else if (getToolChain().getArchName() == "mipsel")
3963 CmdArgs.push_back("-EL");
3964
3965 Args.AddAllArgValues(CmdArgs, options::OPT_Wa_COMMA,
3966 options::OPT_Xassembler);
3967
3968 CmdArgs.push_back("-o");
3969 CmdArgs.push_back(Output.getFilename());
3970
3971 for (InputInfoList::const_iterator
3972 it = Inputs.begin(), ie = Inputs.end(); it != ie; ++it) {
3973 const InputInfo &II = *it;
3974 CmdArgs.push_back(II.getFilename());
3975 }
3976
Joerg Sonnenbergerfcc3ec92011-03-21 14:01:40 +00003977 const char *Exec = Args.MakeArgString(FindTargetProgramPath(getToolChain(),
Joerg Sonnenberger637603a2011-05-16 13:35:02 +00003978 ToolTriple.getTriple(),
3979 "as"));
Benjamin Kramer24f1d3e2011-02-02 18:59:27 +00003980 C.addCommand(new Command(JA, *this, Exec, CmdArgs));
3981}
3982
3983void netbsd::Link::ConstructJob(Compilation &C, const JobAction &JA,
3984 const InputInfo &Output,
3985 const InputInfoList &Inputs,
3986 const ArgList &Args,
3987 const char *LinkingOutput) const {
3988 const Driver &D = getToolChain().getDriver();
3989 ArgStringList CmdArgs;
3990
Joerg Sonnenberger6165ab12011-03-21 13:51:29 +00003991 if (!D.SysRoot.empty())
3992 CmdArgs.push_back(Args.MakeArgString("--sysroot=" + D.SysRoot));
3993
Benjamin Kramer24f1d3e2011-02-02 18:59:27 +00003994 if (Args.hasArg(options::OPT_static)) {
3995 CmdArgs.push_back("-Bstatic");
3996 } else {
3997 if (Args.hasArg(options::OPT_rdynamic))
3998 CmdArgs.push_back("-export-dynamic");
3999 CmdArgs.push_back("--eh-frame-hdr");
4000 if (Args.hasArg(options::OPT_shared)) {
4001 CmdArgs.push_back("-Bshareable");
4002 } else {
4003 CmdArgs.push_back("-dynamic-linker");
4004 CmdArgs.push_back("/libexec/ld.elf_so");
4005 }
4006 }
4007
4008 // When building 32-bit code on NetBSD/amd64, we have to explicitly
4009 // instruct ld in the base system to link 32-bit code.
Joerg Sonnenberger637603a2011-05-16 13:35:02 +00004010 if (ToolTriple.getArch() == llvm::Triple::x86_64 &&
4011 getToolChain().getArch() == llvm::Triple::x86) {
Benjamin Kramer24f1d3e2011-02-02 18:59:27 +00004012 CmdArgs.push_back("-m");
4013 CmdArgs.push_back("elf_i386");
4014 }
4015
4016 if (Output.isFilename()) {
4017 CmdArgs.push_back("-o");
4018 CmdArgs.push_back(Output.getFilename());
4019 } else {
4020 assert(Output.isNothing() && "Invalid output.");
4021 }
4022
4023 if (!Args.hasArg(options::OPT_nostdlib) &&
4024 !Args.hasArg(options::OPT_nostartfiles)) {
4025 if (!Args.hasArg(options::OPT_shared)) {
4026 CmdArgs.push_back(Args.MakeArgString(
4027 getToolChain().GetFilePath("crt0.o")));
4028 CmdArgs.push_back(Args.MakeArgString(
4029 getToolChain().GetFilePath("crti.o")));
4030 CmdArgs.push_back(Args.MakeArgString(
4031 getToolChain().GetFilePath("crtbegin.o")));
4032 } else {
4033 CmdArgs.push_back(Args.MakeArgString(
4034 getToolChain().GetFilePath("crti.o")));
4035 CmdArgs.push_back(Args.MakeArgString(
4036 getToolChain().GetFilePath("crtbeginS.o")));
4037 }
4038 }
4039
4040 Args.AddAllArgs(CmdArgs, options::OPT_L);
4041 Args.AddAllArgs(CmdArgs, options::OPT_T_Group);
4042 Args.AddAllArgs(CmdArgs, options::OPT_e);
4043 Args.AddAllArgs(CmdArgs, options::OPT_s);
4044 Args.AddAllArgs(CmdArgs, options::OPT_t);
4045 Args.AddAllArgs(CmdArgs, options::OPT_Z_Flag);
4046 Args.AddAllArgs(CmdArgs, options::OPT_r);
4047
4048 AddLinkerInputs(getToolChain(), Inputs, Args, CmdArgs);
4049
4050 if (!Args.hasArg(options::OPT_nostdlib) &&
4051 !Args.hasArg(options::OPT_nodefaultlibs)) {
4052 if (D.CCCIsCXX) {
4053 getToolChain().AddCXXStdlibLibArgs(Args, CmdArgs);
4054 CmdArgs.push_back("-lm");
4055 }
4056 // FIXME: For some reason GCC passes -lgcc and -lgcc_s before adding
4057 // the default system libraries. Just mimic this for now.
Benjamin Kramer24f1d3e2011-02-02 18:59:27 +00004058 if (Args.hasArg(options::OPT_static)) {
4059 CmdArgs.push_back("-lgcc_eh");
4060 } else {
4061 CmdArgs.push_back("--as-needed");
4062 CmdArgs.push_back("-lgcc_s");
4063 CmdArgs.push_back("--no-as-needed");
4064 }
Joerg Sonnenberger87717772011-06-07 23:39:17 +00004065 CmdArgs.push_back("-lgcc");
Benjamin Kramer24f1d3e2011-02-02 18:59:27 +00004066
4067 if (Args.hasArg(options::OPT_pthread))
4068 CmdArgs.push_back("-lpthread");
4069 CmdArgs.push_back("-lc");
4070
4071 CmdArgs.push_back("-lgcc");
4072 if (Args.hasArg(options::OPT_static)) {
4073 CmdArgs.push_back("-lgcc_eh");
4074 } else {
4075 CmdArgs.push_back("--as-needed");
4076 CmdArgs.push_back("-lgcc_s");
4077 CmdArgs.push_back("--no-as-needed");
4078 }
4079 }
4080
4081 if (!Args.hasArg(options::OPT_nostdlib) &&
4082 !Args.hasArg(options::OPT_nostartfiles)) {
4083 if (!Args.hasArg(options::OPT_shared))
4084 CmdArgs.push_back(Args.MakeArgString(getToolChain().GetFilePath(
4085 "crtend.o")));
4086 else
4087 CmdArgs.push_back(Args.MakeArgString(getToolChain().GetFilePath(
4088 "crtendS.o")));
4089 CmdArgs.push_back(Args.MakeArgString(getToolChain().GetFilePath(
4090 "crtn.o")));
4091 }
4092
Bill Wendling08760582011-06-27 19:15:03 +00004093 addProfileRT(getToolChain(), Args, CmdArgs, getToolChain().getTriple());
Nick Lewycky82fe5f42011-05-24 21:54:59 +00004094
Joerg Sonnenbergerfcc3ec92011-03-21 14:01:40 +00004095 const char *Exec = Args.MakeArgString(FindTargetProgramPath(getToolChain(),
Joerg Sonnenberger637603a2011-05-16 13:35:02 +00004096 ToolTriple.getTriple(),
4097 "ld"));
Benjamin Kramer24f1d3e2011-02-02 18:59:27 +00004098 C.addCommand(new Command(JA, *this, Exec, CmdArgs));
4099}
4100
Rafael Espindola92b00932010-08-10 00:25:48 +00004101void linuxtools::Assemble::ConstructJob(Compilation &C, const JobAction &JA,
4102 const InputInfo &Output,
4103 const InputInfoList &Inputs,
4104 const ArgList &Args,
4105 const char *LinkingOutput) const {
4106 ArgStringList CmdArgs;
4107
4108 // Add --32/--64 to make sure we get the format we want.
4109 // This is incomplete
4110 if (getToolChain().getArch() == llvm::Triple::x86) {
4111 CmdArgs.push_back("--32");
4112 } else if (getToolChain().getArch() == llvm::Triple::x86_64) {
4113 CmdArgs.push_back("--64");
4114 } else if (getToolChain().getArch() == llvm::Triple::arm) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00004115 StringRef MArch = getToolChain().getArchName();
Rafael Espindola92b00932010-08-10 00:25:48 +00004116 if (MArch == "armv7" || MArch == "armv7a" || MArch == "armv7-a")
4117 CmdArgs.push_back("-mfpu=neon");
4118 }
4119
4120 Args.AddAllArgValues(CmdArgs, options::OPT_Wa_COMMA,
4121 options::OPT_Xassembler);
4122
4123 CmdArgs.push_back("-o");
4124 CmdArgs.push_back(Output.getFilename());
4125
4126 for (InputInfoList::const_iterator
4127 it = Inputs.begin(), ie = Inputs.end(); it != ie; ++it) {
4128 const InputInfo &II = *it;
4129 CmdArgs.push_back(II.getFilename());
4130 }
4131
4132 const char *Exec =
4133 Args.MakeArgString(getToolChain().GetProgramPath("as"));
4134 C.addCommand(new Command(JA, *this, Exec, CmdArgs));
4135}
4136
Rafael Espindolac8f008f2010-11-07 20:14:31 +00004137void linuxtools::Link::ConstructJob(Compilation &C, const JobAction &JA,
4138 const InputInfo &Output,
4139 const InputInfoList &Inputs,
4140 const ArgList &Args,
4141 const char *LinkingOutput) const {
4142 const toolchains::Linux& ToolChain =
4143 static_cast<const toolchains::Linux&>(getToolChain());
4144 const Driver &D = ToolChain.getDriver();
4145 ArgStringList CmdArgs;
4146
Rafael Espindolad1002f62010-11-15 18:28:16 +00004147 // Silence warning for "clang -g foo.o -o foo"
4148 Args.ClaimAllArgs(options::OPT_g_Group);
Rafael Espindolad95a8122011-03-01 05:25:27 +00004149 // and "clang -emit-llvm foo.o -o foo"
4150 Args.ClaimAllArgs(options::OPT_emit_llvm);
Rafael Espindolaf92614c2010-11-17 20:37:10 +00004151 // and for "clang -g foo.o -o foo". Other warning options are already
4152 // handled somewhere else.
4153 Args.ClaimAllArgs(options::OPT_w);
Rafael Espindolad1002f62010-11-15 18:28:16 +00004154
Joerg Sonnenberger6165ab12011-03-21 13:51:29 +00004155 if (!D.SysRoot.empty())
4156 CmdArgs.push_back(Args.MakeArgString("--sysroot=" + D.SysRoot));
Rafael Espindolac8f008f2010-11-07 20:14:31 +00004157
Rafael Espindolad47ac232010-11-17 22:26:15 +00004158 if (Args.hasArg(options::OPT_pie))
4159 CmdArgs.push_back("-pie");
4160
Rafael Espindola1c76c592010-11-07 22:57:16 +00004161 if (Args.hasArg(options::OPT_rdynamic))
4162 CmdArgs.push_back("-export-dynamic");
4163
Rafael Espindola34d77dc2010-11-11 19:34:42 +00004164 if (Args.hasArg(options::OPT_s))
4165 CmdArgs.push_back("-s");
4166
Rafael Espindolac8f008f2010-11-07 20:14:31 +00004167 for (std::vector<std::string>::const_iterator i = ToolChain.ExtraOpts.begin(),
4168 e = ToolChain.ExtraOpts.end();
4169 i != e; ++i)
4170 CmdArgs.push_back(i->c_str());
4171
4172 if (!Args.hasArg(options::OPT_static)) {
4173 CmdArgs.push_back("--eh-frame-hdr");
4174 }
4175
4176 CmdArgs.push_back("-m");
4177 if (ToolChain.getArch() == llvm::Triple::x86)
4178 CmdArgs.push_back("elf_i386");
Eric Christopher84fbdb42011-08-19 00:30:14 +00004179 else if (ToolChain.getArch() == llvm::Triple::arm
Douglas Gregord9bb1522011-03-06 19:11:49 +00004180 || ToolChain.getArch() == llvm::Triple::thumb)
Rafael Espindolac8f008f2010-11-07 20:14:31 +00004181 CmdArgs.push_back("armelf_linux_eabi");
Ted Kremenek43d47cc2011-04-05 22:04:27 +00004182 else if (ToolChain.getArch() == llvm::Triple::ppc)
4183 CmdArgs.push_back("elf32ppclinux");
4184 else if (ToolChain.getArch() == llvm::Triple::ppc64)
4185 CmdArgs.push_back("elf64ppc");
Rafael Espindolac8f008f2010-11-07 20:14:31 +00004186 else
4187 CmdArgs.push_back("elf_x86_64");
4188
4189 if (Args.hasArg(options::OPT_static)) {
Douglas Gregord9bb1522011-03-06 19:11:49 +00004190 if (ToolChain.getArch() == llvm::Triple::arm
4191 || ToolChain.getArch() == llvm::Triple::thumb)
Rafael Espindolac8f008f2010-11-07 20:14:31 +00004192 CmdArgs.push_back("-Bstatic");
4193 else
4194 CmdArgs.push_back("-static");
4195 } else if (Args.hasArg(options::OPT_shared)) {
4196 CmdArgs.push_back("-shared");
4197 }
4198
4199 if (ToolChain.getArch() == llvm::Triple::arm ||
Douglas Gregord9bb1522011-03-06 19:11:49 +00004200 ToolChain.getArch() == llvm::Triple::thumb ||
Rafael Espindolac8f008f2010-11-07 20:14:31 +00004201 (!Args.hasArg(options::OPT_static) &&
4202 !Args.hasArg(options::OPT_shared))) {
4203 CmdArgs.push_back("-dynamic-linker");
4204 if (ToolChain.getArch() == llvm::Triple::x86)
4205 CmdArgs.push_back("/lib/ld-linux.so.2");
Douglas Gregord9bb1522011-03-06 19:11:49 +00004206 else if (ToolChain.getArch() == llvm::Triple::arm ||
4207 ToolChain.getArch() == llvm::Triple::thumb)
Rafael Espindolac8f008f2010-11-07 20:14:31 +00004208 CmdArgs.push_back("/lib/ld-linux.so.3");
Ted Kremenek43d47cc2011-04-05 22:04:27 +00004209 else if (ToolChain.getArch() == llvm::Triple::ppc)
Chris Lattner20b90d02011-04-11 21:15:37 +00004210 CmdArgs.push_back("/lib/ld.so.1");
Ted Kremenek43d47cc2011-04-05 22:04:27 +00004211 else if (ToolChain.getArch() == llvm::Triple::ppc64)
Chris Lattner20b90d02011-04-11 21:15:37 +00004212 CmdArgs.push_back("/lib64/ld64.so.1");
Rafael Espindolac8f008f2010-11-07 20:14:31 +00004213 else
4214 CmdArgs.push_back("/lib64/ld-linux-x86-64.so.2");
4215 }
4216
4217 CmdArgs.push_back("-o");
4218 CmdArgs.push_back(Output.getFilename());
4219
Rafael Espindola81937ec2010-12-01 01:52:43 +00004220 if (!Args.hasArg(options::OPT_nostdlib) &&
4221 !Args.hasArg(options::OPT_nostartfiles)) {
Rafael Espindolad47ac232010-11-17 22:26:15 +00004222 const char *crt1 = NULL;
4223 if (!Args.hasArg(options::OPT_shared)){
4224 if (Args.hasArg(options::OPT_pie))
4225 crt1 = "Scrt1.o";
4226 else
4227 crt1 = "crt1.o";
4228 }
4229 if (crt1)
4230 CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath(crt1)));
Rafael Espindolac8f008f2010-11-07 20:14:31 +00004231
Rafael Espindola9aa60e92010-11-12 03:00:39 +00004232 CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath("crti.o")));
Rafael Espindolac8f008f2010-11-07 20:14:31 +00004233
Rafael Espindola9aa60e92010-11-12 03:00:39 +00004234 const char *crtbegin;
4235 if (Args.hasArg(options::OPT_static))
4236 crtbegin = "crtbeginT.o";
Rafael Espindolad47ac232010-11-17 22:26:15 +00004237 else if (Args.hasArg(options::OPT_shared) || Args.hasArg(options::OPT_pie))
Rafael Espindola9aa60e92010-11-12 03:00:39 +00004238 crtbegin = "crtbeginS.o";
4239 else
4240 crtbegin = "crtbegin.o";
4241 CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath(crtbegin)));
4242 }
Rafael Espindolac8f008f2010-11-07 20:14:31 +00004243
4244 Args.AddAllArgs(CmdArgs, options::OPT_L);
4245
4246 const ToolChain::path_list Paths = ToolChain.getFilePaths();
4247
Roman Divackyee8188a2011-03-01 17:53:14 +00004248 for (ToolChain::path_list::const_iterator i = Paths.begin(), e = Paths.end();
4249 i != e; ++i)
Chris Lattner0e62c1c2011-07-23 10:55:15 +00004250 CmdArgs.push_back(Args.MakeArgString(StringRef("-L") + *i));
Rafael Espindolac8f008f2010-11-07 20:14:31 +00004251
4252 AddLinkerInputs(ToolChain, Inputs, Args, CmdArgs);
4253
Rafael Espindola9aa60e92010-11-12 03:00:39 +00004254 if (D.CCCIsCXX && !Args.hasArg(options::OPT_nostdlib)) {
Rafael Espindolac8f008f2010-11-07 20:14:31 +00004255 ToolChain.AddCXXStdlibLibArgs(Args, CmdArgs);
4256 CmdArgs.push_back("-lm");
4257 }
4258
Rafael Espindola9aa60e92010-11-12 03:00:39 +00004259 if (!Args.hasArg(options::OPT_nostdlib)) {
Nick Lewycky97864da2011-06-04 06:27:06 +00004260 if (Args.hasArg(options::OPT_static))
4261 CmdArgs.push_back("--start-group");
4262
Rafael Espindola9aa60e92010-11-12 03:00:39 +00004263 if (!D.CCCIsCXX)
Rafael Espindolac8f008f2010-11-07 20:14:31 +00004264 CmdArgs.push_back("-lgcc");
Rafael Espindola9aa60e92010-11-12 03:00:39 +00004265
4266 if (Args.hasArg(options::OPT_static)) {
4267 if (D.CCCIsCXX)
4268 CmdArgs.push_back("-lgcc");
4269 } else {
4270 if (!D.CCCIsCXX)
4271 CmdArgs.push_back("--as-needed");
4272 CmdArgs.push_back("-lgcc_s");
4273 if (!D.CCCIsCXX)
4274 CmdArgs.push_back("--no-as-needed");
4275 }
4276
4277 if (Args.hasArg(options::OPT_static))
4278 CmdArgs.push_back("-lgcc_eh");
4279 else if (!Args.hasArg(options::OPT_shared) && D.CCCIsCXX)
4280 CmdArgs.push_back("-lgcc");
4281
4282 if (Args.hasArg(options::OPT_pthread) ||
4283 Args.hasArg(options::OPT_pthreads))
4284 CmdArgs.push_back("-lpthread");
4285
4286 CmdArgs.push_back("-lc");
4287
4288 if (Args.hasArg(options::OPT_static))
4289 CmdArgs.push_back("--end-group");
4290 else {
4291 if (!D.CCCIsCXX)
4292 CmdArgs.push_back("-lgcc");
4293
4294 if (!D.CCCIsCXX)
4295 CmdArgs.push_back("--as-needed");
4296 CmdArgs.push_back("-lgcc_s");
4297 if (!D.CCCIsCXX)
4298 CmdArgs.push_back("--no-as-needed");
4299
4300 if (!Args.hasArg(options::OPT_shared) && D.CCCIsCXX)
4301 CmdArgs.push_back("-lgcc");
4302 }
4303
Rafael Espindolad47ac232010-11-17 22:26:15 +00004304
Rafael Espindola81937ec2010-12-01 01:52:43 +00004305 if (!Args.hasArg(options::OPT_nostartfiles)) {
4306 const char *crtend;
4307 if (Args.hasArg(options::OPT_shared) || Args.hasArg(options::OPT_pie))
4308 crtend = "crtendS.o";
4309 else
4310 crtend = "crtend.o";
Rafael Espindola9aa60e92010-11-12 03:00:39 +00004311
Rafael Espindola81937ec2010-12-01 01:52:43 +00004312 CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath(crtend)));
4313 CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath("crtn.o")));
4314 }
Rafael Espindolac8f008f2010-11-07 20:14:31 +00004315 }
4316
Bill Wendling08760582011-06-27 19:15:03 +00004317 addProfileRT(getToolChain(), Args, CmdArgs, getToolChain().getTriple());
Nick Lewycky82fe5f42011-05-24 21:54:59 +00004318
Rafael Espindolac8f008f2010-11-07 20:14:31 +00004319 if (Args.hasArg(options::OPT_use_gold_plugin)) {
4320 CmdArgs.push_back("-plugin");
4321 std::string Plugin = ToolChain.getDriver().Dir + "/../lib/LLVMgold.so";
4322 CmdArgs.push_back(Args.MakeArgString(Plugin));
4323 }
4324
4325 C.addCommand(new Command(JA, *this, ToolChain.Linker.c_str(), CmdArgs));
4326}
Rafael Espindola92b00932010-08-10 00:25:48 +00004327
Chris Lattner3e2ee142010-07-07 16:01:42 +00004328void minix::Assemble::ConstructJob(Compilation &C, const JobAction &JA,
Daniel Dunbar7fbaf532010-08-02 02:38:28 +00004329 const InputInfo &Output,
4330 const InputInfoList &Inputs,
4331 const ArgList &Args,
4332 const char *LinkingOutput) const {
Chris Lattner3e2ee142010-07-07 16:01:42 +00004333 ArgStringList CmdArgs;
4334
4335 Args.AddAllArgValues(CmdArgs, options::OPT_Wa_COMMA,
4336 options::OPT_Xassembler);
4337
4338 CmdArgs.push_back("-o");
Daniel Dunbarb440f562010-08-02 02:38:21 +00004339 CmdArgs.push_back(Output.getFilename());
Chris Lattner3e2ee142010-07-07 16:01:42 +00004340
4341 for (InputInfoList::const_iterator
4342 it = Inputs.begin(), ie = Inputs.end(); it != ie; ++it) {
4343 const InputInfo &II = *it;
Daniel Dunbarb440f562010-08-02 02:38:21 +00004344 CmdArgs.push_back(II.getFilename());
Chris Lattner3e2ee142010-07-07 16:01:42 +00004345 }
4346
4347 const char *Exec =
Daniel Dunbar9c3ed5f2010-07-14 18:46:23 +00004348 Args.MakeArgString(getToolChain().GetProgramPath("gas"));
Daniel Dunbar7fbaf532010-08-02 02:38:28 +00004349 C.addCommand(new Command(JA, *this, Exec, CmdArgs));
Chris Lattner3e2ee142010-07-07 16:01:42 +00004350}
4351
4352void minix::Link::ConstructJob(Compilation &C, const JobAction &JA,
Daniel Dunbar7fbaf532010-08-02 02:38:28 +00004353 const InputInfo &Output,
4354 const InputInfoList &Inputs,
4355 const ArgList &Args,
4356 const char *LinkingOutput) const {
Chris Lattner3e2ee142010-07-07 16:01:42 +00004357 const Driver &D = getToolChain().getDriver();
4358 ArgStringList CmdArgs;
4359
Daniel Dunbarb440f562010-08-02 02:38:21 +00004360 if (Output.isFilename()) {
Chris Lattner3e2ee142010-07-07 16:01:42 +00004361 CmdArgs.push_back("-o");
4362 CmdArgs.push_back(Output.getFilename());
4363 } else {
4364 assert(Output.isNothing() && "Invalid output.");
4365 }
4366
4367 if (!Args.hasArg(options::OPT_nostdlib) &&
4368 !Args.hasArg(options::OPT_nostartfiles))
Daniel Dunbar9c3ed5f2010-07-14 18:46:23 +00004369 CmdArgs.push_back(Args.MakeArgString(getToolChain().GetFilePath(
Chris Lattner3e2ee142010-07-07 16:01:42 +00004370 "/usr/gnu/lib/crtso.o")));
4371
4372 Args.AddAllArgs(CmdArgs, options::OPT_L);
4373 Args.AddAllArgs(CmdArgs, options::OPT_T_Group);
4374 Args.AddAllArgs(CmdArgs, options::OPT_e);
4375
Daniel Dunbar54423b22010-09-17 00:24:54 +00004376 AddLinkerInputs(getToolChain(), Inputs, Args, CmdArgs);
Chris Lattner3e2ee142010-07-07 16:01:42 +00004377
4378 if (!Args.hasArg(options::OPT_nostdlib) &&
4379 !Args.hasArg(options::OPT_nodefaultlibs)) {
4380 if (D.CCCIsCXX) {
Daniel Dunbar3f7796f2010-09-17 01:20:05 +00004381 getToolChain().AddCXXStdlibLibArgs(Args, CmdArgs);
Chris Lattner3e2ee142010-07-07 16:01:42 +00004382 CmdArgs.push_back("-lm");
4383 }
4384
4385 if (Args.hasArg(options::OPT_pthread))
4386 CmdArgs.push_back("-lpthread");
4387 CmdArgs.push_back("-lc");
4388 CmdArgs.push_back("-lgcc");
4389 CmdArgs.push_back("-L/usr/gnu/lib");
4390 // FIXME: fill in the correct search path for the final
4391 // support libraries.
4392 CmdArgs.push_back("-L/usr/gnu/lib/gcc/i686-pc-minix/4.4.3");
4393 }
4394
4395 if (!Args.hasArg(options::OPT_nostdlib) &&
4396 !Args.hasArg(options::OPT_nostartfiles)) {
Daniel Dunbar9c3ed5f2010-07-14 18:46:23 +00004397 CmdArgs.push_back(Args.MakeArgString(getToolChain().GetFilePath(
Chris Lattner3e2ee142010-07-07 16:01:42 +00004398 "/usr/gnu/lib/libend.a")));
4399 }
4400
Bill Wendling08760582011-06-27 19:15:03 +00004401 addProfileRT(getToolChain(), Args, CmdArgs, getToolChain().getTriple());
Nick Lewycky82fe5f42011-05-24 21:54:59 +00004402
Chris Lattner3e2ee142010-07-07 16:01:42 +00004403 const char *Exec =
Daniel Dunbar9c3ed5f2010-07-14 18:46:23 +00004404 Args.MakeArgString(getToolChain().GetProgramPath("/usr/gnu/bin/gld"));
Daniel Dunbar7fbaf532010-08-02 02:38:28 +00004405 C.addCommand(new Command(JA, *this, Exec, CmdArgs));
Chris Lattner3e2ee142010-07-07 16:01:42 +00004406}
4407
Daniel Dunbarcc912342009-05-02 18:28:39 +00004408/// DragonFly Tools
4409
4410// For now, DragonFly Assemble does just about the same as for
4411// FreeBSD, but this may change soon.
4412void dragonfly::Assemble::ConstructJob(Compilation &C, const JobAction &JA,
Daniel Dunbar7fbaf532010-08-02 02:38:28 +00004413 const InputInfo &Output,
Daniel Dunbar1c7577c2009-11-04 06:24:38 +00004414 const InputInfoList &Inputs,
4415 const ArgList &Args,
4416 const char *LinkingOutput) const {
Daniel Dunbarcc912342009-05-02 18:28:39 +00004417 ArgStringList CmdArgs;
4418
4419 // When building 32-bit code on DragonFly/pc64, we have to explicitly
4420 // instruct as in the base system to assemble 32-bit code.
4421 if (getToolChain().getArchName() == "i386")
4422 CmdArgs.push_back("--32");
4423
4424 Args.AddAllArgValues(CmdArgs, options::OPT_Wa_COMMA,
4425 options::OPT_Xassembler);
4426
4427 CmdArgs.push_back("-o");
Daniel Dunbarb440f562010-08-02 02:38:21 +00004428 CmdArgs.push_back(Output.getFilename());
Daniel Dunbarcc912342009-05-02 18:28:39 +00004429
4430 for (InputInfoList::const_iterator
4431 it = Inputs.begin(), ie = Inputs.end(); it != ie; ++it) {
4432 const InputInfo &II = *it;
Daniel Dunbarb440f562010-08-02 02:38:21 +00004433 CmdArgs.push_back(II.getFilename());
Daniel Dunbarcc912342009-05-02 18:28:39 +00004434 }
4435
4436 const char *Exec =
Daniel Dunbar9c3ed5f2010-07-14 18:46:23 +00004437 Args.MakeArgString(getToolChain().GetProgramPath("as"));
Daniel Dunbar7fbaf532010-08-02 02:38:28 +00004438 C.addCommand(new Command(JA, *this, Exec, CmdArgs));
Daniel Dunbarcc912342009-05-02 18:28:39 +00004439}
4440
4441void dragonfly::Link::ConstructJob(Compilation &C, const JobAction &JA,
Daniel Dunbar7fbaf532010-08-02 02:38:28 +00004442 const InputInfo &Output,
4443 const InputInfoList &Inputs,
4444 const ArgList &Args,
4445 const char *LinkingOutput) const {
Daniel Dunbar083edf72009-12-21 18:54:17 +00004446 const Driver &D = getToolChain().getDriver();
Daniel Dunbarcc912342009-05-02 18:28:39 +00004447 ArgStringList CmdArgs;
4448
Joerg Sonnenberger6165ab12011-03-21 13:51:29 +00004449 if (!D.SysRoot.empty())
4450 CmdArgs.push_back(Args.MakeArgString("--sysroot=" + D.SysRoot));
4451
Daniel Dunbarcc912342009-05-02 18:28:39 +00004452 if (Args.hasArg(options::OPT_static)) {
4453 CmdArgs.push_back("-Bstatic");
4454 } else {
4455 if (Args.hasArg(options::OPT_shared))
4456 CmdArgs.push_back("-Bshareable");
4457 else {
4458 CmdArgs.push_back("-dynamic-linker");
4459 CmdArgs.push_back("/usr/libexec/ld-elf.so.2");
4460 }
4461 }
4462
4463 // When building 32-bit code on DragonFly/pc64, we have to explicitly
4464 // instruct ld in the base system to link 32-bit code.
4465 if (getToolChain().getArchName() == "i386") {
4466 CmdArgs.push_back("-m");
4467 CmdArgs.push_back("elf_i386");
4468 }
4469
Daniel Dunbarb440f562010-08-02 02:38:21 +00004470 if (Output.isFilename()) {
Daniel Dunbarcc912342009-05-02 18:28:39 +00004471 CmdArgs.push_back("-o");
4472 CmdArgs.push_back(Output.getFilename());
4473 } else {
4474 assert(Output.isNothing() && "Invalid output.");
4475 }
4476
4477 if (!Args.hasArg(options::OPT_nostdlib) &&
4478 !Args.hasArg(options::OPT_nostartfiles)) {
4479 if (!Args.hasArg(options::OPT_shared)) {
Chris Lattner3e2ee142010-07-07 16:01:42 +00004480 CmdArgs.push_back(
Daniel Dunbar9c3ed5f2010-07-14 18:46:23 +00004481 Args.MakeArgString(getToolChain().GetFilePath("crt1.o")));
Chris Lattner3e2ee142010-07-07 16:01:42 +00004482 CmdArgs.push_back(
Daniel Dunbar9c3ed5f2010-07-14 18:46:23 +00004483 Args.MakeArgString(getToolChain().GetFilePath("crti.o")));
Chris Lattner3e2ee142010-07-07 16:01:42 +00004484 CmdArgs.push_back(
Daniel Dunbar9c3ed5f2010-07-14 18:46:23 +00004485 Args.MakeArgString(getToolChain().GetFilePath("crtbegin.o")));
Daniel Dunbarcc912342009-05-02 18:28:39 +00004486 } else {
Chris Lattner3e2ee142010-07-07 16:01:42 +00004487 CmdArgs.push_back(
Daniel Dunbar9c3ed5f2010-07-14 18:46:23 +00004488 Args.MakeArgString(getToolChain().GetFilePath("crti.o")));
Chris Lattner3e2ee142010-07-07 16:01:42 +00004489 CmdArgs.push_back(
Daniel Dunbar9c3ed5f2010-07-14 18:46:23 +00004490 Args.MakeArgString(getToolChain().GetFilePath("crtbeginS.o")));
Daniel Dunbarcc912342009-05-02 18:28:39 +00004491 }
4492 }
4493
4494 Args.AddAllArgs(CmdArgs, options::OPT_L);
4495 Args.AddAllArgs(CmdArgs, options::OPT_T_Group);
4496 Args.AddAllArgs(CmdArgs, options::OPT_e);
4497
Daniel Dunbar54423b22010-09-17 00:24:54 +00004498 AddLinkerInputs(getToolChain(), Inputs, Args, CmdArgs);
Daniel Dunbarcc912342009-05-02 18:28:39 +00004499
4500 if (!Args.hasArg(options::OPT_nostdlib) &&
4501 !Args.hasArg(options::OPT_nodefaultlibs)) {
4502 // FIXME: GCC passes on -lgcc, -lgcc_pic and a whole lot of
4503 // rpaths
4504 CmdArgs.push_back("-L/usr/lib/gcc41");
4505
4506 if (!Args.hasArg(options::OPT_static)) {
4507 CmdArgs.push_back("-rpath");
4508 CmdArgs.push_back("/usr/lib/gcc41");
4509
4510 CmdArgs.push_back("-rpath-link");
4511 CmdArgs.push_back("/usr/lib/gcc41");
4512
4513 CmdArgs.push_back("-rpath");
4514 CmdArgs.push_back("/usr/lib");
4515
4516 CmdArgs.push_back("-rpath-link");
4517 CmdArgs.push_back("/usr/lib");
4518 }
4519
Rafael Espindola38360b32010-07-20 12:59:03 +00004520 if (D.CCCIsCXX) {
Daniel Dunbar3f7796f2010-09-17 01:20:05 +00004521 getToolChain().AddCXXStdlibLibArgs(Args, CmdArgs);
Rafael Espindola38360b32010-07-20 12:59:03 +00004522 CmdArgs.push_back("-lm");
4523 }
4524
Daniel Dunbarcc912342009-05-02 18:28:39 +00004525 if (Args.hasArg(options::OPT_shared)) {
4526 CmdArgs.push_back("-lgcc_pic");
4527 } else {
4528 CmdArgs.push_back("-lgcc");
4529 }
4530
4531
4532 if (Args.hasArg(options::OPT_pthread))
Mike Stump0a65b632009-10-31 20:11:46 +00004533 CmdArgs.push_back("-lpthread");
Daniel Dunbarcc912342009-05-02 18:28:39 +00004534
4535 if (!Args.hasArg(options::OPT_nolibc)) {
4536 CmdArgs.push_back("-lc");
4537 }
4538
4539 if (Args.hasArg(options::OPT_shared)) {
4540 CmdArgs.push_back("-lgcc_pic");
4541 } else {
4542 CmdArgs.push_back("-lgcc");
4543 }
4544 }
4545
4546 if (!Args.hasArg(options::OPT_nostdlib) &&
4547 !Args.hasArg(options::OPT_nostartfiles)) {
4548 if (!Args.hasArg(options::OPT_shared))
Chris Lattner3e2ee142010-07-07 16:01:42 +00004549 CmdArgs.push_back(Args.MakeArgString(
Daniel Dunbar9c3ed5f2010-07-14 18:46:23 +00004550 getToolChain().GetFilePath("crtend.o")));
Daniel Dunbarcc912342009-05-02 18:28:39 +00004551 else
Chris Lattner3e2ee142010-07-07 16:01:42 +00004552 CmdArgs.push_back(Args.MakeArgString(
Daniel Dunbar9c3ed5f2010-07-14 18:46:23 +00004553 getToolChain().GetFilePath("crtendS.o")));
Chris Lattner3e2ee142010-07-07 16:01:42 +00004554 CmdArgs.push_back(Args.MakeArgString(
Daniel Dunbar9c3ed5f2010-07-14 18:46:23 +00004555 getToolChain().GetFilePath("crtn.o")));
Daniel Dunbarcc912342009-05-02 18:28:39 +00004556 }
4557
Bill Wendling08760582011-06-27 19:15:03 +00004558 addProfileRT(getToolChain(), Args, CmdArgs, getToolChain().getTriple());
Nick Lewycky82fe5f42011-05-24 21:54:59 +00004559
Daniel Dunbarcc912342009-05-02 18:28:39 +00004560 const char *Exec =
Daniel Dunbar9c3ed5f2010-07-14 18:46:23 +00004561 Args.MakeArgString(getToolChain().GetProgramPath("ld"));
Daniel Dunbar7fbaf532010-08-02 02:38:28 +00004562 C.addCommand(new Command(JA, *this, Exec, CmdArgs));
Daniel Dunbarcc912342009-05-02 18:28:39 +00004563}
Michael J. Spencerb186bc32010-08-21 21:55:07 +00004564
4565void visualstudio::Link::ConstructJob(Compilation &C, const JobAction &JA,
4566 const InputInfo &Output,
4567 const InputInfoList &Inputs,
4568 const ArgList &Args,
4569 const char *LinkingOutput) const {
Michael J. Spencerb186bc32010-08-21 21:55:07 +00004570 ArgStringList CmdArgs;
4571
4572 if (Output.isFilename()) {
Daniel Dunbar2cc3f172010-09-17 00:45:02 +00004573 CmdArgs.push_back(Args.MakeArgString(std::string("-out:") +
4574 Output.getFilename()));
Michael J. Spencerb186bc32010-08-21 21:55:07 +00004575 } else {
4576 assert(Output.isNothing() && "Invalid output.");
4577 }
4578
4579 if (!Args.hasArg(options::OPT_nostdlib) &&
4580 !Args.hasArg(options::OPT_nostartfiles)) {
4581 CmdArgs.push_back("-defaultlib:libcmt");
4582 }
4583
4584 CmdArgs.push_back("-nologo");
4585
Daniel Dunbar54423b22010-09-17 00:24:54 +00004586 AddLinkerInputs(getToolChain(), Inputs, Args, CmdArgs);
Michael J. Spencerb186bc32010-08-21 21:55:07 +00004587
4588 const char *Exec =
Daniel Dunbar54423b22010-09-17 00:24:54 +00004589 Args.MakeArgString(getToolChain().GetProgramPath("link.exe"));
Michael J. Spencerb186bc32010-08-21 21:55:07 +00004590 C.addCommand(new Command(JA, *this, Exec, CmdArgs));
4591}